xref: /linux/rust/kernel/sync.rs (revision f4915933947c71f08ed1c5a6c9b4fdbe735e18cf)
1 // SPDX-License-Identifier: GPL-2.0
2 
3 //! Synchronisation primitives.
4 //!
5 //! This module contains the kernel APIs related to synchronisation that have been ported or
6 //! wrapped for usage by Rust code in the kernel.
7 
8 use crate::pin_init;
9 use crate::prelude::*;
10 use crate::types::Opaque;
11 
12 mod arc;
13 mod condvar;
14 pub mod lock;
15 mod locked_by;
16 pub mod poll;
17 pub mod rcu;
18 
19 pub use arc::{Arc, ArcBorrow, UniqueArc};
20 pub use condvar::{new_condvar, CondVar, CondVarTimeoutResult};
21 pub use lock::global::{global_lock, GlobalGuard, GlobalLock, GlobalLockBackend, GlobalLockedBy};
22 pub use lock::mutex::{new_mutex, Mutex, MutexGuard};
23 pub use lock::spinlock::{new_spinlock, SpinLock, SpinLockGuard};
24 pub use locked_by::LockedBy;
25 
26 /// Represents a lockdep class. It's a wrapper around C's `lock_class_key`.
27 #[repr(transparent)]
28 #[pin_data(PinnedDrop)]
29 pub struct LockClassKey {
30     #[pin]
31     inner: Opaque<bindings::lock_class_key>,
32 }
33 
34 // SAFETY: `bindings::lock_class_key` is designed to be used concurrently from multiple threads and
35 // provides its own synchronization.
36 unsafe impl Sync for LockClassKey {}
37 
38 impl LockClassKey {
39     /// Initializes a dynamically allocated lock class key. In the common case of using a
40     /// statically allocated lock class key, the static_lock_class! macro should be used instead.
41     ///
42     /// # Example
43     /// ```
44     /// # use kernel::{c_str, stack_pin_init};
45     /// # use kernel::alloc::KBox;
46     /// # use kernel::types::ForeignOwnable;
47     /// # use kernel::sync::{LockClassKey, SpinLock};
48     ///
49     /// let key = KBox::pin_init(LockClassKey::new_dynamic(), GFP_KERNEL)?;
50     /// let key_ptr = key.into_foreign();
51     ///
52     /// {
53     ///     stack_pin_init!(let num: SpinLock<u32> = SpinLock::new(
54     ///         0,
55     ///         c_str!("my_spinlock"),
56     ///         // SAFETY: `key_ptr` is returned by the above `into_foreign()`, whose
57     ///         // `from_foreign()` has not yet been called.
58     ///         unsafe { <Pin<KBox<LockClassKey>> as ForeignOwnable>::borrow(key_ptr) }
59     ///     ));
60     /// }
61     ///
62     /// // SAFETY: We dropped `num`, the only use of the key, so the result of the previous
63     /// // `borrow` has also been dropped. Thus, it's safe to use from_foreign.
64     /// unsafe { drop(<Pin<KBox<LockClassKey>> as ForeignOwnable>::from_foreign(key_ptr)) };
65     ///
66     /// # Ok::<(), Error>(())
67     /// ```
new_dynamic() -> impl PinInit<Self>68     pub fn new_dynamic() -> impl PinInit<Self> {
69         pin_init!(Self {
70             // SAFETY: lockdep_register_key expects an uninitialized block of memory
71             inner <- Opaque::ffi_init(|slot| unsafe { bindings::lockdep_register_key(slot) })
72         })
73     }
74 
as_ptr(&self) -> *mut bindings::lock_class_key75     pub(crate) fn as_ptr(&self) -> *mut bindings::lock_class_key {
76         self.inner.get()
77     }
78 }
79 
80 #[pinned_drop]
81 impl PinnedDrop for LockClassKey {
drop(self: Pin<&mut Self>)82     fn drop(self: Pin<&mut Self>) {
83         // SAFETY: self.as_ptr was registered with lockdep and self is pinned, so the address
84         // hasn't changed. Thus, it's safe to pass to unregister.
85         unsafe { bindings::lockdep_unregister_key(self.as_ptr()) }
86     }
87 }
88 
89 /// Defines a new static lock class and returns a pointer to it.
90 #[doc(hidden)]
91 #[macro_export]
92 macro_rules! static_lock_class {
93     () => {{
94         static CLASS: $crate::sync::LockClassKey =
95             // SAFETY: lockdep expects uninitialized memory when it's handed a statically allocated
96             // lock_class_key
97             unsafe { ::core::mem::MaybeUninit::uninit().assume_init() };
98         $crate::prelude::Pin::static_ref(&CLASS)
99     }};
100 }
101 
102 /// Returns the given string, if one is provided, otherwise generates one based on the source code
103 /// location.
104 #[doc(hidden)]
105 #[macro_export]
106 macro_rules! optional_name {
107     () => {
108         $crate::c_str!(::core::concat!(::core::file!(), ":", ::core::line!()))
109     };
110     ($name:literal) => {
111         $crate::c_str!($name)
112     };
113 }
114