[<prev] [next>] [<thread-prev] [day] [month] [year] [list]
Message-ID: <c2becbdf-1258-4a33-9fa9-3b63b53ad099@kernel.org>
Date: Fri, 24 Oct 2025 16:48:23 +0200
From: Danilo Krummrich <dakr@...nel.org>
To: Markus Probst <markus.probst@...teo.de>
Cc: Greg Kroah-Hartman <gregkh@...uxfoundation.org>,
Miguel Ojeda <ojeda@...nel.org>, Alex Gaynor <alex.gaynor@...il.com>,
"Rafael J. Wysocki" <rafael@...nel.org>, Lee Jones <lee@...nel.org>,
Pavel Machek <pavel@...nel.org>, Dave Ertman <david.m.ertman@...el.com>,
Ira Weiny <ira.weiny@...el.com>, Leon Romanovsky <leon@...nel.org>,
Boqun Feng <boqun.feng@...il.com>, Gary Guo <gary@...yguo.net>,
bjorn3_gh@...tonmail.com, Benno Lossin <lossin@...nel.org>,
Andreas Hindborg <a.hindborg@...nel.org>, Alice Ryhl <aliceryhl@...gle.com>,
Trevor Gross <tmgross@...ch.edu>, Bjorn Helgaas <bhelgaas@...gle.com>,
Krzysztof WilczyĆski <kwilczynski@...nel.org>,
rust-for-linux@...r.kernel.org, linux-leds@...r.kernel.org,
linux-pci@...r.kernel.org, linux-kernel@...r.kernel.org
Subject: Re: [PATCH v6 1/2] rust: Add trait to convert a device reference to a
bus device reference
On 10/23/25 7:28 PM, Markus Probst wrote:
> +// SAFETY: `auxilary::Device` is a transparent wrapper of `struct auxiliary_device`.
> +// The offset is guaranteed to point to a valid device field inside `auxilary::Device`.
s/auxilary/auxiliary/
> +unsafe impl<Ctx: device::DeviceContext> device::AsBusDevice<Ctx> for Device<Ctx> {
> + const OFFSET: usize = offset_of!(bindings::auxiliary_device, dev);
> +}
<snip>
> +/// Bus devices can implement this trait to allow abstractions to provide the bus device in
> +/// class device callbacks.
In the first line, please briefly mention what the trait represents.
Subsequently, you can mention the use-case below as a more general description.
> +///
> +/// This must not be used by drivers and is intended for bus and class device abstractions only.
> +///
> +/// # Safety
> +///
> +/// `AsBusDevice::OFFSET` must be a offset to a device field in the implemented struct.
"... the offset of the embedded base `struct device` field within a bus device
structure"
> +pub(crate) unsafe trait AsBusDevice<Ctx: DeviceContext>: AsRef<Device<Ctx>> {
> + /// The relative offset to the device field.
> + ///
> + /// Use `offset_of!(bindings, field)` macro to avoid breakage.
> + const OFFSET: usize;
> +
> + /// Convert a reference to [`Device`] into `Self`.
> + ///
> + /// # Safety
> + ///
> + /// `dev` must be contained in `Self`.
> + unsafe fn from_device(dev: &Device<Ctx>) -> &Self
> + where
> + Self: Sized,
> + {
> + let raw = dev.as_raw();
> + // SAFETY: `raw - Self::OFFSET` is guaranteed by the safety requirements
> + // to be a valid pointer to `Self`.
> + unsafe { &*raw.byte_sub(Self::OFFSET).cast::<Self>() }
> + }
> +}
Powered by blists - more mailing lists