From nobody Mon Jun 8 10:56:39 2026 Received: from mail-244107.protonmail.ch (mail-244107.protonmail.ch [109.224.244.107]) (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 5774A1EB5C2; Fri, 29 May 2026 13:40:17 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=109.224.244.107 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1780062020; cv=none; b=Y+Nk7/bnxQNuSrlRBZC0wWKEIc+rW+VfYjINqmQCG/g4MF86ZXLwvJtgPdrIy7xAjKPaUXdlK1FEjU6M9Pz38z9JKHHv3RtqH+AYaWxpMkhetZnisUn4G51++E6ufCHSD5FJnboxbH4rFvzQ4qmOF0uQDClOyaTvDyyf2+np6oo= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1780062020; c=relaxed/simple; bh=GRu2w6Yl53hjYDtCSyWax23qkoJc99/mS9lhpS2cZNs=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=TWmJ6ZYRNwUvY9gTMTVeaLZm1j21Hw53Se8lwFWo53bZggIttEsYjzKOrcu0zyOOIT8hdjDJAAUZUq59TNhAmIb9csJ09DipoiTuJPKTi7zy+NLxe1rq3rg2bkor/O9hrJPDedNquVIHxDkG3qRPisjQ7JUoBe7+ncYfWLJhWS0= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=onurozkan.dev; spf=pass smtp.mailfrom=onurozkan.dev; dkim=pass (2048-bit key) header.d=onurozkan.dev header.i=@onurozkan.dev header.b=QpxB0QRR; arc=none smtp.client-ip=109.224.244.107 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine 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 (2048-bit key) header.d=onurozkan.dev header.i=@onurozkan.dev header.b="QpxB0QRR" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=onurozkan.dev; s=protonmail; t=1780062014; x=1780321214; bh=ZL9ESIKvQirxwn04NTuae03syak/bnIEqsL+uBn0S2o=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References:From:To: Cc:Date:Subject:Reply-To:Feedback-ID:Message-ID:BIMI-Selector; b=QpxB0QRRMygiPZjw1oV34QjExJrxrqAkCwknEpeMVyiFSEa0sh+6rIRO+2HcXiHtE r8i4FTFmOR0vNj5Se2zIwa1/2QIr0LFjtCm7PQbYGg+8CM5fugC5yee1CyjdWpPJeN 4cdlEbccFe/ffRjLHfi0EGfanamgIPy7cGSMnBZO4NUSuu30zbh6ToKcBu607l1SvL t1q/aqRscmt//RZNPfjJOnDKnpqlbmclaNvGuDJK3sluOhPcGEle5qBY1SDuiq82Lp 171WSKio6Ks9gvuZYrParYATnU30YkZRDbkVqIn1+hvauWTbu41/5P+yre2rrVrQuq Ts2aAxsxYqbWg== X-Pm-Submission-Id: 4gRkxk4FqHz1DDLL From: =?UTF-8?q?Onur=20=C3=96zkan?= To: rcu@vger.kernel.org, rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org Cc: ojeda@kernel.org, boqun@kernel.org, gary@garyguo.net, bjorn3_gh@protonmail.com, lossin@kernel.org, a.hindborg@kernel.org, aliceryhl@google.com, tmgross@umich.edu, dakr@kernel.org, peterz@infradead.org, fujita.tomonori@gmail.com, tamird@kernel.org, jiangshanlai@gmail.com, paulmck@kernel.org, josh@joshtriplett.org, rostedt@goodmis.org, mathieu.desnoyers@efficios.com, =?UTF-8?q?Onur=20=C3=96zkan?= Subject: [PATCH v9 1/4] rust: helpers: add SRCU helpers Date: Fri, 29 May 2026 16:39:51 +0300 Message-ID: <20260529134004.396743-2-work@onurozkan.dev> X-Mailer: git-send-email 2.51.2 In-Reply-To: <20260529134004.396743-1-work@onurozkan.dev> References: <20260529134004.396743-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 helper wrappers for SRCU functions that are exposed to Rust through generated bindings. Signed-off-by: Onur =C3=96zkan --- include/linux/srcu.h | 29 ++++++++++++++++++++--------- kernel/rcu/srcutiny.c | 10 +++++----- kernel/rcu/srcutree.c | 9 +++++---- rust/helpers/helpers.c | 1 + rust/helpers/srcu.c | 30 ++++++++++++++++++++++++++++++ 5 files changed, 61 insertions(+), 18 deletions(-) create mode 100644 rust/helpers/srcu.c diff --git a/include/linux/srcu.h b/include/linux/srcu.h index 81b1938512d5..a028a5b5ebef 100644 --- a/include/linux/srcu.h +++ b/include/linux/srcu.h @@ -25,20 +25,19 @@ context_lock_struct(srcu_struct, __reentrant_ctx_lock); =20 #ifdef CONFIG_DEBUG_LOCK_ALLOC =20 -int __init_srcu_struct(struct srcu_struct *ssp, const char *name, struct l= ock_class_key *key); +int init_srcu_struct_lockdep(struct srcu_struct *ssp, const char *name, + struct lock_class_key *key); +static inline int __init_srcu_struct(struct srcu_struct *ssp, const char *= name, + struct lock_class_key *key) +{ + return init_srcu_struct_lockdep(ssp, name, key); +} #ifndef CONFIG_TINY_SRCU int __init_srcu_struct_fast(struct srcu_struct *ssp, const char *name, str= uct lock_class_key *key); int __init_srcu_struct_fast_updown(struct srcu_struct *ssp, const char *na= me, struct lock_class_key *key); #endif // #ifndef CONFIG_TINY_SRCU =20 -#define init_srcu_struct(ssp) \ -({ \ - static struct lock_class_key __srcu_key; \ - \ - __init_srcu_struct((ssp), #ssp, &__srcu_key); \ -}) - #define init_srcu_struct_fast(ssp) \ ({ \ static struct lock_class_key __srcu_key; \ @@ -56,7 +55,12 @@ int __init_srcu_struct_fast_updown(struct srcu_struct *s= sp, const char *name, #define __SRCU_DEP_MAP_INIT(srcu_name) .dep_map =3D { .name =3D #srcu_name= }, #else /* #ifdef CONFIG_DEBUG_LOCK_ALLOC */ =20 -int init_srcu_struct(struct srcu_struct *ssp); +int init_srcu_struct_generic(struct srcu_struct *ssp); +static inline int __init_srcu_struct(struct srcu_struct *ssp, const char *= name, + struct lock_class_key *key) +{ + return init_srcu_struct_generic(ssp); +} #ifndef CONFIG_TINY_SRCU int init_srcu_struct_fast(struct srcu_struct *ssp); int init_srcu_struct_fast_updown(struct srcu_struct *ssp); @@ -65,6 +69,13 @@ int init_srcu_struct_fast_updown(struct srcu_struct *ssp= ); #define __SRCU_DEP_MAP_INIT(srcu_name) #endif /* #else #ifdef CONFIG_DEBUG_LOCK_ALLOC */ =20 +#define init_srcu_struct(ssp) \ +({ \ + static struct lock_class_key __srcu_key; \ + \ + __init_srcu_struct((ssp), #ssp, &__srcu_key); \ +}) + /* Values for SRCU Tree srcu_data ->srcu_reader_flavor, but also used by r= cutorture. */ #define SRCU_READ_FLAVOR_NORMAL 0x1 // srcu_read_lock(). #define SRCU_READ_FLAVOR_NMI 0x2 // srcu_read_lock_nmisafe(). diff --git a/kernel/rcu/srcutiny.c b/kernel/rcu/srcutiny.c index a2e2d516e51b..780a95e8cad7 100644 --- a/kernel/rcu/srcutiny.c +++ b/kernel/rcu/srcutiny.c @@ -48,15 +48,15 @@ static int init_srcu_struct_fields(struct srcu_struct *= ssp) =20 #ifdef CONFIG_DEBUG_LOCK_ALLOC =20 -int __init_srcu_struct(struct srcu_struct *ssp, const char *name, - struct lock_class_key *key) +int init_srcu_struct_lockdep(struct srcu_struct *ssp, const char *name, + struct lock_class_key *key) { /* Don't re-initialize a lock while it is held. */ debug_check_no_locks_freed((void *)ssp, sizeof(*ssp)); lockdep_init_map(&ssp->dep_map, name, key, 0); return init_srcu_struct_fields(ssp); } -EXPORT_SYMBOL_GPL(__init_srcu_struct); +EXPORT_SYMBOL_GPL(init_srcu_struct_lockdep); =20 #else /* #ifdef CONFIG_DEBUG_LOCK_ALLOC */ =20 @@ -68,11 +68,11 @@ EXPORT_SYMBOL_GPL(__init_srcu_struct); * to any other function. Each srcu_struct represents a separate domain * of SRCU protection. */ -int init_srcu_struct(struct srcu_struct *ssp) +int init_srcu_struct_generic(struct srcu_struct *ssp) { return init_srcu_struct_fields(ssp); } -EXPORT_SYMBOL_GPL(init_srcu_struct); +EXPORT_SYMBOL_GPL(init_srcu_struct_generic); =20 #endif /* #else #ifdef CONFIG_DEBUG_LOCK_ALLOC */ =20 diff --git a/kernel/rcu/srcutree.c b/kernel/rcu/srcutree.c index 0d01cd8c4b4a..45154630c54e 100644 --- a/kernel/rcu/srcutree.c +++ b/kernel/rcu/srcutree.c @@ -266,12 +266,13 @@ __init_srcu_struct_common(struct srcu_struct *ssp, co= nst char *name, struct lock return init_srcu_struct_fields(ssp, false); } =20 -int __init_srcu_struct(struct srcu_struct *ssp, const char *name, struct l= ock_class_key *key) +int init_srcu_struct_lockdep(struct srcu_struct *ssp, const char *name, + struct lock_class_key *key) { ssp->srcu_reader_flavor =3D 0; return __init_srcu_struct_common(ssp, name, key); } -EXPORT_SYMBOL_GPL(__init_srcu_struct); +EXPORT_SYMBOL_GPL(init_srcu_struct_lockdep); =20 int __init_srcu_struct_fast(struct srcu_struct *ssp, const char *name, str= uct lock_class_key *key) { @@ -301,12 +302,12 @@ EXPORT_SYMBOL_GPL(__init_srcu_struct_fast_updown); * to any other function. Each srcu_struct represents a separate domain * of SRCU protection. */ -int init_srcu_struct(struct srcu_struct *ssp) +int init_srcu_struct_generic(struct srcu_struct *ssp) { ssp->srcu_reader_flavor =3D 0; return init_srcu_struct_fields(ssp, false); } -EXPORT_SYMBOL_GPL(init_srcu_struct); +EXPORT_SYMBOL_GPL(init_srcu_struct_generic); =20 /** * init_srcu_struct_fast - initialize a fast-reader sleep-RCU structure diff --git a/rust/helpers/helpers.c b/rust/helpers/helpers.c index 625921e27dfb..f3562d3b3888 100644 --- a/rust/helpers/helpers.c +++ b/rust/helpers/helpers.c @@ -88,6 +88,7 @@ #include "signal.c" #include "slab.c" #include "spinlock.c" +#include "srcu.c" #include "sync.c" #include "task.c" #include "time.c" diff --git a/rust/helpers/srcu.c b/rust/helpers/srcu.c new file mode 100644 index 000000000000..225b3bf9334a --- /dev/null +++ b/rust/helpers/srcu.c @@ -0,0 +1,30 @@ +// SPDX-License-Identifier: GPL-2.0 + +#include + +__rust_helper int rust_helper_init_srcu_struct_with_key(struct srcu_struct= *ssp, + const char *name, + struct lock_class_key *key) +{ + return __init_srcu_struct(ssp, name, key); +} + +__rust_helper int rust_helper_srcu_read_lock(struct srcu_struct *ssp) +{ + return srcu_read_lock(ssp); +} + +__rust_helper void rust_helper_srcu_read_unlock(struct srcu_struct *ssp, i= nt idx) +{ + srcu_read_unlock(ssp, idx); +} + +__rust_helper void rust_helper_srcu_barrier(struct srcu_struct *ssp) +{ + srcu_barrier(ssp); +} + +__rust_helper void rust_helper_synchronize_srcu_expedited(struct srcu_stru= ct *ssp) +{ + synchronize_srcu_expedited(ssp); +} --=20 2.51.2 From nobody Mon Jun 8 10:56:39 2026 Received: from mail-244108.protonmail.ch (mail-244108.protonmail.ch [109.224.244.108]) (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 3E16C327C00 for ; Fri, 29 May 2026 13:40:19 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=109.224.244.108 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1780062022; cv=none; b=Y4EttylGV1roxw1Mbq75dojN9dahCpa2M/Zw2MYC5s64TVg2WK0GveXZ9RBOn4fMPLtpWuPVDoTDWRLzIgU09OLFxMpK4oycB8hln4GcCeK/gpppzTOAPMpeIfIYY8h1tTgtdmkiiXVxbKVXPqGpGK0WTV8FBZ6DcpJlq1Ugjng= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1780062022; c=relaxed/simple; bh=AqTNnLPq2ilHYXq41hOFJaefF2Ku6iuUxVu/iVxfiIQ=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=KS5QlsvpFHAi46fiLL2AtVNL3jcYzNPv13tyiOWdoESABp0v1E3eKYV740wudylgc/kfTjy8Ihx+NyP7tAvvHAVsDgTXCl3EFdd6MClpumiKmIVLILAKa1R0m/tEyEGON1N3FBfXc8xBiZufr2k0i2rhl3ib7DuHilJmFUA95y0= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=onurozkan.dev; spf=pass smtp.mailfrom=onurozkan.dev; dkim=pass (2048-bit key) header.d=onurozkan.dev header.i=@onurozkan.dev header.b=s++3AGjs; arc=none smtp.client-ip=109.224.244.108 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine 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 (2048-bit key) header.d=onurozkan.dev header.i=@onurozkan.dev header.b="s++3AGjs" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=onurozkan.dev; s=protonmail; t=1780062017; x=1780321217; bh=RS5uw7rrHdO0zbI3L/86c50tam7X+ja2SpZF9/n5OEw=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References:From:To: Cc:Date:Subject:Reply-To:Feedback-ID:Message-ID:BIMI-Selector; b=s++3AGjsQnVbIGQLhDbZBiSOydRp+FZXYAp9skGL7sX3+xlawULsL2mHGalOsOe7M cWHHrOs1NvtnEMtlpzROumfkFlSo1jPMPFJO725zU1q0IJ0RSEGqdW+WD6nwIh750P MFnPnOjjRQsHI74inu1YuL4Q6RhdhAd/tmmyjS3y26rdGlSufSRAnGlmW7tPu0G57E XkCFNw9YLMOzmJVik29EUUmAQeY6f2LfnYfNezLzgAX4Md8DxYh9gptXeqiIj/eywM h4DCKi+kaCGERSo+r24v6MZA27NucfjNO0bsy3V5f3YETPteI80YhUtikMgN/VsUFg Zq8gU8WVKG9OA== X-Pm-Submission-Id: 4gRkxn54VVz1DDKv From: =?UTF-8?q?Onur=20=C3=96zkan?= To: rcu@vger.kernel.org, rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org Cc: ojeda@kernel.org, boqun@kernel.org, gary@garyguo.net, bjorn3_gh@protonmail.com, lossin@kernel.org, a.hindborg@kernel.org, aliceryhl@google.com, tmgross@umich.edu, dakr@kernel.org, peterz@infradead.org, fujita.tomonori@gmail.com, tamird@kernel.org, jiangshanlai@gmail.com, paulmck@kernel.org, josh@joshtriplett.org, rostedt@goodmis.org, mathieu.desnoyers@efficios.com, =?UTF-8?q?Onur=20=C3=96zkan?= Subject: [PATCH v9 2/4] srcu: expose srcu_readers_active() Date: Fri, 29 May 2026 16:39:52 +0300 Message-ID: <20260529134004.396743-3-work@onurozkan.dev> X-Mailer: git-send-email 2.51.2 In-Reply-To: <20260529134004.396743-1-work@onurozkan.dev> References: <20260529134004.396743-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 by rust/helpers/srcu.c which now adds rust_helper_srcu_readers_active() as a wrapper around the SRCU helper for Rust callers. To achive this: 1- Move the srcu_readers_active() implementation from "kernel/rcu/srcutree.c" to "include/linux/srcutree.h". 2- Implement a matching srcu_readers_active() in "include/linux/srcutiny.h" and use it on the existing open-coded WARN_ON() check in cleanup_srcu_struct(). Reviewed-by: Gary Guo Reviewed-by: Paul E. McKenney Signed-off-by: Onur =C3=96zkan --- include/linux/srcutiny.h | 13 +++++++++++++ include/linux/srcutree.h | 24 ++++++++++++++++++++++++ kernel/rcu/srcutiny.c | 2 +- kernel/rcu/srcutree.c | 25 ------------------------- rust/helpers/srcu.c | 5 +++++ 5 files changed, 43 insertions(+), 26 deletions(-) diff --git a/include/linux/srcutiny.h b/include/linux/srcutiny.h index 905b629e8fa3..fbcf13bc12d1 100644 --- a/include/linux/srcutiny.h +++ b/include/linux/srcutiny.h @@ -154,4 +154,17 @@ static inline void srcu_torture_stats_print(struct src= u_struct *ssp, data_race(READ_ONCE(ssp->srcu_idx_max))); } =20 +/** + * srcu_readers_active - returns true if there are readers. and false othe= rwise. + * @ssp: which srcu_struct to count active readers (holding srcu_read_lock= ). + * + * Note that this is not an atomic primitive, and can therefore suffer + * severe errors when invoked on an active srcu_struct. That said, it + * can be useful as an error check at cleanup time. + */ +static inline bool srcu_readers_active(struct srcu_struct *ssp) +{ + return READ_ONCE(ssp->srcu_lock_nesting[0]) || READ_ONCE(ssp->srcu_lock_n= esting[1]); +} + #endif diff --git a/include/linux/srcutree.h b/include/linux/srcutree.h index fd1a9270cb9a..75e54e4f963f 100644 --- a/include/linux/srcutree.h +++ b/include/linux/srcutree.h @@ -374,4 +374,28 @@ static inline void srcu_check_read_flavor(struct srcu_= struct *ssp, int read_flav __srcu_check_read_flavor(ssp, read_flavor); } =20 +/** + * srcu_readers_active - returns true if there are readers. and false othe= rwise. + * @ssp: which srcu_struct to count active readers (holding srcu_read_lock= ). + * + * Note that this is not an atomic primitive, and can therefore suffer + * severe errors when invoked on an active srcu_struct. That said, it + * can be useful as an error check at cleanup time. + */ +static inline bool srcu_readers_active(struct srcu_struct *ssp) +{ + int cpu; + unsigned long sum =3D 0; + + for_each_possible_cpu(cpu) { + struct srcu_data *sdp =3D per_cpu_ptr(ssp->sda, cpu); + + sum +=3D atomic_long_read(&sdp->srcu_ctrs[0].srcu_locks); + sum +=3D atomic_long_read(&sdp->srcu_ctrs[1].srcu_locks); + sum -=3D atomic_long_read(&sdp->srcu_ctrs[0].srcu_unlocks); + sum -=3D atomic_long_read(&sdp->srcu_ctrs[1].srcu_unlocks); + } + return sum; +} + #endif diff --git a/kernel/rcu/srcutiny.c b/kernel/rcu/srcutiny.c index 780a95e8cad7..dde99876d473 100644 --- a/kernel/rcu/srcutiny.c +++ b/kernel/rcu/srcutiny.c @@ -85,7 +85,7 @@ EXPORT_SYMBOL_GPL(init_srcu_struct_generic); */ void cleanup_srcu_struct(struct srcu_struct *ssp) { - WARN_ON(ssp->srcu_lock_nesting[0] || ssp->srcu_lock_nesting[1]); + WARN_ON(srcu_readers_active(ssp)); irq_work_sync(&ssp->srcu_irq_work); flush_work(&ssp->srcu_work); WARN_ON(ssp->srcu_gp_running); diff --git a/kernel/rcu/srcutree.c b/kernel/rcu/srcutree.c index 45154630c54e..d1a69320b5c0 100644 --- a/kernel/rcu/srcutree.c +++ b/kernel/rcu/srcutree.c @@ -599,31 +599,6 @@ static bool srcu_readers_active_idx_check(struct srcu_= struct *ssp, int idx) return srcu_readers_lock_idx(ssp, idx, did_gp, unlocks); } =20 -/** - * srcu_readers_active - returns true if there are readers. and false - * otherwise - * @ssp: which srcu_struct to count active readers (holding srcu_read_lock= ). - * - * Note that this is not an atomic primitive, and can therefore suffer - * severe errors when invoked on an active srcu_struct. That said, it - * can be useful as an error check at cleanup time. - */ -static bool srcu_readers_active(struct srcu_struct *ssp) -{ - int cpu; - unsigned long sum =3D 0; - - for_each_possible_cpu(cpu) { - struct srcu_data *sdp =3D per_cpu_ptr(ssp->sda, cpu); - - sum +=3D atomic_long_read(&sdp->srcu_ctrs[0].srcu_locks); - sum +=3D atomic_long_read(&sdp->srcu_ctrs[1].srcu_locks); - sum -=3D atomic_long_read(&sdp->srcu_ctrs[0].srcu_unlocks); - sum -=3D atomic_long_read(&sdp->srcu_ctrs[1].srcu_unlocks); - } - return sum; -} - /* * We use an adaptive strategy for synchronize_srcu() and especially for * synchronize_srcu_expedited(). We spin for a fixed time period diff --git a/rust/helpers/srcu.c b/rust/helpers/srcu.c index 225b3bf9334a..1a2f563640e0 100644 --- a/rust/helpers/srcu.c +++ b/rust/helpers/srcu.c @@ -9,6 +9,11 @@ __rust_helper int rust_helper_init_srcu_struct_with_key(st= ruct srcu_struct *ssp, return __init_srcu_struct(ssp, name, key); } =20 +__rust_helper bool rust_helper_srcu_readers_active(struct srcu_struct *ssp) +{ + return srcu_readers_active(ssp); +} + __rust_helper int rust_helper_srcu_read_lock(struct srcu_struct *ssp) { return srcu_read_lock(ssp); --=20 2.51.2 From nobody Mon Jun 8 10:56:39 2026 Received: from mail-43170.protonmail.ch (mail-43170.protonmail.ch [185.70.43.170]) (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 7356A33A6F7 for ; Fri, 29 May 2026 13:40:22 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=185.70.43.170 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1780062025; cv=none; b=qiIoR1wihgmkggCORK3rDZ+07JNhIjlroTzyLItqa0fmqjlpxQlCIu2R0Y1Gf5Bm60lZ7F2Y+868t7zFONyd1mZsSSyzRbvfyDqp7iMV0BQAdXycT4eitoCVZfD3LwBq50P13OI6s0AE9n6us+++tB+KwvkURSzZHMsPV6xJVgA= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1780062025; c=relaxed/simple; bh=giXeFeoUiWrc7spHUMsnnZecEIRXFQlqry7Q7BYcCbY=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=jR89EhWHdI6i9I7c7EX3t1QYr5tSl1drLUQ9OjHlSdQh6jcoDMeDhSrFYEiNVnKnywtZXGeWEvxbmy2ONbahWkjyWjE+LXmFeyJxCKvVWR2ERovlc1Kv+v9MpKfxunKSbIJxm9HTPRew+aKB1tEG8eHgvVqg7tuNPkLoiBfLFV4= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=onurozkan.dev; spf=pass smtp.mailfrom=onurozkan.dev; dkim=pass (2048-bit key) header.d=onurozkan.dev header.i=@onurozkan.dev header.b=n+pLyoTp; arc=none smtp.client-ip=185.70.43.170 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine 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 (2048-bit key) header.d=onurozkan.dev header.i=@onurozkan.dev header.b="n+pLyoTp" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=onurozkan.dev; s=protonmail; t=1780062020; x=1780321220; bh=EK7O32EuuqORuh6CzkLMiljHX4OuL+Z/KW00TvctW18=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References:From:To: Cc:Date:Subject:Reply-To:Feedback-ID:Message-ID:BIMI-Selector; b=n+pLyoTpPW+SGy7RjVa0/aCbu/jyqW+/qs1gIMccbugFbkx1ZFvTbO0gBnGrcDtUa +AzFy0XxAyTk9vAWDRRL01pHBN1wvKUqjtgyclbD/ODlPcmqSNjKE/awTfiE1QeErw n46fccifvk61uWjPZWHGWyTaQlP5HAPv3blN4yzLBU4WOQQupgIt9LVzJkOS3svxsx Cn8cqYNBRUbyggQkFMY1zCuUU5kqoOkfU+IQz8of56KizPLMChILRfAQmLx8L6qQdG Wmx2z0v3KenNyLR5q1JXo6vH56QpDioiMO8vmBmfKE8iy/1EsmXKBjZ3hdTFK2opde YFZppIq6OKgRw== X-Pm-Submission-Id: 4gRkxr67rhz1DDL0 From: =?UTF-8?q?Onur=20=C3=96zkan?= To: rcu@vger.kernel.org, rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org Cc: ojeda@kernel.org, boqun@kernel.org, gary@garyguo.net, bjorn3_gh@protonmail.com, lossin@kernel.org, a.hindborg@kernel.org, aliceryhl@google.com, tmgross@umich.edu, dakr@kernel.org, peterz@infradead.org, fujita.tomonori@gmail.com, tamird@kernel.org, jiangshanlai@gmail.com, paulmck@kernel.org, josh@joshtriplett.org, rostedt@goodmis.org, mathieu.desnoyers@efficios.com, =?UTF-8?q?Onur=20=C3=96zkan?= Subject: [PATCH v9 3/4] rust: sync: add SRCU abstraction Date: Fri, 29 May 2026 16:39:53 +0300 Message-ID: <20260529134004.396743-4-work@onurozkan.dev> X-Mailer: git-send-email 2.51.2 In-Reply-To: <20260529134004.396743-1-work@onurozkan.dev> References: <20260529134004.396743-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 a Rust abstraction for sleepable RCU (SRCU), backed by C srcu_struct. Provide FFI helpers and a safe wrapper with a guard-based API for read-side critical sections. Cleanup is handled via `PinnedDrop`. It first checks for active read-side sections and emits a warning if any guards were leaked. In that case, it waits in `synchronize_srcu()` rather than risking a UAF by freeing the `srcu_struct` that is still reachable from the C side. It then uses `srcu_barrier()` to drain pending callbacks before finally calling `cleanup_srcu_struct()`. Signed-off-by: Onur =C3=96zkan --- rust/kernel/sync.rs | 2 + rust/kernel/sync/srcu.rs | 171 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 173 insertions(+) create mode 100644 rust/kernel/sync/srcu.rs diff --git a/rust/kernel/sync.rs b/rust/kernel/sync.rs index 993dbf2caa0e..0d6a5f1300c3 100644 --- a/rust/kernel/sync.rs +++ b/rust/kernel/sync.rs @@ -21,6 +21,7 @@ pub mod rcu; mod refcount; mod set_once; +pub mod srcu; =20 pub use arc::{Arc, ArcBorrow, UniqueArc}; pub use completion::Completion; @@ -31,6 +32,7 @@ pub use locked_by::LockedBy; pub use refcount::Refcount; pub use set_once::SetOnce; +pub use srcu::Srcu; =20 /// Represents a lockdep class. /// diff --git a/rust/kernel/sync/srcu.rs b/rust/kernel/sync/srcu.rs new file mode 100644 index 000000000000..723e5e277fd6 --- /dev/null +++ b/rust/kernel/sync/srcu.rs @@ -0,0 +1,171 @@ +// SPDX-License-Identifier: GPL-2.0 + +//! Sleepable read-copy update (SRCU) support. +//! +//! C header: [`include/linux/srcu.h`](srctree/include/linux/srcu.h) + +use crate::{ + bindings, + error::to_result, + prelude::*, + sync::LockClassKey, + types::{ + NotThreadSafe, + Opaque, // + }, +}; + +use pin_init::pin_data; + +/// Creates an [`Srcu`] initialiser with the given name and a newly-create= d lock class. +#[doc(hidden)] +#[macro_export] +macro_rules! new_srcu { + ($($name:literal)?) =3D> { + $crate::sync::Srcu::new($crate::optional_name!($($name)?), $crate:= :static_lock_class!()) + }; +} +pub use new_srcu; + +/// Sleepable read-copy update primitive. +/// +/// SRCU readers may sleep while holding the read-side guard. +/// +/// The destructor waits for active readers and callbacks, so it may sleep. +/// If a read-side guard has been leaked, dropping an [`Srcu`] may never r= eturn. +/// +/// # Invariants +/// +/// This represents a valid `struct srcu_struct` initialized by the C SRCU= API +/// and it remains pinned and valid until the pinned destructor runs. +#[repr(transparent)] +#[pin_data(PinnedDrop)] +pub struct Srcu { + #[pin] + inner: Opaque, +} + +impl Srcu { + /// Creates a new SRCU instance. + #[inline] + pub fn new(name: &'static CStr, key: Pin<&'static LockClassKey>) -> im= pl PinInit { + try_pin_init!(Self { + // INVARIANT: On success, the C initializer creates a valid `s= rcu_struct` and + // it remains pinned until `PinnedDrop` runs. + inner <- Opaque::try_ffi_init(|ptr: *mut bindings::srcu_struct= | { + // SAFETY: `ptr` points to valid uninitialised memory for = a `srcu_struct`. + to_result(unsafe { + bindings::init_srcu_struct_with_key(ptr, name.as_char_= ptr(), key.as_ptr()) + }) + }), + }) + } + + /// Enters an SRCU read-side critical section. + /// + /// Leaking the returned [`Guard`] leaves the SRCU read-side critical + /// section active and makes `drop` sleep forever. + #[inline] + pub fn read_lock(&self) -> Guard<'_> { + // SAFETY: By the type invariants, `self` contains a valid `struct= srcu_struct`. + let idx =3D unsafe { bindings::srcu_read_lock(self.inner.get()) }; + + // INVARIANT: `idx` was returned by `srcu_read_lock()` for this `S= rcu`. + Guard { + srcu: self, + idx, + _not_send: NotThreadSafe, + } + } + + /// Waits until all pre-existing SRCU readers have completed. + #[inline] + pub fn synchronize(&self) { + // SAFETY: By the type invariants, `self` contains a valid `struct= srcu_struct`. + unsafe { bindings::synchronize_srcu(self.inner.get()) }; + } + + /// Waits until all pre-existing SRCU readers have completed, expedite= d. + /// + /// This requests a lower-latency grace period than [`Srcu::synchroniz= e`] typically + /// at the cost of higher system-wide overhead. Prefer [`Srcu::synchro= nize`] by default + /// and use this variant only when reducing reset or teardown latency = is more important + /// than the extra cost. + #[inline] + pub fn synchronize_expedited(&self) { + // SAFETY: By the type invariants, `self` contains a valid `struct= srcu_struct`. + unsafe { bindings::synchronize_srcu_expedited(self.inner.get()) }; + } +} + +#[pinned_drop] +impl PinnedDrop for Srcu { + fn drop(self: Pin<&mut Self>) { + let ptr =3D self.inner.get(); + + if crate::warn_on!( + // SAFETY: By the type invariants, `self` contains a valid and= pinned `struct srcu_struct` + // and `srcu_readers_active()` only checks the active reader c= ount. + unsafe { bindings::srcu_readers_active(ptr) } + ) { + // `cleanup_srcu_struct()` may return early if there are still= active readers. + // This should only happen if a guard was leaked with `mem::fo= rget`, which is + // "WRONG" code and may cause a UAF because Rust will free the= `srcu_struct` + // while it is still referenced from the C side (e.g. by `call= _srcu()` callbacks). + // + // Another consequence of leaking guards is that `call_srcu()`= callbacks will + // never run because the grace period can never complete due t= o permanently + // active readers (i.e. leaked guards). + // + // If this ever happens, that means the guard was leaked by mi= stake and the + // caller must fix the bug. Sleeping here is intentional and l= ess harmful + // than risking a UAF. + // + // SAFETY: By the type invariants, `self` contains a valid and= pinned + // `struct srcu_struct`. + unsafe { bindings::synchronize_srcu(ptr) }; + } + + // Ensure all SRCU callbacks have been finished before freeing. + // SAFETY: By the type invariants, `self` contains a valid and pin= ned `struct srcu_struct`. + unsafe { bindings::srcu_barrier(ptr) }; + + // SAFETY: By the type invariants, `self` contains a valid and pin= ned `struct srcu_struct`. + unsafe { bindings::cleanup_srcu_struct(ptr) }; + } +} + +// SAFETY: `srcu_struct` may be shared and used across threads. +unsafe impl Send for Srcu {} +// SAFETY: `srcu_struct` may be shared and used concurrently. +unsafe impl Sync for Srcu {} + +/// Guard for an active SRCU read-side critical section on a particular [`= Srcu`]. +/// +/// Leaking this guard with [`core::mem::forget`] leaves the SRCU read-side +/// critical section active and makes dropping the associated [`Srcu`] sle= ep forever. +/// +/// # Invariants +/// +/// `idx` is the index returned by `srcu_read_lock()` for `srcu`. +#[must_use =3D "if unused, the lock will be immediately unlocked"] +pub struct Guard<'a> { + srcu: &'a Srcu, + idx: i32, + _not_send: NotThreadSafe, +} + +impl Guard<'_> { + /// Explicitly releases the SRCU read-side critical section. + #[inline] + pub fn unlock(self) {} +} + +impl Drop for Guard<'_> { + #[inline] + fn drop(&mut self) { + // SAFETY: `Guard` is only constructible through `Srcu::read_lock(= )`, + // which returns a valid index for the SRCU instance. + unsafe { bindings::srcu_read_unlock(self.srcu.inner.get(), self.id= x) }; + } +} --=20 2.51.2 From nobody Mon Jun 8 10:56:39 2026 Received: from mail-43172.protonmail.ch (mail-43172.protonmail.ch [185.70.43.172]) (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 960CE33C18B for ; Fri, 29 May 2026 13:40:25 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=185.70.43.172 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1780062035; cv=none; b=Fi86r8//9VN4Lorh4m70BEiZzyLKAcE0pRlEK0Mj6K40O225mm6XHWZZO7yg54+ZzXztWfD6SMEuvc4+ukc6ixqj3SuYRtBVC02ft8vFatLarTnqpS7XgfFFFGMDMJdiqzRWNQssi43bdkuAJ4IX1tHq1OMHlptt50nb+LlcBXU= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1780062035; c=relaxed/simple; bh=iNIea3X1u5UlLSCKpcd9EjyAvSlc5TEnDXzmsvHUUBY=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=PDhDV+ecuFLV36nT15QMz7s3qayGe0xFEpjoUtZCxY/hPLH/ZkGqJZhJr3PDOpp4UaCqUQK9tVUC4+l0lsdhXmvmkYUYT+qrHeFXOdy7W9FCbZiNc8O+WjaEyiImcbT/3ENAMCwE6noRIYD6BuakfaT+IGELkCNMVbNGDdIHNtM= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=onurozkan.dev; spf=pass smtp.mailfrom=onurozkan.dev; dkim=pass (2048-bit key) header.d=onurozkan.dev header.i=@onurozkan.dev header.b=C1VSkMtK; arc=none smtp.client-ip=185.70.43.172 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine 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 (2048-bit key) header.d=onurozkan.dev header.i=@onurozkan.dev header.b="C1VSkMtK" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=onurozkan.dev; s=protonmail; t=1780062022; x=1780321222; bh=JkD+A35aA68GLUHdDWX6WjTBKf+CG0Z+zqkNuKRO2O8=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References:From:To: Cc:Date:Subject:Reply-To:Feedback-ID:Message-ID:BIMI-Selector; b=C1VSkMtKslNINulm98AOcEJ1KLPrK2oVRaXeceQ6vrdPPpi3OtGGpbqsUoBXGsaD7 ZV2Rm3iAe7OEASoWw1threTvfNa5agWU8ZpU8lFKHRttLlgi0xaDdSvb7QJuPU2Zr4 Xjkn3p0TwvSG1cmJ04n5WAAOFqfOLBKx9loBeREnMPaGhLUgeCfrP+ocorPUTJV6YT etOBZ9bRaUs8xsPpCaXyQCuML74v7Scwc8XN7gYTslMHENUAb897bKKKJZA8Ktigbb Kr/BCQk2PqIJhTLpqvZzyvO07Hr2GNEjSPBnyuSmjz/WARjsr19dJ7lM/X4bgIKvoj zB8CbsEvKYFDw== X-Pm-Submission-Id: 4gRkxw0vfhz1DDKv From: =?UTF-8?q?Onur=20=C3=96zkan?= To: rcu@vger.kernel.org, rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org Cc: ojeda@kernel.org, boqun@kernel.org, gary@garyguo.net, bjorn3_gh@protonmail.com, lossin@kernel.org, a.hindborg@kernel.org, aliceryhl@google.com, tmgross@umich.edu, dakr@kernel.org, peterz@infradead.org, fujita.tomonori@gmail.com, tamird@kernel.org, jiangshanlai@gmail.com, paulmck@kernel.org, josh@joshtriplett.org, rostedt@goodmis.org, mathieu.desnoyers@efficios.com, =?UTF-8?q?Onur=20=C3=96zkan?= Subject: [PATCH v9 4/4] MAINTAINERS: add Rust SRCU files to SRCU entry Date: Fri, 29 May 2026 16:39:54 +0300 Message-ID: <20260529134004.396743-5-work@onurozkan.dev> X-Mailer: git-send-email 2.51.2 In-Reply-To: <20260529134004.396743-1-work@onurozkan.dev> References: <20260529134004.396743-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 Include Rust side implementation files to the SRCU maintainer entry. Reviewed-by: Paul E. McKenney Signed-off-by: Onur =C3=96zkan --- MAINTAINERS | 3 +++ 1 file changed, 3 insertions(+) diff --git a/MAINTAINERS b/MAINTAINERS index e0b307b2108c..7739a435f258 100644 --- a/MAINTAINERS +++ b/MAINTAINERS @@ -24624,6 +24624,7 @@ SLEEPABLE READ-COPY UPDATE (SRCU) M: Lai Jiangshan M: "Paul E. McKenney" M: Josh Triplett +M: Onur =C3=96zkan (RUST) R: Steven Rostedt R: Mathieu Desnoyers L: rcu@vger.kernel.org @@ -24632,6 +24633,8 @@ W: http://www.rdrop.com/users/paulmck/RCU/ T: git git://git.kernel.org/pub/scm/linux/kernel/git/rcu/linux.git rcu/dev F: include/linux/srcu*.h F: kernel/rcu/srcu*.c +F: rust/helpers/srcu.c +F: rust/kernel/sync/srcu.rs =20 SMACK SECURITY MODULE M: Casey Schaufler --=20 2.51.2