ia64/linux-2.6.18-xen.hg

view mm/memory.c @ 749:2892ca2b9c17

linux/x86: cleanup IO-APIC code

- get 32-bit code in sync with 64-bit wrt ExtINT pin detection being
unnecessary
- eliminate build warnings resulting from c/s 725

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