Skip to contentSkip to conversation

mm: don't migrate folios containing hwpoisoned pages

PATCH Superseded Yuanhe Shu 4 participants 18 messages Sep 28 – Oct 26

Subsystems (3)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Yuanhe Shu

author
#0 lore raw

cover mm: don't migrate folios containing hwpoisoned pages

Hello,

memory_failure() can end up abandoning a folio while PG_hwpoison is already set on it. Two ways to get there:

1. Race: any isolator (compaction, NUMA balancing, move_pages(), mbind(), CMA, long-term GUP) clears PG_lru from under memory_failure(), which then fails the HWPoisonHandlable() check, exhausts the get_any_page() retries and bails out with MF_IGNORED - without holding any reference, but with the flag set.
2. No race at all: the THP-split-failure path (kill_procs_now() + MF_MSG_UNSPLIT_THP) leaves a large folio on the LRU with a PG_hwpoisoned subpage by design.

In both cases the migrator still holds the folio on its migration list and migrates it like any other: the corrupted data is copied into a fresh folio with no poison marker, and no hwpoison PTE entry is installed on the way back, so the userspace mapping is silently redirected to the corrupted copy - no SIGBUS is ever delivered.

This was reproduced on v7.3-rc4-75-g62f4c998b297 (the base of this series): an order-0 stress workload (madvise(MADV_HWPOISON) racing move_pages()) left the poison flag set on 8426 abandoned folios in a 180-second run; 698 of them were subsequently migrated, and in every case the address then served the copied corrupted data with no SIGBUS.

folio_mc_copy() only catches the corruption at copy time, and only where ARCH_HAS_COPY_MC is implemented (x86_64, PPC64; elsewhere copy_mc_highpage() degrades to a plain copy). Software-injected poison - what MADV_HWPOISON and hwpoison-inject produce, and what tests and fuzzers exercise - never traps during the copy on any architecture. Checking before the copy avoids the migration entirely, on all architectures, for real and simulated poison alike.

Consumers of such a folio already refuse it elsewhere,
through the folio_contain_hwpoisoned_page() helper introduced by
commit 5f5ee52d4f58 ("mm/hwpoison: introduce folio_contain_hwpoisoned_page() helper"):
shmem fails writes to a poisoned folio; reclaim unmaps the
order-0 poisoned page and skips the hwpoisoned large folios it
cannot safely unmap
commit 1b0449544c64 ("mm/vmscan: don't try to reclaim hwpoison folio") commit 9f1e8cd0b7c4 ("mm/vmscan: fix hwpoisoned large folio handling in shrink_folio_list") (skipping, not unmapping, after the unmap variant turned out to
crash on large folios); memory hotplug refuses to migrate it in do_migrate_range(); and the THP split path refuses to read
poisoned subpages
commit 841a8bfcbad9 ("mm: prevent poison consumption when splitting THP"). This series gives the remaining migrators
the same protection - skipping and refusing only, never unmapping:

patch 1: compaction, checked at the isolation point, failing
fast - patch 2's core check would also stop compaction's migrations at migrate_pages() time, but only after the avoidable isolation work

patch 2: the migration core, covering move_pages(2), mbind(),
NUMA balancing, alloc_contig_range() (CMA) and long-term GUP - a check at the entry of migrate_folio_unmap() (fail fast, before allocating the destination) plus a second check in migrate_folio_move() immediately before the copy. The pre-copy check catches poison that lands between the two checks: a GUP-based injection path takes its reference before the unmap, and memory_failure() sets PG_hwpoison before releasing it, so a racing software poison is visible by the time migration is about to copy.

This series was developed independently; while preparing to send it, we noticed that Kaitao Cheng had already proposed the same check for the hugetlb migration path, and in v2 for move_to_new_folio() [1]. That discussion stalled over the concern that a check can still race with memory_failure(). Two points complete it here: the unsplit-THP state above is not a race at all - the entry check catches it deterministically - and for the race itself, the pre-copy check shrinks the window to the final instructions before the copy; the measurement below found zero leaks under adversarial load, and on copy_mc architectures folio_mc_copy() catches real-DRAM corruption even there. The reproducer and the userspace-visible effects are what that thread's review asked for.

[1] https://lore.kernel.org/r/[email protected]/

Measured results:

- order-0 race against move_pages(): the unpatched kernel propagated 698 of 8426 abandoned folios; with patch 2, 0 of 9711 (the abandoned-folio count varies between runs with the race rate).

- deterministic compaction A/B (order-4 mTHP folios brought into the stable unsplit state, clean neighbors as controls): the unpatched kernel migrated 16/16 poisoned folios and 16/16 controls; with patch 1, 0/16 poisoned folios were migrated while the controls still migrated 16/16.

The two patches touch different files, have no build dependency, and each stands on its own.

No single Fixes: tag applies: the race path is as old as the memory_failure() retry logic meeting page isolation, and the unsplit-THP path has existed since commit
5d1fd5dc877b ("mm,hwpoison: introduce MF_MSG_UNSPLIT_THP") (2020) - this series closes a long-standing gap rather than fixing a regression. No cc:stable is proposed for the same reason; backports can be prepared on request if wanted.

Soft offline is unaffected by both patches: it sets the poison marker only after its own migration has succeeded. Out of reach of any check is poison landing in the final instructions before a copy - natively for paths that take no reference, such as a hardware MCE or debugfs corrupt-pfn; for a GUP-based path only if its injecting thread is delayed between taking the reference and setting the flag - or after the copy has already happened, which is a lost race regardless; on x86_64 folio_mc_copy() additionally catches real-DRAM corruption at copy time.

A deterministic reproducer exists for the compaction part: fault order-4 mTHP folios, pin a sibling subpage of each with vmsplice, and inject MADV_HWPOISON on another subpage, so memory_failure() takes the THP-split-failure path and leaves the folio on the LRU with a poisoned subpage; then trigger compaction and check /proc/kpageflags for whether each poisoned folio moved. It needs a boot configuration where order-4, not PMD-sized, folios are what compaction migrates. Both this and the race workload above can be shared on request.

Yuanhe Shu (2):
mm/compaction: skip folios containing hwpoisoned pages mm/migrate: refuse to migrate folios containing hwpoisoned pages

 mm/compaction.c |  8 ++++++++
 mm/migrate.c    | 41 +++++++++++++++++++++++++++++++++++++++--
 2 files changed, 47 insertions(+), 2 deletions(-)
Signature · 1 line
2.43.7
author
#1 lore raw

PATCH 1/2 mm/compaction: skip folios containing hwpoisoned pages

memory_failure() sets PG_hwpoison on the error page before it manages to pin the folio via get_hwpoison_page(). Compaction running on another CPU can isolate that same folio in the meantime, so HWPoisonHandlable() then fails on !PageLRU(), the retry loop in get_any_page() exhausts itself and memory_failure() gives up with MF_IGNORED - while compaction still has the folio on its migratepages list, flagged PG_hwpoison.

Nothing stops compaction from migrating such a folio afterwards. The corrupted data is copied into a fresh folio that carries no poison marker, and no hwpoison PTE entry is installed on the way
back: the userspace mapping is silently redirected to the corrupted copy and no SIGBUS is ever delivered.

Note that a large folio with a PG_hwpoisoned subpage can also sit on the LRU without any race at all, via the THP-split-failure path of memory_failure() (kill_procs_now() + MF_MSG_UNSPLIT_THP). Guarding compaction is therefore needed regardless of how the race in memory_failure() itself might be addressed.

folio_mc_copy() does not close this hole: it only detects corruption at copy time and only where ARCH_HAS_COPY_MC is implemented (x86_64 and PPC64; elsewhere copy_mc_highpage() degrades to a plain copy). Software-injected poison - what MADV_HWPOISON and the hwpoison-inject interface produce, and what tests and fuzzers exercise - never traps during the copy on any architecture. An isolation-time check avoids the migration entirely, on all architectures and for both real and simulated poison.
The check itself is two flag tests on the head page (PG_hwpoison and the folio-level has_hwpoisoned marker), not a walk over subpages.

Neither page reclaim nor memory hotplug lets such a folio be
copied into a fresh one: reclaim unmaps the poisoned order-0 page,
and skips the hwpoisoned large folios it cannot safely unmap, in shrink_folio_list()
commit 1b0449544c64 ("mm/vmscan: don't try to reclaim hwpoison folio") commit 9f1e8cd0b7c4 ("mm/vmscan: fix hwpoisoned large folio handling in shrink_folio_list") while memory hotplug refuses to migrate them in do_migrate_range()
commit 5f5ee52d4f58 ("mm/hwpoison: introduce folio_contain_hwpoisoned_page() helper"). Skip them in compaction as well, for the same reason reclaim settled
on skipping: the UCE is rare and a race with compaction is rarer
still, so skipping is enough, and a later memory_failure() will
handle the folio if the UCE is triggered again - while a migrated
folio would silently propagate the corruption instead.

Deterministic validation on v7.3-rc4-75-g62f4c998b297: order-4 mTHP folios were brought into the stable "large, on LRU, PG_hwpoisoned subpage" state by pinning a sibling subpage with vmsplice and injecting MADV_HWPOISON on another subpage, which makes memory_failure() take the THP-split-failure path; after triggering compaction, /proc/kpageflags shows which poisoned folios moved. The unpatched kernel migrated 16/16 poisoned folios and 16/16 clean controls in the same pageblocks; with this patch, 0/16 poisoned folios were migrated while their controls still migrated 16/16.

Soft offline is not affected: it only sets the poison marker after its own migration has succeeded.

---
 mm/compaction.c | 8 ++++++++
 1 file changed, 8 insertions(+)
mm/compaction.c +8 -0
index a049415512c6..491adcbc5313 100644
@@ -1093,6 +1093,14 @@ isolate_migratepages_block(struct compact_control *cc, unsigned long low_pfn,
 		if (unlikely(!folio))
 			goto isolate_fail;
 
+		/*
+		 * Migrating would copy the corrupted data into a fresh
+		 * folio with no poison marker; skip, as memory hotplug
+		 * refuses to migrate such folios for the same reason.
+		 */
+		if (folio_contain_hwpoisoned_page(folio))
+			goto isolate_fail_put;
+
 		/*
 		 * Migration will fail if an anonymous page is pinned in memory,
 		 * so avoid taking lru_lock and isolating it unnecessarily in an
Signature · 1 line
2.43.7
#2 lore raw

Re: [PATCH 1/2] mm/compaction: skip folios containing hwpoisoned pages

Quoted 20 lines · Yuanhe Shu

memory_failure() sets PG_hwpoison on the error page before it manages to pin the folio via get_hwpoison_page(). Compaction running on another CPU can isolate that same folio in the meantime, so HWPoisonHandlable() then fails on !PageLRU(), the retry loop in get_any_page() exhausts itself and memory_failure() gives up with MF_IGNORED - while compaction still has the folio on its migratepages list, flagged PG_hwpoison.

Nothing stops compaction from migrating such a folio afterwards. The corrupted data is copied into a fresh folio that carries no poison marker, and no hwpoison PTE entry is installed on the way
back: the userspace mapping is silently redirected to the corrupted copy and no SIGBUS is ever delivered.

Note that a large folio with a PG_hwpoisoned subpage can also sit on the LRU without any race at all, via the THP-split-failure path of memory_failure() (kill_procs_now() + MF_MSG_UNSPLIT_THP). Guarding compaction is therefore needed regardless of how the race in memory_failure() itself might be addressed.

Quoted 3 lines

folio_mc_copy() does not close this hole: it only detects corruption at copy time and only where ARCH_HAS_COPY_MC is implemented (x86_64 and PPC64; elsewhere copy_mc_highpage() degrades to a plain copy).

Then they should implement it.

Quoted 3 lines

Software-injected poison - what MADV_HWPOISON and the hwpoison-inject interface produce, and what tests and fuzzers exercise - never traps during the copy on any architecture.

And these are debug interfaces, why do we care?

Quoted 3 lines

An isolation-time check avoids
the migration entirely, on all architectures and for both real and simulated poison.

It's racy. See the link below.

Quoted 18 lines

The check itself is two flag tests on the head page (PG_hwpoison and the folio-level has_hwpoisoned marker), not a walk over subpages.

Neither page reclaim nor memory hotplug lets such a folio be
copied into a fresh one: reclaim unmaps the poisoned order-0 page,
and skips the hwpoisoned large folios it cannot safely unmap, in shrink_folio_list()
commit 1b0449544c64 ("mm/vmscan: don't try to reclaim hwpoison folio") commit 9f1e8cd0b7c4 ("mm/vmscan: fix hwpoisoned large folio handling in shrink_folio_list") while memory hotplug refuses to migrate them in do_migrate_range()
commit 5f5ee52d4f58 ("mm/hwpoison: introduce folio_contain_hwpoisoned_page() helper"). Skip them in compaction as well, for the same reason reclaim settled
on skipping: the UCE is rare and a race with compaction is rarer
still, so skipping is enough, and a later memory_failure() will
handle the folio if the UCE is triggered again - while a migrated
folio would silently propagate the corruption instead.

Quoted 10 lines

Deterministic validation on v7.3-rc4-75-g62f4c998b297: order-4 mTHP folios were brought into the stable "large, on LRU, PG_hwpoisoned subpage" state by pinning a sibling subpage with vmsplice and injecting MADV_HWPOISON on another subpage, which makes memory_failure() take the THP-split-failure path; after triggering compaction, /proc/kpageflags shows which poisoned folios moved. The unpatched kernel migrated 16/16 poisoned folios and 16/16 clean controls in the same pageblocks; with this patch, 0/16 poisoned folios were migrated while their controls still migrated 16/16.

Was any of this written by an LLM?

Quoted 18 lines

Soft offline is not affected: it only sets the poison marker after its own migration has succeeded.

---
 mm/compaction.c | 8 ++++++++
 1 file changed, 8 insertions(+)
mm/compaction.c +1 -0
index a049415512c6..491adcbc5313 100644
@@ -1093,6 +1093,14 @@ isolate_migratepages_block(struct compact_control *cc, unsigned long low_pfn,
 		if (unlikely(!folio))
 			goto isolate_fail;
 
+		/*

mm/compaction.c isolate_migratepages_block(struct compact_control *cc, unsigned long low_pfn, · line 1106

Quoted 10 lines
mm/compaction.c +7 -0
+		 * Migrating would copy the corrupted data into a fresh
+		 * folio with no poison marker; skip, as memory hotplug
+		 * refuses to migrate such folios for the same reason.
+		 */
+		if (folio_contain_hwpoisoned_page(folio))
+			goto isolate_fail_put;
+
 		/*
 		 * Migration will fail if an anonymous page is pinned in memory,
 		 * so avoid taking lru_lock and isolating it unnecessarily in an

See

https://lore.kernel.org/linux-mm/[email protected]/

Signature · 3 lines
Cheers,

David
author
#3 lore raw

Re: [PATCH 1/2] mm/compaction: skip folios containing hwpoisoned pages

Quoted 6 lines · David Hildenbrand
Quoted 3 lines · Yuanhe Shu

folio_mc_copy() does not close this hole: it only detects corruption at copy time and only where ARCH_HAS_COPY_MC is implemented (x86_64 and PPC64; elsewhere copy_mc_highpage() degrades to a plain copy).

Then they should implement it.

Agreed, and nothing here replaces copy_mc. Two things the check adds while that work is pending: on archs without copy_mc the plain copy does not fail, it consumes the poison - the same poison consumption commit 841a8bfcbad9 ("mm: prevent poison consumption when splitting THP") refused to risk on the split path, at your suggestion; and the refusal happens before the copy is attempted at all, while copy_mc recovers only during the read. The check and copy_mc compose.

Quoted 5 lines
Quoted 3 lines

Software-injected poison - what MADV_HWPOISON and the hwpoison-inject
interface produce, and what tests and fuzzers exercise - never traps during the copy on any architecture.

And these are debug interfaces, why do we care?

Because they are how this bug class gets found and reached in
practice: the reclaim-side check this series builds on (1b0449544c64) exists because of a syzkaller report through these interfaces, and carries Cc: stable - as does its large-folio follow-up (9f1e8cd0b7c4), which you Acked. You were also cc'd when Andrew raised the urgency on the hugetlb sibling of this fix for the same reason - "perhaps Bad Guys can find a way of triggering this, so the urgency becomes higher".

The window itself is not debug-only either: a real UCE landing in it behaves identically, and on a non-copy_mc arch that copy consumes real poison instead of failing.

Quoted 5 lines
Quoted 3 lines

An isolation-time check avoids
the migration entirely, on all architectures and for both real and simulated poison.

It's racy. See the link below.

In isolation, yes - and dealing with that race is what patch 2 is for, which is why this is a series. The steady state (split failure leaves the folio on the LRU with the flag already set) is not a race at all, and that is what the isolation-time check catches
deterministically: 16/16 such folios migrated unpatched, 0/16 with it. For poison racing the batch, the load-bearing check is the one patch 2 adds, immediately before the copy. In patch 2's racing workload, the entry check alone still propagated 693 of 9606 - that datapoint is exactly why the second check is there. A GUP-based path takes its reference before the unmap, and memory_failure() sets PG_hwpoison before releasing it, so a racing injection is visible by copy time, unless the injecting thread is delayed between taking the reference and setting the flag. A real MCE can likewise land in the final instructions before the copy; on x86_64 copy_mc catches that one at the read.

That thread is linked from patch 2's changelog precisely because the concern you raised there is what the pre-copy check tries to answer. The patch 1 sentence you quoted overstates the isolation check on its own - I'll scope it to the already-flagged case in a v2.

Quoted 1 line

Was any of this written by an LLM?

The patches, the reproducer and the measurements are mine. I used an LLM mainly for the prose, to keep it accurate and unambiguous, and also to look up related patches and past discussions on the mailing lists. I verified every technical claim against the code and the test logs. Fair point on the missing tag - v2 will carry

Assisted-by LLM.
Signature · 1 line
Yuanhe
reviewer
#4 lore raw

Re: [PATCH 1/2] mm/compaction: skip folios containing hwpoisoned pages

Quoted 8 lines · Yuanhe Shu
Quoted 6 lines · David Hildenbrand
Quoted 3 lines · Yuanhe Shu

folio_mc_copy() does not close this hole: it only detects corruption at copy time and only where ARCH_HAS_COPY_MC is implemented (x86_64 and PPC64; elsewhere copy_mc_highpage() degrades to a plain copy).

Then they should implement it.

Quoted 8 lines

Agreed, and nothing here replaces copy_mc. Two things the check adds while that work is pending: on archs without copy_mc the plain copy does not fail, it consumes the poison - the same poison consumption commit 841a8bfcbad9 ("mm: prevent poison consumption when splitting THP") refused to risk on the split path, at your suggestion; and the refusal happens before the copy is attempted at all, while copy_mc recovers only during the read. The check and copy_mc compose.

Is it good to implement the trick in copy_mc_user_highpage() and copy_mc_highpage() if the macro copy_mc_to_kernel is undefined?

---
Best Regards,
Huang, Ying

author
#5 lore raw

Re: [PATCH 1/2] mm/compaction: skip folios containing hwpoisoned pages

Quoted 2 lines · Ying

Is it good to implement the trick in copy_mc_user_highpage() and copy_mc_highpage() if the macro copy_mc_to_kernel is undefined?

Sounds good. That also covers the non-migration callers of the fallbacks (COW, khugepaged, KSM), and composes with a real copy_mc implementation for actual MCEs. I'll prepare it as a separate patch with your Suggested-by, referenced from the v2 cover letter.

Quoted 4 lines · Kaitao Cheng

Could we recheck PG_hwpoison on the source page after folio_mc_copy() completes, but before the mapping is transferred, and abort migration if the flag is set? This would prevent PG_hwpoison from being lost after migration.

Patch 2 already rechecks before the copy - your v2 check, moved a few lines earlier. What it doesn't cover is the copy window itself: the debugfs corrupt-pfn injector has no GUP ordering and can set the flag mid-copy. Happy to carry the post-copy recheck in v2 as well.

Signature · 1 line
Yuanhe
#6 lore raw

Re: [PATCH 1/2] mm/compaction: skip folios containing hwpoisoned pages

In reply to Huang, Ying
Quoted 15 lines · Ying
Quoted 4 lines · Yuanhe Shu
Quoted 2 lines · David Hildenbrand

Then they should implement it.

Quoted 8 lines

Agreed, and nothing here replaces copy_mc. Two things the check adds while that work is pending: on archs without copy_mc the plain copy does not fail, it consumes the poison - the same poison consumption commit 841a8bfcbad9 ("mm: prevent poison consumption when splitting THP") refused to risk on the split path, at your suggestion; and the refusal happens before the copy is attempted at all, while copy_mc recovers only during the read. The check and copy_mc compose.

Quoted 2 lines

Is it good to implement the trick in copy_mc_user_highpage() and copy_mc_highpage() if the macro copy_mc_to_kernel is undefined?

That is indeed better.

Signature · 3 lines
Cheers,

David
#7 lore raw

Re: [PATCH 1/2] mm/compaction: skip folios containing hwpoisoned pages

In reply to Huang, Ying
Quoted 19 lines · Ying
Quoted 8 lines · Yuanhe Shu
Quoted 6 lines · David Hildenbrand
Quoted 3 lines · Yuanhe Shu

folio_mc_copy() does not close this hole: it only detects corruption at copy time and only where ARCH_HAS_COPY_MC is implemented (x86_64 and PPC64; elsewhere copy_mc_highpage() degrades to a plain copy).

Then they should implement it.

Quoted 8 lines

Agreed, and nothing here replaces copy_mc. Two things the check adds while that work is pending: on archs without copy_mc the plain copy does not fail, it consumes the poison - the same poison consumption commit 841a8bfcbad9 ("mm: prevent poison consumption when splitting THP") refused to risk on the split path, at your suggestion; and the refusal happens before the copy is attempted at all, while copy_mc recovers only during the read. The check and copy_mc compose.

Quoted 2 lines

Is it good to implement the trick in copy_mc_user_highpage() and copy_mc_highpage() if the macro copy_mc_to_kernel is undefined?

On architectures with hardware error correction and exception recovery support, accessing faulty memory automatically triggers an exception, which itself guarantees atomicity.

On architectures without such support, even if we implement this logic in copy_mc_user_highpage() and copy_mc_highpage(), checking the hwpoison flag and copying the data remain two separate operations. They cannot be performed atomically, so there is still a race window between them, however small.

Moving the hwpoison check after the copy might address this issue. Is there anything wrong with my understanding?

Quoted 3 lines

---
Best Regards,
Huang, Ying

Signature · 2 lines
Thanks
Kaitao Cheng
#8 lore raw

Re: [PATCH 1/2] mm/compaction: skip folios containing hwpoisoned pages

Quoted 22 lines · Kaitao Cheng
Quoted 12 lines · Ying
Quoted 9 lines · Yuanhe Shu

Agreed, and nothing here replaces copy_mc. Two things the check adds while that work is pending: on archs without copy_mc the plain copy does not fail, it consumes the poison - the same poison consumption commit 841a8bfcbad9 ("mm: prevent poison consumption when splitting THP") refused to risk on the split path, at your suggestion; and the refusal happens before the copy is attempted at all, while copy_mc recovers only during the read. The check and copy_mc compose.

Quoted 2 lines

Is it good to implement the trick in copy_mc_user_highpage() and copy_mc_highpage() if the macro copy_mc_to_kernel is undefined?

On architectures with hardware error correction and exception recovery support, accessing faulty memory automatically triggers an exception, which itself guarantees atomicity.

Quoted 5 lines

On architectures without such support, even if we implement this logic in copy_mc_user_highpage() and copy_mc_highpage(), checking the hwpoison flag and copying the data remain two separate operations. They cannot be performed atomically, so there is still a race window between them, however small.

Yes. And you need proper architecture support to fix it?

Quoted 3 lines

Moving the hwpoison check after the copy might address this issue. Is there anything wrong with my understanding?

Will architectures that are not prepared for copy_mc_user_highpage() not simply crash if they trigger an MCE during an access?

Signature · 3 lines
Cheers,

David
#9 lore raw

Re: [PATCH 1/2] mm/compaction: skip folios containing hwpoisoned pages

Quoted 23 lines · David Hildenbrand (Arm)
Quoted 22 lines · Kaitao Cheng
Quoted 12 lines · Ying
Quoted 9 lines · Yuanhe Shu

Agreed, and nothing here replaces copy_mc. Two things the check adds while that work is pending: on archs without copy_mc the plain copy does not fail, it consumes the poison - the same poison consumption commit 841a8bfcbad9 ("mm: prevent poison consumption when splitting THP") refused to risk on the split path, at your suggestion; and the refusal happens before the copy is attempted at all, while copy_mc recovers only during the read. The check and copy_mc compose.

Quoted 2 lines

Is it good to implement the trick in copy_mc_user_highpage() and copy_mc_highpage() if the macro copy_mc_to_kernel is undefined?

On architectures with hardware error correction and exception recovery support, accessing faulty memory automatically triggers an exception, which itself guarantees atomicity.

Quoted 7 lines
Quoted 5 lines

On architectures without such support, even if we implement this logic in copy_mc_user_highpage() and copy_mc_highpage(), checking the hwpoison flag and copying the data remain two separate operations. They cannot be performed atomically, so there is still a race window between them, however small.

Yes. And you need proper architecture support to fix it?

It does require architecture support, but that doesn't seem practical, haha.

Quoted 6 lines
Quoted 2 lines

Moving the hwpoison check after the copy might address this issue. Is there anything wrong with my understanding?

Will architectures that are not prepared for copy_mc_user_highpage() not simply crash if they trigger an MCE during an access?

Do we need to check both before and after the copy? This feels a bit redundant, and I'm not sure whether such a small race window warrants an additional check.

Signature · 2 lines
Thanks
Kaitao Cheng
#10 lore raw

Re: [PATCH 1/2] mm/compaction: skip folios containing hwpoisoned pages

Quoted 18 lines · Kaitao Cheng
Quoted 15 lines · David Hildenbrand (Arm)
Quoted 13 lines · Kaitao Cheng

在 2026/10/11 21:14, Huang, Ying 写道:

On architectures with hardware error correction and exception recovery support, accessing faulty memory automatically triggers an exception, which itself guarantees atomicity.

On architectures without such support, even if we implement this logic in copy_mc_user_highpage() and copy_mc_highpage(), checking the hwpoison flag and copying the data remain two separate operations. They cannot be performed atomically, so there is still a race window between them, however small.

Quoted 3 lines
Quoted 1 line

Yes. And you need proper architecture support to fix it?

It does require architecture support, but that doesn't seem practical, haha.

Well, but that's required when an arch wants to rule out all races.

Quoted 4 lines
Quoted 4 lines
Quoted 3 lines

Moving the hwpoison check after the copy might address this issue. Is there anything wrong with my understanding?

Quoted 8 lines
Quoted 3 lines

Will architectures that are not prepared for copy_mc_user_highpage() not simply crash if they trigger an MCE during an access?

Do we need to check both before and after the copy? This feels a bit redundant, and I'm not sure whether such a small race window warrants an additional check.

I don't see how checking after the copy makes any sense? :)

You already crashed if there was a problem, why check? Or what am I missing?

Signature · 3 lines
Cheers,

David
#11 lore raw

Re: [PATCH 1/2] mm/compaction: skip folios containing hwpoisoned pages

Quoted 33 lines · David Hildenbrand (Arm)
Quoted 18 lines · Kaitao Cheng
Quoted 15 lines · David Hildenbrand (Arm)
Quoted 13 lines · Kaitao Cheng

在 2026/10/11 21:14, Huang, Ying 写道:

On architectures with hardware error correction and exception recovery support, accessing faulty memory automatically triggers an exception, which itself guarantees atomicity.

On architectures without such support, even if we implement this logic in copy_mc_user_highpage() and copy_mc_highpage(), checking the hwpoison flag and copying the data remain two separate operations. They cannot be performed atomically, so there is still a race window between them, however small.

Quoted 3 lines
Quoted 1 line

Yes. And you need proper architecture support to fix it?

It does require architecture support, but that doesn't seem practical, haha.

Well, but that's required when an arch wants to rule out all races.

Quoted 8 lines
Quoted 7 lines
Quoted 3 lines

Moving the hwpoison check after the copy might address this issue. Is there anything wrong with my understanding?

Will architectures that are not prepared for copy_mc_user_highpage() not simply crash if they trigger an MCE during an access?

Quoted 8 lines
Quoted 4 lines

Do we need to check both before and after the copy? This feels a bit redundant, and I'm not sure whether such a small race window warrants an additional check.

I don't see how checking after the copy makes any sense? :)

You already crashed if there was a problem, why check? Or what am I missing?

In cases such as transient errors, software poison injection, repeated bit flips in memory, or bit flips in the ECC check bits themselves, a page may be faulty while memcpy() still completes normally. If the copied data is correct, only the PG_hwpoison flag is lost during migration. But if the copied data is corrupted and the kernel or a process subsequently treats it as valid, I think the consequences could be more serious than an immediate crash. However, I cannot estimate how likely this is to happen.

Signature · 2 lines
Thanks
Kaitao Cheng
#12 lore raw

Re: [PATCH 1/2] mm/compaction: skip folios containing hwpoisoned pages

Quoted 21 lines · Kaitao Cheng
Quoted 18 lines · David Hildenbrand (Arm)
Quoted 5 lines · Kaitao Cheng

在 2026/10/20 21:07, David Hildenbrand (Arm) 写道:

It does require architecture support, but that doesn't seem practical, haha.

Well, but that's required when an arch wants to rule out all races.

Quoted 6 lines

Do we need to check both before and after the copy? This feels a bit redundant, and I'm not sure whether such a small race window warrants an additional check.

I don't see how checking after the copy makes any sense? :)

Quoted 8 lines
Quoted 1 line

You already crashed if there was a problem, why check? Or what am I missing?

In cases such as transient errors, software poison injection, repeated bit flips in memory, or bit flips in the ECC check bits themselves, a page may be faulty while memcpy() still completes normally. If the copied data is correct, only the PG_hwpoison flag is lost during migration. But if the copied data is corrupted and the kernel or a process subsequently treats it as valid, I think the consequences could be more serious than an immediate crash. However, I cannot estimate how likely this is to happen.

That's the point, me neither. I'm happy to perform checks before access, or after, in the default copy implementation, but I want to have properly documented how this makes sense and which races exist.

Signature · 3 lines
Cheers,

David
#13 lore raw

Re: [PATCH 1/2] mm/compaction: skip folios containing hwpoisoned pages

Quoted 7 lines · David Hildenbrand (Arm)
Quoted 6 lines · Kaitao Cheng
Quoted 3 lines · David Hildenbrand (Arm)
Quoted 2 lines · Kaitao Cheng
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 3 lines · David Hildenbrand (Arm)

It does require architecture support, but that doesn't seem practical, haha.

Well, but that's required when an arch wants to rule out all races.

Quoted 2 lines
Quoted 2 lines

Will architectures that are not prepared for copy_mc_user_highpage() not simply crash if they trigger an MCE during an access?

After thinking about it, if the memcpy here is guaranteed to cause a crash, wouldn't the system inevitably crash at some point anyway, even if we check folio_contain_hwpoisoned_page() beforehand and return early, since this kind of memory error is unrecoverable?

If the system won't crash, it seems fine to defer the check as well.

Quoted 16 lines
Quoted 8 lines
Quoted 8 lines
Quoted 5 lines

Do we need to check both before and after the copy? This feels a bit redundant, and I'm not sure whether such a small race window warrants an additional check.

I don't see how checking after the copy makes any sense? :)

Quoted 8 lines
Quoted 1 line

You already crashed if there was a problem, why check? Or what am I missing?

In cases such as transient errors, software poison injection, repeated bit flips in memory, or bit flips in the ECC check bits themselves, a page may be faulty while memcpy() still completes normally. If the copied data is correct, only the PG_hwpoison flag is lost during migration. But if the copied data is corrupted and the kernel or a process subsequently treats it as valid, I think the consequences could be more serious than an immediate crash. However, I cannot estimate how likely this is to happen.

Quoted 4 lines

That's the point, me neither. I'm happy to perform checks before access, or after, in the default copy implementation, but I want to have properly documented how this makes sense and which races exist.

Here are a few subtle corner cases I came up with. They may be extremely unlikely to occur.

case A:
CPU 1 CPU 2

if (folio_contain_hwpoisoned_page()) The check passes because
return -EHWPOISON; PG_hwpoisoned is not set.

A transient bus error occurs, or the flag is injected by software.

PG_hwpoisoned is set.

The bus temporarily returns to normal.

memcpy(new, old); The copy succeeds and copies
the correct data.

PG_hwpoisoned is not propagated to the new page, so the flag is lost.

case B:
CPU 1 CPU 2

if (folio_contain_hwpoisoned_page())
return -EHWPOISON;

A memory bit flips, causing an ECC error.

PG_hwpoisoned is set.

Other bits in the same byte then flip, or the ECC check bits themselves flip.

ECC check passes.

memcpy(new, old); The copy succeeds but copies
corrupted data.

PG_hwpoisoned is lost.

The kernel or user treats the corrupted data as valid.

Signature · 2 lines
Thanks
Kaitao Cheng
author
#14 lore raw

Re: [PATCH 1/2] mm/compaction: skip folios containing hwpoisoned pages

Quoted 2 lines · Kaitao Cheng

Here are a few subtle corner cases I came up with. They may be extremely unlikely to occur.

Both cases set the flag while the memcpy still succeeds - that is the "how this makes sense" for checking after the copy: a check before it misses them, the recheck after it catches both, and the refusal is the same either way (the data is still good in case A, already bad in case B). What no flag check, before or after, can catch is corruption whose hardware report lands after the last check; that needs architecture support, as David noted.

The migration paths spell out the same placement and races in v2 (patch 2, just posted), and the fallback patch ("mm/highmem: refuse to copy hwpoisoned folios in the !copy_mc fallbacks") is posted as well, with the races in its changelog.

Signature · 1 line
Yuanhe
#15 lore raw

Re: [PATCH 1/2] mm/compaction: skip folios containing hwpoisoned pages

Quoted 11 lines · Yuanhe Shu
Quoted 2 lines · Kaitao Cheng

Here are a few subtle corner cases I came up with. They may be extremely unlikely to occur.

Both cases set the flag while the memcpy still succeeds - that is the "how this makes sense" for checking after the copy: a check before it misses them, the recheck after it catches both, and the refusal is the same either way (the data is still good in case A, already bad in case B). What no flag check, before or after, can catch is corruption whose hardware report lands after the last check; that needs architecture support, as David noted.

Yes. And you really want to avoid reading corrupt data an triggering some undesired HW reaction (MCE )etc.

Quoted 5 lines

The migration paths spell out the same placement and races in v2 (patch 2, just posted), and the fallback patch ("mm/highmem: refuse to copy hwpoisoned folios in the !copy_mc fallbacks") is posted as well, with the races in its changelog.

Sorry, which patches did you post? Do you have a link? Do the supersede both patches here?

Signature · 3 lines
Cheers,

David
#16 lore raw

Re: [PATCH 1/2] mm/compaction: skip folios containing hwpoisoned pages

In reply to David Hildenbrand (Arm)
Quoted 31 lines · David Hildenbrand (Arm)
Quoted 20 lines · Yuanhe Shu

memory_failure() sets PG_hwpoison on the error page before it manages to pin the folio via get_hwpoison_page(). Compaction running on another CPU can isolate that same folio in the meantime, so HWPoisonHandlable() then fails on !PageLRU(), the retry loop in get_any_page() exhausts itself and memory_failure() gives up with MF_IGNORED - while compaction still has the folio on its migratepages list, flagged PG_hwpoison.

Nothing stops compaction from migrating such a folio afterwards. The corrupted data is copied into a fresh folio that carries no poison marker, and no hwpoison PTE entry is installed on the way
back: the userspace mapping is silently redirected to the corrupted copy and no SIGBUS is ever delivered.

Note that a large folio with a PG_hwpoisoned subpage can also sit on the LRU without any race at all, via the THP-split-failure path of memory_failure() (kill_procs_now() + MF_MSG_UNSPLIT_THP). Guarding compaction is therefore needed regardless of how the race in memory_failure() itself might be addressed.

Quoted 3 lines

folio_mc_copy() does not close this hole: it only detects corruption at copy time and only where ARCH_HAS_COPY_MC is implemented (x86_64 and PPC64; elsewhere copy_mc_highpage() degrades to a plain copy).

Then they should implement it.

Quoted 3 lines

Software-injected poison - what MADV_HWPOISON and the hwpoison-inject interface produce, and what tests and fuzzers exercise - never traps during the copy on any architecture.

Quoted 7 lines

And these are debug interfaces, why do we care?

Quoted 3 lines

An isolation-time check avoids
the migration entirely, on all architectures and for both real and simulated poison.

It's racy. See the link below.

Could we recheck PG_hwpoison on the source page after folio_mc_copy() completes, but before the mapping is transferred, and abort migration if the flag is set? This would prevent PG_hwpoison from being lost after migration.

Completely closing the race window by modifying the arch-specific copy_mc_to_kernel() implementation would presumably also require corresponding hardware and architectural capabilities, which does not seem very practical.

Quoted 65 lines
Quoted 18 lines

The check itself is two flag tests on the head page (PG_hwpoison and the folio-level has_hwpoisoned marker), not a walk over subpages.

Neither page reclaim nor memory hotplug lets such a folio be
copied into a fresh one: reclaim unmaps the poisoned order-0 page,
and skips the hwpoisoned large folios it cannot safely unmap, in shrink_folio_list()
commit 1b0449544c64 ("mm/vmscan: don't try to reclaim hwpoison folio") commit 9f1e8cd0b7c4 ("mm/vmscan: fix hwpoisoned large folio handling in shrink_folio_list") while memory hotplug refuses to migrate them in do_migrate_range()
commit 5f5ee52d4f58 ("mm/hwpoison: introduce folio_contain_hwpoisoned_page() helper"). Skip them in compaction as well, for the same reason reclaim settled
on skipping: the UCE is rare and a race with compaction is rarer
still, so skipping is enough, and a later memory_failure() will
handle the folio if the UCE is triggered again - while a migrated
folio would silently propagate the corruption instead.

Quoted 10 lines

Deterministic validation on v7.3-rc4-75-g62f4c998b297: order-4 mTHP folios were brought into the stable "large, on LRU, PG_hwpoisoned subpage" state by pinning a sibling subpage with vmsplice and injecting MADV_HWPOISON on another subpage, which makes memory_failure() take the THP-split-failure path; after triggering compaction, /proc/kpageflags shows which poisoned folios moved. The unpatched kernel migrated 16/16 poisoned folios and 16/16 clean controls in the same pageblocks; with this patch, 0/16 poisoned folios were migrated while their controls still migrated 16/16.

Was any of this written by an LLM?

Quoted 18 lines

Soft offline is not affected: it only sets the poison marker after its own migration has succeeded.

---
 mm/compaction.c | 8 ++++++++
 1 file changed, 8 insertions(+)
mm/compaction.c +1 -0
index a049415512c6..491adcbc5313 100644
@@ -1093,6 +1093,14 @@ isolate_migratepages_block(struct compact_control *cc, unsigned long low_pfn,
 		if (unlikely(!folio))
 			goto isolate_fail;
 
+		/*

mm/compaction.c isolate_migratepages_block(struct compact_control *cc, unsigned long low_pfn, · line 1106

Quoted 10 lines
mm/compaction.c +7 -0
+		 * Migrating would copy the corrupted data into a fresh
+		 * folio with no poison marker; skip, as memory hotplug
+		 * refuses to migrate such folios for the same reason.
+		 */
+		if (folio_contain_hwpoisoned_page(folio))
+			goto isolate_fail_put;
+
 		/*
 		 * Migration will fail if an anonymous page is pinned in memory,
 		 * so avoid taking lru_lock and isolating it unnecessarily in an

See

https://lore.kernel.org/linux-mm/[email protected]/

Signature · 2 lines
Thanks
Kaitao Cheng
author
#17 lore raw

PATCH 2/2 mm/migrate: refuse to migrate folios containing hwpoisoned pages

In reply to Yuanhe Shu

All callers of migrate_pages() without their own poison handling - move_pages(2), mbind(), NUMA balancing, alloc_contig_range() (CMA) and long-term GUP - can race memory_failure() the same way compaction does (see patch 1): the migration isolates a folio and clears PG_lru, memory_failure() sets PG_hwpoison, then fails to pin it and gives up with MF_IGNORED, and the pending migration copies the corrupted data into a fresh folio that carries no poison marker; the userspace mapping ends up silently serving the corrupted copy with no SIGBUS.

This was reproduced on v7.3-rc4-75-g62f4c998b297: an order-0 stress workload running madvise(MADV_HWPOISON) concurrently with move_pages() left the poison flag set on 8426 abandoned folios in a 180-second run, identified via /proc/kpageflags; 698 of them were subsequently migrated, and in every case the address then served the copied corrupted data with no SIGBUS.

Refuse such migrations in the migration core, with two checks:

- migrate_folio_unmap() checks at entry, so a folio that is already poisoned when the batch reaches it fails immediately, without allocating a destination folio or unmapping it.

- migrate_folio_move() checks again immediately before the copy, because migrate_pages_batch() unmaps a whole chunk of folios before copying any of them, so a poison can land in between. A GUP-based path such as madvise(MADV_HWPOISON) takes its reference while the folio is still mapped, i.e. before the unmap, and memory_failure() sets PG_hwpoison before releasing that reference, so a racing poison is visible by the time migration reaches the copy, unless the injecting thread is delayed between taking the reference and setting the flag. A hardware MCE can likewise land in the final instructions between the check and the copy; on x86_64 folio_mc_copy() additionally catches real-DRAM corruption at copy time.

Both checks fail with -EHWPOISON rather than -EAGAIN: -EAGAIN would
leave the folio on the migration list and retry into the same check,
while -EHWPOISON is a permanent failure, so the folio is put back on
the LRU with the poison flag intact, for reclaim or a later
memory_failure() to deal with - the same reasoning reclaim settled
on in
commit 9f1e8cd0b7c4 ("mm/vmscan: fix hwpoisoned large folio handling in shrink_folio_list"). The hugetlb path gets the same two checks for symmetry and defense
in depth: its unmap-to-copy window is single-folio and much narrower
than the batched path, but the checks cost two bit tests.

Measured with the workload above (180-second runs; the count of abandoned folios varies between runs with the race rate): the unpatched kernel propagated 698 of 8426 abandoned folios; with this patch, 0 of 9711 were propagated.

Soft offline is unaffected: it sets the poison marker only after its own migration has succeeded. Memory hotplug offlining is
unaffected: it filters poisoned folios out before calling migrate_pages(). Reclaim is unaffected: poisoned folios go through its own unmap and skip paths, not migrate_pages().

A check at the copy site was previously proposed by Kaitao Cheng, for the hugetlb path and for move_to_new_folio(); that discussion stalled over the remaining race with memory_failure(). The GUP-reference argument and the measurement above answer that concern for injection paths.

---
 mm/migrate.c | 41 +++++++++++++++++++++++++++++++++++++++--
 1 file changed, 39 insertions(+), 2 deletions(-)
mm/migrate.c +39 -2
index 15b45832bcfa..bdfd490dd8e8 100644
@@ -1222,6 +1222,18 @@ static int migrate_folio_unmap(new_folio_t get_new_folio,
 	bool locked = false;
 	bool dst_locked = false;
 
+	if (unlikely(folio_contain_hwpoisoned_page(src))) {
+		/*
+		 * The copy would propagate the corrupted data into a
+		 * fresh folio with no poison marker.  Fail permanently
+		 * so the folio is put back on the LRU for reclaim or a
+		 * later memory_failure(); it was not unmapped yet, so
+		 * there is nothing to restore.
+		 */
+		migrate_folio_undo_src(src, 0, NULL, false, ret);
+		return -EHWPOISON;
+	}
+
 	dst = get_new_folio(src, private);
 	if (!dst)
 		return -ENOMEM;
@@ -1376,6 +1388,20 @@ static int migrate_folio_move(free_folio_t put_new_folio, unsigned long private,
 	prev = dst->lru.prev;
 	list_del(&dst->lru);
 
+	if (unlikely(folio_contain_hwpoisoned_page(src))) {
+		/*
+		 * Recheck before the copy: a poison can land while the
+		 * batch is still unmapping the rest of its chunk.  A
+		 * GUP-based path takes its reference before the unmap,
+		 * and memory_failure() sets PG_hwpoison before
+		 * releasing it, so a racing poison is visible by now;
+		 * a hardware MCE can still land in the final
+		 * instructions.
+		 */
+		rc = -EHWPOISON;
+		goto out;
+	}
+
 	if (unlikely(page_has_movable_ops(&src->page))) {
 		rc = migrate_movable_ops_page(&dst->page, &src->page, mode);
 		if (rc)
@@ -1521,6 +1547,12 @@ static int unmap_and_move_hugetlb_folio(new_folio_t get_new_folio,
 		goto out_unlock;
 	}
 
+	if (unlikely(folio_contain_hwpoisoned_page(src))) {
+		/* Same reasoning as in migrate_folio_unmap(). */
+		rc = -EHWPOISON;
+		goto out_unlock;
+	}
+
 	if (folio_test_anon(src))
 		anon_vma = folio_get_anon_vma(src);
 
@@ -1546,8 +1578,13 @@ static int unmap_and_move_hugetlb_folio(new_folio_t get_new_folio,
 		was_mapped = 1;
 	}
 
-	if (!folio_mapped(src))
-		rc = move_to_new_folio(dst, src, mode);
+	if (!folio_mapped(src)) {
+		/* Same reasoning as in migrate_folio_move(). */
+		if (unlikely(folio_contain_hwpoisoned_page(src)))
+			rc = -EHWPOISON;
+		else
+			rc = move_to_new_folio(dst, src, mode);
+	}
 
 	if (was_mapped)
 		remove_migration_ptes(src, !rc ? dst : src, ttu);
Signature · 1 line
2.43.7