Skip to content

Commit 79d0197

Browse files
committed
Input: hyperv-keyboard - use guard notation when acquiring spinlock
Using guard notation makes the code more compact and error handling more robust by ensuring that locks are released in all code paths when control leaves critical section. Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Dmitry Torokhov <[email protected]>
1 parent 44f9200 commit 79d0197

File tree

1 file changed

+18
-20
lines changed

1 file changed

+18
-20
lines changed

drivers/input/serio/hyperv-keyboard.c

Lines changed: 18 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,6 @@ static void hv_kbd_on_receive(struct hv_device *hv_dev,
102102
{
103103
struct hv_kbd_dev *kbd_dev = hv_get_drvdata(hv_dev);
104104
struct synth_kbd_keystroke *ks_msg;
105-
unsigned long flags;
106105
u32 msg_type = __le32_to_cpu(msg->header.type);
107106
u32 info;
108107
u16 scan_code;
@@ -147,21 +146,22 @@ static void hv_kbd_on_receive(struct hv_device *hv_dev,
147146
/*
148147
* Inject the information through the serio interrupt.
149148
*/
150-
spin_lock_irqsave(&kbd_dev->lock, flags);
151-
if (kbd_dev->started) {
152-
if (info & IS_E0)
153-
serio_interrupt(kbd_dev->hv_serio,
154-
XTKBD_EMUL0, 0);
155-
if (info & IS_E1)
156-
serio_interrupt(kbd_dev->hv_serio,
157-
XTKBD_EMUL1, 0);
158-
scan_code = __le16_to_cpu(ks_msg->make_code);
159-
if (info & IS_BREAK)
160-
scan_code |= XTKBD_RELEASE;
149+
scoped_guard(spinlock_irqsave, &kbd_dev->lock) {
150+
if (kbd_dev->started) {
151+
if (info & IS_E0)
152+
serio_interrupt(kbd_dev->hv_serio,
153+
XTKBD_EMUL0, 0);
154+
if (info & IS_E1)
155+
serio_interrupt(kbd_dev->hv_serio,
156+
XTKBD_EMUL1, 0);
157+
scan_code = __le16_to_cpu(ks_msg->make_code);
158+
if (info & IS_BREAK)
159+
scan_code |= XTKBD_RELEASE;
161160

162-
serio_interrupt(kbd_dev->hv_serio, scan_code, 0);
161+
serio_interrupt(kbd_dev->hv_serio,
162+
scan_code, 0);
163+
}
163164
}
164-
spin_unlock_irqrestore(&kbd_dev->lock, flags);
165165

166166
/*
167167
* Only trigger a wakeup on key down, otherwise
@@ -292,23 +292,21 @@ static int hv_kbd_connect_to_vsp(struct hv_device *hv_dev)
292292
static int hv_kbd_start(struct serio *serio)
293293
{
294294
struct hv_kbd_dev *kbd_dev = serio->port_data;
295-
unsigned long flags;
296295

297-
spin_lock_irqsave(&kbd_dev->lock, flags);
296+
guard(spinlock_irqsave)(&kbd_dev->lock);
297+
298298
kbd_dev->started = true;
299-
spin_unlock_irqrestore(&kbd_dev->lock, flags);
300299

301300
return 0;
302301
}
303302

304303
static void hv_kbd_stop(struct serio *serio)
305304
{
306305
struct hv_kbd_dev *kbd_dev = serio->port_data;
307-
unsigned long flags;
308306

309-
spin_lock_irqsave(&kbd_dev->lock, flags);
307+
guard(spinlock_irqsave)(&kbd_dev->lock);
308+
310309
kbd_dev->started = false;
311-
spin_unlock_irqrestore(&kbd_dev->lock, flags);
312310
}
313311

314312
static int hv_kbd_probe(struct hv_device *hv_dev,

0 commit comments

Comments
 (0)