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)
40 #include <linux/mman.h>
41 #include <linux/swap.h>
42 #include <linux/smp_lock.h>
43 #include <linux/swapctl.h>
44 #include <linux/iobuf.h>
45 #include <asm/uaccess.h>
46 #include <asm/pgalloc.h>
47 #include <linux/highmem.h>
48 #include <linux/pagemap.h>
51 unsigned long max_mapnr
= 0;
52 unsigned long num_physpages
= 0;
53 void * high_memory
= NULL
;
54 struct page
*highmem_start_page
;
57 * We special-case the C-O-W ZERO_PAGE, because it's such
58 * a common occurrence (no need to read the page to know
59 * that it's zero - better for the cache and memory subsystem).
61 static inline void copy_cow_page(struct page
* from
, struct page
* to
, unsigned long address
)
63 if (from
== ZERO_PAGE(address
)) {
67 copy_highpage(to
, from
);
70 mem_map_t
* mem_map
= NULL
;
73 * oom() prints a message (so that the user knows why the process died),
74 * and gives the process an untrappable SIGKILL.
76 void oom(struct task_struct
* task
)
78 printk("\nOut of memory for %s.\n", task
->comm
);
79 force_sig(SIGKILL
, task
);
83 * Note: this doesn't free the actual pages themselves. That
84 * has been handled earlier when unmapping all the memory regions.
86 static inline void free_one_pmd(pmd_t
* dir
)
97 pte
= pte_offset(dir
, 0);
102 static inline void free_one_pgd(pgd_t
* dir
)
114 pmd
= pmd_offset(dir
, 0);
116 for (j
= 0; j
< PTRS_PER_PMD
; j
++)
121 /* Low and high watermarks for page table cache.
122 The system should try to have pgt_water[0] <= cache elements <= pgt_water[1]
124 int pgt_cache_water
[2] = { 25, 50 };
126 /* Returns the number of pages freed */
127 int check_pgt_cache(void)
129 return do_check_pgt_cache(pgt_cache_water
[0], pgt_cache_water
[1]);
134 * This function clears all user-level page tables of a process - this
135 * is needed by execve(), so that old pages aren't in the way.
137 void clear_page_tables(struct mm_struct
*mm
, unsigned long first
, int nr
)
139 pgd_t
* page_dir
= mm
->pgd
;
143 free_one_pgd(page_dir
);
147 /* keep the page table cache within bounds */
151 #define PTE_TABLE_MASK ((PTRS_PER_PTE-1) * sizeof(pte_t))
152 #define PMD_TABLE_MASK ((PTRS_PER_PMD-1) * sizeof(pmd_t))
155 * copy one vm_area from one task to the other. Assumes the page tables
156 * already present in the new task to be cleared in the whole range
157 * covered by this vma.
159 * 08Jan98 Merged into one routine from several inline routines to reduce
160 * variable count and make things faster. -jj
162 int copy_page_range(struct mm_struct
*dst
, struct mm_struct
*src
,
163 struct vm_area_struct
*vma
)
165 pgd_t
* src_pgd
, * dst_pgd
;
166 unsigned long address
= vma
->vm_start
;
167 unsigned long end
= vma
->vm_end
;
168 unsigned long cow
= (vma
->vm_flags
& (VM_SHARED
| VM_MAYWRITE
)) == VM_MAYWRITE
;
170 src_pgd
= pgd_offset(src
, address
)-1;
171 dst_pgd
= pgd_offset(dst
, address
)-1;
174 pmd_t
* src_pmd
, * dst_pmd
;
176 src_pgd
++; dst_pgd
++;
180 if (pgd_none(*src_pgd
))
181 goto skip_copy_pmd_range
;
182 if (pgd_bad(*src_pgd
)) {
185 skip_copy_pmd_range
: address
= (address
+ PGDIR_SIZE
) & PGDIR_MASK
;
186 if (!address
|| (address
>= end
))
190 if (pgd_none(*dst_pgd
)) {
191 if (!pmd_alloc(dst_pgd
, 0))
195 src_pmd
= pmd_offset(src_pgd
, address
);
196 dst_pmd
= pmd_offset(dst_pgd
, address
);
199 pte_t
* src_pte
, * dst_pte
;
203 if (pmd_none(*src_pmd
))
204 goto skip_copy_pte_range
;
205 if (pmd_bad(*src_pmd
)) {
208 skip_copy_pte_range
: address
= (address
+ PMD_SIZE
) & PMD_MASK
;
211 goto cont_copy_pmd_range
;
213 if (pmd_none(*dst_pmd
)) {
214 if (!pte_alloc(dst_pmd
, 0))
218 src_pte
= pte_offset(src_pmd
, address
);
219 dst_pte
= pte_offset(dst_pmd
, address
);
222 pte_t pte
= *src_pte
;
223 unsigned long page_nr
;
228 goto cont_copy_pte_range
;
229 if (!pte_present(pte
)) {
230 swap_duplicate(pte_to_swp_entry(pte
));
231 set_pte(dst_pte
, pte
);
232 goto cont_copy_pte_range
;
234 page_nr
= pte_pagenr(pte
);
235 if (page_nr
>= max_mapnr
||
236 PageReserved(mem_map
+page_nr
)) {
237 set_pte(dst_pte
, pte
);
238 goto cont_copy_pte_range
;
240 /* If it's a COW mapping, write protect it both in the parent and the child */
242 pte
= pte_wrprotect(pte
);
243 set_pte(src_pte
, pte
);
245 /* If it's a shared mapping, mark it clean in the child */
246 if (vma
->vm_flags
& VM_SHARED
)
247 pte
= pte_mkclean(pte
);
248 set_pte(dst_pte
, pte_mkold(pte
));
249 get_page(mem_map
+ page_nr
);
251 cont_copy_pte_range
: address
+= PAGE_SIZE
;
256 } while ((unsigned long)src_pte
& PTE_TABLE_MASK
);
258 cont_copy_pmd_range
: src_pmd
++;
260 } while ((unsigned long)src_pmd
& PMD_TABLE_MASK
);
270 * Return indicates whether a page was freed so caller can adjust rss
272 static inline int free_pte(pte_t page
)
274 if (pte_present(page
)) {
275 unsigned long nr
= pte_pagenr(page
);
276 if (nr
>= max_mapnr
|| PageReserved(mem_map
+nr
))
279 * free_page() used to be able to clear swap cache
280 * entries. We may now have to do it manually.
282 free_page_and_swap_cache(mem_map
+nr
);
285 swap_free(pte_to_swp_entry(page
));
289 static inline void forget_pte(pte_t page
)
291 if (!pte_none(page
)) {
292 printk("forget_pte: old mapping existed!\n");
297 static inline int zap_pte_range(struct mm_struct
*mm
, pmd_t
* pmd
, unsigned long address
, unsigned long size
)
309 pte
= pte_offset(pmd
, address
);
310 address
&= ~PMD_MASK
;
311 if (address
+ size
> PMD_SIZE
)
312 size
= PMD_SIZE
- address
;
325 freed
+= free_pte(page
);
330 static inline int zap_pmd_range(struct mm_struct
*mm
, pgd_t
* dir
, unsigned long address
, unsigned long size
)
343 pmd
= pmd_offset(dir
, address
);
344 address
&= ~PGDIR_MASK
;
345 end
= address
+ size
;
346 if (end
> PGDIR_SIZE
)
350 freed
+= zap_pte_range(mm
, pmd
, address
, end
- address
);
351 address
= (address
+ PMD_SIZE
) & PMD_MASK
;
353 } while (address
< end
);
358 * remove user pages in a given range.
360 void zap_page_range(struct mm_struct
*mm
, unsigned long address
, unsigned long size
)
363 unsigned long end
= address
+ size
;
366 dir
= pgd_offset(mm
, address
);
369 * This is a long-lived spinlock. That's fine.
370 * There's no contention, because the page table
371 * lock only protects against kswapd anyway, and
372 * even if kswapd happened to be looking at this
373 * process we _want_ it to get stuck.
377 spin_lock(&mm
->page_table_lock
);
379 freed
+= zap_pmd_range(mm
, dir
, address
, end
- address
);
380 address
= (address
+ PGDIR_SIZE
) & PGDIR_MASK
;
382 } while (address
&& (address
< end
));
383 spin_unlock(&mm
->page_table_lock
);
385 * Update rss for the mm_struct (not necessarily current->mm)
396 * Do a quick page-table lookup for a single page.
398 static struct page
* follow_page(unsigned long address
)
403 pgd
= pgd_offset(current
->mm
, address
);
404 pmd
= pmd_offset(pgd
, address
);
406 pte_t
* pte
= pte_offset(pmd
, address
);
407 if (pte
&& pte_present(*pte
))
408 return pte_page(*pte
);
411 printk(KERN_ERR
"Missing page in follow_page\n");
416 * Given a physical address, is there a useful struct page pointing to it?
419 struct page
* get_page_map(struct page
*page
, unsigned long vaddr
)
421 if (MAP_NR(page
) >= max_mapnr
)
423 if (page
== ZERO_PAGE(vaddr
))
425 if (PageReserved(page
))
431 * Force in an entire range of pages from the current process's user VA,
432 * and pin and lock the pages for IO.
435 #define dprintk(x...)
436 int map_user_kiobuf(int rw
, struct kiobuf
*iobuf
, unsigned long va
, size_t len
)
438 unsigned long ptr
, end
;
440 struct mm_struct
* mm
;
441 struct vm_area_struct
* vma
= 0;
447 /* Make sure the iobuf is not already mapped somewhere. */
452 dprintk ("map_user_kiobuf: begin\n");
454 ptr
= va
& PAGE_MASK
;
455 end
= (va
+ len
+ PAGE_SIZE
- 1) & PAGE_MASK
;
456 err
= expand_kiobuf(iobuf
, (end
- ptr
) >> PAGE_SHIFT
);
465 iobuf
->offset
= va
& ~PAGE_MASK
;
471 * First of all, try to fault in all of the necessary pages
474 if (!vma
|| ptr
>= vma
->vm_end
) {
475 vma
= find_vma(current
->mm
, ptr
);
479 if (handle_mm_fault(current
, vma
, ptr
, (rw
==READ
)) <= 0)
481 spin_lock(&mm
->page_table_lock
);
482 map
= follow_page(ptr
);
484 dprintk (KERN_ERR
"Missing page in map_user_kiobuf\n");
487 map
= get_page_map(map
, ptr
);
489 if (TryLockPage(map
)) {
492 atomic_inc(&map
->count
);
494 spin_unlock(&mm
->page_table_lock
);
495 iobuf
->maplist
[i
] = map
;
496 iobuf
->nr_pages
= ++i
;
502 dprintk ("map_user_kiobuf: end OK\n");
508 dprintk ("map_user_kiobuf: end %d\n", err
);
514 * Undo the locking so far, wait on the page we got to, and try again.
516 spin_unlock(&mm
->page_table_lock
);
521 * Did the release also unlock the page we got stuck on?
524 if (!PageLocked(map
)) {
525 /* If so, we may well have the page mapped twice
526 * in the IO address range. Bad news. Of
527 * course, it _might_ * just be a coincidence,
528 * but if it happens more than * once, chances
529 * are we have a double-mapped page. */
530 if (++doublepage
>= 3) {
542 ptr
= va
& PAGE_MASK
;
550 * Unmap all of the pages referenced by a kiobuf. We release the pages,
551 * and unlock them if they were locked.
554 void unmap_kiobuf (struct kiobuf
*iobuf
)
559 for (i
= 0; i
< iobuf
->nr_pages
; i
++) {
560 map
= iobuf
->maplist
[i
];
562 if (map
&& iobuf
->locked
) {
572 static inline void zeromap_pte_range(pte_t
* pte
, unsigned long address
,
573 unsigned long size
, pgprot_t prot
)
577 address
&= ~PMD_MASK
;
578 end
= address
+ size
;
582 pte_t zero_pte
= pte_wrprotect(mk_pte(ZERO_PAGE(address
), prot
));
583 pte_t oldpage
= *pte
;
584 set_pte(pte
, zero_pte
);
586 address
+= PAGE_SIZE
;
588 } while (address
&& (address
< end
));
591 static inline int zeromap_pmd_range(pmd_t
* pmd
, unsigned long address
,
592 unsigned long size
, pgprot_t prot
)
596 address
&= ~PGDIR_MASK
;
597 end
= address
+ size
;
598 if (end
> PGDIR_SIZE
)
601 pte_t
* pte
= pte_alloc(pmd
, address
);
604 zeromap_pte_range(pte
, address
, end
- address
, prot
);
605 address
= (address
+ PMD_SIZE
) & PMD_MASK
;
607 } while (address
&& (address
< end
));
611 int zeromap_page_range(unsigned long address
, unsigned long size
, pgprot_t prot
)
615 unsigned long beg
= address
;
616 unsigned long end
= address
+ size
;
618 dir
= pgd_offset(current
->mm
, address
);
619 flush_cache_range(current
->mm
, beg
, end
);
623 pmd_t
*pmd
= pmd_alloc(dir
, address
);
627 error
= zeromap_pmd_range(pmd
, address
, end
- address
, prot
);
630 address
= (address
+ PGDIR_SIZE
) & PGDIR_MASK
;
632 } while (address
&& (address
< end
));
633 flush_tlb_range(current
->mm
, beg
, end
);
638 * maps a range of physical memory into the requested pages. the old
639 * mappings are removed. any references to nonexistent pages results
640 * in null mappings (currently treated as "copy-on-access")
642 static inline void remap_pte_range(pte_t
* pte
, unsigned long address
, unsigned long size
,
643 unsigned long phys_addr
, pgprot_t prot
)
647 address
&= ~PMD_MASK
;
648 end
= address
+ size
;
653 pte_t oldpage
= *pte
;
656 mapnr
= MAP_NR(__va(phys_addr
));
657 if (mapnr
>= max_mapnr
|| PageReserved(mem_map
+mapnr
))
658 set_pte(pte
, mk_pte_phys(phys_addr
, prot
));
660 address
+= PAGE_SIZE
;
661 phys_addr
+= PAGE_SIZE
;
663 } while (address
&& (address
< end
));
666 static inline int remap_pmd_range(pmd_t
* pmd
, unsigned long address
, unsigned long size
,
667 unsigned long phys_addr
, pgprot_t prot
)
671 address
&= ~PGDIR_MASK
;
672 end
= address
+ size
;
673 if (end
> PGDIR_SIZE
)
675 phys_addr
-= address
;
677 pte_t
* pte
= pte_alloc(pmd
, address
);
680 remap_pte_range(pte
, address
, end
- address
, address
+ phys_addr
, prot
);
681 address
= (address
+ PMD_SIZE
) & PMD_MASK
;
683 } while (address
&& (address
< end
));
687 int remap_page_range(unsigned long from
, unsigned long phys_addr
, unsigned long size
, pgprot_t prot
)
691 unsigned long beg
= from
;
692 unsigned long end
= from
+ size
;
695 dir
= pgd_offset(current
->mm
, from
);
696 flush_cache_range(current
->mm
, beg
, end
);
700 pmd_t
*pmd
= pmd_alloc(dir
, from
);
704 error
= remap_pmd_range(pmd
, from
, end
- from
, phys_addr
+ from
, prot
);
707 from
= (from
+ PGDIR_SIZE
) & PGDIR_MASK
;
709 } while (from
&& (from
< end
));
710 flush_tlb_range(current
->mm
, beg
, end
);
715 * This routine handles present pages, when users try to write
716 * to a shared page. It is done by copying the page to a new address
717 * and decrementing the shared-page counter for the old page.
719 * Goto-purists beware: the only reason for goto's here is that it results
720 * in better assembly code.. The "default" path will see no jumps at all.
722 * Note that this routine assumes that the protection checks have been
723 * done by the caller (the low-level page fault routine in most cases).
724 * Thus we can safely just mark it writable once we've done any necessary
727 * We also mark the page dirty at this point even though the page will
728 * change only once the write actually happens. This avoids a few races,
729 * and potentially makes it more efficient.
731 * We enter with the page table read-lock held, and need to exit without
734 static int do_wp_page(struct task_struct
* tsk
, struct vm_area_struct
* vma
,
735 unsigned long address
, pte_t
*page_table
, pte_t pte
)
737 unsigned long map_nr
;
738 struct page
*old_page
, *new_page
;
740 map_nr
= pte_pagenr(pte
);
741 if (map_nr
>= max_mapnr
)
744 old_page
= mem_map
+ map_nr
;
747 * We can avoid the copy if:
748 * - we're the only user (count == 1)
749 * - the only other user is the swap cache,
750 * and the only swap cache user is itself,
751 * in which case we can remove the page
752 * from the swap cache.
754 switch (page_count(old_page
)) {
757 * Lock the page so that no one can look it up from
758 * the swap cache, grab a reference and start using it.
759 * Can not do lock_page, holding page_table_lock.
761 if (!PageSwapCache(old_page
) || TryLockPage(old_page
))
763 if (is_page_shared(old_page
)) {
764 UnlockPage(old_page
);
767 delete_from_swap_cache_nolock(old_page
);
768 UnlockPage(old_page
);
771 flush_cache_page(vma
, address
);
772 set_pte(page_table
, pte_mkyoung(pte_mkdirty(pte_mkwrite(pte
))));
773 flush_tlb_page(vma
, address
);
774 spin_unlock(&tsk
->mm
->page_table_lock
);
779 * Ok, we need to copy. Oh, well..
781 spin_unlock(&tsk
->mm
->page_table_lock
);
782 new_page
= alloc_page(GFP_HIGHUSER
);
785 spin_lock(&tsk
->mm
->page_table_lock
);
788 * Re-check the pte - we dropped the lock
790 if (pte_val(*page_table
) == pte_val(pte
)) {
791 if (PageReserved(old_page
))
793 copy_cow_page(old_page
, new_page
, address
);
794 flush_page_to_ram(new_page
);
795 flush_cache_page(vma
, address
);
796 set_pte(page_table
, pte_mkwrite(pte_mkdirty(mk_pte(new_page
, vma
->vm_page_prot
))));
797 flush_tlb_page(vma
, address
);
799 /* Free the old page.. */
802 spin_unlock(&tsk
->mm
->page_table_lock
);
803 __free_page(new_page
);
807 spin_unlock(&tsk
->mm
->page_table_lock
);
808 printk("do_wp_page: bogus page at address %08lx (nr %ld)\n",address
,map_nr
);
813 * This function zeroes out partial mmap'ed pages at truncation time..
815 static void partial_clear(struct vm_area_struct
*vma
, unsigned long address
)
821 pte_t
*page_table
, pte
;
823 page_dir
= pgd_offset(vma
->vm_mm
, address
);
824 if (pgd_none(*page_dir
))
826 if (pgd_bad(*page_dir
)) {
827 pgd_ERROR(*page_dir
);
831 page_middle
= pmd_offset(page_dir
, address
);
832 if (pmd_none(*page_middle
))
834 if (pmd_bad(*page_middle
)) {
835 pmd_ERROR(*page_middle
);
836 pmd_clear(page_middle
);
839 page_table
= pte_offset(page_middle
, address
);
841 if (!pte_present(pte
))
843 flush_cache_page(vma
, address
);
844 page
= pte_page(pte
);
845 if (page
-mem_map
>= max_mapnr
)
847 offset
= address
& ~PAGE_MASK
;
848 memclear_highpage_flush(page
, offset
, PAGE_SIZE
- offset
);
852 * Handle all mappings that got truncated by a "truncate()"
855 * NOTE! We have to be ready to update the memory sharing
856 * between the file and the memory map for a potential last
857 * incomplete page. Ugly, but necessary.
859 void vmtruncate(struct inode
* inode
, loff_t offset
)
861 unsigned long partial
, pgoff
;
862 struct vm_area_struct
* mpnt
;
863 struct address_space
*mapping
= inode
->i_mapping
;
865 inode
->i_size
= offset
;
866 truncate_inode_pages(mapping
, offset
);
867 spin_lock(&mapping
->i_shared_lock
);
868 if (!mapping
->i_mmap
)
871 pgoff
= (offset
+ PAGE_CACHE_SIZE
- 1) >> PAGE_CACHE_SHIFT
;
872 partial
= (unsigned long)offset
& (PAGE_CACHE_SIZE
- 1);
874 mpnt
= mapping
->i_mmap
;
876 struct mm_struct
*mm
= mpnt
->vm_mm
;
877 unsigned long start
= mpnt
->vm_start
;
878 unsigned long end
= mpnt
->vm_end
;
879 unsigned long len
= end
- start
;
882 /* mapping wholly truncated? */
883 if (mpnt
->vm_pgoff
>= pgoff
) {
884 flush_cache_range(mm
, start
, end
);
885 zap_page_range(mm
, start
, len
);
886 flush_tlb_range(mm
, start
, end
);
890 /* mapping wholly unaffected? */
891 len
= len
>> PAGE_SHIFT
;
892 diff
= pgoff
- mpnt
->vm_pgoff
;
896 /* Ok, partially affected.. */
897 start
+= diff
<< PAGE_SHIFT
;
898 len
= (len
- diff
) << PAGE_SHIFT
;
899 if (start
& ~PAGE_MASK
) {
900 partial_clear(mpnt
, start
);
901 start
= (start
+ ~PAGE_MASK
) & PAGE_MASK
;
903 flush_cache_range(mm
, start
, end
);
904 zap_page_range(mm
, start
, len
);
905 flush_tlb_range(mm
, start
, end
);
906 } while ((mpnt
= mpnt
->vm_next_share
) != NULL
);
908 spin_unlock(&mapping
->i_shared_lock
);
909 if (inode
->i_op
&& inode
->i_op
->truncate
)
910 inode
->i_op
->truncate(inode
);
916 * Primitive swap readahead code. We simply read an aligned block of
917 * (1 << page_cluster) entries in the swap area. This method is chosen
918 * because it doesn't cost us any seek time. We also make sure to queue
919 * the 'original' request together with the readahead ones...
921 void swapin_readahead(swp_entry_t entry
)
924 struct page
*new_page
;
925 unsigned long offset
;
928 * Get the number of handles we should do readahead io to. Also,
929 * grab temporary references on them, releasing them as io completes.
931 num
= valid_swaphandles(entry
, &offset
);
932 for (i
= 0; i
< num
; offset
++, i
++) {
933 /* Don't block on I/O for read-ahead */
934 if (atomic_read(&nr_async_pages
) >= pager_daemon
.swap_cluster
) {
936 swap_free(SWP_ENTRY(SWP_TYPE(entry
), offset
++));
939 /* Ok, do the async read-ahead now */
940 new_page
= read_swap_cache_async(SWP_ENTRY(SWP_TYPE(entry
), offset
), 0);
941 if (new_page
!= NULL
)
942 __free_page(new_page
);
943 swap_free(SWP_ENTRY(SWP_TYPE(entry
), offset
));
948 static int do_swap_page(struct task_struct
* tsk
,
949 struct vm_area_struct
* vma
, unsigned long address
,
950 pte_t
* page_table
, swp_entry_t entry
, int write_access
)
952 struct page
*page
= lookup_swap_cache(entry
);
957 swapin_readahead(entry
);
958 page
= read_swap_cache(entry
);
963 flush_page_to_ram(page
);
964 flush_icache_page(vma
, page
);
970 pte
= mk_pte(page
, vma
->vm_page_prot
);
972 set_bit(PG_swap_entry
, &page
->flags
);
975 * Freeze the "shared"ness of the page, ie page_count + swap_count.
976 * Must lock page before transferring our swap count to already
977 * obtained page count.
981 if (write_access
&& !is_page_shared(page
)) {
982 delete_from_swap_cache_nolock(page
);
984 page
= replace_with_highmem(page
);
985 pte
= mk_pte(page
, vma
->vm_page_prot
);
986 pte
= pte_mkwrite(pte_mkdirty(pte
));
990 set_pte(page_table
, pte
);
991 /* No need to invalidate - it was non-present before */
992 update_mmu_cache(vma
, address
, pte
);
997 * This only needs the MM semaphore
999 static int do_anonymous_page(struct task_struct
* tsk
, struct vm_area_struct
* vma
, pte_t
*page_table
, int write_access
, unsigned long addr
)
1002 struct page
*page
= NULL
;
1003 pte_t entry
= pte_wrprotect(mk_pte(ZERO_PAGE(addr
), vma
->vm_page_prot
));
1005 page
= alloc_page(GFP_HIGHUSER
);
1008 if (PageHighMem(page
))
1010 clear_highpage(page
);
1011 entry
= pte_mkwrite(pte_mkdirty(mk_pte(page
, vma
->vm_page_prot
)));
1014 flush_page_to_ram(page
);
1016 set_pte(page_table
, entry
);
1017 /* No need to invalidate - it was non-present before */
1018 update_mmu_cache(vma
, addr
, entry
);
1023 * do_no_page() tries to create a new page mapping. It aggressively
1024 * tries to share with existing pages, but makes a separate copy if
1025 * the "write_access" parameter is true in order to avoid the next
1028 * As this is called only for pages that do not currently exist, we
1029 * do not need to flush old virtual caches or the TLB.
1031 * This is called with the MM semaphore held.
1033 static int do_no_page(struct task_struct
* tsk
, struct vm_area_struct
* vma
,
1034 unsigned long address
, int write_access
, pte_t
*page_table
)
1036 struct page
* new_page
;
1039 if (!vma
->vm_ops
|| !vma
->vm_ops
->nopage
)
1040 return do_anonymous_page(tsk
, vma
, page_table
, write_access
, address
);
1043 * The third argument is "no_share", which tells the low-level code
1044 * to copy, not share the page even if sharing is possible. It's
1045 * essentially an early COW detection.
1047 new_page
= vma
->vm_ops
->nopage(vma
, address
& PAGE_MASK
, (vma
->vm_flags
& VM_SHARED
)?0:write_access
);
1048 if (new_page
== NULL
) /* no page was available -- SIGBUS */
1050 if (new_page
== NOPAGE_OOM
)
1055 * This silly early PAGE_DIRTY setting removes a race
1056 * due to the bad i386 page protection. But it's valid
1057 * for other architectures too.
1059 * Note that if write_access is true, we either now have
1060 * an exclusive copy of the page, or this is a shared mapping,
1061 * so we can make it writable and dirty to avoid having to
1062 * handle that later.
1064 flush_page_to_ram(new_page
);
1065 flush_icache_page(vma
, new_page
);
1066 entry
= mk_pte(new_page
, vma
->vm_page_prot
);
1068 entry
= pte_mkwrite(pte_mkdirty(entry
));
1069 } else if (page_count(new_page
) > 1 &&
1070 !(vma
->vm_flags
& VM_SHARED
))
1071 entry
= pte_wrprotect(entry
);
1072 set_pte(page_table
, entry
);
1073 /* no need to invalidate: a not-present page shouldn't be cached */
1074 update_mmu_cache(vma
, address
, entry
);
1079 * These routines also need to handle stuff like marking pages dirty
1080 * and/or accessed for architectures that don't do it in hardware (most
1081 * RISC architectures). The early dirtying is also good on the i386.
1083 * There is also a hook called "update_mmu_cache()" that architectures
1084 * with external mmu caches can use to update those (ie the Sparc or
1085 * PowerPC hashed page tables that act as extended TLBs).
1087 * Note the "page_table_lock". It is to protect against kswapd removing
1088 * pages from under us. Note that kswapd only ever _removes_ pages, never
1089 * adds them. As such, once we have noticed that the page is not present,
1090 * we can drop the lock early.
1092 * The adding of pages is protected by the MM semaphore (which we hold),
1093 * so we don't need to worry about a page being suddenly been added into
1096 static inline int handle_pte_fault(struct task_struct
*tsk
,
1097 struct vm_area_struct
* vma
, unsigned long address
,
1098 int write_access
, pte_t
* pte
)
1103 if (!pte_present(entry
)) {
1104 if (pte_none(entry
))
1105 return do_no_page(tsk
, vma
, address
, write_access
, pte
);
1106 return do_swap_page(tsk
, vma
, address
, pte
, pte_to_swp_entry(entry
), write_access
);
1110 * Ok, the entry was present, we need to get the page table
1111 * lock to synchronize with kswapd, and verify that the entry
1112 * didn't change from under us..
1114 spin_lock(&tsk
->mm
->page_table_lock
);
1115 if (pte_val(entry
) == pte_val(*pte
)) {
1117 if (!pte_write(entry
))
1118 return do_wp_page(tsk
, vma
, address
, pte
, entry
);
1120 entry
= pte_mkdirty(entry
);
1122 entry
= pte_mkyoung(entry
);
1123 set_pte(pte
, entry
);
1124 flush_tlb_page(vma
, address
);
1125 update_mmu_cache(vma
, address
, entry
);
1127 spin_unlock(&tsk
->mm
->page_table_lock
);
1132 * By the time we get here, we already hold the mm semaphore
1134 int handle_mm_fault(struct task_struct
*tsk
, struct vm_area_struct
* vma
,
1135 unsigned long address
, int write_access
)
1141 pgd
= pgd_offset(vma
->vm_mm
, address
);
1142 pmd
= pmd_alloc(pgd
, address
);
1145 pte_t
* pte
= pte_alloc(pmd
, address
);
1147 ret
= handle_pte_fault(tsk
, vma
, address
, write_access
, pte
);
1153 * Simplistic page force-in..
1155 int make_pages_present(unsigned long addr
, unsigned long end
)
1158 struct task_struct
*tsk
= current
;
1159 struct vm_area_struct
* vma
;
1161 vma
= find_vma(tsk
->mm
, addr
);
1162 write
= (vma
->vm_flags
& VM_WRITE
) != 0;
1166 if (handle_mm_fault(tsk
, vma
, addr
, write
) < 0)
1169 } while (addr
< end
);