linux-input.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] Input: pxrc - simplify mutex handling with guard macro
@ 2023-12-01 12:08 Marcus Folkesson
  2023-12-01 13:29 ` Johan Hovold
  0 siblings, 1 reply; 5+ messages in thread
From: Marcus Folkesson @ 2023-12-01 12:08 UTC (permalink / raw)
  To: Dmitry Torokhov; +Cc: linux-input, linux-kernel, Marcus Folkesson

Use the guard(mutex) macro for handle mutex lock/unlocks.

Signed-off-by: Marcus Folkesson <marcus.folkesson@gmail.com>
---
 drivers/input/joystick/pxrc.c | 27 +++++++++++----------------
 1 file changed, 11 insertions(+), 16 deletions(-)

diff --git a/drivers/input/joystick/pxrc.c b/drivers/input/joystick/pxrc.c
index ea2bf5951d67..3c3bf7179b46 100644
--- a/drivers/input/joystick/pxrc.c
+++ b/drivers/input/joystick/pxrc.c
@@ -5,15 +5,17 @@
  * Copyright (C) 2018 Marcus Folkesson <marcus.folkesson@gmail.com>
  */
 
-#include <linux/kernel.h>
+#include <linux/cleanup.h>
 #include <linux/errno.h>
-#include <linux/slab.h>
+#include <linux/input.h>
+#include <linux/kernel.h>
 #include <linux/module.h>
+#include <linux/mutex.h>
+#include <linux/slab.h>
 #include <linux/uaccess.h>
+
 #include <linux/usb.h>
 #include <linux/usb/input.h>
-#include <linux/mutex.h>
-#include <linux/input.h>
 
 #define PXRC_VENDOR_ID		0x1781
 #define PXRC_PRODUCT_ID		0x0898
@@ -89,25 +91,20 @@ static int pxrc_open(struct input_dev *input)
 		dev_err(&pxrc->intf->dev,
 			"%s - usb_submit_urb failed, error: %d\n",
 			__func__, retval);
-		retval = -EIO;
-		goto out;
+		return -EIO;
 	}
 
 	pxrc->is_open = true;
-
-out:
-	mutex_unlock(&pxrc->pm_mutex);
-	return retval;
+	return 0;
 }
 
 static void pxrc_close(struct input_dev *input)
 {
 	struct pxrc *pxrc = input_get_drvdata(input);
 
-	mutex_lock(&pxrc->pm_mutex);
+	guard(mutex)(&pxrc->pm_mutex);
 	usb_kill_urb(pxrc->urb);
 	pxrc->is_open = false;
-	mutex_unlock(&pxrc->pm_mutex);
 }
 
 static void pxrc_free_urb(void *_pxrc)
@@ -208,10 +205,9 @@ static int pxrc_suspend(struct usb_interface *intf, pm_message_t message)
 {
 	struct pxrc *pxrc = usb_get_intfdata(intf);
 
-	mutex_lock(&pxrc->pm_mutex);
+	guard(mutex)(&pxrc->pm_mutex);
 	if (pxrc->is_open)
 		usb_kill_urb(pxrc->urb);
-	mutex_unlock(&pxrc->pm_mutex);
 
 	return 0;
 }
@@ -221,11 +217,10 @@ static int pxrc_resume(struct usb_interface *intf)
 	struct pxrc *pxrc = usb_get_intfdata(intf);
 	int retval = 0;
 
-	mutex_lock(&pxrc->pm_mutex);
+	guard(mutex)(&pxrc->pm_mutex);
 	if (pxrc->is_open && usb_submit_urb(pxrc->urb, GFP_KERNEL) < 0)
 		retval = -EIO;
 
-	mutex_unlock(&pxrc->pm_mutex);
 	return retval;
 }
 

---
base-commit: ffc253263a1375a65fa6c9f62a893e9767fbebfa
change-id: 20231201-pxrc-guard-03dc35771b36

Best regards,
-- 
Marcus Folkesson <marcus.folkesson@gmail.com>


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

* Re: [PATCH] Input: pxrc - simplify mutex handling with guard macro
  2023-12-01 12:08 [PATCH] Input: pxrc - simplify mutex handling with guard macro Marcus Folkesson
@ 2023-12-01 13:29 ` Johan Hovold
  2023-12-01 13:48   ` Marcus Folkesson
  2023-12-01 23:33   ` Dmitry Torokhov
  0 siblings, 2 replies; 5+ messages in thread
From: Johan Hovold @ 2023-12-01 13:29 UTC (permalink / raw)
  To: Marcus Folkesson; +Cc: Dmitry Torokhov, linux-input, linux-kernel

On Fri, Dec 01, 2023 at 01:08:45PM +0100, Marcus Folkesson wrote:
> Use the guard(mutex) macro for handle mutex lock/unlocks.
> 
> Signed-off-by: Marcus Folkesson <marcus.folkesson@gmail.com>

A couple of drive-by comments below.

> ---
>  drivers/input/joystick/pxrc.c | 27 +++++++++++----------------
>  1 file changed, 11 insertions(+), 16 deletions(-)
> 
> diff --git a/drivers/input/joystick/pxrc.c b/drivers/input/joystick/pxrc.c
> index ea2bf5951d67..3c3bf7179b46 100644
> --- a/drivers/input/joystick/pxrc.c
> +++ b/drivers/input/joystick/pxrc.c
> @@ -5,15 +5,17 @@
>   * Copyright (C) 2018 Marcus Folkesson <marcus.folkesson@gmail.com>
>   */
>  
> -#include <linux/kernel.h>
> +#include <linux/cleanup.h>
>  #include <linux/errno.h>
> -#include <linux/slab.h>
> +#include <linux/input.h>
> +#include <linux/kernel.h>
>  #include <linux/module.h>
> +#include <linux/mutex.h>
> +#include <linux/slab.h>
>  #include <linux/uaccess.h>
> +
>  #include <linux/usb.h>
>  #include <linux/usb/input.h>
> -#include <linux/mutex.h>
> -#include <linux/input.h>

Looks like an unrelated change.
  
>  #define PXRC_VENDOR_ID		0x1781
>  #define PXRC_PRODUCT_ID		0x0898
> @@ -89,25 +91,20 @@ static int pxrc_open(struct input_dev *input)
>  		dev_err(&pxrc->intf->dev,
>  			"%s - usb_submit_urb failed, error: %d\n",
>  			__func__, retval);
> -		retval = -EIO;
> -		goto out;
> +		return -EIO;
>  	}
>  
>  	pxrc->is_open = true;
> -
> -out:
> -	mutex_unlock(&pxrc->pm_mutex);
> -	return retval;
> +	return 0;
>  }

Eh, this looks obviously broken. Did you not test this before
submitting? I assume lockdep would complain loudly too.

You're apparently the author of this driver and can test it, but I fear
the coming onslaught of untested guard conversions from the "cleanup"
crew. Not sure I find the result generally more readable either.

Johan

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

* Re: [PATCH] Input: pxrc - simplify mutex handling with guard macro
  2023-12-01 13:29 ` Johan Hovold
@ 2023-12-01 13:48   ` Marcus Folkesson
  2023-12-01 14:03     ` Johan Hovold
  2023-12-01 23:33   ` Dmitry Torokhov
  1 sibling, 1 reply; 5+ messages in thread
From: Marcus Folkesson @ 2023-12-01 13:48 UTC (permalink / raw)
  To: Johan Hovold; +Cc: Dmitry Torokhov, linux-input, linux-kernel

[-- Attachment #1: Type: text/plain, Size: 2467 bytes --]

On Fri, Dec 01, 2023 at 02:29:22PM +0100, Johan Hovold wrote:
> On Fri, Dec 01, 2023 at 01:08:45PM +0100, Marcus Folkesson wrote:
> > Use the guard(mutex) macro for handle mutex lock/unlocks.
> > 
> > Signed-off-by: Marcus Folkesson <marcus.folkesson@gmail.com>
> 
> A couple of drive-by comments below.
> 
> > ---
> >  drivers/input/joystick/pxrc.c | 27 +++++++++++----------------
> >  1 file changed, 11 insertions(+), 16 deletions(-)
> > 
> > diff --git a/drivers/input/joystick/pxrc.c b/drivers/input/joystick/pxrc.c
> > index ea2bf5951d67..3c3bf7179b46 100644
> > --- a/drivers/input/joystick/pxrc.c
> > +++ b/drivers/input/joystick/pxrc.c
> > @@ -5,15 +5,17 @@
> >   * Copyright (C) 2018 Marcus Folkesson <marcus.folkesson@gmail.com>
> >   */
> >  
> > -#include <linux/kernel.h>
> > +#include <linux/cleanup.h>
> >  #include <linux/errno.h>
> > -#include <linux/slab.h>
> > +#include <linux/input.h>
> > +#include <linux/kernel.h>
> >  #include <linux/module.h>
> > +#include <linux/mutex.h>
> > +#include <linux/slab.h>
> >  #include <linux/uaccess.h>
> > +
> >  #include <linux/usb.h>
> >  #include <linux/usb/input.h>
> > -#include <linux/mutex.h>
> > -#include <linux/input.h>
> 
> Looks like an unrelated change.

I reordered the include files as I added cleanup.h.
I can do it in a separate patch if that is preferred.

>   
> >  #define PXRC_VENDOR_ID		0x1781
> >  #define PXRC_PRODUCT_ID		0x0898
> > @@ -89,25 +91,20 @@ static int pxrc_open(struct input_dev *input)
> >  		dev_err(&pxrc->intf->dev,
> >  			"%s - usb_submit_urb failed, error: %d\n",
> >  			__func__, retval);
> > -		retval = -EIO;
> > -		goto out;
> > +		return -EIO;
> >  	}
> >  
> >  	pxrc->is_open = true;
> > -
> > -out:
> > -	mutex_unlock(&pxrc->pm_mutex);
> > -	return retval;
> > +	return 0;
> >  }
> 
> Eh, this looks obviously broken. Did you not test this before
> submitting? I assume lockdep would complain loudly too.

Sorry, it is more that I'm not in the habit of using b4 for submitting
patches yet, so things got wrong.
There is a v2 out there.

The driver (v2) is quickly tested on HW and, what I can tell, seems to behave.

> 
> You're apparently the author of this driver and can test it, but I fear
> the coming onslaught of untested guard conversions from the "cleanup"
> crew. Not sure I find the result generally more readable either.
> 
> Johan


Best regards
Marcus Folkesson

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 833 bytes --]

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

* Re: [PATCH] Input: pxrc - simplify mutex handling with guard macro
  2023-12-01 13:48   ` Marcus Folkesson
@ 2023-12-01 14:03     ` Johan Hovold
  0 siblings, 0 replies; 5+ messages in thread
From: Johan Hovold @ 2023-12-01 14:03 UTC (permalink / raw)
  To: Marcus Folkesson; +Cc: Dmitry Torokhov, linux-input, linux-kernel

[-- Attachment #1: Type: text/plain, Size: 449 bytes --]

On Fri, Dec 01, 2023 at 02:48:06PM +0100, Marcus Folkesson wrote:
> On Fri, Dec 01, 2023 at 02:29:22PM +0100, Johan Hovold wrote:

> > Eh, this looks obviously broken. Did you not test this before
> > submitting? I assume lockdep would complain loudly too.
> 
> Sorry, it is more that I'm not in the habit of using b4 for submitting
> patches yet, so things got wrong.
> There is a v2 out there.

Ah, good, you noticed yourself.

Johan

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 228 bytes --]

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

* Re: [PATCH] Input: pxrc - simplify mutex handling with guard macro
  2023-12-01 13:29 ` Johan Hovold
  2023-12-01 13:48   ` Marcus Folkesson
@ 2023-12-01 23:33   ` Dmitry Torokhov
  1 sibling, 0 replies; 5+ messages in thread
From: Dmitry Torokhov @ 2023-12-01 23:33 UTC (permalink / raw)
  To: Johan Hovold; +Cc: Marcus Folkesson, linux-input, linux-kernel

On Fri, Dec 01, 2023 at 02:29:22PM +0100, Johan Hovold wrote:
> On Fri, Dec 01, 2023 at 01:08:45PM +0100, Marcus Folkesson wrote:
> > Use the guard(mutex) macro for handle mutex lock/unlocks.
> > 
> > Signed-off-by: Marcus Folkesson <marcus.folkesson@gmail.com>
> 
> A couple of drive-by comments below.
> 
> > ---
> >  drivers/input/joystick/pxrc.c | 27 +++++++++++----------------
> >  1 file changed, 11 insertions(+), 16 deletions(-)
> > 
> > diff --git a/drivers/input/joystick/pxrc.c b/drivers/input/joystick/pxrc.c
> > index ea2bf5951d67..3c3bf7179b46 100644
> > --- a/drivers/input/joystick/pxrc.c
> > +++ b/drivers/input/joystick/pxrc.c
> > @@ -5,15 +5,17 @@
> >   * Copyright (C) 2018 Marcus Folkesson <marcus.folkesson@gmail.com>
> >   */
> >  
> > -#include <linux/kernel.h>
> > +#include <linux/cleanup.h>
> >  #include <linux/errno.h>
> > -#include <linux/slab.h>
> > +#include <linux/input.h>
> > +#include <linux/kernel.h>
> >  #include <linux/module.h>
> > +#include <linux/mutex.h>
> > +#include <linux/slab.h>
> >  #include <linux/uaccess.h>
> > +
> >  #include <linux/usb.h>
> >  #include <linux/usb/input.h>
> > -#include <linux/mutex.h>
> > -#include <linux/input.h>
> 
> Looks like an unrelated change.
>   
> >  #define PXRC_VENDOR_ID		0x1781
> >  #define PXRC_PRODUCT_ID		0x0898
> > @@ -89,25 +91,20 @@ static int pxrc_open(struct input_dev *input)
> >  		dev_err(&pxrc->intf->dev,
> >  			"%s - usb_submit_urb failed, error: %d\n",
> >  			__func__, retval);
> > -		retval = -EIO;
> > -		goto out;
> > +		return -EIO;
> >  	}
> >  
> >  	pxrc->is_open = true;
> > -
> > -out:
> > -	mutex_unlock(&pxrc->pm_mutex);
> > -	return retval;
> > +	return 0;
> >  }
> 
> Eh, this looks obviously broken. Did you not test this before
> submitting? I assume lockdep would complain loudly too.
> 
> You're apparently the author of this driver and can test it, but I fear
> the coming onslaught of untested guard conversions from the "cleanup"
> crew. Not sure I find the result generally more readable either.

Yeah, for the code without conditions (like pxrc_close) the utility is
questionable, but I guess it is just a matter of getting used to the new
facilities...

Thanks.

-- 
Dmitry

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

end of thread, other threads:[~2023-12-01 23:33 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2023-12-01 12:08 [PATCH] Input: pxrc - simplify mutex handling with guard macro Marcus Folkesson
2023-12-01 13:29 ` Johan Hovold
2023-12-01 13:48   ` Marcus Folkesson
2023-12-01 14:03     ` Johan Hovold
2023-12-01 23:33   ` Dmitry Torokhov

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