Merge tag 'i2c-host-fixes-6.12-rc1' of git://git.kernel.org/pub/scm/linux/kernel...
[linux.git] / mm / memory.c
1
2 // SPDX-License-Identifier: GPL-2.0-only
3 /*
4  *  linux/mm/memory.c
5  *
6  *  Copyright (C) 1991, 1992, 1993, 1994  Linus Torvalds
7  */
8
9 /*
10  * demand-loading started 01.12.91 - seems it is high on the list of
11  * things wanted, and it should be easy to implement. - Linus
12  */
13
14 /*
15  * Ok, demand-loading was easy, shared pages a little bit tricker. Shared
16  * pages started 02.12.91, seems to work. - Linus.
17  *
18  * Tested sharing by executing about 30 /bin/sh: under the old kernel it
19  * would have taken more than the 6M I have free, but it worked well as
20  * far as I could see.
21  *
22  * Also corrected some "invalidate()"s - I wasn't doing enough of them.
23  */
24
25 /*
26  * Real VM (paging to/from disk) started 18.12.91. Much more work and
27  * thought has to go into this. Oh, well..
28  * 19.12.91  -  works, somewhat. Sometimes I get faults, don't know why.
29  *              Found it. Everything seems to work now.
30  * 20.12.91  -  Ok, making the swap-device changeable like the root.
31  */
32
33 /*
34  * 05.04.94  -  Multi-page memory management added for v1.1.
35  *              Idea by Alex Bligh (alex@cconcepts.co.uk)
36  *
37  * 16.07.99  -  Support of BIGMEM added by Gerhard Wichert, Siemens AG
38  *              (Gerhard.Wichert@pdb.siemens.de)
39  *
40  * Aug/Sep 2004 Changed to four level page tables (Andi Kleen)
41  */
42
43 #include <linux/kernel_stat.h>
44 #include <linux/mm.h>
45 #include <linux/mm_inline.h>
46 #include <linux/sched/mm.h>
47 #include <linux/sched/coredump.h>
48 #include <linux/sched/numa_balancing.h>
49 #include <linux/sched/task.h>
50 #include <linux/hugetlb.h>
51 #include <linux/mman.h>
52 #include <linux/swap.h>
53 #include <linux/highmem.h>
54 #include <linux/pagemap.h>
55 #include <linux/memremap.h>
56 #include <linux/kmsan.h>
57 #include <linux/ksm.h>
58 #include <linux/rmap.h>
59 #include <linux/export.h>
60 #include <linux/delayacct.h>
61 #include <linux/init.h>
62 #include <linux/pfn_t.h>
63 #include <linux/writeback.h>
64 #include <linux/memcontrol.h>
65 #include <linux/mmu_notifier.h>
66 #include <linux/swapops.h>
67 #include <linux/elf.h>
68 #include <linux/gfp.h>
69 #include <linux/migrate.h>
70 #include <linux/string.h>
71 #include <linux/memory-tiers.h>
72 #include <linux/debugfs.h>
73 #include <linux/userfaultfd_k.h>
74 #include <linux/dax.h>
75 #include <linux/oom.h>
76 #include <linux/numa.h>
77 #include <linux/perf_event.h>
78 #include <linux/ptrace.h>
79 #include <linux/vmalloc.h>
80 #include <linux/sched/sysctl.h>
81
82 #include <trace/events/kmem.h>
83
84 #include <asm/io.h>
85 #include <asm/mmu_context.h>
86 #include <asm/pgalloc.h>
87 #include <linux/uaccess.h>
88 #include <asm/tlb.h>
89 #include <asm/tlbflush.h>
90
91 #include "pgalloc-track.h"
92 #include "internal.h"
93 #include "swap.h"
94
95 #if defined(LAST_CPUPID_NOT_IN_PAGE_FLAGS) && !defined(CONFIG_COMPILE_TEST)
96 #warning Unfortunate NUMA and NUMA Balancing config, growing page-frame for last_cpupid.
97 #endif
98
99 #ifndef CONFIG_NUMA
100 unsigned long max_mapnr;
101 EXPORT_SYMBOL(max_mapnr);
102
103 struct page *mem_map;
104 EXPORT_SYMBOL(mem_map);
105 #endif
106
107 static vm_fault_t do_fault(struct vm_fault *vmf);
108 static vm_fault_t do_anonymous_page(struct vm_fault *vmf);
109 static bool vmf_pte_changed(struct vm_fault *vmf);
110
111 /*
112  * Return true if the original pte was a uffd-wp pte marker (so the pte was
113  * wr-protected).
114  */
115 static __always_inline bool vmf_orig_pte_uffd_wp(struct vm_fault *vmf)
116 {
117         if (!userfaultfd_wp(vmf->vma))
118                 return false;
119         if (!(vmf->flags & FAULT_FLAG_ORIG_PTE_VALID))
120                 return false;
121
122         return pte_marker_uffd_wp(vmf->orig_pte);
123 }
124
125 /*
126  * A number of key systems in x86 including ioremap() rely on the assumption
127  * that high_memory defines the upper bound on direct map memory, then end
128  * of ZONE_NORMAL.
129  */
130 void *high_memory;
131 EXPORT_SYMBOL(high_memory);
132
133 /*
134  * Randomize the address space (stacks, mmaps, brk, etc.).
135  *
136  * ( When CONFIG_COMPAT_BRK=y we exclude brk from randomization,
137  *   as ancient (libc5 based) binaries can segfault. )
138  */
139 int randomize_va_space __read_mostly =
140 #ifdef CONFIG_COMPAT_BRK
141                                         1;
142 #else
143                                         2;
144 #endif
145
146 #ifndef arch_wants_old_prefaulted_pte
147 static inline bool arch_wants_old_prefaulted_pte(void)
148 {
149         /*
150          * Transitioning a PTE from 'old' to 'young' can be expensive on
151          * some architectures, even if it's performed in hardware. By
152          * default, "false" means prefaulted entries will be 'young'.
153          */
154         return false;
155 }
156 #endif
157
158 static int __init disable_randmaps(char *s)
159 {
160         randomize_va_space = 0;
161         return 1;
162 }
163 __setup("norandmaps", disable_randmaps);
164
165 unsigned long zero_pfn __read_mostly;
166 EXPORT_SYMBOL(zero_pfn);
167
168 unsigned long highest_memmap_pfn __read_mostly;
169
170 /*
171  * CONFIG_MMU architectures set up ZERO_PAGE in their paging_init()
172  */
173 static int __init init_zero_pfn(void)
174 {
175         zero_pfn = page_to_pfn(ZERO_PAGE(0));
176         return 0;
177 }
178 early_initcall(init_zero_pfn);
179
180 void mm_trace_rss_stat(struct mm_struct *mm, int member)
181 {
182         trace_rss_stat(mm, member);
183 }
184
185 /*
186  * Note: this doesn't free the actual pages themselves. That
187  * has been handled earlier when unmapping all the memory regions.
188  */
189 static void free_pte_range(struct mmu_gather *tlb, pmd_t *pmd,
190                            unsigned long addr)
191 {
192         pgtable_t token = pmd_pgtable(*pmd);
193         pmd_clear(pmd);
194         pte_free_tlb(tlb, token, addr);
195         mm_dec_nr_ptes(tlb->mm);
196 }
197
198 static inline void free_pmd_range(struct mmu_gather *tlb, pud_t *pud,
199                                 unsigned long addr, unsigned long end,
200                                 unsigned long floor, unsigned long ceiling)
201 {
202         pmd_t *pmd;
203         unsigned long next;
204         unsigned long start;
205
206         start = addr;
207         pmd = pmd_offset(pud, addr);
208         do {
209                 next = pmd_addr_end(addr, end);
210                 if (pmd_none_or_clear_bad(pmd))
211                         continue;
212                 free_pte_range(tlb, pmd, addr);
213         } while (pmd++, addr = next, addr != end);
214
215         start &= PUD_MASK;
216         if (start < floor)
217                 return;
218         if (ceiling) {
219                 ceiling &= PUD_MASK;
220                 if (!ceiling)
221                         return;
222         }
223         if (end - 1 > ceiling - 1)
224                 return;
225
226         pmd = pmd_offset(pud, start);
227         pud_clear(pud);
228         pmd_free_tlb(tlb, pmd, start);
229         mm_dec_nr_pmds(tlb->mm);
230 }
231
232 static inline void free_pud_range(struct mmu_gather *tlb, p4d_t *p4d,
233                                 unsigned long addr, unsigned long end,
234                                 unsigned long floor, unsigned long ceiling)
235 {
236         pud_t *pud;
237         unsigned long next;
238         unsigned long start;
239
240         start = addr;
241         pud = pud_offset(p4d, addr);
242         do {
243                 next = pud_addr_end(addr, end);
244                 if (pud_none_or_clear_bad(pud))
245                         continue;
246                 free_pmd_range(tlb, pud, addr, next, floor, ceiling);
247         } while (pud++, addr = next, addr != end);
248
249         start &= P4D_MASK;
250         if (start < floor)
251                 return;
252         if (ceiling) {
253                 ceiling &= P4D_MASK;
254                 if (!ceiling)
255                         return;
256         }
257         if (end - 1 > ceiling - 1)
258                 return;
259
260         pud = pud_offset(p4d, start);
261         p4d_clear(p4d);
262         pud_free_tlb(tlb, pud, start);
263         mm_dec_nr_puds(tlb->mm);
264 }
265
266 static inline void free_p4d_range(struct mmu_gather *tlb, pgd_t *pgd,
267                                 unsigned long addr, unsigned long end,
268                                 unsigned long floor, unsigned long ceiling)
269 {
270         p4d_t *p4d;
271         unsigned long next;
272         unsigned long start;
273
274         start = addr;
275         p4d = p4d_offset(pgd, addr);
276         do {
277                 next = p4d_addr_end(addr, end);
278                 if (p4d_none_or_clear_bad(p4d))
279                         continue;
280                 free_pud_range(tlb, p4d, addr, next, floor, ceiling);
281         } while (p4d++, addr = next, addr != end);
282
283         start &= PGDIR_MASK;
284         if (start < floor)
285                 return;
286         if (ceiling) {
287                 ceiling &= PGDIR_MASK;
288                 if (!ceiling)
289                         return;
290         }
291         if (end - 1 > ceiling - 1)
292                 return;
293
294         p4d = p4d_offset(pgd, start);
295         pgd_clear(pgd);
296         p4d_free_tlb(tlb, p4d, start);
297 }
298
299 /*
300  * This function frees user-level page tables of a process.
301  */
302 void free_pgd_range(struct mmu_gather *tlb,
303                         unsigned long addr, unsigned long end,
304                         unsigned long floor, unsigned long ceiling)
305 {
306         pgd_t *pgd;
307         unsigned long next;
308
309         /*
310          * The next few lines have given us lots of grief...
311          *
312          * Why are we testing PMD* at this top level?  Because often
313          * there will be no work to do at all, and we'd prefer not to
314          * go all the way down to the bottom just to discover that.
315          *
316          * Why all these "- 1"s?  Because 0 represents both the bottom
317          * of the address space and the top of it (using -1 for the
318          * top wouldn't help much: the masks would do the wrong thing).
319          * The rule is that addr 0 and floor 0 refer to the bottom of
320          * the address space, but end 0 and ceiling 0 refer to the top
321          * Comparisons need to use "end - 1" and "ceiling - 1" (though
322          * that end 0 case should be mythical).
323          *
324          * Wherever addr is brought up or ceiling brought down, we must
325          * be careful to reject "the opposite 0" before it confuses the
326          * subsequent tests.  But what about where end is brought down
327          * by PMD_SIZE below? no, end can't go down to 0 there.
328          *
329          * Whereas we round start (addr) and ceiling down, by different
330          * masks at different levels, in order to test whether a table
331          * now has no other vmas using it, so can be freed, we don't
332          * bother to round floor or end up - the tests don't need that.
333          */
334
335         addr &= PMD_MASK;
336         if (addr < floor) {
337                 addr += PMD_SIZE;
338                 if (!addr)
339                         return;
340         }
341         if (ceiling) {
342                 ceiling &= PMD_MASK;
343                 if (!ceiling)
344                         return;
345         }
346         if (end - 1 > ceiling - 1)
347                 end -= PMD_SIZE;
348         if (addr > end - 1)
349                 return;
350         /*
351          * We add page table cache pages with PAGE_SIZE,
352          * (see pte_free_tlb()), flush the tlb if we need
353          */
354         tlb_change_page_size(tlb, PAGE_SIZE);
355         pgd = pgd_offset(tlb->mm, addr);
356         do {
357                 next = pgd_addr_end(addr, end);
358                 if (pgd_none_or_clear_bad(pgd))
359                         continue;
360                 free_p4d_range(tlb, pgd, addr, next, floor, ceiling);
361         } while (pgd++, addr = next, addr != end);
362 }
363
364 void free_pgtables(struct mmu_gather *tlb, struct ma_state *mas,
365                    struct vm_area_struct *vma, unsigned long floor,
366                    unsigned long ceiling, bool mm_wr_locked)
367 {
368         struct unlink_vma_file_batch vb;
369
370         do {
371                 unsigned long addr = vma->vm_start;
372                 struct vm_area_struct *next;
373
374                 /*
375                  * Note: USER_PGTABLES_CEILING may be passed as ceiling and may
376                  * be 0.  This will underflow and is okay.
377                  */
378                 next = mas_find(mas, ceiling - 1);
379                 if (unlikely(xa_is_zero(next)))
380                         next = NULL;
381
382                 /*
383                  * Hide vma from rmap and truncate_pagecache before freeing
384                  * pgtables
385                  */
386                 if (mm_wr_locked)
387                         vma_start_write(vma);
388                 unlink_anon_vmas(vma);
389
390                 if (is_vm_hugetlb_page(vma)) {
391                         unlink_file_vma(vma);
392                         hugetlb_free_pgd_range(tlb, addr, vma->vm_end,
393                                 floor, next ? next->vm_start : ceiling);
394                 } else {
395                         unlink_file_vma_batch_init(&vb);
396                         unlink_file_vma_batch_add(&vb, vma);
397
398                         /*
399                          * Optimization: gather nearby vmas into one call down
400                          */
401                         while (next && next->vm_start <= vma->vm_end + PMD_SIZE
402                                && !is_vm_hugetlb_page(next)) {
403                                 vma = next;
404                                 next = mas_find(mas, ceiling - 1);
405                                 if (unlikely(xa_is_zero(next)))
406                                         next = NULL;
407                                 if (mm_wr_locked)
408                                         vma_start_write(vma);
409                                 unlink_anon_vmas(vma);
410                                 unlink_file_vma_batch_add(&vb, vma);
411                         }
412                         unlink_file_vma_batch_final(&vb);
413                         free_pgd_range(tlb, addr, vma->vm_end,
414                                 floor, next ? next->vm_start : ceiling);
415                 }
416                 vma = next;
417         } while (vma);
418 }
419
420 void pmd_install(struct mm_struct *mm, pmd_t *pmd, pgtable_t *pte)
421 {
422         spinlock_t *ptl = pmd_lock(mm, pmd);
423
424         if (likely(pmd_none(*pmd))) {   /* Has another populated it ? */
425                 mm_inc_nr_ptes(mm);
426                 /*
427                  * Ensure all pte setup (eg. pte page lock and page clearing) are
428                  * visible before the pte is made visible to other CPUs by being
429                  * put into page tables.
430                  *
431                  * The other side of the story is the pointer chasing in the page
432                  * table walking code (when walking the page table without locking;
433                  * ie. most of the time). Fortunately, these data accesses consist
434                  * of a chain of data-dependent loads, meaning most CPUs (alpha
435                  * being the notable exception) will already guarantee loads are
436                  * seen in-order. See the alpha page table accessors for the
437                  * smp_rmb() barriers in page table walking code.
438                  */
439                 smp_wmb(); /* Could be smp_wmb__xxx(before|after)_spin_lock */
440                 pmd_populate(mm, pmd, *pte);
441                 *pte = NULL;
442         }
443         spin_unlock(ptl);
444 }
445
446 int __pte_alloc(struct mm_struct *mm, pmd_t *pmd)
447 {
448         pgtable_t new = pte_alloc_one(mm);
449         if (!new)
450                 return -ENOMEM;
451
452         pmd_install(mm, pmd, &new);
453         if (new)
454                 pte_free(mm, new);
455         return 0;
456 }
457
458 int __pte_alloc_kernel(pmd_t *pmd)
459 {
460         pte_t *new = pte_alloc_one_kernel(&init_mm);
461         if (!new)
462                 return -ENOMEM;
463
464         spin_lock(&init_mm.page_table_lock);
465         if (likely(pmd_none(*pmd))) {   /* Has another populated it ? */
466                 smp_wmb(); /* See comment in pmd_install() */
467                 pmd_populate_kernel(&init_mm, pmd, new);
468                 new = NULL;
469         }
470         spin_unlock(&init_mm.page_table_lock);
471         if (new)
472                 pte_free_kernel(&init_mm, new);
473         return 0;
474 }
475
476 static inline void init_rss_vec(int *rss)
477 {
478         memset(rss, 0, sizeof(int) * NR_MM_COUNTERS);
479 }
480
481 static inline void add_mm_rss_vec(struct mm_struct *mm, int *rss)
482 {
483         int i;
484
485         for (i = 0; i < NR_MM_COUNTERS; i++)
486                 if (rss[i])
487                         add_mm_counter(mm, i, rss[i]);
488 }
489
490 /*
491  * This function is called to print an error when a bad pte
492  * is found. For example, we might have a PFN-mapped pte in
493  * a region that doesn't allow it.
494  *
495  * The calling function must still handle the error.
496  */
497 static void print_bad_pte(struct vm_area_struct *vma, unsigned long addr,
498                           pte_t pte, struct page *page)
499 {
500         pgd_t *pgd = pgd_offset(vma->vm_mm, addr);
501         p4d_t *p4d = p4d_offset(pgd, addr);
502         pud_t *pud = pud_offset(p4d, addr);
503         pmd_t *pmd = pmd_offset(pud, addr);
504         struct address_space *mapping;
505         pgoff_t index;
506         static unsigned long resume;
507         static unsigned long nr_shown;
508         static unsigned long nr_unshown;
509
510         /*
511          * Allow a burst of 60 reports, then keep quiet for that minute;
512          * or allow a steady drip of one report per second.
513          */
514         if (nr_shown == 60) {
515                 if (time_before(jiffies, resume)) {
516                         nr_unshown++;
517                         return;
518                 }
519                 if (nr_unshown) {
520                         pr_alert("BUG: Bad page map: %lu messages suppressed\n",
521                                  nr_unshown);
522                         nr_unshown = 0;
523                 }
524                 nr_shown = 0;
525         }
526         if (nr_shown++ == 0)
527                 resume = jiffies + 60 * HZ;
528
529         mapping = vma->vm_file ? vma->vm_file->f_mapping : NULL;
530         index = linear_page_index(vma, addr);
531
532         pr_alert("BUG: Bad page map in process %s  pte:%08llx pmd:%08llx\n",
533                  current->comm,
534                  (long long)pte_val(pte), (long long)pmd_val(*pmd));
535         if (page)
536                 dump_page(page, "bad pte");
537         pr_alert("addr:%px vm_flags:%08lx anon_vma:%px mapping:%px index:%lx\n",
538                  (void *)addr, vma->vm_flags, vma->anon_vma, mapping, index);
539         pr_alert("file:%pD fault:%ps mmap:%ps read_folio:%ps\n",
540                  vma->vm_file,
541                  vma->vm_ops ? vma->vm_ops->fault : NULL,
542                  vma->vm_file ? vma->vm_file->f_op->mmap : NULL,
543                  mapping ? mapping->a_ops->read_folio : NULL);
544         dump_stack();
545         add_taint(TAINT_BAD_PAGE, LOCKDEP_NOW_UNRELIABLE);
546 }
547
548 /*
549  * vm_normal_page -- This function gets the "struct page" associated with a pte.
550  *
551  * "Special" mappings do not wish to be associated with a "struct page" (either
552  * it doesn't exist, or it exists but they don't want to touch it). In this
553  * case, NULL is returned here. "Normal" mappings do have a struct page.
554  *
555  * There are 2 broad cases. Firstly, an architecture may define a pte_special()
556  * pte bit, in which case this function is trivial. Secondly, an architecture
557  * may not have a spare pte bit, which requires a more complicated scheme,
558  * described below.
559  *
560  * A raw VM_PFNMAP mapping (ie. one that is not COWed) is always considered a
561  * special mapping (even if there are underlying and valid "struct pages").
562  * COWed pages of a VM_PFNMAP are always normal.
563  *
564  * The way we recognize COWed pages within VM_PFNMAP mappings is through the
565  * rules set up by "remap_pfn_range()": the vma will have the VM_PFNMAP bit
566  * set, and the vm_pgoff will point to the first PFN mapped: thus every special
567  * mapping will always honor the rule
568  *
569  *      pfn_of_page == vma->vm_pgoff + ((addr - vma->vm_start) >> PAGE_SHIFT)
570  *
571  * And for normal mappings this is false.
572  *
573  * This restricts such mappings to be a linear translation from virtual address
574  * to pfn. To get around this restriction, we allow arbitrary mappings so long
575  * as the vma is not a COW mapping; in that case, we know that all ptes are
576  * special (because none can have been COWed).
577  *
578  *
579  * In order to support COW of arbitrary special mappings, we have VM_MIXEDMAP.
580  *
581  * VM_MIXEDMAP mappings can likewise contain memory with or without "struct
582  * page" backing, however the difference is that _all_ pages with a struct
583  * page (that is, those where pfn_valid is true) are refcounted and considered
584  * normal pages by the VM. The only exception are zeropages, which are
585  * *never* refcounted.
586  *
587  * The disadvantage is that pages are refcounted (which can be slower and
588  * simply not an option for some PFNMAP users). The advantage is that we
589  * don't have to follow the strict linearity rule of PFNMAP mappings in
590  * order to support COWable mappings.
591  *
592  */
593 struct page *vm_normal_page(struct vm_area_struct *vma, unsigned long addr,
594                             pte_t pte)
595 {
596         unsigned long pfn = pte_pfn(pte);
597
598         if (IS_ENABLED(CONFIG_ARCH_HAS_PTE_SPECIAL)) {
599                 if (likely(!pte_special(pte)))
600                         goto check_pfn;
601                 if (vma->vm_ops && vma->vm_ops->find_special_page)
602                         return vma->vm_ops->find_special_page(vma, addr);
603                 if (vma->vm_flags & (VM_PFNMAP | VM_MIXEDMAP))
604                         return NULL;
605                 if (is_zero_pfn(pfn))
606                         return NULL;
607                 if (pte_devmap(pte))
608                 /*
609                  * NOTE: New users of ZONE_DEVICE will not set pte_devmap()
610                  * and will have refcounts incremented on their struct pages
611                  * when they are inserted into PTEs, thus they are safe to
612                  * return here. Legacy ZONE_DEVICE pages that set pte_devmap()
613                  * do not have refcounts. Example of legacy ZONE_DEVICE is
614                  * MEMORY_DEVICE_FS_DAX type in pmem or virtio_fs drivers.
615                  */
616                         return NULL;
617
618                 print_bad_pte(vma, addr, pte, NULL);
619                 return NULL;
620         }
621
622         /* !CONFIG_ARCH_HAS_PTE_SPECIAL case follows: */
623
624         if (unlikely(vma->vm_flags & (VM_PFNMAP|VM_MIXEDMAP))) {
625                 if (vma->vm_flags & VM_MIXEDMAP) {
626                         if (!pfn_valid(pfn))
627                                 return NULL;
628                         if (is_zero_pfn(pfn))
629                                 return NULL;
630                         goto out;
631                 } else {
632                         unsigned long off;
633                         off = (addr - vma->vm_start) >> PAGE_SHIFT;
634                         if (pfn == vma->vm_pgoff + off)
635                                 return NULL;
636                         if (!is_cow_mapping(vma->vm_flags))
637                                 return NULL;
638                 }
639         }
640
641         if (is_zero_pfn(pfn))
642                 return NULL;
643
644 check_pfn:
645         if (unlikely(pfn > highest_memmap_pfn)) {
646                 print_bad_pte(vma, addr, pte, NULL);
647                 return NULL;
648         }
649
650         /*
651          * NOTE! We still have PageReserved() pages in the page tables.
652          * eg. VDSO mappings can cause them to exist.
653          */
654 out:
655         VM_WARN_ON_ONCE(is_zero_pfn(pfn));
656         return pfn_to_page(pfn);
657 }
658
659 struct folio *vm_normal_folio(struct vm_area_struct *vma, unsigned long addr,
660                             pte_t pte)
661 {
662         struct page *page = vm_normal_page(vma, addr, pte);
663
664         if (page)
665                 return page_folio(page);
666         return NULL;
667 }
668
669 #ifdef CONFIG_PGTABLE_HAS_HUGE_LEAVES
670 struct page *vm_normal_page_pmd(struct vm_area_struct *vma, unsigned long addr,
671                                 pmd_t pmd)
672 {
673         unsigned long pfn = pmd_pfn(pmd);
674
675         /* Currently it's only used for huge pfnmaps */
676         if (unlikely(pmd_special(pmd)))
677                 return NULL;
678
679         if (unlikely(vma->vm_flags & (VM_PFNMAP|VM_MIXEDMAP))) {
680                 if (vma->vm_flags & VM_MIXEDMAP) {
681                         if (!pfn_valid(pfn))
682                                 return NULL;
683                         goto out;
684                 } else {
685                         unsigned long off;
686                         off = (addr - vma->vm_start) >> PAGE_SHIFT;
687                         if (pfn == vma->vm_pgoff + off)
688                                 return NULL;
689                         if (!is_cow_mapping(vma->vm_flags))
690                                 return NULL;
691                 }
692         }
693
694         if (pmd_devmap(pmd))
695                 return NULL;
696         if (is_huge_zero_pmd(pmd))
697                 return NULL;
698         if (unlikely(pfn > highest_memmap_pfn))
699                 return NULL;
700
701         /*
702          * NOTE! We still have PageReserved() pages in the page tables.
703          * eg. VDSO mappings can cause them to exist.
704          */
705 out:
706         return pfn_to_page(pfn);
707 }
708
709 struct folio *vm_normal_folio_pmd(struct vm_area_struct *vma,
710                                   unsigned long addr, pmd_t pmd)
711 {
712         struct page *page = vm_normal_page_pmd(vma, addr, pmd);
713
714         if (page)
715                 return page_folio(page);
716         return NULL;
717 }
718 #endif
719
720 static void restore_exclusive_pte(struct vm_area_struct *vma,
721                                   struct page *page, unsigned long address,
722                                   pte_t *ptep)
723 {
724         struct folio *folio = page_folio(page);
725         pte_t orig_pte;
726         pte_t pte;
727         swp_entry_t entry;
728
729         orig_pte = ptep_get(ptep);
730         pte = pte_mkold(mk_pte(page, READ_ONCE(vma->vm_page_prot)));
731         if (pte_swp_soft_dirty(orig_pte))
732                 pte = pte_mksoft_dirty(pte);
733
734         entry = pte_to_swp_entry(orig_pte);
735         if (pte_swp_uffd_wp(orig_pte))
736                 pte = pte_mkuffd_wp(pte);
737         else if (is_writable_device_exclusive_entry(entry))
738                 pte = maybe_mkwrite(pte_mkdirty(pte), vma);
739
740         VM_BUG_ON_FOLIO(pte_write(pte) && (!folio_test_anon(folio) &&
741                                            PageAnonExclusive(page)), folio);
742
743         /*
744          * No need to take a page reference as one was already
745          * created when the swap entry was made.
746          */
747         if (folio_test_anon(folio))
748                 folio_add_anon_rmap_pte(folio, page, vma, address, RMAP_NONE);
749         else
750                 /*
751                  * Currently device exclusive access only supports anonymous
752                  * memory so the entry shouldn't point to a filebacked page.
753                  */
754                 WARN_ON_ONCE(1);
755
756         set_pte_at(vma->vm_mm, address, ptep, pte);
757
758         /*
759          * No need to invalidate - it was non-present before. However
760          * secondary CPUs may have mappings that need invalidating.
761          */
762         update_mmu_cache(vma, address, ptep);
763 }
764
765 /*
766  * Tries to restore an exclusive pte if the page lock can be acquired without
767  * sleeping.
768  */
769 static int
770 try_restore_exclusive_pte(pte_t *src_pte, struct vm_area_struct *vma,
771                         unsigned long addr)
772 {
773         swp_entry_t entry = pte_to_swp_entry(ptep_get(src_pte));
774         struct page *page = pfn_swap_entry_to_page(entry);
775
776         if (trylock_page(page)) {
777                 restore_exclusive_pte(vma, page, addr, src_pte);
778                 unlock_page(page);
779                 return 0;
780         }
781
782         return -EBUSY;
783 }
784
785 /*
786  * copy one vm_area from one task to the other. Assumes the page tables
787  * already present in the new task to be cleared in the whole range
788  * covered by this vma.
789  */
790
791 static unsigned long
792 copy_nonpresent_pte(struct mm_struct *dst_mm, struct mm_struct *src_mm,
793                 pte_t *dst_pte, pte_t *src_pte, struct vm_area_struct *dst_vma,
794                 struct vm_area_struct *src_vma, unsigned long addr, int *rss)
795 {
796         unsigned long vm_flags = dst_vma->vm_flags;
797         pte_t orig_pte = ptep_get(src_pte);
798         pte_t pte = orig_pte;
799         struct folio *folio;
800         struct page *page;
801         swp_entry_t entry = pte_to_swp_entry(orig_pte);
802
803         if (likely(!non_swap_entry(entry))) {
804                 if (swap_duplicate(entry) < 0)
805                         return -EIO;
806
807                 /* make sure dst_mm is on swapoff's mmlist. */
808                 if (unlikely(list_empty(&dst_mm->mmlist))) {
809                         spin_lock(&mmlist_lock);
810                         if (list_empty(&dst_mm->mmlist))
811                                 list_add(&dst_mm->mmlist,
812                                                 &src_mm->mmlist);
813                         spin_unlock(&mmlist_lock);
814                 }
815                 /* Mark the swap entry as shared. */
816                 if (pte_swp_exclusive(orig_pte)) {
817                         pte = pte_swp_clear_exclusive(orig_pte);
818                         set_pte_at(src_mm, addr, src_pte, pte);
819                 }
820                 rss[MM_SWAPENTS]++;
821         } else if (is_migration_entry(entry)) {
822                 folio = pfn_swap_entry_folio(entry);
823
824                 rss[mm_counter(folio)]++;
825
826                 if (!is_readable_migration_entry(entry) &&
827                                 is_cow_mapping(vm_flags)) {
828                         /*
829                          * COW mappings require pages in both parent and child
830                          * to be set to read. A previously exclusive entry is
831                          * now shared.
832                          */
833                         entry = make_readable_migration_entry(
834                                                         swp_offset(entry));
835                         pte = swp_entry_to_pte(entry);
836                         if (pte_swp_soft_dirty(orig_pte))
837                                 pte = pte_swp_mksoft_dirty(pte);
838                         if (pte_swp_uffd_wp(orig_pte))
839                                 pte = pte_swp_mkuffd_wp(pte);
840                         set_pte_at(src_mm, addr, src_pte, pte);
841                 }
842         } else if (is_device_private_entry(entry)) {
843                 page = pfn_swap_entry_to_page(entry);
844                 folio = page_folio(page);
845
846                 /*
847                  * Update rss count even for unaddressable pages, as
848                  * they should treated just like normal pages in this
849                  * respect.
850                  *
851                  * We will likely want to have some new rss counters
852                  * for unaddressable pages, at some point. But for now
853                  * keep things as they are.
854                  */
855                 folio_get(folio);
856                 rss[mm_counter(folio)]++;
857                 /* Cannot fail as these pages cannot get pinned. */
858                 folio_try_dup_anon_rmap_pte(folio, page, src_vma);
859
860                 /*
861                  * We do not preserve soft-dirty information, because so
862                  * far, checkpoint/restore is the only feature that
863                  * requires that. And checkpoint/restore does not work
864                  * when a device driver is involved (you cannot easily
865                  * save and restore device driver state).
866                  */
867                 if (is_writable_device_private_entry(entry) &&
868                     is_cow_mapping(vm_flags)) {
869                         entry = make_readable_device_private_entry(
870                                                         swp_offset(entry));
871                         pte = swp_entry_to_pte(entry);
872                         if (pte_swp_uffd_wp(orig_pte))
873                                 pte = pte_swp_mkuffd_wp(pte);
874                         set_pte_at(src_mm, addr, src_pte, pte);
875                 }
876         } else if (is_device_exclusive_entry(entry)) {
877                 /*
878                  * Make device exclusive entries present by restoring the
879                  * original entry then copying as for a present pte. Device
880                  * exclusive entries currently only support private writable
881                  * (ie. COW) mappings.
882                  */
883                 VM_BUG_ON(!is_cow_mapping(src_vma->vm_flags));
884                 if (try_restore_exclusive_pte(src_pte, src_vma, addr))
885                         return -EBUSY;
886                 return -ENOENT;
887         } else if (is_pte_marker_entry(entry)) {
888                 pte_marker marker = copy_pte_marker(entry, dst_vma);
889
890                 if (marker)
891                         set_pte_at(dst_mm, addr, dst_pte,
892                                    make_pte_marker(marker));
893                 return 0;
894         }
895         if (!userfaultfd_wp(dst_vma))
896                 pte = pte_swp_clear_uffd_wp(pte);
897         set_pte_at(dst_mm, addr, dst_pte, pte);
898         return 0;
899 }
900
901 /*
902  * Copy a present and normal page.
903  *
904  * NOTE! The usual case is that this isn't required;
905  * instead, the caller can just increase the page refcount
906  * and re-use the pte the traditional way.
907  *
908  * And if we need a pre-allocated page but don't yet have
909  * one, return a negative error to let the preallocation
910  * code know so that it can do so outside the page table
911  * lock.
912  */
913 static inline int
914 copy_present_page(struct vm_area_struct *dst_vma, struct vm_area_struct *src_vma,
915                   pte_t *dst_pte, pte_t *src_pte, unsigned long addr, int *rss,
916                   struct folio **prealloc, struct page *page)
917 {
918         struct folio *new_folio;
919         pte_t pte;
920
921         new_folio = *prealloc;
922         if (!new_folio)
923                 return -EAGAIN;
924
925         /*
926          * We have a prealloc page, all good!  Take it
927          * over and copy the page & arm it.
928          */
929
930         if (copy_mc_user_highpage(&new_folio->page, page, addr, src_vma))
931                 return -EHWPOISON;
932
933         *prealloc = NULL;
934         __folio_mark_uptodate(new_folio);
935         folio_add_new_anon_rmap(new_folio, dst_vma, addr, RMAP_EXCLUSIVE);
936         folio_add_lru_vma(new_folio, dst_vma);
937         rss[MM_ANONPAGES]++;
938
939         /* All done, just insert the new page copy in the child */
940         pte = mk_pte(&new_folio->page, dst_vma->vm_page_prot);
941         pte = maybe_mkwrite(pte_mkdirty(pte), dst_vma);
942         if (userfaultfd_pte_wp(dst_vma, ptep_get(src_pte)))
943                 /* Uffd-wp needs to be delivered to dest pte as well */
944                 pte = pte_mkuffd_wp(pte);
945         set_pte_at(dst_vma->vm_mm, addr, dst_pte, pte);
946         return 0;
947 }
948
949 static __always_inline void __copy_present_ptes(struct vm_area_struct *dst_vma,
950                 struct vm_area_struct *src_vma, pte_t *dst_pte, pte_t *src_pte,
951                 pte_t pte, unsigned long addr, int nr)
952 {
953         struct mm_struct *src_mm = src_vma->vm_mm;
954
955         /* If it's a COW mapping, write protect it both processes. */
956         if (is_cow_mapping(src_vma->vm_flags) && pte_write(pte)) {
957                 wrprotect_ptes(src_mm, addr, src_pte, nr);
958                 pte = pte_wrprotect(pte);
959         }
960
961         /* If it's a shared mapping, mark it clean in the child. */
962         if (src_vma->vm_flags & VM_SHARED)
963                 pte = pte_mkclean(pte);
964         pte = pte_mkold(pte);
965
966         if (!userfaultfd_wp(dst_vma))
967                 pte = pte_clear_uffd_wp(pte);
968
969         set_ptes(dst_vma->vm_mm, addr, dst_pte, pte, nr);
970 }
971
972 /*
973  * Copy one present PTE, trying to batch-process subsequent PTEs that map
974  * consecutive pages of the same folio by copying them as well.
975  *
976  * Returns -EAGAIN if one preallocated page is required to copy the next PTE.
977  * Otherwise, returns the number of copied PTEs (at least 1).
978  */
979 static inline int
980 copy_present_ptes(struct vm_area_struct *dst_vma, struct vm_area_struct *src_vma,
981                  pte_t *dst_pte, pte_t *src_pte, pte_t pte, unsigned long addr,
982                  int max_nr, int *rss, struct folio **prealloc)
983 {
984         struct page *page;
985         struct folio *folio;
986         bool any_writable;
987         fpb_t flags = 0;
988         int err, nr;
989
990         page = vm_normal_page(src_vma, addr, pte);
991         if (unlikely(!page))
992                 goto copy_pte;
993
994         folio = page_folio(page);
995
996         /*
997          * If we likely have to copy, just don't bother with batching. Make
998          * sure that the common "small folio" case is as fast as possible
999          * by keeping the batching logic separate.
1000          */
1001         if (unlikely(!*prealloc && folio_test_large(folio) && max_nr != 1)) {
1002                 if (src_vma->vm_flags & VM_SHARED)
1003                         flags |= FPB_IGNORE_DIRTY;
1004                 if (!vma_soft_dirty_enabled(src_vma))
1005                         flags |= FPB_IGNORE_SOFT_DIRTY;
1006
1007                 nr = folio_pte_batch(folio, addr, src_pte, pte, max_nr, flags,
1008                                      &any_writable, NULL, NULL);
1009                 folio_ref_add(folio, nr);
1010                 if (folio_test_anon(folio)) {
1011                         if (unlikely(folio_try_dup_anon_rmap_ptes(folio, page,
1012                                                                   nr, src_vma))) {
1013                                 folio_ref_sub(folio, nr);
1014                                 return -EAGAIN;
1015                         }
1016                         rss[MM_ANONPAGES] += nr;
1017                         VM_WARN_ON_FOLIO(PageAnonExclusive(page), folio);
1018                 } else {
1019                         folio_dup_file_rmap_ptes(folio, page, nr);
1020                         rss[mm_counter_file(folio)] += nr;
1021                 }
1022                 if (any_writable)
1023                         pte = pte_mkwrite(pte, src_vma);
1024                 __copy_present_ptes(dst_vma, src_vma, dst_pte, src_pte, pte,
1025                                     addr, nr);
1026                 return nr;
1027         }
1028
1029         folio_get(folio);
1030         if (folio_test_anon(folio)) {
1031                 /*
1032                  * If this page may have been pinned by the parent process,
1033                  * copy the page immediately for the child so that we'll always
1034                  * guarantee the pinned page won't be randomly replaced in the
1035                  * future.
1036                  */
1037                 if (unlikely(folio_try_dup_anon_rmap_pte(folio, page, src_vma))) {
1038                         /* Page may be pinned, we have to copy. */
1039                         folio_put(folio);
1040                         err = copy_present_page(dst_vma, src_vma, dst_pte, src_pte,
1041                                                 addr, rss, prealloc, page);
1042                         return err ? err : 1;
1043                 }
1044                 rss[MM_ANONPAGES]++;
1045                 VM_WARN_ON_FOLIO(PageAnonExclusive(page), folio);
1046         } else {
1047                 folio_dup_file_rmap_pte(folio, page);
1048                 rss[mm_counter_file(folio)]++;
1049         }
1050
1051 copy_pte:
1052         __copy_present_ptes(dst_vma, src_vma, dst_pte, src_pte, pte, addr, 1);
1053         return 1;
1054 }
1055
1056 static inline struct folio *folio_prealloc(struct mm_struct *src_mm,
1057                 struct vm_area_struct *vma, unsigned long addr, bool need_zero)
1058 {
1059         struct folio *new_folio;
1060
1061         if (need_zero)
1062                 new_folio = vma_alloc_zeroed_movable_folio(vma, addr);
1063         else
1064                 new_folio = vma_alloc_folio(GFP_HIGHUSER_MOVABLE, 0, vma,
1065                                             addr, false);
1066
1067         if (!new_folio)
1068                 return NULL;
1069
1070         if (mem_cgroup_charge(new_folio, src_mm, GFP_KERNEL)) {
1071                 folio_put(new_folio);
1072                 return NULL;
1073         }
1074         folio_throttle_swaprate(new_folio, GFP_KERNEL);
1075
1076         return new_folio;
1077 }
1078
1079 static int
1080 copy_pte_range(struct vm_area_struct *dst_vma, struct vm_area_struct *src_vma,
1081                pmd_t *dst_pmd, pmd_t *src_pmd, unsigned long addr,
1082                unsigned long end)
1083 {
1084         struct mm_struct *dst_mm = dst_vma->vm_mm;
1085         struct mm_struct *src_mm = src_vma->vm_mm;
1086         pte_t *orig_src_pte, *orig_dst_pte;
1087         pte_t *src_pte, *dst_pte;
1088         pte_t ptent;
1089         spinlock_t *src_ptl, *dst_ptl;
1090         int progress, max_nr, ret = 0;
1091         int rss[NR_MM_COUNTERS];
1092         swp_entry_t entry = (swp_entry_t){0};
1093         struct folio *prealloc = NULL;
1094         int nr;
1095
1096 again:
1097         progress = 0;
1098         init_rss_vec(rss);
1099
1100         /*
1101          * copy_pmd_range()'s prior pmd_none_or_clear_bad(src_pmd), and the
1102          * error handling here, assume that exclusive mmap_lock on dst and src
1103          * protects anon from unexpected THP transitions; with shmem and file
1104          * protected by mmap_lock-less collapse skipping areas with anon_vma
1105          * (whereas vma_needs_copy() skips areas without anon_vma).  A rework
1106          * can remove such assumptions later, but this is good enough for now.
1107          */
1108         dst_pte = pte_alloc_map_lock(dst_mm, dst_pmd, addr, &dst_ptl);
1109         if (!dst_pte) {
1110                 ret = -ENOMEM;
1111                 goto out;
1112         }
1113         src_pte = pte_offset_map_nolock(src_mm, src_pmd, addr, &src_ptl);
1114         if (!src_pte) {
1115                 pte_unmap_unlock(dst_pte, dst_ptl);
1116                 /* ret == 0 */
1117                 goto out;
1118         }
1119         spin_lock_nested(src_ptl, SINGLE_DEPTH_NESTING);
1120         orig_src_pte = src_pte;
1121         orig_dst_pte = dst_pte;
1122         arch_enter_lazy_mmu_mode();
1123
1124         do {
1125                 nr = 1;
1126
1127                 /*
1128                  * We are holding two locks at this point - either of them
1129                  * could generate latencies in another task on another CPU.
1130                  */
1131                 if (progress >= 32) {
1132                         progress = 0;
1133                         if (need_resched() ||
1134                             spin_needbreak(src_ptl) || spin_needbreak(dst_ptl))
1135                                 break;
1136                 }
1137                 ptent = ptep_get(src_pte);
1138                 if (pte_none(ptent)) {
1139                         progress++;
1140                         continue;
1141                 }
1142                 if (unlikely(!pte_present(ptent))) {
1143                         ret = copy_nonpresent_pte(dst_mm, src_mm,
1144                                                   dst_pte, src_pte,
1145                                                   dst_vma, src_vma,
1146                                                   addr, rss);
1147                         if (ret == -EIO) {
1148                                 entry = pte_to_swp_entry(ptep_get(src_pte));
1149                                 break;
1150                         } else if (ret == -EBUSY) {
1151                                 break;
1152                         } else if (!ret) {
1153                                 progress += 8;
1154                                 continue;
1155                         }
1156                         ptent = ptep_get(src_pte);
1157                         VM_WARN_ON_ONCE(!pte_present(ptent));
1158
1159                         /*
1160                          * Device exclusive entry restored, continue by copying
1161                          * the now present pte.
1162                          */
1163                         WARN_ON_ONCE(ret != -ENOENT);
1164                 }
1165                 /* copy_present_ptes() will clear `*prealloc' if consumed */
1166                 max_nr = (end - addr) / PAGE_SIZE;
1167                 ret = copy_present_ptes(dst_vma, src_vma, dst_pte, src_pte,
1168                                         ptent, addr, max_nr, rss, &prealloc);
1169                 /*
1170                  * If we need a pre-allocated page for this pte, drop the
1171                  * locks, allocate, and try again.
1172                  * If copy failed due to hwpoison in source page, break out.
1173                  */
1174                 if (unlikely(ret == -EAGAIN || ret == -EHWPOISON))
1175                         break;
1176                 if (unlikely(prealloc)) {
1177                         /*
1178                          * pre-alloc page cannot be reused by next time so as
1179                          * to strictly follow mempolicy (e.g., alloc_page_vma()
1180                          * will allocate page according to address).  This
1181                          * could only happen if one pinned pte changed.
1182                          */
1183                         folio_put(prealloc);
1184                         prealloc = NULL;
1185                 }
1186                 nr = ret;
1187                 progress += 8 * nr;
1188         } while (dst_pte += nr, src_pte += nr, addr += PAGE_SIZE * nr,
1189                  addr != end);
1190
1191         arch_leave_lazy_mmu_mode();
1192         pte_unmap_unlock(orig_src_pte, src_ptl);
1193         add_mm_rss_vec(dst_mm, rss);
1194         pte_unmap_unlock(orig_dst_pte, dst_ptl);
1195         cond_resched();
1196
1197         if (ret == -EIO) {
1198                 VM_WARN_ON_ONCE(!entry.val);
1199                 if (add_swap_count_continuation(entry, GFP_KERNEL) < 0) {
1200                         ret = -ENOMEM;
1201                         goto out;
1202                 }
1203                 entry.val = 0;
1204         } else if (ret == -EBUSY || unlikely(ret == -EHWPOISON)) {
1205                 goto out;
1206         } else if (ret ==  -EAGAIN) {
1207                 prealloc = folio_prealloc(src_mm, src_vma, addr, false);
1208                 if (!prealloc)
1209                         return -ENOMEM;
1210         } else if (ret < 0) {
1211                 VM_WARN_ON_ONCE(1);
1212         }
1213
1214         /* We've captured and resolved the error. Reset, try again. */
1215         ret = 0;
1216
1217         if (addr != end)
1218                 goto again;
1219 out:
1220         if (unlikely(prealloc))
1221                 folio_put(prealloc);
1222         return ret;
1223 }
1224
1225 static inline int
1226 copy_pmd_range(struct vm_area_struct *dst_vma, struct vm_area_struct *src_vma,
1227                pud_t *dst_pud, pud_t *src_pud, unsigned long addr,
1228                unsigned long end)
1229 {
1230         struct mm_struct *dst_mm = dst_vma->vm_mm;
1231         struct mm_struct *src_mm = src_vma->vm_mm;
1232         pmd_t *src_pmd, *dst_pmd;
1233         unsigned long next;
1234
1235         dst_pmd = pmd_alloc(dst_mm, dst_pud, addr);
1236         if (!dst_pmd)
1237                 return -ENOMEM;
1238         src_pmd = pmd_offset(src_pud, addr);
1239         do {
1240                 next = pmd_addr_end(addr, end);
1241                 if (is_swap_pmd(*src_pmd) || pmd_trans_huge(*src_pmd)
1242                         || pmd_devmap(*src_pmd)) {
1243                         int err;
1244                         VM_BUG_ON_VMA(next-addr != HPAGE_PMD_SIZE, src_vma);
1245                         err = copy_huge_pmd(dst_mm, src_mm, dst_pmd, src_pmd,
1246                                             addr, dst_vma, src_vma);
1247                         if (err == -ENOMEM)
1248                                 return -ENOMEM;
1249                         if (!err)
1250                                 continue;
1251                         /* fall through */
1252                 }
1253                 if (pmd_none_or_clear_bad(src_pmd))
1254                         continue;
1255                 if (copy_pte_range(dst_vma, src_vma, dst_pmd, src_pmd,
1256                                    addr, next))
1257                         return -ENOMEM;
1258         } while (dst_pmd++, src_pmd++, addr = next, addr != end);
1259         return 0;
1260 }
1261
1262 static inline int
1263 copy_pud_range(struct vm_area_struct *dst_vma, struct vm_area_struct *src_vma,
1264                p4d_t *dst_p4d, p4d_t *src_p4d, unsigned long addr,
1265                unsigned long end)
1266 {
1267         struct mm_struct *dst_mm = dst_vma->vm_mm;
1268         struct mm_struct *src_mm = src_vma->vm_mm;
1269         pud_t *src_pud, *dst_pud;
1270         unsigned long next;
1271
1272         dst_pud = pud_alloc(dst_mm, dst_p4d, addr);
1273         if (!dst_pud)
1274                 return -ENOMEM;
1275         src_pud = pud_offset(src_p4d, addr);
1276         do {
1277                 next = pud_addr_end(addr, end);
1278                 if (pud_trans_huge(*src_pud) || pud_devmap(*src_pud)) {
1279                         int err;
1280
1281                         VM_BUG_ON_VMA(next-addr != HPAGE_PUD_SIZE, src_vma);
1282                         err = copy_huge_pud(dst_mm, src_mm,
1283                                             dst_pud, src_pud, addr, src_vma);
1284                         if (err == -ENOMEM)
1285                                 return -ENOMEM;
1286                         if (!err)
1287                                 continue;
1288                         /* fall through */
1289                 }
1290                 if (pud_none_or_clear_bad(src_pud))
1291                         continue;
1292                 if (copy_pmd_range(dst_vma, src_vma, dst_pud, src_pud,
1293                                    addr, next))
1294                         return -ENOMEM;
1295         } while (dst_pud++, src_pud++, addr = next, addr != end);
1296         return 0;
1297 }
1298
1299 static inline int
1300 copy_p4d_range(struct vm_area_struct *dst_vma, struct vm_area_struct *src_vma,
1301                pgd_t *dst_pgd, pgd_t *src_pgd, unsigned long addr,
1302                unsigned long end)
1303 {
1304         struct mm_struct *dst_mm = dst_vma->vm_mm;
1305         p4d_t *src_p4d, *dst_p4d;
1306         unsigned long next;
1307
1308         dst_p4d = p4d_alloc(dst_mm, dst_pgd, addr);
1309         if (!dst_p4d)
1310                 return -ENOMEM;
1311         src_p4d = p4d_offset(src_pgd, addr);
1312         do {
1313                 next = p4d_addr_end(addr, end);
1314                 if (p4d_none_or_clear_bad(src_p4d))
1315                         continue;
1316                 if (copy_pud_range(dst_vma, src_vma, dst_p4d, src_p4d,
1317                                    addr, next))
1318                         return -ENOMEM;
1319         } while (dst_p4d++, src_p4d++, addr = next, addr != end);
1320         return 0;
1321 }
1322
1323 /*
1324  * Return true if the vma needs to copy the pgtable during this fork().  Return
1325  * false when we can speed up fork() by allowing lazy page faults later until
1326  * when the child accesses the memory range.
1327  */
1328 static bool
1329 vma_needs_copy(struct vm_area_struct *dst_vma, struct vm_area_struct *src_vma)
1330 {
1331         /*
1332          * Always copy pgtables when dst_vma has uffd-wp enabled even if it's
1333          * file-backed (e.g. shmem). Because when uffd-wp is enabled, pgtable
1334          * contains uffd-wp protection information, that's something we can't
1335          * retrieve from page cache, and skip copying will lose those info.
1336          */
1337         if (userfaultfd_wp(dst_vma))
1338                 return true;
1339
1340         if (src_vma->vm_flags & (VM_PFNMAP | VM_MIXEDMAP))
1341                 return true;
1342
1343         if (src_vma->anon_vma)
1344                 return true;
1345
1346         /*
1347          * Don't copy ptes where a page fault will fill them correctly.  Fork
1348          * becomes much lighter when there are big shared or private readonly
1349          * mappings. The tradeoff is that copy_page_range is more efficient
1350          * than faulting.
1351          */
1352         return false;
1353 }
1354
1355 int
1356 copy_page_range(struct vm_area_struct *dst_vma, struct vm_area_struct *src_vma)
1357 {
1358         pgd_t *src_pgd, *dst_pgd;
1359         unsigned long next;
1360         unsigned long addr = src_vma->vm_start;
1361         unsigned long end = src_vma->vm_end;
1362         struct mm_struct *dst_mm = dst_vma->vm_mm;
1363         struct mm_struct *src_mm = src_vma->vm_mm;
1364         struct mmu_notifier_range range;
1365         bool is_cow;
1366         int ret;
1367
1368         if (!vma_needs_copy(dst_vma, src_vma))
1369                 return 0;
1370
1371         if (is_vm_hugetlb_page(src_vma))
1372                 return copy_hugetlb_page_range(dst_mm, src_mm, dst_vma, src_vma);
1373
1374         if (unlikely(src_vma->vm_flags & VM_PFNMAP)) {
1375                 /*
1376                  * We do not free on error cases below as remove_vma
1377                  * gets called on error from higher level routine
1378                  */
1379                 ret = track_pfn_copy(src_vma);
1380                 if (ret)
1381                         return ret;
1382         }
1383
1384         /*
1385          * We need to invalidate the secondary MMU mappings only when
1386          * there could be a permission downgrade on the ptes of the
1387          * parent mm. And a permission downgrade will only happen if
1388          * is_cow_mapping() returns true.
1389          */
1390         is_cow = is_cow_mapping(src_vma->vm_flags);
1391
1392         if (is_cow) {
1393                 mmu_notifier_range_init(&range, MMU_NOTIFY_PROTECTION_PAGE,
1394                                         0, src_mm, addr, end);
1395                 mmu_notifier_invalidate_range_start(&range);
1396                 /*
1397                  * Disabling preemption is not needed for the write side, as
1398                  * the read side doesn't spin, but goes to the mmap_lock.
1399                  *
1400                  * Use the raw variant of the seqcount_t write API to avoid
1401                  * lockdep complaining about preemptibility.
1402                  */
1403                 vma_assert_write_locked(src_vma);
1404                 raw_write_seqcount_begin(&src_mm->write_protect_seq);
1405         }
1406
1407         ret = 0;
1408         dst_pgd = pgd_offset(dst_mm, addr);
1409         src_pgd = pgd_offset(src_mm, addr);
1410         do {
1411                 next = pgd_addr_end(addr, end);
1412                 if (pgd_none_or_clear_bad(src_pgd))
1413                         continue;
1414                 if (unlikely(copy_p4d_range(dst_vma, src_vma, dst_pgd, src_pgd,
1415                                             addr, next))) {
1416                         untrack_pfn_clear(dst_vma);
1417                         ret = -ENOMEM;
1418                         break;
1419                 }
1420         } while (dst_pgd++, src_pgd++, addr = next, addr != end);
1421
1422         if (is_cow) {
1423                 raw_write_seqcount_end(&src_mm->write_protect_seq);
1424                 mmu_notifier_invalidate_range_end(&range);
1425         }
1426         return ret;
1427 }
1428
1429 /* Whether we should zap all COWed (private) pages too */
1430 static inline bool should_zap_cows(struct zap_details *details)
1431 {
1432         /* By default, zap all pages */
1433         if (!details)
1434                 return true;
1435
1436         /* Or, we zap COWed pages only if the caller wants to */
1437         return details->even_cows;
1438 }
1439
1440 /* Decides whether we should zap this folio with the folio pointer specified */
1441 static inline bool should_zap_folio(struct zap_details *details,
1442                                     struct folio *folio)
1443 {
1444         /* If we can make a decision without *folio.. */
1445         if (should_zap_cows(details))
1446                 return true;
1447
1448         /* Otherwise we should only zap non-anon folios */
1449         return !folio_test_anon(folio);
1450 }
1451
1452 static inline bool zap_drop_file_uffd_wp(struct zap_details *details)
1453 {
1454         if (!details)
1455                 return false;
1456
1457         return details->zap_flags & ZAP_FLAG_DROP_MARKER;
1458 }
1459
1460 /*
1461  * This function makes sure that we'll replace the none pte with an uffd-wp
1462  * swap special pte marker when necessary. Must be with the pgtable lock held.
1463  */
1464 static inline void
1465 zap_install_uffd_wp_if_needed(struct vm_area_struct *vma,
1466                               unsigned long addr, pte_t *pte, int nr,
1467                               struct zap_details *details, pte_t pteval)
1468 {
1469         /* Zap on anonymous always means dropping everything */
1470         if (vma_is_anonymous(vma))
1471                 return;
1472
1473         if (zap_drop_file_uffd_wp(details))
1474                 return;
1475
1476         for (;;) {
1477                 /* the PFN in the PTE is irrelevant. */
1478                 pte_install_uffd_wp_if_needed(vma, addr, pte, pteval);
1479                 if (--nr == 0)
1480                         break;
1481                 pte++;
1482                 addr += PAGE_SIZE;
1483         }
1484 }
1485
1486 static __always_inline void zap_present_folio_ptes(struct mmu_gather *tlb,
1487                 struct vm_area_struct *vma, struct folio *folio,
1488                 struct page *page, pte_t *pte, pte_t ptent, unsigned int nr,
1489                 unsigned long addr, struct zap_details *details, int *rss,
1490                 bool *force_flush, bool *force_break)
1491 {
1492         struct mm_struct *mm = tlb->mm;
1493         bool delay_rmap = false;
1494
1495         if (!folio_test_anon(folio)) {
1496                 ptent = get_and_clear_full_ptes(mm, addr, pte, nr, tlb->fullmm);
1497                 if (pte_dirty(ptent)) {
1498                         folio_mark_dirty(folio);
1499                         if (tlb_delay_rmap(tlb)) {
1500                                 delay_rmap = true;
1501                                 *force_flush = true;
1502                         }
1503                 }
1504                 if (pte_young(ptent) && likely(vma_has_recency(vma)))
1505                         folio_mark_accessed(folio);
1506                 rss[mm_counter(folio)] -= nr;
1507         } else {
1508                 /* We don't need up-to-date accessed/dirty bits. */
1509                 clear_full_ptes(mm, addr, pte, nr, tlb->fullmm);
1510                 rss[MM_ANONPAGES] -= nr;
1511         }
1512         /* Checking a single PTE in a batch is sufficient. */
1513         arch_check_zapped_pte(vma, ptent);
1514         tlb_remove_tlb_entries(tlb, pte, nr, addr);
1515         if (unlikely(userfaultfd_pte_wp(vma, ptent)))
1516                 zap_install_uffd_wp_if_needed(vma, addr, pte, nr, details,
1517                                               ptent);
1518
1519         if (!delay_rmap) {
1520                 folio_remove_rmap_ptes(folio, page, nr, vma);
1521
1522                 if (unlikely(folio_mapcount(folio) < 0))
1523                         print_bad_pte(vma, addr, ptent, page);
1524         }
1525         if (unlikely(__tlb_remove_folio_pages(tlb, page, nr, delay_rmap))) {
1526                 *force_flush = true;
1527                 *force_break = true;
1528         }
1529 }
1530
1531 /*
1532  * Zap or skip at least one present PTE, trying to batch-process subsequent
1533  * PTEs that map consecutive pages of the same folio.
1534  *
1535  * Returns the number of processed (skipped or zapped) PTEs (at least 1).
1536  */
1537 static inline int zap_present_ptes(struct mmu_gather *tlb,
1538                 struct vm_area_struct *vma, pte_t *pte, pte_t ptent,
1539                 unsigned int max_nr, unsigned long addr,
1540                 struct zap_details *details, int *rss, bool *force_flush,
1541                 bool *force_break)
1542 {
1543         const fpb_t fpb_flags = FPB_IGNORE_DIRTY | FPB_IGNORE_SOFT_DIRTY;
1544         struct mm_struct *mm = tlb->mm;
1545         struct folio *folio;
1546         struct page *page;
1547         int nr;
1548
1549         page = vm_normal_page(vma, addr, ptent);
1550         if (!page) {
1551                 /* We don't need up-to-date accessed/dirty bits. */
1552                 ptep_get_and_clear_full(mm, addr, pte, tlb->fullmm);
1553                 arch_check_zapped_pte(vma, ptent);
1554                 tlb_remove_tlb_entry(tlb, pte, addr);
1555                 if (userfaultfd_pte_wp(vma, ptent))
1556                         zap_install_uffd_wp_if_needed(vma, addr, pte, 1,
1557                                                       details, ptent);
1558                 ksm_might_unmap_zero_page(mm, ptent);
1559                 return 1;
1560         }
1561
1562         folio = page_folio(page);
1563         if (unlikely(!should_zap_folio(details, folio)))
1564                 return 1;
1565
1566         /*
1567          * Make sure that the common "small folio" case is as fast as possible
1568          * by keeping the batching logic separate.
1569          */
1570         if (unlikely(folio_test_large(folio) && max_nr != 1)) {
1571                 nr = folio_pte_batch(folio, addr, pte, ptent, max_nr, fpb_flags,
1572                                      NULL, NULL, NULL);
1573
1574                 zap_present_folio_ptes(tlb, vma, folio, page, pte, ptent, nr,
1575                                        addr, details, rss, force_flush,
1576                                        force_break);
1577                 return nr;
1578         }
1579         zap_present_folio_ptes(tlb, vma, folio, page, pte, ptent, 1, addr,
1580                                details, rss, force_flush, force_break);
1581         return 1;
1582 }
1583
1584 static unsigned long zap_pte_range(struct mmu_gather *tlb,
1585                                 struct vm_area_struct *vma, pmd_t *pmd,
1586                                 unsigned long addr, unsigned long end,
1587                                 struct zap_details *details)
1588 {
1589         bool force_flush = false, force_break = false;
1590         struct mm_struct *mm = tlb->mm;
1591         int rss[NR_MM_COUNTERS];
1592         spinlock_t *ptl;
1593         pte_t *start_pte;
1594         pte_t *pte;
1595         swp_entry_t entry;
1596         int nr;
1597
1598         tlb_change_page_size(tlb, PAGE_SIZE);
1599         init_rss_vec(rss);
1600         start_pte = pte = pte_offset_map_lock(mm, pmd, addr, &ptl);
1601         if (!pte)
1602                 return addr;
1603
1604         flush_tlb_batched_pending(mm);
1605         arch_enter_lazy_mmu_mode();
1606         do {
1607                 pte_t ptent = ptep_get(pte);
1608                 struct folio *folio;
1609                 struct page *page;
1610                 int max_nr;
1611
1612                 nr = 1;
1613                 if (pte_none(ptent))
1614                         continue;
1615
1616                 if (need_resched())
1617                         break;
1618
1619                 if (pte_present(ptent)) {
1620                         max_nr = (end - addr) / PAGE_SIZE;
1621                         nr = zap_present_ptes(tlb, vma, pte, ptent, max_nr,
1622                                               addr, details, rss, &force_flush,
1623                                               &force_break);
1624                         if (unlikely(force_break)) {
1625                                 addr += nr * PAGE_SIZE;
1626                                 break;
1627                         }
1628                         continue;
1629                 }
1630
1631                 entry = pte_to_swp_entry(ptent);
1632                 if (is_device_private_entry(entry) ||
1633                     is_device_exclusive_entry(entry)) {
1634                         page = pfn_swap_entry_to_page(entry);
1635                         folio = page_folio(page);
1636                         if (unlikely(!should_zap_folio(details, folio)))
1637                                 continue;
1638                         /*
1639                          * Both device private/exclusive mappings should only
1640                          * work with anonymous page so far, so we don't need to
1641                          * consider uffd-wp bit when zap. For more information,
1642                          * see zap_install_uffd_wp_if_needed().
1643                          */
1644                         WARN_ON_ONCE(!vma_is_anonymous(vma));
1645                         rss[mm_counter(folio)]--;
1646                         if (is_device_private_entry(entry))
1647                                 folio_remove_rmap_pte(folio, page, vma);
1648                         folio_put(folio);
1649                 } else if (!non_swap_entry(entry)) {
1650                         max_nr = (end - addr) / PAGE_SIZE;
1651                         nr = swap_pte_batch(pte, max_nr, ptent);
1652                         /* Genuine swap entries, hence a private anon pages */
1653                         if (!should_zap_cows(details))
1654                                 continue;
1655                         rss[MM_SWAPENTS] -= nr;
1656                         free_swap_and_cache_nr(entry, nr);
1657                 } else if (is_migration_entry(entry)) {
1658                         folio = pfn_swap_entry_folio(entry);
1659                         if (!should_zap_folio(details, folio))
1660                                 continue;
1661                         rss[mm_counter(folio)]--;
1662                 } else if (pte_marker_entry_uffd_wp(entry)) {
1663                         /*
1664                          * For anon: always drop the marker; for file: only
1665                          * drop the marker if explicitly requested.
1666                          */
1667                         if (!vma_is_anonymous(vma) &&
1668                             !zap_drop_file_uffd_wp(details))
1669                                 continue;
1670                 } else if (is_hwpoison_entry(entry) ||
1671                            is_poisoned_swp_entry(entry)) {
1672                         if (!should_zap_cows(details))
1673                                 continue;
1674                 } else {
1675                         /* We should have covered all the swap entry types */
1676                         pr_alert("unrecognized swap entry 0x%lx\n", entry.val);
1677                         WARN_ON_ONCE(1);
1678                 }
1679                 clear_not_present_full_ptes(mm, addr, pte, nr, tlb->fullmm);
1680                 zap_install_uffd_wp_if_needed(vma, addr, pte, nr, details, ptent);
1681         } while (pte += nr, addr += PAGE_SIZE * nr, addr != end);
1682
1683         add_mm_rss_vec(mm, rss);
1684         arch_leave_lazy_mmu_mode();
1685
1686         /* Do the actual TLB flush before dropping ptl */
1687         if (force_flush) {
1688                 tlb_flush_mmu_tlbonly(tlb);
1689                 tlb_flush_rmaps(tlb, vma);
1690         }
1691         pte_unmap_unlock(start_pte, ptl);
1692
1693         /*
1694          * If we forced a TLB flush (either due to running out of
1695          * batch buffers or because we needed to flush dirty TLB
1696          * entries before releasing the ptl), free the batched
1697          * memory too. Come back again if we didn't do everything.
1698          */
1699         if (force_flush)
1700                 tlb_flush_mmu(tlb);
1701
1702         return addr;
1703 }
1704
1705 static inline unsigned long zap_pmd_range(struct mmu_gather *tlb,
1706                                 struct vm_area_struct *vma, pud_t *pud,
1707                                 unsigned long addr, unsigned long end,
1708                                 struct zap_details *details)
1709 {
1710         pmd_t *pmd;
1711         unsigned long next;
1712
1713         pmd = pmd_offset(pud, addr);
1714         do {
1715                 next = pmd_addr_end(addr, end);
1716                 if (is_swap_pmd(*pmd) || pmd_trans_huge(*pmd) || pmd_devmap(*pmd)) {
1717                         if (next - addr != HPAGE_PMD_SIZE)
1718                                 __split_huge_pmd(vma, pmd, addr, false, NULL);
1719                         else if (zap_huge_pmd(tlb, vma, pmd, addr)) {
1720                                 addr = next;
1721                                 continue;
1722                         }
1723                         /* fall through */
1724                 } else if (details && details->single_folio &&
1725                            folio_test_pmd_mappable(details->single_folio) &&
1726                            next - addr == HPAGE_PMD_SIZE && pmd_none(*pmd)) {
1727                         spinlock_t *ptl = pmd_lock(tlb->mm, pmd);
1728                         /*
1729                          * Take and drop THP pmd lock so that we cannot return
1730                          * prematurely, while zap_huge_pmd() has cleared *pmd,
1731                          * but not yet decremented compound_mapcount().
1732                          */
1733                         spin_unlock(ptl);
1734                 }
1735                 if (pmd_none(*pmd)) {
1736                         addr = next;
1737                         continue;
1738                 }
1739                 addr = zap_pte_range(tlb, vma, pmd, addr, next, details);
1740                 if (addr != next)
1741                         pmd--;
1742         } while (pmd++, cond_resched(), addr != end);
1743
1744         return addr;
1745 }
1746
1747 static inline unsigned long zap_pud_range(struct mmu_gather *tlb,
1748                                 struct vm_area_struct *vma, p4d_t *p4d,
1749                                 unsigned long addr, unsigned long end,
1750                                 struct zap_details *details)
1751 {
1752         pud_t *pud;
1753         unsigned long next;
1754
1755         pud = pud_offset(p4d, addr);
1756         do {
1757                 next = pud_addr_end(addr, end);
1758                 if (pud_trans_huge(*pud) || pud_devmap(*pud)) {
1759                         if (next - addr != HPAGE_PUD_SIZE) {
1760                                 mmap_assert_locked(tlb->mm);
1761                                 split_huge_pud(vma, pud, addr);
1762                         } else if (zap_huge_pud(tlb, vma, pud, addr))
1763                                 goto next;
1764                         /* fall through */
1765                 }
1766                 if (pud_none_or_clear_bad(pud))
1767                         continue;
1768                 next = zap_pmd_range(tlb, vma, pud, addr, next, details);
1769 next:
1770                 cond_resched();
1771         } while (pud++, addr = next, addr != end);
1772
1773         return addr;
1774 }
1775
1776 static inline unsigned long zap_p4d_range(struct mmu_gather *tlb,
1777                                 struct vm_area_struct *vma, pgd_t *pgd,
1778                                 unsigned long addr, unsigned long end,
1779                                 struct zap_details *details)
1780 {
1781         p4d_t *p4d;
1782         unsigned long next;
1783
1784         p4d = p4d_offset(pgd, addr);
1785         do {
1786                 next = p4d_addr_end(addr, end);
1787                 if (p4d_none_or_clear_bad(p4d))
1788                         continue;
1789                 next = zap_pud_range(tlb, vma, p4d, addr, next, details);
1790         } while (p4d++, addr = next, addr != end);
1791
1792         return addr;
1793 }
1794
1795 void unmap_page_range(struct mmu_gather *tlb,
1796                              struct vm_area_struct *vma,
1797                              unsigned long addr, unsigned long end,
1798                              struct zap_details *details)
1799 {
1800         pgd_t *pgd;
1801         unsigned long next;
1802
1803         BUG_ON(addr >= end);
1804         tlb_start_vma(tlb, vma);
1805         pgd = pgd_offset(vma->vm_mm, addr);
1806         do {
1807                 next = pgd_addr_end(addr, end);
1808                 if (pgd_none_or_clear_bad(pgd))
1809                         continue;
1810                 next = zap_p4d_range(tlb, vma, pgd, addr, next, details);
1811         } while (pgd++, addr = next, addr != end);
1812         tlb_end_vma(tlb, vma);
1813 }
1814
1815
1816 static void unmap_single_vma(struct mmu_gather *tlb,
1817                 struct vm_area_struct *vma, unsigned long start_addr,
1818                 unsigned long end_addr,
1819                 struct zap_details *details, bool mm_wr_locked)
1820 {
1821         unsigned long start = max(vma->vm_start, start_addr);
1822         unsigned long end;
1823
1824         if (start >= vma->vm_end)
1825                 return;
1826         end = min(vma->vm_end, end_addr);
1827         if (end <= vma->vm_start)
1828                 return;
1829
1830         if (vma->vm_file)
1831                 uprobe_munmap(vma, start, end);
1832
1833         if (unlikely(vma->vm_flags & VM_PFNMAP))
1834                 untrack_pfn(vma, 0, 0, mm_wr_locked);
1835
1836         if (start != end) {
1837                 if (unlikely(is_vm_hugetlb_page(vma))) {
1838                         /*
1839                          * It is undesirable to test vma->vm_file as it
1840                          * should be non-null for valid hugetlb area.
1841                          * However, vm_file will be NULL in the error
1842                          * cleanup path of mmap_region. When
1843                          * hugetlbfs ->mmap method fails,
1844                          * mmap_region() nullifies vma->vm_file
1845                          * before calling this function to clean up.
1846                          * Since no pte has actually been setup, it is
1847                          * safe to do nothing in this case.
1848                          */
1849                         if (vma->vm_file) {
1850                                 zap_flags_t zap_flags = details ?
1851                                     details->zap_flags : 0;
1852                                 __unmap_hugepage_range(tlb, vma, start, end,
1853                                                              NULL, zap_flags);
1854                         }
1855                 } else
1856                         unmap_page_range(tlb, vma, start, end, details);
1857         }
1858 }
1859
1860 /**
1861  * unmap_vmas - unmap a range of memory covered by a list of vma's
1862  * @tlb: address of the caller's struct mmu_gather
1863  * @mas: the maple state
1864  * @vma: the starting vma
1865  * @start_addr: virtual address at which to start unmapping
1866  * @end_addr: virtual address at which to end unmapping
1867  * @tree_end: The maximum index to check
1868  * @mm_wr_locked: lock flag
1869  *
1870  * Unmap all pages in the vma list.
1871  *
1872  * Only addresses between `start' and `end' will be unmapped.
1873  *
1874  * The VMA list must be sorted in ascending virtual address order.
1875  *
1876  * unmap_vmas() assumes that the caller will flush the whole unmapped address
1877  * range after unmap_vmas() returns.  So the only responsibility here is to
1878  * ensure that any thus-far unmapped pages are flushed before unmap_vmas()
1879  * drops the lock and schedules.
1880  */
1881 void unmap_vmas(struct mmu_gather *tlb, struct ma_state *mas,
1882                 struct vm_area_struct *vma, unsigned long start_addr,
1883                 unsigned long end_addr, unsigned long tree_end,
1884                 bool mm_wr_locked)
1885 {
1886         struct mmu_notifier_range range;
1887         struct zap_details details = {
1888                 .zap_flags = ZAP_FLAG_DROP_MARKER | ZAP_FLAG_UNMAP,
1889                 /* Careful - we need to zap private pages too! */
1890                 .even_cows = true,
1891         };
1892
1893         mmu_notifier_range_init(&range, MMU_NOTIFY_UNMAP, 0, vma->vm_mm,
1894                                 start_addr, end_addr);
1895         mmu_notifier_invalidate_range_start(&range);
1896         do {
1897                 unsigned long start = start_addr;
1898                 unsigned long end = end_addr;
1899                 hugetlb_zap_begin(vma, &start, &end);
1900                 unmap_single_vma(tlb, vma, start, end, &details,
1901                                  mm_wr_locked);
1902                 hugetlb_zap_end(vma, &details);
1903                 vma = mas_find(mas, tree_end - 1);
1904         } while (vma && likely(!xa_is_zero(vma)));
1905         mmu_notifier_invalidate_range_end(&range);
1906 }
1907
1908 /**
1909  * zap_page_range_single - remove user pages in a given range
1910  * @vma: vm_area_struct holding the applicable pages
1911  * @address: starting address of pages to zap
1912  * @size: number of bytes to zap
1913  * @details: details of shared cache invalidation
1914  *
1915  * The range must fit into one VMA.
1916  */
1917 void zap_page_range_single(struct vm_area_struct *vma, unsigned long address,
1918                 unsigned long size, struct zap_details *details)
1919 {
1920         const unsigned long end = address + size;
1921         struct mmu_notifier_range range;
1922         struct mmu_gather tlb;
1923
1924         lru_add_drain();
1925         mmu_notifier_range_init(&range, MMU_NOTIFY_CLEAR, 0, vma->vm_mm,
1926                                 address, end);
1927         hugetlb_zap_begin(vma, &range.start, &range.end);
1928         tlb_gather_mmu(&tlb, vma->vm_mm);
1929         update_hiwater_rss(vma->vm_mm);
1930         mmu_notifier_invalidate_range_start(&range);
1931         /*
1932          * unmap 'address-end' not 'range.start-range.end' as range
1933          * could have been expanded for hugetlb pmd sharing.
1934          */
1935         unmap_single_vma(&tlb, vma, address, end, details, false);
1936         mmu_notifier_invalidate_range_end(&range);
1937         tlb_finish_mmu(&tlb);
1938         hugetlb_zap_end(vma, details);
1939 }
1940
1941 /**
1942  * zap_vma_ptes - remove ptes mapping the vma
1943  * @vma: vm_area_struct holding ptes to be zapped
1944  * @address: starting address of pages to zap
1945  * @size: number of bytes to zap
1946  *
1947  * This function only unmaps ptes assigned to VM_PFNMAP vmas.
1948  *
1949  * The entire address range must be fully contained within the vma.
1950  *
1951  */
1952 void zap_vma_ptes(struct vm_area_struct *vma, unsigned long address,
1953                 unsigned long size)
1954 {
1955         if (!range_in_vma(vma, address, address + size) ||
1956                         !(vma->vm_flags & VM_PFNMAP))
1957                 return;
1958
1959         zap_page_range_single(vma, address, size, NULL);
1960 }
1961 EXPORT_SYMBOL_GPL(zap_vma_ptes);
1962
1963 static pmd_t *walk_to_pmd(struct mm_struct *mm, unsigned long addr)
1964 {
1965         pgd_t *pgd;
1966         p4d_t *p4d;
1967         pud_t *pud;
1968         pmd_t *pmd;
1969
1970         pgd = pgd_offset(mm, addr);
1971         p4d = p4d_alloc(mm, pgd, addr);
1972         if (!p4d)
1973                 return NULL;
1974         pud = pud_alloc(mm, p4d, addr);
1975         if (!pud)
1976                 return NULL;
1977         pmd = pmd_alloc(mm, pud, addr);
1978         if (!pmd)
1979                 return NULL;
1980
1981         VM_BUG_ON(pmd_trans_huge(*pmd));
1982         return pmd;
1983 }
1984
1985 pte_t *__get_locked_pte(struct mm_struct *mm, unsigned long addr,
1986                         spinlock_t **ptl)
1987 {
1988         pmd_t *pmd = walk_to_pmd(mm, addr);
1989
1990         if (!pmd)
1991                 return NULL;
1992         return pte_alloc_map_lock(mm, pmd, addr, ptl);
1993 }
1994
1995 static bool vm_mixed_zeropage_allowed(struct vm_area_struct *vma)
1996 {
1997         VM_WARN_ON_ONCE(vma->vm_flags & VM_PFNMAP);
1998         /*
1999          * Whoever wants to forbid the zeropage after some zeropages
2000          * might already have been mapped has to scan the page tables and
2001          * bail out on any zeropages. Zeropages in COW mappings can
2002          * be unshared using FAULT_FLAG_UNSHARE faults.
2003          */
2004         if (mm_forbids_zeropage(vma->vm_mm))
2005                 return false;
2006         /* zeropages in COW mappings are common and unproblematic. */
2007         if (is_cow_mapping(vma->vm_flags))
2008                 return true;
2009         /* Mappings that do not allow for writable PTEs are unproblematic. */
2010         if (!(vma->vm_flags & (VM_WRITE | VM_MAYWRITE)))
2011                 return true;
2012         /*
2013          * Why not allow any VMA that has vm_ops->pfn_mkwrite? GUP could
2014          * find the shared zeropage and longterm-pin it, which would
2015          * be problematic as soon as the zeropage gets replaced by a different
2016          * page due to vma->vm_ops->pfn_mkwrite, because what's mapped would
2017          * now differ to what GUP looked up. FSDAX is incompatible to
2018          * FOLL_LONGTERM and VM_IO is incompatible to GUP completely (see
2019          * check_vma_flags).
2020          */
2021         return vma->vm_ops && vma->vm_ops->pfn_mkwrite &&
2022                (vma_is_fsdax(vma) || vma->vm_flags & VM_IO);
2023 }
2024
2025 static int validate_page_before_insert(struct vm_area_struct *vma,
2026                                        struct page *page)
2027 {
2028         struct folio *folio = page_folio(page);
2029
2030         if (!folio_ref_count(folio))
2031                 return -EINVAL;
2032         if (unlikely(is_zero_folio(folio))) {
2033                 if (!vm_mixed_zeropage_allowed(vma))
2034                         return -EINVAL;
2035                 return 0;
2036         }
2037         if (folio_test_anon(folio) || folio_test_slab(folio) ||
2038             page_has_type(page))
2039                 return -EINVAL;
2040         flush_dcache_folio(folio);
2041         return 0;
2042 }
2043
2044 static int insert_page_into_pte_locked(struct vm_area_struct *vma, pte_t *pte,
2045                         unsigned long addr, struct page *page, pgprot_t prot)
2046 {
2047         struct folio *folio = page_folio(page);
2048         pte_t pteval;
2049
2050         if (!pte_none(ptep_get(pte)))
2051                 return -EBUSY;
2052         /* Ok, finally just insert the thing.. */
2053         pteval = mk_pte(page, prot);
2054         if (unlikely(is_zero_folio(folio))) {
2055                 pteval = pte_mkspecial(pteval);
2056         } else {
2057                 folio_get(folio);
2058                 inc_mm_counter(vma->vm_mm, mm_counter_file(folio));
2059                 folio_add_file_rmap_pte(folio, page, vma);
2060         }
2061         set_pte_at(vma->vm_mm, addr, pte, pteval);
2062         return 0;
2063 }
2064
2065 static int insert_page(struct vm_area_struct *vma, unsigned long addr,
2066                         struct page *page, pgprot_t prot)
2067 {
2068         int retval;
2069         pte_t *pte;
2070         spinlock_t *ptl;
2071
2072         retval = validate_page_before_insert(vma, page);
2073         if (retval)
2074                 goto out;
2075         retval = -ENOMEM;
2076         pte = get_locked_pte(vma->vm_mm, addr, &ptl);
2077         if (!pte)
2078                 goto out;
2079         retval = insert_page_into_pte_locked(vma, pte, addr, page, prot);
2080         pte_unmap_unlock(pte, ptl);
2081 out:
2082         return retval;
2083 }
2084
2085 static int insert_page_in_batch_locked(struct vm_area_struct *vma, pte_t *pte,
2086                         unsigned long addr, struct page *page, pgprot_t prot)
2087 {
2088         int err;
2089
2090         err = validate_page_before_insert(vma, page);
2091         if (err)
2092                 return err;
2093         return insert_page_into_pte_locked(vma, pte, addr, page, prot);
2094 }
2095
2096 /* insert_pages() amortizes the cost of spinlock operations
2097  * when inserting pages in a loop.
2098  */
2099 static int insert_pages(struct vm_area_struct *vma, unsigned long addr,
2100                         struct page **pages, unsigned long *num, pgprot_t prot)
2101 {
2102         pmd_t *pmd = NULL;
2103         pte_t *start_pte, *pte;
2104         spinlock_t *pte_lock;
2105         struct mm_struct *const mm = vma->vm_mm;
2106         unsigned long curr_page_idx = 0;
2107         unsigned long remaining_pages_total = *num;
2108         unsigned long pages_to_write_in_pmd;
2109         int ret;
2110 more:
2111         ret = -EFAULT;
2112         pmd = walk_to_pmd(mm, addr);
2113         if (!pmd)
2114                 goto out;
2115
2116         pages_to_write_in_pmd = min_t(unsigned long,
2117                 remaining_pages_total, PTRS_PER_PTE - pte_index(addr));
2118
2119         /* Allocate the PTE if necessary; takes PMD lock once only. */
2120         ret = -ENOMEM;
2121         if (pte_alloc(mm, pmd))
2122                 goto out;
2123
2124         while (pages_to_write_in_pmd) {
2125                 int pte_idx = 0;
2126                 const int batch_size = min_t(int, pages_to_write_in_pmd, 8);
2127
2128                 start_pte = pte_offset_map_lock(mm, pmd, addr, &pte_lock);
2129                 if (!start_pte) {
2130                         ret = -EFAULT;
2131                         goto out;
2132                 }
2133                 for (pte = start_pte; pte_idx < batch_size; ++pte, ++pte_idx) {
2134                         int err = insert_page_in_batch_locked(vma, pte,
2135                                 addr, pages[curr_page_idx], prot);
2136                         if (unlikely(err)) {
2137                                 pte_unmap_unlock(start_pte, pte_lock);
2138                                 ret = err;
2139                                 remaining_pages_total -= pte_idx;
2140                                 goto out;
2141                         }
2142                         addr += PAGE_SIZE;
2143                         ++curr_page_idx;
2144                 }
2145                 pte_unmap_unlock(start_pte, pte_lock);
2146                 pages_to_write_in_pmd -= batch_size;
2147                 remaining_pages_total -= batch_size;
2148         }
2149         if (remaining_pages_total)
2150                 goto more;
2151         ret = 0;
2152 out:
2153         *num = remaining_pages_total;
2154         return ret;
2155 }
2156
2157 /**
2158  * vm_insert_pages - insert multiple pages into user vma, batching the pmd lock.
2159  * @vma: user vma to map to
2160  * @addr: target start user address of these pages
2161  * @pages: source kernel pages
2162  * @num: in: number of pages to map. out: number of pages that were *not*
2163  * mapped. (0 means all pages were successfully mapped).
2164  *
2165  * Preferred over vm_insert_page() when inserting multiple pages.
2166  *
2167  * In case of error, we may have mapped a subset of the provided
2168  * pages. It is the caller's responsibility to account for this case.
2169  *
2170  * The same restrictions apply as in vm_insert_page().
2171  */
2172 int vm_insert_pages(struct vm_area_struct *vma, unsigned long addr,
2173                         struct page **pages, unsigned long *num)
2174 {
2175         const unsigned long end_addr = addr + (*num * PAGE_SIZE) - 1;
2176
2177         if (addr < vma->vm_start || end_addr >= vma->vm_end)
2178                 return -EFAULT;
2179         if (!(vma->vm_flags & VM_MIXEDMAP)) {
2180                 BUG_ON(mmap_read_trylock(vma->vm_mm));
2181                 BUG_ON(vma->vm_flags & VM_PFNMAP);
2182                 vm_flags_set(vma, VM_MIXEDMAP);
2183         }
2184         /* Defer page refcount checking till we're about to map that page. */
2185         return insert_pages(vma, addr, pages, num, vma->vm_page_prot);
2186 }
2187 EXPORT_SYMBOL(vm_insert_pages);
2188
2189 /**
2190  * vm_insert_page - insert single page into user vma
2191  * @vma: user vma to map to
2192  * @addr: target user address of this page
2193  * @page: source kernel page
2194  *
2195  * This allows drivers to insert individual pages they've allocated
2196  * into a user vma. The zeropage is supported in some VMAs,
2197  * see vm_mixed_zeropage_allowed().
2198  *
2199  * The page has to be a nice clean _individual_ kernel allocation.
2200  * If you allocate a compound page, you need to have marked it as
2201  * such (__GFP_COMP), or manually just split the page up yourself
2202  * (see split_page()).
2203  *
2204  * NOTE! Traditionally this was done with "remap_pfn_range()" which
2205  * took an arbitrary page protection parameter. This doesn't allow
2206  * that. Your vma protection will have to be set up correctly, which
2207  * means that if you want a shared writable mapping, you'd better
2208  * ask for a shared writable mapping!
2209  *
2210  * The page does not need to be reserved.
2211  *
2212  * Usually this function is called from f_op->mmap() handler
2213  * under mm->mmap_lock write-lock, so it can change vma->vm_flags.
2214  * Caller must set VM_MIXEDMAP on vma if it wants to call this
2215  * function from other places, for example from page-fault handler.
2216  *
2217  * Return: %0 on success, negative error code otherwise.
2218  */
2219 int vm_insert_page(struct vm_area_struct *vma, unsigned long addr,
2220                         struct page *page)
2221 {
2222         if (addr < vma->vm_start || addr >= vma->vm_end)
2223                 return -EFAULT;
2224         if (!(vma->vm_flags & VM_MIXEDMAP)) {
2225                 BUG_ON(mmap_read_trylock(vma->vm_mm));
2226                 BUG_ON(vma->vm_flags & VM_PFNMAP);
2227                 vm_flags_set(vma, VM_MIXEDMAP);
2228         }
2229         return insert_page(vma, addr, page, vma->vm_page_prot);
2230 }
2231 EXPORT_SYMBOL(vm_insert_page);
2232
2233 /*
2234  * __vm_map_pages - maps range of kernel pages into user vma
2235  * @vma: user vma to map to
2236  * @pages: pointer to array of source kernel pages
2237  * @num: number of pages in page array
2238  * @offset: user's requested vm_pgoff
2239  *
2240  * This allows drivers to map range of kernel pages into a user vma.
2241  * The zeropage is supported in some VMAs, see
2242  * vm_mixed_zeropage_allowed().
2243  *
2244  * Return: 0 on success and error code otherwise.
2245  */
2246 static int __vm_map_pages(struct vm_area_struct *vma, struct page **pages,
2247                                 unsigned long num, unsigned long offset)
2248 {
2249         unsigned long count = vma_pages(vma);
2250         unsigned long uaddr = vma->vm_start;
2251         int ret, i;
2252
2253         /* Fail if the user requested offset is beyond the end of the object */
2254         if (offset >= num)
2255                 return -ENXIO;
2256
2257         /* Fail if the user requested size exceeds available object size */
2258         if (count > num - offset)
2259                 return -ENXIO;
2260
2261         for (i = 0; i < count; i++) {
2262                 ret = vm_insert_page(vma, uaddr, pages[offset + i]);
2263                 if (ret < 0)
2264                         return ret;
2265                 uaddr += PAGE_SIZE;
2266         }
2267
2268         return 0;
2269 }
2270
2271 /**
2272  * vm_map_pages - maps range of kernel pages starts with non zero offset
2273  * @vma: user vma to map to
2274  * @pages: pointer to array of source kernel pages
2275  * @num: number of pages in page array
2276  *
2277  * Maps an object consisting of @num pages, catering for the user's
2278  * requested vm_pgoff
2279  *
2280  * If we fail to insert any page into the vma, the function will return
2281  * immediately leaving any previously inserted pages present.  Callers
2282  * from the mmap handler may immediately return the error as their caller
2283  * will destroy the vma, removing any successfully inserted pages. Other
2284  * callers should make their own arrangements for calling unmap_region().
2285  *
2286  * Context: Process context. Called by mmap handlers.
2287  * Return: 0 on success and error code otherwise.
2288  */
2289 int vm_map_pages(struct vm_area_struct *vma, struct page **pages,
2290                                 unsigned long num)
2291 {
2292         return __vm_map_pages(vma, pages, num, vma->vm_pgoff);
2293 }
2294 EXPORT_SYMBOL(vm_map_pages);
2295
2296 /**
2297  * vm_map_pages_zero - map range of kernel pages starts with zero offset
2298  * @vma: user vma to map to
2299  * @pages: pointer to array of source kernel pages
2300  * @num: number of pages in page array
2301  *
2302  * Similar to vm_map_pages(), except that it explicitly sets the offset
2303  * to 0. This function is intended for the drivers that did not consider
2304  * vm_pgoff.
2305  *
2306  * Context: Process context. Called by mmap handlers.
2307  * Return: 0 on success and error code otherwise.
2308  */
2309 int vm_map_pages_zero(struct vm_area_struct *vma, struct page **pages,
2310                                 unsigned long num)
2311 {
2312         return __vm_map_pages(vma, pages, num, 0);
2313 }
2314 EXPORT_SYMBOL(vm_map_pages_zero);
2315
2316 static vm_fault_t insert_pfn(struct vm_area_struct *vma, unsigned long addr,
2317                         pfn_t pfn, pgprot_t prot, bool mkwrite)
2318 {
2319         struct mm_struct *mm = vma->vm_mm;
2320         pte_t *pte, entry;
2321         spinlock_t *ptl;
2322
2323         pte = get_locked_pte(mm, addr, &ptl);
2324         if (!pte)
2325                 return VM_FAULT_OOM;
2326         entry = ptep_get(pte);
2327         if (!pte_none(entry)) {
2328                 if (mkwrite) {
2329                         /*
2330                          * For read faults on private mappings the PFN passed
2331                          * in may not match the PFN we have mapped if the
2332                          * mapped PFN is a writeable COW page.  In the mkwrite
2333                          * case we are creating a writable PTE for a shared
2334                          * mapping and we expect the PFNs to match. If they
2335                          * don't match, we are likely racing with block
2336                          * allocation and mapping invalidation so just skip the
2337                          * update.
2338                          */
2339                         if (pte_pfn(entry) != pfn_t_to_pfn(pfn)) {
2340                                 WARN_ON_ONCE(!is_zero_pfn(pte_pfn(entry)));
2341                                 goto out_unlock;
2342                         }
2343                         entry = pte_mkyoung(entry);
2344                         entry = maybe_mkwrite(pte_mkdirty(entry), vma);
2345                         if (ptep_set_access_flags(vma, addr, pte, entry, 1))
2346                                 update_mmu_cache(vma, addr, pte);
2347                 }
2348                 goto out_unlock;
2349         }
2350
2351         /* Ok, finally just insert the thing.. */
2352         if (pfn_t_devmap(pfn))
2353                 entry = pte_mkdevmap(pfn_t_pte(pfn, prot));
2354         else
2355                 entry = pte_mkspecial(pfn_t_pte(pfn, prot));
2356
2357         if (mkwrite) {
2358                 entry = pte_mkyoung(entry);
2359                 entry = maybe_mkwrite(pte_mkdirty(entry), vma);
2360         }
2361
2362         set_pte_at(mm, addr, pte, entry);
2363         update_mmu_cache(vma, addr, pte); /* XXX: why not for insert_page? */
2364
2365 out_unlock:
2366         pte_unmap_unlock(pte, ptl);
2367         return VM_FAULT_NOPAGE;
2368 }
2369
2370 /**
2371  * vmf_insert_pfn_prot - insert single pfn into user vma with specified pgprot
2372  * @vma: user vma to map to
2373  * @addr: target user address of this page
2374  * @pfn: source kernel pfn
2375  * @pgprot: pgprot flags for the inserted page
2376  *
2377  * This is exactly like vmf_insert_pfn(), except that it allows drivers
2378  * to override pgprot on a per-page basis.
2379  *
2380  * This only makes sense for IO mappings, and it makes no sense for
2381  * COW mappings.  In general, using multiple vmas is preferable;
2382  * vmf_insert_pfn_prot should only be used if using multiple VMAs is
2383  * impractical.
2384  *
2385  * pgprot typically only differs from @vma->vm_page_prot when drivers set
2386  * caching- and encryption bits different than those of @vma->vm_page_prot,
2387  * because the caching- or encryption mode may not be known at mmap() time.
2388  *
2389  * This is ok as long as @vma->vm_page_prot is not used by the core vm
2390  * to set caching and encryption bits for those vmas (except for COW pages).
2391  * This is ensured by core vm only modifying these page table entries using
2392  * functions that don't touch caching- or encryption bits, using pte_modify()
2393  * if needed. (See for example mprotect()).
2394  *
2395  * Also when new page-table entries are created, this is only done using the
2396  * fault() callback, and never using the value of vma->vm_page_prot,
2397  * except for page-table entries that point to anonymous pages as the result
2398  * of COW.
2399  *
2400  * Context: Process context.  May allocate using %GFP_KERNEL.
2401  * Return: vm_fault_t value.
2402  */
2403 vm_fault_t vmf_insert_pfn_prot(struct vm_area_struct *vma, unsigned long addr,
2404                         unsigned long pfn, pgprot_t pgprot)
2405 {
2406         /*
2407          * Technically, architectures with pte_special can avoid all these
2408          * restrictions (same for remap_pfn_range).  However we would like
2409          * consistency in testing and feature parity among all, so we should
2410          * try to keep these invariants in place for everybody.
2411          */
2412         BUG_ON(!(vma->vm_flags & (VM_PFNMAP|VM_MIXEDMAP)));
2413         BUG_ON((vma->vm_flags & (VM_PFNMAP|VM_MIXEDMAP)) ==
2414                                                 (VM_PFNMAP|VM_MIXEDMAP));
2415         BUG_ON((vma->vm_flags & VM_PFNMAP) && is_cow_mapping(vma->vm_flags));
2416         BUG_ON((vma->vm_flags & VM_MIXEDMAP) && pfn_valid(pfn));
2417
2418         if (addr < vma->vm_start || addr >= vma->vm_end)
2419                 return VM_FAULT_SIGBUS;
2420
2421         if (!pfn_modify_allowed(pfn, pgprot))
2422                 return VM_FAULT_SIGBUS;
2423
2424         track_pfn_insert(vma, &pgprot, __pfn_to_pfn_t(pfn, PFN_DEV));
2425
2426         return insert_pfn(vma, addr, __pfn_to_pfn_t(pfn, PFN_DEV), pgprot,
2427                         false);
2428 }
2429 EXPORT_SYMBOL(vmf_insert_pfn_prot);
2430
2431 /**
2432  * vmf_insert_pfn - insert single pfn into user vma
2433  * @vma: user vma to map to
2434  * @addr: target user address of this page
2435  * @pfn: source kernel pfn
2436  *
2437  * Similar to vm_insert_page, this allows drivers to insert individual pages
2438  * they've allocated into a user vma. Same comments apply.
2439  *
2440  * This function should only be called from a vm_ops->fault handler, and
2441  * in that case the handler should return the result of this function.
2442  *
2443  * vma cannot be a COW mapping.
2444  *
2445  * As this is called only for pages that do not currently exist, we
2446  * do not need to flush old virtual caches or the TLB.
2447  *
2448  * Context: Process context.  May allocate using %GFP_KERNEL.
2449  * Return: vm_fault_t value.
2450  */
2451 vm_fault_t vmf_insert_pfn(struct vm_area_struct *vma, unsigned long addr,
2452                         unsigned long pfn)
2453 {
2454         return vmf_insert_pfn_prot(vma, addr, pfn, vma->vm_page_prot);
2455 }
2456 EXPORT_SYMBOL(vmf_insert_pfn);
2457
2458 static bool vm_mixed_ok(struct vm_area_struct *vma, pfn_t pfn, bool mkwrite)
2459 {
2460         if (unlikely(is_zero_pfn(pfn_t_to_pfn(pfn))) &&
2461             (mkwrite || !vm_mixed_zeropage_allowed(vma)))
2462                 return false;
2463         /* these checks mirror the abort conditions in vm_normal_page */
2464         if (vma->vm_flags & VM_MIXEDMAP)
2465                 return true;
2466         if (pfn_t_devmap(pfn))
2467                 return true;
2468         if (pfn_t_special(pfn))
2469                 return true;
2470         if (is_zero_pfn(pfn_t_to_pfn(pfn)))
2471                 return true;
2472         return false;
2473 }
2474
2475 static vm_fault_t __vm_insert_mixed(struct vm_area_struct *vma,
2476                 unsigned long addr, pfn_t pfn, bool mkwrite)
2477 {
2478         pgprot_t pgprot = vma->vm_page_prot;
2479         int err;
2480
2481         if (!vm_mixed_ok(vma, pfn, mkwrite))
2482                 return VM_FAULT_SIGBUS;
2483
2484         if (addr < vma->vm_start || addr >= vma->vm_end)
2485                 return VM_FAULT_SIGBUS;
2486
2487         track_pfn_insert(vma, &pgprot, pfn);
2488
2489         if (!pfn_modify_allowed(pfn_t_to_pfn(pfn), pgprot))
2490                 return VM_FAULT_SIGBUS;
2491
2492         /*
2493          * If we don't have pte special, then we have to use the pfn_valid()
2494          * based VM_MIXEDMAP scheme (see vm_normal_page), and thus we *must*
2495          * refcount the page if pfn_valid is true (hence insert_page rather
2496          * than insert_pfn).  If a zero_pfn were inserted into a VM_MIXEDMAP
2497          * without pte special, it would there be refcounted as a normal page.
2498          */
2499         if (!IS_ENABLED(CONFIG_ARCH_HAS_PTE_SPECIAL) &&
2500             !pfn_t_devmap(pfn) && pfn_t_valid(pfn)) {
2501                 struct page *page;
2502
2503                 /*
2504                  * At this point we are committed to insert_page()
2505                  * regardless of whether the caller specified flags that
2506                  * result in pfn_t_has_page() == false.
2507                  */
2508                 page = pfn_to_page(pfn_t_to_pfn(pfn));
2509                 err = insert_page(vma, addr, page, pgprot);
2510         } else {
2511                 return insert_pfn(vma, addr, pfn, pgprot, mkwrite);
2512         }
2513
2514         if (err == -ENOMEM)
2515                 return VM_FAULT_OOM;
2516         if (err < 0 && err != -EBUSY)
2517                 return VM_FAULT_SIGBUS;
2518
2519         return VM_FAULT_NOPAGE;
2520 }
2521
2522 vm_fault_t vmf_insert_mixed(struct vm_area_struct *vma, unsigned long addr,
2523                 pfn_t pfn)
2524 {
2525         return __vm_insert_mixed(vma, addr, pfn, false);
2526 }
2527 EXPORT_SYMBOL(vmf_insert_mixed);
2528
2529 /*
2530  *  If the insertion of PTE failed because someone else already added a
2531  *  different entry in the mean time, we treat that as success as we assume
2532  *  the same entry was actually inserted.
2533  */
2534 vm_fault_t vmf_insert_mixed_mkwrite(struct vm_area_struct *vma,
2535                 unsigned long addr, pfn_t pfn)
2536 {
2537         return __vm_insert_mixed(vma, addr, pfn, true);
2538 }
2539
2540 /*
2541  * maps a range of physical memory into the requested pages. the old
2542  * mappings are removed. any references to nonexistent pages results
2543  * in null mappings (currently treated as "copy-on-access")
2544  */
2545 static int remap_pte_range(struct mm_struct *mm, pmd_t *pmd,
2546                         unsigned long addr, unsigned long end,
2547                         unsigned long pfn, pgprot_t prot)
2548 {
2549         pte_t *pte, *mapped_pte;
2550         spinlock_t *ptl;
2551         int err = 0;
2552
2553         mapped_pte = pte = pte_alloc_map_lock(mm, pmd, addr, &ptl);
2554         if (!pte)
2555                 return -ENOMEM;
2556         arch_enter_lazy_mmu_mode();
2557         do {
2558                 BUG_ON(!pte_none(ptep_get(pte)));
2559                 if (!pfn_modify_allowed(pfn, prot)) {
2560                         err = -EACCES;
2561                         break;
2562                 }
2563                 set_pte_at(mm, addr, pte, pte_mkspecial(pfn_pte(pfn, prot)));
2564                 pfn++;
2565         } while (pte++, addr += PAGE_SIZE, addr != end);
2566         arch_leave_lazy_mmu_mode();
2567         pte_unmap_unlock(mapped_pte, ptl);
2568         return err;
2569 }
2570
2571 static inline int remap_pmd_range(struct mm_struct *mm, pud_t *pud,
2572                         unsigned long addr, unsigned long end,
2573                         unsigned long pfn, pgprot_t prot)
2574 {
2575         pmd_t *pmd;
2576         unsigned long next;
2577         int err;
2578
2579         pfn -= addr >> PAGE_SHIFT;
2580         pmd = pmd_alloc(mm, pud, addr);
2581         if (!pmd)
2582                 return -ENOMEM;
2583         VM_BUG_ON(pmd_trans_huge(*pmd));
2584         do {
2585                 next = pmd_addr_end(addr, end);
2586                 err = remap_pte_range(mm, pmd, addr, next,
2587                                 pfn + (addr >> PAGE_SHIFT), prot);
2588                 if (err)
2589                         return err;
2590         } while (pmd++, addr = next, addr != end);
2591         return 0;
2592 }
2593
2594 static inline int remap_pud_range(struct mm_struct *mm, p4d_t *p4d,
2595                         unsigned long addr, unsigned long end,
2596                         unsigned long pfn, pgprot_t prot)
2597 {
2598         pud_t *pud;
2599         unsigned long next;
2600         int err;
2601
2602         pfn -= addr >> PAGE_SHIFT;
2603         pud = pud_alloc(mm, p4d, addr);
2604         if (!pud)
2605                 return -ENOMEM;
2606         do {
2607                 next = pud_addr_end(addr, end);
2608                 err = remap_pmd_range(mm, pud, addr, next,
2609                                 pfn + (addr >> PAGE_SHIFT), prot);
2610                 if (err)
2611                         return err;
2612         } while (pud++, addr = next, addr != end);
2613         return 0;
2614 }
2615
2616 static inline int remap_p4d_range(struct mm_struct *mm, pgd_t *pgd,
2617                         unsigned long addr, unsigned long end,
2618                         unsigned long pfn, pgprot_t prot)
2619 {
2620         p4d_t *p4d;
2621         unsigned long next;
2622         int err;
2623
2624         pfn -= addr >> PAGE_SHIFT;
2625         p4d = p4d_alloc(mm, pgd, addr);
2626         if (!p4d)
2627                 return -ENOMEM;
2628         do {
2629                 next = p4d_addr_end(addr, end);
2630                 err = remap_pud_range(mm, p4d, addr, next,
2631                                 pfn + (addr >> PAGE_SHIFT), prot);
2632                 if (err)
2633                         return err;
2634         } while (p4d++, addr = next, addr != end);
2635         return 0;
2636 }
2637
2638 static int remap_pfn_range_internal(struct vm_area_struct *vma, unsigned long addr,
2639                 unsigned long pfn, unsigned long size, pgprot_t prot)
2640 {
2641         pgd_t *pgd;
2642         unsigned long next;
2643         unsigned long end = addr + PAGE_ALIGN(size);
2644         struct mm_struct *mm = vma->vm_mm;
2645         int err;
2646
2647         if (WARN_ON_ONCE(!PAGE_ALIGNED(addr)))
2648                 return -EINVAL;
2649
2650         /*
2651          * Physically remapped pages are special. Tell the
2652          * rest of the world about it:
2653          *   VM_IO tells people not to look at these pages
2654          *      (accesses can have side effects).
2655          *   VM_PFNMAP tells the core MM that the base pages are just
2656          *      raw PFN mappings, and do not have a "struct page" associated
2657          *      with them.
2658          *   VM_DONTEXPAND
2659          *      Disable vma merging and expanding with mremap().
2660          *   VM_DONTDUMP
2661          *      Omit vma from core dump, even when VM_IO turned off.
2662          *
2663          * There's a horrible special case to handle copy-on-write
2664          * behaviour that some programs depend on. We mark the "original"
2665          * un-COW'ed pages by matching them up with "vma->vm_pgoff".
2666          * See vm_normal_page() for details.
2667          */
2668         if (is_cow_mapping(vma->vm_flags)) {
2669                 if (addr != vma->vm_start || end != vma->vm_end)
2670                         return -EINVAL;
2671                 vma->vm_pgoff = pfn;
2672         }
2673
2674         vm_flags_set(vma, VM_IO | VM_PFNMAP | VM_DONTEXPAND | VM_DONTDUMP);
2675
2676         BUG_ON(addr >= end);
2677         pfn -= addr >> PAGE_SHIFT;
2678         pgd = pgd_offset(mm, addr);
2679         flush_cache_range(vma, addr, end);
2680         do {
2681                 next = pgd_addr_end(addr, end);
2682                 err = remap_p4d_range(mm, pgd, addr, next,
2683                                 pfn + (addr >> PAGE_SHIFT), prot);
2684                 if (err)
2685                         return err;
2686         } while (pgd++, addr = next, addr != end);
2687
2688         return 0;
2689 }
2690
2691 /*
2692  * Variant of remap_pfn_range that does not call track_pfn_remap.  The caller
2693  * must have pre-validated the caching bits of the pgprot_t.
2694  */
2695 int remap_pfn_range_notrack(struct vm_area_struct *vma, unsigned long addr,
2696                 unsigned long pfn, unsigned long size, pgprot_t prot)
2697 {
2698         int error = remap_pfn_range_internal(vma, addr, pfn, size, prot);
2699
2700         if (!error)
2701                 return 0;
2702
2703         /*
2704          * A partial pfn range mapping is dangerous: it does not
2705          * maintain page reference counts, and callers may free
2706          * pages due to the error. So zap it early.
2707          */
2708         zap_page_range_single(vma, addr, size, NULL);
2709         return error;
2710 }
2711
2712 /**
2713  * remap_pfn_range - remap kernel memory to userspace
2714  * @vma: user vma to map to
2715  * @addr: target page aligned user address to start at
2716  * @pfn: page frame number of kernel physical memory address
2717  * @size: size of mapping area
2718  * @prot: page protection flags for this mapping
2719  *
2720  * Note: this is only safe if the mm semaphore is held when called.
2721  *
2722  * Return: %0 on success, negative error code otherwise.
2723  */
2724 int remap_pfn_range(struct vm_area_struct *vma, unsigned long addr,
2725                     unsigned long pfn, unsigned long size, pgprot_t prot)
2726 {
2727         int err;
2728
2729         err = track_pfn_remap(vma, &prot, pfn, addr, PAGE_ALIGN(size));
2730         if (err)
2731                 return -EINVAL;
2732
2733         err = remap_pfn_range_notrack(vma, addr, pfn, size, prot);
2734         if (err)
2735                 untrack_pfn(vma, pfn, PAGE_ALIGN(size), true);
2736         return err;
2737 }
2738 EXPORT_SYMBOL(remap_pfn_range);
2739
2740 /**
2741  * vm_iomap_memory - remap memory to userspace
2742  * @vma: user vma to map to
2743  * @start: start of the physical memory to be mapped
2744  * @len: size of area
2745  *
2746  * This is a simplified io_remap_pfn_range() for common driver use. The
2747  * driver just needs to give us the physical memory range to be mapped,
2748  * we'll figure out the rest from the vma information.
2749  *
2750  * NOTE! Some drivers might want to tweak vma->vm_page_prot first to get
2751  * whatever write-combining details or similar.
2752  *
2753  * Return: %0 on success, negative error code otherwise.
2754  */
2755 int vm_iomap_memory(struct vm_area_struct *vma, phys_addr_t start, unsigned long len)
2756 {
2757         unsigned long vm_len, pfn, pages;
2758
2759         /* Check that the physical memory area passed in looks valid */
2760         if (start + len < start)
2761                 return -EINVAL;
2762         /*
2763          * You *really* shouldn't map things that aren't page-aligned,
2764          * but we've historically allowed it because IO memory might
2765          * just have smaller alignment.
2766          */
2767         len += start & ~PAGE_MASK;
2768         pfn = start >> PAGE_SHIFT;
2769         pages = (len + ~PAGE_MASK) >> PAGE_SHIFT;
2770         if (pfn + pages < pfn)
2771                 return -EINVAL;
2772
2773         /* We start the mapping 'vm_pgoff' pages into the area */
2774         if (vma->vm_pgoff > pages)
2775                 return -EINVAL;
2776         pfn += vma->vm_pgoff;
2777         pages -= vma->vm_pgoff;
2778
2779         /* Can we fit all of the mapping? */
2780         vm_len = vma->vm_end - vma->vm_start;
2781         if (vm_len >> PAGE_SHIFT > pages)
2782                 return -EINVAL;
2783
2784         /* Ok, let it rip */
2785         return io_remap_pfn_range(vma, vma->vm_start, pfn, vm_len, vma->vm_page_prot);
2786 }
2787 EXPORT_SYMBOL(vm_iomap_memory);
2788
2789 static int apply_to_pte_range(struct mm_struct *mm, pmd_t *pmd,
2790                                      unsigned long addr, unsigned long end,
2791                                      pte_fn_t fn, void *data, bool create,
2792                                      pgtbl_mod_mask *mask)
2793 {
2794         pte_t *pte, *mapped_pte;
2795         int err = 0;
2796         spinlock_t *ptl;
2797
2798         if (create) {
2799                 mapped_pte = pte = (mm == &init_mm) ?
2800                         pte_alloc_kernel_track(pmd, addr, mask) :
2801                         pte_alloc_map_lock(mm, pmd, addr, &ptl);
2802                 if (!pte)
2803                         return -ENOMEM;
2804         } else {
2805                 mapped_pte = pte = (mm == &init_mm) ?
2806                         pte_offset_kernel(pmd, addr) :
2807                         pte_offset_map_lock(mm, pmd, addr, &ptl);
2808                 if (!pte)
2809                         return -EINVAL;
2810         }
2811
2812         arch_enter_lazy_mmu_mode();
2813
2814         if (fn) {
2815                 do {
2816                         if (create || !pte_none(ptep_get(pte))) {
2817                                 err = fn(pte++, addr, data);
2818                                 if (err)
2819                                         break;
2820                         }
2821                 } while (addr += PAGE_SIZE, addr != end);
2822         }
2823         *mask |= PGTBL_PTE_MODIFIED;
2824
2825         arch_leave_lazy_mmu_mode();
2826
2827         if (mm != &init_mm)
2828                 pte_unmap_unlock(mapped_pte, ptl);
2829         return err;
2830 }
2831
2832 static int apply_to_pmd_range(struct mm_struct *mm, pud_t *pud,
2833                                      unsigned long addr, unsigned long end,
2834                                      pte_fn_t fn, void *data, bool create,
2835                                      pgtbl_mod_mask *mask)
2836 {
2837         pmd_t *pmd;
2838         unsigned long next;
2839         int err = 0;
2840
2841         BUG_ON(pud_leaf(*pud));
2842
2843         if (create) {
2844                 pmd = pmd_alloc_track(mm, pud, addr, mask);
2845                 if (!pmd)
2846                         return -ENOMEM;
2847         } else {
2848                 pmd = pmd_offset(pud, addr);
2849         }
2850         do {
2851                 next = pmd_addr_end(addr, end);
2852                 if (pmd_none(*pmd) && !create)
2853                         continue;
2854                 if (WARN_ON_ONCE(pmd_leaf(*pmd)))
2855                         return -EINVAL;
2856                 if (!pmd_none(*pmd) && WARN_ON_ONCE(pmd_bad(*pmd))) {
2857                         if (!create)
2858                                 continue;
2859                         pmd_clear_bad(pmd);
2860                 }
2861                 err = apply_to_pte_range(mm, pmd, addr, next,
2862                                          fn, data, create, mask);
2863                 if (err)
2864                         break;
2865         } while (pmd++, addr = next, addr != end);
2866
2867         return err;
2868 }
2869
2870 static int apply_to_pud_range(struct mm_struct *mm, p4d_t *p4d,
2871                                      unsigned long addr, unsigned long end,
2872                                      pte_fn_t fn, void *data, bool create,
2873                                      pgtbl_mod_mask *mask)
2874 {
2875         pud_t *pud;
2876         unsigned long next;
2877         int err = 0;
2878
2879         if (create) {
2880                 pud = pud_alloc_track(mm, p4d, addr, mask);
2881                 if (!pud)
2882                         return -ENOMEM;
2883         } else {
2884                 pud = pud_offset(p4d, addr);
2885         }
2886         do {
2887                 next = pud_addr_end(addr, end);
2888                 if (pud_none(*pud) && !create)
2889                         continue;
2890                 if (WARN_ON_ONCE(pud_leaf(*pud)))
2891                         return -EINVAL;
2892                 if (!pud_none(*pud) && WARN_ON_ONCE(pud_bad(*pud))) {
2893                         if (!create)
2894                                 continue;
2895                         pud_clear_bad(pud);
2896                 }
2897                 err = apply_to_pmd_range(mm, pud, addr, next,
2898                                          fn, data, create, mask);
2899                 if (err)
2900                         break;
2901         } while (pud++, addr = next, addr != end);
2902
2903         return err;
2904 }
2905
2906 static int apply_to_p4d_range(struct mm_struct *mm, pgd_t *pgd,
2907                                      unsigned long addr, unsigned long end,
2908                                      pte_fn_t fn, void *data, bool create,
2909                                      pgtbl_mod_mask *mask)
2910 {
2911         p4d_t *p4d;
2912         unsigned long next;
2913         int err = 0;
2914
2915         if (create) {
2916                 p4d = p4d_alloc_track(mm, pgd, addr, mask);
2917                 if (!p4d)
2918                         return -ENOMEM;
2919         } else {
2920                 p4d = p4d_offset(pgd, addr);
2921         }
2922         do {
2923                 next = p4d_addr_end(addr, end);
2924                 if (p4d_none(*p4d) && !create)
2925                         continue;
2926                 if (WARN_ON_ONCE(p4d_leaf(*p4d)))
2927                         return -EINVAL;
2928                 if (!p4d_none(*p4d) && WARN_ON_ONCE(p4d_bad(*p4d))) {
2929                         if (!create)
2930                                 continue;
2931                         p4d_clear_bad(p4d);
2932                 }
2933                 err = apply_to_pud_range(mm, p4d, addr, next,
2934                                          fn, data, create, mask);
2935                 if (err)
2936                         break;
2937         } while (p4d++, addr = next, addr != end);
2938
2939         return err;
2940 }
2941
2942 static int __apply_to_page_range(struct mm_struct *mm, unsigned long addr,
2943                                  unsigned long size, pte_fn_t fn,
2944                                  void *data, bool create)
2945 {
2946         pgd_t *pgd;
2947         unsigned long start = addr, next;
2948         unsigned long end = addr + size;
2949         pgtbl_mod_mask mask = 0;
2950         int err = 0;
2951
2952         if (WARN_ON(addr >= end))
2953                 return -EINVAL;
2954
2955         pgd = pgd_offset(mm, addr);
2956         do {
2957                 next = pgd_addr_end(addr, end);
2958                 if (pgd_none(*pgd) && !create)
2959                         continue;
2960                 if (WARN_ON_ONCE(pgd_leaf(*pgd)))
2961                         return -EINVAL;
2962                 if (!pgd_none(*pgd) && WARN_ON_ONCE(pgd_bad(*pgd))) {
2963                         if (!create)
2964                                 continue;
2965                         pgd_clear_bad(pgd);
2966                 }
2967                 err = apply_to_p4d_range(mm, pgd, addr, next,
2968                                          fn, data, create, &mask);
2969                 if (err)
2970                         break;
2971         } while (pgd++, addr = next, addr != end);
2972
2973         if (mask & ARCH_PAGE_TABLE_SYNC_MASK)
2974                 arch_sync_kernel_mappings(start, start + size);
2975
2976         return err;
2977 }
2978
2979 /*
2980  * Scan a region of virtual memory, filling in page tables as necessary
2981  * and calling a provided function on each leaf page table.
2982  */
2983 int apply_to_page_range(struct mm_struct *mm, unsigned long addr,
2984                         unsigned long size, pte_fn_t fn, void *data)
2985 {
2986         return __apply_to_page_range(mm, addr, size, fn, data, true);
2987 }
2988 EXPORT_SYMBOL_GPL(apply_to_page_range);
2989
2990 /*
2991  * Scan a region of virtual memory, calling a provided function on
2992  * each leaf page table where it exists.
2993  *
2994  * Unlike apply_to_page_range, this does _not_ fill in page tables
2995  * where they are absent.
2996  */
2997 int apply_to_existing_page_range(struct mm_struct *mm, unsigned long addr,
2998                                  unsigned long size, pte_fn_t fn, void *data)
2999 {
3000         return __apply_to_page_range(mm, addr, size, fn, data, false);
3001 }
3002 EXPORT_SYMBOL_GPL(apply_to_existing_page_range);
3003
3004 /*
3005  * handle_pte_fault chooses page fault handler according to an entry which was
3006  * read non-atomically.  Before making any commitment, on those architectures
3007  * or configurations (e.g. i386 with PAE) which might give a mix of unmatched
3008  * parts, do_swap_page must check under lock before unmapping the pte and
3009  * proceeding (but do_wp_page is only called after already making such a check;
3010  * and do_anonymous_page can safely check later on).
3011  */
3012 static inline int pte_unmap_same(struct vm_fault *vmf)
3013 {
3014         int same = 1;
3015 #if defined(CONFIG_SMP) || defined(CONFIG_PREEMPTION)
3016         if (sizeof(pte_t) > sizeof(unsigned long)) {
3017                 spin_lock(vmf->ptl);
3018                 same = pte_same(ptep_get(vmf->pte), vmf->orig_pte);
3019                 spin_unlock(vmf->ptl);
3020         }
3021 #endif
3022         pte_unmap(vmf->pte);
3023         vmf->pte = NULL;
3024         return same;
3025 }
3026
3027 /*
3028  * Return:
3029  *      0:              copied succeeded
3030  *      -EHWPOISON:     copy failed due to hwpoison in source page
3031  *      -EAGAIN:        copied failed (some other reason)
3032  */
3033 static inline int __wp_page_copy_user(struct page *dst, struct page *src,
3034                                       struct vm_fault *vmf)
3035 {
3036         int ret;
3037         void *kaddr;
3038         void __user *uaddr;
3039         struct vm_area_struct *vma = vmf->vma;
3040         struct mm_struct *mm = vma->vm_mm;
3041         unsigned long addr = vmf->address;
3042
3043         if (likely(src)) {
3044                 if (copy_mc_user_highpage(dst, src, addr, vma))
3045                         return -EHWPOISON;
3046                 return 0;
3047         }
3048
3049         /*
3050          * If the source page was a PFN mapping, we don't have
3051          * a "struct page" for it. We do a best-effort copy by
3052          * just copying from the original user address. If that
3053          * fails, we just zero-fill it. Live with it.
3054          */
3055         kaddr = kmap_local_page(dst);
3056         pagefault_disable();
3057         uaddr = (void __user *)(addr & PAGE_MASK);
3058
3059         /*
3060          * On architectures with software "accessed" bits, we would
3061          * take a double page fault, so mark it accessed here.
3062          */
3063         vmf->pte = NULL;
3064         if (!arch_has_hw_pte_young() && !pte_young(vmf->orig_pte)) {
3065                 pte_t entry;
3066
3067                 vmf->pte = pte_offset_map_lock(mm, vmf->pmd, addr, &vmf->ptl);
3068                 if (unlikely(!vmf->pte || !pte_same(ptep_get(vmf->pte), vmf->orig_pte))) {
3069                         /*
3070                          * Other thread has already handled the fault
3071                          * and update local tlb only
3072                          */
3073                         if (vmf->pte)
3074                                 update_mmu_tlb(vma, addr, vmf->pte);
3075                         ret = -EAGAIN;
3076                         goto pte_unlock;
3077                 }
3078
3079                 entry = pte_mkyoung(vmf->orig_pte);
3080                 if (ptep_set_access_flags(vma, addr, vmf->pte, entry, 0))
3081                         update_mmu_cache_range(vmf, vma, addr, vmf->pte, 1);
3082         }
3083
3084         /*
3085          * This really shouldn't fail, because the page is there
3086          * in the page tables. But it might just be unreadable,
3087          * in which case we just give up and fill the result with
3088          * zeroes.
3089          */
3090         if (__copy_from_user_inatomic(kaddr, uaddr, PAGE_SIZE)) {
3091                 if (vmf->pte)
3092                         goto warn;
3093
3094                 /* Re-validate under PTL if the page is still mapped */
3095                 vmf->pte = pte_offset_map_lock(mm, vmf->pmd, addr, &vmf->ptl);
3096                 if (unlikely(!vmf->pte || !pte_same(ptep_get(vmf->pte), vmf->orig_pte))) {
3097                         /* The PTE changed under us, update local tlb */
3098                         if (vmf->pte)
3099                                 update_mmu_tlb(vma, addr, vmf->pte);
3100                         ret = -EAGAIN;
3101                         goto pte_unlock;
3102                 }
3103
3104                 /*
3105                  * The same page can be mapped back since last copy attempt.
3106                  * Try to copy again under PTL.
3107                  */
3108                 if (__copy_from_user_inatomic(kaddr, uaddr, PAGE_SIZE)) {
3109                         /*
3110                          * Give a warn in case there can be some obscure
3111                          * use-case
3112                          */
3113 warn:
3114                         WARN_ON_ONCE(1);
3115                         clear_page(kaddr);
3116                 }
3117         }
3118
3119         ret = 0;
3120
3121 pte_unlock:
3122         if (vmf->pte)
3123                 pte_unmap_unlock(vmf->pte, vmf->ptl);
3124         pagefault_enable();
3125         kunmap_local(kaddr);
3126         flush_dcache_page(dst);
3127
3128         return ret;
3129 }
3130
3131 static gfp_t __get_fault_gfp_mask(struct vm_area_struct *vma)
3132 {
3133         struct file *vm_file = vma->vm_file;
3134
3135         if (vm_file)
3136                 return mapping_gfp_mask(vm_file->f_mapping) | __GFP_FS | __GFP_IO;
3137
3138         /*
3139          * Special mappings (e.g. VDSO) do not have any file so fake
3140          * a default GFP_KERNEL for them.
3141          */
3142         return GFP_KERNEL;
3143 }
3144
3145 /*
3146  * Notify the address space that the page is about to become writable so that
3147  * it can prohibit this or wait for the page to get into an appropriate state.
3148  *
3149  * We do this without the lock held, so that it can sleep if it needs to.
3150  */
3151 static vm_fault_t do_page_mkwrite(struct vm_fault *vmf, struct folio *folio)
3152 {
3153         vm_fault_t ret;
3154         unsigned int old_flags = vmf->flags;
3155
3156         vmf->flags = FAULT_FLAG_WRITE|FAULT_FLAG_MKWRITE;
3157
3158         if (vmf->vma->vm_file &&
3159             IS_SWAPFILE(vmf->vma->vm_file->f_mapping->host))
3160                 return VM_FAULT_SIGBUS;
3161
3162         ret = vmf->vma->vm_ops->page_mkwrite(vmf);
3163         /* Restore original flags so that caller is not surprised */
3164         vmf->flags = old_flags;
3165         if (unlikely(ret & (VM_FAULT_ERROR | VM_FAULT_NOPAGE)))
3166                 return ret;
3167         if (unlikely(!(ret & VM_FAULT_LOCKED))) {
3168                 folio_lock(folio);
3169                 if (!folio->mapping) {
3170                         folio_unlock(folio);
3171                         return 0; /* retry */
3172                 }
3173                 ret |= VM_FAULT_LOCKED;
3174         } else
3175                 VM_BUG_ON_FOLIO(!folio_test_locked(folio), folio);
3176         return ret;
3177 }
3178
3179 /*
3180  * Handle dirtying of a page in shared file mapping on a write fault.
3181  *
3182  * The function expects the page to be locked and unlocks it.
3183  */
3184 static vm_fault_t fault_dirty_shared_page(struct vm_fault *vmf)
3185 {
3186         struct vm_area_struct *vma = vmf->vma;
3187         struct address_space *mapping;
3188         struct folio *folio = page_folio(vmf->page);
3189         bool dirtied;
3190         bool page_mkwrite = vma->vm_ops && vma->vm_ops->page_mkwrite;
3191
3192         dirtied = folio_mark_dirty(folio);
3193         VM_BUG_ON_FOLIO(folio_test_anon(folio), folio);
3194         /*
3195          * Take a local copy of the address_space - folio.mapping may be zeroed
3196          * by truncate after folio_unlock().   The address_space itself remains
3197          * pinned by vma->vm_file's reference.  We rely on folio_unlock()'s
3198          * release semantics to prevent the compiler from undoing this copying.
3199          */
3200         mapping = folio_raw_mapping(folio);
3201         folio_unlock(folio);
3202
3203         if (!page_mkwrite)
3204                 file_update_time(vma->vm_file);
3205
3206         /*
3207          * Throttle page dirtying rate down to writeback speed.
3208          *
3209          * mapping may be NULL here because some device drivers do not
3210          * set page.mapping but still dirty their pages
3211          *
3212          * Drop the mmap_lock before waiting on IO, if we can. The file
3213          * is pinning the mapping, as per above.
3214          */
3215         if ((dirtied || page_mkwrite) && mapping) {
3216                 struct file *fpin;
3217
3218                 fpin = maybe_unlock_mmap_for_io(vmf, NULL);
3219                 balance_dirty_pages_ratelimited(mapping);
3220                 if (fpin) {
3221                         fput(fpin);
3222                         return VM_FAULT_COMPLETED;
3223                 }
3224         }
3225
3226         return 0;
3227 }
3228
3229 /*
3230  * Handle write page faults for pages that can be reused in the current vma
3231  *
3232  * This can happen either due to the mapping being with the VM_SHARED flag,
3233  * or due to us being the last reference standing to the page. In either
3234  * case, all we need to do here is to mark the page as writable and update
3235  * any related book-keeping.
3236  */
3237 static inline void wp_page_reuse(struct vm_fault *vmf, struct folio *folio)
3238         __releases(vmf->ptl)
3239 {
3240         struct vm_area_struct *vma = vmf->vma;
3241         pte_t entry;
3242
3243         VM_BUG_ON(!(vmf->flags & FAULT_FLAG_WRITE));
3244         VM_WARN_ON(is_zero_pfn(pte_pfn(vmf->orig_pte)));
3245
3246         if (folio) {
3247                 VM_BUG_ON(folio_test_anon(folio) &&
3248                           !PageAnonExclusive(vmf->page));
3249                 /*
3250                  * Clear the folio's cpupid information as the existing
3251                  * information potentially belongs to a now completely
3252                  * unrelated process.
3253                  */
3254                 folio_xchg_last_cpupid(folio, (1 << LAST_CPUPID_SHIFT) - 1);
3255         }
3256
3257         flush_cache_page(vma, vmf->address, pte_pfn(vmf->orig_pte));
3258         entry = pte_mkyoung(vmf->orig_pte);
3259         entry = maybe_mkwrite(pte_mkdirty(entry), vma);
3260         if (ptep_set_access_flags(vma, vmf->address, vmf->pte, entry, 1))
3261                 update_mmu_cache_range(vmf, vma, vmf->address, vmf->pte, 1);
3262         pte_unmap_unlock(vmf->pte, vmf->ptl);
3263         count_vm_event(PGREUSE);
3264 }
3265
3266 /*
3267  * We could add a bitflag somewhere, but for now, we know that all
3268  * vm_ops that have a ->map_pages have been audited and don't need
3269  * the mmap_lock to be held.
3270  */
3271 static inline vm_fault_t vmf_can_call_fault(const struct vm_fault *vmf)
3272 {
3273         struct vm_area_struct *vma = vmf->vma;
3274
3275         if (vma->vm_ops->map_pages || !(vmf->flags & FAULT_FLAG_VMA_LOCK))
3276                 return 0;
3277         vma_end_read(vma);
3278         return VM_FAULT_RETRY;
3279 }
3280
3281 /**
3282  * __vmf_anon_prepare - Prepare to handle an anonymous fault.
3283  * @vmf: The vm_fault descriptor passed from the fault handler.
3284  *
3285  * When preparing to insert an anonymous page into a VMA from a
3286  * fault handler, call this function rather than anon_vma_prepare().
3287  * If this vma does not already have an associated anon_vma and we are
3288  * only protected by the per-VMA lock, the caller must retry with the
3289  * mmap_lock held.  __anon_vma_prepare() will look at adjacent VMAs to
3290  * determine if this VMA can share its anon_vma, and that's not safe to
3291  * do with only the per-VMA lock held for this VMA.
3292  *
3293  * Return: 0 if fault handling can proceed.  Any other value should be
3294  * returned to the caller.
3295  */
3296 vm_fault_t __vmf_anon_prepare(struct vm_fault *vmf)
3297 {
3298         struct vm_area_struct *vma = vmf->vma;
3299         vm_fault_t ret = 0;
3300
3301         if (likely(vma->anon_vma))
3302                 return 0;
3303         if (vmf->flags & FAULT_FLAG_VMA_LOCK) {
3304                 if (!mmap_read_trylock(vma->vm_mm))
3305                         return VM_FAULT_RETRY;
3306         }
3307         if (__anon_vma_prepare(vma))
3308                 ret = VM_FAULT_OOM;
3309         if (vmf->flags & FAULT_FLAG_VMA_LOCK)
3310                 mmap_read_unlock(vma->vm_mm);
3311         return ret;
3312 }
3313
3314 /*
3315  * Handle the case of a page which we actually need to copy to a new page,
3316  * either due to COW or unsharing.
3317  *
3318  * Called with mmap_lock locked and the old page referenced, but
3319  * without the ptl held.
3320  *
3321  * High level logic flow:
3322  *
3323  * - Allocate a page, copy the content of the old page to the new one.
3324  * - Handle book keeping and accounting - cgroups, mmu-notifiers, etc.
3325  * - Take the PTL. If the pte changed, bail out and release the allocated page
3326  * - If the pte is still the way we remember it, update the page table and all
3327  *   relevant references. This includes dropping the reference the page-table
3328  *   held to the old page, as well as updating the rmap.
3329  * - In any case, unlock the PTL and drop the reference we took to the old page.
3330  */
3331 static vm_fault_t wp_page_copy(struct vm_fault *vmf)
3332 {
3333         const bool unshare = vmf->flags & FAULT_FLAG_UNSHARE;
3334         struct vm_area_struct *vma = vmf->vma;
3335         struct mm_struct *mm = vma->vm_mm;
3336         struct folio *old_folio = NULL;
3337         struct folio *new_folio = NULL;
3338         pte_t entry;
3339         int page_copied = 0;
3340         struct mmu_notifier_range range;
3341         vm_fault_t ret;
3342         bool pfn_is_zero;
3343
3344         delayacct_wpcopy_start();
3345
3346         if (vmf->page)
3347                 old_folio = page_folio(vmf->page);
3348         ret = vmf_anon_prepare(vmf);
3349         if (unlikely(ret))
3350                 goto out;
3351
3352         pfn_is_zero = is_zero_pfn(pte_pfn(vmf->orig_pte));
3353         new_folio = folio_prealloc(mm, vma, vmf->address, pfn_is_zero);
3354         if (!new_folio)
3355                 goto oom;
3356
3357         if (!pfn_is_zero) {
3358                 int err;
3359
3360                 err = __wp_page_copy_user(&new_folio->page, vmf->page, vmf);
3361                 if (err) {
3362                         /*
3363                          * COW failed, if the fault was solved by other,
3364                          * it's fine. If not, userspace would re-fault on
3365                          * the same address and we will handle the fault
3366                          * from the second attempt.
3367                          * The -EHWPOISON case will not be retried.
3368                          */
3369                         folio_put(new_folio);
3370                         if (old_folio)
3371                                 folio_put(old_folio);
3372
3373                         delayacct_wpcopy_end();
3374                         return err == -EHWPOISON ? VM_FAULT_HWPOISON : 0;
3375                 }
3376                 kmsan_copy_page_meta(&new_folio->page, vmf->page);
3377         }
3378
3379         __folio_mark_uptodate(new_folio);
3380
3381         mmu_notifier_range_init(&range, MMU_NOTIFY_CLEAR, 0, mm,
3382                                 vmf->address & PAGE_MASK,
3383                                 (vmf->address & PAGE_MASK) + PAGE_SIZE);
3384         mmu_notifier_invalidate_range_start(&range);
3385
3386         /*
3387          * Re-check the pte - we dropped the lock
3388          */
3389         vmf->pte = pte_offset_map_lock(mm, vmf->pmd, vmf->address, &vmf->ptl);
3390         if (likely(vmf->pte && pte_same(ptep_get(vmf->pte), vmf->orig_pte))) {
3391                 if (old_folio) {
3392                         if (!folio_test_anon(old_folio)) {
3393                                 dec_mm_counter(mm, mm_counter_file(old_folio));
3394                                 inc_mm_counter(mm, MM_ANONPAGES);
3395                         }
3396                 } else {
3397                         ksm_might_unmap_zero_page(mm, vmf->orig_pte);
3398                         inc_mm_counter(mm, MM_ANONPAGES);
3399                 }
3400                 flush_cache_page(vma, vmf->address, pte_pfn(vmf->orig_pte));
3401                 entry = mk_pte(&new_folio->page, vma->vm_page_prot);
3402                 entry = pte_sw_mkyoung(entry);
3403                 if (unlikely(unshare)) {
3404                         if (pte_soft_dirty(vmf->orig_pte))
3405                                 entry = pte_mksoft_dirty(entry);
3406                         if (pte_uffd_wp(vmf->orig_pte))
3407                                 entry = pte_mkuffd_wp(entry);
3408                 } else {
3409                         entry = maybe_mkwrite(pte_mkdirty(entry), vma);
3410                 }
3411
3412                 /*
3413                  * Clear the pte entry and flush it first, before updating the
3414                  * pte with the new entry, to keep TLBs on different CPUs in
3415                  * sync. This code used to set the new PTE then flush TLBs, but
3416                  * that left a window where the new PTE could be loaded into
3417                  * some TLBs while the old PTE remains in others.
3418                  */
3419                 ptep_clear_flush(vma, vmf->address, vmf->pte);
3420                 folio_add_new_anon_rmap(new_folio, vma, vmf->address, RMAP_EXCLUSIVE);
3421                 folio_add_lru_vma(new_folio, vma);
3422                 BUG_ON(unshare && pte_write(entry));
3423                 set_pte_at(mm, vmf->address, vmf->pte, entry);
3424                 update_mmu_cache_range(vmf, vma, vmf->address, vmf->pte, 1);
3425                 if (old_folio) {
3426                         /*
3427                          * Only after switching the pte to the new page may
3428                          * we remove the mapcount here. Otherwise another
3429                          * process may come and find the rmap count decremented
3430                          * before the pte is switched to the new page, and
3431                          * "reuse" the old page writing into it while our pte
3432                          * here still points into it and can be read by other
3433                          * threads.
3434                          *
3435                          * The critical issue is to order this
3436                          * folio_remove_rmap_pte() with the ptp_clear_flush
3437                          * above. Those stores are ordered by (if nothing else,)
3438                          * the barrier present in the atomic_add_negative
3439                          * in folio_remove_rmap_pte();
3440                          *
3441                          * Then the TLB flush in ptep_clear_flush ensures that
3442                          * no process can access the old page before the
3443                          * decremented mapcount is visible. And the old page
3444                          * cannot be reused until after the decremented
3445                          * mapcount is visible. So transitively, TLBs to
3446                          * old page will be flushed before it can be reused.
3447                          */
3448                         folio_remove_rmap_pte(old_folio, vmf->page, vma);
3449                 }
3450
3451                 /* Free the old page.. */
3452                 new_folio = old_folio;
3453                 page_copied = 1;
3454                 pte_unmap_unlock(vmf->pte, vmf->ptl);
3455         } else if (vmf->pte) {
3456                 update_mmu_tlb(vma, vmf->address, vmf->pte);
3457                 pte_unmap_unlock(vmf->pte, vmf->ptl);
3458         }
3459
3460         mmu_notifier_invalidate_range_end(&range);
3461
3462         if (new_folio)
3463                 folio_put(new_folio);
3464         if (old_folio) {
3465                 if (page_copied)
3466                         free_swap_cache(old_folio);
3467                 folio_put(old_folio);
3468         }
3469
3470         delayacct_wpcopy_end();
3471         return 0;
3472 oom:
3473         ret = VM_FAULT_OOM;
3474 out:
3475         if (old_folio)
3476                 folio_put(old_folio);
3477
3478         delayacct_wpcopy_end();
3479         return ret;
3480 }
3481
3482 /**
3483  * finish_mkwrite_fault - finish page fault for a shared mapping, making PTE
3484  *                        writeable once the page is prepared
3485  *
3486  * @vmf: structure describing the fault
3487  * @folio: the folio of vmf->page
3488  *
3489  * This function handles all that is needed to finish a write page fault in a
3490  * shared mapping due to PTE being read-only once the mapped page is prepared.
3491  * It handles locking of PTE and modifying it.
3492  *
3493  * The function expects the page to be locked or other protection against
3494  * concurrent faults / writeback (such as DAX radix tree locks).
3495  *
3496  * Return: %0 on success, %VM_FAULT_NOPAGE when PTE got changed before
3497  * we acquired PTE lock.
3498  */
3499 static vm_fault_t finish_mkwrite_fault(struct vm_fault *vmf, struct folio *folio)
3500 {
3501         WARN_ON_ONCE(!(vmf->vma->vm_flags & VM_SHARED));
3502         vmf->pte = pte_offset_map_lock(vmf->vma->vm_mm, vmf->pmd, vmf->address,
3503                                        &vmf->ptl);
3504         if (!vmf->pte)
3505                 return VM_FAULT_NOPAGE;
3506         /*
3507          * We might have raced with another page fault while we released the
3508          * pte_offset_map_lock.
3509          */
3510         if (!pte_same(ptep_get(vmf->pte), vmf->orig_pte)) {
3511                 update_mmu_tlb(vmf->vma, vmf->address, vmf->pte);
3512                 pte_unmap_unlock(vmf->pte, vmf->ptl);
3513                 return VM_FAULT_NOPAGE;
3514         }
3515         wp_page_reuse(vmf, folio);
3516         return 0;
3517 }
3518
3519 /*
3520  * Handle write page faults for VM_MIXEDMAP or VM_PFNMAP for a VM_SHARED
3521  * mapping
3522  */
3523 static vm_fault_t wp_pfn_shared(struct vm_fault *vmf)
3524 {
3525         struct vm_area_struct *vma = vmf->vma;
3526
3527         if (vma->vm_ops && vma->vm_ops->pfn_mkwrite) {
3528                 vm_fault_t ret;
3529
3530                 pte_unmap_unlock(vmf->pte, vmf->ptl);
3531                 ret = vmf_can_call_fault(vmf);
3532                 if (ret)
3533                         return ret;
3534
3535                 vmf->flags |= FAULT_FLAG_MKWRITE;
3536                 ret = vma->vm_ops->pfn_mkwrite(vmf);
3537                 if (ret & (VM_FAULT_ERROR | VM_FAULT_NOPAGE))
3538                         return ret;
3539                 return finish_mkwrite_fault(vmf, NULL);
3540         }
3541         wp_page_reuse(vmf, NULL);
3542         return 0;
3543 }
3544
3545 static vm_fault_t wp_page_shared(struct vm_fault *vmf, struct folio *folio)
3546         __releases(vmf->ptl)
3547 {
3548         struct vm_area_struct *vma = vmf->vma;
3549         vm_fault_t ret = 0;
3550
3551         folio_get(folio);
3552
3553         if (vma->vm_ops && vma->vm_ops->page_mkwrite) {
3554                 vm_fault_t tmp;
3555
3556                 pte_unmap_unlock(vmf->pte, vmf->ptl);
3557                 tmp = vmf_can_call_fault(vmf);
3558                 if (tmp) {
3559                         folio_put(folio);
3560                         return tmp;
3561                 }
3562
3563                 tmp = do_page_mkwrite(vmf, folio);
3564                 if (unlikely(!tmp || (tmp &
3565                                       (VM_FAULT_ERROR | VM_FAULT_NOPAGE)))) {
3566                         folio_put(folio);
3567                         return tmp;
3568                 }
3569                 tmp = finish_mkwrite_fault(vmf, folio);
3570                 if (unlikely(tmp & (VM_FAULT_ERROR | VM_FAULT_NOPAGE))) {
3571                         folio_unlock(folio);
3572                         folio_put(folio);
3573                         return tmp;
3574                 }
3575         } else {
3576                 wp_page_reuse(vmf, folio);
3577                 folio_lock(folio);
3578         }
3579         ret |= fault_dirty_shared_page(vmf);
3580         folio_put(folio);
3581
3582         return ret;
3583 }
3584
3585 static bool wp_can_reuse_anon_folio(struct folio *folio,
3586                                     struct vm_area_struct *vma)
3587 {
3588         /*
3589          * We could currently only reuse a subpage of a large folio if no
3590          * other subpages of the large folios are still mapped. However,
3591          * let's just consistently not reuse subpages even if we could
3592          * reuse in that scenario, and give back a large folio a bit
3593          * sooner.
3594          */
3595         if (folio_test_large(folio))
3596                 return false;
3597
3598         /*
3599          * We have to verify under folio lock: these early checks are
3600          * just an optimization to avoid locking the folio and freeing
3601          * the swapcache if there is little hope that we can reuse.
3602          *
3603          * KSM doesn't necessarily raise the folio refcount.
3604          */
3605         if (folio_test_ksm(folio) || folio_ref_count(folio) > 3)
3606                 return false;
3607         if (!folio_test_lru(folio))
3608                 /*
3609                  * We cannot easily detect+handle references from
3610                  * remote LRU caches or references to LRU folios.
3611                  */
3612                 lru_add_drain();
3613         if (folio_ref_count(folio) > 1 + folio_test_swapcache(folio))
3614                 return false;
3615         if (!folio_trylock(folio))
3616                 return false;
3617         if (folio_test_swapcache(folio))
3618                 folio_free_swap(folio);
3619         if (folio_test_ksm(folio) || folio_ref_count(folio) != 1) {
3620                 folio_unlock(folio);
3621                 return false;
3622         }
3623         /*
3624          * Ok, we've got the only folio reference from our mapping
3625          * and the folio is locked, it's dark out, and we're wearing
3626          * sunglasses. Hit it.
3627          */
3628         folio_move_anon_rmap(folio, vma);
3629         folio_unlock(folio);
3630         return true;
3631 }
3632
3633 /*
3634  * This routine handles present pages, when
3635  * * users try to write to a shared page (FAULT_FLAG_WRITE)
3636  * * GUP wants to take a R/O pin on a possibly shared anonymous page
3637  *   (FAULT_FLAG_UNSHARE)
3638  *
3639  * It is done by copying the page to a new address and decrementing the
3640  * shared-page counter for the old page.
3641  *
3642  * Note that this routine assumes that the protection checks have been
3643  * done by the caller (the low-level page fault routine in most cases).
3644  * Thus, with FAULT_FLAG_WRITE, we can safely just mark it writable once we've
3645  * done any necessary COW.
3646  *
3647  * In case of FAULT_FLAG_WRITE, we also mark the page dirty at this point even
3648  * though the page will change only once the write actually happens. This
3649  * avoids a few races, and potentially makes it more efficient.
3650  *
3651  * We enter with non-exclusive mmap_lock (to exclude vma changes,
3652  * but allow concurrent faults), with pte both mapped and locked.
3653  * We return with mmap_lock still held, but pte unmapped and unlocked.
3654  */
3655 static vm_fault_t do_wp_page(struct vm_fault *vmf)
3656         __releases(vmf->ptl)
3657 {
3658         const bool unshare = vmf->flags & FAULT_FLAG_UNSHARE;
3659         struct vm_area_struct *vma = vmf->vma;
3660         struct folio *folio = NULL;
3661         pte_t pte;
3662
3663         if (likely(!unshare)) {
3664                 if (userfaultfd_pte_wp(vma, ptep_get(vmf->pte))) {
3665                         if (!userfaultfd_wp_async(vma)) {
3666                                 pte_unmap_unlock(vmf->pte, vmf->ptl);
3667                                 return handle_userfault(vmf, VM_UFFD_WP);
3668                         }
3669
3670                         /*
3671                          * Nothing needed (cache flush, TLB invalidations,
3672                          * etc.) because we're only removing the uffd-wp bit,
3673                          * which is completely invisible to the user.
3674                          */
3675                         pte = pte_clear_uffd_wp(ptep_get(vmf->pte));
3676
3677                         set_pte_at(vma->vm_mm, vmf->address, vmf->pte, pte);
3678                         /*
3679                          * Update this to be prepared for following up CoW
3680                          * handling
3681                          */
3682                         vmf->orig_pte = pte;
3683                 }
3684
3685                 /*
3686                  * Userfaultfd write-protect can defer flushes. Ensure the TLB
3687                  * is flushed in this case before copying.
3688                  */
3689                 if (unlikely(userfaultfd_wp(vmf->vma) &&
3690                              mm_tlb_flush_pending(vmf->vma->vm_mm)))
3691                         flush_tlb_page(vmf->vma, vmf->address);
3692         }
3693
3694         vmf->page = vm_normal_page(vma, vmf->address, vmf->orig_pte);
3695
3696         if (vmf->page)
3697                 folio = page_folio(vmf->page);
3698
3699         /*
3700          * Shared mapping: we are guaranteed to have VM_WRITE and
3701          * FAULT_FLAG_WRITE set at this point.
3702          */
3703         if (vma->vm_flags & (VM_SHARED | VM_MAYSHARE)) {
3704                 /*
3705                  * VM_MIXEDMAP !pfn_valid() case, or VM_SOFTDIRTY clear on a
3706                  * VM_PFNMAP VMA.
3707                  *
3708                  * We should not cow pages in a shared writeable mapping.
3709                  * Just mark the pages writable and/or call ops->pfn_mkwrite.
3710                  */
3711                 if (!vmf->page)
3712                         return wp_pfn_shared(vmf);
3713                 return wp_page_shared(vmf, folio);
3714         }
3715
3716         /*
3717          * Private mapping: create an exclusive anonymous page copy if reuse
3718          * is impossible. We might miss VM_WRITE for FOLL_FORCE handling.
3719          *
3720          * If we encounter a page that is marked exclusive, we must reuse
3721          * the page without further checks.
3722          */
3723         if (folio && folio_test_anon(folio) &&
3724             (PageAnonExclusive(vmf->page) || wp_can_reuse_anon_folio(folio, vma))) {
3725                 if (!PageAnonExclusive(vmf->page))
3726                         SetPageAnonExclusive(vmf->page);
3727                 if (unlikely(unshare)) {
3728                         pte_unmap_unlock(vmf->pte, vmf->ptl);
3729                         return 0;
3730                 }
3731                 wp_page_reuse(vmf, folio);
3732                 return 0;
3733         }
3734         /*
3735          * Ok, we need to copy. Oh, well..
3736          */
3737         if (folio)
3738                 folio_get(folio);
3739
3740         pte_unmap_unlock(vmf->pte, vmf->ptl);
3741 #ifdef CONFIG_KSM
3742         if (folio && folio_test_ksm(folio))
3743                 count_vm_event(COW_KSM);
3744 #endif
3745         return wp_page_copy(vmf);
3746 }
3747
3748 static void unmap_mapping_range_vma(struct vm_area_struct *vma,
3749                 unsigned long start_addr, unsigned long end_addr,
3750                 struct zap_details *details)
3751 {
3752         zap_page_range_single(vma, start_addr, end_addr - start_addr, details);
3753 }
3754
3755 static inline void unmap_mapping_range_tree(struct rb_root_cached *root,
3756                                             pgoff_t first_index,
3757                                             pgoff_t last_index,
3758                                             struct zap_details *details)
3759 {
3760         struct vm_area_struct *vma;
3761         pgoff_t vba, vea, zba, zea;
3762
3763         vma_interval_tree_foreach(vma, root, first_index, last_index) {
3764                 vba = vma->vm_pgoff;
3765                 vea = vba + vma_pages(vma) - 1;
3766                 zba = max(first_index, vba);
3767                 zea = min(last_index, vea);
3768
3769                 unmap_mapping_range_vma(vma,
3770                         ((zba - vba) << PAGE_SHIFT) + vma->vm_start,
3771                         ((zea - vba + 1) << PAGE_SHIFT) + vma->vm_start,
3772                                 details);
3773         }
3774 }
3775
3776 /**
3777  * unmap_mapping_folio() - Unmap single folio from processes.
3778  * @folio: The locked folio to be unmapped.
3779  *
3780  * Unmap this folio from any userspace process which still has it mmaped.
3781  * Typically, for efficiency, the range of nearby pages has already been
3782  * unmapped by unmap_mapping_pages() or unmap_mapping_range().  But once
3783  * truncation or invalidation holds the lock on a folio, it may find that
3784  * the page has been remapped again: and then uses unmap_mapping_folio()
3785  * to unmap it finally.
3786  */
3787 void unmap_mapping_folio(struct folio *folio)
3788 {
3789         struct address_space *mapping = folio->mapping;
3790         struct zap_details details = { };
3791         pgoff_t first_index;
3792         pgoff_t last_index;
3793
3794         VM_BUG_ON(!folio_test_locked(folio));
3795
3796         first_index = folio->index;
3797         last_index = folio_next_index(folio) - 1;
3798
3799         details.even_cows = false;
3800         details.single_folio = folio;
3801         details.zap_flags = ZAP_FLAG_DROP_MARKER;
3802
3803         i_mmap_lock_read(mapping);
3804         if (unlikely(!RB_EMPTY_ROOT(&mapping->i_mmap.rb_root)))
3805                 unmap_mapping_range_tree(&mapping->i_mmap, first_index,
3806                                          last_index, &details);
3807         i_mmap_unlock_read(mapping);
3808 }
3809
3810 /**
3811  * unmap_mapping_pages() - Unmap pages from processes.
3812  * @mapping: The address space containing pages to be unmapped.
3813  * @start: Index of first page to be unmapped.
3814  * @nr: Number of pages to be unmapped.  0 to unmap to end of file.
3815  * @even_cows: Whether to unmap even private COWed pages.
3816  *
3817  * Unmap the pages in this address space from any userspace process which
3818  * has them mmaped.  Generally, you want to remove COWed pages as well when
3819  * a file is being truncated, but not when invalidating pages from the page
3820  * cache.
3821  */
3822 void unmap_mapping_pages(struct address_space *mapping, pgoff_t start,
3823                 pgoff_t nr, bool even_cows)
3824 {
3825         struct zap_details details = { };
3826         pgoff_t first_index = start;
3827         pgoff_t last_index = start + nr - 1;
3828
3829         details.even_cows = even_cows;
3830         if (last_index < first_index)
3831                 last_index = ULONG_MAX;
3832
3833         i_mmap_lock_read(mapping);
3834         if (unlikely(!RB_EMPTY_ROOT(&mapping->i_mmap.rb_root)))
3835                 unmap_mapping_range_tree(&mapping->i_mmap, first_index,
3836                                          last_index, &details);
3837         i_mmap_unlock_read(mapping);
3838 }
3839 EXPORT_SYMBOL_GPL(unmap_mapping_pages);
3840
3841 /**
3842  * unmap_mapping_range - unmap the portion of all mmaps in the specified
3843  * address_space corresponding to the specified byte range in the underlying
3844  * file.
3845  *
3846  * @mapping: the address space containing mmaps to be unmapped.
3847  * @holebegin: byte in first page to unmap, relative to the start of
3848  * the underlying file.  This will be rounded down to a PAGE_SIZE
3849  * boundary.  Note that this is different from truncate_pagecache(), which
3850  * must keep the partial page.  In contrast, we must get rid of
3851  * partial pages.
3852  * @holelen: size of prospective hole in bytes.  This will be rounded
3853  * up to a PAGE_SIZE boundary.  A holelen of zero truncates to the
3854  * end of the file.
3855  * @even_cows: 1 when truncating a file, unmap even private COWed pages;
3856  * but 0 when invalidating pagecache, don't throw away private data.
3857  */
3858 void unmap_mapping_range(struct address_space *mapping,
3859                 loff_t const holebegin, loff_t const holelen, int even_cows)
3860 {
3861         pgoff_t hba = (pgoff_t)(holebegin) >> PAGE_SHIFT;
3862         pgoff_t hlen = ((pgoff_t)(holelen) + PAGE_SIZE - 1) >> PAGE_SHIFT;
3863
3864         /* Check for overflow. */
3865         if (sizeof(holelen) > sizeof(hlen)) {
3866                 long long holeend =
3867                         (holebegin + holelen + PAGE_SIZE - 1) >> PAGE_SHIFT;
3868                 if (holeend & ~(long long)ULONG_MAX)
3869                         hlen = ULONG_MAX - hba + 1;
3870         }
3871
3872         unmap_mapping_pages(mapping, hba, hlen, even_cows);
3873 }
3874 EXPORT_SYMBOL(unmap_mapping_range);
3875
3876 /*
3877  * Restore a potential device exclusive pte to a working pte entry
3878  */
3879 static vm_fault_t remove_device_exclusive_entry(struct vm_fault *vmf)
3880 {
3881         struct folio *folio = page_folio(vmf->page);
3882         struct vm_area_struct *vma = vmf->vma;
3883         struct mmu_notifier_range range;
3884         vm_fault_t ret;
3885
3886         /*
3887          * We need a reference to lock the folio because we don't hold
3888          * the PTL so a racing thread can remove the device-exclusive
3889          * entry and unmap it. If the folio is free the entry must
3890          * have been removed already. If it happens to have already
3891          * been re-allocated after being freed all we do is lock and
3892          * unlock it.
3893          */
3894         if (!folio_try_get(folio))
3895                 return 0;
3896
3897         ret = folio_lock_or_retry(folio, vmf);
3898         if (ret) {
3899                 folio_put(folio);
3900                 return ret;
3901         }
3902         mmu_notifier_range_init_owner(&range, MMU_NOTIFY_EXCLUSIVE, 0,
3903                                 vma->vm_mm, vmf->address & PAGE_MASK,
3904                                 (vmf->address & PAGE_MASK) + PAGE_SIZE, NULL);
3905         mmu_notifier_invalidate_range_start(&range);
3906
3907         vmf->pte = pte_offset_map_lock(vma->vm_mm, vmf->pmd, vmf->address,
3908                                 &vmf->ptl);
3909         if (likely(vmf->pte && pte_same(ptep_get(vmf->pte), vmf->orig_pte)))
3910                 restore_exclusive_pte(vma, vmf->page, vmf->address, vmf->pte);
3911
3912         if (vmf->pte)
3913                 pte_unmap_unlock(vmf->pte, vmf->ptl);
3914         folio_unlock(folio);
3915         folio_put(folio);
3916
3917         mmu_notifier_invalidate_range_end(&range);
3918         return 0;
3919 }
3920
3921 static inline bool should_try_to_free_swap(struct folio *folio,
3922                                            struct vm_area_struct *vma,
3923                                            unsigned int fault_flags)
3924 {
3925         if (!folio_test_swapcache(folio))
3926                 return false;
3927         if (mem_cgroup_swap_full(folio) || (vma->vm_flags & VM_LOCKED) ||
3928             folio_test_mlocked(folio))
3929                 return true;
3930         /*
3931          * If we want to map a page that's in the swapcache writable, we
3932          * have to detect via the refcount if we're really the exclusive
3933          * user. Try freeing the swapcache to get rid of the swapcache
3934          * reference only in case it's likely that we'll be the exlusive user.
3935          */
3936         return (fault_flags & FAULT_FLAG_WRITE) && !folio_test_ksm(folio) &&
3937                 folio_ref_count(folio) == (1 + folio_nr_pages(folio));
3938 }
3939
3940 static vm_fault_t pte_marker_clear(struct vm_fault *vmf)
3941 {
3942         vmf->pte = pte_offset_map_lock(vmf->vma->vm_mm, vmf->pmd,
3943                                        vmf->address, &vmf->ptl);
3944         if (!vmf->pte)
3945                 return 0;
3946         /*
3947          * Be careful so that we will only recover a special uffd-wp pte into a
3948          * none pte.  Otherwise it means the pte could have changed, so retry.
3949          *
3950          * This should also cover the case where e.g. the pte changed
3951          * quickly from a PTE_MARKER_UFFD_WP into PTE_MARKER_POISONED.
3952          * So is_pte_marker() check is not enough to safely drop the pte.
3953          */
3954         if (pte_same(vmf->orig_pte, ptep_get(vmf->pte)))
3955                 pte_clear(vmf->vma->vm_mm, vmf->address, vmf->pte);
3956         pte_unmap_unlock(vmf->pte, vmf->ptl);
3957         return 0;
3958 }
3959
3960 static vm_fault_t do_pte_missing(struct vm_fault *vmf)
3961 {
3962         if (vma_is_anonymous(vmf->vma))
3963                 return do_anonymous_page(vmf);
3964         else
3965                 return do_fault(vmf);
3966 }
3967
3968 /*
3969  * This is actually a page-missing access, but with uffd-wp special pte
3970  * installed.  It means this pte was wr-protected before being unmapped.
3971  */
3972 static vm_fault_t pte_marker_handle_uffd_wp(struct vm_fault *vmf)
3973 {
3974         /*
3975          * Just in case there're leftover special ptes even after the region
3976          * got unregistered - we can simply clear them.
3977          */
3978         if (unlikely(!userfaultfd_wp(vmf->vma)))
3979                 return pte_marker_clear(vmf);
3980
3981         return do_pte_missing(vmf);
3982 }
3983
3984 static vm_fault_t handle_pte_marker(struct vm_fault *vmf)
3985 {
3986         swp_entry_t entry = pte_to_swp_entry(vmf->orig_pte);
3987         unsigned long marker = pte_marker_get(entry);
3988
3989         /*
3990          * PTE markers should never be empty.  If anything weird happened,
3991          * the best thing to do is to kill the process along with its mm.
3992          */
3993         if (WARN_ON_ONCE(!marker))
3994                 return VM_FAULT_SIGBUS;
3995
3996         /* Higher priority than uffd-wp when data corrupted */
3997         if (marker & PTE_MARKER_POISONED)
3998                 return VM_FAULT_HWPOISON;
3999
4000         if (pte_marker_entry_uffd_wp(entry))
4001                 return pte_marker_handle_uffd_wp(vmf);
4002
4003         /* This is an unknown pte marker */
4004         return VM_FAULT_SIGBUS;
4005 }
4006
4007 static struct folio *__alloc_swap_folio(struct vm_fault *vmf)
4008 {
4009         struct vm_area_struct *vma = vmf->vma;
4010         struct folio *folio;
4011         swp_entry_t entry;
4012
4013         folio = vma_alloc_folio(GFP_HIGHUSER_MOVABLE, 0, vma,
4014                                 vmf->address, false);
4015         if (!folio)
4016                 return NULL;
4017
4018         entry = pte_to_swp_entry(vmf->orig_pte);
4019         if (mem_cgroup_swapin_charge_folio(folio, vma->vm_mm,
4020                                            GFP_KERNEL, entry)) {
4021                 folio_put(folio);
4022                 return NULL;
4023         }
4024
4025         return folio;
4026 }
4027
4028 #ifdef CONFIG_TRANSPARENT_HUGEPAGE
4029 static inline int non_swapcache_batch(swp_entry_t entry, int max_nr)
4030 {
4031         struct swap_info_struct *si = swp_swap_info(entry);
4032         pgoff_t offset = swp_offset(entry);
4033         int i;
4034
4035         /*
4036          * While allocating a large folio and doing swap_read_folio, which is
4037          * the case the being faulted pte doesn't have swapcache. We need to
4038          * ensure all PTEs have no cache as well, otherwise, we might go to
4039          * swap devices while the content is in swapcache.
4040          */
4041         for (i = 0; i < max_nr; i++) {
4042                 if ((si->swap_map[offset + i] & SWAP_HAS_CACHE))
4043                         return i;
4044         }
4045
4046         return i;
4047 }
4048
4049 /*
4050  * Check if the PTEs within a range are contiguous swap entries
4051  * and have consistent swapcache, zeromap.
4052  */
4053 static bool can_swapin_thp(struct vm_fault *vmf, pte_t *ptep, int nr_pages)
4054 {
4055         unsigned long addr;
4056         swp_entry_t entry;
4057         int idx;
4058         pte_t pte;
4059
4060         addr = ALIGN_DOWN(vmf->address, nr_pages * PAGE_SIZE);
4061         idx = (vmf->address - addr) / PAGE_SIZE;
4062         pte = ptep_get(ptep);
4063
4064         if (!pte_same(pte, pte_move_swp_offset(vmf->orig_pte, -idx)))
4065                 return false;
4066         entry = pte_to_swp_entry(pte);
4067         if (swap_pte_batch(ptep, nr_pages, pte) != nr_pages)
4068                 return false;
4069
4070         /*
4071          * swap_read_folio() can't handle the case a large folio is hybridly
4072          * from different backends. And they are likely corner cases. Similar
4073          * things might be added once zswap support large folios.
4074          */
4075         if (unlikely(swap_zeromap_batch(entry, nr_pages, NULL) != nr_pages))
4076                 return false;
4077         if (unlikely(non_swapcache_batch(entry, nr_pages) != nr_pages))
4078                 return false;
4079
4080         return true;
4081 }
4082
4083 static inline unsigned long thp_swap_suitable_orders(pgoff_t swp_offset,
4084                                                      unsigned long addr,
4085                                                      unsigned long orders)
4086 {
4087         int order, nr;
4088
4089         order = highest_order(orders);
4090
4091         /*
4092          * To swap in a THP with nr pages, we require that its first swap_offset
4093          * is aligned with that number, as it was when the THP was swapped out.
4094          * This helps filter out most invalid entries.
4095          */
4096         while (orders) {
4097                 nr = 1 << order;
4098                 if ((addr >> PAGE_SHIFT) % nr == swp_offset % nr)
4099                         break;
4100                 order = next_order(&orders, order);
4101         }
4102
4103         return orders;
4104 }
4105
4106 static struct folio *alloc_swap_folio(struct vm_fault *vmf)
4107 {
4108         struct vm_area_struct *vma = vmf->vma;
4109         unsigned long orders;
4110         struct folio *folio;
4111         unsigned long addr;
4112         swp_entry_t entry;
4113         spinlock_t *ptl;
4114         pte_t *pte;
4115         gfp_t gfp;
4116         int order;
4117
4118         /*
4119          * If uffd is active for the vma we need per-page fault fidelity to
4120          * maintain the uffd semantics.
4121          */
4122         if (unlikely(userfaultfd_armed(vma)))
4123                 goto fallback;
4124
4125         /*
4126          * A large swapped out folio could be partially or fully in zswap. We
4127          * lack handling for such cases, so fallback to swapping in order-0
4128          * folio.
4129          */
4130         if (!zswap_never_enabled())
4131                 goto fallback;
4132
4133         entry = pte_to_swp_entry(vmf->orig_pte);
4134         /*
4135          * Get a list of all the (large) orders below PMD_ORDER that are enabled
4136          * and suitable for swapping THP.
4137          */
4138         orders = thp_vma_allowable_orders(vma, vma->vm_flags,
4139                         TVA_IN_PF | TVA_ENFORCE_SYSFS, BIT(PMD_ORDER) - 1);
4140         orders = thp_vma_suitable_orders(vma, vmf->address, orders);
4141         orders = thp_swap_suitable_orders(swp_offset(entry),
4142                                           vmf->address, orders);
4143
4144         if (!orders)
4145                 goto fallback;
4146
4147         pte = pte_offset_map_lock(vmf->vma->vm_mm, vmf->pmd,
4148                                   vmf->address & PMD_MASK, &ptl);
4149         if (unlikely(!pte))
4150                 goto fallback;
4151
4152         /*
4153          * For do_swap_page, find the highest order where the aligned range is
4154          * completely swap entries with contiguous swap offsets.
4155          */
4156         order = highest_order(orders);
4157         while (orders) {
4158                 addr = ALIGN_DOWN(vmf->address, PAGE_SIZE << order);
4159                 if (can_swapin_thp(vmf, pte + pte_index(addr), 1 << order))
4160                         break;
4161                 order = next_order(&orders, order);
4162         }
4163
4164         pte_unmap_unlock(pte, ptl);
4165
4166         /* Try allocating the highest of the remaining orders. */
4167         gfp = vma_thp_gfp_mask(vma);
4168         while (orders) {
4169                 addr = ALIGN_DOWN(vmf->address, PAGE_SIZE << order);
4170                 folio = vma_alloc_folio(gfp, order, vma, addr, true);
4171                 if (folio) {
4172                         if (!mem_cgroup_swapin_charge_folio(folio, vma->vm_mm,
4173                                                             gfp, entry))
4174                                 return folio;
4175                         folio_put(folio);
4176                 }
4177                 order = next_order(&orders, order);
4178         }
4179
4180 fallback:
4181         return __alloc_swap_folio(vmf);
4182 }
4183 #else /* !CONFIG_TRANSPARENT_HUGEPAGE */
4184 static inline bool can_swapin_thp(struct vm_fault *vmf, pte_t *ptep, int nr_pages)
4185 {
4186         return false;
4187 }
4188
4189 static struct folio *alloc_swap_folio(struct vm_fault *vmf)
4190 {
4191         return __alloc_swap_folio(vmf);
4192 }
4193 #endif /* CONFIG_TRANSPARENT_HUGEPAGE */
4194
4195 /*
4196  * We enter with non-exclusive mmap_lock (to exclude vma changes,
4197  * but allow concurrent faults), and pte mapped but not yet locked.
4198  * We return with pte unmapped and unlocked.
4199  *
4200  * We return with the mmap_lock locked or unlocked in the same cases
4201  * as does filemap_fault().
4202  */
4203 vm_fault_t do_swap_page(struct vm_fault *vmf)
4204 {
4205         struct vm_area_struct *vma = vmf->vma;
4206         struct folio *swapcache, *folio = NULL;
4207         struct page *page;
4208         struct swap_info_struct *si = NULL;
4209         rmap_t rmap_flags = RMAP_NONE;
4210         bool need_clear_cache = false;
4211         bool exclusive = false;
4212         swp_entry_t entry;
4213         pte_t pte;
4214         vm_fault_t ret = 0;
4215         void *shadow = NULL;
4216         int nr_pages;
4217         unsigned long page_idx;
4218         unsigned long address;
4219         pte_t *ptep;
4220
4221         if (!pte_unmap_same(vmf))
4222                 goto out;
4223
4224         entry = pte_to_swp_entry(vmf->orig_pte);
4225         if (unlikely(non_swap_entry(entry))) {
4226                 if (is_migration_entry(entry)) {
4227                         migration_entry_wait(vma->vm_mm, vmf->pmd,
4228                                              vmf->address);
4229                 } else if (is_device_exclusive_entry(entry)) {
4230                         vmf->page = pfn_swap_entry_to_page(entry);
4231                         ret = remove_device_exclusive_entry(vmf);
4232                 } else if (is_device_private_entry(entry)) {
4233                         if (vmf->flags & FAULT_FLAG_VMA_LOCK) {
4234                                 /*
4235                                  * migrate_to_ram is not yet ready to operate
4236                                  * under VMA lock.
4237                                  */
4238                                 vma_end_read(vma);
4239                                 ret = VM_FAULT_RETRY;
4240                                 goto out;
4241                         }
4242
4243                         vmf->page = pfn_swap_entry_to_page(entry);
4244                         vmf->pte = pte_offset_map_lock(vma->vm_mm, vmf->pmd,
4245                                         vmf->address, &vmf->ptl);
4246                         if (unlikely(!vmf->pte ||
4247                                      !pte_same(ptep_get(vmf->pte),
4248                                                         vmf->orig_pte)))
4249                                 goto unlock;
4250
4251                         /*
4252                          * Get a page reference while we know the page can't be
4253                          * freed.
4254                          */
4255                         get_page(vmf->page);
4256                         pte_unmap_unlock(vmf->pte, vmf->ptl);
4257                         ret = vmf->page->pgmap->ops->migrate_to_ram(vmf);
4258                         put_page(vmf->page);
4259                 } else if (is_hwpoison_entry(entry)) {
4260                         ret = VM_FAULT_HWPOISON;
4261                 } else if (is_pte_marker_entry(entry)) {
4262                         ret = handle_pte_marker(vmf);
4263                 } else {
4264                         print_bad_pte(vma, vmf->address, vmf->orig_pte, NULL);
4265                         ret = VM_FAULT_SIGBUS;
4266                 }
4267                 goto out;
4268         }
4269
4270         /* Prevent swapoff from happening to us. */
4271         si = get_swap_device(entry);
4272         if (unlikely(!si))
4273                 goto out;
4274
4275         folio = swap_cache_get_folio(entry, vma, vmf->address);
4276         if (folio)
4277                 page = folio_file_page(folio, swp_offset(entry));
4278         swapcache = folio;
4279
4280         if (!folio) {
4281                 if (data_race(si->flags & SWP_SYNCHRONOUS_IO) &&
4282                     __swap_count(entry) == 1) {
4283                         /* skip swapcache */
4284                         folio = alloc_swap_folio(vmf);
4285                         if (folio) {
4286                                 __folio_set_locked(folio);
4287                                 __folio_set_swapbacked(folio);
4288
4289                                 nr_pages = folio_nr_pages(folio);
4290                                 if (folio_test_large(folio))
4291                                         entry.val = ALIGN_DOWN(entry.val, nr_pages);
4292                                 /*
4293                                  * Prevent parallel swapin from proceeding with
4294                                  * the cache flag. Otherwise, another thread
4295                                  * may finish swapin first, free the entry, and
4296                                  * swapout reusing the same entry. It's
4297                                  * undetectable as pte_same() returns true due
4298                                  * to entry reuse.
4299                                  */
4300                                 if (swapcache_prepare(entry, nr_pages)) {
4301                                         /*
4302                                          * Relax a bit to prevent rapid
4303                                          * repeated page faults.
4304                                          */
4305                                         schedule_timeout_uninterruptible(1);
4306                                         goto out_page;
4307                                 }
4308                                 need_clear_cache = true;
4309
4310                                 mem_cgroup_swapin_uncharge_swap(entry, nr_pages);
4311
4312                                 shadow = get_shadow_from_swap_cache(entry);
4313                                 if (shadow)
4314                                         workingset_refault(folio, shadow);
4315
4316                                 folio_add_lru(folio);
4317
4318                                 /* To provide entry to swap_read_folio() */
4319                                 folio->swap = entry;
4320                                 swap_read_folio(folio, NULL);
4321                                 folio->private = NULL;
4322                         }
4323                 } else {
4324                         folio = swapin_readahead(entry, GFP_HIGHUSER_MOVABLE,
4325                                                 vmf);
4326                         swapcache = folio;
4327                 }
4328
4329                 if (!folio) {
4330                         /*
4331                          * Back out if somebody else faulted in this pte
4332                          * while we released the pte lock.
4333                          */
4334                         vmf->pte = pte_offset_map_lock(vma->vm_mm, vmf->pmd,
4335                                         vmf->address, &vmf->ptl);
4336                         if (likely(vmf->pte &&
4337                                    pte_same(ptep_get(vmf->pte), vmf->orig_pte)))
4338                                 ret = VM_FAULT_OOM;
4339                         goto unlock;
4340                 }
4341
4342                 /* Had to read the page from swap area: Major fault */
4343                 ret = VM_FAULT_MAJOR;
4344                 count_vm_event(PGMAJFAULT);
4345                 count_memcg_event_mm(vma->vm_mm, PGMAJFAULT);
4346                 page = folio_file_page(folio, swp_offset(entry));
4347         } else if (PageHWPoison(page)) {
4348                 /*
4349                  * hwpoisoned dirty swapcache pages are kept for killing
4350                  * owner processes (which may be unknown at hwpoison time)
4351                  */
4352                 ret = VM_FAULT_HWPOISON;
4353                 goto out_release;
4354         }
4355
4356         ret |= folio_lock_or_retry(folio, vmf);
4357         if (ret & VM_FAULT_RETRY)
4358                 goto out_release;
4359
4360         if (swapcache) {
4361                 /*
4362                  * Make sure folio_free_swap() or swapoff did not release the
4363                  * swapcache from under us.  The page pin, and pte_same test
4364                  * below, are not enough to exclude that.  Even if it is still
4365                  * swapcache, we need to check that the page's swap has not
4366                  * changed.
4367                  */
4368                 if (unlikely(!folio_test_swapcache(folio) ||
4369                              page_swap_entry(page).val != entry.val))
4370                         goto out_page;
4371
4372                 /*
4373                  * KSM sometimes has to copy on read faults, for example, if
4374                  * page->index of !PageKSM() pages would be nonlinear inside the
4375                  * anon VMA -- PageKSM() is lost on actual swapout.
4376                  */
4377                 folio = ksm_might_need_to_copy(folio, vma, vmf->address);
4378                 if (unlikely(!folio)) {
4379                         ret = VM_FAULT_OOM;
4380                         folio = swapcache;
4381                         goto out_page;
4382                 } else if (unlikely(folio == ERR_PTR(-EHWPOISON))) {
4383                         ret = VM_FAULT_HWPOISON;
4384                         folio = swapcache;
4385                         goto out_page;
4386                 }
4387                 if (folio != swapcache)
4388                         page = folio_page(folio, 0);
4389
4390                 /*
4391                  * If we want to map a page that's in the swapcache writable, we
4392                  * have to detect via the refcount if we're really the exclusive
4393                  * owner. Try removing the extra reference from the local LRU
4394                  * caches if required.
4395                  */
4396                 if ((vmf->flags & FAULT_FLAG_WRITE) && folio == swapcache &&
4397                     !folio_test_ksm(folio) && !folio_test_lru(folio))
4398                         lru_add_drain();
4399         }
4400
4401         folio_throttle_swaprate(folio, GFP_KERNEL);
4402
4403         /*
4404          * Back out if somebody else already faulted in this pte.
4405          */
4406         vmf->pte = pte_offset_map_lock(vma->vm_mm, vmf->pmd, vmf->address,
4407                         &vmf->ptl);
4408         if (unlikely(!vmf->pte || !pte_same(ptep_get(vmf->pte), vmf->orig_pte)))
4409                 goto out_nomap;
4410
4411         if (unlikely(!folio_test_uptodate(folio))) {
4412                 ret = VM_FAULT_SIGBUS;
4413                 goto out_nomap;
4414         }
4415
4416         /* allocated large folios for SWP_SYNCHRONOUS_IO */
4417         if (folio_test_large(folio) && !folio_test_swapcache(folio)) {
4418                 unsigned long nr = folio_nr_pages(folio);
4419                 unsigned long folio_start = ALIGN_DOWN(vmf->address, nr * PAGE_SIZE);
4420                 unsigned long idx = (vmf->address - folio_start) / PAGE_SIZE;
4421                 pte_t *folio_ptep = vmf->pte - idx;
4422                 pte_t folio_pte = ptep_get(folio_ptep);
4423
4424                 if (!pte_same(folio_pte, pte_move_swp_offset(vmf->orig_pte, -idx)) ||
4425                     swap_pte_batch(folio_ptep, nr, folio_pte) != nr)
4426                         goto out_nomap;
4427
4428                 page_idx = idx;
4429                 address = folio_start;
4430                 ptep = folio_ptep;
4431                 goto check_folio;
4432         }
4433
4434         nr_pages = 1;
4435         page_idx = 0;
4436         address = vmf->address;
4437         ptep = vmf->pte;
4438         if (folio_test_large(folio) && folio_test_swapcache(folio)) {
4439                 int nr = folio_nr_pages(folio);
4440                 unsigned long idx = folio_page_idx(folio, page);
4441                 unsigned long folio_start = address - idx * PAGE_SIZE;
4442                 unsigned long folio_end = folio_start + nr * PAGE_SIZE;
4443                 pte_t *folio_ptep;
4444                 pte_t folio_pte;
4445
4446                 if (unlikely(folio_start < max(address & PMD_MASK, vma->vm_start)))
4447                         goto check_folio;
4448                 if (unlikely(folio_end > pmd_addr_end(address, vma->vm_end)))
4449                         goto check_folio;
4450
4451                 folio_ptep = vmf->pte - idx;
4452                 folio_pte = ptep_get(folio_ptep);
4453                 if (!pte_same(folio_pte, pte_move_swp_offset(vmf->orig_pte, -idx)) ||
4454                     swap_pte_batch(folio_ptep, nr, folio_pte) != nr)
4455                         goto check_folio;
4456
4457                 page_idx = idx;
4458                 address = folio_start;
4459                 ptep = folio_ptep;
4460                 nr_pages = nr;
4461                 entry = folio->swap;
4462                 page = &folio->page;
4463         }
4464
4465 check_folio:
4466         /*
4467          * PG_anon_exclusive reuses PG_mappedtodisk for anon pages. A swap pte
4468          * must never point at an anonymous page in the swapcache that is
4469          * PG_anon_exclusive. Sanity check that this holds and especially, that
4470          * no filesystem set PG_mappedtodisk on a page in the swapcache. Sanity
4471          * check after taking the PT lock and making sure that nobody
4472          * concurrently faulted in this page and set PG_anon_exclusive.
4473          */
4474         BUG_ON(!folio_test_anon(folio) && folio_test_mappedtodisk(folio));
4475         BUG_ON(folio_test_anon(folio) && PageAnonExclusive(page));
4476
4477         /*
4478          * Check under PT lock (to protect against concurrent fork() sharing
4479          * the swap entry concurrently) for certainly exclusive pages.
4480          */
4481         if (!folio_test_ksm(folio)) {
4482                 exclusive = pte_swp_exclusive(vmf->orig_pte);
4483                 if (folio != swapcache) {
4484                         /*
4485                          * We have a fresh page that is not exposed to the
4486                          * swapcache -> certainly exclusive.
4487                          */
4488                         exclusive = true;
4489                 } else if (exclusive && folio_test_writeback(folio) &&
4490                           data_race(si->flags & SWP_STABLE_WRITES)) {
4491                         /*
4492                          * This is tricky: not all swap backends support
4493                          * concurrent page modifications while under writeback.
4494                          *
4495                          * So if we stumble over such a page in the swapcache
4496                          * we must not set the page exclusive, otherwise we can
4497                          * map it writable without further checks and modify it
4498                          * while still under writeback.
4499                          *
4500                          * For these problematic swap backends, simply drop the
4501                          * exclusive marker: this is perfectly fine as we start
4502                          * writeback only if we fully unmapped the page and
4503                          * there are no unexpected references on the page after
4504                          * unmapping succeeded. After fully unmapped, no
4505                          * further GUP references (FOLL_GET and FOLL_PIN) can
4506                          * appear, so dropping the exclusive marker and mapping
4507                          * it only R/O is fine.
4508                          */
4509                         exclusive = false;
4510                 }
4511         }
4512
4513         /*
4514          * Some architectures may have to restore extra metadata to the page
4515          * when reading from swap. This metadata may be indexed by swap entry
4516          * so this must be called before swap_free().
4517          */
4518         arch_swap_restore(folio_swap(entry, folio), folio);
4519
4520         /*
4521          * Remove the swap entry and conditionally try to free up the swapcache.
4522          * We're already holding a reference on the page but haven't mapped it
4523          * yet.
4524          */
4525         swap_free_nr(entry, nr_pages);
4526         if (should_try_to_free_swap(folio, vma, vmf->flags))
4527                 folio_free_swap(folio);
4528
4529         add_mm_counter(vma->vm_mm, MM_ANONPAGES, nr_pages);
4530         add_mm_counter(vma->vm_mm, MM_SWAPENTS, -nr_pages);
4531         pte = mk_pte(page, vma->vm_page_prot);
4532         if (pte_swp_soft_dirty(vmf->orig_pte))
4533                 pte = pte_mksoft_dirty(pte);
4534         if (pte_swp_uffd_wp(vmf->orig_pte))
4535                 pte = pte_mkuffd_wp(pte);
4536
4537         /*
4538          * Same logic as in do_wp_page(); however, optimize for pages that are
4539          * certainly not shared either because we just allocated them without
4540          * exposing them to the swapcache or because the swap entry indicates
4541          * exclusivity.
4542          */
4543         if (!folio_test_ksm(folio) &&
4544             (exclusive || folio_ref_count(folio) == 1)) {
4545                 if ((vma->vm_flags & VM_WRITE) && !userfaultfd_pte_wp(vma, pte) &&
4546                     !pte_needs_soft_dirty_wp(vma, pte)) {
4547                         pte = pte_mkwrite(pte, vma);
4548                         if (vmf->flags & FAULT_FLAG_WRITE) {
4549                                 pte = pte_mkdirty(pte);
4550                                 vmf->flags &= ~FAULT_FLAG_WRITE;
4551                         }
4552                 }
4553                 rmap_flags |= RMAP_EXCLUSIVE;
4554         }
4555         folio_ref_add(folio, nr_pages - 1);
4556         flush_icache_pages(vma, page, nr_pages);
4557         vmf->orig_pte = pte_advance_pfn(pte, page_idx);
4558
4559         /* ksm created a completely new copy */
4560         if (unlikely(folio != swapcache && swapcache)) {
4561                 folio_add_new_anon_rmap(folio, vma, address, RMAP_EXCLUSIVE);
4562                 folio_add_lru_vma(folio, vma);
4563         } else if (!folio_test_anon(folio)) {
4564                 /*
4565                  * We currently only expect small !anon folios which are either
4566                  * fully exclusive or fully shared, or new allocated large
4567                  * folios which are fully exclusive. If we ever get large
4568                  * folios within swapcache here, we have to be careful.
4569                  */
4570                 VM_WARN_ON_ONCE(folio_test_large(folio) && folio_test_swapcache(folio));
4571                 VM_WARN_ON_FOLIO(!folio_test_locked(folio), folio);
4572                 folio_add_new_anon_rmap(folio, vma, address, rmap_flags);
4573         } else {
4574                 folio_add_anon_rmap_ptes(folio, page, nr_pages, vma, address,
4575                                         rmap_flags);
4576         }
4577
4578         VM_BUG_ON(!folio_test_anon(folio) ||
4579                         (pte_write(pte) && !PageAnonExclusive(page)));
4580         set_ptes(vma->vm_mm, address, ptep, pte, nr_pages);
4581         arch_do_swap_page_nr(vma->vm_mm, vma, address,
4582                         pte, pte, nr_pages);
4583
4584         folio_unlock(folio);
4585         if (folio != swapcache && swapcache) {
4586                 /*
4587                  * Hold the lock to avoid the swap entry to be reused
4588                  * until we take the PT lock for the pte_same() check
4589                  * (to avoid false positives from pte_same). For
4590                  * further safety release the lock after the swap_free
4591                  * so that the swap count won't change under a
4592                  * parallel locked swapcache.
4593                  */
4594                 folio_unlock(swapcache);
4595                 folio_put(swapcache);
4596         }
4597
4598         if (vmf->flags & FAULT_FLAG_WRITE) {
4599                 ret |= do_wp_page(vmf);
4600                 if (ret & VM_FAULT_ERROR)
4601                         ret &= VM_FAULT_ERROR;
4602                 goto out;
4603         }
4604
4605         /* No need to invalidate - it was non-present before */
4606         update_mmu_cache_range(vmf, vma, address, ptep, nr_pages);
4607 unlock:
4608         if (vmf->pte)
4609                 pte_unmap_unlock(vmf->pte, vmf->ptl);
4610 out:
4611         /* Clear the swap cache pin for direct swapin after PTL unlock */
4612         if (need_clear_cache)
4613                 swapcache_clear(si, entry, nr_pages);
4614         if (si)
4615                 put_swap_device(si);
4616         return ret;
4617 out_nomap:
4618         if (vmf->pte)
4619                 pte_unmap_unlock(vmf->pte, vmf->ptl);
4620 out_page:
4621         folio_unlock(folio);
4622 out_release:
4623         folio_put(folio);
4624         if (folio != swapcache && swapcache) {
4625                 folio_unlock(swapcache);
4626                 folio_put(swapcache);
4627         }
4628         if (need_clear_cache)
4629                 swapcache_clear(si, entry, nr_pages);
4630         if (si)
4631                 put_swap_device(si);
4632         return ret;
4633 }
4634
4635 static bool pte_range_none(pte_t *pte, int nr_pages)
4636 {
4637         int i;
4638
4639         for (i = 0; i < nr_pages; i++) {
4640                 if (!pte_none(ptep_get_lockless(pte + i)))
4641                         return false;
4642         }
4643
4644         return true;
4645 }
4646
4647 static struct folio *alloc_anon_folio(struct vm_fault *vmf)
4648 {
4649         struct vm_area_struct *vma = vmf->vma;
4650 #ifdef CONFIG_TRANSPARENT_HUGEPAGE
4651         unsigned long orders;
4652         struct folio *folio;
4653         unsigned long addr;
4654         pte_t *pte;
4655         gfp_t gfp;
4656         int order;
4657
4658         /*
4659          * If uffd is active for the vma we need per-page fault fidelity to
4660          * maintain the uffd semantics.
4661          */
4662         if (unlikely(userfaultfd_armed(vma)))
4663                 goto fallback;
4664
4665         /*
4666          * Get a list of all the (large) orders below PMD_ORDER that are enabled
4667          * for this vma. Then filter out the orders that can't be allocated over
4668          * the faulting address and still be fully contained in the vma.
4669          */
4670         orders = thp_vma_allowable_orders(vma, vma->vm_flags,
4671                         TVA_IN_PF | TVA_ENFORCE_SYSFS, BIT(PMD_ORDER) - 1);
4672         orders = thp_vma_suitable_orders(vma, vmf->address, orders);
4673
4674         if (!orders)
4675                 goto fallback;
4676
4677         pte = pte_offset_map(vmf->pmd, vmf->address & PMD_MASK);
4678         if (!pte)
4679                 return ERR_PTR(-EAGAIN);
4680
4681         /*
4682          * Find the highest order where the aligned range is completely
4683          * pte_none(). Note that all remaining orders will be completely
4684          * pte_none().
4685          */
4686         order = highest_order(orders);
4687         while (orders) {
4688                 addr = ALIGN_DOWN(vmf->address, PAGE_SIZE << order);
4689                 if (pte_range_none(pte + pte_index(addr), 1 << order))
4690                         break;
4691                 order = next_order(&orders, order);
4692         }
4693
4694         pte_unmap(pte);
4695
4696         if (!orders)
4697                 goto fallback;
4698
4699         /* Try allocating the highest of the remaining orders. */
4700         gfp = vma_thp_gfp_mask(vma);
4701         while (orders) {
4702                 addr = ALIGN_DOWN(vmf->address, PAGE_SIZE << order);
4703                 folio = vma_alloc_folio(gfp, order, vma, addr, true);
4704                 if (folio) {
4705                         if (mem_cgroup_charge(folio, vma->vm_mm, gfp)) {
4706                                 count_mthp_stat(order, MTHP_STAT_ANON_FAULT_FALLBACK_CHARGE);
4707                                 folio_put(folio);
4708                                 goto next;
4709                         }
4710                         folio_throttle_swaprate(folio, gfp);
4711                         folio_zero_user(folio, vmf->address);
4712                         return folio;
4713                 }
4714 next:
4715                 count_mthp_stat(order, MTHP_STAT_ANON_FAULT_FALLBACK);
4716                 order = next_order(&orders, order);
4717         }
4718
4719 fallback:
4720 #endif
4721         return folio_prealloc(vma->vm_mm, vma, vmf->address, true);
4722 }
4723
4724 /*
4725  * We enter with non-exclusive mmap_lock (to exclude vma changes,
4726  * but allow concurrent faults), and pte mapped but not yet locked.
4727  * We return with mmap_lock still held, but pte unmapped and unlocked.
4728  */
4729 static vm_fault_t do_anonymous_page(struct vm_fault *vmf)
4730 {
4731         struct vm_area_struct *vma = vmf->vma;
4732         unsigned long addr = vmf->address;
4733         struct folio *folio;
4734         vm_fault_t ret = 0;
4735         int nr_pages = 1;
4736         pte_t entry;
4737
4738         /* File mapping without ->vm_ops ? */
4739         if (vma->vm_flags & VM_SHARED)
4740                 return VM_FAULT_SIGBUS;
4741
4742         /*
4743          * Use pte_alloc() instead of pte_alloc_map(), so that OOM can
4744          * be distinguished from a transient failure of pte_offset_map().
4745          */
4746         if (pte_alloc(vma->vm_mm, vmf->pmd))
4747                 return VM_FAULT_OOM;
4748
4749         /* Use the zero-page for reads */
4750         if (!(vmf->flags & FAULT_FLAG_WRITE) &&
4751                         !mm_forbids_zeropage(vma->vm_mm)) {
4752                 entry = pte_mkspecial(pfn_pte(my_zero_pfn(vmf->address),
4753                                                 vma->vm_page_prot));
4754                 vmf->pte = pte_offset_map_lock(vma->vm_mm, vmf->pmd,
4755                                 vmf->address, &vmf->ptl);
4756                 if (!vmf->pte)
4757                         goto unlock;
4758                 if (vmf_pte_changed(vmf)) {
4759                         update_mmu_tlb(vma, vmf->address, vmf->pte);
4760                         goto unlock;
4761                 }
4762                 ret = check_stable_address_space(vma->vm_mm);
4763                 if (ret)
4764                         goto unlock;
4765                 /* Deliver the page fault to userland, check inside PT lock */
4766                 if (userfaultfd_missing(vma)) {
4767                         pte_unmap_unlock(vmf->pte, vmf->ptl);
4768                         return handle_userfault(vmf, VM_UFFD_MISSING);
4769                 }
4770                 goto setpte;
4771         }
4772
4773         /* Allocate our own private page. */
4774         ret = vmf_anon_prepare(vmf);
4775         if (ret)
4776                 return ret;
4777         /* Returns NULL on OOM or ERR_PTR(-EAGAIN) if we must retry the fault */
4778         folio = alloc_anon_folio(vmf);
4779         if (IS_ERR(folio))
4780                 return 0;
4781         if (!folio)
4782                 goto oom;
4783
4784         nr_pages = folio_nr_pages(folio);
4785         addr = ALIGN_DOWN(vmf->address, nr_pages * PAGE_SIZE);
4786
4787         /*
4788          * The memory barrier inside __folio_mark_uptodate makes sure that
4789          * preceding stores to the page contents become visible before
4790          * the set_pte_at() write.
4791          */
4792         __folio_mark_uptodate(folio);
4793
4794         entry = mk_pte(&folio->page, vma->vm_page_prot);
4795         entry = pte_sw_mkyoung(entry);
4796         if (vma->vm_flags & VM_WRITE)
4797                 entry = pte_mkwrite(pte_mkdirty(entry), vma);
4798
4799         vmf->pte = pte_offset_map_lock(vma->vm_mm, vmf->pmd, addr, &vmf->ptl);
4800         if (!vmf->pte)
4801                 goto release;
4802         if (nr_pages == 1 && vmf_pte_changed(vmf)) {
4803                 update_mmu_tlb(vma, addr, vmf->pte);
4804                 goto release;
4805         } else if (nr_pages > 1 && !pte_range_none(vmf->pte, nr_pages)) {
4806                 update_mmu_tlb_range(vma, addr, vmf->pte, nr_pages);
4807                 goto release;
4808         }
4809
4810         ret = check_stable_address_space(vma->vm_mm);
4811         if (ret)
4812                 goto release;
4813
4814         /* Deliver the page fault to userland, check inside PT lock */
4815         if (userfaultfd_missing(vma)) {
4816                 pte_unmap_unlock(vmf->pte, vmf->ptl);
4817                 folio_put(folio);
4818                 return handle_userfault(vmf, VM_UFFD_MISSING);
4819         }
4820
4821         folio_ref_add(folio, nr_pages - 1);
4822         add_mm_counter(vma->vm_mm, MM_ANONPAGES, nr_pages);
4823         count_mthp_stat(folio_order(folio), MTHP_STAT_ANON_FAULT_ALLOC);
4824         folio_add_new_anon_rmap(folio, vma, addr, RMAP_EXCLUSIVE);
4825         folio_add_lru_vma(folio, vma);
4826 setpte:
4827         if (vmf_orig_pte_uffd_wp(vmf))
4828                 entry = pte_mkuffd_wp(entry);
4829         set_ptes(vma->vm_mm, addr, vmf->pte, entry, nr_pages);
4830
4831         /* No need to invalidate - it was non-present before */
4832         update_mmu_cache_range(vmf, vma, addr, vmf->pte, nr_pages);
4833 unlock:
4834         if (vmf->pte)
4835                 pte_unmap_unlock(vmf->pte, vmf->ptl);
4836         return ret;
4837 release:
4838         folio_put(folio);
4839         goto unlock;
4840 oom:
4841         return VM_FAULT_OOM;
4842 }
4843
4844 /*
4845  * The mmap_lock must have been held on entry, and may have been
4846  * released depending on flags and vma->vm_ops->fault() return value.
4847  * See filemap_fault() and __lock_page_retry().
4848  */
4849 static vm_fault_t __do_fault(struct vm_fault *vmf)
4850 {
4851         struct vm_area_struct *vma = vmf->vma;
4852         struct folio *folio;
4853         vm_fault_t ret;
4854
4855         /*
4856          * Preallocate pte before we take page_lock because this might lead to
4857          * deadlocks for memcg reclaim which waits for pages under writeback:
4858          *                              lock_page(A)
4859          *                              SetPageWriteback(A)
4860          *                              unlock_page(A)
4861          * lock_page(B)
4862          *                              lock_page(B)
4863          * pte_alloc_one
4864          *   shrink_folio_list
4865          *     wait_on_page_writeback(A)
4866          *                              SetPageWriteback(B)
4867          *                              unlock_page(B)
4868          *                              # flush A, B to clear the writeback
4869          */
4870         if (pmd_none(*vmf->pmd) && !vmf->prealloc_pte) {
4871                 vmf->prealloc_pte = pte_alloc_one(vma->vm_mm);
4872                 if (!vmf->prealloc_pte)
4873                         return VM_FAULT_OOM;
4874         }
4875
4876         ret = vma->vm_ops->fault(vmf);
4877         if (unlikely(ret & (VM_FAULT_ERROR | VM_FAULT_NOPAGE | VM_FAULT_RETRY |
4878                             VM_FAULT_DONE_COW)))
4879                 return ret;
4880
4881         folio = page_folio(vmf->page);
4882         if (unlikely(PageHWPoison(vmf->page))) {
4883                 vm_fault_t poisonret = VM_FAULT_HWPOISON;
4884                 if (ret & VM_FAULT_LOCKED) {
4885                         if (page_mapped(vmf->page))
4886                                 unmap_mapping_folio(folio);
4887                         /* Retry if a clean folio was removed from the cache. */
4888                         if (mapping_evict_folio(folio->mapping, folio))
4889                                 poisonret = VM_FAULT_NOPAGE;
4890                         folio_unlock(folio);
4891                 }
4892                 folio_put(folio);
4893                 vmf->page = NULL;
4894                 return poisonret;
4895         }
4896
4897         if (unlikely(!(ret & VM_FAULT_LOCKED)))
4898                 folio_lock(folio);
4899         else
4900                 VM_BUG_ON_PAGE(!folio_test_locked(folio), vmf->page);
4901
4902         return ret;
4903 }
4904
4905 #ifdef CONFIG_TRANSPARENT_HUGEPAGE
4906 static void deposit_prealloc_pte(struct vm_fault *vmf)
4907 {
4908         struct vm_area_struct *vma = vmf->vma;
4909
4910         pgtable_trans_huge_deposit(vma->vm_mm, vmf->pmd, vmf->prealloc_pte);
4911         /*
4912          * We are going to consume the prealloc table,
4913          * count that as nr_ptes.
4914          */
4915         mm_inc_nr_ptes(vma->vm_mm);
4916         vmf->prealloc_pte = NULL;
4917 }
4918
4919 vm_fault_t do_set_pmd(struct vm_fault *vmf, struct page *page)
4920 {
4921         struct folio *folio = page_folio(page);
4922         struct vm_area_struct *vma = vmf->vma;
4923         bool write = vmf->flags & FAULT_FLAG_WRITE;
4924         unsigned long haddr = vmf->address & HPAGE_PMD_MASK;
4925         pmd_t entry;
4926         vm_fault_t ret = VM_FAULT_FALLBACK;
4927
4928         if (!thp_vma_suitable_order(vma, haddr, PMD_ORDER))
4929                 return ret;
4930
4931         if (folio_order(folio) != HPAGE_PMD_ORDER)
4932                 return ret;
4933         page = &folio->page;
4934
4935         /*
4936          * Just backoff if any subpage of a THP is corrupted otherwise
4937          * the corrupted page may mapped by PMD silently to escape the
4938          * check.  This kind of THP just can be PTE mapped.  Access to
4939          * the corrupted subpage should trigger SIGBUS as expected.
4940          */
4941         if (unlikely(folio_test_has_hwpoisoned(folio)))
4942                 return ret;
4943
4944         /*
4945          * Archs like ppc64 need additional space to store information
4946          * related to pte entry. Use the preallocated table for that.
4947          */
4948         if (arch_needs_pgtable_deposit() && !vmf->prealloc_pte) {
4949                 vmf->prealloc_pte = pte_alloc_one(vma->vm_mm);
4950                 if (!vmf->prealloc_pte)
4951                         return VM_FAULT_OOM;
4952         }
4953
4954         vmf->ptl = pmd_lock(vma->vm_mm, vmf->pmd);
4955         if (unlikely(!pmd_none(*vmf->pmd)))
4956                 goto out;
4957
4958         flush_icache_pages(vma, page, HPAGE_PMD_NR);
4959
4960         entry = mk_huge_pmd(page, vma->vm_page_prot);
4961         if (write)
4962                 entry = maybe_pmd_mkwrite(pmd_mkdirty(entry), vma);
4963
4964         add_mm_counter(vma->vm_mm, mm_counter_file(folio), HPAGE_PMD_NR);
4965         folio_add_file_rmap_pmd(folio, page, vma);
4966
4967         /*
4968          * deposit and withdraw with pmd lock held
4969          */
4970         if (arch_needs_pgtable_deposit())
4971                 deposit_prealloc_pte(vmf);
4972
4973         set_pmd_at(vma->vm_mm, haddr, vmf->pmd, entry);
4974
4975         update_mmu_cache_pmd(vma, haddr, vmf->pmd);
4976
4977         /* fault is handled */
4978         ret = 0;
4979         count_vm_event(THP_FILE_MAPPED);
4980 out:
4981         spin_unlock(vmf->ptl);
4982         return ret;
4983 }
4984 #else
4985 vm_fault_t do_set_pmd(struct vm_fault *vmf, struct page *page)
4986 {
4987         return VM_FAULT_FALLBACK;
4988 }
4989 #endif
4990
4991 /**
4992  * set_pte_range - Set a range of PTEs to point to pages in a folio.
4993  * @vmf: Fault decription.
4994  * @folio: The folio that contains @page.
4995  * @page: The first page to create a PTE for.
4996  * @nr: The number of PTEs to create.
4997  * @addr: The first address to create a PTE for.
4998  */
4999 void set_pte_range(struct vm_fault *vmf, struct folio *folio,
5000                 struct page *page, unsigned int nr, unsigned long addr)
5001 {
5002         struct vm_area_struct *vma = vmf->vma;
5003         bool write = vmf->flags & FAULT_FLAG_WRITE;
5004         bool prefault = !in_range(vmf->address, addr, nr * PAGE_SIZE);
5005         pte_t entry;
5006
5007         flush_icache_pages(vma, page, nr);
5008         entry = mk_pte(page, vma->vm_page_prot);
5009
5010         if (prefault && arch_wants_old_prefaulted_pte())
5011                 entry = pte_mkold(entry);
5012         else
5013                 entry = pte_sw_mkyoung(entry);
5014
5015         if (write)
5016                 entry = maybe_mkwrite(pte_mkdirty(entry), vma);
5017         if (unlikely(vmf_orig_pte_uffd_wp(vmf)))
5018                 entry = pte_mkuffd_wp(entry);
5019         /* copy-on-write page */
5020         if (write && !(vma->vm_flags & VM_SHARED)) {
5021                 VM_BUG_ON_FOLIO(nr != 1, folio);
5022                 folio_add_new_anon_rmap(folio, vma, addr, RMAP_EXCLUSIVE);
5023                 folio_add_lru_vma(folio, vma);
5024         } else {
5025                 folio_add_file_rmap_ptes(folio, page, nr, vma);
5026         }
5027         set_ptes(vma->vm_mm, addr, vmf->pte, entry, nr);
5028
5029         /* no need to invalidate: a not-present page won't be cached */
5030         update_mmu_cache_range(vmf, vma, addr, vmf->pte, nr);
5031 }
5032
5033 static bool vmf_pte_changed(struct vm_fault *vmf)
5034 {
5035         if (vmf->flags & FAULT_FLAG_ORIG_PTE_VALID)
5036                 return !pte_same(ptep_get(vmf->pte), vmf->orig_pte);
5037
5038         return !pte_none(ptep_get(vmf->pte));
5039 }
5040
5041 /**
5042  * finish_fault - finish page fault once we have prepared the page to fault
5043  *
5044  * @vmf: structure describing the fault
5045  *
5046  * This function handles all that is needed to finish a page fault once the
5047  * page to fault in is prepared. It handles locking of PTEs, inserts PTE for
5048  * given page, adds reverse page mapping, handles memcg charges and LRU
5049  * addition.
5050  *
5051  * The function expects the page to be locked and on success it consumes a
5052  * reference of a page being mapped (for the PTE which maps it).
5053  *
5054  * Return: %0 on success, %VM_FAULT_ code in case of error.
5055  */
5056 vm_fault_t finish_fault(struct vm_fault *vmf)
5057 {
5058         struct vm_area_struct *vma = vmf->vma;
5059         struct page *page;
5060         struct folio *folio;
5061         vm_fault_t ret;
5062         bool is_cow = (vmf->flags & FAULT_FLAG_WRITE) &&
5063                       !(vma->vm_flags & VM_SHARED);
5064         int type, nr_pages;
5065         unsigned long addr = vmf->address;
5066
5067         /* Did we COW the page? */
5068         if (is_cow)
5069                 page = vmf->cow_page;
5070         else
5071                 page = vmf->page;
5072
5073         /*
5074          * check even for read faults because we might have lost our CoWed
5075          * page
5076          */
5077         if (!(vma->vm_flags & VM_SHARED)) {
5078                 ret = check_stable_address_space(vma->vm_mm);
5079                 if (ret)
5080                         return ret;
5081         }
5082
5083         if (pmd_none(*vmf->pmd)) {
5084                 if (PageTransCompound(page)) {
5085                         ret = do_set_pmd(vmf, page);
5086                         if (ret != VM_FAULT_FALLBACK)
5087                                 return ret;
5088                 }
5089
5090                 if (vmf->prealloc_pte)
5091                         pmd_install(vma->vm_mm, vmf->pmd, &vmf->prealloc_pte);
5092                 else if (unlikely(pte_alloc(vma->vm_mm, vmf->pmd)))
5093                         return VM_FAULT_OOM;
5094         }
5095
5096         folio = page_folio(page);
5097         nr_pages = folio_nr_pages(folio);
5098
5099         /*
5100          * Using per-page fault to maintain the uffd semantics, and same
5101          * approach also applies to non-anonymous-shmem faults to avoid
5102          * inflating the RSS of the process.
5103          */
5104         if (!vma_is_anon_shmem(vma) || unlikely(userfaultfd_armed(vma))) {
5105                 nr_pages = 1;
5106         } else if (nr_pages > 1) {
5107                 pgoff_t idx = folio_page_idx(folio, page);
5108                 /* The page offset of vmf->address within the VMA. */
5109                 pgoff_t vma_off = vmf->pgoff - vmf->vma->vm_pgoff;
5110                 /* The index of the entry in the pagetable for fault page. */
5111                 pgoff_t pte_off = pte_index(vmf->address);
5112
5113                 /*
5114                  * Fallback to per-page fault in case the folio size in page
5115                  * cache beyond the VMA limits and PMD pagetable limits.
5116                  */
5117                 if (unlikely(vma_off < idx ||
5118                             vma_off + (nr_pages - idx) > vma_pages(vma) ||
5119                             pte_off < idx ||
5120                             pte_off + (nr_pages - idx)  > PTRS_PER_PTE)) {
5121                         nr_pages = 1;
5122                 } else {
5123                         /* Now we can set mappings for the whole large folio. */
5124                         addr = vmf->address - idx * PAGE_SIZE;
5125                         page = &folio->page;
5126                 }
5127         }
5128
5129         vmf->pte = pte_offset_map_lock(vma->vm_mm, vmf->pmd,
5130                                        addr, &vmf->ptl);
5131         if (!vmf->pte)
5132                 return VM_FAULT_NOPAGE;
5133
5134         /* Re-check under ptl */
5135         if (nr_pages == 1 && unlikely(vmf_pte_changed(vmf))) {
5136                 update_mmu_tlb(vma, addr, vmf->pte);
5137                 ret = VM_FAULT_NOPAGE;
5138                 goto unlock;
5139         } else if (nr_pages > 1 && !pte_range_none(vmf->pte, nr_pages)) {
5140                 update_mmu_tlb_range(vma, addr, vmf->pte, nr_pages);
5141                 ret = VM_FAULT_NOPAGE;
5142                 goto unlock;
5143         }
5144
5145         folio_ref_add(folio, nr_pages - 1);
5146         set_pte_range(vmf, folio, page, nr_pages, addr);
5147         type = is_cow ? MM_ANONPAGES : mm_counter_file(folio);
5148         add_mm_counter(vma->vm_mm, type, nr_pages);
5149         ret = 0;
5150
5151 unlock:
5152         pte_unmap_unlock(vmf->pte, vmf->ptl);
5153         return ret;
5154 }
5155
5156 static unsigned long fault_around_pages __read_mostly =
5157         65536 >> PAGE_SHIFT;
5158
5159 #ifdef CONFIG_DEBUG_FS
5160 static int fault_around_bytes_get(void *data, u64 *val)
5161 {
5162         *val = fault_around_pages << PAGE_SHIFT;
5163         return 0;
5164 }
5165
5166 /*
5167  * fault_around_bytes must be rounded down to the nearest page order as it's
5168  * what do_fault_around() expects to see.
5169  */
5170 static int fault_around_bytes_set(void *data, u64 val)
5171 {
5172         if (val / PAGE_SIZE > PTRS_PER_PTE)
5173                 return -EINVAL;
5174
5175         /*
5176          * The minimum value is 1 page, however this results in no fault-around
5177          * at all. See should_fault_around().
5178          */
5179         val = max(val, PAGE_SIZE);
5180         fault_around_pages = rounddown_pow_of_two(val) >> PAGE_SHIFT;
5181
5182         return 0;
5183 }
5184 DEFINE_DEBUGFS_ATTRIBUTE(fault_around_bytes_fops,
5185                 fault_around_bytes_get, fault_around_bytes_set, "%llu\n");
5186
5187 static int __init fault_around_debugfs(void)
5188 {
5189         debugfs_create_file_unsafe("fault_around_bytes", 0644, NULL, NULL,
5190                                    &fault_around_bytes_fops);
5191         return 0;
5192 }
5193 late_initcall(fault_around_debugfs);
5194 #endif
5195
5196 /*
5197  * do_fault_around() tries to map few pages around the fault address. The hope
5198  * is that the pages will be needed soon and this will lower the number of
5199  * faults to handle.
5200  *
5201  * It uses vm_ops->map_pages() to map the pages, which skips the page if it's
5202  * not ready to be mapped: not up-to-date, locked, etc.
5203  *
5204  * This function doesn't cross VMA or page table boundaries, in order to call
5205  * map_pages() and acquire a PTE lock only once.
5206  *
5207  * fault_around_pages defines how many pages we'll try to map.
5208  * do_fault_around() expects it to be set to a power of two less than or equal
5209  * to PTRS_PER_PTE.
5210  *
5211  * The virtual address of the area that we map is naturally aligned to
5212  * fault_around_pages * PAGE_SIZE rounded down to the machine page size
5213  * (and therefore to page order).  This way it's easier to guarantee
5214  * that we don't cross page table boundaries.
5215  */
5216 static vm_fault_t do_fault_around(struct vm_fault *vmf)
5217 {
5218         pgoff_t nr_pages = READ_ONCE(fault_around_pages);
5219         pgoff_t pte_off = pte_index(vmf->address);
5220         /* The page offset of vmf->address within the VMA. */
5221         pgoff_t vma_off = vmf->pgoff - vmf->vma->vm_pgoff;
5222         pgoff_t from_pte, to_pte;
5223         vm_fault_t ret;
5224
5225         /* The PTE offset of the start address, clamped to the VMA. */
5226         from_pte = max(ALIGN_DOWN(pte_off, nr_pages),
5227                        pte_off - min(pte_off, vma_off));
5228
5229         /* The PTE offset of the end address, clamped to the VMA and PTE. */
5230         to_pte = min3(from_pte + nr_pages, (pgoff_t)PTRS_PER_PTE,
5231                       pte_off + vma_pages(vmf->vma) - vma_off) - 1;
5232
5233         if (pmd_none(*vmf->pmd)) {
5234                 vmf->prealloc_pte = pte_alloc_one(vmf->vma->vm_mm);
5235                 if (!vmf->prealloc_pte)
5236                         return VM_FAULT_OOM;
5237         }
5238
5239         rcu_read_lock();
5240         ret = vmf->vma->vm_ops->map_pages(vmf,
5241                         vmf->pgoff + from_pte - pte_off,
5242                         vmf->pgoff + to_pte - pte_off);
5243         rcu_read_unlock();
5244
5245         return ret;
5246 }
5247
5248 /* Return true if we should do read fault-around, false otherwise */
5249 static inline bool should_fault_around(struct vm_fault *vmf)
5250 {
5251         /* No ->map_pages?  No way to fault around... */
5252         if (!vmf->vma->vm_ops->map_pages)
5253                 return false;
5254
5255         if (uffd_disable_fault_around(vmf->vma))
5256                 return false;
5257
5258         /* A single page implies no faulting 'around' at all. */
5259         return fault_around_pages > 1;
5260 }
5261
5262 static vm_fault_t do_read_fault(struct vm_fault *vmf)
5263 {
5264         vm_fault_t ret = 0;
5265         struct folio *folio;
5266
5267         /*
5268          * Let's call ->map_pages() first and use ->fault() as fallback
5269          * if page by the offset is not ready to be mapped (cold cache or
5270          * something).
5271          */
5272         if (should_fault_around(vmf)) {
5273                 ret = do_fault_around(vmf);
5274                 if (ret)
5275                         return ret;
5276         }
5277
5278         ret = vmf_can_call_fault(vmf);
5279         if (ret)
5280                 return ret;
5281
5282         ret = __do_fault(vmf);
5283         if (unlikely(ret & (VM_FAULT_ERROR | VM_FAULT_NOPAGE | VM_FAULT_RETRY)))
5284                 return ret;
5285
5286         ret |= finish_fault(vmf);
5287         folio = page_folio(vmf->page);
5288         folio_unlock(folio);
5289         if (unlikely(ret & (VM_FAULT_ERROR | VM_FAULT_NOPAGE | VM_FAULT_RETRY)))
5290                 folio_put(folio);
5291         return ret;
5292 }
5293
5294 static vm_fault_t do_cow_fault(struct vm_fault *vmf)
5295 {
5296         struct vm_area_struct *vma = vmf->vma;
5297         struct folio *folio;
5298         vm_fault_t ret;
5299
5300         ret = vmf_can_call_fault(vmf);
5301         if (!ret)
5302                 ret = vmf_anon_prepare(vmf);
5303         if (ret)
5304                 return ret;
5305
5306         folio = folio_prealloc(vma->vm_mm, vma, vmf->address, false);
5307         if (!folio)
5308                 return VM_FAULT_OOM;
5309
5310         vmf->cow_page = &folio->page;
5311
5312         ret = __do_fault(vmf);
5313         if (unlikely(ret & (VM_FAULT_ERROR | VM_FAULT_NOPAGE | VM_FAULT_RETRY)))
5314                 goto uncharge_out;
5315         if (ret & VM_FAULT_DONE_COW)
5316                 return ret;
5317
5318         if (copy_mc_user_highpage(vmf->cow_page, vmf->page, vmf->address, vma)) {
5319                 ret = VM_FAULT_HWPOISON;
5320                 goto unlock;
5321         }
5322         __folio_mark_uptodate(folio);
5323
5324         ret |= finish_fault(vmf);
5325 unlock:
5326         unlock_page(vmf->page);
5327         put_page(vmf->page);
5328         if (unlikely(ret & (VM_FAULT_ERROR | VM_FAULT_NOPAGE | VM_FAULT_RETRY)))
5329                 goto uncharge_out;
5330         return ret;
5331 uncharge_out:
5332         folio_put(folio);
5333         return ret;
5334 }
5335
5336 static vm_fault_t do_shared_fault(struct vm_fault *vmf)
5337 {
5338         struct vm_area_struct *vma = vmf->vma;
5339         vm_fault_t ret, tmp;
5340         struct folio *folio;
5341
5342         ret = vmf_can_call_fault(vmf);
5343         if (ret)
5344                 return ret;
5345
5346         ret = __do_fault(vmf);
5347         if (unlikely(ret & (VM_FAULT_ERROR | VM_FAULT_NOPAGE | VM_FAULT_RETRY)))
5348                 return ret;
5349
5350         folio = page_folio(vmf->page);
5351
5352         /*
5353          * Check if the backing address space wants to know that the page is
5354          * about to become writable
5355          */
5356         if (vma->vm_ops->page_mkwrite) {
5357                 folio_unlock(folio);
5358                 tmp = do_page_mkwrite(vmf, folio);
5359                 if (unlikely(!tmp ||
5360                                 (tmp & (VM_FAULT_ERROR | VM_FAULT_NOPAGE)))) {
5361                         folio_put(folio);
5362                         return tmp;
5363                 }
5364         }
5365
5366         ret |= finish_fault(vmf);
5367         if (unlikely(ret & (VM_FAULT_ERROR | VM_FAULT_NOPAGE |
5368                                         VM_FAULT_RETRY))) {
5369                 folio_unlock(folio);
5370                 folio_put(folio);
5371                 return ret;
5372         }
5373
5374         ret |= fault_dirty_shared_page(vmf);
5375         return ret;
5376 }
5377
5378 /*
5379  * We enter with non-exclusive mmap_lock (to exclude vma changes,
5380  * but allow concurrent faults).
5381  * The mmap_lock may have been released depending on flags and our
5382  * return value.  See filemap_fault() and __folio_lock_or_retry().
5383  * If mmap_lock is released, vma may become invalid (for example
5384  * by other thread calling munmap()).
5385  */
5386 static vm_fault_t do_fault(struct vm_fault *vmf)
5387 {
5388         struct vm_area_struct *vma = vmf->vma;
5389         struct mm_struct *vm_mm = vma->vm_mm;
5390         vm_fault_t ret;
5391
5392         /*
5393          * The VMA was not fully populated on mmap() or missing VM_DONTEXPAND
5394          */
5395         if (!vma->vm_ops->fault) {
5396                 vmf->pte = pte_offset_map_lock(vmf->vma->vm_mm, vmf->pmd,
5397                                                vmf->address, &vmf->ptl);
5398                 if (unlikely(!vmf->pte))
5399                         ret = VM_FAULT_SIGBUS;
5400                 else {
5401                         /*
5402                          * Make sure this is not a temporary clearing of pte
5403                          * by holding ptl and checking again. A R/M/W update
5404                          * of pte involves: take ptl, clearing the pte so that
5405                          * we don't have concurrent modification by hardware
5406                          * followed by an update.
5407                          */
5408                         if (unlikely(pte_none(ptep_get(vmf->pte))))
5409                                 ret = VM_FAULT_SIGBUS;
5410                         else
5411                                 ret = VM_FAULT_NOPAGE;
5412
5413                         pte_unmap_unlock(vmf->pte, vmf->ptl);
5414                 }
5415         } else if (!(vmf->flags & FAULT_FLAG_WRITE))
5416                 ret = do_read_fault(vmf);
5417         else if (!(vma->vm_flags & VM_SHARED))
5418                 ret = do_cow_fault(vmf);
5419         else
5420                 ret = do_shared_fault(vmf);
5421
5422         /* preallocated pagetable is unused: free it */
5423         if (vmf->prealloc_pte) {
5424                 pte_free(vm_mm, vmf->prealloc_pte);
5425                 vmf->prealloc_pte = NULL;
5426         }
5427         return ret;
5428 }
5429
5430 int numa_migrate_check(struct folio *folio, struct vm_fault *vmf,
5431                       unsigned long addr, int *flags,
5432                       bool writable, int *last_cpupid)
5433 {
5434         struct vm_area_struct *vma = vmf->vma;
5435
5436         /*
5437          * Avoid grouping on RO pages in general. RO pages shouldn't hurt as
5438          * much anyway since they can be in shared cache state. This misses
5439          * the case where a mapping is writable but the process never writes
5440          * to it but pte_write gets cleared during protection updates and
5441          * pte_dirty has unpredictable behaviour between PTE scan updates,
5442          * background writeback, dirty balancing and application behaviour.
5443          */
5444         if (!writable)
5445                 *flags |= TNF_NO_GROUP;
5446
5447         /*
5448          * Flag if the folio is shared between multiple address spaces. This
5449          * is later used when determining whether to group tasks together
5450          */
5451         if (folio_likely_mapped_shared(folio) && (vma->vm_flags & VM_SHARED))
5452                 *flags |= TNF_SHARED;
5453         /*
5454          * For memory tiering mode, cpupid of slow memory page is used
5455          * to record page access time.  So use default value.
5456          */
5457         if (folio_use_access_time(folio))
5458                 *last_cpupid = (-1 & LAST_CPUPID_MASK);
5459         else
5460                 *last_cpupid = folio_last_cpupid(folio);
5461
5462         /* Record the current PID acceesing VMA */
5463         vma_set_access_pid_bit(vma);
5464
5465         count_vm_numa_event(NUMA_HINT_FAULTS);
5466 #ifdef CONFIG_NUMA_BALANCING
5467         count_memcg_folio_events(folio, NUMA_HINT_FAULTS, 1);
5468 #endif
5469         if (folio_nid(folio) == numa_node_id()) {
5470                 count_vm_numa_event(NUMA_HINT_FAULTS_LOCAL);
5471                 *flags |= TNF_FAULT_LOCAL;
5472         }
5473
5474         return mpol_misplaced(folio, vmf, addr);
5475 }
5476
5477 static void numa_rebuild_single_mapping(struct vm_fault *vmf, struct vm_area_struct *vma,
5478                                         unsigned long fault_addr, pte_t *fault_pte,
5479                                         bool writable)
5480 {
5481         pte_t pte, old_pte;
5482
5483         old_pte = ptep_modify_prot_start(vma, fault_addr, fault_pte);
5484         pte = pte_modify(old_pte, vma->vm_page_prot);
5485         pte = pte_mkyoung(pte);
5486         if (writable)
5487                 pte = pte_mkwrite(pte, vma);
5488         ptep_modify_prot_commit(vma, fault_addr, fault_pte, old_pte, pte);
5489         update_mmu_cache_range(vmf, vma, fault_addr, fault_pte, 1);
5490 }
5491
5492 static void numa_rebuild_large_mapping(struct vm_fault *vmf, struct vm_area_struct *vma,
5493                                        struct folio *folio, pte_t fault_pte,
5494                                        bool ignore_writable, bool pte_write_upgrade)
5495 {
5496         int nr = pte_pfn(fault_pte) - folio_pfn(folio);
5497         unsigned long start, end, addr = vmf->address;
5498         unsigned long addr_start = addr - (nr << PAGE_SHIFT);
5499         unsigned long pt_start = ALIGN_DOWN(addr, PMD_SIZE);
5500         pte_t *start_ptep;
5501
5502         /* Stay within the VMA and within the page table. */
5503         start = max3(addr_start, pt_start, vma->vm_start);
5504         end = min3(addr_start + folio_size(folio), pt_start + PMD_SIZE,
5505                    vma->vm_end);
5506         start_ptep = vmf->pte - ((addr - start) >> PAGE_SHIFT);
5507
5508         /* Restore all PTEs' mapping of the large folio */
5509         for (addr = start; addr != end; start_ptep++, addr += PAGE_SIZE) {
5510                 pte_t ptent = ptep_get(start_ptep);
5511                 bool writable = false;
5512
5513                 if (!pte_present(ptent) || !pte_protnone(ptent))
5514                         continue;
5515
5516                 if (pfn_folio(pte_pfn(ptent)) != folio)
5517                         continue;
5518
5519                 if (!ignore_writable) {
5520                         ptent = pte_modify(ptent, vma->vm_page_prot);
5521                         writable = pte_write(ptent);
5522                         if (!writable && pte_write_upgrade &&
5523                             can_change_pte_writable(vma, addr, ptent))
5524                                 writable = true;
5525                 }
5526
5527                 numa_rebuild_single_mapping(vmf, vma, addr, start_ptep, writable);
5528         }
5529 }
5530
5531 static vm_fault_t do_numa_page(struct vm_fault *vmf)
5532 {
5533         struct vm_area_struct *vma = vmf->vma;
5534         struct folio *folio = NULL;
5535         int nid = NUMA_NO_NODE;
5536         bool writable = false, ignore_writable = false;
5537         bool pte_write_upgrade = vma_wants_manual_pte_write_upgrade(vma);
5538         int last_cpupid;
5539         int target_nid;
5540         pte_t pte, old_pte;
5541         int flags = 0, nr_pages;
5542
5543         /*
5544          * The pte cannot be used safely until we verify, while holding the page
5545          * table lock, that its contents have not changed during fault handling.
5546          */
5547         spin_lock(vmf->ptl);
5548         /* Read the live PTE from the page tables: */
5549         old_pte = ptep_get(vmf->pte);
5550
5551         if (unlikely(!pte_same(old_pte, vmf->orig_pte))) {
5552                 pte_unmap_unlock(vmf->pte, vmf->ptl);
5553                 return 0;
5554         }
5555
5556         pte = pte_modify(old_pte, vma->vm_page_prot);
5557
5558         /*
5559          * Detect now whether the PTE could be writable; this information
5560          * is only valid while holding the PT lock.
5561          */
5562         writable = pte_write(pte);
5563         if (!writable && pte_write_upgrade &&
5564             can_change_pte_writable(vma, vmf->address, pte))
5565                 writable = true;
5566
5567         folio = vm_normal_folio(vma, vmf->address, pte);
5568         if (!folio || folio_is_zone_device(folio))
5569                 goto out_map;
5570
5571         nid = folio_nid(folio);
5572         nr_pages = folio_nr_pages(folio);
5573
5574         target_nid = numa_migrate_check(folio, vmf, vmf->address, &flags,
5575                                         writable, &last_cpupid);
5576         if (target_nid == NUMA_NO_NODE)
5577                 goto out_map;
5578         if (migrate_misplaced_folio_prepare(folio, vma, target_nid)) {
5579                 flags |= TNF_MIGRATE_FAIL;
5580                 goto out_map;
5581         }
5582         /* The folio is isolated and isolation code holds a folio reference. */
5583         pte_unmap_unlock(vmf->pte, vmf->ptl);
5584         writable = false;
5585         ignore_writable = true;
5586
5587         /* Migrate to the requested node */
5588         if (!migrate_misplaced_folio(folio, vma, target_nid)) {
5589                 nid = target_nid;
5590                 flags |= TNF_MIGRATED;
5591                 task_numa_fault(last_cpupid, nid, nr_pages, flags);
5592                 return 0;
5593         }
5594
5595         flags |= TNF_MIGRATE_FAIL;
5596         vmf->pte = pte_offset_map_lock(vma->vm_mm, vmf->pmd,
5597                                        vmf->address, &vmf->ptl);
5598         if (unlikely(!vmf->pte))
5599                 return 0;
5600         if (unlikely(!pte_same(ptep_get(vmf->pte), vmf->orig_pte))) {
5601                 pte_unmap_unlock(vmf->pte, vmf->ptl);
5602                 return 0;
5603         }
5604 out_map:
5605         /*
5606          * Make it present again, depending on how arch implements
5607          * non-accessible ptes, some can allow access by kernel mode.
5608          */
5609         if (folio && folio_test_large(folio))
5610                 numa_rebuild_large_mapping(vmf, vma, folio, pte, ignore_writable,
5611                                            pte_write_upgrade);
5612         else
5613                 numa_rebuild_single_mapping(vmf, vma, vmf->address, vmf->pte,
5614                                             writable);
5615         pte_unmap_unlock(vmf->pte, vmf->ptl);
5616
5617         if (nid != NUMA_NO_NODE)
5618                 task_numa_fault(last_cpupid, nid, nr_pages, flags);
5619         return 0;
5620 }
5621
5622 static inline vm_fault_t create_huge_pmd(struct vm_fault *vmf)
5623 {
5624         struct vm_area_struct *vma = vmf->vma;
5625         if (vma_is_anonymous(vma))
5626                 return do_huge_pmd_anonymous_page(vmf);
5627         if (vma->vm_ops->huge_fault)
5628                 return vma->vm_ops->huge_fault(vmf, PMD_ORDER);
5629         return VM_FAULT_FALLBACK;
5630 }
5631
5632 /* `inline' is required to avoid gcc 4.1.2 build error */
5633 static inline vm_fault_t wp_huge_pmd(struct vm_fault *vmf)
5634 {
5635         struct vm_area_struct *vma = vmf->vma;
5636         const bool unshare = vmf->flags & FAULT_FLAG_UNSHARE;
5637         vm_fault_t ret;
5638
5639         if (vma_is_anonymous(vma)) {
5640                 if (likely(!unshare) &&
5641                     userfaultfd_huge_pmd_wp(vma, vmf->orig_pmd)) {
5642                         if (userfaultfd_wp_async(vmf->vma))
5643                                 goto split;
5644                         return handle_userfault(vmf, VM_UFFD_WP);
5645                 }
5646                 return do_huge_pmd_wp_page(vmf);
5647         }
5648
5649         if (vma->vm_flags & (VM_SHARED | VM_MAYSHARE)) {
5650                 if (vma->vm_ops->huge_fault) {
5651                         ret = vma->vm_ops->huge_fault(vmf, PMD_ORDER);
5652                         if (!(ret & VM_FAULT_FALLBACK))
5653                                 return ret;
5654                 }
5655         }
5656
5657 split:
5658         /* COW or write-notify handled on pte level: split pmd. */
5659         __split_huge_pmd(vma, vmf->pmd, vmf->address, false, NULL);
5660
5661         return VM_FAULT_FALLBACK;
5662 }
5663
5664 static vm_fault_t create_huge_pud(struct vm_fault *vmf)
5665 {
5666 #if defined(CONFIG_TRANSPARENT_HUGEPAGE) &&                     \
5667         defined(CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD)
5668         struct vm_area_struct *vma = vmf->vma;
5669         /* No support for anonymous transparent PUD pages yet */
5670         if (vma_is_anonymous(vma))
5671                 return VM_FAULT_FALLBACK;
5672         if (vma->vm_ops->huge_fault)
5673                 return vma->vm_ops->huge_fault(vmf, PUD_ORDER);
5674 #endif /* CONFIG_TRANSPARENT_HUGEPAGE */
5675         return VM_FAULT_FALLBACK;
5676 }
5677
5678 static vm_fault_t wp_huge_pud(struct vm_fault *vmf, pud_t orig_pud)
5679 {
5680 #if defined(CONFIG_TRANSPARENT_HUGEPAGE) &&                     \
5681         defined(CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD)
5682         struct vm_area_struct *vma = vmf->vma;
5683         vm_fault_t ret;
5684
5685         /* No support for anonymous transparent PUD pages yet */
5686         if (vma_is_anonymous(vma))
5687                 goto split;
5688         if (vma->vm_flags & (VM_SHARED | VM_MAYSHARE)) {
5689                 if (vma->vm_ops->huge_fault) {
5690                         ret = vma->vm_ops->huge_fault(vmf, PUD_ORDER);
5691                         if (!(ret & VM_FAULT_FALLBACK))
5692                                 return ret;
5693                 }
5694         }
5695 split:
5696         /* COW or write-notify not handled on PUD level: split pud.*/
5697         __split_huge_pud(vma, vmf->pud, vmf->address);
5698 #endif /* CONFIG_TRANSPARENT_HUGEPAGE && CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD */
5699         return VM_FAULT_FALLBACK;
5700 }
5701
5702 /*
5703  * These routines also need to handle stuff like marking pages dirty
5704  * and/or accessed for architectures that don't do it in hardware (most
5705  * RISC architectures).  The early dirtying is also good on the i386.
5706  *
5707  * There is also a hook called "update_mmu_cache()" that architectures
5708  * with external mmu caches can use to update those (ie the Sparc or
5709  * PowerPC hashed page tables that act as extended TLBs).
5710  *
5711  * We enter with non-exclusive mmap_lock (to exclude vma changes, but allow
5712  * concurrent faults).
5713  *
5714  * The mmap_lock may have been released depending on flags and our return value.
5715  * See filemap_fault() and __folio_lock_or_retry().
5716  */
5717 static vm_fault_t handle_pte_fault(struct vm_fault *vmf)
5718 {
5719         pte_t entry;
5720
5721         if (unlikely(pmd_none(*vmf->pmd))) {
5722                 /*
5723                  * Leave __pte_alloc() until later: because vm_ops->fault may
5724                  * want to allocate huge page, and if we expose page table
5725                  * for an instant, it will be difficult to retract from
5726                  * concurrent faults and from rmap lookups.
5727                  */
5728                 vmf->pte = NULL;
5729                 vmf->flags &= ~FAULT_FLAG_ORIG_PTE_VALID;
5730         } else {
5731                 /*
5732                  * A regular pmd is established and it can't morph into a huge
5733                  * pmd by anon khugepaged, since that takes mmap_lock in write
5734                  * mode; but shmem or file collapse to THP could still morph
5735                  * it into a huge pmd: just retry later if so.
5736                  */
5737                 vmf->pte = pte_offset_map_nolock(vmf->vma->vm_mm, vmf->pmd,
5738                                                  vmf->address, &vmf->ptl);
5739                 if (unlikely(!vmf->pte))
5740                         return 0;
5741                 vmf->orig_pte = ptep_get_lockless(vmf->pte);
5742                 vmf->flags |= FAULT_FLAG_ORIG_PTE_VALID;
5743
5744                 if (pte_none(vmf->orig_pte)) {
5745                         pte_unmap(vmf->pte);
5746                         vmf->pte = NULL;
5747                 }
5748         }
5749
5750         if (!vmf->pte)
5751                 return do_pte_missing(vmf);
5752
5753         if (!pte_present(vmf->orig_pte))
5754                 return do_swap_page(vmf);
5755
5756         if (pte_protnone(vmf->orig_pte) && vma_is_accessible(vmf->vma))
5757                 return do_numa_page(vmf);
5758
5759         spin_lock(vmf->ptl);
5760         entry = vmf->orig_pte;
5761         if (unlikely(!pte_same(ptep_get(vmf->pte), entry))) {
5762                 update_mmu_tlb(vmf->vma, vmf->address, vmf->pte);
5763                 goto unlock;
5764         }
5765         if (vmf->flags & (FAULT_FLAG_WRITE|FAULT_FLAG_UNSHARE)) {
5766                 if (!pte_write(entry))
5767                         return do_wp_page(vmf);
5768                 else if (likely(vmf->flags & FAULT_FLAG_WRITE))
5769                         entry = pte_mkdirty(entry);
5770         }
5771         entry = pte_mkyoung(entry);
5772         if (ptep_set_access_flags(vmf->vma, vmf->address, vmf->pte, entry,
5773                                 vmf->flags & FAULT_FLAG_WRITE)) {
5774                 update_mmu_cache_range(vmf, vmf->vma, vmf->address,
5775                                 vmf->pte, 1);
5776         } else {
5777                 /* Skip spurious TLB flush for retried page fault */
5778                 if (vmf->flags & FAULT_FLAG_TRIED)
5779                         goto unlock;
5780                 /*
5781                  * This is needed only for protection faults but the arch code
5782                  * is not yet telling us if this is a protection fault or not.
5783                  * This still avoids useless tlb flushes for .text page faults
5784                  * with threads.
5785                  */
5786                 if (vmf->flags & FAULT_FLAG_WRITE)
5787                         flush_tlb_fix_spurious_fault(vmf->vma, vmf->address,
5788                                                      vmf->pte);
5789         }
5790 unlock:
5791         pte_unmap_unlock(vmf->pte, vmf->ptl);
5792         return 0;
5793 }
5794
5795 /*
5796  * On entry, we hold either the VMA lock or the mmap_lock
5797  * (FAULT_FLAG_VMA_LOCK tells you which).  If VM_FAULT_RETRY is set in
5798  * the result, the mmap_lock is not held on exit.  See filemap_fault()
5799  * and __folio_lock_or_retry().
5800  */
5801 static vm_fault_t __handle_mm_fault(struct vm_area_struct *vma,
5802                 unsigned long address, unsigned int flags)
5803 {
5804         struct vm_fault vmf = {
5805                 .vma = vma,
5806                 .address = address & PAGE_MASK,
5807                 .real_address = address,
5808                 .flags = flags,
5809                 .pgoff = linear_page_index(vma, address),
5810                 .gfp_mask = __get_fault_gfp_mask(vma),
5811         };
5812         struct mm_struct *mm = vma->vm_mm;
5813         unsigned long vm_flags = vma->vm_flags;
5814         pgd_t *pgd;
5815         p4d_t *p4d;
5816         vm_fault_t ret;
5817
5818         pgd = pgd_offset(mm, address);
5819         p4d = p4d_alloc(mm, pgd, address);
5820         if (!p4d)
5821                 return VM_FAULT_OOM;
5822
5823         vmf.pud = pud_alloc(mm, p4d, address);
5824         if (!vmf.pud)
5825                 return VM_FAULT_OOM;
5826 retry_pud:
5827         if (pud_none(*vmf.pud) &&
5828             thp_vma_allowable_order(vma, vm_flags,
5829                                 TVA_IN_PF | TVA_ENFORCE_SYSFS, PUD_ORDER)) {
5830                 ret = create_huge_pud(&vmf);
5831                 if (!(ret & VM_FAULT_FALLBACK))
5832                         return ret;
5833         } else {
5834                 pud_t orig_pud = *vmf.pud;
5835
5836                 barrier();
5837                 if (pud_trans_huge(orig_pud) || pud_devmap(orig_pud)) {
5838
5839                         /*
5840                          * TODO once we support anonymous PUDs: NUMA case and
5841                          * FAULT_FLAG_UNSHARE handling.
5842                          */
5843                         if ((flags & FAULT_FLAG_WRITE) && !pud_write(orig_pud)) {
5844                                 ret = wp_huge_pud(&vmf, orig_pud);
5845                                 if (!(ret & VM_FAULT_FALLBACK))
5846                                         return ret;
5847                         } else {
5848                                 huge_pud_set_accessed(&vmf, orig_pud);
5849                                 return 0;
5850                         }
5851                 }
5852         }
5853
5854         vmf.pmd = pmd_alloc(mm, vmf.pud, address);
5855         if (!vmf.pmd)
5856                 return VM_FAULT_OOM;
5857
5858         /* Huge pud page fault raced with pmd_alloc? */
5859         if (pud_trans_unstable(vmf.pud))
5860                 goto retry_pud;
5861
5862         if (pmd_none(*vmf.pmd) &&
5863             thp_vma_allowable_order(vma, vm_flags,
5864                                 TVA_IN_PF | TVA_ENFORCE_SYSFS, PMD_ORDER)) {
5865                 ret = create_huge_pmd(&vmf);
5866                 if (!(ret & VM_FAULT_FALLBACK))
5867                         return ret;
5868         } else {
5869                 vmf.orig_pmd = pmdp_get_lockless(vmf.pmd);
5870
5871                 if (unlikely(is_swap_pmd(vmf.orig_pmd))) {
5872                         VM_BUG_ON(thp_migration_supported() &&
5873                                           !is_pmd_migration_entry(vmf.orig_pmd));
5874                         if (is_pmd_migration_entry(vmf.orig_pmd))
5875                                 pmd_migration_entry_wait(mm, vmf.pmd);
5876                         return 0;
5877                 }
5878                 if (pmd_trans_huge(vmf.orig_pmd) || pmd_devmap(vmf.orig_pmd)) {
5879                         if (pmd_protnone(vmf.orig_pmd) && vma_is_accessible(vma))
5880                                 return do_huge_pmd_numa_page(&vmf);
5881
5882                         if ((flags & (FAULT_FLAG_WRITE|FAULT_FLAG_UNSHARE)) &&
5883                             !pmd_write(vmf.orig_pmd)) {
5884                                 ret = wp_huge_pmd(&vmf);
5885                                 if (!(ret & VM_FAULT_FALLBACK))
5886                                         return ret;
5887                         } else {
5888                                 huge_pmd_set_accessed(&vmf);
5889                                 return 0;
5890                         }
5891                 }
5892         }
5893
5894         return handle_pte_fault(&vmf);
5895 }
5896
5897 /**
5898  * mm_account_fault - Do page fault accounting
5899  * @mm: mm from which memcg should be extracted. It can be NULL.
5900  * @regs: the pt_regs struct pointer.  When set to NULL, will skip accounting
5901  *        of perf event counters, but we'll still do the per-task accounting to
5902  *        the task who triggered this page fault.
5903  * @address: the faulted address.
5904  * @flags: the fault flags.
5905  * @ret: the fault retcode.
5906  *
5907  * This will take care of most of the page fault accounting.  Meanwhile, it
5908  * will also include the PERF_COUNT_SW_PAGE_FAULTS_[MAJ|MIN] perf counter
5909  * updates.  However, note that the handling of PERF_COUNT_SW_PAGE_FAULTS should
5910  * still be in per-arch page fault handlers at the entry of page fault.
5911  */
5912 static inline void mm_account_fault(struct mm_struct *mm, struct pt_regs *regs,
5913                                     unsigned long address, unsigned int flags,
5914                                     vm_fault_t ret)
5915 {
5916         bool major;
5917
5918         /* Incomplete faults will be accounted upon completion. */
5919         if (ret & VM_FAULT_RETRY)
5920                 return;
5921
5922         /*
5923          * To preserve the behavior of older kernels, PGFAULT counters record
5924          * both successful and failed faults, as opposed to perf counters,
5925          * which ignore failed cases.
5926          */
5927         count_vm_event(PGFAULT);
5928         count_memcg_event_mm(mm, PGFAULT);
5929
5930         /*
5931          * Do not account for unsuccessful faults (e.g. when the address wasn't
5932          * valid).  That includes arch_vma_access_permitted() failing before
5933          * reaching here. So this is not a "this many hardware page faults"
5934          * counter.  We should use the hw profiling for that.
5935          */
5936         if (ret & VM_FAULT_ERROR)
5937                 return;
5938
5939         /*
5940          * We define the fault as a major fault when the final successful fault
5941          * is VM_FAULT_MAJOR, or if it retried (which implies that we couldn't
5942          * handle it immediately previously).
5943          */
5944         major = (ret & VM_FAULT_MAJOR) || (flags & FAULT_FLAG_TRIED);
5945
5946         if (major)
5947                 current->maj_flt++;
5948         else
5949                 current->min_flt++;
5950
5951         /*
5952          * If the fault is done for GUP, regs will be NULL.  We only do the
5953          * accounting for the per thread fault counters who triggered the
5954          * fault, and we skip the perf event updates.
5955          */
5956         if (!regs)
5957                 return;
5958
5959         if (major)
5960                 perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS_MAJ, 1, regs, address);
5961         else
5962                 perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS_MIN, 1, regs, address);
5963 }
5964
5965 #ifdef CONFIG_LRU_GEN
5966 static void lru_gen_enter_fault(struct vm_area_struct *vma)
5967 {
5968         /* the LRU algorithm only applies to accesses with recency */
5969         current->in_lru_fault = vma_has_recency(vma);
5970 }
5971
5972 static void lru_gen_exit_fault(void)
5973 {
5974         current->in_lru_fault = false;
5975 }
5976 #else
5977 static void lru_gen_enter_fault(struct vm_area_struct *vma)
5978 {
5979 }
5980
5981 static void lru_gen_exit_fault(void)
5982 {
5983 }
5984 #endif /* CONFIG_LRU_GEN */
5985
5986 static vm_fault_t sanitize_fault_flags(struct vm_area_struct *vma,
5987                                        unsigned int *flags)
5988 {
5989         if (unlikely(*flags & FAULT_FLAG_UNSHARE)) {
5990                 if (WARN_ON_ONCE(*flags & FAULT_FLAG_WRITE))
5991                         return VM_FAULT_SIGSEGV;
5992                 /*
5993                  * FAULT_FLAG_UNSHARE only applies to COW mappings. Let's
5994                  * just treat it like an ordinary read-fault otherwise.
5995                  */
5996                 if (!is_cow_mapping(vma->vm_flags))
5997                         *flags &= ~FAULT_FLAG_UNSHARE;
5998         } else if (*flags & FAULT_FLAG_WRITE) {
5999                 /* Write faults on read-only mappings are impossible ... */
6000                 if (WARN_ON_ONCE(!(vma->vm_flags & VM_MAYWRITE)))
6001                         return VM_FAULT_SIGSEGV;
6002                 /* ... and FOLL_FORCE only applies to COW mappings. */
6003                 if (WARN_ON_ONCE(!(vma->vm_flags & VM_WRITE) &&
6004                                  !is_cow_mapping(vma->vm_flags)))
6005                         return VM_FAULT_SIGSEGV;
6006         }
6007 #ifdef CONFIG_PER_VMA_LOCK
6008         /*
6009          * Per-VMA locks can't be used with FAULT_FLAG_RETRY_NOWAIT because of
6010          * the assumption that lock is dropped on VM_FAULT_RETRY.
6011          */
6012         if (WARN_ON_ONCE((*flags &
6013                         (FAULT_FLAG_VMA_LOCK | FAULT_FLAG_RETRY_NOWAIT)) ==
6014                         (FAULT_FLAG_VMA_LOCK | FAULT_FLAG_RETRY_NOWAIT)))
6015                 return VM_FAULT_SIGSEGV;
6016 #endif
6017
6018         return 0;
6019 }
6020
6021 /*
6022  * By the time we get here, we already hold the mm semaphore
6023  *
6024  * The mmap_lock may have been released depending on flags and our
6025  * return value.  See filemap_fault() and __folio_lock_or_retry().
6026  */
6027 vm_fault_t handle_mm_fault(struct vm_area_struct *vma, unsigned long address,
6028                            unsigned int flags, struct pt_regs *regs)
6029 {
6030         /* If the fault handler drops the mmap_lock, vma may be freed */
6031         struct mm_struct *mm = vma->vm_mm;
6032         vm_fault_t ret;
6033         bool is_droppable;
6034
6035         __set_current_state(TASK_RUNNING);
6036
6037         ret = sanitize_fault_flags(vma, &flags);
6038         if (ret)
6039                 goto out;
6040
6041         if (!arch_vma_access_permitted(vma, flags & FAULT_FLAG_WRITE,
6042                                             flags & FAULT_FLAG_INSTRUCTION,
6043                                             flags & FAULT_FLAG_REMOTE)) {
6044                 ret = VM_FAULT_SIGSEGV;
6045                 goto out;
6046         }
6047
6048         is_droppable = !!(vma->vm_flags & VM_DROPPABLE);
6049
6050         /*
6051          * Enable the memcg OOM handling for faults triggered in user
6052          * space.  Kernel faults are handled more gracefully.
6053          */
6054         if (flags & FAULT_FLAG_USER)
6055                 mem_cgroup_enter_user_fault();
6056
6057         lru_gen_enter_fault(vma);
6058
6059         if (unlikely(is_vm_hugetlb_page(vma)))
6060                 ret = hugetlb_fault(vma->vm_mm, vma, address, flags);
6061         else
6062                 ret = __handle_mm_fault(vma, address, flags);
6063
6064         /*
6065          * Warning: It is no longer safe to dereference vma-> after this point,
6066          * because mmap_lock might have been dropped by __handle_mm_fault(), so
6067          * vma might be destroyed from underneath us.
6068          */
6069
6070         lru_gen_exit_fault();
6071
6072         /* If the mapping is droppable, then errors due to OOM aren't fatal. */
6073         if (is_droppable)
6074                 ret &= ~VM_FAULT_OOM;
6075
6076         if (flags & FAULT_FLAG_USER) {
6077                 mem_cgroup_exit_user_fault();
6078                 /*
6079                  * The task may have entered a memcg OOM situation but
6080                  * if the allocation error was handled gracefully (no
6081                  * VM_FAULT_OOM), there is no need to kill anything.
6082                  * Just clean up the OOM state peacefully.
6083                  */
6084                 if (task_in_memcg_oom(current) && !(ret & VM_FAULT_OOM))
6085                         mem_cgroup_oom_synchronize(false);
6086         }
6087 out:
6088         mm_account_fault(mm, regs, address, flags, ret);
6089
6090         return ret;
6091 }
6092 EXPORT_SYMBOL_GPL(handle_mm_fault);
6093
6094 #ifdef CONFIG_LOCK_MM_AND_FIND_VMA
6095 #include <linux/extable.h>
6096
6097 static inline bool get_mmap_lock_carefully(struct mm_struct *mm, struct pt_regs *regs)
6098 {
6099         if (likely(mmap_read_trylock(mm)))
6100                 return true;
6101
6102         if (regs && !user_mode(regs)) {
6103                 unsigned long ip = exception_ip(regs);
6104                 if (!search_exception_tables(ip))
6105                         return false;
6106         }
6107
6108         return !mmap_read_lock_killable(mm);
6109 }
6110
6111 static inline bool mmap_upgrade_trylock(struct mm_struct *mm)
6112 {
6113         /*
6114          * We don't have this operation yet.
6115          *
6116          * It should be easy enough to do: it's basically a
6117          *    atomic_long_try_cmpxchg_acquire()
6118          * from RWSEM_READER_BIAS -> RWSEM_WRITER_LOCKED, but
6119          * it also needs the proper lockdep magic etc.
6120          */
6121         return false;
6122 }
6123
6124 static inline bool upgrade_mmap_lock_carefully(struct mm_struct *mm, struct pt_regs *regs)
6125 {
6126         mmap_read_unlock(mm);
6127         if (regs && !user_mode(regs)) {
6128                 unsigned long ip = exception_ip(regs);
6129                 if (!search_exception_tables(ip))
6130                         return false;
6131         }
6132         return !mmap_write_lock_killable(mm);
6133 }
6134
6135 /*
6136  * Helper for page fault handling.
6137  *
6138  * This is kind of equivalend to "mmap_read_lock()" followed
6139  * by "find_extend_vma()", except it's a lot more careful about
6140  * the locking (and will drop the lock on failure).
6141  *
6142  * For example, if we have a kernel bug that causes a page
6143  * fault, we don't want to just use mmap_read_lock() to get
6144  * the mm lock, because that would deadlock if the bug were
6145  * to happen while we're holding the mm lock for writing.
6146  *
6147  * So this checks the exception tables on kernel faults in
6148  * order to only do this all for instructions that are actually
6149  * expected to fault.
6150  *
6151  * We can also actually take the mm lock for writing if we
6152  * need to extend the vma, which helps the VM layer a lot.
6153  */
6154 struct vm_area_struct *lock_mm_and_find_vma(struct mm_struct *mm,
6155                         unsigned long addr, struct pt_regs *regs)
6156 {
6157         struct vm_area_struct *vma;
6158
6159         if (!get_mmap_lock_carefully(mm, regs))
6160                 return NULL;
6161
6162         vma = find_vma(mm, addr);
6163         if (likely(vma && (vma->vm_start <= addr)))
6164                 return vma;
6165
6166         /*
6167          * Well, dang. We might still be successful, but only
6168          * if we can extend a vma to do so.
6169          */
6170         if (!vma || !(vma->vm_flags & VM_GROWSDOWN)) {
6171                 mmap_read_unlock(mm);
6172                 return NULL;
6173         }
6174
6175         /*
6176          * We can try to upgrade the mmap lock atomically,
6177          * in which case we can continue to use the vma
6178          * we already looked up.
6179          *
6180          * Otherwise we'll have to drop the mmap lock and
6181          * re-take it, and also look up the vma again,
6182          * re-checking it.
6183          */
6184         if (!mmap_upgrade_trylock(mm)) {
6185                 if (!upgrade_mmap_lock_carefully(mm, regs))
6186                         return NULL;
6187
6188                 vma = find_vma(mm, addr);
6189                 if (!vma)
6190                         goto fail;
6191                 if (vma->vm_start <= addr)
6192                         goto success;
6193                 if (!(vma->vm_flags & VM_GROWSDOWN))
6194                         goto fail;
6195         }
6196
6197         if (expand_stack_locked(vma, addr))
6198                 goto fail;
6199
6200 success:
6201         mmap_write_downgrade(mm);
6202         return vma;
6203
6204 fail:
6205         mmap_write_unlock(mm);
6206         return NULL;
6207 }
6208 #endif
6209
6210 #ifdef CONFIG_PER_VMA_LOCK
6211 /*
6212  * Lookup and lock a VMA under RCU protection. Returned VMA is guaranteed to be
6213  * stable and not isolated. If the VMA is not found or is being modified the
6214  * function returns NULL.
6215  */
6216 struct vm_area_struct *lock_vma_under_rcu(struct mm_struct *mm,
6217                                           unsigned long address)
6218 {
6219         MA_STATE(mas, &mm->mm_mt, address, address);
6220         struct vm_area_struct *vma;
6221
6222         rcu_read_lock();
6223 retry:
6224         vma = mas_walk(&mas);
6225         if (!vma)
6226                 goto inval;
6227
6228         if (!vma_start_read(vma))
6229                 goto inval;
6230
6231         /* Check if the VMA got isolated after we found it */
6232         if (vma->detached) {
6233                 vma_end_read(vma);
6234                 count_vm_vma_lock_event(VMA_LOCK_MISS);
6235                 /* The area was replaced with another one */
6236                 goto retry;
6237         }
6238         /*
6239          * At this point, we have a stable reference to a VMA: The VMA is
6240          * locked and we know it hasn't already been isolated.
6241          * From here on, we can access the VMA without worrying about which
6242          * fields are accessible for RCU readers.
6243          */
6244
6245         /* Check since vm_start/vm_end might change before we lock the VMA */
6246         if (unlikely(address < vma->vm_start || address >= vma->vm_end))
6247                 goto inval_end_read;
6248
6249         rcu_read_unlock();
6250         return vma;
6251
6252 inval_end_read:
6253         vma_end_read(vma);
6254 inval:
6255         rcu_read_unlock();
6256         count_vm_vma_lock_event(VMA_LOCK_ABORT);
6257         return NULL;
6258 }
6259 #endif /* CONFIG_PER_VMA_LOCK */
6260
6261 #ifndef __PAGETABLE_P4D_FOLDED
6262 /*
6263  * Allocate p4d page table.
6264  * We've already handled the fast-path in-line.
6265  */
6266 int __p4d_alloc(struct mm_struct *mm, pgd_t *pgd, unsigned long address)
6267 {
6268         p4d_t *new = p4d_alloc_one(mm, address);
6269         if (!new)
6270                 return -ENOMEM;
6271
6272         spin_lock(&mm->page_table_lock);
6273         if (pgd_present(*pgd)) {        /* Another has populated it */
6274                 p4d_free(mm, new);
6275         } else {
6276                 smp_wmb(); /* See comment in pmd_install() */
6277                 pgd_populate(mm, pgd, new);
6278         }
6279         spin_unlock(&mm->page_table_lock);
6280         return 0;
6281 }
6282 #endif /* __PAGETABLE_P4D_FOLDED */
6283
6284 #ifndef __PAGETABLE_PUD_FOLDED
6285 /*
6286  * Allocate page upper directory.
6287  * We've already handled the fast-path in-line.
6288  */
6289 int __pud_alloc(struct mm_struct *mm, p4d_t *p4d, unsigned long address)
6290 {
6291         pud_t *new = pud_alloc_one(mm, address);
6292         if (!new)
6293                 return -ENOMEM;
6294
6295         spin_lock(&mm->page_table_lock);
6296         if (!p4d_present(*p4d)) {
6297                 mm_inc_nr_puds(mm);
6298                 smp_wmb(); /* See comment in pmd_install() */
6299                 p4d_populate(mm, p4d, new);
6300         } else  /* Another has populated it */
6301                 pud_free(mm, new);
6302         spin_unlock(&mm->page_table_lock);
6303         return 0;
6304 }
6305 #endif /* __PAGETABLE_PUD_FOLDED */
6306
6307 #ifndef __PAGETABLE_PMD_FOLDED
6308 /*
6309  * Allocate page middle directory.
6310  * We've already handled the fast-path in-line.
6311  */
6312 int __pmd_alloc(struct mm_struct *mm, pud_t *pud, unsigned long address)
6313 {
6314         spinlock_t *ptl;
6315         pmd_t *new = pmd_alloc_one(mm, address);
6316         if (!new)
6317                 return -ENOMEM;
6318
6319         ptl = pud_lock(mm, pud);
6320         if (!pud_present(*pud)) {
6321                 mm_inc_nr_pmds(mm);
6322                 smp_wmb(); /* See comment in pmd_install() */
6323                 pud_populate(mm, pud, new);
6324         } else {        /* Another has populated it */
6325                 pmd_free(mm, new);
6326         }
6327         spin_unlock(ptl);
6328         return 0;
6329 }
6330 #endif /* __PAGETABLE_PMD_FOLDED */
6331
6332 static inline void pfnmap_args_setup(struct follow_pfnmap_args *args,
6333                                      spinlock_t *lock, pte_t *ptep,
6334                                      pgprot_t pgprot, unsigned long pfn_base,
6335                                      unsigned long addr_mask, bool writable,
6336                                      bool special)
6337 {
6338         args->lock = lock;
6339         args->ptep = ptep;
6340         args->pfn = pfn_base + ((args->address & ~addr_mask) >> PAGE_SHIFT);
6341         args->pgprot = pgprot;
6342         args->writable = writable;
6343         args->special = special;
6344 }
6345
6346 static inline void pfnmap_lockdep_assert(struct vm_area_struct *vma)
6347 {
6348 #ifdef CONFIG_LOCKDEP
6349         struct address_space *mapping = vma->vm_file->f_mapping;
6350
6351         if (mapping)
6352                 lockdep_assert(lockdep_is_held(&vma->vm_file->f_mapping->i_mmap_rwsem) ||
6353                                lockdep_is_held(&vma->vm_mm->mmap_lock));
6354         else
6355                 lockdep_assert(lockdep_is_held(&vma->vm_mm->mmap_lock));
6356 #endif
6357 }
6358
6359 /**
6360  * follow_pfnmap_start() - Look up a pfn mapping at a user virtual address
6361  * @args: Pointer to struct @follow_pfnmap_args
6362  *
6363  * The caller needs to setup args->vma and args->address to point to the
6364  * virtual address as the target of such lookup.  On a successful return,
6365  * the results will be put into other output fields.
6366  *
6367  * After the caller finished using the fields, the caller must invoke
6368  * another follow_pfnmap_end() to proper releases the locks and resources
6369  * of such look up request.
6370  *
6371  * During the start() and end() calls, the results in @args will be valid
6372  * as proper locks will be held.  After the end() is called, all the fields
6373  * in @follow_pfnmap_args will be invalid to be further accessed.  Further
6374  * use of such information after end() may require proper synchronizations
6375  * by the caller with page table updates, otherwise it can create a
6376  * security bug.
6377  *
6378  * If the PTE maps a refcounted page, callers are responsible to protect
6379  * against invalidation with MMU notifiers; otherwise access to the PFN at
6380  * a later point in time can trigger use-after-free.
6381  *
6382  * Only IO mappings and raw PFN mappings are allowed.  The mmap semaphore
6383  * should be taken for read, and the mmap semaphore cannot be released
6384  * before the end() is invoked.
6385  *
6386  * This function must not be used to modify PTE content.
6387  *
6388  * Return: zero on success, negative otherwise.
6389  */
6390 int follow_pfnmap_start(struct follow_pfnmap_args *args)
6391 {
6392         struct vm_area_struct *vma = args->vma;
6393         unsigned long address = args->address;
6394         struct mm_struct *mm = vma->vm_mm;
6395         spinlock_t *lock;
6396         pgd_t *pgdp;
6397         p4d_t *p4dp, p4d;
6398         pud_t *pudp, pud;
6399         pmd_t *pmdp, pmd;
6400         pte_t *ptep, pte;
6401
6402         pfnmap_lockdep_assert(vma);
6403
6404         if (unlikely(address < vma->vm_start || address >= vma->vm_end))
6405                 goto out;
6406
6407         if (!(vma->vm_flags & (VM_IO | VM_PFNMAP)))
6408                 goto out;
6409 retry:
6410         pgdp = pgd_offset(mm, address);
6411         if (pgd_none(*pgdp) || unlikely(pgd_bad(*pgdp)))
6412                 goto out;
6413
6414         p4dp = p4d_offset(pgdp, address);
6415         p4d = READ_ONCE(*p4dp);
6416         if (p4d_none(p4d) || unlikely(p4d_bad(p4d)))
6417                 goto out;
6418
6419         pudp = pud_offset(p4dp, address);
6420         pud = READ_ONCE(*pudp);
6421         if (pud_none(pud))
6422                 goto out;
6423         if (pud_leaf(pud)) {
6424                 lock = pud_lock(mm, pudp);
6425                 if (!unlikely(pud_leaf(pud))) {
6426                         spin_unlock(lock);
6427                         goto retry;
6428                 }
6429                 pfnmap_args_setup(args, lock, NULL, pud_pgprot(pud),
6430                                   pud_pfn(pud), PUD_MASK, pud_write(pud),
6431                                   pud_special(pud));
6432                 return 0;
6433         }
6434
6435         pmdp = pmd_offset(pudp, address);
6436         pmd = pmdp_get_lockless(pmdp);
6437         if (pmd_leaf(pmd)) {
6438                 lock = pmd_lock(mm, pmdp);
6439                 if (!unlikely(pmd_leaf(pmd))) {
6440                         spin_unlock(lock);
6441                         goto retry;
6442                 }
6443                 pfnmap_args_setup(args, lock, NULL, pmd_pgprot(pmd),
6444                                   pmd_pfn(pmd), PMD_MASK, pmd_write(pmd),
6445                                   pmd_special(pmd));
6446                 return 0;
6447         }
6448
6449         ptep = pte_offset_map_lock(mm, pmdp, address, &lock);
6450         if (!ptep)
6451                 goto out;
6452         pte = ptep_get(ptep);
6453         if (!pte_present(pte))
6454                 goto unlock;
6455         pfnmap_args_setup(args, lock, ptep, pte_pgprot(pte),
6456                           pte_pfn(pte), PAGE_MASK, pte_write(pte),
6457                           pte_special(pte));
6458         return 0;
6459 unlock:
6460         pte_unmap_unlock(ptep, lock);
6461 out:
6462         return -EINVAL;
6463 }
6464 EXPORT_SYMBOL_GPL(follow_pfnmap_start);
6465
6466 /**
6467  * follow_pfnmap_end(): End a follow_pfnmap_start() process
6468  * @args: Pointer to struct @follow_pfnmap_args
6469  *
6470  * Must be used in pair of follow_pfnmap_start().  See the start() function
6471  * above for more information.
6472  */
6473 void follow_pfnmap_end(struct follow_pfnmap_args *args)
6474 {
6475         if (args->lock)
6476                 spin_unlock(args->lock);
6477         if (args->ptep)
6478                 pte_unmap(args->ptep);
6479 }
6480 EXPORT_SYMBOL_GPL(follow_pfnmap_end);
6481
6482 #ifdef CONFIG_HAVE_IOREMAP_PROT
6483 /**
6484  * generic_access_phys - generic implementation for iomem mmap access
6485  * @vma: the vma to access
6486  * @addr: userspace address, not relative offset within @vma
6487  * @buf: buffer to read/write
6488  * @len: length of transfer
6489  * @write: set to FOLL_WRITE when writing, otherwise reading
6490  *
6491  * This is a generic implementation for &vm_operations_struct.access for an
6492  * iomem mapping. This callback is used by access_process_vm() when the @vma is
6493  * not page based.
6494  */
6495 int generic_access_phys(struct vm_area_struct *vma, unsigned long addr,
6496                         void *buf, int len, int write)
6497 {
6498         resource_size_t phys_addr;
6499         unsigned long prot = 0;
6500         void __iomem *maddr;
6501         int offset = offset_in_page(addr);
6502         int ret = -EINVAL;
6503         bool writable;
6504         struct follow_pfnmap_args args = { .vma = vma, .address = addr };
6505
6506 retry:
6507         if (follow_pfnmap_start(&args))
6508                 return -EINVAL;
6509         prot = pgprot_val(args.pgprot);
6510         phys_addr = (resource_size_t)args.pfn << PAGE_SHIFT;
6511         writable = args.writable;
6512         follow_pfnmap_end(&args);
6513
6514         if ((write & FOLL_WRITE) && !writable)
6515                 return -EINVAL;
6516
6517         maddr = ioremap_prot(phys_addr, PAGE_ALIGN(len + offset), prot);
6518         if (!maddr)
6519                 return -ENOMEM;
6520
6521         if (follow_pfnmap_start(&args))
6522                 goto out_unmap;
6523
6524         if ((prot != pgprot_val(args.pgprot)) ||
6525             (phys_addr != (args.pfn << PAGE_SHIFT)) ||
6526             (writable != args.writable)) {
6527                 follow_pfnmap_end(&args);
6528                 iounmap(maddr);
6529                 goto retry;
6530         }
6531
6532         if (write)
6533                 memcpy_toio(maddr + offset, buf, len);
6534         else
6535                 memcpy_fromio(buf, maddr + offset, len);
6536         ret = len;
6537         follow_pfnmap_end(&args);
6538 out_unmap:
6539         iounmap(maddr);
6540
6541         return ret;
6542 }
6543 EXPORT_SYMBOL_GPL(generic_access_phys);
6544 #endif
6545
6546 /*
6547  * Access another process' address space as given in mm.
6548  */
6549 static int __access_remote_vm(struct mm_struct *mm, unsigned long addr,
6550                               void *buf, int len, unsigned int gup_flags)
6551 {
6552         void *old_buf = buf;
6553         int write = gup_flags & FOLL_WRITE;
6554
6555         if (mmap_read_lock_killable(mm))
6556                 return 0;
6557
6558         /* Untag the address before looking up the VMA */
6559         addr = untagged_addr_remote(mm, addr);
6560
6561         /* Avoid triggering the temporary warning in __get_user_pages */
6562         if (!vma_lookup(mm, addr) && !expand_stack(mm, addr))
6563                 return 0;
6564
6565         /* ignore errors, just check how much was successfully transferred */
6566         while (len) {
6567                 int bytes, offset;
6568                 void *maddr;
6569                 struct vm_area_struct *vma = NULL;
6570                 struct page *page = get_user_page_vma_remote(mm, addr,
6571                                                              gup_flags, &vma);
6572
6573                 if (IS_ERR(page)) {
6574                         /* We might need to expand the stack to access it */
6575                         vma = vma_lookup(mm, addr);
6576                         if (!vma) {
6577                                 vma = expand_stack(mm, addr);
6578
6579                                 /* mmap_lock was dropped on failure */
6580                                 if (!vma)
6581                                         return buf - old_buf;
6582
6583                                 /* Try again if stack expansion worked */
6584                                 continue;
6585                         }
6586
6587                         /*
6588                          * Check if this is a VM_IO | VM_PFNMAP VMA, which
6589                          * we can access using slightly different code.
6590                          */
6591                         bytes = 0;
6592 #ifdef CONFIG_HAVE_IOREMAP_PROT
6593                         if (vma->vm_ops && vma->vm_ops->access)
6594                                 bytes = vma->vm_ops->access(vma, addr, buf,
6595                                                             len, write);
6596 #endif
6597                         if (bytes <= 0)
6598                                 break;
6599                 } else {
6600                         bytes = len;
6601                         offset = addr & (PAGE_SIZE-1);
6602                         if (bytes > PAGE_SIZE-offset)
6603                                 bytes = PAGE_SIZE-offset;
6604
6605                         maddr = kmap_local_page(page);
6606                         if (write) {
6607                                 copy_to_user_page(vma, page, addr,
6608                                                   maddr + offset, buf, bytes);
6609                                 set_page_dirty_lock(page);
6610                         } else {
6611                                 copy_from_user_page(vma, page, addr,
6612                                                     buf, maddr + offset, bytes);
6613                         }
6614                         unmap_and_put_page(page, maddr);
6615                 }
6616                 len -= bytes;
6617                 buf += bytes;
6618                 addr += bytes;
6619         }
6620         mmap_read_unlock(mm);
6621
6622         return buf - old_buf;
6623 }
6624
6625 /**
6626  * access_remote_vm - access another process' address space
6627  * @mm:         the mm_struct of the target address space
6628  * @addr:       start address to access
6629  * @buf:        source or destination buffer
6630  * @len:        number of bytes to transfer
6631  * @gup_flags:  flags modifying lookup behaviour
6632  *
6633  * The caller must hold a reference on @mm.
6634  *
6635  * Return: number of bytes copied from source to destination.
6636  */
6637 int access_remote_vm(struct mm_struct *mm, unsigned long addr,
6638                 void *buf, int len, unsigned int gup_flags)
6639 {
6640         return __access_remote_vm(mm, addr, buf, len, gup_flags);
6641 }
6642
6643 /*
6644  * Access another process' address space.
6645  * Source/target buffer must be kernel space,
6646  * Do not walk the page table directly, use get_user_pages
6647  */
6648 int access_process_vm(struct task_struct *tsk, unsigned long addr,
6649                 void *buf, int len, unsigned int gup_flags)
6650 {
6651         struct mm_struct *mm;
6652         int ret;
6653
6654         mm = get_task_mm(tsk);
6655         if (!mm)
6656                 return 0;
6657
6658         ret = __access_remote_vm(mm, addr, buf, len, gup_flags);
6659
6660         mmput(mm);
6661
6662         return ret;
6663 }
6664 EXPORT_SYMBOL_GPL(access_process_vm);
6665
6666 /*
6667  * Print the name of a VMA.
6668  */
6669 void print_vma_addr(char *prefix, unsigned long ip)
6670 {
6671         struct mm_struct *mm = current->mm;
6672         struct vm_area_struct *vma;
6673
6674         /*
6675          * we might be running from an atomic context so we cannot sleep
6676          */
6677         if (!mmap_read_trylock(mm))
6678                 return;
6679
6680         vma = vma_lookup(mm, ip);
6681         if (vma && vma->vm_file) {
6682                 struct file *f = vma->vm_file;
6683                 ip -= vma->vm_start;
6684                 ip += vma->vm_pgoff << PAGE_SHIFT;
6685                 printk("%s%pD[%lx,%lx+%lx]", prefix, f, ip,
6686                                 vma->vm_start,
6687                                 vma->vm_end - vma->vm_start);
6688         }
6689         mmap_read_unlock(mm);
6690 }
6691
6692 #if defined(CONFIG_PROVE_LOCKING) || defined(CONFIG_DEBUG_ATOMIC_SLEEP)
6693 void __might_fault(const char *file, int line)
6694 {
6695         if (pagefault_disabled())
6696                 return;
6697         __might_sleep(file, line);
6698 #if defined(CONFIG_DEBUG_ATOMIC_SLEEP)
6699         if (current->mm)
6700                 might_lock_read(&current->mm->mmap_lock);
6701 #endif
6702 }
6703 EXPORT_SYMBOL(__might_fault);
6704 #endif
6705
6706 #if defined(CONFIG_TRANSPARENT_HUGEPAGE) || defined(CONFIG_HUGETLBFS)
6707 /*
6708  * Process all subpages of the specified huge page with the specified
6709  * operation.  The target subpage will be processed last to keep its
6710  * cache lines hot.
6711  */
6712 static inline int process_huge_page(
6713         unsigned long addr_hint, unsigned int nr_pages,
6714         int (*process_subpage)(unsigned long addr, int idx, void *arg),
6715         void *arg)
6716 {
6717         int i, n, base, l, ret;
6718         unsigned long addr = addr_hint &
6719                 ~(((unsigned long)nr_pages << PAGE_SHIFT) - 1);
6720
6721         /* Process target subpage last to keep its cache lines hot */
6722         might_sleep();
6723         n = (addr_hint - addr) / PAGE_SIZE;
6724         if (2 * n <= nr_pages) {
6725                 /* If target subpage in first half of huge page */
6726                 base = 0;
6727                 l = n;
6728                 /* Process subpages at the end of huge page */
6729                 for (i = nr_pages - 1; i >= 2 * n; i--) {
6730                         cond_resched();
6731                         ret = process_subpage(addr + i * PAGE_SIZE, i, arg);
6732                         if (ret)
6733                                 return ret;
6734                 }
6735         } else {
6736                 /* If target subpage in second half of huge page */
6737                 base = nr_pages - 2 * (nr_pages - n);
6738                 l = nr_pages - n;
6739                 /* Process subpages at the begin of huge page */
6740                 for (i = 0; i < base; i++) {
6741                         cond_resched();
6742                         ret = process_subpage(addr + i * PAGE_SIZE, i, arg);
6743                         if (ret)
6744                                 return ret;
6745                 }
6746         }
6747         /*
6748          * Process remaining subpages in left-right-left-right pattern
6749          * towards the target subpage
6750          */
6751         for (i = 0; i < l; i++) {
6752                 int left_idx = base + i;
6753                 int right_idx = base + 2 * l - 1 - i;
6754
6755                 cond_resched();
6756                 ret = process_subpage(addr + left_idx * PAGE_SIZE, left_idx, arg);
6757                 if (ret)
6758                         return ret;
6759                 cond_resched();
6760                 ret = process_subpage(addr + right_idx * PAGE_SIZE, right_idx, arg);
6761                 if (ret)
6762                         return ret;
6763         }
6764         return 0;
6765 }
6766
6767 static void clear_gigantic_page(struct folio *folio, unsigned long addr,
6768                                 unsigned int nr_pages)
6769 {
6770         int i;
6771
6772         might_sleep();
6773         for (i = 0; i < nr_pages; i++) {
6774                 cond_resched();
6775                 clear_user_highpage(folio_page(folio, i), addr + i * PAGE_SIZE);
6776         }
6777 }
6778
6779 static int clear_subpage(unsigned long addr, int idx, void *arg)
6780 {
6781         struct folio *folio = arg;
6782
6783         clear_user_highpage(folio_page(folio, idx), addr);
6784         return 0;
6785 }
6786
6787 /**
6788  * folio_zero_user - Zero a folio which will be mapped to userspace.
6789  * @folio: The folio to zero.
6790  * @addr_hint: The address will be accessed or the base address if uncelar.
6791  */
6792 void folio_zero_user(struct folio *folio, unsigned long addr_hint)
6793 {
6794         unsigned int nr_pages = folio_nr_pages(folio);
6795
6796         if (unlikely(nr_pages > MAX_ORDER_NR_PAGES))
6797                 clear_gigantic_page(folio, addr_hint, nr_pages);
6798         else
6799                 process_huge_page(addr_hint, nr_pages, clear_subpage, folio);
6800 }
6801
6802 static int copy_user_gigantic_page(struct folio *dst, struct folio *src,
6803                                    unsigned long addr,
6804                                    struct vm_area_struct *vma,
6805                                    unsigned int nr_pages)
6806 {
6807         int i;
6808         struct page *dst_page;
6809         struct page *src_page;
6810
6811         for (i = 0; i < nr_pages; i++) {
6812                 dst_page = folio_page(dst, i);
6813                 src_page = folio_page(src, i);
6814
6815                 cond_resched();
6816                 if (copy_mc_user_highpage(dst_page, src_page,
6817                                           addr + i*PAGE_SIZE, vma))
6818                         return -EHWPOISON;
6819         }
6820         return 0;
6821 }
6822
6823 struct copy_subpage_arg {
6824         struct folio *dst;
6825         struct folio *src;
6826         struct vm_area_struct *vma;
6827 };
6828
6829 static int copy_subpage(unsigned long addr, int idx, void *arg)
6830 {
6831         struct copy_subpage_arg *copy_arg = arg;
6832         struct page *dst = folio_page(copy_arg->dst, idx);
6833         struct page *src = folio_page(copy_arg->src, idx);
6834
6835         if (copy_mc_user_highpage(dst, src, addr, copy_arg->vma))
6836                 return -EHWPOISON;
6837         return 0;
6838 }
6839
6840 int copy_user_large_folio(struct folio *dst, struct folio *src,
6841                           unsigned long addr_hint, struct vm_area_struct *vma)
6842 {
6843         unsigned int nr_pages = folio_nr_pages(dst);
6844         struct copy_subpage_arg arg = {
6845                 .dst = dst,
6846                 .src = src,
6847                 .vma = vma,
6848         };
6849
6850         if (unlikely(nr_pages > MAX_ORDER_NR_PAGES))
6851                 return copy_user_gigantic_page(dst, src, addr_hint, vma, nr_pages);
6852
6853         return process_huge_page(addr_hint, nr_pages, copy_subpage, &arg);
6854 }
6855
6856 long copy_folio_from_user(struct folio *dst_folio,
6857                            const void __user *usr_src,
6858                            bool allow_pagefault)
6859 {
6860         void *kaddr;
6861         unsigned long i, rc = 0;
6862         unsigned int nr_pages = folio_nr_pages(dst_folio);
6863         unsigned long ret_val = nr_pages * PAGE_SIZE;
6864         struct page *subpage;
6865
6866         for (i = 0; i < nr_pages; i++) {
6867                 subpage = folio_page(dst_folio, i);
6868                 kaddr = kmap_local_page(subpage);
6869                 if (!allow_pagefault)
6870                         pagefault_disable();
6871                 rc = copy_from_user(kaddr, usr_src + i * PAGE_SIZE, PAGE_SIZE);
6872                 if (!allow_pagefault)
6873                         pagefault_enable();
6874                 kunmap_local(kaddr);
6875
6876                 ret_val -= (PAGE_SIZE - rc);
6877                 if (rc)
6878                         break;
6879
6880                 flush_dcache_page(subpage);
6881
6882                 cond_resched();
6883         }
6884         return ret_val;
6885 }
6886 #endif /* CONFIG_TRANSPARENT_HUGEPAGE || CONFIG_HUGETLBFS */
6887
6888 #if defined(CONFIG_SPLIT_PTE_PTLOCKS) && ALLOC_SPLIT_PTLOCKS
6889
6890 static struct kmem_cache *page_ptl_cachep;
6891
6892 void __init ptlock_cache_init(void)
6893 {
6894         page_ptl_cachep = kmem_cache_create("page->ptl", sizeof(spinlock_t), 0,
6895                         SLAB_PANIC, NULL);
6896 }
6897
6898 bool ptlock_alloc(struct ptdesc *ptdesc)
6899 {
6900         spinlock_t *ptl;
6901
6902         ptl = kmem_cache_alloc(page_ptl_cachep, GFP_KERNEL);
6903         if (!ptl)
6904                 return false;
6905         ptdesc->ptl = ptl;
6906         return true;
6907 }
6908
6909 void ptlock_free(struct ptdesc *ptdesc)
6910 {
6911         kmem_cache_free(page_ptl_cachep, ptdesc->ptl);
6912 }
6913 #endif
6914
6915 void vma_pgtable_walk_begin(struct vm_area_struct *vma)
6916 {
6917         if (is_vm_hugetlb_page(vma))
6918                 hugetlb_vma_lock_read(vma);
6919 }
6920
6921 void vma_pgtable_walk_end(struct vm_area_struct *vma)
6922 {
6923         if (is_vm_hugetlb_page(vma))
6924                 hugetlb_vma_unlock_read(vma);
6925 }
This page took 0.42232 seconds and 4 git commands to generate.