[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-ID: <20260116-rcu-box-v1-2-38ebfbcd53f0@google.com>
Date: Fri, 16 Jan 2026 15:46:37 +0000
From: Alice Ryhl <aliceryhl@...gle.com>
To: "Paul E. McKenney" <paulmck@...nel.org>, Boqun Feng <boqun.feng@...il.com>,
"Liam R. Howlett" <Liam.Howlett@...cle.com>
Cc: Gary Guo <gary@...yguo.net>, Miguel Ojeda <ojeda@...nel.org>,
"Björn Roy Baron" <bjorn3_gh@...tonmail.com>, Benno Lossin <lossin@...nel.org>,
Andreas Hindborg <a.hindborg@...nel.org>, Trevor Gross <tmgross@...ch.edu>,
Danilo Krummrich <dakr@...nel.org>, Frederic Weisbecker <frederic@...nel.org>,
Neeraj Upadhyay <neeraj.upadhyay@...nel.org>, Joel Fernandes <joelagnelf@...dia.com>,
Josh Triplett <josh@...htriplett.org>, Uladzislau Rezki <urezki@...il.com>,
Steven Rostedt <rostedt@...dmis.org>, Mathieu Desnoyers <mathieu.desnoyers@...icios.com>,
Lai Jiangshan <jiangshanlai@...il.com>, Zqiang <qiang.zhang@...ux.dev>,
Andrew Ballance <andrewjballance@...il.com>, linux-kernel@...r.kernel.org,
rust-for-linux@...r.kernel.org, rcu@...r.kernel.org,
maple-tree@...ts.infradead.org, linux-mm@...ck.org,
Alice Ryhl <aliceryhl@...gle.com>
Subject: [PATCH RFC 2/2] rust: maple_tree: add load_rcu()
Now that we have a concept of rcu-safe containers, we may add a
load_rcu() method to MapleTree that does not take the spinlock.
Signed-off-by: Alice Ryhl <aliceryhl@...gle.com>
---
rust/kernel/maple_tree.rs | 52 +++++++++++++++++++++++++++++++++++++++++++++++
1 file changed, 52 insertions(+)
diff --git a/rust/kernel/maple_tree.rs b/rust/kernel/maple_tree.rs
index e72eec56bf5772ada09239f47748cd649212d8b0..c466a9fe0a9a059ad5cff131085d610b76050420 100644
--- a/rust/kernel/maple_tree.rs
+++ b/rust/kernel/maple_tree.rs
@@ -16,6 +16,10 @@
alloc::Flags,
error::to_result,
prelude::*,
+ sync::rcu::{
+ self,
+ ForeignOwnableRcu, //
+ },
types::{ForeignOwnable, Opaque},
};
@@ -233,6 +237,54 @@ pub fn erase(&self, index: usize) -> Option<T> {
unsafe { T::try_from_foreign(ret) }
}
+ /// Load the value at the given index with rcu.
+ ///
+ /// # Examples
+ ///
+ /// Read the value under an rcu read lock. Even if the value is removed, it remains accessible
+ /// for one rcu grace period.
+ ///
+ /// ```ignore
+ /// use kernel::{
+ /// maple_tree::MapleTree,
+ /// sync::rcu::{self, RcuBox},
+ /// };
+ ///
+ /// let tree = KBox::pin_init(MapleTree::<RcuBox<i32>>::new(), GFP_KERNEL)?;
+ ///
+ /// let ten = RcuBox::new(10, GFP_KERNEL)?;
+ /// tree.insert(100, ten, GFP_KERNEL)?;
+ ///
+ /// let rcu_read_lock = rcu::Guard::new();
+ /// let ten = tree.load_rcu(100, &rcu_read_lock);
+ /// assert_eq!(ten, Some(&10));
+ ///
+ /// // Even if the value gets removed, we may continue to access it for one rcu grace period.
+ /// tree.erase(100);
+ /// assert_eq!(ten, Some(&10));
+ /// # Ok::<_, Error>(())
+ /// ```
+ #[inline]
+ pub fn load_rcu<'rcu>(
+ &self,
+ index: usize,
+ _rcu: &'rcu rcu::Guard,
+ ) -> Option<T::RcuBorrowed<'rcu>>
+ where
+ T: ForeignOwnableRcu,
+ {
+ // SAFETY: `self.tree` contains a valid maple tree.
+ let ret = unsafe { bindings::mtree_load(self.tree.get(), index) };
+ if ret.is_null() {
+ return None;
+ }
+
+ // SAFETY: If the pointer is not null, then it references a valid instance of `T`. It is
+ // safe to borrow the instance for 'rcu because the signature of this function enforces that
+ // the borrow does not outlive an rcu grace period.
+ Some(unsafe { T::rcu_borrow(ret) })
+ }
+
/// Lock the internal spinlock.
#[inline]
pub fn lock(&self) -> MapleGuard<'_, T> {
--
2.52.0.457.g6b5491de43-goog
Powered by blists - more mailing lists