Commit 742ecf3c authored by Takashi Iwai's avatar Takashi Iwai

ALSA: core: Use guard() for locking

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

Only the code refactoring, and no functional changes.
Signed-off-by: default avatarTakashi Iwai <tiwai@suse.de>
Link: https://lore.kernel.org/r/20240227085306.9764-12-tiwai@suse.de
parent 7234795b
This diff is collapsed.
...@@ -103,7 +103,7 @@ void *snd_lookup_minor_data(unsigned int minor, int type) ...@@ -103,7 +103,7 @@ void *snd_lookup_minor_data(unsigned int minor, int type)
if (minor >= ARRAY_SIZE(snd_minors)) if (minor >= ARRAY_SIZE(snd_minors))
return NULL; return NULL;
mutex_lock(&sound_mutex); guard(mutex)(&sound_mutex);
mreg = snd_minors[minor]; mreg = snd_minors[minor];
if (mreg && mreg->type == type) { if (mreg && mreg->type == type) {
private_data = mreg->private_data; private_data = mreg->private_data;
...@@ -111,7 +111,6 @@ void *snd_lookup_minor_data(unsigned int minor, int type) ...@@ -111,7 +111,6 @@ void *snd_lookup_minor_data(unsigned int minor, int type)
get_device(&mreg->card_ptr->card_dev); get_device(&mreg->card_ptr->card_dev);
} else } else
private_data = NULL; private_data = NULL;
mutex_unlock(&sound_mutex);
return private_data; return private_data;
} }
EXPORT_SYMBOL(snd_lookup_minor_data); EXPORT_SYMBOL(snd_lookup_minor_data);
...@@ -150,17 +149,15 @@ static int snd_open(struct inode *inode, struct file *file) ...@@ -150,17 +149,15 @@ static int snd_open(struct inode *inode, struct file *file)
if (minor >= ARRAY_SIZE(snd_minors)) if (minor >= ARRAY_SIZE(snd_minors))
return -ENODEV; return -ENODEV;
mutex_lock(&sound_mutex); scoped_guard(mutex, &sound_mutex) {
mptr = snd_minors[minor]; mptr = snd_minors[minor];
if (mptr == NULL) { if (mptr == NULL) {
mptr = autoload_device(minor); mptr = autoload_device(minor);
if (!mptr) { if (!mptr)
mutex_unlock(&sound_mutex); return -ENODEV;
return -ENODEV;
} }
new_fops = fops_get(mptr->f_ops);
} }
new_fops = fops_get(mptr->f_ops);
mutex_unlock(&sound_mutex);
if (!new_fops) if (!new_fops)
return -ENODEV; return -ENODEV;
replace_fops(file, new_fops); replace_fops(file, new_fops);
...@@ -269,7 +266,7 @@ int snd_register_device(int type, struct snd_card *card, int dev, ...@@ -269,7 +266,7 @@ int snd_register_device(int type, struct snd_card *card, int dev,
preg->f_ops = f_ops; preg->f_ops = f_ops;
preg->private_data = private_data; preg->private_data = private_data;
preg->card_ptr = card; preg->card_ptr = card;
mutex_lock(&sound_mutex); guard(mutex)(&sound_mutex);
minor = snd_find_free_minor(type, card, dev); minor = snd_find_free_minor(type, card, dev);
if (minor < 0) { if (minor < 0) {
err = minor; err = minor;
...@@ -284,7 +281,6 @@ int snd_register_device(int type, struct snd_card *card, int dev, ...@@ -284,7 +281,6 @@ int snd_register_device(int type, struct snd_card *card, int dev,
snd_minors[minor] = preg; snd_minors[minor] = preg;
error: error:
mutex_unlock(&sound_mutex);
if (err < 0) if (err < 0)
kfree(preg); kfree(preg);
return err; return err;
...@@ -305,7 +301,7 @@ int snd_unregister_device(struct device *dev) ...@@ -305,7 +301,7 @@ int snd_unregister_device(struct device *dev)
int minor; int minor;
struct snd_minor *preg; struct snd_minor *preg;
mutex_lock(&sound_mutex); guard(mutex)(&sound_mutex);
for (minor = 0; minor < ARRAY_SIZE(snd_minors); ++minor) { for (minor = 0; minor < ARRAY_SIZE(snd_minors); ++minor) {
preg = snd_minors[minor]; preg = snd_minors[minor];
if (preg && preg->dev == dev) { if (preg && preg->dev == dev) {
...@@ -315,7 +311,6 @@ int snd_unregister_device(struct device *dev) ...@@ -315,7 +311,6 @@ int snd_unregister_device(struct device *dev)
break; break;
} }
} }
mutex_unlock(&sound_mutex);
if (minor >= ARRAY_SIZE(snd_minors)) if (minor >= ARRAY_SIZE(snd_minors))
return -ENOENT; return -ENOENT;
return 0; return 0;
...@@ -355,7 +350,7 @@ static void snd_minor_info_read(struct snd_info_entry *entry, struct snd_info_bu ...@@ -355,7 +350,7 @@ static void snd_minor_info_read(struct snd_info_entry *entry, struct snd_info_bu
int minor; int minor;
struct snd_minor *mptr; struct snd_minor *mptr;
mutex_lock(&sound_mutex); guard(mutex)(&sound_mutex);
for (minor = 0; minor < SNDRV_OS_MINORS; ++minor) { for (minor = 0; minor < SNDRV_OS_MINORS; ++minor) {
mptr = snd_minors[minor]; mptr = snd_minors[minor];
if (!mptr) if (!mptr)
...@@ -373,7 +368,6 @@ static void snd_minor_info_read(struct snd_info_entry *entry, struct snd_info_bu ...@@ -373,7 +368,6 @@ static void snd_minor_info_read(struct snd_info_entry *entry, struct snd_info_bu
snd_iprintf(buffer, "%3i: : %s\n", minor, snd_iprintf(buffer, "%3i: : %s\n", minor,
snd_device_type_name(mptr->type)); snd_device_type_name(mptr->type));
} }
mutex_unlock(&sound_mutex);
} }
int __init snd_minor_info_init(void) int __init snd_minor_info_init(void)
......
...@@ -29,7 +29,7 @@ void *snd_lookup_oss_minor_data(unsigned int minor, int type) ...@@ -29,7 +29,7 @@ void *snd_lookup_oss_minor_data(unsigned int minor, int type)
if (minor >= ARRAY_SIZE(snd_oss_minors)) if (minor >= ARRAY_SIZE(snd_oss_minors))
return NULL; return NULL;
mutex_lock(&sound_oss_mutex); guard(mutex)(&sound_oss_mutex);
mreg = snd_oss_minors[minor]; mreg = snd_oss_minors[minor];
if (mreg && mreg->type == type) { if (mreg && mreg->type == type) {
private_data = mreg->private_data; private_data = mreg->private_data;
...@@ -37,7 +37,6 @@ void *snd_lookup_oss_minor_data(unsigned int minor, int type) ...@@ -37,7 +37,6 @@ void *snd_lookup_oss_minor_data(unsigned int minor, int type)
get_device(&mreg->card_ptr->card_dev); get_device(&mreg->card_ptr->card_dev);
} else } else
private_data = NULL; private_data = NULL;
mutex_unlock(&sound_oss_mutex);
return private_data; return private_data;
} }
EXPORT_SYMBOL(snd_lookup_oss_minor_data); EXPORT_SYMBOL(snd_lookup_oss_minor_data);
...@@ -106,7 +105,7 @@ int snd_register_oss_device(int type, struct snd_card *card, int dev, ...@@ -106,7 +105,7 @@ int snd_register_oss_device(int type, struct snd_card *card, int dev,
preg->f_ops = f_ops; preg->f_ops = f_ops;
preg->private_data = private_data; preg->private_data = private_data;
preg->card_ptr = card; preg->card_ptr = card;
mutex_lock(&sound_oss_mutex); guard(mutex)(&sound_oss_mutex);
snd_oss_minors[minor] = preg; snd_oss_minors[minor] = preg;
minor_unit = SNDRV_MINOR_OSS_DEVICE(minor); minor_unit = SNDRV_MINOR_OSS_DEVICE(minor);
switch (minor_unit) { switch (minor_unit) {
...@@ -130,7 +129,6 @@ int snd_register_oss_device(int type, struct snd_card *card, int dev, ...@@ -130,7 +129,6 @@ int snd_register_oss_device(int type, struct snd_card *card, int dev,
goto __end; goto __end;
snd_oss_minors[track2] = preg; snd_oss_minors[track2] = preg;
} }
mutex_unlock(&sound_oss_mutex);
return 0; return 0;
__end: __end:
...@@ -139,7 +137,6 @@ int snd_register_oss_device(int type, struct snd_card *card, int dev, ...@@ -139,7 +137,6 @@ int snd_register_oss_device(int type, struct snd_card *card, int dev,
if (register1 >= 0) if (register1 >= 0)
unregister_sound_special(register1); unregister_sound_special(register1);
snd_oss_minors[minor] = NULL; snd_oss_minors[minor] = NULL;
mutex_unlock(&sound_oss_mutex);
kfree(preg); kfree(preg);
return -EBUSY; return -EBUSY;
} }
...@@ -156,12 +153,10 @@ int snd_unregister_oss_device(int type, struct snd_card *card, int dev) ...@@ -156,12 +153,10 @@ int snd_unregister_oss_device(int type, struct snd_card *card, int dev)
return 0; return 0;
if (minor < 0) if (minor < 0)
return minor; return minor;
mutex_lock(&sound_oss_mutex); guard(mutex)(&sound_oss_mutex);
mptr = snd_oss_minors[minor]; mptr = snd_oss_minors[minor];
if (mptr == NULL) { if (mptr == NULL)
mutex_unlock(&sound_oss_mutex);
return -ENOENT; return -ENOENT;
}
switch (SNDRV_MINOR_OSS_DEVICE(minor)) { switch (SNDRV_MINOR_OSS_DEVICE(minor)) {
case SNDRV_MINOR_OSS_PCM: case SNDRV_MINOR_OSS_PCM:
track2 = SNDRV_MINOR_OSS(cidx, SNDRV_MINOR_OSS_AUDIO); track2 = SNDRV_MINOR_OSS(cidx, SNDRV_MINOR_OSS_AUDIO);
...@@ -176,7 +171,6 @@ int snd_unregister_oss_device(int type, struct snd_card *card, int dev) ...@@ -176,7 +171,6 @@ int snd_unregister_oss_device(int type, struct snd_card *card, int dev)
if (track2 >= 0) if (track2 >= 0)
snd_oss_minors[track2] = NULL; snd_oss_minors[track2] = NULL;
snd_oss_minors[minor] = NULL; snd_oss_minors[minor] = NULL;
mutex_unlock(&sound_oss_mutex);
/* call unregister_sound_special() outside sound_oss_mutex; /* call unregister_sound_special() outside sound_oss_mutex;
* otherwise may deadlock, as it can trigger the release of a card * otherwise may deadlock, as it can trigger the release of a card
...@@ -220,7 +214,7 @@ static void snd_minor_info_oss_read(struct snd_info_entry *entry, ...@@ -220,7 +214,7 @@ static void snd_minor_info_oss_read(struct snd_info_entry *entry,
int minor; int minor;
struct snd_minor *mptr; struct snd_minor *mptr;
mutex_lock(&sound_oss_mutex); guard(mutex)(&sound_oss_mutex);
for (minor = 0; minor < SNDRV_OSS_MINORS; ++minor) { for (minor = 0; minor < SNDRV_OSS_MINORS; ++minor) {
mptr = snd_oss_minors[minor]; mptr = snd_oss_minors[minor];
if (!mptr) if (!mptr)
...@@ -233,7 +227,6 @@ static void snd_minor_info_oss_read(struct snd_info_entry *entry, ...@@ -233,7 +227,6 @@ static void snd_minor_info_oss_read(struct snd_info_entry *entry,
snd_iprintf(buffer, "%3i: : %s\n", minor, snd_iprintf(buffer, "%3i: : %s\n", minor,
snd_oss_device_type_name(mptr->type)); snd_oss_device_type_name(mptr->type));
} }
mutex_unlock(&sound_oss_mutex);
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment