162306a36Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0
262306a36Sopenharmony_ci
362306a36Sopenharmony_ci//! A kernel spinlock.
462306a36Sopenharmony_ci//!
562306a36Sopenharmony_ci//! This module allows Rust code to use the kernel's `spinlock_t`.
662306a36Sopenharmony_ci
762306a36Sopenharmony_ciuse crate::bindings;
862306a36Sopenharmony_ci
962306a36Sopenharmony_ci/// Creates a [`SpinLock`] initialiser with the given name and a newly-created lock class.
1062306a36Sopenharmony_ci///
1162306a36Sopenharmony_ci/// It uses the name if one is given, otherwise it generates one based on the file name and line
1262306a36Sopenharmony_ci/// number.
1362306a36Sopenharmony_ci#[macro_export]
1462306a36Sopenharmony_cimacro_rules! new_spinlock {
1562306a36Sopenharmony_ci    ($inner:expr $(, $name:literal)? $(,)?) => {
1662306a36Sopenharmony_ci        $crate::sync::SpinLock::new(
1762306a36Sopenharmony_ci            $inner, $crate::optional_name!($($name)?), $crate::static_lock_class!())
1862306a36Sopenharmony_ci    };
1962306a36Sopenharmony_ci}
2062306a36Sopenharmony_ci
2162306a36Sopenharmony_ci/// A spinlock.
2262306a36Sopenharmony_ci///
2362306a36Sopenharmony_ci/// Exposes the kernel's [`spinlock_t`]. When multiple CPUs attempt to lock the same spinlock, only
2462306a36Sopenharmony_ci/// one at a time is allowed to progress, the others will block (spinning) until the spinlock is
2562306a36Sopenharmony_ci/// unlocked, at which point another CPU will be allowed to make progress.
2662306a36Sopenharmony_ci///
2762306a36Sopenharmony_ci/// Instances of [`SpinLock`] need a lock class and to be pinned. The recommended way to create such
2862306a36Sopenharmony_ci/// instances is with the [`pin_init`](crate::pin_init) and [`new_spinlock`] macros.
2962306a36Sopenharmony_ci///
3062306a36Sopenharmony_ci/// # Examples
3162306a36Sopenharmony_ci///
3262306a36Sopenharmony_ci/// The following example shows how to declare, allocate and initialise a struct (`Example`) that
3362306a36Sopenharmony_ci/// contains an inner struct (`Inner`) that is protected by a spinlock.
3462306a36Sopenharmony_ci///
3562306a36Sopenharmony_ci/// ```
3662306a36Sopenharmony_ci/// use kernel::{init::InPlaceInit, init::PinInit, new_spinlock, pin_init, sync::SpinLock};
3762306a36Sopenharmony_ci///
3862306a36Sopenharmony_ci/// struct Inner {
3962306a36Sopenharmony_ci///     a: u32,
4062306a36Sopenharmony_ci///     b: u32,
4162306a36Sopenharmony_ci/// }
4262306a36Sopenharmony_ci///
4362306a36Sopenharmony_ci/// #[pin_data]
4462306a36Sopenharmony_ci/// struct Example {
4562306a36Sopenharmony_ci///     c: u32,
4662306a36Sopenharmony_ci///     #[pin]
4762306a36Sopenharmony_ci///     d: SpinLock<Inner>,
4862306a36Sopenharmony_ci/// }
4962306a36Sopenharmony_ci///
5062306a36Sopenharmony_ci/// impl Example {
5162306a36Sopenharmony_ci///     fn new() -> impl PinInit<Self> {
5262306a36Sopenharmony_ci///         pin_init!(Self {
5362306a36Sopenharmony_ci///             c: 10,
5462306a36Sopenharmony_ci///             d <- new_spinlock!(Inner { a: 20, b: 30 }),
5562306a36Sopenharmony_ci///         })
5662306a36Sopenharmony_ci///     }
5762306a36Sopenharmony_ci/// }
5862306a36Sopenharmony_ci///
5962306a36Sopenharmony_ci/// // Allocate a boxed `Example`.
6062306a36Sopenharmony_ci/// let e = Box::pin_init(Example::new())?;
6162306a36Sopenharmony_ci/// assert_eq!(e.c, 10);
6262306a36Sopenharmony_ci/// assert_eq!(e.d.lock().a, 20);
6362306a36Sopenharmony_ci/// assert_eq!(e.d.lock().b, 30);
6462306a36Sopenharmony_ci/// # Ok::<(), Error>(())
6562306a36Sopenharmony_ci/// ```
6662306a36Sopenharmony_ci///
6762306a36Sopenharmony_ci/// The following example shows how to use interior mutability to modify the contents of a struct
6862306a36Sopenharmony_ci/// protected by a spinlock despite only having a shared reference:
6962306a36Sopenharmony_ci///
7062306a36Sopenharmony_ci/// ```
7162306a36Sopenharmony_ci/// use kernel::sync::SpinLock;
7262306a36Sopenharmony_ci///
7362306a36Sopenharmony_ci/// struct Example {
7462306a36Sopenharmony_ci///     a: u32,
7562306a36Sopenharmony_ci///     b: u32,
7662306a36Sopenharmony_ci/// }
7762306a36Sopenharmony_ci///
7862306a36Sopenharmony_ci/// fn example(m: &SpinLock<Example>) {
7962306a36Sopenharmony_ci///     let mut guard = m.lock();
8062306a36Sopenharmony_ci///     guard.a += 10;
8162306a36Sopenharmony_ci///     guard.b += 20;
8262306a36Sopenharmony_ci/// }
8362306a36Sopenharmony_ci/// ```
8462306a36Sopenharmony_ci///
8562306a36Sopenharmony_ci/// [`spinlock_t`]: ../../../../include/linux/spinlock.h
8662306a36Sopenharmony_cipub type SpinLock<T> = super::Lock<T, SpinLockBackend>;
8762306a36Sopenharmony_ci
8862306a36Sopenharmony_ci/// A kernel `spinlock_t` lock backend.
8962306a36Sopenharmony_cipub struct SpinLockBackend;
9062306a36Sopenharmony_ci
9162306a36Sopenharmony_ci// SAFETY: The underlying kernel `spinlock_t` object ensures mutual exclusion. `relock` uses the
9262306a36Sopenharmony_ci// default implementation that always calls the same locking method.
9362306a36Sopenharmony_ciunsafe impl super::Backend for SpinLockBackend {
9462306a36Sopenharmony_ci    type State = bindings::spinlock_t;
9562306a36Sopenharmony_ci    type GuardState = ();
9662306a36Sopenharmony_ci
9762306a36Sopenharmony_ci    unsafe fn init(
9862306a36Sopenharmony_ci        ptr: *mut Self::State,
9962306a36Sopenharmony_ci        name: *const core::ffi::c_char,
10062306a36Sopenharmony_ci        key: *mut bindings::lock_class_key,
10162306a36Sopenharmony_ci    ) {
10262306a36Sopenharmony_ci        // SAFETY: The safety requirements ensure that `ptr` is valid for writes, and `name` and
10362306a36Sopenharmony_ci        // `key` are valid for read indefinitely.
10462306a36Sopenharmony_ci        unsafe { bindings::__spin_lock_init(ptr, name, key) }
10562306a36Sopenharmony_ci    }
10662306a36Sopenharmony_ci
10762306a36Sopenharmony_ci    unsafe fn lock(ptr: *mut Self::State) -> Self::GuardState {
10862306a36Sopenharmony_ci        // SAFETY: The safety requirements of this function ensure that `ptr` points to valid
10962306a36Sopenharmony_ci        // memory, and that it has been initialised before.
11062306a36Sopenharmony_ci        unsafe { bindings::spin_lock(ptr) }
11162306a36Sopenharmony_ci    }
11262306a36Sopenharmony_ci
11362306a36Sopenharmony_ci    unsafe fn unlock(ptr: *mut Self::State, _guard_state: &Self::GuardState) {
11462306a36Sopenharmony_ci        // SAFETY: The safety requirements of this function ensure that `ptr` is valid and that the
11562306a36Sopenharmony_ci        // caller is the owner of the mutex.
11662306a36Sopenharmony_ci        unsafe { bindings::spin_unlock(ptr) }
11762306a36Sopenharmony_ci    }
11862306a36Sopenharmony_ci}
119