~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

TOMOYO Linux Cross Reference
Linux/rust/kernel/sync/lock.rs

Version: ~ [ linux-6.12-rc7 ] ~ [ linux-6.11.7 ] ~ [ linux-6.10.14 ] ~ [ linux-6.9.12 ] ~ [ linux-6.8.12 ] ~ [ linux-6.7.12 ] ~ [ linux-6.6.60 ] ~ [ linux-6.5.13 ] ~ [ linux-6.4.16 ] ~ [ linux-6.3.13 ] ~ [ linux-6.2.16 ] ~ [ linux-6.1.116 ] ~ [ linux-6.0.19 ] ~ [ linux-5.19.17 ] ~ [ linux-5.18.19 ] ~ [ linux-5.17.15 ] ~ [ linux-5.16.20 ] ~ [ linux-5.15.171 ] ~ [ linux-5.14.21 ] ~ [ linux-5.13.19 ] ~ [ linux-5.12.19 ] ~ [ linux-5.11.22 ] ~ [ linux-5.10.229 ] ~ [ linux-5.9.16 ] ~ [ linux-5.8.18 ] ~ [ linux-5.7.19 ] ~ [ linux-5.6.19 ] ~ [ linux-5.5.19 ] ~ [ linux-5.4.285 ] ~ [ linux-5.3.18 ] ~ [ linux-5.2.21 ] ~ [ linux-5.1.21 ] ~ [ linux-5.0.21 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.323 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.336 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.337 ] ~ [ linux-4.4.302 ] ~ [ linux-3.10.108 ] ~ [ linux-2.6.32.71 ] ~ [ linux-2.6.0 ] ~ [ linux-2.4.37.11 ] ~ [ unix-v6-master ] ~ [ ccs-tools-1.8.12 ] ~ [ policy-sample ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

Diff markup

Differences between /rust/kernel/sync/lock.rs (Version linux-6.12-rc7) and /rust/kernel/sync/lock.rs (Version linux-4.16.18)


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

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

kernel.org | git.kernel.org | LWN.net | Project Home | SVN repository | Mail admin

Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.

sflogo.php