From: Danilo Krummrich <dakr@redhat.com>
To: gregkh@linuxfoundation.org, rafael@kernel.org, ojeda@kernel.org,
alex.gaynor@gmail.com, wedsonaf@gmail.com, boqun.feng@gmail.com,
gary@garyguo.net, bjorn3_gh@protonmail.com,
benno.lossin@proton.me, a.hindborg@samsung.com,
aliceryhl@google.com, tglx@linutronix.de, mingo@redhat.com,
bp@alien8.de, dave.hansen@linux.intel.com
Cc: rust-for-linux@vger.kernel.org, x86@kernel.org, lyude@redhat.com,
pstanner@redhat.com, ajanulgu@redhat.com, airlied@redhat.com,
Danilo Krummrich <dakr@redhat.com>
Subject: [PATCH 5/8] rust: add rcu abstraction
Date: Mon, 25 Mar 2024 18:49:09 +0100 [thread overview]
Message-ID: <20240325174924.95899-6-dakr@redhat.com> (raw)
In-Reply-To: <20240325174924.95899-1-dakr@redhat.com>
From: Wedson Almeida Filho <wedsonaf@gmail.com>
Add a simple abstraction to guard critical code sections with an rcu
read lock.
Co-developed-by: Andreas Hindborg <a.hindborg@samsung.com>
Signed-off-by: Andreas Hindborg <a.hindborg@samsung.com>
Signed-off-by: Wedson Almeida Filho <wedsonaf@gmail.com>
Signed-off-by: Danilo Krummrich <dakr@redhat.com>
---
rust/helpers.c | 15 ++++++++++++
rust/kernel/sync.rs | 1 +
rust/kernel/sync/rcu.rs | 52 +++++++++++++++++++++++++++++++++++++++++
3 files changed, 68 insertions(+)
create mode 100644 rust/kernel/sync/rcu.rs
diff --git a/rust/helpers.c b/rust/helpers.c
index 1e40661a33d1..3b9ef2868c7c 100644
--- a/rust/helpers.c
+++ b/rust/helpers.c
@@ -27,6 +27,7 @@
#include <linux/err.h>
#include <linux/errname.h>
#include <linux/mutex.h>
+#include <linux/rcupdate.h>
#include <linux/refcount.h>
#include <linux/sched/signal.h>
#include <linux/spinlock.h>
@@ -170,6 +171,20 @@ const char *rust_helper_dev_name(const struct device *dev)
}
EXPORT_SYMBOL_GPL(rust_helper_dev_name);
+/* rcu */
+void rust_helper_rcu_read_lock(void)
+{
+ rcu_read_lock();
+}
+EXPORT_SYMBOL_GPL(rust_helper_rcu_read_lock);
+
+void rust_helper_rcu_read_unlock(void)
+{
+ rcu_read_unlock();
+}
+EXPORT_SYMBOL_GPL(rust_helper_rcu_read_unlock);
+/* end rcu */
+
/*
* `bindgen` binds the C `size_t` type as the Rust `usize` type, so we can
* use it in contexts where Rust expects a `usize` like slice (array) indices.
diff --git a/rust/kernel/sync.rs b/rust/kernel/sync.rs
index d219ee518eff..874a45b0c168 100644
--- a/rust/kernel/sync.rs
+++ b/rust/kernel/sync.rs
@@ -11,6 +11,7 @@
mod condvar;
pub mod lock;
mod locked_by;
+pub mod rcu;
pub use arc::{Arc, ArcBorrow, UniqueArc};
pub use condvar::CondVar;
diff --git a/rust/kernel/sync/rcu.rs b/rust/kernel/sync/rcu.rs
new file mode 100644
index 000000000000..1a1c8ea49359
--- /dev/null
+++ b/rust/kernel/sync/rcu.rs
@@ -0,0 +1,52 @@
+// SPDX-License-Identifier: GPL-2.0
+
+//! RCU support.
+//!
+//! C header: [`include/linux/rcupdate.h`](../../../../include/linux/rcupdate.h)
+
+use crate::bindings;
+use core::marker::PhantomData;
+
+/// Evidence that the RCU read side lock is held on the current thread/CPU.
+///
+/// The type is explicitly not `Send` because this property is per-thread/CPU.
+///
+/// # Invariants
+///
+/// The RCU read side lock is actually held while instances of this guard exist.
+pub struct Guard {
+ _not_send: PhantomData<*mut ()>,
+}
+
+impl Guard {
+ /// Acquires the RCU read side lock and returns a guard.
+ pub fn new() -> Self {
+ // SAFETY: An FFI call with no additional requirements.
+ unsafe { bindings::rcu_read_lock() };
+ // INVARIANT: The RCU read side lock was just acquired above.
+ Self {
+ _not_send: PhantomData,
+ }
+ }
+
+ /// Explicitly releases the RCU read side lock.
+ pub fn unlock(self) {}
+}
+
+impl Default for Guard {
+ fn default() -> Self {
+ Self::new()
+ }
+}
+
+impl Drop for Guard {
+ fn drop(&mut self) {
+ // SAFETY: By the type invariants, the rcu read side is locked, so it is ok to unlock it.
+ unsafe { bindings::rcu_read_unlock() };
+ }
+}
+
+/// Acquires the RCU read side lock.
+pub fn read_lock() -> Guard {
+ Guard::new()
+}
--
2.44.0
next prev parent reply other threads:[~2024-03-25 17:49 UTC|newest]
Thread overview: 53+ messages / expand[flat|nested] mbox.gz Atom feed top
2024-03-25 17:49 [PATCH 0/8] [RFC] Rust device / driver abstractions Danilo Krummrich
2024-03-25 17:49 ` [PATCH 1/8] arch: x86: tools: increase symbol name size Danilo Krummrich
2024-03-25 17:53 ` Miguel Ojeda
2024-03-25 18:01 ` Danilo Krummrich
2024-03-25 18:18 ` Miguel Ojeda
2024-03-25 17:49 ` [PATCH 2/8] rust: device: Add a minimal RawDevice trait Danilo Krummrich
2024-03-25 18:14 ` Greg KH
2024-03-25 18:22 ` Miguel Ojeda
2024-03-26 22:38 ` Danilo Krummrich
2024-03-27 5:25 ` Greg KH
2024-03-27 11:39 ` Danilo Krummrich
2024-03-25 17:49 ` [PATCH 3/8] rust: device: Add a stub abstraction for devices Danilo Krummrich
2024-03-25 17:58 ` Boqun Feng
2024-03-27 11:36 ` Danilo Krummrich
2024-03-25 18:14 ` Greg KH
2024-03-25 18:17 ` Greg KH
2024-03-26 16:01 ` Danilo Krummrich
2024-03-26 18:03 ` Greg KH
2024-03-26 19:03 ` Boqun Feng
2024-03-26 21:01 ` Danilo Krummrich
2024-03-27 1:38 ` Wedson Almeida Filho
2024-03-27 5:22 ` Greg KH
2024-03-27 9:05 ` Philipp Stanner
2024-03-27 9:13 ` Greg KH
2024-03-27 11:35 ` Danilo Krummrich
2024-03-25 17:49 ` [PATCH 4/8] rust: add driver abstraction Danilo Krummrich
2024-03-25 18:12 ` Greg KH
2024-03-25 18:30 ` Greg KH
2024-03-25 19:36 ` David Airlie
2024-03-26 5:37 ` Greg KH
2024-03-26 6:02 ` David Airlie
2024-03-26 6:14 ` Greg KH
2024-03-26 6:34 ` David Airlie
2024-03-31 19:17 ` Fabien Parent
2024-04-02 13:51 ` Danilo Krummrich
2024-03-28 10:41 ` Viresh Kumar
2024-03-25 17:49 ` Danilo Krummrich [this message]
2024-03-25 17:49 ` [PATCH 6/8] rust: add revocable mutex Danilo Krummrich
2024-03-25 18:22 ` Greg KH
2024-03-26 18:13 ` Danilo Krummrich
2024-03-26 18:17 ` Greg KH
2024-03-26 21:32 ` Danilo Krummrich
2024-03-25 17:49 ` [PATCH 7/8] rust: add revocable objects Danilo Krummrich
2024-03-25 18:21 ` Greg KH
2024-03-26 17:07 ` Danilo Krummrich
2024-03-26 18:16 ` Greg KH
2024-03-26 21:48 ` Danilo Krummrich
2024-03-27 1:31 ` Wedson Almeida Filho
2024-03-25 17:49 ` [PATCH 8/8] rust: add device::Data Danilo Krummrich
2024-03-25 18:21 ` Greg KH
2024-03-26 16:54 ` Danilo Krummrich
2024-03-26 18:12 ` Greg KH
2024-03-26 22:24 ` Danilo Krummrich
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=20240325174924.95899-6-dakr@redhat.com \
--to=dakr@redhat.com \
--cc=a.hindborg@samsung.com \
--cc=airlied@redhat.com \
--cc=ajanulgu@redhat.com \
--cc=alex.gaynor@gmail.com \
--cc=aliceryhl@google.com \
--cc=benno.lossin@proton.me \
--cc=bjorn3_gh@protonmail.com \
--cc=boqun.feng@gmail.com \
--cc=bp@alien8.de \
--cc=dave.hansen@linux.intel.com \
--cc=gary@garyguo.net \
--cc=gregkh@linuxfoundation.org \
--cc=lyude@redhat.com \
--cc=mingo@redhat.com \
--cc=ojeda@kernel.org \
--cc=pstanner@redhat.com \
--cc=rafael@kernel.org \
--cc=rust-for-linux@vger.kernel.org \
--cc=tglx@linutronix.de \
--cc=wedsonaf@gmail.com \
--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).