From: Alice Ryhl <aliceryhl@google.com>
To: Andreas Hindborg <a.hindborg@kernel.org>
Cc: "Miguel Ojeda" <ojeda@kernel.org>,
"Boqun Feng" <boqun.feng@gmail.com>,
"Gary Guo" <gary@garyguo.net>,
"Björn Roy Baron" <bjorn3_gh@protonmail.com>,
"Benno Lossin" <lossin@kernel.org>,
"Trevor Gross" <tmgross@umich.edu>,
"Danilo Krummrich" <dakr@kernel.org>,
rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org
Subject: Re: [PATCH] rust: sync: add lazy initialization methods to SetOnce
Date: Tue, 12 May 2026 08:52:56 +0000 [thread overview]
Message-ID: <agLqaELzbof3YHKh@google.com> (raw)
In-Reply-To: <875x4t58de.fsf@t14s.mail-host-address-is-not-set>
On Tue, May 12, 2026 at 10:39:57AM +0200, Andreas Hindborg wrote:
> Andreas Hindborg <a.hindborg@kernel.org> writes:
>
> > "Alice Ryhl" <aliceryhl@google.com> writes:
> >
> >> On Mon, Feb 16, 2026 at 11:26:11AM +0000, Alice Ryhl wrote:
> >>> On Mon, Feb 16, 2026 at 12:10:16PM +0100, Andreas Hindborg wrote:
> >>> > "Alice Ryhl" <aliceryhl@google.com> writes:
> >>> >
> >>> > > On Sun, Feb 15, 2026 at 09:27:17PM +0100, Andreas Hindborg wrote:
> >>> > >> Add methods to get a reference to the contained value or populate the
> >>> > >> SetOnce if empty. The new `as_ref_or_populate` method accepts a value
> >>> > >> directly, while `as_ref_or_populate_with` accepts a fallible closure,
> >>> > >> allowing for lazy initialization that may fail. Both methods spin-wait
> >>> > >> if another thread is concurrently initializing the container.
> >>> > >>
> >>> > >> Also add `populate_with` which takes a fallible closure and serves as
> >>> > >> the implementation basis for the other populate methods.
> >>> > >>
> >>> > >> Signed-off-by: Andreas Hindborg <a.hindborg@kernel.org>
> >>> > >
> >>> > >> + /// Get a reference to the contained object, or populate the [`SetOnce`]
> >>> > >> + /// with the value returned by `callable` and return a reference to that
> >>> > >> + /// object.
> >>> > >> + pub fn as_ref_or_populate_with(&self, callable: impl FnOnce() -> Result<T>) -> Result<&T> {
> >>> > >> + if !self.populate_with(callable)? {
> >>> > >> + while self.init.load(Acquire) != 2 {
> >>> > >> + core::hint::spin_loop();
> >>> > >> + }
> >>> > >
> >>> > > We should not be implementing our own spinlocks.
> >>> >
> >>> > That is a great proverb. I'd be happy to receive a suggestion on an
> >>> > alternate approach for this particular context.
> >>>
> >>> You can add a spinlock to SetOnce. Like I mentioned previously [1],
> >>> support for waiting will require the addition of extra fields.
> >
> > Thanks, I'll be sure to take a look again.
>
> I took a look at this again. I think the structure will be less
> efficient if we use a spin lock.
>
> Initialization is now
> - cmpxchg lock relaxed
> - store pointer
> - store lock release
>
> With a spin lock it will be
> - lock acquire
> - test pointer
> - store pointer
> - lock release
>
> All the other tests for initialization is now:
> - load lock acquire
> - load pointer
>
> They will become
> - lock acquire
> - load pointer
> - test pointer
> - lock release
>
>
> bit_spinlock does not make this any better. It just gives us 64 locks in
> a u64. It does not help us store state of the data structure
> (empty/populated).
>
> Do we want a less efficient data structure in order to gain benefits of
> lockdep and friends?
I'm not just talking about lockdep. Your current implementation is wrong
in several other ways, for example:
1. Spinlocks must disable preemption.
2. It doesn't fall back to a mutex under PREEMPT_RT.
And probably lots of other things. By using the kernel spinlock, you do
not have to worry about the long list of things spinlocks have to get
right.
> >> By the way, back then I suggested renaming it from OnceLock to SetOnce
> >> because you did not support waiting for the value to be populated, and
> >> you said you didn't need that. If you add that feature, then we should
> >> rename it back to OnceLock, or create a new type OnceLock for users who
> >> need that additional feature.
> >
> > That is fair. This is a different use case than the original one though.
> > I think we should keep this as one type for code reuse, but I am fine
> > with renaming to something that describe the usage better.
>
> Please note that even though it could be added, we do not have a `wait`
> method now. What we have are blocking initializers.
You may have open-coded `wait` inside of `as_ref_or_populate_with`, but
you still have the functionality.
Alice
> I personally have no opinion on the name. Is everyone in favor of
> renaming to OnceLock?
>
>
> Best regards,
> Andreas Hindborg
>
>
next prev parent reply other threads:[~2026-05-12 8:53 UTC|newest]
Thread overview: 6+ messages / expand[flat|nested] mbox.gz Atom feed top
[not found] <20260215-set-once-lazy-v1-1-6f5bd2efda11@kernel.org>
[not found] ` <NEEh9XY18Nk7vKZB-Vz24V-2y3HYettAoZbomSB_Ml5KPx7kzR7hsLBSfRZbDRVM7XM3rowqFh0X0IIeuesa4Q==@protonmail.internalid>
[not found] ` <DGPTYBO26YBT.3S14I9F5YT1PW@garyguo.net>
[not found] ` <87o6laatg0.fsf@t14s.mail-host-address-is-not-set>
2026-05-12 8:07 ` [PATCH] rust: sync: add lazy initialization methods to SetOnce Andreas Hindborg
2026-05-12 11:26 ` Gary Guo
[not found] ` <4EYpvxiPLRBBb2fUI9n8ZrVkve50KAvXwj7oFqoYhtpIuY_U7eVtiwnjA-ZR8n7jCwoJL67EI1ex2_DmBs5UMg==@protonmail.internalid>
[not found] ` <aZLZbN5C3wXgt3kL@google.com>
[not found] ` <875x7xdjvr.fsf@t14s.mail-host-address-is-not-set>
[not found] ` <aZL-09Wk-KrDrroy@google.com>
[not found] ` <edX-2eKbEyxxHPE4iAz6_QOunvlvPdtIDaR0dbj7h40wJ4YAgtoTnyNVEN2MzXcGwBtnqfpkuodzBLt9pnkLpg==@protonmail.internalid>
[not found] ` <aZMA_3CBEnV1Sg68@google.com>
[not found] ` <87zf58ddad.fsf@t14s.mail-host-address-is-not-set>
2026-05-12 8:39 ` Andreas Hindborg
2026-05-12 8:52 ` Alice Ryhl [this message]
2026-05-12 9:41 ` Andreas Hindborg
2026-05-12 10:42 ` Andreas Hindborg
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=agLqaELzbof3YHKh@google.com \
--to=aliceryhl@google.com \
--cc=a.hindborg@kernel.org \
--cc=bjorn3_gh@protonmail.com \
--cc=boqun.feng@gmail.com \
--cc=dakr@kernel.org \
--cc=gary@garyguo.net \
--cc=linux-kernel@vger.kernel.org \
--cc=lossin@kernel.org \
--cc=ojeda@kernel.org \
--cc=rust-for-linux@vger.kernel.org \
--cc=tmgross@umich.edu \
/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