Re: [PATCH v16 10/17] rust: sync: Introduce lock::Backend::Context
From: Benno Lossin
Date: Mon Dec 22 2025 - 01:10:59 EST
On Mon Dec 15, 2025 at 6:57 PM CET, Lyude Paul wrote:
> Now that we've introduced an `InterruptDisabled` token for marking
> contexts in which IRQs are disabled, we can have a way to avoid
> `SpinLockIrq` disabling interrupts if the interrupts have already been
> disabled. Basically, a `SpinLockIrq` should work like a `SpinLock` if
> interrupts are disabled. So a function:
>
> (&'a SpinLockIrq, &'a InterruptDisabled) -> Guard<'a, .., SpinLockBackend>
>
> makes senses. Note that due to `Guard` and `InterruptDisabled` having the
> same lifetime, interrupts cannot be enabled while the Guard exists.
>
> Add a `lock_with()` interface for `Lock`, and an associate type of
> `Backend` to describe the context.
This message doesn't mention `Context`, which is included in the title
and the message itself isn't written in imperative style. I'd recommend
putting the `lock_with` in the title, since that is the important part.
> Signed-off-by: Lyude Paul <lyude@xxxxxxxxxx>
> Co-developed-by: Boqun Feng <boqun.feng@xxxxxxxxx>
> Signed-off-by: Boqun Feng <boqun.feng@xxxxxxxxx>
>
> ---
> V10:
> - Fix typos - Dirk
>
> rust/kernel/sync/lock.rs | 12 +++++++++++-
> rust/kernel/sync/lock/mutex.rs | 1 +
> rust/kernel/sync/lock/spinlock.rs | 4 +++-
> 3 files changed, 15 insertions(+), 2 deletions(-)
>
> diff --git a/rust/kernel/sync/lock.rs b/rust/kernel/sync/lock.rs
> index 46a57d1fc309d..bf2d94c1999bd 100644
> --- a/rust/kernel/sync/lock.rs
> +++ b/rust/kernel/sync/lock.rs
> @@ -44,6 +44,9 @@ pub unsafe trait Backend {
> /// [`unlock`]: Backend::unlock
> type GuardState;
>
> + /// The context which can be provided to acquire the lock with a different backend.
> + type Context<'a>;
> +
> /// Initialises the lock.
> ///
> /// # Safety
> @@ -168,8 +171,15 @@ pub unsafe fn from_raw<'a>(ptr: *mut B::State) -> &'a Self {
> }
>
> impl<T: ?Sized, B: Backend> Lock<T, B> {
> + /// Acquires the lock with the given context and gives the caller access to the data protected
> + /// by it.
I feel like this "it" could refer to both the lock and the context, so
it's best to repeat "the lock" here.
> + pub fn lock_with<'a>(&'a self, _context: B::Context<'a>) -> Guard<'a, T, B> {
> + todo!()
> + }
> +
> /// Acquires the lock and gives the caller access to the data protected by it.
> - pub fn lock(&self) -> Guard<'_, T, B> {
> + #[inline]
This change isn't in the message? Is it intended to be here or somewhere
else?
Cheers,
Benno
> + pub fn lock<'a>(&'a self) -> Guard<'a, T, B> {
> // SAFETY: The constructor of the type calls `init`, so the existence of the object proves
> // that `init` was called.
> let state = unsafe { B::lock(self.state.get()) };