kernel/sync.rs
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
8use crate::prelude::*;
9use crate::types::Opaque;
10use pin_init;
11
12mod arc;
13pub mod aref;
14pub mod atomic;
15pub mod barrier;
16pub mod completion;
17mod condvar;
18pub mod lock;
19mod locked_by;
20pub mod poll;
21pub mod rcu;
22mod refcount;
23mod set_once;
24
25pub use arc::{Arc, ArcBorrow, UniqueArc};
26pub use completion::Completion;
27pub use condvar::{new_condvar, CondVar, CondVarTimeoutResult};
28pub use lock::global::{global_lock, GlobalGuard, GlobalLock, GlobalLockBackend, GlobalLockedBy};
29pub use lock::mutex::{new_mutex, Mutex, MutexGuard};
30pub use lock::spinlock::{new_spinlock, SpinLock, SpinLockGuard};
31pub use locked_by::LockedBy;
32pub use refcount::Refcount;
33pub 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)]
38pub 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.
45unsafe impl Sync for LockClassKey {}
46
47impl 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 /// ```
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
84 pub(crate) fn as_ptr(&self) -> *mut bindings::lock_class_key {
85 self.inner.get()
86 }
87}
88
89#[pinned_drop]
90impl PinnedDrop for LockClassKey {
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]
101macro_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]
118macro_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}