Re: [PATCH 4/4] rust: sync: lock: use DropGuard

From: Boqun Feng

Date: Mon Sep 28 2026 - 05:20:36 EST


On Sun, Sep 27, 2026 at 01:45:28PM +0300, Mohamed Osama wrote:
> Replace the ScopeGuard usage in the Rust locking implementation with
> DropGuard.
>
> The guard preserves the existing scope-exit cleanup behavior while using
> the DropGuard API from the Rust kernel memory module.
>
> Signed-off-by: Mohamed Osama <mohamed.osama189110@xxxxxxxxx>

Reviewed-by: Boqun Feng <boqun@xxxxxxxxxx>

Regards,
Boqun

> ---
> rust/kernel/sync/lock.rs | 5 +++--
> 1 file changed, 3 insertions(+), 2 deletions(-)
>
> diff --git a/rust/kernel/sync/lock.rs b/rust/kernel/sync/lock.rs
> index 10b6b5e9b024..15f9cbe76c8d 100644
> --- a/rust/kernel/sync/lock.rs
> +++ b/rust/kernel/sync/lock.rs
> @@ -7,8 +7,9 @@
>
> use super::LockClassKey;
> use crate::{
> + mem::DropGuard,
> str::{CStr, CStrExt as _},
> - types::{NotThreadSafe, Opaque, ScopeGuard},
> + types::{NotThreadSafe, Opaque},
> };
> use core::{cell::UnsafeCell, marker::PhantomPinned, pin::Pin};
> use pin_init::{pin_data, pin_init, PinInit, Wrapper};
> @@ -242,7 +243,7 @@ pub(crate) fn do_unlocked<U>(&mut self, cb: impl FnOnce() -> U) -> U {
> // SAFETY: The caller owns the lock, so it is safe to unlock it.
> unsafe { B::unlock(self.lock.state.get(), &self.state) };
>
> - let _relock = ScopeGuard::new(||
> + let _relock = DropGuard::new((), |_|
> // SAFETY: The lock was just unlocked above and is being relocked now.
> unsafe { B::relock(self.lock.state.get(), &mut self.state) });
>
> --
> 2.43.0
>