[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-ID: <Z917yQdlWYo6GrkG@pollux>
Date: Fri, 21 Mar 2025 15:46:33 +0100
From: Danilo Krummrich <dakr@...nel.org>
To: Boqun Feng <boqun.feng@...il.com>
Cc: bhelgaas@...gle.com, gregkh@...uxfoundation.org, rafael@...nel.org,
ojeda@...nel.org, alex.gaynor@...il.com, gary@...yguo.net,
bjorn3_gh@...tonmail.com, benno.lossin@...ton.me,
a.hindborg@...nel.org, aliceryhl@...gle.com, tmgross@...ch.edu,
linux-pci@...r.kernel.org, rust-for-linux@...r.kernel.org,
linux-kernel@...r.kernel.org
Subject: Re: [PATCH v2 1/4] rust: device: implement Device::parent()
On Fri, Mar 21, 2025 at 07:40:57AM -0700, Boqun Feng wrote:
> On Thu, Mar 20, 2025 at 11:27:43PM +0100, Danilo Krummrich wrote:
> > Device::parent() returns a reference to the device' parent device, if
> > any.
> >
> > Reviewed-by: Alice Ryhl <aliceryhl@...gle.com>
> > Signed-off-by: Danilo Krummrich <dakr@...nel.org>
> > ---
> > rust/kernel/device.rs | 15 +++++++++++++++
> > 1 file changed, 15 insertions(+)
> >
> > diff --git a/rust/kernel/device.rs b/rust/kernel/device.rs
> > index 21b343a1dc4d..f6bdc2646028 100644
> > --- a/rust/kernel/device.rs
> > +++ b/rust/kernel/device.rs
> > @@ -65,6 +65,21 @@ 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.
> > + // - The parent device is only ever set at device creation.
> > + let parent = unsafe { (*self.as_raw()).parent };
> > +
> > + if parent.is_null() {
> > + None
> > + } else {
> > + // SAFETY: Since `parent` is not NULL, it must be a valid pointer to a `struct device`.
> > + Some(unsafe { Self::as_ref(parent) })
>
> The safety comment also needs to explain why the parent device won't be
> gone, I assume a struct device holds a refcount of its parent?
Correct, this is taken generically in device_add().
> Therefore
> the borrow checker would ensure the parent exists as long as the Device
> is borrowed.
Yes.
Powered by blists - more mailing lists