Re: [PATCH v4] usb: cdns3: fix NULL dereference in cdns_host_resume()

From: guoming chen

Date: Tue Nov 24 2026 - 09:20:58 EST


Théo Lebrun <theo.lebrun@xxxxxxxxxxx> 于2026年11月23日周一 19:07写道:
>
> Hello Guomin,
>
> On Mon Nov 23, 2026 at 8:51 AM CET, Guomin chen wrote:
> > From: Guoming Chen <chenguoming88s@xxxxxxxxx>
> >
> > 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,
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