All of lore.kernel.org
 help / color / mirror / Atom feed
From: Boqun Feng <boqun.feng@gmail.com>
To: FUJITA Tomonori <fujita.tomonori@gmail.com>
Cc: a.hindborg@kernel.org, ojeda@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
Subject: Re: [PATCH v1 0/2] Add support for print exactly once
Date: Tue, 11 Nov 2025 17:04:26 -0800	[thread overview]
Message-ID: <aRPdGg01VDJucAfS@tardis.local> (raw)
In-Reply-To: <20251112.094508.554651669948369724.fujita.tomonori@gmail.com>

On Wed, Nov 12, 2025 at 09:45:08AM +0900, FUJITA Tomonori wrote:
> On Tue, 11 Nov 2025 10:02:46 +0100
> Andreas Hindborg <a.hindborg@kernel.org> wrote:
> 
> > Boqun Feng <boqun.feng@gmail.com> writes:
> > 
> >> On Mon, Nov 10, 2025 at 01:16:44PM +0100, Andreas Hindborg wrote:
> >>> Boqun Feng <boqun.feng@gmail.com> writes:
> >>> 
> >>> > On Thu, Nov 06, 2025 at 08:12:31AM +0900, FUJITA Tomonori wrote:
> >>> >> On Wed, 05 Nov 2025 21:59:06 +0100
> >>> >> Andreas Hindborg <a.hindborg@kernel.org> wrote:
> >>> >> 
> >>> >> > "FUJITA Tomonori" <fujita.tomonori@gmail.com> writes:
> >>> >> > 
> >>> >> >> This adds the Rust equivalent of the kernel's DO_ONCE_LITE and
> >>> >> >> pr_*_once macros.
> >>> >> >>
> >>> >> >> A proposal for this feature was made in the past [1], but it didn't
> >>> >> >> reach consensus on the implementation and wasn't merged. After reading
> >>> >> >> the previous discussions, I implemented it using a different approach.
> >>> >> >>
> >>> >> >> In the previous proposal, a structure equivalent to std::sync::Once
> >>> >> >> was implemented to realize the DO_ONCE_LITE macro. The approach tried
> >>> >> >> to provide Once-like semantics by using two atomic values. As pointed
> >>> >> >> out in the previous review comments, I think this approach tries to
> >>> >> >> provide more functionality than needed, making it unnecessarily
> >>> >> >> complex. Also, because data structures in the .data..once section can
> >>> >> >> be cleared at any time (via debugfs clear_warn_once), an
> >>> >> >> implementation using two atomics wouldn't work correctly.
> >>> >> >>
> >>> >> >> Therefore, I decided to drop the idea of emulating Once and took a
> >>> >> >> minimal approach to implement DO_ONCE_LITE with only one atomic
> >>> >> >> variable. While it would be possible to implement the feature entirely
> >>> >> >> as a Rust macro, the functionality that can be implemented as regular
> >>> >> >> functions has been extracted and implemented as the OnceLite struct
> >>> >> >> for better code readability.
> >>> >> >>
> >>> >> >> Of course, unlike the previous proposal, this uses LKMM atomics.
> >>> >> > 
> >>> >> > Please consider if it makes sense to base this on `SetOnce`. It is in
> >>> >> > linux-next now, but was on list here [1].
> >>> >> 
> >>> >> Data placed in the .data..once section can be zero-cleared when a user
> >>> >> writes to debugfs clear_warn_once. In that case, would such data still
> >>> >> be considered a valid SetOnce value?
> >>> >> 
> >>> >
> >>> > It's still a valid value I believe. In term of data races, Rust and C
> >>> > have no difference, so if writing to debugfs could cause issues in Rust,
> >>> > it would cause issues in C as well.
> >>> 
> >>> @Tomo you are right, `SetOnce` would not work with someone (debugfs)
> >>> asynchronously modifying the state atomic variable. It requires
> >>> exclusive access while writing the contained value.
> >>> 
> >>
> >> I mean if we were to use `SetOnce` in pr_*_once(), we should just use
> >> `SetOnce<()>`, and the problem you mentioned doesn't exist in this case.
> > 
> > At the very least it would break the type invariants and assumptions of
> > the `SetOnce` type. There a race condition between `SetOnce::as_ref` and
> > `SetOnce::populate`. I don't think we are allowed to race like this,
> > even if the type is `()`?
> > 
> > At any rate if we do this, we should update the safety invariant of
> > `SetOnce` to state that it is valid to revert the type back to the
> > initial state for zero sized types that do not have a `Drop`
> > implementation.
> 
> I agree. I think that even if the type is (), a race condition would
> still occur if the init member were reset to zero using non-atomic
> operations. For instance, the as_ref() method effectively checks

I already explained this, the debugfs writes have to be treated as
per-byte atomic, otherwise it's data race, using "non-atomic" to
reference that won't be accurate.

> whether a value has been set, so for the () type, it would return
> either Ok(()) or None.
> 
> Although SetOnce is designed to atomically set a value, allowing the
> data to be modified by non-atomic operations at arbitrary times would,
> as I mentioned in another thread, unnecessarily complicate its
> implementation for a feature that would not be useful in other use
> cases.
> 
> Could the root cause of the issue be that OnceLite, which isn't
> updated atomically, is implemented using atomic operations?
> 
> It might be better to implement it with non-atomic operations, as in
> the C version, as that would make the update behavior clearer.
> 

No, that's introducing more problems and inheriting bad things from C.

I would say we can probably implement an OnceFlag type (similiar to
OnceLite but also support cmpxchg so that SetOnce::populate() can use)
and use it to implement SetOnce. Then you can avoid violating SetOnce's
type invariants that Andreas mentioned.

Regards,
Boqun

> What do you think?
> 

  reply	other threads:[~2025-11-12  1:04 UTC|newest]

Thread overview: 52+ 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
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-15 13:37                           ` FUJITA Tomonori
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 [this message]
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=aRPdGg01VDJucAfS@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=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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.