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