Commit 934976f6 authored by Dmitry Torokhov's avatar Dmitry Torokhov

Input: atkbd - use guard notation when acquiring mutex

This 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: default avatarHans de Goede <hdegoede@redhat.com>
Link: https://lore.kernel.org/r/20240825051627.2848495-4-dmitry.torokhov@gmail.comSigned-off-by: default avatarDmitry Torokhov <dmitry.torokhov@gmail.com>
parent d5322d53
...@@ -639,7 +639,7 @@ static void atkbd_event_work(struct work_struct *work) ...@@ -639,7 +639,7 @@ static void atkbd_event_work(struct work_struct *work)
{ {
struct atkbd *atkbd = container_of(work, struct atkbd, event_work.work); struct atkbd *atkbd = container_of(work, struct atkbd, event_work.work);
mutex_lock(&atkbd->mutex); guard(mutex)(&atkbd->mutex);
if (!atkbd->enabled) { if (!atkbd->enabled) {
/* /*
...@@ -657,8 +657,6 @@ static void atkbd_event_work(struct work_struct *work) ...@@ -657,8 +657,6 @@ static void atkbd_event_work(struct work_struct *work)
if (test_and_clear_bit(ATKBD_REP_EVENT_BIT, &atkbd->event_mask)) if (test_and_clear_bit(ATKBD_REP_EVENT_BIT, &atkbd->event_mask))
atkbd_set_repeat_rate(atkbd); atkbd_set_repeat_rate(atkbd);
} }
mutex_unlock(&atkbd->mutex);
} }
/* /*
...@@ -1361,7 +1359,7 @@ static int atkbd_reconnect(struct serio *serio) ...@@ -1361,7 +1359,7 @@ static int atkbd_reconnect(struct serio *serio)
{ {
struct atkbd *atkbd = atkbd_from_serio(serio); struct atkbd *atkbd = atkbd_from_serio(serio);
struct serio_driver *drv = serio->drv; struct serio_driver *drv = serio->drv;
int retval = -1; int error;
if (!atkbd || !drv) { if (!atkbd || !drv) {
dev_dbg(&serio->dev, dev_dbg(&serio->dev,
...@@ -1369,16 +1367,17 @@ static int atkbd_reconnect(struct serio *serio) ...@@ -1369,16 +1367,17 @@ static int atkbd_reconnect(struct serio *serio)
return -1; return -1;
} }
mutex_lock(&atkbd->mutex); guard(mutex)(&atkbd->mutex);
atkbd_disable(atkbd); atkbd_disable(atkbd);
if (atkbd->write) { if (atkbd->write) {
if (atkbd_probe(atkbd)) error = atkbd_probe(atkbd);
goto out; if (error)
return error;
if (atkbd->set != atkbd_select_set(atkbd, atkbd->set, atkbd->extra)) if (atkbd->set != atkbd_select_set(atkbd, atkbd->set, atkbd->extra))
goto out; return -EIO;
/* /*
* Restore LED state and repeat rate. While input core * Restore LED state and repeat rate. While input core
...@@ -1404,11 +1403,7 @@ static int atkbd_reconnect(struct serio *serio) ...@@ -1404,11 +1403,7 @@ static int atkbd_reconnect(struct serio *serio)
if (atkbd->write) if (atkbd->write)
atkbd_activate(atkbd); atkbd_activate(atkbd);
retval = 0; return 0;
out:
mutex_unlock(&atkbd->mutex);
return retval;
} }
static const struct serio_device_id atkbd_serio_ids[] = { static const struct serio_device_id atkbd_serio_ids[] = {
...@@ -1465,17 +1460,15 @@ static ssize_t atkbd_attr_set_helper(struct device *dev, const char *buf, size_t ...@@ -1465,17 +1460,15 @@ static ssize_t atkbd_attr_set_helper(struct device *dev, const char *buf, size_t
struct atkbd *atkbd = atkbd_from_serio(serio); struct atkbd *atkbd = atkbd_from_serio(serio);
int retval; int retval;
retval = mutex_lock_interruptible(&atkbd->mutex); scoped_guard(mutex_intr, &atkbd->mutex) {
if (retval) atkbd_disable(atkbd);
return retval; retval = handler(atkbd, buf, count);
atkbd_enable(atkbd);
atkbd_disable(atkbd); return retval;
retval = handler(atkbd, buf, count); }
atkbd_enable(atkbd);
mutex_unlock(&atkbd->mutex);
return retval; return -EINTR;
} }
static ssize_t atkbd_show_extra(struct atkbd *atkbd, char *buf) static ssize_t atkbd_show_extra(struct atkbd *atkbd, char *buf)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment