Re: [PATCH v7 2/3] mm/page_alloc: only free healthy pages in high-order folio

From: Jiaqi Yan

Date: Fri Dec 04 2026 - 20:29:16 EST


On Wed, Dec 2, 2026 at 8:54 AM David Hildenbrand (Arm) <david@xxxxxxxxxx> wrote:
>
> On 11/10/26 06:33, Jiaqi Yan wrote:
> > At the end of dissolve_free_hugetlb_folio(), a free HugeTLB folio
> > becomes non-HugeTLB, and it is released to buddy allocator as a
> > high-order folio, e.g. a folio that contains 262144 pages if the
> > folio was a 1G HugeTLB hugepage.
> >
> > This is problematic if the HugeTLB hugepage contained HWPoison
> > subpages. In that case, since buddy allocator does not check
> > HWPoison for non-zero-order folio, the raw HWPoison page can be
> > given out with its buddy page and be re-used by either kernel or
> > userspace.
> >
> > Memory failure recovery (MFR) in kernel does attempt to take raw
> > HWPoison page off buddy allocator after
> > dissolve_free_hugetlb_folio(). However, there is always a time
> > window between dissolve_free_hugetlb_folio() frees a HWPoison
> > high-order folio to buddy allocator and MFR takes HWPoison raw page
> > off buddy allocator. Worse, a free HWPoison HugeTLB folio can also
> > be dissolved outside of MFR, e.g. when shrinking the HugeTLB pool,
> > and then nobody takes its HWPoison pages off buddy allocator.
> >
> > Another similar situation is when a transparent huge page (THP)
> > runs into memory failure but splitting failed. Such THP will
> > eventually be released to buddy allocator when owning userspace
> > processes are gone, but with certain subpages having HWPoison.
> >
> > One obvious way to avoid these problems is to always check
> > PageHWPoison() when allocating pages, which check_new_pages() only
> > does if page sanity checks are enabled. However, it is against the
> > past efforts to reduce sanity check overhead [1,2,3].
> >
> > Instead, keep HWPoison pages out of buddy allocator when freeing a
> > high-order folio. __free_pages_prepare() already walks through all
> > tail pages of a high-order folio to check and clear their flags, so
> > counting PageHWPoison() pages in that loop is cheap, as pointed out
> > by David Hildenbrand [4]. Unlike PG_has_hwpoisoned, which is only
> > maintained for THP, this catches HWPoison pages in any high-order
> > folio, including dissolved HugeTLB folios.
>
> So thinking about this again (and hating the diffstat with passion) I think a
> better temporary approach (until we sort this all out properly, eventually, ...
> :) ) would be to set PG_has_hwpoisoned also for hugetlb.
>
> I think your previous patches did that for dissolved hugetlb folios ...
>
> Didn't Willy have some patches to do it properly for hugetlb? Yes indeed:
>
> https://lore.kernel.org/all/20260805210557.1118966-9-willy@xxxxxxxxxxxxx/
>
> Likely we could do it more minimally for the purpose of this stuff here by just
> setting the flag?
>
> Then, as a quick, less intrusive fix, exclude the entire thing from the buddy.
> Don't free individual pages. Just stop that memory from reuse for now.
>
> Of course, we might also be able to do that with PageHWPoison, but
> PG_has_hwpoisoned would be a lot cleaner.
>
> I don't think we want all that complicated "free parts of a broken folio" in a
> commit that actually fixes something real (using hugetlb folios and handing them
> back out again).

Thanks David, that makes sense to me. I will split the "fix" from the
"free healthy pages" part, and send the fix as a new, standalone
series: when a high-order folio with any HWPoison page is freed,
__free_pages_prepare() rejects the whole folio.

The cost is leaking the whole folio, e.g. 1G per memory error for a
1G hugepage. That is intended: reusing a HWPoison page is argubably
worse than losing the memory. Recycling the healthy pages can come as
a separate series on top later, which is where I will rework what v7
did.

For detecting the HWPoison pages, I went with PageHWPoison instead of
PG_has_hwpoisoned, at least for now. __free_pages_prepare() already walks
every tail page of a high-order folio, so it is pretty cheap to just
"has_hwpoisoned |= PageHWPoison(page)" in that loop, which you suggested
earlier.

PG_has_hwpoisoned is only defined with CONFIG_TRANSPARENT_HUGEPAGE
today. Willy's series [1] relaxes that with CONFIG_LARGE_FOLIO, but
it is not merged yet. Besides, for a dissolved HugeTLB folio, the flag
would have to survive until the folio is freed. However, [1] clears
has_hwpoisoned in folio_clear_hugetlb_hwpoison() when moving the error
flags to the raw pages (which is the right thing for his series). So I'd
rather not depend on it, or conflict with it.

Once [1] lands, switching to has_hwpoisoned should be straightforward
if we prefer it.

[1] https://lore.kernel.org/all/20260805210557.1118966-9-willy@xxxxxxxxxxxxx/

Thanks,
Jiaqi

>
> --
> Cheers,
>
> David