From nobody Sat Jul 25 16:51:29 2026 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 48405309F00 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=fTrPndtWpVQBQ2qOFJQn21cnllPeEEufeLnzTiZ7QODFfhR4GqQzekXA09T7UWDFrVQ5sgOZvbKL4irvSYAhP0KFE/Tq6tgbdna0h/XBsc2XP5wdgy5HynbXl7XNgVXzU8gYkA60KtaVV/Cws4Y67Rn3NUbrpQ3B+Ys9Bir/BVo= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1784162742; c=relaxed/simple; bh=45N8Tw0nbFR/tUcRrbcencZ0MXmIl52x5c713vTbZqU=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=T6xgEigsejAEo8b0NaadVOvgWqJsRsHHFNk8fTXFSMIZxZL4OTOdEcK7ZeQemNuE95BUpcUfo5DHnEgF6Pq/m1aUto4WE1hT0ybiwk9bFGOWTkStC5RPaUXtH68Do7H//3Y/iRJ7gddlpsp7LaGwkuYdvrysSoQqc0Zu9RzhRuM= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=jRs7WOuO; 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="jRs7WOuO" Received: by smtp.kernel.org (Postfix) with ESMTPSA id F37C01F000E9; Thu, 16 Jul 2026 00:45:40 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1784162741; bh=vFBYIn8JjXDFeh03QvQBuEg35l79xVLBqEMTfqo+BhM=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=jRs7WOuO+rewZ87f1FwL/ix8vePSY+k0BCCApd6tTnpuGg2fi2wY8aiJup4mL5B11 qopYIJQznvrUBCPostqMnrRl4sC6XdOj4NQL7ZqNZlJ2bitcIq9r5Jwd3ZblfergAC oyWk23n9vYv3oLtbK4PEvLQiZEJfGvFo/t7ELZzR1TACvfsG3y7KJmEJVPg+QEO9KY 6SKm/L600isLjfRTC54l/U3Wf9REYsSBwCgsw25wPyQomhKSS4BuMLXZ2m7qpNCJVo 7kP7IdZUhDlP0Gh72/x82WrstjuJ74E6JK9L1+vlNGPhw59UvRTLEnJRa86GQrdgp5 rfpwDCW86SlFQ== Received: by paulmck-ThinkPad-P17-Gen-1.home (Postfix, from userid 1000) id BDFCACE0569; 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 1/2] smp: Avoid invalid per-CPU CSD lookup with CSD lock debug Date: Wed, 15 Jul 2026 17:45:38 -0700 Message-Id: <20260716004539.13983-1-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: quoted-printable Content-Type: text/plain; charset="utf-8" From: Chuyi Zhou Commit b0473dcd4b1d ("smp: Improve smp_call_function_single() CSD-lock diagnostics") made smp_call_function_single() use the destination CPU's csd_data when CSD lock debugging is enabled. That lets the debug code associate a stuck CSD lock with the target CPU, but it also means the CPU argument is used in per_cpu_ptr() before generic_exec_single() has a chance to validate it. This becomes unsafe when smp_call_function_any() cannot find an online CPU in the supplied mask. In that case the selected CPU can be nr_cpu_ids, and the !wait path calls get_single_csd_data(cpu) before generic_exec_single() returns -ENXIO. With csdlock_debug_enabled set, that indexes the per-CPU offset array with an invalid CPU number. Use the destination CPU's csd_data only when the CPU number is within nr_cpu_ids. For invalid CPU numbers, fall back to the local CPU's csd_data and let generic_exec_single() perform the existing validation and return -ENXIO. Fixes: b0473dcd4b1d ("smp: Improve smp_call_function_single() CSD-lock diag= nostics") Signed-off-by: Chuyi Zhou Reviewed-by: Paul E. McKenney Acked-by: Muchun Song Signed-off-by: Paul E. McKenney --- kernel/smp.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/kernel/smp.c b/kernel/smp.c index a0bb56bd8ddadb..dc6582bb35d084 100644 --- a/kernel/smp.c +++ b/kernel/smp.c @@ -380,7 +380,8 @@ static DEFINE_PER_CPU_SHARED_ALIGNED(call_single_data_t= , csd_data); #ifdef CONFIG_CSD_LOCK_WAIT_DEBUG static call_single_data_t *get_single_csd_data(int cpu) { - if (static_branch_unlikely(&csdlock_debug_enabled)) + if (static_branch_unlikely(&csdlock_debug_enabled) && + (unsigned int)cpu < nr_cpu_ids) return per_cpu_ptr(&csd_data, cpu); return this_cpu_ptr(&csd_data); } --=20 2.40.1 From nobody Sat Jul 25 16:51:29 2026 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: quoted-printable Content-Type: text/plain; charset="utf-8" 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 diag= nostics") 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_si= ngle_data_t *csd) trace_csd_function_exit(func, csd); } =20 -#ifdef CONFIG_CSD_LOCK_WAIT_DEBUG - static DEFINE_STATIC_KEY_MAYBE(CONFIG_CSD_LOCK_WAIT_DEBUG_DEFAULT, csdlock= _debug_enabled); =20 +#ifdef CONFIG_CSD_LOCK_WAIT_DEBUG + /* * Parse the csdlock_debug=3D 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) =20 static __always_inline void csd_lock(call_single_data_t *csd) { - csd_lock_wait(csd); - csd->node.u_flags |=3D 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 =3D 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 |=3D CSD_FLAG_LOCK; + } =20 /* * prevent CPU from reordering the above assignment --=20 2.40.1