Received: by 2002:ab2:3141:0:b0:1ed:23cc:44d1 with SMTP id i1csp1946759lqg; Mon, 4 Mar 2024 08:20:54 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCWzXI3MJPHae7/wu8VVWnoQCbNFQDAzNinIYM/Jvfdn1I3lEwqtXSvLgPH7iagdsSl7XMWeIXx1SulaaBe+VAuxPtx48JmRjDczafnQsg== X-Google-Smtp-Source: AGHT+IE3fzhedG9FqtcvMVlEKHOx3zOiIp0vf0JYuIyvBtm+VwflOHcReLtpM9qfnVzxs+rOmBdw X-Received: by 2002:a05:6a00:23d6:b0:6e6:2781:1945 with SMTP id g22-20020a056a0023d600b006e627811945mr2739048pfc.20.1709569254266; Mon, 04 Mar 2024 08:20:54 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1709569254; cv=pass; d=google.com; s=arc-20160816; b=DDK4dXMo4eK9zO0yfWXI7uKcmZJXHl2cBnizU63jeQ9GjFd/KIft+S7Q9aBCVH63Jj TqtqWh7Vb0VcW2G8CxXuHIbSlqa+27NJ/zdmeziNLSp6J0/WoQxawpLp6oDe0IugZo94 gJLE73DcS+eHXX9O94PwgvjEsfBkZ4BAYU9RyqQbWZAiApACNDTwdsvE8TNPjSpG8K+H AaM09lXsGc4abRdKmNnU8dMrZj7w6zlTfKPNTkYhov6qrwzWGeezV9Sy5YhDbJXf6lhn kY6rQrX9gdYb+a9sz5+HiHq/0CYZf8PaHFCBtWpbinrNaZN2N1yPUUb9BLd8WXFCHTCT eOWA== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:in-reply-to:from:content-language :references:cc:to:subject:user-agent:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:date:message-id:dkim-signature; bh=lwFydJvCBlry2uVIEmH1Py5ewOn9oa12Fu2r23KnJkU=; fh=CDtu05oSVb7zj6lBRQE/EhhvT3F81/UqQ6PuOJvk87c=; b=pHpqZACxs5vwUAv6EOdi3KIfwzbWolvNmZBRN4NIQaWEu3tSwPwUKcVNybdf3KUbAa ojpnf0UjyWVCfuv89bTv69HXNODuGPTGpD3PQ7/dIZkvZOAwrLgQKUKxFMsXgLXAFh76 vS/PQAc7JN8OhPXBINBFCZXzqyM7teBqAOsjKcapjw7drbWXpAKVDuIOnsYS0l0tV7br HROaxncBdxld3BFs92gpt4GVYE4t4vRD9GQ1byd1Im4AdtikhQjPHULDxYdfhVAo2m/+ IrZN1JLxB729PsEPhVzMZD3sfP02ZfmKGDUcjCJrvLXSGxfiJfEx+VmOyWz36iGZyPR/ 7FVw==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@joelfernandes.org header.s=google header.b=R7I1Gw0X; arc=pass (i=1 spf=pass spfdomain=joelfernandes.org dkim=pass dkdomain=joelfernandes.org); spf=pass (google.com: domain of linux-kernel+bounces-90926-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-90926-linux.lists.archive=gmail.com@vger.kernel.org" Return-Path: Received: from sv.mirrors.kernel.org (sv.mirrors.kernel.org. [139.178.88.99]) by mx.google.com with ESMTPS id s31-20020a63451f000000b005dc493c9496si8190461pga.507.2024.03.04.08.20.54 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 04 Mar 2024 08:20:54 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-90926-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) client-ip=139.178.88.99; Authentication-Results: mx.google.com; dkim=pass header.i=@joelfernandes.org header.s=google header.b=R7I1Gw0X; arc=pass (i=1 spf=pass spfdomain=joelfernandes.org dkim=pass dkdomain=joelfernandes.org); spf=pass (google.com: domain of linux-kernel+bounces-90926-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-90926-linux.lists.archive=gmail.com@vger.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 90C2D28254C for ; Mon, 4 Mar 2024 16:19:34 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id C01C34BA94; Mon, 4 Mar 2024 16:19:29 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=joelfernandes.org header.i=@joelfernandes.org header.b="R7I1Gw0X" Received: from mail-qv1-f44.google.com (mail-qv1-f44.google.com [209.85.219.44]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 3ECB3482DA for ; Mon, 4 Mar 2024 16:19:26 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.219.44 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1709569168; cv=none; b=iO0D6csvkd0958X3XMrwGKpFKtUy1XJC6RI6YEYuAlbRMolmTP8otH4pa1GBHOwXlQCPQniept4xy/eKzelhz5tC95lzll13guQN5+9v2W6y/xA3xb2uj3Xvx9L7RDmphaC9KTsmWYKSLbUwJfKVyXdZc3Qc2xX8XBtF3r5e3zM= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1709569168; c=relaxed/simple; bh=AyK9vkFDxSdFY7pAAcpyEeR0clBo4J9X95OLjQ6YFoE=; h=Message-ID:Date:MIME-Version:Subject:To:Cc:References:From: In-Reply-To:Content-Type; b=Tb3G+46HlIOBVkOxSrBm26cSz6sd6eHIWDZAgFmzJpYQh/LnyUTp25r1Iwr/u7X1xKNIdgSYznVFYQXuQMMZn9O+AxtfYDRgzPkNhP/3s8mv2R+8EjvMppqXyj1rYy9VIShJZrJPZCiUwVUvPusLUnEDbIKjMTLawQWPHy+B/gA= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=joelfernandes.org; spf=pass smtp.mailfrom=joelfernandes.org; dkim=pass (1024-bit key) header.d=joelfernandes.org header.i=@joelfernandes.org header.b=R7I1Gw0X; arc=none smtp.client-ip=209.85.219.44 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=joelfernandes.org Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=joelfernandes.org Received: by mail-qv1-f44.google.com with SMTP id 6a1803df08f44-68f571be9ddso42928356d6.0 for ; Mon, 04 Mar 2024 08:19:26 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=joelfernandes.org; s=google; t=1709569165; x=1710173965; darn=vger.kernel.org; h=content-transfer-encoding:in-reply-to:from:content-language :references:cc:to:subject:user-agent:mime-version:date:message-id :from:to:cc:subject:date:message-id:reply-to; bh=lwFydJvCBlry2uVIEmH1Py5ewOn9oa12Fu2r23KnJkU=; b=R7I1Gw0XYobRrdSQVqlXE+Bo0+ez2IcX79w/xf/C31KtRLQZR68t5f3UEcr5wTUpGX PzwQnA2CalRVf+8RWdYxShQmrIHg3sS1kyMEUt/hcg2DuwBjvJNQ+epAzGaf+n4bWvX8 rfrebGxSMBQOr0u+52z2IX2o3LYO1yDCcvqK8= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1709569165; x=1710173965; h=content-transfer-encoding:in-reply-to:from:content-language :references:cc:to:subject:user-agent:mime-version:date:message-id :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=lwFydJvCBlry2uVIEmH1Py5ewOn9oa12Fu2r23KnJkU=; b=EDq+CWHSivKxvfCQhttwfRlkzNHikQ8RMDHd3Qyt/xUGtzhJTNU4LLLkrE4zpG6vVJ gDzL80o+btyDT1s7CG8r5c2NoW1ayqWdSBGa1a8t400+ZES15zd0GeepBFMM5jAwA7tl xT7V4sF69rZoaMxvOaqwEOfInr7+EJISakNVKfQaz+Np9jfzPg+6BvUXP8vuKiYBYjnx WTDU6OWwr0rN0SWwt5SibOF8ud2v/XebqpzrpfUwzfEVB+9us52AYKgHLQ3hKMhx6rf0 mQKiWi+V5mtnZ9c1E7s1Y61z9nmQLWY7Fi7+c4dnFuG4dvRzSDqonzp2kExr1LacYHWK lyyw== X-Forwarded-Encrypted: i=1; AJvYcCXKONDYdRavDvX+RRniaWUmx4ZhG1KH1kF4kTFTQqbemQXd1PXG7P4iyhC119ih5n3ps/SXcTkrUOWTdpcOO+mN6PfsGCLeCQR6oHoq X-Gm-Message-State: AOJu0YxIgYsELcRFQWlK7RshaKt9CJ5ZZC4nDRVOVsHr+0TBqjj5rJqG Ovuw6OpaMNeAwfQyVmmgA29S/eIgR4wUhN7SnIqeuEtXQgAs3Ybt87CnvTBz4hg= X-Received: by 2002:a0c:eed4:0:b0:690:6eb7:c678 with SMTP id h20-20020a0ceed4000000b006906eb7c678mr4951142qvs.18.1709569165125; Mon, 04 Mar 2024 08:19:25 -0800 (PST) Received: from [10.5.0.2] ([91.196.69.189]) by smtp.gmail.com with ESMTPSA id r8-20020a0ccc08000000b0068f6e1c3582sm5142982qvk.146.2024.03.04.08.19.23 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Mon, 04 Mar 2024 08:19:24 -0800 (PST) Message-ID: Date: Mon, 4 Mar 2024 11:19:21 -0500 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: [PATCH] rcutorture: Fix rcu_torture_pipe_update_one()/rcu_torture_writer() data race and concurrency bug To: linke li Cc: Davidlohr Bueso , "Paul E. McKenney" , Josh Triplett , Frederic Weisbecker , Neeraj Upadhyay , Boqun Feng , Steven Rostedt , Mathieu Desnoyers , Lai Jiangshan , Zqiang , linux-kernel@vger.kernel.org, rcu@vger.kernel.org References: Content-Language: en-US From: Joel Fernandes In-Reply-To: Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit On 3/4/2024 5:54 AM, linke li wrote: > Some changes are done to fix a data race in commit 202489101f2e ("rcutorture: Fix rcu_torture_one_read()/rcu_torture_writer() data race") > > { > int i; > > - i = rp->rtort_pipe_count; > + i = READ_ONCE(rp->rtort_pipe_count); > if (i > RCU_TORTURE_PIPE_LEN) > i = RCU_TORTURE_PIPE_LEN; > atomic_inc(&rcu_torture_wcount[i]); > - if (++rp->rtort_pipe_count >= RCU_TORTURE_PIPE_LEN) { > + WRITE_ONCE(rp->rtort_pipe_count, i + 1); > + if (rp->rtort_pipe_count >= RCU_TORTURE_PIPE_LEN) { > rp->rtort_mbtest = 0; > return true; > } > > But ++rp->rtort_pipe_count is meant to add itself by 1, not give i+1 to > rp->rtort_pipe_count, because rp->rtort_pipe_count may write by > rcu_torture_writer() concurrently. > > Also, rp->rtort_pipe_count in the next line should be read using > READ_ONCE() because of data race. > > Signed-off-by: linke li > --- > kernel/rcu/rcutorture.c | 4 ++-- > 1 file changed, 2 insertions(+), 2 deletions(-) > > diff --git a/kernel/rcu/rcutorture.c b/kernel/rcu/rcutorture.c > index 7567ca8e743c..00059ace4fd5 100644 > --- a/kernel/rcu/rcutorture.c > +++ b/kernel/rcu/rcutorture.c > @@ -465,8 +465,8 @@ rcu_torture_pipe_update_one(struct rcu_torture *rp) > 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); > - if (rp->rtort_pipe_count >= RCU_TORTURE_PIPE_LEN) { > + WRITE_ONCE(rp->rtort_pipe_count, rp->rtort_pipe_count + 1); > + if (READ_ONCE(rp->rtort_pipe_count) >= RCU_TORTURE_PIPE_LEN) { I want to say, I am not convinced with the patch because what's wrong with writing to an old index? You win/lose the race anyway, say the CPU executed the WRITE_ONCE() a bit too early/late and another WRITE_ONCE() lost/won, regardless of whether you wrote the "incremented i" or "the increment from the latest value of pipe_count". Anyway, a slightly related/different question: Should that: WRITE_ONCE(rp->rtort_pipe_count, rp->rtort_pipe_count + 1); Be: WRITE_ONCE(rp->rtort_pipe_count, READ_ONCE(rp->rtort_pipe_count) + 1); ? thanks, - Joel