Skip to contentSkip to conversation

usb: core: Unregister device on component_add() failure

PATCH In review Fabio M. De Francesco 2 participants 2 messages Feb 9, 2022 – Feb 10, 2022 USB

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 usb: core: Unregister device on component_add() failure

Commit 8c67d06f3fd9 ("usb: Link the ports to the connectors they are attached to") creates a link to the USB Type-C connector for every new port that is added when possible. If component_add() fails, usb_hub_create_port_device() prints a warning but does not unregister the device and does not return errors to the callers.

Syzbot reported a "WARNING in component_del()".

Fix this issue in usb_hub_create_port_device by calling device_unregister() and returning the errors from component_add().

Reported-and-tested-by [email protected]
Fixes 8c67d06f3fd9 ("usb: Link the ports to the connectors they are attached to")

---

v1->v2: Move find_and_link_peer() soon after the 'if' test for "retval",
as suggested by Heikki Krogerus with his review of v1.

 drivers/usb/core/port.c | 9 ++++++---
 1 file changed, 6 insertions(+), 3 deletions(-)
drivers/usb/core/port.c +6 -3
index c2bbf97a79be..d5bc36ca5b1f 100644
@@ -602,11 +602,14 @@ int usb_hub_create_port_device(struct usb_hub *hub, int port1)
 		return retval;
 	}
 
-	find_and_link_peer(hub, port1);
-
 	retval = component_add(&port_dev->dev, &connector_ops);
-	if (retval)
+	if (retval) {
 		dev_warn(&port_dev->dev, "failed to add component\n");
+		device_unregister(&port_dev->dev);
+		return retval;
+	}
+
+	find_and_link_peer(hub, port1);
 
 	/*
 	 * Enable runtime pm and hold a refernce that hub_configure()
Signature · 1 line
2.34.1
Reviewed-by on v2 1/1
#1 lore raw
Quoted 6 lines · Fabio M. De Francesco

Commit 8c67d06f3fd9 ("usb: Link the ports to the connectors they are attached to") creates a link to the USB Type-C connector for every new port that is added when possible. If component_add() fails, usb_hub_create_port_device() prints a warning but does not unregister the device and does not return errors to the callers.

Quoted 8 lines

Syzbot reported a "WARNING in component_del()".

Fix this issue in usb_hub_create_port_device by calling device_unregister() and returning the errors from component_add().

Reported-and-tested-by [email protected]
Fixes 8c67d06f3fd9 ("usb: Link the ports to the connectors they are attached to")

FWIW:

Quoted 32 lines

---

v1->v2: Move find_and_link_peer() soon after the 'if' test for "retval",
as suggested by Heikki Krogerus with his review of v1.

 drivers/usb/core/port.c | 9 ++++++---
 1 file changed, 6 insertions(+), 3 deletions(-)
drivers/usb/core/port.c +6 -3
index c2bbf97a79be..d5bc36ca5b1f 100644
@@ -602,11 +602,14 @@ int usb_hub_create_port_device(struct usb_hub *hub, int port1)
 		return retval;
 	}
 
-	find_and_link_peer(hub, port1);
-
 	retval = component_add(&port_dev->dev, &connector_ops);
-	if (retval)
+	if (retval) {
 		dev_warn(&port_dev->dev, "failed to add component\n");
+		device_unregister(&port_dev->dev);
+		return retval;
+	}
+
+	find_and_link_peer(hub, port1);
 
 	/*
 	 * Enable runtime pm and hold a refernce that hub_configure()
Signature · 1 line
2.34.1
Signature · 1 line
heikki