Re: [PATCH 1/1] usb: gadget: f_hid: Conduct proper refcounting on shared f_hidg pointer

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On Sun, Nov 20, 2022 at 05:22:19PM +0000, John Keeping wrote:
> On Fri, Nov 18, 2022 at 04:07:24PM -0500, Alan Stern wrote:
> > On Fri, Nov 18, 2022 at 04:37:32PM +0000, John Keeping wrote:
> > > I don't think it's at all simple to fix this - I posted a series
> > > addressing the lifetime issues here a few years ago but didn't chase it
> > > up and there was no feedback:
> > > 
> > > 	https://lore.kernel.org/linux-usb/20191028114228.3679219-1-john@xxxxxxxxxxxx/
> > > 
> > > That includes a patch to remove the embedded struct cdev and manage its
> > > lifetime separately, which I think is needed as there are two different
> > > struct device objects here and we cannot tie their lifetimes together.
> > 
> > I still don't have a clear picture of what the real problem is.  Lee's 
> > original patch description just said "external references are presently 
> > not tracked", with no details about what those external references are. 
> > Why not add just proper cdev_get() and cdev_put() calls to whatever code 
> > handles those external references, so that they _are_ tracked?
> > 
> > What are the two different struct device objects?  Why do their 
> > lifetimes need to be tied together?  If you do need to tie their 
> > lifetimes somehow, why not simply make one of them (the one which is 
> > logically allowed to be shorter-lived) hold a reference to the other?
> 
> The problem is that we have a struct cdev embedded in f_hidg but the
> lifetime of f_hidg is not tied to any kobject so we can't solve this in
> the right way by setting the parent kobject of the cdev.
> 
> While refcounting struct f_hidg is necessary, it's not sufficient
> because the only way to keep it alive long enough for the final
> kobject_put() on the embedded cdev is to tie the lifetime to a kobject
> of its own and there is no suitable object as this is not the model
> followed by gadget function instances.

I see.  The solution is simple: Embed a struct device in struct f_hidg, 
and call cdev_device_add() to add the device and the cdev.  This will 
automatically make the device the parent of the cdev, so the device's 
refcount won't go to 0 until the cdev's refcount does.  Then you can tie 
the f_hidg's lifetime to the device's, so the device's release routine 
can safely deallocate the entire f_hidg structure.

The parent of the new struct device should be set to &gadget->dev.  If 
you can't think of a better name for the device, you could simply append 
":I" to the parent's name, where I is the interface number, or even 
append ":C.I" where C is the config number (like we do on the host 
side).

Alan Stern



[Index of Archives]     [Linux Media]     [Linux Input]     [Linux Audio Users]     [Yosemite News]     [Linux Kernel]     [Linux SCSI]     [Old Linux USB Devel Archive]

  Powered by Linux