[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-Id: <DBB6IYG2VS1O.1TO1VO223ZXZ5@kernel.org>
Date: Sun, 13 Jul 2025 21:37:44 +0200
From: "Danilo Krummrich" <dakr@...nel.org>
To: <gregkh@...uxfoundation.org>, <rafael@...nel.org>, <ojeda@...nel.org>,
<alex.gaynor@...il.com>, <boqun.feng@...il.com>, <gary@...yguo.net>,
<bjorn3_gh@...tonmail.com>, <lossin@...nel.org>, <a.hindborg@...nel.org>,
<aliceryhl@...gle.com>, <tmgross@...ch.edu>,
<daniel.almeida@...labora.com>, <m.wilczynski@...sung.com>
Cc: <rust-for-linux@...r.kernel.org>, <linux-kernel@...r.kernel.org>
Subject: Re: [PATCH 2/2] rust: device: implement Device::as_bound()
On Sun Jul 13, 2025 at 8:26 PM CEST, Danilo Krummrich wrote:
> Provide an unsafe functions for abstractions to convert a regular
> &Device to a &Device<Bound>.
>
> This is useful for registrations that provide certain guarantees for the
> scope of their callbacks, such as IRQs or certain class device
> registrations (e.g. PWM, miscdevice).
>
> Signed-off-by: Danilo Krummrich <dakr@...nel.org>
> ---
> rust/kernel/device.rs | 19 +++++++++++++++++++
> 1 file changed, 19 insertions(+)
>
> diff --git a/rust/kernel/device.rs b/rust/kernel/device.rs
> index d527ceef829e..4d9b052afe92 100644
> --- a/rust/kernel/device.rs
> +++ b/rust/kernel/device.rs
> @@ -60,6 +60,25 @@ pub unsafe fn get_device(ptr: *mut bindings::device) -> ARef<Self> {
> // SAFETY: By the safety requirements ptr is valid
> unsafe { Self::as_ref(ptr) }.into()
> }
> +
> + /// Convert a [`&Device`](Device) into a [`&Device<Bound>`](Device<Bound>).
> + ///
> + /// # Safety
> + ///
> + /// The caller is responsible to ensure that the returned [`&Device<Bound>`](Device<Bound>)
> + /// only lives as long as it can be guaranteed that the [`Device`] is actually bound.
> + pub unsafe fn as_bound(&self) -> &Device<Bound> {
> + let ptr = core::ptr::from_ref(self);
> +
> + // CAST: By the safety requirements the caller is responsible to guarantee that the
> + // returned reference only lives as long as the device is actually bound.
> + let ptr = ptr.cast::<Device<Bound>>();
> +
> + // SAFETY:
> + // - `ptr` comes from `from_ref(self)` above, hence it's guaranteed to be valid.
> + // - Any valid `Device` pointer is also a valid pointer for `Device<Bound>`.
> + unsafe { &*ptr.cast() }
> + }
> }
diff --git a/rust/kernel/device.rs b/rust/kernel/device.rs
index 4d9b052afe92..b6b81546a0da 100644
--- a/rust/kernel/device.rs
+++ b/rust/kernel/device.rs
@@ -72,12 +72,12 @@ pub unsafe fn as_bound(&self) -> &Device<Bound> {
// CAST: By the safety requirements the caller is responsible to guarantee that the
// returned reference only lives as long as the device is actually bound.
- let ptr = ptr.cast::<Device<Bound>>();
+ let ptr = ptr.cast();
// SAFETY:
// - `ptr` comes from `from_ref(self)` above, hence it's guaranteed to be valid.
// - Any valid `Device` pointer is also a valid pointer for `Device<Bound>`.
- unsafe { &*ptr.cast() }
+ unsafe { &*ptr }
}
}
Powered by blists - more mailing lists