linux-usb.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] usb: hub: Minor refactoring in usb_hub_init()
@ 2019-09-23  6:19 Austin Kim
  2019-09-23  7:11 ` Greg KH
  0 siblings, 1 reply; 4+ messages in thread
From: Austin Kim @ 2019-09-23  6:19 UTC (permalink / raw)
  To: gregkh, stern
  Cc: linux-kernel, linux-usb, mathias.nyman, Thinh.Nguyen,
	nsaenzjulienne, jflat, malat, dianders, austindh.kim

Normally when creation of workqueue fails, exception handling takes place
after the call to alloc_workqueue() is made.

But looking into usb_hub_init() function, 'return 0' statement is executed,
when alloc_workqueue() returns valid workqueue pointer.
       if (hub_wq)
               return 0;

This might make other Linux driver developers get confused 
because they could deduce that this is exceptional handling routine.

So perform minor refactoring by adding NULL pointer dereference check
routine right after the call to alloc_workqueue() is made.

Signed-off-by: Austin Kim <austindh.kim@gmail.com>
---
 drivers/usb/core/hub.c | 7 +++++--
 1 file changed, 5 insertions(+), 2 deletions(-)

diff --git a/drivers/usb/core/hub.c b/drivers/usb/core/hub.c
index e8ebacc..0ddbfe6 100644
--- a/drivers/usb/core/hub.c
+++ b/drivers/usb/core/hub.c
@@ -5530,9 +5530,12 @@ int usb_hub_init(void)
 	 * over to the companion full-speed controller.
 	 */
 	hub_wq = alloc_workqueue("usb_hub_wq", WQ_FREEZABLE, 0);
-	if (hub_wq)
-		return 0;
+	if (unlikely(!hub_wq))
+		goto err_workqueue;
+
+	return 0;
 
+err_workqueue:
 	/* Fall through if kernel_thread failed */
 	usb_deregister(&hub_driver);
 	pr_err("%s: can't allocate workqueue for usb hub\n", usbcore_name);
-- 
2.6.2


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

* Re: [PATCH] usb: hub: Minor refactoring in usb_hub_init()
  2019-09-23  6:19 [PATCH] usb: hub: Minor refactoring in usb_hub_init() Austin Kim
@ 2019-09-23  7:11 ` Greg KH
  2019-09-23 14:58   ` Alan Stern
  0 siblings, 1 reply; 4+ messages in thread
From: Greg KH @ 2019-09-23  7:11 UTC (permalink / raw)
  To: Austin Kim
  Cc: stern, linux-kernel, linux-usb, mathias.nyman, Thinh.Nguyen,
	nsaenzjulienne, jflat, malat, dianders

On Mon, Sep 23, 2019 at 03:19:21PM +0900, Austin Kim wrote:
> Normally when creation of workqueue fails, exception handling takes place
> after the call to alloc_workqueue() is made.
> 
> But looking into usb_hub_init() function, 'return 0' statement is executed,
> when alloc_workqueue() returns valid workqueue pointer.
>        if (hub_wq)
>                return 0;
> 
> This might make other Linux driver developers get confused 
> because they could deduce that this is exceptional handling routine.
> 
> So perform minor refactoring by adding NULL pointer dereference check
> routine right after the call to alloc_workqueue() is made.
> 
> Signed-off-by: Austin Kim <austindh.kim@gmail.com>
> ---
>  drivers/usb/core/hub.c | 7 +++++--
>  1 file changed, 5 insertions(+), 2 deletions(-)
> 
> diff --git a/drivers/usb/core/hub.c b/drivers/usb/core/hub.c
> index e8ebacc..0ddbfe6 100644
> --- a/drivers/usb/core/hub.c
> +++ b/drivers/usb/core/hub.c
> @@ -5530,9 +5530,12 @@ int usb_hub_init(void)
>  	 * over to the companion full-speed controller.
>  	 */
>  	hub_wq = alloc_workqueue("usb_hub_wq", WQ_FREEZABLE, 0);
> -	if (hub_wq)
> -		return 0;
> +	if (unlikely(!hub_wq))

Only ever use likely/unlikely if you can measure the difference without
it.  Otherwise the compiler and cpu will almost always do this better
than you.

So please remove this.

> +		goto err_workqueue;
> +
> +	return 0;
>  
> +err_workqueue:
>  	/* Fall through if kernel_thread failed */

This comment is now incorrect.

But really, there is nothing wrong with the original code here.  It
works properly, and while it is not identical to normal code "style"
here, there's nothing wrong with it that I can see.

thanks,

greg k-h

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

* Re: [PATCH] usb: hub: Minor refactoring in usb_hub_init()
  2019-09-23  7:11 ` Greg KH
@ 2019-09-23 14:58   ` Alan Stern
  2019-09-25 10:10     ` Austin Kim
  0 siblings, 1 reply; 4+ messages in thread
From: Alan Stern @ 2019-09-23 14:58 UTC (permalink / raw)
  To: Greg KH
  Cc: Austin Kim, linux-kernel, linux-usb, mathias.nyman, Thinh.Nguyen,
	nsaenzjulienne, jflat, malat, dianders

On Mon, 23 Sep 2019, Greg KH wrote:

> On Mon, Sep 23, 2019 at 03:19:21PM +0900, Austin Kim wrote:
> > Normally when creation of workqueue fails, exception handling takes place
> > after the call to alloc_workqueue() is made.
> > 
> > But looking into usb_hub_init() function, 'return 0' statement is executed,
> > when alloc_workqueue() returns valid workqueue pointer.
> >        if (hub_wq)
> >                return 0;
> > 
> > This might make other Linux driver developers get confused 
> > because they could deduce that this is exceptional handling routine.
> > 
> > So perform minor refactoring by adding NULL pointer dereference check
> > routine right after the call to alloc_workqueue() is made.
> > 
> > Signed-off-by: Austin Kim <austindh.kim@gmail.com>
> > ---
> >  drivers/usb/core/hub.c | 7 +++++--
> >  1 file changed, 5 insertions(+), 2 deletions(-)
> > 
> > diff --git a/drivers/usb/core/hub.c b/drivers/usb/core/hub.c
> > index e8ebacc..0ddbfe6 100644
> > --- a/drivers/usb/core/hub.c
> > +++ b/drivers/usb/core/hub.c
> > @@ -5530,9 +5530,12 @@ int usb_hub_init(void)
> >  	 * over to the companion full-speed controller.
> >  	 */
> >  	hub_wq = alloc_workqueue("usb_hub_wq", WQ_FREEZABLE, 0);
> > -	if (hub_wq)
> > -		return 0;
> > +	if (unlikely(!hub_wq))
> 
> Only ever use likely/unlikely if you can measure the difference without
> it.  Otherwise the compiler and cpu will almost always do this better
> than you.
> 
> So please remove this.
> 
> > +		goto err_workqueue;
> > +
> > +	return 0;
> >  
> > +err_workqueue:
> >  	/* Fall through if kernel_thread failed */
> 
> This comment is now incorrect.
> 
> But really, there is nothing wrong with the original code here.  It
> works properly, and while it is not identical to normal code "style"
> here, there's nothing wrong with it that I can see.

Indeed.  In fact, I suspect that this change would make the code less 
understandable, because the reader would wonder why anybody would go to 
the trouble of jumping over a return statement.  After all, this:

	if (!test)
		jump error;
	return 0;
 error:

just looks like a strange and inefficient way of writing:

	if (test)
		return 0;

Anyone reading it would wonder what the original author was thinking.

If you really want to fix up this subroutine, you could change the two 
"return -1" statements.  They should return an appropriate error code, 
not just -1.

Alan Stern


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

* Re: [PATCH] usb: hub: Minor refactoring in usb_hub_init()
  2019-09-23 14:58   ` Alan Stern
@ 2019-09-25 10:10     ` Austin Kim
  0 siblings, 0 replies; 4+ messages in thread
From: Austin Kim @ 2019-09-25 10:10 UTC (permalink / raw)
  To: Alan Stern
  Cc: Greg KH, linux-kernel, linux-usb, mathias.nyman, Thinh.Nguyen,
	nsaenzjulienne, jflat, malat, Douglas Anderson

> If you really want to fix up this subroutine, you could change the two
> "return -1" statements.  They should return an appropriate error code,
> not just -1.
>
> Alan Stern
>

Thanks for valuable feedback over the patch.
If I generate new patch later, let me make sure to contain an
appropriate error code.

Thanks,
Austin Kim

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

end of thread, other threads:[~2019-09-25 10:10 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2019-09-23  6:19 [PATCH] usb: hub: Minor refactoring in usb_hub_init() Austin Kim
2019-09-23  7:11 ` Greg KH
2019-09-23 14:58   ` Alan Stern
2019-09-25 10:10     ` Austin Kim

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