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)
38 * Aug/Sep 2004 Changed to four level page tables (Andi Kleen)
41 #include <linux/kernel_stat.h>
43 #include <linux/hugetlb.h>
44 #include <linux/mman.h>
45 #include <linux/swap.h>
46 #include <linux/highmem.h>
47 #include <linux/pagemap.h>
48 #include <linux/ksm.h>
49 #include <linux/rmap.h>
50 #include <linux/export.h>
51 #include <linux/delayacct.h>
52 #include <linux/init.h>
53 #include <linux/pfn_t.h>
54 #include <linux/writeback.h>
55 #include <linux/memcontrol.h>
56 #include <linux/mmu_notifier.h>
57 #include <linux/kallsyms.h>
58 #include <linux/swapops.h>
59 #include <linux/elf.h>
60 #include <linux/gfp.h>
61 #include <linux/migrate.h>
62 #include <linux/string.h>
63 #include <linux/dma-debug.h>
64 #include <linux/debugfs.h>
65 #include <linux/userfaultfd_k.h>
66 #include <linux/dax.h>
69 #include <asm/mmu_context.h>
70 #include <asm/pgalloc.h>
71 #include <asm/uaccess.h>
73 #include <asm/tlbflush.h>
74 #include <asm/pgtable.h>
78 #if defined(LAST_CPUPID_NOT_IN_PAGE_FLAGS) && !defined(CONFIG_COMPILE_TEST)
79 #warning Unfortunate NUMA and NUMA Balancing config, growing page-frame for last_cpupid.
82 #ifndef CONFIG_NEED_MULTIPLE_NODES
83 /* use the per-pgdat data instead for discontigmem - mbligh */
84 unsigned long max_mapnr;
87 EXPORT_SYMBOL(max_mapnr);
88 EXPORT_SYMBOL(mem_map);
92 * A number of key systems in x86 including ioremap() rely on the assumption
93 * that high_memory defines the upper bound on direct map memory, then end
94 * of ZONE_NORMAL. Under CONFIG_DISCONTIG this means that max_low_pfn and
95 * highstart_pfn must be the same; there must be no gap between ZONE_NORMAL
100 EXPORT_SYMBOL(high_memory);
103 * Randomize the address space (stacks, mmaps, brk, etc.).
105 * ( When CONFIG_COMPAT_BRK=y we exclude brk from randomization,
106 * as ancient (libc5 based) binaries can segfault. )
108 int randomize_va_space __read_mostly =
109 #ifdef CONFIG_COMPAT_BRK
115 static int __init disable_randmaps(char *s)
117 randomize_va_space = 0;
120 __setup("norandmaps", disable_randmaps);
122 unsigned long zero_pfn __read_mostly;
123 unsigned long highest_memmap_pfn __read_mostly;
125 EXPORT_SYMBOL(zero_pfn);
128 * CONFIG_MMU architectures set up ZERO_PAGE in their paging_init()
130 static int __init init_zero_pfn(void)
132 zero_pfn = page_to_pfn(ZERO_PAGE(0));
135 early_initcall(init_zero_pfn);
138 #if defined(SPLIT_RSS_COUNTING)
140 void sync_mm_rss(struct mm_struct *mm)
144 for (i = 0; i < NR_MM_COUNTERS; i++) {
145 if (current->rss_stat.count[i]) {
146 add_mm_counter(mm, i, current->rss_stat.count[i]);
147 current->rss_stat.count[i] = 0;
150 current->rss_stat.events = 0;
153 static void add_mm_counter_fast(struct mm_struct *mm, int member, int val)
155 struct task_struct *task = current;
157 if (likely(task->mm == mm))
158 task->rss_stat.count[member] += val;
160 add_mm_counter(mm, member, val);
162 #define inc_mm_counter_fast(mm, member) add_mm_counter_fast(mm, member, 1)
163 #define dec_mm_counter_fast(mm, member) add_mm_counter_fast(mm, member, -1)
165 /* sync counter once per 64 page faults */
166 #define TASK_RSS_EVENTS_THRESH (64)
167 static void check_sync_rss_stat(struct task_struct *task)
169 if (unlikely(task != current))
171 if (unlikely(task->rss_stat.events++ > TASK_RSS_EVENTS_THRESH))
172 sync_mm_rss(task->mm);
174 #else /* SPLIT_RSS_COUNTING */
176 #define inc_mm_counter_fast(mm, member) inc_mm_counter(mm, member)
177 #define dec_mm_counter_fast(mm, member) dec_mm_counter(mm, member)
179 static void check_sync_rss_stat(struct task_struct *task)
183 #endif /* SPLIT_RSS_COUNTING */
185 #ifdef HAVE_GENERIC_MMU_GATHER
187 static bool tlb_next_batch(struct mmu_gather *tlb)
189 struct mmu_gather_batch *batch;
193 tlb->active = batch->next;
197 if (tlb->batch_count == MAX_GATHER_BATCH_COUNT)
200 batch = (void *)__get_free_pages(GFP_NOWAIT | __GFP_NOWARN, 0);
207 batch->max = MAX_GATHER_BATCH;
209 tlb->active->next = batch;
216 * Called to initialize an (on-stack) mmu_gather structure for page-table
217 * tear-down from @mm. The @fullmm argument is used when @mm is without
218 * users and we're going to destroy the full address space (exit/execve).
220 void tlb_gather_mmu(struct mmu_gather *tlb, struct mm_struct *mm, unsigned long start, unsigned long end)
224 /* Is it from 0 to ~0? */
225 tlb->fullmm = !(start | (end+1));
226 tlb->need_flush_all = 0;
227 tlb->local.next = NULL;
229 tlb->local.max = ARRAY_SIZE(tlb->__pages);
230 tlb->active = &tlb->local;
231 tlb->batch_count = 0;
233 #ifdef CONFIG_HAVE_RCU_TABLE_FREE
238 __tlb_reset_range(tlb);
241 static void tlb_flush_mmu_tlbonly(struct mmu_gather *tlb)
247 mmu_notifier_invalidate_range(tlb->mm, tlb->start, tlb->end);
248 #ifdef CONFIG_HAVE_RCU_TABLE_FREE
249 tlb_table_flush(tlb);
251 __tlb_reset_range(tlb);
254 static void tlb_flush_mmu_free(struct mmu_gather *tlb)
256 struct mmu_gather_batch *batch;
258 for (batch = &tlb->local; batch && batch->nr; batch = batch->next) {
259 free_pages_and_swap_cache(batch->pages, batch->nr);
262 tlb->active = &tlb->local;
265 void tlb_flush_mmu(struct mmu_gather *tlb)
267 tlb_flush_mmu_tlbonly(tlb);
268 tlb_flush_mmu_free(tlb);
272 * Called at the end of the shootdown operation to free up any resources
273 * that were required.
275 void tlb_finish_mmu(struct mmu_gather *tlb, unsigned long start, unsigned long end)
277 struct mmu_gather_batch *batch, *next;
281 /* keep the page table cache within bounds */
284 for (batch = tlb->local.next; batch; batch = next) {
286 free_pages((unsigned long)batch, 0);
288 tlb->local.next = NULL;
292 * Must perform the equivalent to __free_pte(pte_get_and_clear(ptep)), while
293 * handling the additional races in SMP caused by other CPUs caching valid
294 * mappings in their TLBs. Returns the number of free page slots left.
295 * When out of page slots we must call tlb_flush_mmu().
296 *returns true if the caller should flush.
298 bool __tlb_remove_page_size(struct mmu_gather *tlb, struct page *page, int page_size)
300 struct mmu_gather_batch *batch;
302 VM_BUG_ON(!tlb->end);
305 tlb->page_size = page_size;
307 if (page_size != tlb->page_size)
312 if (batch->nr == batch->max) {
313 if (!tlb_next_batch(tlb))
317 VM_BUG_ON_PAGE(batch->nr > batch->max, page);
319 batch->pages[batch->nr++] = page;
323 void tlb_flush_pmd_range(struct mmu_gather *tlb, unsigned long address,
326 if (tlb->page_size != 0 && tlb->page_size != PMD_SIZE)
329 tlb->page_size = PMD_SIZE;
330 tlb->start = min(tlb->start, address);
331 tlb->end = max(tlb->end, address + size);
333 * Track the last address with which we adjusted the range. This
334 * will be used later to adjust again after a mmu_flush due to
335 * failed __tlb_remove_page
337 tlb->addr = address + size - PMD_SIZE;
339 #endif /* HAVE_GENERIC_MMU_GATHER */
341 #ifdef CONFIG_HAVE_RCU_TABLE_FREE
344 * See the comment near struct mmu_table_batch.
347 static void tlb_remove_table_smp_sync(void *arg)
349 /* Simply deliver the interrupt */
352 static void tlb_remove_table_one(void *table)
355 * This isn't an RCU grace period and hence the page-tables cannot be
356 * assumed to be actually RCU-freed.
358 * It is however sufficient for software page-table walkers that rely on
359 * IRQ disabling. See the comment near struct mmu_table_batch.
361 smp_call_function(tlb_remove_table_smp_sync, NULL, 1);
362 __tlb_remove_table(table);
365 static void tlb_remove_table_rcu(struct rcu_head *head)
367 struct mmu_table_batch *batch;
370 batch = container_of(head, struct mmu_table_batch, rcu);
372 for (i = 0; i < batch->nr; i++)
373 __tlb_remove_table(batch->tables[i]);
375 free_page((unsigned long)batch);
378 void tlb_table_flush(struct mmu_gather *tlb)
380 struct mmu_table_batch **batch = &tlb->batch;
383 call_rcu_sched(&(*batch)->rcu, tlb_remove_table_rcu);
388 void tlb_remove_table(struct mmu_gather *tlb, void *table)
390 struct mmu_table_batch **batch = &tlb->batch;
392 if (*batch == NULL) {
393 *batch = (struct mmu_table_batch *)__get_free_page(GFP_NOWAIT | __GFP_NOWARN);
394 if (*batch == NULL) {
395 tlb_remove_table_one(table);
400 (*batch)->tables[(*batch)->nr++] = table;
401 if ((*batch)->nr == MAX_TABLE_BATCH)
402 tlb_table_flush(tlb);
405 #endif /* CONFIG_HAVE_RCU_TABLE_FREE */
408 * Note: this doesn't free the actual pages themselves. That
409 * has been handled earlier when unmapping all the memory regions.
411 static void free_pte_range(struct mmu_gather *tlb, pmd_t *pmd,
414 pgtable_t token = pmd_pgtable(*pmd);
416 pte_free_tlb(tlb, token, addr);
417 atomic_long_dec(&tlb->mm->nr_ptes);
420 static inline void free_pmd_range(struct mmu_gather *tlb, pud_t *pud,
421 unsigned long addr, unsigned long end,
422 unsigned long floor, unsigned long ceiling)
429 pmd = pmd_offset(pud, addr);
431 next = pmd_addr_end(addr, end);
432 if (pmd_none_or_clear_bad(pmd))
434 free_pte_range(tlb, pmd, addr);
435 } while (pmd++, addr = next, addr != end);
445 if (end - 1 > ceiling - 1)
448 pmd = pmd_offset(pud, start);
450 pmd_free_tlb(tlb, pmd, start);
451 mm_dec_nr_pmds(tlb->mm);
454 static inline void free_pud_range(struct mmu_gather *tlb, pgd_t *pgd,
455 unsigned long addr, unsigned long end,
456 unsigned long floor, unsigned long ceiling)
463 pud = pud_offset(pgd, addr);
465 next = pud_addr_end(addr, end);
466 if (pud_none_or_clear_bad(pud))
468 free_pmd_range(tlb, pud, addr, next, floor, ceiling);
469 } while (pud++, addr = next, addr != end);
475 ceiling &= PGDIR_MASK;
479 if (end - 1 > ceiling - 1)
482 pud = pud_offset(pgd, start);
484 pud_free_tlb(tlb, pud, start);
488 * This function frees user-level page tables of a process.
490 void free_pgd_range(struct mmu_gather *tlb,
491 unsigned long addr, unsigned long end,
492 unsigned long floor, unsigned long ceiling)
498 * The next few lines have given us lots of grief...
500 * Why are we testing PMD* at this top level? Because often
501 * there will be no work to do at all, and we'd prefer not to
502 * go all the way down to the bottom just to discover that.
504 * Why all these "- 1"s? Because 0 represents both the bottom
505 * of the address space and the top of it (using -1 for the
506 * top wouldn't help much: the masks would do the wrong thing).
507 * The rule is that addr 0 and floor 0 refer to the bottom of
508 * the address space, but end 0 and ceiling 0 refer to the top
509 * Comparisons need to use "end - 1" and "ceiling - 1" (though
510 * that end 0 case should be mythical).
512 * Wherever addr is brought up or ceiling brought down, we must
513 * be careful to reject "the opposite 0" before it confuses the
514 * subsequent tests. But what about where end is brought down
515 * by PMD_SIZE below? no, end can't go down to 0 there.
517 * Whereas we round start (addr) and ceiling down, by different
518 * masks at different levels, in order to test whether a table
519 * now has no other vmas using it, so can be freed, we don't
520 * bother to round floor or end up - the tests don't need that.
534 if (end - 1 > ceiling - 1)
539 pgd = pgd_offset(tlb->mm, addr);
541 next = pgd_addr_end(addr, end);
542 if (pgd_none_or_clear_bad(pgd))
544 free_pud_range(tlb, pgd, addr, next, floor, ceiling);
545 } while (pgd++, addr = next, addr != end);
548 void free_pgtables(struct mmu_gather *tlb, struct vm_area_struct *vma,
549 unsigned long floor, unsigned long ceiling)
552 struct vm_area_struct *next = vma->vm_next;
553 unsigned long addr = vma->vm_start;
556 * Hide vma from rmap and truncate_pagecache before freeing
559 unlink_anon_vmas(vma);
560 unlink_file_vma(vma);
562 if (is_vm_hugetlb_page(vma)) {
563 hugetlb_free_pgd_range(tlb, addr, vma->vm_end,
564 floor, next? next->vm_start: ceiling);
567 * Optimization: gather nearby vmas into one call down
569 while (next && next->vm_start <= vma->vm_end + PMD_SIZE
570 && !is_vm_hugetlb_page(next)) {
573 unlink_anon_vmas(vma);
574 unlink_file_vma(vma);
576 free_pgd_range(tlb, addr, vma->vm_end,
577 floor, next? next->vm_start: ceiling);
583 int __pte_alloc(struct mm_struct *mm, pmd_t *pmd, unsigned long address)
586 pgtable_t new = pte_alloc_one(mm, address);
591 * Ensure all pte setup (eg. pte page lock and page clearing) are
592 * visible before the pte is made visible to other CPUs by being
593 * put into page tables.
595 * The other side of the story is the pointer chasing in the page
596 * table walking code (when walking the page table without locking;
597 * ie. most of the time). Fortunately, these data accesses consist
598 * of a chain of data-dependent loads, meaning most CPUs (alpha
599 * being the notable exception) will already guarantee loads are
600 * seen in-order. See the alpha page table accessors for the
601 * smp_read_barrier_depends() barriers in page table walking code.
603 smp_wmb(); /* Could be smp_wmb__xxx(before|after)_spin_lock */
605 ptl = pmd_lock(mm, pmd);
606 if (likely(pmd_none(*pmd))) { /* Has another populated it ? */
607 atomic_long_inc(&mm->nr_ptes);
608 pmd_populate(mm, pmd, new);
617 int __pte_alloc_kernel(pmd_t *pmd, unsigned long address)
619 pte_t *new = pte_alloc_one_kernel(&init_mm, address);
623 smp_wmb(); /* See comment in __pte_alloc */
625 spin_lock(&init_mm.page_table_lock);
626 if (likely(pmd_none(*pmd))) { /* Has another populated it ? */
627 pmd_populate_kernel(&init_mm, pmd, new);
630 spin_unlock(&init_mm.page_table_lock);
632 pte_free_kernel(&init_mm, new);
636 static inline void init_rss_vec(int *rss)
638 memset(rss, 0, sizeof(int) * NR_MM_COUNTERS);
641 static inline void add_mm_rss_vec(struct mm_struct *mm, int *rss)
645 if (current->mm == mm)
647 for (i = 0; i < NR_MM_COUNTERS; i++)
649 add_mm_counter(mm, i, rss[i]);
653 * This function is called to print an error when a bad pte
654 * is found. For example, we might have a PFN-mapped pte in
655 * a region that doesn't allow it.
657 * The calling function must still handle the error.
659 static void print_bad_pte(struct vm_area_struct *vma, unsigned long addr,
660 pte_t pte, struct page *page)
662 pgd_t *pgd = pgd_offset(vma->vm_mm, addr);
663 pud_t *pud = pud_offset(pgd, addr);
664 pmd_t *pmd = pmd_offset(pud, addr);
665 struct address_space *mapping;
667 static unsigned long resume;
668 static unsigned long nr_shown;
669 static unsigned long nr_unshown;
672 * Allow a burst of 60 reports, then keep quiet for that minute;
673 * or allow a steady drip of one report per second.
675 if (nr_shown == 60) {
676 if (time_before(jiffies, resume)) {
681 pr_alert("BUG: Bad page map: %lu messages suppressed\n",
688 resume = jiffies + 60 * HZ;
690 mapping = vma->vm_file ? vma->vm_file->f_mapping : NULL;
691 index = linear_page_index(vma, addr);
693 pr_alert("BUG: Bad page map in process %s pte:%08llx pmd:%08llx\n",
695 (long long)pte_val(pte), (long long)pmd_val(*pmd));
697 dump_page(page, "bad pte");
698 pr_alert("addr:%p vm_flags:%08lx anon_vma:%p mapping:%p index:%lx\n",
699 (void *)addr, vma->vm_flags, vma->anon_vma, mapping, index);
701 * Choose text because data symbols depend on CONFIG_KALLSYMS_ALL=y
703 pr_alert("file:%pD fault:%pf mmap:%pf readpage:%pf\n",
705 vma->vm_ops ? vma->vm_ops->fault : NULL,
706 vma->vm_file ? vma->vm_file->f_op->mmap : NULL,
707 mapping ? mapping->a_ops->readpage : NULL);
709 add_taint(TAINT_BAD_PAGE, LOCKDEP_NOW_UNRELIABLE);
713 * vm_normal_page -- This function gets the "struct page" associated with a pte.
715 * "Special" mappings do not wish to be associated with a "struct page" (either
716 * it doesn't exist, or it exists but they don't want to touch it). In this
717 * case, NULL is returned here. "Normal" mappings do have a struct page.
719 * There are 2 broad cases. Firstly, an architecture may define a pte_special()
720 * pte bit, in which case this function is trivial. Secondly, an architecture
721 * may not have a spare pte bit, which requires a more complicated scheme,
724 * A raw VM_PFNMAP mapping (ie. one that is not COWed) is always considered a
725 * special mapping (even if there are underlying and valid "struct pages").
726 * COWed pages of a VM_PFNMAP are always normal.
728 * The way we recognize COWed pages within VM_PFNMAP mappings is through the
729 * rules set up by "remap_pfn_range()": the vma will have the VM_PFNMAP bit
730 * set, and the vm_pgoff will point to the first PFN mapped: thus every special
731 * mapping will always honor the rule
733 * pfn_of_page == vma->vm_pgoff + ((addr - vma->vm_start) >> PAGE_SHIFT)
735 * And for normal mappings this is false.
737 * This restricts such mappings to be a linear translation from virtual address
738 * to pfn. To get around this restriction, we allow arbitrary mappings so long
739 * as the vma is not a COW mapping; in that case, we know that all ptes are
740 * special (because none can have been COWed).
743 * In order to support COW of arbitrary special mappings, we have VM_MIXEDMAP.
745 * VM_MIXEDMAP mappings can likewise contain memory with or without "struct
746 * page" backing, however the difference is that _all_ pages with a struct
747 * page (that is, those where pfn_valid is true) are refcounted and considered
748 * normal pages by the VM. The disadvantage is that pages are refcounted
749 * (which can be slower and simply not an option for some PFNMAP users). The
750 * advantage is that we don't have to follow the strict linearity rule of
751 * PFNMAP mappings in order to support COWable mappings.
754 #ifdef __HAVE_ARCH_PTE_SPECIAL
755 # define HAVE_PTE_SPECIAL 1
757 # define HAVE_PTE_SPECIAL 0
759 struct page *vm_normal_page(struct vm_area_struct *vma, unsigned long addr,
762 unsigned long pfn = pte_pfn(pte);
764 if (HAVE_PTE_SPECIAL) {
765 if (likely(!pte_special(pte)))
767 if (vma->vm_ops && vma->vm_ops->find_special_page)
768 return vma->vm_ops->find_special_page(vma, addr);
769 if (vma->vm_flags & (VM_PFNMAP | VM_MIXEDMAP))
771 if (!is_zero_pfn(pfn))
772 print_bad_pte(vma, addr, pte, NULL);
776 /* !HAVE_PTE_SPECIAL case follows: */
778 if (unlikely(vma->vm_flags & (VM_PFNMAP|VM_MIXEDMAP))) {
779 if (vma->vm_flags & VM_MIXEDMAP) {
785 off = (addr - vma->vm_start) >> PAGE_SHIFT;
786 if (pfn == vma->vm_pgoff + off)
788 if (!is_cow_mapping(vma->vm_flags))
793 if (is_zero_pfn(pfn))
796 if (unlikely(pfn > highest_memmap_pfn)) {
797 print_bad_pte(vma, addr, pte, NULL);
802 * NOTE! We still have PageReserved() pages in the page tables.
803 * eg. VDSO mappings can cause them to exist.
806 return pfn_to_page(pfn);
809 #ifdef CONFIG_TRANSPARENT_HUGEPAGE
810 struct page *vm_normal_page_pmd(struct vm_area_struct *vma, unsigned long addr,
813 unsigned long pfn = pmd_pfn(pmd);
816 * There is no pmd_special() but there may be special pmds, e.g.
817 * in a direct-access (dax) mapping, so let's just replicate the
818 * !HAVE_PTE_SPECIAL case from vm_normal_page() here.
820 if (unlikely(vma->vm_flags & (VM_PFNMAP|VM_MIXEDMAP))) {
821 if (vma->vm_flags & VM_MIXEDMAP) {
827 off = (addr - vma->vm_start) >> PAGE_SHIFT;
828 if (pfn == vma->vm_pgoff + off)
830 if (!is_cow_mapping(vma->vm_flags))
835 if (is_zero_pfn(pfn))
837 if (unlikely(pfn > highest_memmap_pfn))
841 * NOTE! We still have PageReserved() pages in the page tables.
842 * eg. VDSO mappings can cause them to exist.
845 return pfn_to_page(pfn);
850 * copy one vm_area from one task to the other. Assumes the page tables
851 * already present in the new task to be cleared in the whole range
852 * covered by this vma.
855 static inline unsigned long
856 copy_one_pte(struct mm_struct *dst_mm, struct mm_struct *src_mm,
857 pte_t *dst_pte, pte_t *src_pte, struct vm_area_struct *vma,
858 unsigned long addr, int *rss)
860 unsigned long vm_flags = vma->vm_flags;
861 pte_t pte = *src_pte;
864 /* pte contains position in swap or file, so copy. */
865 if (unlikely(!pte_present(pte))) {
866 swp_entry_t entry = pte_to_swp_entry(pte);
868 if (likely(!non_swap_entry(entry))) {
869 if (swap_duplicate(entry) < 0)
872 /* make sure dst_mm is on swapoff's mmlist. */
873 if (unlikely(list_empty(&dst_mm->mmlist))) {
874 spin_lock(&mmlist_lock);
875 if (list_empty(&dst_mm->mmlist))
876 list_add(&dst_mm->mmlist,
878 spin_unlock(&mmlist_lock);
881 } else if (is_migration_entry(entry)) {
882 page = migration_entry_to_page(entry);
884 rss[mm_counter(page)]++;
886 if (is_write_migration_entry(entry) &&
887 is_cow_mapping(vm_flags)) {
889 * COW mappings require pages in both
890 * parent and child to be set to read.
892 make_migration_entry_read(&entry);
893 pte = swp_entry_to_pte(entry);
894 if (pte_swp_soft_dirty(*src_pte))
895 pte = pte_swp_mksoft_dirty(pte);
896 set_pte_at(src_mm, addr, src_pte, pte);
903 * If it's a COW mapping, write protect it both
904 * in the parent and the child
906 if (is_cow_mapping(vm_flags)) {
907 ptep_set_wrprotect(src_mm, addr, src_pte);
908 pte = pte_wrprotect(pte);
912 * If it's a shared mapping, mark it clean in
915 if (vm_flags & VM_SHARED)
916 pte = pte_mkclean(pte);
917 pte = pte_mkold(pte);
919 page = vm_normal_page(vma, addr, pte);
922 page_dup_rmap(page, false);
923 rss[mm_counter(page)]++;
927 set_pte_at(dst_mm, addr, dst_pte, pte);
931 static int copy_pte_range(struct mm_struct *dst_mm, struct mm_struct *src_mm,
932 pmd_t *dst_pmd, pmd_t *src_pmd, struct vm_area_struct *vma,
933 unsigned long addr, unsigned long end)
935 pte_t *orig_src_pte, *orig_dst_pte;
936 pte_t *src_pte, *dst_pte;
937 spinlock_t *src_ptl, *dst_ptl;
939 int rss[NR_MM_COUNTERS];
940 swp_entry_t entry = (swp_entry_t){0};
945 dst_pte = pte_alloc_map_lock(dst_mm, dst_pmd, addr, &dst_ptl);
948 src_pte = pte_offset_map(src_pmd, addr);
949 src_ptl = pte_lockptr(src_mm, src_pmd);
950 spin_lock_nested(src_ptl, SINGLE_DEPTH_NESTING);
951 orig_src_pte = src_pte;
952 orig_dst_pte = dst_pte;
953 arch_enter_lazy_mmu_mode();
957 * We are holding two locks at this point - either of them
958 * could generate latencies in another task on another CPU.
960 if (progress >= 32) {
962 if (need_resched() ||
963 spin_needbreak(src_ptl) || spin_needbreak(dst_ptl))
966 if (pte_none(*src_pte)) {
970 entry.val = copy_one_pte(dst_mm, src_mm, dst_pte, src_pte,
975 } while (dst_pte++, src_pte++, addr += PAGE_SIZE, addr != end);
977 arch_leave_lazy_mmu_mode();
978 spin_unlock(src_ptl);
979 pte_unmap(orig_src_pte);
980 add_mm_rss_vec(dst_mm, rss);
981 pte_unmap_unlock(orig_dst_pte, dst_ptl);
985 if (add_swap_count_continuation(entry, GFP_KERNEL) < 0)
994 static inline int copy_pmd_range(struct mm_struct *dst_mm, struct mm_struct *src_mm,
995 pud_t *dst_pud, pud_t *src_pud, struct vm_area_struct *vma,
996 unsigned long addr, unsigned long end)
998 pmd_t *src_pmd, *dst_pmd;
1001 dst_pmd = pmd_alloc(dst_mm, dst_pud, addr);
1004 src_pmd = pmd_offset(src_pud, addr);
1006 next = pmd_addr_end(addr, end);
1007 if (pmd_trans_huge(*src_pmd) || pmd_devmap(*src_pmd)) {
1009 VM_BUG_ON(next-addr != HPAGE_PMD_SIZE);
1010 err = copy_huge_pmd(dst_mm, src_mm,
1011 dst_pmd, src_pmd, addr, vma);
1018 if (pmd_none_or_clear_bad(src_pmd))
1020 if (copy_pte_range(dst_mm, src_mm, dst_pmd, src_pmd,
1023 } while (dst_pmd++, src_pmd++, addr = next, addr != end);
1027 static inline int copy_pud_range(struct mm_struct *dst_mm, struct mm_struct *src_mm,
1028 pgd_t *dst_pgd, pgd_t *src_pgd, struct vm_area_struct *vma,
1029 unsigned long addr, unsigned long end)
1031 pud_t *src_pud, *dst_pud;
1034 dst_pud = pud_alloc(dst_mm, dst_pgd, addr);
1037 src_pud = pud_offset(src_pgd, addr);
1039 next = pud_addr_end(addr, end);
1040 if (pud_none_or_clear_bad(src_pud))
1042 if (copy_pmd_range(dst_mm, src_mm, dst_pud, src_pud,
1045 } while (dst_pud++, src_pud++, addr = next, addr != end);
1049 int copy_page_range(struct mm_struct *dst_mm, struct mm_struct *src_mm,
1050 struct vm_area_struct *vma)
1052 pgd_t *src_pgd, *dst_pgd;
1054 unsigned long addr = vma->vm_start;
1055 unsigned long end = vma->vm_end;
1056 unsigned long mmun_start; /* For mmu_notifiers */
1057 unsigned long mmun_end; /* For mmu_notifiers */
1062 * Don't copy ptes where a page fault will fill them correctly.
1063 * Fork becomes much lighter when there are big shared or private
1064 * readonly mappings. The tradeoff is that copy_page_range is more
1065 * efficient than faulting.
1067 if (!(vma->vm_flags & (VM_HUGETLB | VM_PFNMAP | VM_MIXEDMAP)) &&
1071 if (is_vm_hugetlb_page(vma))
1072 return copy_hugetlb_page_range(dst_mm, src_mm, vma);
1074 if (unlikely(vma->vm_flags & VM_PFNMAP)) {
1076 * We do not free on error cases below as remove_vma
1077 * gets called on error from higher level routine
1079 ret = track_pfn_copy(vma);
1085 * We need to invalidate the secondary MMU mappings only when
1086 * there could be a permission downgrade on the ptes of the
1087 * parent mm. And a permission downgrade will only happen if
1088 * is_cow_mapping() returns true.
1090 is_cow = is_cow_mapping(vma->vm_flags);
1094 mmu_notifier_invalidate_range_start(src_mm, mmun_start,
1098 dst_pgd = pgd_offset(dst_mm, addr);
1099 src_pgd = pgd_offset(src_mm, addr);
1101 next = pgd_addr_end(addr, end);
1102 if (pgd_none_or_clear_bad(src_pgd))
1104 if (unlikely(copy_pud_range(dst_mm, src_mm, dst_pgd, src_pgd,
1105 vma, addr, next))) {
1109 } while (dst_pgd++, src_pgd++, addr = next, addr != end);
1112 mmu_notifier_invalidate_range_end(src_mm, mmun_start, mmun_end);
1116 /* Whether we should zap all COWed (private) pages too */
1117 static inline bool should_zap_cows(struct zap_details *details)
1119 /* By default, zap all pages */
1123 /* Or, we zap COWed pages only if the caller wants to */
1124 return !details->check_mapping;
1127 static unsigned long zap_pte_range(struct mmu_gather *tlb,
1128 struct vm_area_struct *vma, pmd_t *pmd,
1129 unsigned long addr, unsigned long end,
1130 struct zap_details *details)
1132 struct mm_struct *mm = tlb->mm;
1133 int force_flush = 0;
1134 int rss[NR_MM_COUNTERS];
1139 struct page *pending_page = NULL;
1143 start_pte = pte_offset_map_lock(mm, pmd, addr, &ptl);
1145 flush_tlb_batched_pending(mm);
1146 arch_enter_lazy_mmu_mode();
1149 if (pte_none(ptent)) {
1153 if (pte_present(ptent)) {
1156 page = vm_normal_page(vma, addr, ptent);
1157 if (unlikely(details) && page) {
1159 * unmap_shared_mapping_pages() wants to
1160 * invalidate cache without truncating:
1161 * unmap shared but keep private pages.
1163 if (details->check_mapping &&
1164 details->check_mapping != page_rmapping(page))
1167 ptent = ptep_get_and_clear_full(mm, addr, pte,
1169 tlb_remove_tlb_entry(tlb, pte, addr);
1170 if (unlikely(!page))
1173 if (!PageAnon(page)) {
1174 if (pte_dirty(ptent)) {
1176 * oom_reaper cannot tear down dirty
1179 if (unlikely(details && details->ignore_dirty))
1182 set_page_dirty(page);
1184 if (pte_young(ptent) &&
1185 likely(!(vma->vm_flags & VM_SEQ_READ)))
1186 mark_page_accessed(page);
1188 rss[mm_counter(page)]--;
1189 page_remove_rmap(page, false);
1190 if (unlikely(page_mapcount(page) < 0))
1191 print_bad_pte(vma, addr, ptent, page);
1192 if (unlikely(__tlb_remove_page(tlb, page))) {
1194 pending_page = page;
1201 entry = pte_to_swp_entry(ptent);
1202 if (!non_swap_entry(entry)) {
1203 /* Genuine swap entry, hence a private anon page */
1204 if (!should_zap_cows(details))
1207 } else if (is_migration_entry(entry)) {
1210 page = migration_entry_to_page(entry);
1211 if (details && details->check_mapping &&
1212 details->check_mapping != page_rmapping(page))
1214 rss[mm_counter(page)]--;
1216 if (unlikely(!free_swap_and_cache(entry)))
1217 print_bad_pte(vma, addr, ptent, NULL);
1218 pte_clear_not_present_full(mm, addr, pte, tlb->fullmm);
1219 } while (pte++, addr += PAGE_SIZE, addr != end);
1221 add_mm_rss_vec(mm, rss);
1222 arch_leave_lazy_mmu_mode();
1224 /* Do the actual TLB flush before dropping ptl */
1226 tlb_flush_mmu_tlbonly(tlb);
1227 pte_unmap_unlock(start_pte, ptl);
1230 * If we forced a TLB flush (either due to running out of
1231 * batch buffers or because we needed to flush dirty TLB
1232 * entries before releasing the ptl), free the batched
1233 * memory too. Restart if we didn't do everything.
1237 tlb_flush_mmu_free(tlb);
1239 /* remove the page with new size */
1240 __tlb_remove_pte_page(tlb, pending_page);
1241 pending_page = NULL;
1250 static inline unsigned long zap_pmd_range(struct mmu_gather *tlb,
1251 struct vm_area_struct *vma, pud_t *pud,
1252 unsigned long addr, unsigned long end,
1253 struct zap_details *details)
1258 pmd = pmd_offset(pud, addr);
1260 next = pmd_addr_end(addr, end);
1261 if (pmd_trans_huge(*pmd) || pmd_devmap(*pmd)) {
1262 if (next - addr != HPAGE_PMD_SIZE) {
1263 VM_BUG_ON_VMA(vma_is_anonymous(vma) &&
1264 !rwsem_is_locked(&tlb->mm->mmap_sem), vma);
1265 split_huge_pmd(vma, pmd, addr);
1266 } else if (zap_huge_pmd(tlb, vma, pmd, addr))
1271 * Here there can be other concurrent MADV_DONTNEED or
1272 * trans huge page faults running, and if the pmd is
1273 * none or trans huge it can change under us. This is
1274 * because MADV_DONTNEED holds the mmap_sem in read
1277 if (pmd_none_or_trans_huge_or_clear_bad(pmd))
1279 next = zap_pte_range(tlb, vma, pmd, addr, next, details);
1282 } while (pmd++, addr = next, addr != end);
1287 static inline unsigned long zap_pud_range(struct mmu_gather *tlb,
1288 struct vm_area_struct *vma, pgd_t *pgd,
1289 unsigned long addr, unsigned long end,
1290 struct zap_details *details)
1295 pud = pud_offset(pgd, addr);
1297 next = pud_addr_end(addr, end);
1298 if (pud_none_or_clear_bad(pud))
1300 next = zap_pmd_range(tlb, vma, pud, addr, next, details);
1301 } while (pud++, addr = next, addr != end);
1306 void unmap_page_range(struct mmu_gather *tlb,
1307 struct vm_area_struct *vma,
1308 unsigned long addr, unsigned long end,
1309 struct zap_details *details)
1314 BUG_ON(addr >= end);
1315 tlb_start_vma(tlb, vma);
1316 pgd = pgd_offset(vma->vm_mm, addr);
1318 next = pgd_addr_end(addr, end);
1319 if (pgd_none_or_clear_bad(pgd))
1321 next = zap_pud_range(tlb, vma, pgd, addr, next, details);
1322 } while (pgd++, addr = next, addr != end);
1323 tlb_end_vma(tlb, vma);
1327 static void unmap_single_vma(struct mmu_gather *tlb,
1328 struct vm_area_struct *vma, unsigned long start_addr,
1329 unsigned long end_addr,
1330 struct zap_details *details)
1332 unsigned long start = max(vma->vm_start, start_addr);
1335 if (start >= vma->vm_end)
1337 end = min(vma->vm_end, end_addr);
1338 if (end <= vma->vm_start)
1342 uprobe_munmap(vma, start, end);
1344 if (unlikely(vma->vm_flags & VM_PFNMAP))
1345 untrack_pfn(vma, 0, 0);
1348 if (unlikely(is_vm_hugetlb_page(vma))) {
1350 * It is undesirable to test vma->vm_file as it
1351 * should be non-null for valid hugetlb area.
1352 * However, vm_file will be NULL in the error
1353 * cleanup path of mmap_region. When
1354 * hugetlbfs ->mmap method fails,
1355 * mmap_region() nullifies vma->vm_file
1356 * before calling this function to clean up.
1357 * Since no pte has actually been setup, it is
1358 * safe to do nothing in this case.
1361 i_mmap_lock_write(vma->vm_file->f_mapping);
1362 __unmap_hugepage_range_final(tlb, vma, start, end, NULL);
1363 i_mmap_unlock_write(vma->vm_file->f_mapping);
1366 unmap_page_range(tlb, vma, start, end, details);
1371 * unmap_vmas - unmap a range of memory covered by a list of vma's
1372 * @tlb: address of the caller's struct mmu_gather
1373 * @vma: the starting vma
1374 * @start_addr: virtual address at which to start unmapping
1375 * @end_addr: virtual address at which to end unmapping
1377 * Unmap all pages in the vma list.
1379 * Only addresses between `start' and `end' will be unmapped.
1381 * The VMA list must be sorted in ascending virtual address order.
1383 * unmap_vmas() assumes that the caller will flush the whole unmapped address
1384 * range after unmap_vmas() returns. So the only responsibility here is to
1385 * ensure that any thus-far unmapped pages are flushed before unmap_vmas()
1386 * drops the lock and schedules.
1388 void unmap_vmas(struct mmu_gather *tlb,
1389 struct vm_area_struct *vma, unsigned long start_addr,
1390 unsigned long end_addr)
1392 struct mm_struct *mm = vma->vm_mm;
1394 mmu_notifier_invalidate_range_start(mm, start_addr, end_addr);
1395 for ( ; vma && vma->vm_start < end_addr; vma = vma->vm_next)
1396 unmap_single_vma(tlb, vma, start_addr, end_addr, NULL);
1397 mmu_notifier_invalidate_range_end(mm, start_addr, end_addr);
1401 * zap_page_range - remove user pages in a given range
1402 * @vma: vm_area_struct holding the applicable pages
1403 * @start: starting address of pages to zap
1404 * @size: number of bytes to zap
1405 * @details: details of shared cache invalidation
1407 * Caller must protect the VMA list
1409 void zap_page_range(struct vm_area_struct *vma, unsigned long start,
1410 unsigned long size, struct zap_details *details)
1412 struct mm_struct *mm = vma->vm_mm;
1413 struct mmu_gather tlb;
1414 unsigned long end = start + size;
1417 tlb_gather_mmu(&tlb, mm, start, end);
1418 update_hiwater_rss(mm);
1419 mmu_notifier_invalidate_range_start(mm, start, end);
1420 for ( ; vma && vma->vm_start < end; vma = vma->vm_next)
1421 unmap_single_vma(&tlb, vma, start, end, details);
1422 mmu_notifier_invalidate_range_end(mm, start, end);
1423 tlb_finish_mmu(&tlb, start, end);
1427 * zap_page_range_single - remove user pages in a given range
1428 * @vma: vm_area_struct holding the applicable pages
1429 * @address: starting address of pages to zap
1430 * @size: number of bytes to zap
1431 * @details: details of shared cache invalidation
1433 * The range must fit into one VMA.
1435 static void zap_page_range_single(struct vm_area_struct *vma, unsigned long address,
1436 unsigned long size, struct zap_details *details)
1438 struct mm_struct *mm = vma->vm_mm;
1439 struct mmu_gather tlb;
1440 unsigned long end = address + size;
1443 tlb_gather_mmu(&tlb, mm, address, end);
1444 update_hiwater_rss(mm);
1445 mmu_notifier_invalidate_range_start(mm, address, end);
1446 unmap_single_vma(&tlb, vma, address, end, details);
1447 mmu_notifier_invalidate_range_end(mm, address, end);
1448 tlb_finish_mmu(&tlb, address, end);
1452 * zap_vma_ptes - remove ptes mapping the vma
1453 * @vma: vm_area_struct holding ptes to be zapped
1454 * @address: starting address of pages to zap
1455 * @size: number of bytes to zap
1457 * This function only unmaps ptes assigned to VM_PFNMAP vmas.
1459 * The entire address range must be fully contained within the vma.
1461 * Returns 0 if successful.
1463 int zap_vma_ptes(struct vm_area_struct *vma, unsigned long address,
1466 if (address < vma->vm_start || address + size > vma->vm_end ||
1467 !(vma->vm_flags & VM_PFNMAP))
1469 zap_page_range_single(vma, address, size, NULL);
1472 EXPORT_SYMBOL_GPL(zap_vma_ptes);
1474 pte_t *__get_locked_pte(struct mm_struct *mm, unsigned long addr,
1477 pgd_t * pgd = pgd_offset(mm, addr);
1478 pud_t * pud = pud_alloc(mm, pgd, addr);
1480 pmd_t * pmd = pmd_alloc(mm, pud, addr);
1482 VM_BUG_ON(pmd_trans_huge(*pmd));
1483 return pte_alloc_map_lock(mm, pmd, addr, ptl);
1490 * This is the old fallback for page remapping.
1492 * For historical reasons, it only allows reserved pages. Only
1493 * old drivers should use this, and they needed to mark their
1494 * pages reserved for the old functions anyway.
1496 static int insert_page(struct vm_area_struct *vma, unsigned long addr,
1497 struct page *page, pgprot_t prot)
1499 struct mm_struct *mm = vma->vm_mm;
1508 flush_dcache_page(page);
1509 pte = get_locked_pte(mm, addr, &ptl);
1513 if (!pte_none(*pte))
1516 /* Ok, finally just insert the thing.. */
1518 inc_mm_counter_fast(mm, mm_counter_file(page));
1519 page_add_file_rmap(page, false);
1520 set_pte_at(mm, addr, pte, mk_pte(page, prot));
1523 pte_unmap_unlock(pte, ptl);
1526 pte_unmap_unlock(pte, ptl);
1532 * vm_insert_page - insert single page into user vma
1533 * @vma: user vma to map to
1534 * @addr: target user address of this page
1535 * @page: source kernel page
1537 * This allows drivers to insert individual pages they've allocated
1540 * The page has to be a nice clean _individual_ kernel allocation.
1541 * If you allocate a compound page, you need to have marked it as
1542 * such (__GFP_COMP), or manually just split the page up yourself
1543 * (see split_page()).
1545 * NOTE! Traditionally this was done with "remap_pfn_range()" which
1546 * took an arbitrary page protection parameter. This doesn't allow
1547 * that. Your vma protection will have to be set up correctly, which
1548 * means that if you want a shared writable mapping, you'd better
1549 * ask for a shared writable mapping!
1551 * The page does not need to be reserved.
1553 * Usually this function is called from f_op->mmap() handler
1554 * under mm->mmap_sem write-lock, so it can change vma->vm_flags.
1555 * Caller must set VM_MIXEDMAP on vma if it wants to call this
1556 * function from other places, for example from page-fault handler.
1558 int vm_insert_page(struct vm_area_struct *vma, unsigned long addr,
1561 if (addr < vma->vm_start || addr >= vma->vm_end)
1563 if (!page_count(page))
1565 if (!(vma->vm_flags & VM_MIXEDMAP)) {
1566 BUG_ON(down_read_trylock(&vma->vm_mm->mmap_sem));
1567 BUG_ON(vma->vm_flags & VM_PFNMAP);
1568 vma->vm_flags |= VM_MIXEDMAP;
1570 return insert_page(vma, addr, page, vma->vm_page_prot);
1572 EXPORT_SYMBOL(vm_insert_page);
1574 static int insert_pfn(struct vm_area_struct *vma, unsigned long addr,
1575 pfn_t pfn, pgprot_t prot)
1577 struct mm_struct *mm = vma->vm_mm;
1583 pte = get_locked_pte(mm, addr, &ptl);
1587 if (!pte_none(*pte))
1590 /* Ok, finally just insert the thing.. */
1591 if (pfn_t_devmap(pfn))
1592 entry = pte_mkdevmap(pfn_t_pte(pfn, prot));
1594 entry = pte_mkspecial(pfn_t_pte(pfn, prot));
1595 set_pte_at(mm, addr, pte, entry);
1596 update_mmu_cache(vma, addr, pte); /* XXX: why not for insert_page? */
1600 pte_unmap_unlock(pte, ptl);
1606 * vm_insert_pfn - insert single pfn into user vma
1607 * @vma: user vma to map to
1608 * @addr: target user address of this page
1609 * @pfn: source kernel pfn
1611 * Similar to vm_insert_page, this allows drivers to insert individual pages
1612 * they've allocated into a user vma. Same comments apply.
1614 * This function should only be called from a vm_ops->fault handler, and
1615 * in that case the handler should return NULL.
1617 * vma cannot be a COW mapping.
1619 * As this is called only for pages that do not currently exist, we
1620 * do not need to flush old virtual caches or the TLB.
1622 int vm_insert_pfn(struct vm_area_struct *vma, unsigned long addr,
1625 return vm_insert_pfn_prot(vma, addr, pfn, vma->vm_page_prot);
1627 EXPORT_SYMBOL(vm_insert_pfn);
1630 * vm_insert_pfn_prot - insert single pfn into user vma with specified pgprot
1631 * @vma: user vma to map to
1632 * @addr: target user address of this page
1633 * @pfn: source kernel pfn
1634 * @pgprot: pgprot flags for the inserted page
1636 * This is exactly like vm_insert_pfn, except that it allows drivers to
1637 * to override pgprot on a per-page basis.
1639 * This only makes sense for IO mappings, and it makes no sense for
1640 * cow mappings. In general, using multiple vmas is preferable;
1641 * vm_insert_pfn_prot should only be used if using multiple VMAs is
1644 int vm_insert_pfn_prot(struct vm_area_struct *vma, unsigned long addr,
1645 unsigned long pfn, pgprot_t pgprot)
1649 * Technically, architectures with pte_special can avoid all these
1650 * restrictions (same for remap_pfn_range). However we would like
1651 * consistency in testing and feature parity among all, so we should
1652 * try to keep these invariants in place for everybody.
1654 BUG_ON(!(vma->vm_flags & (VM_PFNMAP|VM_MIXEDMAP)));
1655 BUG_ON((vma->vm_flags & (VM_PFNMAP|VM_MIXEDMAP)) ==
1656 (VM_PFNMAP|VM_MIXEDMAP));
1657 BUG_ON((vma->vm_flags & VM_PFNMAP) && is_cow_mapping(vma->vm_flags));
1658 BUG_ON((vma->vm_flags & VM_MIXEDMAP) && pfn_valid(pfn));
1660 if (addr < vma->vm_start || addr >= vma->vm_end)
1662 if (track_pfn_insert(vma, &pgprot, __pfn_to_pfn_t(pfn, PFN_DEV)))
1665 if (!pfn_modify_allowed(pfn, pgprot))
1668 ret = insert_pfn(vma, addr, __pfn_to_pfn_t(pfn, PFN_DEV), pgprot);
1672 EXPORT_SYMBOL(vm_insert_pfn_prot);
1674 int vm_insert_mixed(struct vm_area_struct *vma, unsigned long addr,
1677 pgprot_t pgprot = vma->vm_page_prot;
1679 BUG_ON(!(vma->vm_flags & VM_MIXEDMAP));
1681 if (addr < vma->vm_start || addr >= vma->vm_end)
1683 if (track_pfn_insert(vma, &pgprot, pfn))
1686 if (!pfn_modify_allowed(pfn_t_to_pfn(pfn), pgprot))
1690 * If we don't have pte special, then we have to use the pfn_valid()
1691 * based VM_MIXEDMAP scheme (see vm_normal_page), and thus we *must*
1692 * refcount the page if pfn_valid is true (hence insert_page rather
1693 * than insert_pfn). If a zero_pfn were inserted into a VM_MIXEDMAP
1694 * without pte special, it would there be refcounted as a normal page.
1696 if (!HAVE_PTE_SPECIAL && !pfn_t_devmap(pfn) && pfn_t_valid(pfn)) {
1700 * At this point we are committed to insert_page()
1701 * regardless of whether the caller specified flags that
1702 * result in pfn_t_has_page() == false.
1704 page = pfn_to_page(pfn_t_to_pfn(pfn));
1705 return insert_page(vma, addr, page, pgprot);
1707 return insert_pfn(vma, addr, pfn, pgprot);
1709 EXPORT_SYMBOL(vm_insert_mixed);
1712 * maps a range of physical memory into the requested pages. the old
1713 * mappings are removed. any references to nonexistent pages results
1714 * in null mappings (currently treated as "copy-on-access")
1716 static int remap_pte_range(struct mm_struct *mm, pmd_t *pmd,
1717 unsigned long addr, unsigned long end,
1718 unsigned long pfn, pgprot_t prot)
1720 pte_t *pte, *mapped_pte;
1724 mapped_pte = pte = pte_alloc_map_lock(mm, pmd, addr, &ptl);
1727 arch_enter_lazy_mmu_mode();
1729 BUG_ON(!pte_none(*pte));
1730 if (!pfn_modify_allowed(pfn, prot)) {
1734 set_pte_at(mm, addr, pte, pte_mkspecial(pfn_pte(pfn, prot)));
1736 } while (pte++, addr += PAGE_SIZE, addr != end);
1737 arch_leave_lazy_mmu_mode();
1738 pte_unmap_unlock(mapped_pte, ptl);
1742 static inline int remap_pmd_range(struct mm_struct *mm, pud_t *pud,
1743 unsigned long addr, unsigned long end,
1744 unsigned long pfn, pgprot_t prot)
1750 pfn -= addr >> PAGE_SHIFT;
1751 pmd = pmd_alloc(mm, pud, addr);
1754 VM_BUG_ON(pmd_trans_huge(*pmd));
1756 next = pmd_addr_end(addr, end);
1757 err = remap_pte_range(mm, pmd, addr, next,
1758 pfn + (addr >> PAGE_SHIFT), prot);
1761 } while (pmd++, addr = next, addr != end);
1765 static inline int remap_pud_range(struct mm_struct *mm, pgd_t *pgd,
1766 unsigned long addr, unsigned long end,
1767 unsigned long pfn, pgprot_t prot)
1773 pfn -= addr >> PAGE_SHIFT;
1774 pud = pud_alloc(mm, pgd, addr);
1778 next = pud_addr_end(addr, end);
1779 err = remap_pmd_range(mm, pud, addr, next,
1780 pfn + (addr >> PAGE_SHIFT), prot);
1783 } while (pud++, addr = next, addr != end);
1788 * remap_pfn_range - remap kernel memory to userspace
1789 * @vma: user vma to map to
1790 * @addr: target user address to start at
1791 * @pfn: physical address of kernel memory
1792 * @size: size of map area
1793 * @prot: page protection flags for this mapping
1795 * Note: this is only safe if the mm semaphore is held when called.
1797 int remap_pfn_range(struct vm_area_struct *vma, unsigned long addr,
1798 unsigned long pfn, unsigned long size, pgprot_t prot)
1802 unsigned long end = addr + PAGE_ALIGN(size);
1803 struct mm_struct *mm = vma->vm_mm;
1804 unsigned long remap_pfn = pfn;
1808 * Physically remapped pages are special. Tell the
1809 * rest of the world about it:
1810 * VM_IO tells people not to look at these pages
1811 * (accesses can have side effects).
1812 * VM_PFNMAP tells the core MM that the base pages are just
1813 * raw PFN mappings, and do not have a "struct page" associated
1816 * Disable vma merging and expanding with mremap().
1818 * Omit vma from core dump, even when VM_IO turned off.
1820 * There's a horrible special case to handle copy-on-write
1821 * behaviour that some programs depend on. We mark the "original"
1822 * un-COW'ed pages by matching them up with "vma->vm_pgoff".
1823 * See vm_normal_page() for details.
1825 if (is_cow_mapping(vma->vm_flags)) {
1826 if (addr != vma->vm_start || end != vma->vm_end)
1828 vma->vm_pgoff = pfn;
1831 err = track_pfn_remap(vma, &prot, remap_pfn, addr, PAGE_ALIGN(size));
1835 vma->vm_flags |= VM_IO | VM_PFNMAP | VM_DONTEXPAND | VM_DONTDUMP;
1837 BUG_ON(addr >= end);
1838 pfn -= addr >> PAGE_SHIFT;
1839 pgd = pgd_offset(mm, addr);
1840 flush_cache_range(vma, addr, end);
1842 next = pgd_addr_end(addr, end);
1843 err = remap_pud_range(mm, pgd, addr, next,
1844 pfn + (addr >> PAGE_SHIFT), prot);
1847 } while (pgd++, addr = next, addr != end);
1850 untrack_pfn(vma, remap_pfn, PAGE_ALIGN(size));
1854 EXPORT_SYMBOL(remap_pfn_range);
1857 * vm_iomap_memory - remap memory to userspace
1858 * @vma: user vma to map to
1859 * @start: start of area
1860 * @len: size of area
1862 * This is a simplified io_remap_pfn_range() for common driver use. The
1863 * driver just needs to give us the physical memory range to be mapped,
1864 * we'll figure out the rest from the vma information.
1866 * NOTE! Some drivers might want to tweak vma->vm_page_prot first to get
1867 * whatever write-combining details or similar.
1869 int vm_iomap_memory(struct vm_area_struct *vma, phys_addr_t start, unsigned long len)
1871 unsigned long vm_len, pfn, pages;
1873 /* Check that the physical memory area passed in looks valid */
1874 if (start + len < start)
1877 * You *really* shouldn't map things that aren't page-aligned,
1878 * but we've historically allowed it because IO memory might
1879 * just have smaller alignment.
1881 len += start & ~PAGE_MASK;
1882 pfn = start >> PAGE_SHIFT;
1883 pages = (len + ~PAGE_MASK) >> PAGE_SHIFT;
1884 if (pfn + pages < pfn)
1887 /* We start the mapping 'vm_pgoff' pages into the area */
1888 if (vma->vm_pgoff > pages)
1890 pfn += vma->vm_pgoff;
1891 pages -= vma->vm_pgoff;
1893 /* Can we fit all of the mapping? */
1894 vm_len = vma->vm_end - vma->vm_start;
1895 if (vm_len >> PAGE_SHIFT > pages)
1898 /* Ok, let it rip */
1899 return io_remap_pfn_range(vma, vma->vm_start, pfn, vm_len, vma->vm_page_prot);
1901 EXPORT_SYMBOL(vm_iomap_memory);
1903 static int apply_to_pte_range(struct mm_struct *mm, pmd_t *pmd,
1904 unsigned long addr, unsigned long end,
1905 pte_fn_t fn, void *data)
1910 spinlock_t *uninitialized_var(ptl);
1912 pte = (mm == &init_mm) ?
1913 pte_alloc_kernel(pmd, addr) :
1914 pte_alloc_map_lock(mm, pmd, addr, &ptl);
1918 BUG_ON(pmd_huge(*pmd));
1920 arch_enter_lazy_mmu_mode();
1922 token = pmd_pgtable(*pmd);
1925 err = fn(pte++, token, addr, data);
1928 } while (addr += PAGE_SIZE, addr != end);
1930 arch_leave_lazy_mmu_mode();
1933 pte_unmap_unlock(pte-1, ptl);
1937 static int apply_to_pmd_range(struct mm_struct *mm, pud_t *pud,
1938 unsigned long addr, unsigned long end,
1939 pte_fn_t fn, void *data)
1945 BUG_ON(pud_huge(*pud));
1947 pmd = pmd_alloc(mm, pud, addr);
1951 next = pmd_addr_end(addr, end);
1952 err = apply_to_pte_range(mm, pmd, addr, next, fn, data);
1955 } while (pmd++, addr = next, addr != end);
1959 static int apply_to_pud_range(struct mm_struct *mm, pgd_t *pgd,
1960 unsigned long addr, unsigned long end,
1961 pte_fn_t fn, void *data)
1967 pud = pud_alloc(mm, pgd, addr);
1971 next = pud_addr_end(addr, end);
1972 err = apply_to_pmd_range(mm, pud, addr, next, fn, data);
1975 } while (pud++, addr = next, addr != end);
1980 * Scan a region of virtual memory, filling in page tables as necessary
1981 * and calling a provided function on each leaf page table.
1983 int apply_to_page_range(struct mm_struct *mm, unsigned long addr,
1984 unsigned long size, pte_fn_t fn, void *data)
1988 unsigned long end = addr + size;
1991 if (WARN_ON(addr >= end))
1994 pgd = pgd_offset(mm, addr);
1996 next = pgd_addr_end(addr, end);
1997 err = apply_to_pud_range(mm, pgd, addr, next, fn, data);
2000 } while (pgd++, addr = next, addr != end);
2004 EXPORT_SYMBOL_GPL(apply_to_page_range);
2007 * handle_pte_fault chooses page fault handler according to an entry which was
2008 * read non-atomically. Before making any commitment, on those architectures
2009 * or configurations (e.g. i386 with PAE) which might give a mix of unmatched
2010 * parts, do_swap_page must check under lock before unmapping the pte and
2011 * proceeding (but do_wp_page is only called after already making such a check;
2012 * and do_anonymous_page can safely check later on).
2014 static inline int pte_unmap_same(struct mm_struct *mm, pmd_t *pmd,
2015 pte_t *page_table, pte_t orig_pte)
2018 #if defined(CONFIG_SMP) || defined(CONFIG_PREEMPT)
2019 if (sizeof(pte_t) > sizeof(unsigned long)) {
2020 spinlock_t *ptl = pte_lockptr(mm, pmd);
2022 same = pte_same(*page_table, orig_pte);
2026 pte_unmap(page_table);
2030 static inline void cow_user_page(struct page *dst, struct page *src, unsigned long va, struct vm_area_struct *vma)
2032 debug_dma_assert_idle(src);
2035 * If the source page was a PFN mapping, we don't have
2036 * a "struct page" for it. We do a best-effort copy by
2037 * just copying from the original user address. If that
2038 * fails, we just zero-fill it. Live with it.
2040 if (unlikely(!src)) {
2041 void *kaddr = kmap_atomic(dst);
2042 void __user *uaddr = (void __user *)(va & PAGE_MASK);
2045 * This really shouldn't fail, because the page is there
2046 * in the page tables. But it might just be unreadable,
2047 * in which case we just give up and fill the result with
2050 if (__copy_from_user_inatomic(kaddr, uaddr, PAGE_SIZE))
2052 kunmap_atomic(kaddr);
2053 flush_dcache_page(dst);
2055 copy_user_highpage(dst, src, va, vma);
2058 static gfp_t __get_fault_gfp_mask(struct vm_area_struct *vma)
2060 struct file *vm_file = vma->vm_file;
2063 return mapping_gfp_mask(vm_file->f_mapping) | __GFP_FS | __GFP_IO;
2066 * Special mappings (e.g. VDSO) do not have any file so fake
2067 * a default GFP_KERNEL for them.
2073 * Notify the address space that the page is about to become writable so that
2074 * it can prohibit this or wait for the page to get into an appropriate state.
2076 * We do this without the lock held, so that it can sleep if it needs to.
2078 static int do_page_mkwrite(struct vm_area_struct *vma, struct page *page,
2079 unsigned long address)
2081 struct vm_fault vmf;
2084 vmf.virtual_address = (void __user *)(address & PAGE_MASK);
2085 vmf.pgoff = page->index;
2086 vmf.flags = FAULT_FLAG_WRITE|FAULT_FLAG_MKWRITE;
2087 vmf.gfp_mask = __get_fault_gfp_mask(vma);
2089 vmf.cow_page = NULL;
2091 ret = vma->vm_ops->page_mkwrite(vma, &vmf);
2092 if (unlikely(ret & (VM_FAULT_ERROR | VM_FAULT_NOPAGE)))
2094 if (unlikely(!(ret & VM_FAULT_LOCKED))) {
2096 if (!page->mapping) {
2098 return 0; /* retry */
2100 ret |= VM_FAULT_LOCKED;
2102 VM_BUG_ON_PAGE(!PageLocked(page), page);
2107 * Handle write page faults for pages that can be reused in the current vma
2109 * This can happen either due to the mapping being with the VM_SHARED flag,
2110 * or due to us being the last reference standing to the page. In either
2111 * case, all we need to do here is to mark the page as writable and update
2112 * any related book-keeping.
2114 static inline int wp_page_reuse(struct fault_env *fe, pte_t orig_pte,
2115 struct page *page, int page_mkwrite, int dirty_shared)
2118 struct vm_area_struct *vma = fe->vma;
2121 * Clear the pages cpupid information as the existing
2122 * information potentially belongs to a now completely
2123 * unrelated process.
2126 page_cpupid_xchg_last(page, (1 << LAST_CPUPID_SHIFT) - 1);
2128 flush_cache_page(vma, fe->address, pte_pfn(orig_pte));
2129 entry = pte_mkyoung(orig_pte);
2130 entry = maybe_mkwrite(pte_mkdirty(entry), vma);
2131 if (ptep_set_access_flags(vma, fe->address, fe->pte, entry, 1))
2132 update_mmu_cache(vma, fe->address, fe->pte);
2133 pte_unmap_unlock(fe->pte, fe->ptl);
2136 struct address_space *mapping;
2142 dirtied = set_page_dirty(page);
2143 VM_BUG_ON_PAGE(PageAnon(page), page);
2144 mapping = page->mapping;
2148 if ((dirtied || page_mkwrite) && mapping) {
2150 * Some device drivers do not set page.mapping
2151 * but still dirty their pages
2153 balance_dirty_pages_ratelimited(mapping);
2157 file_update_time(vma->vm_file);
2160 return VM_FAULT_WRITE;
2164 * Handle the case of a page which we actually need to copy to a new page.
2166 * Called with mmap_sem locked and the old page referenced, but
2167 * without the ptl held.
2169 * High level logic flow:
2171 * - Allocate a page, copy the content of the old page to the new one.
2172 * - Handle book keeping and accounting - cgroups, mmu-notifiers, etc.
2173 * - Take the PTL. If the pte changed, bail out and release the allocated page
2174 * - If the pte is still the way we remember it, update the page table and all
2175 * relevant references. This includes dropping the reference the page-table
2176 * held to the old page, as well as updating the rmap.
2177 * - In any case, unlock the PTL and drop the reference we took to the old page.
2179 static int wp_page_copy(struct fault_env *fe, pte_t orig_pte,
2180 struct page *old_page)
2182 struct vm_area_struct *vma = fe->vma;
2183 struct mm_struct *mm = vma->vm_mm;
2184 struct page *new_page = NULL;
2186 int page_copied = 0;
2187 const unsigned long mmun_start = fe->address & PAGE_MASK;
2188 const unsigned long mmun_end = mmun_start + PAGE_SIZE;
2189 struct mem_cgroup *memcg;
2191 if (unlikely(anon_vma_prepare(vma)))
2194 if (is_zero_pfn(pte_pfn(orig_pte))) {
2195 new_page = alloc_zeroed_user_highpage_movable(vma, fe->address);
2199 new_page = alloc_page_vma(GFP_HIGHUSER_MOVABLE, vma,
2203 cow_user_page(new_page, old_page, fe->address, vma);
2206 if (mem_cgroup_try_charge(new_page, mm, GFP_KERNEL, &memcg, false))
2209 __SetPageUptodate(new_page);
2211 mmu_notifier_invalidate_range_start(mm, mmun_start, mmun_end);
2214 * Re-check the pte - we dropped the lock
2216 fe->pte = pte_offset_map_lock(mm, fe->pmd, fe->address, &fe->ptl);
2217 if (likely(pte_same(*fe->pte, orig_pte))) {
2219 if (!PageAnon(old_page)) {
2220 dec_mm_counter_fast(mm,
2221 mm_counter_file(old_page));
2222 inc_mm_counter_fast(mm, MM_ANONPAGES);
2225 inc_mm_counter_fast(mm, MM_ANONPAGES);
2227 flush_cache_page(vma, fe->address, pte_pfn(orig_pte));
2228 entry = mk_pte(new_page, vma->vm_page_prot);
2229 entry = maybe_mkwrite(pte_mkdirty(entry), vma);
2231 * Clear the pte entry and flush it first, before updating the
2232 * pte with the new entry. This will avoid a race condition
2233 * seen in the presence of one thread doing SMC and another
2236 ptep_clear_flush_notify(vma, fe->address, fe->pte);
2237 page_add_new_anon_rmap(new_page, vma, fe->address, false);
2238 mem_cgroup_commit_charge(new_page, memcg, false, false);
2239 lru_cache_add_active_or_unevictable(new_page, vma);
2241 * We call the notify macro here because, when using secondary
2242 * mmu page tables (such as kvm shadow page tables), we want the
2243 * new page to be mapped directly into the secondary page table.
2245 set_pte_at_notify(mm, fe->address, fe->pte, entry);
2246 update_mmu_cache(vma, fe->address, fe->pte);
2249 * Only after switching the pte to the new page may
2250 * we remove the mapcount here. Otherwise another
2251 * process may come and find the rmap count decremented
2252 * before the pte is switched to the new page, and
2253 * "reuse" the old page writing into it while our pte
2254 * here still points into it and can be read by other
2257 * The critical issue is to order this
2258 * page_remove_rmap with the ptp_clear_flush above.
2259 * Those stores are ordered by (if nothing else,)
2260 * the barrier present in the atomic_add_negative
2261 * in page_remove_rmap.
2263 * Then the TLB flush in ptep_clear_flush ensures that
2264 * no process can access the old page before the
2265 * decremented mapcount is visible. And the old page
2266 * cannot be reused until after the decremented
2267 * mapcount is visible. So transitively, TLBs to
2268 * old page will be flushed before it can be reused.
2270 page_remove_rmap(old_page, false);
2273 /* Free the old page.. */
2274 new_page = old_page;
2277 mem_cgroup_cancel_charge(new_page, memcg, false);
2283 pte_unmap_unlock(fe->pte, fe->ptl);
2284 mmu_notifier_invalidate_range_end(mm, mmun_start, mmun_end);
2287 * Don't let another task, with possibly unlocked vma,
2288 * keep the mlocked page.
2290 if (page_copied && (vma->vm_flags & VM_LOCKED)) {
2291 lock_page(old_page); /* LRU manipulation */
2292 if (PageMlocked(old_page))
2293 munlock_vma_page(old_page);
2294 unlock_page(old_page);
2298 return page_copied ? VM_FAULT_WRITE : 0;
2304 return VM_FAULT_OOM;
2308 * Handle write page faults for VM_MIXEDMAP or VM_PFNMAP for a VM_SHARED
2311 static int wp_pfn_shared(struct fault_env *fe, pte_t orig_pte)
2313 struct vm_area_struct *vma = fe->vma;
2315 if (vma->vm_ops && vma->vm_ops->pfn_mkwrite) {
2316 struct vm_fault vmf = {
2318 .pgoff = linear_page_index(vma, fe->address),
2320 (void __user *)(fe->address & PAGE_MASK),
2321 .flags = FAULT_FLAG_WRITE | FAULT_FLAG_MKWRITE,
2325 pte_unmap_unlock(fe->pte, fe->ptl);
2326 ret = vma->vm_ops->pfn_mkwrite(vma, &vmf);
2327 if (ret & VM_FAULT_ERROR)
2329 fe->pte = pte_offset_map_lock(vma->vm_mm, fe->pmd, fe->address,
2332 * We might have raced with another page fault while we
2333 * released the pte_offset_map_lock.
2335 if (!pte_same(*fe->pte, orig_pte)) {
2336 pte_unmap_unlock(fe->pte, fe->ptl);
2340 return wp_page_reuse(fe, orig_pte, NULL, 0, 0);
2343 static int wp_page_shared(struct fault_env *fe, pte_t orig_pte,
2344 struct page *old_page)
2347 struct vm_area_struct *vma = fe->vma;
2348 int page_mkwrite = 0;
2352 if (vma->vm_ops && vma->vm_ops->page_mkwrite) {
2355 pte_unmap_unlock(fe->pte, fe->ptl);
2356 tmp = do_page_mkwrite(vma, old_page, fe->address);
2357 if (unlikely(!tmp || (tmp &
2358 (VM_FAULT_ERROR | VM_FAULT_NOPAGE)))) {
2363 * Since we dropped the lock we need to revalidate
2364 * the PTE as someone else may have changed it. If
2365 * they did, we just return, as we can count on the
2366 * MMU to tell us if they didn't also make it writable.
2368 fe->pte = pte_offset_map_lock(vma->vm_mm, fe->pmd, fe->address,
2370 if (!pte_same(*fe->pte, orig_pte)) {
2371 unlock_page(old_page);
2372 pte_unmap_unlock(fe->pte, fe->ptl);
2379 return wp_page_reuse(fe, orig_pte, old_page, page_mkwrite, 1);
2383 * This routine handles present pages, when users try to write
2384 * to a shared page. It is done by copying the page to a new address
2385 * and decrementing the shared-page counter for the old page.
2387 * Note that this routine assumes that the protection checks have been
2388 * done by the caller (the low-level page fault routine in most cases).
2389 * Thus we can safely just mark it writable once we've done any necessary
2392 * We also mark the page dirty at this point even though the page will
2393 * change only once the write actually happens. This avoids a few races,
2394 * and potentially makes it more efficient.
2396 * We enter with non-exclusive mmap_sem (to exclude vma changes,
2397 * but allow concurrent faults), with pte both mapped and locked.
2398 * We return with mmap_sem still held, but pte unmapped and unlocked.
2400 static int do_wp_page(struct fault_env *fe, pte_t orig_pte)
2403 struct vm_area_struct *vma = fe->vma;
2404 struct page *old_page;
2406 old_page = vm_normal_page(vma, fe->address, orig_pte);
2409 * VM_MIXEDMAP !pfn_valid() case, or VM_SOFTDIRTY clear on a
2412 * We should not cow pages in a shared writeable mapping.
2413 * Just mark the pages writable and/or call ops->pfn_mkwrite.
2415 if ((vma->vm_flags & (VM_WRITE|VM_SHARED)) ==
2416 (VM_WRITE|VM_SHARED))
2417 return wp_pfn_shared(fe, orig_pte);
2419 pte_unmap_unlock(fe->pte, fe->ptl);
2420 return wp_page_copy(fe, orig_pte, old_page);
2424 * Take out anonymous pages first, anonymous shared vmas are
2425 * not dirty accountable.
2427 if (PageAnon(old_page) && !PageKsm(old_page)) {
2429 if (!trylock_page(old_page)) {
2431 pte_unmap_unlock(fe->pte, fe->ptl);
2432 lock_page(old_page);
2433 fe->pte = pte_offset_map_lock(vma->vm_mm, fe->pmd,
2434 fe->address, &fe->ptl);
2435 if (!pte_same(*fe->pte, orig_pte)) {
2436 unlock_page(old_page);
2437 pte_unmap_unlock(fe->pte, fe->ptl);
2443 if (reuse_swap_page(old_page, &total_mapcount)) {
2444 if (total_mapcount == 1) {
2446 * The page is all ours. Move it to
2447 * our anon_vma so the rmap code will
2448 * not search our parent or siblings.
2449 * Protected against the rmap code by
2452 page_move_anon_rmap(old_page, vma);
2454 unlock_page(old_page);
2455 return wp_page_reuse(fe, orig_pte, old_page, 0, 0);
2457 unlock_page(old_page);
2458 } else if (unlikely((vma->vm_flags & (VM_WRITE|VM_SHARED)) ==
2459 (VM_WRITE|VM_SHARED))) {
2460 return wp_page_shared(fe, orig_pte, old_page);
2464 * Ok, we need to copy. Oh, well..
2468 pte_unmap_unlock(fe->pte, fe->ptl);
2469 return wp_page_copy(fe, orig_pte, old_page);
2472 static void unmap_mapping_range_vma(struct vm_area_struct *vma,
2473 unsigned long start_addr, unsigned long end_addr,
2474 struct zap_details *details)
2476 zap_page_range_single(vma, start_addr, end_addr - start_addr, details);
2479 static inline void unmap_mapping_range_tree(struct rb_root *root,
2480 struct zap_details *details)
2482 struct vm_area_struct *vma;
2483 pgoff_t vba, vea, zba, zea;
2485 vma_interval_tree_foreach(vma, root,
2486 details->first_index, details->last_index) {
2488 vba = vma->vm_pgoff;
2489 vea = vba + vma_pages(vma) - 1;
2490 zba = details->first_index;
2493 zea = details->last_index;
2497 unmap_mapping_range_vma(vma,
2498 ((zba - vba) << PAGE_SHIFT) + vma->vm_start,
2499 ((zea - vba + 1) << PAGE_SHIFT) + vma->vm_start,
2505 * unmap_mapping_range - unmap the portion of all mmaps in the specified
2506 * address_space corresponding to the specified page range in the underlying
2509 * @mapping: the address space containing mmaps to be unmapped.
2510 * @holebegin: byte in first page to unmap, relative to the start of
2511 * the underlying file. This will be rounded down to a PAGE_SIZE
2512 * boundary. Note that this is different from truncate_pagecache(), which
2513 * must keep the partial page. In contrast, we must get rid of
2515 * @holelen: size of prospective hole in bytes. This will be rounded
2516 * up to a PAGE_SIZE boundary. A holelen of zero truncates to the
2518 * @even_cows: 1 when truncating a file, unmap even private COWed pages;
2519 * but 0 when invalidating pagecache, don't throw away private data.
2521 void unmap_mapping_range(struct address_space *mapping,
2522 loff_t const holebegin, loff_t const holelen, int even_cows)
2524 struct zap_details details = { };
2525 pgoff_t hba = holebegin >> PAGE_SHIFT;
2526 pgoff_t hlen = (holelen + PAGE_SIZE - 1) >> PAGE_SHIFT;
2528 /* Check for overflow. */
2529 if (sizeof(holelen) > sizeof(hlen)) {
2531 (holebegin + holelen + PAGE_SIZE - 1) >> PAGE_SHIFT;
2532 if (holeend & ~(long long)ULONG_MAX)
2533 hlen = ULONG_MAX - hba + 1;
2536 details.check_mapping = even_cows? NULL: mapping;
2537 details.first_index = hba;
2538 details.last_index = hba + hlen - 1;
2539 if (details.last_index < details.first_index)
2540 details.last_index = ULONG_MAX;
2542 i_mmap_lock_write(mapping);
2543 if (unlikely(!RB_EMPTY_ROOT(&mapping->i_mmap)))
2544 unmap_mapping_range_tree(&mapping->i_mmap, &details);
2545 i_mmap_unlock_write(mapping);
2547 EXPORT_SYMBOL(unmap_mapping_range);
2550 * We enter with non-exclusive mmap_sem (to exclude vma changes,
2551 * but allow concurrent faults), and pte mapped but not yet locked.
2552 * We return with pte unmapped and unlocked.
2554 * We return with the mmap_sem locked or unlocked in the same cases
2555 * as does filemap_fault().
2557 int do_swap_page(struct fault_env *fe, pte_t orig_pte)
2559 struct vm_area_struct *vma = fe->vma;
2560 struct page *page, *swapcache;
2561 struct mem_cgroup *memcg;
2568 if (!pte_unmap_same(vma->vm_mm, fe->pmd, fe->pte, orig_pte))
2571 entry = pte_to_swp_entry(orig_pte);
2572 if (unlikely(non_swap_entry(entry))) {
2573 if (is_migration_entry(entry)) {
2574 migration_entry_wait(vma->vm_mm, fe->pmd, fe->address);
2575 } else if (is_hwpoison_entry(entry)) {
2576 ret = VM_FAULT_HWPOISON;
2578 print_bad_pte(vma, fe->address, orig_pte, NULL);
2579 ret = VM_FAULT_SIGBUS;
2583 delayacct_set_flag(DELAYACCT_PF_SWAPIN);
2584 page = lookup_swap_cache(entry);
2586 page = swapin_readahead(entry,
2587 GFP_HIGHUSER_MOVABLE, vma, fe->address);
2590 * Back out if somebody else faulted in this pte
2591 * while we released the pte lock.
2593 fe->pte = pte_offset_map_lock(vma->vm_mm, fe->pmd,
2594 fe->address, &fe->ptl);
2595 if (likely(pte_same(*fe->pte, orig_pte)))
2597 delayacct_clear_flag(DELAYACCT_PF_SWAPIN);
2601 /* Had to read the page from swap area: Major fault */
2602 ret = VM_FAULT_MAJOR;
2603 count_vm_event(PGMAJFAULT);
2604 mem_cgroup_count_vm_event(vma->vm_mm, PGMAJFAULT);
2605 } else if (PageHWPoison(page)) {
2607 * hwpoisoned dirty swapcache pages are kept for killing
2608 * owner processes (which may be unknown at hwpoison time)
2610 ret = VM_FAULT_HWPOISON;
2611 delayacct_clear_flag(DELAYACCT_PF_SWAPIN);
2617 locked = lock_page_or_retry(page, vma->vm_mm, fe->flags);
2619 delayacct_clear_flag(DELAYACCT_PF_SWAPIN);
2621 ret |= VM_FAULT_RETRY;
2626 * Make sure try_to_free_swap or reuse_swap_page or swapoff did not
2627 * release the swapcache from under us. The page pin, and pte_same
2628 * test below, are not enough to exclude that. Even if it is still
2629 * swapcache, we need to check that the page's swap has not changed.
2631 if (unlikely(!PageSwapCache(page) || page_private(page) != entry.val))
2634 page = ksm_might_need_to_copy(page, vma, fe->address);
2635 if (unlikely(!page)) {
2641 if (mem_cgroup_try_charge(page, vma->vm_mm, GFP_KERNEL,
2648 * Back out if somebody else already faulted in this pte.
2650 fe->pte = pte_offset_map_lock(vma->vm_mm, fe->pmd, fe->address,
2652 if (unlikely(!pte_same(*fe->pte, orig_pte)))
2655 if (unlikely(!PageUptodate(page))) {
2656 ret = VM_FAULT_SIGBUS;
2661 * The page isn't present yet, go ahead with the fault.
2663 * Be careful about the sequence of operations here.
2664 * To get its accounting right, reuse_swap_page() must be called
2665 * while the page is counted on swap but not yet in mapcount i.e.
2666 * before page_add_anon_rmap() and swap_free(); try_to_free_swap()
2667 * must be called after the swap_free(), or it will never succeed.
2670 inc_mm_counter_fast(vma->vm_mm, MM_ANONPAGES);
2671 dec_mm_counter_fast(vma->vm_mm, MM_SWAPENTS);
2672 pte = mk_pte(page, vma->vm_page_prot);
2673 if ((fe->flags & FAULT_FLAG_WRITE) && reuse_swap_page(page, NULL)) {
2674 pte = maybe_mkwrite(pte_mkdirty(pte), vma);
2675 fe->flags &= ~FAULT_FLAG_WRITE;
2676 ret |= VM_FAULT_WRITE;
2677 exclusive = RMAP_EXCLUSIVE;
2679 flush_icache_page(vma, page);
2680 if (pte_swp_soft_dirty(orig_pte))
2681 pte = pte_mksoft_dirty(pte);
2682 set_pte_at(vma->vm_mm, fe->address, fe->pte, pte);
2683 if (page == swapcache) {
2684 do_page_add_anon_rmap(page, vma, fe->address, exclusive);
2685 mem_cgroup_commit_charge(page, memcg, true, false);
2686 activate_page(page);
2687 } else { /* ksm created a completely new copy */
2688 page_add_new_anon_rmap(page, vma, fe->address, false);
2689 mem_cgroup_commit_charge(page, memcg, false, false);
2690 lru_cache_add_active_or_unevictable(page, vma);
2694 if (mem_cgroup_swap_full(page) ||
2695 (vma->vm_flags & VM_LOCKED) || PageMlocked(page))
2696 try_to_free_swap(page);
2698 if (page != swapcache) {
2700 * Hold the lock to avoid the swap entry to be reused
2701 * until we take the PT lock for the pte_same() check
2702 * (to avoid false positives from pte_same). For
2703 * further safety release the lock after the swap_free
2704 * so that the swap count won't change under a
2705 * parallel locked swapcache.
2707 unlock_page(swapcache);
2708 put_page(swapcache);
2711 if (fe->flags & FAULT_FLAG_WRITE) {
2712 ret |= do_wp_page(fe, pte);
2713 if (ret & VM_FAULT_ERROR)
2714 ret &= VM_FAULT_ERROR;
2718 /* No need to invalidate - it was non-present before */
2719 update_mmu_cache(vma, fe->address, fe->pte);
2721 pte_unmap_unlock(fe->pte, fe->ptl);
2725 mem_cgroup_cancel_charge(page, memcg, false);
2726 pte_unmap_unlock(fe->pte, fe->ptl);
2731 if (page != swapcache) {
2732 unlock_page(swapcache);
2733 put_page(swapcache);
2739 * We enter with non-exclusive mmap_sem (to exclude vma changes,
2740 * but allow concurrent faults), and pte mapped but not yet locked.
2741 * We return with mmap_sem still held, but pte unmapped and unlocked.
2743 static int do_anonymous_page(struct fault_env *fe)
2745 struct vm_area_struct *vma = fe->vma;
2746 struct mem_cgroup *memcg;
2750 /* File mapping without ->vm_ops ? */
2751 if (vma->vm_flags & VM_SHARED)
2752 return VM_FAULT_SIGBUS;
2755 * Use pte_alloc() instead of pte_alloc_map(). We can't run
2756 * pte_offset_map() on pmds where a huge pmd might be created
2757 * from a different thread.
2759 * pte_alloc_map() is safe to use under down_write(mmap_sem) or when
2760 * parallel threads are excluded by other means.
2762 * Here we only have down_read(mmap_sem).
2764 if (pte_alloc(vma->vm_mm, fe->pmd, fe->address))
2765 return VM_FAULT_OOM;
2767 /* See the comment in pte_alloc_one_map() */
2768 if (unlikely(pmd_trans_unstable(fe->pmd)))
2771 /* Use the zero-page for reads */
2772 if (!(fe->flags & FAULT_FLAG_WRITE) &&
2773 !mm_forbids_zeropage(vma->vm_mm)) {
2774 entry = pte_mkspecial(pfn_pte(my_zero_pfn(fe->address),
2775 vma->vm_page_prot));
2776 fe->pte = pte_offset_map_lock(vma->vm_mm, fe->pmd, fe->address,
2778 if (!pte_none(*fe->pte))
2780 /* Deliver the page fault to userland, check inside PT lock */
2781 if (userfaultfd_missing(vma)) {
2782 pte_unmap_unlock(fe->pte, fe->ptl);
2783 return handle_userfault(fe, VM_UFFD_MISSING);
2788 /* Allocate our own private page. */
2789 if (unlikely(anon_vma_prepare(vma)))
2791 page = alloc_zeroed_user_highpage_movable(vma, fe->address);
2795 if (mem_cgroup_try_charge(page, vma->vm_mm, GFP_KERNEL, &memcg, false))
2799 * The memory barrier inside __SetPageUptodate makes sure that
2800 * preceeding stores to the page contents become visible before
2801 * the set_pte_at() write.
2803 __SetPageUptodate(page);
2805 entry = mk_pte(page, vma->vm_page_prot);
2806 if (vma->vm_flags & VM_WRITE)
2807 entry = pte_mkwrite(pte_mkdirty(entry));
2809 fe->pte = pte_offset_map_lock(vma->vm_mm, fe->pmd, fe->address,
2811 if (!pte_none(*fe->pte))
2814 /* Deliver the page fault to userland, check inside PT lock */
2815 if (userfaultfd_missing(vma)) {
2816 pte_unmap_unlock(fe->pte, fe->ptl);
2817 mem_cgroup_cancel_charge(page, memcg, false);
2819 return handle_userfault(fe, VM_UFFD_MISSING);
2822 inc_mm_counter_fast(vma->vm_mm, MM_ANONPAGES);
2823 page_add_new_anon_rmap(page, vma, fe->address, false);
2824 mem_cgroup_commit_charge(page, memcg, false, false);
2825 lru_cache_add_active_or_unevictable(page, vma);
2827 set_pte_at(vma->vm_mm, fe->address, fe->pte, entry);
2829 /* No need to invalidate - it was non-present before */
2830 update_mmu_cache(vma, fe->address, fe->pte);
2832 pte_unmap_unlock(fe->pte, fe->ptl);
2835 mem_cgroup_cancel_charge(page, memcg, false);
2841 return VM_FAULT_OOM;
2845 * The mmap_sem must have been held on entry, and may have been
2846 * released depending on flags and vma->vm_ops->fault() return value.
2847 * See filemap_fault() and __lock_page_retry().
2849 static int __do_fault(struct fault_env *fe, pgoff_t pgoff,
2850 struct page *cow_page, struct page **page, void **entry)
2852 struct vm_area_struct *vma = fe->vma;
2853 struct vm_fault vmf;
2857 * Preallocate pte before we take page_lock because this might lead to
2858 * deadlocks for memcg reclaim which waits for pages under writeback:
2860 * SetPageWriteback(A)
2866 * wait_on_page_writeback(A)
2867 * SetPageWriteback(B)
2869 * # flush A, B to clear the writeback
2871 if (pmd_none(*fe->pmd) && !fe->prealloc_pte) {
2872 fe->prealloc_pte = pte_alloc_one(vma->vm_mm, fe->address);
2873 if (!fe->prealloc_pte)
2874 return VM_FAULT_OOM;
2875 smp_wmb(); /* See comment in __pte_alloc() */
2878 vmf.virtual_address = (void __user *)(fe->address & PAGE_MASK);
2880 vmf.flags = fe->flags;
2882 vmf.gfp_mask = __get_fault_gfp_mask(vma);
2883 vmf.cow_page = cow_page;
2885 ret = vma->vm_ops->fault(vma, &vmf);
2886 if (unlikely(ret & (VM_FAULT_ERROR | VM_FAULT_NOPAGE | VM_FAULT_RETRY)))
2888 if (ret & VM_FAULT_DAX_LOCKED) {
2893 if (unlikely(PageHWPoison(vmf.page))) {
2894 int poisonret = VM_FAULT_HWPOISON;
2895 if (ret & VM_FAULT_LOCKED) {
2896 /* Retry if a clean page was removed from the cache. */
2897 if (invalidate_inode_page(vmf.page))
2899 unlock_page(vmf.page);
2905 if (unlikely(!(ret & VM_FAULT_LOCKED)))
2906 lock_page(vmf.page);
2908 VM_BUG_ON_PAGE(!PageLocked(vmf.page), vmf.page);
2915 * The ordering of these checks is important for pmds with _PAGE_DEVMAP set.
2916 * If we check pmd_trans_unstable() first we will trip the bad_pmd() check
2917 * inside of pmd_none_or_trans_huge_or_clear_bad(). This will end up correctly
2918 * returning 1 but not before it spams dmesg with the pmd_clear_bad() output.
2920 static int pmd_devmap_trans_unstable(pmd_t *pmd)
2922 return pmd_devmap(*pmd) || pmd_trans_unstable(pmd);
2925 static int pte_alloc_one_map(struct fault_env *fe)
2927 struct vm_area_struct *vma = fe->vma;
2929 if (!pmd_none(*fe->pmd))
2931 if (fe->prealloc_pte) {
2932 fe->ptl = pmd_lock(vma->vm_mm, fe->pmd);
2933 if (unlikely(!pmd_none(*fe->pmd))) {
2934 spin_unlock(fe->ptl);
2938 atomic_long_inc(&vma->vm_mm->nr_ptes);
2939 pmd_populate(vma->vm_mm, fe->pmd, fe->prealloc_pte);
2940 spin_unlock(fe->ptl);
2941 fe->prealloc_pte = 0;
2942 } else if (unlikely(pte_alloc(vma->vm_mm, fe->pmd, fe->address))) {
2943 return VM_FAULT_OOM;
2947 * If a huge pmd materialized under us just retry later. Use
2948 * pmd_trans_unstable() via pmd_devmap_trans_unstable() instead of
2949 * pmd_trans_huge() to ensure the pmd didn't become pmd_trans_huge
2950 * under us and then back to pmd_none, as a result of MADV_DONTNEED
2951 * running immediately after a huge pmd fault in a different thread of
2952 * this mm, in turn leading to a misleading pmd_trans_huge() retval.
2953 * All we have to ensure is that it is a regular pmd that we can walk
2954 * with pte_offset_map() and we can do that through an atomic read in
2955 * C, which is what pmd_trans_unstable() provides.
2957 if (pmd_devmap_trans_unstable(fe->pmd))
2958 return VM_FAULT_NOPAGE;
2961 * At this point we know that our vmf->pmd points to a page of ptes
2962 * and it cannot become pmd_none(), pmd_devmap() or pmd_trans_huge()
2963 * for the duration of the fault. If a racing MADV_DONTNEED runs and
2964 * we zap the ptes pointed to by our vmf->pmd, the vmf->ptl will still
2965 * be valid and we will re-check to make sure the vmf->pte isn't
2966 * pte_none() under vmf->ptl protection when we return to
2969 fe->pte = pte_offset_map_lock(vma->vm_mm, fe->pmd, fe->address,
2974 #ifdef CONFIG_TRANSPARENT_HUGE_PAGECACHE
2976 #define HPAGE_CACHE_INDEX_MASK (HPAGE_PMD_NR - 1)
2977 static inline bool transhuge_vma_suitable(struct vm_area_struct *vma,
2978 unsigned long haddr)
2980 if (((vma->vm_start >> PAGE_SHIFT) & HPAGE_CACHE_INDEX_MASK) !=
2981 (vma->vm_pgoff & HPAGE_CACHE_INDEX_MASK))
2983 if (haddr < vma->vm_start || haddr + HPAGE_PMD_SIZE > vma->vm_end)
2988 static int do_set_pmd(struct fault_env *fe, struct page *page)
2990 struct vm_area_struct *vma = fe->vma;
2991 bool write = fe->flags & FAULT_FLAG_WRITE;
2992 unsigned long haddr = fe->address & HPAGE_PMD_MASK;
2996 if (!transhuge_vma_suitable(vma, haddr))
2997 return VM_FAULT_FALLBACK;
2999 ret = VM_FAULT_FALLBACK;
3000 page = compound_head(page);
3002 fe->ptl = pmd_lock(vma->vm_mm, fe->pmd);
3003 if (unlikely(!pmd_none(*fe->pmd)))
3006 for (i = 0; i < HPAGE_PMD_NR; i++)
3007 flush_icache_page(vma, page + i);
3009 entry = mk_huge_pmd(page, vma->vm_page_prot);
3011 entry = maybe_pmd_mkwrite(pmd_mkdirty(entry), vma);
3013 add_mm_counter(vma->vm_mm, MM_FILEPAGES, HPAGE_PMD_NR);
3014 page_add_file_rmap(page, true);
3016 set_pmd_at(vma->vm_mm, haddr, fe->pmd, entry);
3018 update_mmu_cache_pmd(vma, haddr, fe->pmd);
3020 /* fault is handled */
3022 count_vm_event(THP_FILE_MAPPED);
3024 spin_unlock(fe->ptl);
3028 static int do_set_pmd(struct fault_env *fe, struct page *page)
3036 * alloc_set_pte - setup new PTE entry for given page and add reverse page
3037 * mapping. If needed, the fucntion allocates page table or use pre-allocated.
3039 * @fe: fault environment
3040 * @memcg: memcg to charge page (only for private mappings)
3041 * @page: page to map
3043 * Caller must take care of unlocking fe->ptl, if fe->pte is non-NULL on return.
3045 * Target users are page handler itself and implementations of
3046 * vm_ops->map_pages.
3048 int alloc_set_pte(struct fault_env *fe, struct mem_cgroup *memcg,
3051 struct vm_area_struct *vma = fe->vma;
3052 bool write = fe->flags & FAULT_FLAG_WRITE;
3056 if (pmd_none(*fe->pmd) && PageTransCompound(page) &&
3057 IS_ENABLED(CONFIG_TRANSPARENT_HUGE_PAGECACHE)) {
3059 VM_BUG_ON_PAGE(memcg, page);
3061 ret = do_set_pmd(fe, page);
3062 if (ret != VM_FAULT_FALLBACK)
3067 ret = pte_alloc_one_map(fe);
3072 /* Re-check under ptl */
3073 if (unlikely(!pte_none(*fe->pte)))
3074 return VM_FAULT_NOPAGE;
3076 flush_icache_page(vma, page);
3077 entry = mk_pte(page, vma->vm_page_prot);
3079 entry = maybe_mkwrite(pte_mkdirty(entry), vma);
3080 /* copy-on-write page */
3081 if (write && !(vma->vm_flags & VM_SHARED)) {
3082 inc_mm_counter_fast(vma->vm_mm, MM_ANONPAGES);
3083 page_add_new_anon_rmap(page, vma, fe->address, false);
3084 mem_cgroup_commit_charge(page, memcg, false, false);
3085 lru_cache_add_active_or_unevictable(page, vma);
3087 inc_mm_counter_fast(vma->vm_mm, mm_counter_file(page));
3088 page_add_file_rmap(page, false);
3090 set_pte_at(vma->vm_mm, fe->address, fe->pte, entry);
3092 /* no need to invalidate: a not-present page won't be cached */
3093 update_mmu_cache(vma, fe->address, fe->pte);
3098 static unsigned long fault_around_bytes __read_mostly =
3099 rounddown_pow_of_two(65536);
3101 #ifdef CONFIG_DEBUG_FS
3102 static int fault_around_bytes_get(void *data, u64 *val)
3104 *val = fault_around_bytes;
3109 * fault_around_pages() and fault_around_mask() expects fault_around_bytes
3110 * rounded down to nearest page order. It's what do_fault_around() expects to
3113 static int fault_around_bytes_set(void *data, u64 val)
3115 if (val / PAGE_SIZE > PTRS_PER_PTE)
3117 if (val > PAGE_SIZE)
3118 fault_around_bytes = rounddown_pow_of_two(val);
3120 fault_around_bytes = PAGE_SIZE; /* rounddown_pow_of_two(0) is undefined */
3123 DEFINE_SIMPLE_ATTRIBUTE(fault_around_bytes_fops,
3124 fault_around_bytes_get, fault_around_bytes_set, "%llu\n");
3126 static int __init fault_around_debugfs(void)
3130 ret = debugfs_create_file("fault_around_bytes", 0644, NULL, NULL,
3131 &fault_around_bytes_fops);
3133 pr_warn("Failed to create fault_around_bytes in debugfs");
3136 late_initcall(fault_around_debugfs);
3140 * do_fault_around() tries to map few pages around the fault address. The hope
3141 * is that the pages will be needed soon and this will lower the number of
3144 * It uses vm_ops->map_pages() to map the pages, which skips the page if it's
3145 * not ready to be mapped: not up-to-date, locked, etc.
3147 * This function is called with the page table lock taken. In the split ptlock
3148 * case the page table lock only protects only those entries which belong to
3149 * the page table corresponding to the fault address.
3151 * This function doesn't cross the VMA boundaries, in order to call map_pages()
3154 * fault_around_pages() defines how many pages we'll try to map.
3155 * do_fault_around() expects it to return a power of two less than or equal to
3158 * The virtual address of the area that we map is naturally aligned to the
3159 * fault_around_pages() value (and therefore to page order). This way it's
3160 * easier to guarantee that we don't cross page table boundaries.
3162 static int do_fault_around(struct fault_env *fe, pgoff_t start_pgoff)
3164 unsigned long address = fe->address, nr_pages, mask;
3168 nr_pages = READ_ONCE(fault_around_bytes) >> PAGE_SHIFT;
3169 mask = ~(nr_pages * PAGE_SIZE - 1) & PAGE_MASK;
3171 fe->address = max(address & mask, fe->vma->vm_start);
3172 off = ((address - fe->address) >> PAGE_SHIFT) & (PTRS_PER_PTE - 1);
3176 * end_pgoff is either end of page table or end of vma
3177 * or fault_around_pages() from start_pgoff, depending what is nearest.
3179 end_pgoff = start_pgoff -
3180 ((fe->address >> PAGE_SHIFT) & (PTRS_PER_PTE - 1)) +
3182 end_pgoff = min3(end_pgoff, vma_pages(fe->vma) + fe->vma->vm_pgoff - 1,
3183 start_pgoff + nr_pages - 1);
3185 if (pmd_none(*fe->pmd)) {
3186 fe->prealloc_pte = pte_alloc_one(fe->vma->vm_mm, fe->address);
3187 if (!fe->prealloc_pte)
3189 smp_wmb(); /* See comment in __pte_alloc() */
3192 fe->vma->vm_ops->map_pages(fe, start_pgoff, end_pgoff);
3194 /* preallocated pagetable is unused: free it */
3195 if (fe->prealloc_pte) {
3196 pte_free(fe->vma->vm_mm, fe->prealloc_pte);
3197 fe->prealloc_pte = 0;
3199 /* Huge page is mapped? Page fault is solved */
3200 if (pmd_trans_huge(*fe->pmd)) {
3201 ret = VM_FAULT_NOPAGE;
3205 /* ->map_pages() haven't done anything useful. Cold page cache? */
3209 /* check if the page fault is solved */
3210 fe->pte -= (fe->address >> PAGE_SHIFT) - (address >> PAGE_SHIFT);
3211 if (!pte_none(*fe->pte))
3212 ret = VM_FAULT_NOPAGE;
3213 pte_unmap_unlock(fe->pte, fe->ptl);
3215 fe->address = address;
3220 static int do_read_fault(struct fault_env *fe, pgoff_t pgoff)
3222 struct vm_area_struct *vma = fe->vma;
3223 struct page *fault_page;
3227 * Let's call ->map_pages() first and use ->fault() as fallback
3228 * if page by the offset is not ready to be mapped (cold cache or
3231 if (vma->vm_ops->map_pages && fault_around_bytes >> PAGE_SHIFT > 1) {
3232 ret = do_fault_around(fe, pgoff);
3237 ret = __do_fault(fe, pgoff, NULL, &fault_page, NULL);
3238 if (unlikely(ret & (VM_FAULT_ERROR | VM_FAULT_NOPAGE | VM_FAULT_RETRY)))
3241 ret |= alloc_set_pte(fe, NULL, fault_page);
3243 pte_unmap_unlock(fe->pte, fe->ptl);
3244 unlock_page(fault_page);
3245 if (unlikely(ret & (VM_FAULT_ERROR | VM_FAULT_NOPAGE | VM_FAULT_RETRY)))
3246 put_page(fault_page);
3250 static int do_cow_fault(struct fault_env *fe, pgoff_t pgoff)
3252 struct vm_area_struct *vma = fe->vma;
3253 struct page *fault_page, *new_page;
3255 struct mem_cgroup *memcg;
3258 if (unlikely(anon_vma_prepare(vma)))
3259 return VM_FAULT_OOM;
3261 new_page = alloc_page_vma(GFP_HIGHUSER_MOVABLE, vma, fe->address);
3263 return VM_FAULT_OOM;
3265 if (mem_cgroup_try_charge(new_page, vma->vm_mm, GFP_KERNEL,
3268 return VM_FAULT_OOM;
3271 ret = __do_fault(fe, pgoff, new_page, &fault_page, &fault_entry);
3272 if (unlikely(ret & (VM_FAULT_ERROR | VM_FAULT_NOPAGE | VM_FAULT_RETRY)))
3275 if (!(ret & VM_FAULT_DAX_LOCKED))
3276 copy_user_highpage(new_page, fault_page, fe->address, vma);
3277 __SetPageUptodate(new_page);
3279 ret |= alloc_set_pte(fe, memcg, new_page);
3281 pte_unmap_unlock(fe->pte, fe->ptl);
3282 if (!(ret & VM_FAULT_DAX_LOCKED)) {
3283 unlock_page(fault_page);
3284 put_page(fault_page);
3286 dax_unlock_mapping_entry(vma->vm_file->f_mapping, pgoff);
3288 if (unlikely(ret & (VM_FAULT_ERROR | VM_FAULT_NOPAGE | VM_FAULT_RETRY)))
3292 mem_cgroup_cancel_charge(new_page, memcg, false);
3297 static int do_shared_fault(struct fault_env *fe, pgoff_t pgoff)
3299 struct vm_area_struct *vma = fe->vma;
3300 struct page *fault_page;
3301 struct address_space *mapping;
3305 ret = __do_fault(fe, pgoff, NULL, &fault_page, NULL);
3306 if (unlikely(ret & (VM_FAULT_ERROR | VM_FAULT_NOPAGE | VM_FAULT_RETRY)))
3310 * Check if the backing address space wants to know that the page is
3311 * about to become writable
3313 if (vma->vm_ops->page_mkwrite) {
3314 unlock_page(fault_page);
3315 tmp = do_page_mkwrite(vma, fault_page, fe->address);
3316 if (unlikely(!tmp ||
3317 (tmp & (VM_FAULT_ERROR | VM_FAULT_NOPAGE)))) {
3318 put_page(fault_page);
3323 ret |= alloc_set_pte(fe, NULL, fault_page);
3325 pte_unmap_unlock(fe->pte, fe->ptl);
3326 if (unlikely(ret & (VM_FAULT_ERROR | VM_FAULT_NOPAGE |
3328 unlock_page(fault_page);
3329 put_page(fault_page);
3333 if (set_page_dirty(fault_page))
3336 * Take a local copy of the address_space - page.mapping may be zeroed
3337 * by truncate after unlock_page(). The address_space itself remains
3338 * pinned by vma->vm_file's reference. We rely on unlock_page()'s
3339 * release semantics to prevent the compiler from undoing this copying.
3341 mapping = page_rmapping(fault_page);
3342 unlock_page(fault_page);
3343 if ((dirtied || vma->vm_ops->page_mkwrite) && mapping) {
3345 * Some device drivers do not set page.mapping but still
3348 balance_dirty_pages_ratelimited(mapping);
3351 if (!vma->vm_ops->page_mkwrite)
3352 file_update_time(vma->vm_file);
3358 * We enter with non-exclusive mmap_sem (to exclude vma changes,
3359 * but allow concurrent faults).
3360 * The mmap_sem may have been released depending on flags and our
3361 * return value. See filemap_fault() and __lock_page_or_retry().
3363 static int do_fault(struct fault_env *fe)
3365 struct vm_area_struct *vma = fe->vma;
3366 pgoff_t pgoff = linear_page_index(vma, fe->address);
3369 /* The VMA was not fully populated on mmap() or missing VM_DONTEXPAND */
3370 if (!vma->vm_ops->fault)
3371 ret = VM_FAULT_SIGBUS;
3372 else if (!(fe->flags & FAULT_FLAG_WRITE))
3373 ret = do_read_fault(fe, pgoff);
3374 else if (!(vma->vm_flags & VM_SHARED))
3375 ret = do_cow_fault(fe, pgoff);
3377 ret = do_shared_fault(fe, pgoff);
3379 /* preallocated pagetable is unused: free it */
3380 if (fe->prealloc_pte) {
3381 pte_free(vma->vm_mm, fe->prealloc_pte);
3382 fe->prealloc_pte = 0;
3387 static int numa_migrate_prep(struct page *page, struct vm_area_struct *vma,
3388 unsigned long addr, int page_nid,
3393 count_vm_numa_event(NUMA_HINT_FAULTS);
3394 if (page_nid == numa_node_id()) {
3395 count_vm_numa_event(NUMA_HINT_FAULTS_LOCAL);
3396 *flags |= TNF_FAULT_LOCAL;
3399 return mpol_misplaced(page, vma, addr);
3402 static int do_numa_page(struct fault_env *fe, pte_t pte)
3404 struct vm_area_struct *vma = fe->vma;
3405 struct page *page = NULL;
3409 bool migrated = false;
3410 bool was_writable = pte_write(pte);
3414 * The "pte" at this point cannot be used safely without
3415 * validation through pte_unmap_same(). It's of NUMA type but
3416 * the pfn may be screwed if the read is non atomic.
3418 * We can safely just do a "set_pte_at()", because the old
3419 * page table entry is not accessible, so there would be no
3420 * concurrent hardware modifications to the PTE.
3422 fe->ptl = pte_lockptr(vma->vm_mm, fe->pmd);
3424 if (unlikely(!pte_same(*fe->pte, pte))) {
3425 pte_unmap_unlock(fe->pte, fe->ptl);
3429 /* Make it present again */
3430 pte = pte_modify(pte, vma->vm_page_prot);
3431 pte = pte_mkyoung(pte);
3433 pte = pte_mkwrite(pte);
3434 set_pte_at(vma->vm_mm, fe->address, fe->pte, pte);
3435 update_mmu_cache(vma, fe->address, fe->pte);
3437 page = vm_normal_page(vma, fe->address, pte);
3439 pte_unmap_unlock(fe->pte, fe->ptl);
3443 /* TODO: handle PTE-mapped THP */
3444 if (PageCompound(page)) {
3445 pte_unmap_unlock(fe->pte, fe->ptl);
3450 * Avoid grouping on RO pages in general. RO pages shouldn't hurt as
3451 * much anyway since they can be in shared cache state. This misses
3452 * the case where a mapping is writable but the process never writes
3453 * to it but pte_write gets cleared during protection updates and
3454 * pte_dirty has unpredictable behaviour between PTE scan updates,
3455 * background writeback, dirty balancing and application behaviour.
3457 if (!pte_write(pte))
3458 flags |= TNF_NO_GROUP;
3461 * Flag if the page is shared between multiple address spaces. This
3462 * is later used when determining whether to group tasks together
3464 if (page_mapcount(page) > 1 && (vma->vm_flags & VM_SHARED))
3465 flags |= TNF_SHARED;
3467 last_cpupid = page_cpupid_last(page);
3468 page_nid = page_to_nid(page);
3469 target_nid = numa_migrate_prep(page, vma, fe->address, page_nid,
3471 pte_unmap_unlock(fe->pte, fe->ptl);
3472 if (target_nid == -1) {
3477 /* Migrate to the requested node */
3478 migrated = migrate_misplaced_page(page, vma, target_nid);
3480 page_nid = target_nid;
3481 flags |= TNF_MIGRATED;
3483 flags |= TNF_MIGRATE_FAIL;
3487 task_numa_fault(last_cpupid, page_nid, 1, flags);
3491 static int create_huge_pmd(struct fault_env *fe)
3493 struct vm_area_struct *vma = fe->vma;
3494 if (vma_is_anonymous(vma))
3495 return do_huge_pmd_anonymous_page(fe);
3496 if (vma->vm_ops->pmd_fault)
3497 return vma->vm_ops->pmd_fault(vma, fe->address, fe->pmd,
3499 return VM_FAULT_FALLBACK;
3502 static int wp_huge_pmd(struct fault_env *fe, pmd_t orig_pmd)
3504 if (vma_is_anonymous(fe->vma))
3505 return do_huge_pmd_wp_page(fe, orig_pmd);
3506 if (fe->vma->vm_ops->pmd_fault)
3507 return fe->vma->vm_ops->pmd_fault(fe->vma, fe->address, fe->pmd,
3510 /* COW handled on pte level: split pmd */
3511 VM_BUG_ON_VMA(fe->vma->vm_flags & VM_SHARED, fe->vma);
3512 split_huge_pmd(fe->vma, fe->pmd, fe->address);
3514 return VM_FAULT_FALLBACK;
3517 static inline bool vma_is_accessible(struct vm_area_struct *vma)
3519 return vma->vm_flags & (VM_READ | VM_EXEC | VM_WRITE);
3523 * These routines also need to handle stuff like marking pages dirty
3524 * and/or accessed for architectures that don't do it in hardware (most
3525 * RISC architectures). The early dirtying is also good on the i386.
3527 * There is also a hook called "update_mmu_cache()" that architectures
3528 * with external mmu caches can use to update those (ie the Sparc or
3529 * PowerPC hashed page tables that act as extended TLBs).
3531 * We enter with non-exclusive mmap_sem (to exclude vma changes, but allow
3532 * concurrent faults).
3534 * The mmap_sem may have been released depending on flags and our return value.
3535 * See filemap_fault() and __lock_page_or_retry().
3537 static int handle_pte_fault(struct fault_env *fe)
3541 if (unlikely(pmd_none(*fe->pmd))) {
3543 * Leave __pte_alloc() until later: because vm_ops->fault may
3544 * want to allocate huge page, and if we expose page table
3545 * for an instant, it will be difficult to retract from
3546 * concurrent faults and from rmap lookups.
3550 /* See comment in pte_alloc_one_map() */
3551 if (pmd_devmap_trans_unstable(fe->pmd))
3554 * A regular pmd is established and it can't morph into a huge
3555 * pmd from under us anymore at this point because we hold the
3556 * mmap_sem read mode and khugepaged takes it in write mode.
3557 * So now it's safe to run pte_offset_map().
3559 fe->pte = pte_offset_map(fe->pmd, fe->address);
3564 * some architectures can have larger ptes than wordsize,
3565 * e.g.ppc44x-defconfig has CONFIG_PTE_64BIT=y and
3566 * CONFIG_32BIT=y, so READ_ONCE or ACCESS_ONCE cannot guarantee
3567 * atomic accesses. The code below just needs a consistent
3568 * view for the ifs and we later double check anyway with the
3569 * ptl lock held. So here a barrier will do.
3572 if (pte_none(entry)) {
3579 if (vma_is_anonymous(fe->vma))
3580 return do_anonymous_page(fe);
3582 return do_fault(fe);
3585 if (!pte_present(entry))
3586 return do_swap_page(fe, entry);
3588 if (pte_protnone(entry) && vma_is_accessible(fe->vma))
3589 return do_numa_page(fe, entry);
3591 fe->ptl = pte_lockptr(fe->vma->vm_mm, fe->pmd);
3593 if (unlikely(!pte_same(*fe->pte, entry)))
3595 if (fe->flags & FAULT_FLAG_WRITE) {
3596 if (!pte_write(entry))
3597 return do_wp_page(fe, entry);
3598 entry = pte_mkdirty(entry);
3600 entry = pte_mkyoung(entry);
3601 if (ptep_set_access_flags(fe->vma, fe->address, fe->pte, entry,
3602 fe->flags & FAULT_FLAG_WRITE)) {
3603 update_mmu_cache(fe->vma, fe->address, fe->pte);
3606 * This is needed only for protection faults but the arch code
3607 * is not yet telling us if this is a protection fault or not.
3608 * This still avoids useless tlb flushes for .text page faults
3611 if (fe->flags & FAULT_FLAG_WRITE)
3612 flush_tlb_fix_spurious_fault(fe->vma, fe->address);
3615 pte_unmap_unlock(fe->pte, fe->ptl);
3620 * By the time we get here, we already hold the mm semaphore
3622 * The mmap_sem may have been released depending on flags and our
3623 * return value. See filemap_fault() and __lock_page_or_retry().
3625 static int __handle_mm_fault(struct vm_area_struct *vma, unsigned long address,
3628 struct fault_env fe = {
3633 struct mm_struct *mm = vma->vm_mm;
3637 pgd = pgd_offset(mm, address);
3638 pud = pud_alloc(mm, pgd, address);
3640 return VM_FAULT_OOM;
3641 fe.pmd = pmd_alloc(mm, pud, address);
3643 return VM_FAULT_OOM;
3644 if (pmd_none(*fe.pmd) && transparent_hugepage_enabled(vma)) {
3645 int ret = create_huge_pmd(&fe);
3646 if (!(ret & VM_FAULT_FALLBACK))
3649 pmd_t orig_pmd = *fe.pmd;
3653 if (pmd_trans_huge(orig_pmd) || pmd_devmap(orig_pmd)) {
3654 if (pmd_protnone(orig_pmd) && vma_is_accessible(vma))
3655 return do_huge_pmd_numa_page(&fe, orig_pmd);
3657 if ((fe.flags & FAULT_FLAG_WRITE) &&
3658 !pmd_write(orig_pmd)) {
3659 ret = wp_huge_pmd(&fe, orig_pmd);
3660 if (!(ret & VM_FAULT_FALLBACK))
3663 huge_pmd_set_accessed(&fe, orig_pmd);
3669 return handle_pte_fault(&fe);
3673 * By the time we get here, we already hold the mm semaphore
3675 * The mmap_sem may have been released depending on flags and our
3676 * return value. See filemap_fault() and __lock_page_or_retry().
3678 int handle_mm_fault(struct vm_area_struct *vma, unsigned long address,
3683 __set_current_state(TASK_RUNNING);
3685 count_vm_event(PGFAULT);
3686 mem_cgroup_count_vm_event(vma->vm_mm, PGFAULT);
3688 /* do counter updates before entering really critical section. */
3689 check_sync_rss_stat(current);
3691 if (!arch_vma_access_permitted(vma, flags & FAULT_FLAG_WRITE,
3692 flags & FAULT_FLAG_INSTRUCTION,
3693 flags & FAULT_FLAG_REMOTE))
3694 return VM_FAULT_SIGSEGV;
3697 * Enable the memcg OOM handling for faults triggered in user
3698 * space. Kernel faults are handled more gracefully.
3700 if (flags & FAULT_FLAG_USER)
3701 mem_cgroup_oom_enable();
3703 if (unlikely(is_vm_hugetlb_page(vma)))
3704 ret = hugetlb_fault(vma->vm_mm, vma, address, flags);
3706 ret = __handle_mm_fault(vma, address, flags);
3708 if (flags & FAULT_FLAG_USER) {
3709 mem_cgroup_oom_disable();
3711 * The task may have entered a memcg OOM situation but
3712 * if the allocation error was handled gracefully (no
3713 * VM_FAULT_OOM), there is no need to kill anything.
3714 * Just clean up the OOM state peacefully.
3716 if (task_in_memcg_oom(current) && !(ret & VM_FAULT_OOM))
3717 mem_cgroup_oom_synchronize(false);
3721 * This mm has been already reaped by the oom reaper and so the
3722 * refault cannot be trusted in general. Anonymous refaults would
3723 * lose data and give a zero page instead e.g. This is especially
3724 * problem for use_mm() because regular tasks will just die and
3725 * the corrupted data will not be visible anywhere while kthread
3726 * will outlive the oom victim and potentially propagate the date
3729 if (unlikely((current->flags & PF_KTHREAD) && !(ret & VM_FAULT_ERROR)
3730 && test_bit(MMF_UNSTABLE, &vma->vm_mm->flags))) {
3733 * We are going to enforce SIGBUS but the PF path might have
3734 * dropped the mmap_sem already so take it again so that
3735 * we do not break expectations of all arch specific PF paths
3738 if (ret & VM_FAULT_RETRY)
3739 down_read(&vma->vm_mm->mmap_sem);
3740 ret = VM_FAULT_SIGBUS;
3745 EXPORT_SYMBOL_GPL(handle_mm_fault);
3747 #ifndef __PAGETABLE_PUD_FOLDED
3749 * Allocate page upper directory.
3750 * We've already handled the fast-path in-line.
3752 int __pud_alloc(struct mm_struct *mm, pgd_t *pgd, unsigned long address)
3754 pud_t *new = pud_alloc_one(mm, address);
3758 smp_wmb(); /* See comment in __pte_alloc */
3760 spin_lock(&mm->page_table_lock);
3761 if (pgd_present(*pgd)) /* Another has populated it */
3764 pgd_populate(mm, pgd, new);
3765 spin_unlock(&mm->page_table_lock);
3768 #endif /* __PAGETABLE_PUD_FOLDED */
3770 #ifndef __PAGETABLE_PMD_FOLDED
3772 * Allocate page middle directory.
3773 * We've already handled the fast-path in-line.
3775 int __pmd_alloc(struct mm_struct *mm, pud_t *pud, unsigned long address)
3777 pmd_t *new = pmd_alloc_one(mm, address);
3781 smp_wmb(); /* See comment in __pte_alloc */
3783 spin_lock(&mm->page_table_lock);
3784 #ifndef __ARCH_HAS_4LEVEL_HACK
3785 if (!pud_present(*pud)) {
3787 pud_populate(mm, pud, new);
3788 } else /* Another has populated it */
3791 if (!pgd_present(*pud)) {
3793 pgd_populate(mm, pud, new);
3794 } else /* Another has populated it */
3796 #endif /* __ARCH_HAS_4LEVEL_HACK */
3797 spin_unlock(&mm->page_table_lock);
3800 #endif /* __PAGETABLE_PMD_FOLDED */
3802 static int __follow_pte_pmd(struct mm_struct *mm, unsigned long address,
3803 pte_t **ptepp, pmd_t **pmdpp, spinlock_t **ptlp)
3810 pgd = pgd_offset(mm, address);
3811 if (pgd_none(*pgd) || unlikely(pgd_bad(*pgd)))
3814 pud = pud_offset(pgd, address);
3815 if (pud_none(*pud) || unlikely(pud_bad(*pud)))
3818 pmd = pmd_offset(pud, address);
3819 VM_BUG_ON(pmd_trans_huge(*pmd));
3821 if (pmd_huge(*pmd)) {
3825 *ptlp = pmd_lock(mm, pmd);
3826 if (pmd_huge(*pmd)) {
3833 if (pmd_none(*pmd) || unlikely(pmd_bad(*pmd)))
3836 ptep = pte_offset_map_lock(mm, pmd, address, ptlp);
3839 if (!pte_present(*ptep))
3844 pte_unmap_unlock(ptep, *ptlp);
3849 static inline int follow_pte(struct mm_struct *mm, unsigned long address,
3850 pte_t **ptepp, spinlock_t **ptlp)
3854 /* (void) is needed to make gcc happy */
3855 (void) __cond_lock(*ptlp,
3856 !(res = __follow_pte_pmd(mm, address, ptepp, NULL,
3861 int follow_pte_pmd(struct mm_struct *mm, unsigned long address,
3862 pte_t **ptepp, pmd_t **pmdpp, spinlock_t **ptlp)
3866 /* (void) is needed to make gcc happy */
3867 (void) __cond_lock(*ptlp,
3868 !(res = __follow_pte_pmd(mm, address, ptepp, pmdpp,
3872 EXPORT_SYMBOL(follow_pte_pmd);
3875 * follow_pfn - look up PFN at a user virtual address
3876 * @vma: memory mapping
3877 * @address: user virtual address
3878 * @pfn: location to store found PFN
3880 * Only IO mappings and raw PFN mappings are allowed.
3882 * Returns zero and the pfn at @pfn on success, -ve otherwise.
3884 int follow_pfn(struct vm_area_struct *vma, unsigned long address,
3891 if (!(vma->vm_flags & (VM_IO | VM_PFNMAP)))
3894 ret = follow_pte(vma->vm_mm, address, &ptep, &ptl);
3897 *pfn = pte_pfn(*ptep);
3898 pte_unmap_unlock(ptep, ptl);
3901 EXPORT_SYMBOL(follow_pfn);
3903 #ifdef CONFIG_HAVE_IOREMAP_PROT
3904 int follow_phys(struct vm_area_struct *vma,
3905 unsigned long address, unsigned int flags,
3906 unsigned long *prot, resource_size_t *phys)
3912 if (!(vma->vm_flags & (VM_IO | VM_PFNMAP)))
3915 if (follow_pte(vma->vm_mm, address, &ptep, &ptl))
3919 if ((flags & FOLL_WRITE) && !pte_write(pte))
3922 *prot = pgprot_val(pte_pgprot(pte));
3923 *phys = (resource_size_t)pte_pfn(pte) << PAGE_SHIFT;
3927 pte_unmap_unlock(ptep, ptl);
3932 int generic_access_phys(struct vm_area_struct *vma, unsigned long addr,
3933 void *buf, int len, int write)
3935 resource_size_t phys_addr;
3936 unsigned long prot = 0;
3937 void __iomem *maddr;
3938 int offset = addr & (PAGE_SIZE-1);
3940 if (follow_phys(vma, addr, write, &prot, &phys_addr))
3943 maddr = ioremap_prot(phys_addr, PAGE_ALIGN(len + offset), prot);
3948 memcpy_toio(maddr + offset, buf, len);
3950 memcpy_fromio(buf, maddr + offset, len);
3955 EXPORT_SYMBOL_GPL(generic_access_phys);
3959 * Access another process' address space as given in mm. If non-NULL, use the
3960 * given task for page fault accounting.
3962 int __access_remote_vm(struct task_struct *tsk, struct mm_struct *mm,
3963 unsigned long addr, void *buf, int len, unsigned int gup_flags)
3965 struct vm_area_struct *vma;
3966 void *old_buf = buf;
3967 int write = gup_flags & FOLL_WRITE;
3969 down_read(&mm->mmap_sem);
3970 /* ignore errors, just check how much was successfully transferred */
3972 int bytes, ret, offset;
3974 struct page *page = NULL;
3976 ret = get_user_pages_remote(tsk, mm, addr, 1,
3977 gup_flags, &page, &vma);
3979 #ifndef CONFIG_HAVE_IOREMAP_PROT
3983 * Check if this is a VM_IO | VM_PFNMAP VMA, which
3984 * we can access using slightly different code.
3986 vma = find_vma(mm, addr);
3987 if (!vma || vma->vm_start > addr)
3989 if (vma->vm_ops && vma->vm_ops->access)
3990 ret = vma->vm_ops->access(vma, addr, buf,
3998 offset = addr & (PAGE_SIZE-1);
3999 if (bytes > PAGE_SIZE-offset)
4000 bytes = PAGE_SIZE-offset;
4004 copy_to_user_page(vma, page, addr,
4005 maddr + offset, buf, bytes);
4006 set_page_dirty_lock(page);
4008 copy_from_user_page(vma, page, addr,
4009 buf, maddr + offset, bytes);
4018 up_read(&mm->mmap_sem);
4020 return buf - old_buf;
4024 * access_remote_vm - access another process' address space
4025 * @mm: the mm_struct of the target address space
4026 * @addr: start address to access
4027 * @buf: source or destination buffer
4028 * @len: number of bytes to transfer
4029 * @gup_flags: flags modifying lookup behaviour
4031 * The caller must hold a reference on @mm.
4033 int access_remote_vm(struct mm_struct *mm, unsigned long addr,
4034 void *buf, int len, unsigned int gup_flags)
4036 return __access_remote_vm(NULL, mm, addr, buf, len, gup_flags);
4040 * Access another process' address space.
4041 * Source/target buffer must be kernel space,
4042 * Do not walk the page table directly, use get_user_pages
4044 int access_process_vm(struct task_struct *tsk, unsigned long addr,
4045 void *buf, int len, unsigned int gup_flags)
4047 struct mm_struct *mm;
4050 mm = get_task_mm(tsk);
4054 ret = __access_remote_vm(tsk, mm, addr, buf, len, gup_flags);
4062 * Print the name of a VMA.
4064 void print_vma_addr(char *prefix, unsigned long ip)
4066 struct mm_struct *mm = current->mm;
4067 struct vm_area_struct *vma;
4070 * Do not print if we are in atomic
4071 * contexts (in exception stacks, etc.):
4073 if (preempt_count())
4076 down_read(&mm->mmap_sem);
4077 vma = find_vma(mm, ip);
4078 if (vma && vma->vm_file) {
4079 struct file *f = vma->vm_file;
4080 char *buf = (char *)__get_free_page(GFP_KERNEL);
4084 p = file_path(f, buf, PAGE_SIZE);
4087 printk("%s%s[%lx+%lx]", prefix, kbasename(p),
4089 vma->vm_end - vma->vm_start);
4090 free_page((unsigned long)buf);
4093 up_read(&mm->mmap_sem);
4096 #if defined(CONFIG_PROVE_LOCKING) || defined(CONFIG_DEBUG_ATOMIC_SLEEP)
4097 void __might_fault(const char *file, int line)
4100 * Some code (nfs/sunrpc) uses socket ops on kernel memory while
4101 * holding the mmap_sem, this is safe because kernel memory doesn't
4102 * get paged out, therefore we'll never actually fault, and the
4103 * below annotations will generate false positives.
4105 if (segment_eq(get_fs(), KERNEL_DS))
4107 if (pagefault_disabled())
4109 __might_sleep(file, line, 0);
4110 #if defined(CONFIG_DEBUG_ATOMIC_SLEEP)
4112 might_lock_read(¤t->mm->mmap_sem);
4115 EXPORT_SYMBOL(__might_fault);
4118 #if defined(CONFIG_TRANSPARENT_HUGEPAGE) || defined(CONFIG_HUGETLBFS)
4119 static void clear_gigantic_page(struct page *page,
4121 unsigned int pages_per_huge_page)
4124 struct page *p = page;
4127 for (i = 0; i < pages_per_huge_page;
4128 i++, p = mem_map_next(p, page, i)) {
4130 clear_user_highpage(p, addr + i * PAGE_SIZE);
4133 void clear_huge_page(struct page *page,
4134 unsigned long addr, unsigned int pages_per_huge_page)
4138 if (unlikely(pages_per_huge_page > MAX_ORDER_NR_PAGES)) {
4139 clear_gigantic_page(page, addr, pages_per_huge_page);
4144 for (i = 0; i < pages_per_huge_page; i++) {
4146 clear_user_highpage(page + i, addr + i * PAGE_SIZE);
4150 static void copy_user_gigantic_page(struct page *dst, struct page *src,
4152 struct vm_area_struct *vma,
4153 unsigned int pages_per_huge_page)
4156 struct page *dst_base = dst;
4157 struct page *src_base = src;
4159 for (i = 0; i < pages_per_huge_page; ) {
4161 copy_user_highpage(dst, src, addr + i*PAGE_SIZE, vma);
4164 dst = mem_map_next(dst, dst_base, i);
4165 src = mem_map_next(src, src_base, i);
4169 void copy_user_huge_page(struct page *dst, struct page *src,
4170 unsigned long addr, struct vm_area_struct *vma,
4171 unsigned int pages_per_huge_page)
4175 if (unlikely(pages_per_huge_page > MAX_ORDER_NR_PAGES)) {
4176 copy_user_gigantic_page(dst, src, addr, vma,
4177 pages_per_huge_page);
4182 for (i = 0; i < pages_per_huge_page; i++) {
4184 copy_user_highpage(dst + i, src + i, addr + i*PAGE_SIZE, vma);
4187 #endif /* CONFIG_TRANSPARENT_HUGEPAGE || CONFIG_HUGETLBFS */
4189 #if USE_SPLIT_PTE_PTLOCKS && ALLOC_SPLIT_PTLOCKS
4191 static struct kmem_cache *page_ptl_cachep;
4193 void __init ptlock_cache_init(void)
4195 page_ptl_cachep = kmem_cache_create("page->ptl", sizeof(spinlock_t), 0,
4199 bool ptlock_alloc(struct page *page)
4203 ptl = kmem_cache_alloc(page_ptl_cachep, GFP_KERNEL);
4210 void ptlock_free(struct page *page)
4212 kmem_cache_free(page_ptl_cachep, page->ptl);