|
| 1 | +// SPDX-License-Identifier: GPL-2.0 |
| 2 | + |
| 3 | +//! A kernel spinlock. |
| 4 | +//! |
| 5 | +//! This module allows Rust code to use the kernel's [`struct spinlock`]. |
| 6 | +//! |
| 7 | +//! See <https://www.kernel.org/doc/Documentation/locking/spinlocks.txt>. |
| 8 | +
|
| 9 | +use super::{Guard, Lock, NeedsLockClass}; |
| 10 | +use crate::{bindings, c_types, CStr}; |
| 11 | +use core::{cell::UnsafeCell, marker::PhantomPinned, pin::Pin}; |
| 12 | + |
| 13 | +extern "C" { |
| 14 | + #[allow(improper_ctypes)] |
| 15 | + fn rust_helper_spin_lock_init( |
| 16 | + lock: *mut bindings::spinlock_t, |
| 17 | + name: *const c_types::c_char, |
| 18 | + key: *mut bindings::lock_class_key, |
| 19 | + ); |
| 20 | + fn rust_helper_spin_lock(lock: *mut bindings::spinlock); |
| 21 | + fn rust_helper_spin_unlock(lock: *mut bindings::spinlock); |
| 22 | +} |
| 23 | + |
| 24 | +/// Safely initialises a [`SpinLock`] with the given name, generating a new lock class. |
| 25 | +#[macro_export] |
| 26 | +macro_rules! spinlock_init { |
| 27 | + ($spinlock:expr, $name:literal) => { |
| 28 | + $crate::init_with_lockdep!($spinlock, $name) |
| 29 | + }; |
| 30 | +} |
| 31 | + |
| 32 | +/// Exposes the kernel's [`spinlock_t`]. When multiple CPUs attempt to lock the same spinlock, only |
| 33 | +/// one at a time is allowed to progress, the others will block (spinning) until the spinlock is |
| 34 | +/// unlocked, at which point another CPU will be allowed to make progress. |
| 35 | +/// |
| 36 | +/// A [`SpinLock`] must first be initialised with a call to [`SpinLock::init`] before it can be |
| 37 | +/// used. The [`spinlock_init`] macro is provided to automatically assign a new lock class to a |
| 38 | +/// spinlock instance. |
| 39 | +/// |
| 40 | +/// [`SpinLock`] does not manage the interrupt state, so it can be used in only two cases: (a) when |
| 41 | +/// the caller knows that interrupts are disabled, or (b) when callers never use it in interrupt |
| 42 | +/// handlers (in which case it is ok for interrupts to be enabled). |
| 43 | +/// |
| 44 | +/// [`spinlock_t`]: ../../../include/linux/spinlock.h |
| 45 | +pub struct SpinLock<T: ?Sized> { |
| 46 | + spin_lock: UnsafeCell<bindings::spinlock>, |
| 47 | + |
| 48 | + /// Spinlocks are architecture-defined. So we conservatively require them to be pinned in case |
| 49 | + /// some architecture uses self-references now or in the future. |
| 50 | + _pin: PhantomPinned, |
| 51 | + |
| 52 | + data: UnsafeCell<T>, |
| 53 | +} |
| 54 | + |
| 55 | +// SAFETY: `SpinLock` can be transferred across thread boundaries iff the data it protects can. |
| 56 | +unsafe impl<T: ?Sized + Send> Send for SpinLock<T> {} |
| 57 | + |
| 58 | +// SAFETY: `SpinLock` serialises the interior mutability it provides, so it is `Sync` as long as the |
| 59 | +// data it protects is `Send`. |
| 60 | +unsafe impl<T: ?Sized + Send> Sync for SpinLock<T> {} |
| 61 | + |
| 62 | +impl<T> SpinLock<T> { |
| 63 | + /// Constructs a new spinlock. |
| 64 | + /// |
| 65 | + /// # Safety |
| 66 | + /// |
| 67 | + /// The caller must call [`SpinLock::init`] before using the spinlock. |
| 68 | + pub unsafe fn new(t: T) -> Self { |
| 69 | + Self { |
| 70 | + spin_lock: UnsafeCell::new(bindings::spinlock::default()), |
| 71 | + data: UnsafeCell::new(t), |
| 72 | + _pin: PhantomPinned, |
| 73 | + } |
| 74 | + } |
| 75 | +} |
| 76 | + |
| 77 | +impl<T: ?Sized> SpinLock<T> { |
| 78 | + /// Locks the spinlock and gives the caller access to the data protected by it. Only one thread |
| 79 | + /// at a time is allowed to access the protected data. |
| 80 | + pub fn lock(&self) -> Guard<Self> { |
| 81 | + self.lock_noguard(); |
| 82 | + // SAFETY: The spinlock was just acquired. |
| 83 | + unsafe { Guard::new(self) } |
| 84 | + } |
| 85 | +} |
| 86 | + |
| 87 | +impl<T: ?Sized> NeedsLockClass for SpinLock<T> { |
| 88 | + unsafe fn init(self: Pin<&Self>, name: CStr<'static>, key: *mut bindings::lock_class_key) { |
| 89 | + rust_helper_spin_lock_init(self.spin_lock.get(), name.as_ptr() as _, key); |
| 90 | + } |
| 91 | +} |
| 92 | + |
| 93 | +impl<T: ?Sized> Lock for SpinLock<T> { |
| 94 | + type Inner = T; |
| 95 | + |
| 96 | + fn lock_noguard(&self) { |
| 97 | + // SAFETY: `spin_lock` points to valid memory. |
| 98 | + unsafe { rust_helper_spin_lock(self.spin_lock.get()) }; |
| 99 | + } |
| 100 | + |
| 101 | + unsafe fn unlock(&self) { |
| 102 | + rust_helper_spin_unlock(self.spin_lock.get()); |
| 103 | + } |
| 104 | + |
| 105 | + unsafe fn locked_data(&self) -> &UnsafeCell<T> { |
| 106 | + &self.data |
| 107 | + } |
| 108 | +} |
0 commit comments