diff options
author | Andreas Hindborg <a.hindborg@samsung.com> | 2023-09-04 13:21:40 +0200 |
---|---|---|
committer | Danilo Krummrich <dakr@redhat.com> | 2023-11-22 22:01:11 +0100 |
commit | 82e18eb791e9d30854263b3ae8be7b9864db0fcb (patch) | |
tree | 8ec8d4d79cb6ac2111ff91b2037d3308e4088dcd | |
parent | b8c8b3710e29339c735550c67615ee61bc4a22fe (diff) |
rust: add rcu abstraction
Based on https://github.com/wedsonaf/linux/commit/02541e65a7e778c0049fed86ae49302bc07abed3
-rw-r--r-- | rust/helpers.c | 15 | ||||
-rw-r--r-- | rust/kernel/sync.rs | 1 | ||||
-rw-r--r-- | rust/kernel/sync/rcu.rs | 52 |
3 files changed, 68 insertions, 0 deletions
diff --git a/rust/helpers.c b/rust/helpers.c index 92b494ad66a6..c776086017fd 100644 --- a/rust/helpers.c +++ b/rust/helpers.c @@ -29,6 +29,7 @@ #include <linux/highmem.h> #include <linux/mutex.h> #include <linux/pci.h> +#include <linux/rcupdate.h> #include <linux/refcount.h> #include <linux/sched/signal.h> #include <linux/spinlock.h> @@ -319,6 +320,20 @@ void rust_helper_memcpy_fromio(void *to, const volatile void __iomem *from, long EXPORT_SYMBOL_GPL(rust_helper_memcpy_fromio); /* end io.h */ +/* 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 arc; 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() +} |