Linux-USB Archive on lore.kernel.org
 help / color / Atom feed
* [PATCH] usb: roles: Cakk try_module_get() from usb_role_switch_find_by_fwnode()
@ 2021-04-08 20:36 Hans de Goede
  2021-04-08 23:09 ` Guenter Roeck
  0 siblings, 1 reply; 4+ messages in thread
From: Hans de Goede @ 2021-04-08 20:36 UTC (permalink / raw)
  To: Greg Kroah-Hartman, Guenter Roeck, Heikki Krogerus
  Cc: Hans de Goede, linux-usb

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(+)

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


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH] usb: roles: Cakk try_module_get() from usb_role_switch_find_by_fwnode()
  2021-04-08 20:36 [PATCH] usb: roles: Cakk try_module_get() from usb_role_switch_find_by_fwnode() Hans de Goede
@ 2021-04-08 23:09 ` Guenter Roeck
  2021-04-09  9:30   ` Heikki Krogerus
  0 siblings, 1 reply; 4+ messages in thread
From: Guenter Roeck @ 2021-04-08 23:09 UTC (permalink / raw)
  To: Hans de Goede; +Cc: Greg Kroah-Hartman, Heikki Krogerus, linux-usb

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
> 

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH] usb: roles: Cakk try_module_get() from usb_role_switch_find_by_fwnode()
  2021-04-08 23:09 ` Guenter Roeck
@ 2021-04-09  9:30   ` Heikki Krogerus
  2021-04-09 12:41     ` Hans de Goede
  0 siblings, 1 reply; 4+ messages in thread
From: Heikki Krogerus @ 2021-04-09  9:30 UTC (permalink / raw)
  To: Guenter Roeck; +Cc: Hans de Goede, Greg Kroah-Hartman, linux-usb

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

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH] usb: roles: Cakk try_module_get() from usb_role_switch_find_by_fwnode()
  2021-04-09  9:30   ` Heikki Krogerus
@ 2021-04-09 12:41     ` Hans de Goede
  0 siblings, 0 replies; 4+ messages in thread
From: Hans de Goede @ 2021-04-09 12:41 UTC (permalink / raw)
  To: Heikki Krogerus, Guenter Roeck; +Cc: Greg Kroah-Hartman, linux-usb

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


^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, back to index

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-04-08 20:36 [PATCH] usb: roles: Cakk try_module_get() from usb_role_switch_find_by_fwnode() Hans de Goede
2021-04-08 23:09 ` Guenter Roeck
2021-04-09  9:30   ` Heikki Krogerus
2021-04-09 12:41     ` Hans de Goede

Linux-USB Archive on lore.kernel.org

Archives are clonable:
	git clone --mirror https://lore.kernel.org/linux-usb/0 linux-usb/git/0.git

	# If you have public-inbox 1.1+ installed, you may
	# initialize and index your mirror using the following commands:
	public-inbox-init -V2 linux-usb linux-usb/ https://lore.kernel.org/linux-usb \
		linux-usb@vger.kernel.org
	public-inbox-index linux-usb

Example config snippet for mirrors

Newsgroup available over NNTP:
	nntp://nntp.lore.kernel.org/org.kernel.vger.linux-usb


AGPL code for this site: git clone https://public-inbox.org/public-inbox.git