From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (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 3B08A3909B3 for ; Thu, 26 Mar 2026 06:55:53 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1774508153; cv=none; b=jn2UJqOlKFZ1b/Vc7ahr8u0t8tjDcbIKZvVzD03OfVITlfdjPRcRao9wMUbyPsZNTx9CYeSjhFn1+EhUjnT7rbecGq6gSTExbdoRQqFLGBZwZwSavkGF8w/mtMc/NJAM57RPw4CLLmeavVBDmBFb/oaw//S7HnvnrE+pmYBlDAI= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1774508153; c=relaxed/simple; bh=8sG5/4DML5NSTRd6XZj5n+aYKAsS4hPMAZ9HC9w0jjQ=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=CI93QZHmWK75CaR6WwTmeDy6thlAT2UUnatoLu4lJ5j/KFpSGQ35OWJGPCfjwjac13ySPmMr2mtXlMTXhJEiLvPzvBv/UxRLSyuNTh7k0x4rLuuanxu4uarqQGYYvQ9HpcePdNGLCb3AHFPc76BtKp5W+4h49X8usTI6jGAnXaA= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linux.dev header.i=@linux.dev header.b=SOHJaEs8; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linux.dev header.i=@linux.dev header.b="SOHJaEs8" Received: by smtp.kernel.org (Postfix) with ESMTPS id BA39DC2BCB9; Thu, 26 Mar 2026 06:55:52 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linux.dev; s=korg; t=1774508152; bh=8sG5/4DML5NSTRd6XZj5n+aYKAsS4hPMAZ9HC9w0jjQ=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=SOHJaEs8rv1wcIQRcx+QCq+MndcBSE3rTA345fV0kk2zRD6ZJWcW9V/kqN6jgfIYo W/hNi4QOZ5PuIFeWdy1dWfP+6hi7h5Ya7NXf3k0++/2q9aC6VSY3XzbRdWWMit0FTa gW5vgN127fpE0ZePLyouqgMbdIDPSZG8mFeKMlTk= Received: from aws-us-west-2-korg-lkml-1.web.codeaurora.org (localhost.localdomain [127.0.0.1]) by smtp.lore.kernel.org (Postfix) with ESMTP id AC8A7109E55A; Thu, 26 Mar 2026 06:55:52 +0000 (UTC) From: Alvin Sun Date: Thu, 26 Mar 2026 14:52:57 +0800 Subject: [PATCH 04/13] rust: sync: add hazard pointer abstraction Precedence: bulk X-Mailing-List: rust-for-linux@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 7bit Message-Id: <20260326-b4-tyr-debugfs-v1-4-074badd18716@linux.dev> References: <20260326-b4-tyr-debugfs-v1-0-074badd18716@linux.dev> In-Reply-To: <20260326-b4-tyr-debugfs-v1-0-074badd18716@linux.dev> To: Miguel Ojeda , Boqun Feng , Gary Guo , =?utf-8?q?Bj=C3=B6rn_Roy_Baron?= , Benno Lossin , Andreas Hindborg , Alice Ryhl , Trevor Gross , Danilo Krummrich , David Airlie , Simona Vetter , Sumit Semwal , =?utf-8?q?Christian_K=C3=B6nig?= , Daniel Almeida Cc: rust-for-linux@vger.kernel.org, dri-devel@lists.freedesktop.org, Alvin Sun X-Mailer: b4 0.14.3 X-Developer-Signature: v=1; a=ed25519-sha256; t=1774508149; l=4702; i=alvin.sun@linux.dev; s=20260317; h=from:subject:message-id; bh=8sG5/4DML5NSTRd6XZj5n+aYKAsS4hPMAZ9HC9w0jjQ=; b=YR1ZJg1z532+Tja18moGSpkoIqzBWqKo/jm8I9c5IQ8WeGEFJV19rZ/ujdzVlcL89QwvcGk7v iA2oaLtRZ66CqEofG7egooB/8hhzclgPbpC4s1jWBgvy626lXhpOXOz X-Developer-Key: i=alvin.sun@linux.dev; a=ed25519; pk=CHcwQp8GSoj25V/L1ZWNSQjWp9eSIb0s9LKr0Nm3WuE= X-Endpoint-Received: by B4 Relay for alvin.sun@linux.dev/20260317 with auth_id=684 Add C helpers and Rust wrappers for the kernel hazard pointer API, to be used by revocable and other code. Signed-off-by: Alvin Sun --- rust/bindings/bindings_helper.h | 1 + rust/helpers/hazptr.c | 13 ++++++ rust/helpers/helpers.c | 1 + rust/kernel/sync.rs | 1 + rust/kernel/sync/hazptr.rs | 91 +++++++++++++++++++++++++++++++++++++++++ 5 files changed, 107 insertions(+) diff --git a/rust/bindings/bindings_helper.h b/rust/bindings/bindings_helper.h index 9058b09a016ec..8ace50a3dc104 100644 --- a/rust/bindings/bindings_helper.h +++ b/rust/bindings/bindings_helper.h @@ -60,6 +60,7 @@ #include #include #include +#include #include #include #include diff --git a/rust/helpers/hazptr.c b/rust/helpers/hazptr.c new file mode 100644 index 0000000000000..aa53fbcd0da1d --- /dev/null +++ b/rust/helpers/hazptr.c @@ -0,0 +1,13 @@ +// SPDX-License-Identifier: GPL-2.0 + +#include + +__rust_helper void *rust_helper_hazptr_acquire(struct hazptr_ctx *ctx, void *const *addr_p) +{ + return hazptr_acquire(ctx, addr_p); +} + +__rust_helper void rust_helper_hazptr_release(struct hazptr_ctx *ctx, void *addr) +{ + hazptr_release(ctx, addr); +} diff --git a/rust/helpers/helpers.c b/rust/helpers/helpers.c index f1ac40b0bd1a4..ed8cb74d394a3 100644 --- a/rust/helpers/helpers.c +++ b/rust/helpers/helpers.c @@ -34,6 +34,7 @@ #include "err.c" #include "irq.c" #include "fs.c" +#include "hazptr.c" #include "io.c" #include "jump_label.c" #include "kunit.c" diff --git a/rust/kernel/sync.rs b/rust/kernel/sync.rs index 993dbf2caa0e3..45da761eaee05 100644 --- a/rust/kernel/sync.rs +++ b/rust/kernel/sync.rs @@ -15,6 +15,7 @@ pub mod barrier; pub mod completion; mod condvar; +pub mod hazptr; pub mod lock; mod locked_by; pub mod poll; diff --git a/rust/kernel/sync/hazptr.rs b/rust/kernel/sync/hazptr.rs new file mode 100644 index 0000000000000..f94ae45dd3d66 --- /dev/null +++ b/rust/kernel/sync/hazptr.rs @@ -0,0 +1,91 @@ +// SPDX-License-Identifier: GPL-2.0 + +//! Hazard pointer support. +//! +//! C header: [`include/linux/hazptr.h`](srctree/include/linux/hazptr.h) + +use crate::{ + bindings, + ffi::c_void, + prelude::*, + types::Opaque, // +}; +use core::{ + ops::Deref, + ptr::NonNull, // +}; + +/// Per-thread context for one hazard-pointer acquire/release pair. +#[repr(transparent)] +pub struct HazptrCtx(Opaque); + +impl HazptrCtx { + /// Creates a new zero-initialized context for one acquire/release pair. + #[inline] + pub const fn new() -> Self { + Self(Opaque::zeroed()) + } + + /// Get the raw pointer. + #[inline] + pub fn as_raw(self: &Pin<&mut Self>) -> *mut bindings::hazptr_ctx { + self.0.get() + } +} + +impl Default for HazptrCtx { + fn default() -> Self { + Self::new() + } +} + +/// Guard holding a hazard-pointer-protected reference. +pub struct Guard<'a, T> { + ctx: Pin<&'a mut HazptrCtx>, + ptr: NonNull, +} + +impl<'a, T> Guard<'a, T> { + #[inline] + fn new(ctx: Pin<&'a mut HazptrCtx>, ptr: NonNull) -> Self { + Self { ctx, ptr } + } +} + +impl Deref for Guard<'_, T> { + type Target = T; + + #[inline] + fn deref(&self) -> &T { + // SAFETY: hazptr protocol keeps the pointer valid until release. + unsafe { self.ptr.as_ref() } + } +} + +impl Drop for Guard<'_, T> { + #[inline] + fn drop(&mut self) { + // SAFETY: `self.ctx` and `self.ptr` are the same as the ones used in `acquire`. + unsafe { bindings::hazptr_release(self.ctx.as_raw().cast(), self.ptr.as_ptr().cast()) }; + } +} + +/// Acquires a hazard pointer for the pointer at `addr_p` and returns a guard. +/// +/// Returns `None` if the loaded value is null. +#[inline] +pub fn acquire<'a, T>( + ctx: Pin<&'a mut HazptrCtx>, + addr_p: *const *const T, +) -> Option> { + // SAFETY: ctx is valid and pinned, addr_p is a valid pointer to a pointer. + let ptr = unsafe { bindings::hazptr_acquire(ctx.as_raw().cast(), addr_p.cast()) }; + NonNull::new(ptr.cast()).map(|p| Guard::new(ctx, p)) +} + +/// Waits until no slot holds `addr`. +#[inline] +pub fn synchronize(addr: usize) { + // SAFETY: addr is only compared with slot values, not dereferenced. + unsafe { bindings::hazptr_synchronize(addr as *mut c_void) }; +} -- 2.43.0