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)
37 #include <linux/mman.h>
38 #include <linux/swap.h>
39 #include <linux/pagemap.h>
40 #include <linux/smp_lock.h>
41 #include <linux/swapctl.h>
42 #include <linux/iobuf.h>
44 #include <asm/uaccess.h>
45 #include <asm/pgtable.h>
47 unsigned long max_mapnr
= 0;
48 unsigned long num_physpages
= 0;
49 void * high_memory
= NULL
;
52 * We special-case the C-O-W ZERO_PAGE, because it's such
53 * a common occurrence (no need to read the page to know
54 * that it's zero - better for the cache and memory subsystem).
56 static inline void copy_cow_page(unsigned long from
, unsigned long to
)
58 if (from
== ZERO_PAGE(to
)) {
65 mem_map_t
* mem_map
= NULL
;
68 * oom() prints a message (so that the user knows why the process died),
69 * and gives the process an untrappable SIGKILL.
71 void oom(struct task_struct
* task
)
73 printk("\nOut of memory for %s.\n", task
->comm
);
74 force_sig(SIGKILL
, task
);
78 * Note: this doesn't free the actual pages themselves. That
79 * has been handled earlier when unmapping all the memory regions.
81 static inline void free_one_pmd(pmd_t
* dir
)
88 printk("free_one_pmd: bad directory entry %08lx\n", pmd_val(*dir
));
92 pte
= pte_offset(dir
, 0);
97 static inline void free_one_pgd(pgd_t
* dir
)
105 printk("free_one_pgd: bad directory entry %08lx\n", pgd_val(*dir
));
109 pmd
= pmd_offset(dir
, 0);
111 for (j
= 0; j
< PTRS_PER_PMD
; j
++)
116 /* Low and high watermarks for page table cache.
117 The system should try to have pgt_water[0] <= cache elements <= pgt_water[1]
119 int pgt_cache_water
[2] = { 25, 50 };
121 /* Returns the number of pages freed */
122 int check_pgt_cache(void)
124 return do_check_pgt_cache(pgt_cache_water
[0], pgt_cache_water
[1]);
129 * This function clears all user-level page tables of a process - this
130 * is needed by execve(), so that old pages aren't in the way.
132 void clear_page_tables(struct mm_struct
*mm
, unsigned long first
, int nr
)
134 pgd_t
* page_dir
= mm
->pgd
;
138 free_one_pgd(page_dir
);
142 /* keep the page table cache within bounds */
146 #define PTE_TABLE_MASK ((PTRS_PER_PTE-1) * sizeof(pte_t))
147 #define PMD_TABLE_MASK ((PTRS_PER_PMD-1) * sizeof(pmd_t))
150 * copy one vm_area from one task to the other. Assumes the page tables
151 * already present in the new task to be cleared in the whole range
152 * covered by this vma.
154 * 08Jan98 Merged into one routine from several inline routines to reduce
155 * variable count and make things faster. -jj
157 int copy_page_range(struct mm_struct
*dst
, struct mm_struct
*src
,
158 struct vm_area_struct
*vma
)
160 pgd_t
* src_pgd
, * dst_pgd
;
161 unsigned long address
= vma
->vm_start
;
162 unsigned long end
= vma
->vm_end
;
163 unsigned long cow
= (vma
->vm_flags
& (VM_SHARED
| VM_MAYWRITE
)) == VM_MAYWRITE
;
165 src_pgd
= pgd_offset(src
, address
)-1;
166 dst_pgd
= pgd_offset(dst
, address
)-1;
169 pmd_t
* src_pmd
, * dst_pmd
;
171 src_pgd
++; dst_pgd
++;
175 if (pgd_none(*src_pgd
))
176 goto skip_copy_pmd_range
;
177 if (pgd_bad(*src_pgd
)) {
178 printk("copy_pmd_range: bad pgd (%08lx)\n",
181 skip_copy_pmd_range
: address
= (address
+ PGDIR_SIZE
) & PGDIR_MASK
;
186 if (pgd_none(*dst_pgd
)) {
187 if (!pmd_alloc(dst_pgd
, 0))
191 src_pmd
= pmd_offset(src_pgd
, address
);
192 dst_pmd
= pmd_offset(dst_pgd
, address
);
195 pte_t
* src_pte
, * dst_pte
;
199 if (pmd_none(*src_pmd
))
200 goto skip_copy_pte_range
;
201 if (pmd_bad(*src_pmd
)) {
202 printk("copy_pte_range: bad pmd (%08lx)\n", pmd_val(*src_pmd
));
204 skip_copy_pte_range
: address
= (address
+ PMD_SIZE
) & PMD_MASK
;
207 goto cont_copy_pmd_range
;
209 if (pmd_none(*dst_pmd
)) {
210 if (!pte_alloc(dst_pmd
, 0))
214 src_pte
= pte_offset(src_pmd
, address
);
215 dst_pte
= pte_offset(dst_pmd
, address
);
218 pte_t pte
= *src_pte
;
219 unsigned long page_nr
;
224 goto cont_copy_pte_range
;
225 if (!pte_present(pte
)) {
226 swap_duplicate(pte_val(pte
));
227 set_pte(dst_pte
, pte
);
228 goto cont_copy_pte_range
;
230 page_nr
= MAP_NR(pte_page(pte
));
231 if (page_nr
>= max_mapnr
||
232 PageReserved(mem_map
+page_nr
)) {
233 set_pte(dst_pte
, pte
);
234 goto cont_copy_pte_range
;
236 /* If it's a COW mapping, write protect it both in the parent and the child */
238 pte
= pte_wrprotect(pte
);
239 set_pte(src_pte
, pte
);
241 /* If it's a shared mapping, mark it clean in the child */
242 if (vma
->vm_flags
& VM_SHARED
)
243 pte
= pte_mkclean(pte
);
244 set_pte(dst_pte
, pte_mkold(pte
));
245 get_page(mem_map
+ page_nr
);
247 cont_copy_pte_range
: address
+= PAGE_SIZE
;
252 } while ((unsigned long)src_pte
& PTE_TABLE_MASK
);
254 cont_copy_pmd_range
: src_pmd
++;
256 } while ((unsigned long)src_pmd
& PMD_TABLE_MASK
);
266 * Return indicates whether a page was freed so caller can adjust rss
268 static inline int free_pte(pte_t page
)
270 if (pte_present(page
)) {
271 unsigned long addr
= pte_page(page
);
272 if (MAP_NR(addr
) >= max_mapnr
|| PageReserved(mem_map
+MAP_NR(addr
)))
275 * free_page() used to be able to clear swap cache
276 * entries. We may now have to do it manually.
278 free_page_and_swap_cache(addr
);
281 swap_free(pte_val(page
));
285 static inline void forget_pte(pte_t page
)
287 if (!pte_none(page
)) {
288 printk("forget_pte: old mapping existed!\n");
293 static inline int zap_pte_range(struct mm_struct
*mm
, pmd_t
* pmd
, unsigned long address
, unsigned long size
)
301 printk("zap_pte_range: bad pmd (%08lx)\n", pmd_val(*pmd
));
305 pte
= pte_offset(pmd
, address
);
306 address
&= ~PMD_MASK
;
307 if (address
+ size
> PMD_SIZE
)
308 size
= PMD_SIZE
- address
;
321 freed
+= free_pte(page
);
326 static inline int zap_pmd_range(struct mm_struct
*mm
, pgd_t
* dir
, unsigned long address
, unsigned long size
)
335 printk("zap_pmd_range: bad pgd (%08lx)\n", pgd_val(*dir
));
339 pmd
= pmd_offset(dir
, address
);
340 address
&= ~PGDIR_MASK
;
341 end
= address
+ size
;
342 if (end
> PGDIR_SIZE
)
346 freed
+= zap_pte_range(mm
, pmd
, address
, end
- address
);
347 address
= (address
+ PMD_SIZE
) & PMD_MASK
;
349 } while (address
< end
);
354 * remove user pages in a given range.
356 void zap_page_range(struct mm_struct
*mm
, unsigned long address
, unsigned long size
)
359 unsigned long end
= address
+ size
;
362 dir
= pgd_offset(mm
, address
);
365 * This is a long-lived spinlock. That's fine.
366 * There's no contention, because the page table
367 * lock only protects against kswapd anyway, and
368 * even if kswapd happened to be looking at this
369 * process we _want_ it to get stuck.
371 spin_lock(&mm
->page_table_lock
);
372 while (address
< end
) {
373 freed
+= zap_pmd_range(mm
, dir
, address
, end
- address
);
374 address
= (address
+ PGDIR_SIZE
) & PGDIR_MASK
;
377 spin_unlock(&mm
->page_table_lock
);
379 * Update rss for the mm_struct (not necessarily current->mm)
390 * Do a quick page-table lookup for a single page.
392 static unsigned long follow_page(unsigned long address
)
397 pgd
= pgd_offset(current
->mm
, address
);
398 pmd
= pmd_offset(pgd
, address
);
400 pte_t
* pte
= pte_offset(pmd
, address
);
401 if (pte
&& pte_present(*pte
)) {
402 return pte_page(*pte
);
406 printk(KERN_ERR
"Missing page in follow_page\n");
411 * Given a physical address, is there a useful struct page pointing to it?
414 static struct page
* get_page_map(unsigned long page
)
418 if (MAP_NR(page
) >= max_mapnr
)
420 if (page
== ZERO_PAGE(page
))
422 map
= mem_map
+ MAP_NR(page
);
423 if (PageReserved(map
))
429 * Force in an entire range of pages from the current process's user VA,
430 * and pin and lock the pages for IO.
433 #define dprintk(x...)
434 int map_user_kiobuf(int rw
, struct kiobuf
*iobuf
, unsigned long va
, size_t len
)
436 unsigned long ptr
, end
;
438 struct mm_struct
* mm
;
439 struct vm_area_struct
* vma
= 0;
446 /* Make sure the iobuf is not already mapped somewhere. */
451 dprintk ("map_user_kiobuf: begin\n");
453 ptr
= va
& PAGE_MASK
;
454 end
= (va
+ len
+ PAGE_SIZE
- 1) & PAGE_MASK
;
455 err
= expand_kiobuf(iobuf
, (end
- ptr
) >> PAGE_SHIFT
);
464 iobuf
->offset
= va
& ~PAGE_MASK
;
470 * First of all, try to fault in all of the necessary pages
473 if (!vma
|| ptr
>= vma
->vm_end
) {
474 vma
= find_vma(current
->mm
, ptr
);
478 if (handle_mm_fault(current
, vma
, ptr
, (rw
==READ
)) <= 0)
480 spin_lock(&mm
->page_table_lock
);
481 page
= follow_page(ptr
);
483 dprintk (KERN_ERR
"Missing page in map_user_kiobuf\n");
487 map
= get_page_map(page
);
489 if (TryLockPage(map
)) {
492 atomic_inc(&map
->count
);
494 spin_unlock(&mm
->page_table_lock
);
495 dprintk ("Installing page %p %p: %d\n", (void *)page
, map
, i
);
496 iobuf
->pagelist
[i
] = page
;
497 iobuf
->maplist
[i
] = map
;
498 iobuf
->nr_pages
= ++i
;
504 dprintk ("map_user_kiobuf: end OK\n");
510 dprintk ("map_user_kiobuf: end %d\n", err
);
516 * Undo the locking so far, wait on the page we got to, and try again.
518 spin_unlock(&mm
->page_table_lock
);
523 * Did the release also unlock the page we got stuck on?
526 if (!PageLocked(map
)) {
527 /* If so, we may well have the page mapped twice
528 * in the IO address range. Bad news. Of
529 * course, it _might_ * just be a coincidence,
530 * but if it happens more than * once, chances
531 * are we have a double-mapped page. */
532 if (++doublepage
>= 3) {
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
),
584 pte_t oldpage
= *pte
;
585 set_pte(pte
, zero_pte
);
587 address
+= PAGE_SIZE
;
589 } while (address
< end
);
592 static inline int zeromap_pmd_range(pmd_t
* pmd
, unsigned long address
,
593 unsigned long size
, pgprot_t prot
)
597 address
&= ~PGDIR_MASK
;
598 end
= address
+ size
;
599 if (end
> PGDIR_SIZE
)
602 pte_t
* pte
= pte_alloc(pmd
, address
);
605 zeromap_pte_range(pte
, address
, end
- address
, prot
);
606 address
= (address
+ PMD_SIZE
) & PMD_MASK
;
608 } while (address
< end
);
612 int zeromap_page_range(unsigned long address
, unsigned long size
, pgprot_t prot
)
616 unsigned long beg
= address
;
617 unsigned long end
= address
+ size
;
619 dir
= pgd_offset(current
->mm
, address
);
620 flush_cache_range(current
->mm
, beg
, end
);
621 while (address
< end
) {
622 pmd_t
*pmd
= pmd_alloc(dir
, address
);
626 error
= zeromap_pmd_range(pmd
, address
, end
- address
, prot
);
629 address
= (address
+ PGDIR_SIZE
) & PGDIR_MASK
;
632 flush_tlb_range(current
->mm
, beg
, end
);
637 * maps a range of physical memory into the requested pages. the old
638 * mappings are removed. any references to nonexistent pages results
639 * in null mappings (currently treated as "copy-on-access")
641 static inline void remap_pte_range(pte_t
* pte
, unsigned long address
, unsigned long size
,
642 unsigned long phys_addr
, pgprot_t prot
)
646 address
&= ~PMD_MASK
;
647 end
= address
+ size
;
652 pte_t oldpage
= *pte
;
655 mapnr
= MAP_NR(__va(phys_addr
));
656 if (mapnr
>= max_mapnr
|| PageReserved(mem_map
+mapnr
))
657 set_pte(pte
, mk_pte_phys(phys_addr
, prot
));
659 address
+= PAGE_SIZE
;
660 phys_addr
+= PAGE_SIZE
;
662 } while (address
< end
);
665 static inline int remap_pmd_range(pmd_t
* pmd
, unsigned long address
, unsigned long size
,
666 unsigned long phys_addr
, pgprot_t prot
)
670 address
&= ~PGDIR_MASK
;
671 end
= address
+ size
;
672 if (end
> PGDIR_SIZE
)
674 phys_addr
-= address
;
676 pte_t
* pte
= pte_alloc(pmd
, address
);
679 remap_pte_range(pte
, address
, end
- address
, address
+ phys_addr
, prot
);
680 address
= (address
+ PMD_SIZE
) & PMD_MASK
;
682 } while (address
< end
);
686 int remap_page_range(unsigned long from
, unsigned long phys_addr
, unsigned long size
, pgprot_t prot
)
690 unsigned long beg
= from
;
691 unsigned long end
= from
+ size
;
694 dir
= pgd_offset(current
->mm
, from
);
695 flush_cache_range(current
->mm
, beg
, end
);
697 pmd_t
*pmd
= pmd_alloc(dir
, from
);
701 error
= remap_pmd_range(pmd
, from
, end
- from
, phys_addr
+ from
, prot
);
704 from
= (from
+ PGDIR_SIZE
) & PGDIR_MASK
;
707 flush_tlb_range(current
->mm
, beg
, end
);
712 * This routine is used to map in a page into an address space: needed by
713 * execve() for the initial stack and environment pages.
715 unsigned long put_dirty_page(struct task_struct
* tsk
, unsigned long page
, unsigned long address
)
721 if (MAP_NR(page
) >= max_mapnr
)
722 printk("put_dirty_page: trying to put page %08lx at %08lx\n",page
,address
);
723 if (page_count(mem_map
+ MAP_NR(page
)) != 1)
724 printk("mem_map disagrees with %08lx at %08lx\n",page
,address
);
725 pgd
= pgd_offset(tsk
->mm
,address
);
726 pmd
= pmd_alloc(pgd
, address
);
732 pte
= pte_alloc(pmd
, address
);
738 if (!pte_none(*pte
)) {
739 printk("put_dirty_page: pte %08lx already exists\n",
744 flush_page_to_ram(page
);
745 set_pte(pte
, pte_mkwrite(pte_mkdirty(mk_pte(page
, PAGE_COPY
))));
746 /* no need for flush_tlb */
751 * This routine handles present pages, when users try to write
752 * to a shared page. It is done by copying the page to a new address
753 * and decrementing the shared-page counter for the old page.
755 * Goto-purists beware: the only reason for goto's here is that it results
756 * in better assembly code.. The "default" path will see no jumps at all.
758 * Note that this routine assumes that the protection checks have been
759 * done by the caller (the low-level page fault routine in most cases).
760 * Thus we can safely just mark it writable once we've done any necessary
763 * We also mark the page dirty at this point even though the page will
764 * change only once the write actually happens. This avoids a few races,
765 * and potentially makes it more efficient.
767 * We enter with the page table read-lock held, and need to exit without
770 static int do_wp_page(struct task_struct
* tsk
, struct vm_area_struct
* vma
,
771 unsigned long address
, pte_t
*page_table
, pte_t pte
)
773 unsigned long old_page
, new_page
;
776 old_page
= pte_page(pte
);
777 if (MAP_NR(old_page
) >= max_mapnr
)
780 page
= mem_map
+ MAP_NR(old_page
);
783 * We can avoid the copy if:
784 * - we're the only user (count == 1)
785 * - the only other user is the swap cache,
786 * and the only swap cache user is itself,
787 * in which case we can remove the page
788 * from the swap cache.
790 switch (page_count(page
)) {
792 if (!PageSwapCache(page
))
794 if (swap_count(page
->offset
) != 1)
796 delete_from_swap_cache(page
);
799 flush_cache_page(vma
, address
);
800 set_pte(page_table
, pte_mkyoung(pte_mkdirty(pte_mkwrite(pte
))));
801 flush_tlb_page(vma
, address
);
802 spin_unlock(&tsk
->mm
->page_table_lock
);
807 * Ok, we need to copy. Oh, well..
809 spin_unlock(&tsk
->mm
->page_table_lock
);
810 new_page
= __get_free_page(GFP_USER
);
813 spin_lock(&tsk
->mm
->page_table_lock
);
816 * Re-check the pte - we dropped the lock
818 if (pte_val(*page_table
) == pte_val(pte
)) {
819 if (PageReserved(page
))
821 copy_cow_page(old_page
,new_page
);
822 flush_page_to_ram(old_page
);
823 flush_page_to_ram(new_page
);
824 flush_cache_page(vma
, address
);
825 set_pte(page_table
, pte_mkwrite(pte_mkdirty(mk_pte(new_page
, vma
->vm_page_prot
))));
826 flush_tlb_page(vma
, address
);
828 /* Free the old page.. */
831 spin_unlock(&tsk
->mm
->page_table_lock
);
836 spin_unlock(&tsk
->mm
->page_table_lock
);
837 printk("do_wp_page: bogus page at address %08lx (%08lx)\n",address
,old_page
);
842 * This function zeroes out partial mmap'ed pages at truncation time..
844 static void partial_clear(struct vm_area_struct
*vma
, unsigned long address
)
848 pte_t
*page_table
, pte
;
850 page_dir
= pgd_offset(vma
->vm_mm
, address
);
851 if (pgd_none(*page_dir
))
853 if (pgd_bad(*page_dir
)) {
854 printk("bad page table directory entry %p:[%lx]\n", page_dir
, pgd_val(*page_dir
));
858 page_middle
= pmd_offset(page_dir
, address
);
859 if (pmd_none(*page_middle
))
861 if (pmd_bad(*page_middle
)) {
862 printk("bad page table directory entry %p:[%lx]\n", page_dir
, pgd_val(*page_dir
));
863 pmd_clear(page_middle
);
866 page_table
= pte_offset(page_middle
, address
);
868 if (!pte_present(pte
))
870 flush_cache_page(vma
, address
);
871 address
&= ~PAGE_MASK
;
872 address
+= pte_page(pte
);
873 if (MAP_NR(address
) >= max_mapnr
)
875 memset((void *) address
, 0, PAGE_SIZE
- (address
& ~PAGE_MASK
));
876 flush_page_to_ram(pte_page(pte
));
880 * Handle all mappings that got truncated by a "truncate()"
883 * NOTE! We have to be ready to update the memory sharing
884 * between the file and the memory map for a potential last
885 * incomplete page. Ugly, but necessary.
887 void vmtruncate(struct inode
* inode
, unsigned long offset
)
889 struct vm_area_struct
* mpnt
;
891 truncate_inode_pages(inode
, offset
);
892 spin_lock(&inode
->i_shared_lock
);
895 mpnt
= inode
->i_mmap
;
897 struct mm_struct
*mm
= mpnt
->vm_mm
;
898 unsigned long start
= mpnt
->vm_start
;
899 unsigned long end
= mpnt
->vm_end
;
900 unsigned long len
= end
- start
;
903 /* mapping wholly truncated? */
904 if (mpnt
->vm_offset
>= offset
) {
905 flush_cache_range(mm
, start
, end
);
906 zap_page_range(mm
, start
, len
);
907 flush_tlb_range(mm
, start
, end
);
910 /* mapping wholly unaffected? */
911 diff
= offset
- mpnt
->vm_offset
;
914 /* Ok, partially affected.. */
916 len
= (len
- diff
) & PAGE_MASK
;
917 if (start
& ~PAGE_MASK
) {
918 partial_clear(mpnt
, start
);
919 start
= (start
+ ~PAGE_MASK
) & PAGE_MASK
;
921 flush_cache_range(mm
, start
, end
);
922 zap_page_range(mm
, start
, len
);
923 flush_tlb_range(mm
, start
, end
);
924 } while ((mpnt
= mpnt
->vm_next_share
) != NULL
);
926 spin_unlock(&inode
->i_shared_lock
);
932 * Primitive swap readahead code. We simply read an aligned block of
933 * (1 << page_cluster) entries in the swap area. This method is chosen
934 * because it doesn't cost us any seek time. We also make sure to queue
935 * the 'original' request together with the readahead ones...
937 void swapin_readahead(unsigned long entry
)
940 struct page
*new_page
;
941 unsigned long offset
= SWP_OFFSET(entry
);
942 struct swap_info_struct
*swapdev
= SWP_TYPE(entry
) + swap_info
;
944 offset
= (offset
>> page_cluster
) << page_cluster
;
946 i
= 1 << page_cluster
;
948 /* Don't read-ahead past the end of the swap area */
949 if (offset
>= swapdev
->max
)
951 /* Don't block on I/O for read-ahead */
952 if (atomic_read(&nr_async_pages
) >= pager_daemon
.swap_cluster
)
954 /* Don't read in bad or busy pages */
955 if (!swapdev
->swap_map
[offset
])
957 if (swapdev
->swap_map
[offset
] == SWAP_MAP_BAD
)
960 /* Ok, do the async read-ahead now */
961 new_page
= read_swap_cache_async(SWP_ENTRY(SWP_TYPE(entry
), offset
), 0);
962 if (new_page
!= NULL
)
963 __free_page(new_page
);
969 static int do_swap_page(struct task_struct
* tsk
,
970 struct vm_area_struct
* vma
, unsigned long address
,
971 pte_t
* page_table
, unsigned long entry
, int write_access
)
973 struct page
*page
= lookup_swap_cache(entry
);
978 swapin_readahead(entry
);
979 page
= read_swap_cache(entry
);
984 flush_page_to_ram(page_address(page
));
993 pte
= mk_pte(page_address(page
), vma
->vm_page_prot
);
995 if (write_access
&& !is_page_shared(page
)) {
996 delete_from_swap_cache(page
);
997 pte
= pte_mkwrite(pte_mkdirty(pte
));
999 set_pte(page_table
, pte
);
1000 /* No need to invalidate - it was non-present before */
1001 update_mmu_cache(vma
, address
, pte
);
1006 * This only needs the MM semaphore
1008 static int do_anonymous_page(struct task_struct
* tsk
, struct vm_area_struct
* vma
, pte_t
*page_table
, int write_access
, unsigned long addr
)
1010 pte_t entry
= pte_wrprotect(mk_pte(ZERO_PAGE(addr
), vma
->vm_page_prot
));
1012 unsigned long page
= __get_free_page(GFP_USER
);
1016 entry
= pte_mkwrite(pte_mkdirty(mk_pte(page
, vma
->vm_page_prot
)));
1019 flush_page_to_ram(page
);
1021 set_pte(page_table
, entry
);
1022 /* No need to invalidate - it was non-present before */
1023 update_mmu_cache(vma
, addr
, entry
);
1028 * do_no_page() tries to create a new page mapping. It aggressively
1029 * tries to share with existing pages, but makes a separate copy if
1030 * the "write_access" parameter is true in order to avoid the next
1033 * As this is called only for pages that do not currently exist, we
1034 * do not need to flush old virtual caches or the TLB.
1036 * This is called with the MM semaphore and the kernel lock held.
1037 * We need to release the kernel lock as soon as possible..
1039 static int do_no_page(struct task_struct
* tsk
, struct vm_area_struct
* vma
,
1040 unsigned long address
, int write_access
, pte_t
*page_table
)
1045 if (!vma
->vm_ops
|| !vma
->vm_ops
->nopage
)
1046 return do_anonymous_page(tsk
, vma
, page_table
, write_access
, address
);
1049 * The third argument is "no_share", which tells the low-level code
1050 * to copy, not share the page even if sharing is possible. It's
1051 * essentially an early COW detection.
1053 page
= vma
->vm_ops
->nopage(vma
, address
& PAGE_MASK
, (vma
->vm_flags
& VM_SHARED
)?0:write_access
);
1055 return 0; /* SIGBUS - but we _really_ should know whether it is OOM or SIGBUS */
1057 return -1; /* OOM */
1062 * This silly early PAGE_DIRTY setting removes a race
1063 * due to the bad i386 page protection. But it's valid
1064 * for other architectures too.
1066 * Note that if write_access is true, we either now have
1067 * an exclusive copy of the page, or this is a shared mapping,
1068 * so we can make it writable and dirty to avoid having to
1069 * handle that later.
1071 flush_page_to_ram(page
);
1072 entry
= mk_pte(page
, vma
->vm_page_prot
);
1074 entry
= pte_mkwrite(pte_mkdirty(entry
));
1075 } else if (page_count(mem_map
+MAP_NR(page
)) > 1 &&
1076 !(vma
->vm_flags
& VM_SHARED
))
1077 entry
= pte_wrprotect(entry
);
1078 set_pte(page_table
, entry
);
1079 /* no need to invalidate: a not-present page shouldn't be cached */
1080 update_mmu_cache(vma
, address
, entry
);
1085 * These routines also need to handle stuff like marking pages dirty
1086 * and/or accessed for architectures that don't do it in hardware (most
1087 * RISC architectures). The early dirtying is also good on the i386.
1089 * There is also a hook called "update_mmu_cache()" that architectures
1090 * with external mmu caches can use to update those (ie the Sparc or
1091 * PowerPC hashed page tables that act as extended TLBs).
1093 * Note the "page_table_lock". It is to protect against kswapd removing
1094 * pages from under us. Note that kswapd only ever _removes_ pages, never
1095 * adds them. As such, once we have noticed that the page is not present,
1096 * we can drop the lock early.
1098 * The adding of pages is protected by the MM semaphore (which we hold),
1099 * so we don't need to worry about a page being suddenly been added into
1102 static inline int handle_pte_fault(struct task_struct
*tsk
,
1103 struct vm_area_struct
* vma
, unsigned long address
,
1104 int write_access
, pte_t
* pte
)
1109 if (!pte_present(entry
)) {
1110 if (pte_none(entry
))
1111 return do_no_page(tsk
, vma
, address
, write_access
, pte
);
1112 return do_swap_page(tsk
, vma
, address
, pte
, pte_val(entry
), write_access
);
1116 * Ok, the entry was present, we need to get the page table
1117 * lock to synchronize with kswapd, and verify that the entry
1118 * didn't change from under us..
1120 spin_lock(&tsk
->mm
->page_table_lock
);
1121 if (pte_val(entry
) == pte_val(*pte
)) {
1123 if (!pte_write(entry
))
1124 return do_wp_page(tsk
, vma
, address
, pte
, entry
);
1126 entry
= pte_mkdirty(entry
);
1128 entry
= pte_mkyoung(entry
);
1129 set_pte(pte
, entry
);
1130 flush_tlb_page(vma
, address
);
1131 update_mmu_cache(vma
, address
, entry
);
1133 spin_unlock(&tsk
->mm
->page_table_lock
);
1138 * By the time we get here, we already hold the mm semaphore
1140 int handle_mm_fault(struct task_struct
*tsk
, struct vm_area_struct
* vma
,
1141 unsigned long address
, int write_access
)
1146 pgd
= pgd_offset(vma
->vm_mm
, address
);
1147 pmd
= pmd_alloc(pgd
, address
);
1149 pte_t
* pte
= pte_alloc(pmd
, address
);
1151 return handle_pte_fault(tsk
, vma
, address
, write_access
, pte
);
1157 * Simplistic page force-in..
1159 int make_pages_present(unsigned long addr
, unsigned long end
)
1162 struct task_struct
*tsk
= current
;
1163 struct vm_area_struct
* vma
;
1165 vma
= find_vma(tsk
->mm
, addr
);
1166 write
= (vma
->vm_flags
& VM_WRITE
) != 0;
1167 while (addr
< end
) {
1168 if (handle_mm_fault(tsk
, vma
, addr
, write
) < 0)