4 * Copyright (C) 1991, 1992, 1993, 1994 Linus Torvalds
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
13 * Ok, demand-loading was easy, shared pages a little bit tricker. Shared
14 * pages started 02.12.91, seems to work. - Linus.
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
20 * Also corrected some "invalidate()"s - I wasn't doing enough of them.
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.
32 * 05.04.94 - Multi-page memory management added for v1.1.
33 * Idea by Alex Bligh (alex@cconcepts.co.uk)
35 * 16.07.99 - Support of BIGMEM added by Gerhard Wichert, Siemens AG
36 * (Gerhard.Wichert@pdb.siemens.de)
38 * Aug/Sep 2004 Changed to four level page tables (Andi Kleen)
41 #include <linux/kernel_stat.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>
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;
67 EXPORT_SYMBOL(max_mapnr);
68 EXPORT_SYMBOL(mem_map);
71 unsigned long num_physpages;
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
80 unsigned long vmalloc_earlyreserve;
82 EXPORT_SYMBOL(num_physpages);
83 EXPORT_SYMBOL(high_memory);
84 EXPORT_SYMBOL(vmalloc_earlyreserve);
87 * Note: this doesn't free the actual pages themselves. That
88 * has been handled earlier when unmapping all the memory regions.
90 static inline void clear_pmd_range(struct mmu_gather *tlb, pmd_t *pmd, unsigned long start, unsigned long end)
96 if (unlikely(pmd_bad(*pmd))) {
101 if (!((start | end) & ~PMD_MASK)) {
102 /* Only clear full, aligned ranges */
103 page = pmd_page(*pmd);
105 dec_page_state(nr_page_table_pages);
107 pte_free_tlb(tlb, page);
111 static inline void clear_pud_range(struct mmu_gather *tlb, pud_t *pud, unsigned long start, unsigned long end)
113 unsigned long addr = start, next;
118 if (unlikely(pud_bad(*pud))) {
124 pmd = __pmd = pmd_offset(pud, start);
126 next = (addr + PMD_SIZE) & PMD_MASK;
127 if (next > end || next <= addr)
130 clear_pmd_range(tlb, pmd, addr, next);
133 } while (addr && (addr < end));
135 if (!((start | end) & ~PUD_MASK)) {
136 /* Only clear full, aligned ranges */
138 pmd_free_tlb(tlb, __pmd);
143 static inline void clear_pgd_range(struct mmu_gather *tlb, pgd_t *pgd, unsigned long start, unsigned long end)
145 unsigned long addr = start, next;
150 if (unlikely(pgd_bad(*pgd))) {
156 pud = __pud = pud_offset(pgd, start);
158 next = (addr + PUD_SIZE) & PUD_MASK;
159 if (next > end || next <= addr)
162 clear_pud_range(tlb, pud, addr, next);
165 } while (addr && (addr < end));
167 if (!((start | end) & ~PGDIR_MASK)) {
168 /* Only clear full, aligned ranges */
170 pud_free_tlb(tlb, __pud);
175 * This function clears user-level page tables of a process.
177 * Must be called with pagetable lock held.
179 void clear_page_range(struct mmu_gather *tlb, unsigned long start, unsigned long end)
181 unsigned long addr = start, next;
182 pgd_t * pgd = pgd_offset(tlb->mm, start);
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)
190 clear_pgd_range(tlb, pgd, addr, next);
196 pte_t fastcall * pte_alloc_map(struct mm_struct *mm, pmd_t *pmd, unsigned long address)
198 if (!pmd_present(*pmd)) {
201 spin_unlock(&mm->page_table_lock);
202 new = pte_alloc_one(mm, address);
203 spin_lock(&mm->page_table_lock);
207 * Because we dropped the lock, we should re-check the
208 * entry, as somebody else could have populated it..
210 if (pmd_present(*pmd)) {
215 inc_page_state(nr_page_table_pages);
216 pmd_populate(mm, pmd, new);
219 return pte_offset_map(pmd, address);
222 pte_t fastcall * pte_alloc_kernel(struct mm_struct *mm, pmd_t *pmd, unsigned long address)
224 if (!pmd_present(*pmd)) {
227 spin_unlock(&mm->page_table_lock);
228 new = pte_alloc_one_kernel(mm, address);
229 spin_lock(&mm->page_table_lock);
234 * Because we dropped the lock, we should re-check the
235 * entry, as somebody else could have populated it..
237 if (pmd_present(*pmd)) {
238 pte_free_kernel(new);
241 pmd_populate_kernel(mm, pmd, new);
244 return pte_offset_kernel(pmd, address);
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.
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().
257 copy_swap_pte(struct mm_struct *dst_mm, struct mm_struct *src_mm, pte_t pte)
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);
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,
274 pte_t pte = *src_pte;
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);
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
292 page = pfn_to_page(pfn);
294 if (!page || PageReserved(page)) {
295 set_pte(dst_pte, pte);
300 * If it's a COW mapping, write protect it both
301 * in the parent and the child
303 if ((vm_flags & (VM_SHARED | VM_MAYWRITE)) == VM_MAYWRITE) {
304 ptep_set_wrprotect(src_pte);
309 * If it's a shared mapping, mark it clean in
312 if (vm_flags & VM_SHARED)
313 pte = pte_mkclean(pte);
314 pte = pte_mkold(pte);
316 vx_rsspages_inc(dst_mm);
318 vx_anonpages_inc(dst_mm);
319 set_pte(dst_pte, pte);
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)
327 pte_t *src_pte, *dst_pte;
329 unsigned long vm_flags = vma->vm_flags;
331 d = dst_pte = pte_alloc_map(dst_mm, dst_pmd, addr);
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++) {
340 copy_one_pte(dst_mm, src_mm, d, s, vm_flags, addr);
342 pte_unmap_nested(src_pte);
344 spin_unlock(&src_mm->page_table_lock);
345 cond_resched_lock(&dst_mm->page_table_lock);
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)
353 pmd_t *src_pmd, *dst_pmd;
357 src_pmd = pmd_offset(src_pud, addr);
358 dst_pmd = pmd_alloc(dst_mm, dst_pud, addr);
362 for (; addr < end; addr = next, src_pmd++, dst_pmd++) {
363 next = (addr + PMD_SIZE) & PMD_MASK;
364 if (next > end || next <= addr)
366 if (pmd_none(*src_pmd))
368 if (pmd_bad(*src_pmd)) {
373 err = copy_pte_range(dst_mm, src_mm, dst_pmd, src_pmd,
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)
385 pud_t *src_pud, *dst_pud;
389 src_pud = pud_offset(src_pgd, addr);
390 dst_pud = pud_alloc(dst_mm, dst_pgd, addr);
394 for (; addr < end; addr = next, src_pud++, dst_pud++) {
395 next = (addr + PUD_SIZE) & PUD_MASK;
396 if (next > end || next <= addr)
398 if (pud_none(*src_pud))
400 if (pud_bad(*src_pud)) {
405 err = copy_pmd_range(dst_mm, src_mm, dst_pud, src_pud,
413 int copy_page_range(struct mm_struct *dst, struct mm_struct *src,
414 struct vm_area_struct *vma)
416 pgd_t *src_pgd, *dst_pgd;
417 unsigned long addr, start, end, next;
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);
429 while (addr && (addr < end-1)) {
430 next = (addr + PGDIR_SIZE) & PGDIR_MASK;
431 if (next > end || next <= addr)
433 if (pgd_none(*src_pgd))
435 if (pgd_bad(*src_pgd)) {
440 err = copy_pud_range(dst, src, dst_pgd, src_pgd,
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)
458 unsigned long offset;
463 if (unlikely(pmd_bad(*pmd))) {
468 ptep = pte_offset_map(pmd, address);
469 offset = address & ~PMD_MASK;
470 if (offset + size > PMD_SIZE)
471 size = PMD_SIZE - offset;
473 if (details && !details->check_mapping && !details->nonlinear_vma)
475 for (offset=0; offset < size; ptep++, offset += PAGE_SIZE) {
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))
487 if (unlikely(details) && page) {
489 * unmap_shared_mapping_pages() wants to
490 * invalidate cache without truncating:
491 * unmap shared but keep private pages.
493 if (details->check_mapping &&
494 details->check_mapping != page->mapping)
497 * Each page->index must be checked when
498 * invalidating or truncating nonlinear.
500 if (details->nonlinear_vma &&
501 (page->index < details->first_index ||
502 page->index > details->last_index))
505 pte = ptep_get_and_clear(ptep);
506 tlb_remove_tlb_entry(tlb, ptep, address+offset);
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));
514 set_page_dirty(page);
516 vx_anonpages_dec(tlb->mm);
517 else if (pte_young(pte))
518 mark_page_accessed(page);
520 page_remove_rmap(page);
521 tlb_remove_page(tlb, page);
525 * If details->check_mapping, we leave swap entries;
526 * if details->nonlinear_vma, we leave file entries.
528 if (unlikely(details))
531 free_swap_and_cache(pte_to_swp_entry(pte));
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)
546 if (unlikely(pud_bad(*pud))) {
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);
556 zap_pte_range(tlb, pmd, address, end - address, details);
557 address = (address + PMD_SIZE) & PMD_MASK;
559 } while (address && (address < end));
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)
570 if (unlikely(pgd_bad(*pgd))) {
575 pud = pud_offset(pgd, address);
577 zap_pmd_range(tlb, pud, address, end - address, details);
578 address = (address + PUD_SIZE) & PUD_MASK;
580 } while (address && (address < end));
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)
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)
598 zap_pud_range(tlb, pgd, address, next, details);
602 tlb_end_vma(tlb, vma);
605 #ifdef CONFIG_PREEMPT
606 # define ZAP_BLOCK_SIZE (8 * PAGE_SIZE)
608 /* No preempt: go for improved straight-line efficiency */
609 # define ZAP_BLOCK_SIZE (1024 * PAGE_SIZE)
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
622 * Returns the number of vma's which were covered by the unmapping.
624 * Unmap all pages in the vma list. Called under page_table_lock.
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.
630 * Only addresses between `start' and `end' will be unmapped.
632 * The VMA list must be sorted in ascending virtual address order.
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.
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)
644 unsigned long zap_bytes = ZAP_BLOCK_SIZE;
645 unsigned long tlb_start = 0; /* For tlb_finish_mmu */
646 int tlb_start_valid = 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) {
655 start = max(vma->vm_start, start_addr);
656 if (start >= vma->vm_end)
658 end = min(vma->vm_end, end_addr);
659 if (end <= vma->vm_start)
662 if (vma->vm_flags & VM_ACCOUNT)
663 *nr_accounted += (end - start) >> PAGE_SHIFT;
666 while (start != end) {
669 if (!tlb_start_valid) {
674 if (is_vm_hugetlb_page(vma)) {
676 unmap_hugepage_range(vma, start, end);
678 block = min(zap_bytes, end - start);
679 unmap_page_range(*tlbp, vma, start,
680 start + block, details);
685 if ((long)zap_bytes > 0)
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))) {
694 /* must reset count of rss freed */
695 *tlbp = tlb_gather_mmu(mm, fullmm);
696 details->break_addr = start;
699 spin_unlock(&mm->page_table_lock);
701 spin_lock(&mm->page_table_lock);
704 *tlbp = tlb_gather_mmu(mm, fullmm);
706 zap_bytes = ZAP_BLOCK_SIZE;
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
720 void zap_page_range(struct vm_area_struct *vma, unsigned long address,
721 unsigned long size, struct zap_details *details)
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);
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);
743 * Do a quick page-table lookup for a single page.
744 * mm->page_table_lock must be held.
747 __follow_page(struct mm_struct *mm, unsigned long address, int read, int write)
756 page = follow_huge_addr(mm, address, write);
760 pgd = pgd_offset(mm, address);
761 if (pgd_none(*pgd) || unlikely(pgd_bad(*pgd)))
764 pud = pud_offset(pgd, address);
765 if (pud_none(*pud) || unlikely(pud_bad(*pud)))
768 pmd = pmd_offset(pud, address);
769 if (pmd_none(*pmd) || unlikely(pmd_bad(*pmd)))
772 return follow_huge_pmd(mm, address, pmd, write);
774 ptep = pte_offset_map(pmd, address);
780 if (pte_present(pte)) {
781 if (write && !pte_write(pte))
783 if (read && !pte_read(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);
800 follow_page(struct mm_struct *mm, unsigned long address, int write)
802 return __follow_page(mm, address, /*read*/0, write);
806 check_user_page_readable(struct mm_struct *mm, unsigned long address)
808 return __follow_page(mm, address, /*read*/1, /*write*/0) != NULL;
811 EXPORT_SYMBOL(check_user_page_readable);
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.
819 static inline struct page *get_page_map(struct page *page)
821 if (!pfn_valid(page_to_pfn(page)))
828 untouched_anonymous_page(struct mm_struct* mm, struct vm_area_struct *vma,
829 unsigned long address)
835 /* Check if the vma is for an anonymous mapping. */
836 if (vma->vm_ops && vma->vm_ops->nopage)
839 /* Check if page directory entry exists. */
840 pgd = pgd_offset(mm, address);
841 if (pgd_none(*pgd) || unlikely(pgd_bad(*pgd)))
844 pud = pud_offset(pgd, address);
845 if (pud_none(*pud) || unlikely(pud_bad(*pud)))
848 /* Check if page middle directory entry exists. */
849 pmd = pmd_offset(pud, address);
850 if (pmd_none(*pmd) || unlikely(pmd_bad(*pmd)))
853 /* There is a pte slot for 'address' in 'mm'. */
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)
866 * Require read or write permissions.
867 * If 'force' is set, we only require the "MAY" flags.
869 flags = write ? (VM_WRITE | VM_MAYWRITE) : (VM_READ | VM_MAYREAD);
870 flags &= force ? (VM_MAYREAD | VM_MAYWRITE) : (VM_READ | VM_WRITE);
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);
884 if (write) /* user gate pages are read-only */
885 return i ? : -EFAULT;
887 pgd = pgd_offset_k(pg);
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));
898 pages[i] = pte_page(*pte);
910 if (!vma || (vma->vm_flags & VM_IO)
911 || !(flags & vma->vm_flags))
912 return i ? : -EFAULT;
914 if (is_vm_hugetlb_page(vma)) {
915 i = follow_hugetlb_page(mm, vma, pages, vmas,
919 spin_lock(&mm->page_table_lock);
922 int lookup_write = write;
924 cond_resched_lock(&mm->page_table_lock);
925 while (!(map = follow_page(mm, start, lookup_write))) {
927 * Shortcut for anonymous pages. We don't want
928 * to force the creation of pages tables for
929 * insanly big anonymously mapped areas that
930 * nobody touched so far. This is important
931 * for doing a core dump for these mappings.
934 untouched_anonymous_page(mm,vma,start)) {
935 map = ZERO_PAGE(start);
938 spin_unlock(&mm->page_table_lock);
939 switch (handle_mm_fault(mm,vma,start,write)) {
946 case VM_FAULT_SIGBUS:
947 return i ? i : -EFAULT;
949 return i ? i : -ENOMEM;
954 * Now that we have performed a write fault
955 * and surely no longer have a shared page we
956 * shouldn't write, we shouldn't ignore an
957 * unwritable page in the page table if
958 * we are forcing write access.
960 lookup_write = write && !force;
961 spin_lock(&mm->page_table_lock);
964 pages[i] = get_page_map(map);
966 spin_unlock(&mm->page_table_lock);
968 page_cache_release(pages[i]);
972 flush_dcache_page(pages[i]);
973 if (!PageReserved(pages[i]))
974 page_cache_get(pages[i]);
981 } while(len && start < vma->vm_end);
982 spin_unlock(&mm->page_table_lock);
988 EXPORT_SYMBOL(get_user_pages);
990 static void zeromap_pte_range(pte_t * pte, unsigned long address,
991 unsigned long size, pgprot_t prot)
995 address &= ~PMD_MASK;
996 end = address + size;
1000 pte_t zero_pte = pte_wrprotect(mk_pte(ZERO_PAGE(address), prot));
1001 BUG_ON(!pte_none(*pte));
1002 set_pte(pte, zero_pte);
1003 address += PAGE_SIZE;
1005 } while (address && (address < end));
1008 static inline int zeromap_pmd_range(struct mm_struct *mm, pmd_t * pmd,
1009 unsigned long address, unsigned long size, pgprot_t prot)
1011 unsigned long base, end;
1013 base = address & PUD_MASK;
1014 address &= ~PUD_MASK;
1015 end = address + size;
1019 pte_t * pte = pte_alloc_map(mm, pmd, base + address);
1022 zeromap_pte_range(pte, base + address, end - address, prot);
1024 address = (address + PMD_SIZE) & PMD_MASK;
1026 } while (address && (address < end));
1030 static inline int zeromap_pud_range(struct mm_struct *mm, pud_t * pud,
1031 unsigned long address,
1032 unsigned long size, pgprot_t prot)
1034 unsigned long base, end;
1037 base = address & PGDIR_MASK;
1038 address &= ~PGDIR_MASK;
1039 end = address + size;
1040 if (end > PGDIR_SIZE)
1043 pmd_t * pmd = pmd_alloc(mm, pud, base + address);
1047 error = zeromap_pmd_range(mm, pmd, base + address,
1048 end - address, prot);
1051 address = (address + PUD_SIZE) & PUD_MASK;
1053 } while (address && (address < end));
1057 int zeromap_page_range(struct vm_area_struct *vma, unsigned long address,
1058 unsigned long size, pgprot_t prot)
1063 unsigned long beg = address;
1064 unsigned long end = address + size;
1066 struct mm_struct *mm = vma->vm_mm;
1068 pgd = pgd_offset(mm, address);
1069 flush_cache_range(vma, beg, end);
1070 BUG_ON(address >= end);
1071 BUG_ON(end > vma->vm_end);
1073 spin_lock(&mm->page_table_lock);
1074 for (i = pgd_index(address); i <= pgd_index(end-1); i++) {
1075 pud_t *pud = pud_alloc(mm, pgd, address);
1079 next = (address + PGDIR_SIZE) & PGDIR_MASK;
1080 if (next <= beg || next > end)
1082 error = zeromap_pud_range(mm, pud, address,
1083 next - address, prot);
1090 * Why flush? zeromap_pte_range has a BUG_ON for !pte_none()
1092 flush_tlb_range(vma, beg, end);
1093 spin_unlock(&mm->page_table_lock);
1098 * maps a range of physical memory into the requested pages. the old
1099 * mappings are removed. any references to nonexistent pages results
1100 * in null mappings (currently treated as "copy-on-access")
1103 remap_pte_range(pte_t * pte, unsigned long address, unsigned long size,
1104 unsigned long pfn, pgprot_t prot)
1108 address &= ~PMD_MASK;
1109 end = address + size;
1113 BUG_ON(!pte_none(*pte));
1114 if (!pfn_valid(pfn) || PageReserved(pfn_to_page(pfn)))
1115 set_pte(pte, pfn_pte(pfn, prot));
1116 address += PAGE_SIZE;
1119 } while (address && (address < end));
1123 remap_pmd_range(struct mm_struct *mm, pmd_t * pmd, unsigned long address,
1124 unsigned long size, unsigned long pfn, pgprot_t prot)
1126 unsigned long base, end;
1128 base = address & PUD_MASK;
1129 address &= ~PUD_MASK;
1130 end = address + size;
1133 pfn -= (address >> PAGE_SHIFT);
1135 pte_t * pte = pte_alloc_map(mm, pmd, base + address);
1138 remap_pte_range(pte, base + address, end - address,
1139 (address >> PAGE_SHIFT) + pfn, prot);
1141 address = (address + PMD_SIZE) & PMD_MASK;
1143 } while (address && (address < end));
1147 static inline int remap_pud_range(struct mm_struct *mm, pud_t * pud,
1148 unsigned long address, unsigned long size,
1149 unsigned long pfn, pgprot_t prot)
1151 unsigned long base, end;
1154 base = address & PGDIR_MASK;
1155 address &= ~PGDIR_MASK;
1156 end = address + size;
1157 if (end > PGDIR_SIZE)
1159 pfn -= address >> PAGE_SHIFT;
1161 pmd_t *pmd = pmd_alloc(mm, pud, base+address);
1165 error = remap_pmd_range(mm, pmd, base + address, end - address,
1166 (address >> PAGE_SHIFT) + pfn, prot);
1169 address = (address + PUD_SIZE) & PUD_MASK;
1171 } while (address && (address < end));
1175 /* Note: this is only safe if the mm semaphore is held when called. */
1176 int remap_pfn_range(struct vm_area_struct *vma, unsigned long from,
1177 unsigned long pfn, unsigned long size, pgprot_t prot)
1181 unsigned long beg = from;
1182 unsigned long end = from + size;
1184 struct mm_struct *mm = vma->vm_mm;
1187 pfn -= from >> PAGE_SHIFT;
1188 pgd = pgd_offset(mm, from);
1189 flush_cache_range(vma, beg, end);
1190 BUG_ON(from >= end);
1193 * Physically remapped pages are special. Tell the
1194 * rest of the world about it:
1195 * VM_IO tells people not to look at these pages
1196 * (accesses can have side effects).
1197 * VM_RESERVED tells swapout not to try to touch
1200 vma->vm_flags |= VM_IO | VM_RESERVED;
1202 spin_lock(&mm->page_table_lock);
1203 for (i = pgd_index(beg); i <= pgd_index(end-1); i++) {
1204 pud_t *pud = pud_alloc(mm, pgd, from);
1208 next = (from + PGDIR_SIZE) & PGDIR_MASK;
1209 if (next > end || next <= from)
1211 error = remap_pud_range(mm, pud, from, end - from,
1212 pfn + (from >> PAGE_SHIFT), prot);
1219 * Why flush? remap_pte_range has a BUG_ON for !pte_none()
1221 flush_tlb_range(vma, beg, end);
1222 spin_unlock(&mm->page_table_lock);
1227 EXPORT_SYMBOL(remap_pfn_range);
1230 * Do pte_mkwrite, but only if the vma says VM_WRITE. We do this when
1231 * servicing faults for write access. In the normal case, do always want
1232 * pte_mkwrite. But get_user_pages can cause write faults for mappings
1233 * that do not have writing enabled, when used by access_process_vm.
1235 static inline pte_t maybe_mkwrite(pte_t pte, struct vm_area_struct *vma)
1237 if (likely(vma->vm_flags & VM_WRITE))
1238 pte = pte_mkwrite(pte);
1243 * We hold the mm semaphore for reading and vma->vm_mm->page_table_lock
1245 static inline void break_cow(struct vm_area_struct * vma, struct page * new_page, unsigned long address,
1250 flush_cache_page(vma, address);
1251 entry = maybe_mkwrite(pte_mkdirty(mk_pte(new_page, vma->vm_page_prot)),
1253 ptep_establish(vma, address, page_table, entry);
1254 update_mmu_cache(vma, address, entry);
1258 * This routine handles present pages, when users try to write
1259 * to a shared page. It is done by copying the page to a new address
1260 * and decrementing the shared-page counter for the old page.
1262 * Goto-purists beware: the only reason for goto's here is that it results
1263 * in better assembly code.. The "default" path will see no jumps at all.
1265 * Note that this routine assumes that the protection checks have been
1266 * done by the caller (the low-level page fault routine in most cases).
1267 * Thus we can safely just mark it writable once we've done any necessary
1270 * We also mark the page dirty at this point even though the page will
1271 * change only once the write actually happens. This avoids a few races,
1272 * and potentially makes it more efficient.
1274 * We hold the mm semaphore and the page_table_lock on entry and exit
1275 * with the page_table_lock released.
1277 static int do_wp_page(struct mm_struct *mm, struct vm_area_struct * vma,
1278 unsigned long address, pte_t *page_table, pmd_t *pmd, pte_t pte)
1280 struct page *old_page, *new_page;
1281 unsigned long pfn = pte_pfn(pte);
1284 if (unlikely(!pfn_valid(pfn))) {
1286 * This should really halt the system so it can be debugged or
1287 * at least the kernel stops what it's doing before it corrupts
1288 * data, but for the moment just pretend this is OOM.
1290 pte_unmap(page_table);
1291 printk(KERN_ERR "do_wp_page: bogus page at address %08lx\n",
1293 spin_unlock(&mm->page_table_lock);
1294 return VM_FAULT_OOM;
1296 old_page = pfn_to_page(pfn);
1298 if (!TestSetPageLocked(old_page)) {
1299 int reuse = can_share_swap_page(old_page);
1300 unlock_page(old_page);
1302 flush_cache_page(vma, address);
1303 entry = maybe_mkwrite(pte_mkyoung(pte_mkdirty(pte)),
1305 ptep_set_access_flags(vma, address, page_table, entry, 1);
1306 update_mmu_cache(vma, address, entry);
1307 pte_unmap(page_table);
1308 spin_unlock(&mm->page_table_lock);
1309 return VM_FAULT_MINOR;
1312 pte_unmap(page_table);
1315 * Ok, we need to copy. Oh, well..
1317 if (!PageReserved(old_page))
1318 page_cache_get(old_page);
1319 spin_unlock(&mm->page_table_lock);
1321 if (unlikely(anon_vma_prepare(vma)))
1323 if (old_page == ZERO_PAGE(address)) {
1324 new_page = alloc_zeroed_user_highpage(vma, address);
1328 new_page = alloc_page_vma(GFP_HIGHUSER, vma, address);
1331 copy_user_highpage(new_page, old_page, address);
1334 * Re-check the pte - we dropped the lock
1336 spin_lock(&mm->page_table_lock);
1337 page_table = pte_offset_map(pmd, address);
1338 if (likely(pte_same(*page_table, pte))) {
1339 if (PageAnon(old_page))
1340 vx_anonpages_dec(mm);
1341 if (PageReserved(old_page)) {
1342 vx_rsspages_inc(mm);
1343 acct_update_integrals();
1344 update_mem_hiwater();
1346 page_remove_rmap(old_page);
1347 break_cow(vma, new_page, address, page_table);
1348 lru_cache_add_active(new_page);
1349 page_add_anon_rmap(new_page, vma, address);
1351 /* Free the old page.. */
1352 new_page = old_page;
1354 pte_unmap(page_table);
1355 page_cache_release(new_page);
1356 page_cache_release(old_page);
1357 spin_unlock(&mm->page_table_lock);
1358 return VM_FAULT_MINOR;
1361 page_cache_release(old_page);
1362 return VM_FAULT_OOM;
1366 * Helper functions for unmap_mapping_range().
1368 * __ Notes on dropping i_mmap_lock to reduce latency while unmapping __
1370 * We have to restart searching the prio_tree whenever we drop the lock,
1371 * since the iterator is only valid while the lock is held, and anyway
1372 * a later vma might be split and reinserted earlier while lock dropped.
1374 * The list of nonlinear vmas could be handled more efficiently, using
1375 * a placeholder, but handle it in the same way until a need is shown.
1376 * It is important to search the prio_tree before nonlinear list: a vma
1377 * may become nonlinear and be shifted from prio_tree to nonlinear list
1378 * while the lock is dropped; but never shifted from list to prio_tree.
1380 * In order to make forward progress despite restarting the search,
1381 * vm_truncate_count is used to mark a vma as now dealt with, so we can
1382 * quickly skip it next time around. Since the prio_tree search only
1383 * shows us those vmas affected by unmapping the range in question, we
1384 * can't efficiently keep all vmas in step with mapping->truncate_count:
1385 * so instead reset them all whenever it wraps back to 0 (then go to 1).
1386 * mapping->truncate_count and vma->vm_truncate_count are protected by
1389 * In order to make forward progress despite repeatedly restarting some
1390 * large vma, note the break_addr set by unmap_vmas when it breaks out:
1391 * and restart from that address when we reach that vma again. It might
1392 * have been split or merged, shrunk or extended, but never shifted: so
1393 * restart_addr remains valid so long as it remains in the vma's range.
1394 * unmap_mapping_range forces truncate_count to leap over page-aligned
1395 * values so we can save vma's restart_addr in its truncate_count field.
1397 #define is_restart_addr(truncate_count) (!((truncate_count) & ~PAGE_MASK))
1399 static void reset_vma_truncate_counts(struct address_space *mapping)
1401 struct vm_area_struct *vma;
1402 struct prio_tree_iter iter;
1404 vma_prio_tree_foreach(vma, &iter, &mapping->i_mmap, 0, ULONG_MAX)
1405 vma->vm_truncate_count = 0;
1406 list_for_each_entry(vma, &mapping->i_mmap_nonlinear, shared.vm_set.list)
1407 vma->vm_truncate_count = 0;
1410 static int unmap_mapping_range_vma(struct vm_area_struct *vma,
1411 unsigned long start_addr, unsigned long end_addr,
1412 struct zap_details *details)
1414 unsigned long restart_addr;
1418 restart_addr = vma->vm_truncate_count;
1419 if (is_restart_addr(restart_addr) && start_addr < restart_addr) {
1420 start_addr = restart_addr;
1421 if (start_addr >= end_addr) {
1422 /* Top of vma has been split off since last time */
1423 vma->vm_truncate_count = details->truncate_count;
1428 details->break_addr = end_addr;
1429 zap_page_range(vma, start_addr, end_addr - start_addr, details);
1432 * We cannot rely on the break test in unmap_vmas:
1433 * on the one hand, we don't want to restart our loop
1434 * just because that broke out for the page_table_lock;
1435 * on the other hand, it does no test when vma is small.
1437 need_break = need_resched() ||
1438 need_lockbreak(details->i_mmap_lock);
1440 if (details->break_addr >= end_addr) {
1441 /* We have now completed this vma: mark it so */
1442 vma->vm_truncate_count = details->truncate_count;
1446 /* Note restart_addr in vma's truncate_count field */
1447 vma->vm_truncate_count = details->break_addr;
1452 spin_unlock(details->i_mmap_lock);
1454 spin_lock(details->i_mmap_lock);
1458 static inline void unmap_mapping_range_tree(struct prio_tree_root *root,
1459 struct zap_details *details)
1461 struct vm_area_struct *vma;
1462 struct prio_tree_iter iter;
1463 pgoff_t vba, vea, zba, zea;
1466 vma_prio_tree_foreach(vma, &iter, root,
1467 details->first_index, details->last_index) {
1468 /* Skip quickly over those we have already dealt with */
1469 if (vma->vm_truncate_count == details->truncate_count)
1472 vba = vma->vm_pgoff;
1473 vea = vba + ((vma->vm_end - vma->vm_start) >> PAGE_SHIFT) - 1;
1474 /* Assume for now that PAGE_CACHE_SHIFT == PAGE_SHIFT */
1475 zba = details->first_index;
1478 zea = details->last_index;
1482 if (unmap_mapping_range_vma(vma,
1483 ((zba - vba) << PAGE_SHIFT) + vma->vm_start,
1484 ((zea - vba + 1) << PAGE_SHIFT) + vma->vm_start,
1490 static inline void unmap_mapping_range_list(struct list_head *head,
1491 struct zap_details *details)
1493 struct vm_area_struct *vma;
1496 * In nonlinear VMAs there is no correspondence between virtual address
1497 * offset and file offset. So we must perform an exhaustive search
1498 * across *all* the pages in each nonlinear VMA, not just the pages
1499 * whose virtual address lies outside the file truncation point.
1502 list_for_each_entry(vma, head, shared.vm_set.list) {
1503 /* Skip quickly over those we have already dealt with */
1504 if (vma->vm_truncate_count == details->truncate_count)
1506 details->nonlinear_vma = vma;
1507 if (unmap_mapping_range_vma(vma, vma->vm_start,
1508 vma->vm_end, details) < 0)
1514 * unmap_mapping_range - unmap the portion of all mmaps
1515 * in the specified address_space corresponding to the specified
1516 * page range in the underlying file.
1517 * @address_space: the address space containing mmaps to be unmapped.
1518 * @holebegin: byte in first page to unmap, relative to the start of
1519 * the underlying file. This will be rounded down to a PAGE_SIZE
1520 * boundary. Note that this is different from vmtruncate(), which
1521 * must keep the partial page. In contrast, we must get rid of
1523 * @holelen: size of prospective hole in bytes. This will be rounded
1524 * up to a PAGE_SIZE boundary. A holelen of zero truncates to the
1526 * @even_cows: 1 when truncating a file, unmap even private COWed pages;
1527 * but 0 when invalidating pagecache, don't throw away private data.
1529 void unmap_mapping_range(struct address_space *mapping,
1530 loff_t const holebegin, loff_t const holelen, int even_cows)
1532 struct zap_details details;
1533 pgoff_t hba = holebegin >> PAGE_SHIFT;
1534 pgoff_t hlen = (holelen + PAGE_SIZE - 1) >> PAGE_SHIFT;
1536 /* Check for overflow. */
1537 if (sizeof(holelen) > sizeof(hlen)) {
1539 (holebegin + holelen + PAGE_SIZE - 1) >> PAGE_SHIFT;
1540 if (holeend & ~(long long)ULONG_MAX)
1541 hlen = ULONG_MAX - hba + 1;
1544 details.check_mapping = even_cows? NULL: mapping;
1545 details.nonlinear_vma = NULL;
1546 details.first_index = hba;
1547 details.last_index = hba + hlen - 1;
1548 if (details.last_index < details.first_index)
1549 details.last_index = ULONG_MAX;
1550 details.i_mmap_lock = &mapping->i_mmap_lock;
1552 spin_lock(&mapping->i_mmap_lock);
1554 /* serialize i_size write against truncate_count write */
1556 /* Protect against page faults, and endless unmapping loops */
1557 mapping->truncate_count++;
1559 * For archs where spin_lock has inclusive semantics like ia64
1560 * this smp_mb() will prevent to read pagetable contents
1561 * before the truncate_count increment is visible to
1565 if (unlikely(is_restart_addr(mapping->truncate_count))) {
1566 if (mapping->truncate_count == 0)
1567 reset_vma_truncate_counts(mapping);
1568 mapping->truncate_count++;
1570 details.truncate_count = mapping->truncate_count;
1572 if (unlikely(!prio_tree_empty(&mapping->i_mmap)))
1573 unmap_mapping_range_tree(&mapping->i_mmap, &details);
1574 if (unlikely(!list_empty(&mapping->i_mmap_nonlinear)))
1575 unmap_mapping_range_list(&mapping->i_mmap_nonlinear, &details);
1576 spin_unlock(&mapping->i_mmap_lock);
1578 EXPORT_SYMBOL(unmap_mapping_range);
1581 * Handle all mappings that got truncated by a "truncate()"
1584 * NOTE! We have to be ready to update the memory sharing
1585 * between the file and the memory map for a potential last
1586 * incomplete page. Ugly, but necessary.
1588 int vmtruncate(struct inode * inode, loff_t offset)
1590 struct address_space *mapping = inode->i_mapping;
1591 unsigned long limit;
1593 if (inode->i_size < offset)
1596 * truncation of in-use swapfiles is disallowed - it would cause
1597 * subsequent swapout to scribble on the now-freed blocks.
1599 if (IS_SWAPFILE(inode))
1601 i_size_write(inode, offset);
1602 unmap_mapping_range(mapping, offset + PAGE_SIZE - 1, 0, 1);
1603 truncate_inode_pages(mapping, offset);
1607 limit = current->signal->rlim[RLIMIT_FSIZE].rlim_cur;
1608 if (limit != RLIM_INFINITY && offset > limit)
1610 if (offset > inode->i_sb->s_maxbytes)
1612 i_size_write(inode, offset);
1615 if (inode->i_op && inode->i_op->truncate)
1616 inode->i_op->truncate(inode);
1619 send_sig(SIGXFSZ, current, 0);
1626 EXPORT_SYMBOL(vmtruncate);
1629 * Primitive swap readahead code. We simply read an aligned block of
1630 * (1 << page_cluster) entries in the swap area. This method is chosen
1631 * because it doesn't cost us any seek time. We also make sure to queue
1632 * the 'original' request together with the readahead ones...
1634 * This has been extended to use the NUMA policies from the mm triggering
1637 * Caller must hold down_read on the vma->vm_mm if vma is not NULL.
1639 void swapin_readahead(swp_entry_t entry, unsigned long addr,struct vm_area_struct *vma)
1642 struct vm_area_struct *next_vma = vma ? vma->vm_next : NULL;
1645 struct page *new_page;
1646 unsigned long offset;
1649 * Get the number of handles we should do readahead io to.
1651 num = valid_swaphandles(entry, &offset);
1652 for (i = 0; i < num; offset++, i++) {
1653 /* Ok, do the async read-ahead now */
1654 new_page = read_swap_cache_async(swp_entry(swp_type(entry),
1655 offset), vma, addr);
1658 page_cache_release(new_page);
1661 * Find the next applicable VMA for the NUMA policy.
1667 if (addr >= vma->vm_end) {
1669 next_vma = vma ? vma->vm_next : NULL;
1671 if (vma && addr < vma->vm_start)
1674 if (next_vma && addr >= next_vma->vm_start) {
1676 next_vma = vma->vm_next;
1681 lru_add_drain(); /* Push any new pages onto the LRU now */
1685 * We hold the mm semaphore and the page_table_lock on entry and
1686 * should release the pagetable lock on exit..
1688 static int do_swap_page(struct mm_struct * mm,
1689 struct vm_area_struct * vma, unsigned long address,
1690 pte_t *page_table, pmd_t *pmd, pte_t orig_pte, int write_access)
1693 swp_entry_t entry = pte_to_swp_entry(orig_pte);
1695 int ret = VM_FAULT_MINOR;
1697 pte_unmap(page_table);
1698 spin_unlock(&mm->page_table_lock);
1699 page = lookup_swap_cache(entry);
1701 swapin_readahead(entry, address, vma);
1702 page = read_swap_cache_async(entry, vma, address);
1705 * Back out if somebody else faulted in this pte while
1706 * we released the page table lock.
1708 spin_lock(&mm->page_table_lock);
1709 page_table = pte_offset_map(pmd, address);
1710 if (likely(pte_same(*page_table, orig_pte)))
1713 ret = VM_FAULT_MINOR;
1714 pte_unmap(page_table);
1715 spin_unlock(&mm->page_table_lock);
1719 /* Had to read the page from swap area: Major fault */
1720 ret = VM_FAULT_MAJOR;
1721 inc_page_state(pgmajfault);
1725 if (!vx_rsspages_avail(mm, 1)) {
1729 mark_page_accessed(page);
1733 * Back out if somebody else faulted in this pte while we
1734 * released the page table lock.
1736 spin_lock(&mm->page_table_lock);
1737 page_table = pte_offset_map(pmd, address);
1738 if (unlikely(!pte_same(*page_table, orig_pte))) {
1739 pte_unmap(page_table);
1740 spin_unlock(&mm->page_table_lock);
1742 page_cache_release(page);
1743 ret = VM_FAULT_MINOR;
1747 /* The page isn't present yet, go ahead with the fault. */
1751 remove_exclusive_swap_page(page);
1753 vx_rsspages_inc(mm);
1754 acct_update_integrals();
1755 update_mem_hiwater();
1757 pte = mk_pte(page, vma->vm_page_prot);
1758 if (write_access && can_share_swap_page(page)) {
1759 pte = maybe_mkwrite(pte_mkdirty(pte), vma);
1764 flush_icache_page(vma, page);
1765 set_pte(page_table, pte);
1766 page_add_anon_rmap(page, vma, address);
1769 if (do_wp_page(mm, vma, address,
1770 page_table, pmd, pte) == VM_FAULT_OOM)
1775 /* No need to invalidate - it was non-present before */
1776 update_mmu_cache(vma, address, pte);
1777 pte_unmap(page_table);
1778 spin_unlock(&mm->page_table_lock);
1784 * We are called with the MM semaphore and page_table_lock
1785 * spinlock held to protect against concurrent faults in
1786 * multithreaded programs.
1789 do_anonymous_page(struct mm_struct *mm, struct vm_area_struct *vma,
1790 pte_t *page_table, pmd_t *pmd, int write_access,
1794 struct page * page = ZERO_PAGE(addr);
1796 /* Read-only mapping of ZERO_PAGE. */
1797 entry = pte_wrprotect(mk_pte(ZERO_PAGE(addr), vma->vm_page_prot));
1799 /* ..except if it's a write access */
1801 /* Allocate our own private page. */
1802 pte_unmap(page_table);
1803 spin_unlock(&mm->page_table_lock);
1805 if (!vx_rsspages_avail(mm, 1))
1807 if (unlikely(anon_vma_prepare(vma)))
1809 page = alloc_zeroed_user_highpage(vma, addr);
1813 spin_lock(&mm->page_table_lock);
1814 page_table = pte_offset_map(pmd, addr);
1816 if (!pte_none(*page_table)) {
1817 pte_unmap(page_table);
1818 page_cache_release(page);
1819 spin_unlock(&mm->page_table_lock);
1822 vx_rsspages_inc(mm);
1823 acct_update_integrals();
1824 update_mem_hiwater();
1825 entry = maybe_mkwrite(pte_mkdirty(mk_pte(page,
1826 vma->vm_page_prot)),
1828 lru_cache_add_active(page);
1829 SetPageReferenced(page);
1830 page_add_anon_rmap(page, vma, addr);
1833 set_pte(page_table, entry);
1834 pte_unmap(page_table);
1836 /* No need to invalidate - it was non-present before */
1837 update_mmu_cache(vma, addr, entry);
1838 spin_unlock(&mm->page_table_lock);
1840 return VM_FAULT_MINOR;
1842 return VM_FAULT_OOM;
1846 * do_no_page() tries to create a new page mapping. It aggressively
1847 * tries to share with existing pages, but makes a separate copy if
1848 * the "write_access" parameter is true in order to avoid the next
1851 * As this is called only for pages that do not currently exist, we
1852 * do not need to flush old virtual caches or the TLB.
1854 * This is called with the MM semaphore held and the page table
1855 * spinlock held. Exit with the spinlock released.
1858 do_no_page(struct mm_struct *mm, struct vm_area_struct *vma,
1859 unsigned long address, int write_access, pte_t *page_table, pmd_t *pmd)
1861 struct page * new_page;
1862 struct address_space *mapping = NULL;
1864 unsigned int sequence = 0;
1865 int ret = VM_FAULT_MINOR;
1868 if (!vma->vm_ops || !vma->vm_ops->nopage)
1869 return do_anonymous_page(mm, vma, page_table,
1870 pmd, write_access, address);
1871 pte_unmap(page_table);
1872 spin_unlock(&mm->page_table_lock);
1875 mapping = vma->vm_file->f_mapping;
1876 sequence = mapping->truncate_count;
1877 smp_rmb(); /* serializes i_size against truncate_count */
1881 /* FIXME: is that check useful here? */
1882 if (!vx_rsspages_avail(mm, 1))
1883 return VM_FAULT_OOM;
1884 new_page = vma->vm_ops->nopage(vma, address & PAGE_MASK, &ret);
1886 * No smp_rmb is needed here as long as there's a full
1887 * spin_lock/unlock sequence inside the ->nopage callback
1888 * (for the pagecache lookup) that acts as an implicit
1889 * smp_mb() and prevents the i_size read to happen
1890 * after the next truncate_count read.
1893 /* no page was available -- either SIGBUS or OOM */
1894 if (new_page == NOPAGE_SIGBUS)
1895 return VM_FAULT_SIGBUS;
1896 if (new_page == NOPAGE_OOM)
1897 return VM_FAULT_OOM;
1900 * Should we do an early C-O-W break?
1902 if (write_access && !(vma->vm_flags & VM_SHARED)) {
1905 if (unlikely(anon_vma_prepare(vma)))
1907 page = alloc_page_vma(GFP_HIGHUSER, vma, address);
1910 copy_user_highpage(page, new_page, address);
1911 page_cache_release(new_page);
1916 spin_lock(&mm->page_table_lock);
1918 * For a file-backed vma, someone could have truncated or otherwise
1919 * invalidated this page. If unmap_mapping_range got called,
1920 * retry getting the page.
1922 if (mapping && unlikely(sequence != mapping->truncate_count)) {
1923 sequence = mapping->truncate_count;
1924 spin_unlock(&mm->page_table_lock);
1925 page_cache_release(new_page);
1928 page_table = pte_offset_map(pmd, address);
1931 * This silly early PAGE_DIRTY setting removes a race
1932 * due to the bad i386 page protection. But it's valid
1933 * for other architectures too.
1935 * Note that if write_access is true, we either now have
1936 * an exclusive copy of the page, or this is a shared mapping,
1937 * so we can make it writable and dirty to avoid having to
1938 * handle that later.
1940 /* Only go through if we didn't race with anybody else... */
1941 if (pte_none(*page_table)) {
1942 if (!PageReserved(new_page))
1943 vx_rsspages_inc(mm);
1944 acct_update_integrals();
1945 update_mem_hiwater();
1947 flush_icache_page(vma, new_page);
1948 entry = mk_pte(new_page, vma->vm_page_prot);
1950 entry = maybe_mkwrite(pte_mkdirty(entry), vma);
1951 set_pte(page_table, entry);
1953 lru_cache_add_active(new_page);
1954 page_add_anon_rmap(new_page, vma, address);
1956 page_add_file_rmap(new_page);
1957 pte_unmap(page_table);
1959 /* One of our sibling threads was faster, back out. */
1960 pte_unmap(page_table);
1961 page_cache_release(new_page);
1962 spin_unlock(&mm->page_table_lock);
1966 /* no need to invalidate: a not-present page shouldn't be cached */
1967 update_mmu_cache(vma, address, entry);
1968 spin_unlock(&mm->page_table_lock);
1972 page_cache_release(new_page);
1978 * Fault of a previously existing named mapping. Repopulate the pte
1979 * from the encoded file_pte if possible. This enables swappable
1982 static int do_file_page(struct mm_struct * mm, struct vm_area_struct * vma,
1983 unsigned long address, int write_access, pte_t *pte, pmd_t *pmd)
1985 unsigned long pgoff;
1988 BUG_ON(!vma->vm_ops || !vma->vm_ops->nopage);
1990 * Fall back to the linear mapping if the fs does not support
1993 if (!vma->vm_ops || !vma->vm_ops->populate ||
1994 (write_access && !(vma->vm_flags & VM_SHARED))) {
1996 return do_no_page(mm, vma, address, write_access, pte, pmd);
1999 pgoff = pte_to_pgoff(*pte);
2002 spin_unlock(&mm->page_table_lock);
2004 err = vma->vm_ops->populate(vma, address & PAGE_MASK, PAGE_SIZE, vma->vm_page_prot, pgoff, 0);
2006 return VM_FAULT_OOM;
2008 return VM_FAULT_SIGBUS;
2009 return VM_FAULT_MAJOR;
2013 * These routines also need to handle stuff like marking pages dirty
2014 * and/or accessed for architectures that don't do it in hardware (most
2015 * RISC architectures). The early dirtying is also good on the i386.
2017 * There is also a hook called "update_mmu_cache()" that architectures
2018 * with external mmu caches can use to update those (ie the Sparc or
2019 * PowerPC hashed page tables that act as extended TLBs).
2021 * Note the "page_table_lock". It is to protect against kswapd removing
2022 * pages from under us. Note that kswapd only ever _removes_ pages, never
2023 * adds them. As such, once we have noticed that the page is not present,
2024 * we can drop the lock early.
2026 * The adding of pages is protected by the MM semaphore (which we hold),
2027 * so we don't need to worry about a page being suddenly been added into
2030 * We enter with the pagetable spinlock held, we are supposed to
2031 * release it when done.
2033 static inline int handle_pte_fault(struct mm_struct *mm,
2034 struct vm_area_struct * vma, unsigned long address,
2035 int write_access, pte_t *pte, pmd_t *pmd)
2040 if (!pte_present(entry)) {
2042 * If it truly wasn't present, we know that kswapd
2043 * and the PTE updates will not touch it later. So
2046 if (pte_none(entry))
2047 return do_no_page(mm, vma, address, write_access, pte, pmd);
2048 if (pte_file(entry))
2049 return do_file_page(mm, vma, address, write_access, pte, pmd);
2050 return do_swap_page(mm, vma, address, pte, pmd, entry, write_access);
2054 if (!pte_write(entry))
2055 return do_wp_page(mm, vma, address, pte, pmd, entry);
2057 entry = pte_mkdirty(entry);
2059 entry = pte_mkyoung(entry);
2060 ptep_set_access_flags(vma, address, pte, entry, write_access);
2061 update_mmu_cache(vma, address, entry);
2063 spin_unlock(&mm->page_table_lock);
2064 return VM_FAULT_MINOR;
2068 * By the time we get here, we already hold the mm semaphore
2070 int handle_mm_fault(struct mm_struct *mm, struct vm_area_struct * vma,
2071 unsigned long address, int write_access)
2078 __set_current_state(TASK_RUNNING);
2080 inc_page_state(pgfault);
2082 if (is_vm_hugetlb_page(vma))
2083 return VM_FAULT_SIGBUS; /* mapping truncation does this. */
2086 * We need the page table lock to synchronize with kswapd
2087 * and the SMP-safe atomic PTE updates.
2089 pgd = pgd_offset(mm, address);
2090 spin_lock(&mm->page_table_lock);
2092 pud = pud_alloc(mm, pgd, address);
2096 pmd = pmd_alloc(mm, pud, address);
2100 pte = pte_alloc_map(mm, pmd, address);
2104 return handle_pte_fault(mm, vma, address, write_access, pte, pmd);
2107 spin_unlock(&mm->page_table_lock);
2108 return VM_FAULT_OOM;
2111 #ifndef __ARCH_HAS_4LEVEL_HACK
2113 * Allocate page upper directory.
2115 * We've already handled the fast-path in-line, and we own the
2118 * On a two-level or three-level page table, this ends up actually being
2119 * entirely optimized away.
2121 pud_t fastcall *__pud_alloc(struct mm_struct *mm, pgd_t *pgd, unsigned long address)
2125 spin_unlock(&mm->page_table_lock);
2126 new = pud_alloc_one(mm, address);
2127 spin_lock(&mm->page_table_lock);
2132 * Because we dropped the lock, we should re-check the
2133 * entry, as somebody else could have populated it..
2135 if (pgd_present(*pgd)) {
2139 pgd_populate(mm, pgd, new);
2141 return pud_offset(pgd, address);
2145 * Allocate page middle directory.
2147 * We've already handled the fast-path in-line, and we own the
2150 * On a two-level page table, this ends up actually being entirely
2153 pmd_t fastcall *__pmd_alloc(struct mm_struct *mm, pud_t *pud, unsigned long address)
2157 spin_unlock(&mm->page_table_lock);
2158 new = pmd_alloc_one(mm, address);
2159 spin_lock(&mm->page_table_lock);
2164 * Because we dropped the lock, we should re-check the
2165 * entry, as somebody else could have populated it..
2167 if (pud_present(*pud)) {
2171 pud_populate(mm, pud, new);
2173 return pmd_offset(pud, address);
2176 pmd_t fastcall *__pmd_alloc(struct mm_struct *mm, pud_t *pud, unsigned long address)
2180 spin_unlock(&mm->page_table_lock);
2181 new = pmd_alloc_one(mm, address);
2182 spin_lock(&mm->page_table_lock);
2187 * Because we dropped the lock, we should re-check the
2188 * entry, as somebody else could have populated it..
2190 if (pgd_present(*pud)) {
2194 pgd_populate(mm, pud, new);
2196 return pmd_offset(pud, address);
2200 int make_pages_present(unsigned long addr, unsigned long end)
2202 int ret, len, write;
2203 struct vm_area_struct * vma;
2205 vma = find_vma(current->mm, addr);
2208 write = (vma->vm_flags & VM_WRITE) != 0;
2211 if (end > vma->vm_end)
2213 len = (end+PAGE_SIZE-1)/PAGE_SIZE-addr/PAGE_SIZE;
2214 ret = get_user_pages(current, current->mm, addr,
2215 len, write, 0, NULL, NULL);
2218 return ret == len ? 0 : -1;
2222 * Map a vmalloc()-space virtual address to the physical page.
2224 struct page * vmalloc_to_page(void * vmalloc_addr)
2226 unsigned long addr = (unsigned long) vmalloc_addr;
2227 struct page *page = NULL;
2228 pgd_t *pgd = pgd_offset_k(addr);
2233 if (!pgd_none(*pgd)) {
2234 pud = pud_offset(pgd, addr);
2235 if (!pud_none(*pud)) {
2236 pmd = pmd_offset(pud, addr);
2237 if (!pmd_none(*pmd)) {
2238 ptep = pte_offset_map(pmd, addr);
2240 if (pte_present(pte))
2241 page = pte_page(pte);
2249 EXPORT_SYMBOL(vmalloc_to_page);
2252 * Map a vmalloc()-space virtual address to the physical page frame number.
2254 unsigned long vmalloc_to_pfn(void * vmalloc_addr)
2256 return page_to_pfn(vmalloc_to_page(vmalloc_addr));
2259 EXPORT_SYMBOL(vmalloc_to_pfn);
2262 * update_mem_hiwater
2263 * - update per process rss and vm high water data
2265 void update_mem_hiwater(void)
2267 struct task_struct *tsk = current;
2270 if (tsk->mm->hiwater_rss < tsk->mm->rss)
2271 tsk->mm->hiwater_rss = tsk->mm->rss;
2272 if (tsk->mm->hiwater_vm < tsk->mm->total_vm)
2273 tsk->mm->hiwater_vm = tsk->mm->total_vm;
2277 #if !defined(__HAVE_ARCH_GATE_AREA)
2279 #if defined(AT_SYSINFO_EHDR)
2280 struct vm_area_struct gate_vma;
2282 static int __init gate_vma_init(void)
2284 gate_vma.vm_mm = NULL;
2285 gate_vma.vm_start = FIXADDR_USER_START;
2286 gate_vma.vm_end = FIXADDR_USER_END;
2287 gate_vma.vm_page_prot = PAGE_READONLY;
2288 gate_vma.vm_flags = 0;
2291 __initcall(gate_vma_init);
2294 struct vm_area_struct *get_gate_vma(struct task_struct *tsk)
2296 #ifdef AT_SYSINFO_EHDR
2303 int in_gate_area_no_task(unsigned long addr)
2305 #ifdef AT_SYSINFO_EHDR
2306 if ((addr >= FIXADDR_USER_START) && (addr < FIXADDR_USER_END))
2312 #endif /* __HAVE_ARCH_GATE_AREA */