Re: [PATCH v12 5/6] rust: ww_mutex: implement LockSet
From: Onur Özkan
Date: Thu Oct 15 2026 - 07:31:46 EST
On Mon, 12 Oct 2026 13:02:15 +0100
Gary Guo <gary@xxxxxxxxxxx> wrote:
> On Mon Oct 12, 2026 at 11:26 AM BST, Onur Özkan wrote:
> > LockSet is a high-level and safe API built on top of
> > ww_mutex which provides a safe and easy to use API
> > while keeping the ww_mutex semantics.
> >
> > When EDEADLK is hit it drops all held locks and waits for the
> > contended mutex. It then retries the given (by the user)
> > locking algorithm with that mutex held and the same acquire
> > context until it succeeds.
>
> Do we really need this, or this can be achieved by simple retry loops?
>
> fn retry_if_deadlock<R>(f: impl FnOnce() -> Result<R>) -> Result<R> {
> loop {
> match f() {
> Err(EDEADLK) => (),
> e => return e,
> }
> }
> }
>
> stack_pin_init!(let acq_ctx = AcquireCtx::new(&class));
> retry_if_deadlock(|| {
> let mut gaurd1 = acq_ctx.lock(&mutex1)?;
> let mut guard2 = acq_ctx.lock(&mutex2)?;
>
> acq_ctx.done();
>
> *guard1 += 1;
> *guard2 += 1;
> });
>
> I think the only difference is that the `LockSet` acquires the contending mutex
> first. However, I imagine we can solve this by having a wrapped `AcquireCtx`
> that provides that.
That's what LockSet already is. How would you design that wrapped AcquireCtx and
what would we gain by replacing LockSet with another wrapper?
You need to remember the contended mutex, release all acquired locks before
waiting for it and retry with that mutex held and the same acquire context. Also
for a dynamic set, you also need to keep the acquired locks held across loop
iterations and helper calls and handle repeated objects. With ordinary guards,
that bookkeeping falls on the caller unless the wrapper handles it. LockSet
already does all this.
Thanks,
Onur
>
> Best,
> Gary
>
> >
> > Signed-off-by: Onur Özkan <work@xxxxxxxxxxxxx>
> > ---
> > rust/kernel/sync/lock/ww_mutex.rs | 6 +
> > rust/kernel/sync/lock/ww_mutex/lock_set.rs | 449 +++++++++++++++++++++
> > 2 files changed, 455 insertions(+)
> > create mode 100644 rust/kernel/sync/lock/ww_mutex/lock_set.rs
>