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)
39 #include <linux/kernel_stat.h>
41 #include <linux/hugetlb.h>
42 #include <linux/mman.h>
43 #include <linux/swap.h>
44 #include <linux/highmem.h>
45 #include <linux/pagemap.h>
46 #include <linux/rmap.h>
47 #include <linux/module.h>
48 #include <linux/init.h>
50 #include <asm/pgalloc.h>
51 #include <asm/uaccess.h>
53 #include <asm/tlbflush.h>
54 #include <asm/pgtable.h>
56 #include <linux/swapops.h>
57 #include <linux/elf.h>
59 #ifndef CONFIG_DISCONTIGMEM
60 /* use the per-pgdat data instead for discontigmem - mbligh */
61 unsigned long max_mapnr;
64 EXPORT_SYMBOL(max_mapnr);
65 EXPORT_SYMBOL(mem_map);
68 unsigned long num_physpages;
70 struct page *highmem_start_page;
72 EXPORT_SYMBOL(num_physpages);
73 EXPORT_SYMBOL(highmem_start_page);
74 EXPORT_SYMBOL(high_memory);
77 * We special-case the C-O-W ZERO_PAGE, because it's such
78 * a common occurrence (no need to read the page to know
79 * that it's zero - better for the cache and memory subsystem).
81 static inline void copy_cow_page(struct page * from, struct page * to, unsigned long address)
83 if (from == ZERO_PAGE(address)) {
84 clear_user_highpage(to, address);
87 copy_user_highpage(to, from, address);
91 * Note: this doesn't free the actual pages themselves. That
92 * has been handled earlier when unmapping all the memory regions.
94 static inline void free_one_pmd(struct mmu_gather *tlb, pmd_t * dir)
100 if (unlikely(pmd_bad(*dir))) {
105 page = pmd_page(*dir);
107 dec_page_state(nr_page_table_pages);
108 pte_free_tlb(tlb, page);
111 static inline void free_one_pgd(struct mmu_gather *tlb, pgd_t * dir)
118 if (unlikely(pgd_bad(*dir))) {
123 pmd = pmd_offset(dir, 0);
125 for (j = 0; j < PTRS_PER_PMD ; j++)
126 free_one_pmd(tlb, pmd+j);
127 pmd_free_tlb(tlb, pmd);
131 * This function clears all user-level page tables of a process - this
132 * is needed by execve(), so that old pages aren't in the way.
134 * Must be called with pagetable lock held.
136 void clear_page_tables(struct mmu_gather *tlb, unsigned long first, int nr)
138 pgd_t * page_dir = tlb->mm->pgd;
142 free_one_pgd(tlb, page_dir);
147 pte_t fastcall * pte_alloc_map(struct mm_struct *mm, pmd_t *pmd, unsigned long address)
149 if (!pmd_present(*pmd)) {
152 spin_unlock(&mm->page_table_lock);
153 new = pte_alloc_one(mm, address);
154 spin_lock(&mm->page_table_lock);
159 * Because we dropped the lock, we should re-check the
160 * entry, as somebody else could have populated it..
162 if (pmd_present(*pmd)) {
166 inc_page_state(nr_page_table_pages);
167 pmd_populate(mm, pmd, new);
170 return pte_offset_map(pmd, address);
173 pte_t fastcall * pte_alloc_kernel(struct mm_struct *mm, pmd_t *pmd, unsigned long address)
175 if (!pmd_present(*pmd)) {
178 spin_unlock(&mm->page_table_lock);
179 new = pte_alloc_one_kernel(mm, address);
180 spin_lock(&mm->page_table_lock);
185 * Because we dropped the lock, we should re-check the
186 * entry, as somebody else could have populated it..
188 if (pmd_present(*pmd)) {
189 pte_free_kernel(new);
192 pmd_populate_kernel(mm, pmd, new);
195 return pte_offset_kernel(pmd, address);
197 #define PTE_TABLE_MASK ((PTRS_PER_PTE-1) * sizeof(pte_t))
198 #define PMD_TABLE_MASK ((PTRS_PER_PMD-1) * sizeof(pmd_t))
201 * copy one vm_area from one task to the other. Assumes the page tables
202 * already present in the new task to be cleared in the whole range
203 * covered by this vma.
205 * 08Jan98 Merged into one routine from several inline routines to reduce
206 * variable count and make things faster. -jj
208 * dst->page_table_lock is held on entry and exit,
209 * but may be dropped within pmd_alloc() and pte_alloc_map().
211 int copy_page_range(struct mm_struct *dst, struct mm_struct *src,
212 struct vm_area_struct *vma)
214 pgd_t * src_pgd, * dst_pgd;
215 unsigned long address = vma->vm_start;
216 unsigned long end = vma->vm_end;
219 if (is_vm_hugetlb_page(vma))
220 return copy_hugetlb_page_range(dst, src, vma);
222 cow = (vma->vm_flags & (VM_SHARED | VM_MAYWRITE)) == VM_MAYWRITE;
223 src_pgd = pgd_offset(src, address)-1;
224 dst_pgd = pgd_offset(dst, address)-1;
227 pmd_t * src_pmd, * dst_pmd;
229 src_pgd++; dst_pgd++;
233 if (pgd_none(*src_pgd))
234 goto skip_copy_pmd_range;
235 if (unlikely(pgd_bad(*src_pgd))) {
238 skip_copy_pmd_range: address = (address + PGDIR_SIZE) & PGDIR_MASK;
239 if (!address || (address >= end))
244 src_pmd = pmd_offset(src_pgd, address);
245 dst_pmd = pmd_alloc(dst, dst_pgd, address);
250 pte_t * src_pte, * dst_pte;
254 if (pmd_none(*src_pmd))
255 goto skip_copy_pte_range;
256 if (unlikely(pmd_bad(*src_pmd))) {
260 address = (address + PMD_SIZE) & PMD_MASK;
263 goto cont_copy_pmd_range;
266 dst_pte = pte_alloc_map(dst, dst_pmd, address);
269 spin_lock(&src->page_table_lock);
270 src_pte = pte_offset_map_nested(src_pmd, address);
272 pte_t pte = *src_pte;
279 goto cont_copy_pte_range_noset;
280 /* pte contains position in swap, so copy. */
281 if (!pte_present(pte)) {
283 swap_duplicate(pte_to_swp_entry(pte));
284 set_pte(dst_pte, pte);
285 goto cont_copy_pte_range_noset;
288 /* the pte points outside of valid memory, the
289 * mapping is assumed to be good, meaningful
290 * and not mapped via rmap - duplicate the
295 page = pfn_to_page(pfn);
297 if (!page || PageReserved(page)) {
298 set_pte(dst_pte, pte);
299 goto cont_copy_pte_range_noset;
303 * If it's a COW mapping, write protect it both
304 * in the parent and the child
307 ptep_set_wrprotect(src_pte);
312 * If it's a shared mapping, mark it clean in
315 if (vma->vm_flags & VM_SHARED)
316 pte = pte_mkclean(pte);
317 pte = pte_mkold(pte);
320 set_pte(dst_pte, pte);
322 cont_copy_pte_range_noset:
323 address += PAGE_SIZE;
324 if (address >= end) {
325 pte_unmap_nested(src_pte);
331 } while ((unsigned long)src_pte & PTE_TABLE_MASK);
332 pte_unmap_nested(src_pte-1);
333 pte_unmap(dst_pte-1);
334 spin_unlock(&src->page_table_lock);
335 cond_resched_lock(&dst->page_table_lock);
339 } while ((unsigned long)src_pmd & PMD_TABLE_MASK);
342 spin_unlock(&src->page_table_lock);
349 static void zap_pte_range(struct mmu_gather *tlb,
350 pmd_t *pmd, unsigned long address,
351 unsigned long size, struct zap_details *details)
353 unsigned long offset;
358 if (unlikely(pmd_bad(*pmd))) {
363 ptep = pte_offset_map(pmd, address);
364 offset = address & ~PMD_MASK;
365 if (offset + size > PMD_SIZE)
366 size = PMD_SIZE - offset;
368 if (details && !details->check_mapping && !details->nonlinear_vma)
370 for (offset=0; offset < size; ptep++, offset += PAGE_SIZE) {
374 if (pte_present(pte)) {
375 struct page *page = NULL;
376 unsigned long pfn = pte_pfn(pte);
377 if (pfn_valid(pfn)) {
378 page = pfn_to_page(pfn);
379 if (PageReserved(page))
382 if (unlikely(details) && page) {
384 * unmap_shared_mapping_pages() wants to
385 * invalidate cache without truncating:
386 * unmap shared but keep private pages.
388 if (details->check_mapping &&
389 details->check_mapping != page->mapping)
392 * Each page->index must be checked when
393 * invalidating or truncating nonlinear.
395 if (details->nonlinear_vma &&
396 (page->index < details->first_index ||
397 page->index > details->last_index))
400 pte = ptep_get_and_clear(ptep);
401 tlb_remove_tlb_entry(tlb, ptep, address+offset);
404 if (unlikely(details) && details->nonlinear_vma
405 && linear_page_index(details->nonlinear_vma,
406 address+offset) != page->index)
407 set_pte(ptep, pgoff_to_pte(page->index));
409 set_page_dirty(page);
410 if (pte_young(pte) && page_mapping(page))
411 mark_page_accessed(page);
413 page_remove_rmap(page);
414 tlb_remove_page(tlb, page);
418 * If details->check_mapping, we leave swap entries;
419 * if details->nonlinear_vma, we leave file entries.
421 if (unlikely(details))
424 free_swap_and_cache(pte_to_swp_entry(pte));
430 static void zap_pmd_range(struct mmu_gather *tlb,
431 pgd_t * dir, unsigned long address,
432 unsigned long size, struct zap_details *details)
439 if (unlikely(pgd_bad(*dir))) {
444 pmd = pmd_offset(dir, address);
445 end = address + size;
446 if (end > ((address + PGDIR_SIZE) & PGDIR_MASK))
447 end = ((address + PGDIR_SIZE) & PGDIR_MASK);
449 zap_pte_range(tlb, pmd, address, end - address, details);
450 address = (address + PMD_SIZE) & PMD_MASK;
452 } while (address && (address < end));
455 static void unmap_page_range(struct mmu_gather *tlb,
456 struct vm_area_struct *vma, unsigned long address,
457 unsigned long end, struct zap_details *details)
461 BUG_ON(address >= end);
462 dir = pgd_offset(vma->vm_mm, address);
463 tlb_start_vma(tlb, vma);
465 zap_pmd_range(tlb, dir, address, end - address, details);
466 address = (address + PGDIR_SIZE) & PGDIR_MASK;
468 } while (address && (address < end));
469 tlb_end_vma(tlb, vma);
472 /* Dispose of an entire struct mmu_gather per rescheduling point */
473 #if defined(CONFIG_SMP) && defined(CONFIG_PREEMPT)
474 #define ZAP_BLOCK_SIZE (FREE_PTE_NR * PAGE_SIZE)
477 /* For UP, 256 pages at a time gives nice low latency */
478 #if !defined(CONFIG_SMP) && defined(CONFIG_PREEMPT)
479 #define ZAP_BLOCK_SIZE (256 * PAGE_SIZE)
482 /* No preempt: go for improved straight-line efficiency */
483 #if !defined(CONFIG_PREEMPT)
484 #define ZAP_BLOCK_SIZE (1024 * PAGE_SIZE)
488 * unmap_vmas - unmap a range of memory covered by a list of vma's
489 * @tlbp: address of the caller's struct mmu_gather
490 * @mm: the controlling mm_struct
491 * @vma: the starting vma
492 * @start_addr: virtual address at which to start unmapping
493 * @end_addr: virtual address at which to end unmapping
494 * @nr_accounted: Place number of unmapped pages in vm-accountable vma's here
495 * @details: details of nonlinear truncation or shared cache invalidation
497 * Returns the number of vma's which were covered by the unmapping.
499 * Unmap all pages in the vma list. Called under page_table_lock.
501 * We aim to not hold page_table_lock for too long (for scheduling latency
502 * reasons). So zap pages in ZAP_BLOCK_SIZE bytecounts. This means we need to
503 * return the ending mmu_gather to the caller.
505 * Only addresses between `start' and `end' will be unmapped.
507 * The VMA list must be sorted in ascending virtual address order.
509 * unmap_vmas() assumes that the caller will flush the whole unmapped address
510 * range after unmap_vmas() returns. So the only responsibility here is to
511 * ensure that any thus-far unmapped pages are flushed before unmap_vmas()
512 * drops the lock and schedules.
514 int unmap_vmas(struct mmu_gather **tlbp, struct mm_struct *mm,
515 struct vm_area_struct *vma, unsigned long start_addr,
516 unsigned long end_addr, unsigned long *nr_accounted,
517 struct zap_details *details)
519 unsigned long zap_bytes = ZAP_BLOCK_SIZE;
520 unsigned long tlb_start = 0; /* For tlb_finish_mmu */
521 int tlb_start_valid = 0;
523 int atomic = details && details->atomic;
525 for ( ; vma && vma->vm_start < end_addr; vma = vma->vm_next) {
529 start = max(vma->vm_start, start_addr);
530 if (start >= vma->vm_end)
532 end = min(vma->vm_end, end_addr);
533 if (end <= vma->vm_start)
536 if (vma->vm_flags & VM_ACCOUNT)
537 *nr_accounted += (end - start) >> PAGE_SHIFT;
540 while (start != end) {
543 if (!tlb_start_valid) {
548 if (is_vm_hugetlb_page(vma)) {
550 unmap_hugepage_range(vma, start, end);
552 block = min(zap_bytes, end - start);
553 unmap_page_range(*tlbp, vma, start,
554 start + block, details);
559 if ((long)zap_bytes > 0)
561 if (!atomic && need_resched()) {
562 int fullmm = tlb_is_full_mm(*tlbp);
563 tlb_finish_mmu(*tlbp, tlb_start, start);
564 cond_resched_lock(&mm->page_table_lock);
565 *tlbp = tlb_gather_mmu(mm, fullmm);
568 zap_bytes = ZAP_BLOCK_SIZE;
575 * zap_page_range - remove user pages in a given range
576 * @vma: vm_area_struct holding the applicable pages
577 * @address: starting address of pages to zap
578 * @size: number of bytes to zap
579 * @details: details of nonlinear truncation or shared cache invalidation
581 void zap_page_range(struct vm_area_struct *vma, unsigned long address,
582 unsigned long size, struct zap_details *details)
584 struct mm_struct *mm = vma->vm_mm;
585 struct mmu_gather *tlb;
586 unsigned long end = address + size;
587 unsigned long nr_accounted = 0;
589 if (is_vm_hugetlb_page(vma)) {
590 zap_hugepage_range(vma, address, size);
595 spin_lock(&mm->page_table_lock);
596 tlb = tlb_gather_mmu(mm, 0);
597 unmap_vmas(&tlb, mm, vma, address, end, &nr_accounted, details);
598 tlb_finish_mmu(tlb, address, end);
599 spin_unlock(&mm->page_table_lock);
603 * Do a quick page-table lookup for a single page.
604 * mm->page_table_lock must be held.
607 follow_page(struct mm_struct *mm, unsigned long address, int write)
615 page = follow_huge_addr(mm, address, write);
619 pgd = pgd_offset(mm, address);
620 if (pgd_none(*pgd) || unlikely(pgd_bad(*pgd)))
623 pmd = pmd_offset(pgd, address);
627 return follow_huge_pmd(mm, address, pmd, write);
628 if (unlikely(pmd_bad(*pmd)))
631 ptep = pte_offset_map(pmd, address);
637 if (pte_present(pte)) {
638 if (write && !pte_write(pte))
641 if (pfn_valid(pfn)) {
642 page = pfn_to_page(pfn);
643 if (write && !pte_dirty(pte) && !PageDirty(page))
644 set_page_dirty(page);
645 mark_page_accessed(page);
655 * Given a physical address, is there a useful struct page pointing to
656 * it? This may become more complex in the future if we start dealing
657 * with IO-aperture pages for direct-IO.
660 static inline struct page *get_page_map(struct page *page)
662 if (!pfn_valid(page_to_pfn(page)))
669 untouched_anonymous_page(struct mm_struct* mm, struct vm_area_struct *vma,
670 unsigned long address)
675 /* Check if the vma is for an anonymous mapping. */
676 if (vma->vm_ops && vma->vm_ops->nopage)
679 /* Check if page directory entry exists. */
680 pgd = pgd_offset(mm, address);
681 if (pgd_none(*pgd) || unlikely(pgd_bad(*pgd)))
684 /* Check if page middle directory entry exists. */
685 pmd = pmd_offset(pgd, address);
686 if (pmd_none(*pmd) || unlikely(pmd_bad(*pmd)))
689 /* There is a pte slot for 'address' in 'mm'. */
694 int get_user_pages(struct task_struct *tsk, struct mm_struct *mm,
695 unsigned long start, int len, int write, int force,
696 struct page **pages, struct vm_area_struct **vmas)
702 * Require read or write permissions.
703 * If 'force' is set, we only require the "MAY" flags.
705 flags = write ? (VM_WRITE | VM_MAYWRITE) : (VM_READ | VM_MAYREAD);
706 flags &= force ? (VM_MAYREAD | VM_MAYWRITE) : (VM_READ | VM_WRITE);
710 struct vm_area_struct * vma;
712 vma = find_extend_vma(mm, start);
713 if (!vma && in_gate_area(tsk, start)) {
714 unsigned long pg = start & PAGE_MASK;
715 struct vm_area_struct *gate_vma = get_gate_vma(tsk);
719 if (write) /* user gate pages are read-only */
720 return i ? : -EFAULT;
721 pgd = pgd_offset_k(pg);
723 return i ? : -EFAULT;
724 pmd = pmd_offset(pgd, pg);
726 return i ? : -EFAULT;
727 pte = pte_offset_kernel(pmd, pg);
728 if (!pte || !pte_present(*pte))
729 return i ? : -EFAULT;
731 pages[i] = pte_page(*pte);
742 if (!vma || (pages && (vma->vm_flags & VM_IO))
743 || !(flags & vma->vm_flags))
744 return i ? : -EFAULT;
746 if (is_vm_hugetlb_page(vma)) {
747 i = follow_hugetlb_page(mm, vma, pages, vmas,
751 spin_lock(&mm->page_table_lock);
754 int lookup_write = write;
755 while (!(map = follow_page(mm, start, lookup_write))) {
757 * Shortcut for anonymous pages. We don't want
758 * to force the creation of pages tables for
759 * insanly big anonymously mapped areas that
760 * nobody touched so far. This is important
761 * for doing a core dump for these mappings.
764 untouched_anonymous_page(mm,vma,start)) {
765 map = ZERO_PAGE(start);
768 spin_unlock(&mm->page_table_lock);
769 switch (handle_mm_fault(mm,vma,start,write)) {
776 case VM_FAULT_SIGBUS:
777 return i ? i : -EFAULT;
779 return i ? i : -ENOMEM;
784 * Now that we have performed a write fault
785 * and surely no longer have a shared page we
786 * shouldn't write, we shouldn't ignore an
787 * unwritable page in the page table if
788 * we are forcing write access.
790 lookup_write = write && !force;
791 spin_lock(&mm->page_table_lock);
794 pages[i] = get_page_map(map);
796 spin_unlock(&mm->page_table_lock);
798 page_cache_release(pages[i]);
802 flush_dcache_page(pages[i]);
803 if (!PageReserved(pages[i]))
804 page_cache_get(pages[i]);
811 } while(len && start < vma->vm_end);
812 spin_unlock(&mm->page_table_lock);
818 EXPORT_SYMBOL(get_user_pages);
820 static void zeromap_pte_range(pte_t * pte, unsigned long address,
821 unsigned long size, pgprot_t prot)
825 address &= ~PMD_MASK;
826 end = address + size;
830 pte_t zero_pte = pte_wrprotect(mk_pte(ZERO_PAGE(address), prot));
831 BUG_ON(!pte_none(*pte));
832 set_pte(pte, zero_pte);
833 address += PAGE_SIZE;
835 } while (address && (address < end));
838 static inline int zeromap_pmd_range(struct mm_struct *mm, pmd_t * pmd, unsigned long address,
839 unsigned long size, pgprot_t prot)
841 unsigned long base, end;
843 base = address & PGDIR_MASK;
844 address &= ~PGDIR_MASK;
845 end = address + size;
846 if (end > PGDIR_SIZE)
849 pte_t * pte = pte_alloc_map(mm, pmd, base + address);
852 zeromap_pte_range(pte, base + address, end - address, prot);
854 address = (address + PMD_SIZE) & PMD_MASK;
856 } while (address && (address < end));
860 int zeromap_page_range(struct vm_area_struct *vma, unsigned long address, unsigned long size, pgprot_t prot)
864 unsigned long beg = address;
865 unsigned long end = address + size;
866 struct mm_struct *mm = vma->vm_mm;
868 dir = pgd_offset(mm, address);
869 flush_cache_range(vma, beg, end);
873 spin_lock(&mm->page_table_lock);
875 pmd_t *pmd = pmd_alloc(mm, dir, address);
879 error = zeromap_pmd_range(mm, pmd, address, end - address, prot);
882 address = (address + PGDIR_SIZE) & PGDIR_MASK;
884 } while (address && (address < end));
886 * Why flush? zeromap_pte_range has a BUG_ON for !pte_none()
888 flush_tlb_range(vma, beg, end);
889 spin_unlock(&mm->page_table_lock);
894 * maps a range of physical memory into the requested pages. the old
895 * mappings are removed. any references to nonexistent pages results
896 * in null mappings (currently treated as "copy-on-access")
898 static inline void remap_pte_range(pte_t * pte, unsigned long address, unsigned long size,
899 unsigned long phys_addr, pgprot_t prot)
904 address &= ~PMD_MASK;
905 end = address + size;
908 pfn = phys_addr >> PAGE_SHIFT;
910 BUG_ON(!pte_none(*pte));
911 if (!pfn_valid(pfn) || PageReserved(pfn_to_page(pfn)))
912 set_pte(pte, pfn_pte(pfn, prot));
913 address += PAGE_SIZE;
916 } while (address && (address < end));
919 static inline int remap_pmd_range(struct mm_struct *mm, pmd_t * pmd, unsigned long address, unsigned long size,
920 unsigned long phys_addr, pgprot_t prot)
922 unsigned long base, end;
924 base = address & PGDIR_MASK;
925 address &= ~PGDIR_MASK;
926 end = address + size;
927 if (end > PGDIR_SIZE)
929 phys_addr -= address;
931 pte_t * pte = pte_alloc_map(mm, pmd, base + address);
934 remap_pte_range(pte, base + address, end - address, address + phys_addr, prot);
936 address = (address + PMD_SIZE) & PMD_MASK;
938 } while (address && (address < end));
942 /* Note: this is only safe if the mm semaphore is held when called. */
943 int remap_page_range(struct vm_area_struct *vma, unsigned long from, unsigned long phys_addr, unsigned long size, pgprot_t prot)
947 unsigned long beg = from;
948 unsigned long end = from + size;
949 struct mm_struct *mm = vma->vm_mm;
952 dir = pgd_offset(mm, from);
953 flush_cache_range(vma, beg, end);
957 spin_lock(&mm->page_table_lock);
959 pmd_t *pmd = pmd_alloc(mm, dir, from);
963 error = remap_pmd_range(mm, pmd, from, end - from, phys_addr + from, prot);
966 from = (from + PGDIR_SIZE) & PGDIR_MASK;
968 } while (from && (from < end));
970 * Why flush? remap_pte_range has a BUG_ON for !pte_none()
972 flush_tlb_range(vma, beg, end);
973 spin_unlock(&mm->page_table_lock);
977 EXPORT_SYMBOL(remap_page_range);
980 * Do pte_mkwrite, but only if the vma says VM_WRITE. We do this when
981 * servicing faults for write access. In the normal case, do always want
982 * pte_mkwrite. But get_user_pages can cause write faults for mappings
983 * that do not have writing enabled, when used by access_process_vm.
985 static inline pte_t maybe_mkwrite(pte_t pte, struct vm_area_struct *vma)
987 if (likely(vma->vm_flags & VM_WRITE))
988 pte = pte_mkwrite(pte);
993 * We hold the mm semaphore for reading and vma->vm_mm->page_table_lock
995 static inline void break_cow(struct vm_area_struct * vma, struct page * new_page, unsigned long address,
1000 flush_cache_page(vma, address);
1001 entry = maybe_mkwrite(pte_mkdirty(mk_pte(new_page, vma->vm_page_prot)),
1003 ptep_establish(vma, address, page_table, entry);
1004 update_mmu_cache(vma, address, entry);
1008 * This routine handles present pages, when users try to write
1009 * to a shared page. It is done by copying the page to a new address
1010 * and decrementing the shared-page counter for the old page.
1012 * Goto-purists beware: the only reason for goto's here is that it results
1013 * in better assembly code.. The "default" path will see no jumps at all.
1015 * Note that this routine assumes that the protection checks have been
1016 * done by the caller (the low-level page fault routine in most cases).
1017 * Thus we can safely just mark it writable once we've done any necessary
1020 * We also mark the page dirty at this point even though the page will
1021 * change only once the write actually happens. This avoids a few races,
1022 * and potentially makes it more efficient.
1024 * We hold the mm semaphore and the page_table_lock on entry and exit
1025 * with the page_table_lock released.
1027 static int do_wp_page(struct mm_struct *mm, struct vm_area_struct * vma,
1028 unsigned long address, pte_t *page_table, pmd_t *pmd, pte_t pte)
1030 struct page *old_page, *new_page;
1031 unsigned long pfn = pte_pfn(pte);
1034 if (unlikely(!pfn_valid(pfn))) {
1036 * This should really halt the system so it can be debugged or
1037 * at least the kernel stops what it's doing before it corrupts
1038 * data, but for the moment just pretend this is OOM.
1040 pte_unmap(page_table);
1041 printk(KERN_ERR "do_wp_page: bogus page at address %08lx\n",
1043 spin_unlock(&mm->page_table_lock);
1044 return VM_FAULT_OOM;
1046 old_page = pfn_to_page(pfn);
1048 if (!TestSetPageLocked(old_page)) {
1049 int reuse = can_share_swap_page(old_page);
1050 unlock_page(old_page);
1052 flush_cache_page(vma, address);
1053 entry = maybe_mkwrite(pte_mkyoung(pte_mkdirty(pte)),
1055 ptep_set_access_flags(vma, address, page_table, entry, 1);
1056 update_mmu_cache(vma, address, entry);
1057 pte_unmap(page_table);
1058 spin_unlock(&mm->page_table_lock);
1059 return VM_FAULT_MINOR;
1062 pte_unmap(page_table);
1065 * Ok, we need to copy. Oh, well..
1067 page_cache_get(old_page);
1068 spin_unlock(&mm->page_table_lock);
1070 if (unlikely(anon_vma_prepare(vma)))
1072 new_page = alloc_page_vma(GFP_HIGHUSER, vma, address);
1075 copy_cow_page(old_page,new_page,address);
1078 * Re-check the pte - we dropped the lock
1080 spin_lock(&mm->page_table_lock);
1081 page_table = pte_offset_map(pmd, address);
1082 if (likely(pte_same(*page_table, pte))) {
1083 if (PageReserved(old_page))
1086 page_remove_rmap(old_page);
1087 break_cow(vma, new_page, address, page_table);
1088 lru_cache_add_active(new_page);
1089 page_add_anon_rmap(new_page, vma, address);
1091 /* Free the old page.. */
1092 new_page = old_page;
1094 pte_unmap(page_table);
1095 page_cache_release(new_page);
1096 page_cache_release(old_page);
1097 spin_unlock(&mm->page_table_lock);
1098 return VM_FAULT_MINOR;
1101 page_cache_release(old_page);
1102 return VM_FAULT_OOM;
1106 * Helper function for unmap_mapping_range().
1108 static inline void unmap_mapping_range_list(struct prio_tree_root *root,
1109 struct zap_details *details)
1111 struct vm_area_struct *vma = NULL;
1112 struct prio_tree_iter iter;
1113 pgoff_t vba, vea, zba, zea;
1115 while ((vma = vma_prio_tree_next(vma, root, &iter,
1116 details->first_index, details->last_index)) != NULL) {
1117 vba = vma->vm_pgoff;
1118 vea = vba + ((vma->vm_end - vma->vm_start) >> PAGE_SHIFT) - 1;
1119 /* Assume for now that PAGE_CACHE_SHIFT == PAGE_SHIFT */
1120 zba = details->first_index;
1123 zea = details->last_index;
1127 ((zba - vba) << PAGE_SHIFT) + vma->vm_start,
1128 (zea - zba + 1) << PAGE_SHIFT, details);
1133 * unmap_mapping_range - unmap the portion of all mmaps
1134 * in the specified address_space corresponding to the specified
1135 * page range in the underlying file.
1136 * @address_space: the address space containing mmaps to be unmapped.
1137 * @holebegin: byte in first page to unmap, relative to the start of
1138 * the underlying file. This will be rounded down to a PAGE_SIZE
1139 * boundary. Note that this is different from vmtruncate(), which
1140 * must keep the partial page. In contrast, we must get rid of
1142 * @holelen: size of prospective hole in bytes. This will be rounded
1143 * up to a PAGE_SIZE boundary. A holelen of zero truncates to the
1145 * @even_cows: 1 when truncating a file, unmap even private COWed pages;
1146 * but 0 when invalidating pagecache, don't throw away private data.
1148 void unmap_mapping_range(struct address_space *mapping,
1149 loff_t const holebegin, loff_t const holelen, int even_cows)
1151 struct zap_details details;
1152 pgoff_t hba = holebegin >> PAGE_SHIFT;
1153 pgoff_t hlen = (holelen + PAGE_SIZE - 1) >> PAGE_SHIFT;
1155 /* Check for overflow. */
1156 if (sizeof(holelen) > sizeof(hlen)) {
1158 (holebegin + holelen + PAGE_SIZE - 1) >> PAGE_SHIFT;
1159 if (holeend & ~(long long)ULONG_MAX)
1160 hlen = ULONG_MAX - hba + 1;
1163 details.check_mapping = even_cows? NULL: mapping;
1164 details.nonlinear_vma = NULL;
1165 details.first_index = hba;
1166 details.last_index = hba + hlen - 1;
1167 details.atomic = 1; /* A spinlock is held */
1168 if (details.last_index < details.first_index)
1169 details.last_index = ULONG_MAX;
1171 spin_lock(&mapping->i_mmap_lock);
1172 /* Protect against page fault */
1173 atomic_inc(&mapping->truncate_count);
1175 if (unlikely(!prio_tree_empty(&mapping->i_mmap)))
1176 unmap_mapping_range_list(&mapping->i_mmap, &details);
1179 * In nonlinear VMAs there is no correspondence between virtual address
1180 * offset and file offset. So we must perform an exhaustive search
1181 * across *all* the pages in each nonlinear VMA, not just the pages
1182 * whose virtual address lies outside the file truncation point.
1184 if (unlikely(!list_empty(&mapping->i_mmap_nonlinear))) {
1185 struct vm_area_struct *vma;
1186 list_for_each_entry(vma, &mapping->i_mmap_nonlinear,
1187 shared.vm_set.list) {
1188 details.nonlinear_vma = vma;
1189 zap_page_range(vma, vma->vm_start,
1190 vma->vm_end - vma->vm_start, &details);
1193 spin_unlock(&mapping->i_mmap_lock);
1195 EXPORT_SYMBOL(unmap_mapping_range);
1198 * Handle all mappings that got truncated by a "truncate()"
1201 * NOTE! We have to be ready to update the memory sharing
1202 * between the file and the memory map for a potential last
1203 * incomplete page. Ugly, but necessary.
1205 int vmtruncate(struct inode * inode, loff_t offset)
1207 struct address_space *mapping = inode->i_mapping;
1208 unsigned long limit;
1210 if (inode->i_size < offset)
1212 i_size_write(inode, offset);
1213 unmap_mapping_range(mapping, offset + PAGE_SIZE - 1, 0, 1);
1214 truncate_inode_pages(mapping, offset);
1218 limit = current->rlim[RLIMIT_FSIZE].rlim_cur;
1219 if (limit != RLIM_INFINITY && offset > limit)
1221 if (offset > inode->i_sb->s_maxbytes)
1223 i_size_write(inode, offset);
1226 if (inode->i_op && inode->i_op->truncate)
1227 inode->i_op->truncate(inode);
1230 send_sig(SIGXFSZ, current, 0);
1235 EXPORT_SYMBOL(vmtruncate);
1238 * Primitive swap readahead code. We simply read an aligned block of
1239 * (1 << page_cluster) entries in the swap area. This method is chosen
1240 * because it doesn't cost us any seek time. We also make sure to queue
1241 * the 'original' request together with the readahead ones...
1243 * This has been extended to use the NUMA policies from the mm triggering
1246 * Caller must hold down_read on the vma->vm_mm if vma is not NULL.
1248 void swapin_readahead(swp_entry_t entry, unsigned long addr,struct vm_area_struct *vma)
1251 struct vm_area_struct *next_vma = vma ? vma->vm_next : NULL;
1254 struct page *new_page;
1255 unsigned long offset;
1258 * Get the number of handles we should do readahead io to.
1260 num = valid_swaphandles(entry, &offset);
1261 for (i = 0; i < num; offset++, i++) {
1262 /* Ok, do the async read-ahead now */
1263 new_page = read_swap_cache_async(swp_entry(swp_type(entry),
1264 offset), vma, addr);
1267 page_cache_release(new_page);
1270 * Find the next applicable VMA for the NUMA policy.
1276 if (addr >= vma->vm_end) {
1278 next_vma = vma ? vma->vm_next : NULL;
1280 if (vma && addr < vma->vm_start)
1283 if (next_vma && addr >= next_vma->vm_start) {
1285 next_vma = vma->vm_next;
1290 lru_add_drain(); /* Push any new pages onto the LRU now */
1294 * We hold the mm semaphore and the page_table_lock on entry and
1295 * should release the pagetable lock on exit..
1297 static int do_swap_page(struct mm_struct * mm,
1298 struct vm_area_struct * vma, unsigned long address,
1299 pte_t *page_table, pmd_t *pmd, pte_t orig_pte, int write_access)
1302 swp_entry_t entry = pte_to_swp_entry(orig_pte);
1304 int ret = VM_FAULT_MINOR;
1306 pte_unmap(page_table);
1307 spin_unlock(&mm->page_table_lock);
1308 page = lookup_swap_cache(entry);
1310 swapin_readahead(entry, address, vma);
1311 page = read_swap_cache_async(entry, vma, address);
1314 * Back out if somebody else faulted in this pte while
1315 * we released the page table lock.
1317 spin_lock(&mm->page_table_lock);
1318 page_table = pte_offset_map(pmd, address);
1319 if (likely(pte_same(*page_table, orig_pte)))
1322 ret = VM_FAULT_MINOR;
1323 pte_unmap(page_table);
1324 spin_unlock(&mm->page_table_lock);
1328 /* Had to read the page from swap area: Major fault */
1329 ret = VM_FAULT_MAJOR;
1330 inc_page_state(pgmajfault);
1333 mark_page_accessed(page);
1337 * Back out if somebody else faulted in this pte while we
1338 * released the page table lock.
1340 spin_lock(&mm->page_table_lock);
1341 page_table = pte_offset_map(pmd, address);
1342 if (unlikely(!pte_same(*page_table, orig_pte))) {
1343 pte_unmap(page_table);
1344 spin_unlock(&mm->page_table_lock);
1346 page_cache_release(page);
1347 ret = VM_FAULT_MINOR;
1351 /* The page isn't present yet, go ahead with the fault. */
1355 remove_exclusive_swap_page(page);
1358 pte = mk_pte(page, vma->vm_page_prot);
1359 if (write_access && can_share_swap_page(page)) {
1360 pte = maybe_mkwrite(pte_mkdirty(pte), vma);
1365 flush_icache_page(vma, page);
1366 set_pte(page_table, pte);
1367 page_add_anon_rmap(page, vma, address);
1370 if (do_wp_page(mm, vma, address,
1371 page_table, pmd, pte) == VM_FAULT_OOM)
1376 /* No need to invalidate - it was non-present before */
1377 update_mmu_cache(vma, address, pte);
1378 pte_unmap(page_table);
1379 spin_unlock(&mm->page_table_lock);
1385 * We are called with the MM semaphore and page_table_lock
1386 * spinlock held to protect against concurrent faults in
1387 * multithreaded programs.
1390 do_anonymous_page(struct mm_struct *mm, struct vm_area_struct *vma,
1391 pte_t *page_table, pmd_t *pmd, int write_access,
1395 struct page * page = ZERO_PAGE(addr);
1397 /* Read-only mapping of ZERO_PAGE. */
1398 entry = pte_wrprotect(mk_pte(ZERO_PAGE(addr), vma->vm_page_prot));
1400 /* ..except if it's a write access */
1402 /* Allocate our own private page. */
1403 pte_unmap(page_table);
1404 spin_unlock(&mm->page_table_lock);
1406 if (unlikely(anon_vma_prepare(vma)))
1408 page = alloc_page_vma(GFP_HIGHUSER, vma, addr);
1411 clear_user_highpage(page, addr);
1413 spin_lock(&mm->page_table_lock);
1414 page_table = pte_offset_map(pmd, addr);
1416 if (!pte_none(*page_table)) {
1417 pte_unmap(page_table);
1418 page_cache_release(page);
1419 spin_unlock(&mm->page_table_lock);
1423 entry = maybe_mkwrite(pte_mkdirty(mk_pte(page,
1424 vma->vm_page_prot)),
1426 lru_cache_add_active(page);
1427 mark_page_accessed(page);
1428 page_add_anon_rmap(page, vma, addr);
1431 set_pte(page_table, entry);
1432 pte_unmap(page_table);
1434 /* No need to invalidate - it was non-present before */
1435 update_mmu_cache(vma, addr, entry);
1436 spin_unlock(&mm->page_table_lock);
1438 return VM_FAULT_MINOR;
1440 return VM_FAULT_OOM;
1444 * do_no_page() tries to create a new page mapping. It aggressively
1445 * tries to share with existing pages, but makes a separate copy if
1446 * the "write_access" parameter is true in order to avoid the next
1449 * As this is called only for pages that do not currently exist, we
1450 * do not need to flush old virtual caches or the TLB.
1452 * This is called with the MM semaphore held and the page table
1453 * spinlock held. Exit with the spinlock released.
1456 do_no_page(struct mm_struct *mm, struct vm_area_struct *vma,
1457 unsigned long address, int write_access, pte_t *page_table, pmd_t *pmd)
1459 struct page * new_page;
1460 struct address_space *mapping = NULL;
1463 int ret = VM_FAULT_MINOR;
1466 if (!vma->vm_ops || !vma->vm_ops->nopage)
1467 return do_anonymous_page(mm, vma, page_table,
1468 pmd, write_access, address);
1469 pte_unmap(page_table);
1470 spin_unlock(&mm->page_table_lock);
1473 mapping = vma->vm_file->f_mapping;
1474 sequence = atomic_read(&mapping->truncate_count);
1476 smp_rmb(); /* Prevent CPU from reordering lock-free ->nopage() */
1478 new_page = vma->vm_ops->nopage(vma, address & PAGE_MASK, &ret);
1480 /* no page was available -- either SIGBUS or OOM */
1481 if (new_page == NOPAGE_SIGBUS)
1482 return VM_FAULT_SIGBUS;
1483 if (new_page == NOPAGE_OOM)
1484 return VM_FAULT_OOM;
1487 * Should we do an early C-O-W break?
1489 if (write_access && !(vma->vm_flags & VM_SHARED)) {
1492 if (unlikely(anon_vma_prepare(vma)))
1494 page = alloc_page_vma(GFP_HIGHUSER, vma, address);
1497 copy_user_highpage(page, new_page, address);
1498 page_cache_release(new_page);
1503 spin_lock(&mm->page_table_lock);
1505 * For a file-backed vma, someone could have truncated or otherwise
1506 * invalidated this page. If unmap_mapping_range got called,
1507 * retry getting the page.
1510 (unlikely(sequence != atomic_read(&mapping->truncate_count)))) {
1511 sequence = atomic_read(&mapping->truncate_count);
1512 spin_unlock(&mm->page_table_lock);
1513 page_cache_release(new_page);
1516 page_table = pte_offset_map(pmd, address);
1519 * This silly early PAGE_DIRTY setting removes a race
1520 * due to the bad i386 page protection. But it's valid
1521 * for other architectures too.
1523 * Note that if write_access is true, we either now have
1524 * an exclusive copy of the page, or this is a shared mapping,
1525 * so we can make it writable and dirty to avoid having to
1526 * handle that later.
1528 /* Only go through if we didn't race with anybody else... */
1529 if (pte_none(*page_table)) {
1530 if (!PageReserved(new_page))
1532 flush_icache_page(vma, new_page);
1533 entry = mk_pte(new_page, vma->vm_page_prot);
1535 entry = maybe_mkwrite(pte_mkdirty(entry), vma);
1536 set_pte(page_table, entry);
1538 lru_cache_add_active(new_page);
1539 page_add_anon_rmap(new_page, vma, address);
1541 page_add_file_rmap(new_page);
1542 pte_unmap(page_table);
1544 /* One of our sibling threads was faster, back out. */
1545 pte_unmap(page_table);
1546 page_cache_release(new_page);
1547 spin_unlock(&mm->page_table_lock);
1551 /* no need to invalidate: a not-present page shouldn't be cached */
1552 update_mmu_cache(vma, address, entry);
1553 spin_unlock(&mm->page_table_lock);
1557 page_cache_release(new_page);
1563 * Fault of a previously existing named mapping. Repopulate the pte
1564 * from the encoded file_pte if possible. This enables swappable
1567 static int do_file_page(struct mm_struct * mm, struct vm_area_struct * vma,
1568 unsigned long address, int write_access, pte_t *pte, pmd_t *pmd)
1570 unsigned long pgoff;
1573 BUG_ON(!vma->vm_ops || !vma->vm_ops->nopage);
1575 * Fall back to the linear mapping if the fs does not support
1578 if (!vma->vm_ops || !vma->vm_ops->populate ||
1579 (write_access && !(vma->vm_flags & VM_SHARED))) {
1581 return do_no_page(mm, vma, address, write_access, pte, pmd);
1584 pgoff = pte_to_pgoff(*pte);
1587 spin_unlock(&mm->page_table_lock);
1589 err = vma->vm_ops->populate(vma, address & PAGE_MASK, PAGE_SIZE, vma->vm_page_prot, pgoff, 0);
1591 return VM_FAULT_OOM;
1593 return VM_FAULT_SIGBUS;
1594 return VM_FAULT_MAJOR;
1598 * These routines also need to handle stuff like marking pages dirty
1599 * and/or accessed for architectures that don't do it in hardware (most
1600 * RISC architectures). The early dirtying is also good on the i386.
1602 * There is also a hook called "update_mmu_cache()" that architectures
1603 * with external mmu caches can use to update those (ie the Sparc or
1604 * PowerPC hashed page tables that act as extended TLBs).
1606 * Note the "page_table_lock". It is to protect against kswapd removing
1607 * pages from under us. Note that kswapd only ever _removes_ pages, never
1608 * adds them. As such, once we have noticed that the page is not present,
1609 * we can drop the lock early.
1611 * The adding of pages is protected by the MM semaphore (which we hold),
1612 * so we don't need to worry about a page being suddenly been added into
1615 * We enter with the pagetable spinlock held, we are supposed to
1616 * release it when done.
1618 static inline int handle_pte_fault(struct mm_struct *mm,
1619 struct vm_area_struct * vma, unsigned long address,
1620 int write_access, pte_t *pte, pmd_t *pmd)
1625 if (!pte_present(entry)) {
1627 * If it truly wasn't present, we know that kswapd
1628 * and the PTE updates will not touch it later. So
1631 if (pte_none(entry))
1632 return do_no_page(mm, vma, address, write_access, pte, pmd);
1633 if (pte_file(entry))
1634 return do_file_page(mm, vma, address, write_access, pte, pmd);
1635 return do_swap_page(mm, vma, address, pte, pmd, entry, write_access);
1639 if (!pte_write(entry))
1640 return do_wp_page(mm, vma, address, pte, pmd, entry);
1642 entry = pte_mkdirty(entry);
1644 entry = pte_mkyoung(entry);
1645 ptep_set_access_flags(vma, address, pte, entry, write_access);
1646 update_mmu_cache(vma, address, entry);
1648 spin_unlock(&mm->page_table_lock);
1649 return VM_FAULT_MINOR;
1653 * By the time we get here, we already hold the mm semaphore
1655 int handle_mm_fault(struct mm_struct *mm, struct vm_area_struct * vma,
1656 unsigned long address, int write_access)
1661 __set_current_state(TASK_RUNNING);
1662 pgd = pgd_offset(mm, address);
1664 inc_page_state(pgfault);
1666 if (is_vm_hugetlb_page(vma))
1667 return VM_FAULT_SIGBUS; /* mapping truncation does this. */
1670 * We need the page table lock to synchronize with kswapd
1671 * and the SMP-safe atomic PTE updates.
1673 spin_lock(&mm->page_table_lock);
1674 pmd = pmd_alloc(mm, pgd, address);
1677 pte_t * pte = pte_alloc_map(mm, pmd, address);
1679 return handle_pte_fault(mm, vma, address, write_access, pte, pmd);
1681 spin_unlock(&mm->page_table_lock);
1682 return VM_FAULT_OOM;
1686 * Allocate page middle directory.
1688 * We've already handled the fast-path in-line, and we own the
1691 * On a two-level page table, this ends up actually being entirely
1694 pmd_t fastcall *__pmd_alloc(struct mm_struct *mm, pgd_t *pgd, unsigned long address)
1698 spin_unlock(&mm->page_table_lock);
1699 new = pmd_alloc_one(mm, address);
1700 spin_lock(&mm->page_table_lock);
1705 * Because we dropped the lock, we should re-check the
1706 * entry, as somebody else could have populated it..
1708 if (pgd_present(*pgd)) {
1712 pgd_populate(mm, pgd, new);
1714 return pmd_offset(pgd, address);
1717 int make_pages_present(unsigned long addr, unsigned long end)
1719 int ret, len, write;
1720 struct vm_area_struct * vma;
1722 vma = find_vma(current->mm, addr);
1723 write = (vma->vm_flags & VM_WRITE) != 0;
1726 if (end > vma->vm_end)
1728 len = (end+PAGE_SIZE-1)/PAGE_SIZE-addr/PAGE_SIZE;
1729 ret = get_user_pages(current, current->mm, addr,
1730 len, write, 0, NULL, NULL);
1733 return ret == len ? 0 : -1;
1737 * Map a vmalloc()-space virtual address to the physical page.
1739 struct page * vmalloc_to_page(void * vmalloc_addr)
1741 unsigned long addr = (unsigned long) vmalloc_addr;
1742 struct page *page = NULL;
1743 pgd_t *pgd = pgd_offset_k(addr);
1747 if (!pgd_none(*pgd)) {
1748 pmd = pmd_offset(pgd, addr);
1749 if (!pmd_none(*pmd)) {
1751 ptep = pte_offset_map(pmd, addr);
1753 if (pte_present(pte))
1754 page = pte_page(pte);
1762 EXPORT_SYMBOL(vmalloc_to_page);
1764 #if !defined(CONFIG_ARCH_GATE_AREA)
1766 #if defined(AT_SYSINFO_EHDR)
1767 struct vm_area_struct gate_vma;
1769 static int __init gate_vma_init(void)
1771 gate_vma.vm_mm = NULL;
1772 gate_vma.vm_start = FIXADDR_USER_START;
1773 gate_vma.vm_end = FIXADDR_USER_END;
1774 gate_vma.vm_page_prot = PAGE_READONLY;
1775 gate_vma.vm_flags = 0;
1778 __initcall(gate_vma_init);
1781 struct vm_area_struct *get_gate_vma(struct task_struct *tsk)
1783 #ifdef AT_SYSINFO_EHDR
1790 int in_gate_area(struct task_struct *task, unsigned long addr)
1792 #ifdef AT_SYSINFO_EHDR
1793 if ((addr >= FIXADDR_USER_START) && (addr < FIXADDR_USER_END))