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