1 // SPDX-License-Identifier: GPL-2.0 1 // SPDX-License-Identifier: GPL-2.0 2 2 3 //! Generic kernel lock and guard. 3 //! Generic kernel lock and guard. 4 //! 4 //! 5 //! It contains a generic Rust 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 w 6 //! spinlocks, raw spinlocks) to be provided with minimal effort. 7 7 8 use super::LockClassKey; 8 use super::LockClassKey; 9 use crate::{init::PinInit, pin_init, str::CStr 9 use crate::{init::PinInit, pin_init, str::CStr, types::Opaque, types::ScopeGuard}; 10 use core::{cell::UnsafeCell, marker::PhantomDa 10 use core::{cell::UnsafeCell, marker::PhantomData, marker::PhantomPinned}; 11 use macros::pin_data; 11 use macros::pin_data; 12 12 13 pub mod mutex; 13 pub mod mutex; 14 pub mod spinlock; 14 pub mod spinlock; 15 15 16 /// The "backend" of a lock. 16 /// The "backend" of a lock. 17 /// 17 /// 18 /// It is the actual implementation of the loc 18 /// It is the actual implementation of the lock, without the need to repeat patterns used in all 19 /// locks. 19 /// locks. 20 /// 20 /// 21 /// # Safety 21 /// # Safety 22 /// 22 /// 23 /// - Implementers must ensure that only one t 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 [`lo 24 /// is owned, that is, between calls to [`lock`] and [`unlock`]. 25 /// - Implementers must also ensure that [`rel 25 /// - Implementers must also ensure that [`relock`] uses the same locking method as the original 26 /// lock operation. 26 /// lock operation. 27 /// 27 /// 28 /// [`lock`]: Backend::lock 28 /// [`lock`]: Backend::lock 29 /// [`unlock`]: Backend::unlock 29 /// [`unlock`]: Backend::unlock 30 /// [`relock`]: Backend::relock 30 /// [`relock`]: Backend::relock 31 pub unsafe trait Backend { 31 pub unsafe trait Backend { 32 /// The state required by the lock. 32 /// The state required by the lock. 33 type State; 33 type State; 34 34 35 /// The state required to be kept between 35 /// The state required to be kept between [`lock`] and [`unlock`]. 36 /// 36 /// 37 /// [`lock`]: Backend::lock 37 /// [`lock`]: Backend::lock 38 /// [`unlock`]: Backend::unlock 38 /// [`unlock`]: Backend::unlock 39 type GuardState; 39 type GuardState; 40 40 41 /// Initialises the lock. 41 /// Initialises the lock. 42 /// 42 /// 43 /// # Safety 43 /// # Safety 44 /// 44 /// 45 /// `ptr` must be valid for write for the 45 /// `ptr` must be valid for write for the duration of the call, while `name` and `key` must 46 /// remain valid for read indefinitely. 46 /// remain valid for read indefinitely. 47 unsafe fn init( 47 unsafe fn init( 48 ptr: *mut Self::State, 48 ptr: *mut Self::State, 49 name: *const core::ffi::c_char, 49 name: *const core::ffi::c_char, 50 key: *mut bindings::lock_class_key, 50 key: *mut bindings::lock_class_key, 51 ); 51 ); 52 52 53 /// Acquires the lock, making the caller i 53 /// Acquires the lock, making the caller its owner. 54 /// 54 /// 55 /// # Safety 55 /// # Safety 56 /// 56 /// 57 /// Callers must ensure that [`Backend::in 57 /// Callers must ensure that [`Backend::init`] has been previously called. 58 #[must_use] 58 #[must_use] 59 unsafe fn lock(ptr: *mut Self::State) -> S 59 unsafe fn lock(ptr: *mut Self::State) -> Self::GuardState; 60 60 61 /// Releases the lock, giving up its owner 61 /// Releases the lock, giving up its ownership. 62 /// 62 /// 63 /// # Safety 63 /// # Safety 64 /// 64 /// 65 /// It must only be called by the current 65 /// It must only be called by the current owner of the lock. 66 unsafe fn unlock(ptr: *mut Self::State, gu 66 unsafe fn unlock(ptr: *mut Self::State, guard_state: &Self::GuardState); 67 67 68 /// Reacquires the lock, making the caller 68 /// Reacquires the lock, making the caller its owner. 69 /// 69 /// 70 /// # Safety 70 /// # Safety 71 /// 71 /// 72 /// Callers must ensure that `guard_state` 72 /// Callers must ensure that `guard_state` comes from a previous call to [`Backend::lock`] (or 73 /// variant) that has been unlocked with [ 73 /// variant) that has been unlocked with [`Backend::unlock`] and will be relocked now. 74 unsafe fn relock(ptr: *mut Self::State, gu 74 unsafe fn relock(ptr: *mut Self::State, guard_state: &mut Self::GuardState) { 75 // SAFETY: The safety requirements ens 75 // SAFETY: The safety requirements ensure that the lock is initialised. 76 *guard_state = unsafe { Self::lock(ptr 76 *guard_state = unsafe { Self::lock(ptr) }; 77 } 77 } 78 } 78 } 79 79 80 /// A mutual exclusion primitive. 80 /// A mutual exclusion primitive. 81 /// 81 /// 82 /// Exposes one of the kernel locking primitiv 82 /// Exposes one of the kernel locking primitives. Which one is exposed depends on the lock 83 /// [`Backend`] specified as the generic param 83 /// [`Backend`] specified as the generic parameter `B`. 84 #[pin_data] 84 #[pin_data] 85 pub struct Lock<T: ?Sized, B: Backend> { 85 pub struct Lock<T: ?Sized, B: Backend> { 86 /// The kernel lock object. 86 /// The kernel lock object. 87 #[pin] 87 #[pin] 88 state: Opaque<B::State>, 88 state: Opaque<B::State>, 89 89 90 /// Some locks are known to be self-refere 90 /// Some locks are known to be self-referential (e.g., mutexes), while others are architecture 91 /// or config defined (e.g., spinlocks). S 91 /// or config defined (e.g., spinlocks). So we conservatively require them to be pinned in case 92 /// some architecture uses self-references 92 /// some architecture uses self-references now or in the future. 93 #[pin] 93 #[pin] 94 _pin: PhantomPinned, 94 _pin: PhantomPinned, 95 95 96 /// The data protected by the lock. 96 /// The data protected by the lock. 97 pub(crate) data: UnsafeCell<T>, 97 pub(crate) data: UnsafeCell<T>, 98 } 98 } 99 99 100 // SAFETY: `Lock` can be transferred across th 100 // SAFETY: `Lock` can be transferred across thread boundaries iff the data it protects can. 101 unsafe impl<T: ?Sized + Send, B: Backend> Send 101 unsafe impl<T: ?Sized + Send, B: Backend> Send for Lock<T, B> {} 102 102 103 // SAFETY: `Lock` serialises the interior muta 103 // SAFETY: `Lock` serialises the interior mutability it provides, so it is `Sync` as long as the 104 // data it protects is `Send`. 104 // data it protects is `Send`. 105 unsafe impl<T: ?Sized + Send, B: Backend> Sync 105 unsafe impl<T: ?Sized + Send, B: Backend> Sync for Lock<T, B> {} 106 106 107 impl<T, B: Backend> Lock<T, B> { 107 impl<T, B: Backend> Lock<T, B> { 108 /// Constructs a new lock initialiser. 108 /// Constructs a new lock initialiser. 109 pub fn new(t: T, name: &'static CStr, key: 109 pub fn new(t: T, name: &'static CStr, key: &'static LockClassKey) -> impl PinInit<Self> { 110 pin_init!(Self { 110 pin_init!(Self { 111 data: UnsafeCell::new(t), 111 data: UnsafeCell::new(t), 112 _pin: PhantomPinned, 112 _pin: PhantomPinned, 113 // SAFETY: `slot` is valid while t 113 // SAFETY: `slot` is valid while the closure is called and both `name` and `key` have 114 // static lifetimes so they live i 114 // static lifetimes so they live indefinitely. 115 state <- Opaque::ffi_init(|slot| u 115 state <- Opaque::ffi_init(|slot| unsafe { 116 B::init(slot, name.as_char_ptr 116 B::init(slot, name.as_char_ptr(), key.as_ptr()) 117 }), 117 }), 118 }) 118 }) 119 } 119 } 120 } 120 } 121 121 122 impl<T: ?Sized, B: Backend> Lock<T, B> { 122 impl<T: ?Sized, B: Backend> Lock<T, B> { 123 /// Acquires the lock and gives the caller 123 /// Acquires the lock and gives the caller access to the data protected by it. 124 pub fn lock(&self) -> Guard<'_, T, B> { 124 pub fn lock(&self) -> Guard<'_, T, B> { 125 // SAFETY: The constructor of the type 125 // SAFETY: The constructor of the type calls `init`, so the existence of the object proves 126 // that `init` was called. 126 // that `init` was called. 127 let state = unsafe { B::lock(self.stat 127 let state = unsafe { B::lock(self.state.get()) }; 128 // SAFETY: The lock was just acquired. 128 // SAFETY: The lock was just acquired. 129 unsafe { Guard::new(self, state) } 129 unsafe { Guard::new(self, state) } 130 } 130 } 131 } 131 } 132 132 133 /// A lock guard. 133 /// A lock guard. 134 /// 134 /// 135 /// Allows mutual exclusion primitives that im 135 /// Allows mutual exclusion primitives that implement the [`Backend`] trait to automatically unlock 136 /// when a guard goes out of scope. It also pr 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. 137 /// protected by the lock. 138 #[must_use = "the lock unlocks immediately whe 138 #[must_use = "the lock unlocks immediately when the guard is unused"] 139 pub struct Guard<'a, T: ?Sized, B: Backend> { 139 pub struct Guard<'a, T: ?Sized, B: Backend> { 140 pub(crate) lock: &'a Lock<T, B>, 140 pub(crate) lock: &'a Lock<T, B>, 141 pub(crate) state: B::GuardState, 141 pub(crate) state: B::GuardState, 142 _not_send: PhantomData<*mut ()>, 142 _not_send: PhantomData<*mut ()>, 143 } 143 } 144 144 145 // SAFETY: `Guard` is sync when the data prote 145 // SAFETY: `Guard` is sync when the data protected by the lock is also sync. 146 unsafe impl<T: Sync + ?Sized, B: Backend> Sync 146 unsafe impl<T: Sync + ?Sized, B: Backend> Sync for Guard<'_, T, B> {} 147 147 148 impl<T: ?Sized, B: Backend> Guard<'_, T, B> { 148 impl<T: ?Sized, B: Backend> Guard<'_, T, B> { 149 pub(crate) fn do_unlocked<U>(&mut self, cb 149 pub(crate) fn do_unlocked<U>(&mut self, cb: impl FnOnce() -> U) -> U { 150 // SAFETY: The caller owns the lock, s 150 // SAFETY: The caller owns the lock, so it is safe to unlock it. 151 unsafe { B::unlock(self.lock.state.get 151 unsafe { B::unlock(self.lock.state.get(), &self.state) }; 152 152 153 // SAFETY: The lock was just unlocked 153 // SAFETY: The lock was just unlocked above and is being relocked now. 154 let _relock = 154 let _relock = 155 ScopeGuard::new(|| unsafe { B::rel 155 ScopeGuard::new(|| unsafe { B::relock(self.lock.state.get(), &mut self.state) }); 156 156 157 cb() 157 cb() 158 } 158 } 159 } 159 } 160 160 161 impl<T: ?Sized, B: Backend> core::ops::Deref f 161 impl<T: ?Sized, B: Backend> core::ops::Deref for Guard<'_, T, B> { 162 type Target = T; 162 type Target = T; 163 163 164 fn deref(&self) -> &Self::Target { 164 fn deref(&self) -> &Self::Target { 165 // SAFETY: The caller owns the lock, s 165 // SAFETY: The caller owns the lock, so it is safe to deref the protected data. 166 unsafe { &*self.lock.data.get() } 166 unsafe { &*self.lock.data.get() } 167 } 167 } 168 } 168 } 169 169 170 impl<T: ?Sized, B: Backend> core::ops::DerefMu 170 impl<T: ?Sized, B: Backend> core::ops::DerefMut for Guard<'_, T, B> { 171 fn deref_mut(&mut self) -> &mut Self::Targ 171 fn deref_mut(&mut self) -> &mut Self::Target { 172 // SAFETY: The caller owns the lock, s 172 // SAFETY: The caller owns the lock, so it is safe to deref the protected data. 173 unsafe { &mut *self.lock.data.get() } 173 unsafe { &mut *self.lock.data.get() } 174 } 174 } 175 } 175 } 176 176 177 impl<T: ?Sized, B: Backend> Drop for Guard<'_, 177 impl<T: ?Sized, B: Backend> Drop for Guard<'_, T, B> { 178 fn drop(&mut self) { 178 fn drop(&mut self) { 179 // SAFETY: The caller owns the lock, s 179 // SAFETY: The caller owns the lock, so it is safe to unlock it. 180 unsafe { B::unlock(self.lock.state.get 180 unsafe { B::unlock(self.lock.state.get(), &self.state) }; 181 } 181 } 182 } 182 } 183 183 184 impl<'a, T: ?Sized, B: Backend> Guard<'a, T, B 184 impl<'a, T: ?Sized, B: Backend> Guard<'a, T, B> { 185 /// Constructs a new immutable lock guard. 185 /// Constructs a new immutable lock guard. 186 /// 186 /// 187 /// # Safety 187 /// # Safety 188 /// 188 /// 189 /// The caller must ensure that it owns th 189 /// The caller must ensure that it owns the lock. 190 pub(crate) unsafe fn new(lock: &'a Lock<T, 190 pub(crate) unsafe fn new(lock: &'a Lock<T, B>, state: B::GuardState) -> Self { 191 Self { 191 Self { 192 lock, 192 lock, 193 state, 193 state, 194 _not_send: PhantomData, 194 _not_send: PhantomData, 195 } 195 } 196 } 196 } 197 } 197 }
Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.