[PATCH] mm/hugetlb: Fix null pointer dereference of nodemask in hugetlb_cma_alloc_frozen_folio

From: Sourav Panda

Date: Thu Jul 02 2026 - 17:57:23 EST


alloc_buddy_hugetlb_folio_with_mpol() can pass a NULL nodemask to
hugetlb_cma_alloc_frozen_folio() as a fallback to allocate from all
nodes. In this case, hugetlb_cma_alloc_frozen_folio() blindly
dereferences it in for_each_node_mask(), leading to a null pointer
dereference.

Fix this by checking if nodemask is NULL and defaulting to
node_states[N_MEMORY] if it is.

Signed-off-by: Sourav Panda <souravpanda@xxxxxxxxxx>
---
mm/hugetlb_cma.c | 3 +++
1 file changed, 3 insertions(+)

diff --git a/mm/hugetlb_cma.c b/mm/hugetlb_cma.c
index 39344d6c78d8..563e11e607e5 100644
--- a/mm/hugetlb_cma.c
+++ b/mm/hugetlb_cma.c
@@ -34,6 +34,9 @@ struct folio *hugetlb_cma_alloc_frozen_folio(int order, gfp_t gfp_mask,
if (!hugetlb_cma_size)
return NULL;

+ if (!nodemask)
+ nodemask = &node_states[N_MEMORY];
+
if (hugetlb_cma[nid])
page = cma_alloc_frozen_compound(hugetlb_cma[nid], order);

--
2.55.0.rc0.799.gd6f94ed593-goog