Skip to contentSkip to conversation

usb: roles: Call try_module_get() from usb_role_switch_find_by_fwnode()

PATCH In review Hans de Goede 2 participants 2 messages Apr 9, 2021 USB

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 usb: roles: Call try_module_get() from usb_role_switch_find_by_fwnode()

usb_role_switch_find_by_fwnode() returns a reference to the role-switch which must be put by calling usb_role_switch_put().

usb_role_switch_put() calls module_put(sw->dev.parent->driver->owner), add a matching try_module_get() to usb_role_switch_find_by_fwnode(), making it behave the same as the other usb_role_switch functions which return a reference.

This avoids a WARN_ON being hit at kernel/module.c:1158 due to the module-refcount going below 0.

Fixes c6919d5e0cd1 ("usb: roles: Add usb_role_switch_find_by_fwnode()")

---
Changes in v2:
- Fix typo in Subject
- Add Fixes tag

---
 drivers/usb/roles/class.c | 2 ++
 1 file changed, 2 insertions(+)
drivers/usb/roles/class.c +2 -0
index 97f37077b7f9..33b637d0d8d9 100644
@@ -189,6 +189,8 @@ usb_role_switch_find_by_fwnode(const struct fwnode_handle *fwnode)
 		return NULL;
 
 	dev = class_find_device_by_fwnode(role_class, fwnode);
+	if (dev)
+		WARN_ON(!try_module_get(dev->parent->driver->owner));
 
 	return dev ? to_role_switch(dev) : NULL;
 }
Signature · 1 line
2.30.2
Reviewed-by on v2 1/1
#1 lore raw
Quoted 8 lines · Hans de Goede

usb_role_switch_find_by_fwnode() returns a reference to the role-switch which must be put by calling usb_role_switch_put().

usb_role_switch_put() calls module_put(sw->dev.parent->driver->owner), add a matching try_module_get() to usb_role_switch_find_by_fwnode(), making it behave the same as the other usb_role_switch functions which return a reference.

Quoted 6 lines

This avoids a WARN_ON being hit at kernel/module.c:1158 due to the module-refcount going below 0.

Fixes c6919d5e0cd1 ("usb: roles: Add usb_role_switch_find_by_fwnode()")
Quoted 23 lines

---
Changes in v2:
- Fix typo in Subject
- Add Fixes tag

---
 drivers/usb/roles/class.c | 2 ++
 1 file changed, 2 insertions(+)
drivers/usb/roles/class.c +2 -0
index 97f37077b7f9..33b637d0d8d9 100644
@@ -189,6 +189,8 @@ usb_role_switch_find_by_fwnode(const struct fwnode_handle *fwnode)
 		return NULL;
 
 	dev = class_find_device_by_fwnode(role_class, fwnode);
+	if (dev)
+		WARN_ON(!try_module_get(dev->parent->driver->owner));
 
 	return dev ? to_role_switch(dev) : NULL;
 }
Signature · 1 line
2.30.2

thanks,

Signature · 1 line
heikki