Re: [PATCH v2 2/2] rust: miscdevice: access the `struct miscdevice` from fops->open()
From: Alice Ryhl
Date: Mon Dec 09 2024 - 06:36:26 EST
On Mon, Dec 9, 2024 at 12:07 PM Danilo Krummrich <dakr@xxxxxxxxxx> wrote:
>
> On Mon, Dec 09, 2024 at 07:27:47AM +0000, Alice Ryhl wrote:
> > Providing access to the underlying `struct miscdevice` is useful for
> > various reasons. For example, this allows you access the miscdevice's
> > internal `struct device` for use with the `dev_*` printing macros.
> >
> > Note that since the underlying `struct miscdevice` could get freed at
> > any point after the fops->open() call, only the open call is given
> > access to it. To print from other calls, they should take a refcount on
> > the device to keep it alive.
> >
> > Signed-off-by: Alice Ryhl <aliceryhl@xxxxxxxxxx>
> > ---
> > rust/kernel/miscdevice.rs | 19 ++++++++++++++++---
> > 1 file changed, 16 insertions(+), 3 deletions(-)
> >
> > diff --git a/rust/kernel/miscdevice.rs b/rust/kernel/miscdevice.rs
> > index 0cb79676c139..c5af1d5ec4be 100644
> > --- a/rust/kernel/miscdevice.rs
> > +++ b/rust/kernel/miscdevice.rs
> > @@ -104,7 +104,7 @@ pub trait MiscDevice {
> > /// Called when the misc device is opened.
> > ///
> > /// The returned pointer will be stored as the private data for the file.
> > - fn open(_file: &File) -> Result<Self::Ptr>;
> > + fn open(_file: &File, _misc: &MiscDeviceRegistration<Self>) -> Result<Self::Ptr>;
>
> How is the user of this abstraction supposed to access the underlying struct
> miscdevice e.g. from other fops? AFAICS, there is no way for the user to store a
> device pointer / reference in their driver private data.
I had assumed that the miscdevice does not necessarily live long
enough for that to be okay ... but if it does we can change it. See
other thread with Greg.
> I also think it's a bit weird to pass the registration structure in open() to
> access the device.
>
> I think we need an actual representation of a struct miscdevice, i.e.
> `misc::Device`.
It sounds like we can just rename `MiscDeviceRegistration` to `Device`.
> We can discuss whether we want to implement it like I implemented `pci::Device`
> and `platform::Device`, i.e. as an `ARef<device::Device>` or if we do it like
> you proposed, but I think things should be aligned.
Let's figure out the lifetime of `struct miscdevice` first ...
Alice