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