From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-qt1-f173.google.com (mail-qt1-f173.google.com [209.85.160.173]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 4D7F032C95E for ; Wed, 5 Nov 2025 16:19:42 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.160.173 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1762359584; cv=none; b=VUWpe+S7joqQJ39aHqqwQ9QmQotQM3EQFy1tg/szf/2kubCtRGXGFM8IysC8vFvAbTXsxRzg+o0s6N4Yc49CvdnVbidSjaOlRbmmxEJ+T3/O/pxgm03F2lGLFRyQLMR9WP+CosYqZqlR/cHaSfkdMxN/KAnnKhSX0ts8UdQkOlY= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1762359584; c=relaxed/simple; bh=dbCDPBiNBhFIXFVa90JYIObrY4bveChuN5qtF9bWh4Q=; h=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=PpJsUKZyfou4xehQig01yxsoiL2C1UhDsmWBJd61hVHu9MqffcIDPyDBMZvBaTsPrfr9hN8/v1AxgrKv4tigg1QDYgoOjw/kyTs6pafrr6ArRt/JglHAKoZ63d8F1Z0AEp91AIvkhJIW2dPbl9W4gDw9lOZgZu8sNkb4psyUT0s= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=L6VBaEp1; arc=none smtp.client-ip=209.85.160.173 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="L6VBaEp1" Received: by mail-qt1-f173.google.com with SMTP id d75a77b69052e-4ed75832448so12748591cf.2 for ; Wed, 05 Nov 2025 08:19:41 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1762359581; x=1762964381; darn=vger.kernel.org; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:feedback-id:from:to:cc:subject:date :message-id:reply-to; bh=clQymDoQR+usvHkNl/CVhyGiibN8/xiDUK+NJRWeKvM=; b=L6VBaEp139AbYfQ16aEdkVct9Kak2S/b2Uk88NehQrNflk9VdjZB2LwuPJ5csmr++7 Ze0bd5o0h5Jj3POWq25aTaY585Rmex7mjhqV2cVbyiFItx+Ly9udMjLfu6l81ZtH2QLi giu+0JM8Ly2lGZEc4aZAJLm9pQyyLiWQYsy1UuraXKTFJDEef5U7TumIUnbwxHAtIB14 jM+LnXHkh4OoPr/rXHnEuObrb2Qa3rpArg59EPpzOeU4AtZ8aFMTuNsCVt0B2hvsbKXj yxw94nfX2izGnBLM8lzAUcWHBY+ZnoS5/c5i4Zu4bBf+7MCxg5WtY/5huY+kd8QMkOaI IaCg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1762359581; x=1762964381; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:feedback-id:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=clQymDoQR+usvHkNl/CVhyGiibN8/xiDUK+NJRWeKvM=; b=NjTJYp9AMUSF9f/AL+XobYT0AEtcgrfwNpnaCLNlAwOSPZD/JfytPzItlHpIHsZqKs a+IzI9TSV0kHuyUARfyGZn3VEQL4kY0arFhhrw6+9YmEcg1dnqzCJuzt3/Tq5XJRWK/M PXnu5ybkvWnyABbZylfm9Lc4QBdKoFTuqf6EORfnco/TFuEm9ew81pT0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ec:f49c:af11 with SMTP id d75a77b69052e-4ed725e1bc5mr55291791cf.46.1762359580922; Wed, 05 Nov 2025 08:19:40 -0800 (PST) Received: from fauth-a1-smtp.messagingengine.com (fauth-a1-smtp.messagingengine.com. [103.168.172.200]) by smtp.gmail.com with ESMTPSA id d75a77b69052e-4ed5fc126f5sm39361651cf.27.2025.11.05.08.19.40 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 05 Nov 2025 08:19:40 -0800 (PST) Received: from phl-compute-04.internal (phl-compute-04.internal [10.202.2.44]) by mailfauth.phl.internal (Postfix) with ESMTP id D9CAAF40068; Wed, 5 Nov 2025 11:19:39 -0500 (EST) Received: from phl-mailfrontend-01 ([10.202.2.162]) by phl-compute-04.internal (MEProxy); Wed, 05 Nov 2025 11:19:39 -0500 X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgeeffedrtdeggddukeegfeejucetufdoteggodetrf dotffvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdfurfetoffkrfgpnffqhgenuceu rghilhhouhhtmecufedttdenucesvcftvggtihhpihgvnhhtshculddquddttddmnecujf gurhepfffhvfevuffkfhggtggujgesthdtredttddtvdenucfhrhhomhepuehoqhhunhcu hfgvnhhguceosghoqhhunhdrfhgvnhhgsehgmhgrihhlrdgtohhmqeenucggtffrrghtth gvrhhnpeehudfgudffffetuedtvdehueevledvhfelleeivedtgeeuhfegueevieduffei vdenucevlhhushhtvghrufhiiigvpedtnecurfgrrhgrmhepmhgrihhlfhhrohhmpegsoh hquhhnodhmvghsmhhtphgruhhthhhpvghrshhonhgrlhhithihqdeiledvgeehtdeigedq udejjeekheehhedvqdgsohhquhhnrdhfvghngheppehgmhgrihhlrdgtohhmsehfihigmh gvrdhnrghmvgdpnhgspghrtghpthhtohepuddvpdhmohguvgepshhmthhpohhuthdprhgt phhtthhopehfuhhjihhtrgdrthhomhhonhhorhhisehgmhgrihhlrdgtohhmpdhrtghpth htohepohhjvggurgeskhgvrhhnvghlrdhorhhgpdhrtghpthhtoheprgdrhhhinhgusgho rhhgsehkvghrnhgvlhdrohhrghdprhgtphhtthhopegrlhhitggvrhihhhhlsehgohhogh hlvgdrtghomhdprhgtphhtthhopegsjhhorhhnfegpghhhsehprhhothhonhhmrghilhdr tghomhdprhgtphhtthhopegurghkrheskhgvrhhnvghlrdhorhhgpdhrtghpthhtohepgh grrhihsehgrghrhihguhhordhnvghtpdhrtghpthhtoheplhhoshhsihhnsehkvghrnhgv lhdrohhrghdprhgtphhtthhopehruhhsthdqfhhorhdqlhhinhhugiesvhhgvghrrdhkvg hrnhgvlhdrohhrgh X-ME-Proxy: Feedback-ID: iad51458e:Fastmail Received: by mail.messagingengine.com (Postfix) with ESMTPA; Wed, 5 Nov 2025 11:19:39 -0500 (EST) Date: Wed, 5 Nov 2025 08:19:37 -0800 From: Boqun Feng To: FUJITA Tomonori 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 Message-ID: References: <20251105054731.3194118-1-fujita.tomonori@gmail.com> <20251105054731.3194118-2-fujita.tomonori@gmail.com> Precedence: bulk X-Mailing-List: rust-for-linux@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20251105054731.3194118-2-fujita.tomonori@gmail.com> Hi Tomo, On Wed, Nov 05, 2025 at 02:47:30PM +0900, FUJITA Tomonori 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. > Thanks for the patch. > Signed-off-by: FUJITA Tomonori > --- > 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; Would it make more sense to put it the kernel::sync module? > #[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) > + > +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); > + > +#[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. 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? > + pub fn call_once(&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). Otherwise it looks good to me. Thanks! Regards, Boqun > + 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) > + }}; > +} > -- > 2.43.0 >