rust-for-linux.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Boqun Feng <boqun.feng@gmail.com>
To: FUJITA Tomonori <fujita.tomonori@gmail.com>
Cc: ojeda@kernel.org, a.hindborg@kernel.org, aliceryhl@google.com,
	bjorn3_gh@protonmail.com, dakr@kernel.org, gary@garyguo.net,
	lossin@kernel.org, rust-for-linux@vger.kernel.org,
	tmgross@umich.edu, jens.korinth.tuta.io@kernel.org
Subject: Re: [PATCH v1 1/2] rust: Add support for calling a function exactly once
Date: Thu, 6 Nov 2025 06:46:39 -0800	[thread overview]
Message-ID: <aQy0zxs53EAnntwR@tardis.local> (raw)
In-Reply-To: <20251106.091026.1308953895982406095.fujita.tomonori@gmail.com>

On Thu, Nov 06, 2025 at 09:10:26AM +0900, FUJITA Tomonori wrote:
> On Wed, 5 Nov 2025 08:19:37 -0800
> Boqun Feng <boqun.feng@gmail.com> wrote:
> 
> >> diff --git a/rust/kernel/lib.rs b/rust/kernel/lib.rs
> >> index 3dd7bebe7888..19553eb8c188 100644
> >> --- a/rust/kernel/lib.rs
> >> +++ b/rust/kernel/lib.rs
> >> @@ -110,6 +110,7 @@
> >>  #[cfg(CONFIG_NET)]
> >>  pub mod net;
> >>  pub mod of;
> >> +pub mod once_lite;
> > 
> > Would it make more sense to put it the kernel::sync module?
> 
> I actually considered that as well. The OnceLite structure could
> probably live under the sync module.
> 
> However, the do_once_lite macro places its data in the .data..once
> section, which can be zero-cleared when a user writes to debugfs
> clear_warn_once. In that case, there is no guarantee of any atomicity,

This is not true actually, clear_warn_once_set() uses memset() to zero
the memory, which is usually implemented by kernel itself and at least
indicates per-byte atomicity, so it totally works with other atomic
accesses in the kernel memory model. Otherwise pr_*_once() will be
considered as data races.

> so it doesn't really fit the semantics expected for the sync module.
> 
> For now, OnceLite the only used by do_once_lite macro, so I didn't see
> much benefit in splitting it into separate files.

I lean towards Andreas' suggestion that we should use SetOnce() here if
possible. I was missing that before this reply, thank Andrea for bring
it up.

> Also, data placed in the .data..once section should ideally be of a
> type whose zero-cleared state is clearly valid, which makes it
> doubtful that OnceLite would be generally useful in the way that other
> synchronization primitives in sync are.
> 

Why? A lot of synchronization primitives have 0 as a valid value, no?

> From a Rust perspective, data that is shared with the C side and can
> be zero-cleared at any time might ideally require some special
> structures? However, what we actually want to achieve here is simply

I don't think special structures are required or it's already
implemented in our Atomic type.

> something like "probably print only once", which is a very simple use
> case. So I'm not sure it's worth introducing something complicated.
> 

That's my point (and probably also Andreas' point), we already has the
type `SetOnce` to do this, no need for a `OnceLite` type if not
necessary, and the fact that it can be zero'd by debugfs doesn't change
it as I explained above.

> 
> >> +impl OnceLite {
> >> +    /// Creates a new [`OnceLite`] in the incomplete state.
> >> +    #[inline(always)]
> >> +    #[allow(clippy::new_without_default)]
> >> +    pub const fn new() -> Self {
> >> +        OnceLite(Atomic::new(State::Incomplete))
> >> +    }
> >> +
> >> +    /// Calls the provided function exactly once.
> > 
> > I think a few more comments here won't hurt:
> > 
> >     /// There is no other synchronization between two `call_once()`s
> >     /// except that only one will execute `f`, in other words, callers
> >     /// should not use a failed `call_once()` as a proof that another
> >     /// `call_once()` has already finished and the effect is observable
> >     /// to this thread.
> > 
> > Thoughts?
> 
> I don't expect OnceLite to be used in cases where it matters whether
> another thread has completed call_once(), but adding the above comment
> wouldn't hurt, I think.
> 
> 
> >> +    pub fn call_once<F>(&self, f: F) -> bool
> >> +    where
> >> +        F: FnOnce(),
> >> +    {
> >> +        let old = self.0.xchg(State::Complete, Relaxed);
> > 
> > And we probably want a // ORDERING: comment here to explain why
> > `Relaxed` is used here (because of the semantics mentioned above in the
> > comment).
> 
> What kind of comment do you think would be appropriate here?
> 

If we still need this (i.e not using SetOnce), then something like:

// ORDERING: `Relaxed` is used here since no synchronization is required
// for `call_once()`.

Regards,
Boqun

  reply	other threads:[~2025-11-06 14:46 UTC|newest]

Thread overview: 51+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <nsSZZk6z9Ia7Gl5JS9LVNDRjc-9eFvtSWyLI4SSjsHNouDkDV-GmXnixMYlIpGHryPfhLr8Z2W_CkWd6D2frYQ==@protonmail.internalid>
2025-11-05  5:47 ` [PATCH v1 0/2] Add support for print exactly once FUJITA Tomonori
2025-11-05  5:47   ` [PATCH v1 1/2] rust: Add support for calling a function " FUJITA Tomonori
2025-11-05  9:21     ` Onur Özkan
2025-11-05 10:35       ` Alice Ryhl
2025-11-05 10:32     ` Alice Ryhl
2025-11-06  0:34       ` FUJITA Tomonori
2025-11-05 16:19     ` Boqun Feng
2025-11-06  0:10       ` FUJITA Tomonori
2025-11-06 14:46         ` Boqun Feng [this message]
2025-11-07  9:03           ` Alice Ryhl
2025-11-10  9:21             ` FUJITA Tomonori
2025-11-10 16:14               ` Boqun Feng
2025-11-10 16:37                 ` Miguel Ojeda
2025-11-10 16:55                   ` Boqun Feng
2025-11-11 21:42                     ` Miguel Ojeda
2025-11-11  3:09                 ` FUJITA Tomonori
2025-11-11  5:17                   ` Boqun Feng
2025-11-11  9:12                     ` Andreas Hindborg
2025-11-11 23:38                       ` FUJITA Tomonori
2025-11-12  9:04                         ` Andreas Hindborg
2025-11-11 21:43                     ` FUJITA Tomonori
2025-11-12  1:30                       ` Boqun Feng
2025-11-12  2:23                         ` Boqun Feng
2025-11-12  9:10                         ` Andreas Hindborg
2025-11-14 15:03                           ` Boqun Feng
2025-11-12 13:17                         ` FUJITA Tomonori
2025-11-05  5:47   ` [PATCH v1 2/2] rust: Add pr_*_once macros FUJITA Tomonori
2025-11-05 10:33     ` Alice Ryhl
2025-11-05 20:59   ` [PATCH v1 0/2] Add support for print exactly once Andreas Hindborg
2025-11-05 23:12     ` FUJITA Tomonori
2025-11-06 14:31       ` Boqun Feng
2025-11-10 12:16         ` Andreas Hindborg
2025-11-10 16:08           ` Boqun Feng
2025-11-11  9:02             ` Andreas Hindborg
2025-11-12  0:45               ` FUJITA Tomonori
2025-11-12  1:04                 ` Boqun Feng
2025-11-12  1:18                   ` FUJITA Tomonori
2025-11-12  1:35                     ` Boqun Feng
2025-11-13  9:55                       ` FUJITA Tomonori
2025-11-11  1:28           ` FUJITA Tomonori
2025-11-13 10:07   ` Alice Ryhl
2025-11-13 11:18     ` FUJITA Tomonori
2025-11-13 12:06       ` Alice Ryhl
2025-11-14  0:47         ` FUJITA Tomonori
2025-11-14  0:57           ` Boqun Feng
2025-11-14  1:12             ` FUJITA Tomonori
2025-11-14  1:19               ` Boqun Feng
2025-11-14  9:48                 ` Alice Ryhl
2025-11-14 13:55                   ` FUJITA Tomonori
2025-11-14 13:47                 ` FUJITA Tomonori
2025-11-13 15:20       ` Boqun Feng

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=aQy0zxs53EAnntwR@tardis.local \
    --to=boqun.feng@gmail.com \
    --cc=a.hindborg@kernel.org \
    --cc=aliceryhl@google.com \
    --cc=bjorn3_gh@protonmail.com \
    --cc=dakr@kernel.org \
    --cc=fujita.tomonori@gmail.com \
    --cc=gary@garyguo.net \
    --cc=jens.korinth.tuta.io@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;
as well as URLs for NNTP newsgroup(s).