1 /* 2 * linux/mm/memory.c 3 * 4 * Copyright (C) 1991, 1992, 1993, 1994 Linus Torvalds 5 */ 6 7 /* 8 * demand-loading started 01.12.91 - seems it is high on the list of 9 * things wanted, and it should be easy to implement. - Linus 10 */ 11 12 /* 13 * Ok, demand-loading was easy, shared pages a little bit tricker. Shared 14 * pages started 02.12.91, seems to work. - Linus. 15 * 16 * Tested sharing by executing about 30 /bin/sh: under the old kernel it 17 * would have taken more than the 6M I have free, but it worked well as 18 * far as I could see. 19 * 20 * Also corrected some "invalidate()"s - I wasn't doing enough of them. 21 */ 22 23 /* 24 * Real VM (paging to/from disk) started 18.12.91. Much more work and 25 * thought has to go into this. Oh, well.. 26 * 19.12.91 - works, somewhat. Sometimes I get faults, don't know why. 27 * Found it. Everything seems to work now. 28 * 20.12.91 - Ok, making the swap-device changeable like the root. 29 */ 30 31 /* 32 * 05.04.94 - Multi-page memory management added for v1.1. 33 * Idea by Alex Bligh (alex@cconcepts.co.uk) 34 * 35 * 16.07.99 - Support of BIGMEM added by Gerhard Wichert, Siemens AG 36 * (Gerhard.Wichert@pdb.siemens.de) 37 * 38 * Aug/Sep 2004 Changed to four level page tables (Andi Kleen) 39 */ 40 41 #include <linux/kernel_stat.h> 42 #include <linux/mm.h> 43 #include <linux/sched/mm.h> 44 #include <linux/sched/coredump.h> 45 #include <linux/sched/numa_balancing.h> 46 #include <linux/hugetlb.h> 47 #include <linux/mman.h> 48 #include <linux/swap.h> 49 #include <linux/highmem.h> 50 #include <linux/pagemap.h> 51 #include <linux/ksm.h> 52 #include <linux/rmap.h> 53 #include <linux/export.h> 54 #include <linux/delayacct.h> 55 #include <linux/init.h> 56 #include <linux/pfn_t.h> 57 #include <linux/writeback.h> 58 #include <linux/memcontrol.h> 59 #include <linux/mmu_notifier.h> 60 #include <linux/kallsyms.h> 61 #include <linux/swapops.h> 62 #include <linux/elf.h> 63 #include <linux/gfp.h> 64 #include <linux/migrate.h> 65 #include <linux/string.h> 66 #include <linux/dma-debug.h> 67 #include <linux/debugfs.h> 68 #include <linux/userfaultfd_k.h> 69 #include <linux/dax.h> 70 71 #include <asm/io.h> 72 #include <asm/mmu_context.h> 73 #include <asm/pgalloc.h> 74 #include <linux/uaccess.h> 75 #include <asm/tlb.h> 76 #include <asm/tlbflush.h> 77 #include <asm/pgtable.h> 78 79 #include "internal.h" 80 81 #ifdef LAST_CPUPID_NOT_IN_PAGE_FLAGS 82 #warning Unfortunate NUMA and NUMA Balancing config, growing page-frame for last_cpupid. 83 #endif 84 85 #ifndef CONFIG_NEED_MULTIPLE_NODES 86 /* use the per-pgdat data instead for discontigmem - mbligh */ 87 unsigned long max_mapnr; 88 EXPORT_SYMBOL(max_mapnr); 89 90 struct page *mem_map; 91 EXPORT_SYMBOL(mem_map); 92 #endif 93 94 /* 95 * A number of key systems in x86 including ioremap() rely on the assumption 96 * that high_memory defines the upper bound on direct map memory, then end 97 * of ZONE_NORMAL. Under CONFIG_DISCONTIG this means that max_low_pfn and 98 * highstart_pfn must be the same; there must be no gap between ZONE_NORMAL 99 * and ZONE_HIGHMEM. 100 */ 101 void *high_memory; 102 EXPORT_SYMBOL(high_memory); 103 104 /* 105 * Randomize the address space (stacks, mmaps, brk, etc.). 106 * 107 * ( When CONFIG_COMPAT_BRK=y we exclude brk from randomization, 108 * as ancient (libc5 based) binaries can segfault. ) 109 */ 110 int randomize_va_space __read_mostly = 111 #ifdef CONFIG_COMPAT_BRK 112 1; 113 #else 114 2; 115 #endif 116 117 static int __init disable_randmaps(char *s) 118 { 119 randomize_va_space = 0; 120 return 1; 121 } 122 __setup("norandmaps", disable_randmaps); 123 124 unsigned long zero_pfn __read_mostly; 125 EXPORT_SYMBOL(zero_pfn); 126 127 unsigned long highest_memmap_pfn __read_mostly; 128 129 /* 130 * CONFIG_MMU architectures set up ZERO_PAGE in their paging_init() 131 */ 132 static int __init init_zero_pfn(void) 133 { 134 zero_pfn = page_to_pfn(ZERO_PAGE(0)); 135 return 0; 136 } 137 core_initcall(init_zero_pfn); 138 139 140 #if defined(SPLIT_RSS_COUNTING) 141 142 void sync_mm_rss(struct mm_struct *mm) 143 { 144 int i; 145 146 for (i = 0; i < NR_MM_COUNTERS; i++) { 147 if (current->rss_stat.count[i]) { 148 add_mm_counter(mm, i, current->rss_stat.count[i]); 149 current->rss_stat.count[i] = 0; 150 } 151 } 152 current->rss_stat.events = 0; 153 } 154 155 static void add_mm_counter_fast(struct mm_struct *mm, int member, int val) 156 { 157 struct task_struct *task = current; 158 159 if (likely(task->mm == mm)) 160 task->rss_stat.count[member] += val; 161 else 162 add_mm_counter(mm, member, val); 163 } 164 #define inc_mm_counter_fast(mm, member) add_mm_counter_fast(mm, member, 1) 165 #define dec_mm_counter_fast(mm, member) add_mm_counter_fast(mm, member, -1) 166 167 /* sync counter once per 64 page faults */ 168 #define TASK_RSS_EVENTS_THRESH (64) 169 static void check_sync_rss_stat(struct task_struct *task) 170 { 171 if (unlikely(task != current)) 172 return; 173 if (unlikely(task->rss_stat.events++ > TASK_RSS_EVENTS_THRESH)) 174 sync_mm_rss(task->mm); 175 } 176 #else /* SPLIT_RSS_COUNTING */ 177 178 #define inc_mm_counter_fast(mm, member) inc_mm_counter(mm, member) 179 #define dec_mm_counter_fast(mm, member) dec_mm_counter(mm, member) 180 181 static void check_sync_rss_stat(struct task_struct *task) 182 { 183 } 184 185 #endif /* SPLIT_RSS_COUNTING */ 186 187 #ifdef HAVE_GENERIC_MMU_GATHER 188 189 static bool tlb_next_batch(struct mmu_gather *tlb) 190 { 191 struct mmu_gather_batch *batch; 192 193 batch = tlb->active; 194 if (batch->next) { 195 tlb->active = batch->next; 196 return true; 197 } 198 199 if (tlb->batch_count == MAX_GATHER_BATCH_COUNT) 200 return false; 201 202 batch = (void *)__get_free_pages(GFP_NOWAIT | __GFP_NOWARN, 0); 203 if (!batch) 204 return false; 205 206 tlb->batch_count++; 207 batch->next = NULL; 208 batch->nr = 0; 209 batch->max = MAX_GATHER_BATCH; 210 211 tlb->active->next = batch; 212 tlb->active = batch; 213 214 return true; 215 } 216 217 /* tlb_gather_mmu 218 * Called to initialize an (on-stack) mmu_gather structure for page-table 219 * tear-down from @mm. The @fullmm argument is used when @mm is without 220 * users and we're going to destroy the full address space (exit/execve). 221 */ 222 void tlb_gather_mmu(struct mmu_gather *tlb, struct mm_struct *mm, unsigned long start, unsigned long end) 223 { 224 tlb->mm = mm; 225 226 /* Is it from 0 to ~0? */ 227 tlb->fullmm = !(start | (end+1)); 228 tlb->need_flush_all = 0; 229 tlb->local.next = NULL; 230 tlb->local.nr = 0; 231 tlb->local.max = ARRAY_SIZE(tlb->__pages); 232 tlb->active = &tlb->local; 233 tlb->batch_count = 0; 234 235 #ifdef CONFIG_HAVE_RCU_TABLE_FREE 236 tlb->batch = NULL; 237 #endif 238 tlb->page_size = 0; 239 240 __tlb_reset_range(tlb); 241 } 242 243 static void tlb_flush_mmu_tlbonly(struct mmu_gather *tlb) 244 { 245 if (!tlb->end) 246 return; 247 248 tlb_flush(tlb); 249 mmu_notifier_invalidate_range(tlb->mm, tlb->start, tlb->end); 250 #ifdef CONFIG_HAVE_RCU_TABLE_FREE 251 tlb_table_flush(tlb); 252 #endif 253 __tlb_reset_range(tlb); 254 } 255 256 static void tlb_flush_mmu_free(struct mmu_gather *tlb) 257 { 258 struct mmu_gather_batch *batch; 259 260 for (batch = &tlb->local; batch && batch->nr; batch = batch->next) { 261 free_pages_and_swap_cache(batch->pages, batch->nr); 262 batch->nr = 0; 263 } 264 tlb->active = &tlb->local; 265 } 266 267 void tlb_flush_mmu(struct mmu_gather *tlb) 268 { 269 tlb_flush_mmu_tlbonly(tlb); 270 tlb_flush_mmu_free(tlb); 271 } 272 273 /* tlb_finish_mmu 274 * Called at the end of the shootdown operation to free up any resources 275 * that were required. 276 */ 277 void tlb_finish_mmu(struct mmu_gather *tlb, unsigned long start, unsigned long end) 278 { 279 struct mmu_gather_batch *batch, *next; 280 281 tlb_flush_mmu(tlb); 282 283 /* keep the page table cache within bounds */ 284 check_pgt_cache(); 285 286 for (batch = tlb->local.next; batch; batch = next) { 287 next = batch->next; 288 free_pages((unsigned long)batch, 0); 289 } 290 tlb->local.next = NULL; 291 } 292 293 /* __tlb_remove_page 294 * Must perform the equivalent to __free_pte(pte_get_and_clear(ptep)), while 295 * handling the additional races in SMP caused by other CPUs caching valid 296 * mappings in their TLBs. Returns the number of free page slots left. 297 * When out of page slots we must call tlb_flush_mmu(). 298 *returns true if the caller should flush. 299 */ 300 bool __tlb_remove_page_size(struct mmu_gather *tlb, struct page *page, int page_size) 301 { 302 struct mmu_gather_batch *batch; 303 304 VM_BUG_ON(!tlb->end); 305 VM_WARN_ON(tlb->page_size != page_size); 306 307 batch = tlb->active; 308 /* 309 * Add the page and check if we are full. If so 310 * force a flush. 311 */ 312 batch->pages[batch->nr++] = page; 313 if (batch->nr == batch->max) { 314 if (!tlb_next_batch(tlb)) 315 return true; 316 batch = tlb->active; 317 } 318 VM_BUG_ON_PAGE(batch->nr > batch->max, page); 319 320 return false; 321 } 322 323 #endif /* HAVE_GENERIC_MMU_GATHER */ 324 325 #ifdef CONFIG_HAVE_RCU_TABLE_FREE 326 327 /* 328 * See the comment near struct mmu_table_batch. 329 */ 330 331 static void tlb_remove_table_smp_sync(void *arg) 332 { 333 /* Simply deliver the interrupt */ 334 } 335 336 static void tlb_remove_table_one(void *table) 337 { 338 /* 339 * This isn't an RCU grace period and hence the page-tables cannot be 340 * assumed to be actually RCU-freed. 341 * 342 * It is however sufficient for software page-table walkers that rely on 343 * IRQ disabling. See the comment near struct mmu_table_batch. 344 */ 345 smp_call_function(tlb_remove_table_smp_sync, NULL, 1); 346 __tlb_remove_table(table); 347 } 348 349 static void tlb_remove_table_rcu(struct rcu_head *head) 350 { 351 struct mmu_table_batch *batch; 352 int i; 353 354 batch = container_of(head, struct mmu_table_batch, rcu); 355 356 for (i = 0; i < batch->nr; i++) 357 __tlb_remove_table(batch->tables[i]); 358 359 free_page((unsigned long)batch); 360 } 361 362 void tlb_table_flush(struct mmu_gather *tlb) 363 { 364 struct mmu_table_batch **batch = &tlb->batch; 365 366 if (*batch) { 367 call_rcu_sched(&(*batch)->rcu, tlb_remove_table_rcu); 368 *batch = NULL; 369 } 370 } 371 372 void tlb_remove_table(struct mmu_gather *tlb, void *table) 373 { 374 struct mmu_table_batch **batch = &tlb->batch; 375 376 /* 377 * When there's less then two users of this mm there cannot be a 378 * concurrent page-table walk. 379 */ 380 if (atomic_read(&tlb->mm->mm_users) < 2) { 381 __tlb_remove_table(table); 382 return; 383 } 384 385 if (*batch == NULL) { 386 *batch = (struct mmu_table_batch *)__get_free_page(GFP_NOWAIT | __GFP_NOWARN); 387 if (*batch == NULL) { 388 tlb_remove_table_one(table); 389 return; 390 } 391 (*batch)->nr = 0; 392 } 393 (*batch)->tables[(*batch)->nr++] = table; 394 if ((*batch)->nr == MAX_TABLE_BATCH) 395 tlb_table_flush(tlb); 396 } 397 398 #endif /* CONFIG_HAVE_RCU_TABLE_FREE */ 399 400 /* 401 * Note: this doesn't free the actual pages themselves. That 402 * has been handled earlier when unmapping all the memory regions. 403 */ 404 static void free_pte_range(struct mmu_gather *tlb, pmd_t *pmd, 405 unsigned long addr) 406 { 407 pgtable_t token = pmd_pgtable(*pmd); 408 pmd_clear(pmd); 409 pte_free_tlb(tlb, token, addr); 410 atomic_long_dec(&tlb->mm->nr_ptes); 411 } 412 413 static inline void free_pmd_range(struct mmu_gather *tlb, pud_t *pud, 414 unsigned long addr, unsigned long end, 415 unsigned long floor, unsigned long ceiling) 416 { 417 pmd_t *pmd; 418 unsigned long next; 419 unsigned long start; 420 421 start = addr; 422 pmd = pmd_offset(pud, addr); 423 do { 424 next = pmd_addr_end(addr, end); 425 if (pmd_none_or_clear_bad(pmd)) 426 continue; 427 free_pte_range(tlb, pmd, addr); 428 } while (pmd++, addr = next, addr != end); 429 430 start &= PUD_MASK; 431 if (start < floor) 432 return; 433 if (ceiling) { 434 ceiling &= PUD_MASK; 435 if (!ceiling) 436 return; 437 } 438 if (end - 1 > ceiling - 1) 439 return; 440 441 pmd = pmd_offset(pud, start); 442 pud_clear(pud); 443 pmd_free_tlb(tlb, pmd, start); 444 mm_dec_nr_pmds(tlb->mm); 445 } 446 447 static inline void free_pud_range(struct mmu_gather *tlb, pgd_t *pgd, 448 unsigned long addr, unsigned long end, 449 unsigned long floor, unsigned long ceiling) 450 { 451 pud_t *pud; 452 unsigned long next; 453 unsigned long start; 454 455 start = addr; 456 pud = pud_offset(pgd, addr); 457 do { 458 next = pud_addr_end(addr, end); 459 if (pud_none_or_clear_bad(pud)) 460 continue; 461 free_pmd_range(tlb, pud, addr, next, floor, ceiling); 462 } while (pud++, addr = next, addr != end); 463 464 start &= PGDIR_MASK; 465 if (start < floor) 466 return; 467 if (ceiling) { 468 ceiling &= PGDIR_MASK; 469 if (!ceiling) 470 return; 471 } 472 if (end - 1 > ceiling - 1) 473 return; 474 475 pud = pud_offset(pgd, start); 476 pgd_clear(pgd); 477 pud_free_tlb(tlb, pud, start); 478 } 479 480 /* 481 * This function frees user-level page tables of a process. 482 */ 483 void free_pgd_range(struct mmu_gather *tlb, 484 unsigned long addr, unsigned long end, 485 unsigned long floor, unsigned long ceiling) 486 { 487 pgd_t *pgd; 488 unsigned long next; 489 490 /* 491 * The next few lines have given us lots of grief... 492 * 493 * Why are we testing PMD* at this top level? Because often 494 * there will be no work to do at all, and we'd prefer not to 495 * go all the way down to the bottom just to discover that. 496 * 497 * Why all these "- 1"s? Because 0 represents both the bottom 498 * of the address space and the top of it (using -1 for the 499 * top wouldn't help much: the masks would do the wrong thing). 500 * The rule is that addr 0 and floor 0 refer to the bottom of 501 * the address space, but end 0 and ceiling 0 refer to the top 502 * Comparisons need to use "end - 1" and "ceiling - 1" (though 503 * that end 0 case should be mythical). 504 * 505 * Wherever addr is brought up or ceiling brought down, we must 506 * be careful to reject "the opposite 0" before it confuses the 507 * subsequent tests. But what about where end is brought down 508 * by PMD_SIZE below? no, end can't go down to 0 there. 509 * 510 * Whereas we round start (addr) and ceiling down, by different 511 * masks at different levels, in order to test whether a table 512 * now has no other vmas using it, so can be freed, we don't 513 * bother to round floor or end up - the tests don't need that. 514 */ 515 516 addr &= PMD_MASK; 517 if (addr < floor) { 518 addr += PMD_SIZE; 519 if (!addr) 520 return; 521 } 522 if (ceiling) { 523 ceiling &= PMD_MASK; 524 if (!ceiling) 525 return; 526 } 527 if (end - 1 > ceiling - 1) 528 end -= PMD_SIZE; 529 if (addr > end - 1) 530 return; 531 /* 532 * We add page table cache pages with PAGE_SIZE, 533 * (see pte_free_tlb()), flush the tlb if we need 534 */ 535 tlb_remove_check_page_size_change(tlb, PAGE_SIZE); 536 pgd = pgd_offset(tlb->mm, addr); 537 do { 538 next = pgd_addr_end(addr, end); 539 if (pgd_none_or_clear_bad(pgd)) 540 continue; 541 free_pud_range(tlb, pgd, addr, next, floor, ceiling); 542 } while (pgd++, addr = next, addr != end); 543 } 544 545 void free_pgtables(struct mmu_gather *tlb, struct vm_area_struct *vma, 546 unsigned long floor, unsigned long ceiling) 547 { 548 while (vma) { 549 struct vm_area_struct *next = vma->vm_next; 550 unsigned long addr = vma->vm_start; 551 552 /* 553 * Hide vma from rmap and truncate_pagecache before freeing 554 * pgtables 555 */ 556 unlink_anon_vmas(vma); 557 unlink_file_vma(vma); 558 559 if (is_vm_hugetlb_page(vma)) { 560 hugetlb_free_pgd_range(tlb, addr, vma->vm_end, 561 floor, next ? next->vm_start : ceiling); 562 } else { 563 /* 564 * Optimization: gather nearby vmas into one call down 565 */ 566 while (next && next->vm_start <= vma->vm_end + PMD_SIZE 567 && !is_vm_hugetlb_page(next)) { 568 vma = next; 569 next = vma->vm_next; 570 unlink_anon_vmas(vma); 571 unlink_file_vma(vma); 572 } 573 free_pgd_range(tlb, addr, vma->vm_end, 574 floor, next ? next->vm_start : ceiling); 575 } 576 vma = next; 577 } 578 } 579 580 int __pte_alloc(struct mm_struct *mm, pmd_t *pmd, unsigned long address) 581 { 582 spinlock_t *ptl; 583 pgtable_t new = pte_alloc_one(mm, address); 584 if (!new) 585 return -ENOMEM; 586 587 /* 588 * Ensure all pte setup (eg. pte page lock and page clearing) are 589 * visible before the pte is made visible to other CPUs by being 590 * put into page tables. 591 * 592 * The other side of the story is the pointer chasing in the page 593 * table walking code (when walking the page table without locking; 594 * ie. most of the time). Fortunately, these data accesses consist 595 * of a chain of data-dependent loads, meaning most CPUs (alpha 596 * being the notable exception) will already guarantee loads are 597 * seen in-order. See the alpha page table accessors for the 598 * smp_read_barrier_depends() barriers in page table walking code. 599 */ 600 smp_wmb(); /* Could be smp_wmb__xxx(before|after)_spin_lock */ 601 602 ptl = pmd_lock(mm, pmd); 603 if (likely(pmd_none(*pmd))) { /* Has another populated it ? */ 604 atomic_long_inc(&mm->nr_ptes); 605 pmd_populate(mm, pmd, new); 606 new = NULL; 607 } 608 spin_unlock(ptl); 609 if (new) 610 pte_free(mm, new); 611 return 0; 612 } 613 614 int __pte_alloc_kernel(pmd_t *pmd, unsigned long address) 615 { 616 pte_t *new = pte_alloc_one_kernel(&init_mm, address); 617 if (!new) 618 return -ENOMEM; 619 620 smp_wmb(); /* See comment in __pte_alloc */ 621 622 spin_lock(&init_mm.page_table_lock); 623 if (likely(pmd_none(*pmd))) { /* Has another populated it ? */ 624 pmd_populate_kernel(&init_mm, pmd, new); 625 new = NULL; 626 } 627 spin_unlock(&init_mm.page_table_lock); 628 if (new) 629 pte_free_kernel(&init_mm, new); 630 return 0; 631 } 632 633 static inline void init_rss_vec(int *rss) 634 { 635 memset(rss, 0, sizeof(int) * NR_MM_COUNTERS); 636 } 637 638 static inline void add_mm_rss_vec(struct mm_struct *mm, int *rss) 639 { 640 int i; 641 642 if (current->mm == mm) 643 sync_mm_rss(mm); 644 for (i = 0; i < NR_MM_COUNTERS; i++) 645 if (rss[i]) 646 add_mm_counter(mm, i, rss[i]); 647 } 648 649 /* 650 * This function is called to print an error when a bad pte 651 * is found. For example, we might have a PFN-mapped pte in 652 * a region that doesn't allow it. 653 * 654 * The calling function must still handle the error. 655 */ 656 static void print_bad_pte(struct vm_area_struct *vma, unsigned long addr, 657 pte_t pte, struct page *page) 658 { 659 pgd_t *pgd = pgd_offset(vma->vm_mm, addr); 660 pud_t *pud = pud_offset(pgd, addr); 661 pmd_t *pmd = pmd_offset(pud, addr); 662 struct address_space *mapping; 663 pgoff_t index; 664 static unsigned long resume; 665 static unsigned long nr_shown; 666 static unsigned long nr_unshown; 667 668 /* 669 * Allow a burst of 60 reports, then keep quiet for that minute; 670 * or allow a steady drip of one report per second. 671 */ 672 if (nr_shown == 60) { 673 if (time_before(jiffies, resume)) { 674 nr_unshown++; 675 return; 676 } 677 if (nr_unshown) { 678 pr_alert("BUG: Bad page map: %lu messages suppressed\n", 679 nr_unshown); 680 nr_unshown = 0; 681 } 682 nr_shown = 0; 683 } 684 if (nr_shown++ == 0) 685 resume = jiffies + 60 * HZ; 686 687 mapping = vma->vm_file ? vma->vm_file->f_mapping : NULL; 688 index = linear_page_index(vma, addr); 689 690 pr_alert("BUG: Bad page map in process %s pte:%08llx pmd:%08llx\n", 691 current->comm, 692 (long long)pte_val(pte), (long long)pmd_val(*pmd)); 693 if (page) 694 dump_page(page, "bad pte"); 695 pr_alert("addr:%p vm_flags:%08lx anon_vma:%p mapping:%p index:%lx\n", 696 (void *)addr, vma->vm_flags, vma->anon_vma, mapping, index); 697 /* 698 * Choose text because data symbols depend on CONFIG_KALLSYMS_ALL=y 699 */ 700 pr_alert("file:%pD fault:%pf mmap:%pf readpage:%pf\n", 701 vma->vm_file, 702 vma->vm_ops ? vma->vm_ops->fault : NULL, 703 vma->vm_file ? vma->vm_file->f_op->mmap : NULL, 704 mapping ? mapping->a_ops->readpage : NULL); 705 dump_stack(); 706 add_taint(TAINT_BAD_PAGE, LOCKDEP_NOW_UNRELIABLE); 707 } 708 709 /* 710 * vm_normal_page -- This function gets the "struct page" associated with a pte. 711 * 712 * "Special" mappings do not wish to be associated with a "struct page" (either 713 * it doesn't exist, or it exists but they don't want to touch it). In this 714 * case, NULL is returned here. "Normal" mappings do have a struct page. 715 * 716 * There are 2 broad cases. Firstly, an architecture may define a pte_special() 717 * pte bit, in which case this function is trivial. Secondly, an architecture 718 * may not have a spare pte bit, which requires a more complicated scheme, 719 * described below. 720 * 721 * A raw VM_PFNMAP mapping (ie. one that is not COWed) is always considered a 722 * special mapping (even if there are underlying and valid "struct pages"). 723 * COWed pages of a VM_PFNMAP are always normal. 724 * 725 * The way we recognize COWed pages within VM_PFNMAP mappings is through the 726 * rules set up by "remap_pfn_range()": the vma will have the VM_PFNMAP bit 727 * set, and the vm_pgoff will point to the first PFN mapped: thus every special 728 * mapping will always honor the rule 729 * 730 * pfn_of_page == vma->vm_pgoff + ((addr - vma->vm_start) >> PAGE_SHIFT) 731 * 732 * And for normal mappings this is false. 733 * 734 * This restricts such mappings to be a linear translation from virtual address 735 * to pfn. To get around this restriction, we allow arbitrary mappings so long 736 * as the vma is not a COW mapping; in that case, we know that all ptes are 737 * special (because none can have been COWed). 738 * 739 * 740 * In order to support COW of arbitrary special mappings, we have VM_MIXEDMAP. 741 * 742 * VM_MIXEDMAP mappings can likewise contain memory with or without "struct 743 * page" backing, however the difference is that _all_ pages with a struct 744 * page (that is, those where pfn_valid is true) are refcounted and considered 745 * normal pages by the VM. The disadvantage is that pages are refcounted 746 * (which can be slower and simply not an option for some PFNMAP users). The 747 * advantage is that we don't have to follow the strict linearity rule of 748 * PFNMAP mappings in order to support COWable mappings. 749 * 750 */ 751 #ifdef __HAVE_ARCH_PTE_SPECIAL 752 # define HAVE_PTE_SPECIAL 1 753 #else 754 # define HAVE_PTE_SPECIAL 0 755 #endif 756 struct page *vm_normal_page(struct vm_area_struct *vma, unsigned long addr, 757 pte_t pte) 758 { 759 unsigned long pfn = pte_pfn(pte); 760 761 if (HAVE_PTE_SPECIAL) { 762 if (likely(!pte_special(pte))) 763 goto check_pfn; 764 if (vma->vm_ops && vma->vm_ops->find_special_page) 765 return vma->vm_ops->find_special_page(vma, addr); 766 if (vma->vm_flags & (VM_PFNMAP | VM_MIXEDMAP)) 767 return NULL; 768 if (!is_zero_pfn(pfn)) 769 print_bad_pte(vma, addr, pte, NULL); 770 return NULL; 771 } 772 773 /* !HAVE_PTE_SPECIAL case follows: */ 774 775 if (unlikely(vma->vm_flags & (VM_PFNMAP|VM_MIXEDMAP))) { 776 if (vma->vm_flags & VM_MIXEDMAP) { 777 if (!pfn_valid(pfn)) 778 return NULL; 779 goto out; 780 } else { 781 unsigned long off; 782 off = (addr - vma->vm_start) >> PAGE_SHIFT; 783 if (pfn == vma->vm_pgoff + off) 784 return NULL; 785 if (!is_cow_mapping(vma->vm_flags)) 786 return NULL; 787 } 788 } 789 790 if (is_zero_pfn(pfn)) 791 return NULL; 792 check_pfn: 793 if (unlikely(pfn > highest_memmap_pfn)) { 794 print_bad_pte(vma, addr, pte, NULL); 795 return NULL; 796 } 797 798 /* 799 * NOTE! We still have PageReserved() pages in the page tables. 800 * eg. VDSO mappings can cause them to exist. 801 */ 802 out: 803 return pfn_to_page(pfn); 804 } 805 806 #ifdef CONFIG_TRANSPARENT_HUGEPAGE 807 struct page *vm_normal_page_pmd(struct vm_area_struct *vma, unsigned long addr, 808 pmd_t pmd) 809 { 810 unsigned long pfn = pmd_pfn(pmd); 811 812 /* 813 * There is no pmd_special() but there may be special pmds, e.g. 814 * in a direct-access (dax) mapping, so let's just replicate the 815 * !HAVE_PTE_SPECIAL case from vm_normal_page() here. 816 */ 817 if (unlikely(vma->vm_flags & (VM_PFNMAP|VM_MIXEDMAP))) { 818 if (vma->vm_flags & VM_MIXEDMAP) { 819 if (!pfn_valid(pfn)) 820 return NULL; 821 goto out; 822 } else { 823 unsigned long off; 824 off = (addr - vma->vm_start) >> PAGE_SHIFT; 825 if (pfn == vma->vm_pgoff + off) 826 return NULL; 827 if (!is_cow_mapping(vma->vm_flags)) 828 return NULL; 829 } 830 } 831 832 if (is_zero_pfn(pfn)) 833 return NULL; 834 if (unlikely(pfn > highest_memmap_pfn)) 835 return NULL; 836 837 /* 838 * NOTE! We still have PageReserved() pages in the page tables. 839 * eg. VDSO mappings can cause them to exist. 840 */ 841 out: 842 return pfn_to_page(pfn); 843 } 844 #endif 845 846 /* 847 * copy one vm_area from one task to the other. Assumes the page tables 848 * already present in the new task to be cleared in the whole range 849 * covered by this vma. 850 */ 851 852 static inline unsigned long 853 copy_one_pte(struct mm_struct *dst_mm, struct mm_struct *src_mm, 854 pte_t *dst_pte, pte_t *src_pte, struct vm_area_struct *vma, 855 unsigned long addr, int *rss) 856 { 857 unsigned long vm_flags = vma->vm_flags; 858 pte_t pte = *src_pte; 859 struct page *page; 860 861 /* pte contains position in swap or file, so copy. */ 862 if (unlikely(!pte_present(pte))) { 863 swp_entry_t entry = pte_to_swp_entry(pte); 864 865 if (likely(!non_swap_entry(entry))) { 866 if (swap_duplicate(entry) < 0) 867 return entry.val; 868 869 /* make sure dst_mm is on swapoff's mmlist. */ 870 if (unlikely(list_empty(&dst_mm->mmlist))) { 871 spin_lock(&mmlist_lock); 872 if (list_empty(&dst_mm->mmlist)) 873 list_add(&dst_mm->mmlist, 874 &src_mm->mmlist); 875 spin_unlock(&mmlist_lock); 876 } 877 rss[MM_SWAPENTS]++; 878 } else if (is_migration_entry(entry)) { 879 page = migration_entry_to_page(entry); 880 881 rss[mm_counter(page)]++; 882 883 if (is_write_migration_entry(entry) && 884 is_cow_mapping(vm_flags)) { 885 /* 886 * COW mappings require pages in both 887 * parent and child to be set to read. 888 */ 889 make_migration_entry_read(&entry); 890 pte = swp_entry_to_pte(entry); 891 if (pte_swp_soft_dirty(*src_pte)) 892 pte = pte_swp_mksoft_dirty(pte); 893 set_pte_at(src_mm, addr, src_pte, pte); 894 } 895 } 896 goto out_set_pte; 897 } 898 899 /* 900 * If it's a COW mapping, write protect it both 901 * in the parent and the child 902 */ 903 if (is_cow_mapping(vm_flags)) { 904 ptep_set_wrprotect(src_mm, addr, src_pte); 905 pte = pte_wrprotect(pte); 906 } 907 908 /* 909 * If it's a shared mapping, mark it clean in 910 * the child 911 */ 912 if (vm_flags & VM_SHARED) 913 pte = pte_mkclean(pte); 914 pte = pte_mkold(pte); 915 916 page = vm_normal_page(vma, addr, pte); 917 if (page) { 918 get_page(page); 919 page_dup_rmap(page, false); 920 rss[mm_counter(page)]++; 921 } 922 923 out_set_pte: 924 set_pte_at(dst_mm, addr, dst_pte, pte); 925 return 0; 926 } 927 928 static int copy_pte_range(struct mm_struct *dst_mm, struct mm_struct *src_mm, 929 pmd_t *dst_pmd, pmd_t *src_pmd, struct vm_area_struct *vma, 930 unsigned long addr, unsigned long end) 931 { 932 pte_t *orig_src_pte, *orig_dst_pte; 933 pte_t *src_pte, *dst_pte; 934 spinlock_t *src_ptl, *dst_ptl; 935 int progress = 0; 936 int rss[NR_MM_COUNTERS]; 937 swp_entry_t entry = (swp_entry_t){0}; 938 939 again: 940 init_rss_vec(rss); 941 942 dst_pte = pte_alloc_map_lock(dst_mm, dst_pmd, addr, &dst_ptl); 943 if (!dst_pte) 944 return -ENOMEM; 945 src_pte = pte_offset_map(src_pmd, addr); 946 src_ptl = pte_lockptr(src_mm, src_pmd); 947 spin_lock_nested(src_ptl, SINGLE_DEPTH_NESTING); 948 orig_src_pte = src_pte; 949 orig_dst_pte = dst_pte; 950 arch_enter_lazy_mmu_mode(); 951 952 do { 953 /* 954 * We are holding two locks at this point - either of them 955 * could generate latencies in another task on another CPU. 956 */ 957 if (progress >= 32) { 958 progress = 0; 959 if (need_resched() || 960 spin_needbreak(src_ptl) || spin_needbreak(dst_ptl)) 961 break; 962 } 963 if (pte_none(*src_pte)) { 964 progress++; 965 continue; 966 } 967 entry.val = copy_one_pte(dst_mm, src_mm, dst_pte, src_pte, 968 vma, addr, rss); 969 if (entry.val) 970 break; 971 progress += 8; 972 } while (dst_pte++, src_pte++, addr += PAGE_SIZE, addr != end); 973 974 arch_leave_lazy_mmu_mode(); 975 spin_unlock(src_ptl); 976 pte_unmap(orig_src_pte); 977 add_mm_rss_vec(dst_mm, rss); 978 pte_unmap_unlock(orig_dst_pte, dst_ptl); 979 cond_resched(); 980 981 if (entry.val) { 982 if (add_swap_count_continuation(entry, GFP_KERNEL) < 0) 983 return -ENOMEM; 984 progress = 0; 985 } 986 if (addr != end) 987 goto again; 988 return 0; 989 } 990 991 static inline int copy_pmd_range(struct mm_struct *dst_mm, struct mm_struct *src_mm, 992 pud_t *dst_pud, pud_t *src_pud, struct vm_area_struct *vma, 993 unsigned long addr, unsigned long end) 994 { 995 pmd_t *src_pmd, *dst_pmd; 996 unsigned long next; 997 998 dst_pmd = pmd_alloc(dst_mm, dst_pud, addr); 999 if (!dst_pmd) 1000 return -ENOMEM; 1001 src_pmd = pmd_offset(src_pud, addr); 1002 do { 1003 next = pmd_addr_end(addr, end); 1004 if (pmd_trans_huge(*src_pmd) || pmd_devmap(*src_pmd)) { 1005 int err; 1006 VM_BUG_ON_VMA(next-addr != HPAGE_PMD_SIZE, vma); 1007 err = copy_huge_pmd(dst_mm, src_mm, 1008 dst_pmd, src_pmd, addr, vma); 1009 if (err == -ENOMEM) 1010 return -ENOMEM; 1011 if (!err) 1012 continue; 1013 /* fall through */ 1014 } 1015 if (pmd_none_or_clear_bad(src_pmd)) 1016 continue; 1017 if (copy_pte_range(dst_mm, src_mm, dst_pmd, src_pmd, 1018 vma, addr, next)) 1019 return -ENOMEM; 1020 } while (dst_pmd++, src_pmd++, addr = next, addr != end); 1021 return 0; 1022 } 1023 1024 static inline int copy_pud_range(struct mm_struct *dst_mm, struct mm_struct *src_mm, 1025 pgd_t *dst_pgd, pgd_t *src_pgd, struct vm_area_struct *vma, 1026 unsigned long addr, unsigned long end) 1027 { 1028 pud_t *src_pud, *dst_pud; 1029 unsigned long next; 1030 1031 dst_pud = pud_alloc(dst_mm, dst_pgd, addr); 1032 if (!dst_pud) 1033 return -ENOMEM; 1034 src_pud = pud_offset(src_pgd, addr); 1035 do { 1036 next = pud_addr_end(addr, end); 1037 if (pud_trans_huge(*src_pud) || pud_devmap(*src_pud)) { 1038 int err; 1039 1040 VM_BUG_ON_VMA(next-addr != HPAGE_PUD_SIZE, vma); 1041 err = copy_huge_pud(dst_mm, src_mm, 1042 dst_pud, src_pud, addr, vma); 1043 if (err == -ENOMEM) 1044 return -ENOMEM; 1045 if (!err) 1046 continue; 1047 /* fall through */ 1048 } 1049 if (pud_none_or_clear_bad(src_pud)) 1050 continue; 1051 if (copy_pmd_range(dst_mm, src_mm, dst_pud, src_pud, 1052 vma, addr, next)) 1053 return -ENOMEM; 1054 } while (dst_pud++, src_pud++, addr = next, addr != end); 1055 return 0; 1056 } 1057 1058 int copy_page_range(struct mm_struct *dst_mm, struct mm_struct *src_mm, 1059 struct vm_area_struct *vma) 1060 { 1061 pgd_t *src_pgd, *dst_pgd; 1062 unsigned long next; 1063 unsigned long addr = vma->vm_start; 1064 unsigned long end = vma->vm_end; 1065 unsigned long mmun_start; /* For mmu_notifiers */ 1066 unsigned long mmun_end; /* For mmu_notifiers */ 1067 bool is_cow; 1068 int ret; 1069 1070 /* 1071 * Don't copy ptes where a page fault will fill them correctly. 1072 * Fork becomes much lighter when there are big shared or private 1073 * readonly mappings. The tradeoff is that copy_page_range is more 1074 * efficient than faulting. 1075 */ 1076 if (!(vma->vm_flags & (VM_HUGETLB | VM_PFNMAP | VM_MIXEDMAP)) && 1077 !vma->anon_vma) 1078 return 0; 1079 1080 if (is_vm_hugetlb_page(vma)) 1081 return copy_hugetlb_page_range(dst_mm, src_mm, vma); 1082 1083 if (unlikely(vma->vm_flags & VM_PFNMAP)) { 1084 /* 1085 * We do not free on error cases below as remove_vma 1086 * gets called on error from higher level routine 1087 */ 1088 ret = track_pfn_copy(vma); 1089 if (ret) 1090 return ret; 1091 } 1092 1093 /* 1094 * We need to invalidate the secondary MMU mappings only when 1095 * there could be a permission downgrade on the ptes of the 1096 * parent mm. And a permission downgrade will only happen if 1097 * is_cow_mapping() returns true. 1098 */ 1099 is_cow = is_cow_mapping(vma->vm_flags); 1100 mmun_start = addr; 1101 mmun_end = end; 1102 if (is_cow) 1103 mmu_notifier_invalidate_range_start(src_mm, mmun_start, 1104 mmun_end); 1105 1106 ret = 0; 1107 dst_pgd = pgd_offset(dst_mm, addr); 1108 src_pgd = pgd_offset(src_mm, addr); 1109 do { 1110 next = pgd_addr_end(addr, end); 1111 if (pgd_none_or_clear_bad(src_pgd)) 1112 continue; 1113 if (unlikely(copy_pud_range(dst_mm, src_mm, dst_pgd, src_pgd, 1114 vma, addr, next))) { 1115 ret = -ENOMEM; 1116 break; 1117 } 1118 } while (dst_pgd++, src_pgd++, addr = next, addr != end); 1119 1120 if (is_cow) 1121 mmu_notifier_invalidate_range_end(src_mm, mmun_start, mmun_end); 1122 return ret; 1123 } 1124 1125 static unsigned long zap_pte_range(struct mmu_gather *tlb, 1126 struct vm_area_struct *vma, pmd_t *pmd, 1127 unsigned long addr, unsigned long end, 1128 struct zap_details *details) 1129 { 1130 struct mm_struct *mm = tlb->mm; 1131 int force_flush = 0; 1132 int rss[NR_MM_COUNTERS]; 1133 spinlock_t *ptl; 1134 pte_t *start_pte; 1135 pte_t *pte; 1136 swp_entry_t entry; 1137 1138 tlb_remove_check_page_size_change(tlb, PAGE_SIZE); 1139 again: 1140 init_rss_vec(rss); 1141 start_pte = pte_offset_map_lock(mm, pmd, addr, &ptl); 1142 pte = start_pte; 1143 arch_enter_lazy_mmu_mode(); 1144 do { 1145 pte_t ptent = *pte; 1146 if (pte_none(ptent)) 1147 continue; 1148 1149 if (pte_present(ptent)) { 1150 struct page *page; 1151 1152 page = vm_normal_page(vma, addr, ptent); 1153 if (unlikely(details) && page) { 1154 /* 1155 * unmap_shared_mapping_pages() wants to 1156 * invalidate cache without truncating: 1157 * unmap shared but keep private pages. 1158 */ 1159 if (details->check_mapping && 1160 details->check_mapping != page_rmapping(page)) 1161 continue; 1162 } 1163 ptent = ptep_get_and_clear_full(mm, addr, pte, 1164 tlb->fullmm); 1165 tlb_remove_tlb_entry(tlb, pte, addr); 1166 if (unlikely(!page)) 1167 continue; 1168 1169 if (!PageAnon(page)) { 1170 if (pte_dirty(ptent)) { 1171 force_flush = 1; 1172 set_page_dirty(page); 1173 } 1174 if (pte_young(ptent) && 1175 likely(!(vma->vm_flags & VM_SEQ_READ))) 1176 mark_page_accessed(page); 1177 } 1178 rss[mm_counter(page)]--; 1179 page_remove_rmap(page, false); 1180 if (unlikely(page_mapcount(page) < 0)) 1181 print_bad_pte(vma, addr, ptent, page); 1182 if (unlikely(__tlb_remove_page(tlb, page))) { 1183 force_flush = 1; 1184 addr += PAGE_SIZE; 1185 break; 1186 } 1187 continue; 1188 } 1189 /* If details->check_mapping, we leave swap entries. */ 1190 if (unlikely(details)) 1191 continue; 1192 1193 entry = pte_to_swp_entry(ptent); 1194 if (!non_swap_entry(entry)) 1195 rss[MM_SWAPENTS]--; 1196 else if (is_migration_entry(entry)) { 1197 struct page *page; 1198 1199 page = migration_entry_to_page(entry); 1200 rss[mm_counter(page)]--; 1201 } 1202 if (unlikely(!free_swap_and_cache(entry))) 1203 print_bad_pte(vma, addr, ptent, NULL); 1204 pte_clear_not_present_full(mm, addr, pte, tlb->fullmm); 1205 } while (pte++, addr += PAGE_SIZE, addr != end); 1206 1207 add_mm_rss_vec(mm, rss); 1208 arch_leave_lazy_mmu_mode(); 1209 1210 /* Do the actual TLB flush before dropping ptl */ 1211 if (force_flush) 1212 tlb_flush_mmu_tlbonly(tlb); 1213 pte_unmap_unlock(start_pte, ptl); 1214 1215 /* 1216 * If we forced a TLB flush (either due to running out of 1217 * batch buffers or because we needed to flush dirty TLB 1218 * entries before releasing the ptl), free the batched 1219 * memory too. Restart if we didn't do everything. 1220 */ 1221 if (force_flush) { 1222 force_flush = 0; 1223 tlb_flush_mmu_free(tlb); 1224 if (addr != end) 1225 goto again; 1226 } 1227 1228 return addr; 1229 } 1230 1231 static inline unsigned long zap_pmd_range(struct mmu_gather *tlb, 1232 struct vm_area_struct *vma, pud_t *pud, 1233 unsigned long addr, unsigned long end, 1234 struct zap_details *details) 1235 { 1236 pmd_t *pmd; 1237 unsigned long next; 1238 1239 pmd = pmd_offset(pud, addr); 1240 do { 1241 next = pmd_addr_end(addr, end); 1242 if (pmd_trans_huge(*pmd) || pmd_devmap(*pmd)) { 1243 if (next - addr != HPAGE_PMD_SIZE) { 1244 VM_BUG_ON_VMA(vma_is_anonymous(vma) && 1245 !rwsem_is_locked(&tlb->mm->mmap_sem), vma); 1246 __split_huge_pmd(vma, pmd, addr, false, NULL); 1247 } else if (zap_huge_pmd(tlb, vma, pmd, addr)) 1248 goto next; 1249 /* fall through */ 1250 } 1251 /* 1252 * Here there can be other concurrent MADV_DONTNEED or 1253 * trans huge page faults running, and if the pmd is 1254 * none or trans huge it can change under us. This is 1255 * because MADV_DONTNEED holds the mmap_sem in read 1256 * mode. 1257 */ 1258 if (pmd_none_or_trans_huge_or_clear_bad(pmd)) 1259 goto next; 1260 next = zap_pte_range(tlb, vma, pmd, addr, next, details); 1261 next: 1262 cond_resched(); 1263 } while (pmd++, addr = next, addr != end); 1264 1265 return addr; 1266 } 1267 1268 static inline unsigned long zap_pud_range(struct mmu_gather *tlb, 1269 struct vm_area_struct *vma, pgd_t *pgd, 1270 unsigned long addr, unsigned long end, 1271 struct zap_details *details) 1272 { 1273 pud_t *pud; 1274 unsigned long next; 1275 1276 pud = pud_offset(pgd, addr); 1277 do { 1278 next = pud_addr_end(addr, end); 1279 if (pud_trans_huge(*pud) || pud_devmap(*pud)) { 1280 if (next - addr != HPAGE_PUD_SIZE) { 1281 VM_BUG_ON_VMA(!rwsem_is_locked(&tlb->mm->mmap_sem), vma); 1282 split_huge_pud(vma, pud, addr); 1283 } else if (zap_huge_pud(tlb, vma, pud, addr)) 1284 goto next; 1285 /* fall through */ 1286 } 1287 if (pud_none_or_clear_bad(pud)) 1288 continue; 1289 next = zap_pmd_range(tlb, vma, pud, addr, next, details); 1290 next: 1291 cond_resched(); 1292 } while (pud++, addr = next, addr != end); 1293 1294 return addr; 1295 } 1296 1297 void unmap_page_range(struct mmu_gather *tlb, 1298 struct vm_area_struct *vma, 1299 unsigned long addr, unsigned long end, 1300 struct zap_details *details) 1301 { 1302 pgd_t *pgd; 1303 unsigned long next; 1304 1305 BUG_ON(addr >= end); 1306 tlb_start_vma(tlb, vma); 1307 pgd = pgd_offset(vma->vm_mm, addr); 1308 do { 1309 next = pgd_addr_end(addr, end); 1310 if (pgd_none_or_clear_bad(pgd)) 1311 continue; 1312 next = zap_pud_range(tlb, vma, pgd, addr, next, details); 1313 } while (pgd++, addr = next, addr != end); 1314 tlb_end_vma(tlb, vma); 1315 } 1316 1317 1318 static void unmap_single_vma(struct mmu_gather *tlb, 1319 struct vm_area_struct *vma, unsigned long start_addr, 1320 unsigned long end_addr, 1321 struct zap_details *details) 1322 { 1323 unsigned long start = max(vma->vm_start, start_addr); 1324 unsigned long end; 1325 1326 if (start >= vma->vm_end) 1327 return; 1328 end = min(vma->vm_end, end_addr); 1329 if (end <= vma->vm_start) 1330 return; 1331 1332 if (vma->vm_file) 1333 uprobe_munmap(vma, start, end); 1334 1335 if (unlikely(vma->vm_flags & VM_PFNMAP)) 1336 untrack_pfn(vma, 0, 0); 1337 1338 if (start != end) { 1339 if (unlikely(is_vm_hugetlb_page(vma))) { 1340 /* 1341 * It is undesirable to test vma->vm_file as it 1342 * should be non-null for valid hugetlb area. 1343 * However, vm_file will be NULL in the error 1344 * cleanup path of mmap_region. When 1345 * hugetlbfs ->mmap method fails, 1346 * mmap_region() nullifies vma->vm_file 1347 * before calling this function to clean up. 1348 * Since no pte has actually been setup, it is 1349 * safe to do nothing in this case. 1350 */ 1351 if (vma->vm_file) { 1352 i_mmap_lock_write(vma->vm_file->f_mapping); 1353 __unmap_hugepage_range_final(tlb, vma, start, end, NULL); 1354 i_mmap_unlock_write(vma->vm_file->f_mapping); 1355 } 1356 } else 1357 unmap_page_range(tlb, vma, start, end, details); 1358 } 1359 } 1360 1361 /** 1362 * unmap_vmas - unmap a range of memory covered by a list of vma's 1363 * @tlb: address of the caller's struct mmu_gather 1364 * @vma: the starting vma 1365 * @start_addr: virtual address at which to start unmapping 1366 * @end_addr: virtual address at which to end unmapping 1367 * 1368 * Unmap all pages in the vma list. 1369 * 1370 * Only addresses between `start' and `end' will be unmapped. 1371 * 1372 * The VMA list must be sorted in ascending virtual address order. 1373 * 1374 * unmap_vmas() assumes that the caller will flush the whole unmapped address 1375 * range after unmap_vmas() returns. So the only responsibility here is to 1376 * ensure that any thus-far unmapped pages are flushed before unmap_vmas() 1377 * drops the lock and schedules. 1378 */ 1379 void unmap_vmas(struct mmu_gather *tlb, 1380 struct vm_area_struct *vma, unsigned long start_addr, 1381 unsigned long end_addr) 1382 { 1383 struct mm_struct *mm = vma->vm_mm; 1384 1385 mmu_notifier_invalidate_range_start(mm, start_addr, end_addr); 1386 for ( ; vma && vma->vm_start < end_addr; vma = vma->vm_next) 1387 unmap_single_vma(tlb, vma, start_addr, end_addr, NULL); 1388 mmu_notifier_invalidate_range_end(mm, start_addr, end_addr); 1389 } 1390 1391 /** 1392 * zap_page_range - remove user pages in a given range 1393 * @vma: vm_area_struct holding the applicable pages 1394 * @start: starting address of pages to zap 1395 * @size: number of bytes to zap 1396 * 1397 * Caller must protect the VMA list 1398 */ 1399 void zap_page_range(struct vm_area_struct *vma, unsigned long start, 1400 unsigned long size) 1401 { 1402 struct mm_struct *mm = vma->vm_mm; 1403 struct mmu_gather tlb; 1404 unsigned long end = start + size; 1405 1406 lru_add_drain(); 1407 tlb_gather_mmu(&tlb, mm, start, end); 1408 update_hiwater_rss(mm); 1409 mmu_notifier_invalidate_range_start(mm, start, end); 1410 for ( ; vma && vma->vm_start < end; vma = vma->vm_next) 1411 unmap_single_vma(&tlb, vma, start, end, NULL); 1412 mmu_notifier_invalidate_range_end(mm, start, end); 1413 tlb_finish_mmu(&tlb, start, end); 1414 } 1415 1416 /** 1417 * zap_page_range_single - remove user pages in a given range 1418 * @vma: vm_area_struct holding the applicable pages 1419 * @address: starting address of pages to zap 1420 * @size: number of bytes to zap 1421 * @details: details of shared cache invalidation 1422 * 1423 * The range must fit into one VMA. 1424 */ 1425 static void zap_page_range_single(struct vm_area_struct *vma, unsigned long address, 1426 unsigned long size, struct zap_details *details) 1427 { 1428 struct mm_struct *mm = vma->vm_mm; 1429 struct mmu_gather tlb; 1430 unsigned long end = address + size; 1431 1432 lru_add_drain(); 1433 tlb_gather_mmu(&tlb, mm, address, end); 1434 update_hiwater_rss(mm); 1435 mmu_notifier_invalidate_range_start(mm, address, end); 1436 unmap_single_vma(&tlb, vma, address, end, details); 1437 mmu_notifier_invalidate_range_end(mm, address, end); 1438 tlb_finish_mmu(&tlb, address, end); 1439 } 1440 1441 /** 1442 * zap_vma_ptes - remove ptes mapping the vma 1443 * @vma: vm_area_struct holding ptes to be zapped 1444 * @address: starting address of pages to zap 1445 * @size: number of bytes to zap 1446 * 1447 * This function only unmaps ptes assigned to VM_PFNMAP vmas. 1448 * 1449 * The entire address range must be fully contained within the vma. 1450 * 1451 * Returns 0 if successful. 1452 */ 1453 int zap_vma_ptes(struct vm_area_struct *vma, unsigned long address, 1454 unsigned long size) 1455 { 1456 if (address < vma->vm_start || address + size > vma->vm_end || 1457 !(vma->vm_flags & VM_PFNMAP)) 1458 return -1; 1459 zap_page_range_single(vma, address, size, NULL); 1460 return 0; 1461 } 1462 EXPORT_SYMBOL_GPL(zap_vma_ptes); 1463 1464 pte_t *__get_locked_pte(struct mm_struct *mm, unsigned long addr, 1465 spinlock_t **ptl) 1466 { 1467 pgd_t *pgd = pgd_offset(mm, addr); 1468 pud_t *pud = pud_alloc(mm, pgd, addr); 1469 if (pud) { 1470 pmd_t *pmd = pmd_alloc(mm, pud, addr); 1471 if (pmd) { 1472 VM_BUG_ON(pmd_trans_huge(*pmd)); 1473 return pte_alloc_map_lock(mm, pmd, addr, ptl); 1474 } 1475 } 1476 return NULL; 1477 } 1478 1479 /* 1480 * This is the old fallback for page remapping. 1481 * 1482 * For historical reasons, it only allows reserved pages. Only 1483 * old drivers should use this, and they needed to mark their 1484 * pages reserved for the old functions anyway. 1485 */ 1486 static int insert_page(struct vm_area_struct *vma, unsigned long addr, 1487 struct page *page, pgprot_t prot) 1488 { 1489 struct mm_struct *mm = vma->vm_mm; 1490 int retval; 1491 pte_t *pte; 1492 spinlock_t *ptl; 1493 1494 retval = -EINVAL; 1495 if (PageAnon(page)) 1496 goto out; 1497 retval = -ENOMEM; 1498 flush_dcache_page(page); 1499 pte = get_locked_pte(mm, addr, &ptl); 1500 if (!pte) 1501 goto out; 1502 retval = -EBUSY; 1503 if (!pte_none(*pte)) 1504 goto out_unlock; 1505 1506 /* Ok, finally just insert the thing.. */ 1507 get_page(page); 1508 inc_mm_counter_fast(mm, mm_counter_file(page)); 1509 page_add_file_rmap(page, false); 1510 set_pte_at(mm, addr, pte, mk_pte(page, prot)); 1511 1512 retval = 0; 1513 pte_unmap_unlock(pte, ptl); 1514 return retval; 1515 out_unlock: 1516 pte_unmap_unlock(pte, ptl); 1517 out: 1518 return retval; 1519 } 1520 1521 /** 1522 * vm_insert_page - insert single page into user vma 1523 * @vma: user vma to map to 1524 * @addr: target user address of this page 1525 * @page: source kernel page 1526 * 1527 * This allows drivers to insert individual pages they've allocated 1528 * into a user vma. 1529 * 1530 * The page has to be a nice clean _individual_ kernel allocation. 1531 * If you allocate a compound page, you need to have marked it as 1532 * such (__GFP_COMP), or manually just split the page up yourself 1533 * (see split_page()). 1534 * 1535 * NOTE! Traditionally this was done with "remap_pfn_range()" which 1536 * took an arbitrary page protection parameter. This doesn't allow 1537 * that. Your vma protection will have to be set up correctly, which 1538 * means that if you want a shared writable mapping, you'd better 1539 * ask for a shared writable mapping! 1540 * 1541 * The page does not need to be reserved. 1542 * 1543 * Usually this function is called from f_op->mmap() handler 1544 * under mm->mmap_sem write-lock, so it can change vma->vm_flags. 1545 * Caller must set VM_MIXEDMAP on vma if it wants to call this 1546 * function from other places, for example from page-fault handler. 1547 */ 1548 int vm_insert_page(struct vm_area_struct *vma, unsigned long addr, 1549 struct page *page) 1550 { 1551 if (addr < vma->vm_start || addr >= vma->vm_end) 1552 return -EFAULT; 1553 if (!page_count(page)) 1554 return -EINVAL; 1555 if (!(vma->vm_flags & VM_MIXEDMAP)) { 1556 BUG_ON(down_read_trylock(&vma->vm_mm->mmap_sem)); 1557 BUG_ON(vma->vm_flags & VM_PFNMAP); 1558 vma->vm_flags |= VM_MIXEDMAP; 1559 } 1560 return insert_page(vma, addr, page, vma->vm_page_prot); 1561 } 1562 EXPORT_SYMBOL(vm_insert_page); 1563 1564 static int insert_pfn(struct vm_area_struct *vma, unsigned long addr, 1565 pfn_t pfn, pgprot_t prot) 1566 { 1567 struct mm_struct *mm = vma->vm_mm; 1568 int retval; 1569 pte_t *pte, entry; 1570 spinlock_t *ptl; 1571 1572 retval = -ENOMEM; 1573 pte = get_locked_pte(mm, addr, &ptl); 1574 if (!pte) 1575 goto out; 1576 retval = -EBUSY; 1577 if (!pte_none(*pte)) 1578 goto out_unlock; 1579 1580 /* Ok, finally just insert the thing.. */ 1581 if (pfn_t_devmap(pfn)) 1582 entry = pte_mkdevmap(pfn_t_pte(pfn, prot)); 1583 else 1584 entry = pte_mkspecial(pfn_t_pte(pfn, prot)); 1585 set_pte_at(mm, addr, pte, entry); 1586 update_mmu_cache(vma, addr, pte); /* XXX: why not for insert_page? */ 1587 1588 retval = 0; 1589 out_unlock: 1590 pte_unmap_unlock(pte, ptl); 1591 out: 1592 return retval; 1593 } 1594 1595 /** 1596 * vm_insert_pfn - insert single pfn into user vma 1597 * @vma: user vma to map to 1598 * @addr: target user address of this page 1599 * @pfn: source kernel pfn 1600 * 1601 * Similar to vm_insert_page, this allows drivers to insert individual pages 1602 * they've allocated into a user vma. Same comments apply. 1603 * 1604 * This function should only be called from a vm_ops->fault handler, and 1605 * in that case the handler should return NULL. 1606 * 1607 * vma cannot be a COW mapping. 1608 * 1609 * As this is called only for pages that do not currently exist, we 1610 * do not need to flush old virtual caches or the TLB. 1611 */ 1612 int vm_insert_pfn(struct vm_area_struct *vma, unsigned long addr, 1613 unsigned long pfn) 1614 { 1615 return vm_insert_pfn_prot(vma, addr, pfn, vma->vm_page_prot); 1616 } 1617 EXPORT_SYMBOL(vm_insert_pfn); 1618 1619 /** 1620 * vm_insert_pfn_prot - insert single pfn into user vma with specified pgprot 1621 * @vma: user vma to map to 1622 * @addr: target user address of this page 1623 * @pfn: source kernel pfn 1624 * @pgprot: pgprot flags for the inserted page 1625 * 1626 * This is exactly like vm_insert_pfn, except that it allows drivers to 1627 * to override pgprot on a per-page basis. 1628 * 1629 * This only makes sense for IO mappings, and it makes no sense for 1630 * cow mappings. In general, using multiple vmas is preferable; 1631 * vm_insert_pfn_prot should only be used if using multiple VMAs is 1632 * impractical. 1633 */ 1634 int vm_insert_pfn_prot(struct vm_area_struct *vma, unsigned long addr, 1635 unsigned long pfn, pgprot_t pgprot) 1636 { 1637 int ret; 1638 /* 1639 * Technically, architectures with pte_special can avoid all these 1640 * restrictions (same for remap_pfn_range). However we would like 1641 * consistency in testing and feature parity among all, so we should 1642 * try to keep these invariants in place for everybody. 1643 */ 1644 BUG_ON(!(vma->vm_flags & (VM_PFNMAP|VM_MIXEDMAP))); 1645 BUG_ON((vma->vm_flags & (VM_PFNMAP|VM_MIXEDMAP)) == 1646 (VM_PFNMAP|VM_MIXEDMAP)); 1647 BUG_ON((vma->vm_flags & VM_PFNMAP) && is_cow_mapping(vma->vm_flags)); 1648 BUG_ON((vma->vm_flags & VM_MIXEDMAP) && pfn_valid(pfn)); 1649 1650 if (addr < vma->vm_start || addr >= vma->vm_end) 1651 return -EFAULT; 1652 1653 track_pfn_insert(vma, &pgprot, __pfn_to_pfn_t(pfn, PFN_DEV)); 1654 1655 ret = insert_pfn(vma, addr, __pfn_to_pfn_t(pfn, PFN_DEV), pgprot); 1656 1657 return ret; 1658 } 1659 EXPORT_SYMBOL(vm_insert_pfn_prot); 1660 1661 int vm_insert_mixed(struct vm_area_struct *vma, unsigned long addr, 1662 pfn_t pfn) 1663 { 1664 pgprot_t pgprot = vma->vm_page_prot; 1665 1666 BUG_ON(!(vma->vm_flags & VM_MIXEDMAP)); 1667 1668 if (addr < vma->vm_start || addr >= vma->vm_end) 1669 return -EFAULT; 1670 1671 track_pfn_insert(vma, &pgprot, pfn); 1672 1673 /* 1674 * If we don't have pte special, then we have to use the pfn_valid() 1675 * based VM_MIXEDMAP scheme (see vm_normal_page), and thus we *must* 1676 * refcount the page if pfn_valid is true (hence insert_page rather 1677 * than insert_pfn). If a zero_pfn were inserted into a VM_MIXEDMAP 1678 * without pte special, it would there be refcounted as a normal page. 1679 */ 1680 if (!HAVE_PTE_SPECIAL && !pfn_t_devmap(pfn) && pfn_t_valid(pfn)) { 1681 struct page *page; 1682 1683 /* 1684 * At this point we are committed to insert_page() 1685 * regardless of whether the caller specified flags that 1686 * result in pfn_t_has_page() == false. 1687 */ 1688 page = pfn_to_page(pfn_t_to_pfn(pfn)); 1689 return insert_page(vma, addr, page, pgprot); 1690 } 1691 return insert_pfn(vma, addr, pfn, pgprot); 1692 } 1693 EXPORT_SYMBOL(vm_insert_mixed); 1694 1695 /* 1696 * maps a range of physical memory into the requested pages. the old 1697 * mappings are removed. any references to nonexistent pages results 1698 * in null mappings (currently treated as "copy-on-access") 1699 */ 1700 static int remap_pte_range(struct mm_struct *mm, pmd_t *pmd, 1701 unsigned long addr, unsigned long end, 1702 unsigned long pfn, pgprot_t prot) 1703 { 1704 pte_t *pte; 1705 spinlock_t *ptl; 1706 1707 pte = pte_alloc_map_lock(mm, pmd, addr, &ptl); 1708 if (!pte) 1709 return -ENOMEM; 1710 arch_enter_lazy_mmu_mode(); 1711 do { 1712 BUG_ON(!pte_none(*pte)); 1713 set_pte_at(mm, addr, pte, pte_mkspecial(pfn_pte(pfn, prot))); 1714 pfn++; 1715 } while (pte++, addr += PAGE_SIZE, addr != end); 1716 arch_leave_lazy_mmu_mode(); 1717 pte_unmap_unlock(pte - 1, ptl); 1718 return 0; 1719 } 1720 1721 static inline int remap_pmd_range(struct mm_struct *mm, pud_t *pud, 1722 unsigned long addr, unsigned long end, 1723 unsigned long pfn, pgprot_t prot) 1724 { 1725 pmd_t *pmd; 1726 unsigned long next; 1727 1728 pfn -= addr >> PAGE_SHIFT; 1729 pmd = pmd_alloc(mm, pud, addr); 1730 if (!pmd) 1731 return -ENOMEM; 1732 VM_BUG_ON(pmd_trans_huge(*pmd)); 1733 do { 1734 next = pmd_addr_end(addr, end); 1735 if (remap_pte_range(mm, pmd, addr, next, 1736 pfn + (addr >> PAGE_SHIFT), prot)) 1737 return -ENOMEM; 1738 } while (pmd++, addr = next, addr != end); 1739 return 0; 1740 } 1741 1742 static inline int remap_pud_range(struct mm_struct *mm, pgd_t *pgd, 1743 unsigned long addr, unsigned long end, 1744 unsigned long pfn, pgprot_t prot) 1745 { 1746 pud_t *pud; 1747 unsigned long next; 1748 1749 pfn -= addr >> PAGE_SHIFT; 1750 pud = pud_alloc(mm, pgd, addr); 1751 if (!pud) 1752 return -ENOMEM; 1753 do { 1754 next = pud_addr_end(addr, end); 1755 if (remap_pmd_range(mm, pud, addr, next, 1756 pfn + (addr >> PAGE_SHIFT), prot)) 1757 return -ENOMEM; 1758 } while (pud++, addr = next, addr != end); 1759 return 0; 1760 } 1761 1762 /** 1763 * remap_pfn_range - remap kernel memory to userspace 1764 * @vma: user vma to map to 1765 * @addr: target user address to start at 1766 * @pfn: physical address of kernel memory 1767 * @size: size of map area 1768 * @prot: page protection flags for this mapping 1769 * 1770 * Note: this is only safe if the mm semaphore is held when called. 1771 */ 1772 int remap_pfn_range(struct vm_area_struct *vma, unsigned long addr, 1773 unsigned long pfn, unsigned long size, pgprot_t prot) 1774 { 1775 pgd_t *pgd; 1776 unsigned long next; 1777 unsigned long end = addr + PAGE_ALIGN(size); 1778 struct mm_struct *mm = vma->vm_mm; 1779 unsigned long remap_pfn = pfn; 1780 int err; 1781 1782 /* 1783 * Physically remapped pages are special. Tell the 1784 * rest of the world about it: 1785 * VM_IO tells people not to look at these pages 1786 * (accesses can have side effects). 1787 * VM_PFNMAP tells the core MM that the base pages are just 1788 * raw PFN mappings, and do not have a "struct page" associated 1789 * with them. 1790 * VM_DONTEXPAND 1791 * Disable vma merging and expanding with mremap(). 1792 * VM_DONTDUMP 1793 * Omit vma from core dump, even when VM_IO turned off. 1794 * 1795 * There's a horrible special case to handle copy-on-write 1796 * behaviour that some programs depend on. We mark the "original" 1797 * un-COW'ed pages by matching them up with "vma->vm_pgoff". 1798 * See vm_normal_page() for details. 1799 */ 1800 if (is_cow_mapping(vma->vm_flags)) { 1801 if (addr != vma->vm_start || end != vma->vm_end) 1802 return -EINVAL; 1803 vma->vm_pgoff = pfn; 1804 } 1805 1806 err = track_pfn_remap(vma, &prot, remap_pfn, addr, PAGE_ALIGN(size)); 1807 if (err) 1808 return -EINVAL; 1809 1810 vma->vm_flags |= VM_IO | VM_PFNMAP | VM_DONTEXPAND | VM_DONTDUMP; 1811 1812 BUG_ON(addr >= end); 1813 pfn -= addr >> PAGE_SHIFT; 1814 pgd = pgd_offset(mm, addr); 1815 flush_cache_range(vma, addr, end); 1816 do { 1817 next = pgd_addr_end(addr, end); 1818 err = remap_pud_range(mm, pgd, addr, next, 1819 pfn + (addr >> PAGE_SHIFT), prot); 1820 if (err) 1821 break; 1822 } while (pgd++, addr = next, addr != end); 1823 1824 if (err) 1825 untrack_pfn(vma, remap_pfn, PAGE_ALIGN(size)); 1826 1827 return err; 1828 } 1829 EXPORT_SYMBOL(remap_pfn_range); 1830 1831 /** 1832 * vm_iomap_memory - remap memory to userspace 1833 * @vma: user vma to map to 1834 * @start: start of area 1835 * @len: size of area 1836 * 1837 * This is a simplified io_remap_pfn_range() for common driver use. The 1838 * driver just needs to give us the physical memory range to be mapped, 1839 * we'll figure out the rest from the vma information. 1840 * 1841 * NOTE! Some drivers might want to tweak vma->vm_page_prot first to get 1842 * whatever write-combining details or similar. 1843 */ 1844 int vm_iomap_memory(struct vm_area_struct *vma, phys_addr_t start, unsigned long len) 1845 { 1846 unsigned long vm_len, pfn, pages; 1847 1848 /* Check that the physical memory area passed in looks valid */ 1849 if (start + len < start) 1850 return -EINVAL; 1851 /* 1852 * You *really* shouldn't map things that aren't page-aligned, 1853 * but we've historically allowed it because IO memory might 1854 * just have smaller alignment. 1855 */ 1856 len += start & ~PAGE_MASK; 1857 pfn = start >> PAGE_SHIFT; 1858 pages = (len + ~PAGE_MASK) >> PAGE_SHIFT; 1859 if (pfn + pages < pfn) 1860 return -EINVAL; 1861 1862 /* We start the mapping 'vm_pgoff' pages into the area */ 1863 if (vma->vm_pgoff > pages) 1864 return -EINVAL; 1865 pfn += vma->vm_pgoff; 1866 pages -= vma->vm_pgoff; 1867 1868 /* Can we fit all of the mapping? */ 1869 vm_len = vma->vm_end - vma->vm_start; 1870 if (vm_len >> PAGE_SHIFT > pages) 1871 return -EINVAL; 1872 1873 /* Ok, let it rip */ 1874 return io_remap_pfn_range(vma, vma->vm_start, pfn, vm_len, vma->vm_page_prot); 1875 } 1876 EXPORT_SYMBOL(vm_iomap_memory); 1877 1878 static int apply_to_pte_range(struct mm_struct *mm, pmd_t *pmd, 1879 unsigned long addr, unsigned long end, 1880 pte_fn_t fn, void *data) 1881 { 1882 pte_t *pte; 1883 int err; 1884 pgtable_t token; 1885 spinlock_t *uninitialized_var(ptl); 1886 1887 pte = (mm == &init_mm) ? 1888 pte_alloc_kernel(pmd, addr) : 1889 pte_alloc_map_lock(mm, pmd, addr, &ptl); 1890 if (!pte) 1891 return -ENOMEM; 1892 1893 BUG_ON(pmd_huge(*pmd)); 1894 1895 arch_enter_lazy_mmu_mode(); 1896 1897 token = pmd_pgtable(*pmd); 1898 1899 do { 1900 err = fn(pte++, token, addr, data); 1901 if (err) 1902 break; 1903 } while (addr += PAGE_SIZE, addr != end); 1904 1905 arch_leave_lazy_mmu_mode(); 1906 1907 if (mm != &init_mm) 1908 pte_unmap_unlock(pte-1, ptl); 1909 return err; 1910 } 1911 1912 static int apply_to_pmd_range(struct mm_struct *mm, pud_t *pud, 1913 unsigned long addr, unsigned long end, 1914 pte_fn_t fn, void *data) 1915 { 1916 pmd_t *pmd; 1917 unsigned long next; 1918 int err; 1919 1920 BUG_ON(pud_huge(*pud)); 1921 1922 pmd = pmd_alloc(mm, pud, addr); 1923 if (!pmd) 1924 return -ENOMEM; 1925 do { 1926 next = pmd_addr_end(addr, end); 1927 err = apply_to_pte_range(mm, pmd, addr, next, fn, data); 1928 if (err) 1929 break; 1930 } while (pmd++, addr = next, addr != end); 1931 return err; 1932 } 1933 1934 static int apply_to_pud_range(struct mm_struct *mm, pgd_t *pgd, 1935 unsigned long addr, unsigned long end, 1936 pte_fn_t fn, void *data) 1937 { 1938 pud_t *pud; 1939 unsigned long next; 1940 int err; 1941 1942 pud = pud_alloc(mm, pgd, addr); 1943 if (!pud) 1944 return -ENOMEM; 1945 do { 1946 next = pud_addr_end(addr, end); 1947 err = apply_to_pmd_range(mm, pud, addr, next, fn, data); 1948 if (err) 1949 break; 1950 } while (pud++, addr = next, addr != end); 1951 return err; 1952 } 1953 1954 /* 1955 * Scan a region of virtual memory, filling in page tables as necessary 1956 * and calling a provided function on each leaf page table. 1957 */ 1958 int apply_to_page_range(struct mm_struct *mm, unsigned long addr, 1959 unsigned long size, pte_fn_t fn, void *data) 1960 { 1961 pgd_t *pgd; 1962 unsigned long next; 1963 unsigned long end = addr + size; 1964 int err; 1965 1966 if (WARN_ON(addr >= end)) 1967 return -EINVAL; 1968 1969 pgd = pgd_offset(mm, addr); 1970 do { 1971 next = pgd_addr_end(addr, end); 1972 err = apply_to_pud_range(mm, pgd, addr, next, fn, data); 1973 if (err) 1974 break; 1975 } while (pgd++, addr = next, addr != end); 1976 1977 return err; 1978 } 1979 EXPORT_SYMBOL_GPL(apply_to_page_range); 1980 1981 /* 1982 * handle_pte_fault chooses page fault handler according to an entry which was 1983 * read non-atomically. Before making any commitment, on those architectures 1984 * or configurations (e.g. i386 with PAE) which might give a mix of unmatched 1985 * parts, do_swap_page must check under lock before unmapping the pte and 1986 * proceeding (but do_wp_page is only called after already making such a check; 1987 * and do_anonymous_page can safely check later on). 1988 */ 1989 static inline int pte_unmap_same(struct mm_struct *mm, pmd_t *pmd, 1990 pte_t *page_table, pte_t orig_pte) 1991 { 1992 int same = 1; 1993 #if defined(CONFIG_SMP) || defined(CONFIG_PREEMPT) 1994 if (sizeof(pte_t) > sizeof(unsigned long)) { 1995 spinlock_t *ptl = pte_lockptr(mm, pmd); 1996 spin_lock(ptl); 1997 same = pte_same(*page_table, orig_pte); 1998 spin_unlock(ptl); 1999 } 2000 #endif 2001 pte_unmap(page_table); 2002 return same; 2003 } 2004 2005 static inline void cow_user_page(struct page *dst, struct page *src, unsigned long va, struct vm_area_struct *vma) 2006 { 2007 debug_dma_assert_idle(src); 2008 2009 /* 2010 * If the source page was a PFN mapping, we don't have 2011 * a "struct page" for it. We do a best-effort copy by 2012 * just copying from the original user address. If that 2013 * fails, we just zero-fill it. Live with it. 2014 */ 2015 if (unlikely(!src)) { 2016 void *kaddr = kmap_atomic(dst); 2017 void __user *uaddr = (void __user *)(va & PAGE_MASK); 2018 2019 /* 2020 * This really shouldn't fail, because the page is there 2021 * in the page tables. But it might just be unreadable, 2022 * in which case we just give up and fill the result with 2023 * zeroes. 2024 */ 2025 if (__copy_from_user_inatomic(kaddr, uaddr, PAGE_SIZE)) 2026 clear_page(kaddr); 2027 kunmap_atomic(kaddr); 2028 flush_dcache_page(dst); 2029 } else 2030 copy_user_highpage(dst, src, va, vma); 2031 } 2032 2033 static gfp_t __get_fault_gfp_mask(struct vm_area_struct *vma) 2034 { 2035 struct file *vm_file = vma->vm_file; 2036 2037 if (vm_file) 2038 return mapping_gfp_mask(vm_file->f_mapping) | __GFP_FS | __GFP_IO; 2039 2040 /* 2041 * Special mappings (e.g. VDSO) do not have any file so fake 2042 * a default GFP_KERNEL for them. 2043 */ 2044 return GFP_KERNEL; 2045 } 2046 2047 /* 2048 * Notify the address space that the page is about to become writable so that 2049 * it can prohibit this or wait for the page to get into an appropriate state. 2050 * 2051 * We do this without the lock held, so that it can sleep if it needs to. 2052 */ 2053 static int do_page_mkwrite(struct vm_fault *vmf) 2054 { 2055 int ret; 2056 struct page *page = vmf->page; 2057 unsigned int old_flags = vmf->flags; 2058 2059 vmf->flags = FAULT_FLAG_WRITE|FAULT_FLAG_MKWRITE; 2060 2061 ret = vmf->vma->vm_ops->page_mkwrite(vmf); 2062 /* Restore original flags so that caller is not surprised */ 2063 vmf->flags = old_flags; 2064 if (unlikely(ret & (VM_FAULT_ERROR | VM_FAULT_NOPAGE))) 2065 return ret; 2066 if (unlikely(!(ret & VM_FAULT_LOCKED))) { 2067 lock_page(page); 2068 if (!page->mapping) { 2069 unlock_page(page); 2070 return 0; /* retry */ 2071 } 2072 ret |= VM_FAULT_LOCKED; 2073 } else 2074 VM_BUG_ON_PAGE(!PageLocked(page), page); 2075 return ret; 2076 } 2077 2078 /* 2079 * Handle dirtying of a page in shared file mapping on a write fault. 2080 * 2081 * The function expects the page to be locked and unlocks it. 2082 */ 2083 static void fault_dirty_shared_page(struct vm_area_struct *vma, 2084 struct page *page) 2085 { 2086 struct address_space *mapping; 2087 bool dirtied; 2088 bool page_mkwrite = vma->vm_ops && vma->vm_ops->page_mkwrite; 2089 2090 dirtied = set_page_dirty(page); 2091 VM_BUG_ON_PAGE(PageAnon(page), page); 2092 /* 2093 * Take a local copy of the address_space - page.mapping may be zeroed 2094 * by truncate after unlock_page(). The address_space itself remains 2095 * pinned by vma->vm_file's reference. We rely on unlock_page()'s 2096 * release semantics to prevent the compiler from undoing this copying. 2097 */ 2098 mapping = page_rmapping(page); 2099 unlock_page(page); 2100 2101 if ((dirtied || page_mkwrite) && mapping) { 2102 /* 2103 * Some device drivers do not set page.mapping 2104 * but still dirty their pages 2105 */ 2106 balance_dirty_pages_ratelimited(mapping); 2107 } 2108 2109 if (!page_mkwrite) 2110 file_update_time(vma->vm_file); 2111 } 2112 2113 /* 2114 * Handle write page faults for pages that can be reused in the current vma 2115 * 2116 * This can happen either due to the mapping being with the VM_SHARED flag, 2117 * or due to us being the last reference standing to the page. In either 2118 * case, all we need to do here is to mark the page as writable and update 2119 * any related book-keeping. 2120 */ 2121 static inline void wp_page_reuse(struct vm_fault *vmf) 2122 __releases(vmf->ptl) 2123 { 2124 struct vm_area_struct *vma = vmf->vma; 2125 struct page *page = vmf->page; 2126 pte_t entry; 2127 /* 2128 * Clear the pages cpupid information as the existing 2129 * information potentially belongs to a now completely 2130 * unrelated process. 2131 */ 2132 if (page) 2133 page_cpupid_xchg_last(page, (1 << LAST_CPUPID_SHIFT) - 1); 2134 2135 flush_cache_page(vma, vmf->address, pte_pfn(vmf->orig_pte)); 2136 entry = pte_mkyoung(vmf->orig_pte); 2137 entry = maybe_mkwrite(pte_mkdirty(entry), vma); 2138 if (ptep_set_access_flags(vma, vmf->address, vmf->pte, entry, 1)) 2139 update_mmu_cache(vma, vmf->address, vmf->pte); 2140 pte_unmap_unlock(vmf->pte, vmf->ptl); 2141 } 2142 2143 /* 2144 * Handle the case of a page which we actually need to copy to a new page. 2145 * 2146 * Called with mmap_sem locked and the old page referenced, but 2147 * without the ptl held. 2148 * 2149 * High level logic flow: 2150 * 2151 * - Allocate a page, copy the content of the old page to the new one. 2152 * - Handle book keeping and accounting - cgroups, mmu-notifiers, etc. 2153 * - Take the PTL. If the pte changed, bail out and release the allocated page 2154 * - If the pte is still the way we remember it, update the page table and all 2155 * relevant references. This includes dropping the reference the page-table 2156 * held to the old page, as well as updating the rmap. 2157 * - In any case, unlock the PTL and drop the reference we took to the old page. 2158 */ 2159 static int wp_page_copy(struct vm_fault *vmf) 2160 { 2161 struct vm_area_struct *vma = vmf->vma; 2162 struct mm_struct *mm = vma->vm_mm; 2163 struct page *old_page = vmf->page; 2164 struct page *new_page = NULL; 2165 pte_t entry; 2166 int page_copied = 0; 2167 const unsigned long mmun_start = vmf->address & PAGE_MASK; 2168 const unsigned long mmun_end = mmun_start + PAGE_SIZE; 2169 struct mem_cgroup *memcg; 2170 2171 if (unlikely(anon_vma_prepare(vma))) 2172 goto oom; 2173 2174 if (is_zero_pfn(pte_pfn(vmf->orig_pte))) { 2175 new_page = alloc_zeroed_user_highpage_movable(vma, 2176 vmf->address); 2177 if (!new_page) 2178 goto oom; 2179 } else { 2180 new_page = alloc_page_vma(GFP_HIGHUSER_MOVABLE, vma, 2181 vmf->address); 2182 if (!new_page) 2183 goto oom; 2184 cow_user_page(new_page, old_page, vmf->address, vma); 2185 } 2186 2187 if (mem_cgroup_try_charge(new_page, mm, GFP_KERNEL, &memcg, false)) 2188 goto oom_free_new; 2189 2190 __SetPageUptodate(new_page); 2191 2192 mmu_notifier_invalidate_range_start(mm, mmun_start, mmun_end); 2193 2194 /* 2195 * Re-check the pte - we dropped the lock 2196 */ 2197 vmf->pte = pte_offset_map_lock(mm, vmf->pmd, vmf->address, &vmf->ptl); 2198 if (likely(pte_same(*vmf->pte, vmf->orig_pte))) { 2199 if (old_page) { 2200 if (!PageAnon(old_page)) { 2201 dec_mm_counter_fast(mm, 2202 mm_counter_file(old_page)); 2203 inc_mm_counter_fast(mm, MM_ANONPAGES); 2204 } 2205 } else { 2206 inc_mm_counter_fast(mm, MM_ANONPAGES); 2207 } 2208 flush_cache_page(vma, vmf->address, pte_pfn(vmf->orig_pte)); 2209 entry = mk_pte(new_page, vma->vm_page_prot); 2210 entry = maybe_mkwrite(pte_mkdirty(entry), vma); 2211 /* 2212 * Clear the pte entry and flush it first, before updating the 2213 * pte with the new entry. This will avoid a race condition 2214 * seen in the presence of one thread doing SMC and another 2215 * thread doing COW. 2216 */ 2217 ptep_clear_flush_notify(vma, vmf->address, vmf->pte); 2218 page_add_new_anon_rmap(new_page, vma, vmf->address, false); 2219 mem_cgroup_commit_charge(new_page, memcg, false, false); 2220 lru_cache_add_active_or_unevictable(new_page, vma); 2221 /* 2222 * We call the notify macro here because, when using secondary 2223 * mmu page tables (such as kvm shadow page tables), we want the 2224 * new page to be mapped directly into the secondary page table. 2225 */ 2226 set_pte_at_notify(mm, vmf->address, vmf->pte, entry); 2227 update_mmu_cache(vma, vmf->address, vmf->pte); 2228 if (old_page) { 2229 /* 2230 * Only after switching the pte to the new page may 2231 * we remove the mapcount here. Otherwise another 2232 * process may come and find the rmap count decremented 2233 * before the pte is switched to the new page, and 2234 * "reuse" the old page writing into it while our pte 2235 * here still points into it and can be read by other 2236 * threads. 2237 * 2238 * The critical issue is to order this 2239 * page_remove_rmap with the ptp_clear_flush above. 2240 * Those stores are ordered by (if nothing else,) 2241 * the barrier present in the atomic_add_negative 2242 * in page_remove_rmap. 2243 * 2244 * Then the TLB flush in ptep_clear_flush ensures that 2245 * no process can access the old page before the 2246 * decremented mapcount is visible. And the old page 2247 * cannot be reused until after the decremented 2248 * mapcount is visible. So transitively, TLBs to 2249 * old page will be flushed before it can be reused. 2250 */ 2251 page_remove_rmap(old_page, false); 2252 } 2253 2254 /* Free the old page.. */ 2255 new_page = old_page; 2256 page_copied = 1; 2257 } else { 2258 mem_cgroup_cancel_charge(new_page, memcg, false); 2259 } 2260 2261 if (new_page) 2262 put_page(new_page); 2263 2264 pte_unmap_unlock(vmf->pte, vmf->ptl); 2265 mmu_notifier_invalidate_range_end(mm, mmun_start, mmun_end); 2266 if (old_page) { 2267 /* 2268 * Don't let another task, with possibly unlocked vma, 2269 * keep the mlocked page. 2270 */ 2271 if (page_copied && (vma->vm_flags & VM_LOCKED)) { 2272 lock_page(old_page); /* LRU manipulation */ 2273 if (PageMlocked(old_page)) 2274 munlock_vma_page(old_page); 2275 unlock_page(old_page); 2276 } 2277 put_page(old_page); 2278 } 2279 return page_copied ? VM_FAULT_WRITE : 0; 2280 oom_free_new: 2281 put_page(new_page); 2282 oom: 2283 if (old_page) 2284 put_page(old_page); 2285 return VM_FAULT_OOM; 2286 } 2287 2288 /** 2289 * finish_mkwrite_fault - finish page fault for a shared mapping, making PTE 2290 * writeable once the page is prepared 2291 * 2292 * @vmf: structure describing the fault 2293 * 2294 * This function handles all that is needed to finish a write page fault in a 2295 * shared mapping due to PTE being read-only once the mapped page is prepared. 2296 * It handles locking of PTE and modifying it. The function returns 2297 * VM_FAULT_WRITE on success, 0 when PTE got changed before we acquired PTE 2298 * lock. 2299 * 2300 * The function expects the page to be locked or other protection against 2301 * concurrent faults / writeback (such as DAX radix tree locks). 2302 */ 2303 int finish_mkwrite_fault(struct vm_fault *vmf) 2304 { 2305 WARN_ON_ONCE(!(vmf->vma->vm_flags & VM_SHARED)); 2306 vmf->pte = pte_offset_map_lock(vmf->vma->vm_mm, vmf->pmd, vmf->address, 2307 &vmf->ptl); 2308 /* 2309 * We might have raced with another page fault while we released the 2310 * pte_offset_map_lock. 2311 */ 2312 if (!pte_same(*vmf->pte, vmf->orig_pte)) { 2313 pte_unmap_unlock(vmf->pte, vmf->ptl); 2314 return VM_FAULT_NOPAGE; 2315 } 2316 wp_page_reuse(vmf); 2317 return 0; 2318 } 2319 2320 /* 2321 * Handle write page faults for VM_MIXEDMAP or VM_PFNMAP for a VM_SHARED 2322 * mapping 2323 */ 2324 static int wp_pfn_shared(struct vm_fault *vmf) 2325 { 2326 struct vm_area_struct *vma = vmf->vma; 2327 2328 if (vma->vm_ops && vma->vm_ops->pfn_mkwrite) { 2329 int ret; 2330 2331 pte_unmap_unlock(vmf->pte, vmf->ptl); 2332 vmf->flags |= FAULT_FLAG_MKWRITE; 2333 ret = vma->vm_ops->pfn_mkwrite(vmf); 2334 if (ret & (VM_FAULT_ERROR | VM_FAULT_NOPAGE)) 2335 return ret; 2336 return finish_mkwrite_fault(vmf); 2337 } 2338 wp_page_reuse(vmf); 2339 return VM_FAULT_WRITE; 2340 } 2341 2342 static int wp_page_shared(struct vm_fault *vmf) 2343 __releases(vmf->ptl) 2344 { 2345 struct vm_area_struct *vma = vmf->vma; 2346 2347 get_page(vmf->page); 2348 2349 if (vma->vm_ops && vma->vm_ops->page_mkwrite) { 2350 int tmp; 2351 2352 pte_unmap_unlock(vmf->pte, vmf->ptl); 2353 tmp = do_page_mkwrite(vmf); 2354 if (unlikely(!tmp || (tmp & 2355 (VM_FAULT_ERROR | VM_FAULT_NOPAGE)))) { 2356 put_page(vmf->page); 2357 return tmp; 2358 } 2359 tmp = finish_mkwrite_fault(vmf); 2360 if (unlikely(tmp & (VM_FAULT_ERROR | VM_FAULT_NOPAGE))) { 2361 unlock_page(vmf->page); 2362 put_page(vmf->page); 2363 return tmp; 2364 } 2365 } else { 2366 wp_page_reuse(vmf); 2367 lock_page(vmf->page); 2368 } 2369 fault_dirty_shared_page(vma, vmf->page); 2370 put_page(vmf->page); 2371 2372 return VM_FAULT_WRITE; 2373 } 2374 2375 /* 2376 * This routine handles present pages, when users try to write 2377 * to a shared page. It is done by copying the page to a new address 2378 * and decrementing the shared-page counter for the old page. 2379 * 2380 * Note that this routine assumes that the protection checks have been 2381 * done by the caller (the low-level page fault routine in most cases). 2382 * Thus we can safely just mark it writable once we've done any necessary 2383 * COW. 2384 * 2385 * We also mark the page dirty at this point even though the page will 2386 * change only once the write actually happens. This avoids a few races, 2387 * and potentially makes it more efficient. 2388 * 2389 * We enter with non-exclusive mmap_sem (to exclude vma changes, 2390 * but allow concurrent faults), with pte both mapped and locked. 2391 * We return with mmap_sem still held, but pte unmapped and unlocked. 2392 */ 2393 static int do_wp_page(struct vm_fault *vmf) 2394 __releases(vmf->ptl) 2395 { 2396 struct vm_area_struct *vma = vmf->vma; 2397 2398 vmf->page = vm_normal_page(vma, vmf->address, vmf->orig_pte); 2399 if (!vmf->page) { 2400 /* 2401 * VM_MIXEDMAP !pfn_valid() case, or VM_SOFTDIRTY clear on a 2402 * VM_PFNMAP VMA. 2403 * 2404 * We should not cow pages in a shared writeable mapping. 2405 * Just mark the pages writable and/or call ops->pfn_mkwrite. 2406 */ 2407 if ((vma->vm_flags & (VM_WRITE|VM_SHARED)) == 2408 (VM_WRITE|VM_SHARED)) 2409 return wp_pfn_shared(vmf); 2410 2411 pte_unmap_unlock(vmf->pte, vmf->ptl); 2412 return wp_page_copy(vmf); 2413 } 2414 2415 /* 2416 * Take out anonymous pages first, anonymous shared vmas are 2417 * not dirty accountable. 2418 */ 2419 if (PageAnon(vmf->page) && !PageKsm(vmf->page)) { 2420 int total_mapcount; 2421 if (!trylock_page(vmf->page)) { 2422 get_page(vmf->page); 2423 pte_unmap_unlock(vmf->pte, vmf->ptl); 2424 lock_page(vmf->page); 2425 vmf->pte = pte_offset_map_lock(vma->vm_mm, vmf->pmd, 2426 vmf->address, &vmf->ptl); 2427 if (!pte_same(*vmf->pte, vmf->orig_pte)) { 2428 unlock_page(vmf->page); 2429 pte_unmap_unlock(vmf->pte, vmf->ptl); 2430 put_page(vmf->page); 2431 return 0; 2432 } 2433 put_page(vmf->page); 2434 } 2435 if (reuse_swap_page(vmf->page, &total_mapcount)) { 2436 if (total_mapcount == 1) { 2437 /* 2438 * The page is all ours. Move it to 2439 * our anon_vma so the rmap code will 2440 * not search our parent or siblings. 2441 * Protected against the rmap code by 2442 * the page lock. 2443 */ 2444 page_move_anon_rmap(vmf->page, vma); 2445 } 2446 unlock_page(vmf->page); 2447 wp_page_reuse(vmf); 2448 return VM_FAULT_WRITE; 2449 } 2450 unlock_page(vmf->page); 2451 } else if (unlikely((vma->vm_flags & (VM_WRITE|VM_SHARED)) == 2452 (VM_WRITE|VM_SHARED))) { 2453 return wp_page_shared(vmf); 2454 } 2455 2456 /* 2457 * Ok, we need to copy. Oh, well.. 2458 */ 2459 get_page(vmf->page); 2460 2461 pte_unmap_unlock(vmf->pte, vmf->ptl); 2462 return wp_page_copy(vmf); 2463 } 2464 2465 static void unmap_mapping_range_vma(struct vm_area_struct *vma, 2466 unsigned long start_addr, unsigned long end_addr, 2467 struct zap_details *details) 2468 { 2469 zap_page_range_single(vma, start_addr, end_addr - start_addr, details); 2470 } 2471 2472 static inline void unmap_mapping_range_tree(struct rb_root *root, 2473 struct zap_details *details) 2474 { 2475 struct vm_area_struct *vma; 2476 pgoff_t vba, vea, zba, zea; 2477 2478 vma_interval_tree_foreach(vma, root, 2479 details->first_index, details->last_index) { 2480 2481 vba = vma->vm_pgoff; 2482 vea = vba + vma_pages(vma) - 1; 2483 zba = details->first_index; 2484 if (zba < vba) 2485 zba = vba; 2486 zea = details->last_index; 2487 if (zea > vea) 2488 zea = vea; 2489 2490 unmap_mapping_range_vma(vma, 2491 ((zba - vba) << PAGE_SHIFT) + vma->vm_start, 2492 ((zea - vba + 1) << PAGE_SHIFT) + vma->vm_start, 2493 details); 2494 } 2495 } 2496 2497 /** 2498 * unmap_mapping_range - unmap the portion of all mmaps in the specified 2499 * address_space corresponding to the specified page range in the underlying 2500 * file. 2501 * 2502 * @mapping: the address space containing mmaps to be unmapped. 2503 * @holebegin: byte in first page to unmap, relative to the start of 2504 * the underlying file. This will be rounded down to a PAGE_SIZE 2505 * boundary. Note that this is different from truncate_pagecache(), which 2506 * must keep the partial page. In contrast, we must get rid of 2507 * partial pages. 2508 * @holelen: size of prospective hole in bytes. This will be rounded 2509 * up to a PAGE_SIZE boundary. A holelen of zero truncates to the 2510 * end of the file. 2511 * @even_cows: 1 when truncating a file, unmap even private COWed pages; 2512 * but 0 when invalidating pagecache, don't throw away private data. 2513 */ 2514 void unmap_mapping_range(struct address_space *mapping, 2515 loff_t const holebegin, loff_t const holelen, int even_cows) 2516 { 2517 struct zap_details details = { }; 2518 pgoff_t hba = holebegin >> PAGE_SHIFT; 2519 pgoff_t hlen = (holelen + PAGE_SIZE - 1) >> PAGE_SHIFT; 2520 2521 /* Check for overflow. */ 2522 if (sizeof(holelen) > sizeof(hlen)) { 2523 long long holeend = 2524 (holebegin + holelen + PAGE_SIZE - 1) >> PAGE_SHIFT; 2525 if (holeend & ~(long long)ULONG_MAX) 2526 hlen = ULONG_MAX - hba + 1; 2527 } 2528 2529 details.check_mapping = even_cows ? NULL : mapping; 2530 details.first_index = hba; 2531 details.last_index = hba + hlen - 1; 2532 if (details.last_index < details.first_index) 2533 details.last_index = ULONG_MAX; 2534 2535 i_mmap_lock_write(mapping); 2536 if (unlikely(!RB_EMPTY_ROOT(&mapping->i_mmap))) 2537 unmap_mapping_range_tree(&mapping->i_mmap, &details); 2538 i_mmap_unlock_write(mapping); 2539 } 2540 EXPORT_SYMBOL(unmap_mapping_range); 2541 2542 /* 2543 * We enter with non-exclusive mmap_sem (to exclude vma changes, 2544 * but allow concurrent faults), and pte mapped but not yet locked. 2545 * We return with pte unmapped and unlocked. 2546 * 2547 * We return with the mmap_sem locked or unlocked in the same cases 2548 * as does filemap_fault(). 2549 */ 2550 int do_swap_page(struct vm_fault *vmf) 2551 { 2552 struct vm_area_struct *vma = vmf->vma; 2553 struct page *page, *swapcache; 2554 struct mem_cgroup *memcg; 2555 swp_entry_t entry; 2556 pte_t pte; 2557 int locked; 2558 int exclusive = 0; 2559 int ret = 0; 2560 2561 if (!pte_unmap_same(vma->vm_mm, vmf->pmd, vmf->pte, vmf->orig_pte)) 2562 goto out; 2563 2564 entry = pte_to_swp_entry(vmf->orig_pte); 2565 if (unlikely(non_swap_entry(entry))) { 2566 if (is_migration_entry(entry)) { 2567 migration_entry_wait(vma->vm_mm, vmf->pmd, 2568 vmf->address); 2569 } else if (is_hwpoison_entry(entry)) { 2570 ret = VM_FAULT_HWPOISON; 2571 } else { 2572 print_bad_pte(vma, vmf->address, vmf->orig_pte, NULL); 2573 ret = VM_FAULT_SIGBUS; 2574 } 2575 goto out; 2576 } 2577 delayacct_set_flag(DELAYACCT_PF_SWAPIN); 2578 page = lookup_swap_cache(entry); 2579 if (!page) { 2580 page = swapin_readahead(entry, GFP_HIGHUSER_MOVABLE, vma, 2581 vmf->address); 2582 if (!page) { 2583 /* 2584 * Back out if somebody else faulted in this pte 2585 * while we released the pte lock. 2586 */ 2587 vmf->pte = pte_offset_map_lock(vma->vm_mm, vmf->pmd, 2588 vmf->address, &vmf->ptl); 2589 if (likely(pte_same(*vmf->pte, vmf->orig_pte))) 2590 ret = VM_FAULT_OOM; 2591 delayacct_clear_flag(DELAYACCT_PF_SWAPIN); 2592 goto unlock; 2593 } 2594 2595 /* Had to read the page from swap area: Major fault */ 2596 ret = VM_FAULT_MAJOR; 2597 count_vm_event(PGMAJFAULT); 2598 mem_cgroup_count_vm_event(vma->vm_mm, PGMAJFAULT); 2599 } else if (PageHWPoison(page)) { 2600 /* 2601 * hwpoisoned dirty swapcache pages are kept for killing 2602 * owner processes (which may be unknown at hwpoison time) 2603 */ 2604 ret = VM_FAULT_HWPOISON; 2605 delayacct_clear_flag(DELAYACCT_PF_SWAPIN); 2606 swapcache = page; 2607 goto out_release; 2608 } 2609 2610 swapcache = page; 2611 locked = lock_page_or_retry(page, vma->vm_mm, vmf->flags); 2612 2613 delayacct_clear_flag(DELAYACCT_PF_SWAPIN); 2614 if (!locked) { 2615 ret |= VM_FAULT_RETRY; 2616 goto out_release; 2617 } 2618 2619 /* 2620 * Make sure try_to_free_swap or reuse_swap_page or swapoff did not 2621 * release the swapcache from under us. The page pin, and pte_same 2622 * test below, are not enough to exclude that. Even if it is still 2623 * swapcache, we need to check that the page's swap has not changed. 2624 */ 2625 if (unlikely(!PageSwapCache(page) || page_private(page) != entry.val)) 2626 goto out_page; 2627 2628 page = ksm_might_need_to_copy(page, vma, vmf->address); 2629 if (unlikely(!page)) { 2630 ret = VM_FAULT_OOM; 2631 page = swapcache; 2632 goto out_page; 2633 } 2634 2635 if (mem_cgroup_try_charge(page, vma->vm_mm, GFP_KERNEL, 2636 &memcg, false)) { 2637 ret = VM_FAULT_OOM; 2638 goto out_page; 2639 } 2640 2641 /* 2642 * Back out if somebody else already faulted in this pte. 2643 */ 2644 vmf->pte = pte_offset_map_lock(vma->vm_mm, vmf->pmd, vmf->address, 2645 &vmf->ptl); 2646 if (unlikely(!pte_same(*vmf->pte, vmf->orig_pte))) 2647 goto out_nomap; 2648 2649 if (unlikely(!PageUptodate(page))) { 2650 ret = VM_FAULT_SIGBUS; 2651 goto out_nomap; 2652 } 2653 2654 /* 2655 * The page isn't present yet, go ahead with the fault. 2656 * 2657 * Be careful about the sequence of operations here. 2658 * To get its accounting right, reuse_swap_page() must be called 2659 * while the page is counted on swap but not yet in mapcount i.e. 2660 * before page_add_anon_rmap() and swap_free(); try_to_free_swap() 2661 * must be called after the swap_free(), or it will never succeed. 2662 */ 2663 2664 inc_mm_counter_fast(vma->vm_mm, MM_ANONPAGES); 2665 dec_mm_counter_fast(vma->vm_mm, MM_SWAPENTS); 2666 pte = mk_pte(page, vma->vm_page_prot); 2667 if ((vmf->flags & FAULT_FLAG_WRITE) && reuse_swap_page(page, NULL)) { 2668 pte = maybe_mkwrite(pte_mkdirty(pte), vma); 2669 vmf->flags &= ~FAULT_FLAG_WRITE; 2670 ret |= VM_FAULT_WRITE; 2671 exclusive = RMAP_EXCLUSIVE; 2672 } 2673 flush_icache_page(vma, page); 2674 if (pte_swp_soft_dirty(vmf->orig_pte)) 2675 pte = pte_mksoft_dirty(pte); 2676 set_pte_at(vma->vm_mm, vmf->address, vmf->pte, pte); 2677 vmf->orig_pte = pte; 2678 if (page == swapcache) { 2679 do_page_add_anon_rmap(page, vma, vmf->address, exclusive); 2680 mem_cgroup_commit_charge(page, memcg, true, false); 2681 activate_page(page); 2682 } else { /* ksm created a completely new copy */ 2683 page_add_new_anon_rmap(page, vma, vmf->address, false); 2684 mem_cgroup_commit_charge(page, memcg, false, false); 2685 lru_cache_add_active_or_unevictable(page, vma); 2686 } 2687 2688 swap_free(entry); 2689 if (mem_cgroup_swap_full(page) || 2690 (vma->vm_flags & VM_LOCKED) || PageMlocked(page)) 2691 try_to_free_swap(page); 2692 unlock_page(page); 2693 if (page != swapcache) { 2694 /* 2695 * Hold the lock to avoid the swap entry to be reused 2696 * until we take the PT lock for the pte_same() check 2697 * (to avoid false positives from pte_same). For 2698 * further safety release the lock after the swap_free 2699 * so that the swap count won't change under a 2700 * parallel locked swapcache. 2701 */ 2702 unlock_page(swapcache); 2703 put_page(swapcache); 2704 } 2705 2706 if (vmf->flags & FAULT_FLAG_WRITE) { 2707 ret |= do_wp_page(vmf); 2708 if (ret & VM_FAULT_ERROR) 2709 ret &= VM_FAULT_ERROR; 2710 goto out; 2711 } 2712 2713 /* No need to invalidate - it was non-present before */ 2714 update_mmu_cache(vma, vmf->address, vmf->pte); 2715 unlock: 2716 pte_unmap_unlock(vmf->pte, vmf->ptl); 2717 out: 2718 return ret; 2719 out_nomap: 2720 mem_cgroup_cancel_charge(page, memcg, false); 2721 pte_unmap_unlock(vmf->pte, vmf->ptl); 2722 out_page: 2723 unlock_page(page); 2724 out_release: 2725 put_page(page); 2726 if (page != swapcache) { 2727 unlock_page(swapcache); 2728 put_page(swapcache); 2729 } 2730 return ret; 2731 } 2732 2733 /* 2734 * This is like a special single-page "expand_{down|up}wards()", 2735 * except we must first make sure that 'address{-|+}PAGE_SIZE' 2736 * doesn't hit another vma. 2737 */ 2738 static inline int check_stack_guard_page(struct vm_area_struct *vma, unsigned long address) 2739 { 2740 address &= PAGE_MASK; 2741 if ((vma->vm_flags & VM_GROWSDOWN) && address == vma->vm_start) { 2742 struct vm_area_struct *prev = vma->vm_prev; 2743 2744 /* 2745 * Is there a mapping abutting this one below? 2746 * 2747 * That's only ok if it's the same stack mapping 2748 * that has gotten split.. 2749 */ 2750 if (prev && prev->vm_end == address) 2751 return prev->vm_flags & VM_GROWSDOWN ? 0 : -ENOMEM; 2752 2753 return expand_downwards(vma, address - PAGE_SIZE); 2754 } 2755 if ((vma->vm_flags & VM_GROWSUP) && address + PAGE_SIZE == vma->vm_end) { 2756 struct vm_area_struct *next = vma->vm_next; 2757 2758 /* As VM_GROWSDOWN but s/below/above/ */ 2759 if (next && next->vm_start == address + PAGE_SIZE) 2760 return next->vm_flags & VM_GROWSUP ? 0 : -ENOMEM; 2761 2762 return expand_upwards(vma, address + PAGE_SIZE); 2763 } 2764 return 0; 2765 } 2766 2767 /* 2768 * We enter with non-exclusive mmap_sem (to exclude vma changes, 2769 * but allow concurrent faults), and pte mapped but not yet locked. 2770 * We return with mmap_sem still held, but pte unmapped and unlocked. 2771 */ 2772 static int do_anonymous_page(struct vm_fault *vmf) 2773 { 2774 struct vm_area_struct *vma = vmf->vma; 2775 struct mem_cgroup *memcg; 2776 struct page *page; 2777 pte_t entry; 2778 2779 /* File mapping without ->vm_ops ? */ 2780 if (vma->vm_flags & VM_SHARED) 2781 return VM_FAULT_SIGBUS; 2782 2783 /* Check if we need to add a guard page to the stack */ 2784 if (check_stack_guard_page(vma, vmf->address) < 0) 2785 return VM_FAULT_SIGSEGV; 2786 2787 /* 2788 * Use pte_alloc() instead of pte_alloc_map(). We can't run 2789 * pte_offset_map() on pmds where a huge pmd might be created 2790 * from a different thread. 2791 * 2792 * pte_alloc_map() is safe to use under down_write(mmap_sem) or when 2793 * parallel threads are excluded by other means. 2794 * 2795 * Here we only have down_read(mmap_sem). 2796 */ 2797 if (pte_alloc(vma->vm_mm, vmf->pmd, vmf->address)) 2798 return VM_FAULT_OOM; 2799 2800 /* See the comment in pte_alloc_one_map() */ 2801 if (unlikely(pmd_trans_unstable(vmf->pmd))) 2802 return 0; 2803 2804 /* Use the zero-page for reads */ 2805 if (!(vmf->flags & FAULT_FLAG_WRITE) && 2806 !mm_forbids_zeropage(vma->vm_mm)) { 2807 entry = pte_mkspecial(pfn_pte(my_zero_pfn(vmf->address), 2808 vma->vm_page_prot)); 2809 vmf->pte = pte_offset_map_lock(vma->vm_mm, vmf->pmd, 2810 vmf->address, &vmf->ptl); 2811 if (!pte_none(*vmf->pte)) 2812 goto unlock; 2813 /* Deliver the page fault to userland, check inside PT lock */ 2814 if (userfaultfd_missing(vma)) { 2815 pte_unmap_unlock(vmf->pte, vmf->ptl); 2816 return handle_userfault(vmf, VM_UFFD_MISSING); 2817 } 2818 goto setpte; 2819 } 2820 2821 /* Allocate our own private page. */ 2822 if (unlikely(anon_vma_prepare(vma))) 2823 goto oom; 2824 page = alloc_zeroed_user_highpage_movable(vma, vmf->address); 2825 if (!page) 2826 goto oom; 2827 2828 if (mem_cgroup_try_charge(page, vma->vm_mm, GFP_KERNEL, &memcg, false)) 2829 goto oom_free_page; 2830 2831 /* 2832 * The memory barrier inside __SetPageUptodate makes sure that 2833 * preceeding stores to the page contents become visible before 2834 * the set_pte_at() write. 2835 */ 2836 __SetPageUptodate(page); 2837 2838 entry = mk_pte(page, vma->vm_page_prot); 2839 if (vma->vm_flags & VM_WRITE) 2840 entry = pte_mkwrite(pte_mkdirty(entry)); 2841 2842 vmf->pte = pte_offset_map_lock(vma->vm_mm, vmf->pmd, vmf->address, 2843 &vmf->ptl); 2844 if (!pte_none(*vmf->pte)) 2845 goto release; 2846 2847 /* Deliver the page fault to userland, check inside PT lock */ 2848 if (userfaultfd_missing(vma)) { 2849 pte_unmap_unlock(vmf->pte, vmf->ptl); 2850 mem_cgroup_cancel_charge(page, memcg, false); 2851 put_page(page); 2852 return handle_userfault(vmf, VM_UFFD_MISSING); 2853 } 2854 2855 inc_mm_counter_fast(vma->vm_mm, MM_ANONPAGES); 2856 page_add_new_anon_rmap(page, vma, vmf->address, false); 2857 mem_cgroup_commit_charge(page, memcg, false, false); 2858 lru_cache_add_active_or_unevictable(page, vma); 2859 setpte: 2860 set_pte_at(vma->vm_mm, vmf->address, vmf->pte, entry); 2861 2862 /* No need to invalidate - it was non-present before */ 2863 update_mmu_cache(vma, vmf->address, vmf->pte); 2864 unlock: 2865 pte_unmap_unlock(vmf->pte, vmf->ptl); 2866 return 0; 2867 release: 2868 mem_cgroup_cancel_charge(page, memcg, false); 2869 put_page(page); 2870 goto unlock; 2871 oom_free_page: 2872 put_page(page); 2873 oom: 2874 return VM_FAULT_OOM; 2875 } 2876 2877 /* 2878 * The mmap_sem must have been held on entry, and may have been 2879 * released depending on flags and vma->vm_ops->fault() return value. 2880 * See filemap_fault() and __lock_page_retry(). 2881 */ 2882 static int __do_fault(struct vm_fault *vmf) 2883 { 2884 struct vm_area_struct *vma = vmf->vma; 2885 int ret; 2886 2887 ret = vma->vm_ops->fault(vmf); 2888 if (unlikely(ret & (VM_FAULT_ERROR | VM_FAULT_NOPAGE | VM_FAULT_RETRY | 2889 VM_FAULT_DONE_COW))) 2890 return ret; 2891 2892 if (unlikely(PageHWPoison(vmf->page))) { 2893 if (ret & VM_FAULT_LOCKED) 2894 unlock_page(vmf->page); 2895 put_page(vmf->page); 2896 vmf->page = NULL; 2897 return VM_FAULT_HWPOISON; 2898 } 2899 2900 if (unlikely(!(ret & VM_FAULT_LOCKED))) 2901 lock_page(vmf->page); 2902 else 2903 VM_BUG_ON_PAGE(!PageLocked(vmf->page), vmf->page); 2904 2905 return ret; 2906 } 2907 2908 static int pte_alloc_one_map(struct vm_fault *vmf) 2909 { 2910 struct vm_area_struct *vma = vmf->vma; 2911 2912 if (!pmd_none(*vmf->pmd)) 2913 goto map_pte; 2914 if (vmf->prealloc_pte) { 2915 vmf->ptl = pmd_lock(vma->vm_mm, vmf->pmd); 2916 if (unlikely(!pmd_none(*vmf->pmd))) { 2917 spin_unlock(vmf->ptl); 2918 goto map_pte; 2919 } 2920 2921 atomic_long_inc(&vma->vm_mm->nr_ptes); 2922 pmd_populate(vma->vm_mm, vmf->pmd, vmf->prealloc_pte); 2923 spin_unlock(vmf->ptl); 2924 vmf->prealloc_pte = NULL; 2925 } else if (unlikely(pte_alloc(vma->vm_mm, vmf->pmd, vmf->address))) { 2926 return VM_FAULT_OOM; 2927 } 2928 map_pte: 2929 /* 2930 * If a huge pmd materialized under us just retry later. Use 2931 * pmd_trans_unstable() instead of pmd_trans_huge() to ensure the pmd 2932 * didn't become pmd_trans_huge under us and then back to pmd_none, as 2933 * a result of MADV_DONTNEED running immediately after a huge pmd fault 2934 * in a different thread of this mm, in turn leading to a misleading 2935 * pmd_trans_huge() retval. All we have to ensure is that it is a 2936 * regular pmd that we can walk with pte_offset_map() and we can do that 2937 * through an atomic read in C, which is what pmd_trans_unstable() 2938 * provides. 2939 */ 2940 if (pmd_trans_unstable(vmf->pmd) || pmd_devmap(*vmf->pmd)) 2941 return VM_FAULT_NOPAGE; 2942 2943 vmf->pte = pte_offset_map_lock(vma->vm_mm, vmf->pmd, vmf->address, 2944 &vmf->ptl); 2945 return 0; 2946 } 2947 2948 #ifdef CONFIG_TRANSPARENT_HUGE_PAGECACHE 2949 2950 #define HPAGE_CACHE_INDEX_MASK (HPAGE_PMD_NR - 1) 2951 static inline bool transhuge_vma_suitable(struct vm_area_struct *vma, 2952 unsigned long haddr) 2953 { 2954 if (((vma->vm_start >> PAGE_SHIFT) & HPAGE_CACHE_INDEX_MASK) != 2955 (vma->vm_pgoff & HPAGE_CACHE_INDEX_MASK)) 2956 return false; 2957 if (haddr < vma->vm_start || haddr + HPAGE_PMD_SIZE > vma->vm_end) 2958 return false; 2959 return true; 2960 } 2961 2962 static void deposit_prealloc_pte(struct vm_fault *vmf) 2963 { 2964 struct vm_area_struct *vma = vmf->vma; 2965 2966 pgtable_trans_huge_deposit(vma->vm_mm, vmf->pmd, vmf->prealloc_pte); 2967 /* 2968 * We are going to consume the prealloc table, 2969 * count that as nr_ptes. 2970 */ 2971 atomic_long_inc(&vma->vm_mm->nr_ptes); 2972 vmf->prealloc_pte = NULL; 2973 } 2974 2975 static int do_set_pmd(struct vm_fault *vmf, struct page *page) 2976 { 2977 struct vm_area_struct *vma = vmf->vma; 2978 bool write = vmf->flags & FAULT_FLAG_WRITE; 2979 unsigned long haddr = vmf->address & HPAGE_PMD_MASK; 2980 pmd_t entry; 2981 int i, ret; 2982 2983 if (!transhuge_vma_suitable(vma, haddr)) 2984 return VM_FAULT_FALLBACK; 2985 2986 ret = VM_FAULT_FALLBACK; 2987 page = compound_head(page); 2988 2989 /* 2990 * Archs like ppc64 need additonal space to store information 2991 * related to pte entry. Use the preallocated table for that. 2992 */ 2993 if (arch_needs_pgtable_deposit() && !vmf->prealloc_pte) { 2994 vmf->prealloc_pte = pte_alloc_one(vma->vm_mm, vmf->address); 2995 if (!vmf->prealloc_pte) 2996 return VM_FAULT_OOM; 2997 smp_wmb(); /* See comment in __pte_alloc() */ 2998 } 2999 3000 vmf->ptl = pmd_lock(vma->vm_mm, vmf->pmd); 3001 if (unlikely(!pmd_none(*vmf->pmd))) 3002 goto out; 3003 3004 for (i = 0; i < HPAGE_PMD_NR; i++) 3005 flush_icache_page(vma, page + i); 3006 3007 entry = mk_huge_pmd(page, vma->vm_page_prot); 3008 if (write) 3009 entry = maybe_pmd_mkwrite(pmd_mkdirty(entry), vma); 3010 3011 add_mm_counter(vma->vm_mm, MM_FILEPAGES, HPAGE_PMD_NR); 3012 page_add_file_rmap(page, true); 3013 /* 3014 * deposit and withdraw with pmd lock held 3015 */ 3016 if (arch_needs_pgtable_deposit()) 3017 deposit_prealloc_pte(vmf); 3018 3019 set_pmd_at(vma->vm_mm, haddr, vmf->pmd, entry); 3020 3021 update_mmu_cache_pmd(vma, haddr, vmf->pmd); 3022 3023 /* fault is handled */ 3024 ret = 0; 3025 count_vm_event(THP_FILE_MAPPED); 3026 out: 3027 spin_unlock(vmf->ptl); 3028 return ret; 3029 } 3030 #else 3031 static int do_set_pmd(struct vm_fault *vmf, struct page *page) 3032 { 3033 BUILD_BUG(); 3034 return 0; 3035 } 3036 #endif 3037 3038 /** 3039 * alloc_set_pte - setup new PTE entry for given page and add reverse page 3040 * mapping. If needed, the fucntion allocates page table or use pre-allocated. 3041 * 3042 * @vmf: fault environment 3043 * @memcg: memcg to charge page (only for private mappings) 3044 * @page: page to map 3045 * 3046 * Caller must take care of unlocking vmf->ptl, if vmf->pte is non-NULL on 3047 * return. 3048 * 3049 * Target users are page handler itself and implementations of 3050 * vm_ops->map_pages. 3051 */ 3052 int alloc_set_pte(struct vm_fault *vmf, struct mem_cgroup *memcg, 3053 struct page *page) 3054 { 3055 struct vm_area_struct *vma = vmf->vma; 3056 bool write = vmf->flags & FAULT_FLAG_WRITE; 3057 pte_t entry; 3058 int ret; 3059 3060 if (pmd_none(*vmf->pmd) && PageTransCompound(page) && 3061 IS_ENABLED(CONFIG_TRANSPARENT_HUGE_PAGECACHE)) { 3062 /* THP on COW? */ 3063 VM_BUG_ON_PAGE(memcg, page); 3064 3065 ret = do_set_pmd(vmf, page); 3066 if (ret != VM_FAULT_FALLBACK) 3067 return ret; 3068 } 3069 3070 if (!vmf->pte) { 3071 ret = pte_alloc_one_map(vmf); 3072 if (ret) 3073 return ret; 3074 } 3075 3076 /* Re-check under ptl */ 3077 if (unlikely(!pte_none(*vmf->pte))) 3078 return VM_FAULT_NOPAGE; 3079 3080 flush_icache_page(vma, page); 3081 entry = mk_pte(page, vma->vm_page_prot); 3082 if (write) 3083 entry = maybe_mkwrite(pte_mkdirty(entry), vma); 3084 /* copy-on-write page */ 3085 if (write && !(vma->vm_flags & VM_SHARED)) { 3086 inc_mm_counter_fast(vma->vm_mm, MM_ANONPAGES); 3087 page_add_new_anon_rmap(page, vma, vmf->address, false); 3088 mem_cgroup_commit_charge(page, memcg, false, false); 3089 lru_cache_add_active_or_unevictable(page, vma); 3090 } else { 3091 inc_mm_counter_fast(vma->vm_mm, mm_counter_file(page)); 3092 page_add_file_rmap(page, false); 3093 } 3094 set_pte_at(vma->vm_mm, vmf->address, vmf->pte, entry); 3095 3096 /* no need to invalidate: a not-present page won't be cached */ 3097 update_mmu_cache(vma, vmf->address, vmf->pte); 3098 3099 return 0; 3100 } 3101 3102 3103 /** 3104 * finish_fault - finish page fault once we have prepared the page to fault 3105 * 3106 * @vmf: structure describing the fault 3107 * 3108 * This function handles all that is needed to finish a page fault once the 3109 * page to fault in is prepared. It handles locking of PTEs, inserts PTE for 3110 * given page, adds reverse page mapping, handles memcg charges and LRU 3111 * addition. The function returns 0 on success, VM_FAULT_ code in case of 3112 * error. 3113 * 3114 * The function expects the page to be locked and on success it consumes a 3115 * reference of a page being mapped (for the PTE which maps it). 3116 */ 3117 int finish_fault(struct vm_fault *vmf) 3118 { 3119 struct page *page; 3120 int ret; 3121 3122 /* Did we COW the page? */ 3123 if ((vmf->flags & FAULT_FLAG_WRITE) && 3124 !(vmf->vma->vm_flags & VM_SHARED)) 3125 page = vmf->cow_page; 3126 else 3127 page = vmf->page; 3128 ret = alloc_set_pte(vmf, vmf->memcg, page); 3129 if (vmf->pte) 3130 pte_unmap_unlock(vmf->pte, vmf->ptl); 3131 return ret; 3132 } 3133 3134 static unsigned long fault_around_bytes __read_mostly = 3135 rounddown_pow_of_two(65536); 3136 3137 #ifdef CONFIG_DEBUG_FS 3138 static int fault_around_bytes_get(void *data, u64 *val) 3139 { 3140 *val = fault_around_bytes; 3141 return 0; 3142 } 3143 3144 /* 3145 * fault_around_pages() and fault_around_mask() expects fault_around_bytes 3146 * rounded down to nearest page order. It's what do_fault_around() expects to 3147 * see. 3148 */ 3149 static int fault_around_bytes_set(void *data, u64 val) 3150 { 3151 if (val / PAGE_SIZE > PTRS_PER_PTE) 3152 return -EINVAL; 3153 if (val > PAGE_SIZE) 3154 fault_around_bytes = rounddown_pow_of_two(val); 3155 else 3156 fault_around_bytes = PAGE_SIZE; /* rounddown_pow_of_two(0) is undefined */ 3157 return 0; 3158 } 3159 DEFINE_SIMPLE_ATTRIBUTE(fault_around_bytes_fops, 3160 fault_around_bytes_get, fault_around_bytes_set, "%llu\n"); 3161 3162 static int __init fault_around_debugfs(void) 3163 { 3164 void *ret; 3165 3166 ret = debugfs_create_file("fault_around_bytes", 0644, NULL, NULL, 3167 &fault_around_bytes_fops); 3168 if (!ret) 3169 pr_warn("Failed to create fault_around_bytes in debugfs"); 3170 return 0; 3171 } 3172 late_initcall(fault_around_debugfs); 3173 #endif 3174 3175 /* 3176 * do_fault_around() tries to map few pages around the fault address. The hope 3177 * is that the pages will be needed soon and this will lower the number of 3178 * faults to handle. 3179 * 3180 * It uses vm_ops->map_pages() to map the pages, which skips the page if it's 3181 * not ready to be mapped: not up-to-date, locked, etc. 3182 * 3183 * This function is called with the page table lock taken. In the split ptlock 3184 * case the page table lock only protects only those entries which belong to 3185 * the page table corresponding to the fault address. 3186 * 3187 * This function doesn't cross the VMA boundaries, in order to call map_pages() 3188 * only once. 3189 * 3190 * fault_around_pages() defines how many pages we'll try to map. 3191 * do_fault_around() expects it to return a power of two less than or equal to 3192 * PTRS_PER_PTE. 3193 * 3194 * The virtual address of the area that we map is naturally aligned to the 3195 * fault_around_pages() value (and therefore to page order). This way it's 3196 * easier to guarantee that we don't cross page table boundaries. 3197 */ 3198 static int do_fault_around(struct vm_fault *vmf) 3199 { 3200 unsigned long address = vmf->address, nr_pages, mask; 3201 pgoff_t start_pgoff = vmf->pgoff; 3202 pgoff_t end_pgoff; 3203 int off, ret = 0; 3204 3205 nr_pages = READ_ONCE(fault_around_bytes) >> PAGE_SHIFT; 3206 mask = ~(nr_pages * PAGE_SIZE - 1) & PAGE_MASK; 3207 3208 vmf->address = max(address & mask, vmf->vma->vm_start); 3209 off = ((address - vmf->address) >> PAGE_SHIFT) & (PTRS_PER_PTE - 1); 3210 start_pgoff -= off; 3211 3212 /* 3213 * end_pgoff is either end of page table or end of vma 3214 * or fault_around_pages() from start_pgoff, depending what is nearest. 3215 */ 3216 end_pgoff = start_pgoff - 3217 ((vmf->address >> PAGE_SHIFT) & (PTRS_PER_PTE - 1)) + 3218 PTRS_PER_PTE - 1; 3219 end_pgoff = min3(end_pgoff, vma_pages(vmf->vma) + vmf->vma->vm_pgoff - 1, 3220 start_pgoff + nr_pages - 1); 3221 3222 if (pmd_none(*vmf->pmd)) { 3223 vmf->prealloc_pte = pte_alloc_one(vmf->vma->vm_mm, 3224 vmf->address); 3225 if (!vmf->prealloc_pte) 3226 goto out; 3227 smp_wmb(); /* See comment in __pte_alloc() */ 3228 } 3229 3230 vmf->vma->vm_ops->map_pages(vmf, start_pgoff, end_pgoff); 3231 3232 /* Huge page is mapped? Page fault is solved */ 3233 if (pmd_trans_huge(*vmf->pmd)) { 3234 ret = VM_FAULT_NOPAGE; 3235 goto out; 3236 } 3237 3238 /* ->map_pages() haven't done anything useful. Cold page cache? */ 3239 if (!vmf->pte) 3240 goto out; 3241 3242 /* check if the page fault is solved */ 3243 vmf->pte -= (vmf->address >> PAGE_SHIFT) - (address >> PAGE_SHIFT); 3244 if (!pte_none(*vmf->pte)) 3245 ret = VM_FAULT_NOPAGE; 3246 pte_unmap_unlock(vmf->pte, vmf->ptl); 3247 out: 3248 vmf->address = address; 3249 vmf->pte = NULL; 3250 return ret; 3251 } 3252 3253 static int do_read_fault(struct vm_fault *vmf) 3254 { 3255 struct vm_area_struct *vma = vmf->vma; 3256 int ret = 0; 3257 3258 /* 3259 * Let's call ->map_pages() first and use ->fault() as fallback 3260 * if page by the offset is not ready to be mapped (cold cache or 3261 * something). 3262 */ 3263 if (vma->vm_ops->map_pages && fault_around_bytes >> PAGE_SHIFT > 1) { 3264 ret = do_fault_around(vmf); 3265 if (ret) 3266 return ret; 3267 } 3268 3269 ret = __do_fault(vmf); 3270 if (unlikely(ret & (VM_FAULT_ERROR | VM_FAULT_NOPAGE | VM_FAULT_RETRY))) 3271 return ret; 3272 3273 ret |= finish_fault(vmf); 3274 unlock_page(vmf->page); 3275 if (unlikely(ret & (VM_FAULT_ERROR | VM_FAULT_NOPAGE | VM_FAULT_RETRY))) 3276 put_page(vmf->page); 3277 return ret; 3278 } 3279 3280 static int do_cow_fault(struct vm_fault *vmf) 3281 { 3282 struct vm_area_struct *vma = vmf->vma; 3283 int ret; 3284 3285 if (unlikely(anon_vma_prepare(vma))) 3286 return VM_FAULT_OOM; 3287 3288 vmf->cow_page = alloc_page_vma(GFP_HIGHUSER_MOVABLE, vma, vmf->address); 3289 if (!vmf->cow_page) 3290 return VM_FAULT_OOM; 3291 3292 if (mem_cgroup_try_charge(vmf->cow_page, vma->vm_mm, GFP_KERNEL, 3293 &vmf->memcg, false)) { 3294 put_page(vmf->cow_page); 3295 return VM_FAULT_OOM; 3296 } 3297 3298 ret = __do_fault(vmf); 3299 if (unlikely(ret & (VM_FAULT_ERROR | VM_FAULT_NOPAGE | VM_FAULT_RETRY))) 3300 goto uncharge_out; 3301 if (ret & VM_FAULT_DONE_COW) 3302 return ret; 3303 3304 copy_user_highpage(vmf->cow_page, vmf->page, vmf->address, vma); 3305 __SetPageUptodate(vmf->cow_page); 3306 3307 ret |= finish_fault(vmf); 3308 unlock_page(vmf->page); 3309 put_page(vmf->page); 3310 if (unlikely(ret & (VM_FAULT_ERROR | VM_FAULT_NOPAGE | VM_FAULT_RETRY))) 3311 goto uncharge_out; 3312 return ret; 3313 uncharge_out: 3314 mem_cgroup_cancel_charge(vmf->cow_page, vmf->memcg, false); 3315 put_page(vmf->cow_page); 3316 return ret; 3317 } 3318 3319 static int do_shared_fault(struct vm_fault *vmf) 3320 { 3321 struct vm_area_struct *vma = vmf->vma; 3322 int ret, tmp; 3323 3324 ret = __do_fault(vmf); 3325 if (unlikely(ret & (VM_FAULT_ERROR | VM_FAULT_NOPAGE | VM_FAULT_RETRY))) 3326 return ret; 3327 3328 /* 3329 * Check if the backing address space wants to know that the page is 3330 * about to become writable 3331 */ 3332 if (vma->vm_ops->page_mkwrite) { 3333 unlock_page(vmf->page); 3334 tmp = do_page_mkwrite(vmf); 3335 if (unlikely(!tmp || 3336 (tmp & (VM_FAULT_ERROR | VM_FAULT_NOPAGE)))) { 3337 put_page(vmf->page); 3338 return tmp; 3339 } 3340 } 3341 3342 ret |= finish_fault(vmf); 3343 if (unlikely(ret & (VM_FAULT_ERROR | VM_FAULT_NOPAGE | 3344 VM_FAULT_RETRY))) { 3345 unlock_page(vmf->page); 3346 put_page(vmf->page); 3347 return ret; 3348 } 3349 3350 fault_dirty_shared_page(vma, vmf->page); 3351 return ret; 3352 } 3353 3354 /* 3355 * We enter with non-exclusive mmap_sem (to exclude vma changes, 3356 * but allow concurrent faults). 3357 * The mmap_sem may have been released depending on flags and our 3358 * return value. See filemap_fault() and __lock_page_or_retry(). 3359 */ 3360 static int do_fault(struct vm_fault *vmf) 3361 { 3362 struct vm_area_struct *vma = vmf->vma; 3363 int ret; 3364 3365 /* The VMA was not fully populated on mmap() or missing VM_DONTEXPAND */ 3366 if (!vma->vm_ops->fault) 3367 ret = VM_FAULT_SIGBUS; 3368 else if (!(vmf->flags & FAULT_FLAG_WRITE)) 3369 ret = do_read_fault(vmf); 3370 else if (!(vma->vm_flags & VM_SHARED)) 3371 ret = do_cow_fault(vmf); 3372 else 3373 ret = do_shared_fault(vmf); 3374 3375 /* preallocated pagetable is unused: free it */ 3376 if (vmf->prealloc_pte) { 3377 pte_free(vma->vm_mm, vmf->prealloc_pte); 3378 vmf->prealloc_pte = NULL; 3379 } 3380 return ret; 3381 } 3382 3383 static int numa_migrate_prep(struct page *page, struct vm_area_struct *vma, 3384 unsigned long addr, int page_nid, 3385 int *flags) 3386 { 3387 get_page(page); 3388 3389 count_vm_numa_event(NUMA_HINT_FAULTS); 3390 if (page_nid == numa_node_id()) { 3391 count_vm_numa_event(NUMA_HINT_FAULTS_LOCAL); 3392 *flags |= TNF_FAULT_LOCAL; 3393 } 3394 3395 return mpol_misplaced(page, vma, addr); 3396 } 3397 3398 static int do_numa_page(struct vm_fault *vmf) 3399 { 3400 struct vm_area_struct *vma = vmf->vma; 3401 struct page *page = NULL; 3402 int page_nid = -1; 3403 int last_cpupid; 3404 int target_nid; 3405 bool migrated = false; 3406 pte_t pte; 3407 bool was_writable = pte_savedwrite(vmf->orig_pte); 3408 int flags = 0; 3409 3410 /* 3411 * The "pte" at this point cannot be used safely without 3412 * validation through pte_unmap_same(). It's of NUMA type but 3413 * the pfn may be screwed if the read is non atomic. 3414 */ 3415 vmf->ptl = pte_lockptr(vma->vm_mm, vmf->pmd); 3416 spin_lock(vmf->ptl); 3417 if (unlikely(!pte_same(*vmf->pte, vmf->orig_pte))) { 3418 pte_unmap_unlock(vmf->pte, vmf->ptl); 3419 goto out; 3420 } 3421 3422 /* 3423 * Make it present again, Depending on how arch implementes non 3424 * accessible ptes, some can allow access by kernel mode. 3425 */ 3426 pte = ptep_modify_prot_start(vma->vm_mm, vmf->address, vmf->pte); 3427 pte = pte_modify(pte, vma->vm_page_prot); 3428 pte = pte_mkyoung(pte); 3429 if (was_writable) 3430 pte = pte_mkwrite(pte); 3431 ptep_modify_prot_commit(vma->vm_mm, vmf->address, vmf->pte, pte); 3432 update_mmu_cache(vma, vmf->address, vmf->pte); 3433 3434 page = vm_normal_page(vma, vmf->address, pte); 3435 if (!page) { 3436 pte_unmap_unlock(vmf->pte, vmf->ptl); 3437 return 0; 3438 } 3439 3440 /* TODO: handle PTE-mapped THP */ 3441 if (PageCompound(page)) { 3442 pte_unmap_unlock(vmf->pte, vmf->ptl); 3443 return 0; 3444 } 3445 3446 /* 3447 * Avoid grouping on RO pages in general. RO pages shouldn't hurt as 3448 * much anyway since they can be in shared cache state. This misses 3449 * the case where a mapping is writable but the process never writes 3450 * to it but pte_write gets cleared during protection updates and 3451 * pte_dirty has unpredictable behaviour between PTE scan updates, 3452 * background writeback, dirty balancing and application behaviour. 3453 */ 3454 if (!pte_write(pte)) 3455 flags |= TNF_NO_GROUP; 3456 3457 /* 3458 * Flag if the page is shared between multiple address spaces. This 3459 * is later used when determining whether to group tasks together 3460 */ 3461 if (page_mapcount(page) > 1 && (vma->vm_flags & VM_SHARED)) 3462 flags |= TNF_SHARED; 3463 3464 last_cpupid = page_cpupid_last(page); 3465 page_nid = page_to_nid(page); 3466 target_nid = numa_migrate_prep(page, vma, vmf->address, page_nid, 3467 &flags); 3468 pte_unmap_unlock(vmf->pte, vmf->ptl); 3469 if (target_nid == -1) { 3470 put_page(page); 3471 goto out; 3472 } 3473 3474 /* Migrate to the requested node */ 3475 migrated = migrate_misplaced_page(page, vma, target_nid); 3476 if (migrated) { 3477 page_nid = target_nid; 3478 flags |= TNF_MIGRATED; 3479 } else 3480 flags |= TNF_MIGRATE_FAIL; 3481 3482 out: 3483 if (page_nid != -1) 3484 task_numa_fault(last_cpupid, page_nid, 1, flags); 3485 return 0; 3486 } 3487 3488 static int create_huge_pmd(struct vm_fault *vmf) 3489 { 3490 if (vma_is_anonymous(vmf->vma)) 3491 return do_huge_pmd_anonymous_page(vmf); 3492 if (vmf->vma->vm_ops->huge_fault) 3493 return vmf->vma->vm_ops->huge_fault(vmf, PE_SIZE_PMD); 3494 return VM_FAULT_FALLBACK; 3495 } 3496 3497 static int wp_huge_pmd(struct vm_fault *vmf, pmd_t orig_pmd) 3498 { 3499 if (vma_is_anonymous(vmf->vma)) 3500 return do_huge_pmd_wp_page(vmf, orig_pmd); 3501 if (vmf->vma->vm_ops->huge_fault) 3502 return vmf->vma->vm_ops->huge_fault(vmf, PE_SIZE_PMD); 3503 3504 /* COW handled on pte level: split pmd */ 3505 VM_BUG_ON_VMA(vmf->vma->vm_flags & VM_SHARED, vmf->vma); 3506 __split_huge_pmd(vmf->vma, vmf->pmd, vmf->address, false, NULL); 3507 3508 return VM_FAULT_FALLBACK; 3509 } 3510 3511 static inline bool vma_is_accessible(struct vm_area_struct *vma) 3512 { 3513 return vma->vm_flags & (VM_READ | VM_EXEC | VM_WRITE); 3514 } 3515 3516 static int create_huge_pud(struct vm_fault *vmf) 3517 { 3518 #ifdef CONFIG_TRANSPARENT_HUGEPAGE 3519 /* No support for anonymous transparent PUD pages yet */ 3520 if (vma_is_anonymous(vmf->vma)) 3521 return VM_FAULT_FALLBACK; 3522 if (vmf->vma->vm_ops->huge_fault) 3523 return vmf->vma->vm_ops->huge_fault(vmf, PE_SIZE_PUD); 3524 #endif /* CONFIG_TRANSPARENT_HUGEPAGE */ 3525 return VM_FAULT_FALLBACK; 3526 } 3527 3528 static int wp_huge_pud(struct vm_fault *vmf, pud_t orig_pud) 3529 { 3530 #ifdef CONFIG_TRANSPARENT_HUGEPAGE 3531 /* No support for anonymous transparent PUD pages yet */ 3532 if (vma_is_anonymous(vmf->vma)) 3533 return VM_FAULT_FALLBACK; 3534 if (vmf->vma->vm_ops->huge_fault) 3535 return vmf->vma->vm_ops->huge_fault(vmf, PE_SIZE_PUD); 3536 #endif /* CONFIG_TRANSPARENT_HUGEPAGE */ 3537 return VM_FAULT_FALLBACK; 3538 } 3539 3540 /* 3541 * These routines also need to handle stuff like marking pages dirty 3542 * and/or accessed for architectures that don't do it in hardware (most 3543 * RISC architectures). The early dirtying is also good on the i386. 3544 * 3545 * There is also a hook called "update_mmu_cache()" that architectures 3546 * with external mmu caches can use to update those (ie the Sparc or 3547 * PowerPC hashed page tables that act as extended TLBs). 3548 * 3549 * We enter with non-exclusive mmap_sem (to exclude vma changes, but allow 3550 * concurrent faults). 3551 * 3552 * The mmap_sem may have been released depending on flags and our return value. 3553 * See filemap_fault() and __lock_page_or_retry(). 3554 */ 3555 static int handle_pte_fault(struct vm_fault *vmf) 3556 { 3557 pte_t entry; 3558 3559 if (unlikely(pmd_none(*vmf->pmd))) { 3560 /* 3561 * Leave __pte_alloc() until later: because vm_ops->fault may 3562 * want to allocate huge page, and if we expose page table 3563 * for an instant, it will be difficult to retract from 3564 * concurrent faults and from rmap lookups. 3565 */ 3566 vmf->pte = NULL; 3567 } else { 3568 /* See comment in pte_alloc_one_map() */ 3569 if (pmd_trans_unstable(vmf->pmd) || pmd_devmap(*vmf->pmd)) 3570 return 0; 3571 /* 3572 * A regular pmd is established and it can't morph into a huge 3573 * pmd from under us anymore at this point because we hold the 3574 * mmap_sem read mode and khugepaged takes it in write mode. 3575 * So now it's safe to run pte_offset_map(). 3576 */ 3577 vmf->pte = pte_offset_map(vmf->pmd, vmf->address); 3578 vmf->orig_pte = *vmf->pte; 3579 3580 /* 3581 * some architectures can have larger ptes than wordsize, 3582 * e.g.ppc44x-defconfig has CONFIG_PTE_64BIT=y and 3583 * CONFIG_32BIT=y, so READ_ONCE or ACCESS_ONCE cannot guarantee 3584 * atomic accesses. The code below just needs a consistent 3585 * view for the ifs and we later double check anyway with the 3586 * ptl lock held. So here a barrier will do. 3587 */ 3588 barrier(); 3589 if (pte_none(vmf->orig_pte)) { 3590 pte_unmap(vmf->pte); 3591 vmf->pte = NULL; 3592 } 3593 } 3594 3595 if (!vmf->pte) { 3596 if (vma_is_anonymous(vmf->vma)) 3597 return do_anonymous_page(vmf); 3598 else 3599 return do_fault(vmf); 3600 } 3601 3602 if (!pte_present(vmf->orig_pte)) 3603 return do_swap_page(vmf); 3604 3605 if (pte_protnone(vmf->orig_pte) && vma_is_accessible(vmf->vma)) 3606 return do_numa_page(vmf); 3607 3608 vmf->ptl = pte_lockptr(vmf->vma->vm_mm, vmf->pmd); 3609 spin_lock(vmf->ptl); 3610 entry = vmf->orig_pte; 3611 if (unlikely(!pte_same(*vmf->pte, entry))) 3612 goto unlock; 3613 if (vmf->flags & FAULT_FLAG_WRITE) { 3614 if (!pte_write(entry)) 3615 return do_wp_page(vmf); 3616 entry = pte_mkdirty(entry); 3617 } 3618 entry = pte_mkyoung(entry); 3619 if (ptep_set_access_flags(vmf->vma, vmf->address, vmf->pte, entry, 3620 vmf->flags & FAULT_FLAG_WRITE)) { 3621 update_mmu_cache(vmf->vma, vmf->address, vmf->pte); 3622 } else { 3623 /* 3624 * This is needed only for protection faults but the arch code 3625 * is not yet telling us if this is a protection fault or not. 3626 * This still avoids useless tlb flushes for .text page faults 3627 * with threads. 3628 */ 3629 if (vmf->flags & FAULT_FLAG_WRITE) 3630 flush_tlb_fix_spurious_fault(vmf->vma, vmf->address); 3631 } 3632 unlock: 3633 pte_unmap_unlock(vmf->pte, vmf->ptl); 3634 return 0; 3635 } 3636 3637 /* 3638 * By the time we get here, we already hold the mm semaphore 3639 * 3640 * The mmap_sem may have been released depending on flags and our 3641 * return value. See filemap_fault() and __lock_page_or_retry(). 3642 */ 3643 static int __handle_mm_fault(struct vm_area_struct *vma, unsigned long address, 3644 unsigned int flags) 3645 { 3646 struct vm_fault vmf = { 3647 .vma = vma, 3648 .address = address & PAGE_MASK, 3649 .flags = flags, 3650 .pgoff = linear_page_index(vma, address), 3651 .gfp_mask = __get_fault_gfp_mask(vma), 3652 }; 3653 struct mm_struct *mm = vma->vm_mm; 3654 pgd_t *pgd; 3655 int ret; 3656 3657 pgd = pgd_offset(mm, address); 3658 3659 vmf.pud = pud_alloc(mm, pgd, address); 3660 if (!vmf.pud) 3661 return VM_FAULT_OOM; 3662 if (pud_none(*vmf.pud) && transparent_hugepage_enabled(vma)) { 3663 ret = create_huge_pud(&vmf); 3664 if (!(ret & VM_FAULT_FALLBACK)) 3665 return ret; 3666 } else { 3667 pud_t orig_pud = *vmf.pud; 3668 3669 barrier(); 3670 if (pud_trans_huge(orig_pud) || pud_devmap(orig_pud)) { 3671 unsigned int dirty = flags & FAULT_FLAG_WRITE; 3672 3673 /* NUMA case for anonymous PUDs would go here */ 3674 3675 if (dirty && !pud_write(orig_pud)) { 3676 ret = wp_huge_pud(&vmf, orig_pud); 3677 if (!(ret & VM_FAULT_FALLBACK)) 3678 return ret; 3679 } else { 3680 huge_pud_set_accessed(&vmf, orig_pud); 3681 return 0; 3682 } 3683 } 3684 } 3685 3686 vmf.pmd = pmd_alloc(mm, vmf.pud, address); 3687 if (!vmf.pmd) 3688 return VM_FAULT_OOM; 3689 if (pmd_none(*vmf.pmd) && transparent_hugepage_enabled(vma)) { 3690 ret = create_huge_pmd(&vmf); 3691 if (!(ret & VM_FAULT_FALLBACK)) 3692 return ret; 3693 } else { 3694 pmd_t orig_pmd = *vmf.pmd; 3695 3696 barrier(); 3697 if (pmd_trans_huge(orig_pmd) || pmd_devmap(orig_pmd)) { 3698 if (pmd_protnone(orig_pmd) && vma_is_accessible(vma)) 3699 return do_huge_pmd_numa_page(&vmf, orig_pmd); 3700 3701 if ((vmf.flags & FAULT_FLAG_WRITE) && 3702 !pmd_write(orig_pmd)) { 3703 ret = wp_huge_pmd(&vmf, orig_pmd); 3704 if (!(ret & VM_FAULT_FALLBACK)) 3705 return ret; 3706 } else { 3707 huge_pmd_set_accessed(&vmf, orig_pmd); 3708 return 0; 3709 } 3710 } 3711 } 3712 3713 return handle_pte_fault(&vmf); 3714 } 3715 3716 /* 3717 * By the time we get here, we already hold the mm semaphore 3718 * 3719 * The mmap_sem may have been released depending on flags and our 3720 * return value. See filemap_fault() and __lock_page_or_retry(). 3721 */ 3722 int handle_mm_fault(struct vm_area_struct *vma, unsigned long address, 3723 unsigned int flags) 3724 { 3725 int ret; 3726 3727 __set_current_state(TASK_RUNNING); 3728 3729 count_vm_event(PGFAULT); 3730 mem_cgroup_count_vm_event(vma->vm_mm, PGFAULT); 3731 3732 /* do counter updates before entering really critical section. */ 3733 check_sync_rss_stat(current); 3734 3735 /* 3736 * Enable the memcg OOM handling for faults triggered in user 3737 * space. Kernel faults are handled more gracefully. 3738 */ 3739 if (flags & FAULT_FLAG_USER) 3740 mem_cgroup_oom_enable(); 3741 3742 if (!arch_vma_access_permitted(vma, flags & FAULT_FLAG_WRITE, 3743 flags & FAULT_FLAG_INSTRUCTION, 3744 flags & FAULT_FLAG_REMOTE)) 3745 return VM_FAULT_SIGSEGV; 3746 3747 if (unlikely(is_vm_hugetlb_page(vma))) 3748 ret = hugetlb_fault(vma->vm_mm, vma, address, flags); 3749 else 3750 ret = __handle_mm_fault(vma, address, flags); 3751 3752 if (flags & FAULT_FLAG_USER) { 3753 mem_cgroup_oom_disable(); 3754 /* 3755 * The task may have entered a memcg OOM situation but 3756 * if the allocation error was handled gracefully (no 3757 * VM_FAULT_OOM), there is no need to kill anything. 3758 * Just clean up the OOM state peacefully. 3759 */ 3760 if (task_in_memcg_oom(current) && !(ret & VM_FAULT_OOM)) 3761 mem_cgroup_oom_synchronize(false); 3762 } 3763 3764 /* 3765 * This mm has been already reaped by the oom reaper and so the 3766 * refault cannot be trusted in general. Anonymous refaults would 3767 * lose data and give a zero page instead e.g. This is especially 3768 * problem for use_mm() because regular tasks will just die and 3769 * the corrupted data will not be visible anywhere while kthread 3770 * will outlive the oom victim and potentially propagate the date 3771 * further. 3772 */ 3773 if (unlikely((current->flags & PF_KTHREAD) && !(ret & VM_FAULT_ERROR) 3774 && test_bit(MMF_UNSTABLE, &vma->vm_mm->flags))) 3775 ret = VM_FAULT_SIGBUS; 3776 3777 return ret; 3778 } 3779 EXPORT_SYMBOL_GPL(handle_mm_fault); 3780 3781 #ifndef __PAGETABLE_PUD_FOLDED 3782 /* 3783 * Allocate page upper directory. 3784 * We've already handled the fast-path in-line. 3785 */ 3786 int __pud_alloc(struct mm_struct *mm, pgd_t *pgd, unsigned long address) 3787 { 3788 pud_t *new = pud_alloc_one(mm, address); 3789 if (!new) 3790 return -ENOMEM; 3791 3792 smp_wmb(); /* See comment in __pte_alloc */ 3793 3794 spin_lock(&mm->page_table_lock); 3795 if (pgd_present(*pgd)) /* Another has populated it */ 3796 pud_free(mm, new); 3797 else 3798 pgd_populate(mm, pgd, new); 3799 spin_unlock(&mm->page_table_lock); 3800 return 0; 3801 } 3802 #endif /* __PAGETABLE_PUD_FOLDED */ 3803 3804 #ifndef __PAGETABLE_PMD_FOLDED 3805 /* 3806 * Allocate page middle directory. 3807 * We've already handled the fast-path in-line. 3808 */ 3809 int __pmd_alloc(struct mm_struct *mm, pud_t *pud, unsigned long address) 3810 { 3811 spinlock_t *ptl; 3812 pmd_t *new = pmd_alloc_one(mm, address); 3813 if (!new) 3814 return -ENOMEM; 3815 3816 smp_wmb(); /* See comment in __pte_alloc */ 3817 3818 ptl = pud_lock(mm, pud); 3819 #ifndef __ARCH_HAS_4LEVEL_HACK 3820 if (!pud_present(*pud)) { 3821 mm_inc_nr_pmds(mm); 3822 pud_populate(mm, pud, new); 3823 } else /* Another has populated it */ 3824 pmd_free(mm, new); 3825 #else 3826 if (!pgd_present(*pud)) { 3827 mm_inc_nr_pmds(mm); 3828 pgd_populate(mm, pud, new); 3829 } else /* Another has populated it */ 3830 pmd_free(mm, new); 3831 #endif /* __ARCH_HAS_4LEVEL_HACK */ 3832 spin_unlock(ptl); 3833 return 0; 3834 } 3835 #endif /* __PAGETABLE_PMD_FOLDED */ 3836 3837 static int __follow_pte_pmd(struct mm_struct *mm, unsigned long address, 3838 pte_t **ptepp, pmd_t **pmdpp, spinlock_t **ptlp) 3839 { 3840 pgd_t *pgd; 3841 pud_t *pud; 3842 pmd_t *pmd; 3843 pte_t *ptep; 3844 3845 pgd = pgd_offset(mm, address); 3846 if (pgd_none(*pgd) || unlikely(pgd_bad(*pgd))) 3847 goto out; 3848 3849 pud = pud_offset(pgd, address); 3850 if (pud_none(*pud) || unlikely(pud_bad(*pud))) 3851 goto out; 3852 3853 pmd = pmd_offset(pud, address); 3854 VM_BUG_ON(pmd_trans_huge(*pmd)); 3855 3856 if (pmd_huge(*pmd)) { 3857 if (!pmdpp) 3858 goto out; 3859 3860 *ptlp = pmd_lock(mm, pmd); 3861 if (pmd_huge(*pmd)) { 3862 *pmdpp = pmd; 3863 return 0; 3864 } 3865 spin_unlock(*ptlp); 3866 } 3867 3868 if (pmd_none(*pmd) || unlikely(pmd_bad(*pmd))) 3869 goto out; 3870 3871 ptep = pte_offset_map_lock(mm, pmd, address, ptlp); 3872 if (!ptep) 3873 goto out; 3874 if (!pte_present(*ptep)) 3875 goto unlock; 3876 *ptepp = ptep; 3877 return 0; 3878 unlock: 3879 pte_unmap_unlock(ptep, *ptlp); 3880 out: 3881 return -EINVAL; 3882 } 3883 3884 static inline int follow_pte(struct mm_struct *mm, unsigned long address, 3885 pte_t **ptepp, spinlock_t **ptlp) 3886 { 3887 int res; 3888 3889 /* (void) is needed to make gcc happy */ 3890 (void) __cond_lock(*ptlp, 3891 !(res = __follow_pte_pmd(mm, address, ptepp, NULL, 3892 ptlp))); 3893 return res; 3894 } 3895 3896 int follow_pte_pmd(struct mm_struct *mm, unsigned long address, 3897 pte_t **ptepp, pmd_t **pmdpp, spinlock_t **ptlp) 3898 { 3899 int res; 3900 3901 /* (void) is needed to make gcc happy */ 3902 (void) __cond_lock(*ptlp, 3903 !(res = __follow_pte_pmd(mm, address, ptepp, pmdpp, 3904 ptlp))); 3905 return res; 3906 } 3907 EXPORT_SYMBOL(follow_pte_pmd); 3908 3909 /** 3910 * follow_pfn - look up PFN at a user virtual address 3911 * @vma: memory mapping 3912 * @address: user virtual address 3913 * @pfn: location to store found PFN 3914 * 3915 * Only IO mappings and raw PFN mappings are allowed. 3916 * 3917 * Returns zero and the pfn at @pfn on success, -ve otherwise. 3918 */ 3919 int follow_pfn(struct vm_area_struct *vma, unsigned long address, 3920 unsigned long *pfn) 3921 { 3922 int ret = -EINVAL; 3923 spinlock_t *ptl; 3924 pte_t *ptep; 3925 3926 if (!(vma->vm_flags & (VM_IO | VM_PFNMAP))) 3927 return ret; 3928 3929 ret = follow_pte(vma->vm_mm, address, &ptep, &ptl); 3930 if (ret) 3931 return ret; 3932 *pfn = pte_pfn(*ptep); 3933 pte_unmap_unlock(ptep, ptl); 3934 return 0; 3935 } 3936 EXPORT_SYMBOL(follow_pfn); 3937 3938 #ifdef CONFIG_HAVE_IOREMAP_PROT 3939 int follow_phys(struct vm_area_struct *vma, 3940 unsigned long address, unsigned int flags, 3941 unsigned long *prot, resource_size_t *phys) 3942 { 3943 int ret = -EINVAL; 3944 pte_t *ptep, pte; 3945 spinlock_t *ptl; 3946 3947 if (!(vma->vm_flags & (VM_IO | VM_PFNMAP))) 3948 goto out; 3949 3950 if (follow_pte(vma->vm_mm, address, &ptep, &ptl)) 3951 goto out; 3952 pte = *ptep; 3953 3954 if ((flags & FOLL_WRITE) && !pte_write(pte)) 3955 goto unlock; 3956 3957 *prot = pgprot_val(pte_pgprot(pte)); 3958 *phys = (resource_size_t)pte_pfn(pte) << PAGE_SHIFT; 3959 3960 ret = 0; 3961 unlock: 3962 pte_unmap_unlock(ptep, ptl); 3963 out: 3964 return ret; 3965 } 3966 3967 int generic_access_phys(struct vm_area_struct *vma, unsigned long addr, 3968 void *buf, int len, int write) 3969 { 3970 resource_size_t phys_addr; 3971 unsigned long prot = 0; 3972 void __iomem *maddr; 3973 int offset = addr & (PAGE_SIZE-1); 3974 3975 if (follow_phys(vma, addr, write, &prot, &phys_addr)) 3976 return -EINVAL; 3977 3978 maddr = ioremap_prot(phys_addr, PAGE_ALIGN(len + offset), prot); 3979 if (write) 3980 memcpy_toio(maddr + offset, buf, len); 3981 else 3982 memcpy_fromio(buf, maddr + offset, len); 3983 iounmap(maddr); 3984 3985 return len; 3986 } 3987 EXPORT_SYMBOL_GPL(generic_access_phys); 3988 #endif 3989 3990 /* 3991 * Access another process' address space as given in mm. If non-NULL, use the 3992 * given task for page fault accounting. 3993 */ 3994 int __access_remote_vm(struct task_struct *tsk, struct mm_struct *mm, 3995 unsigned long addr, void *buf, int len, unsigned int gup_flags) 3996 { 3997 struct vm_area_struct *vma; 3998 void *old_buf = buf; 3999 int write = gup_flags & FOLL_WRITE; 4000 4001 down_read(&mm->mmap_sem); 4002 /* ignore errors, just check how much was successfully transferred */ 4003 while (len) { 4004 int bytes, ret, offset; 4005 void *maddr; 4006 struct page *page = NULL; 4007 4008 ret = get_user_pages_remote(tsk, mm, addr, 1, 4009 gup_flags, &page, &vma, NULL); 4010 if (ret <= 0) { 4011 #ifndef CONFIG_HAVE_IOREMAP_PROT 4012 break; 4013 #else 4014 /* 4015 * Check if this is a VM_IO | VM_PFNMAP VMA, which 4016 * we can access using slightly different code. 4017 */ 4018 vma = find_vma(mm, addr); 4019 if (!vma || vma->vm_start > addr) 4020 break; 4021 if (vma->vm_ops && vma->vm_ops->access) 4022 ret = vma->vm_ops->access(vma, addr, buf, 4023 len, write); 4024 if (ret <= 0) 4025 break; 4026 bytes = ret; 4027 #endif 4028 } else { 4029 bytes = len; 4030 offset = addr & (PAGE_SIZE-1); 4031 if (bytes > PAGE_SIZE-offset) 4032 bytes = PAGE_SIZE-offset; 4033 4034 maddr = kmap(page); 4035 if (write) { 4036 copy_to_user_page(vma, page, addr, 4037 maddr + offset, buf, bytes); 4038 set_page_dirty_lock(page); 4039 } else { 4040 copy_from_user_page(vma, page, addr, 4041 buf, maddr + offset, bytes); 4042 } 4043 kunmap(page); 4044 put_page(page); 4045 } 4046 len -= bytes; 4047 buf += bytes; 4048 addr += bytes; 4049 } 4050 up_read(&mm->mmap_sem); 4051 4052 return buf - old_buf; 4053 } 4054 4055 /** 4056 * access_remote_vm - access another process' address space 4057 * @mm: the mm_struct of the target address space 4058 * @addr: start address to access 4059 * @buf: source or destination buffer 4060 * @len: number of bytes to transfer 4061 * @gup_flags: flags modifying lookup behaviour 4062 * 4063 * The caller must hold a reference on @mm. 4064 */ 4065 int access_remote_vm(struct mm_struct *mm, unsigned long addr, 4066 void *buf, int len, unsigned int gup_flags) 4067 { 4068 return __access_remote_vm(NULL, mm, addr, buf, len, gup_flags); 4069 } 4070 4071 /* 4072 * Access another process' address space. 4073 * Source/target buffer must be kernel space, 4074 * Do not walk the page table directly, use get_user_pages 4075 */ 4076 int access_process_vm(struct task_struct *tsk, unsigned long addr, 4077 void *buf, int len, unsigned int gup_flags) 4078 { 4079 struct mm_struct *mm; 4080 int ret; 4081 4082 mm = get_task_mm(tsk); 4083 if (!mm) 4084 return 0; 4085 4086 ret = __access_remote_vm(tsk, mm, addr, buf, len, gup_flags); 4087 4088 mmput(mm); 4089 4090 return ret; 4091 } 4092 EXPORT_SYMBOL_GPL(access_process_vm); 4093 4094 /* 4095 * Print the name of a VMA. 4096 */ 4097 void print_vma_addr(char *prefix, unsigned long ip) 4098 { 4099 struct mm_struct *mm = current->mm; 4100 struct vm_area_struct *vma; 4101 4102 /* 4103 * Do not print if we are in atomic 4104 * contexts (in exception stacks, etc.): 4105 */ 4106 if (preempt_count()) 4107 return; 4108 4109 down_read(&mm->mmap_sem); 4110 vma = find_vma(mm, ip); 4111 if (vma && vma->vm_file) { 4112 struct file *f = vma->vm_file; 4113 char *buf = (char *)__get_free_page(GFP_KERNEL); 4114 if (buf) { 4115 char *p; 4116 4117 p = file_path(f, buf, PAGE_SIZE); 4118 if (IS_ERR(p)) 4119 p = "?"; 4120 printk("%s%s[%lx+%lx]", prefix, kbasename(p), 4121 vma->vm_start, 4122 vma->vm_end - vma->vm_start); 4123 free_page((unsigned long)buf); 4124 } 4125 } 4126 up_read(&mm->mmap_sem); 4127 } 4128 4129 #if defined(CONFIG_PROVE_LOCKING) || defined(CONFIG_DEBUG_ATOMIC_SLEEP) 4130 void __might_fault(const char *file, int line) 4131 { 4132 /* 4133 * Some code (nfs/sunrpc) uses socket ops on kernel memory while 4134 * holding the mmap_sem, this is safe because kernel memory doesn't 4135 * get paged out, therefore we'll never actually fault, and the 4136 * below annotations will generate false positives. 4137 */ 4138 if (segment_eq(get_fs(), KERNEL_DS)) 4139 return; 4140 if (pagefault_disabled()) 4141 return; 4142 __might_sleep(file, line, 0); 4143 #if defined(CONFIG_DEBUG_ATOMIC_SLEEP) 4144 if (current->mm) 4145 might_lock_read(¤t->mm->mmap_sem); 4146 #endif 4147 } 4148 EXPORT_SYMBOL(__might_fault); 4149 #endif 4150 4151 #if defined(CONFIG_TRANSPARENT_HUGEPAGE) || defined(CONFIG_HUGETLBFS) 4152 static void clear_gigantic_page(struct page *page, 4153 unsigned long addr, 4154 unsigned int pages_per_huge_page) 4155 { 4156 int i; 4157 struct page *p = page; 4158 4159 might_sleep(); 4160 for (i = 0; i < pages_per_huge_page; 4161 i++, p = mem_map_next(p, page, i)) { 4162 cond_resched(); 4163 clear_user_highpage(p, addr + i * PAGE_SIZE); 4164 } 4165 } 4166 void clear_huge_page(struct page *page, 4167 unsigned long addr, unsigned int pages_per_huge_page) 4168 { 4169 int i; 4170 4171 if (unlikely(pages_per_huge_page > MAX_ORDER_NR_PAGES)) { 4172 clear_gigantic_page(page, addr, pages_per_huge_page); 4173 return; 4174 } 4175 4176 might_sleep(); 4177 for (i = 0; i < pages_per_huge_page; i++) { 4178 cond_resched(); 4179 clear_user_highpage(page + i, addr + i * PAGE_SIZE); 4180 } 4181 } 4182 4183 static void copy_user_gigantic_page(struct page *dst, struct page *src, 4184 unsigned long addr, 4185 struct vm_area_struct *vma, 4186 unsigned int pages_per_huge_page) 4187 { 4188 int i; 4189 struct page *dst_base = dst; 4190 struct page *src_base = src; 4191 4192 for (i = 0; i < pages_per_huge_page; ) { 4193 cond_resched(); 4194 copy_user_highpage(dst, src, addr + i*PAGE_SIZE, vma); 4195 4196 i++; 4197 dst = mem_map_next(dst, dst_base, i); 4198 src = mem_map_next(src, src_base, i); 4199 } 4200 } 4201 4202 void copy_user_huge_page(struct page *dst, struct page *src, 4203 unsigned long addr, struct vm_area_struct *vma, 4204 unsigned int pages_per_huge_page) 4205 { 4206 int i; 4207 4208 if (unlikely(pages_per_huge_page > MAX_ORDER_NR_PAGES)) { 4209 copy_user_gigantic_page(dst, src, addr, vma, 4210 pages_per_huge_page); 4211 return; 4212 } 4213 4214 might_sleep(); 4215 for (i = 0; i < pages_per_huge_page; i++) { 4216 cond_resched(); 4217 copy_user_highpage(dst + i, src + i, addr + i*PAGE_SIZE, vma); 4218 } 4219 } 4220 4221 long copy_huge_page_from_user(struct page *dst_page, 4222 const void __user *usr_src, 4223 unsigned int pages_per_huge_page, 4224 bool allow_pagefault) 4225 { 4226 void *src = (void *)usr_src; 4227 void *page_kaddr; 4228 unsigned long i, rc = 0; 4229 unsigned long ret_val = pages_per_huge_page * PAGE_SIZE; 4230 4231 for (i = 0; i < pages_per_huge_page; i++) { 4232 if (allow_pagefault) 4233 page_kaddr = kmap(dst_page + i); 4234 else 4235 page_kaddr = kmap_atomic(dst_page + i); 4236 rc = copy_from_user(page_kaddr, 4237 (const void __user *)(src + i * PAGE_SIZE), 4238 PAGE_SIZE); 4239 if (allow_pagefault) 4240 kunmap(dst_page + i); 4241 else 4242 kunmap_atomic(page_kaddr); 4243 4244 ret_val -= (PAGE_SIZE - rc); 4245 if (rc) 4246 break; 4247 4248 cond_resched(); 4249 } 4250 return ret_val; 4251 } 4252 #endif /* CONFIG_TRANSPARENT_HUGEPAGE || CONFIG_HUGETLBFS */ 4253 4254 #if USE_SPLIT_PTE_PTLOCKS && ALLOC_SPLIT_PTLOCKS 4255 4256 static struct kmem_cache *page_ptl_cachep; 4257 4258 void __init ptlock_cache_init(void) 4259 { 4260 page_ptl_cachep = kmem_cache_create("page->ptl", sizeof(spinlock_t), 0, 4261 SLAB_PANIC, NULL); 4262 } 4263 4264 bool ptlock_alloc(struct page *page) 4265 { 4266 spinlock_t *ptl; 4267 4268 ptl = kmem_cache_alloc(page_ptl_cachep, GFP_KERNEL); 4269 if (!ptl) 4270 return false; 4271 page->ptl = ptl; 4272 return true; 4273 } 4274 4275 void ptlock_free(struct page *page) 4276 { 4277 kmem_cache_free(page_ptl_cachep, page->ptl); 4278 } 4279 #endif 4280