From: Matthew Wilcox <willy@infradead.org>
To: Eric Biggers <ebiggers@kernel.org>
Cc: linux-kernel@vger.kernel.org, linux-arch@vger.kernel.org,
"Paul E . McKenney" <paulmck@kernel.org>,
linux-fsdevel@vger.kernel.org, Akira Yokosawa <akiyks@gmail.com>,
Alan Stern <stern@rowland.harvard.edu>,
Andrea Parri <parri.andrea@gmail.com>,
Boqun Feng <boqun.feng@gmail.com>,
Daniel Lustig <dlustig@nvidia.com>,
"Darrick J . Wong" <darrick.wong@oracle.com>,
Dave Chinner <david@fromorbit.com>,
David Howells <dhowells@redhat.com>,
Jade Alglave <j.alglave@ucl.ac.uk>,
Luc Maranget <luc.maranget@inria.fr>,
Nicholas Piggin <npiggin@gmail.com>,
Peter Zijlstra <peterz@infradead.org>,
Will Deacon <will@kernel.org>
Subject: Re: [PATCH] tools/memory-model: document the "one-time init" pattern
Date: Sat, 18 Jul 2020 03:13:04 +0100 [thread overview]
Message-ID: <20200718021304.GS12769@casper.infradead.org> (raw)
In-Reply-To: <20200718013839.GD2183@sol.localdomain>
On Fri, Jul 17, 2020 at 06:38:39PM -0700, Eric Biggers wrote:
> On Fri, Jul 17, 2020 at 06:47:50PM +0100, Matthew Wilcox wrote:
> > On Thu, Jul 16, 2020 at 09:44:27PM -0700, Eric Biggers wrote:
> > > +If that doesn't apply, you'll have to implement one-time init yourself.
> > > +
> > > +The simplest implementation just uses a mutex and an 'inited' flag.
> > > +This implementation should be used where feasible:
> >
> > I think some syntactic sugar should make it feasible for normal people
> > to implement the most efficient version of this just like they use locks.
>
> Note that the cmpxchg version is not necessarily the "most efficient".
>
> If the one-time initialization is expensive, e.g. if it allocates a lot of
> memory or if takes a long time, it could be better to use the mutex version so
> that at most one task does it.
Sure, but I think those are far less common than just allocating a single
thing.
> > How about something like this ...
> >
> > once.h:
> >
> > static struct init_once_pointer {
> > void *p;
> > };
> >
> > static inline void *once_get(struct init_once_pointer *oncep)
> > { ... }
> >
> > static inline bool once_store(struct init_once_pointer *oncep, void *p)
> > { ... }
> >
> > --- foo.c ---
> >
> > struct foo *get_foo(gfp_t gfp)
> > {
> > static struct init_once_pointer my_foo;
> > struct foo *foop;
> >
> > foop = once_get(&my_foo);
> > if (foop)
> > return foop;
> >
> > foop = alloc_foo(gfp);
> > if (!once_store(&my_foo, foop)) {
> > free_foo(foop);
> > foop = once_get(&my_foo);
> > }
> >
> > return foop;
> > }
> >
> > Any kernel programmer should be able to handle that pattern. And no mutex!
>
> I don't think this version would be worthwhile. It eliminates type safety due
> to the use of 'void *', and doesn't actually save any lines of code. Nor does
> it eliminate the need to correctly implement the cmpxchg failure case, which is
> tricky (it must free the object and get the new one) and will be rarely tested.
You're missing the point. It prevents people from trying to optimise
"can I use READ_ONCE() here, or do I need to use smp_rmb()?" The type
safety is provided by the get_foo() function. I suppose somebody could
play some games with _Generic or something, but there's really no need to.
It's like using a list_head and casting to the container_of.
> It also forces all users of the struct to use this helper function to access it.
> That could be considered a good thing, but it's also bad because even with
> one-time init there's still usually some sort of ordering of "initialization"
> vs. "use". Just taking a random example I'm familiar with, we do one-time init
> of inode::i_crypt_info when we open an encrypted file, so we guarantee it's set
> for all I/O to the file, where we then simply access ->i_crypt_info directly.
> We don't want the code to read like it's initializing ->i_crypt_info in the
> middle of ->writepages(), since that would be wrong.
Right, and I wouldn't use this pattern for that. You can't get to
writepages without having opened the file, so just initialising the
pointer in open is fine.
> An improvement might be to make once_store() take the free function as a
> parameter so that it would handle the failure case for you:
>
> struct foo *get_foo(gfp_t gfp)
> {
> static struct init_once_pointer my_foo;
> struct foo *foop;
>
> foop = once_get(&my_foo);
> if (!foop) {
> foop = alloc_foo(gfp);
> if (foop)
> once_store(&my_foo, foop, free_foo);
Need to mark once_store as __must_check to avoid the bug you have here:
foop = once_store(&my_foo, foop, free_foo);
Maybe we could use a macro for once_store so we could write:
void *once_get(struct init_pointer_once *);
int once_store(struct init_pointer_once *, void *);
#define once_alloc(s, o_alloc, o_free) ({ \
void *__p = o_alloc; \
if (__p) { \
if (!once_store(s, __p)) { \
o_free(__p); \
__p = once_get(s); \
} \
} \
__p; \
})
---
struct foo *alloc_foo(gfp_t);
void free_foo(struct foo *);
struct foo *get_foo(gfp_t gfp)
{
static struct init_pointer_once my_foo;
struct foo *foop;
foop = once_get(&my_foo);
if (!foop)
foop = once_alloc(&my_foo, alloc_foo(gfp), free_foo);
return foop;
}
That's pretty hard to misuse (I compile-tested it, and it works).
next prev parent reply other threads:[~2020-07-18 2:13 UTC|newest]
Thread overview: 57+ messages / expand[flat|nested] mbox.gz Atom feed top
2020-07-17 4:44 [PATCH] tools/memory-model: document the "one-time init" pattern Eric Biggers
2020-07-17 5:49 ` Sedat Dilek
2020-07-17 5:49 ` Sedat Dilek
2020-07-17 12:35 ` Matthew Wilcox
2020-07-17 14:26 ` Alan Stern
2020-07-17 14:26 ` Alan Stern
2020-07-17 17:47 ` Matthew Wilcox
2020-07-17 17:47 ` Matthew Wilcox
2020-07-17 17:51 ` Alan Stern
2020-07-18 1:02 ` Eric Biggers
2020-07-27 12:51 ` Matthew Wilcox
2020-07-17 21:05 ` Darrick J. Wong
2020-07-18 0:44 ` Darrick J. Wong
2020-07-18 1:38 ` Eric Biggers
2020-07-18 2:13 ` Matthew Wilcox [this message]
2020-07-18 2:13 ` Matthew Wilcox
2020-07-18 5:28 ` Eric Biggers
2020-07-18 14:35 ` Alan Stern
2020-07-20 2:07 ` Dave Chinner
2020-07-20 9:00 ` Peter Zijlstra
2020-07-27 15:17 ` Alan Stern
2020-07-27 15:17 ` Alan Stern
2020-07-27 15:28 ` Matthew Wilcox
2020-07-27 15:28 ` Matthew Wilcox
2020-07-27 16:01 ` Paul E. McKenney
2020-07-27 16:01 ` Paul E. McKenney
2020-07-27 16:31 ` Alan Stern
2020-07-27 16:59 ` Matthew Wilcox
2020-07-27 19:13 ` Alan Stern
2020-07-27 19:13 ` Alan Stern
2020-07-17 20:53 ` Darrick J. Wong
2020-07-18 0:58 ` Eric Biggers
2020-07-18 1:25 ` Alan Stern
2020-07-18 1:25 ` Alan Stern
2020-07-18 1:40 ` Matthew Wilcox
2020-07-18 2:00 ` Dave Chinner
2020-07-18 14:21 ` Alan Stern
2020-07-18 2:00 ` Eric Biggers
2020-07-18 2:00 ` Eric Biggers
2020-07-18 1:42 ` Dave Chinner
2020-07-18 1:42 ` Dave Chinner
2020-07-18 14:08 ` Alan Stern
2020-07-18 14:08 ` Alan Stern
2020-07-20 1:33 ` Dave Chinner
2020-07-20 14:52 ` Alan Stern
2020-07-20 14:52 ` Alan Stern
2020-07-20 15:37 ` Darrick J. Wong
2020-07-20 15:37 ` Darrick J. Wong
2020-07-20 15:39 ` Matthew Wilcox
2020-07-20 15:39 ` Matthew Wilcox
2020-07-20 16:04 ` Paul E. McKenney
2020-07-20 16:04 ` Paul E. McKenney
2020-07-20 16:48 ` peterz
2020-07-20 16:48 ` peterz
2020-07-20 22:06 ` Paul E. McKenney
2020-07-20 16:12 ` Alan Stern
2020-07-20 16:12 ` Alan Stern
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20200718021304.GS12769@casper.infradead.org \
--to=willy@infradead.org \
--cc=akiyks@gmail.com \
--cc=boqun.feng@gmail.com \
--cc=darrick.wong@oracle.com \
--cc=david@fromorbit.com \
--cc=dhowells@redhat.com \
--cc=dlustig@nvidia.com \
--cc=ebiggers@kernel.org \
--cc=j.alglave@ucl.ac.uk \
--cc=linux-arch@vger.kernel.org \
--cc=linux-fsdevel@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=luc.maranget@inria.fr \
--cc=npiggin@gmail.com \
--cc=parri.andrea@gmail.com \
--cc=paulmck@kernel.org \
--cc=peterz@infradead.org \
--cc=stern@rowland.harvard.edu \
--cc=will@kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).