Skip to content

Commit dd491bd

Browse files
Darksonnjannau
authored andcommitted
rust: maple_tree: add lock guard for maple tree
To load a value, one must be careful to hold the lock while accessing it. To enable this, we add a lock() method so that you can perform operations on the value before the spinlock is released. This adds a MapleGuard type without using the existing SpinLock type. This ensures that the MapleGuard type is not unnecessarily large, and that it is easy to swap out the type of lock in case the C maple tree is changed to use a different kind of lock. There are two ways of using the lock guard: You can call load() directly to load a value under the lock, or you can create an MaState to iterate the tree with find(). The find() method does not have the mas_ prefix since it's a method on MaState, and being a method on that struct serves a similar purpose to the mas_ prefix in C. Link: https://lkml.kernel.org/r/20250902-maple-tree-v3-2-fb5c8958fb1e@google.com Co-developed-by: Andrew Ballance <andrewjballance@gmail.com> Signed-off-by: Andrew Ballance <andrewjballance@gmail.com> Reviewed-by: Andrew Ballance <andrewjballance@gmail.com> Reviewed-by: Danilo Krummrich <dakr@kernel.org> Signed-off-by: Alice Ryhl <aliceryhl@google.com> Cc: Andreas Hindborg <a.hindborg@kernel.org> Cc: Björn Roy Baron <bjorn3_gh@protonmail.com> Cc: Boqun Feng <boqun.feng@gmail.com> Cc: Daniel Almeida <daniel.almeida@collabora.com> Cc: Gary Guo <gary@garyguo.net> Cc: Liam Howlett <liam.howlett@oracle.com> Cc: Lorenzo Stoakes <lorenzo.stoakes@oracle.com> Cc: Miguel Ojeda <ojeda@kernel.org> Cc: Trevor Gross <tmgross@umich.edu> Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
1 parent ec575f4 commit dd491bd

1 file changed

Lines changed: 140 additions & 0 deletions

File tree

rust/kernel/maple_tree.rs

Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,23 @@ impl<T: ForeignOwnable> MapleTree<T> {
213213
unsafe { T::try_from_foreign(ret) }
214214
}
215215

216+
/// Lock the internal spinlock.
217+
#[inline]
218+
pub fn lock(&self) -> MapleGuard<'_, T> {
219+
// SAFETY: It's safe to lock the spinlock in a maple tree.
220+
unsafe { bindings::spin_lock(self.ma_lock()) };
221+
222+
// INVARIANT: We just took the spinlock.
223+
MapleGuard(self)
224+
}
225+
226+
#[inline]
227+
fn ma_lock(&self) -> *mut bindings::spinlock_t {
228+
// SAFETY: This pointer offset operation stays in-bounds.
229+
let lock_ptr = unsafe { &raw mut (*self.tree.get()).__bindgen_anon_1.ma_lock };
230+
lock_ptr.cast()
231+
}
232+
216233
/// Free all `T` instances in this tree.
217234
///
218235
/// # Safety
@@ -256,6 +273,91 @@ impl<T: ForeignOwnable> PinnedDrop for MapleTree<T> {
256273
}
257274
}
258275

276+
/// A reference to a [`MapleTree`] that owns the inner lock.
277+
///
278+
/// # Invariants
279+
///
280+
/// This guard owns the inner spinlock.
281+
#[must_use = "if unused, the lock will be immediately unlocked"]
282+
pub struct MapleGuard<'tree, T: ForeignOwnable>(&'tree MapleTree<T>);
283+
284+
impl<'tree, T: ForeignOwnable> Drop for MapleGuard<'tree, T> {
285+
#[inline]
286+
fn drop(&mut self) {
287+
// SAFETY: By the type invariants, we hold this spinlock.
288+
unsafe { bindings::spin_unlock(self.0.ma_lock()) };
289+
}
290+
}
291+
292+
impl<'tree, T: ForeignOwnable> MapleGuard<'tree, T> {
293+
/// Create a [`MaState`] protected by this lock guard.
294+
pub fn ma_state(&mut self, first: usize, end: usize) -> MaState<'_, T> {
295+
// SAFETY: The `MaState` borrows this `MapleGuard`, so it can also borrow the `MapleGuard`s
296+
// read/write permissions to the maple tree.
297+
unsafe { MaState::new_raw(self.0, first, end) }
298+
}
299+
300+
/// Load the value at the given index.
301+
///
302+
/// # Examples
303+
///
304+
/// Read the value while holding the spinlock.
305+
///
306+
/// ```
307+
/// use kernel::maple_tree::MapleTree;
308+
///
309+
/// let tree = KBox::pin_init(MapleTree::<KBox<i32>>::new(), GFP_KERNEL)?;
310+
///
311+
/// let ten = KBox::new(10, GFP_KERNEL)?;
312+
/// let twenty = KBox::new(20, GFP_KERNEL)?;
313+
/// tree.insert(100, ten, GFP_KERNEL)?;
314+
/// tree.insert(200, twenty, GFP_KERNEL)?;
315+
///
316+
/// let mut lock = tree.lock();
317+
/// assert_eq!(lock.load(100).map(|v| *v), Some(10));
318+
/// assert_eq!(lock.load(200).map(|v| *v), Some(20));
319+
/// assert_eq!(lock.load(300).map(|v| *v), None);
320+
/// # Ok::<_, Error>(())
321+
/// ```
322+
///
323+
/// Increment refcount under the lock, to keep value alive afterwards.
324+
///
325+
/// ```
326+
/// use kernel::maple_tree::MapleTree;
327+
/// use kernel::sync::Arc;
328+
///
329+
/// let tree = KBox::pin_init(MapleTree::<Arc<i32>>::new(), GFP_KERNEL)?;
330+
///
331+
/// let ten = Arc::new(10, GFP_KERNEL)?;
332+
/// let twenty = Arc::new(20, GFP_KERNEL)?;
333+
/// tree.insert(100, ten, GFP_KERNEL)?;
334+
/// tree.insert(200, twenty, GFP_KERNEL)?;
335+
///
336+
/// // Briefly take the lock to increment the refcount.
337+
/// let value = tree.lock().load(100).map(Arc::from);
338+
///
339+
/// // At this point, another thread might remove the value.
340+
/// tree.erase(100);
341+
///
342+
/// // But we can still access it because we took a refcount.
343+
/// assert_eq!(value.map(|v| *v), Some(10));
344+
/// # Ok::<_, Error>(())
345+
/// ```
346+
#[inline]
347+
pub fn load(&mut self, index: usize) -> Option<T::BorrowedMut<'_>> {
348+
// SAFETY: `self.tree` contains a valid maple tree.
349+
let ret = unsafe { bindings::mtree_load(self.0.tree.get(), index) };
350+
if ret.is_null() {
351+
return None;
352+
}
353+
354+
// SAFETY: If the pointer is not null, then it references a valid instance of `T`. It is
355+
// safe to borrow the instance mutably because the signature of this function enforces that
356+
// the mutable borrow is not used after the spinlock is dropped.
357+
Some(unsafe { T::borrow_mut(ret) })
358+
}
359+
}
360+
259361
/// A helper type used for navigating a [`MapleTree`].
260362
///
261363
/// # Invariants
@@ -309,6 +411,44 @@ impl<'tree, T: ForeignOwnable> MaState<'tree, T> {
309411
// to the tree.
310412
unsafe { bindings::mas_find(self.as_raw(), max) }
311413
}
414+
415+
/// Find the next entry in the maple tree.
416+
///
417+
/// # Examples
418+
///
419+
/// Iterate the maple tree.
420+
///
421+
/// ```
422+
/// use kernel::maple_tree::MapleTree;
423+
/// use kernel::sync::Arc;
424+
///
425+
/// let tree = KBox::pin_init(MapleTree::<Arc<i32>>::new(), GFP_KERNEL)?;
426+
///
427+
/// let ten = Arc::new(10, GFP_KERNEL)?;
428+
/// let twenty = Arc::new(20, GFP_KERNEL)?;
429+
/// tree.insert(100, ten, GFP_KERNEL)?;
430+
/// tree.insert(200, twenty, GFP_KERNEL)?;
431+
///
432+
/// let mut ma_lock = tree.lock();
433+
/// let mut iter = ma_lock.ma_state(0, usize::MAX);
434+
///
435+
/// assert_eq!(iter.find(usize::MAX).map(|v| *v), Some(10));
436+
/// assert_eq!(iter.find(usize::MAX).map(|v| *v), Some(20));
437+
/// assert!(iter.find(usize::MAX).is_none());
438+
/// # Ok::<_, Error>(())
439+
/// ```
440+
#[inline]
441+
pub fn find(&mut self, max: usize) -> Option<T::BorrowedMut<'_>> {
442+
let ret = self.mas_find_raw(max);
443+
if ret.is_null() {
444+
return None;
445+
}
446+
447+
// SAFETY: If the pointer is not null, then it references a valid instance of `T`. It's
448+
// safe to access it mutably as the returned reference borrows this `MaState`, and the
449+
// `MaState` has read/write access to the maple tree.
450+
Some(unsafe { T::borrow_mut(ret) })
451+
}
312452
}
313453

314454
/// Error type for failure to insert a new value.

0 commit comments

Comments
 (0)