linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] USB: core: Improve unlocking of a mutex in two functions
@ 2017-11-04 20:12 SF Markus Elfring
  2017-11-04 23:25 ` Geert Uytterhoeven
  0 siblings, 1 reply; 5+ messages in thread
From: SF Markus Elfring @ 2017-11-04 20:12 UTC (permalink / raw)
  To: linux-usb, Alan Stern, Eugene Korenevsky, Geert Uytterhoeven,
	Greg Kroah-Hartman, Günter Röck, Jaejoong Kim,
	Johan Hovold, Jonathan Corbet, Mathias Nyman,
	Mauro Carvalho Chehab
  Cc: LKML, kernel-janitors

From: Markus Elfring <elfring@users.sourceforge.net>
Date: Sat, 4 Nov 2017 21:00:46 +0100

* Add jump targets so that a call of the function "mutex_unlock" is stored
  only twice in these function implementations.

* Replace five calls by goto statements.

This issue was detected by using the Coccinelle software.

Signed-off-by: Markus Elfring <elfring@users.sourceforge.net>
---
 drivers/usb/core/hub.c     |  8 ++++----
 drivers/usb/core/message.c | 18 ++++++++++--------
 2 files changed, 14 insertions(+), 12 deletions(-)

diff --git a/drivers/usb/core/hub.c b/drivers/usb/core/hub.c
index 196a0a5540ed..8f3067c2015c 100644
--- a/drivers/usb/core/hub.c
+++ b/drivers/usb/core/hub.c
@@ -5518,8 +5518,7 @@ static int usb_reset_and_verify_device(struct usb_device *udev)
 		dev_warn(&udev->dev,
 				"Busted HC?  Not enough HCD resources for "
 				"old configuration.\n");
-		mutex_unlock(hcd->bandwidth_mutex);
-		goto re_enumerate;
+		goto unlock;
 	}
 	ret = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
 			USB_REQ_SET_CONFIGURATION, 0,
@@ -5529,8 +5528,7 @@ static int usb_reset_and_verify_device(struct usb_device *udev)
 		dev_err(&udev->dev,
 			"can't restore configuration #%d (error=%d)\n",
 			udev->actconfig->desc.bConfigurationValue, ret);
-		mutex_unlock(hcd->bandwidth_mutex);
-		goto re_enumerate;
+		goto unlock;
 	}
 	mutex_unlock(hcd->bandwidth_mutex);
 	usb_set_device_state(udev, USB_STATE_CONFIGURED);
@@ -5583,6 +5581,8 @@ static int usb_reset_and_verify_device(struct usb_device *udev)
 	udev->bos = bos;
 	return 0;
 
+unlock:
+	mutex_unlock(hcd->bandwidth_mutex);
 re_enumerate:
 	usb_release_bos_descriptor(udev);
 	udev->bos = bos;
diff --git a/drivers/usb/core/message.c b/drivers/usb/core/message.c
index 371a07d874a3..fe8e36cc7def 100644
--- a/drivers/usb/core/message.c
+++ b/drivers/usb/core/message.c
@@ -1321,8 +1321,8 @@ int usb_set_interface(struct usb_device *dev, int interface, int alternate)
 	 */
 	if (usb_disable_lpm(dev)) {
 		dev_err(&iface->dev, "%s Failed to disable LPM\n.", __func__);
-		mutex_unlock(hcd->bandwidth_mutex);
-		return -ENOMEM;
+		ret = -ENOMEM;
+		goto unlock;
 	}
 	/* Changing alt-setting also frees any allocated streams */
 	for (i = 0; i < iface->cur_altsetting->desc.bNumEndpoints; i++)
@@ -1332,9 +1332,7 @@ int usb_set_interface(struct usb_device *dev, int interface, int alternate)
 	if (ret < 0) {
 		dev_info(&dev->dev, "Not enough bandwidth for altsetting %d\n",
 				alternate);
-		usb_enable_lpm(dev);
-		mutex_unlock(hcd->bandwidth_mutex);
-		return ret;
+		goto enable_lpm;
 	}
 
 	if (dev->quirks & USB_QUIRK_NO_SET_INTF)
@@ -1355,9 +1353,7 @@ int usb_set_interface(struct usb_device *dev, int interface, int alternate)
 	} else if (ret < 0) {
 		/* Re-instate the old alt setting */
 		usb_hcd_alloc_bandwidth(dev, NULL, alt, iface->cur_altsetting);
-		usb_enable_lpm(dev);
-		mutex_unlock(hcd->bandwidth_mutex);
-		return ret;
+		goto enable_lpm;
 	}
 	mutex_unlock(hcd->bandwidth_mutex);
 
@@ -1413,6 +1409,12 @@ int usb_set_interface(struct usb_device *dev, int interface, int alternate)
 		create_intf_ep_devs(iface);
 	}
 	return 0;
+
+enable_lpm:
+	usb_enable_lpm(dev);
+unlock:
+	mutex_unlock(hcd->bandwidth_mutex);
+	return ret;
 }
 EXPORT_SYMBOL_GPL(usb_set_interface);
 
-- 
2.15.0

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

* Re: [PATCH] USB: core: Improve unlocking of a mutex in two functions
  2017-11-04 20:12 [PATCH] USB: core: Improve unlocking of a mutex in two functions SF Markus Elfring
@ 2017-11-04 23:25 ` Geert Uytterhoeven
  2017-11-05  7:30   ` Greg Kroah-Hartman
  2017-11-05  8:29   ` [PATCH] " SF Markus Elfring
  0 siblings, 2 replies; 5+ messages in thread
From: Geert Uytterhoeven @ 2017-11-04 23:25 UTC (permalink / raw)
  To: SF Markus Elfring
  Cc: USB list, Alan Stern, Eugene Korenevsky, Greg Kroah-Hartman,
	Günter Röck, Jaejoong Kim, Johan Hovold,
	Jonathan Corbet, Mathias Nyman, Mauro Carvalho Chehab, LKML,
	kernel-janitors

On Sat, Nov 4, 2017 at 9:12 PM, SF Markus Elfring
<elfring@users.sourceforge.net> wrote:
> From: Markus Elfring <elfring@users.sourceforge.net>
> Date: Sat, 4 Nov 2017 21:00:46 +0100
>
> * Add jump targets so that a call of the function "mutex_unlock" is stored
>   only twice in these function implementations.
>
> * Replace five calls by goto statements.
>
> This issue was detected by using the Coccinelle software.
>
> Signed-off-by: Markus Elfring <elfring@users.sourceforge.net>

NAKed-by: Geert Uytterhoeven <geert@linux-m68k.org>

> ---
>  drivers/usb/core/hub.c     |  8 ++++----
>  drivers/usb/core/message.c | 18 ++++++++++--------
>  2 files changed, 14 insertions(+), 12 deletions(-)
>
> diff --git a/drivers/usb/core/hub.c b/drivers/usb/core/hub.c
> index 196a0a5540ed..8f3067c2015c 100644
> --- a/drivers/usb/core/hub.c
> +++ b/drivers/usb/core/hub.c
> @@ -5518,8 +5518,7 @@ static int usb_reset_and_verify_device(struct usb_device *udev)
>                 dev_warn(&udev->dev,
>                                 "Busted HC?  Not enough HCD resources for "
>                                 "old configuration.\n");
> -               mutex_unlock(hcd->bandwidth_mutex);
> -               goto re_enumerate;
> +               goto unlock;
>         }
>         ret = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
>                         USB_REQ_SET_CONFIGURATION, 0,
> @@ -5529,8 +5528,7 @@ static int usb_reset_and_verify_device(struct usb_device *udev)
>                 dev_err(&udev->dev,
>                         "can't restore configuration #%d (error=%d)\n",
>                         udev->actconfig->desc.bConfigurationValue, ret);
> -               mutex_unlock(hcd->bandwidth_mutex);
> -               goto re_enumerate;
> +               goto unlock;
>         }
>         mutex_unlock(hcd->bandwidth_mutex);
>         usb_set_device_state(udev, USB_STATE_CONFIGURED);
> @@ -5583,6 +5581,8 @@ static int usb_reset_and_verify_device(struct usb_device *udev)
>         udev->bos = bos;
>         return 0;
>
> +unlock:
> +       mutex_unlock(hcd->bandwidth_mutex);

This makes it harder for the reader, as the mutex_unlock() is now far
below the block
of code that's protected by the lock.

>  re_enumerate:
>         usb_release_bos_descriptor(udev);
>         udev->bos = bos;
> diff --git a/drivers/usb/core/message.c b/drivers/usb/core/message.c
> index 371a07d874a3..fe8e36cc7def 100644
> --- a/drivers/usb/core/message.c
> +++ b/drivers/usb/core/message.c
> @@ -1321,8 +1321,8 @@ int usb_set_interface(struct usb_device *dev, int interface, int alternate)
>          */
>         if (usb_disable_lpm(dev)) {
>                 dev_err(&iface->dev, "%s Failed to disable LPM\n.", __func__);
> -               mutex_unlock(hcd->bandwidth_mutex);
> -               return -ENOMEM;
> +               ret = -ENOMEM;
> +               goto unlock;
>         }
>         /* Changing alt-setting also frees any allocated streams */
>         for (i = 0; i < iface->cur_altsetting->desc.bNumEndpoints; i++)
> @@ -1332,9 +1332,7 @@ int usb_set_interface(struct usb_device *dev, int interface, int alternate)
>         if (ret < 0) {
>                 dev_info(&dev->dev, "Not enough bandwidth for altsetting %d\n",
>                                 alternate);
> -               usb_enable_lpm(dev);
> -               mutex_unlock(hcd->bandwidth_mutex);
> -               return ret;
> +               goto enable_lpm;
>         }
>
>         if (dev->quirks & USB_QUIRK_NO_SET_INTF)
> @@ -1355,9 +1353,7 @@ int usb_set_interface(struct usb_device *dev, int interface, int alternate)
>         } else if (ret < 0) {
>                 /* Re-instate the old alt setting */
>                 usb_hcd_alloc_bandwidth(dev, NULL, alt, iface->cur_altsetting);
> -               usb_enable_lpm(dev);
> -               mutex_unlock(hcd->bandwidth_mutex);
> -               return ret;
> +               goto enable_lpm;
>         }
>         mutex_unlock(hcd->bandwidth_mutex);
>
> @@ -1413,6 +1409,12 @@ int usb_set_interface(struct usb_device *dev, int interface, int alternate)
>                 create_intf_ep_devs(iface);
>         }
>         return 0;
> +
> +enable_lpm:
> +       usb_enable_lpm(dev);
> +unlock:
> +       mutex_unlock(hcd->bandwidth_mutex);

Likewise.

> +       return ret;
>  }
>  EXPORT_SYMBOL_GPL(usb_set_interface);

Gr{oetje,eeting}s,

                        Geert

--
Geert Uytterhoeven -- There's lots of Linux beyond ia32 -- geert@linux-m68k.org

In personal conversations with technical people, I call myself a hacker. But
when I'm talking to journalists I just say "programmer" or something like that.
                                -- Linus Torvalds

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

* Re: [PATCH] USB: core: Improve unlocking of a mutex in two functions
  2017-11-04 23:25 ` Geert Uytterhoeven
@ 2017-11-05  7:30   ` Greg Kroah-Hartman
  2017-11-05  9:33     ` SF Markus Elfring
  2017-11-05  8:29   ` [PATCH] " SF Markus Elfring
  1 sibling, 1 reply; 5+ messages in thread
From: Greg Kroah-Hartman @ 2017-11-05  7:30 UTC (permalink / raw)
  To: Geert Uytterhoeven
  Cc: SF Markus Elfring, USB list, Alan Stern, Eugene Korenevsky,
	Günter Röck, Jaejoong Kim, Johan Hovold,
	Jonathan Corbet, Mathias Nyman, Mauro Carvalho Chehab, LKML,
	kernel-janitors

On Sun, Nov 05, 2017 at 12:25:19AM +0100, Geert Uytterhoeven wrote:
> On Sat, Nov 4, 2017 at 9:12 PM, SF Markus Elfring
> <elfring@users.sourceforge.net> wrote:
> > From: Markus Elfring <elfring@users.sourceforge.net>
> > Date: Sat, 4 Nov 2017 21:00:46 +0100
> >
> > * Add jump targets so that a call of the function "mutex_unlock" is stored
> >   only twice in these function implementations.
> >
> > * Replace five calls by goto statements.
> >
> > This issue was detected by using the Coccinelle software.
> >
> > Signed-off-by: Markus Elfring <elfring@users.sourceforge.net>
> 
> NAKed-by: Geert Uytterhoeven <geert@linux-m68k.org>

Don't worry, or waste your time, I don't take patches from this author
as they are in my blacklist.

greg k-h

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

* Re: [PATCH] USB: core: Improve unlocking of a mutex in two functions
  2017-11-04 23:25 ` Geert Uytterhoeven
  2017-11-05  7:30   ` Greg Kroah-Hartman
@ 2017-11-05  8:29   ` SF Markus Elfring
  1 sibling, 0 replies; 5+ messages in thread
From: SF Markus Elfring @ 2017-11-05  8:29 UTC (permalink / raw)
  To: Geert Uytterhoeven, linux-usb, kernel-janitors
  Cc: Alan Stern, Eugene Korenevsky, Greg Kroah-Hartman,
	Günter Röck, Jaejoong Kim, Johan Hovold,
	Jonathan Corbet, Mathias Nyman, Mauro Carvalho Chehab, LKML

>> @@ -5529,8 +5528,7 @@ static int usb_reset_and_verify_device(struct usb_device *udev)
>>                 dev_err(&udev->dev,
>>                         "can't restore configuration #%d (error=%d)\n",
>>                         udev->actconfig->desc.bConfigurationValue, ret);
>> -               mutex_unlock(hcd->bandwidth_mutex);
>> -               goto re_enumerate;
>> +               goto unlock;
>>         }
>>         mutex_unlock(hcd->bandwidth_mutex);
>>         usb_set_device_state(udev, USB_STATE_CONFIGURED);
>> @@ -5583,6 +5581,8 @@ static int usb_reset_and_verify_device(struct usb_device *udev)
>>         udev->bos = bos;
>>         return 0;
>>
>> +unlock:
>> +       mutex_unlock(hcd->bandwidth_mutex);
> 
> This makes it harder for the reader,

I am curious if the view on the preferred code readability can be clarified further.


> as the mutex_unlock() is now far below the block
> of code that's protected by the lock.

I got an other software development opinion for this aspect.
Can the label be clear enough about the shown purpose already?

Regards,
Markus

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

* Re: USB: core: Improve unlocking of a mutex in two functions
  2017-11-05  7:30   ` Greg Kroah-Hartman
@ 2017-11-05  9:33     ` SF Markus Elfring
  0 siblings, 0 replies; 5+ messages in thread
From: SF Markus Elfring @ 2017-11-05  9:33 UTC (permalink / raw)
  To: Greg Kroah-Hartman, linux-usb, kernel-janitors
  Cc: Geert Uytterhoeven, Alan Stern, Eugene Korenevsky,
	Günter Röck, Jaejoong Kim, Johan Hovold,
	Jonathan Corbet, Mathias Nyman, Mauro Carvalho Chehab, LKML

> Don't worry, or waste your time, I don't take patches from this author
> as they are in my blacklist.

I am curious if our dialogue can become more constructive again. 🌈


I can offer another bit of information for this software development discussion. 💭

The affected source files can be compiled for the processor architecture “x86_64”
by a tool like “GCC 6.4.1+r251631-1.3” from the software distribution
“openSUSE Tumbleweed” with the following command example.

my_cc=/usr/bin/gcc-6 \
&& my_module1=drivers/usb/core/hub.o \
&& my_module2=drivers/usb/core/message.o \
&& git checkout next-20171102 \
&& make -j4 CC="${my_cc}" HOSTCC="${my_cc}" allmodconfig "${my_module1}" "${my_module2}" \
&& size "${my_module1}" "${my_module2}" \
&& git checkout ':/^USB: core: Improve unlocking of a mutex in two functions' \
&& make -j4 CC="${my_cc}" HOSTCC="${my_cc}" allmodconfig "${my_module1}" "${my_module2}" \
&& size "${my_module1}" "${my_module2}"


🔮 Do you find the following differences useful for further clarification?

╔═════════╤══════╗
║ module  │ text ║
╠═════════╪══════╣
║ hub     │ -16  ║
║ message │ -48  ║
╚═════════╧══════╝


Regards,
Markus

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

end of thread, other threads:[~2017-11-05  9:34 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2017-11-04 20:12 [PATCH] USB: core: Improve unlocking of a mutex in two functions SF Markus Elfring
2017-11-04 23:25 ` Geert Uytterhoeven
2017-11-05  7:30   ` Greg Kroah-Hartman
2017-11-05  9:33     ` SF Markus Elfring
2017-11-05  8:29   ` [PATCH] " SF Markus Elfring

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).