* [PATCH v3] usb: cdns3: fix NULL dereference in cdns_host_resume()
@ 2026-11-23 4:43 Guomin chen
2026-11-23 4:56 ` sashiko-bot
2026-11-23 7:51 ` [PATCH v4] " Guomin chen
0 siblings, 2 replies; 5+ messages in thread
From: Guomin chen @ 2026-11-23 4:43 UTC (permalink / raw)
To: Peter Chen, Pawel Laszczak, Greg Kroah-Hartman
Cc: Roger Quadros, linux-usb, linux-kernel, Guoming Chen
From: Guoming Chen <chenguoming88s@gmail.com>
cdns_host_resume() assumes that the xhci-hcd child device is bound and
passes platform_get_drvdata() straight to hcd_to_xhci_priv(). That is
not guaranteed: __cdns_host_init() creates the child with
platform_device_add(), but the child is only probed once the xhci-plat
driver is registered. cdns3/ is linked before host/ in
drivers/usb/Makefile and both drivers are device_initcall(), so on the
built-in path the child is registered before xhci-plat's initcall runs;
when xhci-plat is a module, the child stays unbound until that module is
loaded. Platforms that set CDNS3_DEFAULT_PM_RUNTIME_ALLOW (cdns3-imx
is the only in-tree user) let the controller autosuspend (20 ms), so
the parent may already be runtime-suspended by the time the child's
probe actually runs.
When the child is finally probed, the driver core resumes its parent
before calling really_probe(): __driver_probe_device() does
pm_runtime_get_sync(dev->parent). That runs cdns_resume() ->
cdns_host_resume() while platform_get_drvdata() still returns NULL,
because the drvdata is only set later, by dev_set_drvdata() in
__usb_create_hcd() called from xhci_plat_probe(). hcd_to_xhci_priv(NULL)
then evaluates usb_hcd_is_primary_hcd(NULL), which dereferences
NULL + offsetof(struct usb_hcd, primary_hcd) (the offset depends on the
configuration). The oops faults in the first instruction of that
helper:
pc : usb_hcd_is_primary_hcd+0x0/0x1c
lr : cdns_host_resume+0x24/0x44
Commit 87e4b043b98a ("usb: cdns3: fix role switching during resume")
only skips the resume callback when the role changes during resume. The
role does not change here: the parent is resumed by the driver core
while the child is being probed, so that fix does not apply.
Return early when the host child is not available: it has either not
been probed yet, or cdns->host_dev is NULL because starting the host
role failed after cdns_role_start() had already set cdns->role. Nothing
is lost in either case: a child that is not bound yet has no state to
restore, and xhci_plat_priv.power_lost is only consumed when an already
initialized host resumes. cdns_wakeup_irq() already checks
cdns->host_dev the same way.
Fixes: 3a85c1011540 ("usb: host: cdns3: forward lost power information to xhci")
Signed-off-by: Guoming Chen <chenguoming88s@gmail.com>
---
Changes in v3:
- Add the changelog below the tear line, as requested after v2.
- Note that commit 87e4b043b98a covers only the role-switch variant,
and correct the description of when cdns->host_dev can be NULL in
both the commit message and the code comment. Clarify a few other
statements in the commit message. No functional changes.
Changes in v2:
- Resend with the correct sender address; the patch is unchanged from
v1.
---
drivers/usb/cdns3/host.c | 20 +++++++++++++++++---
1 file changed, 17 insertions(+), 3 deletions(-)
diff --git a/drivers/usb/cdns3/host.c b/drivers/usb/cdns3/host.c
index e01264dc625d..2f430e1e0089 100644
--- a/drivers/usb/cdns3/host.c
+++ b/drivers/usb/cdns3/host.c
@@ -140,9 +140,23 @@ static void cdns_host_exit(struct cdns *cdns)
static int cdns_host_resume(struct cdns *cdns, bool power_lost)
{
- struct usb_hcd *hcd = platform_get_drvdata(cdns->host_dev);
- struct xhci_plat_priv *priv = hcd_to_xhci_priv(hcd);
-
+ struct usb_hcd *hcd;
+ struct xhci_plat_priv *priv;
+
+ /*
+ * The xhci-hcd child may not be bound yet: cdns3 creates it, but
+ * xhci-plat probes it only later, and the drvdata is set in that
+ * probe. cdns->host_dev itself can be NULL, e.g. when the host
+ * role failed to start.
+ */
+ if (!cdns->host_dev)
+ return 0;
+
+ hcd = platform_get_drvdata(cdns->host_dev);
+ if (!hcd)
+ return 0;
+
+ priv = hcd_to_xhci_priv(hcd);
priv->power_lost = power_lost;
return 0;
--
2.34.1
^ permalink raw reply related [flat|nested] 5+ messages in thread* Re: [PATCH v3] usb: cdns3: fix NULL dereference in cdns_host_resume()
2026-11-23 4:43 [PATCH v3] usb: cdns3: fix NULL dereference in cdns_host_resume() Guomin chen
@ 2026-11-23 4:56 ` sashiko-bot
2026-11-23 7:51 ` [PATCH v4] " Guomin chen
1 sibling, 0 replies; 5+ messages in thread
From: sashiko-bot @ 2026-11-23 4:56 UTC (permalink / raw)
To: Guomin chen; +Cc: linux-usb
Thank you for your contribution! Sashiko AI review found 1 potential
issue to consider:
- [Medium] usb: cdns3: dangling cdns->host_dev on __cdns_host_init()
error paths
--
commit 65a7dd0ee7f8dab7d9774ef07f61af4db003e801
Author: Guoming Chen <chenguoming88s@gmail.com>
usb: cdns3: fix NULL dereference in cdns_host_resume()
This commit adds NULL checks for cdns->host_dev and its driver data in
cdns_host_resume() to avoid a NULL pointer dereference when the parent
resumes before the xhci-hcd child device has been probed or after host
role startup fails.
> diff --git a/drivers/usb/cdns3/host.c b/drivers/usb/cdns3/host.c
> index e01264dc625da..2f430e1e00897 100644
> --- a/drivers/usb/cdns3/host.c
> +++ b/drivers/usb/cdns3/host.c
> @@ -140,9 +140,23 @@ static void cdns_host_exit(struct cdns *cdns)
>
> static int cdns_host_resume(struct cdns *cdns, bool power_lost)
> {
> - struct usb_hcd *hcd = platform_get_drvdata(cdns->host_dev);
> - struct xhci_plat_priv *priv = hcd_to_xhci_priv(hcd);
> -
> + struct usb_hcd *hcd;
> + struct xhci_plat_priv *priv;
> +
> + /*
> + * The xhci-hcd child may not be bound yet: cdns3 creates it, but
> + * xhci-plat probes it only later, and the drvdata is set in that
> + * probe. cdns->host_dev itself can be NULL, e.g. when the host
> + * role failed to start.
> + */
> + if (!cdns->host_dev)
> + return 0;
> +
> + hcd = platform_get_drvdata(cdns->host_dev);
> + if (!hcd)
> + return 0;
[Severity: Medium]
Is cdns->host_dev guaranteed to be NULL when the host role fails to start?
__cdns_host_init() assigns cdns->host_dev immediately after
platform_device_alloc() and does not clear it on the subsequent error
paths:
drivers/usb/cdns3/host.c:__cdns_host_init() {
...
xhci->dev.parent = cdns->dev;
cdns->host_dev = xhci;
ret = platform_device_add_resources(xhci, cdns->xhci_res,
CDNS_XHCI_RESOURCES_NUM);
if (ret) {
dev_err(cdns->dev, "couldn't add resources to xHCI device\n");
goto err1;
}
...
free_memory:
kfree(cdns->xhci_plat_data);
err1:
platform_device_put(xhci);
return ret;
}
If platform_device_add_resources(), kmemdup(), platform_device_add_data()
or platform_device_add() fails after cdns_role_start() has set
cdns->role = USB_ROLE_HOST, platform_device_put(xhci) frees the device
while cdns->host_dev remains non-NULL. A subsequent cdns_host_resume()
will bypass the !cdns->host_dev check and pass the dangling pointer to
platform_get_drvdata(cdns->host_dev), causing a use-after-free.
Assigning cdns->host_dev only after platform_device_add() succeeds (or
clearing cdns->host_dev = NULL under err1) would ensure the new NULL check
catches all host-init failures.
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261123044347.3273-1-chenguoming88s@gmail.com?part=1
^ permalink raw reply [flat|nested] 5+ messages in thread* [PATCH v4] usb: cdns3: fix NULL dereference in cdns_host_resume()
2026-11-23 4:43 [PATCH v3] usb: cdns3: fix NULL dereference in cdns_host_resume() Guomin chen
2026-11-23 4:56 ` sashiko-bot
@ 2026-11-23 7:51 ` Guomin chen
2026-11-23 8:05 ` sashiko-bot
2026-11-23 11:07 ` Théo Lebrun
1 sibling, 2 replies; 5+ messages in thread
From: Guomin chen @ 2026-11-23 7:51 UTC (permalink / raw)
To: Peter Chen, Pawel Laszczak, Greg Kroah-Hartman
Cc: Roger Quadros, Théo Lebrun, linux-usb, linux-kernel, stable,
Guoming Chen
From: Guoming Chen <chenguoming88s@gmail.com>
cdns_host_resume() assumes that the xhci-hcd child is bound and passes
platform_get_drvdata() to hcd_to_xhci_priv() unchecked. The child is
created by __cdns_host_init(), but xhci-plat probes it only when it
registers; cdns3/ precedes host/ in drivers/usb/Makefile, so with both
built in the child is registered before xhci-plat's initcall runs, and
with xhci-plat as a module it stays unbound until the module loads.
Platforms that allow runtime PM (cdns3-imx is the only in-tree user)
autosuspend the controller after 20 ms, so the parent may already be
suspended when the child is probed.
Resuming the parent, either for the child's probe
(__driver_probe_device() calls pm_runtime_get_sync(dev->parent)) or on
a system-wide resume, runs cdns_host_resume() while the drvdata is
still NULL; xhci_plat_probe() sets it later via __usb_create_hcd().
hcd_to_xhci_priv(NULL) then dereferences NULL + offsetof(struct
usb_hcd, primary_hcd):
pc : usb_hcd_is_primary_hcd+0x0/0x1c
lr : cdns_host_resume+0x24/0x44
Commit 87e4b043b98a ("usb: cdns3: fix role switching during resume")
only skips the callback when the role changes during resume, which is
not the case here.
Return early when there is no hcd to notify: the host role can fail to
start after cdns_role_start() set cdns->role, or the child may not be
probed yet. An unbound child has no state to restore, power_lost is
only consumed by an initialized host, and cdns_wakeup_irq() already
checks cdns->host_dev the same way. Also publish cdns->host_dev only
after platform_device_add() succeeds: the error paths free the child
with platform_device_put() while the pointer stays set, and the next
resume would dereference freed memory instead of bailing out.
Fixes: 3a85c1011540 ("usb: host: cdns3: forward lost power information to xhci")
Cc: stable@vger.kernel.org
Signed-off-by: Guoming Chen <chenguoming88s@gmail.com>
---
Changes in v4:
- Assign cdns->host_dev only after platform_device_add() succeeds, as
reported by Sashiko; otherwise the error paths leave a dangling
pointer.
- Cc stable@vger.kernel.org, mention the system-wide resume path, and
shorten the commit message.
Changes in v3:
- Add the changelog below the tear line.
- Clarify that 87e4b043b98a covers only the role-switch variant, and
correct the description of when cdns->host_dev can be NULL. No
functional changes.
Changes in v2:
- Resend with the correct sender address; the patch is unchanged from
v1.
---
drivers/usb/cdns3/host.c | 28 ++++++++++++++++++++++++----
1 file changed, 24 insertions(+), 4 deletions(-)
diff --git a/drivers/usb/cdns3/host.c b/drivers/usb/cdns3/host.c
index e01264dc625d..b0269c21e339 100644
--- a/drivers/usb/cdns3/host.c
+++ b/drivers/usb/cdns3/host.c
@@ -81,7 +81,6 @@ static int __cdns_host_init(struct cdns *cdns)
}
xhci->dev.parent = cdns->dev;
- cdns->host_dev = xhci;
ret = platform_device_add_resources(xhci, cdns->xhci_res,
CDNS_XHCI_RESOURCES_NUM);
@@ -116,6 +115,13 @@ static int __cdns_host_init(struct cdns *cdns)
goto free_memory;
}
+ /*
+ * Publish the child only once it has been added: the error paths
+ * above free it, and a stale cdns->host_dev would be dereferenced
+ * by the next resume.
+ */
+ cdns->host_dev = xhci;
+
/* Glue needs to access xHCI region register for Power management */
hcd = platform_get_drvdata(xhci);
if (hcd)
@@ -140,9 +146,23 @@ static void cdns_host_exit(struct cdns *cdns)
static int cdns_host_resume(struct cdns *cdns, bool power_lost)
{
- struct usb_hcd *hcd = platform_get_drvdata(cdns->host_dev);
- struct xhci_plat_priv *priv = hcd_to_xhci_priv(hcd);
-
+ struct usb_hcd *hcd;
+ struct xhci_plat_priv *priv;
+
+ /*
+ * The xhci-hcd child may not be bound yet: cdns3 creates it, but
+ * xhci-plat probes it only later, and the drvdata is set in that
+ * probe. cdns->host_dev itself can be NULL, e.g. when the host
+ * role failed to start.
+ */
+ if (!cdns->host_dev)
+ return 0;
+
+ hcd = platform_get_drvdata(cdns->host_dev);
+ if (!hcd)
+ return 0;
+
+ priv = hcd_to_xhci_priv(hcd);
priv->power_lost = power_lost;
return 0;
--
2.34.1
^ permalink raw reply related [flat|nested] 5+ messages in thread* Re: [PATCH v4] usb: cdns3: fix NULL dereference in cdns_host_resume()
2026-11-23 7:51 ` [PATCH v4] " Guomin chen
@ 2026-11-23 8:05 ` sashiko-bot
2026-11-23 11:07 ` Théo Lebrun
1 sibling, 0 replies; 5+ messages in thread
From: sashiko-bot @ 2026-11-23 8:05 UTC (permalink / raw)
To: Guomin chen; +Cc: linux-usb
> From: Guoming Chen <chenguoming88s@gmail.com>
>
> cdns_host_resume() assumes that the xhci-hcd child is bound and passes
> platform_get_drvdata() to hcd_to_xhci_priv() unchecked. The child is
> created by __cdns_host_init(), but xhci-plat probes it only when it
> registers; cdns3/ precedes host/ in drivers/usb/Makefile, so with both
> built in the child is registered before xhci-plat's initcall runs, and
> with xhci-plat as a module it stays unbound until the module loads.
> Platforms that allow runtime PM (cdns3-imx is the only in-tree user)
> autosuspend the controller after 20 ms, so the parent may already be
> suspended when the child is probed.
>
> Resuming the parent, either for the child's probe
> (__driver_probe_device() calls pm_runtime_get_sync(dev->parent)) or on
> a system-wide resume, runs cdns_host_resume() while the drvdata is
> [ ... ]
> resume would dereference freed memory instead of bailing out.
>
> Fixes: 3a85c1011540 ("usb: host: cdns3: forward lost power information to xhci")
> Cc: stable@vger.kernel.org
> Signed-off-by: Guoming Chen <chenguoming88s@gmail.com>
Sashiko has reviewed this patch and found no issues.
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261123075116.3868-1-chenguoming88s@gmail.com?part=1
^ permalink raw reply [flat|nested] 5+ messages in thread* Re: [PATCH v4] usb: cdns3: fix NULL dereference in cdns_host_resume()
2026-11-23 7:51 ` [PATCH v4] " Guomin chen
2026-11-23 8:05 ` sashiko-bot
@ 2026-11-23 11:07 ` Théo Lebrun
1 sibling, 0 replies; 5+ messages in thread
From: Théo Lebrun @ 2026-11-23 11:07 UTC (permalink / raw)
To: Guomin chen, Peter Chen, Pawel Laszczak, Greg Kroah-Hartman
Cc: Roger Quadros, linux-usb, linux-kernel, stable
Hello Guomin,
On Mon Nov 23, 2026 at 8:51 AM CET, Guomin chen wrote:
> From: Guoming Chen <chenguoming88s@gmail.com>
>
> cdns_host_resume() assumes that the xhci-hcd child is bound and passes
> platform_get_drvdata() to hcd_to_xhci_priv() unchecked. The child is
> created by __cdns_host_init(), but xhci-plat probes it only when it
> registers; cdns3/ precedes host/ in drivers/usb/Makefile, so with both
> built in the child is registered before xhci-plat's initcall runs, and
> with xhci-plat as a module it stays unbound until the module loads.
> Platforms that allow runtime PM (cdns3-imx is the only in-tree user)
> autosuspend the controller after 20 ms, so the parent may already be
> suspended when the child is probed.
>
> Resuming the parent, either for the child's probe
> (__driver_probe_device() calls pm_runtime_get_sync(dev->parent)) or on
> a system-wide resume, runs cdns_host_resume() while the drvdata is
> still NULL; xhci_plat_probe() sets it later via __usb_create_hcd().
> hcd_to_xhci_priv(NULL) then dereferences NULL + offsetof(struct
> usb_hcd, primary_hcd):
>
> pc : usb_hcd_is_primary_hcd+0x0/0x1c
> lr : cdns_host_resume+0x24/0x44
>
> Commit 87e4b043b98a ("usb: cdns3: fix role switching during resume")
> only skips the callback when the role changes during resume, which is
> not the case here.
>
> Return early when there is no hcd to notify: the host role can fail to
> start after cdns_role_start() set cdns->role, or the child may not be
> probed yet. An unbound child has no state to restore, power_lost is
> only consumed by an initialized host, and cdns_wakeup_irq() already
> checks cdns->host_dev the same way. Also publish cdns->host_dev only
> after platform_device_add() succeeds: the error paths free the child
> with platform_device_put() while the pointer stays set, and the next
> resume would dereference freed memory instead of bailing out.
Your commit is too long and robotic; didn't read!
Reading the patch, you protect against two things:
- cdns->host_dev == NULL
- platform_get_drvdata(cdns->host_dev) == NULL
Your commit message should say that out loud and give an exact way for
those two to occur. It should also mention the hardware used to
reproduce the issue, with full kernel panic log.
Thanks,
--
Théo Lebrun, Bootlin
Embedded Linux and Kernel engineering
https://bootlin.com
^ permalink raw reply [flat|nested] 5+ messages in thread
end of thread, other threads:[~2026-11-23 11:07 UTC | newest]
Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-23 4:43 [PATCH v3] usb: cdns3: fix NULL dereference in cdns_host_resume() Guomin chen
2026-11-23 4:56 ` sashiko-bot
2026-11-23 7:51 ` [PATCH v4] " Guomin chen
2026-11-23 8:05 ` sashiko-bot
2026-11-23 11:07 ` Théo Lebrun
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox