rust: add ww_mutex support
Subsystems (3)
Onur Özkan
reviewerv13 cover rust: add ww_mutex support
This series adds Rust abstractions for ww_mutex, including Class, Mutex, AcquireCtx and MutexGuard. AcquireCtxBackoff and ww_transaction handle deadlock recovery with ordinary guards. DynLockSet tracks dynamically discovered locks and provides separate acquisition and work callbacks.
Changes in v13:
- Require static classes and remove the class lifetime parameters from Mutex and AcquireCtx.
- Make AcquireCtx's locking methods safe. Panic on context drop if any acquired locks remain and check for acquired-lock count overflow before entering C.
- Tighten AcquireCtx::from_raw()'s safety contract to cover forgotten guards.
- Return Option from try_lock().
- Split LockSet into AcquireCtxBackoff and DynLockSet in separate patches. Add ww_transaction() for allocation-free retries with ordinary guards.
- Start each DynLockSet transaction with a new ticket, keep it unchanged across retries and clear pending backoff state on return.
Dropped Alice's Reviewed-by from the Mutex/AcquireCtx patch because the locking API and safety contracts changed.
Previous version: https://lore.kernel.org/all/[email protected]
Onur Özkan (7):
rust: add C wrappers for ww_mutex inline functions
ww_mutex: add ww_class field unconditionally
rust: implement Class for ww_class support
rust: ww_mutex: add Mutex, AcquireCtx and MutexGuard
rust: ww_mutex: add AcquireCtxBackoff
rust: ww_mutex: implement DynLockSet
MAINTAINERS: add Onur Özkan as WW MUTEX maintainer
MAINTAINERS | 2 + include/linux/ww_mutex.h | 8 +- rust/helpers/helpers.c | 1 + rust/helpers/ww_mutex.c | 33 ++ rust/kernel/sync/lock.rs | 1 + rust/kernel/sync/lock/ww_mutex.rs | 449 ++++++++++++++++++ rust/kernel/sync/lock/ww_mutex/acquire_ctx.rs | 179 +++++++ .../sync/lock/ww_mutex/acquire_ctx_backoff.rs | 413 ++++++++++++++++ rust/kernel/sync/lock/ww_mutex/class.rs | 130 +++++ .../kernel/sync/lock/ww_mutex/dyn_lock_set.rs | 351 ++++++++++++++ 10 files changed, 1561 insertions(+), 6 deletions(-) create mode 100644 rust/helpers/ww_mutex.c create mode 100644 rust/kernel/sync/lock/ww_mutex.rs create mode 100644 rust/kernel/sync/lock/ww_mutex/acquire_ctx.rs create mode 100644 rust/kernel/sync/lock/ww_mutex/acquire_ctx_backoff.rs create mode 100644 rust/kernel/sync/lock/ww_mutex/class.rs create mode 100644 rust/kernel/sync/lock/ww_mutex/dyn_lock_set.rs
base-commit: 47324d3a5b3abd781295044d01d92d09f184e872
Signature · 1 line
2.51.2
Onur Özkan
reviewerPATCH v13 1/7 rust: add C wrappers for ww_mutex inline functions
Some of the kernel's ww_mutex functions are implemented as static inline, so they are inaccessible from Rust as bindgen can't generate code on them. This patch provides C function wrappers around these inline implementations, so bindgen can see them and generate the corresponding Rust code.
--- MAINTAINERS | 1 + rust/helpers/helpers.c | 1 + rust/helpers/ww_mutex.c | 33 +++++++++++++++++++++++++++++++++ 3 files changed, 35 insertions(+) create mode 100644 rust/helpers/ww_mutex.c
MAINTAINERS +1 -0
@@ -15283,6 +15283,7 @@ F: kernel/locking/ F: lib/locking*.[ch] F: rust/helpers/mutex.c F: rust/helpers/spinlock.c +F: rust/helpers/ww_mutex.c F: rust/kernel/sync/lock.rs F: rust/kernel/sync/lock/ F: rust/kernel/sync/locked_by.rs
rust/helpers/helpers.c +1 -0
@@ -105,4 +105,5 @@#include "vmalloc.c" #include "wait.c" #include "workqueue.c" +#include "ww_mutex.c" #include "xarray.c"
rust/helpers/ww_mutex.c new file +33 -0
@@ -0,0 +1,33 @@ +// SPDX-License-Identifier: GPL-2.0 + +#include <linux/ww_mutex.h> + +__rust_helper +void rust_helper_ww_mutex_init(struct ww_mutex *lock, struct ww_class *ww_class) +{ +ww_mutex_init (lock, ww_class); +} + +__rust_helper +void rust_helper_ww_acquire_init(struct ww_acquire_ctx *ctx, struct ww_class *ww_class) +{ +ww_acquire_init (ctx, ww_class); +} + +__rust_helper +void rust_helper_ww_acquire_done(struct ww_acquire_ctx *ctx) +{ +ww_acquire_done (ctx); +} + +__rust_helper +void rust_helper_ww_acquire_fini(struct ww_acquire_ctx *ctx) +{ +ww_acquire_fini (ctx); +} + +__rust_helper +bool rust_helper_ww_mutex_is_locked(struct ww_mutex *lock) +{ +return ww_mutex_is_locked (lock); +}
Signature · 1 line
2.51.2
Onur Özkan
reviewerPATCH v13 2/7 ww_mutex: add ww_class field unconditionally
In reply to Onur ÖzkanRemoves DEBUG_WW_MUTEXES guard around the ww_class field in struct ww_mutex and struct ww_acquire_ctx to always initialize it.
Keeping the class pointer available unconditionally allows Rust abstractions to keep from_raw() simple on Mutex and AcquireCtx and validate Mutex and AcquireCtx class pairing in much simpler way.
--- include/linux/ww_mutex.h | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-)
include/linux/ww_mutex.h +2 -6
@@ -48,9 +48,7 @@ struct ww_class {context_lock_struct (ww_mutex) {struct WW_MUTEX_BASE base;struct ww_acquire_ctx *ctx; -#ifdef DEBUG_WW_MUTEXES struct ww_class *ww_class; -#endif };context_lock_struct (ww_acquire_ctx) {
@@ -59,9 +57,9 @@ context_lock_struct(ww_acquire_ctx) {unsigned int acquired;unsigned short wounded;unsigned short is_wait_die; +struct ww_class *ww_class;#ifdef DEBUG_WW_MUTEXES unsigned int done_acquire; -struct ww_class *ww_class;void *contending_lock;#endif #ifdef CONFIG_DEBUG_LOCK_ALLOC
@@ -111,9 +109,7 @@ static inline void ww_mutex_init(struct ww_mutex *lock, {ww_mutex_base_init (&lock->base, ww_class->mutex_name, &ww_class->mutex_key); lock->ctx =NULL ; -#ifdef DEBUG_WW_MUTEXES lock->ww_class = ww_class; -#endif }/**
@@ -149,8 +145,8 @@ static inline void ww_acquire_init(struct ww_acquire_ctx *ctx, ctx->acquired =0 ; ctx->wounded = false; ctx->is_wait_die = ww_class->is_wait_die; -#ifdef DEBUG_WW_MUTEXES ctx->ww_class = ww_class; +#ifdef DEBUG_WW_MUTEXES ctx->done_acquire =0 ; ctx->contending_lock =NULL ;#endif
Signature · 1 line
2.51.2
Onur Özkan
reviewerPATCH v13 3/7 rust: implement Class for ww_class support
In reply to Onur ÖzkanAdds the Class type, the first step in supporting ww_mutex in Rust. Class represents ww_class, used for deadlock avoidance for supporting both wait-die and wound-wait semantics.
Also adds the define_ww_class and define_wd_class macros for safely declaring static instances.
--- rust/kernel/sync/lock.rs | 1 + rust/kernel/sync/lock/ww_mutex.rs | 7 ++ rust/kernel/sync/lock/ww_mutex/class.rs | 130 ++++++++++++++++++++++++ 3 files changed, 138 insertions(+) create mode 100644 rust/kernel/sync/lock/ww_mutex.rs create mode 100644 rust/kernel/sync/lock/ww_mutex/class.rs
rust/kernel/sync/lock.rs +1 -0
@@ -15,6 +15,7 @@pub mod mutex;pub mod spinlock; +pub mod ww_mutex;pub (super )mod global;pub use global::{GlobalGuard ,GlobalLock ,GlobalLockBackend ,GlobalLockedBy };
rust/kernel/sync/lock/ww_mutex.rs new file +7 -0
@@ -0,0 +1,7 @@ +// SPDX-License-Identifier: GPL-2.0 + +//! Rust abstractions for the kernel's wound-wait locking primitives. + +pub use class::Class ; + +mod class;
rust/kernel/sync/lock/ww_mutex/class.rs new file +130 -0
130 lines not on this page · raw message
Signature · 1 line
2.51.2
Onur Özkan
reviewerPATCH v13 4/7 rust: ww_mutex: add Mutex, AcquireCtx and MutexGuard
In reply to Onur ÖzkanCovers the low-level locking API (lock, try_lock and interruptible variants) and integration with kernel bindings.
--- rust/kernel/sync/lock/ww_mutex.rs | 431 ++++++++++++++++++ rust/kernel/sync/lock/ww_mutex/acquire_ctx.rs | 179 ++++++++ 2 files changed, 610 insertions(+) create mode 100644 rust/kernel/sync/lock/ww_mutex/acquire_ctx.rs
rust/kernel/sync/lock/ww_mutex.rs +431 -0
@@ -1,7 +1,438 @@// SPDX-License-Identifier: GPL-2.0 //! Rust abstractions for the kernel's wound-wait locking primitives. +//! +//! It is designed to avoid deadlocks when locking multiple [`Mutex`]es +//! that belong to the same [`Class`]. Each lock acquisition uses an +//! [`AcquireCtx`] to track ordering and ensure forward progress. +//! +//! See srctree/Documentation/locking/ww-mutex-design.rst for more details. +use crate ::{ + bindings, + container_of, + error::to_result, + prelude::*, + types::{ +NotThreadSafe , +Opaque ,// + },// +}; +use core::cell::UnsafeCell ; + +pub use acquire_ctx::AcquireCtx ;pub use class::Class ; +mod acquire_ctx;mod class; + +/// A wound-wait (ww) mutex that is powered with deadlock avoidance +/// when acquiring multiple locks of the same [`Class`]. +/// +/// Each mutex belongs to a [`Class`], which the wound-wait algorithm +/// uses to figure out the order of acquisition and prevent deadlocks. +/// +/// # Examples +/// +/// ``` +/// use kernel::{ +/// define_ww_class, +/// sync::{ +/// lock::ww_mutex::{ +/// AcquireCtx, +/// Class, +/// Mutex, // +/// }, +/// Arc, +/// }, +/// }; +/// use pin_init::stack_pin_init; +/// +/// define_ww_class!(SOME_WW_CLASS); +/// +/// let mutex = Arc::pin_init(Mutex::new(42, &SOME_WW_CLASS), GFP_KERNEL)?; +/// let ctx = KBox::pin_init(AcquireCtx::new(&SOME_WW_CLASS), GFP_KERNEL)?; +/// +/// let guard = ctx.lock(&mutex)?; +/// assert_eq!(*guard, 42); +/// +/// # Ok::<(), Error>(()) +/// ``` +#[pin_data] +#[repr( C )] +pub struct Mutex <T : ?Sized > { +#[pin] + inner:Opaque <bindings::ww_mutex >, + data:UnsafeCell <T >, +} + +impl <T >Mutex <T > { +/// Initializes [`Mutex`] with the given `data` and [`Class`]. +#[inline] +pub fn new (data:T , class: &'staticClass ) ->impl PinInit <Self > { +let class_ptr = class.inner.get (); +pin_init! (Mutex { + inner <-Opaque ::ffi_init (|slot: *mut bindings::ww_mutex | { +// SAFETY: `class` is valid indefinitely. +unsafe { bindings::ww_mutex_init (slot, class_ptr) } + }), + data:UnsafeCell ::new (data), + }) + } +} + +impl <T : ?Sized >Mutex <T > { +/// Checks if this [`Mutex`] is currently locked. +/// +/// The returned value is racy as another thread can acquire +/// or release the lock immediately after this call returns. +#[inline] +pub fn is_locked (&self) ->bool { +// SAFETY: It's safe to call `ww_mutex_is_locked` on +// a valid mutex. +unsafe { bindings::ww_mutex_is_locked (self.inner.get ()) } + } + +/// Locks this [`Mutex`] without [`AcquireCtx`]. +#[inline] +pub fn lock (&self) ->Result <MutexGuard <'_,T >> { +lock (self,None ) + } + +/// Similar to [`Self::lock`], but can be interrupted by signals. +#[inline] +pub fn lock_interruptible (&self) ->Result <MutexGuard <'_,T >> { +lock_interruptible (self,None ) + } + +/// Tries to lock this [`Mutex`] with no [`AcquireCtx`] and without blocking. +/// +/// Returns `None` if the mutex could not be acquired. +#[must_use = "if unused, the lock will be immediately unlocked" ] +#[inline] +pub fn try_lock (&self) ->Option <MutexGuard <'_,T >> { +try_lock (self,None ) + } +} + +impl Mutex <()> { +/// Creates a [`Mutex`] from a raw pointer. +/// +/// This function is intended for interoperability with C code. +/// +/// # Safety +/// +/// The caller must ensure that: +/// +/// - `ptr` is a valid pointer to a `ww_mutex`. +/// - `ptr` must remain valid for the lifetime `'a`. +/// - The `ww_class` associated with this mutex must be valid indefinitely. +#[inline] +pub unsafe fn from_raw <'a>(ptr: *mut bindings::ww_mutex ) -> &'aSelf { +// SAFETY: By the safety contract, the caller guarantees that `ptr` +// points to a valid `ww_mutex` which is the `inner` field of `Mutex`, +// that it remains valid for the lifetime `'a` and the associated +// `ww_class` remains valid indefinitely. +// +// Because [`Mutex`] is `#[repr(C)]`, the `inner` field sits at a +// stable offset that `container_of!` can safely rely on. +unsafe { &*container_of! (Opaque ::cast_from (ptr),Self , inner) } + } +} + +// SAFETY: `Mutex` can be sent to another thread if the protected +// data `T` can be. +unsafe impl <T : ?Sized +Send >Send for Mutex <T > {} + +// SAFETY: `Mutex` serialises access to the protected data, so it is +// `Sync` as long as the data is `Send`. +unsafe impl <T : ?Sized +Send >Sync for Mutex <T > {} + +/// A guard that provides exclusive access to the data protected +/// by a [`Mutex`]. +/// +/// # Invariants +/// +/// The guard holds an exclusive lock on the associated [`Mutex`]. The lock is held +/// for the entire lifetime of this guard and is automatically released when the +/// guard is dropped. +#[must_use = "the lock unlocks immediately when the guard is unused" ] +pub struct MutexGuard <'a,T : ?Sized > { + mutex: &'aMutex <T >, + _not_send:NotThreadSafe , +} + +impl <'a,T : ?Sized >MutexGuard <'a,T > { +/// Creates a new guard for the given [`Mutex`]. +#[inline] +fn new (mutex: &'aMutex <T >) ->Self { +debug_assert! (mutex.is_locked ()); + +// INVARIANT: Locking and `from_raw` ensure that the current task holds the mutex +// and transfer sole responsibility for unlocking it to this guard. +Self { + mutex, + _not_send:NotThreadSafe , + } + } +} + +impl <'a>MutexGuard <'a, ()> { +/// Creates a [`MutexGuard`] from a raw pointer. +/// +/// This function is intended for interoperability with C code. +/// +/// # Safety +/// +/// The caller must ensure that: +/// +/// - `ptr` is a valid pointer to a `ww_mutex`. +/// - `ptr` must remain valid for the lifetime `'b`. +/// - The `ww_class` associated with the `ww_mutex` must be valid indefinitely. +/// - The current task holds the mutex. +/// - The caller transfers responsibility for unlocking the mutex to the returned guard +/// and must not unlock it separately (including through another guard). +/// - Any associated acquire context remains valid until the mutex is unlocked, +/// even if the returned guard is forgotten. The context must not be finalized or +/// reinitialized before then. +#[inline] +pub unsafe fn from_raw <'b>(ptr: *mut bindings::ww_mutex ) ->MutexGuard <'b, ()> { +// SAFETY: By this function's safety contract, the caller guarantees that `ptr` points to a +// valid `ww_mutex` which is the `inner` field of a `Mutex`. The caller also guarantees +// that `ptr` is valid for the lifetime `'b` and its `ww_class` is valid indefinitely. +let mutex =unsafe {Mutex ::from_raw (ptr) }; + +MutexGuard ::new (mutex) + } +} + +impl <T : ?Sized > core::ops::Deref for MutexGuard <'_,T > { +type Target =T ; + +#[inline] +fn deref (&self) -> &Self ::Target { +// SAFETY: self.mutex is locked, so we have exclusive access. +unsafe { &*self.mutex.data.get () } + } +} + +impl <T : ?Sized +Unpin > core::ops::DerefMut for MutexGuard <'_,T > { +#[inline] +fn deref_mut (&mut self) -> &mut Self ::Target { +// SAFETY: self.mutex is locked, so we have exclusive access. +unsafe { &mut *self.mutex.data.get () } + } +} + +impl <T : ?Sized >Drop for MutexGuard <'_,T > { +#[inline] +fn drop (&mut self) { +// SAFETY: self.mutex is locked and are about to release it. +unsafe { bindings::ww_mutex_unlock (self.mutex.inner.get ()) }; + } +} + +// SAFETY: `MutexGuard` can be shared between threads if the data can. +unsafe impl <T : ?Sized +Sync >Sync for MutexGuard <'_,T > {} + +/// Locks a mutex with an optional acquire context. +#[inline] +fn lock <'a,T : ?Sized >(mutex: &'aMutex <T >, ctx:Option <&AcquireCtx >) ->Result <MutexGuard <'a,T >> { +let ctx_ptr =match ctx { +Some (ctx) => { + ctx.validate_mutex (mutex)?; + ctx.inner.get () + } +None => core::ptr::null_mut (), + }; + +// SAFETY: `mutex` is pinned and valid. `ctx_ptr` is null or refers to a pinned context of +// the same class. Callers keep the context valid until the mutex is unlocked. +let ret =unsafe { bindings::ww_mutex_lock (mutex.inner.get (), ctx_ptr) }; +to_result (ret)?; + +Ok (MutexGuard ::new (mutex)) +} + +/// Locks a mutex with an optional acquire context, allowing interruption by signals. +#[inline] +fn lock_interruptible <'a,T : ?Sized >( + mutex: &'aMutex <T >, + ctx:Option <&AcquireCtx >, +) ->Result <MutexGuard <'a,T >> { +let ctx_ptr =match ctx { +Some (ctx) => { + ctx.validate_mutex (mutex)?; + ctx.inner.get () + } +None => core::ptr::null_mut (), + }; + +// SAFETY: `mutex` is pinned and valid. `ctx_ptr` is null or refers to a pinned context of +// the same class. Callers keep the context valid until the mutex is unlocked. +let ret =unsafe { bindings::ww_mutex_lock_interruptible (mutex.inner.get (), ctx_ptr) }; +to_result (ret)?; + +Ok (MutexGuard ::new (mutex)) +} + +/// Tries to lock a mutex with an optional acquire context without blocking. +#[inline] +fn try_lock <'a,T : ?Sized >( + mutex: &'aMutex <T >, + ctx:Option <&AcquireCtx >, +) ->Option <MutexGuard <'a,T >> { +let ctx_ptr =match ctx { +Some (ctx) => { + ctx.validate_mutex (mutex).ok ()?; + ctx.inner.get () + } +None => core::ptr::null_mut (), + }; + +// SAFETY: `mutex` is pinned and valid. `ctx_ptr` is null or refers to a pinned context of +// the same class. Callers keep the context valid until the mutex is unlocked. +let ret =unsafe { bindings::ww_mutex_trylock (mutex.inner.get (), ctx_ptr) }; + +if ret !=0 { +Some (MutexGuard ::new (mutex)) + }else { +None + } +} + +#[kunit_tests(rust_kernel_ww_mutex)] +mod tests { +use super ::*; +use crate ::{ + define_wd_class, + define_ww_class, + sync::Arc ,// + }; + +define_ww_class! (TEST_WOUND_WAIT_CLASS ); +define_wd_class! (TEST_WAIT_DIE_CLASS ); + +#[test] +fn test_ww_mutex_basic_lock_unlock () ->Result { +let mutex =Arc ::pin_init (Mutex ::new (42 , &TEST_WOUND_WAIT_CLASS ),GFP_KERNEL )?; +let ctx =KBox ::pin_init (AcquireCtx ::new (&TEST_WOUND_WAIT_CLASS ),GFP_KERNEL )?; + +let guard = ctx.lock (&mutex)?; +assert_eq! (*guard,42 ); + +// Drop the lock. +drop (guard); + +let mut guard = ctx.lock (&mutex)?; + *guard =100 ; +assert_eq! (*guard,100 ); + +Ok (()) + } + +#[test] +fn test_ww_mutex_trylock () ->Result { +let mutex =Arc ::pin_init (Mutex ::new (123 , &TEST_WAIT_DIE_CLASS ),GFP_KERNEL )?; +let ctx =KBox ::pin_init (AcquireCtx ::new (&TEST_WAIT_DIE_CLASS ),GFP_KERNEL )?; + +// `try_lock` on unlocked mutex should succeed. +let guard = ctx.try_lock (&mutex).ok_or (EBUSY )?; +assert_eq! (*guard,123 ); + +// Now it should fail immediately as it's already locked. +assert! (ctx.try_lock (&mutex).is_none ()); + +drop (guard); +assert! (ctx.try_lock (&mutex).is_some ()); + +Ok (()) + } + +#[test] +fn test_ww_mutex_class_mismatch () ->Result { +let mutex =Arc ::pin_init (Mutex ::new (123 , &TEST_WOUND_WAIT_CLASS ),GFP_KERNEL )?; +let ctx =KBox ::pin_init (AcquireCtx ::new (&TEST_WAIT_DIE_CLASS ),GFP_KERNEL )?; + +assert_eq! (ctx.lock (&mutex).err (),Some (EINVAL )); +assert_eq! (ctx.lock_interruptible (&mutex).err (),Some (EINVAL )); +assert! (ctx.try_lock (&mutex).is_none ()); +assert! (!mutex.is_locked ()); + +Ok (()) + } + +#[test] +fn test_ww_mutex_is_locked () ->Result { +let mutex =Arc ::pin_init (Mutex ::new ("hello" , &TEST_WOUND_WAIT_CLASS ),GFP_KERNEL )?; +let ctx =KBox ::pin_init (AcquireCtx ::new (&TEST_WOUND_WAIT_CLASS ),GFP_KERNEL )?; + +// Should not be locked initially. +assert! (!mutex.is_locked ()); + +let guard = ctx.lock (&mutex)?; +assert! (mutex.is_locked ()); + +drop (guard); +assert! (!mutex.is_locked ()); + +Ok (()) + } + +#[test] +fn test_ww_acquire_context_done () ->Result { +let mutex1 =Arc ::pin_init (Mutex ::new (1 , &TEST_WAIT_DIE_CLASS ),GFP_KERNEL )?; +let mutex2 =Arc ::pin_init (Mutex ::new (2 , &TEST_WAIT_DIE_CLASS ),GFP_KERNEL )?; +let ctx =KBox ::pin_init (AcquireCtx ::new (&TEST_WAIT_DIE_CLASS ),GFP_KERNEL )?; + +// Acquire multiple mutexes with the same context. +let guard1 = ctx.lock (&mutex1)?; +let guard2 = ctx.lock (&mutex2)?; + +assert_eq! (*guard1,1 ); +assert_eq! (*guard2,2 ); + + ctx.done (); + +Ok (()) + } + +#[test] +fn test_mutex_without_ctx () ->Result { +let mutex =Arc ::pin_init (Mutex ::new (100 , &TEST_WOUND_WAIT_CLASS ),GFP_KERNEL )?; +let guard = mutex.lock ()?; + +assert_eq! (*guard,100 ); +assert! (mutex.is_locked ()); +assert! (mutex.try_lock ().is_none ()); + +drop (guard); +assert! (!mutex.is_locked ()); + +let guard = mutex.try_lock ().ok_or (EBUSY )?; +assert_eq! (*guard,100 ); +drop (guard); +assert! (!mutex.is_locked ()); + +Ok (()) + } + +#[test] +fn test_guard_from_raw_unlocks_mutex () ->Result { +let mutex =Arc ::pin_init (Mutex ::new ((), &TEST_WOUND_WAIT_CLASS ),GFP_KERNEL )?; +let guard = mutex.lock ()?; + +// Keep the mutex locked without leaving another guard responsible for unlocking it. + core::mem::forget (guard); + +// SAFETY: `mutex` and its class remain valid for this test. The current task holds +// the mutex, and forgetting the previous guard lets this guard take over unlocking it. +let guard =unsafe {MutexGuard ::from_raw (mutex.inner.get ()) }; +assert! (mutex.is_locked ()); + +drop (guard); +assert! (!mutex.is_locked ()); + +Ok (()) + } +}
rust/kernel/sync/lock/ww_mutex/acquire_ctx.rs new file +179 -0
179 lines not on this page · raw message
Signature · 1 line
2.51.2
Onur Özkan
reviewerPATCH v13 5/7 rust: ww_mutex: add AcquireCtxBackoff
In reply to Onur ÖzkanAcquireCtxBackoff wraps AcquireCtx to remember the contended mutex. The ww_transaction helper provides deadlock retries with ordinary guards.
After the caller releases its guards, backoff waits for the contended mutex. Retries keep the same acquire context and ticket.
--- rust/kernel/sync/lock/ww_mutex.rs | 7 + .../sync/lock/ww_mutex/acquire_ctx_backoff.rs | 283 ++++++++++++++++++ 2 files changed, 290 insertions(+) create mode 100644 rust/kernel/sync/lock/ww_mutex/acquire_ctx_backoff.rs
rust/kernel/sync/lock/ww_mutex.rs +7 -0
@@ -6,6 +6,8 @@//! that belong to the same [`Class`]. Each lock acquisition uses an //! [`AcquireCtx`] to track ordering and ensure forward progress. //! +//! [`ww_transaction`] provides automatic deadlock recovery for ordinary guards. +//! //! See srctree/Documentation/locking/ww-mutex-design.rst for more details. use crate ::{
@@ -21,9 +23,14 @@use core::cell::UnsafeCell ;pub use acquire_ctx::AcquireCtx ; +pub use acquire_ctx_backoff::{ + ww_transaction, +AcquireCtxBackoff ,// +};pub use class::Class ;mod acquire_ctx; +mod acquire_ctx_backoff;mod class;/// A wound-wait (ww) mutex that is powered with deadlock avoidance
rust/kernel/sync/lock/ww_mutex/acquire_ctx_backoff.rs new file +283 -0
283 lines not on this page · raw message
Signature · 1 line
2.51.2
Onur Özkan
reviewerPATCH v13 6/7 rust: ww_mutex: implement DynLockSet
In reply to Onur ÖzkanDynLockSet is a high-level and safe API built on top of ww_mutex for dynamically discovered locks. It keeps acquired locks held, handles repeated objects and provides access to their data.
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.
--- rust/kernel/sync/lock/ww_mutex.rs | 4 + .../sync/lock/ww_mutex/acquire_ctx_backoff.rs | 138 ++++++- .../kernel/sync/lock/ww_mutex/dyn_lock_set.rs | 351 ++++++++++++++++++ 3 files changed, 489 insertions(+), 4 deletions(-) create mode 100644 rust/kernel/sync/lock/ww_mutex/dyn_lock_set.rs
rust/kernel/sync/lock/ww_mutex.rs +4 -0
@@ -7,6 +7,8 @@//! [`AcquireCtx`] to track ordering and ensure forward progress. //! //! [`ww_transaction`] provides automatic deadlock recovery for ordinary guards. +//! Use [`DynLockSet::ww_transaction`] for dynamically discovered locks and +//! separate acquisition and work callbacks. //! //! See srctree/Documentation/locking/ww-mutex-design.rst for more details.
@@ -28,10 +30,12 @@ AcquireCtxBackoff, // };pub use class::Class ; +pub use dyn_lock_set::DynLockSet ;mod acquire_ctx;mod acquire_ctx_backoff;mod class; +mod dyn_lock_set;/// A wound-wait (ww) mutex that is powered with deadlock avoidance /// when acquiring multiple locks of the same [`Class`].
rust/kernel/sync/lock/ww_mutex/acquire_ctx_backoff.rs +134 -4
175 lines not on this page · raw message
rust/kernel/sync/lock/ww_mutex/dyn_lock_set.rs new file +351 -0
351 lines not on this page · raw message
Signature · 1 line
2.51.2
Onur Özkan
reviewerPATCH v13 7/7 MAINTAINERS: add Onur Özkan as WW MUTEX maintainer
In reply to Onur ÖzkanAdd myself as a WW MUTEX maintainer to reflect ongoing work and future maintenance of WW MUTEX.
--- MAINTAINERS | 1 + 1 file changed, 1 insertion(+)
MAINTAINERS +1 -0
@@ -15265,6 +15265,7 @@ M: Peter Zijlstra <[email protected]> M: Ingo Molnar <[email protected]> M: Will Deacon <[email protected]> M: Boqun Feng <[email protected]> (LOCKDEP & RUST) +M: Onur Özkan <[email protected]> (WW MUTEX) R: Waiman Long <[email protected]> L: [email protected] S: Maintained
Signature · 1 line
2.51.2
Re: [PATCH v13 6/7] rust: ww_mutex: implement DynLockSet
In reply to Onur ÖzkanHi Onur,
Quoted 363 lines · Onur Özkan
DynLockSet is a high-level and safe API built on top of
ww_mutexfor dynamically discovered locks. It keeps acquired locks held, handles repeated objects and provides access to their data.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.
Signed-off-by Onur Özkan <[email protected]>--- rust/kernel/sync/lock/ww_mutex.rs | 4 + .../sync/lock/ww_mutex/acquire_ctx_backoff.rs | 138 ++++++- .../kernel/sync/lock/ww_mutex/dyn_lock_set.rs | 351 ++++++++++++++++++ 3 files changed, 489 insertions(+), 4 deletions(-) create mode 100644 rust/kernel/sync/lock/ww_mutex/dyn_lock_set.rsrust/kernel/sync/lock/ww_mutex.rs +4
-0@@ -7,6 +7,8 @@//! [`AcquireCtx`] to track ordering and ensure forward progress. //! //! [`ww_transaction`] provides automatic deadlock recovery for ordinary guards. +//! Use [`DynLockSet::ww_transaction`] for dynamically discovered locks and +//! separate acquisition and work callbacks. //! //! See srctree/Documentation/locking/ww-mutex-design.rst for more details. @@ -28,10 +30,12 @@ AcquireCtxBackoff, // };pub use class::Class ; +pub use dyn_lock_set::DynLockSet ;mod acquire_ctx;mod acquire_ctx_backoff;mod class; +mod dyn_lock_set;/// A wound-wait (ww) mutex that is powered with deadlock avoidance /// when acquiring multiple locks of the same [`Class`]. rust/kernel/sync/lock/ww_mutex/acquire_ctx_backoff.rs +134
-4@@ -8,13 +8,17 @@ Mutex, MutexGuard, // }; -use crate ::prelude::*; +use crate ::{ + bindings, + prelude::*,// +};use core::cell::Cell ;/// An [`AcquireCtx`] that remembers the contended mutex. /// /// After [`Self::lock`] returns [`EDEADLK`], release all guards and call -/// [`Self::backoff`] before retrying. [`ww_transaction`] handles this retry loop. +/// [`Self::backoff`] before retrying. [`ww_transaction`] and +/// [`super::DynLockSet::ww_transaction`] handle this retry loop. /// /// Mutexes must outlive this wrapper so they remain valid for retry. /// @@ -109,11 +113,36 @@ pub fn done(&self) { }#[inline] -fn assert_no_locks_held (&self) { +pub (super )fn take_contended_guard (&self) ->Option <MutexGuard <'_, ()>> { + self.backoff_guard.take () + } + +#[inline] +pub (super )fn assert_no_locks_held (&self) {// SAFETY: This initialized context belongs to the current task. let acquired =unsafe { (*self.ctx.inner.get ()).acquired };assert_eq! (acquired,0 ,"ww transaction still holds caller-owned locks" ); } + +#[inline] +pub (super )fn clear_backoff (&self) { +drop (self.backoff_guard.take ()); + self.contended.set (None ); + } + +pub (super )fn restart (&self) { + self.clear_backoff (); + self.assert_no_locks_held (); +let ptr = self.ctx.inner.get (); +// SAFETY: This wrapper exclusively borrows the initialized, pinned context. +// No acquired locks remain, so it can be finalized and reinitialized with +// its static class. +unsafe { +let class = (*ptr).ww_class; + bindings::ww_acquire_fini (ptr); + bindings::ww_acquire_init (ptr, class); + } + } }impl Drop for AcquireCtxBackoff <'_, '_> {@@ -164,7 +193,7 @@ pub fn ww_transaction<'a, R>(retry (&ctx, ||transaction (&ctx)) } -fn retry <R >( +pub (super )fn retry <R >( ctx: &AcquireCtxBackoff <'_, '_>,mut transaction:impl FnMut () ->Result <R >, ) ->Result <R > {@@ -178,6 +207,7 @@ fn retry<R>(#[kunit_tests(rust_kernel_ww_transaction)] mod tests { +use super ::super ::DynLockSet ;use super ::*;use crate ::define_ww_class;use pin_init::stack_pin_init;@@ -280,4 +310,104 @@ fn test_deadlock_without_contention() -> Result {assert! (!mutex.is_locked ());Ok (()) } + +#[test] +fn test_dynamic_retry () ->Result { +stack_pin_init! (let first =Mutex ::new (10 , &TEST_TRANSACTION_CLASS )); +stack_pin_init! (let second =Mutex ::new (20 , &TEST_TRANSACTION_CLASS )); +stack_pin_init! (let acquire_ctx =AcquireCtx ::new (&TEST_TRANSACTION_CLASS )); +let ctx =AcquireCtxBackoff ::new (acquire_ctx.as_mut ()); +let mut set =DynLockSet ::new (&ctx); +let mut attempts =0 ; +let mut identity =None ; + +let result = set.ww_transaction ( + |set| { + attempts +=1 ; +let ptr = ctx.ctx.inner.get (); +// SAFETY: The pinned context is initialized and used only by +// this task throughout the transaction. +let stamp =unsafe { (*ptr).stamp }; +if attempts ==1 { + identity =Some ((ptr, stamp)); + set.lock (&first)?; +// SAFETY: `second` is initialized, pinned, and outlives the transaction. +let mutex =unsafe {Mutex ::from_raw (second.inner.get ()) }; + ctx.contended.set (Some (mutex)); +return Err (EDEADLK ); + } + +assert_eq! (attempts,2 ); +assert_eq! (identity,Some ((ptr, stamp))); +assert! (!first.is_locked ()); +// Backoff makes the contended mutex available before lock(). +assert_eq! (set.with_locked (&second, |v| *v)?,20 ); + set.lock (&second)?; + set.lock (&first) + }, + |set| { + set.with_locked (&first, |v| *v +=1 )?; + set.with_locked (&second, |v| { + *v +=2 ; + *v + }) + }, + )?; + +assert_eq! (result,22 ); +assert_eq! (attempts,2 ); +assert! (!first.is_locked ()); +assert! (!second.is_locked ()); +Ok (()) + } + +#[test] +fn test_dynamic_error_cleanup () ->Result { +stack_pin_init! (let first =Mutex ::new (1 , &TEST_TRANSACTION_CLASS )); +stack_pin_init! (let contended =Mutex ::new (2 , &TEST_TRANSACTION_CLASS )); +stack_pin_init! (let acquire_ctx =AcquireCtx ::new (&TEST_TRANSACTION_CLASS )); +let ctx =AcquireCtxBackoff ::new (acquire_ctx.as_mut ()); +let mut set =DynLockSet ::new (&ctx); + +for swallowin [false ,true ] { +let mut work_ran =false ; +let result = set.ww_transaction ( + |set| { + set.lock (&first)?; +// A swallowed or mapped EDEADLK must not poison the next call. +// SAFETY: `contended` is initialized, pinned, and outlives the transaction. +let mutex =unsafe {Mutex ::from_raw (contended.inner.get ()) }; + ctx.contended.set (Some (mutex)); +assert_eq! (set.lock (&first),Err (EDEADLK )); +if swallow { +Ok (()) + }else { +Err (EINVAL ) + } + }, + |set| { + work_ran =true ; + ctx.done (); + set.with_locked (&first, |v| *v +=1 ) + }, + ); + +assert_eq! (result,if swallow {Ok (()) }else {Err (EINVAL ) }); +assert_eq! (work_ran, swallow); +assert! (ctx.contended.get ().is_none ()); +assert! (!first.is_locked ()); +assert! (!contended.is_locked ()); + + set.ww_transaction ( + |set| { + set.lock (&contended)?; + set.lock (&first) + }, + |set| set.with_locked (&contended, |v| *v +=1 ), + )?; +assert! (!first.is_locked ()); +assert! (!contended.is_locked ()); + } +Ok (()) + } }rust/kernel/sync/lock/ww_mutex/dyn_lock_set.rs new file +130
-0@@ -0,0 +1,351 @@ +// SPDX-License-Identifier: GPL-2.0 + +//! Wound/wait locking for a set of objects discovered at runtime. +//! +//! [`DynLockSet`] keeps locks held while the caller inspects objects and finds +//! more locks to acquire. It handles repeated mutexes and provides access to +//! their protected data. [`DynLockSet::ww_transaction`] handles deadlock retries +//! and runs the work callback once all locks are held. + +use super ::{ + acquire_ctx_backoff::retry, +AcquireCtxBackoff , +Mutex , +MutexGuard ,// +}; +use crate ::{ + bindings, + prelude::*, + types::NotThreadSafe ,// +}; +use core::ptr::NonNull ; + +/// Used by [`DynLockSet`] to track acquired locks. +/// +/// This type is strictly crate-private and must never be exposed +/// outside this crate. +struct RawGuard { + mutex_ptr:NonNull <bindings::ww_mutex >, + _not_send:NotThreadSafe , +} + +impl Drop for RawGuard { +#[inline] +fn drop (&mut self) { +// SAFETY: `mutex_ptr` originates from a locked `Mutex` and remains +// valid for the lifetime of this guard, so unlocking here is sound. +unsafe { bindings::ww_mutex_unlock (self.mutex_ptr.as_ptr ()) }; + } +} + +/// A tracked set of [`Mutex`] locks acquired with the same [`AcquireCtxBackoff`]. +/// +/// Keeps locks held across loops and helper calls, handles repeated mutexes and +/// provides access to their data through [`Self::with_locked`]. +pub struct DynLockSet <'ctx, 'm> { + ctx: &'ctxAcquireCtxBackoff <'ctx, 'm>, + taken:KVec <RawGuard >, +} + +impl <'ctx, 'm>DynLockSet <'ctx, 'm> { +/// Creates an empty set using the given acquire context. +#[inline] +pub fn new (ctx: &'ctxAcquireCtxBackoff <'ctx, 'm>) ->Self { +Self { + ctx, + taken:KVec ::new (), + } + } + +/// Attempts to lock the given [`Mutex`] and stores a guard for it. +/// +/// Returns success if the mutex is already held by this set, unless deadlock +/// recovery is pending. +pub fn lock <T >(&mut self, mutex: &'mMutex <T >) ->Result { +match self.ctx.lock (mutex) { +Ok (guard) => self.add_guard (guard), +Err (e)if e ==EALREADY => { +// The context may hold a guard owned outside this set. +if self + .taken + .iter () + .any (|guard| guard.mutex_ptr.as_ptr () == mutex.inner.get ()) + { +Ok (()) + }else { +Err (e) + } + } +Err (e) =>Err (e), + } + } + +/// Acquires a dynamic set, retrying on deadlock then runs `work` once. +/// +/// Each call releases existing locks and starts a new transaction. Retries keep +/// the same ticket. The set retains its allocation across retries and calls. +/// +/// `work` runs once after acquisition and must not acquire more locks. Its errors +/// are returned without retrying. All locks and pending backoff state are released +/// on return. The callbacks must not change the set's context or nest transactions +/// on that context. +/// +/// # Panics +/// +/// Panics if the context holds caller-owned locks outside this set at a transaction +/// boundary, including forgotten guards. +/// +/// # Examples +/// +/// ``` +/// use kernel::{ +/// define_ww_class, +/// prelude::*, +/// sync::lock::ww_mutex::{ +/// AcquireCtx, +/// AcquireCtxBackoff, +/// DynLockSet, +/// Mutex, // +/// }, // +/// }; +/// use pin_init::stack_pin_init; +/// +/// define_ww_class!(CLASS); +/// +/// stack_pin_init!(let first = Mutex::new(1, &CLASS)); +/// stack_pin_init!(let second = Mutex::new(2, &CLASS)); +/// +/// stack_pin_init!(let acquire_ctx = AcquireCtx::new(&CLASS)); +/// let ctx = AcquireCtxBackoff::new(acquire_ctx.as_mut()); +/// let mut set = DynLockSet::new(&ctx); +/// +/// set.ww_transaction( +/// |set| { +/// set.lock(&first)?; +/// +/// if set.with_locked(&first, |value| *value > 0)? { +/// set.lock(&second)?; +/// } +/// +/// Ok(())
rust/kernel/sync/lock/ww_mutex/dyn_lock_set.rs line 140
Quoted 10 lines
rust/kernel/sync/lock/ww_mutex/dyn_lock_set.rs new file +10
-0+/// }, +/// |set| set.with_locked(&first, |value| *value += 1), +/// )?; +/// # Ok::<(), Error>(()) +/// ``` +pub fn ww_transaction <R >( + &mut self, +mut acquire:impl FnMut (&mut Self ) ->Result , + work:impl FnOnce (&mut Self ) ->Result <R >, + ) ->Result <R > {
So I have been pondering a bit on that one, in a context of drm_exec and potentially having a Rust version of drm_gpuvm_exec_lock & co, and I'm having some doubts really. The drm_exec implementation, apart from locking and reserving fences, also plays with the drm_gem_object reference counting. The drm_exec pairs each tracked lock with a GEM reference and releases both during backoff or cleanup. Proposed implementation makes it rather cumbersome to drop the reference when unlocking the mutex on a rollback (or backoff in this series terminology). I assume that would require some sort of extension to the DynLockSet ? Or am I missing smth ?
---
BR
Beata
Quoted 215 lines
rust/kernel/sync/lock/ww_mutex/dyn_lock_set.rs new file +211
-0+let ctx = self.ctx; + self.clear (); + ctx.restart (); + +let result =retry (ctx, || { +if let Some (guard) = self.ctx.take_contended_guard () { + self.add_guard (guard)?; + } + +let result =acquire (self); +if result.is_err () { + self.clear (); + } + + result + }); + +let result = result.and_then (|()|work (self)); + + self.clear (); + ctx.clear_backoff (); + ctx.assert_no_locks_held (); + + result + } + +fn add_guard <T : ?Sized >(&mut self, guard:MutexGuard <'ctx,T >) ->Result { +let raw_guard =RawGuard { +// SAFETY: The guard holds this mutex which remains valid for `'ctx`. + mutex_ptr:unsafe {NonNull ::new_unchecked (guard.mutex.inner.get ()) }, + _not_send:NotThreadSafe , + }; + +// Transfer unlocking to `raw_guard` before the fallible push so failure +// drops only one guard and unlocks the mutex exactly once. + core::mem::forget (guard); + self.taken.push (raw_guard,GFP_KERNEL )?; + +Ok (()) + } + +/// Executes `access` with a mutable reference to the data behind [`Mutex`]. +/// +/// Fails with [`EINVAL`] if the [`Mutex`] was not locked in this set. +pub fn with_locked <T :Unpin ,Y >( + &mut self, + mutex: &'mMutex <T >, + access:impl for <'b>FnOnce (&'bmut T ) ->Y , + ) ->Result <Y > { +let mutex_ptr = mutex.inner.get (); + +if self + .taken + .iter () + .any (|guard| guard.mutex_ptr.as_ptr () == mutex_ptr) + { +// SAFETY: We hold the lock corresponding to `mutex` so we have +// exclusive access to its protected data. +let value =unsafe { &mut *mutex.data.get () }; + +Ok (access (value)) + }else { +// `mutex` isn't locked in this set. +Err (EINVAL ) + } + } + +/// Releases all locks in the set. +#[inline] +pub fn clear (&mut self) { +// `Drop` implementation of the `RawGuard` takes care of the unlocking. + self.taken.clear (); + } +} + +impl Drop for DynLockSet <'_, '_> { +#[inline] +fn drop (&mut self) { + self.clear (); + } +} + +#[kunit_tests(rust_kernel_dyn_lock_set)] +mod tests { +use super ::super ::AcquireCtx ; +use super ::*; +use crate ::{ + define_wd_class, + define_ww_class,// + }; +use pin_init::stack_pin_init; + +define_ww_class! (TEST_WOUND_WAIT_CLASS ); +define_wd_class! (TEST_WAIT_DIE_CLASS ); + +#[test] +fn test_lock_and_clear () ->Result { +stack_pin_init! (let mutex =Mutex ::new (10 , &TEST_WOUND_WAIT_CLASS )); +stack_pin_init! (let acquire_ctx =AcquireCtx ::new (&TEST_WOUND_WAIT_CLASS )); +let ctx =AcquireCtxBackoff ::new (acquire_ctx.as_mut ()); +let mut set =DynLockSet ::new (&ctx); + +assert_eq! (set.taken.capacity (),0 ); + + set.lock (&mutex)?; + set.lock (&mutex)?; +assert_eq! (set.taken.len (),1 ); + + set.with_locked (&mutex, |v| *v +=7 )?; +assert_eq! (set.with_locked (&mutex, |v| *v)?,17 ); + +let capacity = set.taken.capacity (); +assert! (capacity >0 ); + + set.clear (); +assert! (!mutex.is_locked ()); +assert_eq! (set.taken.len (),0 ); +assert_eq! (set.taken.capacity (), capacity); + +Ok (()) + } + +#[test] +fn test_drop_unlocks () ->Result { +stack_pin_init! (let first =Mutex ::new (1 , &TEST_WOUND_WAIT_CLASS )); +stack_pin_init! (let second =Mutex ::new (2 , &TEST_WOUND_WAIT_CLASS )); +stack_pin_init! (let acquire_ctx =AcquireCtx ::new (&TEST_WOUND_WAIT_CLASS )); +let ctx =AcquireCtxBackoff ::new (acquire_ctx.as_mut ()); + + { +let mut set =DynLockSet ::new (&ctx); + + set.lock (&first)?; + set.lock (&second)?; + } + +assert! (!first.is_locked ()); +assert! (!second.is_locked ()); + +Ok (()) + } + +#[test] +fn test_context_reuse () ->Result { +stack_pin_init! (let mutex =Mutex ::new (5 , &TEST_WOUND_WAIT_CLASS )); +let mut acquire_ctx =KBox ::pin_init (AcquireCtx ::new (&TEST_WOUND_WAIT_CLASS ),GFP_KERNEL )?; + + { +let ctx =AcquireCtxBackoff ::new (acquire_ctx.as_mut ()); +let mut set =DynLockSet ::new (&ctx); + + set.ww_transaction ( + |set| set.lock (&mutex), + |set| set.with_locked (&mutex, |value| *value +=1 ), + )?; + +assert! (!mutex.is_locked ()); + } + +let guard = acquire_ctx.lock (&mutex)?; +assert_eq! (*guard,6 ); + +Ok (()) + } + +#[test] +fn test_reject_external_guard () ->Result { +stack_pin_init! (let mutex =Mutex ::new (5 , &TEST_WOUND_WAIT_CLASS )); +stack_pin_init! (let acquire_ctx =AcquireCtx ::new (&TEST_WOUND_WAIT_CLASS )); +let ctx =AcquireCtxBackoff ::new (acquire_ctx.as_mut ()); +let guard = ctx.lock (&mutex)?; +let mut set =DynLockSet ::new (&ctx); + +assert_eq! (set.lock (&mutex),Err (EALREADY )); +assert_eq! (set.with_locked (&mutex, |v| *v),Err (EINVAL )); + + set.clear (); +assert! (mutex.is_locked ()); +assert_eq! (*guard,5 ); + +drop (guard); +assert! (!mutex.is_locked ()); + +Ok (()) + } + +#[test] +fn test_access_without_lock () ->Result { +stack_pin_init! (let mutex =Mutex ::new (5 , &TEST_WOUND_WAIT_CLASS )); +stack_pin_init! (let acquire_ctx =AcquireCtx ::new (&TEST_WOUND_WAIT_CLASS )); +let ctx =AcquireCtxBackoff ::new (acquire_ctx.as_mut ()); +let mut set =DynLockSet ::new (&ctx); + +assert_eq! (set.with_locked (&mutex, |v| *v),Err (EINVAL )); + +Ok (()) + } + +#[test] +fn test_class_mismatch () ->Result { +stack_pin_init! (let mutex =Mutex ::new (5 , &TEST_WOUND_WAIT_CLASS )); +stack_pin_init! (let acquire_ctx =AcquireCtx ::new (&TEST_WAIT_DIE_CLASS )); +let ctx =AcquireCtxBackoff ::new (acquire_ctx.as_mut ()); +let mut set =DynLockSet ::new (&ctx); + +assert_eq! (set.lock (&mutex),Err (EINVAL )); +assert! (!mutex.is_locked ()); + +Ok (()) + } +}Signature · 1 line
2.51.2
Onur Özkan
reviewerRe: [PATCH v13 6/7] rust: ww_mutex: implement DynLockSet
Quoted 45 lines · On Wed, 11 Nov 2026 20:55:29 +0100 Beata Michalska <[email protected]>…
Hi Onur,
Quoted 42 lines · Onur Özkan
DynLockSet is a high-level and safe API built on top of
ww_mutexfor dynamically discovered locks. It keeps acquired locks held, handles repeated objects and provides access to their data.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.
Signed-off-by Onur Özkan <[email protected]>--- rust/kernel/sync/lock/ww_mutex.rs | 4 + .../sync/lock/ww_mutex/acquire_ctx_backoff.rs | 138 ++++++- .../kernel/sync/lock/ww_mutex/dyn_lock_set.rs | 351 ++++++++++++++++++ 3 files changed, 489 insertions(+), 4 deletions(-) create mode 100644 rust/kernel/sync/lock/ww_mutex/dyn_lock_set.rsrust/kernel/sync/lock/ww_mutex.rs +4
-0@@ -7,6 +7,8 @@//! [`AcquireCtx`] to track ordering and ensure forward progress. //! //! [`ww_transaction`] provides automatic deadlock recovery for ordinary guards. +//! Use [`DynLockSet::ww_transaction`] for dynamically discovered locks and +//! separate acquisition and work callbacks. //! //! See srctree/Documentation/locking/ww-mutex-design.rst for more details. @@ -28,10 +30,12 @@ AcquireCtxBackoff, // };pub use class::Class ; +pub use dyn_lock_set::DynLockSet ;mod acquire_ctx;mod acquire_ctx_backoff;mod class; +mod dyn_lock_set;/// A wound-wait (ww) mutex that is powered with deadlock avoidance
[...]
Quoted 69 lines
Quoted 49 lines
rust/kernel/sync/lock/ww_mutex.rs +49
-0+ +/// Acquires a dynamic set, retrying on deadlock then runs `work` once. +/// +/// Each call releases existing locks and starts a new transaction. Retries keep +/// the same ticket. The set retains its allocation across retries and calls. +/// +/// `work` runs once after acquisition and must not acquire more locks. Its errors +/// are returned without retrying. All locks and pending backoff state are released +/// on return. The callbacks must not change the set's context or nest transactions +/// on that context. +/// +/// # Panics +/// +/// Panics if the context holds caller-owned locks outside this set at a transaction +/// boundary, including forgotten guards. +/// +/// # Examples +/// +/// ``` +/// use kernel::{ +/// define_ww_class, +/// prelude::*, +/// sync::lock::ww_mutex::{ +/// AcquireCtx, +/// AcquireCtxBackoff, +/// DynLockSet, +/// Mutex, // +/// }, // +/// }; +/// use pin_init::stack_pin_init; +/// +/// define_ww_class!(CLASS); +/// +/// stack_pin_init!(let first = Mutex::new(1, &CLASS)); +/// stack_pin_init!(let second = Mutex::new(2, &CLASS)); +/// +/// stack_pin_init!(let acquire_ctx = AcquireCtx::new(&CLASS)); +/// let ctx = AcquireCtxBackoff::new(acquire_ctx.as_mut()); +/// let mut set = DynLockSet::new(&ctx); +/// +/// set.ww_transaction( +/// |set| { +/// set.lock(&first)?; +/// +/// if set.with_locked(&first, |value| *value > 0)? { +/// set.lock(&second)?; +/// } +/// +/// Ok(()) rust/kernel/sync/lock/ww_mutex.rs
Quoted 10 lines
rust/kernel/sync/lock/ww_mutex.rs +10
-0+/// }, +/// |set| set.with_locked(&first, |value| *value += 1), +/// )?; +/// # Ok::<(), Error>(()) +/// ``` +pub fn ww_transaction <R >( + &mut self, +mut acquire:impl FnMut (&mut Self ) ->Result , + work:impl FnOnce (&mut Self ) ->Result <R >, + ) ->Result <R > {So I have been pondering a bit on that one, in a context of
drm_execand potentially having a Rust version ofdrm_gpuvm_exec_lock& co, and I'm having some doubts really. Thedrm_execimplementation, apart from locking and reserving fences, also plays with thedrm_gem_objectreference counting. Thedrm_execpairs each tracked lock with a GEM reference and releases both during backoff or cleanup. Proposed implementation makes it rather cumbersome to drop the reference when unlocking the mutex on a rollback (or backoff in this series terminology). I assume that would require some sort of extension to theDynLockSet? Or am I missing smth ?
Quoted 3 lines
---
BR
Beata
Current API tracks the locks but leaves ownership of the objects to the caller and, yes, we need some sort of extension on DynLockSet probably together with AcqureCtx and its wrapper type.
Does current API limit your GPUVM use case? Could you share an example of the flow you need? I would be happy to extend the API if that's needed.
Thanks,
Onur
Quoted 27 lines
Quoted 27 lines
rust/kernel/sync/lock/ww_mutex.rs +26
-0+let ctx = self.ctx; + self.clear (); + ctx.restart (); + +let result =retry (ctx, || { +if let Some (guard) = self.ctx.take_contended_guard () { + self.add_guard (guard)?; + } + +let result =acquire (self); +if result.is_err () { + self.clear (); + } + + result + }); + +let result = result.and_then (|()|work (self)); + + self.clear (); + ctx.clear_backoff (); + ctx.assert_no_locks_held (); + + result + } +
[...]
Quoted 23 lines
Quoted 23 lines
rust/kernel/sync/lock/ww_mutex.rs +19
-0+ +assert_eq! (set.with_locked (&mutex, |v| *v),Err (EINVAL )); + +Ok (()) + } + +#[test] +fn test_class_mismatch () ->Result { +stack_pin_init! (let mutex =Mutex ::new (5 , &TEST_WOUND_WAIT_CLASS )); +stack_pin_init! (let acquire_ctx =AcquireCtx ::new (&TEST_WAIT_DIE_CLASS )); +let ctx =AcquireCtxBackoff ::new (acquire_ctx.as_mut ()); +let mut set =DynLockSet ::new (&ctx); + +assert_eq! (set.lock (&mutex),Err (EINVAL )); +assert! (!mutex.is_locked ()); + +Ok (()) + } +}Signature · 1 line
2.51.2
Re: [PATCH v13 6/7] rust: ww_mutex: implement DynLockSet
Hi Onur,
Apologies for late reply, got wrapped up in other things.
Quoted 124 lines · Onur Özkan
Quoted 45 lines · On Wed, 11 Nov 2026 20:55:29 +0100 Beata Michalska <[email protected]>…
Hi Onur,
Quoted 42 lines · Onur Özkan
DynLockSet is a high-level and safe API built on top of
ww_mutexfor dynamically discovered locks. It keeps acquired locks held, handles repeated objects and provides access to their data.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.
Signed-off-by Onur Özkan <[email protected]>--- rust/kernel/sync/lock/ww_mutex.rs | 4 + .../sync/lock/ww_mutex/acquire_ctx_backoff.rs | 138 ++++++- .../kernel/sync/lock/ww_mutex/dyn_lock_set.rs | 351 ++++++++++++++++++ 3 files changed, 489 insertions(+), 4 deletions(-) create mode 100644 rust/kernel/sync/lock/ww_mutex/dyn_lock_set.rsrust/kernel/sync/lock/ww_mutex.rs +4
-0@@ -7,6 +7,8 @@//! [`AcquireCtx`] to track ordering and ensure forward progress. //! //! [`ww_transaction`] provides automatic deadlock recovery for ordinary guards. +//! Use [`DynLockSet::ww_transaction`] for dynamically discovered locks and +//! separate acquisition and work callbacks. //! //! See srctree/Documentation/locking/ww-mutex-design.rst for more details. @@ -28,10 +30,12 @@ AcquireCtxBackoff, // };pub use class::Class ; +pub use dyn_lock_set::DynLockSet ;mod acquire_ctx;mod acquire_ctx_backoff;mod class; +mod dyn_lock_set;/// A wound-wait (ww) mutex that is powered with deadlock avoidance [...]
Quoted 69 lines
Quoted 49 lines
rust/kernel/sync/lock/ww_mutex.rs +49
-0+ +/// Acquires a dynamic set, retrying on deadlock then runs `work` once. +/// +/// Each call releases existing locks and starts a new transaction. Retries keep +/// the same ticket. The set retains its allocation across retries and calls. +/// +/// `work` runs once after acquisition and must not acquire more locks. Its errors +/// are returned without retrying. All locks and pending backoff state are released +/// on return. The callbacks must not change the set's context or nest transactions +/// on that context. +/// +/// # Panics +/// +/// Panics if the context holds caller-owned locks outside this set at a transaction +/// boundary, including forgotten guards. +/// +/// # Examples +/// +/// ``` +/// use kernel::{ +/// define_ww_class, +/// prelude::*, +/// sync::lock::ww_mutex::{ +/// AcquireCtx, +/// AcquireCtxBackoff, +/// DynLockSet, +/// Mutex, // +/// }, // +/// }; +/// use pin_init::stack_pin_init; +/// +/// define_ww_class!(CLASS); +/// +/// stack_pin_init!(let first = Mutex::new(1, &CLASS)); +/// stack_pin_init!(let second = Mutex::new(2, &CLASS)); +/// +/// stack_pin_init!(let acquire_ctx = AcquireCtx::new(&CLASS)); +/// let ctx = AcquireCtxBackoff::new(acquire_ctx.as_mut()); +/// let mut set = DynLockSet::new(&ctx); +/// +/// set.ww_transaction( +/// |set| { +/// set.lock(&first)?; +/// +/// if set.with_locked(&first, |value| *value > 0)? { +/// set.lock(&second)?; +/// } +/// +/// Ok(()) rust/kernel/sync/lock/ww_mutex.rs
Quoted 10 lines
rust/kernel/sync/lock/ww_mutex.rs +10
-0+/// }, +/// |set| set.with_locked(&first, |value| *value += 1), +/// )?; +/// # Ok::<(), Error>(()) +/// ``` +pub fn ww_transaction <R >( + &mut self, +mut acquire:impl FnMut (&mut Self ) ->Result , + work:impl FnOnce (&mut Self ) ->Result <R >, + ) ->Result <R > {So I have been pondering a bit on that one, in a context of
drm_execand potentially having a Rust version ofdrm_gpuvm_exec_lock& co, and I'm having some doubts really. Thedrm_execimplementation, apart from locking and reserving fences, also plays with thedrm_gem_objectreference counting. Thedrm_execpairs each tracked lock with a GEM reference and releases both during backoff or cleanup. Proposed implementation makes it rather cumbersome to drop the reference when unlocking the mutex on a rollback (or backoff in this series terminology). I assume that would require some sort of extension to theDynLockSet? Or am I missing smth ?Quoted 3 lines
---
BR
Beata
Quoted 7 lines
Current API tracks the locks but leaves ownership of the objects to the caller and, yes, we need some sort of extension on DynLockSet probably together with AcqureCtx and its wrapper type.
Does current API limit your GPUVM use case? Could you share an example of the flow you need? I would be happy to extend the API if that's needed.
So what I have been looking into, is locking selected BO's - which is where the DynLockSet comes into play. The tricky bit is when we need/should pair each lock taken with bumped reference count on the object itself, and drop that reference on releasing the lock. And as the first step is pretty straightforward, the second is the one that needs a bit of attention. That said, It has been suggested (credits for Gary) that we could simply bump the reference count before entering the retry loop, so we do not have to go back and forth on each retry.
I still need to explore the viability of this approach but it looks like it is worth the shot.
---
BR
Beata
Quoted 56 lines
Thanks,
OnurQuoted 27 lines
Quoted 27 lines
rust/kernel/sync/lock/ww_mutex.rs +26
-0+let ctx = self.ctx; + self.clear (); + ctx.restart (); + +let result =retry (ctx, || { +if let Some (guard) = self.ctx.take_contended_guard () { + self.add_guard (guard)?; + } + +let result =acquire (self); +if result.is_err () { + self.clear (); + } + + result + }); + +let result = result.and_then (|()|work (self)); + + self.clear (); + ctx.clear_backoff (); + ctx.assert_no_locks_held (); + + result + } +[...]
Quoted 23 lines
Quoted 23 lines
rust/kernel/sync/lock/ww_mutex.rs +19
-0+ +assert_eq! (set.with_locked (&mutex, |v| *v),Err (EINVAL )); + +Ok (()) + } + +#[test] +fn test_class_mismatch () ->Result { +stack_pin_init! (let mutex =Mutex ::new (5 , &TEST_WOUND_WAIT_CLASS )); +stack_pin_init! (let acquire_ctx =AcquireCtx ::new (&TEST_WAIT_DIE_CLASS )); +let ctx =AcquireCtxBackoff ::new (acquire_ctx.as_mut ()); +let mut set =DynLockSet ::new (&ctx); + +assert_eq! (set.lock (&mutex),Err (EINVAL )); +assert! (!mutex.is_locked ()); + +Ok (()) + } +}Signature · 1 line
2.51.2