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/mman.h>
42 #include <linux/swap.h>
43 #include <linux/highmem.h>
44 #include <linux/pagemap.h>
45 #include <linux/vcache.h>
47 #include <asm/pgalloc.h>
49 #include <asm/uaccess.h>
51 #include <asm/tlbflush.h>
53 #include <linux/swapops.h>
55 #ifndef CONFIG_DISCONTIGMEM
56 /* use the per-pgdat data instead for discontigmem - mbligh */
57 unsigned long max_mapnr
;
61 unsigned long num_physpages
;
63 struct page
*highmem_start_page
;
66 * We special-case the C-O-W ZERO_PAGE, because it's such
67 * a common occurrence (no need to read the page to know
68 * that it's zero - better for the cache and memory subsystem).
70 static inline void copy_cow_page(struct page
* from
, struct page
* to
, unsigned long address
)
72 if (from
== ZERO_PAGE(address
)) {
73 clear_user_highpage(to
, address
);
76 copy_user_highpage(to
, from
, address
);
80 * Note: this doesn't free the actual pages themselves. That
81 * has been handled earlier when unmapping all the memory regions.
83 static inline void free_one_pmd(mmu_gather_t
*tlb
, pmd_t
* dir
)
94 page
= pmd_page(*dir
);
96 pgtable_remove_rmap(page
);
97 pte_free_tlb(tlb
, page
);
100 static inline void free_one_pgd(mmu_gather_t
*tlb
, pgd_t
* dir
)
112 pmd
= pmd_offset(dir
, 0);
114 for (j
= 0; j
< PTRS_PER_PMD
; j
++) {
115 prefetchw(pmd
+j
+(PREFETCH_STRIDE
/16));
116 free_one_pmd(tlb
, pmd
+j
);
118 pmd_free_tlb(tlb
, pmd
);
122 * This function clears all user-level page tables of a process - this
123 * is needed by execve(), so that old pages aren't in the way.
125 * Must be called with pagetable lock held.
127 void clear_page_tables(mmu_gather_t
*tlb
, unsigned long first
, int nr
)
129 pgd_t
* page_dir
= tlb
->mm
->pgd
;
133 free_one_pgd(tlb
, page_dir
);
138 pte_t
* pte_alloc_map(struct mm_struct
*mm
, pmd_t
*pmd
, unsigned long address
)
140 if (!pmd_present(*pmd
)) {
143 spin_unlock(&mm
->page_table_lock
);
144 new = pte_alloc_one(mm
, address
);
145 spin_lock(&mm
->page_table_lock
);
150 * Because we dropped the lock, we should re-check the
151 * entry, as somebody else could have populated it..
153 if (pmd_present(*pmd
)) {
157 pgtable_add_rmap(new, mm
, address
);
158 pmd_populate(mm
, pmd
, new);
161 if (pmd_present(*pmd
))
162 return pte_offset_map(pmd
, address
);
166 pte_t
* pte_alloc_kernel(struct mm_struct
*mm
, pmd_t
*pmd
, unsigned long address
)
168 if (!pmd_present(*pmd
)) {
171 spin_unlock(&mm
->page_table_lock
);
172 new = pte_alloc_one_kernel(mm
, address
);
173 spin_lock(&mm
->page_table_lock
);
178 * Because we dropped the lock, we should re-check the
179 * entry, as somebody else could have populated it..
181 if (pmd_present(*pmd
)) {
182 pte_free_kernel(new);
185 pgtable_add_rmap(virt_to_page(new), mm
, address
);
186 pmd_populate_kernel(mm
, pmd
, new);
189 return pte_offset_kernel(pmd
, address
);
191 #define PTE_TABLE_MASK ((PTRS_PER_PTE-1) * sizeof(pte_t))
192 #define PMD_TABLE_MASK ((PTRS_PER_PMD-1) * sizeof(pmd_t))
195 * copy one vm_area from one task to the other. Assumes the page tables
196 * already present in the new task to be cleared in the whole range
197 * covered by this vma.
199 * 08Jan98 Merged into one routine from several inline routines to reduce
200 * variable count and make things faster. -jj
202 * dst->page_table_lock is held on entry and exit,
203 * but may be dropped within pmd_alloc() and pte_alloc_map().
205 int copy_page_range(struct mm_struct
*dst
, struct mm_struct
*src
,
206 struct vm_area_struct
*vma
)
208 pgd_t
* src_pgd
, * dst_pgd
;
209 unsigned long address
= vma
->vm_start
;
210 unsigned long end
= vma
->vm_end
;
211 unsigned long cow
= (vma
->vm_flags
& (VM_SHARED
| VM_MAYWRITE
)) == VM_MAYWRITE
;
213 if (is_vm_hugetlb_page(vma
))
214 return copy_hugetlb_page_range(dst
, src
, vma
);
216 src_pgd
= pgd_offset(src
, address
)-1;
217 dst_pgd
= pgd_offset(dst
, address
)-1;
220 pmd_t
* src_pmd
, * dst_pmd
;
222 src_pgd
++; dst_pgd
++;
226 if (pgd_none(*src_pgd
))
227 goto skip_copy_pmd_range
;
228 if (pgd_bad(*src_pgd
)) {
231 skip_copy_pmd_range
: address
= (address
+ PGDIR_SIZE
) & PGDIR_MASK
;
232 if (!address
|| (address
>= end
))
237 src_pmd
= pmd_offset(src_pgd
, address
);
238 dst_pmd
= pmd_alloc(dst
, dst_pgd
, address
);
243 pte_t
* src_pte
, * dst_pte
;
247 if (pmd_none(*src_pmd
))
248 goto skip_copy_pte_range
;
249 if (pmd_bad(*src_pmd
)) {
252 skip_copy_pte_range
: address
= (address
+ PMD_SIZE
) & PMD_MASK
;
255 goto cont_copy_pmd_range
;
258 dst_pte
= pte_alloc_map(dst
, dst_pmd
, address
);
261 spin_lock(&src
->page_table_lock
);
262 src_pte
= pte_offset_map_nested(src_pmd
, address
);
264 pte_t pte
= *src_pte
;
265 struct page
*ptepage
;
271 goto cont_copy_pte_range_noset
;
272 /* pte contains position in swap, so copy. */
273 if (!pte_present(pte
)) {
274 swap_duplicate(pte_to_swp_entry(pte
));
275 set_pte(dst_pte
, pte
);
276 goto cont_copy_pte_range_noset
;
278 ptepage
= pte_page(pte
);
281 goto cont_copy_pte_range
;
282 ptepage
= pfn_to_page(pfn
);
283 if (PageReserved(ptepage
))
284 goto cont_copy_pte_range
;
286 /* If it's a COW mapping, write protect it both in the parent and the child */
288 ptep_set_wrprotect(src_pte
);
292 /* If it's a shared mapping, mark it clean in the child */
293 if (vma
->vm_flags
& VM_SHARED
)
294 pte
= pte_mkclean(pte
);
295 pte
= pte_mkold(pte
);
299 cont_copy_pte_range
: set_pte(dst_pte
, pte
);
300 page_add_rmap(ptepage
, dst_pte
);
301 cont_copy_pte_range_noset
: address
+= PAGE_SIZE
;
302 if (address
>= end
) {
303 pte_unmap_nested(src_pte
);
309 } while ((unsigned long)src_pte
& PTE_TABLE_MASK
);
310 pte_unmap_nested(src_pte
-1);
311 pte_unmap(dst_pte
-1);
312 spin_unlock(&src
->page_table_lock
);
314 cont_copy_pmd_range
: src_pmd
++;
316 } while ((unsigned long)src_pmd
& PMD_TABLE_MASK
);
319 spin_unlock(&src
->page_table_lock
);
326 static void zap_pte_range(mmu_gather_t
*tlb
, pmd_t
* pmd
, unsigned long address
, unsigned long size
)
328 unsigned long offset
;
338 ptep
= pte_offset_map(pmd
, address
);
339 offset
= address
& ~PMD_MASK
;
340 if (offset
+ size
> PMD_SIZE
)
341 size
= PMD_SIZE
- offset
;
343 for (offset
=0; offset
< size
; ptep
++, offset
+= PAGE_SIZE
) {
347 if (pte_present(pte
)) {
348 unsigned long pfn
= pte_pfn(pte
);
350 pte
= ptep_get_and_clear(ptep
);
351 tlb_remove_tlb_entry(tlb
, ptep
, address
+offset
);
352 if (pfn_valid(pfn
)) {
353 struct page
*page
= pfn_to_page(pfn
);
354 if (!PageReserved(page
)) {
356 set_page_dirty(page
);
357 if (page
->mapping
&& pte_young(pte
) &&
358 !PageSwapCache(page
))
359 mark_page_accessed(page
);
361 page_remove_rmap(page
, ptep
);
362 tlb_remove_page(tlb
, page
);
366 free_swap_and_cache(pte_to_swp_entry(pte
));
373 static void zap_pmd_range(mmu_gather_t
*tlb
, pgd_t
* dir
, unsigned long address
, unsigned long size
)
385 pmd
= pmd_offset(dir
, address
);
386 end
= address
+ size
;
387 if (end
> ((address
+ PGDIR_SIZE
) & PGDIR_MASK
))
388 end
= ((address
+ PGDIR_SIZE
) & PGDIR_MASK
);
390 zap_pte_range(tlb
, pmd
, address
, end
- address
);
391 address
= (address
+ PMD_SIZE
) & PMD_MASK
;
393 } while (address
< end
);
396 void unmap_page_range(mmu_gather_t
*tlb
, struct vm_area_struct
*vma
, unsigned long address
, unsigned long end
)
400 BUG_ON(address
>= end
);
402 dir
= pgd_offset(vma
->vm_mm
, address
);
403 tlb_start_vma(tlb
, vma
);
405 zap_pmd_range(tlb
, dir
, address
, end
- address
);
406 address
= (address
+ PGDIR_SIZE
) & PGDIR_MASK
;
408 } while (address
&& (address
< end
));
409 tlb_end_vma(tlb
, vma
);
412 /* Dispose of an entire mmu_gather_t per rescheduling point */
413 #if defined(CONFIG_SMP) && defined(CONFIG_PREEMPT)
414 #define ZAP_BLOCK_SIZE (FREE_PTE_NR * PAGE_SIZE)
417 /* For UP, 256 pages at a time gives nice low latency */
418 #if !defined(CONFIG_SMP) && defined(CONFIG_PREEMPT)
419 #define ZAP_BLOCK_SIZE (256 * PAGE_SIZE)
422 /* No preempt: go for the best straight-line efficiency */
423 #if !defined(CONFIG_PREEMPT)
424 #define ZAP_BLOCK_SIZE (~(0UL))
428 * zap_page_range - remove user pages in a given range
429 * @vma: vm_area_struct holding the applicable pages
430 * @address: starting address of pages to zap
431 * @size: number of bytes to zap
433 void zap_page_range(struct vm_area_struct
*vma
, unsigned long address
, unsigned long size
)
435 struct mm_struct
*mm
= vma
->vm_mm
;
437 unsigned long end
, block
;
439 spin_lock(&mm
->page_table_lock
);
442 * This was once a long-held spinlock. Now we break the
443 * work up into ZAP_BLOCK_SIZE units and relinquish the
444 * lock after each interation. This drastically lowers
445 * lock contention and allows for a preemption point.
448 block
= (size
> ZAP_BLOCK_SIZE
) ? ZAP_BLOCK_SIZE
: size
;
449 end
= address
+ block
;
451 flush_cache_range(vma
, address
, end
);
452 tlb
= tlb_gather_mmu(mm
, 0);
453 unmap_page_range(tlb
, vma
, address
, end
);
454 tlb_finish_mmu(tlb
, address
, end
);
456 cond_resched_lock(&mm
->page_table_lock
);
462 spin_unlock(&mm
->page_table_lock
);
466 * Do a quick page-table lookup for a single page.
467 * mm->page_table_lock must be held.
470 follow_page(struct mm_struct
*mm
, unsigned long address
, int write
)
477 pgd
= pgd_offset(mm
, address
);
478 if (pgd_none(*pgd
) || pgd_bad(*pgd
))
481 pmd
= pmd_offset(pgd
, address
);
482 if (pmd_none(*pmd
) || pmd_bad(*pmd
))
485 ptep
= pte_offset_map(pmd
, address
);
491 if (pte_present(pte
)) {
492 if (!write
|| (pte_write(pte
) && pte_dirty(pte
))) {
495 return pfn_to_page(pfn
);
504 * Given a physical address, is there a useful struct page pointing to
505 * it? This may become more complex in the future if we start dealing
506 * with IO-aperture pages for direct-IO.
509 static inline struct page
*get_page_map(struct page
*page
)
511 if (!pfn_valid(page_to_pfn(page
)))
517 int get_user_pages(struct task_struct
*tsk
, struct mm_struct
*mm
,
518 unsigned long start
, int len
, int write
, int force
,
519 struct page
**pages
, struct vm_area_struct
**vmas
)
525 * Require read or write permissions.
526 * If 'force' is set, we only require the "MAY" flags.
528 flags
= write
? (VM_WRITE
| VM_MAYWRITE
) : (VM_READ
| VM_MAYREAD
);
529 flags
&= force
? (VM_MAYREAD
| VM_MAYWRITE
) : (VM_READ
| VM_WRITE
);
533 struct vm_area_struct
* vma
;
535 vma
= find_extend_vma(mm
, start
);
537 if (!vma
|| (pages
&& (vma
->vm_flags
& VM_IO
))
538 || !(flags
& vma
->vm_flags
))
539 return i
? : -EFAULT
;
541 if (is_vm_hugetlb_page(vma
)) {
542 i
= follow_hugetlb_page(mm
, vma
, pages
, vmas
,
546 spin_lock(&mm
->page_table_lock
);
549 while (!(map
= follow_page(mm
, start
, write
))) {
550 spin_unlock(&mm
->page_table_lock
);
551 switch (handle_mm_fault(mm
,vma
,start
,write
)) {
558 case VM_FAULT_SIGBUS
:
559 return i
? i
: -EFAULT
;
561 return i
? i
: -ENOMEM
;
565 spin_lock(&mm
->page_table_lock
);
568 pages
[i
] = get_page_map(map
);
570 spin_unlock(&mm
->page_table_lock
);
572 page_cache_release(pages
[i
]);
576 if (!PageReserved(pages
[i
]))
577 page_cache_get(pages
[i
]);
584 } while(len
&& start
< vma
->vm_end
);
585 spin_unlock(&mm
->page_table_lock
);
591 static inline void zeromap_pte_range(pte_t
* pte
, unsigned long address
,
592 unsigned long size
, pgprot_t prot
)
596 address
&= ~PMD_MASK
;
597 end
= address
+ size
;
601 pte_t zero_pte
= pte_wrprotect(mk_pte(ZERO_PAGE(address
), prot
));
602 BUG_ON(!pte_none(*pte
));
603 set_pte(pte
, zero_pte
);
604 address
+= PAGE_SIZE
;
606 } while (address
&& (address
< end
));
609 static inline int zeromap_pmd_range(struct mm_struct
*mm
, pmd_t
* pmd
, unsigned long address
,
610 unsigned long size
, pgprot_t prot
)
614 address
&= ~PGDIR_MASK
;
615 end
= address
+ size
;
616 if (end
> PGDIR_SIZE
)
619 pte_t
* pte
= pte_alloc_map(mm
, pmd
, address
);
622 zeromap_pte_range(pte
, address
, end
- address
, prot
);
624 address
= (address
+ PMD_SIZE
) & PMD_MASK
;
626 } while (address
&& (address
< end
));
630 int zeromap_page_range(struct vm_area_struct
*vma
, unsigned long address
, unsigned long size
, pgprot_t prot
)
634 unsigned long beg
= address
;
635 unsigned long end
= address
+ size
;
636 struct mm_struct
*mm
= vma
->vm_mm
;
638 dir
= pgd_offset(mm
, address
);
639 flush_cache_range(vma
, beg
, end
);
643 spin_lock(&mm
->page_table_lock
);
645 pmd_t
*pmd
= pmd_alloc(mm
, dir
, address
);
649 error
= zeromap_pmd_range(mm
, pmd
, address
, end
- address
, prot
);
652 address
= (address
+ PGDIR_SIZE
) & PGDIR_MASK
;
654 } while (address
&& (address
< end
));
655 flush_tlb_range(vma
, beg
, end
);
656 spin_unlock(&mm
->page_table_lock
);
661 * maps a range of physical memory into the requested pages. the old
662 * mappings are removed. any references to nonexistent pages results
663 * in null mappings (currently treated as "copy-on-access")
665 static inline void remap_pte_range(pte_t
* pte
, unsigned long address
, unsigned long size
,
666 unsigned long phys_addr
, pgprot_t prot
)
671 address
&= ~PMD_MASK
;
672 end
= address
+ size
;
675 pfn
= phys_addr
>> PAGE_SHIFT
;
677 BUG_ON(!pte_none(*pte
));
678 if (!pfn_valid(pfn
) || PageReserved(pfn_to_page(pfn
)))
679 set_pte(pte
, pfn_pte(pfn
, prot
));
680 address
+= PAGE_SIZE
;
683 } while (address
&& (address
< end
));
686 static inline int remap_pmd_range(struct mm_struct
*mm
, pmd_t
* pmd
, unsigned long address
, unsigned long size
,
687 unsigned long phys_addr
, pgprot_t prot
)
689 unsigned long base
, end
;
691 base
= address
& PGDIR_MASK
;
692 address
&= ~PGDIR_MASK
;
693 end
= address
+ size
;
694 if (end
> PGDIR_SIZE
)
696 phys_addr
-= address
;
698 pte_t
* pte
= pte_alloc_map(mm
, pmd
, base
+ address
);
701 remap_pte_range(pte
, base
+ address
, end
- address
, address
+ phys_addr
, prot
);
703 address
= (address
+ PMD_SIZE
) & PMD_MASK
;
705 } while (address
&& (address
< end
));
709 /* Note: this is only safe if the mm semaphore is held when called. */
710 int remap_page_range(struct vm_area_struct
*vma
, unsigned long from
, unsigned long phys_addr
, unsigned long size
, pgprot_t prot
)
714 unsigned long beg
= from
;
715 unsigned long end
= from
+ size
;
716 struct mm_struct
*mm
= vma
->vm_mm
;
719 dir
= pgd_offset(mm
, from
);
720 flush_cache_range(vma
, beg
, end
);
724 spin_lock(&mm
->page_table_lock
);
726 pmd_t
*pmd
= pmd_alloc(mm
, dir
, from
);
730 error
= remap_pmd_range(mm
, pmd
, from
, end
- from
, phys_addr
+ from
, prot
);
733 from
= (from
+ PGDIR_SIZE
) & PGDIR_MASK
;
735 } while (from
&& (from
< end
));
736 flush_tlb_range(vma
, beg
, end
);
737 spin_unlock(&mm
->page_table_lock
);
742 * Establish a new mapping:
743 * - flush the old one
744 * - update the page tables
745 * - inform the TLB about the new one
747 * We hold the mm semaphore for reading and vma->vm_mm->page_table_lock
749 static inline void establish_pte(struct vm_area_struct
* vma
, unsigned long address
, pte_t
*page_table
, pte_t entry
)
751 set_pte(page_table
, entry
);
752 flush_tlb_page(vma
, address
);
753 update_mmu_cache(vma
, address
, entry
);
757 * We hold the mm semaphore for reading and vma->vm_mm->page_table_lock
759 static inline void break_cow(struct vm_area_struct
* vma
, struct page
* new_page
, unsigned long address
,
762 invalidate_vcache(address
, vma
->vm_mm
, new_page
);
763 flush_page_to_ram(new_page
);
764 flush_cache_page(vma
, address
);
765 establish_pte(vma
, address
, page_table
, pte_mkwrite(pte_mkdirty(mk_pte(new_page
, vma
->vm_page_prot
))));
769 * This routine handles present pages, when users try to write
770 * to a shared page. It is done by copying the page to a new address
771 * and decrementing the shared-page counter for the old page.
773 * Goto-purists beware: the only reason for goto's here is that it results
774 * in better assembly code.. The "default" path will see no jumps at all.
776 * Note that this routine assumes that the protection checks have been
777 * done by the caller (the low-level page fault routine in most cases).
778 * Thus we can safely just mark it writable once we've done any necessary
781 * We also mark the page dirty at this point even though the page will
782 * change only once the write actually happens. This avoids a few races,
783 * and potentially makes it more efficient.
785 * We hold the mm semaphore and the page_table_lock on entry and exit
786 * with the page_table_lock released.
788 static int do_wp_page(struct mm_struct
*mm
, struct vm_area_struct
* vma
,
789 unsigned long address
, pte_t
*page_table
, pmd_t
*pmd
, pte_t pte
)
791 struct page
*old_page
, *new_page
;
792 unsigned long pfn
= pte_pfn(pte
);
796 old_page
= pfn_to_page(pfn
);
798 if (!TestSetPageLocked(old_page
)) {
799 int reuse
= can_share_swap_page(old_page
);
800 unlock_page(old_page
);
802 flush_cache_page(vma
, address
);
803 establish_pte(vma
, address
, page_table
, pte_mkyoung(pte_mkdirty(pte_mkwrite(pte
))));
804 pte_unmap(page_table
);
805 spin_unlock(&mm
->page_table_lock
);
806 return VM_FAULT_MINOR
;
809 pte_unmap(page_table
);
812 * Ok, we need to copy. Oh, well..
814 page_cache_get(old_page
);
815 spin_unlock(&mm
->page_table_lock
);
817 new_page
= alloc_page(GFP_HIGHUSER
);
820 copy_cow_page(old_page
,new_page
,address
);
823 * Re-check the pte - we dropped the lock
825 spin_lock(&mm
->page_table_lock
);
826 page_table
= pte_offset_map(pmd
, address
);
827 if (pte_same(*page_table
, pte
)) {
828 if (PageReserved(old_page
))
830 page_remove_rmap(old_page
, page_table
);
831 break_cow(vma
, new_page
, address
, page_table
);
832 page_add_rmap(new_page
, page_table
);
833 lru_cache_add(new_page
);
835 /* Free the old page.. */
838 pte_unmap(page_table
);
839 spin_unlock(&mm
->page_table_lock
);
840 page_cache_release(new_page
);
841 page_cache_release(old_page
);
842 return VM_FAULT_MINOR
;
845 pte_unmap(page_table
);
846 spin_unlock(&mm
->page_table_lock
);
847 printk(KERN_ERR
"do_wp_page: bogus page at address %08lx\n", address
);
849 * This should really halt the system so it can be debugged or
850 * at least the kernel stops what it's doing before it corrupts
851 * data, but for the moment just pretend this is OOM.
855 page_cache_release(old_page
);
859 static void vmtruncate_list(struct list_head
*head
, unsigned long pgoff
)
861 unsigned long start
, end
, len
, diff
;
862 struct vm_area_struct
*vma
;
863 struct list_head
*curr
;
865 list_for_each(curr
, head
) {
866 vma
= list_entry(curr
, struct vm_area_struct
, shared
);
867 start
= vma
->vm_start
;
871 /* mapping wholly truncated? */
872 if (vma
->vm_pgoff
>= pgoff
) {
873 zap_page_range(vma
, start
, len
);
877 /* mapping wholly unaffected? */
878 len
= len
>> PAGE_SHIFT
;
879 diff
= pgoff
- vma
->vm_pgoff
;
883 /* Ok, partially affected.. */
884 start
+= diff
<< PAGE_SHIFT
;
885 len
= (len
- diff
) << PAGE_SHIFT
;
886 zap_page_range(vma
, start
, len
);
891 * Handle all mappings that got truncated by a "truncate()"
894 * NOTE! We have to be ready to update the memory sharing
895 * between the file and the memory map for a potential last
896 * incomplete page. Ugly, but necessary.
898 int vmtruncate(struct inode
* inode
, loff_t offset
)
901 struct address_space
*mapping
= inode
->i_mapping
;
904 if (inode
->i_size
< offset
)
906 inode
->i_size
= offset
;
907 spin_lock(&mapping
->i_shared_lock
);
908 if (list_empty(&mapping
->i_mmap
) && list_empty(&mapping
->i_mmap_shared
))
911 pgoff
= (offset
+ PAGE_CACHE_SIZE
- 1) >> PAGE_CACHE_SHIFT
;
912 if (!list_empty(&mapping
->i_mmap
))
913 vmtruncate_list(&mapping
->i_mmap
, pgoff
);
914 if (!list_empty(&mapping
->i_mmap_shared
))
915 vmtruncate_list(&mapping
->i_mmap_shared
, pgoff
);
918 spin_unlock(&mapping
->i_shared_lock
);
919 truncate_inode_pages(mapping
, offset
);
923 limit
= current
->rlim
[RLIMIT_FSIZE
].rlim_cur
;
924 if (limit
!= RLIM_INFINITY
&& offset
> limit
)
926 if (offset
> inode
->i_sb
->s_maxbytes
)
928 inode
->i_size
= offset
;
931 if (inode
->i_op
&& inode
->i_op
->truncate
)
932 inode
->i_op
->truncate(inode
);
935 send_sig(SIGXFSZ
, current
, 0);
941 * Primitive swap readahead code. We simply read an aligned block of
942 * (1 << page_cluster) entries in the swap area. This method is chosen
943 * because it doesn't cost us any seek time. We also make sure to queue
944 * the 'original' request together with the readahead ones...
946 void swapin_readahead(swp_entry_t entry
)
949 struct page
*new_page
;
950 unsigned long offset
;
953 * Get the number of handles we should do readahead io to.
955 num
= valid_swaphandles(entry
, &offset
);
956 for (i
= 0; i
< num
; offset
++, i
++) {
957 /* Ok, do the async read-ahead now */
958 new_page
= read_swap_cache_async(swp_entry(swp_type(entry
), offset
));
961 page_cache_release(new_page
);
967 * We hold the mm semaphore and the page_table_lock on entry and
968 * should release the pagetable lock on exit..
970 static int do_swap_page(struct mm_struct
* mm
,
971 struct vm_area_struct
* vma
, unsigned long address
,
972 pte_t
*page_table
, pmd_t
*pmd
, pte_t orig_pte
, int write_access
)
975 swp_entry_t entry
= pte_to_swp_entry(orig_pte
);
977 int ret
= VM_FAULT_MINOR
;
979 pte_unmap(page_table
);
980 spin_unlock(&mm
->page_table_lock
);
981 page
= lookup_swap_cache(entry
);
983 swapin_readahead(entry
);
984 page
= read_swap_cache_async(entry
);
987 * Back out if somebody else faulted in this pte while
988 * we released the page table lock.
990 spin_lock(&mm
->page_table_lock
);
991 page_table
= pte_offset_map(pmd
, address
);
992 if (pte_same(*page_table
, orig_pte
))
995 ret
= VM_FAULT_MINOR
;
996 pte_unmap(page_table
);
997 spin_unlock(&mm
->page_table_lock
);
1001 /* Had to read the page from swap area: Major fault */
1002 ret
= VM_FAULT_MAJOR
;
1003 inc_page_state(pgmajfault
);
1006 mark_page_accessed(page
);
1010 * Back out if somebody else faulted in this pte while we
1011 * released the page table lock.
1013 spin_lock(&mm
->page_table_lock
);
1014 page_table
= pte_offset_map(pmd
, address
);
1015 if (!pte_same(*page_table
, orig_pte
)) {
1016 pte_unmap(page_table
);
1017 spin_unlock(&mm
->page_table_lock
);
1019 page_cache_release(page
);
1020 return VM_FAULT_MINOR
;
1023 /* The page isn't present yet, go ahead with the fault. */
1027 remove_exclusive_swap_page(page
);
1030 pte
= mk_pte(page
, vma
->vm_page_prot
);
1031 if (write_access
&& can_share_swap_page(page
))
1032 pte
= pte_mkdirty(pte_mkwrite(pte
));
1035 flush_page_to_ram(page
);
1036 flush_icache_page(vma
, page
);
1037 set_pte(page_table
, pte
);
1038 page_add_rmap(page
, page_table
);
1040 /* No need to invalidate - it was non-present before */
1041 update_mmu_cache(vma
, address
, pte
);
1042 pte_unmap(page_table
);
1043 spin_unlock(&mm
->page_table_lock
);
1048 * We are called with the MM semaphore and page_table_lock
1049 * spinlock held to protect against concurrent faults in
1050 * multithreaded programs.
1052 static int do_anonymous_page(struct mm_struct
* mm
, struct vm_area_struct
* vma
, pte_t
*page_table
, pmd_t
*pmd
, int write_access
, unsigned long addr
)
1055 struct page
* page
= ZERO_PAGE(addr
);
1057 /* Read-only mapping of ZERO_PAGE. */
1058 entry
= pte_wrprotect(mk_pte(ZERO_PAGE(addr
), vma
->vm_page_prot
));
1060 /* ..except if it's a write access */
1062 /* Allocate our own private page. */
1063 pte_unmap(page_table
);
1064 spin_unlock(&mm
->page_table_lock
);
1066 page
= alloc_page(GFP_HIGHUSER
);
1069 clear_user_highpage(page
, addr
);
1071 spin_lock(&mm
->page_table_lock
);
1072 page_table
= pte_offset_map(pmd
, addr
);
1074 if (!pte_none(*page_table
)) {
1075 pte_unmap(page_table
);
1076 page_cache_release(page
);
1077 spin_unlock(&mm
->page_table_lock
);
1078 return VM_FAULT_MINOR
;
1081 flush_page_to_ram(page
);
1082 entry
= pte_mkwrite(pte_mkdirty(mk_pte(page
, vma
->vm_page_prot
)));
1083 lru_cache_add(page
);
1084 mark_page_accessed(page
);
1087 set_pte(page_table
, entry
);
1088 page_add_rmap(page
, page_table
); /* ignores ZERO_PAGE */
1089 pte_unmap(page_table
);
1091 /* No need to invalidate - it was non-present before */
1092 update_mmu_cache(vma
, addr
, entry
);
1093 spin_unlock(&mm
->page_table_lock
);
1094 return VM_FAULT_MINOR
;
1097 return VM_FAULT_OOM
;
1101 * do_no_page() tries to create a new page mapping. It aggressively
1102 * tries to share with existing pages, but makes a separate copy if
1103 * the "write_access" parameter is true in order to avoid the next
1106 * As this is called only for pages that do not currently exist, we
1107 * do not need to flush old virtual caches or the TLB.
1109 * This is called with the MM semaphore held and the page table
1110 * spinlock held. Exit with the spinlock released.
1112 static int do_no_page(struct mm_struct
* mm
, struct vm_area_struct
* vma
,
1113 unsigned long address
, int write_access
, pte_t
*page_table
, pmd_t
*pmd
)
1115 struct page
* new_page
;
1118 if (!vma
->vm_ops
|| !vma
->vm_ops
->nopage
)
1119 return do_anonymous_page(mm
, vma
, page_table
, pmd
, write_access
, address
);
1120 pte_unmap(page_table
);
1121 spin_unlock(&mm
->page_table_lock
);
1123 new_page
= vma
->vm_ops
->nopage(vma
, address
& PAGE_MASK
, 0);
1125 /* no page was available -- either SIGBUS or OOM */
1126 if (new_page
== NOPAGE_SIGBUS
)
1127 return VM_FAULT_SIGBUS
;
1128 if (new_page
== NOPAGE_OOM
)
1129 return VM_FAULT_OOM
;
1132 * Should we do an early C-O-W break?
1134 if (write_access
&& !(vma
->vm_flags
& VM_SHARED
)) {
1135 struct page
* page
= alloc_page(GFP_HIGHUSER
);
1137 page_cache_release(new_page
);
1138 return VM_FAULT_OOM
;
1140 copy_user_highpage(page
, new_page
, address
);
1141 page_cache_release(new_page
);
1142 lru_cache_add(page
);
1146 spin_lock(&mm
->page_table_lock
);
1147 page_table
= pte_offset_map(pmd
, address
);
1150 * This silly early PAGE_DIRTY setting removes a race
1151 * due to the bad i386 page protection. But it's valid
1152 * for other architectures too.
1154 * Note that if write_access is true, we either now have
1155 * an exclusive copy of the page, or this is a shared mapping,
1156 * so we can make it writable and dirty to avoid having to
1157 * handle that later.
1159 /* Only go through if we didn't race with anybody else... */
1160 if (pte_none(*page_table
)) {
1162 flush_page_to_ram(new_page
);
1163 flush_icache_page(vma
, new_page
);
1164 entry
= mk_pte(new_page
, vma
->vm_page_prot
);
1166 entry
= pte_mkwrite(pte_mkdirty(entry
));
1167 set_pte(page_table
, entry
);
1168 page_add_rmap(new_page
, page_table
);
1169 pte_unmap(page_table
);
1171 /* One of our sibling threads was faster, back out. */
1172 pte_unmap(page_table
);
1173 page_cache_release(new_page
);
1174 spin_unlock(&mm
->page_table_lock
);
1175 return VM_FAULT_MINOR
;
1178 /* no need to invalidate: a not-present page shouldn't be cached */
1179 update_mmu_cache(vma
, address
, entry
);
1180 spin_unlock(&mm
->page_table_lock
);
1181 return VM_FAULT_MAJOR
;
1185 * These routines also need to handle stuff like marking pages dirty
1186 * and/or accessed for architectures that don't do it in hardware (most
1187 * RISC architectures). The early dirtying is also good on the i386.
1189 * There is also a hook called "update_mmu_cache()" that architectures
1190 * with external mmu caches can use to update those (ie the Sparc or
1191 * PowerPC hashed page tables that act as extended TLBs).
1193 * Note the "page_table_lock". It is to protect against kswapd removing
1194 * pages from under us. Note that kswapd only ever _removes_ pages, never
1195 * adds them. As such, once we have noticed that the page is not present,
1196 * we can drop the lock early.
1198 * The adding of pages is protected by the MM semaphore (which we hold),
1199 * so we don't need to worry about a page being suddenly been added into
1202 * We enter with the pagetable spinlock held, we are supposed to
1203 * release it when done.
1205 static inline int handle_pte_fault(struct mm_struct
*mm
,
1206 struct vm_area_struct
* vma
, unsigned long address
,
1207 int write_access
, pte_t
*pte
, pmd_t
*pmd
)
1212 if (!pte_present(entry
)) {
1214 * If it truly wasn't present, we know that kswapd
1215 * and the PTE updates will not touch it later. So
1218 if (pte_none(entry
))
1219 return do_no_page(mm
, vma
, address
, write_access
, pte
, pmd
);
1220 return do_swap_page(mm
, vma
, address
, pte
, pmd
, entry
, write_access
);
1224 if (!pte_write(entry
))
1225 return do_wp_page(mm
, vma
, address
, pte
, pmd
, entry
);
1227 entry
= pte_mkdirty(entry
);
1229 entry
= pte_mkyoung(entry
);
1230 establish_pte(vma
, address
, pte
, entry
);
1232 spin_unlock(&mm
->page_table_lock
);
1233 return VM_FAULT_MINOR
;
1237 * By the time we get here, we already hold the mm semaphore
1239 int handle_mm_fault(struct mm_struct
*mm
, struct vm_area_struct
* vma
,
1240 unsigned long address
, int write_access
)
1245 current
->state
= TASK_RUNNING
;
1246 pgd
= pgd_offset(mm
, address
);
1248 inc_page_state(pgfault
);
1250 * We need the page table lock to synchronize with kswapd
1251 * and the SMP-safe atomic PTE updates.
1253 spin_lock(&mm
->page_table_lock
);
1254 pmd
= pmd_alloc(mm
, pgd
, address
);
1257 pte_t
* pte
= pte_alloc_map(mm
, pmd
, address
);
1259 return handle_pte_fault(mm
, vma
, address
, write_access
, pte
, pmd
);
1261 spin_unlock(&mm
->page_table_lock
);
1262 return VM_FAULT_OOM
;
1266 * Allocate page middle directory.
1268 * We've already handled the fast-path in-line, and we own the
1271 * On a two-level page table, this ends up actually being entirely
1274 pmd_t
*__pmd_alloc(struct mm_struct
*mm
, pgd_t
*pgd
, unsigned long address
)
1278 spin_unlock(&mm
->page_table_lock
);
1279 new = pmd_alloc_one(mm
, address
);
1280 spin_lock(&mm
->page_table_lock
);
1285 * Because we dropped the lock, we should re-check the
1286 * entry, as somebody else could have populated it..
1288 if (pgd_present(*pgd
)) {
1292 pgd_populate(mm
, pgd
, new);
1294 return pmd_offset(pgd
, address
);
1297 int make_pages_present(unsigned long addr
, unsigned long end
)
1299 int ret
, len
, write
;
1300 struct vm_area_struct
* vma
;
1302 vma
= find_vma(current
->mm
, addr
);
1303 write
= (vma
->vm_flags
& VM_WRITE
) != 0;
1306 if (end
> vma
->vm_end
)
1308 len
= (end
+PAGE_SIZE
-1)/PAGE_SIZE
-addr
/PAGE_SIZE
;
1309 ret
= get_user_pages(current
, current
->mm
, addr
,
1310 len
, write
, 0, NULL
, NULL
);
1311 return ret
== len
? 0 : -1;
1315 * Map a vmalloc()-space virtual address to the physical page.
1317 struct page
* vmalloc_to_page(void * vmalloc_addr
)
1319 unsigned long addr
= (unsigned long) vmalloc_addr
;
1320 struct page
*page
= NULL
;
1321 pgd_t
*pgd
= pgd_offset_k(addr
);
1325 if (!pgd_none(*pgd
)) {
1326 pmd
= pmd_offset(pgd
, addr
);
1327 if (!pmd_none(*pmd
)) {
1329 ptep
= pte_offset_map(pmd
, addr
);
1331 if (pte_present(pte
))
1332 page
= pte_page(pte
);