Received: by 2002:a05:6a10:1287:0:0:0:0 with SMTP id d7csp1561208pxv; Fri, 23 Jul 2021 11:14:15 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxEk4KbUAmJs/35QBpFOOwecfYY9JT+UxIqqL1AD6846w0PUit0v1VDF8FWaeV7+93KTIml X-Received: by 2002:a05:6402:605:: with SMTP id n5mr7063920edv.89.1627064045099; Fri, 23 Jul 2021 11:14:05 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1627064045; cv=none; d=google.com; s=arc-20160816; b=MXOMOuEjVcYapAOEI9OCsxdca3ykjLyG1O0HwmKPcAFyPS7mJlEaeiMyZGVzDl8TUl dvNoseprucaxRCoHP3fxkJBPNbchBu7vw+qGvNjQvODFJ8yr/oArqjKI3saiGAEY4n1G gXID2BWuow+GAddDiACoY+RK5PtM67UNilDNSjfhNy62VeY4ilDE1D0wjrHWDWjXAztx cxekJzYtdWfaUPRuCJ3ROynGx+S6c0SpyTdIsuoWeVTlsIpU59BNYjBk7mXuJezre4NU SeSfKghpCm4XAzpB4uIv1vDE+3kLphhz7fJ2CXHdozGPxc00oZXXmXGVHNXW760i4kZY WMBQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:user-agent:in-reply-to:content-disposition :mime-version:references:message-id:subject:cc:to:from:date; bh=NiEpKB7qMPS8vXl4B0eBF/yduibnj+rXX7KrPbmSYDs=; b=bQahpfYjEYdxTz5SNhcYXo0v2aCype24JO3pbhnsNGay1w5DNL/vyWP8TsLT04U0Sz 0XEuZ3MoPT2Dt766bacSfxT8uH5mOucNTFn8atuuMHFhJJdLR031fkHrsgUNPNKJkcRg kVMv1s/x7mQVpBJ6QNisjQ5yEfIQu0z0pe/g3bTL35yw3OsKJbXyTizh+s7BD/rOTKha 3deV3RLQlGLy5bFOIuEz0asSutujPSavQqARmZGfs+AZhjZBtJ2dspGmiahZSi7BtAqD 6d1qrV5crixBUnFCAnlu2/nfZ7CVooHU+kjLClyPD+YvzBLpnUxqMWSD1SIhQY/8vJ2c IYxA== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id b3si30965217ejb.219.2021.07.23.11.13.40; Fri, 23 Jul 2021 11:14:05 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229565AbhGWRbQ (ORCPT + 99 others); Fri, 23 Jul 2021 13:31:16 -0400 Received: from netrider.rowland.org ([192.131.102.5]:52629 "HELO netrider.rowland.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with SMTP id S229570AbhGWRbG (ORCPT ); Fri, 23 Jul 2021 13:31:06 -0400 Received: (qmail 49012 invoked by uid 1000); 23 Jul 2021 14:11:38 -0400 Date: Fri, 23 Jul 2021 14:11:38 -0400 From: Alan Stern To: "Paul E. McKenney" Cc: linux-kernel@vger.kernel.org, linux-arch@vger.kernel.org, kernel-team@fb.com, mingo@kernel.org, parri.andrea@gmail.com, will@kernel.org, peterz@infradead.org, boqun.feng@gmail.com, npiggin@gmail.com, dhowells@redhat.com, j.alglave@ucl.ac.uk, luc.maranget@inria.fr, akiyks@gmail.com, Manfred Spraul Subject: Re: [PATCH memory-model 2/4] tools/memory-model: Add example for heuristic lockless reads Message-ID: <20210723181138.GA48833@rowland.harvard.edu> References: <20210721210726.GA828672@paulmck-ThinkPad-P17-Gen-1> <20210721211003.869892-2-paulmck@kernel.org> <20210723020846.GA26397@rowland.harvard.edu> <20210723162431.GF4397@paulmck-ThinkPad-P17-Gen-1> <20210723165947.GA46562@rowland.harvard.edu> <20210723173010.GI4397@paulmck-ThinkPad-P17-Gen-1> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20210723173010.GI4397@paulmck-ThinkPad-P17-Gen-1> User-Agent: Mutt/1.10.1 (2018-07-13) Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Fri, Jul 23, 2021 at 10:30:10AM -0700, Paul E. McKenney wrote: > On Fri, Jul 23, 2021 at 12:59:47PM -0400, Alan Stern wrote: > > On Fri, Jul 23, 2021 at 09:24:31AM -0700, Paul E. McKenney wrote: > > > On Thu, Jul 22, 2021 at 10:08:46PM -0400, Alan Stern wrote: > > > > > > > + void do_something_locked(struct foo *fp) > > > > > + { > > > > > + bool gf = true; > > > > > + > > > > > + /* IMPORTANT: Heuristic plus spin_lock()! */ > > > > > + if (!data_race(global_flag)) { > > > > > + spin_lock(&fp->f_lock); > > > > > + if (!smp_load_acquire(&global_flag)) { > > > > > > > + void begin_global(void) > > > > > + { > > > > > + int i; > > > > > + > > > > > + spin_lock(&global_lock); > > > > > + WRITE_ONCE(global_flag, true); > > > > > > > > Why does this need to be WRITE_ONCE? It still races with the first read > > > > of global_flag above. > > > > > > But also with the smp_load_acquire() of global_flag, right? > > > > What I'm curious about is why, given these two races, you notate one of > > them by changing a normal write to WRITE_ONCE and you notate the other > > by changing a normal read to a data_race() read. Why not handle them > > both the same way? > > Because the code can tolerate the first read returning complete nonsense, > but needs the value from the second read to be exact at that point in > time. In other words, if the second read races with the WRITE_ONCE, it needs to get either the value before the write or the value after the write; nothing else will do because it isn't a heuristic here. Fair point. > (If the value changes immediately after being read, the fact that > ->f_lock is held prevents begin_global() from completing.) This seems like something worth explaining in the document. That "IMPORTANT" comment doesn't really get the full point across. Alan