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

Configure Feed

Select the types of activity you want to include in your feed.

ALSA: hwdep: Use guard() for locking

We can simplify the code gracefully with new guard() macro and co for
automatic cleanup of locks.

There are still a few remaining explicit mutex_lock/unlock calls, and
those are for the places where we do temporary unlock/relock, which
doesn't fit well with the guard(), so far.

Only the code refactoring, and no functional changes.

Signed-off-by: Takashi Iwai <tiwai@suse.de>
Link: https://lore.kernel.org/r/20240227085306.9764-6-tiwai@suse.de

+38 -47
+38 -47
sound/core/hwdep.c
··· 149 149 struct snd_hwdep *hw = file->private_data; 150 150 struct module *mod = hw->card->module; 151 151 152 - mutex_lock(&hw->open_mutex); 153 - if (hw->ops.release) 154 - err = hw->ops.release(hw, file); 155 - if (hw->used > 0) 156 - hw->used--; 157 - mutex_unlock(&hw->open_mutex); 152 + scoped_guard(mutex, &hw->open_mutex) { 153 + if (hw->ops.release) 154 + err = hw->ops.release(hw, file); 155 + if (hw->used > 0) 156 + hw->used--; 157 + } 158 158 wake_up(&hw->open_wait); 159 159 160 160 snd_card_file_remove(hw->card, file); ··· 272 272 273 273 if (get_user(device, (int __user *)arg)) 274 274 return -EFAULT; 275 - mutex_lock(&register_mutex); 276 275 277 - if (device < 0) 278 - device = 0; 279 - else if (device < SNDRV_MINOR_HWDEPS) 280 - device++; 281 - else 282 - device = SNDRV_MINOR_HWDEPS; 276 + scoped_guard(mutex, &register_mutex) { 277 + if (device < 0) 278 + device = 0; 279 + else if (device < SNDRV_MINOR_HWDEPS) 280 + device++; 281 + else 282 + device = SNDRV_MINOR_HWDEPS; 283 283 284 - while (device < SNDRV_MINOR_HWDEPS) { 285 - if (snd_hwdep_search(card, device)) 286 - break; 287 - device++; 284 + while (device < SNDRV_MINOR_HWDEPS) { 285 + if (snd_hwdep_search(card, device)) 286 + break; 287 + device++; 288 + } 289 + if (device >= SNDRV_MINOR_HWDEPS) 290 + device = -1; 291 + if (put_user(device, (int __user *)arg)) 292 + return -EFAULT; 293 + return 0; 288 294 } 289 - if (device >= SNDRV_MINOR_HWDEPS) 290 - device = -1; 291 - mutex_unlock(&register_mutex); 292 - if (put_user(device, (int __user *)arg)) 293 - return -EFAULT; 294 - return 0; 295 + break; 295 296 } 296 297 case SNDRV_CTL_IOCTL_HWDEP_INFO: 297 298 { 298 299 struct snd_hwdep_info __user *info = (struct snd_hwdep_info __user *)arg; 299 - int device, err; 300 + int device; 300 301 struct snd_hwdep *hwdep; 301 302 302 303 if (get_user(device, &info->device)) 303 304 return -EFAULT; 304 - mutex_lock(&register_mutex); 305 - hwdep = snd_hwdep_search(card, device); 306 - if (hwdep) 307 - err = snd_hwdep_info(hwdep, info); 308 - else 309 - err = -ENXIO; 310 - mutex_unlock(&register_mutex); 311 - return err; 305 + scoped_guard(mutex, &register_mutex) { 306 + hwdep = snd_hwdep_search(card, device); 307 + if (!hwdep) 308 + return -ENXIO; 309 + return snd_hwdep_info(hwdep, info); 310 + } 311 + break; 312 312 } 313 313 } 314 314 return -ENOIOCTLCMD; ··· 422 422 struct snd_card *card = hwdep->card; 423 423 int err; 424 424 425 - mutex_lock(&register_mutex); 426 - if (snd_hwdep_search(card, hwdep->device)) { 427 - mutex_unlock(&register_mutex); 425 + guard(mutex)(&register_mutex); 426 + if (snd_hwdep_search(card, hwdep->device)) 428 427 return -EBUSY; 429 - } 430 428 list_add_tail(&hwdep->list, &snd_hwdep_devices); 431 429 err = snd_register_device(SNDRV_DEVICE_TYPE_HWDEP, 432 430 hwdep->card, hwdep->device, ··· 432 434 if (err < 0) { 433 435 dev_err(hwdep->dev, "unable to register\n"); 434 436 list_del(&hwdep->list); 435 - mutex_unlock(&register_mutex); 436 437 return err; 437 438 } 438 439 ··· 451 454 hwdep->ossreg = 1; 452 455 } 453 456 #endif 454 - mutex_unlock(&register_mutex); 455 457 return 0; 456 458 } 457 459 ··· 460 464 461 465 if (snd_BUG_ON(!hwdep)) 462 466 return -ENXIO; 463 - mutex_lock(&register_mutex); 464 - if (snd_hwdep_search(hwdep->card, hwdep->device) != hwdep) { 465 - mutex_unlock(&register_mutex); 467 + guard(mutex)(&register_mutex); 468 + if (snd_hwdep_search(hwdep->card, hwdep->device) != hwdep) 466 469 return -EINVAL; 467 - } 468 - mutex_lock(&hwdep->open_mutex); 470 + guard(mutex)(&hwdep->open_mutex); 469 471 wake_up(&hwdep->open_wait); 470 472 #ifdef CONFIG_SND_OSSEMUL 471 473 if (hwdep->ossreg) ··· 471 477 #endif 472 478 snd_unregister_device(hwdep->dev); 473 479 list_del_init(&hwdep->list); 474 - mutex_unlock(&hwdep->open_mutex); 475 - mutex_unlock(&register_mutex); 476 480 return 0; 477 481 } 478 482 ··· 484 492 { 485 493 struct snd_hwdep *hwdep; 486 494 487 - mutex_lock(&register_mutex); 495 + guard(mutex)(&register_mutex); 488 496 list_for_each_entry(hwdep, &snd_hwdep_devices, list) 489 497 snd_iprintf(buffer, "%02i-%02i: %s\n", 490 498 hwdep->card->number, hwdep->device, hwdep->name); 491 - mutex_unlock(&register_mutex); 492 499 } 493 500 494 501 static struct snd_info_entry *snd_hwdep_proc_entry;