From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from sg-3-112.ptr.tlmpb.com (sg-3-112.ptr.tlmpb.com [101.45.255.112]) (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 CC8313655D5 for ; Mon, 8 Jun 2026 11:25:57 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=101.45.255.112 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1780917960; cv=none; b=oEhMgKfz/X27+i0bBkpVTIkTukIL/U6GZYfj1gS8fAG80xKj8E+kkMVRZn+HNrJZa2/oegvNUQDsFr1GvLRQXbQacdW+Eof0hvy0nEVj+flm6QBMVH2Ns463nwbVwd8ea7Pf6MJyqUznIe75A16e/Q8xOO5tIh5lqNkvK8V+kTE= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1780917960; c=relaxed/simple; bh=1pDPiUxMsp99Iby2/b2Agncj4AYRUXXvZ/5FZpaltQQ=; h=In-Reply-To:Date:From:Mime-Version:References:To:Subject: Message-Id:Content-Type:Cc; b=VFfYBiVphUKOu3b9XnV/lH2psrZjiySzw4+J6uQoOkKkqU5KfHCQ4HMzeGwSgbifWNWN9t2CTLxzH+4AI0028AFVD8BFwZOlEn7HYwHx0o6Y9Dmbc0XtThHdfEwd9OK1NN93KgmSzcvYkTlMVRGNWtjqp2X/9Yk1+68gRvdTRQU= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=bytedance.com; spf=pass smtp.mailfrom=bytedance.com; dkim=pass (2048-bit key) header.d=bytedance.com header.i=@bytedance.com header.b=qwcv9Vf2; arc=none smtp.client-ip=101.45.255.112 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=bytedance.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=bytedance.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=bytedance.com header.i=@bytedance.com header.b="qwcv9Vf2" DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; s=2212171451; d=bytedance.com; t=1780917915; h=from:subject: mime-version:from:date:message-id:subject:to:cc:reply-to:content-type: mime-version:in-reply-to:message-id; bh=Oc3bseWnSu9RnSSbp0B3bI+ZCOvjs+wLsRKN5ExsiHY=; b=qwcv9Vf2fbPjukHyhJjyx1IIc2Lckan8M0GomeFEl2HzIbJUN/ALAxFvI3HKXufW0oNSg9 /OCWT7h+mB3r59ydQ4NvTg5+cx7NySK6ORcieo9O/XnxgCNQvPCtxnpaIa/kdwxTtdpnQQ dGHGKRvJTjmXixF7sFfgQ/y9NUXqiUGWS4UmUr07S5b8fRE7kKSJi6ZgUaPVlmFf3FnIQ3 LzC0L4WQjDvqcpJV/khsenf/jJa8z1u+S8f30EgHxY92Op+vHWjek5zvYnUGmmXELx5uGF 9KXb2x+bNoA6sTYSKTb5Iabo5Hn8nRicdhpkdgAwUAvSA8jjPqnpKPArsrNSsg== Content-Transfer-Encoding: 7bit In-Reply-To: <20260608112349.3955175-1-zhouchuyi@bytedance.com> Date: Mon, 8 Jun 2026 19:23:49 +0800 From: "Chuyi Zhou" Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20260608112349.3955175-1-zhouchuyi@bytedance.com> To: , , , , , Subject: [PATCH 2/2] smp: Make CSD lock acquisition atomic for debug mode Message-Id: <20260608112349.3955175-3-zhouchuyi@bytedance.com> X-Original-From: Chuyi Zhou Content-Type: text/plain; charset=UTF-8 Cc: , "Chuyi Zhou" X-Lms-Return-Path: X-Mailer: git-send-email 2.20.1 Commit b0473dcd4b1d ("smp: Improve smp_call_function_single() CSD-lock diagnostics") changed smp_call_function_single() so that, when CSD lock debugging is enabled, async !wait calls use the destination CPU csd_data. That improves diagnostics, but it also removes the single-writer property that made the old csd_lock() safe: multiple CPUs can now prepare the same destination CPU CSD concurrently. csd_lock() currently waits for CSD_FLAG_LOCK to clear and then sets the bit with a non-atomic read-modify-write. Two senders can both see an unlocked CSD, set the bit, overwrite the callback fields, and enqueue the same llist node. Re-adding a node that is already the queue head can make node->next point to itself, leaving the target CPU stuck walking call_single_queue. Later synchronous work, such as a TLB shootdown, can then remain queued and trigger soft-lockup warnings or panics. Keep the single csd_lock() implementation, but when CSD lock debugging is enabled, acquire CSD_FLAG_LOCK with try_cmpxchg_acquire(). This makes the destination CPU CSD a real atomic lock in the only configuration where it can be shared by multiple remote senders, while preserving the existing non-debug fast path. Fixes: b0473dcd4b1d ("smp: Improve smp_call_function_single() CSD-lock diagnostics") Signed-off-by: Chuyi Zhou --- kernel/smp.c | 26 ++++++++++++++++++++++---- 1 file changed, 22 insertions(+), 4 deletions(-) diff --git a/kernel/smp.c b/kernel/smp.c index dc6582bb35d0..b9448fe3b84f 100644 --- a/kernel/smp.c +++ b/kernel/smp.c @@ -137,10 +137,10 @@ csd_do_func(smp_call_func_t func, void *info, call_single_data_t *csd) trace_csd_function_exit(func, csd); } -#ifdef CONFIG_CSD_LOCK_WAIT_DEBUG - static DEFINE_STATIC_KEY_MAYBE(CONFIG_CSD_LOCK_WAIT_DEBUG_DEFAULT, csdlock_debug_enabled); +#ifdef CONFIG_CSD_LOCK_WAIT_DEBUG + /* * Parse the csdlock_debug= kernel boot parameter. * @@ -342,6 +342,10 @@ static __always_inline void csd_lock_wait(call_single_data_t *csd) smp_cond_load_acquire(&csd->node.u_flags, !(VAL & CSD_FLAG_LOCK)); } #else +static __always_inline void __csd_lock_wait(call_single_data_t *csd) +{ +} + static void csd_lock_record(call_single_data_t *csd) { } @@ -354,8 +358,22 @@ static __always_inline void csd_lock_wait(call_single_data_t *csd) static __always_inline void csd_lock(call_single_data_t *csd) { - csd_lock_wait(csd); - csd->node.u_flags |= CSD_FLAG_LOCK; + if (IS_ENABLED(CONFIG_CSD_LOCK_WAIT_DEBUG) && + static_branch_unlikely(&csdlock_debug_enabled)) { + unsigned int flags; + + for (;;) { + __csd_lock_wait(csd); + flags = READ_ONCE(csd->node.u_flags); + if (!(flags & CSD_FLAG_LOCK) && + try_cmpxchg_acquire(&csd->node.u_flags, &flags, + flags | CSD_FLAG_LOCK)) + break; + } + } else { + csd_lock_wait(csd); + csd->node.u_flags |= CSD_FLAG_LOCK; + } /* * prevent CPU from reordering the above assignment -- 2.20.1