Devicetree
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Sebastian Reichel" <sebastian.reichel@collabora.com>
Cc: robh@kernel.org, devicetree@vger.kernel.org, olteanv@gmail.com,
	vkoul@kernel.org, linux-phy@lists.infradead.org,
	neil.armstrong@linaro.org, conor+dt@kernel.org
Subject: Re: [PATCH v14 31/38] usb: dwc3: rockchip: support PHY reset notifications
Date: Fri, 14 Aug 2026 05:43:48 +0000	[thread overview]
Message-ID: <20260814054349.6DFEC1F000E9@smtp.kernel.org> (raw)
In-Reply-To: <20260813-rockchip-usbdp-cleanup-v14-31-b5ad9c68fa11@collabora.com>

Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
- [High] The PHY notification handler unconditionally drops a PM usage reference if `pm_runtime_get_if_active()` returns `-EINVAL`, causing a usage counter underflow and potentially dropping references owned by other parts of the driver.
- [High] The PHY notifier unregistration is deferred to the devres teardown phase, creating a race condition where an asynchronous PHY reset event accesses unclocked and reset DWC3 hardware, causing a kernel panic.
--

--- Patch [31]: [PATCH v14 31/38] usb: dwc3: rockchip: support PHY reset notifications ---
commit f458b4c213899f50f1d40c52f7c5aaeb38fdb675
Author: Sebastian Reichel <sebastian.reichel@collabora.com>

usb: dwc3: rockchip: support PHY reset notifications

On recent Rockchip platforms (at least RK3588 & RK3576), DWC3 IP is used
with a USBDP PHY providing USB3 and DP. This PHY needs to be reset when
the mode changes, which may happen when plugging in different USB-C
devices. This adds handling for the new PHY framework reset notifications.

> diff --git a/drivers/usb/dwc3/dwc3-rockchip.c b/drivers/usb/dwc3/dwc3-rockchip.c
> index 1df33625b69f8..9e91e5f7e53d6 100644
> --- a/drivers/usb/dwc3/dwc3-rockchip.c
> +++ b/drivers/usb/dwc3/dwc3-rockchip.c
[ ... ]
> +static int dwc3_usb3_phy_notify(struct notifier_block *nb,
> +				unsigned long action, void *data)
> +{
[ ... ]
> +	switch (action) {
> +	case PHY_NOTIFY_PRE_RESET:
> +		/*
> +		 * If already suspended, the resume path will reinit GUSB3PIPECTL
> +		 * via dwc3_core_init(). A forced resume is not possible as that
> +		 * would call phy_init() resulting in a deadlock. Due to the
> +		 * phy_init() in the resume path there is also no need to block
> +		 * async RPM resume on our side, since the PHY synchronizes it
> +		 * for us.
> +		 *
> +		 * pm_runtime_get_if_active() returns 0 when suspended (skip),
> +		 * 1 when active (ref held), or -EINVAL when PM is disabled
> +		 * (device always active). In the -EINVAL case PM ref counting
> +		 * is a no-op, so the unconditional put in POST_RESET is safe.
> +		 */
> +		ret = pm_runtime_get_if_active(dwc->dev);
> +		if (!ret)
> +			return NOTIFY_OK;

[Severity: High]
Does this code risk a PM usage counter underflow if PM is disabled?

When pm_runtime_get_if_active() returns -EINVAL, the !ret check evaluates
to false, allowing execution to proceed without incrementing the usage counter.

[ ... ]
> +	case PHY_NOTIFY_POST_RESET:
[ ... ]
> +		/*
> +		 * Deassert PHY soft reset to reconnect the PIPE interface
> +		 * after PHY reinitialization.
> +		 */
> +		reg = dwc3_readl(dwc, DWC3_GUSB3PIPECTL(port));
> +		reg &= ~DWC3_GUSB3PIPECTL_PHYSOFTRST;
> +		dwc3_writel(dwc, DWC3_GUSB3PIPECTL(port), reg);
> +		spin_unlock_irqrestore(&dwc->lock, flags);
> +
> +		pm_runtime_put_autosuspend(dwc->dev);

[Severity: High]
Is it safe to unconditionally call pm_runtime_put_autosuspend() here?

The comment in PHY_NOTIFY_PRE_RESET states that PM ref counting is a no-op
in the -EINVAL case, but pm_runtime_put_autosuspend() will decrement the
usage counter regardless. This could cause an unbalanced put and drop
references owned by other parts of the driver.

> +		break;
> +	}
> +
> +	return NOTIFY_OK;
> +}
> +
> +static void dwc3_rk_phy_unregister_notifiers(void *data)
> +{
> +	struct dwc3_rockchip *dwc_rk = data;
> +	struct dwc3 *dwc = &dwc_rk->dwc;
> +	int i;
> +
> +	for (i = 0; i < dwc->num_usb3_ports; i++)
> +		phy_unregister_notifier(dwc->usb3_generic_phy[i],
> +					&dwc_rk->usb3_phy_nb[i].nb);
> +
> +	/* Release any PM references from in-flight resets */
> +	for (i = 0; i < dwc->num_usb3_ports; i++) {
> +		if (dwc_rk->phy_reset_active & BIT(i))
> +			pm_runtime_put_autosuspend(dwc->dev);

[Severity: High]
Can this also cause a PM usage counter underflow?

If an in-flight reset was initiated while pm_runtime_get_if_active()
returned -EINVAL, we didn't acquire a reference. Calling
pm_runtime_put_autosuspend() here would result in the same unbalanced put.

> +	}
> +	dwc_rk->phy_reset_active = 0;
> +}
> +
> +static int dwc3_rk_phy_register_notifiers(struct dwc3 *dwc)
> +{
> +	struct dwc3_rockchip *dwc_rk = container_of(dwc, struct dwc3_rockchip, dwc);
> +	int i;
> +
> +	for (i = 0; i < dwc->num_usb3_ports; i++) {
> +		dwc_rk->usb3_phy_nb[i].nb.notifier_call = dwc3_usb3_phy_notify;
> +		dwc_rk->usb3_phy_nb[i].dwc_rk = dwc_rk;
> +		dwc_rk->usb3_phy_nb[i].port_index = i;
> +		phy_register_notifier(dwc->usb3_generic_phy[i],
> +				      &dwc_rk->usb3_phy_nb[i].nb);
> +	}
> +
> +	return devm_add_action_or_reset(dwc->dev, dwc3_rk_phy_unregister_notifiers, dwc_rk);

[Severity: High]
Can deferring notifier unregistration to the devres phase cause a kernel
panic during driver unbind?

When the driver is unbound, the DWC3 core is typically shut down (clocks
disabled and hardware reset asserted) before the devres teardown phase runs.

If an external PHY reset event arrives in this window, dwc3_usb3_phy_notify()
would execute and attempt to read DWC3_GUSB3PIPECTL from unclocked and reset
hardware, potentially causing a Synchronous External Abort.

> +}

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20260813-rockchip-usbdp-cleanup-v14-0-b5ad9c68fa11@collabora.com?part=31

  reply	other threads:[~2026-08-14  5:43 UTC|newest]

Thread overview: 57+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-08-13 20:51 [PATCH v14 00/38] phy: rockchip: usbdp: Clean up the mess Sebastian Reichel
2026-08-13 20:51 ` [PATCH v14 01/38] dt-bindings: phy: rockchip-usbdp: add improved ports scheme Sebastian Reichel
2026-08-13 20:51 ` [PATCH v14 02/38] phy: rockchip: usbdp: Update mode_change after error handling Sebastian Reichel
2026-08-14  1:19   ` sashiko-bot
2026-08-13 20:51 ` [PATCH v14 03/38] phy: rockchip: usbdp: Do not lose USB3 PHY status Sebastian Reichel
2026-08-14  1:33   ` sashiko-bot
2026-08-13 20:51 ` [PATCH v14 04/38] phy: rockchip: usbdp: Fix devm_clk_bulk_get_all check Sebastian Reichel
2026-08-14  1:41   ` sashiko-bot
2026-08-13 20:51 ` [PATCH v14 05/38] phy: rockchip: usbdp: Handle missing clock-names DT property gracefully Sebastian Reichel
2026-08-14  1:53   ` sashiko-bot
2026-08-13 20:51 ` [PATCH v14 06/38] phy: rockchip: usbdp: Drop seamless DP takeover Sebastian Reichel
2026-08-14  2:06   ` sashiko-bot
2026-08-13 20:51 ` [PATCH v14 07/38] phy: rockchip: usbdp: Keep clocks running on PHY re-init Sebastian Reichel
2026-08-14  2:16   ` sashiko-bot
2026-08-13 20:51 ` [PATCH v14 08/38] phy: rockchip: usbdp: Amend SSC modulation deviation Sebastian Reichel
2026-08-13 20:51 ` [PATCH v14 09/38] phy: rockchip: usbdp: Fix LFPS detect threshold control Sebastian Reichel
2026-08-13 20:51 ` [PATCH v14 10/38] phy: rockchip: usbdp: Add missing mode_change update Sebastian Reichel
2026-08-14  2:41   ` sashiko-bot
2026-08-13 20:51 ` [PATCH v14 11/38] phy: rockchip: usbdp: Support single-lane DP Sebastian Reichel
2026-08-14  2:55   ` sashiko-bot
2026-08-13 20:51 ` [PATCH v14 12/38] phy: rockchip: usbdp: Limit DP lane count to muxed lanes Sebastian Reichel
2026-08-14  3:07   ` sashiko-bot
2026-08-13 20:51 ` [PATCH v14 13/38] phy: rockchip: usbdp: Rename DP lane functions Sebastian Reichel
2026-08-13 20:51 ` [PATCH v14 14/38] phy: rockchip: usbdp: Use FIELD_PREP_WM16_CONST Sebastian Reichel
2026-08-13 20:51 ` [PATCH v14 15/38] phy: rockchip: usbdp: Cleanup DP lane selection function Sebastian Reichel
2026-08-13 20:51 ` [PATCH v14 16/38] phy: rockchip: usbdp: Register DP aux bridge Sebastian Reichel
2026-08-13 20:52 ` [PATCH v14 17/38] phy: rockchip: usbdp: Drop DP HPD handling Sebastian Reichel
2026-08-13 20:52 ` [PATCH v14 18/38] phy: rockchip: usbdp: Rename mode_change to phy_needs_reinit Sebastian Reichel
2026-08-13 20:52 ` [PATCH v14 19/38] phy: rockchip: usbdp: Re-init the PHY on orientation change Sebastian Reichel
2026-08-14  3:57   ` sashiko-bot
2026-08-13 20:52 ` [PATCH v14 20/38] phy: rockchip: usbdp: Factor out lane_mux_sel setup Sebastian Reichel
2026-08-14  4:10   ` sashiko-bot
2026-08-13 20:52 ` [PATCH v14 21/38] phy: rockchip: usbdp: Properly handle TYPEC_STATE_SAFE and TYPEC_STATE_USB Sebastian Reichel
2026-08-14  4:23   ` sashiko-bot
2026-08-13 20:52 ` [PATCH v14 22/38] phy: rockchip: usbdp: Use guard functions for mutex Sebastian Reichel
2026-08-13 20:52 ` [PATCH v14 23/38] phy: rockchip: usbdp: Hold mutex in DP PHY configure Sebastian Reichel
2026-08-13 20:52 ` [PATCH v14 24/38] phy: rockchip: usbdp: Add some extra debug messages Sebastian Reichel
2026-08-13 20:52 ` [PATCH v14 25/38] phy: rockchip: usbdp: Avoid xHCI SErrors Sebastian Reichel
2026-08-14  4:52   ` sashiko-bot
2026-08-13 20:52 ` [PATCH v14 26/38] phy: rockchip: usbdp: Handle rk_udphy_reset_deassert errors Sebastian Reichel
2026-08-13 20:52 ` [PATCH v14 27/38] phy: rockchip: usbdp: Only enable USB3 when not in high-speed mode Sebastian Reichel
2026-08-13 20:52 ` [PATCH v14 28/38] phy: core: add notifier infrastructure Sebastian Reichel
2026-08-14  5:14   ` sashiko-bot
2026-08-13 20:52 ` [PATCH v14 29/38] usb: dwc3: rockchip: introduce glue driver Sebastian Reichel
2026-08-14  5:22   ` sashiko-bot
2026-08-13 20:52 ` [PATCH v14 30/38] usb: dwc3: core: add post PHY registration hook for platform glue Sebastian Reichel
2026-08-13 20:52 ` [PATCH v14 31/38] usb: dwc3: rockchip: support PHY reset notifications Sebastian Reichel
2026-08-14  5:43   ` sashiko-bot [this message]
2026-08-13 20:52 ` [PATCH v14 32/38] usb: dwc3: rockchip: fix USB-C reconnect in gadget mode Sebastian Reichel
2026-08-13 20:52 ` [PATCH v14 33/38] phy: rockchip: usbdp: Add phy reset notification support Sebastian Reichel
2026-08-14  6:12   ` sashiko-bot
2026-08-13 20:52 ` [PATCH v14 34/38] phy: rockchip: usbdp: Drop -EPROBE_DEFER hack Sebastian Reichel
2026-08-13 20:52 ` [PATCH v14 35/38] phy: rockchip: usbdp: Rename mode to hw_mode Sebastian Reichel
2026-08-13 20:52 ` [PATCH v14 36/38] phy: rockchip: usbdp: Fix power state handling Sebastian Reichel
2026-08-13 20:52 ` [PATCH v14 37/38] phy: rockchip: usbdp: Re-init PHY on mux change Sebastian Reichel
2026-08-14  6:49   ` sashiko-bot
2026-08-13 20:52 ` [PATCH v14 38/38] phy: rockchip: usbdp: Add USB-C state without DP enabled Sebastian Reichel

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=20260814054349.6DFEC1F000E9@smtp.kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=conor+dt@kernel.org \
    --cc=devicetree@vger.kernel.org \
    --cc=linux-phy@lists.infradead.org \
    --cc=neil.armstrong@linaro.org \
    --cc=olteanv@gmail.com \
    --cc=robh@kernel.org \
    --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