linux-input.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Dmitry Torokhov <dmitry.torokhov@gmail.com>
To: Javier Carrasco <javier.carrasco.cruz@gmail.com>
Cc: linux-input@vger.kernel.org,
	Michael Hennerich <michael.hennerich@analog.com>,
	Ville Syrjala <syrjala@sci.fi>,
	Support Opensource <support.opensource@diasemi.com>,
	Eddie James <eajames@linux.ibm.com>,
	Andrey Moiseev <o2g.org.ru@gmail.com>,
	Hans de Goede <hdegoede@redhat.com>,
	Jeff LaBundy <jeff@labundy.com>,
	linux-kernel@vger.kernel.org
Subject: [PATCH v2 17/22] Input: pegasus_notetaker - use guard notation when acquiring mutex
Date: Wed, 4 Sep 2024 13:59:18 -0700	[thread overview]
Message-ID: <ZtjKJsArLu3byTU6@google.com> (raw)
In-Reply-To: <a41bb88a-b624-4b5b-a2ea-b49761c45a93@gmail.com>

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.

Reviewed-by: Javier Carrasco <javier.carrasco.cruz@gmail.com>
Signed-off-by: Dmitry Torokhov <dmitry.torokhov@gmail.com>
---

V2: fixed whitespace issues, added Reviewed-by from Javier

 drivers/input/tablet/pegasus_notetaker.c |   86 +++++++++++++++++-------------
 1 file changed, 48 insertions(+), 38 deletions(-)

diff --git a/drivers/input/tablet/pegasus_notetaker.c b/drivers/input/tablet/pegasus_notetaker.c
index a68da2988f9c..8d6b71d59793 100644
--- a/drivers/input/tablet/pegasus_notetaker.c
+++ b/drivers/input/tablet/pegasus_notetaker.c
@@ -214,6 +214,28 @@ static void pegasus_init(struct work_struct *work)
 			error);
 }
 
+static int __pegasus_open(struct pegasus *pegasus)
+{
+	int error;
+
+	guard(mutex)(&pegasus->pm_mutex);
+
+	pegasus->irq->dev = pegasus->usbdev;
+	if (usb_submit_urb(pegasus->irq, GFP_KERNEL))
+		return -EIO;
+
+	error = pegasus_set_mode(pegasus, PEN_MODE_XY, NOTETAKER_LED_MOUSE);
+	if (error) {
+		usb_kill_urb(pegasus->irq);
+		cancel_work_sync(&pegasus->init);
+		return error;
+	}
+
+	pegasus->is_open = true;
+
+	return 0;
+}
+
 static int pegasus_open(struct input_dev *dev)
 {
 	struct pegasus *pegasus = input_get_drvdata(dev);
@@ -223,39 +245,25 @@ static int pegasus_open(struct input_dev *dev)
 	if (error)
 		return error;
 
-	mutex_lock(&pegasus->pm_mutex);
-	pegasus->irq->dev = pegasus->usbdev;
-	if (usb_submit_urb(pegasus->irq, GFP_KERNEL)) {
-		error = -EIO;
-		goto err_autopm_put;
+	error = __pegasus_open(pegasus);
+	if (error) {
+		usb_autopm_put_interface(pegasus->intf);
+		return error;
 	}
 
-	error = pegasus_set_mode(pegasus, PEN_MODE_XY, NOTETAKER_LED_MOUSE);
-	if (error)
-		goto err_kill_urb;
-
-	pegasus->is_open = true;
-	mutex_unlock(&pegasus->pm_mutex);
 	return 0;
-
-err_kill_urb:
-	usb_kill_urb(pegasus->irq);
-	cancel_work_sync(&pegasus->init);
-err_autopm_put:
-	mutex_unlock(&pegasus->pm_mutex);
-	usb_autopm_put_interface(pegasus->intf);
-	return error;
 }
 
 static void pegasus_close(struct input_dev *dev)
 {
 	struct pegasus *pegasus = input_get_drvdata(dev);
 
-	mutex_lock(&pegasus->pm_mutex);
-	usb_kill_urb(pegasus->irq);
-	cancel_work_sync(&pegasus->init);
-	pegasus->is_open = false;
-	mutex_unlock(&pegasus->pm_mutex);
+	scoped_guard(mutex, &pegasus->pm_mutex) {
+		usb_kill_urb(pegasus->irq);
+		cancel_work_sync(&pegasus->init);
+
+		pegasus->is_open = false;
+	}
 
 	usb_autopm_put_interface(pegasus->intf);
 }
@@ -411,10 +419,10 @@ static int pegasus_suspend(struct usb_interface *intf, pm_message_t message)
 {
 	struct pegasus *pegasus = usb_get_intfdata(intf);
 
-	mutex_lock(&pegasus->pm_mutex);
+	guard(mutex)(&pegasus->pm_mutex);
+
 	usb_kill_urb(pegasus->irq);
 	cancel_work_sync(&pegasus->init);
-	mutex_unlock(&pegasus->pm_mutex);
 
 	return 0;
 }
@@ -422,31 +430,33 @@ static int pegasus_suspend(struct usb_interface *intf, pm_message_t message)
 static int pegasus_resume(struct usb_interface *intf)
 {
 	struct pegasus *pegasus = usb_get_intfdata(intf);
-	int retval = 0;
 
-	mutex_lock(&pegasus->pm_mutex);
+	guard(mutex)(&pegasus->pm_mutex);
+
 	if (pegasus->is_open && usb_submit_urb(pegasus->irq, GFP_NOIO) < 0)
-		retval = -EIO;
-	mutex_unlock(&pegasus->pm_mutex);
+		return -EIO;
 
-	return retval;
+	return 0;
 }
 
 static int pegasus_reset_resume(struct usb_interface *intf)
 {
 	struct pegasus *pegasus = usb_get_intfdata(intf);
-	int retval = 0;
+	int error;
+
+	guard(mutex)(&pegasus->pm_mutex);
 
-	mutex_lock(&pegasus->pm_mutex);
 	if (pegasus->is_open) {
-		retval = pegasus_set_mode(pegasus, PEN_MODE_XY,
+		error = pegasus_set_mode(pegasus, PEN_MODE_XY,
 					  NOTETAKER_LED_MOUSE);
-		if (!retval && usb_submit_urb(pegasus->irq, GFP_NOIO) < 0)
-			retval = -EIO;
+		if (error)
+			return error;
+
+		if (usb_submit_urb(pegasus->irq, GFP_NOIO) < 0)
+			return -EIO;
 	}
-	mutex_unlock(&pegasus->pm_mutex);
 
-	return retval;
+	return 0;
 }
 
 static const struct usb_device_id pegasus_ids[] = {

  reply	other threads:[~2024-09-04 20:59 UTC|newest]

Thread overview: 63+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-09-04  4:42 [PATCH 00/22] Convert misc input drivers to use new cleanup facilities Dmitry Torokhov
2024-09-04  4:42 ` [PATCH 01/22] Input: ad714x - use guard notation when acquiring mutex Dmitry Torokhov
2024-09-04 18:47   ` Javier Carrasco
2024-09-04  4:42 ` [PATCH 02/22] Input: ati_remote2 " Dmitry Torokhov
2024-09-04 18:49   ` Javier Carrasco
2024-09-04  4:42 ` [PATCH 03/22] Input: cm109 - use guard notation when acquiring mutex and spinlock Dmitry Torokhov
2024-09-04 19:44   ` Javier Carrasco
2024-09-04  4:42 ` [PATCH 04/22] Input: cma3000_d0x - use guard notation when acquiring mutex Dmitry Torokhov
2024-09-04 18:51   ` Javier Carrasco
2024-09-04  4:42 ` [PATCH 05/22] Input: da7280 - use guard notation when acquiring mutex and spinlock Dmitry Torokhov
2024-09-04 19:02   ` Javier Carrasco
2024-09-04  4:42 ` [PATCH 06/22] Input: kxtj9 - use guard notation when acquiring mutex/disabling irq Dmitry Torokhov
2024-09-04 19:03   ` Javier Carrasco
2024-09-04  4:42 ` [PATCH 07/22] Input: drv260x - use guard notation when acquiring mutex Dmitry Torokhov
2024-09-04 19:05   ` Javier Carrasco
2024-09-04  4:42 ` [PATCH 08/22] Input: drv2665 " Dmitry Torokhov
2024-09-04 19:07   ` Javier Carrasco
2024-09-04  4:42 ` [PATCH 09/22] Input: drv2667 " Dmitry Torokhov
2024-09-04 19:08   ` Javier Carrasco
2024-09-04  4:42 ` [PATCH 10/22] Input: ideapad_slidebar - use guard notation when acquiring spinlock Dmitry Torokhov
2024-09-04 19:09   ` Javier Carrasco
2024-09-04  4:47 ` [PATCH 11/22] Input: ibm-panel " Dmitry Torokhov
2024-09-04 19:11   ` Javier Carrasco
2024-09-04 21:56   ` Eddie James
2024-09-04  4:47 ` [PATCH 12/22] Input: iqs269a - use guard notation when acquiring mutex Dmitry Torokhov
2024-09-04 13:53   ` Javier Carrasco
2024-09-04 18:21     ` Dmitry Torokhov
2024-09-04 18:41       ` Javier Carrasco
2024-09-04 18:53         ` Dmitry Torokhov
2024-09-08 22:05   ` Jeff LaBundy
2024-09-04  4:48 ` [PATCH 13/22] Input: iqs269a - use cleanup facility for fwnodes Dmitry Torokhov
2024-09-04 11:13   ` Javier Carrasco
2024-09-08 22:08   ` Jeff LaBundy
2024-09-04  4:48 ` [PATCH 14/22] Input: iqs626a " Dmitry Torokhov
2024-09-04 11:10   ` Javier Carrasco
2024-09-09  0:02   ` Jeff LaBundy
2024-09-09  1:31     ` Dmitry Torokhov
2024-09-10 15:12       ` Jeff LaBundy
2024-09-04  4:48 ` [PATCH 15/22] Input: iqs7222 " Dmitry Torokhov
2024-09-04 10:50   ` Javier Carrasco
2024-09-04 18:26     ` Dmitry Torokhov
2024-09-04 18:46       ` Javier Carrasco
2024-09-09  0:12   ` Jeff LaBundy
2024-09-09  1:34     ` Dmitry Torokhov
2024-09-10 15:14       ` Jeff LaBundy
2024-09-04  4:48 ` [PATCH 16/22] Input: max8997_haptic - use guard notation when acquiring mutex Dmitry Torokhov
2024-09-04 19:12   ` Javier Carrasco
2024-09-04  4:48 ` [PATCH 17/22] Input: pegasus_notetaker " Dmitry Torokhov
2024-09-04 19:52   ` Javier Carrasco
2024-09-04 20:59     ` Dmitry Torokhov [this message]
2024-09-04  4:49 ` [PATCH 18/22] Input: powermate - use guard notation when acquiring spinlock Dmitry Torokhov
2024-09-04 19:16   ` Javier Carrasco
2024-09-04  4:49 ` [PATCH 19/22] Input: pwm-beeper " Dmitry Torokhov
2024-09-04 19:22   ` Javier Carrasco
2024-09-04  4:49 ` [PATCH 20/22] Input: regulator-haptic - use guard notation when acquiring mutex Dmitry Torokhov
2024-09-04 19:27   ` Javier Carrasco
2024-09-04 20:55     ` [PATCH v2 " Dmitry Torokhov
2024-09-04 21:41       ` Javier Carrasco
2024-09-07  3:40   ` [PATCH " kernel test robot
2024-09-04  4:49 ` [PATCH 21/22] Input: rotary_encoder " Dmitry Torokhov
2024-09-04 19:32   ` Javier Carrasco
2024-09-04  4:49 ` [PATCH 22/22] Input: sparcspkr - use guard notation when acquiring spinlock Dmitry Torokhov
2024-09-04 19:33   ` Javier Carrasco

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=ZtjKJsArLu3byTU6@google.com \
    --to=dmitry.torokhov@gmail.com \
    --cc=eajames@linux.ibm.com \
    --cc=hdegoede@redhat.com \
    --cc=javier.carrasco.cruz@gmail.com \
    --cc=jeff@labundy.com \
    --cc=linux-input@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=michael.hennerich@analog.com \
    --cc=o2g.org.ru@gmail.com \
    --cc=support.opensource@diasemi.com \
    --cc=syrjala@sci.fi \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).