Linux USB
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Fabrice Gasnier" <fabrice.gasnier@foss.st.com>
Cc: linux-usb@vger.kernel.org
Subject: Re: [PATCH v6 04/11] usb: dwc2: clean up old role on mode change during suspend
Date: Wed, 25 Nov 2026 16:57:49 +0000	[thread overview]
Message-ID: <sashiko-outbox-233788@kernel.org> (raw)
In-Reply-To: <20261125-dwc2-low-power-fixes-stm32-v6-4-d5df33b93eaf@foss.st.com>

Thank you for your contribution! Sashiko AI review found 1 potential
issue to consider:
- [Medium] usb: dwc2: device endpoint register access in host mode in
  dwc2_hsotg_role_lost()
--

commit 9eca46957e8d956eb9059255d4d71f159a5ec1a4
Author: Fabrice Gasnier <fabrice.gasnier@foss.st.com>

usb: dwc2: clean up old role on mode change during suspend

This commit records the active role at suspend and, if register state is
lost during suspend while the OTG ID pin changes, cleans up the old role
and initializes the new role on resume instead of restoring the previous
role's register backup.

> diff --git a/drivers/usb/dwc2/gadget.c b/drivers/usb/dwc2/gadget.c
> index 295091a87b129..9b12c336e4aa9 100644
> --- a/drivers/usb/dwc2/gadget.c
> +++ b/drivers/usb/dwc2/gadget.c
> @@ -5251,6 +5251,26 @@ int dwc2_hsotg_resume(struct dwc2_hsotg *hsotg, bool lost)
>  	return 0;
>  }
>  
> +/**
> + * dwc2_hsotg_role_lost - drop gadget state after a role change in suspend
> + * @hsotg: Programming view of the DWC_otg controller
> + *
> + * The core came back from register loss in host mode while the gadget was
> + * active at suspend. Disconnect the gadget and clear stale software state
> + * without touching the endpoint registers, which are not usable in host mode.
> + */
> +void dwc2_hsotg_role_lost(struct dwc2_hsotg *hsotg)
> +{
> +	unsigned long flags;
> +
> +	spin_lock_irqsave(&hsotg->lock, flags);
> +	dwc2_hsotg_disconnect(hsotg);
> +	dwc2_clear_fifo_map(hsotg);
> +	hsotg->gadget.speed = USB_SPEED_UNKNOWN;
> +	dwc2_clear_lost_state(hsotg);
> +	spin_unlock_irqrestore(&hsotg->lock, flags);
> +}

[Severity: Medium]
Can dwc2_hsotg_role_lost() still access device endpoint registers while
the controller is in host mode, and leave endpoint state stale when
usb_ep_disable() is deferred?

When the USB bus is already suspended (hsotg->lx_state is DWC2_L2 or
DWC2_L1) at system suspend, dwc2_hsotg_suspend() returns early, leaving
hsotg->connected set, endpoints enabled with non-zero fifo_index and
fifo_size, and hsotg->op_state at OTG_STATE_B_PERIPHERAL.  On resume into
host mode, dwc2_hsotg_role_lost() runs before dwc2_hcd_restore() updates
hsotg->op_state to OTG_STATE_A_HOST, and calls dwc2_hsotg_disconnect():

1. dwc2_hsotg_disconnect() calls kill_all_requests() on every endpoint,
   which reads DTXFSTS(ep->fifo_index) and flushes the TX FIFO when
   hsotg->dedicated_fifos is set:

drivers/usb/dwc2/gadget.c:kill_all_requests() {
    ...
	if (!hsotg->dedicated_fifos)
		return;
	size = (dwc2_readl(hsotg, DTXFSTS(ep->fifo_index)) & 0xffff) * 4;
	if (size < ep->fifo_size)
		dwc2_hsotg_txfifo_flush(hsotg, ep->fifo_index);
}

2. dwc2_hsotg_disconnect() then invokes call_gadget(hsotg, disconnect).
   Gadget drivers that synchronously call usb_ep_disable() during
   disconnect enter dwc2_hsotg_ep_disable(), where the host-mode guard
   (hsotg->op_state != OTG_STATE_B_PERIPHERAL) does not trigger because
   op_state has not been updated yet, so it reads and writes DIEPCTL,
   DOEPCTL, DAINTMSK and DTXFSTS while the hardware is in host mode.

3. Conversely, if a gadget function driver defers usb_ep_disable() to a
   workqueue or thread that runs after dwc2_hcd_restore() sets op_state to
   OTG_STATE_A_HOST, dwc2_hsotg_ep_disable() returns -EINVAL without
   clearing hs_ep->fifo_index or hs_ep->fifo_size, and usb_ep_disable()
   leaves ep->enabled set so the endpoint cannot be re-enabled on the next
   switch back to device mode.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261125-dwc2-low-power-fixes-stm32-v6-0-d5df33b93eaf@foss.st.com?part=4

  reply	other threads:[~2026-11-25 16:57 UTC|newest]

Thread overview: 17+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-25 16:32 [PATCH v6 00/11] usb: dwc2: fix low power issues found on stm32mp1 Fabrice Gasnier
2026-11-25 16:32 ` [PATCH v6 01/11] usb: dwc2: fix recovery after PM domain off Fabrice Gasnier
2026-11-25 16:58   ` sashiko-bot
2026-11-25 16:32 ` [PATCH v6 02/11] usb: dwc2: keep TOUTCAL canary valid after PHY init Fabrice Gasnier
2026-11-25 16:32 ` [PATCH v6 03/11] usb: dwc2: drop stale state after register restore on resume Fabrice Gasnier
2026-11-25 17:09   ` sashiko-bot
2026-11-25 16:32 ` [PATCH v6 04/11] usb: dwc2: clean up old role on mode change during suspend Fabrice Gasnier
2026-11-25 16:57   ` sashiko-bot [this message]
2026-11-25 16:32 ` [PATCH v6 05/11] usb: dwc2: fix a possible interrupt storm in hcd resume Fabrice Gasnier
2026-11-25 16:32 ` [PATCH v6 06/11] usb: dwc2: fix a possible interrupt storm on stm32mp1 Fabrice Gasnier
2026-11-25 16:32 ` [PATCH v6 07/11] usb: dwc2: hcd: fix possible successive vbus supply enables Fabrice Gasnier
2026-11-25 16:32 ` [PATCH v6 08/11] usb: dwc2: hcd: don't disable external vbus in system suspend Fabrice Gasnier
2026-11-25 16:32 ` [PATCH v6 09/11] usb: dwc2: restore peripheral mode on low-level enable Fabrice Gasnier
2026-11-25 17:08   ` sashiko-bot
2026-11-25 16:32 ` [PATCH v6 10/11] usb: dwc2: platform: fix stm32mp15 detection logic power management Fabrice Gasnier
2026-11-25 16:32 ` [PATCH v6 11/11] usb: dwc2: drop redundant ll_hw_enabled checks in suspend/resume Fabrice Gasnier
2026-11-25 17:09 ` [PATCH v6 00/11] usb: dwc2: fix low power issues found on stm32mp1 sashiko-bot

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-233788@kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=fabrice.gasnier@foss.st.com \
    --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