Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /*
0003  *  linux/mm/memory.c
0004  *
0005  *  Copyright (C) 1991, 1992, 1993, 1994  Linus Torvalds
0006  */
0007 
0008 /*
0009  * demand-loading started 01.12.91 - seems it is high on the list of
0010  * things wanted, and it should be easy to implement. - Linus
0011  */
0012 
0013 /*
0014  * Ok, demand-loading was easy, shared pages a little bit tricker. Shared
0015  * pages started 02.12.91, seems to work. - Linus.
0016  *
0017  * Tested sharing by executing about 30 /bin/sh: under the old kernel it
0018  * would have taken more than the 6M I have free, but it worked well as
0019  * far as I could see.
0020  *
0021  * Also corrected some "invalidate()"s - I wasn't doing enough of them.
0022  */
0023 
0024 /*
0025  * Real VM (paging to/from disk) started 18.12.91. Much more work and
0026  * thought has to go into this. Oh, well..
0027  * 19.12.91  -  works, somewhat. Sometimes I get faults, don't know why.
0028  *      Found it. Everything seems to work now.
0029  * 20.12.91  -  Ok, making the swap-device changeable like the root.
0030  */
0031 
0032 /*
0033  * 05.04.94  -  Multi-page memory management added for v1.1.
0034  *              Idea by Alex Bligh (alex@cconcepts.co.uk)
0035  *
0036  * 16.07.99  -  Support of BIGMEM added by Gerhard Wichert, Siemens AG
0037  *      (Gerhard.Wichert@pdb.siemens.de)
0038  *
0039  * Aug/Sep 2004 Changed to four level page tables (Andi Kleen)
0040  */
0041 
0042 #include <linux/kernel_stat.h>
0043 #include <linux/mm.h>
0044 #include <linux/mm_inline.h>
0045 #include <linux/sched/mm.h>
0046 #include <linux/sched/coredump.h>
0047 #include <linux/sched/numa_balancing.h>
0048 #include <linux/sched/task.h>
0049 #include <linux/hugetlb.h>
0050 #include <linux/mman.h>
0051 #include <linux/swap.h>
0052 #include <linux/highmem.h>
0053 #include <linux/pagemap.h>
0054 #include <linux/memremap.h>
0055 #include <linux/ksm.h>
0056 #include <linux/rmap.h>
0057 #include <linux/export.h>
0058 #include <linux/delayacct.h>
0059 #include <linux/init.h>
0060 #include <linux/pfn_t.h>
0061 #include <linux/writeback.h>
0062 #include <linux/memcontrol.h>
0063 #include <linux/mmu_notifier.h>
0064 #include <linux/swapops.h>
0065 #include <linux/elf.h>
0066 #include <linux/gfp.h>
0067 #include <linux/migrate.h>
0068 #include <linux/string.h>
0069 #include <linux/debugfs.h>
0070 #include <linux/userfaultfd_k.h>
0071 #include <linux/dax.h>
0072 #include <linux/oom.h>
0073 #include <linux/numa.h>
0074 #include <linux/perf_event.h>
0075 #include <linux/ptrace.h>
0076 #include <linux/vmalloc.h>
0077 
0078 #include <trace/events/kmem.h>
0079 
0080 #include <asm/io.h>
0081 #include <asm/mmu_context.h>
0082 #include <asm/pgalloc.h>
0083 #include <linux/uaccess.h>
0084 #include <asm/tlb.h>
0085 #include <asm/tlbflush.h>
0086 
0087 #include "pgalloc-track.h"
0088 #include "internal.h"
0089 #include "swap.h"
0090 
0091 #if defined(LAST_CPUPID_NOT_IN_PAGE_FLAGS) && !defined(CONFIG_COMPILE_TEST)
0092 #warning Unfortunate NUMA and NUMA Balancing config, growing page-frame for last_cpupid.
0093 #endif
0094 
0095 #ifndef CONFIG_NUMA
0096 unsigned long max_mapnr;
0097 EXPORT_SYMBOL(max_mapnr);
0098 
0099 struct page *mem_map;
0100 EXPORT_SYMBOL(mem_map);
0101 #endif
0102 
0103 static vm_fault_t do_fault(struct vm_fault *vmf);
0104 
0105 /*
0106  * A number of key systems in x86 including ioremap() rely on the assumption
0107  * that high_memory defines the upper bound on direct map memory, then end
0108  * of ZONE_NORMAL.  Under CONFIG_DISCONTIG this means that max_low_pfn and
0109  * highstart_pfn must be the same; there must be no gap between ZONE_NORMAL
0110  * and ZONE_HIGHMEM.
0111  */
0112 void *high_memory;
0113 EXPORT_SYMBOL(high_memory);
0114 
0115 /*
0116  * Randomize the address space (stacks, mmaps, brk, etc.).
0117  *
0118  * ( When CONFIG_COMPAT_BRK=y we exclude brk from randomization,
0119  *   as ancient (libc5 based) binaries can segfault. )
0120  */
0121 int randomize_va_space __read_mostly =
0122 #ifdef CONFIG_COMPAT_BRK
0123                     1;
0124 #else
0125                     2;
0126 #endif
0127 
0128 #ifndef arch_faults_on_old_pte
0129 static inline bool arch_faults_on_old_pte(void)
0130 {
0131     /*
0132      * Those arches which don't have hw access flag feature need to
0133      * implement their own helper. By default, "true" means pagefault
0134      * will be hit on old pte.
0135      */
0136     return true;
0137 }
0138 #endif
0139 
0140 #ifndef arch_wants_old_prefaulted_pte
0141 static inline bool arch_wants_old_prefaulted_pte(void)
0142 {
0143     /*
0144      * Transitioning a PTE from 'old' to 'young' can be expensive on
0145      * some architectures, even if it's performed in hardware. By
0146      * default, "false" means prefaulted entries will be 'young'.
0147      */
0148     return false;
0149 }
0150 #endif
0151 
0152 static int __init disable_randmaps(char *s)
0153 {
0154     randomize_va_space = 0;
0155     return 1;
0156 }
0157 __setup("norandmaps", disable_randmaps);
0158 
0159 unsigned long zero_pfn __read_mostly;
0160 EXPORT_SYMBOL(zero_pfn);
0161 
0162 unsigned long highest_memmap_pfn __read_mostly;
0163 
0164 /*
0165  * CONFIG_MMU architectures set up ZERO_PAGE in their paging_init()
0166  */
0167 static int __init init_zero_pfn(void)
0168 {
0169     zero_pfn = page_to_pfn(ZERO_PAGE(0));
0170     return 0;
0171 }
0172 early_initcall(init_zero_pfn);
0173 
0174 void mm_trace_rss_stat(struct mm_struct *mm, int member, long count)
0175 {
0176     trace_rss_stat(mm, member, count);
0177 }
0178 
0179 #if defined(SPLIT_RSS_COUNTING)
0180 
0181 void sync_mm_rss(struct mm_struct *mm)
0182 {
0183     int i;
0184 
0185     for (i = 0; i < NR_MM_COUNTERS; i++) {
0186         if (current->rss_stat.count[i]) {
0187             add_mm_counter(mm, i, current->rss_stat.count[i]);
0188             current->rss_stat.count[i] = 0;
0189         }
0190     }
0191     current->rss_stat.events = 0;
0192 }
0193 
0194 static void add_mm_counter_fast(struct mm_struct *mm, int member, int val)
0195 {
0196     struct task_struct *task = current;
0197 
0198     if (likely(task->mm == mm))
0199         task->rss_stat.count[member] += val;
0200     else
0201         add_mm_counter(mm, member, val);
0202 }
0203 #define inc_mm_counter_fast(mm, member) add_mm_counter_fast(mm, member, 1)
0204 #define dec_mm_counter_fast(mm, member) add_mm_counter_fast(mm, member, -1)
0205 
0206 /* sync counter once per 64 page faults */
0207 #define TASK_RSS_EVENTS_THRESH  (64)
0208 static void check_sync_rss_stat(struct task_struct *task)
0209 {
0210     if (unlikely(task != current))
0211         return;
0212     if (unlikely(task->rss_stat.events++ > TASK_RSS_EVENTS_THRESH))
0213         sync_mm_rss(task->mm);
0214 }
0215 #else /* SPLIT_RSS_COUNTING */
0216 
0217 #define inc_mm_counter_fast(mm, member) inc_mm_counter(mm, member)
0218 #define dec_mm_counter_fast(mm, member) dec_mm_counter(mm, member)
0219 
0220 static void check_sync_rss_stat(struct task_struct *task)
0221 {
0222 }
0223 
0224 #endif /* SPLIT_RSS_COUNTING */
0225 
0226 /*
0227  * Note: this doesn't free the actual pages themselves. That
0228  * has been handled earlier when unmapping all the memory regions.
0229  */
0230 static void free_pte_range(struct mmu_gather *tlb, pmd_t *pmd,
0231                unsigned long addr)
0232 {
0233     pgtable_t token = pmd_pgtable(*pmd);
0234     pmd_clear(pmd);
0235     pte_free_tlb(tlb, token, addr);
0236     mm_dec_nr_ptes(tlb->mm);
0237 }
0238 
0239 static inline void free_pmd_range(struct mmu_gather *tlb, pud_t *pud,
0240                 unsigned long addr, unsigned long end,
0241                 unsigned long floor, unsigned long ceiling)
0242 {
0243     pmd_t *pmd;
0244     unsigned long next;
0245     unsigned long start;
0246 
0247     start = addr;
0248     pmd = pmd_offset(pud, addr);
0249     do {
0250         next = pmd_addr_end(addr, end);
0251         if (pmd_none_or_clear_bad(pmd))
0252             continue;
0253         free_pte_range(tlb, pmd, addr);
0254     } while (pmd++, addr = next, addr != end);
0255 
0256     start &= PUD_MASK;
0257     if (start < floor)
0258         return;
0259     if (ceiling) {
0260         ceiling &= PUD_MASK;
0261         if (!ceiling)
0262             return;
0263     }
0264     if (end - 1 > ceiling - 1)
0265         return;
0266 
0267     pmd = pmd_offset(pud, start);
0268     pud_clear(pud);
0269     pmd_free_tlb(tlb, pmd, start);
0270     mm_dec_nr_pmds(tlb->mm);
0271 }
0272 
0273 static inline void free_pud_range(struct mmu_gather *tlb, p4d_t *p4d,
0274                 unsigned long addr, unsigned long end,
0275                 unsigned long floor, unsigned long ceiling)
0276 {
0277     pud_t *pud;
0278     unsigned long next;
0279     unsigned long start;
0280 
0281     start = addr;
0282     pud = pud_offset(p4d, addr);
0283     do {
0284         next = pud_addr_end(addr, end);
0285         if (pud_none_or_clear_bad(pud))
0286             continue;
0287         free_pmd_range(tlb, pud, addr, next, floor, ceiling);
0288     } while (pud++, addr = next, addr != end);
0289 
0290     start &= P4D_MASK;
0291     if (start < floor)
0292         return;
0293     if (ceiling) {
0294         ceiling &= P4D_MASK;
0295         if (!ceiling)
0296             return;
0297     }
0298     if (end - 1 > ceiling - 1)
0299         return;
0300 
0301     pud = pud_offset(p4d, start);
0302     p4d_clear(p4d);
0303     pud_free_tlb(tlb, pud, start);
0304     mm_dec_nr_puds(tlb->mm);
0305 }
0306 
0307 static inline void free_p4d_range(struct mmu_gather *tlb, pgd_t *pgd,
0308                 unsigned long addr, unsigned long end,
0309                 unsigned long floor, unsigned long ceiling)
0310 {
0311     p4d_t *p4d;
0312     unsigned long next;
0313     unsigned long start;
0314 
0315     start = addr;
0316     p4d = p4d_offset(pgd, addr);
0317     do {
0318         next = p4d_addr_end(addr, end);
0319         if (p4d_none_or_clear_bad(p4d))
0320             continue;
0321         free_pud_range(tlb, p4d, addr, next, floor, ceiling);
0322     } while (p4d++, addr = next, addr != end);
0323 
0324     start &= PGDIR_MASK;
0325     if (start < floor)
0326         return;
0327     if (ceiling) {
0328         ceiling &= PGDIR_MASK;
0329         if (!ceiling)
0330             return;
0331     }
0332     if (end - 1 > ceiling - 1)
0333         return;
0334 
0335     p4d = p4d_offset(pgd, start);
0336     pgd_clear(pgd);
0337     p4d_free_tlb(tlb, p4d, start);
0338 }
0339 
0340 /*
0341  * This function frees user-level page tables of a process.
0342  */
0343 void free_pgd_range(struct mmu_gather *tlb,
0344             unsigned long addr, unsigned long end,
0345             unsigned long floor, unsigned long ceiling)
0346 {
0347     pgd_t *pgd;
0348     unsigned long next;
0349 
0350     /*
0351      * The next few lines have given us lots of grief...
0352      *
0353      * Why are we testing PMD* at this top level?  Because often
0354      * there will be no work to do at all, and we'd prefer not to
0355      * go all the way down to the bottom just to discover that.
0356      *
0357      * Why all these "- 1"s?  Because 0 represents both the bottom
0358      * of the address space and the top of it (using -1 for the
0359      * top wouldn't help much: the masks would do the wrong thing).
0360      * The rule is that addr 0 and floor 0 refer to the bottom of
0361      * the address space, but end 0 and ceiling 0 refer to the top
0362      * Comparisons need to use "end - 1" and "ceiling - 1" (though
0363      * that end 0 case should be mythical).
0364      *
0365      * Wherever addr is brought up or ceiling brought down, we must
0366      * be careful to reject "the opposite 0" before it confuses the
0367      * subsequent tests.  But what about where end is brought down
0368      * by PMD_SIZE below? no, end can't go down to 0 there.
0369      *
0370      * Whereas we round start (addr) and ceiling down, by different
0371      * masks at different levels, in order to test whether a table
0372      * now has no other vmas using it, so can be freed, we don't
0373      * bother to round floor or end up - the tests don't need that.
0374      */
0375 
0376     addr &= PMD_MASK;
0377     if (addr < floor) {
0378         addr += PMD_SIZE;
0379         if (!addr)
0380             return;
0381     }
0382     if (ceiling) {
0383         ceiling &= PMD_MASK;
0384         if (!ceiling)
0385             return;
0386     }
0387     if (end - 1 > ceiling - 1)
0388         end -= PMD_SIZE;
0389     if (addr > end - 1)
0390         return;
0391     /*
0392      * We add page table cache pages with PAGE_SIZE,
0393      * (see pte_free_tlb()), flush the tlb if we need
0394      */
0395     tlb_change_page_size(tlb, PAGE_SIZE);
0396     pgd = pgd_offset(tlb->mm, addr);
0397     do {
0398         next = pgd_addr_end(addr, end);
0399         if (pgd_none_or_clear_bad(pgd))
0400             continue;
0401         free_p4d_range(tlb, pgd, addr, next, floor, ceiling);
0402     } while (pgd++, addr = next, addr != end);
0403 }
0404 
0405 void free_pgtables(struct mmu_gather *tlb, struct vm_area_struct *vma,
0406         unsigned long floor, unsigned long ceiling)
0407 {
0408     while (vma) {
0409         struct vm_area_struct *next = vma->vm_next;
0410         unsigned long addr = vma->vm_start;
0411 
0412         /*
0413          * Hide vma from rmap and truncate_pagecache before freeing
0414          * pgtables
0415          */
0416         unlink_anon_vmas(vma);
0417         unlink_file_vma(vma);
0418 
0419         if (is_vm_hugetlb_page(vma)) {
0420             hugetlb_free_pgd_range(tlb, addr, vma->vm_end,
0421                 floor, next ? next->vm_start : ceiling);
0422         } else {
0423             /*
0424              * Optimization: gather nearby vmas into one call down
0425              */
0426             while (next && next->vm_start <= vma->vm_end + PMD_SIZE
0427                    && !is_vm_hugetlb_page(next)) {
0428                 vma = next;
0429                 next = vma->vm_next;
0430                 unlink_anon_vmas(vma);
0431                 unlink_file_vma(vma);
0432             }
0433             free_pgd_range(tlb, addr, vma->vm_end,
0434                 floor, next ? next->vm_start : ceiling);
0435         }
0436         vma = next;
0437     }
0438 }
0439 
0440 void pmd_install(struct mm_struct *mm, pmd_t *pmd, pgtable_t *pte)
0441 {
0442     spinlock_t *ptl = pmd_lock(mm, pmd);
0443 
0444     if (likely(pmd_none(*pmd))) {   /* Has another populated it ? */
0445         mm_inc_nr_ptes(mm);
0446         /*
0447          * Ensure all pte setup (eg. pte page lock and page clearing) are
0448          * visible before the pte is made visible to other CPUs by being
0449          * put into page tables.
0450          *
0451          * The other side of the story is the pointer chasing in the page
0452          * table walking code (when walking the page table without locking;
0453          * ie. most of the time). Fortunately, these data accesses consist
0454          * of a chain of data-dependent loads, meaning most CPUs (alpha
0455          * being the notable exception) will already guarantee loads are
0456          * seen in-order. See the alpha page table accessors for the
0457          * smp_rmb() barriers in page table walking code.
0458          */
0459         smp_wmb(); /* Could be smp_wmb__xxx(before|after)_spin_lock */
0460         pmd_populate(mm, pmd, *pte);
0461         *pte = NULL;
0462     }
0463     spin_unlock(ptl);
0464 }
0465 
0466 int __pte_alloc(struct mm_struct *mm, pmd_t *pmd)
0467 {
0468     pgtable_t new = pte_alloc_one(mm);
0469     if (!new)
0470         return -ENOMEM;
0471 
0472     pmd_install(mm, pmd, &new);
0473     if (new)
0474         pte_free(mm, new);
0475     return 0;
0476 }
0477 
0478 int __pte_alloc_kernel(pmd_t *pmd)
0479 {
0480     pte_t *new = pte_alloc_one_kernel(&init_mm);
0481     if (!new)
0482         return -ENOMEM;
0483 
0484     spin_lock(&init_mm.page_table_lock);
0485     if (likely(pmd_none(*pmd))) {   /* Has another populated it ? */
0486         smp_wmb(); /* See comment in pmd_install() */
0487         pmd_populate_kernel(&init_mm, pmd, new);
0488         new = NULL;
0489     }
0490     spin_unlock(&init_mm.page_table_lock);
0491     if (new)
0492         pte_free_kernel(&init_mm, new);
0493     return 0;
0494 }
0495 
0496 static inline void init_rss_vec(int *rss)
0497 {
0498     memset(rss, 0, sizeof(int) * NR_MM_COUNTERS);
0499 }
0500 
0501 static inline void add_mm_rss_vec(struct mm_struct *mm, int *rss)
0502 {
0503     int i;
0504 
0505     if (current->mm == mm)
0506         sync_mm_rss(mm);
0507     for (i = 0; i < NR_MM_COUNTERS; i++)
0508         if (rss[i])
0509             add_mm_counter(mm, i, rss[i]);
0510 }
0511 
0512 /*
0513  * This function is called to print an error when a bad pte
0514  * is found. For example, we might have a PFN-mapped pte in
0515  * a region that doesn't allow it.
0516  *
0517  * The calling function must still handle the error.
0518  */
0519 static void print_bad_pte(struct vm_area_struct *vma, unsigned long addr,
0520               pte_t pte, struct page *page)
0521 {
0522     pgd_t *pgd = pgd_offset(vma->vm_mm, addr);
0523     p4d_t *p4d = p4d_offset(pgd, addr);
0524     pud_t *pud = pud_offset(p4d, addr);
0525     pmd_t *pmd = pmd_offset(pud, addr);
0526     struct address_space *mapping;
0527     pgoff_t index;
0528     static unsigned long resume;
0529     static unsigned long nr_shown;
0530     static unsigned long nr_unshown;
0531 
0532     /*
0533      * Allow a burst of 60 reports, then keep quiet for that minute;
0534      * or allow a steady drip of one report per second.
0535      */
0536     if (nr_shown == 60) {
0537         if (time_before(jiffies, resume)) {
0538             nr_unshown++;
0539             return;
0540         }
0541         if (nr_unshown) {
0542             pr_alert("BUG: Bad page map: %lu messages suppressed\n",
0543                  nr_unshown);
0544             nr_unshown = 0;
0545         }
0546         nr_shown = 0;
0547     }
0548     if (nr_shown++ == 0)
0549         resume = jiffies + 60 * HZ;
0550 
0551     mapping = vma->vm_file ? vma->vm_file->f_mapping : NULL;
0552     index = linear_page_index(vma, addr);
0553 
0554     pr_alert("BUG: Bad page map in process %s  pte:%08llx pmd:%08llx\n",
0555          current->comm,
0556          (long long)pte_val(pte), (long long)pmd_val(*pmd));
0557     if (page)
0558         dump_page(page, "bad pte");
0559     pr_alert("addr:%px vm_flags:%08lx anon_vma:%px mapping:%px index:%lx\n",
0560          (void *)addr, vma->vm_flags, vma->anon_vma, mapping, index);
0561     pr_alert("file:%pD fault:%ps mmap:%ps read_folio:%ps\n",
0562          vma->vm_file,
0563          vma->vm_ops ? vma->vm_ops->fault : NULL,
0564          vma->vm_file ? vma->vm_file->f_op->mmap : NULL,
0565          mapping ? mapping->a_ops->read_folio : NULL);
0566     dump_stack();
0567     add_taint(TAINT_BAD_PAGE, LOCKDEP_NOW_UNRELIABLE);
0568 }
0569 
0570 /*
0571  * vm_normal_page -- This function gets the "struct page" associated with a pte.
0572  *
0573  * "Special" mappings do not wish to be associated with a "struct page" (either
0574  * it doesn't exist, or it exists but they don't want to touch it). In this
0575  * case, NULL is returned here. "Normal" mappings do have a struct page.
0576  *
0577  * There are 2 broad cases. Firstly, an architecture may define a pte_special()
0578  * pte bit, in which case this function is trivial. Secondly, an architecture
0579  * may not have a spare pte bit, which requires a more complicated scheme,
0580  * described below.
0581  *
0582  * A raw VM_PFNMAP mapping (ie. one that is not COWed) is always considered a
0583  * special mapping (even if there are underlying and valid "struct pages").
0584  * COWed pages of a VM_PFNMAP are always normal.
0585  *
0586  * The way we recognize COWed pages within VM_PFNMAP mappings is through the
0587  * rules set up by "remap_pfn_range()": the vma will have the VM_PFNMAP bit
0588  * set, and the vm_pgoff will point to the first PFN mapped: thus every special
0589  * mapping will always honor the rule
0590  *
0591  *  pfn_of_page == vma->vm_pgoff + ((addr - vma->vm_start) >> PAGE_SHIFT)
0592  *
0593  * And for normal mappings this is false.
0594  *
0595  * This restricts such mappings to be a linear translation from virtual address
0596  * to pfn. To get around this restriction, we allow arbitrary mappings so long
0597  * as the vma is not a COW mapping; in that case, we know that all ptes are
0598  * special (because none can have been COWed).
0599  *
0600  *
0601  * In order to support COW of arbitrary special mappings, we have VM_MIXEDMAP.
0602  *
0603  * VM_MIXEDMAP mappings can likewise contain memory with or without "struct
0604  * page" backing, however the difference is that _all_ pages with a struct
0605  * page (that is, those where pfn_valid is true) are refcounted and considered
0606  * normal pages by the VM. The disadvantage is that pages are refcounted
0607  * (which can be slower and simply not an option for some PFNMAP users). The
0608  * advantage is that we don't have to follow the strict linearity rule of
0609  * PFNMAP mappings in order to support COWable mappings.
0610  *
0611  */
0612 struct page *vm_normal_page(struct vm_area_struct *vma, unsigned long addr,
0613                 pte_t pte)
0614 {
0615     unsigned long pfn = pte_pfn(pte);
0616 
0617     if (IS_ENABLED(CONFIG_ARCH_HAS_PTE_SPECIAL)) {
0618         if (likely(!pte_special(pte)))
0619             goto check_pfn;
0620         if (vma->vm_ops && vma->vm_ops->find_special_page)
0621             return vma->vm_ops->find_special_page(vma, addr);
0622         if (vma->vm_flags & (VM_PFNMAP | VM_MIXEDMAP))
0623             return NULL;
0624         if (is_zero_pfn(pfn))
0625             return NULL;
0626         if (pte_devmap(pte))
0627         /*
0628          * NOTE: New users of ZONE_DEVICE will not set pte_devmap()
0629          * and will have refcounts incremented on their struct pages
0630          * when they are inserted into PTEs, thus they are safe to
0631          * return here. Legacy ZONE_DEVICE pages that set pte_devmap()
0632          * do not have refcounts. Example of legacy ZONE_DEVICE is
0633          * MEMORY_DEVICE_FS_DAX type in pmem or virtio_fs drivers.
0634          */
0635             return NULL;
0636 
0637         print_bad_pte(vma, addr, pte, NULL);
0638         return NULL;
0639     }
0640 
0641     /* !CONFIG_ARCH_HAS_PTE_SPECIAL case follows: */
0642 
0643     if (unlikely(vma->vm_flags & (VM_PFNMAP|VM_MIXEDMAP))) {
0644         if (vma->vm_flags & VM_MIXEDMAP) {
0645             if (!pfn_valid(pfn))
0646                 return NULL;
0647             goto out;
0648         } else {
0649             unsigned long off;
0650             off = (addr - vma->vm_start) >> PAGE_SHIFT;
0651             if (pfn == vma->vm_pgoff + off)
0652                 return NULL;
0653             if (!is_cow_mapping(vma->vm_flags))
0654                 return NULL;
0655         }
0656     }
0657 
0658     if (is_zero_pfn(pfn))
0659         return NULL;
0660 
0661 check_pfn:
0662     if (unlikely(pfn > highest_memmap_pfn)) {
0663         print_bad_pte(vma, addr, pte, NULL);
0664         return NULL;
0665     }
0666 
0667     /*
0668      * NOTE! We still have PageReserved() pages in the page tables.
0669      * eg. VDSO mappings can cause them to exist.
0670      */
0671 out:
0672     return pfn_to_page(pfn);
0673 }
0674 
0675 #ifdef CONFIG_TRANSPARENT_HUGEPAGE
0676 struct page *vm_normal_page_pmd(struct vm_area_struct *vma, unsigned long addr,
0677                 pmd_t pmd)
0678 {
0679     unsigned long pfn = pmd_pfn(pmd);
0680 
0681     /*
0682      * There is no pmd_special() but there may be special pmds, e.g.
0683      * in a direct-access (dax) mapping, so let's just replicate the
0684      * !CONFIG_ARCH_HAS_PTE_SPECIAL case from vm_normal_page() here.
0685      */
0686     if (unlikely(vma->vm_flags & (VM_PFNMAP|VM_MIXEDMAP))) {
0687         if (vma->vm_flags & VM_MIXEDMAP) {
0688             if (!pfn_valid(pfn))
0689                 return NULL;
0690             goto out;
0691         } else {
0692             unsigned long off;
0693             off = (addr - vma->vm_start) >> PAGE_SHIFT;
0694             if (pfn == vma->vm_pgoff + off)
0695                 return NULL;
0696             if (!is_cow_mapping(vma->vm_flags))
0697                 return NULL;
0698         }
0699     }
0700 
0701     if (pmd_devmap(pmd))
0702         return NULL;
0703     if (is_huge_zero_pmd(pmd))
0704         return NULL;
0705     if (unlikely(pfn > highest_memmap_pfn))
0706         return NULL;
0707 
0708     /*
0709      * NOTE! We still have PageReserved() pages in the page tables.
0710      * eg. VDSO mappings can cause them to exist.
0711      */
0712 out:
0713     return pfn_to_page(pfn);
0714 }
0715 #endif
0716 
0717 static void restore_exclusive_pte(struct vm_area_struct *vma,
0718                   struct page *page, unsigned long address,
0719                   pte_t *ptep)
0720 {
0721     pte_t pte;
0722     swp_entry_t entry;
0723 
0724     pte = pte_mkold(mk_pte(page, READ_ONCE(vma->vm_page_prot)));
0725     if (pte_swp_soft_dirty(*ptep))
0726         pte = pte_mksoft_dirty(pte);
0727 
0728     entry = pte_to_swp_entry(*ptep);
0729     if (pte_swp_uffd_wp(*ptep))
0730         pte = pte_mkuffd_wp(pte);
0731     else if (is_writable_device_exclusive_entry(entry))
0732         pte = maybe_mkwrite(pte_mkdirty(pte), vma);
0733 
0734     VM_BUG_ON(pte_write(pte) && !(PageAnon(page) && PageAnonExclusive(page)));
0735 
0736     /*
0737      * No need to take a page reference as one was already
0738      * created when the swap entry was made.
0739      */
0740     if (PageAnon(page))
0741         page_add_anon_rmap(page, vma, address, RMAP_NONE);
0742     else
0743         /*
0744          * Currently device exclusive access only supports anonymous
0745          * memory so the entry shouldn't point to a filebacked page.
0746          */
0747         WARN_ON_ONCE(1);
0748 
0749     set_pte_at(vma->vm_mm, address, ptep, pte);
0750 
0751     /*
0752      * No need to invalidate - it was non-present before. However
0753      * secondary CPUs may have mappings that need invalidating.
0754      */
0755     update_mmu_cache(vma, address, ptep);
0756 }
0757 
0758 /*
0759  * Tries to restore an exclusive pte if the page lock can be acquired without
0760  * sleeping.
0761  */
0762 static int
0763 try_restore_exclusive_pte(pte_t *src_pte, struct vm_area_struct *vma,
0764             unsigned long addr)
0765 {
0766     swp_entry_t entry = pte_to_swp_entry(*src_pte);
0767     struct page *page = pfn_swap_entry_to_page(entry);
0768 
0769     if (trylock_page(page)) {
0770         restore_exclusive_pte(vma, page, addr, src_pte);
0771         unlock_page(page);
0772         return 0;
0773     }
0774 
0775     return -EBUSY;
0776 }
0777 
0778 /*
0779  * copy one vm_area from one task to the other. Assumes the page tables
0780  * already present in the new task to be cleared in the whole range
0781  * covered by this vma.
0782  */
0783 
0784 static unsigned long
0785 copy_nonpresent_pte(struct mm_struct *dst_mm, struct mm_struct *src_mm,
0786         pte_t *dst_pte, pte_t *src_pte, struct vm_area_struct *dst_vma,
0787         struct vm_area_struct *src_vma, unsigned long addr, int *rss)
0788 {
0789     unsigned long vm_flags = dst_vma->vm_flags;
0790     pte_t pte = *src_pte;
0791     struct page *page;
0792     swp_entry_t entry = pte_to_swp_entry(pte);
0793 
0794     if (likely(!non_swap_entry(entry))) {
0795         if (swap_duplicate(entry) < 0)
0796             return -EIO;
0797 
0798         /* make sure dst_mm is on swapoff's mmlist. */
0799         if (unlikely(list_empty(&dst_mm->mmlist))) {
0800             spin_lock(&mmlist_lock);
0801             if (list_empty(&dst_mm->mmlist))
0802                 list_add(&dst_mm->mmlist,
0803                         &src_mm->mmlist);
0804             spin_unlock(&mmlist_lock);
0805         }
0806         /* Mark the swap entry as shared. */
0807         if (pte_swp_exclusive(*src_pte)) {
0808             pte = pte_swp_clear_exclusive(*src_pte);
0809             set_pte_at(src_mm, addr, src_pte, pte);
0810         }
0811         rss[MM_SWAPENTS]++;
0812     } else if (is_migration_entry(entry)) {
0813         page = pfn_swap_entry_to_page(entry);
0814 
0815         rss[mm_counter(page)]++;
0816 
0817         if (!is_readable_migration_entry(entry) &&
0818                 is_cow_mapping(vm_flags)) {
0819             /*
0820              * COW mappings require pages in both parent and child
0821              * to be set to read. A previously exclusive entry is
0822              * now shared.
0823              */
0824             entry = make_readable_migration_entry(
0825                             swp_offset(entry));
0826             pte = swp_entry_to_pte(entry);
0827             if (pte_swp_soft_dirty(*src_pte))
0828                 pte = pte_swp_mksoft_dirty(pte);
0829             if (pte_swp_uffd_wp(*src_pte))
0830                 pte = pte_swp_mkuffd_wp(pte);
0831             set_pte_at(src_mm, addr, src_pte, pte);
0832         }
0833     } else if (is_device_private_entry(entry)) {
0834         page = pfn_swap_entry_to_page(entry);
0835 
0836         /*
0837          * Update rss count even for unaddressable pages, as
0838          * they should treated just like normal pages in this
0839          * respect.
0840          *
0841          * We will likely want to have some new rss counters
0842          * for unaddressable pages, at some point. But for now
0843          * keep things as they are.
0844          */
0845         get_page(page);
0846         rss[mm_counter(page)]++;
0847         /* Cannot fail as these pages cannot get pinned. */
0848         BUG_ON(page_try_dup_anon_rmap(page, false, src_vma));
0849 
0850         /*
0851          * We do not preserve soft-dirty information, because so
0852          * far, checkpoint/restore is the only feature that
0853          * requires that. And checkpoint/restore does not work
0854          * when a device driver is involved (you cannot easily
0855          * save and restore device driver state).
0856          */
0857         if (is_writable_device_private_entry(entry) &&
0858             is_cow_mapping(vm_flags)) {
0859             entry = make_readable_device_private_entry(
0860                             swp_offset(entry));
0861             pte = swp_entry_to_pte(entry);
0862             if (pte_swp_uffd_wp(*src_pte))
0863                 pte = pte_swp_mkuffd_wp(pte);
0864             set_pte_at(src_mm, addr, src_pte, pte);
0865         }
0866     } else if (is_device_exclusive_entry(entry)) {
0867         /*
0868          * Make device exclusive entries present by restoring the
0869          * original entry then copying as for a present pte. Device
0870          * exclusive entries currently only support private writable
0871          * (ie. COW) mappings.
0872          */
0873         VM_BUG_ON(!is_cow_mapping(src_vma->vm_flags));
0874         if (try_restore_exclusive_pte(src_pte, src_vma, addr))
0875             return -EBUSY;
0876         return -ENOENT;
0877     } else if (is_pte_marker_entry(entry)) {
0878         /*
0879          * We're copying the pgtable should only because dst_vma has
0880          * uffd-wp enabled, do sanity check.
0881          */
0882         WARN_ON_ONCE(!userfaultfd_wp(dst_vma));
0883         set_pte_at(dst_mm, addr, dst_pte, pte);
0884         return 0;
0885     }
0886     if (!userfaultfd_wp(dst_vma))
0887         pte = pte_swp_clear_uffd_wp(pte);
0888     set_pte_at(dst_mm, addr, dst_pte, pte);
0889     return 0;
0890 }
0891 
0892 /*
0893  * Copy a present and normal page.
0894  *
0895  * NOTE! The usual case is that this isn't required;
0896  * instead, the caller can just increase the page refcount
0897  * and re-use the pte the traditional way.
0898  *
0899  * And if we need a pre-allocated page but don't yet have
0900  * one, return a negative error to let the preallocation
0901  * code know so that it can do so outside the page table
0902  * lock.
0903  */
0904 static inline int
0905 copy_present_page(struct vm_area_struct *dst_vma, struct vm_area_struct *src_vma,
0906           pte_t *dst_pte, pte_t *src_pte, unsigned long addr, int *rss,
0907           struct page **prealloc, struct page *page)
0908 {
0909     struct page *new_page;
0910     pte_t pte;
0911 
0912     new_page = *prealloc;
0913     if (!new_page)
0914         return -EAGAIN;
0915 
0916     /*
0917      * We have a prealloc page, all good!  Take it
0918      * over and copy the page & arm it.
0919      */
0920     *prealloc = NULL;
0921     copy_user_highpage(new_page, page, addr, src_vma);
0922     __SetPageUptodate(new_page);
0923     page_add_new_anon_rmap(new_page, dst_vma, addr);
0924     lru_cache_add_inactive_or_unevictable(new_page, dst_vma);
0925     rss[mm_counter(new_page)]++;
0926 
0927     /* All done, just insert the new page copy in the child */
0928     pte = mk_pte(new_page, dst_vma->vm_page_prot);
0929     pte = maybe_mkwrite(pte_mkdirty(pte), dst_vma);
0930     if (userfaultfd_pte_wp(dst_vma, *src_pte))
0931         /* Uffd-wp needs to be delivered to dest pte as well */
0932         pte = pte_wrprotect(pte_mkuffd_wp(pte));
0933     set_pte_at(dst_vma->vm_mm, addr, dst_pte, pte);
0934     return 0;
0935 }
0936 
0937 /*
0938  * Copy one pte.  Returns 0 if succeeded, or -EAGAIN if one preallocated page
0939  * is required to copy this pte.
0940  */
0941 static inline int
0942 copy_present_pte(struct vm_area_struct *dst_vma, struct vm_area_struct *src_vma,
0943          pte_t *dst_pte, pte_t *src_pte, unsigned long addr, int *rss,
0944          struct page **prealloc)
0945 {
0946     struct mm_struct *src_mm = src_vma->vm_mm;
0947     unsigned long vm_flags = src_vma->vm_flags;
0948     pte_t pte = *src_pte;
0949     struct page *page;
0950 
0951     page = vm_normal_page(src_vma, addr, pte);
0952     if (page && PageAnon(page)) {
0953         /*
0954          * If this page may have been pinned by the parent process,
0955          * copy the page immediately for the child so that we'll always
0956          * guarantee the pinned page won't be randomly replaced in the
0957          * future.
0958          */
0959         get_page(page);
0960         if (unlikely(page_try_dup_anon_rmap(page, false, src_vma))) {
0961             /* Page maybe pinned, we have to copy. */
0962             put_page(page);
0963             return copy_present_page(dst_vma, src_vma, dst_pte, src_pte,
0964                          addr, rss, prealloc, page);
0965         }
0966         rss[mm_counter(page)]++;
0967     } else if (page) {
0968         get_page(page);
0969         page_dup_file_rmap(page, false);
0970         rss[mm_counter(page)]++;
0971     }
0972 
0973     /*
0974      * If it's a COW mapping, write protect it both
0975      * in the parent and the child
0976      */
0977     if (is_cow_mapping(vm_flags) && pte_write(pte)) {
0978         ptep_set_wrprotect(src_mm, addr, src_pte);
0979         pte = pte_wrprotect(pte);
0980     }
0981     VM_BUG_ON(page && PageAnon(page) && PageAnonExclusive(page));
0982 
0983     /*
0984      * If it's a shared mapping, mark it clean in
0985      * the child
0986      */
0987     if (vm_flags & VM_SHARED)
0988         pte = pte_mkclean(pte);
0989     pte = pte_mkold(pte);
0990 
0991     if (!userfaultfd_wp(dst_vma))
0992         pte = pte_clear_uffd_wp(pte);
0993 
0994     set_pte_at(dst_vma->vm_mm, addr, dst_pte, pte);
0995     return 0;
0996 }
0997 
0998 static inline struct page *
0999 page_copy_prealloc(struct mm_struct *src_mm, struct vm_area_struct *vma,
1000            unsigned long addr)
1001 {
1002     struct page *new_page;
1003 
1004     new_page = alloc_page_vma(GFP_HIGHUSER_MOVABLE, vma, addr);
1005     if (!new_page)
1006         return NULL;
1007 
1008     if (mem_cgroup_charge(page_folio(new_page), src_mm, GFP_KERNEL)) {
1009         put_page(new_page);
1010         return NULL;
1011     }
1012     cgroup_throttle_swaprate(new_page, GFP_KERNEL);
1013 
1014     return new_page;
1015 }
1016 
1017 static int
1018 copy_pte_range(struct vm_area_struct *dst_vma, struct vm_area_struct *src_vma,
1019            pmd_t *dst_pmd, pmd_t *src_pmd, unsigned long addr,
1020            unsigned long end)
1021 {
1022     struct mm_struct *dst_mm = dst_vma->vm_mm;
1023     struct mm_struct *src_mm = src_vma->vm_mm;
1024     pte_t *orig_src_pte, *orig_dst_pte;
1025     pte_t *src_pte, *dst_pte;
1026     spinlock_t *src_ptl, *dst_ptl;
1027     int progress, ret = 0;
1028     int rss[NR_MM_COUNTERS];
1029     swp_entry_t entry = (swp_entry_t){0};
1030     struct page *prealloc = NULL;
1031 
1032 again:
1033     progress = 0;
1034     init_rss_vec(rss);
1035 
1036     dst_pte = pte_alloc_map_lock(dst_mm, dst_pmd, addr, &dst_ptl);
1037     if (!dst_pte) {
1038         ret = -ENOMEM;
1039         goto out;
1040     }
1041     src_pte = pte_offset_map(src_pmd, addr);
1042     src_ptl = pte_lockptr(src_mm, src_pmd);
1043     spin_lock_nested(src_ptl, SINGLE_DEPTH_NESTING);
1044     orig_src_pte = src_pte;
1045     orig_dst_pte = dst_pte;
1046     arch_enter_lazy_mmu_mode();
1047 
1048     do {
1049         /*
1050          * We are holding two locks at this point - either of them
1051          * could generate latencies in another task on another CPU.
1052          */
1053         if (progress >= 32) {
1054             progress = 0;
1055             if (need_resched() ||
1056                 spin_needbreak(src_ptl) || spin_needbreak(dst_ptl))
1057                 break;
1058         }
1059         if (pte_none(*src_pte)) {
1060             progress++;
1061             continue;
1062         }
1063         if (unlikely(!pte_present(*src_pte))) {
1064             ret = copy_nonpresent_pte(dst_mm, src_mm,
1065                           dst_pte, src_pte,
1066                           dst_vma, src_vma,
1067                           addr, rss);
1068             if (ret == -EIO) {
1069                 entry = pte_to_swp_entry(*src_pte);
1070                 break;
1071             } else if (ret == -EBUSY) {
1072                 break;
1073             } else if (!ret) {
1074                 progress += 8;
1075                 continue;
1076             }
1077 
1078             /*
1079              * Device exclusive entry restored, continue by copying
1080              * the now present pte.
1081              */
1082             WARN_ON_ONCE(ret != -ENOENT);
1083         }
1084         /* copy_present_pte() will clear `*prealloc' if consumed */
1085         ret = copy_present_pte(dst_vma, src_vma, dst_pte, src_pte,
1086                        addr, rss, &prealloc);
1087         /*
1088          * If we need a pre-allocated page for this pte, drop the
1089          * locks, allocate, and try again.
1090          */
1091         if (unlikely(ret == -EAGAIN))
1092             break;
1093         if (unlikely(prealloc)) {
1094             /*
1095              * pre-alloc page cannot be reused by next time so as
1096              * to strictly follow mempolicy (e.g., alloc_page_vma()
1097              * will allocate page according to address).  This
1098              * could only happen if one pinned pte changed.
1099              */
1100             put_page(prealloc);
1101             prealloc = NULL;
1102         }
1103         progress += 8;
1104     } while (dst_pte++, src_pte++, addr += PAGE_SIZE, addr != end);
1105 
1106     arch_leave_lazy_mmu_mode();
1107     spin_unlock(src_ptl);
1108     pte_unmap(orig_src_pte);
1109     add_mm_rss_vec(dst_mm, rss);
1110     pte_unmap_unlock(orig_dst_pte, dst_ptl);
1111     cond_resched();
1112 
1113     if (ret == -EIO) {
1114         VM_WARN_ON_ONCE(!entry.val);
1115         if (add_swap_count_continuation(entry, GFP_KERNEL) < 0) {
1116             ret = -ENOMEM;
1117             goto out;
1118         }
1119         entry.val = 0;
1120     } else if (ret == -EBUSY) {
1121         goto out;
1122     } else if (ret ==  -EAGAIN) {
1123         prealloc = page_copy_prealloc(src_mm, src_vma, addr);
1124         if (!prealloc)
1125             return -ENOMEM;
1126     } else if (ret) {
1127         VM_WARN_ON_ONCE(1);
1128     }
1129 
1130     /* We've captured and resolved the error. Reset, try again. */
1131     ret = 0;
1132 
1133     if (addr != end)
1134         goto again;
1135 out:
1136     if (unlikely(prealloc))
1137         put_page(prealloc);
1138     return ret;
1139 }
1140 
1141 static inline int
1142 copy_pmd_range(struct vm_area_struct *dst_vma, struct vm_area_struct *src_vma,
1143            pud_t *dst_pud, pud_t *src_pud, unsigned long addr,
1144            unsigned long end)
1145 {
1146     struct mm_struct *dst_mm = dst_vma->vm_mm;
1147     struct mm_struct *src_mm = src_vma->vm_mm;
1148     pmd_t *src_pmd, *dst_pmd;
1149     unsigned long next;
1150 
1151     dst_pmd = pmd_alloc(dst_mm, dst_pud, addr);
1152     if (!dst_pmd)
1153         return -ENOMEM;
1154     src_pmd = pmd_offset(src_pud, addr);
1155     do {
1156         next = pmd_addr_end(addr, end);
1157         if (is_swap_pmd(*src_pmd) || pmd_trans_huge(*src_pmd)
1158             || pmd_devmap(*src_pmd)) {
1159             int err;
1160             VM_BUG_ON_VMA(next-addr != HPAGE_PMD_SIZE, src_vma);
1161             err = copy_huge_pmd(dst_mm, src_mm, dst_pmd, src_pmd,
1162                         addr, dst_vma, src_vma);
1163             if (err == -ENOMEM)
1164                 return -ENOMEM;
1165             if (!err)
1166                 continue;
1167             /* fall through */
1168         }
1169         if (pmd_none_or_clear_bad(src_pmd))
1170             continue;
1171         if (copy_pte_range(dst_vma, src_vma, dst_pmd, src_pmd,
1172                    addr, next))
1173             return -ENOMEM;
1174     } while (dst_pmd++, src_pmd++, addr = next, addr != end);
1175     return 0;
1176 }
1177 
1178 static inline int
1179 copy_pud_range(struct vm_area_struct *dst_vma, struct vm_area_struct *src_vma,
1180            p4d_t *dst_p4d, p4d_t *src_p4d, unsigned long addr,
1181            unsigned long end)
1182 {
1183     struct mm_struct *dst_mm = dst_vma->vm_mm;
1184     struct mm_struct *src_mm = src_vma->vm_mm;
1185     pud_t *src_pud, *dst_pud;
1186     unsigned long next;
1187 
1188     dst_pud = pud_alloc(dst_mm, dst_p4d, addr);
1189     if (!dst_pud)
1190         return -ENOMEM;
1191     src_pud = pud_offset(src_p4d, addr);
1192     do {
1193         next = pud_addr_end(addr, end);
1194         if (pud_trans_huge(*src_pud) || pud_devmap(*src_pud)) {
1195             int err;
1196 
1197             VM_BUG_ON_VMA(next-addr != HPAGE_PUD_SIZE, src_vma);
1198             err = copy_huge_pud(dst_mm, src_mm,
1199                         dst_pud, src_pud, addr, src_vma);
1200             if (err == -ENOMEM)
1201                 return -ENOMEM;
1202             if (!err)
1203                 continue;
1204             /* fall through */
1205         }
1206         if (pud_none_or_clear_bad(src_pud))
1207             continue;
1208         if (copy_pmd_range(dst_vma, src_vma, dst_pud, src_pud,
1209                    addr, next))
1210             return -ENOMEM;
1211     } while (dst_pud++, src_pud++, addr = next, addr != end);
1212     return 0;
1213 }
1214 
1215 static inline int
1216 copy_p4d_range(struct vm_area_struct *dst_vma, struct vm_area_struct *src_vma,
1217            pgd_t *dst_pgd, pgd_t *src_pgd, unsigned long addr,
1218            unsigned long end)
1219 {
1220     struct mm_struct *dst_mm = dst_vma->vm_mm;
1221     p4d_t *src_p4d, *dst_p4d;
1222     unsigned long next;
1223 
1224     dst_p4d = p4d_alloc(dst_mm, dst_pgd, addr);
1225     if (!dst_p4d)
1226         return -ENOMEM;
1227     src_p4d = p4d_offset(src_pgd, addr);
1228     do {
1229         next = p4d_addr_end(addr, end);
1230         if (p4d_none_or_clear_bad(src_p4d))
1231             continue;
1232         if (copy_pud_range(dst_vma, src_vma, dst_p4d, src_p4d,
1233                    addr, next))
1234             return -ENOMEM;
1235     } while (dst_p4d++, src_p4d++, addr = next, addr != end);
1236     return 0;
1237 }
1238 
1239 /*
1240  * Return true if the vma needs to copy the pgtable during this fork().  Return
1241  * false when we can speed up fork() by allowing lazy page faults later until
1242  * when the child accesses the memory range.
1243  */
1244 static bool
1245 vma_needs_copy(struct vm_area_struct *dst_vma, struct vm_area_struct *src_vma)
1246 {
1247     /*
1248      * Always copy pgtables when dst_vma has uffd-wp enabled even if it's
1249      * file-backed (e.g. shmem). Because when uffd-wp is enabled, pgtable
1250      * contains uffd-wp protection information, that's something we can't
1251      * retrieve from page cache, and skip copying will lose those info.
1252      */
1253     if (userfaultfd_wp(dst_vma))
1254         return true;
1255 
1256     if (src_vma->vm_flags & (VM_PFNMAP | VM_MIXEDMAP))
1257         return true;
1258 
1259     if (src_vma->anon_vma)
1260         return true;
1261 
1262     /*
1263      * Don't copy ptes where a page fault will fill them correctly.  Fork
1264      * becomes much lighter when there are big shared or private readonly
1265      * mappings. The tradeoff is that copy_page_range is more efficient
1266      * than faulting.
1267      */
1268     return false;
1269 }
1270 
1271 int
1272 copy_page_range(struct vm_area_struct *dst_vma, struct vm_area_struct *src_vma)
1273 {
1274     pgd_t *src_pgd, *dst_pgd;
1275     unsigned long next;
1276     unsigned long addr = src_vma->vm_start;
1277     unsigned long end = src_vma->vm_end;
1278     struct mm_struct *dst_mm = dst_vma->vm_mm;
1279     struct mm_struct *src_mm = src_vma->vm_mm;
1280     struct mmu_notifier_range range;
1281     bool is_cow;
1282     int ret;
1283 
1284     if (!vma_needs_copy(dst_vma, src_vma))
1285         return 0;
1286 
1287     if (is_vm_hugetlb_page(src_vma))
1288         return copy_hugetlb_page_range(dst_mm, src_mm, dst_vma, src_vma);
1289 
1290     if (unlikely(src_vma->vm_flags & VM_PFNMAP)) {
1291         /*
1292          * We do not free on error cases below as remove_vma
1293          * gets called on error from higher level routine
1294          */
1295         ret = track_pfn_copy(src_vma);
1296         if (ret)
1297             return ret;
1298     }
1299 
1300     /*
1301      * We need to invalidate the secondary MMU mappings only when
1302      * there could be a permission downgrade on the ptes of the
1303      * parent mm. And a permission downgrade will only happen if
1304      * is_cow_mapping() returns true.
1305      */
1306     is_cow = is_cow_mapping(src_vma->vm_flags);
1307 
1308     if (is_cow) {
1309         mmu_notifier_range_init(&range, MMU_NOTIFY_PROTECTION_PAGE,
1310                     0, src_vma, src_mm, addr, end);
1311         mmu_notifier_invalidate_range_start(&range);
1312         /*
1313          * Disabling preemption is not needed for the write side, as
1314          * the read side doesn't spin, but goes to the mmap_lock.
1315          *
1316          * Use the raw variant of the seqcount_t write API to avoid
1317          * lockdep complaining about preemptibility.
1318          */
1319         mmap_assert_write_locked(src_mm);
1320         raw_write_seqcount_begin(&src_mm->write_protect_seq);
1321     }
1322 
1323     ret = 0;
1324     dst_pgd = pgd_offset(dst_mm, addr);
1325     src_pgd = pgd_offset(src_mm, addr);
1326     do {
1327         next = pgd_addr_end(addr, end);
1328         if (pgd_none_or_clear_bad(src_pgd))
1329             continue;
1330         if (unlikely(copy_p4d_range(dst_vma, src_vma, dst_pgd, src_pgd,
1331                         addr, next))) {
1332             ret = -ENOMEM;
1333             break;
1334         }
1335     } while (dst_pgd++, src_pgd++, addr = next, addr != end);
1336 
1337     if (is_cow) {
1338         raw_write_seqcount_end(&src_mm->write_protect_seq);
1339         mmu_notifier_invalidate_range_end(&range);
1340     }
1341     return ret;
1342 }
1343 
1344 /*
1345  * Parameter block passed down to zap_pte_range in exceptional cases.
1346  */
1347 struct zap_details {
1348     struct folio *single_folio; /* Locked folio to be unmapped */
1349     bool even_cows;         /* Zap COWed private pages too? */
1350     zap_flags_t zap_flags;      /* Extra flags for zapping */
1351 };
1352 
1353 /* Whether we should zap all COWed (private) pages too */
1354 static inline bool should_zap_cows(struct zap_details *details)
1355 {
1356     /* By default, zap all pages */
1357     if (!details)
1358         return true;
1359 
1360     /* Or, we zap COWed pages only if the caller wants to */
1361     return details->even_cows;
1362 }
1363 
1364 /* Decides whether we should zap this page with the page pointer specified */
1365 static inline bool should_zap_page(struct zap_details *details, struct page *page)
1366 {
1367     /* If we can make a decision without *page.. */
1368     if (should_zap_cows(details))
1369         return true;
1370 
1371     /* E.g. the caller passes NULL for the case of a zero page */
1372     if (!page)
1373         return true;
1374 
1375     /* Otherwise we should only zap non-anon pages */
1376     return !PageAnon(page);
1377 }
1378 
1379 static inline bool zap_drop_file_uffd_wp(struct zap_details *details)
1380 {
1381     if (!details)
1382         return false;
1383 
1384     return details->zap_flags & ZAP_FLAG_DROP_MARKER;
1385 }
1386 
1387 /*
1388  * This function makes sure that we'll replace the none pte with an uffd-wp
1389  * swap special pte marker when necessary. Must be with the pgtable lock held.
1390  */
1391 static inline void
1392 zap_install_uffd_wp_if_needed(struct vm_area_struct *vma,
1393                   unsigned long addr, pte_t *pte,
1394                   struct zap_details *details, pte_t pteval)
1395 {
1396     if (zap_drop_file_uffd_wp(details))
1397         return;
1398 
1399     pte_install_uffd_wp_if_needed(vma, addr, pte, pteval);
1400 }
1401 
1402 static unsigned long zap_pte_range(struct mmu_gather *tlb,
1403                 struct vm_area_struct *vma, pmd_t *pmd,
1404                 unsigned long addr, unsigned long end,
1405                 struct zap_details *details)
1406 {
1407     struct mm_struct *mm = tlb->mm;
1408     int force_flush = 0;
1409     int rss[NR_MM_COUNTERS];
1410     spinlock_t *ptl;
1411     pte_t *start_pte;
1412     pte_t *pte;
1413     swp_entry_t entry;
1414 
1415     tlb_change_page_size(tlb, PAGE_SIZE);
1416 again:
1417     init_rss_vec(rss);
1418     start_pte = pte_offset_map_lock(mm, pmd, addr, &ptl);
1419     pte = start_pte;
1420     flush_tlb_batched_pending(mm);
1421     arch_enter_lazy_mmu_mode();
1422     do {
1423         pte_t ptent = *pte;
1424         struct page *page;
1425 
1426         if (pte_none(ptent))
1427             continue;
1428 
1429         if (need_resched())
1430             break;
1431 
1432         if (pte_present(ptent)) {
1433             page = vm_normal_page(vma, addr, ptent);
1434             if (unlikely(!should_zap_page(details, page)))
1435                 continue;
1436             ptent = ptep_get_and_clear_full(mm, addr, pte,
1437                             tlb->fullmm);
1438             tlb_remove_tlb_entry(tlb, pte, addr);
1439             zap_install_uffd_wp_if_needed(vma, addr, pte, details,
1440                               ptent);
1441             if (unlikely(!page))
1442                 continue;
1443 
1444             if (!PageAnon(page)) {
1445                 if (pte_dirty(ptent)) {
1446                     force_flush = 1;
1447                     set_page_dirty(page);
1448                 }
1449                 if (pte_young(ptent) &&
1450                     likely(!(vma->vm_flags & VM_SEQ_READ)))
1451                     mark_page_accessed(page);
1452             }
1453             rss[mm_counter(page)]--;
1454             page_remove_rmap(page, vma, false);
1455             if (unlikely(page_mapcount(page) < 0))
1456                 print_bad_pte(vma, addr, ptent, page);
1457             if (unlikely(__tlb_remove_page(tlb, page))) {
1458                 force_flush = 1;
1459                 addr += PAGE_SIZE;
1460                 break;
1461             }
1462             continue;
1463         }
1464 
1465         entry = pte_to_swp_entry(ptent);
1466         if (is_device_private_entry(entry) ||
1467             is_device_exclusive_entry(entry)) {
1468             page = pfn_swap_entry_to_page(entry);
1469             if (unlikely(!should_zap_page(details, page)))
1470                 continue;
1471             /*
1472              * Both device private/exclusive mappings should only
1473              * work with anonymous page so far, so we don't need to
1474              * consider uffd-wp bit when zap. For more information,
1475              * see zap_install_uffd_wp_if_needed().
1476              */
1477             WARN_ON_ONCE(!vma_is_anonymous(vma));
1478             rss[mm_counter(page)]--;
1479             if (is_device_private_entry(entry))
1480                 page_remove_rmap(page, vma, false);
1481             put_page(page);
1482         } else if (!non_swap_entry(entry)) {
1483             /* Genuine swap entry, hence a private anon page */
1484             if (!should_zap_cows(details))
1485                 continue;
1486             rss[MM_SWAPENTS]--;
1487             if (unlikely(!free_swap_and_cache(entry)))
1488                 print_bad_pte(vma, addr, ptent, NULL);
1489         } else if (is_migration_entry(entry)) {
1490             page = pfn_swap_entry_to_page(entry);
1491             if (!should_zap_page(details, page))
1492                 continue;
1493             rss[mm_counter(page)]--;
1494         } else if (pte_marker_entry_uffd_wp(entry)) {
1495             /* Only drop the uffd-wp marker if explicitly requested */
1496             if (!zap_drop_file_uffd_wp(details))
1497                 continue;
1498         } else if (is_hwpoison_entry(entry) ||
1499                is_swapin_error_entry(entry)) {
1500             if (!should_zap_cows(details))
1501                 continue;
1502         } else {
1503             /* We should have covered all the swap entry types */
1504             WARN_ON_ONCE(1);
1505         }
1506         pte_clear_not_present_full(mm, addr, pte, tlb->fullmm);
1507         zap_install_uffd_wp_if_needed(vma, addr, pte, details, ptent);
1508     } while (pte++, addr += PAGE_SIZE, addr != end);
1509 
1510     add_mm_rss_vec(mm, rss);
1511     arch_leave_lazy_mmu_mode();
1512 
1513     /* Do the actual TLB flush before dropping ptl */
1514     if (force_flush)
1515         tlb_flush_mmu_tlbonly(tlb);
1516     pte_unmap_unlock(start_pte, ptl);
1517 
1518     /*
1519      * If we forced a TLB flush (either due to running out of
1520      * batch buffers or because we needed to flush dirty TLB
1521      * entries before releasing the ptl), free the batched
1522      * memory too. Restart if we didn't do everything.
1523      */
1524     if (force_flush) {
1525         force_flush = 0;
1526         tlb_flush_mmu(tlb);
1527     }
1528 
1529     if (addr != end) {
1530         cond_resched();
1531         goto again;
1532     }
1533 
1534     return addr;
1535 }
1536 
1537 static inline unsigned long zap_pmd_range(struct mmu_gather *tlb,
1538                 struct vm_area_struct *vma, pud_t *pud,
1539                 unsigned long addr, unsigned long end,
1540                 struct zap_details *details)
1541 {
1542     pmd_t *pmd;
1543     unsigned long next;
1544 
1545     pmd = pmd_offset(pud, addr);
1546     do {
1547         next = pmd_addr_end(addr, end);
1548         if (is_swap_pmd(*pmd) || pmd_trans_huge(*pmd) || pmd_devmap(*pmd)) {
1549             if (next - addr != HPAGE_PMD_SIZE)
1550                 __split_huge_pmd(vma, pmd, addr, false, NULL);
1551             else if (zap_huge_pmd(tlb, vma, pmd, addr))
1552                 goto next;
1553             /* fall through */
1554         } else if (details && details->single_folio &&
1555                folio_test_pmd_mappable(details->single_folio) &&
1556                next - addr == HPAGE_PMD_SIZE && pmd_none(*pmd)) {
1557             spinlock_t *ptl = pmd_lock(tlb->mm, pmd);
1558             /*
1559              * Take and drop THP pmd lock so that we cannot return
1560              * prematurely, while zap_huge_pmd() has cleared *pmd,
1561              * but not yet decremented compound_mapcount().
1562              */
1563             spin_unlock(ptl);
1564         }
1565 
1566         /*
1567          * Here there can be other concurrent MADV_DONTNEED or
1568          * trans huge page faults running, and if the pmd is
1569          * none or trans huge it can change under us. This is
1570          * because MADV_DONTNEED holds the mmap_lock in read
1571          * mode.
1572          */
1573         if (pmd_none_or_trans_huge_or_clear_bad(pmd))
1574             goto next;
1575         next = zap_pte_range(tlb, vma, pmd, addr, next, details);
1576 next:
1577         cond_resched();
1578     } while (pmd++, addr = next, addr != end);
1579 
1580     return addr;
1581 }
1582 
1583 static inline unsigned long zap_pud_range(struct mmu_gather *tlb,
1584                 struct vm_area_struct *vma, p4d_t *p4d,
1585                 unsigned long addr, unsigned long end,
1586                 struct zap_details *details)
1587 {
1588     pud_t *pud;
1589     unsigned long next;
1590 
1591     pud = pud_offset(p4d, addr);
1592     do {
1593         next = pud_addr_end(addr, end);
1594         if (pud_trans_huge(*pud) || pud_devmap(*pud)) {
1595             if (next - addr != HPAGE_PUD_SIZE) {
1596                 mmap_assert_locked(tlb->mm);
1597                 split_huge_pud(vma, pud, addr);
1598             } else if (zap_huge_pud(tlb, vma, pud, addr))
1599                 goto next;
1600             /* fall through */
1601         }
1602         if (pud_none_or_clear_bad(pud))
1603             continue;
1604         next = zap_pmd_range(tlb, vma, pud, addr, next, details);
1605 next:
1606         cond_resched();
1607     } while (pud++, addr = next, addr != end);
1608 
1609     return addr;
1610 }
1611 
1612 static inline unsigned long zap_p4d_range(struct mmu_gather *tlb,
1613                 struct vm_area_struct *vma, pgd_t *pgd,
1614                 unsigned long addr, unsigned long end,
1615                 struct zap_details *details)
1616 {
1617     p4d_t *p4d;
1618     unsigned long next;
1619 
1620     p4d = p4d_offset(pgd, addr);
1621     do {
1622         next = p4d_addr_end(addr, end);
1623         if (p4d_none_or_clear_bad(p4d))
1624             continue;
1625         next = zap_pud_range(tlb, vma, p4d, addr, next, details);
1626     } while (p4d++, addr = next, addr != end);
1627 
1628     return addr;
1629 }
1630 
1631 void unmap_page_range(struct mmu_gather *tlb,
1632                  struct vm_area_struct *vma,
1633                  unsigned long addr, unsigned long end,
1634                  struct zap_details *details)
1635 {
1636     pgd_t *pgd;
1637     unsigned long next;
1638 
1639     BUG_ON(addr >= end);
1640     tlb_start_vma(tlb, vma);
1641     pgd = pgd_offset(vma->vm_mm, addr);
1642     do {
1643         next = pgd_addr_end(addr, end);
1644         if (pgd_none_or_clear_bad(pgd))
1645             continue;
1646         next = zap_p4d_range(tlb, vma, pgd, addr, next, details);
1647     } while (pgd++, addr = next, addr != end);
1648     tlb_end_vma(tlb, vma);
1649 }
1650 
1651 
1652 static void unmap_single_vma(struct mmu_gather *tlb,
1653         struct vm_area_struct *vma, unsigned long start_addr,
1654         unsigned long end_addr,
1655         struct zap_details *details)
1656 {
1657     unsigned long start = max(vma->vm_start, start_addr);
1658     unsigned long end;
1659 
1660     if (start >= vma->vm_end)
1661         return;
1662     end = min(vma->vm_end, end_addr);
1663     if (end <= vma->vm_start)
1664         return;
1665 
1666     if (vma->vm_file)
1667         uprobe_munmap(vma, start, end);
1668 
1669     if (unlikely(vma->vm_flags & VM_PFNMAP))
1670         untrack_pfn(vma, 0, 0);
1671 
1672     if (start != end) {
1673         if (unlikely(is_vm_hugetlb_page(vma))) {
1674             /*
1675              * It is undesirable to test vma->vm_file as it
1676              * should be non-null for valid hugetlb area.
1677              * However, vm_file will be NULL in the error
1678              * cleanup path of mmap_region. When
1679              * hugetlbfs ->mmap method fails,
1680              * mmap_region() nullifies vma->vm_file
1681              * before calling this function to clean up.
1682              * Since no pte has actually been setup, it is
1683              * safe to do nothing in this case.
1684              */
1685             if (vma->vm_file) {
1686                 zap_flags_t zap_flags = details ?
1687                     details->zap_flags : 0;
1688                 i_mmap_lock_write(vma->vm_file->f_mapping);
1689                 __unmap_hugepage_range_final(tlb, vma, start, end,
1690                                  NULL, zap_flags);
1691                 i_mmap_unlock_write(vma->vm_file->f_mapping);
1692             }
1693         } else
1694             unmap_page_range(tlb, vma, start, end, details);
1695     }
1696 }
1697 
1698 /**
1699  * unmap_vmas - unmap a range of memory covered by a list of vma's
1700  * @tlb: address of the caller's struct mmu_gather
1701  * @vma: the starting vma
1702  * @start_addr: virtual address at which to start unmapping
1703  * @end_addr: virtual address at which to end unmapping
1704  *
1705  * Unmap all pages in the vma list.
1706  *
1707  * Only addresses between `start' and `end' will be unmapped.
1708  *
1709  * The VMA list must be sorted in ascending virtual address order.
1710  *
1711  * unmap_vmas() assumes that the caller will flush the whole unmapped address
1712  * range after unmap_vmas() returns.  So the only responsibility here is to
1713  * ensure that any thus-far unmapped pages are flushed before unmap_vmas()
1714  * drops the lock and schedules.
1715  */
1716 void unmap_vmas(struct mmu_gather *tlb,
1717         struct vm_area_struct *vma, unsigned long start_addr,
1718         unsigned long end_addr)
1719 {
1720     struct mmu_notifier_range range;
1721     struct zap_details details = {
1722         .zap_flags = ZAP_FLAG_DROP_MARKER,
1723         /* Careful - we need to zap private pages too! */
1724         .even_cows = true,
1725     };
1726 
1727     mmu_notifier_range_init(&range, MMU_NOTIFY_UNMAP, 0, vma, vma->vm_mm,
1728                 start_addr, end_addr);
1729     mmu_notifier_invalidate_range_start(&range);
1730     for ( ; vma && vma->vm_start < end_addr; vma = vma->vm_next)
1731         unmap_single_vma(tlb, vma, start_addr, end_addr, &details);
1732     mmu_notifier_invalidate_range_end(&range);
1733 }
1734 
1735 /**
1736  * zap_page_range - remove user pages in a given range
1737  * @vma: vm_area_struct holding the applicable pages
1738  * @start: starting address of pages to zap
1739  * @size: number of bytes to zap
1740  *
1741  * Caller must protect the VMA list
1742  */
1743 void zap_page_range(struct vm_area_struct *vma, unsigned long start,
1744         unsigned long size)
1745 {
1746     struct mmu_notifier_range range;
1747     struct mmu_gather tlb;
1748 
1749     lru_add_drain();
1750     mmu_notifier_range_init(&range, MMU_NOTIFY_CLEAR, 0, vma, vma->vm_mm,
1751                 start, start + size);
1752     tlb_gather_mmu(&tlb, vma->vm_mm);
1753     update_hiwater_rss(vma->vm_mm);
1754     mmu_notifier_invalidate_range_start(&range);
1755     for ( ; vma && vma->vm_start < range.end; vma = vma->vm_next)
1756         unmap_single_vma(&tlb, vma, start, range.end, NULL);
1757     mmu_notifier_invalidate_range_end(&range);
1758     tlb_finish_mmu(&tlb);
1759 }
1760 
1761 /**
1762  * zap_page_range_single - remove user pages in a given range
1763  * @vma: vm_area_struct holding the applicable pages
1764  * @address: starting address of pages to zap
1765  * @size: number of bytes to zap
1766  * @details: details of shared cache invalidation
1767  *
1768  * The range must fit into one VMA.
1769  */
1770 static void zap_page_range_single(struct vm_area_struct *vma, unsigned long address,
1771         unsigned long size, struct zap_details *details)
1772 {
1773     struct mmu_notifier_range range;
1774     struct mmu_gather tlb;
1775 
1776     lru_add_drain();
1777     mmu_notifier_range_init(&range, MMU_NOTIFY_CLEAR, 0, vma, vma->vm_mm,
1778                 address, address + size);
1779     tlb_gather_mmu(&tlb, vma->vm_mm);
1780     update_hiwater_rss(vma->vm_mm);
1781     mmu_notifier_invalidate_range_start(&range);
1782     unmap_single_vma(&tlb, vma, address, range.end, details);
1783     mmu_notifier_invalidate_range_end(&range);
1784     tlb_finish_mmu(&tlb);
1785 }
1786 
1787 /**
1788  * zap_vma_ptes - remove ptes mapping the vma
1789  * @vma: vm_area_struct holding ptes to be zapped
1790  * @address: starting address of pages to zap
1791  * @size: number of bytes to zap
1792  *
1793  * This function only unmaps ptes assigned to VM_PFNMAP vmas.
1794  *
1795  * The entire address range must be fully contained within the vma.
1796  *
1797  */
1798 void zap_vma_ptes(struct vm_area_struct *vma, unsigned long address,
1799         unsigned long size)
1800 {
1801     if (!range_in_vma(vma, address, address + size) ||
1802                 !(vma->vm_flags & VM_PFNMAP))
1803         return;
1804 
1805     zap_page_range_single(vma, address, size, NULL);
1806 }
1807 EXPORT_SYMBOL_GPL(zap_vma_ptes);
1808 
1809 static pmd_t *walk_to_pmd(struct mm_struct *mm, unsigned long addr)
1810 {
1811     pgd_t *pgd;
1812     p4d_t *p4d;
1813     pud_t *pud;
1814     pmd_t *pmd;
1815 
1816     pgd = pgd_offset(mm, addr);
1817     p4d = p4d_alloc(mm, pgd, addr);
1818     if (!p4d)
1819         return NULL;
1820     pud = pud_alloc(mm, p4d, addr);
1821     if (!pud)
1822         return NULL;
1823     pmd = pmd_alloc(mm, pud, addr);
1824     if (!pmd)
1825         return NULL;
1826 
1827     VM_BUG_ON(pmd_trans_huge(*pmd));
1828     return pmd;
1829 }
1830 
1831 pte_t *__get_locked_pte(struct mm_struct *mm, unsigned long addr,
1832             spinlock_t **ptl)
1833 {
1834     pmd_t *pmd = walk_to_pmd(mm, addr);
1835 
1836     if (!pmd)
1837         return NULL;
1838     return pte_alloc_map_lock(mm, pmd, addr, ptl);
1839 }
1840 
1841 static int validate_page_before_insert(struct page *page)
1842 {
1843     if (PageAnon(page) || PageSlab(page) || page_has_type(page))
1844         return -EINVAL;
1845     flush_dcache_page(page);
1846     return 0;
1847 }
1848 
1849 static int insert_page_into_pte_locked(struct vm_area_struct *vma, pte_t *pte,
1850             unsigned long addr, struct page *page, pgprot_t prot)
1851 {
1852     if (!pte_none(*pte))
1853         return -EBUSY;
1854     /* Ok, finally just insert the thing.. */
1855     get_page(page);
1856     inc_mm_counter_fast(vma->vm_mm, mm_counter_file(page));
1857     page_add_file_rmap(page, vma, false);
1858     set_pte_at(vma->vm_mm, addr, pte, mk_pte(page, prot));
1859     return 0;
1860 }
1861 
1862 /*
1863  * This is the old fallback for page remapping.
1864  *
1865  * For historical reasons, it only allows reserved pages. Only
1866  * old drivers should use this, and they needed to mark their
1867  * pages reserved for the old functions anyway.
1868  */
1869 static int insert_page(struct vm_area_struct *vma, unsigned long addr,
1870             struct page *page, pgprot_t prot)
1871 {
1872     int retval;
1873     pte_t *pte;
1874     spinlock_t *ptl;
1875 
1876     retval = validate_page_before_insert(page);
1877     if (retval)
1878         goto out;
1879     retval = -ENOMEM;
1880     pte = get_locked_pte(vma->vm_mm, addr, &ptl);
1881     if (!pte)
1882         goto out;
1883     retval = insert_page_into_pte_locked(vma, pte, addr, page, prot);
1884     pte_unmap_unlock(pte, ptl);
1885 out:
1886     return retval;
1887 }
1888 
1889 #ifdef pte_index
1890 static int insert_page_in_batch_locked(struct vm_area_struct *vma, pte_t *pte,
1891             unsigned long addr, struct page *page, pgprot_t prot)
1892 {
1893     int err;
1894 
1895     if (!page_count(page))
1896         return -EINVAL;
1897     err = validate_page_before_insert(page);
1898     if (err)
1899         return err;
1900     return insert_page_into_pte_locked(vma, pte, addr, page, prot);
1901 }
1902 
1903 /* insert_pages() amortizes the cost of spinlock operations
1904  * when inserting pages in a loop. Arch *must* define pte_index.
1905  */
1906 static int insert_pages(struct vm_area_struct *vma, unsigned long addr,
1907             struct page **pages, unsigned long *num, pgprot_t prot)
1908 {
1909     pmd_t *pmd = NULL;
1910     pte_t *start_pte, *pte;
1911     spinlock_t *pte_lock;
1912     struct mm_struct *const mm = vma->vm_mm;
1913     unsigned long curr_page_idx = 0;
1914     unsigned long remaining_pages_total = *num;
1915     unsigned long pages_to_write_in_pmd;
1916     int ret;
1917 more:
1918     ret = -EFAULT;
1919     pmd = walk_to_pmd(mm, addr);
1920     if (!pmd)
1921         goto out;
1922 
1923     pages_to_write_in_pmd = min_t(unsigned long,
1924         remaining_pages_total, PTRS_PER_PTE - pte_index(addr));
1925 
1926     /* Allocate the PTE if necessary; takes PMD lock once only. */
1927     ret = -ENOMEM;
1928     if (pte_alloc(mm, pmd))
1929         goto out;
1930 
1931     while (pages_to_write_in_pmd) {
1932         int pte_idx = 0;
1933         const int batch_size = min_t(int, pages_to_write_in_pmd, 8);
1934 
1935         start_pte = pte_offset_map_lock(mm, pmd, addr, &pte_lock);
1936         for (pte = start_pte; pte_idx < batch_size; ++pte, ++pte_idx) {
1937             int err = insert_page_in_batch_locked(vma, pte,
1938                 addr, pages[curr_page_idx], prot);
1939             if (unlikely(err)) {
1940                 pte_unmap_unlock(start_pte, pte_lock);
1941                 ret = err;
1942                 remaining_pages_total -= pte_idx;
1943                 goto out;
1944             }
1945             addr += PAGE_SIZE;
1946             ++curr_page_idx;
1947         }
1948         pte_unmap_unlock(start_pte, pte_lock);
1949         pages_to_write_in_pmd -= batch_size;
1950         remaining_pages_total -= batch_size;
1951     }
1952     if (remaining_pages_total)
1953         goto more;
1954     ret = 0;
1955 out:
1956     *num = remaining_pages_total;
1957     return ret;
1958 }
1959 #endif  /* ifdef pte_index */
1960 
1961 /**
1962  * vm_insert_pages - insert multiple pages into user vma, batching the pmd lock.
1963  * @vma: user vma to map to
1964  * @addr: target start user address of these pages
1965  * @pages: source kernel pages
1966  * @num: in: number of pages to map. out: number of pages that were *not*
1967  * mapped. (0 means all pages were successfully mapped).
1968  *
1969  * Preferred over vm_insert_page() when inserting multiple pages.
1970  *
1971  * In case of error, we may have mapped a subset of the provided
1972  * pages. It is the caller's responsibility to account for this case.
1973  *
1974  * The same restrictions apply as in vm_insert_page().
1975  */
1976 int vm_insert_pages(struct vm_area_struct *vma, unsigned long addr,
1977             struct page **pages, unsigned long *num)
1978 {
1979 #ifdef pte_index
1980     const unsigned long end_addr = addr + (*num * PAGE_SIZE) - 1;
1981 
1982     if (addr < vma->vm_start || end_addr >= vma->vm_end)
1983         return -EFAULT;
1984     if (!(vma->vm_flags & VM_MIXEDMAP)) {
1985         BUG_ON(mmap_read_trylock(vma->vm_mm));
1986         BUG_ON(vma->vm_flags & VM_PFNMAP);
1987         vma->vm_flags |= VM_MIXEDMAP;
1988     }
1989     /* Defer page refcount checking till we're about to map that page. */
1990     return insert_pages(vma, addr, pages, num, vma->vm_page_prot);
1991 #else
1992     unsigned long idx = 0, pgcount = *num;
1993     int err = -EINVAL;
1994 
1995     for (; idx < pgcount; ++idx) {
1996         err = vm_insert_page(vma, addr + (PAGE_SIZE * idx), pages[idx]);
1997         if (err)
1998             break;
1999     }
2000     *num = pgcount - idx;
2001     return err;
2002 #endif  /* ifdef pte_index */
2003 }
2004 EXPORT_SYMBOL(vm_insert_pages);
2005 
2006 /**
2007  * vm_insert_page - insert single page into user vma
2008  * @vma: user vma to map to
2009  * @addr: target user address of this page
2010  * @page: source kernel page
2011  *
2012  * This allows drivers to insert individual pages they've allocated
2013  * into a user vma.
2014  *
2015  * The page has to be a nice clean _individual_ kernel allocation.
2016  * If you allocate a compound page, you need to have marked it as
2017  * such (__GFP_COMP), or manually just split the page up yourself
2018  * (see split_page()).
2019  *
2020  * NOTE! Traditionally this was done with "remap_pfn_range()" which
2021  * took an arbitrary page protection parameter. This doesn't allow
2022  * that. Your vma protection will have to be set up correctly, which
2023  * means that if you want a shared writable mapping, you'd better
2024  * ask for a shared writable mapping!
2025  *
2026  * The page does not need to be reserved.
2027  *
2028  * Usually this function is called from f_op->mmap() handler
2029  * under mm->mmap_lock write-lock, so it can change vma->vm_flags.
2030  * Caller must set VM_MIXEDMAP on vma if it wants to call this
2031  * function from other places, for example from page-fault handler.
2032  *
2033  * Return: %0 on success, negative error code otherwise.
2034  */
2035 int vm_insert_page(struct vm_area_struct *vma, unsigned long addr,
2036             struct page *page)
2037 {
2038     if (addr < vma->vm_start || addr >= vma->vm_end)
2039         return -EFAULT;
2040     if (!page_count(page))
2041         return -EINVAL;
2042     if (!(vma->vm_flags & VM_MIXEDMAP)) {
2043         BUG_ON(mmap_read_trylock(vma->vm_mm));
2044         BUG_ON(vma->vm_flags & VM_PFNMAP);
2045         vma->vm_flags |= VM_MIXEDMAP;
2046     }
2047     return insert_page(vma, addr, page, vma->vm_page_prot);
2048 }
2049 EXPORT_SYMBOL(vm_insert_page);
2050 
2051 /*
2052  * __vm_map_pages - maps range of kernel pages into user vma
2053  * @vma: user vma to map to
2054  * @pages: pointer to array of source kernel pages
2055  * @num: number of pages in page array
2056  * @offset: user's requested vm_pgoff
2057  *
2058  * This allows drivers to map range of kernel pages into a user vma.
2059  *
2060  * Return: 0 on success and error code otherwise.
2061  */
2062 static int __vm_map_pages(struct vm_area_struct *vma, struct page **pages,
2063                 unsigned long num, unsigned long offset)
2064 {
2065     unsigned long count = vma_pages(vma);
2066     unsigned long uaddr = vma->vm_start;
2067     int ret, i;
2068 
2069     /* Fail if the user requested offset is beyond the end of the object */
2070     if (offset >= num)
2071         return -ENXIO;
2072 
2073     /* Fail if the user requested size exceeds available object size */
2074     if (count > num - offset)
2075         return -ENXIO;
2076 
2077     for (i = 0; i < count; i++) {
2078         ret = vm_insert_page(vma, uaddr, pages[offset + i]);
2079         if (ret < 0)
2080             return ret;
2081         uaddr += PAGE_SIZE;
2082     }
2083 
2084     return 0;
2085 }
2086 
2087 /**
2088  * vm_map_pages - maps range of kernel pages starts with non zero offset
2089  * @vma: user vma to map to
2090  * @pages: pointer to array of source kernel pages
2091  * @num: number of pages in page array
2092  *
2093  * Maps an object consisting of @num pages, catering for the user's
2094  * requested vm_pgoff
2095  *
2096  * If we fail to insert any page into the vma, the function will return
2097  * immediately leaving any previously inserted pages present.  Callers
2098  * from the mmap handler may immediately return the error as their caller
2099  * will destroy the vma, removing any successfully inserted pages. Other
2100  * callers should make their own arrangements for calling unmap_region().
2101  *
2102  * Context: Process context. Called by mmap handlers.
2103  * Return: 0 on success and error code otherwise.
2104  */
2105 int vm_map_pages(struct vm_area_struct *vma, struct page **pages,
2106                 unsigned long num)
2107 {
2108     return __vm_map_pages(vma, pages, num, vma->vm_pgoff);
2109 }
2110 EXPORT_SYMBOL(vm_map_pages);
2111 
2112 /**
2113  * vm_map_pages_zero - map range of kernel pages starts with zero offset
2114  * @vma: user vma to map to
2115  * @pages: pointer to array of source kernel pages
2116  * @num: number of pages in page array
2117  *
2118  * Similar to vm_map_pages(), except that it explicitly sets the offset
2119  * to 0. This function is intended for the drivers that did not consider
2120  * vm_pgoff.
2121  *
2122  * Context: Process context. Called by mmap handlers.
2123  * Return: 0 on success and error code otherwise.
2124  */
2125 int vm_map_pages_zero(struct vm_area_struct *vma, struct page **pages,
2126                 unsigned long num)
2127 {
2128     return __vm_map_pages(vma, pages, num, 0);
2129 }
2130 EXPORT_SYMBOL(vm_map_pages_zero);
2131 
2132 static vm_fault_t insert_pfn(struct vm_area_struct *vma, unsigned long addr,
2133             pfn_t pfn, pgprot_t prot, bool mkwrite)
2134 {
2135     struct mm_struct *mm = vma->vm_mm;
2136     pte_t *pte, entry;
2137     spinlock_t *ptl;
2138 
2139     pte = get_locked_pte(mm, addr, &ptl);
2140     if (!pte)
2141         return VM_FAULT_OOM;
2142     if (!pte_none(*pte)) {
2143         if (mkwrite) {
2144             /*
2145              * For read faults on private mappings the PFN passed
2146              * in may not match the PFN we have mapped if the
2147              * mapped PFN is a writeable COW page.  In the mkwrite
2148              * case we are creating a writable PTE for a shared
2149              * mapping and we expect the PFNs to match. If they
2150              * don't match, we are likely racing with block
2151              * allocation and mapping invalidation so just skip the
2152              * update.
2153              */
2154             if (pte_pfn(*pte) != pfn_t_to_pfn(pfn)) {
2155                 WARN_ON_ONCE(!is_zero_pfn(pte_pfn(*pte)));
2156                 goto out_unlock;
2157             }
2158             entry = pte_mkyoung(*pte);
2159             entry = maybe_mkwrite(pte_mkdirty(entry), vma);
2160             if (ptep_set_access_flags(vma, addr, pte, entry, 1))
2161                 update_mmu_cache(vma, addr, pte);
2162         }
2163         goto out_unlock;
2164     }
2165 
2166     /* Ok, finally just insert the thing.. */
2167     if (pfn_t_devmap(pfn))
2168         entry = pte_mkdevmap(pfn_t_pte(pfn, prot));
2169     else
2170         entry = pte_mkspecial(pfn_t_pte(pfn, prot));
2171 
2172     if (mkwrite) {
2173         entry = pte_mkyoung(entry);
2174         entry = maybe_mkwrite(pte_mkdirty(entry), vma);
2175     }
2176 
2177     set_pte_at(mm, addr, pte, entry);
2178     update_mmu_cache(vma, addr, pte); /* XXX: why not for insert_page? */
2179 
2180 out_unlock:
2181     pte_unmap_unlock(pte, ptl);
2182     return VM_FAULT_NOPAGE;
2183 }
2184 
2185 /**
2186  * vmf_insert_pfn_prot - insert single pfn into user vma with specified pgprot
2187  * @vma: user vma to map to
2188  * @addr: target user address of this page
2189  * @pfn: source kernel pfn
2190  * @pgprot: pgprot flags for the inserted page
2191  *
2192  * This is exactly like vmf_insert_pfn(), except that it allows drivers
2193  * to override pgprot on a per-page basis.
2194  *
2195  * This only makes sense for IO mappings, and it makes no sense for
2196  * COW mappings.  In general, using multiple vmas is preferable;
2197  * vmf_insert_pfn_prot should only be used if using multiple VMAs is
2198  * impractical.
2199  *
2200  * See vmf_insert_mixed_prot() for a discussion of the implication of using
2201  * a value of @pgprot different from that of @vma->vm_page_prot.
2202  *
2203  * Context: Process context.  May allocate using %GFP_KERNEL.
2204  * Return: vm_fault_t value.
2205  */
2206 vm_fault_t vmf_insert_pfn_prot(struct vm_area_struct *vma, unsigned long addr,
2207             unsigned long pfn, pgprot_t pgprot)
2208 {
2209     /*
2210      * Technically, architectures with pte_special can avoid all these
2211      * restrictions (same for remap_pfn_range).  However we would like
2212      * consistency in testing and feature parity among all, so we should
2213      * try to keep these invariants in place for everybody.
2214      */
2215     BUG_ON(!(vma->vm_flags & (VM_PFNMAP|VM_MIXEDMAP)));
2216     BUG_ON((vma->vm_flags & (VM_PFNMAP|VM_MIXEDMAP)) ==
2217                         (VM_PFNMAP|VM_MIXEDMAP));
2218     BUG_ON((vma->vm_flags & VM_PFNMAP) && is_cow_mapping(vma->vm_flags));
2219     BUG_ON((vma->vm_flags & VM_MIXEDMAP) && pfn_valid(pfn));
2220 
2221     if (addr < vma->vm_start || addr >= vma->vm_end)
2222         return VM_FAULT_SIGBUS;
2223 
2224     if (!pfn_modify_allowed(pfn, pgprot))
2225         return VM_FAULT_SIGBUS;
2226 
2227     track_pfn_insert(vma, &pgprot, __pfn_to_pfn_t(pfn, PFN_DEV));
2228 
2229     return insert_pfn(vma, addr, __pfn_to_pfn_t(pfn, PFN_DEV), pgprot,
2230             false);
2231 }
2232 EXPORT_SYMBOL(vmf_insert_pfn_prot);
2233 
2234 /**
2235  * vmf_insert_pfn - insert single pfn into user vma
2236  * @vma: user vma to map to
2237  * @addr: target user address of this page
2238  * @pfn: source kernel pfn
2239  *
2240  * Similar to vm_insert_page, this allows drivers to insert individual pages
2241  * they've allocated into a user vma. Same comments apply.
2242  *
2243  * This function should only be called from a vm_ops->fault handler, and
2244  * in that case the handler should return the result of this function.
2245  *
2246  * vma cannot be a COW mapping.
2247  *
2248  * As this is called only for pages that do not currently exist, we
2249  * do not need to flush old virtual caches or the TLB.
2250  *
2251  * Context: Process context.  May allocate using %GFP_KERNEL.
2252  * Return: vm_fault_t value.
2253  */
2254 vm_fault_t vmf_insert_pfn(struct vm_area_struct *vma, unsigned long addr,
2255             unsigned long pfn)
2256 {
2257     return vmf_insert_pfn_prot(vma, addr, pfn, vma->vm_page_prot);
2258 }
2259 EXPORT_SYMBOL(vmf_insert_pfn);
2260 
2261 static bool vm_mixed_ok(struct vm_area_struct *vma, pfn_t pfn)
2262 {
2263     /* these checks mirror the abort conditions in vm_normal_page */
2264     if (vma->vm_flags & VM_MIXEDMAP)
2265         return true;
2266     if (pfn_t_devmap(pfn))
2267         return true;
2268     if (pfn_t_special(pfn))
2269         return true;
2270     if (is_zero_pfn(pfn_t_to_pfn(pfn)))
2271         return true;
2272     return false;
2273 }
2274 
2275 static vm_fault_t __vm_insert_mixed(struct vm_area_struct *vma,
2276         unsigned long addr, pfn_t pfn, pgprot_t pgprot,
2277         bool mkwrite)
2278 {
2279     int err;
2280 
2281     BUG_ON(!vm_mixed_ok(vma, pfn));
2282 
2283     if (addr < vma->vm_start || addr >= vma->vm_end)
2284         return VM_FAULT_SIGBUS;
2285 
2286     track_pfn_insert(vma, &pgprot, pfn);
2287 
2288     if (!pfn_modify_allowed(pfn_t_to_pfn(pfn), pgprot))
2289         return VM_FAULT_SIGBUS;
2290 
2291     /*
2292      * If we don't have pte special, then we have to use the pfn_valid()
2293      * based VM_MIXEDMAP scheme (see vm_normal_page), and thus we *must*
2294      * refcount the page if pfn_valid is true (hence insert_page rather
2295      * than insert_pfn).  If a zero_pfn were inserted into a VM_MIXEDMAP
2296      * without pte special, it would there be refcounted as a normal page.
2297      */
2298     if (!IS_ENABLED(CONFIG_ARCH_HAS_PTE_SPECIAL) &&
2299         !pfn_t_devmap(pfn) && pfn_t_valid(pfn)) {
2300         struct page *page;
2301 
2302         /*
2303          * At this point we are committed to insert_page()
2304          * regardless of whether the caller specified flags that
2305          * result in pfn_t_has_page() == false.
2306          */
2307         page = pfn_to_page(pfn_t_to_pfn(pfn));
2308         err = insert_page(vma, addr, page, pgprot);
2309     } else {
2310         return insert_pfn(vma, addr, pfn, pgprot, mkwrite);
2311     }
2312 
2313     if (err == -ENOMEM)
2314         return VM_FAULT_OOM;
2315     if (err < 0 && err != -EBUSY)
2316         return VM_FAULT_SIGBUS;
2317 
2318     return VM_FAULT_NOPAGE;
2319 }
2320 
2321 /**
2322  * vmf_insert_mixed_prot - insert single pfn into user vma with specified pgprot
2323  * @vma: user vma to map to
2324  * @addr: target user address of this page
2325  * @pfn: source kernel pfn
2326  * @pgprot: pgprot flags for the inserted page
2327  *
2328  * This is exactly like vmf_insert_mixed(), except that it allows drivers
2329  * to override pgprot on a per-page basis.
2330  *
2331  * Typically this function should be used by drivers to set caching- and
2332  * encryption bits different than those of @vma->vm_page_prot, because
2333  * the caching- or encryption mode may not be known at mmap() time.
2334  * This is ok as long as @vma->vm_page_prot is not used by the core vm
2335  * to set caching and encryption bits for those vmas (except for COW pages).
2336  * This is ensured by core vm only modifying these page table entries using
2337  * functions that don't touch caching- or encryption bits, using pte_modify()
2338  * if needed. (See for example mprotect()).
2339  * Also when new page-table entries are created, this is only done using the
2340  * fault() callback, and never using the value of vma->vm_page_prot,
2341  * except for page-table entries that point to anonymous pages as the result
2342  * of COW.
2343  *
2344  * Context: Process context.  May allocate using %GFP_KERNEL.
2345  * Return: vm_fault_t value.
2346  */
2347 vm_fault_t vmf_insert_mixed_prot(struct vm_area_struct *vma, unsigned long addr,
2348                  pfn_t pfn, pgprot_t pgprot)
2349 {
2350     return __vm_insert_mixed(vma, addr, pfn, pgprot, false);
2351 }
2352 EXPORT_SYMBOL(vmf_insert_mixed_prot);
2353 
2354 vm_fault_t vmf_insert_mixed(struct vm_area_struct *vma, unsigned long addr,
2355         pfn_t pfn)
2356 {
2357     return __vm_insert_mixed(vma, addr, pfn, vma->vm_page_prot, false);
2358 }
2359 EXPORT_SYMBOL(vmf_insert_mixed);
2360 
2361 /*
2362  *  If the insertion of PTE failed because someone else already added a
2363  *  different entry in the mean time, we treat that as success as we assume
2364  *  the same entry was actually inserted.
2365  */
2366 vm_fault_t vmf_insert_mixed_mkwrite(struct vm_area_struct *vma,
2367         unsigned long addr, pfn_t pfn)
2368 {
2369     return __vm_insert_mixed(vma, addr, pfn, vma->vm_page_prot, true);
2370 }
2371 EXPORT_SYMBOL(vmf_insert_mixed_mkwrite);
2372 
2373 /*
2374  * maps a range of physical memory into the requested pages. the old
2375  * mappings are removed. any references to nonexistent pages results
2376  * in null mappings (currently treated as "copy-on-access")
2377  */
2378 static int remap_pte_range(struct mm_struct *mm, pmd_t *pmd,
2379             unsigned long addr, unsigned long end,
2380             unsigned long pfn, pgprot_t prot)
2381 {
2382     pte_t *pte, *mapped_pte;
2383     spinlock_t *ptl;
2384     int err = 0;
2385 
2386     mapped_pte = pte = pte_alloc_map_lock(mm, pmd, addr, &ptl);
2387     if (!pte)
2388         return -ENOMEM;
2389     arch_enter_lazy_mmu_mode();
2390     do {
2391         BUG_ON(!pte_none(*pte));
2392         if (!pfn_modify_allowed(pfn, prot)) {
2393             err = -EACCES;
2394             break;
2395         }
2396         set_pte_at(mm, addr, pte, pte_mkspecial(pfn_pte(pfn, prot)));
2397         pfn++;
2398     } while (pte++, addr += PAGE_SIZE, addr != end);
2399     arch_leave_lazy_mmu_mode();
2400     pte_unmap_unlock(mapped_pte, ptl);
2401     return err;
2402 }
2403 
2404 static inline int remap_pmd_range(struct mm_struct *mm, pud_t *pud,
2405             unsigned long addr, unsigned long end,
2406             unsigned long pfn, pgprot_t prot)
2407 {
2408     pmd_t *pmd;
2409     unsigned long next;
2410     int err;
2411 
2412     pfn -= addr >> PAGE_SHIFT;
2413     pmd = pmd_alloc(mm, pud, addr);
2414     if (!pmd)
2415         return -ENOMEM;
2416     VM_BUG_ON(pmd_trans_huge(*pmd));
2417     do {
2418         next = pmd_addr_end(addr, end);
2419         err = remap_pte_range(mm, pmd, addr, next,
2420                 pfn + (addr >> PAGE_SHIFT), prot);
2421         if (err)
2422             return err;
2423     } while (pmd++, addr = next, addr != end);
2424     return 0;
2425 }
2426 
2427 static inline int remap_pud_range(struct mm_struct *mm, p4d_t *p4d,
2428             unsigned long addr, unsigned long end,
2429             unsigned long pfn, pgprot_t prot)
2430 {
2431     pud_t *pud;
2432     unsigned long next;
2433     int err;
2434 
2435     pfn -= addr >> PAGE_SHIFT;
2436     pud = pud_alloc(mm, p4d, addr);
2437     if (!pud)
2438         return -ENOMEM;
2439     do {
2440         next = pud_addr_end(addr, end);
2441         err = remap_pmd_range(mm, pud, addr, next,
2442                 pfn + (addr >> PAGE_SHIFT), prot);
2443         if (err)
2444             return err;
2445     } while (pud++, addr = next, addr != end);
2446     return 0;
2447 }
2448 
2449 static inline int remap_p4d_range(struct mm_struct *mm, pgd_t *pgd,
2450             unsigned long addr, unsigned long end,
2451             unsigned long pfn, pgprot_t prot)
2452 {
2453     p4d_t *p4d;
2454     unsigned long next;
2455     int err;
2456 
2457     pfn -= addr >> PAGE_SHIFT;
2458     p4d = p4d_alloc(mm, pgd, addr);
2459     if (!p4d)
2460         return -ENOMEM;
2461     do {
2462         next = p4d_addr_end(addr, end);
2463         err = remap_pud_range(mm, p4d, addr, next,
2464                 pfn + (addr >> PAGE_SHIFT), prot);
2465         if (err)
2466             return err;
2467     } while (p4d++, addr = next, addr != end);
2468     return 0;
2469 }
2470 
2471 /*
2472  * Variant of remap_pfn_range that does not call track_pfn_remap.  The caller
2473  * must have pre-validated the caching bits of the pgprot_t.
2474  */
2475 int remap_pfn_range_notrack(struct vm_area_struct *vma, unsigned long addr,
2476         unsigned long pfn, unsigned long size, pgprot_t prot)
2477 {
2478     pgd_t *pgd;
2479     unsigned long next;
2480     unsigned long end = addr + PAGE_ALIGN(size);
2481     struct mm_struct *mm = vma->vm_mm;
2482     int err;
2483 
2484     if (WARN_ON_ONCE(!PAGE_ALIGNED(addr)))
2485         return -EINVAL;
2486 
2487     /*
2488      * Physically remapped pages are special. Tell the
2489      * rest of the world about it:
2490      *   VM_IO tells people not to look at these pages
2491      *  (accesses can have side effects).
2492      *   VM_PFNMAP tells the core MM that the base pages are just
2493      *  raw PFN mappings, and do not have a "struct page" associated
2494      *  with them.
2495      *   VM_DONTEXPAND
2496      *      Disable vma merging and expanding with mremap().
2497      *   VM_DONTDUMP
2498      *      Omit vma from core dump, even when VM_IO turned off.
2499      *
2500      * There's a horrible special case to handle copy-on-write
2501      * behaviour that some programs depend on. We mark the "original"
2502      * un-COW'ed pages by matching them up with "vma->vm_pgoff".
2503      * See vm_normal_page() for details.
2504      */
2505     if (is_cow_mapping(vma->vm_flags)) {
2506         if (addr != vma->vm_start || end != vma->vm_end)
2507             return -EINVAL;
2508         vma->vm_pgoff = pfn;
2509     }
2510 
2511     vma->vm_flags |= VM_IO | VM_PFNMAP | VM_DONTEXPAND | VM_DONTDUMP;
2512 
2513     BUG_ON(addr >= end);
2514     pfn -= addr >> PAGE_SHIFT;
2515     pgd = pgd_offset(mm, addr);
2516     flush_cache_range(vma, addr, end);
2517     do {
2518         next = pgd_addr_end(addr, end);
2519         err = remap_p4d_range(mm, pgd, addr, next,
2520                 pfn + (addr >> PAGE_SHIFT), prot);
2521         if (err)
2522             return err;
2523     } while (pgd++, addr = next, addr != end);
2524 
2525     return 0;
2526 }
2527 
2528 /**
2529  * remap_pfn_range - remap kernel memory to userspace
2530  * @vma: user vma to map to
2531  * @addr: target page aligned user address to start at
2532  * @pfn: page frame number of kernel physical memory address
2533  * @size: size of mapping area
2534  * @prot: page protection flags for this mapping
2535  *
2536  * Note: this is only safe if the mm semaphore is held when called.
2537  *
2538  * Return: %0 on success, negative error code otherwise.
2539  */
2540 int remap_pfn_range(struct vm_area_struct *vma, unsigned long addr,
2541             unsigned long pfn, unsigned long size, pgprot_t prot)
2542 {
2543     int err;
2544 
2545     err = track_pfn_remap(vma, &prot, pfn, addr, PAGE_ALIGN(size));
2546     if (err)
2547         return -EINVAL;
2548 
2549     err = remap_pfn_range_notrack(vma, addr, pfn, size, prot);
2550     if (err)
2551         untrack_pfn(vma, pfn, PAGE_ALIGN(size));
2552     return err;
2553 }
2554 EXPORT_SYMBOL(remap_pfn_range);
2555 
2556 /**
2557  * vm_iomap_memory - remap memory to userspace
2558  * @vma: user vma to map to
2559  * @start: start of the physical memory to be mapped
2560  * @len: size of area
2561  *
2562  * This is a simplified io_remap_pfn_range() for common driver use. The
2563  * driver just needs to give us the physical memory range to be mapped,
2564  * we'll figure out the rest from the vma information.
2565  *
2566  * NOTE! Some drivers might want to tweak vma->vm_page_prot first to get
2567  * whatever write-combining details or similar.
2568  *
2569  * Return: %0 on success, negative error code otherwise.
2570  */
2571 int vm_iomap_memory(struct vm_area_struct *vma, phys_addr_t start, unsigned long len)
2572 {
2573     unsigned long vm_len, pfn, pages;
2574 
2575     /* Check that the physical memory area passed in looks valid */
2576     if (start + len < start)
2577         return -EINVAL;
2578     /*
2579      * You *really* shouldn't map things that aren't page-aligned,
2580      * but we've historically allowed it because IO memory might
2581      * just have smaller alignment.
2582      */
2583     len += start & ~PAGE_MASK;
2584     pfn = start >> PAGE_SHIFT;
2585     pages = (len + ~PAGE_MASK) >> PAGE_SHIFT;
2586     if (pfn + pages < pfn)
2587         return -EINVAL;
2588 
2589     /* We start the mapping 'vm_pgoff' pages into the area */
2590     if (vma->vm_pgoff > pages)
2591         return -EINVAL;
2592     pfn += vma->vm_pgoff;
2593     pages -= vma->vm_pgoff;
2594 
2595     /* Can we fit all of the mapping? */
2596     vm_len = vma->vm_end - vma->vm_start;
2597     if (vm_len >> PAGE_SHIFT > pages)
2598         return -EINVAL;
2599 
2600     /* Ok, let it rip */
2601     return io_remap_pfn_range(vma, vma->vm_start, pfn, vm_len, vma->vm_page_prot);
2602 }
2603 EXPORT_SYMBOL(vm_iomap_memory);
2604 
2605 static int apply_to_pte_range(struct mm_struct *mm, pmd_t *pmd,
2606                      unsigned long addr, unsigned long end,
2607                      pte_fn_t fn, void *data, bool create,
2608                      pgtbl_mod_mask *mask)
2609 {
2610     pte_t *pte, *mapped_pte;
2611     int err = 0;
2612     spinlock_t *ptl;
2613 
2614     if (create) {
2615         mapped_pte = pte = (mm == &init_mm) ?
2616             pte_alloc_kernel_track(pmd, addr, mask) :
2617             pte_alloc_map_lock(mm, pmd, addr, &ptl);
2618         if (!pte)
2619             return -ENOMEM;
2620     } else {
2621         mapped_pte = pte = (mm == &init_mm) ?
2622             pte_offset_kernel(pmd, addr) :
2623             pte_offset_map_lock(mm, pmd, addr, &ptl);
2624     }
2625 
2626     BUG_ON(pmd_huge(*pmd));
2627 
2628     arch_enter_lazy_mmu_mode();
2629 
2630     if (fn) {
2631         do {
2632             if (create || !pte_none(*pte)) {
2633                 err = fn(pte++, addr, data);
2634                 if (err)
2635                     break;
2636             }
2637         } while (addr += PAGE_SIZE, addr != end);
2638     }
2639     *mask |= PGTBL_PTE_MODIFIED;
2640 
2641     arch_leave_lazy_mmu_mode();
2642 
2643     if (mm != &init_mm)
2644         pte_unmap_unlock(mapped_pte, ptl);
2645     return err;
2646 }
2647 
2648 static int apply_to_pmd_range(struct mm_struct *mm, pud_t *pud,
2649                      unsigned long addr, unsigned long end,
2650                      pte_fn_t fn, void *data, bool create,
2651                      pgtbl_mod_mask *mask)
2652 {
2653     pmd_t *pmd;
2654     unsigned long next;
2655     int err = 0;
2656 
2657     BUG_ON(pud_huge(*pud));
2658 
2659     if (create) {
2660         pmd = pmd_alloc_track(mm, pud, addr, mask);
2661         if (!pmd)
2662             return -ENOMEM;
2663     } else {
2664         pmd = pmd_offset(pud, addr);
2665     }
2666     do {
2667         next = pmd_addr_end(addr, end);
2668         if (pmd_none(*pmd) && !create)
2669             continue;
2670         if (WARN_ON_ONCE(pmd_leaf(*pmd)))
2671             return -EINVAL;
2672         if (!pmd_none(*pmd) && WARN_ON_ONCE(pmd_bad(*pmd))) {
2673             if (!create)
2674                 continue;
2675             pmd_clear_bad(pmd);
2676         }
2677         err = apply_to_pte_range(mm, pmd, addr, next,
2678                      fn, data, create, mask);
2679         if (err)
2680             break;
2681     } while (pmd++, addr = next, addr != end);
2682 
2683     return err;
2684 }
2685 
2686 static int apply_to_pud_range(struct mm_struct *mm, p4d_t *p4d,
2687                      unsigned long addr, unsigned long end,
2688                      pte_fn_t fn, void *data, bool create,
2689                      pgtbl_mod_mask *mask)
2690 {
2691     pud_t *pud;
2692     unsigned long next;
2693     int err = 0;
2694 
2695     if (create) {
2696         pud = pud_alloc_track(mm, p4d, addr, mask);
2697         if (!pud)
2698             return -ENOMEM;
2699     } else {
2700         pud = pud_offset(p4d, addr);
2701     }
2702     do {
2703         next = pud_addr_end(addr, end);
2704         if (pud_none(*pud) && !create)
2705             continue;
2706         if (WARN_ON_ONCE(pud_leaf(*pud)))
2707             return -EINVAL;
2708         if (!pud_none(*pud) && WARN_ON_ONCE(pud_bad(*pud))) {
2709             if (!create)
2710                 continue;
2711             pud_clear_bad(pud);
2712         }
2713         err = apply_to_pmd_range(mm, pud, addr, next,
2714                      fn, data, create, mask);
2715         if (err)
2716             break;
2717     } while (pud++, addr = next, addr != end);
2718 
2719     return err;
2720 }
2721 
2722 static int apply_to_p4d_range(struct mm_struct *mm, pgd_t *pgd,
2723                      unsigned long addr, unsigned long end,
2724                      pte_fn_t fn, void *data, bool create,
2725                      pgtbl_mod_mask *mask)
2726 {
2727     p4d_t *p4d;
2728     unsigned long next;
2729     int err = 0;
2730 
2731     if (create) {
2732         p4d = p4d_alloc_track(mm, pgd, addr, mask);
2733         if (!p4d)
2734             return -ENOMEM;
2735     } else {
2736         p4d = p4d_offset(pgd, addr);
2737     }
2738     do {
2739         next = p4d_addr_end(addr, end);
2740         if (p4d_none(*p4d) && !create)
2741             continue;
2742         if (WARN_ON_ONCE(p4d_leaf(*p4d)))
2743             return -EINVAL;
2744         if (!p4d_none(*p4d) && WARN_ON_ONCE(p4d_bad(*p4d))) {
2745             if (!create)
2746                 continue;
2747             p4d_clear_bad(p4d);
2748         }
2749         err = apply_to_pud_range(mm, p4d, addr, next,
2750                      fn, data, create, mask);
2751         if (err)
2752             break;
2753     } while (p4d++, addr = next, addr != end);
2754 
2755     return err;
2756 }
2757 
2758 static int __apply_to_page_range(struct mm_struct *mm, unsigned long addr,
2759                  unsigned long size, pte_fn_t fn,
2760                  void *data, bool create)
2761 {
2762     pgd_t *pgd;
2763     unsigned long start = addr, next;
2764     unsigned long end = addr + size;
2765     pgtbl_mod_mask mask = 0;
2766     int err = 0;
2767 
2768     if (WARN_ON(addr >= end))
2769         return -EINVAL;
2770 
2771     pgd = pgd_offset(mm, addr);
2772     do {
2773         next = pgd_addr_end(addr, end);
2774         if (pgd_none(*pgd) && !create)
2775             continue;
2776         if (WARN_ON_ONCE(pgd_leaf(*pgd)))
2777             return -EINVAL;
2778         if (!pgd_none(*pgd) && WARN_ON_ONCE(pgd_bad(*pgd))) {
2779             if (!create)
2780                 continue;
2781             pgd_clear_bad(pgd);
2782         }
2783         err = apply_to_p4d_range(mm, pgd, addr, next,
2784                      fn, data, create, &mask);
2785         if (err)
2786             break;
2787     } while (pgd++, addr = next, addr != end);
2788 
2789     if (mask & ARCH_PAGE_TABLE_SYNC_MASK)
2790         arch_sync_kernel_mappings(start, start + size);
2791 
2792     return err;
2793 }
2794 
2795 /*
2796  * Scan a region of virtual memory, filling in page tables as necessary
2797  * and calling a provided function on each leaf page table.
2798  */
2799 int apply_to_page_range(struct mm_struct *mm, unsigned long addr,
2800             unsigned long size, pte_fn_t fn, void *data)
2801 {
2802     return __apply_to_page_range(mm, addr, size, fn, data, true);
2803 }
2804 EXPORT_SYMBOL_GPL(apply_to_page_range);
2805 
2806 /*
2807  * Scan a region of virtual memory, calling a provided function on
2808  * each leaf page table where it exists.
2809  *
2810  * Unlike apply_to_page_range, this does _not_ fill in page tables
2811  * where they are absent.
2812  */
2813 int apply_to_existing_page_range(struct mm_struct *mm, unsigned long addr,
2814                  unsigned long size, pte_fn_t fn, void *data)
2815 {
2816     return __apply_to_page_range(mm, addr, size, fn, data, false);
2817 }
2818 EXPORT_SYMBOL_GPL(apply_to_existing_page_range);
2819 
2820 /*
2821  * handle_pte_fault chooses page fault handler according to an entry which was
2822  * read non-atomically.  Before making any commitment, on those architectures
2823  * or configurations (e.g. i386 with PAE) which might give a mix of unmatched
2824  * parts, do_swap_page must check under lock before unmapping the pte and
2825  * proceeding (but do_wp_page is only called after already making such a check;
2826  * and do_anonymous_page can safely check later on).
2827  */
2828 static inline int pte_unmap_same(struct vm_fault *vmf)
2829 {
2830     int same = 1;
2831 #if defined(CONFIG_SMP) || defined(CONFIG_PREEMPTION)
2832     if (sizeof(pte_t) > sizeof(unsigned long)) {
2833         spinlock_t *ptl = pte_lockptr(vmf->vma->vm_mm, vmf->pmd);
2834         spin_lock(ptl);
2835         same = pte_same(*vmf->pte, vmf->orig_pte);
2836         spin_unlock(ptl);
2837     }
2838 #endif
2839     pte_unmap(vmf->pte);
2840     vmf->pte = NULL;
2841     return same;
2842 }
2843 
2844 static inline bool __wp_page_copy_user(struct page *dst, struct page *src,
2845                        struct vm_fault *vmf)
2846 {
2847     bool ret;
2848     void *kaddr;
2849     void __user *uaddr;
2850     bool locked = false;
2851     struct vm_area_struct *vma = vmf->vma;
2852     struct mm_struct *mm = vma->vm_mm;
2853     unsigned long addr = vmf->address;
2854 
2855     if (likely(src)) {
2856         copy_user_highpage(dst, src, addr, vma);
2857         return true;
2858     }
2859 
2860     /*
2861      * If the source page was a PFN mapping, we don't have
2862      * a "struct page" for it. We do a best-effort copy by
2863      * just copying from the original user address. If that
2864      * fails, we just zero-fill it. Live with it.
2865      */
2866     kaddr = kmap_atomic(dst);
2867     uaddr = (void __user *)(addr & PAGE_MASK);
2868 
2869     /*
2870      * On architectures with software "accessed" bits, we would
2871      * take a double page fault, so mark it accessed here.
2872      */
2873     if (arch_faults_on_old_pte() && !pte_young(vmf->orig_pte)) {
2874         pte_t entry;
2875 
2876         vmf->pte = pte_offset_map_lock(mm, vmf->pmd, addr, &vmf->ptl);
2877         locked = true;
2878         if (!likely(pte_same(*vmf->pte, vmf->orig_pte))) {
2879             /*
2880              * Other thread has already handled the fault
2881              * and update local tlb only
2882              */
2883             update_mmu_tlb(vma, addr, vmf->pte);
2884             ret = false;
2885             goto pte_unlock;
2886         }
2887 
2888         entry = pte_mkyoung(vmf->orig_pte);
2889         if (ptep_set_access_flags(vma, addr, vmf->pte, entry, 0))
2890             update_mmu_cache(vma, addr, vmf->pte);
2891     }
2892 
2893     /*
2894      * This really shouldn't fail, because the page is there
2895      * in the page tables. But it might just be unreadable,
2896      * in which case we just give up and fill the result with
2897      * zeroes.
2898      */
2899     if (__copy_from_user_inatomic(kaddr, uaddr, PAGE_SIZE)) {
2900         if (locked)
2901             goto warn;
2902 
2903         /* Re-validate under PTL if the page is still mapped */
2904         vmf->pte = pte_offset_map_lock(mm, vmf->pmd, addr, &vmf->ptl);
2905         locked = true;
2906         if (!likely(pte_same(*vmf->pte, vmf->orig_pte))) {
2907             /* The PTE changed under us, update local tlb */
2908             update_mmu_tlb(vma, addr, vmf->pte);
2909             ret = false;
2910             goto pte_unlock;
2911         }
2912 
2913         /*
2914          * The same page can be mapped back since last copy attempt.
2915          * Try to copy again under PTL.
2916          */
2917         if (__copy_from_user_inatomic(kaddr, uaddr, PAGE_SIZE)) {
2918             /*
2919              * Give a warn in case there can be some obscure
2920              * use-case
2921              */
2922 warn:
2923             WARN_ON_ONCE(1);
2924             clear_page(kaddr);
2925         }
2926     }
2927 
2928     ret = true;
2929 
2930 pte_unlock:
2931     if (locked)
2932         pte_unmap_unlock(vmf->pte, vmf->ptl);
2933     kunmap_atomic(kaddr);
2934     flush_dcache_page(dst);
2935 
2936     return ret;
2937 }
2938 
2939 static gfp_t __get_fault_gfp_mask(struct vm_area_struct *vma)
2940 {
2941     struct file *vm_file = vma->vm_file;
2942 
2943     if (vm_file)
2944         return mapping_gfp_mask(vm_file->f_mapping) | __GFP_FS | __GFP_IO;
2945 
2946     /*
2947      * Special mappings (e.g. VDSO) do not have any file so fake
2948      * a default GFP_KERNEL for them.
2949      */
2950     return GFP_KERNEL;
2951 }
2952 
2953 /*
2954  * Notify the address space that the page is about to become writable so that
2955  * it can prohibit this or wait for the page to get into an appropriate state.
2956  *
2957  * We do this without the lock held, so that it can sleep if it needs to.
2958  */
2959 static vm_fault_t do_page_mkwrite(struct vm_fault *vmf)
2960 {
2961     vm_fault_t ret;
2962     struct page *page = vmf->page;
2963     unsigned int old_flags = vmf->flags;
2964 
2965     vmf->flags = FAULT_FLAG_WRITE|FAULT_FLAG_MKWRITE;
2966 
2967     if (vmf->vma->vm_file &&
2968         IS_SWAPFILE(vmf->vma->vm_file->f_mapping->host))
2969         return VM_FAULT_SIGBUS;
2970 
2971     ret = vmf->vma->vm_ops->page_mkwrite(vmf);
2972     /* Restore original flags so that caller is not surprised */
2973     vmf->flags = old_flags;
2974     if (unlikely(ret & (VM_FAULT_ERROR | VM_FAULT_NOPAGE)))
2975         return ret;
2976     if (unlikely(!(ret & VM_FAULT_LOCKED))) {
2977         lock_page(page);
2978         if (!page->mapping) {
2979             unlock_page(page);
2980             return 0; /* retry */
2981         }
2982         ret |= VM_FAULT_LOCKED;
2983     } else
2984         VM_BUG_ON_PAGE(!PageLocked(page), page);
2985     return ret;
2986 }
2987 
2988 /*
2989  * Handle dirtying of a page in shared file mapping on a write fault.
2990  *
2991  * The function expects the page to be locked and unlocks it.
2992  */
2993 static vm_fault_t fault_dirty_shared_page(struct vm_fault *vmf)
2994 {
2995     struct vm_area_struct *vma = vmf->vma;
2996     struct address_space *mapping;
2997     struct page *page = vmf->page;
2998     bool dirtied;
2999     bool page_mkwrite = vma->vm_ops && vma->vm_ops->page_mkwrite;
3000 
3001     dirtied = set_page_dirty(page);
3002     VM_BUG_ON_PAGE(PageAnon(page), page);
3003     /*
3004      * Take a local copy of the address_space - page.mapping may be zeroed
3005      * by truncate after unlock_page().   The address_space itself remains
3006      * pinned by vma->vm_file's reference.  We rely on unlock_page()'s
3007      * release semantics to prevent the compiler from undoing this copying.
3008      */
3009     mapping = page_rmapping(page);
3010     unlock_page(page);
3011 
3012     if (!page_mkwrite)
3013         file_update_time(vma->vm_file);
3014 
3015     /*
3016      * Throttle page dirtying rate down to writeback speed.
3017      *
3018      * mapping may be NULL here because some device drivers do not
3019      * set page.mapping but still dirty their pages
3020      *
3021      * Drop the mmap_lock before waiting on IO, if we can. The file
3022      * is pinning the mapping, as per above.
3023      */
3024     if ((dirtied || page_mkwrite) && mapping) {
3025         struct file *fpin;
3026 
3027         fpin = maybe_unlock_mmap_for_io(vmf, NULL);
3028         balance_dirty_pages_ratelimited(mapping);
3029         if (fpin) {
3030             fput(fpin);
3031             return VM_FAULT_COMPLETED;
3032         }
3033     }
3034 
3035     return 0;
3036 }
3037 
3038 /*
3039  * Handle write page faults for pages that can be reused in the current vma
3040  *
3041  * This can happen either due to the mapping being with the VM_SHARED flag,
3042  * or due to us being the last reference standing to the page. In either
3043  * case, all we need to do here is to mark the page as writable and update
3044  * any related book-keeping.
3045  */
3046 static inline void wp_page_reuse(struct vm_fault *vmf)
3047     __releases(vmf->ptl)
3048 {
3049     struct vm_area_struct *vma = vmf->vma;
3050     struct page *page = vmf->page;
3051     pte_t entry;
3052 
3053     VM_BUG_ON(!(vmf->flags & FAULT_FLAG_WRITE));
3054     VM_BUG_ON(page && PageAnon(page) && !PageAnonExclusive(page));
3055 
3056     /*
3057      * Clear the pages cpupid information as the existing
3058      * information potentially belongs to a now completely
3059      * unrelated process.
3060      */
3061     if (page)
3062         page_cpupid_xchg_last(page, (1 << LAST_CPUPID_SHIFT) - 1);
3063 
3064     flush_cache_page(vma, vmf->address, pte_pfn(vmf->orig_pte));
3065     entry = pte_mkyoung(vmf->orig_pte);
3066     entry = maybe_mkwrite(pte_mkdirty(entry), vma);
3067     if (ptep_set_access_flags(vma, vmf->address, vmf->pte, entry, 1))
3068         update_mmu_cache(vma, vmf->address, vmf->pte);
3069     pte_unmap_unlock(vmf->pte, vmf->ptl);
3070     count_vm_event(PGREUSE);
3071 }
3072 
3073 /*
3074  * Handle the case of a page which we actually need to copy to a new page,
3075  * either due to COW or unsharing.
3076  *
3077  * Called with mmap_lock locked and the old page referenced, but
3078  * without the ptl held.
3079  *
3080  * High level logic flow:
3081  *
3082  * - Allocate a page, copy the content of the old page to the new one.
3083  * - Handle book keeping and accounting - cgroups, mmu-notifiers, etc.
3084  * - Take the PTL. If the pte changed, bail out and release the allocated page
3085  * - If the pte is still the way we remember it, update the page table and all
3086  *   relevant references. This includes dropping the reference the page-table
3087  *   held to the old page, as well as updating the rmap.
3088  * - In any case, unlock the PTL and drop the reference we took to the old page.
3089  */
3090 static vm_fault_t wp_page_copy(struct vm_fault *vmf)
3091 {
3092     const bool unshare = vmf->flags & FAULT_FLAG_UNSHARE;
3093     struct vm_area_struct *vma = vmf->vma;
3094     struct mm_struct *mm = vma->vm_mm;
3095     struct page *old_page = vmf->page;
3096     struct page *new_page = NULL;
3097     pte_t entry;
3098     int page_copied = 0;
3099     struct mmu_notifier_range range;
3100 
3101     delayacct_wpcopy_start();
3102 
3103     if (unlikely(anon_vma_prepare(vma)))
3104         goto oom;
3105 
3106     if (is_zero_pfn(pte_pfn(vmf->orig_pte))) {
3107         new_page = alloc_zeroed_user_highpage_movable(vma,
3108                                   vmf->address);
3109         if (!new_page)
3110             goto oom;
3111     } else {
3112         new_page = alloc_page_vma(GFP_HIGHUSER_MOVABLE, vma,
3113                 vmf->address);
3114         if (!new_page)
3115             goto oom;
3116 
3117         if (!__wp_page_copy_user(new_page, old_page, vmf)) {
3118             /*
3119              * COW failed, if the fault was solved by other,
3120              * it's fine. If not, userspace would re-fault on
3121              * the same address and we will handle the fault
3122              * from the second attempt.
3123              */
3124             put_page(new_page);
3125             if (old_page)
3126                 put_page(old_page);
3127 
3128             delayacct_wpcopy_end();
3129             return 0;
3130         }
3131     }
3132 
3133     if (mem_cgroup_charge(page_folio(new_page), mm, GFP_KERNEL))
3134         goto oom_free_new;
3135     cgroup_throttle_swaprate(new_page, GFP_KERNEL);
3136 
3137     __SetPageUptodate(new_page);
3138 
3139     mmu_notifier_range_init(&range, MMU_NOTIFY_CLEAR, 0, vma, mm,
3140                 vmf->address & PAGE_MASK,
3141                 (vmf->address & PAGE_MASK) + PAGE_SIZE);
3142     mmu_notifier_invalidate_range_start(&range);
3143 
3144     /*
3145      * Re-check the pte - we dropped the lock
3146      */
3147     vmf->pte = pte_offset_map_lock(mm, vmf->pmd, vmf->address, &vmf->ptl);
3148     if (likely(pte_same(*vmf->pte, vmf->orig_pte))) {
3149         if (old_page) {
3150             if (!PageAnon(old_page)) {
3151                 dec_mm_counter_fast(mm,
3152                         mm_counter_file(old_page));
3153                 inc_mm_counter_fast(mm, MM_ANONPAGES);
3154             }
3155         } else {
3156             inc_mm_counter_fast(mm, MM_ANONPAGES);
3157         }
3158         flush_cache_page(vma, vmf->address, pte_pfn(vmf->orig_pte));
3159         entry = mk_pte(new_page, vma->vm_page_prot);
3160         entry = pte_sw_mkyoung(entry);
3161         if (unlikely(unshare)) {
3162             if (pte_soft_dirty(vmf->orig_pte))
3163                 entry = pte_mksoft_dirty(entry);
3164             if (pte_uffd_wp(vmf->orig_pte))
3165                 entry = pte_mkuffd_wp(entry);
3166         } else {
3167             entry = maybe_mkwrite(pte_mkdirty(entry), vma);
3168         }
3169 
3170         /*
3171          * Clear the pte entry and flush it first, before updating the
3172          * pte with the new entry, to keep TLBs on different CPUs in
3173          * sync. This code used to set the new PTE then flush TLBs, but
3174          * that left a window where the new PTE could be loaded into
3175          * some TLBs while the old PTE remains in others.
3176          */
3177         ptep_clear_flush_notify(vma, vmf->address, vmf->pte);
3178         page_add_new_anon_rmap(new_page, vma, vmf->address);
3179         lru_cache_add_inactive_or_unevictable(new_page, vma);
3180         /*
3181          * We call the notify macro here because, when using secondary
3182          * mmu page tables (such as kvm shadow page tables), we want the
3183          * new page to be mapped directly into the secondary page table.
3184          */
3185         BUG_ON(unshare && pte_write(entry));
3186         set_pte_at_notify(mm, vmf->address, vmf->pte, entry);
3187         update_mmu_cache(vma, vmf->address, vmf->pte);
3188         if (old_page) {
3189             /*
3190              * Only after switching the pte to the new page may
3191              * we remove the mapcount here. Otherwise another
3192              * process may come and find the rmap count decremented
3193              * before the pte is switched to the new page, and
3194              * "reuse" the old page writing into it while our pte
3195              * here still points into it and can be read by other
3196              * threads.
3197              *
3198              * The critical issue is to order this
3199              * page_remove_rmap with the ptp_clear_flush above.
3200              * Those stores are ordered by (if nothing else,)
3201              * the barrier present in the atomic_add_negative
3202              * in page_remove_rmap.
3203              *
3204              * Then the TLB flush in ptep_clear_flush ensures that
3205              * no process can access the old page before the
3206              * decremented mapcount is visible. And the old page
3207              * cannot be reused until after the decremented
3208              * mapcount is visible. So transitively, TLBs to
3209              * old page will be flushed before it can be reused.
3210              */
3211             page_remove_rmap(old_page, vma, false);
3212         }
3213 
3214         /* Free the old page.. */
3215         new_page = old_page;
3216         page_copied = 1;
3217     } else {
3218         update_mmu_tlb(vma, vmf->address, vmf->pte);
3219     }
3220 
3221     if (new_page)
3222         put_page(new_page);
3223 
3224     pte_unmap_unlock(vmf->pte, vmf->ptl);
3225     /*
3226      * No need to double call mmu_notifier->invalidate_range() callback as
3227      * the above ptep_clear_flush_notify() did already call it.
3228      */
3229     mmu_notifier_invalidate_range_only_end(&range);
3230     if (old_page) {
3231         if (page_copied)
3232             free_swap_cache(old_page);
3233         put_page(old_page);
3234     }
3235 
3236     delayacct_wpcopy_end();
3237     return (page_copied && !unshare) ? VM_FAULT_WRITE : 0;
3238 oom_free_new:
3239     put_page(new_page);
3240 oom:
3241     if (old_page)
3242         put_page(old_page);
3243 
3244     delayacct_wpcopy_end();
3245     return VM_FAULT_OOM;
3246 }
3247 
3248 /**
3249  * finish_mkwrite_fault - finish page fault for a shared mapping, making PTE
3250  *            writeable once the page is prepared
3251  *
3252  * @vmf: structure describing the fault
3253  *
3254  * This function handles all that is needed to finish a write page fault in a
3255  * shared mapping due to PTE being read-only once the mapped page is prepared.
3256  * It handles locking of PTE and modifying it.
3257  *
3258  * The function expects the page to be locked or other protection against
3259  * concurrent faults / writeback (such as DAX radix tree locks).
3260  *
3261  * Return: %0 on success, %VM_FAULT_NOPAGE when PTE got changed before
3262  * we acquired PTE lock.
3263  */
3264 vm_fault_t finish_mkwrite_fault(struct vm_fault *vmf)
3265 {
3266     WARN_ON_ONCE(!(vmf->vma->vm_flags & VM_SHARED));
3267     vmf->pte = pte_offset_map_lock(vmf->vma->vm_mm, vmf->pmd, vmf->address,
3268                        &vmf->ptl);
3269     /*
3270      * We might have raced with another page fault while we released the
3271      * pte_offset_map_lock.
3272      */
3273     if (!pte_same(*vmf->pte, vmf->orig_pte)) {
3274         update_mmu_tlb(vmf->vma, vmf->address, vmf->pte);
3275         pte_unmap_unlock(vmf->pte, vmf->ptl);
3276         return VM_FAULT_NOPAGE;
3277     }
3278     wp_page_reuse(vmf);
3279     return 0;
3280 }
3281 
3282 /*
3283  * Handle write page faults for VM_MIXEDMAP or VM_PFNMAP for a VM_SHARED
3284  * mapping
3285  */
3286 static vm_fault_t wp_pfn_shared(struct vm_fault *vmf)
3287 {
3288     struct vm_area_struct *vma = vmf->vma;
3289 
3290     if (vma->vm_ops && vma->vm_ops->pfn_mkwrite) {
3291         vm_fault_t ret;
3292 
3293         pte_unmap_unlock(vmf->pte, vmf->ptl);
3294         vmf->flags |= FAULT_FLAG_MKWRITE;
3295         ret = vma->vm_ops->pfn_mkwrite(vmf);
3296         if (ret & (VM_FAULT_ERROR | VM_FAULT_NOPAGE))
3297             return ret;
3298         return finish_mkwrite_fault(vmf);
3299     }
3300     wp_page_reuse(vmf);
3301     return VM_FAULT_WRITE;
3302 }
3303 
3304 static vm_fault_t wp_page_shared(struct vm_fault *vmf)
3305     __releases(vmf->ptl)
3306 {
3307     struct vm_area_struct *vma = vmf->vma;
3308     vm_fault_t ret = VM_FAULT_WRITE;
3309 
3310     get_page(vmf->page);
3311 
3312     if (vma->vm_ops && vma->vm_ops->page_mkwrite) {
3313         vm_fault_t tmp;
3314 
3315         pte_unmap_unlock(vmf->pte, vmf->ptl);
3316         tmp = do_page_mkwrite(vmf);
3317         if (unlikely(!tmp || (tmp &
3318                       (VM_FAULT_ERROR | VM_FAULT_NOPAGE)))) {
3319             put_page(vmf->page);
3320             return tmp;
3321         }
3322         tmp = finish_mkwrite_fault(vmf);
3323         if (unlikely(tmp & (VM_FAULT_ERROR | VM_FAULT_NOPAGE))) {
3324             unlock_page(vmf->page);
3325             put_page(vmf->page);
3326             return tmp;
3327         }
3328     } else {
3329         wp_page_reuse(vmf);
3330         lock_page(vmf->page);
3331     }
3332     ret |= fault_dirty_shared_page(vmf);
3333     put_page(vmf->page);
3334 
3335     return ret;
3336 }
3337 
3338 /*
3339  * This routine handles present pages, when
3340  * * users try to write to a shared page (FAULT_FLAG_WRITE)
3341  * * GUP wants to take a R/O pin on a possibly shared anonymous page
3342  *   (FAULT_FLAG_UNSHARE)
3343  *
3344  * It is done by copying the page to a new address and decrementing the
3345  * shared-page counter for the old page.
3346  *
3347  * Note that this routine assumes that the protection checks have been
3348  * done by the caller (the low-level page fault routine in most cases).
3349  * Thus, with FAULT_FLAG_WRITE, we can safely just mark it writable once we've
3350  * done any necessary COW.
3351  *
3352  * In case of FAULT_FLAG_WRITE, we also mark the page dirty at this point even
3353  * though the page will change only once the write actually happens. This
3354  * avoids a few races, and potentially makes it more efficient.
3355  *
3356  * We enter with non-exclusive mmap_lock (to exclude vma changes,
3357  * but allow concurrent faults), with pte both mapped and locked.
3358  * We return with mmap_lock still held, but pte unmapped and unlocked.
3359  */
3360 static vm_fault_t do_wp_page(struct vm_fault *vmf)
3361     __releases(vmf->ptl)
3362 {
3363     const bool unshare = vmf->flags & FAULT_FLAG_UNSHARE;
3364     struct vm_area_struct *vma = vmf->vma;
3365 
3366     VM_BUG_ON(unshare && (vmf->flags & FAULT_FLAG_WRITE));
3367     VM_BUG_ON(!unshare && !(vmf->flags & FAULT_FLAG_WRITE));
3368 
3369     if (likely(!unshare)) {
3370         if (userfaultfd_pte_wp(vma, *vmf->pte)) {
3371             pte_unmap_unlock(vmf->pte, vmf->ptl);
3372             return handle_userfault(vmf, VM_UFFD_WP);
3373         }
3374 
3375         /*
3376          * Userfaultfd write-protect can defer flushes. Ensure the TLB
3377          * is flushed in this case before copying.
3378          */
3379         if (unlikely(userfaultfd_wp(vmf->vma) &&
3380                  mm_tlb_flush_pending(vmf->vma->vm_mm)))
3381             flush_tlb_page(vmf->vma, vmf->address);
3382     }
3383 
3384     vmf->page = vm_normal_page(vma, vmf->address, vmf->orig_pte);
3385     if (!vmf->page) {
3386         if (unlikely(unshare)) {
3387             /* No anonymous page -> nothing to do. */
3388             pte_unmap_unlock(vmf->pte, vmf->ptl);
3389             return 0;
3390         }
3391 
3392         /*
3393          * VM_MIXEDMAP !pfn_valid() case, or VM_SOFTDIRTY clear on a
3394          * VM_PFNMAP VMA.
3395          *
3396          * We should not cow pages in a shared writeable mapping.
3397          * Just mark the pages writable and/or call ops->pfn_mkwrite.
3398          */
3399         if ((vma->vm_flags & (VM_WRITE|VM_SHARED)) ==
3400                      (VM_WRITE|VM_SHARED))
3401             return wp_pfn_shared(vmf);
3402 
3403         pte_unmap_unlock(vmf->pte, vmf->ptl);
3404         return wp_page_copy(vmf);
3405     }
3406 
3407     /*
3408      * Take out anonymous pages first, anonymous shared vmas are
3409      * not dirty accountable.
3410      */
3411     if (PageAnon(vmf->page)) {
3412         struct page *page = vmf->page;
3413 
3414         /*
3415          * If the page is exclusive to this process we must reuse the
3416          * page without further checks.
3417          */
3418         if (PageAnonExclusive(page))
3419             goto reuse;
3420 
3421         /*
3422          * We have to verify under page lock: these early checks are
3423          * just an optimization to avoid locking the page and freeing
3424          * the swapcache if there is little hope that we can reuse.
3425          *
3426          * PageKsm() doesn't necessarily raise the page refcount.
3427          */
3428         if (PageKsm(page) || page_count(page) > 3)
3429             goto copy;
3430         if (!PageLRU(page))
3431             /*
3432              * Note: We cannot easily detect+handle references from
3433              * remote LRU pagevecs or references to PageLRU() pages.
3434              */
3435             lru_add_drain();
3436         if (page_count(page) > 1 + PageSwapCache(page))
3437             goto copy;
3438         if (!trylock_page(page))
3439             goto copy;
3440         if (PageSwapCache(page))
3441             try_to_free_swap(page);
3442         if (PageKsm(page) || page_count(page) != 1) {
3443             unlock_page(page);
3444             goto copy;
3445         }
3446         /*
3447          * Ok, we've got the only page reference from our mapping
3448          * and the page is locked, it's dark out, and we're wearing
3449          * sunglasses. Hit it.
3450          */
3451         page_move_anon_rmap(page, vma);
3452         unlock_page(page);
3453 reuse:
3454         if (unlikely(unshare)) {
3455             pte_unmap_unlock(vmf->pte, vmf->ptl);
3456             return 0;
3457         }
3458         wp_page_reuse(vmf);
3459         return VM_FAULT_WRITE;
3460     } else if (unshare) {
3461         /* No anonymous page -> nothing to do. */
3462         pte_unmap_unlock(vmf->pte, vmf->ptl);
3463         return 0;
3464     } else if (unlikely((vma->vm_flags & (VM_WRITE|VM_SHARED)) ==
3465                     (VM_WRITE|VM_SHARED))) {
3466         return wp_page_shared(vmf);
3467     }
3468 copy:
3469     /*
3470      * Ok, we need to copy. Oh, well..
3471      */
3472     get_page(vmf->page);
3473 
3474     pte_unmap_unlock(vmf->pte, vmf->ptl);
3475 #ifdef CONFIG_KSM
3476     if (PageKsm(vmf->page))
3477         count_vm_event(COW_KSM);
3478 #endif
3479     return wp_page_copy(vmf);
3480 }
3481 
3482 static void unmap_mapping_range_vma(struct vm_area_struct *vma,
3483         unsigned long start_addr, unsigned long end_addr,
3484         struct zap_details *details)
3485 {
3486     zap_page_range_single(vma, start_addr, end_addr - start_addr, details);
3487 }
3488 
3489 static inline void unmap_mapping_range_tree(struct rb_root_cached *root,
3490                         pgoff_t first_index,
3491                         pgoff_t last_index,
3492                         struct zap_details *details)
3493 {
3494     struct vm_area_struct *vma;
3495     pgoff_t vba, vea, zba, zea;
3496 
3497     vma_interval_tree_foreach(vma, root, first_index, last_index) {
3498         vba = vma->vm_pgoff;
3499         vea = vba + vma_pages(vma) - 1;
3500         zba = max(first_index, vba);
3501         zea = min(last_index, vea);
3502 
3503         unmap_mapping_range_vma(vma,
3504             ((zba - vba) << PAGE_SHIFT) + vma->vm_start,
3505             ((zea - vba + 1) << PAGE_SHIFT) + vma->vm_start,
3506                 details);
3507     }
3508 }
3509 
3510 /**
3511  * unmap_mapping_folio() - Unmap single folio from processes.
3512  * @folio: The locked folio to be unmapped.
3513  *
3514  * Unmap this folio from any userspace process which still has it mmaped.
3515  * Typically, for efficiency, the range of nearby pages has already been
3516  * unmapped by unmap_mapping_pages() or unmap_mapping_range().  But once
3517  * truncation or invalidation holds the lock on a folio, it may find that
3518  * the page has been remapped again: and then uses unmap_mapping_folio()
3519  * to unmap it finally.
3520  */
3521 void unmap_mapping_folio(struct folio *folio)
3522 {
3523     struct address_space *mapping = folio->mapping;
3524     struct zap_details details = { };
3525     pgoff_t first_index;
3526     pgoff_t last_index;
3527 
3528     VM_BUG_ON(!folio_test_locked(folio));
3529 
3530     first_index = folio->index;
3531     last_index = folio->index + folio_nr_pages(folio) - 1;
3532 
3533     details.even_cows = false;
3534     details.single_folio = folio;
3535     details.zap_flags = ZAP_FLAG_DROP_MARKER;
3536 
3537     i_mmap_lock_read(mapping);
3538     if (unlikely(!RB_EMPTY_ROOT(&mapping->i_mmap.rb_root)))
3539         unmap_mapping_range_tree(&mapping->i_mmap, first_index,
3540                      last_index, &details);
3541     i_mmap_unlock_read(mapping);
3542 }
3543 
3544 /**
3545  * unmap_mapping_pages() - Unmap pages from processes.
3546  * @mapping: The address space containing pages to be unmapped.
3547  * @start: Index of first page to be unmapped.
3548  * @nr: Number of pages to be unmapped.  0 to unmap to end of file.
3549  * @even_cows: Whether to unmap even private COWed pages.
3550  *
3551  * Unmap the pages in this address space from any userspace process which
3552  * has them mmaped.  Generally, you want to remove COWed pages as well when
3553  * a file is being truncated, but not when invalidating pages from the page
3554  * cache.
3555  */
3556 void unmap_mapping_pages(struct address_space *mapping, pgoff_t start,
3557         pgoff_t nr, bool even_cows)
3558 {
3559     struct zap_details details = { };
3560     pgoff_t first_index = start;
3561     pgoff_t last_index = start + nr - 1;
3562 
3563     details.even_cows = even_cows;
3564     if (last_index < first_index)
3565         last_index = ULONG_MAX;
3566 
3567     i_mmap_lock_read(mapping);
3568     if (unlikely(!RB_EMPTY_ROOT(&mapping->i_mmap.rb_root)))
3569         unmap_mapping_range_tree(&mapping->i_mmap, first_index,
3570                      last_index, &details);
3571     i_mmap_unlock_read(mapping);
3572 }
3573 EXPORT_SYMBOL_GPL(unmap_mapping_pages);
3574 
3575 /**
3576  * unmap_mapping_range - unmap the portion of all mmaps in the specified
3577  * address_space corresponding to the specified byte range in the underlying
3578  * file.
3579  *
3580  * @mapping: the address space containing mmaps to be unmapped.
3581  * @holebegin: byte in first page to unmap, relative to the start of
3582  * the underlying file.  This will be rounded down to a PAGE_SIZE
3583  * boundary.  Note that this is different from truncate_pagecache(), which
3584  * must keep the partial page.  In contrast, we must get rid of
3585  * partial pages.
3586  * @holelen: size of prospective hole in bytes.  This will be rounded
3587  * up to a PAGE_SIZE boundary.  A holelen of zero truncates to the
3588  * end of the file.
3589  * @even_cows: 1 when truncating a file, unmap even private COWed pages;
3590  * but 0 when invalidating pagecache, don't throw away private data.
3591  */
3592 void unmap_mapping_range(struct address_space *mapping,
3593         loff_t const holebegin, loff_t const holelen, int even_cows)
3594 {
3595     pgoff_t hba = holebegin >> PAGE_SHIFT;
3596     pgoff_t hlen = (holelen + PAGE_SIZE - 1) >> PAGE_SHIFT;
3597 
3598     /* Check for overflow. */
3599     if (sizeof(holelen) > sizeof(hlen)) {
3600         long long holeend =
3601             (holebegin + holelen + PAGE_SIZE - 1) >> PAGE_SHIFT;
3602         if (holeend & ~(long long)ULONG_MAX)
3603             hlen = ULONG_MAX - hba + 1;
3604     }
3605 
3606     unmap_mapping_pages(mapping, hba, hlen, even_cows);
3607 }
3608 EXPORT_SYMBOL(unmap_mapping_range);
3609 
3610 /*
3611  * Restore a potential device exclusive pte to a working pte entry
3612  */
3613 static vm_fault_t remove_device_exclusive_entry(struct vm_fault *vmf)
3614 {
3615     struct page *page = vmf->page;
3616     struct vm_area_struct *vma = vmf->vma;
3617     struct mmu_notifier_range range;
3618 
3619     if (!lock_page_or_retry(page, vma->vm_mm, vmf->flags))
3620         return VM_FAULT_RETRY;
3621     mmu_notifier_range_init_owner(&range, MMU_NOTIFY_EXCLUSIVE, 0, vma,
3622                 vma->vm_mm, vmf->address & PAGE_MASK,
3623                 (vmf->address & PAGE_MASK) + PAGE_SIZE, NULL);
3624     mmu_notifier_invalidate_range_start(&range);
3625 
3626     vmf->pte = pte_offset_map_lock(vma->vm_mm, vmf->pmd, vmf->address,
3627                 &vmf->ptl);
3628     if (likely(pte_same(*vmf->pte, vmf->orig_pte)))
3629         restore_exclusive_pte(vma, page, vmf->address, vmf->pte);
3630 
3631     pte_unmap_unlock(vmf->pte, vmf->ptl);
3632     unlock_page(page);
3633 
3634     mmu_notifier_invalidate_range_end(&range);
3635     return 0;
3636 }
3637 
3638 static inline bool should_try_to_free_swap(struct page *page,
3639                        struct vm_area_struct *vma,
3640                        unsigned int fault_flags)
3641 {
3642     if (!PageSwapCache(page))
3643         return false;
3644     if (mem_cgroup_swap_full(page) || (vma->vm_flags & VM_LOCKED) ||
3645         PageMlocked(page))
3646         return true;
3647     /*
3648      * If we want to map a page that's in the swapcache writable, we
3649      * have to detect via the refcount if we're really the exclusive
3650      * user. Try freeing the swapcache to get rid of the swapcache
3651      * reference only in case it's likely that we'll be the exlusive user.
3652      */
3653     return (fault_flags & FAULT_FLAG_WRITE) && !PageKsm(page) &&
3654         page_count(page) == 2;
3655 }
3656 
3657 static vm_fault_t pte_marker_clear(struct vm_fault *vmf)
3658 {
3659     vmf->pte = pte_offset_map_lock(vmf->vma->vm_mm, vmf->pmd,
3660                        vmf->address, &vmf->ptl);
3661     /*
3662      * Be careful so that we will only recover a special uffd-wp pte into a
3663      * none pte.  Otherwise it means the pte could have changed, so retry.
3664      */
3665     if (is_pte_marker(*vmf->pte))
3666         pte_clear(vmf->vma->vm_mm, vmf->address, vmf->pte);
3667     pte_unmap_unlock(vmf->pte, vmf->ptl);
3668     return 0;
3669 }
3670 
3671 /*
3672  * This is actually a page-missing access, but with uffd-wp special pte
3673  * installed.  It means this pte was wr-protected before being unmapped.
3674  */
3675 static vm_fault_t pte_marker_handle_uffd_wp(struct vm_fault *vmf)
3676 {
3677     /*
3678      * Just in case there're leftover special ptes even after the region
3679      * got unregistered - we can simply clear them.  We can also do that
3680      * proactively when e.g. when we do UFFDIO_UNREGISTER upon some uffd-wp
3681      * ranges, but it should be more efficient to be done lazily here.
3682      */
3683     if (unlikely(!userfaultfd_wp(vmf->vma) || vma_is_anonymous(vmf->vma)))
3684         return pte_marker_clear(vmf);
3685 
3686     /* do_fault() can handle pte markers too like none pte */
3687     return do_fault(vmf);
3688 }
3689 
3690 static vm_fault_t handle_pte_marker(struct vm_fault *vmf)
3691 {
3692     swp_entry_t entry = pte_to_swp_entry(vmf->orig_pte);
3693     unsigned long marker = pte_marker_get(entry);
3694 
3695     /*
3696      * PTE markers should always be with file-backed memories, and the
3697      * marker should never be empty.  If anything weird happened, the best
3698      * thing to do is to kill the process along with its mm.
3699      */
3700     if (WARN_ON_ONCE(vma_is_anonymous(vmf->vma) || !marker))
3701         return VM_FAULT_SIGBUS;
3702 
3703     if (pte_marker_entry_uffd_wp(entry))
3704         return pte_marker_handle_uffd_wp(vmf);
3705 
3706     /* This is an unknown pte marker */
3707     return VM_FAULT_SIGBUS;
3708 }
3709 
3710 /*
3711  * We enter with non-exclusive mmap_lock (to exclude vma changes,
3712  * but allow concurrent faults), and pte mapped but not yet locked.
3713  * We return with pte unmapped and unlocked.
3714  *
3715  * We return with the mmap_lock locked or unlocked in the same cases
3716  * as does filemap_fault().
3717  */
3718 vm_fault_t do_swap_page(struct vm_fault *vmf)
3719 {
3720     struct vm_area_struct *vma = vmf->vma;
3721     struct page *page = NULL, *swapcache;
3722     struct swap_info_struct *si = NULL;
3723     rmap_t rmap_flags = RMAP_NONE;
3724     bool exclusive = false;
3725     swp_entry_t entry;
3726     pte_t pte;
3727     int locked;
3728     vm_fault_t ret = 0;
3729     void *shadow = NULL;
3730 
3731     if (!pte_unmap_same(vmf))
3732         goto out;
3733 
3734     entry = pte_to_swp_entry(vmf->orig_pte);
3735     if (unlikely(non_swap_entry(entry))) {
3736         if (is_migration_entry(entry)) {
3737             migration_entry_wait(vma->vm_mm, vmf->pmd,
3738                          vmf->address);
3739         } else if (is_device_exclusive_entry(entry)) {
3740             vmf->page = pfn_swap_entry_to_page(entry);
3741             ret = remove_device_exclusive_entry(vmf);
3742         } else if (is_device_private_entry(entry)) {
3743             vmf->page = pfn_swap_entry_to_page(entry);
3744             ret = vmf->page->pgmap->ops->migrate_to_ram(vmf);
3745         } else if (is_hwpoison_entry(entry)) {
3746             ret = VM_FAULT_HWPOISON;
3747         } else if (is_swapin_error_entry(entry)) {
3748             ret = VM_FAULT_SIGBUS;
3749         } else if (is_pte_marker_entry(entry)) {
3750             ret = handle_pte_marker(vmf);
3751         } else {
3752             print_bad_pte(vma, vmf->address, vmf->orig_pte, NULL);
3753             ret = VM_FAULT_SIGBUS;
3754         }
3755         goto out;
3756     }
3757 
3758     /* Prevent swapoff from happening to us. */
3759     si = get_swap_device(entry);
3760     if (unlikely(!si))
3761         goto out;
3762 
3763     page = lookup_swap_cache(entry, vma, vmf->address);
3764     swapcache = page;
3765 
3766     if (!page) {
3767         if (data_race(si->flags & SWP_SYNCHRONOUS_IO) &&
3768             __swap_count(entry) == 1) {
3769             /* skip swapcache */
3770             page = alloc_page_vma(GFP_HIGHUSER_MOVABLE, vma,
3771                             vmf->address);
3772             if (page) {
3773                 __SetPageLocked(page);
3774                 __SetPageSwapBacked(page);
3775 
3776                 if (mem_cgroup_swapin_charge_page(page,
3777                     vma->vm_mm, GFP_KERNEL, entry)) {
3778                     ret = VM_FAULT_OOM;
3779                     goto out_page;
3780                 }
3781                 mem_cgroup_swapin_uncharge_swap(entry);
3782 
3783                 shadow = get_shadow_from_swap_cache(entry);
3784                 if (shadow)
3785                     workingset_refault(page_folio(page),
3786                                 shadow);
3787 
3788                 lru_cache_add(page);
3789 
3790                 /* To provide entry to swap_readpage() */
3791                 set_page_private(page, entry.val);
3792                 swap_readpage(page, true, NULL);
3793                 set_page_private(page, 0);
3794             }
3795         } else {
3796             page = swapin_readahead(entry, GFP_HIGHUSER_MOVABLE,
3797                         vmf);
3798             swapcache = page;
3799         }
3800 
3801         if (!page) {
3802             /*
3803              * Back out if somebody else faulted in this pte
3804              * while we released the pte lock.
3805              */
3806             vmf->pte = pte_offset_map_lock(vma->vm_mm, vmf->pmd,
3807                     vmf->address, &vmf->ptl);
3808             if (likely(pte_same(*vmf->pte, vmf->orig_pte)))
3809                 ret = VM_FAULT_OOM;
3810             goto unlock;
3811         }
3812 
3813         /* Had to read the page from swap area: Major fault */
3814         ret = VM_FAULT_MAJOR;
3815         count_vm_event(PGMAJFAULT);
3816         count_memcg_event_mm(vma->vm_mm, PGMAJFAULT);
3817     } else if (PageHWPoison(page)) {
3818         /*
3819          * hwpoisoned dirty swapcache pages are kept for killing
3820          * owner processes (which may be unknown at hwpoison time)
3821          */
3822         ret = VM_FAULT_HWPOISON;
3823         goto out_release;
3824     }
3825 
3826     locked = lock_page_or_retry(page, vma->vm_mm, vmf->flags);
3827 
3828     if (!locked) {
3829         ret |= VM_FAULT_RETRY;
3830         goto out_release;
3831     }
3832 
3833     if (swapcache) {
3834         /*
3835          * Make sure try_to_free_swap or swapoff did not release the
3836          * swapcache from under us.  The page pin, and pte_same test
3837          * below, are not enough to exclude that.  Even if it is still
3838          * swapcache, we need to check that the page's swap has not
3839          * changed.
3840          */
3841         if (unlikely(!PageSwapCache(page) ||
3842                  page_private(page) != entry.val))
3843             goto out_page;
3844 
3845         /*
3846          * KSM sometimes has to copy on read faults, for example, if
3847          * page->index of !PageKSM() pages would be nonlinear inside the
3848          * anon VMA -- PageKSM() is lost on actual swapout.
3849          */
3850         page = ksm_might_need_to_copy(page, vma, vmf->address);
3851         if (unlikely(!page)) {
3852             ret = VM_FAULT_OOM;
3853             page = swapcache;
3854             goto out_page;
3855         }
3856 
3857         /*
3858          * If we want to map a page that's in the swapcache writable, we
3859          * have to detect via the refcount if we're really the exclusive
3860          * owner. Try removing the extra reference from the local LRU
3861          * pagevecs if required.
3862          */
3863         if ((vmf->flags & FAULT_FLAG_WRITE) && page == swapcache &&
3864             !PageKsm(page) && !PageLRU(page))
3865             lru_add_drain();
3866     }
3867 
3868     cgroup_throttle_swaprate(page, GFP_KERNEL);
3869 
3870     /*
3871      * Back out if somebody else already faulted in this pte.
3872      */
3873     vmf->pte = pte_offset_map_lock(vma->vm_mm, vmf->pmd, vmf->address,
3874             &vmf->ptl);
3875     if (unlikely(!pte_same(*vmf->pte, vmf->orig_pte)))
3876         goto out_nomap;
3877 
3878     if (unlikely(!PageUptodate(page))) {
3879         ret = VM_FAULT_SIGBUS;
3880         goto out_nomap;
3881     }
3882 
3883     /*
3884      * PG_anon_exclusive reuses PG_mappedtodisk for anon pages. A swap pte
3885      * must never point at an anonymous page in the swapcache that is
3886      * PG_anon_exclusive. Sanity check that this holds and especially, that
3887      * no filesystem set PG_mappedtodisk on a page in the swapcache. Sanity
3888      * check after taking the PT lock and making sure that nobody
3889      * concurrently faulted in this page and set PG_anon_exclusive.
3890      */
3891     BUG_ON(!PageAnon(page) && PageMappedToDisk(page));
3892     BUG_ON(PageAnon(page) && PageAnonExclusive(page));
3893 
3894     /*
3895      * Check under PT lock (to protect against concurrent fork() sharing
3896      * the swap entry concurrently) for certainly exclusive pages.
3897      */
3898     if (!PageKsm(page)) {
3899         /*
3900          * Note that pte_swp_exclusive() == false for architectures
3901          * without __HAVE_ARCH_PTE_SWP_EXCLUSIVE.
3902          */
3903         exclusive = pte_swp_exclusive(vmf->orig_pte);
3904         if (page != swapcache) {
3905             /*
3906              * We have a fresh page that is not exposed to the
3907              * swapcache -> certainly exclusive.
3908              */
3909             exclusive = true;
3910         } else if (exclusive && PageWriteback(page) &&
3911               data_race(si->flags & SWP_STABLE_WRITES)) {
3912             /*
3913              * This is tricky: not all swap backends support
3914              * concurrent page modifications while under writeback.
3915              *
3916              * So if we stumble over such a page in the swapcache
3917              * we must not set the page exclusive, otherwise we can
3918              * map it writable without further checks and modify it
3919              * while still under writeback.
3920              *
3921              * For these problematic swap backends, simply drop the
3922              * exclusive marker: this is perfectly fine as we start
3923              * writeback only if we fully unmapped the page and
3924              * there are no unexpected references on the page after
3925              * unmapping succeeded. After fully unmapped, no
3926              * further GUP references (FOLL_GET and FOLL_PIN) can
3927              * appear, so dropping the exclusive marker and mapping
3928              * it only R/O is fine.
3929              */
3930             exclusive = false;
3931         }
3932     }
3933 
3934     /*
3935      * Remove the swap entry and conditionally try to free up the swapcache.
3936      * We're already holding a reference on the page but haven't mapped it
3937      * yet.
3938      */
3939     swap_free(entry);
3940     if (should_try_to_free_swap(page, vma, vmf->flags))
3941         try_to_free_swap(page);
3942 
3943     inc_mm_counter_fast(vma->vm_mm, MM_ANONPAGES);
3944     dec_mm_counter_fast(vma->vm_mm, MM_SWAPENTS);
3945     pte = mk_pte(page, vma->vm_page_prot);
3946 
3947     /*
3948      * Same logic as in do_wp_page(); however, optimize for pages that are
3949      * certainly not shared either because we just allocated them without
3950      * exposing them to the swapcache or because the swap entry indicates
3951      * exclusivity.
3952      */
3953     if (!PageKsm(page) && (exclusive || page_count(page) == 1)) {
3954         if (vmf->flags & FAULT_FLAG_WRITE) {
3955             pte = maybe_mkwrite(pte_mkdirty(pte), vma);
3956             vmf->flags &= ~FAULT_FLAG_WRITE;
3957             ret |= VM_FAULT_WRITE;
3958         }
3959         rmap_flags |= RMAP_EXCLUSIVE;
3960     }
3961     flush_icache_page(vma, page);
3962     if (pte_swp_soft_dirty(vmf->orig_pte))
3963         pte = pte_mksoft_dirty(pte);
3964     if (pte_swp_uffd_wp(vmf->orig_pte)) {
3965         pte = pte_mkuffd_wp(pte);
3966         pte = pte_wrprotect(pte);
3967     }
3968     vmf->orig_pte = pte;
3969 
3970     /* ksm created a completely new copy */
3971     if (unlikely(page != swapcache && swapcache)) {
3972         page_add_new_anon_rmap(page, vma, vmf->address);
3973         lru_cache_add_inactive_or_unevictable(page, vma);
3974     } else {
3975         page_add_anon_rmap(page, vma, vmf->address, rmap_flags);
3976     }
3977 
3978     VM_BUG_ON(!PageAnon(page) || (pte_write(pte) && !PageAnonExclusive(page)));
3979     set_pte_at(vma->vm_mm, vmf->address, vmf->pte, pte);
3980     arch_do_swap_page(vma->vm_mm, vma, vmf->address, pte, vmf->orig_pte);
3981 
3982     unlock_page(page);
3983     if (page != swapcache && swapcache) {
3984         /*
3985          * Hold the lock to avoid the swap entry to be reused
3986          * until we take the PT lock for the pte_same() check
3987          * (to avoid false positives from pte_same). For
3988          * further safety release the lock after the swap_free
3989          * so that the swap count won't change under a
3990          * parallel locked swapcache.
3991          */
3992         unlock_page(swapcache);
3993         put_page(swapcache);
3994     }
3995 
3996     if (vmf->flags & FAULT_FLAG_WRITE) {
3997         ret |= do_wp_page(vmf);
3998         if (ret & VM_FAULT_ERROR)
3999             ret &= VM_FAULT_ERROR;
4000         goto out;
4001     }
4002 
4003     /* No need to invalidate - it was non-present before */
4004     update_mmu_cache(vma, vmf->address, vmf->pte);
4005 unlock:
4006     pte_unmap_unlock(vmf->pte, vmf->ptl);
4007 out:
4008     if (si)
4009         put_swap_device(si);
4010     return ret;
4011 out_nomap:
4012     pte_unmap_unlock(vmf->pte, vmf->ptl);
4013 out_page:
4014     unlock_page(page);
4015 out_release:
4016     put_page(page);
4017     if (page != swapcache && swapcache) {
4018         unlock_page(swapcache);
4019         put_page(swapcache);
4020     }
4021     if (si)
4022         put_swap_device(si);
4023     return ret;
4024 }
4025 
4026 /*
4027  * We enter with non-exclusive mmap_lock (to exclude vma changes,
4028  * but allow concurrent faults), and pte mapped but not yet locked.
4029  * We return with mmap_lock still held, but pte unmapped and unlocked.
4030  */
4031 static vm_fault_t do_anonymous_page(struct vm_fault *vmf)
4032 {
4033     struct vm_area_struct *vma = vmf->vma;
4034     struct page *page;
4035     vm_fault_t ret = 0;
4036     pte_t entry;
4037 
4038     /* File mapping without ->vm_ops ? */
4039     if (vma->vm_flags & VM_SHARED)
4040         return VM_FAULT_SIGBUS;
4041 
4042     /*
4043      * Use pte_alloc() instead of pte_alloc_map().  We can't run
4044      * pte_offset_map() on pmds where a huge pmd might be created
4045      * from a different thread.
4046      *
4047      * pte_alloc_map() is safe to use under mmap_write_lock(mm) or when
4048      * parallel threads are excluded by other means.
4049      *
4050      * Here we only have mmap_read_lock(mm).
4051      */
4052     if (pte_alloc(vma->vm_mm, vmf->pmd))
4053         return VM_FAULT_OOM;
4054 
4055     /* See comment in handle_pte_fault() */
4056     if (unlikely(pmd_trans_unstable(vmf->pmd)))
4057         return 0;
4058 
4059     /* Use the zero-page for reads */
4060     if (!(vmf->flags & FAULT_FLAG_WRITE) &&
4061             !mm_forbids_zeropage(vma->vm_mm)) {
4062         entry = pte_mkspecial(pfn_pte(my_zero_pfn(vmf->address),
4063                         vma->vm_page_prot));
4064         vmf->pte = pte_offset_map_lock(vma->vm_mm, vmf->pmd,
4065                 vmf->address, &vmf->ptl);
4066         if (!pte_none(*vmf->pte)) {
4067             update_mmu_tlb(vma, vmf->address, vmf->pte);
4068             goto unlock;
4069         }
4070         ret = check_stable_address_space(vma->vm_mm);
4071         if (ret)
4072             goto unlock;
4073         /* Deliver the page fault to userland, check inside PT lock */
4074         if (userfaultfd_missing(vma)) {
4075             pte_unmap_unlock(vmf->pte, vmf->ptl);
4076             return handle_userfault(vmf, VM_UFFD_MISSING);
4077         }
4078         goto setpte;
4079     }
4080 
4081     /* Allocate our own private page. */
4082     if (unlikely(anon_vma_prepare(vma)))
4083         goto oom;
4084     page = alloc_zeroed_user_highpage_movable(vma, vmf->address);
4085     if (!page)
4086         goto oom;
4087 
4088     if (mem_cgroup_charge(page_folio(page), vma->vm_mm, GFP_KERNEL))
4089         goto oom_free_page;
4090     cgroup_throttle_swaprate(page, GFP_KERNEL);
4091 
4092     /*
4093      * The memory barrier inside __SetPageUptodate makes sure that
4094      * preceding stores to the page contents become visible before
4095      * the set_pte_at() write.
4096      */
4097     __SetPageUptodate(page);
4098 
4099     entry = mk_pte(page, vma->vm_page_prot);
4100     entry = pte_sw_mkyoung(entry);
4101     if (vma->vm_flags & VM_WRITE)
4102         entry = pte_mkwrite(pte_mkdirty(entry));
4103 
4104     vmf->pte = pte_offset_map_lock(vma->vm_mm, vmf->pmd, vmf->address,
4105             &vmf->ptl);
4106     if (!pte_none(*vmf->pte)) {
4107         update_mmu_cache(vma, vmf->address, vmf->pte);
4108         goto release;
4109     }
4110 
4111     ret = check_stable_address_space(vma->vm_mm);
4112     if (ret)
4113         goto release;
4114 
4115     /* Deliver the page fault to userland, check inside PT lock */
4116     if (userfaultfd_missing(vma)) {
4117         pte_unmap_unlock(vmf->pte, vmf->ptl);
4118         put_page(page);
4119         return handle_userfault(vmf, VM_UFFD_MISSING);
4120     }
4121 
4122     inc_mm_counter_fast(vma->vm_mm, MM_ANONPAGES);
4123     page_add_new_anon_rmap(page, vma, vmf->address);
4124     lru_cache_add_inactive_or_unevictable(page, vma);
4125 setpte:
4126     set_pte_at(vma->vm_mm, vmf->address, vmf->pte, entry);
4127 
4128     /* No need to invalidate - it was non-present before */
4129     update_mmu_cache(vma, vmf->address, vmf->pte);
4130 unlock:
4131     pte_unmap_unlock(vmf->pte, vmf->ptl);
4132     return ret;
4133 release:
4134     put_page(page);
4135     goto unlock;
4136 oom_free_page:
4137     put_page(page);
4138 oom:
4139     return VM_FAULT_OOM;
4140 }
4141 
4142 /*
4143  * The mmap_lock must have been held on entry, and may have been
4144  * released depending on flags and vma->vm_ops->fault() return value.
4145  * See filemap_fault() and __lock_page_retry().
4146  */
4147 static vm_fault_t __do_fault(struct vm_fault *vmf)
4148 {
4149     struct vm_area_struct *vma = vmf->vma;
4150     vm_fault_t ret;
4151 
4152     /*
4153      * Preallocate pte before we take page_lock because this might lead to
4154      * deadlocks for memcg reclaim which waits for pages under writeback:
4155      *              lock_page(A)
4156      *              SetPageWriteback(A)
4157      *              unlock_page(A)
4158      * lock_page(B)
4159      *              lock_page(B)
4160      * pte_alloc_one
4161      *   shrink_page_list
4162      *     wait_on_page_writeback(A)
4163      *              SetPageWriteback(B)
4164      *              unlock_page(B)
4165      *              # flush A, B to clear the writeback
4166      */
4167     if (pmd_none(*vmf->pmd) && !vmf->prealloc_pte) {
4168         vmf->prealloc_pte = pte_alloc_one(vma->vm_mm);
4169         if (!vmf->prealloc_pte)
4170             return VM_FAULT_OOM;
4171     }
4172 
4173     ret = vma->vm_ops->fault(vmf);
4174     if (unlikely(ret & (VM_FAULT_ERROR | VM_FAULT_NOPAGE | VM_FAULT_RETRY |
4175                 VM_FAULT_DONE_COW)))
4176         return ret;
4177 
4178     if (unlikely(PageHWPoison(vmf->page))) {
4179         struct page *page = vmf->page;
4180         vm_fault_t poisonret = VM_FAULT_HWPOISON;
4181         if (ret & VM_FAULT_LOCKED) {
4182             if (page_mapped(page))
4183                 unmap_mapping_pages(page_mapping(page),
4184                             page->index, 1, false);
4185             /* Retry if a clean page was removed from the cache. */
4186             if (invalidate_inode_page(page))
4187                 poisonret = VM_FAULT_NOPAGE;
4188             unlock_page(page);
4189         }
4190         put_page(page);
4191         vmf->page = NULL;
4192         return poisonret;
4193     }
4194 
4195     if (unlikely(!(ret & VM_FAULT_LOCKED)))
4196         lock_page(vmf->page);
4197     else
4198         VM_BUG_ON_PAGE(!PageLocked(vmf->page), vmf->page);
4199 
4200     return ret;
4201 }
4202 
4203 #ifdef CONFIG_TRANSPARENT_HUGEPAGE
4204 static void deposit_prealloc_pte(struct vm_fault *vmf)
4205 {
4206     struct vm_area_struct *vma = vmf->vma;
4207 
4208     pgtable_trans_huge_deposit(vma->vm_mm, vmf->pmd, vmf->prealloc_pte);
4209     /*
4210      * We are going to consume the prealloc table,
4211      * count that as nr_ptes.
4212      */
4213     mm_inc_nr_ptes(vma->vm_mm);
4214     vmf->prealloc_pte = NULL;
4215 }
4216 
4217 vm_fault_t do_set_pmd(struct vm_fault *vmf, struct page *page)
4218 {
4219     struct vm_area_struct *vma = vmf->vma;
4220     bool write = vmf->flags & FAULT_FLAG_WRITE;
4221     unsigned long haddr = vmf->address & HPAGE_PMD_MASK;
4222     pmd_t entry;
4223     int i;
4224     vm_fault_t ret = VM_FAULT_FALLBACK;
4225 
4226     if (!transhuge_vma_suitable(vma, haddr))
4227         return ret;
4228 
4229     page = compound_head(page);
4230     if (compound_order(page) != HPAGE_PMD_ORDER)
4231         return ret;
4232 
4233     /*
4234      * Just backoff if any subpage of a THP is corrupted otherwise
4235      * the corrupted page may mapped by PMD silently to escape the
4236      * check.  This kind of THP just can be PTE mapped.  Access to
4237      * the corrupted subpage should trigger SIGBUS as expected.
4238      */
4239     if (unlikely(PageHasHWPoisoned(page)))
4240         return ret;
4241 
4242     /*
4243      * Archs like ppc64 need additional space to store information
4244      * related to pte entry. Use the preallocated table for that.
4245      */
4246     if (arch_needs_pgtable_deposit() && !vmf->prealloc_pte) {
4247         vmf->prealloc_pte = pte_alloc_one(vma->vm_mm);
4248         if (!vmf->prealloc_pte)
4249             return VM_FAULT_OOM;
4250     }
4251 
4252     vmf->ptl = pmd_lock(vma->vm_mm, vmf->pmd);
4253     if (unlikely(!pmd_none(*vmf->pmd)))
4254         goto out;
4255 
4256     for (i = 0; i < HPAGE_PMD_NR; i++)
4257         flush_icache_page(vma, page + i);
4258 
4259     entry = mk_huge_pmd(page, vma->vm_page_prot);
4260     if (write)
4261         entry = maybe_pmd_mkwrite(pmd_mkdirty(entry), vma);
4262 
4263     add_mm_counter(vma->vm_mm, mm_counter_file(page), HPAGE_PMD_NR);
4264     page_add_file_rmap(page, vma, true);
4265 
4266     /*
4267      * deposit and withdraw with pmd lock held
4268      */
4269     if (arch_needs_pgtable_deposit())
4270         deposit_prealloc_pte(vmf);
4271 
4272     set_pmd_at(vma->vm_mm, haddr, vmf->pmd, entry);
4273 
4274     update_mmu_cache_pmd(vma, haddr, vmf->pmd);
4275 
4276     /* fault is handled */
4277     ret = 0;
4278     count_vm_event(THP_FILE_MAPPED);
4279 out:
4280     spin_unlock(vmf->ptl);
4281     return ret;
4282 }
4283 #else
4284 vm_fault_t do_set_pmd(struct vm_fault *vmf, struct page *page)
4285 {
4286     return VM_FAULT_FALLBACK;
4287 }
4288 #endif
4289 
4290 void do_set_pte(struct vm_fault *vmf, struct page *page, unsigned long addr)
4291 {
4292     struct vm_area_struct *vma = vmf->vma;
4293     bool uffd_wp = pte_marker_uffd_wp(vmf->orig_pte);
4294     bool write = vmf->flags & FAULT_FLAG_WRITE;
4295     bool prefault = vmf->address != addr;
4296     pte_t entry;
4297 
4298     flush_icache_page(vma, page);
4299     entry = mk_pte(page, vma->vm_page_prot);
4300 
4301     if (prefault && arch_wants_old_prefaulted_pte())
4302         entry = pte_mkold(entry);
4303     else
4304         entry = pte_sw_mkyoung(entry);
4305 
4306     if (write)
4307         entry = maybe_mkwrite(pte_mkdirty(entry), vma);
4308     if (unlikely(uffd_wp))
4309         entry = pte_mkuffd_wp(pte_wrprotect(entry));
4310     /* copy-on-write page */
4311     if (write && !(vma->vm_flags & VM_SHARED)) {
4312         inc_mm_counter_fast(vma->vm_mm, MM_ANONPAGES);
4313         page_add_new_anon_rmap(page, vma, addr);
4314         lru_cache_add_inactive_or_unevictable(page, vma);
4315     } else {
4316         inc_mm_counter_fast(vma->vm_mm, mm_counter_file(page));
4317         page_add_file_rmap(page, vma, false);
4318     }
4319     set_pte_at(vma->vm_mm, addr, vmf->pte, entry);
4320 }
4321 
4322 static bool vmf_pte_changed(struct vm_fault *vmf)
4323 {
4324     if (vmf->flags & FAULT_FLAG_ORIG_PTE_VALID)
4325         return !pte_same(*vmf->pte, vmf->orig_pte);
4326 
4327     return !pte_none(*vmf->pte);
4328 }
4329 
4330 /**
4331  * finish_fault - finish page fault once we have prepared the page to fault
4332  *
4333  * @vmf: structure describing the fault
4334  *
4335  * This function handles all that is needed to finish a page fault once the
4336  * page to fault in is prepared. It handles locking of PTEs, inserts PTE for
4337  * given page, adds reverse page mapping, handles memcg charges and LRU
4338  * addition.
4339  *
4340  * The function expects the page to be locked and on success it consumes a
4341  * reference of a page being mapped (for the PTE which maps it).
4342  *
4343  * Return: %0 on success, %VM_FAULT_ code in case of error.
4344  */
4345 vm_fault_t finish_fault(struct vm_fault *vmf)
4346 {
4347     struct vm_area_struct *vma = vmf->vma;
4348     struct page *page;
4349     vm_fault_t ret;
4350 
4351     /* Did we COW the page? */
4352     if ((vmf->flags & FAULT_FLAG_WRITE) && !(vma->vm_flags & VM_SHARED))
4353         page = vmf->cow_page;
4354     else
4355         page = vmf->page;
4356 
4357     /*
4358      * check even for read faults because we might have lost our CoWed
4359      * page
4360      */
4361     if (!(vma->vm_flags & VM_SHARED)) {
4362         ret = check_stable_address_space(vma->vm_mm);
4363         if (ret)
4364             return ret;
4365     }
4366 
4367     if (pmd_none(*vmf->pmd)) {
4368         if (PageTransCompound(page)) {
4369             ret = do_set_pmd(vmf, page);
4370             if (ret != VM_FAULT_FALLBACK)
4371                 return ret;
4372         }
4373 
4374         if (vmf->prealloc_pte)
4375             pmd_install(vma->vm_mm, vmf->pmd, &vmf->prealloc_pte);
4376         else if (unlikely(pte_alloc(vma->vm_mm, vmf->pmd)))
4377             return VM_FAULT_OOM;
4378     }
4379 
4380     /*
4381      * See comment in handle_pte_fault() for how this scenario happens, we
4382      * need to return NOPAGE so that we drop this page.
4383      */
4384     if (pmd_devmap_trans_unstable(vmf->pmd))
4385         return VM_FAULT_NOPAGE;
4386 
4387     vmf->pte = pte_offset_map_lock(vma->vm_mm, vmf->pmd,
4388                       vmf->address, &vmf->ptl);
4389 
4390     /* Re-check under ptl */
4391     if (likely(!vmf_pte_changed(vmf))) {
4392         do_set_pte(vmf, page, vmf->address);
4393 
4394         /* no need to invalidate: a not-present page won't be cached */
4395         update_mmu_cache(vma, vmf->address, vmf->pte);
4396 
4397         ret = 0;
4398     } else {
4399         update_mmu_tlb(vma, vmf->address, vmf->pte);
4400         ret = VM_FAULT_NOPAGE;
4401     }
4402 
4403     pte_unmap_unlock(vmf->pte, vmf->ptl);
4404     return ret;
4405 }
4406 
4407 static unsigned long fault_around_bytes __read_mostly =
4408     rounddown_pow_of_two(65536);
4409 
4410 #ifdef CONFIG_DEBUG_FS
4411 static int fault_around_bytes_get(void *data, u64 *val)
4412 {
4413     *val = fault_around_bytes;
4414     return 0;
4415 }
4416 
4417 /*
4418  * fault_around_bytes must be rounded down to the nearest page order as it's
4419  * what do_fault_around() expects to see.
4420  */
4421 static int fault_around_bytes_set(void *data, u64 val)
4422 {
4423     if (val / PAGE_SIZE > PTRS_PER_PTE)
4424         return -EINVAL;
4425     if (val > PAGE_SIZE)
4426         fault_around_bytes = rounddown_pow_of_two(val);
4427     else
4428         fault_around_bytes = PAGE_SIZE; /* rounddown_pow_of_two(0) is undefined */
4429     return 0;
4430 }
4431 DEFINE_DEBUGFS_ATTRIBUTE(fault_around_bytes_fops,
4432         fault_around_bytes_get, fault_around_bytes_set, "%llu\n");
4433 
4434 static int __init fault_around_debugfs(void)
4435 {
4436     debugfs_create_file_unsafe("fault_around_bytes", 0644, NULL, NULL,
4437                    &fault_around_bytes_fops);
4438     return 0;
4439 }
4440 late_initcall(fault_around_debugfs);
4441 #endif
4442 
4443 /*
4444  * do_fault_around() tries to map few pages around the fault address. The hope
4445  * is that the pages will be needed soon and this will lower the number of
4446  * faults to handle.
4447  *
4448  * It uses vm_ops->map_pages() to map the pages, which skips the page if it's
4449  * not ready to be mapped: not up-to-date, locked, etc.
4450  *
4451  * This function doesn't cross the VMA boundaries, in order to call map_pages()
4452  * only once.
4453  *
4454  * fault_around_bytes defines how many bytes we'll try to map.
4455  * do_fault_around() expects it to be set to a power of two less than or equal
4456  * to PTRS_PER_PTE.
4457  *
4458  * The virtual address of the area that we map is naturally aligned to
4459  * fault_around_bytes rounded down to the machine page size
4460  * (and therefore to page order).  This way it's easier to guarantee
4461  * that we don't cross page table boundaries.
4462  */
4463 static vm_fault_t do_fault_around(struct vm_fault *vmf)
4464 {
4465     unsigned long address = vmf->address, nr_pages, mask;
4466     pgoff_t start_pgoff = vmf->pgoff;
4467     pgoff_t end_pgoff;
4468     int off;
4469 
4470     nr_pages = READ_ONCE(fault_around_bytes) >> PAGE_SHIFT;
4471     mask = ~(nr_pages * PAGE_SIZE - 1) & PAGE_MASK;
4472 
4473     address = max(address & mask, vmf->vma->vm_start);
4474     off = ((vmf->address - address) >> PAGE_SHIFT) & (PTRS_PER_PTE - 1);
4475     start_pgoff -= off;
4476 
4477     /*
4478      *  end_pgoff is either the end of the page table, the end of
4479      *  the vma or nr_pages from start_pgoff, depending what is nearest.
4480      */
4481     end_pgoff = start_pgoff -
4482         ((address >> PAGE_SHIFT) & (PTRS_PER_PTE - 1)) +
4483         PTRS_PER_PTE - 1;
4484     end_pgoff = min3(end_pgoff, vma_pages(vmf->vma) + vmf->vma->vm_pgoff - 1,
4485             start_pgoff + nr_pages - 1);
4486 
4487     if (pmd_none(*vmf->pmd)) {
4488         vmf->prealloc_pte = pte_alloc_one(vmf->vma->vm_mm);
4489         if (!vmf->prealloc_pte)
4490             return VM_FAULT_OOM;
4491     }
4492 
4493     return vmf->vma->vm_ops->map_pages(vmf, start_pgoff, end_pgoff);
4494 }
4495 
4496 /* Return true if we should do read fault-around, false otherwise */
4497 static inline bool should_fault_around(struct vm_fault *vmf)
4498 {
4499     /* No ->map_pages?  No way to fault around... */
4500     if (!vmf->vma->vm_ops->map_pages)
4501         return false;
4502 
4503     if (uffd_disable_fault_around(vmf->vma))
4504         return false;
4505 
4506     return fault_around_bytes >> PAGE_SHIFT > 1;
4507 }
4508 
4509 static vm_fault_t do_read_fault(struct vm_fault *vmf)
4510 {
4511     vm_fault_t ret = 0;
4512 
4513     /*
4514      * Let's call ->map_pages() first and use ->fault() as fallback
4515      * if page by the offset is not ready to be mapped (cold cache or
4516      * something).
4517      */
4518     if (should_fault_around(vmf)) {
4519         ret = do_fault_around(vmf);
4520         if (ret)
4521             return ret;
4522     }
4523 
4524     ret = __do_fault(vmf);
4525     if (unlikely(ret & (VM_FAULT_ERROR | VM_FAULT_NOPAGE | VM_FAULT_RETRY)))
4526         return ret;
4527 
4528     ret |= finish_fault(vmf);
4529     unlock_page(vmf->page);
4530     if (unlikely(ret & (VM_FAULT_ERROR | VM_FAULT_NOPAGE | VM_FAULT_RETRY)))
4531         put_page(vmf->page);
4532     return ret;
4533 }
4534 
4535 static vm_fault_t do_cow_fault(struct vm_fault *vmf)
4536 {
4537     struct vm_area_struct *vma = vmf->vma;
4538     vm_fault_t ret;
4539 
4540     if (unlikely(anon_vma_prepare(vma)))
4541         return VM_FAULT_OOM;
4542 
4543     vmf->cow_page = alloc_page_vma(GFP_HIGHUSER_MOVABLE, vma, vmf->address);
4544     if (!vmf->cow_page)
4545         return VM_FAULT_OOM;
4546 
4547     if (mem_cgroup_charge(page_folio(vmf->cow_page), vma->vm_mm,
4548                 GFP_KERNEL)) {
4549         put_page(vmf->cow_page);
4550         return VM_FAULT_OOM;
4551     }
4552     cgroup_throttle_swaprate(vmf->cow_page, GFP_KERNEL);
4553 
4554     ret = __do_fault(vmf);
4555     if (unlikely(ret & (VM_FAULT_ERROR | VM_FAULT_NOPAGE | VM_FAULT_RETRY)))
4556         goto uncharge_out;
4557     if (ret & VM_FAULT_DONE_COW)
4558         return ret;
4559 
4560     copy_user_highpage(vmf->cow_page, vmf->page, vmf->address, vma);
4561     __SetPageUptodate(vmf->cow_page);
4562 
4563     ret |= finish_fault(vmf);
4564     unlock_page(vmf->page);
4565     put_page(vmf->page);
4566     if (unlikely(ret & (VM_FAULT_ERROR | VM_FAULT_NOPAGE | VM_FAULT_RETRY)))
4567         goto uncharge_out;
4568     return ret;
4569 uncharge_out:
4570     put_page(vmf->cow_page);
4571     return ret;
4572 }
4573 
4574 static vm_fault_t do_shared_fault(struct vm_fault *vmf)
4575 {
4576     struct vm_area_struct *vma = vmf->vma;
4577     vm_fault_t ret, tmp;
4578 
4579     ret = __do_fault(vmf);
4580     if (unlikely(ret & (VM_FAULT_ERROR | VM_FAULT_NOPAGE | VM_FAULT_RETRY)))
4581         return ret;
4582 
4583     /*
4584      * Check if the backing address space wants to know that the page is
4585      * about to become writable
4586      */
4587     if (vma->vm_ops->page_mkwrite) {
4588         unlock_page(vmf->page);
4589         tmp = do_page_mkwrite(vmf);
4590         if (unlikely(!tmp ||
4591                 (tmp & (VM_FAULT_ERROR | VM_FAULT_NOPAGE)))) {
4592             put_page(vmf->page);
4593             return tmp;
4594         }
4595     }
4596 
4597     ret |= finish_fault(vmf);
4598     if (unlikely(ret & (VM_FAULT_ERROR | VM_FAULT_NOPAGE |
4599                     VM_FAULT_RETRY))) {
4600         unlock_page(vmf->page);
4601         put_page(vmf->page);
4602         return ret;
4603     }
4604 
4605     ret |= fault_dirty_shared_page(vmf);
4606     return ret;
4607 }
4608 
4609 /*
4610  * We enter with non-exclusive mmap_lock (to exclude vma changes,
4611  * but allow concurrent faults).
4612  * The mmap_lock may have been released depending on flags and our
4613  * return value.  See filemap_fault() and __folio_lock_or_retry().
4614  * If mmap_lock is released, vma may become invalid (for example
4615  * by other thread calling munmap()).
4616  */
4617 static vm_fault_t do_fault(struct vm_fault *vmf)
4618 {
4619     struct vm_area_struct *vma = vmf->vma;
4620     struct mm_struct *vm_mm = vma->vm_mm;
4621     vm_fault_t ret;
4622 
4623     /*
4624      * The VMA was not fully populated on mmap() or missing VM_DONTEXPAND
4625      */
4626     if (!vma->vm_ops->fault) {
4627         /*
4628          * If we find a migration pmd entry or a none pmd entry, which
4629          * should never happen, return SIGBUS
4630          */
4631         if (unlikely(!pmd_present(*vmf->pmd)))
4632             ret = VM_FAULT_SIGBUS;
4633         else {
4634             vmf->pte = pte_offset_map_lock(vmf->vma->vm_mm,
4635                                vmf->pmd,
4636                                vmf->address,
4637                                &vmf->ptl);
4638             /*
4639              * Make sure this is not a temporary clearing of pte
4640              * by holding ptl and checking again. A R/M/W update
4641              * of pte involves: take ptl, clearing the pte so that
4642              * we don't have concurrent modification by hardware
4643              * followed by an update.
4644              */
4645             if (unlikely(pte_none(*vmf->pte)))
4646                 ret = VM_FAULT_SIGBUS;
4647             else
4648                 ret = VM_FAULT_NOPAGE;
4649 
4650             pte_unmap_unlock(vmf->pte, vmf->ptl);
4651         }
4652     } else if (!(vmf->flags & FAULT_FLAG_WRITE))
4653         ret = do_read_fault(vmf);
4654     else if (!(vma->vm_flags & VM_SHARED))
4655         ret = do_cow_fault(vmf);
4656     else
4657         ret = do_shared_fault(vmf);
4658 
4659     /* preallocated pagetable is unused: free it */
4660     if (vmf->prealloc_pte) {
4661         pte_free(vm_mm, vmf->prealloc_pte);
4662         vmf->prealloc_pte = NULL;
4663     }
4664     return ret;
4665 }
4666 
4667 int numa_migrate_prep(struct page *page, struct vm_area_struct *vma,
4668               unsigned long addr, int page_nid, int *flags)
4669 {
4670     get_page(page);
4671 
4672     count_vm_numa_event(NUMA_HINT_FAULTS);
4673     if (page_nid == numa_node_id()) {
4674         count_vm_numa_event(NUMA_HINT_FAULTS_LOCAL);
4675         *flags |= TNF_FAULT_LOCAL;
4676     }
4677 
4678     return mpol_misplaced(page, vma, addr);
4679 }
4680 
4681 static vm_fault_t do_numa_page(struct vm_fault *vmf)
4682 {
4683     struct vm_area_struct *vma = vmf->vma;
4684     struct page *page = NULL;
4685     int page_nid = NUMA_NO_NODE;
4686     int last_cpupid;
4687     int target_nid;
4688     pte_t pte, old_pte;
4689     bool was_writable = pte_savedwrite(vmf->orig_pte);
4690     int flags = 0;
4691 
4692     /*
4693      * The "pte" at this point cannot be used safely without
4694      * validation through pte_unmap_same(). It's of NUMA type but
4695      * the pfn may be screwed if the read is non atomic.
4696      */
4697     vmf->ptl = pte_lockptr(vma->vm_mm, vmf->pmd);
4698     spin_lock(vmf->ptl);
4699     if (unlikely(!pte_same(*vmf->pte, vmf->orig_pte))) {
4700         pte_unmap_unlock(vmf->pte, vmf->ptl);
4701         goto out;
4702     }
4703 
4704     /* Get the normal PTE  */
4705     old_pte = ptep_get(vmf->pte);
4706     pte = pte_modify(old_pte, vma->vm_page_prot);
4707 
4708     page = vm_normal_page(vma, vmf->address, pte);
4709     if (!page || is_zone_device_page(page))
4710         goto out_map;
4711 
4712     /* TODO: handle PTE-mapped THP */
4713     if (PageCompound(page))
4714         goto out_map;
4715 
4716     /*
4717      * Avoid grouping on RO pages in general. RO pages shouldn't hurt as
4718      * much anyway since they can be in shared cache state. This misses
4719      * the case where a mapping is writable but the process never writes
4720      * to it but pte_write gets cleared during protection updates and
4721      * pte_dirty has unpredictable behaviour between PTE scan updates,
4722      * background writeback, dirty balancing and application behaviour.
4723      */
4724     if (!was_writable)
4725         flags |= TNF_NO_GROUP;
4726 
4727     /*
4728      * Flag if the page is shared between multiple address spaces. This
4729      * is later used when determining whether to group tasks together
4730      */
4731     if (page_mapcount(page) > 1 && (vma->vm_flags & VM_SHARED))
4732         flags |= TNF_SHARED;
4733 
4734     last_cpupid = page_cpupid_last(page);
4735     page_nid = page_to_nid(page);
4736     target_nid = numa_migrate_prep(page, vma, vmf->address, page_nid,
4737             &flags);
4738     if (target_nid == NUMA_NO_NODE) {
4739         put_page(page);
4740         goto out_map;
4741     }
4742     pte_unmap_unlock(vmf->pte, vmf->ptl);
4743 
4744     /* Migrate to the requested node */
4745     if (migrate_misplaced_page(page, vma, target_nid)) {
4746         page_nid = target_nid;
4747         flags |= TNF_MIGRATED;
4748     } else {
4749         flags |= TNF_MIGRATE_FAIL;
4750         vmf->pte = pte_offset_map(vmf->pmd, vmf->address);
4751         spin_lock(vmf->ptl);
4752         if (unlikely(!pte_same(*vmf->pte, vmf->orig_pte))) {
4753             pte_unmap_unlock(vmf->pte, vmf->ptl);
4754             goto out;
4755         }
4756         goto out_map;
4757     }
4758 
4759 out:
4760     if (page_nid != NUMA_NO_NODE)
4761         task_numa_fault(last_cpupid, page_nid, 1, flags);
4762     return 0;
4763 out_map:
4764     /*
4765      * Make it present again, depending on how arch implements
4766      * non-accessible ptes, some can allow access by kernel mode.
4767      */
4768     old_pte = ptep_modify_prot_start(vma, vmf->address, vmf->pte);
4769     pte = pte_modify(old_pte, vma->vm_page_prot);
4770     pte = pte_mkyoung(pte);
4771     if (was_writable)
4772         pte = pte_mkwrite(pte);
4773     ptep_modify_prot_commit(vma, vmf->address, vmf->pte, old_pte, pte);
4774     update_mmu_cache(vma, vmf->address, vmf->pte);
4775     pte_unmap_unlock(vmf->pte, vmf->ptl);
4776     goto out;
4777 }
4778 
4779 static inline vm_fault_t create_huge_pmd(struct vm_fault *vmf)
4780 {
4781     if (vma_is_anonymous(vmf->vma))
4782         return do_huge_pmd_anonymous_page(vmf);
4783     if (vmf->vma->vm_ops->huge_fault)
4784         return vmf->vma->vm_ops->huge_fault(vmf, PE_SIZE_PMD);
4785     return VM_FAULT_FALLBACK;
4786 }
4787 
4788 /* `inline' is required to avoid gcc 4.1.2 build error */
4789 static inline vm_fault_t wp_huge_pmd(struct vm_fault *vmf)
4790 {
4791     const bool unshare = vmf->flags & FAULT_FLAG_UNSHARE;
4792 
4793     if (vma_is_anonymous(vmf->vma)) {
4794         if (likely(!unshare) &&
4795             userfaultfd_huge_pmd_wp(vmf->vma, vmf->orig_pmd))
4796             return handle_userfault(vmf, VM_UFFD_WP);
4797         return do_huge_pmd_wp_page(vmf);
4798     }
4799     if (vmf->vma->vm_ops->huge_fault) {
4800         vm_fault_t ret = vmf->vma->vm_ops->huge_fault(vmf, PE_SIZE_PMD);
4801 
4802         if (!(ret & VM_FAULT_FALLBACK))
4803             return ret;
4804     }
4805 
4806     /* COW or write-notify handled on pte level: split pmd. */
4807     __split_huge_pmd(vmf->vma, vmf->pmd, vmf->address, false, NULL);
4808 
4809     return VM_FAULT_FALLBACK;
4810 }
4811 
4812 static vm_fault_t create_huge_pud(struct vm_fault *vmf)
4813 {
4814 #if defined(CONFIG_TRANSPARENT_HUGEPAGE) &&         \
4815     defined(CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD)
4816     /* No support for anonymous transparent PUD pages yet */
4817     if (vma_is_anonymous(vmf->vma))
4818         return VM_FAULT_FALLBACK;
4819     if (vmf->vma->vm_ops->huge_fault)
4820         return vmf->vma->vm_ops->huge_fault(vmf, PE_SIZE_PUD);
4821 #endif /* CONFIG_TRANSPARENT_HUGEPAGE */
4822     return VM_FAULT_FALLBACK;
4823 }
4824 
4825 static vm_fault_t wp_huge_pud(struct vm_fault *vmf, pud_t orig_pud)
4826 {
4827 #if defined(CONFIG_TRANSPARENT_HUGEPAGE) &&         \
4828     defined(CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD)
4829     /* No support for anonymous transparent PUD pages yet */
4830     if (vma_is_anonymous(vmf->vma))
4831         goto split;
4832     if (vmf->vma->vm_ops->huge_fault) {
4833         vm_fault_t ret = vmf->vma->vm_ops->huge_fault(vmf, PE_SIZE_PUD);
4834 
4835         if (!(ret & VM_FAULT_FALLBACK))
4836             return ret;
4837     }
4838 split:
4839     /* COW or write-notify not handled on PUD level: split pud.*/
4840     __split_huge_pud(vmf->vma, vmf->pud, vmf->address);
4841 #endif /* CONFIG_TRANSPARENT_HUGEPAGE && CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD */
4842     return VM_FAULT_FALLBACK;
4843 }
4844 
4845 /*
4846  * These routines also need to handle stuff like marking pages dirty
4847  * and/or accessed for architectures that don't do it in hardware (most
4848  * RISC architectures).  The early dirtying is also good on the i386.
4849  *
4850  * There is also a hook called "update_mmu_cache()" that architectures
4851  * with external mmu caches can use to update those (ie the Sparc or
4852  * PowerPC hashed page tables that act as extended TLBs).
4853  *
4854  * We enter with non-exclusive mmap_lock (to exclude vma changes, but allow
4855  * concurrent faults).
4856  *
4857  * The mmap_lock may have been released depending on flags and our return value.
4858  * See filemap_fault() and __folio_lock_or_retry().
4859  */
4860 static vm_fault_t handle_pte_fault(struct vm_fault *vmf)
4861 {
4862     pte_t entry;
4863 
4864     if (unlikely(pmd_none(*vmf->pmd))) {
4865         /*
4866          * Leave __pte_alloc() until later: because vm_ops->fault may
4867          * want to allocate huge page, and if we expose page table
4868          * for an instant, it will be difficult to retract from
4869          * concurrent faults and from rmap lookups.
4870          */
4871         vmf->pte = NULL;
4872         vmf->flags &= ~FAULT_FLAG_ORIG_PTE_VALID;
4873     } else {
4874         /*
4875          * If a huge pmd materialized under us just retry later.  Use
4876          * pmd_trans_unstable() via pmd_devmap_trans_unstable() instead
4877          * of pmd_trans_huge() to ensure the pmd didn't become
4878          * pmd_trans_huge under us and then back to pmd_none, as a
4879          * result of MADV_DONTNEED running immediately after a huge pmd
4880          * fault in a different thread of this mm, in turn leading to a
4881          * misleading pmd_trans_huge() retval. All we have to ensure is
4882          * that it is a regular pmd that we can walk with
4883          * pte_offset_map() and we can do that through an atomic read
4884          * in C, which is what pmd_trans_unstable() provides.
4885          */
4886         if (pmd_devmap_trans_unstable(vmf->pmd))
4887             return 0;
4888         /*
4889          * A regular pmd is established and it can't morph into a huge
4890          * pmd from under us anymore at this point because we hold the
4891          * mmap_lock read mode and khugepaged takes it in write mode.
4892          * So now it's safe to run pte_offset_map().
4893          */
4894         vmf->pte = pte_offset_map(vmf->pmd, vmf->address);
4895         vmf->orig_pte = *vmf->pte;
4896         vmf->flags |= FAULT_FLAG_ORIG_PTE_VALID;
4897 
4898         /*
4899          * some architectures can have larger ptes than wordsize,
4900          * e.g.ppc44x-defconfig has CONFIG_PTE_64BIT=y and
4901          * CONFIG_32BIT=y, so READ_ONCE cannot guarantee atomic
4902          * accesses.  The code below just needs a consistent view
4903          * for the ifs and we later double check anyway with the
4904          * ptl lock held. So here a barrier will do.
4905          */
4906         barrier();
4907         if (pte_none(vmf->orig_pte)) {
4908             pte_unmap(vmf->pte);
4909             vmf->pte = NULL;
4910         }
4911     }
4912 
4913     if (!vmf->pte) {
4914         if (vma_is_anonymous(vmf->vma))
4915             return do_anonymous_page(vmf);
4916         else
4917             return do_fault(vmf);
4918     }
4919 
4920     if (!pte_present(vmf->orig_pte))
4921         return do_swap_page(vmf);
4922 
4923     if (pte_protnone(vmf->orig_pte) && vma_is_accessible(vmf->vma))
4924         return do_numa_page(vmf);
4925 
4926     vmf->ptl = pte_lockptr(vmf->vma->vm_mm, vmf->pmd);
4927     spin_lock(vmf->ptl);
4928     entry = vmf->orig_pte;
4929     if (unlikely(!pte_same(*vmf->pte, entry))) {
4930         update_mmu_tlb(vmf->vma, vmf->address, vmf->pte);
4931         goto unlock;
4932     }
4933     if (vmf->flags & (FAULT_FLAG_WRITE|FAULT_FLAG_UNSHARE)) {
4934         if (!pte_write(entry))
4935             return do_wp_page(vmf);
4936         else if (likely(vmf->flags & FAULT_FLAG_WRITE))
4937             entry = pte_mkdirty(entry);
4938     }
4939     entry = pte_mkyoung(entry);
4940     if (ptep_set_access_flags(vmf->vma, vmf->address, vmf->pte, entry,
4941                 vmf->flags & FAULT_FLAG_WRITE)) {
4942         update_mmu_cache(vmf->vma, vmf->address, vmf->pte);
4943     } else {
4944         /* Skip spurious TLB flush for retried page fault */
4945         if (vmf->flags & FAULT_FLAG_TRIED)
4946             goto unlock;
4947         /*
4948          * This is needed only for protection faults but the arch code
4949          * is not yet telling us if this is a protection fault or not.
4950          * This still avoids useless tlb flushes for .text page faults
4951          * with threads.
4952          */
4953         if (vmf->flags & FAULT_FLAG_WRITE)
4954             flush_tlb_fix_spurious_fault(vmf->vma, vmf->address);
4955     }
4956 unlock:
4957     pte_unmap_unlock(vmf->pte, vmf->ptl);
4958     return 0;
4959 }
4960 
4961 /*
4962  * By the time we get here, we already hold the mm semaphore
4963  *
4964  * The mmap_lock may have been released depending on flags and our
4965  * return value.  See filemap_fault() and __folio_lock_or_retry().
4966  */
4967 static vm_fault_t __handle_mm_fault(struct vm_area_struct *vma,
4968         unsigned long address, unsigned int flags)
4969 {
4970     struct vm_fault vmf = {
4971         .vma = vma,
4972         .address = address & PAGE_MASK,
4973         .real_address = address,
4974         .flags = flags,
4975         .pgoff = linear_page_index(vma, address),
4976         .gfp_mask = __get_fault_gfp_mask(vma),
4977     };
4978     struct mm_struct *mm = vma->vm_mm;
4979     unsigned long vm_flags = vma->vm_flags;
4980     pgd_t *pgd;
4981     p4d_t *p4d;
4982     vm_fault_t ret;
4983 
4984     pgd = pgd_offset(mm, address);
4985     p4d = p4d_alloc(mm, pgd, address);
4986     if (!p4d)
4987         return VM_FAULT_OOM;
4988 
4989     vmf.pud = pud_alloc(mm, p4d, address);
4990     if (!vmf.pud)
4991         return VM_FAULT_OOM;
4992 retry_pud:
4993     if (pud_none(*vmf.pud) &&
4994         hugepage_vma_check(vma, vm_flags, false, true)) {
4995         ret = create_huge_pud(&vmf);
4996         if (!(ret & VM_FAULT_FALLBACK))
4997             return ret;
4998     } else {
4999         pud_t orig_pud = *vmf.pud;
5000 
5001         barrier();
5002         if (pud_trans_huge(orig_pud) || pud_devmap(orig_pud)) {
5003 
5004             /*
5005              * TODO once we support anonymous PUDs: NUMA case and
5006              * FAULT_FLAG_UNSHARE handling.
5007              */
5008             if ((flags & FAULT_FLAG_WRITE) && !pud_write(orig_pud)) {
5009                 ret = wp_huge_pud(&vmf, orig_pud);
5010                 if (!(ret & VM_FAULT_FALLBACK))
5011                     return ret;
5012             } else {
5013                 huge_pud_set_accessed(&vmf, orig_pud);
5014                 return 0;
5015             }
5016         }
5017     }
5018 
5019     vmf.pmd = pmd_alloc(mm, vmf.pud, address);
5020     if (!vmf.pmd)
5021         return VM_FAULT_OOM;
5022 
5023     /* Huge pud page fault raced with pmd_alloc? */
5024     if (pud_trans_unstable(vmf.pud))
5025         goto retry_pud;
5026 
5027     if (pmd_none(*vmf.pmd) &&
5028         hugepage_vma_check(vma, vm_flags, false, true)) {
5029         ret = create_huge_pmd(&vmf);
5030         if (!(ret & VM_FAULT_FALLBACK))
5031             return ret;
5032     } else {
5033         vmf.orig_pmd = *vmf.pmd;
5034 
5035         barrier();
5036         if (unlikely(is_swap_pmd(vmf.orig_pmd))) {
5037             VM_BUG_ON(thp_migration_supported() &&
5038                       !is_pmd_migration_entry(vmf.orig_pmd));
5039             if (is_pmd_migration_entry(vmf.orig_pmd))
5040                 pmd_migration_entry_wait(mm, vmf.pmd);
5041             return 0;
5042         }
5043         if (pmd_trans_huge(vmf.orig_pmd) || pmd_devmap(vmf.orig_pmd)) {
5044             if (pmd_protnone(vmf.orig_pmd) && vma_is_accessible(vma))
5045                 return do_huge_pmd_numa_page(&vmf);
5046 
5047             if ((flags & (FAULT_FLAG_WRITE|FAULT_FLAG_UNSHARE)) &&
5048                 !pmd_write(vmf.orig_pmd)) {
5049                 ret = wp_huge_pmd(&vmf);
5050                 if (!(ret & VM_FAULT_FALLBACK))
5051                     return ret;
5052             } else {
5053                 huge_pmd_set_accessed(&vmf);
5054                 return 0;
5055             }
5056         }
5057     }
5058 
5059     return handle_pte_fault(&vmf);
5060 }
5061 
5062 /**
5063  * mm_account_fault - Do page fault accounting
5064  *
5065  * @regs: the pt_regs struct pointer.  When set to NULL, will skip accounting
5066  *        of perf event counters, but we'll still do the per-task accounting to
5067  *        the task who triggered this page fault.
5068  * @address: the faulted address.
5069  * @flags: the fault flags.
5070  * @ret: the fault retcode.
5071  *
5072  * This will take care of most of the page fault accounting.  Meanwhile, it
5073  * will also include the PERF_COUNT_SW_PAGE_FAULTS_[MAJ|MIN] perf counter
5074  * updates.  However, note that the handling of PERF_COUNT_SW_PAGE_FAULTS should
5075  * still be in per-arch page fault handlers at the entry of page fault.
5076  */
5077 static inline void mm_account_fault(struct pt_regs *regs,
5078                     unsigned long address, unsigned int flags,
5079                     vm_fault_t ret)
5080 {
5081     bool major;
5082 
5083     /*
5084      * We don't do accounting for some specific faults:
5085      *
5086      * - Unsuccessful faults (e.g. when the address wasn't valid).  That
5087      *   includes arch_vma_access_permitted() failing before reaching here.
5088      *   So this is not a "this many hardware page faults" counter.  We
5089      *   should use the hw profiling for that.
5090      *
5091      * - Incomplete faults (VM_FAULT_RETRY).  They will only be counted
5092      *   once they're completed.
5093      */
5094     if (ret & (VM_FAULT_ERROR | VM_FAULT_RETRY))
5095         return;
5096 
5097     /*
5098      * We define the fault as a major fault when the final successful fault
5099      * is VM_FAULT_MAJOR, or if it retried (which implies that we couldn't
5100      * handle it immediately previously).
5101      */
5102     major = (ret & VM_FAULT_MAJOR) || (flags & FAULT_FLAG_TRIED);
5103 
5104     if (major)
5105         current->maj_flt++;
5106     else
5107         current->min_flt++;
5108 
5109     /*
5110      * If the fault is done for GUP, regs will be NULL.  We only do the
5111      * accounting for the per thread fault counters who triggered the
5112      * fault, and we skip the perf event updates.
5113      */
5114     if (!regs)
5115         return;
5116 
5117     if (major)
5118         perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS_MAJ, 1, regs, address);
5119     else
5120         perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS_MIN, 1, regs, address);
5121 }
5122 
5123 /*
5124  * By the time we get here, we already hold the mm semaphore
5125  *
5126  * The mmap_lock may have been released depending on flags and our
5127  * return value.  See filemap_fault() and __folio_lock_or_retry().
5128  */
5129 vm_fault_t handle_mm_fault(struct vm_area_struct *vma, unsigned long address,
5130                unsigned int flags, struct pt_regs *regs)
5131 {
5132     vm_fault_t ret;
5133 
5134     __set_current_state(TASK_RUNNING);
5135 
5136     count_vm_event(PGFAULT);
5137     count_memcg_event_mm(vma->vm_mm, PGFAULT);
5138 
5139     /* do counter updates before entering really critical section. */
5140     check_sync_rss_stat(current);
5141 
5142     if (!arch_vma_access_permitted(vma, flags & FAULT_FLAG_WRITE,
5143                         flags & FAULT_FLAG_INSTRUCTION,
5144                         flags & FAULT_FLAG_REMOTE))
5145         return VM_FAULT_SIGSEGV;
5146 
5147     /*
5148      * Enable the memcg OOM handling for faults triggered in user
5149      * space.  Kernel faults are handled more gracefully.
5150      */
5151     if (flags & FAULT_FLAG_USER)
5152         mem_cgroup_enter_user_fault();
5153 
5154     if (unlikely(is_vm_hugetlb_page(vma)))
5155         ret = hugetlb_fault(vma->vm_mm, vma, address, flags);
5156     else
5157         ret = __handle_mm_fault(vma, address, flags);
5158 
5159     if (flags & FAULT_FLAG_USER) {
5160         mem_cgroup_exit_user_fault();
5161         /*
5162          * The task may have entered a memcg OOM situation but
5163          * if the allocation error was handled gracefully (no
5164          * VM_FAULT_OOM), there is no need to kill anything.
5165          * Just clean up the OOM state peacefully.
5166          */
5167         if (task_in_memcg_oom(current) && !(ret & VM_FAULT_OOM))
5168             mem_cgroup_oom_synchronize(false);
5169     }
5170 
5171     mm_account_fault(regs, address, flags, ret);
5172 
5173     return ret;
5174 }
5175 EXPORT_SYMBOL_GPL(handle_mm_fault);
5176 
5177 #ifndef __PAGETABLE_P4D_FOLDED
5178 /*
5179  * Allocate p4d page table.
5180  * We've already handled the fast-path in-line.
5181  */
5182 int __p4d_alloc(struct mm_struct *mm, pgd_t *pgd, unsigned long address)
5183 {
5184     p4d_t *new = p4d_alloc_one(mm, address);
5185     if (!new)
5186         return -ENOMEM;
5187 
5188     spin_lock(&mm->page_table_lock);
5189     if (pgd_present(*pgd)) {    /* Another has populated it */
5190         p4d_free(mm, new);
5191     } else {
5192         smp_wmb(); /* See comment in pmd_install() */
5193         pgd_populate(mm, pgd, new);
5194     }
5195     spin_unlock(&mm->page_table_lock);
5196     return 0;
5197 }
5198 #endif /* __PAGETABLE_P4D_FOLDED */
5199 
5200 #ifndef __PAGETABLE_PUD_FOLDED
5201 /*
5202  * Allocate page upper directory.
5203  * We've already handled the fast-path in-line.
5204  */
5205 int __pud_alloc(struct mm_struct *mm, p4d_t *p4d, unsigned long address)
5206 {
5207     pud_t *new = pud_alloc_one(mm, address);
5208     if (!new)
5209         return -ENOMEM;
5210 
5211     spin_lock(&mm->page_table_lock);
5212     if (!p4d_present(*p4d)) {
5213         mm_inc_nr_puds(mm);
5214         smp_wmb(); /* See comment in pmd_install() */
5215         p4d_populate(mm, p4d, new);
5216     } else  /* Another has populated it */
5217         pud_free(mm, new);
5218     spin_unlock(&mm->page_table_lock);
5219     return 0;
5220 }
5221 #endif /* __PAGETABLE_PUD_FOLDED */
5222 
5223 #ifndef __PAGETABLE_PMD_FOLDED
5224 /*
5225  * Allocate page middle directory.
5226  * We've already handled the fast-path in-line.
5227  */
5228 int __pmd_alloc(struct mm_struct *mm, pud_t *pud, unsigned long address)
5229 {
5230     spinlock_t *ptl;
5231     pmd_t *new = pmd_alloc_one(mm, address);
5232     if (!new)
5233         return -ENOMEM;
5234 
5235     ptl = pud_lock(mm, pud);
5236     if (!pud_present(*pud)) {
5237         mm_inc_nr_pmds(mm);
5238         smp_wmb(); /* See comment in pmd_install() */
5239         pud_populate(mm, pud, new);
5240     } else {    /* Another has populated it */
5241         pmd_free(mm, new);
5242     }
5243     spin_unlock(ptl);
5244     return 0;
5245 }
5246 #endif /* __PAGETABLE_PMD_FOLDED */
5247 
5248 /**
5249  * follow_pte - look up PTE at a user virtual address
5250  * @mm: the mm_struct of the target address space
5251  * @address: user virtual address
5252  * @ptepp: location to store found PTE
5253  * @ptlp: location to store the lock for the PTE
5254  *
5255  * On a successful return, the pointer to the PTE is stored in @ptepp;
5256  * the corresponding lock is taken and its location is stored in @ptlp.
5257  * The contents of the PTE are only stable until @ptlp is released;
5258  * any further use, if any, must be protected against invalidation
5259  * with MMU notifiers.
5260  *
5261  * Only IO mappings and raw PFN mappings are allowed.  The mmap semaphore
5262  * should be taken for read.
5263  *
5264  * KVM uses this function.  While it is arguably less bad than ``follow_pfn``,
5265  * it is not a good general-purpose API.
5266  *
5267  * Return: zero on success, -ve otherwise.
5268  */
5269 int follow_pte(struct mm_struct *mm, unsigned long address,
5270            pte_t **ptepp, spinlock_t **ptlp)
5271 {
5272     pgd_t *pgd;
5273     p4d_t *p4d;
5274     pud_t *pud;
5275     pmd_t *pmd;
5276     pte_t *ptep;
5277 
5278     pgd = pgd_offset(mm, address);
5279     if (pgd_none(*pgd) || unlikely(pgd_bad(*pgd)))
5280         goto out;
5281 
5282     p4d = p4d_offset(pgd, address);
5283     if (p4d_none(*p4d) || unlikely(p4d_bad(*p4d)))
5284         goto out;
5285 
5286     pud = pud_offset(p4d, address);
5287     if (pud_none(*pud) || unlikely(pud_bad(*pud)))
5288         goto out;
5289 
5290     pmd = pmd_offset(pud, address);
5291     VM_BUG_ON(pmd_trans_huge(*pmd));
5292 
5293     if (pmd_none(*pmd) || unlikely(pmd_bad(*pmd)))
5294         goto out;
5295 
5296     ptep = pte_offset_map_lock(mm, pmd, address, ptlp);
5297     if (!pte_present(*ptep))
5298         goto unlock;
5299     *ptepp = ptep;
5300     return 0;
5301 unlock:
5302     pte_unmap_unlock(ptep, *ptlp);
5303 out:
5304     return -EINVAL;
5305 }
5306 EXPORT_SYMBOL_GPL(follow_pte);
5307 
5308 /**
5309  * follow_pfn - look up PFN at a user virtual address
5310  * @vma: memory mapping
5311  * @address: user virtual address
5312  * @pfn: location to store found PFN
5313  *
5314  * Only IO mappings and raw PFN mappings are allowed.
5315  *
5316  * This function does not allow the caller to read the permissions
5317  * of the PTE.  Do not use it.
5318  *
5319  * Return: zero and the pfn at @pfn on success, -ve otherwise.
5320  */
5321 int follow_pfn(struct vm_area_struct *vma, unsigned long address,
5322     unsigned long *pfn)
5323 {
5324     int ret = -EINVAL;
5325     spinlock_t *ptl;
5326     pte_t *ptep;
5327 
5328     if (!(vma->vm_flags & (VM_IO | VM_PFNMAP)))
5329         return ret;
5330 
5331     ret = follow_pte(vma->vm_mm, address, &ptep, &ptl);
5332     if (ret)
5333         return ret;
5334     *pfn = pte_pfn(*ptep);
5335     pte_unmap_unlock(ptep, ptl);
5336     return 0;
5337 }
5338 EXPORT_SYMBOL(follow_pfn);
5339 
5340 #ifdef CONFIG_HAVE_IOREMAP_PROT
5341 int follow_phys(struct vm_area_struct *vma,
5342         unsigned long address, unsigned int flags,
5343         unsigned long *prot, resource_size_t *phys)
5344 {
5345     int ret = -EINVAL;
5346     pte_t *ptep, pte;
5347     spinlock_t *ptl;
5348 
5349     if (!(vma->vm_flags & (VM_IO | VM_PFNMAP)))
5350         goto out;
5351 
5352     if (follow_pte(vma->vm_mm, address, &ptep, &ptl))
5353         goto out;
5354     pte = *ptep;
5355 
5356     if ((flags & FOLL_WRITE) && !pte_write(pte))
5357         goto unlock;
5358 
5359     *prot = pgprot_val(pte_pgprot(pte));
5360     *phys = (resource_size_t)pte_pfn(pte) << PAGE_SHIFT;
5361 
5362     ret = 0;
5363 unlock:
5364     pte_unmap_unlock(ptep, ptl);
5365 out:
5366     return ret;
5367 }
5368 
5369 /**
5370  * generic_access_phys - generic implementation for iomem mmap access
5371  * @vma: the vma to access
5372  * @addr: userspace address, not relative offset within @vma
5373  * @buf: buffer to read/write
5374  * @len: length of transfer
5375  * @write: set to FOLL_WRITE when writing, otherwise reading
5376  *
5377  * This is a generic implementation for &vm_operations_struct.access for an
5378  * iomem mapping. This callback is used by access_process_vm() when the @vma is
5379  * not page based.
5380  */
5381 int generic_access_phys(struct vm_area_struct *vma, unsigned long addr,
5382             void *buf, int len, int write)
5383 {
5384     resource_size_t phys_addr;
5385     unsigned long prot = 0;
5386     void __iomem *maddr;
5387     pte_t *ptep, pte;
5388     spinlock_t *ptl;
5389     int offset = offset_in_page(addr);
5390     int ret = -EINVAL;
5391 
5392     if (!(vma->vm_flags & (VM_IO | VM_PFNMAP)))
5393         return -EINVAL;
5394 
5395 retry:
5396     if (follow_pte(vma->vm_mm, addr, &ptep, &ptl))
5397         return -EINVAL;
5398     pte = *ptep;
5399     pte_unmap_unlock(ptep, ptl);
5400 
5401     prot = pgprot_val(pte_pgprot(pte));
5402     phys_addr = (resource_size_t)pte_pfn(pte) << PAGE_SHIFT;
5403 
5404     if ((write & FOLL_WRITE) && !pte_write(pte))
5405         return -EINVAL;
5406 
5407     maddr = ioremap_prot(phys_addr, PAGE_ALIGN(len + offset), prot);
5408     if (!maddr)
5409         return -ENOMEM;
5410 
5411     if (follow_pte(vma->vm_mm, addr, &ptep, &ptl))
5412         goto out_unmap;
5413 
5414     if (!pte_same(pte, *ptep)) {
5415         pte_unmap_unlock(ptep, ptl);
5416         iounmap(maddr);
5417 
5418         goto retry;
5419     }
5420 
5421     if (write)
5422         memcpy_toio(maddr + offset, buf, len);
5423     else
5424         memcpy_fromio(buf, maddr + offset, len);
5425     ret = len;
5426     pte_unmap_unlock(ptep, ptl);
5427 out_unmap:
5428     iounmap(maddr);
5429 
5430     return ret;
5431 }
5432 EXPORT_SYMBOL_GPL(generic_access_phys);
5433 #endif
5434 
5435 /*
5436  * Access another process' address space as given in mm.
5437  */
5438 int __access_remote_vm(struct mm_struct *mm, unsigned long addr, void *buf,
5439                int len, unsigned int gup_flags)
5440 {
5441     struct vm_area_struct *vma;
5442     void *old_buf = buf;
5443     int write = gup_flags & FOLL_WRITE;
5444 
5445     if (mmap_read_lock_killable(mm))
5446         return 0;
5447 
5448     /* ignore errors, just check how much was successfully transferred */
5449     while (len) {
5450         int bytes, ret, offset;
5451         void *maddr;
5452         struct page *page = NULL;
5453 
5454         ret = get_user_pages_remote(mm, addr, 1,
5455                 gup_flags, &page, &vma, NULL);
5456         if (ret <= 0) {
5457 #ifndef CONFIG_HAVE_IOREMAP_PROT
5458             break;
5459 #else
5460             /*
5461              * Check if this is a VM_IO | VM_PFNMAP VMA, which
5462              * we can access using slightly different code.
5463              */
5464             vma = vma_lookup(mm, addr);
5465             if (!vma)
5466                 break;
5467             if (vma->vm_ops && vma->vm_ops->access)
5468                 ret = vma->vm_ops->access(vma, addr, buf,
5469                               len, write);
5470             if (ret <= 0)
5471                 break;
5472             bytes = ret;
5473 #endif
5474         } else {
5475             bytes = len;
5476             offset = addr & (PAGE_SIZE-1);
5477             if (bytes > PAGE_SIZE-offset)
5478                 bytes = PAGE_SIZE-offset;
5479 
5480             maddr = kmap(page);
5481             if (write) {
5482                 copy_to_user_page(vma, page, addr,
5483                           maddr + offset, buf, bytes);
5484                 set_page_dirty_lock(page);
5485             } else {
5486                 copy_from_user_page(vma, page, addr,
5487                             buf, maddr + offset, bytes);
5488             }
5489             kunmap(page);
5490             put_page(page);
5491         }
5492         len -= bytes;
5493         buf += bytes;
5494         addr += bytes;
5495     }
5496     mmap_read_unlock(mm);
5497 
5498     return buf - old_buf;
5499 }
5500 
5501 /**
5502  * access_remote_vm - access another process' address space
5503  * @mm:     the mm_struct of the target address space
5504  * @addr:   start address to access
5505  * @buf:    source or destination buffer
5506  * @len:    number of bytes to transfer
5507  * @gup_flags:  flags modifying lookup behaviour
5508  *
5509  * The caller must hold a reference on @mm.
5510  *
5511  * Return: number of bytes copied from source to destination.
5512  */
5513 int access_remote_vm(struct mm_struct *mm, unsigned long addr,
5514         void *buf, int len, unsigned int gup_flags)
5515 {
5516     return __access_remote_vm(mm, addr, buf, len, gup_flags);
5517 }
5518 
5519 /*
5520  * Access another process' address space.
5521  * Source/target buffer must be kernel space,
5522  * Do not walk the page table directly, use get_user_pages
5523  */
5524 int access_process_vm(struct task_struct *tsk, unsigned long addr,
5525         void *buf, int len, unsigned int gup_flags)
5526 {
5527     struct mm_struct *mm;
5528     int ret;
5529 
5530     mm = get_task_mm(tsk);
5531     if (!mm)
5532         return 0;
5533 
5534     ret = __access_remote_vm(mm, addr, buf, len, gup_flags);
5535 
5536     mmput(mm);
5537 
5538     return ret;
5539 }
5540 EXPORT_SYMBOL_GPL(access_process_vm);
5541 
5542 /*
5543  * Print the name of a VMA.
5544  */
5545 void print_vma_addr(char *prefix, unsigned long ip)
5546 {
5547     struct mm_struct *mm = current->mm;
5548     struct vm_area_struct *vma;
5549 
5550     /*
5551      * we might be running from an atomic context so we cannot sleep
5552      */
5553     if (!mmap_read_trylock(mm))
5554         return;
5555 
5556     vma = find_vma(mm, ip);
5557     if (vma && vma->vm_file) {
5558         struct file *f = vma->vm_file;
5559         char *buf = (char *)__get_free_page(GFP_NOWAIT);
5560         if (buf) {
5561             char *p;
5562 
5563             p = file_path(f, buf, PAGE_SIZE);
5564             if (IS_ERR(p))
5565                 p = "?";
5566             printk("%s%s[%lx+%lx]", prefix, kbasename(p),
5567                     vma->vm_start,
5568                     vma->vm_end - vma->vm_start);
5569             free_page((unsigned long)buf);
5570         }
5571     }
5572     mmap_read_unlock(mm);
5573 }
5574 
5575 #if defined(CONFIG_PROVE_LOCKING) || defined(CONFIG_DEBUG_ATOMIC_SLEEP)
5576 void __might_fault(const char *file, int line)
5577 {
5578     if (pagefault_disabled())
5579         return;
5580     __might_sleep(file, line);
5581 #if defined(CONFIG_DEBUG_ATOMIC_SLEEP)
5582     if (current->mm)
5583         might_lock_read(&current->mm->mmap_lock);
5584 #endif
5585 }
5586 EXPORT_SYMBOL(__might_fault);
5587 #endif
5588 
5589 #if defined(CONFIG_TRANSPARENT_HUGEPAGE) || defined(CONFIG_HUGETLBFS)
5590 /*
5591  * Process all subpages of the specified huge page with the specified
5592  * operation.  The target subpage will be processed last to keep its
5593  * cache lines hot.
5594  */
5595 static inline void process_huge_page(
5596     unsigned long addr_hint, unsigned int pages_per_huge_page,
5597     void (*process_subpage)(unsigned long addr, int idx, void *arg),
5598     void *arg)
5599 {
5600     int i, n, base, l;
5601     unsigned long addr = addr_hint &
5602         ~(((unsigned long)pages_per_huge_page << PAGE_SHIFT) - 1);
5603 
5604     /* Process target subpage last to keep its cache lines hot */
5605     might_sleep();
5606     n = (addr_hint - addr) / PAGE_SIZE;
5607     if (2 * n <= pages_per_huge_page) {
5608         /* If target subpage in first half of huge page */
5609         base = 0;
5610         l = n;
5611         /* Process subpages at the end of huge page */
5612         for (i = pages_per_huge_page - 1; i >= 2 * n; i--) {
5613             cond_resched();
5614             process_subpage(addr + i * PAGE_SIZE, i, arg);
5615         }
5616     } else {
5617         /* If target subpage in second half of huge page */
5618         base = pages_per_huge_page - 2 * (pages_per_huge_page - n);
5619         l = pages_per_huge_page - n;
5620         /* Process subpages at the begin of huge page */
5621         for (i = 0; i < base; i++) {
5622             cond_resched();
5623             process_subpage(addr + i * PAGE_SIZE, i, arg);
5624         }
5625     }
5626     /*
5627      * Process remaining subpages in left-right-left-right pattern
5628      * towards the target subpage
5629      */
5630     for (i = 0; i < l; i++) {
5631         int left_idx = base + i;
5632         int right_idx = base + 2 * l - 1 - i;
5633 
5634         cond_resched();
5635         process_subpage(addr + left_idx * PAGE_SIZE, left_idx, arg);
5636         cond_resched();
5637         process_subpage(addr + right_idx * PAGE_SIZE, right_idx, arg);
5638     }
5639 }
5640 
5641 static void clear_gigantic_page(struct page *page,
5642                 unsigned long addr,
5643                 unsigned int pages_per_huge_page)
5644 {
5645     int i;
5646     struct page *p = page;
5647 
5648     might_sleep();
5649     for (i = 0; i < pages_per_huge_page;
5650          i++, p = mem_map_next(p, page, i)) {
5651         cond_resched();
5652         clear_user_highpage(p, addr + i * PAGE_SIZE);
5653     }
5654 }
5655 
5656 static void clear_subpage(unsigned long addr, int idx, void *arg)
5657 {
5658     struct page *page = arg;
5659 
5660     clear_user_highpage(page + idx, addr);
5661 }
5662 
5663 void clear_huge_page(struct page *page,
5664              unsigned long addr_hint, unsigned int pages_per_huge_page)
5665 {
5666     unsigned long addr = addr_hint &
5667         ~(((unsigned long)pages_per_huge_page << PAGE_SHIFT) - 1);
5668 
5669     if (unlikely(pages_per_huge_page > MAX_ORDER_NR_PAGES)) {
5670         clear_gigantic_page(page, addr, pages_per_huge_page);
5671         return;
5672     }
5673 
5674     process_huge_page(addr_hint, pages_per_huge_page, clear_subpage, page);
5675 }
5676 
5677 static void copy_user_gigantic_page(struct page *dst, struct page *src,
5678                     unsigned long addr,
5679                     struct vm_area_struct *vma,
5680                     unsigned int pages_per_huge_page)
5681 {
5682     int i;
5683     struct page *dst_base = dst;
5684     struct page *src_base = src;
5685 
5686     for (i = 0; i < pages_per_huge_page; ) {
5687         cond_resched();
5688         copy_user_highpage(dst, src, addr + i*PAGE_SIZE, vma);
5689 
5690         i++;
5691         dst = mem_map_next(dst, dst_base, i);
5692         src = mem_map_next(src, src_base, i);
5693     }
5694 }
5695 
5696 struct copy_subpage_arg {
5697     struct page *dst;
5698     struct page *src;
5699     struct vm_area_struct *vma;
5700 };
5701 
5702 static void copy_subpage(unsigned long addr, int idx, void *arg)
5703 {
5704     struct copy_subpage_arg *copy_arg = arg;
5705 
5706     copy_user_highpage(copy_arg->dst + idx, copy_arg->src + idx,
5707                addr, copy_arg->vma);
5708 }
5709 
5710 void copy_user_huge_page(struct page *dst, struct page *src,
5711              unsigned long addr_hint, struct vm_area_struct *vma,
5712              unsigned int pages_per_huge_page)
5713 {
5714     unsigned long addr = addr_hint &
5715         ~(((unsigned long)pages_per_huge_page << PAGE_SHIFT) - 1);
5716     struct copy_subpage_arg arg = {
5717         .dst = dst,
5718         .src = src,
5719         .vma = vma,
5720     };
5721 
5722     if (unlikely(pages_per_huge_page > MAX_ORDER_NR_PAGES)) {
5723         copy_user_gigantic_page(dst, src, addr, vma,
5724                     pages_per_huge_page);
5725         return;
5726     }
5727 
5728     process_huge_page(addr_hint, pages_per_huge_page, copy_subpage, &arg);
5729 }
5730 
5731 long copy_huge_page_from_user(struct page *dst_page,
5732                 const void __user *usr_src,
5733                 unsigned int pages_per_huge_page,
5734                 bool allow_pagefault)
5735 {
5736     void *page_kaddr;
5737     unsigned long i, rc = 0;
5738     unsigned long ret_val = pages_per_huge_page * PAGE_SIZE;
5739     struct page *subpage = dst_page;
5740 
5741     for (i = 0; i < pages_per_huge_page;
5742          i++, subpage = mem_map_next(subpage, dst_page, i)) {
5743         if (allow_pagefault)
5744             page_kaddr = kmap(subpage);
5745         else
5746             page_kaddr = kmap_atomic(subpage);
5747         rc = copy_from_user(page_kaddr,
5748                 usr_src + i * PAGE_SIZE, PAGE_SIZE);
5749         if (allow_pagefault)
5750             kunmap(subpage);
5751         else
5752             kunmap_atomic(page_kaddr);
5753 
5754         ret_val -= (PAGE_SIZE - rc);
5755         if (rc)
5756             break;
5757 
5758         flush_dcache_page(subpage);
5759 
5760         cond_resched();
5761     }
5762     return ret_val;
5763 }
5764 #endif /* CONFIG_TRANSPARENT_HUGEPAGE || CONFIG_HUGETLBFS */
5765 
5766 #if USE_SPLIT_PTE_PTLOCKS && ALLOC_SPLIT_PTLOCKS
5767 
5768 static struct kmem_cache *page_ptl_cachep;
5769 
5770 void __init ptlock_cache_init(void)
5771 {
5772     page_ptl_cachep = kmem_cache_create("page->ptl", sizeof(spinlock_t), 0,
5773             SLAB_PANIC, NULL);
5774 }
5775 
5776 bool ptlock_alloc(struct page *page)
5777 {
5778     spinlock_t *ptl;
5779 
5780     ptl = kmem_cache_alloc(page_ptl_cachep, GFP_KERNEL);
5781     if (!ptl)
5782         return false;
5783     page->ptl = ptl;
5784     return true;
5785 }
5786 
5787 void ptlock_free(struct page *page)
5788 {
5789     kmem_cache_free(page_ptl_cachep, page->ptl);
5790 }
5791 #endif