direct-io.hg

view linux-2.6-xen-sparse/mm/memory.c @ 10323:a936c9c3ea60

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