patch-2_6_7-vs1_9_1_12
[linux-2.6.git] / mm / memory.c
1 /*
2  *  linux/mm/memory.c
3  *
4  *  Copyright (C) 1991, 1992, 1993, 1994  Linus Torvalds
5  */
6
7 /*
8  * demand-loading started 01.12.91 - seems it is high on the list of
9  * things wanted, and it should be easy to implement. - Linus
10  */
11
12 /*
13  * Ok, demand-loading was easy, shared pages a little bit tricker. Shared
14  * pages started 02.12.91, seems to work. - Linus.
15  *
16  * Tested sharing by executing about 30 /bin/sh: under the old kernel it
17  * would have taken more than the 6M I have free, but it worked well as
18  * far as I could see.
19  *
20  * Also corrected some "invalidate()"s - I wasn't doing enough of them.
21  */
22
23 /*
24  * Real VM (paging to/from disk) started 18.12.91. Much more work and
25  * thought has to go into this. Oh, well..
26  * 19.12.91  -  works, somewhat. Sometimes I get faults, don't know why.
27  *              Found it. Everything seems to work now.
28  * 20.12.91  -  Ok, making the swap-device changeable like the root.
29  */
30
31 /*
32  * 05.04.94  -  Multi-page memory management added for v1.1.
33  *              Idea by Alex Bligh (alex@cconcepts.co.uk)
34  *
35  * 16.07.99  -  Support of BIGMEM added by Gerhard Wichert, Siemens AG
36  *              (Gerhard.Wichert@pdb.siemens.de)
37  */
38
39 #include <linux/kernel_stat.h>
40 #include <linux/mm.h>
41 #include <linux/hugetlb.h>
42 #include <linux/mman.h>
43 #include <linux/swap.h>
44 #include <linux/highmem.h>
45 #include <linux/pagemap.h>
46 #include <linux/rmap.h>
47 #include <linux/module.h>
48 #include <linux/init.h>
49
50 #include <asm/pgalloc.h>
51 #include <asm/uaccess.h>
52 #include <asm/tlb.h>
53 #include <asm/tlbflush.h>
54 #include <asm/pgtable.h>
55
56 #include <linux/swapops.h>
57 #include <linux/elf.h>
58
59 #ifndef CONFIG_DISCONTIGMEM
60 /* use the per-pgdat data instead for discontigmem - mbligh */
61 unsigned long max_mapnr;
62 struct page *mem_map;
63
64 EXPORT_SYMBOL(max_mapnr);
65 EXPORT_SYMBOL(mem_map);
66 #endif
67
68 unsigned long num_physpages;
69 void * high_memory;
70 struct page *highmem_start_page;
71
72 EXPORT_SYMBOL(num_physpages);
73 EXPORT_SYMBOL(highmem_start_page);
74 EXPORT_SYMBOL(high_memory);
75
76 /*
77  * We special-case the C-O-W ZERO_PAGE, because it's such
78  * a common occurrence (no need to read the page to know
79  * that it's zero - better for the cache and memory subsystem).
80  */
81 static inline void copy_cow_page(struct page * from, struct page * to, unsigned long address)
82 {
83         if (from == ZERO_PAGE(address)) {
84                 clear_user_highpage(to, address);
85                 return;
86         }
87         copy_user_highpage(to, from, address);
88 }
89
90 /*
91  * Note: this doesn't free the actual pages themselves. That
92  * has been handled earlier when unmapping all the memory regions.
93  */
94 static inline void free_one_pmd(struct mmu_gather *tlb, pmd_t * dir)
95 {
96         struct page *page;
97
98         if (pmd_none(*dir))
99                 return;
100         if (unlikely(pmd_bad(*dir))) {
101                 pmd_ERROR(*dir);
102                 pmd_clear(dir);
103                 return;
104         }
105         page = pmd_page(*dir);
106         pmd_clear(dir);
107         dec_page_state(nr_page_table_pages);
108         pte_free_tlb(tlb, page);
109 }
110
111 static inline void free_one_pgd(struct mmu_gather *tlb, pgd_t * dir)
112 {
113         int j;
114         pmd_t * pmd;
115
116         if (pgd_none(*dir))
117                 return;
118         if (unlikely(pgd_bad(*dir))) {
119                 pgd_ERROR(*dir);
120                 pgd_clear(dir);
121                 return;
122         }
123         pmd = pmd_offset(dir, 0);
124         pgd_clear(dir);
125         for (j = 0; j < PTRS_PER_PMD ; j++)
126                 free_one_pmd(tlb, pmd+j);
127         pmd_free_tlb(tlb, pmd);
128 }
129
130 /*
131  * This function clears all user-level page tables of a process - this
132  * is needed by execve(), so that old pages aren't in the way.
133  *
134  * Must be called with pagetable lock held.
135  */
136 void clear_page_tables(struct mmu_gather *tlb, unsigned long first, int nr)
137 {
138         pgd_t * page_dir = tlb->mm->pgd;
139
140         page_dir += first;
141         do {
142                 free_one_pgd(tlb, page_dir);
143                 page_dir++;
144         } while (--nr);
145 }
146
147 pte_t fastcall * pte_alloc_map(struct mm_struct *mm, pmd_t *pmd, unsigned long address)
148 {
149         if (!pmd_present(*pmd)) {
150                 struct page *new;
151
152                 spin_unlock(&mm->page_table_lock);
153                 new = pte_alloc_one(mm, address);
154                 spin_lock(&mm->page_table_lock);
155                 if (!new)
156                         return NULL;
157
158                 /*
159                  * Because we dropped the lock, we should re-check the
160                  * entry, as somebody else could have populated it..
161                  */
162                 if (pmd_present(*pmd)) {
163                         pte_free(new);
164                         goto out;
165                 }
166                 inc_page_state(nr_page_table_pages);
167                 pmd_populate(mm, pmd, new);
168         }
169 out:
170         return pte_offset_map(pmd, address);
171 }
172
173 pte_t fastcall * pte_alloc_kernel(struct mm_struct *mm, pmd_t *pmd, unsigned long address)
174 {
175         if (!pmd_present(*pmd)) {
176                 pte_t *new;
177
178                 spin_unlock(&mm->page_table_lock);
179                 new = pte_alloc_one_kernel(mm, address);
180                 spin_lock(&mm->page_table_lock);
181                 if (!new)
182                         return NULL;
183
184                 /*
185                  * Because we dropped the lock, we should re-check the
186                  * entry, as somebody else could have populated it..
187                  */
188                 if (pmd_present(*pmd)) {
189                         pte_free_kernel(new);
190                         goto out;
191                 }
192                 pmd_populate_kernel(mm, pmd, new);
193         }
194 out:
195         return pte_offset_kernel(pmd, address);
196 }
197 #define PTE_TABLE_MASK  ((PTRS_PER_PTE-1) * sizeof(pte_t))
198 #define PMD_TABLE_MASK  ((PTRS_PER_PMD-1) * sizeof(pmd_t))
199
200 /*
201  * copy one vm_area from one task to the other. Assumes the page tables
202  * already present in the new task to be cleared in the whole range
203  * covered by this vma.
204  *
205  * 08Jan98 Merged into one routine from several inline routines to reduce
206  *         variable count and make things faster. -jj
207  *
208  * dst->page_table_lock is held on entry and exit,
209  * but may be dropped within pmd_alloc() and pte_alloc_map().
210  */
211 int copy_page_range(struct mm_struct *dst, struct mm_struct *src,
212                         struct vm_area_struct *vma)
213 {
214         pgd_t * src_pgd, * dst_pgd;
215         unsigned long address = vma->vm_start;
216         unsigned long end = vma->vm_end;
217         unsigned long cow;
218
219         if (is_vm_hugetlb_page(vma))
220                 return copy_hugetlb_page_range(dst, src, vma);
221
222         cow = (vma->vm_flags & (VM_SHARED | VM_MAYWRITE)) == VM_MAYWRITE;
223         src_pgd = pgd_offset(src, address)-1;
224         dst_pgd = pgd_offset(dst, address)-1;
225
226         for (;;) {
227                 pmd_t * src_pmd, * dst_pmd;
228
229                 src_pgd++; dst_pgd++;
230                 
231                 /* copy_pmd_range */
232                 
233                 if (pgd_none(*src_pgd))
234                         goto skip_copy_pmd_range;
235                 if (unlikely(pgd_bad(*src_pgd))) {
236                         pgd_ERROR(*src_pgd);
237                         pgd_clear(src_pgd);
238 skip_copy_pmd_range:    address = (address + PGDIR_SIZE) & PGDIR_MASK;
239                         if (!address || (address >= end))
240                                 goto out;
241                         continue;
242                 }
243
244                 src_pmd = pmd_offset(src_pgd, address);
245                 dst_pmd = pmd_alloc(dst, dst_pgd, address);
246                 if (!dst_pmd)
247                         goto nomem;
248
249                 do {
250                         pte_t * src_pte, * dst_pte;
251                 
252                         /* copy_pte_range */
253                 
254                         if (pmd_none(*src_pmd))
255                                 goto skip_copy_pte_range;
256                         if (unlikely(pmd_bad(*src_pmd))) {
257                                 pmd_ERROR(*src_pmd);
258                                 pmd_clear(src_pmd);
259 skip_copy_pte_range:
260                                 address = (address + PMD_SIZE) & PMD_MASK;
261                                 if (address >= end)
262                                         goto out;
263                                 goto cont_copy_pmd_range;
264                         }
265
266                         dst_pte = pte_alloc_map(dst, dst_pmd, address);
267                         if (!dst_pte)
268                                 goto nomem;
269                         spin_lock(&src->page_table_lock);       
270                         src_pte = pte_offset_map_nested(src_pmd, address);
271                         do {
272                                 pte_t pte = *src_pte;
273                                 struct page *page;
274                                 unsigned long pfn;
275
276                                 if (!vx_rsspages_avail(dst, 1)) {
277                                         spin_unlock(&src->page_table_lock);
278                                         goto nomem;
279                                 }
280                                 /* copy_one_pte */
281
282                                 if (pte_none(pte))
283                                         goto cont_copy_pte_range_noset;
284                                 /* pte contains position in swap, so copy. */
285                                 if (!pte_present(pte)) {
286                                         if (!pte_file(pte))
287                                                 swap_duplicate(pte_to_swp_entry(pte));
288                                         set_pte(dst_pte, pte);
289                                         goto cont_copy_pte_range_noset;
290                                 }
291                                 pfn = pte_pfn(pte);
292                                 /* the pte points outside of valid memory, the
293                                  * mapping is assumed to be good, meaningful
294                                  * and not mapped via rmap - duplicate the
295                                  * mapping as is.
296                                  */
297                                 page = NULL;
298                                 if (pfn_valid(pfn)) 
299                                         page = pfn_to_page(pfn); 
300
301                                 if (!page || PageReserved(page)) {
302                                         set_pte(dst_pte, pte);
303                                         goto cont_copy_pte_range_noset;
304                                 }
305
306                                 /*
307                                  * If it's a COW mapping, write protect it both
308                                  * in the parent and the child
309                                  */
310                                 if (cow) {
311                                         ptep_set_wrprotect(src_pte);
312                                         pte = *src_pte;
313                                 }
314
315                                 /*
316                                  * If it's a shared mapping, mark it clean in
317                                  * the child
318                                  */
319                                 if (vma->vm_flags & VM_SHARED)
320                                         pte = pte_mkclean(pte);
321                                 pte = pte_mkold(pte);
322                                 get_page(page);
323                                 // dst->rss++;
324                                 vx_rsspages_inc(dst);
325                                 set_pte(dst_pte, pte);
326                                 page_dup_rmap(page);
327 cont_copy_pte_range_noset:
328                                 address += PAGE_SIZE;
329                                 if (address >= end) {
330                                         pte_unmap_nested(src_pte);
331                                         pte_unmap(dst_pte);
332                                         goto out_unlock;
333                                 }
334                                 src_pte++;
335                                 dst_pte++;
336                         } while ((unsigned long)src_pte & PTE_TABLE_MASK);
337                         pte_unmap_nested(src_pte-1);
338                         pte_unmap(dst_pte-1);
339                         spin_unlock(&src->page_table_lock);
340                         cond_resched_lock(&dst->page_table_lock);
341 cont_copy_pmd_range:
342                         src_pmd++;
343                         dst_pmd++;
344                 } while ((unsigned long)src_pmd & PMD_TABLE_MASK);
345         }
346 out_unlock:
347         spin_unlock(&src->page_table_lock);
348 out:
349         return 0;
350 nomem:
351         return -ENOMEM;
352 }
353
354 static void zap_pte_range(struct mmu_gather *tlb,
355                 pmd_t *pmd, unsigned long address,
356                 unsigned long size, struct zap_details *details)
357 {
358         unsigned long offset;
359         pte_t *ptep;
360
361         if (pmd_none(*pmd))
362                 return;
363         if (unlikely(pmd_bad(*pmd))) {
364                 pmd_ERROR(*pmd);
365                 pmd_clear(pmd);
366                 return;
367         }
368         ptep = pte_offset_map(pmd, address);
369         offset = address & ~PMD_MASK;
370         if (offset + size > PMD_SIZE)
371                 size = PMD_SIZE - offset;
372         size &= PAGE_MASK;
373         if (details && !details->check_mapping && !details->nonlinear_vma)
374                 details = NULL;
375         for (offset=0; offset < size; ptep++, offset += PAGE_SIZE) {
376                 pte_t pte = *ptep;
377                 if (pte_none(pte))
378                         continue;
379                 if (pte_present(pte)) {
380                         struct page *page = NULL;
381                         unsigned long pfn = pte_pfn(pte);
382                         if (pfn_valid(pfn)) {
383                                 page = pfn_to_page(pfn);
384                                 if (PageReserved(page))
385                                         page = NULL;
386                         }
387                         if (unlikely(details) && page) {
388                                 /*
389                                  * unmap_shared_mapping_pages() wants to
390                                  * invalidate cache without truncating:
391                                  * unmap shared but keep private pages.
392                                  */
393                                 if (details->check_mapping &&
394                                     details->check_mapping != page->mapping)
395                                         continue;
396                                 /*
397                                  * Each page->index must be checked when
398                                  * invalidating or truncating nonlinear.
399                                  */
400                                 if (details->nonlinear_vma &&
401                                     (page->index < details->first_index ||
402                                      page->index > details->last_index))
403                                         continue;
404                         }
405                         pte = ptep_get_and_clear(ptep);
406                         tlb_remove_tlb_entry(tlb, ptep, address+offset);
407                         if (unlikely(!page))
408                                 continue;
409                         if (unlikely(details) && details->nonlinear_vma
410                             && linear_page_index(details->nonlinear_vma,
411                                         address+offset) != page->index)
412                                 set_pte(ptep, pgoff_to_pte(page->index));
413                         if (pte_dirty(pte))
414                                 set_page_dirty(page);
415                         if (pte_young(pte) && page_mapping(page))
416                                 mark_page_accessed(page);
417                         tlb->freed++;
418                         page_remove_rmap(page);
419                         tlb_remove_page(tlb, page);
420                         continue;
421                 }
422                 /*
423                  * If details->check_mapping, we leave swap entries;
424                  * if details->nonlinear_vma, we leave file entries.
425                  */
426                 if (unlikely(details))
427                         continue;
428                 if (!pte_file(pte))
429                         free_swap_and_cache(pte_to_swp_entry(pte));
430                 pte_clear(ptep);
431         }
432         pte_unmap(ptep-1);
433 }
434
435 static void zap_pmd_range(struct mmu_gather *tlb,
436                 pgd_t * dir, unsigned long address,
437                 unsigned long size, struct zap_details *details)
438 {
439         pmd_t * pmd;
440         unsigned long end;
441
442         if (pgd_none(*dir))
443                 return;
444         if (unlikely(pgd_bad(*dir))) {
445                 pgd_ERROR(*dir);
446                 pgd_clear(dir);
447                 return;
448         }
449         pmd = pmd_offset(dir, address);
450         end = address + size;
451         if (end > ((address + PGDIR_SIZE) & PGDIR_MASK))
452                 end = ((address + PGDIR_SIZE) & PGDIR_MASK);
453         do {
454                 zap_pte_range(tlb, pmd, address, end - address, details);
455                 address = (address + PMD_SIZE) & PMD_MASK; 
456                 pmd++;
457         } while (address && (address < end));
458 }
459
460 static void unmap_page_range(struct mmu_gather *tlb,
461                 struct vm_area_struct *vma, unsigned long address,
462                 unsigned long end, struct zap_details *details)
463 {
464         pgd_t * dir;
465
466         BUG_ON(address >= end);
467         dir = pgd_offset(vma->vm_mm, address);
468         tlb_start_vma(tlb, vma);
469         do {
470                 zap_pmd_range(tlb, dir, address, end - address, details);
471                 address = (address + PGDIR_SIZE) & PGDIR_MASK;
472                 dir++;
473         } while (address && (address < end));
474         tlb_end_vma(tlb, vma);
475 }
476
477 /* Dispose of an entire struct mmu_gather per rescheduling point */
478 #if defined(CONFIG_SMP) && defined(CONFIG_PREEMPT)
479 #define ZAP_BLOCK_SIZE  (FREE_PTE_NR * PAGE_SIZE)
480 #endif
481
482 /* For UP, 256 pages at a time gives nice low latency */
483 #if !defined(CONFIG_SMP) && defined(CONFIG_PREEMPT)
484 #define ZAP_BLOCK_SIZE  (256 * PAGE_SIZE)
485 #endif
486
487 /* No preempt: go for improved straight-line efficiency */
488 #if !defined(CONFIG_PREEMPT)
489 #define ZAP_BLOCK_SIZE  (1024 * PAGE_SIZE)
490 #endif
491
492 /**
493  * unmap_vmas - unmap a range of memory covered by a list of vma's
494  * @tlbp: address of the caller's struct mmu_gather
495  * @mm: the controlling mm_struct
496  * @vma: the starting vma
497  * @start_addr: virtual address at which to start unmapping
498  * @end_addr: virtual address at which to end unmapping
499  * @nr_accounted: Place number of unmapped pages in vm-accountable vma's here
500  * @details: details of nonlinear truncation or shared cache invalidation
501  *
502  * Returns the number of vma's which were covered by the unmapping.
503  *
504  * Unmap all pages in the vma list.  Called under page_table_lock.
505  *
506  * We aim to not hold page_table_lock for too long (for scheduling latency
507  * reasons).  So zap pages in ZAP_BLOCK_SIZE bytecounts.  This means we need to
508  * return the ending mmu_gather to the caller.
509  *
510  * Only addresses between `start' and `end' will be unmapped.
511  *
512  * The VMA list must be sorted in ascending virtual address order.
513  *
514  * unmap_vmas() assumes that the caller will flush the whole unmapped address
515  * range after unmap_vmas() returns.  So the only responsibility here is to
516  * ensure that any thus-far unmapped pages are flushed before unmap_vmas()
517  * drops the lock and schedules.
518  */
519 int unmap_vmas(struct mmu_gather **tlbp, struct mm_struct *mm,
520                 struct vm_area_struct *vma, unsigned long start_addr,
521                 unsigned long end_addr, unsigned long *nr_accounted,
522                 struct zap_details *details)
523 {
524         unsigned long zap_bytes = ZAP_BLOCK_SIZE;
525         unsigned long tlb_start = 0;    /* For tlb_finish_mmu */
526         int tlb_start_valid = 0;
527         int ret = 0;
528         int atomic = details && details->atomic;
529
530         for ( ; vma && vma->vm_start < end_addr; vma = vma->vm_next) {
531                 unsigned long start;
532                 unsigned long end;
533
534                 start = max(vma->vm_start, start_addr);
535                 if (start >= vma->vm_end)
536                         continue;
537                 end = min(vma->vm_end, end_addr);
538                 if (end <= vma->vm_start)
539                         continue;
540
541                 if (vma->vm_flags & VM_ACCOUNT)
542                         *nr_accounted += (end - start) >> PAGE_SHIFT;
543
544                 ret++;
545                 while (start != end) {
546                         unsigned long block;
547
548                         if (!tlb_start_valid) {
549                                 tlb_start = start;
550                                 tlb_start_valid = 1;
551                         }
552
553                         if (is_vm_hugetlb_page(vma)) {
554                                 block = end - start;
555                                 unmap_hugepage_range(vma, start, end);
556                         } else {
557                                 block = min(zap_bytes, end - start);
558                                 unmap_page_range(*tlbp, vma, start,
559                                                 start + block, details);
560                         }
561
562                         start += block;
563                         zap_bytes -= block;
564                         if ((long)zap_bytes > 0)
565                                 continue;
566                         if (!atomic && need_resched()) {
567                                 int fullmm = tlb_is_full_mm(*tlbp);
568                                 tlb_finish_mmu(*tlbp, tlb_start, start);
569                                 cond_resched_lock(&mm->page_table_lock);
570                                 *tlbp = tlb_gather_mmu(mm, fullmm);
571                                 tlb_start_valid = 0;
572                         }
573                         zap_bytes = ZAP_BLOCK_SIZE;
574                 }
575         }
576         return ret;
577 }
578
579 /**
580  * zap_page_range - remove user pages in a given range
581  * @vma: vm_area_struct holding the applicable pages
582  * @address: starting address of pages to zap
583  * @size: number of bytes to zap
584  * @details: details of nonlinear truncation or shared cache invalidation
585  */
586 void zap_page_range(struct vm_area_struct *vma, unsigned long address,
587                 unsigned long size, struct zap_details *details)
588 {
589         struct mm_struct *mm = vma->vm_mm;
590         struct mmu_gather *tlb;
591         unsigned long end = address + size;
592         unsigned long nr_accounted = 0;
593
594         if (is_vm_hugetlb_page(vma)) {
595                 zap_hugepage_range(vma, address, size);
596                 return;
597         }
598
599         lru_add_drain();
600         spin_lock(&mm->page_table_lock);
601         tlb = tlb_gather_mmu(mm, 0);
602         unmap_vmas(&tlb, mm, vma, address, end, &nr_accounted, details);
603         tlb_finish_mmu(tlb, address, end);
604         spin_unlock(&mm->page_table_lock);
605 }
606
607 /*
608  * Do a quick page-table lookup for a single page.
609  * mm->page_table_lock must be held.
610  */
611 struct page *
612 follow_page(struct mm_struct *mm, unsigned long address, int write) 
613 {
614         pgd_t *pgd;
615         pmd_t *pmd;
616         pte_t *ptep, pte;
617         unsigned long pfn;
618         struct page *page;
619
620         page = follow_huge_addr(mm, address, write);
621         if (! IS_ERR(page))
622                 return page;
623
624         pgd = pgd_offset(mm, address);
625         if (pgd_none(*pgd) || unlikely(pgd_bad(*pgd)))
626                 goto out;
627
628         pmd = pmd_offset(pgd, address);
629         if (pmd_none(*pmd))
630                 goto out;
631         if (pmd_huge(*pmd))
632                 return follow_huge_pmd(mm, address, pmd, write);
633         if (unlikely(pmd_bad(*pmd)))
634                 goto out;
635
636         ptep = pte_offset_map(pmd, address);
637         if (!ptep)
638                 goto out;
639
640         pte = *ptep;
641         pte_unmap(ptep);
642         if (pte_present(pte)) {
643                 if (write && !pte_write(pte))
644                         goto out;
645                 pfn = pte_pfn(pte);
646                 if (pfn_valid(pfn)) {
647                         page = pfn_to_page(pfn);
648                         if (write && !pte_dirty(pte) && !PageDirty(page))
649                                 set_page_dirty(page);
650                         mark_page_accessed(page);
651                         return page;
652                 }
653         }
654
655 out:
656         return NULL;
657 }
658
659 /* 
660  * Given a physical address, is there a useful struct page pointing to
661  * it?  This may become more complex in the future if we start dealing
662  * with IO-aperture pages for direct-IO.
663  */
664
665 static inline struct page *get_page_map(struct page *page)
666 {
667         if (!pfn_valid(page_to_pfn(page)))
668                 return 0;
669         return page;
670 }
671
672
673 static inline int
674 untouched_anonymous_page(struct mm_struct* mm, struct vm_area_struct *vma,
675                          unsigned long address)
676 {
677         pgd_t *pgd;
678         pmd_t *pmd;
679
680         /* Check if the vma is for an anonymous mapping. */
681         if (vma->vm_ops && vma->vm_ops->nopage)
682                 return 0;
683
684         /* Check if page directory entry exists. */
685         pgd = pgd_offset(mm, address);
686         if (pgd_none(*pgd) || unlikely(pgd_bad(*pgd)))
687                 return 1;
688
689         /* Check if page middle directory entry exists. */
690         pmd = pmd_offset(pgd, address);
691         if (pmd_none(*pmd) || unlikely(pmd_bad(*pmd)))
692                 return 1;
693
694         /* There is a pte slot for 'address' in 'mm'. */
695         return 0;
696 }
697
698
699 int get_user_pages(struct task_struct *tsk, struct mm_struct *mm,
700                 unsigned long start, int len, int write, int force,
701                 struct page **pages, struct vm_area_struct **vmas)
702 {
703         int i;
704         unsigned int flags;
705
706         /* 
707          * Require read or write permissions.
708          * If 'force' is set, we only require the "MAY" flags.
709          */
710         flags = write ? (VM_WRITE | VM_MAYWRITE) : (VM_READ | VM_MAYREAD);
711         flags &= force ? (VM_MAYREAD | VM_MAYWRITE) : (VM_READ | VM_WRITE);
712         i = 0;
713
714         do {
715                 struct vm_area_struct * vma;
716
717                 vma = find_extend_vma(mm, start);
718                 if (!vma && in_gate_area(tsk, start)) {
719                         unsigned long pg = start & PAGE_MASK;
720                         struct vm_area_struct *gate_vma = get_gate_vma(tsk);
721                         pgd_t *pgd;
722                         pmd_t *pmd;
723                         pte_t *pte;
724                         if (write) /* user gate pages are read-only */
725                                 return i ? : -EFAULT;
726                         pgd = pgd_offset_k(pg);
727                         if (!pgd)
728                                 return i ? : -EFAULT;
729                         pmd = pmd_offset(pgd, pg);
730                         if (!pmd)
731                                 return i ? : -EFAULT;
732                         pte = pte_offset_kernel(pmd, pg);
733                         if (!pte || !pte_present(*pte))
734                                 return i ? : -EFAULT;
735                         if (pages) {
736                                 pages[i] = pte_page(*pte);
737                                 get_page(pages[i]);
738                         }
739                         if (vmas)
740                                 vmas[i] = gate_vma;
741                         i++;
742                         start += PAGE_SIZE;
743                         len--;
744                         continue;
745                 }
746
747                 if (!vma || (pages && (vma->vm_flags & VM_IO))
748                                 || !(flags & vma->vm_flags))
749                         return i ? : -EFAULT;
750
751                 if (is_vm_hugetlb_page(vma)) {
752                         i = follow_hugetlb_page(mm, vma, pages, vmas,
753                                                 &start, &len, i);
754                         continue;
755                 }
756                 spin_lock(&mm->page_table_lock);
757                 do {
758                         struct page *map;
759                         int lookup_write = write;
760                         while (!(map = follow_page(mm, start, lookup_write))) {
761                                 /*
762                                  * Shortcut for anonymous pages. We don't want
763                                  * to force the creation of pages tables for
764                                  * insanly big anonymously mapped areas that
765                                  * nobody touched so far. This is important
766                                  * for doing a core dump for these mappings.
767                                  */
768                                 if (!lookup_write &&
769                                     untouched_anonymous_page(mm,vma,start)) {
770                                         map = ZERO_PAGE(start);
771                                         break;
772                                 }
773                                 spin_unlock(&mm->page_table_lock);
774                                 switch (handle_mm_fault(mm,vma,start,write)) {
775                                 case VM_FAULT_MINOR:
776                                         tsk->min_flt++;
777                                         break;
778                                 case VM_FAULT_MAJOR:
779                                         tsk->maj_flt++;
780                                         break;
781                                 case VM_FAULT_SIGBUS:
782                                         return i ? i : -EFAULT;
783                                 case VM_FAULT_OOM:
784                                         return i ? i : -ENOMEM;
785                                 default:
786                                         BUG();
787                                 }
788                                 /*
789                                  * Now that we have performed a write fault
790                                  * and surely no longer have a shared page we
791                                  * shouldn't write, we shouldn't ignore an
792                                  * unwritable page in the page table if
793                                  * we are forcing write access.
794                                  */
795                                 lookup_write = write && !force;
796                                 spin_lock(&mm->page_table_lock);
797                         }
798                         if (pages) {
799                                 pages[i] = get_page_map(map);
800                                 if (!pages[i]) {
801                                         spin_unlock(&mm->page_table_lock);
802                                         while (i--)
803                                                 page_cache_release(pages[i]);
804                                         i = -EFAULT;
805                                         goto out;
806                                 }
807                                 flush_dcache_page(pages[i]);
808                                 if (!PageReserved(pages[i]))
809                                         page_cache_get(pages[i]);
810                         }
811                         if (vmas)
812                                 vmas[i] = vma;
813                         i++;
814                         start += PAGE_SIZE;
815                         len--;
816                 } while(len && start < vma->vm_end);
817                 spin_unlock(&mm->page_table_lock);
818         } while(len);
819 out:
820         return i;
821 }
822
823 EXPORT_SYMBOL(get_user_pages);
824
825 static void zeromap_pte_range(pte_t * pte, unsigned long address,
826                                      unsigned long size, pgprot_t prot)
827 {
828         unsigned long end;
829
830         address &= ~PMD_MASK;
831         end = address + size;
832         if (end > PMD_SIZE)
833                 end = PMD_SIZE;
834         do {
835                 pte_t zero_pte = pte_wrprotect(mk_pte(ZERO_PAGE(address), prot));
836                 BUG_ON(!pte_none(*pte));
837                 set_pte(pte, zero_pte);
838                 address += PAGE_SIZE;
839                 pte++;
840         } while (address && (address < end));
841 }
842
843 static inline int zeromap_pmd_range(struct mm_struct *mm, pmd_t * pmd, unsigned long address,
844                                     unsigned long size, pgprot_t prot)
845 {
846         unsigned long base, end;
847
848         base = address & PGDIR_MASK;
849         address &= ~PGDIR_MASK;
850         end = address + size;
851         if (end > PGDIR_SIZE)
852                 end = PGDIR_SIZE;
853         do {
854                 pte_t * pte = pte_alloc_map(mm, pmd, base + address);
855                 if (!pte)
856                         return -ENOMEM;
857                 zeromap_pte_range(pte, base + address, end - address, prot);
858                 pte_unmap(pte);
859                 address = (address + PMD_SIZE) & PMD_MASK;
860                 pmd++;
861         } while (address && (address < end));
862         return 0;
863 }
864
865 int zeromap_page_range(struct vm_area_struct *vma, unsigned long address, unsigned long size, pgprot_t prot)
866 {
867         int error = 0;
868         pgd_t * dir;
869         unsigned long beg = address;
870         unsigned long end = address + size;
871         struct mm_struct *mm = vma->vm_mm;
872
873         dir = pgd_offset(mm, address);
874         flush_cache_range(vma, beg, end);
875         if (address >= end)
876                 BUG();
877
878         spin_lock(&mm->page_table_lock);
879         do {
880                 pmd_t *pmd = pmd_alloc(mm, dir, address);
881                 error = -ENOMEM;
882                 if (!pmd)
883                         break;
884                 error = zeromap_pmd_range(mm, pmd, address, end - address, prot);
885                 if (error)
886                         break;
887                 address = (address + PGDIR_SIZE) & PGDIR_MASK;
888                 dir++;
889         } while (address && (address < end));
890         /*
891          * Why flush? zeromap_pte_range has a BUG_ON for !pte_none()
892          */
893         flush_tlb_range(vma, beg, end);
894         spin_unlock(&mm->page_table_lock);
895         return error;
896 }
897
898 /*
899  * maps a range of physical memory into the requested pages. the old
900  * mappings are removed. any references to nonexistent pages results
901  * in null mappings (currently treated as "copy-on-access")
902  */
903 static inline void remap_pte_range(pte_t * pte, unsigned long address, unsigned long size,
904         unsigned long phys_addr, pgprot_t prot)
905 {
906         unsigned long end;
907         unsigned long pfn;
908
909         address &= ~PMD_MASK;
910         end = address + size;
911         if (end > PMD_SIZE)
912                 end = PMD_SIZE;
913         pfn = phys_addr >> PAGE_SHIFT;
914         do {
915                 BUG_ON(!pte_none(*pte));
916                 if (!pfn_valid(pfn) || PageReserved(pfn_to_page(pfn)))
917                         set_pte(pte, pfn_pte(pfn, prot));
918                 address += PAGE_SIZE;
919                 pfn++;
920                 pte++;
921         } while (address && (address < end));
922 }
923
924 static inline int remap_pmd_range(struct mm_struct *mm, pmd_t * pmd, unsigned long address, unsigned long size,
925         unsigned long phys_addr, pgprot_t prot)
926 {
927         unsigned long base, end;
928
929         base = address & PGDIR_MASK;
930         address &= ~PGDIR_MASK;
931         end = address + size;
932         if (end > PGDIR_SIZE)
933                 end = PGDIR_SIZE;
934         phys_addr -= address;
935         do {
936                 pte_t * pte = pte_alloc_map(mm, pmd, base + address);
937                 if (!pte)
938                         return -ENOMEM;
939                 remap_pte_range(pte, base + address, end - address, address + phys_addr, prot);
940                 pte_unmap(pte);
941                 address = (address + PMD_SIZE) & PMD_MASK;
942                 pmd++;
943         } while (address && (address < end));
944         return 0;
945 }
946
947 /*  Note: this is only safe if the mm semaphore is held when called. */
948 int remap_page_range(struct vm_area_struct *vma, unsigned long from, unsigned long phys_addr, unsigned long size, pgprot_t prot)
949 {
950         int error = 0;
951         pgd_t * dir;
952         unsigned long beg = from;
953         unsigned long end = from + size;
954         struct mm_struct *mm = vma->vm_mm;
955
956         phys_addr -= from;
957         dir = pgd_offset(mm, from);
958         flush_cache_range(vma, beg, end);
959         if (from >= end)
960                 BUG();
961
962         spin_lock(&mm->page_table_lock);
963         do {
964                 pmd_t *pmd = pmd_alloc(mm, dir, from);
965                 error = -ENOMEM;
966                 if (!pmd)
967                         break;
968                 error = remap_pmd_range(mm, pmd, from, end - from, phys_addr + from, prot);
969                 if (error)
970                         break;
971                 from = (from + PGDIR_SIZE) & PGDIR_MASK;
972                 dir++;
973         } while (from && (from < end));
974         /*
975          * Why flush? remap_pte_range has a BUG_ON for !pte_none()
976          */
977         flush_tlb_range(vma, beg, end);
978         spin_unlock(&mm->page_table_lock);
979         return error;
980 }
981
982 EXPORT_SYMBOL(remap_page_range);
983
984 /*
985  * Do pte_mkwrite, but only if the vma says VM_WRITE.  We do this when
986  * servicing faults for write access.  In the normal case, do always want
987  * pte_mkwrite.  But get_user_pages can cause write faults for mappings
988  * that do not have writing enabled, when used by access_process_vm.
989  */
990 static inline pte_t maybe_mkwrite(pte_t pte, struct vm_area_struct *vma)
991 {
992         if (likely(vma->vm_flags & VM_WRITE))
993                 pte = pte_mkwrite(pte);
994         return pte;
995 }
996
997 /*
998  * We hold the mm semaphore for reading and vma->vm_mm->page_table_lock
999  */
1000 static inline void break_cow(struct vm_area_struct * vma, struct page * new_page, unsigned long address, 
1001                 pte_t *page_table)
1002 {
1003         pte_t entry;
1004
1005         flush_cache_page(vma, address);
1006         entry = maybe_mkwrite(pte_mkdirty(mk_pte(new_page, vma->vm_page_prot)),
1007                               vma);
1008         ptep_establish(vma, address, page_table, entry);
1009         update_mmu_cache(vma, address, entry);
1010 }
1011
1012 /*
1013  * This routine handles present pages, when users try to write
1014  * to a shared page. It is done by copying the page to a new address
1015  * and decrementing the shared-page counter for the old page.
1016  *
1017  * Goto-purists beware: the only reason for goto's here is that it results
1018  * in better assembly code.. The "default" path will see no jumps at all.
1019  *
1020  * Note that this routine assumes that the protection checks have been
1021  * done by the caller (the low-level page fault routine in most cases).
1022  * Thus we can safely just mark it writable once we've done any necessary
1023  * COW.
1024  *
1025  * We also mark the page dirty at this point even though the page will
1026  * change only once the write actually happens. This avoids a few races,
1027  * and potentially makes it more efficient.
1028  *
1029  * We hold the mm semaphore and the page_table_lock on entry and exit
1030  * with the page_table_lock released.
1031  */
1032 static int do_wp_page(struct mm_struct *mm, struct vm_area_struct * vma,
1033         unsigned long address, pte_t *page_table, pmd_t *pmd, pte_t pte)
1034 {
1035         struct page *old_page, *new_page;
1036         unsigned long pfn = pte_pfn(pte);
1037         pte_t entry;
1038
1039         if (unlikely(!pfn_valid(pfn))) {
1040                 /*
1041                  * This should really halt the system so it can be debugged or
1042                  * at least the kernel stops what it's doing before it corrupts
1043                  * data, but for the moment just pretend this is OOM.
1044                  */
1045                 pte_unmap(page_table);
1046                 printk(KERN_ERR "do_wp_page: bogus page at address %08lx\n",
1047                                 address);
1048                 spin_unlock(&mm->page_table_lock);
1049                 return VM_FAULT_OOM;
1050         }
1051         old_page = pfn_to_page(pfn);
1052
1053         if (!TestSetPageLocked(old_page)) {
1054                 int reuse = can_share_swap_page(old_page);
1055                 unlock_page(old_page);
1056                 if (reuse) {
1057                         flush_cache_page(vma, address);
1058                         entry = maybe_mkwrite(pte_mkyoung(pte_mkdirty(pte)),
1059                                               vma);
1060                         ptep_set_access_flags(vma, address, page_table, entry, 1);
1061                         update_mmu_cache(vma, address, entry);
1062                         pte_unmap(page_table);
1063                         spin_unlock(&mm->page_table_lock);
1064                         return VM_FAULT_MINOR;
1065                 }
1066         }
1067         pte_unmap(page_table);
1068
1069         /*
1070          * Ok, we need to copy. Oh, well..
1071          */
1072         page_cache_get(old_page);
1073         spin_unlock(&mm->page_table_lock);
1074
1075         if (unlikely(anon_vma_prepare(vma)))
1076                 goto no_new_page;
1077         new_page = alloc_page_vma(GFP_HIGHUSER, vma, address);
1078         if (!new_page)
1079                 goto no_new_page;
1080         copy_cow_page(old_page,new_page,address);
1081
1082         /*
1083          * Re-check the pte - we dropped the lock
1084          */
1085         spin_lock(&mm->page_table_lock);
1086         page_table = pte_offset_map(pmd, address);
1087         if (likely(pte_same(*page_table, pte))) {
1088                 if (PageReserved(old_page))
1089                         // ++mm->rss;
1090                         vx_rsspages_inc(mm);
1091                 else
1092                         page_remove_rmap(old_page);
1093                 break_cow(vma, new_page, address, page_table);
1094                 lru_cache_add_active(new_page);
1095                 page_add_anon_rmap(new_page, vma, address);
1096
1097                 /* Free the old page.. */
1098                 new_page = old_page;
1099         }
1100         pte_unmap(page_table);
1101         page_cache_release(new_page);
1102         page_cache_release(old_page);
1103         spin_unlock(&mm->page_table_lock);
1104         return VM_FAULT_MINOR;
1105
1106 no_new_page:
1107         page_cache_release(old_page);
1108         return VM_FAULT_OOM;
1109 }
1110
1111 /*
1112  * Helper function for unmap_mapping_range().
1113  */
1114 static inline void unmap_mapping_range_list(struct prio_tree_root *root,
1115                                             struct zap_details *details)
1116 {
1117         struct vm_area_struct *vma = NULL;
1118         struct prio_tree_iter iter;
1119         pgoff_t vba, vea, zba, zea;
1120
1121         while ((vma = vma_prio_tree_next(vma, root, &iter,
1122                         details->first_index, details->last_index)) != NULL) {
1123                 vba = vma->vm_pgoff;
1124                 vea = vba + ((vma->vm_end - vma->vm_start) >> PAGE_SHIFT) - 1;
1125                 /* Assume for now that PAGE_CACHE_SHIFT == PAGE_SHIFT */
1126                 zba = details->first_index;
1127                 if (zba < vba)
1128                         zba = vba;
1129                 zea = details->last_index;
1130                 if (zea > vea)
1131                         zea = vea;
1132                 zap_page_range(vma,
1133                         ((zba - vba) << PAGE_SHIFT) + vma->vm_start,
1134                         (zea - zba + 1) << PAGE_SHIFT, details);
1135         }
1136 }
1137
1138 /**
1139  * unmap_mapping_range - unmap the portion of all mmaps
1140  * in the specified address_space corresponding to the specified
1141  * page range in the underlying file.
1142  * @address_space: the address space containing mmaps to be unmapped.
1143  * @holebegin: byte in first page to unmap, relative to the start of
1144  * the underlying file.  This will be rounded down to a PAGE_SIZE
1145  * boundary.  Note that this is different from vmtruncate(), which
1146  * must keep the partial page.  In contrast, we must get rid of
1147  * partial pages.
1148  * @holelen: size of prospective hole in bytes.  This will be rounded
1149  * up to a PAGE_SIZE boundary.  A holelen of zero truncates to the
1150  * end of the file.
1151  * @even_cows: 1 when truncating a file, unmap even private COWed pages;
1152  * but 0 when invalidating pagecache, don't throw away private data.
1153  */
1154 void unmap_mapping_range(struct address_space *mapping,
1155                 loff_t const holebegin, loff_t const holelen, int even_cows)
1156 {
1157         struct zap_details details;
1158         pgoff_t hba = holebegin >> PAGE_SHIFT;
1159         pgoff_t hlen = (holelen + PAGE_SIZE - 1) >> PAGE_SHIFT;
1160
1161         /* Check for overflow. */
1162         if (sizeof(holelen) > sizeof(hlen)) {
1163                 long long holeend =
1164                         (holebegin + holelen + PAGE_SIZE - 1) >> PAGE_SHIFT;
1165                 if (holeend & ~(long long)ULONG_MAX)
1166                         hlen = ULONG_MAX - hba + 1;
1167         }
1168
1169         details.check_mapping = even_cows? NULL: mapping;
1170         details.nonlinear_vma = NULL;
1171         details.first_index = hba;
1172         details.last_index = hba + hlen - 1;
1173         details.atomic = 1;     /* A spinlock is held */
1174         if (details.last_index < details.first_index)
1175                 details.last_index = ULONG_MAX;
1176
1177         spin_lock(&mapping->i_mmap_lock);
1178         /* Protect against page fault */
1179         atomic_inc(&mapping->truncate_count);
1180
1181         if (unlikely(!prio_tree_empty(&mapping->i_mmap)))
1182                 unmap_mapping_range_list(&mapping->i_mmap, &details);
1183
1184         /*
1185          * In nonlinear VMAs there is no correspondence between virtual address
1186          * offset and file offset.  So we must perform an exhaustive search
1187          * across *all* the pages in each nonlinear VMA, not just the pages
1188          * whose virtual address lies outside the file truncation point.
1189          */
1190         if (unlikely(!list_empty(&mapping->i_mmap_nonlinear))) {
1191                 struct vm_area_struct *vma;
1192                 list_for_each_entry(vma, &mapping->i_mmap_nonlinear,
1193                                                 shared.vm_set.list) {
1194                         details.nonlinear_vma = vma;
1195                         zap_page_range(vma, vma->vm_start,
1196                                 vma->vm_end - vma->vm_start, &details);
1197                 }
1198         }
1199         spin_unlock(&mapping->i_mmap_lock);
1200 }
1201 EXPORT_SYMBOL(unmap_mapping_range);
1202
1203 /*
1204  * Handle all mappings that got truncated by a "truncate()"
1205  * system call.
1206  *
1207  * NOTE! We have to be ready to update the memory sharing
1208  * between the file and the memory map for a potential last
1209  * incomplete page.  Ugly, but necessary.
1210  */
1211 int vmtruncate(struct inode * inode, loff_t offset)
1212 {
1213         struct address_space *mapping = inode->i_mapping;
1214         unsigned long limit;
1215
1216         if (inode->i_size < offset)
1217                 goto do_expand;
1218         i_size_write(inode, offset);
1219         unmap_mapping_range(mapping, offset + PAGE_SIZE - 1, 0, 1);
1220         truncate_inode_pages(mapping, offset);
1221         goto out_truncate;
1222
1223 do_expand:
1224         limit = current->rlim[RLIMIT_FSIZE].rlim_cur;
1225         if (limit != RLIM_INFINITY && offset > limit)
1226                 goto out_sig;
1227         if (offset > inode->i_sb->s_maxbytes)
1228                 goto out;
1229         i_size_write(inode, offset);
1230
1231 out_truncate:
1232         if (inode->i_op && inode->i_op->truncate)
1233                 inode->i_op->truncate(inode);
1234         return 0;
1235 out_sig:
1236         send_sig(SIGXFSZ, current, 0);
1237 out:
1238         return -EFBIG;
1239 }
1240
1241 EXPORT_SYMBOL(vmtruncate);
1242
1243 /* 
1244  * Primitive swap readahead code. We simply read an aligned block of
1245  * (1 << page_cluster) entries in the swap area. This method is chosen
1246  * because it doesn't cost us any seek time.  We also make sure to queue
1247  * the 'original' request together with the readahead ones...  
1248  *
1249  * This has been extended to use the NUMA policies from the mm triggering
1250  * the readahead.
1251  *
1252  * Caller must hold down_read on the vma->vm_mm if vma is not NULL.
1253  */
1254 void swapin_readahead(swp_entry_t entry, unsigned long addr,struct vm_area_struct *vma)
1255 {
1256 #ifdef CONFIG_NUMA
1257         struct vm_area_struct *next_vma = vma ? vma->vm_next : NULL;
1258 #endif
1259         int i, num;
1260         struct page *new_page;
1261         unsigned long offset;
1262
1263         /*
1264          * Get the number of handles we should do readahead io to.
1265          */
1266         num = valid_swaphandles(entry, &offset);
1267         for (i = 0; i < num; offset++, i++) {
1268                 /* Ok, do the async read-ahead now */
1269                 new_page = read_swap_cache_async(swp_entry(swp_type(entry),
1270                                                            offset), vma, addr);
1271                 if (!new_page)
1272                         break;
1273                 page_cache_release(new_page);
1274 #ifdef CONFIG_NUMA
1275                 /*
1276                  * Find the next applicable VMA for the NUMA policy.
1277                  */
1278                 addr += PAGE_SIZE;
1279                 if (addr == 0)
1280                         vma = NULL;
1281                 if (vma) {
1282                         if (addr >= vma->vm_end) {
1283                                 vma = next_vma;
1284                                 next_vma = vma ? vma->vm_next : NULL;
1285                         }
1286                         if (vma && addr < vma->vm_start)
1287                                 vma = NULL;
1288                 } else {
1289                         if (next_vma && addr >= next_vma->vm_start) {
1290                                 vma = next_vma;
1291                                 next_vma = vma->vm_next;
1292                         }
1293                 }
1294 #endif
1295         }
1296         lru_add_drain();        /* Push any new pages onto the LRU now */
1297 }
1298
1299 /*
1300  * We hold the mm semaphore and the page_table_lock on entry and
1301  * should release the pagetable lock on exit..
1302  */
1303 static int do_swap_page(struct mm_struct * mm,
1304         struct vm_area_struct * vma, unsigned long address,
1305         pte_t *page_table, pmd_t *pmd, pte_t orig_pte, int write_access)
1306 {
1307         struct page *page;
1308         swp_entry_t entry = pte_to_swp_entry(orig_pte);
1309         pte_t pte;
1310         int ret = VM_FAULT_MINOR;
1311
1312         pte_unmap(page_table);
1313         spin_unlock(&mm->page_table_lock);
1314         page = lookup_swap_cache(entry);
1315         if (!page) {
1316                 swapin_readahead(entry, address, vma);
1317                 page = read_swap_cache_async(entry, vma, address);
1318                 if (!page) {
1319                         /*
1320                          * Back out if somebody else faulted in this pte while
1321                          * we released the page table lock.
1322                          */
1323                         spin_lock(&mm->page_table_lock);
1324                         page_table = pte_offset_map(pmd, address);
1325                         if (likely(pte_same(*page_table, orig_pte)))
1326                                 ret = VM_FAULT_OOM;
1327                         else
1328                                 ret = VM_FAULT_MINOR;
1329                         pte_unmap(page_table);
1330                         spin_unlock(&mm->page_table_lock);
1331                         goto out;
1332                 }
1333
1334                 /* Had to read the page from swap area: Major fault */
1335                 ret = VM_FAULT_MAJOR;
1336                 inc_page_state(pgmajfault);
1337         }
1338
1339         if (!vx_rsspages_avail(mm, 1)) {
1340                 ret = VM_FAULT_OOM;
1341                 goto out;
1342         }
1343         mark_page_accessed(page);
1344         lock_page(page);
1345
1346         /*
1347          * Back out if somebody else faulted in this pte while we
1348          * released the page table lock.
1349          */
1350         spin_lock(&mm->page_table_lock);
1351         page_table = pte_offset_map(pmd, address);
1352         if (unlikely(!pte_same(*page_table, orig_pte))) {
1353                 pte_unmap(page_table);
1354                 spin_unlock(&mm->page_table_lock);
1355                 unlock_page(page);
1356                 page_cache_release(page);
1357                 ret = VM_FAULT_MINOR;
1358                 goto out;
1359         }
1360
1361         /* The page isn't present yet, go ahead with the fault. */
1362                 
1363         swap_free(entry);
1364         if (vm_swap_full())
1365                 remove_exclusive_swap_page(page);
1366
1367         // mm->rss++;
1368         vx_rsspages_inc(mm);
1369         pte = mk_pte(page, vma->vm_page_prot);
1370         if (write_access && can_share_swap_page(page)) {
1371                 pte = maybe_mkwrite(pte_mkdirty(pte), vma);
1372                 write_access = 0;
1373         }
1374         unlock_page(page);
1375
1376         flush_icache_page(vma, page);
1377         set_pte(page_table, pte);
1378         page_add_anon_rmap(page, vma, address);
1379
1380         if (write_access) {
1381                 if (do_wp_page(mm, vma, address,
1382                                 page_table, pmd, pte) == VM_FAULT_OOM)
1383                         ret = VM_FAULT_OOM;
1384                 goto out;
1385         }
1386
1387         /* No need to invalidate - it was non-present before */
1388         update_mmu_cache(vma, address, pte);
1389         pte_unmap(page_table);
1390         spin_unlock(&mm->page_table_lock);
1391 out:
1392         return ret;
1393 }
1394
1395 /*
1396  * We are called with the MM semaphore and page_table_lock
1397  * spinlock held to protect against concurrent faults in
1398  * multithreaded programs. 
1399  */
1400 static int
1401 do_anonymous_page(struct mm_struct *mm, struct vm_area_struct *vma,
1402                 pte_t *page_table, pmd_t *pmd, int write_access,
1403                 unsigned long addr)
1404 {
1405         pte_t entry;
1406         struct page * page = ZERO_PAGE(addr);
1407
1408         /* Read-only mapping of ZERO_PAGE. */
1409         entry = pte_wrprotect(mk_pte(ZERO_PAGE(addr), vma->vm_page_prot));
1410
1411         /* ..except if it's a write access */
1412         if (write_access) {
1413                 /* Allocate our own private page. */
1414                 pte_unmap(page_table);
1415                 spin_unlock(&mm->page_table_lock);
1416
1417                 if (unlikely(anon_vma_prepare(vma)))
1418                         goto no_mem;
1419                 if (!vx_rsspages_avail(mm, 1))
1420                         goto no_mem;
1421
1422                 page = alloc_page_vma(GFP_HIGHUSER, vma, addr);
1423                 if (!page)
1424                         goto no_mem;
1425                 clear_user_highpage(page, addr);
1426
1427                 spin_lock(&mm->page_table_lock);
1428                 page_table = pte_offset_map(pmd, addr);
1429
1430                 if (!pte_none(*page_table)) {
1431                         pte_unmap(page_table);
1432                         page_cache_release(page);
1433                         spin_unlock(&mm->page_table_lock);
1434                         goto out;
1435                 }
1436                 // mm->rss++;
1437                 vx_rsspages_inc(mm);
1438                 entry = maybe_mkwrite(pte_mkdirty(mk_pte(page,
1439                                                          vma->vm_page_prot)),
1440                                       vma);
1441                 lru_cache_add_active(page);
1442                 mark_page_accessed(page);
1443                 page_add_anon_rmap(page, vma, addr);
1444         }
1445
1446         set_pte(page_table, entry);
1447         pte_unmap(page_table);
1448
1449         /* No need to invalidate - it was non-present before */
1450         update_mmu_cache(vma, addr, entry);
1451         spin_unlock(&mm->page_table_lock);
1452 out:
1453         return VM_FAULT_MINOR;
1454 no_mem:
1455         return VM_FAULT_OOM;
1456 }
1457
1458 /*
1459  * do_no_page() tries to create a new page mapping. It aggressively
1460  * tries to share with existing pages, but makes a separate copy if
1461  * the "write_access" parameter is true in order to avoid the next
1462  * page fault.
1463  *
1464  * As this is called only for pages that do not currently exist, we
1465  * do not need to flush old virtual caches or the TLB.
1466  *
1467  * This is called with the MM semaphore held and the page table
1468  * spinlock held. Exit with the spinlock released.
1469  */
1470 static int
1471 do_no_page(struct mm_struct *mm, struct vm_area_struct *vma,
1472         unsigned long address, int write_access, pte_t *page_table, pmd_t *pmd)
1473 {
1474         struct page * new_page;
1475         struct address_space *mapping = NULL;
1476         pte_t entry;
1477         int sequence = 0;
1478         int ret = VM_FAULT_MINOR;
1479         int anon = 0;
1480
1481         if (!vma->vm_ops || !vma->vm_ops->nopage)
1482                 return do_anonymous_page(mm, vma, page_table,
1483                                         pmd, write_access, address);
1484         pte_unmap(page_table);
1485         spin_unlock(&mm->page_table_lock);
1486
1487         if (vma->vm_file) {
1488                 mapping = vma->vm_file->f_mapping;
1489                 sequence = atomic_read(&mapping->truncate_count);
1490         }
1491         smp_rmb();  /* Prevent CPU from reordering lock-free ->nopage() */
1492 retry:
1493         new_page = vma->vm_ops->nopage(vma, address & PAGE_MASK, &ret);
1494
1495         /* no page was available -- either SIGBUS or OOM */
1496         if (new_page == NOPAGE_SIGBUS)
1497                 return VM_FAULT_SIGBUS;
1498         if (new_page == NOPAGE_OOM)
1499                 return VM_FAULT_OOM;
1500         if (!vx_rsspages_avail(mm, 1))
1501                 return VM_FAULT_OOM;
1502
1503         /*
1504          * Should we do an early C-O-W break?
1505          */
1506         if (write_access && !(vma->vm_flags & VM_SHARED)) {
1507                 struct page *page;
1508
1509                 if (unlikely(anon_vma_prepare(vma)))
1510                         goto oom;
1511                 page = alloc_page_vma(GFP_HIGHUSER, vma, address);
1512                 if (!page)
1513                         goto oom;
1514                 copy_user_highpage(page, new_page, address);
1515                 page_cache_release(new_page);
1516                 new_page = page;
1517                 anon = 1;
1518         }
1519
1520         spin_lock(&mm->page_table_lock);
1521         /*
1522          * For a file-backed vma, someone could have truncated or otherwise
1523          * invalidated this page.  If unmap_mapping_range got called,
1524          * retry getting the page.
1525          */
1526         if (mapping &&
1527               (unlikely(sequence != atomic_read(&mapping->truncate_count)))) {
1528                 sequence = atomic_read(&mapping->truncate_count);
1529                 spin_unlock(&mm->page_table_lock);
1530                 page_cache_release(new_page);
1531                 goto retry;
1532         }
1533         page_table = pte_offset_map(pmd, address);
1534
1535         /*
1536          * This silly early PAGE_DIRTY setting removes a race
1537          * due to the bad i386 page protection. But it's valid
1538          * for other architectures too.
1539          *
1540          * Note that if write_access is true, we either now have
1541          * an exclusive copy of the page, or this is a shared mapping,
1542          * so we can make it writable and dirty to avoid having to
1543          * handle that later.
1544          */
1545         /* Only go through if we didn't race with anybody else... */
1546         if (pte_none(*page_table)) {
1547                 if (!PageReserved(new_page))
1548                         // ++mm->rss;
1549                         vx_rsspages_inc(mm);
1550                 flush_icache_page(vma, new_page);
1551                 entry = mk_pte(new_page, vma->vm_page_prot);
1552                 if (write_access)
1553                         entry = maybe_mkwrite(pte_mkdirty(entry), vma);
1554                 set_pte(page_table, entry);
1555                 if (anon) {
1556                         lru_cache_add_active(new_page);
1557                         page_add_anon_rmap(new_page, vma, address);
1558                 } else
1559                         page_add_file_rmap(new_page);
1560                 pte_unmap(page_table);
1561         } else {
1562                 /* One of our sibling threads was faster, back out. */
1563                 pte_unmap(page_table);
1564                 page_cache_release(new_page);
1565                 spin_unlock(&mm->page_table_lock);
1566                 goto out;
1567         }
1568
1569         /* no need to invalidate: a not-present page shouldn't be cached */
1570         update_mmu_cache(vma, address, entry);
1571         spin_unlock(&mm->page_table_lock);
1572 out:
1573         return ret;
1574 oom:
1575         page_cache_release(new_page);
1576         ret = VM_FAULT_OOM;
1577         goto out;
1578 }
1579
1580 /*
1581  * Fault of a previously existing named mapping. Repopulate the pte
1582  * from the encoded file_pte if possible. This enables swappable
1583  * nonlinear vmas.
1584  */
1585 static int do_file_page(struct mm_struct * mm, struct vm_area_struct * vma,
1586         unsigned long address, int write_access, pte_t *pte, pmd_t *pmd)
1587 {
1588         unsigned long pgoff;
1589         int err;
1590
1591         BUG_ON(!vma->vm_ops || !vma->vm_ops->nopage);
1592         /*
1593          * Fall back to the linear mapping if the fs does not support
1594          * ->populate:
1595          */
1596         if (!vma->vm_ops || !vma->vm_ops->populate || 
1597                         (write_access && !(vma->vm_flags & VM_SHARED))) {
1598                 pte_clear(pte);
1599                 return do_no_page(mm, vma, address, write_access, pte, pmd);
1600         }
1601
1602         pgoff = pte_to_pgoff(*pte);
1603
1604         pte_unmap(pte);
1605         spin_unlock(&mm->page_table_lock);
1606
1607         err = vma->vm_ops->populate(vma, address & PAGE_MASK, PAGE_SIZE, vma->vm_page_prot, pgoff, 0);
1608         if (err == -ENOMEM)
1609                 return VM_FAULT_OOM;
1610         if (err)
1611                 return VM_FAULT_SIGBUS;
1612         return VM_FAULT_MAJOR;
1613 }
1614
1615 /*
1616  * These routines also need to handle stuff like marking pages dirty
1617  * and/or accessed for architectures that don't do it in hardware (most
1618  * RISC architectures).  The early dirtying is also good on the i386.
1619  *
1620  * There is also a hook called "update_mmu_cache()" that architectures
1621  * with external mmu caches can use to update those (ie the Sparc or
1622  * PowerPC hashed page tables that act as extended TLBs).
1623  *
1624  * Note the "page_table_lock". It is to protect against kswapd removing
1625  * pages from under us. Note that kswapd only ever _removes_ pages, never
1626  * adds them. As such, once we have noticed that the page is not present,
1627  * we can drop the lock early.
1628  *
1629  * The adding of pages is protected by the MM semaphore (which we hold),
1630  * so we don't need to worry about a page being suddenly been added into
1631  * our VM.
1632  *
1633  * We enter with the pagetable spinlock held, we are supposed to
1634  * release it when done.
1635  */
1636 static inline int handle_pte_fault(struct mm_struct *mm,
1637         struct vm_area_struct * vma, unsigned long address,
1638         int write_access, pte_t *pte, pmd_t *pmd)
1639 {
1640         pte_t entry;
1641
1642         entry = *pte;
1643         if (!pte_present(entry)) {
1644                 /*
1645                  * If it truly wasn't present, we know that kswapd
1646                  * and the PTE updates will not touch it later. So
1647                  * drop the lock.
1648                  */
1649                 if (pte_none(entry))
1650                         return do_no_page(mm, vma, address, write_access, pte, pmd);
1651                 if (pte_file(entry))
1652                         return do_file_page(mm, vma, address, write_access, pte, pmd);
1653                 return do_swap_page(mm, vma, address, pte, pmd, entry, write_access);
1654         }
1655
1656         if (write_access) {
1657                 if (!pte_write(entry))
1658                         return do_wp_page(mm, vma, address, pte, pmd, entry);
1659
1660                 entry = pte_mkdirty(entry);
1661         }
1662         entry = pte_mkyoung(entry);
1663         ptep_set_access_flags(vma, address, pte, entry, write_access);
1664         update_mmu_cache(vma, address, entry);
1665         pte_unmap(pte);
1666         spin_unlock(&mm->page_table_lock);
1667         return VM_FAULT_MINOR;
1668 }
1669
1670 /*
1671  * By the time we get here, we already hold the mm semaphore
1672  */
1673 int handle_mm_fault(struct mm_struct *mm, struct vm_area_struct * vma,
1674         unsigned long address, int write_access)
1675 {
1676         pgd_t *pgd;
1677         pmd_t *pmd;
1678
1679         __set_current_state(TASK_RUNNING);
1680         pgd = pgd_offset(mm, address);
1681
1682         inc_page_state(pgfault);
1683
1684         if (is_vm_hugetlb_page(vma))
1685                 return VM_FAULT_SIGBUS; /* mapping truncation does this. */
1686
1687         /*
1688          * We need the page table lock to synchronize with kswapd
1689          * and the SMP-safe atomic PTE updates.
1690          */
1691         spin_lock(&mm->page_table_lock);
1692         pmd = pmd_alloc(mm, pgd, address);
1693
1694         if (pmd) {
1695                 pte_t * pte = pte_alloc_map(mm, pmd, address);
1696                 if (pte)
1697                         return handle_pte_fault(mm, vma, address, write_access, pte, pmd);
1698         }
1699         spin_unlock(&mm->page_table_lock);
1700         return VM_FAULT_OOM;
1701 }
1702
1703 /*
1704  * Allocate page middle directory.
1705  *
1706  * We've already handled the fast-path in-line, and we own the
1707  * page table lock.
1708  *
1709  * On a two-level page table, this ends up actually being entirely
1710  * optimized away.
1711  */
1712 pmd_t fastcall *__pmd_alloc(struct mm_struct *mm, pgd_t *pgd, unsigned long address)
1713 {
1714         pmd_t *new;
1715
1716         spin_unlock(&mm->page_table_lock);
1717         new = pmd_alloc_one(mm, address);
1718         spin_lock(&mm->page_table_lock);
1719         if (!new)
1720                 return NULL;
1721
1722         /*
1723          * Because we dropped the lock, we should re-check the
1724          * entry, as somebody else could have populated it..
1725          */
1726         if (pgd_present(*pgd)) {
1727                 pmd_free(new);
1728                 goto out;
1729         }
1730         pgd_populate(mm, pgd, new);
1731 out:
1732         return pmd_offset(pgd, address);
1733 }
1734
1735 int make_pages_present(unsigned long addr, unsigned long end)
1736 {
1737         int ret, len, write;
1738         struct vm_area_struct * vma;
1739
1740         vma = find_vma(current->mm, addr);
1741         write = (vma->vm_flags & VM_WRITE) != 0;
1742         if (addr >= end)
1743                 BUG();
1744         if (end > vma->vm_end)
1745                 BUG();
1746         len = (end+PAGE_SIZE-1)/PAGE_SIZE-addr/PAGE_SIZE;
1747         ret = get_user_pages(current, current->mm, addr,
1748                         len, write, 0, NULL, NULL);
1749         if (ret < 0)
1750                 return ret;
1751         return ret == len ? 0 : -1;
1752 }
1753
1754 /* 
1755  * Map a vmalloc()-space virtual address to the physical page.
1756  */
1757 struct page * vmalloc_to_page(void * vmalloc_addr)
1758 {
1759         unsigned long addr = (unsigned long) vmalloc_addr;
1760         struct page *page = NULL;
1761         pgd_t *pgd = pgd_offset_k(addr);
1762         pmd_t *pmd;
1763         pte_t *ptep, pte;
1764   
1765         if (!pgd_none(*pgd)) {
1766                 pmd = pmd_offset(pgd, addr);
1767                 if (!pmd_none(*pmd)) {
1768                         preempt_disable();
1769                         ptep = pte_offset_map(pmd, addr);
1770                         pte = *ptep;
1771                         if (pte_present(pte))
1772                                 page = pte_page(pte);
1773                         pte_unmap(ptep);
1774                         preempt_enable();
1775                 }
1776         }
1777         return page;
1778 }
1779
1780 EXPORT_SYMBOL(vmalloc_to_page);
1781
1782 #if !defined(CONFIG_ARCH_GATE_AREA)
1783
1784 #if defined(AT_SYSINFO_EHDR)
1785 struct vm_area_struct gate_vma;
1786
1787 static int __init gate_vma_init(void)
1788 {
1789         gate_vma.vm_mm = NULL;
1790         gate_vma.vm_start = FIXADDR_USER_START;
1791         gate_vma.vm_end = FIXADDR_USER_END;
1792         gate_vma.vm_page_prot = PAGE_READONLY;
1793         gate_vma.vm_flags = 0;
1794         return 0;
1795 }
1796 __initcall(gate_vma_init);
1797 #endif
1798
1799 struct vm_area_struct *get_gate_vma(struct task_struct *tsk)
1800 {
1801 #ifdef AT_SYSINFO_EHDR
1802         return &gate_vma;
1803 #else
1804         return 0;
1805 #endif
1806 }
1807
1808 int in_gate_area(struct task_struct *task, unsigned long addr)
1809 {
1810 #ifdef AT_SYSINFO_EHDR
1811         if ((addr >= FIXADDR_USER_START) && (addr < FIXADDR_USER_END))
1812                 return 1;
1813 #endif
1814         return 0;
1815 }
1816
1817 #endif