diff mbox series

[02/22] Input: ati_remote2 - use guard notation when acquiring mutex

Message ID 20240904044244.1042174-3-dmitry.torokhov@gmail.com (mailing list archive)
State New
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/ati_remote2.c | 57 +++++++++++---------------------
 1 file changed, 19 insertions(+), 38 deletions(-)

Comments

Javier Carrasco Sept. 4, 2024, 6:49 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/ati_remote2.c | 57 +++++++++++---------------------
>  1 file changed, 19 insertions(+), 38 deletions(-)
> 
> diff --git a/drivers/input/misc/ati_remote2.c b/drivers/input/misc/ati_remote2.c
> index 795f69edb4b2..e84649af801d 100644
> --- a/drivers/input/misc/ati_remote2.c
> +++ b/drivers/input/misc/ati_remote2.c
> @@ -244,29 +244,21 @@ static int ati_remote2_open(struct input_dev *idev)
>  	if (r) {
>  		dev_err(&ar2->intf[0]->dev,
>  			"%s(): usb_autopm_get_interface() = %d\n", __func__, r);
> -		goto fail1;
> +		return r;
>  	}
>  
> -	mutex_lock(&ati_remote2_mutex);
> +	scoped_guard(mutex, &ati_remote2_mutex) {
> +		if (!(ar2->flags & ATI_REMOTE2_SUSPENDED)) {
> +			r = ati_remote2_submit_urbs(ar2);
> +			if (r)
> +				break;
> +		}
>  
> -	if (!(ar2->flags & ATI_REMOTE2_SUSPENDED)) {
> -		r = ati_remote2_submit_urbs(ar2);
> -		if (r)
> -			goto fail2;
> +		ar2->flags |= ATI_REMOTE2_OPENED;
>  	}
>  
> -	ar2->flags |= ATI_REMOTE2_OPENED;
> -
> -	mutex_unlock(&ati_remote2_mutex);
> -
>  	usb_autopm_put_interface(ar2->intf[0]);
>  
> -	return 0;
> -
> - fail2:
> -	mutex_unlock(&ati_remote2_mutex);
> -	usb_autopm_put_interface(ar2->intf[0]);
> - fail1:
>  	return r;
>  }
>  
> @@ -276,14 +268,12 @@ static void ati_remote2_close(struct input_dev *idev)
>  
>  	dev_dbg(&ar2->intf[0]->dev, "%s()\n", __func__);
>  
> -	mutex_lock(&ati_remote2_mutex);
> +	guard(mutex)(&ati_remote2_mutex);
>  
>  	if (!(ar2->flags & ATI_REMOTE2_SUSPENDED))
>  		ati_remote2_kill_urbs(ar2);
>  
>  	ar2->flags &= ~ATI_REMOTE2_OPENED;
> -
> -	mutex_unlock(&ati_remote2_mutex);
>  }
>  
>  static void ati_remote2_input_mouse(struct ati_remote2 *ar2)
> @@ -713,16 +703,14 @@ static ssize_t ati_remote2_store_channel_mask(struct device *dev,
>  		return r;
>  	}
>  
> -	mutex_lock(&ati_remote2_mutex);
> -
> -	if (mask != ar2->channel_mask) {
> -		r = ati_remote2_setup(ar2, mask);
> -		if (!r)
> -			ar2->channel_mask = mask;
> +	scoped_guard(mutex, &ati_remote2_mutex) {
> +		if (mask != ar2->channel_mask) {
> +			r = ati_remote2_setup(ar2, mask);
> +			if (!r)
> +				ar2->channel_mask = mask;
> +		}
>  	}
>  
> -	mutex_unlock(&ati_remote2_mutex);
> -
>  	usb_autopm_put_interface(ar2->intf[0]);
>  
>  	return r ? r : count;
> @@ -892,15 +880,13 @@ static int ati_remote2_suspend(struct usb_interface *interface,
>  
>  	dev_dbg(&ar2->intf[0]->dev, "%s()\n", __func__);
>  
> -	mutex_lock(&ati_remote2_mutex);
> +	guard(mutex)(&ati_remote2_mutex);
>  
>  	if (ar2->flags & ATI_REMOTE2_OPENED)
>  		ati_remote2_kill_urbs(ar2);
>  
>  	ar2->flags |= ATI_REMOTE2_SUSPENDED;
>  
> -	mutex_unlock(&ati_remote2_mutex);
> -
>  	return 0;
>  }
>  
> @@ -917,7 +903,7 @@ static int ati_remote2_resume(struct usb_interface *interface)
>  
>  	dev_dbg(&ar2->intf[0]->dev, "%s()\n", __func__);
>  
> -	mutex_lock(&ati_remote2_mutex);
> +	guard(mutex)(&ati_remote2_mutex);
>  
>  	if (ar2->flags & ATI_REMOTE2_OPENED)
>  		r = ati_remote2_submit_urbs(ar2);
> @@ -925,8 +911,6 @@ static int ati_remote2_resume(struct usb_interface *interface)
>  	if (!r)
>  		ar2->flags &= ~ATI_REMOTE2_SUSPENDED;
>  
> -	mutex_unlock(&ati_remote2_mutex);
> -
>  	return r;
>  }
>  
> @@ -943,11 +927,11 @@ static int ati_remote2_reset_resume(struct usb_interface *interface)
>  
>  	dev_dbg(&ar2->intf[0]->dev, "%s()\n", __func__);
>  
> -	mutex_lock(&ati_remote2_mutex);
> +	guard(mutex)(&ati_remote2_mutex);
>  
>  	r = ati_remote2_setup(ar2, ar2->channel_mask);
>  	if (r)
> -		goto out;
> +		return r;
>  
>  	if (ar2->flags & ATI_REMOTE2_OPENED)
>  		r = ati_remote2_submit_urbs(ar2);
> @@ -955,9 +939,6 @@ static int ati_remote2_reset_resume(struct usb_interface *interface)
>  	if (!r)
>  		ar2->flags &= ~ATI_REMOTE2_SUSPENDED;
>  
> - out:
> -	mutex_unlock(&ati_remote2_mutex);
> -
>  	return r;
>  }
>  

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

Patch

diff --git a/drivers/input/misc/ati_remote2.c b/drivers/input/misc/ati_remote2.c
index 795f69edb4b2..e84649af801d 100644
--- a/drivers/input/misc/ati_remote2.c
+++ b/drivers/input/misc/ati_remote2.c
@@ -244,29 +244,21 @@  static int ati_remote2_open(struct input_dev *idev)
 	if (r) {
 		dev_err(&ar2->intf[0]->dev,
 			"%s(): usb_autopm_get_interface() = %d\n", __func__, r);
-		goto fail1;
+		return r;
 	}
 
-	mutex_lock(&ati_remote2_mutex);
+	scoped_guard(mutex, &ati_remote2_mutex) {
+		if (!(ar2->flags & ATI_REMOTE2_SUSPENDED)) {
+			r = ati_remote2_submit_urbs(ar2);
+			if (r)
+				break;
+		}
 
-	if (!(ar2->flags & ATI_REMOTE2_SUSPENDED)) {
-		r = ati_remote2_submit_urbs(ar2);
-		if (r)
-			goto fail2;
+		ar2->flags |= ATI_REMOTE2_OPENED;
 	}
 
-	ar2->flags |= ATI_REMOTE2_OPENED;
-
-	mutex_unlock(&ati_remote2_mutex);
-
 	usb_autopm_put_interface(ar2->intf[0]);
 
-	return 0;
-
- fail2:
-	mutex_unlock(&ati_remote2_mutex);
-	usb_autopm_put_interface(ar2->intf[0]);
- fail1:
 	return r;
 }
 
@@ -276,14 +268,12 @@  static void ati_remote2_close(struct input_dev *idev)
 
 	dev_dbg(&ar2->intf[0]->dev, "%s()\n", __func__);
 
-	mutex_lock(&ati_remote2_mutex);
+	guard(mutex)(&ati_remote2_mutex);
 
 	if (!(ar2->flags & ATI_REMOTE2_SUSPENDED))
 		ati_remote2_kill_urbs(ar2);
 
 	ar2->flags &= ~ATI_REMOTE2_OPENED;
-
-	mutex_unlock(&ati_remote2_mutex);
 }
 
 static void ati_remote2_input_mouse(struct ati_remote2 *ar2)
@@ -713,16 +703,14 @@  static ssize_t ati_remote2_store_channel_mask(struct device *dev,
 		return r;
 	}
 
-	mutex_lock(&ati_remote2_mutex);
-
-	if (mask != ar2->channel_mask) {
-		r = ati_remote2_setup(ar2, mask);
-		if (!r)
-			ar2->channel_mask = mask;
+	scoped_guard(mutex, &ati_remote2_mutex) {
+		if (mask != ar2->channel_mask) {
+			r = ati_remote2_setup(ar2, mask);
+			if (!r)
+				ar2->channel_mask = mask;
+		}
 	}
 
-	mutex_unlock(&ati_remote2_mutex);
-
 	usb_autopm_put_interface(ar2->intf[0]);
 
 	return r ? r : count;
@@ -892,15 +880,13 @@  static int ati_remote2_suspend(struct usb_interface *interface,
 
 	dev_dbg(&ar2->intf[0]->dev, "%s()\n", __func__);
 
-	mutex_lock(&ati_remote2_mutex);
+	guard(mutex)(&ati_remote2_mutex);
 
 	if (ar2->flags & ATI_REMOTE2_OPENED)
 		ati_remote2_kill_urbs(ar2);
 
 	ar2->flags |= ATI_REMOTE2_SUSPENDED;
 
-	mutex_unlock(&ati_remote2_mutex);
-
 	return 0;
 }
 
@@ -917,7 +903,7 @@  static int ati_remote2_resume(struct usb_interface *interface)
 
 	dev_dbg(&ar2->intf[0]->dev, "%s()\n", __func__);
 
-	mutex_lock(&ati_remote2_mutex);
+	guard(mutex)(&ati_remote2_mutex);
 
 	if (ar2->flags & ATI_REMOTE2_OPENED)
 		r = ati_remote2_submit_urbs(ar2);
@@ -925,8 +911,6 @@  static int ati_remote2_resume(struct usb_interface *interface)
 	if (!r)
 		ar2->flags &= ~ATI_REMOTE2_SUSPENDED;
 
-	mutex_unlock(&ati_remote2_mutex);
-
 	return r;
 }
 
@@ -943,11 +927,11 @@  static int ati_remote2_reset_resume(struct usb_interface *interface)
 
 	dev_dbg(&ar2->intf[0]->dev, "%s()\n", __func__);
 
-	mutex_lock(&ati_remote2_mutex);
+	guard(mutex)(&ati_remote2_mutex);
 
 	r = ati_remote2_setup(ar2, ar2->channel_mask);
 	if (r)
-		goto out;
+		return r;
 
 	if (ar2->flags & ATI_REMOTE2_OPENED)
 		r = ati_remote2_submit_urbs(ar2);
@@ -955,9 +939,6 @@  static int ati_remote2_reset_resume(struct usb_interface *interface)
 	if (!r)
 		ar2->flags &= ~ATI_REMOTE2_SUSPENDED;
 
- out:
-	mutex_unlock(&ati_remote2_mutex);
-
 	return r;
 }