Skip to contentSkip to conversation

USB: Fix device driver race

PATCH Superseded Bastien Nocera 4 participants 13 messages Jul 24, 2020 – Jul 25, 2020 USB

v4 of 5 Superseded Latest is v5 →

lore
Share & tools
lore
#0 lore raw

PATCH v4 1/1 USB: Fix device driver race

When a new device with a specialised device driver is plugged in, the new driver will be modprobe()'d but the driver core will attach the "generic" driver to the device.

After that, nothing will trigger a reprobe when the modprobe()'d device driver has finished initialising, as the device has the "generic" driver attached to it.

Trigger a reprobe ourselves when new specialised drivers get registered.

Fixes 88b7381a939d

---

Changes since v3:
- Only reprobe devices that could use the new driver
- Many code fixes

Changes since v2:
- Fix formatting

Changes since v1:
- Simplified after Alan Stern's comments and some clarifications from Benjamin Tissoires.

 drivers/usb/core/driver.c | 32 ++++++++++++++++++++++++++++++--
 1 file changed, 30 insertions(+), 2 deletions(-)
drivers/usb/core/driver.c +30 -2
index f81606c6a35b..6731a8e104bc 100644
@@ -905,6 +905,27 @@ static int usb_uevent(struct device *dev, struct kobj_uevent_env *env)
 	return 0;
 }
 
+static int __usb_bus_reprobe_drivers(struct device *dev, void *data)
+{
+	struct usb_device_driver *new_udriver = data;
+	struct usb_device_driver *udriver;
+	struct usb_device *udev;
+
+	if (!dev->driver)
+		return 0;
+
+	udriver = to_usb_device_driver(dev->driver);
+	if (udriver != &usb_generic_driver)
+		return 0;
+
+	udev = to_usb_device(dev);
+	if (usb_device_match_id(udev, new_udriver->id_table) != NULL ||
+	    (new_udriver->match && new_udriver->match(udev) == 0))
+		device_reprobe(dev);
+
+	return 0;
+}
+
 /**
  * usb_register_device_driver - register a USB device (not interface) driver
  * @new_udriver: USB operations for the device driver
@@ -934,13 +955,20 @@ int usb_register_device_driver(struct usb_device_driver *new_udriver,
 
 	retval = driver_register(&new_udriver->drvwrap.driver);
 
-	if (!retval)
+	if (!retval) {
 		pr_info("%s: registered new device driver %s\n",
 			usbcore_name, new_udriver->name);
-	else
+		/* Check whether any device could be better served with
+		 * this new driver
+		 */
+		if (new_udriver->match || new_udriver->id_table)
+			bus_for_each_dev(&usb_bus_type, NULL, new_udriver,
+					 __usb_bus_reprobe_drivers);
+	} else {
 		printk(KERN_ERR "%s: error %d registering device "
 			"	driver %s\n",
 			usbcore_name, retval, new_udriver->name);
+	}
 
 	return retval;
 }
#1 lore raw

Hello!

Quoted 11 lines · Bastien Nocera

When a new device with a specialised device driver is plugged in, the new driver will be modprobe()'d but the driver core will attach the "generic" driver to the device.

After that, nothing will trigger a reprobe when the modprobe()'d device driver has finished initialising, as the device has the "generic" driver attached to it.

Trigger a reprobe ourselves when new specialised drivers get registered.

Fixes 88b7381a939d

It's strange that nobody has noticed... you need to cite the commit
summary here as well, enclosed into ("").

Quoted 1 line

[...]

drivers/usb/core/driver.c

Quoted 4 lines
drivers/usb/core/driver.c +0 -0
index f81606c6a35b..6731a8e104bc 100644

[...]

MBR, Sergei

#2 lore raw
Quoted 14 lines · Sergei Shtylyov

Hello!

Quoted 10 lines · Bastien Nocera

When a new device with a specialised device driver is plugged in, the
new driver will be modprobe()'d but the driver core will attach the "generic" driver to the device.

After that, nothing will trigger a reprobe when the modprobe()'d device
driver has finished initialising, as the device has the "generic" driver attached to it.

Quoted 8 lines
Quoted 4 lines

Trigger a reprobe ourselves when new specialised drivers get registered.

Fixes 88b7381a939d

It's strange that nobody has noticed... you need to cite the
commit
summary here as well, enclosed into ("").

Changed locally, thanks!

Quoted 9 lines
Quoted 1 line

[...]

drivers/usb/core/driver.c

Quoted 4 lines
drivers/usb/core/driver.c +0 -0
index f81606c6a35b..6731a8e104bc 100644

[...]

MBR, Sergei

#3 lore raw
In reply to Bastien Nocera
Quoted 10 lines · Bastien Nocera

When a new device with a specialised device driver is plugged in, the new driver will be modprobe()'d but the driver core will attach the "generic" driver to the device.

After that, nothing will trigger a reprobe when the modprobe()'d device driver has finished initialising, as the device has the "generic" driver attached to it.

Trigger a reprobe ourselves when new specialised drivers get registered.

...

drivers/usb/core/driver.c static int usb_uevent(struct device *dev, struct kobj_uevent_env *env) · line 919

Quoted 12 lines
drivers/usb/core/driver.c +12 -0
+static int __usb_bus_reprobe_drivers(struct device *dev, void *data)
+{
+       struct usb_device_driver *new_udriver = data;
+       struct usb_device_driver *udriver;
+       struct usb_device *udev;
+
+       if (!dev->driver)
+               return 0;
+
+       udriver = to_usb_device_driver(dev->driver);
+       if (udriver != &usb_generic_driver)
+               return 0;

What about

static bool is_dev_usb_generic_driver(dev)

{
      struct usb_device_driver *udd = dev->driver ?

to_usb_device_driver(dev->driver) : NULL;

      return udd == &usb_generic_driver;
}

if (!is_dev_usb_generic_driver)

    return 0;

?

drivers/usb/core/driver.c static int usb_uevent(struct device *dev, struct kobj_uevent_env *env) · line 926

Quoted 6 lines
drivers/usb/core/driver.c +6 -0
+       udev = to_usb_device(dev);
+       if (usb_device_match_id(udev, new_udriver->id_table) != NULL ||
+           (new_udriver->match && new_udriver->match(udev) == 0))
+               device_reprobe(dev);
+
+       return 0;

What about

     udev = to_usb_device(dev);
     if (usb_device_match_id(udev, new_udriver->id_table) == NULL)
       return 0;

?

  if (new_udriver->match && new_udriver->match(udev) == 0))
               device_reprobe(dev);
    return 0;
Quoted 1 line

+}

...

drivers/usb/core/driver.c int usb_register_device_driver(struct usb_device_driver *new_udriver, · line 963

Quoted 3 lines
drivers/usb/core/driver.c +3 -0
+               /* Check whether any device could be better served with
+                * this new driver
+                */

Comment style?

...

Quoted 1 line

+ if (new_udriver->match || new_udriver->id_table)

But match check is incorporated in the loop function.

Quoted 2 lines
drivers/usb/core/driver.c +2 -0
+                       bus_for_each_dev(&usb_bus_type, NULL, new_udriver,
+                                        __usb_bus_reprobe_drivers);
Signature · 2 lines
With Best Regards,
Andy Shevchenko
#4 lore raw
Quoted 1 line · Andy Shevchenko

On Fri, Jul 24, 2020 at 12:03 PM Bastien Nocera <[email protected]> wrote:

...

Quoted 37 lines

drivers/usb/core/driver.c static int usb_uevent(struct device *dev, struct kobj_uevent_env *env) · line 919

Quoted 12 lines
drivers/usb/core/driver.c +12 -0
+static int __usb_bus_reprobe_drivers(struct device *dev, void *data)
+{
+       struct usb_device_driver *new_udriver = data;
+       struct usb_device_driver *udriver;
+       struct usb_device *udev;
+
+       if (!dev->driver)
+               return 0;
+
+       udriver = to_usb_device_driver(dev->driver);
+       if (udriver != &usb_generic_driver)
+               return 0;

What about

static bool is_dev_usb_generic_driver(dev)

{
      struct usb_device_driver *udd = dev->driver ?

to_usb_device_driver(dev->driver) : NULL;

      return udd == &usb_generic_driver;
}

if (!is_dev_usb_generic_driver)

    return 0;

?

drivers/usb/core/driver.c static int usb_uevent(struct device *dev, struct kobj_uevent_env *env) · line 926

Quoted 6 lines
drivers/usb/core/driver.c +6 -0
+       udev = to_usb_device(dev);
+       if (usb_device_match_id(udev, new_udriver->id_table) != NULL ||
+           (new_udriver->match && new_udriver->match(udev) == 0))
+               device_reprobe(dev);
+
+       return 0;

What about

Quoted 10 lines
     udev = to_usb_device(dev);
     if (usb_device_match_id(udev, new_udriver->id_table) == NULL)
       return 0;

?

  if (new_udriver->match && new_udriver->match(udev) == 0))
               device_reprobe(dev);
    return 0;
Quoted 1 line

+}

It actually sparks a lot of similarities with __check_usb_generic(). Perhaps you may unify them?

Signature · 2 lines
With Best Regards,
Andy Shevchenko
#5 lore raw
Quoted 52 lines · Andy Shevchenko
Quoted 2 lines · Andy Shevchenko

On Fri, Jul 24, 2020 at 12:03 PM Bastien Nocera <[email protected]> wrote:

...

Quoted 45 lines

drivers/usb/core/driver.c static int usb_uevent(struct device *dev, struct kobj_uevent_env *env) · line 919

Quoted 13 lines

+static int __usb_bus_reprobe_drivers(struct device *dev, void *data)

drivers/usb/core/driver.c +11 -0
+{
+       struct usb_device_driver *new_udriver = data;
+       struct usb_device_driver *udriver;
+       struct usb_device *udev;
+
+       if (!dev->driver)
+               return 0;
+
+       udriver = to_usb_device_driver(dev->driver);
+       if (udriver != &usb_generic_driver)
+               return 0;

What about

static bool is_dev_usb_generic_driver(dev)

{
      struct usb_device_driver *udd = dev->driver ?

to_usb_device_driver(dev->driver) : NULL;

      return udd == &usb_generic_driver;
}

if (!is_dev_usb_generic_driver)

    return 0;

?

drivers/usb/core/driver.c static int usb_uevent(struct device *dev, struct kobj_uevent_env *env) · line 926

Quoted 8 lines
(quoted diff) +2 -0
+       udev = to_usb_device(dev);
+       if (usb_device_match_id(udev, new_udriver->id_table) !=

NULL ||
+ (new_udriver->match && new_udriver->match(udev) ==
0))

drivers/usb/core/driver.c +3 -0
+               device_reprobe(dev);
+
+       return 0;

What about

     udev = to_usb_device(dev);
     if (usb_device_match_id(udev, new_udriver->id_table) == NULL)
       return 0;

?

Quoted 8 lines
Quoted 5 lines
  if (new_udriver->match && new_udriver->match(udev) == 0))
               device_reprobe(dev);
    return 0;
Quoted 1 line

+}

It actually sparks a lot of similarities with __check_usb_generic(). Perhaps you may unify them?

They might look alike, but apart from that last check (id_table and match), they're really not. It's a different device driver that's getting checked against the generic driver.

I tried to merge those, and it ended up being either completely wrong, or just as long as the code that used to be there before.

#6 lore raw
In reply to Andy Shevchenko
Quoted 38 lines · Andy Shevchenko
Quoted 12 lines · Bastien Nocera

When a new device with a specialised device driver is plugged in, the
new driver will be modprobe()'d but the driver core will attach the "generic" driver to the device.

After that, nothing will trigger a reprobe when the modprobe()'d device
driver has finished initialising, as the device has the "generic" driver attached to it.

Trigger a reprobe ourselves when new specialised drivers get registered.

...

drivers/usb/core/driver.c static int usb_uevent(struct device *dev, struct kobj_uevent_env *env) · line 919

Quoted 13 lines

+static int __usb_bus_reprobe_drivers(struct device *dev, void *data)

drivers/usb/core/driver.c +11 -0
+{
+       struct usb_device_driver *new_udriver = data;
+       struct usb_device_driver *udriver;
+       struct usb_device *udev;
+
+       if (!dev->driver)
+               return 0;
+
+       udriver = to_usb_device_driver(dev->driver);
+       if (udriver != &usb_generic_driver)
+               return 0;

What about

static bool is_dev_usb_generic_driver(dev)

{
      struct usb_device_driver *udd = dev->driver ?

to_usb_device_driver(dev->driver) : NULL;

Quoted 7 lines
      return udd == &usb_generic_driver;
}

if (!is_dev_usb_generic_driver)

    return 0;

?

Great, done locally.

Quoted 10 lines

drivers/usb/core/driver.c static int usb_uevent(struct device *dev, struct kobj_uevent_env *env) · line 926

Quoted 7 lines
(quoted diff) +2 -0
+       udev = to_usb_device(dev);
+       if (usb_device_match_id(udev, new_udriver->id_table) !=

NULL ||

drivers/usb/core/driver.c +4 -0
+           (new_udriver->match && new_udriver->match(udev) == 0))
+               device_reprobe(dev);
+
+       return 0;

What about

Quoted 8 lines
     udev = to_usb_device(dev);
     if (usb_device_match_id(udev, new_udriver->id_table) == NULL)
       return 0;

?

  if (new_udriver->match && new_udriver->match(udev) == 0))
               device_reprobe(dev);
    return 0;

That's not the same conditionals. If there's ->match but no matching ->id_table, you're not going to reprobe, when we'd want to.

Quoted 10 lines
Quoted 1 line

+}

...

drivers/usb/core/driver.c int usb_register_device_driver(struct usb_device_driver *new_udriver, · line 963

Quoted 4 lines

+ /* Check whether any device could be better served
with

drivers/usb/core/driver.c +2 -0
+                * this new driver
+                */

Comment style?

Fixed locally.

Quoted 5 lines

...

Quoted 1 line

+ if (new_udriver->match || new_udriver->id_table)

But match check is incorporated in the loop function.

It's ->match || ->id_table, not simply ->match. Drivers can have either, and we need to reprobe if either of those are present (and we're also checking in the function to avoid calling a NULL pointer ;)

Quoted 4 lines
Quoted 4 lines

+ bus_for_each_dev(&usb_bus_type, NULL,
new_udriver,
+ __usb_bus_reprobe_drivers)
;

#7 lore raw
In reply to Andy Shevchenko
Quoted 34 lines · Andy Shevchenko
Quoted 10 lines · Bastien Nocera

When a new device with a specialised device driver is plugged in, the new driver will be modprobe()'d but the driver core will attach the "generic" driver to the device.

After that, nothing will trigger a reprobe when the modprobe()'d device driver has finished initialising, as the device has the "generic" driver attached to it.

Trigger a reprobe ourselves when new specialised drivers get registered.

...

drivers/usb/core/driver.c static int usb_uevent(struct device *dev, struct kobj_uevent_env *env) · line 919

Quoted 12 lines
drivers/usb/core/driver.c +12 -0
+static int __usb_bus_reprobe_drivers(struct device *dev, void *data)
+{
+       struct usb_device_driver *new_udriver = data;
+       struct usb_device_driver *udriver;
+       struct usb_device *udev;
+
+       if (!dev->driver)
+               return 0;
+
+       udriver = to_usb_device_driver(dev->driver);
+       if (udriver != &usb_generic_driver)
+               return 0;

What about

static bool is_dev_usb_generic_driver(dev)

{
      struct usb_device_driver *udd = dev->driver ?

to_usb_device_driver(dev->driver) : NULL;

Quoted 7 lines
      return udd == &usb_generic_driver;
}

if (!is_dev_usb_generic_driver)

    return 0;

?

Why would you want to add more lines of code to do the same thing? Abstraction is fine up to point, but excessive abstraction only makes things more difficult.

Quoted 8 lines

drivers/usb/core/driver.c static int usb_uevent(struct device *dev, struct kobj_uevent_env *env) · line 926

Quoted 7 lines
drivers/usb/core/driver.c +6 -0
+       udev = to_usb_device(dev);
+       if (usb_device_match_id(udev, new_udriver->id_table) != NULL ||
+           (new_udriver->match && new_udriver->match(udev) == 0))
+               device_reprobe(dev);
+
+       return 0;
Quoted 6 lines

What about

     udev = to_usb_device(dev);
     if (usb_device_match_id(udev, new_udriver->id_table) == NULL)
       return 0;

?

The logic is wrong. If usb_device_match_id() returns NULL then we want to go ahead with the second test, not give up right away. But this could be written as:

	if (usb_device_match_id(udev, new_udriver->id_table) == NULL &&
			(!new_udriver->match || new_udriver->match(udev) != 0))
		return 0;

	device_reprobe(dev);

This would make the overall flow of the routine more uniform.

Quoted 5 lines

drivers/usb/core/driver.c int usb_register_device_driver(struct usb_device_driver *new_udriver, · line 963

Quoted 3 lines
drivers/usb/core/driver.c +3 -0
+               /* Check whether any device could be better served with
+                * this new driver
+                */

Comment style?

Right:

	/*
	 * Multiline comments
	 * should look like this.
	 */
Quoted 5 lines

...

Quoted 1 line

+ if (new_udriver->match || new_udriver->id_table)

But match check is incorporated in the loop function.

Agreed, this test is redundant. However, we should test that new_udriver != &usb_generic_driver.

Alan Stern

Quoted 7 lines
Quoted 3 lines
drivers/usb/core/driver.c +2 -0
+                       bus_for_each_dev(&usb_bus_type, NULL, new_udriver,
+                                        __usb_bus_reprobe_drivers);
Signature · 2 lines
With Best Regards,
Andy Shevchenko
#8 lore raw
Quoted 46 lines · Alan Stern
Quoted 45 lines · Andy Shevchenko
Quoted 13 lines · Bastien Nocera

When a new device with a specialised device driver is plugged in, the
new driver will be modprobe()'d but the driver core will attach the
"generic" driver to the device.

After that, nothing will trigger a reprobe when the modprobe()'d device
driver has finished initialising, as the device has the "generic" driver attached to it.

Trigger a reprobe ourselves when new specialised drivers get registered.

...

drivers/usb/core/driver.c static int usb_uevent(struct device *dev, struct kobj_uevent_env *env) · line 919

Quoted 13 lines

+static int __usb_bus_reprobe_drivers(struct device *dev, void *data)

drivers/usb/core/driver.c +11 -0
+{
+       struct usb_device_driver *new_udriver = data;
+       struct usb_device_driver *udriver;
+       struct usb_device *udev;
+
+       if (!dev->driver)
+               return 0;
+
+       udriver = to_usb_device_driver(dev->driver);
+       if (udriver != &usb_generic_driver)
+               return 0;

What about

static bool is_dev_usb_generic_driver(dev)

{
      struct usb_device_driver *udd = dev->driver ?

to_usb_device_driver(dev->driver) : NULL;

      return udd == &usb_generic_driver;
}

if (!is_dev_usb_generic_driver)

    return 0;
Quoted 6 lines
Quoted 1 line

?

Why would you want to add more lines of code to do the same thing? Abstraction is fine up to point, but excessive abstraction only makes
things more difficult.

I actually like that one, it made the code clearer IMO.

Quoted 21 lines
Quoted 9 lines

drivers/usb/core/driver.c static int usb_uevent(struct device *dev, struct kobj_uevent_env *env) · line 926

Quoted 8 lines
(quoted diff) +2 -0
+       udev = to_usb_device(dev);
+       if (usb_device_match_id(udev, new_udriver->id_table) !=

NULL ||
+ (new_udriver->match && new_udriver->match(udev) ==
0))

drivers/usb/core/driver.c +3 -0
+               device_reprobe(dev);
+
+       return 0;
Quoted 6 lines

What about

     udev = to_usb_device(dev);
     if (usb_device_match_id(udev, new_udriver->id_table) == NULL)
       return 0;

?

The logic is wrong. If usb_device_match_id() returns NULL then we want
to go ahead with the second test, not give up right away. But this could be written as:

Quoted 8 lines
	if (usb_device_match_id(udev, new_udriver->id_table) == NULL &&
			(!new_udriver->match || new_udriver-
Quoted 1 line

match(udev) != 0))

		return 0;

	device_reprobe(dev);

This would make the overall flow of the routine more uniform.

Adopted.

Quoted 14 lines
Quoted 6 lines

drivers/usb/core/driver.c int usb_register_device_driver(struct usb_device_driver *new_udriver, · line 963

Quoted 4 lines

+ /* Check whether any device could be better
served with

drivers/usb/core/driver.c +2 -0
+                * this new driver
+                */

Comment style?

Right:

	/*
	 * Multiline comments
	 * should look like this.
	 */
Quoted 8 lines
Quoted 5 lines

...

Quoted 1 line

+ if (new_udriver->match || new_udriver->id_table)

But match check is incorporated in the loop function.

Agreed, this test is redundant. However, we should test that new_udriver != &usb_generic_driver.

Do you really want to loop over every USB device when you know for a fact that not a single one will match?

I guess it's unlikely, the generic driver would be loaded before any device, and the specialised drivers need to be able to selected, so I've done that locally.

Quoted 10 lines

Alan Stern

Quoted 8 lines
Quoted 4 lines

+ bus_for_each_dev(&usb_bus_type, NULL,
new_udriver,
+ __usb_bus_reprobe_driver
s);

Signature · 2 lines
With Best Regards,
Andy Shevchenko
#9 lore raw
Quoted 11 lines · Bastien Nocera
Quoted 6 lines · Alan Stern
Quoted 3 lines
Quoted 1 line

+ if (new_udriver->match || new_udriver->id_table)

But match check is incorporated in the loop function.

Agreed, this test is redundant. However, we should test that new_udriver != &usb_generic_driver.

Do you really want to loop over every USB device when you know for a fact that not a single one will match?

Think of it the other way around: How often will anybody load a specialized USB device driver that doesn't have a match function or ID table? It wouldn't match any devices!

Quoted 2 lines

I guess it's unlikely, the generic driver would be loaded before any device,

Since it's built into usbcore, I guess that's true.

Quoted 2 lines

and the specialised drivers need to be able to selected, so
I've done that locally.

Okay, you're ready to submit the next version?

Alan Stern

#10 lore raw
Quoted 23 lines · Alan Stern
Quoted 4 lines · Bastien Nocera
Quoted 3 lines · Alan Stern
Quoted 3 lines
Quoted 2 lines

+ if (new_udriver->match || new_udriver-

Quoted 1 line

id_table)

Quoted 8 lines
Quoted 4 lines
Quoted 1 line

But match check is incorporated in the loop function.

Agreed, this test is redundant. However, we should test that new_udriver != &usb_generic_driver.

Do you really want to loop over every USB device when you know for a
fact that not a single one will match?

Think of it the other way around: How often will anybody load a specialized USB device driver that doesn't have a match function or ID
table? It wouldn't match any devices!

Quoted 3 lines

I guess it's unlikely, the generic driver would be loaded before any
device,

Quoted 6 lines

Since it's built into usbcore, I guess that's true.

Quoted 2 lines

and the specialised drivers need to be able to selected, so
I've done that locally.

Okay, you're ready to submit the next version?

It's compiling. Then I need to test it. I've also added a couple of fixes/cleanups I ran into while doing this.

#11 lore raw
Quoted 1 line · Bastien Nocera

<snip>

Quoted 2 lines

It's compiling. Then I need to test it. I've also added a couple of fixes/cleanups I ran into while doing this.

And sent

#12 lore raw
In reply to Alan Stern
Quoted 2 lines · Alan Stern
Quoted 1 line · Andy Shevchenko

On Fri, Jul 24, 2020 at 12:03 PM Bastien Nocera <[email protected]> wrote:

...

Quoted 10 lines
Quoted 10 lines

What about

static bool is_dev_usb_generic_driver(dev)

{
      struct usb_device_driver *udd = dev->driver ?

to_usb_device_driver(dev->driver) : NULL;

      return udd == &usb_generic_driver;
}
Quoted 8 lines
Quoted 4 lines

if (!is_dev_usb_generic_driver)

    return 0;

?

Why would you want to add more lines of code to do the same thing? Abstraction is fine up to point, but excessive abstraction only makes things more difficult.

At least to put into ternary will actually reduce LOCs in the original code (if we drop helper).

The idea of a helper comes to mind that somebody else might reuse (__check_usb_generic()?).

...

Quoted 3 lines

The logic is wrong. If usb_device_match_id() returns NULL then we want to go ahead with the second test, not give up right away. But this could be written as:

Yes, sorry I didn't think well here.

Quoted 8 lines
        if (usb_device_match_id(udev, new_udriver->id_table) == NULL &&
                        (!new_udriver->match || new_udriver->match(udev) != 0))
                return 0;

        device_reprobe(dev);

This would make the overall flow of the routine more uniform.

Yes, my intention was to make all error cases go first.

Signature · 2 lines
With Best Regards,
Andy Shevchenko