lists.openwall.net   lists  /  announce  owl-users  owl-dev  john-users  john-dev  passwdqc-users  yescrypt  popa3d-users  /  oss-security  kernel-hardening  musl  sabotage  tlsify  passwords  /  crypt-dev  xvendor  /  Bugtraq  Full-Disclosure  linux-kernel  linux-netdev  linux-ext4  linux-hardening  linux-cve-announce  PHC 
Open Source and information security mailing list archives
 
Hash Suite: Windows password security audit tool. GUI, reports in PDF.
[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-ID: <OCj9As8--F-9@tuta.io>
Date: Wed, 27 Nov 2024 20:46:01 +0100 (CET)
From: jens.korinth@...a.io
To: Daniel Sedlak <daniel@...lak.dev>
Cc: Miguel Ojeda <ojeda@...nel.org>, Alex Gaynor <alex.gaynor@...il.com>,
	Boqun Feng <boqun.feng@...il.com>, Gary Guo <gary@...yguo.net>,
	Björn Roy Baron <bjorn3_gh@...tonmail.com>,
	Benno Lossin <benno.lossin@...ton.me>,
	Andreas Hindborg <a.hindborg@...nel.org>,
	Alice Ryhl <aliceryhl@...gle.com>, Trevor Gross <tmgross@...ch.edu>,
	Rust For Linux <rust-for-linux@...r.kernel.org>,
	FUJITA Tomonori <fujita.tomonori@...il.com>,
	Dirk Behme <dirk.behme@...il.com>,
	Linux Kernel <linux-kernel@...r.kernel.org>
Subject: Re: [PATCH v4 1/3] rust: Add `OnceLite` for executing code once

> Have you considered it to be implemented like `AtomicU32`? I think™ that
> one atomic variable is more than enough.

Just to clarify - you mean something like this? Nevermind the magic numbers,
I'd replace them, of course.

diff --git a/rust/kernel/once_lite.rs b/rust/kernel/once_lite.rs
index 723c3244fc85..0622ecbfced5 100644
--- a/rust/kernel/once_lite.rs
+++ b/rust/kernel/once_lite.rs
@@ -16,7 +16,7 @@
//!
//! Reference: <https://doc.rust-lang.org/std/sync/struct.Once.html>
 
-use core::sync::atomic::{AtomicBool, Ordering::Relaxed};
+use core::sync::atomic::{AtomicU32, Ordering::Acquire, Ordering::Relaxed};
 
/// A low-level synchronization primitive for one-time global execution.
///
@@ -44,13 +44,13 @@
/// assert_eq!(x, 42);
/// ```
///
-pub struct OnceLite(AtomicBool, AtomicBool);
+pub struct OnceLite(AtomicU32);
 
impl OnceLite {
     /// Creates a new `OnceLite` value.
     #[inline(always)]
     pub const fn new() -> Self {
-        Self(AtomicBool::new(false), AtomicBool::new(false))
+        Self(AtomicU32::new(0))
     }
 
     /// Performs an initialization routine once and only once. The given
@@ -71,10 +71,10 @@ pub const fn new() -> Self {
     /// [`DO_ONCE_LITE_IF`]: srctree/include/once_lite.h
     #[inline(always)]
     pub fn call_once<F: FnOnce()>(&self, f: F) {
-        if !self.0.load(Relaxed) && !self.0.swap(true, Relaxed) {
-            f()
+        if self.0.load(Relaxed) == 0 && self.0.compare_exchange(0, 1, Acquire, Relaxed) == Ok(0) {
+            f();
+            self.0.store(2, Relaxed);
         };
-        self.1.store(true, Relaxed);
     }
 
     /// Returns `true` if some `call_once` call has completed successfully.
@@ -98,7 +98,7 @@ pub fn call_once<F: FnOnce()>(&self, f: F) {
     /// ```
     #[inline(always)]
     pub fn is_completed(&self) -> bool {
-        self.1.load(Relaxed)
+        self.0.load(Relaxed) > 1
     }
}

> The `rust` part should be default value for rustdoc tests, can we please
> omit that?

Will do!

Jens

Powered by blists - more mailing lists

Powered by Openwall GNU/*/Linux Powered by OpenVZ