From: Dmitry Torokhov <dmitry.torokhov@gmail.com>
To: linux-input@vger.kernel.org
Cc: Erick Archer <erick.archer@outlook.com>,
Christophe JAILLET <christophe.jaillet@wanadoo.fr>,
linux-kernel@vger.kernel.org
Subject: [PATCH 2/6] Input: gamecon - use guard notation when acquiring mutex
Date: Tue, 3 Sep 2024 21:30:59 -0700 [thread overview]
Message-ID: <20240904043104.1030257-3-dmitry.torokhov@gmail.com> (raw)
In-Reply-To: <20240904043104.1030257-1-dmitry.torokhov@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.
Signed-off-by: Dmitry Torokhov <dmitry.torokhov@gmail.com>
---
drivers/input/joystick/gamecon.c | 22 ++++++++++------------
1 file changed, 10 insertions(+), 12 deletions(-)
diff --git a/drivers/input/joystick/gamecon.c b/drivers/input/joystick/gamecon.c
index c38de3094553..968c0e653f2e 100644
--- a/drivers/input/joystick/gamecon.c
+++ b/drivers/input/joystick/gamecon.c
@@ -765,33 +765,31 @@ static void gc_timer(struct timer_list *t)
static int gc_open(struct input_dev *dev)
{
struct gc *gc = input_get_drvdata(dev);
- int err;
- err = mutex_lock_interruptible(&gc->mutex);
- if (err)
- return err;
+ scoped_guard(mutex_intr, &gc->mutex) {
+ if (!gc->used++) {
+ parport_claim(gc->pd);
+ parport_write_control(gc->pd->port, 0x04);
+ mod_timer(&gc->timer, jiffies + GC_REFRESH_TIME);
+ }
- if (!gc->used++) {
- parport_claim(gc->pd);
- parport_write_control(gc->pd->port, 0x04);
- mod_timer(&gc->timer, jiffies + GC_REFRESH_TIME);
+ return 0;
}
- mutex_unlock(&gc->mutex);
- return 0;
+ return -EINTR;
}
static void gc_close(struct input_dev *dev)
{
struct gc *gc = input_get_drvdata(dev);
- mutex_lock(&gc->mutex);
+ guard(mutex)(&gc->mutex);
+
if (!--gc->used) {
del_timer_sync(&gc->timer);
parport_write_control(gc->pd->port, 0x00);
parport_release(gc->pd);
}
- mutex_unlock(&gc->mutex);
}
static int gc_setup_pad(struct gc *gc, int idx, int pad_type)
--
2.46.0.469.g59c65b2a67-goog
next prev parent reply other threads:[~2024-09-04 4:31 UTC|newest]
Thread overview: 10+ messages / expand[flat|nested] mbox.gz Atom feed top
2024-09-04 4:30 [PATCH 0/6] Convert joystick drivers to use new cleanup facilities Dmitry Torokhov
2024-09-04 4:30 ` [PATCH 1/6] Input: db9 - use guard notation when acquiring mutex Dmitry Torokhov
2024-11-20 18:33 ` David Lechner
2024-11-21 3:52 ` Dmitry Torokhov
2024-09-04 4:30 ` Dmitry Torokhov [this message]
2024-09-04 4:31 ` [PATCH 3/6] Input: iforce - use guard notation when acquiring mutex and spinlock Dmitry Torokhov
2024-09-04 4:31 ` [PATCH 4/6] Input: n64joy - use guard notation when acquiring mutex Dmitry Torokhov
2024-09-04 4:31 ` [PATCH 5/6] Input: turbografx " Dmitry Torokhov
2024-09-04 4:31 ` [PATCH 6/6] Input: xpad - use guard notation when acquiring mutex and spinlock Dmitry Torokhov
2024-09-04 4:43 ` [PATCH 0/6] Convert joystick drivers to use new cleanup facilities Dmitry Torokhov
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=20240904043104.1030257-3-dmitry.torokhov@gmail.com \
--to=dmitry.torokhov@gmail.com \
--cc=christophe.jaillet@wanadoo.fr \
--cc=erick.archer@outlook.com \
--cc=linux-input@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
/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).