Skip to content

Commit 1c9be13

Browse files
Xu Yanggregkh
authored andcommitted
usb: roles: fix NULL pointer issue when put module's reference
In current design, usb role class driver will get usb_role_switch parent's module reference after the user get usb_role_switch device and put the reference after the user put the usb_role_switch device. However, the parent device of usb_role_switch may be removed before the user put the usb_role_switch. If so, then, NULL pointer issue will be met when the user put the parent module's reference. This will save the module pointer in structure of usb_role_switch. Then, we don't need to find module by iterating long relations. Fixes: 5c54fca ("usb: roles: Take care of driver module reference counting") cc: stable@vger.kernel.org Signed-off-by: Xu Yang <xu.yang_2@nxp.com> Acked-by: Heikki Krogerus <heikki.krogerus@linux.intel.com> Link: https://lore.kernel.org/r/20240129093739.2371530-1-xu.yang_2@nxp.com Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
1 parent 47625b0 commit 1c9be13

File tree

1 file changed

+11
-6
lines changed

1 file changed

+11
-6
lines changed

drivers/usb/roles/class.c

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ static const struct class role_class = {
2121
struct usb_role_switch {
2222
struct device dev;
2323
struct mutex lock; /* device lock*/
24+
struct module *module; /* the module this device depends on */
2425
enum usb_role role;
2526

2627
/* From descriptor */
@@ -135,7 +136,7 @@ struct usb_role_switch *usb_role_switch_get(struct device *dev)
135136
usb_role_switch_match);
136137

137138
if (!IS_ERR_OR_NULL(sw))
138-
WARN_ON(!try_module_get(sw->dev.parent->driver->owner));
139+
WARN_ON(!try_module_get(sw->module));
139140

140141
return sw;
141142
}
@@ -157,7 +158,7 @@ struct usb_role_switch *fwnode_usb_role_switch_get(struct fwnode_handle *fwnode)
157158
sw = fwnode_connection_find_match(fwnode, "usb-role-switch",
158159
NULL, usb_role_switch_match);
159160
if (!IS_ERR_OR_NULL(sw))
160-
WARN_ON(!try_module_get(sw->dev.parent->driver->owner));
161+
WARN_ON(!try_module_get(sw->module));
161162

162163
return sw;
163164
}
@@ -172,7 +173,7 @@ EXPORT_SYMBOL_GPL(fwnode_usb_role_switch_get);
172173
void usb_role_switch_put(struct usb_role_switch *sw)
173174
{
174175
if (!IS_ERR_OR_NULL(sw)) {
175-
module_put(sw->dev.parent->driver->owner);
176+
module_put(sw->module);
176177
put_device(&sw->dev);
177178
}
178179
}
@@ -189,15 +190,18 @@ struct usb_role_switch *
189190
usb_role_switch_find_by_fwnode(const struct fwnode_handle *fwnode)
190191
{
191192
struct device *dev;
193+
struct usb_role_switch *sw = NULL;
192194

193195
if (!fwnode)
194196
return NULL;
195197

196198
dev = class_find_device_by_fwnode(&role_class, fwnode);
197-
if (dev)
198-
WARN_ON(!try_module_get(dev->parent->driver->owner));
199+
if (dev) {
200+
sw = to_role_switch(dev);
201+
WARN_ON(!try_module_get(sw->module));
202+
}
199203

200-
return dev ? to_role_switch(dev) : NULL;
204+
return sw;
201205
}
202206
EXPORT_SYMBOL_GPL(usb_role_switch_find_by_fwnode);
203207

@@ -338,6 +342,7 @@ usb_role_switch_register(struct device *parent,
338342
sw->set = desc->set;
339343
sw->get = desc->get;
340344

345+
sw->module = parent->driver->owner;
341346
sw->dev.parent = parent;
342347
sw->dev.fwnode = desc->fwnode;
343348
sw->dev.class = &role_class;

0 commit comments

Comments
 (0)