Commit d648843a authored by Takashi Iwai's avatar Takashi Iwai

ALSA: compress_offload: Use guard() for locking

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

The explicit mutex_lock/unlock are still seen only in
snd_compress_wait_for_drain() which does temporary unlock/relocking.

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-3-tiwai@suse.de
parent 631896f7
...@@ -127,9 +127,8 @@ static int snd_compr_open(struct inode *inode, struct file *f) ...@@ -127,9 +127,8 @@ static int snd_compr_open(struct inode *inode, struct file *f)
init_waitqueue_head(&runtime->sleep); init_waitqueue_head(&runtime->sleep);
data->stream.runtime = runtime; data->stream.runtime = runtime;
f->private_data = (void *)data; f->private_data = (void *)data;
mutex_lock(&compr->lock); scoped_guard(mutex, &compr->lock)
ret = compr->ops->open(&data->stream); ret = compr->ops->open(&data->stream);
mutex_unlock(&compr->lock);
if (ret) { if (ret) {
kfree(runtime); kfree(runtime);
kfree(data); kfree(data);
...@@ -288,7 +287,7 @@ static ssize_t snd_compr_write(struct file *f, const char __user *buf, ...@@ -288,7 +287,7 @@ static ssize_t snd_compr_write(struct file *f, const char __user *buf,
return -EFAULT; return -EFAULT;
stream = &data->stream; stream = &data->stream;
mutex_lock(&stream->device->lock); guard(mutex)(&stream->device->lock);
/* write is allowed when stream is running or has been steup */ /* write is allowed when stream is running or has been steup */
switch (stream->runtime->state) { switch (stream->runtime->state) {
case SNDRV_PCM_STATE_SETUP: case SNDRV_PCM_STATE_SETUP:
...@@ -296,7 +295,6 @@ static ssize_t snd_compr_write(struct file *f, const char __user *buf, ...@@ -296,7 +295,6 @@ static ssize_t snd_compr_write(struct file *f, const char __user *buf,
case SNDRV_PCM_STATE_RUNNING: case SNDRV_PCM_STATE_RUNNING:
break; break;
default: default:
mutex_unlock(&stream->device->lock);
return -EBADFD; return -EBADFD;
} }
...@@ -322,7 +320,6 @@ static ssize_t snd_compr_write(struct file *f, const char __user *buf, ...@@ -322,7 +320,6 @@ static ssize_t snd_compr_write(struct file *f, const char __user *buf,
pr_debug("stream prepared, Houston we are good to go\n"); pr_debug("stream prepared, Houston we are good to go\n");
} }
mutex_unlock(&stream->device->lock);
return retval; return retval;
} }
...@@ -339,7 +336,7 @@ static ssize_t snd_compr_read(struct file *f, char __user *buf, ...@@ -339,7 +336,7 @@ static ssize_t snd_compr_read(struct file *f, char __user *buf,
return -EFAULT; return -EFAULT;
stream = &data->stream; stream = &data->stream;
mutex_lock(&stream->device->lock); guard(mutex)(&stream->device->lock);
/* read is allowed when stream is running, paused, draining and setup /* read is allowed when stream is running, paused, draining and setup
* (yes setup is state which we transition to after stop, so if user * (yes setup is state which we transition to after stop, so if user
...@@ -350,11 +347,9 @@ static ssize_t snd_compr_read(struct file *f, char __user *buf, ...@@ -350,11 +347,9 @@ static ssize_t snd_compr_read(struct file *f, char __user *buf,
case SNDRV_PCM_STATE_PREPARED: case SNDRV_PCM_STATE_PREPARED:
case SNDRV_PCM_STATE_SUSPENDED: case SNDRV_PCM_STATE_SUSPENDED:
case SNDRV_PCM_STATE_DISCONNECTED: case SNDRV_PCM_STATE_DISCONNECTED:
retval = -EBADFD; return -EBADFD;
goto out;
case SNDRV_PCM_STATE_XRUN: case SNDRV_PCM_STATE_XRUN:
retval = -EPIPE; return -EPIPE;
goto out;
} }
avail = snd_compr_get_avail(stream); avail = snd_compr_get_avail(stream);
...@@ -363,17 +358,13 @@ static ssize_t snd_compr_read(struct file *f, char __user *buf, ...@@ -363,17 +358,13 @@ static ssize_t snd_compr_read(struct file *f, char __user *buf,
if (avail > count) if (avail > count)
avail = count; avail = count;
if (stream->ops->copy) { if (stream->ops->copy)
retval = stream->ops->copy(stream, buf, avail); retval = stream->ops->copy(stream, buf, avail);
} else { else
retval = -ENXIO; return -ENXIO;
goto out;
}
if (retval > 0) if (retval > 0)
stream->runtime->total_bytes_transferred += retval; stream->runtime->total_bytes_transferred += retval;
out:
mutex_unlock(&stream->device->lock);
return retval; return retval;
} }
...@@ -402,13 +393,12 @@ static __poll_t snd_compr_poll(struct file *f, poll_table *wait) ...@@ -402,13 +393,12 @@ static __poll_t snd_compr_poll(struct file *f, poll_table *wait)
stream = &data->stream; stream = &data->stream;
mutex_lock(&stream->device->lock); guard(mutex)(&stream->device->lock);
switch (stream->runtime->state) { switch (stream->runtime->state) {
case SNDRV_PCM_STATE_OPEN: case SNDRV_PCM_STATE_OPEN:
case SNDRV_PCM_STATE_XRUN: case SNDRV_PCM_STATE_XRUN:
retval = snd_compr_get_poll(stream) | EPOLLERR; return snd_compr_get_poll(stream) | EPOLLERR;
goto out;
default: default:
break; break;
} }
...@@ -433,11 +423,9 @@ static __poll_t snd_compr_poll(struct file *f, poll_table *wait) ...@@ -433,11 +423,9 @@ static __poll_t snd_compr_poll(struct file *f, poll_table *wait)
retval = snd_compr_get_poll(stream); retval = snd_compr_get_poll(stream);
break; break;
default: default:
retval = snd_compr_get_poll(stream) | EPOLLERR; return snd_compr_get_poll(stream) | EPOLLERR;
break;
} }
out:
mutex_unlock(&stream->device->lock);
return retval; return retval;
} }
...@@ -795,12 +783,10 @@ static void error_delayed_work(struct work_struct *work) ...@@ -795,12 +783,10 @@ static void error_delayed_work(struct work_struct *work)
stream = container_of(work, struct snd_compr_stream, error_work.work); stream = container_of(work, struct snd_compr_stream, error_work.work);
mutex_lock(&stream->device->lock); guard(mutex)(&stream->device->lock);
stream->ops->trigger(stream, SNDRV_PCM_TRIGGER_STOP); stream->ops->trigger(stream, SNDRV_PCM_TRIGGER_STOP);
wake_up(&stream->runtime->sleep); wake_up(&stream->runtime->sleep);
mutex_unlock(&stream->device->lock);
} }
/** /**
...@@ -957,70 +943,52 @@ static long snd_compr_ioctl(struct file *f, unsigned int cmd, unsigned long arg) ...@@ -957,70 +943,52 @@ static long snd_compr_ioctl(struct file *f, unsigned int cmd, unsigned long arg)
{ {
struct snd_compr_file *data = f->private_data; struct snd_compr_file *data = f->private_data;
struct snd_compr_stream *stream; struct snd_compr_stream *stream;
int retval = -ENOTTY;
if (snd_BUG_ON(!data)) if (snd_BUG_ON(!data))
return -EFAULT; return -EFAULT;
stream = &data->stream; stream = &data->stream;
mutex_lock(&stream->device->lock); guard(mutex)(&stream->device->lock);
switch (_IOC_NR(cmd)) { switch (_IOC_NR(cmd)) {
case _IOC_NR(SNDRV_COMPRESS_IOCTL_VERSION): case _IOC_NR(SNDRV_COMPRESS_IOCTL_VERSION):
retval = put_user(SNDRV_COMPRESS_VERSION, return put_user(SNDRV_COMPRESS_VERSION,
(int __user *)arg) ? -EFAULT : 0; (int __user *)arg) ? -EFAULT : 0;
break;
case _IOC_NR(SNDRV_COMPRESS_GET_CAPS): case _IOC_NR(SNDRV_COMPRESS_GET_CAPS):
retval = snd_compr_get_caps(stream, arg); return snd_compr_get_caps(stream, arg);
break;
#ifndef COMPR_CODEC_CAPS_OVERFLOW #ifndef COMPR_CODEC_CAPS_OVERFLOW
case _IOC_NR(SNDRV_COMPRESS_GET_CODEC_CAPS): case _IOC_NR(SNDRV_COMPRESS_GET_CODEC_CAPS):
retval = snd_compr_get_codec_caps(stream, arg); return snd_compr_get_codec_caps(stream, arg);
break;
#endif #endif
case _IOC_NR(SNDRV_COMPRESS_SET_PARAMS): case _IOC_NR(SNDRV_COMPRESS_SET_PARAMS):
retval = snd_compr_set_params(stream, arg); return snd_compr_set_params(stream, arg);
break;
case _IOC_NR(SNDRV_COMPRESS_GET_PARAMS): case _IOC_NR(SNDRV_COMPRESS_GET_PARAMS):
retval = snd_compr_get_params(stream, arg); return snd_compr_get_params(stream, arg);
break;
case _IOC_NR(SNDRV_COMPRESS_SET_METADATA): case _IOC_NR(SNDRV_COMPRESS_SET_METADATA):
retval = snd_compr_set_metadata(stream, arg); return snd_compr_set_metadata(stream, arg);
break;
case _IOC_NR(SNDRV_COMPRESS_GET_METADATA): case _IOC_NR(SNDRV_COMPRESS_GET_METADATA):
retval = snd_compr_get_metadata(stream, arg); return snd_compr_get_metadata(stream, arg);
break;
case _IOC_NR(SNDRV_COMPRESS_TSTAMP): case _IOC_NR(SNDRV_COMPRESS_TSTAMP):
retval = snd_compr_tstamp(stream, arg); return snd_compr_tstamp(stream, arg);
break;
case _IOC_NR(SNDRV_COMPRESS_AVAIL): case _IOC_NR(SNDRV_COMPRESS_AVAIL):
retval = snd_compr_ioctl_avail(stream, arg); return snd_compr_ioctl_avail(stream, arg);
break;
case _IOC_NR(SNDRV_COMPRESS_PAUSE): case _IOC_NR(SNDRV_COMPRESS_PAUSE):
retval = snd_compr_pause(stream); return snd_compr_pause(stream);
break;
case _IOC_NR(SNDRV_COMPRESS_RESUME): case _IOC_NR(SNDRV_COMPRESS_RESUME):
retval = snd_compr_resume(stream); return snd_compr_resume(stream);
break;
case _IOC_NR(SNDRV_COMPRESS_START): case _IOC_NR(SNDRV_COMPRESS_START):
retval = snd_compr_start(stream); return snd_compr_start(stream);
break;
case _IOC_NR(SNDRV_COMPRESS_STOP): case _IOC_NR(SNDRV_COMPRESS_STOP):
retval = snd_compr_stop(stream); return snd_compr_stop(stream);
break;
case _IOC_NR(SNDRV_COMPRESS_DRAIN): case _IOC_NR(SNDRV_COMPRESS_DRAIN):
retval = snd_compr_drain(stream); return snd_compr_drain(stream);
break;
case _IOC_NR(SNDRV_COMPRESS_PARTIAL_DRAIN): case _IOC_NR(SNDRV_COMPRESS_PARTIAL_DRAIN):
retval = snd_compr_partial_drain(stream); return snd_compr_partial_drain(stream);
break;
case _IOC_NR(SNDRV_COMPRESS_NEXT_TRACK): case _IOC_NR(SNDRV_COMPRESS_NEXT_TRACK):
retval = snd_compr_next_track(stream); return snd_compr_next_track(stream);
break;
} }
mutex_unlock(&stream->device->lock);
return retval; return -ENOTTY;
} }
/* support of 32bit userspace on 64bit platforms */ /* support of 32bit userspace on 64bit platforms */
......
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