usb: cdns3: fix NULL dereference in cdns_host_resume()
PATCH v3 1/1 usb: cdns3: fix NULL dereference in cdns_host_resume()
From: Guoming Chen <[email protected]>
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.
---
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(-)
drivers/usb/cdns3/host.c +17 -3
@@ -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 ;
Signature · 1 line
2.34.1
[email protected]
AI reviewThank 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 --
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 <[email protected]>
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.
Quoted 16 lines
drivers/usb/cdns3/host.c +5
-3@@ -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
drivers/usb/cdns3/host.c static void cdns_host_exit(struct cdns *cdns) · line 157
Quoted 10 lines
drivers/usb/cdns3/host.c +10
-0+* 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.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
PATCH v4 1/1 usb: cdns3: fix NULL dereference in cdns_host_resume()
In reply to Guomin chenFrom: Guoming Chen <[email protected]>
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.
---
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 [email protected], 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(-)
drivers/usb/cdns3/host.c +24 -4
@@ -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 ;
Signature · 1 line
2.34.1
[email protected]
AI reviewSashiko has reviewed this patch and found no issues.
Quoted 18 lines
From: Guoming Chen <[email protected]>
cdns_host_resume()assumes that the xhci-hcd child is bound and passesplatform_get_drvdata()tohcd_to_xhci_priv()unchecked. The child is created by__cdns_host_init(), but xhci-plat probes it only when it registers; cdns3/ precedes host/ indrivers/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()callspm_runtime_get_sync(dev->parent)) or on a system-wide resume, runscdns_host_resume()while the drvdata is [ ... ]
resume would dereference freed memory instead of bailing out.
Quoted 3 lines
Fixes 3a85c1011540 ("usb: host: cdns3: forward lost power information to xhci")Signed-off-by Guoming Chen <[email protected]>
Sashiko has reviewed this patch and found no issues.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
Hello Guomin,
Quoted 26 lines · Guomin chen
From: Guoming Chen <[email protected]>
cdns_host_resume()assumes that the xhci-hcd child is bound and passesplatform_get_drvdata()tohcd_to_xhci_priv()unchecked. The child is created by__cdns_host_init(), but xhci-plat probes it only when it registers; cdns3/ precedes host/ indrivers/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()callspm_runtime_get_sync(dev->parent)) or on a system-wide resume, runscdns_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/0x44Commit
87e4b043b98a("usb: cdns3: fix role switching during resume") only skips the callback when the role changes during resume, which is not the case here.
Quoted 8 lines
Return early when there is no hcd to notify: the host role can fail to start after
cdns_role_start()setcdns->role, or the child may not be probed yet. An unbound child has no state to restore,power_lostis only consumed by an initialized host, andcdns_wakeup_irq()already checkscdns->host_devthe same way. Also publishcdns->host_devonly afterplatform_device_add()succeeds: the error paths free the child withplatform_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
Quoted 45 lines · Théo Lebrun <[email protected]> 于2026年11月23日周一 19:07写道:
Hello Guomin,
Quoted 26 lines · Guomin chen
From: Guoming Chen <[email protected]>
cdns_host_resume()assumes that the xhci-hcd child is bound and passesplatform_get_drvdata()tohcd_to_xhci_priv()unchecked. The child is created by__cdns_host_init(), but xhci-plat probes it only when it registers; cdns3/ precedes host/ indrivers/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()callspm_runtime_get_sync(dev->parent)) or on a system-wide resume, runscdns_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/0x44Commit
87e4b043b98a("usb: cdns3: fix role switching during resume") only skips the callback when the role changes during resume, which is not the case here.Quoted 8 lines
Return early when there is no hcd to notify: the host role can fail to start after
cdns_role_start()setcdns->role, or the child may not be probed yet. An unbound child has no state to restore,power_lostis only consumed by an initialized host, andcdns_wakeup_irq()already checkscdns->host_devthe same way. Also publishcdns->host_devonly afterplatform_device_add()succeeds: the error paths free the child withplatform_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
Quoted 5 lines
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,
Hi Théo,
thanks for the review. You asked for the exact way each of the two cases happens, the hardware it reproduces on, and the full panic log; here is what I can share, plus what I plan for v5. Machine identifiers are redacted from the log.
Both NULL cases are reachable in the code:
- cdns->host_dev: cdns_role_start() sets cdns->role before calling the role's start callback and does not undo it when the callback fails. cdns_resume() then calls the role's resume callback regardless of the role driver's state, so cdns_host_resume() runs for the host role with cdns->host_dev == NULL.
- the child's drvdata: __cdns_host_init() creates the xhci-hcd platform device, but the drvdata is set only in xhci_plat_probe(), by __usb_create_hcd(). The parent is resumed before that: when the child is first probed, __driver_probe_device() calls pm_runtime_get_sync(dev->parent) before really_probe(); a system resume can also arrive while the child is still unbound.
What matters for this bug is the configuration, not the board: the glue enables runtime PM (CDNS3_DEFAULT_PM_RUNTIME_ALLOW), so cdns3-plat.c uses a 20 ms autosuspend instead of pm_runtime_forbid(), and the controller can already be suspended when xhci-plat registers. cdns3/ is linked before host/ in drivers/usb/Makefile, so the child can exist and stay unbound when xhci_plat_init() later registers its driver. The in-tree i.MX8QM glue sets the same quirk, so the window is not specific to this platform.
The oops below is the second case. The faulting address, 0x208, is offsetof(struct usb_hcd, primary_hcd), read by usb_hcd_is_primary_hcd() through hcd_to_xhci_priv(NULL).
Unable to handle kernel NULL pointer dereference at virtual address
0000000000000208
Mem abort info:
ESR = 0x0000000096000004
EC = 0x25: DABT (current EL), IL = 32 bits SET = 0, FnV = 0
EA = 0, S1PTW = 0
FSC = 0x04: level 0 translation fault
Data abort info:
ISV = 0, ISS = 0x00000004, ISS2 = 0x00000000 CM = 0, WnR = 0, TnD = 0, TagAccess = 0
GCS = 0, Overlay = 0, DirtyBit = 0, Xs = 0
[0000000000000208] user address but active_mm is swapper
Internal error: Oops: 0000000096000004 [#1] SMP Modules linked in:
pstate: 81400009 (Nzcv daif +PAN -UAO -TCO +DIT -SSBS BTYPE=--)
pc : usb_hcd_is_primary_hcd+0x0/0x1c lr : cdns_host_resume+0x24/0x44
sp : ffff80008007ba40
x29: ffff80008007ba40
x28: ffff0000993c8000
x27: ffff0000a0a39148
x26: 0000000000000001
x25: 0000000000000000
x24: 0000000000000001
x23: 0000000000000002
x22: ffff0000a0a39104
x21: 0000000000000410
x20: 0000000000000000
x19: 0000000000000000
x18: 00000000ffffffff
x17: 2072657473696765
x16: 722074616c705f69
x15: 63687820746e6569
x14: 6c63203a5d6d7364
x13: ffffc001a9b23dc8
x12: 000000000000083d
x11: 00000000000002bf
x10: 0000c001087aec88
x9 : 0000000000000000
x8 : 0101010101010101
x7 : ff7f7f7f7f7f7f7f
x6 : 8080808080808080
x5 : 0000000000000023
x4 : 1fffffffffffffff
x3 : ffff0000a0a39c80
x2 : ffffc001a829e030
x1 : 0000000000000000
x0 : 0000000000000000
Call trace: usb_hcd_is_primary_hcd+0x0/0x1c (P) cdns_resume+0x50/0xdc cdnsp_controller_resume.isra.0+0x14c/0x198 cdnsp_plat_runtime_resume+0x20/0x10c pm_generic_runtime_resume+0x2c/0x44 __rpm_callback+0x48/0x1dc rpm_callback+0x74/0x80 rpm_resume+0x44c/0x63c __pm_runtime_resume+0x4c/0x90 __driver_probe_device+0x64/0x1a0 driver_probe_device+0x3c/0x110 __driver_attach+0xf4/0x1fc bus_for_each_dev+0x78/0xd8 driver_attach+0x24/0x30 bus_add_driver+0x110/0x234 driver_register+0x60/0x128 __platform_driver_register+0x24/0x30 xhci_plat_init+0x44/0x50 do_one_initcall+0x58/0x264 kernel_init_freeable+0x1e4/0x3c0 kernel_init+0x20/0x1dc ret_from_fork+0x10/0x20 Code: 52800003 f9407ca5 d63f00a0 17ffffe4 (f9410401) ---[ end trace 0000000000000000 ]--- Kernel panic - not syncing: Oops: Fatal exception
The fix returns early in both cases. This does not weaken the path the commit was added for: on a system resume the child is bound and power_lost is still forwarded to xhci-plat exactly as before. The other hunk, publishing cdns->host_dev only after platform_device_add() succeeds (reported by Sashiko in v4), keeps cdns_host_exit() and the next resume from touching a child that an error path already freed.
The log is from a 6.18-based kernel, but the function at the crash site is unchanged from mainline, and the v5 patch applies to current mainline.
In v5 I will state both checks and the exact path that produces each NULL in the commit message, and include the relevant frames of this trace; the code is unchanged. I will send v5 shortly.
Thanks,
Guoming
PATCH v5 1/1 usb: cdns3: fix NULL dereference in cdns_host_resume()
In reply to Guomin chencdns_host_resume() dereferences the xhci-hcd child's drvdata without checking it, and both the child and its drvdata can be NULL:
- cdns->host_dev: cdns_role_start() sets cdns->role before calling the role's start callback and keeps it on failure, so the next resume still calls cdns_host_resume() after a failed host start.
- the child's drvdata: __cdns_host_init() creates the child, but xhci-plat sets the drvdata only in its probe. The parent is resumed before that, by __driver_probe_device() when the child is first probed and by a system resume in between.
Commit 87e4b043b98a ("usb: cdns3: fix role switching during resume") only covers a role change during resume.
Check both and return early; the child's probe initializes the controller from scratch, so power_lost has nothing to act on yet. Publish cdns->host_dev only after platform_device_add() succeeds, so the error paths leave it NULL instead of pointing at a freed device.
The resume-before-probe case oopses as:
pc : usb_hcd_is_primary_hcd+0x0/0x1c lr : cdns_host_resume+0x24/0x44 Call trace: usb_hcd_is_primary_hcd+0x0/0x1c cdns_resume+0x50/0xdc cdnsp_controller_resume.isra.0+0x14c/0x198 cdnsp_plat_runtime_resume+0x20/0x10c ... __pm_runtime_resume+0x4c/0x90 __driver_probe_device+0x64/0x1a0 xhci_plat_init+0x44/0x50
---
Changes in v5:
- Rewrite the commit message around the two NULL checks and their exact causes; add the oops call trace. No functional changes.
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 [email protected], 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(-)
drivers/usb/cdns3/host.c +24 -4
@@ -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 ;
Signature · 1 line
2.34.1
[email protected]
AI reviewSashiko has reviewed this patch and found no issues.
Quoted 15 lines
cdns_host_resume()dereferences the xhci-hcd child's drvdata without checking it, and both the child and its drvdata can be NULL:-
cdns->host_dev:cdns_role_start()setscdns->rolebefore calling the role's start callback and keeps it on failure, so the next resume still callscdns_host_resume()after a failed host start.- the child's drvdata:
__cdns_host_init()creates the child, but xhci-plat sets the drvdata only in its probe. The parent is resumed before that, by__driver_probe_device()when the child is first probed and by a system resume in between.Commit
87e4b043b98a("usb: cdns3: fix role switching during resume") only covers a role change during resume.
Quoted 6 lines
[ ... ]
xhci_plat_init+0x44/0x50Fixes 3a85c1011540 ("usb: host: cdns3: forward lost power information to xhci")Signed-off-by Guoming Chen <[email protected]>
Sashiko has reviewed this patch and found no issues.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
Hello Guoming,
Quoted 6 lines · Guoming Chen
cdns_host_resume()dereferences the xhci-hcd child's drvdata without checking it, and both the child and its drvdata can be NULL:-
cdns->host_dev:cdns_role_start()setscdns->rolebefore calling the role's start callback and keeps it on failure, so the next resume still callscdns_host_resume()after a failed host start.
That explanation is much clearer. But the fix here isn't to patch resume to support being called if the role wasn't started. The fix is to never call resume if the role wasn't started successfully.
Two ways to do this (there might be more):
- either we unset cdns->role if role->start() failed
- or we don't call role->resume() if state == INACTIVE
Please provide the platform name + full kernel panic, this was asked for in my last email... Or you say that you have never seen that bug in practice.
Quoted 4 lines
- the child's drvdata:
__cdns_host_init()creates the child, but xhci-plat sets the drvdata only in its probe. The parent is resumed before that, by__driver_probe_device()when the child is first probed and by a system resume in between.
Here again if the role isn't all setup, then its resume should never get called. Wouldn't the check for state==ACTIVE before calling role->resume() be enough?
In any case, again, having a resume callback be called when a device/role isn't ready isn't correct and *that* should be fixed.
Again, for this bug here. You were asked to provide the platform used and full kernel panics (two because you are fixing two bugs here). What is found below in your commit is not a full panic log, it is a tiny hand(?)-crafted snippet. Or you say that you have never seen that bug in practice.
Quoted 2 lines
Commit
87e4b043b98a("usb: cdns3: fix role switching during resume") only covers a role change during resume.
Funny you mention this commit; it does the proper thing of NOT calling resume when it would be broken to do so.
Quoted 19 lines
Check both and return early; the child's probe initializes the controller from scratch, so
power_losthas nothing to act on yet. Publishcdns->host_devonly afterplatform_device_add()succeeds, so the error paths leave it NULL instead of pointing at a freed device.The resume-before-probe case oopses as:
pc : usb_hcd_is_primary_hcd+0x0/0x1c lr : cdns_host_resume+0x24/0x44 Call trace: usb_hcd_is_primary_hcd+0x0/0x1c cdns_resume+0x50/0xdc cdnsp_controller_resume.isra.0+0x14c/0x198 cdnsp_plat_runtime_resume+0x20/0x10c ... __pm_runtime_resume+0x4c/0x90 __driver_probe_device+0x64/0x1a0 xhci_plat_init+0x44/0x50
Quoted 7 lines
Fixes 3a85c1011540 ("usb: host: cdns3: forward lost power information to xhci")Signed-off-by Guoming Chen <[email protected]>---
Changes in v5:
- Rewrite the commit message around the two NULL checks and their exact causes; add the oops call trace. No functional changes.
From v4 to v5 you dropped the Cc to lkml and stable mailing lists. Why?
Thanks,
--
Théo Lebrun, Bootlin
Embedded Linux and Kernel engineering
https://bootlin.com