Received: by 2002:a25:e74b:0:0:0:0:0 with SMTP id e72csp66828ybh; Fri, 17 Jul 2020 19:00:58 -0700 (PDT) X-Google-Smtp-Source: ABdhPJw/rhKxyKsvqXhz33IZdeF5RdcH52tbRfpYrpHR78JjGC52gbusIwshbTBpONv7TIknRTvX X-Received: by 2002:a17:906:8688:: with SMTP id g8mr10981239ejx.505.1595037658620; Fri, 17 Jul 2020 19:00:58 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1595037658; cv=none; d=google.com; s=arc-20160816; b=z87MJk5J6hRhRqVDFgXIHuyvQ0mNL+6wQZP66uCKafJsrdC/ZHJWP0Nw/ftkO+96A3 LEG7F8ARRFHKCzSa1XNKxZvhWcaAvERRYT8KBnIEUpeoovmLBimWTmHd3CnfEawAh8QH g9X0/5zUadhAGPDwbYAEMkzVmPoDxGNeySvXMsXZeFnetactQLEkonvmIOYgu24iUqPk 5THe51Jz5Ica/zOOKFJgae2ppMcW/gz4xLJ6fAgdvzrN1crzp/8iwprhx6LVDSfrM7A7 +VLuHeB7yF37tRPCd5Q6nl3q40jGE4+fsOdpGRkQwMYA1QRigLLppish88MOxRNp9YJ/ dhJw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:in-reply-to:content-disposition :mime-version:references:message-id:subject:cc:to:from:date :dkim-signature; bh=0QI1eZj3vboscCDtCsxPOCLpcrjvbXTnMYS0oe5tjsg=; b=wGoyimHJijlTpJkq+4IvFHQWfUkc/4oLg1c2lKG5Wxbva+8pV+TZfDib3ok91nWaWD O5MQhERTgikpm15H6bD+u0Ohfk30DnJybp0d1mCuRiO0vnEUbLVOoI2InFekMosjFnes /kFOeom8o65mVmQswtSNxv87T+U33As01wOU/H9IhBaxFQOLkN1bmydBxLXmw4QN7GGa XF8PDCyRQGFJrYq5WXU+ZPKq7pXJoi8s+odp8fclyB8u4TScKBNjryraBvKzdXMlLw6c YMMJauBhhMoftL1Oz8HJtpLrQHp3Qjv/iUqRStesX1smB96hUB+cCee5qQCA0Lz7m9yk DWeA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b="Nw/u3nWY"; 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; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id qc14si6762770ejb.611.2020.07.17.19.00.36; Fri, 17 Jul 2020 19:00:58 -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; dkim=pass header.i=@kernel.org header.s=default header.b="Nw/u3nWY"; 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; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728238AbgGRCAN (ORCPT + 99 others); Fri, 17 Jul 2020 22:00:13 -0400 Received: from mail.kernel.org ([198.145.29.99]:53624 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726665AbgGRCAM (ORCPT ); Fri, 17 Jul 2020 22:00:12 -0400 Received: from sol.localdomain (c-107-3-166-239.hsd1.ca.comcast.net [107.3.166.239]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id 6E9632070E; Sat, 18 Jul 2020 02:00:10 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1595037611; bh=I2Mq1wDqXWIAJrmNEz3XRIN/ncXHwMctELd7o7JBcUM=; h=Date:From:To:Cc:Subject:References:In-Reply-To:From; b=Nw/u3nWYES+vEAJm5OT9r+QAcWU7zPoxYs0q9VeXAdOAQ2klSRmYa7ociYSbAB78P cU1Qx1y891hOdSbQNksLUzjOyxtbR9S4GEeSluWauAbeDCPLboMaATjDdE2D+mcTBU UHM0jFrG/Xmj2F7uzPQKdTRgt87o2C3U57GaeMVg= Date: Fri, 17 Jul 2020 19:00:09 -0700 From: Eric Biggers To: Alan Stern Cc: "Darrick J. Wong" , linux-kernel@vger.kernel.org, linux-arch@vger.kernel.org, "Paul E . McKenney" , linux-fsdevel@vger.kernel.org, Akira Yokosawa , Andrea Parri , Boqun Feng , Daniel Lustig , Dave Chinner , David Howells , Jade Alglave , Luc Maranget , Nicholas Piggin , Peter Zijlstra , Will Deacon Subject: Re: [PATCH] tools/memory-model: document the "one-time init" pattern Message-ID: <20200718020009.GE2183@sol.localdomain> References: <20200717044427.68747-1-ebiggers@kernel.org> <20200717205340.GR7625@magnolia> <20200718005857.GB2183@sol.localdomain> <20200718012555.GA1168834@rowland.harvard.edu> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20200718012555.GA1168834@rowland.harvard.edu> Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Fri, Jul 17, 2020 at 09:25:55PM -0400, Alan Stern wrote: > On Fri, Jul 17, 2020 at 05:58:57PM -0700, Eric Biggers wrote: > > On Fri, Jul 17, 2020 at 01:53:40PM -0700, Darrick J. Wong wrote: > > > > +There are also cases in which the smp_load_acquire() can be replaced by > > > > +the more lightweight READ_ONCE(). (smp_store_release() is still > > > > +required.) Specifically, if all initialized memory is transitively > > > > +reachable from the pointer itself, then there is no control dependency > > > > > > I don't quite understand what "transitively reachable from the pointer > > > itself" means? Does that describe the situation where all the objects > > > reachable through the object that the global struct foo pointer points > > > at are /only/ reachable via that global pointer? > > > > > > > The intent is that "transitively reachable" means that all initialized memory > > can be reached by dereferencing the pointer in some way, e.g. p->a->b[5]->c. > > > > It could also be the case that allocating the object initializes some global or > > static data, which isn't reachable in that way. Access to that data would then > > be a control dependency, which a data dependency barrier wouldn't work for. > > > > It's possible I misunderstood something. (Note the next paragraph does say that > > using READ_ONCE() is discouraged, exactly for this reason -- it can be hard to > > tell whether it's correct.) Suggestions of what to write here are appreciated. > > Perhaps something like this: > > Specifically, if the only way to reach the initialized memory > involves dereferencing the pointer itself then READ_ONCE() is > sufficient. This is because there will be an address dependency > between reading the pointer and accessing the memory, which will > ensure proper ordering. But if some of the initialized memory > is reachable some other way (for example, if it is global or > static data) then there need not be an address dependency, > merely a control dependency (checking whether the pointer is > non-NULL). Control dependencies do not always ensure ordering > -- certainly not for reads, and depending on the compiler, > possibly not for some writes -- and therefore a load-acquire is > necessary. > > Perhaps this is more wordy than you want, but it does get the important > ideas across. > How about: There are also cases in which the smp_load_acquire() can be replaced by the more lightweight READ_ONCE(). (smp_store_release() is still required.) Specifically, if the only way to reach the initialized memory involves dereferencing the pointer itself, then the data dependency barrier provided by READ_ONCE() is sufficient. However, if some of the initialized memory is reachable some other way (for example, if it is global or static data) then there need not be an address dependency, merely a control dependency (checking whether the pointer is non-NULL). READ_ONCE() is *not* sufficient in that case. The optimization of replacing smp_load_acquire() with READ_ONCE() is discouraged for nontrivial data structures, since it can be difficult to determine if it is correct. In particular, for complex data structures the correctness of the READ_ONCE() optimization may depend on internal implementation details of other kernel subsystems.