[PATCH 05/22] Input: da7280 - use guard notation when acquiring mutex and spinlock

Dmitry Torokhov posted 22 patches 1 year, 3 months ago
[PATCH 05/22] Input: da7280 - use guard notation when acquiring mutex and spinlock
Posted by Dmitry Torokhov 1 year, 3 months ago
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/da7280.c | 26 ++++++++++++--------------
 1 file changed, 12 insertions(+), 14 deletions(-)

diff --git a/drivers/input/misc/da7280.c b/drivers/input/misc/da7280.c
index 1629b7ea4cbd..e4a605c6af15 100644
--- a/drivers/input/misc/da7280.c
+++ b/drivers/input/misc/da7280.c
@@ -1263,39 +1263,37 @@ static int da7280_suspend(struct device *dev)
 {
 	struct da7280_haptic *haptics = dev_get_drvdata(dev);
 
-	mutex_lock(&haptics->input_dev->mutex);
+	guard(mutex)(&haptics->input_dev->mutex);
 
 	/*
 	 * Make sure no new requests will be submitted while device is
 	 * suspended.
 	 */
-	spin_lock_irq(&haptics->input_dev->event_lock);
-	haptics->suspended = true;
-	spin_unlock_irq(&haptics->input_dev->event_lock);
+	scoped_guard(spinlock_irq, &haptics->input_dev->event_lock) {
+		haptics->suspended = true;
+	}
 
 	da7280_haptic_stop(haptics);
 
-	mutex_unlock(&haptics->input_dev->mutex);
-
 	return 0;
 }
 
 static int da7280_resume(struct device *dev)
 {
 	struct da7280_haptic *haptics = dev_get_drvdata(dev);
-	int retval;
+	int error;
 
-	mutex_lock(&haptics->input_dev->mutex);
+	guard(mutex)(&haptics->input_dev->mutex);
 
-	retval = da7280_haptic_start(haptics);
-	if (!retval) {
-		spin_lock_irq(&haptics->input_dev->event_lock);
+	error = da7280_haptic_start(haptics);
+	if (error)
+		return error;
+
+	scoped_guard(spinlock_irq, &haptics->input_dev->event_lock) {
 		haptics->suspended = false;
-		spin_unlock_irq(&haptics->input_dev->event_lock);
 	}
 
-	mutex_unlock(&haptics->input_dev->mutex);
-	return retval;
+	return 0;
 }
 
 #ifdef CONFIG_OF
-- 
2.46.0.469.g59c65b2a67-goog
Re: [PATCH 05/22] Input: da7280 - use guard notation when acquiring mutex and spinlock
Posted by Javier Carrasco 1 year, 3 months ago
On 04/09/2024 06:42, 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/da7280.c | 26 ++++++++++++--------------
>  1 file changed, 12 insertions(+), 14 deletions(-)
> 
> diff --git a/drivers/input/misc/da7280.c b/drivers/input/misc/da7280.c
> index 1629b7ea4cbd..e4a605c6af15 100644
> --- a/drivers/input/misc/da7280.c
> +++ b/drivers/input/misc/da7280.c
> @@ -1263,39 +1263,37 @@ static int da7280_suspend(struct device *dev)
>  {
>  	struct da7280_haptic *haptics = dev_get_drvdata(dev);
>  
> -	mutex_lock(&haptics->input_dev->mutex);
> +	guard(mutex)(&haptics->input_dev->mutex);
>  
>  	/*
>  	 * Make sure no new requests will be submitted while device is
>  	 * suspended.
>  	 */
> -	spin_lock_irq(&haptics->input_dev->event_lock);
> -	haptics->suspended = true;
> -	spin_unlock_irq(&haptics->input_dev->event_lock);
> +	scoped_guard(spinlock_irq, &haptics->input_dev->event_lock) {
> +		haptics->suspended = true;
> +	}
>  
>  	da7280_haptic_stop(haptics);
>  
> -	mutex_unlock(&haptics->input_dev->mutex);
> -
>  	return 0;
>  }
>  
>  static int da7280_resume(struct device *dev)
>  {
>  	struct da7280_haptic *haptics = dev_get_drvdata(dev);
> -	int retval;
> +	int error;
>  
> -	mutex_lock(&haptics->input_dev->mutex);
> +	guard(mutex)(&haptics->input_dev->mutex);
>  
> -	retval = da7280_haptic_start(haptics);
> -	if (!retval) {
> -		spin_lock_irq(&haptics->input_dev->event_lock);
> +	error = da7280_haptic_start(haptics);
> +	if (error)
> +		return error;
> +
> +	scoped_guard(spinlock_irq, &haptics->input_dev->event_lock) {
>  		haptics->suspended = false;
> -		spin_unlock_irq(&haptics->input_dev->event_lock);
>  	}
>  
> -	mutex_unlock(&haptics->input_dev->mutex);
> -	return retval;
> +	return 0;
>  }
>  
>  #ifdef CONFIG_OF

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