Linux-PHY Archive on lore.kernel.org
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Sebastian Reichel" <sebastian.reichel@collabora.com>
Cc: linux-phy@lists.infradead.org, neil.armstrong@linaro.org,
	olteanv@gmail.com, vkoul@kernel.org
Subject: Re: [PATCH v16 6/6] usb: dwc3: rockchip: fix USB-C reconnect in gadget mode
Date: Thu, 24 Sep 2026 17:40:13 +0000	[thread overview]
Message-ID: <20260924174014.366B21F000FF@smtp.kernel.org> (raw)
In-Reply-To: <20260924-b4-rockchip-dwc3-rockchip-glue-v16-6-126a2e9133c3@collabora.com>

Thank you for your contribution! Sashiko AI review found 4 potential issue(s) to consider:
- [High] usb: dwc3: rockchip: VBUS update skipped due to early return on suspended device
- [High] usb: dwc3: rockchip: VBUS updates are lost when no gadget driver is bound
- [High] usb: dwc3: rockchip: TOCTOU use-after-free of dwc->gadget in vbus handler
- [Medium] usb: dwc3: rockchip: data race on dwc_rk->role during PHY reset and role switch
--

commit 955d4b753ff8d796d1e7b3c020a5064ca8b3dd0a
Author: Sebastian Reichel <sebastian.reichel@collabora.com>

usb: dwc3: rockchip: fix USB-C reconnect in gadget mode

When USB-C is configured in gadget mode and the cable is unplugged the USB
controller is suspended. After plugging in the cable again, the USB controller
stays suspended and thus the port status remains not-attached. Fix this by
informing the USB core about VBUS updates when the PHY mode changes.

> diff --git a/drivers/usb/dwc3/dwc3-rockchip.c b/drivers/usb/dwc3/dwc3-rockchip.c
> index 17b6c2ca9e192..ba8ffef2f27e4 100644
> --- a/drivers/usb/dwc3/dwc3-rockchip.c
> +++ b/drivers/usb/dwc3/dwc3-rockchip.c
> @@ -25,8 +25,17 @@ struct dwc3_rockchip {
[ ... ]
> +static void dwc3_rockchip_vbus_handler(struct dwc3 *dwc, bool present)
> +{
> +	if (!dwc->gadget || !dwc->gadget_driver)
> +		return;
> +
> +	usb_udc_vbus_handler(dwc->gadget, present);
> +}

[Severity: High]
Can dropping physical VBUS events when no gadget driver is bound
desynchronize the UDC core's state from reality?

The UDC core tracks udc->vbus globally and it defaults to true. If the cable
is physically disconnected while no driver is bound, the false update is
dropped. When a user subsequently loads a gadget driver, the UDC core
observes udc->vbus == true and wrongly activates the pullup resistor. Pre-set
role events will often occur before a gadget driver binds, guaranteeing that
these VBUS updates will be incorrectly dropped.

usb_udc_vbus_handler() safely buffers updates without a driver, making this
check unnecessary.

[Severity: High]
Does reading dwc->gadget locklessly here introduce a Time-of-Check to
Time-of-Use (TOCTOU) use-after-free vulnerability?

This handler is invoked asynchronously by the PHY notifier and locklessly reads
dwc->gadget and dwc->gadget_driver. Concurrently, during a DRD role switch or
driver unbind, dwc3_gadget_exit() in drivers/usb/dwc3/gadget.c sets
dwc->gadget_driver to NULL and calls usb_put_gadget(dwc->gadget), freeing it
without setting the pointer to NULL.

If the PHY thread passes the !dwc->gadget_driver check before teardown unbinds
the driver, but is then preempted, could it dereference a freed gadget pointer
when invoking usb_udc_vbus_handler()?

> +
>  static int dwc3_usb3_phy_notify(struct notifier_block *nb,
>  				unsigned long action, void *data)
>  {
> @@ -57,6 +66,8 @@ static int dwc3_usb3_phy_notify(struct notifier_block *nb,
>  		if (!ret)
>  			return NOTIFY_OK;
>  
> +		dwc3_rockchip_vbus_handler(dwc, false);
> +

[Severity: High]
Does this early return completely bypass the newly introduced VBUS handler
logic when the controller is suspended?

When the controller is suspended, pm_runtime_get_if_active(dwc->dev)
returns 0. This causes dwc3_usb3_phy_notify() to return early from
PHY_NOTIFY_PRE_RESET, entirely skipping the newly added
dwc3_rockchip_vbus_handler() call.

Since this patch is trying to fix the case where the controller stays
suspended on cable plug-in, doesn't bailing here cause the patch to fail to
achieve its primary goal?

>  		/*
>  		 * Assert USB3 PHY soft reset within DWC3 before the external
>  		 * PHY resets. This disconnects the PIPE interface, preventing
[ ... ]
>  	case PHY_NOTIFY_POST_RESET:
> @@ -89,6 +101,8 @@ static int dwc3_usb3_phy_notify(struct notifier_block *nb,
>  		dwc3_writel(dwc, DWC3_GUSB3PIPECTL(port), reg);
>  		spin_unlock_irqrestore(&dwc->lock, flags);
>  
> +		dwc3_rockchip_vbus_handler(dwc, dwc_rk->role == USB_ROLE_DEVICE);
> +

[Severity: High]
Because the PHY_NOTIFY_PRE_RESET logic bailed early on a suspended device,
phy_reset_active is false. Thus the PHY_NOTIFY_POST_RESET handler will early
return before this point:

	case PHY_NOTIFY_POST_RESET:
		spin_lock_irqsave(&dwc->lock, flags);
		if (!(dwc_rk->phy_reset_active & BIT(port))) {
			spin_unlock_irqrestore(&dwc->lock, flags);
			return NOTIFY_OK;
		}

Does this also skip the POST_RESET VBUS update, leaving the device
perpetually suspended?

[Severity: Medium]
Is there a data race on dwc_rk->role here?

This reads dwc_rk->role outside of any locking boundary when determining
whether to propagate a true VBUS state.

>  		pm_runtime_put_autosuspend(dwc->dev);
>  		break;
>  	}
> @@ -130,7 +144,16 @@ static int dwc3_rk_phy_register_notifiers(struct dwc3 *dwc)
>  	return devm_add_action_or_reset(dwc->dev, dwc3_rk_phy_unregister_notifiers, dwc_rk);
>  }
>  
> +static void dwc3_rockchip_set_role(struct dwc3 *dwc, enum usb_role role)
> +{
> +	struct dwc3_rockchip *dwc_rk = container_of(dwc, struct dwc3_rockchip, dwc);
> +
> +	dwc_rk->role = role;
> +	dwc3_rockchip_vbus_handler(dwc, role == USB_ROLE_DEVICE);
> +}

[Severity: Medium]
Is there a data race on dwc_rk->role here?

This locklessly mutates the rockchip-specific role state, which could be read
concurrently during a PHY reset. Can this result in inconsistent role
observations that propagate an incorrect VBUS state to the UDC core?

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20260924-b4-rockchip-dwc3-rockchip-glue-v16-0-126a2e9133c3@collabora.com?part=6

-- 
linux-phy mailing list
linux-phy@lists.infradead.org
https://lists.infradead.org/mailman/listinfo/linux-phy

  reply	other threads:[~2026-09-24 17:40 UTC|newest]

Thread overview: 21+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-09-24 17:25 [PATCH v16 0/6] usb: dwc3: introduce Rockchip glue driver Sebastian Reichel
2026-09-24 17:25 ` [PATCH v16 1/6] phy: core: add notifier infrastructure Sebastian Reichel
2026-09-24 17:35   ` sashiko-bot
2026-09-26  7:47   ` Manivannan Sadhasivam
2026-10-03  9:04   ` Vinod Koul
2026-10-04 12:44     ` Sebastian Reichel
2026-10-05  8:30       ` Vinod Koul
2026-10-06  1:11         ` Sebastian Reichel
2026-09-24 17:25 ` [PATCH v16 2/6] usb: dwc3: rockchip: introduce glue driver Sebastian Reichel
2026-09-24 17:36   ` sashiko-bot
2026-09-24 17:25 ` [PATCH v16 3/6] usb: dwc3: core: add post PHY registration hook for platform glue Sebastian Reichel
2026-09-24 17:25 ` [PATCH v16 4/6] usb: dwc3: rockchip: support PHY reset notifications Sebastian Reichel
2026-09-24 17:38   ` sashiko-bot
2026-09-26  7:51   ` Manivannan Sadhasivam
2026-10-02 23:39   ` Thinh Nguyen
2026-09-24 17:25 ` [PATCH v16 5/6] usb: gadget: define stub for usb_udc_vbus_handler Sebastian Reichel
2026-09-24 17:25 ` [PATCH v16 6/6] usb: dwc3: rockchip: fix USB-C reconnect in gadget mode Sebastian Reichel
2026-09-24 17:40   ` sashiko-bot [this message]
2026-09-27 12:02   ` Igor Paunovic
2026-10-02 22:57   ` Thinh Nguyen
2026-09-27 12:02 ` [PATCH v16 0/6] usb: dwc3: introduce Rockchip glue driver Igor Paunovic

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=20260924174014.366B21F000FF@smtp.kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=linux-phy@lists.infradead.org \
    --cc=neil.armstrong@linaro.org \
    --cc=olteanv@gmail.com \
    --cc=sashiko-reviews@lists.linux.dev \
    --cc=sebastian.reichel@collabora.com \
    --cc=vkoul@kernel.org \
    /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