Received: by 2002:ab2:3319:0:b0:1ef:7a0f:c32d with SMTP id i25csp111750lqc; Thu, 7 Mar 2024 11:48:53 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCWb4cUFyr7QNtVe40JqGwOsqDO6yLVvM3KQAJXZI8oS8i9OWSU57NopS+VinC+XjZwfIxXtXgpKthdNKVvBbvAyx06b3QxNd/NVZvB54w== X-Google-Smtp-Source: AGHT+IHyNJtIJRTg4OBB9coxRB+X1KBFdMJmjPKyhFPEWPlIqLcSXs7YPnEnObCnq2PEiI2KBHzd X-Received: by 2002:a17:903:41c1:b0:1dc:af70:9f71 with SMTP id u1-20020a17090341c100b001dcaf709f71mr3785339ple.20.1709840933414; Thu, 07 Mar 2024 11:48:53 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1709840933; cv=pass; d=google.com; s=arc-20160816; b=mfB7TMTbIZYG4HIhqVmspvdw0zhmVvslPKgAVSCW3/g9om2DSCPIcubYbbXyeIctke +3Fv2sVkRTaRQpHtWGg/zpFOxF9+HwaG72pUYrjtCe3oojZbBFnE+oQJiCm8BlVgbiNI Sj5Y27iCK3R25FRPDH2P+nDy+jaJVJIqN0IP1PbnPQPM10Kiots+Wcx5jOHUVxAuAS2E BcEdl6wb2UOsnkFdldlvLUm0rkE0ZlgXjPcWFaJZwlaPS4tXdZPdbixxwAgY0E6HMQ8D fjJJge4s7Y9JPXmrXjF0DL9ScvJuCLz2AiI8ZByTI99SAhsJjAzRh+ApilKrXdyV0gDS fVgw== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=in-reply-to:content-disposition:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:references:reply-to:message-id :subject:cc:to:from:date:dkim-signature; bh=kcv1wB82QvaLFHMeVl5ChDci331WIwhLiOP9YFTQp2A=; fh=x4jX7DJCpIoNFusqnt1UOCguYaNLis+cTWSDT907oSM=; b=x+RMrurWauqpz63+4u3kRiulZnrO5IZm4jGTSJkVEY9Vi2iBJWY5wtC/rc38uenuVU NzIr6uHuJzApnImV7F7wbAk2I7faXVBBqiEEGj+L7XO4iySLF6GRRe7qBLS2gSw6lKPA fb+LxmOKrhZVqJYkxeySG+IlM6JbI/dOIcaHBw0LKIow15L4jfa4xhyutUuaevRyamPO VXq12XPevNGuW97DHVZjUxF1Wo9amUA46aP2RVqSuKfmsfp+rEI+JmaPnenxXawZQBvZ h7axRcIDQoL7s4ElXh7kKpxlgEds8Ix/Ez83uydE0e/G2W9845x0afufIVJK4Kl/xQG5 mIpg==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=TrQCKxgt; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-kernel+bounces-96128-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-96128-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from sv.mirrors.kernel.org (sv.mirrors.kernel.org. [2604:1380:45e3:2400::1]) by mx.google.com with ESMTPS id q12-20020a170902dacc00b001dc8df92f1dsi13906656plx.234.2024.03.07.11.48.53 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 07 Mar 2024 11:48:53 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-96128-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) client-ip=2604:1380:45e3:2400::1; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=TrQCKxgt; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-kernel+bounces-96128-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-96128-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by sv.mirrors.kernel.org (Postfix) with ESMTPS id 5523B282B22 for ; Thu, 7 Mar 2024 19:47:44 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 7209A1369B4; Thu, 7 Mar 2024 19:47:37 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="TrQCKxgt" 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 5FDB4136989; Thu, 7 Mar 2024 19:47:36 +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=1709840856; cv=none; b=tmImzljWWREsS5epNfkNf3PfmSmYe95NQitv+THRRN/MQ4NbDX/z/F1SA2jdto+a+iimdWESkNY2b2lKabCkg1eO/6j/i+d+7vukbZnHoSH5yPAFsLerMylI1dKGmO78omlTDpiAmBdWjwX9TMVZsVcxOjLbRSo+xNc3t4VUEX8= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1709840856; c=relaxed/simple; bh=OHMHjrmXLAl+M5prZnWsXYc3CPrx3uhUlRe0pX2R6CM=; h=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=jSutElqdKNdF3aH1Mc2qO+VLHkhW8T1JSxe/OhJ17g2ulbHph/jhvWDYSEx9CJEj0v8H7FUwhXPaYLQrGYeVyw+9K3GAJEXFVtAHBI0Xn3eQlvdaTugOlgGZ5SdDMjEdRRp/4EC1ZTCUNpT2XwU2Ifj+vHBCBEPQLXqSOpmm8u8= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=TrQCKxgt; arc=none smtp.client-ip=10.30.226.201 Received: by smtp.kernel.org (Postfix) with ESMTPSA id D894FC433C7; Thu, 7 Mar 2024 19:47:35 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1709840855; bh=OHMHjrmXLAl+M5prZnWsXYc3CPrx3uhUlRe0pX2R6CM=; h=Date:From:To:Cc:Subject:Reply-To:References:In-Reply-To:From; b=TrQCKxgt61A6b11OT+EDHqfSLuUFzmlZq9xGRsenob5TEn7/IstinO10wOl6paOnv GY2tT4Usoc6RQq4Dy8ZPnUDb97YEJuQTClOTvMYvo8KJ/fr4ojm4Pvk4QTMoIa4mgY SaoWufgDt5RgKinvMUAIKQqTjvec7oVGmT95SdCMYb3RS6RU8tjehGcY+yVt/oUzWp O0UbBuMkEAI1X97PhfWKWmUr97cmzOl5RtovIJ8sNEVR18wvBwdWRCWa+jRH+MKVLW G2cbyy3VcX6vnxd2Oat/J5RI/czw0AC/Nn15j4BeLsypo8ULPaUHncmbogr1ah3GWo Oer+72wTykJFw== Received: by paulmck-ThinkPad-P17-Gen-1.home (Postfix, from userid 1000) id 62B50CE0716; Thu, 7 Mar 2024 11:47:35 -0800 (PST) Date: Thu, 7 Mar 2024 11:47:35 -0800 From: "Paul E. McKenney" To: Mathieu Desnoyers Cc: Linus Torvalds , Steven Rostedt , linke li , joel@joelfernandes.org, boqun.feng@gmail.com, dave@stgolabs.net, frederic@kernel.org, jiangshanlai@gmail.com, josh@joshtriplett.org, linux-kernel@vger.kernel.org, qiang.zhang1211@gmail.com, quic_neeraju@quicinc.com, rcu@vger.kernel.org Subject: Re: [PATCH] rcutorture: Fix rcu_torture_pipe_update_one()/rcu_torture_writer() data race and concurrency bug Message-ID: Reply-To: paulmck@kernel.org References: <20240306135504.2b3872ef@gandalf.local.home> <20240306142738.7b66a716@rorschach.local.home> <83b47424-e5e0-46de-aa63-d413a5aa6cec@paulmck-laptop> <851dc594-d2ea-4050-b7c6-e33a1cddf3a1@efficios.com> <72b14322-78c1-4479-9c4e-b0e11c1f0d53@paulmck-laptop> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: On Thu, Mar 07, 2024 at 08:53:05AM -0500, Mathieu Desnoyers wrote: > On 2024-03-06 22:37, Paul E. McKenney wrote: > > On Wed, Mar 06, 2024 at 10:06:21PM -0500, Mathieu Desnoyers wrote: > [...] > > > > > As far as the WRITE_ONCE(x, READ_ONCE(x) + 1) pattern > > > is concerned, the only valid use-case I can think of is > > > split counters or RCU implementations where there is a > > > single updater doing the increment, and one or more > > > concurrent reader threads that need to snapshot a > > > consistent value with READ_ONCE(). > > > [...] > > > > So what would you use that pattern for? > > > > One possibility is a per-CPU statistical counter in userspace on a > > fastpath, in cases where losing the occasional count is OK. Then learning > > your CPU (and possibly being immediately migrated to some other CPU), > > READ_ONCE() of the count, increment, and WRITE_ONCE() might (or might not) > > make sense. > > > > I suppose the same in the kernel if there was a fastpath so extreme you > > could not afford to disable preemption. > > > > At best, very niche. > > > > Or am I suffering a failure of imagination yet again? ;-) > > The (niche) use-cases I have in mind are split-counters and RCU > grace period tracking, where precise counters totals are needed > (no lost count). > > In the kernel, this could be: Thank you for looking into this! > - A per-cpu counter, each counter incremented from thread context with > preemption disabled (single updater per counter), read concurrently by > other threads. WRITE_ONCE/READ_ONCE is useful to make sure there > is no store/load tearing there. Atomics on the update would be stronger > than necessary on the increment fast-path. But if preemption is disabled, the updater can read the value without READ_ONCE() without risk of concurrent update. Or are you concerned about interrupt handlers? This would have to be a read from the interrupt handler, given that an updated from the interrupt handler could result in a lost count. > - A per-thread counter (e.g. within task_struct), only incremented by the > single thread, read by various threads concurrently. Ditto. > - A counter which increment happens to be already protected by a lock, read > by various threads without taking the lock. (technically doable, but > I'm not sure I see a relevant use-case for it) In that case, the lock would exclude concurrent updates, so the lock holder would not need READ_ONCE(), correct? > In user-space: > > - The "per-cpu" counter would have to use rseq for increments to prevent > inopportune migrations, which needs to be implemented in assembler anyway. > The counter reads would have to use READ_ONCE(). Fair enough! > - The per-thread counter (Thread-Local Storage) incremented by a single > thread, read by various threads concurrently, is a good target > for WRITE_ONCE()/READ_ONCE() pairing. This is actually what we do in > various liburcu implementations which track read-side critical sections > per-thread. Agreed, but do any of these use WRITE_ONCE(x, READ_ONCE(x) + 1) or similar? > - Same as for the kernel, a counter increment protected by a lock which > needs to be read from various threads concurrently without taking > the lock could be a valid use-case, though I fail to see how it is > useful due to lack of imagination on my part. ;-) In RCU, we have "WRITE_ONCE(*sp, *sp + 1)" for this use case, though here we have the WRITE_ONCE() but not the READ_ONCE() because we hold the lock excluding any other updates. > I'm possibly missing other use-cases, but those come to mind as not > involving racy counter increments. > > I agree that use-cases are so niche that we probably do _not_ want to > introduce ADD_SHARED() for that purpose in a common header file, > because I suspect that it would then become misused in plenty of > scenarios where the updates are actually racy and would be better > served by atomics or local-atomics. I agree that unless or until we have a reasonable number of use cases, we should open-code it. With a big fat comment explaining how it works. ;-) Thanx, Paul