[PATCH v13 13/17] rust: sync: Add a lifetime parameter to lock::global::GlobalGuard

Lyude Paul
2025-10-13T15:54:54Z

While a GlobalLock is always going to be static, in the case of locks with
explicit backend contexts the GlobalGuard will not be 'static and will
instead share the lifetime of the context. So, add a lifetime parameter to
GlobalGuard to allow for this so we can implement GlobalGuard support for
SpinlockIrq.

Signed-off-by: Lyude Paul <lyude@redhat.com>
---
rust/kernel/sync/lock/global.rs | 18 +++++++++---------
1 file changed, 9 insertions(+), 9 deletions(-)

diff --git a/rust/kernel/sync/lock/global.rs b/rust/kernel/sync/lock/global.rs
index dfdd913d6c1f5..f846ecf9168e7 100644
--- a/rust/kernel/sync/lock/global.rs
+++ b/rust/kernel/sync/lock/global.rs
@@ -77,14 +77,14 @@ pub unsafe fn init(&'static self) {
}

/// Lock this global lock.
- pub fn lock(&'static self) -> GlobalGuard<G> {
+ pub fn lock(&'static self) -> GlobalGuard<'static, G> {
GlobalGuard {
inner: self.inner.lock(),
}
}

/// Try to lock this global lock.
- pub fn try_lock(&'static self) -> Option<GlobalGuard<G>> {
+ pub fn try_lock(&'static self) -> Option<GlobalGuard<'static, G>> {
Some(GlobalGuard {
inner: self.inner.try_lock()?,
})
@@ -94,11 +94,11 @@ pub fn try_lock(&'static self) -> Option<GlobalGuard<G>> {
/// A guard for a [`GlobalLock`].
///
/// See [`global_lock!`] for examples.
-pub struct GlobalGuard<G: GlobalLockBackend> {
- inner: Guard<'static, G::Item, G::Backend>,
+pub struct GlobalGuard<'a, G: GlobalLockBackend> {
+ inner: Guard<'a, G::Item, G::Backend>,
}

-impl<G: GlobalLockBackend> core::ops::Deref for GlobalGuard<G> {
+impl<'a, G: GlobalLockBackend> core::ops::Deref for GlobalGuard<'a, G> {
type Target = G::Item;

fn deref(&self) -> &Self::Target {
@@ -106,7 +106,7 @@ fn deref(&self) -> &Self::Target {
}
}

-impl<G: GlobalLockBackend> core::ops::DerefMut for GlobalGuard<G> {
+impl<'a, G: GlobalLockBackend> core::ops::DerefMut for GlobalGuard<'a, G> {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.inner
}
@@ -154,7 +154,7 @@ impl<T: ?Sized, G: GlobalLockBackend> GlobalLockedBy<T, G> {
/// Access the value immutably.
///
/// The caller must prove shared access to the lock.
- pub fn as_ref<'a>(&'a self, _guard: &'a GlobalGuard<G>) -> &'a T {
+ pub fn as_ref<'a>(&'a self, _guard: &'a GlobalGuard<'_, G>) -> &'a T {
// SAFETY: The lock is globally unique, so there can only be one guard.
unsafe { &*self.value.get() }
}
@@ -162,7 +162,7 @@ pub fn as_ref<'a>(&'a self, _guard: &'a GlobalGuard<G>) -> &'a T {
/// Access the value mutably.
///
/// The caller must prove shared exclusive to the lock.
- pub fn as_mut<'a>(&'a self, _guard: &'a mut GlobalGuard<G>) -> &'a mut T {
+ pub fn as_mut<'a>(&'a self, _guard: &'a mut GlobalGuard<'_, G>) -> &'a mut T {
// SAFETY: The lock is globally unique, so there can only be one guard.
unsafe { &mut *self.value.get() }
}
@@ -232,7 +232,7 @@ pub fn get_mut(&mut self) -> &mut T {
/// /// Increment the counter in this instance.
/// ///
/// /// The caller must hold the `MY_MUTEX` mutex.
-/// fn increment(&self, guard: &mut GlobalGuard<MY_MUTEX>) -> u32 {
+/// fn increment(&self, guard: &mut GlobalGuard<'_, MY_MUTEX>) -> u32 {
/// let my_counter = self.my_counter.as_mut(guard);
/// *my_counter += 1;
/// *my_counter
--
2.51.0