diff mbox series

[18/22] Input: powermate - use guard notation when acquiring spinlock

Message ID 20240904044902.1049017-1-dmitry.torokhov@gmail.com (mailing list archive)
State Mainlined
Commit c6849218541d29a7725fd375b7095cb02df4b422
Headers show
Series Convert misc input drivers to use new cleanup facilities | expand

Commit Message

Dmitry Torokhov Sept. 4, 2024, 4:49 a.m. UTC
Using guard notation makes the code more compact and error handling
more robust by ensuring that locks are released in all code paths
when control leaves critical section.

Signed-off-by: Dmitry Torokhov <dmitry.torokhov@gmail.com>
---
 drivers/input/misc/powermate.c | 11 ++---------
 1 file changed, 2 insertions(+), 9 deletions(-)

Comments

Javier Carrasco Sept. 4, 2024, 7:16 p.m. UTC | #1
On 04/09/2024 06:49, Dmitry Torokhov wrote:
> Using guard notation makes the code more compact and error handling
> more robust by ensuring that locks are released in all code paths
> when control leaves critical section.
> 
> Signed-off-by: Dmitry Torokhov <dmitry.torokhov@gmail.com>
> ---
>  drivers/input/misc/powermate.c | 11 ++---------
>  1 file changed, 2 insertions(+), 9 deletions(-)
> 
> diff --git a/drivers/input/misc/powermate.c b/drivers/input/misc/powermate.c
> index 4b039abffc4b..ecb92ee5ebbc 100644
> --- a/drivers/input/misc/powermate.c
> +++ b/drivers/input/misc/powermate.c
> @@ -194,22 +194,18 @@ static void powermate_sync_state(struct powermate_device *pm)
>  static void powermate_config_complete(struct urb *urb)
>  {
>  	struct powermate_device *pm = urb->context;
> -	unsigned long flags;
>  
>  	if (urb->status)
>  		printk(KERN_ERR "powermate: config urb returned %d\n", urb->status);
>  
> -	spin_lock_irqsave(&pm->lock, flags);
> +	guard(spinlock_irqsave)(&pm->lock);
>  	powermate_sync_state(pm);
> -	spin_unlock_irqrestore(&pm->lock, flags);
>  }
>  
>  /* Set the LED up as described and begin the sync with the hardware if required */
>  static void powermate_pulse_led(struct powermate_device *pm, int static_brightness, int pulse_speed,
>  				int pulse_table, int pulse_asleep, int pulse_awake)
>  {
> -	unsigned long flags;
> -
>  	if (pulse_speed < 0)
>  		pulse_speed = 0;
>  	if (pulse_table < 0)
> @@ -222,8 +218,7 @@ static void powermate_pulse_led(struct powermate_device *pm, int static_brightne
>  	pulse_asleep = !!pulse_asleep;
>  	pulse_awake = !!pulse_awake;
>  
> -
> -	spin_lock_irqsave(&pm->lock, flags);
> +	guard(spinlock_irqsave)(&pm->lock);
>  
>  	/* mark state updates which are required */
>  	if (static_brightness != pm->static_brightness) {
> @@ -245,8 +240,6 @@ static void powermate_pulse_led(struct powermate_device *pm, int static_brightne
>  	}
>  
>  	powermate_sync_state(pm);
> -
> -	spin_unlock_irqrestore(&pm->lock, flags);
>  }
>  
>  /* Callback from the Input layer when an event arrives from userspace to configure the LED */

Reviewed-by: Javier Carrasco <javier.carrasco.cruz@gmail.com>
diff mbox series

Patch

diff --git a/drivers/input/misc/powermate.c b/drivers/input/misc/powermate.c
index 4b039abffc4b..ecb92ee5ebbc 100644
--- a/drivers/input/misc/powermate.c
+++ b/drivers/input/misc/powermate.c
@@ -194,22 +194,18 @@  static void powermate_sync_state(struct powermate_device *pm)
 static void powermate_config_complete(struct urb *urb)
 {
 	struct powermate_device *pm = urb->context;
-	unsigned long flags;
 
 	if (urb->status)
 		printk(KERN_ERR "powermate: config urb returned %d\n", urb->status);
 
-	spin_lock_irqsave(&pm->lock, flags);
+	guard(spinlock_irqsave)(&pm->lock);
 	powermate_sync_state(pm);
-	spin_unlock_irqrestore(&pm->lock, flags);
 }
 
 /* Set the LED up as described and begin the sync with the hardware if required */
 static void powermate_pulse_led(struct powermate_device *pm, int static_brightness, int pulse_speed,
 				int pulse_table, int pulse_asleep, int pulse_awake)
 {
-	unsigned long flags;
-
 	if (pulse_speed < 0)
 		pulse_speed = 0;
 	if (pulse_table < 0)
@@ -222,8 +218,7 @@  static void powermate_pulse_led(struct powermate_device *pm, int static_brightne
 	pulse_asleep = !!pulse_asleep;
 	pulse_awake = !!pulse_awake;
 
-
-	spin_lock_irqsave(&pm->lock, flags);
+	guard(spinlock_irqsave)(&pm->lock);
 
 	/* mark state updates which are required */
 	if (static_brightness != pm->static_brightness) {
@@ -245,8 +240,6 @@  static void powermate_pulse_led(struct powermate_device *pm, int static_brightne
 	}
 
 	powermate_sync_state(pm);
-
-	spin_unlock_irqrestore(&pm->lock, flags);
 }
 
 /* Callback from the Input layer when an event arrives from userspace to configure the LED */