From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=35.155.198.111 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1794002707; b=RVkI0BkUgVmE5wq4A0RurMCjL+sQQUL8Vkhwrlxq++PxtvHuPqeROWq69eJTJZi5NUQn oLysHTIsHZXzp8QPLD38pM77A3vU0XUM41ezAKwuxekflKK5XxDCf+3195BXCjX33eszx 5udAd77Yw/ptohHiB7Sg+uGvgyKxdOGIzmpxYGW+ds5NrrduFDXvsBYPyPEoFiMD38jeY SLzOX7Uso/zW2lDyFOes5ZvynGUMPT/hs++4m/bw3BGYrQ0yz0zFpI0HNNk2DzjbVHB/R KBVD14glp46VK2lFqocfxyHaKh2UEEqYPxPNSDMDvf8pOi3TIJUglMTtH54W03vloxw== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1794002707; h=DMARC-Filter:DKIM-Signature:X-CSE-ConnectionGUID:X-CSE-MsgGUID: X-IronPort-AV:Received:Received:X-Farcaster-Flow-ID:Received:Received: From:To:CC:Subject:Date:Message-ID:X-Mailer:In-Reply-To:References: MIME-Version:Content-Transfer-Encoding:Content-Type:X-Originating-IP: X-ClientProxiedBy; bh=JQrnZyzpIUupH0TaN2kMzMCbwSY3jfKGJ+ewuWVPB6I=; b=J4d90e4IJtcCXqB03JhtB5xMI0kR8XVM83PAHvd4+VmEBeQN/uLpNdm3HOXpveecaQXs Z1evlYn2zpKx+vNTUaVlsrmqejnQjLiLRpxGpMwlopIiwY7+IPM7RemC0h9J6e/ZM/Elf xKDuT3aPswiXPlAuJ6XodMX+cMRHc4amMKvW0L4wEMMziioC5aSXuhxwx9krjZ7cyZk1y 1VNJgZDNvsxaoPyDgXevAYdUc8gNOd6yufPA8F4yIpAmc3XnbCqfqbQM02PFDswr/N42A y6/XStoukGzw8vMjx1VlCf+wzgtk0Htrke0+ghUjKI0FWe0gPTg7ZbB7Rjx3wcjH0/Q== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=amazon.com; spf=pass smtp.mailfrom=amazon.com; dkim=pass header.d=amazon.com header.i=@amazon.com header.a=rsa-sha256 header.s=amazoncorp2 header.b=X3sNcK8Y; arc=none smtp.remote-ip=35.155.198.111 Received: from pdx-out-009.esa.us-west-2.outbound.mail-perimeter.amazon.com (pdx-out-009.esa.us-west-2.outbound.mail-perimeter.amazon.com [35.155.198.111]) (using TLSv1.2 with cipher ECDHE-ECDSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 2281A606A32C for ; Fri, 6 Nov 2026 22:05:06 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 2281A606A32C Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=amazon.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=amazon.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key, unprotected) header.d=amazon.com header.i=@amazon.com header.a=rsa-sha256 header.s=amazoncorp2 header.b=X3sNcK8Y DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amazon.com; i=@amazon.com; q=dns/txt; s=amazoncorp2; t=1794002707; x=1825538707; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=JQrnZyzpIUupH0TaN2kMzMCbwSY3jfKGJ+ewuWVPB6I=; b=X3sNcK8YuadY4HoaRiEGX2jddbMHu79DradC34i/q6nT/36y1QWZsDc6 tBDIrtXdonMXB+caZvF5GO3mbiFzGZveMafQVdnKK+rFUsVhNdFBGIdAT zZ/0A6VG15HPfTHqNLqd+uNVYoFA8fbO/+6JyYPQKCsqC5QJA9JnxPsbP oLUVMNhb0+25fksPtgcxw+Ua1Jdx2/yM7uoIW8g16ArWyd419WR1JkTbJ 2GxWRUHnzBq5CFnoKHatf1C8lhlk/WuMN52oaODkZF0RhchFP2OBGJ70f zfV6GL3wwusrqbHAfCNmwrclsOy7UExrK1BK3Iy2WtbQ9zDzjow1xqPSA g==; X-CSE-ConnectionGUID: BsP+PFkDQeq/tQoMfeCXqg== X-CSE-MsgGUID: 56EaeuCBTlu51Vy6K1srYA== X-IronPort-AV: E=Sophos;i="6.27,199,1787011200"; d="scan'208";a="33262472" Received: from ip-10-5-6-203.us-west-2.compute.internal (HELO smtpout.naws.us-west-2.prod.farcaster.email.amazon.dev) ([10.5.6.203]) by internal-pdx-out-009.esa.us-west-2.outbound.mail-perimeter.amazon.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 06 Nov 2026 22:05:06 +0000 Received: from EX19MTAUWA002.ant.amazon.com [205.251.233.178:8940] by smtpin.naws.us-west-2.prod.farcaster.email.amazon.dev [10.0.22.238:2525] with esmtp (Farcaster) id 0df736da-0ef7-4954-a906-dd90519d5c1f; Fri, 6 Nov 2026 22:05:06 +0000 (UTC) X-Farcaster-Flow-ID: 0df736da-0ef7-4954-a906-dd90519d5c1f Received: from EX19D001UWA001.ant.amazon.com (10.13.138.214) by EX19MTAUWA002.ant.amazon.com (10.250.64.202) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA) id 15.2.2562.56; Fri, 6 Nov 2026 22:05:06 +0000 Received: from dev-dsk-mkbund-2b-9ea2efba.us-west-2.amazon.com (10.2.100.138) by EX19D001UWA001.ant.amazon.com (10.13.138.214) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA) id 15.2.2562.56; Fri, 6 Nov 2026 22:05:05 +0000 From: Mark Bundschuh To: CC: , , , Ge Yang , Muchun Song , Oscar Salvador , Baolin Wang , Barry Song <21cnbao@gmail.com>, David Hildenbrand , Andrew Morton , Mark Bundschuh Subject: [PATCH v2 5.15 2/2] mm/hugetlb: fix kernel NULL pointer dereference when replacing free hugetlb folios Date: Fri, 6 Nov 2026 22:04:44 +0000 Message-ID: <20261106220445.2859944-3-mkbund@amazon.com> X-Mailer: git-send-email 2.50.1 In-Reply-To: <20261106220445.2859944-1-mkbund@amazon.com> References: <20261105070206.3094675-1-mkbund@amazon.com> <20261106220445.2859944-1-mkbund@amazon.com> X-Mailing-List: stable@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Content-Type: text/plain X-ClientProxiedBy: EX19D031UWC002.ant.amazon.com (10.13.139.212) To EX19D001UWA001.ant.amazon.com (10.13.138.214) From: Ge Yang [ Upstream commit 113ed54ad276c352ee5ce109bdcf0df118a43bda ] A kernel crash was observed when replacing free hugetlb folios: BUG: kernel NULL pointer dereference, address: 0000000000000028 PGD 0 P4D 0 Oops: Oops: 0000 [#1] SMP NOPTI CPU: 28 UID: 0 PID: 29639 Comm: test_cma.sh Tainted 6.15.0-rc6-zp #41 PREEMPT(voluntary) RIP: 0010:alloc_and_dissolve_hugetlb_folio+0x1d/0x1f0 RSP: 0018:ffffc9000b30fa90 EFLAGS: 00010286 RAX: 0000000000000000 RBX: 0000000000342cca RCX: ffffea0043000000 RDX: ffffc9000b30fb08 RSI: ffffea0043000000 RDI: 0000000000000000 RBP: ffffc9000b30fb20 R08: 0000000000001000 R09: 0000000000000000 R10: ffff88886f92eb00 R11: 0000000000000000 R12: ffffea0043000000 R13: 0000000000000000 R14: 00000000010c0200 R15: 0000000000000004 FS: 00007fcda5f14740(0000) GS:ffff8888ec1d8000(0000) knlGS:0000000000000000 CS: 0010 DS: 0000 ES: 0000 CR0: 0000000080050033 CR2: 0000000000000028 CR3: 0000000391402000 CR4: 0000000000350ef0 Call Trace: replace_free_hugepage_folios+0xb6/0x100 alloc_contig_range_noprof+0x18a/0x590 ? srso_return_thunk+0x5/0x5f ? down_read+0x12/0xa0 ? srso_return_thunk+0x5/0x5f cma_range_alloc.constprop.0+0x131/0x290 __cma_alloc+0xcf/0x2c0 cma_alloc_write+0x43/0xb0 simple_attr_write_xsigned.constprop.0.isra.0+0xb2/0x110 debugfs_attr_write+0x46/0x70 full_proxy_write+0x62/0xa0 vfs_write+0xf8/0x420 ? srso_return_thunk+0x5/0x5f ? filp_flush+0x86/0xa0 ? srso_return_thunk+0x5/0x5f ? filp_close+0x1f/0x30 ? srso_return_thunk+0x5/0x5f ? do_dup2+0xaf/0x160 ? srso_return_thunk+0x5/0x5f ksys_write+0x65/0xe0 do_syscall_64+0x64/0x170 entry_SYSCALL_64_after_hwframe+0x76/0x7e There is a potential race between __update_and_free_hugetlb_folio() and replace_free_hugepage_folios(): CPU1 CPU2 __update_and_free_hugetlb_folio replace_free_hugepage_folios folio_test_hugetlb(folio) -- It's still hugetlb folio. __folio_clear_hugetlb(folio) hugetlb_free_folio(folio) h = folio_hstate(folio) -- Here, h is NULL pointer When the above race condition occurs, folio_hstate(folio) returns NULL, and subsequent access to this NULL pointer will cause the system to crash. To resolve this issue, execute folio_hstate(folio) under the protection of the hugetlb_lock lock, ensuring that folio_hstate(folio) does not return NULL. Link: https://lkml.kernel.org/r/1747884137-26685-1-git-send-email-yangge1116@126.com Fixes: 04f13d241b8b ("mm: replace free hugepage folios after migration") Signed-off-by: Ge Yang Reviewed-by: Muchun Song Reviewed-by: Oscar Salvador Cc: Baolin Wang Cc: Barry Song <21cnbao@gmail.com> Cc: David Hildenbrand Cc: Signed-off-by: Andrew Morton [ 5.15: Use pages instead of folios. ] Signed-off-by: Mark Bundschuh --- mm/hugetlb.c | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/mm/hugetlb.c b/mm/hugetlb.c index 4fb98320f9df..3d7e8826e452 100644 --- a/mm/hugetlb.c +++ b/mm/hugetlb.c @@ -2756,12 +2756,20 @@ int replace_free_hugepage_folios(unsigned long start_pfn, unsigned long end_pfn) while (start_pfn < end_pfn) { page = compound_head(pfn_to_page(start_pfn)); + + /* + * The page might have been dissolved from under our feet, so make sure + * to carefully check the state under the lock. + */ + spin_lock_irq(&hugetlb_lock); if (PageHuge(page)) { h = page_hstate(page); } else { + spin_unlock_irq(&hugetlb_lock); start_pfn++; continue; } + spin_unlock_irq(&hugetlb_lock); if (!page_count(page)) { ret = alloc_and_dissolve_huge_page(h, page, &isolate_list); -- 2.50.1