rust-for-linux.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Boqun Feng <boqun.feng@gmail.com>
To: rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org,
	linux-arch@vger.kernel.org, llvm@lists.linux.dev
Cc: "Miguel Ojeda" <ojeda@kernel.org>,
	"Alex Gaynor" <alex.gaynor@gmail.com>,
	"Wedson Almeida Filho" <wedsonaf@gmail.com>,
	"Boqun Feng" <boqun.feng@gmail.com>,
	"Gary Guo" <gary@garyguo.net>,
	"Björn Roy Baron" <bjorn3_gh@protonmail.com>,
	"Benno Lossin" <benno.lossin@proton.me>,
	"Andreas Hindborg" <a.hindborg@samsung.com>,
	"Alice Ryhl" <aliceryhl@google.com>,
	"Alan Stern" <stern@rowland.harvard.edu>,
	"Andrea Parri" <parri.andrea@gmail.com>,
	"Will Deacon" <will@kernel.org>,
	"Peter Zijlstra" <peterz@infradead.org>,
	"Nicholas Piggin" <npiggin@gmail.com>,
	"David Howells" <dhowells@redhat.com>,
	"Jade Alglave" <j.alglave@ucl.ac.uk>,
	"Luc Maranget" <luc.maranget@inria.fr>,
	"Paul E. McKenney" <paulmck@kernel.org>,
	"Akira Yokosawa" <akiyks@gmail.com>,
	"Daniel Lustig" <dlustig@nvidia.com>,
	"Joel Fernandes" <joel@joelfernandes.org>,
	"Nathan Chancellor" <nathan@kernel.org>,
	"Nick Desaulniers" <ndesaulniers@google.com>,
	kent.overstreet@gmail.com,
	"Greg Kroah-Hartman" <gregkh@linuxfoundation.org>,
	elver@google.com, "Mark Rutland" <mark.rutland@arm.com>,
	"Thomas Gleixner" <tglx@linutronix.de>,
	"Ingo Molnar" <mingo@redhat.com>,
	"Borislav Petkov" <bp@alien8.de>,
	"Dave Hansen" <dave.hansen@linux.intel.com>,
	x86@kernel.org, "H. Peter Anvin" <hpa@zytor.com>,
	"Catalin Marinas" <catalin.marinas@arm.com>,
	torvalds@linux-foundation.org,
	linux-arm-kernel@lists.infradead.org,
	linux-fsdevel@vger.kernel.org
Subject: [WIP 1/3] rust: Introduce atomic module
Date: Fri, 22 Mar 2024 16:38:36 -0700	[thread overview]
Message-ID: <20240322233838.868874-2-boqun.feng@gmail.com> (raw)
In-Reply-To: <20240322233838.868874-1-boqun.feng@gmail.com>

Although Rust has its own memory ordering model (in the standard C++
memory model), having two models is not wise to start with: it increases
the difficulty for correctness reasoning. Since we use Linux Kernel
Memory Model for C code in kernel, it makes sense that Rust code also
uses LKMM, therefore introduce a module to provide LKMM atomic
primitives.

Signed-off-by: Boqun Feng <boqun.feng@gmail.com>
---
 rust/kernel/sync.rs                 |  1 +
 rust/kernel/sync/atomic.rs          | 42 ++++++++++++++++++++++++++++
 rust/kernel/sync/atomic/arch.rs     |  9 ++++++
 rust/kernel/sync/atomic/arch/x86.rs | 43 +++++++++++++++++++++++++++++
 4 files changed, 95 insertions(+)
 create mode 100644 rust/kernel/sync/atomic.rs
 create mode 100644 rust/kernel/sync/atomic/arch.rs
 create mode 100644 rust/kernel/sync/atomic/arch/x86.rs

diff --git a/rust/kernel/sync.rs b/rust/kernel/sync.rs
index c983f63fd56e..dc2d26712f26 100644
--- a/rust/kernel/sync.rs
+++ b/rust/kernel/sync.rs
@@ -8,6 +8,7 @@
 use crate::types::Opaque;
 
 mod arc;
+pub mod atomic;
 mod condvar;
 pub mod lock;
 mod locked_by;
diff --git a/rust/kernel/sync/atomic.rs b/rust/kernel/sync/atomic.rs
new file mode 100644
index 000000000000..280040705fb0
--- /dev/null
+++ b/rust/kernel/sync/atomic.rs
@@ -0,0 +1,42 @@
+// SPDX-License-Identifier: GPL-2.0
+
+//! Atomic and barrier primitives.
+//!
+//! These primitives should have the same semantics as their C counterparts, for precise definitions
+//! of the semantics, please refer to tools/memory-model. Note that Linux Kernel Memory
+//! (Consistency) Model is the only model for Rust development in kernel right now, please avoid to
+//! use Rust's own atomics.
+
+use core::cell::UnsafeCell;
+
+mod arch;
+
+/// An atomic `i32`.
+pub struct AtomicI32(pub(crate) UnsafeCell<i32>);
+
+impl AtomicI32 {
+    /// Creates a new atomic value.
+    pub fn new(v: i32) -> Self {
+        Self(UnsafeCell::new(v))
+    }
+
+    /// Adds `i` to the atomic variable with RELAXED ordering.
+    ///
+    /// Returns the old value before the add.
+    ///
+    /// # Example
+    ///
+    /// ```rust
+    /// use kernel::sync::atomic::AtomicI32;
+    ///
+    /// let a = AtomicI32::new(0);
+    /// let b = a.fetch_add_relaxed(1);
+    /// let c = a.fetch_add_relaxed(2);
+    ///
+    /// assert_eq!(b, 0);
+    /// assert_eq!(c, 1);
+    /// ```
+    pub fn fetch_add_relaxed(&self, i: i32) -> i32 {
+        arch::i32_fetch_add_relaxed(&self.0, i)
+    }
+}
diff --git a/rust/kernel/sync/atomic/arch.rs b/rust/kernel/sync/atomic/arch.rs
new file mode 100644
index 000000000000..3eb5a103a69a
--- /dev/null
+++ b/rust/kernel/sync/atomic/arch.rs
@@ -0,0 +1,9 @@
+// SPDX-License-Identifier: GPL-2.0
+
+//! Architectural atomic and barrier primitives.
+
+#[cfg(CONFIG_X86)]
+pub(crate) use x86::*;
+
+#[cfg(CONFIG_X86)]
+pub(crate) mod x86;
diff --git a/rust/kernel/sync/atomic/arch/x86.rs b/rust/kernel/sync/atomic/arch/x86.rs
new file mode 100644
index 000000000000..2d715f740b22
--- /dev/null
+++ b/rust/kernel/sync/atomic/arch/x86.rs
@@ -0,0 +1,43 @@
+// SPDX-License-Identifier: GPL-2.0
+
+//! x86 implementation for atomic and barrier primitives.
+
+use core::arch::asm;
+use core::cell::UnsafeCell;
+
+/// Generates an instruction with "lock" prefix.
+#[cfg(CONFIG_SMP)]
+macro_rules! lock_instr {
+    ($i:literal) => { concat!("lock; ", $i) }
+}
+
+#[cfg(not(CONFIG_SMP))]
+macro_rules! lock_instr {
+    ($i:literal) => { $i }
+}
+
+/// Atomically exchanges and adds `i` to `*v` in a wrapping way.
+///
+/// Return the old value before the addition.
+///
+/// # Safety
+///
+/// The caller need to make sure `v` points to a valid `i32`.
+unsafe fn i32_xadd(v: *mut i32, mut i: i32) -> i32 {
+    // SAFETY: Per function safety requirement, the address of `v` is valid for "xadd".
+    unsafe {
+        asm!(
+            lock_instr!("xaddl {i:e}, ({v})"),
+            i = inout(reg) i,
+            v = in(reg) v,
+            options(att_syntax, preserves_flags),
+        );
+    }
+
+    i
+}
+
+pub(crate) fn i32_fetch_add_relaxed(v: &UnsafeCell<i32>, i: i32) -> i32 {
+    // SAFETY: `v.get()` points to a valid `i32`.
+    unsafe { i32_xadd(v.get(), i) }
+}
-- 
2.44.0


  reply	other threads:[~2024-03-22 23:39 UTC|newest]

Thread overview: 76+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-03-22 23:38 [WIP 0/3] Memory model and atomic API in Rust Boqun Feng
2024-03-22 23:38 ` Boqun Feng [this message]
2024-03-22 23:52   ` [WIP 1/3] rust: Introduce atomic module Andrew Lunn
2024-03-23  0:03     ` Boqun Feng
2024-03-23 19:13       ` Miguel Ojeda
2024-03-23 19:30         ` Boqun Feng
2024-03-23  9:58     ` Alice Ryhl
2024-03-23 14:10       ` Andrew Lunn
2024-03-23 19:09         ` Miguel Ojeda
2024-03-26  5:56         ` Trevor Gross
2024-03-22 23:38 ` [WIP 2/3] rust: atomic: Add ARM64 fetch_add_relaxed() Boqun Feng
2024-03-22 23:38 ` [WIP 3/3] rust: atomic: Add fetch_sub_release() Boqun Feng
2024-03-22 23:57 ` [WIP 0/3] Memory model and atomic API in Rust Kent Overstreet
2024-03-23  0:12   ` Linus Torvalds
2024-03-23  0:21     ` Kent Overstreet
2024-03-23  0:36       ` Linus Torvalds
2024-03-23  2:07         ` Kent Overstreet
2024-03-23  2:26           ` Boqun Feng
2024-03-23  2:33             ` Kent Overstreet
2024-03-23  2:57               ` Boqun Feng
2024-03-23  3:10                 ` Kent Overstreet
2024-03-23  3:51                   ` Boqun Feng
2024-03-23  4:16                     ` Kent Overstreet
2024-03-25 13:56         ` Philipp Stanner
2024-03-25 17:44           ` Linus Torvalds
2024-03-25 18:59             ` Kent Overstreet
2024-03-25 19:44               ` Linus Torvalds
2024-03-25 21:14                 ` Kent Overstreet
2024-03-25 21:37                   ` Boqun Feng
2024-03-25 22:09                     ` Kent Overstreet
2024-03-25 22:38                       ` Boqun Feng
2024-03-25 23:02                         ` Kent Overstreet
2024-03-25 23:41                           ` Boqun Feng
2024-03-26  0:05                 ` Dr. David Alan Gilbert
2024-03-26  0:36                   ` Kent Overstreet
2024-03-26  1:35                     ` Dr. David Alan Gilbert
2024-03-26  3:28                       ` Kent Overstreet
2024-03-26  2:51                   ` Boqun Feng
2024-03-26  3:49                   ` Linus Torvalds
2024-03-26 14:35                     ` Dr. David Alan Gilbert
2024-03-27 16:16                     ` comex
2024-03-27 18:50                       ` Kent Overstreet
2024-03-27 19:07                         ` Linus Torvalds
2024-03-27 19:41                           ` Kent Overstreet
2024-03-27 20:45                             ` Linus Torvalds
2024-03-27 21:41                               ` Kent Overstreet
2024-03-27 22:57                                 ` Linus Torvalds
2024-03-27 23:35                                   ` Kent Overstreet
2024-03-27 21:21                             ` Boqun Feng
2024-03-27 21:49                               ` Kent Overstreet
2024-03-27 22:26                                 ` Boqun Feng
2024-03-27 21:56                               ` comex
2024-03-27 22:02                                 ` comex
2024-04-05 17:13                           ` Philipp Stanner
2024-04-08 16:02             ` Matthew Wilcox
2024-04-08 16:55               ` Paul E. McKenney
2024-04-08 17:03                 ` Matthew Wilcox
2024-04-08 18:47                   ` Paul E. McKenney
2024-04-09  0:58                   ` Kent Overstreet
2024-04-09  4:47                     ` Paul E. McKenney
2024-04-08 17:01               ` Linus Torvalds
2024-04-08 18:14                 ` Al Viro
2024-04-08 20:05                   ` Linus Torvalds
2024-03-23 21:40     ` comex
2024-03-24 15:22       ` Alan Stern
2024-03-24 17:37         ` comex
2024-03-23  0:15   ` Boqun Feng
2024-03-23  0:49     ` Boqun Feng
2024-03-23  1:42       ` Kent Overstreet
2024-03-23 14:29     ` Andrew Lunn
2024-03-23 14:41       ` Boqun Feng
2024-03-23 14:55         ` Boqun Feng
2024-03-25 10:44 ` Mark Rutland
2024-03-25 20:59   ` Boqun Feng
2024-04-09 10:50     ` Peter Zijlstra
2024-04-16 18:12       ` 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=20240322233838.868874-2-boqun.feng@gmail.com \
    --to=boqun.feng@gmail.com \
    --cc=a.hindborg@samsung.com \
    --cc=akiyks@gmail.com \
    --cc=alex.gaynor@gmail.com \
    --cc=aliceryhl@google.com \
    --cc=benno.lossin@proton.me \
    --cc=bjorn3_gh@protonmail.com \
    --cc=bp@alien8.de \
    --cc=catalin.marinas@arm.com \
    --cc=dave.hansen@linux.intel.com \
    --cc=dhowells@redhat.com \
    --cc=dlustig@nvidia.com \
    --cc=elver@google.com \
    --cc=gary@garyguo.net \
    --cc=gregkh@linuxfoundation.org \
    --cc=hpa@zytor.com \
    --cc=j.alglave@ucl.ac.uk \
    --cc=joel@joelfernandes.org \
    --cc=kent.overstreet@gmail.com \
    --cc=linux-arch@vger.kernel.org \
    --cc=linux-arm-kernel@lists.infradead.org \
    --cc=linux-fsdevel@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=llvm@lists.linux.dev \
    --cc=luc.maranget@inria.fr \
    --cc=mark.rutland@arm.com \
    --cc=mingo@redhat.com \
    --cc=nathan@kernel.org \
    --cc=ndesaulniers@google.com \
    --cc=npiggin@gmail.com \
    --cc=ojeda@kernel.org \
    --cc=parri.andrea@gmail.com \
    --cc=paulmck@kernel.org \
    --cc=peterz@infradead.org \
    --cc=rust-for-linux@vger.kernel.org \
    --cc=stern@rowland.harvard.edu \
    --cc=tglx@linutronix.de \
    --cc=torvalds@linux-foundation.org \
    --cc=wedsonaf@gmail.com \
    --cc=will@kernel.org \
    --cc=x86@kernel.org \
    /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).