Commit 4b72362b authored by Takashi Iwai's avatar Takashi Iwai

ALSA: info: 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-7-tiwai@suse.de
parent e6684d08
...@@ -105,17 +105,15 @@ static loff_t snd_info_entry_llseek(struct file *file, loff_t offset, int orig) ...@@ -105,17 +105,15 @@ static loff_t snd_info_entry_llseek(struct file *file, loff_t offset, int orig)
{ {
struct snd_info_private_data *data; struct snd_info_private_data *data;
struct snd_info_entry *entry; struct snd_info_entry *entry;
loff_t ret = -EINVAL, size; loff_t size;
data = file->private_data; data = file->private_data;
entry = data->entry; entry = data->entry;
mutex_lock(&entry->access); guard(mutex)(&entry->access);
if (entry->c.ops->llseek) { if (entry->c.ops->llseek)
ret = entry->c.ops->llseek(entry, return entry->c.ops->llseek(entry,
data->file_private_data, data->file_private_data,
file, offset, orig); file, offset, orig);
goto out;
}
size = entry->size; size = entry->size;
switch (orig) { switch (orig) {
...@@ -126,21 +124,18 @@ static loff_t snd_info_entry_llseek(struct file *file, loff_t offset, int orig) ...@@ -126,21 +124,18 @@ static loff_t snd_info_entry_llseek(struct file *file, loff_t offset, int orig)
break; break;
case SEEK_END: case SEEK_END:
if (!size) if (!size)
goto out; return -EINVAL;
offset += size; offset += size;
break; break;
default: default:
goto out; return -EINVAL;
} }
if (offset < 0) if (offset < 0)
goto out; return -EINVAL;
if (size && offset > size) if (size && offset > size)
offset = size; offset = size;
file->f_pos = offset; file->f_pos = offset;
ret = offset; return offset;
out:
mutex_unlock(&entry->access);
return ret;
} }
static ssize_t snd_info_entry_read(struct file *file, char __user *buffer, static ssize_t snd_info_entry_read(struct file *file, char __user *buffer,
...@@ -238,10 +233,10 @@ static int snd_info_entry_open(struct inode *inode, struct file *file) ...@@ -238,10 +233,10 @@ static int snd_info_entry_open(struct inode *inode, struct file *file)
struct snd_info_private_data *data; struct snd_info_private_data *data;
int mode, err; int mode, err;
mutex_lock(&info_mutex); guard(mutex)(&info_mutex);
err = alloc_info_private(entry, &data); err = alloc_info_private(entry, &data);
if (err < 0) if (err < 0)
goto unlock; return err;
mode = file->f_flags & O_ACCMODE; mode = file->f_flags & O_ACCMODE;
if (((mode == O_RDONLY || mode == O_RDWR) && !entry->c.ops->read) || if (((mode == O_RDONLY || mode == O_RDWR) && !entry->c.ops->read) ||
...@@ -257,14 +252,11 @@ static int snd_info_entry_open(struct inode *inode, struct file *file) ...@@ -257,14 +252,11 @@ static int snd_info_entry_open(struct inode *inode, struct file *file)
} }
file->private_data = data; file->private_data = data;
mutex_unlock(&info_mutex);
return 0; return 0;
error: error:
kfree(data); kfree(data);
module_put(entry->module); module_put(entry->module);
unlock:
mutex_unlock(&info_mutex);
return err; return err;
} }
...@@ -306,7 +298,6 @@ static ssize_t snd_info_text_entry_write(struct file *file, ...@@ -306,7 +298,6 @@ static ssize_t snd_info_text_entry_write(struct file *file,
struct snd_info_buffer *buf; struct snd_info_buffer *buf;
loff_t pos; loff_t pos;
size_t next; size_t next;
int err = 0;
if (!entry->c.text.write) if (!entry->c.text.write)
return -EIO; return -EIO;
...@@ -317,34 +308,24 @@ static ssize_t snd_info_text_entry_write(struct file *file, ...@@ -317,34 +308,24 @@ static ssize_t snd_info_text_entry_write(struct file *file,
/* don't handle too large text inputs */ /* don't handle too large text inputs */
if (next > 16 * 1024) if (next > 16 * 1024)
return -EIO; return -EIO;
mutex_lock(&entry->access); guard(mutex)(&entry->access);
buf = data->wbuffer; buf = data->wbuffer;
if (!buf) { if (!buf) {
data->wbuffer = buf = kzalloc(sizeof(*buf), GFP_KERNEL); data->wbuffer = buf = kzalloc(sizeof(*buf), GFP_KERNEL);
if (!buf) { if (!buf)
err = -ENOMEM; return -ENOMEM;
goto error;
}
} }
if (next > buf->len) { if (next > buf->len) {
char *nbuf = kvzalloc(PAGE_ALIGN(next), GFP_KERNEL); char *nbuf = kvzalloc(PAGE_ALIGN(next), GFP_KERNEL);
if (!nbuf) { if (!nbuf)
err = -ENOMEM; return -ENOMEM;
goto error;
}
kvfree(buf->buffer); kvfree(buf->buffer);
buf->buffer = nbuf; buf->buffer = nbuf;
buf->len = PAGE_ALIGN(next); buf->len = PAGE_ALIGN(next);
} }
if (copy_from_user(buf->buffer + pos, buffer, count)) { if (copy_from_user(buf->buffer + pos, buffer, count))
err = -EFAULT; return -EFAULT;
goto error;
}
buf->size = next; buf->size = next;
error:
mutex_unlock(&entry->access);
if (err < 0)
return err;
*offset = next; *offset = next;
return count; return count;
} }
...@@ -369,10 +350,10 @@ static int snd_info_text_entry_open(struct inode *inode, struct file *file) ...@@ -369,10 +350,10 @@ static int snd_info_text_entry_open(struct inode *inode, struct file *file)
struct snd_info_private_data *data; struct snd_info_private_data *data;
int err; int err;
mutex_lock(&info_mutex); guard(mutex)(&info_mutex);
err = alloc_info_private(entry, &data); err = alloc_info_private(entry, &data);
if (err < 0) if (err < 0)
goto unlock; return err;
data->rbuffer = kzalloc(sizeof(*data->rbuffer), GFP_KERNEL); data->rbuffer = kzalloc(sizeof(*data->rbuffer), GFP_KERNEL);
if (!data->rbuffer) { if (!data->rbuffer) {
...@@ -386,15 +367,12 @@ static int snd_info_text_entry_open(struct inode *inode, struct file *file) ...@@ -386,15 +367,12 @@ static int snd_info_text_entry_open(struct inode *inode, struct file *file)
err = single_open(file, snd_info_seq_show, data); err = single_open(file, snd_info_seq_show, data);
if (err < 0) if (err < 0)
goto error; goto error;
mutex_unlock(&info_mutex);
return 0; return 0;
error: error:
kfree(data->rbuffer); kfree(data->rbuffer);
kfree(data); kfree(data);
module_put(entry->module); module_put(entry->module);
unlock:
mutex_unlock(&info_mutex);
return err; return err;
} }
...@@ -549,7 +527,7 @@ int snd_info_card_register(struct snd_card *card) ...@@ -549,7 +527,7 @@ int snd_info_card_register(struct snd_card *card)
*/ */
void snd_info_card_id_change(struct snd_card *card) void snd_info_card_id_change(struct snd_card *card)
{ {
mutex_lock(&info_mutex); guard(mutex)(&info_mutex);
if (card->proc_root_link) { if (card->proc_root_link) {
proc_remove(card->proc_root_link); proc_remove(card->proc_root_link);
card->proc_root_link = NULL; card->proc_root_link = NULL;
...@@ -558,7 +536,6 @@ void snd_info_card_id_change(struct snd_card *card) ...@@ -558,7 +536,6 @@ void snd_info_card_id_change(struct snd_card *card)
card->proc_root_link = proc_symlink(card->id, card->proc_root_link = proc_symlink(card->id,
snd_proc_root->p, snd_proc_root->p,
card->proc_root->name); card->proc_root->name);
mutex_unlock(&info_mutex);
} }
/* /*
...@@ -574,12 +551,11 @@ void snd_info_card_disconnect(struct snd_card *card) ...@@ -574,12 +551,11 @@ void snd_info_card_disconnect(struct snd_card *card)
if (card->proc_root) if (card->proc_root)
proc_remove(card->proc_root->p); proc_remove(card->proc_root->p);
mutex_lock(&info_mutex); guard(mutex)(&info_mutex);
if (card->proc_root) if (card->proc_root)
snd_info_clear_entries(card->proc_root); snd_info_clear_entries(card->proc_root);
card->proc_root_link = NULL; card->proc_root_link = NULL;
card->proc_root = NULL; card->proc_root = NULL;
mutex_unlock(&info_mutex);
} }
/* /*
...@@ -703,9 +679,8 @@ snd_info_create_entry(const char *name, struct snd_info_entry *parent, ...@@ -703,9 +679,8 @@ snd_info_create_entry(const char *name, struct snd_info_entry *parent,
entry->parent = parent; entry->parent = parent;
entry->module = module; entry->module = module;
if (parent) { if (parent) {
mutex_lock(&parent->access); guard(mutex)(&parent->access);
list_add_tail(&entry->list, &parent->children); list_add_tail(&entry->list, &parent->children);
mutex_unlock(&parent->access);
} }
return entry; return entry;
} }
...@@ -775,9 +750,8 @@ void snd_info_free_entry(struct snd_info_entry * entry) ...@@ -775,9 +750,8 @@ void snd_info_free_entry(struct snd_info_entry * entry)
return; return;
if (entry->p) { if (entry->p) {
proc_remove(entry->p); proc_remove(entry->p);
mutex_lock(&info_mutex); guard(mutex)(&info_mutex);
snd_info_clear_entries(entry); snd_info_clear_entries(entry);
mutex_unlock(&info_mutex);
} }
/* free all children at first */ /* free all children at first */
...@@ -786,9 +760,8 @@ void snd_info_free_entry(struct snd_info_entry * entry) ...@@ -786,9 +760,8 @@ void snd_info_free_entry(struct snd_info_entry * entry)
p = entry->parent; p = entry->parent;
if (p) { if (p) {
mutex_lock(&p->access); guard(mutex)(&p->access);
list_del(&entry->list); list_del(&entry->list);
mutex_unlock(&p->access);
} }
kfree(entry->name); kfree(entry->name);
if (entry->private_free) if (entry->private_free)
...@@ -804,15 +777,13 @@ static int __snd_info_register(struct snd_info_entry *entry) ...@@ -804,15 +777,13 @@ static int __snd_info_register(struct snd_info_entry *entry)
if (snd_BUG_ON(!entry)) if (snd_BUG_ON(!entry))
return -ENXIO; return -ENXIO;
root = entry->parent == NULL ? snd_proc_root->p : entry->parent->p; root = entry->parent == NULL ? snd_proc_root->p : entry->parent->p;
mutex_lock(&info_mutex); guard(mutex)(&info_mutex);
if (entry->p || !root) if (entry->p || !root)
goto unlock; return 0;
if (S_ISDIR(entry->mode)) { if (S_ISDIR(entry->mode)) {
p = proc_mkdir_mode(entry->name, entry->mode, root); p = proc_mkdir_mode(entry->name, entry->mode, root);
if (!p) { if (!p)
mutex_unlock(&info_mutex);
return -ENOMEM; return -ENOMEM;
}
} else { } else {
const struct proc_ops *ops; const struct proc_ops *ops;
if (entry->content == SNDRV_INFO_CONTENT_DATA) if (entry->content == SNDRV_INFO_CONTENT_DATA)
...@@ -821,15 +792,11 @@ static int __snd_info_register(struct snd_info_entry *entry) ...@@ -821,15 +792,11 @@ static int __snd_info_register(struct snd_info_entry *entry)
ops = &snd_info_text_entry_ops; ops = &snd_info_text_entry_ops;
p = proc_create_data(entry->name, entry->mode, root, p = proc_create_data(entry->name, entry->mode, root,
ops, entry); ops, entry);
if (!p) { if (!p)
mutex_unlock(&info_mutex);
return -ENOMEM; return -ENOMEM;
}
proc_set_size(p, entry->size); proc_set_size(p, entry->size);
} }
entry->p = p; entry->p = p;
unlock:
mutex_unlock(&info_mutex);
return 0; return 0;
} }
......
...@@ -29,20 +29,17 @@ int snd_oss_info_register(int dev, int num, char *string) ...@@ -29,20 +29,17 @@ int snd_oss_info_register(int dev, int num, char *string)
return -ENXIO; return -ENXIO;
if (snd_BUG_ON(num < 0 || num >= SNDRV_CARDS)) if (snd_BUG_ON(num < 0 || num >= SNDRV_CARDS))
return -ENXIO; return -ENXIO;
mutex_lock(&strings); guard(mutex)(&strings);
if (string == NULL) { if (string == NULL) {
x = snd_sndstat_strings[num][dev]; x = snd_sndstat_strings[num][dev];
kfree(x); kfree(x);
x = NULL; x = NULL;
} else { } else {
x = kstrdup(string, GFP_KERNEL); x = kstrdup(string, GFP_KERNEL);
if (x == NULL) { if (x == NULL)
mutex_unlock(&strings);
return -ENOMEM; return -ENOMEM;
} }
}
snd_sndstat_strings[num][dev] = x; snd_sndstat_strings[num][dev] = x;
mutex_unlock(&strings);
return 0; return 0;
} }
EXPORT_SYMBOL(snd_oss_info_register); EXPORT_SYMBOL(snd_oss_info_register);
...@@ -53,7 +50,7 @@ static int snd_sndstat_show_strings(struct snd_info_buffer *buf, char *id, int d ...@@ -53,7 +50,7 @@ static int snd_sndstat_show_strings(struct snd_info_buffer *buf, char *id, int d
char *str; char *str;
snd_iprintf(buf, "\n%s:", id); snd_iprintf(buf, "\n%s:", id);
mutex_lock(&strings); guard(mutex)(&strings);
for (idx = 0; idx < SNDRV_CARDS; idx++) { for (idx = 0; idx < SNDRV_CARDS; idx++) {
str = snd_sndstat_strings[idx][dev]; str = snd_sndstat_strings[idx][dev];
if (str) { if (str) {
...@@ -64,7 +61,6 @@ static int snd_sndstat_show_strings(struct snd_info_buffer *buf, char *id, int d ...@@ -64,7 +61,6 @@ static int snd_sndstat_show_strings(struct snd_info_buffer *buf, char *id, int d
snd_iprintf(buf, "%i: %s\n", idx, str); snd_iprintf(buf, "%i: %s\n", idx, str);
} }
} }
mutex_unlock(&strings);
if (ok < 0) if (ok < 0)
snd_iprintf(buf, " NOT ENABLED IN CONFIG\n"); snd_iprintf(buf, " NOT ENABLED IN CONFIG\n");
return ok; return ok;
......
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