Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

Input: n64joy - use guard notation when acquiring mutex

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.

Link: https://lore.kernel.org/r/20240904043104.1030257-5-dmitry.torokhov@gmail.com
Signed-off-by: Dmitry Torokhov <dmitry.torokhov@gmail.com>

+14 -17
+14 -17
drivers/input/joystick/n64joy.c
··· 191 191 static int n64joy_open(struct input_dev *dev) 192 192 { 193 193 struct n64joy_priv *priv = input_get_drvdata(dev); 194 - int err; 195 194 196 - err = mutex_lock_interruptible(&priv->n64joy_mutex); 197 - if (err) 198 - return err; 195 + scoped_guard(mutex_intr, &priv->n64joy_mutex) { 196 + if (!priv->n64joy_opened) { 197 + /* 198 + * We could use the vblank irq, but it's not important 199 + * if the poll point slightly changes. 200 + */ 201 + timer_setup(&priv->timer, n64joy_poll, 0); 202 + mod_timer(&priv->timer, jiffies + msecs_to_jiffies(16)); 203 + } 199 204 200 - if (!priv->n64joy_opened) { 201 - /* 202 - * We could use the vblank irq, but it's not important if 203 - * the poll point slightly changes. 204 - */ 205 - timer_setup(&priv->timer, n64joy_poll, 0); 206 - mod_timer(&priv->timer, jiffies + msecs_to_jiffies(16)); 205 + priv->n64joy_opened++; 206 + return 0; 207 207 } 208 208 209 - priv->n64joy_opened++; 210 - 211 - mutex_unlock(&priv->n64joy_mutex); 212 - return err; 209 + return -EINTR; 213 210 } 214 211 215 212 static void n64joy_close(struct input_dev *dev) 216 213 { 217 214 struct n64joy_priv *priv = input_get_drvdata(dev); 218 215 219 - mutex_lock(&priv->n64joy_mutex); 216 + guard(mutex)(&priv->n64joy_mutex); 217 + 220 218 if (!--priv->n64joy_opened) 221 219 del_timer_sync(&priv->timer); 222 - mutex_unlock(&priv->n64joy_mutex); 223 220 } 224 221 225 222 static const u64 __initconst scandata[] ____cacheline_aligned = {