Re: [PATCH v4 1/3] mm: memfd/hugetlb: memfd-based userspace HugeTLB MFR policy

From: Jiaqi Yan

Date: Sat Nov 28 2026 - 15:01:47 EST


On Mon, Nov 16, 2026 at 8:50 PM Chris Down <chris@xxxxxxxxxxxxxx> wrote:
>
> Hi Jiaqi,
>
> This generally looks reasonable in principle, but some comments.

Thanks a lot for these super helpful review, Chris! Below are
what I have changed and tested locally over the past couple of weeks.

>
> Jiaqi Yan writes:
> >+ if (!mapping_mf_keep_ue_mapped(mapping)) {
> >+ ret = VM_FAULT_HWPOISON_LARGE |
> >+ VM_FAULT_SET_HINDEX(hstate_index(h));
> >+ goto backout_unlocked;
> >+ }
>
> This doesn't look right to me. For example, hugetlb_mfill_atomic_pte() also
> checks folio_test_hwpoison(), so UFFDIO_CONTINUE on such a folio still fails
> with -EIO. Also, do_migrate_range() calls unmap_poisoned_folio() on any
> mapped folio with HWPoison set, so offlining the memory block unmaps the
> hugepage after all.

Agreed, I am adding the support for both cases:

- hugetlb_mfill_atomic_pte() won't fail with -EIO if the HWPoison
folio is in the page cache of an MFD_MF_KEEP_UE_MAPPED memfd,
which covers UFFDIO_CONTINUE. VMM during post-copy would need this.
A HWPoison anon folio from a private UFFDIO_COPY, however, still
fails with -EIO.

- do_migrate_range() won't unmap_poisoned_folio() on a folio kept mapped
by MFD_MF_KEEP_UE_MAPPED. Such a folio is in use and no
longer migratable, so after do_migrate_range(), offlining the memory block now
fails with -EBUSY (dissolve_free_hugetlb_folios() fails on it) until userspace
releases the hugepage, the same as with a long-term pin. I think the
policy userspace opted into should win over hot-remove here, and I
will document it.

Also added two new modes in hugetlb-mfr.c to cover both cases: "uffd"
checks that
UFFDIO_CONTINUE maps the HWPoison hugepage from a second mapping, and
"offline" checks that offlining the memory block fails with EBUSY while
the memory stays accessible.

>
> >+ folio_put(folio);
> >+
> >+ /* Refcount now should be zero and ready to dissolve folio. */
> >+ ret = dissolve_free_hugetlb_folio(folio);
> >+ if (ret)
> >+ pr_err("failed to dissolve hugetlb folio: %d\n", ret);
>
> This isn't a good idea when somebody else still holds a reference, such as
> a long term pin from io_uring or iommufd (Jason was asking about the latter
> in [1]). dissolve_free_hugetlb_folio() returns -EBUSY, and when that last
> reference goes away later, free_huge_folio() puts the folio on the free list
> with HWPoison still set. Now, while this sketchy page may well never be
> handed out again, it is nevertheless counted in free_huge_pages, so a later
> mmap() can end up getting its reservation against it, and as a result, a
> SIGBUS when it faults. That seems like a real problem for a VMM with
> passthrough devices.
>
> I didn't check in detail, but since the folio has to be dissolved when its
> last reference goes, wherever that happens, perhaps one way to fix this
> would be to do it from free_huge_folio() instead, and treat a HWPoison folio
> there much like a surplus one? That would cover the pinned case, and I think
> it would also let you drop filemap_offline_hwpoison_folio() and the
> folio_batch changes in remove_inode_hugepages() entirely.

Thanks for this great suggestion. Incorporated it into my next revision.
free_huge_folio() now takes a HWPoison folio out of the pool and frees
it with update_and_free_hugetlb_folio(), like it does for a surplus
folio, instead of enqueuing it. With "mm/page_alloc: only free healthy
pages in high-order folio" [2], only the healthy pages go back to the
buddy allocator, and the HWPoison ones are taken off. The folio is
never counted in free_huge_pages, whoever drops the last reference,
including an unpin from iommufd or io_uring long after the memfd is
gone. As you already pointed out, filemap_offline_hwpoison_folio() and the
folio_batch changes in remove_inode_hugepages() are gone.

This is not specific to MFD_MF_KEEP_UE_MAPPED: an anonymous
HugeTLB folio pinned when memory_failure() runs hits the same problem
today. So I am moving this change into the next version of [2], as a new
patch on top of "only free healthy pages", and this series will still be
based on it.

During implementing your idea, I found two adjustments to memory_failure()
have to came with it:

- free_huge_folio() can run in atomic context, so with HVO the folio
is freed later by free_hpage_work. me_huge_page() used to drop its
refcount and then dissolve and check PageHWPoisonTakenOff(), which
could now report MF_FAILED (and make MADV_HWPOISON return -EBUSY)
for a page that gets isolated right after. My solution is to make
me_huge_page()
test folio_put_testzero(): if it drops the last reference, it waits for
free_hpage_work and then checks PageHWPoisonTakenOff(); otherwise
someone (e.g. a pin) still holds the folio, and the result is
MF_FAILED as before.

- If in get_huge_page_for_hwpoison(), folio_try_get() fails because the last
reference is being dropped concurrently, I am going to call it
MF_HUGETLB_FREEING, instead of MF_HUGETLB_FREED.
Unlike MF_HUGETLB_FREED, for MF_HUGETLB_FREEING
memory_failure() no longer locks and dissolves the folio, as it could race
with someone else freeing the same folio to buddy. free_huge_folio() picks
its branch under hugetlb_lock, and hugetlb_update_hwpoison() already
marks the folio HWPoison under the same lock, so memory_failure() can let
the other pending free takes care of the foilo, and report MF_DELAYED.

To provide coverage for long-term pins: the memory-failure selftest
now gets anon_hugetlb_pinned (the hugepage is freed at unpin, and all
remaining free hugepages can be reserved and faulted in without
SIGBUS) and anon_hugetlb_surplus, and hugetlb-mfr gets a "pin" mode.
The pin comes from an io_uring fixed buffer, or from gup_test
(PIN_LONGTERM_TEST_START) when io_uring is not available.

>
> >+ if (hugetlb_should_keep_hwpoison_mapped(folio, mapping))
> >+ /* A new raw HWPoison page in a still mapped hugepage. */
> >+ ret = MF_HUGETLB_IN_USED;
>
> This also looks a little wrong, because a 2nd error now gets past here and
> reaches the hwpoison_filter() check in try_memory_failure_hugetlb(), which
> calls folio_clear_hugetlb_hwpoison() when the page is filtered. That clears
> HWPoison for the whole folio, earlier errors included. I'd suggest instead
> that it only undoes what this call added.

Right, a filtered 2nd error would undo the earlier ones too. Undoing
only what the current call added is doable: it needs to know whether
the call set the folio's HWPoison flag, and to remove only its own
raw_hwp_list entry.

Considering hwpoison_filter() is a debugfs testing aid, I don't think the
extra code is worth it for that; I simply skip hwpoison_filter() if a
folio is of
an MFD_MF_KEEP_UE_MAPPED memfd. If you think the filter should
be supported, I can reconsider adding the precise undo.

I drafted the next version and ran the new memory-failure and hugetlb-mfr
selftests with HVO on and off, 1G (boot-time) and 2M (runtime) hugepages,
errors injected with both corrupt-pfn and MADV_HWPOISON, and the HWPoison
hugepage released by truncate, close of a sealed memfd, unpin (io_uring
and gup_test) and after UFFDIO_CONTINUE; everything passes, including
200 iterations of the anon HugeTLB test with HVO on.

I still need to take a look at the comments from William and Miaohe; hopefully
I can get something ready to post as v5 by Chrismas Eve.

Thanks!
Jiaqi

>
> Thanks!
>
> Chris
>
> [1] https://lore.kernel.org/r/20250120172626.GO5556@xxxxxxxxxx

[2] https://lore.kernel.org/linux-mm/20261110053331.2708996-1-jiaqiyan@xxxxxxxxxx