[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-ID: <CAH5fLggvFanUvysDkZiLqFz4Ay7XSP5LF3CvxBU3xgWE3PSZXQ@mail.gmail.com>
Date: Thu, 20 Mar 2025 09:40:45 +0100
From: Alice Ryhl <aliceryhl@...gle.com>
To: Danilo Krummrich <dakr@...nel.org>
Cc: bhelgaas@...gle.com, gregkh@...uxfoundation.org, rafael@...nel.org,
ojeda@...nel.org, alex.gaynor@...il.com, boqun.feng@...il.com,
gary@...yguo.net, bjorn3_gh@...tonmail.com, benno.lossin@...ton.me,
a.hindborg@...nel.org, tmgross@...ch.edu, linux-pci@...r.kernel.org,
rust-for-linux@...r.kernel.org, linux-kernel@...r.kernel.org
Subject: Re: [PATCH 1/4] rust: device: implement Device::parent()
On Thu, Mar 20, 2025 at 9:34 AM Alice Ryhl <aliceryhl@...gle.com> wrote:
>
> On Wed, Mar 19, 2025 at 09:30:25PM +0100, Danilo Krummrich wrote:
> > Device::parent() returns a reference to the device' parent device, if
> > any.
> >
> > Signed-off-by: Danilo Krummrich <dakr@...nel.org>
> > ---
> > rust/kernel/device.rs | 13 +++++++++++++
> > 1 file changed, 13 insertions(+)
> >
> > diff --git a/rust/kernel/device.rs b/rust/kernel/device.rs
> > index 21b343a1dc4d..76b341441f3f 100644
> > --- a/rust/kernel/device.rs
> > +++ b/rust/kernel/device.rs
> > @@ -65,6 +65,19 @@ pub(crate) fn as_raw(&self) -> *mut bindings::device {
> > self.0.get()
> > }
> >
> > + /// Returns a reference to the parent device, if any.
> > + pub fn parent<'a>(&self) -> Option<&'a Self> {
> > + // SAFETY: By the type invariant `self.as_raw()` is always valid.
> > + let parent = unsafe { *self.as_raw() }.parent;
>
> This means:
> 1. Copy the entire `struct device` onto the stack.
> 2. Read the `parent` field of the copy.
>
> Please write this instead to only read the `parent` field:
> let parent = unsafe { *self.as_raw().parent };
Sorry I meant (*self.as_raw()).parent
Powered by blists - more mailing lists