[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-ID: <Z8rV6y5GLgovaWdN@google.com>
Date: Fri, 7 Mar 2025 11:18:03 +0000
From: Alice Ryhl <aliceryhl@...gle.com>
To: Abdiel Janulgue <abdiel.janulgue@...il.com>
Cc: rust-for-linux@...r.kernel.org, daniel.almeida@...labora.com,
dakr@...nel.org, robin.murphy@....com, Miguel Ojeda <ojeda@...nel.org>,
Alex Gaynor <alex.gaynor@...il.com>, Boqun Feng <boqun.feng@...il.com>,
Gary Guo <gary@...yguo.net>,
"Björn Roy Baron" <bjorn3_gh@...tonmail.com>, Benno Lossin <benno.lossin@...ton.me>,
Andreas Hindborg <a.hindborg@...nel.org>, Trevor Gross <tmgross@...ch.edu>,
Valentin Obst <kernel@...entinobst.de>, open list <linux-kernel@...r.kernel.org>,
Christoph Hellwig <hch@....de>, Marek Szyprowski <m.szyprowski@...sung.com>, airlied@...hat.com,
"open list:DMA MAPPING HELPERS" <iommu@...ts.linux.dev>
Subject: Re: [PATCH v13 3/7] rust: pci: impl AsMut<Device> for pci::Device
On Fri, Mar 07, 2025 at 01:06:20PM +0200, Abdiel Janulgue wrote:
> From: Danilo Krummrich <dakr@...nel.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@...nel.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
Powered by blists - more mailing lists