[PATCH 2/4] rust: device: implement bus_type_raw()

Danilo Krummrich posted 4 patches 9 months ago
There is a newer version of this series
[PATCH 2/4] rust: device: implement bus_type_raw()
Posted by Danilo Krummrich 9 months ago
Implement bus_type_raw(), which returns a raw pointer to the device'
struct bus_type.

This is useful for bus devices, to implement the following trait.

	impl TryFrom<&Device> for &pci::Device

With this a caller can try to get the bus specific device from a generic
device in a safe way. try_from() will only succeed if the generic
device' bus type pointer matches the pointer of the bus' type.

Signed-off-by: Danilo Krummrich <dakr@kernel.org>
---
 rust/kernel/device.rs | 7 +++++++
 1 file changed, 7 insertions(+)

diff --git a/rust/kernel/device.rs b/rust/kernel/device.rs
index 76b341441f3f..e2de0efd4a27 100644
--- a/rust/kernel/device.rs
+++ b/rust/kernel/device.rs
@@ -78,6 +78,13 @@ pub fn parent<'a>(&self) -> Option<&'a Self> {
         }
     }
 
+    /// Returns a raw pointer to the device' bus type.
+    #[expect(unused)]
+    pub(crate) fn bus_type_raw(&self) -> *const bindings::bus_type {
+        // SAFETY: By the type invariants, `self.as_raw()` is a valid pointer to a `struct device`.
+        unsafe { (*self.as_raw()).bus }
+    }
+
     /// Convert a raw C `struct device` pointer to a `&'a Device`.
     ///
     /// # Safety
-- 
2.48.1
Re: [PATCH 2/4] rust: device: implement bus_type_raw()
Posted by Alice Ryhl 9 months ago
On Wed, Mar 19, 2025 at 09:30:26PM +0100, Danilo Krummrich wrote:
> Implement bus_type_raw(), which returns a raw pointer to the device'
> struct bus_type.
> 
> This is useful for bus devices, to implement the following trait.
> 
> 	impl TryFrom<&Device> for &pci::Device
> 
> With this a caller can try to get the bus specific device from a generic
> device in a safe way. try_from() will only succeed if the generic
> device' bus type pointer matches the pointer of the bus' type.
> 
> Signed-off-by: Danilo Krummrich <dakr@kernel.org>
> ---
>  rust/kernel/device.rs | 7 +++++++
>  1 file changed, 7 insertions(+)
> 
> diff --git a/rust/kernel/device.rs b/rust/kernel/device.rs
> index 76b341441f3f..e2de0efd4a27 100644
> --- a/rust/kernel/device.rs
> +++ b/rust/kernel/device.rs
> @@ -78,6 +78,13 @@ pub fn parent<'a>(&self) -> Option<&'a Self> {
>          }
>      }
>  
> +    /// Returns a raw pointer to the device' bus type.
> +    #[expect(unused)]
> +    pub(crate) fn bus_type_raw(&self) -> *const bindings::bus_type {
> +        // SAFETY: By the type invariants, `self.as_raw()` is a valid pointer to a `struct device`.

Is this field immutable?

> +        unsafe { (*self.as_raw()).bus }
> +    }
> +
>      /// Convert a raw C `struct device` pointer to a `&'a Device`.
>      ///
>      /// # Safety
> -- 
> 2.48.1
>
Re: [PATCH 2/4] rust: device: implement bus_type_raw()
Posted by Danilo Krummrich 9 months ago
On Thu, Mar 20, 2025 at 08:36:02AM +0000, Alice Ryhl wrote:
> On Wed, Mar 19, 2025 at 09:30:26PM +0100, Danilo Krummrich wrote:
> > Implement bus_type_raw(), which returns a raw pointer to the device'
> > struct bus_type.
> > 
> > This is useful for bus devices, to implement the following trait.
> > 
> > 	impl TryFrom<&Device> for &pci::Device
> > 
> > With this a caller can try to get the bus specific device from a generic
> > device in a safe way. try_from() will only succeed if the generic
> > device' bus type pointer matches the pointer of the bus' type.
> > 
> > Signed-off-by: Danilo Krummrich <dakr@kernel.org>
> > ---
> >  rust/kernel/device.rs | 7 +++++++
> >  1 file changed, 7 insertions(+)
> > 
> > diff --git a/rust/kernel/device.rs b/rust/kernel/device.rs
> > index 76b341441f3f..e2de0efd4a27 100644
> > --- a/rust/kernel/device.rs
> > +++ b/rust/kernel/device.rs
> > @@ -78,6 +78,13 @@ pub fn parent<'a>(&self) -> Option<&'a Self> {
> >          }
> >      }
> >  
> > +    /// Returns a raw pointer to the device' bus type.
> > +    #[expect(unused)]
> > +    pub(crate) fn bus_type_raw(&self) -> *const bindings::bus_type {
> > +        // SAFETY: By the type invariants, `self.as_raw()` is a valid pointer to a `struct device`.
> 
> Is this field immutable?

dev->bus is a pointer to a const struct bus_type, yes.

> 
> > +        unsafe { (*self.as_raw()).bus }
> > +    }
> > +
> >      /// Convert a raw C `struct device` pointer to a `&'a Device`.
> >      ///
> >      /// # Safety
> > -- 
> > 2.48.1
> >
Re: [PATCH 2/4] rust: device: implement bus_type_raw()
Posted by Alice Ryhl 9 months ago
On Thu, Mar 20, 2025 at 01:00:23PM +0100, Danilo Krummrich wrote:
> On Thu, Mar 20, 2025 at 08:36:02AM +0000, Alice Ryhl wrote:
> > On Wed, Mar 19, 2025 at 09:30:26PM +0100, Danilo Krummrich wrote:
> > > Implement bus_type_raw(), which returns a raw pointer to the device'
> > > struct bus_type.
> > > 
> > > This is useful for bus devices, to implement the following trait.
> > > 
> > > 	impl TryFrom<&Device> for &pci::Device
> > > 
> > > With this a caller can try to get the bus specific device from a generic
> > > device in a safe way. try_from() will only succeed if the generic
> > > device' bus type pointer matches the pointer of the bus' type.
> > > 
> > > Signed-off-by: Danilo Krummrich <dakr@kernel.org>
> > > ---
> > >  rust/kernel/device.rs | 7 +++++++
> > >  1 file changed, 7 insertions(+)
> > > 
> > > diff --git a/rust/kernel/device.rs b/rust/kernel/device.rs
> > > index 76b341441f3f..e2de0efd4a27 100644
> > > --- a/rust/kernel/device.rs
> > > +++ b/rust/kernel/device.rs
> > > @@ -78,6 +78,13 @@ pub fn parent<'a>(&self) -> Option<&'a Self> {
> > >          }
> > >      }
> > >  
> > > +    /// Returns a raw pointer to the device' bus type.
> > > +    #[expect(unused)]
> > > +    pub(crate) fn bus_type_raw(&self) -> *const bindings::bus_type {
> > > +        // SAFETY: By the type invariants, `self.as_raw()` is a valid pointer to a `struct device`.
> > 
> > Is this field immutable?
> 
> dev->bus is a pointer to a const struct bus_type, yes.

With that added to the SAFETY comment to justify reading the field is
data-race free, you may add:

Reviewed-by: Alice Ryhl <aliceryhl@google.com>