ksm: use a folio in replace_page()
[linux-block.git] / mm / memory.c
CommitLineData
457c8996 1// SPDX-License-Identifier: GPL-2.0-only
1da177e4
LT
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.
166f61b9 34 * Idea by Alex Bligh (alex@cconcepts.co.uk)
1da177e4
LT
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>
36090def 44#include <linux/mm_inline.h>
6e84f315 45#include <linux/sched/mm.h>
f7ccbae4 46#include <linux/sched/coredump.h>
6a3827d7 47#include <linux/sched/numa_balancing.h>
29930025 48#include <linux/sched/task.h>
1da177e4
LT
49#include <linux/hugetlb.h>
50#include <linux/mman.h>
51#include <linux/swap.h>
52#include <linux/highmem.h>
53#include <linux/pagemap.h>
5042db43 54#include <linux/memremap.h>
9a840895 55#include <linux/ksm.h>
1da177e4 56#include <linux/rmap.h>
b95f1b31 57#include <linux/export.h>
0ff92245 58#include <linux/delayacct.h>
1da177e4 59#include <linux/init.h>
01c8f1c4 60#include <linux/pfn_t.h>
edc79b2a 61#include <linux/writeback.h>
8a9f3ccd 62#include <linux/memcontrol.h>
cddb8a5c 63#include <linux/mmu_notifier.h>
3dc14741
HD
64#include <linux/swapops.h>
65#include <linux/elf.h>
5a0e3ad6 66#include <linux/gfp.h>
4daae3b4 67#include <linux/migrate.h>
2fbc57c5 68#include <linux/string.h>
467b171a 69#include <linux/memory-tiers.h>
1592eef0 70#include <linux/debugfs.h>
6b251fc9 71#include <linux/userfaultfd_k.h>
bc2466e4 72#include <linux/dax.h>
6b31d595 73#include <linux/oom.h>
98fa15f3 74#include <linux/numa.h>
bce617ed
PX
75#include <linux/perf_event.h>
76#include <linux/ptrace.h>
e80d3909 77#include <linux/vmalloc.h>
33024536 78#include <linux/sched/sysctl.h>
1da177e4 79
b3d1411b
JFG
80#include <trace/events/kmem.h>
81
6952b61d 82#include <asm/io.h>
33a709b2 83#include <asm/mmu_context.h>
1da177e4 84#include <asm/pgalloc.h>
7c0f6ba6 85#include <linux/uaccess.h>
1da177e4
LT
86#include <asm/tlb.h>
87#include <asm/tlbflush.h>
1da177e4 88
e80d3909 89#include "pgalloc-track.h"
42b77728 90#include "internal.h"
014bb1de 91#include "swap.h"
42b77728 92
af27d940 93#if defined(LAST_CPUPID_NOT_IN_PAGE_FLAGS) && !defined(CONFIG_COMPILE_TEST)
90572890 94#warning Unfortunate NUMA and NUMA Balancing config, growing page-frame for last_cpupid.
75980e97
PZ
95#endif
96
a9ee6cf5 97#ifndef CONFIG_NUMA
1da177e4 98unsigned long max_mapnr;
1da177e4 99EXPORT_SYMBOL(max_mapnr);
166f61b9
TH
100
101struct page *mem_map;
1da177e4
LT
102EXPORT_SYMBOL(mem_map);
103#endif
104
5c041f5d
PX
105static vm_fault_t do_fault(struct vm_fault *vmf);
106
1da177e4
LT
107/*
108 * A number of key systems in x86 including ioremap() rely on the assumption
109 * that high_memory defines the upper bound on direct map memory, then end
110 * of ZONE_NORMAL. Under CONFIG_DISCONTIG this means that max_low_pfn and
111 * highstart_pfn must be the same; there must be no gap between ZONE_NORMAL
112 * and ZONE_HIGHMEM.
113 */
166f61b9 114void *high_memory;
1da177e4 115EXPORT_SYMBOL(high_memory);
1da177e4 116
32a93233
IM
117/*
118 * Randomize the address space (stacks, mmaps, brk, etc.).
119 *
120 * ( When CONFIG_COMPAT_BRK=y we exclude brk from randomization,
121 * as ancient (libc5 based) binaries can segfault. )
122 */
123int randomize_va_space __read_mostly =
124#ifdef CONFIG_COMPAT_BRK
125 1;
126#else
127 2;
128#endif
a62eaf15 129
46bdb427
WD
130#ifndef arch_wants_old_prefaulted_pte
131static inline bool arch_wants_old_prefaulted_pte(void)
132{
133 /*
134 * Transitioning a PTE from 'old' to 'young' can be expensive on
135 * some architectures, even if it's performed in hardware. By
136 * default, "false" means prefaulted entries will be 'young'.
137 */
138 return false;
139}
140#endif
141
a62eaf15
AK
142static int __init disable_randmaps(char *s)
143{
144 randomize_va_space = 0;
9b41046c 145 return 1;
a62eaf15
AK
146}
147__setup("norandmaps", disable_randmaps);
148
62eede62 149unsigned long zero_pfn __read_mostly;
0b70068e
AB
150EXPORT_SYMBOL(zero_pfn);
151
166f61b9
TH
152unsigned long highest_memmap_pfn __read_mostly;
153
a13ea5b7
HD
154/*
155 * CONFIG_MMU architectures set up ZERO_PAGE in their paging_init()
156 */
157static int __init init_zero_pfn(void)
158{
159 zero_pfn = page_to_pfn(ZERO_PAGE(0));
160 return 0;
161}
e720e7d0 162early_initcall(init_zero_pfn);
a62eaf15 163
e4dcad20 164void mm_trace_rss_stat(struct mm_struct *mm, int member, long count)
b3d1411b 165{
e4dcad20 166 trace_rss_stat(mm, member, count);
b3d1411b 167}
d559db08 168
34e55232
KH
169#if defined(SPLIT_RSS_COUNTING)
170
ea48cf78 171void sync_mm_rss(struct mm_struct *mm)
34e55232
KH
172{
173 int i;
174
175 for (i = 0; i < NR_MM_COUNTERS; i++) {
05af2e10
DR
176 if (current->rss_stat.count[i]) {
177 add_mm_counter(mm, i, current->rss_stat.count[i]);
178 current->rss_stat.count[i] = 0;
34e55232
KH
179 }
180 }
05af2e10 181 current->rss_stat.events = 0;
34e55232
KH
182}
183
184static void add_mm_counter_fast(struct mm_struct *mm, int member, int val)
185{
186 struct task_struct *task = current;
187
188 if (likely(task->mm == mm))
189 task->rss_stat.count[member] += val;
190 else
191 add_mm_counter(mm, member, val);
192}
193#define inc_mm_counter_fast(mm, member) add_mm_counter_fast(mm, member, 1)
194#define dec_mm_counter_fast(mm, member) add_mm_counter_fast(mm, member, -1)
195
196/* sync counter once per 64 page faults */
197#define TASK_RSS_EVENTS_THRESH (64)
198static void check_sync_rss_stat(struct task_struct *task)
199{
200 if (unlikely(task != current))
201 return;
202 if (unlikely(task->rss_stat.events++ > TASK_RSS_EVENTS_THRESH))
ea48cf78 203 sync_mm_rss(task->mm);
34e55232 204}
9547d01b 205#else /* SPLIT_RSS_COUNTING */
34e55232
KH
206
207#define inc_mm_counter_fast(mm, member) inc_mm_counter(mm, member)
208#define dec_mm_counter_fast(mm, member) dec_mm_counter(mm, member)
209
210static void check_sync_rss_stat(struct task_struct *task)
211{
212}
213
9547d01b
PZ
214#endif /* SPLIT_RSS_COUNTING */
215
1da177e4
LT
216/*
217 * Note: this doesn't free the actual pages themselves. That
218 * has been handled earlier when unmapping all the memory regions.
219 */
9e1b32ca
BH
220static void free_pte_range(struct mmu_gather *tlb, pmd_t *pmd,
221 unsigned long addr)
1da177e4 222{
2f569afd 223 pgtable_t token = pmd_pgtable(*pmd);
e0da382c 224 pmd_clear(pmd);
9e1b32ca 225 pte_free_tlb(tlb, token, addr);
c4812909 226 mm_dec_nr_ptes(tlb->mm);
1da177e4
LT
227}
228
e0da382c
HD
229static inline void free_pmd_range(struct mmu_gather *tlb, pud_t *pud,
230 unsigned long addr, unsigned long end,
231 unsigned long floor, unsigned long ceiling)
1da177e4
LT
232{
233 pmd_t *pmd;
234 unsigned long next;
e0da382c 235 unsigned long start;
1da177e4 236
e0da382c 237 start = addr;
1da177e4 238 pmd = pmd_offset(pud, addr);
1da177e4
LT
239 do {
240 next = pmd_addr_end(addr, end);
241 if (pmd_none_or_clear_bad(pmd))
242 continue;
9e1b32ca 243 free_pte_range(tlb, pmd, addr);
1da177e4
LT
244 } while (pmd++, addr = next, addr != end);
245
e0da382c
HD
246 start &= PUD_MASK;
247 if (start < floor)
248 return;
249 if (ceiling) {
250 ceiling &= PUD_MASK;
251 if (!ceiling)
252 return;
1da177e4 253 }
e0da382c
HD
254 if (end - 1 > ceiling - 1)
255 return;
256
257 pmd = pmd_offset(pud, start);
258 pud_clear(pud);
9e1b32ca 259 pmd_free_tlb(tlb, pmd, start);
dc6c9a35 260 mm_dec_nr_pmds(tlb->mm);
1da177e4
LT
261}
262
c2febafc 263static inline void free_pud_range(struct mmu_gather *tlb, p4d_t *p4d,
e0da382c
HD
264 unsigned long addr, unsigned long end,
265 unsigned long floor, unsigned long ceiling)
1da177e4
LT
266{
267 pud_t *pud;
268 unsigned long next;
e0da382c 269 unsigned long start;
1da177e4 270
e0da382c 271 start = addr;
c2febafc 272 pud = pud_offset(p4d, addr);
1da177e4
LT
273 do {
274 next = pud_addr_end(addr, end);
275 if (pud_none_or_clear_bad(pud))
276 continue;
e0da382c 277 free_pmd_range(tlb, pud, addr, next, floor, ceiling);
1da177e4
LT
278 } while (pud++, addr = next, addr != end);
279
c2febafc
KS
280 start &= P4D_MASK;
281 if (start < floor)
282 return;
283 if (ceiling) {
284 ceiling &= P4D_MASK;
285 if (!ceiling)
286 return;
287 }
288 if (end - 1 > ceiling - 1)
289 return;
290
291 pud = pud_offset(p4d, start);
292 p4d_clear(p4d);
293 pud_free_tlb(tlb, pud, start);
b4e98d9a 294 mm_dec_nr_puds(tlb->mm);
c2febafc
KS
295}
296
297static inline void free_p4d_range(struct mmu_gather *tlb, pgd_t *pgd,
298 unsigned long addr, unsigned long end,
299 unsigned long floor, unsigned long ceiling)
300{
301 p4d_t *p4d;
302 unsigned long next;
303 unsigned long start;
304
305 start = addr;
306 p4d = p4d_offset(pgd, addr);
307 do {
308 next = p4d_addr_end(addr, end);
309 if (p4d_none_or_clear_bad(p4d))
310 continue;
311 free_pud_range(tlb, p4d, addr, next, floor, ceiling);
312 } while (p4d++, addr = next, addr != end);
313
e0da382c
HD
314 start &= PGDIR_MASK;
315 if (start < floor)
316 return;
317 if (ceiling) {
318 ceiling &= PGDIR_MASK;
319 if (!ceiling)
320 return;
1da177e4 321 }
e0da382c
HD
322 if (end - 1 > ceiling - 1)
323 return;
324
c2febafc 325 p4d = p4d_offset(pgd, start);
e0da382c 326 pgd_clear(pgd);
c2febafc 327 p4d_free_tlb(tlb, p4d, start);
1da177e4
LT
328}
329
330/*
e0da382c 331 * This function frees user-level page tables of a process.
1da177e4 332 */
42b77728 333void free_pgd_range(struct mmu_gather *tlb,
e0da382c
HD
334 unsigned long addr, unsigned long end,
335 unsigned long floor, unsigned long ceiling)
1da177e4
LT
336{
337 pgd_t *pgd;
338 unsigned long next;
e0da382c
HD
339
340 /*
341 * The next few lines have given us lots of grief...
342 *
343 * Why are we testing PMD* at this top level? Because often
344 * there will be no work to do at all, and we'd prefer not to
345 * go all the way down to the bottom just to discover that.
346 *
347 * Why all these "- 1"s? Because 0 represents both the bottom
348 * of the address space and the top of it (using -1 for the
349 * top wouldn't help much: the masks would do the wrong thing).
350 * The rule is that addr 0 and floor 0 refer to the bottom of
351 * the address space, but end 0 and ceiling 0 refer to the top
352 * Comparisons need to use "end - 1" and "ceiling - 1" (though
353 * that end 0 case should be mythical).
354 *
355 * Wherever addr is brought up or ceiling brought down, we must
356 * be careful to reject "the opposite 0" before it confuses the
357 * subsequent tests. But what about where end is brought down
358 * by PMD_SIZE below? no, end can't go down to 0 there.
359 *
360 * Whereas we round start (addr) and ceiling down, by different
361 * masks at different levels, in order to test whether a table
362 * now has no other vmas using it, so can be freed, we don't
363 * bother to round floor or end up - the tests don't need that.
364 */
1da177e4 365
e0da382c
HD
366 addr &= PMD_MASK;
367 if (addr < floor) {
368 addr += PMD_SIZE;
369 if (!addr)
370 return;
371 }
372 if (ceiling) {
373 ceiling &= PMD_MASK;
374 if (!ceiling)
375 return;
376 }
377 if (end - 1 > ceiling - 1)
378 end -= PMD_SIZE;
379 if (addr > end - 1)
380 return;
07e32661
AK
381 /*
382 * We add page table cache pages with PAGE_SIZE,
383 * (see pte_free_tlb()), flush the tlb if we need
384 */
ed6a7935 385 tlb_change_page_size(tlb, PAGE_SIZE);
42b77728 386 pgd = pgd_offset(tlb->mm, addr);
1da177e4
LT
387 do {
388 next = pgd_addr_end(addr, end);
389 if (pgd_none_or_clear_bad(pgd))
390 continue;
c2febafc 391 free_p4d_range(tlb, pgd, addr, next, floor, ceiling);
1da177e4 392 } while (pgd++, addr = next, addr != end);
e0da382c
HD
393}
394
763ecb03
LH
395void free_pgtables(struct mmu_gather *tlb, struct maple_tree *mt,
396 struct vm_area_struct *vma, unsigned long floor,
397 unsigned long ceiling)
e0da382c 398{
763ecb03
LH
399 MA_STATE(mas, mt, vma->vm_end, vma->vm_end);
400
401 do {
e0da382c 402 unsigned long addr = vma->vm_start;
763ecb03
LH
403 struct vm_area_struct *next;
404
405 /*
406 * Note: USER_PGTABLES_CEILING may be passed as ceiling and may
407 * be 0. This will underflow and is okay.
408 */
409 next = mas_find(&mas, ceiling - 1);
e0da382c 410
8f4f8c16 411 /*
25d9e2d1 412 * Hide vma from rmap and truncate_pagecache before freeing
413 * pgtables
8f4f8c16 414 */
5beb4930 415 unlink_anon_vmas(vma);
8f4f8c16
HD
416 unlink_file_vma(vma);
417
9da61aef 418 if (is_vm_hugetlb_page(vma)) {
3bf5ee95 419 hugetlb_free_pgd_range(tlb, addr, vma->vm_end,
166f61b9 420 floor, next ? next->vm_start : ceiling);
3bf5ee95
HD
421 } else {
422 /*
423 * Optimization: gather nearby vmas into one call down
424 */
425 while (next && next->vm_start <= vma->vm_end + PMD_SIZE
4866920b 426 && !is_vm_hugetlb_page(next)) {
3bf5ee95 427 vma = next;
763ecb03 428 next = mas_find(&mas, ceiling - 1);
5beb4930 429 unlink_anon_vmas(vma);
8f4f8c16 430 unlink_file_vma(vma);
3bf5ee95
HD
431 }
432 free_pgd_range(tlb, addr, vma->vm_end,
166f61b9 433 floor, next ? next->vm_start : ceiling);
3bf5ee95 434 }
e0da382c 435 vma = next;
763ecb03 436 } while (vma);
1da177e4
LT
437}
438
03c4f204 439void pmd_install(struct mm_struct *mm, pmd_t *pmd, pgtable_t *pte)
1da177e4 440{
03c4f204 441 spinlock_t *ptl = pmd_lock(mm, pmd);
1bb3630e 442
8ac1f832 443 if (likely(pmd_none(*pmd))) { /* Has another populated it ? */
c4812909 444 mm_inc_nr_ptes(mm);
ed33b5a6
QZ
445 /*
446 * Ensure all pte setup (eg. pte page lock and page clearing) are
447 * visible before the pte is made visible to other CPUs by being
448 * put into page tables.
449 *
450 * The other side of the story is the pointer chasing in the page
451 * table walking code (when walking the page table without locking;
452 * ie. most of the time). Fortunately, these data accesses consist
453 * of a chain of data-dependent loads, meaning most CPUs (alpha
454 * being the notable exception) will already guarantee loads are
455 * seen in-order. See the alpha page table accessors for the
456 * smp_rmb() barriers in page table walking code.
457 */
458 smp_wmb(); /* Could be smp_wmb__xxx(before|after)_spin_lock */
03c4f204
QZ
459 pmd_populate(mm, pmd, *pte);
460 *pte = NULL;
4b471e88 461 }
c4088ebd 462 spin_unlock(ptl);
03c4f204
QZ
463}
464
4cf58924 465int __pte_alloc(struct mm_struct *mm, pmd_t *pmd)
1da177e4 466{
4cf58924 467 pgtable_t new = pte_alloc_one(mm);
1bb3630e
HD
468 if (!new)
469 return -ENOMEM;
470
03c4f204 471 pmd_install(mm, pmd, &new);
2f569afd
MS
472 if (new)
473 pte_free(mm, new);
1bb3630e 474 return 0;
1da177e4
LT
475}
476
4cf58924 477int __pte_alloc_kernel(pmd_t *pmd)
1da177e4 478{
4cf58924 479 pte_t *new = pte_alloc_one_kernel(&init_mm);
1bb3630e
HD
480 if (!new)
481 return -ENOMEM;
482
483 spin_lock(&init_mm.page_table_lock);
8ac1f832 484 if (likely(pmd_none(*pmd))) { /* Has another populated it ? */
ed33b5a6 485 smp_wmb(); /* See comment in pmd_install() */
1bb3630e 486 pmd_populate_kernel(&init_mm, pmd, new);
2f569afd 487 new = NULL;
4b471e88 488 }
1bb3630e 489 spin_unlock(&init_mm.page_table_lock);
2f569afd
MS
490 if (new)
491 pte_free_kernel(&init_mm, new);
1bb3630e 492 return 0;
1da177e4
LT
493}
494
d559db08
KH
495static inline void init_rss_vec(int *rss)
496{
497 memset(rss, 0, sizeof(int) * NR_MM_COUNTERS);
498}
499
500static inline void add_mm_rss_vec(struct mm_struct *mm, int *rss)
ae859762 501{
d559db08
KH
502 int i;
503
34e55232 504 if (current->mm == mm)
05af2e10 505 sync_mm_rss(mm);
d559db08
KH
506 for (i = 0; i < NR_MM_COUNTERS; i++)
507 if (rss[i])
508 add_mm_counter(mm, i, rss[i]);
ae859762
HD
509}
510
b5810039 511/*
6aab341e
LT
512 * This function is called to print an error when a bad pte
513 * is found. For example, we might have a PFN-mapped pte in
514 * a region that doesn't allow it.
b5810039
NP
515 *
516 * The calling function must still handle the error.
517 */
3dc14741
HD
518static void print_bad_pte(struct vm_area_struct *vma, unsigned long addr,
519 pte_t pte, struct page *page)
b5810039 520{
3dc14741 521 pgd_t *pgd = pgd_offset(vma->vm_mm, addr);
c2febafc
KS
522 p4d_t *p4d = p4d_offset(pgd, addr);
523 pud_t *pud = pud_offset(p4d, addr);
3dc14741
HD
524 pmd_t *pmd = pmd_offset(pud, addr);
525 struct address_space *mapping;
526 pgoff_t index;
d936cf9b
HD
527 static unsigned long resume;
528 static unsigned long nr_shown;
529 static unsigned long nr_unshown;
530
531 /*
532 * Allow a burst of 60 reports, then keep quiet for that minute;
533 * or allow a steady drip of one report per second.
534 */
535 if (nr_shown == 60) {
536 if (time_before(jiffies, resume)) {
537 nr_unshown++;
538 return;
539 }
540 if (nr_unshown) {
1170532b
JP
541 pr_alert("BUG: Bad page map: %lu messages suppressed\n",
542 nr_unshown);
d936cf9b
HD
543 nr_unshown = 0;
544 }
545 nr_shown = 0;
546 }
547 if (nr_shown++ == 0)
548 resume = jiffies + 60 * HZ;
3dc14741
HD
549
550 mapping = vma->vm_file ? vma->vm_file->f_mapping : NULL;
551 index = linear_page_index(vma, addr);
552
1170532b
JP
553 pr_alert("BUG: Bad page map in process %s pte:%08llx pmd:%08llx\n",
554 current->comm,
555 (long long)pte_val(pte), (long long)pmd_val(*pmd));
718a3821 556 if (page)
f0b791a3 557 dump_page(page, "bad pte");
6aa9b8b2 558 pr_alert("addr:%px vm_flags:%08lx anon_vma:%px mapping:%px index:%lx\n",
1170532b 559 (void *)addr, vma->vm_flags, vma->anon_vma, mapping, index);
7e0a1265 560 pr_alert("file:%pD fault:%ps mmap:%ps read_folio:%ps\n",
2682582a
KK
561 vma->vm_file,
562 vma->vm_ops ? vma->vm_ops->fault : NULL,
563 vma->vm_file ? vma->vm_file->f_op->mmap : NULL,
7e0a1265 564 mapping ? mapping->a_ops->read_folio : NULL);
b5810039 565 dump_stack();
373d4d09 566 add_taint(TAINT_BAD_PAGE, LOCKDEP_NOW_UNRELIABLE);
b5810039
NP
567}
568
ee498ed7 569/*
7e675137 570 * vm_normal_page -- This function gets the "struct page" associated with a pte.
6aab341e 571 *
7e675137
NP
572 * "Special" mappings do not wish to be associated with a "struct page" (either
573 * it doesn't exist, or it exists but they don't want to touch it). In this
574 * case, NULL is returned here. "Normal" mappings do have a struct page.
b379d790 575 *
7e675137
NP
576 * There are 2 broad cases. Firstly, an architecture may define a pte_special()
577 * pte bit, in which case this function is trivial. Secondly, an architecture
578 * may not have a spare pte bit, which requires a more complicated scheme,
579 * described below.
580 *
581 * A raw VM_PFNMAP mapping (ie. one that is not COWed) is always considered a
582 * special mapping (even if there are underlying and valid "struct pages").
583 * COWed pages of a VM_PFNMAP are always normal.
6aab341e 584 *
b379d790
JH
585 * The way we recognize COWed pages within VM_PFNMAP mappings is through the
586 * rules set up by "remap_pfn_range()": the vma will have the VM_PFNMAP bit
7e675137
NP
587 * set, and the vm_pgoff will point to the first PFN mapped: thus every special
588 * mapping will always honor the rule
6aab341e
LT
589 *
590 * pfn_of_page == vma->vm_pgoff + ((addr - vma->vm_start) >> PAGE_SHIFT)
591 *
7e675137
NP
592 * And for normal mappings this is false.
593 *
594 * This restricts such mappings to be a linear translation from virtual address
595 * to pfn. To get around this restriction, we allow arbitrary mappings so long
596 * as the vma is not a COW mapping; in that case, we know that all ptes are
597 * special (because none can have been COWed).
b379d790 598 *
b379d790 599 *
7e675137 600 * In order to support COW of arbitrary special mappings, we have VM_MIXEDMAP.
b379d790
JH
601 *
602 * VM_MIXEDMAP mappings can likewise contain memory with or without "struct
603 * page" backing, however the difference is that _all_ pages with a struct
604 * page (that is, those where pfn_valid is true) are refcounted and considered
605 * normal pages by the VM. The disadvantage is that pages are refcounted
606 * (which can be slower and simply not an option for some PFNMAP users). The
607 * advantage is that we don't have to follow the strict linearity rule of
608 * PFNMAP mappings in order to support COWable mappings.
609 *
ee498ed7 610 */
25b2995a
CH
611struct page *vm_normal_page(struct vm_area_struct *vma, unsigned long addr,
612 pte_t pte)
ee498ed7 613{
22b31eec 614 unsigned long pfn = pte_pfn(pte);
7e675137 615
00b3a331 616 if (IS_ENABLED(CONFIG_ARCH_HAS_PTE_SPECIAL)) {
b38af472 617 if (likely(!pte_special(pte)))
22b31eec 618 goto check_pfn;
667a0a06
DV
619 if (vma->vm_ops && vma->vm_ops->find_special_page)
620 return vma->vm_ops->find_special_page(vma, addr);
a13ea5b7
HD
621 if (vma->vm_flags & (VM_PFNMAP | VM_MIXEDMAP))
622 return NULL;
df6ad698
JG
623 if (is_zero_pfn(pfn))
624 return NULL;
e1fb4a08 625 if (pte_devmap(pte))
3218f871
AS
626 /*
627 * NOTE: New users of ZONE_DEVICE will not set pte_devmap()
628 * and will have refcounts incremented on their struct pages
629 * when they are inserted into PTEs, thus they are safe to
630 * return here. Legacy ZONE_DEVICE pages that set pte_devmap()
631 * do not have refcounts. Example of legacy ZONE_DEVICE is
632 * MEMORY_DEVICE_FS_DAX type in pmem or virtio_fs drivers.
633 */
e1fb4a08
DJ
634 return NULL;
635
df6ad698 636 print_bad_pte(vma, addr, pte, NULL);
7e675137
NP
637 return NULL;
638 }
639
00b3a331 640 /* !CONFIG_ARCH_HAS_PTE_SPECIAL case follows: */
7e675137 641
b379d790
JH
642 if (unlikely(vma->vm_flags & (VM_PFNMAP|VM_MIXEDMAP))) {
643 if (vma->vm_flags & VM_MIXEDMAP) {
644 if (!pfn_valid(pfn))
645 return NULL;
646 goto out;
647 } else {
7e675137
NP
648 unsigned long off;
649 off = (addr - vma->vm_start) >> PAGE_SHIFT;
b379d790
JH
650 if (pfn == vma->vm_pgoff + off)
651 return NULL;
652 if (!is_cow_mapping(vma->vm_flags))
653 return NULL;
654 }
6aab341e
LT
655 }
656
b38af472
HD
657 if (is_zero_pfn(pfn))
658 return NULL;
00b3a331 659
22b31eec
HD
660check_pfn:
661 if (unlikely(pfn > highest_memmap_pfn)) {
662 print_bad_pte(vma, addr, pte, NULL);
663 return NULL;
664 }
6aab341e
LT
665
666 /*
7e675137 667 * NOTE! We still have PageReserved() pages in the page tables.
7e675137 668 * eg. VDSO mappings can cause them to exist.
6aab341e 669 */
b379d790 670out:
6aab341e 671 return pfn_to_page(pfn);
ee498ed7
HD
672}
673
28093f9f
GS
674#ifdef CONFIG_TRANSPARENT_HUGEPAGE
675struct page *vm_normal_page_pmd(struct vm_area_struct *vma, unsigned long addr,
676 pmd_t pmd)
677{
678 unsigned long pfn = pmd_pfn(pmd);
679
680 /*
681 * There is no pmd_special() but there may be special pmds, e.g.
682 * in a direct-access (dax) mapping, so let's just replicate the
00b3a331 683 * !CONFIG_ARCH_HAS_PTE_SPECIAL case from vm_normal_page() here.
28093f9f
GS
684 */
685 if (unlikely(vma->vm_flags & (VM_PFNMAP|VM_MIXEDMAP))) {
686 if (vma->vm_flags & VM_MIXEDMAP) {
687 if (!pfn_valid(pfn))
688 return NULL;
689 goto out;
690 } else {
691 unsigned long off;
692 off = (addr - vma->vm_start) >> PAGE_SHIFT;
693 if (pfn == vma->vm_pgoff + off)
694 return NULL;
695 if (!is_cow_mapping(vma->vm_flags))
696 return NULL;
697 }
698 }
699
e1fb4a08
DJ
700 if (pmd_devmap(pmd))
701 return NULL;
3cde287b 702 if (is_huge_zero_pmd(pmd))
28093f9f
GS
703 return NULL;
704 if (unlikely(pfn > highest_memmap_pfn))
705 return NULL;
706
707 /*
708 * NOTE! We still have PageReserved() pages in the page tables.
709 * eg. VDSO mappings can cause them to exist.
710 */
711out:
712 return pfn_to_page(pfn);
713}
714#endif
715
b756a3b5
AP
716static void restore_exclusive_pte(struct vm_area_struct *vma,
717 struct page *page, unsigned long address,
718 pte_t *ptep)
719{
720 pte_t pte;
721 swp_entry_t entry;
722
723 pte = pte_mkold(mk_pte(page, READ_ONCE(vma->vm_page_prot)));
724 if (pte_swp_soft_dirty(*ptep))
725 pte = pte_mksoft_dirty(pte);
726
727 entry = pte_to_swp_entry(*ptep);
728 if (pte_swp_uffd_wp(*ptep))
729 pte = pte_mkuffd_wp(pte);
730 else if (is_writable_device_exclusive_entry(entry))
731 pte = maybe_mkwrite(pte_mkdirty(pte), vma);
732
6c287605
DH
733 VM_BUG_ON(pte_write(pte) && !(PageAnon(page) && PageAnonExclusive(page)));
734
b756a3b5
AP
735 /*
736 * No need to take a page reference as one was already
737 * created when the swap entry was made.
738 */
739 if (PageAnon(page))
f1e2db12 740 page_add_anon_rmap(page, vma, address, RMAP_NONE);
b756a3b5
AP
741 else
742 /*
743 * Currently device exclusive access only supports anonymous
744 * memory so the entry shouldn't point to a filebacked page.
745 */
4d8ff640 746 WARN_ON_ONCE(1);
b756a3b5 747
1eba86c0
PT
748 set_pte_at(vma->vm_mm, address, ptep, pte);
749
b756a3b5
AP
750 /*
751 * No need to invalidate - it was non-present before. However
752 * secondary CPUs may have mappings that need invalidating.
753 */
754 update_mmu_cache(vma, address, ptep);
755}
756
757/*
758 * Tries to restore an exclusive pte if the page lock can be acquired without
759 * sleeping.
760 */
761static int
762try_restore_exclusive_pte(pte_t *src_pte, struct vm_area_struct *vma,
763 unsigned long addr)
764{
765 swp_entry_t entry = pte_to_swp_entry(*src_pte);
766 struct page *page = pfn_swap_entry_to_page(entry);
767
768 if (trylock_page(page)) {
769 restore_exclusive_pte(vma, page, addr, src_pte);
770 unlock_page(page);
771 return 0;
772 }
773
774 return -EBUSY;
775}
776
1da177e4
LT
777/*
778 * copy one vm_area from one task to the other. Assumes the page tables
779 * already present in the new task to be cleared in the whole range
780 * covered by this vma.
1da177e4
LT
781 */
782
df3a57d1
LT
783static unsigned long
784copy_nonpresent_pte(struct mm_struct *dst_mm, struct mm_struct *src_mm,
8f34f1ea
PX
785 pte_t *dst_pte, pte_t *src_pte, struct vm_area_struct *dst_vma,
786 struct vm_area_struct *src_vma, unsigned long addr, int *rss)
1da177e4 787{
8f34f1ea 788 unsigned long vm_flags = dst_vma->vm_flags;
1da177e4
LT
789 pte_t pte = *src_pte;
790 struct page *page;
df3a57d1
LT
791 swp_entry_t entry = pte_to_swp_entry(pte);
792
793 if (likely(!non_swap_entry(entry))) {
794 if (swap_duplicate(entry) < 0)
9a5cc85c 795 return -EIO;
df3a57d1
LT
796
797 /* make sure dst_mm is on swapoff's mmlist. */
798 if (unlikely(list_empty(&dst_mm->mmlist))) {
799 spin_lock(&mmlist_lock);
800 if (list_empty(&dst_mm->mmlist))
801 list_add(&dst_mm->mmlist,
802 &src_mm->mmlist);
803 spin_unlock(&mmlist_lock);
804 }
1493a191
DH
805 /* Mark the swap entry as shared. */
806 if (pte_swp_exclusive(*src_pte)) {
807 pte = pte_swp_clear_exclusive(*src_pte);
808 set_pte_at(src_mm, addr, src_pte, pte);
809 }
df3a57d1
LT
810 rss[MM_SWAPENTS]++;
811 } else if (is_migration_entry(entry)) {
af5cdaf8 812 page = pfn_swap_entry_to_page(entry);
1da177e4 813
df3a57d1 814 rss[mm_counter(page)]++;
5042db43 815
6c287605 816 if (!is_readable_migration_entry(entry) &&
df3a57d1 817 is_cow_mapping(vm_flags)) {
5042db43 818 /*
6c287605
DH
819 * COW mappings require pages in both parent and child
820 * to be set to read. A previously exclusive entry is
821 * now shared.
5042db43 822 */
4dd845b5
AP
823 entry = make_readable_migration_entry(
824 swp_offset(entry));
df3a57d1
LT
825 pte = swp_entry_to_pte(entry);
826 if (pte_swp_soft_dirty(*src_pte))
827 pte = pte_swp_mksoft_dirty(pte);
828 if (pte_swp_uffd_wp(*src_pte))
829 pte = pte_swp_mkuffd_wp(pte);
830 set_pte_at(src_mm, addr, src_pte, pte);
831 }
832 } else if (is_device_private_entry(entry)) {
af5cdaf8 833 page = pfn_swap_entry_to_page(entry);
5042db43 834
df3a57d1
LT
835 /*
836 * Update rss count even for unaddressable pages, as
837 * they should treated just like normal pages in this
838 * respect.
839 *
840 * We will likely want to have some new rss counters
841 * for unaddressable pages, at some point. But for now
842 * keep things as they are.
843 */
844 get_page(page);
845 rss[mm_counter(page)]++;
fb3d824d
DH
846 /* Cannot fail as these pages cannot get pinned. */
847 BUG_ON(page_try_dup_anon_rmap(page, false, src_vma));
df3a57d1
LT
848
849 /*
850 * We do not preserve soft-dirty information, because so
851 * far, checkpoint/restore is the only feature that
852 * requires that. And checkpoint/restore does not work
853 * when a device driver is involved (you cannot easily
854 * save and restore device driver state).
855 */
4dd845b5 856 if (is_writable_device_private_entry(entry) &&
df3a57d1 857 is_cow_mapping(vm_flags)) {
4dd845b5
AP
858 entry = make_readable_device_private_entry(
859 swp_offset(entry));
df3a57d1
LT
860 pte = swp_entry_to_pte(entry);
861 if (pte_swp_uffd_wp(*src_pte))
862 pte = pte_swp_mkuffd_wp(pte);
863 set_pte_at(src_mm, addr, src_pte, pte);
1da177e4 864 }
b756a3b5
AP
865 } else if (is_device_exclusive_entry(entry)) {
866 /*
867 * Make device exclusive entries present by restoring the
868 * original entry then copying as for a present pte. Device
869 * exclusive entries currently only support private writable
870 * (ie. COW) mappings.
871 */
872 VM_BUG_ON(!is_cow_mapping(src_vma->vm_flags));
873 if (try_restore_exclusive_pte(src_pte, src_vma, addr))
874 return -EBUSY;
875 return -ENOENT;
c56d1b62
PX
876 } else if (is_pte_marker_entry(entry)) {
877 /*
878 * We're copying the pgtable should only because dst_vma has
879 * uffd-wp enabled, do sanity check.
880 */
881 WARN_ON_ONCE(!userfaultfd_wp(dst_vma));
882 set_pte_at(dst_mm, addr, dst_pte, pte);
883 return 0;
1da177e4 884 }
8f34f1ea
PX
885 if (!userfaultfd_wp(dst_vma))
886 pte = pte_swp_clear_uffd_wp(pte);
df3a57d1
LT
887 set_pte_at(dst_mm, addr, dst_pte, pte);
888 return 0;
889}
890
70e806e4 891/*
b51ad4f8 892 * Copy a present and normal page.
70e806e4 893 *
b51ad4f8
DH
894 * NOTE! The usual case is that this isn't required;
895 * instead, the caller can just increase the page refcount
896 * and re-use the pte the traditional way.
70e806e4
PX
897 *
898 * And if we need a pre-allocated page but don't yet have
899 * one, return a negative error to let the preallocation
900 * code know so that it can do so outside the page table
901 * lock.
902 */
903static inline int
c78f4636
PX
904copy_present_page(struct vm_area_struct *dst_vma, struct vm_area_struct *src_vma,
905 pte_t *dst_pte, pte_t *src_pte, unsigned long addr, int *rss,
b51ad4f8 906 struct page **prealloc, struct page *page)
70e806e4
PX
907{
908 struct page *new_page;
b51ad4f8 909 pte_t pte;
70e806e4 910
70e806e4
PX
911 new_page = *prealloc;
912 if (!new_page)
913 return -EAGAIN;
914
915 /*
916 * We have a prealloc page, all good! Take it
917 * over and copy the page & arm it.
918 */
919 *prealloc = NULL;
c78f4636 920 copy_user_highpage(new_page, page, addr, src_vma);
70e806e4 921 __SetPageUptodate(new_page);
40f2bbf7 922 page_add_new_anon_rmap(new_page, dst_vma, addr);
c78f4636 923 lru_cache_add_inactive_or_unevictable(new_page, dst_vma);
70e806e4
PX
924 rss[mm_counter(new_page)]++;
925
926 /* All done, just insert the new page copy in the child */
c78f4636
PX
927 pte = mk_pte(new_page, dst_vma->vm_page_prot);
928 pte = maybe_mkwrite(pte_mkdirty(pte), dst_vma);
8f34f1ea
PX
929 if (userfaultfd_pte_wp(dst_vma, *src_pte))
930 /* Uffd-wp needs to be delivered to dest pte as well */
931 pte = pte_wrprotect(pte_mkuffd_wp(pte));
c78f4636 932 set_pte_at(dst_vma->vm_mm, addr, dst_pte, pte);
70e806e4
PX
933 return 0;
934}
935
936/*
937 * Copy one pte. Returns 0 if succeeded, or -EAGAIN if one preallocated page
938 * is required to copy this pte.
939 */
940static inline int
c78f4636
PX
941copy_present_pte(struct vm_area_struct *dst_vma, struct vm_area_struct *src_vma,
942 pte_t *dst_pte, pte_t *src_pte, unsigned long addr, int *rss,
943 struct page **prealloc)
df3a57d1 944{
c78f4636
PX
945 struct mm_struct *src_mm = src_vma->vm_mm;
946 unsigned long vm_flags = src_vma->vm_flags;
df3a57d1
LT
947 pte_t pte = *src_pte;
948 struct page *page;
949
c78f4636 950 page = vm_normal_page(src_vma, addr, pte);
fb3d824d 951 if (page && PageAnon(page)) {
b51ad4f8
DH
952 /*
953 * If this page may have been pinned by the parent process,
954 * copy the page immediately for the child so that we'll always
955 * guarantee the pinned page won't be randomly replaced in the
956 * future.
957 */
70e806e4 958 get_page(page);
fb3d824d
DH
959 if (unlikely(page_try_dup_anon_rmap(page, false, src_vma))) {
960 /* Page maybe pinned, we have to copy. */
961 put_page(page);
962 return copy_present_page(dst_vma, src_vma, dst_pte, src_pte,
963 addr, rss, prealloc, page);
964 }
965 rss[mm_counter(page)]++;
b51ad4f8 966 } else if (page) {
70e806e4 967 get_page(page);
fb3d824d 968 page_dup_file_rmap(page, false);
70e806e4
PX
969 rss[mm_counter(page)]++;
970 }
971
1da177e4
LT
972 /*
973 * If it's a COW mapping, write protect it both
974 * in the parent and the child
975 */
1b2de5d0 976 if (is_cow_mapping(vm_flags) && pte_write(pte)) {
1da177e4 977 ptep_set_wrprotect(src_mm, addr, src_pte);
3dc90795 978 pte = pte_wrprotect(pte);
1da177e4 979 }
6c287605 980 VM_BUG_ON(page && PageAnon(page) && PageAnonExclusive(page));
1da177e4
LT
981
982 /*
983 * If it's a shared mapping, mark it clean in
984 * the child
985 */
986 if (vm_flags & VM_SHARED)
987 pte = pte_mkclean(pte);
988 pte = pte_mkold(pte);
6aab341e 989
8f34f1ea 990 if (!userfaultfd_wp(dst_vma))
b569a176
PX
991 pte = pte_clear_uffd_wp(pte);
992
c78f4636 993 set_pte_at(dst_vma->vm_mm, addr, dst_pte, pte);
70e806e4
PX
994 return 0;
995}
996
997static inline struct page *
998page_copy_prealloc(struct mm_struct *src_mm, struct vm_area_struct *vma,
999 unsigned long addr)
1000{
1001 struct page *new_page;
1002
1003 new_page = alloc_page_vma(GFP_HIGHUSER_MOVABLE, vma, addr);
1004 if (!new_page)
1005 return NULL;
1006
8f425e4e 1007 if (mem_cgroup_charge(page_folio(new_page), src_mm, GFP_KERNEL)) {
70e806e4
PX
1008 put_page(new_page);
1009 return NULL;
6aab341e 1010 }
70e806e4 1011 cgroup_throttle_swaprate(new_page, GFP_KERNEL);
ae859762 1012
70e806e4 1013 return new_page;
1da177e4
LT
1014}
1015
c78f4636
PX
1016static int
1017copy_pte_range(struct vm_area_struct *dst_vma, struct vm_area_struct *src_vma,
1018 pmd_t *dst_pmd, pmd_t *src_pmd, unsigned long addr,
1019 unsigned long end)
1da177e4 1020{
c78f4636
PX
1021 struct mm_struct *dst_mm = dst_vma->vm_mm;
1022 struct mm_struct *src_mm = src_vma->vm_mm;
c36987e2 1023 pte_t *orig_src_pte, *orig_dst_pte;
1da177e4 1024 pte_t *src_pte, *dst_pte;
c74df32c 1025 spinlock_t *src_ptl, *dst_ptl;
70e806e4 1026 int progress, ret = 0;
d559db08 1027 int rss[NR_MM_COUNTERS];
570a335b 1028 swp_entry_t entry = (swp_entry_t){0};
70e806e4 1029 struct page *prealloc = NULL;
1da177e4
LT
1030
1031again:
70e806e4 1032 progress = 0;
d559db08
KH
1033 init_rss_vec(rss);
1034
c74df32c 1035 dst_pte = pte_alloc_map_lock(dst_mm, dst_pmd, addr, &dst_ptl);
70e806e4
PX
1036 if (!dst_pte) {
1037 ret = -ENOMEM;
1038 goto out;
1039 }
ece0e2b6 1040 src_pte = pte_offset_map(src_pmd, addr);
4c21e2f2 1041 src_ptl = pte_lockptr(src_mm, src_pmd);
f20dc5f7 1042 spin_lock_nested(src_ptl, SINGLE_DEPTH_NESTING);
c36987e2
DN
1043 orig_src_pte = src_pte;
1044 orig_dst_pte = dst_pte;
6606c3e0 1045 arch_enter_lazy_mmu_mode();
1da177e4 1046
1da177e4
LT
1047 do {
1048 /*
1049 * We are holding two locks at this point - either of them
1050 * could generate latencies in another task on another CPU.
1051 */
e040f218
HD
1052 if (progress >= 32) {
1053 progress = 0;
1054 if (need_resched() ||
95c354fe 1055 spin_needbreak(src_ptl) || spin_needbreak(dst_ptl))
e040f218
HD
1056 break;
1057 }
1da177e4
LT
1058 if (pte_none(*src_pte)) {
1059 progress++;
1060 continue;
1061 }
79a1971c 1062 if (unlikely(!pte_present(*src_pte))) {
9a5cc85c
AP
1063 ret = copy_nonpresent_pte(dst_mm, src_mm,
1064 dst_pte, src_pte,
1065 dst_vma, src_vma,
1066 addr, rss);
1067 if (ret == -EIO) {
1068 entry = pte_to_swp_entry(*src_pte);
79a1971c 1069 break;
b756a3b5
AP
1070 } else if (ret == -EBUSY) {
1071 break;
1072 } else if (!ret) {
1073 progress += 8;
1074 continue;
9a5cc85c 1075 }
b756a3b5
AP
1076
1077 /*
1078 * Device exclusive entry restored, continue by copying
1079 * the now present pte.
1080 */
1081 WARN_ON_ONCE(ret != -ENOENT);
79a1971c 1082 }
70e806e4 1083 /* copy_present_pte() will clear `*prealloc' if consumed */
c78f4636
PX
1084 ret = copy_present_pte(dst_vma, src_vma, dst_pte, src_pte,
1085 addr, rss, &prealloc);
70e806e4
PX
1086 /*
1087 * If we need a pre-allocated page for this pte, drop the
1088 * locks, allocate, and try again.
1089 */
1090 if (unlikely(ret == -EAGAIN))
1091 break;
1092 if (unlikely(prealloc)) {
1093 /*
1094 * pre-alloc page cannot be reused by next time so as
1095 * to strictly follow mempolicy (e.g., alloc_page_vma()
1096 * will allocate page according to address). This
1097 * could only happen if one pinned pte changed.
1098 */
1099 put_page(prealloc);
1100 prealloc = NULL;
1101 }
1da177e4
LT
1102 progress += 8;
1103 } while (dst_pte++, src_pte++, addr += PAGE_SIZE, addr != end);
1da177e4 1104
6606c3e0 1105 arch_leave_lazy_mmu_mode();
c74df32c 1106 spin_unlock(src_ptl);
ece0e2b6 1107 pte_unmap(orig_src_pte);
d559db08 1108 add_mm_rss_vec(dst_mm, rss);
c36987e2 1109 pte_unmap_unlock(orig_dst_pte, dst_ptl);
c74df32c 1110 cond_resched();
570a335b 1111
9a5cc85c
AP
1112 if (ret == -EIO) {
1113 VM_WARN_ON_ONCE(!entry.val);
70e806e4
PX
1114 if (add_swap_count_continuation(entry, GFP_KERNEL) < 0) {
1115 ret = -ENOMEM;
1116 goto out;
1117 }
1118 entry.val = 0;
b756a3b5
AP
1119 } else if (ret == -EBUSY) {
1120 goto out;
9a5cc85c 1121 } else if (ret == -EAGAIN) {
c78f4636 1122 prealloc = page_copy_prealloc(src_mm, src_vma, addr);
70e806e4 1123 if (!prealloc)
570a335b 1124 return -ENOMEM;
9a5cc85c
AP
1125 } else if (ret) {
1126 VM_WARN_ON_ONCE(1);
570a335b 1127 }
9a5cc85c
AP
1128
1129 /* We've captured and resolved the error. Reset, try again. */
1130 ret = 0;
1131
1da177e4
LT
1132 if (addr != end)
1133 goto again;
70e806e4
PX
1134out:
1135 if (unlikely(prealloc))
1136 put_page(prealloc);
1137 return ret;
1da177e4
LT
1138}
1139
c78f4636
PX
1140static inline int
1141copy_pmd_range(struct vm_area_struct *dst_vma, struct vm_area_struct *src_vma,
1142 pud_t *dst_pud, pud_t *src_pud, unsigned long addr,
1143 unsigned long end)
1da177e4 1144{
c78f4636
PX
1145 struct mm_struct *dst_mm = dst_vma->vm_mm;
1146 struct mm_struct *src_mm = src_vma->vm_mm;
1da177e4
LT
1147 pmd_t *src_pmd, *dst_pmd;
1148 unsigned long next;
1149
1150 dst_pmd = pmd_alloc(dst_mm, dst_pud, addr);
1151 if (!dst_pmd)
1152 return -ENOMEM;
1153 src_pmd = pmd_offset(src_pud, addr);
1154 do {
1155 next = pmd_addr_end(addr, end);
84c3fc4e
ZY
1156 if (is_swap_pmd(*src_pmd) || pmd_trans_huge(*src_pmd)
1157 || pmd_devmap(*src_pmd)) {
71e3aac0 1158 int err;
c78f4636 1159 VM_BUG_ON_VMA(next-addr != HPAGE_PMD_SIZE, src_vma);
8f34f1ea
PX
1160 err = copy_huge_pmd(dst_mm, src_mm, dst_pmd, src_pmd,
1161 addr, dst_vma, src_vma);
71e3aac0
AA
1162 if (err == -ENOMEM)
1163 return -ENOMEM;
1164 if (!err)
1165 continue;
1166 /* fall through */
1167 }
1da177e4
LT
1168 if (pmd_none_or_clear_bad(src_pmd))
1169 continue;
c78f4636
PX
1170 if (copy_pte_range(dst_vma, src_vma, dst_pmd, src_pmd,
1171 addr, next))
1da177e4
LT
1172 return -ENOMEM;
1173 } while (dst_pmd++, src_pmd++, addr = next, addr != end);
1174 return 0;
1175}
1176
c78f4636
PX
1177static inline int
1178copy_pud_range(struct vm_area_struct *dst_vma, struct vm_area_struct *src_vma,
1179 p4d_t *dst_p4d, p4d_t *src_p4d, unsigned long addr,
1180 unsigned long end)
1da177e4 1181{
c78f4636
PX
1182 struct mm_struct *dst_mm = dst_vma->vm_mm;
1183 struct mm_struct *src_mm = src_vma->vm_mm;
1da177e4
LT
1184 pud_t *src_pud, *dst_pud;
1185 unsigned long next;
1186
c2febafc 1187 dst_pud = pud_alloc(dst_mm, dst_p4d, addr);
1da177e4
LT
1188 if (!dst_pud)
1189 return -ENOMEM;
c2febafc 1190 src_pud = pud_offset(src_p4d, addr);
1da177e4
LT
1191 do {
1192 next = pud_addr_end(addr, end);
a00cc7d9
MW
1193 if (pud_trans_huge(*src_pud) || pud_devmap(*src_pud)) {
1194 int err;
1195
c78f4636 1196 VM_BUG_ON_VMA(next-addr != HPAGE_PUD_SIZE, src_vma);
a00cc7d9 1197 err = copy_huge_pud(dst_mm, src_mm,
c78f4636 1198 dst_pud, src_pud, addr, src_vma);
a00cc7d9
MW
1199 if (err == -ENOMEM)
1200 return -ENOMEM;
1201 if (!err)
1202 continue;
1203 /* fall through */
1204 }
1da177e4
LT
1205 if (pud_none_or_clear_bad(src_pud))
1206 continue;
c78f4636
PX
1207 if (copy_pmd_range(dst_vma, src_vma, dst_pud, src_pud,
1208 addr, next))
1da177e4
LT
1209 return -ENOMEM;
1210 } while (dst_pud++, src_pud++, addr = next, addr != end);
1211 return 0;
1212}
1213
c78f4636
PX
1214static inline int
1215copy_p4d_range(struct vm_area_struct *dst_vma, struct vm_area_struct *src_vma,
1216 pgd_t *dst_pgd, pgd_t *src_pgd, unsigned long addr,
1217 unsigned long end)
c2febafc 1218{
c78f4636 1219 struct mm_struct *dst_mm = dst_vma->vm_mm;
c2febafc
KS
1220 p4d_t *src_p4d, *dst_p4d;
1221 unsigned long next;
1222
1223 dst_p4d = p4d_alloc(dst_mm, dst_pgd, addr);
1224 if (!dst_p4d)
1225 return -ENOMEM;
1226 src_p4d = p4d_offset(src_pgd, addr);
1227 do {
1228 next = p4d_addr_end(addr, end);
1229 if (p4d_none_or_clear_bad(src_p4d))
1230 continue;
c78f4636
PX
1231 if (copy_pud_range(dst_vma, src_vma, dst_p4d, src_p4d,
1232 addr, next))
c2febafc
KS
1233 return -ENOMEM;
1234 } while (dst_p4d++, src_p4d++, addr = next, addr != end);
1235 return 0;
1236}
1237
c56d1b62
PX
1238/*
1239 * Return true if the vma needs to copy the pgtable during this fork(). Return
1240 * false when we can speed up fork() by allowing lazy page faults later until
1241 * when the child accesses the memory range.
1242 */
bc70fbf2 1243static bool
c56d1b62
PX
1244vma_needs_copy(struct vm_area_struct *dst_vma, struct vm_area_struct *src_vma)
1245{
1246 /*
1247 * Always copy pgtables when dst_vma has uffd-wp enabled even if it's
1248 * file-backed (e.g. shmem). Because when uffd-wp is enabled, pgtable
1249 * contains uffd-wp protection information, that's something we can't
1250 * retrieve from page cache, and skip copying will lose those info.
1251 */
1252 if (userfaultfd_wp(dst_vma))
1253 return true;
1254
bcd51a3c 1255 if (src_vma->vm_flags & (VM_PFNMAP | VM_MIXEDMAP))
c56d1b62
PX
1256 return true;
1257
1258 if (src_vma->anon_vma)
1259 return true;
1260
1261 /*
1262 * Don't copy ptes where a page fault will fill them correctly. Fork
1263 * becomes much lighter when there are big shared or private readonly
1264 * mappings. The tradeoff is that copy_page_range is more efficient
1265 * than faulting.
1266 */
1267 return false;
1268}
1269
c78f4636
PX
1270int
1271copy_page_range(struct vm_area_struct *dst_vma, struct vm_area_struct *src_vma)
1da177e4
LT
1272{
1273 pgd_t *src_pgd, *dst_pgd;
1274 unsigned long next;
c78f4636
PX
1275 unsigned long addr = src_vma->vm_start;
1276 unsigned long end = src_vma->vm_end;
1277 struct mm_struct *dst_mm = dst_vma->vm_mm;
1278 struct mm_struct *src_mm = src_vma->vm_mm;
ac46d4f3 1279 struct mmu_notifier_range range;
2ec74c3e 1280 bool is_cow;
cddb8a5c 1281 int ret;
1da177e4 1282
c56d1b62 1283 if (!vma_needs_copy(dst_vma, src_vma))
0661a336 1284 return 0;
d992895b 1285
c78f4636 1286 if (is_vm_hugetlb_page(src_vma))
bc70fbf2 1287 return copy_hugetlb_page_range(dst_mm, src_mm, dst_vma, src_vma);
1da177e4 1288
c78f4636 1289 if (unlikely(src_vma->vm_flags & VM_PFNMAP)) {
2ab64037 1290 /*
1291 * We do not free on error cases below as remove_vma
1292 * gets called on error from higher level routine
1293 */
c78f4636 1294 ret = track_pfn_copy(src_vma);
2ab64037 1295 if (ret)
1296 return ret;
1297 }
1298
cddb8a5c
AA
1299 /*
1300 * We need to invalidate the secondary MMU mappings only when
1301 * there could be a permission downgrade on the ptes of the
1302 * parent mm. And a permission downgrade will only happen if
1303 * is_cow_mapping() returns true.
1304 */
c78f4636 1305 is_cow = is_cow_mapping(src_vma->vm_flags);
ac46d4f3
JG
1306
1307 if (is_cow) {
7269f999 1308 mmu_notifier_range_init(&range, MMU_NOTIFY_PROTECTION_PAGE,
c78f4636 1309 0, src_vma, src_mm, addr, end);
ac46d4f3 1310 mmu_notifier_invalidate_range_start(&range);
57efa1fe
JG
1311 /*
1312 * Disabling preemption is not needed for the write side, as
1313 * the read side doesn't spin, but goes to the mmap_lock.
1314 *
1315 * Use the raw variant of the seqcount_t write API to avoid
1316 * lockdep complaining about preemptibility.
1317 */
1318 mmap_assert_write_locked(src_mm);
1319 raw_write_seqcount_begin(&src_mm->write_protect_seq);
ac46d4f3 1320 }
cddb8a5c
AA
1321
1322 ret = 0;
1da177e4
LT
1323 dst_pgd = pgd_offset(dst_mm, addr);
1324 src_pgd = pgd_offset(src_mm, addr);
1325 do {
1326 next = pgd_addr_end(addr, end);
1327 if (pgd_none_or_clear_bad(src_pgd))
1328 continue;
c78f4636
PX
1329 if (unlikely(copy_p4d_range(dst_vma, src_vma, dst_pgd, src_pgd,
1330 addr, next))) {
cddb8a5c
AA
1331 ret = -ENOMEM;
1332 break;
1333 }
1da177e4 1334 } while (dst_pgd++, src_pgd++, addr = next, addr != end);
cddb8a5c 1335
57efa1fe
JG
1336 if (is_cow) {
1337 raw_write_seqcount_end(&src_mm->write_protect_seq);
ac46d4f3 1338 mmu_notifier_invalidate_range_end(&range);
57efa1fe 1339 }
cddb8a5c 1340 return ret;
1da177e4
LT
1341}
1342
3506659e
MWO
1343/*
1344 * Parameter block passed down to zap_pte_range in exceptional cases.
1345 */
1346struct zap_details {
3506659e 1347 struct folio *single_folio; /* Locked folio to be unmapped */
2e148f1e 1348 bool even_cows; /* Zap COWed private pages too? */
999dad82 1349 zap_flags_t zap_flags; /* Extra flags for zapping */
3506659e
MWO
1350};
1351
5abfd71d
PX
1352/* Whether we should zap all COWed (private) pages too */
1353static inline bool should_zap_cows(struct zap_details *details)
1354{
1355 /* By default, zap all pages */
1356 if (!details)
1357 return true;
1358
1359 /* Or, we zap COWed pages only if the caller wants to */
2e148f1e 1360 return details->even_cows;
5abfd71d
PX
1361}
1362
2e148f1e 1363/* Decides whether we should zap this page with the page pointer specified */
254ab940 1364static inline bool should_zap_page(struct zap_details *details, struct page *page)
3506659e 1365{
5abfd71d
PX
1366 /* If we can make a decision without *page.. */
1367 if (should_zap_cows(details))
254ab940 1368 return true;
5abfd71d
PX
1369
1370 /* E.g. the caller passes NULL for the case of a zero page */
1371 if (!page)
254ab940 1372 return true;
3506659e 1373
2e148f1e
PX
1374 /* Otherwise we should only zap non-anon pages */
1375 return !PageAnon(page);
3506659e
MWO
1376}
1377
999dad82
PX
1378static inline bool zap_drop_file_uffd_wp(struct zap_details *details)
1379{
1380 if (!details)
1381 return false;
1382
1383 return details->zap_flags & ZAP_FLAG_DROP_MARKER;
1384}
1385
1386/*
1387 * This function makes sure that we'll replace the none pte with an uffd-wp
1388 * swap special pte marker when necessary. Must be with the pgtable lock held.
1389 */
1390static inline void
1391zap_install_uffd_wp_if_needed(struct vm_area_struct *vma,
1392 unsigned long addr, pte_t *pte,
1393 struct zap_details *details, pte_t pteval)
1394{
1395 if (zap_drop_file_uffd_wp(details))
1396 return;
1397
1398 pte_install_uffd_wp_if_needed(vma, addr, pte, pteval);
1399}
1400
51c6f666 1401static unsigned long zap_pte_range(struct mmu_gather *tlb,
b5810039 1402 struct vm_area_struct *vma, pmd_t *pmd,
1da177e4 1403 unsigned long addr, unsigned long end,
97a89413 1404 struct zap_details *details)
1da177e4 1405{
b5810039 1406 struct mm_struct *mm = tlb->mm;
d16dfc55 1407 int force_flush = 0;
d559db08 1408 int rss[NR_MM_COUNTERS];
97a89413 1409 spinlock_t *ptl;
5f1a1907 1410 pte_t *start_pte;
97a89413 1411 pte_t *pte;
8a5f14a2 1412 swp_entry_t entry;
d559db08 1413
ed6a7935 1414 tlb_change_page_size(tlb, PAGE_SIZE);
d16dfc55 1415again:
e303297e 1416 init_rss_vec(rss);
5f1a1907
SR
1417 start_pte = pte_offset_map_lock(mm, pmd, addr, &ptl);
1418 pte = start_pte;
3ea27719 1419 flush_tlb_batched_pending(mm);
6606c3e0 1420 arch_enter_lazy_mmu_mode();
1da177e4
LT
1421 do {
1422 pte_t ptent = *pte;
8018db85
PX
1423 struct page *page;
1424
166f61b9 1425 if (pte_none(ptent))
1da177e4 1426 continue;
6f5e6b9e 1427
7b167b68
MK
1428 if (need_resched())
1429 break;
1430
1da177e4 1431 if (pte_present(ptent)) {
25b2995a 1432 page = vm_normal_page(vma, addr, ptent);
254ab940 1433 if (unlikely(!should_zap_page(details, page)))
91b61ef3 1434 continue;
b5810039 1435 ptent = ptep_get_and_clear_full(mm, addr, pte,
a600388d 1436 tlb->fullmm);
1da177e4 1437 tlb_remove_tlb_entry(tlb, pte, addr);
999dad82
PX
1438 zap_install_uffd_wp_if_needed(vma, addr, pte, details,
1439 ptent);
1da177e4
LT
1440 if (unlikely(!page))
1441 continue;
eca56ff9
JM
1442
1443 if (!PageAnon(page)) {
1cf35d47
LT
1444 if (pte_dirty(ptent)) {
1445 force_flush = 1;
6237bcd9 1446 set_page_dirty(page);
1cf35d47 1447 }
4917e5d0 1448 if (pte_young(ptent) &&
64363aad 1449 likely(!(vma->vm_flags & VM_SEQ_READ)))
bf3f3bc5 1450 mark_page_accessed(page);
6237bcd9 1451 }
eca56ff9 1452 rss[mm_counter(page)]--;
cea86fe2 1453 page_remove_rmap(page, vma, false);
3dc14741
HD
1454 if (unlikely(page_mapcount(page) < 0))
1455 print_bad_pte(vma, addr, ptent, page);
e9d55e15 1456 if (unlikely(__tlb_remove_page(tlb, page))) {
1cf35d47 1457 force_flush = 1;
ce9ec37b 1458 addr += PAGE_SIZE;
d16dfc55 1459 break;
1cf35d47 1460 }
1da177e4
LT
1461 continue;
1462 }
5042db43
JG
1463
1464 entry = pte_to_swp_entry(ptent);
b756a3b5
AP
1465 if (is_device_private_entry(entry) ||
1466 is_device_exclusive_entry(entry)) {
8018db85 1467 page = pfn_swap_entry_to_page(entry);
254ab940 1468 if (unlikely(!should_zap_page(details, page)))
91b61ef3 1469 continue;
999dad82
PX
1470 /*
1471 * Both device private/exclusive mappings should only
1472 * work with anonymous page so far, so we don't need to
1473 * consider uffd-wp bit when zap. For more information,
1474 * see zap_install_uffd_wp_if_needed().
1475 */
1476 WARN_ON_ONCE(!vma_is_anonymous(vma));
5042db43 1477 rss[mm_counter(page)]--;
b756a3b5 1478 if (is_device_private_entry(entry))
cea86fe2 1479 page_remove_rmap(page, vma, false);
5042db43 1480 put_page(page);
8018db85 1481 } else if (!non_swap_entry(entry)) {
5abfd71d
PX
1482 /* Genuine swap entry, hence a private anon page */
1483 if (!should_zap_cows(details))
1484 continue;
8a5f14a2 1485 rss[MM_SWAPENTS]--;
8018db85
PX
1486 if (unlikely(!free_swap_and_cache(entry)))
1487 print_bad_pte(vma, addr, ptent, NULL);
5abfd71d 1488 } else if (is_migration_entry(entry)) {
af5cdaf8 1489 page = pfn_swap_entry_to_page(entry);
254ab940 1490 if (!should_zap_page(details, page))
5abfd71d 1491 continue;
eca56ff9 1492 rss[mm_counter(page)]--;
999dad82
PX
1493 } else if (pte_marker_entry_uffd_wp(entry)) {
1494 /* Only drop the uffd-wp marker if explicitly requested */
1495 if (!zap_drop_file_uffd_wp(details))
1496 continue;
9f186f9e
ML
1497 } else if (is_hwpoison_entry(entry) ||
1498 is_swapin_error_entry(entry)) {
5abfd71d
PX
1499 if (!should_zap_cows(details))
1500 continue;
1501 } else {
1502 /* We should have covered all the swap entry types */
1503 WARN_ON_ONCE(1);
b084d435 1504 }
9888a1ca 1505 pte_clear_not_present_full(mm, addr, pte, tlb->fullmm);
999dad82 1506 zap_install_uffd_wp_if_needed(vma, addr, pte, details, ptent);
97a89413 1507 } while (pte++, addr += PAGE_SIZE, addr != end);
ae859762 1508
d559db08 1509 add_mm_rss_vec(mm, rss);
6606c3e0 1510 arch_leave_lazy_mmu_mode();
51c6f666 1511
1cf35d47 1512 /* Do the actual TLB flush before dropping ptl */
fb7332a9 1513 if (force_flush)
1cf35d47 1514 tlb_flush_mmu_tlbonly(tlb);
1cf35d47
LT
1515 pte_unmap_unlock(start_pte, ptl);
1516
1517 /*
1518 * If we forced a TLB flush (either due to running out of
1519 * batch buffers or because we needed to flush dirty TLB
1520 * entries before releasing the ptl), free the batched
1521 * memory too. Restart if we didn't do everything.
1522 */
1523 if (force_flush) {
1524 force_flush = 0;
fa0aafb8 1525 tlb_flush_mmu(tlb);
7b167b68
MK
1526 }
1527
1528 if (addr != end) {
1529 cond_resched();
1530 goto again;
d16dfc55
PZ
1531 }
1532
51c6f666 1533 return addr;
1da177e4
LT
1534}
1535
51c6f666 1536static inline unsigned long zap_pmd_range(struct mmu_gather *tlb,
b5810039 1537 struct vm_area_struct *vma, pud_t *pud,
1da177e4 1538 unsigned long addr, unsigned long end,
97a89413 1539 struct zap_details *details)
1da177e4
LT
1540{
1541 pmd_t *pmd;
1542 unsigned long next;
1543
1544 pmd = pmd_offset(pud, addr);
1545 do {
1546 next = pmd_addr_end(addr, end);
84c3fc4e 1547 if (is_swap_pmd(*pmd) || pmd_trans_huge(*pmd) || pmd_devmap(*pmd)) {
53406ed1 1548 if (next - addr != HPAGE_PMD_SIZE)
fd60775a 1549 __split_huge_pmd(vma, pmd, addr, false, NULL);
53406ed1 1550 else if (zap_huge_pmd(tlb, vma, pmd, addr))
1a5a9906 1551 goto next;
71e3aac0 1552 /* fall through */
3506659e
MWO
1553 } else if (details && details->single_folio &&
1554 folio_test_pmd_mappable(details->single_folio) &&
22061a1f
HD
1555 next - addr == HPAGE_PMD_SIZE && pmd_none(*pmd)) {
1556 spinlock_t *ptl = pmd_lock(tlb->mm, pmd);
1557 /*
1558 * Take and drop THP pmd lock so that we cannot return
1559 * prematurely, while zap_huge_pmd() has cleared *pmd,
1560 * but not yet decremented compound_mapcount().
1561 */
1562 spin_unlock(ptl);
71e3aac0 1563 }
22061a1f 1564
1a5a9906
AA
1565 /*
1566 * Here there can be other concurrent MADV_DONTNEED or
1567 * trans huge page faults running, and if the pmd is
1568 * none or trans huge it can change under us. This is
c1e8d7c6 1569 * because MADV_DONTNEED holds the mmap_lock in read
1a5a9906
AA
1570 * mode.
1571 */
1572 if (pmd_none_or_trans_huge_or_clear_bad(pmd))
1573 goto next;
97a89413 1574 next = zap_pte_range(tlb, vma, pmd, addr, next, details);
1a5a9906 1575next:
97a89413
PZ
1576 cond_resched();
1577 } while (pmd++, addr = next, addr != end);
51c6f666
RH
1578
1579 return addr;
1da177e4
LT
1580}
1581
51c6f666 1582static inline unsigned long zap_pud_range(struct mmu_gather *tlb,
c2febafc 1583 struct vm_area_struct *vma, p4d_t *p4d,
1da177e4 1584 unsigned long addr, unsigned long end,
97a89413 1585 struct zap_details *details)
1da177e4
LT
1586{
1587 pud_t *pud;
1588 unsigned long next;
1589
c2febafc 1590 pud = pud_offset(p4d, addr);
1da177e4
LT
1591 do {
1592 next = pud_addr_end(addr, end);
a00cc7d9
MW
1593 if (pud_trans_huge(*pud) || pud_devmap(*pud)) {
1594 if (next - addr != HPAGE_PUD_SIZE) {
42fc5414 1595 mmap_assert_locked(tlb->mm);
a00cc7d9
MW
1596 split_huge_pud(vma, pud, addr);
1597 } else if (zap_huge_pud(tlb, vma, pud, addr))
1598 goto next;
1599 /* fall through */
1600 }
97a89413 1601 if (pud_none_or_clear_bad(pud))
1da177e4 1602 continue;
97a89413 1603 next = zap_pmd_range(tlb, vma, pud, addr, next, details);
a00cc7d9
MW
1604next:
1605 cond_resched();
97a89413 1606 } while (pud++, addr = next, addr != end);
51c6f666
RH
1607
1608 return addr;
1da177e4
LT
1609}
1610
c2febafc
KS
1611static inline unsigned long zap_p4d_range(struct mmu_gather *tlb,
1612 struct vm_area_struct *vma, pgd_t *pgd,
1613 unsigned long addr, unsigned long end,
1614 struct zap_details *details)
1615{
1616 p4d_t *p4d;
1617 unsigned long next;
1618
1619 p4d = p4d_offset(pgd, addr);
1620 do {
1621 next = p4d_addr_end(addr, end);
1622 if (p4d_none_or_clear_bad(p4d))
1623 continue;
1624 next = zap_pud_range(tlb, vma, p4d, addr, next, details);
1625 } while (p4d++, addr = next, addr != end);
1626
1627 return addr;
1628}
1629
aac45363 1630void unmap_page_range(struct mmu_gather *tlb,
038c7aa1
AV
1631 struct vm_area_struct *vma,
1632 unsigned long addr, unsigned long end,
1633 struct zap_details *details)
1da177e4
LT
1634{
1635 pgd_t *pgd;
1636 unsigned long next;
1637
1da177e4
LT
1638 BUG_ON(addr >= end);
1639 tlb_start_vma(tlb, vma);
1640 pgd = pgd_offset(vma->vm_mm, addr);
1641 do {
1642 next = pgd_addr_end(addr, end);
97a89413 1643 if (pgd_none_or_clear_bad(pgd))
1da177e4 1644 continue;
c2febafc 1645 next = zap_p4d_range(tlb, vma, pgd, addr, next, details);
97a89413 1646 } while (pgd++, addr = next, addr != end);
1da177e4
LT
1647 tlb_end_vma(tlb, vma);
1648}
51c6f666 1649
f5cc4eef
AV
1650
1651static void unmap_single_vma(struct mmu_gather *tlb,
1652 struct vm_area_struct *vma, unsigned long start_addr,
4f74d2c8 1653 unsigned long end_addr,
f5cc4eef
AV
1654 struct zap_details *details)
1655{
1656 unsigned long start = max(vma->vm_start, start_addr);
1657 unsigned long end;
1658
1659 if (start >= vma->vm_end)
1660 return;
1661 end = min(vma->vm_end, end_addr);
1662 if (end <= vma->vm_start)
1663 return;
1664
cbc91f71
SD
1665 if (vma->vm_file)
1666 uprobe_munmap(vma, start, end);
1667
b3b9c293 1668 if (unlikely(vma->vm_flags & VM_PFNMAP))
5180da41 1669 untrack_pfn(vma, 0, 0);
f5cc4eef
AV
1670
1671 if (start != end) {
1672 if (unlikely(is_vm_hugetlb_page(vma))) {
1673 /*
1674 * It is undesirable to test vma->vm_file as it
1675 * should be non-null for valid hugetlb area.
1676 * However, vm_file will be NULL in the error
7aa6b4ad 1677 * cleanup path of mmap_region. When
f5cc4eef 1678 * hugetlbfs ->mmap method fails,
7aa6b4ad 1679 * mmap_region() nullifies vma->vm_file
f5cc4eef
AV
1680 * before calling this function to clean up.
1681 * Since no pte has actually been setup, it is
1682 * safe to do nothing in this case.
1683 */
24669e58 1684 if (vma->vm_file) {
05e90bd0
PX
1685 zap_flags_t zap_flags = details ?
1686 details->zap_flags : 0;
83cde9e8 1687 i_mmap_lock_write(vma->vm_file->f_mapping);
05e90bd0
PX
1688 __unmap_hugepage_range_final(tlb, vma, start, end,
1689 NULL, zap_flags);
83cde9e8 1690 i_mmap_unlock_write(vma->vm_file->f_mapping);
24669e58 1691 }
f5cc4eef
AV
1692 } else
1693 unmap_page_range(tlb, vma, start, end, details);
1694 }
1da177e4
LT
1695}
1696
1da177e4
LT
1697/**
1698 * unmap_vmas - unmap a range of memory covered by a list of vma's
0164f69d 1699 * @tlb: address of the caller's struct mmu_gather
763ecb03 1700 * @mt: the maple tree
1da177e4
LT
1701 * @vma: the starting vma
1702 * @start_addr: virtual address at which to start unmapping
1703 * @end_addr: virtual address at which to end unmapping
1da177e4 1704 *
508034a3 1705 * Unmap all pages in the vma list.
1da177e4 1706 *
1da177e4
LT
1707 * Only addresses between `start' and `end' will be unmapped.
1708 *
1709 * The VMA list must be sorted in ascending virtual address order.
1710 *
1711 * unmap_vmas() assumes that the caller will flush the whole unmapped address
1712 * range after unmap_vmas() returns. So the only responsibility here is to
1713 * ensure that any thus-far unmapped pages are flushed before unmap_vmas()
1714 * drops the lock and schedules.
1715 */
763ecb03 1716void unmap_vmas(struct mmu_gather *tlb, struct maple_tree *mt,
1da177e4 1717 struct vm_area_struct *vma, unsigned long start_addr,
4f74d2c8 1718 unsigned long end_addr)
1da177e4 1719{
ac46d4f3 1720 struct mmu_notifier_range range;
999dad82
PX
1721 struct zap_details details = {
1722 .zap_flags = ZAP_FLAG_DROP_MARKER,
1723 /* Careful - we need to zap private pages too! */
1724 .even_cows = true,
1725 };
763ecb03 1726 MA_STATE(mas, mt, vma->vm_end, vma->vm_end);
1da177e4 1727
6f4f13e8
JG
1728 mmu_notifier_range_init(&range, MMU_NOTIFY_UNMAP, 0, vma, vma->vm_mm,
1729 start_addr, end_addr);
ac46d4f3 1730 mmu_notifier_invalidate_range_start(&range);
763ecb03 1731 do {
999dad82 1732 unmap_single_vma(tlb, vma, start_addr, end_addr, &details);
763ecb03 1733 } while ((vma = mas_find(&mas, end_addr - 1)) != NULL);
ac46d4f3 1734 mmu_notifier_invalidate_range_end(&range);
1da177e4
LT
1735}
1736
1737/**
1738 * zap_page_range - remove user pages in a given range
1739 * @vma: vm_area_struct holding the applicable pages
eb4546bb 1740 * @start: starting address of pages to zap
1da177e4 1741 * @size: number of bytes to zap
f5cc4eef
AV
1742 *
1743 * Caller must protect the VMA list
1da177e4 1744 */
7e027b14 1745void zap_page_range(struct vm_area_struct *vma, unsigned long start,
ecf1385d 1746 unsigned long size)
1da177e4 1747{
763ecb03
LH
1748 struct maple_tree *mt = &vma->vm_mm->mm_mt;
1749 unsigned long end = start + size;
ac46d4f3 1750 struct mmu_notifier_range range;
d16dfc55 1751 struct mmu_gather tlb;
763ecb03 1752 MA_STATE(mas, mt, vma->vm_end, vma->vm_end);
1da177e4 1753
1da177e4 1754 lru_add_drain();
7269f999 1755 mmu_notifier_range_init(&range, MMU_NOTIFY_CLEAR, 0, vma, vma->vm_mm,
6f4f13e8 1756 start, start + size);
a72afd87 1757 tlb_gather_mmu(&tlb, vma->vm_mm);
ac46d4f3
JG
1758 update_hiwater_rss(vma->vm_mm);
1759 mmu_notifier_invalidate_range_start(&range);
763ecb03 1760 do {
ac46d4f3 1761 unmap_single_vma(&tlb, vma, start, range.end, NULL);
763ecb03 1762 } while ((vma = mas_find(&mas, end - 1)) != NULL);
ac46d4f3 1763 mmu_notifier_invalidate_range_end(&range);
ae8eba8b 1764 tlb_finish_mmu(&tlb);
1da177e4
LT
1765}
1766
f5cc4eef
AV
1767/**
1768 * zap_page_range_single - remove user pages in a given range
1769 * @vma: vm_area_struct holding the applicable pages
1770 * @address: starting address of pages to zap
1771 * @size: number of bytes to zap
8a5f14a2 1772 * @details: details of shared cache invalidation
f5cc4eef
AV
1773 *
1774 * The range must fit into one VMA.
1da177e4 1775 */
f5cc4eef 1776static void zap_page_range_single(struct vm_area_struct *vma, unsigned long address,
1da177e4
LT
1777 unsigned long size, struct zap_details *details)
1778{
ac46d4f3 1779 struct mmu_notifier_range range;
d16dfc55 1780 struct mmu_gather tlb;
1da177e4 1781
1da177e4 1782 lru_add_drain();
7269f999 1783 mmu_notifier_range_init(&range, MMU_NOTIFY_CLEAR, 0, vma, vma->vm_mm,
6f4f13e8 1784 address, address + size);
a72afd87 1785 tlb_gather_mmu(&tlb, vma->vm_mm);
ac46d4f3
JG
1786 update_hiwater_rss(vma->vm_mm);
1787 mmu_notifier_invalidate_range_start(&range);
1788 unmap_single_vma(&tlb, vma, address, range.end, details);
1789 mmu_notifier_invalidate_range_end(&range);
ae8eba8b 1790 tlb_finish_mmu(&tlb);
1da177e4
LT
1791}
1792
c627f9cc
JS
1793/**
1794 * zap_vma_ptes - remove ptes mapping the vma
1795 * @vma: vm_area_struct holding ptes to be zapped
1796 * @address: starting address of pages to zap
1797 * @size: number of bytes to zap
1798 *
1799 * This function only unmaps ptes assigned to VM_PFNMAP vmas.
1800 *
1801 * The entire address range must be fully contained within the vma.
1802 *
c627f9cc 1803 */
27d036e3 1804void zap_vma_ptes(struct vm_area_struct *vma, unsigned long address,
c627f9cc
JS
1805 unsigned long size)
1806{
88a35912 1807 if (!range_in_vma(vma, address, address + size) ||
c627f9cc 1808 !(vma->vm_flags & VM_PFNMAP))
27d036e3
LR
1809 return;
1810
f5cc4eef 1811 zap_page_range_single(vma, address, size, NULL);
c627f9cc
JS
1812}
1813EXPORT_SYMBOL_GPL(zap_vma_ptes);
1814
8cd3984d 1815static pmd_t *walk_to_pmd(struct mm_struct *mm, unsigned long addr)
c9cfcddf 1816{
c2febafc
KS
1817 pgd_t *pgd;
1818 p4d_t *p4d;
1819 pud_t *pud;
1820 pmd_t *pmd;
1821
1822 pgd = pgd_offset(mm, addr);
1823 p4d = p4d_alloc(mm, pgd, addr);
1824 if (!p4d)
1825 return NULL;
1826 pud = pud_alloc(mm, p4d, addr);
1827 if (!pud)
1828 return NULL;
1829 pmd = pmd_alloc(mm, pud, addr);
1830 if (!pmd)
1831 return NULL;
1832
1833 VM_BUG_ON(pmd_trans_huge(*pmd));
8cd3984d
AR
1834 return pmd;
1835}
1836
1837pte_t *__get_locked_pte(struct mm_struct *mm, unsigned long addr,
1838 spinlock_t **ptl)
1839{
1840 pmd_t *pmd = walk_to_pmd(mm, addr);
1841
1842 if (!pmd)
1843 return NULL;
c2febafc 1844 return pte_alloc_map_lock(mm, pmd, addr, ptl);
c9cfcddf
LT
1845}
1846
8efd6f5b
AR
1847static int validate_page_before_insert(struct page *page)
1848{
1849 if (PageAnon(page) || PageSlab(page) || page_has_type(page))
1850 return -EINVAL;
1851 flush_dcache_page(page);
1852 return 0;
1853}
1854
cea86fe2 1855static int insert_page_into_pte_locked(struct vm_area_struct *vma, pte_t *pte,
8efd6f5b
AR
1856 unsigned long addr, struct page *page, pgprot_t prot)
1857{
1858 if (!pte_none(*pte))
1859 return -EBUSY;
1860 /* Ok, finally just insert the thing.. */
1861 get_page(page);
cea86fe2
HD
1862 inc_mm_counter_fast(vma->vm_mm, mm_counter_file(page));
1863 page_add_file_rmap(page, vma, false);
1864 set_pte_at(vma->vm_mm, addr, pte, mk_pte(page, prot));
8efd6f5b
AR
1865 return 0;
1866}
1867
238f58d8
LT
1868/*
1869 * This is the old fallback for page remapping.
1870 *
1871 * For historical reasons, it only allows reserved pages. Only
1872 * old drivers should use this, and they needed to mark their
1873 * pages reserved for the old functions anyway.
1874 */
423bad60
NP
1875static int insert_page(struct vm_area_struct *vma, unsigned long addr,
1876 struct page *page, pgprot_t prot)
238f58d8
LT
1877{
1878 int retval;
c9cfcddf 1879 pte_t *pte;
8a9f3ccd
BS
1880 spinlock_t *ptl;
1881
8efd6f5b
AR
1882 retval = validate_page_before_insert(page);
1883 if (retval)
5b4e655e 1884 goto out;
238f58d8 1885 retval = -ENOMEM;
cea86fe2 1886 pte = get_locked_pte(vma->vm_mm, addr, &ptl);
238f58d8 1887 if (!pte)
5b4e655e 1888 goto out;
cea86fe2 1889 retval = insert_page_into_pte_locked(vma, pte, addr, page, prot);
238f58d8
LT
1890 pte_unmap_unlock(pte, ptl);
1891out:
1892 return retval;
1893}
1894
8cd3984d 1895#ifdef pte_index
cea86fe2 1896static int insert_page_in_batch_locked(struct vm_area_struct *vma, pte_t *pte,
8cd3984d
AR
1897 unsigned long addr, struct page *page, pgprot_t prot)
1898{
1899 int err;
1900
1901 if (!page_count(page))
1902 return -EINVAL;
1903 err = validate_page_before_insert(page);
7f70c2a6
AR
1904 if (err)
1905 return err;
cea86fe2 1906 return insert_page_into_pte_locked(vma, pte, addr, page, prot);
8cd3984d
AR
1907}
1908
1909/* insert_pages() amortizes the cost of spinlock operations
1910 * when inserting pages in a loop. Arch *must* define pte_index.
1911 */
1912static int insert_pages(struct vm_area_struct *vma, unsigned long addr,
1913 struct page **pages, unsigned long *num, pgprot_t prot)
1914{
1915 pmd_t *pmd = NULL;
7f70c2a6
AR
1916 pte_t *start_pte, *pte;
1917 spinlock_t *pte_lock;
8cd3984d
AR
1918 struct mm_struct *const mm = vma->vm_mm;
1919 unsigned long curr_page_idx = 0;
1920 unsigned long remaining_pages_total = *num;
1921 unsigned long pages_to_write_in_pmd;
1922 int ret;
1923more:
1924 ret = -EFAULT;
1925 pmd = walk_to_pmd(mm, addr);
1926 if (!pmd)
1927 goto out;
1928
1929 pages_to_write_in_pmd = min_t(unsigned long,
1930 remaining_pages_total, PTRS_PER_PTE - pte_index(addr));
1931
1932 /* Allocate the PTE if necessary; takes PMD lock once only. */
1933 ret = -ENOMEM;
1934 if (pte_alloc(mm, pmd))
1935 goto out;
8cd3984d
AR
1936
1937 while (pages_to_write_in_pmd) {
1938 int pte_idx = 0;
1939 const int batch_size = min_t(int, pages_to_write_in_pmd, 8);
1940
7f70c2a6
AR
1941 start_pte = pte_offset_map_lock(mm, pmd, addr, &pte_lock);
1942 for (pte = start_pte; pte_idx < batch_size; ++pte, ++pte_idx) {
cea86fe2 1943 int err = insert_page_in_batch_locked(vma, pte,
8cd3984d
AR
1944 addr, pages[curr_page_idx], prot);
1945 if (unlikely(err)) {
7f70c2a6 1946 pte_unmap_unlock(start_pte, pte_lock);
8cd3984d
AR
1947 ret = err;
1948 remaining_pages_total -= pte_idx;
1949 goto out;
1950 }
1951 addr += PAGE_SIZE;
1952 ++curr_page_idx;
1953 }
7f70c2a6 1954 pte_unmap_unlock(start_pte, pte_lock);
8cd3984d
AR
1955 pages_to_write_in_pmd -= batch_size;
1956 remaining_pages_total -= batch_size;
1957 }
1958 if (remaining_pages_total)
1959 goto more;
1960 ret = 0;
1961out:
1962 *num = remaining_pages_total;
1963 return ret;
1964}
1965#endif /* ifdef pte_index */
1966
1967/**
1968 * vm_insert_pages - insert multiple pages into user vma, batching the pmd lock.
1969 * @vma: user vma to map to
1970 * @addr: target start user address of these pages
1971 * @pages: source kernel pages
1972 * @num: in: number of pages to map. out: number of pages that were *not*
1973 * mapped. (0 means all pages were successfully mapped).
1974 *
1975 * Preferred over vm_insert_page() when inserting multiple pages.
1976 *
1977 * In case of error, we may have mapped a subset of the provided
1978 * pages. It is the caller's responsibility to account for this case.
1979 *
1980 * The same restrictions apply as in vm_insert_page().
1981 */
1982int vm_insert_pages(struct vm_area_struct *vma, unsigned long addr,
1983 struct page **pages, unsigned long *num)
1984{
1985#ifdef pte_index
1986 const unsigned long end_addr = addr + (*num * PAGE_SIZE) - 1;
1987
1988 if (addr < vma->vm_start || end_addr >= vma->vm_end)
1989 return -EFAULT;
1990 if (!(vma->vm_flags & VM_MIXEDMAP)) {
d8ed45c5 1991 BUG_ON(mmap_read_trylock(vma->vm_mm));
8cd3984d
AR
1992 BUG_ON(vma->vm_flags & VM_PFNMAP);
1993 vma->vm_flags |= VM_MIXEDMAP;
1994 }
1995 /* Defer page refcount checking till we're about to map that page. */
1996 return insert_pages(vma, addr, pages, num, vma->vm_page_prot);
1997#else
1998 unsigned long idx = 0, pgcount = *num;
45779b03 1999 int err = -EINVAL;
8cd3984d
AR
2000
2001 for (; idx < pgcount; ++idx) {
2002 err = vm_insert_page(vma, addr + (PAGE_SIZE * idx), pages[idx]);
2003 if (err)
2004 break;
2005 }
2006 *num = pgcount - idx;
2007 return err;
2008#endif /* ifdef pte_index */
2009}
2010EXPORT_SYMBOL(vm_insert_pages);
2011
bfa5bf6d
REB
2012/**
2013 * vm_insert_page - insert single page into user vma
2014 * @vma: user vma to map to
2015 * @addr: target user address of this page
2016 * @page: source kernel page
2017 *
a145dd41
LT
2018 * This allows drivers to insert individual pages they've allocated
2019 * into a user vma.
2020 *
2021 * The page has to be a nice clean _individual_ kernel allocation.
2022 * If you allocate a compound page, you need to have marked it as
2023 * such (__GFP_COMP), or manually just split the page up yourself
8dfcc9ba 2024 * (see split_page()).
a145dd41
LT
2025 *
2026 * NOTE! Traditionally this was done with "remap_pfn_range()" which
2027 * took an arbitrary page protection parameter. This doesn't allow
2028 * that. Your vma protection will have to be set up correctly, which
2029 * means that if you want a shared writable mapping, you'd better
2030 * ask for a shared writable mapping!
2031 *
2032 * The page does not need to be reserved.
4b6e1e37
KK
2033 *
2034 * Usually this function is called from f_op->mmap() handler
c1e8d7c6 2035 * under mm->mmap_lock write-lock, so it can change vma->vm_flags.
4b6e1e37
KK
2036 * Caller must set VM_MIXEDMAP on vma if it wants to call this
2037 * function from other places, for example from page-fault handler.
a862f68a
MR
2038 *
2039 * Return: %0 on success, negative error code otherwise.
a145dd41 2040 */
423bad60
NP
2041int vm_insert_page(struct vm_area_struct *vma, unsigned long addr,
2042 struct page *page)
a145dd41
LT
2043{
2044 if (addr < vma->vm_start || addr >= vma->vm_end)
2045 return -EFAULT;
2046 if (!page_count(page))
2047 return -EINVAL;
4b6e1e37 2048 if (!(vma->vm_flags & VM_MIXEDMAP)) {
d8ed45c5 2049 BUG_ON(mmap_read_trylock(vma->vm_mm));
4b6e1e37
KK
2050 BUG_ON(vma->vm_flags & VM_PFNMAP);
2051 vma->vm_flags |= VM_MIXEDMAP;
2052 }
423bad60 2053 return insert_page(vma, addr, page, vma->vm_page_prot);
a145dd41 2054}
e3c3374f 2055EXPORT_SYMBOL(vm_insert_page);
a145dd41 2056
a667d745
SJ
2057/*
2058 * __vm_map_pages - maps range of kernel pages into user vma
2059 * @vma: user vma to map to
2060 * @pages: pointer to array of source kernel pages
2061 * @num: number of pages in page array
2062 * @offset: user's requested vm_pgoff
2063 *
2064 * This allows drivers to map range of kernel pages into a user vma.
2065 *
2066 * Return: 0 on success and error code otherwise.
2067 */
2068static int __vm_map_pages(struct vm_area_struct *vma, struct page **pages,
2069 unsigned long num, unsigned long offset)
2070{
2071 unsigned long count = vma_pages(vma);
2072 unsigned long uaddr = vma->vm_start;
2073 int ret, i;
2074
2075 /* Fail if the user requested offset is beyond the end of the object */
96756fcb 2076 if (offset >= num)
a667d745
SJ
2077 return -ENXIO;
2078
2079 /* Fail if the user requested size exceeds available object size */
2080 if (count > num - offset)
2081 return -ENXIO;
2082
2083 for (i = 0; i < count; i++) {
2084 ret = vm_insert_page(vma, uaddr, pages[offset + i]);
2085 if (ret < 0)
2086 return ret;
2087 uaddr += PAGE_SIZE;
2088 }
2089
2090 return 0;
2091}
2092
2093/**
2094 * vm_map_pages - maps range of kernel pages starts with non zero offset
2095 * @vma: user vma to map to
2096 * @pages: pointer to array of source kernel pages
2097 * @num: number of pages in page array
2098 *
2099 * Maps an object consisting of @num pages, catering for the user's
2100 * requested vm_pgoff
2101 *
2102 * If we fail to insert any page into the vma, the function will return
2103 * immediately leaving any previously inserted pages present. Callers
2104 * from the mmap handler may immediately return the error as their caller
2105 * will destroy the vma, removing any successfully inserted pages. Other
2106 * callers should make their own arrangements for calling unmap_region().
2107 *
2108 * Context: Process context. Called by mmap handlers.
2109 * Return: 0 on success and error code otherwise.
2110 */
2111int vm_map_pages(struct vm_area_struct *vma, struct page **pages,
2112 unsigned long num)
2113{
2114 return __vm_map_pages(vma, pages, num, vma->vm_pgoff);
2115}
2116EXPORT_SYMBOL(vm_map_pages);
2117
2118/**
2119 * vm_map_pages_zero - map range of kernel pages starts with zero offset
2120 * @vma: user vma to map to
2121 * @pages: pointer to array of source kernel pages
2122 * @num: number of pages in page array
2123 *
2124 * Similar to vm_map_pages(), except that it explicitly sets the offset
2125 * to 0. This function is intended for the drivers that did not consider
2126 * vm_pgoff.
2127 *
2128 * Context: Process context. Called by mmap handlers.
2129 * Return: 0 on success and error code otherwise.
2130 */
2131int vm_map_pages_zero(struct vm_area_struct *vma, struct page **pages,
2132 unsigned long num)
2133{
2134 return __vm_map_pages(vma, pages, num, 0);
2135}
2136EXPORT_SYMBOL(vm_map_pages_zero);
2137
9b5a8e00 2138static vm_fault_t insert_pfn(struct vm_area_struct *vma, unsigned long addr,
b2770da6 2139 pfn_t pfn, pgprot_t prot, bool mkwrite)
423bad60
NP
2140{
2141 struct mm_struct *mm = vma->vm_mm;
423bad60
NP
2142 pte_t *pte, entry;
2143 spinlock_t *ptl;
2144
423bad60
NP
2145 pte = get_locked_pte(mm, addr, &ptl);
2146 if (!pte)
9b5a8e00 2147 return VM_FAULT_OOM;
b2770da6
RZ
2148 if (!pte_none(*pte)) {
2149 if (mkwrite) {
2150 /*
2151 * For read faults on private mappings the PFN passed
2152 * in may not match the PFN we have mapped if the
2153 * mapped PFN is a writeable COW page. In the mkwrite
2154 * case we are creating a writable PTE for a shared
f2c57d91
JK
2155 * mapping and we expect the PFNs to match. If they
2156 * don't match, we are likely racing with block
2157 * allocation and mapping invalidation so just skip the
2158 * update.
b2770da6 2159 */
f2c57d91
JK
2160 if (pte_pfn(*pte) != pfn_t_to_pfn(pfn)) {
2161 WARN_ON_ONCE(!is_zero_pfn(pte_pfn(*pte)));
b2770da6 2162 goto out_unlock;
f2c57d91 2163 }
cae85cb8
JK
2164 entry = pte_mkyoung(*pte);
2165 entry = maybe_mkwrite(pte_mkdirty(entry), vma);
2166 if (ptep_set_access_flags(vma, addr, pte, entry, 1))
2167 update_mmu_cache(vma, addr, pte);
2168 }
2169 goto out_unlock;
b2770da6 2170 }
423bad60
NP
2171
2172 /* Ok, finally just insert the thing.. */
01c8f1c4
DW
2173 if (pfn_t_devmap(pfn))
2174 entry = pte_mkdevmap(pfn_t_pte(pfn, prot));
2175 else
2176 entry = pte_mkspecial(pfn_t_pte(pfn, prot));
b2770da6 2177
b2770da6
RZ
2178 if (mkwrite) {
2179 entry = pte_mkyoung(entry);
2180 entry = maybe_mkwrite(pte_mkdirty(entry), vma);
2181 }
2182
423bad60 2183 set_pte_at(mm, addr, pte, entry);
4b3073e1 2184 update_mmu_cache(vma, addr, pte); /* XXX: why not for insert_page? */
423bad60 2185
423bad60
NP
2186out_unlock:
2187 pte_unmap_unlock(pte, ptl);
9b5a8e00 2188 return VM_FAULT_NOPAGE;
423bad60
NP
2189}
2190
f5e6d1d5
MW
2191/**
2192 * vmf_insert_pfn_prot - insert single pfn into user vma with specified pgprot
2193 * @vma: user vma to map to
2194 * @addr: target user address of this page
2195 * @pfn: source kernel pfn
2196 * @pgprot: pgprot flags for the inserted page
2197 *
a1a0aea5 2198 * This is exactly like vmf_insert_pfn(), except that it allows drivers
f5e6d1d5
MW
2199 * to override pgprot on a per-page basis.
2200 *
2201 * This only makes sense for IO mappings, and it makes no sense for
2202 * COW mappings. In general, using multiple vmas is preferable;
ae2b01f3 2203 * vmf_insert_pfn_prot should only be used if using multiple VMAs is
f5e6d1d5
MW
2204 * impractical.
2205 *
574c5b3d
TH
2206 * See vmf_insert_mixed_prot() for a discussion of the implication of using
2207 * a value of @pgprot different from that of @vma->vm_page_prot.
2208 *
ae2b01f3 2209 * Context: Process context. May allocate using %GFP_KERNEL.
f5e6d1d5
MW
2210 * Return: vm_fault_t value.
2211 */
2212vm_fault_t vmf_insert_pfn_prot(struct vm_area_struct *vma, unsigned long addr,
2213 unsigned long pfn, pgprot_t pgprot)
2214{
6d958546
MW
2215 /*
2216 * Technically, architectures with pte_special can avoid all these
2217 * restrictions (same for remap_pfn_range). However we would like
2218 * consistency in testing and feature parity among all, so we should
2219 * try to keep these invariants in place for everybody.
2220 */
2221 BUG_ON(!(vma->vm_flags & (VM_PFNMAP|VM_MIXEDMAP)));
2222 BUG_ON((vma->vm_flags & (VM_PFNMAP|VM_MIXEDMAP)) ==
2223 (VM_PFNMAP|VM_MIXEDMAP));
2224 BUG_ON((vma->vm_flags & VM_PFNMAP) && is_cow_mapping(vma->vm_flags));
2225 BUG_ON((vma->vm_flags & VM_MIXEDMAP) && pfn_valid(pfn));
2226
2227 if (addr < vma->vm_start || addr >= vma->vm_end)
2228 return VM_FAULT_SIGBUS;
2229
2230 if (!pfn_modify_allowed(pfn, pgprot))
2231 return VM_FAULT_SIGBUS;
2232
2233 track_pfn_insert(vma, &pgprot, __pfn_to_pfn_t(pfn, PFN_DEV));
2234
9b5a8e00 2235 return insert_pfn(vma, addr, __pfn_to_pfn_t(pfn, PFN_DEV), pgprot,
6d958546 2236 false);
f5e6d1d5
MW
2237}
2238EXPORT_SYMBOL(vmf_insert_pfn_prot);
e0dc0d8f 2239
ae2b01f3
MW
2240/**
2241 * vmf_insert_pfn - insert single pfn into user vma
2242 * @vma: user vma to map to
2243 * @addr: target user address of this page
2244 * @pfn: source kernel pfn
2245 *
2246 * Similar to vm_insert_page, this allows drivers to insert individual pages
2247 * they've allocated into a user vma. Same comments apply.
2248 *
2249 * This function should only be called from a vm_ops->fault handler, and
2250 * in that case the handler should return the result of this function.
2251 *
2252 * vma cannot be a COW mapping.
2253 *
2254 * As this is called only for pages that do not currently exist, we
2255 * do not need to flush old virtual caches or the TLB.
2256 *
2257 * Context: Process context. May allocate using %GFP_KERNEL.
2258 * Return: vm_fault_t value.
2259 */
2260vm_fault_t vmf_insert_pfn(struct vm_area_struct *vma, unsigned long addr,
2261 unsigned long pfn)
2262{
2263 return vmf_insert_pfn_prot(vma, addr, pfn, vma->vm_page_prot);
2264}
2265EXPORT_SYMBOL(vmf_insert_pfn);
2266
785a3fab
DW
2267static bool vm_mixed_ok(struct vm_area_struct *vma, pfn_t pfn)
2268{
2269 /* these checks mirror the abort conditions in vm_normal_page */
2270 if (vma->vm_flags & VM_MIXEDMAP)
2271 return true;
2272 if (pfn_t_devmap(pfn))
2273 return true;
2274 if (pfn_t_special(pfn))
2275 return true;
2276 if (is_zero_pfn(pfn_t_to_pfn(pfn)))
2277 return true;
2278 return false;
2279}
2280
79f3aa5b 2281static vm_fault_t __vm_insert_mixed(struct vm_area_struct *vma,
574c5b3d
TH
2282 unsigned long addr, pfn_t pfn, pgprot_t pgprot,
2283 bool mkwrite)
423bad60 2284{
79f3aa5b 2285 int err;
87744ab3 2286
785a3fab 2287 BUG_ON(!vm_mixed_ok(vma, pfn));
e0dc0d8f 2288
423bad60 2289 if (addr < vma->vm_start || addr >= vma->vm_end)
79f3aa5b 2290 return VM_FAULT_SIGBUS;
308a047c
BP
2291
2292 track_pfn_insert(vma, &pgprot, pfn);
e0dc0d8f 2293
42e4089c 2294 if (!pfn_modify_allowed(pfn_t_to_pfn(pfn), pgprot))
79f3aa5b 2295 return VM_FAULT_SIGBUS;
42e4089c 2296
423bad60
NP
2297 /*
2298 * If we don't have pte special, then we have to use the pfn_valid()
2299 * based VM_MIXEDMAP scheme (see vm_normal_page), and thus we *must*
2300 * refcount the page if pfn_valid is true (hence insert_page rather
62eede62
HD
2301 * than insert_pfn). If a zero_pfn were inserted into a VM_MIXEDMAP
2302 * without pte special, it would there be refcounted as a normal page.
423bad60 2303 */
00b3a331
LD
2304 if (!IS_ENABLED(CONFIG_ARCH_HAS_PTE_SPECIAL) &&
2305 !pfn_t_devmap(pfn) && pfn_t_valid(pfn)) {
423bad60
NP
2306 struct page *page;
2307
03fc2da6
DW
2308 /*
2309 * At this point we are committed to insert_page()
2310 * regardless of whether the caller specified flags that
2311 * result in pfn_t_has_page() == false.
2312 */
2313 page = pfn_to_page(pfn_t_to_pfn(pfn));
79f3aa5b
MW
2314 err = insert_page(vma, addr, page, pgprot);
2315 } else {
9b5a8e00 2316 return insert_pfn(vma, addr, pfn, pgprot, mkwrite);
423bad60 2317 }
b2770da6 2318
5d747637
MW
2319 if (err == -ENOMEM)
2320 return VM_FAULT_OOM;
2321 if (err < 0 && err != -EBUSY)
2322 return VM_FAULT_SIGBUS;
2323
2324 return VM_FAULT_NOPAGE;
e0dc0d8f 2325}
79f3aa5b 2326
574c5b3d
TH
2327/**
2328 * vmf_insert_mixed_prot - insert single pfn into user vma with specified pgprot
2329 * @vma: user vma to map to
2330 * @addr: target user address of this page
2331 * @pfn: source kernel pfn
2332 * @pgprot: pgprot flags for the inserted page
2333 *
a1a0aea5 2334 * This is exactly like vmf_insert_mixed(), except that it allows drivers
574c5b3d
TH
2335 * to override pgprot on a per-page basis.
2336 *
2337 * Typically this function should be used by drivers to set caching- and
2338 * encryption bits different than those of @vma->vm_page_prot, because
2339 * the caching- or encryption mode may not be known at mmap() time.
2340 * This is ok as long as @vma->vm_page_prot is not used by the core vm
2341 * to set caching and encryption bits for those vmas (except for COW pages).
2342 * This is ensured by core vm only modifying these page table entries using
2343 * functions that don't touch caching- or encryption bits, using pte_modify()
2344 * if needed. (See for example mprotect()).
2345 * Also when new page-table entries are created, this is only done using the
2346 * fault() callback, and never using the value of vma->vm_page_prot,
2347 * except for page-table entries that point to anonymous pages as the result
2348 * of COW.
2349 *
2350 * Context: Process context. May allocate using %GFP_KERNEL.
2351 * Return: vm_fault_t value.
2352 */
2353vm_fault_t vmf_insert_mixed_prot(struct vm_area_struct *vma, unsigned long addr,
2354 pfn_t pfn, pgprot_t pgprot)
2355{
2356 return __vm_insert_mixed(vma, addr, pfn, pgprot, false);
2357}
5379e4dd 2358EXPORT_SYMBOL(vmf_insert_mixed_prot);
574c5b3d 2359
79f3aa5b
MW
2360vm_fault_t vmf_insert_mixed(struct vm_area_struct *vma, unsigned long addr,
2361 pfn_t pfn)
2362{
574c5b3d 2363 return __vm_insert_mixed(vma, addr, pfn, vma->vm_page_prot, false);
79f3aa5b 2364}
5d747637 2365EXPORT_SYMBOL(vmf_insert_mixed);
e0dc0d8f 2366
ab77dab4
SJ
2367/*
2368 * If the insertion of PTE failed because someone else already added a
2369 * different entry in the mean time, we treat that as success as we assume
2370 * the same entry was actually inserted.
2371 */
ab77dab4
SJ
2372vm_fault_t vmf_insert_mixed_mkwrite(struct vm_area_struct *vma,
2373 unsigned long addr, pfn_t pfn)
b2770da6 2374{
574c5b3d 2375 return __vm_insert_mixed(vma, addr, pfn, vma->vm_page_prot, true);
b2770da6 2376}
ab77dab4 2377EXPORT_SYMBOL(vmf_insert_mixed_mkwrite);
b2770da6 2378
1da177e4
LT
2379/*
2380 * maps a range of physical memory into the requested pages. the old
2381 * mappings are removed. any references to nonexistent pages results
2382 * in null mappings (currently treated as "copy-on-access")
2383 */
2384static int remap_pte_range(struct mm_struct *mm, pmd_t *pmd,
2385 unsigned long addr, unsigned long end,
2386 unsigned long pfn, pgprot_t prot)
2387{
90a3e375 2388 pte_t *pte, *mapped_pte;
c74df32c 2389 spinlock_t *ptl;
42e4089c 2390 int err = 0;
1da177e4 2391
90a3e375 2392 mapped_pte = pte = pte_alloc_map_lock(mm, pmd, addr, &ptl);
1da177e4
LT
2393 if (!pte)
2394 return -ENOMEM;
6606c3e0 2395 arch_enter_lazy_mmu_mode();
1da177e4
LT
2396 do {
2397 BUG_ON(!pte_none(*pte));
42e4089c
AK
2398 if (!pfn_modify_allowed(pfn, prot)) {
2399 err = -EACCES;
2400 break;
2401 }
7e675137 2402 set_pte_at(mm, addr, pte, pte_mkspecial(pfn_pte(pfn, prot)));
1da177e4
LT
2403 pfn++;
2404 } while (pte++, addr += PAGE_SIZE, addr != end);
6606c3e0 2405 arch_leave_lazy_mmu_mode();
90a3e375 2406 pte_unmap_unlock(mapped_pte, ptl);
42e4089c 2407 return err;
1da177e4
LT
2408}
2409
2410static inline int remap_pmd_range(struct mm_struct *mm, pud_t *pud,
2411 unsigned long addr, unsigned long end,
2412 unsigned long pfn, pgprot_t prot)
2413{
2414 pmd_t *pmd;
2415 unsigned long next;
42e4089c 2416 int err;
1da177e4
LT
2417
2418 pfn -= addr >> PAGE_SHIFT;
2419 pmd = pmd_alloc(mm, pud, addr);
2420 if (!pmd)
2421 return -ENOMEM;
f66055ab 2422 VM_BUG_ON(pmd_trans_huge(*pmd));
1da177e4
LT
2423 do {
2424 next = pmd_addr_end(addr, end);
42e4089c
AK
2425 err = remap_pte_range(mm, pmd, addr, next,
2426 pfn + (addr >> PAGE_SHIFT), prot);
2427 if (err)
2428 return err;
1da177e4
LT
2429 } while (pmd++, addr = next, addr != end);
2430 return 0;
2431}
2432
c2febafc 2433static inline int remap_pud_range(struct mm_struct *mm, p4d_t *p4d,
1da177e4
LT
2434 unsigned long addr, unsigned long end,
2435 unsigned long pfn, pgprot_t prot)
2436{
2437 pud_t *pud;
2438 unsigned long next;
42e4089c 2439 int err;
1da177e4
LT
2440
2441 pfn -= addr >> PAGE_SHIFT;
c2febafc 2442 pud = pud_alloc(mm, p4d, addr);
1da177e4
LT
2443 if (!pud)
2444 return -ENOMEM;
2445 do {
2446 next = pud_addr_end(addr, end);
42e4089c
AK
2447 err = remap_pmd_range(mm, pud, addr, next,
2448 pfn + (addr >> PAGE_SHIFT), prot);
2449 if (err)
2450 return err;
1da177e4
LT
2451 } while (pud++, addr = next, addr != end);
2452 return 0;
2453}
2454
c2febafc
KS
2455static inline int remap_p4d_range(struct mm_struct *mm, pgd_t *pgd,
2456 unsigned long addr, unsigned long end,
2457 unsigned long pfn, pgprot_t prot)
2458{
2459 p4d_t *p4d;
2460 unsigned long next;
42e4089c 2461 int err;
c2febafc
KS
2462
2463 pfn -= addr >> PAGE_SHIFT;
2464 p4d = p4d_alloc(mm, pgd, addr);
2465 if (!p4d)
2466 return -ENOMEM;
2467 do {
2468 next = p4d_addr_end(addr, end);
42e4089c
AK
2469 err = remap_pud_range(mm, p4d, addr, next,
2470 pfn + (addr >> PAGE_SHIFT), prot);
2471 if (err)
2472 return err;
c2febafc
KS
2473 } while (p4d++, addr = next, addr != end);
2474 return 0;
2475}
2476
74ffa5a3
CH
2477/*
2478 * Variant of remap_pfn_range that does not call track_pfn_remap. The caller
2479 * must have pre-validated the caching bits of the pgprot_t.
bfa5bf6d 2480 */
74ffa5a3
CH
2481int remap_pfn_range_notrack(struct vm_area_struct *vma, unsigned long addr,
2482 unsigned long pfn, unsigned long size, pgprot_t prot)
1da177e4
LT
2483{
2484 pgd_t *pgd;
2485 unsigned long next;
2d15cab8 2486 unsigned long end = addr + PAGE_ALIGN(size);
1da177e4
LT
2487 struct mm_struct *mm = vma->vm_mm;
2488 int err;
2489
0c4123e3
AZ
2490 if (WARN_ON_ONCE(!PAGE_ALIGNED(addr)))
2491 return -EINVAL;
2492
1da177e4
LT
2493 /*
2494 * Physically remapped pages are special. Tell the
2495 * rest of the world about it:
2496 * VM_IO tells people not to look at these pages
2497 * (accesses can have side effects).
6aab341e
LT
2498 * VM_PFNMAP tells the core MM that the base pages are just
2499 * raw PFN mappings, and do not have a "struct page" associated
2500 * with them.
314e51b9
KK
2501 * VM_DONTEXPAND
2502 * Disable vma merging and expanding with mremap().
2503 * VM_DONTDUMP
2504 * Omit vma from core dump, even when VM_IO turned off.
fb155c16
LT
2505 *
2506 * There's a horrible special case to handle copy-on-write
2507 * behaviour that some programs depend on. We mark the "original"
2508 * un-COW'ed pages by matching them up with "vma->vm_pgoff".
b3b9c293 2509 * See vm_normal_page() for details.
1da177e4 2510 */
b3b9c293
KK
2511 if (is_cow_mapping(vma->vm_flags)) {
2512 if (addr != vma->vm_start || end != vma->vm_end)
2513 return -EINVAL;
fb155c16 2514 vma->vm_pgoff = pfn;
b3b9c293
KK
2515 }
2516
314e51b9 2517 vma->vm_flags |= VM_IO | VM_PFNMAP | VM_DONTEXPAND | VM_DONTDUMP;
1da177e4
LT
2518
2519 BUG_ON(addr >= end);
2520 pfn -= addr >> PAGE_SHIFT;
2521 pgd = pgd_offset(mm, addr);
2522 flush_cache_range(vma, addr, end);
1da177e4
LT
2523 do {
2524 next = pgd_addr_end(addr, end);
c2febafc 2525 err = remap_p4d_range(mm, pgd, addr, next,
1da177e4
LT
2526 pfn + (addr >> PAGE_SHIFT), prot);
2527 if (err)
74ffa5a3 2528 return err;
1da177e4 2529 } while (pgd++, addr = next, addr != end);
2ab64037 2530
74ffa5a3
CH
2531 return 0;
2532}
2533
2534/**
2535 * remap_pfn_range - remap kernel memory to userspace
2536 * @vma: user vma to map to
2537 * @addr: target page aligned user address to start at
2538 * @pfn: page frame number of kernel physical memory address
2539 * @size: size of mapping area
2540 * @prot: page protection flags for this mapping
2541 *
2542 * Note: this is only safe if the mm semaphore is held when called.
2543 *
2544 * Return: %0 on success, negative error code otherwise.
2545 */
2546int remap_pfn_range(struct vm_area_struct *vma, unsigned long addr,
2547 unsigned long pfn, unsigned long size, pgprot_t prot)
2548{
2549 int err;
2550
2551 err = track_pfn_remap(vma, &prot, pfn, addr, PAGE_ALIGN(size));
2ab64037 2552 if (err)
74ffa5a3 2553 return -EINVAL;
2ab64037 2554
74ffa5a3
CH
2555 err = remap_pfn_range_notrack(vma, addr, pfn, size, prot);
2556 if (err)
2557 untrack_pfn(vma, pfn, PAGE_ALIGN(size));
1da177e4
LT
2558 return err;
2559}
2560EXPORT_SYMBOL(remap_pfn_range);
2561
b4cbb197
LT
2562/**
2563 * vm_iomap_memory - remap memory to userspace
2564 * @vma: user vma to map to
abd69b9e 2565 * @start: start of the physical memory to be mapped
b4cbb197
LT
2566 * @len: size of area
2567 *
2568 * This is a simplified io_remap_pfn_range() for common driver use. The
2569 * driver just needs to give us the physical memory range to be mapped,
2570 * we'll figure out the rest from the vma information.
2571 *
2572 * NOTE! Some drivers might want to tweak vma->vm_page_prot first to get
2573 * whatever write-combining details or similar.
a862f68a
MR
2574 *
2575 * Return: %0 on success, negative error code otherwise.
b4cbb197
LT
2576 */
2577int vm_iomap_memory(struct vm_area_struct *vma, phys_addr_t start, unsigned long len)
2578{
2579 unsigned long vm_len, pfn, pages;
2580
2581 /* Check that the physical memory area passed in looks valid */
2582 if (start + len < start)
2583 return -EINVAL;
2584 /*
2585 * You *really* shouldn't map things that aren't page-aligned,
2586 * but we've historically allowed it because IO memory might
2587 * just have smaller alignment.
2588 */
2589 len += start & ~PAGE_MASK;
2590 pfn = start >> PAGE_SHIFT;
2591 pages = (len + ~PAGE_MASK) >> PAGE_SHIFT;
2592 if (pfn + pages < pfn)
2593 return -EINVAL;
2594
2595 /* We start the mapping 'vm_pgoff' pages into the area */
2596 if (vma->vm_pgoff > pages)
2597 return -EINVAL;
2598 pfn += vma->vm_pgoff;
2599 pages -= vma->vm_pgoff;
2600
2601 /* Can we fit all of the mapping? */
2602 vm_len = vma->vm_end - vma->vm_start;
2603 if (vm_len >> PAGE_SHIFT > pages)
2604 return -EINVAL;
2605
2606 /* Ok, let it rip */
2607 return io_remap_pfn_range(vma, vma->vm_start, pfn, vm_len, vma->vm_page_prot);
2608}
2609EXPORT_SYMBOL(vm_iomap_memory);
2610
aee16b3c
JF
2611static int apply_to_pte_range(struct mm_struct *mm, pmd_t *pmd,
2612 unsigned long addr, unsigned long end,
e80d3909
JR
2613 pte_fn_t fn, void *data, bool create,
2614 pgtbl_mod_mask *mask)
aee16b3c 2615{
8abb50c7 2616 pte_t *pte, *mapped_pte;
be1db475 2617 int err = 0;
3f649ab7 2618 spinlock_t *ptl;
aee16b3c 2619
be1db475 2620 if (create) {
8abb50c7 2621 mapped_pte = pte = (mm == &init_mm) ?
e80d3909 2622 pte_alloc_kernel_track(pmd, addr, mask) :
be1db475
DA
2623 pte_alloc_map_lock(mm, pmd, addr, &ptl);
2624 if (!pte)
2625 return -ENOMEM;
2626 } else {
8abb50c7 2627 mapped_pte = pte = (mm == &init_mm) ?
be1db475
DA
2628 pte_offset_kernel(pmd, addr) :
2629 pte_offset_map_lock(mm, pmd, addr, &ptl);
2630 }
aee16b3c
JF
2631
2632 BUG_ON(pmd_huge(*pmd));
2633
38e0edb1
JF
2634 arch_enter_lazy_mmu_mode();
2635
eeb4a05f
CH
2636 if (fn) {
2637 do {
2638 if (create || !pte_none(*pte)) {
2639 err = fn(pte++, addr, data);
2640 if (err)
2641 break;
2642 }
2643 } while (addr += PAGE_SIZE, addr != end);
2644 }
e80d3909 2645 *mask |= PGTBL_PTE_MODIFIED;
aee16b3c 2646
38e0edb1
JF
2647 arch_leave_lazy_mmu_mode();
2648
aee16b3c 2649 if (mm != &init_mm)
8abb50c7 2650 pte_unmap_unlock(mapped_pte, ptl);
aee16b3c
JF
2651 return err;
2652}
2653
2654static int apply_to_pmd_range(struct mm_struct *mm, pud_t *pud,
2655 unsigned long addr, unsigned long end,
e80d3909
JR
2656 pte_fn_t fn, void *data, bool create,
2657 pgtbl_mod_mask *mask)
aee16b3c
JF
2658{
2659 pmd_t *pmd;
2660 unsigned long next;
be1db475 2661 int err = 0;
aee16b3c 2662
ceb86879
AK
2663 BUG_ON(pud_huge(*pud));
2664
be1db475 2665 if (create) {
e80d3909 2666 pmd = pmd_alloc_track(mm, pud, addr, mask);
be1db475
DA
2667 if (!pmd)
2668 return -ENOMEM;
2669 } else {
2670 pmd = pmd_offset(pud, addr);
2671 }
aee16b3c
JF
2672 do {
2673 next = pmd_addr_end(addr, end);
0c95cba4
NP
2674 if (pmd_none(*pmd) && !create)
2675 continue;
2676 if (WARN_ON_ONCE(pmd_leaf(*pmd)))
2677 return -EINVAL;
2678 if (!pmd_none(*pmd) && WARN_ON_ONCE(pmd_bad(*pmd))) {
2679 if (!create)
2680 continue;
2681 pmd_clear_bad(pmd);
be1db475 2682 }
0c95cba4
NP
2683 err = apply_to_pte_range(mm, pmd, addr, next,
2684 fn, data, create, mask);
2685 if (err)
2686 break;
aee16b3c 2687 } while (pmd++, addr = next, addr != end);
0c95cba4 2688
aee16b3c
JF
2689 return err;
2690}
2691
c2febafc 2692static int apply_to_pud_range(struct mm_struct *mm, p4d_t *p4d,
aee16b3c 2693 unsigned long addr, unsigned long end,
e80d3909
JR
2694 pte_fn_t fn, void *data, bool create,
2695 pgtbl_mod_mask *mask)
aee16b3c
JF
2696{
2697 pud_t *pud;
2698 unsigned long next;
be1db475 2699 int err = 0;
aee16b3c 2700
be1db475 2701 if (create) {
e80d3909 2702 pud = pud_alloc_track(mm, p4d, addr, mask);
be1db475
DA
2703 if (!pud)
2704 return -ENOMEM;
2705 } else {
2706 pud = pud_offset(p4d, addr);
2707 }
aee16b3c
JF
2708 do {
2709 next = pud_addr_end(addr, end);
0c95cba4
NP
2710 if (pud_none(*pud) && !create)
2711 continue;
2712 if (WARN_ON_ONCE(pud_leaf(*pud)))
2713 return -EINVAL;
2714 if (!pud_none(*pud) && WARN_ON_ONCE(pud_bad(*pud))) {
2715 if (!create)
2716 continue;
2717 pud_clear_bad(pud);
be1db475 2718 }
0c95cba4
NP
2719 err = apply_to_pmd_range(mm, pud, addr, next,
2720 fn, data, create, mask);
2721 if (err)
2722 break;
aee16b3c 2723 } while (pud++, addr = next, addr != end);
0c95cba4 2724
aee16b3c
JF
2725 return err;
2726}
2727
c2febafc
KS
2728static int apply_to_p4d_range(struct mm_struct *mm, pgd_t *pgd,
2729 unsigned long addr, unsigned long end,
e80d3909
JR
2730 pte_fn_t fn, void *data, bool create,
2731 pgtbl_mod_mask *mask)
c2febafc
KS
2732{
2733 p4d_t *p4d;
2734 unsigned long next;
be1db475 2735 int err = 0;
c2febafc 2736
be1db475 2737 if (create) {
e80d3909 2738 p4d = p4d_alloc_track(mm, pgd, addr, mask);
be1db475
DA
2739 if (!p4d)
2740 return -ENOMEM;
2741 } else {
2742 p4d = p4d_offset(pgd, addr);
2743 }
c2febafc
KS
2744 do {
2745 next = p4d_addr_end(addr, end);
0c95cba4
NP
2746 if (p4d_none(*p4d) && !create)
2747 continue;
2748 if (WARN_ON_ONCE(p4d_leaf(*p4d)))
2749 return -EINVAL;
2750 if (!p4d_none(*p4d) && WARN_ON_ONCE(p4d_bad(*p4d))) {
2751 if (!create)
2752 continue;
2753 p4d_clear_bad(p4d);
be1db475 2754 }
0c95cba4
NP
2755 err = apply_to_pud_range(mm, p4d, addr, next,
2756 fn, data, create, mask);
2757 if (err)
2758 break;
c2febafc 2759 } while (p4d++, addr = next, addr != end);
0c95cba4 2760
c2febafc
KS
2761 return err;
2762}
2763
be1db475
DA
2764static int __apply_to_page_range(struct mm_struct *mm, unsigned long addr,
2765 unsigned long size, pte_fn_t fn,
2766 void *data, bool create)
aee16b3c
JF
2767{
2768 pgd_t *pgd;
e80d3909 2769 unsigned long start = addr, next;
57250a5b 2770 unsigned long end = addr + size;
e80d3909 2771 pgtbl_mod_mask mask = 0;
be1db475 2772 int err = 0;
aee16b3c 2773
9cb65bc3
MP
2774 if (WARN_ON(addr >= end))
2775 return -EINVAL;
2776
aee16b3c
JF
2777 pgd = pgd_offset(mm, addr);
2778 do {
2779 next = pgd_addr_end(addr, end);
0c95cba4 2780 if (pgd_none(*pgd) && !create)
be1db475 2781 continue;
0c95cba4
NP
2782 if (WARN_ON_ONCE(pgd_leaf(*pgd)))
2783 return -EINVAL;
2784 if (!pgd_none(*pgd) && WARN_ON_ONCE(pgd_bad(*pgd))) {
2785 if (!create)
2786 continue;
2787 pgd_clear_bad(pgd);
2788 }
2789 err = apply_to_p4d_range(mm, pgd, addr, next,
2790 fn, data, create, &mask);
aee16b3c
JF
2791 if (err)
2792 break;
2793 } while (pgd++, addr = next, addr != end);
57250a5b 2794
e80d3909
JR
2795 if (mask & ARCH_PAGE_TABLE_SYNC_MASK)
2796 arch_sync_kernel_mappings(start, start + size);
2797
aee16b3c
JF
2798 return err;
2799}
be1db475
DA
2800
2801/*
2802 * Scan a region of virtual memory, filling in page tables as necessary
2803 * and calling a provided function on each leaf page table.
2804 */
2805int apply_to_page_range(struct mm_struct *mm, unsigned long addr,
2806 unsigned long size, pte_fn_t fn, void *data)
2807{
2808 return __apply_to_page_range(mm, addr, size, fn, data, true);
2809}
aee16b3c
JF
2810EXPORT_SYMBOL_GPL(apply_to_page_range);
2811
be1db475
DA
2812/*
2813 * Scan a region of virtual memory, calling a provided function on
2814 * each leaf page table where it exists.
2815 *
2816 * Unlike apply_to_page_range, this does _not_ fill in page tables
2817 * where they are absent.
2818 */
2819int apply_to_existing_page_range(struct mm_struct *mm, unsigned long addr,
2820 unsigned long size, pte_fn_t fn, void *data)
2821{
2822 return __apply_to_page_range(mm, addr, size, fn, data, false);
2823}
2824EXPORT_SYMBOL_GPL(apply_to_existing_page_range);
2825
8f4e2101 2826/*
9b4bdd2f
KS
2827 * handle_pte_fault chooses page fault handler according to an entry which was
2828 * read non-atomically. Before making any commitment, on those architectures
2829 * or configurations (e.g. i386 with PAE) which might give a mix of unmatched
2830 * parts, do_swap_page must check under lock before unmapping the pte and
2831 * proceeding (but do_wp_page is only called after already making such a check;
a335b2e1 2832 * and do_anonymous_page can safely check later on).
8f4e2101 2833 */
2ca99358 2834static inline int pte_unmap_same(struct vm_fault *vmf)
8f4e2101
HD
2835{
2836 int same = 1;
923717cb 2837#if defined(CONFIG_SMP) || defined(CONFIG_PREEMPTION)
8f4e2101 2838 if (sizeof(pte_t) > sizeof(unsigned long)) {
2ca99358 2839 spinlock_t *ptl = pte_lockptr(vmf->vma->vm_mm, vmf->pmd);
4c21e2f2 2840 spin_lock(ptl);
2ca99358 2841 same = pte_same(*vmf->pte, vmf->orig_pte);
4c21e2f2 2842 spin_unlock(ptl);
8f4e2101
HD
2843 }
2844#endif
2ca99358
PX
2845 pte_unmap(vmf->pte);
2846 vmf->pte = NULL;
8f4e2101
HD
2847 return same;
2848}
2849
c89357e2
DH
2850static inline bool __wp_page_copy_user(struct page *dst, struct page *src,
2851 struct vm_fault *vmf)
6aab341e 2852{
83d116c5
JH
2853 bool ret;
2854 void *kaddr;
2855 void __user *uaddr;
c3e5ea6e 2856 bool locked = false;
83d116c5
JH
2857 struct vm_area_struct *vma = vmf->vma;
2858 struct mm_struct *mm = vma->vm_mm;
2859 unsigned long addr = vmf->address;
2860
83d116c5
JH
2861 if (likely(src)) {
2862 copy_user_highpage(dst, src, addr, vma);
2863 return true;
2864 }
2865
6aab341e
LT
2866 /*
2867 * If the source page was a PFN mapping, we don't have
2868 * a "struct page" for it. We do a best-effort copy by
2869 * just copying from the original user address. If that
2870 * fails, we just zero-fill it. Live with it.
2871 */
83d116c5
JH
2872 kaddr = kmap_atomic(dst);
2873 uaddr = (void __user *)(addr & PAGE_MASK);
2874
2875 /*
2876 * On architectures with software "accessed" bits, we would
2877 * take a double page fault, so mark it accessed here.
2878 */
e1fd09e3 2879 if (!arch_has_hw_pte_young() && !pte_young(vmf->orig_pte)) {
83d116c5 2880 pte_t entry;
5d2a2dbb 2881
83d116c5 2882 vmf->pte = pte_offset_map_lock(mm, vmf->pmd, addr, &vmf->ptl);
c3e5ea6e 2883 locked = true;
83d116c5
JH
2884 if (!likely(pte_same(*vmf->pte, vmf->orig_pte))) {
2885 /*
2886 * Other thread has already handled the fault
7df67697 2887 * and update local tlb only
83d116c5 2888 */
7df67697 2889 update_mmu_tlb(vma, addr, vmf->pte);
83d116c5
JH
2890 ret = false;
2891 goto pte_unlock;
2892 }
2893
2894 entry = pte_mkyoung(vmf->orig_pte);
2895 if (ptep_set_access_flags(vma, addr, vmf->pte, entry, 0))
2896 update_mmu_cache(vma, addr, vmf->pte);
2897 }
2898
2899 /*
2900 * This really shouldn't fail, because the page is there
2901 * in the page tables. But it might just be unreadable,
2902 * in which case we just give up and fill the result with
2903 * zeroes.
2904 */
2905 if (__copy_from_user_inatomic(kaddr, uaddr, PAGE_SIZE)) {
c3e5ea6e
KS
2906 if (locked)
2907 goto warn;
2908
2909 /* Re-validate under PTL if the page is still mapped */
2910 vmf->pte = pte_offset_map_lock(mm, vmf->pmd, addr, &vmf->ptl);
2911 locked = true;
2912 if (!likely(pte_same(*vmf->pte, vmf->orig_pte))) {
7df67697
BM
2913 /* The PTE changed under us, update local tlb */
2914 update_mmu_tlb(vma, addr, vmf->pte);
c3e5ea6e
KS
2915 ret = false;
2916 goto pte_unlock;
2917 }
2918
5d2a2dbb 2919 /*
985ba004 2920 * The same page can be mapped back since last copy attempt.
c3e5ea6e 2921 * Try to copy again under PTL.
5d2a2dbb 2922 */
c3e5ea6e
KS
2923 if (__copy_from_user_inatomic(kaddr, uaddr, PAGE_SIZE)) {
2924 /*
2925 * Give a warn in case there can be some obscure
2926 * use-case
2927 */
2928warn:
2929 WARN_ON_ONCE(1);
2930 clear_page(kaddr);
2931 }
83d116c5
JH
2932 }
2933
2934 ret = true;
2935
2936pte_unlock:
c3e5ea6e 2937 if (locked)
83d116c5
JH
2938 pte_unmap_unlock(vmf->pte, vmf->ptl);
2939 kunmap_atomic(kaddr);
2940 flush_dcache_page(dst);
2941
2942 return ret;
6aab341e
LT
2943}
2944
c20cd45e
MH
2945static gfp_t __get_fault_gfp_mask(struct vm_area_struct *vma)
2946{
2947 struct file *vm_file = vma->vm_file;
2948
2949 if (vm_file)
2950 return mapping_gfp_mask(vm_file->f_mapping) | __GFP_FS | __GFP_IO;
2951
2952 /*
2953 * Special mappings (e.g. VDSO) do not have any file so fake
2954 * a default GFP_KERNEL for them.
2955 */
2956 return GFP_KERNEL;
2957}
2958
fb09a464
KS
2959/*
2960 * Notify the address space that the page is about to become writable so that
2961 * it can prohibit this or wait for the page to get into an appropriate state.
2962 *
2963 * We do this without the lock held, so that it can sleep if it needs to.
2964 */
2b740303 2965static vm_fault_t do_page_mkwrite(struct vm_fault *vmf)
fb09a464 2966{
2b740303 2967 vm_fault_t ret;
38b8cb7f
JK
2968 struct page *page = vmf->page;
2969 unsigned int old_flags = vmf->flags;
fb09a464 2970
38b8cb7f 2971 vmf->flags = FAULT_FLAG_WRITE|FAULT_FLAG_MKWRITE;
fb09a464 2972
dc617f29
DW
2973 if (vmf->vma->vm_file &&
2974 IS_SWAPFILE(vmf->vma->vm_file->f_mapping->host))
2975 return VM_FAULT_SIGBUS;
2976
11bac800 2977 ret = vmf->vma->vm_ops->page_mkwrite(vmf);
38b8cb7f
JK
2978 /* Restore original flags so that caller is not surprised */
2979 vmf->flags = old_flags;
fb09a464
KS
2980 if (unlikely(ret & (VM_FAULT_ERROR | VM_FAULT_NOPAGE)))
2981 return ret;
2982 if (unlikely(!(ret & VM_FAULT_LOCKED))) {
2983 lock_page(page);
2984 if (!page->mapping) {
2985 unlock_page(page);
2986 return 0; /* retry */
2987 }
2988 ret |= VM_FAULT_LOCKED;
2989 } else
2990 VM_BUG_ON_PAGE(!PageLocked(page), page);
2991 return ret;
2992}
2993
97ba0c2b
JK
2994/*
2995 * Handle dirtying of a page in shared file mapping on a write fault.
2996 *
2997 * The function expects the page to be locked and unlocks it.
2998 */
89b15332 2999static vm_fault_t fault_dirty_shared_page(struct vm_fault *vmf)
97ba0c2b 3000{
89b15332 3001 struct vm_area_struct *vma = vmf->vma;
97ba0c2b 3002 struct address_space *mapping;
89b15332 3003 struct page *page = vmf->page;
97ba0c2b
JK
3004 bool dirtied;
3005 bool page_mkwrite = vma->vm_ops && vma->vm_ops->page_mkwrite;
3006
3007 dirtied = set_page_dirty(page);
3008 VM_BUG_ON_PAGE(PageAnon(page), page);
3009 /*
3010 * Take a local copy of the address_space - page.mapping may be zeroed
3011 * by truncate after unlock_page(). The address_space itself remains
3012 * pinned by vma->vm_file's reference. We rely on unlock_page()'s
3013 * release semantics to prevent the compiler from undoing this copying.
3014 */
3015 mapping = page_rmapping(page);
3016 unlock_page(page);
3017
89b15332
JW
3018 if (!page_mkwrite)
3019 file_update_time(vma->vm_file);
3020
3021 /*
3022 * Throttle page dirtying rate down to writeback speed.
3023 *
3024 * mapping may be NULL here because some device drivers do not
3025 * set page.mapping but still dirty their pages
3026 *
c1e8d7c6 3027 * Drop the mmap_lock before waiting on IO, if we can. The file
89b15332
JW
3028 * is pinning the mapping, as per above.
3029 */
97ba0c2b 3030 if ((dirtied || page_mkwrite) && mapping) {
89b15332
JW
3031 struct file *fpin;
3032
3033 fpin = maybe_unlock_mmap_for_io(vmf, NULL);
97ba0c2b 3034 balance_dirty_pages_ratelimited(mapping);
89b15332
JW
3035 if (fpin) {
3036 fput(fpin);
d9272525 3037 return VM_FAULT_COMPLETED;
89b15332 3038 }
97ba0c2b
JK
3039 }
3040
89b15332 3041 return 0;
97ba0c2b
JK
3042}
3043
4e047f89
SR
3044/*
3045 * Handle write page faults for pages that can be reused in the current vma
3046 *
3047 * This can happen either due to the mapping being with the VM_SHARED flag,
3048 * or due to us being the last reference standing to the page. In either
3049 * case, all we need to do here is to mark the page as writable and update
3050 * any related book-keeping.
3051 */
997dd98d 3052static inline void wp_page_reuse(struct vm_fault *vmf)
82b0f8c3 3053 __releases(vmf->ptl)
4e047f89 3054{
82b0f8c3 3055 struct vm_area_struct *vma = vmf->vma;
a41b70d6 3056 struct page *page = vmf->page;
4e047f89 3057 pte_t entry;
6c287605 3058
c89357e2 3059 VM_BUG_ON(!(vmf->flags & FAULT_FLAG_WRITE));
cdb281e6 3060 VM_BUG_ON(page && PageAnon(page) && !PageAnonExclusive(page));
6c287605 3061
4e047f89
SR
3062 /*
3063 * Clear the pages cpupid information as the existing
3064 * information potentially belongs to a now completely
3065 * unrelated process.
3066 */
3067 if (page)
3068 page_cpupid_xchg_last(page, (1 << LAST_CPUPID_SHIFT) - 1);
3069
2994302b
JK
3070 flush_cache_page(vma, vmf->address, pte_pfn(vmf->orig_pte));
3071 entry = pte_mkyoung(vmf->orig_pte);
4e047f89 3072 entry = maybe_mkwrite(pte_mkdirty(entry), vma);
82b0f8c3
JK
3073 if (ptep_set_access_flags(vma, vmf->address, vmf->pte, entry, 1))
3074 update_mmu_cache(vma, vmf->address, vmf->pte);
3075 pte_unmap_unlock(vmf->pte, vmf->ptl);
798a6b87 3076 count_vm_event(PGREUSE);
4e047f89
SR
3077}
3078
2f38ab2c 3079/*
c89357e2
DH
3080 * Handle the case of a page which we actually need to copy to a new page,
3081 * either due to COW or unsharing.
2f38ab2c 3082 *
c1e8d7c6 3083 * Called with mmap_lock locked and the old page referenced, but
2f38ab2c
SR
3084 * without the ptl held.
3085 *
3086 * High level logic flow:
3087 *
3088 * - Allocate a page, copy the content of the old page to the new one.
3089 * - Handle book keeping and accounting - cgroups, mmu-notifiers, etc.
3090 * - Take the PTL. If the pte changed, bail out and release the allocated page
3091 * - If the pte is still the way we remember it, update the page table and all
3092 * relevant references. This includes dropping the reference the page-table
3093 * held to the old page, as well as updating the rmap.
3094 * - In any case, unlock the PTL and drop the reference we took to the old page.
3095 */
2b740303 3096static vm_fault_t wp_page_copy(struct vm_fault *vmf)
2f38ab2c 3097{
c89357e2 3098 const bool unshare = vmf->flags & FAULT_FLAG_UNSHARE;
82b0f8c3 3099 struct vm_area_struct *vma = vmf->vma;
bae473a4 3100 struct mm_struct *mm = vma->vm_mm;
a41b70d6 3101 struct page *old_page = vmf->page;
2f38ab2c 3102 struct page *new_page = NULL;
2f38ab2c
SR
3103 pte_t entry;
3104 int page_copied = 0;
ac46d4f3 3105 struct mmu_notifier_range range;
2f38ab2c 3106
662ce1dc
YY
3107 delayacct_wpcopy_start();
3108
2f38ab2c
SR
3109 if (unlikely(anon_vma_prepare(vma)))
3110 goto oom;
3111
2994302b 3112 if (is_zero_pfn(pte_pfn(vmf->orig_pte))) {
82b0f8c3
JK
3113 new_page = alloc_zeroed_user_highpage_movable(vma,
3114 vmf->address);
2f38ab2c
SR
3115 if (!new_page)
3116 goto oom;
3117 } else {
bae473a4 3118 new_page = alloc_page_vma(GFP_HIGHUSER_MOVABLE, vma,
82b0f8c3 3119 vmf->address);
2f38ab2c
SR
3120 if (!new_page)
3121 goto oom;
83d116c5 3122
c89357e2 3123 if (!__wp_page_copy_user(new_page, old_page, vmf)) {
83d116c5
JH
3124 /*
3125 * COW failed, if the fault was solved by other,
3126 * it's fine. If not, userspace would re-fault on
3127 * the same address and we will handle the fault
3128 * from the second attempt.
3129 */
3130 put_page(new_page);
3131 if (old_page)
3132 put_page(old_page);
662ce1dc
YY
3133
3134 delayacct_wpcopy_end();
83d116c5
JH
3135 return 0;
3136 }
2f38ab2c 3137 }
2f38ab2c 3138
8f425e4e 3139 if (mem_cgroup_charge(page_folio(new_page), mm, GFP_KERNEL))
2f38ab2c 3140 goto oom_free_new;
9d82c694 3141 cgroup_throttle_swaprate(new_page, GFP_KERNEL);
2f38ab2c 3142
eb3c24f3
MG
3143 __SetPageUptodate(new_page);
3144
7269f999 3145 mmu_notifier_range_init(&range, MMU_NOTIFY_CLEAR, 0, vma, mm,
6f4f13e8 3146 vmf->address & PAGE_MASK,
ac46d4f3
JG
3147 (vmf->address & PAGE_MASK) + PAGE_SIZE);
3148 mmu_notifier_invalidate_range_start(&range);
2f38ab2c
SR
3149
3150 /*
3151 * Re-check the pte - we dropped the lock
3152 */
82b0f8c3 3153 vmf->pte = pte_offset_map_lock(mm, vmf->pmd, vmf->address, &vmf->ptl);
2994302b 3154 if (likely(pte_same(*vmf->pte, vmf->orig_pte))) {
2f38ab2c
SR
3155 if (old_page) {
3156 if (!PageAnon(old_page)) {
eca56ff9
JM
3157 dec_mm_counter_fast(mm,
3158 mm_counter_file(old_page));
2f38ab2c
SR
3159 inc_mm_counter_fast(mm, MM_ANONPAGES);
3160 }
3161 } else {
3162 inc_mm_counter_fast(mm, MM_ANONPAGES);
3163 }
2994302b 3164 flush_cache_page(vma, vmf->address, pte_pfn(vmf->orig_pte));
2f38ab2c 3165 entry = mk_pte(new_page, vma->vm_page_prot);
50c25ee9 3166 entry = pte_sw_mkyoung(entry);
c89357e2
DH
3167 if (unlikely(unshare)) {
3168 if (pte_soft_dirty(vmf->orig_pte))
3169 entry = pte_mksoft_dirty(entry);
3170 if (pte_uffd_wp(vmf->orig_pte))
3171 entry = pte_mkuffd_wp(entry);
3172 } else {
3173 entry = maybe_mkwrite(pte_mkdirty(entry), vma);
3174 }
111fe718 3175
2f38ab2c
SR
3176 /*
3177 * Clear the pte entry and flush it first, before updating the
111fe718
NP
3178 * pte with the new entry, to keep TLBs on different CPUs in
3179 * sync. This code used to set the new PTE then flush TLBs, but
3180 * that left a window where the new PTE could be loaded into
3181 * some TLBs while the old PTE remains in others.
2f38ab2c 3182 */
82b0f8c3 3183 ptep_clear_flush_notify(vma, vmf->address, vmf->pte);
40f2bbf7 3184 page_add_new_anon_rmap(new_page, vma, vmf->address);
b518154e 3185 lru_cache_add_inactive_or_unevictable(new_page, vma);
2f38ab2c
SR
3186 /*
3187 * We call the notify macro here because, when using secondary
3188 * mmu page tables (such as kvm shadow page tables), we want the
3189 * new page to be mapped directly into the secondary page table.
3190 */
c89357e2 3191 BUG_ON(unshare && pte_write(entry));
82b0f8c3
JK
3192 set_pte_at_notify(mm, vmf->address, vmf->pte, entry);
3193 update_mmu_cache(vma, vmf->address, vmf->pte);
2f38ab2c
SR
3194 if (old_page) {
3195 /*
3196 * Only after switching the pte to the new page may
3197 * we remove the mapcount here. Otherwise another
3198 * process may come and find the rmap count decremented
3199 * before the pte is switched to the new page, and
3200 * "reuse" the old page writing into it while our pte
3201 * here still points into it and can be read by other
3202 * threads.
3203 *
3204 * The critical issue is to order this
3205 * page_remove_rmap with the ptp_clear_flush above.
3206 * Those stores are ordered by (if nothing else,)
3207 * the barrier present in the atomic_add_negative
3208 * in page_remove_rmap.
3209 *
3210 * Then the TLB flush in ptep_clear_flush ensures that
3211 * no process can access the old page before the
3212 * decremented mapcount is visible. And the old page
3213 * cannot be reused until after the decremented
3214 * mapcount is visible. So transitively, TLBs to
3215 * old page will be flushed before it can be reused.
3216 */
cea86fe2 3217 page_remove_rmap(old_page, vma, false);
2f38ab2c
SR
3218 }
3219
3220 /* Free the old page.. */
3221 new_page = old_page;
3222 page_copied = 1;
3223 } else {
7df67697 3224 update_mmu_tlb(vma, vmf->address, vmf->pte);
2f38ab2c
SR
3225 }
3226
3227 if (new_page)
09cbfeaf 3228 put_page(new_page);
2f38ab2c 3229
82b0f8c3 3230 pte_unmap_unlock(vmf->pte, vmf->ptl);
4645b9fe
JG
3231 /*
3232 * No need to double call mmu_notifier->invalidate_range() callback as
3233 * the above ptep_clear_flush_notify() did already call it.
3234 */
ac46d4f3 3235 mmu_notifier_invalidate_range_only_end(&range);
2f38ab2c 3236 if (old_page) {
f4c4a3f4
HY
3237 if (page_copied)
3238 free_swap_cache(old_page);
09cbfeaf 3239 put_page(old_page);
2f38ab2c 3240 }
662ce1dc
YY
3241
3242 delayacct_wpcopy_end();
c89357e2 3243 return (page_copied && !unshare) ? VM_FAULT_WRITE : 0;
2f38ab2c 3244oom_free_new:
09cbfeaf 3245 put_page(new_page);
2f38ab2c
SR
3246oom:
3247 if (old_page)
09cbfeaf 3248 put_page(old_page);
662ce1dc
YY
3249
3250 delayacct_wpcopy_end();
2f38ab2c
SR
3251 return VM_FAULT_OOM;
3252}
3253
66a6197c
JK
3254/**
3255 * finish_mkwrite_fault - finish page fault for a shared mapping, making PTE
3256 * writeable once the page is prepared
3257 *
3258 * @vmf: structure describing the fault
3259 *
3260 * This function handles all that is needed to finish a write page fault in a
3261 * shared mapping due to PTE being read-only once the mapped page is prepared.
a862f68a 3262 * It handles locking of PTE and modifying it.
66a6197c
JK
3263 *
3264 * The function expects the page to be locked or other protection against
3265 * concurrent faults / writeback (such as DAX radix tree locks).
a862f68a 3266 *
2797e79f 3267 * Return: %0 on success, %VM_FAULT_NOPAGE when PTE got changed before
a862f68a 3268 * we acquired PTE lock.
66a6197c 3269 */
2b740303 3270vm_fault_t finish_mkwrite_fault(struct vm_fault *vmf)
66a6197c
JK
3271{
3272 WARN_ON_ONCE(!(vmf->vma->vm_flags & VM_SHARED));
3273 vmf->pte = pte_offset_map_lock(vmf->vma->vm_mm, vmf->pmd, vmf->address,
3274 &vmf->ptl);
3275 /*
3276 * We might have raced with another page fault while we released the
3277 * pte_offset_map_lock.
3278 */
3279 if (!pte_same(*vmf->pte, vmf->orig_pte)) {
7df67697 3280 update_mmu_tlb(vmf->vma, vmf->address, vmf->pte);
66a6197c 3281 pte_unmap_unlock(vmf->pte, vmf->ptl);
a19e2553 3282 return VM_FAULT_NOPAGE;
66a6197c
JK
3283 }
3284 wp_page_reuse(vmf);
a19e2553 3285 return 0;
66a6197c
JK
3286}
3287
dd906184
BH
3288/*
3289 * Handle write page faults for VM_MIXEDMAP or VM_PFNMAP for a VM_SHARED
3290 * mapping
3291 */
2b740303 3292static vm_fault_t wp_pfn_shared(struct vm_fault *vmf)
dd906184 3293{
82b0f8c3 3294 struct vm_area_struct *vma = vmf->vma;
bae473a4 3295
dd906184 3296 if (vma->vm_ops && vma->vm_ops->pfn_mkwrite) {
2b740303 3297 vm_fault_t ret;
dd906184 3298
82b0f8c3 3299 pte_unmap_unlock(vmf->pte, vmf->ptl);
fe82221f 3300 vmf->flags |= FAULT_FLAG_MKWRITE;
11bac800 3301 ret = vma->vm_ops->pfn_mkwrite(vmf);
2f89dc12 3302 if (ret & (VM_FAULT_ERROR | VM_FAULT_NOPAGE))
dd906184 3303 return ret;
66a6197c 3304 return finish_mkwrite_fault(vmf);
dd906184 3305 }
997dd98d
JK
3306 wp_page_reuse(vmf);
3307 return VM_FAULT_WRITE;
dd906184
BH
3308}
3309
2b740303 3310static vm_fault_t wp_page_shared(struct vm_fault *vmf)
82b0f8c3 3311 __releases(vmf->ptl)
93e478d4 3312{
82b0f8c3 3313 struct vm_area_struct *vma = vmf->vma;
89b15332 3314 vm_fault_t ret = VM_FAULT_WRITE;
93e478d4 3315
a41b70d6 3316 get_page(vmf->page);
93e478d4 3317
93e478d4 3318 if (vma->vm_ops && vma->vm_ops->page_mkwrite) {
2b740303 3319 vm_fault_t tmp;
93e478d4 3320
82b0f8c3 3321 pte_unmap_unlock(vmf->pte, vmf->ptl);
38b8cb7f 3322 tmp = do_page_mkwrite(vmf);
93e478d4
SR
3323 if (unlikely(!tmp || (tmp &
3324 (VM_FAULT_ERROR | VM_FAULT_NOPAGE)))) {
a41b70d6 3325 put_page(vmf->page);
93e478d4
SR
3326 return tmp;
3327 }
66a6197c 3328 tmp = finish_mkwrite_fault(vmf);
a19e2553 3329 if (unlikely(tmp & (VM_FAULT_ERROR | VM_FAULT_NOPAGE))) {
a41b70d6 3330 unlock_page(vmf->page);
a41b70d6 3331 put_page(vmf->page);
66a6197c 3332 return tmp;
93e478d4 3333 }
66a6197c
JK
3334 } else {
3335 wp_page_reuse(vmf);
997dd98d 3336 lock_page(vmf->page);
93e478d4 3337 }
89b15332 3338 ret |= fault_dirty_shared_page(vmf);
997dd98d 3339 put_page(vmf->page);
93e478d4 3340
89b15332 3341 return ret;
93e478d4
SR
3342}
3343
1da177e4 3344/*
c89357e2
DH
3345 * This routine handles present pages, when
3346 * * users try to write to a shared page (FAULT_FLAG_WRITE)
3347 * * GUP wants to take a R/O pin on a possibly shared anonymous page
3348 * (FAULT_FLAG_UNSHARE)
3349 *
3350 * It is done by copying the page to a new address and decrementing the
3351 * shared-page counter for the old page.
1da177e4 3352 *
1da177e4
LT
3353 * Note that this routine assumes that the protection checks have been
3354 * done by the caller (the low-level page fault routine in most cases).
c89357e2
DH
3355 * Thus, with FAULT_FLAG_WRITE, we can safely just mark it writable once we've
3356 * done any necessary COW.
1da177e4 3357 *
c89357e2
DH
3358 * In case of FAULT_FLAG_WRITE, we also mark the page dirty at this point even
3359 * though the page will change only once the write actually happens. This
3360 * avoids a few races, and potentially makes it more efficient.
1da177e4 3361 *
c1e8d7c6 3362 * We enter with non-exclusive mmap_lock (to exclude vma changes,
8f4e2101 3363 * but allow concurrent faults), with pte both mapped and locked.
c1e8d7c6 3364 * We return with mmap_lock still held, but pte unmapped and unlocked.
1da177e4 3365 */
2b740303 3366static vm_fault_t do_wp_page(struct vm_fault *vmf)
82b0f8c3 3367 __releases(vmf->ptl)
1da177e4 3368{
c89357e2 3369 const bool unshare = vmf->flags & FAULT_FLAG_UNSHARE;
82b0f8c3 3370 struct vm_area_struct *vma = vmf->vma;
e4a2ed94 3371 struct folio *folio;
1da177e4 3372
c89357e2
DH
3373 VM_BUG_ON(unshare && (vmf->flags & FAULT_FLAG_WRITE));
3374 VM_BUG_ON(!unshare && !(vmf->flags & FAULT_FLAG_WRITE));
529b930b 3375
c89357e2
DH
3376 if (likely(!unshare)) {
3377 if (userfaultfd_pte_wp(vma, *vmf->pte)) {
3378 pte_unmap_unlock(vmf->pte, vmf->ptl);
3379 return handle_userfault(vmf, VM_UFFD_WP);
3380 }
3381
3382 /*
3383 * Userfaultfd write-protect can defer flushes. Ensure the TLB
3384 * is flushed in this case before copying.
3385 */
3386 if (unlikely(userfaultfd_wp(vmf->vma) &&
3387 mm_tlb_flush_pending(vmf->vma->vm_mm)))
3388 flush_tlb_page(vmf->vma, vmf->address);
3389 }
6ce64428 3390
a41b70d6
JK
3391 vmf->page = vm_normal_page(vma, vmf->address, vmf->orig_pte);
3392 if (!vmf->page) {
c89357e2
DH
3393 if (unlikely(unshare)) {
3394 /* No anonymous page -> nothing to do. */
3395 pte_unmap_unlock(vmf->pte, vmf->ptl);
3396 return 0;
3397 }
3398
251b97f5 3399 /*
64e45507
PF
3400 * VM_MIXEDMAP !pfn_valid() case, or VM_SOFTDIRTY clear on a
3401 * VM_PFNMAP VMA.
251b97f5
PZ
3402 *
3403 * We should not cow pages in a shared writeable mapping.
dd906184 3404 * Just mark the pages writable and/or call ops->pfn_mkwrite.
251b97f5
PZ
3405 */
3406 if ((vma->vm_flags & (VM_WRITE|VM_SHARED)) ==
3407 (VM_WRITE|VM_SHARED))
2994302b 3408 return wp_pfn_shared(vmf);
2f38ab2c 3409
82b0f8c3 3410 pte_unmap_unlock(vmf->pte, vmf->ptl);
a41b70d6 3411 return wp_page_copy(vmf);
251b97f5 3412 }
1da177e4 3413
d08b3851 3414 /*
ee6a6457
PZ
3415 * Take out anonymous pages first, anonymous shared vmas are
3416 * not dirty accountable.
d08b3851 3417 */
e4a2ed94
MWO
3418 folio = page_folio(vmf->page);
3419 if (folio_test_anon(folio)) {
6c287605
DH
3420 /*
3421 * If the page is exclusive to this process we must reuse the
3422 * page without further checks.
3423 */
e4a2ed94 3424 if (PageAnonExclusive(vmf->page))
6c287605
DH
3425 goto reuse;
3426
53a05ad9 3427 /*
e4a2ed94
MWO
3428 * We have to verify under folio lock: these early checks are
3429 * just an optimization to avoid locking the folio and freeing
53a05ad9
DH
3430 * the swapcache if there is little hope that we can reuse.
3431 *
e4a2ed94 3432 * KSM doesn't necessarily raise the folio refcount.
53a05ad9 3433 */
e4a2ed94 3434 if (folio_test_ksm(folio) || folio_ref_count(folio) > 3)
d4c47097 3435 goto copy;
e4a2ed94 3436 if (!folio_test_lru(folio))
d4c47097
DH
3437 /*
3438 * Note: We cannot easily detect+handle references from
e4a2ed94 3439 * remote LRU pagevecs or references to LRU folios.
d4c47097
DH
3440 */
3441 lru_add_drain();
e4a2ed94 3442 if (folio_ref_count(folio) > 1 + folio_test_swapcache(folio))
09854ba9 3443 goto copy;
e4a2ed94 3444 if (!folio_trylock(folio))
09854ba9 3445 goto copy;
e4a2ed94
MWO
3446 if (folio_test_swapcache(folio))
3447 folio_free_swap(folio);
3448 if (folio_test_ksm(folio) || folio_ref_count(folio) != 1) {
3449 folio_unlock(folio);
52d1e606 3450 goto copy;
b009c024 3451 }
09854ba9 3452 /*
e4a2ed94
MWO
3453 * Ok, we've got the only folio reference from our mapping
3454 * and the folio is locked, it's dark out, and we're wearing
53a05ad9 3455 * sunglasses. Hit it.
09854ba9 3456 */
e4a2ed94
MWO
3457 page_move_anon_rmap(vmf->page, vma);
3458 folio_unlock(folio);
6c287605 3459reuse:
c89357e2
DH
3460 if (unlikely(unshare)) {
3461 pte_unmap_unlock(vmf->pte, vmf->ptl);
3462 return 0;
3463 }
be068f29 3464 wp_page_reuse(vmf);
09854ba9 3465 return VM_FAULT_WRITE;
c89357e2
DH
3466 } else if (unshare) {
3467 /* No anonymous page -> nothing to do. */
3468 pte_unmap_unlock(vmf->pte, vmf->ptl);
3469 return 0;
ee6a6457 3470 } else if (unlikely((vma->vm_flags & (VM_WRITE|VM_SHARED)) ==
d08b3851 3471 (VM_WRITE|VM_SHARED))) {
a41b70d6 3472 return wp_page_shared(vmf);
1da177e4 3473 }
52d1e606 3474copy:
1da177e4
LT
3475 /*
3476 * Ok, we need to copy. Oh, well..
3477 */
a41b70d6 3478 get_page(vmf->page);
28766805 3479
82b0f8c3 3480 pte_unmap_unlock(vmf->pte, vmf->ptl);
94bfe85b
YY
3481#ifdef CONFIG_KSM
3482 if (PageKsm(vmf->page))
3483 count_vm_event(COW_KSM);
3484#endif
a41b70d6 3485 return wp_page_copy(vmf);
1da177e4
LT
3486}
3487
97a89413 3488static void unmap_mapping_range_vma(struct vm_area_struct *vma,
1da177e4
LT
3489 unsigned long start_addr, unsigned long end_addr,
3490 struct zap_details *details)
3491{
f5cc4eef 3492 zap_page_range_single(vma, start_addr, end_addr - start_addr, details);
1da177e4
LT
3493}
3494
f808c13f 3495static inline void unmap_mapping_range_tree(struct rb_root_cached *root,
232a6a1c
PX
3496 pgoff_t first_index,
3497 pgoff_t last_index,
1da177e4
LT
3498 struct zap_details *details)
3499{
3500 struct vm_area_struct *vma;
1da177e4
LT
3501 pgoff_t vba, vea, zba, zea;
3502
232a6a1c 3503 vma_interval_tree_foreach(vma, root, first_index, last_index) {
1da177e4 3504 vba = vma->vm_pgoff;
d6e93217 3505 vea = vba + vma_pages(vma) - 1;
f9871da9
ML
3506 zba = max(first_index, vba);
3507 zea = min(last_index, vea);
1da177e4 3508
97a89413 3509 unmap_mapping_range_vma(vma,
1da177e4
LT
3510 ((zba - vba) << PAGE_SHIFT) + vma->vm_start,
3511 ((zea - vba + 1) << PAGE_SHIFT) + vma->vm_start,
97a89413 3512 details);
1da177e4
LT
3513 }
3514}
3515
22061a1f 3516/**
3506659e
MWO
3517 * unmap_mapping_folio() - Unmap single folio from processes.
3518 * @folio: The locked folio to be unmapped.
22061a1f 3519 *
3506659e 3520 * Unmap this folio from any userspace process which still has it mmaped.
22061a1f
HD
3521 * Typically, for efficiency, the range of nearby pages has already been
3522 * unmapped by unmap_mapping_pages() or unmap_mapping_range(). But once
3506659e
MWO
3523 * truncation or invalidation holds the lock on a folio, it may find that
3524 * the page has been remapped again: and then uses unmap_mapping_folio()
22061a1f
HD
3525 * to unmap it finally.
3526 */
3506659e 3527void unmap_mapping_folio(struct folio *folio)
22061a1f 3528{
3506659e 3529 struct address_space *mapping = folio->mapping;
22061a1f 3530 struct zap_details details = { };
232a6a1c
PX
3531 pgoff_t first_index;
3532 pgoff_t last_index;
22061a1f 3533
3506659e 3534 VM_BUG_ON(!folio_test_locked(folio));
22061a1f 3535
3506659e
MWO
3536 first_index = folio->index;
3537 last_index = folio->index + folio_nr_pages(folio) - 1;
232a6a1c 3538
2e148f1e 3539 details.even_cows = false;
3506659e 3540 details.single_folio = folio;
999dad82 3541 details.zap_flags = ZAP_FLAG_DROP_MARKER;
22061a1f 3542
2c865995 3543 i_mmap_lock_read(mapping);
22061a1f 3544 if (unlikely(!RB_EMPTY_ROOT(&mapping->i_mmap.rb_root)))
232a6a1c
PX
3545 unmap_mapping_range_tree(&mapping->i_mmap, first_index,
3546 last_index, &details);
2c865995 3547 i_mmap_unlock_read(mapping);
22061a1f
HD
3548}
3549
977fbdcd
MW
3550/**
3551 * unmap_mapping_pages() - Unmap pages from processes.
3552 * @mapping: The address space containing pages to be unmapped.
3553 * @start: Index of first page to be unmapped.
3554 * @nr: Number of pages to be unmapped. 0 to unmap to end of file.
3555 * @even_cows: Whether to unmap even private COWed pages.
3556 *
3557 * Unmap the pages in this address space from any userspace process which
3558 * has them mmaped. Generally, you want to remove COWed pages as well when
3559 * a file is being truncated, but not when invalidating pages from the page
3560 * cache.
3561 */
3562void unmap_mapping_pages(struct address_space *mapping, pgoff_t start,
3563 pgoff_t nr, bool even_cows)
3564{
3565 struct zap_details details = { };
232a6a1c
PX
3566 pgoff_t first_index = start;
3567 pgoff_t last_index = start + nr - 1;
977fbdcd 3568
2e148f1e 3569 details.even_cows = even_cows;
232a6a1c
PX
3570 if (last_index < first_index)
3571 last_index = ULONG_MAX;
977fbdcd 3572
2c865995 3573 i_mmap_lock_read(mapping);
977fbdcd 3574 if (unlikely(!RB_EMPTY_ROOT(&mapping->i_mmap.rb_root)))
232a6a1c
PX
3575 unmap_mapping_range_tree(&mapping->i_mmap, first_index,
3576 last_index, &details);
2c865995 3577 i_mmap_unlock_read(mapping);
977fbdcd 3578}
6e0e99d5 3579EXPORT_SYMBOL_GPL(unmap_mapping_pages);
977fbdcd 3580
1da177e4 3581/**
8a5f14a2 3582 * unmap_mapping_range - unmap the portion of all mmaps in the specified
977fbdcd 3583 * address_space corresponding to the specified byte range in the underlying
8a5f14a2
KS
3584 * file.
3585 *
3d41088f 3586 * @mapping: the address space containing mmaps to be unmapped.
1da177e4
LT
3587 * @holebegin: byte in first page to unmap, relative to the start of
3588 * the underlying file. This will be rounded down to a PAGE_SIZE
25d9e2d1 3589 * boundary. Note that this is different from truncate_pagecache(), which
1da177e4
LT
3590 * must keep the partial page. In contrast, we must get rid of
3591 * partial pages.
3592 * @holelen: size of prospective hole in bytes. This will be rounded
3593 * up to a PAGE_SIZE boundary. A holelen of zero truncates to the
3594 * end of the file.
3595 * @even_cows: 1 when truncating a file, unmap even private COWed pages;
3596 * but 0 when invalidating pagecache, don't throw away private data.
3597 */
3598void unmap_mapping_range(struct address_space *mapping,
3599 loff_t const holebegin, loff_t const holelen, int even_cows)
3600{
1da177e4
LT
3601 pgoff_t hba = holebegin >> PAGE_SHIFT;
3602 pgoff_t hlen = (holelen + PAGE_SIZE - 1) >> PAGE_SHIFT;
3603
3604 /* Check for overflow. */
3605 if (sizeof(holelen) > sizeof(hlen)) {
3606 long long holeend =
3607 (holebegin + holelen + PAGE_SIZE - 1) >> PAGE_SHIFT;
3608 if (holeend & ~(long long)ULONG_MAX)
3609 hlen = ULONG_MAX - hba + 1;
3610 }
3611
977fbdcd 3612 unmap_mapping_pages(mapping, hba, hlen, even_cows);
1da177e4
LT
3613}
3614EXPORT_SYMBOL(unmap_mapping_range);
3615
b756a3b5
AP
3616/*
3617 * Restore a potential device exclusive pte to a working pte entry
3618 */
3619static vm_fault_t remove_device_exclusive_entry(struct vm_fault *vmf)
3620{
3621 struct page *page = vmf->page;
3622 struct vm_area_struct *vma = vmf->vma;
3623 struct mmu_notifier_range range;
3624
3625 if (!lock_page_or_retry(page, vma->vm_mm, vmf->flags))
3626 return VM_FAULT_RETRY;
3627 mmu_notifier_range_init_owner(&range, MMU_NOTIFY_EXCLUSIVE, 0, vma,
3628 vma->vm_mm, vmf->address & PAGE_MASK,
3629 (vmf->address & PAGE_MASK) + PAGE_SIZE, NULL);
3630 mmu_notifier_invalidate_range_start(&range);
3631
3632 vmf->pte = pte_offset_map_lock(vma->vm_mm, vmf->pmd, vmf->address,
3633 &vmf->ptl);
3634 if (likely(pte_same(*vmf->pte, vmf->orig_pte)))
3635 restore_exclusive_pte(vma, page, vmf->address, vmf->pte);
3636
3637 pte_unmap_unlock(vmf->pte, vmf->ptl);
3638 unlock_page(page);
3639
3640 mmu_notifier_invalidate_range_end(&range);
3641 return 0;
3642}
3643
c145e0b4
DH
3644static inline bool should_try_to_free_swap(struct page *page,
3645 struct vm_area_struct *vma,
3646 unsigned int fault_flags)
3647{
3648 if (!PageSwapCache(page))
3649 return false;
3650 if (mem_cgroup_swap_full(page) || (vma->vm_flags & VM_LOCKED) ||
3651 PageMlocked(page))
3652 return true;
3653 /*
3654 * If we want to map a page that's in the swapcache writable, we
3655 * have to detect via the refcount if we're really the exclusive
3656 * user. Try freeing the swapcache to get rid of the swapcache
3657 * reference only in case it's likely that we'll be the exlusive user.
3658 */
3659 return (fault_flags & FAULT_FLAG_WRITE) && !PageKsm(page) &&
3660 page_count(page) == 2;
3661}
3662
9c28a205
PX
3663static vm_fault_t pte_marker_clear(struct vm_fault *vmf)
3664{
3665 vmf->pte = pte_offset_map_lock(vmf->vma->vm_mm, vmf->pmd,
3666 vmf->address, &vmf->ptl);
3667 /*
3668 * Be careful so that we will only recover a special uffd-wp pte into a
3669 * none pte. Otherwise it means the pte could have changed, so retry.
3670 */
3671 if (is_pte_marker(*vmf->pte))
3672 pte_clear(vmf->vma->vm_mm, vmf->address, vmf->pte);
3673 pte_unmap_unlock(vmf->pte, vmf->ptl);
3674 return 0;
3675}
3676
3677/*
3678 * This is actually a page-missing access, but with uffd-wp special pte
3679 * installed. It means this pte was wr-protected before being unmapped.
3680 */
3681static vm_fault_t pte_marker_handle_uffd_wp(struct vm_fault *vmf)
3682{
3683 /*
3684 * Just in case there're leftover special ptes even after the region
3685 * got unregistered - we can simply clear them. We can also do that
3686 * proactively when e.g. when we do UFFDIO_UNREGISTER upon some uffd-wp
3687 * ranges, but it should be more efficient to be done lazily here.
3688 */
3689 if (unlikely(!userfaultfd_wp(vmf->vma) || vma_is_anonymous(vmf->vma)))
3690 return pte_marker_clear(vmf);
3691
3692 /* do_fault() can handle pte markers too like none pte */
3693 return do_fault(vmf);
3694}
3695
5c041f5d
PX
3696static vm_fault_t handle_pte_marker(struct vm_fault *vmf)
3697{
3698 swp_entry_t entry = pte_to_swp_entry(vmf->orig_pte);
3699 unsigned long marker = pte_marker_get(entry);
3700
3701 /*
3702 * PTE markers should always be with file-backed memories, and the
3703 * marker should never be empty. If anything weird happened, the best
3704 * thing to do is to kill the process along with its mm.
3705 */
3706 if (WARN_ON_ONCE(vma_is_anonymous(vmf->vma) || !marker))
3707 return VM_FAULT_SIGBUS;
3708
9c28a205
PX
3709 if (pte_marker_entry_uffd_wp(entry))
3710 return pte_marker_handle_uffd_wp(vmf);
3711
3712 /* This is an unknown pte marker */
3713 return VM_FAULT_SIGBUS;
5c041f5d
PX
3714}
3715
1da177e4 3716/*
c1e8d7c6 3717 * We enter with non-exclusive mmap_lock (to exclude vma changes,
8f4e2101 3718 * but allow concurrent faults), and pte mapped but not yet locked.
9a95f3cf
PC
3719 * We return with pte unmapped and unlocked.
3720 *
c1e8d7c6 3721 * We return with the mmap_lock locked or unlocked in the same cases
9a95f3cf 3722 * as does filemap_fault().
1da177e4 3723 */
2b740303 3724vm_fault_t do_swap_page(struct vm_fault *vmf)
1da177e4 3725{
82b0f8c3 3726 struct vm_area_struct *vma = vmf->vma;
d4f9565a
MWO
3727 struct folio *swapcache, *folio = NULL;
3728 struct page *page;
2799e775 3729 struct swap_info_struct *si = NULL;
14f9135d 3730 rmap_t rmap_flags = RMAP_NONE;
1493a191 3731 bool exclusive = false;
65500d23 3732 swp_entry_t entry;
1da177e4 3733 pte_t pte;
d065bd81 3734 int locked;
2b740303 3735 vm_fault_t ret = 0;
aae466b0 3736 void *shadow = NULL;
1da177e4 3737
2ca99358 3738 if (!pte_unmap_same(vmf))
8f4e2101 3739 goto out;
65500d23 3740
2994302b 3741 entry = pte_to_swp_entry(vmf->orig_pte);
d1737fdb
AK
3742 if (unlikely(non_swap_entry(entry))) {
3743 if (is_migration_entry(entry)) {
82b0f8c3
JK
3744 migration_entry_wait(vma->vm_mm, vmf->pmd,
3745 vmf->address);
b756a3b5
AP
3746 } else if (is_device_exclusive_entry(entry)) {
3747 vmf->page = pfn_swap_entry_to_page(entry);
3748 ret = remove_device_exclusive_entry(vmf);
5042db43 3749 } else if (is_device_private_entry(entry)) {
af5cdaf8 3750 vmf->page = pfn_swap_entry_to_page(entry);
897e6365 3751 ret = vmf->page->pgmap->ops->migrate_to_ram(vmf);
d1737fdb
AK
3752 } else if (is_hwpoison_entry(entry)) {
3753 ret = VM_FAULT_HWPOISON;
9f186f9e
ML
3754 } else if (is_swapin_error_entry(entry)) {
3755 ret = VM_FAULT_SIGBUS;
5c041f5d
PX
3756 } else if (is_pte_marker_entry(entry)) {
3757 ret = handle_pte_marker(vmf);
d1737fdb 3758 } else {
2994302b 3759 print_bad_pte(vma, vmf->address, vmf->orig_pte, NULL);
d99be1a8 3760 ret = VM_FAULT_SIGBUS;
d1737fdb 3761 }
0697212a
CL
3762 goto out;
3763 }
0bcac06f 3764
2799e775
ML
3765 /* Prevent swapoff from happening to us. */
3766 si = get_swap_device(entry);
3767 if (unlikely(!si))
3768 goto out;
0bcac06f 3769
5a423081
MWO
3770 folio = swap_cache_get_folio(entry, vma, vmf->address);
3771 if (folio)
3772 page = folio_file_page(folio, swp_offset(entry));
d4f9565a 3773 swapcache = folio;
f8020772 3774
d4f9565a 3775 if (!folio) {
a449bf58
QC
3776 if (data_race(si->flags & SWP_SYNCHRONOUS_IO) &&
3777 __swap_count(entry) == 1) {
0bcac06f 3778 /* skip swapcache */
63ad4add
MWO
3779 folio = vma_alloc_folio(GFP_HIGHUSER_MOVABLE, 0,
3780 vma, vmf->address, false);
3781 page = &folio->page;
3782 if (folio) {
3783 __folio_set_locked(folio);
3784 __folio_set_swapbacked(folio);
4c6355b2 3785
65995918 3786 if (mem_cgroup_swapin_charge_folio(folio,
63ad4add
MWO
3787 vma->vm_mm, GFP_KERNEL,
3788 entry)) {
545b1b07 3789 ret = VM_FAULT_OOM;
4c6355b2 3790 goto out_page;
545b1b07 3791 }
0add0c77 3792 mem_cgroup_swapin_uncharge_swap(entry);
4c6355b2 3793
aae466b0
JK
3794 shadow = get_shadow_from_swap_cache(entry);
3795 if (shadow)
63ad4add 3796 workingset_refault(folio, shadow);
0076f029 3797
63ad4add 3798 folio_add_lru(folio);
0add0c77
SB
3799
3800 /* To provide entry to swap_readpage() */
63ad4add 3801 folio_set_swap_entry(folio, entry);
5169b844 3802 swap_readpage(page, true, NULL);
63ad4add 3803 folio->private = NULL;
0bcac06f 3804 }
aa8d22a1 3805 } else {
e9e9b7ec
MK
3806 page = swapin_readahead(entry, GFP_HIGHUSER_MOVABLE,
3807 vmf);
63ad4add
MWO
3808 if (page)
3809 folio = page_folio(page);
d4f9565a 3810 swapcache = folio;
0bcac06f
MK
3811 }
3812
d4f9565a 3813 if (!folio) {
1da177e4 3814 /*
8f4e2101
HD
3815 * Back out if somebody else faulted in this pte
3816 * while we released the pte lock.
1da177e4 3817 */
82b0f8c3
JK
3818 vmf->pte = pte_offset_map_lock(vma->vm_mm, vmf->pmd,
3819 vmf->address, &vmf->ptl);
2994302b 3820 if (likely(pte_same(*vmf->pte, vmf->orig_pte)))
1da177e4 3821 ret = VM_FAULT_OOM;
65500d23 3822 goto unlock;
1da177e4
LT
3823 }
3824
3825 /* Had to read the page from swap area: Major fault */
3826 ret = VM_FAULT_MAJOR;
f8891e5e 3827 count_vm_event(PGMAJFAULT);
2262185c 3828 count_memcg_event_mm(vma->vm_mm, PGMAJFAULT);
d1737fdb 3829 } else if (PageHWPoison(page)) {
71f72525
WF
3830 /*
3831 * hwpoisoned dirty swapcache pages are kept for killing
3832 * owner processes (which may be unknown at hwpoison time)
3833 */
d1737fdb 3834 ret = VM_FAULT_HWPOISON;
4779cb31 3835 goto out_release;
1da177e4
LT
3836 }
3837
82b0f8c3 3838 locked = lock_page_or_retry(page, vma->vm_mm, vmf->flags);
e709ffd6 3839
d065bd81
ML
3840 if (!locked) {
3841 ret |= VM_FAULT_RETRY;
3842 goto out_release;
3843 }
073e587e 3844
84d60fdd
DH
3845 if (swapcache) {
3846 /*
3847 * Make sure try_to_free_swap or swapoff did not release the
3848 * swapcache from under us. The page pin, and pte_same test
3849 * below, are not enough to exclude that. Even if it is still
3850 * swapcache, we need to check that the page's swap has not
3851 * changed.
3852 */
63ad4add 3853 if (unlikely(!folio_test_swapcache(folio) ||
84d60fdd
DH
3854 page_private(page) != entry.val))
3855 goto out_page;
3856
3857 /*
3858 * KSM sometimes has to copy on read faults, for example, if
3859 * page->index of !PageKSM() pages would be nonlinear inside the
3860 * anon VMA -- PageKSM() is lost on actual swapout.
3861 */
3862 page = ksm_might_need_to_copy(page, vma, vmf->address);
3863 if (unlikely(!page)) {
3864 ret = VM_FAULT_OOM;
84d60fdd
DH
3865 goto out_page;
3866 }
63ad4add 3867 folio = page_folio(page);
c145e0b4
DH
3868
3869 /*
3870 * If we want to map a page that's in the swapcache writable, we
3871 * have to detect via the refcount if we're really the exclusive
3872 * owner. Try removing the extra reference from the local LRU
3873 * pagevecs if required.
3874 */
d4f9565a 3875 if ((vmf->flags & FAULT_FLAG_WRITE) && folio == swapcache &&
63ad4add 3876 !folio_test_ksm(folio) && !folio_test_lru(folio))
c145e0b4 3877 lru_add_drain();
5ad64688
HD
3878 }
3879
9d82c694 3880 cgroup_throttle_swaprate(page, GFP_KERNEL);
8a9f3ccd 3881
1da177e4 3882 /*
8f4e2101 3883 * Back out if somebody else already faulted in this pte.
1da177e4 3884 */
82b0f8c3
JK
3885 vmf->pte = pte_offset_map_lock(vma->vm_mm, vmf->pmd, vmf->address,
3886 &vmf->ptl);
2994302b 3887 if (unlikely(!pte_same(*vmf->pte, vmf->orig_pte)))
b8107480 3888 goto out_nomap;
b8107480 3889
63ad4add 3890 if (unlikely(!folio_test_uptodate(folio))) {
b8107480
KK
3891 ret = VM_FAULT_SIGBUS;
3892 goto out_nomap;
1da177e4
LT
3893 }
3894
78fbe906
DH
3895 /*
3896 * PG_anon_exclusive reuses PG_mappedtodisk for anon pages. A swap pte
3897 * must never point at an anonymous page in the swapcache that is
3898 * PG_anon_exclusive. Sanity check that this holds and especially, that
3899 * no filesystem set PG_mappedtodisk on a page in the swapcache. Sanity
3900 * check after taking the PT lock and making sure that nobody
3901 * concurrently faulted in this page and set PG_anon_exclusive.
3902 */
63ad4add
MWO
3903 BUG_ON(!folio_test_anon(folio) && folio_test_mappedtodisk(folio));
3904 BUG_ON(folio_test_anon(folio) && PageAnonExclusive(page));
78fbe906 3905
1493a191
DH
3906 /*
3907 * Check under PT lock (to protect against concurrent fork() sharing
3908 * the swap entry concurrently) for certainly exclusive pages.
3909 */
63ad4add 3910 if (!folio_test_ksm(folio)) {
1493a191
DH
3911 /*
3912 * Note that pte_swp_exclusive() == false for architectures
3913 * without __HAVE_ARCH_PTE_SWP_EXCLUSIVE.
3914 */
3915 exclusive = pte_swp_exclusive(vmf->orig_pte);
d4f9565a 3916 if (folio != swapcache) {
1493a191
DH
3917 /*
3918 * We have a fresh page that is not exposed to the
3919 * swapcache -> certainly exclusive.
3920 */
3921 exclusive = true;
63ad4add 3922 } else if (exclusive && folio_test_writeback(folio) &&
eacde327 3923 data_race(si->flags & SWP_STABLE_WRITES)) {
1493a191
DH
3924 /*
3925 * This is tricky: not all swap backends support
3926 * concurrent page modifications while under writeback.
3927 *
3928 * So if we stumble over such a page in the swapcache
3929 * we must not set the page exclusive, otherwise we can
3930 * map it writable without further checks and modify it
3931 * while still under writeback.
3932 *
3933 * For these problematic swap backends, simply drop the
3934 * exclusive marker: this is perfectly fine as we start
3935 * writeback only if we fully unmapped the page and
3936 * there are no unexpected references on the page after
3937 * unmapping succeeded. After fully unmapped, no
3938 * further GUP references (FOLL_GET and FOLL_PIN) can
3939 * appear, so dropping the exclusive marker and mapping
3940 * it only R/O is fine.
3941 */
3942 exclusive = false;
3943 }
3944 }
3945
8c7c6e34 3946 /*
c145e0b4
DH
3947 * Remove the swap entry and conditionally try to free up the swapcache.
3948 * We're already holding a reference on the page but haven't mapped it
3949 * yet.
8c7c6e34 3950 */
c145e0b4
DH
3951 swap_free(entry);
3952 if (should_try_to_free_swap(page, vma, vmf->flags))
3953 try_to_free_swap(page);
1da177e4 3954
bae473a4
KS
3955 inc_mm_counter_fast(vma->vm_mm, MM_ANONPAGES);
3956 dec_mm_counter_fast(vma->vm_mm, MM_SWAPENTS);
1da177e4 3957 pte = mk_pte(page, vma->vm_page_prot);
c145e0b4
DH
3958
3959 /*
1493a191
DH
3960 * Same logic as in do_wp_page(); however, optimize for pages that are
3961 * certainly not shared either because we just allocated them without
3962 * exposing them to the swapcache or because the swap entry indicates
3963 * exclusivity.
c145e0b4 3964 */
63ad4add
MWO
3965 if (!folio_test_ksm(folio) &&
3966 (exclusive || folio_ref_count(folio) == 1)) {
6c287605
DH
3967 if (vmf->flags & FAULT_FLAG_WRITE) {
3968 pte = maybe_mkwrite(pte_mkdirty(pte), vma);
3969 vmf->flags &= ~FAULT_FLAG_WRITE;
3970 ret |= VM_FAULT_WRITE;
3971 }
14f9135d 3972 rmap_flags |= RMAP_EXCLUSIVE;
1da177e4 3973 }
1da177e4 3974 flush_icache_page(vma, page);
2994302b 3975 if (pte_swp_soft_dirty(vmf->orig_pte))
179ef71c 3976 pte = pte_mksoft_dirty(pte);
f45ec5ff
PX
3977 if (pte_swp_uffd_wp(vmf->orig_pte)) {
3978 pte = pte_mkuffd_wp(pte);
3979 pte = pte_wrprotect(pte);
3980 }
2994302b 3981 vmf->orig_pte = pte;
0bcac06f
MK
3982
3983 /* ksm created a completely new copy */
d4f9565a 3984 if (unlikely(folio != swapcache && swapcache)) {
40f2bbf7 3985 page_add_new_anon_rmap(page, vma, vmf->address);
63ad4add 3986 folio_add_lru_vma(folio, vma);
0bcac06f 3987 } else {
f1e2db12 3988 page_add_anon_rmap(page, vma, vmf->address, rmap_flags);
00501b53 3989 }
1da177e4 3990
63ad4add
MWO
3991 VM_BUG_ON(!folio_test_anon(folio) ||
3992 (pte_write(pte) && !PageAnonExclusive(page)));
1eba86c0
PT
3993 set_pte_at(vma->vm_mm, vmf->address, vmf->pte, pte);
3994 arch_do_swap_page(vma->vm_mm, vma, vmf->address, pte, vmf->orig_pte);
3995
63ad4add 3996 folio_unlock(folio);
d4f9565a 3997 if (folio != swapcache && swapcache) {
4969c119
AA
3998 /*
3999 * Hold the lock to avoid the swap entry to be reused
4000 * until we take the PT lock for the pte_same() check
4001 * (to avoid false positives from pte_same). For
4002 * further safety release the lock after the swap_free
4003 * so that the swap count won't change under a
4004 * parallel locked swapcache.
4005 */
d4f9565a
MWO
4006 folio_unlock(swapcache);
4007 folio_put(swapcache);
4969c119 4008 }
c475a8ab 4009
82b0f8c3 4010 if (vmf->flags & FAULT_FLAG_WRITE) {
2994302b 4011 ret |= do_wp_page(vmf);
61469f1d
HD
4012 if (ret & VM_FAULT_ERROR)
4013 ret &= VM_FAULT_ERROR;
1da177e4
LT
4014 goto out;
4015 }
4016
4017 /* No need to invalidate - it was non-present before */
82b0f8c3 4018 update_mmu_cache(vma, vmf->address, vmf->pte);
65500d23 4019unlock:
82b0f8c3 4020 pte_unmap_unlock(vmf->pte, vmf->ptl);
1da177e4 4021out:
2799e775
ML
4022 if (si)
4023 put_swap_device(si);
1da177e4 4024 return ret;
b8107480 4025out_nomap:
82b0f8c3 4026 pte_unmap_unlock(vmf->pte, vmf->ptl);
bc43f75c 4027out_page:
63ad4add 4028 folio_unlock(folio);
4779cb31 4029out_release:
63ad4add 4030 folio_put(folio);
d4f9565a
MWO
4031 if (folio != swapcache && swapcache) {
4032 folio_unlock(swapcache);
4033 folio_put(swapcache);
4969c119 4034 }
2799e775
ML
4035 if (si)
4036 put_swap_device(si);
65500d23 4037 return ret;
1da177e4
LT
4038}
4039
4040/*
c1e8d7c6 4041 * We enter with non-exclusive mmap_lock (to exclude vma changes,
8f4e2101 4042 * but allow concurrent faults), and pte mapped but not yet locked.
c1e8d7c6 4043 * We return with mmap_lock still held, but pte unmapped and unlocked.
1da177e4 4044 */
2b740303 4045static vm_fault_t do_anonymous_page(struct vm_fault *vmf)
1da177e4 4046{
82b0f8c3 4047 struct vm_area_struct *vma = vmf->vma;
8f4e2101 4048 struct page *page;
2b740303 4049 vm_fault_t ret = 0;
1da177e4 4050 pte_t entry;
1da177e4 4051
6b7339f4
KS
4052 /* File mapping without ->vm_ops ? */
4053 if (vma->vm_flags & VM_SHARED)
4054 return VM_FAULT_SIGBUS;
4055
7267ec00
KS
4056 /*
4057 * Use pte_alloc() instead of pte_alloc_map(). We can't run
4058 * pte_offset_map() on pmds where a huge pmd might be created
4059 * from a different thread.
4060 *
3e4e28c5 4061 * pte_alloc_map() is safe to use under mmap_write_lock(mm) or when
7267ec00
KS
4062 * parallel threads are excluded by other means.
4063 *
3e4e28c5 4064 * Here we only have mmap_read_lock(mm).
7267ec00 4065 */
4cf58924 4066 if (pte_alloc(vma->vm_mm, vmf->pmd))
7267ec00
KS
4067 return VM_FAULT_OOM;
4068
f9ce0be7 4069 /* See comment in handle_pte_fault() */
82b0f8c3 4070 if (unlikely(pmd_trans_unstable(vmf->pmd)))
7267ec00
KS
4071 return 0;
4072
11ac5524 4073 /* Use the zero-page for reads */
82b0f8c3 4074 if (!(vmf->flags & FAULT_FLAG_WRITE) &&
bae473a4 4075 !mm_forbids_zeropage(vma->vm_mm)) {
82b0f8c3 4076 entry = pte_mkspecial(pfn_pte(my_zero_pfn(vmf->address),
62eede62 4077 vma->vm_page_prot));
82b0f8c3
JK
4078 vmf->pte = pte_offset_map_lock(vma->vm_mm, vmf->pmd,
4079 vmf->address, &vmf->ptl);
7df67697
BM
4080 if (!pte_none(*vmf->pte)) {
4081 update_mmu_tlb(vma, vmf->address, vmf->pte);
a13ea5b7 4082 goto unlock;
7df67697 4083 }
6b31d595
MH
4084 ret = check_stable_address_space(vma->vm_mm);
4085 if (ret)
4086 goto unlock;
6b251fc9
AA
4087 /* Deliver the page fault to userland, check inside PT lock */
4088 if (userfaultfd_missing(vma)) {
82b0f8c3
JK
4089 pte_unmap_unlock(vmf->pte, vmf->ptl);
4090 return handle_userfault(vmf, VM_UFFD_MISSING);
6b251fc9 4091 }
a13ea5b7
HD
4092 goto setpte;
4093 }
4094
557ed1fa 4095 /* Allocate our own private page. */
557ed1fa
NP
4096 if (unlikely(anon_vma_prepare(vma)))
4097 goto oom;
82b0f8c3 4098 page = alloc_zeroed_user_highpage_movable(vma, vmf->address);
557ed1fa
NP
4099 if (!page)
4100 goto oom;
eb3c24f3 4101
8f425e4e 4102 if (mem_cgroup_charge(page_folio(page), vma->vm_mm, GFP_KERNEL))
eb3c24f3 4103 goto oom_free_page;
9d82c694 4104 cgroup_throttle_swaprate(page, GFP_KERNEL);
eb3c24f3 4105
52f37629
MK
4106 /*
4107 * The memory barrier inside __SetPageUptodate makes sure that
f4f5329d 4108 * preceding stores to the page contents become visible before
52f37629
MK
4109 * the set_pte_at() write.
4110 */
0ed361de 4111 __SetPageUptodate(page);
8f4e2101 4112
557ed1fa 4113 entry = mk_pte(page, vma->vm_page_prot);
50c25ee9 4114 entry = pte_sw_mkyoung(entry);
1ac0cb5d
HD
4115 if (vma->vm_flags & VM_WRITE)
4116 entry = pte_mkwrite(pte_mkdirty(entry));
1da177e4 4117
82b0f8c3
JK
4118 vmf->pte = pte_offset_map_lock(vma->vm_mm, vmf->pmd, vmf->address,
4119 &vmf->ptl);
7df67697
BM
4120 if (!pte_none(*vmf->pte)) {
4121 update_mmu_cache(vma, vmf->address, vmf->pte);
557ed1fa 4122 goto release;
7df67697 4123 }
9ba69294 4124
6b31d595
MH
4125 ret = check_stable_address_space(vma->vm_mm);
4126 if (ret)
4127 goto release;
4128
6b251fc9
AA
4129 /* Deliver the page fault to userland, check inside PT lock */
4130 if (userfaultfd_missing(vma)) {
82b0f8c3 4131 pte_unmap_unlock(vmf->pte, vmf->ptl);
09cbfeaf 4132 put_page(page);
82b0f8c3 4133 return handle_userfault(vmf, VM_UFFD_MISSING);
6b251fc9
AA
4134 }
4135
bae473a4 4136 inc_mm_counter_fast(vma->vm_mm, MM_ANONPAGES);
40f2bbf7 4137 page_add_new_anon_rmap(page, vma, vmf->address);
b518154e 4138 lru_cache_add_inactive_or_unevictable(page, vma);
a13ea5b7 4139setpte:
82b0f8c3 4140 set_pte_at(vma->vm_mm, vmf->address, vmf->pte, entry);
1da177e4
LT
4141
4142 /* No need to invalidate - it was non-present before */
82b0f8c3 4143 update_mmu_cache(vma, vmf->address, vmf->pte);
65500d23 4144unlock:
82b0f8c3 4145 pte_unmap_unlock(vmf->pte, vmf->ptl);
6b31d595 4146 return ret;
8f4e2101 4147release:
09cbfeaf 4148 put_page(page);
8f4e2101 4149 goto unlock;
8a9f3ccd 4150oom_free_page:
09cbfeaf 4151 put_page(page);
65500d23 4152oom:
1da177e4
LT
4153 return VM_FAULT_OOM;
4154}
4155
9a95f3cf 4156/*
c1e8d7c6 4157 * The mmap_lock must have been held on entry, and may have been
9a95f3cf
PC
4158 * released depending on flags and vma->vm_ops->fault() return value.
4159 * See filemap_fault() and __lock_page_retry().
4160 */
2b740303 4161static vm_fault_t __do_fault(struct vm_fault *vmf)
7eae74af 4162{
82b0f8c3 4163 struct vm_area_struct *vma = vmf->vma;
2b740303 4164 vm_fault_t ret;
7eae74af 4165
63f3655f
MH
4166 /*
4167 * Preallocate pte before we take page_lock because this might lead to
4168 * deadlocks for memcg reclaim which waits for pages under writeback:
4169 * lock_page(A)
4170 * SetPageWriteback(A)
4171 * unlock_page(A)
4172 * lock_page(B)
4173 * lock_page(B)
d383807a 4174 * pte_alloc_one
63f3655f
MH
4175 * shrink_page_list
4176 * wait_on_page_writeback(A)
4177 * SetPageWriteback(B)
4178 * unlock_page(B)
4179 * # flush A, B to clear the writeback
4180 */
4181 if (pmd_none(*vmf->pmd) && !vmf->prealloc_pte) {
a7069ee3 4182 vmf->prealloc_pte = pte_alloc_one(vma->vm_mm);
63f3655f
MH
4183 if (!vmf->prealloc_pte)
4184 return VM_FAULT_OOM;
63f3655f
MH
4185 }
4186
11bac800 4187 ret = vma->vm_ops->fault(vmf);
3917048d 4188 if (unlikely(ret & (VM_FAULT_ERROR | VM_FAULT_NOPAGE | VM_FAULT_RETRY |
b1aa812b 4189 VM_FAULT_DONE_COW)))
bc2466e4 4190 return ret;
7eae74af 4191
667240e0 4192 if (unlikely(PageHWPoison(vmf->page))) {
3149c79f 4193 struct page *page = vmf->page;
e53ac737
RR
4194 vm_fault_t poisonret = VM_FAULT_HWPOISON;
4195 if (ret & VM_FAULT_LOCKED) {
3149c79f
RR
4196 if (page_mapped(page))
4197 unmap_mapping_pages(page_mapping(page),
4198 page->index, 1, false);
e53ac737 4199 /* Retry if a clean page was removed from the cache. */
3149c79f
RR
4200 if (invalidate_inode_page(page))
4201 poisonret = VM_FAULT_NOPAGE;
4202 unlock_page(page);
e53ac737 4203 }
3149c79f 4204 put_page(page);
936ca80d 4205 vmf->page = NULL;
e53ac737 4206 return poisonret;
7eae74af
KS
4207 }
4208
4209 if (unlikely(!(ret & VM_FAULT_LOCKED)))
667240e0 4210 lock_page(vmf->page);
7eae74af 4211 else
667240e0 4212 VM_BUG_ON_PAGE(!PageLocked(vmf->page), vmf->page);
7eae74af 4213
7eae74af
KS
4214 return ret;
4215}
4216
396bcc52 4217#ifdef CONFIG_TRANSPARENT_HUGEPAGE
82b0f8c3 4218static void deposit_prealloc_pte(struct vm_fault *vmf)
953c66c2 4219{
82b0f8c3 4220 struct vm_area_struct *vma = vmf->vma;
953c66c2 4221
82b0f8c3 4222 pgtable_trans_huge_deposit(vma->vm_mm, vmf->pmd, vmf->prealloc_pte);
953c66c2
AK
4223 /*
4224 * We are going to consume the prealloc table,
4225 * count that as nr_ptes.
4226 */
c4812909 4227 mm_inc_nr_ptes(vma->vm_mm);
7f2b6ce8 4228 vmf->prealloc_pte = NULL;
953c66c2
AK
4229}
4230
f9ce0be7 4231vm_fault_t do_set_pmd(struct vm_fault *vmf, struct page *page)
10102459 4232{
82b0f8c3
JK
4233 struct vm_area_struct *vma = vmf->vma;
4234 bool write = vmf->flags & FAULT_FLAG_WRITE;
4235 unsigned long haddr = vmf->address & HPAGE_PMD_MASK;
10102459 4236 pmd_t entry;
2b740303 4237 int i;
d01ac3c3 4238 vm_fault_t ret = VM_FAULT_FALLBACK;
10102459
KS
4239
4240 if (!transhuge_vma_suitable(vma, haddr))
d01ac3c3 4241 return ret;
10102459 4242
10102459 4243 page = compound_head(page);
d01ac3c3
MWO
4244 if (compound_order(page) != HPAGE_PMD_ORDER)
4245 return ret;
10102459 4246
eac96c3e
YS
4247 /*
4248 * Just backoff if any subpage of a THP is corrupted otherwise
4249 * the corrupted page may mapped by PMD silently to escape the
4250 * check. This kind of THP just can be PTE mapped. Access to
4251 * the corrupted subpage should trigger SIGBUS as expected.
4252 */
4253 if (unlikely(PageHasHWPoisoned(page)))
4254 return ret;
4255
953c66c2 4256 /*
f0953a1b 4257 * Archs like ppc64 need additional space to store information
953c66c2
AK
4258 * related to pte entry. Use the preallocated table for that.
4259 */
82b0f8c3 4260 if (arch_needs_pgtable_deposit() && !vmf->prealloc_pte) {
4cf58924 4261 vmf->prealloc_pte = pte_alloc_one(vma->vm_mm);
82b0f8c3 4262 if (!vmf->prealloc_pte)
953c66c2 4263 return VM_FAULT_OOM;
953c66c2
AK
4264 }
4265
82b0f8c3
JK
4266 vmf->ptl = pmd_lock(vma->vm_mm, vmf->pmd);
4267 if (unlikely(!pmd_none(*vmf->pmd)))
10102459
KS
4268 goto out;
4269
4270 for (i = 0; i < HPAGE_PMD_NR; i++)
4271 flush_icache_page(vma, page + i);
4272
4273 entry = mk_huge_pmd(page, vma->vm_page_prot);
4274 if (write)
f55e1014 4275 entry = maybe_pmd_mkwrite(pmd_mkdirty(entry), vma);
10102459 4276
fadae295 4277 add_mm_counter(vma->vm_mm, mm_counter_file(page), HPAGE_PMD_NR);
cea86fe2
HD
4278 page_add_file_rmap(page, vma, true);
4279
953c66c2
AK
4280 /*
4281 * deposit and withdraw with pmd lock held
4282 */
4283 if (arch_needs_pgtable_deposit())
82b0f8c3 4284 deposit_prealloc_pte(vmf);
10102459 4285
82b0f8c3 4286 set_pmd_at(vma->vm_mm, haddr, vmf->pmd, entry);
10102459 4287
82b0f8c3 4288 update_mmu_cache_pmd(vma, haddr, vmf->pmd);
10102459
KS
4289
4290 /* fault is handled */
4291 ret = 0;
95ecedcd 4292 count_vm_event(THP_FILE_MAPPED);
10102459 4293out:
82b0f8c3 4294 spin_unlock(vmf->ptl);
10102459
KS
4295 return ret;
4296}
4297#else
f9ce0be7 4298vm_fault_t do_set_pmd(struct vm_fault *vmf, struct page *page)
10102459 4299{
f9ce0be7 4300 return VM_FAULT_FALLBACK;
10102459
KS
4301}
4302#endif
4303
9d3af4b4 4304void do_set_pte(struct vm_fault *vmf, struct page *page, unsigned long addr)
3bb97794 4305{
82b0f8c3 4306 struct vm_area_struct *vma = vmf->vma;
9c28a205 4307 bool uffd_wp = pte_marker_uffd_wp(vmf->orig_pte);
82b0f8c3 4308 bool write = vmf->flags & FAULT_FLAG_WRITE;
9d3af4b4 4309 bool prefault = vmf->address != addr;
3bb97794 4310 pte_t entry;
7267ec00 4311
3bb97794
KS
4312 flush_icache_page(vma, page);
4313 entry = mk_pte(page, vma->vm_page_prot);
46bdb427
WD
4314
4315 if (prefault && arch_wants_old_prefaulted_pte())
4316 entry = pte_mkold(entry);
50c25ee9
TB
4317 else
4318 entry = pte_sw_mkyoung(entry);
46bdb427 4319
3bb97794
KS
4320 if (write)
4321 entry = maybe_mkwrite(pte_mkdirty(entry), vma);
9c28a205
PX
4322 if (unlikely(uffd_wp))
4323 entry = pte_mkuffd_wp(pte_wrprotect(entry));
bae473a4
KS
4324 /* copy-on-write page */
4325 if (write && !(vma->vm_flags & VM_SHARED)) {
3bb97794 4326 inc_mm_counter_fast(vma->vm_mm, MM_ANONPAGES);
40f2bbf7 4327 page_add_new_anon_rmap(page, vma, addr);
b518154e 4328 lru_cache_add_inactive_or_unevictable(page, vma);
3bb97794 4329 } else {
eca56ff9 4330 inc_mm_counter_fast(vma->vm_mm, mm_counter_file(page));
cea86fe2 4331 page_add_file_rmap(page, vma, false);
3bb97794 4332 }
9d3af4b4 4333 set_pte_at(vma->vm_mm, addr, vmf->pte, entry);
3bb97794
KS
4334}
4335
f46f2ade
PX
4336static bool vmf_pte_changed(struct vm_fault *vmf)
4337{
4338 if (vmf->flags & FAULT_FLAG_ORIG_PTE_VALID)
4339 return !pte_same(*vmf->pte, vmf->orig_pte);
4340
4341 return !pte_none(*vmf->pte);
4342}
4343
9118c0cb
JK
4344/**
4345 * finish_fault - finish page fault once we have prepared the page to fault
4346 *
4347 * @vmf: structure describing the fault
4348 *
4349 * This function handles all that is needed to finish a page fault once the
4350 * page to fault in is prepared. It handles locking of PTEs, inserts PTE for
4351 * given page, adds reverse page mapping, handles memcg charges and LRU
a862f68a 4352 * addition.
9118c0cb
JK
4353 *
4354 * The function expects the page to be locked and on success it consumes a
4355 * reference of a page being mapped (for the PTE which maps it).
a862f68a
MR
4356 *
4357 * Return: %0 on success, %VM_FAULT_ code in case of error.
9118c0cb 4358 */
2b740303 4359vm_fault_t finish_fault(struct vm_fault *vmf)
9118c0cb 4360{
f9ce0be7 4361 struct vm_area_struct *vma = vmf->vma;
9118c0cb 4362 struct page *page;
f9ce0be7 4363 vm_fault_t ret;
9118c0cb
JK
4364
4365 /* Did we COW the page? */
f9ce0be7 4366 if ((vmf->flags & FAULT_FLAG_WRITE) && !(vma->vm_flags & VM_SHARED))
9118c0cb
JK
4367 page = vmf->cow_page;
4368 else
4369 page = vmf->page;
6b31d595
MH
4370
4371 /*
4372 * check even for read faults because we might have lost our CoWed
4373 * page
4374 */
f9ce0be7
KS
4375 if (!(vma->vm_flags & VM_SHARED)) {
4376 ret = check_stable_address_space(vma->vm_mm);
4377 if (ret)
4378 return ret;
4379 }
4380
4381 if (pmd_none(*vmf->pmd)) {
4382 if (PageTransCompound(page)) {
4383 ret = do_set_pmd(vmf, page);
4384 if (ret != VM_FAULT_FALLBACK)
4385 return ret;
4386 }
4387
03c4f204
QZ
4388 if (vmf->prealloc_pte)
4389 pmd_install(vma->vm_mm, vmf->pmd, &vmf->prealloc_pte);
4390 else if (unlikely(pte_alloc(vma->vm_mm, vmf->pmd)))
f9ce0be7
KS
4391 return VM_FAULT_OOM;
4392 }
4393
3fe2895c
JB
4394 /*
4395 * See comment in handle_pte_fault() for how this scenario happens, we
4396 * need to return NOPAGE so that we drop this page.
4397 */
f9ce0be7 4398 if (pmd_devmap_trans_unstable(vmf->pmd))
3fe2895c 4399 return VM_FAULT_NOPAGE;
f9ce0be7
KS
4400
4401 vmf->pte = pte_offset_map_lock(vma->vm_mm, vmf->pmd,
4402 vmf->address, &vmf->ptl);
70427f6e 4403
f9ce0be7 4404 /* Re-check under ptl */
70427f6e 4405 if (likely(!vmf_pte_changed(vmf))) {
9d3af4b4 4406 do_set_pte(vmf, page, vmf->address);
70427f6e
SA
4407
4408 /* no need to invalidate: a not-present page won't be cached */
4409 update_mmu_cache(vma, vmf->address, vmf->pte);
4410
4411 ret = 0;
4412 } else {
4413 update_mmu_tlb(vma, vmf->address, vmf->pte);
f9ce0be7 4414 ret = VM_FAULT_NOPAGE;
70427f6e 4415 }
f9ce0be7 4416
f9ce0be7 4417 pte_unmap_unlock(vmf->pte, vmf->ptl);
9118c0cb
JK
4418 return ret;
4419}
4420
3a91053a
KS
4421static unsigned long fault_around_bytes __read_mostly =
4422 rounddown_pow_of_two(65536);
a9b0f861 4423
a9b0f861
KS
4424#ifdef CONFIG_DEBUG_FS
4425static int fault_around_bytes_get(void *data, u64 *val)
1592eef0 4426{
a9b0f861 4427 *val = fault_around_bytes;
1592eef0
KS
4428 return 0;
4429}
4430
b4903d6e 4431/*
da391d64
WK
4432 * fault_around_bytes must be rounded down to the nearest page order as it's
4433 * what do_fault_around() expects to see.
b4903d6e 4434 */
a9b0f861 4435static int fault_around_bytes_set(void *data, u64 val)
1592eef0 4436{
a9b0f861 4437 if (val / PAGE_SIZE > PTRS_PER_PTE)
1592eef0 4438 return -EINVAL;
b4903d6e
AR
4439 if (val > PAGE_SIZE)
4440 fault_around_bytes = rounddown_pow_of_two(val);
4441 else
4442 fault_around_bytes = PAGE_SIZE; /* rounddown_pow_of_two(0) is undefined */
1592eef0
KS
4443 return 0;
4444}
0a1345f8 4445DEFINE_DEBUGFS_ATTRIBUTE(fault_around_bytes_fops,
a9b0f861 4446 fault_around_bytes_get, fault_around_bytes_set, "%llu\n");
1592eef0
KS
4447
4448static int __init fault_around_debugfs(void)
4449{
d9f7979c
GKH
4450 debugfs_create_file_unsafe("fault_around_bytes", 0644, NULL, NULL,
4451 &fault_around_bytes_fops);
1592eef0
KS
4452 return 0;
4453}
4454late_initcall(fault_around_debugfs);
1592eef0 4455#endif
8c6e50b0 4456
1fdb412b
KS
4457/*
4458 * do_fault_around() tries to map few pages around the fault address. The hope
4459 * is that the pages will be needed soon and this will lower the number of
4460 * faults to handle.
4461 *
4462 * It uses vm_ops->map_pages() to map the pages, which skips the page if it's
4463 * not ready to be mapped: not up-to-date, locked, etc.
4464 *
1fdb412b
KS
4465 * This function doesn't cross the VMA boundaries, in order to call map_pages()
4466 * only once.
4467 *
da391d64
WK
4468 * fault_around_bytes defines how many bytes we'll try to map.
4469 * do_fault_around() expects it to be set to a power of two less than or equal
4470 * to PTRS_PER_PTE.
1fdb412b 4471 *
da391d64
WK
4472 * The virtual address of the area that we map is naturally aligned to
4473 * fault_around_bytes rounded down to the machine page size
4474 * (and therefore to page order). This way it's easier to guarantee
4475 * that we don't cross page table boundaries.
1fdb412b 4476 */
2b740303 4477static vm_fault_t do_fault_around(struct vm_fault *vmf)
8c6e50b0 4478{
82b0f8c3 4479 unsigned long address = vmf->address, nr_pages, mask;
0721ec8b 4480 pgoff_t start_pgoff = vmf->pgoff;
bae473a4 4481 pgoff_t end_pgoff;
2b740303 4482 int off;
8c6e50b0 4483
4db0c3c2 4484 nr_pages = READ_ONCE(fault_around_bytes) >> PAGE_SHIFT;
aecd6f44
KS
4485 mask = ~(nr_pages * PAGE_SIZE - 1) & PAGE_MASK;
4486
f9ce0be7
KS
4487 address = max(address & mask, vmf->vma->vm_start);
4488 off = ((vmf->address - address) >> PAGE_SHIFT) & (PTRS_PER_PTE - 1);
bae473a4 4489 start_pgoff -= off;
8c6e50b0
KS
4490
4491 /*
da391d64
WK
4492 * end_pgoff is either the end of the page table, the end of
4493 * the vma or nr_pages from start_pgoff, depending what is nearest.
8c6e50b0 4494 */
bae473a4 4495 end_pgoff = start_pgoff -
f9ce0be7 4496 ((address >> PAGE_SHIFT) & (PTRS_PER_PTE - 1)) +
8c6e50b0 4497 PTRS_PER_PTE - 1;
82b0f8c3 4498 end_pgoff = min3(end_pgoff, vma_pages(vmf->vma) + vmf->vma->vm_pgoff - 1,
bae473a4 4499 start_pgoff + nr_pages - 1);
8c6e50b0 4500
82b0f8c3 4501 if (pmd_none(*vmf->pmd)) {
4cf58924 4502 vmf->prealloc_pte = pte_alloc_one(vmf->vma->vm_mm);
82b0f8c3 4503 if (!vmf->prealloc_pte)
f9ce0be7 4504 return VM_FAULT_OOM;
8c6e50b0
KS
4505 }
4506
f9ce0be7 4507 return vmf->vma->vm_ops->map_pages(vmf, start_pgoff, end_pgoff);
8c6e50b0
KS
4508}
4509
9c28a205
PX
4510/* Return true if we should do read fault-around, false otherwise */
4511static inline bool should_fault_around(struct vm_fault *vmf)
4512{
4513 /* No ->map_pages? No way to fault around... */
4514 if (!vmf->vma->vm_ops->map_pages)
4515 return false;
4516
4517 if (uffd_disable_fault_around(vmf->vma))
4518 return false;
4519
4520 return fault_around_bytes >> PAGE_SHIFT > 1;
4521}
4522
2b740303 4523static vm_fault_t do_read_fault(struct vm_fault *vmf)
e655fb29 4524{
2b740303 4525 vm_fault_t ret = 0;
8c6e50b0
KS
4526
4527 /*
4528 * Let's call ->map_pages() first and use ->fault() as fallback
4529 * if page by the offset is not ready to be mapped (cold cache or
4530 * something).
4531 */
9c28a205
PX
4532 if (should_fault_around(vmf)) {
4533 ret = do_fault_around(vmf);
4534 if (ret)
4535 return ret;
8c6e50b0 4536 }
e655fb29 4537
936ca80d 4538 ret = __do_fault(vmf);
e655fb29
KS
4539 if (unlikely(ret & (VM_FAULT_ERROR | VM_FAULT_NOPAGE | VM_FAULT_RETRY)))
4540 return ret;
4541
9118c0cb 4542 ret |= finish_fault(vmf);
936ca80d 4543 unlock_page(vmf->page);
7267ec00 4544 if (unlikely(ret & (VM_FAULT_ERROR | VM_FAULT_NOPAGE | VM_FAULT_RETRY)))
936ca80d 4545 put_page(vmf->page);
e655fb29
KS
4546 return ret;
4547}
4548
2b740303 4549static vm_fault_t do_cow_fault(struct vm_fault *vmf)
ec47c3b9 4550{
82b0f8c3 4551 struct vm_area_struct *vma = vmf->vma;
2b740303 4552 vm_fault_t ret;
ec47c3b9
KS
4553
4554 if (unlikely(anon_vma_prepare(vma)))
4555 return VM_FAULT_OOM;
4556
936ca80d
JK
4557 vmf->cow_page = alloc_page_vma(GFP_HIGHUSER_MOVABLE, vma, vmf->address);
4558 if (!vmf->cow_page)
ec47c3b9
KS
4559 return VM_FAULT_OOM;
4560
8f425e4e
MWO
4561 if (mem_cgroup_charge(page_folio(vmf->cow_page), vma->vm_mm,
4562 GFP_KERNEL)) {
936ca80d 4563 put_page(vmf->cow_page);
ec47c3b9
KS
4564 return VM_FAULT_OOM;
4565 }
9d82c694 4566 cgroup_throttle_swaprate(vmf->cow_page, GFP_KERNEL);
ec47c3b9 4567
936ca80d 4568 ret = __do_fault(vmf);
ec47c3b9
KS
4569 if (unlikely(ret & (VM_FAULT_ERROR | VM_FAULT_NOPAGE | VM_FAULT_RETRY)))
4570 goto uncharge_out;
3917048d
JK
4571 if (ret & VM_FAULT_DONE_COW)
4572 return ret;
ec47c3b9 4573
b1aa812b 4574 copy_user_highpage(vmf->cow_page, vmf->page, vmf->address, vma);
936ca80d 4575 __SetPageUptodate(vmf->cow_page);
ec47c3b9 4576
9118c0cb 4577 ret |= finish_fault(vmf);
b1aa812b
JK
4578 unlock_page(vmf->page);
4579 put_page(vmf->page);
7267ec00
KS
4580 if (unlikely(ret & (VM_FAULT_ERROR | VM_FAULT_NOPAGE | VM_FAULT_RETRY)))
4581 goto uncharge_out;
ec47c3b9
KS
4582 return ret;
4583uncharge_out:
936ca80d 4584 put_page(vmf->cow_page);
ec47c3b9
KS
4585 return ret;
4586}
4587
2b740303 4588static vm_fault_t do_shared_fault(struct vm_fault *vmf)
1da177e4 4589{
82b0f8c3 4590 struct vm_area_struct *vma = vmf->vma;
2b740303 4591 vm_fault_t ret, tmp;
1d65f86d 4592
936ca80d 4593 ret = __do_fault(vmf);
7eae74af 4594 if (unlikely(ret & (VM_FAULT_ERROR | VM_FAULT_NOPAGE | VM_FAULT_RETRY)))
f0c6d4d2 4595 return ret;
1da177e4
LT
4596
4597 /*
f0c6d4d2
KS
4598 * Check if the backing address space wants to know that the page is
4599 * about to become writable
1da177e4 4600 */
fb09a464 4601 if (vma->vm_ops->page_mkwrite) {
936ca80d 4602 unlock_page(vmf->page);
38b8cb7f 4603 tmp = do_page_mkwrite(vmf);
fb09a464
KS
4604 if (unlikely(!tmp ||
4605 (tmp & (VM_FAULT_ERROR | VM_FAULT_NOPAGE)))) {
936ca80d 4606 put_page(vmf->page);
fb09a464 4607 return tmp;
4294621f 4608 }
fb09a464
KS
4609 }
4610
9118c0cb 4611 ret |= finish_fault(vmf);
7267ec00
KS
4612 if (unlikely(ret & (VM_FAULT_ERROR | VM_FAULT_NOPAGE |
4613 VM_FAULT_RETRY))) {
936ca80d
JK
4614 unlock_page(vmf->page);
4615 put_page(vmf->page);
f0c6d4d2 4616 return ret;
1da177e4 4617 }
b827e496 4618
89b15332 4619 ret |= fault_dirty_shared_page(vmf);
1d65f86d 4620 return ret;
54cb8821 4621}
d00806b1 4622
9a95f3cf 4623/*
c1e8d7c6 4624 * We enter with non-exclusive mmap_lock (to exclude vma changes,
9a95f3cf 4625 * but allow concurrent faults).
c1e8d7c6 4626 * The mmap_lock may have been released depending on flags and our
9138e47e 4627 * return value. See filemap_fault() and __folio_lock_or_retry().
c1e8d7c6 4628 * If mmap_lock is released, vma may become invalid (for example
fc8efd2d 4629 * by other thread calling munmap()).
9a95f3cf 4630 */
2b740303 4631static vm_fault_t do_fault(struct vm_fault *vmf)
54cb8821 4632{
82b0f8c3 4633 struct vm_area_struct *vma = vmf->vma;
fc8efd2d 4634 struct mm_struct *vm_mm = vma->vm_mm;
2b740303 4635 vm_fault_t ret;
54cb8821 4636
ff09d7ec
AK
4637 /*
4638 * The VMA was not fully populated on mmap() or missing VM_DONTEXPAND
4639 */
4640 if (!vma->vm_ops->fault) {
4641 /*
4642 * If we find a migration pmd entry or a none pmd entry, which
4643 * should never happen, return SIGBUS
4644 */
4645 if (unlikely(!pmd_present(*vmf->pmd)))
4646 ret = VM_FAULT_SIGBUS;
4647 else {
4648 vmf->pte = pte_offset_map_lock(vmf->vma->vm_mm,
4649 vmf->pmd,
4650 vmf->address,
4651 &vmf->ptl);
4652 /*
4653 * Make sure this is not a temporary clearing of pte
4654 * by holding ptl and checking again. A R/M/W update
4655 * of pte involves: take ptl, clearing the pte so that
4656 * we don't have concurrent modification by hardware
4657 * followed by an update.
4658 */
4659 if (unlikely(pte_none(*vmf->pte)))
4660 ret = VM_FAULT_SIGBUS;
4661 else
4662 ret = VM_FAULT_NOPAGE;
4663
4664 pte_unmap_unlock(vmf->pte, vmf->ptl);
4665 }
4666 } else if (!(vmf->flags & FAULT_FLAG_WRITE))
b0b9b3df
HD
4667 ret = do_read_fault(vmf);
4668 else if (!(vma->vm_flags & VM_SHARED))
4669 ret = do_cow_fault(vmf);
4670 else
4671 ret = do_shared_fault(vmf);
4672
4673 /* preallocated pagetable is unused: free it */
4674 if (vmf->prealloc_pte) {
fc8efd2d 4675 pte_free(vm_mm, vmf->prealloc_pte);
7f2b6ce8 4676 vmf->prealloc_pte = NULL;
b0b9b3df
HD
4677 }
4678 return ret;
54cb8821
NP
4679}
4680
f4c0d836
YS
4681int numa_migrate_prep(struct page *page, struct vm_area_struct *vma,
4682 unsigned long addr, int page_nid, int *flags)
9532fec1
MG
4683{
4684 get_page(page);
4685
4686 count_vm_numa_event(NUMA_HINT_FAULTS);
04bb2f94 4687 if (page_nid == numa_node_id()) {
9532fec1 4688 count_vm_numa_event(NUMA_HINT_FAULTS_LOCAL);
04bb2f94
RR
4689 *flags |= TNF_FAULT_LOCAL;
4690 }
9532fec1
MG
4691
4692 return mpol_misplaced(page, vma, addr);
4693}
4694
2b740303 4695static vm_fault_t do_numa_page(struct vm_fault *vmf)
d10e63f2 4696{
82b0f8c3 4697 struct vm_area_struct *vma = vmf->vma;
4daae3b4 4698 struct page *page = NULL;
98fa15f3 4699 int page_nid = NUMA_NO_NODE;
90572890 4700 int last_cpupid;
cbee9f88 4701 int target_nid;
04a86453 4702 pte_t pte, old_pte;
288bc549 4703 bool was_writable = pte_savedwrite(vmf->orig_pte);
6688cc05 4704 int flags = 0;
d10e63f2
MG
4705
4706 /*
166f61b9
TH
4707 * The "pte" at this point cannot be used safely without
4708 * validation through pte_unmap_same(). It's of NUMA type but
4709 * the pfn may be screwed if the read is non atomic.
166f61b9 4710 */
82b0f8c3
JK
4711 vmf->ptl = pte_lockptr(vma->vm_mm, vmf->pmd);
4712 spin_lock(vmf->ptl);
cee216a6 4713 if (unlikely(!pte_same(*vmf->pte, vmf->orig_pte))) {
82b0f8c3 4714 pte_unmap_unlock(vmf->pte, vmf->ptl);
4daae3b4
MG
4715 goto out;
4716 }
4717
b99a342d
HY
4718 /* Get the normal PTE */
4719 old_pte = ptep_get(vmf->pte);
04a86453 4720 pte = pte_modify(old_pte, vma->vm_page_prot);
d10e63f2 4721
82b0f8c3 4722 page = vm_normal_page(vma, vmf->address, pte);
3218f871 4723 if (!page || is_zone_device_page(page))
b99a342d 4724 goto out_map;
d10e63f2 4725
e81c4802 4726 /* TODO: handle PTE-mapped THP */
b99a342d
HY
4727 if (PageCompound(page))
4728 goto out_map;
e81c4802 4729
6688cc05 4730 /*
bea66fbd
MG
4731 * Avoid grouping on RO pages in general. RO pages shouldn't hurt as
4732 * much anyway since they can be in shared cache state. This misses
4733 * the case where a mapping is writable but the process never writes
4734 * to it but pte_write gets cleared during protection updates and
4735 * pte_dirty has unpredictable behaviour between PTE scan updates,
4736 * background writeback, dirty balancing and application behaviour.
6688cc05 4737 */
b99a342d 4738 if (!was_writable)
6688cc05
PZ
4739 flags |= TNF_NO_GROUP;
4740
dabe1d99
RR
4741 /*
4742 * Flag if the page is shared between multiple address spaces. This
4743 * is later used when determining whether to group tasks together
4744 */
4745 if (page_mapcount(page) > 1 && (vma->vm_flags & VM_SHARED))
4746 flags |= TNF_SHARED;
4747
8191acbd 4748 page_nid = page_to_nid(page);
33024536
HY
4749 /*
4750 * For memory tiering mode, cpupid of slow memory page is used
4751 * to record page access time. So use default value.
4752 */
4753 if ((sysctl_numa_balancing_mode & NUMA_BALANCING_MEMORY_TIERING) &&
4754 !node_is_toptier(page_nid))
4755 last_cpupid = (-1 & LAST_CPUPID_MASK);
4756 else
4757 last_cpupid = page_cpupid_last(page);
82b0f8c3 4758 target_nid = numa_migrate_prep(page, vma, vmf->address, page_nid,
bae473a4 4759 &flags);
98fa15f3 4760 if (target_nid == NUMA_NO_NODE) {
4daae3b4 4761 put_page(page);
b99a342d 4762 goto out_map;
4daae3b4 4763 }
b99a342d 4764 pte_unmap_unlock(vmf->pte, vmf->ptl);
4daae3b4
MG
4765
4766 /* Migrate to the requested node */
bf90ac19 4767 if (migrate_misplaced_page(page, vma, target_nid)) {
8191acbd 4768 page_nid = target_nid;
6688cc05 4769 flags |= TNF_MIGRATED;
b99a342d 4770 } else {
074c2381 4771 flags |= TNF_MIGRATE_FAIL;
b99a342d
HY
4772 vmf->pte = pte_offset_map(vmf->pmd, vmf->address);
4773 spin_lock(vmf->ptl);
4774 if (unlikely(!pte_same(*vmf->pte, vmf->orig_pte))) {
4775 pte_unmap_unlock(vmf->pte, vmf->ptl);
4776 goto out;
4777 }
4778 goto out_map;
4779 }
4daae3b4
MG
4780
4781out:
98fa15f3 4782 if (page_nid != NUMA_NO_NODE)
6688cc05 4783 task_numa_fault(last_cpupid, page_nid, 1, flags);
d10e63f2 4784 return 0;
b99a342d
HY
4785out_map:
4786 /*
4787 * Make it present again, depending on how arch implements
4788 * non-accessible ptes, some can allow access by kernel mode.
4789 */
4790 old_pte = ptep_modify_prot_start(vma, vmf->address, vmf->pte);
4791 pte = pte_modify(old_pte, vma->vm_page_prot);
4792 pte = pte_mkyoung(pte);
4793 if (was_writable)
4794 pte = pte_mkwrite(pte);
4795 ptep_modify_prot_commit(vma, vmf->address, vmf->pte, old_pte, pte);
4796 update_mmu_cache(vma, vmf->address, vmf->pte);
4797 pte_unmap_unlock(vmf->pte, vmf->ptl);
4798 goto out;
d10e63f2
MG
4799}
4800
2b740303 4801static inline vm_fault_t create_huge_pmd(struct vm_fault *vmf)
b96375f7 4802{
f4200391 4803 if (vma_is_anonymous(vmf->vma))
82b0f8c3 4804 return do_huge_pmd_anonymous_page(vmf);
a2d58167 4805 if (vmf->vma->vm_ops->huge_fault)
c791ace1 4806 return vmf->vma->vm_ops->huge_fault(vmf, PE_SIZE_PMD);
b96375f7
MW
4807 return VM_FAULT_FALLBACK;
4808}
4809
183f24aa 4810/* `inline' is required to avoid gcc 4.1.2 build error */
5db4f15c 4811static inline vm_fault_t wp_huge_pmd(struct vm_fault *vmf)
b96375f7 4812{
c89357e2
DH
4813 const bool unshare = vmf->flags & FAULT_FLAG_UNSHARE;
4814
529b930b 4815 if (vma_is_anonymous(vmf->vma)) {
c89357e2
DH
4816 if (likely(!unshare) &&
4817 userfaultfd_huge_pmd_wp(vmf->vma, vmf->orig_pmd))
529b930b 4818 return handle_userfault(vmf, VM_UFFD_WP);
5db4f15c 4819 return do_huge_pmd_wp_page(vmf);
529b930b 4820 }
327e9fd4
THV
4821 if (vmf->vma->vm_ops->huge_fault) {
4822 vm_fault_t ret = vmf->vma->vm_ops->huge_fault(vmf, PE_SIZE_PMD);
4823
4824 if (!(ret & VM_FAULT_FALLBACK))
4825 return ret;
4826 }
af9e4d5f 4827
327e9fd4 4828 /* COW or write-notify handled on pte level: split pmd. */
82b0f8c3 4829 __split_huge_pmd(vmf->vma, vmf->pmd, vmf->address, false, NULL);
af9e4d5f 4830
b96375f7
MW
4831 return VM_FAULT_FALLBACK;
4832}
4833
2b740303 4834static vm_fault_t create_huge_pud(struct vm_fault *vmf)
a00cc7d9 4835{
14c99d65
GJ
4836#if defined(CONFIG_TRANSPARENT_HUGEPAGE) && \
4837 defined(CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD)
4838 /* No support for anonymous transparent PUD pages yet */
4839 if (vma_is_anonymous(vmf->vma))
4840 return VM_FAULT_FALLBACK;
4841 if (vmf->vma->vm_ops->huge_fault)
4842 return vmf->vma->vm_ops->huge_fault(vmf, PE_SIZE_PUD);
4843#endif /* CONFIG_TRANSPARENT_HUGEPAGE */
4844 return VM_FAULT_FALLBACK;
4845}
4846
4847static vm_fault_t wp_huge_pud(struct vm_fault *vmf, pud_t orig_pud)
4848{
327e9fd4
THV
4849#if defined(CONFIG_TRANSPARENT_HUGEPAGE) && \
4850 defined(CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD)
a00cc7d9
MW
4851 /* No support for anonymous transparent PUD pages yet */
4852 if (vma_is_anonymous(vmf->vma))
327e9fd4
THV
4853 goto split;
4854 if (vmf->vma->vm_ops->huge_fault) {
4855 vm_fault_t ret = vmf->vma->vm_ops->huge_fault(vmf, PE_SIZE_PUD);
4856
4857 if (!(ret & VM_FAULT_FALLBACK))
4858 return ret;
4859 }
4860split:
4861 /* COW or write-notify not handled on PUD level: split pud.*/
4862 __split_huge_pud(vmf->vma, vmf->pud, vmf->address);
14c99d65 4863#endif /* CONFIG_TRANSPARENT_HUGEPAGE && CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD */
a00cc7d9
MW
4864 return VM_FAULT_FALLBACK;
4865}
4866
1da177e4
LT
4867/*
4868 * These routines also need to handle stuff like marking pages dirty
4869 * and/or accessed for architectures that don't do it in hardware (most
4870 * RISC architectures). The early dirtying is also good on the i386.
4871 *
4872 * There is also a hook called "update_mmu_cache()" that architectures
4873 * with external mmu caches can use to update those (ie the Sparc or
4874 * PowerPC hashed page tables that act as extended TLBs).
4875 *
c1e8d7c6 4876 * We enter with non-exclusive mmap_lock (to exclude vma changes, but allow
7267ec00 4877 * concurrent faults).
9a95f3cf 4878 *
c1e8d7c6 4879 * The mmap_lock may have been released depending on flags and our return value.
9138e47e 4880 * See filemap_fault() and __folio_lock_or_retry().
1da177e4 4881 */
2b740303 4882static vm_fault_t handle_pte_fault(struct vm_fault *vmf)
1da177e4
LT
4883{
4884 pte_t entry;
4885
82b0f8c3 4886 if (unlikely(pmd_none(*vmf->pmd))) {
7267ec00
KS
4887 /*
4888 * Leave __pte_alloc() until later: because vm_ops->fault may
4889 * want to allocate huge page, and if we expose page table
4890 * for an instant, it will be difficult to retract from
4891 * concurrent faults and from rmap lookups.
4892 */
82b0f8c3 4893 vmf->pte = NULL;
f46f2ade 4894 vmf->flags &= ~FAULT_FLAG_ORIG_PTE_VALID;
7267ec00 4895 } else {
f9ce0be7
KS
4896 /*
4897 * If a huge pmd materialized under us just retry later. Use
4898 * pmd_trans_unstable() via pmd_devmap_trans_unstable() instead
4899 * of pmd_trans_huge() to ensure the pmd didn't become
4900 * pmd_trans_huge under us and then back to pmd_none, as a
4901 * result of MADV_DONTNEED running immediately after a huge pmd
4902 * fault in a different thread of this mm, in turn leading to a
4903 * misleading pmd_trans_huge() retval. All we have to ensure is
4904 * that it is a regular pmd that we can walk with
4905 * pte_offset_map() and we can do that through an atomic read
4906 * in C, which is what pmd_trans_unstable() provides.
4907 */
d0f0931d 4908 if (pmd_devmap_trans_unstable(vmf->pmd))
7267ec00
KS
4909 return 0;
4910 /*
4911 * A regular pmd is established and it can't morph into a huge
4912 * pmd from under us anymore at this point because we hold the
c1e8d7c6 4913 * mmap_lock read mode and khugepaged takes it in write mode.
7267ec00
KS
4914 * So now it's safe to run pte_offset_map().
4915 */
82b0f8c3 4916 vmf->pte = pte_offset_map(vmf->pmd, vmf->address);
2994302b 4917 vmf->orig_pte = *vmf->pte;
f46f2ade 4918 vmf->flags |= FAULT_FLAG_ORIG_PTE_VALID;
7267ec00
KS
4919
4920 /*
4921 * some architectures can have larger ptes than wordsize,
4922 * e.g.ppc44x-defconfig has CONFIG_PTE_64BIT=y and
b03a0fe0
PM
4923 * CONFIG_32BIT=y, so READ_ONCE cannot guarantee atomic
4924 * accesses. The code below just needs a consistent view
4925 * for the ifs and we later double check anyway with the
7267ec00
KS
4926 * ptl lock held. So here a barrier will do.
4927 */
4928 barrier();
2994302b 4929 if (pte_none(vmf->orig_pte)) {
82b0f8c3
JK
4930 pte_unmap(vmf->pte);
4931 vmf->pte = NULL;
65500d23 4932 }
1da177e4
LT
4933 }
4934
82b0f8c3
JK
4935 if (!vmf->pte) {
4936 if (vma_is_anonymous(vmf->vma))
4937 return do_anonymous_page(vmf);
7267ec00 4938 else
82b0f8c3 4939 return do_fault(vmf);
7267ec00
KS
4940 }
4941
2994302b
JK
4942 if (!pte_present(vmf->orig_pte))
4943 return do_swap_page(vmf);
7267ec00 4944
2994302b
JK
4945 if (pte_protnone(vmf->orig_pte) && vma_is_accessible(vmf->vma))
4946 return do_numa_page(vmf);
d10e63f2 4947
82b0f8c3
JK
4948 vmf->ptl = pte_lockptr(vmf->vma->vm_mm, vmf->pmd);
4949 spin_lock(vmf->ptl);
2994302b 4950 entry = vmf->orig_pte;
7df67697
BM
4951 if (unlikely(!pte_same(*vmf->pte, entry))) {
4952 update_mmu_tlb(vmf->vma, vmf->address, vmf->pte);
8f4e2101 4953 goto unlock;
7df67697 4954 }
c89357e2 4955 if (vmf->flags & (FAULT_FLAG_WRITE|FAULT_FLAG_UNSHARE)) {
f6f37321 4956 if (!pte_write(entry))
2994302b 4957 return do_wp_page(vmf);
c89357e2
DH
4958 else if (likely(vmf->flags & FAULT_FLAG_WRITE))
4959 entry = pte_mkdirty(entry);
1da177e4
LT
4960 }
4961 entry = pte_mkyoung(entry);
82b0f8c3
JK
4962 if (ptep_set_access_flags(vmf->vma, vmf->address, vmf->pte, entry,
4963 vmf->flags & FAULT_FLAG_WRITE)) {
4964 update_mmu_cache(vmf->vma, vmf->address, vmf->pte);
1a44e149 4965 } else {
b7333b58
YS
4966 /* Skip spurious TLB flush for retried page fault */
4967 if (vmf->flags & FAULT_FLAG_TRIED)
4968 goto unlock;
1a44e149
AA
4969 /*
4970 * This is needed only for protection faults but the arch code
4971 * is not yet telling us if this is a protection fault or not.
4972 * This still avoids useless tlb flushes for .text page faults
4973 * with threads.
4974 */
82b0f8c3
JK
4975 if (vmf->flags & FAULT_FLAG_WRITE)
4976 flush_tlb_fix_spurious_fault(vmf->vma, vmf->address);
1a44e149 4977 }
8f4e2101 4978unlock:
82b0f8c3 4979 pte_unmap_unlock(vmf->pte, vmf->ptl);
83c54070 4980 return 0;
1da177e4
LT
4981}
4982
4983/*
4984 * By the time we get here, we already hold the mm semaphore
9a95f3cf 4985 *
c1e8d7c6 4986 * The mmap_lock may have been released depending on flags and our
9138e47e 4987 * return value. See filemap_fault() and __folio_lock_or_retry().
1da177e4 4988 */
2b740303
SJ
4989static vm_fault_t __handle_mm_fault(struct vm_area_struct *vma,
4990 unsigned long address, unsigned int flags)
1da177e4 4991{
82b0f8c3 4992 struct vm_fault vmf = {
bae473a4 4993 .vma = vma,
1a29d85e 4994 .address = address & PAGE_MASK,
824ddc60 4995 .real_address = address,
bae473a4 4996 .flags = flags,
0721ec8b 4997 .pgoff = linear_page_index(vma, address),
667240e0 4998 .gfp_mask = __get_fault_gfp_mask(vma),
bae473a4 4999 };
dcddffd4 5000 struct mm_struct *mm = vma->vm_mm;
7da4e2cb 5001 unsigned long vm_flags = vma->vm_flags;
1da177e4 5002 pgd_t *pgd;
c2febafc 5003 p4d_t *p4d;
2b740303 5004 vm_fault_t ret;
1da177e4 5005
1da177e4 5006 pgd = pgd_offset(mm, address);
c2febafc
KS
5007 p4d = p4d_alloc(mm, pgd, address);
5008 if (!p4d)
5009 return VM_FAULT_OOM;
a00cc7d9 5010
c2febafc 5011 vmf.pud = pud_alloc(mm, p4d, address);
a00cc7d9 5012 if (!vmf.pud)
c74df32c 5013 return VM_FAULT_OOM;
625110b5 5014retry_pud:
7da4e2cb 5015 if (pud_none(*vmf.pud) &&
a7f4e6e4 5016 hugepage_vma_check(vma, vm_flags, false, true, true)) {
a00cc7d9
MW
5017 ret = create_huge_pud(&vmf);
5018 if (!(ret & VM_FAULT_FALLBACK))
5019 return ret;
5020 } else {
5021 pud_t orig_pud = *vmf.pud;
5022
5023 barrier();
5024 if (pud_trans_huge(orig_pud) || pud_devmap(orig_pud)) {
a00cc7d9 5025
c89357e2
DH
5026 /*
5027 * TODO once we support anonymous PUDs: NUMA case and
5028 * FAULT_FLAG_UNSHARE handling.
5029 */
5030 if ((flags & FAULT_FLAG_WRITE) && !pud_write(orig_pud)) {
a00cc7d9
MW
5031 ret = wp_huge_pud(&vmf, orig_pud);
5032 if (!(ret & VM_FAULT_FALLBACK))
5033 return ret;
5034 } else {
5035 huge_pud_set_accessed(&vmf, orig_pud);
5036 return 0;
5037 }
5038 }
5039 }
5040
5041 vmf.pmd = pmd_alloc(mm, vmf.pud, address);
82b0f8c3 5042 if (!vmf.pmd)
c74df32c 5043 return VM_FAULT_OOM;
625110b5
TH
5044
5045 /* Huge pud page fault raced with pmd_alloc? */
5046 if (pud_trans_unstable(vmf.pud))
5047 goto retry_pud;
5048
7da4e2cb 5049 if (pmd_none(*vmf.pmd) &&
a7f4e6e4 5050 hugepage_vma_check(vma, vm_flags, false, true, true)) {
a2d58167 5051 ret = create_huge_pmd(&vmf);
c0292554
KS
5052 if (!(ret & VM_FAULT_FALLBACK))
5053 return ret;
71e3aac0 5054 } else {
5db4f15c 5055 vmf.orig_pmd = *vmf.pmd;
1f1d06c3 5056
71e3aac0 5057 barrier();
5db4f15c 5058 if (unlikely(is_swap_pmd(vmf.orig_pmd))) {
84c3fc4e 5059 VM_BUG_ON(thp_migration_supported() &&
5db4f15c
YS
5060 !is_pmd_migration_entry(vmf.orig_pmd));
5061 if (is_pmd_migration_entry(vmf.orig_pmd))
84c3fc4e
ZY
5062 pmd_migration_entry_wait(mm, vmf.pmd);
5063 return 0;
5064 }
5db4f15c
YS
5065 if (pmd_trans_huge(vmf.orig_pmd) || pmd_devmap(vmf.orig_pmd)) {
5066 if (pmd_protnone(vmf.orig_pmd) && vma_is_accessible(vma))
5067 return do_huge_pmd_numa_page(&vmf);
d10e63f2 5068
c89357e2
DH
5069 if ((flags & (FAULT_FLAG_WRITE|FAULT_FLAG_UNSHARE)) &&
5070 !pmd_write(vmf.orig_pmd)) {
5db4f15c 5071 ret = wp_huge_pmd(&vmf);
9845cbbd
KS
5072 if (!(ret & VM_FAULT_FALLBACK))
5073 return ret;
a1dd450b 5074 } else {
5db4f15c 5075 huge_pmd_set_accessed(&vmf);
9845cbbd 5076 return 0;
1f1d06c3 5077 }
71e3aac0
AA
5078 }
5079 }
5080
82b0f8c3 5081 return handle_pte_fault(&vmf);
1da177e4
LT
5082}
5083
bce617ed 5084/**
f0953a1b 5085 * mm_account_fault - Do page fault accounting
bce617ed
PX
5086 *
5087 * @regs: the pt_regs struct pointer. When set to NULL, will skip accounting
5088 * of perf event counters, but we'll still do the per-task accounting to
5089 * the task who triggered this page fault.
5090 * @address: the faulted address.
5091 * @flags: the fault flags.
5092 * @ret: the fault retcode.
5093 *
f0953a1b 5094 * This will take care of most of the page fault accounting. Meanwhile, it
bce617ed 5095 * will also include the PERF_COUNT_SW_PAGE_FAULTS_[MAJ|MIN] perf counter
f0953a1b 5096 * updates. However, note that the handling of PERF_COUNT_SW_PAGE_FAULTS should
bce617ed
PX
5097 * still be in per-arch page fault handlers at the entry of page fault.
5098 */
5099static inline void mm_account_fault(struct pt_regs *regs,
5100 unsigned long address, unsigned int flags,
5101 vm_fault_t ret)
5102{
5103 bool major;
5104
5105 /*
5106 * We don't do accounting for some specific faults:
5107 *
5108 * - Unsuccessful faults (e.g. when the address wasn't valid). That
5109 * includes arch_vma_access_permitted() failing before reaching here.
5110 * So this is not a "this many hardware page faults" counter. We
5111 * should use the hw profiling for that.
5112 *
5113 * - Incomplete faults (VM_FAULT_RETRY). They will only be counted
5114 * once they're completed.
5115 */
5116 if (ret & (VM_FAULT_ERROR | VM_FAULT_RETRY))
5117 return;
5118
5119 /*
5120 * We define the fault as a major fault when the final successful fault
5121 * is VM_FAULT_MAJOR, or if it retried (which implies that we couldn't
5122 * handle it immediately previously).
5123 */
5124 major = (ret & VM_FAULT_MAJOR) || (flags & FAULT_FLAG_TRIED);
5125
a2beb5f1
PX
5126 if (major)
5127 current->maj_flt++;
5128 else
5129 current->min_flt++;
5130
bce617ed 5131 /*
a2beb5f1
PX
5132 * If the fault is done for GUP, regs will be NULL. We only do the
5133 * accounting for the per thread fault counters who triggered the
5134 * fault, and we skip the perf event updates.
bce617ed
PX
5135 */
5136 if (!regs)
5137 return;
5138
a2beb5f1 5139 if (major)
bce617ed 5140 perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS_MAJ, 1, regs, address);
a2beb5f1 5141 else
bce617ed 5142 perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS_MIN, 1, regs, address);
bce617ed
PX
5143}
5144
ec1c86b2
YZ
5145#ifdef CONFIG_LRU_GEN
5146static void lru_gen_enter_fault(struct vm_area_struct *vma)
5147{
5148 /* the LRU algorithm doesn't apply to sequential or random reads */
5149 current->in_lru_fault = !(vma->vm_flags & (VM_SEQ_READ | VM_RAND_READ));
5150}
5151
5152static void lru_gen_exit_fault(void)
5153{
5154 current->in_lru_fault = false;
5155}
5156#else
5157static void lru_gen_enter_fault(struct vm_area_struct *vma)
5158{
5159}
5160
5161static void lru_gen_exit_fault(void)
5162{
5163}
5164#endif /* CONFIG_LRU_GEN */
5165
9a95f3cf
PC
5166/*
5167 * By the time we get here, we already hold the mm semaphore
5168 *
c1e8d7c6 5169 * The mmap_lock may have been released depending on flags and our
9138e47e 5170 * return value. See filemap_fault() and __folio_lock_or_retry().
9a95f3cf 5171 */
2b740303 5172vm_fault_t handle_mm_fault(struct vm_area_struct *vma, unsigned long address,
bce617ed 5173 unsigned int flags, struct pt_regs *regs)
519e5247 5174{
2b740303 5175 vm_fault_t ret;
519e5247
JW
5176
5177 __set_current_state(TASK_RUNNING);
5178
5179 count_vm_event(PGFAULT);
2262185c 5180 count_memcg_event_mm(vma->vm_mm, PGFAULT);
519e5247
JW
5181
5182 /* do counter updates before entering really critical section. */
5183 check_sync_rss_stat(current);
5184
de0c799b
LD
5185 if (!arch_vma_access_permitted(vma, flags & FAULT_FLAG_WRITE,
5186 flags & FAULT_FLAG_INSTRUCTION,
5187 flags & FAULT_FLAG_REMOTE))
5188 return VM_FAULT_SIGSEGV;
5189
519e5247
JW
5190 /*
5191 * Enable the memcg OOM handling for faults triggered in user
5192 * space. Kernel faults are handled more gracefully.
5193 */
5194 if (flags & FAULT_FLAG_USER)
29ef680a 5195 mem_cgroup_enter_user_fault();
519e5247 5196
ec1c86b2
YZ
5197 lru_gen_enter_fault(vma);
5198
bae473a4
KS
5199 if (unlikely(is_vm_hugetlb_page(vma)))
5200 ret = hugetlb_fault(vma->vm_mm, vma, address, flags);
5201 else
5202 ret = __handle_mm_fault(vma, address, flags);
519e5247 5203
ec1c86b2
YZ
5204 lru_gen_exit_fault();
5205
49426420 5206 if (flags & FAULT_FLAG_USER) {
29ef680a 5207 mem_cgroup_exit_user_fault();
166f61b9
TH
5208 /*
5209 * The task may have entered a memcg OOM situation but
5210 * if the allocation error was handled gracefully (no
5211 * VM_FAULT_OOM), there is no need to kill anything.
5212 * Just clean up the OOM state peacefully.
5213 */
5214 if (task_in_memcg_oom(current) && !(ret & VM_FAULT_OOM))
5215 mem_cgroup_oom_synchronize(false);
49426420 5216 }
3812c8c8 5217
bce617ed
PX
5218 mm_account_fault(regs, address, flags, ret);
5219
519e5247
JW
5220 return ret;
5221}
e1d6d01a 5222EXPORT_SYMBOL_GPL(handle_mm_fault);
519e5247 5223
90eceff1
KS
5224#ifndef __PAGETABLE_P4D_FOLDED
5225/*
5226 * Allocate p4d page table.
5227 * We've already handled the fast-path in-line.
5228 */
5229int __p4d_alloc(struct mm_struct *mm, pgd_t *pgd, unsigned long address)
5230{
5231 p4d_t *new = p4d_alloc_one(mm, address);
5232 if (!new)
5233 return -ENOMEM;
5234
90eceff1 5235 spin_lock(&mm->page_table_lock);
ed33b5a6 5236 if (pgd_present(*pgd)) { /* Another has populated it */
90eceff1 5237 p4d_free(mm, new);
ed33b5a6
QZ
5238 } else {
5239 smp_wmb(); /* See comment in pmd_install() */
90eceff1 5240 pgd_populate(mm, pgd, new);
ed33b5a6 5241 }
90eceff1
KS
5242 spin_unlock(&mm->page_table_lock);
5243 return 0;
5244}
5245#endif /* __PAGETABLE_P4D_FOLDED */
5246
1da177e4
LT
5247#ifndef __PAGETABLE_PUD_FOLDED
5248/*
5249 * Allocate page upper directory.
872fec16 5250 * We've already handled the fast-path in-line.
1da177e4 5251 */
c2febafc 5252int __pud_alloc(struct mm_struct *mm, p4d_t *p4d, unsigned long address)
1da177e4 5253{
c74df32c
HD
5254 pud_t *new = pud_alloc_one(mm, address);
5255 if (!new)
1bb3630e 5256 return -ENOMEM;
1da177e4 5257
872fec16 5258 spin_lock(&mm->page_table_lock);
b4e98d9a
KS
5259 if (!p4d_present(*p4d)) {
5260 mm_inc_nr_puds(mm);
ed33b5a6 5261 smp_wmb(); /* See comment in pmd_install() */
c2febafc 5262 p4d_populate(mm, p4d, new);
b4e98d9a 5263 } else /* Another has populated it */
5e541973 5264 pud_free(mm, new);
c74df32c 5265 spin_unlock(&mm->page_table_lock);
1bb3630e 5266 return 0;
1da177e4
LT
5267}
5268#endif /* __PAGETABLE_PUD_FOLDED */
5269
5270#ifndef __PAGETABLE_PMD_FOLDED
5271/*
5272 * Allocate page middle directory.
872fec16 5273 * We've already handled the fast-path in-line.
1da177e4 5274 */
1bb3630e 5275int __pmd_alloc(struct mm_struct *mm, pud_t *pud, unsigned long address)
1da177e4 5276{
a00cc7d9 5277 spinlock_t *ptl;
c74df32c
HD
5278 pmd_t *new = pmd_alloc_one(mm, address);
5279 if (!new)
1bb3630e 5280 return -ENOMEM;
1da177e4 5281
a00cc7d9 5282 ptl = pud_lock(mm, pud);
dc6c9a35
KS
5283 if (!pud_present(*pud)) {
5284 mm_inc_nr_pmds(mm);
ed33b5a6 5285 smp_wmb(); /* See comment in pmd_install() */
1bb3630e 5286 pud_populate(mm, pud, new);
ed33b5a6 5287 } else { /* Another has populated it */
5e541973 5288 pmd_free(mm, new);
ed33b5a6 5289 }
a00cc7d9 5290 spin_unlock(ptl);
1bb3630e 5291 return 0;
e0f39591 5292}
1da177e4
LT
5293#endif /* __PAGETABLE_PMD_FOLDED */
5294
0e5e64c0
MS
5295/**
5296 * follow_pte - look up PTE at a user virtual address
5297 * @mm: the mm_struct of the target address space
5298 * @address: user virtual address
5299 * @ptepp: location to store found PTE
5300 * @ptlp: location to store the lock for the PTE
5301 *
5302 * On a successful return, the pointer to the PTE is stored in @ptepp;
5303 * the corresponding lock is taken and its location is stored in @ptlp.
5304 * The contents of the PTE are only stable until @ptlp is released;
5305 * any further use, if any, must be protected against invalidation
5306 * with MMU notifiers.
5307 *
5308 * Only IO mappings and raw PFN mappings are allowed. The mmap semaphore
5309 * should be taken for read.
5310 *
5311 * KVM uses this function. While it is arguably less bad than ``follow_pfn``,
5312 * it is not a good general-purpose API.
5313 *
5314 * Return: zero on success, -ve otherwise.
5315 */
5316int follow_pte(struct mm_struct *mm, unsigned long address,
5317 pte_t **ptepp, spinlock_t **ptlp)
f8ad0f49
JW
5318{
5319 pgd_t *pgd;
c2febafc 5320 p4d_t *p4d;
f8ad0f49
JW
5321 pud_t *pud;
5322 pmd_t *pmd;
5323 pte_t *ptep;
5324
5325 pgd = pgd_offset(mm, address);
5326 if (pgd_none(*pgd) || unlikely(pgd_bad(*pgd)))
5327 goto out;
5328
c2febafc
KS
5329 p4d = p4d_offset(pgd, address);
5330 if (p4d_none(*p4d) || unlikely(p4d_bad(*p4d)))
5331 goto out;
5332
5333 pud = pud_offset(p4d, address);
f8ad0f49
JW
5334 if (pud_none(*pud) || unlikely(pud_bad(*pud)))
5335 goto out;
5336
5337 pmd = pmd_offset(pud, address);
f66055ab 5338 VM_BUG_ON(pmd_trans_huge(*pmd));
f8ad0f49 5339
09796395 5340 if (pmd_none(*pmd) || unlikely(pmd_bad(*pmd)))
f8ad0f49
JW
5341 goto out;
5342
5343 ptep = pte_offset_map_lock(mm, pmd, address, ptlp);
f8ad0f49
JW
5344 if (!pte_present(*ptep))
5345 goto unlock;
5346 *ptepp = ptep;
5347 return 0;
5348unlock:
5349 pte_unmap_unlock(ptep, *ptlp);
5350out:
5351 return -EINVAL;
5352}
9fd6dad1
PB
5353EXPORT_SYMBOL_GPL(follow_pte);
5354
3b6748e2
JW
5355/**
5356 * follow_pfn - look up PFN at a user virtual address
5357 * @vma: memory mapping
5358 * @address: user virtual address
5359 * @pfn: location to store found PFN
5360 *
5361 * Only IO mappings and raw PFN mappings are allowed.
5362 *
9fd6dad1
PB
5363 * This function does not allow the caller to read the permissions
5364 * of the PTE. Do not use it.
5365 *
a862f68a 5366 * Return: zero and the pfn at @pfn on success, -ve otherwise.
3b6748e2
JW
5367 */
5368int follow_pfn(struct vm_area_struct *vma, unsigned long address,
5369 unsigned long *pfn)
5370{
5371 int ret = -EINVAL;
5372 spinlock_t *ptl;
5373 pte_t *ptep;
5374
5375 if (!(vma->vm_flags & (VM_IO | VM_PFNMAP)))
5376 return ret;
5377
9fd6dad1 5378 ret = follow_pte(vma->vm_mm, address, &ptep, &ptl);
3b6748e2
JW
5379 if (ret)
5380 return ret;
5381 *pfn = pte_pfn(*ptep);
5382 pte_unmap_unlock(ptep, ptl);
5383 return 0;
5384}
5385EXPORT_SYMBOL(follow_pfn);
5386
28b2ee20 5387#ifdef CONFIG_HAVE_IOREMAP_PROT
d87fe660 5388int follow_phys(struct vm_area_struct *vma,
5389 unsigned long address, unsigned int flags,
5390 unsigned long *prot, resource_size_t *phys)
28b2ee20 5391{
03668a4d 5392 int ret = -EINVAL;
28b2ee20
RR
5393 pte_t *ptep, pte;
5394 spinlock_t *ptl;
28b2ee20 5395
d87fe660 5396 if (!(vma->vm_flags & (VM_IO | VM_PFNMAP)))
5397 goto out;
28b2ee20 5398
9fd6dad1 5399 if (follow_pte(vma->vm_mm, address, &ptep, &ptl))
d87fe660 5400 goto out;
28b2ee20 5401 pte = *ptep;
03668a4d 5402
f6f37321 5403 if ((flags & FOLL_WRITE) && !pte_write(pte))
28b2ee20 5404 goto unlock;
28b2ee20
RR
5405
5406 *prot = pgprot_val(pte_pgprot(pte));
03668a4d 5407 *phys = (resource_size_t)pte_pfn(pte) << PAGE_SHIFT;
28b2ee20 5408
03668a4d 5409 ret = 0;
28b2ee20
RR
5410unlock:
5411 pte_unmap_unlock(ptep, ptl);
5412out:
d87fe660 5413 return ret;
28b2ee20
RR
5414}
5415
96667f8a
DV
5416/**
5417 * generic_access_phys - generic implementation for iomem mmap access
5418 * @vma: the vma to access
f0953a1b 5419 * @addr: userspace address, not relative offset within @vma
96667f8a
DV
5420 * @buf: buffer to read/write
5421 * @len: length of transfer
5422 * @write: set to FOLL_WRITE when writing, otherwise reading
5423 *
5424 * This is a generic implementation for &vm_operations_struct.access for an
5425 * iomem mapping. This callback is used by access_process_vm() when the @vma is
5426 * not page based.
5427 */
28b2ee20
RR
5428int generic_access_phys(struct vm_area_struct *vma, unsigned long addr,
5429 void *buf, int len, int write)
5430{
5431 resource_size_t phys_addr;
5432 unsigned long prot = 0;
2bc7273b 5433 void __iomem *maddr;
96667f8a
DV
5434 pte_t *ptep, pte;
5435 spinlock_t *ptl;
5436 int offset = offset_in_page(addr);
5437 int ret = -EINVAL;
5438
5439 if (!(vma->vm_flags & (VM_IO | VM_PFNMAP)))
5440 return -EINVAL;
5441
5442retry:
e913a8cd 5443 if (follow_pte(vma->vm_mm, addr, &ptep, &ptl))
96667f8a
DV
5444 return -EINVAL;
5445 pte = *ptep;
5446 pte_unmap_unlock(ptep, ptl);
28b2ee20 5447
96667f8a
DV
5448 prot = pgprot_val(pte_pgprot(pte));
5449 phys_addr = (resource_size_t)pte_pfn(pte) << PAGE_SHIFT;
5450
5451 if ((write & FOLL_WRITE) && !pte_write(pte))
28b2ee20
RR
5452 return -EINVAL;
5453
9cb12d7b 5454 maddr = ioremap_prot(phys_addr, PAGE_ALIGN(len + offset), prot);
24eee1e4 5455 if (!maddr)
5456 return -ENOMEM;
5457
e913a8cd 5458 if (follow_pte(vma->vm_mm, addr, &ptep, &ptl))
96667f8a
DV
5459 goto out_unmap;
5460
5461 if (!pte_same(pte, *ptep)) {
5462 pte_unmap_unlock(ptep, ptl);
5463 iounmap(maddr);
5464
5465 goto retry;
5466 }
5467
28b2ee20
RR
5468 if (write)
5469 memcpy_toio(maddr + offset, buf, len);
5470 else
5471 memcpy_fromio(buf, maddr + offset, len);
96667f8a
DV
5472 ret = len;
5473 pte_unmap_unlock(ptep, ptl);
5474out_unmap:
28b2ee20
RR
5475 iounmap(maddr);
5476
96667f8a 5477 return ret;
28b2ee20 5478}
5a73633e 5479EXPORT_SYMBOL_GPL(generic_access_phys);
28b2ee20
RR
5480#endif
5481
0ec76a11 5482/*
d3f5ffca 5483 * Access another process' address space as given in mm.
0ec76a11 5484 */
d3f5ffca
JH
5485int __access_remote_vm(struct mm_struct *mm, unsigned long addr, void *buf,
5486 int len, unsigned int gup_flags)
0ec76a11 5487{
0ec76a11 5488 struct vm_area_struct *vma;
0ec76a11 5489 void *old_buf = buf;
442486ec 5490 int write = gup_flags & FOLL_WRITE;
0ec76a11 5491
d8ed45c5 5492 if (mmap_read_lock_killable(mm))
1e426fe2
KK
5493 return 0;
5494
183ff22b 5495 /* ignore errors, just check how much was successfully transferred */
0ec76a11
DH
5496 while (len) {
5497 int bytes, ret, offset;
5498 void *maddr;
28b2ee20 5499 struct page *page = NULL;
0ec76a11 5500
64019a2e 5501 ret = get_user_pages_remote(mm, addr, 1,
5b56d49f 5502 gup_flags, &page, &vma, NULL);
28b2ee20 5503 if (ret <= 0) {
dbffcd03
RR
5504#ifndef CONFIG_HAVE_IOREMAP_PROT
5505 break;
5506#else
28b2ee20
RR
5507 /*
5508 * Check if this is a VM_IO | VM_PFNMAP VMA, which
5509 * we can access using slightly different code.
5510 */
3e418f98
LH
5511 vma = vma_lookup(mm, addr);
5512 if (!vma)
28b2ee20
RR
5513 break;
5514 if (vma->vm_ops && vma->vm_ops->access)
5515 ret = vma->vm_ops->access(vma, addr, buf,
5516 len, write);
5517 if (ret <= 0)
28b2ee20
RR
5518 break;
5519 bytes = ret;
dbffcd03 5520#endif
0ec76a11 5521 } else {
28b2ee20
RR
5522 bytes = len;
5523 offset = addr & (PAGE_SIZE-1);
5524 if (bytes > PAGE_SIZE-offset)
5525 bytes = PAGE_SIZE-offset;
5526
5527 maddr = kmap(page);
5528 if (write) {
5529 copy_to_user_page(vma, page, addr,
5530 maddr + offset, buf, bytes);
5531 set_page_dirty_lock(page);
5532 } else {
5533 copy_from_user_page(vma, page, addr,
5534 buf, maddr + offset, bytes);
5535 }
5536 kunmap(page);
09cbfeaf 5537 put_page(page);
0ec76a11 5538 }
0ec76a11
DH
5539 len -= bytes;
5540 buf += bytes;
5541 addr += bytes;
5542 }
d8ed45c5 5543 mmap_read_unlock(mm);
0ec76a11
DH
5544
5545 return buf - old_buf;
5546}
03252919 5547
5ddd36b9 5548/**
ae91dbfc 5549 * access_remote_vm - access another process' address space
5ddd36b9
SW
5550 * @mm: the mm_struct of the target address space
5551 * @addr: start address to access
5552 * @buf: source or destination buffer
5553 * @len: number of bytes to transfer
6347e8d5 5554 * @gup_flags: flags modifying lookup behaviour
5ddd36b9
SW
5555 *
5556 * The caller must hold a reference on @mm.
a862f68a
MR
5557 *
5558 * Return: number of bytes copied from source to destination.
5ddd36b9
SW
5559 */
5560int access_remote_vm(struct mm_struct *mm, unsigned long addr,
6347e8d5 5561 void *buf, int len, unsigned int gup_flags)
5ddd36b9 5562{
d3f5ffca 5563 return __access_remote_vm(mm, addr, buf, len, gup_flags);
5ddd36b9
SW
5564}
5565
206cb636
SW
5566/*
5567 * Access another process' address space.
5568 * Source/target buffer must be kernel space,
5569 * Do not walk the page table directly, use get_user_pages
5570 */
5571int access_process_vm(struct task_struct *tsk, unsigned long addr,
f307ab6d 5572 void *buf, int len, unsigned int gup_flags)
206cb636
SW
5573{
5574 struct mm_struct *mm;
5575 int ret;
5576
5577 mm = get_task_mm(tsk);
5578 if (!mm)
5579 return 0;
5580
d3f5ffca 5581 ret = __access_remote_vm(mm, addr, buf, len, gup_flags);
442486ec 5582
206cb636
SW
5583 mmput(mm);
5584
5585 return ret;
5586}
fcd35857 5587EXPORT_SYMBOL_GPL(access_process_vm);
206cb636 5588
03252919
AK
5589/*
5590 * Print the name of a VMA.
5591 */
5592void print_vma_addr(char *prefix, unsigned long ip)
5593{
5594 struct mm_struct *mm = current->mm;
5595 struct vm_area_struct *vma;
5596
e8bff74a 5597 /*
0a7f682d 5598 * we might be running from an atomic context so we cannot sleep
e8bff74a 5599 */
d8ed45c5 5600 if (!mmap_read_trylock(mm))
e8bff74a
IM
5601 return;
5602
03252919
AK
5603 vma = find_vma(mm, ip);
5604 if (vma && vma->vm_file) {
5605 struct file *f = vma->vm_file;
0a7f682d 5606 char *buf = (char *)__get_free_page(GFP_NOWAIT);
03252919 5607 if (buf) {
2fbc57c5 5608 char *p;
03252919 5609
9bf39ab2 5610 p = file_path(f, buf, PAGE_SIZE);
03252919
AK
5611 if (IS_ERR(p))
5612 p = "?";
2fbc57c5 5613 printk("%s%s[%lx+%lx]", prefix, kbasename(p),
03252919
AK
5614 vma->vm_start,
5615 vma->vm_end - vma->vm_start);
5616 free_page((unsigned long)buf);
5617 }
5618 }
d8ed45c5 5619 mmap_read_unlock(mm);
03252919 5620}
3ee1afa3 5621
662bbcb2 5622#if defined(CONFIG_PROVE_LOCKING) || defined(CONFIG_DEBUG_ATOMIC_SLEEP)
9ec23531 5623void __might_fault(const char *file, int line)
3ee1afa3 5624{
9ec23531 5625 if (pagefault_disabled())
662bbcb2 5626 return;
42a38756 5627 __might_sleep(file, line);
9ec23531 5628#if defined(CONFIG_DEBUG_ATOMIC_SLEEP)
662bbcb2 5629 if (current->mm)
da1c55f1 5630 might_lock_read(&current->mm->mmap_lock);
9ec23531 5631#endif
3ee1afa3 5632}
9ec23531 5633EXPORT_SYMBOL(__might_fault);
3ee1afa3 5634#endif
47ad8475
AA
5635
5636#if defined(CONFIG_TRANSPARENT_HUGEPAGE) || defined(CONFIG_HUGETLBFS)
c6ddfb6c
HY
5637/*
5638 * Process all subpages of the specified huge page with the specified
5639 * operation. The target subpage will be processed last to keep its
5640 * cache lines hot.
5641 */
5642static inline void process_huge_page(
5643 unsigned long addr_hint, unsigned int pages_per_huge_page,
5644 void (*process_subpage)(unsigned long addr, int idx, void *arg),
5645 void *arg)
47ad8475 5646{
c79b57e4
HY
5647 int i, n, base, l;
5648 unsigned long addr = addr_hint &
5649 ~(((unsigned long)pages_per_huge_page << PAGE_SHIFT) - 1);
47ad8475 5650
c6ddfb6c 5651 /* Process target subpage last to keep its cache lines hot */
47ad8475 5652 might_sleep();
c79b57e4
HY
5653 n = (addr_hint - addr) / PAGE_SIZE;
5654 if (2 * n <= pages_per_huge_page) {
c6ddfb6c 5655 /* If target subpage in first half of huge page */
c79b57e4
HY
5656 base = 0;
5657 l = n;
c6ddfb6c 5658 /* Process subpages at the end of huge page */
c79b57e4
HY
5659 for (i = pages_per_huge_page - 1; i >= 2 * n; i--) {
5660 cond_resched();
c6ddfb6c 5661 process_subpage(addr + i * PAGE_SIZE, i, arg);
c79b57e4
HY
5662 }
5663 } else {
c6ddfb6c 5664 /* If target subpage in second half of huge page */
c79b57e4
HY
5665 base = pages_per_huge_page - 2 * (pages_per_huge_page - n);
5666 l = pages_per_huge_page - n;
c6ddfb6c 5667 /* Process subpages at the begin of huge page */
c79b57e4
HY
5668 for (i = 0; i < base; i++) {
5669 cond_resched();
c6ddfb6c 5670 process_subpage(addr + i * PAGE_SIZE, i, arg);
c79b57e4
HY
5671 }
5672 }
5673 /*
c6ddfb6c
HY
5674 * Process remaining subpages in left-right-left-right pattern
5675 * towards the target subpage
c79b57e4
HY
5676 */
5677 for (i = 0; i < l; i++) {
5678 int left_idx = base + i;
5679 int right_idx = base + 2 * l - 1 - i;
5680
5681 cond_resched();
c6ddfb6c 5682 process_subpage(addr + left_idx * PAGE_SIZE, left_idx, arg);
47ad8475 5683 cond_resched();
c6ddfb6c 5684 process_subpage(addr + right_idx * PAGE_SIZE, right_idx, arg);
47ad8475
AA
5685 }
5686}
5687
c6ddfb6c
HY
5688static void clear_gigantic_page(struct page *page,
5689 unsigned long addr,
5690 unsigned int pages_per_huge_page)
5691{
5692 int i;
5693 struct page *p = page;
5694
5695 might_sleep();
5696 for (i = 0; i < pages_per_huge_page;
5697 i++, p = mem_map_next(p, page, i)) {
5698 cond_resched();
5699 clear_user_highpage(p, addr + i * PAGE_SIZE);
5700 }
5701}
5702
5703static void clear_subpage(unsigned long addr, int idx, void *arg)
5704{
5705 struct page *page = arg;
5706
5707 clear_user_highpage(page + idx, addr);
5708}
5709
5710void clear_huge_page(struct page *page,
5711 unsigned long addr_hint, unsigned int pages_per_huge_page)
5712{
5713 unsigned long addr = addr_hint &
5714 ~(((unsigned long)pages_per_huge_page << PAGE_SHIFT) - 1);
5715
5716 if (unlikely(pages_per_huge_page > MAX_ORDER_NR_PAGES)) {
5717 clear_gigantic_page(page, addr, pages_per_huge_page);
5718 return;
5719 }
5720
5721 process_huge_page(addr_hint, pages_per_huge_page, clear_subpage, page);
5722}
5723
47ad8475
AA
5724static void copy_user_gigantic_page(struct page *dst, struct page *src,
5725 unsigned long addr,
5726 struct vm_area_struct *vma,
5727 unsigned int pages_per_huge_page)
5728{
5729 int i;
5730 struct page *dst_base = dst;
5731 struct page *src_base = src;
5732
5733 for (i = 0; i < pages_per_huge_page; ) {
5734 cond_resched();
5735 copy_user_highpage(dst, src, addr + i*PAGE_SIZE, vma);
5736
5737 i++;
5738 dst = mem_map_next(dst, dst_base, i);
5739 src = mem_map_next(src, src_base, i);
5740 }
5741}
5742
c9f4cd71
HY
5743struct copy_subpage_arg {
5744 struct page *dst;
5745 struct page *src;
5746 struct vm_area_struct *vma;
5747};
5748
5749static void copy_subpage(unsigned long addr, int idx, void *arg)
5750{
5751 struct copy_subpage_arg *copy_arg = arg;
5752
5753 copy_user_highpage(copy_arg->dst + idx, copy_arg->src + idx,
5754 addr, copy_arg->vma);
5755}
5756
47ad8475 5757void copy_user_huge_page(struct page *dst, struct page *src,
c9f4cd71 5758 unsigned long addr_hint, struct vm_area_struct *vma,
47ad8475
AA
5759 unsigned int pages_per_huge_page)
5760{
c9f4cd71
HY
5761 unsigned long addr = addr_hint &
5762 ~(((unsigned long)pages_per_huge_page << PAGE_SHIFT) - 1);
5763 struct copy_subpage_arg arg = {
5764 .dst = dst,
5765 .src = src,
5766 .vma = vma,
5767 };
47ad8475
AA
5768
5769 if (unlikely(pages_per_huge_page > MAX_ORDER_NR_PAGES)) {
5770 copy_user_gigantic_page(dst, src, addr, vma,
5771 pages_per_huge_page);
5772 return;
5773 }
5774
c9f4cd71 5775 process_huge_page(addr_hint, pages_per_huge_page, copy_subpage, &arg);
47ad8475 5776}
fa4d75c1
MK
5777
5778long copy_huge_page_from_user(struct page *dst_page,
5779 const void __user *usr_src,
810a56b9
MK
5780 unsigned int pages_per_huge_page,
5781 bool allow_pagefault)
fa4d75c1 5782{
fa4d75c1
MK
5783 void *page_kaddr;
5784 unsigned long i, rc = 0;
5785 unsigned long ret_val = pages_per_huge_page * PAGE_SIZE;
3272cfc2 5786 struct page *subpage = dst_page;
fa4d75c1 5787
3272cfc2
MK
5788 for (i = 0; i < pages_per_huge_page;
5789 i++, subpage = mem_map_next(subpage, dst_page, i)) {
810a56b9 5790 if (allow_pagefault)
3272cfc2 5791 page_kaddr = kmap(subpage);
810a56b9 5792 else
3272cfc2 5793 page_kaddr = kmap_atomic(subpage);
fa4d75c1 5794 rc = copy_from_user(page_kaddr,
b063e374 5795 usr_src + i * PAGE_SIZE, PAGE_SIZE);
810a56b9 5796 if (allow_pagefault)
3272cfc2 5797 kunmap(subpage);
810a56b9
MK
5798 else
5799 kunmap_atomic(page_kaddr);
fa4d75c1
MK
5800
5801 ret_val -= (PAGE_SIZE - rc);
5802 if (rc)
5803 break;
5804
e763243c
MS
5805 flush_dcache_page(subpage);
5806
fa4d75c1
MK
5807 cond_resched();
5808 }
5809 return ret_val;
5810}
47ad8475 5811#endif /* CONFIG_TRANSPARENT_HUGEPAGE || CONFIG_HUGETLBFS */
49076ec2 5812
40b64acd 5813#if USE_SPLIT_PTE_PTLOCKS && ALLOC_SPLIT_PTLOCKS
b35f1819
KS
5814
5815static struct kmem_cache *page_ptl_cachep;
5816
5817void __init ptlock_cache_init(void)
5818{
5819 page_ptl_cachep = kmem_cache_create("page->ptl", sizeof(spinlock_t), 0,
5820 SLAB_PANIC, NULL);
5821}
5822
539edb58 5823bool ptlock_alloc(struct page *page)
49076ec2
KS
5824{
5825 spinlock_t *ptl;
5826
b35f1819 5827 ptl = kmem_cache_alloc(page_ptl_cachep, GFP_KERNEL);
49076ec2
KS
5828 if (!ptl)
5829 return false;
539edb58 5830 page->ptl = ptl;
49076ec2
KS
5831 return true;
5832}
5833
539edb58 5834void ptlock_free(struct page *page)
49076ec2 5835{
b35f1819 5836 kmem_cache_free(page_ptl_cachep, page->ptl);
49076ec2
KS
5837}
5838#endif