On Tue, May 12, 2026 at 05:06:43PM -0400, Michael S. Tsirkin wrote:
> gather_surplus_pages() pre-allocates hugetlb pages into the pool
> during mmap. Pass __GFP_ZERO so these pages are zeroed by the
> buddy allocator, and HPG_zeroed is set by alloc_surplus_hugetlb_folio.
>
> Add bool *zeroed output to alloc_hugetlb_folio_reserve() so
> callers can check whether the pool page is known-zero. memfd's
> memfd_alloc_folio() uses this to skip the explicit folio_zero_user()
> when the page is already zero.
>
> This avoids redundant zeroing for memfd hugetlb pages that were
> pre-allocated into the pool and never mapped to userspace.
>
> Signed-off-by: Michael S. Tsirkin <[email protected]>
> Assisted-by: Claude:claude-opus-4-6
> ---
> include/linux/hugetlb.h | 6 ++++--
> mm/hugetlb.c | 11 +++++++++--
> mm/memfd.c | 14 ++++++++------
> 3 files changed, 21 insertions(+), 10 deletions(-)
>
... snip ...
> @@ -2221,6 +2221,12 @@ struct folio *alloc_hugetlb_folio_reserve(struct
> hstate *h, int preferred_nid,
> h->resv_huge_pages--;
>
> spin_unlock_irq(&hugetlb_lock);
> +
> + if (zeroed && folio) {
> + *zeroed = folio_test_hugetlb_zeroed(folio);
> + folio_clear_hugetlb_zeroed(folio);
> + }
> +
> return folio;
> }
Hmmm...
if the intent is to simply communicate to the next function up whether
the folio has been zeroed just so that function can zero it - does it
make sense instead to just zero it in this function instead?
Also the only user is memfd, and that appears to always want the folio
zeroed, so i think it simplifies this entire patch to:
struct folio* alloc_hugetlb_folio_reserve() {
...
if (folio && !folio_test_hugetlb_zeroed(folio))
folio_zero_user(folio, 0);
else if (folio)
folio_clear_hugetlb_zeroed(folio);
return folio;
...
}
with no API changes
~Gregory