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 6D31C367F4D for ; Mon, 27 Apr 2026 05:47:45 +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=1777268865; cv=none; b=rG4Fai9zLvhXVGcgODkZeXWyuooT9gaWrICM28ez/8FUpLeAL8Kfj3Ng57HGdZrAgJ6WgR3Y902CRWcR6JzLXurTmGk+PqGX0urryDbCOPaT1mtcEoZOxESHj37xUFyUWBOa17gkg8nhwSf04rur06fotnMT6f0QPlT0Wa0oAys= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1777268865; c=relaxed/simple; bh=s6RWkj+HUih9pUmMqw2vjrtv/MWn2+njvQOUZJznKuY=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=X8hJz5Q2lUu++l+o65y6ivHUEjC3MhHb5rC+mM1J4iDB3QRRgP0ZuinAx6oFlQi7WwpsiSPf6oepHTWU3a1YtjNGc3jTrhdLsOYuIHF3CIy+99yw7UuWNQrQ90m3rioZw10WmoIx8b3MS6ZZ8+HQ0OlAAmOZS//1SX1tihJNpck= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=cUBNH1Da; 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="cUBNH1Da" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 2E985C2BCB4; Mon, 27 Apr 2026 05:47:43 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1777268865; bh=s6RWkj+HUih9pUmMqw2vjrtv/MWn2+njvQOUZJznKuY=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=cUBNH1Da8+rgcNrWLu/NS8WKGa50Nfcmuq/qgpI0tS114WnTsOpODhMiCubEqJOv+ DY8eK0USdbnNsPHRoAsbxIrek+eLAti5oijZpfZDu3HTbgw03OyaM7F5iMfuEU/1I1 yw1vXE0rW/iLqTA1GWfmoV9RewEExjj2BxJvth/uH/4xpVrajB9GWgseBX1QFNLLNy 3m6ZadlQB1q9Ziuounl2qn2BF1fO0NbMl1EqEtXUnYz8/hxsg55Pc0MQ28cn4QNtdD h5NdPjidWU+BuPTD5Bnh9dLYOKmF1clsELiKndcnp8pcJ5PVmLag3BtOFA8hYJK6xI GLtMDrattAWsA== From: "Harry Yoo (Oracle)" To: Vlastimil Babka , Harry Yoo , Andrew Morton , Alexei Starovoitov Cc: Harry Yoo , Hao Li , Christoph Lameter , David Rientjes , Roman Gushchin , linux-mm@kvack.org, linux-kernel@vger.kernel.org Subject: [PATCH mm-hotfixes 2/2] mm/slub: return NULL early from kmalloc_nolock() in NMI on UP Date: Mon, 27 Apr 2026 14:47:35 +0900 Message-ID: <20260427054736.566559-3-harry@kernel.org> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20260427054736.566559-1-harry@kernel.org> References: <20260427054736.566559-1-harry@kernel.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit 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 a 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 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