Re: [PATCH] thunderbolt: Keep the USB4 port when component_add() fails
From: Mika Westerberg
Date: Mon Nov 16 2026 - 08:58:22 EST
Hi,
On Thu, Nov 12, 2026 at 09:57:03AM +0000, Suyuan Wang via B4 Relay wrote:
> From: Suyuan Wang <stevewong@xxxxxxxxxxx>
>
> When component_add() fails, usb4_port_device_add() unregisters the
> device but doesn't return:
>
> ret = component_add(&usb4->dev, &connector_ops);
> if (ret) {
> dev_err(&usb4->dev, "failed to add component\n");
> device_unregister(&usb4->dev);
> }
>
> The port was registered just before, so device_unregister() normally
> drops the last reference and usb4_port_device_release() frees usb4.
> The function still goes on to set up runtime PM on usb4->dev and
> returns usb4 to usb4_switch_add_ports(), which stores the stale pointer
> in port->usb4. When the router is removed later,
> usb4_switch_remove_ports() calls component_del() and
> device_unregister() on that pointer again.
>
> component_add() can fail on allocation, or when binding the aggregate
> driver fails. It isn't common, but when it happens the result is a
> use-after-free and a double unregister.
>
> The USB4 port can keep working without the component link. Keep the
> port registered when component_add() fails, and track whether the
> component was added, so that usb4_port_device_remove() only calls
> component_del() for a registered component. Otherwise component_del()
> would hit its WARN_ON().
>
> Found with a static analysis tool and checked by hand.
>
> Fixes: 5dddb4169284 ("thunderbolt: Link USB4 ports to their USB Type-C connectors")
> Reported-by: Alok Tiwari <alok.a.tiwari@xxxxxxxxxx>
> Closes: https://lore.kernel.org/all/20250622140455.2884518-1-alok.a.tiwari@xxxxxxxxxx/
> Suggested-by: Mika Westerberg <mika.westerberg@xxxxxxxxxxxxxxx>
> Cc: stable@xxxxxxxxxxxxxxx
> Co-developed-by: Kaixuan Li <kaixuan.li@xxxxxxxxxx>
> Signed-off-by: Kaixuan Li <kaixuan.li@xxxxxxxxxx>
> Signed-off-by: Suyuan Wang <stevewong@xxxxxxxxxxx>
Applied to thunderbolt.git/fixes, thanks!