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);
317 vx_rsspages_inc(dst_mm);
319 // dst_mm->anon_rss++;
320 vx_anonpages_inc(dst_mm);
321 set_pte(dst_pte, pte);
325 static int copy_pte_range(struct mm_struct *dst_mm, struct mm_struct *src_mm,
326 pmd_t *dst_pmd, pmd_t *src_pmd, struct vm_area_struct *vma,
327 unsigned long addr, unsigned long end)
329 pte_t *src_pte, *dst_pte;
331 unsigned long vm_flags = vma->vm_flags;
333 d = dst_pte = pte_alloc_map(dst_mm, dst_pmd, addr);
337 spin_lock(&src_mm->page_table_lock);
338 s = src_pte = pte_offset_map_nested(src_pmd, addr);
339 for (; addr < end; addr += PAGE_SIZE, s++, d++) {
342 copy_one_pte(dst_mm, src_mm, d, s, vm_flags, addr);
344 pte_unmap_nested(src_pte);
346 spin_unlock(&src_mm->page_table_lock);
347 cond_resched_lock(&dst_mm->page_table_lock);
351 static int copy_pmd_range(struct mm_struct *dst_mm, struct mm_struct *src_mm,
352 pud_t *dst_pud, pud_t *src_pud, struct vm_area_struct *vma,
353 unsigned long addr, unsigned long end)
355 pmd_t *src_pmd, *dst_pmd;
359 src_pmd = pmd_offset(src_pud, addr);
360 dst_pmd = pmd_alloc(dst_mm, dst_pud, addr);
364 for (; addr < end; addr = next, src_pmd++, dst_pmd++) {
365 next = (addr + PMD_SIZE) & PMD_MASK;
366 if (next > end || next <= addr)
368 if (pmd_none(*src_pmd))
370 if (pmd_bad(*src_pmd)) {
375 err = copy_pte_range(dst_mm, src_mm, dst_pmd, src_pmd,
383 static int copy_pud_range(struct mm_struct *dst_mm, struct mm_struct *src_mm,
384 pgd_t *dst_pgd, pgd_t *src_pgd, struct vm_area_struct *vma,
385 unsigned long addr, unsigned long end)
387 pud_t *src_pud, *dst_pud;
391 src_pud = pud_offset(src_pgd, addr);
392 dst_pud = pud_alloc(dst_mm, dst_pgd, addr);
396 for (; addr < end; addr = next, src_pud++, dst_pud++) {
397 next = (addr + PUD_SIZE) & PUD_MASK;
398 if (next > end || next <= addr)
400 if (pud_none(*src_pud))
402 if (pud_bad(*src_pud)) {
407 err = copy_pmd_range(dst_mm, src_mm, dst_pud, src_pud,
415 int copy_page_range(struct mm_struct *dst, struct mm_struct *src,
416 struct vm_area_struct *vma)
418 pgd_t *src_pgd, *dst_pgd;
419 unsigned long addr, start, end, next;
422 if (is_vm_hugetlb_page(vma))
423 return copy_hugetlb_page_range(dst, src, vma);
425 start = vma->vm_start;
426 src_pgd = pgd_offset(src, start);
427 dst_pgd = pgd_offset(dst, start);
431 while (addr && (addr < end-1)) {
432 next = (addr + PGDIR_SIZE) & PGDIR_MASK;
433 if (next > end || next <= addr)
435 if (pgd_none(*src_pgd))
437 if (pgd_bad(*src_pgd)) {
442 err = copy_pud_range(dst, src, dst_pgd, src_pgd,
456 static void zap_pte_range(struct mmu_gather *tlb,
457 pmd_t *pmd, unsigned long address,
458 unsigned long size, struct zap_details *details)
460 unsigned long offset;
465 if (unlikely(pmd_bad(*pmd))) {
470 ptep = pte_offset_map(pmd, address);
471 offset = address & ~PMD_MASK;
472 if (offset + size > PMD_SIZE)
473 size = PMD_SIZE - offset;
475 if (details && !details->check_mapping && !details->nonlinear_vma)
477 for (offset=0; offset < size; ptep++, offset += PAGE_SIZE) {
481 if (pte_present(pte)) {
482 struct page *page = NULL;
483 unsigned long pfn = pte_pfn(pte);
484 if (pfn_valid(pfn)) {
485 page = pfn_to_page(pfn);
486 if (PageReserved(page))
489 if (unlikely(details) && page) {
491 * unmap_shared_mapping_pages() wants to
492 * invalidate cache without truncating:
493 * unmap shared but keep private pages.
495 if (details->check_mapping &&
496 details->check_mapping != page->mapping)
499 * Each page->index must be checked when
500 * invalidating or truncating nonlinear.
502 if (details->nonlinear_vma &&
503 (page->index < details->first_index ||
504 page->index > details->last_index))
507 pte = ptep_get_and_clear(ptep);
508 tlb_remove_tlb_entry(tlb, ptep, address+offset);
511 if (unlikely(details) && details->nonlinear_vma
512 && linear_page_index(details->nonlinear_vma,
513 address+offset) != page->index)
514 set_pte(ptep, pgoff_to_pte(page->index));
516 set_page_dirty(page);
518 // tlb->mm->anon_rss--;
519 vx_anonpages_dec(tlb->mm);
520 else if (pte_young(pte))
521 mark_page_accessed(page);
523 page_remove_rmap(page);
524 tlb_remove_page(tlb, page);
528 * If details->check_mapping, we leave swap entries;
529 * if details->nonlinear_vma, we leave file entries.
531 if (unlikely(details))
534 free_swap_and_cache(pte_to_swp_entry(pte));
540 static void zap_pmd_range(struct mmu_gather *tlb,
541 pud_t *pud, unsigned long address,
542 unsigned long size, struct zap_details *details)
549 if (unlikely(pud_bad(*pud))) {
554 pmd = pmd_offset(pud, address);
555 end = address + size;
556 if (end > ((address + PUD_SIZE) & PUD_MASK))
557 end = ((address + PUD_SIZE) & PUD_MASK);
559 zap_pte_range(tlb, pmd, address, end - address, details);
560 address = (address + PMD_SIZE) & PMD_MASK;
562 } while (address && (address < end));
565 static void zap_pud_range(struct mmu_gather *tlb,
566 pgd_t * pgd, unsigned long address,
567 unsigned long end, struct zap_details *details)
573 if (unlikely(pgd_bad(*pgd))) {
578 pud = pud_offset(pgd, address);
580 zap_pmd_range(tlb, pud, address, end - address, details);
581 address = (address + PUD_SIZE) & PUD_MASK;
583 } while (address && (address < end));
586 static void unmap_page_range(struct mmu_gather *tlb,
587 struct vm_area_struct *vma, unsigned long address,
588 unsigned long end, struct zap_details *details)
594 BUG_ON(address >= end);
595 pgd = pgd_offset(vma->vm_mm, address);
596 tlb_start_vma(tlb, vma);
597 for (i = pgd_index(address); i <= pgd_index(end-1); i++) {
598 next = (address + PGDIR_SIZE) & PGDIR_MASK;
599 if (next <= address || next > end)
601 zap_pud_range(tlb, pgd, address, next, details);
605 tlb_end_vma(tlb, vma);
608 #ifdef CONFIG_PREEMPT
609 # define ZAP_BLOCK_SIZE (8 * PAGE_SIZE)
611 /* No preempt: go for improved straight-line efficiency */
612 # define ZAP_BLOCK_SIZE (1024 * PAGE_SIZE)
616 * unmap_vmas - unmap a range of memory covered by a list of vma's
617 * @tlbp: address of the caller's struct mmu_gather
618 * @mm: the controlling mm_struct
619 * @vma: the starting vma
620 * @start_addr: virtual address at which to start unmapping
621 * @end_addr: virtual address at which to end unmapping
622 * @nr_accounted: Place number of unmapped pages in vm-accountable vma's here
623 * @details: details of nonlinear truncation or shared cache invalidation
625 * Returns the number of vma's which were covered by the unmapping.
627 * Unmap all pages in the vma list. Called under page_table_lock.
629 * We aim to not hold page_table_lock for too long (for scheduling latency
630 * reasons). So zap pages in ZAP_BLOCK_SIZE bytecounts. This means we need to
631 * return the ending mmu_gather to the caller.
633 * Only addresses between `start' and `end' will be unmapped.
635 * The VMA list must be sorted in ascending virtual address order.
637 * unmap_vmas() assumes that the caller will flush the whole unmapped address
638 * range after unmap_vmas() returns. So the only responsibility here is to
639 * ensure that any thus-far unmapped pages are flushed before unmap_vmas()
640 * drops the lock and schedules.
642 int unmap_vmas(struct mmu_gather **tlbp, struct mm_struct *mm,
643 struct vm_area_struct *vma, unsigned long start_addr,
644 unsigned long end_addr, unsigned long *nr_accounted,
645 struct zap_details *details)
647 unsigned long zap_bytes = ZAP_BLOCK_SIZE;
648 unsigned long tlb_start = 0; /* For tlb_finish_mmu */
649 int tlb_start_valid = 0;
651 spinlock_t *i_mmap_lock = details? details->i_mmap_lock: NULL;
652 int fullmm = tlb_is_full_mm(*tlbp);
654 for ( ; vma && vma->vm_start < end_addr; vma = vma->vm_next) {
658 start = max(vma->vm_start, start_addr);
659 if (start >= vma->vm_end)
661 end = min(vma->vm_end, end_addr);
662 if (end <= vma->vm_start)
665 if (vma->vm_flags & VM_ACCOUNT)
666 *nr_accounted += (end - start) >> PAGE_SHIFT;
669 while (start != end) {
672 if (!tlb_start_valid) {
677 if (is_vm_hugetlb_page(vma)) {
679 unmap_hugepage_range(vma, start, end);
681 block = min(zap_bytes, end - start);
682 unmap_page_range(*tlbp, vma, start,
683 start + block, details);
688 if ((long)zap_bytes > 0)
691 tlb_finish_mmu(*tlbp, tlb_start, start);
693 if (need_resched() ||
694 need_lockbreak(&mm->page_table_lock) ||
695 (i_mmap_lock && need_lockbreak(i_mmap_lock))) {
697 /* must reset count of rss freed */
698 *tlbp = tlb_gather_mmu(mm, fullmm);
699 details->break_addr = start;
702 spin_unlock(&mm->page_table_lock);
704 spin_lock(&mm->page_table_lock);
707 *tlbp = tlb_gather_mmu(mm, fullmm);
709 zap_bytes = ZAP_BLOCK_SIZE;
717 * zap_page_range - remove user pages in a given range
718 * @vma: vm_area_struct holding the applicable pages
719 * @address: starting address of pages to zap
720 * @size: number of bytes to zap
721 * @details: details of nonlinear truncation or shared cache invalidation
723 void zap_page_range(struct vm_area_struct *vma, unsigned long address,
724 unsigned long size, struct zap_details *details)
726 struct mm_struct *mm = vma->vm_mm;
727 struct mmu_gather *tlb;
728 unsigned long end = address + size;
729 unsigned long nr_accounted = 0;
731 if (is_vm_hugetlb_page(vma)) {
732 zap_hugepage_range(vma, address, size);
737 spin_lock(&mm->page_table_lock);
738 tlb = tlb_gather_mmu(mm, 0);
739 unmap_vmas(&tlb, mm, vma, address, end, &nr_accounted, details);
740 tlb_finish_mmu(tlb, address, end);
741 acct_update_integrals();
742 spin_unlock(&mm->page_table_lock);
746 * Do a quick page-table lookup for a single page.
747 * mm->page_table_lock must be held.
750 __follow_page(struct mm_struct *mm, unsigned long address, int read, int write)
759 page = follow_huge_addr(mm, address, write);
763 pgd = pgd_offset(mm, address);
764 if (pgd_none(*pgd) || unlikely(pgd_bad(*pgd)))
767 pud = pud_offset(pgd, address);
768 if (pud_none(*pud) || unlikely(pud_bad(*pud)))
771 pmd = pmd_offset(pud, address);
772 if (pmd_none(*pmd) || unlikely(pmd_bad(*pmd)))
775 return follow_huge_pmd(mm, address, pmd, write);
777 ptep = pte_offset_map(pmd, address);
783 if (pte_present(pte)) {
784 if (write && !pte_write(pte))
786 if (read && !pte_read(pte))
789 if (pfn_valid(pfn)) {
790 page = pfn_to_page(pfn);
791 if (write && !pte_dirty(pte) && !PageDirty(page))
792 set_page_dirty(page);
793 mark_page_accessed(page);
803 follow_page(struct mm_struct *mm, unsigned long address, int write)
805 return __follow_page(mm, address, /*read*/0, write);
809 check_user_page_readable(struct mm_struct *mm, unsigned long address)
811 return __follow_page(mm, address, /*read*/1, /*write*/0) != NULL;
814 EXPORT_SYMBOL(check_user_page_readable);
817 * Given a physical address, is there a useful struct page pointing to
818 * it? This may become more complex in the future if we start dealing
819 * with IO-aperture pages for direct-IO.
822 static inline struct page *get_page_map(struct page *page)
824 if (!pfn_valid(page_to_pfn(page)))
831 untouched_anonymous_page(struct mm_struct* mm, struct vm_area_struct *vma,
832 unsigned long address)
838 /* Check if the vma is for an anonymous mapping. */
839 if (vma->vm_ops && vma->vm_ops->nopage)
842 /* Check if page directory entry exists. */
843 pgd = pgd_offset(mm, address);
844 if (pgd_none(*pgd) || unlikely(pgd_bad(*pgd)))
847 pud = pud_offset(pgd, address);
848 if (pud_none(*pud) || unlikely(pud_bad(*pud)))
851 /* Check if page middle directory entry exists. */
852 pmd = pmd_offset(pud, address);
853 if (pmd_none(*pmd) || unlikely(pmd_bad(*pmd)))
856 /* There is a pte slot for 'address' in 'mm'. */
861 int get_user_pages(struct task_struct *tsk, struct mm_struct *mm,
862 unsigned long start, int len, int write, int force,
863 struct page **pages, struct vm_area_struct **vmas)
869 * Require read or write permissions.
870 * If 'force' is set, we only require the "MAY" flags.
872 flags = write ? (VM_WRITE | VM_MAYWRITE) : (VM_READ | VM_MAYREAD);
873 flags &= force ? (VM_MAYREAD | VM_MAYWRITE) : (VM_READ | VM_WRITE);
877 struct vm_area_struct * vma;
879 vma = find_extend_vma(mm, start);
880 if (!vma && in_gate_area(tsk, start)) {
881 unsigned long pg = start & PAGE_MASK;
882 struct vm_area_struct *gate_vma = get_gate_vma(tsk);
887 if (write) /* user gate pages are read-only */
888 return i ? : -EFAULT;
890 pgd = pgd_offset_k(pg);
892 pgd = pgd_offset_gate(mm, pg);
893 BUG_ON(pgd_none(*pgd));
894 pud = pud_offset(pgd, pg);
895 BUG_ON(pud_none(*pud));
896 pmd = pmd_offset(pud, pg);
897 BUG_ON(pmd_none(*pmd));
898 pte = pte_offset_map(pmd, pg);
899 BUG_ON(pte_none(*pte));
901 pages[i] = pte_page(*pte);
913 if (!vma || (vma->vm_flags & VM_IO)
914 || !(flags & vma->vm_flags))
915 return i ? : -EFAULT;
917 if (is_vm_hugetlb_page(vma)) {
918 i = follow_hugetlb_page(mm, vma, pages, vmas,
922 spin_lock(&mm->page_table_lock);
925 int lookup_write = write;
927 cond_resched_lock(&mm->page_table_lock);
928 while (!(map = follow_page(mm, start, lookup_write))) {
930 * Shortcut for anonymous pages. We don't want
931 * to force the creation of pages tables for
932 * insanly big anonymously mapped areas that
933 * nobody touched so far. This is important
934 * for doing a core dump for these mappings.
937 untouched_anonymous_page(mm,vma,start)) {
938 map = ZERO_PAGE(start);
941 spin_unlock(&mm->page_table_lock);
942 switch (handle_mm_fault(mm,vma,start,write)) {
949 case VM_FAULT_SIGBUS:
950 return i ? i : -EFAULT;
952 return i ? i : -ENOMEM;
957 * Now that we have performed a write fault
958 * and surely no longer have a shared page we
959 * shouldn't write, we shouldn't ignore an
960 * unwritable page in the page table if
961 * we are forcing write access.
963 lookup_write = write && !force;
964 spin_lock(&mm->page_table_lock);
967 pages[i] = get_page_map(map);
969 spin_unlock(&mm->page_table_lock);
971 page_cache_release(pages[i]);
975 flush_dcache_page(pages[i]);
976 if (!PageReserved(pages[i]))
977 page_cache_get(pages[i]);
984 } while(len && start < vma->vm_end);
985 spin_unlock(&mm->page_table_lock);
991 EXPORT_SYMBOL(get_user_pages);
993 static void zeromap_pte_range(pte_t * pte, unsigned long address,
994 unsigned long size, pgprot_t prot)
998 address &= ~PMD_MASK;
999 end = address + size;
1003 pte_t zero_pte = pte_wrprotect(mk_pte(ZERO_PAGE(address), prot));
1004 BUG_ON(!pte_none(*pte));
1005 set_pte(pte, zero_pte);
1006 address += PAGE_SIZE;
1008 } while (address && (address < end));
1011 static inline int zeromap_pmd_range(struct mm_struct *mm, pmd_t * pmd,
1012 unsigned long address, unsigned long size, pgprot_t prot)
1014 unsigned long base, end;
1016 base = address & PUD_MASK;
1017 address &= ~PUD_MASK;
1018 end = address + size;
1022 pte_t * pte = pte_alloc_map(mm, pmd, base + address);
1025 zeromap_pte_range(pte, base + address, end - address, prot);
1027 address = (address + PMD_SIZE) & PMD_MASK;
1029 } while (address && (address < end));
1033 static inline int zeromap_pud_range(struct mm_struct *mm, pud_t * pud,
1034 unsigned long address,
1035 unsigned long size, pgprot_t prot)
1037 unsigned long base, end;
1040 base = address & PGDIR_MASK;
1041 address &= ~PGDIR_MASK;
1042 end = address + size;
1043 if (end > PGDIR_SIZE)
1046 pmd_t * pmd = pmd_alloc(mm, pud, base + address);
1050 error = zeromap_pmd_range(mm, pmd, base + address,
1051 end - address, prot);
1054 address = (address + PUD_SIZE) & PUD_MASK;
1056 } while (address && (address < end));
1060 int zeromap_page_range(struct vm_area_struct *vma, unsigned long address,
1061 unsigned long size, pgprot_t prot)
1066 unsigned long beg = address;
1067 unsigned long end = address + size;
1069 struct mm_struct *mm = vma->vm_mm;
1071 pgd = pgd_offset(mm, address);
1072 flush_cache_range(vma, beg, end);
1073 BUG_ON(address >= end);
1074 BUG_ON(end > vma->vm_end);
1076 spin_lock(&mm->page_table_lock);
1077 for (i = pgd_index(address); i <= pgd_index(end-1); i++) {
1078 pud_t *pud = pud_alloc(mm, pgd, address);
1082 next = (address + PGDIR_SIZE) & PGDIR_MASK;
1083 if (next <= beg || next > end)
1085 error = zeromap_pud_range(mm, pud, address,
1086 next - address, prot);
1093 * Why flush? zeromap_pte_range has a BUG_ON for !pte_none()
1095 flush_tlb_range(vma, beg, end);
1096 spin_unlock(&mm->page_table_lock);
1101 * maps a range of physical memory into the requested pages. the old
1102 * mappings are removed. any references to nonexistent pages results
1103 * in null mappings (currently treated as "copy-on-access")
1106 remap_pte_range(pte_t * pte, unsigned long address, unsigned long size,
1107 unsigned long pfn, pgprot_t prot)
1111 address &= ~PMD_MASK;
1112 end = address + size;
1116 BUG_ON(!pte_none(*pte));
1117 if (!pfn_valid(pfn) || PageReserved(pfn_to_page(pfn)))
1118 set_pte(pte, pfn_pte(pfn, prot));
1119 address += PAGE_SIZE;
1122 } while (address && (address < end));
1126 remap_pmd_range(struct mm_struct *mm, pmd_t * pmd, unsigned long address,
1127 unsigned long size, unsigned long pfn, pgprot_t prot)
1129 unsigned long base, end;
1131 base = address & PUD_MASK;
1132 address &= ~PUD_MASK;
1133 end = address + size;
1136 pfn -= (address >> PAGE_SHIFT);
1138 pte_t * pte = pte_alloc_map(mm, pmd, base + address);
1141 remap_pte_range(pte, base + address, end - address,
1142 (address >> PAGE_SHIFT) + pfn, prot);
1144 address = (address + PMD_SIZE) & PMD_MASK;
1146 } while (address && (address < end));
1150 static inline int remap_pud_range(struct mm_struct *mm, pud_t * pud,
1151 unsigned long address, unsigned long size,
1152 unsigned long pfn, pgprot_t prot)
1154 unsigned long base, end;
1157 base = address & PGDIR_MASK;
1158 address &= ~PGDIR_MASK;
1159 end = address + size;
1160 if (end > PGDIR_SIZE)
1162 pfn -= address >> PAGE_SHIFT;
1164 pmd_t *pmd = pmd_alloc(mm, pud, base+address);
1168 error = remap_pmd_range(mm, pmd, base + address, end - address,
1169 (address >> PAGE_SHIFT) + pfn, prot);
1172 address = (address + PUD_SIZE) & PUD_MASK;
1174 } while (address && (address < end));
1178 /* Note: this is only safe if the mm semaphore is held when called. */
1179 int remap_pfn_range(struct vm_area_struct *vma, unsigned long from,
1180 unsigned long pfn, unsigned long size, pgprot_t prot)
1184 unsigned long beg = from;
1185 unsigned long end = from + size;
1187 struct mm_struct *mm = vma->vm_mm;
1190 pfn -= from >> PAGE_SHIFT;
1191 pgd = pgd_offset(mm, from);
1192 flush_cache_range(vma, beg, end);
1193 BUG_ON(from >= end);
1196 * Physically remapped pages are special. Tell the
1197 * rest of the world about it:
1198 * VM_IO tells people not to look at these pages
1199 * (accesses can have side effects).
1200 * VM_RESERVED tells swapout not to try to touch
1203 vma->vm_flags |= VM_IO | VM_RESERVED;
1205 spin_lock(&mm->page_table_lock);
1206 for (i = pgd_index(beg); i <= pgd_index(end-1); i++) {
1207 pud_t *pud = pud_alloc(mm, pgd, from);
1211 next = (from + PGDIR_SIZE) & PGDIR_MASK;
1212 if (next > end || next <= from)
1214 error = remap_pud_range(mm, pud, from, end - from,
1215 pfn + (from >> PAGE_SHIFT), prot);
1222 * Why flush? remap_pte_range has a BUG_ON for !pte_none()
1224 flush_tlb_range(vma, beg, end);
1225 spin_unlock(&mm->page_table_lock);
1230 EXPORT_SYMBOL(remap_pfn_range);
1233 * Do pte_mkwrite, but only if the vma says VM_WRITE. We do this when
1234 * servicing faults for write access. In the normal case, do always want
1235 * pte_mkwrite. But get_user_pages can cause write faults for mappings
1236 * that do not have writing enabled, when used by access_process_vm.
1238 static inline pte_t maybe_mkwrite(pte_t pte, struct vm_area_struct *vma)
1240 if (likely(vma->vm_flags & VM_WRITE))
1241 pte = pte_mkwrite(pte);
1246 * We hold the mm semaphore for reading and vma->vm_mm->page_table_lock
1248 static inline void break_cow(struct vm_area_struct * vma, struct page * new_page, unsigned long address,
1253 flush_cache_page(vma, address);
1254 entry = maybe_mkwrite(pte_mkdirty(mk_pte(new_page, vma->vm_page_prot)),
1256 ptep_establish(vma, address, page_table, entry);
1257 update_mmu_cache(vma, address, entry);
1261 * This routine handles present pages, when users try to write
1262 * to a shared page. It is done by copying the page to a new address
1263 * and decrementing the shared-page counter for the old page.
1265 * Goto-purists beware: the only reason for goto's here is that it results
1266 * in better assembly code.. The "default" path will see no jumps at all.
1268 * Note that this routine assumes that the protection checks have been
1269 * done by the caller (the low-level page fault routine in most cases).
1270 * Thus we can safely just mark it writable once we've done any necessary
1273 * We also mark the page dirty at this point even though the page will
1274 * change only once the write actually happens. This avoids a few races,
1275 * and potentially makes it more efficient.
1277 * We hold the mm semaphore and the page_table_lock on entry and exit
1278 * with the page_table_lock released.
1280 static int do_wp_page(struct mm_struct *mm, struct vm_area_struct * vma,
1281 unsigned long address, pte_t *page_table, pmd_t *pmd, pte_t pte)
1283 struct page *old_page, *new_page;
1284 unsigned long pfn = pte_pfn(pte);
1287 if (unlikely(!pfn_valid(pfn))) {
1289 * This should really halt the system so it can be debugged or
1290 * at least the kernel stops what it's doing before it corrupts
1291 * data, but for the moment just pretend this is OOM.
1293 pte_unmap(page_table);
1294 printk(KERN_ERR "do_wp_page: bogus page at address %08lx\n",
1296 spin_unlock(&mm->page_table_lock);
1297 return VM_FAULT_OOM;
1299 old_page = pfn_to_page(pfn);
1301 if (!TestSetPageLocked(old_page)) {
1302 int reuse = can_share_swap_page(old_page);
1303 unlock_page(old_page);
1305 flush_cache_page(vma, address);
1306 entry = maybe_mkwrite(pte_mkyoung(pte_mkdirty(pte)),
1308 ptep_set_access_flags(vma, address, page_table, entry, 1);
1309 update_mmu_cache(vma, address, entry);
1310 pte_unmap(page_table);
1311 spin_unlock(&mm->page_table_lock);
1312 return VM_FAULT_MINOR;
1315 pte_unmap(page_table);
1318 * Ok, we need to copy. Oh, well..
1320 if (!PageReserved(old_page))
1321 page_cache_get(old_page);
1322 spin_unlock(&mm->page_table_lock);
1324 if (unlikely(anon_vma_prepare(vma)))
1326 if (old_page == ZERO_PAGE(address)) {
1327 new_page = alloc_zeroed_user_highpage(vma, address);
1331 new_page = alloc_page_vma(GFP_HIGHUSER, vma, address);
1334 copy_user_highpage(new_page, old_page, address);
1337 * Re-check the pte - we dropped the lock
1339 spin_lock(&mm->page_table_lock);
1340 page_table = pte_offset_map(pmd, address);
1341 if (likely(pte_same(*page_table, pte))) {
1342 if (PageAnon(old_page))
1344 vx_anonpages_dec(mm);
1345 if (PageReserved(old_page)) {
1347 vx_rsspages_inc(mm);
1348 acct_update_integrals();
1349 update_mem_hiwater();
1351 page_remove_rmap(old_page);
1352 break_cow(vma, new_page, address, page_table);
1353 lru_cache_add_active(new_page);
1354 page_add_anon_rmap(new_page, vma, address);
1356 /* Free the old page.. */
1357 new_page = old_page;
1359 pte_unmap(page_table);
1360 page_cache_release(new_page);
1361 page_cache_release(old_page);
1362 spin_unlock(&mm->page_table_lock);
1363 return VM_FAULT_MINOR;
1366 page_cache_release(old_page);
1367 return VM_FAULT_OOM;
1371 * Helper functions for unmap_mapping_range().
1373 * __ Notes on dropping i_mmap_lock to reduce latency while unmapping __
1375 * We have to restart searching the prio_tree whenever we drop the lock,
1376 * since the iterator is only valid while the lock is held, and anyway
1377 * a later vma might be split and reinserted earlier while lock dropped.
1379 * The list of nonlinear vmas could be handled more efficiently, using
1380 * a placeholder, but handle it in the same way until a need is shown.
1381 * It is important to search the prio_tree before nonlinear list: a vma
1382 * may become nonlinear and be shifted from prio_tree to nonlinear list
1383 * while the lock is dropped; but never shifted from list to prio_tree.
1385 * In order to make forward progress despite restarting the search,
1386 * vm_truncate_count is used to mark a vma as now dealt with, so we can
1387 * quickly skip it next time around. Since the prio_tree search only
1388 * shows us those vmas affected by unmapping the range in question, we
1389 * can't efficiently keep all vmas in step with mapping->truncate_count:
1390 * so instead reset them all whenever it wraps back to 0 (then go to 1).
1391 * mapping->truncate_count and vma->vm_truncate_count are protected by
1394 * In order to make forward progress despite repeatedly restarting some
1395 * large vma, note the break_addr set by unmap_vmas when it breaks out:
1396 * and restart from that address when we reach that vma again. It might
1397 * have been split or merged, shrunk or extended, but never shifted: so
1398 * restart_addr remains valid so long as it remains in the vma's range.
1399 * unmap_mapping_range forces truncate_count to leap over page-aligned
1400 * values so we can save vma's restart_addr in its truncate_count field.
1402 #define is_restart_addr(truncate_count) (!((truncate_count) & ~PAGE_MASK))
1404 static void reset_vma_truncate_counts(struct address_space *mapping)
1406 struct vm_area_struct *vma;
1407 struct prio_tree_iter iter;
1409 vma_prio_tree_foreach(vma, &iter, &mapping->i_mmap, 0, ULONG_MAX)
1410 vma->vm_truncate_count = 0;
1411 list_for_each_entry(vma, &mapping->i_mmap_nonlinear, shared.vm_set.list)
1412 vma->vm_truncate_count = 0;
1415 static int unmap_mapping_range_vma(struct vm_area_struct *vma,
1416 unsigned long start_addr, unsigned long end_addr,
1417 struct zap_details *details)
1419 unsigned long restart_addr;
1423 restart_addr = vma->vm_truncate_count;
1424 if (is_restart_addr(restart_addr) && start_addr < restart_addr) {
1425 start_addr = restart_addr;
1426 if (start_addr >= end_addr) {
1427 /* Top of vma has been split off since last time */
1428 vma->vm_truncate_count = details->truncate_count;
1433 details->break_addr = end_addr;
1434 zap_page_range(vma, start_addr, end_addr - start_addr, details);
1437 * We cannot rely on the break test in unmap_vmas:
1438 * on the one hand, we don't want to restart our loop
1439 * just because that broke out for the page_table_lock;
1440 * on the other hand, it does no test when vma is small.
1442 need_break = need_resched() ||
1443 need_lockbreak(details->i_mmap_lock);
1445 if (details->break_addr >= end_addr) {
1446 /* We have now completed this vma: mark it so */
1447 vma->vm_truncate_count = details->truncate_count;
1451 /* Note restart_addr in vma's truncate_count field */
1452 vma->vm_truncate_count = details->break_addr;
1457 spin_unlock(details->i_mmap_lock);
1459 spin_lock(details->i_mmap_lock);
1463 static inline void unmap_mapping_range_tree(struct prio_tree_root *root,
1464 struct zap_details *details)
1466 struct vm_area_struct *vma;
1467 struct prio_tree_iter iter;
1468 pgoff_t vba, vea, zba, zea;
1471 vma_prio_tree_foreach(vma, &iter, root,
1472 details->first_index, details->last_index) {
1473 /* Skip quickly over those we have already dealt with */
1474 if (vma->vm_truncate_count == details->truncate_count)
1477 vba = vma->vm_pgoff;
1478 vea = vba + ((vma->vm_end - vma->vm_start) >> PAGE_SHIFT) - 1;
1479 /* Assume for now that PAGE_CACHE_SHIFT == PAGE_SHIFT */
1480 zba = details->first_index;
1483 zea = details->last_index;
1487 if (unmap_mapping_range_vma(vma,
1488 ((zba - vba) << PAGE_SHIFT) + vma->vm_start,
1489 ((zea - vba + 1) << PAGE_SHIFT) + vma->vm_start,
1495 static inline void unmap_mapping_range_list(struct list_head *head,
1496 struct zap_details *details)
1498 struct vm_area_struct *vma;
1501 * In nonlinear VMAs there is no correspondence between virtual address
1502 * offset and file offset. So we must perform an exhaustive search
1503 * across *all* the pages in each nonlinear VMA, not just the pages
1504 * whose virtual address lies outside the file truncation point.
1507 list_for_each_entry(vma, head, shared.vm_set.list) {
1508 /* Skip quickly over those we have already dealt with */
1509 if (vma->vm_truncate_count == details->truncate_count)
1511 details->nonlinear_vma = vma;
1512 if (unmap_mapping_range_vma(vma, vma->vm_start,
1513 vma->vm_end, details) < 0)
1519 * unmap_mapping_range - unmap the portion of all mmaps
1520 * in the specified address_space corresponding to the specified
1521 * page range in the underlying file.
1522 * @address_space: the address space containing mmaps to be unmapped.
1523 * @holebegin: byte in first page to unmap, relative to the start of
1524 * the underlying file. This will be rounded down to a PAGE_SIZE
1525 * boundary. Note that this is different from vmtruncate(), which
1526 * must keep the partial page. In contrast, we must get rid of
1528 * @holelen: size of prospective hole in bytes. This will be rounded
1529 * up to a PAGE_SIZE boundary. A holelen of zero truncates to the
1531 * @even_cows: 1 when truncating a file, unmap even private COWed pages;
1532 * but 0 when invalidating pagecache, don't throw away private data.
1534 void unmap_mapping_range(struct address_space *mapping,
1535 loff_t const holebegin, loff_t const holelen, int even_cows)
1537 struct zap_details details;
1538 pgoff_t hba = holebegin >> PAGE_SHIFT;
1539 pgoff_t hlen = (holelen + PAGE_SIZE - 1) >> PAGE_SHIFT;
1541 /* Check for overflow. */
1542 if (sizeof(holelen) > sizeof(hlen)) {
1544 (holebegin + holelen + PAGE_SIZE - 1) >> PAGE_SHIFT;
1545 if (holeend & ~(long long)ULONG_MAX)
1546 hlen = ULONG_MAX - hba + 1;
1549 details.check_mapping = even_cows? NULL: mapping;
1550 details.nonlinear_vma = NULL;
1551 details.first_index = hba;
1552 details.last_index = hba + hlen - 1;
1553 if (details.last_index < details.first_index)
1554 details.last_index = ULONG_MAX;
1555 details.i_mmap_lock = &mapping->i_mmap_lock;
1557 spin_lock(&mapping->i_mmap_lock);
1559 /* serialize i_size write against truncate_count write */
1561 /* Protect against page faults, and endless unmapping loops */
1562 mapping->truncate_count++;
1564 * For archs where spin_lock has inclusive semantics like ia64
1565 * this smp_mb() will prevent to read pagetable contents
1566 * before the truncate_count increment is visible to
1570 if (unlikely(is_restart_addr(mapping->truncate_count))) {
1571 if (mapping->truncate_count == 0)
1572 reset_vma_truncate_counts(mapping);
1573 mapping->truncate_count++;
1575 details.truncate_count = mapping->truncate_count;
1577 if (unlikely(!prio_tree_empty(&mapping->i_mmap)))
1578 unmap_mapping_range_tree(&mapping->i_mmap, &details);
1579 if (unlikely(!list_empty(&mapping->i_mmap_nonlinear)))
1580 unmap_mapping_range_list(&mapping->i_mmap_nonlinear, &details);
1581 spin_unlock(&mapping->i_mmap_lock);
1583 EXPORT_SYMBOL(unmap_mapping_range);
1586 * Handle all mappings that got truncated by a "truncate()"
1589 * NOTE! We have to be ready to update the memory sharing
1590 * between the file and the memory map for a potential last
1591 * incomplete page. Ugly, but necessary.
1593 int vmtruncate(struct inode * inode, loff_t offset)
1595 struct address_space *mapping = inode->i_mapping;
1596 unsigned long limit;
1598 if (inode->i_size < offset)
1601 * truncation of in-use swapfiles is disallowed - it would cause
1602 * subsequent swapout to scribble on the now-freed blocks.
1604 if (IS_SWAPFILE(inode))
1606 i_size_write(inode, offset);
1607 unmap_mapping_range(mapping, offset + PAGE_SIZE - 1, 0, 1);
1608 truncate_inode_pages(mapping, offset);
1612 limit = current->signal->rlim[RLIMIT_FSIZE].rlim_cur;
1613 if (limit != RLIM_INFINITY && offset > limit)
1615 if (offset > inode->i_sb->s_maxbytes)
1617 i_size_write(inode, offset);
1620 if (inode->i_op && inode->i_op->truncate)
1621 inode->i_op->truncate(inode);
1624 send_sig(SIGXFSZ, current, 0);
1631 EXPORT_SYMBOL(vmtruncate);
1634 * Primitive swap readahead code. We simply read an aligned block of
1635 * (1 << page_cluster) entries in the swap area. This method is chosen
1636 * because it doesn't cost us any seek time. We also make sure to queue
1637 * the 'original' request together with the readahead ones...
1639 * This has been extended to use the NUMA policies from the mm triggering
1642 * Caller must hold down_read on the vma->vm_mm if vma is not NULL.
1644 void swapin_readahead(swp_entry_t entry, unsigned long addr,struct vm_area_struct *vma)
1647 struct vm_area_struct *next_vma = vma ? vma->vm_next : NULL;
1650 struct page *new_page;
1651 unsigned long offset;
1654 * Get the number of handles we should do readahead io to.
1656 num = valid_swaphandles(entry, &offset);
1657 for (i = 0; i < num; offset++, i++) {
1658 /* Ok, do the async read-ahead now */
1659 new_page = read_swap_cache_async(swp_entry(swp_type(entry),
1660 offset), vma, addr);
1663 page_cache_release(new_page);
1666 * Find the next applicable VMA for the NUMA policy.
1672 if (addr >= vma->vm_end) {
1674 next_vma = vma ? vma->vm_next : NULL;
1676 if (vma && addr < vma->vm_start)
1679 if (next_vma && addr >= next_vma->vm_start) {
1681 next_vma = vma->vm_next;
1686 lru_add_drain(); /* Push any new pages onto the LRU now */
1690 * We hold the mm semaphore and the page_table_lock on entry and
1691 * should release the pagetable lock on exit..
1693 static int do_swap_page(struct mm_struct * mm,
1694 struct vm_area_struct * vma, unsigned long address,
1695 pte_t *page_table, pmd_t *pmd, pte_t orig_pte, int write_access)
1698 swp_entry_t entry = pte_to_swp_entry(orig_pte);
1700 int ret = VM_FAULT_MINOR;
1702 pte_unmap(page_table);
1703 spin_unlock(&mm->page_table_lock);
1704 page = lookup_swap_cache(entry);
1706 swapin_readahead(entry, address, vma);
1707 page = read_swap_cache_async(entry, vma, address);
1710 * Back out if somebody else faulted in this pte while
1711 * we released the page table lock.
1713 spin_lock(&mm->page_table_lock);
1714 page_table = pte_offset_map(pmd, address);
1715 if (likely(pte_same(*page_table, orig_pte)))
1718 ret = VM_FAULT_MINOR;
1719 pte_unmap(page_table);
1720 spin_unlock(&mm->page_table_lock);
1724 /* Had to read the page from swap area: Major fault */
1725 ret = VM_FAULT_MAJOR;
1726 inc_page_state(pgmajfault);
1730 if (!vx_rsspages_avail(mm, 1)) {
1734 mark_page_accessed(page);
1738 * Back out if somebody else faulted in this pte while we
1739 * released the page table lock.
1741 spin_lock(&mm->page_table_lock);
1742 page_table = pte_offset_map(pmd, address);
1743 if (unlikely(!pte_same(*page_table, orig_pte))) {
1744 pte_unmap(page_table);
1745 spin_unlock(&mm->page_table_lock);
1747 page_cache_release(page);
1748 ret = VM_FAULT_MINOR;
1752 /* The page isn't present yet, go ahead with the fault. */
1756 remove_exclusive_swap_page(page);
1759 vx_rsspages_inc(mm);
1760 acct_update_integrals();
1761 update_mem_hiwater();
1763 pte = mk_pte(page, vma->vm_page_prot);
1764 if (write_access && can_share_swap_page(page)) {
1765 pte = maybe_mkwrite(pte_mkdirty(pte), vma);
1770 flush_icache_page(vma, page);
1771 set_pte(page_table, pte);
1772 page_add_anon_rmap(page, vma, address);
1775 if (do_wp_page(mm, vma, address,
1776 page_table, pmd, pte) == VM_FAULT_OOM)
1781 /* No need to invalidate - it was non-present before */
1782 update_mmu_cache(vma, address, pte);
1783 pte_unmap(page_table);
1784 spin_unlock(&mm->page_table_lock);
1790 * We are called with the MM semaphore and page_table_lock
1791 * spinlock held to protect against concurrent faults in
1792 * multithreaded programs.
1795 do_anonymous_page(struct mm_struct *mm, struct vm_area_struct *vma,
1796 pte_t *page_table, pmd_t *pmd, int write_access,
1800 struct page * page = ZERO_PAGE(addr);
1802 /* Read-only mapping of ZERO_PAGE. */
1803 entry = pte_wrprotect(mk_pte(ZERO_PAGE(addr), vma->vm_page_prot));
1805 /* ..except if it's a write access */
1807 /* Allocate our own private page. */
1808 pte_unmap(page_table);
1809 spin_unlock(&mm->page_table_lock);
1811 if (!vx_rsspages_avail(mm, 1))
1813 if (unlikely(anon_vma_prepare(vma)))
1815 page = alloc_zeroed_user_highpage(vma, addr);
1819 spin_lock(&mm->page_table_lock);
1820 page_table = pte_offset_map(pmd, addr);
1822 if (!pte_none(*page_table)) {
1823 pte_unmap(page_table);
1824 page_cache_release(page);
1825 spin_unlock(&mm->page_table_lock);
1829 vx_rsspages_inc(mm);
1830 acct_update_integrals();
1831 update_mem_hiwater();
1832 entry = maybe_mkwrite(pte_mkdirty(mk_pte(page,
1833 vma->vm_page_prot)),
1835 lru_cache_add_active(page);
1836 SetPageReferenced(page);
1837 page_add_anon_rmap(page, vma, addr);
1840 set_pte(page_table, entry);
1841 pte_unmap(page_table);
1843 /* No need to invalidate - it was non-present before */
1844 update_mmu_cache(vma, addr, entry);
1845 spin_unlock(&mm->page_table_lock);
1847 return VM_FAULT_MINOR;
1849 return VM_FAULT_OOM;
1853 * do_no_page() tries to create a new page mapping. It aggressively
1854 * tries to share with existing pages, but makes a separate copy if
1855 * the "write_access" parameter is true in order to avoid the next
1858 * As this is called only for pages that do not currently exist, we
1859 * do not need to flush old virtual caches or the TLB.
1861 * This is called with the MM semaphore held and the page table
1862 * spinlock held. Exit with the spinlock released.
1865 do_no_page(struct mm_struct *mm, struct vm_area_struct *vma,
1866 unsigned long address, int write_access, pte_t *page_table, pmd_t *pmd)
1868 struct page * new_page;
1869 struct address_space *mapping = NULL;
1871 unsigned int sequence = 0;
1872 int ret = VM_FAULT_MINOR;
1875 if (!vma->vm_ops || !vma->vm_ops->nopage)
1876 return do_anonymous_page(mm, vma, page_table,
1877 pmd, write_access, address);
1878 pte_unmap(page_table);
1879 spin_unlock(&mm->page_table_lock);
1882 mapping = vma->vm_file->f_mapping;
1883 sequence = mapping->truncate_count;
1884 smp_rmb(); /* serializes i_size against truncate_count */
1888 /* FIXME: is that check useful here? */
1889 if (!vx_rsspages_avail(mm, 1))
1890 return VM_FAULT_OOM;
1891 new_page = vma->vm_ops->nopage(vma, address & PAGE_MASK, &ret);
1893 * No smp_rmb is needed here as long as there's a full
1894 * spin_lock/unlock sequence inside the ->nopage callback
1895 * (for the pagecache lookup) that acts as an implicit
1896 * smp_mb() and prevents the i_size read to happen
1897 * after the next truncate_count read.
1900 /* no page was available -- either SIGBUS or OOM */
1901 if (new_page == NOPAGE_SIGBUS)
1902 return VM_FAULT_SIGBUS;
1903 if (new_page == NOPAGE_OOM)
1904 return VM_FAULT_OOM;
1907 * Should we do an early C-O-W break?
1909 if (write_access && !(vma->vm_flags & VM_SHARED)) {
1912 if (unlikely(anon_vma_prepare(vma)))
1914 page = alloc_page_vma(GFP_HIGHUSER, vma, address);
1917 copy_user_highpage(page, new_page, address);
1918 page_cache_release(new_page);
1923 spin_lock(&mm->page_table_lock);
1925 * For a file-backed vma, someone could have truncated or otherwise
1926 * invalidated this page. If unmap_mapping_range got called,
1927 * retry getting the page.
1929 if (mapping && unlikely(sequence != mapping->truncate_count)) {
1930 sequence = mapping->truncate_count;
1931 spin_unlock(&mm->page_table_lock);
1932 page_cache_release(new_page);
1935 page_table = pte_offset_map(pmd, address);
1938 * This silly early PAGE_DIRTY setting removes a race
1939 * due to the bad i386 page protection. But it's valid
1940 * for other architectures too.
1942 * Note that if write_access is true, we either now have
1943 * an exclusive copy of the page, or this is a shared mapping,
1944 * so we can make it writable and dirty to avoid having to
1945 * handle that later.
1947 /* Only go through if we didn't race with anybody else... */
1948 if (pte_none(*page_table)) {
1949 if (!PageReserved(new_page))
1951 vx_rsspages_inc(mm);
1952 acct_update_integrals();
1953 update_mem_hiwater();
1955 flush_icache_page(vma, new_page);
1956 entry = mk_pte(new_page, vma->vm_page_prot);
1958 entry = maybe_mkwrite(pte_mkdirty(entry), vma);
1959 set_pte(page_table, entry);
1961 lru_cache_add_active(new_page);
1962 page_add_anon_rmap(new_page, vma, address);
1964 page_add_file_rmap(new_page);
1965 pte_unmap(page_table);
1967 /* One of our sibling threads was faster, back out. */
1968 pte_unmap(page_table);
1969 page_cache_release(new_page);
1970 spin_unlock(&mm->page_table_lock);
1974 /* no need to invalidate: a not-present page shouldn't be cached */
1975 update_mmu_cache(vma, address, entry);
1976 spin_unlock(&mm->page_table_lock);
1980 page_cache_release(new_page);
1986 * Fault of a previously existing named mapping. Repopulate the pte
1987 * from the encoded file_pte if possible. This enables swappable
1990 static int do_file_page(struct mm_struct * mm, struct vm_area_struct * vma,
1991 unsigned long address, int write_access, pte_t *pte, pmd_t *pmd)
1993 unsigned long pgoff;
1996 BUG_ON(!vma->vm_ops || !vma->vm_ops->nopage);
1998 * Fall back to the linear mapping if the fs does not support
2001 if (!vma->vm_ops || !vma->vm_ops->populate ||
2002 (write_access && !(vma->vm_flags & VM_SHARED))) {
2004 return do_no_page(mm, vma, address, write_access, pte, pmd);
2007 pgoff = pte_to_pgoff(*pte);
2010 spin_unlock(&mm->page_table_lock);
2012 err = vma->vm_ops->populate(vma, address & PAGE_MASK, PAGE_SIZE, vma->vm_page_prot, pgoff, 0);
2014 return VM_FAULT_OOM;
2016 return VM_FAULT_SIGBUS;
2017 return VM_FAULT_MAJOR;
2021 * These routines also need to handle stuff like marking pages dirty
2022 * and/or accessed for architectures that don't do it in hardware (most
2023 * RISC architectures). The early dirtying is also good on the i386.
2025 * There is also a hook called "update_mmu_cache()" that architectures
2026 * with external mmu caches can use to update those (ie the Sparc or
2027 * PowerPC hashed page tables that act as extended TLBs).
2029 * Note the "page_table_lock". It is to protect against kswapd removing
2030 * pages from under us. Note that kswapd only ever _removes_ pages, never
2031 * adds them. As such, once we have noticed that the page is not present,
2032 * we can drop the lock early.
2034 * The adding of pages is protected by the MM semaphore (which we hold),
2035 * so we don't need to worry about a page being suddenly been added into
2038 * We enter with the pagetable spinlock held, we are supposed to
2039 * release it when done.
2041 static inline int handle_pte_fault(struct mm_struct *mm,
2042 struct vm_area_struct * vma, unsigned long address,
2043 int write_access, pte_t *pte, pmd_t *pmd)
2048 if (!pte_present(entry)) {
2050 * If it truly wasn't present, we know that kswapd
2051 * and the PTE updates will not touch it later. So
2054 if (pte_none(entry))
2055 return do_no_page(mm, vma, address, write_access, pte, pmd);
2056 if (pte_file(entry))
2057 return do_file_page(mm, vma, address, write_access, pte, pmd);
2058 return do_swap_page(mm, vma, address, pte, pmd, entry, write_access);
2062 if (!pte_write(entry))
2063 return do_wp_page(mm, vma, address, pte, pmd, entry);
2065 entry = pte_mkdirty(entry);
2067 entry = pte_mkyoung(entry);
2068 ptep_set_access_flags(vma, address, pte, entry, write_access);
2069 update_mmu_cache(vma, address, entry);
2071 spin_unlock(&mm->page_table_lock);
2072 return VM_FAULT_MINOR;
2076 * By the time we get here, we already hold the mm semaphore
2078 int handle_mm_fault(struct mm_struct *mm, struct vm_area_struct * vma,
2079 unsigned long address, int write_access)
2086 __set_current_state(TASK_RUNNING);
2088 inc_page_state(pgfault);
2090 if (is_vm_hugetlb_page(vma))
2091 return VM_FAULT_SIGBUS; /* mapping truncation does this. */
2094 * We need the page table lock to synchronize with kswapd
2095 * and the SMP-safe atomic PTE updates.
2097 pgd = pgd_offset(mm, address);
2098 spin_lock(&mm->page_table_lock);
2100 pud = pud_alloc(mm, pgd, address);
2104 pmd = pmd_alloc(mm, pud, address);
2108 pte = pte_alloc_map(mm, pmd, address);
2112 return handle_pte_fault(mm, vma, address, write_access, pte, pmd);
2115 spin_unlock(&mm->page_table_lock);
2116 return VM_FAULT_OOM;
2119 #ifndef __ARCH_HAS_4LEVEL_HACK
2121 * Allocate page upper directory.
2123 * We've already handled the fast-path in-line, and we own the
2126 * On a two-level or three-level page table, this ends up actually being
2127 * entirely optimized away.
2129 pud_t fastcall *__pud_alloc(struct mm_struct *mm, pgd_t *pgd, unsigned long address)
2133 spin_unlock(&mm->page_table_lock);
2134 new = pud_alloc_one(mm, address);
2135 spin_lock(&mm->page_table_lock);
2140 * Because we dropped the lock, we should re-check the
2141 * entry, as somebody else could have populated it..
2143 if (pgd_present(*pgd)) {
2147 pgd_populate(mm, pgd, new);
2149 return pud_offset(pgd, address);
2153 * Allocate page middle directory.
2155 * We've already handled the fast-path in-line, and we own the
2158 * On a two-level page table, this ends up actually being entirely
2161 pmd_t fastcall *__pmd_alloc(struct mm_struct *mm, pud_t *pud, unsigned long address)
2165 spin_unlock(&mm->page_table_lock);
2166 new = pmd_alloc_one(mm, address);
2167 spin_lock(&mm->page_table_lock);
2172 * Because we dropped the lock, we should re-check the
2173 * entry, as somebody else could have populated it..
2175 if (pud_present(*pud)) {
2179 pud_populate(mm, pud, new);
2181 return pmd_offset(pud, address);
2184 pmd_t fastcall *__pmd_alloc(struct mm_struct *mm, pud_t *pud, unsigned long address)
2188 spin_unlock(&mm->page_table_lock);
2189 new = pmd_alloc_one(mm, address);
2190 spin_lock(&mm->page_table_lock);
2195 * Because we dropped the lock, we should re-check the
2196 * entry, as somebody else could have populated it..
2198 if (pgd_present(*pud)) {
2202 pgd_populate(mm, pud, new);
2204 return pmd_offset(pud, address);
2208 int make_pages_present(unsigned long addr, unsigned long end)
2210 int ret, len, write;
2211 struct vm_area_struct * vma;
2213 vma = find_vma(current->mm, addr);
2216 write = (vma->vm_flags & VM_WRITE) != 0;
2219 if (end > vma->vm_end)
2221 len = (end+PAGE_SIZE-1)/PAGE_SIZE-addr/PAGE_SIZE;
2222 ret = get_user_pages(current, current->mm, addr,
2223 len, write, 0, NULL, NULL);
2226 return ret == len ? 0 : -1;
2230 * Map a vmalloc()-space virtual address to the physical page.
2232 struct page * vmalloc_to_page(void * vmalloc_addr)
2234 unsigned long addr = (unsigned long) vmalloc_addr;
2235 struct page *page = NULL;
2236 pgd_t *pgd = pgd_offset_k(addr);
2241 if (!pgd_none(*pgd)) {
2242 pud = pud_offset(pgd, addr);
2243 if (!pud_none(*pud)) {
2244 pmd = pmd_offset(pud, addr);
2245 if (!pmd_none(*pmd)) {
2246 ptep = pte_offset_map(pmd, addr);
2248 if (pte_present(pte))
2249 page = pte_page(pte);
2257 EXPORT_SYMBOL(vmalloc_to_page);
2260 * Map a vmalloc()-space virtual address to the physical page frame number.
2262 unsigned long vmalloc_to_pfn(void * vmalloc_addr)
2264 return page_to_pfn(vmalloc_to_page(vmalloc_addr));
2267 EXPORT_SYMBOL(vmalloc_to_pfn);
2270 * update_mem_hiwater
2271 * - update per process rss and vm high water data
2273 void update_mem_hiwater(void)
2275 struct task_struct *tsk = current;
2278 if (tsk->mm->hiwater_rss < tsk->mm->rss)
2279 tsk->mm->hiwater_rss = tsk->mm->rss;
2280 if (tsk->mm->hiwater_vm < tsk->mm->total_vm)
2281 tsk->mm->hiwater_vm = tsk->mm->total_vm;
2285 #if !defined(__HAVE_ARCH_GATE_AREA)
2287 #if defined(AT_SYSINFO_EHDR)
2288 struct vm_area_struct gate_vma;
2290 static int __init gate_vma_init(void)
2292 gate_vma.vm_mm = NULL;
2293 gate_vma.vm_start = FIXADDR_USER_START;
2294 gate_vma.vm_end = FIXADDR_USER_END;
2295 gate_vma.vm_page_prot = PAGE_READONLY;
2296 gate_vma.vm_flags = 0;
2299 __initcall(gate_vma_init);
2302 struct vm_area_struct *get_gate_vma(struct task_struct *tsk)
2304 #ifdef AT_SYSINFO_EHDR
2311 int in_gate_area_no_task(unsigned long addr)
2313 #ifdef AT_SYSINFO_EHDR
2314 if ((addr >= FIXADDR_USER_START) && (addr < FIXADDR_USER_END))
2320 #endif /* __HAVE_ARCH_GATE_AREA */