* [PATCH] PM: core: Do not wait for a device's own completion
@ 2026-12-01 7:29 Ziyu Zhang
2026-12-01 7:37 ` sashiko-bot
0 siblings, 1 reply; 3+ messages in thread
From: Ziyu Zhang @ 2026-12-01 7:29 UTC (permalink / raw)
To: rafael
Cc: pavel, lenb, gregkh, dakr, johan, linux-pm, driver-core,
linux-usb, linux-kernel
device_pm_wait_for_dev() waits on the completion of @dev when the
subordinate has async suspend enabled. That completion is signaled only
after the PM callback of @dev returns, and dpm_resume() reinitializes it
when the phase starts, so passing the same device twice waits for a
callback that is still running:
resume of D device_pm_wait_for_dev(D, D)
---------- -----------------------------
device_resume(D)
device_lock(D)
resume callback
dpm_wait(D, async = true)
wait_for_completion(
&D->power.completion)
/* returns only here */
device_unlock(D)
complete_all(&D->power.completion)
The wait has no timeout and device_resume() holds D->mutex across the
callback, so the resume never finishes and D stays locked for the rest
of the transition.
ehci_platform is the only caller in the tree that can build this pair.
ehci_platform_resume() takes the companion of its controller from the
device tree and passes it to device_pm_wait_for_dev() together with the
controller. A "companion" phandle that points back at its own node
makes both arguments the same device: of_parse_phandle() returns the
node and of_find_device_by_node() returns the platform device that is
being resumed.
Return without waiting when both arguments are the same device. A
device cannot depend on a completion that is signaled only after the
callback it is running returns, so there is nothing to wait for. The
other two callers, in usb/core, always pass two different devices.
If two EHCI controllers name each other as companions, their resume
callbacks can still wait for each other's completion because the device
pointers differ. One option for ehci-platform would be to skip the
wait when the companion is another EHCI controller that points back to
the first. This would preserve the existing resume ordering between
EHCI and OHCI companions.
Signed-off-by: Ziyu Zhang <ziyuzhang201@gmail.com>
---
drivers/base/power/main.c | 6 ++++++
1 file changed, 6 insertions(+)
diff --git a/drivers/base/power/main.c b/drivers/base/power/main.c
index 88a31e09e4f3..16506c1fac8b 100644
--- a/drivers/base/power/main.c
+++ b/drivers/base/power/main.c
@@ -2401,9 +2401,15 @@ EXPORT_SYMBOL_GPL(__suspend_report_result);
* device_pm_wait_for_dev - Wait for suspend/resume of a device to complete.
* @subordinate: Device that needs to wait for @dev.
* @dev: Device to wait for.
+ *
+ * A device that resolves to itself is not waited for: its completion is
+ * only signaled after the PM callback that is running right now returns.
*/
int device_pm_wait_for_dev(struct device *subordinate, struct device *dev)
{
+ if (subordinate == dev)
+ return async_error;
+
dpm_wait(dev, subordinate->power.async_suspend);
return async_error;
}
--
2.34.1
^ permalink raw reply related [flat|nested] 3+ messages in thread* Re: [PATCH] PM: core: Do not wait for a device's own completion
2026-12-01 7:29 [PATCH] PM: core: Do not wait for a device's own completion Ziyu Zhang
@ 2026-12-01 7:37 ` sashiko-bot
2026-12-01 8:28 ` ziyu zhang
0 siblings, 1 reply; 3+ messages in thread
From: sashiko-bot @ 2026-12-01 7:37 UTC (permalink / raw)
To: Ziyu Zhang; +Cc: linux-usb
Thank you for your contribution! Sashiko AI review found 2 potential
issues to consider:
- [Medium] PM: core: device_pm_wait_for_dev() papers over self-companion
DT bug
- [Low] PM: core: missing Fixes trailer for device_pm_wait_for_dev()
deadlock fix
--
commit 5ff73b0e5039a3b4e0f21b35ef7f4e433f37c17b
Author: Ziyu Zhang <ziyuzhang201@gmail.com>
> PM: core: Do not wait for a device's own completion
>
> device_pm_wait_for_dev() waits on the completion of @dev when the
> subordinate has async suspend enabled. That completion is signaled only
> after the PM callback of @dev returns, and dpm_resume() reinitializes it
> when the phase starts, so passing the same device twice waits for a
> callback that is still running:
>
> resume of D device_pm_wait_for_dev(D, D)
> ---------- -----------------------------
> device_resume(D)
> device_lock(D)
> resume callback
> dpm_wait(D, async = true)
> wait_for_completion(
> &D->power.completion)
> /* returns only here */
> device_unlock(D)
> complete_all(&D->power.completion)
>
> The wait has no timeout and device_resume() holds D->mutex across the
> callback, so the resume never finishes and D stays locked for the rest
> of the transition.
>
> ehci_platform is the only caller in the tree that can build this pair.
> ehci_platform_resume() takes the companion of its controller from the
> device tree and passes it to device_pm_wait_for_dev() together with the
> controller. A "companion" phandle that points back at its own node
> makes both arguments the same device: of_parse_phandle() returns the
> node and of_find_device_by_node() returns the platform device that is
> being resumed.
>
> Return without waiting when both arguments are the same device. A
> device cannot depend on a completion that is signaled only after the
> callback it is running returns, so there is nothing to wait for. The
> other two callers, in usb/core, always pass two different devices.
>
> If two EHCI controllers name each other as companions, their resume
> callbacks can still wait for each other's completion because the device
> pointers differ. One option for ehci-platform would be to skip the
> wait when the companion is another EHCI controller that points back to
> the first. This would preserve the existing resume ordering between
> EHCI and OHCI companions.
>
> Signed-off-by: Ziyu Zhang <ziyuzhang201@gmail.com>
[Severity: Low]
Since this fixes a deadlock in device_pm_wait_for_dev(), should it carry a
Fixes tag such as:
Fixes: f8824cee405c ("PM: Allow device drivers to use dpm_wait()")
or:
Fixes: d4d75128b8fd ("usb: host: ehci-platform: fix usb 1.1 device is not connected in system resume")
> diff --git a/drivers/base/power/main.c b/drivers/base/power/main.c
> index 88a31e09e4f3f..16506c1fac8bc 100644
> --- a/drivers/base/power/main.c
> +++ b/drivers/base/power/main.c
> @@ -2401,9 +2401,15 @@ EXPORT_SYMBOL_GPL(__suspend_report_result);
> * device_pm_wait_for_dev - Wait for suspend/resume of a device to complete.
> * @subordinate: Device that needs to wait for @dev.
> * @dev: Device to wait for.
> + *
> + * A device that resolves to itself is not waited for: its completion is
> + * only signaled after the PM callback that is running right now returns.
> */
> int device_pm_wait_for_dev(struct device *subordinate, struct device *dev)
> {
> + if (subordinate == dev)
> + return async_error;
> +
> dpm_wait(dev, subordinate->power.async_suspend);
> return async_error;
> }
[Severity: Medium]
Should a self-referential "companion" phandle be rejected in
usb_of_get_companion_dev() instead of being silently ignored in
device_pm_wait_for_dev()?
A device cannot be its own companion, and leaving usb_of_get_companion_dev()
to return the caller's own device when node == dev->of_node leaves other
consumers broken for the same malformed device tree. For example, in
ehci-platform.c, quirk_poll_rebind_companion() unbinds companion_dev from
inside quirk_poll_work():
drivers/usb/host/ehci-platform.c:quirk_poll_rebind_companion() {
...
companion_dev = usb_of_get_companion_dev(hcd->self.controller);
if (!companion_dev)
return;
device_release_driver(companion_dev);
if (device_attach(companion_dev) < 0)
ehci_err(ehci, "%s: failed\n", __func__);
...
}
When companion_dev is hcd->self.controller, device_release_driver() runs
ehci_platform_remove() (which only does a non-synchronous
cancel_delayed_work(&priv->poll_work)) and frees hcd and ehci while
quirk_poll_rebind_companion() is still running, leading to a use-after-free.
Similarly, renesas_usb3_role_work() calls device_release_driver() on the
companion stored in usb3->host_dev, which calls renesas_usb3_remove() on
itself and deadlocks in cancel_work_sync(&usb3->role_work).
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261201072944.73205-1-ziyuzhang201@gmail.com?part=1
^ permalink raw reply [flat|nested] 3+ messages in thread* Re: [PATCH] PM: core: Do not wait for a device's own completion
2026-12-01 7:37 ` sashiko-bot
@ 2026-12-01 8:28 ` ziyu zhang
0 siblings, 0 replies; 3+ messages in thread
From: ziyu zhang @ 2026-12-01 8:28 UTC (permalink / raw)
To: sashiko-reviews; +Cc: linux-usb
On Tue, 1 Dec 2026 at 15:37, <sashiko-bot@kernel.org> wrote:
>
> Thank you for your contribution! Sashiko AI review found 2 potential
> issues to consider:
> - [Medium] PM: core: device_pm_wait_for_dev() papers over self-companion
> DT bug
> - [Low] PM: core: missing Fixes trailer for device_pm_wait_for_dev()
> deadlock fix
> --
>
> commit 5ff73b0e5039a3b4e0f21b35ef7f4e433f37c17b
> Author: Ziyu Zhang <ziyuzhang201@gmail.com>
>
> > PM: core: Do not wait for a device's own completion
> >
> > device_pm_wait_for_dev() waits on the completion of @dev when the
> > subordinate has async suspend enabled. That completion is signaled only
> > after the PM callback of @dev returns, and dpm_resume() reinitializes it
> > when the phase starts, so passing the same device twice waits for a
> > callback that is still running:
> >
> > resume of D device_pm_wait_for_dev(D, D)
> > ---------- -----------------------------
> > device_resume(D)
> > device_lock(D)
> > resume callback
> > dpm_wait(D, async = true)
> > wait_for_completion(
> > &D->power.completion)
> > /* returns only here */
> > device_unlock(D)
> > complete_all(&D->power.completion)
> >
> > The wait has no timeout and device_resume() holds D->mutex across the
> > callback, so the resume never finishes and D stays locked for the rest
> > of the transition.
> >
> > ehci_platform is the only caller in the tree that can build this pair.
> > ehci_platform_resume() takes the companion of its controller from the
> > device tree and passes it to device_pm_wait_for_dev() together with the
> > controller. A "companion" phandle that points back at its own node
> > makes both arguments the same device: of_parse_phandle() returns the
> > node and of_find_device_by_node() returns the platform device that is
> > being resumed.
> >
> > Return without waiting when both arguments are the same device. A
> > device cannot depend on a completion that is signaled only after the
> > callback it is running returns, so there is nothing to wait for. The
> > other two callers, in usb/core, always pass two different devices.
> >
> > If two EHCI controllers name each other as companions, their resume
> > callbacks can still wait for each other's completion because the device
> > pointers differ. One option for ehci-platform would be to skip the
> > wait when the companion is another EHCI controller that points back to
> > the first. This would preserve the existing resume ordering between
> > EHCI and OHCI companions.
> >
> > Signed-off-by: Ziyu Zhang <ziyuzhang201@gmail.com>
>
> [Severity: Low]
> Since this fixes a deadlock in device_pm_wait_for_dev(), should it carry a
> Fixes tag such as:
>
> Fixes: f8824cee405c ("PM: Allow device drivers to use dpm_wait()")
>
> or:
>
> Fixes: d4d75128b8fd ("usb: host: ehci-platform: fix usb 1.1 device is not connected in system resume")
>
No in-tree DT describes a companion phandle pointing at its own node, and
the helper has never supported that case, so this is a guard rather than a
repair of a specific commit's change. I'd rather not attach a Fixes tag.
> > diff --git a/drivers/base/power/main.c b/drivers/base/power/main.c
> > index 88a31e09e4f3f..16506c1fac8bc 100644
> > --- a/drivers/base/power/main.c
> > +++ b/drivers/base/power/main.c
> > @@ -2401,9 +2401,15 @@ EXPORT_SYMBOL_GPL(__suspend_report_result);
> > * device_pm_wait_for_dev - Wait for suspend/resume of a device to complete.
> > * @subordinate: Device that needs to wait for @dev.
> > * @dev: Device to wait for.
> > + *
> > + * A device that resolves to itself is not waited for: its completion is
> > + * only signaled after the PM callback that is running right now returns.
> > */
> > int device_pm_wait_for_dev(struct device *subordinate, struct device *dev)
> > {
> > + if (subordinate == dev)
> > + return async_error;
> > +
> > dpm_wait(dev, subordinate->power.async_suspend);
> > return async_error;
> > }
>
> [Severity: Medium]
> Should a self-referential "companion" phandle be rejected in
> usb_of_get_companion_dev() instead of being silently ignored in
> device_pm_wait_for_dev()?
>
> A device cannot be its own companion, and leaving usb_of_get_companion_dev()
> to return the caller's own device when node == dev->of_node leaves other
> consumers broken for the same malformed device tree. For example, in
> ehci-platform.c, quirk_poll_rebind_companion() unbinds companion_dev from
> inside quirk_poll_work():
>
> drivers/usb/host/ehci-platform.c:quirk_poll_rebind_companion() {
> ...
> companion_dev = usb_of_get_companion_dev(hcd->self.controller);
> if (!companion_dev)
> return;
>
> device_release_driver(companion_dev);
> if (device_attach(companion_dev) < 0)
> ehci_err(ehci, "%s: failed\n", __func__);
> ...
> }
>
> When companion_dev is hcd->self.controller, device_release_driver() runs
> ehci_platform_remove() (which only does a non-synchronous
> cancel_delayed_work(&priv->poll_work)) and frees hcd and ehci while
> quirk_poll_rebind_companion() is still running, leading to a use-after-free.
> Similarly, renesas_usb3_role_work() calls device_release_driver() on the
> companion stored in usb3->host_dev, which calls renesas_usb3_remove() on
> itself and deadlocks in cancel_work_sync(&usb3->role_work).
>
Agreed. The value is produced there, and ehci-platform's rebind quirk and
renesas_usb3 hit the same phandle with a worse outcome (unbinding their own
driver). I'll move the check into usb_of_get_companion_dev() and repost;
please drop this version.
> --
> Sashiko AI review · https://sashiko.dev/#/patchset/20261201072944.73205-1-ziyuzhang201@gmail.com?part=1
^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2026-12-01 8:28 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-12-01 7:29 [PATCH] PM: core: Do not wait for a device's own completion Ziyu Zhang
2026-12-01 7:37 ` sashiko-bot
2026-12-01 8:28 ` ziyu zhang
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.