Re: [PATCH v3 4/5] i3c: add i3cdev module to expose i3c dev in /dev

From: Boris Brezillon
Date: Mon Feb 24 2020 - 06:22:13 EST


On Mon, 24 Feb 2020 11:04:50 +0000
Vitor Soares <Vitor.Soares@xxxxxxxxxxxx> wrote:

> Hi Boris,
>
> From: Boris Brezillon <boris.brezillon@xxxxxxxxxxxxx>
> Date: Fri, Feb 21, 2020 at 22:32:16
>
> > On Wed, 19 Feb 2020 01:20:42 +0100
> > Vitor Soares <Vitor.Soares@xxxxxxxxxxxx> wrote:
> >
> > > +static int i3cdev_detach(struct device *dev, void *dummy)
> > > +{
> > > + struct i3cdev_data *i3cdev;
> > > + struct i3c_device *i3c;
> > > +
> > > + if (dev->type == &i3c_masterdev_type)
> > > + return 0;
> > > +
> > > + i3c = dev_to_i3cdev(dev);
> > > +
> > > + i3cdev = i3cdev_get_drvdata(i3c);
> > > + if (!i3cdev)
> > > + return 0;
> > > +
> > > + /* Prevent transfers while cdev removal */
> > > + mutex_lock(&i3cdev->xfer_lock);
> > > + cdev_del(&i3cdev->cdev);
> >
> > When cdev_del() returns there might be opened FDs pointing to your
> > i3cdev [1] ...
>
> Yes, I know. I protected the driver part but I missed the
> file->private_data.

Not sure what you mean by protection, but if you meant locking, then
it's not enough: you need to refcnt the struct if you want to prevent
use-after-free situations.

BTW, I had a closer look at the usbdev implementation, and maybe you
should base yours on usb instead of i2c. They seem to register a cdev at
module init time, and add a dev_t per device at device registration
time. Not sure how they handle the userspace-driver vs kernel-driver
concurrency, but maybe returning EACCES (or EBUSY) instead of
detaching/attaching the i3cdev everytime a device is bound/unbound
would be simpler.

Also, I think Arnd was right, it'd be simpler if i3cdev support was
integrated to the core (still left as a option so it can be disabled,
but with a dedicated i3cdev field in i3c_device instead hijacking the
driver private field).