diff mbox series

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

Message ID 20210408203611.544005-1-hdegoede@redhat.com
State New
Headers show
Series usb: roles: Cakk try_module_get() from usb_role_switch_find_by_fwnode() | expand

Commit Message

Hans de Goede April 8, 2021, 8:36 p.m. UTC
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.

Signed-off-by: Hans de Goede <hdegoede@redhat.com>
---
 drivers/usb/roles/class.c | 2 ++
 1 file changed, 2 insertions(+)

Comments

Guenter Roeck April 8, 2021, 11:09 p.m. UTC | #1
On Thu, Apr 08, 2021 at 10:36:11PM +0200, Hans de Goede wrote:
> 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.
> 

Took me a while to figure out what the subject line is supposed
to mean.

s/Cakk/Call/

Otherwise

Reviewed-by: Guenter Roeck <linux@roeck-us.net>

It might be useful though to explain the difference between
fwnode_usb_role_switch_get() and usb_role_switch_find_by_fwnode(),
and why two different functions are needed, both passing fwnode
as parameter and returning a pointer to usb_role_switch.

Thanks,
Guenter

> Signed-off-by: Hans de Goede <hdegoede@redhat.com>
> ---
>  drivers/usb/roles/class.c | 2 ++
>  1 file changed, 2 insertions(+)
> 
> diff --git a/drivers/usb/roles/class.c b/drivers/usb/roles/class.c
> index 97f37077b7f9..33b637d0d8d9 100644
> --- a/drivers/usb/roles/class.c
> +++ b/drivers/usb/roles/class.c
> @@ -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;
>  }
> -- 
> 2.30.2
>
Heikki Krogerus April 9, 2021, 9:30 a.m. UTC | #2
On Thu, Apr 08, 2021 at 04:09:04PM -0700, Guenter Roeck wrote:
> On Thu, Apr 08, 2021 at 10:36:11PM +0200, Hans de Goede wrote:

> > 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.

> > 

> 

> Took me a while to figure out what the subject line is supposed

> to mean.

> 

> s/Cakk/Call/

> 

> Otherwise

> 

> Reviewed-by: Guenter Roeck <linux@roeck-us.net>

> 

> It might be useful though to explain the difference between

> fwnode_usb_role_switch_get() and usb_role_switch_find_by_fwnode(),

> and why two different functions are needed, both passing fwnode

> as parameter and returning a pointer to usb_role_switch.


Yes, the function names are confusing indeed. My proposal is to rename
usb_role_switch_find_by_fwnode() to fwnode_to_usb_role_switch().

I can prepare a patch for that if you guys are OK with it, or Hans,
would you prefer to send that together with this one?

Actually, shouldn't this be marked as a fix?


thanks,

-- 
heikki
Hans de Goede April 9, 2021, 12:41 p.m. UTC | #3
Hi,

On 4/9/21 11:30 AM, Heikki Krogerus wrote:
> On Thu, Apr 08, 2021 at 04:09:04PM -0700, Guenter Roeck wrote:

>> On Thu, Apr 08, 2021 at 10:36:11PM +0200, Hans de Goede wrote:

>>> 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.

>>>

>>

>> Took me a while to figure out what the subject line is supposed

>> to mean.

>>

>> s/Cakk/Call/

>>

>> Otherwise

>>

>> Reviewed-by: Guenter Roeck <linux@roeck-us.net>

>>

>> It might be useful though to explain the difference between

>> fwnode_usb_role_switch_get() and usb_role_switch_find_by_fwnode(),

>> and why two different functions are needed, both passing fwnode

>> as parameter and returning a pointer to usb_role_switch.


Sorry about thetypo, I completely missed that while preparing the patch,
fixed for v2.

> Yes, the function names are confusing indeed. My proposal is to rename

> usb_role_switch_find_by_fwnode() to fwnode_to_usb_role_switch().

> 

> I can prepare a patch for that if you guys are OK with it, or Hans,

> would you prefer to send that together with this one?


If you can send a patch to apply on top of my v2 of this patch then
that would be great.

> Actually, shouldn't this be marked as a fix?


That is a good point I've added a fixes tag for v2.

Regards,

Hans
diff mbox series

Patch

diff --git a/drivers/usb/roles/class.c b/drivers/usb/roles/class.c
index 97f37077b7f9..33b637d0d8d9 100644
--- a/drivers/usb/roles/class.c
+++ b/drivers/usb/roles/class.c
@@ -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;
 }