Received: by 2002:ab2:788f:0:b0:1ee:8f2e:70ae with SMTP id b15csp550185lqi; Thu, 7 Mar 2024 05:20:20 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCX0MDi33roz//YHOd39hNh6BdFwdHkMD8TCb0FEhzKYpQkj5jRfRuWtwM/PZH/0vY7s4Zrvw86pyqtt1R8m8PIuEXwxQAFwoJ0OsFnXug== X-Google-Smtp-Source: AGHT+IGJCX7B9F2g6uMBGJ9EJcveuT85/aaOpUHnh25iSKFEhjetb1XHzrGwhBSE6DOEMwMiJcYO X-Received: by 2002:a17:902:64cf:b0:1dd:61b4:ef89 with SMTP id y15-20020a17090264cf00b001dd61b4ef89mr280357pli.41.1709817620493; Thu, 07 Mar 2024 05:20:20 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1709817620; cv=pass; d=google.com; s=arc-20160816; b=BWhVDrBQ8ua12yutZ+6vYIDlWl2Gzlb/USqig+97z4MeCmuHMdZypma4vNwk5cbAhH xcw3BJoaHxN4Gv2J/os5XJ8mQp+FTKQ9PWZuZVlGS4jeDMW4Xrr+fhh93rL1/BTurAQ0 MtbzXOEOiCOOSnZVzm9LU6fEIxjaNcfmUk7mAqziV4CMVXAaF0CQAaCwstMpEmsI6rvj XFdgiwdmYk1MoZtohfiYHE5PGAL7gEZ9Wba7hs8DMLNSrnrfIgSc0p33IQuVChYMKQct 01HHeNsG/KkBFkuvIuJatHRnFhUD4udfOeyDIN5wSgD8QYgptVDNy9ItNRJD7xxKF2RC qQUg== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:references:in-reply-to:message-id :subject:cc:to:from:date; bh=owLvSJPwVI7PU5T7TdT3m0tJ7rnEafVO0i/Gxtu4ziE=; fh=4dU+JhieUHr/RI05mehfCLNhp2+1Zi8phVAoV5qYL6U=; b=dvaeOVXfuL+0YWFqrey9bokUs4+HEgPDZ3rrfBFhVhDq1BHwN2UiydOHJJTsvjwScR 1K6r6+nx/RIjuG2fDr5a/LEVw1UT8mEjxOR5cyo+BIwQvnuAcFzcwoplEE+dmR6uRP0g NBnhWdzkTKaBFuvMQaD1jlsow67hVScpj3/SZJlMu/pa6LljMjkm6Ky7u9TogvrU0v6M ZNzkJsif5KnT3TmPhF9MMH1SlXfm3YPiDhIPFU107gvlC3rWklsubqfp9LI9Tc+chYGY VLp8l8JNl8EAlpVk4MpSOts02WhcNminmh1ONOxPD0ZTAaJ9zlH+bT+i1fdabp2Kp/Uj N0Cg==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; arc=pass (i=1); spf=pass (google.com: domain of linux-kernel+bounces-95583-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:40f1:3f00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-95583-linux.lists.archive=gmail.com@vger.kernel.org" Return-Path: Received: from sy.mirrors.kernel.org (sy.mirrors.kernel.org. [2604:1380:40f1:3f00::1]) by mx.google.com with ESMTPS id o10-20020a170902778a00b001dc8a50e480si13165039pll.549.2024.03.07.05.20.19 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 07 Mar 2024 05:20:20 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-95583-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:40f1:3f00::1 as permitted sender) client-ip=2604:1380:40f1:3f00::1; Authentication-Results: mx.google.com; arc=pass (i=1); spf=pass (google.com: domain of linux-kernel+bounces-95583-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:40f1:3f00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-95583-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 sy.mirrors.kernel.org (Postfix) with ESMTPS id E8187B2294F for ; Thu, 7 Mar 2024 13:19:14 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 02C8A12C552; Thu, 7 Mar 2024 13:19:08 +0000 (UTC) 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 7548184FD5; Thu, 7 Mar 2024 13:19:07 +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=1709817547; cv=none; b=LVQdwHCnTO/nkTBjS65Z4Mh9QwF5PtaTZXmDZ+4wQTx+p7VMpwQOZv84qur0iHrGgFZW5WoUZ0jlREzLQMCHi8KRekFdwuBJE1FM7AtAAVY1oOSKQNnHyfu0bcIgDjNI/7rJ9RQTTh4PzEH1KSE0cOQX0e9Ie/BL3bmPuLcS/Yg= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1709817547; c=relaxed/simple; bh=32FNlvr62WRPZ4KinH/QcBoyS2NG2VDQhLVgPhvPXGo=; h=Date:From:To:Cc:Subject:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=Xgsl7mHwcatbNk+BbDQe8xpIXZDh4KPy+cY2cFfG7neJf7x4Yj2hAD9EVbQ6IgJ2PYBxnOaJDFlHaDkc/da9pZ4kDz69vvvLIxqXkd+PMOJuMxxku8Ul/ONstzj40ywEpHSB53cqG8g8M4LYmkKFLtwoEO79hK8qXsosl85iWp8= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 Received: by smtp.kernel.org (Postfix) with ESMTPSA id 7E33FC433C7; Thu, 7 Mar 2024 13:19:05 +0000 (UTC) Date: Thu, 7 Mar 2024 08:20:59 -0500 From: Steven Rostedt To: Linus Torvalds Cc: "Paul E. McKenney" , 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, mathieu.desnoyers@efficios.com, 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: <20240307082059.1dc2582d@gandalf.local.home> In-Reply-To: References: <20240306103719.1d241b93@gandalf.local.home> <27665890-8314-4252-8622-1e019fee27e4@paulmck-laptop> <20240306130103.6da71ddf@gandalf.local.home> <20240306135504.2b3872ef@gandalf.local.home> <20240306144713.2e1709ad@gandalf.local.home> X-Mailer: Claws Mail 3.19.1 (GTK+ 2.24.33; x86_64-pc-linux-gnu) 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-Transfer-Encoding: 7bit On Wed, 6 Mar 2024 12:06:00 -0800 Linus Torvalds wrote: > On Wed, 6 Mar 2024 at 11:45, Steven Rostedt wrote: > > > > Here's the back story. I received the following patch: > > > > https://lore.kernel.org/all/tencent_BA1473492BC618B473864561EA3AB1418908@qq.com/ > > > > I didn't like it. My reply was: > > > > > - rbwork->wait_index++; > > > + WRITE_ONCE(rbwork->wait_index, READ_ONCE(rbwork->wait_index) + 1); > > > > I mean the above is really ugly. If this is the new thing to do, we need > > better macros. > > > > If anything, just convert it to an atomic_t. > > The right thing is definitely to convert it to an atomic_t. Agreed. > > The memory barriers can probably also be turned into atomic ordering, > although we don't always have all the variates. > > But for example, that > > /* Make sure to see the new wait index */ > smp_rmb(); > if (wait_index != work->wait_index) > break; > > looks odd, and should probably do an "atomic_read_acquire()" instead > of a rmb and a (non-atomic and non-READ_ONCE thing). > > The first READ_ONCE() should probably also be that atomic_read_acquire() op. > > On the writing side, my gut feel is that the > > rbwork->wait_index++; > /* make sure the waiters see the new index */ > smp_wmb(); > > should be an "atomic_inc_release(&rbwork->wait_index);" but we don't > actually have that operation. We only have the "release" versions for > things that return a value. > > So it would probably need to be either > > atomic_inc(&rbwork->wait_index); > /* make sure the waiters see the new index */ > smp_wmb(); > > or > > atomic_inc_return_release(&rbwork->wait_index); > > or we'd need to add the "basic atomics with ordering semantics" (which > we aren't going to do unless we end up with a lot more people who want > them). > > I dunno. I didn't look all *that* closely at the code. The above might > be garbage too. Somebody who actually knows the code should think > about what ordering they actually were looking for. > > (And I note that 'wait_index' is of type 'long' in 'struct > rb_irq_work', so I guess it should be "atomic_long_t" instead - just > shows how little attention I paid on the first read-through, which > should make everybody go "I need to double-check Linus here") It doesn't need to be long. I'm not even sure why I made it long. Probably for natural alignment. The code isn't that complex. You can have a list of waiters waiting for the ring buffer to fill to various watermarks (in most cases, it's just one waiter). When a write happens, it looks to see if the smallest watermark is hit, if so, calls irqwork to wakeup all the waiters. The waiters will wake up, check to see if a signal is pending or if the ring buffer has hit the watermark the waiter was waiting for and exit the wait loop. What the wait_index does, is just a way to force all waiters out of the wait loop regardless of the watermark the waiter is waiting for. Before a waiter goes into the wait loop, it saves the current wait_index. The waker will increment the wait_index and then call the same irq_work to wake up all the waiters. After the wakeup happens, the waiter will test if the new wait_index matches what it was before it entered the loop, and if it is different, it falls out of the loop. Then the caller of the ring_buffer_wait() can re-evaluate if it needs to enter the wait again. The wait_index loop exit was needed for when the file descriptor of a file that uses a ring buffer closes and it needs to wake up all the readers of that file descriptor to notify their tasks that the file closed. So we can switch the: rbwork->wait_index++; smp_wmb(); into just a: (void)atomic_inc_return_release(&rbwork->wait_index); and the: smp_rmb() if (wait_index != work->wait_index) into: if (wait_index != atomic_read_acquire(&rb->wait_index)) I'll write up a patch. Hmm, I have the same wait_index logic at the higher level doing basically the same thing (at the close of the file). I'll switch that over too. -- Steve