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 * A number of key systems in x86 including ioremap() rely on the assumption
71 * that high_memory defines the upper bound on direct map memory, then end
72 * of ZONE_NORMAL. Under CONFIG_DISCONTIG this means that max_low_pfn and
73 * highstart_pfn must be the same; there must be no gap between ZONE_NORMAL
77 struct page
*highmem_start_page
;
78 unsigned long vmalloc_earlyreserve
;
80 EXPORT_SYMBOL(num_physpages
);
81 EXPORT_SYMBOL(highmem_start_page
);
82 EXPORT_SYMBOL(high_memory
);
83 EXPORT_SYMBOL(vmalloc_earlyreserve
);
86 * We special-case the C-O-W ZERO_PAGE, because it's such
87 * a common occurrence (no need to read the page to know
88 * that it's zero - better for the cache and memory subsystem).
90 static inline void copy_cow_page(struct page
* from
, struct page
* to
, unsigned long address
)
92 if (from
== ZERO_PAGE(address
)) {
93 clear_user_highpage(to
, address
);
96 copy_user_highpage(to
, from
, address
);
100 * Note: this doesn't free the actual pages themselves. That
101 * has been handled earlier when unmapping all the memory regions.
103 static inline void free_one_pmd(struct mmu_gather
*tlb
, pmd_t
* dir
)
109 if (unlikely(pmd_bad(*dir
))) {
114 page
= pmd_page(*dir
);
116 dec_page_state(nr_page_table_pages
);
117 pte_free_tlb(tlb
, page
);
120 static inline void free_one_pgd(struct mmu_gather
*tlb
, pgd_t
* dir
)
127 if (unlikely(pgd_bad(*dir
))) {
132 pmd
= pmd_offset(dir
, 0);
134 for (j
= 0; j
< PTRS_PER_PMD
; j
++)
135 free_one_pmd(tlb
, pmd
+j
);
136 pmd_free_tlb(tlb
, pmd
);
140 * This function clears all user-level page tables of a process - this
141 * is needed by execve(), so that old pages aren't in the way.
143 * Must be called with pagetable lock held.
145 void clear_page_tables(struct mmu_gather
*tlb
, unsigned long first
, int nr
)
147 pgd_t
* page_dir
= tlb
->mm
->pgd
;
151 free_one_pgd(tlb
, page_dir
);
156 pte_t fastcall
* pte_alloc_map(struct mm_struct
*mm
, pmd_t
*pmd
, unsigned long address
)
158 if (!pmd_present(*pmd
)) {
161 spin_unlock(&mm
->page_table_lock
);
162 new = pte_alloc_one(mm
, address
);
163 spin_lock(&mm
->page_table_lock
);
168 * Because we dropped the lock, we should re-check the
169 * entry, as somebody else could have populated it..
171 if (pmd_present(*pmd
)) {
175 inc_page_state(nr_page_table_pages
);
176 pmd_populate(mm
, pmd
, new);
179 return pte_offset_map(pmd
, address
);
182 pte_t fastcall
* pte_alloc_kernel(struct mm_struct
*mm
, pmd_t
*pmd
, unsigned long address
)
184 if (!pmd_present(*pmd
)) {
187 spin_unlock(&mm
->page_table_lock
);
188 new = pte_alloc_one_kernel(mm
, address
);
189 spin_lock(&mm
->page_table_lock
);
194 * Because we dropped the lock, we should re-check the
195 * entry, as somebody else could have populated it..
197 if (pmd_present(*pmd
)) {
198 pte_free_kernel(new);
201 pmd_populate_kernel(mm
, pmd
, new);
204 return pte_offset_kernel(pmd
, address
);
206 #define PTE_TABLE_MASK ((PTRS_PER_PTE-1) * sizeof(pte_t))
207 #define PMD_TABLE_MASK ((PTRS_PER_PMD-1) * sizeof(pmd_t))
210 * copy one vm_area from one task to the other. Assumes the page tables
211 * already present in the new task to be cleared in the whole range
212 * covered by this vma.
214 * 08Jan98 Merged into one routine from several inline routines to reduce
215 * variable count and make things faster. -jj
217 * dst->page_table_lock is held on entry and exit,
218 * but may be dropped within pmd_alloc() and pte_alloc_map().
220 int copy_page_range(struct mm_struct
*dst
, struct mm_struct
*src
,
221 struct vm_area_struct
*vma
)
223 pgd_t
* src_pgd
, * dst_pgd
;
224 unsigned long address
= vma
->vm_start
;
225 unsigned long end
= vma
->vm_end
;
228 if (is_vm_hugetlb_page(vma
))
229 return copy_hugetlb_page_range(dst
, src
, vma
);
231 cow
= (vma
->vm_flags
& (VM_SHARED
| VM_MAYWRITE
)) == VM_MAYWRITE
;
232 src_pgd
= pgd_offset(src
, address
)-1;
233 dst_pgd
= pgd_offset(dst
, address
)-1;
236 pmd_t
* src_pmd
, * dst_pmd
;
238 src_pgd
++; dst_pgd
++;
242 if (pgd_none(*src_pgd
))
243 goto skip_copy_pmd_range
;
244 if (unlikely(pgd_bad(*src_pgd
))) {
247 skip_copy_pmd_range
: address
= (address
+ PGDIR_SIZE
) & PGDIR_MASK
;
248 if (!address
|| (address
>= end
))
253 src_pmd
= pmd_offset(src_pgd
, address
);
254 dst_pmd
= pmd_alloc(dst
, dst_pgd
, address
);
259 pte_t
* src_pte
, * dst_pte
;
263 if (pmd_none(*src_pmd
))
264 goto skip_copy_pte_range
;
265 if (unlikely(pmd_bad(*src_pmd
))) {
269 address
= (address
+ PMD_SIZE
) & PMD_MASK
;
272 goto cont_copy_pmd_range
;
275 dst_pte
= pte_alloc_map(dst
, dst_pmd
, address
);
278 spin_lock(&src
->page_table_lock
);
279 src_pte
= pte_offset_map_nested(src_pmd
, address
);
281 pte_t pte
= *src_pte
;
288 goto cont_copy_pte_range_noset
;
289 /* pte contains position in swap, so copy. */
290 if (!pte_present(pte
)) {
292 swap_duplicate(pte_to_swp_entry(pte
));
293 set_pte(dst_pte
, pte
);
294 goto cont_copy_pte_range_noset
;
297 /* the pte points outside of valid memory, the
298 * mapping is assumed to be good, meaningful
299 * and not mapped via rmap - duplicate the
304 page
= pfn_to_page(pfn
);
306 if (!page
|| PageReserved(page
)) {
307 set_pte(dst_pte
, pte
);
308 goto cont_copy_pte_range_noset
;
312 * If it's a COW mapping, write protect it both
313 * in the parent and the child
316 ptep_set_wrprotect(src_pte
);
321 * If it's a shared mapping, mark it clean in
324 if (vma
->vm_flags
& VM_SHARED
)
325 pte
= pte_mkclean(pte
);
326 pte
= pte_mkold(pte
);
329 set_pte(dst_pte
, pte
);
331 cont_copy_pte_range_noset
:
332 address
+= PAGE_SIZE
;
333 if (address
>= end
) {
334 pte_unmap_nested(src_pte
);
340 } while ((unsigned long)src_pte
& PTE_TABLE_MASK
);
341 pte_unmap_nested(src_pte
-1);
342 pte_unmap(dst_pte
-1);
343 spin_unlock(&src
->page_table_lock
);
344 cond_resched_lock(&dst
->page_table_lock
);
348 } while ((unsigned long)src_pmd
& PMD_TABLE_MASK
);
351 spin_unlock(&src
->page_table_lock
);
358 static void zap_pte_range(struct mmu_gather
*tlb
,
359 pmd_t
*pmd
, unsigned long address
,
360 unsigned long size
, struct zap_details
*details
)
362 unsigned long offset
;
367 if (unlikely(pmd_bad(*pmd
))) {
372 ptep
= pte_offset_map(pmd
, address
);
373 offset
= address
& ~PMD_MASK
;
374 if (offset
+ size
> PMD_SIZE
)
375 size
= PMD_SIZE
- offset
;
377 if (details
&& !details
->check_mapping
&& !details
->nonlinear_vma
)
379 for (offset
=0; offset
< size
; ptep
++, offset
+= PAGE_SIZE
) {
383 if (pte_present(pte
)) {
384 struct page
*page
= NULL
;
385 unsigned long pfn
= pte_pfn(pte
);
386 if (pfn_valid(pfn
)) {
387 page
= pfn_to_page(pfn
);
388 if (PageReserved(page
))
391 if (unlikely(details
) && page
) {
393 * unmap_shared_mapping_pages() wants to
394 * invalidate cache without truncating:
395 * unmap shared but keep private pages.
397 if (details
->check_mapping
&&
398 details
->check_mapping
!= page
->mapping
)
401 * Each page->index must be checked when
402 * invalidating or truncating nonlinear.
404 if (details
->nonlinear_vma
&&
405 (page
->index
< details
->first_index
||
406 page
->index
> details
->last_index
))
409 pte
= ptep_get_and_clear(ptep
);
410 tlb_remove_tlb_entry(tlb
, ptep
, address
+offset
);
413 if (unlikely(details
) && details
->nonlinear_vma
414 && linear_page_index(details
->nonlinear_vma
,
415 address
+offset
) != page
->index
)
416 set_pte(ptep
, pgoff_to_pte(page
->index
));
418 set_page_dirty(page
);
419 if (pte_young(pte
) && !PageAnon(page
))
420 mark_page_accessed(page
);
422 page_remove_rmap(page
);
423 tlb_remove_page(tlb
, page
);
427 * If details->check_mapping, we leave swap entries;
428 * if details->nonlinear_vma, we leave file entries.
430 if (unlikely(details
))
433 free_swap_and_cache(pte_to_swp_entry(pte
));
439 static void zap_pmd_range(struct mmu_gather
*tlb
,
440 pgd_t
* dir
, unsigned long address
,
441 unsigned long size
, struct zap_details
*details
)
448 if (unlikely(pgd_bad(*dir
))) {
453 pmd
= pmd_offset(dir
, address
);
454 end
= address
+ size
;
455 if (end
> ((address
+ PGDIR_SIZE
) & PGDIR_MASK
))
456 end
= ((address
+ PGDIR_SIZE
) & PGDIR_MASK
);
458 zap_pte_range(tlb
, pmd
, address
, end
- address
, details
);
459 address
= (address
+ PMD_SIZE
) & PMD_MASK
;
461 } while (address
&& (address
< end
));
464 static void unmap_page_range(struct mmu_gather
*tlb
,
465 struct vm_area_struct
*vma
, unsigned long address
,
466 unsigned long end
, struct zap_details
*details
)
470 BUG_ON(address
>= end
);
471 dir
= pgd_offset(vma
->vm_mm
, address
);
472 tlb_start_vma(tlb
, vma
);
474 zap_pmd_range(tlb
, dir
, address
, end
- address
, details
);
475 address
= (address
+ PGDIR_SIZE
) & PGDIR_MASK
;
477 } while (address
&& (address
< end
));
478 tlb_end_vma(tlb
, vma
);
481 /* Dispose of an entire struct mmu_gather per rescheduling point */
482 #if defined(CONFIG_SMP) && defined(CONFIG_PREEMPT)
483 #define ZAP_BLOCK_SIZE (FREE_PTE_NR * PAGE_SIZE)
486 /* For UP, 256 pages at a time gives nice low latency */
487 #if !defined(CONFIG_SMP) && defined(CONFIG_PREEMPT)
488 #define ZAP_BLOCK_SIZE (256 * PAGE_SIZE)
491 /* No preempt: go for improved straight-line efficiency */
492 #if !defined(CONFIG_PREEMPT)
493 #define ZAP_BLOCK_SIZE (1024 * PAGE_SIZE)
497 * unmap_vmas - unmap a range of memory covered by a list of vma's
498 * @tlbp: address of the caller's struct mmu_gather
499 * @mm: the controlling mm_struct
500 * @vma: the starting vma
501 * @start_addr: virtual address at which to start unmapping
502 * @end_addr: virtual address at which to end unmapping
503 * @nr_accounted: Place number of unmapped pages in vm-accountable vma's here
504 * @details: details of nonlinear truncation or shared cache invalidation
506 * Returns the number of vma's which were covered by the unmapping.
508 * Unmap all pages in the vma list. Called under page_table_lock.
510 * We aim to not hold page_table_lock for too long (for scheduling latency
511 * reasons). So zap pages in ZAP_BLOCK_SIZE bytecounts. This means we need to
512 * return the ending mmu_gather to the caller.
514 * Only addresses between `start' and `end' will be unmapped.
516 * The VMA list must be sorted in ascending virtual address order.
518 * unmap_vmas() assumes that the caller will flush the whole unmapped address
519 * range after unmap_vmas() returns. So the only responsibility here is to
520 * ensure that any thus-far unmapped pages are flushed before unmap_vmas()
521 * drops the lock and schedules.
523 int unmap_vmas(struct mmu_gather
**tlbp
, struct mm_struct
*mm
,
524 struct vm_area_struct
*vma
, unsigned long start_addr
,
525 unsigned long end_addr
, unsigned long *nr_accounted
,
526 struct zap_details
*details
)
528 unsigned long zap_bytes
= ZAP_BLOCK_SIZE
;
529 unsigned long tlb_start
= 0; /* For tlb_finish_mmu */
530 int tlb_start_valid
= 0;
532 int atomic
= details
&& details
->atomic
;
534 for ( ; vma
&& vma
->vm_start
< end_addr
; vma
= vma
->vm_next
) {
538 start
= max(vma
->vm_start
, start_addr
);
539 if (start
>= vma
->vm_end
)
541 end
= min(vma
->vm_end
, end_addr
);
542 if (end
<= vma
->vm_start
)
545 if (vma
->vm_flags
& VM_ACCOUNT
)
546 *nr_accounted
+= (end
- start
) >> PAGE_SHIFT
;
549 while (start
!= end
) {
552 if (!tlb_start_valid
) {
557 if (is_vm_hugetlb_page(vma
)) {
559 unmap_hugepage_range(vma
, start
, end
);
561 block
= min(zap_bytes
, end
- start
);
562 unmap_page_range(*tlbp
, vma
, start
,
563 start
+ block
, details
);
568 if ((long)zap_bytes
> 0)
570 if (!atomic
&& need_resched()) {
571 int fullmm
= tlb_is_full_mm(*tlbp
);
572 tlb_finish_mmu(*tlbp
, tlb_start
, start
);
573 cond_resched_lock(&mm
->page_table_lock
);
574 *tlbp
= tlb_gather_mmu(mm
, fullmm
);
577 zap_bytes
= ZAP_BLOCK_SIZE
;
584 * zap_page_range - remove user pages in a given range
585 * @vma: vm_area_struct holding the applicable pages
586 * @address: starting address of pages to zap
587 * @size: number of bytes to zap
588 * @details: details of nonlinear truncation or shared cache invalidation
590 void zap_page_range(struct vm_area_struct
*vma
, unsigned long address
,
591 unsigned long size
, struct zap_details
*details
)
593 struct mm_struct
*mm
= vma
->vm_mm
;
594 struct mmu_gather
*tlb
;
595 unsigned long end
= address
+ size
;
596 unsigned long nr_accounted
= 0;
598 if (is_vm_hugetlb_page(vma
)) {
599 zap_hugepage_range(vma
, address
, size
);
604 spin_lock(&mm
->page_table_lock
);
605 tlb
= tlb_gather_mmu(mm
, 0);
606 unmap_vmas(&tlb
, mm
, vma
, address
, end
, &nr_accounted
, details
);
607 tlb_finish_mmu(tlb
, address
, end
);
608 spin_unlock(&mm
->page_table_lock
);
612 * Do a quick page-table lookup for a single page.
613 * mm->page_table_lock must be held.
616 follow_page(struct mm_struct
*mm
, unsigned long address
, int write
)
624 page
= follow_huge_addr(mm
, address
, write
);
628 pgd
= pgd_offset(mm
, address
);
629 if (pgd_none(*pgd
) || unlikely(pgd_bad(*pgd
)))
632 pmd
= pmd_offset(pgd
, address
);
636 return follow_huge_pmd(mm
, address
, pmd
, write
);
637 if (unlikely(pmd_bad(*pmd
)))
640 ptep
= pte_offset_map(pmd
, address
);
646 if (pte_present(pte
)) {
647 if (write
&& !pte_write(pte
))
650 if (pfn_valid(pfn
)) {
651 page
= pfn_to_page(pfn
);
652 if (write
&& !pte_dirty(pte
) && !PageDirty(page
))
653 set_page_dirty(page
);
654 mark_page_accessed(page
);
664 * Given a physical address, is there a useful struct page pointing to
665 * it? This may become more complex in the future if we start dealing
666 * with IO-aperture pages for direct-IO.
669 static inline struct page
*get_page_map(struct page
*page
)
671 if (!pfn_valid(page_to_pfn(page
)))
678 untouched_anonymous_page(struct mm_struct
* mm
, struct vm_area_struct
*vma
,
679 unsigned long address
)
684 /* Check if the vma is for an anonymous mapping. */
685 if (vma
->vm_ops
&& vma
->vm_ops
->nopage
)
688 /* Check if page directory entry exists. */
689 pgd
= pgd_offset(mm
, address
);
690 if (pgd_none(*pgd
) || unlikely(pgd_bad(*pgd
)))
693 /* Check if page middle directory entry exists. */
694 pmd
= pmd_offset(pgd
, address
);
695 if (pmd_none(*pmd
) || unlikely(pmd_bad(*pmd
)))
698 /* There is a pte slot for 'address' in 'mm'. */
703 int get_user_pages(struct task_struct
*tsk
, struct mm_struct
*mm
,
704 unsigned long start
, int len
, int write
, int force
,
705 struct page
**pages
, struct vm_area_struct
**vmas
)
711 * Require read or write permissions.
712 * If 'force' is set, we only require the "MAY" flags.
714 flags
= write
? (VM_WRITE
| VM_MAYWRITE
) : (VM_READ
| VM_MAYREAD
);
715 flags
&= force
? (VM_MAYREAD
| VM_MAYWRITE
) : (VM_READ
| VM_WRITE
);
719 struct vm_area_struct
* vma
;
721 vma
= find_extend_vma(mm
, start
);
722 if (!vma
&& in_gate_area(tsk
, start
)) {
723 unsigned long pg
= start
& PAGE_MASK
;
724 struct vm_area_struct
*gate_vma
= get_gate_vma(tsk
);
728 if (write
) /* user gate pages are read-only */
729 return i
? : -EFAULT
;
730 pgd
= pgd_offset_gate(mm
, pg
);
732 return i
? : -EFAULT
;
733 pmd
= pmd_offset(pgd
, pg
);
735 return i
? : -EFAULT
;
736 pte
= pte_offset_map(pmd
, pg
);
738 return i
? : -EFAULT
;
739 if (!pte_present(*pte
)) {
741 return i
? : -EFAULT
;
744 pages
[i
] = pte_page(*pte
);
756 if (!vma
|| (pages
&& (vma
->vm_flags
& VM_IO
))
757 || !(flags
& vma
->vm_flags
))
758 return i
? : -EFAULT
;
760 if (is_vm_hugetlb_page(vma
)) {
761 i
= follow_hugetlb_page(mm
, vma
, pages
, vmas
,
765 spin_lock(&mm
->page_table_lock
);
768 int lookup_write
= write
;
769 while (!(map
= follow_page(mm
, start
, lookup_write
))) {
771 * Shortcut for anonymous pages. We don't want
772 * to force the creation of pages tables for
773 * insanly big anonymously mapped areas that
774 * nobody touched so far. This is important
775 * for doing a core dump for these mappings.
778 untouched_anonymous_page(mm
,vma
,start
)) {
779 map
= ZERO_PAGE(start
);
782 spin_unlock(&mm
->page_table_lock
);
783 switch (handle_mm_fault(mm
,vma
,start
,write
)) {
790 case VM_FAULT_SIGBUS
:
791 return i
? i
: -EFAULT
;
793 return i
? i
: -ENOMEM
;
798 * Now that we have performed a write fault
799 * and surely no longer have a shared page we
800 * shouldn't write, we shouldn't ignore an
801 * unwritable page in the page table if
802 * we are forcing write access.
804 lookup_write
= write
&& !force
;
805 spin_lock(&mm
->page_table_lock
);
808 pages
[i
] = get_page_map(map
);
810 spin_unlock(&mm
->page_table_lock
);
812 page_cache_release(pages
[i
]);
816 flush_dcache_page(pages
[i
]);
817 if (!PageReserved(pages
[i
]))
818 page_cache_get(pages
[i
]);
825 } while(len
&& start
< vma
->vm_end
);
826 spin_unlock(&mm
->page_table_lock
);
832 EXPORT_SYMBOL(get_user_pages
);
834 static void zeromap_pte_range(pte_t
* pte
, unsigned long address
,
835 unsigned long size
, pgprot_t prot
)
839 address
&= ~PMD_MASK
;
840 end
= address
+ size
;
844 pte_t zero_pte
= pte_wrprotect(mk_pte(ZERO_PAGE(address
), prot
));
845 BUG_ON(!pte_none(*pte
));
846 set_pte(pte
, zero_pte
);
847 address
+= PAGE_SIZE
;
849 } while (address
&& (address
< end
));
852 static inline int zeromap_pmd_range(struct mm_struct
*mm
, pmd_t
* pmd
, unsigned long address
,
853 unsigned long size
, pgprot_t prot
)
855 unsigned long base
, end
;
857 base
= address
& PGDIR_MASK
;
858 address
&= ~PGDIR_MASK
;
859 end
= address
+ size
;
860 if (end
> PGDIR_SIZE
)
863 pte_t
* pte
= pte_alloc_map(mm
, pmd
, base
+ address
);
866 zeromap_pte_range(pte
, base
+ address
, end
- address
, prot
);
868 address
= (address
+ PMD_SIZE
) & PMD_MASK
;
870 } while (address
&& (address
< end
));
874 int zeromap_page_range(struct vm_area_struct
*vma
, unsigned long address
, unsigned long size
, pgprot_t prot
)
878 unsigned long beg
= address
;
879 unsigned long end
= address
+ size
;
880 struct mm_struct
*mm
= vma
->vm_mm
;
882 dir
= pgd_offset(mm
, address
);
883 flush_cache_range(vma
, beg
, end
);
887 spin_lock(&mm
->page_table_lock
);
889 pmd_t
*pmd
= pmd_alloc(mm
, dir
, address
);
893 error
= zeromap_pmd_range(mm
, pmd
, address
, end
- address
, prot
);
896 address
= (address
+ PGDIR_SIZE
) & PGDIR_MASK
;
898 } while (address
&& (address
< end
));
900 * Why flush? zeromap_pte_range has a BUG_ON for !pte_none()
902 flush_tlb_range(vma
, beg
, end
);
903 spin_unlock(&mm
->page_table_lock
);
908 * maps a range of physical memory into the requested pages. the old
909 * mappings are removed. any references to nonexistent pages results
910 * in null mappings (currently treated as "copy-on-access")
912 static inline void remap_pte_range(pte_t
* pte
, unsigned long address
, unsigned long size
,
913 unsigned long phys_addr
, pgprot_t prot
)
918 address
&= ~PMD_MASK
;
919 end
= address
+ size
;
922 pfn
= phys_addr
>> PAGE_SHIFT
;
924 BUG_ON(!pte_none(*pte
));
925 if (!pfn_valid(pfn
) || PageReserved(pfn_to_page(pfn
)))
926 set_pte(pte
, pfn_pte(pfn
, prot
));
927 address
+= PAGE_SIZE
;
930 } while (address
&& (address
< end
));
933 static inline int remap_pmd_range(struct mm_struct
*mm
, pmd_t
* pmd
, unsigned long address
, unsigned long size
,
934 unsigned long phys_addr
, pgprot_t prot
)
936 unsigned long base
, end
;
938 base
= address
& PGDIR_MASK
;
939 address
&= ~PGDIR_MASK
;
940 end
= address
+ size
;
941 if (end
> PGDIR_SIZE
)
943 phys_addr
-= address
;
945 pte_t
* pte
= pte_alloc_map(mm
, pmd
, base
+ address
);
948 remap_pte_range(pte
, base
+ address
, end
- address
, address
+ phys_addr
, prot
);
950 address
= (address
+ PMD_SIZE
) & PMD_MASK
;
952 } while (address
&& (address
< end
));
956 /* Note: this is only safe if the mm semaphore is held when called. */
957 int remap_page_range(struct vm_area_struct
*vma
, unsigned long from
, unsigned long phys_addr
, unsigned long size
, pgprot_t prot
)
961 unsigned long beg
= from
;
962 unsigned long end
= from
+ size
;
963 struct mm_struct
*mm
= vma
->vm_mm
;
966 dir
= pgd_offset(mm
, from
);
967 flush_cache_range(vma
, beg
, end
);
971 spin_lock(&mm
->page_table_lock
);
973 pmd_t
*pmd
= pmd_alloc(mm
, dir
, from
);
977 error
= remap_pmd_range(mm
, pmd
, from
, end
- from
, phys_addr
+ from
, prot
);
980 from
= (from
+ PGDIR_SIZE
) & PGDIR_MASK
;
982 } while (from
&& (from
< end
));
984 * Why flush? remap_pte_range has a BUG_ON for !pte_none()
986 flush_tlb_range(vma
, beg
, end
);
987 spin_unlock(&mm
->page_table_lock
);
991 EXPORT_SYMBOL(remap_page_range
);
994 * Do pte_mkwrite, but only if the vma says VM_WRITE. We do this when
995 * servicing faults for write access. In the normal case, do always want
996 * pte_mkwrite. But get_user_pages can cause write faults for mappings
997 * that do not have writing enabled, when used by access_process_vm.
999 static inline pte_t
maybe_mkwrite(pte_t pte
, struct vm_area_struct
*vma
)
1001 if (likely(vma
->vm_flags
& VM_WRITE
))
1002 pte
= pte_mkwrite(pte
);
1007 * We hold the mm semaphore for reading and vma->vm_mm->page_table_lock
1009 static inline void break_cow(struct vm_area_struct
* vma
, struct page
* new_page
, unsigned long address
,
1014 flush_cache_page(vma
, address
);
1015 entry
= maybe_mkwrite(pte_mkdirty(mk_pte(new_page
, vma
->vm_page_prot
)),
1017 ptep_establish(vma
, address
, page_table
, entry
);
1018 update_mmu_cache(vma
, address
, entry
);
1022 * This routine handles present pages, when users try to write
1023 * to a shared page. It is done by copying the page to a new address
1024 * and decrementing the shared-page counter for the old page.
1026 * Goto-purists beware: the only reason for goto's here is that it results
1027 * in better assembly code.. The "default" path will see no jumps at all.
1029 * Note that this routine assumes that the protection checks have been
1030 * done by the caller (the low-level page fault routine in most cases).
1031 * Thus we can safely just mark it writable once we've done any necessary
1034 * We also mark the page dirty at this point even though the page will
1035 * change only once the write actually happens. This avoids a few races,
1036 * and potentially makes it more efficient.
1038 * We hold the mm semaphore and the page_table_lock on entry and exit
1039 * with the page_table_lock released.
1041 static int do_wp_page(struct mm_struct
*mm
, struct vm_area_struct
* vma
,
1042 unsigned long address
, pte_t
*page_table
, pmd_t
*pmd
, pte_t pte
)
1044 struct page
*old_page
, *new_page
;
1045 unsigned long pfn
= pte_pfn(pte
);
1048 if (unlikely(!pfn_valid(pfn
))) {
1050 * This should really halt the system so it can be debugged or
1051 * at least the kernel stops what it's doing before it corrupts
1052 * data, but for the moment just pretend this is OOM.
1054 pte_unmap(page_table
);
1055 printk(KERN_ERR
"do_wp_page: bogus page at address %08lx\n",
1057 spin_unlock(&mm
->page_table_lock
);
1058 return VM_FAULT_OOM
;
1060 old_page
= pfn_to_page(pfn
);
1062 if (!TestSetPageLocked(old_page
)) {
1063 int reuse
= can_share_swap_page(old_page
);
1064 unlock_page(old_page
);
1066 flush_cache_page(vma
, address
);
1067 entry
= maybe_mkwrite(pte_mkyoung(pte_mkdirty(pte
)),
1069 ptep_set_access_flags(vma
, address
, page_table
, entry
, 1);
1070 update_mmu_cache(vma
, address
, entry
);
1071 pte_unmap(page_table
);
1072 spin_unlock(&mm
->page_table_lock
);
1073 return VM_FAULT_MINOR
;
1076 pte_unmap(page_table
);
1079 * Ok, we need to copy. Oh, well..
1081 if (!PageReserved(old_page
))
1082 page_cache_get(old_page
);
1083 spin_unlock(&mm
->page_table_lock
);
1085 if (unlikely(anon_vma_prepare(vma
)))
1087 new_page
= alloc_page_vma(GFP_HIGHUSER
, vma
, address
);
1090 copy_cow_page(old_page
,new_page
,address
);
1093 * Re-check the pte - we dropped the lock
1095 spin_lock(&mm
->page_table_lock
);
1096 page_table
= pte_offset_map(pmd
, address
);
1097 if (likely(pte_same(*page_table
, pte
))) {
1098 if (PageReserved(old_page
))
1101 page_remove_rmap(old_page
);
1102 break_cow(vma
, new_page
, address
, page_table
);
1103 lru_cache_add_active(new_page
);
1104 page_add_anon_rmap(new_page
, vma
, address
);
1106 /* Free the old page.. */
1107 new_page
= old_page
;
1109 pte_unmap(page_table
);
1110 page_cache_release(new_page
);
1111 page_cache_release(old_page
);
1112 spin_unlock(&mm
->page_table_lock
);
1113 return VM_FAULT_MINOR
;
1116 page_cache_release(old_page
);
1117 return VM_FAULT_OOM
;
1121 * Helper function for unmap_mapping_range().
1123 static inline void unmap_mapping_range_list(struct prio_tree_root
*root
,
1124 struct zap_details
*details
)
1126 struct vm_area_struct
*vma
;
1127 struct prio_tree_iter iter
;
1128 pgoff_t vba
, vea
, zba
, zea
;
1130 vma_prio_tree_foreach(vma
, &iter
, root
,
1131 details
->first_index
, details
->last_index
) {
1132 vba
= vma
->vm_pgoff
;
1133 vea
= vba
+ ((vma
->vm_end
- vma
->vm_start
) >> PAGE_SHIFT
) - 1;
1134 /* Assume for now that PAGE_CACHE_SHIFT == PAGE_SHIFT */
1135 zba
= details
->first_index
;
1138 zea
= details
->last_index
;
1142 ((zba
- vba
) << PAGE_SHIFT
) + vma
->vm_start
,
1143 (zea
- zba
+ 1) << PAGE_SHIFT
, details
);
1148 * unmap_mapping_range - unmap the portion of all mmaps
1149 * in the specified address_space corresponding to the specified
1150 * page range in the underlying file.
1151 * @address_space: the address space containing mmaps to be unmapped.
1152 * @holebegin: byte in first page to unmap, relative to the start of
1153 * the underlying file. This will be rounded down to a PAGE_SIZE
1154 * boundary. Note that this is different from vmtruncate(), which
1155 * must keep the partial page. In contrast, we must get rid of
1157 * @holelen: size of prospective hole in bytes. This will be rounded
1158 * up to a PAGE_SIZE boundary. A holelen of zero truncates to the
1160 * @even_cows: 1 when truncating a file, unmap even private COWed pages;
1161 * but 0 when invalidating pagecache, don't throw away private data.
1163 void unmap_mapping_range(struct address_space
*mapping
,
1164 loff_t
const holebegin
, loff_t
const holelen
, int even_cows
)
1166 struct zap_details details
;
1167 pgoff_t hba
= holebegin
>> PAGE_SHIFT
;
1168 pgoff_t hlen
= (holelen
+ PAGE_SIZE
- 1) >> PAGE_SHIFT
;
1170 /* Check for overflow. */
1171 if (sizeof(holelen
) > sizeof(hlen
)) {
1173 (holebegin
+ holelen
+ PAGE_SIZE
- 1) >> PAGE_SHIFT
;
1174 if (holeend
& ~(long long)ULONG_MAX
)
1175 hlen
= ULONG_MAX
- hba
+ 1;
1178 details
.check_mapping
= even_cows
? NULL
: mapping
;
1179 details
.nonlinear_vma
= NULL
;
1180 details
.first_index
= hba
;
1181 details
.last_index
= hba
+ hlen
- 1;
1182 details
.atomic
= 1; /* A spinlock is held */
1183 if (details
.last_index
< details
.first_index
)
1184 details
.last_index
= ULONG_MAX
;
1186 spin_lock(&mapping
->i_mmap_lock
);
1187 /* Protect against page fault */
1188 atomic_inc(&mapping
->truncate_count
);
1190 if (unlikely(!prio_tree_empty(&mapping
->i_mmap
)))
1191 unmap_mapping_range_list(&mapping
->i_mmap
, &details
);
1194 * In nonlinear VMAs there is no correspondence between virtual address
1195 * offset and file offset. So we must perform an exhaustive search
1196 * across *all* the pages in each nonlinear VMA, not just the pages
1197 * whose virtual address lies outside the file truncation point.
1199 if (unlikely(!list_empty(&mapping
->i_mmap_nonlinear
))) {
1200 struct vm_area_struct
*vma
;
1201 list_for_each_entry(vma
, &mapping
->i_mmap_nonlinear
,
1202 shared
.vm_set
.list
) {
1203 details
.nonlinear_vma
= vma
;
1204 zap_page_range(vma
, vma
->vm_start
,
1205 vma
->vm_end
- vma
->vm_start
, &details
);
1208 spin_unlock(&mapping
->i_mmap_lock
);
1210 EXPORT_SYMBOL(unmap_mapping_range
);
1213 * Handle all mappings that got truncated by a "truncate()"
1216 * NOTE! We have to be ready to update the memory sharing
1217 * between the file and the memory map for a potential last
1218 * incomplete page. Ugly, but necessary.
1220 int vmtruncate(struct inode
* inode
, loff_t offset
)
1222 struct address_space
*mapping
= inode
->i_mapping
;
1223 unsigned long limit
;
1225 if (inode
->i_size
< offset
)
1228 * truncation of in-use swapfiles is disallowed - it would cause
1229 * subsequent swapout to scribble on the now-freed blocks.
1231 if (IS_SWAPFILE(inode
))
1233 i_size_write(inode
, offset
);
1234 unmap_mapping_range(mapping
, offset
+ PAGE_SIZE
- 1, 0, 1);
1235 truncate_inode_pages(mapping
, offset
);
1239 limit
= current
->rlim
[RLIMIT_FSIZE
].rlim_cur
;
1240 if (limit
!= RLIM_INFINITY
&& offset
> limit
)
1242 if (offset
> inode
->i_sb
->s_maxbytes
)
1244 i_size_write(inode
, offset
);
1247 if (inode
->i_op
&& inode
->i_op
->truncate
)
1248 inode
->i_op
->truncate(inode
);
1251 send_sig(SIGXFSZ
, current
, 0);
1258 EXPORT_SYMBOL(vmtruncate
);
1261 * Primitive swap readahead code. We simply read an aligned block of
1262 * (1 << page_cluster) entries in the swap area. This method is chosen
1263 * because it doesn't cost us any seek time. We also make sure to queue
1264 * the 'original' request together with the readahead ones...
1266 * This has been extended to use the NUMA policies from the mm triggering
1269 * Caller must hold down_read on the vma->vm_mm if vma is not NULL.
1271 void swapin_readahead(swp_entry_t entry
, unsigned long addr
,struct vm_area_struct
*vma
)
1274 struct vm_area_struct
*next_vma
= vma
? vma
->vm_next
: NULL
;
1277 struct page
*new_page
;
1278 unsigned long offset
;
1281 * Get the number of handles we should do readahead io to.
1283 num
= valid_swaphandles(entry
, &offset
);
1284 for (i
= 0; i
< num
; offset
++, i
++) {
1285 /* Ok, do the async read-ahead now */
1286 new_page
= read_swap_cache_async(swp_entry(swp_type(entry
),
1287 offset
), vma
, addr
);
1290 page_cache_release(new_page
);
1293 * Find the next applicable VMA for the NUMA policy.
1299 if (addr
>= vma
->vm_end
) {
1301 next_vma
= vma
? vma
->vm_next
: NULL
;
1303 if (vma
&& addr
< vma
->vm_start
)
1306 if (next_vma
&& addr
>= next_vma
->vm_start
) {
1308 next_vma
= vma
->vm_next
;
1313 lru_add_drain(); /* Push any new pages onto the LRU now */
1317 * We hold the mm semaphore and the page_table_lock on entry and
1318 * should release the pagetable lock on exit..
1320 static int do_swap_page(struct mm_struct
* mm
,
1321 struct vm_area_struct
* vma
, unsigned long address
,
1322 pte_t
*page_table
, pmd_t
*pmd
, pte_t orig_pte
, int write_access
)
1325 swp_entry_t entry
= pte_to_swp_entry(orig_pte
);
1327 int ret
= VM_FAULT_MINOR
;
1329 pte_unmap(page_table
);
1330 spin_unlock(&mm
->page_table_lock
);
1331 page
= lookup_swap_cache(entry
);
1333 swapin_readahead(entry
, address
, vma
);
1334 page
= read_swap_cache_async(entry
, vma
, address
);
1337 * Back out if somebody else faulted in this pte while
1338 * we released the page table lock.
1340 spin_lock(&mm
->page_table_lock
);
1341 page_table
= pte_offset_map(pmd
, address
);
1342 if (likely(pte_same(*page_table
, orig_pte
)))
1345 ret
= VM_FAULT_MINOR
;
1346 pte_unmap(page_table
);
1347 spin_unlock(&mm
->page_table_lock
);
1351 /* Had to read the page from swap area: Major fault */
1352 ret
= VM_FAULT_MAJOR
;
1353 inc_page_state(pgmajfault
);
1357 mark_page_accessed(page
);
1361 * Back out if somebody else faulted in this pte while we
1362 * released the page table lock.
1364 spin_lock(&mm
->page_table_lock
);
1365 page_table
= pte_offset_map(pmd
, address
);
1366 if (unlikely(!pte_same(*page_table
, orig_pte
))) {
1367 pte_unmap(page_table
);
1368 spin_unlock(&mm
->page_table_lock
);
1370 page_cache_release(page
);
1371 ret
= VM_FAULT_MINOR
;
1375 /* The page isn't present yet, go ahead with the fault. */
1379 remove_exclusive_swap_page(page
);
1382 pte
= mk_pte(page
, vma
->vm_page_prot
);
1383 if (write_access
&& can_share_swap_page(page
)) {
1384 pte
= maybe_mkwrite(pte_mkdirty(pte
), vma
);
1389 flush_icache_page(vma
, page
);
1390 set_pte(page_table
, pte
);
1391 page_add_anon_rmap(page
, vma
, address
);
1394 if (do_wp_page(mm
, vma
, address
,
1395 page_table
, pmd
, pte
) == VM_FAULT_OOM
)
1400 /* No need to invalidate - it was non-present before */
1401 update_mmu_cache(vma
, address
, pte
);
1402 pte_unmap(page_table
);
1403 spin_unlock(&mm
->page_table_lock
);
1409 * We are called with the MM semaphore and page_table_lock
1410 * spinlock held to protect against concurrent faults in
1411 * multithreaded programs.
1414 do_anonymous_page(struct mm_struct
*mm
, struct vm_area_struct
*vma
,
1415 pte_t
*page_table
, pmd_t
*pmd
, int write_access
,
1419 struct page
* page
= ZERO_PAGE(addr
);
1421 /* Read-only mapping of ZERO_PAGE. */
1422 entry
= pte_wrprotect(mk_pte(ZERO_PAGE(addr
), vma
->vm_page_prot
));
1424 /* ..except if it's a write access */
1426 /* Allocate our own private page. */
1427 pte_unmap(page_table
);
1428 spin_unlock(&mm
->page_table_lock
);
1430 if (unlikely(anon_vma_prepare(vma
)))
1432 page
= alloc_page_vma(GFP_HIGHUSER
, vma
, addr
);
1435 clear_user_highpage(page
, addr
);
1437 spin_lock(&mm
->page_table_lock
);
1438 page_table
= pte_offset_map(pmd
, addr
);
1440 if (!pte_none(*page_table
)) {
1441 pte_unmap(page_table
);
1442 page_cache_release(page
);
1443 spin_unlock(&mm
->page_table_lock
);
1447 entry
= maybe_mkwrite(pte_mkdirty(mk_pte(page
,
1448 vma
->vm_page_prot
)),
1450 lru_cache_add_active(page
);
1451 mark_page_accessed(page
);
1452 page_add_anon_rmap(page
, vma
, addr
);
1455 set_pte(page_table
, entry
);
1456 pte_unmap(page_table
);
1458 /* No need to invalidate - it was non-present before */
1459 update_mmu_cache(vma
, addr
, entry
);
1460 spin_unlock(&mm
->page_table_lock
);
1462 return VM_FAULT_MINOR
;
1464 return VM_FAULT_OOM
;
1468 * do_no_page() tries to create a new page mapping. It aggressively
1469 * tries to share with existing pages, but makes a separate copy if
1470 * the "write_access" parameter is true in order to avoid the next
1473 * As this is called only for pages that do not currently exist, we
1474 * do not need to flush old virtual caches or the TLB.
1476 * This is called with the MM semaphore held and the page table
1477 * spinlock held. Exit with the spinlock released.
1480 do_no_page(struct mm_struct
*mm
, struct vm_area_struct
*vma
,
1481 unsigned long address
, int write_access
, pte_t
*page_table
, pmd_t
*pmd
)
1483 struct page
* new_page
;
1484 struct address_space
*mapping
= NULL
;
1487 int ret
= VM_FAULT_MINOR
;
1490 if (!vma
->vm_ops
|| !vma
->vm_ops
->nopage
)
1491 return do_anonymous_page(mm
, vma
, page_table
,
1492 pmd
, write_access
, address
);
1493 pte_unmap(page_table
);
1494 spin_unlock(&mm
->page_table_lock
);
1497 mapping
= vma
->vm_file
->f_mapping
;
1498 sequence
= atomic_read(&mapping
->truncate_count
);
1500 smp_rmb(); /* Prevent CPU from reordering lock-free ->nopage() */
1502 new_page
= vma
->vm_ops
->nopage(vma
, address
& PAGE_MASK
, &ret
);
1504 /* no page was available -- either SIGBUS or OOM */
1505 if (new_page
== NOPAGE_SIGBUS
)
1506 return VM_FAULT_SIGBUS
;
1507 if (new_page
== NOPAGE_OOM
)
1508 return VM_FAULT_OOM
;
1511 * Should we do an early C-O-W break?
1513 if (write_access
&& !(vma
->vm_flags
& VM_SHARED
)) {
1516 if (unlikely(anon_vma_prepare(vma
)))
1518 page
= alloc_page_vma(GFP_HIGHUSER
, vma
, address
);
1521 copy_user_highpage(page
, new_page
, address
);
1522 page_cache_release(new_page
);
1527 spin_lock(&mm
->page_table_lock
);
1529 * For a file-backed vma, someone could have truncated or otherwise
1530 * invalidated this page. If unmap_mapping_range got called,
1531 * retry getting the page.
1534 (unlikely(sequence
!= atomic_read(&mapping
->truncate_count
)))) {
1535 sequence
= atomic_read(&mapping
->truncate_count
);
1536 spin_unlock(&mm
->page_table_lock
);
1537 page_cache_release(new_page
);
1540 page_table
= pte_offset_map(pmd
, address
);
1543 * This silly early PAGE_DIRTY setting removes a race
1544 * due to the bad i386 page protection. But it's valid
1545 * for other architectures too.
1547 * Note that if write_access is true, we either now have
1548 * an exclusive copy of the page, or this is a shared mapping,
1549 * so we can make it writable and dirty to avoid having to
1550 * handle that later.
1552 /* Only go through if we didn't race with anybody else... */
1553 if (pte_none(*page_table
)) {
1554 if (!PageReserved(new_page
))
1556 flush_icache_page(vma
, new_page
);
1557 entry
= mk_pte(new_page
, vma
->vm_page_prot
);
1559 entry
= maybe_mkwrite(pte_mkdirty(entry
), vma
);
1560 set_pte(page_table
, entry
);
1562 lru_cache_add_active(new_page
);
1563 page_add_anon_rmap(new_page
, vma
, address
);
1565 page_add_file_rmap(new_page
);
1566 pte_unmap(page_table
);
1568 /* One of our sibling threads was faster, back out. */
1569 pte_unmap(page_table
);
1570 page_cache_release(new_page
);
1571 spin_unlock(&mm
->page_table_lock
);
1575 /* no need to invalidate: a not-present page shouldn't be cached */
1576 update_mmu_cache(vma
, address
, entry
);
1577 spin_unlock(&mm
->page_table_lock
);
1581 page_cache_release(new_page
);
1587 * Fault of a previously existing named mapping. Repopulate the pte
1588 * from the encoded file_pte if possible. This enables swappable
1591 static int do_file_page(struct mm_struct
* mm
, struct vm_area_struct
* vma
,
1592 unsigned long address
, int write_access
, pte_t
*pte
, pmd_t
*pmd
)
1594 unsigned long pgoff
;
1597 BUG_ON(!vma
->vm_ops
|| !vma
->vm_ops
->nopage
);
1599 * Fall back to the linear mapping if the fs does not support
1602 if (!vma
->vm_ops
|| !vma
->vm_ops
->populate
||
1603 (write_access
&& !(vma
->vm_flags
& VM_SHARED
))) {
1605 return do_no_page(mm
, vma
, address
, write_access
, pte
, pmd
);
1608 pgoff
= pte_to_pgoff(*pte
);
1611 spin_unlock(&mm
->page_table_lock
);
1613 err
= vma
->vm_ops
->populate(vma
, address
& PAGE_MASK
, PAGE_SIZE
, vma
->vm_page_prot
, pgoff
, 0);
1615 return VM_FAULT_OOM
;
1617 return VM_FAULT_SIGBUS
;
1618 return VM_FAULT_MAJOR
;
1622 * These routines also need to handle stuff like marking pages dirty
1623 * and/or accessed for architectures that don't do it in hardware (most
1624 * RISC architectures). The early dirtying is also good on the i386.
1626 * There is also a hook called "update_mmu_cache()" that architectures
1627 * with external mmu caches can use to update those (ie the Sparc or
1628 * PowerPC hashed page tables that act as extended TLBs).
1630 * Note the "page_table_lock". It is to protect against kswapd removing
1631 * pages from under us. Note that kswapd only ever _removes_ pages, never
1632 * adds them. As such, once we have noticed that the page is not present,
1633 * we can drop the lock early.
1635 * The adding of pages is protected by the MM semaphore (which we hold),
1636 * so we don't need to worry about a page being suddenly been added into
1639 * We enter with the pagetable spinlock held, we are supposed to
1640 * release it when done.
1642 static inline int handle_pte_fault(struct mm_struct
*mm
,
1643 struct vm_area_struct
* vma
, unsigned long address
,
1644 int write_access
, pte_t
*pte
, pmd_t
*pmd
)
1649 if (!pte_present(entry
)) {
1651 * If it truly wasn't present, we know that kswapd
1652 * and the PTE updates will not touch it later. So
1655 if (pte_none(entry
))
1656 return do_no_page(mm
, vma
, address
, write_access
, pte
, pmd
);
1657 if (pte_file(entry
))
1658 return do_file_page(mm
, vma
, address
, write_access
, pte
, pmd
);
1659 return do_swap_page(mm
, vma
, address
, pte
, pmd
, entry
, write_access
);
1663 if (!pte_write(entry
))
1664 return do_wp_page(mm
, vma
, address
, pte
, pmd
, entry
);
1666 entry
= pte_mkdirty(entry
);
1668 entry
= pte_mkyoung(entry
);
1669 ptep_set_access_flags(vma
, address
, pte
, entry
, write_access
);
1670 update_mmu_cache(vma
, address
, entry
);
1672 spin_unlock(&mm
->page_table_lock
);
1673 return VM_FAULT_MINOR
;
1677 * By the time we get here, we already hold the mm semaphore
1679 int handle_mm_fault(struct mm_struct
*mm
, struct vm_area_struct
* vma
,
1680 unsigned long address
, int write_access
)
1685 __set_current_state(TASK_RUNNING
);
1686 pgd
= pgd_offset(mm
, address
);
1688 inc_page_state(pgfault
);
1690 if (is_vm_hugetlb_page(vma
))
1691 return VM_FAULT_SIGBUS
; /* mapping truncation does this. */
1694 * We need the page table lock to synchronize with kswapd
1695 * and the SMP-safe atomic PTE updates.
1697 spin_lock(&mm
->page_table_lock
);
1698 pmd
= pmd_alloc(mm
, pgd
, address
);
1701 pte_t
* pte
= pte_alloc_map(mm
, pmd
, address
);
1703 return handle_pte_fault(mm
, vma
, address
, write_access
, pte
, pmd
);
1705 spin_unlock(&mm
->page_table_lock
);
1706 return VM_FAULT_OOM
;
1710 * Allocate page middle directory.
1712 * We've already handled the fast-path in-line, and we own the
1715 * On a two-level page table, this ends up actually being entirely
1718 pmd_t fastcall
*__pmd_alloc(struct mm_struct
*mm
, pgd_t
*pgd
, unsigned long address
)
1722 spin_unlock(&mm
->page_table_lock
);
1723 new = pmd_alloc_one(mm
, address
);
1724 spin_lock(&mm
->page_table_lock
);
1729 * Because we dropped the lock, we should re-check the
1730 * entry, as somebody else could have populated it..
1732 if (pgd_present(*pgd
)) {
1736 pgd_populate(mm
, pgd
, new);
1738 return pmd_offset(pgd
, address
);
1741 int make_pages_present(unsigned long addr
, unsigned long end
)
1743 int ret
, len
, write
;
1744 struct vm_area_struct
* vma
;
1746 vma
= find_vma(current
->mm
, addr
);
1749 write
= (vma
->vm_flags
& VM_WRITE
) != 0;
1752 if (end
> vma
->vm_end
)
1754 len
= (end
+PAGE_SIZE
-1)/PAGE_SIZE
-addr
/PAGE_SIZE
;
1755 ret
= get_user_pages(current
, current
->mm
, addr
,
1756 len
, write
, 0, NULL
, NULL
);
1759 return ret
== len
? 0 : -1;
1763 * Map a vmalloc()-space virtual address to the physical page.
1765 struct page
* vmalloc_to_page(void * vmalloc_addr
)
1767 unsigned long addr
= (unsigned long) vmalloc_addr
;
1768 struct page
*page
= NULL
;
1769 pgd_t
*pgd
= pgd_offset_k(addr
);
1773 if (!pgd_none(*pgd
)) {
1774 pmd
= pmd_offset(pgd
, addr
);
1775 if (!pmd_none(*pmd
)) {
1777 ptep
= pte_offset_map(pmd
, addr
);
1779 if (pte_present(pte
))
1780 page
= pte_page(pte
);
1788 EXPORT_SYMBOL(vmalloc_to_page
);
1790 #if !defined(CONFIG_ARCH_GATE_AREA)
1792 #if defined(AT_SYSINFO_EHDR)
1793 struct vm_area_struct gate_vma
;
1795 static int __init
gate_vma_init(void)
1797 gate_vma
.vm_mm
= NULL
;
1798 gate_vma
.vm_start
= FIXADDR_USER_START
;
1799 gate_vma
.vm_end
= FIXADDR_USER_END
;
1800 gate_vma
.vm_page_prot
= PAGE_READONLY
;
1801 gate_vma
.vm_flags
= 0;
1804 __initcall(gate_vma_init
);
1807 struct vm_area_struct
*get_gate_vma(struct task_struct
*tsk
)
1809 #ifdef AT_SYSINFO_EHDR
1816 int in_gate_area(struct task_struct
*task
, unsigned long addr
)
1818 #ifdef AT_SYSINFO_EHDR
1819 if ((addr
>= FIXADDR_USER_START
) && (addr
< FIXADDR_USER_END
))