Received: by 2002:ab2:788f:0:b0:1ee:8f2e:70ae with SMTP id b15csp317160lqi; Wed, 6 Mar 2024 19:38:39 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCWIAxVeb/5CCO9rK+6vP83HyywLB8/NYfXDJDk+/usNyD3TN98JHAMdszZrhAWrWLKmvVNl9Vf7Lo6+9TQOm6tCSEWbD/oSNO8qrXKy8g== X-Google-Smtp-Source: AGHT+IFO5RQQb/ciwui6FaHyboh8OmZPmgL2TZaS/71cFl2lp6NHIKxSi/2/sDn4ClWoCN+CDkE9 X-Received: by 2002:a25:8681:0:b0:dc6:d6f6:cc13 with SMTP id z1-20020a258681000000b00dc6d6f6cc13mr14909230ybk.20.1709782719738; Wed, 06 Mar 2024 19:38:39 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1709782719; cv=pass; d=google.com; s=arc-20160816; b=GouWjgmHQ6LYp0uQnFloCekpcx6+eHt4QdsgcDkDOOhC1m77yGZaRLlPjS6pNR8Byo /WjcRsDimj3M9yuWmt+wsLbveU7FPj9UfoQukign2POC0NOL5v7+Wk0JikS+jBodiw3P AoW/xZDBjid/+6ij8i7iDeQM4rz0Vh1Ij7XVi85AQsJvDlfxDdlZriPABm04T0HaE6kE R6xbx6Z7WAPDfLpBlTo+Saze3GK4tDjU+ck1Aswd9Tc1MEz1rxvW7GiPWoIlkyS9RojA QDOFCcT9rXRnrIj2GrxOC/J6JiiH7yI1ft8Izv2UjI/9X3TIUE3hA9QNg4FgcDag6Wh8 JpmQ== 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=70FbvFuIaJPoQjUt2gUwyG34M/+e5J6RFrs5ehvGzuY=; fh=x4jX7DJCpIoNFusqnt1UOCguYaNLis+cTWSDT907oSM=; b=TBUytm6KDQnNGf/TytTlI6AQdHT++j6AvAu6d61NKV8mRLV9A225I8ReO71kqfLsvF HWexBZJycpKw9MZ+uSwWjpk1CSdm1wzUUNFss5ixtMFW6Mdy6lgCbIhX85Vv2OLhkZH9 gvi9LbmRWXc1XhXBS4eN8Y5brzyuXBf+Dx/ykEWi1y4IRh7Co9b0D7f3JmlIivl7foun o9mbXttFBgUp5WuRNqwHC4VIOVl4sd0KzDNJujAO5oBBn3N4tJlHNf5C42TjRgTlkOsH hTJr7J3Gv44qFe4Zcn5BXQszwYsyjvr0kYr3m3zBeIVPDOILIs9fi6DdN3D5cfFWq3Ue xPbg==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b="OGPJ/FCD"; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-kernel+bounces-94930-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.48.161 as permitted sender) smtp.mailfrom="linux-kernel+bounces-94930-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from sy.mirrors.kernel.org (sy.mirrors.kernel.org. [147.75.48.161]) by mx.google.com with ESMTPS id h38-20020a635326000000b005ced2a6b890si13113621pgb.668.2024.03.06.19.38.39 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 06 Mar 2024 19:38:39 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-94930-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.48.161 as permitted sender) client-ip=147.75.48.161; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b="OGPJ/FCD"; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-kernel+bounces-94930-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.48.161 as permitted sender) smtp.mailfrom="linux-kernel+bounces-94930-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 sy.mirrors.kernel.org (Postfix) with ESMTPS id 9BCC5B21B7A for ; Thu, 7 Mar 2024 03:38:22 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 49C891CAA0; Thu, 7 Mar 2024 03:37:15 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="OGPJ/FCD" 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 608101C6A5; Thu, 7 Mar 2024 03:37:14 +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=1709782634; cv=none; b=Q7fJ4zoLvZBRqCMow+bFnSiDfAgmlVUc+fRVDnpZo1uFkr8H8Ezo9j0eqKhjW5Qz6lR5OIcQzKRxnt/JDum+71b4Tmvp/8UMBOMCjeswilmUVkjbvS/MIfa2jWqnHjDcCVJH9RkpIJou1PgH2qqu/JKmeF8yzBFOqe0Ppn6O9hw= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1709782634; c=relaxed/simple; bh=JpkBX6MHPZqlwpjx+IbTH/pg6POGPon5x6eqvGocBZE=; h=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=QbMFNHSx8dPhdp1aoP3DqnTy73Nobx8bj/s0sdftLQjv4CylcKZCwlG5dwKZGUR0JfRxC7AOuFxxFJozDcPm0OEjsfqbID78Mt1YkJTdT3DsvOOnjVqmjzyytwuaks8v0uK0J/y9xI8dSlv2slwDo25rTkPCO4Or8AgAucOlrUQ= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=OGPJ/FCD; arc=none smtp.client-ip=10.30.226.201 Received: by smtp.kernel.org (Postfix) with ESMTPSA id 015F7C43390; Thu, 7 Mar 2024 03:37:13 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1709782634; bh=JpkBX6MHPZqlwpjx+IbTH/pg6POGPon5x6eqvGocBZE=; h=Date:From:To:Cc:Subject:Reply-To:References:In-Reply-To:From; b=OGPJ/FCDV2u3aQ8hxjKb0kADh6zybOXV/zlsnCBTK7AwlgYDj/HDKZKgziLl7+n+q 0eBQTYd86LZyhhu2mEvV03gEuaxe/WdzxIjbZvZlHSPtGukQoZFLgXNKoO3Gog2tEJ 0dPQ2Tj2XOwoZG4frVZI3XIG5D90F6w1Zyggr9y8C80OgVsA+Dsd5RY6tRjROda1Um SMWY8hhHtUj2/X9odjCGTL5ENTkreTGweO4JACm6hWskTUtkJX5vrehCKUQYwtCj/j vClLlfKBou1t5YxNvgaH7tf/t4ALhu4po1R6G0i7hz4RoQGrBP6H9zdNjq0Nr3n08V 38KrOpoY4xcng== Received: by paulmck-ThinkPad-P17-Gen-1.home (Postfix, from userid 1000) id 98D0FCE10B8; Wed, 6 Mar 2024 19:37:13 -0800 (PST) Date: Wed, 6 Mar 2024 19:37:13 -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: <72b14322-78c1-4479-9c4e-b0e11c1f0d53@paulmck-laptop> Reply-To: paulmck@kernel.org References: <27665890-8314-4252-8622-1e019fee27e4@paulmck-laptop> <20240306130103.6da71ddf@gandalf.local.home> <20240306135504.2b3872ef@gandalf.local.home> <20240306142738.7b66a716@rorschach.local.home> <83b47424-e5e0-46de-aa63-d413a5aa6cec@paulmck-laptop> <851dc594-d2ea-4050-b7c6-e33a1cddf3a1@efficios.com> 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: <851dc594-d2ea-4050-b7c6-e33a1cddf3a1@efficios.com> On Wed, Mar 06, 2024 at 10:06:21PM -0500, Mathieu Desnoyers wrote: > On 2024-03-06 21:43, Linus Torvalds wrote: > [...] > > > > Honestly, this all makes me think that we'd be *much* better off > > showing the real "handoff" with smp_store_release() and > > smp_load_acquire(). > > We've done something similar in liburcu (userspace code) to allow > Thread Sanitizer to understand the happens-before relationships > within the RCU implementations and lock-free data structures. > > Moving to load-acquire/store-release (C11 model in our case) > allowed us to provide enough happens-before relationship for > Thread Sanitizer to understand what is happening under the > hood in liburcu and perform relevant race detection of user > code. Good point! In the kernel, though, KCSAN understands the Linux-kernel memory model, and so we don't get that sort of false positive. > 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(). It is wrong here. OK, not wrong from a functional viewpoint, but it is at best very confusing. I am applying patches to fix this. I will push out a new "dev" branch on -rcu that will make this function appear as shown below. 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? ;-) Thanx, Paul ------------------------------------------------------------------------ static bool rcu_torture_pipe_update_one(struct rcu_torture *rp) { int i; struct rcu_torture_reader_check *rtrcp = READ_ONCE(rp->rtort_chkp); if (rtrcp) { WRITE_ONCE(rp->rtort_chkp, NULL); smp_store_release(&rtrcp->rtc_ready, 1); // Pair with smp_load_acquire(). } i = rp->rtort_pipe_count; if (i > RCU_TORTURE_PIPE_LEN) i = RCU_TORTURE_PIPE_LEN; atomic_inc(&rcu_torture_wcount[i]); WRITE_ONCE(rp->rtort_pipe_count, i + 1); ASSERT_EXCLUSIVE_WRITER(rp->rtort_pipe_count); if (i + 1 >= RCU_TORTURE_PIPE_LEN) { rp->rtort_mbtest = 0; return true; } return false; }