Received: by 2002:ab2:788f:0:b0:1ee:8f2e:70ae with SMTP id b15csp323255lqi; Wed, 6 Mar 2024 19:58:59 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCXdkh9ccLD3koAplMQDXaxIevOCLu/kZuKxKsWtUWq5xo9BovaT7C7Pjyh5qDud1REf0wmauwThSbynId4oOnPJX/aUpzi2N7Tsxp17yQ== X-Google-Smtp-Source: AGHT+IFTsZJRsmjdEG90YmskN1a5M+1TEiZSNw8QgyOc3dFjukJZArpgP1QshXrI5iaNEKPeN7IJ X-Received: by 2002:a17:906:6d14:b0:a45:27bd:e1ff with SMTP id m20-20020a1709066d1400b00a4527bde1ffmr7417340ejr.6.1709783938851; Wed, 06 Mar 2024 19:58:58 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1709783938; cv=pass; d=google.com; s=arc-20160816; b=Z0vtqAzvy954g8xOVf+5zGlqI6+lbDCODLU92ntJ+27X5Yhd6oaW27ZphrsOq65SbM /PLZ0VUkkspvORjy4AFihpYh29tBy8t6wx+xX7Wwnc6x0IOcy63omOKuibt1oy4aVdJC c38dpt5AiOhQQHY10tLDK1I6Kqt4RbUd8qzXxRIxgAue/Rrz50+wdxL8mUX9p4/+1jBV gP7tQCB00LWCX5NZ9PamTNyNu6pYIQBxXculfmVyX31T6S7F+w9cL0nHZiE9YRStzcXg iULkOOQzLySOWVKipTTTGLwoWTImmWERACMR3iCs4dStLo62r7toyXb0jmcLGFjj3s8y 4Ikg== 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=aCUU9UtcfoR0+kAYZMET4LuriEaj02RfcGFQMN4cXIU=; fh=hhnQvbWcsqsMSRpX2FYJjomT9lCkvcAfZBqdBPYgNHE=; b=HOJMgDZ2Y0xGoilVhfKzjy3sRR9ZDBh2KLlW1TtBvAt2Bc7eX9/PiJF3qvJtwSFvR1 JG1QdLUvnb3jppJKOu/Zpc2mE9X7kyP/9R/A5LcV7uZ1wBMhehOihgTWz8Dh0UVm+6ll o+8KEbTYTA6sAV9Z8R7LJepYxmpW2FVEJQDq2JsHRhekSoI7MOBGFW9xEE9rLZwbSiq/ +TwQNtqjDmid5i8Ba6vhsAFGAcVXmsxFEnh1ZTardEm+7pBuE8niyxTUstpiF80iA8BW 9rsCQyFiaLQGRssTUJE+FZqj10o8qeuo6JFzG//wT2rP+L5whRP56+AjJ1sU4AsbqWo9 8mXw==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=dePuzdOv; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-kernel+bounces-94945-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) smtp.mailfrom="linux-kernel+bounces-94945-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from am.mirrors.kernel.org (am.mirrors.kernel.org. [147.75.80.249]) by mx.google.com with ESMTPS id sa19-20020a1709076d1300b00a43bf12748asi5873720ejc.537.2024.03.06.19.58.58 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 06 Mar 2024 19:58:58 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-94945-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) client-ip=147.75.80.249; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=dePuzdOv; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-kernel+bounces-94945-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) smtp.mailfrom="linux-kernel+bounces-94945-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 am.mirrors.kernel.org (Postfix) with ESMTPS id 6BF011F23792 for ; Thu, 7 Mar 2024 03:58:58 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id D82FE168AB; Thu, 7 Mar 2024 03:58:51 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="dePuzdOv" 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 05A8813FF5; Thu, 7 Mar 2024 03:58:50 +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=1709783931; cv=none; b=EVUDGin7xr649hAz9zI0B1eAUmJ5Vb9hcvsosqWlokrEUlXWJRQVcNM+GhpW6u+F78hQj8qlQ0/AZNODU6LGZlMbixwPunosNUzMqwEFR0gFd9QuNKDTmj7dFEHH1laGzuWs2eqPbi+7hNPvCsZIyRvc2+6kb7F5mmNJoVzD9qY= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1709783931; c=relaxed/simple; bh=0TJXdgdBtAlqCZeCOaVcYTRM1lqRL/9UgdNdraEbWe0=; h=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=L/5DCKQhl1As4xll70u8m7RKuCILjKS03C+4YbLFQEw/Uj2VYa+rfyFbJcgGX+3uCbrtSLZ9CQ+wnsbWmOog/XMHSUNx6dKAuwkhntwN1fDgESJylhw4hPlKz2PT1rfq2cUWCvFV0eXlaiErw3XvF9xDN8NtfWfFpiCel/HkomI= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=dePuzdOv; arc=none smtp.client-ip=10.30.226.201 Received: by smtp.kernel.org (Postfix) with ESMTPSA id 8E39AC43394; Thu, 7 Mar 2024 03:58:50 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1709783930; bh=0TJXdgdBtAlqCZeCOaVcYTRM1lqRL/9UgdNdraEbWe0=; h=Date:From:To:Cc:Subject:Reply-To:References:In-Reply-To:From; b=dePuzdOvoOg4rColoeij5qOOc2JOQppIIL4FuaKi8qvLf3WKZVDcOI4AcVEk5XHDc zZh9jcM5521nQcONIadAtSd2+kalmGdcugkhCBYQk9WISwEdfyBAgN9DoQMn56cvQ5 D9vtoY4bt3XjaDFWWOxjUHf5Ia9lxru92MXUo1DATJRf+m6bBElWy9TwhD4BaHw+hk engbq9+8uADCL3wLSv5I1ivOqMCo3Fi3EETxG6h/3rEXrUvdbtSLyJHvrtYb8nmLSX Ny26qzw+yHFxWW9oW3a28pdFivNP57gyQFLtZ95scIZNNNaYNITTY9aGcUPT2SlIKV zHCYQUzQNYf6g== Received: by paulmck-ThinkPad-P17-Gen-1.home (Postfix, from userid 1000) id 2DC5DCE10B8; Wed, 6 Mar 2024 19:58:50 -0800 (PST) Date: Wed, 6 Mar 2024 19:58:50 -0800 From: "Paul E. McKenney" To: linke li Cc: Joel Fernandes , Davidlohr Bueso , Josh Triplett , Frederic Weisbecker , Neeraj Upadhyay , Boqun Feng , Steven Rostedt , Mathieu Desnoyers , Lai Jiangshan , Zqiang , linux-kernel@vger.kernel.org, rcu@vger.kernel.org Subject: Re: [PATCH v2] rcutorture: Reuse value read using READ_ONCE instead of re-reading it Message-ID: Reply-To: paulmck@kernel.org References: 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 Wed, Mar 06, 2024 at 10:05:20AM +0800, linke li wrote: > rp->rtort_pipe_count is modified concurrently by rcu_torture_writer(). To > prevent a data race, reuse i which is read using READ_ONCE before instead > of re-reading it. > > Signed-off-by: linke li > Reviewed-by: Joel Fernandes (Google) > --- Thank you both! This topic got quite a bit of discussion [1], with the result that I took your patch, but edited your commit log. Could you please take a look below and let me know if I messed anything up? Thanx, Paul [1] https://lore.kernel.org/all/20240306103719.1d241b93@gandalf.local.home/ ------------------------------------------------------------------------ commit e3038bbf5d746fd4c72975b792abbb63fa3f3421 Author: linke li Date: Wed Mar 6 19:51:10 2024 -0800 rcutorture: Re-use value stored to ->rtort_pipe_count instead of re-reading Currently, the rcu_torture_pipe_update_one() writes the value (i + 1) to rp->rtort_pipe_count, then immediately re-reads it in order to compare it to RCU_TORTURE_PIPE_LEN. This re-read is pointless because no other update to rp->rtort_pipe_count can occur at this point. This commit therefore instead re-uses the (i + 1) value stored in the comparison instead of re-reading rp->rtort_pipe_count. Signed-off-by: linke li Reviewed-by: Joel Fernandes (Google) Signed-off-by: Paul E. McKenney Cc: Linus Torvalds diff --git a/kernel/rcu/rcutorture.c b/kernel/rcu/rcutorture.c index 0cb5452ecd945..dd7d5ba457409 100644 --- a/kernel/rcu/rcutorture.c +++ b/kernel/rcu/rcutorture.c @@ -467,7 +467,7 @@ rcu_torture_pipe_update_one(struct rcu_torture *rp) atomic_inc(&rcu_torture_wcount[i]); WRITE_ONCE(rp->rtort_pipe_count, i + 1); ASSERT_EXCLUSIVE_WRITER(rp->rtort_pipe_count); - if (rp->rtort_pipe_count >= RCU_TORTURE_PIPE_LEN) { + if (i + 1 >= RCU_TORTURE_PIPE_LEN) { rp->rtort_mbtest = 0; return true; }