From: "Danilo Krummrich" <dakr@kernel.org>
To: <gregkh@linuxfoundation.org>, <rafael@kernel.org>,
<ojeda@kernel.org>, <alex.gaynor@gmail.com>,
<boqun.feng@gmail.com>, <gary@garyguo.net>,
<bjorn3_gh@protonmail.com>, <lossin@kernel.org>,
<a.hindborg@kernel.org>, <aliceryhl@google.com>,
<tmgross@umich.edu>, <daniel.almeida@collabora.com>,
<m.wilczynski@samsung.com>
Cc: <rust-for-linux@vger.kernel.org>, <linux-kernel@vger.kernel.org>
Subject: Re: [PATCH 2/2] rust: device: implement Device::as_bound()
Date: Sun, 13 Jul 2025 21:37:44 +0200 [thread overview]
Message-ID: <DBB6IYG2VS1O.1TO1VO223ZXZ5@kernel.org> (raw)
In-Reply-To: <20250713182737.64448-2-dakr@kernel.org>
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@kernel.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 }
}
}
next prev parent reply other threads:[~2025-07-13 19:37 UTC|newest]
Thread overview: 12+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-07-13 18:26 [PATCH 1/2] rust: devres: provide an accessor for the device Danilo Krummrich
2025-07-13 18:26 ` [PATCH 2/2] rust: device: implement Device::as_bound() Danilo Krummrich
2025-07-13 19:37 ` Danilo Krummrich [this message]
2025-07-13 19:39 ` Benno Lossin
2025-07-14 5:30 ` Greg KH
2025-07-14 9:49 ` Danilo Krummrich
2025-07-14 13:24 ` Greg KH
2025-07-14 13:26 ` Danilo Krummrich
2025-07-15 20:30 ` Danilo Krummrich
2025-07-13 19:39 ` [PATCH 1/2] rust: devres: provide an accessor for the device Benno Lossin
2025-07-14 5:30 ` Greg KH
2025-07-15 20:29 ` Danilo Krummrich
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=DBB6IYG2VS1O.1TO1VO223ZXZ5@kernel.org \
--to=dakr@kernel.org \
--cc=a.hindborg@kernel.org \
--cc=alex.gaynor@gmail.com \
--cc=aliceryhl@google.com \
--cc=bjorn3_gh@protonmail.com \
--cc=boqun.feng@gmail.com \
--cc=daniel.almeida@collabora.com \
--cc=gary@garyguo.net \
--cc=gregkh@linuxfoundation.org \
--cc=linux-kernel@vger.kernel.org \
--cc=lossin@kernel.org \
--cc=m.wilczynski@samsung.com \
--cc=ojeda@kernel.org \
--cc=rafael@kernel.org \
--cc=rust-for-linux@vger.kernel.org \
--cc=tmgross@umich.edu \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.