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