4 * Copyright (C) 1991, 1992, 1993, 1994 Linus Torvalds
8 * demand-loading started 01.12.91 - seems it is high on the list of
9 * things wanted, and it should be easy to implement. - Linus
13 * Ok, demand-loading was easy, shared pages a little bit tricker. Shared
14 * pages started 02.12.91, seems to work. - Linus.
16 * Tested sharing by executing about 30 /bin/sh: under the old kernel it
17 * would have taken more than the 6M I have free, but it worked well as
20 * Also corrected some "invalidate()"s - I wasn't doing enough of them.
24 * Real VM (paging to/from disk) started 18.12.91. Much more work and
25 * thought has to go into this. Oh, well..
26 * 19.12.91 - works, somewhat. Sometimes I get faults, don't know why.
27 * Found it. Everything seems to work now.
28 * 20.12.91 - Ok, making the swap-device changeable like the root.
32 * 05.04.94 - Multi-page memory management added for v1.1.
33 * Idea by Alex Bligh (alex@cconcepts.co.uk)
35 * 16.07.99 - Support of BIGMEM added by Gerhard Wichert, Siemens AG
36 * (Gerhard.Wichert@pdb.siemens.de)
39 #include <linux/kernel_stat.h>
41 #include <linux/hugetlb.h>
42 #include <linux/mman.h>
43 #include <linux/swap.h>
44 #include <linux/highmem.h>
45 #include <linux/pagemap.h>
46 #include <linux/rmap.h>
47 #include <linux/module.h>
48 #include <linux/init.h>
50 #include <asm/pgalloc.h>
51 #include <asm/uaccess.h>
53 #include <asm/tlbflush.h>
54 #include <asm/pgtable.h>
56 #include <linux/swapops.h>
57 #include <linux/elf.h>
59 #ifndef CONFIG_DISCONTIGMEM
60 /* use the per-pgdat data instead for discontigmem - mbligh */
61 unsigned long max_mapnr;
64 EXPORT_SYMBOL(max_mapnr);
65 EXPORT_SYMBOL(mem_map);
68 unsigned long num_physpages;
70 * A number of key systems in x86 including ioremap() rely on the assumption
71 * that high_memory defines the upper bound on direct map memory, then end
72 * of ZONE_NORMAL. Under CONFIG_DISCONTIG this means that max_low_pfn and
73 * highstart_pfn must be the same; there must be no gap between ZONE_NORMAL
77 struct page *highmem_start_page;
78 unsigned long vmalloc_earlyreserve;
80 EXPORT_SYMBOL(num_physpages);
81 EXPORT_SYMBOL(highmem_start_page);
82 EXPORT_SYMBOL(high_memory);
83 EXPORT_SYMBOL(vmalloc_earlyreserve);
86 * We special-case the C-O-W ZERO_PAGE, because it's such
87 * a common occurrence (no need to read the page to know
88 * that it's zero - better for the cache and memory subsystem).
90 static inline void copy_cow_page(struct page * from, struct page * to, unsigned long address)
92 if (from == ZERO_PAGE(address)) {
93 clear_user_highpage(to, address);
96 copy_user_highpage(to, from, address);
100 * Note: this doesn't free the actual pages themselves. That
101 * has been handled earlier when unmapping all the memory regions.
103 static inline void free_one_pmd(struct mmu_gather *tlb, pmd_t * dir)
109 if (unlikely(pmd_bad(*dir))) {
114 page = pmd_page(*dir);
116 dec_page_state(nr_page_table_pages);
117 pte_free_tlb(tlb, page);
120 static inline void free_one_pgd(struct mmu_gather *tlb, pgd_t * dir,
128 if (unlikely(pgd_bad(*dir))) {
133 pmd = pmd_offset(dir, 0);
135 for (j = 0; j < PTRS_PER_PMD ; j++) {
136 if (pgd_idx * PGDIR_SIZE + j * PMD_SIZE >= TASK_SIZE)
138 free_one_pmd(tlb, pmd+j);
140 pmd_free_tlb(tlb, pmd);
144 * This function clears all user-level page tables of a process - this
145 * is needed by execve(), so that old pages aren't in the way.
147 * Must be called with pagetable lock held.
149 void clear_page_tables(struct mmu_gather *tlb, unsigned long first, int nr)
151 pgd_t * page_dir = tlb->mm->pgd;
156 free_one_pgd(tlb, page_dir, pgd_idx);
162 pte_t fastcall * pte_alloc_map(struct mm_struct *mm, pmd_t *pmd, unsigned long address)
164 if (!pmd_present(*pmd)) {
167 spin_unlock(&mm->page_table_lock);
168 new = pte_alloc_one(mm, address);
169 spin_lock(&mm->page_table_lock);
174 * Because we dropped the lock, we should re-check the
175 * entry, as somebody else could have populated it..
177 if (pmd_present(*pmd)) {
181 inc_page_state(nr_page_table_pages);
182 pmd_populate(mm, pmd, new);
185 return pte_offset_map(pmd, address);
188 pte_t fastcall * pte_alloc_kernel(struct mm_struct *mm, pmd_t *pmd, unsigned long address)
190 if (!pmd_present(*pmd)) {
193 spin_unlock(&mm->page_table_lock);
194 new = pte_alloc_one_kernel(mm, address);
195 spin_lock(&mm->page_table_lock);
200 * Because we dropped the lock, we should re-check the
201 * entry, as somebody else could have populated it..
203 if (pmd_present(*pmd)) {
204 pte_free_kernel(new);
207 pmd_populate_kernel(mm, pmd, new);
210 return pte_offset_kernel(pmd, address);
212 #define PTE_TABLE_MASK ((PTRS_PER_PTE-1) * sizeof(pte_t))
213 #define PMD_TABLE_MASK ((PTRS_PER_PMD-1) * sizeof(pmd_t))
216 * copy one vm_area from one task to the other. Assumes the page tables
217 * already present in the new task to be cleared in the whole range
218 * covered by this vma.
220 * 08Jan98 Merged into one routine from several inline routines to reduce
221 * variable count and make things faster. -jj
223 * dst->page_table_lock is held on entry and exit,
224 * but may be dropped within pmd_alloc() and pte_alloc_map().
226 int copy_page_range(struct mm_struct *dst, struct mm_struct *src,
227 struct vm_area_struct *vma)
229 pgd_t * src_pgd, * dst_pgd;
230 unsigned long address = vma->vm_start;
231 unsigned long end = vma->vm_end;
234 if (is_vm_hugetlb_page(vma))
235 return copy_hugetlb_page_range(dst, src, vma);
237 cow = (vma->vm_flags & (VM_SHARED | VM_MAYWRITE)) == VM_MAYWRITE;
238 src_pgd = pgd_offset(src, address)-1;
239 dst_pgd = pgd_offset(dst, address)-1;
242 pmd_t * src_pmd, * dst_pmd;
244 src_pgd++; dst_pgd++;
248 if (pgd_none(*src_pgd))
249 goto skip_copy_pmd_range;
250 if (unlikely(pgd_bad(*src_pgd))) {
253 skip_copy_pmd_range: address = (address + PGDIR_SIZE) & PGDIR_MASK;
254 if (!address || (address >= end))
259 src_pmd = pmd_offset(src_pgd, address);
260 dst_pmd = pmd_alloc(dst, dst_pgd, address);
265 pte_t * src_pte, * dst_pte;
269 if (pmd_none(*src_pmd))
270 goto skip_copy_pte_range;
271 if (unlikely(pmd_bad(*src_pmd))) {
275 address = (address + PMD_SIZE) & PMD_MASK;
278 goto cont_copy_pmd_range;
281 dst_pte = pte_alloc_map(dst, dst_pmd, address);
284 spin_lock(&src->page_table_lock);
285 src_pte = pte_offset_map_nested(src_pmd, address);
287 pte_t pte = *src_pte;
294 goto cont_copy_pte_range_noset;
295 /* pte contains position in swap, so copy. */
296 if (!pte_present(pte)) {
298 swap_duplicate(pte_to_swp_entry(pte));
299 set_pte(dst_pte, pte);
300 goto cont_copy_pte_range_noset;
303 /* the pte points outside of valid memory, the
304 * mapping is assumed to be good, meaningful
305 * and not mapped via rmap - duplicate the
310 page = pfn_to_page(pfn);
312 if (!page || PageReserved(page)) {
313 set_pte(dst_pte, pte);
314 goto cont_copy_pte_range_noset;
318 * If it's a COW mapping, write protect it both
319 * in the parent and the child
322 ptep_set_wrprotect(src_pte);
327 * If it's a shared mapping, mark it clean in
330 if (vma->vm_flags & VM_SHARED)
331 pte = pte_mkclean(pte);
332 pte = pte_mkold(pte);
335 set_pte(dst_pte, pte);
337 cont_copy_pte_range_noset:
338 address += PAGE_SIZE;
339 if (address >= end) {
340 pte_unmap_nested(src_pte);
346 } while ((unsigned long)src_pte & PTE_TABLE_MASK);
347 pte_unmap_nested(src_pte-1);
348 pte_unmap(dst_pte-1);
349 spin_unlock(&src->page_table_lock);
350 cond_resched_lock(&dst->page_table_lock);
354 } while ((unsigned long)src_pmd & PMD_TABLE_MASK);
357 spin_unlock(&src->page_table_lock);
364 static void zap_pte_range(struct mmu_gather *tlb,
365 pmd_t *pmd, unsigned long address,
366 unsigned long size, struct zap_details *details)
368 unsigned long offset;
373 if (unlikely(pmd_bad(*pmd))) {
378 ptep = pte_offset_map(pmd, address);
379 offset = address & ~PMD_MASK;
380 if (offset + size > PMD_SIZE)
381 size = PMD_SIZE - offset;
383 if (details && !details->check_mapping && !details->nonlinear_vma)
385 for (offset=0; offset < size; ptep++, offset += PAGE_SIZE) {
389 if (pte_present(pte)) {
390 struct page *page = NULL;
391 unsigned long pfn = pte_pfn(pte);
392 if (pfn_valid(pfn)) {
393 page = pfn_to_page(pfn);
394 if (PageReserved(page))
397 if (unlikely(details) && page) {
399 * unmap_shared_mapping_pages() wants to
400 * invalidate cache without truncating:
401 * unmap shared but keep private pages.
403 if (details->check_mapping &&
404 details->check_mapping != page->mapping)
407 * Each page->index must be checked when
408 * invalidating or truncating nonlinear.
410 if (details->nonlinear_vma &&
411 (page->index < details->first_index ||
412 page->index > details->last_index))
415 pte = ptep_get_and_clear(ptep);
416 tlb_remove_tlb_entry(tlb, ptep, address+offset);
419 if (unlikely(details) && details->nonlinear_vma
420 && linear_page_index(details->nonlinear_vma,
421 address+offset) != page->index)
422 set_pte(ptep, pgoff_to_pte(page->index));
424 set_page_dirty(page);
425 if (pte_young(pte) && !PageAnon(page))
426 mark_page_accessed(page);
428 page_remove_rmap(page);
429 tlb_remove_page(tlb, page);
433 * If details->check_mapping, we leave swap entries;
434 * if details->nonlinear_vma, we leave file entries.
436 if (unlikely(details))
439 free_swap_and_cache(pte_to_swp_entry(pte));
445 static void zap_pmd_range(struct mmu_gather *tlb,
446 pgd_t * dir, unsigned long address,
447 unsigned long size, struct zap_details *details)
450 unsigned long end, pgd_boundary;
454 if (unlikely(pgd_bad(*dir))) {
459 pmd = pmd_offset(dir, address);
460 end = address + size;
461 pgd_boundary = ((address + PGDIR_SIZE) & PGDIR_MASK);
462 if (pgd_boundary && (end > pgd_boundary))
465 zap_pte_range(tlb, pmd, address, end - address, details);
466 address = (address + PMD_SIZE) & PMD_MASK;
468 } while (address && (address < end));
471 static void unmap_page_range(struct mmu_gather *tlb,
472 struct vm_area_struct *vma, unsigned long address,
473 unsigned long end, struct zap_details *details)
477 BUG_ON(address >= end);
478 dir = pgd_offset(vma->vm_mm, address);
479 tlb_start_vma(tlb, vma);
481 zap_pmd_range(tlb, dir, address, end - address, details);
482 address = (address + PGDIR_SIZE) & PGDIR_MASK;
484 } while (address && (address < end));
485 tlb_end_vma(tlb, vma);
488 #ifdef CONFIG_PREEMPT_VOLUNTARY
489 # define ZAP_BLOCK_SIZE (128 * PAGE_SIZE)
492 /* Dispose of an entire struct mmu_gather per rescheduling point */
493 #if defined(CONFIG_SMP) && defined(CONFIG_PREEMPT)
494 #define ZAP_BLOCK_SIZE (FREE_PTE_NR * PAGE_SIZE)
497 /* For UP, 256 pages at a time gives nice low latency */
498 #if !defined(CONFIG_SMP) && defined(CONFIG_PREEMPT)
499 #define ZAP_BLOCK_SIZE (256 * PAGE_SIZE)
502 /* No preempt: go for improved straight-line efficiency */
503 #if !defined(CONFIG_PREEMPT)
504 #define ZAP_BLOCK_SIZE (1024 * PAGE_SIZE)
510 * unmap_vmas - unmap a range of memory covered by a list of vma's
511 * @tlbp: address of the caller's struct mmu_gather
512 * @mm: the controlling mm_struct
513 * @vma: the starting vma
514 * @start_addr: virtual address at which to start unmapping
515 * @end_addr: virtual address at which to end unmapping
516 * @nr_accounted: Place number of unmapped pages in vm-accountable vma's here
517 * @details: details of nonlinear truncation or shared cache invalidation
519 * Returns the number of vma's which were covered by the unmapping.
521 * Unmap all pages in the vma list. Called under page_table_lock.
523 * We aim to not hold page_table_lock for too long (for scheduling latency
524 * reasons). So zap pages in ZAP_BLOCK_SIZE bytecounts. This means we need to
525 * return the ending mmu_gather to the caller.
527 * Only addresses between `start' and `end' will be unmapped.
529 * The VMA list must be sorted in ascending virtual address order.
531 * unmap_vmas() assumes that the caller will flush the whole unmapped address
532 * range after unmap_vmas() returns. So the only responsibility here is to
533 * ensure that any thus-far unmapped pages are flushed before unmap_vmas()
534 * drops the lock and schedules.
536 int unmap_vmas(struct mmu_gather **tlbp, struct mm_struct *mm,
537 struct vm_area_struct *vma, unsigned long start_addr,
538 unsigned long end_addr, unsigned long *nr_accounted,
539 struct zap_details *details)
541 unsigned long zap_bytes = ZAP_BLOCK_SIZE;
542 unsigned long tlb_start = 0; /* For tlb_finish_mmu */
543 int tlb_start_valid = 0;
545 int atomic = details && details->atomic;
547 for ( ; vma && vma->vm_start < end_addr; vma = vma->vm_next) {
551 start = max(vma->vm_start, start_addr);
552 if (start >= vma->vm_end)
554 end = min(vma->vm_end, end_addr);
555 if (end <= vma->vm_start)
558 if (vma->vm_flags & VM_ACCOUNT)
559 *nr_accounted += (end - start) >> PAGE_SHIFT;
562 while (start != end) {
565 if (!tlb_start_valid) {
570 if (is_vm_hugetlb_page(vma)) {
572 unmap_hugepage_range(vma, start, end);
574 block = min(zap_bytes, end - start);
575 unmap_page_range(*tlbp, vma, start,
576 start + block, details);
581 if (!atomic && need_resched()) {
582 int fullmm = tlb_is_full_mm(*tlbp);
583 tlb_finish_mmu(*tlbp, tlb_start, start);
584 cond_resched_lock(&mm->page_table_lock);
585 *tlbp = tlb_gather_mmu(mm, fullmm);
588 if ((long)zap_bytes > 0)
590 zap_bytes = ZAP_BLOCK_SIZE;
597 * zap_page_range - remove user pages in a given range
598 * @vma: vm_area_struct holding the applicable pages
599 * @address: starting address of pages to zap
600 * @size: number of bytes to zap
601 * @details: details of nonlinear truncation or shared cache invalidation
603 void zap_page_range(struct vm_area_struct *vma, unsigned long address,
604 unsigned long size, struct zap_details *details)
606 struct mm_struct *mm = vma->vm_mm;
607 struct mmu_gather *tlb;
608 unsigned long end = address + size;
609 unsigned long nr_accounted = 0;
611 if (is_vm_hugetlb_page(vma)) {
612 zap_hugepage_range(vma, address, size);
617 spin_lock(&mm->page_table_lock);
618 tlb = tlb_gather_mmu(mm, 0);
619 unmap_vmas(&tlb, mm, vma, address, end, &nr_accounted, details);
620 tlb_finish_mmu(tlb, address, end);
621 spin_unlock(&mm->page_table_lock);
625 * Do a quick page-table lookup for a single page.
626 * mm->page_table_lock must be held.
629 follow_page(struct mm_struct *mm, unsigned long address, int write)
637 page = follow_huge_addr(mm, address, write);
641 pgd = pgd_offset(mm, address);
642 if (pgd_none(*pgd) || unlikely(pgd_bad(*pgd)))
645 pmd = pmd_offset(pgd, address);
649 return follow_huge_pmd(mm, address, pmd, write);
650 if (unlikely(pmd_bad(*pmd)))
653 ptep = pte_offset_map(pmd, address);
659 if (pte_present(pte)) {
660 if (write && !pte_write(pte))
663 if (pfn_valid(pfn)) {
664 page = pfn_to_page(pfn);
665 if (write && !pte_dirty(pte) && !PageDirty(page))
666 set_page_dirty(page);
667 mark_page_accessed(page);
677 follow_page_pfn(struct mm_struct *mm, unsigned long address, int write,
678 unsigned long *pfn_ptr)
687 page = follow_huge_addr(mm, address, write);
691 pgd = pgd_offset(mm, address);
692 if (pgd_none(*pgd) || pgd_bad(*pgd))
695 pmd = pmd_offset(pgd, address);
699 return follow_huge_pmd(mm, address, pmd, write);
703 ptep = pte_offset_map(pmd, address);
709 if (pte_present(pte)) {
710 if (write && !pte_write(pte))
712 if (write && !pte_dirty(pte)) {
713 struct page *page = pte_page(pte);
714 if (!PageDirty(page))
715 set_page_dirty(page);
718 if (pfn_valid(pfn)) {
719 struct page *page = pfn_to_page(pfn);
721 mark_page_accessed(page);
735 * Given a physical address, is there a useful struct page pointing to
736 * it? This may become more complex in the future if we start dealing
737 * with IO-aperture pages for direct-IO.
740 static inline struct page *get_page_map(struct page *page)
742 if (!pfn_valid(page_to_pfn(page)))
748 #ifndef CONFIG_X86_4G
750 untouched_anonymous_page(struct mm_struct* mm, struct vm_area_struct *vma,
751 unsigned long address)
756 /* Check if the vma is for an anonymous mapping. */
757 if (vma->vm_ops && vma->vm_ops->nopage)
760 /* Check if page directory entry exists. */
761 pgd = pgd_offset(mm, address);
762 if (pgd_none(*pgd) || unlikely(pgd_bad(*pgd)))
765 /* Check if page middle directory entry exists. */
766 pmd = pmd_offset(pgd, address);
767 if (pmd_none(*pmd) || unlikely(pmd_bad(*pmd)))
770 /* There is a pte slot for 'address' in 'mm'. */
776 int get_user_pages(struct task_struct *tsk, struct mm_struct *mm,
777 unsigned long start, int len, int write, int force,
778 struct page **pages, struct vm_area_struct **vmas)
784 * Require read or write permissions.
785 * If 'force' is set, we only require the "MAY" flags.
787 flags = write ? (VM_WRITE | VM_MAYWRITE) : (VM_READ | VM_MAYREAD);
788 flags &= force ? (VM_MAYREAD | VM_MAYWRITE) : (VM_READ | VM_WRITE);
792 struct vm_area_struct * vma;
794 vma = find_extend_vma(mm, start);
795 if (!vma && in_gate_area(tsk, start)) {
796 unsigned long pg = start & PAGE_MASK;
797 struct vm_area_struct *gate_vma = get_gate_vma(tsk);
801 if (write) /* user gate pages are read-only */
802 return i ? : -EFAULT;
803 pgd = pgd_offset(mm, pg);
805 return i ? : -EFAULT;
806 pmd = pmd_offset(pgd, pg);
808 return i ? : -EFAULT;
809 pte = pte_offset_map(pmd, pg);
811 return i ? : -EFAULT;
812 if (!pte_present(*pte)) {
814 return i ? : -EFAULT;
817 pages[i] = pte_page(*pte);
829 if (!vma || (pages && (vma->vm_flags & VM_IO))
830 || !(flags & vma->vm_flags))
831 return i ? : -EFAULT;
833 if (is_vm_hugetlb_page(vma)) {
834 i = follow_hugetlb_page(mm, vma, pages, vmas,
838 spin_lock(&mm->page_table_lock);
841 int lookup_write = write;
842 while (!(map = follow_page(mm, start, lookup_write))) {
844 * Shortcut for anonymous pages. We don't want
845 * to force the creation of pages tables for
846 * insanly big anonymously mapped areas that
847 * nobody touched so far. This is important
848 * for doing a core dump for these mappings.
850 * disable this for 4:4 - it prevents
851 * follow_page() from ever seeing these pages.
853 * (The 'fix' is dubious anyway, there's
854 * nothing that this code avoids which couldnt
855 * be triggered from userspace anyway.)
857 #ifndef CONFIG_X86_4G
859 untouched_anonymous_page(mm,vma,start)) {
860 map = ZERO_PAGE(start);
864 spin_unlock(&mm->page_table_lock);
865 switch (handle_mm_fault(mm,vma,start,write)) {
872 case VM_FAULT_SIGBUS:
873 return i ? i : -EFAULT;
875 return i ? i : -ENOMEM;
880 * Now that we have performed a write fault
881 * and surely no longer have a shared page we
882 * shouldn't write, we shouldn't ignore an
883 * unwritable page in the page table if
884 * we are forcing write access.
886 lookup_write = write && !force;
887 spin_lock(&mm->page_table_lock);
890 pages[i] = get_page_map(map);
892 spin_unlock(&mm->page_table_lock);
894 page_cache_release(pages[i]);
898 flush_dcache_page(pages[i]);
899 if (!PageReserved(pages[i]))
900 page_cache_get(pages[i]);
907 } while(len && start < vma->vm_end);
908 spin_unlock(&mm->page_table_lock);
914 EXPORT_SYMBOL(get_user_pages);
916 static void zeromap_pte_range(pte_t * pte, unsigned long address,
917 unsigned long size, pgprot_t prot)
921 address &= ~PMD_MASK;
922 end = address + size;
926 pte_t zero_pte = pte_wrprotect(mk_pte(ZERO_PAGE(address), prot));
927 BUG_ON(!pte_none(*pte));
928 set_pte(pte, zero_pte);
929 address += PAGE_SIZE;
931 } while (address && (address < end));
934 static inline int zeromap_pmd_range(struct mm_struct *mm, pmd_t * pmd, unsigned long address,
935 unsigned long size, pgprot_t prot)
937 unsigned long base, end;
939 base = address & PGDIR_MASK;
940 address &= ~PGDIR_MASK;
941 end = address + size;
942 if (end > PGDIR_SIZE)
945 pte_t * pte = pte_alloc_map(mm, pmd, base + address);
948 zeromap_pte_range(pte, base + address, end - address, prot);
950 address = (address + PMD_SIZE) & PMD_MASK;
952 } while (address && (address < end));
956 int zeromap_page_range(struct vm_area_struct *vma, unsigned long address, unsigned long size, pgprot_t prot)
960 unsigned long beg = address;
961 unsigned long end = address + size;
962 struct mm_struct *mm = vma->vm_mm;
964 dir = pgd_offset(mm, address);
965 flush_cache_range(vma, beg, end);
969 spin_lock(&mm->page_table_lock);
971 pmd_t *pmd = pmd_alloc(mm, dir, address);
975 error = zeromap_pmd_range(mm, pmd, address, end - address, prot);
978 address = (address + PGDIR_SIZE) & PGDIR_MASK;
980 } while (address && (address < end));
982 * Why flush? zeromap_pte_range has a BUG_ON for !pte_none()
984 flush_tlb_range(vma, beg, end);
985 spin_unlock(&mm->page_table_lock);
990 * maps a range of physical memory into the requested pages. the old
991 * mappings are removed. any references to nonexistent pages results
992 * in null mappings (currently treated as "copy-on-access")
994 static inline void remap_pte_range(pte_t * pte, unsigned long address, unsigned long size,
995 unsigned long phys_addr, pgprot_t prot)
1000 address &= ~PMD_MASK;
1001 end = address + size;
1004 pfn = phys_addr >> PAGE_SHIFT;
1006 BUG_ON(!pte_none(*pte));
1007 if (!pfn_valid(pfn) || PageReserved(pfn_to_page(pfn)))
1008 set_pte(pte, pfn_pte(pfn, prot));
1009 address += PAGE_SIZE;
1012 } while (address && (address < end));
1015 static inline int remap_pmd_range(struct mm_struct *mm, pmd_t * pmd, unsigned long address, unsigned long size,
1016 unsigned long phys_addr, pgprot_t prot)
1018 unsigned long base, end;
1020 base = address & PGDIR_MASK;
1021 address &= ~PGDIR_MASK;
1022 end = address + size;
1023 if (end > PGDIR_SIZE)
1025 phys_addr -= address;
1027 pte_t * pte = pte_alloc_map(mm, pmd, base + address);
1030 remap_pte_range(pte, base + address, end - address, address + phys_addr, prot);
1032 address = (address + PMD_SIZE) & PMD_MASK;
1034 } while (address && (address < end));
1038 /* Note: this is only safe if the mm semaphore is held when called. */
1039 int remap_page_range(struct vm_area_struct *vma, unsigned long from, unsigned long phys_addr, unsigned long size, pgprot_t prot)
1043 unsigned long beg = from;
1044 unsigned long end = from + size;
1045 struct mm_struct *mm = vma->vm_mm;
1048 dir = pgd_offset(mm, from);
1049 flush_cache_range(vma, beg, end);
1053 spin_lock(&mm->page_table_lock);
1055 pmd_t *pmd = pmd_alloc(mm, dir, from);
1059 error = remap_pmd_range(mm, pmd, from, end - from, phys_addr + from, prot);
1062 from = (from + PGDIR_SIZE) & PGDIR_MASK;
1064 } while (from && (from < end));
1066 * Why flush? remap_pte_range has a BUG_ON for !pte_none()
1068 flush_tlb_range(vma, beg, end);
1069 spin_unlock(&mm->page_table_lock);
1073 EXPORT_SYMBOL(remap_page_range);
1076 * Do pte_mkwrite, but only if the vma says VM_WRITE. We do this when
1077 * servicing faults for write access. In the normal case, do always want
1078 * pte_mkwrite. But get_user_pages can cause write faults for mappings
1079 * that do not have writing enabled, when used by access_process_vm.
1081 static inline pte_t maybe_mkwrite(pte_t pte, struct vm_area_struct *vma)
1083 if (likely(vma->vm_flags & VM_WRITE))
1084 pte = pte_mkwrite(pte);
1089 * We hold the mm semaphore for reading and vma->vm_mm->page_table_lock
1091 static inline void break_cow(struct vm_area_struct * vma, struct page * new_page, unsigned long address,
1096 flush_cache_page(vma, address);
1097 entry = maybe_mkwrite(pte_mkdirty(mk_pte(new_page, vma->vm_page_prot)),
1099 ptep_establish(vma, address, page_table, entry);
1100 update_mmu_cache(vma, address, entry);
1104 * This routine handles present pages, when users try to write
1105 * to a shared page. It is done by copying the page to a new address
1106 * and decrementing the shared-page counter for the old page.
1108 * Goto-purists beware: the only reason for goto's here is that it results
1109 * in better assembly code.. The "default" path will see no jumps at all.
1111 * Note that this routine assumes that the protection checks have been
1112 * done by the caller (the low-level page fault routine in most cases).
1113 * Thus we can safely just mark it writable once we've done any necessary
1116 * We also mark the page dirty at this point even though the page will
1117 * change only once the write actually happens. This avoids a few races,
1118 * and potentially makes it more efficient.
1120 * We hold the mm semaphore and the page_table_lock on entry and exit
1121 * with the page_table_lock released.
1123 static int do_wp_page(struct mm_struct *mm, struct vm_area_struct * vma,
1124 unsigned long address, pte_t *page_table, pmd_t *pmd, pte_t pte)
1126 struct page *old_page, *new_page;
1127 unsigned long pfn = pte_pfn(pte);
1130 if (unlikely(!pfn_valid(pfn))) {
1132 * This should really halt the system so it can be debugged or
1133 * at least the kernel stops what it's doing before it corrupts
1134 * data, but for the moment just pretend this is OOM.
1136 pte_unmap(page_table);
1137 printk(KERN_ERR "do_wp_page: bogus page at address %08lx\n",
1139 spin_unlock(&mm->page_table_lock);
1140 return VM_FAULT_OOM;
1142 old_page = pfn_to_page(pfn);
1144 if (!TestSetPageLocked(old_page)) {
1145 int reuse = can_share_swap_page(old_page);
1146 unlock_page(old_page);
1148 flush_cache_page(vma, address);
1149 entry = maybe_mkwrite(pte_mkyoung(pte_mkdirty(pte)),
1151 ptep_set_access_flags(vma, address, page_table, entry, 1);
1152 update_mmu_cache(vma, address, entry);
1153 pte_unmap(page_table);
1154 spin_unlock(&mm->page_table_lock);
1155 return VM_FAULT_MINOR;
1158 pte_unmap(page_table);
1161 * Ok, we need to copy. Oh, well..
1163 if (!PageReserved(old_page))
1164 page_cache_get(old_page);
1165 spin_unlock(&mm->page_table_lock);
1167 if (unlikely(anon_vma_prepare(vma)))
1169 new_page = alloc_page_vma(GFP_HIGHUSER, vma, address);
1172 copy_cow_page(old_page,new_page,address);
1175 * Re-check the pte - we dropped the lock
1177 spin_lock(&mm->page_table_lock);
1178 page_table = pte_offset_map(pmd, address);
1179 if (likely(pte_same(*page_table, pte))) {
1180 if (PageReserved(old_page))
1183 page_remove_rmap(old_page);
1184 break_cow(vma, new_page, address, page_table);
1185 lru_cache_add_active(new_page);
1186 page_add_anon_rmap(new_page, vma, address);
1188 /* Free the old page.. */
1189 new_page = old_page;
1191 pte_unmap(page_table);
1192 page_cache_release(new_page);
1193 page_cache_release(old_page);
1194 spin_unlock(&mm->page_table_lock);
1195 return VM_FAULT_MINOR;
1198 page_cache_release(old_page);
1199 return VM_FAULT_OOM;
1203 * Helper function for unmap_mapping_range().
1205 static inline void unmap_mapping_range_list(struct prio_tree_root *root,
1206 struct zap_details *details)
1208 struct vm_area_struct *vma = NULL;
1209 struct prio_tree_iter iter;
1210 pgoff_t vba, vea, zba, zea;
1212 while ((vma = vma_prio_tree_next(vma, root, &iter,
1213 details->first_index, details->last_index)) != NULL) {
1214 vba = vma->vm_pgoff;
1215 vea = vba + ((vma->vm_end - vma->vm_start) >> PAGE_SHIFT) - 1;
1216 /* Assume for now that PAGE_CACHE_SHIFT == PAGE_SHIFT */
1217 zba = details->first_index;
1220 zea = details->last_index;
1224 ((zba - vba) << PAGE_SHIFT) + vma->vm_start,
1225 (zea - zba + 1) << PAGE_SHIFT, details);
1230 * unmap_mapping_range - unmap the portion of all mmaps
1231 * in the specified address_space corresponding to the specified
1232 * page range in the underlying file.
1233 * @address_space: the address space containing mmaps to be unmapped.
1234 * @holebegin: byte in first page to unmap, relative to the start of
1235 * the underlying file. This will be rounded down to a PAGE_SIZE
1236 * boundary. Note that this is different from vmtruncate(), which
1237 * must keep the partial page. In contrast, we must get rid of
1239 * @holelen: size of prospective hole in bytes. This will be rounded
1240 * up to a PAGE_SIZE boundary. A holelen of zero truncates to the
1242 * @even_cows: 1 when truncating a file, unmap even private COWed pages;
1243 * but 0 when invalidating pagecache, don't throw away private data.
1245 void unmap_mapping_range(struct address_space *mapping,
1246 loff_t const holebegin, loff_t const holelen, int even_cows)
1248 struct zap_details details;
1249 pgoff_t hba = holebegin >> PAGE_SHIFT;
1250 pgoff_t hlen = (holelen + PAGE_SIZE - 1) >> PAGE_SHIFT;
1252 /* Check for overflow. */
1253 if (sizeof(holelen) > sizeof(hlen)) {
1255 (holebegin + holelen + PAGE_SIZE - 1) >> PAGE_SHIFT;
1256 if (holeend & ~(long long)ULONG_MAX)
1257 hlen = ULONG_MAX - hba + 1;
1260 details.check_mapping = even_cows? NULL: mapping;
1261 details.nonlinear_vma = NULL;
1262 details.first_index = hba;
1263 details.last_index = hba + hlen - 1;
1264 details.atomic = 1; /* A spinlock is held */
1265 if (details.last_index < details.first_index)
1266 details.last_index = ULONG_MAX;
1268 spin_lock(&mapping->i_mmap_lock);
1269 /* Protect against page fault */
1270 atomic_inc(&mapping->truncate_count);
1272 if (unlikely(!prio_tree_empty(&mapping->i_mmap)))
1273 unmap_mapping_range_list(&mapping->i_mmap, &details);
1276 * In nonlinear VMAs there is no correspondence between virtual address
1277 * offset and file offset. So we must perform an exhaustive search
1278 * across *all* the pages in each nonlinear VMA, not just the pages
1279 * whose virtual address lies outside the file truncation point.
1281 if (unlikely(!list_empty(&mapping->i_mmap_nonlinear))) {
1282 struct vm_area_struct *vma;
1283 list_for_each_entry(vma, &mapping->i_mmap_nonlinear,
1284 shared.vm_set.list) {
1285 details.nonlinear_vma = vma;
1286 zap_page_range(vma, vma->vm_start,
1287 vma->vm_end - vma->vm_start, &details);
1290 spin_unlock(&mapping->i_mmap_lock);
1292 EXPORT_SYMBOL(unmap_mapping_range);
1295 * Handle all mappings that got truncated by a "truncate()"
1298 * NOTE! We have to be ready to update the memory sharing
1299 * between the file and the memory map for a potential last
1300 * incomplete page. Ugly, but necessary.
1302 int vmtruncate(struct inode * inode, loff_t offset)
1304 struct address_space *mapping = inode->i_mapping;
1305 unsigned long limit;
1307 if (inode->i_size < offset)
1310 * truncation of in-use swapfiles is disallowed - it would cause
1311 * subsequent swapout to scribble on the now-freed blocks.
1313 if (IS_SWAPFILE(inode))
1315 i_size_write(inode, offset);
1316 unmap_mapping_range(mapping, offset + PAGE_SIZE - 1, 0, 1);
1317 truncate_inode_pages(mapping, offset);
1321 limit = current->rlim[RLIMIT_FSIZE].rlim_cur;
1322 if (limit != RLIM_INFINITY && offset > limit)
1324 if (offset > inode->i_sb->s_maxbytes)
1326 i_size_write(inode, offset);
1329 if (inode->i_op && inode->i_op->truncate)
1330 inode->i_op->truncate(inode);
1333 send_sig(SIGXFSZ, current, 0);
1340 EXPORT_SYMBOL(vmtruncate);
1343 * Primitive swap readahead code. We simply read an aligned block of
1344 * (1 << page_cluster) entries in the swap area. This method is chosen
1345 * because it doesn't cost us any seek time. We also make sure to queue
1346 * the 'original' request together with the readahead ones...
1348 * This has been extended to use the NUMA policies from the mm triggering
1351 * Caller must hold down_read on the vma->vm_mm if vma is not NULL.
1353 void swapin_readahead(swp_entry_t entry, unsigned long addr,struct vm_area_struct *vma)
1356 struct vm_area_struct *next_vma = vma ? vma->vm_next : NULL;
1359 struct page *new_page;
1360 unsigned long offset;
1363 * Get the number of handles we should do readahead io to.
1365 num = valid_swaphandles(entry, &offset);
1366 for (i = 0; i < num; offset++, i++) {
1367 /* Ok, do the async read-ahead now */
1368 new_page = read_swap_cache_async(swp_entry(swp_type(entry),
1369 offset), vma, addr);
1372 page_cache_release(new_page);
1375 * Find the next applicable VMA for the NUMA policy.
1381 if (addr >= vma->vm_end) {
1383 next_vma = vma ? vma->vm_next : NULL;
1385 if (vma && addr < vma->vm_start)
1388 if (next_vma && addr >= next_vma->vm_start) {
1390 next_vma = vma->vm_next;
1395 lru_add_drain(); /* Push any new pages onto the LRU now */
1399 * We hold the mm semaphore and the page_table_lock on entry and
1400 * should release the pagetable lock on exit..
1402 static int do_swap_page(struct mm_struct * mm,
1403 struct vm_area_struct * vma, unsigned long address,
1404 pte_t *page_table, pmd_t *pmd, pte_t orig_pte, int write_access)
1407 swp_entry_t entry = pte_to_swp_entry(orig_pte);
1409 int ret = VM_FAULT_MINOR;
1411 pte_unmap(page_table);
1412 spin_unlock(&mm->page_table_lock);
1413 page = lookup_swap_cache(entry);
1415 swapin_readahead(entry, address, vma);
1416 page = read_swap_cache_async(entry, vma, address);
1419 * Back out if somebody else faulted in this pte while
1420 * we released the page table lock.
1422 spin_lock(&mm->page_table_lock);
1423 page_table = pte_offset_map(pmd, address);
1424 if (likely(pte_same(*page_table, orig_pte)))
1427 ret = VM_FAULT_MINOR;
1428 pte_unmap(page_table);
1429 spin_unlock(&mm->page_table_lock);
1433 /* Had to read the page from swap area: Major fault */
1434 ret = VM_FAULT_MAJOR;
1435 inc_page_state(pgmajfault);
1438 mark_page_accessed(page);
1442 * Back out if somebody else faulted in this pte while we
1443 * released the page table lock.
1445 spin_lock(&mm->page_table_lock);
1446 page_table = pte_offset_map(pmd, address);
1447 if (unlikely(!pte_same(*page_table, orig_pte))) {
1448 pte_unmap(page_table);
1449 spin_unlock(&mm->page_table_lock);
1451 page_cache_release(page);
1452 ret = VM_FAULT_MINOR;
1456 /* The page isn't present yet, go ahead with the fault. */
1460 remove_exclusive_swap_page(page);
1463 pte = mk_pte(page, vma->vm_page_prot);
1464 if (write_access && can_share_swap_page(page)) {
1465 pte = maybe_mkwrite(pte_mkdirty(pte), vma);
1470 flush_icache_page(vma, page);
1471 set_pte(page_table, pte);
1472 page_add_anon_rmap(page, vma, address);
1475 if (do_wp_page(mm, vma, address,
1476 page_table, pmd, pte) == VM_FAULT_OOM)
1481 /* No need to invalidate - it was non-present before */
1482 update_mmu_cache(vma, address, pte);
1483 pte_unmap(page_table);
1484 spin_unlock(&mm->page_table_lock);
1490 * We are called with the MM semaphore and page_table_lock
1491 * spinlock held to protect against concurrent faults in
1492 * multithreaded programs.
1495 do_anonymous_page(struct mm_struct *mm, struct vm_area_struct *vma,
1496 pte_t *page_table, pmd_t *pmd, int write_access,
1500 struct page * page = ZERO_PAGE(addr);
1502 /* Read-only mapping of ZERO_PAGE. */
1503 entry = pte_wrprotect(mk_pte(ZERO_PAGE(addr), vma->vm_page_prot));
1505 /* ..except if it's a write access */
1507 /* Allocate our own private page. */
1508 pte_unmap(page_table);
1509 spin_unlock(&mm->page_table_lock);
1511 if (unlikely(anon_vma_prepare(vma)))
1513 page = alloc_page_vma(GFP_HIGHUSER, vma, addr);
1516 clear_user_highpage(page, addr);
1518 spin_lock(&mm->page_table_lock);
1519 page_table = pte_offset_map(pmd, addr);
1521 if (!pte_none(*page_table)) {
1522 pte_unmap(page_table);
1523 page_cache_release(page);
1524 spin_unlock(&mm->page_table_lock);
1528 entry = maybe_mkwrite(pte_mkdirty(mk_pte(page,
1529 vma->vm_page_prot)),
1531 lru_cache_add_active(page);
1532 mark_page_accessed(page);
1533 page_add_anon_rmap(page, vma, addr);
1536 set_pte(page_table, entry);
1537 pte_unmap(page_table);
1539 /* No need to invalidate - it was non-present before */
1540 update_mmu_cache(vma, addr, entry);
1541 spin_unlock(&mm->page_table_lock);
1543 return VM_FAULT_MINOR;
1545 return VM_FAULT_OOM;
1549 * do_no_page() tries to create a new page mapping. It aggressively
1550 * tries to share with existing pages, but makes a separate copy if
1551 * the "write_access" parameter is true in order to avoid the next
1554 * As this is called only for pages that do not currently exist, we
1555 * do not need to flush old virtual caches or the TLB.
1557 * This is called with the MM semaphore held and the page table
1558 * spinlock held. Exit with the spinlock released.
1561 do_no_page(struct mm_struct *mm, struct vm_area_struct *vma,
1562 unsigned long address, int write_access, pte_t *page_table, pmd_t *pmd)
1564 struct page * new_page;
1565 struct address_space *mapping = NULL;
1568 int ret = VM_FAULT_MINOR;
1571 if (!vma->vm_ops || !vma->vm_ops->nopage)
1572 return do_anonymous_page(mm, vma, page_table,
1573 pmd, write_access, address);
1574 pte_unmap(page_table);
1575 spin_unlock(&mm->page_table_lock);
1578 mapping = vma->vm_file->f_mapping;
1579 sequence = atomic_read(&mapping->truncate_count);
1581 smp_rmb(); /* Prevent CPU from reordering lock-free ->nopage() */
1583 new_page = vma->vm_ops->nopage(vma, address & PAGE_MASK, &ret);
1585 /* no page was available -- either SIGBUS or OOM */
1586 if (new_page == NOPAGE_SIGBUS)
1587 return VM_FAULT_SIGBUS;
1588 if (new_page == NOPAGE_OOM)
1589 return VM_FAULT_OOM;
1592 * Should we do an early C-O-W break?
1594 if (write_access && !(vma->vm_flags & VM_SHARED)) {
1597 if (unlikely(anon_vma_prepare(vma)))
1599 page = alloc_page_vma(GFP_HIGHUSER, vma, address);
1602 copy_user_highpage(page, new_page, address);
1603 page_cache_release(new_page);
1608 spin_lock(&mm->page_table_lock);
1610 * For a file-backed vma, someone could have truncated or otherwise
1611 * invalidated this page. If unmap_mapping_range got called,
1612 * retry getting the page.
1615 (unlikely(sequence != atomic_read(&mapping->truncate_count)))) {
1616 sequence = atomic_read(&mapping->truncate_count);
1617 spin_unlock(&mm->page_table_lock);
1618 page_cache_release(new_page);
1621 page_table = pte_offset_map(pmd, address);
1624 * This silly early PAGE_DIRTY setting removes a race
1625 * due to the bad i386 page protection. But it's valid
1626 * for other architectures too.
1628 * Note that if write_access is true, we either now have
1629 * an exclusive copy of the page, or this is a shared mapping,
1630 * so we can make it writable and dirty to avoid having to
1631 * handle that later.
1633 /* Only go through if we didn't race with anybody else... */
1634 if (pte_none(*page_table)) {
1635 if (!PageReserved(new_page))
1637 flush_icache_page(vma, new_page);
1638 entry = mk_pte(new_page, vma->vm_page_prot);
1640 entry = maybe_mkwrite(pte_mkdirty(entry), vma);
1641 set_pte(page_table, entry);
1643 lru_cache_add_active(new_page);
1644 page_add_anon_rmap(new_page, vma, address);
1646 page_add_file_rmap(new_page);
1647 pte_unmap(page_table);
1649 /* One of our sibling threads was faster, back out. */
1650 pte_unmap(page_table);
1651 page_cache_release(new_page);
1652 spin_unlock(&mm->page_table_lock);
1656 /* no need to invalidate: a not-present page shouldn't be cached */
1657 update_mmu_cache(vma, address, entry);
1658 spin_unlock(&mm->page_table_lock);
1662 page_cache_release(new_page);
1668 * Fault of a previously existing named mapping. Repopulate the pte
1669 * from the encoded file_pte if possible. This enables swappable
1672 static int do_file_page(struct mm_struct * mm, struct vm_area_struct * vma,
1673 unsigned long address, int write_access, pte_t *pte, pmd_t *pmd)
1675 unsigned long pgoff;
1678 BUG_ON(!vma->vm_ops || !vma->vm_ops->nopage);
1680 * Fall back to the linear mapping if the fs does not support
1683 if (!vma->vm_ops || !vma->vm_ops->populate ||
1684 (write_access && !(vma->vm_flags & VM_SHARED))) {
1686 return do_no_page(mm, vma, address, write_access, pte, pmd);
1689 pgoff = pte_to_pgoff(*pte);
1692 spin_unlock(&mm->page_table_lock);
1694 err = vma->vm_ops->populate(vma, address & PAGE_MASK, PAGE_SIZE, vma->vm_page_prot, pgoff, 0);
1696 return VM_FAULT_OOM;
1698 return VM_FAULT_SIGBUS;
1699 return VM_FAULT_MAJOR;
1703 * These routines also need to handle stuff like marking pages dirty
1704 * and/or accessed for architectures that don't do it in hardware (most
1705 * RISC architectures). The early dirtying is also good on the i386.
1707 * There is also a hook called "update_mmu_cache()" that architectures
1708 * with external mmu caches can use to update those (ie the Sparc or
1709 * PowerPC hashed page tables that act as extended TLBs).
1711 * Note the "page_table_lock". It is to protect against kswapd removing
1712 * pages from under us. Note that kswapd only ever _removes_ pages, never
1713 * adds them. As such, once we have noticed that the page is not present,
1714 * we can drop the lock early.
1716 * The adding of pages is protected by the MM semaphore (which we hold),
1717 * so we don't need to worry about a page being suddenly been added into
1720 * We enter with the pagetable spinlock held, we are supposed to
1721 * release it when done.
1723 static inline int handle_pte_fault(struct mm_struct *mm,
1724 struct vm_area_struct * vma, unsigned long address,
1725 int write_access, pte_t *pte, pmd_t *pmd)
1730 if (!pte_present(entry)) {
1732 * If it truly wasn't present, we know that kswapd
1733 * and the PTE updates will not touch it later. So
1736 if (pte_none(entry))
1737 return do_no_page(mm, vma, address, write_access, pte, pmd);
1738 if (pte_file(entry))
1739 return do_file_page(mm, vma, address, write_access, pte, pmd);
1740 return do_swap_page(mm, vma, address, pte, pmd, entry, write_access);
1744 if (!pte_write(entry))
1745 return do_wp_page(mm, vma, address, pte, pmd, entry);
1747 entry = pte_mkdirty(entry);
1749 entry = pte_mkyoung(entry);
1750 ptep_set_access_flags(vma, address, pte, entry, write_access);
1751 update_mmu_cache(vma, address, entry);
1753 spin_unlock(&mm->page_table_lock);
1754 return VM_FAULT_MINOR;
1758 * By the time we get here, we already hold the mm semaphore
1760 int handle_mm_fault(struct mm_struct *mm, struct vm_area_struct * vma,
1761 unsigned long address, int write_access)
1766 __set_current_state(TASK_RUNNING);
1767 pgd = pgd_offset(mm, address);
1769 inc_page_state(pgfault);
1771 if (is_vm_hugetlb_page(vma))
1772 return VM_FAULT_SIGBUS; /* mapping truncation does this. */
1775 * We need the page table lock to synchronize with kswapd
1776 * and the SMP-safe atomic PTE updates.
1778 spin_lock(&mm->page_table_lock);
1779 pmd = pmd_alloc(mm, pgd, address);
1782 pte_t * pte = pte_alloc_map(mm, pmd, address);
1784 return handle_pte_fault(mm, vma, address, write_access, pte, pmd);
1786 spin_unlock(&mm->page_table_lock);
1787 return VM_FAULT_OOM;
1791 * Allocate page middle directory.
1793 * We've already handled the fast-path in-line, and we own the
1796 * On a two-level page table, this ends up actually being entirely
1799 pmd_t fastcall *__pmd_alloc(struct mm_struct *mm, pgd_t *pgd, unsigned long address)
1803 spin_unlock(&mm->page_table_lock);
1804 new = pmd_alloc_one(mm, address);
1805 spin_lock(&mm->page_table_lock);
1810 * Because we dropped the lock, we should re-check the
1811 * entry, as somebody else could have populated it..
1813 if (pgd_present(*pgd)) {
1817 pgd_populate(mm, pgd, new);
1819 return pmd_offset(pgd, address);
1822 int make_pages_present(unsigned long addr, unsigned long end)
1824 int ret, len, write;
1825 struct vm_area_struct * vma;
1827 vma = find_vma(current->mm, addr);
1828 write = (vma->vm_flags & VM_WRITE) != 0;
1831 if (end > vma->vm_end)
1833 len = (end+PAGE_SIZE-1)/PAGE_SIZE-addr/PAGE_SIZE;
1834 ret = get_user_pages(current, current->mm, addr,
1835 len, write, 0, NULL, NULL);
1838 return ret == len ? 0 : -1;
1842 * Map a vmalloc()-space virtual address to the physical page.
1844 struct page * vmalloc_to_page(void * vmalloc_addr)
1846 unsigned long addr = (unsigned long) vmalloc_addr;
1847 struct page *page = NULL;
1848 pgd_t *pgd = pgd_offset_k(addr);
1852 if (!pgd_none(*pgd)) {
1853 pmd = pmd_offset(pgd, addr);
1854 if (!pmd_none(*pmd)) {
1856 ptep = pte_offset_map(pmd, addr);
1858 if (pte_present(pte))
1859 page = pte_page(pte);
1867 EXPORT_SYMBOL(vmalloc_to_page);
1869 #if !defined(CONFIG_ARCH_GATE_AREA)
1871 #if defined(AT_SYSINFO_EHDR)
1872 struct vm_area_struct gate_vma;
1874 static int __init gate_vma_init(void)
1876 gate_vma.vm_mm = NULL;
1877 gate_vma.vm_start = FIXADDR_USER_START;
1878 gate_vma.vm_end = FIXADDR_USER_END;
1879 gate_vma.vm_page_prot = PAGE_READONLY;
1880 gate_vma.vm_flags = 0;
1883 __initcall(gate_vma_init);
1886 struct vm_area_struct *get_gate_vma(struct task_struct *tsk)
1888 #ifdef AT_SYSINFO_EHDR
1895 int in_gate_area(struct task_struct *task, unsigned long addr)
1897 #ifdef AT_SYSINFO_EHDR
1898 if ((addr >= FIXADDR_USER_START) && (addr < FIXADDR_USER_END))