From: Onur <work@onurozkan.dev>
To: Daniel Almeida <daniel.almeida@collabora.com>
Cc: rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org,
lossin@kernel.org, lyude@redhat.com, ojeda@kernel.org,
alex.gaynor@gmail.com, boqun.feng@gmail.com, gary@garyguo.net,
a.hindborg@kernel.org, aliceryhl@google.com, tmgross@umich.edu,
dakr@kernel.org, peterz@infradead.org, mingo@redhat.com,
will@kernel.org, longman@redhat.com, felipe_life@live.com,
daniel@sedlak.dev, bjorn3_gh@protonmail.com
Subject: Re: [PATCH v6 6/7] rust: ww_mutex/exec: add high-level API
Date: Sun, 7 Sep 2025 11:38:17 +0300 [thread overview]
Message-ID: <20250907113817.768acf3d@nimda.home> (raw)
In-Reply-To: <20250907112006.6bdbb478@nimda.home>
On Sun, 7 Sep 2025 11:20:06 +0300
Onur <work@onurozkan.dev> wrote:
> On Sat, 6 Sep 2025 12:04:34 -0300
> Daniel Almeida <daniel.almeida@collabora.com> wrote:
>
> >
> >
> > > On 6 Sep 2025, at 08:13, Onur <work@onurozkan.dev> wrote:
> > >
> > > On Fri, 5 Sep 2025 16:42:09 -0300
> > > Daniel Almeida <daniel.almeida@collabora.com> wrote:
> > >
> > >> Hi Onur,
> > >>
> > >>> On 3 Sep 2025, at 10:13, Onur Özkan <work@onurozkan.dev> wrote:
> > >>>
> > >>> `ExecContext` is a helper built on top of ww_mutex
> > >>
> > >> Again, I wonder what people think about this particular name.
> > >>
> > >>> that provides a retrying interface for lock acquisition.
> > >>> When `EDEADLK` is hit, it drops all held locks, resets
> > >>> the acquire context and retries the given (by the user)
> > >>> locking algorithm until it succeeds.
> > >>>
> > >>> The API keeps track of acquired locks, cleans them up
> > >>> automatically and allows data access to the protected
> > >>> data through `with_locked()`. The `lock_all()` helper
> > >>> allows implementing multi-mutex algorithms in a simpler
> > >>> and less error-prone way while keeping the ww_mutex
> > >>> semantics.
> > >>>
> > >>
> > >> Great, this was exactly what I was looking for! :)
> > >>
> > >>> Signed-off-by: Onur Özkan <work@onurozkan.dev>
> > >>> ---
> > >>> rust/kernel/sync/lock/ww_mutex.rs | 2 +
> > >>> rust/kernel/sync/lock/ww_mutex/exec.rs | 176
> > >>> +++++++++++++++++++++++++ 2 files changed, 178 insertions(+)
> > >>> create mode 100644 rust/kernel/sync/lock/ww_mutex/exec.rs
> > >>>
> > >>> diff --git a/rust/kernel/sync/lock/ww_mutex.rs
> > >>> b/rust/kernel/sync/lock/ww_mutex.rs index
> > >>> b415d6deae9b..7de6578513e5 100644 ---
> > >>> a/rust/kernel/sync/lock/ww_mutex.rs +++
> > >>> b/rust/kernel/sync/lock/ww_mutex.rs @@ -16,6 +16,8 @@
> > >>> use core::cell::UnsafeCell;
> > >>> use core::marker::PhantomData;
> > >>>
> > >>> +pub mod exec;
> > >>> +
> > >>> /// Create static [`WwClass`] instances.
> > >>> ///
> > >>> /// # Examples
> > >>> diff --git a/rust/kernel/sync/lock/ww_mutex/exec.rs
> > >>> b/rust/kernel/sync/lock/ww_mutex/exec.rs new file mode 100644
> > >>> index 000000000000..2f1fc540f0b8
> > >>> --- /dev/null
> > >>> +++ b/rust/kernel/sync/lock/ww_mutex/exec.rs
> > >>> @@ -0,0 +1,176 @@
> > >>> +// SPDX-License-Identifier: GPL-2.0
> > >>> +
> > >>> +//! A high-level [`WwMutex`] execution helper.
> > >>> +//!
> > >>> +//! Provides a retrying lock mechanism on top of [`WwMutex`]
> > >>> and [`WwAcquireCtx`]. +//! It detects [`EDEADLK`] and handles
> > >>> it by rolling back and retrying the +//! user-supplied locking
> > >>> algorithm until success. +
> > >>> +use crate::prelude::*;
> > >>> +use crate::sync::lock::ww_mutex::{WwAcquireCtx, WwClass,
> > >>> WwMutex, WwMutexGuard}; +use core::ptr;
> > >>> +
> > >>> +/// High-level execution type for ww_mutex.
> > >>> +///
> > >>> +/// Tracks a series of locks acquired under a common
> > >>> [`WwAcquireCtx`]. +/// It ensures proper cleanup and retry
> > >>> mechanism on deadlocks and provides +/// type-safe access to
> > >>> locked data via [`with_locked`]. +///
> > >>> +/// Typical usage is through [`lock_all`], which retries a
> > >>> user-supplied +/// locking algorithm until it succeeds without
> > >>> deadlock. +pub struct ExecContext<'a> {
> > >>> + class: &'a WwClass,
> > >>> + acquire: Pin<KBox<WwAcquireCtx<'a>>>,
> > >>> + taken: KVec<WwMutexGuard<'a, ()>>,
> > >>> +}
> > >>> +
> > >>> +impl<'a> Drop for ExecContext<'a> {
> > >>> + fn drop(&mut self) {
> > >>> + self.release_all_locks();
> > >>
> > >> If we move this to the acquire context, then we can do away with
> > >> this drop impl.
> > >>
> > >>> + }
> > >>> +}
> > >>> +
> > >>> +impl<'a> ExecContext<'a> {
> > >>> + /// Creates a new [`ExecContext`] for the given lock class.
> > >>> + ///
> > >>> + /// All locks taken through this context must belong to the
> > >>> same class.
> > >>> + ///
> > >>> + /// TODO: Add some safety mechanism to ensure classes are
> > >>> not different.
> > >>
> > >> core::ptr::eq()?
> > >>
> > >
> > > I was thinking more of a type-level mechanism to do ensure that.
> >
> > Why?
> >
>
> So that wait-wound and wait-die classes don't get mixed up in the
> same `ExecContext` by using type validation at compile time.
>
> Of course, `core::ptr::eq()` is still useful/required when the classes
> are of the same type but not exactly the same value. Maybe we can do
> both.
>
>
> Thanks,
> Onur
I will also look into whether it's possible to remove the class from the
mutex and instead derive it from ExecContext and WwAcquireCtx. This
would fix both issues at once in a better way.
-Onur
next prev parent reply other threads:[~2025-09-07 8:43 UTC|newest]
Thread overview: 34+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-09-03 13:13 [PATCH v6 0/7] rust: add `ww_mutex` support Onur Özkan
2025-09-03 13:13 ` [PATCH v6 1/7] rust: add C wrappers for ww_mutex inline functions Onur Özkan
2025-09-03 13:46 ` Daniel Almeida
2025-09-03 13:13 ` [PATCH v6 2/7] rust: implement `WwClass` for ww_mutex support Onur Özkan
2025-09-03 16:06 ` Boqun Feng
2025-09-04 8:23 ` Onur Özkan
2025-09-03 13:13 ` [PATCH v6 3/7] rust: implement `WwMutex`, `WwAcquireCtx` and `WwMutexGuard` Onur Özkan
2025-09-05 18:49 ` Daniel Almeida
2025-09-05 19:03 ` Daniel Almeida
2025-09-06 11:38 ` Onur
2025-10-22 10:47 ` Onur Özkan
2025-09-06 11:35 ` Onur
2025-09-03 13:13 ` [PATCH v6 4/7] add KUnit coverage on Rust ww_mutex implementation Onur Özkan
2025-09-05 19:04 ` Daniel Almeida
2025-09-03 13:13 ` [PATCH v6 5/7] rust: ww_mutex: add context-free locking functions Onur Özkan
2025-09-05 19:14 ` Daniel Almeida
2025-09-06 11:20 ` Onur
2025-10-21 13:31 ` Daniel Almeida
2025-10-21 13:20 ` Onur Özkan
2025-09-03 13:13 ` [PATCH v6 6/7] rust: ww_mutex/exec: add high-level API Onur Özkan
2025-09-05 19:42 ` Daniel Almeida
2025-09-06 11:13 ` Onur
2025-09-06 15:04 ` Daniel Almeida
2025-09-07 8:20 ` Onur
2025-09-07 8:38 ` Onur [this message]
2025-10-21 19:36 ` Onur Özkan
2025-10-21 13:24 ` Onur Özkan
2025-10-21 14:04 ` Onur Özkan
2025-09-05 23:11 ` Elle Rhumsaa
2025-09-06 11:47 ` Onur Özkan
2025-09-03 13:13 ` [PATCH v6 7/7] add KUnit coverage on ww_mutex/exec implementation Onur Özkan
2025-09-05 23:12 ` Elle Rhumsaa
2025-10-16 19:47 ` [PATCH v6 0/7] rust: add `ww_mutex` support Lyude Paul
2025-10-17 5:03 ` Onur Özkan
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20250907113817.768acf3d@nimda.home \
--to=work@onurozkan.dev \
--cc=a.hindborg@kernel.org \
--cc=alex.gaynor@gmail.com \
--cc=aliceryhl@google.com \
--cc=bjorn3_gh@protonmail.com \
--cc=boqun.feng@gmail.com \
--cc=dakr@kernel.org \
--cc=daniel.almeida@collabora.com \
--cc=daniel@sedlak.dev \
--cc=felipe_life@live.com \
--cc=gary@garyguo.net \
--cc=linux-kernel@vger.kernel.org \
--cc=longman@redhat.com \
--cc=lossin@kernel.org \
--cc=lyude@redhat.com \
--cc=mingo@redhat.com \
--cc=ojeda@kernel.org \
--cc=peterz@infradead.org \
--cc=rust-for-linux@vger.kernel.org \
--cc=tmgross@umich.edu \
--cc=will@kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox