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;
291 if (!vx_rsspages_avail(dst, 1)) {
292 spin_unlock(&src->page_table_lock);
298 goto cont_copy_pte_range_noset;
299 /* pte contains position in swap, so copy. */
300 if (!pte_present(pte)) {
302 swap_duplicate(pte_to_swp_entry(pte));
303 set_pte(dst_pte, pte);
304 goto cont_copy_pte_range_noset;
307 /* the pte points outside of valid memory, the
308 * mapping is assumed to be good, meaningful
309 * and not mapped via rmap - duplicate the
314 page = pfn_to_page(pfn);
316 if (!page || PageReserved(page)) {
317 set_pte(dst_pte, pte);
318 goto cont_copy_pte_range_noset;
322 * If it's a COW mapping, write protect it both
323 * in the parent and the child
326 ptep_set_wrprotect(src_pte);
331 * If it's a shared mapping, mark it clean in
334 if (vma->vm_flags & VM_SHARED)
335 pte = pte_mkclean(pte);
336 pte = pte_mkold(pte);
339 vx_rsspages_inc(dst);
340 set_pte(dst_pte, pte);
342 cont_copy_pte_range_noset:
343 address += PAGE_SIZE;
344 if (address >= end) {
345 pte_unmap_nested(src_pte);
351 } while ((unsigned long)src_pte & PTE_TABLE_MASK);
352 pte_unmap_nested(src_pte-1);
353 pte_unmap(dst_pte-1);
354 spin_unlock(&src->page_table_lock);
355 cond_resched_lock(&dst->page_table_lock);
359 } while ((unsigned long)src_pmd & PMD_TABLE_MASK);
362 spin_unlock(&src->page_table_lock);
369 static void zap_pte_range(struct mmu_gather *tlb,
370 pmd_t *pmd, unsigned long address,
371 unsigned long size, struct zap_details *details)
373 unsigned long offset;
378 if (unlikely(pmd_bad(*pmd))) {
383 ptep = pte_offset_map(pmd, address);
384 offset = address & ~PMD_MASK;
385 if (offset + size > PMD_SIZE)
386 size = PMD_SIZE - offset;
388 if (details && !details->check_mapping && !details->nonlinear_vma)
390 for (offset=0; offset < size; ptep++, offset += PAGE_SIZE) {
394 if (pte_present(pte)) {
395 struct page *page = NULL;
396 unsigned long pfn = pte_pfn(pte);
397 if (pfn_valid(pfn)) {
398 page = pfn_to_page(pfn);
399 if (PageReserved(page))
402 if (unlikely(details) && page) {
404 * unmap_shared_mapping_pages() wants to
405 * invalidate cache without truncating:
406 * unmap shared but keep private pages.
408 if (details->check_mapping &&
409 details->check_mapping != page->mapping)
412 * Each page->index must be checked when
413 * invalidating or truncating nonlinear.
415 if (details->nonlinear_vma &&
416 (page->index < details->first_index ||
417 page->index > details->last_index))
420 pte = ptep_get_and_clear(ptep);
421 tlb_remove_tlb_entry(tlb, ptep, address+offset);
424 if (unlikely(details) && details->nonlinear_vma
425 && linear_page_index(details->nonlinear_vma,
426 address+offset) != page->index)
427 set_pte(ptep, pgoff_to_pte(page->index));
429 set_page_dirty(page);
430 if (pte_young(pte) && !PageAnon(page))
431 mark_page_accessed(page);
433 page_remove_rmap(page);
434 tlb_remove_page(tlb, page);
438 * If details->check_mapping, we leave swap entries;
439 * if details->nonlinear_vma, we leave file entries.
441 if (unlikely(details))
444 free_swap_and_cache(pte_to_swp_entry(pte));
450 static void zap_pmd_range(struct mmu_gather *tlb,
451 pgd_t * dir, unsigned long address,
452 unsigned long size, struct zap_details *details)
455 unsigned long end, pgd_boundary;
459 if (unlikely(pgd_bad(*dir))) {
464 pmd = pmd_offset(dir, address);
465 end = address + size;
466 pgd_boundary = ((address + PGDIR_SIZE) & PGDIR_MASK);
467 if (pgd_boundary && (end > pgd_boundary))
470 zap_pte_range(tlb, pmd, address, end - address, details);
471 address = (address + PMD_SIZE) & PMD_MASK;
473 } while (address && (address < end));
476 static void unmap_page_range(struct mmu_gather *tlb,
477 struct vm_area_struct *vma, unsigned long address,
478 unsigned long end, struct zap_details *details)
482 BUG_ON(address >= end);
483 dir = pgd_offset(vma->vm_mm, address);
484 tlb_start_vma(tlb, vma);
486 zap_pmd_range(tlb, dir, address, end - address, details);
487 address = (address + PGDIR_SIZE) & PGDIR_MASK;
489 } while (address && (address < end));
490 tlb_end_vma(tlb, vma);
493 #ifdef CONFIG_PREEMPT_VOLUNTARY
494 # define ZAP_BLOCK_SIZE (128 * PAGE_SIZE)
497 /* Dispose of an entire struct mmu_gather per rescheduling point */
498 #if defined(CONFIG_SMP) && defined(CONFIG_PREEMPT)
499 #define ZAP_BLOCK_SIZE (FREE_PTE_NR * PAGE_SIZE)
502 /* For UP, 256 pages at a time gives nice low latency */
503 #if !defined(CONFIG_SMP) && defined(CONFIG_PREEMPT)
504 #define ZAP_BLOCK_SIZE (256 * PAGE_SIZE)
507 /* No preempt: go for improved straight-line efficiency */
508 #if !defined(CONFIG_PREEMPT)
509 #define ZAP_BLOCK_SIZE (1024 * PAGE_SIZE)
515 * unmap_vmas - unmap a range of memory covered by a list of vma's
516 * @tlbp: address of the caller's struct mmu_gather
517 * @mm: the controlling mm_struct
518 * @vma: the starting vma
519 * @start_addr: virtual address at which to start unmapping
520 * @end_addr: virtual address at which to end unmapping
521 * @nr_accounted: Place number of unmapped pages in vm-accountable vma's here
522 * @details: details of nonlinear truncation or shared cache invalidation
524 * Returns the number of vma's which were covered by the unmapping.
526 * Unmap all pages in the vma list. Called under page_table_lock.
528 * We aim to not hold page_table_lock for too long (for scheduling latency
529 * reasons). So zap pages in ZAP_BLOCK_SIZE bytecounts. This means we need to
530 * return the ending mmu_gather to the caller.
532 * Only addresses between `start' and `end' will be unmapped.
534 * The VMA list must be sorted in ascending virtual address order.
536 * unmap_vmas() assumes that the caller will flush the whole unmapped address
537 * range after unmap_vmas() returns. So the only responsibility here is to
538 * ensure that any thus-far unmapped pages are flushed before unmap_vmas()
539 * drops the lock and schedules.
541 int unmap_vmas(struct mmu_gather **tlbp, struct mm_struct *mm,
542 struct vm_area_struct *vma, unsigned long start_addr,
543 unsigned long end_addr, unsigned long *nr_accounted,
544 struct zap_details *details)
546 unsigned long zap_bytes = ZAP_BLOCK_SIZE;
547 unsigned long tlb_start = 0; /* For tlb_finish_mmu */
548 int tlb_start_valid = 0;
550 int atomic = details && details->atomic;
552 for ( ; vma && vma->vm_start < end_addr; vma = vma->vm_next) {
556 start = max(vma->vm_start, start_addr);
557 if (start >= vma->vm_end)
559 end = min(vma->vm_end, end_addr);
560 if (end <= vma->vm_start)
563 if (vma->vm_flags & VM_ACCOUNT)
564 *nr_accounted += (end - start) >> PAGE_SHIFT;
567 while (start != end) {
570 if (!tlb_start_valid) {
575 if (is_vm_hugetlb_page(vma)) {
577 unmap_hugepage_range(vma, start, end);
579 block = min(zap_bytes, end - start);
580 unmap_page_range(*tlbp, vma, start,
581 start + block, details);
586 if (!atomic && need_resched()) {
587 int fullmm = tlb_is_full_mm(*tlbp);
588 tlb_finish_mmu(*tlbp, tlb_start, start);
589 cond_resched_lock(&mm->page_table_lock);
590 *tlbp = tlb_gather_mmu(mm, fullmm);
593 if ((long)zap_bytes > 0)
595 zap_bytes = ZAP_BLOCK_SIZE;
602 * zap_page_range - remove user pages in a given range
603 * @vma: vm_area_struct holding the applicable pages
604 * @address: starting address of pages to zap
605 * @size: number of bytes to zap
606 * @details: details of nonlinear truncation or shared cache invalidation
608 void zap_page_range(struct vm_area_struct *vma, unsigned long address,
609 unsigned long size, struct zap_details *details)
611 struct mm_struct *mm = vma->vm_mm;
612 struct mmu_gather *tlb;
613 unsigned long end = address + size;
614 unsigned long nr_accounted = 0;
616 if (is_vm_hugetlb_page(vma)) {
617 zap_hugepage_range(vma, address, size);
622 spin_lock(&mm->page_table_lock);
623 tlb = tlb_gather_mmu(mm, 0);
624 unmap_vmas(&tlb, mm, vma, address, end, &nr_accounted, details);
625 tlb_finish_mmu(tlb, address, end);
626 spin_unlock(&mm->page_table_lock);
630 * Do a quick page-table lookup for a single page.
631 * mm->page_table_lock must be held.
634 follow_page(struct mm_struct *mm, unsigned long address, int write)
642 page = follow_huge_addr(mm, address, write);
646 pgd = pgd_offset(mm, address);
647 if (pgd_none(*pgd) || unlikely(pgd_bad(*pgd)))
650 pmd = pmd_offset(pgd, address);
654 return follow_huge_pmd(mm, address, pmd, write);
655 if (unlikely(pmd_bad(*pmd)))
658 ptep = pte_offset_map(pmd, address);
664 if (pte_present(pte)) {
665 if (write && !pte_write(pte))
668 if (pfn_valid(pfn)) {
669 page = pfn_to_page(pfn);
670 if (write && !pte_dirty(pte) && !PageDirty(page))
671 set_page_dirty(page);
672 mark_page_accessed(page);
682 follow_page_pfn(struct mm_struct *mm, unsigned long address, int write,
683 unsigned long *pfn_ptr)
692 page = follow_huge_addr(mm, address, write);
696 pgd = pgd_offset(mm, address);
697 if (pgd_none(*pgd) || pgd_bad(*pgd))
700 pmd = pmd_offset(pgd, address);
704 return follow_huge_pmd(mm, address, pmd, write);
708 ptep = pte_offset_map(pmd, address);
714 if (pte_present(pte)) {
715 if (write && !pte_write(pte))
717 if (write && !pte_dirty(pte)) {
718 struct page *page = pte_page(pte);
719 if (!PageDirty(page))
720 set_page_dirty(page);
723 if (pfn_valid(pfn)) {
724 struct page *page = pfn_to_page(pfn);
726 mark_page_accessed(page);
740 * Given a physical address, is there a useful struct page pointing to
741 * it? This may become more complex in the future if we start dealing
742 * with IO-aperture pages for direct-IO.
745 static inline struct page *get_page_map(struct page *page)
747 if (!pfn_valid(page_to_pfn(page)))
753 #ifndef CONFIG_X86_4G
755 untouched_anonymous_page(struct mm_struct* mm, struct vm_area_struct *vma,
756 unsigned long address)
761 /* Check if the vma is for an anonymous mapping. */
762 if (vma->vm_ops && vma->vm_ops->nopage)
765 /* Check if page directory entry exists. */
766 pgd = pgd_offset(mm, address);
767 if (pgd_none(*pgd) || unlikely(pgd_bad(*pgd)))
770 /* Check if page middle directory entry exists. */
771 pmd = pmd_offset(pgd, address);
772 if (pmd_none(*pmd) || unlikely(pmd_bad(*pmd)))
775 /* There is a pte slot for 'address' in 'mm'. */
781 int get_user_pages(struct task_struct *tsk, struct mm_struct *mm,
782 unsigned long start, int len, int write, int force,
783 struct page **pages, struct vm_area_struct **vmas)
789 * Require read or write permissions.
790 * If 'force' is set, we only require the "MAY" flags.
792 flags = write ? (VM_WRITE | VM_MAYWRITE) : (VM_READ | VM_MAYREAD);
793 flags &= force ? (VM_MAYREAD | VM_MAYWRITE) : (VM_READ | VM_WRITE);
797 struct vm_area_struct * vma;
799 vma = find_extend_vma(mm, start);
800 if (!vma && in_gate_area(tsk, start)) {
801 unsigned long pg = start & PAGE_MASK;
802 struct vm_area_struct *gate_vma = get_gate_vma(tsk);
806 if (write) /* user gate pages are read-only */
807 return i ? : -EFAULT;
808 pgd = pgd_offset_gate(mm, pg);
810 return i ? : -EFAULT;
811 pmd = pmd_offset(pgd, pg);
813 return i ? : -EFAULT;
814 pte = pte_offset_map(pmd, pg);
816 return i ? : -EFAULT;
817 if (!pte_present(*pte)) {
819 return i ? : -EFAULT;
822 pages[i] = pte_page(*pte);
834 if (!vma || (pages && (vma->vm_flags & VM_IO))
835 || !(flags & vma->vm_flags))
836 return i ? : -EFAULT;
838 if (is_vm_hugetlb_page(vma)) {
839 i = follow_hugetlb_page(mm, vma, pages, vmas,
843 spin_lock(&mm->page_table_lock);
846 int lookup_write = write;
847 while (!(map = follow_page(mm, start, lookup_write))) {
849 * Shortcut for anonymous pages. We don't want
850 * to force the creation of pages tables for
851 * insanly big anonymously mapped areas that
852 * nobody touched so far. This is important
853 * for doing a core dump for these mappings.
855 * disable this for 4:4 - it prevents
856 * follow_page() from ever seeing these pages.
858 * (The 'fix' is dubious anyway, there's
859 * nothing that this code avoids which couldnt
860 * be triggered from userspace anyway.)
862 #ifndef CONFIG_X86_4G
864 untouched_anonymous_page(mm,vma,start)) {
865 map = ZERO_PAGE(start);
869 spin_unlock(&mm->page_table_lock);
870 switch (handle_mm_fault(mm,vma,start,write)) {
877 case VM_FAULT_SIGBUS:
878 return i ? i : -EFAULT;
880 return i ? i : -ENOMEM;
885 * Now that we have performed a write fault
886 * and surely no longer have a shared page we
887 * shouldn't write, we shouldn't ignore an
888 * unwritable page in the page table if
889 * we are forcing write access.
891 lookup_write = write && !force;
892 spin_lock(&mm->page_table_lock);
895 pages[i] = get_page_map(map);
897 spin_unlock(&mm->page_table_lock);
899 page_cache_release(pages[i]);
903 flush_dcache_page(pages[i]);
904 if (!PageReserved(pages[i]))
905 page_cache_get(pages[i]);
912 } while(len && start < vma->vm_end);
913 spin_unlock(&mm->page_table_lock);
919 EXPORT_SYMBOL(get_user_pages);
921 static void zeromap_pte_range(pte_t * pte, unsigned long address,
922 unsigned long size, pgprot_t prot)
926 address &= ~PMD_MASK;
927 end = address + size;
931 pte_t zero_pte = pte_wrprotect(mk_pte(ZERO_PAGE(address), prot));
932 BUG_ON(!pte_none(*pte));
933 set_pte(pte, zero_pte);
934 address += PAGE_SIZE;
936 } while (address && (address < end));
939 static inline int zeromap_pmd_range(struct mm_struct *mm, pmd_t * pmd, unsigned long address,
940 unsigned long size, pgprot_t prot)
942 unsigned long base, end;
944 base = address & PGDIR_MASK;
945 address &= ~PGDIR_MASK;
946 end = address + size;
947 if (end > PGDIR_SIZE)
950 pte_t * pte = pte_alloc_map(mm, pmd, base + address);
953 zeromap_pte_range(pte, base + address, end - address, prot);
955 address = (address + PMD_SIZE) & PMD_MASK;
957 } while (address && (address < end));
961 int zeromap_page_range(struct vm_area_struct *vma, unsigned long address, unsigned long size, pgprot_t prot)
965 unsigned long beg = address;
966 unsigned long end = address + size;
967 struct mm_struct *mm = vma->vm_mm;
969 dir = pgd_offset(mm, address);
970 flush_cache_range(vma, beg, end);
974 spin_lock(&mm->page_table_lock);
976 pmd_t *pmd = pmd_alloc(mm, dir, address);
980 error = zeromap_pmd_range(mm, pmd, address, end - address, prot);
983 address = (address + PGDIR_SIZE) & PGDIR_MASK;
985 } while (address && (address < end));
987 * Why flush? zeromap_pte_range has a BUG_ON for !pte_none()
989 flush_tlb_range(vma, beg, end);
990 spin_unlock(&mm->page_table_lock);
995 * maps a range of physical memory into the requested pages. the old
996 * mappings are removed. any references to nonexistent pages results
997 * in null mappings (currently treated as "copy-on-access")
999 static inline void remap_pte_range(pte_t * pte, unsigned long address, unsigned long size,
1000 unsigned long phys_addr, pgprot_t prot)
1005 address &= ~PMD_MASK;
1006 end = address + size;
1009 pfn = phys_addr >> PAGE_SHIFT;
1011 BUG_ON(!pte_none(*pte));
1012 if (!pfn_valid(pfn) || PageReserved(pfn_to_page(pfn)))
1013 set_pte(pte, pfn_pte(pfn, prot));
1014 address += PAGE_SIZE;
1017 } while (address && (address < end));
1020 static inline int remap_pmd_range(struct mm_struct *mm, pmd_t * pmd, unsigned long address, unsigned long size,
1021 unsigned long phys_addr, pgprot_t prot)
1023 unsigned long base, end;
1025 base = address & PGDIR_MASK;
1026 address &= ~PGDIR_MASK;
1027 end = address + size;
1028 if (end > PGDIR_SIZE)
1030 phys_addr -= address;
1032 pte_t * pte = pte_alloc_map(mm, pmd, base + address);
1035 remap_pte_range(pte, base + address, end - address, address + phys_addr, prot);
1037 address = (address + PMD_SIZE) & PMD_MASK;
1039 } while (address && (address < end));
1043 /* Note: this is only safe if the mm semaphore is held when called. */
1044 int remap_page_range(struct vm_area_struct *vma, unsigned long from, unsigned long phys_addr, unsigned long size, pgprot_t prot)
1048 unsigned long beg = from;
1049 unsigned long end = from + size;
1050 struct mm_struct *mm = vma->vm_mm;
1053 dir = pgd_offset(mm, from);
1054 flush_cache_range(vma, beg, end);
1058 spin_lock(&mm->page_table_lock);
1060 pmd_t *pmd = pmd_alloc(mm, dir, from);
1064 error = remap_pmd_range(mm, pmd, from, end - from, phys_addr + from, prot);
1067 from = (from + PGDIR_SIZE) & PGDIR_MASK;
1069 } while (from && (from < end));
1071 * Why flush? remap_pte_range has a BUG_ON for !pte_none()
1073 flush_tlb_range(vma, beg, end);
1074 spin_unlock(&mm->page_table_lock);
1078 EXPORT_SYMBOL(remap_page_range);
1081 * Do pte_mkwrite, but only if the vma says VM_WRITE. We do this when
1082 * servicing faults for write access. In the normal case, do always want
1083 * pte_mkwrite. But get_user_pages can cause write faults for mappings
1084 * that do not have writing enabled, when used by access_process_vm.
1086 static inline pte_t maybe_mkwrite(pte_t pte, struct vm_area_struct *vma)
1088 if (likely(vma->vm_flags & VM_WRITE))
1089 pte = pte_mkwrite(pte);
1094 * We hold the mm semaphore for reading and vma->vm_mm->page_table_lock
1096 static inline void break_cow(struct vm_area_struct * vma, struct page * new_page, unsigned long address,
1101 flush_cache_page(vma, address);
1102 entry = maybe_mkwrite(pte_mkdirty(mk_pte(new_page, vma->vm_page_prot)),
1104 ptep_establish(vma, address, page_table, entry);
1105 update_mmu_cache(vma, address, entry);
1109 * This routine handles present pages, when users try to write
1110 * to a shared page. It is done by copying the page to a new address
1111 * and decrementing the shared-page counter for the old page.
1113 * Goto-purists beware: the only reason for goto's here is that it results
1114 * in better assembly code.. The "default" path will see no jumps at all.
1116 * Note that this routine assumes that the protection checks have been
1117 * done by the caller (the low-level page fault routine in most cases).
1118 * Thus we can safely just mark it writable once we've done any necessary
1121 * We also mark the page dirty at this point even though the page will
1122 * change only once the write actually happens. This avoids a few races,
1123 * and potentially makes it more efficient.
1125 * We hold the mm semaphore and the page_table_lock on entry and exit
1126 * with the page_table_lock released.
1128 static int do_wp_page(struct mm_struct *mm, struct vm_area_struct * vma,
1129 unsigned long address, pte_t *page_table, pmd_t *pmd, pte_t pte)
1131 struct page *old_page, *new_page;
1132 unsigned long pfn = pte_pfn(pte);
1135 if (unlikely(!pfn_valid(pfn))) {
1137 * This should really halt the system so it can be debugged or
1138 * at least the kernel stops what it's doing before it corrupts
1139 * data, but for the moment just pretend this is OOM.
1141 pte_unmap(page_table);
1142 printk(KERN_ERR "do_wp_page: bogus page at address %08lx\n",
1144 spin_unlock(&mm->page_table_lock);
1145 return VM_FAULT_OOM;
1147 old_page = pfn_to_page(pfn);
1149 if (!TestSetPageLocked(old_page)) {
1150 int reuse = can_share_swap_page(old_page);
1151 unlock_page(old_page);
1153 flush_cache_page(vma, address);
1154 entry = maybe_mkwrite(pte_mkyoung(pte_mkdirty(pte)),
1156 ptep_set_access_flags(vma, address, page_table, entry, 1);
1157 update_mmu_cache(vma, address, entry);
1158 pte_unmap(page_table);
1159 spin_unlock(&mm->page_table_lock);
1160 return VM_FAULT_MINOR;
1163 pte_unmap(page_table);
1166 * Ok, we need to copy. Oh, well..
1168 if (!PageReserved(old_page))
1169 page_cache_get(old_page);
1170 spin_unlock(&mm->page_table_lock);
1172 if (unlikely(anon_vma_prepare(vma)))
1174 new_page = alloc_page_vma(GFP_HIGHUSER, vma, address);
1177 copy_cow_page(old_page,new_page,address);
1180 * Re-check the pte - we dropped the lock
1182 spin_lock(&mm->page_table_lock);
1183 page_table = pte_offset_map(pmd, address);
1184 if (likely(pte_same(*page_table, pte))) {
1185 if (PageReserved(old_page))
1187 vx_rsspages_inc(mm);
1189 page_remove_rmap(old_page);
1190 break_cow(vma, new_page, address, page_table);
1191 lru_cache_add_active(new_page);
1192 page_add_anon_rmap(new_page, vma, address);
1194 /* Free the old page.. */
1195 new_page = old_page;
1197 pte_unmap(page_table);
1198 page_cache_release(new_page);
1199 page_cache_release(old_page);
1200 spin_unlock(&mm->page_table_lock);
1201 return VM_FAULT_MINOR;
1204 page_cache_release(old_page);
1205 return VM_FAULT_OOM;
1209 * Helper function for unmap_mapping_range().
1211 static inline void unmap_mapping_range_list(struct prio_tree_root *root,
1212 struct zap_details *details)
1214 struct vm_area_struct *vma = NULL;
1215 struct prio_tree_iter iter;
1216 pgoff_t vba, vea, zba, zea;
1218 while ((vma = vma_prio_tree_next(vma, root, &iter,
1219 details->first_index, details->last_index)) != NULL) {
1220 vba = vma->vm_pgoff;
1221 vea = vba + ((vma->vm_end - vma->vm_start) >> PAGE_SHIFT) - 1;
1222 /* Assume for now that PAGE_CACHE_SHIFT == PAGE_SHIFT */
1223 zba = details->first_index;
1226 zea = details->last_index;
1230 ((zba - vba) << PAGE_SHIFT) + vma->vm_start,
1231 (zea - zba + 1) << PAGE_SHIFT, details);
1236 * unmap_mapping_range - unmap the portion of all mmaps
1237 * in the specified address_space corresponding to the specified
1238 * page range in the underlying file.
1239 * @address_space: the address space containing mmaps to be unmapped.
1240 * @holebegin: byte in first page to unmap, relative to the start of
1241 * the underlying file. This will be rounded down to a PAGE_SIZE
1242 * boundary. Note that this is different from vmtruncate(), which
1243 * must keep the partial page. In contrast, we must get rid of
1245 * @holelen: size of prospective hole in bytes. This will be rounded
1246 * up to a PAGE_SIZE boundary. A holelen of zero truncates to the
1248 * @even_cows: 1 when truncating a file, unmap even private COWed pages;
1249 * but 0 when invalidating pagecache, don't throw away private data.
1251 void unmap_mapping_range(struct address_space *mapping,
1252 loff_t const holebegin, loff_t const holelen, int even_cows)
1254 struct zap_details details;
1255 pgoff_t hba = holebegin >> PAGE_SHIFT;
1256 pgoff_t hlen = (holelen + PAGE_SIZE - 1) >> PAGE_SHIFT;
1258 /* Check for overflow. */
1259 if (sizeof(holelen) > sizeof(hlen)) {
1261 (holebegin + holelen + PAGE_SIZE - 1) >> PAGE_SHIFT;
1262 if (holeend & ~(long long)ULONG_MAX)
1263 hlen = ULONG_MAX - hba + 1;
1266 details.check_mapping = even_cows? NULL: mapping;
1267 details.nonlinear_vma = NULL;
1268 details.first_index = hba;
1269 details.last_index = hba + hlen - 1;
1270 details.atomic = 1; /* A spinlock is held */
1271 if (details.last_index < details.first_index)
1272 details.last_index = ULONG_MAX;
1274 spin_lock(&mapping->i_mmap_lock);
1275 /* Protect against page fault */
1276 atomic_inc(&mapping->truncate_count);
1278 if (unlikely(!prio_tree_empty(&mapping->i_mmap)))
1279 unmap_mapping_range_list(&mapping->i_mmap, &details);
1282 * In nonlinear VMAs there is no correspondence between virtual address
1283 * offset and file offset. So we must perform an exhaustive search
1284 * across *all* the pages in each nonlinear VMA, not just the pages
1285 * whose virtual address lies outside the file truncation point.
1287 if (unlikely(!list_empty(&mapping->i_mmap_nonlinear))) {
1288 struct vm_area_struct *vma;
1289 list_for_each_entry(vma, &mapping->i_mmap_nonlinear,
1290 shared.vm_set.list) {
1291 details.nonlinear_vma = vma;
1292 zap_page_range(vma, vma->vm_start,
1293 vma->vm_end - vma->vm_start, &details);
1296 spin_unlock(&mapping->i_mmap_lock);
1298 EXPORT_SYMBOL(unmap_mapping_range);
1301 * Handle all mappings that got truncated by a "truncate()"
1304 * NOTE! We have to be ready to update the memory sharing
1305 * between the file and the memory map for a potential last
1306 * incomplete page. Ugly, but necessary.
1308 int vmtruncate(struct inode * inode, loff_t offset)
1310 struct address_space *mapping = inode->i_mapping;
1311 unsigned long limit;
1313 if (inode->i_size < offset)
1316 * truncation of in-use swapfiles is disallowed - it would cause
1317 * subsequent swapout to scribble on the now-freed blocks.
1319 if (IS_SWAPFILE(inode))
1321 i_size_write(inode, offset);
1322 unmap_mapping_range(mapping, offset + PAGE_SIZE - 1, 0, 1);
1323 truncate_inode_pages(mapping, offset);
1327 limit = current->rlim[RLIMIT_FSIZE].rlim_cur;
1328 if (limit != RLIM_INFINITY && offset > limit)
1330 if (offset > inode->i_sb->s_maxbytes)
1332 i_size_write(inode, offset);
1335 if (inode->i_op && inode->i_op->truncate)
1336 inode->i_op->truncate(inode);
1339 send_sig(SIGXFSZ, current, 0);
1346 EXPORT_SYMBOL(vmtruncate);
1349 * Primitive swap readahead code. We simply read an aligned block of
1350 * (1 << page_cluster) entries in the swap area. This method is chosen
1351 * because it doesn't cost us any seek time. We also make sure to queue
1352 * the 'original' request together with the readahead ones...
1354 * This has been extended to use the NUMA policies from the mm triggering
1357 * Caller must hold down_read on the vma->vm_mm if vma is not NULL.
1359 void swapin_readahead(swp_entry_t entry, unsigned long addr,struct vm_area_struct *vma)
1362 struct vm_area_struct *next_vma = vma ? vma->vm_next : NULL;
1365 struct page *new_page;
1366 unsigned long offset;
1369 * Get the number of handles we should do readahead io to.
1371 num = valid_swaphandles(entry, &offset);
1372 for (i = 0; i < num; offset++, i++) {
1373 /* Ok, do the async read-ahead now */
1374 new_page = read_swap_cache_async(swp_entry(swp_type(entry),
1375 offset), vma, addr);
1378 page_cache_release(new_page);
1381 * Find the next applicable VMA for the NUMA policy.
1387 if (addr >= vma->vm_end) {
1389 next_vma = vma ? vma->vm_next : NULL;
1391 if (vma && addr < vma->vm_start)
1394 if (next_vma && addr >= next_vma->vm_start) {
1396 next_vma = vma->vm_next;
1401 lru_add_drain(); /* Push any new pages onto the LRU now */
1405 * We hold the mm semaphore and the page_table_lock on entry and
1406 * should release the pagetable lock on exit..
1408 static int do_swap_page(struct mm_struct * mm,
1409 struct vm_area_struct * vma, unsigned long address,
1410 pte_t *page_table, pmd_t *pmd, pte_t orig_pte, int write_access)
1413 swp_entry_t entry = pte_to_swp_entry(orig_pte);
1415 int ret = VM_FAULT_MINOR;
1417 pte_unmap(page_table);
1418 spin_unlock(&mm->page_table_lock);
1419 page = lookup_swap_cache(entry);
1421 swapin_readahead(entry, address, vma);
1422 page = read_swap_cache_async(entry, vma, address);
1425 * Back out if somebody else faulted in this pte while
1426 * we released the page table lock.
1428 spin_lock(&mm->page_table_lock);
1429 page_table = pte_offset_map(pmd, address);
1430 if (likely(pte_same(*page_table, orig_pte)))
1433 ret = VM_FAULT_MINOR;
1434 pte_unmap(page_table);
1435 spin_unlock(&mm->page_table_lock);
1439 /* Had to read the page from swap area: Major fault */
1440 ret = VM_FAULT_MAJOR;
1441 inc_page_state(pgmajfault);
1445 if (!vx_rsspages_avail(mm, 1)) {
1449 mark_page_accessed(page);
1453 * Back out if somebody else faulted in this pte while we
1454 * released the page table lock.
1456 spin_lock(&mm->page_table_lock);
1457 page_table = pte_offset_map(pmd, address);
1458 if (unlikely(!pte_same(*page_table, orig_pte))) {
1459 pte_unmap(page_table);
1460 spin_unlock(&mm->page_table_lock);
1462 page_cache_release(page);
1463 ret = VM_FAULT_MINOR;
1467 /* The page isn't present yet, go ahead with the fault. */
1471 remove_exclusive_swap_page(page);
1474 vx_rsspages_inc(mm);
1475 pte = mk_pte(page, vma->vm_page_prot);
1476 if (write_access && can_share_swap_page(page)) {
1477 pte = maybe_mkwrite(pte_mkdirty(pte), vma);
1482 flush_icache_page(vma, page);
1483 set_pte(page_table, pte);
1484 page_add_anon_rmap(page, vma, address);
1487 if (do_wp_page(mm, vma, address,
1488 page_table, pmd, pte) == VM_FAULT_OOM)
1493 /* No need to invalidate - it was non-present before */
1494 update_mmu_cache(vma, address, pte);
1495 pte_unmap(page_table);
1496 spin_unlock(&mm->page_table_lock);
1502 * We are called with the MM semaphore and page_table_lock
1503 * spinlock held to protect against concurrent faults in
1504 * multithreaded programs.
1507 do_anonymous_page(struct mm_struct *mm, struct vm_area_struct *vma,
1508 pte_t *page_table, pmd_t *pmd, int write_access,
1512 struct page * page = ZERO_PAGE(addr);
1514 /* Read-only mapping of ZERO_PAGE. */
1515 entry = pte_wrprotect(mk_pte(ZERO_PAGE(addr), vma->vm_page_prot));
1517 /* ..except if it's a write access */
1519 /* Allocate our own private page. */
1520 pte_unmap(page_table);
1521 spin_unlock(&mm->page_table_lock);
1523 if (unlikely(anon_vma_prepare(vma)))
1525 if (!vx_rsspages_avail(mm, 1))
1528 page = alloc_page_vma(GFP_HIGHUSER, vma, addr);
1531 clear_user_highpage(page, addr);
1533 spin_lock(&mm->page_table_lock);
1534 page_table = pte_offset_map(pmd, addr);
1536 if (!pte_none(*page_table)) {
1537 pte_unmap(page_table);
1538 page_cache_release(page);
1539 spin_unlock(&mm->page_table_lock);
1543 vx_rsspages_inc(mm);
1544 entry = maybe_mkwrite(pte_mkdirty(mk_pte(page,
1545 vma->vm_page_prot)),
1547 lru_cache_add_active(page);
1548 mark_page_accessed(page);
1549 page_add_anon_rmap(page, vma, addr);
1552 set_pte(page_table, entry);
1553 pte_unmap(page_table);
1555 /* No need to invalidate - it was non-present before */
1556 update_mmu_cache(vma, addr, entry);
1557 spin_unlock(&mm->page_table_lock);
1559 return VM_FAULT_MINOR;
1561 return VM_FAULT_OOM;
1565 * do_no_page() tries to create a new page mapping. It aggressively
1566 * tries to share with existing pages, but makes a separate copy if
1567 * the "write_access" parameter is true in order to avoid the next
1570 * As this is called only for pages that do not currently exist, we
1571 * do not need to flush old virtual caches or the TLB.
1573 * This is called with the MM semaphore held and the page table
1574 * spinlock held. Exit with the spinlock released.
1577 do_no_page(struct mm_struct *mm, struct vm_area_struct *vma,
1578 unsigned long address, int write_access, pte_t *page_table, pmd_t *pmd)
1580 struct page * new_page;
1581 struct address_space *mapping = NULL;
1584 int ret = VM_FAULT_MINOR;
1587 if (!vma->vm_ops || !vma->vm_ops->nopage)
1588 return do_anonymous_page(mm, vma, page_table,
1589 pmd, write_access, address);
1590 pte_unmap(page_table);
1591 spin_unlock(&mm->page_table_lock);
1594 mapping = vma->vm_file->f_mapping;
1595 sequence = atomic_read(&mapping->truncate_count);
1597 smp_rmb(); /* Prevent CPU from reordering lock-free ->nopage() */
1599 new_page = vma->vm_ops->nopage(vma, address & PAGE_MASK, &ret);
1601 /* no page was available -- either SIGBUS or OOM */
1602 if (new_page == NOPAGE_SIGBUS)
1603 return VM_FAULT_SIGBUS;
1604 if (new_page == NOPAGE_OOM)
1605 return VM_FAULT_OOM;
1606 if (!vx_rsspages_avail(mm, 1))
1607 return VM_FAULT_OOM;
1610 * Should we do an early C-O-W break?
1612 if (write_access && !(vma->vm_flags & VM_SHARED)) {
1615 if (unlikely(anon_vma_prepare(vma)))
1617 page = alloc_page_vma(GFP_HIGHUSER, vma, address);
1620 copy_user_highpage(page, new_page, address);
1621 page_cache_release(new_page);
1626 spin_lock(&mm->page_table_lock);
1628 * For a file-backed vma, someone could have truncated or otherwise
1629 * invalidated this page. If unmap_mapping_range got called,
1630 * retry getting the page.
1633 (unlikely(sequence != atomic_read(&mapping->truncate_count)))) {
1634 sequence = atomic_read(&mapping->truncate_count);
1635 spin_unlock(&mm->page_table_lock);
1636 page_cache_release(new_page);
1639 page_table = pte_offset_map(pmd, address);
1642 * This silly early PAGE_DIRTY setting removes a race
1643 * due to the bad i386 page protection. But it's valid
1644 * for other architectures too.
1646 * Note that if write_access is true, we either now have
1647 * an exclusive copy of the page, or this is a shared mapping,
1648 * so we can make it writable and dirty to avoid having to
1649 * handle that later.
1651 /* Only go through if we didn't race with anybody else... */
1652 if (pte_none(*page_table)) {
1653 if (!PageReserved(new_page))
1655 vx_rsspages_inc(mm);
1656 flush_icache_page(vma, new_page);
1657 entry = mk_pte(new_page, vma->vm_page_prot);
1659 entry = maybe_mkwrite(pte_mkdirty(entry), vma);
1660 set_pte(page_table, entry);
1662 lru_cache_add_active(new_page);
1663 page_add_anon_rmap(new_page, vma, address);
1665 page_add_file_rmap(new_page);
1666 pte_unmap(page_table);
1668 /* One of our sibling threads was faster, back out. */
1669 pte_unmap(page_table);
1670 page_cache_release(new_page);
1671 spin_unlock(&mm->page_table_lock);
1675 /* no need to invalidate: a not-present page shouldn't be cached */
1676 update_mmu_cache(vma, address, entry);
1677 spin_unlock(&mm->page_table_lock);
1681 page_cache_release(new_page);
1687 * Fault of a previously existing named mapping. Repopulate the pte
1688 * from the encoded file_pte if possible. This enables swappable
1691 static int do_file_page(struct mm_struct * mm, struct vm_area_struct * vma,
1692 unsigned long address, int write_access, pte_t *pte, pmd_t *pmd)
1694 unsigned long pgoff;
1697 BUG_ON(!vma->vm_ops || !vma->vm_ops->nopage);
1699 * Fall back to the linear mapping if the fs does not support
1702 if (!vma->vm_ops || !vma->vm_ops->populate ||
1703 (write_access && !(vma->vm_flags & VM_SHARED))) {
1705 return do_no_page(mm, vma, address, write_access, pte, pmd);
1708 pgoff = pte_to_pgoff(*pte);
1711 spin_unlock(&mm->page_table_lock);
1713 err = vma->vm_ops->populate(vma, address & PAGE_MASK, PAGE_SIZE, vma->vm_page_prot, pgoff, 0);
1715 return VM_FAULT_OOM;
1717 return VM_FAULT_SIGBUS;
1718 return VM_FAULT_MAJOR;
1722 * These routines also need to handle stuff like marking pages dirty
1723 * and/or accessed for architectures that don't do it in hardware (most
1724 * RISC architectures). The early dirtying is also good on the i386.
1726 * There is also a hook called "update_mmu_cache()" that architectures
1727 * with external mmu caches can use to update those (ie the Sparc or
1728 * PowerPC hashed page tables that act as extended TLBs).
1730 * Note the "page_table_lock". It is to protect against kswapd removing
1731 * pages from under us. Note that kswapd only ever _removes_ pages, never
1732 * adds them. As such, once we have noticed that the page is not present,
1733 * we can drop the lock early.
1735 * The adding of pages is protected by the MM semaphore (which we hold),
1736 * so we don't need to worry about a page being suddenly been added into
1739 * We enter with the pagetable spinlock held, we are supposed to
1740 * release it when done.
1742 static inline int handle_pte_fault(struct mm_struct *mm,
1743 struct vm_area_struct * vma, unsigned long address,
1744 int write_access, pte_t *pte, pmd_t *pmd)
1749 if (!pte_present(entry)) {
1751 * If it truly wasn't present, we know that kswapd
1752 * and the PTE updates will not touch it later. So
1755 if (pte_none(entry))
1756 return do_no_page(mm, vma, address, write_access, pte, pmd);
1757 if (pte_file(entry))
1758 return do_file_page(mm, vma, address, write_access, pte, pmd);
1759 return do_swap_page(mm, vma, address, pte, pmd, entry, write_access);
1763 if (!pte_write(entry))
1764 return do_wp_page(mm, vma, address, pte, pmd, entry);
1766 entry = pte_mkdirty(entry);
1768 entry = pte_mkyoung(entry);
1769 ptep_set_access_flags(vma, address, pte, entry, write_access);
1770 update_mmu_cache(vma, address, entry);
1772 spin_unlock(&mm->page_table_lock);
1773 return VM_FAULT_MINOR;
1777 * By the time we get here, we already hold the mm semaphore
1779 int handle_mm_fault(struct mm_struct *mm, struct vm_area_struct * vma,
1780 unsigned long address, int write_access)
1785 __set_current_state(TASK_RUNNING);
1786 pgd = pgd_offset(mm, address);
1788 inc_page_state(pgfault);
1790 if (is_vm_hugetlb_page(vma))
1791 return VM_FAULT_SIGBUS; /* mapping truncation does this. */
1794 * We need the page table lock to synchronize with kswapd
1795 * and the SMP-safe atomic PTE updates.
1797 set_delay_flag(current,PF_MEMIO);
1798 spin_lock(&mm->page_table_lock);
1799 pmd = pmd_alloc(mm, pgd, address);
1802 pte_t * pte = pte_alloc_map(mm, pmd, address);
1804 int rc = handle_pte_fault(mm, vma, address, write_access, pte, pmd);
1805 clear_delay_flag(current,PF_MEMIO);
1809 spin_unlock(&mm->page_table_lock);
1810 clear_delay_flag(current,PF_MEMIO);
1811 return VM_FAULT_OOM;
1815 * Allocate page middle directory.
1817 * We've already handled the fast-path in-line, and we own the
1820 * On a two-level page table, this ends up actually being entirely
1823 pmd_t fastcall *__pmd_alloc(struct mm_struct *mm, pgd_t *pgd, unsigned long address)
1827 spin_unlock(&mm->page_table_lock);
1828 new = pmd_alloc_one(mm, address);
1829 spin_lock(&mm->page_table_lock);
1834 * Because we dropped the lock, we should re-check the
1835 * entry, as somebody else could have populated it..
1837 if (pgd_present(*pgd)) {
1841 pgd_populate(mm, pgd, new);
1843 return pmd_offset(pgd, address);
1846 int make_pages_present(unsigned long addr, unsigned long end)
1848 int ret, len, write;
1849 struct vm_area_struct * vma;
1851 vma = find_vma(current->mm, addr);
1852 write = (vma->vm_flags & VM_WRITE) != 0;
1855 if (end > vma->vm_end)
1857 len = (end+PAGE_SIZE-1)/PAGE_SIZE-addr/PAGE_SIZE;
1858 ret = get_user_pages(current, current->mm, addr,
1859 len, write, 0, NULL, NULL);
1862 return ret == len ? 0 : -1;
1866 * Map a vmalloc()-space virtual address to the physical page.
1868 struct page * vmalloc_to_page(void * vmalloc_addr)
1870 unsigned long addr = (unsigned long) vmalloc_addr;
1871 struct page *page = NULL;
1872 pgd_t *pgd = pgd_offset_k(addr);
1876 if (!pgd_none(*pgd)) {
1877 pmd = pmd_offset(pgd, addr);
1878 if (!pmd_none(*pmd)) {
1880 ptep = pte_offset_map(pmd, addr);
1882 if (pte_present(pte))
1883 page = pte_page(pte);
1891 EXPORT_SYMBOL(vmalloc_to_page);
1893 #if !defined(CONFIG_ARCH_GATE_AREA)
1895 #if defined(AT_SYSINFO_EHDR)
1896 struct vm_area_struct gate_vma;
1898 static int __init gate_vma_init(void)
1900 gate_vma.vm_mm = NULL;
1901 gate_vma.vm_start = FIXADDR_USER_START;
1902 gate_vma.vm_end = FIXADDR_USER_END;
1903 gate_vma.vm_page_prot = PAGE_READONLY;
1904 gate_vma.vm_flags = 0;
1907 __initcall(gate_vma_init);
1910 struct vm_area_struct *get_gate_vma(struct task_struct *tsk)
1912 #ifdef AT_SYSINFO_EHDR
1919 int in_gate_area(struct task_struct *task, unsigned long addr)
1921 #ifdef AT_SYSINFO_EHDR
1922 if ((addr >= FIXADDR_USER_START) && (addr < FIXADDR_USER_END))