Received: by 2002:a17:90b:8d0:0:0:0:0 with SMTP id ds16csp5059566pjb; Mon, 27 Jul 2020 11:52:36 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwm8INBM7Jo4SAQQLae796E1zgRYY/KnEYaloDTfg7cO5ThG48JYQX9nNUF594Cy5DlvbQO X-Received: by 2002:aa7:ca10:: with SMTP id y16mr15976292eds.345.1595875955840; Mon, 27 Jul 2020 11:52:35 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1595875955; cv=none; d=google.com; s=arc-20160816; b=gHLV64R4LeB0SFptgiSt7NNDbkNFFz1FtL7o8DZ/j8yxr+qm2Qjm+9OUNOAIt5/DxM Ol37vjUu4BIUVGd4pRiI2HDlq5SawiAEeZYI3dwXRhUGrHgYy9IlmyN4MSFGdmNHHl6+ Mm1g2rw0B9lRe3sl3HKTDRzjZmFMTh9rvjfwORPEaWx/CuMiBeZX3tbr5zV6WUiAHwYk 1pkermWVns+SNJs27Sqq8hxid98dZeYc5OQp2VyOVcQUO+fgrP9q/BQIYNCV/EBZIWmX YZrjhNtDYnkdZjqiY7Xsi+s+JRPA3a0l6I2ap5SJGMmAvG+LvczKCRVxDlyG2AW1bWCA 3y3A== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:user-agent:in-reply-to :content-disposition:mime-version:references:message-id:subject:cc :to:from:date; bh=c110bbrEA1uzwWFRyKUTzoJdQ4N02GDxPUYckae5WhE=; b=ZdUNjbtnyriItOESf1Ud5KJ9xbxJ+1u9TMAueSmC3t6WN8yDq/XZ/m1k97H5LwYLrx 1R46llQ+9Og0w8kNpprIiogJXoR6Ra54zL+JOR34QhwZS9mXVRyoeTUP8ZuV8uAbxS4K boJSKNVvy/haqZkCaXPt8omvvcNbRnxqKzHVfWCbgHdfrWb3d7fbJkgvwNlu7FnkXnKN eboAKoaFTWGvd1soc+wERT6j0+aOYgcI2+jqqs0a+eWZLCnqDfRI5mlhdUN0McwSitLm msKDlTLsDA45N+PSl5Pd2Qy9fNR3WVz8H6i66+VCnsaPu4ShNVsRBWk3yV0Q/WrZ2X2j LvAQ== 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 t19si6367566ejj.568.2020.07.27.11.52.13; Mon, 27 Jul 2020 11:52:35 -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 S1730962AbgG0Qbv (ORCPT + 99 others); Mon, 27 Jul 2020 12:31:51 -0400 Received: from netrider.rowland.org ([192.131.102.5]:42363 "HELO netrider.rowland.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with SMTP id S1730966AbgG0Qbu (ORCPT ); Mon, 27 Jul 2020 12:31:50 -0400 Received: (qmail 1473885 invoked by uid 1000); 27 Jul 2020 12:31:49 -0400 Date: Mon, 27 Jul 2020 12:31:49 -0400 From: Alan Stern To: Matthew Wilcox Cc: Eric Biggers , 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 , "Darrick J . Wong" , 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: <20200727163149.GD1468275@rowland.harvard.edu> References: <20200717044427.68747-1-ebiggers@kernel.org> <20200717174750.GQ12769@casper.infradead.org> <20200718013839.GD2183@sol.localdomain> <20200718021304.GS12769@casper.infradead.org> <20200718052818.GF2183@sol.localdomain> <20200727151746.GC1468275@rowland.harvard.edu> <20200727152827.GM23808@casper.infradead.org> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20200727152827.GM23808@casper.infradead.org> User-Agent: Mutt/1.10.1 (2018-07-13) Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Mon, Jul 27, 2020 at 04:28:27PM +0100, Matthew Wilcox wrote: > On Mon, Jul 27, 2020 at 11:17:46AM -0400, Alan Stern wrote: > > Given a type "T", an object x of type pointer-to-T, and a function > > "func" that takes various arguments and returns a pointer-to-T, the > > accepted API for calling func once would be to create once_func() as > > follows: > > > > T *once_func(T **ppt, args...) > > { > > static DEFINE_MUTEX(mut); > > T *p; > > > > p = smp_load_acquire(ppt); /* Mild optimization */ > > if (p) > > return p; > > > > mutex_lock(mut); > > p = smp_load_acquire(ppt); > > if (!p) { > > p = func(args...); > > if (!IS_ERR_OR_NULL(p)) > > smp_store_release(ppt, p); > > } > > mutex_unlock(mut); > > return p; > > } > > > > Users then would have to call once_func(&x, args...) and check the > > result. Different x objects would constitute different "once" > > domains. > [...] > > In fact, the only drawback I can think of is that because this relies > > on a single mutex for all the different possible x's, it might lead to > > locking conflicts (if func had to call once_func() recursively, for > > example). In most reasonable situations such conflicts would not > > arise. > > Another drawback for this approach relative to my get_foo() approach > upthread is that, because we don't have compiler support, there's no > enforcement that accesses to 'x' go through once_func(). My approach > wraps accesses in a deliberately-opaque struct so you have to write > some really ugly code to get at the raw value, and it's just easier to > call get_foo(). Something like that could be included in once_func too. It's relatively tangential to the main point I was making, which was to settle on an overall API and discuss how it should be described in recipes.txt. Alan Stern