Re: [PATCH v12 5/6] rust: ww_mutex: implement LockSet

From: Gary Guo

Date: Thu Oct 15 2026 - 10:09:43 EST


On Thu Oct 15, 2026 at 1:26 PM BST, Onur Özkan wrote:
> On Thu, 15 Oct 2026 12:53:10 +0100
> Gary Guo <gary@xxxxxxxxxxx> wrote:
>
>> If you do not need the "acquire the contended lock first" behaviour, then
>> there's no need for wrapper, even. Also, if this behaviour is truly universal,
>> perhaps it should become part of `AcquireCtx`'s default behaviour instead.
>
> We need that functionality for the DRM use-case, it's not something that should
> be applied to the ww mutex primitives but I would like to hear @Daniel's opinion
> on this.
>
> [snip]
>
> This still needs the contended-lock backoff when retrying the full transaction.
> Simply restarting the callback can repeatedly hit EDEADLK, so the proposed code
> is not going to work.

WW mutex is fair so the transaction will progress eventually when it becomes the
oldest transaction. I think acquring the contended lock first is basically just
a protection against pathological cases where say, old txn and new txn acquires
lock in opposite order, than new txn would have to wound repeatedly as old txn
make its way through..

But regardless I think this behaviour can just become a thin wrapper on
`AcquireCtx` like my earlier email. Something like

#[pin_data]
struct AcquireCtxBackoff<'a> {
#[pin]
ctx: AcquireCtx,
// Set by `lock()` on `EDEADLK`; the mutex remains valid for `'a`.
contended: Option<NonNull<bindings::ww_mutex>>,
contend_acquired: bool,
}

impl<'a> AcquireCtxBackOff<'a> {
fn lock(&mut self, m: &'a Mutex<T>) -> Result<MutexGuard<'_, T>> {
if let Some(contended) = self.contended {
if !self.contended_acquired {
lock contended
}

if m.as_ptr() == contended.as_ptr() {
self.contended = None;
self.contended_acquired = false;
return a guard
}
}

match ... {
Ok(...) => ...,
Err(EDEADLK) => {
if self.contended_acquired {
unlock contended
}
self.contended = Some(m.as_ptr());
Err(EDEADLK)?
}
Err(err) => Err(err)?
}
}
}

Then code that want backoff behaviour can just use this ctx.

Best,
Gary