From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=100.103.45.18 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796211605; b=GRRtWtjYFDRi8pneTln+YFLSKAjA2EW/WrG1VH1PwU4Bqvqy91v5uSSNfAxs18aEoTni +5m9iVYwR2Z4nXF2rpNOKXNZTkD2BtJlhwyMv5k+ln9rkkFyl8JZIM6x3r6zlpkf093It u8B9TsDPrRz8PWMNUx7tUnq/art5cw9wy3s49O89a7Sa9NazSZX/dUd0QAfbgXO60HeKK rHQnAfZTPWyOnQ6Ni8/PP8lVWQSZquBi4l+6IkiwTGwd/GkKH1KbSfkLV2I4oNsAMe9bR Q4QgI1Ur7dWltd9VRL2JhPFrLQt2hKNgGFI/iKqHTilLwgtoBf2dZqr2LVlZCnY+ZPA== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796211605; h=Received:DKIM-Signature:From:To:Cc:Subject:Date:Message-ID:X-Mailer: In-Reply-To:References:MIME-Version:Content-Transfer-Encoding; bh=VO+6gAJrOp59+MXrrq8Or1SjjZ3E7FM1oDCbJqbwEn4=; b=W/se3Ja4gEQ9Qi5l9MVgYzY8Mi/kopi4ZaZPaIl/Wb8RtTt0QG8+Thwj/NDeUjlJcURo ZctmWhJdmrvn46BDwAqsa5hixjrlJVV+r0gUvEI+1I/MCH9YboMD2CgPK4pSWepS09tn7 1Coh1ZHDoJ9DPuedoqfdZluBhGN9Ab1RCjCi/74ZRBuEeBp3Q0XOg9Z3bF80bb3LtAn2G ZOa2XeVAm+ZW1tmkKc5U9ExphVtquTYXMb2KRuUbkaWv+N2SCmLACBc0nvM+KrPB5M6NP jDH2xk0eLXZi3QgsSf44fq6hhsJGNP46zgsrYsynSq07+idAVf4LdCqjAGWyAbHIt8A== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=TscUHgKW; arc=none smtp.remote-ip=100.103.45.18 Received: from smtp.kernel.org (quasi.space.kernel.org [100.103.45.18]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id D86CA606A323; Wed, 2 Dec 2026 11:40:05 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=TscUHgKW Received: by smtp.kernel.org (Postfix) with ESMTPSA id C1D501F000C1; Wed, 2 Dec 2026 11:39:57 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1796211605; bh=VO+6gAJrOp59+MXrrq8Or1SjjZ3E7FM1oDCbJqbwEn4=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=TscUHgKWcblAV7uG5RDGh4uklKJKNZC85pcLs5TzeT75J76DkjA6MIX+zcL88OT34 R6SLQhYhAA+qetRkkCNxeDtYShd4Dhe2npw6OOFirkzXUflzj/gZLGgpSmLURH/iW3 rhYh870i6nyQBnY0W5DYl34rGFynn4sx3K7l/FzhkFcj1AtukuLTzSzrCYmFZUC1ux QiTMJjQr6rbjzt5peQ3Kc4THB4kPcNknBvdLDKjCbLrZODSZlr7kNSzkmnGQ+mL8uY zJFbOxxQKAzG6pMK7+/HpZO48Q9VCEZBbVNQAGjnmf/+XCuqvYACE4Q8fHSmfjbt5o AwIi0WZi236Kw== From: Philipp Stanner To: Miguel Ojeda , Boqun Feng , Gary Guo , =?UTF-8?q?Bj=C3=B6rn=20Roy=20Baron?= , Benno Lossin , Andreas Hindborg , Alice Ryhl , Trevor Gross , Danilo Krummrich , Daniel Almeida , Tamir Duberstein , Alexandre Courbot , =?UTF-8?q?Onur=20=C3=96zkan?= , Sumit Semwal , =?UTF-8?q?Christian=20K=C3=B6nig?= , Philipp Stanner , Matthew Brost , David Airlie , Simona Vetter , "Paul E. McKenney" , Frederic Weisbecker , Neeraj Upadhyay , Joel Fernandes , Josh Triplett , Uladzislau Rezki , Steven Rostedt , Mathieu Desnoyers , Lai Jiangshan , Zqiang , Lyude Paul , "Joel Fernandes (NVIDIA)" , Boris Brezillon , Yuvan Shen Cc: rust-for-linux@vger.kernel.org, linux-media@vger.kernel.org, dri-devel@lists.freedesktop.org, rcu@vger.kernel.org Subject: [PATCH v3 1/9] rust: rcu: Add RcuState Date: Wed, 2 Dec 2026 12:39:07 +0100 Message-ID: <20261202113914.1660411-3-phasta@kernel.org> X-Mailer: git-send-email 2.56.0 In-Reply-To: <20261202113914.1660411-2-phasta@kernel.org> References: <20261202113914.1660411-2-phasta@kernel.org> X-Mailing-List: rcu@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Currently, Rust's RCU abstractions only provide a full synchronize_rcu(). Many users, however, only want to wait for one grace period if a specific grace period has not already passed. Add RcuState, an object identifying a grace period, and implement a method to conditionally synchronize for that grace period. Signed-off-by: Philipp Stanner Suggested-by: Gary Guo --- rust/helpers/rcu.c | 5 +++++ rust/kernel/sync/rcu.rs | 35 +++++++++++++++++++++++++++++++++++ 2 files changed, 40 insertions(+) diff --git a/rust/helpers/rcu.c b/rust/helpers/rcu.c index 481274c05857..570ee76b7247 100644 --- a/rust/helpers/rcu.c +++ b/rust/helpers/rcu.c @@ -11,3 +11,8 @@ __rust_helper void rust_helper_rcu_read_unlock(void) { rcu_read_unlock(); } + +__rust_helper void rust_helper_cond_synchronize_rcu(unsigned long cookie) +{ + cond_synchronize_rcu(cookie); +} diff --git a/rust/kernel/sync/rcu.rs b/rust/kernel/sync/rcu.rs index 0daa1ac87d81..7cb5a7d31e57 100644 --- a/rust/kernel/sync/rcu.rs +++ b/rust/kernel/sync/rcu.rs @@ -86,3 +86,38 @@ pub fn synchronize_rcu() { // SAFETY: `synchronize_rcu()` is always safe to be called from process context. unsafe { bindings::synchronize_rcu() }; } + +/// An RCU grace period snapshot. +/// +/// Useful for waiting for said grace period if necessary. If you just drop this +/// object, nothing happens. +#[derive(Copy, Clone)] +pub struct RcuState(usize); + +impl RcuState { + /// Create a new RcuState. It stores an identifier of the current grace + /// period so that you can later water on it. + pub fn new() -> Self { + // SAFETY: `get_state_synchronize_rcu()` is always safe to be called. + let cookie: usize = unsafe { bindings::get_state_synchronize_rcu() }; + + Self(cookie) + } + + /// Make sure that a full RCU grace period has passed since this RcuState + /// was created. + /// + /// Executes a [`synchronize_rcu()`] if that's not the case. Thus, you must + /// not call this in atomic context. + pub fn cond_synchronize(self) { + // SAFETY: This is the right cookie. The function runs in process context. + unsafe { bindings::cond_synchronize_rcu(self.0) }; + } +} + +impl Default for RcuState { + #[inline] + fn default() -> Self { + Self::new() + } +} -- 2.56.0