[PATCH 3/6] mm/memory-failure: keep the page online while handling it
From: Gregory Price (Meta)
Date: Tue Oct 27 2026 - 00:24:10 EST
memory_failure(), soft_offline_page() and unpoison_memory() check that a
pfn is online, then sleep on mf_mutex before using its page. If the
block goes offline meanwhile, a DEBUG_VM kernel faults on the poisoned
memmap, and removal frees it on any kernel:
CPU 1 (soft_offline_page) CPU 2 (offline_pages)
pfn_to_online_page()
page is online
mutex_lock(&mf_mutex)
offline_mem_sections()
remove_pfn_range_from_zone()
page_init_poison()
get_hwpoison_page()
zone_pcp_disable(page_zone(page))
zone of a poisoned page
Oops: general protection fault, probably for non-canonical address 0xdffffc000000069e: 0000 [#1] SMP KASAN
RIP: 0010:__zone_set_pageset_high_and_batch+0x89/0x270
zone_pcp_disable+0x28/0x40
get_hwpoison_page+0xc2/0x1160
soft_offline_page.part.0+0xe7/0xbf0
memory_failure() faults the same way. unpoison_memory() does not fault,
but it clears flags on the poisoned page and returns success.
Hold get_online_mems() from before the online check to the last use of
the page. soft_offline_page() took it only around get_hwpoison_page();
it now takes it once, earlier.
With a kprobe parking each entry point across the offline, base faults,
or for unpoison_memory() succeeds, in 3 of 3 runs each. With this patch
all 9 runs wait and fail with -EIO or -ENXIO, and lockdep stays clean
over offline/online cycles mixed with error injection.
Fixes: 96c804a6ae8c ("mm/memory-failure.c: don't access uninitialized memmaps in memory_failure()")
Fixes: feec24a6139d ("mm, soft-offline: convert parameter to pfn")
Fixes: d613f53c83ec ("mm/memory-failure: fix VM_BUG_ON_PAGE(PagePoisoned(page)) when unpoison memory")
Cc: stable@xxxxxxxxxxxxxxx
Signed-off-by: Gregory Price (Meta) <gourry@xxxxxxxxxx>
---
mm/memory-failure.c | 31 ++++++++++++++++++++++---------
1 file changed, 22 insertions(+), 9 deletions(-)
diff --git a/mm/memory-failure.c b/mm/memory-failure.c
index 0c96eb5119976..cdae2a1988bb8 100644
--- a/mm/memory-failure.c
+++ b/mm/memory-failure.c
@@ -2394,6 +2394,8 @@ int memory_failure(unsigned long pfn, int flags)
if (!sysctl_memory_failure_recovery)
panic("Memory failure on page %lx", pfn);
+ /* Keep the page online from the check below until we are done */
+ get_online_mems();
mutex_lock(&mf_mutex);
if (!(flags & MF_SW_SIMULATED))
@@ -2608,11 +2610,13 @@ int memory_failure(unsigned long pfn, int flags)
identify_page_state:
res = identify_page_state(pfn, p, page_flags);
mutex_unlock(&mf_mutex);
+ put_online_mems();
return res;
unlock_page:
folio_unlock(folio);
unlock_mutex:
mutex_unlock(&mf_mutex);
+ put_online_mems();
return res;
}
EXPORT_SYMBOL_GPL(memory_failure);
@@ -2742,9 +2746,12 @@ int unpoison_memory(unsigned long pfn)
static DEFINE_RATELIMIT_STATE(unpoison_rs, DEFAULT_RATELIMIT_INTERVAL,
DEFAULT_RATELIMIT_BURST);
+ get_online_mems();
p = pfn_to_online_page(pfn);
- if (!p)
+ if (!p) {
+ put_online_mems();
return -EIO;
+ }
folio = page_folio(p);
mutex_lock(&mf_mutex);
@@ -2833,6 +2840,7 @@ int unpoison_memory(unsigned long pfn)
unpoison_pr_info("%#lx: software-unpoisoned page\n",
page_to_pfn(p), &unpoison_rs);
}
+ put_online_mems();
return ret;
}
EXPORT_SYMBOL(unpoison_memory);
@@ -2974,38 +2982,42 @@ int soft_offline_page(unsigned long pfn, int flags)
return -ENXIO;
}
+ if (!sysctl_enable_soft_offline) {
+ pr_info_once("disabled by /proc/sys/vm/enable_soft_offline\n");
+ put_ref_page(pfn, flags);
+ return -EOPNOTSUPP;
+ }
+
+ /* Keep the page online from the check below until we are done */
+ get_online_mems();
+
/* Only online pages can be soft-offlined (esp., not ZONE_DEVICE). */
page = pfn_to_online_page(pfn);
if (!page) {
+ put_online_mems();
put_ref_page(pfn, flags);
return -EIO;
}
- if (!sysctl_enable_soft_offline) {
- pr_info_once("disabled by /proc/sys/vm/enable_soft_offline\n");
- put_ref_page(pfn, flags);
- return -EOPNOTSUPP;
- }
-
mutex_lock(&mf_mutex);
if (PageHWPoison(page)) {
pr_info("%#lx: page already poisoned\n", pfn);
put_ref_page(pfn, flags);
mutex_unlock(&mf_mutex);
+ put_online_mems();
return 0;
}
retry:
- get_online_mems();
ret = get_hwpoison_page(page, flags | MF_SOFT_OFFLINE);
- put_online_mems();
if (hwpoison_filter(page)) {
if (ret > 0)
put_page(page);
mutex_unlock(&mf_mutex);
+ put_online_mems();
return -EOPNOTSUPP;
}
@@ -3023,6 +3035,7 @@ int soft_offline_page(unsigned long pfn, int flags)
}
mutex_unlock(&mf_mutex);
+ put_online_mems();
return ret;
}
--
2.53.0-Meta