1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * Copyright (C) 2008, 2009 Intel Corporation 4 * Authors: Andi Kleen, Fengguang Wu 5 * 6 * High level machine check handler. Handles pages reported by the 7 * hardware as being corrupted usually due to a multi-bit ECC memory or cache 8 * failure. 9 * 10 * In addition there is a "soft offline" entry point that allows stop using 11 * not-yet-corrupted-by-suspicious pages without killing anything. 12 * 13 * Handles page cache pages in various states. The tricky part 14 * here is that we can access any page asynchronously in respect to 15 * other VM users, because memory failures could happen anytime and 16 * anywhere. This could violate some of their assumptions. This is why 17 * this code has to be extremely careful. Generally it tries to use 18 * normal locking rules, as in get the standard locks, even if that means 19 * the error handling takes potentially a long time. 20 * 21 * It can be very tempting to add handling for obscure cases here. 22 * In general any code for handling new cases should only be added iff: 23 * - You know how to test it. 24 * - You have a test that can be added to mce-test 25 * https://git.kernel.org/cgit/utils/cpu/mce/mce-test.git/ 26 * - The case actually shows up as a frequent (top 10) page state in 27 * tools/mm/page-types when running a real workload. 28 * 29 * There are several operations here with exponential complexity because 30 * of unsuitable VM data structures. For example the operation to map back 31 * from RMAP chains to processes has to walk the complete process list and 32 * has non linear complexity with the number. But since memory corruptions 33 * are rare we hope to get away with this. This avoids impacting the core 34 * VM. 35 */ 36 37 #define pr_fmt(fmt) "Memory failure: " fmt 38 39 #include <linux/kernel.h> 40 #include <linux/mm.h> 41 #include <linux/memory-failure.h> 42 #include <linux/page-flags.h> 43 #include <linux/sched/signal.h> 44 #include <linux/sched/task.h> 45 #include <linux/dax.h> 46 #include <linux/ksm.h> 47 #include <linux/rmap.h> 48 #include <linux/export.h> 49 #include <linux/pagemap.h> 50 #include <linux/swap.h> 51 #include <linux/backing-dev.h> 52 #include <linux/migrate.h> 53 #include <linux/slab.h> 54 #include <linux/swapops.h> 55 #include <linux/hugetlb.h> 56 #include <linux/memory_hotplug.h> 57 #include <linux/mm_inline.h> 58 #include <linux/memremap.h> 59 #include <linux/kfifo.h> 60 #include <linux/ratelimit.h> 61 #include <linux/pagewalk.h> 62 #include <linux/shmem_fs.h> 63 #include <linux/sysctl.h> 64 #include "swap.h" 65 #include "internal.h" 66 #include "ras/ras_event.h" 67 68 static int sysctl_memory_failure_early_kill __read_mostly; 69 70 static int sysctl_memory_failure_recovery __read_mostly = 1; 71 72 static int sysctl_enable_soft_offline __read_mostly = 1; 73 74 atomic_long_t num_poisoned_pages __read_mostly = ATOMIC_LONG_INIT(0); 75 76 static bool hw_memory_failure __read_mostly = false; 77 78 static DEFINE_MUTEX(mf_mutex); 79 80 void num_poisoned_pages_inc(unsigned long pfn) 81 { 82 atomic_long_inc(&num_poisoned_pages); 83 memblk_nr_poison_inc(pfn); 84 } 85 86 void num_poisoned_pages_sub(unsigned long pfn, long i) 87 { 88 atomic_long_sub(i, &num_poisoned_pages); 89 if (pfn != -1UL) 90 memblk_nr_poison_sub(pfn, i); 91 } 92 93 /** 94 * MF_ATTR_RO - Create sysfs entry for each memory failure statistics. 95 * @_name: name of the file in the per NUMA sysfs directory. 96 */ 97 #define MF_ATTR_RO(_name) \ 98 static ssize_t _name##_show(struct device *dev, \ 99 struct device_attribute *attr, \ 100 char *buf) \ 101 { \ 102 struct memory_failure_stats *mf_stats = \ 103 &NODE_DATA(dev->id)->mf_stats; \ 104 return sysfs_emit(buf, "%lu\n", mf_stats->_name); \ 105 } \ 106 static DEVICE_ATTR_RO(_name) 107 108 MF_ATTR_RO(total); 109 MF_ATTR_RO(ignored); 110 MF_ATTR_RO(failed); 111 MF_ATTR_RO(delayed); 112 MF_ATTR_RO(recovered); 113 114 static struct attribute *memory_failure_attr[] = { 115 &dev_attr_total.attr, 116 &dev_attr_ignored.attr, 117 &dev_attr_failed.attr, 118 &dev_attr_delayed.attr, 119 &dev_attr_recovered.attr, 120 NULL, 121 }; 122 123 const struct attribute_group memory_failure_attr_group = { 124 .name = "memory_failure", 125 .attrs = memory_failure_attr, 126 }; 127 128 static const struct ctl_table memory_failure_table[] = { 129 { 130 .procname = "memory_failure_early_kill", 131 .data = &sysctl_memory_failure_early_kill, 132 .maxlen = sizeof(sysctl_memory_failure_early_kill), 133 .mode = 0644, 134 .proc_handler = proc_dointvec_minmax, 135 .extra1 = SYSCTL_ZERO, 136 .extra2 = SYSCTL_ONE, 137 }, 138 { 139 .procname = "memory_failure_recovery", 140 .data = &sysctl_memory_failure_recovery, 141 .maxlen = sizeof(sysctl_memory_failure_recovery), 142 .mode = 0644, 143 .proc_handler = proc_dointvec_minmax, 144 .extra1 = SYSCTL_ZERO, 145 .extra2 = SYSCTL_ONE, 146 }, 147 { 148 .procname = "enable_soft_offline", 149 .data = &sysctl_enable_soft_offline, 150 .maxlen = sizeof(sysctl_enable_soft_offline), 151 .mode = 0644, 152 .proc_handler = proc_dointvec_minmax, 153 .extra1 = SYSCTL_ZERO, 154 .extra2 = SYSCTL_ONE, 155 } 156 }; 157 158 static struct rb_root_cached pfn_space_itree = RB_ROOT_CACHED; 159 160 static DEFINE_MUTEX(pfn_space_lock); 161 162 /* 163 * Return values: 164 * 1: the page is dissolved (if needed) and taken off from buddy, 165 * 0: the page is dissolved (if needed) and not taken off from buddy, 166 * < 0: failed to dissolve. 167 */ 168 static int __page_handle_poison(struct page *page) 169 { 170 int ret; 171 172 /* 173 * zone_pcp_disable() can't be used here. It will 174 * hold pcp_batch_high_lock and dissolve_free_hugetlb_folio() might hold 175 * cpu_hotplug_lock via static_key_slow_dec() when hugetlb vmemmap 176 * optimization is enabled. This will break current lock dependency 177 * chain and leads to deadlock. 178 * Disabling pcp before dissolving the page was a deterministic 179 * approach because we made sure that those pages cannot end up in any 180 * PCP list. Draining PCP lists expels those pages to the buddy system, 181 * but nothing guarantees that those pages do not get back to a PCP 182 * queue if we need to refill those. 183 */ 184 ret = dissolve_free_hugetlb_folio(page_folio(page)); 185 if (!ret) { 186 drain_all_pages(page_zone(page)); 187 ret = take_page_off_buddy(page); 188 } 189 190 return ret; 191 } 192 193 static bool page_handle_poison(struct page *page, bool hugepage_or_freepage, bool release) 194 { 195 if (hugepage_or_freepage) { 196 /* 197 * Doing this check for free pages is also fine since 198 * dissolve_free_hugetlb_folio() returns 0 for non-hugetlb folios as well. 199 */ 200 if (__page_handle_poison(page) <= 0) 201 /* 202 * We could fail to take off the target page from buddy 203 * for example due to racy page allocation, but that's 204 * acceptable because soft-offlined page is not broken 205 * and if someone really want to use it, they should 206 * take it. 207 */ 208 return false; 209 } 210 211 SetPageHWPoison(page); 212 if (release) 213 put_page(page); 214 page_ref_inc(page); 215 num_poisoned_pages_inc(page_to_pfn(page)); 216 217 return true; 218 } 219 220 static hwpoison_filter_func_t __rcu *hwpoison_filter_func __read_mostly; 221 222 void hwpoison_filter_register(hwpoison_filter_func_t *filter) 223 { 224 rcu_assign_pointer(hwpoison_filter_func, filter); 225 } 226 EXPORT_SYMBOL_GPL(hwpoison_filter_register); 227 228 void hwpoison_filter_unregister(void) 229 { 230 RCU_INIT_POINTER(hwpoison_filter_func, NULL); 231 synchronize_rcu(); 232 } 233 EXPORT_SYMBOL_GPL(hwpoison_filter_unregister); 234 235 static int hwpoison_filter(struct page *p) 236 { 237 int ret = 0; 238 hwpoison_filter_func_t *filter; 239 240 rcu_read_lock(); 241 filter = rcu_dereference(hwpoison_filter_func); 242 if (filter) 243 ret = filter(p); 244 rcu_read_unlock(); 245 246 return ret; 247 } 248 249 /* 250 * Kill all processes that have a poisoned page mapped and then isolate 251 * the page. 252 * 253 * General strategy: 254 * Find all processes having the page mapped and kill them. 255 * But we keep a page reference around so that the page is not 256 * actually freed yet. 257 * Then stash the page away 258 * 259 * There's no convenient way to get back to mapped processes 260 * from the VMAs. So do a brute-force search over all 261 * running processes. 262 * 263 * Remember that machine checks are not common (or rather 264 * if they are common you have other problems), so this shouldn't 265 * be a performance issue. 266 * 267 * Also there are some races possible while we get from the 268 * error detection to actually handle it. 269 */ 270 271 struct to_kill { 272 struct list_head nd; 273 struct task_struct *tsk; 274 unsigned long addr; 275 short size_shift; 276 }; 277 278 /* 279 * Send all the processes who have the page mapped a signal. 280 * ``action optional'' if they are not immediately affected by the error 281 * ``action required'' if error happened in current execution context 282 */ 283 static int kill_proc(struct to_kill *tk, unsigned long pfn, int flags) 284 { 285 struct task_struct *t = tk->tsk; 286 short addr_lsb = tk->size_shift; 287 int ret = 0; 288 289 pr_err("%#lx: Sending SIGBUS to %s:%d due to hardware memory corruption\n", 290 pfn, t->comm, task_pid_nr(t)); 291 292 if ((flags & MF_ACTION_REQUIRED) && (t == current)) 293 ret = force_sig_mceerr(BUS_MCEERR_AR, 294 (void __user *)tk->addr, addr_lsb); 295 else 296 /* 297 * Signal other processes sharing the page if they have 298 * PF_MCE_EARLY set. 299 * Don't use force here, it's convenient if the signal 300 * can be temporarily blocked. 301 */ 302 ret = send_sig_mceerr(BUS_MCEERR_AO, (void __user *)tk->addr, 303 addr_lsb, t); 304 if (ret < 0) 305 pr_info("Error sending signal to %s:%d: %d\n", 306 t->comm, task_pid_nr(t), ret); 307 return ret; 308 } 309 310 /* 311 * Unknown page type encountered. Try to check whether it can turn PageLRU by 312 * lru_add_drain_all. 313 */ 314 void shake_folio(struct folio *folio) 315 { 316 if (folio_test_hugetlb(folio)) 317 return; 318 /* 319 * TODO: Could shrink slab caches here if a lightweight range-based 320 * shrinker will be available. 321 */ 322 if (folio_test_slab(folio)) 323 return; 324 325 lru_add_drain_all(); 326 } 327 EXPORT_SYMBOL_GPL(shake_folio); 328 329 static void shake_page(struct page *page) 330 { 331 shake_folio(page_folio(page)); 332 } 333 334 static unsigned long dev_pagemap_mapping_shift(struct vm_area_struct *vma, 335 unsigned long address) 336 { 337 unsigned long ret = 0; 338 pgd_t *pgd; 339 p4d_t *p4d; 340 pud_t *pud; 341 pmd_t *pmd; 342 pte_t *pte; 343 pte_t ptent; 344 345 VM_BUG_ON_VMA(address == -EFAULT, vma); 346 pgd = pgd_offset(vma->vm_mm, address); 347 if (!pgd_present(*pgd)) 348 return 0; 349 p4d = p4d_offset(pgd, address); 350 if (!p4d_present(*p4d)) 351 return 0; 352 pud = pud_offset(p4d, address); 353 if (!pud_present(*pud)) 354 return 0; 355 if (pud_trans_huge(*pud)) 356 return PUD_SHIFT; 357 pmd = pmd_offset(pud, address); 358 if (!pmd_present(*pmd)) 359 return 0; 360 if (pmd_trans_huge(*pmd)) 361 return PMD_SHIFT; 362 pte = pte_offset_map(pmd, address); 363 if (!pte) 364 return 0; 365 ptent = ptep_get(pte); 366 if (pte_present(ptent)) 367 ret = PAGE_SHIFT; 368 pte_unmap(pte); 369 return ret; 370 } 371 372 /* 373 * Failure handling: if we can't find or can't kill a process there's 374 * not much we can do. We just print a message and ignore otherwise. 375 */ 376 377 /* 378 * Schedule a process for later kill. 379 * Uses GFP_ATOMIC allocations to avoid potential recursions in the VM. 380 */ 381 static void __add_to_kill(struct task_struct *tsk, const struct page *p, 382 struct vm_area_struct *vma, struct list_head *to_kill, 383 unsigned long addr) 384 { 385 struct to_kill *tk; 386 387 tk = kmalloc(sizeof(struct to_kill), GFP_ATOMIC); 388 if (!tk) { 389 pr_err("Out of memory while machine check handling\n"); 390 return; 391 } 392 393 tk->addr = addr; 394 if (is_zone_device_page(p)) 395 tk->size_shift = dev_pagemap_mapping_shift(vma, tk->addr); 396 else 397 tk->size_shift = folio_shift(page_folio(p)); 398 399 /* 400 * Send SIGKILL if "tk->addr == -EFAULT". Also, as 401 * "tk->size_shift" is always non-zero for !is_zone_device_page(), 402 * so "tk->size_shift == 0" effectively checks no mapping on 403 * ZONE_DEVICE. Indeed, when a devdax page is mmapped N times 404 * to a process' address space, it's possible not all N VMAs 405 * contain mappings for the page, but at least one VMA does. 406 * Only deliver SIGBUS with payload derived from the VMA that 407 * has a mapping for the page. 408 */ 409 if (tk->addr == -EFAULT) { 410 pr_info("Unable to find user space address %lx in %s\n", 411 page_to_pfn(p), tsk->comm); 412 } else if (tk->size_shift == 0) { 413 kfree(tk); 414 return; 415 } 416 417 get_task_struct(tsk); 418 tk->tsk = tsk; 419 list_add_tail(&tk->nd, to_kill); 420 } 421 422 static void add_to_kill_anon_file(struct task_struct *tsk, const struct page *p, 423 struct vm_area_struct *vma, struct list_head *to_kill, 424 unsigned long addr) 425 { 426 if (addr == -EFAULT) 427 return; 428 __add_to_kill(tsk, p, vma, to_kill, addr); 429 } 430 431 #ifdef CONFIG_KSM 432 static bool task_in_to_kill_list(struct list_head *to_kill, 433 struct task_struct *tsk) 434 { 435 struct to_kill *tk, *next; 436 437 list_for_each_entry_safe(tk, next, to_kill, nd) { 438 if (tk->tsk == tsk) 439 return true; 440 } 441 442 return false; 443 } 444 445 void add_to_kill_ksm(struct task_struct *tsk, const struct page *p, 446 struct vm_area_struct *vma, struct list_head *to_kill, 447 unsigned long addr) 448 { 449 if (!task_in_to_kill_list(to_kill, tsk)) 450 __add_to_kill(tsk, p, vma, to_kill, addr); 451 } 452 #endif 453 /* 454 * Kill the processes that have been collected earlier. 455 * 456 * Only do anything when FORCEKILL is set, otherwise just free the 457 * list (this is used for clean pages which do not need killing) 458 */ 459 static void kill_procs(struct list_head *to_kill, int forcekill, 460 unsigned long pfn, int flags) 461 { 462 struct to_kill *tk, *next; 463 464 list_for_each_entry_safe(tk, next, to_kill, nd) { 465 if (forcekill) { 466 if (tk->addr == -EFAULT) { 467 pr_err("%#lx: forcibly killing %s:%d because of failure to unmap corrupted page\n", 468 pfn, tk->tsk->comm, task_pid_nr(tk->tsk)); 469 do_send_sig_info(SIGKILL, SEND_SIG_PRIV, 470 tk->tsk, PIDTYPE_PID); 471 } 472 473 /* 474 * In theory the process could have mapped 475 * something else on the address in-between. We could 476 * check for that, but we need to tell the 477 * process anyways. 478 */ 479 else if (kill_proc(tk, pfn, flags) < 0) 480 pr_err("%#lx: Cannot send advisory machine check signal to %s:%d\n", 481 pfn, tk->tsk->comm, task_pid_nr(tk->tsk)); 482 } 483 list_del(&tk->nd); 484 put_task_struct(tk->tsk); 485 kfree(tk); 486 } 487 } 488 489 /* 490 * Find a dedicated thread which is supposed to handle SIGBUS(BUS_MCEERR_AO) 491 * on behalf of the thread group. Return task_struct of the (first found) 492 * dedicated thread if found, and return NULL otherwise. 493 * 494 * We already hold rcu lock in the caller, so we don't have to call 495 * rcu_read_lock/unlock() in this function. 496 */ 497 static struct task_struct *find_early_kill_thread(struct task_struct *tsk) 498 { 499 struct task_struct *t; 500 501 for_each_thread(tsk, t) { 502 if (t->flags & PF_MCE_PROCESS) { 503 if (t->flags & PF_MCE_EARLY) 504 return t; 505 } else { 506 if (sysctl_memory_failure_early_kill) 507 return t; 508 } 509 } 510 return NULL; 511 } 512 513 /* 514 * Determine whether a given process is "early kill" process which expects 515 * to be signaled when some page under the process is hwpoisoned. 516 * Return task_struct of the dedicated thread (main thread unless explicitly 517 * specified) if the process is "early kill" and otherwise returns NULL. 518 * 519 * Note that the above is true for Action Optional case. For Action Required 520 * case, it's only meaningful to the current thread which need to be signaled 521 * with SIGBUS, this error is Action Optional for other non current 522 * processes sharing the same error page,if the process is "early kill", the 523 * task_struct of the dedicated thread will also be returned. 524 */ 525 struct task_struct *task_early_kill(struct task_struct *tsk, int force_early) 526 { 527 if (!tsk->mm) 528 return NULL; 529 /* 530 * Comparing ->mm here because current task might represent 531 * a subthread, while tsk always points to the main thread. 532 */ 533 if (force_early && tsk->mm == current->mm) 534 return current; 535 536 return find_early_kill_thread(tsk); 537 } 538 539 /* 540 * Collect processes when the error hit an anonymous page. 541 */ 542 static void collect_procs_anon(const struct folio *folio, 543 const struct page *page, struct list_head *to_kill, 544 int force_early) 545 { 546 struct task_struct *tsk; 547 struct anon_vma *av; 548 pgoff_t pgoff; 549 550 av = folio_lock_anon_vma_read(folio, NULL); 551 if (av == NULL) /* Not actually mapped anymore */ 552 return; 553 554 pgoff = page_pgoff(folio, page); 555 rcu_read_lock(); 556 for_each_process(tsk) { 557 struct vm_area_struct *vma; 558 struct anon_vma_chain *vmac; 559 struct task_struct *t = task_early_kill(tsk, force_early); 560 unsigned long addr; 561 562 if (!t) 563 continue; 564 anon_vma_interval_tree_foreach(vmac, &av->rb_root, 565 pgoff, pgoff) { 566 vma = vmac->vma; 567 if (vma->vm_mm != t->mm) 568 continue; 569 addr = page_mapped_in_vma(page, vma); 570 add_to_kill_anon_file(t, page, vma, to_kill, addr); 571 } 572 } 573 rcu_read_unlock(); 574 anon_vma_unlock_read(av); 575 } 576 577 /* 578 * Collect processes when the error hit a file mapped page. 579 */ 580 static void collect_procs_file(const struct folio *folio, 581 const struct page *page, struct list_head *to_kill, 582 int force_early) 583 { 584 struct vm_area_struct *vma; 585 struct task_struct *tsk; 586 struct address_space *mapping = folio->mapping; 587 pgoff_t pgoff; 588 589 i_mmap_lock_read(mapping); 590 rcu_read_lock(); 591 pgoff = page_pgoff(folio, page); 592 for_each_process(tsk) { 593 struct task_struct *t = task_early_kill(tsk, force_early); 594 unsigned long addr; 595 596 if (!t) 597 continue; 598 vma_interval_tree_foreach(vma, &mapping->i_mmap, pgoff, 599 pgoff) { 600 /* 601 * Send early kill signal to tasks where a vma covers 602 * the page but the corrupted page is not necessarily 603 * mapped in its pte. 604 * Assume applications who requested early kill want 605 * to be informed of all such data corruptions. 606 */ 607 if (vma->vm_mm != t->mm) 608 continue; 609 addr = page_address_in_vma(folio, page, vma); 610 add_to_kill_anon_file(t, page, vma, to_kill, addr); 611 } 612 } 613 rcu_read_unlock(); 614 i_mmap_unlock_read(mapping); 615 } 616 617 #ifdef CONFIG_FS_DAX 618 static void add_to_kill_fsdax(struct task_struct *tsk, const struct page *p, 619 struct vm_area_struct *vma, 620 struct list_head *to_kill, pgoff_t pgoff) 621 { 622 unsigned long addr = vma_address(vma, pgoff, 1); 623 __add_to_kill(tsk, p, vma, to_kill, addr); 624 } 625 626 /* 627 * Collect processes when the error hit a fsdax page. 628 */ 629 static void collect_procs_fsdax(const struct page *page, 630 struct address_space *mapping, pgoff_t pgoff, 631 struct list_head *to_kill, bool pre_remove) 632 { 633 struct vm_area_struct *vma; 634 struct task_struct *tsk; 635 636 i_mmap_lock_read(mapping); 637 rcu_read_lock(); 638 for_each_process(tsk) { 639 struct task_struct *t = tsk; 640 641 /* 642 * Search for all tasks while MF_MEM_PRE_REMOVE is set, because 643 * the current may not be the one accessing the fsdax page. 644 * Otherwise, search for the current task. 645 */ 646 if (!pre_remove) 647 t = task_early_kill(tsk, true); 648 if (!t) 649 continue; 650 vma_interval_tree_foreach(vma, &mapping->i_mmap, pgoff, pgoff) { 651 if (vma->vm_mm == t->mm) 652 add_to_kill_fsdax(t, page, vma, to_kill, pgoff); 653 } 654 } 655 rcu_read_unlock(); 656 i_mmap_unlock_read(mapping); 657 } 658 #endif /* CONFIG_FS_DAX */ 659 660 /* 661 * Collect the processes who have the corrupted page mapped to kill. 662 */ 663 static void collect_procs(const struct folio *folio, const struct page *page, 664 struct list_head *tokill, int force_early) 665 { 666 if (!folio->mapping) 667 return; 668 if (unlikely(folio_test_ksm(folio))) 669 collect_procs_ksm(folio, page, tokill, force_early); 670 else if (folio_test_anon(folio)) 671 collect_procs_anon(folio, page, tokill, force_early); 672 else 673 collect_procs_file(folio, page, tokill, force_early); 674 } 675 676 struct hwpoison_walk { 677 struct to_kill tk; 678 unsigned long pfn; 679 int flags; 680 }; 681 682 static void set_to_kill(struct to_kill *tk, unsigned long addr, short shift) 683 { 684 tk->addr = addr; 685 tk->size_shift = shift; 686 } 687 688 static int check_hwpoisoned_entry(pte_t pte, unsigned long addr, short shift, 689 unsigned long poisoned_pfn, struct to_kill *tk) 690 { 691 unsigned long pfn = 0; 692 693 if (pte_present(pte)) { 694 pfn = pte_pfn(pte); 695 } else { 696 swp_entry_t swp = pte_to_swp_entry(pte); 697 698 if (is_hwpoison_entry(swp)) 699 pfn = swp_offset_pfn(swp); 700 } 701 702 if (!pfn || pfn != poisoned_pfn) 703 return 0; 704 705 set_to_kill(tk, addr, shift); 706 return 1; 707 } 708 709 #ifdef CONFIG_TRANSPARENT_HUGEPAGE 710 static int check_hwpoisoned_pmd_entry(pmd_t *pmdp, unsigned long addr, 711 struct hwpoison_walk *hwp) 712 { 713 pmd_t pmd = *pmdp; 714 unsigned long pfn; 715 unsigned long hwpoison_vaddr; 716 717 if (!pmd_present(pmd)) 718 return 0; 719 pfn = pmd_pfn(pmd); 720 if (pfn <= hwp->pfn && hwp->pfn < pfn + HPAGE_PMD_NR) { 721 hwpoison_vaddr = addr + ((hwp->pfn - pfn) << PAGE_SHIFT); 722 set_to_kill(&hwp->tk, hwpoison_vaddr, PAGE_SHIFT); 723 return 1; 724 } 725 return 0; 726 } 727 #else 728 static int check_hwpoisoned_pmd_entry(pmd_t *pmdp, unsigned long addr, 729 struct hwpoison_walk *hwp) 730 { 731 return 0; 732 } 733 #endif 734 735 static int hwpoison_pte_range(pmd_t *pmdp, unsigned long addr, 736 unsigned long end, struct mm_walk *walk) 737 { 738 struct hwpoison_walk *hwp = walk->private; 739 int ret = 0; 740 pte_t *ptep, *mapped_pte; 741 spinlock_t *ptl; 742 743 ptl = pmd_trans_huge_lock(pmdp, walk->vma); 744 if (ptl) { 745 ret = check_hwpoisoned_pmd_entry(pmdp, addr, hwp); 746 spin_unlock(ptl); 747 goto out; 748 } 749 750 mapped_pte = ptep = pte_offset_map_lock(walk->vma->vm_mm, pmdp, 751 addr, &ptl); 752 if (!ptep) 753 goto out; 754 755 for (; addr != end; ptep++, addr += PAGE_SIZE) { 756 ret = check_hwpoisoned_entry(ptep_get(ptep), addr, PAGE_SHIFT, 757 hwp->pfn, &hwp->tk); 758 if (ret == 1) 759 break; 760 } 761 pte_unmap_unlock(mapped_pte, ptl); 762 out: 763 cond_resched(); 764 return ret; 765 } 766 767 #ifdef CONFIG_HUGETLB_PAGE 768 static int hwpoison_hugetlb_range(pte_t *ptep, unsigned long hmask, 769 unsigned long addr, unsigned long end, 770 struct mm_walk *walk) 771 { 772 struct hwpoison_walk *hwp = walk->private; 773 struct hstate *h = hstate_vma(walk->vma); 774 spinlock_t *ptl; 775 pte_t pte; 776 int ret; 777 778 ptl = huge_pte_lock(h, walk->mm, ptep); 779 pte = huge_ptep_get(walk->mm, addr, ptep); 780 ret = check_hwpoisoned_entry(pte, addr, huge_page_shift(h), 781 hwp->pfn, &hwp->tk); 782 spin_unlock(ptl); 783 return ret; 784 } 785 #else 786 #define hwpoison_hugetlb_range NULL 787 #endif 788 789 static int hwpoison_test_walk(unsigned long start, unsigned long end, 790 struct mm_walk *walk) 791 { 792 /* We also want to consider pages mapped into VM_PFNMAP. */ 793 return 0; 794 } 795 796 static const struct mm_walk_ops hwpoison_walk_ops = { 797 .pmd_entry = hwpoison_pte_range, 798 .hugetlb_entry = hwpoison_hugetlb_range, 799 .test_walk = hwpoison_test_walk, 800 .walk_lock = PGWALK_RDLOCK, 801 }; 802 803 /* 804 * Sends SIGBUS to the current process with error info. 805 * 806 * This function is intended to handle "Action Required" MCEs on already 807 * hardware poisoned pages. They could happen, for example, when 808 * memory_failure() failed to unmap the error page at the first call, or 809 * when multiple local machine checks happened on different CPUs. 810 * 811 * MCE handler currently has no easy access to the error virtual address, 812 * so this function walks page table to find it. The returned virtual address 813 * is proper in most cases, but it could be wrong when the application 814 * process has multiple entries mapping the error page. 815 */ 816 static int kill_accessing_process(struct task_struct *p, unsigned long pfn, 817 int flags) 818 { 819 int ret; 820 struct hwpoison_walk priv = { 821 .pfn = pfn, 822 }; 823 priv.tk.tsk = p; 824 825 if (!p->mm) 826 return -EFAULT; 827 828 mmap_read_lock(p->mm); 829 ret = walk_page_range(p->mm, 0, TASK_SIZE, &hwpoison_walk_ops, 830 (void *)&priv); 831 /* 832 * ret = 1 when CMCI wins, regardless of whether try_to_unmap() 833 * succeeds or fails, then kill the process with SIGBUS. 834 * ret = 0 when poison page is a clean page and it's dropped, no 835 * SIGBUS is needed. 836 */ 837 if (ret == 1 && priv.tk.addr) 838 kill_proc(&priv.tk, pfn, flags); 839 mmap_read_unlock(p->mm); 840 841 return ret > 0 ? -EHWPOISON : 0; 842 } 843 844 /* 845 * MF_IGNORED - The m-f() handler marks the page as PG_hwpoisoned'ed. 846 * But it could not do more to isolate the page from being accessed again, 847 * nor does it kill the process. This is extremely rare and one of the 848 * potential causes is that the page state has been changed due to 849 * underlying race condition. This is the most severe outcomes. 850 * 851 * MF_FAILED - The m-f() handler marks the page as PG_hwpoisoned'ed. 852 * It should have killed the process, but it can't isolate the page, 853 * due to conditions such as extra pin, unmap failure, etc. Accessing 854 * the page again may trigger another MCE and the process will be killed 855 * by the m-f() handler immediately. 856 * 857 * MF_DELAYED - The m-f() handler marks the page as PG_hwpoisoned'ed. 858 * The page is unmapped, and is removed from the LRU or file mapping. 859 * An attempt to access the page again will trigger page fault and the 860 * PF handler will kill the process. 861 * 862 * MF_RECOVERED - The m-f() handler marks the page as PG_hwpoisoned'ed. 863 * The page has been completely isolated, that is, unmapped, taken out of 864 * the buddy system, or hole-punnched out of the file mapping. 865 */ 866 static const char *action_name[] = { 867 [MF_IGNORED] = "Ignored", 868 [MF_FAILED] = "Failed", 869 [MF_DELAYED] = "Delayed", 870 [MF_RECOVERED] = "Recovered", 871 }; 872 873 static const char * const action_page_types[] = { 874 [MF_MSG_KERNEL] = "reserved kernel page", 875 [MF_MSG_KERNEL_HIGH_ORDER] = "high-order kernel page", 876 [MF_MSG_HUGE] = "huge page", 877 [MF_MSG_FREE_HUGE] = "free huge page", 878 [MF_MSG_GET_HWPOISON] = "get hwpoison page", 879 [MF_MSG_UNMAP_FAILED] = "unmapping failed page", 880 [MF_MSG_DIRTY_SWAPCACHE] = "dirty swapcache page", 881 [MF_MSG_CLEAN_SWAPCACHE] = "clean swapcache page", 882 [MF_MSG_DIRTY_MLOCKED_LRU] = "dirty mlocked LRU page", 883 [MF_MSG_CLEAN_MLOCKED_LRU] = "clean mlocked LRU page", 884 [MF_MSG_DIRTY_UNEVICTABLE_LRU] = "dirty unevictable LRU page", 885 [MF_MSG_CLEAN_UNEVICTABLE_LRU] = "clean unevictable LRU page", 886 [MF_MSG_DIRTY_LRU] = "dirty LRU page", 887 [MF_MSG_CLEAN_LRU] = "clean LRU page", 888 [MF_MSG_TRUNCATED_LRU] = "already truncated LRU page", 889 [MF_MSG_BUDDY] = "free buddy page", 890 [MF_MSG_DAX] = "dax page", 891 [MF_MSG_UNSPLIT_THP] = "unsplit thp", 892 [MF_MSG_ALREADY_POISONED] = "already poisoned page", 893 [MF_MSG_PFN_MAP] = "non struct page pfn", 894 [MF_MSG_UNKNOWN] = "unknown page", 895 }; 896 897 /* 898 * XXX: It is possible that a page is isolated from LRU cache, 899 * and then kept in swap cache or failed to remove from page cache. 900 * The page count will stop it from being freed by unpoison. 901 * Stress tests should be aware of this memory leak problem. 902 */ 903 static int delete_from_lru_cache(struct folio *folio) 904 { 905 if (folio_isolate_lru(folio)) { 906 /* 907 * Clear sensible page flags, so that the buddy system won't 908 * complain when the folio is unpoison-and-freed. 909 */ 910 folio_clear_active(folio); 911 folio_clear_unevictable(folio); 912 913 /* 914 * Poisoned page might never drop its ref count to 0 so we have 915 * to uncharge it manually from its memcg. 916 */ 917 mem_cgroup_uncharge(folio); 918 919 /* 920 * drop the refcount elevated by folio_isolate_lru() 921 */ 922 folio_put(folio); 923 return 0; 924 } 925 return -EIO; 926 } 927 928 static int truncate_error_folio(struct folio *folio, unsigned long pfn, 929 struct address_space *mapping) 930 { 931 int ret = MF_FAILED; 932 933 if (mapping->a_ops->error_remove_folio) { 934 int err = mapping->a_ops->error_remove_folio(mapping, folio); 935 936 if (err != 0) 937 pr_info("%#lx: Failed to punch page: %d\n", pfn, err); 938 else if (!filemap_release_folio(folio, GFP_NOIO)) 939 pr_info("%#lx: failed to release buffers\n", pfn); 940 else 941 ret = MF_RECOVERED; 942 } else { 943 /* 944 * If the file system doesn't support it just invalidate 945 * This fails on dirty or anything with private pages 946 */ 947 if (mapping_evict_folio(mapping, folio)) 948 ret = MF_RECOVERED; 949 else 950 pr_info("%#lx: Failed to invalidate\n", pfn); 951 } 952 953 return ret; 954 } 955 956 struct page_state { 957 unsigned long mask; 958 unsigned long res; 959 enum mf_action_page_type type; 960 961 /* Callback ->action() has to unlock the relevant page inside it. */ 962 int (*action)(struct page_state *ps, struct page *p); 963 }; 964 965 /* 966 * Return true if page is still referenced by others, otherwise return 967 * false. 968 * 969 * The extra_pins is true when one extra refcount is expected. 970 */ 971 static bool has_extra_refcount(struct page_state *ps, struct page *p, 972 bool extra_pins) 973 { 974 int count = page_count(p) - 1; 975 976 if (extra_pins) 977 count -= folio_nr_pages(page_folio(p)); 978 979 if (count > 0) { 980 pr_err("%#lx: %s still referenced by %d users\n", 981 page_to_pfn(p), action_page_types[ps->type], count); 982 return true; 983 } 984 985 return false; 986 } 987 988 /* 989 * Error hit kernel page. 990 * Do nothing, try to be lucky and not touch this instead. For a few cases we 991 * could be more sophisticated. 992 */ 993 static int me_kernel(struct page_state *ps, struct page *p) 994 { 995 unlock_page(p); 996 return MF_IGNORED; 997 } 998 999 /* 1000 * Page in unknown state. Do nothing. 1001 * This is a catch-all in case we fail to make sense of the page state. 1002 */ 1003 static int me_unknown(struct page_state *ps, struct page *p) 1004 { 1005 pr_err("%#lx: Unknown page state\n", page_to_pfn(p)); 1006 unlock_page(p); 1007 return MF_IGNORED; 1008 } 1009 1010 /* 1011 * Clean (or cleaned) page cache page. 1012 */ 1013 static int me_pagecache_clean(struct page_state *ps, struct page *p) 1014 { 1015 struct folio *folio = page_folio(p); 1016 int ret; 1017 struct address_space *mapping; 1018 bool extra_pins; 1019 1020 delete_from_lru_cache(folio); 1021 1022 /* 1023 * For anonymous folios the only reference left 1024 * should be the one m_f() holds. 1025 */ 1026 if (folio_test_anon(folio)) { 1027 ret = MF_RECOVERED; 1028 goto out; 1029 } 1030 1031 /* 1032 * Now truncate the page in the page cache. This is really 1033 * more like a "temporary hole punch" 1034 * Don't do this for block devices when someone else 1035 * has a reference, because it could be file system metadata 1036 * and that's not safe to truncate. 1037 */ 1038 mapping = folio_mapping(folio); 1039 if (!mapping) { 1040 /* Folio has been torn down in the meantime */ 1041 ret = MF_FAILED; 1042 goto out; 1043 } 1044 1045 /* 1046 * The shmem page is kept in page cache instead of truncating 1047 * so is expected to have an extra refcount after error-handling. 1048 */ 1049 extra_pins = shmem_mapping(mapping); 1050 1051 /* 1052 * Truncation is a bit tricky. Enable it per file system for now. 1053 * 1054 * Open: to take i_rwsem or not for this? Right now we don't. 1055 */ 1056 ret = truncate_error_folio(folio, page_to_pfn(p), mapping); 1057 if (has_extra_refcount(ps, p, extra_pins)) 1058 ret = MF_FAILED; 1059 1060 out: 1061 folio_unlock(folio); 1062 1063 return ret; 1064 } 1065 1066 /* 1067 * Dirty pagecache page 1068 * Issues: when the error hit a hole page the error is not properly 1069 * propagated. 1070 */ 1071 static int me_pagecache_dirty(struct page_state *ps, struct page *p) 1072 { 1073 struct folio *folio = page_folio(p); 1074 struct address_space *mapping = folio_mapping(folio); 1075 1076 /* TBD: print more information about the file. */ 1077 if (mapping) { 1078 /* 1079 * IO error will be reported by write(), fsync(), etc. 1080 * who check the mapping. 1081 * This way the application knows that something went 1082 * wrong with its dirty file data. 1083 */ 1084 mapping_set_error(mapping, -EIO); 1085 } 1086 1087 return me_pagecache_clean(ps, p); 1088 } 1089 1090 /* 1091 * Clean and dirty swap cache. 1092 * 1093 * Dirty swap cache page is tricky to handle. The page could live both in page 1094 * table and swap cache(ie. page is freshly swapped in). So it could be 1095 * referenced concurrently by 2 types of PTEs: 1096 * normal PTEs and swap PTEs. We try to handle them consistently by calling 1097 * try_to_unmap(!TTU_HWPOISON) to convert the normal PTEs to swap PTEs, 1098 * and then 1099 * - clear dirty bit to prevent IO 1100 * - remove from LRU 1101 * - but keep in the swap cache, so that when we return to it on 1102 * a later page fault, we know the application is accessing 1103 * corrupted data and shall be killed (we installed simple 1104 * interception code in do_swap_page to catch it). 1105 * 1106 * Clean swap cache pages can be directly isolated. A later page fault will 1107 * bring in the known good data from disk. 1108 */ 1109 static int me_swapcache_dirty(struct page_state *ps, struct page *p) 1110 { 1111 struct folio *folio = page_folio(p); 1112 int ret; 1113 bool extra_pins = false; 1114 1115 folio_clear_dirty(folio); 1116 /* Trigger EIO in shmem: */ 1117 folio_clear_uptodate(folio); 1118 1119 ret = delete_from_lru_cache(folio) ? MF_FAILED : MF_DELAYED; 1120 folio_unlock(folio); 1121 1122 if (ret == MF_DELAYED) 1123 extra_pins = true; 1124 1125 if (has_extra_refcount(ps, p, extra_pins)) 1126 ret = MF_FAILED; 1127 1128 return ret; 1129 } 1130 1131 static int me_swapcache_clean(struct page_state *ps, struct page *p) 1132 { 1133 struct folio *folio = page_folio(p); 1134 int ret; 1135 1136 swap_cache_del_folio(folio); 1137 1138 ret = delete_from_lru_cache(folio) ? MF_FAILED : MF_RECOVERED; 1139 folio_unlock(folio); 1140 1141 if (has_extra_refcount(ps, p, false)) 1142 ret = MF_FAILED; 1143 1144 return ret; 1145 } 1146 1147 /* 1148 * Huge pages. Needs work. 1149 * Issues: 1150 * - Error on hugepage is contained in hugepage unit (not in raw page unit.) 1151 * To narrow down kill region to one page, we need to break up pmd. 1152 */ 1153 static int me_huge_page(struct page_state *ps, struct page *p) 1154 { 1155 struct folio *folio = page_folio(p); 1156 int res; 1157 struct address_space *mapping; 1158 bool extra_pins = false; 1159 1160 mapping = folio_mapping(folio); 1161 if (mapping) { 1162 res = truncate_error_folio(folio, page_to_pfn(p), mapping); 1163 /* The page is kept in page cache. */ 1164 extra_pins = true; 1165 folio_unlock(folio); 1166 } else { 1167 folio_unlock(folio); 1168 /* 1169 * migration entry prevents later access on error hugepage, 1170 * so we can free and dissolve it into buddy to save healthy 1171 * subpages. 1172 */ 1173 folio_put(folio); 1174 if (__page_handle_poison(p) > 0) { 1175 page_ref_inc(p); 1176 res = MF_RECOVERED; 1177 } else { 1178 res = MF_FAILED; 1179 } 1180 } 1181 1182 if (has_extra_refcount(ps, p, extra_pins)) 1183 res = MF_FAILED; 1184 1185 return res; 1186 } 1187 1188 /* 1189 * Various page states we can handle. 1190 * 1191 * A page state is defined by its current page->flags bits. 1192 * The table matches them in order and calls the right handler. 1193 * 1194 * This is quite tricky because we can access page at any time 1195 * in its live cycle, so all accesses have to be extremely careful. 1196 * 1197 * This is not complete. More states could be added. 1198 * For any missing state don't attempt recovery. 1199 */ 1200 1201 #define dirty (1UL << PG_dirty) 1202 #define sc ((1UL << PG_swapcache) | (1UL << PG_swapbacked)) 1203 #define unevict (1UL << PG_unevictable) 1204 #define mlock (1UL << PG_mlocked) 1205 #define lru (1UL << PG_lru) 1206 #define head (1UL << PG_head) 1207 #define reserved (1UL << PG_reserved) 1208 1209 static struct page_state error_states[] = { 1210 { reserved, reserved, MF_MSG_KERNEL, me_kernel }, 1211 /* 1212 * free pages are specially detected outside this table: 1213 * PG_buddy pages only make a small fraction of all free pages. 1214 */ 1215 1216 { head, head, MF_MSG_HUGE, me_huge_page }, 1217 1218 { sc|dirty, sc|dirty, MF_MSG_DIRTY_SWAPCACHE, me_swapcache_dirty }, 1219 { sc|dirty, sc, MF_MSG_CLEAN_SWAPCACHE, me_swapcache_clean }, 1220 1221 { mlock|dirty, mlock|dirty, MF_MSG_DIRTY_MLOCKED_LRU, me_pagecache_dirty }, 1222 { mlock|dirty, mlock, MF_MSG_CLEAN_MLOCKED_LRU, me_pagecache_clean }, 1223 1224 { unevict|dirty, unevict|dirty, MF_MSG_DIRTY_UNEVICTABLE_LRU, me_pagecache_dirty }, 1225 { unevict|dirty, unevict, MF_MSG_CLEAN_UNEVICTABLE_LRU, me_pagecache_clean }, 1226 1227 { lru|dirty, lru|dirty, MF_MSG_DIRTY_LRU, me_pagecache_dirty }, 1228 { lru|dirty, lru, MF_MSG_CLEAN_LRU, me_pagecache_clean }, 1229 1230 /* 1231 * Catchall entry: must be at end. 1232 */ 1233 { 0, 0, MF_MSG_UNKNOWN, me_unknown }, 1234 }; 1235 1236 #undef dirty 1237 #undef sc 1238 #undef unevict 1239 #undef mlock 1240 #undef lru 1241 #undef head 1242 #undef reserved 1243 1244 static void update_per_node_mf_stats(unsigned long pfn, 1245 enum mf_result result) 1246 { 1247 int nid = MAX_NUMNODES; 1248 struct memory_failure_stats *mf_stats = NULL; 1249 1250 nid = pfn_to_nid(pfn); 1251 if (unlikely(nid < 0 || nid >= MAX_NUMNODES)) { 1252 WARN_ONCE(1, "Memory failure: pfn=%#lx, invalid nid=%d", pfn, nid); 1253 return; 1254 } 1255 1256 mf_stats = &NODE_DATA(nid)->mf_stats; 1257 switch (result) { 1258 case MF_IGNORED: 1259 ++mf_stats->ignored; 1260 break; 1261 case MF_FAILED: 1262 ++mf_stats->failed; 1263 break; 1264 case MF_DELAYED: 1265 ++mf_stats->delayed; 1266 break; 1267 case MF_RECOVERED: 1268 ++mf_stats->recovered; 1269 break; 1270 default: 1271 WARN_ONCE(1, "Memory failure: mf_result=%d is not properly handled", result); 1272 break; 1273 } 1274 ++mf_stats->total; 1275 } 1276 1277 /* 1278 * "Dirty/Clean" indication is not 100% accurate due to the possibility of 1279 * setting PG_dirty outside page lock. See also comment above set_page_dirty(). 1280 */ 1281 static int action_result(unsigned long pfn, enum mf_action_page_type type, 1282 enum mf_result result) 1283 { 1284 trace_memory_failure_event(pfn, type, result); 1285 1286 if (type != MF_MSG_ALREADY_POISONED && type != MF_MSG_PFN_MAP) { 1287 num_poisoned_pages_inc(pfn); 1288 update_per_node_mf_stats(pfn, result); 1289 } 1290 1291 pr_err("%#lx: recovery action for %s: %s\n", 1292 pfn, action_page_types[type], action_name[result]); 1293 1294 return (result == MF_RECOVERED || result == MF_DELAYED) ? 0 : -EBUSY; 1295 } 1296 1297 static int page_action(struct page_state *ps, struct page *p, 1298 unsigned long pfn) 1299 { 1300 int result; 1301 1302 /* page p should be unlocked after returning from ps->action(). */ 1303 result = ps->action(ps, p); 1304 1305 /* Could do more checks here if page looks ok */ 1306 /* 1307 * Could adjust zone counters here to correct for the missing page. 1308 */ 1309 1310 return action_result(pfn, ps->type, result); 1311 } 1312 1313 static inline bool PageHWPoisonTakenOff(struct page *page) 1314 { 1315 return PageHWPoison(page) && page_private(page) == MAGIC_HWPOISON; 1316 } 1317 1318 void SetPageHWPoisonTakenOff(struct page *page) 1319 { 1320 set_page_private(page, MAGIC_HWPOISON); 1321 } 1322 1323 void ClearPageHWPoisonTakenOff(struct page *page) 1324 { 1325 if (PageHWPoison(page)) 1326 set_page_private(page, 0); 1327 } 1328 1329 /* 1330 * Return true if a page type of a given page is supported by hwpoison 1331 * mechanism (while handling could fail), otherwise false. This function 1332 * does not return true for hugetlb or device memory pages, so it's assumed 1333 * to be called only in the context where we never have such pages. 1334 */ 1335 static inline bool HWPoisonHandlable(struct page *page, unsigned long flags) 1336 { 1337 if (PageSlab(page)) 1338 return false; 1339 1340 /* Soft offline could migrate movable_ops pages */ 1341 if ((flags & MF_SOFT_OFFLINE) && page_has_movable_ops(page)) 1342 return true; 1343 1344 return PageLRU(page) || is_free_buddy_page(page); 1345 } 1346 1347 static int __get_hwpoison_page(struct page *page, unsigned long flags) 1348 { 1349 struct folio *folio = page_folio(page); 1350 int ret = 0; 1351 bool hugetlb = false; 1352 1353 ret = get_hwpoison_hugetlb_folio(folio, &hugetlb, false); 1354 if (hugetlb) { 1355 /* Make sure hugetlb demotion did not happen from under us. */ 1356 if (folio == page_folio(page)) 1357 return ret; 1358 if (ret > 0) { 1359 folio_put(folio); 1360 folio = page_folio(page); 1361 } 1362 } 1363 1364 /* 1365 * This check prevents from calling folio_try_get() for any 1366 * unsupported type of folio in order to reduce the risk of unexpected 1367 * races caused by taking a folio refcount. 1368 */ 1369 if (!HWPoisonHandlable(&folio->page, flags)) 1370 return -EBUSY; 1371 1372 if (folio_try_get(folio)) { 1373 if (folio == page_folio(page)) 1374 return 1; 1375 1376 pr_info("%#lx cannot catch tail\n", page_to_pfn(page)); 1377 folio_put(folio); 1378 } 1379 1380 return 0; 1381 } 1382 1383 #define GET_PAGE_MAX_RETRY_NUM 3 1384 1385 static int get_any_page(struct page *p, unsigned long flags) 1386 { 1387 int ret = 0, pass = 0; 1388 bool count_increased = false; 1389 1390 if (flags & MF_COUNT_INCREASED) 1391 count_increased = true; 1392 1393 try_again: 1394 if (!count_increased) { 1395 ret = __get_hwpoison_page(p, flags); 1396 if (!ret) { 1397 if (page_count(p)) { 1398 /* We raced with an allocation, retry. */ 1399 if (pass++ < GET_PAGE_MAX_RETRY_NUM) 1400 goto try_again; 1401 ret = -EBUSY; 1402 } else if (!PageHuge(p) && !is_free_buddy_page(p)) { 1403 /* We raced with put_page, retry. */ 1404 if (pass++ < GET_PAGE_MAX_RETRY_NUM) 1405 goto try_again; 1406 ret = -EIO; 1407 } 1408 goto out; 1409 } else if (ret == -EBUSY) { 1410 /* 1411 * We raced with (possibly temporary) unhandlable 1412 * page, retry. 1413 */ 1414 if (pass++ < 3) { 1415 shake_page(p); 1416 goto try_again; 1417 } 1418 ret = -EIO; 1419 goto out; 1420 } 1421 } 1422 1423 if (PageHuge(p) || HWPoisonHandlable(p, flags)) { 1424 ret = 1; 1425 } else { 1426 /* 1427 * A page we cannot handle. Check whether we can turn 1428 * it into something we can handle. 1429 */ 1430 if (pass++ < GET_PAGE_MAX_RETRY_NUM) { 1431 put_page(p); 1432 shake_page(p); 1433 count_increased = false; 1434 goto try_again; 1435 } 1436 put_page(p); 1437 ret = -EIO; 1438 } 1439 out: 1440 if (ret == -EIO) 1441 pr_err("%#lx: unhandlable page.\n", page_to_pfn(p)); 1442 1443 return ret; 1444 } 1445 1446 static int __get_unpoison_page(struct page *page) 1447 { 1448 struct folio *folio = page_folio(page); 1449 int ret = 0; 1450 bool hugetlb = false; 1451 1452 ret = get_hwpoison_hugetlb_folio(folio, &hugetlb, true); 1453 if (hugetlb) { 1454 /* Make sure hugetlb demotion did not happen from under us. */ 1455 if (folio == page_folio(page)) 1456 return ret; 1457 if (ret > 0) 1458 folio_put(folio); 1459 } 1460 1461 /* 1462 * PageHWPoisonTakenOff pages are not only marked as PG_hwpoison, 1463 * but also isolated from buddy freelist, so need to identify the 1464 * state and have to cancel both operations to unpoison. 1465 */ 1466 if (PageHWPoisonTakenOff(page)) 1467 return -EHWPOISON; 1468 1469 return get_page_unless_zero(page) ? 1 : 0; 1470 } 1471 1472 /** 1473 * get_hwpoison_page() - Get refcount for memory error handling 1474 * @p: Raw error page (hit by memory error) 1475 * @flags: Flags controlling behavior of error handling 1476 * 1477 * get_hwpoison_page() takes a page refcount of an error page to handle memory 1478 * error on it, after checking that the error page is in a well-defined state 1479 * (defined as a page-type we can successfully handle the memory error on it, 1480 * such as LRU page and hugetlb page). 1481 * 1482 * Memory error handling could be triggered at any time on any type of page, 1483 * so it's prone to race with typical memory management lifecycle (like 1484 * allocation and free). So to avoid such races, get_hwpoison_page() takes 1485 * extra care for the error page's state (as done in __get_hwpoison_page()), 1486 * and has some retry logic in get_any_page(). 1487 * 1488 * When called from unpoison_memory(), the caller should already ensure that 1489 * the given page has PG_hwpoison. So it's never reused for other page 1490 * allocations, and __get_unpoison_page() never races with them. 1491 * 1492 * Return: 0 on failure or free buddy (hugetlb) page, 1493 * 1 on success for in-use pages in a well-defined state, 1494 * -EIO for pages on which we can not handle memory errors, 1495 * -EBUSY when get_hwpoison_page() has raced with page lifecycle 1496 * operations like allocation and free, 1497 * -EHWPOISON when the page is hwpoisoned and taken off from buddy. 1498 */ 1499 static int get_hwpoison_page(struct page *p, unsigned long flags) 1500 { 1501 int ret; 1502 1503 zone_pcp_disable(page_zone(p)); 1504 if (flags & MF_UNPOISON) 1505 ret = __get_unpoison_page(p); 1506 else 1507 ret = get_any_page(p, flags); 1508 zone_pcp_enable(page_zone(p)); 1509 1510 return ret; 1511 } 1512 1513 /* 1514 * The caller must guarantee the folio isn't large folio, except hugetlb. 1515 * try_to_unmap() can't handle it. 1516 */ 1517 int unmap_poisoned_folio(struct folio *folio, unsigned long pfn, bool must_kill) 1518 { 1519 enum ttu_flags ttu = TTU_IGNORE_MLOCK | TTU_SYNC | TTU_HWPOISON; 1520 struct address_space *mapping; 1521 1522 if (folio_test_swapcache(folio)) { 1523 pr_err("%#lx: keeping poisoned page in swap cache\n", pfn); 1524 ttu &= ~TTU_HWPOISON; 1525 } 1526 1527 /* 1528 * Propagate the dirty bit from PTEs to struct page first, because we 1529 * need this to decide if we should kill or just drop the page. 1530 * XXX: the dirty test could be racy: set_page_dirty() may not always 1531 * be called inside page lock (it's recommended but not enforced). 1532 */ 1533 mapping = folio_mapping(folio); 1534 if (!must_kill && !folio_test_dirty(folio) && mapping && 1535 mapping_can_writeback(mapping)) { 1536 if (folio_mkclean(folio)) { 1537 folio_set_dirty(folio); 1538 } else { 1539 ttu &= ~TTU_HWPOISON; 1540 pr_info("%#lx: corrupted page was clean: dropped without side effects\n", 1541 pfn); 1542 } 1543 } 1544 1545 if (folio_test_hugetlb(folio) && !folio_test_anon(folio)) { 1546 /* 1547 * For hugetlb folios in shared mappings, try_to_unmap 1548 * could potentially call huge_pmd_unshare. Because of 1549 * this, take semaphore in write mode here and set 1550 * TTU_RMAP_LOCKED to indicate we have taken the lock 1551 * at this higher level. 1552 */ 1553 mapping = hugetlb_folio_mapping_lock_write(folio); 1554 if (!mapping) { 1555 pr_info("%#lx: could not lock mapping for mapped hugetlb folio\n", 1556 folio_pfn(folio)); 1557 return -EBUSY; 1558 } 1559 1560 try_to_unmap(folio, ttu|TTU_RMAP_LOCKED); 1561 i_mmap_unlock_write(mapping); 1562 } else { 1563 try_to_unmap(folio, ttu); 1564 } 1565 1566 return folio_mapped(folio) ? -EBUSY : 0; 1567 } 1568 1569 /* 1570 * Do all that is necessary to remove user space mappings. Unmap 1571 * the pages and send SIGBUS to the processes if the data was dirty. 1572 */ 1573 static bool hwpoison_user_mappings(struct folio *folio, struct page *p, 1574 unsigned long pfn, int flags) 1575 { 1576 LIST_HEAD(tokill); 1577 bool unmap_success; 1578 int forcekill; 1579 bool mlocked = folio_test_mlocked(folio); 1580 1581 /* 1582 * Here we are interested only in user-mapped pages, so skip any 1583 * other types of pages. 1584 */ 1585 if (folio_test_reserved(folio) || folio_test_slab(folio) || 1586 folio_test_pgtable(folio) || folio_test_offline(folio)) 1587 return true; 1588 if (!(folio_test_lru(folio) || folio_test_hugetlb(folio))) 1589 return true; 1590 1591 /* 1592 * This check implies we don't kill processes if their pages 1593 * are in the swap cache early. Those are always late kills. 1594 */ 1595 if (!folio_mapped(folio)) 1596 return true; 1597 1598 /* 1599 * First collect all the processes that have the page 1600 * mapped in dirty form. This has to be done before try_to_unmap, 1601 * because ttu takes the rmap data structures down. 1602 */ 1603 collect_procs(folio, p, &tokill, flags & MF_ACTION_REQUIRED); 1604 1605 unmap_success = !unmap_poisoned_folio(folio, pfn, flags & MF_MUST_KILL); 1606 if (!unmap_success) 1607 pr_err("%#lx: failed to unmap page (folio mapcount=%d)\n", 1608 pfn, folio_mapcount(folio)); 1609 1610 /* 1611 * try_to_unmap() might put mlocked page in lru cache, so call 1612 * shake_page() again to ensure that it's flushed. 1613 */ 1614 if (mlocked) 1615 shake_folio(folio); 1616 1617 /* 1618 * Now that the dirty bit has been propagated to the 1619 * struct page and all unmaps done we can decide if 1620 * killing is needed or not. Only kill when the page 1621 * was dirty or the process is not restartable, 1622 * otherwise the tokill list is merely 1623 * freed. When there was a problem unmapping earlier 1624 * use a more force-full uncatchable kill to prevent 1625 * any accesses to the poisoned memory. 1626 */ 1627 forcekill = folio_test_dirty(folio) || (flags & MF_MUST_KILL) || 1628 !unmap_success; 1629 kill_procs(&tokill, forcekill, pfn, flags); 1630 1631 return unmap_success; 1632 } 1633 1634 static int identify_page_state(unsigned long pfn, struct page *p, 1635 unsigned long page_flags) 1636 { 1637 struct page_state *ps; 1638 1639 /* 1640 * The first check uses the current page flags which may not have any 1641 * relevant information. The second check with the saved page flags is 1642 * carried out only if the first check can't determine the page status. 1643 */ 1644 for (ps = error_states;; ps++) 1645 if ((p->flags.f & ps->mask) == ps->res) 1646 break; 1647 1648 page_flags |= (p->flags.f & (1UL << PG_dirty)); 1649 1650 if (!ps->mask) 1651 for (ps = error_states;; ps++) 1652 if ((page_flags & ps->mask) == ps->res) 1653 break; 1654 return page_action(ps, p, pfn); 1655 } 1656 1657 /* 1658 * When 'release' is 'false', it means that if thp split has failed, 1659 * there is still more to do, hence the page refcount we took earlier 1660 * is still needed. 1661 */ 1662 static int try_to_split_thp_page(struct page *page, bool release) 1663 { 1664 int ret; 1665 1666 lock_page(page); 1667 ret = split_huge_page(page); 1668 unlock_page(page); 1669 1670 if (ret && release) 1671 put_page(page); 1672 1673 return ret; 1674 } 1675 1676 static void unmap_and_kill(struct list_head *to_kill, unsigned long pfn, 1677 struct address_space *mapping, pgoff_t index, int flags) 1678 { 1679 struct to_kill *tk; 1680 unsigned long size = 0; 1681 1682 list_for_each_entry(tk, to_kill, nd) 1683 if (tk->size_shift) 1684 size = max(size, 1UL << tk->size_shift); 1685 1686 if (size) { 1687 /* 1688 * Unmap the largest mapping to avoid breaking up device-dax 1689 * mappings which are constant size. The actual size of the 1690 * mapping being torn down is communicated in siginfo, see 1691 * kill_proc() 1692 */ 1693 loff_t start = ((loff_t)index << PAGE_SHIFT) & ~(size - 1); 1694 1695 unmap_mapping_range(mapping, start, size, 0); 1696 } 1697 1698 kill_procs(to_kill, flags & MF_MUST_KILL, pfn, flags); 1699 } 1700 1701 /* 1702 * Only dev_pagemap pages get here, such as fsdax when the filesystem 1703 * either do not claim or fails to claim a hwpoison event, or devdax. 1704 * The fsdax pages are initialized per base page, and the devdax pages 1705 * could be initialized either as base pages, or as compound pages with 1706 * vmemmap optimization enabled. Devdax is simplistic in its dealing with 1707 * hwpoison, such that, if a subpage of a compound page is poisoned, 1708 * simply mark the compound head page is by far sufficient. 1709 */ 1710 static int mf_generic_kill_procs(unsigned long long pfn, int flags, 1711 struct dev_pagemap *pgmap) 1712 { 1713 struct folio *folio = pfn_folio(pfn); 1714 LIST_HEAD(to_kill); 1715 dax_entry_t cookie; 1716 int rc = 0; 1717 1718 /* 1719 * Prevent the inode from being freed while we are interrogating 1720 * the address_space, typically this would be handled by 1721 * lock_page(), but dax pages do not use the page lock. This 1722 * also prevents changes to the mapping of this pfn until 1723 * poison signaling is complete. 1724 */ 1725 cookie = dax_lock_folio(folio); 1726 if (!cookie) 1727 return -EBUSY; 1728 1729 if (hwpoison_filter(&folio->page)) { 1730 rc = -EOPNOTSUPP; 1731 goto unlock; 1732 } 1733 1734 switch (pgmap->type) { 1735 case MEMORY_DEVICE_PRIVATE: 1736 case MEMORY_DEVICE_COHERENT: 1737 /* 1738 * TODO: Handle device pages which may need coordination 1739 * with device-side memory. 1740 */ 1741 rc = -ENXIO; 1742 goto unlock; 1743 default: 1744 break; 1745 } 1746 1747 /* 1748 * Use this flag as an indication that the dax page has been 1749 * remapped UC to prevent speculative consumption of poison. 1750 */ 1751 SetPageHWPoison(&folio->page); 1752 1753 /* 1754 * Unlike System-RAM there is no possibility to swap in a 1755 * different physical page at a given virtual address, so all 1756 * userspace consumption of ZONE_DEVICE memory necessitates 1757 * SIGBUS (i.e. MF_MUST_KILL) 1758 */ 1759 flags |= MF_ACTION_REQUIRED | MF_MUST_KILL; 1760 collect_procs(folio, &folio->page, &to_kill, true); 1761 1762 unmap_and_kill(&to_kill, pfn, folio->mapping, folio->index, flags); 1763 unlock: 1764 dax_unlock_folio(folio, cookie); 1765 return rc; 1766 } 1767 1768 #ifdef CONFIG_FS_DAX 1769 /** 1770 * mf_dax_kill_procs - Collect and kill processes who are using this file range 1771 * @mapping: address_space of the file in use 1772 * @index: start pgoff of the range within the file 1773 * @count: length of the range, in unit of PAGE_SIZE 1774 * @mf_flags: memory failure flags 1775 */ 1776 int mf_dax_kill_procs(struct address_space *mapping, pgoff_t index, 1777 unsigned long count, int mf_flags) 1778 { 1779 LIST_HEAD(to_kill); 1780 dax_entry_t cookie; 1781 struct page *page; 1782 size_t end = index + count; 1783 bool pre_remove = mf_flags & MF_MEM_PRE_REMOVE; 1784 1785 mf_flags |= MF_ACTION_REQUIRED | MF_MUST_KILL; 1786 1787 for (; index < end; index++) { 1788 page = NULL; 1789 cookie = dax_lock_mapping_entry(mapping, index, &page); 1790 if (!cookie) 1791 return -EBUSY; 1792 if (!page) 1793 goto unlock; 1794 1795 if (!pre_remove) 1796 SetPageHWPoison(page); 1797 1798 /* 1799 * The pre_remove case is revoking access, the memory is still 1800 * good and could theoretically be put back into service. 1801 */ 1802 collect_procs_fsdax(page, mapping, index, &to_kill, pre_remove); 1803 unmap_and_kill(&to_kill, page_to_pfn(page), mapping, 1804 index, mf_flags); 1805 unlock: 1806 dax_unlock_mapping_entry(mapping, index, cookie); 1807 } 1808 return 0; 1809 } 1810 EXPORT_SYMBOL_GPL(mf_dax_kill_procs); 1811 #endif /* CONFIG_FS_DAX */ 1812 1813 #ifdef CONFIG_HUGETLB_PAGE 1814 1815 /* 1816 * Struct raw_hwp_page represents information about "raw error page", 1817 * constructing singly linked list from ->_hugetlb_hwpoison field of folio. 1818 */ 1819 struct raw_hwp_page { 1820 struct llist_node node; 1821 struct page *page; 1822 }; 1823 1824 static inline struct llist_head *raw_hwp_list_head(struct folio *folio) 1825 { 1826 return (struct llist_head *)&folio->_hugetlb_hwpoison; 1827 } 1828 1829 bool is_raw_hwpoison_page_in_hugepage(struct page *page) 1830 { 1831 struct llist_head *raw_hwp_head; 1832 struct raw_hwp_page *p; 1833 struct folio *folio = page_folio(page); 1834 bool ret = false; 1835 1836 if (!folio_test_hwpoison(folio)) 1837 return false; 1838 1839 if (!folio_test_hugetlb(folio)) 1840 return PageHWPoison(page); 1841 1842 /* 1843 * When RawHwpUnreliable is set, kernel lost track of which subpages 1844 * are HWPOISON. So return as if ALL subpages are HWPOISONed. 1845 */ 1846 if (folio_test_hugetlb_raw_hwp_unreliable(folio)) 1847 return true; 1848 1849 mutex_lock(&mf_mutex); 1850 1851 raw_hwp_head = raw_hwp_list_head(folio); 1852 llist_for_each_entry(p, raw_hwp_head->first, node) { 1853 if (page == p->page) { 1854 ret = true; 1855 break; 1856 } 1857 } 1858 1859 mutex_unlock(&mf_mutex); 1860 1861 return ret; 1862 } 1863 1864 static unsigned long __folio_free_raw_hwp(struct folio *folio, bool move_flag) 1865 { 1866 struct llist_node *head; 1867 struct raw_hwp_page *p, *next; 1868 unsigned long count = 0; 1869 1870 head = llist_del_all(raw_hwp_list_head(folio)); 1871 llist_for_each_entry_safe(p, next, head, node) { 1872 if (move_flag) 1873 SetPageHWPoison(p->page); 1874 else 1875 num_poisoned_pages_sub(page_to_pfn(p->page), 1); 1876 kfree(p); 1877 count++; 1878 } 1879 return count; 1880 } 1881 1882 static int folio_set_hugetlb_hwpoison(struct folio *folio, struct page *page) 1883 { 1884 struct llist_head *head; 1885 struct raw_hwp_page *raw_hwp; 1886 struct raw_hwp_page *p; 1887 int ret = folio_test_set_hwpoison(folio) ? -EHWPOISON : 0; 1888 1889 /* 1890 * Once the hwpoison hugepage has lost reliable raw error info, 1891 * there is little meaning to keep additional error info precisely, 1892 * so skip to add additional raw error info. 1893 */ 1894 if (folio_test_hugetlb_raw_hwp_unreliable(folio)) 1895 return -EHWPOISON; 1896 head = raw_hwp_list_head(folio); 1897 llist_for_each_entry(p, head->first, node) { 1898 if (p->page == page) 1899 return -EHWPOISON; 1900 } 1901 1902 raw_hwp = kmalloc(sizeof(struct raw_hwp_page), GFP_ATOMIC); 1903 if (raw_hwp) { 1904 raw_hwp->page = page; 1905 llist_add(&raw_hwp->node, head); 1906 /* the first error event will be counted in action_result(). */ 1907 if (ret) 1908 num_poisoned_pages_inc(page_to_pfn(page)); 1909 } else { 1910 /* 1911 * Failed to save raw error info. We no longer trace all 1912 * hwpoisoned subpages, and we need refuse to free/dissolve 1913 * this hwpoisoned hugepage. 1914 */ 1915 folio_set_hugetlb_raw_hwp_unreliable(folio); 1916 /* 1917 * Once hugetlb_raw_hwp_unreliable is set, raw_hwp_page is not 1918 * used any more, so free it. 1919 */ 1920 __folio_free_raw_hwp(folio, false); 1921 } 1922 return ret; 1923 } 1924 1925 static unsigned long folio_free_raw_hwp(struct folio *folio, bool move_flag) 1926 { 1927 /* 1928 * hugetlb_vmemmap_optimized hugepages can't be freed because struct 1929 * pages for tail pages are required but they don't exist. 1930 */ 1931 if (move_flag && folio_test_hugetlb_vmemmap_optimized(folio)) 1932 return 0; 1933 1934 /* 1935 * hugetlb_raw_hwp_unreliable hugepages shouldn't be unpoisoned by 1936 * definition. 1937 */ 1938 if (folio_test_hugetlb_raw_hwp_unreliable(folio)) 1939 return 0; 1940 1941 return __folio_free_raw_hwp(folio, move_flag); 1942 } 1943 1944 void folio_clear_hugetlb_hwpoison(struct folio *folio) 1945 { 1946 if (folio_test_hugetlb_raw_hwp_unreliable(folio)) 1947 return; 1948 if (folio_test_hugetlb_vmemmap_optimized(folio)) 1949 return; 1950 folio_clear_hwpoison(folio); 1951 folio_free_raw_hwp(folio, true); 1952 } 1953 1954 /* 1955 * Called from hugetlb code with hugetlb_lock held. 1956 * 1957 * Return values: 1958 * 0 - free hugepage 1959 * 1 - in-use hugepage 1960 * 2 - not a hugepage 1961 * -EBUSY - the hugepage is busy (try to retry) 1962 * -EHWPOISON - the hugepage is already hwpoisoned 1963 */ 1964 int __get_huge_page_for_hwpoison(unsigned long pfn, int flags, 1965 bool *migratable_cleared) 1966 { 1967 struct page *page = pfn_to_page(pfn); 1968 struct folio *folio = page_folio(page); 1969 int ret = 2; /* fallback to normal page handling */ 1970 bool count_increased = false; 1971 1972 if (!folio_test_hugetlb(folio)) 1973 goto out; 1974 1975 if (flags & MF_COUNT_INCREASED) { 1976 ret = 1; 1977 count_increased = true; 1978 } else if (folio_test_hugetlb_freed(folio)) { 1979 ret = 0; 1980 } else if (folio_test_hugetlb_migratable(folio)) { 1981 ret = folio_try_get(folio); 1982 if (ret) 1983 count_increased = true; 1984 } else { 1985 ret = -EBUSY; 1986 if (!(flags & MF_NO_RETRY)) 1987 goto out; 1988 } 1989 1990 if (folio_set_hugetlb_hwpoison(folio, page)) { 1991 ret = -EHWPOISON; 1992 goto out; 1993 } 1994 1995 /* 1996 * Clearing hugetlb_migratable for hwpoisoned hugepages to prevent them 1997 * from being migrated by memory hotremove. 1998 */ 1999 if (count_increased && folio_test_hugetlb_migratable(folio)) { 2000 folio_clear_hugetlb_migratable(folio); 2001 *migratable_cleared = true; 2002 } 2003 2004 return ret; 2005 out: 2006 if (count_increased) 2007 folio_put(folio); 2008 return ret; 2009 } 2010 2011 /* 2012 * Taking refcount of hugetlb pages needs extra care about race conditions 2013 * with basic operations like hugepage allocation/free/demotion. 2014 * So some of prechecks for hwpoison (pinning, and testing/setting 2015 * PageHWPoison) should be done in single hugetlb_lock range. 2016 */ 2017 static int try_memory_failure_hugetlb(unsigned long pfn, int flags, int *hugetlb) 2018 { 2019 int res; 2020 struct page *p = pfn_to_page(pfn); 2021 struct folio *folio; 2022 unsigned long page_flags; 2023 bool migratable_cleared = false; 2024 2025 *hugetlb = 1; 2026 retry: 2027 res = get_huge_page_for_hwpoison(pfn, flags, &migratable_cleared); 2028 if (res == 2) { /* fallback to normal page handling */ 2029 *hugetlb = 0; 2030 return 0; 2031 } else if (res == -EHWPOISON) { 2032 if (flags & MF_ACTION_REQUIRED) { 2033 folio = page_folio(p); 2034 res = kill_accessing_process(current, folio_pfn(folio), flags); 2035 } 2036 action_result(pfn, MF_MSG_ALREADY_POISONED, MF_FAILED); 2037 return res; 2038 } else if (res == -EBUSY) { 2039 if (!(flags & MF_NO_RETRY)) { 2040 flags |= MF_NO_RETRY; 2041 goto retry; 2042 } 2043 return action_result(pfn, MF_MSG_GET_HWPOISON, MF_IGNORED); 2044 } 2045 2046 folio = page_folio(p); 2047 folio_lock(folio); 2048 2049 if (hwpoison_filter(p)) { 2050 folio_clear_hugetlb_hwpoison(folio); 2051 if (migratable_cleared) 2052 folio_set_hugetlb_migratable(folio); 2053 folio_unlock(folio); 2054 if (res == 1) 2055 folio_put(folio); 2056 return -EOPNOTSUPP; 2057 } 2058 2059 /* 2060 * Handling free hugepage. The possible race with hugepage allocation 2061 * or demotion can be prevented by PageHWPoison flag. 2062 */ 2063 if (res == 0) { 2064 folio_unlock(folio); 2065 if (__page_handle_poison(p) > 0) { 2066 page_ref_inc(p); 2067 res = MF_RECOVERED; 2068 } else { 2069 res = MF_FAILED; 2070 } 2071 return action_result(pfn, MF_MSG_FREE_HUGE, res); 2072 } 2073 2074 page_flags = folio->flags.f; 2075 2076 if (!hwpoison_user_mappings(folio, p, pfn, flags)) { 2077 folio_unlock(folio); 2078 return action_result(pfn, MF_MSG_UNMAP_FAILED, MF_FAILED); 2079 } 2080 2081 return identify_page_state(pfn, p, page_flags); 2082 } 2083 2084 #else 2085 static inline int try_memory_failure_hugetlb(unsigned long pfn, int flags, int *hugetlb) 2086 { 2087 return 0; 2088 } 2089 2090 static inline unsigned long folio_free_raw_hwp(struct folio *folio, bool flag) 2091 { 2092 return 0; 2093 } 2094 #endif /* CONFIG_HUGETLB_PAGE */ 2095 2096 /* Drop the extra refcount in case we come from madvise() */ 2097 static void put_ref_page(unsigned long pfn, int flags) 2098 { 2099 if (!(flags & MF_COUNT_INCREASED)) 2100 return; 2101 2102 put_page(pfn_to_page(pfn)); 2103 } 2104 2105 static int memory_failure_dev_pagemap(unsigned long pfn, int flags, 2106 struct dev_pagemap *pgmap) 2107 { 2108 int rc = -ENXIO; 2109 2110 /* device metadata space is not recoverable */ 2111 if (!pgmap_pfn_valid(pgmap, pfn)) 2112 goto out; 2113 2114 /* 2115 * Call driver's implementation to handle the memory failure, otherwise 2116 * fall back to generic handler. 2117 */ 2118 if (pgmap_has_memory_failure(pgmap)) { 2119 rc = pgmap->ops->memory_failure(pgmap, pfn, 1, flags); 2120 /* 2121 * Fall back to generic handler too if operation is not 2122 * supported inside the driver/device/filesystem. 2123 */ 2124 if (rc != -EOPNOTSUPP) 2125 goto out; 2126 } 2127 2128 rc = mf_generic_kill_procs(pfn, flags, pgmap); 2129 out: 2130 /* drop pgmap ref acquired in caller */ 2131 put_dev_pagemap(pgmap); 2132 if (rc != -EOPNOTSUPP) 2133 action_result(pfn, MF_MSG_DAX, rc ? MF_FAILED : MF_RECOVERED); 2134 return rc; 2135 } 2136 2137 /* 2138 * The calling condition is as such: thp split failed, page might have 2139 * been RDMA pinned, not much can be done for recovery. 2140 * But a SIGBUS should be delivered with vaddr provided so that the user 2141 * application has a chance to recover. Also, application processes' 2142 * election for MCE early killed will be honored. 2143 */ 2144 static void kill_procs_now(struct page *p, unsigned long pfn, int flags, 2145 struct folio *folio) 2146 { 2147 LIST_HEAD(tokill); 2148 2149 folio_lock(folio); 2150 collect_procs(folio, p, &tokill, flags & MF_ACTION_REQUIRED); 2151 folio_unlock(folio); 2152 2153 kill_procs(&tokill, true, pfn, flags); 2154 } 2155 2156 int register_pfn_address_space(struct pfn_address_space *pfn_space) 2157 { 2158 guard(mutex)(&pfn_space_lock); 2159 2160 if (interval_tree_iter_first(&pfn_space_itree, 2161 pfn_space->node.start, 2162 pfn_space->node.last)) 2163 return -EBUSY; 2164 2165 interval_tree_insert(&pfn_space->node, &pfn_space_itree); 2166 2167 return 0; 2168 } 2169 EXPORT_SYMBOL_GPL(register_pfn_address_space); 2170 2171 void unregister_pfn_address_space(struct pfn_address_space *pfn_space) 2172 { 2173 guard(mutex)(&pfn_space_lock); 2174 2175 if (interval_tree_iter_first(&pfn_space_itree, 2176 pfn_space->node.start, 2177 pfn_space->node.last)) 2178 interval_tree_remove(&pfn_space->node, &pfn_space_itree); 2179 } 2180 EXPORT_SYMBOL_GPL(unregister_pfn_address_space); 2181 2182 static void add_to_kill_pfn(struct task_struct *tsk, 2183 struct vm_area_struct *vma, 2184 struct list_head *to_kill, 2185 unsigned long pfn) 2186 { 2187 struct to_kill *tk; 2188 2189 tk = kmalloc(sizeof(*tk), GFP_ATOMIC); 2190 if (!tk) { 2191 pr_info("Unable to kill proc %d\n", tsk->pid); 2192 return; 2193 } 2194 2195 /* Check for pgoff not backed by struct page */ 2196 tk->addr = vma_address(vma, pfn, 1); 2197 tk->size_shift = PAGE_SHIFT; 2198 2199 if (tk->addr == -EFAULT) 2200 pr_info("Unable to find address %lx in %s\n", 2201 pfn, tsk->comm); 2202 2203 get_task_struct(tsk); 2204 tk->tsk = tsk; 2205 list_add_tail(&tk->nd, to_kill); 2206 } 2207 2208 /* 2209 * Collect processes when the error hit a PFN not backed by struct page. 2210 */ 2211 static void collect_procs_pfn(struct address_space *mapping, 2212 unsigned long pfn, struct list_head *to_kill) 2213 { 2214 struct vm_area_struct *vma; 2215 struct task_struct *tsk; 2216 2217 i_mmap_lock_read(mapping); 2218 rcu_read_lock(); 2219 for_each_process(tsk) { 2220 struct task_struct *t = tsk; 2221 2222 t = task_early_kill(tsk, true); 2223 if (!t) 2224 continue; 2225 vma_interval_tree_foreach(vma, &mapping->i_mmap, pfn, pfn) { 2226 if (vma->vm_mm == t->mm) 2227 add_to_kill_pfn(t, vma, to_kill, pfn); 2228 } 2229 } 2230 rcu_read_unlock(); 2231 i_mmap_unlock_read(mapping); 2232 } 2233 2234 /** 2235 * memory_failure_pfn - Handle memory failure on a page not backed by 2236 * struct page. 2237 * @pfn: Page Number of the corrupted page 2238 * @flags: fine tune action taken 2239 * 2240 * Return: 2241 * 0 - success, 2242 * -EBUSY - Page PFN does not belong to any address space mapping. 2243 */ 2244 static int memory_failure_pfn(unsigned long pfn, int flags) 2245 { 2246 struct interval_tree_node *node; 2247 LIST_HEAD(tokill); 2248 2249 scoped_guard(mutex, &pfn_space_lock) { 2250 bool mf_handled = false; 2251 2252 /* 2253 * Modules registers with MM the address space mapping to 2254 * the device memory they manage. Iterate to identify 2255 * exactly which address space has mapped to this failing 2256 * PFN. 2257 */ 2258 for (node = interval_tree_iter_first(&pfn_space_itree, pfn, pfn); node; 2259 node = interval_tree_iter_next(node, pfn, pfn)) { 2260 struct pfn_address_space *pfn_space = 2261 container_of(node, struct pfn_address_space, node); 2262 2263 collect_procs_pfn(pfn_space->mapping, pfn, &tokill); 2264 2265 mf_handled = true; 2266 } 2267 2268 if (!mf_handled) 2269 return action_result(pfn, MF_MSG_PFN_MAP, MF_IGNORED); 2270 } 2271 2272 /* 2273 * Unlike System-RAM there is no possibility to swap in a different 2274 * physical page at a given virtual address, so all userspace 2275 * consumption of direct PFN memory necessitates SIGBUS (i.e. 2276 * MF_MUST_KILL) 2277 */ 2278 flags |= MF_ACTION_REQUIRED | MF_MUST_KILL; 2279 2280 kill_procs(&tokill, true, pfn, flags); 2281 2282 return action_result(pfn, MF_MSG_PFN_MAP, MF_RECOVERED); 2283 } 2284 2285 /** 2286 * memory_failure - Handle memory failure of a page. 2287 * @pfn: Page Number of the corrupted page 2288 * @flags: fine tune action taken 2289 * 2290 * This function is called by the low level machine check code 2291 * of an architecture when it detects hardware memory corruption 2292 * of a page. It tries its best to recover, which includes 2293 * dropping pages, killing processes etc. 2294 * 2295 * The function is primarily of use for corruptions that 2296 * happen outside the current execution context (e.g. when 2297 * detected by a background scrubber) 2298 * 2299 * Must run in process context (e.g. a work queue) with interrupts 2300 * enabled and no spinlocks held. 2301 * 2302 * Return: 2303 * 0 - success, 2304 * -ENXIO - memory not managed by the kernel 2305 * -EOPNOTSUPP - hwpoison_filter() filtered the error event, 2306 * -EHWPOISON - the page was already poisoned, potentially 2307 * kill process, 2308 * other negative values - failure. 2309 */ 2310 int memory_failure(unsigned long pfn, int flags) 2311 { 2312 struct page *p; 2313 struct folio *folio; 2314 struct dev_pagemap *pgmap; 2315 int res = 0; 2316 unsigned long page_flags; 2317 bool retry = true; 2318 int hugetlb = 0; 2319 2320 if (!sysctl_memory_failure_recovery) 2321 panic("Memory failure on page %lx", pfn); 2322 2323 mutex_lock(&mf_mutex); 2324 2325 if (!(flags & MF_SW_SIMULATED)) 2326 hw_memory_failure = true; 2327 2328 p = pfn_to_online_page(pfn); 2329 if (!p) { 2330 res = arch_memory_failure(pfn, flags); 2331 if (res == 0) 2332 goto unlock_mutex; 2333 2334 if (!pfn_valid(pfn) && !arch_is_platform_page(PFN_PHYS(pfn))) { 2335 /* 2336 * The PFN is not backed by struct page. 2337 */ 2338 res = memory_failure_pfn(pfn, flags); 2339 goto unlock_mutex; 2340 } 2341 2342 if (pfn_valid(pfn)) { 2343 pgmap = get_dev_pagemap(pfn); 2344 put_ref_page(pfn, flags); 2345 if (pgmap) { 2346 res = memory_failure_dev_pagemap(pfn, flags, 2347 pgmap); 2348 goto unlock_mutex; 2349 } 2350 } 2351 pr_err("%#lx: memory outside kernel control\n", pfn); 2352 res = -ENXIO; 2353 goto unlock_mutex; 2354 } 2355 2356 try_again: 2357 res = try_memory_failure_hugetlb(pfn, flags, &hugetlb); 2358 if (hugetlb) 2359 goto unlock_mutex; 2360 2361 if (TestSetPageHWPoison(p)) { 2362 res = -EHWPOISON; 2363 if (flags & MF_ACTION_REQUIRED) 2364 res = kill_accessing_process(current, pfn, flags); 2365 if (flags & MF_COUNT_INCREASED) 2366 put_page(p); 2367 action_result(pfn, MF_MSG_ALREADY_POISONED, MF_FAILED); 2368 goto unlock_mutex; 2369 } 2370 2371 /* 2372 * We need/can do nothing about count=0 pages. 2373 * 1) it's a free page, and therefore in safe hand: 2374 * check_new_page() will be the gate keeper. 2375 * 2) it's part of a non-compound high order page. 2376 * Implies some kernel user: cannot stop them from 2377 * R/W the page; let's pray that the page has been 2378 * used and will be freed some time later. 2379 * In fact it's dangerous to directly bump up page count from 0, 2380 * that may make page_ref_freeze()/page_ref_unfreeze() mismatch. 2381 */ 2382 if (!(flags & MF_COUNT_INCREASED)) { 2383 res = get_hwpoison_page(p, flags); 2384 if (!res) { 2385 if (is_free_buddy_page(p)) { 2386 if (take_page_off_buddy(p)) { 2387 page_ref_inc(p); 2388 res = MF_RECOVERED; 2389 } else { 2390 /* We lost the race, try again */ 2391 if (retry) { 2392 ClearPageHWPoison(p); 2393 retry = false; 2394 goto try_again; 2395 } 2396 res = MF_FAILED; 2397 } 2398 res = action_result(pfn, MF_MSG_BUDDY, res); 2399 } else { 2400 res = action_result(pfn, MF_MSG_KERNEL_HIGH_ORDER, MF_IGNORED); 2401 } 2402 goto unlock_mutex; 2403 } else if (res < 0) { 2404 res = action_result(pfn, MF_MSG_GET_HWPOISON, MF_IGNORED); 2405 goto unlock_mutex; 2406 } 2407 } 2408 2409 folio = page_folio(p); 2410 2411 /* filter pages that are protected from hwpoison test by users */ 2412 folio_lock(folio); 2413 if (hwpoison_filter(p)) { 2414 ClearPageHWPoison(p); 2415 folio_unlock(folio); 2416 folio_put(folio); 2417 res = -EOPNOTSUPP; 2418 goto unlock_mutex; 2419 } 2420 folio_unlock(folio); 2421 2422 if (folio_test_large(folio)) { 2423 /* 2424 * The flag must be set after the refcount is bumped 2425 * otherwise it may race with THP split. 2426 * And the flag can't be set in get_hwpoison_page() since 2427 * it is called by soft offline too and it is just called 2428 * for !MF_COUNT_INCREASED. So here seems to be the best 2429 * place. 2430 * 2431 * Don't need care about the above error handling paths for 2432 * get_hwpoison_page() since they handle either free page 2433 * or unhandlable page. The refcount is bumped iff the 2434 * page is a valid handlable page. 2435 */ 2436 folio_set_has_hwpoisoned(folio); 2437 if (try_to_split_thp_page(p, false) < 0) { 2438 res = -EHWPOISON; 2439 kill_procs_now(p, pfn, flags, folio); 2440 put_page(p); 2441 action_result(pfn, MF_MSG_UNSPLIT_THP, MF_FAILED); 2442 goto unlock_mutex; 2443 } 2444 VM_BUG_ON_PAGE(!page_count(p), p); 2445 folio = page_folio(p); 2446 } 2447 2448 /* 2449 * We ignore non-LRU pages for good reasons. 2450 * - PG_locked is only well defined for LRU pages and a few others 2451 * - to avoid races with __SetPageLocked() 2452 * - to avoid races with __SetPageSlab*() (and more non-atomic ops) 2453 * The check (unnecessarily) ignores LRU pages being isolated and 2454 * walked by the page reclaim code, however that's not a big loss. 2455 */ 2456 shake_folio(folio); 2457 2458 folio_lock(folio); 2459 2460 /* 2461 * We're only intended to deal with the non-Compound page here. 2462 * The page cannot become compound pages again as folio has been 2463 * splited and extra refcnt is held. 2464 */ 2465 WARN_ON(folio_test_large(folio)); 2466 2467 /* 2468 * We use page flags to determine what action should be taken, but 2469 * the flags can be modified by the error containment action. One 2470 * example is an mlocked page, where PG_mlocked is cleared by 2471 * folio_remove_rmap_*() in try_to_unmap_one(). So to determine page 2472 * status correctly, we save a copy of the page flags at this time. 2473 */ 2474 page_flags = folio->flags.f; 2475 2476 /* 2477 * __munlock_folio() may clear a writeback folio's LRU flag without 2478 * the folio lock. We need to wait for writeback completion for this 2479 * folio or it may trigger a vfs BUG while evicting inode. 2480 */ 2481 if (!folio_test_lru(folio) && !folio_test_writeback(folio)) 2482 goto identify_page_state; 2483 2484 /* 2485 * It's very difficult to mess with pages currently under IO 2486 * and in many cases impossible, so we just avoid it here. 2487 */ 2488 folio_wait_writeback(folio); 2489 2490 /* 2491 * Now take care of user space mappings. 2492 * Abort on fail: __filemap_remove_folio() assumes unmapped page. 2493 */ 2494 if (!hwpoison_user_mappings(folio, p, pfn, flags)) { 2495 res = action_result(pfn, MF_MSG_UNMAP_FAILED, MF_FAILED); 2496 goto unlock_page; 2497 } 2498 2499 /* 2500 * Torn down by someone else? 2501 */ 2502 if (folio_test_lru(folio) && !folio_test_swapcache(folio) && 2503 folio->mapping == NULL) { 2504 res = action_result(pfn, MF_MSG_TRUNCATED_LRU, MF_IGNORED); 2505 goto unlock_page; 2506 } 2507 2508 identify_page_state: 2509 res = identify_page_state(pfn, p, page_flags); 2510 mutex_unlock(&mf_mutex); 2511 return res; 2512 unlock_page: 2513 folio_unlock(folio); 2514 unlock_mutex: 2515 mutex_unlock(&mf_mutex); 2516 return res; 2517 } 2518 EXPORT_SYMBOL_GPL(memory_failure); 2519 2520 #define MEMORY_FAILURE_FIFO_ORDER 4 2521 #define MEMORY_FAILURE_FIFO_SIZE (1 << MEMORY_FAILURE_FIFO_ORDER) 2522 2523 struct memory_failure_entry { 2524 unsigned long pfn; 2525 int flags; 2526 }; 2527 2528 struct memory_failure_cpu { 2529 DECLARE_KFIFO(fifo, struct memory_failure_entry, 2530 MEMORY_FAILURE_FIFO_SIZE); 2531 raw_spinlock_t lock; 2532 struct work_struct work; 2533 }; 2534 2535 static DEFINE_PER_CPU(struct memory_failure_cpu, memory_failure_cpu); 2536 2537 /** 2538 * memory_failure_queue - Schedule handling memory failure of a page. 2539 * @pfn: Page Number of the corrupted page 2540 * @flags: Flags for memory failure handling 2541 * 2542 * This function is called by the low level hardware error handler 2543 * when it detects hardware memory corruption of a page. It schedules 2544 * the recovering of error page, including dropping pages, killing 2545 * processes etc. 2546 * 2547 * The function is primarily of use for corruptions that 2548 * happen outside the current execution context (e.g. when 2549 * detected by a background scrubber) 2550 * 2551 * Can run in IRQ context. 2552 */ 2553 void memory_failure_queue(unsigned long pfn, int flags) 2554 { 2555 struct memory_failure_cpu *mf_cpu; 2556 unsigned long proc_flags; 2557 bool buffer_overflow; 2558 struct memory_failure_entry entry = { 2559 .pfn = pfn, 2560 .flags = flags, 2561 }; 2562 2563 mf_cpu = &get_cpu_var(memory_failure_cpu); 2564 raw_spin_lock_irqsave(&mf_cpu->lock, proc_flags); 2565 buffer_overflow = !kfifo_put(&mf_cpu->fifo, entry); 2566 if (!buffer_overflow) 2567 schedule_work_on(smp_processor_id(), &mf_cpu->work); 2568 raw_spin_unlock_irqrestore(&mf_cpu->lock, proc_flags); 2569 put_cpu_var(memory_failure_cpu); 2570 if (buffer_overflow) 2571 pr_err("buffer overflow when queuing memory failure at %#lx\n", 2572 pfn); 2573 } 2574 EXPORT_SYMBOL_GPL(memory_failure_queue); 2575 2576 static void memory_failure_work_func(struct work_struct *work) 2577 { 2578 struct memory_failure_cpu *mf_cpu; 2579 struct memory_failure_entry entry = { 0, }; 2580 unsigned long proc_flags; 2581 int gotten; 2582 2583 mf_cpu = container_of(work, struct memory_failure_cpu, work); 2584 for (;;) { 2585 raw_spin_lock_irqsave(&mf_cpu->lock, proc_flags); 2586 gotten = kfifo_get(&mf_cpu->fifo, &entry); 2587 raw_spin_unlock_irqrestore(&mf_cpu->lock, proc_flags); 2588 if (!gotten) 2589 break; 2590 if (entry.flags & MF_SOFT_OFFLINE) 2591 soft_offline_page(entry.pfn, entry.flags); 2592 else 2593 memory_failure(entry.pfn, entry.flags); 2594 } 2595 } 2596 2597 static int __init memory_failure_init(void) 2598 { 2599 struct memory_failure_cpu *mf_cpu; 2600 int cpu; 2601 2602 for_each_possible_cpu(cpu) { 2603 mf_cpu = &per_cpu(memory_failure_cpu, cpu); 2604 raw_spin_lock_init(&mf_cpu->lock); 2605 INIT_KFIFO(mf_cpu->fifo); 2606 INIT_WORK(&mf_cpu->work, memory_failure_work_func); 2607 } 2608 2609 register_sysctl_init("vm", memory_failure_table); 2610 2611 return 0; 2612 } 2613 core_initcall(memory_failure_init); 2614 2615 #undef pr_fmt 2616 #define pr_fmt(fmt) "Unpoison: " fmt 2617 #define unpoison_pr_info(fmt, pfn, rs) \ 2618 ({ \ 2619 if (__ratelimit(rs)) \ 2620 pr_info(fmt, pfn); \ 2621 }) 2622 2623 /** 2624 * unpoison_memory - Unpoison a previously poisoned page 2625 * @pfn: Page number of the to be unpoisoned page 2626 * 2627 * Software-unpoison a page that has been poisoned by 2628 * memory_failure() earlier. 2629 * 2630 * This is only done on the software-level, so it only works 2631 * for linux injected failures, not real hardware failures 2632 * 2633 * Returns 0 for success, otherwise -errno. 2634 */ 2635 int unpoison_memory(unsigned long pfn) 2636 { 2637 struct folio *folio; 2638 struct page *p; 2639 int ret = -EBUSY, ghp; 2640 unsigned long count; 2641 bool huge = false; 2642 static DEFINE_RATELIMIT_STATE(unpoison_rs, DEFAULT_RATELIMIT_INTERVAL, 2643 DEFAULT_RATELIMIT_BURST); 2644 2645 p = pfn_to_online_page(pfn); 2646 if (!p) 2647 return -EIO; 2648 folio = page_folio(p); 2649 2650 mutex_lock(&mf_mutex); 2651 2652 if (hw_memory_failure) { 2653 unpoison_pr_info("%#lx: disabled after HW memory failure\n", 2654 pfn, &unpoison_rs); 2655 ret = -EOPNOTSUPP; 2656 goto unlock_mutex; 2657 } 2658 2659 if (is_huge_zero_folio(folio)) { 2660 unpoison_pr_info("%#lx: huge zero page is not supported\n", 2661 pfn, &unpoison_rs); 2662 ret = -EOPNOTSUPP; 2663 goto unlock_mutex; 2664 } 2665 2666 if (!PageHWPoison(p)) { 2667 unpoison_pr_info("%#lx: page was already unpoisoned\n", 2668 pfn, &unpoison_rs); 2669 goto unlock_mutex; 2670 } 2671 2672 if (folio_ref_count(folio) > 1) { 2673 unpoison_pr_info("%#lx: someone grabs the hwpoison page\n", 2674 pfn, &unpoison_rs); 2675 goto unlock_mutex; 2676 } 2677 2678 if (folio_test_slab(folio) || folio_test_pgtable(folio) || 2679 folio_test_reserved(folio) || folio_test_offline(folio)) 2680 goto unlock_mutex; 2681 2682 if (folio_mapped(folio)) { 2683 unpoison_pr_info("%#lx: someone maps the hwpoison page\n", 2684 pfn, &unpoison_rs); 2685 goto unlock_mutex; 2686 } 2687 2688 if (folio_mapping(folio)) { 2689 unpoison_pr_info("%#lx: the hwpoison page has non-NULL mapping\n", 2690 pfn, &unpoison_rs); 2691 goto unlock_mutex; 2692 } 2693 2694 ghp = get_hwpoison_page(p, MF_UNPOISON); 2695 if (!ghp) { 2696 if (folio_test_hugetlb(folio)) { 2697 huge = true; 2698 count = folio_free_raw_hwp(folio, false); 2699 if (count == 0) 2700 goto unlock_mutex; 2701 } 2702 ret = folio_test_clear_hwpoison(folio) ? 0 : -EBUSY; 2703 } else if (ghp < 0) { 2704 if (ghp == -EHWPOISON) { 2705 ret = put_page_back_buddy(p) ? 0 : -EBUSY; 2706 } else { 2707 ret = ghp; 2708 unpoison_pr_info("%#lx: failed to grab page\n", 2709 pfn, &unpoison_rs); 2710 } 2711 } else { 2712 if (folio_test_hugetlb(folio)) { 2713 huge = true; 2714 count = folio_free_raw_hwp(folio, false); 2715 if (count == 0) { 2716 folio_put(folio); 2717 goto unlock_mutex; 2718 } 2719 } 2720 2721 folio_put(folio); 2722 if (TestClearPageHWPoison(p)) { 2723 folio_put(folio); 2724 ret = 0; 2725 } 2726 } 2727 2728 unlock_mutex: 2729 mutex_unlock(&mf_mutex); 2730 if (!ret) { 2731 if (!huge) 2732 num_poisoned_pages_sub(pfn, 1); 2733 unpoison_pr_info("%#lx: software-unpoisoned page\n", 2734 page_to_pfn(p), &unpoison_rs); 2735 } 2736 return ret; 2737 } 2738 EXPORT_SYMBOL(unpoison_memory); 2739 2740 #undef pr_fmt 2741 #define pr_fmt(fmt) "Soft offline: " fmt 2742 2743 /* 2744 * soft_offline_in_use_page handles hugetlb-pages and non-hugetlb pages. 2745 * If the page is a non-dirty unmapped page-cache page, it simply invalidates. 2746 * If the page is mapped, it migrates the contents over. 2747 */ 2748 static int soft_offline_in_use_page(struct page *page) 2749 { 2750 long ret = 0; 2751 unsigned long pfn = page_to_pfn(page); 2752 struct folio *folio = page_folio(page); 2753 char const *msg_page[] = {"page", "hugepage"}; 2754 bool huge = folio_test_hugetlb(folio); 2755 bool isolated; 2756 LIST_HEAD(pagelist); 2757 struct migration_target_control mtc = { 2758 .nid = NUMA_NO_NODE, 2759 .gfp_mask = GFP_USER | __GFP_MOVABLE | __GFP_RETRY_MAYFAIL, 2760 .reason = MR_MEMORY_FAILURE, 2761 }; 2762 2763 if (!huge && folio_test_large(folio)) { 2764 if (try_to_split_thp_page(page, true)) { 2765 pr_info("%#lx: thp split failed\n", pfn); 2766 return -EBUSY; 2767 } 2768 folio = page_folio(page); 2769 } 2770 2771 folio_lock(folio); 2772 if (!huge) 2773 folio_wait_writeback(folio); 2774 if (PageHWPoison(page)) { 2775 folio_unlock(folio); 2776 folio_put(folio); 2777 pr_info("%#lx: page already poisoned\n", pfn); 2778 return 0; 2779 } 2780 2781 if (!huge && folio_test_lru(folio) && !folio_test_swapcache(folio)) 2782 /* 2783 * Try to invalidate first. This should work for 2784 * non dirty unmapped page cache pages. 2785 */ 2786 ret = mapping_evict_folio(folio_mapping(folio), folio); 2787 folio_unlock(folio); 2788 2789 if (ret) { 2790 pr_info("%#lx: invalidated\n", pfn); 2791 page_handle_poison(page, false, true); 2792 return 0; 2793 } 2794 2795 isolated = isolate_folio_to_list(folio, &pagelist); 2796 2797 /* 2798 * If we succeed to isolate the folio, we grabbed another refcount on 2799 * the folio, so we can safely drop the one we got from get_any_page(). 2800 * If we failed to isolate the folio, it means that we cannot go further 2801 * and we will return an error, so drop the reference we got from 2802 * get_any_page() as well. 2803 */ 2804 folio_put(folio); 2805 2806 if (isolated) { 2807 ret = migrate_pages(&pagelist, alloc_migration_target, NULL, 2808 (unsigned long)&mtc, MIGRATE_SYNC, MR_MEMORY_FAILURE, NULL); 2809 if (!ret) { 2810 bool release = !huge; 2811 2812 if (!page_handle_poison(page, huge, release)) 2813 ret = -EBUSY; 2814 } else { 2815 if (!list_empty(&pagelist)) 2816 putback_movable_pages(&pagelist); 2817 2818 pr_info("%#lx: %s migration failed %ld, type %pGp\n", 2819 pfn, msg_page[huge], ret, &page->flags.f); 2820 if (ret > 0) 2821 ret = -EBUSY; 2822 } 2823 } else { 2824 pr_info("%#lx: %s isolation failed, page count %d, type %pGp\n", 2825 pfn, msg_page[huge], page_count(page), &page->flags.f); 2826 ret = -EBUSY; 2827 } 2828 return ret; 2829 } 2830 2831 /** 2832 * soft_offline_page - Soft offline a page. 2833 * @pfn: pfn to soft-offline 2834 * @flags: flags. Same as memory_failure(). 2835 * 2836 * Returns 0 on success, 2837 * -EOPNOTSUPP for hwpoison_filter() filtered the error event, or 2838 * disabled by /proc/sys/vm/enable_soft_offline, 2839 * < 0 otherwise negated errno. 2840 * 2841 * Soft offline a page, by migration or invalidation, 2842 * without killing anything. This is for the case when 2843 * a page is not corrupted yet (so it's still valid to access), 2844 * but has had a number of corrected errors and is better taken 2845 * out. 2846 * 2847 * The actual policy on when to do that is maintained by 2848 * user space. 2849 * 2850 * This should never impact any application or cause data loss, 2851 * however it might take some time. 2852 * 2853 * This is not a 100% solution for all memory, but tries to be 2854 * ``good enough'' for the majority of memory. 2855 */ 2856 int soft_offline_page(unsigned long pfn, int flags) 2857 { 2858 int ret; 2859 bool try_again = true; 2860 struct page *page; 2861 2862 if (!pfn_valid(pfn)) { 2863 WARN_ON_ONCE(flags & MF_COUNT_INCREASED); 2864 return -ENXIO; 2865 } 2866 2867 /* Only online pages can be soft-offlined (esp., not ZONE_DEVICE). */ 2868 page = pfn_to_online_page(pfn); 2869 if (!page) { 2870 put_ref_page(pfn, flags); 2871 return -EIO; 2872 } 2873 2874 if (!sysctl_enable_soft_offline) { 2875 pr_info_once("disabled by /proc/sys/vm/enable_soft_offline\n"); 2876 put_ref_page(pfn, flags); 2877 return -EOPNOTSUPP; 2878 } 2879 2880 mutex_lock(&mf_mutex); 2881 2882 if (PageHWPoison(page)) { 2883 pr_info("%#lx: page already poisoned\n", pfn); 2884 put_ref_page(pfn, flags); 2885 mutex_unlock(&mf_mutex); 2886 return 0; 2887 } 2888 2889 retry: 2890 get_online_mems(); 2891 ret = get_hwpoison_page(page, flags | MF_SOFT_OFFLINE); 2892 put_online_mems(); 2893 2894 if (hwpoison_filter(page)) { 2895 if (ret > 0) 2896 put_page(page); 2897 2898 mutex_unlock(&mf_mutex); 2899 return -EOPNOTSUPP; 2900 } 2901 2902 if (ret > 0) { 2903 ret = soft_offline_in_use_page(page); 2904 } else if (ret == 0) { 2905 if (!page_handle_poison(page, true, false)) { 2906 if (try_again) { 2907 try_again = false; 2908 flags &= ~MF_COUNT_INCREASED; 2909 goto retry; 2910 } 2911 ret = -EBUSY; 2912 } 2913 } 2914 2915 mutex_unlock(&mf_mutex); 2916 2917 return ret; 2918 } 2919