| 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 |
/// Creates a [`SpinLock`] initialiser with the given name and a newly-created lock class. |
| 8 |
/// |
| 9 |
/// It uses the name if one is given, otherwise it generates one based on the file name and line |
| 10 |
/// number. |
| 11 |
#[macro_export] |
| 12 |
macro_rules! new_spinlock { |
| 13 |
($inner:expr $(, $name:literal)? $(,)?) => { |
| 14 |
$crate::sync::SpinLock::new( |
| 15 |
$inner, $crate::optional_name!($($name)?), $crate::static_lock_class!()) |
| 16 |
}; |
| 17 |
} |
| 18 |
pub use new_spinlock; |
| 19 |
|
| 20 |
/// A spinlock. |
| 21 |
/// |
| 22 |
/// Exposes the kernel's [`spinlock_t`]. When multiple CPUs attempt to lock the same spinlock, only |
| 23 |
/// one at a time is allowed to progress, the others will block (spinning) until the spinlock is |
| 24 |
/// unlocked, at which point another CPU will be allowed to make progress. |
| 25 |
/// |
| 26 |
/// Instances of [`SpinLock`] need a lock class and to be pinned. The recommended way to create such |
| 27 |
/// instances is with the [`pin_init`](pin_init::pin_init) and [`new_spinlock`] macros. |
| 28 |
/// |
| 29 |
/// # Examples |
| 30 |
/// |
| 31 |
/// The following example shows how to declare, allocate and initialise a struct (`Example`) that |
| 32 |
/// contains an inner struct (`Inner`) that is protected by a spinlock. |
| 33 |
/// |
| 34 |
/// ``` |
| 35 |
/// use kernel::sync::{new_spinlock, SpinLock}; |
| 36 |
/// |
| 37 |
/// struct Inner { |
| 38 |
/// a: u32, |
| 39 |
/// b: u32, |
| 40 |
/// } |
| 41 |
/// |
| 42 |
/// #[pin_data] |
| 43 |
/// struct Example { |
| 44 |
/// c: u32, |
| 45 |
/// #[pin] |
| 46 |
/// d: SpinLock<Inner>, |
| 47 |
/// } |
| 48 |
/// |
| 49 |
/// impl Example { |
| 50 |
/// fn new() -> impl PinInit<Self> { |
| 51 |
/// pin_init!(Self { |
| 52 |
/// c: 10, |
| 53 |
/// d <- new_spinlock!(Inner { a: 20, b: 30 }), |
| 54 |
/// }) |
| 55 |
/// } |
| 56 |
/// } |
| 57 |
/// |
| 58 |
/// // Allocate a boxed `Example`. |
| 59 |
/// let e = KBox::pin_init(Example::new(), GFP_KERNEL)?; |
| 60 |
/// assert_eq!(e.c, 10); |
| 61 |
/// assert_eq!(e.d.lock().a, 20); |
| 62 |
/// assert_eq!(e.d.lock().b, 30); |
| 63 |
/// # Ok::<(), Error>(()) |
| 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`]: srctree/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 |
/// A [`Guard`] acquired from locking a [`SpinLock`]. |
| 91 |
/// |
| 92 |
/// This is simply a type alias for a [`Guard`] returned from locking a [`SpinLock`]. It will unlock |
| 93 |
/// the [`SpinLock`] upon being dropped. |
| 94 |
/// |
| 95 |
/// [`Guard`]: super::Guard |
| 96 |
pub type SpinLockGuard<'a, T> = super::Guard<'a, T, SpinLockBackend>; |
| 97 |
|
| 98 |
// SAFETY: The underlying kernel `spinlock_t` object ensures mutual exclusion. `relock` uses the |
| 99 |
// default implementation that always calls the same locking method. |
| 100 |
unsafe impl super::Backend for SpinLockBackend { |
| 101 |
type State = bindings::spinlock_t; |
| 102 |
type GuardState = (); |
| 103 |
|
| 104 |
#[inline] |
| 105 |
unsafe fn init( |
| 106 |
ptr: *mut Self::State, |
| 107 |
name: *const crate::ffi::c_char, |
| 108 |
key: *mut bindings::lock_class_key, |
| 109 |
) { |
| 110 |
// SAFETY: The safety requirements ensure that `ptr` is valid for writes, and `name` and |
| 111 |
// `key` are valid for read indefinitely. |
| 112 |
unsafe { bindings::__spin_lock_init(ptr, name, key) } |
| 113 |
} |
| 114 |
|
| 115 |
#[inline] |
| 116 |
unsafe fn lock(ptr: *mut Self::State) -> Self::GuardState { |
| 117 |
// SAFETY: The safety requirements of this function ensure that `ptr` points to valid |
| 118 |
// memory, and that it has been initialised before. |
| 119 |
unsafe { bindings::spin_lock(ptr) } |
| 120 |
} |
| 121 |
|
| 122 |
#[inline] |
| 123 |
unsafe fn unlock(ptr: *mut Self::State, _guard_state: &Self::GuardState) { |
| 124 |
// SAFETY: The safety requirements of this function ensure that `ptr` is valid and that the |
| 125 |
// caller is the owner of the spinlock. |
| 126 |
unsafe { bindings::spin_unlock(ptr) } |
| 127 |
} |
| 128 |
|
| 129 |
#[inline] |
| 130 |
unsafe fn try_lock(ptr: *mut Self::State) -> Option<Self::GuardState> { |
| 131 |
// SAFETY: The `ptr` pointer is guaranteed to be valid and initialized before use. |
| 132 |
let result = unsafe { bindings::spin_trylock(ptr) }; |
| 133 |
|
| 134 |
if result != 0 { |
| 135 |
Some(()) |
| 136 |
} else { |
| 137 |
None |
| 138 |
} |
| 139 |
} |
| 140 |
|
| 141 |
#[inline] |
| 142 |
unsafe fn assert_is_held(ptr: *mut Self::State) { |
| 143 |
// SAFETY: The `ptr` pointer is guaranteed to be valid and initialized before use. |
| 144 |
unsafe { bindings::spin_assert_is_held(ptr) } |
| 145 |
} |
| 146 |
} |