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