xref: /linux/rust/kernel/sync/lock/spinlock.rs (revision 00280272)
1c6d917a4SWedson Almeida Filho // SPDX-License-Identifier: GPL-2.0
2c6d917a4SWedson Almeida Filho 
3c6d917a4SWedson Almeida Filho //! A kernel spinlock.
4c6d917a4SWedson Almeida Filho //!
5c6d917a4SWedson Almeida Filho //! This module allows Rust code to use the kernel's `spinlock_t`.
6c6d917a4SWedson Almeida Filho 
7c6d917a4SWedson Almeida Filho /// Creates a [`SpinLock`] initialiser with the given name and a newly-created lock class.
8c6d917a4SWedson Almeida Filho ///
9c6d917a4SWedson Almeida Filho /// It uses the name if one is given, otherwise it generates one based on the file name and line
10c6d917a4SWedson Almeida Filho /// number.
11c6d917a4SWedson Almeida Filho #[macro_export]
12c6d917a4SWedson Almeida Filho macro_rules! new_spinlock {
13c6d917a4SWedson Almeida Filho     ($inner:expr $(, $name:literal)? $(,)?) => {
14c6d917a4SWedson Almeida Filho         $crate::sync::SpinLock::new(
15c6d917a4SWedson Almeida Filho             $inner, $crate::optional_name!($($name)?), $crate::static_lock_class!())
16c6d917a4SWedson Almeida Filho     };
17c6d917a4SWedson Almeida Filho }
18e283ee23SAlice Ryhl pub use new_spinlock;
19c6d917a4SWedson Almeida Filho 
20c6d917a4SWedson Almeida Filho /// A spinlock.
21c6d917a4SWedson Almeida Filho ///
22c6d917a4SWedson Almeida Filho /// Exposes the kernel's [`spinlock_t`]. When multiple CPUs attempt to lock the same spinlock, only
23c6d917a4SWedson Almeida Filho /// one at a time is allowed to progress, the others will block (spinning) until the spinlock is
24c6d917a4SWedson Almeida Filho /// unlocked, at which point another CPU will be allowed to make progress.
25c6d917a4SWedson Almeida Filho ///
26c6d917a4SWedson Almeida Filho /// Instances of [`SpinLock`] need a lock class and to be pinned. The recommended way to create such
27c6d917a4SWedson Almeida Filho /// instances is with the [`pin_init`](crate::pin_init) and [`new_spinlock`] macros.
28c6d917a4SWedson Almeida Filho ///
29c6d917a4SWedson Almeida Filho /// # Examples
30c6d917a4SWedson Almeida Filho ///
31c6d917a4SWedson Almeida Filho /// The following example shows how to declare, allocate and initialise a struct (`Example`) that
32c6d917a4SWedson Almeida Filho /// contains an inner struct (`Inner`) that is protected by a spinlock.
33c6d917a4SWedson Almeida Filho ///
34c6d917a4SWedson Almeida Filho /// ```
35e283ee23SAlice Ryhl /// use kernel::sync::{new_spinlock, SpinLock};
36c6d917a4SWedson Almeida Filho ///
37c6d917a4SWedson Almeida Filho /// struct Inner {
38c6d917a4SWedson Almeida Filho ///     a: u32,
39c6d917a4SWedson Almeida Filho ///     b: u32,
40c6d917a4SWedson Almeida Filho /// }
41c6d917a4SWedson Almeida Filho ///
42c6d917a4SWedson Almeida Filho /// #[pin_data]
43c6d917a4SWedson Almeida Filho /// struct Example {
44c6d917a4SWedson Almeida Filho ///     c: u32,
45c6d917a4SWedson Almeida Filho ///     #[pin]
46c6d917a4SWedson Almeida Filho ///     d: SpinLock<Inner>,
47c6d917a4SWedson Almeida Filho /// }
48c6d917a4SWedson Almeida Filho ///
49c6d917a4SWedson Almeida Filho /// impl Example {
50c6d917a4SWedson Almeida Filho ///     fn new() -> impl PinInit<Self> {
51c6d917a4SWedson Almeida Filho ///         pin_init!(Self {
52c6d917a4SWedson Almeida Filho ///             c: 10,
53c6d917a4SWedson Almeida Filho ///             d <- new_spinlock!(Inner { a: 20, b: 30 }),
54c6d917a4SWedson Almeida Filho ///         })
55c6d917a4SWedson Almeida Filho ///     }
56c6d917a4SWedson Almeida Filho /// }
57c6d917a4SWedson Almeida Filho ///
58c6d917a4SWedson Almeida Filho /// // Allocate a boxed `Example`.
59*c34aa00dSWedson Almeida Filho /// let e = Box::pin_init(Example::new(), GFP_KERNEL)?;
60c6d917a4SWedson Almeida Filho /// assert_eq!(e.c, 10);
61c6d917a4SWedson Almeida Filho /// assert_eq!(e.d.lock().a, 20);
62c6d917a4SWedson Almeida Filho /// assert_eq!(e.d.lock().b, 30);
63bfa7dff0SMiguel Ojeda /// # Ok::<(), Error>(())
64c6d917a4SWedson Almeida Filho /// ```
65c6d917a4SWedson Almeida Filho ///
66c6d917a4SWedson Almeida Filho /// The following example shows how to use interior mutability to modify the contents of a struct
67c6d917a4SWedson Almeida Filho /// protected by a spinlock despite only having a shared reference:
68c6d917a4SWedson Almeida Filho ///
69c6d917a4SWedson Almeida Filho /// ```
70c6d917a4SWedson Almeida Filho /// use kernel::sync::SpinLock;
71c6d917a4SWedson Almeida Filho ///
72c6d917a4SWedson Almeida Filho /// struct Example {
73c6d917a4SWedson Almeida Filho ///     a: u32,
74c6d917a4SWedson Almeida Filho ///     b: u32,
75c6d917a4SWedson Almeida Filho /// }
76c6d917a4SWedson Almeida Filho ///
77c6d917a4SWedson Almeida Filho /// fn example(m: &SpinLock<Example>) {
78c6d917a4SWedson Almeida Filho ///     let mut guard = m.lock();
79c6d917a4SWedson Almeida Filho ///     guard.a += 10;
80c6d917a4SWedson Almeida Filho ///     guard.b += 20;
81c6d917a4SWedson Almeida Filho /// }
82c6d917a4SWedson Almeida Filho /// ```
83c6d917a4SWedson Almeida Filho ///
84bc2e7d5cSMiguel Ojeda /// [`spinlock_t`]: srctree/include/linux/spinlock.h
85c6d917a4SWedson Almeida Filho pub type SpinLock<T> = super::Lock<T, SpinLockBackend>;
86c6d917a4SWedson Almeida Filho 
87c6d917a4SWedson Almeida Filho /// A kernel `spinlock_t` lock backend.
88c6d917a4SWedson Almeida Filho pub struct SpinLockBackend;
89c6d917a4SWedson Almeida Filho 
90e32cca32SWedson Almeida Filho // SAFETY: The underlying kernel `spinlock_t` object ensures mutual exclusion. `relock` uses the
91e32cca32SWedson Almeida Filho // default implementation that always calls the same locking method.
92c6d917a4SWedson Almeida Filho unsafe impl super::Backend for SpinLockBackend {
93c6d917a4SWedson Almeida Filho     type State = bindings::spinlock_t;
94c6d917a4SWedson Almeida Filho     type GuardState = ();
95c6d917a4SWedson Almeida Filho 
init( ptr: *mut Self::State, name: *const core::ffi::c_char, key: *mut bindings::lock_class_key, )96c6d917a4SWedson Almeida Filho     unsafe fn init(
97c6d917a4SWedson Almeida Filho         ptr: *mut Self::State,
98c6d917a4SWedson Almeida Filho         name: *const core::ffi::c_char,
99c6d917a4SWedson Almeida Filho         key: *mut bindings::lock_class_key,
100c6d917a4SWedson Almeida Filho     ) {
101c6d917a4SWedson Almeida Filho         // SAFETY: The safety requirements ensure that `ptr` is valid for writes, and `name` and
102c6d917a4SWedson Almeida Filho         // `key` are valid for read indefinitely.
103c6d917a4SWedson Almeida Filho         unsafe { bindings::__spin_lock_init(ptr, name, key) }
104c6d917a4SWedson Almeida Filho     }
105c6d917a4SWedson Almeida Filho 
lock(ptr: *mut Self::State) -> Self::GuardState106c6d917a4SWedson Almeida Filho     unsafe fn lock(ptr: *mut Self::State) -> Self::GuardState {
107c6d917a4SWedson Almeida Filho         // SAFETY: The safety requirements of this function ensure that `ptr` points to valid
108c6d917a4SWedson Almeida Filho         // memory, and that it has been initialised before.
109c6d917a4SWedson Almeida Filho         unsafe { bindings::spin_lock(ptr) }
110c6d917a4SWedson Almeida Filho     }
111c6d917a4SWedson Almeida Filho 
unlock(ptr: *mut Self::State, _guard_state: &Self::GuardState)112c6d917a4SWedson Almeida Filho     unsafe fn unlock(ptr: *mut Self::State, _guard_state: &Self::GuardState) {
113c6d917a4SWedson Almeida Filho         // SAFETY: The safety requirements of this function ensure that `ptr` is valid and that the
114b6cda913SValentin Obst         // caller is the owner of the spinlock.
115c6d917a4SWedson Almeida Filho         unsafe { bindings::spin_unlock(ptr) }
116c6d917a4SWedson Almeida Filho     }
117c6d917a4SWedson Almeida Filho }
118