Skip to contentSkip to conversation

usb: core: Unregister device on component_add() failure

PATCH Superseded Fabio M. De Francesco 2 participants 3 messages Feb 8, 2022 – Feb 9, 2022 USB

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 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-by [email protected]
Fixes 8c67d06f3fd9 ("usb: Link the ports to the connectors they are attached to")
---
 drivers/usb/core/port.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
drivers/usb/core/port.c +4 -1
index c2bbf97a79be..8455b235976a 100644
@@ -605,8 +605,11 @@ int usb_hub_create_port_device(struct usb_hub *hub, int port1)
 	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;
+	}
 
 	/*
 	 * Enable runtime pm and hold a refernce that hub_configure()
Signature · 1 line
2.34.1
#1 lore raw
Quoted 23 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.

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-by [email protected]
Fixes 8c67d06f3fd9 ("usb: Link the ports to the connectors they are attached to")
---
 drivers/usb/core/port.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
drivers/usb/core/port.c +0 -0
index c2bbf97a79be..8455b235976a 100644

drivers/usb/core/port.c int usb_hub_create_port_device(struct usb_hub *hub, int port1) · line 611

Quoted 8 lines
drivers/usb/core/port.c +3 -1
@@ -605,8 +605,11 @@ int usb_hub_create_port_device(struct usb_hub *hub, int port1)
 	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;

You didn't remove the peer links. Either remove them here separately, or alternatively you can also just shuffle the code so that you only create those links after the component_add() call:

drivers/usb/core/port.c +6 -3
index c2bbf97a79bec..d5bc36ca5b1f7 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()

thanks,

Signature · 1 line
heikki
#2 lore raw
Quoted 19 lines · On mercoled? 9 febbraio 2022 15:00:09 CET Heikki Krogerus wrote:
Quoted 18 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.

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-by [email protected]
Fixes 8c67d06f3fd9 ("usb: Link the ports to the connectors they are attached to")
---
 drivers/usb/core/port.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
Quoted 8 lines
Quoted 2 lines

[...]

You didn't remove the peer links. Either remove them here separately, or alternatively you can also just shuffle the code so that you only create those links after the component_add() call:

[...]

Hello Heikki,

Thanks for your review and suggestion. I think that I'll use the second of the two possible solutions (shuffle the code).

I had to spend some time to understand the code of usb_hub_create_port_device(), component_add() and component_del(). Unfortunately, the USB core is very far from the usual things I look at or care of. Therefore I missed that find_and_link_peer() does some work that must be either unwound or simply postponed. I agree with you that the latter is the best solution.

I need some minutes to submit v2.

Again, thanks,

Fabio

Quoted 5 lines

thanks,

Signature · 1 line
heikki