All of lore.kernel.org
 help / color / mirror / Atom feed
From: "Onur Özkan" <work@onurozkan.dev>
To: FUJITA Tomonori <fujita.tomonori@gmail.com>
Cc: ojeda@kernel.org, a.hindborg@kernel.org, aliceryhl@google.com,
	bjorn3_gh@protonmail.com, boqun.feng@gmail.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: Wed, 5 Nov 2025 12:21:25 +0300	[thread overview]
Message-ID: <20251105122125.5b84fa83@nimda> (raw)
In-Reply-To: <20251105054731.3194118-2-fujita.tomonori@gmail.com>

On Wed,  5 Nov 2025 14:47:30 +0900
FUJITA Tomonori <fujita.tomonori@gmail.com> wrote:

> Add the Rust equivalent of the kernel's `DO_ONCE_LITE` macro. 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 maintainability.
> 
> Signed-off-by: FUJITA Tomonori <fujita.tomonori@gmail.com>
> ---
>  rust/kernel/lib.rs       |  1 +
>  rust/kernel/once_lite.rs | 71
> ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 72
> insertions(+) create mode 100644 rust/kernel/once_lite.rs
> 
> 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;
>  #[cfg(CONFIG_PM_OPP)]
>  pub mod opp;
>  pub mod page;
> diff --git a/rust/kernel/once_lite.rs b/rust/kernel/once_lite.rs
> new file mode 100644
> index 000000000000..44ad5dbdc67e
> --- /dev/null
> +++ b/rust/kernel/once_lite.rs
> @@ -0,0 +1,71 @@
> +// SPDX-License-Identifier: GPL-2.0
> +
> +//! Support for calling a function exactly once.
> +//!
> +//! C header:
> [`include/linux/once_lite.h`](srctree/include/linux/once_lite.h) +

This seems like a pure Rust implementation. What exactly do we need
from the "once_lite.h" in this module?

-Onur

> +use crate::sync::atomic::{Atomic, AtomicType, Relaxed};
> +
> +/// A lightweight `call_once` primitive.
> +///
> +/// This structure provides the Rust equivalent of the kernel's
> `DO_ONCE_LITE` macro. +/// 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 maintainability.
> +pub struct OnceLite(Atomic<State>); +
> +#[derive(Clone, Copy, PartialEq, Eq)]
> +#[repr(i32)]
> +enum State {
> +    Incomplete = 0,
> +    Complete = 1,
> +}
> +
> +// SAFETY: `State` and `i32` has the same size and alignment, and
> it's round-trip +// transmutable to `i32`.
> +unsafe impl AtomicType for State {
> +    type Repr = i32;
> +}
> +
> +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.
> +    pub fn call_once<F>(&self, f: F) -> bool
> +    where
> +        F: FnOnce(),
> +    {
> +        let old = self.0.xchg(State::Complete, Relaxed);
> +        if old == State::Complete {
> +            return false;
> +        }
> +
> +        f();
> +        true
> +    }
> +}
> +
> +/// Run the given function exactly once.
> +///
> +/// This is equivalent to the kernel's `DO_ONCE_LITE` macro.
> +///
> +/// # Examples
> +///
> +/// ```
> +/// kernel::do_once_lite!(|| {
> +///     kernel::pr_info!("This will be printed only once\n");
> +/// });
> +/// ```
> +#[macro_export]
> +macro_rules! do_once_lite {
> +    ($e:expr) => {{
> +        #[link_section = ".data..once"]
> +        static ONCE: $crate::once_lite::OnceLite =
> $crate::once_lite::OnceLite::new();
> +        ONCE.call_once($e)
> +    }};
> +}


  reply	other threads:[~2025-11-05  9:29 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 [this message]
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
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=20251105122125.5b84fa83@nimda \
    --to=work@onurozkan.dev \
    --cc=a.hindborg@kernel.org \
    --cc=aliceryhl@google.com \
    --cc=bjorn3_gh@protonmail.com \
    --cc=boqun.feng@gmail.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 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.