From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-alma10-1.taild15c8.ts.net [100.103.45.18]) (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 4848F30C150 for ; Thu, 16 Jul 2026 00:45:41 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=100.103.45.18 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1784162742; cv=none; b=bkB0Vc/HmzsYwSKes4/bijSSB6swpa/eOiidHY1Uea+1foDkCev8XQ8sZlmu8eWNdiA5xL08JCruWOfi0pbtZFo264rMEosrgqAElIbOIcNwGAixyAcPPP07Ofq7BrsoddzUKa8P0G7IbX6pHqrpAI4etII0RiV3MbW8HejSxNE= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1784162742; c=relaxed/simple; bh=ZCbZDNup88OUl45fhlkvMPMqP0Nbh8kzA3Ff3kXp9YI=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=pZTrNFfIzM/DxXLf47MQ6dTYmv25L7+418e3ak+XjGyEawapGgt4XKp22B4VB4vZfS8CVs4Xnd/1yscCSLwLF7KOfmr6sCbTSVmi+dbmdFEyZ3PX/UBFQsun5s+wd7QzztFcki94PqowG2gETDD4keGi4SBW3U9R4fmPSBGNeRg= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=Uixib448; arc=none smtp.client-ip=100.103.45.18 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="Uixib448" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 0D0A41F00A3A; Thu, 16 Jul 2026 00:45:41 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1784162741; bh=k+bCZyBaTbXL+5vHyp9eYf2HUibbvWMY44yvOfrepeU=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=Uixib448t/dMgxnCnTKLIJyBSvRB/sSRtSizdXVQYKCRDXQngwAbhjyON/vyMP9Zc kRWugoViPl7ryTKr9B1TfEYzdkhTH59Nu06s+IG0wbfpQuMLuEmllGVcVrTHbSYLE2 t6TxMqHTdvcn3c63mqW4sRfhGq2RY4D3bnlMCAu/cEopyQb8g1wIklydvEmdiokQnS wLb0Ifw7XuqFevZcwjp49RA9oz/INWgN5tQY5s2kNM/+EVyU+LxgQccGsnzB1GbjFh fEKZxuHMezAFo5JEk5obRBlIzZ3+0chc+80zkCX1AlFlEKY7Zf+7fG9ea1s+EahkJ5 UwQCI0cytHCgQ== Received: by paulmck-ThinkPad-P17-Gen-1.home (Postfix, from userid 1000) id C10A6CE0B87; Wed, 15 Jul 2026 17:45:40 -0700 (PDT) From: "Paul E. McKenney" To: linux-kernel@vger.kernel.org Cc: Thomas Gleixner , Chuyi Zhou , Ulf Hansson , Muchun Song , Joel Fernandes , Marco Crivellari , "Rafael J. Wysocki" , "Paul E . McKenney" Subject: [PATCH 2/2] smp: Make CSD lock acquisition atomic for debug mode Date: Wed, 15 Jul 2026 17:45:39 -0700 Message-Id: <20260716004539.13983-2-paulmck@kernel.org> X-Mailer: git-send-email 2.40.1 In-Reply-To: <01070aaf-540c-403b-b589-5185f62eb29a@paulmck-laptop> References: <01070aaf-540c-403b-b589-5185f62eb29a@paulmck-laptop> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit From: Chuyi Zhou 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 Signed-off-by: Paul E. McKenney --- kernel/smp.c | 26 ++++++++++++++++++++++---- 1 file changed, 22 insertions(+), 4 deletions(-) diff --git a/kernel/smp.c b/kernel/smp.c index dc6582bb35d084..b9448fe3b84ff5 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.40.1