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