1 // SPDX-License-Identifier: GPL-2.0
3 //! Generic kernel lock and guard.
5 //! It contains a generic Rust lock and guard that allow for different backends (e.g., mutexes,
6 //! spinlocks, raw spinlocks) to be provided with minimal effort.
8 use super::LockClassKey;
9 use crate::{init::PinInit, pin_init, str::CStr, types::Opaque, types::ScopeGuard};
10 use core::{cell::UnsafeCell, marker::PhantomData, marker::PhantomPinned};
16 /// The "backend" of a lock.
18 /// It is the actual implementation of the lock, without the need to repeat patterns used in all
23 /// - Implementers must ensure that only one thread/CPU may access the protected data once the lock
24 /// is owned, that is, between calls to [`lock`] and [`unlock`].
25 /// - Implementers must also ensure that [`relock`] uses the same locking method as the original
28 /// [`lock`]: Backend::lock
29 /// [`unlock`]: Backend::unlock
30 /// [`relock`]: Backend::relock
31 pub unsafe trait Backend {
32 /// The state required by the lock.
35 /// The state required to be kept between [`lock`] and [`unlock`].
37 /// [`lock`]: Backend::lock
38 /// [`unlock`]: Backend::unlock
41 /// Initialises the lock.
45 /// `ptr` must be valid for write for the duration of the call, while `name` and `key` must
46 /// remain valid for read indefinitely.
48 ptr: *mut Self::State,
49 name: *const core::ffi::c_char,
50 key: *mut bindings::lock_class_key,
53 /// Acquires the lock, making the caller its owner.
57 /// Callers must ensure that [`Backend::init`] has been previously called.
59 unsafe fn lock(ptr: *mut Self::State) -> Self::GuardState;
61 /// Releases the lock, giving up its ownership.
65 /// It must only be called by the current owner of the lock.
66 unsafe fn unlock(ptr: *mut Self::State, guard_state: &Self::GuardState);
68 /// Reacquires the lock, making the caller its owner.
72 /// Callers must ensure that `guard_state` comes from a previous call to [`Backend::lock`] (or
73 /// variant) that has been unlocked with [`Backend::unlock`] and will be relocked now.
74 unsafe fn relock(ptr: *mut Self::State, guard_state: &mut Self::GuardState) {
75 // SAFETY: The safety requirements ensure that the lock is initialised.
76 *guard_state = unsafe { Self::lock(ptr) };
80 /// A mutual exclusion primitive.
82 /// Exposes one of the kernel locking primitives. Which one is exposed depends on the lock
83 /// [`Backend`] specified as the generic parameter `B`.
85 pub struct Lock<T: ?Sized, B: Backend> {
86 /// The kernel lock object.
88 state: Opaque<B::State>,
90 /// Some locks are known to be self-referential (e.g., mutexes), while others are architecture
91 /// or config defined (e.g., spinlocks). So we conservatively require them to be pinned in case
92 /// some architecture uses self-references now or in the future.
96 /// The data protected by the lock.
97 pub(crate) data: UnsafeCell<T>,
100 // SAFETY: `Lock` can be transferred across thread boundaries iff the data it protects can.
101 unsafe impl<T: ?Sized + Send, B: Backend> Send for Lock<T, B> {}
103 // SAFETY: `Lock` serialises the interior mutability it provides, so it is `Sync` as long as the
104 // data it protects is `Send`.
105 unsafe impl<T: ?Sized + Send, B: Backend> Sync for Lock<T, B> {}
107 impl<T, B: Backend> Lock<T, B> {
108 /// Constructs a new lock initialiser.
109 pub fn new(t: T, name: &'static CStr, key: &'static LockClassKey) -> impl PinInit<Self> {
111 data: UnsafeCell::new(t),
113 // SAFETY: `slot` is valid while the closure is called and both `name` and `key` have
114 // static lifetimes so they live indefinitely.
115 state <- Opaque::ffi_init(|slot| unsafe {
116 B::init(slot, name.as_char_ptr(), key.as_ptr())
122 impl<T: ?Sized, B: Backend> Lock<T, B> {
123 /// Acquires the lock and gives the caller access to the data protected by it.
124 pub fn lock(&self) -> Guard<'_, T, B> {
125 // SAFETY: The constructor of the type calls `init`, so the existence of the object proves
126 // that `init` was called.
127 let state = unsafe { B::lock(self.state.get()) };
128 // SAFETY: The lock was just acquired.
129 unsafe { Guard::new(self, state) }
135 /// Allows mutual exclusion primitives that implement the [`Backend`] trait to automatically unlock
136 /// when a guard goes out of scope. It also provides a safe and convenient way to access the data
137 /// protected by the lock.
138 #[must_use = "the lock unlocks immediately when the guard is unused"]
139 pub struct Guard<'a, T: ?Sized, B: Backend> {
140 pub(crate) lock: &'a Lock<T, B>,
141 pub(crate) state: B::GuardState,
142 _not_send: PhantomData<*mut ()>,
145 // SAFETY: `Guard` is sync when the data protected by the lock is also sync.
146 unsafe impl<T: Sync + ?Sized, B: Backend> Sync for Guard<'_, T, B> {}
148 impl<T: ?Sized, B: Backend> Guard<'_, T, B> {
149 pub(crate) fn do_unlocked<U>(&mut self, cb: impl FnOnce() -> U) -> U {
150 // SAFETY: The caller owns the lock, so it is safe to unlock it.
151 unsafe { B::unlock(self.lock.state.get(), &self.state) };
153 // SAFETY: The lock was just unlocked above and is being relocked now.
155 ScopeGuard::new(|| unsafe { B::relock(self.lock.state.get(), &mut self.state) });
161 impl<T: ?Sized, B: Backend> core::ops::Deref for Guard<'_, T, B> {
164 fn deref(&self) -> &Self::Target {
165 // SAFETY: The caller owns the lock, so it is safe to deref the protected data.
166 unsafe { &*self.lock.data.get() }
170 impl<T: ?Sized, B: Backend> core::ops::DerefMut for Guard<'_, T, B> {
171 fn deref_mut(&mut self) -> &mut Self::Target {
172 // SAFETY: The caller owns the lock, so it is safe to deref the protected data.
173 unsafe { &mut *self.lock.data.get() }
177 impl<T: ?Sized, B: Backend> Drop for Guard<'_, T, B> {
179 // SAFETY: The caller owns the lock, so it is safe to unlock it.
180 unsafe { B::unlock(self.lock.state.get(), &self.state) };
184 impl<'a, T: ?Sized, B: Backend> Guard<'a, T, B> {
185 /// Constructs a new immutable lock guard.
189 /// The caller must ensure that it owns the lock.
190 pub(crate) unsafe fn new(lock: &'a Lock<T, B>, state: B::GuardState) -> Self {
194 _not_send: PhantomData,