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