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 <linux/highmem.h>
46 #include <linux/pagemap.h>
47 #include <linux/module.h>
49 #include <asm/pgalloc.h>
50 #include <asm/uaccess.h>
53 unsigned long max_mapnr;
54 unsigned long num_physpages;
55 unsigned long num_mappedpages;
57 struct page *highmem_start_page;
60 * We special-case the C-O-W ZERO_PAGE, because it's such
61 * a common occurrence (no need to read the page to know
62 * that it's zero - better for the cache and memory subsystem).
64 static inline void copy_cow_page(struct page * from, struct page * to, unsigned long address)
66 if (from == ZERO_PAGE(address)) {
67 clear_user_highpage(to, address);
70 copy_user_highpage(to, from, address);
76 * Called by TLB shootdown
78 void __free_pte(pte_t pte)
80 struct page *page = pte_page(pte);
81 if ((!VALID_PAGE(page)) || PageReserved(page))
85 free_page_and_swap_cache(page);
90 * Note: this doesn't free the actual pages themselves. That
91 * has been handled earlier when unmapping all the memory regions.
93 static inline void free_one_pmd(pmd_t * dir)
104 pte = pte_offset(dir, 0);
109 static inline void free_one_pgd(pgd_t * dir)
121 pmd = pmd_offset(dir, 0);
123 for (j = 0; j < PTRS_PER_PMD ; j++) {
124 prefetchw(pmd+j+(PREFETCH_STRIDE/16));
130 /* Low and high watermarks for page table cache.
131 The system should try to have pgt_water[0] <= cache elements <= pgt_water[1]
133 int pgt_cache_water[2] = { 25, 50 };
135 /* Returns the number of pages freed */
136 int check_pgt_cache(void)
138 return do_check_pgt_cache(pgt_cache_water[0], pgt_cache_water[1]);
143 * This function clears all user-level page tables of a process - this
144 * is needed by execve(), so that old pages aren't in the way.
146 void clear_page_tables(struct mm_struct *mm, unsigned long first, int nr)
148 pgd_t * page_dir = mm->pgd;
150 spin_lock(&mm->page_table_lock);
153 free_one_pgd(page_dir);
156 spin_unlock(&mm->page_table_lock);
158 /* keep the page table cache within bounds */
162 #define PTE_TABLE_MASK ((PTRS_PER_PTE-1) * sizeof(pte_t))
163 #define PMD_TABLE_MASK ((PTRS_PER_PMD-1) * sizeof(pmd_t))
166 * copy one vm_area from one task to the other. Assumes the page tables
167 * already present in the new task to be cleared in the whole range
168 * covered by this vma.
170 * 08Jan98 Merged into one routine from several inline routines to reduce
171 * variable count and make things faster. -jj
173 * dst->page_table_lock is held on entry and exit,
174 * but may be dropped within pmd_alloc() and pte_alloc().
176 int copy_page_range(struct mm_struct *dst, struct mm_struct *src,
177 struct vm_area_struct *vma)
179 pgd_t * src_pgd, * dst_pgd;
180 unsigned long address = vma->vm_start;
181 unsigned long end = vma->vm_end;
182 unsigned long cow = (vma->vm_flags & (VM_SHARED | VM_MAYWRITE)) == VM_MAYWRITE;
184 src_pgd = pgd_offset(src, address)-1;
185 dst_pgd = pgd_offset(dst, address)-1;
188 pmd_t * src_pmd, * dst_pmd;
190 src_pgd++; dst_pgd++;
194 if (pgd_none(*src_pgd))
195 goto skip_copy_pmd_range;
196 if (pgd_bad(*src_pgd)) {
199 skip_copy_pmd_range: address = (address + PGDIR_SIZE) & PGDIR_MASK;
200 if (!address || (address >= end))
205 src_pmd = pmd_offset(src_pgd, address);
206 dst_pmd = pmd_alloc(dst, dst_pgd, address);
211 pte_t * src_pte, * dst_pte;
215 if (pmd_none(*src_pmd))
216 goto skip_copy_pte_range;
217 if (pmd_bad(*src_pmd)) {
220 skip_copy_pte_range: address = (address + PMD_SIZE) & PMD_MASK;
223 goto cont_copy_pmd_range;
226 src_pte = pte_offset(src_pmd, address);
227 dst_pte = pte_alloc(dst, dst_pmd, address);
231 spin_lock(&src->page_table_lock);
233 pte_t pte = *src_pte;
234 struct page *ptepage;
239 goto cont_copy_pte_range_noset;
240 if (!pte_present(pte)) {
241 swap_duplicate(pte_to_swp_entry(pte));
242 goto cont_copy_pte_range;
244 ptepage = pte_page(pte);
245 if ((!VALID_PAGE(ptepage)) ||
246 PageReserved(ptepage))
247 goto cont_copy_pte_range;
249 /* If it's a COW mapping, write protect it both in the parent and the child */
250 if (cow && pte_write(pte)) {
251 ptep_set_wrprotect(src_pte);
255 /* If it's a shared mapping, mark it clean in the child */
256 if (vma->vm_flags & VM_SHARED)
257 pte = pte_mkclean(pte);
258 pte = pte_mkold(pte);
262 cont_copy_pte_range: set_pte(dst_pte, pte);
263 cont_copy_pte_range_noset: address += PAGE_SIZE;
268 } while ((unsigned long)src_pte & PTE_TABLE_MASK);
269 spin_unlock(&src->page_table_lock);
271 cont_copy_pmd_range: src_pmd++;
273 } while ((unsigned long)src_pmd & PMD_TABLE_MASK);
276 spin_unlock(&src->page_table_lock);
284 * Return indicates whether a page was freed so caller can adjust rss
286 static inline void forget_pte(pte_t page)
288 if (!pte_none(page)) {
289 printk("forget_pte: old mapping existed!\n");
294 static inline int zap_pte_range(mmu_gather_t *tlb, pmd_t * pmd, unsigned long address, unsigned long size)
296 unsigned long offset;
307 ptep = pte_offset(pmd, address);
308 offset = address & ~PMD_MASK;
309 if (offset + size > PMD_SIZE)
310 size = PMD_SIZE - offset;
312 for (offset=0; offset < size; ptep++, offset += PAGE_SIZE) {
316 if (pte_present(pte)) {
317 struct page *page = pte_page(pte);
318 if (VALID_PAGE(page) && !PageReserved(page))
320 /* This will eventually call __free_pte on the pte. */
321 tlb_remove_page(tlb, ptep, address + offset);
323 free_swap_and_cache(pte_to_swp_entry(pte));
331 static inline int zap_pmd_range(mmu_gather_t *tlb, pgd_t * dir, unsigned long address, unsigned long size)
344 pmd = pmd_offset(dir, address);
345 end = address + size;
346 if (end > ((address + PGDIR_SIZE) & PGDIR_MASK))
347 end = ((address + PGDIR_SIZE) & PGDIR_MASK);
350 freed += zap_pte_range(tlb, 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)
364 unsigned long start = address, end = address + size;
367 dir = pgd_offset(mm, address);
370 * This is a long-lived spinlock. That's fine.
371 * There's no contention, because the page table
372 * lock only protects against kswapd anyway, and
373 * even if kswapd happened to be looking at this
374 * process we _want_ it to get stuck.
378 spin_lock(&mm->page_table_lock);
379 flush_cache_range(mm, address, end);
380 tlb = tlb_gather_mmu(mm);
383 freed += zap_pmd_range(tlb, dir, address, end - address);
384 address = (address + PGDIR_SIZE) & PGDIR_MASK;
386 } while (address && (address < end));
388 /* this will flush any remaining tlb entries */
389 tlb_finish_mmu(tlb, start, end);
392 * Update rss for the mm_struct (not necessarily current->mm)
393 * Notice that rss is an unsigned long.
399 spin_unlock(&mm->page_table_lock);
403 * Do a quick page-table lookup for a single page.
405 static struct page * follow_page(struct mm_struct *mm, unsigned long address, int write)
411 pgd = pgd_offset(mm, address);
412 if (pgd_none(*pgd) || pgd_bad(*pgd))
415 pmd = pmd_offset(pgd, address);
416 if (pmd_none(*pmd) || pmd_bad(*pmd))
419 ptep = pte_offset(pmd, address);
424 if (pte_present(pte)) {
426 (pte_write(pte) && pte_dirty(pte)))
427 return pte_page(pte);
435 * Given a physical address, is there a useful struct page pointing to
436 * it? This may become more complex in the future if we start dealing
437 * with IO-aperture pages in kiobufs.
440 static inline struct page * get_page_map(struct page *page)
442 if (!VALID_PAGE(page))
448 * Please read Documentation/cachetlb.txt before using this function,
449 * accessing foreign memory spaces can cause cache coherency problems.
451 * Accessing a VM_IO area is even more dangerous, therefore the function
452 * fails if pages is != NULL and a VM_IO area is found.
454 int get_user_pages(struct task_struct *tsk, struct mm_struct *mm, unsigned long start,
455 int len, int write, int force, struct page **pages, struct vm_area_struct **vmas)
461 * Require read or write permissions.
462 * If 'force' is set, we only require the "MAY" flags.
464 flags = write ? (VM_WRITE | VM_MAYWRITE) : (VM_READ | VM_MAYREAD);
465 flags &= force ? (VM_MAYREAD | VM_MAYWRITE) : (VM_READ | VM_WRITE);
469 struct vm_area_struct * vma;
471 vma = find_extend_vma(mm, start);
473 if ( !vma || (pages && vma->vm_flags & VM_IO) || !(flags & vma->vm_flags) )
474 return i ? : -EFAULT;
476 spin_lock(&mm->page_table_lock);
479 while (!(map = follow_page(mm, start, write))) {
480 spin_unlock(&mm->page_table_lock);
481 switch (handle_mm_fault(mm, vma, start, write)) {
495 spin_lock(&mm->page_table_lock);
498 pages[i] = get_page_map(map);
499 /* FIXME: call the correct function,
500 * depending on the type of the found page
502 if (!pages[i] || PageReserved(pages[i])) {
503 if (pages[i] != ZERO_PAGE(start))
506 page_cache_get(pages[i]);
513 } while(len && start < vma->vm_end);
514 spin_unlock(&mm->page_table_lock);
520 * We found an invalid page in the VMA. Release all we have
524 spin_unlock(&mm->page_table_lock);
526 page_cache_release(pages[i]);
531 EXPORT_SYMBOL(get_user_pages);
534 * Force in an entire range of pages from the current process's user VA,
535 * and pin them in physical memory.
537 #define dprintk(x...)
539 int map_user_kiobuf(int rw, struct kiobuf *iobuf, unsigned long va, size_t len)
542 struct mm_struct * mm;
544 /* Make sure the iobuf is not already mapped somewhere. */
549 dprintk ("map_user_kiobuf: begin\n");
551 pgcount = (va + len + PAGE_SIZE - 1)/PAGE_SIZE - va/PAGE_SIZE;
552 /* mapping 0 bytes is not permitted */
554 err = expand_kiobuf(iobuf, pgcount);
559 iobuf->offset = va & (PAGE_SIZE-1);
562 /* Try to fault in all of the necessary pages */
563 down_read(&mm->mmap_sem);
564 /* rw==READ means read from disk, write into memory area */
565 err = get_user_pages(current, mm, va, pgcount,
566 (rw==READ), 0, iobuf->maplist, NULL);
567 up_read(&mm->mmap_sem);
570 dprintk ("map_user_kiobuf: end %d\n", err);
573 iobuf->nr_pages = err;
575 /* FIXME: flush superflous for rw==READ,
576 * probably wrong function for rw==WRITE
578 flush_dcache_page(iobuf->maplist[pgcount]);
580 dprintk ("map_user_kiobuf: end OK\n");
585 * Mark all of the pages in a kiobuf as dirty
587 * We need to be able to deal with short reads from disk: if an IO error
588 * occurs, the number of bytes read into memory may be less than the
589 * size of the kiobuf, so we have to stop marking pages dirty once the
590 * requested byte count has been reached.
592 * Must be called from process context - set_page_dirty() takes VFS locks.
595 void mark_dirty_kiobuf(struct kiobuf *iobuf, int bytes)
597 int index, offset, remaining;
600 index = iobuf->offset >> PAGE_SHIFT;
601 offset = iobuf->offset & ~PAGE_MASK;
603 if (remaining > iobuf->length)
604 remaining = iobuf->length;
606 while (remaining > 0 && index < iobuf->nr_pages) {
607 page = iobuf->maplist[index];
609 if (!PageReserved(page))
610 set_page_dirty(page);
612 remaining -= (PAGE_SIZE - offset);
619 * Unmap all of the pages referenced by a kiobuf. We release the pages,
620 * and unlock them if they were locked.
623 void unmap_kiobuf (struct kiobuf *iobuf)
628 for (i = 0; i < iobuf->nr_pages; i++) {
629 map = iobuf->maplist[i];
633 /* FIXME: cache flush missing for rw==READ
634 * FIXME: call the correct reference counting function
636 page_cache_release(map);
646 * Lock down all of the pages of a kiovec for IO.
648 * If any page is mapped twice in the kiovec, we return the error -EINVAL.
650 * The optional wait parameter causes the lock call to block until all
651 * pages can be locked if set. If wait==0, the lock operation is
652 * aborted if any locked pages are found and -EAGAIN is returned.
655 int lock_kiovec(int nr, struct kiobuf *iovec[], int wait)
657 struct kiobuf *iobuf;
659 struct page *page, **ppage;
665 for (i = 0; i < nr; i++) {
671 ppage = iobuf->maplist;
672 for (j = 0; j < iobuf->nr_pages; ppage++, j++) {
677 if (TryLockPage(page)) {
679 struct page *tmp = *--ppage;
694 * We couldn't lock one of the pages. Undo the locking so far,
695 * wait on the page we got to, and try again.
698 unlock_kiovec(nr, iovec);
703 * Did the release also unlock the page we got stuck on?
705 if (!PageLocked(page)) {
707 * If so, we may well have the page mapped twice
708 * in the IO address range. Bad news. Of
709 * course, it _might_ just be a coincidence,
710 * but if it happens more than once, chances
711 * are we have a double-mapped page.
713 if (++doublepage >= 3)
726 * Unlock all of the pages of a kiovec after IO.
729 int unlock_kiovec(int nr, struct kiobuf *iovec[])
731 struct kiobuf *iobuf;
733 struct page *page, **ppage;
735 for (i = 0; i < nr; i++) {
742 ppage = iobuf->maplist;
743 for (j = 0; j < iobuf->nr_pages; ppage++, j++) {
753 static inline void zeromap_pte_range(pte_t * pte, unsigned long address,
754 unsigned long size, pgprot_t prot)
758 address &= ~PMD_MASK;
759 end = address + size;
763 pte_t zero_pte = pte_wrprotect(mk_pte(ZERO_PAGE(address), prot));
764 pte_t oldpage = ptep_get_and_clear(pte);
765 set_pte(pte, zero_pte);
767 address += PAGE_SIZE;
769 } while (address && (address < end));
772 static inline int zeromap_pmd_range(struct mm_struct *mm, pmd_t * pmd, unsigned long address,
773 unsigned long size, pgprot_t prot)
777 address &= ~PGDIR_MASK;
778 end = address + size;
779 if (end > PGDIR_SIZE)
782 pte_t * pte = pte_alloc(mm, pmd, address);
785 zeromap_pte_range(pte, address, end - address, prot);
786 address = (address + PMD_SIZE) & PMD_MASK;
788 } while (address && (address < end));
792 int zeromap_page_range(unsigned long address, unsigned long size, pgprot_t prot)
796 unsigned long beg = address;
797 unsigned long end = address + size;
798 struct mm_struct *mm = current->mm;
800 dir = pgd_offset(mm, address);
801 flush_cache_range(mm, beg, end);
805 spin_lock(&mm->page_table_lock);
807 pmd_t *pmd = pmd_alloc(mm, dir, address);
811 error = zeromap_pmd_range(mm, pmd, address, end - address, prot);
814 address = (address + PGDIR_SIZE) & PGDIR_MASK;
816 } while (address && (address < end));
817 spin_unlock(&mm->page_table_lock);
818 flush_tlb_range(mm, beg, end);
823 * maps a range of physical memory into the requested pages. the old
824 * mappings are removed. any references to nonexistent pages results
825 * in null mappings (currently treated as "copy-on-access")
827 static inline void remap_pte_range(pte_t * pte, unsigned long address, unsigned long size,
828 unsigned long phys_addr, pgprot_t prot)
832 address &= ~PMD_MASK;
833 end = address + size;
839 oldpage = ptep_get_and_clear(pte);
841 page = virt_to_page(__va(phys_addr));
842 if ((!VALID_PAGE(page)) || PageReserved(page))
843 set_pte(pte, mk_pte_phys(phys_addr, prot));
845 address += PAGE_SIZE;
846 phys_addr += PAGE_SIZE;
848 } while (address && (address < end));
851 static inline int remap_pmd_range(struct mm_struct *mm, pmd_t * pmd, unsigned long address, unsigned long size,
852 unsigned long phys_addr, pgprot_t prot)
856 address &= ~PGDIR_MASK;
857 end = address + size;
858 if (end > PGDIR_SIZE)
860 phys_addr -= address;
862 pte_t * pte = pte_alloc(mm, pmd, address);
865 remap_pte_range(pte, address, end - address, address + phys_addr, prot);
866 address = (address + PMD_SIZE) & PMD_MASK;
868 } while (address && (address < end));
872 /* Note: this is only safe if the mm semaphore is held when called. */
873 int remap_page_range(unsigned long from, unsigned long phys_addr, unsigned long size, pgprot_t prot)
877 unsigned long beg = from;
878 unsigned long end = from + size;
879 struct mm_struct *mm = current->mm;
882 dir = pgd_offset(mm, from);
883 flush_cache_range(mm, beg, end);
887 spin_lock(&mm->page_table_lock);
889 pmd_t *pmd = pmd_alloc(mm, dir, from);
893 error = remap_pmd_range(mm, pmd, from, end - from, phys_addr + from, prot);
896 from = (from + PGDIR_SIZE) & PGDIR_MASK;
898 } while (from && (from < end));
899 spin_unlock(&mm->page_table_lock);
900 flush_tlb_range(mm, beg, end);
905 * Establish a new mapping:
906 * - flush the old one
907 * - update the page tables
908 * - inform the TLB about the new one
910 * We hold the mm semaphore for reading and vma->vm_mm->page_table_lock
912 static inline void establish_pte(struct vm_area_struct * vma, unsigned long address, pte_t *page_table, pte_t entry)
914 set_pte(page_table, entry);
915 flush_tlb_page(vma, address);
916 update_mmu_cache(vma, address, entry);
920 * We hold the mm semaphore for reading and vma->vm_mm->page_table_lock
922 static inline void break_cow(struct vm_area_struct * vma, struct page * new_page, unsigned long address,
925 flush_page_to_ram(new_page);
926 flush_cache_page(vma, address);
927 establish_pte(vma, address, page_table, pte_mkwrite(pte_mkdirty(mk_pte(new_page, vma->vm_page_prot))));
931 * This routine handles present pages, when users try to write
932 * to a shared page. It is done by copying the page to a new address
933 * and decrementing the shared-page counter for the old page.
935 * Goto-purists beware: the only reason for goto's here is that it results
936 * in better assembly code.. The "default" path will see no jumps at all.
938 * Note that this routine assumes that the protection checks have been
939 * done by the caller (the low-level page fault routine in most cases).
940 * Thus we can safely just mark it writable once we've done any necessary
943 * We also mark the page dirty at this point even though the page will
944 * change only once the write actually happens. This avoids a few races,
945 * and potentially makes it more efficient.
947 * We hold the mm semaphore and the page_table_lock on entry and exit
948 * with the page_table_lock released.
950 static int do_wp_page(struct mm_struct *mm, struct vm_area_struct * vma,
951 unsigned long address, pte_t *page_table, pte_t pte)
953 struct page *old_page, *new_page;
955 old_page = pte_page(pte);
956 if (!VALID_PAGE(old_page))
959 if (!TryLockPage(old_page)) {
960 int reuse = can_share_swap_page(old_page);
961 unlock_page(old_page);
963 flush_cache_page(vma, address);
964 establish_pte(vma, address, page_table, pte_mkyoung(pte_mkdirty(pte_mkwrite(pte))));
965 spin_unlock(&mm->page_table_lock);
966 return 1; /* Minor fault */
971 * Ok, we need to copy. Oh, well..
973 page_cache_get(old_page);
974 spin_unlock(&mm->page_table_lock);
976 new_page = alloc_page(GFP_HIGHUSER);
979 copy_cow_page(old_page,new_page,address);
982 * Re-check the pte - we dropped the lock
984 spin_lock(&mm->page_table_lock);
985 if (pte_same(*page_table, pte)) {
986 if (PageReserved(old_page))
988 break_cow(vma, new_page, address, page_table);
990 lru_cache_add(new_page);
992 /* Free the old page.. */
995 spin_unlock(&mm->page_table_lock);
996 page_cache_release(new_page);
997 page_cache_release(old_page);
998 return 1; /* Minor fault */
1001 spin_unlock(&mm->page_table_lock);
1002 printk("do_wp_page: bogus page at address %08lx (page 0x%lx)\n",address,(unsigned long)old_page);
1005 page_cache_release(old_page);
1009 static void vmtruncate_list(struct vm_area_struct *mpnt, unsigned long pgoff)
1012 struct mm_struct *mm = mpnt->vm_mm;
1013 unsigned long start = mpnt->vm_start;
1014 unsigned long end = mpnt->vm_end;
1015 unsigned long len = end - start;
1018 /* mapping wholly truncated? */
1019 if (mpnt->vm_pgoff >= pgoff) {
1020 zap_page_range(mm, start, len);
1024 /* mapping wholly unaffected? */
1025 len = len >> PAGE_SHIFT;
1026 diff = pgoff - mpnt->vm_pgoff;
1030 /* Ok, partially affected.. */
1031 start += diff << PAGE_SHIFT;
1032 len = (len - diff) << PAGE_SHIFT;
1033 zap_page_range(mm, start, len);
1034 } while ((mpnt = mpnt->vm_next_share) != NULL);
1038 * Handle all mappings that got truncated by a "truncate()"
1041 * NOTE! We have to be ready to update the memory sharing
1042 * between the file and the memory map for a potential last
1043 * incomplete page. Ugly, but necessary.
1045 int vmtruncate(struct inode * inode, loff_t offset)
1047 unsigned long pgoff;
1048 struct address_space *mapping = inode->i_mapping;
1049 unsigned long limit;
1051 if (inode->i_size < offset)
1053 inode->i_size = offset;
1054 spin_lock(&mapping->i_shared_lock);
1055 if (!mapping->i_mmap && !mapping->i_mmap_shared)
1058 pgoff = (offset + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
1059 if (mapping->i_mmap != NULL)
1060 vmtruncate_list(mapping->i_mmap, pgoff);
1061 if (mapping->i_mmap_shared != NULL)
1062 vmtruncate_list(mapping->i_mmap_shared, pgoff);
1065 spin_unlock(&mapping->i_shared_lock);
1066 truncate_inode_pages(mapping, offset);
1070 limit = current->rlim[RLIMIT_FSIZE].rlim_cur;
1071 if (limit != RLIM_INFINITY && offset > limit)
1073 if (offset > inode->i_sb->s_maxbytes)
1075 inode->i_size = offset;
1078 if (inode->i_op && inode->i_op->truncate) {
1080 inode->i_op->truncate(inode);
1085 send_sig(SIGXFSZ, current, 0);
1091 * Primitive swap readahead code. We simply read an aligned block of
1092 * (1 << page_cluster) entries in the swap area. This method is chosen
1093 * because it doesn't cost us any seek time. We also make sure to queue
1094 * the 'original' request together with the readahead ones...
1096 void swapin_readahead(swp_entry_t entry)
1099 struct page *new_page;
1100 unsigned long offset;
1103 * Get the number of handles we should do readahead io to.
1105 num = valid_swaphandles(entry, &offset);
1106 for (i = 0; i < num; offset++, i++) {
1107 /* Ok, do the async read-ahead now */
1108 new_page = read_swap_cache_async(SWP_ENTRY(SWP_TYPE(entry), offset));
1111 page_cache_release(new_page);
1117 * We hold the mm semaphore and the page_table_lock on entry and
1118 * should release the pagetable lock on exit..
1120 static int do_swap_page(struct mm_struct * mm,
1121 struct vm_area_struct * vma, unsigned long address,
1122 pte_t * page_table, pte_t orig_pte, int write_access)
1125 swp_entry_t entry = pte_to_swp_entry(orig_pte);
1129 spin_unlock(&mm->page_table_lock);
1130 page = lookup_swap_cache(entry);
1132 swapin_readahead(entry);
1133 page = read_swap_cache_async(entry);
1136 * Back out if somebody else faulted in this pte while
1137 * we released the page table lock.
1140 spin_lock(&mm->page_table_lock);
1141 retval = pte_same(*page_table, orig_pte) ? -1 : 1;
1142 spin_unlock(&mm->page_table_lock);
1146 /* Had to read the page from swap area: Major fault */
1150 mark_page_accessed(page);
1155 * Back out if somebody else faulted in this pte while we
1156 * released the page table lock.
1158 spin_lock(&mm->page_table_lock);
1159 if (!pte_same(*page_table, orig_pte)) {
1160 spin_unlock(&mm->page_table_lock);
1162 page_cache_release(page);
1166 /* The page isn't present yet, go ahead with the fault. */
1170 remove_exclusive_swap_page(page);
1173 pte = mk_pte(page, vma->vm_page_prot);
1174 if (write_access && can_share_swap_page(page))
1175 pte = pte_mkdirty(pte_mkwrite(pte));
1178 flush_page_to_ram(page);
1179 flush_icache_page(vma, page);
1180 set_pte(page_table, pte);
1182 /* No need to invalidate - it was non-present before */
1183 update_mmu_cache(vma, address, pte);
1184 spin_unlock(&mm->page_table_lock);
1189 * We are called with the MM semaphore and page_table_lock
1190 * spinlock held to protect against concurrent faults in
1191 * multithreaded programs.
1193 static int do_anonymous_page(struct mm_struct * mm, struct vm_area_struct * vma, pte_t *page_table, int write_access, unsigned long addr)
1197 /* Read-only mapping of ZERO_PAGE. */
1198 entry = pte_wrprotect(mk_pte(ZERO_PAGE(addr), vma->vm_page_prot));
1200 /* ..except if it's a write access */
1204 /* Allocate our own private page. */
1205 spin_unlock(&mm->page_table_lock);
1207 page = alloc_page(GFP_HIGHUSER);
1210 clear_user_highpage(page, addr);
1212 spin_lock(&mm->page_table_lock);
1213 if (!pte_none(*page_table)) {
1214 page_cache_release(page);
1215 spin_unlock(&mm->page_table_lock);
1219 flush_page_to_ram(page);
1220 entry = pte_mkwrite(pte_mkdirty(mk_pte(page, vma->vm_page_prot)));
1222 lru_cache_add(page);
1223 mark_page_accessed(page);
1226 set_pte(page_table, entry);
1228 /* No need to invalidate - it was non-present before */
1229 update_mmu_cache(vma, addr, entry);
1230 spin_unlock(&mm->page_table_lock);
1231 return 1; /* Minor fault */
1238 * do_no_page() tries to create a new page mapping. It aggressively
1239 * tries to share with existing pages, but makes a separate copy if
1240 * the "write_access" parameter is true in order to avoid the next
1243 * As this is called only for pages that do not currently exist, we
1244 * do not need to flush old virtual caches or the TLB.
1246 * This is called with the MM semaphore held and the page table
1247 * spinlock held. Exit with the spinlock released.
1249 static int do_no_page(struct mm_struct * mm, struct vm_area_struct * vma,
1250 unsigned long address, int write_access, pte_t *page_table)
1252 struct page * new_page;
1255 if (!vma->vm_ops || !vma->vm_ops->nopage)
1256 return do_anonymous_page(mm, vma, page_table, write_access, address);
1257 spin_unlock(&mm->page_table_lock);
1259 new_page = vma->vm_ops->nopage(vma, address & PAGE_MASK, 0);
1261 if (new_page == NULL) /* no page was available -- SIGBUS */
1263 if (new_page == NOPAGE_OOM)
1267 * Should we do an early C-O-W break?
1269 if (write_access && !(vma->vm_flags & VM_SHARED)) {
1270 struct page * page = alloc_page(GFP_HIGHUSER);
1272 page_cache_release(new_page);
1275 copy_user_highpage(page, new_page, address);
1276 page_cache_release(new_page);
1278 lru_cache_add(page);
1282 spin_lock(&mm->page_table_lock);
1284 * This silly early PAGE_DIRTY setting removes a race
1285 * due to the bad i386 page protection. But it's valid
1286 * for other architectures too.
1288 * Note that if write_access is true, we either now have
1289 * an exclusive copy of the page, or this is a shared mapping,
1290 * so we can make it writable and dirty to avoid having to
1291 * handle that later.
1293 /* Only go through if we didn't race with anybody else... */
1294 if (pte_none(*page_table)) {
1295 if (!PageReserved(new_page))
1297 flush_page_to_ram(new_page);
1298 flush_icache_page(vma, new_page);
1299 entry = mk_pte(new_page, vma->vm_page_prot);
1301 entry = pte_mkwrite(pte_mkdirty(entry));
1302 set_pte(page_table, entry);
1304 /* One of our sibling threads was faster, back out. */
1305 page_cache_release(new_page);
1306 spin_unlock(&mm->page_table_lock);
1310 /* no need to invalidate: a not-present page shouldn't be cached */
1311 update_mmu_cache(vma, address, entry);
1312 spin_unlock(&mm->page_table_lock);
1313 return 2; /* Major fault */
1317 * These routines also need to handle stuff like marking pages dirty
1318 * and/or accessed for architectures that don't do it in hardware (most
1319 * RISC architectures). The early dirtying is also good on the i386.
1321 * There is also a hook called "update_mmu_cache()" that architectures
1322 * with external mmu caches can use to update those (ie the Sparc or
1323 * PowerPC hashed page tables that act as extended TLBs).
1325 * Note the "page_table_lock". It is to protect against kswapd removing
1326 * pages from under us. Note that kswapd only ever _removes_ pages, never
1327 * adds them. As such, once we have noticed that the page is not present,
1328 * we can drop the lock early.
1330 * The adding of pages is protected by the MM semaphore (which we hold),
1331 * so we don't need to worry about a page being suddenly been added into
1334 * We enter with the pagetable spinlock held, we are supposed to
1335 * release it when done.
1337 static inline int handle_pte_fault(struct mm_struct *mm,
1338 struct vm_area_struct * vma, unsigned long address,
1339 int write_access, pte_t * pte)
1344 if (!pte_present(entry)) {
1346 * If it truly wasn't present, we know that kswapd
1347 * and the PTE updates will not touch it later. So
1350 if (pte_none(entry))
1351 return do_no_page(mm, vma, address, write_access, pte);
1352 return do_swap_page(mm, vma, address, pte, entry, write_access);
1356 if (!pte_write(entry))
1357 return do_wp_page(mm, vma, address, pte, entry);
1359 entry = pte_mkdirty(entry);
1361 entry = pte_mkyoung(entry);
1362 establish_pte(vma, address, pte, entry);
1363 spin_unlock(&mm->page_table_lock);
1368 * By the time we get here, we already hold the mm semaphore
1370 int handle_mm_fault(struct mm_struct *mm, struct vm_area_struct * vma,
1371 unsigned long address, int write_access)
1376 current->state = TASK_RUNNING;
1377 pgd = pgd_offset(mm, address);
1380 * We need the page table lock to synchronize with kswapd
1381 * and the SMP-safe atomic PTE updates.
1383 spin_lock(&mm->page_table_lock);
1384 pmd = pmd_alloc(mm, pgd, address);
1387 pte_t * pte = pte_alloc(mm, pmd, address);
1389 return handle_pte_fault(mm, vma, address, write_access, pte);
1391 spin_unlock(&mm->page_table_lock);
1396 * Allocate page middle directory.
1398 * We've already handled the fast-path in-line, and we own the
1401 * On a two-level page table, this ends up actually being entirely
1404 pmd_t fastcall *__pmd_alloc(struct mm_struct *mm, pgd_t *pgd, unsigned long address)
1408 /* "fast" allocation can happen without dropping the lock.. */
1409 new = pmd_alloc_one_fast(mm, address);
1411 spin_unlock(&mm->page_table_lock);
1412 new = pmd_alloc_one(mm, address);
1413 spin_lock(&mm->page_table_lock);
1418 * Because we dropped the lock, we should re-check the
1419 * entry, as somebody else could have populated it..
1421 if (!pgd_none(*pgd)) {
1427 pgd_populate(mm, pgd, new);
1429 return pmd_offset(pgd, address);
1433 * Allocate the page table directory.
1435 * We've already handled the fast-path in-line, and we own the
1438 pte_t fastcall *pte_alloc(struct mm_struct *mm, pmd_t *pmd, unsigned long address)
1440 if (pmd_none(*pmd)) {
1443 /* "fast" allocation can happen without dropping the lock.. */
1444 new = pte_alloc_one_fast(mm, address);
1446 spin_unlock(&mm->page_table_lock);
1447 new = pte_alloc_one(mm, address);
1448 spin_lock(&mm->page_table_lock);
1453 * Because we dropped the lock, we should re-check the
1454 * entry, as somebody else could have populated it..
1456 if (!pmd_none(*pmd)) {
1462 pmd_populate(mm, pmd, new);
1465 return pte_offset(pmd, address);
1468 int make_pages_present(unsigned long addr, unsigned long end)
1470 int ret, len, write;
1471 struct vm_area_struct * vma;
1473 vma = find_vma(current->mm, addr);
1474 write = (vma->vm_flags & VM_WRITE) != 0;
1477 if (end > vma->vm_end)
1479 len = (end+PAGE_SIZE-1)/PAGE_SIZE-addr/PAGE_SIZE;
1480 ret = get_user_pages(current, current->mm, addr,
1481 len, write, 0, NULL, NULL);
1482 return ret == len ? 0 : -1;
1485 struct page * vmalloc_to_page(void * vmalloc_addr)
1487 unsigned long addr = (unsigned long) vmalloc_addr;
1488 struct page *page = NULL;
1493 pgd = pgd_offset_k(addr);
1494 if (!pgd_none(*pgd)) {
1495 pmd = pmd_offset(pgd, addr);
1496 if (!pmd_none(*pmd)) {
1497 pte = pte_offset(pmd, addr);
1498 if (pte_present(*pte)) {
1499 page = pte_page(*pte);