3e0e9ec8ede09ba8bb11b052811350b7c59d3164
[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  * Aug/Sep 2004 Changed to four level page tables (Andi Kleen)
39  */
40
41 #include <linux/kernel_stat.h>
42 #include <linux/mm.h>
43 #include <linux/hugetlb.h>
44 #include <linux/mman.h>
45 #include <linux/swap.h>
46 #include <linux/highmem.h>
47 #include <linux/pagemap.h>
48 #include <linux/rmap.h>
49 #include <linux/acct.h>
50 #include <linux/module.h>
51 #include <linux/init.h>
52
53 #include <asm/pgalloc.h>
54 #include <asm/uaccess.h>
55 #include <asm/tlb.h>
56 #include <asm/tlbflush.h>
57 #include <asm/pgtable.h>
58
59 #include <linux/swapops.h>
60 #include <linux/elf.h>
61
62 #ifndef CONFIG_DISCONTIGMEM
63 /* use the per-pgdat data instead for discontigmem - mbligh */
64 unsigned long max_mapnr;
65 struct page *mem_map;
66
67 EXPORT_SYMBOL(max_mapnr);
68 EXPORT_SYMBOL(mem_map);
69 #endif
70
71 unsigned long num_physpages;
72 /*
73  * A number of key systems in x86 including ioremap() rely on the assumption
74  * that high_memory defines the upper bound on direct map memory, then end
75  * of ZONE_NORMAL.  Under CONFIG_DISCONTIG this means that max_low_pfn and
76  * highstart_pfn must be the same; there must be no gap between ZONE_NORMAL
77  * and ZONE_HIGHMEM.
78  */
79 void * high_memory;
80 unsigned long vmalloc_earlyreserve;
81
82 EXPORT_SYMBOL(num_physpages);
83 EXPORT_SYMBOL(high_memory);
84 EXPORT_SYMBOL(vmalloc_earlyreserve);
85
86 /*
87  * Note: this doesn't free the actual pages themselves. That
88  * has been handled earlier when unmapping all the memory regions.
89  */
90 static inline void clear_pmd_range(struct mmu_gather *tlb, pmd_t *pmd, unsigned long start, unsigned long end)
91 {
92         struct page *page;
93
94         if (pmd_none(*pmd))
95                 return;
96         if (unlikely(pmd_bad(*pmd))) {
97                 pmd_ERROR(*pmd);
98                 pmd_clear(pmd);
99                 return;
100         }
101         if (!((start | end) & ~PMD_MASK)) {
102                 /* Only clear full, aligned ranges */
103                 page = pmd_page(*pmd);
104                 pmd_clear(pmd);
105                 dec_page_state(nr_page_table_pages);
106                 tlb->mm->nr_ptes--;
107                 pte_free_tlb(tlb, page);
108         }
109 }
110
111 static inline void clear_pud_range(struct mmu_gather *tlb, pud_t *pud, unsigned long start, unsigned long end)
112 {
113         unsigned long addr = start, next;
114         pmd_t *pmd, *__pmd;
115
116         if (pud_none(*pud))
117                 return;
118         if (unlikely(pud_bad(*pud))) {
119                 pud_ERROR(*pud);
120                 pud_clear(pud);
121                 return;
122         }
123
124         pmd = __pmd = pmd_offset(pud, start);
125         do {
126                 next = (addr + PMD_SIZE) & PMD_MASK;
127                 if (next > end || next <= addr)
128                         next = end;
129                 
130                 clear_pmd_range(tlb, pmd, addr, next);
131                 pmd++;
132                 addr = next;
133         } while (addr && (addr < end));
134
135         if (!((start | end) & ~PUD_MASK)) {
136                 /* Only clear full, aligned ranges */
137                 pud_clear(pud);
138                 pmd_free_tlb(tlb, __pmd);
139         }
140 }
141
142
143 static inline void clear_pgd_range(struct mmu_gather *tlb, pgd_t *pgd, unsigned long start, unsigned long end)
144 {
145         unsigned long addr = start, next;
146         pud_t *pud, *__pud;
147
148         if (pgd_none(*pgd))
149                 return;
150         if (unlikely(pgd_bad(*pgd))) {
151                 pgd_ERROR(*pgd);
152                 pgd_clear(pgd);
153                 return;
154         }
155
156         pud = __pud = pud_offset(pgd, start);
157         do {
158                 next = (addr + PUD_SIZE) & PUD_MASK;
159                 if (next > end || next <= addr)
160                         next = end;
161                 
162                 clear_pud_range(tlb, pud, addr, next);
163                 pud++;
164                 addr = next;
165         } while (addr && (addr < end));
166
167         if (!((start | end) & ~PGDIR_MASK)) {
168                 /* Only clear full, aligned ranges */
169                 pgd_clear(pgd);
170                 pud_free_tlb(tlb, __pud);
171         }
172 }
173
174 /*
175  * This function clears user-level page tables of a process.
176  *
177  * Must be called with pagetable lock held.
178  */
179 void clear_page_range(struct mmu_gather *tlb, unsigned long start, unsigned long end)
180 {
181         unsigned long addr = start, next;
182         pgd_t * pgd = pgd_offset(tlb->mm, start);
183         unsigned long i;
184
185         for (i = pgd_index(start); i <= pgd_index(end-1); i++) {
186                 next = (addr + PGDIR_SIZE) & PGDIR_MASK;
187                 if (next > end || next <= addr)
188                         next = end;
189                 
190                 clear_pgd_range(tlb, pgd, addr, next);
191                 pgd++;
192                 addr = next;
193         }
194 }
195
196 pte_t fastcall * pte_alloc_map(struct mm_struct *mm, pmd_t *pmd, unsigned long address)
197 {
198         if (!pmd_present(*pmd)) {
199                 struct page *new;
200
201                 spin_unlock(&mm->page_table_lock);
202                 new = pte_alloc_one(mm, address);
203                 spin_lock(&mm->page_table_lock);
204                 if (!new)
205                         return NULL;
206                 /*
207                  * Because we dropped the lock, we should re-check the
208                  * entry, as somebody else could have populated it..
209                  */
210                 if (pmd_present(*pmd)) {
211                         pte_free(new);
212                         goto out;
213                 }
214                 mm->nr_ptes++;
215                 inc_page_state(nr_page_table_pages);
216                 pmd_populate(mm, pmd, new);
217         }
218 out:
219         return pte_offset_map(pmd, address);
220 }
221
222 pte_t fastcall * pte_alloc_kernel(struct mm_struct *mm, pmd_t *pmd, unsigned long address)
223 {
224         if (!pmd_present(*pmd)) {
225                 pte_t *new;
226
227                 spin_unlock(&mm->page_table_lock);
228                 new = pte_alloc_one_kernel(mm, address);
229                 spin_lock(&mm->page_table_lock);
230                 if (!new)
231                         return NULL;
232
233                 /*
234                  * Because we dropped the lock, we should re-check the
235                  * entry, as somebody else could have populated it..
236                  */
237                 if (pmd_present(*pmd)) {
238                         pte_free_kernel(new);
239                         goto out;
240                 }
241                 pmd_populate_kernel(mm, pmd, new);
242         }
243 out:
244         return pte_offset_kernel(pmd, address);
245 }
246
247 /*
248  * copy one vm_area from one task to the other. Assumes the page tables
249  * already present in the new task to be cleared in the whole range
250  * covered by this vma.
251  *
252  * dst->page_table_lock is held on entry and exit,
253  * but may be dropped within p[mg]d_alloc() and pte_alloc_map().
254  */
255
256 static inline void
257 copy_swap_pte(struct mm_struct *dst_mm, struct mm_struct *src_mm, pte_t pte)
258 {
259         if (pte_file(pte))
260                 return;
261         swap_duplicate(pte_to_swp_entry(pte));
262         if (list_empty(&dst_mm->mmlist)) {
263                 spin_lock(&mmlist_lock);
264                 list_add(&dst_mm->mmlist, &src_mm->mmlist);
265                 spin_unlock(&mmlist_lock);
266         }
267 }
268
269 static inline void
270 copy_one_pte(struct mm_struct *dst_mm,  struct mm_struct *src_mm,
271                 pte_t *dst_pte, pte_t *src_pte, unsigned long vm_flags,
272                 unsigned long addr)
273 {
274         pte_t pte = *src_pte;
275         struct page *page;
276         unsigned long pfn;
277
278         /* pte contains position in swap, so copy. */
279         if (!pte_present(pte)) {
280                 copy_swap_pte(dst_mm, src_mm, pte);
281                 set_pte(dst_pte, pte);
282                 return;
283         }
284         pfn = pte_pfn(pte);
285         /* the pte points outside of valid memory, the
286          * mapping is assumed to be good, meaningful
287          * and not mapped via rmap - duplicate the
288          * mapping as is.
289          */
290         page = NULL;
291         if (pfn_valid(pfn))
292                 page = pfn_to_page(pfn);
293
294         if (!page || PageReserved(page)) {
295                 set_pte(dst_pte, pte);
296                 return;
297         }
298
299         /*
300          * If it's a COW mapping, write protect it both
301          * in the parent and the child
302          */
303         if ((vm_flags & (VM_SHARED | VM_MAYWRITE)) == VM_MAYWRITE) {
304                 ptep_set_wrprotect(src_pte);
305                 pte = *src_pte;
306         }
307
308         /*
309          * If it's a shared mapping, mark it clean in
310          * the child
311          */
312         if (vm_flags & VM_SHARED)
313                 pte = pte_mkclean(pte);
314         pte = pte_mkold(pte);
315         get_page(page);
316         // dst_mm->rss++;
317         vx_rsspages_inc(dst_mm);
318         if (PageAnon(page))
319                 // dst_mm->anon_rss++;
320                 vx_anonpages_inc(dst_mm);
321         set_pte(dst_pte, pte);
322         page_dup_rmap(page);
323 }
324
325 static int copy_pte_range(struct mm_struct *dst_mm,  struct mm_struct *src_mm,
326                 pmd_t *dst_pmd, pmd_t *src_pmd, struct vm_area_struct *vma,
327                 unsigned long addr, unsigned long end)
328 {
329         pte_t *src_pte, *dst_pte;
330         pte_t *s, *d;
331         unsigned long vm_flags = vma->vm_flags;
332
333         d = dst_pte = pte_alloc_map(dst_mm, dst_pmd, addr);
334         if (!dst_pte)
335                 return -ENOMEM;
336
337         spin_lock(&src_mm->page_table_lock);
338         s = src_pte = pte_offset_map_nested(src_pmd, addr);
339         for (; addr < end; addr += PAGE_SIZE, s++, d++) {
340                 if (pte_none(*s))
341                         continue;
342                 copy_one_pte(dst_mm, src_mm, d, s, vm_flags, addr);
343         }
344         pte_unmap_nested(src_pte);
345         pte_unmap(dst_pte);
346         spin_unlock(&src_mm->page_table_lock);
347         cond_resched_lock(&dst_mm->page_table_lock);
348         return 0;
349 }
350
351 static int copy_pmd_range(struct mm_struct *dst_mm,  struct mm_struct *src_mm,
352                 pud_t *dst_pud, pud_t *src_pud, struct vm_area_struct *vma,
353                 unsigned long addr, unsigned long end)
354 {
355         pmd_t *src_pmd, *dst_pmd;
356         int err = 0;
357         unsigned long next;
358
359         src_pmd = pmd_offset(src_pud, addr);
360         dst_pmd = pmd_alloc(dst_mm, dst_pud, addr);
361         if (!dst_pmd)
362                 return -ENOMEM;
363
364         for (; addr < end; addr = next, src_pmd++, dst_pmd++) {
365                 next = (addr + PMD_SIZE) & PMD_MASK;
366                 if (next > end || next <= addr)
367                         next = end;
368                 if (pmd_none(*src_pmd))
369                         continue;
370                 if (pmd_bad(*src_pmd)) {
371                         pmd_ERROR(*src_pmd);
372                         pmd_clear(src_pmd);
373                         continue;
374                 }
375                 err = copy_pte_range(dst_mm, src_mm, dst_pmd, src_pmd,
376                                                         vma, addr, next);
377                 if (err)
378                         break;
379         }
380         return err;
381 }
382
383 static int copy_pud_range(struct mm_struct *dst_mm,  struct mm_struct *src_mm,
384                 pgd_t *dst_pgd, pgd_t *src_pgd, struct vm_area_struct *vma,
385                 unsigned long addr, unsigned long end)
386 {
387         pud_t *src_pud, *dst_pud;
388         int err = 0;
389         unsigned long next;
390
391         src_pud = pud_offset(src_pgd, addr);
392         dst_pud = pud_alloc(dst_mm, dst_pgd, addr);
393         if (!dst_pud)
394                 return -ENOMEM;
395
396         for (; addr < end; addr = next, src_pud++, dst_pud++) {
397                 next = (addr + PUD_SIZE) & PUD_MASK;
398                 if (next > end || next <= addr)
399                         next = end;
400                 if (pud_none(*src_pud))
401                         continue;
402                 if (pud_bad(*src_pud)) {
403                         pud_ERROR(*src_pud);
404                         pud_clear(src_pud);
405                         continue;
406                 }
407                 err = copy_pmd_range(dst_mm, src_mm, dst_pud, src_pud,
408                                                         vma, addr, next);
409                 if (err)
410                         break;
411         }
412         return err;
413 }
414
415 int copy_page_range(struct mm_struct *dst, struct mm_struct *src,
416                 struct vm_area_struct *vma)
417 {
418         pgd_t *src_pgd, *dst_pgd;
419         unsigned long addr, start, end, next;
420         int err = 0;
421
422         if (is_vm_hugetlb_page(vma))
423                 return copy_hugetlb_page_range(dst, src, vma);
424
425         start = vma->vm_start;
426         src_pgd = pgd_offset(src, start);
427         dst_pgd = pgd_offset(dst, start);
428
429         end = vma->vm_end;
430         addr = start;
431         while (addr && (addr < end-1)) {
432                 next = (addr + PGDIR_SIZE) & PGDIR_MASK;
433                 if (next > end || next <= addr)
434                         next = end;
435                 if (pgd_none(*src_pgd))
436                         goto next_pgd;
437                 if (pgd_bad(*src_pgd)) {
438                         pgd_ERROR(*src_pgd);
439                         pgd_clear(src_pgd);
440                         goto next_pgd;
441                 }
442                 err = copy_pud_range(dst, src, dst_pgd, src_pgd,
443                                                         vma, addr, next);
444                 if (err)
445                         break;
446
447 next_pgd:
448                 src_pgd++;
449                 dst_pgd++;
450                 addr = next;
451         }
452
453         return err;
454 }
455
456 static void zap_pte_range(struct mmu_gather *tlb,
457                 pmd_t *pmd, unsigned long address,
458                 unsigned long size, struct zap_details *details)
459 {
460         unsigned long offset;
461         pte_t *ptep;
462
463         if (pmd_none(*pmd))
464                 return;
465         if (unlikely(pmd_bad(*pmd))) {
466                 pmd_ERROR(*pmd);
467                 pmd_clear(pmd);
468                 return;
469         }
470         ptep = pte_offset_map(pmd, address);
471         offset = address & ~PMD_MASK;
472         if (offset + size > PMD_SIZE)
473                 size = PMD_SIZE - offset;
474         size &= PAGE_MASK;
475         if (details && !details->check_mapping && !details->nonlinear_vma)
476                 details = NULL;
477         for (offset=0; offset < size; ptep++, offset += PAGE_SIZE) {
478                 pte_t pte = *ptep;
479                 if (pte_none(pte))
480                         continue;
481                 if (pte_present(pte)) {
482                         struct page *page = NULL;
483                         unsigned long pfn = pte_pfn(pte);
484                         if (pfn_valid(pfn)) {
485                                 page = pfn_to_page(pfn);
486                                 if (PageReserved(page))
487                                         page = NULL;
488                         }
489                         if (unlikely(details) && page) {
490                                 /*
491                                  * unmap_shared_mapping_pages() wants to
492                                  * invalidate cache without truncating:
493                                  * unmap shared but keep private pages.
494                                  */
495                                 if (details->check_mapping &&
496                                     details->check_mapping != page->mapping)
497                                         continue;
498                                 /*
499                                  * Each page->index must be checked when
500                                  * invalidating or truncating nonlinear.
501                                  */
502                                 if (details->nonlinear_vma &&
503                                     (page->index < details->first_index ||
504                                      page->index > details->last_index))
505                                         continue;
506                         }
507                         pte = ptep_get_and_clear(ptep);
508                         tlb_remove_tlb_entry(tlb, ptep, address+offset);
509                         if (unlikely(!page))
510                                 continue;
511                         if (unlikely(details) && details->nonlinear_vma
512                             && linear_page_index(details->nonlinear_vma,
513                                         address+offset) != page->index)
514                                 set_pte(ptep, pgoff_to_pte(page->index));
515                         if (pte_dirty(pte))
516                                 set_page_dirty(page);
517                         if (PageAnon(page))
518                                 // tlb->mm->anon_rss--;
519                                 vx_anonpages_dec(tlb->mm);
520                         else if (pte_young(pte))
521                                 mark_page_accessed(page);
522                         tlb->freed++;
523                         page_remove_rmap(page);
524                         tlb_remove_page(tlb, page);
525                         continue;
526                 }
527                 /*
528                  * If details->check_mapping, we leave swap entries;
529                  * if details->nonlinear_vma, we leave file entries.
530                  */
531                 if (unlikely(details))
532                         continue;
533                 if (!pte_file(pte))
534                         free_swap_and_cache(pte_to_swp_entry(pte));
535                 pte_clear(ptep);
536         }
537         pte_unmap(ptep-1);
538 }
539
540 static void zap_pmd_range(struct mmu_gather *tlb,
541                 pud_t *pud, unsigned long address,
542                 unsigned long size, struct zap_details *details)
543 {
544         pmd_t * pmd;
545         unsigned long end;
546
547         if (pud_none(*pud))
548                 return;
549         if (unlikely(pud_bad(*pud))) {
550                 pud_ERROR(*pud);
551                 pud_clear(pud);
552                 return;
553         }
554         pmd = pmd_offset(pud, address);
555         end = address + size;
556         if (end > ((address + PUD_SIZE) & PUD_MASK))
557                 end = ((address + PUD_SIZE) & PUD_MASK);
558         do {
559                 zap_pte_range(tlb, pmd, address, end - address, details);
560                 address = (address + PMD_SIZE) & PMD_MASK; 
561                 pmd++;
562         } while (address && (address < end));
563 }
564
565 static void zap_pud_range(struct mmu_gather *tlb,
566                 pgd_t * pgd, unsigned long address,
567                 unsigned long end, struct zap_details *details)
568 {
569         pud_t * pud;
570
571         if (pgd_none(*pgd))
572                 return;
573         if (unlikely(pgd_bad(*pgd))) {
574                 pgd_ERROR(*pgd);
575                 pgd_clear(pgd);
576                 return;
577         }
578         pud = pud_offset(pgd, address);
579         do {
580                 zap_pmd_range(tlb, pud, address, end - address, details);
581                 address = (address + PUD_SIZE) & PUD_MASK; 
582                 pud++;
583         } while (address && (address < end));
584 }
585
586 static void unmap_page_range(struct mmu_gather *tlb,
587                 struct vm_area_struct *vma, unsigned long address,
588                 unsigned long end, struct zap_details *details)
589 {
590         unsigned long next;
591         pgd_t *pgd;
592         int i;
593
594         BUG_ON(address >= end);
595         pgd = pgd_offset(vma->vm_mm, address);
596         tlb_start_vma(tlb, vma);
597         for (i = pgd_index(address); i <= pgd_index(end-1); i++) {
598                 next = (address + PGDIR_SIZE) & PGDIR_MASK;
599                 if (next <= address || next > end)
600                         next = end;
601                 zap_pud_range(tlb, pgd, address, next, details);
602                 address = next;
603                 pgd++;
604         }
605         tlb_end_vma(tlb, vma);
606 }
607
608 #ifdef CONFIG_PREEMPT
609 # define ZAP_BLOCK_SIZE (8 * PAGE_SIZE)
610 #else
611 /* No preempt: go for improved straight-line efficiency */
612 # define ZAP_BLOCK_SIZE (1024 * PAGE_SIZE)
613 #endif
614
615 /**
616  * unmap_vmas - unmap a range of memory covered by a list of vma's
617  * @tlbp: address of the caller's struct mmu_gather
618  * @mm: the controlling mm_struct
619  * @vma: the starting vma
620  * @start_addr: virtual address at which to start unmapping
621  * @end_addr: virtual address at which to end unmapping
622  * @nr_accounted: Place number of unmapped pages in vm-accountable vma's here
623  * @details: details of nonlinear truncation or shared cache invalidation
624  *
625  * Returns the number of vma's which were covered by the unmapping.
626  *
627  * Unmap all pages in the vma list.  Called under page_table_lock.
628  *
629  * We aim to not hold page_table_lock for too long (for scheduling latency
630  * reasons).  So zap pages in ZAP_BLOCK_SIZE bytecounts.  This means we need to
631  * return the ending mmu_gather to the caller.
632  *
633  * Only addresses between `start' and `end' will be unmapped.
634  *
635  * The VMA list must be sorted in ascending virtual address order.
636  *
637  * unmap_vmas() assumes that the caller will flush the whole unmapped address
638  * range after unmap_vmas() returns.  So the only responsibility here is to
639  * ensure that any thus-far unmapped pages are flushed before unmap_vmas()
640  * drops the lock and schedules.
641  */
642 int unmap_vmas(struct mmu_gather **tlbp, struct mm_struct *mm,
643                 struct vm_area_struct *vma, unsigned long start_addr,
644                 unsigned long end_addr, unsigned long *nr_accounted,
645                 struct zap_details *details)
646 {
647         unsigned long zap_bytes = ZAP_BLOCK_SIZE;
648         unsigned long tlb_start = 0;    /* For tlb_finish_mmu */
649         int tlb_start_valid = 0;
650         int ret = 0;
651         spinlock_t *i_mmap_lock = details? details->i_mmap_lock: NULL;
652         int fullmm = tlb_is_full_mm(*tlbp);
653
654         for ( ; vma && vma->vm_start < end_addr; vma = vma->vm_next) {
655                 unsigned long start;
656                 unsigned long end;
657
658                 start = max(vma->vm_start, start_addr);
659                 if (start >= vma->vm_end)
660                         continue;
661                 end = min(vma->vm_end, end_addr);
662                 if (end <= vma->vm_start)
663                         continue;
664
665                 if (vma->vm_flags & VM_ACCOUNT)
666                         *nr_accounted += (end - start) >> PAGE_SHIFT;
667
668                 ret++;
669                 while (start != end) {
670                         unsigned long block;
671
672                         if (!tlb_start_valid) {
673                                 tlb_start = start;
674                                 tlb_start_valid = 1;
675                         }
676
677                         if (is_vm_hugetlb_page(vma)) {
678                                 block = end - start;
679                                 unmap_hugepage_range(vma, start, end);
680                         } else {
681                                 block = min(zap_bytes, end - start);
682                                 unmap_page_range(*tlbp, vma, start,
683                                                 start + block, details);
684                         }
685
686                         start += block;
687                         zap_bytes -= block;
688                         if ((long)zap_bytes > 0)
689                                 continue;
690
691                         tlb_finish_mmu(*tlbp, tlb_start, start);
692
693                         if (need_resched() ||
694                                 need_lockbreak(&mm->page_table_lock) ||
695                                 (i_mmap_lock && need_lockbreak(i_mmap_lock))) {
696                                 if (i_mmap_lock) {
697                                         /* must reset count of rss freed */
698                                         *tlbp = tlb_gather_mmu(mm, fullmm);
699                                         details->break_addr = start;
700                                         goto out;
701                                 }
702                                 spin_unlock(&mm->page_table_lock);
703                                 cond_resched();
704                                 spin_lock(&mm->page_table_lock);
705                         }
706
707                         *tlbp = tlb_gather_mmu(mm, fullmm);
708                         tlb_start_valid = 0;
709                         zap_bytes = ZAP_BLOCK_SIZE;
710                 }
711         }
712 out:
713         return ret;
714 }
715
716 /**
717  * zap_page_range - remove user pages in a given range
718  * @vma: vm_area_struct holding the applicable pages
719  * @address: starting address of pages to zap
720  * @size: number of bytes to zap
721  * @details: details of nonlinear truncation or shared cache invalidation
722  */
723 void zap_page_range(struct vm_area_struct *vma, unsigned long address,
724                 unsigned long size, struct zap_details *details)
725 {
726         struct mm_struct *mm = vma->vm_mm;
727         struct mmu_gather *tlb;
728         unsigned long end = address + size;
729         unsigned long nr_accounted = 0;
730
731         if (is_vm_hugetlb_page(vma)) {
732                 zap_hugepage_range(vma, address, size);
733                 return;
734         }
735
736         lru_add_drain();
737         spin_lock(&mm->page_table_lock);
738         tlb = tlb_gather_mmu(mm, 0);
739         unmap_vmas(&tlb, mm, vma, address, end, &nr_accounted, details);
740         tlb_finish_mmu(tlb, address, end);
741         acct_update_integrals();
742         spin_unlock(&mm->page_table_lock);
743 }
744
745 /*
746  * Do a quick page-table lookup for a single page.
747  * mm->page_table_lock must be held.
748  */
749 static struct page *
750 __follow_page(struct mm_struct *mm, unsigned long address, int read, int write)
751 {
752         pgd_t *pgd;
753         pud_t *pud;
754         pmd_t *pmd;
755         pte_t *ptep, pte;
756         unsigned long pfn;
757         struct page *page;
758
759         page = follow_huge_addr(mm, address, write);
760         if (! IS_ERR(page))
761                 return page;
762
763         pgd = pgd_offset(mm, address);
764         if (pgd_none(*pgd) || unlikely(pgd_bad(*pgd)))
765                 goto out;
766
767         pud = pud_offset(pgd, address);
768         if (pud_none(*pud) || unlikely(pud_bad(*pud)))
769                 goto out;
770         
771         pmd = pmd_offset(pud, address);
772         if (pmd_none(*pmd) || unlikely(pmd_bad(*pmd)))
773                 goto out;
774         if (pmd_huge(*pmd))
775                 return follow_huge_pmd(mm, address, pmd, write);
776
777         ptep = pte_offset_map(pmd, address);
778         if (!ptep)
779                 goto out;
780
781         pte = *ptep;
782         pte_unmap(ptep);
783         if (pte_present(pte)) {
784                 if (write && !pte_write(pte))
785                         goto out;
786                 if (read && !pte_read(pte))
787                         goto out;
788                 pfn = pte_pfn(pte);
789                 if (pfn_valid(pfn)) {
790                         page = pfn_to_page(pfn);
791                         if (write && !pte_dirty(pte) && !PageDirty(page))
792                                 set_page_dirty(page);
793                         mark_page_accessed(page);
794                         return page;
795                 }
796         }
797
798 out:
799         return NULL;
800 }
801
802 struct page *
803 follow_page(struct mm_struct *mm, unsigned long address, int write)
804 {
805         return __follow_page(mm, address, /*read*/0, write);
806 }
807
808 int
809 check_user_page_readable(struct mm_struct *mm, unsigned long address)
810 {
811         return __follow_page(mm, address, /*read*/1, /*write*/0) != NULL;
812 }
813
814 EXPORT_SYMBOL(check_user_page_readable);
815
816 /* 
817  * Given a physical address, is there a useful struct page pointing to
818  * it?  This may become more complex in the future if we start dealing
819  * with IO-aperture pages for direct-IO.
820  */
821
822 static inline struct page *get_page_map(struct page *page)
823 {
824         if (!pfn_valid(page_to_pfn(page)))
825                 return NULL;
826         return page;
827 }
828
829
830 static inline int
831 untouched_anonymous_page(struct mm_struct* mm, struct vm_area_struct *vma,
832                          unsigned long address)
833 {
834         pgd_t *pgd;
835         pud_t *pud;
836         pmd_t *pmd;
837
838         /* Check if the vma is for an anonymous mapping. */
839         if (vma->vm_ops && vma->vm_ops->nopage)
840                 return 0;
841
842         /* Check if page directory entry exists. */
843         pgd = pgd_offset(mm, address);
844         if (pgd_none(*pgd) || unlikely(pgd_bad(*pgd)))
845                 return 1;
846
847         pud = pud_offset(pgd, address);
848         if (pud_none(*pud) || unlikely(pud_bad(*pud)))
849                 return 1;
850
851         /* Check if page middle directory entry exists. */
852         pmd = pmd_offset(pud, address);
853         if (pmd_none(*pmd) || unlikely(pmd_bad(*pmd)))
854                 return 1;
855
856         /* There is a pte slot for 'address' in 'mm'. */
857         return 0;
858 }
859
860
861 int get_user_pages(struct task_struct *tsk, struct mm_struct *mm,
862                 unsigned long start, int len, int write, int force,
863                 struct page **pages, struct vm_area_struct **vmas)
864 {
865         int i;
866         unsigned int flags;
867
868         /* 
869          * Require read or write permissions.
870          * If 'force' is set, we only require the "MAY" flags.
871          */
872         flags = write ? (VM_WRITE | VM_MAYWRITE) : (VM_READ | VM_MAYREAD);
873         flags &= force ? (VM_MAYREAD | VM_MAYWRITE) : (VM_READ | VM_WRITE);
874         i = 0;
875
876         do {
877                 struct vm_area_struct * vma;
878
879                 vma = find_extend_vma(mm, start);
880                 if (!vma && in_gate_area(tsk, start)) {
881                         unsigned long pg = start & PAGE_MASK;
882                         struct vm_area_struct *gate_vma = get_gate_vma(tsk);
883                         pgd_t *pgd;
884                         pud_t *pud;
885                         pmd_t *pmd;
886                         pte_t *pte;
887                         if (write) /* user gate pages are read-only */
888                                 return i ? : -EFAULT;
889                         if (pg > TASK_SIZE)
890                                 pgd = pgd_offset_k(pg);
891                         else
892                                 pgd = pgd_offset_gate(mm, pg);
893                         BUG_ON(pgd_none(*pgd));
894                         pud = pud_offset(pgd, pg);
895                         BUG_ON(pud_none(*pud));
896                         pmd = pmd_offset(pud, pg);
897                         BUG_ON(pmd_none(*pmd));
898                         pte = pte_offset_map(pmd, pg);
899                         BUG_ON(pte_none(*pte));
900                         if (pages) {
901                                 pages[i] = pte_page(*pte);
902                                 get_page(pages[i]);
903                         }
904                         pte_unmap(pte);
905                         if (vmas)
906                                 vmas[i] = gate_vma;
907                         i++;
908                         start += PAGE_SIZE;
909                         len--;
910                         continue;
911                 }
912
913                 if (!vma || (vma->vm_flags & VM_IO)
914                                 || !(flags & vma->vm_flags))
915                         return i ? : -EFAULT;
916
917                 if (is_vm_hugetlb_page(vma)) {
918                         i = follow_hugetlb_page(mm, vma, pages, vmas,
919                                                 &start, &len, i);
920                         continue;
921                 }
922                 spin_lock(&mm->page_table_lock);
923                 do {
924                         struct page *map;
925                         int lookup_write = write;
926
927                         cond_resched_lock(&mm->page_table_lock);
928                         while (!(map = follow_page(mm, start, lookup_write))) {
929                                 /*
930                                  * Shortcut for anonymous pages. We don't want
931                                  * to force the creation of pages tables for
932                                  * insanly big anonymously mapped areas that
933                                  * nobody touched so far. This is important
934                                  * for doing a core dump for these mappings.
935                                  */
936                                 if (!lookup_write &&
937                                     untouched_anonymous_page(mm,vma,start)) {
938                                         map = ZERO_PAGE(start);
939                                         break;
940                                 }
941                                 spin_unlock(&mm->page_table_lock);
942                                 switch (handle_mm_fault(mm,vma,start,write)) {
943                                 case VM_FAULT_MINOR:
944                                         tsk->min_flt++;
945                                         break;
946                                 case VM_FAULT_MAJOR:
947                                         tsk->maj_flt++;
948                                         break;
949                                 case VM_FAULT_SIGBUS:
950                                         return i ? i : -EFAULT;
951                                 case VM_FAULT_OOM:
952                                         return i ? i : -ENOMEM;
953                                 default:
954                                         BUG();
955                                 }
956                                 /*
957                                  * Now that we have performed a write fault
958                                  * and surely no longer have a shared page we
959                                  * shouldn't write, we shouldn't ignore an
960                                  * unwritable page in the page table if
961                                  * we are forcing write access.
962                                  */
963                                 lookup_write = write && !force;
964                                 spin_lock(&mm->page_table_lock);
965                         }
966                         if (pages) {
967                                 pages[i] = get_page_map(map);
968                                 if (!pages[i]) {
969                                         spin_unlock(&mm->page_table_lock);
970                                         while (i--)
971                                                 page_cache_release(pages[i]);
972                                         i = -EFAULT;
973                                         goto out;
974                                 }
975                                 flush_dcache_page(pages[i]);
976                                 if (!PageReserved(pages[i]))
977                                         page_cache_get(pages[i]);
978                         }
979                         if (vmas)
980                                 vmas[i] = vma;
981                         i++;
982                         start += PAGE_SIZE;
983                         len--;
984                 } while(len && start < vma->vm_end);
985                 spin_unlock(&mm->page_table_lock);
986         } while(len);
987 out:
988         return i;
989 }
990
991 EXPORT_SYMBOL(get_user_pages);
992
993 static void zeromap_pte_range(pte_t * pte, unsigned long address,
994                                      unsigned long size, pgprot_t prot)
995 {
996         unsigned long end;
997
998         address &= ~PMD_MASK;
999         end = address + size;
1000         if (end > PMD_SIZE)
1001                 end = PMD_SIZE;
1002         do {
1003                 pte_t zero_pte = pte_wrprotect(mk_pte(ZERO_PAGE(address), prot));
1004                 BUG_ON(!pte_none(*pte));
1005                 set_pte(pte, zero_pte);
1006                 address += PAGE_SIZE;
1007                 pte++;
1008         } while (address && (address < end));
1009 }
1010
1011 static inline int zeromap_pmd_range(struct mm_struct *mm, pmd_t * pmd,
1012                 unsigned long address, unsigned long size, pgprot_t prot)
1013 {
1014         unsigned long base, end;
1015
1016         base = address & PUD_MASK;
1017         address &= ~PUD_MASK;
1018         end = address + size;
1019         if (end > PUD_SIZE)
1020                 end = PUD_SIZE;
1021         do {
1022                 pte_t * pte = pte_alloc_map(mm, pmd, base + address);
1023                 if (!pte)
1024                         return -ENOMEM;
1025                 zeromap_pte_range(pte, base + address, end - address, prot);
1026                 pte_unmap(pte);
1027                 address = (address + PMD_SIZE) & PMD_MASK;
1028                 pmd++;
1029         } while (address && (address < end));
1030         return 0;
1031 }
1032
1033 static inline int zeromap_pud_range(struct mm_struct *mm, pud_t * pud,
1034                                     unsigned long address,
1035                                     unsigned long size, pgprot_t prot)
1036 {
1037         unsigned long base, end;
1038         int error = 0;
1039
1040         base = address & PGDIR_MASK;
1041         address &= ~PGDIR_MASK;
1042         end = address + size;
1043         if (end > PGDIR_SIZE)
1044                 end = PGDIR_SIZE;
1045         do {
1046                 pmd_t * pmd = pmd_alloc(mm, pud, base + address);
1047                 error = -ENOMEM;
1048                 if (!pmd)
1049                         break;
1050                 error = zeromap_pmd_range(mm, pmd, base + address,
1051                                           end - address, prot);
1052                 if (error)
1053                         break;
1054                 address = (address + PUD_SIZE) & PUD_MASK;
1055                 pud++;
1056         } while (address && (address < end));
1057         return 0;
1058 }
1059
1060 int zeromap_page_range(struct vm_area_struct *vma, unsigned long address,
1061                                         unsigned long size, pgprot_t prot)
1062 {
1063         int i;
1064         int error = 0;
1065         pgd_t * pgd;
1066         unsigned long beg = address;
1067         unsigned long end = address + size;
1068         unsigned long next;
1069         struct mm_struct *mm = vma->vm_mm;
1070
1071         pgd = pgd_offset(mm, address);
1072         flush_cache_range(vma, beg, end);
1073         BUG_ON(address >= end);
1074         BUG_ON(end > vma->vm_end);
1075
1076         spin_lock(&mm->page_table_lock);
1077         for (i = pgd_index(address); i <= pgd_index(end-1); i++) {
1078                 pud_t *pud = pud_alloc(mm, pgd, address);
1079                 error = -ENOMEM;
1080                 if (!pud)
1081                         break;
1082                 next = (address + PGDIR_SIZE) & PGDIR_MASK;
1083                 if (next <= beg || next > end)
1084                         next = end;
1085                 error = zeromap_pud_range(mm, pud, address,
1086                                                 next - address, prot);
1087                 if (error)
1088                         break;
1089                 address = next;
1090                 pgd++;
1091         }
1092         /*
1093          * Why flush? zeromap_pte_range has a BUG_ON for !pte_none()
1094          */
1095         flush_tlb_range(vma, beg, end);
1096         spin_unlock(&mm->page_table_lock);
1097         return error;
1098 }
1099
1100 /*
1101  * maps a range of physical memory into the requested pages. the old
1102  * mappings are removed. any references to nonexistent pages results
1103  * in null mappings (currently treated as "copy-on-access")
1104  */
1105 static inline void
1106 remap_pte_range(pte_t * pte, unsigned long address, unsigned long size,
1107                 unsigned long pfn, pgprot_t prot)
1108 {
1109         unsigned long end;
1110
1111         address &= ~PMD_MASK;
1112         end = address + size;
1113         if (end > PMD_SIZE)
1114                 end = PMD_SIZE;
1115         do {
1116                 BUG_ON(!pte_none(*pte));
1117                 if (!pfn_valid(pfn) || PageReserved(pfn_to_page(pfn)))
1118                         set_pte(pte, pfn_pte(pfn, prot));
1119                 address += PAGE_SIZE;
1120                 pfn++;
1121                 pte++;
1122         } while (address && (address < end));
1123 }
1124
1125 static inline int
1126 remap_pmd_range(struct mm_struct *mm, pmd_t * pmd, unsigned long address,
1127                 unsigned long size, unsigned long pfn, pgprot_t prot)
1128 {
1129         unsigned long base, end;
1130
1131         base = address & PUD_MASK;
1132         address &= ~PUD_MASK;
1133         end = address + size;
1134         if (end > PUD_SIZE)
1135                 end = PUD_SIZE;
1136         pfn -= (address >> PAGE_SHIFT);
1137         do {
1138                 pte_t * pte = pte_alloc_map(mm, pmd, base + address);
1139                 if (!pte)
1140                         return -ENOMEM;
1141                 remap_pte_range(pte, base + address, end - address,
1142                                 (address >> PAGE_SHIFT) + pfn, prot);
1143                 pte_unmap(pte);
1144                 address = (address + PMD_SIZE) & PMD_MASK;
1145                 pmd++;
1146         } while (address && (address < end));
1147         return 0;
1148 }
1149
1150 static inline int remap_pud_range(struct mm_struct *mm, pud_t * pud,
1151                                   unsigned long address, unsigned long size,
1152                                   unsigned long pfn, pgprot_t prot)
1153 {
1154         unsigned long base, end;
1155         int error;
1156
1157         base = address & PGDIR_MASK;
1158         address &= ~PGDIR_MASK;
1159         end = address + size;
1160         if (end > PGDIR_SIZE)
1161                 end = PGDIR_SIZE;
1162         pfn -= address >> PAGE_SHIFT;
1163         do {
1164                 pmd_t *pmd = pmd_alloc(mm, pud, base+address);
1165                 error = -ENOMEM;
1166                 if (!pmd)
1167                         break;
1168                 error = remap_pmd_range(mm, pmd, base + address, end - address,
1169                                 (address >> PAGE_SHIFT) + pfn, prot);
1170                 if (error)
1171                         break;
1172                 address = (address + PUD_SIZE) & PUD_MASK;
1173                 pud++;
1174         } while (address && (address < end));
1175         return error;
1176 }
1177
1178 /*  Note: this is only safe if the mm semaphore is held when called. */
1179 int remap_pfn_range(struct vm_area_struct *vma, unsigned long from,
1180                     unsigned long pfn, unsigned long size, pgprot_t prot)
1181 {
1182         int error = 0;
1183         pgd_t *pgd;
1184         unsigned long beg = from;
1185         unsigned long end = from + size;
1186         unsigned long next;
1187         struct mm_struct *mm = vma->vm_mm;
1188         int i;
1189
1190         pfn -= from >> PAGE_SHIFT;
1191         pgd = pgd_offset(mm, from);
1192         flush_cache_range(vma, beg, end);
1193         BUG_ON(from >= end);
1194
1195         /*
1196          * Physically remapped pages are special. Tell the
1197          * rest of the world about it:
1198          *   VM_IO tells people not to look at these pages
1199          *      (accesses can have side effects).
1200          *   VM_RESERVED tells swapout not to try to touch
1201          *      this region.
1202          */
1203         vma->vm_flags |= VM_IO | VM_RESERVED;
1204
1205         spin_lock(&mm->page_table_lock);
1206         for (i = pgd_index(beg); i <= pgd_index(end-1); i++) {
1207                 pud_t *pud = pud_alloc(mm, pgd, from);
1208                 error = -ENOMEM;
1209                 if (!pud)
1210                         break;
1211                 next = (from + PGDIR_SIZE) & PGDIR_MASK;
1212                 if (next > end || next <= from)
1213                         next = end;
1214                 error = remap_pud_range(mm, pud, from, end - from,
1215                                         pfn + (from >> PAGE_SHIFT), prot);
1216                 if (error)
1217                         break;
1218                 from = next;
1219                 pgd++;
1220         }
1221         /*
1222          * Why flush? remap_pte_range has a BUG_ON for !pte_none()
1223          */
1224         flush_tlb_range(vma, beg, end);
1225         spin_unlock(&mm->page_table_lock);
1226
1227         return error;
1228 }
1229
1230 EXPORT_SYMBOL(remap_pfn_range);
1231
1232 /*
1233  * Do pte_mkwrite, but only if the vma says VM_WRITE.  We do this when
1234  * servicing faults for write access.  In the normal case, do always want
1235  * pte_mkwrite.  But get_user_pages can cause write faults for mappings
1236  * that do not have writing enabled, when used by access_process_vm.
1237  */
1238 static inline pte_t maybe_mkwrite(pte_t pte, struct vm_area_struct *vma)
1239 {
1240         if (likely(vma->vm_flags & VM_WRITE))
1241                 pte = pte_mkwrite(pte);
1242         return pte;
1243 }
1244
1245 /*
1246  * We hold the mm semaphore for reading and vma->vm_mm->page_table_lock
1247  */
1248 static inline void break_cow(struct vm_area_struct * vma, struct page * new_page, unsigned long address, 
1249                 pte_t *page_table)
1250 {
1251         pte_t entry;
1252
1253         flush_cache_page(vma, address);
1254         entry = maybe_mkwrite(pte_mkdirty(mk_pte(new_page, vma->vm_page_prot)),
1255                               vma);
1256         ptep_establish(vma, address, page_table, entry);
1257         update_mmu_cache(vma, address, entry);
1258 }
1259
1260 /*
1261  * This routine handles present pages, when users try to write
1262  * to a shared page. It is done by copying the page to a new address
1263  * and decrementing the shared-page counter for the old page.
1264  *
1265  * Goto-purists beware: the only reason for goto's here is that it results
1266  * in better assembly code.. The "default" path will see no jumps at all.
1267  *
1268  * Note that this routine assumes that the protection checks have been
1269  * done by the caller (the low-level page fault routine in most cases).
1270  * Thus we can safely just mark it writable once we've done any necessary
1271  * COW.
1272  *
1273  * We also mark the page dirty at this point even though the page will
1274  * change only once the write actually happens. This avoids a few races,
1275  * and potentially makes it more efficient.
1276  *
1277  * We hold the mm semaphore and the page_table_lock on entry and exit
1278  * with the page_table_lock released.
1279  */
1280 static int do_wp_page(struct mm_struct *mm, struct vm_area_struct * vma,
1281         unsigned long address, pte_t *page_table, pmd_t *pmd, pte_t pte)
1282 {
1283         struct page *old_page, *new_page;
1284         unsigned long pfn = pte_pfn(pte);
1285         pte_t entry;
1286
1287         if (unlikely(!pfn_valid(pfn))) {
1288                 /*
1289                  * This should really halt the system so it can be debugged or
1290                  * at least the kernel stops what it's doing before it corrupts
1291                  * data, but for the moment just pretend this is OOM.
1292                  */
1293                 pte_unmap(page_table);
1294                 printk(KERN_ERR "do_wp_page: bogus page at address %08lx\n",
1295                                 address);
1296                 spin_unlock(&mm->page_table_lock);
1297                 return VM_FAULT_OOM;
1298         }
1299         old_page = pfn_to_page(pfn);
1300
1301         if (!TestSetPageLocked(old_page)) {
1302                 int reuse = can_share_swap_page(old_page);
1303                 unlock_page(old_page);
1304                 if (reuse) {
1305                         flush_cache_page(vma, address);
1306                         entry = maybe_mkwrite(pte_mkyoung(pte_mkdirty(pte)),
1307                                               vma);
1308                         ptep_set_access_flags(vma, address, page_table, entry, 1);
1309                         update_mmu_cache(vma, address, entry);
1310                         pte_unmap(page_table);
1311                         spin_unlock(&mm->page_table_lock);
1312                         return VM_FAULT_MINOR;
1313                 }
1314         }
1315         pte_unmap(page_table);
1316
1317         /*
1318          * Ok, we need to copy. Oh, well..
1319          */
1320         if (!PageReserved(old_page))
1321                 page_cache_get(old_page);
1322         spin_unlock(&mm->page_table_lock);
1323
1324         if (unlikely(anon_vma_prepare(vma)))
1325                 goto no_new_page;
1326         if (old_page == ZERO_PAGE(address)) {
1327                 new_page = alloc_zeroed_user_highpage(vma, address);
1328                 if (!new_page)
1329                         goto no_new_page;
1330         } else {
1331                 new_page = alloc_page_vma(GFP_HIGHUSER, vma, address);
1332                 if (!new_page)
1333                         goto no_new_page;
1334                 copy_user_highpage(new_page, old_page, address);
1335         }
1336         /*
1337          * Re-check the pte - we dropped the lock
1338          */
1339         spin_lock(&mm->page_table_lock);
1340         page_table = pte_offset_map(pmd, address);
1341         if (likely(pte_same(*page_table, pte))) {
1342                 if (PageAnon(old_page))
1343                         // mm->anon_rss--;
1344                         vx_anonpages_dec(mm);
1345                 if (PageReserved(old_page)) {
1346                         // ++mm->rss;
1347                         vx_rsspages_inc(mm);
1348                         acct_update_integrals();
1349                         update_mem_hiwater();
1350                 } else
1351                         page_remove_rmap(old_page);
1352                 break_cow(vma, new_page, address, page_table);
1353                 lru_cache_add_active(new_page);
1354                 page_add_anon_rmap(new_page, vma, address);
1355
1356                 /* Free the old page.. */
1357                 new_page = old_page;
1358         }
1359         pte_unmap(page_table);
1360         page_cache_release(new_page);
1361         page_cache_release(old_page);
1362         spin_unlock(&mm->page_table_lock);
1363         return VM_FAULT_MINOR;
1364
1365 no_new_page:
1366         page_cache_release(old_page);
1367         return VM_FAULT_OOM;
1368 }
1369
1370 /*
1371  * Helper functions for unmap_mapping_range().
1372  *
1373  * __ Notes on dropping i_mmap_lock to reduce latency while unmapping __
1374  *
1375  * We have to restart searching the prio_tree whenever we drop the lock,
1376  * since the iterator is only valid while the lock is held, and anyway
1377  * a later vma might be split and reinserted earlier while lock dropped.
1378  *
1379  * The list of nonlinear vmas could be handled more efficiently, using
1380  * a placeholder, but handle it in the same way until a need is shown.
1381  * It is important to search the prio_tree before nonlinear list: a vma
1382  * may become nonlinear and be shifted from prio_tree to nonlinear list
1383  * while the lock is dropped; but never shifted from list to prio_tree.
1384  *
1385  * In order to make forward progress despite restarting the search,
1386  * vm_truncate_count is used to mark a vma as now dealt with, so we can
1387  * quickly skip it next time around.  Since the prio_tree search only
1388  * shows us those vmas affected by unmapping the range in question, we
1389  * can't efficiently keep all vmas in step with mapping->truncate_count:
1390  * so instead reset them all whenever it wraps back to 0 (then go to 1).
1391  * mapping->truncate_count and vma->vm_truncate_count are protected by
1392  * i_mmap_lock.
1393  *
1394  * In order to make forward progress despite repeatedly restarting some
1395  * large vma, note the break_addr set by unmap_vmas when it breaks out:
1396  * and restart from that address when we reach that vma again.  It might
1397  * have been split or merged, shrunk or extended, but never shifted: so
1398  * restart_addr remains valid so long as it remains in the vma's range.
1399  * unmap_mapping_range forces truncate_count to leap over page-aligned
1400  * values so we can save vma's restart_addr in its truncate_count field.
1401  */
1402 #define is_restart_addr(truncate_count) (!((truncate_count) & ~PAGE_MASK))
1403
1404 static void reset_vma_truncate_counts(struct address_space *mapping)
1405 {
1406         struct vm_area_struct *vma;
1407         struct prio_tree_iter iter;
1408
1409         vma_prio_tree_foreach(vma, &iter, &mapping->i_mmap, 0, ULONG_MAX)
1410                 vma->vm_truncate_count = 0;
1411         list_for_each_entry(vma, &mapping->i_mmap_nonlinear, shared.vm_set.list)
1412                 vma->vm_truncate_count = 0;
1413 }
1414
1415 static int unmap_mapping_range_vma(struct vm_area_struct *vma,
1416                 unsigned long start_addr, unsigned long end_addr,
1417                 struct zap_details *details)
1418 {
1419         unsigned long restart_addr;
1420         int need_break;
1421
1422 again:
1423         restart_addr = vma->vm_truncate_count;
1424         if (is_restart_addr(restart_addr) && start_addr < restart_addr) {
1425                 start_addr = restart_addr;
1426                 if (start_addr >= end_addr) {
1427                         /* Top of vma has been split off since last time */
1428                         vma->vm_truncate_count = details->truncate_count;
1429                         return 0;
1430                 }
1431         }
1432
1433         details->break_addr = end_addr;
1434         zap_page_range(vma, start_addr, end_addr - start_addr, details);
1435
1436         /*
1437          * We cannot rely on the break test in unmap_vmas:
1438          * on the one hand, we don't want to restart our loop
1439          * just because that broke out for the page_table_lock;
1440          * on the other hand, it does no test when vma is small.
1441          */
1442         need_break = need_resched() ||
1443                         need_lockbreak(details->i_mmap_lock);
1444
1445         if (details->break_addr >= end_addr) {
1446                 /* We have now completed this vma: mark it so */
1447                 vma->vm_truncate_count = details->truncate_count;
1448                 if (!need_break)
1449                         return 0;
1450         } else {
1451                 /* Note restart_addr in vma's truncate_count field */
1452                 vma->vm_truncate_count = details->break_addr;
1453                 if (!need_break)
1454                         goto again;
1455         }
1456
1457         spin_unlock(details->i_mmap_lock);
1458         cond_resched();
1459         spin_lock(details->i_mmap_lock);
1460         return -EINTR;
1461 }
1462
1463 static inline void unmap_mapping_range_tree(struct prio_tree_root *root,
1464                                             struct zap_details *details)
1465 {
1466         struct vm_area_struct *vma;
1467         struct prio_tree_iter iter;
1468         pgoff_t vba, vea, zba, zea;
1469
1470 restart:
1471         vma_prio_tree_foreach(vma, &iter, root,
1472                         details->first_index, details->last_index) {
1473                 /* Skip quickly over those we have already dealt with */
1474                 if (vma->vm_truncate_count == details->truncate_count)
1475                         continue;
1476
1477                 vba = vma->vm_pgoff;
1478                 vea = vba + ((vma->vm_end - vma->vm_start) >> PAGE_SHIFT) - 1;
1479                 /* Assume for now that PAGE_CACHE_SHIFT == PAGE_SHIFT */
1480                 zba = details->first_index;
1481                 if (zba < vba)
1482                         zba = vba;
1483                 zea = details->last_index;
1484                 if (zea > vea)
1485                         zea = vea;
1486
1487                 if (unmap_mapping_range_vma(vma,
1488                         ((zba - vba) << PAGE_SHIFT) + vma->vm_start,
1489                         ((zea - vba + 1) << PAGE_SHIFT) + vma->vm_start,
1490                                 details) < 0)
1491                         goto restart;
1492         }
1493 }
1494
1495 static inline void unmap_mapping_range_list(struct list_head *head,
1496                                             struct zap_details *details)
1497 {
1498         struct vm_area_struct *vma;
1499
1500         /*
1501          * In nonlinear VMAs there is no correspondence between virtual address
1502          * offset and file offset.  So we must perform an exhaustive search
1503          * across *all* the pages in each nonlinear VMA, not just the pages
1504          * whose virtual address lies outside the file truncation point.
1505          */
1506 restart:
1507         list_for_each_entry(vma, head, shared.vm_set.list) {
1508                 /* Skip quickly over those we have already dealt with */
1509                 if (vma->vm_truncate_count == details->truncate_count)
1510                         continue;
1511                 details->nonlinear_vma = vma;
1512                 if (unmap_mapping_range_vma(vma, vma->vm_start,
1513                                         vma->vm_end, details) < 0)
1514                         goto restart;
1515         }
1516 }
1517
1518 /**
1519  * unmap_mapping_range - unmap the portion of all mmaps
1520  * in the specified address_space corresponding to the specified
1521  * page range in the underlying file.
1522  * @address_space: the address space containing mmaps to be unmapped.
1523  * @holebegin: byte in first page to unmap, relative to the start of
1524  * the underlying file.  This will be rounded down to a PAGE_SIZE
1525  * boundary.  Note that this is different from vmtruncate(), which
1526  * must keep the partial page.  In contrast, we must get rid of
1527  * partial pages.
1528  * @holelen: size of prospective hole in bytes.  This will be rounded
1529  * up to a PAGE_SIZE boundary.  A holelen of zero truncates to the
1530  * end of the file.
1531  * @even_cows: 1 when truncating a file, unmap even private COWed pages;
1532  * but 0 when invalidating pagecache, don't throw away private data.
1533  */
1534 void unmap_mapping_range(struct address_space *mapping,
1535                 loff_t const holebegin, loff_t const holelen, int even_cows)
1536 {
1537         struct zap_details details;
1538         pgoff_t hba = holebegin >> PAGE_SHIFT;
1539         pgoff_t hlen = (holelen + PAGE_SIZE - 1) >> PAGE_SHIFT;
1540
1541         /* Check for overflow. */
1542         if (sizeof(holelen) > sizeof(hlen)) {
1543                 long long holeend =
1544                         (holebegin + holelen + PAGE_SIZE - 1) >> PAGE_SHIFT;
1545                 if (holeend & ~(long long)ULONG_MAX)
1546                         hlen = ULONG_MAX - hba + 1;
1547         }
1548
1549         details.check_mapping = even_cows? NULL: mapping;
1550         details.nonlinear_vma = NULL;
1551         details.first_index = hba;
1552         details.last_index = hba + hlen - 1;
1553         if (details.last_index < details.first_index)
1554                 details.last_index = ULONG_MAX;
1555         details.i_mmap_lock = &mapping->i_mmap_lock;
1556
1557         spin_lock(&mapping->i_mmap_lock);
1558
1559         /* serialize i_size write against truncate_count write */
1560         smp_wmb();
1561         /* Protect against page faults, and endless unmapping loops */
1562         mapping->truncate_count++;
1563         /*
1564          * For archs where spin_lock has inclusive semantics like ia64
1565          * this smp_mb() will prevent to read pagetable contents
1566          * before the truncate_count increment is visible to
1567          * other cpus.
1568          */
1569         smp_mb();
1570         if (unlikely(is_restart_addr(mapping->truncate_count))) {
1571                 if (mapping->truncate_count == 0)
1572                         reset_vma_truncate_counts(mapping);
1573                 mapping->truncate_count++;
1574         }
1575         details.truncate_count = mapping->truncate_count;
1576
1577         if (unlikely(!prio_tree_empty(&mapping->i_mmap)))
1578                 unmap_mapping_range_tree(&mapping->i_mmap, &details);
1579         if (unlikely(!list_empty(&mapping->i_mmap_nonlinear)))
1580                 unmap_mapping_range_list(&mapping->i_mmap_nonlinear, &details);
1581         spin_unlock(&mapping->i_mmap_lock);
1582 }
1583 EXPORT_SYMBOL(unmap_mapping_range);
1584
1585 /*
1586  * Handle all mappings that got truncated by a "truncate()"
1587  * system call.
1588  *
1589  * NOTE! We have to be ready to update the memory sharing
1590  * between the file and the memory map for a potential last
1591  * incomplete page.  Ugly, but necessary.
1592  */
1593 int vmtruncate(struct inode * inode, loff_t offset)
1594 {
1595         struct address_space *mapping = inode->i_mapping;
1596         unsigned long limit;
1597
1598         if (inode->i_size < offset)
1599                 goto do_expand;
1600         /*
1601          * truncation of in-use swapfiles is disallowed - it would cause
1602          * subsequent swapout to scribble on the now-freed blocks.
1603          */
1604         if (IS_SWAPFILE(inode))
1605                 goto out_busy;
1606         i_size_write(inode, offset);
1607         unmap_mapping_range(mapping, offset + PAGE_SIZE - 1, 0, 1);
1608         truncate_inode_pages(mapping, offset);
1609         goto out_truncate;
1610
1611 do_expand:
1612         limit = current->signal->rlim[RLIMIT_FSIZE].rlim_cur;
1613         if (limit != RLIM_INFINITY && offset > limit)
1614                 goto out_sig;
1615         if (offset > inode->i_sb->s_maxbytes)
1616                 goto out_big;
1617         i_size_write(inode, offset);
1618
1619 out_truncate:
1620         if (inode->i_op && inode->i_op->truncate)
1621                 inode->i_op->truncate(inode);
1622         return 0;
1623 out_sig:
1624         send_sig(SIGXFSZ, current, 0);
1625 out_big:
1626         return -EFBIG;
1627 out_busy:
1628         return -ETXTBSY;
1629 }
1630
1631 EXPORT_SYMBOL(vmtruncate);
1632
1633 /* 
1634  * Primitive swap readahead code. We simply read an aligned block of
1635  * (1 << page_cluster) entries in the swap area. This method is chosen
1636  * because it doesn't cost us any seek time.  We also make sure to queue
1637  * the 'original' request together with the readahead ones...  
1638  *
1639  * This has been extended to use the NUMA policies from the mm triggering
1640  * the readahead.
1641  *
1642  * Caller must hold down_read on the vma->vm_mm if vma is not NULL.
1643  */
1644 void swapin_readahead(swp_entry_t entry, unsigned long addr,struct vm_area_struct *vma)
1645 {
1646 #ifdef CONFIG_NUMA
1647         struct vm_area_struct *next_vma = vma ? vma->vm_next : NULL;
1648 #endif
1649         int i, num;
1650         struct page *new_page;
1651         unsigned long offset;
1652
1653         /*
1654          * Get the number of handles we should do readahead io to.
1655          */
1656         num = valid_swaphandles(entry, &offset);
1657         for (i = 0; i < num; offset++, i++) {
1658                 /* Ok, do the async read-ahead now */
1659                 new_page = read_swap_cache_async(swp_entry(swp_type(entry),
1660                                                            offset), vma, addr);
1661                 if (!new_page)
1662                         break;
1663                 page_cache_release(new_page);
1664 #ifdef CONFIG_NUMA
1665                 /*
1666                  * Find the next applicable VMA for the NUMA policy.
1667                  */
1668                 addr += PAGE_SIZE;
1669                 if (addr == 0)
1670                         vma = NULL;
1671                 if (vma) {
1672                         if (addr >= vma->vm_end) {
1673                                 vma = next_vma;
1674                                 next_vma = vma ? vma->vm_next : NULL;
1675                         }
1676                         if (vma && addr < vma->vm_start)
1677                                 vma = NULL;
1678                 } else {
1679                         if (next_vma && addr >= next_vma->vm_start) {
1680                                 vma = next_vma;
1681                                 next_vma = vma->vm_next;
1682                         }
1683                 }
1684 #endif
1685         }
1686         lru_add_drain();        /* Push any new pages onto the LRU now */
1687 }
1688
1689 /*
1690  * We hold the mm semaphore and the page_table_lock on entry and
1691  * should release the pagetable lock on exit..
1692  */
1693 static int do_swap_page(struct mm_struct * mm,
1694         struct vm_area_struct * vma, unsigned long address,
1695         pte_t *page_table, pmd_t *pmd, pte_t orig_pte, int write_access)
1696 {
1697         struct page *page;
1698         swp_entry_t entry = pte_to_swp_entry(orig_pte);
1699         pte_t pte;
1700         int ret = VM_FAULT_MINOR;
1701
1702         pte_unmap(page_table);
1703         spin_unlock(&mm->page_table_lock);
1704         page = lookup_swap_cache(entry);
1705         if (!page) {
1706                 swapin_readahead(entry, address, vma);
1707                 page = read_swap_cache_async(entry, vma, address);
1708                 if (!page) {
1709                         /*
1710                          * Back out if somebody else faulted in this pte while
1711                          * we released the page table lock.
1712                          */
1713                         spin_lock(&mm->page_table_lock);
1714                         page_table = pte_offset_map(pmd, address);
1715                         if (likely(pte_same(*page_table, orig_pte)))
1716                                 ret = VM_FAULT_OOM;
1717                         else
1718                                 ret = VM_FAULT_MINOR;
1719                         pte_unmap(page_table);
1720                         spin_unlock(&mm->page_table_lock);
1721                         goto out;
1722                 }
1723
1724                 /* Had to read the page from swap area: Major fault */
1725                 ret = VM_FAULT_MAJOR;
1726                 inc_page_state(pgmajfault);
1727                 grab_swap_token();
1728         }
1729
1730         if (!vx_rsspages_avail(mm, 1)) {
1731                 ret = VM_FAULT_OOM;
1732                 goto out;
1733         }
1734         mark_page_accessed(page);
1735         lock_page(page);
1736
1737         /*
1738          * Back out if somebody else faulted in this pte while we
1739          * released the page table lock.
1740          */
1741         spin_lock(&mm->page_table_lock);
1742         page_table = pte_offset_map(pmd, address);
1743         if (unlikely(!pte_same(*page_table, orig_pte))) {
1744                 pte_unmap(page_table);
1745                 spin_unlock(&mm->page_table_lock);
1746                 unlock_page(page);
1747                 page_cache_release(page);
1748                 ret = VM_FAULT_MINOR;
1749                 goto out;
1750         }
1751
1752         /* The page isn't present yet, go ahead with the fault. */
1753                 
1754         swap_free(entry);
1755         if (vm_swap_full())
1756                 remove_exclusive_swap_page(page);
1757
1758         // mm->rss++;
1759         vx_rsspages_inc(mm);
1760         acct_update_integrals();
1761         update_mem_hiwater();
1762
1763         pte = mk_pte(page, vma->vm_page_prot);
1764         if (write_access && can_share_swap_page(page)) {
1765                 pte = maybe_mkwrite(pte_mkdirty(pte), vma);
1766                 write_access = 0;
1767         }
1768         unlock_page(page);
1769
1770         flush_icache_page(vma, page);
1771         set_pte(page_table, pte);
1772         page_add_anon_rmap(page, vma, address);
1773
1774         if (write_access) {
1775                 if (do_wp_page(mm, vma, address,
1776                                 page_table, pmd, pte) == VM_FAULT_OOM)
1777                         ret = VM_FAULT_OOM;
1778                 goto out;
1779         }
1780
1781         /* No need to invalidate - it was non-present before */
1782         update_mmu_cache(vma, address, pte);
1783         pte_unmap(page_table);
1784         spin_unlock(&mm->page_table_lock);
1785 out:
1786         return ret;
1787 }
1788
1789 /*
1790  * We are called with the MM semaphore and page_table_lock
1791  * spinlock held to protect against concurrent faults in
1792  * multithreaded programs. 
1793  */
1794 static int
1795 do_anonymous_page(struct mm_struct *mm, struct vm_area_struct *vma,
1796                 pte_t *page_table, pmd_t *pmd, int write_access,
1797                 unsigned long addr)
1798 {
1799         pte_t entry;
1800         struct page * page = ZERO_PAGE(addr);
1801
1802         /* Read-only mapping of ZERO_PAGE. */
1803         entry = pte_wrprotect(mk_pte(ZERO_PAGE(addr), vma->vm_page_prot));
1804
1805         /* ..except if it's a write access */
1806         if (write_access) {
1807                 /* Allocate our own private page. */
1808                 pte_unmap(page_table);
1809                 spin_unlock(&mm->page_table_lock);
1810
1811                 if (!vx_rsspages_avail(mm, 1))
1812                         goto no_mem;
1813                 if (unlikely(anon_vma_prepare(vma)))
1814                         goto no_mem;
1815                 page = alloc_zeroed_user_highpage(vma, addr);
1816                 if (!page)
1817                         goto no_mem;
1818
1819                 spin_lock(&mm->page_table_lock);
1820                 page_table = pte_offset_map(pmd, addr);
1821
1822                 if (!pte_none(*page_table)) {
1823                         pte_unmap(page_table);
1824                         page_cache_release(page);
1825                         spin_unlock(&mm->page_table_lock);
1826                         goto out;
1827                 }
1828                 // mm->rss++;
1829                 vx_rsspages_inc(mm);
1830                 acct_update_integrals();
1831                 update_mem_hiwater();
1832                 entry = maybe_mkwrite(pte_mkdirty(mk_pte(page,
1833                                                          vma->vm_page_prot)),
1834                                       vma);
1835                 lru_cache_add_active(page);
1836                 SetPageReferenced(page);
1837                 page_add_anon_rmap(page, vma, addr);
1838         }
1839
1840         set_pte(page_table, entry);
1841         pte_unmap(page_table);
1842
1843         /* No need to invalidate - it was non-present before */
1844         update_mmu_cache(vma, addr, entry);
1845         spin_unlock(&mm->page_table_lock);
1846 out:
1847         return VM_FAULT_MINOR;
1848 no_mem:
1849         return VM_FAULT_OOM;
1850 }
1851
1852 /*
1853  * do_no_page() tries to create a new page mapping. It aggressively
1854  * tries to share with existing pages, but makes a separate copy if
1855  * the "write_access" parameter is true in order to avoid the next
1856  * page fault.
1857  *
1858  * As this is called only for pages that do not currently exist, we
1859  * do not need to flush old virtual caches or the TLB.
1860  *
1861  * This is called with the MM semaphore held and the page table
1862  * spinlock held. Exit with the spinlock released.
1863  */
1864 static int
1865 do_no_page(struct mm_struct *mm, struct vm_area_struct *vma,
1866         unsigned long address, int write_access, pte_t *page_table, pmd_t *pmd)
1867 {
1868         struct page * new_page;
1869         struct address_space *mapping = NULL;
1870         pte_t entry;
1871         unsigned int sequence = 0;
1872         int ret = VM_FAULT_MINOR;
1873         int anon = 0;
1874
1875         if (!vma->vm_ops || !vma->vm_ops->nopage)
1876                 return do_anonymous_page(mm, vma, page_table,
1877                                         pmd, write_access, address);
1878         pte_unmap(page_table);
1879         spin_unlock(&mm->page_table_lock);
1880
1881         if (vma->vm_file) {
1882                 mapping = vma->vm_file->f_mapping;
1883                 sequence = mapping->truncate_count;
1884                 smp_rmb(); /* serializes i_size against truncate_count */
1885         }
1886 retry:
1887         cond_resched();
1888         /* FIXME: is that check useful here? */
1889         if (!vx_rsspages_avail(mm, 1))
1890                 return VM_FAULT_OOM;
1891         new_page = vma->vm_ops->nopage(vma, address & PAGE_MASK, &ret);
1892         /*
1893          * No smp_rmb is needed here as long as there's a full
1894          * spin_lock/unlock sequence inside the ->nopage callback
1895          * (for the pagecache lookup) that acts as an implicit
1896          * smp_mb() and prevents the i_size read to happen
1897          * after the next truncate_count read.
1898          */
1899
1900         /* no page was available -- either SIGBUS or OOM */
1901         if (new_page == NOPAGE_SIGBUS)
1902                 return VM_FAULT_SIGBUS;
1903         if (new_page == NOPAGE_OOM)
1904                 return VM_FAULT_OOM;
1905
1906         /*
1907          * Should we do an early C-O-W break?
1908          */
1909         if (write_access && !(vma->vm_flags & VM_SHARED)) {
1910                 struct page *page;
1911
1912                 if (unlikely(anon_vma_prepare(vma)))
1913                         goto oom;
1914                 page = alloc_page_vma(GFP_HIGHUSER, vma, address);
1915                 if (!page)
1916                         goto oom;
1917                 copy_user_highpage(page, new_page, address);
1918                 page_cache_release(new_page);
1919                 new_page = page;
1920                 anon = 1;
1921         }
1922
1923         spin_lock(&mm->page_table_lock);
1924         /*
1925          * For a file-backed vma, someone could have truncated or otherwise
1926          * invalidated this page.  If unmap_mapping_range got called,
1927          * retry getting the page.
1928          */
1929         if (mapping && unlikely(sequence != mapping->truncate_count)) {
1930                 sequence = mapping->truncate_count;
1931                 spin_unlock(&mm->page_table_lock);
1932                 page_cache_release(new_page);
1933                 goto retry;
1934         }
1935         page_table = pte_offset_map(pmd, address);
1936
1937         /*
1938          * This silly early PAGE_DIRTY setting removes a race
1939          * due to the bad i386 page protection. But it's valid
1940          * for other architectures too.
1941          *
1942          * Note that if write_access is true, we either now have
1943          * an exclusive copy of the page, or this is a shared mapping,
1944          * so we can make it writable and dirty to avoid having to
1945          * handle that later.
1946          */
1947         /* Only go through if we didn't race with anybody else... */
1948         if (pte_none(*page_table)) {
1949                 if (!PageReserved(new_page))
1950                         // ++mm->rss;
1951                         vx_rsspages_inc(mm);
1952                 acct_update_integrals();
1953                 update_mem_hiwater();
1954
1955                 flush_icache_page(vma, new_page);
1956                 entry = mk_pte(new_page, vma->vm_page_prot);
1957                 if (write_access)
1958                         entry = maybe_mkwrite(pte_mkdirty(entry), vma);
1959                 set_pte(page_table, entry);
1960                 if (anon) {
1961                         lru_cache_add_active(new_page);
1962                         page_add_anon_rmap(new_page, vma, address);
1963                 } else
1964                         page_add_file_rmap(new_page);
1965                 pte_unmap(page_table);
1966         } else {
1967                 /* One of our sibling threads was faster, back out. */
1968                 pte_unmap(page_table);
1969                 page_cache_release(new_page);
1970                 spin_unlock(&mm->page_table_lock);
1971                 goto out;
1972         }
1973
1974         /* no need to invalidate: a not-present page shouldn't be cached */
1975         update_mmu_cache(vma, address, entry);
1976         spin_unlock(&mm->page_table_lock);
1977 out:
1978         return ret;
1979 oom:
1980         page_cache_release(new_page);
1981         ret = VM_FAULT_OOM;
1982         goto out;
1983 }
1984
1985 /*
1986  * Fault of a previously existing named mapping. Repopulate the pte
1987  * from the encoded file_pte if possible. This enables swappable
1988  * nonlinear vmas.
1989  */
1990 static int do_file_page(struct mm_struct * mm, struct vm_area_struct * vma,
1991         unsigned long address, int write_access, pte_t *pte, pmd_t *pmd)
1992 {
1993         unsigned long pgoff;
1994         int err;
1995
1996         BUG_ON(!vma->vm_ops || !vma->vm_ops->nopage);
1997         /*
1998          * Fall back to the linear mapping if the fs does not support
1999          * ->populate:
2000          */
2001         if (!vma->vm_ops || !vma->vm_ops->populate || 
2002                         (write_access && !(vma->vm_flags & VM_SHARED))) {
2003                 pte_clear(pte);
2004                 return do_no_page(mm, vma, address, write_access, pte, pmd);
2005         }
2006
2007         pgoff = pte_to_pgoff(*pte);
2008
2009         pte_unmap(pte);
2010         spin_unlock(&mm->page_table_lock);
2011
2012         err = vma->vm_ops->populate(vma, address & PAGE_MASK, PAGE_SIZE, vma->vm_page_prot, pgoff, 0);
2013         if (err == -ENOMEM)
2014                 return VM_FAULT_OOM;
2015         if (err)
2016                 return VM_FAULT_SIGBUS;
2017         return VM_FAULT_MAJOR;
2018 }
2019
2020 /*
2021  * These routines also need to handle stuff like marking pages dirty
2022  * and/or accessed for architectures that don't do it in hardware (most
2023  * RISC architectures).  The early dirtying is also good on the i386.
2024  *
2025  * There is also a hook called "update_mmu_cache()" that architectures
2026  * with external mmu caches can use to update those (ie the Sparc or
2027  * PowerPC hashed page tables that act as extended TLBs).
2028  *
2029  * Note the "page_table_lock". It is to protect against kswapd removing
2030  * pages from under us. Note that kswapd only ever _removes_ pages, never
2031  * adds them. As such, once we have noticed that the page is not present,
2032  * we can drop the lock early.
2033  *
2034  * The adding of pages is protected by the MM semaphore (which we hold),
2035  * so we don't need to worry about a page being suddenly been added into
2036  * our VM.
2037  *
2038  * We enter with the pagetable spinlock held, we are supposed to
2039  * release it when done.
2040  */
2041 static inline int handle_pte_fault(struct mm_struct *mm,
2042         struct vm_area_struct * vma, unsigned long address,
2043         int write_access, pte_t *pte, pmd_t *pmd)
2044 {
2045         pte_t entry;
2046
2047         entry = *pte;
2048         if (!pte_present(entry)) {
2049                 /*
2050                  * If it truly wasn't present, we know that kswapd
2051                  * and the PTE updates will not touch it later. So
2052                  * drop the lock.
2053                  */
2054                 if (pte_none(entry))
2055                         return do_no_page(mm, vma, address, write_access, pte, pmd);
2056                 if (pte_file(entry))
2057                         return do_file_page(mm, vma, address, write_access, pte, pmd);
2058                 return do_swap_page(mm, vma, address, pte, pmd, entry, write_access);
2059         }
2060
2061         if (write_access) {
2062                 if (!pte_write(entry))
2063                         return do_wp_page(mm, vma, address, pte, pmd, entry);
2064
2065                 entry = pte_mkdirty(entry);
2066         }
2067         entry = pte_mkyoung(entry);
2068         ptep_set_access_flags(vma, address, pte, entry, write_access);
2069         update_mmu_cache(vma, address, entry);
2070         pte_unmap(pte);
2071         spin_unlock(&mm->page_table_lock);
2072         return VM_FAULT_MINOR;
2073 }
2074
2075 /*
2076  * By the time we get here, we already hold the mm semaphore
2077  */
2078 int handle_mm_fault(struct mm_struct *mm, struct vm_area_struct * vma,
2079                 unsigned long address, int write_access)
2080 {
2081         pgd_t *pgd;
2082         pud_t *pud;
2083         pmd_t *pmd;
2084         pte_t *pte;
2085
2086         __set_current_state(TASK_RUNNING);
2087
2088         inc_page_state(pgfault);
2089
2090         if (is_vm_hugetlb_page(vma))
2091                 return VM_FAULT_SIGBUS; /* mapping truncation does this. */
2092
2093         /*
2094          * We need the page table lock to synchronize with kswapd
2095          * and the SMP-safe atomic PTE updates.
2096          */
2097         pgd = pgd_offset(mm, address);
2098         spin_lock(&mm->page_table_lock);
2099
2100         pud = pud_alloc(mm, pgd, address);
2101         if (!pud)
2102                 goto oom;
2103
2104         pmd = pmd_alloc(mm, pud, address);
2105         if (!pmd)
2106                 goto oom;
2107
2108         pte = pte_alloc_map(mm, pmd, address);
2109         if (!pte)
2110                 goto oom;
2111         
2112         return handle_pte_fault(mm, vma, address, write_access, pte, pmd);
2113
2114  oom:
2115         spin_unlock(&mm->page_table_lock);
2116         return VM_FAULT_OOM;
2117 }
2118
2119 #ifndef __ARCH_HAS_4LEVEL_HACK
2120 /*
2121  * Allocate page upper directory.
2122  *
2123  * We've already handled the fast-path in-line, and we own the
2124  * page table lock.
2125  *
2126  * On a two-level or three-level page table, this ends up actually being
2127  * entirely optimized away.
2128  */
2129 pud_t fastcall *__pud_alloc(struct mm_struct *mm, pgd_t *pgd, unsigned long address)
2130 {
2131         pud_t *new;
2132
2133         spin_unlock(&mm->page_table_lock);
2134         new = pud_alloc_one(mm, address);
2135         spin_lock(&mm->page_table_lock);
2136         if (!new)
2137                 return NULL;
2138
2139         /*
2140          * Because we dropped the lock, we should re-check the
2141          * entry, as somebody else could have populated it..
2142          */
2143         if (pgd_present(*pgd)) {
2144                 pud_free(new);
2145                 goto out;
2146         }
2147         pgd_populate(mm, pgd, new);
2148  out:
2149         return pud_offset(pgd, address);
2150 }
2151
2152 /*
2153  * Allocate page middle directory.
2154  *
2155  * We've already handled the fast-path in-line, and we own the
2156  * page table lock.
2157  *
2158  * On a two-level page table, this ends up actually being entirely
2159  * optimized away.
2160  */
2161 pmd_t fastcall *__pmd_alloc(struct mm_struct *mm, pud_t *pud, unsigned long address)
2162 {
2163         pmd_t *new;
2164
2165         spin_unlock(&mm->page_table_lock);
2166         new = pmd_alloc_one(mm, address);
2167         spin_lock(&mm->page_table_lock);
2168         if (!new)
2169                 return NULL;
2170
2171         /*
2172          * Because we dropped the lock, we should re-check the
2173          * entry, as somebody else could have populated it..
2174          */
2175         if (pud_present(*pud)) {
2176                 pmd_free(new);
2177                 goto out;
2178         }
2179         pud_populate(mm, pud, new);
2180  out:
2181         return pmd_offset(pud, address);
2182 }
2183 #else
2184 pmd_t fastcall *__pmd_alloc(struct mm_struct *mm, pud_t *pud, unsigned long address)
2185 {
2186         pmd_t *new;
2187
2188         spin_unlock(&mm->page_table_lock);
2189         new = pmd_alloc_one(mm, address);
2190         spin_lock(&mm->page_table_lock);
2191         if (!new)
2192                 return NULL;
2193
2194         /*
2195          * Because we dropped the lock, we should re-check the
2196          * entry, as somebody else could have populated it..
2197          */
2198         if (pgd_present(*pud)) {
2199                 pmd_free(new);
2200                 goto out;
2201         }
2202         pgd_populate(mm, pud, new);
2203 out:
2204         return pmd_offset(pud, address);
2205 }
2206 #endif
2207
2208 int make_pages_present(unsigned long addr, unsigned long end)
2209 {
2210         int ret, len, write;
2211         struct vm_area_struct * vma;
2212
2213         vma = find_vma(current->mm, addr);
2214         if (!vma)
2215                 return -1;
2216         write = (vma->vm_flags & VM_WRITE) != 0;
2217         if (addr >= end)
2218                 BUG();
2219         if (end > vma->vm_end)
2220                 BUG();
2221         len = (end+PAGE_SIZE-1)/PAGE_SIZE-addr/PAGE_SIZE;
2222         ret = get_user_pages(current, current->mm, addr,
2223                         len, write, 0, NULL, NULL);
2224         if (ret < 0)
2225                 return ret;
2226         return ret == len ? 0 : -1;
2227 }
2228
2229 /* 
2230  * Map a vmalloc()-space virtual address to the physical page.
2231  */
2232 struct page * vmalloc_to_page(void * vmalloc_addr)
2233 {
2234         unsigned long addr = (unsigned long) vmalloc_addr;
2235         struct page *page = NULL;
2236         pgd_t *pgd = pgd_offset_k(addr);
2237         pud_t *pud;
2238         pmd_t *pmd;
2239         pte_t *ptep, pte;
2240   
2241         if (!pgd_none(*pgd)) {
2242                 pud = pud_offset(pgd, addr);
2243                 if (!pud_none(*pud)) {
2244                         pmd = pmd_offset(pud, addr);
2245                         if (!pmd_none(*pmd)) {
2246                                 ptep = pte_offset_map(pmd, addr);
2247                                 pte = *ptep;
2248                                 if (pte_present(pte))
2249                                         page = pte_page(pte);
2250                                 pte_unmap(ptep);
2251                         }
2252                 }
2253         }
2254         return page;
2255 }
2256
2257 EXPORT_SYMBOL(vmalloc_to_page);
2258
2259 /*
2260  * Map a vmalloc()-space virtual address to the physical page frame number.
2261  */
2262 unsigned long vmalloc_to_pfn(void * vmalloc_addr)
2263 {
2264         return page_to_pfn(vmalloc_to_page(vmalloc_addr));
2265 }
2266
2267 EXPORT_SYMBOL(vmalloc_to_pfn);
2268
2269 /*
2270  * update_mem_hiwater
2271  *      - update per process rss and vm high water data
2272  */
2273 void update_mem_hiwater(void)
2274 {
2275         struct task_struct *tsk = current;
2276
2277         if (tsk->mm) {
2278                 if (tsk->mm->hiwater_rss < tsk->mm->rss)
2279                         tsk->mm->hiwater_rss = tsk->mm->rss;
2280                 if (tsk->mm->hiwater_vm < tsk->mm->total_vm)
2281                         tsk->mm->hiwater_vm = tsk->mm->total_vm;
2282         }
2283 }
2284
2285 #if !defined(__HAVE_ARCH_GATE_AREA)
2286
2287 #if defined(AT_SYSINFO_EHDR)
2288 struct vm_area_struct gate_vma;
2289
2290 static int __init gate_vma_init(void)
2291 {
2292         gate_vma.vm_mm = NULL;
2293         gate_vma.vm_start = FIXADDR_USER_START;
2294         gate_vma.vm_end = FIXADDR_USER_END;
2295         gate_vma.vm_page_prot = PAGE_READONLY;
2296         gate_vma.vm_flags = 0;
2297         return 0;
2298 }
2299 __initcall(gate_vma_init);
2300 #endif
2301
2302 struct vm_area_struct *get_gate_vma(struct task_struct *tsk)
2303 {
2304 #ifdef AT_SYSINFO_EHDR
2305         return &gate_vma;
2306 #else
2307         return NULL;
2308 #endif
2309 }
2310
2311 int in_gate_area_no_task(unsigned long addr)
2312 {
2313 #ifdef AT_SYSINFO_EHDR
2314         if ((addr >= FIXADDR_USER_START) && (addr < FIXADDR_USER_END))
2315                 return 1;
2316 #endif
2317         return 0;
2318 }
2319
2320 #endif  /* __HAVE_ARCH_GATE_AREA */