From: sashiko-bot@kernel.org
To: "Alexey Charkov" <alchark@flipper.net>
Cc: linux-usb@vger.kernel.org
Subject: Re: [PATCH v3 3/3] usb: gadget: udc: Decide on connecting in one place
Date: Tue, 17 Nov 2026 13:19:49 +0000 [thread overview]
Message-ID: <sashiko-outbox-218865@kernel.org> (raw)
In-Reply-To: <20261117-udc-connect-v3-3-9413b491d213@flipper.net>
Thank you for your contribution! Sashiko AI review found 4 potential
issues to consider:
- [High] usb: gadget: udc: missing driver->disconnect() when
!ops->pullup
- [Medium] usb: gadget: udc: -EOPNOTSUPP from pullup() breaks bind and
activation
- [Medium] usb: gadget: udc: driver->disconnect() called when pullup
disable fails
- [Low] usb: gadget: udc: unreachable !gadget->ops->pullup check in
disconnect
--
commit f20c9b97618b74d0e27972a1d4cf419b12e02088
Author: Alexey Charkov <alchark@flipper.net>
usb: gadget: udc: Decide on connecting in one place
This commit centralizes UDC connection state evaluation in
usb_udc_update_connection_locked() so that pullup control depends on driver
binding, VBUS presence, activation state, and sysfs soft_connect.
> diff --git a/drivers/usb/gadget/udc/core.c b/drivers/usb/gadget/udc/core.c
> index 449b303c2f813..496083045a993 100644
> --- a/drivers/usb/gadget/udc/core.c
> +++ b/drivers/usb/gadget/udc/core.c
[ ... ]
> @@ -766,26 +773,20 @@ static int usb_gadget_disconnect_locked(struct usb_gadget *gadget)
> {
> int ret = 0;
>
> - if (!gadget->ops->pullup) {
> - ret = -EOPNOTSUPP;
> - goto out;
> - }
> -
> if (!gadget->connected)
> goto out;
>
> - if (gadget->deactivated || !gadget->udc->started) {
> - /*
> - * If gadget is deactivated we only save new state.
> - * Gadget will stay disconnected after activation.
> - */
> - gadget->connected = false;
> + if (!gadget->ops->pullup) {
> + ret = -EOPNOTSUPP;
> goto out;
> }
[Severity: Low]
Is this !gadget->ops->pullup check reachable? gadget->connected is only set
in usb_gadget_connect_locked() after checking that gadget->ops->pullup is
non-NULL, so when ops->pullup is NULL, !gadget->connected always jumps to out
first and usb_gadget_disconnect() returns 0 instead of -EOPNOTSUPP.
>
> ret = gadget->ops->pullup(gadget, 0);
> if (!ret)
> gadget->connected = 0;
> + else if (ret != -EOPNOTSUPP)
> + dev_warn(&gadget->udc->dev, "failed to disable the pullup: %d\n",
> + ret);
>
> mutex_lock(&udc_lock);
> if (gadget->udc->driver)
> gadget->udc->driver->disconnect(gadget);
> mutex_unlock(&udc_lock);
[Severity: Medium]
Should gadget->udc->driver->disconnect(gadget) still run when
gadget->ops->pullup(gadget, 0) fails?
When disabling the pullup fails, gadget->connected stays 1 and
usb_gadget_deactivate() rolls back gadget->deactivated = false under the
assumption that the gadget remains connected and active. However,
driver->disconnect() has already reset the composite configuration and
disabled the function endpoints while the hardware pullup is still enabled,
and any later retry (such as from soft_connect_store() or vbus_event_work())
will call driver->disconnect() again.
[ ... ]
> @@ -823,6 +828,34 @@ int usb_gadget_disconnect(struct usb_gadget *gadget)
> }
> EXPORT_SYMBOL_GPL(usb_gadget_disconnect);
>
> +static void usb_udc_update_allow_connect(struct usb_udc *udc)
> + __must_hold(&udc->connect_lock)
> +{
> + udc->allow_connect = udc->bound && udc->vbus &&
> + !udc->gadget->deactivated &&
> + !udc->soft_disconnected;
> +}
> +
> +/*
> + * Decide whether the gadget may connect and connect or disconnect it to match.
> + * Call this after changing any of the state that
> + * usb_udc_update_allow_connect() checks.
> + */
> +static int usb_udc_update_connection_locked(struct usb_udc *udc)
> + __must_hold(&udc->connect_lock)
> +{
> + usb_udc_update_allow_connect(udc);
> +
> + /* Without a pullup control there is nothing to switch */
> + if (!udc->gadget->ops->pullup)
> + return 0;
> +
> + if (udc->allow_connect)
> + return usb_gadget_connect_locked(udc->gadget);
> +
> + return usb_gadget_disconnect_locked(udc->gadget);
> +}
[Severity: Medium]
What happens when a UDC driver defines ops->pullup, but that callback returns
-EOPNOTSUPP?
usb_gadget_connect_locked() and usb_gadget_disconnect_locked() suppress the
warning when ops->pullup() returns -EOPNOTSUPP, but still return -EOPNOTSUPP
to usb_udc_update_connection_locked(). UDC drivers such as pxa27x_udc (when
no pullup GPIO is configured) and fsl_udc_core (when VBUS is inactive, since
it does not use usb_udc_vbus_handler()) return -EOPNOTSUPP from their pullup
callbacks, which causes gadget_bind_driver(), usb_gadget_activate(), and
usb_gadget_deactivate() to fail with -EOPNOTSUPP.
[ ... ]
> @@ -1726,21 +1730,31 @@ static void gadget_unbind_driver(struct device *dev)
>
> dev_dbg(&udc->dev, "unbinding gadget driver [%s]\n", driver->function);
>
> - udc->allow_connect = false;
> - cancel_work_sync(&udc->vbus_work);
> mutex_lock(&udc->connect_lock);
> - usb_gadget_disconnect_locked(gadget);
> + udc->bound = false;
> + usb_udc_update_connection_locked(udc);
> + /*
> + * The UDC stops below even if the pullup refused to switch off (some
> + * UDCs refuse without VBUS), so treat the gadget as disconnected from
> + * now on. Neither a late VBUS event nor the next driver's bind should
> + * act on a connection that ends with this driver.
> + */
> + gadget->connected = false;
> usb_gadget_disable_async_callbacks(udc);
> if (gadget->irq)
> synchronize_irq(gadget->irq);
> mutex_unlock(&udc->connect_lock);
>
> + /* A VBUS event can no longer connect the gadget, so just wait it out */
> + cancel_work_sync(&udc->vbus_work);
> +
> udc->driver->unbind(gadget);
[Severity: High]
On a UDC without ops->pullup (such as max3420_udc or fotg210-udc), does
udc->driver->disconnect(gadget) ever get called before udc->driver->unbind()
or on VBUS loss?
With usb_udc_update_connection_locked() returning 0 early when
!udc->gadget->ops->pullup, gadget_bind_driver() now succeeds and starts the
UDC without setting gadget->connected. Once the host enumerates and
configures the gadget, unbinding the driver (or losing VBUS in
vbus_event_work()) skips usb_gadget_disconnect_locked() and never invokes
udc->driver->disconnect(gadget). udc->driver->unbind(gadget) then runs while
cdev->config is still active and function->disable() has never been called,
triggering WARN_ON(cdev->config) in __composite_unbind() or tearing down
active functions in configfs_composite_unbind() with enabled endpoints and
dangling function state (such as use-after-free on dev->port_usb in u_ether).
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261117-udc-connect-v3-0-9413b491d213@flipper.net?part=3
prev parent reply other threads:[~2026-11-17 13:19 UTC|newest]
Thread overview: 7+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-17 12:48 [PATCH v3 0/3] usb: gadget: udc: Rework when the gadget connects Alexey Charkov
2026-11-17 12:48 ` [PATCH v3 1/3] usb: gadget: udc: Clear a pending connection when starting the UDC Alexey Charkov
2026-11-17 13:03 ` sashiko-bot
2026-11-17 12:48 ` [PATCH v3 2/3] usb: gadget: udc: Forget a deactivation when the gadget driver unbinds Alexey Charkov
2026-11-17 13:06 ` sashiko-bot
2026-11-17 12:48 ` [PATCH v3 3/3] usb: gadget: udc: Decide on connecting in one place Alexey Charkov
2026-11-17 13:19 ` sashiko-bot [this message]
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=sashiko-outbox-218865@kernel.org \
--to=sashiko-bot@kernel.org \
--cc=alchark@flipper.net \
--cc=linux-usb@vger.kernel.org \
--cc=sashiko-reviews@lists.linux.dev \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
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).