diff mbox series

[04/22] Input: cma3000_d0x - use guard notation when acquiring mutex

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

Commit Message

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

Signed-off-by: Dmitry Torokhov <dmitry.torokhov@gmail.com>
---
 drivers/input/misc/cma3000_d0x.c | 16 ++++------------
 1 file changed, 4 insertions(+), 12 deletions(-)

Comments

Javier Carrasco Sept. 4, 2024, 6:51 p.m. UTC | #1
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 mutexes are released in all code paths
> when control leaves critical section.
> 
> Signed-off-by: Dmitry Torokhov <dmitry.torokhov@gmail.com>
> ---
>  drivers/input/misc/cma3000_d0x.c | 16 ++++------------
>  1 file changed, 4 insertions(+), 12 deletions(-)
> 
> diff --git a/drivers/input/misc/cma3000_d0x.c b/drivers/input/misc/cma3000_d0x.c
> index 0c68e924a1cc..cfc12332bee1 100644
> --- a/drivers/input/misc/cma3000_d0x.c
> +++ b/drivers/input/misc/cma3000_d0x.c
> @@ -217,15 +217,13 @@ static int cma3000_open(struct input_dev *input_dev)
>  {
>  	struct cma3000_accl_data *data = input_get_drvdata(input_dev);
>  
> -	mutex_lock(&data->mutex);
> +	guard(mutex)(&data->mutex);
>  
>  	if (!data->suspended)
>  		cma3000_poweron(data);
>  
>  	data->opened = true;
>  
> -	mutex_unlock(&data->mutex);
> -
>  	return 0;
>  }
>  
> @@ -233,40 +231,34 @@ static void cma3000_close(struct input_dev *input_dev)
>  {
>  	struct cma3000_accl_data *data = input_get_drvdata(input_dev);
>  
> -	mutex_lock(&data->mutex);
> +	guard(mutex)(&data->mutex);
>  
>  	if (!data->suspended)
>  		cma3000_poweroff(data);
>  
>  	data->opened = false;
> -
> -	mutex_unlock(&data->mutex);
>  }
>  
>  void cma3000_suspend(struct cma3000_accl_data *data)
>  {
> -	mutex_lock(&data->mutex);
> +	guard(mutex)(&data->mutex);
>  
>  	if (!data->suspended && data->opened)
>  		cma3000_poweroff(data);
>  
>  	data->suspended = true;
> -
> -	mutex_unlock(&data->mutex);
>  }
>  EXPORT_SYMBOL(cma3000_suspend);
>  
>  
>  void cma3000_resume(struct cma3000_accl_data *data)
>  {
> -	mutex_lock(&data->mutex);
> +	guard(mutex)(&data->mutex);
>  
>  	if (data->suspended && data->opened)
>  		cma3000_poweron(data);
>  
>  	data->suspended = false;
> -
> -	mutex_unlock(&data->mutex);
>  }
>  EXPORT_SYMBOL(cma3000_resume);
>  

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

Patch

diff --git a/drivers/input/misc/cma3000_d0x.c b/drivers/input/misc/cma3000_d0x.c
index 0c68e924a1cc..cfc12332bee1 100644
--- a/drivers/input/misc/cma3000_d0x.c
+++ b/drivers/input/misc/cma3000_d0x.c
@@ -217,15 +217,13 @@  static int cma3000_open(struct input_dev *input_dev)
 {
 	struct cma3000_accl_data *data = input_get_drvdata(input_dev);
 
-	mutex_lock(&data->mutex);
+	guard(mutex)(&data->mutex);
 
 	if (!data->suspended)
 		cma3000_poweron(data);
 
 	data->opened = true;
 
-	mutex_unlock(&data->mutex);
-
 	return 0;
 }
 
@@ -233,40 +231,34 @@  static void cma3000_close(struct input_dev *input_dev)
 {
 	struct cma3000_accl_data *data = input_get_drvdata(input_dev);
 
-	mutex_lock(&data->mutex);
+	guard(mutex)(&data->mutex);
 
 	if (!data->suspended)
 		cma3000_poweroff(data);
 
 	data->opened = false;
-
-	mutex_unlock(&data->mutex);
 }
 
 void cma3000_suspend(struct cma3000_accl_data *data)
 {
-	mutex_lock(&data->mutex);
+	guard(mutex)(&data->mutex);
 
 	if (!data->suspended && data->opened)
 		cma3000_poweroff(data);
 
 	data->suspended = true;
-
-	mutex_unlock(&data->mutex);
 }
 EXPORT_SYMBOL(cma3000_suspend);
 
 
 void cma3000_resume(struct cma3000_accl_data *data)
 {
-	mutex_lock(&data->mutex);
+	guard(mutex)(&data->mutex);
 
 	if (data->suspended && data->opened)
 		cma3000_poweron(data);
 
 	data->suspended = false;
-
-	mutex_unlock(&data->mutex);
 }
 EXPORT_SYMBOL(cma3000_resume);