Re: [PATCH v2 09/35] mm/hugetlb: free surplus folios of other nodes on free

From: Jose A. Perez de Azpillaga

Date: Fri Dec 04 2026 - 05:46:35 EST


On 12/3/26 12:37 PM, David Hildenbrand (Arm) wrote:
> Surplus hugetlb folios are accounted per node, but reservations are
> global. When free_huge_folio() frees a hugetlb folio whose node doesn't
> have any surplus folios, we currently keep it as a free hugetlb folio.
>
> However, we might then have surplus folios on other nodes that are no
> longer required for the global reservations.
>
> This can be reproduced with two NUMA nodes, nr_hugepages=1 on node 0 and
> nr_overcommit_hugepages=1:
>
> (1) Allocate the single hugetlb folio on node 0 from the pool.
> (2) With MPOL_BIND to node 1, mmap(MAP_SHARED) a hugetlb memfd that
> needs a single folio. This will reserve + allocate a surplus folio
> on node 1.
> (3) Free the folio from (1).
> (4) With MPOL_BIND to node 0, fault in the folio of the memfd, which
> consumes the reservation and allocates node 0's free folio, and
> immediately punch a hole, freeing the folio without restoring the
> reservation. Node 0 has no surplus folios, so the folio is kept.
>
> The pool ends up with a free surplus folio on node 1 that nobody
> reserved:
>
> HugePages_Total: 2
> HugePages_Free: 2
> HugePages_Rsvd: 0
> HugePages_Surp: 1
>
> Let's fix it by freeing a surplus folio of another node when
> free_huge_folio() keeps a folio whose node has no surplus folios, using
> the same rule as for the folio's own node: the free folios cover all
> reservations without it.
>
> With this fix, the surplus folio is already freed in (3), as the two
> free folios then exceed the single reservation, and we correctly end up
> with a single free folio on node 0 and no surplus folios.
>
> Note that the freed surplus folio might have been required by a task
> that was bound to that node. But we don't care about that: reservations
> are inherently not NUMA aware, and anybody who needs guarantees on
> specific nodes has to preallocate, like QEMU does, for example.
>
> This has low urgency.
>
> Fixes: 7893d1d505d5 ("hugetlb: Try to grow hugetlb pool for MAP_PRIVATE mappings")
> Cc: stable@xxxxxxxxxxxxxxx
> Reported-by: Chris Mason <mason@xxxxxxxxxx>
> Tested-by: Hongfu Li <lihongfu@xxxxxxxxxx>
> Reviewed-by: Hongfu Li <lihongfu@xxxxxxxxxx>
> Signed-off-by: David Hildenbrand (Arm) <david@xxxxxxxxxx>
> ---
> mm/hugetlb.c | 31 +++++++++++++++++++++++++------
> 1 file changed, 25 insertions(+), 6 deletions(-)
>
> diff --git a/mm/hugetlb.c b/mm/hugetlb.c
> index 662cb87fe740a..5984377160d37 100644
> --- a/mm/hugetlb.c
> +++ b/mm/hugetlb.c
> @@ -142,6 +142,8 @@ static void hugetlb_unmap_range(struct mmu_gather *tlb,
> unsigned long end, struct folio *folio,
> zap_flags_t zap_flags);
> static struct resv_map *vma_resv_map(struct vm_area_struct *vma);
> +static struct folio *remove_pool_hugetlb_folio(struct hstate *h,
> + nodemask_t *nodes_allowed, bool acct_surplus);
>
> static inline bool subpool_is_free(struct hugepage_subpool *spool)
> {
> @@ -1767,6 +1769,7 @@ void free_huge_folio(struct folio *folio)
> struct hstate *h = folio_hstate(folio);
> int nid = folio_nid(folio);
> struct hugepage_subpool *spool = hugetlb_folio_subpool(folio);
> + struct folio *surplus_folio = NULL;
> bool restore_reserve;
> unsigned long flags;
>
> @@ -1815,19 +1818,35 @@ void free_huge_folio(struct folio *folio)
> folio_clear_hugetlb_temporary(folio);
> spin_unlock_irqrestore(&hugetlb_lock, flags);
> update_and_free_hugetlb_folio(h, folio, true);
> + return;
> } else if (h->surplus_huge_pages_node[nid] &&
> h->free_huge_pages >= h->resv_huge_pages) {
> /* Free this surplus folio that is not required globally. */
> remove_hugetlb_folio(h, folio, true);
> spin_unlock_irqrestore(&hugetlb_lock, flags);
> update_and_free_hugetlb_folio(h, folio, true);
> - } else {
> - mod_node_page_state(folio_pgdat(folio), NR_HUGETLB,
> - -(long)pages_per_huge_page(h));
> - arch_clear_hugetlb_flags(folio);
> - enqueue_hugetlb_folio(h, folio);
> - spin_unlock_irqrestore(&hugetlb_lock, flags);
> + return;
> }
> +
> + mod_node_page_state(folio_pgdat(folio), NR_HUGETLB,
> + -(long)pages_per_huge_page(h));
> + arch_clear_hugetlb_flags(folio);
> + enqueue_hugetlb_folio(h, folio);
> +
> + /*
> + * This is not a surplus folio on this node, but we might have surplus
> + * folios on other nodes that are no longer required, as reservations
> + * are global and surplus folios are per node. So free another folio
> + * instead.
> + */
> + if (h->surplus_huge_pages &&
> + h->free_huge_pages > h->resv_huge_pages)
> + surplus_folio = remove_pool_hugetlb_folio(h, &node_states[N_MEMORY],
> + /* acct_surplus= */ true);

with Chris's hwpoison check folded in here:

Reviewed-by: Jose A. Perez de Azpillaga <azpijr@xxxxxxxxx>

--
cheers,
jose a. p-a