Skip to contentSkip to conversation

usb: core: Call disconnect() only if it is provided by driver

PATCH In review Dmytro Bagrii 4 participants 7 messages May 19, 2022 USB

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 usb: core: Call disconnect() only if it is provided by driver

A driver may use devres allocations. Disconnect handler is not needed in this case. Allow such driver to leave .disconnect field uninitialized in struct usb_driver instead of providing empty stub function.

---
 drivers/usb/core/driver.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
drivers/usb/core/driver.c +2 -1
index 355ed33a2179..d7fe440b033c 100644
@@ -455,7 +455,8 @@ static int usb_unbind_interface(struct device *dev)
 	if (!driver->soft_unbind || udev->state == USB_STATE_NOTATTACHED)
 		usb_disable_interface(udev, intf, false);
 
-	driver->disconnect(intf);
+	if (driver->disconnect)
+		driver->disconnect(intf);
 
 	/* Free streams */
 	for (i = 0, j = 0; i < intf->cur_altsetting->desc.bNumEndpoints; i++) {
Signature · 1 line
2.36.1
maintainer
#1 lore raw
Quoted 26 lines · Dmytro Bagrii

A driver may use devres allocations. Disconnect handler is not needed in this case. Allow such driver to leave .disconnect field uninitialized in struct usb_driver instead of providing empty stub function.

---
 drivers/usb/core/driver.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
drivers/usb/core/driver.c +2 -1
index 355ed33a2179..d7fe440b033c 100644
@@ -455,7 +455,8 @@ static int usb_unbind_interface(struct device *dev)
 	if (!driver->soft_unbind || udev->state == USB_STATE_NOTATTACHED)
 		usb_disable_interface(udev, intf, false);
 
-	driver->disconnect(intf);
+	if (driver->disconnect)
+		driver->disconnect(intf);
 
 	/* Free streams */
 	for (i = 0, j = 0; i < intf->cur_altsetting->desc.bNumEndpoints; i++) {
Signature · 1 line
2.36.1

What in-kernel driver has this issue and does not have a disconnect callback?

thanks,

greg k-h

#2 lore raw
Quoted 22 lines · Greg KH
Quoted 21 lines · Dmytro Bagrii

A driver may use devres allocations. Disconnect handler is not needed in this case. Allow such driver to leave .disconnect field uninitialized in struct usb_driver instead of providing empty stub function.

---
 drivers/usb/core/driver.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
drivers/usb/core/driver.c +2 -1
index 355ed33a2179..d7fe440b033c 100644
@@ -455,7 +455,8 @@ static int usb_unbind_interface(struct device *dev)
 	if (!driver->soft_unbind || udev->state == USB_STATE_NOTATTACHED)
 		usb_disable_interface(udev, intf, false);
 
-	driver->disconnect(intf);
+	if (driver->disconnect)
+		driver->disconnect(intf);
 
Quoted 8 lines
Quoted 5 lines
drivers/usb/core/driver.c +0 -0
 	/* Free streams */
 	for (i = 0, j = 0; i < intf->cur_altsetting->desc.bNumEndpoints; i++) {
Signature · 1 line
2.36.1

What in-kernel driver has this issue and does not have a disconnect callback?

I don't see such in-kernel USB drivers yet. I develop an out-of-tree driver and use devres for all initialization including controllers registration etc. For actions which aren't available with devm_*, e.g. URB allocation i use devm_add_action_or_reset() for deinit. I realized my _disconnect() function is empty, nothing to do there. All deinitialization is done during usb interface release. If i leave .disconnect field uninitialized in struct usb_driver, obviously i get NULL pointer dereference bug. Thus i decided that allowing a driver not to provide disconnect() is a good idea, as it is done for remove() in SPI and I2C subsystems.

Signature · 2 lines
Best Regards,
Dmytro Bagrii.
maintainer
#3 lore raw
Quoted 32 lines · Dmytro Bagrii
Quoted 22 lines · Greg KH
Quoted 21 lines · Dmytro Bagrii

A driver may use devres allocations. Disconnect handler is not needed in this case. Allow such driver to leave .disconnect field uninitialized in struct usb_driver instead of providing empty stub function.

---
 drivers/usb/core/driver.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
drivers/usb/core/driver.c +2 -1
index 355ed33a2179..d7fe440b033c 100644
@@ -455,7 +455,8 @@ static int usb_unbind_interface(struct device *dev)
 	if (!driver->soft_unbind || udev->state == USB_STATE_NOTATTACHED)
 		usb_disable_interface(udev, intf, false);
 
-	driver->disconnect(intf);
+	if (driver->disconnect)
+		driver->disconnect(intf);
 
Quoted 8 lines
Quoted 5 lines
drivers/usb/core/driver.c +0 -0
 	/* Free streams */
 	for (i = 0, j = 0; i < intf->cur_altsetting->desc.bNumEndpoints; i++) {
Signature · 1 line
2.36.1

What in-kernel driver has this issue and does not have a disconnect callback?

Quoted 1 line

I don't see such in-kernel USB drivers yet.

Great, then all is well. We can not make kernel changes for out-of-tree drivers for obvious reasons.

When you submit your driver, we will be glad to consider this change. But as others changed, odds are your driver is incorrect and should have a disconnect call. Unless it is a very simple driver that could be done instead in userspace with usbfs/libusb?

thanks,

greg k-h

#4 lore raw
Quoted 35 lines · Greg KH
Quoted 32 lines · Dmytro Bagrii
Quoted 22 lines · Greg KH
Quoted 21 lines · Dmytro Bagrii

A driver may use devres allocations. Disconnect handler is not needed in this case. Allow such driver to leave .disconnect field uninitialized in struct usb_driver instead of providing empty stub function.

---
 drivers/usb/core/driver.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
drivers/usb/core/driver.c +2 -1
index 355ed33a2179..d7fe440b033c 100644
@@ -455,7 +455,8 @@ static int usb_unbind_interface(struct device *dev)
 	if (!driver->soft_unbind || udev->state == USB_STATE_NOTATTACHED)
 		usb_disable_interface(udev, intf, false);
 
-	driver->disconnect(intf);
+	if (driver->disconnect)
+		driver->disconnect(intf);
 
Quoted 8 lines
Quoted 5 lines
drivers/usb/core/driver.c +0 -0
 	/* Free streams */
 	for (i = 0, j = 0; i < intf->cur_altsetting->desc.bNumEndpoints; i++) {
Signature · 1 line
2.36.1

What in-kernel driver has this issue and does not have a disconnect callback?

Quoted 1 line

I don't see such in-kernel USB drivers yet.

Quoted 7 lines

Great, then all is well. We can not make kernel changes for out-of-tree drivers for obvious reasons.

When you submit your driver, we will be glad to consider this change. But as others changed, odds are your driver is incorrect and should have a disconnect call. Unless it is a very simple driver that could be done instead in userspace with usbfs/libusb?

Ok, i agree, my propoposed change is premature.
Of course, i'm checking the driver for memory, refcounts and other resources leakage during development and not going to publish it until make sure it works correctly.
There are some limitations with libusb in my case, e.g. it is unable to bind existing in-tree drivers to a bus provided by hardware over USB. Thank you for explanation.

Signature · 2 lines
Best Regards,
Dmytro Bagrii.
#5 lore raw
In reply to Dmytro Bagrii
Quoted 14 lines · Dmytro Bagrii

A driver may use devres allocations. Disconnect handler is not needed in this case. Allow such driver to leave .disconnect field uninitialized in struct usb_driver instead of providing empty stub function.

---
 drivers/usb/core/driver.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
drivers/usb/core/driver.c +0 -0
index 355ed33a2179..d7fe440b033c 100644

drivers/usb/core/driver.c static int usb_unbind_interface(struct device *dev) · line 462

Quoted 9 lines
drivers/usb/core/driver.c +2 -1
@@ -455,7 +455,8 @@ static int usb_unbind_interface(struct device *dev)
 	if (!driver->soft_unbind || udev->state == USB_STATE_NOTATTACHED)
 		usb_disable_interface(udev, intf, false);
 
-	driver->disconnect(intf);
+	if (driver->disconnect)
+		driver->disconnect(intf);
 
 	/* Free streams */
 	for (i = 0, j = 0; i < intf->cur_altsetting->desc.bNumEndpoints; i++) {

I'm very dubious about this change. Disconnect routines generally do more than just deallocation.

Can you point to any drivers that would actually benefit from this?

Alan Stern

#6 lore raw
In reply to Dmytro Bagrii
Quoted 3 lines · Dmytro Bagrii

A driver may use devres allocations. Disconnect handler is not needed in this case. Allow such driver to leave .disconnect field uninitialized in struct usb_driver instead of providing empty stub function.

It is needed. disconnect() means that you have to give up ownership of the interface and must cease IO. That cannot be done with devres.
I am pretty sure that a driver without disconnect() is buggy. disconnect() is the one method that is really not optional.

    Regards
        Oliver