thunderbolt: Keep the USB4 port when component_add() fails
Subsystems (2)
PATCH 1/1 thunderbolt: Keep the USB4 port when component_add() fails
From: Suyuan Wang <[email protected]>
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.
---
Alok asked about the missing return in 2025, and a one-line return patch was posted in May. In both threads Mika said he'd rather keep the port, so this does that instead.
https://lore.kernel.org/all/[email protected]/
Compile tested only, I didn't try to force component_add() to fail.
--- drivers/thunderbolt/tb.h | 2 ++ drivers/thunderbolt/usb4_port.c | 12 +++++++----- 2 files changed, 9 insertions(+), 5 deletions(-)
drivers/thunderbolt/tb.h +2 -0
@@ -314,6 +314,7 @@ struct tb_port {* @can_offline: Does the port have necessary platform support to move * it into offline mode and back * @offline: The port is currently in offline mode +* @component_added: The port was added to the component framework * @margining: Pointer to margining structure if enabled */ struct usb4_port {
@@ -321,6 +322,7 @@ struct usb4_port {struct tb_port *port;bool can_offline;bool offline; +bool component_added;#ifdef CONFIG_USB4_DEBUGFS_MARGINING struct tb_margining *margining;#endif
drivers/thunderbolt/usb4_port.c +7 -5
@@ -320,11 +320,12 @@ struct usb4_port *usb4_port_device_add(struct tb_port *port)return ERR_PTR (ret); } +/* The port works fine without the component, so don't fail here */ ret =component_add (&usb4->dev, &connector_ops); -if (ret) { -dev_err (&usb4->dev,"failed to add component\n" ); -device_unregister (&usb4->dev); - } +if (ret) +dev_err (&usb4->dev,"failed to add component: %d\n" , ret); +else + usb4->component_added = true;if (!tb_is_upstream_port (port))device_set_wakeup_capable (&usb4->dev, true);
@@ -348,7 +349,8 @@ struct usb4_port *usb4_port_device_add(struct tb_port *port)*/ void usb4_port_device_remove (struct usb4_port *usb4) { -component_del (&usb4->dev, &connector_ops); +if (usb4->component_added) +component_del (&usb4->dev, &connector_ops);device_unregister (&usb4->dev); }
---
base-commit: 630059a5a632377b9b7b487baccde92b7ae1dd47
change-id: 20261112-thunderbolt-7d1eb9b68e19
Best regards,
Signature · 1 line
Suyuan Wang <[email protected]>
[email protected]
AI reviewSashiko has reviewed this patch and found no issues.
Quoted 15 lines
From: Suyuan Wang <[email protected]>
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 andusb4_port_device_release()frees usb4. The function still goes on to set up runtime PM onusb4->devand returns usb4 tousb4_switch_add_ports(), which stores the stale pointer
Quoted 6 lines
[ ... ]Suggested-by Mika Westerberg <[email protected]>Co-developed-by Kaixuan Li <[email protected]>Signed-off-by Kaixuan Li <[email protected]>Signed-off-by Suyuan Wang <[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
Mika Westerberg
AppliedHi,
Quoted 31 lines · On Thu, Nov 12, 2026 at 09:57:03AM +0000, Suyuan Wang via B4 Relay wrote:
From: Suyuan Wang <[email protected]>
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 andusb4_port_device_release()frees usb4. The function still goes on to set up runtime PM onusb4->devand returns usb4 tousb4_switch_add_ports(), which stores the stale pointer inport->usb4. When the router is removed later,usb4_switch_remove_ports()callscomponent_del()anddevice_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 thatusb4_port_device_remove()only callscomponent_del()for a registered component. Otherwisecomponent_del()would hit itsWARN_ON().Found with a static analysis tool and checked by hand.
Quoted 8 lines
Fixes 5dddb4169284 ("thunderbolt: Link USB4 ports to their USB Type-C connectors")Reported-by Alok Tiwari <[email protected]>Suggested-by Mika Westerberg <[email protected]>Co-developed-by Kaixuan Li <[email protected]>Signed-off-by Kaixuan Li <[email protected]>Signed-off-by Suyuan Wang <[email protected]>
Applied to thunderbolt.git/fixes, thanks!