Propagate errors out from compressed streams
Subsystems (4)
v5 cover Propagate errors out from compressed streams
If the DSP suffers an unrecoverable error, the driver likely knows about this, however the framework may not get informed because errors returned from pointer requests are ignored within the framework.
Things work out fine if user-space is doing a read as reads return error status back to user-space so the user can find out that things have gone bad. However, if user-space is doing an avail request there is no path for the error to come back up to user-space. The pointer request returns zero available data, so a read never happens and we basically just end up sitting waiting for data on a stream that we know full well has died.
This patch set attempts to address this and ensure that errors are fully propagated to user-space and we don't ever end up wait for data that will never come.
Changes since v4:
- Fix issue noticed by Vinod whereby the error state was being overwritten when snd_compr_stop was called
It might be easiest to put the ASoC changes through Mark's tree as there are some merge conflicts otherwise, as such I have based this series on Mark's tree.
Thanks,
Charles
Charles Keepax (4):
ALSA: compress: Replace complex if statement with switch
ALSA: compress: Add function to indicate the stream has gone bad
ASoC: wm_adsp: Use new snd_compr_stop_error to signal stream failure
ASoC: compress: Pass error out of soc_compr_pointer
include/sound/compress_driver.h | 5 +++ sound/core/compress_offload.c | 79 ++++++++++++++++++++++++++++++++++++++--- sound/soc/codecs/wm_adsp.c | 11 ++++-- sound/soc/soc-compress.c | 5 +-- 4 files changed, 91 insertions(+), 9 deletions(-)
Signature · 1 line
2.1.4
PATCH v5 1/4 ALSA: compress: Replace complex if statement with switch
A switch statement looks a bit cleaner than an if statement spread over 3 lines, as such update this to a switch.
--- sound/core/compress_offload.c | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-)
sound/core/compress_offload.c +6 -3
@@ -288,9 +288,12 @@ static ssize_t snd_compr_write(struct file *f, const char __user *buf, stream = &data->stream;mutex_lock (&stream->device->lock);/* write is allowed when stream is running or has been steup */ -if (stream->runtime->state !=SNDRV_PCM_STATE_SETUP && - stream->runtime->state !=SNDRV_PCM_STATE_PREPARED && - stream->runtime->state !=SNDRV_PCM_STATE_RUNNING ) { +switch (stream->runtime->state) { +case SNDRV_PCM_STATE_SETUP : +case SNDRV_PCM_STATE_PREPARED : +case SNDRV_PCM_STATE_RUNNING : +break ; +default :mutex_unlock (&stream->device->lock);return -EBADFD ; }
Signature · 1 line
2.1.4
PATCH v5 2/4 ALSA: compress: Add function to indicate the stream has gone bad
In reply to Charles KeepaxCurrently, the avail IOCTL doesn't pass any error status, which means typically on error it simply shows no data available. This can lead to situations where user-space is waiting indefinitely for data that will never come as the DSP has suffered an unrecoverable error.
Add snd_compr_stop_error which end drivers can call to indicate the stream has suffered an unrecoverable error and stop it. The avail and poll IOCTLs are then updated to report if the stream is in an error state to user-space. Allowing the error to propagate out. Processing of the actual snd_compr_stop needs to be deferred to a worker thread as the end driver may detect the errors during an existing operation callback.
--- include/sound/compress_driver.h | 5 +++ sound/core/compress_offload.c | 70 +++++++++++++++++++++++++++++++++++++++-- 2 files changed, 73 insertions(+), 2 deletions(-)
include/sound/compress_driver.h +5 -0
@@ -68,6 +68,7 @@ struct snd_compr_runtime {* @ops: pointer to DSP callbacks * @runtime: pointer to runtime structure * @device: device pointer +* @error_work: delayed work used when closing the stream due to an error * @direction: stream direction, playback/recording * @metadata_set: metadata set flag, true when set * @next_track: has userspace signal next track transition, true when set
@@ -78,6 +79,7 @@ struct snd_compr_stream {struct snd_compr_ops *ops;struct snd_compr_runtime *runtime;struct snd_compr *device; +struct delayed_work error_work;enum snd_compr_direction direction;bool metadata_set;bool next_track;
@@ -187,4 +189,7 @@ static inline void snd_compr_drain_notify(struct snd_compr_stream *stream) wake_up(&stream->runtime->sleep); } +int snd_compr_stop_error (struct snd_compr_stream *stream, +snd_pcm_state_t state); +#endif
sound/core/compress_offload.c +68 -2
125 lines not on this page · raw message
Signature · 1 line
2.1.4
Re: [PATCH v5 2/4] ALSA: compress: Add function to indicate the stream has gone bad
Quoted 6 lines · Charles Keepax
Currently, the avail IOCTL doesn't pass any error status, which means typically on error it simply shows no data available. This can lead to situations where user-space is waiting indefinitely for data that will never come as the DSP has suffered an unrecoverable error.
Quoted 7 lines
Add
snd_compr_stop_errorwhich end drivers can call to indicate the stream has suffered an unrecoverable error and stop it. The avail and poll IOCTLs are then updated to report if the stream is in an error state to user-space. Allowing the error to propagate out. Processing of the actualsnd_compr_stopneeds to be deferred to a worker thread as the end driver may detect the errors during an existing operation callback.
Btw are you patching tinycompress as well to handle EPIPE?
Thanks
Signature · 1 line
~Vinod
Re: [PATCH v5 2/4] ALSA: compress: Add function to indicate the stream has gone bad
Quoted 15 lines · Vinod Koul
Quoted 6 lines · Charles Keepax
Currently, the avail IOCTL doesn't pass any error status, which means typically on error it simply shows no data available. This can lead to situations where user-space is waiting indefinitely for data that will never come as the DSP has suffered an unrecoverable error.
Quoted 7 lines
Add
snd_compr_stop_errorwhich end drivers can call to indicate the stream has suffered an unrecoverable error and stop it. The avail and poll IOCTLs are then updated to report if the stream is in an error state to user-space. Allowing the error to propagate out. Processing of the actualsnd_compr_stopneeds to be deferred to a worker thread as the end driver may detect the errors during an existing operation callback.
Quoted 3 lines
Acked-by Vinod Koul <[email protected]>Btw are you patching tinycompress as well to handle EPIPE?
I hadn't planned on it but I am happy to do so. The current behaviour was sufficient for our needs in that it reports the error and shuts the stream.
Are you thinking of just printing that a over/under run occurred or do you want to try and add some recovery code as well?
Thanks,
Charles
Re: [PATCH v5 2/4] ALSA: compress: Add function to indicate the stream has gone bad
Quoted 18 lines · Charles Keepax
Quoted 17 lines · Vinod Koul
Quoted 6 lines · Charles Keepax
Currently, the avail IOCTL doesn't pass any error status, which means typically on error it simply shows no data available. This can lead to situations where user-space is waiting indefinitely for data that will never come as the DSP has suffered an unrecoverable error.
Quoted 7 lines
Add
snd_compr_stop_errorwhich end drivers can call to indicate the stream has suffered an unrecoverable error and stop it. The avail and poll IOCTLs are then updated to report if the stream is in an error state to user-space. Allowing the error to propagate out. Processing of the actualsnd_compr_stopneeds to be deferred to a worker thread as the end driver may detect the errors during an existing operation callback.Acked-by Vinod Koul <[email protected]>
Quoted 8 lines
Quoted 1 line
Btw are you patching tinycompress as well to handle EPIPE?
I hadn't planned on it but I am happy to do so. The current behaviour was sufficient for our needs in that it reports the error and shuts the stream.
Are you thinking of just printing that a over/under run occurred or do you want to try and add some recovery code as well?
To start with yes adding prints would help. Adding code to handle EPIPE would be good and propagating that to user.
Quoted 1 line
From lib, certainly we want to see this error progated so that upper layers
can do recovery
Recovery is tricky here as we are doing compressed data so essentially new data has to be on frame boundary, so upper layers should decide.
Thanks
Signature · 1 line
~Vinod
Re: [PATCH v5 2/4] ALSA: compress: Add function to indicate the stream has gone bad
In reply to Charles KeepaxQuoted 9 lines
+ *
snd_compr_stop_error: Report a fatal error on a stream
+ * @stream: pointer to stream
+ * @state: state to transition the stream to
+ *
+ * Stop the stream and set its state.
+ *
+ * Should be called with compressed device lock held.
+ */
sound/core/compress_offload.c static int snd_compr_stop(struct snd_compr_stream *stream) · line 762
Quoted 7 lines
sound/core/compress_offload.c +7
-0+int snd_compr_stop_error (struct snd_compr_stream *stream, +snd_pcm_state_t state) +{ +if (stream->runtime->state == state) +return 0 ; + + stream->runtime->state = state;
Minor nit-pick: should there be a consistency check to make sure the new state makes sense - or maybe just a log to help debug? e.g. XRUN should only come if the state in run or draining stages, applying the new state unconditionally could lead to issues. And question for my education since I see no lock/mutex: is the state always consistent or is there a risk of this state being changed while some other thread or interrupt handling modifies it was well?
Re: [PATCH v5 2/4] ALSA: compress: Add function to indicate the stream has gone bad
Quoted 13 lines · Pierre-Louis Bossart
Quoted 13 lines
+ *
snd_compr_stop_error: Report a fatal error on a stream
+ * @stream: pointer to stream
+ * @state: state to transition the stream to
+ *
+ * Stop the stream and set its state.
+ *
+ * Should be called with compressed device lock held.
+ */sound/core/compress_offload.c +4
-0+int snd_compr_stop_error (struct snd_compr_stream *stream, +snd_pcm_state_t state) +{ +if (stream->runtime->state == state)
Quoted 8 lines
sound/core/compress_offload.c static int snd_compr_stop(struct snd_compr_stream *stream) · line 762
Quoted 3 lines
sound/core/compress_offload.c +3
-0+return 0 ; + + stream->runtime->state = state;Minor nit-pick: should there be a consistency check to make sure the new state makes sense - or maybe just a log to help debug? e.g. XRUN should only come if the state in run or draining stages, applying the new state unconditionally could lead to issues.
I think given the function can now report more than just a XRUN it probably makes sense to set it unconditionally. As you might be reporting some error that doesn't require the stream to be running.
It probably would make sense to only call trigger with TRIGGER_STOP if the stream is already running through. How about I add a check for the state in the delayed work? And I can certainly add a print to say the state was set, that probably makes sense anyway as it is an error being reported.
Quoted 3 lines
And question for my education since I see no lock/mutex: is the state always consistent or is there a risk of this state being changed while some other thread or interrupt handling modifies it was well?
As the comment says it is expected the lock should be held when calling the function. I could put a lockdep assert in, if we want to be cautious on this front?
Thanks,
Charles
Re: [PATCH v5 2/4] ALSA: compress: Add function to indicate the stream has gone bad
Quoted 25 lines · Charles Keepax
Quoted 11 lines · Pierre-Louis Bossart
Quoted 11 lines
+ *
snd_compr_stop_error: Report a fatal error on a stream
+ * @stream: pointer to stream
+ * @state: state to transition the stream to
+ *
+ * Stop the stream and set its state.
+ *
+ * Should be called with compressed device lock held.
+ */sound/core/compress_offload.c +3
-0+int snd_compr_stop_error (struct snd_compr_stream *stream, +snd_pcm_state_t state) +{Quoted 8 lines
sound/core/compress_offload.c static int snd_compr_stop(struct snd_compr_stream *stream) · line 762
Quoted 4 lines
sound/core/compress_offload.c +4
-0+if (stream->runtime->state == state) +return 0 ; + + stream->runtime->state = state;Minor nit-pick: should there be a consistency check to make sure the new state makes sense - or maybe just a log to help debug? e.g. XRUN should only come if the state in run or draining stages, applying the new state unconditionally could lead to issues.
I think given the function can now report more than just a XRUN it probably makes sense to set it unconditionally. As you might be reporting some error that doesn't require the stream to be running.
Quoted 5 lines
It probably would make sense to only call trigger with
TRIGGER_STOPif the stream is already running through. How about I add a check for the state in the delayed work? And I can certainly add a print to say the state was set, that probably makes sense anyway as it is an error being reported.
ok, a log would be fine.
Quoted 7 lines
Quoted 4 lines
And question for my education since I see no lock/mutex: is the state always consistent or is there a risk of this state being changed while some other thread or interrupt handling modifies it was well?
As the comment says it is expected the lock should be held when calling the function. I could put a lockdep assert in, if we want to be cautious on this front?
I missed the comment, thanks the clarification.
No objections from me, this sounds good.
PATCH v5 3/4 ASoC: wm_adsp: Use new snd_compr_stop_error to signal stream failure
In reply to Charles KeepaxIf we encounter a fatal error on the compressed stream call the new snd_compr_stop_error to shutdown the stream and allow the core to inform user-space that the stream is no longer valid.
--- sound/soc/codecs/wm_adsp.c | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-)
sound/soc/codecs/wm_adsp.c +9 -2
@@ -2913,6 +2913,7 @@ int wm_adsp_compr_pointer(struct snd_compr_stream *stream, }if (compr->buf->error) { +snd_compr_stop_error (stream,SNDRV_PCM_STATE_XRUN ); ret = -EIO ; goto out; }
@@ -2930,8 +2931,12 @@ int wm_adsp_compr_pointer(struct snd_compr_stream *stream,*/ if (buf->avail <wm_adsp_compr_frag_words (compr)) { ret =wm_adsp_buffer_get_error (buf); -if (ret <0 ) +if (ret <0 ) { +if (compr->buf->error) +snd_compr_stop_error (stream, +SNDRV_PCM_STATE_XRUN ); goto out; + } ret =wm_adsp_buffer_reenable_irq (buf);if (ret <0 ) {
@@ -3029,8 +3034,10 @@ static int wm_adsp_compr_read(struct wm_adsp_compr *compr,if (!compr->buf)return -ENXIO ; -if (compr->buf->error) +if (compr->buf->error) { +snd_compr_stop_error (compr->stream,SNDRV_PCM_STATE_XRUN );return -EIO ; + } count /=WM_ADSP_DATA_WORD_SIZE ;
Signature · 1 line
2.1.4
Re: [PATCH v5 3/4] ASoC: wm_adsp: Use new snd_compr_stop_error to signal stream failure
Quoted 3 lines · Charles Keepax
If we encounter a fatal error on the compressed stream call the new
snd_compr_stop_errorto shutdown the stream and allow the core to inform user-space that the stream is no longer valid.
Signature · 1 line
~Vinod
PATCH v5 4/4 ASoC: compress: Pass error out of soc_compr_pointer
In reply to Charles KeepaxBoth soc_compr_pointer and the platform driver pointer callback return ints but current soc_compr_pointer always returns 0. Update this so we return the actual value from the platform driver callback. This doesn't fix any issues simply makes the code more consistent.
--- sound/soc/soc-compress.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-)
sound/soc/soc-compress.c +3 -2
@@ -530,14 +530,15 @@ static int soc_compr_pointer(struct snd_compr_stream *cstream, {struct snd_soc_pcm_runtime *rtd = cstream->private_data;struct snd_soc_platform *platform = rtd->platform; +int ret =0 ;mutex_lock_nested (&rtd->pcm_mutex, rtd->pcm_subclass);if (platform->driver->compr_ops && platform->driver->compr_ops->pointer) - platform->driver->compr_ops->pointer (cstream, tstamp); + ret = platform->driver->compr_ops->pointer (cstream, tstamp);mutex_unlock (&rtd->pcm_mutex); -return 0 ; +return ret; } static int soc_compr_copy(struct snd_compr_stream *cstream,
Signature · 1 line
2.1.4
Quoted 18 lines · Charles Keepax
If the DSP suffers an unrecoverable error, the driver likely knows about this, however the framework may not get informed because errors returned from pointer requests are ignored within the framework.
Things work out fine if user-space is doing a read as reads return error status back to user-space so the user can find out that things have gone bad. However, if user-space is doing an avail request there is no path for the error to come back up to user-space. The pointer request returns zero available data, so a read never happens and we basically just end up sitting waiting for data on a stream that we know full well has died.
This patch set attempts to address this and ensure that errors are fully propagated to user-space and we don't ever end up wait for data that will never come.
Quoted 7 lines
Changes since v4:
- Fix issue noticed by Vinod whereby the error state was being overwritten whensnd_compr_stopwas calledIt might be easiest to put the ASoC changes through Mark's tree as there are some merge conflicts otherwise, as such I have based this series on Mark's tree.
If it goes through Mark's tree, feel free to take my ack to these patches:
Or if it's easier through my tree, let me know.
thanks,
Takashi