From nobody Mon Jun 8 17:38:08 2026 Received: from mail-106112.protonmail.ch (mail-106112.protonmail.ch [79.135.106.112]) (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 C8BED43E9C4 for ; Wed, 27 May 2026 17:41:32 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=79.135.106.112 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1779903695; cv=none; b=tdJzSJGpz1EXG6Wf/yeu2K9KPKqEmd7TKc7oy23oGkxGT7X+Yl9GPMtw+D/BMS53dvdgQMUIL6jkqI202WA4XOvk26HvAW1YZ9gznQDM15kiWcbyvEeL87qsJ114emlGZHvsunsZL6O1f0liykIPdfkFehQ6kJpLb2mvpkPj6+c= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1779903695; c=relaxed/simple; bh=I2i3uzJwOMoBCVBbB1VcYmfDP4IyfkcCqKPPG2CKhu8=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=ggS85m8MdWciRLH30eloncnr84doFEKIeCG68jgb8pwLiFIeW2RhXJ/dG/RqPPlWAPHFGtNHhzUc5CD8CuxLFf/LTZ+ILHCalU4ObkFc+lAJYpwbPhO89NBGCBBwYKNNaYQp2C0+QrarwZ6bORlSW66obh52lhho/pWYPkWx6s4= 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=O0eV1D8B; arc=none smtp.client-ip=79.135.106.112 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="O0eV1D8B" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=onurozkan.dev; s=protonmail; t=1779903690; x=1780162890; bh=SyYfA8fMKv0cZigbZYxNO7h2rJuUY7s0jyURAudjKKk=; 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=O0eV1D8BMJ3rVl8Xg3dS8JV8s7gRkVCuMMT0uGZkdPTtTMmRp3ggLtN7GhPbxprZw 2ZdOqQKV9ab3o9TLabByASBiXdij2fEthUWE2bgwI9UqRhabruuAUI432NYazMxz5/ uKzydPj8rb0sBA5V0u/lC9vGWawe6wNcyFB5lGqs3M+aqjeXWGwNNtonhF0Dt4tg/k IkLBY1jRhz11RJwXRwhUgcSDGVXhFDEZ7NsKKkixq7DoVAO7d0xIcCU5bYdJ6TzxLe jX442E4qgtMpnW+WP3g7m5Ws6St5Y+gH6f7+27xJHqsLUgxfe7b5lxewFQLk2Q9GeP dGWBhXtJUom6Q== X-Pm-Submission-Id: 4gQcP128nlz1DF70 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 v5 1/4] rust: helpers: add SRCU helpers Date: Wed, 27 May 2026 20:40:42 +0300 Message-ID: <20260527174120.510447-2-work@onurozkan.dev> X-Mailer: git-send-email 2.51.2 In-Reply-To: <20260527174120.510447-1-work@onurozkan.dev> References: <20260527174120.510447-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 --- rust/helpers/helpers.c | 1 + rust/helpers/srcu.c | 34 ++++++++++++++++++++++++++++++++++ 2 files changed, 35 insertions(+) create mode 100644 rust/helpers/srcu.c 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..79dd24a104ef --- /dev/null +++ b/rust/helpers/srcu.c @@ -0,0 +1,34 @@ +// 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) +{ +#ifdef CONFIG_DEBUG_LOCK_ALLOC + return __init_srcu_struct(ssp, name, key); +#else /* !CONFIG_DEBUG_LOCK_ALLOC */ + return init_srcu_struct(ssp); +#endif /* CONFIG_DEBUG_LOCK_ALLOC */ +} + +__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 17:38:08 2026 Received: from mail-43171.protonmail.ch (mail-43171.protonmail.ch [185.70.43.171]) (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 A36B744DB69; Wed, 27 May 2026 17:41:37 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=185.70.43.171 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1779903700; cv=none; b=fJRbTdiVTIWlPto5A0RZj2jlp4mhYAYPtVZqYREfjq2ARK8QBUQrFFd6lz6dm3OMejXZSCjpvs286m4i4M5wO9MMxYdm6vDd3s6Nbl+aPLMIjKse7zCaLCWazlaIyziRdeSPbDlB4dSB/HQd3h2ZVoO7iwfiA7lBzTZmIcfjBtY= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1779903700; c=relaxed/simple; bh=4jNvNrbzfTwzJjLwSjHboOQERe+1rglIxn+MEh/dkKw=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=BdNUHD32OrDcrZn3h07uWuh7ff05lPUgGdz9ErtdIFZ2UFxYGfraqDOPURygDnKbs90DOrUtR2SgPBPgtyYogp8vGasMkAK5O9lokZlhplDevuBcFMsQJSEoxdpsd9LFUg36Y7QMu5WaREwCAhicBms0giG5VH9HeA7u1wLDwzE= 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=n8TN5khM; arc=none smtp.client-ip=185.70.43.171 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="n8TN5khM" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=onurozkan.dev; s=protonmail; t=1779903690; x=1780162890; bh=KCC4Lc7DPH4rf7fGK9T09hSlzakl84CnCP2v22rvbnM=; 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=n8TN5khMwMKPivH12GzkakaQHd/6QhxDLUctQWkq+HMjW6dw4GE2Kb1NTaHgKI5rh GKg7i5OQHPshk9jVBuvR+MP2Px2SZ634PiJcfLWzxVPCFL3tgaorYGZLNMnpwyhV7E Cb+eKesAxhXnfWedG1Q40ypIdyBRbUFS9W2Yatio4VBD9yApAuNNYRKxtiiVYXxmzy rFImw0wSOtL5pUwTwADo4uXzUo0DDLHKGpAHx2co3+IKL3rFgFdFRBpta90tqBMuhk kCp3ZALOVJwCT/3zON809bt7Y39gknwi1TcYN08+wcayP/TiCyg/6VAF7u+s9wkuD1 JMZ0OrWnuKLFw== X-Pm-Submission-Id: 4gQcP41xnxz1DDs2 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 v5 2/4] srcu: expose srcu_readers_active() Date: Wed, 27 May 2026 20:40:43 +0300 Message-ID: <20260527174120.510447-3-work@onurozkan.dev> X-Mailer: git-send-email 2.51.2 In-Reply-To: <20260527174120.510447-1-work@onurozkan.dev> References: <20260527174120.510447-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 Move srcu_readers_active() from kernel/rcu/srcutree.c into include/linux/srcu.h so it can be reused by Rust SRCU helpers. Provide a CONFIG_TINY_SRCU implementation in the header as well so the helper is available regardless of the selected SRCU backend. 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. Signed-off-by: Onur =C3=96zkan --- include/linux/srcu.h | 29 +++++++++++++++++++++++++++++ kernel/rcu/srcutiny.c | 2 +- kernel/rcu/srcutree.c | 25 ------------------------- rust/helpers/srcu.c | 5 +++++ 4 files changed, 35 insertions(+), 26 deletions(-) diff --git a/include/linux/srcu.h b/include/linux/srcu.h index 81b1938512d5..5ca35efb4536 100644 --- a/include/linux/srcu.h +++ b/include/linux/srcu.h @@ -92,6 +92,35 @@ void call_srcu(struct srcu_struct *ssp, struct rcu_head = *head, void cleanup_srcu_struct(struct srcu_struct *ssp); void synchronize_srcu(struct srcu_struct *ssp); =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) +{ +#ifdef CONFIG_TINY_SRCU + return READ_ONCE(ssp->srcu_lock_nesting[0]) || READ_ONCE(ssp->srcu_lock_n= esting[1]); +#else + 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 +} + #define SRCU_GET_STATE_COMPLETED 0x1 =20 /** diff --git a/kernel/rcu/srcutiny.c b/kernel/rcu/srcutiny.c index a2e2d516e51b..5dc26af604bf 100644 --- a/kernel/rcu/srcutiny.c +++ b/kernel/rcu/srcutiny.c @@ -85,7 +85,7 @@ EXPORT_SYMBOL_GPL(init_srcu_struct); */ 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 0d01cd8c4b4a..b1e97ba2e53f 100644 --- a/kernel/rcu/srcutree.c +++ b/kernel/rcu/srcutree.c @@ -598,31 +598,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 79dd24a104ef..fa4b5879dda5 100644 --- a/rust/helpers/srcu.c +++ b/rust/helpers/srcu.c @@ -13,6 +13,11 @@ __rust_helper int rust_helper_init_srcu_struct_with_key(= struct srcu_struct *ssp, #endif /* CONFIG_DEBUG_LOCK_ALLOC */ } =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 17:38:08 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 5F9E944E04A for ; Wed, 27 May 2026 17:41:41 +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=1779903710; cv=none; b=fB2N5kNYPAiXPPWB2Eio2mMX/RR8iOl/IiiMs6doou2wMGUjQq3f2GdoaELfDrtk55edhj9vs8Qf6qFPl/sVhsVBYzGc1GROwAQli0s9VuWQylJeqMBwWKRvzYhA1vVpGskndVrLDZUFuAl7OhBcCRqDGVazCreMDtzB+FLHFS8= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1779903710; c=relaxed/simple; bh=nwto9TSN5DwcrkY38NX1+Y3ppUDOvXkhEPgbZdMEZeg=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=hob9npS6wVP0cZxhzij2MvhWpeqozN9T2A6oLiOiHxkUNgOYKRkL3+kPSskrIERreWW6j4BGoiEiG3KTcYXkTnMqlmH6w1nbjWjDptfdq/AE7ousY3EiKH+h3u0CtL2cq2Oz0rem5XGkuIt4JlfZ9oWntD+UCM/TNyjE4WaNAxY= 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=UMiK4Qh5; 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="UMiK4Qh5" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=onurozkan.dev; s=protonmail; t=1779903693; x=1780162893; bh=hucoFCxR6ZRmPQZBC0IZgPLRY3PhP3652ZNo3G0VcFc=; 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=UMiK4Qh5DsqNBP7iR9ypjBgaheHW3VNAKr88LS/BgVEmxoWhPoC8RNa90no5k+FOu MkHGz8TPLdRvI4AlET6s3V5f2/RxGbnkJiFYMz/xtEWtaPGXQI00yHusHTYH2XuF5Q hj1yWBykHsSlnASfYtxIhAHB6Bp6vJ0g30YvWk9FJPNjjZ+0UA4OGJ7yp0yi4V9AX6 XoKefl2ys+xsFQniHQuHwcixFhsJE1gXoXBObUy1LSoOT/D0Q8XsqD2NHsvABaeDfd g32G1VUwmS9G652/JrQa43kEgiaNzk/TTA8JRJxBEi2vyYaYaMo757QbxgHN6LiNrj VoZxuUYZnUNnQ== X-Pm-Submission-Id: 4gQcP60Scmz1DF6t 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 v5 3/4] rust: sync: add SRCU abstraction Date: Wed, 27 May 2026 20:40:44 +0300 Message-ID: <20260527174120.510447-4-work@onurozkan.dev> X-Mailer: git-send-email 2.51.2 In-Reply-To: <20260527174120.510447-1-work@onurozkan.dev> References: <20260527174120.510447-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`, which explicitly drains pending grace periods and callbacks via `synchronize_srcu` and `srcu_barrier` before executing `cleanup_srcu_struct` to guarantee memory safety e.g. when there are leaked guards (via `mem::forget($guard)`). Signed-off-by: Onur =C3=96zkan --- rust/kernel/sync.rs | 2 + rust/kernel/sync/srcu.rs | 166 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 168 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..343f00d070c7 --- /dev/null +++ b/rust/kernel/sync/srcu.rs @@ -0,0 +1,166 @@ +// 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(); + + // SAFETY: By the type invariants, `self` contains a valid and pin= ned `struct srcu_struct` + // and `srcu_readers_active()` only checks the active reader count. + if unsafe { bindings::srcu_readers_active(ptr) } { + crate::pr_warn!( + "Leaked `Guard` detected while dropping SRCU; drop will bl= ock forever.\n" + ); + } + + // `cleanup_srcu_struct()` may return early if readers are still a= ctive. Because `Srcu` + // owns the embedded `srcu_struct`, returning from `drop` in that = state could free memory + // that is still referenced by the C side. + // + // Wait for all readers to complete first. If any `Guard` was leak= ed, `synchronize_srcu()` + // will sleep forever. + // + // SAFETY: By the type invariants, `self` contains a valid and pin= ned `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 17:38:08 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 8E83B44DB9D for ; Wed, 27 May 2026 17:41:40 +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=1779903703; cv=none; b=N6n6OlpotKkopPPE3fEPwsPiFaxz/9sqA2OEhucW/x4stvlazKjCl5fFzrWwaTf3z5vWSkz7DZN1t5F0jEnm6rfnJoKqYwnwqhGh/yxAnHUkma1fnCUTl/4OOd1y5NNL47lWMdC0+By47I3nS2qqDjZctkmd3gGJV2LLWv3sw1A= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1779903703; c=relaxed/simple; bh=OXKg7IoJRXxJABbEWtm/U0WbRH/zsPKFQvYu+ZBgzKg=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=QQCLoGOmcziaVLoUX1TnL4L/TXtnBe4JS5kfsgVtji+INLqhy2trSZ7nBorSPdsz097AvMrzUwk4bE9deIj3YEIxaoPUay8DHE9hVuGB3UidqgJkBZ8ocKKMED1psJVy+I2bCQPaqDG87BHLUWgr7bUj28rhIo5pYnXb2AODYMs= 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=JgD9QnH+; 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="JgD9QnH+" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=onurozkan.dev; s=protonmail; t=1779903696; x=1780162896; bh=/JxKK4le+0Qf2c0FvyVzReRLUsasiZAYNwA0SE2Vp0s=; 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=JgD9QnH+7VD1y4WizJTPjezZz86u6jfiZcdT4wGPgjbeEF1BPEkuaNawRAWIbrQ2z uVwuXLZ+fFa0tUBcTiS5BlundMfkh6VowvIlUjZL0BpDrlEgIhmfNX8SjUn62eZcHi zP0YLvwIpnSO/5pQPAT1LcdAlxHNhWINyEQIznUrbHvMF/vzUm6jdqYhptSEVCeqJd DHS6XqRmehYmVhsatSySZU4/MCbYAFTKRWkx9Ueq9OPos9CyZhoSHi9V0SCuPyh3z5 HSuQYmNeGLw+pW6RyFJICqPLTJ7Td6yqKVMVYQerSVEN5VkUooXTdwfnQ9bC6xlyo4 7K1GEMBPqgzZg== X-Pm-Submission-Id: 4gQcP91Q70z1DF70 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 v5 4/4] MAINTAINERS: add Rust SRCU files to SRCU entry Date: Wed, 27 May 2026 20:40:45 +0300 Message-ID: <20260527174120.510447-5-work@onurozkan.dev> X-Mailer: git-send-email 2.51.2 In-Reply-To: <20260527174120.510447-1-work@onurozkan.dev> References: <20260527174120.510447-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. Signed-off-by: Onur =C3=96zkan Reviewed-by: Paul E. McKenney --- 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