From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id A5662390234; Mon, 27 Apr 2026 07:10:34 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1777273834; cv=none; b=cYo60rzW2a0ldEZwrFdktHYn3W1Qwmy0TdYdV9PCF89hsCV263sn2UdZ1jVB19pO5keBOop2KxOP7PCB5juQqSxrAoADTty4SIVpU18PDhlDPb4+V0oHVKmKUo84G91AW9HFTB2vHvB9+COESkcW6OHatrMMiJ0IRYgIhrEmI0k= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1777273834; c=relaxed/simple; bh=XdF7XzRa+uODLYSJER4F3+l7tRbl97mH0ZDNkW5g1/M=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=OUosx3WmdW9jsqX1oUvDCEfAS/v9TjP0cYYbDJrsDldVTjEkmrm8ND4QDTT/Ez5OmsHnyNjcewmmA3VW4+s4nHHYn+Xi8WWgEQhHonfSf0D3cnFRBNc5nOtrqmN0Iy6JC1y8Xn0L0VXgLg90EbDFCrsAIpjKb8ejlhCPlTnAP/A= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=h1WPTm/7; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="h1WPTm/7" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 6265CC2BCB9; Mon, 27 Apr 2026 07:10:31 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1777273834; bh=XdF7XzRa+uODLYSJER4F3+l7tRbl97mH0ZDNkW5g1/M=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=h1WPTm/7QlbvpxONpZIjyQnpJr6EYMV7k2vo7US6F0HAY4IArxzBTGL7BWC/X0XGj 9bNX81UbibUZo0CqTQptZWXTyogqqcq1VvMllbSjGKMZV0ejjZ460LuerJVLdwwY/T 74PAP8PWrie/s6nHPH6KSpGaff6DD3DHTtaA053lf2fB/juXjq13WQh5JHOoU+51mm 3soWU4q0jEgU01GZ5xgUlNVDpWrcoadEihit/OUTmjbBt/vZ9DAHvbbeh310IyeFdo ENOTUXZ+vvq4OMeC7oMnQOsGl23ewtTJV70bYY/B8/DMKEGNGN+9G/PPtQr4Kigsk4 AMG0KIQIrCG7Q== From: "Harry Yoo (Oracle)" Date: Mon, 27 Apr 2026 16:09:53 +0900 Subject: [PATCH mm-hotfixes v2 2/2] mm/slab: return NULL early from kmalloc_nolock() in NMI on UP Precedence: bulk X-Mailing-List: stable@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 7bit Message-Id: <20260427-nolock-api-fix-v2-2-a6b83a92d9a4@kernel.org> References: <20260427-nolock-api-fix-v2-0-a6b83a92d9a4@kernel.org> In-Reply-To: <20260427-nolock-api-fix-v2-0-a6b83a92d9a4@kernel.org> To: Andrew Morton , Vlastimil Babka , Suren Baghdasaryan , Michal Hocko , Brendan Jackman , Johannes Weiner , Zi Yan , Harry Yoo , Shakeel Butt , Alexei Starovoitov , Hao Li , Christoph Lameter , David Rientjes , Roman Gushchin Cc: linux-mm@kvack.org, linux-kernel@vger.kernel.org, stable@vger.kernel.org X-Mailer: b4 0.16-dev On UP kernels (!CONFIG_SMP), spin_trylock() is a no-op that unconditionally succeeds even when the lock is already held. As a result, kmalloc_nolock() called from NMI context can re-enter the slab allocator and acquire n->list_lock that the interrupted context is already holding, corrupting slab state. With CONFIG_DEBUG_SPINLOCK on UP, the following BUG is triggered with the slub_kunit test module: BUG: spinlock trylock failure on UP on CPU#0, kunit_try_catch/243 [...] Call Trace: dump_stack_lvl+0x3f/0x60 do_raw_spin_trylock+0x41/0x50 _raw_spin_trylock+0x24/0x50 get_from_partial_node+0x120/0x4d0 ___slab_alloc+0x8a/0x4c0 kmalloc_nolock_noprof+0x164/0x310 [...] Fix this by returning NULL early when invoked from NMI on a UP kernel. Link: https://lore.kernel.org/linux-mm/ad_cqe51pvr1WaDg@hyeyoo Cc: Fixes: af92793e52c3 ("slab: Introduce kmalloc_nolock() and kfree_nolock().") Signed-off-by: Harry Yoo (Oracle) --- mm/slub.c | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/mm/slub.c b/mm/slub.c index 92362eeb13e5..b4ec15df92f6 100644 --- a/mm/slub.c +++ b/mm/slub.c @@ -5339,6 +5339,10 @@ void *kmalloc_nolock_noprof(size_t size, gfp_t gfp_flags, int node) if (IS_ENABLED(CONFIG_PREEMPT_RT) && (in_nmi() || in_hardirq())) return NULL; + /* On UP, spin_trylock() always succeeds even when it is locked */ + if (!IS_ENABLED(CONFIG_SMP) && in_nmi()) + return NULL; + retry: if (unlikely(size > KMALLOC_MAX_CACHE_SIZE)) return NULL; -- 2.43.0