[<prev] [next>] [<thread-prev] [day] [month] [year] [list]
Message-Id: <DBPC27REX4N1.3JA4SSHDYXAHJ@kernel.org>
Date: Wed, 30 Jul 2025 12:55:18 +0200
From: "Benno Lossin" <lossin@...nel.org>
To: Onur Özkan <work@...rozkan.dev>
Cc: <linux-kernel@...r.kernel.org>, <rust-for-linux@...r.kernel.org>,
<ojeda@...nel.org>, <alex.gaynor@...il.com>, <boqun.feng@...il.com>,
<gary@...yguo.net>, <a.hindborg@...nel.org>, <aliceryhl@...gle.com>,
<tmgross@...ch.edu>, <dakr@...nel.org>, <peterz@...radead.org>,
<mingo@...hat.com>, <will@...nel.org>, <longman@...hat.com>,
<felipe_life@...e.com>, <daniel@...lak.dev>, <bjorn3_gh@...tonmail.com>,
"Lyude" <thatslyude@...il.com>
Subject: Re: [PATCH v5 0/3] rust: add `ww_mutex` support
On Wed Jul 30, 2025 at 12:24 PM CEST, Onur Özkan wrote:
> On Tue, 29 Jul 2025 19:15:12 +0200
> "Benno Lossin" <lossin@...nel.org> wrote:
>
>> > - The second note is about how EDEADLK is handled. On the C side, it
>> > looks like some code paths may not release all the previously locked
>> > mutexes or have a special/custom logic when locking returns EDEADLK
>> > (see [3]). So, handling EDEADLK automatically (pointed
>> > in [1]) can be quite useful for most cases, but that could also be a
>> > limitation in certain scenarios.
>> >
>> > I was thinking we could provide an alternative version of each
>> > `lock*` function that accepts a closure which is called on the
>> > EDEADLK error. This way, we can support both auto-release locks and
>> > custom logic for handling EDEADLK scenarios.
>> >
>> > Something like this (just a dummy code for demonstration):
>> >
>> > ctx.lock_and_handle_edeadlk(|active_locks| {
>> > // user-defined handling here
>> > });
>>
>> But this function wouldn't be locking any additional locks, right?
>>
>> I think the closure makes sense to give as a way to allow custom code.
>> But we definitely should try to get the common use-cases closure-free
>> (except of course they run completely custom code to their specific
>> use-case).
>>
>> We can also try to invent a custom return type that is used instead of
>> `Result`. So for example:
>>
>> let a: WwMutex<'_, A>;
>> let b: WwMutex<'_, B>;
>> let ctx: WwAcquireCtx<'_>;
>>
>> ctx.enter() // EnteredContext<'_, ()>
>> .lock(a) // LockAttempt<'_, A, ()>
>> .or_err(a)? // EnteredContext<'_, (A,)>
>> .lock(b) // LockAttempt<'_, B, (A,)>
>> .or_lock_slow(a, b) // Result<EnteredContext<'_, (A, B,)>>
>> ?.finish() // (WwMutexGuard<'_, A>, WwMutexGuard<'_,
>> B>)
>>
>> But no idea if this is actually useful...
>
> That wouldn't work if the user wants to lock `a` and `b` in separate
> calls, right? If user wants to lock `a` right away and lock `b` later
> under certain conditions (still in the same context as `a`), then to
> automatically release `a`, we have to keep the locked mutexes in some
> dynamic list inside `EnteredContext` so we can access all the locked
> mutexes when we want to unlock them on EDEADLK.
Not sure I understand, you can write:
let a: WwMutex<'_, A>;
let b: WwMutex<'_, B>;
let ctx: WwAcquireCtx<'_>;
let lock_ctx = ctx.enter()
.lock(a)
.or_err(a)?;
if !cond() {
return ...;
}
lock_ctx.lock(b)
.or_lock_slow(a, b)?
.finish()
>> What I think would be a good way forward would be to convert some
>> existing C uses of `WwMutex` to the intended Rust API and see how it
>> looks. Best to cover several different kinds of uses.
>
> Good idea. I will try find sometime to do that during next week.
Thanks!
---
Cheers,
Benno
Powered by blists - more mailing lists