1 // SPDX-License-Identifier: GPL-2.0 1 // SPDX-License-Identifier: GPL-2.0 2 2 3 //! A kernel mutex. 3 //! A kernel mutex. 4 //! 4 //! 5 //! This module allows Rust code to use the ke 5 //! This module allows Rust code to use the kernel's `struct mutex`. 6 6 7 /// Creates a [`Mutex`] initialiser with the g 7 /// Creates a [`Mutex`] initialiser with the given name and a newly-created lock class. 8 /// 8 /// 9 /// It uses the name if one is given, otherwis 9 /// It uses the name if one is given, otherwise it generates one based on the file name and line 10 /// number. 10 /// number. 11 #[macro_export] 11 #[macro_export] 12 macro_rules! new_mutex { 12 macro_rules! new_mutex { 13 ($inner:expr $(, $name:literal)? $(,)?) => 13 ($inner:expr $(, $name:literal)? $(,)?) => { 14 $crate::sync::Mutex::new( 14 $crate::sync::Mutex::new( 15 $inner, $crate::optional_name!($($ 15 $inner, $crate::optional_name!($($name)?), $crate::static_lock_class!()) 16 }; 16 }; 17 } 17 } 18 pub use new_mutex; 18 pub use new_mutex; 19 19 20 /// A mutual exclusion primitive. 20 /// A mutual exclusion primitive. 21 /// 21 /// 22 /// Exposes the kernel's [`struct mutex`]. Whe 22 /// Exposes the kernel's [`struct mutex`]. When multiple threads attempt to lock the same mutex, 23 /// only one at a time is allowed to progress, 23 /// only one at a time is allowed to progress, the others will block (sleep) until the mutex is 24 /// unlocked, at which point another thread wi 24 /// unlocked, at which point another thread will be allowed to wake up and make progress. 25 /// 25 /// 26 /// Since it may block, [`Mutex`] needs to be 26 /// Since it may block, [`Mutex`] needs to be used with care in atomic contexts. 27 /// 27 /// 28 /// Instances of [`Mutex`] need a lock class a 28 /// Instances of [`Mutex`] need a lock class and to be pinned. The recommended way to create such 29 /// instances is with the [`pin_init`](crate:: 29 /// instances is with the [`pin_init`](crate::pin_init) and [`new_mutex`] macros. 30 /// 30 /// 31 /// # Examples 31 /// # Examples 32 /// 32 /// 33 /// The following example shows how to declare 33 /// The following example shows how to declare, allocate and initialise a struct (`Example`) that 34 /// contains an inner struct (`Inner`) that is 34 /// contains an inner struct (`Inner`) that is protected by a mutex. 35 /// 35 /// 36 /// ``` 36 /// ``` 37 /// use kernel::sync::{new_mutex, Mutex}; 37 /// use kernel::sync::{new_mutex, Mutex}; 38 /// 38 /// 39 /// struct Inner { 39 /// struct Inner { 40 /// a: u32, 40 /// a: u32, 41 /// b: u32, 41 /// b: u32, 42 /// } 42 /// } 43 /// 43 /// 44 /// #[pin_data] 44 /// #[pin_data] 45 /// struct Example { 45 /// struct Example { 46 /// c: u32, 46 /// c: u32, 47 /// #[pin] 47 /// #[pin] 48 /// d: Mutex<Inner>, 48 /// d: Mutex<Inner>, 49 /// } 49 /// } 50 /// 50 /// 51 /// impl Example { 51 /// impl Example { 52 /// fn new() -> impl PinInit<Self> { 52 /// fn new() -> impl PinInit<Self> { 53 /// pin_init!(Self { 53 /// pin_init!(Self { 54 /// c: 10, 54 /// c: 10, 55 /// d <- new_mutex!(Inner { a: 20, 55 /// d <- new_mutex!(Inner { a: 20, b: 30 }), 56 /// }) 56 /// }) 57 /// } 57 /// } 58 /// } 58 /// } 59 /// 59 /// 60 /// // Allocate a boxed `Example`. 60 /// // Allocate a boxed `Example`. 61 /// let e = Box::pin_init(Example::new(), GFP_ 61 /// let e = Box::pin_init(Example::new(), GFP_KERNEL)?; 62 /// assert_eq!(e.c, 10); 62 /// assert_eq!(e.c, 10); 63 /// assert_eq!(e.d.lock().a, 20); 63 /// assert_eq!(e.d.lock().a, 20); 64 /// assert_eq!(e.d.lock().b, 30); 64 /// assert_eq!(e.d.lock().b, 30); 65 /// # Ok::<(), Error>(()) 65 /// # Ok::<(), Error>(()) 66 /// ``` 66 /// ``` 67 /// 67 /// 68 /// The following example shows how to use int 68 /// The following example shows how to use interior mutability to modify the contents of a struct 69 /// protected by a mutex despite only having a 69 /// protected by a mutex despite only having a shared reference: 70 /// 70 /// 71 /// ``` 71 /// ``` 72 /// use kernel::sync::Mutex; 72 /// use kernel::sync::Mutex; 73 /// 73 /// 74 /// struct Example { 74 /// struct Example { 75 /// a: u32, 75 /// a: u32, 76 /// b: u32, 76 /// b: u32, 77 /// } 77 /// } 78 /// 78 /// 79 /// fn example(m: &Mutex<Example>) { 79 /// fn example(m: &Mutex<Example>) { 80 /// let mut guard = m.lock(); 80 /// let mut guard = m.lock(); 81 /// guard.a += 10; 81 /// guard.a += 10; 82 /// guard.b += 20; 82 /// guard.b += 20; 83 /// } 83 /// } 84 /// ``` 84 /// ``` 85 /// 85 /// 86 /// [`struct mutex`]: srctree/include/linux/mu 86 /// [`struct mutex`]: srctree/include/linux/mutex.h 87 pub type Mutex<T> = super::Lock<T, MutexBacken 87 pub type Mutex<T> = super::Lock<T, MutexBackend>; 88 88 89 /// A kernel `struct mutex` lock backend. 89 /// A kernel `struct mutex` lock backend. 90 pub struct MutexBackend; 90 pub struct MutexBackend; 91 91 92 // SAFETY: The underlying kernel `struct mutex 92 // SAFETY: The underlying kernel `struct mutex` object ensures mutual exclusion. 93 unsafe impl super::Backend for MutexBackend { 93 unsafe impl super::Backend for MutexBackend { 94 type State = bindings::mutex; 94 type State = bindings::mutex; 95 type GuardState = (); 95 type GuardState = (); 96 96 97 unsafe fn init( 97 unsafe fn init( 98 ptr: *mut Self::State, 98 ptr: *mut Self::State, 99 name: *const core::ffi::c_char, 99 name: *const core::ffi::c_char, 100 key: *mut bindings::lock_class_key, 100 key: *mut bindings::lock_class_key, 101 ) { 101 ) { 102 // SAFETY: The safety requirements ens 102 // SAFETY: The safety requirements ensure that `ptr` is valid for writes, and `name` and 103 // `key` are valid for read indefinite 103 // `key` are valid for read indefinitely. 104 unsafe { bindings::__mutex_init(ptr, n 104 unsafe { bindings::__mutex_init(ptr, name, key) } 105 } 105 } 106 106 107 unsafe fn lock(ptr: *mut Self::State) -> S 107 unsafe fn lock(ptr: *mut Self::State) -> Self::GuardState { 108 // SAFETY: The safety requirements of 108 // SAFETY: The safety requirements of this function ensure that `ptr` points to valid 109 // memory, and that it has been initia 109 // memory, and that it has been initialised before. 110 unsafe { bindings::mutex_lock(ptr) }; 110 unsafe { bindings::mutex_lock(ptr) }; 111 } 111 } 112 112 113 unsafe fn unlock(ptr: *mut Self::State, _g 113 unsafe fn unlock(ptr: *mut Self::State, _guard_state: &Self::GuardState) { 114 // SAFETY: The safety requirements of 114 // SAFETY: The safety requirements of this function ensure that `ptr` is valid and that the 115 // caller is the owner of the mutex. 115 // caller is the owner of the mutex. 116 unsafe { bindings::mutex_unlock(ptr) } 116 unsafe { bindings::mutex_unlock(ptr) }; 117 } 117 } 118 } 118 }
Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.