linux-usb.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* Adding and removing the same gadget multiple times
@ 2020-07-28 19:32 Alan Stern
  2020-07-29  1:47 ` Peter Chen
  0 siblings, 1 reply; 5+ messages in thread
From: Alan Stern @ 2020-07-28 19:32 UTC (permalink / raw)
  To: Roger Quadros; +Cc: USB mailing list

Roger:

Your commit fac323471df6 ("usb: udc: allow adding and removing the same 
gadget device") from a few years ago just caught my eye.  (I don't 
recall whether I noticed it at the time.)

In any case, we need to talk about it.  What you're doing -- 
unregistering and re-registering the struct device embedded in the 
gadget structure -- is strictly forbidden by the kernel's device model. 
It's even mentioned specifically in the kerneldoc for device_add().

Now, I guess doing this would be okay _if_ you took care not to 
re-register the device until all references to the previous incarnation 
have been dropped.  In particular, setting the structure's memory to 0 
should not be done immediately after calling device_unregister() -- 
which is what the commit does -- but rather in the release routine.

Do you know which UDC drivers actually do re-register their gadgets?  In 
particular, do they have their own release routines or do they rely on 
the default usb_udc_nop_release() provided by the UDC core?  Moving the 
memset into that routine ought to be okay; leaving it where it is would 
be a time bomb waiting to go off.  I'm suprised it hasn't caused 
problems already.

Furthermore, drivers that do re-register their gadgets should wait until 
gadget->dev.release is NULL, indicating that the release routine has 
been called and the memory has been wiped.  If they re-register before 
that, the re-registration will fail for the same reasons you observed 
when you wrote the commit.

Of course, a cleaner alternative would be to allocate the gadget 
structure dynamically.  Then instead of re-registering the old one, the 
driver could allocate a new one and register it instead, with no 
concerns about reuse.

Alan Stern

^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2020-07-30  2:37 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-07-28 19:32 Adding and removing the same gadget multiple times Alan Stern
2020-07-29  1:47 ` Peter Chen
2020-07-29 14:14   ` Alan Stern
2020-07-29 14:19     ` Greg KH
2020-07-30  2:37     ` Peter Chen

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).