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 162CF2459EA; Sun, 19 Oct 2025 14:28:47 +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=1760884128; cv=none; b=uuje19fGo6gsunfKZX4qwT2ZefmT1Gm1QG4pq/JBY5Vb0j1uV2UrFAscFe+76adm4d3gXEbY5aL4Z8fzjTH90sIzKY2oLTORdNAohwciWEGbATr+OciaD6NGllazg0PSNypZ9/N1loTh4+1TdUYq3r3IRSNjD1vPneXmf2YPZig= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1760884128; c=relaxed/simple; bh=2Lk3VteT5ZUYtGV3L7w6wKgSGuoqrZ/lGhL+exIXDm8=; h=Mime-Version:Content-Type:Date:Message-Id:To:From:Subject:Cc: References:In-Reply-To; b=Cc8IFpNXVqlSEP9dMgyjZwMJehP0YGanSKe0P+61FC3S4dpZqPLzi+CsytLqjvp0UZ9dwbzEjxc+CtZ4rBPECe2y0bmT4m/RjJrVLiUP95ctmTM+jnJKAMmVY9p2jpEeC5o1w2Ks2cvTDzafhx/H34NuoGb3GdqQM2N3tlIFxI4= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=MP03uqEj; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="MP03uqEj" Received: by smtp.kernel.org (Postfix) with ESMTPSA id D4830C4CEE7; Sun, 19 Oct 2025 14:28:42 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1760884127; bh=2Lk3VteT5ZUYtGV3L7w6wKgSGuoqrZ/lGhL+exIXDm8=; h=Date:To:From:Subject:Cc:References:In-Reply-To:From; b=MP03uqEjX3dT3tNfORhl0l847PRKqtZ5CKZZ6jtJVg/gObj2g3axOfyuXJAOL/CCd 6et/er0QZsqeHeYSpFjj4gs7GCC/aN27fEpcA19izrrOOWBG6ctcuB4LNj6bo/64A+ sLqp8CaLTIL7U1AU7IiuLT2oy/5MyqMyseRFgC1e54qOl27t6crh33k2F03oekgIWB SqylejnRdM1buwwgRTLQxtvicZHCu+9a2mLXT0EUTFLlyOuOYnn2gMZJsSK0+q8HLQ +bHhisezI8jZ+1eNnQnFqoNFtxSfVA2NgUUI9onNLltSFLTzrn+lhexkX0rX3mrp8x bDxQ2guleBZ2g== Precedence: bulk X-Mailing-List: linux-leds@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset=UTF-8 Date: Sun, 19 Oct 2025 16:28:40 +0200 Message-Id: To: "Markus Probst" From: "Danilo Krummrich" Subject: Re: [PATCH v5 1/2] rust: Add trait to convert a device reference to a bus device reference Cc: "Greg Kroah-Hartman" , "Miguel Ojeda" , "Alex Gaynor" , "Rafael J. Wysocki" , "Igor Korotin" , "Lee Jones" , "Pavel Machek" , "Dave Ertman" , "Ira Weiny" , "Leon Romanovsky" , "Boqun Feng" , "Gary Guo" , , "Benno Lossin" , "Andreas Hindborg" , "Alice Ryhl" , "Trevor Gross" , "Daniel Almeida" , "Bjorn Helgaas" , , , , , References: <20251018205912.1528811-1-markus.probst@posteo.de> <20251018205912.1528811-2-markus.probst@posteo.de> In-Reply-To: <20251018205912.1528811-2-markus.probst@posteo.de> On Sat Oct 18, 2025 at 10:59 PM CEST, Markus Probst wrote: > Implement the `IntoBusDevice` trait for converting a `Device` reference t= o a > bus device reference for all bus devices. `Device` implements this trait = as a > fallback. > > The `IntoBusDevice` trait allows abstractions to provide the bus device i= n > class device callbacks. > > Signed-off-by: Markus Probst > --- > rust/kernel/auxiliary.rs | 7 +++++++ > rust/kernel/device.rs | 41 ++++++++++++++++++++++++++++++++++++++++ > rust/kernel/i2c.rs | 7 +++++++ i2c is not upstream yet, hence it should not be part of this patch. Instead= you should include the platform bus though. > rust/kernel/pci.rs | 7 +++++++ > rust/kernel/usb.rs | 6 ++++++ > 5 files changed, 68 insertions(+) > > diff --git a/rust/kernel/auxiliary.rs b/rust/kernel/auxiliary.rs > index e11848bbf206..dea24265f549 100644 > --- a/rust/kernel/auxiliary.rs > +++ b/rust/kernel/auxiliary.rs > @@ -15,6 +15,7 @@ > }; > use core::{ > marker::PhantomData, > + mem::offset_of, > ptr::{addr_of_mut, NonNull}, > }; > =20 > @@ -239,6 +240,12 @@ extern "C" fn release(dev: *mut bindings::device) { > } > } > =20 > +// SAFETY: `auxilary::Device` is a transparent wrapper of `struct auxili= ary_device`. > +// The offset is guaranteed to point to a valid device field inside `aux= ilary::Device`. > +unsafe impl device::IntoBusDevice for D= evice { > + const OFFSET: usize =3D offset_of!(bindings::auxiliary_device, dev); > +} > + > // SAFETY: `Device` is a transparent wrapper of a type that doesn't depe= nd on `Device`'s generic > // argument. > kernel::impl_device_context_deref!(unsafe { Device }); > diff --git a/rust/kernel/device.rs b/rust/kernel/device.rs > index 1321e6f0b53c..5527854a195f 100644 > --- a/rust/kernel/device.rs > +++ b/rust/kernel/device.rs > @@ -511,6 +511,47 @@ impl DeviceContext for Core {} > impl DeviceContext for CoreInternal {} > impl DeviceContext for Normal {} > =20 > +/// Bus devices can implement this trait to allow abstractions to provid= e the bus device in > +/// class device callbacks. > +/// > +/// # Safety > +/// > +/// `IntoBusDevice::OFFSET` must be a offset to a device field in the im= plemented struct. I think we should also require that this must only be implemented by bus de= vice types. > +pub(crate) unsafe trait IntoBusDevice: > + AsRef> We should probably name this AsBusDevice. > +{ > + /// 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) -> &Self As mentioned in the other thread, my concern remains that this could be abu= sed by drivers. For now the trait is pub(crate), but with the new build system coming soon, we're able to split things out of the kernel crate, and hence bus abstracti= ons and driver-core code may end up in different crates requiring this to becom= e public. We should at least document that this must not be used by drivers and is intended for bus and class device abstractions only. > + where > + Self: Sized, > + { > + let raw =3D dev.as_raw(); > + // SAFETY: `raw - Self::OFFSET` is guaranteed by the safety requ= irements > + // to be a valid pointer to `Self`. > + unsafe { &*raw.byte_sub(Self::OFFSET).cast::() } > + } > +} > + > +// SAFETY: `Device` is a transparent wrapper of `device`. > +unsafe impl IntoBusDevice for Device { > + const OFFSET: usize =3D 0; > +} A generic device is not guaranteed to be a bus device. Also, I don't see a reason why class device abstractions would want to work with a generic devi= ce rather than the actual bus device parent. Hence, let's drop this impl.