ia64/xen-unstable

view linux-2.6-xen-sparse/mm/memory.c @ 13197:a9a43705f26b

Fix HVM booting through Xen-API when the kernel is unspecified.

Signed-off-by: Ewan Mellor <ewan@xensource.com>
author Ewan Mellor <ewan@xensource.com>
date Wed Dec 27 00:38:01 2006 +0000 (2006-12-27)
parents 260426e3924f
children 4fad820a2233
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 }
885 EXPORT_SYMBOL(zap_page_range);
887 /*
888 * Do a quick page-table lookup for a single page.
889 */
890 struct page *follow_page(struct vm_area_struct *vma, unsigned long address,
891 unsigned int flags)
892 {
893 pgd_t *pgd;
894 pud_t *pud;
895 pmd_t *pmd;
896 pte_t *ptep, pte;
897 spinlock_t *ptl;
898 struct page *page;
899 struct mm_struct *mm = vma->vm_mm;
901 page = follow_huge_addr(mm, address, flags & FOLL_WRITE);
902 if (!IS_ERR(page)) {
903 BUG_ON(flags & FOLL_GET);
904 goto out;
905 }
907 page = NULL;
908 pgd = pgd_offset(mm, address);
909 if (pgd_none(*pgd) || unlikely(pgd_bad(*pgd)))
910 goto no_page_table;
912 pud = pud_offset(pgd, address);
913 if (pud_none(*pud) || unlikely(pud_bad(*pud)))
914 goto no_page_table;
916 pmd = pmd_offset(pud, address);
917 if (pmd_none(*pmd) || unlikely(pmd_bad(*pmd)))
918 goto no_page_table;
920 if (pmd_huge(*pmd)) {
921 BUG_ON(flags & FOLL_GET);
922 page = follow_huge_pmd(mm, address, pmd, flags & FOLL_WRITE);
923 goto out;
924 }
926 ptep = pte_offset_map_lock(mm, pmd, address, &ptl);
927 if (!ptep)
928 goto out;
930 pte = *ptep;
931 if (!pte_present(pte))
932 goto unlock;
933 if ((flags & FOLL_WRITE) && !pte_write(pte))
934 goto unlock;
935 page = vm_normal_page(vma, address, pte);
936 if (unlikely(!page))
937 goto unlock;
939 if (flags & FOLL_GET)
940 get_page(page);
941 if (flags & FOLL_TOUCH) {
942 if ((flags & FOLL_WRITE) &&
943 !pte_dirty(pte) && !PageDirty(page))
944 set_page_dirty(page);
945 mark_page_accessed(page);
946 }
947 unlock:
948 pte_unmap_unlock(ptep, ptl);
949 out:
950 return page;
952 no_page_table:
953 /*
954 * When core dumping an enormous anonymous area that nobody
955 * has touched so far, we don't want to allocate page tables.
956 */
957 if (flags & FOLL_ANON) {
958 page = ZERO_PAGE(address);
959 if (flags & FOLL_GET)
960 get_page(page);
961 BUG_ON(flags & FOLL_WRITE);
962 }
963 return page;
964 }
966 int get_user_pages(struct task_struct *tsk, struct mm_struct *mm,
967 unsigned long start, int len, int write, int force,
968 struct page **pages, struct vm_area_struct **vmas)
969 {
970 int i;
971 unsigned int vm_flags;
973 /*
974 * Require read or write permissions.
975 * If 'force' is set, we only require the "MAY" flags.
976 */
977 vm_flags = write ? (VM_WRITE | VM_MAYWRITE) : (VM_READ | VM_MAYREAD);
978 vm_flags &= force ? (VM_MAYREAD | VM_MAYWRITE) : (VM_READ | VM_WRITE);
979 i = 0;
981 do {
982 struct vm_area_struct *vma;
983 unsigned int foll_flags;
985 vma = find_extend_vma(mm, start);
986 if (!vma && in_gate_area(tsk, start)) {
987 unsigned long pg = start & PAGE_MASK;
988 struct vm_area_struct *gate_vma = get_gate_vma(tsk);
989 pgd_t *pgd;
990 pud_t *pud;
991 pmd_t *pmd;
992 pte_t *pte;
993 if (write) /* user gate pages are read-only */
994 return i ? : -EFAULT;
995 if (pg > TASK_SIZE)
996 pgd = pgd_offset_k(pg);
997 else
998 pgd = pgd_offset_gate(mm, pg);
999 BUG_ON(pgd_none(*pgd));
1000 pud = pud_offset(pgd, pg);
1001 BUG_ON(pud_none(*pud));
1002 pmd = pmd_offset(pud, pg);
1003 if (pmd_none(*pmd))
1004 return i ? : -EFAULT;
1005 pte = pte_offset_map(pmd, pg);
1006 if (pte_none(*pte)) {
1007 pte_unmap(pte);
1008 return i ? : -EFAULT;
1010 if (pages) {
1011 struct page *page = vm_normal_page(gate_vma, start, *pte);
1012 pages[i] = page;
1013 if (page)
1014 get_page(page);
1016 pte_unmap(pte);
1017 if (vmas)
1018 vmas[i] = gate_vma;
1019 i++;
1020 start += PAGE_SIZE;
1021 len--;
1022 continue;
1025 #ifdef CONFIG_XEN
1026 if (vma && (vma->vm_flags & VM_FOREIGN)) {
1027 struct page **map = vma->vm_private_data;
1028 int offset = (start - vma->vm_start) >> PAGE_SHIFT;
1029 if (map[offset] != NULL) {
1030 if (pages) {
1031 struct page *page = map[offset];
1033 pages[i] = page;
1034 get_page(page);
1036 if (vmas)
1037 vmas[i] = vma;
1038 i++;
1039 start += PAGE_SIZE;
1040 len--;
1041 continue;
1044 #endif
1045 if (!vma || (vma->vm_flags & (VM_IO | VM_PFNMAP))
1046 || !(vm_flags & vma->vm_flags))
1047 return i ? : -EFAULT;
1049 if (is_vm_hugetlb_page(vma)) {
1050 i = follow_hugetlb_page(mm, vma, pages, vmas,
1051 &start, &len, i);
1052 continue;
1055 foll_flags = FOLL_TOUCH;
1056 if (pages)
1057 foll_flags |= FOLL_GET;
1058 if (!write && !(vma->vm_flags & VM_LOCKED) &&
1059 (!vma->vm_ops || !vma->vm_ops->nopage))
1060 foll_flags |= FOLL_ANON;
1062 do {
1063 struct page *page;
1065 if (write)
1066 foll_flags |= FOLL_WRITE;
1068 cond_resched();
1069 while (!(page = follow_page(vma, start, foll_flags))) {
1070 int ret;
1071 ret = __handle_mm_fault(mm, vma, start,
1072 foll_flags & FOLL_WRITE);
1073 /*
1074 * The VM_FAULT_WRITE bit tells us that do_wp_page has
1075 * broken COW when necessary, even if maybe_mkwrite
1076 * decided not to set pte_write. We can thus safely do
1077 * subsequent page lookups as if they were reads.
1078 */
1079 if (ret & VM_FAULT_WRITE)
1080 foll_flags &= ~FOLL_WRITE;
1082 switch (ret & ~VM_FAULT_WRITE) {
1083 case VM_FAULT_MINOR:
1084 tsk->min_flt++;
1085 break;
1086 case VM_FAULT_MAJOR:
1087 tsk->maj_flt++;
1088 break;
1089 case VM_FAULT_SIGBUS:
1090 return i ? i : -EFAULT;
1091 case VM_FAULT_OOM:
1092 return i ? i : -ENOMEM;
1093 default:
1094 BUG();
1097 if (pages) {
1098 pages[i] = page;
1099 flush_dcache_page(page);
1101 if (vmas)
1102 vmas[i] = vma;
1103 i++;
1104 start += PAGE_SIZE;
1105 len--;
1106 } while (len && start < vma->vm_end);
1107 } while (len);
1108 return i;
1110 EXPORT_SYMBOL(get_user_pages);
1112 static int zeromap_pte_range(struct mm_struct *mm, pmd_t *pmd,
1113 unsigned long addr, unsigned long end, pgprot_t prot)
1115 pte_t *pte;
1116 spinlock_t *ptl;
1118 pte = pte_alloc_map_lock(mm, pmd, addr, &ptl);
1119 if (!pte)
1120 return -ENOMEM;
1121 do {
1122 struct page *page = ZERO_PAGE(addr);
1123 pte_t zero_pte = pte_wrprotect(mk_pte(page, prot));
1124 page_cache_get(page);
1125 page_add_file_rmap(page);
1126 inc_mm_counter(mm, file_rss);
1127 BUG_ON(!pte_none(*pte));
1128 set_pte_at(mm, addr, pte, zero_pte);
1129 } while (pte++, addr += PAGE_SIZE, addr != end);
1130 pte_unmap_unlock(pte - 1, ptl);
1131 return 0;
1134 static inline int zeromap_pmd_range(struct mm_struct *mm, pud_t *pud,
1135 unsigned long addr, unsigned long end, pgprot_t prot)
1137 pmd_t *pmd;
1138 unsigned long next;
1140 pmd = pmd_alloc(mm, pud, addr);
1141 if (!pmd)
1142 return -ENOMEM;
1143 do {
1144 next = pmd_addr_end(addr, end);
1145 if (zeromap_pte_range(mm, pmd, addr, next, prot))
1146 return -ENOMEM;
1147 } while (pmd++, addr = next, addr != end);
1148 return 0;
1151 static inline int zeromap_pud_range(struct mm_struct *mm, pgd_t *pgd,
1152 unsigned long addr, unsigned long end, pgprot_t prot)
1154 pud_t *pud;
1155 unsigned long next;
1157 pud = pud_alloc(mm, pgd, addr);
1158 if (!pud)
1159 return -ENOMEM;
1160 do {
1161 next = pud_addr_end(addr, end);
1162 if (zeromap_pmd_range(mm, pud, addr, next, prot))
1163 return -ENOMEM;
1164 } while (pud++, addr = next, addr != end);
1165 return 0;
1168 int zeromap_page_range(struct vm_area_struct *vma,
1169 unsigned long addr, unsigned long size, pgprot_t prot)
1171 pgd_t *pgd;
1172 unsigned long next;
1173 unsigned long end = addr + size;
1174 struct mm_struct *mm = vma->vm_mm;
1175 int err;
1177 BUG_ON(addr >= end);
1178 pgd = pgd_offset(mm, addr);
1179 flush_cache_range(vma, addr, end);
1180 do {
1181 next = pgd_addr_end(addr, end);
1182 err = zeromap_pud_range(mm, pgd, addr, next, prot);
1183 if (err)
1184 break;
1185 } while (pgd++, addr = next, addr != end);
1186 return err;
1189 pte_t * fastcall get_locked_pte(struct mm_struct *mm, unsigned long addr, spinlock_t **ptl)
1191 pgd_t * pgd = pgd_offset(mm, addr);
1192 pud_t * pud = pud_alloc(mm, pgd, addr);
1193 if (pud) {
1194 pmd_t * pmd = pmd_alloc(mm, pud, addr);
1195 if (pmd)
1196 return pte_alloc_map_lock(mm, pmd, addr, ptl);
1198 return NULL;
1201 /*
1202 * This is the old fallback for page remapping.
1204 * For historical reasons, it only allows reserved pages. Only
1205 * old drivers should use this, and they needed to mark their
1206 * pages reserved for the old functions anyway.
1207 */
1208 static int insert_page(struct mm_struct *mm, unsigned long addr, struct page *page, pgprot_t prot)
1210 int retval;
1211 pte_t *pte;
1212 spinlock_t *ptl;
1214 retval = -EINVAL;
1215 if (PageAnon(page))
1216 goto out;
1217 retval = -ENOMEM;
1218 flush_dcache_page(page);
1219 pte = get_locked_pte(mm, addr, &ptl);
1220 if (!pte)
1221 goto out;
1222 retval = -EBUSY;
1223 if (!pte_none(*pte))
1224 goto out_unlock;
1226 /* Ok, finally just insert the thing.. */
1227 get_page(page);
1228 inc_mm_counter(mm, file_rss);
1229 page_add_file_rmap(page);
1230 set_pte_at(mm, addr, pte, mk_pte(page, prot));
1232 retval = 0;
1233 out_unlock:
1234 pte_unmap_unlock(pte, ptl);
1235 out:
1236 return retval;
1239 /*
1240 * This allows drivers to insert individual pages they've allocated
1241 * into a user vma.
1243 * The page has to be a nice clean _individual_ kernel allocation.
1244 * If you allocate a compound page, you need to have marked it as
1245 * such (__GFP_COMP), or manually just split the page up yourself
1246 * (which is mainly an issue of doing "set_page_count(page, 1)" for
1247 * each sub-page, and then freeing them one by one when you free
1248 * them rather than freeing it as a compound page).
1250 * NOTE! Traditionally this was done with "remap_pfn_range()" which
1251 * took an arbitrary page protection parameter. This doesn't allow
1252 * that. Your vma protection will have to be set up correctly, which
1253 * means that if you want a shared writable mapping, you'd better
1254 * ask for a shared writable mapping!
1256 * The page does not need to be reserved.
1257 */
1258 int vm_insert_page(struct vm_area_struct *vma, unsigned long addr, struct page *page)
1260 if (addr < vma->vm_start || addr >= vma->vm_end)
1261 return -EFAULT;
1262 if (!page_count(page))
1263 return -EINVAL;
1264 vma->vm_flags |= VM_INSERTPAGE;
1265 return insert_page(vma->vm_mm, addr, page, vma->vm_page_prot);
1267 EXPORT_SYMBOL(vm_insert_page);
1269 /*
1270 * maps a range of physical memory into the requested pages. the old
1271 * mappings are removed. any references to nonexistent pages results
1272 * in null mappings (currently treated as "copy-on-access")
1273 */
1274 static int remap_pte_range(struct mm_struct *mm, pmd_t *pmd,
1275 unsigned long addr, unsigned long end,
1276 unsigned long pfn, pgprot_t prot)
1278 pte_t *pte;
1279 spinlock_t *ptl;
1281 pte = pte_alloc_map_lock(mm, pmd, addr, &ptl);
1282 if (!pte)
1283 return -ENOMEM;
1284 do {
1285 BUG_ON(!pte_none(*pte));
1286 set_pte_at(mm, addr, pte, pfn_pte(pfn, prot));
1287 pfn++;
1288 } while (pte++, addr += PAGE_SIZE, addr != end);
1289 pte_unmap_unlock(pte - 1, ptl);
1290 return 0;
1293 static inline int remap_pmd_range(struct mm_struct *mm, pud_t *pud,
1294 unsigned long addr, unsigned long end,
1295 unsigned long pfn, pgprot_t prot)
1297 pmd_t *pmd;
1298 unsigned long next;
1300 pfn -= addr >> PAGE_SHIFT;
1301 pmd = pmd_alloc(mm, pud, addr);
1302 if (!pmd)
1303 return -ENOMEM;
1304 do {
1305 next = pmd_addr_end(addr, end);
1306 if (remap_pte_range(mm, pmd, addr, next,
1307 pfn + (addr >> PAGE_SHIFT), prot))
1308 return -ENOMEM;
1309 } while (pmd++, addr = next, addr != end);
1310 return 0;
1313 static inline int remap_pud_range(struct mm_struct *mm, pgd_t *pgd,
1314 unsigned long addr, unsigned long end,
1315 unsigned long pfn, pgprot_t prot)
1317 pud_t *pud;
1318 unsigned long next;
1320 pfn -= addr >> PAGE_SHIFT;
1321 pud = pud_alloc(mm, pgd, addr);
1322 if (!pud)
1323 return -ENOMEM;
1324 do {
1325 next = pud_addr_end(addr, end);
1326 if (remap_pmd_range(mm, pud, addr, next,
1327 pfn + (addr >> PAGE_SHIFT), prot))
1328 return -ENOMEM;
1329 } while (pud++, addr = next, addr != end);
1330 return 0;
1333 /* Note: this is only safe if the mm semaphore is held when called. */
1334 int remap_pfn_range(struct vm_area_struct *vma, unsigned long addr,
1335 unsigned long pfn, unsigned long size, pgprot_t prot)
1337 pgd_t *pgd;
1338 unsigned long next;
1339 unsigned long end = addr + PAGE_ALIGN(size);
1340 struct mm_struct *mm = vma->vm_mm;
1341 int err;
1343 /*
1344 * Physically remapped pages are special. Tell the
1345 * rest of the world about it:
1346 * VM_IO tells people not to look at these pages
1347 * (accesses can have side effects).
1348 * VM_RESERVED is specified all over the place, because
1349 * in 2.4 it kept swapout's vma scan off this vma; but
1350 * in 2.6 the LRU scan won't even find its pages, so this
1351 * flag means no more than count its pages in reserved_vm,
1352 * and omit it from core dump, even when VM_IO turned off.
1353 * VM_PFNMAP tells the core MM that the base pages are just
1354 * raw PFN mappings, and do not have a "struct page" associated
1355 * with them.
1357 * There's a horrible special case to handle copy-on-write
1358 * behaviour that some programs depend on. We mark the "original"
1359 * un-COW'ed pages by matching them up with "vma->vm_pgoff".
1360 */
1361 if (is_cow_mapping(vma->vm_flags)) {
1362 if (addr != vma->vm_start || end != vma->vm_end)
1363 return -EINVAL;
1364 vma->vm_pgoff = pfn;
1367 vma->vm_flags |= VM_IO | VM_RESERVED | VM_PFNMAP;
1369 BUG_ON(addr >= end);
1370 pfn -= addr >> PAGE_SHIFT;
1371 pgd = pgd_offset(mm, addr);
1372 flush_cache_range(vma, addr, end);
1373 do {
1374 next = pgd_addr_end(addr, end);
1375 err = remap_pud_range(mm, pgd, addr, next,
1376 pfn + (addr >> PAGE_SHIFT), prot);
1377 if (err)
1378 break;
1379 } while (pgd++, addr = next, addr != end);
1380 return err;
1382 EXPORT_SYMBOL(remap_pfn_range);
1384 #ifdef CONFIG_XEN
1385 static inline int apply_to_pte_range(struct mm_struct *mm, pmd_t *pmd,
1386 unsigned long addr, unsigned long end,
1387 pte_fn_t fn, void *data)
1389 pte_t *pte;
1390 int err;
1391 struct page *pmd_page;
1392 spinlock_t *ptl;
1394 pte = (mm == &init_mm) ?
1395 pte_alloc_kernel(pmd, addr) :
1396 pte_alloc_map_lock(mm, pmd, addr, &ptl);
1397 if (!pte)
1398 return -ENOMEM;
1400 BUG_ON(pmd_huge(*pmd));
1402 pmd_page = pmd_page(*pmd);
1404 do {
1405 err = fn(pte, pmd_page, addr, data);
1406 if (err)
1407 break;
1408 } while (pte++, addr += PAGE_SIZE, addr != end);
1410 if (mm != &init_mm)
1411 pte_unmap_unlock(pte-1, ptl);
1412 return err;
1415 static inline int apply_to_pmd_range(struct mm_struct *mm, pud_t *pud,
1416 unsigned long addr, unsigned long end,
1417 pte_fn_t fn, void *data)
1419 pmd_t *pmd;
1420 unsigned long next;
1421 int err;
1423 pmd = pmd_alloc(mm, pud, addr);
1424 if (!pmd)
1425 return -ENOMEM;
1426 do {
1427 next = pmd_addr_end(addr, end);
1428 err = apply_to_pte_range(mm, pmd, addr, next, fn, data);
1429 if (err)
1430 break;
1431 } while (pmd++, addr = next, addr != end);
1432 return err;
1435 static inline int apply_to_pud_range(struct mm_struct *mm, pgd_t *pgd,
1436 unsigned long addr, unsigned long end,
1437 pte_fn_t fn, void *data)
1439 pud_t *pud;
1440 unsigned long next;
1441 int err;
1443 pud = pud_alloc(mm, pgd, addr);
1444 if (!pud)
1445 return -ENOMEM;
1446 do {
1447 next = pud_addr_end(addr, end);
1448 err = apply_to_pmd_range(mm, pud, addr, next, fn, data);
1449 if (err)
1450 break;
1451 } while (pud++, addr = next, addr != end);
1452 return err;
1455 /*
1456 * Scan a region of virtual memory, filling in page tables as necessary
1457 * and calling a provided function on each leaf page table.
1458 */
1459 int apply_to_page_range(struct mm_struct *mm, unsigned long addr,
1460 unsigned long size, pte_fn_t fn, void *data)
1462 pgd_t *pgd;
1463 unsigned long next;
1464 unsigned long end = addr + size;
1465 int err;
1467 BUG_ON(addr >= end);
1468 pgd = pgd_offset(mm, addr);
1469 do {
1470 next = pgd_addr_end(addr, end);
1471 err = apply_to_pud_range(mm, pgd, addr, next, fn, data);
1472 if (err)
1473 break;
1474 } while (pgd++, addr = next, addr != end);
1475 return err;
1477 EXPORT_SYMBOL_GPL(apply_to_page_range);
1478 #endif
1480 /*
1481 * handle_pte_fault chooses page fault handler according to an entry
1482 * which was read non-atomically. Before making any commitment, on
1483 * those architectures or configurations (e.g. i386 with PAE) which
1484 * might give a mix of unmatched parts, do_swap_page and do_file_page
1485 * must check under lock before unmapping the pte and proceeding
1486 * (but do_wp_page is only called after already making such a check;
1487 * and do_anonymous_page and do_no_page can safely check later on).
1488 */
1489 static inline int pte_unmap_same(struct mm_struct *mm, pmd_t *pmd,
1490 pte_t *page_table, pte_t orig_pte)
1492 int same = 1;
1493 #if defined(CONFIG_SMP) || defined(CONFIG_PREEMPT)
1494 if (sizeof(pte_t) > sizeof(unsigned long)) {
1495 spinlock_t *ptl = pte_lockptr(mm, pmd);
1496 spin_lock(ptl);
1497 same = pte_same(*page_table, orig_pte);
1498 spin_unlock(ptl);
1500 #endif
1501 pte_unmap(page_table);
1502 return same;
1505 /*
1506 * Do pte_mkwrite, but only if the vma says VM_WRITE. We do this when
1507 * servicing faults for write access. In the normal case, do always want
1508 * pte_mkwrite. But get_user_pages can cause write faults for mappings
1509 * that do not have writing enabled, when used by access_process_vm.
1510 */
1511 static inline pte_t maybe_mkwrite(pte_t pte, struct vm_area_struct *vma)
1513 if (likely(vma->vm_flags & VM_WRITE))
1514 pte = pte_mkwrite(pte);
1515 return pte;
1518 static inline void cow_user_page(struct page *dst, struct page *src, unsigned long va)
1520 /*
1521 * If the source page was a PFN mapping, we don't have
1522 * a "struct page" for it. We do a best-effort copy by
1523 * just copying from the original user address. If that
1524 * fails, we just zero-fill it. Live with it.
1525 */
1526 if (unlikely(!src)) {
1527 void *kaddr = kmap_atomic(dst, KM_USER0);
1528 void __user *uaddr = (void __user *)(va & PAGE_MASK);
1530 /*
1531 * This really shouldn't fail, because the page is there
1532 * in the page tables. But it might just be unreadable,
1533 * in which case we just give up and fill the result with
1534 * zeroes.
1535 */
1536 if (__copy_from_user_inatomic(kaddr, uaddr, PAGE_SIZE))
1537 memset(kaddr, 0, PAGE_SIZE);
1538 kunmap_atomic(kaddr, KM_USER0);
1539 flush_dcache_page(dst);
1540 return;
1543 copy_user_highpage(dst, src, va);
1546 /*
1547 * This routine handles present pages, when users try to write
1548 * to a shared page. It is done by copying the page to a new address
1549 * and decrementing the shared-page counter for the old page.
1551 * Note that this routine assumes that the protection checks have been
1552 * done by the caller (the low-level page fault routine in most cases).
1553 * Thus we can safely just mark it writable once we've done any necessary
1554 * COW.
1556 * We also mark the page dirty at this point even though the page will
1557 * change only once the write actually happens. This avoids a few races,
1558 * and potentially makes it more efficient.
1560 * We enter with non-exclusive mmap_sem (to exclude vma changes,
1561 * but allow concurrent faults), with pte both mapped and locked.
1562 * We return with mmap_sem still held, but pte unmapped and unlocked.
1563 */
1564 static int do_wp_page(struct mm_struct *mm, struct vm_area_struct *vma,
1565 unsigned long address, pte_t *page_table, pmd_t *pmd,
1566 spinlock_t *ptl, pte_t orig_pte)
1568 struct page *old_page, *new_page;
1569 pte_t entry;
1570 int ret = VM_FAULT_MINOR;
1572 old_page = vm_normal_page(vma, address, orig_pte);
1573 if (!old_page)
1574 goto gotten;
1576 if (PageAnon(old_page) && !TestSetPageLocked(old_page)) {
1577 int reuse = can_share_swap_page(old_page);
1578 unlock_page(old_page);
1579 if (reuse) {
1580 flush_cache_page(vma, address, pte_pfn(orig_pte));
1581 entry = pte_mkyoung(orig_pte);
1582 entry = maybe_mkwrite(pte_mkdirty(entry), vma);
1583 ptep_set_access_flags(vma, address, page_table, entry, 1);
1584 update_mmu_cache(vma, address, entry);
1585 lazy_mmu_prot_update(entry);
1586 ret |= VM_FAULT_WRITE;
1587 goto unlock;
1591 /*
1592 * Ok, we need to copy. Oh, well..
1593 */
1594 page_cache_get(old_page);
1595 gotten:
1596 pte_unmap_unlock(page_table, ptl);
1598 if (unlikely(anon_vma_prepare(vma)))
1599 goto oom;
1600 if (old_page == ZERO_PAGE(address)) {
1601 new_page = alloc_zeroed_user_highpage(vma, address);
1602 if (!new_page)
1603 goto oom;
1604 } else {
1605 new_page = alloc_page_vma(GFP_HIGHUSER, vma, address);
1606 if (!new_page)
1607 goto oom;
1608 cow_user_page(new_page, old_page, address);
1611 /*
1612 * Re-check the pte - we dropped the lock
1613 */
1614 page_table = pte_offset_map_lock(mm, pmd, address, &ptl);
1615 if (likely(pte_same(*page_table, orig_pte))) {
1616 if (old_page) {
1617 page_remove_rmap(old_page);
1618 if (!PageAnon(old_page)) {
1619 dec_mm_counter(mm, file_rss);
1620 inc_mm_counter(mm, anon_rss);
1622 } else
1623 inc_mm_counter(mm, anon_rss);
1624 flush_cache_page(vma, address, pte_pfn(orig_pte));
1625 entry = mk_pte(new_page, vma->vm_page_prot);
1626 entry = maybe_mkwrite(pte_mkdirty(entry), vma);
1627 ptep_establish(vma, address, page_table, entry);
1628 update_mmu_cache(vma, address, entry);
1629 lazy_mmu_prot_update(entry);
1630 lru_cache_add_active(new_page);
1631 page_add_new_anon_rmap(new_page, vma, address);
1633 /* Free the old page.. */
1634 new_page = old_page;
1635 ret |= VM_FAULT_WRITE;
1637 if (new_page)
1638 page_cache_release(new_page);
1639 if (old_page)
1640 page_cache_release(old_page);
1641 unlock:
1642 pte_unmap_unlock(page_table, ptl);
1643 return ret;
1644 oom:
1645 if (old_page)
1646 page_cache_release(old_page);
1647 return VM_FAULT_OOM;
1650 /*
1651 * Helper functions for unmap_mapping_range().
1653 * __ Notes on dropping i_mmap_lock to reduce latency while unmapping __
1655 * We have to restart searching the prio_tree whenever we drop the lock,
1656 * since the iterator is only valid while the lock is held, and anyway
1657 * a later vma might be split and reinserted earlier while lock dropped.
1659 * The list of nonlinear vmas could be handled more efficiently, using
1660 * a placeholder, but handle it in the same way until a need is shown.
1661 * It is important to search the prio_tree before nonlinear list: a vma
1662 * may become nonlinear and be shifted from prio_tree to nonlinear list
1663 * while the lock is dropped; but never shifted from list to prio_tree.
1665 * In order to make forward progress despite restarting the search,
1666 * vm_truncate_count is used to mark a vma as now dealt with, so we can
1667 * quickly skip it next time around. Since the prio_tree search only
1668 * shows us those vmas affected by unmapping the range in question, we
1669 * can't efficiently keep all vmas in step with mapping->truncate_count:
1670 * so instead reset them all whenever it wraps back to 0 (then go to 1).
1671 * mapping->truncate_count and vma->vm_truncate_count are protected by
1672 * i_mmap_lock.
1674 * In order to make forward progress despite repeatedly restarting some
1675 * large vma, note the restart_addr from unmap_vmas when it breaks out:
1676 * and restart from that address when we reach that vma again. It might
1677 * have been split or merged, shrunk or extended, but never shifted: so
1678 * restart_addr remains valid so long as it remains in the vma's range.
1679 * unmap_mapping_range forces truncate_count to leap over page-aligned
1680 * values so we can save vma's restart_addr in its truncate_count field.
1681 */
1682 #define is_restart_addr(truncate_count) (!((truncate_count) & ~PAGE_MASK))
1684 static void reset_vma_truncate_counts(struct address_space *mapping)
1686 struct vm_area_struct *vma;
1687 struct prio_tree_iter iter;
1689 vma_prio_tree_foreach(vma, &iter, &mapping->i_mmap, 0, ULONG_MAX)
1690 vma->vm_truncate_count = 0;
1691 list_for_each_entry(vma, &mapping->i_mmap_nonlinear, shared.vm_set.list)
1692 vma->vm_truncate_count = 0;
1695 static int unmap_mapping_range_vma(struct vm_area_struct *vma,
1696 unsigned long start_addr, unsigned long end_addr,
1697 struct zap_details *details)
1699 unsigned long restart_addr;
1700 int need_break;
1702 again:
1703 restart_addr = vma->vm_truncate_count;
1704 if (is_restart_addr(restart_addr) && start_addr < restart_addr) {
1705 start_addr = restart_addr;
1706 if (start_addr >= end_addr) {
1707 /* Top of vma has been split off since last time */
1708 vma->vm_truncate_count = details->truncate_count;
1709 return 0;
1713 restart_addr = zap_page_range(vma, start_addr,
1714 end_addr - start_addr, details);
1715 need_break = need_resched() ||
1716 need_lockbreak(details->i_mmap_lock);
1718 if (restart_addr >= end_addr) {
1719 /* We have now completed this vma: mark it so */
1720 vma->vm_truncate_count = details->truncate_count;
1721 if (!need_break)
1722 return 0;
1723 } else {
1724 /* Note restart_addr in vma's truncate_count field */
1725 vma->vm_truncate_count = restart_addr;
1726 if (!need_break)
1727 goto again;
1730 spin_unlock(details->i_mmap_lock);
1731 cond_resched();
1732 spin_lock(details->i_mmap_lock);
1733 return -EINTR;
1736 static inline void unmap_mapping_range_tree(struct prio_tree_root *root,
1737 struct zap_details *details)
1739 struct vm_area_struct *vma;
1740 struct prio_tree_iter iter;
1741 pgoff_t vba, vea, zba, zea;
1743 restart:
1744 vma_prio_tree_foreach(vma, &iter, root,
1745 details->first_index, details->last_index) {
1746 /* Skip quickly over those we have already dealt with */
1747 if (vma->vm_truncate_count == details->truncate_count)
1748 continue;
1750 vba = vma->vm_pgoff;
1751 vea = vba + ((vma->vm_end - vma->vm_start) >> PAGE_SHIFT) - 1;
1752 /* Assume for now that PAGE_CACHE_SHIFT == PAGE_SHIFT */
1753 zba = details->first_index;
1754 if (zba < vba)
1755 zba = vba;
1756 zea = details->last_index;
1757 if (zea > vea)
1758 zea = vea;
1760 if (unmap_mapping_range_vma(vma,
1761 ((zba - vba) << PAGE_SHIFT) + vma->vm_start,
1762 ((zea - vba + 1) << PAGE_SHIFT) + vma->vm_start,
1763 details) < 0)
1764 goto restart;
1768 static inline void unmap_mapping_range_list(struct list_head *head,
1769 struct zap_details *details)
1771 struct vm_area_struct *vma;
1773 /*
1774 * In nonlinear VMAs there is no correspondence between virtual address
1775 * offset and file offset. So we must perform an exhaustive search
1776 * across *all* the pages in each nonlinear VMA, not just the pages
1777 * whose virtual address lies outside the file truncation point.
1778 */
1779 restart:
1780 list_for_each_entry(vma, head, shared.vm_set.list) {
1781 /* Skip quickly over those we have already dealt with */
1782 if (vma->vm_truncate_count == details->truncate_count)
1783 continue;
1784 details->nonlinear_vma = vma;
1785 if (unmap_mapping_range_vma(vma, vma->vm_start,
1786 vma->vm_end, details) < 0)
1787 goto restart;
1791 /**
1792 * unmap_mapping_range - unmap the portion of all mmaps
1793 * in the specified address_space corresponding to the specified
1794 * page range in the underlying file.
1795 * @mapping: the address space containing mmaps to be unmapped.
1796 * @holebegin: byte in first page to unmap, relative to the start of
1797 * the underlying file. This will be rounded down to a PAGE_SIZE
1798 * boundary. Note that this is different from vmtruncate(), which
1799 * must keep the partial page. In contrast, we must get rid of
1800 * partial pages.
1801 * @holelen: size of prospective hole in bytes. This will be rounded
1802 * up to a PAGE_SIZE boundary. A holelen of zero truncates to the
1803 * end of the file.
1804 * @even_cows: 1 when truncating a file, unmap even private COWed pages;
1805 * but 0 when invalidating pagecache, don't throw away private data.
1806 */
1807 void unmap_mapping_range(struct address_space *mapping,
1808 loff_t const holebegin, loff_t const holelen, int even_cows)
1810 struct zap_details details;
1811 pgoff_t hba = holebegin >> PAGE_SHIFT;
1812 pgoff_t hlen = (holelen + PAGE_SIZE - 1) >> PAGE_SHIFT;
1814 /* Check for overflow. */
1815 if (sizeof(holelen) > sizeof(hlen)) {
1816 long long holeend =
1817 (holebegin + holelen + PAGE_SIZE - 1) >> PAGE_SHIFT;
1818 if (holeend & ~(long long)ULONG_MAX)
1819 hlen = ULONG_MAX - hba + 1;
1822 details.check_mapping = even_cows? NULL: mapping;
1823 details.nonlinear_vma = NULL;
1824 details.first_index = hba;
1825 details.last_index = hba + hlen - 1;
1826 if (details.last_index < details.first_index)
1827 details.last_index = ULONG_MAX;
1828 details.i_mmap_lock = &mapping->i_mmap_lock;
1830 spin_lock(&mapping->i_mmap_lock);
1832 /* serialize i_size write against truncate_count write */
1833 smp_wmb();
1834 /* Protect against page faults, and endless unmapping loops */
1835 mapping->truncate_count++;
1836 /*
1837 * For archs where spin_lock has inclusive semantics like ia64
1838 * this smp_mb() will prevent to read pagetable contents
1839 * before the truncate_count increment is visible to
1840 * other cpus.
1841 */
1842 smp_mb();
1843 if (unlikely(is_restart_addr(mapping->truncate_count))) {
1844 if (mapping->truncate_count == 0)
1845 reset_vma_truncate_counts(mapping);
1846 mapping->truncate_count++;
1848 details.truncate_count = mapping->truncate_count;
1850 if (unlikely(!prio_tree_empty(&mapping->i_mmap)))
1851 unmap_mapping_range_tree(&mapping->i_mmap, &details);
1852 if (unlikely(!list_empty(&mapping->i_mmap_nonlinear)))
1853 unmap_mapping_range_list(&mapping->i_mmap_nonlinear, &details);
1854 spin_unlock(&mapping->i_mmap_lock);
1856 EXPORT_SYMBOL(unmap_mapping_range);
1858 /*
1859 * Handle all mappings that got truncated by a "truncate()"
1860 * system call.
1862 * NOTE! We have to be ready to update the memory sharing
1863 * between the file and the memory map for a potential last
1864 * incomplete page. Ugly, but necessary.
1865 */
1866 int vmtruncate(struct inode * inode, loff_t offset)
1868 struct address_space *mapping = inode->i_mapping;
1869 unsigned long limit;
1871 if (inode->i_size < offset)
1872 goto do_expand;
1873 /*
1874 * truncation of in-use swapfiles is disallowed - it would cause
1875 * subsequent swapout to scribble on the now-freed blocks.
1876 */
1877 if (IS_SWAPFILE(inode))
1878 goto out_busy;
1879 i_size_write(inode, offset);
1880 unmap_mapping_range(mapping, offset + PAGE_SIZE - 1, 0, 1);
1881 truncate_inode_pages(mapping, offset);
1882 goto out_truncate;
1884 do_expand:
1885 limit = current->signal->rlim[RLIMIT_FSIZE].rlim_cur;
1886 if (limit != RLIM_INFINITY && offset > limit)
1887 goto out_sig;
1888 if (offset > inode->i_sb->s_maxbytes)
1889 goto out_big;
1890 i_size_write(inode, offset);
1892 out_truncate:
1893 if (inode->i_op && inode->i_op->truncate)
1894 inode->i_op->truncate(inode);
1895 return 0;
1896 out_sig:
1897 send_sig(SIGXFSZ, current, 0);
1898 out_big:
1899 return -EFBIG;
1900 out_busy:
1901 return -ETXTBSY;
1903 EXPORT_SYMBOL(vmtruncate);
1905 int vmtruncate_range(struct inode *inode, loff_t offset, loff_t end)
1907 struct address_space *mapping = inode->i_mapping;
1909 /*
1910 * If the underlying filesystem is not going to provide
1911 * a way to truncate a range of blocks (punch a hole) -
1912 * we should return failure right now.
1913 */
1914 if (!inode->i_op || !inode->i_op->truncate_range)
1915 return -ENOSYS;
1917 mutex_lock(&inode->i_mutex);
1918 down_write(&inode->i_alloc_sem);
1919 unmap_mapping_range(mapping, offset, (end - offset), 1);
1920 truncate_inode_pages_range(mapping, offset, end);
1921 inode->i_op->truncate_range(inode, offset, end);
1922 up_write(&inode->i_alloc_sem);
1923 mutex_unlock(&inode->i_mutex);
1925 return 0;
1927 EXPORT_SYMBOL(vmtruncate_range);
1929 /*
1930 * Primitive swap readahead code. We simply read an aligned block of
1931 * (1 << page_cluster) entries in the swap area. This method is chosen
1932 * because it doesn't cost us any seek time. We also make sure to queue
1933 * the 'original' request together with the readahead ones...
1935 * This has been extended to use the NUMA policies from the mm triggering
1936 * the readahead.
1938 * Caller must hold down_read on the vma->vm_mm if vma is not NULL.
1939 */
1940 void swapin_readahead(swp_entry_t entry, unsigned long addr,struct vm_area_struct *vma)
1942 #ifdef CONFIG_NUMA
1943 struct vm_area_struct *next_vma = vma ? vma->vm_next : NULL;
1944 #endif
1945 int i, num;
1946 struct page *new_page;
1947 unsigned long offset;
1949 /*
1950 * Get the number of handles we should do readahead io to.
1951 */
1952 num = valid_swaphandles(entry, &offset);
1953 for (i = 0; i < num; offset++, i++) {
1954 /* Ok, do the async read-ahead now */
1955 new_page = read_swap_cache_async(swp_entry(swp_type(entry),
1956 offset), vma, addr);
1957 if (!new_page)
1958 break;
1959 page_cache_release(new_page);
1960 #ifdef CONFIG_NUMA
1961 /*
1962 * Find the next applicable VMA for the NUMA policy.
1963 */
1964 addr += PAGE_SIZE;
1965 if (addr == 0)
1966 vma = NULL;
1967 if (vma) {
1968 if (addr >= vma->vm_end) {
1969 vma = next_vma;
1970 next_vma = vma ? vma->vm_next : NULL;
1972 if (vma && addr < vma->vm_start)
1973 vma = NULL;
1974 } else {
1975 if (next_vma && addr >= next_vma->vm_start) {
1976 vma = next_vma;
1977 next_vma = vma->vm_next;
1980 #endif
1982 lru_add_drain(); /* Push any new pages onto the LRU now */
1985 /*
1986 * We enter with non-exclusive mmap_sem (to exclude vma changes,
1987 * but allow concurrent faults), and pte mapped but not yet locked.
1988 * We return with mmap_sem still held, but pte unmapped and unlocked.
1989 */
1990 static int do_swap_page(struct mm_struct *mm, struct vm_area_struct *vma,
1991 unsigned long address, pte_t *page_table, pmd_t *pmd,
1992 int write_access, pte_t orig_pte)
1994 spinlock_t *ptl;
1995 struct page *page;
1996 swp_entry_t entry;
1997 pte_t pte;
1998 int ret = VM_FAULT_MINOR;
2000 if (!pte_unmap_same(mm, pmd, page_table, orig_pte))
2001 goto out;
2003 entry = pte_to_swp_entry(orig_pte);
2004 again:
2005 page = lookup_swap_cache(entry);
2006 if (!page) {
2007 swapin_readahead(entry, address, vma);
2008 page = read_swap_cache_async(entry, vma, address);
2009 if (!page) {
2010 /*
2011 * Back out if somebody else faulted in this pte
2012 * while we released the pte lock.
2013 */
2014 page_table = pte_offset_map_lock(mm, pmd, address, &ptl);
2015 if (likely(pte_same(*page_table, orig_pte)))
2016 ret = VM_FAULT_OOM;
2017 goto unlock;
2020 /* Had to read the page from swap area: Major fault */
2021 ret = VM_FAULT_MAJOR;
2022 inc_page_state(pgmajfault);
2023 grab_swap_token();
2026 mark_page_accessed(page);
2027 lock_page(page);
2028 if (!PageSwapCache(page)) {
2029 /* Page migration has occured */
2030 unlock_page(page);
2031 page_cache_release(page);
2032 goto again;
2035 /*
2036 * Back out if somebody else already faulted in this pte.
2037 */
2038 page_table = pte_offset_map_lock(mm, pmd, address, &ptl);
2039 if (unlikely(!pte_same(*page_table, orig_pte)))
2040 goto out_nomap;
2042 if (unlikely(!PageUptodate(page))) {
2043 ret = VM_FAULT_SIGBUS;
2044 goto out_nomap;
2047 /* The page isn't present yet, go ahead with the fault. */
2049 inc_mm_counter(mm, anon_rss);
2050 pte = mk_pte(page, vma->vm_page_prot);
2051 if (write_access && can_share_swap_page(page)) {
2052 pte = maybe_mkwrite(pte_mkdirty(pte), vma);
2053 write_access = 0;
2056 flush_icache_page(vma, page);
2057 set_pte_at(mm, address, page_table, pte);
2058 page_add_anon_rmap(page, vma, address);
2060 swap_free(entry);
2061 if (vm_swap_full())
2062 remove_exclusive_swap_page(page);
2063 unlock_page(page);
2065 if (write_access) {
2066 if (do_wp_page(mm, vma, address,
2067 page_table, pmd, ptl, pte) == VM_FAULT_OOM)
2068 ret = VM_FAULT_OOM;
2069 goto out;
2072 /* No need to invalidate - it was non-present before */
2073 update_mmu_cache(vma, address, pte);
2074 lazy_mmu_prot_update(pte);
2075 unlock:
2076 pte_unmap_unlock(page_table, ptl);
2077 out:
2078 return ret;
2079 out_nomap:
2080 pte_unmap_unlock(page_table, ptl);
2081 unlock_page(page);
2082 page_cache_release(page);
2083 return ret;
2086 /*
2087 * We enter with non-exclusive mmap_sem (to exclude vma changes,
2088 * but allow concurrent faults), and pte mapped but not yet locked.
2089 * We return with mmap_sem still held, but pte unmapped and unlocked.
2090 */
2091 static int do_anonymous_page(struct mm_struct *mm, struct vm_area_struct *vma,
2092 unsigned long address, pte_t *page_table, pmd_t *pmd,
2093 int write_access)
2095 struct page *page;
2096 spinlock_t *ptl;
2097 pte_t entry;
2099 if (write_access) {
2100 /* Allocate our own private page. */
2101 pte_unmap(page_table);
2103 if (unlikely(anon_vma_prepare(vma)))
2104 goto oom;
2105 page = alloc_zeroed_user_highpage(vma, address);
2106 if (!page)
2107 goto oom;
2109 entry = mk_pte(page, vma->vm_page_prot);
2110 entry = maybe_mkwrite(pte_mkdirty(entry), vma);
2112 page_table = pte_offset_map_lock(mm, pmd, address, &ptl);
2113 if (!pte_none(*page_table))
2114 goto release;
2115 inc_mm_counter(mm, anon_rss);
2116 lru_cache_add_active(page);
2117 page_add_new_anon_rmap(page, vma, address);
2118 } else {
2119 /* Map the ZERO_PAGE - vm_page_prot is readonly */
2120 page = ZERO_PAGE(address);
2121 page_cache_get(page);
2122 entry = mk_pte(page, vma->vm_page_prot);
2124 ptl = pte_lockptr(mm, pmd);
2125 spin_lock(ptl);
2126 if (!pte_none(*page_table))
2127 goto release;
2128 inc_mm_counter(mm, file_rss);
2129 page_add_file_rmap(page);
2132 set_pte_at(mm, address, page_table, entry);
2134 /* No need to invalidate - it was non-present before */
2135 update_mmu_cache(vma, address, entry);
2136 lazy_mmu_prot_update(entry);
2137 unlock:
2138 pte_unmap_unlock(page_table, ptl);
2139 return VM_FAULT_MINOR;
2140 release:
2141 page_cache_release(page);
2142 goto unlock;
2143 oom:
2144 return VM_FAULT_OOM;
2147 /*
2148 * do_no_page() tries to create a new page mapping. It aggressively
2149 * tries to share with existing pages, but makes a separate copy if
2150 * the "write_access" parameter is true in order to avoid the next
2151 * page fault.
2153 * As this is called only for pages that do not currently exist, we
2154 * do not need to flush old virtual caches or the TLB.
2156 * We enter with non-exclusive mmap_sem (to exclude vma changes,
2157 * but allow concurrent faults), and pte mapped but not yet locked.
2158 * We return with mmap_sem still held, but pte unmapped and unlocked.
2159 */
2160 static int do_no_page(struct mm_struct *mm, struct vm_area_struct *vma,
2161 unsigned long address, pte_t *page_table, pmd_t *pmd,
2162 int write_access)
2164 spinlock_t *ptl;
2165 struct page *new_page;
2166 struct address_space *mapping = NULL;
2167 pte_t entry;
2168 unsigned int sequence = 0;
2169 int ret = VM_FAULT_MINOR;
2170 int anon = 0;
2172 pte_unmap(page_table);
2173 BUG_ON(vma->vm_flags & VM_PFNMAP);
2175 if (vma->vm_file) {
2176 mapping = vma->vm_file->f_mapping;
2177 sequence = mapping->truncate_count;
2178 smp_rmb(); /* serializes i_size against truncate_count */
2180 retry:
2181 new_page = vma->vm_ops->nopage(vma, address & PAGE_MASK, &ret);
2182 /*
2183 * No smp_rmb is needed here as long as there's a full
2184 * spin_lock/unlock sequence inside the ->nopage callback
2185 * (for the pagecache lookup) that acts as an implicit
2186 * smp_mb() and prevents the i_size read to happen
2187 * after the next truncate_count read.
2188 */
2190 /* no page was available -- either SIGBUS or OOM */
2191 if (new_page == NOPAGE_SIGBUS)
2192 return VM_FAULT_SIGBUS;
2193 if (new_page == NOPAGE_OOM)
2194 return VM_FAULT_OOM;
2196 /*
2197 * Should we do an early C-O-W break?
2198 */
2199 if (write_access && !(vma->vm_flags & VM_SHARED)) {
2200 struct page *page;
2202 if (unlikely(anon_vma_prepare(vma)))
2203 goto oom;
2204 page = alloc_page_vma(GFP_HIGHUSER, vma, address);
2205 if (!page)
2206 goto oom;
2207 copy_user_highpage(page, new_page, address);
2208 page_cache_release(new_page);
2209 new_page = page;
2210 anon = 1;
2213 page_table = pte_offset_map_lock(mm, pmd, address, &ptl);
2214 /*
2215 * For a file-backed vma, someone could have truncated or otherwise
2216 * invalidated this page. If unmap_mapping_range got called,
2217 * retry getting the page.
2218 */
2219 if (mapping && unlikely(sequence != mapping->truncate_count)) {
2220 pte_unmap_unlock(page_table, ptl);
2221 page_cache_release(new_page);
2222 cond_resched();
2223 sequence = mapping->truncate_count;
2224 smp_rmb();
2225 goto retry;
2228 /*
2229 * This silly early PAGE_DIRTY setting removes a race
2230 * due to the bad i386 page protection. But it's valid
2231 * for other architectures too.
2233 * Note that if write_access is true, we either now have
2234 * an exclusive copy of the page, or this is a shared mapping,
2235 * so we can make it writable and dirty to avoid having to
2236 * handle that later.
2237 */
2238 /* Only go through if we didn't race with anybody else... */
2239 if (pte_none(*page_table)) {
2240 flush_icache_page(vma, new_page);
2241 entry = mk_pte(new_page, vma->vm_page_prot);
2242 if (write_access)
2243 entry = maybe_mkwrite(pte_mkdirty(entry), vma);
2244 set_pte_at(mm, address, page_table, entry);
2245 if (anon) {
2246 inc_mm_counter(mm, anon_rss);
2247 lru_cache_add_active(new_page);
2248 page_add_new_anon_rmap(new_page, vma, address);
2249 } else {
2250 inc_mm_counter(mm, file_rss);
2251 page_add_file_rmap(new_page);
2253 } else {
2254 /* One of our sibling threads was faster, back out. */
2255 page_cache_release(new_page);
2256 goto unlock;
2259 /* no need to invalidate: a not-present page shouldn't be cached */
2260 update_mmu_cache(vma, address, entry);
2261 lazy_mmu_prot_update(entry);
2262 unlock:
2263 pte_unmap_unlock(page_table, ptl);
2264 return ret;
2265 oom:
2266 page_cache_release(new_page);
2267 return VM_FAULT_OOM;
2270 /*
2271 * Fault of a previously existing named mapping. Repopulate the pte
2272 * from the encoded file_pte if possible. This enables swappable
2273 * nonlinear vmas.
2275 * We enter with non-exclusive mmap_sem (to exclude vma changes,
2276 * but allow concurrent faults), and pte mapped but not yet locked.
2277 * We return with mmap_sem still held, but pte unmapped and unlocked.
2278 */
2279 static int do_file_page(struct mm_struct *mm, struct vm_area_struct *vma,
2280 unsigned long address, pte_t *page_table, pmd_t *pmd,
2281 int write_access, pte_t orig_pte)
2283 pgoff_t pgoff;
2284 int err;
2286 if (!pte_unmap_same(mm, pmd, page_table, orig_pte))
2287 return VM_FAULT_MINOR;
2289 if (unlikely(!(vma->vm_flags & VM_NONLINEAR))) {
2290 /*
2291 * Page table corrupted: show pte and kill process.
2292 */
2293 print_bad_pte(vma, orig_pte, address);
2294 return VM_FAULT_OOM;
2296 /* We can then assume vm->vm_ops && vma->vm_ops->populate */
2298 pgoff = pte_to_pgoff(orig_pte);
2299 err = vma->vm_ops->populate(vma, address & PAGE_MASK, PAGE_SIZE,
2300 vma->vm_page_prot, pgoff, 0);
2301 if (err == -ENOMEM)
2302 return VM_FAULT_OOM;
2303 if (err)
2304 return VM_FAULT_SIGBUS;
2305 return VM_FAULT_MAJOR;
2308 /*
2309 * These routines also need to handle stuff like marking pages dirty
2310 * and/or accessed for architectures that don't do it in hardware (most
2311 * RISC architectures). The early dirtying is also good on the i386.
2313 * There is also a hook called "update_mmu_cache()" that architectures
2314 * with external mmu caches can use to update those (ie the Sparc or
2315 * PowerPC hashed page tables that act as extended TLBs).
2317 * We enter with non-exclusive mmap_sem (to exclude vma changes,
2318 * but allow concurrent faults), and pte mapped but not yet locked.
2319 * We return with mmap_sem still held, but pte unmapped and unlocked.
2320 */
2321 static inline int handle_pte_fault(struct mm_struct *mm,
2322 struct vm_area_struct *vma, unsigned long address,
2323 pte_t *pte, pmd_t *pmd, int write_access)
2325 pte_t entry;
2326 pte_t old_entry;
2327 spinlock_t *ptl;
2329 old_entry = entry = *pte;
2330 if (!pte_present(entry)) {
2331 if (pte_none(entry)) {
2332 if (!vma->vm_ops || !vma->vm_ops->nopage)
2333 return do_anonymous_page(mm, vma, address,
2334 pte, pmd, write_access);
2335 return do_no_page(mm, vma, address,
2336 pte, pmd, write_access);
2338 if (pte_file(entry))
2339 return do_file_page(mm, vma, address,
2340 pte, pmd, write_access, entry);
2341 return do_swap_page(mm, vma, address,
2342 pte, pmd, write_access, entry);
2345 ptl = pte_lockptr(mm, pmd);
2346 spin_lock(ptl);
2347 if (unlikely(!pte_same(*pte, entry)))
2348 goto unlock;
2349 if (write_access) {
2350 if (!pte_write(entry))
2351 return do_wp_page(mm, vma, address,
2352 pte, pmd, ptl, entry);
2353 entry = pte_mkdirty(entry);
2355 entry = pte_mkyoung(entry);
2356 if (!pte_same(old_entry, entry)) {
2357 ptep_set_access_flags(vma, address, pte, entry, write_access);
2358 update_mmu_cache(vma, address, entry);
2359 lazy_mmu_prot_update(entry);
2360 } else {
2361 /*
2362 * This is needed only for protection faults but the arch code
2363 * is not yet telling us if this is a protection fault or not.
2364 * This still avoids useless tlb flushes for .text page faults
2365 * with threads.
2366 */
2367 if (write_access)
2368 flush_tlb_page(vma, address);
2370 unlock:
2371 pte_unmap_unlock(pte, ptl);
2372 return VM_FAULT_MINOR;
2375 /*
2376 * By the time we get here, we already hold the mm semaphore
2377 */
2378 int __handle_mm_fault(struct mm_struct *mm, struct vm_area_struct *vma,
2379 unsigned long address, int write_access)
2381 pgd_t *pgd;
2382 pud_t *pud;
2383 pmd_t *pmd;
2384 pte_t *pte;
2386 __set_current_state(TASK_RUNNING);
2388 inc_page_state(pgfault);
2390 if (unlikely(is_vm_hugetlb_page(vma)))
2391 return hugetlb_fault(mm, vma, address, write_access);
2393 pgd = pgd_offset(mm, address);
2394 pud = pud_alloc(mm, pgd, address);
2395 if (!pud)
2396 return VM_FAULT_OOM;
2397 pmd = pmd_alloc(mm, pud, address);
2398 if (!pmd)
2399 return VM_FAULT_OOM;
2400 pte = pte_alloc_map(mm, pmd, address);
2401 if (!pte)
2402 return VM_FAULT_OOM;
2404 return handle_pte_fault(mm, vma, address, pte, pmd, write_access);
2407 EXPORT_SYMBOL_GPL(__handle_mm_fault);
2409 #ifndef __PAGETABLE_PUD_FOLDED
2410 /*
2411 * Allocate page upper directory.
2412 * We've already handled the fast-path in-line.
2413 */
2414 int __pud_alloc(struct mm_struct *mm, pgd_t *pgd, unsigned long address)
2416 pud_t *new = pud_alloc_one(mm, address);
2417 if (!new)
2418 return -ENOMEM;
2420 spin_lock(&mm->page_table_lock);
2421 if (pgd_present(*pgd)) /* Another has populated it */
2422 pud_free(new);
2423 else
2424 pgd_populate(mm, pgd, new);
2425 spin_unlock(&mm->page_table_lock);
2426 return 0;
2428 #else
2429 /* Workaround for gcc 2.96 */
2430 int __pud_alloc(struct mm_struct *mm, pgd_t *pgd, unsigned long address)
2432 return 0;
2434 #endif /* __PAGETABLE_PUD_FOLDED */
2436 #ifndef __PAGETABLE_PMD_FOLDED
2437 /*
2438 * Allocate page middle directory.
2439 * We've already handled the fast-path in-line.
2440 */
2441 int __pmd_alloc(struct mm_struct *mm, pud_t *pud, unsigned long address)
2443 pmd_t *new = pmd_alloc_one(mm, address);
2444 if (!new)
2445 return -ENOMEM;
2447 spin_lock(&mm->page_table_lock);
2448 #ifndef __ARCH_HAS_4LEVEL_HACK
2449 if (pud_present(*pud)) /* Another has populated it */
2450 pmd_free(new);
2451 else
2452 pud_populate(mm, pud, new);
2453 #else
2454 if (pgd_present(*pud)) /* Another has populated it */
2455 pmd_free(new);
2456 else
2457 pgd_populate(mm, pud, new);
2458 #endif /* __ARCH_HAS_4LEVEL_HACK */
2459 spin_unlock(&mm->page_table_lock);
2460 return 0;
2462 #else
2463 /* Workaround for gcc 2.96 */
2464 int __pmd_alloc(struct mm_struct *mm, pud_t *pud, unsigned long address)
2466 return 0;
2468 #endif /* __PAGETABLE_PMD_FOLDED */
2470 int make_pages_present(unsigned long addr, unsigned long end)
2472 int ret, len, write;
2473 struct vm_area_struct * vma;
2475 vma = find_vma(current->mm, addr);
2476 if (!vma)
2477 return -1;
2478 write = (vma->vm_flags & VM_WRITE) != 0;
2479 if (addr >= end)
2480 BUG();
2481 if (end > vma->vm_end)
2482 BUG();
2483 len = (end+PAGE_SIZE-1)/PAGE_SIZE-addr/PAGE_SIZE;
2484 ret = get_user_pages(current, current->mm, addr,
2485 len, write, 0, NULL, NULL);
2486 if (ret < 0)
2487 return ret;
2488 return ret == len ? 0 : -1;
2491 /*
2492 * Map a vmalloc()-space virtual address to the physical page.
2493 */
2494 struct page * vmalloc_to_page(void * vmalloc_addr)
2496 unsigned long addr = (unsigned long) vmalloc_addr;
2497 struct page *page = NULL;
2498 pgd_t *pgd = pgd_offset_k(addr);
2499 pud_t *pud;
2500 pmd_t *pmd;
2501 pte_t *ptep, pte;
2503 if (!pgd_none(*pgd)) {
2504 pud = pud_offset(pgd, addr);
2505 if (!pud_none(*pud)) {
2506 pmd = pmd_offset(pud, addr);
2507 if (!pmd_none(*pmd)) {
2508 ptep = pte_offset_map(pmd, addr);
2509 pte = *ptep;
2510 if (pte_present(pte))
2511 page = pte_page(pte);
2512 pte_unmap(ptep);
2516 return page;
2519 EXPORT_SYMBOL(vmalloc_to_page);
2521 /*
2522 * Map a vmalloc()-space virtual address to the physical page frame number.
2523 */
2524 unsigned long vmalloc_to_pfn(void * vmalloc_addr)
2526 return page_to_pfn(vmalloc_to_page(vmalloc_addr));
2529 EXPORT_SYMBOL(vmalloc_to_pfn);
2531 #if !defined(__HAVE_ARCH_GATE_AREA)
2533 #if defined(AT_SYSINFO_EHDR)
2534 static struct vm_area_struct gate_vma;
2536 static int __init gate_vma_init(void)
2538 gate_vma.vm_mm = NULL;
2539 gate_vma.vm_start = FIXADDR_USER_START;
2540 gate_vma.vm_end = FIXADDR_USER_END;
2541 gate_vma.vm_page_prot = PAGE_READONLY;
2542 gate_vma.vm_flags = 0;
2543 return 0;
2545 __initcall(gate_vma_init);
2546 #endif
2548 struct vm_area_struct *get_gate_vma(struct task_struct *tsk)
2550 #ifdef AT_SYSINFO_EHDR
2551 return &gate_vma;
2552 #else
2553 return NULL;
2554 #endif
2557 int in_gate_area_no_task(unsigned long addr)
2559 #ifdef AT_SYSINFO_EHDR
2560 if ((addr >= FIXADDR_USER_START) && (addr < FIXADDR_USER_END))
2561 return 1;
2562 #endif
2563 return 0;
2566 #endif /* __HAVE_ARCH_GATE_AREA */