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

Input: pegasus_notetaker - 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.

Reviewed-by: Javier Carrasco <javier.carrasco.cruz@gmail.com>
Link: https://lore.kernel.org/r/ZtjKJsArLu3byTU6@google.com
Signed-off-by: Dmitry Torokhov <dmitry.torokhov@gmail.com>

+49 -39
+49 -39
drivers/input/tablet/pegasus_notetaker.c
··· 214 214 error); 215 215 } 216 216 217 + static int __pegasus_open(struct pegasus *pegasus) 218 + { 219 + int error; 220 + 221 + guard(mutex)(&pegasus->pm_mutex); 222 + 223 + pegasus->irq->dev = pegasus->usbdev; 224 + if (usb_submit_urb(pegasus->irq, GFP_KERNEL)) 225 + return -EIO; 226 + 227 + error = pegasus_set_mode(pegasus, PEN_MODE_XY, NOTETAKER_LED_MOUSE); 228 + if (error) { 229 + usb_kill_urb(pegasus->irq); 230 + cancel_work_sync(&pegasus->init); 231 + return error; 232 + } 233 + 234 + pegasus->is_open = true; 235 + 236 + return 0; 237 + } 238 + 217 239 static int pegasus_open(struct input_dev *dev) 218 240 { 219 241 struct pegasus *pegasus = input_get_drvdata(dev); ··· 245 223 if (error) 246 224 return error; 247 225 248 - mutex_lock(&pegasus->pm_mutex); 249 - pegasus->irq->dev = pegasus->usbdev; 250 - if (usb_submit_urb(pegasus->irq, GFP_KERNEL)) { 251 - error = -EIO; 252 - goto err_autopm_put; 226 + error = __pegasus_open(pegasus); 227 + if (error) { 228 + usb_autopm_put_interface(pegasus->intf); 229 + return error; 253 230 } 254 231 255 - error = pegasus_set_mode(pegasus, PEN_MODE_XY, NOTETAKER_LED_MOUSE); 256 - if (error) 257 - goto err_kill_urb; 258 - 259 - pegasus->is_open = true; 260 - mutex_unlock(&pegasus->pm_mutex); 261 232 return 0; 262 - 263 - err_kill_urb: 264 - usb_kill_urb(pegasus->irq); 265 - cancel_work_sync(&pegasus->init); 266 - err_autopm_put: 267 - mutex_unlock(&pegasus->pm_mutex); 268 - usb_autopm_put_interface(pegasus->intf); 269 - return error; 270 233 } 271 234 272 235 static void pegasus_close(struct input_dev *dev) 273 236 { 274 237 struct pegasus *pegasus = input_get_drvdata(dev); 275 238 276 - mutex_lock(&pegasus->pm_mutex); 277 - usb_kill_urb(pegasus->irq); 278 - cancel_work_sync(&pegasus->init); 279 - pegasus->is_open = false; 280 - mutex_unlock(&pegasus->pm_mutex); 239 + scoped_guard(mutex, &pegasus->pm_mutex) { 240 + usb_kill_urb(pegasus->irq); 241 + cancel_work_sync(&pegasus->init); 242 + 243 + pegasus->is_open = false; 244 + } 281 245 282 246 usb_autopm_put_interface(pegasus->intf); 283 247 } ··· 419 411 { 420 412 struct pegasus *pegasus = usb_get_intfdata(intf); 421 413 422 - mutex_lock(&pegasus->pm_mutex); 414 + guard(mutex)(&pegasus->pm_mutex); 415 + 423 416 usb_kill_urb(pegasus->irq); 424 417 cancel_work_sync(&pegasus->init); 425 - mutex_unlock(&pegasus->pm_mutex); 426 418 427 419 return 0; 428 420 } ··· 430 422 static int pegasus_resume(struct usb_interface *intf) 431 423 { 432 424 struct pegasus *pegasus = usb_get_intfdata(intf); 433 - int retval = 0; 434 425 435 - mutex_lock(&pegasus->pm_mutex); 426 + guard(mutex)(&pegasus->pm_mutex); 427 + 436 428 if (pegasus->is_open && usb_submit_urb(pegasus->irq, GFP_NOIO) < 0) 437 - retval = -EIO; 438 - mutex_unlock(&pegasus->pm_mutex); 429 + return -EIO; 439 430 440 - return retval; 431 + return 0; 441 432 } 442 433 443 434 static int pegasus_reset_resume(struct usb_interface *intf) 444 435 { 445 436 struct pegasus *pegasus = usb_get_intfdata(intf); 446 - int retval = 0; 437 + int error; 447 438 448 - mutex_lock(&pegasus->pm_mutex); 439 + guard(mutex)(&pegasus->pm_mutex); 440 + 449 441 if (pegasus->is_open) { 450 - retval = pegasus_set_mode(pegasus, PEN_MODE_XY, 442 + error = pegasus_set_mode(pegasus, PEN_MODE_XY, 451 443 NOTETAKER_LED_MOUSE); 452 - if (!retval && usb_submit_urb(pegasus->irq, GFP_NOIO) < 0) 453 - retval = -EIO; 454 - } 455 - mutex_unlock(&pegasus->pm_mutex); 444 + if (error) 445 + return error; 456 446 457 - return retval; 447 + if (usb_submit_urb(pegasus->irq, GFP_NOIO) < 0) 448 + return -EIO; 449 + } 450 + 451 + return 0; 458 452 } 459 453 460 454 static const struct usb_device_id pegasus_ids[] = {