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

Input: turbografx - 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-6-dmitry.torokhov@gmail.com
Signed-off-by: Dmitry Torokhov <dmitry.torokhov@gmail.com>

+10 -12
+10 -12
drivers/input/joystick/turbografx.c
··· 103 103 static int tgfx_open(struct input_dev *dev) 104 104 { 105 105 struct tgfx *tgfx = input_get_drvdata(dev); 106 - int err; 107 106 108 - err = mutex_lock_interruptible(&tgfx->sem); 109 - if (err) 110 - return err; 107 + scoped_guard(mutex_intr, &tgfx->sem) { 108 + if (!tgfx->used++) { 109 + parport_claim(tgfx->pd); 110 + parport_write_control(tgfx->pd->port, 0x04); 111 + mod_timer(&tgfx->timer, jiffies + TGFX_REFRESH_TIME); 112 + } 111 113 112 - if (!tgfx->used++) { 113 - parport_claim(tgfx->pd); 114 - parport_write_control(tgfx->pd->port, 0x04); 115 - mod_timer(&tgfx->timer, jiffies + TGFX_REFRESH_TIME); 114 + return 0; 116 115 } 117 116 118 - mutex_unlock(&tgfx->sem); 119 - return 0; 117 + return -EINTR; 120 118 } 121 119 122 120 static void tgfx_close(struct input_dev *dev) 123 121 { 124 122 struct tgfx *tgfx = input_get_drvdata(dev); 125 123 126 - mutex_lock(&tgfx->sem); 124 + guard(mutex)(&tgfx->sem); 125 + 127 126 if (!--tgfx->used) { 128 127 del_timer_sync(&tgfx->timer); 129 128 parport_write_control(tgfx->pd->port, 0x00); 130 129 parport_release(tgfx->pd); 131 130 } 132 - mutex_unlock(&tgfx->sem); 133 131 } 134 132 135 133