backported vs2.1.x fix to irq handling, which caused incorrect scheduler behavior
[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/module.h>
50 #include <linux/delayacct.h>
51 #include <linux/init.h>
52 #include <linux/writeback.h>
53 #include <linux/vs_base.h>
54 #include <linux/vs_memory.h>
55
56 #include <asm/pgalloc.h>
57 #include <asm/uaccess.h>
58 #include <asm/tlb.h>
59 #include <asm/tlbflush.h>
60 #include <asm/pgtable.h>
61
62 #include <linux/swapops.h>
63 #include <linux/elf.h>
64
65 #ifndef CONFIG_NEED_MULTIPLE_NODES
66 /* use the per-pgdat data instead for discontigmem - mbligh */
67 unsigned long max_mapnr;
68 struct page *mem_map;
69
70 EXPORT_SYMBOL(max_mapnr);
71 EXPORT_SYMBOL(mem_map);
72 #endif
73
74 unsigned long num_physpages;
75 /*
76  * A number of key systems in x86 including ioremap() rely on the assumption
77  * that high_memory defines the upper bound on direct map memory, then end
78  * of ZONE_NORMAL.  Under CONFIG_DISCONTIG this means that max_low_pfn and
79  * highstart_pfn must be the same; there must be no gap between ZONE_NORMAL
80  * and ZONE_HIGHMEM.
81  */
82 void * high_memory;
83 unsigned long vmalloc_earlyreserve;
84
85 EXPORT_SYMBOL(num_physpages);
86 EXPORT_SYMBOL(high_memory);
87 EXPORT_SYMBOL(vmalloc_earlyreserve);
88
89 int randomize_va_space __read_mostly = 1;
90
91 static int __init disable_randmaps(char *s)
92 {
93         randomize_va_space = 0;
94         return 1;
95 }
96 __setup("norandmaps", disable_randmaps);
97
98
99 /*
100  * If a p?d_bad entry is found while walking page tables, report
101  * the error, before resetting entry to p?d_none.  Usually (but
102  * very seldom) called out from the p?d_none_or_clear_bad macros.
103  */
104
105 void pgd_clear_bad(pgd_t *pgd)
106 {
107         pgd_ERROR(*pgd);
108         pgd_clear(pgd);
109 }
110
111 void pud_clear_bad(pud_t *pud)
112 {
113         pud_ERROR(*pud);
114         pud_clear(pud);
115 }
116
117 void pmd_clear_bad(pmd_t *pmd)
118 {
119         pmd_ERROR(*pmd);
120         pmd_clear(pmd);
121 }
122
123 /*
124  * Note: this doesn't free the actual pages themselves. That
125  * has been handled earlier when unmapping all the memory regions.
126  */
127 static void free_pte_range(struct mmu_gather *tlb, pmd_t *pmd)
128 {
129         struct page *page = pmd_page(*pmd);
130         pmd_clear(pmd);
131         pte_lock_deinit(page);
132         pte_free_tlb(tlb, page);
133         dec_zone_page_state(page, NR_PAGETABLE);
134         tlb->mm->nr_ptes--;
135 }
136
137 static inline void free_pmd_range(struct mmu_gather *tlb, pud_t *pud,
138                                 unsigned long addr, unsigned long end,
139                                 unsigned long floor, unsigned long ceiling)
140 {
141         pmd_t *pmd;
142         unsigned long next;
143         unsigned long start;
144
145         start = addr;
146         pmd = pmd_offset(pud, addr);
147         do {
148                 next = pmd_addr_end(addr, end);
149                 if (pmd_none_or_clear_bad(pmd))
150                         continue;
151                 free_pte_range(tlb, pmd);
152         } while (pmd++, addr = next, addr != end);
153
154         start &= PUD_MASK;
155         if (start < floor)
156                 return;
157         if (ceiling) {
158                 ceiling &= PUD_MASK;
159                 if (!ceiling)
160                         return;
161         }
162         if (end - 1 > ceiling - 1)
163                 return;
164
165         pmd = pmd_offset(pud, start);
166         pud_clear(pud);
167         pmd_free_tlb(tlb, pmd);
168 }
169
170 static inline void free_pud_range(struct mmu_gather *tlb, pgd_t *pgd,
171                                 unsigned long addr, unsigned long end,
172                                 unsigned long floor, unsigned long ceiling)
173 {
174         pud_t *pud;
175         unsigned long next;
176         unsigned long start;
177
178         start = addr;
179         pud = pud_offset(pgd, addr);
180         do {
181                 next = pud_addr_end(addr, end);
182                 if (pud_none_or_clear_bad(pud))
183                         continue;
184                 free_pmd_range(tlb, pud, addr, next, floor, ceiling);
185         } while (pud++, addr = next, addr != end);
186
187         start &= PGDIR_MASK;
188         if (start < floor)
189                 return;
190         if (ceiling) {
191                 ceiling &= PGDIR_MASK;
192                 if (!ceiling)
193                         return;
194         }
195         if (end - 1 > ceiling - 1)
196                 return;
197
198         pud = pud_offset(pgd, start);
199         pgd_clear(pgd);
200         pud_free_tlb(tlb, pud);
201 }
202
203 /*
204  * This function frees user-level page tables of a process.
205  *
206  * Must be called with pagetable lock held.
207  */
208 void free_pgd_range(struct mmu_gather **tlb,
209                         unsigned long addr, unsigned long end,
210                         unsigned long floor, unsigned long ceiling)
211 {
212         pgd_t *pgd;
213         unsigned long next;
214         unsigned long start;
215
216         /*
217          * The next few lines have given us lots of grief...
218          *
219          * Why are we testing PMD* at this top level?  Because often
220          * there will be no work to do at all, and we'd prefer not to
221          * go all the way down to the bottom just to discover that.
222          *
223          * Why all these "- 1"s?  Because 0 represents both the bottom
224          * of the address space and the top of it (using -1 for the
225          * top wouldn't help much: the masks would do the wrong thing).
226          * The rule is that addr 0 and floor 0 refer to the bottom of
227          * the address space, but end 0 and ceiling 0 refer to the top
228          * Comparisons need to use "end - 1" and "ceiling - 1" (though
229          * that end 0 case should be mythical).
230          *
231          * Wherever addr is brought up or ceiling brought down, we must
232          * be careful to reject "the opposite 0" before it confuses the
233          * subsequent tests.  But what about where end is brought down
234          * by PMD_SIZE below? no, end can't go down to 0 there.
235          *
236          * Whereas we round start (addr) and ceiling down, by different
237          * masks at different levels, in order to test whether a table
238          * now has no other vmas using it, so can be freed, we don't
239          * bother to round floor or end up - the tests don't need that.
240          */
241
242         addr &= PMD_MASK;
243         if (addr < floor) {
244                 addr += PMD_SIZE;
245                 if (!addr)
246                         return;
247         }
248         if (ceiling) {
249                 ceiling &= PMD_MASK;
250                 if (!ceiling)
251                         return;
252         }
253         if (end - 1 > ceiling - 1)
254                 end -= PMD_SIZE;
255         if (addr > end - 1)
256                 return;
257
258         start = addr;
259         pgd = pgd_offset((*tlb)->mm, addr);
260         do {
261                 next = pgd_addr_end(addr, end);
262                 if (pgd_none_or_clear_bad(pgd))
263                         continue;
264                 free_pud_range(*tlb, pgd, addr, next, floor, ceiling);
265         } while (pgd++, addr = next, addr != end);
266
267         if (!(*tlb)->fullmm)
268                 flush_tlb_pgtables((*tlb)->mm, start, end);
269 }
270
271 void free_pgtables(struct mmu_gather **tlb, struct vm_area_struct *vma,
272                 unsigned long floor, unsigned long ceiling)
273 {
274         while (vma) {
275                 struct vm_area_struct *next = vma->vm_next;
276                 unsigned long addr = vma->vm_start;
277
278                 /*
279                  * Hide vma from rmap and vmtruncate before freeing pgtables
280                  */
281                 anon_vma_unlink(vma);
282                 unlink_file_vma(vma);
283
284                 if (is_vm_hugetlb_page(vma)) {
285                         hugetlb_free_pgd_range(tlb, addr, vma->vm_end,
286                                 floor, next? next->vm_start: ceiling);
287                 } else {
288                         /*
289                          * Optimization: gather nearby vmas into one call down
290                          */
291                         while (next && next->vm_start <= vma->vm_end + PMD_SIZE
292                                && !is_vm_hugetlb_page(next)) {
293                                 vma = next;
294                                 next = vma->vm_next;
295                                 anon_vma_unlink(vma);
296                                 unlink_file_vma(vma);
297                         }
298                         free_pgd_range(tlb, addr, vma->vm_end,
299                                 floor, next? next->vm_start: ceiling);
300                 }
301                 vma = next;
302         }
303 }
304
305 int __pte_alloc(struct mm_struct *mm, pmd_t *pmd, unsigned long address)
306 {
307         struct page *new = pte_alloc_one(mm, address);
308         if (!new)
309                 return -ENOMEM;
310
311         pte_lock_init(new);
312         spin_lock(&mm->page_table_lock);
313         if (pmd_present(*pmd)) {        /* Another has populated it */
314                 pte_lock_deinit(new);
315                 pte_free(new);
316         } else {
317                 mm->nr_ptes++;
318                 inc_zone_page_state(new, NR_PAGETABLE);
319                 pmd_populate(mm, pmd, new);
320         }
321         spin_unlock(&mm->page_table_lock);
322         return 0;
323 }
324
325 int __pte_alloc_kernel(pmd_t *pmd, unsigned long address)
326 {
327         pte_t *new = pte_alloc_one_kernel(&init_mm, address);
328         if (!new)
329                 return -ENOMEM;
330
331         spin_lock(&init_mm.page_table_lock);
332         if (pmd_present(*pmd))          /* Another has populated it */
333                 pte_free_kernel(new);
334         else
335                 pmd_populate_kernel(&init_mm, pmd, new);
336         spin_unlock(&init_mm.page_table_lock);
337         return 0;
338 }
339
340 static inline void add_mm_rss(struct mm_struct *mm, int file_rss, int anon_rss)
341 {
342         if (file_rss)
343                 add_mm_counter(mm, file_rss, file_rss);
344         if (anon_rss)
345                 add_mm_counter(mm, anon_rss, anon_rss);
346 }
347
348 /*
349  * This function is called to print an error when a bad pte
350  * is found. For example, we might have a PFN-mapped pte in
351  * a region that doesn't allow it.
352  *
353  * The calling function must still handle the error.
354  */
355 void print_bad_pte(struct vm_area_struct *vma, pte_t pte, unsigned long vaddr)
356 {
357         printk(KERN_ERR "Bad pte = %08llx, process = %s, "
358                         "vm_flags = %lx, vaddr = %lx\n",
359                 (long long)pte_val(pte),
360                 (vma->vm_mm == current->mm ? current->comm : "???"),
361                 vma->vm_flags, vaddr);
362         dump_stack();
363 }
364
365 static inline int is_cow_mapping(unsigned int flags)
366 {
367         return (flags & (VM_SHARED | VM_MAYWRITE)) == VM_MAYWRITE;
368 }
369
370 /*
371  * This function gets the "struct page" associated with a pte.
372  *
373  * NOTE! Some mappings do not have "struct pages". A raw PFN mapping
374  * will have each page table entry just pointing to a raw page frame
375  * number, and as far as the VM layer is concerned, those do not have
376  * pages associated with them - even if the PFN might point to memory
377  * that otherwise is perfectly fine and has a "struct page".
378  *
379  * The way we recognize those mappings is through the rules set up
380  * by "remap_pfn_range()": the vma will have the VM_PFNMAP bit set,
381  * and the vm_pgoff will point to the first PFN mapped: thus every
382  * page that is a raw mapping will always honor the rule
383  *
384  *      pfn_of_page == vma->vm_pgoff + ((addr - vma->vm_start) >> PAGE_SHIFT)
385  *
386  * and if that isn't true, the page has been COW'ed (in which case it
387  * _does_ have a "struct page" associated with it even if it is in a
388  * VM_PFNMAP range).
389  */
390 struct page *vm_normal_page(struct vm_area_struct *vma, unsigned long addr, pte_t pte)
391 {
392         unsigned long pfn = pte_pfn(pte);
393
394         if (unlikely(vma->vm_flags & VM_PFNMAP)) {
395                 unsigned long off = (addr - vma->vm_start) >> PAGE_SHIFT;
396                 if (pfn == vma->vm_pgoff + off)
397                         return NULL;
398                 if (!is_cow_mapping(vma->vm_flags))
399                         return NULL;
400         }
401
402         /*
403          * Add some anal sanity checks for now. Eventually,
404          * we should just do "return pfn_to_page(pfn)", but
405          * in the meantime we check that we get a valid pfn,
406          * and that the resulting page looks ok.
407          */
408         if (unlikely(!pfn_valid(pfn))) {
409                 if (!(vma->vm_flags & VM_RESERVED))
410                         print_bad_pte(vma, pte, addr);
411                 return NULL;
412         }
413
414         /*
415          * NOTE! We still have PageReserved() pages in the page 
416          * tables. 
417          *
418          * The PAGE_ZERO() pages and various VDSO mappings can
419          * cause them to exist.
420          */
421         return pfn_to_page(pfn);
422 }
423
424 /*
425  * copy one vm_area from one task to the other. Assumes the page tables
426  * already present in the new task to be cleared in the whole range
427  * covered by this vma.
428  */
429
430 static inline void
431 copy_one_pte(struct mm_struct *dst_mm, struct mm_struct *src_mm,
432                 pte_t *dst_pte, pte_t *src_pte, struct vm_area_struct *vma,
433                 unsigned long addr, int *rss)
434 {
435         unsigned long vm_flags = vma->vm_flags;
436         pte_t pte = *src_pte;
437         struct page *page;
438
439         /* pte contains position in swap or file, so copy. */
440         if (unlikely(!pte_present(pte))) {
441                 if (!pte_file(pte)) {
442                         swp_entry_t entry = pte_to_swp_entry(pte);
443
444                         swap_duplicate(entry);
445                         /* make sure dst_mm is on swapoff's mmlist. */
446                         if (unlikely(list_empty(&dst_mm->mmlist))) {
447                                 spin_lock(&mmlist_lock);
448                                 if (list_empty(&dst_mm->mmlist))
449                                         list_add(&dst_mm->mmlist,
450                                                  &src_mm->mmlist);
451                                 spin_unlock(&mmlist_lock);
452                         }
453                         if (is_write_migration_entry(entry) &&
454                                         is_cow_mapping(vm_flags)) {
455                                 /*
456                                  * COW mappings require pages in both parent
457                                  * and child to be set to read.
458                                  */
459                                 make_migration_entry_read(&entry);
460                                 pte = swp_entry_to_pte(entry);
461                                 set_pte_at(src_mm, addr, src_pte, pte);
462                         }
463                 }
464                 goto out_set_pte;
465         }
466
467         /*
468          * If it's a COW mapping, write protect it both
469          * in the parent and the child
470          */
471         if (is_cow_mapping(vm_flags)) {
472                 ptep_set_wrprotect(src_mm, addr, src_pte);
473                 pte = *src_pte;
474         }
475
476         /*
477          * If it's a shared mapping, mark it clean in
478          * the child
479          */
480         if (vm_flags & VM_SHARED)
481                 pte = pte_mkclean(pte);
482         pte = pte_mkold(pte);
483
484         page = vm_normal_page(vma, addr, pte);
485         if (page) {
486                 get_page(page);
487                 page_dup_rmap(page);
488                 rss[!!PageAnon(page)]++;
489         }
490
491 out_set_pte:
492         set_pte_at(dst_mm, addr, dst_pte, pte);
493 }
494
495 static int copy_pte_range(struct mm_struct *dst_mm, struct mm_struct *src_mm,
496                 pmd_t *dst_pmd, pmd_t *src_pmd, struct vm_area_struct *vma,
497                 unsigned long addr, unsigned long end)
498 {
499         pte_t *src_pte, *dst_pte;
500         spinlock_t *src_ptl, *dst_ptl;
501         int progress = 0;
502         int rss[2];
503
504 again:
505         rss[1] = rss[0] = 0;
506         dst_pte = pte_alloc_map_lock(dst_mm, dst_pmd, addr, &dst_ptl);
507         if (!dst_pte)
508                 return -ENOMEM;
509         src_pte = pte_offset_map_nested(src_pmd, addr);
510         src_ptl = pte_lockptr(src_mm, src_pmd);
511         spin_lock_nested(src_ptl, SINGLE_DEPTH_NESTING);
512
513         do {
514                 /*
515                  * We are holding two locks at this point - either of them
516                  * could generate latencies in another task on another CPU.
517                  */
518                 if (progress >= 32) {
519                         progress = 0;
520                         if (need_resched() ||
521                             need_lockbreak(src_ptl) ||
522                             need_lockbreak(dst_ptl))
523                                 break;
524                 }
525                 if (pte_none(*src_pte)) {
526                         progress++;
527                         continue;
528                 }
529                 copy_one_pte(dst_mm, src_mm, dst_pte, src_pte, vma, addr, rss);
530                 progress += 8;
531         } while (dst_pte++, src_pte++, addr += PAGE_SIZE, addr != end);
532
533         spin_unlock(src_ptl);
534         pte_unmap_nested(src_pte - 1);
535         add_mm_rss(dst_mm, rss[0], rss[1]);
536         pte_unmap_unlock(dst_pte - 1, dst_ptl);
537         cond_resched();
538         if (addr != end)
539                 goto again;
540         return 0;
541 }
542
543 static inline int copy_pmd_range(struct mm_struct *dst_mm, struct mm_struct *src_mm,
544                 pud_t *dst_pud, pud_t *src_pud, struct vm_area_struct *vma,
545                 unsigned long addr, unsigned long end)
546 {
547         pmd_t *src_pmd, *dst_pmd;
548         unsigned long next;
549
550         dst_pmd = pmd_alloc(dst_mm, dst_pud, addr);
551         if (!dst_pmd)
552                 return -ENOMEM;
553         src_pmd = pmd_offset(src_pud, addr);
554         do {
555                 next = pmd_addr_end(addr, end);
556                 if (pmd_none_or_clear_bad(src_pmd))
557                         continue;
558                 if (copy_pte_range(dst_mm, src_mm, dst_pmd, src_pmd,
559                                                 vma, addr, next))
560                         return -ENOMEM;
561         } while (dst_pmd++, src_pmd++, addr = next, addr != end);
562         return 0;
563 }
564
565 static inline int copy_pud_range(struct mm_struct *dst_mm, struct mm_struct *src_mm,
566                 pgd_t *dst_pgd, pgd_t *src_pgd, struct vm_area_struct *vma,
567                 unsigned long addr, unsigned long end)
568 {
569         pud_t *src_pud, *dst_pud;
570         unsigned long next;
571
572         dst_pud = pud_alloc(dst_mm, dst_pgd, addr);
573         if (!dst_pud)
574                 return -ENOMEM;
575         src_pud = pud_offset(src_pgd, addr);
576         do {
577                 next = pud_addr_end(addr, end);
578                 if (pud_none_or_clear_bad(src_pud))
579                         continue;
580                 if (copy_pmd_range(dst_mm, src_mm, dst_pud, src_pud,
581                                                 vma, addr, next))
582                         return -ENOMEM;
583         } while (dst_pud++, src_pud++, addr = next, addr != end);
584         return 0;
585 }
586
587 int copy_page_range(struct mm_struct *dst_mm, struct mm_struct *src_mm,
588                 struct vm_area_struct *vma)
589 {
590         pgd_t *src_pgd, *dst_pgd;
591         unsigned long next;
592         unsigned long addr = vma->vm_start;
593         unsigned long end = vma->vm_end;
594
595         /*
596          * Don't copy ptes where a page fault will fill them correctly.
597          * Fork becomes much lighter when there are big shared or private
598          * readonly mappings. The tradeoff is that copy_page_range is more
599          * efficient than faulting.
600          */
601         if (!(vma->vm_flags & (VM_HUGETLB|VM_NONLINEAR|VM_PFNMAP|VM_INSERTPAGE))) {
602                 if (!vma->anon_vma)
603                         return 0;
604         }
605
606         if (is_vm_hugetlb_page(vma))
607                 return copy_hugetlb_page_range(dst_mm, src_mm, vma);
608
609         dst_pgd = pgd_offset(dst_mm, addr);
610         src_pgd = pgd_offset(src_mm, addr);
611         do {
612                 next = pgd_addr_end(addr, end);
613                 if (pgd_none_or_clear_bad(src_pgd))
614                         continue;
615                 if (copy_pud_range(dst_mm, src_mm, dst_pgd, src_pgd,
616                                                 vma, addr, next))
617                         return -ENOMEM;
618         } while (dst_pgd++, src_pgd++, addr = next, addr != end);
619         return 0;
620 }
621
622 static unsigned long zap_pte_range(struct mmu_gather *tlb,
623                                 struct vm_area_struct *vma, pmd_t *pmd,
624                                 unsigned long addr, unsigned long end,
625                                 long *zap_work, struct zap_details *details)
626 {
627         struct mm_struct *mm = tlb->mm;
628         pte_t *pte;
629         spinlock_t *ptl;
630         int file_rss = 0;
631         int anon_rss = 0;
632
633         pte = pte_offset_map_lock(mm, pmd, addr, &ptl);
634         do {
635                 pte_t ptent = *pte;
636                 if (pte_none(ptent)) {
637                         (*zap_work)--;
638                         continue;
639                 }
640
641                 (*zap_work) -= PAGE_SIZE;
642
643                 if (pte_present(ptent)) {
644                         struct page *page;
645
646                         page = vm_normal_page(vma, addr, ptent);
647                         if (unlikely(details) && page) {
648                                 /*
649                                  * unmap_shared_mapping_pages() wants to
650                                  * invalidate cache without truncating:
651                                  * unmap shared but keep private pages.
652                                  */
653                                 if (details->check_mapping &&
654                                     details->check_mapping != page->mapping)
655                                         continue;
656                                 /*
657                                  * Each page->index must be checked when
658                                  * invalidating or truncating nonlinear.
659                                  */
660                                 if (details->nonlinear_vma &&
661                                     (page->index < details->first_index ||
662                                      page->index > details->last_index))
663                                         continue;
664                         }
665                         ptent = ptep_get_and_clear_full(mm, addr, pte,
666                                                         tlb->fullmm);
667                         tlb_remove_tlb_entry(tlb, pte, addr);
668                         if (unlikely(!page))
669                                 continue;
670                         if (unlikely(details) && details->nonlinear_vma
671                             && linear_page_index(details->nonlinear_vma,
672                                                 addr) != page->index)
673                                 set_pte_at(mm, addr, pte,
674                                            pgoff_to_pte(page->index));
675                         if (PageAnon(page))
676                                 anon_rss--;
677                         else {
678                                 if (pte_dirty(ptent))
679                                         set_page_dirty(page);
680                                 if (pte_young(ptent))
681                                         mark_page_accessed(page);
682                                 file_rss--;
683                         }
684                         page_remove_rmap(page, vma);
685                         tlb_remove_page(tlb, page);
686                         continue;
687                 }
688                 /*
689                  * If details->check_mapping, we leave swap entries;
690                  * if details->nonlinear_vma, we leave file entries.
691                  */
692                 if (unlikely(details))
693                         continue;
694                 if (!pte_file(ptent))
695                         free_swap_and_cache(pte_to_swp_entry(ptent));
696                 pte_clear_full(mm, addr, pte, tlb->fullmm);
697         } while (pte++, addr += PAGE_SIZE, (addr != end && *zap_work > 0));
698
699         add_mm_rss(mm, file_rss, anon_rss);
700         pte_unmap_unlock(pte - 1, ptl);
701
702         return addr;
703 }
704
705 static inline unsigned long zap_pmd_range(struct mmu_gather *tlb,
706                                 struct vm_area_struct *vma, pud_t *pud,
707                                 unsigned long addr, unsigned long end,
708                                 long *zap_work, struct zap_details *details)
709 {
710         pmd_t *pmd;
711         unsigned long next;
712
713         pmd = pmd_offset(pud, addr);
714         do {
715                 next = pmd_addr_end(addr, end);
716                 if (pmd_none_or_clear_bad(pmd)) {
717                         (*zap_work)--;
718                         continue;
719                 }
720                 next = zap_pte_range(tlb, vma, pmd, addr, next,
721                                                 zap_work, details);
722         } while (pmd++, addr = next, (addr != end && *zap_work > 0));
723
724         return addr;
725 }
726
727 static inline unsigned long zap_pud_range(struct mmu_gather *tlb,
728                                 struct vm_area_struct *vma, pgd_t *pgd,
729                                 unsigned long addr, unsigned long end,
730                                 long *zap_work, struct zap_details *details)
731 {
732         pud_t *pud;
733         unsigned long next;
734
735         pud = pud_offset(pgd, addr);
736         do {
737                 next = pud_addr_end(addr, end);
738                 if (pud_none_or_clear_bad(pud)) {
739                         (*zap_work)--;
740                         continue;
741                 }
742                 next = zap_pmd_range(tlb, vma, pud, addr, next,
743                                                 zap_work, details);
744         } while (pud++, addr = next, (addr != end && *zap_work > 0));
745
746         return addr;
747 }
748
749 static unsigned long unmap_page_range(struct mmu_gather *tlb,
750                                 struct vm_area_struct *vma,
751                                 unsigned long addr, unsigned long end,
752                                 long *zap_work, struct zap_details *details)
753 {
754         pgd_t *pgd;
755         unsigned long next;
756
757         if (details && !details->check_mapping && !details->nonlinear_vma)
758                 details = NULL;
759
760         BUG_ON(addr >= end);
761         tlb_start_vma(tlb, vma);
762         pgd = pgd_offset(vma->vm_mm, addr);
763         do {
764                 next = pgd_addr_end(addr, end);
765                 if (pgd_none_or_clear_bad(pgd)) {
766                         (*zap_work)--;
767                         continue;
768                 }
769                 next = zap_pud_range(tlb, vma, pgd, addr, next,
770                                                 zap_work, details);
771         } while (pgd++, addr = next, (addr != end && *zap_work > 0));
772         tlb_end_vma(tlb, vma);
773
774         return addr;
775 }
776
777 #ifdef CONFIG_PREEMPT
778 # define ZAP_BLOCK_SIZE (8 * PAGE_SIZE)
779 #else
780 /* No preempt: go for improved straight-line efficiency */
781 # define ZAP_BLOCK_SIZE (1024 * PAGE_SIZE)
782 #endif
783
784 /**
785  * unmap_vmas - unmap a range of memory covered by a list of vma's
786  * @tlbp: address of the caller's struct mmu_gather
787  * @vma: the starting vma
788  * @start_addr: virtual address at which to start unmapping
789  * @end_addr: virtual address at which to end unmapping
790  * @nr_accounted: Place number of unmapped pages in vm-accountable vma's here
791  * @details: details of nonlinear truncation or shared cache invalidation
792  *
793  * Returns the end address of the unmapping (restart addr if interrupted).
794  *
795  * Unmap all pages in the vma list.
796  *
797  * We aim to not hold locks for too long (for scheduling latency reasons).
798  * So zap pages in ZAP_BLOCK_SIZE bytecounts.  This means we need to
799  * return the ending mmu_gather to the caller.
800  *
801  * Only addresses between `start' and `end' will be unmapped.
802  *
803  * The VMA list must be sorted in ascending virtual address order.
804  *
805  * unmap_vmas() assumes that the caller will flush the whole unmapped address
806  * range after unmap_vmas() returns.  So the only responsibility here is to
807  * ensure that any thus-far unmapped pages are flushed before unmap_vmas()
808  * drops the lock and schedules.
809  */
810 unsigned long unmap_vmas(struct mmu_gather **tlbp,
811                 struct vm_area_struct *vma, unsigned long start_addr,
812                 unsigned long end_addr, unsigned long *nr_accounted,
813                 struct zap_details *details)
814 {
815         long zap_work = ZAP_BLOCK_SIZE;
816         unsigned long tlb_start = 0;    /* For tlb_finish_mmu */
817         int tlb_start_valid = 0;
818         unsigned long start = start_addr;
819         spinlock_t *i_mmap_lock = details? details->i_mmap_lock: NULL;
820         int fullmm = (*tlbp)->fullmm;
821
822         for ( ; vma && vma->vm_start < end_addr; vma = vma->vm_next) {
823                 unsigned long end;
824
825                 start = max(vma->vm_start, start_addr);
826                 if (start >= vma->vm_end)
827                         continue;
828                 end = min(vma->vm_end, end_addr);
829                 if (end <= vma->vm_start)
830                         continue;
831
832                 if (vma->vm_flags & VM_ACCOUNT)
833                         *nr_accounted += (end - start) >> PAGE_SHIFT;
834
835                 while (start != end) {
836                         if (!tlb_start_valid) {
837                                 tlb_start = start;
838                                 tlb_start_valid = 1;
839                         }
840
841                         if (unlikely(is_vm_hugetlb_page(vma))) {
842                                 unmap_hugepage_range(vma, start, end);
843                                 zap_work -= (end - start) /
844                                                 (HPAGE_SIZE / PAGE_SIZE);
845                                 start = end;
846                         } else
847                                 start = unmap_page_range(*tlbp, vma,
848                                                 start, end, &zap_work, details);
849
850                         if (zap_work > 0) {
851                                 BUG_ON(start != end);
852                                 break;
853                         }
854
855                         tlb_finish_mmu(*tlbp, tlb_start, start);
856
857                         if (need_resched() ||
858                                 (i_mmap_lock && need_lockbreak(i_mmap_lock))) {
859                                 if (i_mmap_lock) {
860                                         *tlbp = NULL;
861                                         goto out;
862                                 }
863                                 cond_resched();
864                         }
865
866                         *tlbp = tlb_gather_mmu(vma->vm_mm, fullmm);
867                         tlb_start_valid = 0;
868                         zap_work = ZAP_BLOCK_SIZE;
869                 }
870         }
871 out:
872         return start;   /* which is now the end (or restart) address */
873 }
874
875 /**
876  * zap_page_range - remove user pages in a given range
877  * @vma: vm_area_struct holding the applicable pages
878  * @address: starting address of pages to zap
879  * @size: number of bytes to zap
880  * @details: details of nonlinear truncation or shared cache invalidation
881  */
882 unsigned long zap_page_range(struct vm_area_struct *vma, unsigned long address,
883                 unsigned long size, struct zap_details *details)
884 {
885         struct mm_struct *mm = vma->vm_mm;
886         struct mmu_gather *tlb;
887         unsigned long end = address + size;
888         unsigned long nr_accounted = 0;
889
890         lru_add_drain();
891         tlb = tlb_gather_mmu(mm, 0);
892         update_hiwater_rss(mm);
893         end = unmap_vmas(&tlb, vma, address, end, &nr_accounted, details);
894         if (tlb)
895                 tlb_finish_mmu(tlb, address, end);
896         return end;
897 }
898 EXPORT_SYMBOL(zap_page_range);
899
900 /*
901  * Do a quick page-table lookup for a single page.
902  */
903 struct page *follow_page(struct vm_area_struct *vma, unsigned long address,
904                         unsigned int flags)
905 {
906         pgd_t *pgd;
907         pud_t *pud;
908         pmd_t *pmd;
909         pte_t *ptep, pte;
910         spinlock_t *ptl;
911         struct page *page;
912         struct mm_struct *mm = vma->vm_mm;
913
914         page = follow_huge_addr(mm, address, flags & FOLL_WRITE);
915         if (!IS_ERR(page)) {
916                 BUG_ON(flags & FOLL_GET);
917                 goto out;
918         }
919
920         page = NULL;
921         pgd = pgd_offset(mm, address);
922         if (pgd_none(*pgd) || unlikely(pgd_bad(*pgd)))
923                 goto no_page_table;
924
925         pud = pud_offset(pgd, address);
926         if (pud_none(*pud) || unlikely(pud_bad(*pud)))
927                 goto no_page_table;
928         
929         pmd = pmd_offset(pud, address);
930         if (pmd_none(*pmd) || unlikely(pmd_bad(*pmd)))
931                 goto no_page_table;
932
933         if (pmd_huge(*pmd)) {
934                 BUG_ON(flags & FOLL_GET);
935                 page = follow_huge_pmd(mm, address, pmd, flags & FOLL_WRITE);
936                 goto out;
937         }
938
939         ptep = pte_offset_map_lock(mm, pmd, address, &ptl);
940         if (!ptep)
941                 goto out;
942
943         pte = *ptep;
944         if (!pte_present(pte))
945                 goto unlock;
946         if ((flags & FOLL_WRITE) && !pte_write(pte))
947                 goto unlock;
948         page = vm_normal_page(vma, address, pte);
949         if (unlikely(!page))
950                 goto unlock;
951
952         if (flags & FOLL_GET)
953                 get_page(page);
954         if (flags & FOLL_TOUCH) {
955                 if ((flags & FOLL_WRITE) &&
956                     !pte_dirty(pte) && !PageDirty(page))
957                         set_page_dirty(page);
958                 mark_page_accessed(page);
959         }
960 unlock:
961         pte_unmap_unlock(ptep, ptl);
962 out:
963         return page;
964
965 no_page_table:
966         /*
967          * When core dumping an enormous anonymous area that nobody
968          * has touched so far, we don't want to allocate page tables.
969          */
970         if (flags & FOLL_ANON) {
971                 page = ZERO_PAGE(address);
972                 if (flags & FOLL_GET)
973                         get_page(page);
974                 BUG_ON(flags & FOLL_WRITE);
975         }
976         return page;
977 }
978
979 int get_user_pages(struct task_struct *tsk, struct mm_struct *mm,
980                 unsigned long start, int len, int write, int force,
981                 struct page **pages, struct vm_area_struct **vmas)
982 {
983         int i;
984         unsigned int vm_flags;
985
986         /* 
987          * Require read or write permissions.
988          * If 'force' is set, we only require the "MAY" flags.
989          */
990         vm_flags  = write ? (VM_WRITE | VM_MAYWRITE) : (VM_READ | VM_MAYREAD);
991         vm_flags &= force ? (VM_MAYREAD | VM_MAYWRITE) : (VM_READ | VM_WRITE);
992         i = 0;
993
994         do {
995                 struct vm_area_struct *vma;
996                 unsigned int foll_flags;
997
998                 vma = find_extend_vma(mm, start);
999                 if (!vma && in_gate_area(tsk, start)) {
1000                         unsigned long pg = start & PAGE_MASK;
1001                         struct vm_area_struct *gate_vma = get_gate_vma(tsk);
1002                         pgd_t *pgd;
1003                         pud_t *pud;
1004                         pmd_t *pmd;
1005                         pte_t *pte;
1006                         if (write) /* user gate pages are read-only */
1007                                 return i ? : -EFAULT;
1008                         if (pg > TASK_SIZE)
1009                                 pgd = pgd_offset_k(pg);
1010                         else
1011                                 pgd = pgd_offset_gate(mm, pg);
1012                         BUG_ON(pgd_none(*pgd));
1013                         pud = pud_offset(pgd, pg);
1014                         BUG_ON(pud_none(*pud));
1015                         pmd = pmd_offset(pud, pg);
1016                         if (pmd_none(*pmd))
1017                                 return i ? : -EFAULT;
1018                         pte = pte_offset_map(pmd, pg);
1019                         if (pte_none(*pte)) {
1020                                 pte_unmap(pte);
1021                                 return i ? : -EFAULT;
1022                         }
1023                         if (pages) {
1024                                 struct page *page = vm_normal_page(gate_vma, start, *pte);
1025                                 pages[i] = page;
1026                                 if (page)
1027                                         get_page(page);
1028                         }
1029                         pte_unmap(pte);
1030                         if (vmas)
1031                                 vmas[i] = gate_vma;
1032                         i++;
1033                         start += PAGE_SIZE;
1034                         len--;
1035                         continue;
1036                 }
1037
1038 #ifdef CONFIG_XEN
1039                 if (vma && (vma->vm_flags & VM_FOREIGN)) {
1040                         struct page **map = vma->vm_private_data;
1041                         int offset = (start - vma->vm_start) >> PAGE_SHIFT;
1042                         if (map[offset] != NULL) {
1043                                 if (pages) {
1044                                         struct page *page = map[offset];
1045                                         
1046                                         pages[i] = page;
1047                                         get_page(page);
1048                                 }
1049                                 if (vmas)
1050                                         vmas[i] = vma;
1051                                 i++;
1052                                 start += PAGE_SIZE;
1053                                 len--;
1054                                 continue;
1055                         }
1056                 }
1057 #endif
1058                 if (!vma || (vma->vm_flags & (VM_IO | VM_PFNMAP))
1059                                 || !(vm_flags & vma->vm_flags))
1060                         return i ? : -EFAULT;
1061
1062                 if (is_vm_hugetlb_page(vma)) {
1063                         i = follow_hugetlb_page(mm, vma, pages, vmas,
1064                                                 &start, &len, i);
1065                         continue;
1066                 }
1067
1068                 foll_flags = FOLL_TOUCH;
1069                 if (pages)
1070                         foll_flags |= FOLL_GET;
1071                 if (!write && !(vma->vm_flags & VM_LOCKED) &&
1072                     (!vma->vm_ops || !vma->vm_ops->nopage))
1073                         foll_flags |= FOLL_ANON;
1074
1075                 do {
1076                         struct page *page;
1077
1078                         if (write)
1079                                 foll_flags |= FOLL_WRITE;
1080
1081                         cond_resched();
1082                         while (!(page = follow_page(vma, start, foll_flags))) {
1083                                 int ret;
1084                                 ret = __handle_mm_fault(mm, vma, start,
1085                                                 foll_flags & FOLL_WRITE);
1086                                 /*
1087                                  * The VM_FAULT_WRITE bit tells us that do_wp_page has
1088                                  * broken COW when necessary, even if maybe_mkwrite
1089                                  * decided not to set pte_write. We can thus safely do
1090                                  * subsequent page lookups as if they were reads.
1091                                  */
1092                                 if (ret & VM_FAULT_WRITE)
1093                                         foll_flags &= ~FOLL_WRITE;
1094                                 
1095                                 switch (ret & ~VM_FAULT_WRITE) {
1096                                 case VM_FAULT_MINOR:
1097                                         tsk->min_flt++;
1098                                         break;
1099                                 case VM_FAULT_MAJOR:
1100                                         tsk->maj_flt++;
1101                                         break;
1102                                 case VM_FAULT_SIGBUS:
1103                                         return i ? i : -EFAULT;
1104                                 case VM_FAULT_OOM:
1105                                         return i ? i : -ENOMEM;
1106                                 default:
1107                                         BUG();
1108                                 }
1109                         }
1110                         if (pages) {
1111                                 pages[i] = page;
1112
1113                                 flush_anon_page(page, start);
1114                                 flush_dcache_page(page);
1115                         }
1116                         if (vmas)
1117                                 vmas[i] = vma;
1118                         i++;
1119                         start += PAGE_SIZE;
1120                         len--;
1121                 } while (len && start < vma->vm_end);
1122         } while (len);
1123         return i;
1124 }
1125 EXPORT_SYMBOL(get_user_pages);
1126
1127 static int zeromap_pte_range(struct mm_struct *mm, pmd_t *pmd,
1128                         unsigned long addr, unsigned long end, pgprot_t prot)
1129 {
1130         pte_t *pte;
1131         spinlock_t *ptl;
1132
1133         pte = pte_alloc_map_lock(mm, pmd, addr, &ptl);
1134         if (!pte)
1135                 return -ENOMEM;
1136         do {
1137                 struct page *page = ZERO_PAGE(addr);
1138                 pte_t zero_pte = pte_wrprotect(mk_pte(page, prot));
1139                 page_cache_get(page);
1140                 page_add_file_rmap(page);
1141                 inc_mm_counter(mm, file_rss);
1142                 BUG_ON(!pte_none(*pte));
1143                 set_pte_at(mm, addr, pte, zero_pte);
1144         } while (pte++, addr += PAGE_SIZE, addr != end);
1145         pte_unmap_unlock(pte - 1, ptl);
1146         return 0;
1147 }
1148
1149 static inline int zeromap_pmd_range(struct mm_struct *mm, pud_t *pud,
1150                         unsigned long addr, unsigned long end, pgprot_t prot)
1151 {
1152         pmd_t *pmd;
1153         unsigned long next;
1154
1155         pmd = pmd_alloc(mm, pud, addr);
1156         if (!pmd)
1157                 return -ENOMEM;
1158         do {
1159                 next = pmd_addr_end(addr, end);
1160                 if (zeromap_pte_range(mm, pmd, addr, next, prot))
1161                         return -ENOMEM;
1162         } while (pmd++, addr = next, addr != end);
1163         return 0;
1164 }
1165
1166 static inline int zeromap_pud_range(struct mm_struct *mm, pgd_t *pgd,
1167                         unsigned long addr, unsigned long end, pgprot_t prot)
1168 {
1169         pud_t *pud;
1170         unsigned long next;
1171
1172         pud = pud_alloc(mm, pgd, addr);
1173         if (!pud)
1174                 return -ENOMEM;
1175         do {
1176                 next = pud_addr_end(addr, end);
1177                 if (zeromap_pmd_range(mm, pud, addr, next, prot))
1178                         return -ENOMEM;
1179         } while (pud++, addr = next, addr != end);
1180         return 0;
1181 }
1182
1183 int zeromap_page_range(struct vm_area_struct *vma,
1184                         unsigned long addr, unsigned long size, pgprot_t prot)
1185 {
1186         pgd_t *pgd;
1187         unsigned long next;
1188         unsigned long end = addr + size;
1189         struct mm_struct *mm = vma->vm_mm;
1190         int err;
1191
1192         BUG_ON(addr >= end);
1193         pgd = pgd_offset(mm, addr);
1194         flush_cache_range(vma, addr, end);
1195         do {
1196                 next = pgd_addr_end(addr, end);
1197                 err = zeromap_pud_range(mm, pgd, addr, next, prot);
1198                 if (err)
1199                         break;
1200         } while (pgd++, addr = next, addr != end);
1201         return err;
1202 }
1203
1204 pte_t * fastcall get_locked_pte(struct mm_struct *mm, unsigned long addr, spinlock_t **ptl)
1205 {
1206         pgd_t * pgd = pgd_offset(mm, addr);
1207         pud_t * pud = pud_alloc(mm, pgd, addr);
1208         if (pud) {
1209                 pmd_t * pmd = pmd_alloc(mm, pud, addr);
1210                 if (pmd)
1211                         return pte_alloc_map_lock(mm, pmd, addr, ptl);
1212         }
1213         return NULL;
1214 }
1215
1216 /*
1217  * This is the old fallback for page remapping.
1218  *
1219  * For historical reasons, it only allows reserved pages. Only
1220  * old drivers should use this, and they needed to mark their
1221  * pages reserved for the old functions anyway.
1222  */
1223 static int insert_page(struct mm_struct *mm, unsigned long addr, struct page *page, pgprot_t prot)
1224 {
1225         int retval;
1226         pte_t *pte;
1227         spinlock_t *ptl;  
1228
1229         retval = -EINVAL;
1230         if (PageAnon(page))
1231                 goto out;
1232         retval = -ENOMEM;
1233         flush_dcache_page(page);
1234         pte = get_locked_pte(mm, addr, &ptl);
1235         if (!pte)
1236                 goto out;
1237         retval = -EBUSY;
1238         if (!pte_none(*pte))
1239                 goto out_unlock;
1240
1241         /* Ok, finally just insert the thing.. */
1242         get_page(page);
1243         inc_mm_counter(mm, file_rss);
1244         page_add_file_rmap(page);
1245         set_pte_at(mm, addr, pte, mk_pte(page, prot));
1246
1247         retval = 0;
1248 out_unlock:
1249         pte_unmap_unlock(pte, ptl);
1250 out:
1251         return retval;
1252 }
1253
1254 /*
1255  * This allows drivers to insert individual pages they've allocated
1256  * into a user vma.
1257  *
1258  * The page has to be a nice clean _individual_ kernel allocation.
1259  * If you allocate a compound page, you need to have marked it as
1260  * such (__GFP_COMP), or manually just split the page up yourself
1261  * (see split_page()).
1262  *
1263  * NOTE! Traditionally this was done with "remap_pfn_range()" which
1264  * took an arbitrary page protection parameter. This doesn't allow
1265  * that. Your vma protection will have to be set up correctly, which
1266  * means that if you want a shared writable mapping, you'd better
1267  * ask for a shared writable mapping!
1268  *
1269  * The page does not need to be reserved.
1270  */
1271 int vm_insert_page(struct vm_area_struct *vma, unsigned long addr, struct page *page)
1272 {
1273         if (addr < vma->vm_start || addr >= vma->vm_end)
1274                 return -EFAULT;
1275         if (!page_count(page))
1276                 return -EINVAL;
1277         vma->vm_flags |= VM_INSERTPAGE;
1278         return insert_page(vma->vm_mm, addr, page, vma->vm_page_prot);
1279 }
1280 EXPORT_SYMBOL(vm_insert_page);
1281
1282 /*
1283  * maps a range of physical memory into the requested pages. the old
1284  * mappings are removed. any references to nonexistent pages results
1285  * in null mappings (currently treated as "copy-on-access")
1286  */
1287 static int remap_pte_range(struct mm_struct *mm, pmd_t *pmd,
1288                         unsigned long addr, unsigned long end,
1289                         unsigned long pfn, pgprot_t prot)
1290 {
1291         pte_t *pte;
1292         spinlock_t *ptl;
1293
1294         pte = pte_alloc_map_lock(mm, pmd, addr, &ptl);
1295         if (!pte)
1296                 return -ENOMEM;
1297         do {
1298                 BUG_ON(!pte_none(*pte));
1299                 set_pte_at(mm, addr, pte, pfn_pte(pfn, prot));
1300                 pfn++;
1301         } while (pte++, addr += PAGE_SIZE, addr != end);
1302         pte_unmap_unlock(pte - 1, ptl);
1303         return 0;
1304 }
1305
1306 static inline int remap_pmd_range(struct mm_struct *mm, pud_t *pud,
1307                         unsigned long addr, unsigned long end,
1308                         unsigned long pfn, pgprot_t prot)
1309 {
1310         pmd_t *pmd;
1311         unsigned long next;
1312
1313         pfn -= addr >> PAGE_SHIFT;
1314         pmd = pmd_alloc(mm, pud, addr);
1315         if (!pmd)
1316                 return -ENOMEM;
1317         do {
1318                 next = pmd_addr_end(addr, end);
1319                 if (remap_pte_range(mm, pmd, addr, next,
1320                                 pfn + (addr >> PAGE_SHIFT), prot))
1321                         return -ENOMEM;
1322         } while (pmd++, addr = next, addr != end);
1323         return 0;
1324 }
1325
1326 static inline int remap_pud_range(struct mm_struct *mm, pgd_t *pgd,
1327                         unsigned long addr, unsigned long end,
1328                         unsigned long pfn, pgprot_t prot)
1329 {
1330         pud_t *pud;
1331         unsigned long next;
1332
1333         pfn -= addr >> PAGE_SHIFT;
1334         pud = pud_alloc(mm, pgd, addr);
1335         if (!pud)
1336                 return -ENOMEM;
1337         do {
1338                 next = pud_addr_end(addr, end);
1339                 if (remap_pmd_range(mm, pud, addr, next,
1340                                 pfn + (addr >> PAGE_SHIFT), prot))
1341                         return -ENOMEM;
1342         } while (pud++, addr = next, addr != end);
1343         return 0;
1344 }
1345
1346 /*  Note: this is only safe if the mm semaphore is held when called. */
1347 int remap_pfn_range(struct vm_area_struct *vma, unsigned long addr,
1348                     unsigned long pfn, unsigned long size, pgprot_t prot)
1349 {
1350         pgd_t *pgd;
1351         unsigned long next;
1352         unsigned long end = addr + PAGE_ALIGN(size);
1353         struct mm_struct *mm = vma->vm_mm;
1354         int err;
1355
1356         /*
1357          * Physically remapped pages are special. Tell the
1358          * rest of the world about it:
1359          *   VM_IO tells people not to look at these pages
1360          *      (accesses can have side effects).
1361          *   VM_RESERVED is specified all over the place, because
1362          *      in 2.4 it kept swapout's vma scan off this vma; but
1363          *      in 2.6 the LRU scan won't even find its pages, so this
1364          *      flag means no more than count its pages in reserved_vm,
1365          *      and omit it from core dump, even when VM_IO turned off.
1366          *   VM_PFNMAP tells the core MM that the base pages are just
1367          *      raw PFN mappings, and do not have a "struct page" associated
1368          *      with them.
1369          *
1370          * There's a horrible special case to handle copy-on-write
1371          * behaviour that some programs depend on. We mark the "original"
1372          * un-COW'ed pages by matching them up with "vma->vm_pgoff".
1373          */
1374         if (is_cow_mapping(vma->vm_flags)) {
1375                 if (addr != vma->vm_start || end != vma->vm_end)
1376                         return -EINVAL;
1377                 vma->vm_pgoff = pfn;
1378         }
1379
1380         vma->vm_flags |= VM_IO | VM_RESERVED | VM_PFNMAP;
1381
1382         BUG_ON(addr >= end);
1383         pfn -= addr >> PAGE_SHIFT;
1384         pgd = pgd_offset(mm, addr);
1385         flush_cache_range(vma, addr, end);
1386         do {
1387                 next = pgd_addr_end(addr, end);
1388                 err = remap_pud_range(mm, pgd, addr, next,
1389                                 pfn + (addr >> PAGE_SHIFT), prot);
1390                 if (err)
1391                         break;
1392         } while (pgd++, addr = next, addr != end);
1393         return err;
1394 }
1395 EXPORT_SYMBOL(remap_pfn_range);
1396
1397 #ifdef CONFIG_XEN
1398 static inline int apply_to_pte_range(struct mm_struct *mm, pmd_t *pmd,
1399                                      unsigned long addr, unsigned long end,
1400                                      pte_fn_t fn, void *data)
1401 {
1402         pte_t *pte;
1403         int err;
1404         struct page *pmd_page;
1405         spinlock_t *ptl;
1406
1407         pte = (mm == &init_mm) ?
1408                 pte_alloc_kernel(pmd, addr) :
1409                 pte_alloc_map_lock(mm, pmd, addr, &ptl);
1410         if (!pte)
1411                 return -ENOMEM;
1412
1413         BUG_ON(pmd_huge(*pmd));
1414
1415         pmd_page = pmd_page(*pmd);
1416
1417         do {
1418                 err = fn(pte, pmd_page, addr, data);
1419                 if (err)
1420                         break;
1421         } while (pte++, addr += PAGE_SIZE, addr != end);
1422
1423         if (mm != &init_mm)
1424                 pte_unmap_unlock(pte-1, ptl);
1425         return err;
1426 }
1427
1428 static inline int apply_to_pmd_range(struct mm_struct *mm, pud_t *pud,
1429                                      unsigned long addr, unsigned long end,
1430                                      pte_fn_t fn, void *data)
1431 {
1432         pmd_t *pmd;
1433         unsigned long next;
1434         int err;
1435
1436         pmd = pmd_alloc(mm, pud, addr);
1437         if (!pmd)
1438                 return -ENOMEM;
1439         do {
1440                 next = pmd_addr_end(addr, end);
1441                 err = apply_to_pte_range(mm, pmd, addr, next, fn, data);
1442                 if (err)
1443                         break;
1444         } while (pmd++, addr = next, addr != end);
1445         return err;
1446 }
1447
1448 static inline int apply_to_pud_range(struct mm_struct *mm, pgd_t *pgd,
1449                                      unsigned long addr, unsigned long end,
1450                                      pte_fn_t fn, void *data)
1451 {
1452         pud_t *pud;
1453         unsigned long next;
1454         int err;
1455
1456         pud = pud_alloc(mm, pgd, addr);
1457         if (!pud)
1458                 return -ENOMEM;
1459         do {
1460                 next = pud_addr_end(addr, end);
1461                 err = apply_to_pmd_range(mm, pud, addr, next, fn, data);
1462                 if (err)
1463                         break;
1464         } while (pud++, addr = next, addr != end);
1465         return err;
1466 }
1467
1468 /*
1469  * Scan a region of virtual memory, filling in page tables as necessary
1470  * and calling a provided function on each leaf page table.
1471  */
1472 int apply_to_page_range(struct mm_struct *mm, unsigned long addr,
1473                         unsigned long size, pte_fn_t fn, void *data)
1474 {
1475         pgd_t *pgd;
1476         unsigned long next;
1477         unsigned long end = addr + size;
1478         int err;
1479
1480         BUG_ON(addr >= end);
1481         pgd = pgd_offset(mm, addr);
1482         do {
1483                 next = pgd_addr_end(addr, end);
1484                 err = apply_to_pud_range(mm, pgd, addr, next, fn, data);
1485                 if (err)
1486                         break;
1487         } while (pgd++, addr = next, addr != end);
1488         return err;
1489 }
1490 EXPORT_SYMBOL_GPL(apply_to_page_range);
1491 #endif
1492
1493 /*
1494  * handle_pte_fault chooses page fault handler according to an entry
1495  * which was read non-atomically.  Before making any commitment, on
1496  * those architectures or configurations (e.g. i386 with PAE) which
1497  * might give a mix of unmatched parts, do_swap_page and do_file_page
1498  * must check under lock before unmapping the pte and proceeding
1499  * (but do_wp_page is only called after already making such a check;
1500  * and do_anonymous_page and do_no_page can safely check later on).
1501  */
1502 static inline int pte_unmap_same(struct mm_struct *mm, pmd_t *pmd,
1503                                 pte_t *page_table, pte_t orig_pte)
1504 {
1505         int same = 1;
1506 #if defined(CONFIG_SMP) || defined(CONFIG_PREEMPT)
1507         if (sizeof(pte_t) > sizeof(unsigned long)) {
1508                 spinlock_t *ptl = pte_lockptr(mm, pmd);
1509                 spin_lock(ptl);
1510                 same = pte_same(*page_table, orig_pte);
1511                 spin_unlock(ptl);
1512         }
1513 #endif
1514         pte_unmap(page_table);
1515         return same;
1516 }
1517
1518 /*
1519  * Do pte_mkwrite, but only if the vma says VM_WRITE.  We do this when
1520  * servicing faults for write access.  In the normal case, do always want
1521  * pte_mkwrite.  But get_user_pages can cause write faults for mappings
1522  * that do not have writing enabled, when used by access_process_vm.
1523  */
1524 static inline pte_t maybe_mkwrite(pte_t pte, struct vm_area_struct *vma)
1525 {
1526         if (likely(vma->vm_flags & VM_WRITE))
1527                 pte = pte_mkwrite(pte);
1528         return pte;
1529 }
1530
1531 static inline void cow_user_page(struct page *dst, struct page *src, unsigned long va)
1532 {
1533         /*
1534          * If the source page was a PFN mapping, we don't have
1535          * a "struct page" for it. We do a best-effort copy by
1536          * just copying from the original user address. If that
1537          * fails, we just zero-fill it. Live with it.
1538          */
1539         if (unlikely(!src)) {
1540                 void *kaddr = kmap_atomic(dst, KM_USER0);
1541                 void __user *uaddr = (void __user *)(va & PAGE_MASK);
1542
1543                 /*
1544                  * This really shouldn't fail, because the page is there
1545                  * in the page tables. But it might just be unreadable,
1546                  * in which case we just give up and fill the result with
1547                  * zeroes.
1548                  */
1549                 if (__copy_from_user_inatomic(kaddr, uaddr, PAGE_SIZE))
1550                         memset(kaddr, 0, PAGE_SIZE);
1551                 kunmap_atomic(kaddr, KM_USER0);
1552                 return;
1553                 
1554         }
1555         copy_user_highpage(dst, src, va);
1556 }
1557
1558 /*
1559  * This routine handles present pages, when users try to write
1560  * to a shared page. It is done by copying the page to a new address
1561  * and decrementing the shared-page counter for the old page.
1562  *
1563  * Note that this routine assumes that the protection checks have been
1564  * done by the caller (the low-level page fault routine in most cases).
1565  * Thus we can safely just mark it writable once we've done any necessary
1566  * COW.
1567  *
1568  * We also mark the page dirty at this point even though the page will
1569  * change only once the write actually happens. This avoids a few races,
1570  * and potentially makes it more efficient.
1571  *
1572  * We enter with non-exclusive mmap_sem (to exclude vma changes,
1573  * but allow concurrent faults), with pte both mapped and locked.
1574  * We return with mmap_sem still held, but pte unmapped and unlocked.
1575  */
1576 static int do_wp_page(struct mm_struct *mm, struct vm_area_struct *vma,
1577                 unsigned long address, pte_t *page_table, pmd_t *pmd,
1578                 spinlock_t *ptl, pte_t orig_pte)
1579 {
1580         struct page *old_page, *new_page;
1581         pte_t entry;
1582         int reuse = 0, ret = VM_FAULT_MINOR;
1583         struct page *dirty_page = NULL;
1584
1585         old_page = vm_normal_page(vma, address, orig_pte);
1586         if (!old_page)
1587                 goto gotten;
1588
1589         /*
1590          * Take out anonymous pages first, anonymous shared vmas are
1591          * not dirty accountable.
1592          */
1593         if (PageAnon(old_page)) {
1594                 if (!TestSetPageLocked(old_page)) {
1595                         reuse = can_share_swap_page(old_page);
1596                         unlock_page(old_page);
1597                 }
1598         } else if (unlikely((vma->vm_flags & (VM_WRITE|VM_SHARED)) ==
1599                                         (VM_WRITE|VM_SHARED))) {
1600                 /*
1601                  * Only catch write-faults on shared writable pages,
1602                  * read-only shared pages can get COWed by
1603                  * get_user_pages(.write=1, .force=1).
1604                  */
1605                 if (vma->vm_ops && vma->vm_ops->page_mkwrite) {
1606                         /*
1607                          * Notify the address space that the page is about to
1608                          * become writable so that it can prohibit this or wait
1609                          * for the page to get into an appropriate state.
1610                          *
1611                          * We do this without the lock held, so that it can
1612                          * sleep if it needs to.
1613                          */
1614                         page_cache_get(old_page);
1615                         pte_unmap_unlock(page_table, ptl);
1616
1617                         if (vma->vm_ops->page_mkwrite(vma, old_page) < 0)
1618                                 goto unwritable_page;
1619
1620                         page_cache_release(old_page);
1621
1622                         /*
1623                          * Since we dropped the lock we need to revalidate
1624                          * the PTE as someone else may have changed it.  If
1625                          * they did, we just return, as we can count on the
1626                          * MMU to tell us if they didn't also make it writable.
1627                          */
1628                         page_table = pte_offset_map_lock(mm, pmd, address,
1629                                                          &ptl);
1630                         if (!pte_same(*page_table, orig_pte))
1631                                 goto unlock;
1632                 }
1633                 dirty_page = old_page;
1634                 get_page(dirty_page);
1635                 reuse = 1;
1636         }
1637
1638         if (reuse) {
1639                 flush_cache_page(vma, address, pte_pfn(orig_pte));
1640                 entry = pte_mkyoung(orig_pte);
1641                 entry = maybe_mkwrite(pte_mkdirty(entry), vma);
1642                 ptep_set_access_flags(vma, address, page_table, entry, 1);
1643                 update_mmu_cache(vma, address, entry);
1644                 lazy_mmu_prot_update(entry);
1645                 ret |= VM_FAULT_WRITE;
1646                 goto unlock;
1647         }
1648
1649         /*
1650          * Ok, we need to copy. Oh, well..
1651          */
1652         page_cache_get(old_page);
1653 gotten:
1654         pte_unmap_unlock(page_table, ptl);
1655
1656         if (unlikely(anon_vma_prepare(vma)))
1657                 goto oom;
1658         if (old_page == ZERO_PAGE(address)) {
1659                 new_page = alloc_zeroed_user_highpage(vma, address);
1660                 if (!new_page)
1661                         goto oom;
1662         } else {
1663                 new_page = alloc_page_vma(GFP_HIGHUSER, vma, address);
1664                 if (!new_page)
1665                         goto oom;
1666                 cow_user_page(new_page, old_page, address);
1667         }
1668
1669         /*
1670          * Re-check the pte - we dropped the lock
1671          */
1672         page_table = pte_offset_map_lock(mm, pmd, address, &ptl);
1673         if (likely(pte_same(*page_table, orig_pte))) {
1674                 if (old_page) {
1675                         page_remove_rmap(old_page, vma);
1676                         if (!PageAnon(old_page)) {
1677                                 dec_mm_counter(mm, file_rss);
1678                                 inc_mm_counter(mm, anon_rss);
1679                         }
1680                 } else
1681                         inc_mm_counter(mm, anon_rss);
1682                 flush_cache_page(vma, address, pte_pfn(orig_pte));
1683                 entry = mk_pte(new_page, vma->vm_page_prot);
1684                 entry = maybe_mkwrite(pte_mkdirty(entry), vma);
1685                 lazy_mmu_prot_update(entry);
1686                 /*
1687                  * Clear the pte entry and flush it first, before updating the
1688                  * pte with the new entry. This will avoid a race condition
1689                  * seen in the presence of one thread doing SMC and another
1690                  * thread doing COW.
1691                  */
1692                 ptep_clear_flush(vma, address, page_table);
1693                 set_pte_at(mm, address, page_table, entry);
1694                 update_mmu_cache(vma, address, entry);
1695                 lru_cache_add_active(new_page);
1696                 page_add_new_anon_rmap(new_page, vma, address);
1697
1698                 /* Free the old page.. */
1699                 new_page = old_page;
1700                 ret |= VM_FAULT_WRITE;
1701         }
1702         if (new_page)
1703                 page_cache_release(new_page);
1704         if (old_page)
1705                 page_cache_release(old_page);
1706 unlock:
1707         pte_unmap_unlock(page_table, ptl);
1708         if (dirty_page) {
1709                 set_page_dirty_balance(dirty_page);
1710                 put_page(dirty_page);
1711         }
1712         return ret;
1713 oom:
1714         if (old_page)
1715                 page_cache_release(old_page);
1716         return VM_FAULT_OOM;
1717
1718 unwritable_page:
1719         page_cache_release(old_page);
1720         return VM_FAULT_SIGBUS;
1721 }
1722
1723 /*
1724  * Helper functions for unmap_mapping_range().
1725  *
1726  * __ Notes on dropping i_mmap_lock to reduce latency while unmapping __
1727  *
1728  * We have to restart searching the prio_tree whenever we drop the lock,
1729  * since the iterator is only valid while the lock is held, and anyway
1730  * a later vma might be split and reinserted earlier while lock dropped.
1731  *
1732  * The list of nonlinear vmas could be handled more efficiently, using
1733  * a placeholder, but handle it in the same way until a need is shown.
1734  * It is important to search the prio_tree before nonlinear list: a vma
1735  * may become nonlinear and be shifted from prio_tree to nonlinear list
1736  * while the lock is dropped; but never shifted from list to prio_tree.
1737  *
1738  * In order to make forward progress despite restarting the search,
1739  * vm_truncate_count is used to mark a vma as now dealt with, so we can
1740  * quickly skip it next time around.  Since the prio_tree search only
1741  * shows us those vmas affected by unmapping the range in question, we
1742  * can't efficiently keep all vmas in step with mapping->truncate_count:
1743  * so instead reset them all whenever it wraps back to 0 (then go to 1).
1744  * mapping->truncate_count and vma->vm_truncate_count are protected by
1745  * i_mmap_lock.
1746  *
1747  * In order to make forward progress despite repeatedly restarting some
1748  * large vma, note the restart_addr from unmap_vmas when it breaks out:
1749  * and restart from that address when we reach that vma again.  It might
1750  * have been split or merged, shrunk or extended, but never shifted: so
1751  * restart_addr remains valid so long as it remains in the vma's range.
1752  * unmap_mapping_range forces truncate_count to leap over page-aligned
1753  * values so we can save vma's restart_addr in its truncate_count field.
1754  */
1755 #define is_restart_addr(truncate_count) (!((truncate_count) & ~PAGE_MASK))
1756
1757 static void reset_vma_truncate_counts(struct address_space *mapping)
1758 {
1759         struct vm_area_struct *vma;
1760         struct prio_tree_iter iter;
1761
1762         vma_prio_tree_foreach(vma, &iter, &mapping->i_mmap, 0, ULONG_MAX)
1763                 vma->vm_truncate_count = 0;
1764         list_for_each_entry(vma, &mapping->i_mmap_nonlinear, shared.vm_set.list)
1765                 vma->vm_truncate_count = 0;
1766 }
1767
1768 static int unmap_mapping_range_vma(struct vm_area_struct *vma,
1769                 unsigned long start_addr, unsigned long end_addr,
1770                 struct zap_details *details)
1771 {
1772         unsigned long restart_addr;
1773         int need_break;
1774
1775 again:
1776         restart_addr = vma->vm_truncate_count;
1777         if (is_restart_addr(restart_addr) && start_addr < restart_addr) {
1778                 start_addr = restart_addr;
1779                 if (start_addr >= end_addr) {
1780                         /* Top of vma has been split off since last time */
1781                         vma->vm_truncate_count = details->truncate_count;
1782                         return 0;
1783                 }
1784         }
1785
1786         restart_addr = zap_page_range(vma, start_addr,
1787                                         end_addr - start_addr, details);
1788         need_break = need_resched() ||
1789                         need_lockbreak(details->i_mmap_lock);
1790
1791         if (restart_addr >= end_addr) {
1792                 /* We have now completed this vma: mark it so */
1793                 vma->vm_truncate_count = details->truncate_count;
1794                 if (!need_break)
1795                         return 0;
1796         } else {
1797                 /* Note restart_addr in vma's truncate_count field */
1798                 vma->vm_truncate_count = restart_addr;
1799                 if (!need_break)
1800                         goto again;
1801         }
1802
1803         spin_unlock(details->i_mmap_lock);
1804         cond_resched();
1805         spin_lock(details->i_mmap_lock);
1806         return -EINTR;
1807 }
1808
1809 static inline void unmap_mapping_range_tree(struct prio_tree_root *root,
1810                                             struct zap_details *details)
1811 {
1812         struct vm_area_struct *vma;
1813         struct prio_tree_iter iter;
1814         pgoff_t vba, vea, zba, zea;
1815
1816 restart:
1817         vma_prio_tree_foreach(vma, &iter, root,
1818                         details->first_index, details->last_index) {
1819                 /* Skip quickly over those we have already dealt with */
1820                 if (vma->vm_truncate_count == details->truncate_count)
1821                         continue;
1822
1823                 vba = vma->vm_pgoff;
1824                 vea = vba + ((vma->vm_end - vma->vm_start) >> PAGE_SHIFT) - 1;
1825                 /* Assume for now that PAGE_CACHE_SHIFT == PAGE_SHIFT */
1826                 zba = details->first_index;
1827                 if (zba < vba)
1828                         zba = vba;
1829                 zea = details->last_index;
1830                 if (zea > vea)
1831                         zea = vea;
1832
1833                 if (unmap_mapping_range_vma(vma,
1834                         ((zba - vba) << PAGE_SHIFT) + vma->vm_start,
1835                         ((zea - vba + 1) << PAGE_SHIFT) + vma->vm_start,
1836                                 details) < 0)
1837                         goto restart;
1838         }
1839 }
1840
1841 static inline void unmap_mapping_range_list(struct list_head *head,
1842                                             struct zap_details *details)
1843 {
1844         struct vm_area_struct *vma;
1845
1846         /*
1847          * In nonlinear VMAs there is no correspondence between virtual address
1848          * offset and file offset.  So we must perform an exhaustive search
1849          * across *all* the pages in each nonlinear VMA, not just the pages
1850          * whose virtual address lies outside the file truncation point.
1851          */
1852 restart:
1853         list_for_each_entry(vma, head, shared.vm_set.list) {
1854                 /* Skip quickly over those we have already dealt with */
1855                 if (vma->vm_truncate_count == details->truncate_count)
1856                         continue;
1857                 details->nonlinear_vma = vma;
1858                 if (unmap_mapping_range_vma(vma, vma->vm_start,
1859                                         vma->vm_end, details) < 0)
1860                         goto restart;
1861         }
1862 }
1863
1864 /**
1865  * unmap_mapping_range - unmap the portion of all mmaps
1866  * in the specified address_space corresponding to the specified
1867  * page range in the underlying file.
1868  * @mapping: the address space containing mmaps to be unmapped.
1869  * @holebegin: byte in first page to unmap, relative to the start of
1870  * the underlying file.  This will be rounded down to a PAGE_SIZE
1871  * boundary.  Note that this is different from vmtruncate(), which
1872  * must keep the partial page.  In contrast, we must get rid of
1873  * partial pages.
1874  * @holelen: size of prospective hole in bytes.  This will be rounded
1875  * up to a PAGE_SIZE boundary.  A holelen of zero truncates to the
1876  * end of the file.
1877  * @even_cows: 1 when truncating a file, unmap even private COWed pages;
1878  * but 0 when invalidating pagecache, don't throw away private data.
1879  */
1880 void unmap_mapping_range(struct address_space *mapping,
1881                 loff_t const holebegin, loff_t const holelen, int even_cows)
1882 {
1883         struct zap_details details;
1884         pgoff_t hba = holebegin >> PAGE_SHIFT;
1885         pgoff_t hlen = (holelen + PAGE_SIZE - 1) >> PAGE_SHIFT;
1886
1887         /* Check for overflow. */
1888         if (sizeof(holelen) > sizeof(hlen)) {
1889                 long long holeend =
1890                         (holebegin + holelen + PAGE_SIZE - 1) >> PAGE_SHIFT;
1891                 if (holeend & ~(long long)ULONG_MAX)
1892                         hlen = ULONG_MAX - hba + 1;
1893         }
1894
1895         details.check_mapping = even_cows? NULL: mapping;
1896         details.nonlinear_vma = NULL;
1897         details.first_index = hba;
1898         details.last_index = hba + hlen - 1;
1899         if (details.last_index < details.first_index)
1900                 details.last_index = ULONG_MAX;
1901         details.i_mmap_lock = &mapping->i_mmap_lock;
1902
1903         spin_lock(&mapping->i_mmap_lock);
1904
1905         /* serialize i_size write against truncate_count write */
1906         smp_wmb();
1907         /* Protect against page faults, and endless unmapping loops */
1908         mapping->truncate_count++;
1909         /*
1910          * For archs where spin_lock has inclusive semantics like ia64
1911          * this smp_mb() will prevent to read pagetable contents
1912          * before the truncate_count increment is visible to
1913          * other cpus.
1914          */
1915         smp_mb();
1916         if (unlikely(is_restart_addr(mapping->truncate_count))) {
1917                 if (mapping->truncate_count == 0)
1918                         reset_vma_truncate_counts(mapping);
1919                 mapping->truncate_count++;
1920         }
1921         details.truncate_count = mapping->truncate_count;
1922
1923         if (unlikely(!prio_tree_empty(&mapping->i_mmap)))
1924                 unmap_mapping_range_tree(&mapping->i_mmap, &details);
1925         if (unlikely(!list_empty(&mapping->i_mmap_nonlinear)))
1926                 unmap_mapping_range_list(&mapping->i_mmap_nonlinear, &details);
1927         spin_unlock(&mapping->i_mmap_lock);
1928 }
1929 EXPORT_SYMBOL(unmap_mapping_range);
1930
1931 /*
1932  * Handle all mappings that got truncated by a "truncate()"
1933  * system call.
1934  *
1935  * NOTE! We have to be ready to update the memory sharing
1936  * between the file and the memory map for a potential last
1937  * incomplete page.  Ugly, but necessary.
1938  */
1939 int vmtruncate(struct inode * inode, loff_t offset)
1940 {
1941         struct address_space *mapping = inode->i_mapping;
1942         unsigned long limit;
1943
1944         if (inode->i_size < offset)
1945                 goto do_expand;
1946         /*
1947          * truncation of in-use swapfiles is disallowed - it would cause
1948          * subsequent swapout to scribble on the now-freed blocks.
1949          */
1950         if (IS_SWAPFILE(inode))
1951                 goto out_busy;
1952         i_size_write(inode, offset);
1953         unmap_mapping_range(mapping, offset + PAGE_SIZE - 1, 0, 1);
1954         truncate_inode_pages(mapping, offset);
1955         goto out_truncate;
1956
1957 do_expand:
1958         limit = current->signal->rlim[RLIMIT_FSIZE].rlim_cur;
1959         if (limit != RLIM_INFINITY && offset > limit)
1960                 goto out_sig;
1961         if (offset > inode->i_sb->s_maxbytes)
1962                 goto out_big;
1963         i_size_write(inode, offset);
1964
1965 out_truncate:
1966         if (inode->i_op && inode->i_op->truncate)
1967                 inode->i_op->truncate(inode);
1968         return 0;
1969 out_sig:
1970         send_sig(SIGXFSZ, current, 0);
1971 out_big:
1972         return -EFBIG;
1973 out_busy:
1974         return -ETXTBSY;
1975 }
1976 EXPORT_SYMBOL(vmtruncate);
1977
1978 int vmtruncate_range(struct inode *inode, loff_t offset, loff_t end)
1979 {
1980         struct address_space *mapping = inode->i_mapping;
1981
1982         /*
1983          * If the underlying filesystem is not going to provide
1984          * a way to truncate a range of blocks (punch a hole) -
1985          * we should return failure right now.
1986          */
1987         if (!inode->i_op || !inode->i_op->truncate_range)
1988                 return -ENOSYS;
1989
1990         mutex_lock(&inode->i_mutex);
1991         down_write(&inode->i_alloc_sem);
1992         unmap_mapping_range(mapping, offset, (end - offset), 1);
1993         truncate_inode_pages_range(mapping, offset, end);
1994         inode->i_op->truncate_range(inode, offset, end);
1995         up_write(&inode->i_alloc_sem);
1996         mutex_unlock(&inode->i_mutex);
1997
1998         return 0;
1999 }
2000 EXPORT_UNUSED_SYMBOL(vmtruncate_range);  /*  June 2006  */
2001
2002 /* 
2003  * Primitive swap readahead code. We simply read an aligned block of
2004  * (1 << page_cluster) entries in the swap area. This method is chosen
2005  * because it doesn't cost us any seek time.  We also make sure to queue
2006  * the 'original' request together with the readahead ones...  
2007  *
2008  * This has been extended to use the NUMA policies from the mm triggering
2009  * the readahead.
2010  *
2011  * Caller must hold down_read on the vma->vm_mm if vma is not NULL.
2012  */
2013 void swapin_readahead(swp_entry_t entry, unsigned long addr,struct vm_area_struct *vma)
2014 {
2015 #ifdef CONFIG_NUMA
2016         struct vm_area_struct *next_vma = vma ? vma->vm_next : NULL;
2017 #endif
2018         int i, num;
2019         struct page *new_page;
2020         unsigned long offset;
2021
2022         /*
2023          * Get the number of handles we should do readahead io to.
2024          */
2025         num = valid_swaphandles(entry, &offset);
2026         for (i = 0; i < num; offset++, i++) {
2027                 /* Ok, do the async read-ahead now */
2028                 new_page = read_swap_cache_async(swp_entry(swp_type(entry),
2029                                                            offset), vma, addr);
2030                 if (!new_page)
2031                         break;
2032                 page_cache_release(new_page);
2033 #ifdef CONFIG_NUMA
2034                 /*
2035                  * Find the next applicable VMA for the NUMA policy.
2036                  */
2037                 addr += PAGE_SIZE;
2038                 if (addr == 0)
2039                         vma = NULL;
2040                 if (vma) {
2041                         if (addr >= vma->vm_end) {
2042                                 vma = next_vma;
2043                                 next_vma = vma ? vma->vm_next : NULL;
2044                         }
2045                         if (vma && addr < vma->vm_start)
2046                                 vma = NULL;
2047                 } else {
2048                         if (next_vma && addr >= next_vma->vm_start) {
2049                                 vma = next_vma;
2050                                 next_vma = vma->vm_next;
2051                         }
2052                 }
2053 #endif
2054         }
2055         lru_add_drain();        /* Push any new pages onto the LRU now */
2056 }
2057
2058 /*
2059  * We enter with non-exclusive mmap_sem (to exclude vma changes,
2060  * but allow concurrent faults), and pte mapped but not yet locked.
2061  * We return with mmap_sem still held, but pte unmapped and unlocked.
2062  */
2063 static int do_swap_page(struct mm_struct *mm, struct vm_area_struct *vma,
2064                 unsigned long address, pte_t *page_table, pmd_t *pmd,
2065                 int write_access, pte_t orig_pte)
2066 {
2067         spinlock_t *ptl;
2068         struct page *page;
2069         swp_entry_t entry;
2070         pte_t pte;
2071         int ret = VM_FAULT_MINOR;
2072
2073         if (!pte_unmap_same(mm, pmd, page_table, orig_pte))
2074                 goto out;
2075
2076         entry = pte_to_swp_entry(orig_pte);
2077         if (is_migration_entry(entry)) {
2078                 migration_entry_wait(mm, pmd, address);
2079                 goto out;
2080         }
2081         delayacct_set_flag(DELAYACCT_PF_SWAPIN);
2082         page = lookup_swap_cache(entry);
2083         if (!page) {
2084                 swapin_readahead(entry, address, vma);
2085                 page = read_swap_cache_async(entry, vma, address);
2086                 if (!page) {
2087                         /*
2088                          * Back out if somebody else faulted in this pte
2089                          * while we released the pte lock.
2090                          */
2091                         page_table = pte_offset_map_lock(mm, pmd, address, &ptl);
2092                         if (likely(pte_same(*page_table, orig_pte)))
2093                                 ret = VM_FAULT_OOM;
2094                         delayacct_clear_flag(DELAYACCT_PF_SWAPIN);
2095                         goto unlock;
2096                 }
2097
2098                 /* Had to read the page from swap area: Major fault */
2099                 ret = VM_FAULT_MAJOR;
2100                 count_vm_event(PGMAJFAULT);
2101                 grab_swap_token();
2102         }
2103
2104         if (!vx_rsspages_avail(mm, 1)) {
2105                 ret = VM_FAULT_OOM;
2106                 goto out;
2107         }
2108
2109         delayacct_clear_flag(DELAYACCT_PF_SWAPIN);
2110         mark_page_accessed(page);
2111         lock_page(page);
2112
2113         /*
2114          * Back out if somebody else already faulted in this pte.
2115          */
2116         page_table = pte_offset_map_lock(mm, pmd, address, &ptl);
2117         if (unlikely(!pte_same(*page_table, orig_pte)))
2118                 goto out_nomap;
2119
2120         if (unlikely(!PageUptodate(page))) {
2121                 ret = VM_FAULT_SIGBUS;
2122                 goto out_nomap;
2123         }
2124
2125         /* The page isn't present yet, go ahead with the fault. */
2126
2127         inc_mm_counter(mm, anon_rss);
2128         pte = mk_pte(page, vma->vm_page_prot);
2129         if (write_access && can_share_swap_page(page)) {
2130                 pte = maybe_mkwrite(pte_mkdirty(pte), vma);
2131                 write_access = 0;
2132         }
2133
2134         flush_icache_page(vma, page);
2135         set_pte_at(mm, address, page_table, pte);
2136         page_add_anon_rmap(page, vma, address);
2137
2138         swap_free(entry);
2139         if (vm_swap_full())
2140                 remove_exclusive_swap_page(page);
2141         unlock_page(page);
2142
2143         if (write_access) {
2144                 if (do_wp_page(mm, vma, address,
2145                                 page_table, pmd, ptl, pte) == VM_FAULT_OOM)
2146                         ret = VM_FAULT_OOM;
2147                 goto out;
2148         }
2149
2150         /* No need to invalidate - it was non-present before */
2151         update_mmu_cache(vma, address, pte);
2152         lazy_mmu_prot_update(pte);
2153 unlock:
2154         pte_unmap_unlock(page_table, ptl);
2155 out:
2156         return ret;
2157 out_nomap:
2158         pte_unmap_unlock(page_table, ptl);
2159         unlock_page(page);
2160         page_cache_release(page);
2161         return ret;
2162 }
2163
2164 /*
2165  * We enter with non-exclusive mmap_sem (to exclude vma changes,
2166  * but allow concurrent faults), and pte mapped but not yet locked.
2167  * We return with mmap_sem still held, but pte unmapped and unlocked.
2168  */
2169 static int do_anonymous_page(struct mm_struct *mm, struct vm_area_struct *vma,
2170                 unsigned long address, pte_t *page_table, pmd_t *pmd,
2171                 int write_access)
2172 {
2173         struct page *page;
2174         spinlock_t *ptl;
2175         pte_t entry;
2176
2177         if (write_access) {
2178                 /* Allocate our own private page. */
2179                 pte_unmap(page_table);
2180
2181                 if (!vx_rsspages_avail(mm, 1))
2182                         goto oom;
2183                 if (unlikely(anon_vma_prepare(vma)))
2184                         goto oom;
2185                 page = alloc_zeroed_user_highpage(vma, address);
2186                 if (!page)
2187                         goto oom;
2188
2189                 entry = mk_pte(page, vma->vm_page_prot);
2190                 entry = maybe_mkwrite(pte_mkdirty(entry), vma);
2191
2192                 page_table = pte_offset_map_lock(mm, pmd, address, &ptl);
2193                 if (!pte_none(*page_table))
2194                         goto release;
2195                 inc_mm_counter(mm, anon_rss);
2196                 lru_cache_add_active(page);
2197                 page_add_new_anon_rmap(page, vma, address);
2198         } else {
2199                 /* Map the ZERO_PAGE - vm_page_prot is readonly */
2200                 page = ZERO_PAGE(address);
2201                 page_cache_get(page);
2202                 entry = mk_pte(page, vma->vm_page_prot);
2203
2204                 ptl = pte_lockptr(mm, pmd);
2205                 spin_lock(ptl);
2206                 if (!pte_none(*page_table))
2207                         goto release;
2208                 inc_mm_counter(mm, file_rss);
2209                 page_add_file_rmap(page);
2210         }
2211
2212         set_pte_at(mm, address, page_table, entry);
2213
2214         /* No need to invalidate - it was non-present before */
2215         update_mmu_cache(vma, address, entry);
2216         lazy_mmu_prot_update(entry);
2217 unlock:
2218         pte_unmap_unlock(page_table, ptl);
2219         return VM_FAULT_MINOR;
2220 release:
2221         page_cache_release(page);
2222         goto unlock;
2223 oom:
2224         return VM_FAULT_OOM;
2225 }
2226
2227 /*
2228  * do_no_page() tries to create a new page mapping. It aggressively
2229  * tries to share with existing pages, but makes a separate copy if
2230  * the "write_access" parameter is true in order to avoid the next
2231  * page fault.
2232  *
2233  * As this is called only for pages that do not currently exist, we
2234  * do not need to flush old virtual caches or the TLB.
2235  *
2236  * We enter with non-exclusive mmap_sem (to exclude vma changes,
2237  * but allow concurrent faults), and pte mapped but not yet locked.
2238  * We return with mmap_sem still held, but pte unmapped and unlocked.
2239  */
2240 static int do_no_page(struct mm_struct *mm, struct vm_area_struct *vma,
2241                 unsigned long address, pte_t *page_table, pmd_t *pmd,
2242                 int write_access)
2243 {
2244         spinlock_t *ptl;
2245         struct page *new_page;
2246         struct address_space *mapping = NULL;
2247         pte_t entry;
2248         unsigned int sequence = 0;
2249         int ret = VM_FAULT_MINOR;
2250         int anon = 0;
2251         struct page *dirty_page = NULL;
2252
2253         pte_unmap(page_table);
2254         BUG_ON(vma->vm_flags & VM_PFNMAP);
2255
2256         if (vma->vm_file) {
2257                 mapping = vma->vm_file->f_mapping;
2258                 sequence = mapping->truncate_count;
2259                 smp_rmb(); /* serializes i_size against truncate_count */
2260         }
2261 retry:
2262         /* FIXME: is that check useful here? */
2263         if (!vx_rsspages_avail(mm, 1))
2264                 return VM_FAULT_OOM;
2265         new_page = vma->vm_ops->nopage(vma, address & PAGE_MASK, &ret);
2266         /*
2267          * No smp_rmb is needed here as long as there's a full
2268          * spin_lock/unlock sequence inside the ->nopage callback
2269          * (for the pagecache lookup) that acts as an implicit
2270          * smp_mb() and prevents the i_size read to happen
2271          * after the next truncate_count read.
2272          */
2273
2274         /* no page was available -- either SIGBUS or OOM */
2275         if (new_page == NOPAGE_SIGBUS)
2276                 return VM_FAULT_SIGBUS;
2277         if (new_page == NOPAGE_OOM)
2278                 return VM_FAULT_OOM;
2279
2280         /*
2281          * Should we do an early C-O-W break?
2282          */
2283         if (write_access) {
2284                 if (!(vma->vm_flags & VM_SHARED)) {
2285                         struct page *page;
2286
2287                         if (unlikely(anon_vma_prepare(vma)))
2288                                 goto oom;
2289                         page = alloc_page_vma(GFP_HIGHUSER, vma, address);
2290                         if (!page)
2291                                 goto oom;
2292                         copy_user_highpage(page, new_page, address);
2293                         page_cache_release(new_page);
2294                         new_page = page;
2295                         anon = 1;
2296
2297                 } else {
2298                         /* if the page will be shareable, see if the backing
2299                          * address space wants to know that the page is about
2300                          * to become writable */
2301                         if (vma->vm_ops->page_mkwrite &&
2302                             vma->vm_ops->page_mkwrite(vma, new_page) < 0
2303                             ) {
2304                                 page_cache_release(new_page);
2305                                 return VM_FAULT_SIGBUS;
2306                         }
2307                 }
2308         }
2309
2310         page_table = pte_offset_map_lock(mm, pmd, address, &ptl);
2311         /*
2312          * For a file-backed vma, someone could have truncated or otherwise
2313          * invalidated this page.  If unmap_mapping_range got called,
2314          * retry getting the page.
2315          */
2316         if (mapping && unlikely(sequence != mapping->truncate_count)) {
2317                 pte_unmap_unlock(page_table, ptl);
2318                 page_cache_release(new_page);
2319                 cond_resched();
2320                 sequence = mapping->truncate_count;
2321                 smp_rmb();
2322                 goto retry;
2323         }
2324
2325         /*
2326          * This silly early PAGE_DIRTY setting removes a race
2327          * due to the bad i386 page protection. But it's valid
2328          * for other architectures too.
2329          *
2330          * Note that if write_access is true, we either now have
2331          * an exclusive copy of the page, or this is a shared mapping,
2332          * so we can make it writable and dirty to avoid having to
2333          * handle that later.
2334          */
2335         /* Only go through if we didn't race with anybody else... */
2336         if (pte_none(*page_table)) {
2337                 flush_icache_page(vma, new_page);
2338                 entry = mk_pte(new_page, vma->vm_page_prot);
2339                 if (write_access)
2340                         entry = maybe_mkwrite(pte_mkdirty(entry), vma);
2341                 set_pte_at(mm, address, page_table, entry);
2342                 if (anon) {
2343                         inc_mm_counter(mm, anon_rss);
2344                         lru_cache_add_active(new_page);
2345                         page_add_new_anon_rmap(new_page, vma, address);
2346                 } else {
2347                         inc_mm_counter(mm, file_rss);
2348                         page_add_file_rmap(new_page);
2349                         if (write_access) {
2350                                 dirty_page = new_page;
2351                                 get_page(dirty_page);
2352                         }
2353                 }
2354         } else {
2355                 /* One of our sibling threads was faster, back out. */
2356                 page_cache_release(new_page);
2357                 goto unlock;
2358         }
2359
2360         /* no need to invalidate: a not-present page shouldn't be cached */
2361         update_mmu_cache(vma, address, entry);
2362         lazy_mmu_prot_update(entry);
2363 unlock:
2364         pte_unmap_unlock(page_table, ptl);
2365         if (dirty_page) {
2366                 set_page_dirty_balance(dirty_page);
2367                 put_page(dirty_page);
2368         }
2369         return ret;
2370 oom:
2371         page_cache_release(new_page);
2372         return VM_FAULT_OOM;
2373 }
2374
2375 /*
2376  * Fault of a previously existing named mapping. Repopulate the pte
2377  * from the encoded file_pte if possible. This enables swappable
2378  * nonlinear vmas.
2379  *
2380  * We enter with non-exclusive mmap_sem (to exclude vma changes,
2381  * but allow concurrent faults), and pte mapped but not yet locked.
2382  * We return with mmap_sem still held, but pte unmapped and unlocked.
2383  */
2384 static int do_file_page(struct mm_struct *mm, struct vm_area_struct *vma,
2385                 unsigned long address, pte_t *page_table, pmd_t *pmd,
2386                 int write_access, pte_t orig_pte)
2387 {
2388         pgoff_t pgoff;
2389         int err;
2390
2391         if (!pte_unmap_same(mm, pmd, page_table, orig_pte))
2392                 return VM_FAULT_MINOR;
2393
2394         if (unlikely(!(vma->vm_flags & VM_NONLINEAR))) {
2395                 /*
2396                  * Page table corrupted: show pte and kill process.
2397                  */
2398                 print_bad_pte(vma, orig_pte, address);
2399                 return VM_FAULT_OOM;
2400         }
2401         /* We can then assume vm->vm_ops && vma->vm_ops->populate */
2402
2403         pgoff = pte_to_pgoff(orig_pte);
2404         err = vma->vm_ops->populate(vma, address & PAGE_MASK, PAGE_SIZE,
2405                                         vma->vm_page_prot, pgoff, 0);
2406         if (err == -ENOMEM)
2407                 return VM_FAULT_OOM;
2408         if (err)
2409                 return VM_FAULT_SIGBUS;
2410         return VM_FAULT_MAJOR;
2411 }
2412
2413 /*
2414  * These routines also need to handle stuff like marking pages dirty
2415  * and/or accessed for architectures that don't do it in hardware (most
2416  * RISC architectures).  The early dirtying is also good on the i386.
2417  *
2418  * There is also a hook called "update_mmu_cache()" that architectures
2419  * with external mmu caches can use to update those (ie the Sparc or
2420  * PowerPC hashed page tables that act as extended TLBs).
2421  *
2422  * We enter with non-exclusive mmap_sem (to exclude vma changes,
2423  * but allow concurrent faults), and pte mapped but not yet locked.
2424  * We return with mmap_sem still held, but pte unmapped and unlocked.
2425  */
2426 static inline int handle_pte_fault(struct mm_struct *mm,
2427                 struct vm_area_struct *vma, unsigned long address,
2428                 pte_t *pte, pmd_t *pmd, int write_access)
2429 {
2430         pte_t entry;
2431         pte_t old_entry;
2432         spinlock_t *ptl;
2433
2434         old_entry = entry = *pte;
2435         if (!pte_present(entry)) {
2436                 if (pte_none(entry)) {
2437                         if (!vma->vm_ops || !vma->vm_ops->nopage)
2438                                 return do_anonymous_page(mm, vma, address,
2439                                         pte, pmd, write_access);
2440                         return do_no_page(mm, vma, address,
2441                                         pte, pmd, write_access);
2442                 }
2443                 if (pte_file(entry))
2444                         return do_file_page(mm, vma, address,
2445                                         pte, pmd, write_access, entry);
2446                 return do_swap_page(mm, vma, address,
2447                                         pte, pmd, write_access, entry);
2448         }
2449
2450         ptl = pte_lockptr(mm, pmd);
2451         spin_lock(ptl);
2452         if (unlikely(!pte_same(*pte, entry)))
2453                 goto unlock;
2454         if (write_access) {
2455                 if (!pte_write(entry))
2456                         return do_wp_page(mm, vma, address,
2457                                         pte, pmd, ptl, entry);
2458                 entry = pte_mkdirty(entry);
2459         }
2460         entry = pte_mkyoung(entry);
2461         if (!pte_same(old_entry, entry)) {
2462                 ptep_set_access_flags(vma, address, pte, entry, write_access);
2463                 update_mmu_cache(vma, address, entry);
2464                 lazy_mmu_prot_update(entry);
2465         } else {
2466                 /*
2467                  * This is needed only for protection faults but the arch code
2468                  * is not yet telling us if this is a protection fault or not.
2469                  * This still avoids useless tlb flushes for .text page faults
2470                  * with threads.
2471                  */
2472                 if (write_access)
2473                         flush_tlb_page(vma, address);
2474         }
2475 unlock:
2476         pte_unmap_unlock(pte, ptl);
2477         return VM_FAULT_MINOR;
2478 }
2479
2480 /*
2481  * By the time we get here, we already hold the mm semaphore
2482  */
2483 int __handle_mm_fault(struct mm_struct *mm, struct vm_area_struct *vma,
2484                 unsigned long address, int write_access)
2485 {
2486         pgd_t *pgd;
2487         pud_t *pud;
2488         pmd_t *pmd;
2489         pte_t *pte;
2490
2491         __set_current_state(TASK_RUNNING);
2492
2493         count_vm_event(PGFAULT);
2494
2495         if (unlikely(is_vm_hugetlb_page(vma)))
2496                 return hugetlb_fault(mm, vma, address, write_access);
2497
2498         pgd = pgd_offset(mm, address);
2499         pud = pud_alloc(mm, pgd, address);
2500         if (!pud)
2501                 return VM_FAULT_OOM;
2502         pmd = pmd_alloc(mm, pud, address);
2503         if (!pmd)
2504                 return VM_FAULT_OOM;
2505         pte = pte_alloc_map(mm, pmd, address);
2506         if (!pte)
2507                 return VM_FAULT_OOM;
2508
2509         return handle_pte_fault(mm, vma, address, pte, pmd, write_access);
2510 }
2511
2512 EXPORT_SYMBOL_GPL(__handle_mm_fault);
2513
2514 #ifndef __PAGETABLE_PUD_FOLDED
2515 /*
2516  * Allocate page upper directory.
2517  * We've already handled the fast-path in-line.
2518  */
2519 int __pud_alloc(struct mm_struct *mm, pgd_t *pgd, unsigned long address)
2520 {
2521         pud_t *new = pud_alloc_one(mm, address);
2522         if (!new)
2523                 return -ENOMEM;
2524
2525         spin_lock(&mm->page_table_lock);
2526         if (pgd_present(*pgd))          /* Another has populated it */
2527                 pud_free(new);
2528         else
2529                 pgd_populate(mm, pgd, new);
2530         spin_unlock(&mm->page_table_lock);
2531         return 0;
2532 }
2533 #else
2534 /* Workaround for gcc 2.96 */
2535 int __pud_alloc(struct mm_struct *mm, pgd_t *pgd, unsigned long address)
2536 {
2537         return 0;
2538 }
2539 #endif /* __PAGETABLE_PUD_FOLDED */
2540
2541 #ifndef __PAGETABLE_PMD_FOLDED
2542 /*
2543  * Allocate page middle directory.
2544  * We've already handled the fast-path in-line.
2545  */
2546 int __pmd_alloc(struct mm_struct *mm, pud_t *pud, unsigned long address)
2547 {
2548         pmd_t *new = pmd_alloc_one(mm, address);
2549         if (!new)
2550                 return -ENOMEM;
2551
2552         spin_lock(&mm->page_table_lock);
2553 #ifndef __ARCH_HAS_4LEVEL_HACK
2554         if (pud_present(*pud))          /* Another has populated it */
2555                 pmd_free(new);
2556         else
2557                 pud_populate(mm, pud, new);
2558 #else
2559         if (pgd_present(*pud))          /* Another has populated it */
2560                 pmd_free(new);
2561         else
2562                 pgd_populate(mm, pud, new);
2563 #endif /* __ARCH_HAS_4LEVEL_HACK */
2564         spin_unlock(&mm->page_table_lock);
2565         return 0;
2566 }
2567 #else
2568 /* Workaround for gcc 2.96 */
2569 int __pmd_alloc(struct mm_struct *mm, pud_t *pud, unsigned long address)
2570 {
2571         return 0;
2572 }
2573 #endif /* __PAGETABLE_PMD_FOLDED */
2574
2575 int make_pages_present(unsigned long addr, unsigned long end)
2576 {
2577         int ret, len, write;
2578         struct vm_area_struct * vma;
2579
2580         vma = find_vma(current->mm, addr);
2581         if (!vma)
2582                 return -1;
2583         write = (vma->vm_flags & VM_WRITE) != 0;
2584         BUG_ON(addr >= end);
2585         BUG_ON(end > vma->vm_end);
2586         len = (end+PAGE_SIZE-1)/PAGE_SIZE-addr/PAGE_SIZE;
2587         ret = get_user_pages(current, current->mm, addr,
2588                         len, write, 0, NULL, NULL);
2589         if (ret < 0)
2590                 return ret;
2591         return ret == len ? 0 : -1;
2592 }
2593
2594 /* 
2595  * Map a vmalloc()-space virtual address to the physical page.
2596  */
2597 struct page * vmalloc_to_page(void * vmalloc_addr)
2598 {
2599         unsigned long addr = (unsigned long) vmalloc_addr;
2600         struct page *page = NULL;
2601         pgd_t *pgd = pgd_offset_k(addr);
2602         pud_t *pud;
2603         pmd_t *pmd;
2604         pte_t *ptep, pte;
2605   
2606         if (!pgd_none(*pgd)) {
2607                 pud = pud_offset(pgd, addr);
2608                 if (!pud_none(*pud)) {
2609                         pmd = pmd_offset(pud, addr);
2610                         if (!pmd_none(*pmd)) {
2611                                 ptep = pte_offset_map(pmd, addr);
2612                                 pte = *ptep;
2613                                 if (pte_present(pte))
2614                                         page = pte_page(pte);
2615                                 pte_unmap(ptep);
2616                         }
2617                 }
2618         }
2619         return page;
2620 }
2621
2622 EXPORT_SYMBOL(vmalloc_to_page);
2623
2624 /*
2625  * Map a vmalloc()-space virtual address to the physical page frame number.
2626  */
2627 unsigned long vmalloc_to_pfn(void * vmalloc_addr)
2628 {
2629         return page_to_pfn(vmalloc_to_page(vmalloc_addr));
2630 }
2631
2632 EXPORT_SYMBOL(vmalloc_to_pfn);
2633
2634 #if !defined(__HAVE_ARCH_GATE_AREA)
2635
2636 #if defined(AT_SYSINFO_EHDR)
2637 static struct vm_area_struct gate_vma;
2638
2639 static int __init gate_vma_init(void)
2640 {
2641         gate_vma.vm_mm = NULL;
2642         gate_vma.vm_start = FIXADDR_USER_START;
2643         gate_vma.vm_end = FIXADDR_USER_END;
2644         gate_vma.vm_page_prot = PAGE_READONLY;
2645         gate_vma.vm_flags = 0;
2646         return 0;
2647 }
2648 __initcall(gate_vma_init);
2649 #endif
2650
2651 struct vm_area_struct *get_gate_vma(struct task_struct *tsk)
2652 {
2653 #ifdef AT_SYSINFO_EHDR
2654         return &gate_vma;
2655 #else
2656         return NULL;
2657 #endif
2658 }
2659
2660 int in_gate_area_no_task(unsigned long addr)
2661 {
2662 #ifdef AT_SYSINFO_EHDR
2663         if ((addr >= FIXADDR_USER_START) && (addr < FIXADDR_USER_END))
2664                 return 1;
2665 #endif
2666         return 0;
2667 }
2668
2669 #endif  /* __HAVE_ARCH_GATE_AREA */