Skip to content

Commit acd91b2

Browse files
committed
rust: lock: introduce SpinLock
This is the `spinlock_t` lock backend and allows Rust code to use the kernel spinlock idiomatically. Cc: Peter Zijlstra <[email protected]> Cc: Ingo Molnar <[email protected]> Cc: Will Deacon <[email protected]> Cc: Waiman Long <[email protected]> Signed-off-by: Wedson Almeida Filho <[email protected]>
1 parent 1577a19 commit acd91b2

File tree

4 files changed

+142
-1
lines changed

4 files changed

+142
-1
lines changed

rust/helpers.c

+24
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
#include <linux/build_bug.h>
2323
#include <linux/refcount.h>
2424
#include <linux/mutex.h>
25+
#include <linux/spinlock.h>
2526

2627
__noreturn void rust_helper_BUG(void)
2728
{
@@ -35,6 +36,29 @@ void rust_helper_mutex_lock(struct mutex *lock)
3536
}
3637
EXPORT_SYMBOL_GPL(rust_helper_mutex_lock);
3738

39+
void rust_helper___spin_lock_init(spinlock_t *lock, const char *name,
40+
struct lock_class_key *key)
41+
{
42+
#ifdef CONFIG_DEBUG_SPINLOCK
43+
spin_lock_init_with_key(lock, name, key);
44+
#else
45+
spin_lock_init(lock);
46+
#endif
47+
}
48+
EXPORT_SYMBOL_GPL(rust_helper___spin_lock_init);
49+
50+
void rust_helper_spin_lock(spinlock_t *lock)
51+
{
52+
spin_lock(lock);
53+
}
54+
EXPORT_SYMBOL_GPL(rust_helper_spin_lock);
55+
56+
void rust_helper_spin_unlock(spinlock_t *lock)
57+
{
58+
spin_unlock(lock);
59+
}
60+
EXPORT_SYMBOL_GPL(rust_helper_spin_unlock);
61+
3862
refcount_t rust_helper_REFCOUNT_INIT(int n)
3963
{
4064
return (refcount_t)REFCOUNT_INIT(n);

rust/kernel/sync.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ mod arc;
1111
pub mod lock;
1212

1313
pub use arc::{Arc, ArcBorrow, UniqueArc};
14-
pub use lock::mutex::Mutex;
14+
pub use lock::{mutex::Mutex, spinlock::SpinLock};
1515

1616
/// Represents a lockdep class. It's a wrapper around C's `lock_class_key`.
1717
#[repr(transparent)]

rust/kernel/sync/lock.rs

+1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ use core::{cell::UnsafeCell, marker::PhantomData, marker::PhantomPinned};
1111
use macros::pin_data;
1212

1313
pub mod mutex;
14+
pub mod spinlock;
1415

1516
/// The "backend" of a lock.
1617
///

rust/kernel/sync/lock/spinlock.rs

+116
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
// SPDX-License-Identifier: GPL-2.0
2+
3+
//! A kernel spinlock.
4+
//!
5+
//! This module allows Rust code to use the kernel's `spinlock_t`.
6+
7+
use crate::bindings;
8+
9+
/// Creates a [`SpinLock`] initialiser with the given name and a newly-created lock class.
10+
///
11+
/// It uses the name if one is given, otherwise it generates one based on the file name and line
12+
/// number.
13+
#[macro_export]
14+
macro_rules! new_spinlock {
15+
($inner:expr $(, $name:literal)? $(,)?) => {
16+
$crate::sync::SpinLock::new(
17+
$inner, $crate::optional_name!($($name)?), $crate::static_lock_class!())
18+
};
19+
}
20+
21+
/// A spinlock.
22+
///
23+
/// Exposes the kernel's [`spinlock_t`]. When multiple CPUs attempt to lock the same spinlock, only
24+
/// one at a time is allowed to progress, the others will block (spinning) until the spinlock is
25+
/// unlocked, at which point another CPU will be allowed to make progress.
26+
///
27+
/// Instances of [`SpinLock`] need a lock class and to be pinned. The recommended way to create such
28+
/// instances is with the [`pin_init`](crate::pin_init) and [`new_spinlock`] macros.
29+
///
30+
/// # Examples
31+
///
32+
/// The following example shows how to declare, allocate and initialise a struct (`Example`) that
33+
/// contains an inner struct (`Inner`) that is protected by a spinlock.
34+
///
35+
/// ```
36+
/// use kernel::{init::InPlaceInit, init::PinInit, new_spinlock, pin_init, sync::SpinLock};
37+
///
38+
/// struct Inner {
39+
/// a: u32,
40+
/// b: u32,
41+
/// }
42+
///
43+
/// #[pin_data]
44+
/// struct Example {
45+
/// c: u32,
46+
/// #[pin]
47+
/// d: SpinLock<Inner>,
48+
/// }
49+
///
50+
/// impl Example {
51+
/// fn new() -> impl PinInit<Self> {
52+
/// pin_init!(Self {
53+
/// c: 10,
54+
/// d <- new_spinlock!(Inner { a: 20, b: 30 }),
55+
/// })
56+
/// }
57+
/// }
58+
///
59+
/// // Allocate a boxed `Example`.
60+
/// let e = Box::pin_init(Example::new())?;
61+
/// assert_eq!(e.c, 10);
62+
/// assert_eq!(e.d.lock().a, 20);
63+
/// assert_eq!(e.d.lock().b, 30);
64+
/// ```
65+
///
66+
/// The following example shows how to use interior mutability to modify the contents of a struct
67+
/// protected by a spinlock despite only having a shared reference:
68+
///
69+
/// ```
70+
/// use kernel::sync::SpinLock;
71+
///
72+
/// struct Example {
73+
/// a: u32,
74+
/// b: u32,
75+
/// }
76+
///
77+
/// fn example(m: &SpinLock<Example>) {
78+
/// let mut guard = m.lock();
79+
/// guard.a += 10;
80+
/// guard.b += 20;
81+
/// }
82+
/// ```
83+
///
84+
/// [`spinlock_t`]: ../../../../include/linux/spinlock.h
85+
pub type SpinLock<T> = super::Lock<T, SpinLockBackend>;
86+
87+
/// A kernel `spinlock_t` lock backend.
88+
pub struct SpinLockBackend;
89+
90+
// SAFETY: The underlying kernel `spinlock_t` object ensures mutual exclusion.
91+
unsafe impl super::Backend for SpinLockBackend {
92+
type State = bindings::spinlock_t;
93+
type GuardState = ();
94+
95+
unsafe fn init(
96+
ptr: *mut Self::State,
97+
name: *const core::ffi::c_char,
98+
key: *mut bindings::lock_class_key,
99+
) {
100+
// SAFETY: The safety requirements ensure that `ptr` is valid for writes, and `name` and
101+
// `key` are valid for read indefinitely.
102+
unsafe { bindings::__spin_lock_init(ptr, name, key) }
103+
}
104+
105+
unsafe fn lock(ptr: *mut Self::State) -> Self::GuardState {
106+
// SAFETY: The safety requirements of this function ensure that `ptr` points to valid
107+
// memory, and that it has been initialised before.
108+
unsafe { bindings::spin_lock(ptr) }
109+
}
110+
111+
unsafe fn unlock(ptr: *mut Self::State, _guard_state: &Self::GuardState) {
112+
// SAFETY: The safety requirements of this function ensure that `ptr` is valid and that the
113+
// caller is the owner of the mutex.
114+
unsafe { bindings::spin_unlock(ptr) }
115+
}
116+
}

0 commit comments

Comments
 (0)