Received: by 2002:a25:e74b:0:0:0:0:0 with SMTP id e72csp403997ybh; Sat, 18 Jul 2020 07:35:53 -0700 (PDT) X-Google-Smtp-Source: ABdhPJy74qvzQHnxfBWKu80AgdB3xsMtQEOIxcqVeni1xvUj3jZKHtmn8w6ks1fY3f0855XjrKf6 X-Received: by 2002:a17:906:b16:: with SMTP id u22mr12836056ejg.53.1595082952990; Sat, 18 Jul 2020 07:35:52 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1595082952; cv=none; d=google.com; s=arc-20160816; b=I3qYULIE4S1Ceo93/ATKniOTgL4tlEKDoR48gdlS8sScJA4ik29Kty7t6WqsC1ZSfx SE6D+QdNQXhy2AzS+AeRX9qRcu4HTxnBWTe4E4q5yMzWpD5fKyIFLiJMTZ9YrevcbxPJ sTLLjA9vXvPHqfh+WqdUzaHkdYRuwvf7Y6pYvUO1Nq82qoUW0bB9U3QOG2tGR680AsQ6 LmokCF1r3TR/pttqgftWY2Ss9oLXI1r95BjsJvtNqkCtYfOOhHxF5HZtQN01jLYYYRZW tRGzgC/vBkQsm2uHcWxu3arh+WV4MevzOmrqiDhmUNtjvCLuJ+qvI0VIAP/4WbYuib5d NwEA== 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=b0xPvLxl4W/OkUCHoLnKqIVPkFbXOheQ6JCCw/0HKMU=; b=eEFYPwUH8SXrnQG/nsA4pfYPT7F9QMds4Wa+7D3wk2rHSD1pQVDURrp68JPIOC6vRv jCA5HmQUjzyihslxfjOCIMK3nbo1PXAhujmNAJdIDvaO/d+ZLW8pIjkvMnspqbfg7/at Arcs6+LY+wPvHq1UJy0o8AZ8lh8dDvscB9EV1IjspubQxcUpIqbau4SMbQVvo93DNeCm XVHeWIlSQQ57c6nXtskeeXoOcVyS0ZRoWQBSoWkNG+LoBNAY/Ma8EFySvxGmZ44b2/Cd QctZq/lqr0RsYnMASWm6Yf5G3roUCtLstijudQ5phvU1y//wogk/rbNfQ5hYc5xpi1lq +MrQ== 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 mf27si7256474ejb.83.2020.07.18.07.35.30; Sat, 18 Jul 2020 07:35:52 -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 S1727775AbgGROfE (ORCPT + 99 others); Sat, 18 Jul 2020 10:35:04 -0400 Received: from netrider.rowland.org ([192.131.102.5]:49105 "HELO netrider.rowland.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with SMTP id S1726574AbgGROfD (ORCPT ); Sat, 18 Jul 2020 10:35:03 -0400 Received: (qmail 1180388 invoked by uid 1000); 18 Jul 2020 10:35:02 -0400 Date: Sat, 18 Jul 2020 10:35:02 -0400 From: Alan Stern To: Eric Biggers Cc: Matthew Wilcox , 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: <20200718143502.GC1179836@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> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20200718052818.GF2183@sol.localdomain> 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 Fri, Jul 17, 2020 at 10:28:18PM -0700, Eric Biggers wrote: > /** > * INIT_ONCE() - do one-time initialization > * @done: pointer to a 'bool' flag that tracks whether initialization has been > * done yet or not. Must be false by default. > * @mutex: pointer to a mutex to use to synchronize executions of @init_func > * @init_func: the one-time initialization function > * @...: additional arguments to pass to @init_func (optional) > * > * This is a more general version of DO_ONCE_BLOCKING() which supports > * non-static data by allowing the user to specify their own 'done' flag and > * mutex. > * > * Return: 0 on success (done or already done), or a negative errno value > * returned by @init_func. It might be worth pointing out explicitly that init_func can be called multiple times, if it returns an error. > */ > #define INIT_ONCE(done, mutex, init_func, ...) \ > ({ \ > int err = 0; \ > \ > if (!smp_load_acquire(done)) { \ > mutex_lock(mutex); \ > if (!*(done)) { \ > err = init_func(__VA_ARGS__); \ > if (!err) \ > smp_store_release((done), true); \ > } \ > mutex_unlock(mutex); \ > } \ > err; \ > }) If this macro is invoked in multiple places for the same object (which is not unlikely), there is a distinct risk that people will supply different mutexes or done variables for the invocations. IMO a better approach would be to have a macro which, given a variable name v, generates an actual init_once_v() function. Then code wanting to use v would call init_once_v() first, with no danger of inconsistent usage. You can fill in the details... Alan Stern