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