[PATCH v13 3/7] rust: pci: impl AsMut<Device> for pci::Device

Abdiel Janulgue posted 7 patches 11 months, 1 week ago
There is a newer version of this series
[PATCH v13 3/7] rust: pci: impl AsMut<Device> for pci::Device
Posted by Abdiel Janulgue 11 months, 1 week ago
From: Danilo Krummrich <dakr@kernel.org>

Some device methods require mutable references, since they change the
underlying struct device without lock protection.

Hence, make it possible to retrieve a mutable reference to a Device from
a mutable pci::Device.

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

diff --git a/rust/kernel/pci.rs b/rust/kernel/pci.rs
index 4c98b5b9aa1e..141430dac2d5 100644
--- a/rust/kernel/pci.rs
+++ b/rust/kernel/pci.rs
@@ -432,3 +432,14 @@ fn as_ref(&self) -> &device::Device {
         &self.0
     }
 }
+
+impl AsMut<device::Device> for Device {
+    fn as_mut(&mut self) -> &mut device::Device {
+        // SAFETY:
+        // - `self.0.as_raw()` is valid by the type invariant of `device::Device`,
+        // - `struct device` is embedded in `struct pci_dev`, hence it is safe to give out a
+        //    mutable reference for `device::Device` if we have a mutable reference to the
+        //    corresponding `pci::Device`.
+        unsafe { &mut *self.0.as_raw().cast() }
+    }
+}
-- 
2.43.0
Re: [PATCH v13 3/7] rust: pci: impl AsMut<Device> for pci::Device
Posted by Greg KH 11 months, 1 week ago
On Fri, Mar 07, 2025 at 01:06:20PM +0200, Abdiel Janulgue wrote:
> From: Danilo Krummrich <dakr@kernel.org>
> 
> Some device methods require mutable references, since they change the
> underlying struct device without lock protection.
> 
> Hence, make it possible to retrieve a mutable reference to a Device from
> a mutable pci::Device.
> 
> Signed-off-by: Danilo Krummrich <dakr@kernel.org>
> ---

Note, you can't forward on a patch from someone else without also
signing off on it.  Please read the DCO for what this means and why.

thanks,

greg k-h
Re: [PATCH v13 3/7] rust: pci: impl AsMut<Device> for pci::Device
Posted by Abdiel Janulgue 11 months, 1 week ago

On 07/03/2025 16:18, Greg KH wrote:
> On Fri, Mar 07, 2025 at 01:06:20PM +0200, Abdiel Janulgue wrote:
>> From: Danilo Krummrich <dakr@kernel.org>
>>
>> Some device methods require mutable references, since they change the
>> underlying struct device without lock protection.
>>
>> Hence, make it possible to retrieve a mutable reference to a Device from
>> a mutable pci::Device.
>>
>> Signed-off-by: Danilo Krummrich <dakr@kernel.org>
>> ---
> 
> Note, you can't forward on a patch from someone else without also
> signing off on it.  Please read the DCO for what this means and why.
> 
Thanks for the reminder! Will do so in next revision.

Regards,
Abdiel
Re: [PATCH v13 3/7] rust: pci: impl AsMut<Device> for pci::Device
Posted by Alice Ryhl 11 months, 1 week ago
On Fri, Mar 07, 2025 at 01:06:20PM +0200, Abdiel Janulgue wrote:
> From: Danilo Krummrich <dakr@kernel.org>
> 
> Some device methods require mutable references, since they change the
> underlying struct device without lock protection.
> 
> Hence, make it possible to retrieve a mutable reference to a Device from
> a mutable pci::Device.
> 
> Signed-off-by: Danilo Krummrich <dakr@kernel.org>
> ---
>  rust/kernel/pci.rs | 11 +++++++++++
>  1 file changed, 11 insertions(+)
> 
> diff --git a/rust/kernel/pci.rs b/rust/kernel/pci.rs
> index 4c98b5b9aa1e..141430dac2d5 100644
> --- a/rust/kernel/pci.rs
> +++ b/rust/kernel/pci.rs
> @@ -432,3 +432,14 @@ fn as_ref(&self) -> &device::Device {
>          &self.0
>      }
>  }
> +
> +impl AsMut<device::Device> for Device {
> +    fn as_mut(&mut self) -> &mut device::Device {
> +        // SAFETY:
> +        // - `self.0.as_raw()` is valid by the type invariant of `device::Device`,
> +        // - `struct device` is embedded in `struct pci_dev`, hence it is safe to give out a
> +        //    mutable reference for `device::Device` if we have a mutable reference to the
> +        //    corresponding `pci::Device`.
> +        unsafe { &mut *self.0.as_raw().cast() }
> +    }
> +}

This makes it possible to call `mem::swap` on two devices, which I doubt
is allowed.

Alice
Re: [PATCH v13 3/7] rust: pci: impl AsMut<Device> for pci::Device
Posted by Danilo Krummrich 11 months, 1 week ago
On Fri, Mar 07, 2025 at 11:18:03AM +0000, Alice Ryhl wrote:
> On Fri, Mar 07, 2025 at 01:06:20PM +0200, Abdiel Janulgue wrote:
> > From: Danilo Krummrich <dakr@kernel.org>
> > 
> > Some device methods require mutable references, since they change the
> > underlying struct device without lock protection.
> > 
> > Hence, make it possible to retrieve a mutable reference to a Device from
> > a mutable pci::Device.
> > 
> > Signed-off-by: Danilo Krummrich <dakr@kernel.org>
> > ---
> >  rust/kernel/pci.rs | 11 +++++++++++
> >  1 file changed, 11 insertions(+)
> > 
> > diff --git a/rust/kernel/pci.rs b/rust/kernel/pci.rs
> > index 4c98b5b9aa1e..141430dac2d5 100644
> > --- a/rust/kernel/pci.rs
> > +++ b/rust/kernel/pci.rs
> > @@ -432,3 +432,14 @@ fn as_ref(&self) -> &device::Device {
> >          &self.0
> >      }
> >  }
> > +
> > +impl AsMut<device::Device> for Device {
> > +    fn as_mut(&mut self) -> &mut device::Device {
> > +        // SAFETY:
> > +        // - `self.0.as_raw()` is valid by the type invariant of `device::Device`,
> > +        // - `struct device` is embedded in `struct pci_dev`, hence it is safe to give out a
> > +        //    mutable reference for `device::Device` if we have a mutable reference to the
> > +        //    corresponding `pci::Device`.
> > +        unsafe { &mut *self.0.as_raw().cast() }
> > +    }
> > +}
> 
> This makes it possible to call `mem::swap` on two devices, which I doubt
> is allowed.

Good catch, that's of course not allowed.

Obviously, this is for Device::dma_set_mask(), introduced in a subsequent patch.

Thinking about it, maybe it should be implemented by bus devices instead (e.g.
pci::Device) -- dma_set_mask() doesn't make sense for every device.