From nobody Tue Dec 16 07:27:47 2025 Received: from forward101d.mail.yandex.net (forward101d.mail.yandex.net [178.154.239.212]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 0BCBC25D536; Sat, 1 Nov 2025 16:11:12 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=178.154.239.212 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1762013477; cv=none; b=E5kvDmHyagIZ8YGIHvMZ+1dKDEdaHY7Wbk4aO52mPPtCA0SYX2OMBTQAy0V15U7eZC0dQ7MLvT9qDNGbczi4Io4m+y6m8NCSptYtJqXkCrH7NhF6akcZsUFcYExkAv4f2g3N21IwITpUKcuHWXJDVOwhhp/Mk8l8OmJ6Z1+DrYE= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1762013477; c=relaxed/simple; bh=vG9lfhUCRakuAGuT3dA0QBxjkAp8v6T2Rb+b5FMFZgg=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=Hq0dxi0VJ2hOphIiW9+XBW0xb/AFB+FtU3BVGlh5nHneYne3POEer9Hc9a3vfRwZZWZbZrTiY4X3fj2KWTz2DcZ0AhGY6/EhcDkirLS6QI0C3ZgwJDnhuwrdeZmwkS6Na7NlS5xZgp/093NkrgzSaiSsIG4aGlq14kJOx/HQVu8= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=onurozkan.dev; spf=pass smtp.mailfrom=onurozkan.dev; dkim=pass (1024-bit key) header.d=onurozkan.dev header.i=@onurozkan.dev header.b=TZuAQ2Nc; arc=none smtp.client-ip=178.154.239.212 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=onurozkan.dev Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=onurozkan.dev Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=onurozkan.dev header.i=@onurozkan.dev header.b="TZuAQ2Nc" Received: from mail-nwsmtp-smtp-production-main-99.klg.yp-c.yandex.net (mail-nwsmtp-smtp-production-main-99.klg.yp-c.yandex.net [IPv6:2a02:6b8:c42:d7c6:0:640:5e67:0]) by forward101d.mail.yandex.net (Yandex) with ESMTPS id C0F39C0041; Sat, 01 Nov 2025 19:11:08 +0300 (MSK) Received: by mail-nwsmtp-smtp-production-main-99.klg.yp-c.yandex.net (smtp/Yandex) with ESMTPSA id wAkixRhLDOs0-FPxOZO3o; Sat, 01 Nov 2025 19:11:07 +0300 X-Yandex-Fwd: 1 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=onurozkan.dev; s=mail; t=1762013467; bh=KtbJ7gwd1xqf54f0xvnpxlmrj7mnWdH6IdsdCodutvg=; h=Cc:Message-ID:References:Date:In-Reply-To:Subject:To:From; b=TZuAQ2NcRlTqhLImf2pTVru6k5a1BLPT9UmuuiTPpQ+DAEkbMwRTA3aH2kJZ6kVN1 IBLslnvd7QAbqDxannDTfXTqT5hhYpWOwWZlM7FI3P9IPOWBqS0rarMLDcmxlvVoJR d/48RCrLvty4Q1e3Nod5+c509xv0C8RbWy1IdkjE= Authentication-Results: mail-nwsmtp-smtp-production-main-99.klg.yp-c.yandex.net; dkim=pass header.i=@onurozkan.dev From: =?UTF-8?q?Onur=20=C3=96zkan?= To: rust-for-linux@vger.kernel.org Cc: 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, daniel.almeida@collabora.com, linux-kernel@vger.kernel.org, =?UTF-8?q?Onur=20=C3=96zkan?= Subject: [PATCH v7 1/6] rust: add C wrappers for ww_mutex inline functions Date: Sat, 1 Nov 2025 19:10:51 +0300 Message-ID: <20251101161056.22408-2-work@onurozkan.dev> X-Mailer: git-send-email 2.51.2 In-Reply-To: <20251101161056.22408-1-work@onurozkan.dev> References: <20251101161056.22408-1-work@onurozkan.dev> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable 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. Signed-off-by: Onur =C3=96zkan Reviewed-by: Daniel Almeida Reviewed-by: Lyude Paul --- MAINTAINERS | 1 + rust/helpers/helpers.c | 1 + rust/helpers/ww_mutex.c | 39 +++++++++++++++++++++++++++++++++++++++ 3 files changed, 41 insertions(+) create mode 100644 rust/helpers/ww_mutex.c diff --git a/MAINTAINERS b/MAINTAINERS index 545a4776795e..03dca09d189a 100644 --- a/MAINTAINERS +++ b/MAINTAINERS @@ -14527,6 +14527,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 diff --git a/rust/helpers/helpers.c b/rust/helpers/helpers.c index 551da6c9b506..58cf14d74516 100644 --- a/rust/helpers/helpers.c +++ b/rust/helpers/helpers.c @@ -59,4 +59,5 @@ #include "vmalloc.c" #include "wait.c" #include "workqueue.c" +#include "ww_mutex.c" #include "xarray.c" diff --git a/rust/helpers/ww_mutex.c b/rust/helpers/ww_mutex.c new file mode 100644 index 000000000000..61a487653394 --- /dev/null +++ b/rust/helpers/ww_mutex.c @@ -0,0 +1,39 @@ +// SPDX-License-Identifier: GPL-2.0 + +#include + +void rust_helper_ww_mutex_init(struct ww_mutex *lock, struct ww_class *ww_= class) +{ + ww_mutex_init(lock, ww_class); +} + +void rust_helper_ww_acquire_init(struct ww_acquire_ctx *ctx, struct ww_cla= ss *ww_class) +{ + ww_acquire_init(ctx, ww_class); +} + +void rust_helper_ww_acquire_done(struct ww_acquire_ctx *ctx) +{ + ww_acquire_done(ctx); +} + +void rust_helper_ww_acquire_fini(struct ww_acquire_ctx *ctx) +{ + ww_acquire_fini(ctx); +} + +void rust_helper_ww_mutex_lock_slow(struct ww_mutex *lock, struct ww_acqui= re_ctx *ctx) +{ + ww_mutex_lock_slow(lock, ctx); +} + +int rust_helper_ww_mutex_lock_slow_interruptible(struct ww_mutex *lock, st= ruct ww_acquire_ctx *ctx) +{ + return ww_mutex_lock_slow_interruptible(lock, ctx); +} + +bool rust_helper_ww_mutex_is_locked(struct ww_mutex *lock) +{ + return ww_mutex_is_locked(lock); +} + --=20 2.51.2 From nobody Tue Dec 16 07:27:47 2025 Received: from forward103d.mail.yandex.net (forward103d.mail.yandex.net [178.154.239.214]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id BE53C22172E; Sat, 1 Nov 2025 16:11:15 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=178.154.239.214 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1762013478; cv=none; b=pbAkvzjBbkWunWnB/4qWhBEttUgBJLZjlNQvnheQMUVlbUmBPXGCvza7yvuU3efmaFr0waP/OmXvXb5E+B53wlsCDNcPzl/8FT5gCDzcyV38+Pwpl472vpTrRK7czwUCRl2r0RKwanWO54yCBXilwaqhWitoYQXAZUODrvcn3qI= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1762013478; c=relaxed/simple; bh=g/y0VSXvPRU/zyilBUexl1Kwo2HCQ6otTjPXzT6Httw=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=eBODfkAMW6/BrIhH8s+0gKz0txCVo1aWfpAidCsuzN2BE13NrM1Nbd2EaZ2bItYHULlFeV0lZj0uhEX1Ikm4t+74kELmda2TQUrr71ItKI9seTAGZaWIW1AJCTJYT4UI6DlnxPnvhQ4hiKiBZ/CJO66gBkwFPFR0graUSoemtpI= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=onurozkan.dev; spf=pass smtp.mailfrom=onurozkan.dev; dkim=pass (1024-bit key) header.d=onurozkan.dev header.i=@onurozkan.dev header.b=mlDvymYV; arc=none smtp.client-ip=178.154.239.214 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=onurozkan.dev Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=onurozkan.dev Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=onurozkan.dev header.i=@onurozkan.dev header.b="mlDvymYV" Received: from mail-nwsmtp-smtp-production-main-99.klg.yp-c.yandex.net (mail-nwsmtp-smtp-production-main-99.klg.yp-c.yandex.net [IPv6:2a02:6b8:c42:d7c6:0:640:5e67:0]) by forward103d.mail.yandex.net (Yandex) with ESMTPS id D9FBDC00CE; Sat, 01 Nov 2025 19:11:11 +0300 (MSK) Received: by mail-nwsmtp-smtp-production-main-99.klg.yp-c.yandex.net (smtp/Yandex) with ESMTPSA id wAkixRhLDOs0-fEWxwdXz; Sat, 01 Nov 2025 19:11:10 +0300 X-Yandex-Fwd: 1 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=onurozkan.dev; s=mail; t=1762013471; bh=87A9u2bi5X8AtzknDv82nR70wZ9vXkMZioJ4Ntio0pA=; h=Cc:Message-ID:References:Date:In-Reply-To:Subject:To:From; b=mlDvymYV07haU9HEi3nU1x8vnvzL7Bapg14LGeU5itu42Rwu1va23O45zARZeDADh dgYXvTmxNrKJ6b9Y/QLzd/sZx68unKGWGqM5gVpQxQHpGBlHynPXDVFApmg1tK0JtX EQ3bUM9+tTrvuLzb4z2VVIykDPnczMu84TrQWWs8= Authentication-Results: mail-nwsmtp-smtp-production-main-99.klg.yp-c.yandex.net; dkim=pass header.i=@onurozkan.dev From: =?UTF-8?q?Onur=20=C3=96zkan?= To: rust-for-linux@vger.kernel.org Cc: 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, daniel.almeida@collabora.com, linux-kernel@vger.kernel.org, =?UTF-8?q?Onur=20=C3=96zkan?= Subject: [PATCH v7 2/6] rust: implement `Class` for ww_class support Date: Sat, 1 Nov 2025 19:10:52 +0300 Message-ID: <20251101161056.22408-3-work@onurozkan.dev> X-Mailer: git-send-email 2.51.2 In-Reply-To: <20251101161056.22408-1-work@onurozkan.dev> References: <20251101161056.22408-1-work@onurozkan.dev> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Adds 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_class!` macro for safely declaring static instances. Signed-off-by: Onur =C3=96zkan Reviewed-by: Daniel Almeida Reviewed-by: Lyude Paul --- rust/kernel/sync/lock.rs | 1 + rust/kernel/sync/lock/ww_mutex.rs | 7 ++ rust/kernel/sync/lock/ww_mutex/class.rs | 156 ++++++++++++++++++++++++ 3 files changed, 164 insertions(+) create mode 100644 rust/kernel/sync/lock/ww_mutex.rs create mode 100644 rust/kernel/sync/lock/ww_mutex/class.rs diff --git a/rust/kernel/sync/lock.rs b/rust/kernel/sync/lock.rs index 27202beef90c..5b320c2b28c1 100644 --- a/rust/kernel/sync/lock.rs +++ b/rust/kernel/sync/lock.rs @@ -15,6 +15,7 @@ =20 pub mod mutex; pub mod spinlock; +pub mod ww_mutex; =20 pub(super) mod global; pub use global::{GlobalGuard, GlobalLock, GlobalLockBackend, GlobalLockedB= y}; diff --git a/rust/kernel/sync/lock/ww_mutex.rs b/rust/kernel/sync/lock/ww_m= utex.rs new file mode 100644 index 000000000000..727c51cc73af --- /dev/null +++ b/rust/kernel/sync/lock/ww_mutex.rs @@ -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; diff --git a/rust/kernel/sync/lock/ww_mutex/class.rs b/rust/kernel/sync/loc= k/ww_mutex/class.rs new file mode 100644 index 000000000000..d10db5da01d2 --- /dev/null +++ b/rust/kernel/sync/lock/ww_mutex/class.rs @@ -0,0 +1,156 @@ +// SPDX-License-Identifier: GPL-2.0 + +//! Provides [`Class`] to group wound/wait mutexes to be acquired together +//! and specifies which deadlock avoidance algorithm to use (e.g., wound-w= ait +//! or wait-die). +//! +//! The [`define_class`] macro and [`Class::new_wait_die`]/[`Class::new_wo= und_wait`] +//! constructors provide safe ways to create classes. + +use crate::bindings; +use crate::prelude::*; +use crate::types::Opaque; + +/// Creates static [`Class`] instances. +/// +/// # Examples +/// +/// ``` +/// use kernel::{c_str, define_class}; +/// +/// define_class!(WOUND_WAIT_GLOBAL_CLASS, wound_wait, c_str!("wound_wait_= global_class")); +/// define_class!(WAIT_DIE_GLOBAL_CLASS, wait_die, c_str!("wait_die_global= _class")); +/// ``` +#[macro_export] +macro_rules! define_class { + ($name:ident, wound_wait, $class_name:expr) =3D> { + static $name: $crate::sync::lock::ww_mutex::Class =3D + // SAFETY: This is `static`, so address is fixed and won't mov= e. + unsafe { $crate::sync::lock::ww_mutex::Class::unpinned_new($cl= ass_name, false) }; + }; + ($name:ident, wait_die, $class_name:expr) =3D> { + static $name: $crate::sync::lock::ww_mutex::Class =3D + // SAFETY: This is `static`, so address is fixed and won't mov= e. + unsafe { $crate::sync::lock::ww_mutex::Class::unpinned_new($cl= ass_name, true) }; + }; +} + +/// Used to group mutexes together for deadlock avoidance. +/// +/// All mutexes that might be acquired together should use the same class. +/// +/// # Examples +/// +/// ``` +/// use kernel::sync::lock::ww_mutex::Class; +/// use kernel::c_str; +/// use pin_init::stack_pin_init; +/// +/// stack_pin_init!(let _wait_die_class =3D Class::new_wait_die(c_str!("so= me_class"))); +/// stack_pin_init!(let _wound_wait_class =3D Class::new_wound_wait(c_str!= ("some_other_class"))); +/// +/// # Ok::<(), Error>(()) +/// ``` +#[pin_data] +#[repr(transparent)] +pub struct Class { + #[pin] + pub(super) inner: Opaque, +} + +// SAFETY: [`Class`] is set up once and never modified. It's fine to share= it across threads. +unsafe impl Sync for Class {} +// SAFETY: Doesn't hold anything thread-specific. It's safe to send to oth= er threads. +unsafe impl Send for Class {} + +impl Class { + /// Creates an unpinned [`Class`]. + /// + /// # Safety + /// + /// Caller must guarantee that the returned value is not moved after c= reation. + pub const unsafe fn unpinned_new(name: &'static CStr, is_wait_die: boo= l) -> Self { + Class { + inner: Opaque::new(bindings::ww_class { + stamp: bindings::atomic_long_t { counter: 0 }, + acquire_name: name.as_char_ptr(), + mutex_name: name.as_char_ptr(), + is_wait_die: is_wait_die as u32, + // TODO: Replace with `bindings::lock_class_key::default()= ` once + // stabilized for `const`. + // + // SAFETY: This is always zero-initialized when defined wi= th + // `DEFINE_WD_CLASS` globally on C side. + // + // For reference, see __WW_CLASS_INITIALIZER() in + // "include/linux/ww_mutex.h". + acquire_key: unsafe { core::mem::zeroed() }, + // TODO: Replace with `bindings::lock_class_key::default()= ` once + // stabilized for `const`. + // + // SAFETY: This is always zero-initialized when defined wi= th + // `DEFINE_WD_CLASS` globally on C side. + // + // For reference, see __WW_CLASS_INITIALIZER() in + // "include/linux/ww_mutex.h". + mutex_key: unsafe { core::mem::zeroed() }, + }), + } + } + + /// Creates a [`Class`]. + /// + /// You should not use this function directly. Use the [`define_class!= `] + /// macro or call [`Class::new_wait_die`] or [`Class::new_wound_wait`]= instead. + fn new(name: &'static CStr, is_wait_die: bool) -> impl PinInit { + pin_init! { + Self { + inner <- Opaque::ffi_init(|slot: *mut bindings::ww_class| { + // SAFETY: The fields are being initialized. The `name= ` pointer is valid for a + // static lifetime. The keys are zeroed, which is what= the C side does. + unsafe { + slot.write(bindings::ww_class { + stamp: bindings::atomic_long_t { counter: 0 }, + acquire_name: name.as_char_ptr(), + mutex_name: name.as_char_ptr(), + is_wait_die: is_wait_die.into(), + // TODO: Replace with `bindings::lock_class_ke= y::default()` once + // stabilized for `const`. + // + // SAFETY: This is always zero-initialized whe= n defined with + // `DEFINE_WD_CLASS` globally on C side. + // + // For reference, see __WW_CLASS_INITIALIZER()= in + // "include/linux/ww_mutex.h". + acquire_key: core::mem::zeroed(), + mutex_key: core::mem::zeroed(), + }); + } + }), + } + } + } + + /// Creates wait-die [`Class`]. + pub fn new_wait_die(name: &'static CStr) -> impl PinInit { + Self::new(name, true) + } + + /// Creates wound-wait [`Class`]. + pub fn new_wound_wait(name: &'static CStr) -> impl PinInit { + Self::new(name, false) + } + + /// Creates a `Class` from a raw pointer. + /// + /// This function is intended for interoperability with C code. + /// + /// # Safety + /// + /// The caller must ensure that `ptr` points to the `inner` field of + /// [`Class`] and that it remains valid for the lifetime `'a`. + pub const unsafe fn from_raw<'a>(ptr: *mut bindings::ww_class) -> &'a = Self { + // SAFETY: By the safety contract, `ptr` is valid to construct `Cl= ass`. + unsafe { &*ptr.cast() } + } +} --=20 2.51.2 From nobody Tue Dec 16 07:27:47 2025 Received: from forward102d.mail.yandex.net (forward102d.mail.yandex.net [178.154.239.213]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 0EA9925D209; Sat, 1 Nov 2025 16:11:17 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=178.154.239.213 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1762013480; cv=none; b=t6gIOZyi0rNpa0/c6LF/tq7oVpy/xac2j9auQwz7egnI4+5HGvF+H+kJMBPgbi4K0QQiLZPSDrXVibdQgM7/orxmfL+e3UeLoOIuXp3zqlZ7+Z2ewnycLng389MwwY30KZgoWlpd6FQ3ZSg/k6r4PYMglfZFSeaA7oejWF2qW7Y= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1762013480; c=relaxed/simple; bh=8qpHjS58yRnMjonBxk24D7YNjaxNwLJ18ZG97fwDO+A=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=M+fGH5NLTVsukluC4hdOntWTLp49KXSHc0v7um5aeROuT4SYEupXhwYnZ6RjvfWxp4Fx8TiQ/OwN0fv/+KlZX2le9QPTtFufhUlOBiFbRD2p7SLrNDAAkTIgNVYPnCJRv+vNbzFuNF5ap8nyYUKypEWOYC/0uuJ5VmkLg+GBRDc= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=onurozkan.dev; spf=pass smtp.mailfrom=onurozkan.dev; dkim=pass (1024-bit key) header.d=onurozkan.dev header.i=@onurozkan.dev header.b=GEOGDbAv; arc=none smtp.client-ip=178.154.239.213 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=onurozkan.dev Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=onurozkan.dev Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=onurozkan.dev header.i=@onurozkan.dev header.b="GEOGDbAv" Received: from mail-nwsmtp-smtp-production-main-99.klg.yp-c.yandex.net (mail-nwsmtp-smtp-production-main-99.klg.yp-c.yandex.net [IPv6:2a02:6b8:c42:d7c6:0:640:5e67:0]) by forward102d.mail.yandex.net (Yandex) with ESMTPS id 50F10C000D; Sat, 01 Nov 2025 19:11:15 +0300 (MSK) Received: by mail-nwsmtp-smtp-production-main-99.klg.yp-c.yandex.net (smtp/Yandex) with ESMTPSA id wAkixRhLDOs0-1ieWV4zK; Sat, 01 Nov 2025 19:11:14 +0300 X-Yandex-Fwd: 1 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=onurozkan.dev; s=mail; t=1762013474; bh=yFK6lRt2ssfqukpmgrenlQa2EpfYl8xx6d3OUJKDPLc=; h=Cc:Message-ID:References:Date:In-Reply-To:Subject:To:From; b=GEOGDbAvp4ONdbyrlH93ZEKVQJOjifOaWYp0B3bX64Pvzdi3m5P8oV0jt9CbsE7Au T4GR2bzQae21vNdGW0+J8urP2zF/30Tl0C2ZVIcxukvPu5PYodTSJr8DuSACIAijGL 2Pktv5kQebYG0Q/3PLvvGpm3Z/e69ZoP2pAuJmtQ= Authentication-Results: mail-nwsmtp-smtp-production-main-99.klg.yp-c.yandex.net; dkim=pass header.i=@onurozkan.dev From: =?UTF-8?q?Onur=20=C3=96zkan?= To: rust-for-linux@vger.kernel.org Cc: 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, daniel.almeida@collabora.com, linux-kernel@vger.kernel.org, =?UTF-8?q?Onur=20=C3=96zkan?= Subject: [PATCH v7 3/6] rust: error: add EDEADLK Date: Sat, 1 Nov 2025 19:10:53 +0300 Message-ID: <20251101161056.22408-4-work@onurozkan.dev> X-Mailer: git-send-email 2.51.2 In-Reply-To: <20251101161056.22408-1-work@onurozkan.dev> References: <20251101161056.22408-1-work@onurozkan.dev> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable This is needed for the ww_mutex implementation so we can handle EDEADLK on lock attempts. Signed-off-by: Onur =C3=96zkan Reviewed-by: Daniel Almeida Reviewed-by: Lyude Paul --- rust/kernel/error.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/rust/kernel/error.rs b/rust/kernel/error.rs index 1c0e0e241daa..9bf1072cfe19 100644 --- a/rust/kernel/error.rs +++ b/rust/kernel/error.rs @@ -66,6 +66,7 @@ macro_rules! declare_err { declare_err!(EPIPE, "Broken pipe."); declare_err!(EDOM, "Math argument out of domain of func."); declare_err!(ERANGE, "Math result not representable."); + declare_err!(EDEADLK, "Resource deadlock avoided."); declare_err!(EOVERFLOW, "Value too large for defined data type."); declare_err!(ETIMEDOUT, "Connection timed out."); declare_err!(ERESTARTSYS, "Restart the system call."); --=20 2.51.2 From nobody Tue Dec 16 07:27:47 2025 Received: from forward101d.mail.yandex.net (forward101d.mail.yandex.net [178.154.239.212]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id C82D71DFE26; Sat, 1 Nov 2025 16:11:20 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=178.154.239.212 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1762013483; cv=none; b=LKjGNGsnJhKDTRh2R5DCLikuX5I6FRpmLYe24bEvzmTFqbfDTl8gifvZbMoEnUtdSfnahJ6t9Vt+4Ai6qmWFlL8d6PD8fvkHq/evIbC8y7AWD/9ZUcx5HqEzgCghFPyguvZMBQ/XCr0uTpurLe/FFvFIN7U1w7LLrIAwTldgX7w= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1762013483; c=relaxed/simple; bh=pQZ1MaIlcBXVwJxO4wZy9IihKHbiQd8gAWLdk6As2pg=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=cBYdRgQOUFzbxK5/+QMoKBfyYKPZL1gh74aH3FGWCeegtXBUybM4djas3va1SWBT2ab/iw4zZ5RPuuZF0a2XBdS89PkYhK/lKuWSb16Bvr+o9hv1HsM8mgNC85Jllf3nMC4UIgaiGnCKheMLQU1/eCuerD4PsCXPCTr0cE243lg= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=onurozkan.dev; spf=pass smtp.mailfrom=onurozkan.dev; dkim=pass (1024-bit key) header.d=onurozkan.dev header.i=@onurozkan.dev header.b=FCAatfVT; arc=none smtp.client-ip=178.154.239.212 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=onurozkan.dev Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=onurozkan.dev Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=onurozkan.dev header.i=@onurozkan.dev header.b="FCAatfVT" Received: from mail-nwsmtp-smtp-production-main-99.klg.yp-c.yandex.net (mail-nwsmtp-smtp-production-main-99.klg.yp-c.yandex.net [IPv6:2a02:6b8:c42:d7c6:0:640:5e67:0]) by forward101d.mail.yandex.net (Yandex) with ESMTPS id 5A126C0084; Sat, 01 Nov 2025 19:11:18 +0300 (MSK) Received: by mail-nwsmtp-smtp-production-main-99.klg.yp-c.yandex.net (smtp/Yandex) with ESMTPSA id wAkixRhLDOs0-AQDSChSN; Sat, 01 Nov 2025 19:11:17 +0300 X-Yandex-Fwd: 1 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=onurozkan.dev; s=mail; t=1762013477; bh=dAkHDegiGh4VGjRaX+pxYFkxfd7h4NKoi/+XtBkhWuw=; h=Cc:Message-ID:References:Date:In-Reply-To:Subject:To:From; b=FCAatfVTG1I0K+iR72eG+T+0RQVw70mAXechJI/d4uwoXOC+6NhAM1RCzMvVUXhBm v54FyvYvXHtfZ0A/IybQ5V+T+NQKxJ/oU31kKNDyHbixm3ppB66K8N53k/QZHIyiL7 gk/U0pJBacSZ9qchjiNF2pnDJnff01tVmiNtdOPU= Authentication-Results: mail-nwsmtp-smtp-production-main-99.klg.yp-c.yandex.net; dkim=pass header.i=@onurozkan.dev From: =?UTF-8?q?Onur=20=C3=96zkan?= To: rust-for-linux@vger.kernel.org Cc: 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, daniel.almeida@collabora.com, linux-kernel@vger.kernel.org, =?UTF-8?q?Onur=20=C3=96zkan?= Subject: [PATCH v7 4/6] rust: ww_mutex: add Mutex, AcquireCtx and MutexGuard Date: Sat, 1 Nov 2025 19:10:54 +0300 Message-ID: <20251101161056.22408-5-work@onurozkan.dev> X-Mailer: git-send-email 2.51.2 In-Reply-To: <20251101161056.22408-1-work@onurozkan.dev> References: <20251101161056.22408-1-work@onurozkan.dev> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Implements full locking API (lock, try_lock, slow path, interruptible variants) and integration with kernel bindings. Signed-off-by: Onur =C3=96zkan --- rust/kernel/sync/lock/ww_mutex.rs | 276 ++++++++++++++++++ rust/kernel/sync/lock/ww_mutex/acquire_ctx.rs | 211 +++++++++++++ 2 files changed, 487 insertions(+) create mode 100644 rust/kernel/sync/lock/ww_mutex/acquire_ctx.rs diff --git a/rust/kernel/sync/lock/ww_mutex.rs b/rust/kernel/sync/lock/ww_m= utex.rs index 727c51cc73af..2a9c1c20281b 100644 --- a/rust/kernel/sync/lock/ww_mutex.rs +++ b/rust/kernel/sync/lock/ww_mutex.rs @@ -1,7 +1,283 @@ // SPDX-License-Identifier: GPL-2.0 =20 //! 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. =20 +use crate::error::to_result; +use crate::prelude::*; +use crate::types::{NotThreadSafe, Opaque}; +use crate::{bindings, container_of}; + +use core::cell::UnsafeCell; +use core::marker::PhantomData; + +pub use acquire_ctx::AcquireCtx; pub use class::Class; =20 +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::c_str; +/// use kernel::sync::Arc; +/// use kernel::sync::lock::ww_mutex::{AcquireCtx, Class, Mutex}; +/// use pin_init::stack_pin_init; +/// +/// stack_pin_init!(let class =3D Class::new_wound_wait(c_str!("some_class= "))); +/// let mutex =3D Arc::pin_init(Mutex::new(42, &class), GFP_KERNEL)?; +/// +/// let ctx =3D KBox::pin_init(AcquireCtx::new(&class), GFP_KERNEL)?; +/// +/// // SAFETY: Both `ctx` and `mutex` uses the same class. +/// let guard =3D unsafe { ctx.lock(&mutex)? }; +/// assert_eq!(*guard, 42); +/// +/// # Ok::<(), Error>(()) +/// ``` +#[pin_data] +pub struct Mutex<'a, T: ?Sized> { + #[pin] + inner: Opaque, + _p: PhantomData<&'a Class>, + data: UnsafeCell, +} + +// SAFETY: `Mutex` can be sent to another thread if the protected +// data `T` can be. +unsafe impl Send for Mutex<'_, T> {} + +// SAFETY: `Mutex` can be shared across threads if the protected +// data `T` can be. +unsafe impl Sync for Mutex<'_, T> {} + +impl<'class, T> Mutex<'class, T> { + /// Initializes [`Mutex`] with the given `data` and [`Class`]. + pub fn new(data: T, class: &'class Class) -> impl PinInit { + let class_ptr =3D class.inner.get(); + pin_init!(Mutex { + inner <- Opaque::ffi_init(|slot: *mut bindings::ww_mutex| { + // SAFETY: `class` is valid for the lifetime `'class` capt= ured by `Self`. + unsafe { bindings::ww_mutex_init(slot, class_ptr) } + }), + data: UnsafeCell::new(data), + _p: PhantomData + }) + } +} + +impl<'class> Mutex<'class, ()> { + /// 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_mute= x` + /// and that it remains valid for the lifetime `'a`. + pub unsafe fn from_raw<'a>(ptr: *mut bindings::ww_mutex) -> &'a Self { + // SAFETY: By the safety contract, the caller guarantees that `ptr` + // points to a valid `ww_mutex` which is the `inner` field of `Mut= ex` + // and remains valid for the lifetime `'a`. + unsafe { &*container_of!(Opaque::cast_from(ptr), Self, inner) } + } +} + +impl<'class, T: ?Sized> Mutex<'class, T> { + /// Checks if the mutex is currently locked. + pub fn is_locked(&self) -> bool { + // SAFETY: The mutex is pinned and valid. + unsafe { bindings::ww_mutex_is_locked(self.inner.get()) } + } + + /// Locks the given mutex without acquire context ([`AcquireCtx`]). + pub fn lock<'a>(&'a self) -> Result> { + // SAFETY: `ctx` is `None`, so no class matching is required. + unsafe { lock_common(self, None, LockKind::Regular) } + } + + /// Similar to `lock`, but can be interrupted by signals. + pub fn lock_interruptible<'a>(&'a self) -> Result> { + // SAFETY: `ctx` is `None`, so no class matching is required. + unsafe { lock_common(self, None, LockKind::Interruptible) } + } + + /// Locks the given mutex without acquire context ([`AcquireCtx`]) usi= ng the slow path. + /// + /// This function should be used when `lock` fails (typically due to a= potential deadlock). + pub fn lock_slow<'a>(&'a self) -> Result> { + // SAFETY: `ctx` is `None`, so no class matching is required. + unsafe { lock_common(self, None, LockKind::Slow) } + } + + /// Similar to `lock_slow`, but can be interrupted by signals. + pub fn lock_slow_interruptible<'a>(&'a self) -> Result> { + // SAFETY: `ctx` is `None`, so no class matching is required. + unsafe { lock_common(self, None, LockKind::SlowInterruptible) } + } + + /// Tries to lock the mutex with no [`AcquireCtx`] and without blockin= g. + /// + /// Unlike `lock`, no deadlock handling is performed. + pub fn try_lock<'a>(&'a self) -> Result> { + // SAFETY: `ctx` is `None`, so no class matching is required. + unsafe { lock_common(self, None, LockKind::Try) } + } +} + +/// 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 loc= k is held +/// for the entire lifetime of this guard and is automatically released wh= en the +/// guard is dropped. +#[must_use =3D "the lock unlocks immediately when the guard is unused"] +pub struct MutexGuard<'a, T: ?Sized> { + mutex: &'a Mutex<'a, T>, + _not_send: NotThreadSafe, +} + +// SAFETY: [`MutexGuard`] can be shared between threads if the data can. +unsafe impl Sync for MutexGuard<'_, T> {} + +impl<'a, T: ?Sized> MutexGuard<'a, T> { + /// Creates a new guard for a locked mutex. + fn new(mutex: &'a Mutex<'a, T>) -> Self { + 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_mute= x` + /// and that it remains valid for the lifetime `'a`. + pub unsafe fn from_raw<'b>(ptr: *mut bindings::ww_mutex) -> MutexGuard= <'b, ()> { + // SAFETY: By the safety contract, the caller guarantees that `ptr` + // points to a valid `ww_mutex` which is the `mutex` field of `Mut= ex` + // and remains valid for the lifetime `'a`. + let mutex =3D unsafe { Mutex::from_raw(ptr) }; + + MutexGuard::new(mutex) + } +} + +impl core::ops::Deref for MutexGuard<'_, T> { + type Target =3D T; + + fn deref(&self) -> &Self::Target { + // SAFETY: We hold the lock, so we have exclusive access. + unsafe { &*self.mutex.data.get() } + } +} + +impl core::ops::DerefMut for MutexGuard<'_, T> { + fn deref_mut(&mut self) -> &mut Self::Target { + // SAFETY: We hold the lock, so we have exclusive access. + unsafe { &mut *self.mutex.data.get() } + } +} + +impl Drop for MutexGuard<'_, T> { + fn drop(&mut self) { + // SAFETY: We hold the lock and are about to release it. + unsafe { bindings::ww_mutex_unlock(self.mutex.inner.get()) }; + } +} + +/// Locking kinds used by [`lock_common`] to unify the internal +/// locking logic. +/// +/// It's best not to expose this type (and [`lock_common`]) to the +/// kernel, as it allows internal API changes without worrying +/// about breaking external compatibility. +#[derive(Copy, Clone, Debug)] +enum LockKind { + /// Blocks until lock is acquired. + Regular, + /// Blocks but can be interrupted by signals. + Interruptible, + /// Used in slow path after deadlock detection. + Slow, + /// Slow path but interruptible. + SlowInterruptible, + /// Does not block, returns immediately if busy. + Try, +} + +/// Internal helper that unifies the different locking kinds. +/// +/// # Safety +/// +/// If `ctx` is `Some`, the given `mutex` must be created with the [`Class= `] that +/// was used to initialize `ctx`. +unsafe fn lock_common<'a, T: ?Sized>( + mutex: &'a Mutex<'a, T>, + ctx: Option<&AcquireCtx<'_>>, + kind: LockKind, +) -> Result> { + let ctx_ptr =3D ctx.map_or(core::ptr::null_mut(), |c| c.inner.get()); + + let mutex_ptr =3D mutex.inner.get(); + + match kind { + LockKind::Regular =3D> { + // SAFETY: `Mutex` is always pinned. If `AcquireCtx` is `Some`= , it is pinned, + // if `None`, it is set to `core::ptr::null_mut()`. Both cases= are safe. + let ret =3D unsafe { bindings::ww_mutex_lock(mutex_ptr, ctx_pt= r) }; + + to_result(ret)?; + } + LockKind::Interruptible =3D> { + // SAFETY: `Mutex` is always pinned. If `AcquireCtx` is `Some`= , it is pinned, + // if `None`, it is set to `core::ptr::null_mut()`. Both cases= are safe. + let ret =3D unsafe { bindings::ww_mutex_lock_interruptible(mut= ex_ptr, ctx_ptr) }; + + to_result(ret)?; + } + LockKind::Slow =3D> { + // SAFETY: `Mutex` is always pinned. If `AcquireCtx` is `Some`= , it is pinned, + // if `None`, it is set to `core::ptr::null_mut()`. Both cases= are safe. + unsafe { bindings::ww_mutex_lock_slow(mutex_ptr, ctx_ptr) }; + } + LockKind::SlowInterruptible =3D> { + // SAFETY: `Mutex` is always pinned. If `AcquireCtx` is `Some`= , it is pinned, + // if `None`, it is set to `core::ptr::null_mut()`. Both cases= are safe. + let ret =3D unsafe { bindings::ww_mutex_lock_slow_interruptibl= e(mutex_ptr, ctx_ptr) }; + + to_result(ret)?; + } + LockKind::Try =3D> { + // SAFETY: `Mutex` is always pinned. If `AcquireCtx` is `Some`= , it is pinned, + // if `None`, it is set to `core::ptr::null_mut()`. Both cases= are safe. + let ret =3D unsafe { bindings::ww_mutex_trylock(mutex_ptr, ctx= _ptr) }; + + if ret =3D=3D 0 { + return Err(EBUSY); + } else { + to_result(ret)?; + } + } + }; + + Ok(MutexGuard::new(mutex)) +} diff --git a/rust/kernel/sync/lock/ww_mutex/acquire_ctx.rs b/rust/kernel/sy= nc/lock/ww_mutex/acquire_ctx.rs new file mode 100644 index 000000000000..65a955b5809d --- /dev/null +++ b/rust/kernel/sync/lock/ww_mutex/acquire_ctx.rs @@ -0,0 +1,211 @@ +// SPDX-License-Identifier: GPL-2.0 + +//! Provides [`AcquireCtx`] for managing multiple wound/wait +//! mutexes from the same [`Class`]. + +use crate::bindings; +use crate::prelude::*; +use crate::types::Opaque; + +use core::marker::PhantomData; + +use super::{lock_common, Class, LockKind, Mutex, MutexGuard}; + +/// Groups multiple [`Mutex`]es for deadlock avoidance when acquired +/// with the same [`Class`]. +/// +/// # Examples +/// +/// ``` +/// use kernel::sync::lock::ww_mutex::{Class, AcquireCtx, Mutex}; +/// use kernel::c_str; +/// use kernel::sync::Arc; +/// use pin_init::stack_pin_init; +/// +/// stack_pin_init!(let class =3D Class::new_wound_wait(c_str!("demo"))); +/// +/// // Create mutexes. +/// let mutex1 =3D Arc::pin_init(Mutex::new(1, &class), GFP_KERNEL)?; +/// let mutex2 =3D Arc::pin_init(Mutex::new(2, &class), GFP_KERNEL)?; +/// +/// // Create acquire context for deadlock avoidance. +/// let ctx =3D KBox::pin_init(AcquireCtx::new(&class), GFP_KERNEL)?; +/// +/// let guard1 =3D unsafe { ctx.lock(&mutex1)? }; +/// let guard2 =3D unsafe { ctx.lock(&mutex2)? }; +/// +/// // Mark acquisition phase as complete. +/// // SAFETY: It's called exactly once here and nowhere else. +/// unsafe { ctx.done() }; +/// +/// # Ok::<(), Error>(()) +/// ``` +#[pin_data(PinnedDrop)] +#[repr(transparent)] +pub struct AcquireCtx<'a> { + #[pin] + pub(super) inner: Opaque, + _p: PhantomData<&'a Class>, +} + +impl<'class> AcquireCtx<'class> { + /// Initializes a new [`AcquireCtx`] with the given `class`. + pub fn new(class: &'class Class) -> impl PinInit { + let class_ptr =3D class.inner.get(); + pin_init!(AcquireCtx { + inner <- Opaque::ffi_init(|slot: *mut bindings::ww_acquire_ctx= | { + // SAFETY: `class` is valid for the lifetime `'class` capt= ured + // by `AcquireCtx`. + unsafe { bindings::ww_acquire_init(slot, class_ptr) } + }), + _p: PhantomData + }) + } + + /// Creates a [`AcquireCtx`] 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 the `inner= ` field + /// of [`AcquireCtx`] and that it remains valid for the lifetime `'a`. + pub unsafe fn from_raw<'a>(ptr: *mut bindings::ww_acquire_ctx) -> &'a = Self { + // SAFETY: By the safety contract, `ptr` is valid to construct `Ac= quireCtx`. + unsafe { &*ptr.cast() } + } + + /// Marks the end of the acquire phase. + /// + /// Calling this function is optional. It is just useful to document + /// the code and clearly designated the acquire phase from actually + /// using the locked data structures. + /// + /// After calling this function, no more mutexes can be acquired with + /// this context. + /// + /// # Safety + /// + /// The caller must ensure that this function is called only once. + pub unsafe fn done(&self) { + // SAFETY: By the safety contract, the caller guarantees that this + // function is called only once. + unsafe { bindings::ww_acquire_done(self.inner.get()) }; + } + + /// Re-initializes the [`AcquireCtx`]. + /// + /// Must be called after releasing all locks when [`EDEADLK`] occurs. + /// + /// # Safety + /// + /// The given class must be equal to the class that was used to + /// initialize this [`AcquireCtx`]. + pub unsafe fn reinit(self: Pin<&mut Self>, class: &'class Class) { + let ctx =3D self.inner.get(); + + // SAFETY: + // - Lifetime of any guard (which hold an immutable borrow of `se= lf`) cannot overlap + // with the execution of this function. This enforces that all = locks acquired via + // this context have been released. + // + // - `ctx` is valid pointer to a `ww_acquire_ctx`. + // + // - `ctx` is guaranteed to be initialized because `ww_acquire_fi= ni` + // can only be called from the `Drop` implementation. + // + // - `ww_acquire_fini` is safe to call on an initialized context. + unsafe { bindings::ww_acquire_fini(ctx) }; + + // SAFETY: + // - `ctx` is valid pointer to a `ww_acquire_ctx`. + // + // - `class` is a valid pointer to a `ww_class`. + // + // - `ww_acquire_init` is safe to call with valid pointers + // to initialize an uninitialized context. + // + // - By the safety contract, the caller guarantees that the given + // `class` is the same as the one used to initialize this `Acq= uireCtx`. + unsafe { bindings::ww_acquire_init(ctx, class.inner.get()) }; + } + + /// Locks the given mutex on this [`AcquireCtx`]. + /// + /// # Safety + /// + /// The given `mutex` must be created with the [`Class`] that was used + /// to initialize this [`AcquireCtx`]. + pub unsafe fn lock<'a, T>(&'a self, mutex: &'a Mutex<'a, T>) -> Result= > { + // SAFETY: By the safety contract, `mutex` belongs to the same `Cl= ass` + // as `self` does. + unsafe { lock_common(mutex, Some(self), LockKind::Regular) } + } + + /// Similar to `lock`, but can be interrupted by signals. + /// + /// # Safety + /// + /// The given `mutex` must be created with the [`Class`] that was used + /// to initialize this [`AcquireCtx`]. + pub unsafe fn lock_interruptible<'a, T>( + &'a self, + mutex: &'a Mutex<'a, T>, + ) -> Result> { + // SAFETY: By the safety contract, `mutex` belongs to the same `Cl= ass` + // as `self` does. + unsafe { lock_common(mutex, Some(self), LockKind::Interruptible) } + } + + /// Locks the given mutex on this [`AcquireCtx`] using the slow path. + /// + /// This function should be used when `lock` fails (typically due to a= potential deadlock). + /// + /// # Safety + /// + /// The given `mutex` must be created with the [`Class`] that was used + /// to initialize this [`AcquireCtx`]. + pub unsafe fn lock_slow<'a, T>(&'a self, mutex: &'a Mutex<'a, T>) -> R= esult> { + // SAFETY: By the safety contract, `mutex` belongs to the same `Cl= ass` + // as `self` does. + unsafe { lock_common(mutex, Some(self), LockKind::Slow) } + } + + /// Similar to `lock_slow`, but can be interrupted by signals. + /// + /// # Safety + /// + /// The given `mutex` must be created with the [`Class`] that was used + /// to initialize this [`AcquireCtx`]. + pub unsafe fn lock_slow_interruptible<'a, T>( + &'a self, + mutex: &'a Mutex<'a, T>, + ) -> Result> { + // SAFETY: By the safety contract, `mutex` belongs to the same `Cl= ass` + // as `self` does. + unsafe { lock_common(mutex, Some(self), LockKind::SlowInterruptibl= e) } + } + + /// Tries to lock the mutex on this [`AcquireCtx`] without blocking. + /// + /// Unlike `lock`, no deadlock handling is performed. + /// + /// # Safety + /// + /// The given `mutex` must be created with the [`Class`] that was used + /// to initialize this [`AcquireCtx`]. + pub unsafe fn try_lock<'a, T>(&'a self, mutex: &'a Mutex<'a, T>) -> Re= sult> { + // SAFETY: By the safety contract, `mutex` belongs to the same `Cl= ass` + // as `self` does. + unsafe { lock_common(mutex, Some(self), LockKind::Try) } + } +} + +#[pinned_drop] +impl PinnedDrop for AcquireCtx<'_> { + fn drop(self: Pin<&mut Self>) { + // SAFETY: Given the lifetime bounds we know no locks are held, + // so calling `ww_acquire_fini` is safe. + unsafe { bindings::ww_acquire_fini(self.inner.get()) }; + } +} --=20 2.51.2 From nobody Tue Dec 16 07:27:47 2025 Received: from forward103d.mail.yandex.net (forward103d.mail.yandex.net [178.154.239.214]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 92182299943; Sat, 1 Nov 2025 16:11:23 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=178.154.239.214 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1762013486; cv=none; b=kecxmlpqmDOQE5U/Uz7TpnSPNCz5ibLF1x1YHmCgopMhD+JYIaiSaCa5ujpBxobqs549Fj2p2HBxP8QH/dnH8zo3GpKgLOziR1QpCnNPEKzkK+rI3DUJ4O8Bo1dTuaIqD2AjZ9V4M2q34Gre7LLiekyWG971BEgmMGq9KBa7hdg= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1762013486; c=relaxed/simple; bh=rbCeEuQmjACa3b+JGbAUkecAav6r4rUs3d5KiQCiLEg=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=ZqvoHTEQm7hbi9Ph7gI2GHx6Ejv97jkwepF+b0VxITq6X+yHoAbxe1vDClA3MYuYMZXh3azlOC9YCP+yrXbiGNM/LRowKrlf6Ga+pWeZfjNv/hl5T9Og1R6VmEVmJxNdtrjDnALbvNdyC6uzwO1Ky6UJlOpYdQ/UPyc6Sqcc49o= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=onurozkan.dev; spf=pass smtp.mailfrom=onurozkan.dev; dkim=pass (1024-bit key) header.d=onurozkan.dev header.i=@onurozkan.dev header.b=HiirFc5t; arc=none smtp.client-ip=178.154.239.214 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=onurozkan.dev Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=onurozkan.dev Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=onurozkan.dev header.i=@onurozkan.dev header.b="HiirFc5t" Received: from mail-nwsmtp-smtp-production-main-99.klg.yp-c.yandex.net (mail-nwsmtp-smtp-production-main-99.klg.yp-c.yandex.net [IPv6:2a02:6b8:c42:d7c6:0:640:5e67:0]) by forward103d.mail.yandex.net (Yandex) with ESMTPS id 0C88EC00A2; Sat, 01 Nov 2025 19:11:21 +0300 (MSK) Received: by mail-nwsmtp-smtp-production-main-99.klg.yp-c.yandex.net (smtp/Yandex) with ESMTPSA id wAkixRhLDOs0-mtTarMM9; Sat, 01 Nov 2025 19:11:20 +0300 X-Yandex-Fwd: 1 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=onurozkan.dev; s=mail; t=1762013480; bh=Q0ZO3k2nzppJbl+LT2AvxYEwKlwIAbMp9O9oraiRtEM=; h=Cc:Message-ID:References:Date:In-Reply-To:Subject:To:From; b=HiirFc5t8cLaYcqEfu8VgU2jHZ8hyylYG/dhYJRKAgpCQANagTSC9Y1a6Xf6OgvwU BTdgJYdPRI3iqw1ZN3Z1zKBgLxO2NwOXB3XsByXkslNDDsTCkt4x0HGtkH43CV3Aia nS8E7vWsrGffeATkdngksDOU1wsMIFdQ+roRnMwA= Authentication-Results: mail-nwsmtp-smtp-production-main-99.klg.yp-c.yandex.net; dkim=pass header.i=@onurozkan.dev From: =?UTF-8?q?Onur=20=C3=96zkan?= To: rust-for-linux@vger.kernel.org Cc: 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, daniel.almeida@collabora.com, linux-kernel@vger.kernel.org, =?UTF-8?q?Onur=20=C3=96zkan?= Subject: [PATCH v7 5/6] rust: ww_mutex: implement LockSet Date: Sat, 1 Nov 2025 19:10:55 +0300 Message-ID: <20251101161056.22408-6-work@onurozkan.dev> X-Mailer: git-send-email 2.51.2 In-Reply-To: <20251101161056.22408-1-work@onurozkan.dev> References: <20251101161056.22408-1-work@onurozkan.dev> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable `LockSet` is a high-level and safe API built on top of ww_mutex, provides a simple API while keeping the ww_mutex semantics. 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. Signed-off-by: Onur =C3=96zkan --- rust/kernel/sync/lock/ww_mutex.rs | 6 + rust/kernel/sync/lock/ww_mutex/lock_set.rs | 245 +++++++++++++++++++++ 2 files changed, 251 insertions(+) create mode 100644 rust/kernel/sync/lock/ww_mutex/lock_set.rs diff --git a/rust/kernel/sync/lock/ww_mutex.rs b/rust/kernel/sync/lock/ww_m= utex.rs index 2a9c1c20281b..d4c3b272912d 100644 --- a/rust/kernel/sync/lock/ww_mutex.rs +++ b/rust/kernel/sync/lock/ww_mutex.rs @@ -5,6 +5,10 @@ //! 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. +//! +//! It is recommended to use [`LockSet`] as it provides safe high-level +//! interface that automatically handles deadlocks, retries and context +//! management. =20 use crate::error::to_result; use crate::prelude::*; @@ -16,9 +20,11 @@ =20 pub use acquire_ctx::AcquireCtx; pub use class::Class; +pub use lock_set::LockSet; =20 mod acquire_ctx; mod class; +mod lock_set; =20 /// A wound-wait (ww) mutex that is powered with deadlock avoidance /// when acquiring multiple locks of the same [`Class`]. diff --git a/rust/kernel/sync/lock/ww_mutex/lock_set.rs b/rust/kernel/sync/= lock/ww_mutex/lock_set.rs new file mode 100644 index 000000000000..ae234fd1e0be --- /dev/null +++ b/rust/kernel/sync/lock/ww_mutex/lock_set.rs @@ -0,0 +1,245 @@ +// SPDX-License-Identifier: GPL-2.0 + +//! Provides [`LockSet`] which automatically detects [`EDEADLK`], +//! releases all locks, resets the state and retries the user +//! supplied locking algorithm until success. + +use super::{AcquireCtx, Class, Mutex}; +use crate::bindings; +use crate::prelude::*; +use crate::types::NotThreadSafe; +use core::ptr::NonNull; + +/// A tracked set of [`Mutex`] locks acquired under the same [`Class`]. +/// +/// It ensures proper cleanup and retry mechanism on deadlocks and provides +/// safe access to locked data via [`LockSet::with_locked`]. +/// +/// Typical usage is through [`LockSet::lock_all`], which retries a +/// user supplied locking algorithm until it succeeds without deadlock. +pub struct LockSet<'a> { + acquire_ctx: Pin>>, + taken: KVec, + class: &'a Class, +} + +/// Used by `LockSet` to track acquired locks. +/// +/// This type is strictly crate-private and must never be exposed +/// outside this crate. +struct RawGuard { + mutex_ptr: NonNull, + _not_send: NotThreadSafe, +} + +impl Drop for RawGuard { + 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 soun= d. + unsafe { bindings::ww_mutex_unlock(self.mutex_ptr.as_ptr()) }; + } +} + +impl<'a> Drop for LockSet<'a> { + fn drop(&mut self) { + self.release_all_locks(); + } +} + +impl<'a> LockSet<'a> { + /// Creates a new [`LockSet`] with the given class. + /// + /// All locks taken through this [`LockSet`] must belong to the + /// same class. + pub fn new(class: &'a Class) -> Result { + Ok(Self { + acquire_ctx: KBox::pin_init(AcquireCtx::new(class), GFP_KERNEL= )?, + taken: KVec::new(), + class, + }) + } + + /// Creates a new [`LockSet`] using an existing [`AcquireCtx`] and + /// [`Class`]. + /// + /// # Safety + /// + /// The caller must ensure that `acquire_ctx` is properly initialized, + /// holds no mutexes and that the provided `class` matches the one used + /// to initialize the given `acquire_ctx`. + pub unsafe fn new_with_acquire_ctx( + acquire_ctx: Pin>>, + class: &'a Class, + ) -> Self { + Self { + acquire_ctx, + taken: KVec::new(), + class, + } + } + + /// Attempts to lock a [`Mutex`] and records the guard. + /// + /// Returns [`EDEADLK`] if lock ordering would cause a deadlock. + /// + /// Returns [`EBUSY`] if `mutex` was locked outside of this [`LockSet`= ]. + /// + /// # Safety + /// + /// The given `mutex` must be created with the [`Class`] that was used + /// to initialize this [`LockSet`]. + pub unsafe fn lock(&mut self, mutex: &'a Mutex<'a, T>) -> Result { + if mutex.is_locked() + && !self + .taken + .iter() + .any(|guard| guard.mutex_ptr.as_ptr() =3D=3D mutex.inner.g= et()) + { + return Err(EBUSY); + } + + // SAFETY: By the safety contract, `mutex` belongs to the same `Cl= ass` + // as `self.acquire_ctx` does. + let guard =3D unsafe { self.acquire_ctx.lock(mutex)? }; + + self.taken.push( + RawGuard { + // SAFETY: We just locked it above so it's a valid pointer. + mutex_ptr: unsafe { NonNull::new_unchecked(guard.mutex.inn= er.get()) }, + _not_send: NotThreadSafe, + }, + GFP_KERNEL, + )?; + + // Avoid unlocking here; `release_all_locks` (also run by `Drop`) + // performs the unlock for `LockSet`. + core::mem::forget(guard); + + Ok(()) + } + + /// Runs `locking_algorithm` until success with retrying on deadlock. + /// + /// `locking_algorithm` should attempt to acquire all needed locks. + /// If [`EDEADLK`] is detected, this function will roll back, reset + /// the context and retry automatically. + /// + /// Once all locks are acquired successfully, `on_all_locks_taken` is + /// invoked for exclusive access to the locked values. Afterwards, all + /// locks are released. + /// + /// # Example + /// + /// ``` + /// use kernel::alloc::KBox; + /// use kernel::c_str; + /// use kernel::prelude::*; + /// use kernel::sync::Arc; + /// use kernel::sync::lock::ww_mutex::{Class, LockSet, Mutex}; + /// use pin_init::stack_pin_init; + /// + /// stack_pin_init!(let class =3D Class::new_wound_wait(c_str!("test")= )); + /// + /// let mutex1 =3D Arc::pin_init(Mutex::new(0, &class), GFP_KERNEL)?; + /// let mutex2 =3D Arc::pin_init(Mutex::new(0, &class), GFP_KERNEL)?; + /// let mut lock_set =3D KBox::pin_init(LockSet::new(&class)?, GFP_KER= NEL)?; + /// + /// lock_set.lock_all( + /// // `locking_algorithm` closure + /// |lock_set| { + /// // SAFETY: Both `lock_set` and `mutex1` uses the same clas= s. + /// unsafe { lock_set.lock(&mutex1)? }; + /// + /// // SAFETY: Both `lock_set` and `mutex2` uses the same clas= s. + /// unsafe { lock_set.lock(&mutex2)? }; + /// + /// Ok(()) + /// }, + /// // `on_all_locks_taken` closure + /// |lock_set| { + /// // Safely mutate both values while holding the locks. + /// lock_set.with_locked(&mutex1, |v| *v +=3D 1)?; + /// lock_set.with_locked(&mutex2, |v| *v +=3D 1)?; + /// + /// Ok(()) + /// }, + /// )?; + /// + /// # Ok::<(), Error>(()) + /// ``` + pub fn lock_all( + &mut self, + mut locking_algorithm: T, + mut on_all_locks_taken: Y, + ) -> Result + where + T: FnMut(&mut LockSet<'a>) -> Result, + Y: FnMut(&mut LockSet<'a>) -> Result, + { + loop { + match locking_algorithm(self) { + Ok(()) =3D> { + // All locks in `locking_algorithm` succeeded. + // The user can now safely use them in `on_all_locks_t= aken`. + let res =3D on_all_locks_taken(self); + self.release_all_locks(); + + return res; + } + Err(e) if e =3D=3D EDEADLK =3D> { + // Deadlock detected, retry from scratch. + self.cleanup_on_deadlock(); + continue; + } + Err(e) =3D> { + self.release_all_locks(); + return Err(e); + } + } + } + } + + /// Executes `access` with a mutable reference to the data behind `mut= ex`. + /// + /// Fails with [`EINVAL`] if the mutex was not locked in this [`LockSe= t`]. + pub fn with_locked( + &mut self, + mutex: &'a Mutex<'a, T>, + access: impl for<'b> FnOnce(&'b mut T) -> Y, + ) -> Result { + let mutex_ptr =3D mutex.inner.get(); + + if self + .taken + .iter() + .any(|guard| guard.mutex_ptr.as_ptr() =3D=3D mutex_ptr) + { + // SAFETY: We hold the lock corresponding to `mutex`, so we ha= ve + // exclusive access to its protected data. + let value =3D unsafe { &mut *mutex.data.get() }; + Ok(access(value)) + } else { + // `mutex` isn't locked in this `LockSet`. + Err(EINVAL) + } + } + + /// Releases all currently held locks in this [`LockSet`]. + fn release_all_locks(&mut self) { + // `Drop` implementation of the `RawGuard` takes care of the unloc= king. + self.taken.clear(); + } + + /// Resets this [`LockSet`] after a deadlock detection. + /// + /// Drops all held locks and reinitializes the [`AcquireCtx`]. + /// + /// It is intended to be used for internal implementation only. + fn cleanup_on_deadlock(&mut self) { + self.release_all_locks(); + + // SAFETY: We are passing the same `class` that was used + // to initialize `self.acquire_ctx`. + unsafe { self.acquire_ctx.as_mut().reinit(self.class) }; + } +} --=20 2.51.2 From nobody Tue Dec 16 07:27:47 2025 Received: from forward100d.mail.yandex.net (forward100d.mail.yandex.net [178.154.239.211]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id E14C02E6CA8; Sat, 1 Nov 2025 16:11:26 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=178.154.239.211 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1762013489; cv=none; b=FLX1pQhFvf9uBdm3o2SgeFNP3dAbyvYnukxPE2bLQxUXinhdPFAdMggTwuUrWOjmTcpTonqqiPFFF7VCzulI342GOiz02WGTUp2I8L34OEUd0nOvF8fTQnN0pY0aLmixT/1BRXxMFRHUmkBIk9c2SsbfUN8He8Or1Y5ADEs6Ndc= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1762013489; c=relaxed/simple; bh=Knwxuci/X+GhWYa24wgFnzbOEjJvzPD64yA+A2FQd+g=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=Gye4qiAMflEJeSGTExeCmTshajyvaalJDbjwxEHUnhORJ9k0vWyEeVTJz0mdXdbIPpPMmrgP04hr9bqWTJd/74mvPSkz+hQiX4tBeFd3zpK4tTDP3GNCu5djK1nqLKHYsmgjHCG6A7IuaxLt3n/8IJZNXLEAQtlA3iIiFjHAOlI= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=onurozkan.dev; spf=pass smtp.mailfrom=onurozkan.dev; dkim=pass (1024-bit key) header.d=onurozkan.dev header.i=@onurozkan.dev header.b=HO+i7eBO; arc=none smtp.client-ip=178.154.239.211 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=onurozkan.dev Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=onurozkan.dev Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=onurozkan.dev header.i=@onurozkan.dev header.b="HO+i7eBO" Received: from mail-nwsmtp-smtp-production-main-99.klg.yp-c.yandex.net (mail-nwsmtp-smtp-production-main-99.klg.yp-c.yandex.net [IPv6:2a02:6b8:c42:d7c6:0:640:5e67:0]) by forward100d.mail.yandex.net (Yandex) with ESMTPS id 132C5C0031; Sat, 01 Nov 2025 19:11:24 +0300 (MSK) Received: by mail-nwsmtp-smtp-production-main-99.klg.yp-c.yandex.net (smtp/Yandex) with ESMTPSA id wAkixRhLDOs0-PWpMpCYg; Sat, 01 Nov 2025 19:11:23 +0300 X-Yandex-Fwd: 1 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=onurozkan.dev; s=mail; t=1762013483; bh=AXzltMKi+uQ4kaGjizVoMVhSCSVN1iyKFtehXj1aQCI=; h=Cc:Message-ID:References:Date:In-Reply-To:Subject:To:From; b=HO+i7eBOWFMLoxsIaCuz7DHVQDpe0lKb3HDC2qruW2vaE7zVzGxhvVPk4SaEe8TvL +rDEblrvljqLMEL4JRn1qllwQj/v7rdNATirY8s5noPifSEXvNVpJOJ2OVZa2Svn1r GvHyFz6HK0OlveyBSBi10iVojLqEuHAUBDaN7HPg= Authentication-Results: mail-nwsmtp-smtp-production-main-99.klg.yp-c.yandex.net; dkim=pass header.i=@onurozkan.dev From: =?UTF-8?q?Onur=20=C3=96zkan?= To: rust-for-linux@vger.kernel.org Cc: 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, daniel.almeida@collabora.com, linux-kernel@vger.kernel.org, =?UTF-8?q?Onur=20=C3=96zkan?= Subject: [PATCH v7 6/6] rust: add test coverage for ww_mutex implementation Date: Sat, 1 Nov 2025 19:10:56 +0300 Message-ID: <20251101161056.22408-7-work@onurozkan.dev> X-Mailer: git-send-email 2.51.2 In-Reply-To: <20251101161056.22408-1-work@onurozkan.dev> References: <20251101161056.22408-1-work@onurozkan.dev> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Adds test coverage on the core ww_mutex functionality. Signed-off-by: Onur =C3=96zkan --- rust/kernel/sync/lock/ww_mutex.rs | 151 +++++++++++++++++++ rust/kernel/sync/lock/ww_mutex/lock_set.rs | 165 +++++++++++++++++++++ 2 files changed, 316 insertions(+) diff --git a/rust/kernel/sync/lock/ww_mutex.rs b/rust/kernel/sync/lock/ww_m= utex.rs index d4c3b272912d..cf4872bd258e 100644 --- a/rust/kernel/sync/lock/ww_mutex.rs +++ b/rust/kernel/sync/lock/ww_mutex.rs @@ -287,3 +287,154 @@ unsafe fn lock_common<'a, T: ?Sized>( =20 Ok(MutexGuard::new(mutex)) } + +#[kunit_tests(rust_kernel_ww_mutex)] +mod tests { + use crate::prelude::*; + use crate::sync::Arc; + use crate::{c_str, define_class}; + use pin_init::stack_pin_init; + + use super::*; + + // A simple coverage on `define_class` macro. + define_class!(TEST_WOUND_WAIT_CLASS, wound_wait, c_str!("test")); + define_class!(TEST_WAIT_DIE_CLASS, wait_die, c_str!("test")); + + #[test] + fn test_ww_mutex_basic_lock_unlock() -> Result { + stack_pin_init!(let class =3D Class::new_wound_wait(c_str!("test")= )); + + let mutex =3D Arc::pin_init(Mutex::new(42, &class), GFP_KERNEL)?; + + let ctx =3D KBox::pin_init(AcquireCtx::new(&class), GFP_KERNEL)?; + + // SAFETY: Both `ctx` and `mutex` uses the same class. + let guard =3D unsafe { ctx.lock(&mutex)? }; + assert_eq!(*guard, 42); + + // Drop the lock. + drop(guard); + + // SAFETY: Both `ctx` and `mutex` uses the same class. + let mut guard =3D unsafe { ctx.lock(&mutex)? }; + *guard =3D 100; + assert_eq!(*guard, 100); + + Ok(()) + } + + #[test] + fn test_ww_mutex_trylock() -> Result { + stack_pin_init!(let class =3D Class::new_wound_wait(c_str!("test")= )); + + let mutex =3D Arc::pin_init(Mutex::new(123, &class), GFP_KERNEL)?; + + let ctx =3D KBox::pin_init(AcquireCtx::new(&class), GFP_KERNEL)?; + + // `try_lock` on unlocked mutex should succeed. + // SAFETY: Both `ctx` and `mutex` uses the same class. + let guard =3D unsafe { ctx.try_lock(&mutex)? }; + assert_eq!(*guard, 123); + + // Now it should fail immediately as it's already locked. + // SAFETY: Both `ctx` and `mutex` uses the same class. + assert!(unsafe { ctx.try_lock(&mutex).is_err() }); + + Ok(()) + } + + #[test] + fn test_ww_mutex_is_locked() -> Result { + stack_pin_init!(let class =3D Class::new_wait_die(c_str!("test"))); + + let mutex =3D Arc::pin_init(Mutex::new("hello", &class), GFP_KERNE= L)?; + + let ctx =3D KBox::pin_init(AcquireCtx::new(&class), GFP_KERNEL)?; + + // Should not be locked initially. + assert!(!mutex.is_locked()); + + // SAFETY: Both `ctx` and `mutex` uses the same class. + let guard =3D unsafe { ctx.lock(&mutex)? }; + assert!(mutex.is_locked()); + + drop(guard); + assert!(!mutex.is_locked()); + + Ok(()) + } + + #[test] + fn test_ww_acquire_context_done() -> Result { + stack_pin_init!(let class =3D Class::new_wound_wait(c_str!("test")= )); + + let mutex1 =3D Arc::pin_init(Mutex::new(1, &class), GFP_KERNEL)?; + let mutex2 =3D Arc::pin_init(Mutex::new(2, &class), GFP_KERNEL)?; + + let ctx =3D KBox::pin_init(AcquireCtx::new(&class), GFP_KERNEL)?; + + // Acquire multiple mutexes with the same context. + // SAFETY: Both `ctx` and `mutex1` uses the same class. + let guard1 =3D unsafe { ctx.lock(&mutex1)? }; + // SAFETY: Both `ctx` and `mutex2` uses the same class. + let guard2 =3D unsafe { ctx.lock(&mutex2)? }; + + assert_eq!(*guard1, 1); + assert_eq!(*guard2, 2); + + // SAFETY: It's called exactly once here and nowhere else. + unsafe { ctx.done() }; + + // We shouldn't be able to lock once it's `done`. + // SAFETY: Both `ctx` and `mutex1` uses the same class. + assert!(unsafe { ctx.lock(&mutex1).is_err() }); + // SAFETY: Both `ctx` and `mutex2` uses the same class. + assert!(unsafe { ctx.lock(&mutex2).is_err() }); + + Ok(()) + } + + #[test] + fn test_with_global_classes() -> Result { + let mutex1 =3D Arc::pin_init(Mutex::new(100, &TEST_WOUND_WAIT_CLAS= S), GFP_KERNEL)?; + let mutex2 =3D Arc::pin_init(Mutex::new(200, &TEST_WAIT_DIE_CLASS)= , GFP_KERNEL)?; + + let ww_ctx =3D KBox::pin_init(AcquireCtx::new(&TEST_WOUND_WAIT_CLA= SS), GFP_KERNEL)?; + let wd_ctx =3D KBox::pin_init(AcquireCtx::new(&TEST_WAIT_DIE_CLASS= ), GFP_KERNEL)?; + + // SAFETY: Both `ww_ctx` and `mutex1` uses the same class. + let ww_guard =3D unsafe { ww_ctx.lock(&mutex1)? }; + // SAFETY: Both `wd_ctx` and `mutex2` uses the same class. + let wd_guard =3D unsafe { wd_ctx.lock(&mutex2)? }; + + assert_eq!(*ww_guard, 100); + assert_eq!(*wd_guard, 200); + + assert!(mutex1.is_locked()); + assert!(mutex2.is_locked()); + + drop(ww_guard); + drop(wd_guard); + + assert!(!mutex1.is_locked()); + assert!(!mutex2.is_locked()); + + Ok(()) + } + + #[test] + fn test_mutex_without_ctx() -> Result { + let mutex =3D Arc::pin_init(Mutex::new(100, &TEST_WOUND_WAIT_CLASS= ), GFP_KERNEL)?; + let guard =3D mutex.lock()?; + + assert_eq!(*guard, 100); + assert!(mutex.is_locked()); + + drop(guard); + + assert!(!mutex.is_locked()); + + Ok(()) + } +} diff --git a/rust/kernel/sync/lock/ww_mutex/lock_set.rs b/rust/kernel/sync/= lock/ww_mutex/lock_set.rs index ae234fd1e0be..f5800755fc6b 100644 --- a/rust/kernel/sync/lock/ww_mutex/lock_set.rs +++ b/rust/kernel/sync/lock/ww_mutex/lock_set.rs @@ -243,3 +243,168 @@ fn cleanup_on_deadlock(&mut self) { unsafe { self.acquire_ctx.as_mut().reinit(self.class) }; } } + +#[kunit_tests(rust_kernel_lock_set)] +mod tests { + use crate::c_str; + use crate::prelude::*; + use crate::sync::Arc; + use pin_init::stack_pin_init; + + use super::*; + + #[test] + fn test_lock_set_basic_lock_unlock() -> Result { + stack_pin_init!(let class =3D Class::new_wound_wait(c_str!("test")= )); + + let mutex =3D Arc::pin_init(Mutex::new(10, &class), GFP_KERNEL)?; + let mut lock_set =3D KBox::pin_init(LockSet::new(&class)?, GFP_KER= NEL)?; + + // SAFETY: Both `lock_set` and `mutex` uses the same class. + unsafe { lock_set.lock(&mutex)? }; + + lock_set.with_locked(&mutex, |v| { + assert_eq!(*v, 10); + })?; + + lock_set.release_all_locks(); + assert!(!mutex.is_locked()); + + Ok(()) + } + + #[test] + fn test_lock_set_with_locked_mutates_data() -> Result { + stack_pin_init!(let class =3D Class::new_wound_wait(c_str!("test")= )); + + let mutex =3D Arc::pin_init(Mutex::new(5, &class), GFP_KERNEL)?; + let mut lock_set =3D KBox::pin_init(LockSet::new(&class)?, GFP_KER= NEL)?; + + // SAFETY: Both `lock_set` and `mutex` uses the same class. + unsafe { lock_set.lock(&mutex)? }; + + lock_set.with_locked(&mutex, |v| { + assert_eq!(*v, 5); + // Increment the value. + *v +=3D 7; + })?; + + lock_set.with_locked(&mutex, |v| { + // Check that mutation took effect. + assert_eq!(*v, 12); + })?; + + Ok(()) + } + + #[test] + fn test_lock_all_success() -> Result { + stack_pin_init!(let class =3D Class::new_wound_wait(c_str!("test")= )); + + let mutex1 =3D Arc::pin_init(Mutex::new(1, &class), GFP_KERNEL)?; + let mutex2 =3D Arc::pin_init(Mutex::new(2, &class), GFP_KERNEL)?; + let mut lock_set =3D KBox::pin_init(LockSet::new(&class)?, GFP_KER= NEL)?; + + let res =3D lock_set.lock_all( + // `locking_algorithm` closure + |lock_set| { + // SAFETY: Both `lock_set` and `mutex1` uses the same clas= s. + let _ =3D unsafe { lock_set.lock(&mutex1)? }; + + // SAFETY: Both `lock_set` and `mutex2` uses the same clas= s. + let _ =3D unsafe { lock_set.lock(&mutex2)? }; + Ok(()) + }, + // `on_all_locks_taken` closure + |lock_set| { + lock_set.with_locked(&mutex1, |v| *v +=3D 10)?; + lock_set.with_locked(&mutex2, |v| *v +=3D 20)?; + Ok(( + lock_set.with_locked(&mutex1, |v| *v)?, + lock_set.with_locked(&mutex2, |v| *v)?, + )) + }, + )?; + + assert_eq!(res, (11, 22)); + assert!(!mutex1.is_locked()); + assert!(!mutex2.is_locked()); + + Ok(()) + } + + #[test] + fn test_with_different_input_type() -> Result { + stack_pin_init!(let class =3D Class::new_wound_wait(c_str!("test")= )); + + let mutex1 =3D Arc::pin_init(Mutex::new(1, &class), GFP_KERNEL)?; + let mutex2 =3D Arc::pin_init(Mutex::new("hello", &class), GFP_KERN= EL)?; + let mut lock_set =3D KBox::pin_init(LockSet::new(&class)?, GFP_KER= NEL)?; + + lock_set.lock_all( + // `locking_algorithm` closure + |lock_set| { + // SAFETY: Both `lock_set` and `mutex1` uses the same clas= s. + unsafe { lock_set.lock(&mutex1)? }; + + // SAFETY: Both `lock_set` and `mutex2` uses the same clas= s. + unsafe { lock_set.lock(&mutex2)? }; + + Ok(()) + }, + // `on_all_locks_taken` closure + |lock_set| { + lock_set.with_locked(&mutex1, |v| assert_eq!(*v, 1))?; + lock_set.with_locked(&mutex2, |v| assert_eq!(*v, "hello"))= ?; + Ok(()) + }, + )?; + + Ok(()) + } + + #[test] + fn test_lock_all_retries_on_deadlock() -> Result { + stack_pin_init!(let class =3D Class::new_wound_wait(c_str!("test")= )); + + let mutex =3D Arc::pin_init(Mutex::new(99, &class), GFP_KERNEL)?; + let mut lock_set =3D KBox::pin_init(LockSet::new(&class)?, GFP_KER= NEL)?; + let mut first_try =3D true; + + let res =3D lock_set.lock_all( + // `locking_algorithm` closure + |lock_set| { + if first_try { + first_try =3D false; + // Simulate deadlock on first attempt. + return Err(EDEADLK); + } + // SAFETY: Both `lock_set` and `mutex` uses the same class. + unsafe { lock_set.lock(&mutex) } + }, + // `on_all_locks_taken` closure + |lock_set| { + lock_set.with_locked(&mutex, |v| { + *v +=3D 1; + *v + }) + }, + )?; + + assert_eq!(res, 100); + Ok(()) + } + + #[test] + fn test_with_locked_on_unlocked_mutex() -> Result { + stack_pin_init!(let class =3D Class::new_wound_wait(c_str!("test")= )); + + let mutex =3D Arc::pin_init(Mutex::new(5, &class), GFP_KERNEL)?; + let mut lock_set =3D KBox::pin_init(LockSet::new(&class)?, GFP_KER= NEL)?; + + let ecode =3D lock_set.with_locked(&mutex, |_v| {}).unwrap_err(); + assert_eq!(EINVAL, ecode); + + Ok(()) + } +} --=20 2.51.2