From nobody Sat Jul 4 19:59:43 2026 Received: from mail-pj1-f73.google.com (mail-pj1-f73.google.com [209.85.216.73]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id E2F7B1DED40 for ; Sat, 4 Jul 2026 17:49:32 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.216.73 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1783187374; cv=none; b=dViS/o8yZCTVdib2TrSqqvIY6NzqCh30LWcJOsutZ9SBvFAVpYxP+oob/QbVhS3KsnD8XqBfew5fn0C1f3J8On5hq12sxwCzaVPx4rhUzs4uqzmj3yTmSOY8QD9u3qJAE2fV1VEkmGoZDqwtsqZx+luVlGAG8kcdR2WtGlgh0hg= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1783187374; c=relaxed/simple; bh=jXOHQqAKq9cZbHnAqg7/JSwMYcb83xBQk2xVOryUE3g=; h=Date:Mime-Version:Message-ID:Subject:From:To:Cc:Content-Type; b=fiSElt3+jBTB0EY/OuLKItMLUcs0cgc/PX0qRI0Mu7+nLh5Hp2lj2zK0r2n0G/gtLot2E4Ckszm8CfsBeY7PQ/2M7GPPueEY8m60xT+IBRtCYGmJUHdeivjI784ULK34pqa5bqtQDqL99wXomgrrxnTgUNM3rTJun2lf3mgFlps= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--souravpanda.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=HHXVKG+s; arc=none smtp.client-ip=209.85.216.73 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--souravpanda.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="HHXVKG+s" Received: by mail-pj1-f73.google.com with SMTP id 98e67ed59e1d1-3810960140eso2499489a91.2 for ; Sat, 04 Jul 2026 10:49:32 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20251104; t=1783187372; x=1783792172; darn=vger.kernel.org; h=cc:to:from:subject:message-id:mime-version:date:from:to:cc:subject :date:message-id:reply-to; bh=vk4Q1IL+H0DJ+/uFKCde3qqkuwUWo/vem0qrwOJHzYI=; b=HHXVKG+shZJbYs3jm+OUBRmH6bFu+oGbqVsxW42wMP1yFAeIt/xkeIkEsS9GydiipD Ey0HrXEbvcAnfYJDioJo7c/oL4lgj1PmDgAMuz88ZwNPDj0xeH2bD3U6UDDPHPYzZn7I mm6z0r7oXVjGR9oxjQug3AgKEIRm/K1BNwRWdTzpeYoymIrHGe6PtfUhKp0Gk1j4gAmL sitWKd4A+c01+X7tktamt4rugBX/ufMCNeXm+kAWt0dVB//PXfDtNoLHjNOTKIIvUc1b T7gCpJxcvAbiCUOCtQ+BkiQpJlZLrHKAkN+nXHwaKaP5QCuqSIOPUzE0mUmxMDNDMdBB AQMg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1783187372; x=1783792172; h=cc:to:from:subject:message-id:mime-version:date:x-gm-message-state :from:to:cc:subject:date:message-id:reply-to; bh=vk4Q1IL+H0DJ+/uFKCde3qqkuwUWo/vem0qrwOJHzYI=; b=fgjYO7pfvPVPCVS1/ZcKpS9EL7B1qVrIDnJBTI9ntuDBjMy9wIsaQnlScyEI4+1r5I PBzTdhXuWZuW3nSeHqcX/AcxhMplR6N3vJG4wZLJQEhnrMV3z1PLwB9+nA5UEiGdmrla b5MKnUYZB+VwqHZ83ythOJ/0XBhiEFntQFLy5bV9fOVFWvuWNRrQg2gN0sH6c3TUKfSu zbrThpDT5tdrCmRUI8qG8M7z2weglMB3HHH6/rIYqAQMm6ar4+uI2Pn3aBlLUIj8Cr0q x3AchNO+9aQq1FfB3gPgjuZJP3lMkCFOmhLnYQqLYUc2PvprgPdyPbth5Q2CHCy14QwS Hd3A== X-Forwarded-Encrypted: i=1; AHgh+RqU8C80uSvcjs9Nuk53BK9sksWoV+txIge+OVaQbAf0j+ih4Tz6hSsTeA7eApxdFlIKXowowCLfF/YrfH8=@vger.kernel.org X-Gm-Message-State: AOJu0YzyAcr8zmaohjnpKJaOdj68CMyOTnxfZ1MHzduLVv0pJ6ZxIFwl 9LW4g/a3grci9zowWkwS6qexGRThnTPO1385gG1aeIxDNKlX8E4t5ap56HsVNY6t8Fr4jNGcIGj dde+EWDTkcCz7xGupd2x0uyVDTA== X-Received: from pjp15.prod.google.com ([2002:a17:90b:55cf:b0:37d:a52a:ff00]) (user=souravpanda job=prod-delivery.src-stubby-dispatcher) by 2002:a17:90b:28c3:b0:37d:ee77:78ac with SMTP id 98e67ed59e1d1-3829f00771cmr3866565a91.19.1783187371917; Sat, 04 Jul 2026 10:49:31 -0700 (PDT) Date: Sat, 4 Jul 2026 17:49:30 +0000 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 X-Mailer: git-send-email 2.55.0.rc0.799.gd6f94ed593-goog Message-ID: <20260704174930.2885785-1-souravpanda@google.com> Subject: [PATCH v2] mm/hugetlb: Fix null nodemask in alloc_fresh_hugetlb_folio From: Sourav Panda To: muchun.song@linux.dev, osalvador@suse.de, akpm@linux-foundation.org Cc: david@kernel.org, surenb@google.com, fvdl@google.com, gthelen@google.com, souravpanda@google.com, linux-mm@kvack.org, linux-kernel@vger.kernel.org Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" alloc_buddy_hugetlb_folio_with_mpol() can pass a NULL nodemask to alloc_fresh_hugetlb_folio() as a fallback to allocate from all nodes. If order is gigantic, alloc_fresh_hugetlb_folio() propagates the NULL nodemask down to hugetlb_cma_alloc_frozen_folio() which blindly dereferences it in for_each_node_mask(), leading to a null pointer dereference. Similarly, if the CMA allocation fails, the fallback alloc_contig_frozen_pages() is also called with a NULL nodemask, which may cause issues. Fix this by explicitly checking if nodemask is NULL in alloc_fresh_hugetlb_folio() and defaulting to cpuset_current_mems_allowed. This ensures that both the CMA and contiguous allocators receive a valid nodemask. Additionally, this patch adds a missing node_isset(nid, *nodemask) check in hugetlb_cma_alloc_frozen_folio() to ensure the initial node allocation attempt respects the memory policy. Fixes: eb02f14c4a2b ("mm/hugetlb: allow overcommitting gigantic hugepages") Cc: stable@vger.kernel.org Signed-off-by: Sourav Panda --- Changes in v2: - Fixed in alloc_fresh_hugetlb_folio(), because we also need a right node_m= ask for alloc_contig_frozen_pages() as suggested by Muchun. - Added node_isset() check to the initial preferred node allocation in huge= tlb_cma_alloc_frozen_folio() to respect memory policy, as suggested by Sash= iko. - Added Cc stable as suggested by Andrew Morton. - v1: https://lore.kernel.org/linux-mm/20260702215713.627941-1-souravpanda@= google.com/ mm/hugetlb.c | 3 +++ mm/hugetlb_cma.c | 2 +- 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/mm/hugetlb.c b/mm/hugetlb.c index 571212b80835..ab5deba4f7a1 100644 --- a/mm/hugetlb.c +++ b/mm/hugetlb.c @@ -1865,6 +1865,9 @@ static struct folio *alloc_fresh_hugetlb_folio(struct= hstate *h, { struct folio *folio; =20 + if (!nmask) + nmask =3D &cpuset_current_mems_allowed; + folio =3D only_alloc_fresh_hugetlb_folio(h, gfp_mask, nid, nmask, NULL); if (folio) hugetlb_vmemmap_optimize_folio(h, folio); diff --git a/mm/hugetlb_cma.c b/mm/hugetlb_cma.c index 39344d6c78d8..79dbd0baafa3 100644 --- a/mm/hugetlb_cma.c +++ b/mm/hugetlb_cma.c @@ -34,7 +34,7 @@ struct folio *hugetlb_cma_alloc_frozen_folio(int order, g= fp_t gfp_mask, if (!hugetlb_cma_size) return NULL; =20 - if (hugetlb_cma[nid]) + if (hugetlb_cma[nid] && node_isset(nid, *nodemask)) page =3D cma_alloc_frozen_compound(hugetlb_cma[nid], order); =20 if (!page && !(gfp_mask & __GFP_THISNODE)) { --=20 2.55.0.rc0.799.gd6f94ed593-goog