Re: [PATCH v6 2/2] usb: gadget: udc: core: Prevent soft_connect_store() race

From: Alan Stern
Date: Thu Jun 08 2023 - 11:27:15 EST


On Wed, Jun 07, 2023 at 10:17:04PM -0700, Badhri Jagan Sridharan wrote:
> On Wed, Jun 7, 2023 at 11:26 AM Alan Stern <stern@xxxxxxxxxxxxxxxxxxx>
> wrote:
> > > @@ -756,10 +772,12 @@ int usb_gadget_disconnect(struct usb_gadget
> > *gadget)
> > > if (!gadget->connected)
> > > goto out;
> > >
> > > - if (gadget->deactivated) {
> > > + if (gadget->deactivated || !gadget->udc->started) {
> >
> > Do you really need to add this extra test? After all, if the gadget
> > isn't started then how could the previous test of gadget->connected
> > possibly succeed?
> >
> > In fact, I suspect this entire section of code was always useless, since
> > the gadget couldn't be connected now if it was already deactivated.
> >
>
> Thanks Alan ! Will fix all other comments in v7 but not sure about this one.
> Although the ->pullup() function will not be called,
> -> connected flag could actually be set when the gadget is not started.
>
> - if (gadget->deactivated || !gadget->udc->allow_connect) {
> + if (gadget->deactivated || !gadget->udc->allow_connect ||
> !gadget->udc->started) {
> /*
> * If gadget is deactivated we only save new state.
> * Gadget will be connected automatically after activation.
> + *
> + * udc first needs to be started before gadget can be pulled up.
> */
> gadget->connected = true;
>
> This could happen, for instance, when usb_udc_vbus_handler() is invoked
> after soft_connect_store() disconnects the gadget.
> Same applies to when usb_gadget_connect() is called after the gadget has
> been deactivated through usb_gadget_deactivate().
>
> This implies that the checks should be there, right ?

Yes, you're right; the checks do need to be there. I had forgotten
about these possible cases. Ignore that comment.

Alan Stern