From nobody Sat Feb 7 15:35:17 2026 Received: from forward205a.mail.yandex.net (forward205a.mail.yandex.net [178.154.239.88]) (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 ED6A442049; Fri, 2 Jan 2026 18:00:52 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=178.154.239.88 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1767376856; cv=none; b=nqABzmd+HyNkeMwZ+A2Pous61cBx+Awb+88Dg781NBiAvUkVXaTByDLqXDIiM+ebTLAL2VsvC5x0bn4IZTbkTSye/+7bB5s3v09PPiHY/k4aVQeRhYgVgZ1JnEKEfFC9uUU4VA5k0wSeo/NxG0GdwODVy10uG7YJn5D7Te2MNRo= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1767376856; c=relaxed/simple; bh=2EVSplVnK7R0RFPqc62S106ZcYBEfbsD9zMBWMzUI8M=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=CuK2SPhhZePrmSP3IHTQaLXgMFUSUbsppmynjuj91NNrqHwFb0oqUc1d1j+hAMn0gqaNpVbgIZrij5TV3P4Usr3ra5rXLjMxayn5FLa+1duEK0oBynPyg1b3fI6DSkDyq45bnRHPBLYI4JMDDck/odgzTt6TEowRyx54RP6+pm8= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject 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=cgx4XjcT; arc=none smtp.client-ip=178.154.239.88 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject 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="cgx4XjcT" Received: from forward101a.mail.yandex.net (forward101a.mail.yandex.net [IPv6:2a02:6b8:c0e:500:1:45:d181:d101]) by forward205a.mail.yandex.net (Yandex) with ESMTPS id CAEFBC402B; Fri, 02 Jan 2026 20:54:58 +0300 (MSK) Received: from mail-nwsmtp-smtp-production-main-55.vla.yp-c.yandex.net (mail-nwsmtp-smtp-production-main-55.vla.yp-c.yandex.net [IPv6:2a02:6b8:c1f:582e:0:640:200:0]) by forward101a.mail.yandex.net (Yandex) with ESMTPS id 3180B80C31; Fri, 02 Jan 2026 20:54:51 +0300 (MSK) Received: by mail-nwsmtp-smtp-production-main-55.vla.yp-c.yandex.net (smtp/Yandex) with ESMTPSA id prp9aJGHE8c0-WyS3it1X; Fri, 02 Jan 2026 20:54:50 +0300 X-Yandex-Fwd: 1 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=onurozkan.dev; s=mail; t=1767376490; bh=23tOmmBtRzPwCEyTF8gBAW2nUgVB9qSXyEXIMs1Iu8A=; h=Cc:Message-ID:References:Date:In-Reply-To:Subject:To:From; b=cgx4XjcTRMuFcD6cSmNX+HUXykhhK+M/wtJo0ofwliYy/kxE7jNHnShhnQmyl0JW2 6MAqRM/fIbiuHJw1qwjktfOorl1hxKpCPpWiXzU02Oh8T+Fb3CoOBnjUd86KejaP1Z O9J13dOGwykXu0dhO/3ntLYQvt9ljKQgND5v+2sw= Authentication-Results: mail-nwsmtp-smtp-production-main-55.vla.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, daniel.almeida@collabora.com, thomas.hellstrom@linux.intel.com, linux-kernel@vger.kernel.org, =?UTF-8?q?Onur=20=C3=96zkan?= Subject: [PATCH v9 1/7] rust: add C wrappers for ww_mutex inline functions Date: Fri, 2 Jan 2026 20:52:56 +0300 Message-ID: <20260102175302.194435-2-work@onurozkan.dev> X-Mailer: git-send-email 2.51.2 In-Reply-To: <20260102175302.194435-1-work@onurozkan.dev> References: <20260102175302.194435-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 --- MAINTAINERS | 1 + rust/helpers/helpers.c | 1 + rust/helpers/ww_mutex.c | 45 +++++++++++++++++++++++++++++++++++++++++ 3 files changed, 47 insertions(+) create mode 100644 rust/helpers/ww_mutex.c diff --git a/MAINTAINERS b/MAINTAINERS index 765ad2daa218..d245b7b38b21 100644 --- a/MAINTAINERS +++ b/MAINTAINERS @@ -14728,6 +14728,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 79c72762ad9c..a95fb3abaab3 100644 --- a/rust/helpers/helpers.c +++ b/rust/helpers/helpers.c @@ -61,4 +61,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..2ea19aafbd4e --- /dev/null +++ b/rust/helpers/ww_mutex.c @@ -0,0 +1,45 @@ +// SPDX-License-Identifier: GPL-2.0 + +#include + +__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_cla= ss *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 +void rust_helper_ww_mutex_lock_slow(struct ww_mutex *lock, struct ww_acqui= re_ctx *ctx) +{ + ww_mutex_lock_slow(lock, ctx); +} + +__rust_helper +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); +} + +__rust_helper +bool rust_helper_ww_mutex_is_locked(struct ww_mutex *lock) +{ + return ww_mutex_is_locked(lock); +} --=20 2.51.2 From nobody Sat Feb 7 15:35:17 2026 Received: from forward202a.mail.yandex.net (forward202a.mail.yandex.net [178.154.239.91]) (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 B3CDF2FF172; Fri, 2 Jan 2026 18:01:27 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=178.154.239.91 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1767376893; cv=none; b=IzPlPRt4SixYM0ruHM/COBtozpRwVWSjR6n1pXjX5iO4hjgLu+MO0wjNtHjO6VAr9iIIRyS8GFTllQ/ZddZYmiLzWQvpP41cXisDsioSShPrcBh0fUzabFsKD8yTKgvLdnGtiBLk5GL/1OpazbsrvWExosCkXgJkm3zwBkw15Is= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1767376893; c=relaxed/simple; bh=6F/+tezkEB2vztihYOYq49PMzQhO8YkLYqMevzRARmk=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=VFC6RE55MUwR6uLyOwABMeMOLpICn14rqzTgrXx3LP89aYmvqc6+DBodHBAoKAR5e77kASXbTCEDbpPIX6EHlAEtHFCfwkGnvjAC1TnZohLYCwUHQdbY608d7BTRfp9ec8WdcrlnO4t/rPRRNLFtpiJBhf/4/SNQE1viPnjDv4k= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject 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=RHrgWLqL; arc=none smtp.client-ip=178.154.239.91 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject 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="RHrgWLqL" Received: from forward103a.mail.yandex.net (forward103a.mail.yandex.net [IPv6:2a02:6b8:c0e:500:1:45:d181:d103]) by forward202a.mail.yandex.net (Yandex) with ESMTPS id F25DB83D85; Fri, 02 Jan 2026 20:55:02 +0300 (MSK) Received: from mail-nwsmtp-smtp-production-main-55.vla.yp-c.yandex.net (mail-nwsmtp-smtp-production-main-55.vla.yp-c.yandex.net [IPv6:2a02:6b8:c1f:582e:0:640:200:0]) by forward103a.mail.yandex.net (Yandex) with ESMTPS id 4AD4780872; Fri, 02 Jan 2026 20:54:54 +0300 (MSK) Received: by mail-nwsmtp-smtp-production-main-55.vla.yp-c.yandex.net (smtp/Yandex) with ESMTPSA id prp9aJGHE8c0-YrQU3jny; Fri, 02 Jan 2026 20:54:53 +0300 X-Yandex-Fwd: 1 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=onurozkan.dev; s=mail; t=1767376493; bh=8pibJ9wAikpYKSyeQ9ysDFVDnPDbGtUh727m8wRpnxs=; h=Cc:Message-ID:References:Date:In-Reply-To:Subject:To:From; b=RHrgWLqLS8ib5ny2yvz5C4qr2VoTeAVQjTpkSr/vL83SVRCqQeKCnJJrT+xOCOnvx 7SeOaN3D2JjZq1jJAeDQ/+E8A9QOF8FTGHbzfMtzgoOj4nkjxF2PoaZM2L7fLigsom I8oJ4fVJQPMdfHg1Fy5DXe1dUS2owC9UZIcUYHuA= Authentication-Results: mail-nwsmtp-smtp-production-main-55.vla.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, daniel.almeida@collabora.com, thomas.hellstrom@linux.intel.com, linux-kernel@vger.kernel.org, =?UTF-8?q?Onur=20=C3=96zkan?= Subject: [PATCH v9 2/7] ww_mutex: add ww_class field unconditionally Date: Fri, 2 Jan 2026 20:52:57 +0300 Message-ID: <20260102175302.194435-3-work@onurozkan.dev> X-Mailer: git-send-email 2.51.2 In-Reply-To: <20260102175302.194435-1-work@onurozkan.dev> References: <20260102175302.194435-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 Removes 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, simplify AcquireCtx reinitialization logic and validate Mutex and AcquireCtx class pairing in much simpler way. Link: https://lore.kernel.org/all/ECC0425A-8B18-4626-8EA8-2F843C45E0A1@coll= abora.com/ Signed-off-by: Onur =C3=96zkan --- include/linux/ww_mutex.h | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/include/linux/ww_mutex.h b/include/linux/ww_mutex.h index 45ff6f7a872b..c9835035b6d6 100644 --- a/include/linux/ww_mutex.h +++ b/include/linux/ww_mutex.h @@ -47,9 +47,7 @@ struct ww_class { struct ww_mutex { struct WW_MUTEX_BASE base; struct ww_acquire_ctx *ctx; -#ifdef DEBUG_WW_MUTEXES struct ww_class *ww_class; -#endif }; =20 struct ww_acquire_ctx { @@ -58,9 +56,9 @@ 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 @@ -110,9 +108,7 @@ static inline void ww_mutex_init(struct ww_mutex *lock, { ww_mutex_base_init(&lock->base, ww_class->mutex_name, &ww_class->mutex_ke= y); lock->ctx =3D NULL; -#ifdef DEBUG_WW_MUTEXES lock->ww_class =3D ww_class; -#endif } =20 /** @@ -147,8 +143,8 @@ static inline void ww_acquire_init(struct ww_acquire_ct= x *ctx, ctx->acquired =3D 0; ctx->wounded =3D false; ctx->is_wait_die =3D ww_class->is_wait_die; -#ifdef DEBUG_WW_MUTEXES ctx->ww_class =3D ww_class; +#ifdef DEBUG_WW_MUTEXES ctx->done_acquire =3D 0; ctx->contending_lock =3D NULL; #endif --=20 2.51.2 From nobody Sat Feb 7 15:35:17 2026 Received: from forward201a.mail.yandex.net (forward201a.mail.yandex.net [178.154.239.92]) (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 6462F25A357; Fri, 2 Jan 2026 18:02:55 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=178.154.239.92 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1767376977; cv=none; b=fX6nnsazFgdn9RUCUJ7jiL474TgsW4E1fEofMgylqdoTJin8E5GVHcDmTvq2oB6vFVxRZ2wTmCBoTuQo/8frlP6l4/mr1HaOGlDxaY3xU0AFyZOejOTbter8f/wFr48tET7P80mm64DhGIYH07cU3I0UPbEugMI620jDsalh55c= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1767376977; c=relaxed/simple; bh=vfNKBcpbZfVUaG2HIOhR81QGP7isefAqWuFwxVyXrK8=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=eD/iU2NxrgdC48YEEjjWcdc3MkuzeLBZcwKEYvYohOYJBm55N7CkUc94sVQuGcnnoUJGNRmuNNIF6LodWNuG/PkrMAgM+JZcdeKZXU09V9+czSnPHQOggZ11zr1LmZEBjbYkYoaygGV54TBRX57NqIpDHRtE0kvcUJiHthfPwkc= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject 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=XCN8SekX; arc=none smtp.client-ip=178.154.239.92 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject 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="XCN8SekX" Received: from forward103a.mail.yandex.net (forward103a.mail.yandex.net [IPv6:2a02:6b8:c0e:500:1:45:d181:d103]) by forward201a.mail.yandex.net (Yandex) with ESMTPS id C2B9883E5A; Fri, 02 Jan 2026 20:55:04 +0300 (MSK) Received: from mail-nwsmtp-smtp-production-main-55.vla.yp-c.yandex.net (mail-nwsmtp-smtp-production-main-55.vla.yp-c.yandex.net [IPv6:2a02:6b8:c1f:582e:0:640:200:0]) by forward103a.mail.yandex.net (Yandex) with ESMTPS id A2A08808B2; Fri, 02 Jan 2026 20:54:56 +0300 (MSK) Received: by mail-nwsmtp-smtp-production-main-55.vla.yp-c.yandex.net (smtp/Yandex) with ESMTPSA id prp9aJGHE8c0-54Bf4Wdk; Fri, 02 Jan 2026 20:54:56 +0300 X-Yandex-Fwd: 1 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=onurozkan.dev; s=mail; t=1767376496; bh=a50rz/ne4O3HC3C6Yio58qwQfeOtFy6yA7MhwTMu8KY=; h=Cc:Message-ID:References:Date:In-Reply-To:Subject:To:From; b=XCN8SekXqs32/n93KnjRKwGxjngkqnJskjolZP2dl7ESwmBXf0rM62c0IXhsHktKn ni9sCKpUZomnHMjA8fLCs3Yp648addK0laMRoSZ5mje11SdFHy7lBqRlVf4k2fmliG jm5DPkANOZBRaIj/j7wf9Zut6YOJjPlRR0YO+m9E= Authentication-Results: mail-nwsmtp-smtp-production-main-55.vla.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, daniel.almeida@collabora.com, thomas.hellstrom@linux.intel.com, linux-kernel@vger.kernel.org, =?UTF-8?q?Onur=20=C3=96zkan?= Subject: [PATCH v9 3/7] rust: error: add EDEADLK Date: Fri, 2 Jan 2026 20:52:58 +0300 Message-ID: <20260102175302.194435-4-work@onurozkan.dev> X-Mailer: git-send-email 2.51.2 In-Reply-To: <20260102175302.194435-1-work@onurozkan.dev> References: <20260102175302.194435-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 --- rust/kernel/error.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/rust/kernel/error.rs b/rust/kernel/error.rs index 258b12afdcba..c2b95db23abc 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 Sat Feb 7 15:35:17 2026 Received: from forward200a.mail.yandex.net (forward200a.mail.yandex.net [178.154.239.93]) (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 3F22E20C038; Fri, 2 Jan 2026 18:01:49 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=178.154.239.93 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1767376912; cv=none; b=FWZQ21ftWK180LAA5VQQezcxVqiNraKk0MCjMjtxQTl2+Fm7MOWqtwEW8BUNaB8mce1To23lfmUB62Gicz+HhwGYqRxHm14J20acJXs/ednYhQAKFU1EGLtdarKIlxCLyzkkrWOoBfn1QMJFsRhR8lG5wLEYba+5taGozlN96bQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1767376912; c=relaxed/simple; bh=mx1XwExiBMpwPcIDb8Bk/zFD7ucxlMXBHJ/Q5HhXBW8=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=p3VKQzyO65QmboRo44fkdt/wg+YUO8IelwQyL3y96ILauin7TkBdREftzzKB4hlHDQNKpHN3bbMzCYGcsNBfCdjkgsi9EW2H8jLY03Jzn0KBHq+9I8rc2z71z+vnrTJT1pIUN9F6bYgWTwmQQaia5jxmtpM3rKvJ2UjqThRfcNg= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject 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=JyAgWu8Z; arc=none smtp.client-ip=178.154.239.93 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject 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="JyAgWu8Z" Received: from forward100a.mail.yandex.net (forward100a.mail.yandex.net [IPv6:2a02:6b8:c0e:500:1:45:d181:d100]) by forward200a.mail.yandex.net (Yandex) with ESMTPS id D76D9C2E9E; Fri, 02 Jan 2026 20:55:07 +0300 (MSK) Received: from mail-nwsmtp-smtp-production-main-55.vla.yp-c.yandex.net (mail-nwsmtp-smtp-production-main-55.vla.yp-c.yandex.net [IPv6:2a02:6b8:c1f:582e:0:640:200:0]) by forward100a.mail.yandex.net (Yandex) with ESMTPS id 756CCC0150; Fri, 02 Jan 2026 20:54:59 +0300 (MSK) Received: by mail-nwsmtp-smtp-production-main-55.vla.yp-c.yandex.net (smtp/Yandex) with ESMTPSA id prp9aJGHE8c0-wHYenAXA; Fri, 02 Jan 2026 20:54:58 +0300 X-Yandex-Fwd: 1 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=onurozkan.dev; s=mail; t=1767376498; bh=5h1taYK8Ik1h8HK1Vjon+hdeAv1dK1mYLr6IxPRcYGw=; h=Cc:Message-ID:References:Date:In-Reply-To:Subject:To:From; b=JyAgWu8Z/+bfGwTVQAzboWruZNH8tj7jwktdcTCisOVr6B1M9lXrXbzowQr61yXLJ mZMC3pGizEgThTk7Uc92i2YOrwqsNZwoF5T510Qq+lxnzx7EGXjrocFoRgsHBJRGlA GEvf89yX0qTJ3XMKS075LOluzK1YwBNLR2pgebtE= Authentication-Results: mail-nwsmtp-smtp-production-main-55.vla.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, daniel.almeida@collabora.com, thomas.hellstrom@linux.intel.com, linux-kernel@vger.kernel.org, =?UTF-8?q?Onur=20=C3=96zkan?= Subject: [PATCH v9 4/7] rust: implement Class for ww_class support Date: Fri, 2 Jan 2026 20:52:59 +0300 Message-ID: <20260102175302.194435-5-work@onurozkan.dev> X-Mailer: git-send-email 2.51.2 In-Reply-To: <20260102175302.194435-1-work@onurozkan.dev> References: <20260102175302.194435-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 --- rust/kernel/sync/lock.rs | 1 + rust/kernel/sync/lock/ww_mutex.rs | 7 ++ rust/kernel/sync/lock/ww_mutex/class.rs | 139 ++++++++++++++++++++++++ 3 files changed, 147 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 46a57d1fc309..ef7f4a023177 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..d57478f33659 --- /dev/null +++ b/rust/kernel/sync/lock/ww_mutex/class.rs @@ -0,0 +1,139 @@ +// 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_ww_class!`] and [`define_wd_class!`] macros provide safe +//! ways to create classes. + +use crate::bindings; +use crate::prelude::*; +use crate::types::Opaque; + +/// Defines a static wound-wait [`Class`]. +/// +/// # Examples +/// +/// ``` +/// use kernel::define_ww_class; +/// +/// define_ww_class!(SOME_WW_CLASS); +/// ``` +#[macro_export] +macro_rules! define_ww_class { + ($name:ident) =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::new_unpinned( + $crate::c_str!(::core::stringify!($name)), + false, + ) + }; + }; +} + +/// Defines a static wait-die [`Class`]. +/// +/// # Examples +/// +/// ``` +/// use kernel::define_wd_class; +/// +/// define_wd_class!(SOME_WD_CLASS); +/// ``` +#[macro_export] +macro_rules! define_wd_class { + ($name:ident) =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::new_unpinned( + $crate::c_str!(::core::stringify!($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::{define_ww_class, define_wd_class}; +/// +/// define_ww_class!(SOME_WW_CLASS); +/// define_wd_class!(SOME_WD_CLASS); +/// +/// # Ok::<(), Error>(()) +/// ``` +#[pin_data] +#[repr(transparent)] +pub struct Class { + #[pin] + pub(super) inner: Opaque, +} + +impl Class { + /// Creates an unpinned [`Class`]. + /// + /// You should prefer using [`define_ww_class!`] and [`define_wd_class= !`] + /// macros. This function is `pub` only so that those macros can use i= t. + /// The alternative would be to expose the private fields of [`Class`] + /// which is less desirable. + /// + /// # Safety + /// + /// Caller must guarantee that the returned value must be pinned before + /// its first use. + pub const unsafe fn new_unpinned(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_ptr().cast(), + mutex_name: name.as_ptr().cast(), + 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`] 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() } + } +} + +// 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 {} --=20 2.51.2 From nobody Sat Feb 7 15:35:17 2026 Received: from forward102a.mail.yandex.net (forward102a.mail.yandex.net [178.154.239.85]) (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 ACD0B2F0692; Fri, 2 Jan 2026 17:55:03 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=178.154.239.85 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1767376507; cv=none; b=VTjAKDWw6Yboa66IWhJnx3GA7rT1JQOnRu6x41ebwpMeYTi1pfpK6hfuik7UD9ZOmAVJHvEaE03EZ7cXwqbkfwf4/2rQOE3AaWHdWSuLeQRmZEnnD+bHypuG+UxqbQbwRH2Ed53S8GazO0k2BjCnmV8vdMgmpbFGY1NRdc02NLQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1767376507; c=relaxed/simple; bh=25x+sAYJp7qnvxhk3sOfQe/bC59OV3tr/l5YDyAK8xg=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=IUrO14QEGr8cXe1X14xcADV+lqY2zqg+pknHZYgUhlw62sFutJJW7E7PUPpRmGJNvmRbDE/JNQp/XBYcLZ5PJIDJ1jkWXXpPl8mcaxYtJFShg4ALvEQGL+w3osRTzGcxkq7U6Kv5DkCksIa9UBeVzY1Nlmhs719bG9kYUJgYiLo= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject 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=WpM+r3kP; arc=none smtp.client-ip=178.154.239.85 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject 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="WpM+r3kP" Received: from mail-nwsmtp-smtp-production-main-55.vla.yp-c.yandex.net (mail-nwsmtp-smtp-production-main-55.vla.yp-c.yandex.net [IPv6:2a02:6b8:c1f:582e:0:640:200:0]) by forward102a.mail.yandex.net (Yandex) with ESMTPS id B16B6C0060; Fri, 02 Jan 2026 20:55:01 +0300 (MSK) Received: by mail-nwsmtp-smtp-production-main-55.vla.yp-c.yandex.net (smtp/Yandex) with ESMTPSA id prp9aJGHE8c0-CkzZj8ot; Fri, 02 Jan 2026 20:55:01 +0300 X-Yandex-Fwd: 1 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=onurozkan.dev; s=mail; t=1767376501; bh=QQqW0wvmkpCLh1acWmkvfEX/csEWCFVEhj0E3Fps+Js=; h=Cc:Message-ID:References:Date:In-Reply-To:Subject:To:From; b=WpM+r3kPkqlt35e/nYb5ds1KYMHl2JLKBprj7GFDGt8CRwKqevA2b/c2GiNSVcvl6 0shDDEGAnXck4luyYYGovbhhzFuwnYoDniGqczIhqERJeh480uvv+/hN/I6GNPbu0c Y/u2Tz4FsFlT7fXFKwx/bLu1jzvcM100GbRizsDI= Authentication-Results: mail-nwsmtp-smtp-production-main-55.vla.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, daniel.almeida@collabora.com, thomas.hellstrom@linux.intel.com, linux-kernel@vger.kernel.org, =?UTF-8?q?Onur=20=C3=96zkan?= Subject: [PATCH v9 5/7] rust: ww_mutex: add Mutex, AcquireCtx and MutexGuard Date: Fri, 2 Jan 2026 20:53:00 +0300 Message-ID: <20260102175302.194435-6-work@onurozkan.dev> X-Mailer: git-send-email 2.51.2 In-Reply-To: <20260102175302.194435-1-work@onurozkan.dev> References: <20260102175302.194435-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 Covers the entire low-level 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 | 433 ++++++++++++++++++ rust/kernel/sync/lock/ww_mutex/acquire_ctx.rs | 172 +++++++ 2 files changed, 605 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..d12d75169052 100644 --- a/rust/kernel/sync/lock/ww_mutex.rs +++ b/rust/kernel/sync/lock/ww_mutex.rs @@ -1,7 +1,440 @@ // 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. +//! +//! See srctree/Documentation/locking/ww-mutex-design.rst for more details. =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 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; +/// use kernel::sync::Arc; +/// use kernel::sync::lock::ww_mutex::{AcquireCtx, Class, Mutex}; +/// use pin_init::stack_pin_init; +/// +/// define_ww_class!(SOME_WW_CLASS); +/// +/// let mutex =3D Arc::pin_init(Mutex::new(42, &SOME_WW_CLASS), GFP_KERNEL= )?; +/// let ctx =3D KBox::pin_init(AcquireCtx::new(&SOME_WW_CLASS), GFP_KERNEL= )?; +/// +/// let guard =3D ctx.lock(&mutex)?; +/// assert_eq!(*guard, 42); +/// +/// # Ok::<(), Error>(()) +/// ``` +#[pin_data] +#[repr(C)] +pub struct Mutex<'a, T: ?Sized> { + _p: PhantomData<&'a Class>, + #[pin] + inner: Opaque, + data: UnsafeCell, +} + +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, T: ?Sized> Mutex<'class, 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. + 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`]. + pub fn lock(&self) -> Result> { + lock_common(self, None, LockKind::Regular) + } + + /// Similar to [`Self::lock`], but can be interrupted by signals. + pub fn lock_interruptible(&self) -> Result> { + lock_common(self, None, LockKind::Interruptible) + } + + /// Locks this [`Mutex`] without [`AcquireCtx`] using the slow path. + /// + /// This function should be used when [`Self::lock`] fails (typically = due + /// to a potential deadlock). + pub fn lock_slow(&self) -> Result> { + lock_common(self, None, LockKind::Slow) + } + + /// Similar to [`Self::lock_slow`], but can be interrupted by signals. + pub fn lock_slow_interruptible(&self) -> Result> { + lock_common(self, None, LockKind::SlowInterruptible) + } + + /// Tries to lock this [`Mutex`] with no [`AcquireCtx`] and without bl= ocking. + /// + /// Unlike [`Self::lock`], no deadlock handling is performed. + pub fn try_lock(&self) -> Result> { + lock_common(self, None, LockKind::Try) + } +} + +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_mutex`. + /// - `ptr` must remain valid for the lifetime `'a`. + /// - ww_class associated with this mutex must be valid for + /// the lifetime `'class`. + 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`, + // that it remains valid for the lifetime `'a` and the associated + // ww_class outlives `'class`. + // + // 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 Send for Mutex<'_, T> {} + +// SAFETY: `Mutex` can be shared across threads if the protected +// data `T` can be. +unsafe impl 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 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, +} + +impl<'a, T: ?Sized> MutexGuard<'a, T> { + /// Creates a new guard for the given [`Mutex`]. + fn new(mutex: &'a Mutex<'a, T>) -> Self { + assert!(mutex.is_locked()); + + Self { + mutex, + _not_send: NotThreadSafe, + } + } +} + +impl<'a> MutexGuard<'a, ()> { + /// Creates a [`MutexGuard`] from a raw pointer. + /// + /// If the given pointer refers to a mutex that is not locked, + /// returns [`EINVAL`]. + /// + /// 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 for = the lifetime `'b`. + pub unsafe fn from_raw<'b>(ptr: *mut bindings::ww_mutex) -> Result> { + // SAFETY: By this function's safety contract, the caller guarante= es that `ptr` points to a + // valid `ww_mutex` which is the `inner` field of a `Mutex`. The c= aller also guarantees + // that both `ptr` and the associated `ww_class` are valid for the= lifetime `'b`. + let mutex =3D unsafe { Mutex::from_raw(ptr) }; + + if !mutex.is_locked() { + return Err(EINVAL); + } + + Ok(MutexGuard::new(mutex)) + } +} + +impl core::ops::Deref for MutexGuard<'_, T> { + type Target =3D T; + + fn deref(&self) -> &Self::Target { + // SAFETY: self.mutex is locked, 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: self.mutex is locked, so we have exclusive access. + unsafe { &mut *self.mutex.data.get() } + } +} + +impl Drop for MutexGuard<'_, T> { + 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 Sync for MutexGuard<'_, T> {} + +/// 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. +/// +/// Returns [`EINVAL`] if the [`Mutex`] has a different [`Class`]. +fn lock_common<'a, T: ?Sized>( + mutex: &'a Mutex<'a, T>, + ctx: Option<&AcquireCtx<'_>>, + kind: LockKind, +) -> Result> { + let mutex_ptr =3D mutex.inner.get(); + + let ctx_ptr =3D match ctx { + Some(acquire_ctx) =3D> { + let ctx_ptr =3D acquire_ctx.inner.get(); + + // SAFETY: `ctx_ptr` is a valid pointer for the entire + // lifetime of `ctx`. + let ctx_class =3D unsafe { (*ctx_ptr).ww_class }; + + // SAFETY: `mutex_ptr` is a valid pointer for the entire + // lifetime of `mutex`. + let mutex_class =3D unsafe { (*mutex_ptr).ww_class }; + + // `ctx` and `mutex` must use the same class. + if ctx_class !=3D mutex_class { + return Err(EINVAL); + } + + ctx_ptr + } + None =3D> core::ptr::null_mut(), + }; + + 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)) +} + +#[kunit_tests(rust_kernel_ww_mutex)] +mod tests { + use crate::prelude::*; + use crate::sync::Arc; + use crate::{define_wd_class, define_ww_class}; + + use super::*; + + 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 =3D Arc::pin_init(Mutex::new(42, &TEST_WOUND_WAIT_CLASS)= , GFP_KERNEL)?; + let ctx =3D KBox::pin_init(AcquireCtx::new(&TEST_WOUND_WAIT_CLASS)= , GFP_KERNEL)?; + + let guard =3D ctx.lock(&mutex)?; + assert_eq!(*guard, 42); + + // Drop the lock. + drop(guard); + + let mut guard =3D ctx.lock(&mutex)?; + *guard =3D 100; + assert_eq!(*guard, 100); + + Ok(()) + } + + #[test] + fn test_ww_mutex_trylock() -> Result { + let mutex =3D Arc::pin_init(Mutex::new(123, &TEST_WAIT_DIE_CLASS),= GFP_KERNEL)?; + let ctx =3D KBox::pin_init(AcquireCtx::new(&TEST_WAIT_DIE_CLASS), = GFP_KERNEL)?; + + // `try_lock` on unlocked mutex should succeed. + let guard =3D ctx.try_lock(&mutex)?; + assert_eq!(*guard, 123); + + // Now it should fail immediately as it's already locked. + assert!(ctx.try_lock(&mutex).is_err()); + + Ok(()) + } + + #[test] + fn test_ww_mutex_is_locked() -> Result { + let mutex =3D Arc::pin_init(Mutex::new("hello", &TEST_WOUND_WAIT_C= LASS), GFP_KERNEL)?; + let ctx =3D KBox::pin_init(AcquireCtx::new(&TEST_WOUND_WAIT_CLASS)= , GFP_KERNEL)?; + + // Should not be locked initially. + assert!(!mutex.is_locked()); + + let guard =3D ctx.lock(&mutex)?; + assert!(mutex.is_locked()); + + drop(guard); + assert!(!mutex.is_locked()); + + Ok(()) + } + + #[test] + fn test_ww_acquire_context_done() -> Result { + let mutex1 =3D Arc::pin_init(Mutex::new(1, &TEST_WAIT_DIE_CLASS), = GFP_KERNEL)?; + let mutex2 =3D Arc::pin_init(Mutex::new(2, &TEST_WAIT_DIE_CLASS), = GFP_KERNEL)?; + let ctx =3D KBox::pin_init(AcquireCtx::new(&TEST_WAIT_DIE_CLASS), = GFP_KERNEL)?; + + // Acquire multiple mutexes with the same context. + let guard1 =3D ctx.lock(&mutex1)?; + let guard2 =3D 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`. + assert!(ctx.lock(&mutex1).is_err()); + assert!(ctx.lock(&mutex2).is_err()); + + 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(()) + } + + #[test] + fn test_guard_from_raw_with_unlocked_mutex() -> Result { + let mutex =3D Arc::pin_init(Mutex::new((), &TEST_WOUND_WAIT_CLASS)= , GFP_KERNEL)?; + + assert!(!mutex.is_locked()); + + // SAFETY: `mutex` remains valid for the duration of this test. + match unsafe { MutexGuard::from_raw(mutex.inner.get()) } { + // Should fail with `EINVAL` because the mutex is not locked. + Err(e) =3D> assert_eq!(e, EINVAL), + _ =3D> unreachable!(), + }; + + Ok(()) + } +} +>>>>>>> 90f935950ff8 (rust: ww_mutex: add Mutex, AcquireCtx and MutexGuard) 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..4b773c76bd2c --- /dev/null +++ b/rust/kernel/sync/lock/ww_mutex/acquire_ctx.rs @@ -0,0 +1,172 @@ +// 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::define_ww_class; +/// use kernel::sync::Arc; +/// use pin_init::stack_pin_init; +/// +/// define_ww_class!(SOME_WW_CLASS); +/// +/// // Create mutexes. +/// let mutex1 =3D Arc::pin_init(Mutex::new(1, &SOME_WW_CLASS), GFP_KERNEL= )?; +/// let mutex2 =3D Arc::pin_init(Mutex::new(2, &SOME_WW_CLASS), GFP_KERNEL= )?; +/// +/// // Create acquire context for deadlock avoidance. +/// let ctx =3D KBox::pin_init(AcquireCtx::new(&SOME_WW_CLASS), GFP_KERNEL= )?; +/// +/// let guard1 =3D ctx.lock(&mutex1)?; +/// let guard2 =3D 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 + /// and after calling it, no further mutexes are acquired using + /// this context. + 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 caller must ensure no locks are held in this [`AcquireCtx`]. + pub unsafe fn reinit(self: Pin<&mut Self>) { + let ctx =3D self.inner.get(); + + // SAFETY: `ww_class` is always a valid pointer in properly initia= lized + // `AcquireCtx`. + let class_ptr =3D unsafe { (*ctx).ww_class }; + + // 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 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: `ww_acquire_init` is safe to call with valid pointers + // to initialize an uninitialized context. + unsafe { bindings::ww_acquire_init(ctx, class_ptr) }; + } + + /// Locks the given [`Mutex`] on this [`AcquireCtx`]. + pub fn lock<'a, T>(&'a self, mutex: &'a Mutex<'a, T>) -> Result> { + lock_common(mutex, Some(self), LockKind::Regular) + } + + /// Similar to [`Self::lock`], but can be interrupted by signals. + pub fn lock_interruptible<'a, T>( + &'a self, + mutex: &'a Mutex<'a, T>, + ) -> Result> { + lock_common(mutex, Some(self), LockKind::Interruptible) + } + + /// Locks the given [`Mutex`] on this [`AcquireCtx`] using the slow pa= th. + /// + /// This function should be used when [`Self::lock`] fails (typically = due + /// to a potential deadlock). + pub fn lock_slow<'a, T>(&'a self, mutex: &'a Mutex<'a, T>) -> Result> { + lock_common(mutex, Some(self), LockKind::Slow) + } + + /// Similar to [`Self::lock_slow`], but can be interrupted by signals. + pub fn lock_slow_interruptible<'a, T>( + &'a self, + mutex: &'a Mutex<'a, T>, + ) -> Result> { + lock_common(mutex, Some(self), LockKind::SlowInterruptible) + } + + /// Tries to lock the [`Mutex`] on this [`AcquireCtx`] without blockin= g. + /// + /// Unlike [`Self::lock`], no deadlock handling is performed. + pub fn try_lock<'a, T>(&'a self, mutex: &'a Mutex<'a, T>) -> Result> { + 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 Sat Feb 7 15:35:17 2026 Received: from forward205a.mail.yandex.net (forward205a.mail.yandex.net [178.154.239.88]) (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 279422F9C3D; Fri, 2 Jan 2026 17:55:17 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=178.154.239.88 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1767376522; cv=none; b=uek2rclnhlHSylUTE5uUCS6Q+zj4cerIFe4VwFFn1t5eDxAoSSmCQdevhJmkAt2sR33IhezemzeQCJC5lOqYVRBYmgh6U/CrP5zzyZcwSRDT23sBCNlr5dQHEv9AIYq77WFJSE/Gl4rpwhz1ownth9OG9i8roIkB7OZl9ZeE0q8= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1767376522; c=relaxed/simple; bh=w6KnlRyoTP4eKlWnAcChKZUA9WzZFZFPtmk574yP+p8=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=OdqJ4sZHjZtRUbjJDSSjtzDoZ1J7ecJogUZQQLghjMpUjtemTBIChUUvTUVXeiM3OMecWNGdb+uM/vhSgWFYEyDEmOUTpEGwAJj1G+BlPt9wQZQv0CSDjDPGsY+yml1LvgaTmpGD0tr0aKLHBj+uYnUBtwPOuL8+8VvaXlDeuV4= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject 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=Qkq05QlT; arc=none smtp.client-ip=178.154.239.88 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject 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="Qkq05QlT" Received: from forward100a.mail.yandex.net (forward100a.mail.yandex.net [IPv6:2a02:6b8:c0e:500:1:45:d181:d100]) by forward205a.mail.yandex.net (Yandex) with ESMTPS id 6BE4EC2D46; Fri, 02 Jan 2026 20:55:12 +0300 (MSK) Received: from mail-nwsmtp-smtp-production-main-55.vla.yp-c.yandex.net (mail-nwsmtp-smtp-production-main-55.vla.yp-c.yandex.net [IPv6:2a02:6b8:c1f:582e:0:640:200:0]) by forward100a.mail.yandex.net (Yandex) with ESMTPS id 45918C0186; Fri, 02 Jan 2026 20:55:04 +0300 (MSK) Received: by mail-nwsmtp-smtp-production-main-55.vla.yp-c.yandex.net (smtp/Yandex) with ESMTPSA id prp9aJGHE8c0-uHbEsNxI; Fri, 02 Jan 2026 20:55:03 +0300 X-Yandex-Fwd: 1 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=onurozkan.dev; s=mail; t=1767376503; bh=0lTtdEMG9pUJcbBjwgY52gaQeBPBWuRmjxbYp/fttiM=; h=Cc:Message-ID:References:Date:In-Reply-To:Subject:To:From; b=Qkq05QlTxVgc7uXZtRgEe2hK/K2DYy3S/tDz2bKyaQV2fWKtYuvKztwhVjCIQfnU1 bGvaln5YQ+M2FkxOxxD+J+4EY9fGVT2VV1DEs/61kD3FmenSf+E8QhXxtPZkCkb0Ml 1NzdjAZp3m/bWX2EoGJAL1Y4pU+NvHvcRrb7vUyc= Authentication-Results: mail-nwsmtp-smtp-production-main-55.vla.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, daniel.almeida@collabora.com, thomas.hellstrom@linux.intel.com, linux-kernel@vger.kernel.org, =?UTF-8?q?Onur=20=C3=96zkan?= Subject: [PATCH v9 6/7] rust: ww_mutex: implement LockSet Date: Fri, 2 Jan 2026 20:53:01 +0300 Message-ID: <20260102175302.194435-7-work@onurozkan.dev> X-Mailer: git-send-email 2.51.2 In-Reply-To: <20260102175302.194435-1-work@onurozkan.dev> References: <20260102175302.194435-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 which provides a safe and easy to use 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 | 370 +++++++++++++++++++++ 2 files changed, 376 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 d12d75169052..ab1c74a64225 100644 --- a/rust/kernel/sync/lock/ww_mutex.rs +++ b/rust/kernel/sync/lock/ww_mutex.rs @@ -6,6 +6,10 @@ //! 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. +//! //! See srctree/Documentation/locking/ww-mutex-design.rst for more details. =20 use crate::error::to_result; @@ -18,10 +22,12 @@ =20 pub use acquire_ctx::AcquireCtx; pub use class::Class; +pub use lock_set::LockSet; =20 mod class; 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..db5ca4fe7cb4 --- /dev/null +++ b/rust/kernel/sync/lock/ww_mutex/lock_set.rs @@ -0,0 +1,370 @@ +// 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, +} + +/// 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(), + }) + } + + /// Creates a new [`LockSet`] using an existing [`AcquireCtx`]. + /// + /// # Safety + /// + /// The caller must ensure that `acquire_ctx` is properly initialized + /// and holds no [`Mutex`]es. + pub unsafe fn new_with_acquire_ctx(acquire_ctx: Pin>>) -> Self { + Self { + acquire_ctx, + taken: KVec::new(), + } + } + + /// Attempts to lock the given [`Mutex`] and stores a guard for it. + pub fn lock(&mut self, mutex: &'a Mutex<'a, T>) -> Result { + let guard =3D 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::define_ww_class; + /// use kernel::prelude::*; + /// use kernel::sync::Arc; + /// use kernel::sync::lock::ww_mutex::{LockSet, Mutex}; + /// use pin_init::stack_pin_init; + /// + /// define_ww_class!(SOME_WOUND_WAIT_CLASS); + /// + /// let mutex1 =3D Arc::pin_init(Mutex::new(0, &SOME_WOUND_WAIT_CLASS)= , GFP_KERNEL)?; + /// let mutex2 =3D Arc::pin_init(Mutex::new(0, &SOME_WOUND_WAIT_CLASS)= , GFP_KERNEL)?; + /// let mut lock_set =3D KBox::pin_init(LockSet::new(&SOME_WOUND_WAIT_= CLASS)?, GFP_KERNEL)?; + /// + /// lock_set.lock_all( + /// // `locking_algorithm` closure + /// |lock_set| { + /// lock_set.lock(&mutex1)?; + /// 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 [`Mu= tex`]. + /// + /// Fails with [`EINVAL`] if the [`Mutex`] was not locked in this [`Lo= ckSet`]. + 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 released all the locks just above. + unsafe { self.acquire_ctx.as_mut().reinit() }; + } +} + +#[kunit_tests(rust_kernel_lock_set)] +mod tests { + use crate::prelude::*; + use crate::sync::Arc; + use crate::{define_wd_class, define_ww_class}; + + use super::*; + + define_ww_class!(TEST_WOUND_WAIT_CLASS); + define_wd_class!(TEST_WAIT_DIE_CLASS); + + #[test] + fn test_lock_set_basic_lock_unlock() -> Result { + let mutex =3D Arc::pin_init(Mutex::new(10, &TEST_WOUND_WAIT_CLASS)= , GFP_KERNEL)?; + let mut lock_set =3D KBox::pin_init(LockSet::new(&TEST_WOUND_WAIT_= CLASS)?, GFP_KERNEL)?; + + 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 { + let mutex =3D Arc::pin_init(Mutex::new(5, &TEST_WOUND_WAIT_CLASS),= GFP_KERNEL)?; + let mut lock_set =3D KBox::pin_init(LockSet::new(&TEST_WOUND_WAIT_= CLASS)?, GFP_KERNEL)?; + + 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 { + let mutex1 =3D Arc::pin_init(Mutex::new(1, &TEST_WOUND_WAIT_CLASS)= , GFP_KERNEL)?; + let mutex2 =3D Arc::pin_init(Mutex::new(2, &TEST_WOUND_WAIT_CLASS)= , GFP_KERNEL)?; + let mut lock_set =3D KBox::pin_init(LockSet::new(&TEST_WOUND_WAIT_= CLASS)?, GFP_KERNEL)?; + + let res =3D lock_set.lock_all( + // `locking_algorithm` closure + |lock_set| { + let _ =3D lock_set.lock(&mutex1)?; + let _ =3D 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 { + let mutex1 =3D Arc::pin_init(Mutex::new(1, &TEST_WOUND_WAIT_CLASS)= , GFP_KERNEL)?; + let mutex2 =3D Arc::pin_init(Mutex::new("hello", &TEST_WOUND_WAIT_= CLASS), GFP_KERNEL)?; + let mut lock_set =3D KBox::pin_init(LockSet::new(&TEST_WOUND_WAIT_= CLASS)?, GFP_KERNEL)?; + + lock_set.lock_all( + // `locking_algorithm` closure + |lock_set| { + lock_set.lock(&mutex1)?; + 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 { + let mutex =3D Arc::pin_init(Mutex::new(99, &TEST_WOUND_WAIT_CLASS)= , GFP_KERNEL)?; + let mut lock_set =3D KBox::pin_init(LockSet::new(&TEST_WOUND_WAIT_= CLASS)?, GFP_KERNEL)?; + 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); + } + 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 { + let mutex =3D Arc::pin_init(Mutex::new(5, &TEST_WOUND_WAIT_CLASS),= GFP_KERNEL)?; + let mut lock_set =3D KBox::pin_init(LockSet::new(&TEST_WOUND_WAIT_= CLASS)?, GFP_KERNEL)?; + + let ecode =3D lock_set.with_locked(&mutex, |_v| {}).unwrap_err(); + assert_eq!(EINVAL, ecode); + + Ok(()) + } + + #[test] + fn test_with_different_classes() -> Result { + let mutex =3D Arc::pin_init(Mutex::new(5, &TEST_WOUND_WAIT_CLASS),= GFP_KERNEL)?; + let mut lock_set =3D KBox::pin_init(LockSet::new(&TEST_WAIT_DIE_CL= ASS)?, GFP_KERNEL)?; + + let ecode =3D lock_set.lock(&mutex).unwrap_err(); + assert_eq!(EINVAL, ecode); + + Ok(()) + } +} --=20 2.51.2 From nobody Sat Feb 7 15:35:17 2026 Received: from forward102a.mail.yandex.net (forward102a.mail.yandex.net [178.154.239.85]) (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 5F3522F9D85; Fri, 2 Jan 2026 17:55:08 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=178.154.239.85 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1767376511; cv=none; b=rJGs9DcaSTRdVjIqBFkGABWZdNJZvqvMMKhQ+wTe1VswGSg5nV1sUbe5kAkGDRs0jzAWLUYMXPKeNJ0kQTz8yJHkU5ndJysIwXCADbiwMk/eSPUUGBlWK1mOAS7z+Q4KrkCxETgpKA9LsNc17yAv09JCKThzLRYUpjsJnuz+DW4= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1767376511; c=relaxed/simple; bh=pIPbM2JoHK97PAIfWiXrLHFnud25dExbbNJRjxl3oZQ=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=Mj9XNtMl3FYpXb3EEY/yBl4aPVLFK92LO1TlJWY9gD5Cy6Luj8eqdUASAdjYyCa70nk6Thlfljv5AIu1aeU7qAoxvdjoIsMRo+KmBVxp1TuATczlBb6gXdBew6xNCEIcvgk2XQaUr0dO9xqCXxgmDY/jDbjl94hAgkG/FQq1yWk= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject 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=OFVPhIbs; arc=none smtp.client-ip=178.154.239.85 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject 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="OFVPhIbs" Received: from mail-nwsmtp-smtp-production-main-55.vla.yp-c.yandex.net (mail-nwsmtp-smtp-production-main-55.vla.yp-c.yandex.net [IPv6:2a02:6b8:c1f:582e:0:640:200:0]) by forward102a.mail.yandex.net (Yandex) with ESMTPS id 75986C0065; Fri, 02 Jan 2026 20:55:06 +0300 (MSK) Received: by mail-nwsmtp-smtp-production-main-55.vla.yp-c.yandex.net (smtp/Yandex) with ESMTPSA id prp9aJGHE8c0-cs6Lh4nw; Fri, 02 Jan 2026 20:55:06 +0300 X-Yandex-Fwd: 1 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=onurozkan.dev; s=mail; t=1767376506; bh=33qM/U8DQYbp0BuJiAKK9JU4NB+HvAWHzGL2WIebHbo=; h=Cc:Message-ID:References:Date:In-Reply-To:Subject:To:From; b=OFVPhIbswmDd1txzkU9wfPHAcBHDH56BqHuR9d65O9/IIpAOFzbtquFZj4eSTqH16 6TNIvNdDBiXC6ItVEUaydDv35AmNGDkpd1Gnr9BIvuXeDxsYA/2rrTGm6IaRFy0Ab1 eF3LRp9+8qq11k44gsDKAp7kU/1j+m+70QzIy85Y= Authentication-Results: mail-nwsmtp-smtp-production-main-55.vla.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, daniel.almeida@collabora.com, thomas.hellstrom@linux.intel.com, linux-kernel@vger.kernel.org, =?UTF-8?q?Onur=20=C3=96zkan?= Subject: [PATCH v9 7/7] =?UTF-8?q?MAINTAINERS:=20add=20Onur=20=C3=96zkan?= =?UTF-8?q?=20as=20WW=20MUTEX=20maintainer?= Date: Fri, 2 Jan 2026 20:53:02 +0300 Message-ID: <20260102175302.194435-8-work@onurozkan.dev> X-Mailer: git-send-email 2.51.2 In-Reply-To: <20260102175302.194435-1-work@onurozkan.dev> References: <20260102175302.194435-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 Add myself as a WW MUTEX maintainer to reflect ongoing work and future maintenance of WW MUTEX. Signed-off-by: Onur =C3=96zkan --- MAINTAINERS | 1 + 1 file changed, 1 insertion(+) diff --git a/MAINTAINERS b/MAINTAINERS index d245b7b38b21..10eeeaf0ccb6 100644 --- a/MAINTAINERS +++ b/MAINTAINERS @@ -14711,6 +14711,7 @@ M: Peter Zijlstra M: Ingo Molnar M: Will Deacon M: Boqun Feng (LOCKDEP & RUST) +M: Onur =C3=96zkan (WW MUTEX) R: Waiman Long L: linux-kernel@vger.kernel.org S: Maintained --=20 2.51.2