direct-io.hg

view linux-2.6.11-xen-sparse/mm/memory.c @ 5669:ff5d7ccd8d69

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