Re: [PATCH 1/3] mm: don't PTE-batch a swap-in over a hardware-poisoned subpage

From: Usama Arif

Date: Wed Nov 11 2026 - 13:54:35 EST


On Wed, 11 Nov 2026 13:18:36 +0000 "Lorenzo Stoakes (ARM)" <ljs@xxxxxxxxxx> wrote:

> On Wed, Nov 11, 2026 at 03:38:29AM -0800, Usama Arif wrote:
> > do_swap_page() checks PageHWPoison() on the faulting page, but the
> > large-folio batching path then maps the rest of the folio without looking
> > at the other subpages. A poisoned subpage that is not the one being
> > faulted gets mapped anyway, and the fault that would have reported it
> > never happens.
> >
> > Skip batching for such a folio and let each page take its own fault,
> > where the existing check catches it.
> >
> > Scan the subpages rather than testing the folio-level PG_has_hwpoisoned:
> > memory_failure() sets PageHWPoison() on the subpage first and only sets
> > the folio flag much later, and the bail-outs in between - for example
> > get_hwpoison_page() losing a page lifecycle race - return with the
> > subpage marked and the folio flag never set.
> >
> > Fixes: 508758960b8d ("mm: swap: entirely map large folios found in swapcache")
> > Cc: stable@xxxxxxxxxxxxxxx
> > Signed-off-by: Usama Arif <usama.arif@xxxxxxxxx>
> > ---
> > mm/internal.h | 15 +++++++++++++++
> > mm/memory.c | 4 ++++
> > 2 files changed, 19 insertions(+)
> >
> > diff --git a/mm/internal.h b/mm/internal.h
> > index cd3675c044e0a..e61c02f4019b0 100644
> > --- a/mm/internal.h
> > +++ b/mm/internal.h
> > @@ -28,6 +28,21 @@ struct folio_batch;
> > int __copy_remote_mm_str(struct mm_struct *mm, unsigned long addr,
> > void *buf, int len, unsigned int gup_flags);
> >
> > +/*
> > + * Check every page of the folio for HWPoison, rather than relying on the
> > + * folio-level PG_has_hwpoisoned, which memory_failure() can leave clear.
> > + */
> > +static inline bool folio_has_any_hwpoisoned_page(const struct folio *folio)
> > +{
> > + long nr = folio_nr_pages(folio);
> > + long i;
> > +
> > + for (i = 0; i < nr; i++)
> > + if (PageHWPoison(folio_page(folio, i)))
> > + return true;
> > + return false;
> > +}
> > +
> > /* mm/workingset.c */
> > bool workingset_test_recent(void *shadow, bool file, bool *workingset,
> > bool flush);
> > diff --git a/mm/memory.c b/mm/memory.c
> > index d477a4f986bc8..4b7d456332200 100644
> > --- a/mm/memory.c
> > +++ b/mm/memory.c
> > @@ -5158,6 +5158,10 @@ vm_fault_t do_swap_page(struct vm_fault *vmf)
> > swap_pte_batch(folio_ptep, nr, folio_pte) != nr)
> > goto check_folio;
> >
> > + /* Let a poisoned subpage take its own fault. */
> > + if (folio_has_any_hwpoisoned_page(folio))
> > + goto check_folio;
> > +

Hello Lorenzo,

Thanks for the reviews!

>
> Hm, isn't scanning every single page in the folio potentially quite costly
> here? I thought the point of batching was to improve performance overall?

Swapping in requires checking every subpage, so there would be a cost.
I guess trading it off for correctness :(

>
> It seems broken to have stuff potentially interact with hwpoison but then
> to only set the folio-level flag much later?
>

PG_has_hwpoisoned lives in the second page, so memory_failure() only
sets it once it holds a folio reference, otherwise it could race with a
THP split. If get_hwpoison_page() can't get a reference,
memory_failure() gives up with only the subpage marked. The comment
above folio_set_has_hwpoisoned() in memory_failure() says that is fine:

Don't need care about the above error handling paths for
get_hwpoison_page() since they handle either free page
or unhandlable page.

But a large folio that is briefly off the LRU, e.g. isolated by
reclaim, takes those paths too and can still be mapped afterwards.

> Also why are we swapping out folios containing hwpoison pages? Shouldn't we
> migrate away from the hwpoison? :) (I'm not really familiar with the hw
> poison stuff)
>
>

The error hits a folio that is already in the swap
cache, e.g. while reclaim has it off the LRU to write it out, and
get_hwpoison_page() fails for any folio that is off the LRU. Normally
memory_failure() would split the folio and isolate the bad page (for
an uncorrected error there is nothing to migrate; that is soft
offline). Here it does neither, and the folio stays in the swap cache
with only the subpage marked.

While looking at this I also noticed that the fallback drops a freshly
read (not yet anon) folio from the swap cache and frees it with the
poisoned page in it. We should actually actually also check folio_test_anon()
before checking folio_has_any_hwpoisoned_page().

> > page_idx = idx;
> > address = folio_start;
> > ptep = folio_ptep;
> > --
> > 2.53.0-Meta
> >
>
> --
> Cheers, Lorenzo
>