ALSA: compress: fix drain calls blocking other compress functions
Subsystems (2)
PATCH 1/1 ALSA: compress: fix drain calls blocking other compress functions
The drain and drain_notify callback were blocked by low level driver untill the draining was complete. Due to this being invoked with big fat mutex held, others ops like reading timestamp, calling pause, drop were blocked.
So to fix this we add a new snd_compr_drain_notify() API. This would be required to be invoked by low level driver when drain or partial drain has been completed by the DSP. Thus we make the drain and partial_drain callback as non blocking and driver returns immediately after notifying DSP. The waiting is done while relasing the lock so that other ops can go ahead.
--- include/sound/compress_driver.h | 12 ++++++++++ sound/core/compress_offload.c | 43 +++++++++++++++++++++++++++++++++++--- 2 files changed, 51 insertions(+), 4 deletions(-)
include/sound/compress_driver.h +12 -0
@@ -48,6 +48,8 @@ struct snd_compr_ops;* the ring buffer * @total_bytes_transferred: cumulative bytes transferred by offload DSP * @sleep: poll sleep +* @wait: drain wait queuq +* @draining: draining wait condition */ struct snd_compr_runtime {snd_pcm_state_t state;
@@ -59,6 +61,8 @@ struct snd_compr_runtime {u64 total_bytes_available;u64 total_bytes_transferred;wait_queue_head_t sleep; +wait_queue_head_t wait; +unsigned int draining;void *private_data; };
@@ -171,4 +175,12 @@ static inline void snd_compr_fragment_elapsed(struct snd_compr_stream *stream) wake_up(&stream->runtime->sleep); } +static inline void snd_compr_drain_notify (struct snd_compr_stream *stream) +{ +snd_BUG_ON (!stream); + + stream->runtime->draining =1 ; +wake_up (&stream->runtime->wait); +} +#endif
sound/core/compress_offload.c +39 -4
79 lines not on this page · raw message
Signature · 1 line
1.7.0.4
PATCH 1/1 ALSA: compress: fix drain calls blocking other compress functions
The drain and drain_notify callback were blocked by low level driver untill the draining was complete. Due to this being invoked with big fat mutex held, others ops like reading timestamp, calling pause, drop were blocked.
So to fix this we add a new snd_compr_drain_notify() API. This would be required to be invoked by low level driver when drain or partial drain has been completed by the DSP. Thus we make the drain and partial_drain callback as non blocking and driver returns immediately after notifying DSP.
The waiting is done while relasing the lock so that other ops can go ahead.
---
v2:
fix the 80 line warn
move the state change to compress_drain()
include/sound/compress_driver.h | 12 ++++++++++ sound/core/compress_offload.c | 43 ++++++++++++++++++++++++++++++++++++-- 2 files changed, 52 insertions(+), 3 deletions(-)
include/sound/compress_driver.h +12 -0
@@ -48,6 +48,8 @@ struct snd_compr_ops;* the ring buffer * @total_bytes_transferred: cumulative bytes transferred by offload DSP * @sleep: poll sleep +* @wait: drain wait queuq +* @draining: draining wait condition */ struct snd_compr_runtime {snd_pcm_state_t state;
@@ -59,6 +61,8 @@ struct snd_compr_runtime {u64 total_bytes_available;u64 total_bytes_transferred;wait_queue_head_t sleep; +wait_queue_head_t wait; +unsigned int draining;void *private_data; };
@@ -171,4 +175,12 @@ static inline void snd_compr_fragment_elapsed(struct snd_compr_stream *stream) wake_up(&stream->runtime->sleep); } +static inline void snd_compr_drain_notify (struct snd_compr_stream *stream) +{ +snd_BUG_ON (!stream); + + stream->runtime->draining =1 ; +wake_up (&stream->runtime->wait); +} +#endif
sound/core/compress_offload.c +40 -3
79 lines not on this page · raw message
Signature · 1 line
1.7.0.4
At Thu, 24 Oct 2013 13:05:41 +0530,
Quoted 27 lines · Vinod Koul
The drain and
drain_notifycallback were blocked by low level driver untill the draining was complete. Due to this being invoked with big fat mutex held, others ops like reading timestamp, calling pause, drop were blocked.So to fix this we add a new
snd_compr_drain_notify()API. This would be required to be invoked by low level driver when drain or partial drain has been completed by the DSP. Thus we make the drain andpartial_draincallback as non blocking and driver returns immediately after notifying DSP.
The waiting is done while relasing the lock so that other ops can go ahead.Signed-off-by Vinod Koul <[email protected]>---
v2:
fix the 80 line warn
move the state change tocompress_drain()include/sound/compress_driver.h | 12 ++++++++++ sound/core/compress_offload.c | 43 ++++++++++++++++++++++++++++++++++++-- 2 files changed, 52 insertions(+), 3 deletions(-)include/sound/compress_driver.h +0
-0
include/sound/compress_driver.h struct snd_compr_ops; · line 52
Quoted 5 lines
include/sound/compress_driver.h +2
-0@@ -48,6 +48,8 @@ struct snd_compr_ops;* the ring buffer * @total_bytes_transferred: cumulative bytes transferred by offload DSP * @sleep: poll sleep +* @wait: drain wait queuq +* @draining: draining wait condition
What does this mean exactly? I thought draining = 1 meaning that the stream is draining (i.e. waiting for the finish notification), but it looks opposite in your code below...
Quoted 38 lines
include/sound/compress_driver.h +10
-0*/ struct snd_compr_runtime {snd_pcm_state_t state;@@ -59,6 +61,8 @@ struct snd_compr_runtime {u64 total_bytes_available;u64 total_bytes_transferred;wait_queue_head_t sleep; +wait_queue_head_t wait; +unsigned int draining;void *private_data; };@@ -171,4 +175,12 @@ static inline void snd_compr_fragment_elapsed(struct snd_compr_stream *stream) wake_up(&stream->runtime->sleep); } +static inline void snd_compr_drain_notify (struct snd_compr_stream *stream) +{ +snd_BUG_ON (!stream); + + stream->runtime->draining =1 ; +wake_up (&stream->runtime->wait); +} +#endif sound/core/compress_offload.c +1
-0@@ -123,6 +123,7 @@ static int snd_compr_open(struct inode *inode, struct file *f) } runtime->state =SNDRV_PCM_STATE_OPEN ;init_waitqueue_head (&runtime->sleep); +init_waitqueue_head (&runtime->wait); data->stream.runtime = runtime; f->private_data = (void *)data;mutex_lock (&compr->lock);
sound/core/compress_offload.c static int snd_compr_stop(struct snd_compr_stream *stream) · line 687
Quoted 5 lines
sound/core/compress_offload.c +2
-0@@ -682,12 +683,36 @@ static int snd_compr_stop(struct snd_compr_stream *stream)if (!retval) { stream->runtime->state =SNDRV_PCM_STATE_SETUP ;wake_up (&stream->runtime->sleep); + stream->runtime->draining =1 ; +wake_up (&stream->runtime->wait);
Not using snd_compr_drain_notify()?
Quoted 11 lines
sound/core/compress_offload.c +5
-0stream->runtime->total_bytes_available =0 ; stream->runtime->total_bytes_transferred =0 ; }return retval; } +static int snd_compress_wait_for_drain (struct snd_compr_stream *stream) +{ +int retval =0 ; + +/*
sound/core/compress_offload.c static int snd_compr_stop(struct snd_compr_stream *stream) · line 708
Quoted 10 lines
sound/core/compress_offload.c +10
-0+* we are called with lock held. So drop the lock while we wait for +* drain complete notfication from the driver +*/ + stream->runtime->state =SNDRV_PCM_STATE_DRAINING ; +mutex_unlock (&stream->device->lock); + + retval =wait_event_interruptible (stream->runtime->wait, + stream->runtime->draining); +if (retval) +pr_err ("Wait for drain failed %d\n" , retval);
If you make it interruptible, it's no actual kernel error when user interrupts the operation, so no reason to put the kernel error message there. At least, better to check whether it's a user interruption or a system error.
Quoted 17 lines
sound/core/compress_offload.c +9
-0+ +wake_up (&stream->runtime->sleep); +mutex_lock (&stream->device->lock); + +return retval; +} +static int snd_compr_drain (struct snd_compr_stream *stream) {int retval;@@ -695,11 +720,17 @@ static int snd_compr_drain(struct snd_compr_stream *stream)if (stream->runtime->state ==SNDRV_PCM_STATE_PREPARED || stream->runtime->state ==SNDRV_PCM_STATE_SETUP )return -EPERM ; + + stream->runtime->draining =0 ; retval = stream->ops->trigger (stream,SND_COMPR_TRIGGER_DRAIN );
sound/core/compress_offload.c static int snd_compr_drain(struct snd_compr_stream *stream) · line 733
Quoted 10 lines
sound/core/compress_offload.c +6
-2-if (!retval) { - stream->runtime->state =SNDRV_PCM_STATE_DRAINING ; +if (retval) { +pr_err ("SND_COMPR_TRIGGER_DRAIN failed %d\n" , retval);wake_up (&stream->runtime->sleep); +return retval; } + + retval =snd_compress_wait_for_drain (stream); + stream->runtime->state =SNDRV_PCM_STATE_SETUP ;
So, the runtime state goes to SETUP even if wait_for_drain() returns an error? In other words, compress core expects that the driver takes care of the proper state change in the error handling?
thanks,
Takashi
Quoted 24 lines
sound/core/compress_offload.c +7
-1return retval; }@@ -735,10 +766,16 @@ static int snd_compr_partial_drain(struct snd_compr_stream *stream)if (stream->next_track == false)return -EPERM ; + stream->runtime->draining =0 ; retval = stream->ops->trigger (stream,SND_COMPR_TRIGGER_PARTIAL_DRAIN ); +if (retval) { +pr_err ("Partial drain returned failure\n" ); +wake_up (&stream->runtime->sleep); +return retval; + } stream->next_track = false; -return retval; +return snd_compress_wait_for_drain (stream); }static long snd_compr_ioctl (struct file *f,unsigned int cmd,unsigned long arg)Signature · 1 line
1.7.0.4
Quoted 24 lines · Takashi Iwai
At Thu, 24 Oct 2013 13:05:41 +0530,
Quoted 22 lines · Vinod Koul
The drain and
drain_notifycallback were blocked by low level driver untill the draining was complete. Due to this being invoked with big fat mutex held, others ops like reading timestamp, calling pause, drop were blocked.So to fix this we add a new
snd_compr_drain_notify()API. This would be required to be invoked by low level driver when drain or partial drain has been completed by the DSP. Thus we make the drain andpartial_draincallback as non blocking and driver returns immediately after notifying DSP.
The waiting is done while relasing the lock so that other ops can go ahead.Signed-off-by Vinod Koul <[email protected]>---
v2:
fix the 80 line warn
move the state change tocompress_drain()include/sound/compress_driver.h | 12 ++++++++++ sound/core/compress_offload.c | 43 ++++++++++++++++++++++++++++++++++++-- 2 files changed, 52 insertions(+), 3 deletions(-)
Quoted 14 lines
include/sound/compress_driver.h struct snd_compr_ops; · line 52
Quoted 10 lines
include/sound/compress_driver.h +2
-0@@ -48,6 +48,8 @@ struct snd_compr_ops;* the ring buffer * @total_bytes_transferred: cumulative bytes transferred by offload DSP * @sleep: poll sleep +* @wait: drain wait queuq +* @draining: draining wait condition What does this mean exactly? I thought draining = 1 meaning that the stream is draining (i.e. waiting for the finish notification), but it looks opposite in your code below...
What i meant was the draining wait conditions which would be used for waiting. Before wait the caller will set to 0 and on completion the callback will set to 1 and wake up.
Quoted 37 lines
Quoted 37 lines
include/sound/compress_driver.h +10
-0*/ struct snd_compr_runtime {snd_pcm_state_t state;@@ -59,6 +61,8 @@ struct snd_compr_runtime {u64 total_bytes_available;u64 total_bytes_transferred;wait_queue_head_t sleep; +wait_queue_head_t wait; +unsigned int draining;void *private_data; };@@ -171,4 +175,12 @@ static inline void snd_compr_fragment_elapsed(struct snd_compr_stream *stream) wake_up(&stream->runtime->sleep); } +static inline void snd_compr_drain_notify (struct snd_compr_stream *stream) +{ +snd_BUG_ON (!stream); + + stream->runtime->draining =1 ; +wake_up (&stream->runtime->wait); +} +#endif sound/core/compress_offload.c +1
-0@@ -123,6 +123,7 @@ static int snd_compr_open(struct inode *inode, struct file *f) } runtime->state =SNDRV_PCM_STATE_OPEN ;init_waitqueue_head (&runtime->sleep); +init_waitqueue_head (&runtime->wait); data->stream.runtime = runtime; f->private_data = (void *)data;mutex_lock (&compr->lock);
Quoted 8 lines
sound/core/compress_offload.c static int snd_compr_stop(struct snd_compr_stream *stream) · line 687
Quoted 6 lines
sound/core/compress_offload.c +2
-0@@ -682,12 +683,36 @@ static int snd_compr_stop(struct snd_compr_stream *stream)if (!retval) { stream->runtime->state =SNDRV_PCM_STATE_SETUP ;wake_up (&stream->runtime->sleep); + stream->runtime->draining =1 ; +wake_up (&stream->runtime->wait);Not using
snd_compr_drain_notify()?
Yes that should be added, thanks for pointing!
Quoted 18 lines
Quoted 18 lines
sound/core/compress_offload.c +12
-0stream->runtime->total_bytes_available =0 ; stream->runtime->total_bytes_transferred =0 ; }return retval; } +static int snd_compress_wait_for_drain (struct snd_compr_stream *stream) +{ +int retval =0 ; + +/* +* we are called with lock held. So drop the lock while we wait for +* drain complete notfication from the driver +*/ + stream->runtime->state =SNDRV_PCM_STATE_DRAINING ; +mutex_unlock (&stream->device->lock); + + retval = wait_event_interruptible(stream->runtime->wait,
Quoted 8 lines
sound/core/compress_offload.c static int snd_compr_stop(struct snd_compr_stream *stream) · line 708
Quoted 3 lines
sound/core/compress_offload.c +3
-0+ stream->runtime->draining); +if (retval) +pr_err ("Wait for drain failed %d\n" , retval);If you make it interruptible, it's no actual kernel error when user interrupts the operation, so no reason to put the kernel error message there. At least, better to check whether it's a user interruption or a system error.
Agreed. I think I dont even need to make it interruptible. If user is terminating the stop will be invoked and then with above fix it should work. We can do away with interrutiple part :)
Quoted 23 lines
Quoted 23 lines
sound/core/compress_offload.c +12
-2+ +wake_up (&stream->runtime->sleep); +mutex_lock (&stream->device->lock); + +return retval; +} +static int snd_compr_drain (struct snd_compr_stream *stream) {int retval;@@ -695,11 +720,17 @@ static int snd_compr_drain(struct snd_compr_stream *stream)if (stream->runtime->state ==SNDRV_PCM_STATE_PREPARED || stream->runtime->state ==SNDRV_PCM_STATE_SETUP )return -EPERM ; + + stream->runtime->draining =0 ; retval = stream->ops->trigger (stream,SND_COMPR_TRIGGER_DRAIN ); -if (!retval) { - stream->runtime->state =SNDRV_PCM_STATE_DRAINING ; +if (retval) { +pr_err ("SND_COMPR_TRIGGER_DRAIN failed %d\n" , retval);wake_up (&stream->runtime->sleep); +return retval;
Quoted 8 lines
sound/core/compress_offload.c static int snd_compr_drain(struct snd_compr_stream *stream) · line 733
Quoted 4 lines
sound/core/compress_offload.c +3
-0} + + retval =snd_compress_wait_for_drain (stream); + stream->runtime->state =SNDRV_PCM_STATE_SETUP ;So, the runtime state goes to SETUP even if
wait_for_drain()returns an error? In other words, compress core expects that the driver takes care of the proper state change in the error handling?
I guess it would make sense to communicate error. I havent added notion of status on drain complete. By adding another status flag in the callback we cna let driver tell us if error occured. Then pass the error in draining value and do the state change accordingly.
--
~Vinod
At Thu, 24 Oct 2013 14:57:24 +0530,
Quoted 40 lines · Vinod Koul
Quoted 24 lines · Takashi Iwai
At Thu, 24 Oct 2013 13:05:41 +0530,
Quoted 22 lines · Vinod Koul
The drain and
drain_notifycallback were blocked by low level driver untill the draining was complete. Due to this being invoked with big fat mutex held, others ops like reading timestamp, calling pause, drop were blocked.So to fix this we add a new
snd_compr_drain_notify()API. This would be required to be invoked by low level driver when drain or partial drain has been completed by the DSP. Thus we make the drain andpartial_draincallback as non blocking and driver returns immediately after notifying DSP.
The waiting is done while relasing the lock so that other ops can go ahead.Signed-off-by Vinod Koul <[email protected]>---
v2:
fix the 80 line warn
move the state change tocompress_drain()include/sound/compress_driver.h | 12 ++++++++++ sound/core/compress_offload.c | 43 ++++++++++++++++++++++++++++++++++++-- 2 files changed, 52 insertions(+), 3 deletions(-)Quoted 14 lines
include/sound/compress_driver.h struct snd_compr_ops; · line 52
Quoted 10 lines
include/sound/compress_driver.h +2
-0@@ -48,6 +48,8 @@ struct snd_compr_ops;* the ring buffer * @total_bytes_transferred: cumulative bytes transferred by offload DSP * @sleep: poll sleep +* @wait: drain wait queuq +* @draining: draining wait condition What does this mean exactly? I thought draining = 1 meaning that the stream is draining (i.e. waiting for the finish notification), but it looks opposite in your code below...
Quoted 3 lines
What i meant was the draining wait conditions which would be used for waiting. Before wait the caller will set to 0 and on completion the callback will set to 1 and wake up.
Well, it's not so intuitive as the flag being named "draining". Please give a more detailed comment (or name it a bit less-confusing one :)
Quoted 108 lines
Quoted 37 lines
Quoted 37 lines
include/sound/compress_driver.h +10
-0*/ struct snd_compr_runtime {snd_pcm_state_t state;@@ -59,6 +61,8 @@ struct snd_compr_runtime {u64 total_bytes_available;u64 total_bytes_transferred;wait_queue_head_t sleep; +wait_queue_head_t wait; +unsigned int draining;void *private_data; };@@ -171,4 +175,12 @@ static inline void snd_compr_fragment_elapsed(struct snd_compr_stream *stream) wake_up(&stream->runtime->sleep); } +static inline void snd_compr_drain_notify (struct snd_compr_stream *stream) +{ +snd_BUG_ON (!stream); + + stream->runtime->draining =1 ; +wake_up (&stream->runtime->wait); +} +#endif sound/core/compress_offload.c +1
-0@@ -123,6 +123,7 @@ static int snd_compr_open(struct inode *inode, struct file *f) } runtime->state =SNDRV_PCM_STATE_OPEN ;init_waitqueue_head (&runtime->sleep); +init_waitqueue_head (&runtime->wait); data->stream.runtime = runtime; f->private_data = (void *)data;mutex_lock (&compr->lock);Quoted 8 lines
sound/core/compress_offload.c static int snd_compr_stop(struct snd_compr_stream *stream) · line 687
Quoted 6 lines
sound/core/compress_offload.c +2
-0@@ -682,12 +683,36 @@ static int snd_compr_stop(struct snd_compr_stream *stream)if (!retval) { stream->runtime->state =SNDRV_PCM_STATE_SETUP ;wake_up (&stream->runtime->sleep); + stream->runtime->draining =1 ; +wake_up (&stream->runtime->wait);Not using
snd_compr_drain_notify()?Yes that should be added, thanks for pointing!
Quoted 18 lines
Quoted 18 lines
sound/core/compress_offload.c +12
-0stream->runtime->total_bytes_available =0 ; stream->runtime->total_bytes_transferred =0 ; }return retval; } +static int snd_compress_wait_for_drain (struct snd_compr_stream *stream) +{ +int retval =0 ; + +/* +* we are called with lock held. So drop the lock while we wait for +* drain complete notfication from the driver +*/ + stream->runtime->state =SNDRV_PCM_STATE_DRAINING ; +mutex_unlock (&stream->device->lock); + + retval = wait_event_interruptible(stream->runtime->wait,Quoted 8 lines
sound/core/compress_offload.c static int snd_compr_stop(struct snd_compr_stream *stream) · line 708
Quoted 3 lines
sound/core/compress_offload.c +3
-0+ stream->runtime->draining); +if (retval) +pr_err ("Wait for drain failed %d\n" , retval);If you make it interruptible, it's no actual kernel error when user interrupts the operation, so no reason to put the kernel error message there. At least, better to check whether it's a user interruption or a system error.
Agreed. I think I dont even need to make it interruptible. If user is terminating the stop will be invoked and then with above fix it should work. We can do away with interrutiple part :)
Quoted 23 lines
Quoted 23 lines
sound/core/compress_offload.c +12
-2+ +wake_up (&stream->runtime->sleep); +mutex_lock (&stream->device->lock); + +return retval; +} +static int snd_compr_drain (struct snd_compr_stream *stream) {int retval;@@ -695,11 +720,17 @@ static int snd_compr_drain(struct snd_compr_stream *stream)if (stream->runtime->state ==SNDRV_PCM_STATE_PREPARED || stream->runtime->state ==SNDRV_PCM_STATE_SETUP )return -EPERM ; + + stream->runtime->draining =0 ; retval = stream->ops->trigger (stream,SND_COMPR_TRIGGER_DRAIN ); -if (!retval) { - stream->runtime->state =SNDRV_PCM_STATE_DRAINING ; +if (retval) { +pr_err ("SND_COMPR_TRIGGER_DRAIN failed %d\n" , retval);wake_up (&stream->runtime->sleep); +return retval;Quoted 8 lines
sound/core/compress_offload.c static int snd_compr_drain(struct snd_compr_stream *stream) · line 733
Quoted 4 lines
sound/core/compress_offload.c +3
-0} + + retval =snd_compress_wait_for_drain (stream); + stream->runtime->state =SNDRV_PCM_STATE_SETUP ;So, the runtime state goes to SETUP even if
wait_for_drain()returns an error? In other words, compress core expects that the driver takes care of the proper state change in the error handling?
Quoted 4 lines
I guess it would make sense to communicate error. I havent added notion of status on drain complete. By adding another status flag in the callback we cna let driver tell us if error occured. Then pass the error in draining value and do the state change accordingly.
I think it's fine as in the current patch, I just wanted to make sure that this runtime state change is the designed behavior. If so, it'd be better to document / comment clearly.
thanks,
Takashi
Quoted 42 lines · Takashi Iwai
At Thu, 24 Oct 2013 14:57:24 +0530,
Quoted 40 lines · Vinod Koul
Quoted 38 lines · Takashi Iwai
At Thu, 24 Oct 2013 13:05:41 +0530,
Quoted 27 lines · Vinod Koul
The drain and
drain_notifycallback were blocked by low level driver untill the draining was complete. Due to this being invoked with big fat mutex held, others ops like reading timestamp, calling pause, drop were blocked.So to fix this we add a new
snd_compr_drain_notify()API. This would be required to be invoked by low level driver when drain or partial drain has been completed by the DSP. Thus we make the drain andpartial_draincallback as non blocking and driver returns immediately after notifying DSP.
The waiting is done while relasing the lock so that other ops can go ahead.Signed-off-by Vinod Koul <[email protected]>---
v2:
fix the 80 line warn
move the state change tocompress_drain()include/sound/compress_driver.h | 12 ++++++++++ sound/core/compress_offload.c | 43 ++++++++++++++++++++++++++++++++++++-- 2 files changed, 52 insertions(+), 3 deletions(-)include/sound/compress_driver.h +0
-0include/sound/compress_driver.h struct snd_compr_ops; · line 52
Quoted 5 lines
include/sound/compress_driver.h +2
-0@@ -48,6 +48,8 @@ struct snd_compr_ops;* the ring buffer * @total_bytes_transferred: cumulative bytes transferred by offload DSP * @sleep: poll sleep +* @wait: drain wait queuq +* @draining: draining wait condition What does this mean exactly? I thought draining = 1 meaning that the stream is draining (i.e. waiting for the finish notification), but it looks opposite in your code below...
Quoted 7 lines
Quoted 3 lines
What i meant was the draining wait conditions which would be used for waiting. Before wait the caller will set to 0 and on completion the callback will set to 1 and wake up.
Well, it's not so intuitive as the flag being named "draining". Please give a more detailed comment (or name it a bit less-confusing one :)
hmmm, yes it needs a better name :)
perhaps drain_condition or drain_wake would be apt!
Quoted 31 lines
Quoted 31 lines
Quoted 31 lines
Quoted 17 lines
include/sound/compress_driver.h +9
-0+ +wake_up (&stream->runtime->sleep); +mutex_lock (&stream->device->lock); + +return retval; +} +static int snd_compr_drain (struct snd_compr_stream *stream) {int retval;@@ -695,11 +720,17 @@ static int snd_compr_drain(struct snd_compr_stream *stream)if (stream->runtime->state ==SNDRV_PCM_STATE_PREPARED || stream->runtime->state ==SNDRV_PCM_STATE_SETUP )return -EPERM ; + + stream->runtime->draining =0 ; retval = stream->ops->trigger (stream,SND_COMPR_TRIGGER_DRAIN );include/sound/compress_driver.h static int snd_compr_drain(struct snd_compr_stream *stream) · line 733
Quoted 10 lines
include/sound/compress_driver.h +6
-2-if (!retval) { - stream->runtime->state =SNDRV_PCM_STATE_DRAINING ; +if (retval) { +pr_err ("SND_COMPR_TRIGGER_DRAIN failed %d\n" , retval);wake_up (&stream->runtime->sleep); +return retval; } + + retval =snd_compress_wait_for_drain (stream); + stream->runtime->state =SNDRV_PCM_STATE_SETUP ;So, the runtime state goes to SETUP even if
wait_for_drain()returns an error? In other words, compress core expects that the driver takes care of the proper state change in the error handling?
Quoted 8 lines
Quoted 4 lines
I guess it would make sense to communicate error. I havent added notion of status on drain complete. By adding another status flag in the callback we cna let driver tell us if error occured. Then pass the error in draining value and do the state change accordingly.
I think it's fine as in the current patch, I just wanted to make sure that this runtime state change is the designed behavior. If so, it'd be better to document / comment clearly.
Sure, lets add that explictly!
Signature · 1 line
~Vinod
At Thu, 24 Oct 2013 16:05:05 +0530,
Quoted 48 lines · Vinod Koul
Quoted 46 lines · Takashi Iwai
At Thu, 24 Oct 2013 14:57:24 +0530,
Quoted 43 lines · Vinod Koul
Quoted 24 lines · Takashi Iwai
At Thu, 24 Oct 2013 13:05:41 +0530,
Quoted 22 lines · Vinod Koul
The drain and
drain_notifycallback were blocked by low level driver untill the draining was complete. Due to this being invoked with big fat mutex held, others ops like reading timestamp, calling pause, drop were blocked.So to fix this we add a new
snd_compr_drain_notify()API. This would be required to be invoked by low level driver when drain or partial drain has been completed by the DSP. Thus we make the drain andpartial_draincallback as non blocking and driver returns immediately after notifying DSP.
The waiting is done while relasing the lock so that other ops can go ahead.Signed-off-by Vinod Koul <[email protected]>---
v2:
fix the 80 line warn
move the state change tocompress_drain()include/sound/compress_driver.h | 12 ++++++++++ sound/core/compress_offload.c | 43 ++++++++++++++++++++++++++++++++++++-- 2 files changed, 52 insertions(+), 3 deletions(-)Quoted 14 lines
include/sound/compress_driver.h struct snd_compr_ops; · line 52
Quoted 10 lines
include/sound/compress_driver.h +2
-0@@ -48,6 +48,8 @@ struct snd_compr_ops;* the ring buffer * @total_bytes_transferred: cumulative bytes transferred by offload DSP * @sleep: poll sleep +* @wait: drain wait queuq +* @draining: draining wait condition What does this mean exactly? I thought draining = 1 meaning that the stream is draining (i.e. waiting for the finish notification), but it looks opposite in your code below...
What i meant was the draining wait conditions which would be used for waiting. Before wait the caller will set to 0 and on completion the callback will set to 1 and wake up.
Quoted 5 lines
Quoted 3 lines
Well, it's not so intuitive as the flag being named "draining". Please give a more detailed comment (or name it a bit less-confusing one :)
hmmm, yes it needs a better name :)
perhapsdrain_conditionordrain_wakewould be apt!
Yes, drain_wake would be more self-explanatory.
Quoted 36 lines
Quoted 36 lines
Quoted 35 lines
Quoted 23 lines
Quoted 23 lines
include/sound/compress_driver.h +12
-2+ +wake_up (&stream->runtime->sleep); +mutex_lock (&stream->device->lock); + +return retval; +} +static int snd_compr_drain (struct snd_compr_stream *stream) {int retval;@@ -695,11 +720,17 @@ static int snd_compr_drain(struct snd_compr_stream *stream)if (stream->runtime->state ==SNDRV_PCM_STATE_PREPARED || stream->runtime->state ==SNDRV_PCM_STATE_SETUP )return -EPERM ; + + stream->runtime->draining =0 ; retval = stream->ops->trigger (stream,SND_COMPR_TRIGGER_DRAIN ); -if (!retval) { - stream->runtime->state =SNDRV_PCM_STATE_DRAINING ; +if (retval) { +pr_err ("SND_COMPR_TRIGGER_DRAIN failed %d\n" , retval);wake_up (&stream->runtime->sleep); +return retval;Quoted 8 lines
include/sound/compress_driver.h static int snd_compr_drain(struct snd_compr_stream *stream) · line 733
Quoted 4 lines
include/sound/compress_driver.h +3
-0} + + retval =snd_compress_wait_for_drain (stream); + stream->runtime->state =SNDRV_PCM_STATE_SETUP ;So, the runtime state goes to SETUP even if
wait_for_drain()returns an error? In other words, compress core expects that the driver takes care of the proper state change in the error handling?I guess it would make sense to communicate error. I havent added notion of status on drain complete. By adding another status flag in the callback we cna let driver tell us if error occured. Then pass the error in draining value and do the state change accordingly.
Quoted 4 lines
Quoted 3 lines
I think it's fine as in the current patch, I just wanted to make sure that this runtime state change is the designed behavior. If so, it'd be better to document / comment clearly.
Sure, lets add that explictly!
OK, thanks.
Takashi
Quoted 4 lines · Takashi Iwai
Quoted 2 lines
hmmm, yes it needs a better name :)
perhapsdrain_conditionordrain_wakewould be apt!Yes,
drain_wakewould be more self-explanatory.
Ok sent v3 with this a few moment ago :)
--
~Vinod
PATCH v3 1/1 ALSA: compress: fix drain calls blocking other compress functions
In reply to Vinod KoulThe drain and drain_notify callback were blocked by low level driver untill the draining was complete. Due to this being invoked with big fat mutex held, others ops like reading timestamp, calling pause, drop were blocked.
So to fix this we add a new snd_compr_drain_notify() API. This would be required to be invoked by low level driver when drain or partial drain has been completed by the DSP. Thus we make the drain and partial_drain callback as non blocking and driver returns immediately after notifying DSP.
The waiting is done while relasing the lock so that other ops can go ahead.
---
v3:
call snd_compr_drain_notify from compress_stop()
rename draining -> drain_wake
add some comments on state transistion after drain
v2:
fix the 80 line warn
move the state change to compress_drain()
include/sound/compress_driver.h | 12 +++++++++++ sound/core/compress_offload.c | 41 ++++++++++++++++++++++++++++++++++++-- 2 files changed, 50 insertions(+), 3 deletions(-)
include/sound/compress_driver.h +12 -0
@@ -48,6 +48,8 @@ struct snd_compr_ops;* the ring buffer * @total_bytes_transferred: cumulative bytes transferred by offload DSP * @sleep: poll sleep +* @wait: drain wait queue +* @drain_wake: condition for drain wake */ struct snd_compr_runtime {snd_pcm_state_t state;
@@ -59,6 +61,8 @@ struct snd_compr_runtime {u64 total_bytes_available;u64 total_bytes_transferred;wait_queue_head_t sleep; +wait_queue_head_t wait; +unsigned int drain_wake;void *private_data; };
@@ -171,4 +175,12 @@ static inline void snd_compr_fragment_elapsed(struct snd_compr_stream *stream) wake_up(&stream->runtime->sleep); } +static inline void snd_compr_drain_notify (struct snd_compr_stream *stream) +{ +snd_BUG_ON (!stream); + + stream->runtime->drain_wake =1 ; +wake_up (&stream->runtime->wait); +} +#endif
sound/core/compress_offload.c +38 -3
77 lines not on this page · raw message
Signature · 1 line
1.7.0.4
At Thu, 24 Oct 2013 16:37:31 +0530,
Quoted 51 lines · Vinod Koul
The drain and
drain_notifycallback were blocked by low level driver untill the draining was complete. Due to this being invoked with big fat mutex held, others ops like reading timestamp, calling pause, drop were blocked.So to fix this we add a new
snd_compr_drain_notify()API. This would be required to be invoked by low level driver when drain or partial drain has been completed by the DSP. Thus we make the drain andpartial_draincallback as non blocking and driver returns immediately after notifying DSP.
The waiting is done while relasing the lock so that other ops can go ahead.Signed-off-by Vinod Koul <[email protected]>---
v3:
callsnd_compr_drain_notifyfromcompress_stop()rename draining -> drain_wakeadd some comments on state transistion after drain
v2:
fix the 80 line warn
move the state change tocompress_drain()include/sound/compress_driver.h | 12 +++++++++++ sound/core/compress_offload.c | 41 ++++++++++++++++++++++++++++++++++++-- 2 files changed, 50 insertions(+), 3 deletions(-)include/sound/compress_driver.h +4
-0@@ -48,6 +48,8 @@ struct snd_compr_ops;* the ring buffer * @total_bytes_transferred: cumulative bytes transferred by offload DSP * @sleep: poll sleep +* @wait: drain wait queue +* @drain_wake: condition for drain wake */ struct snd_compr_runtime {snd_pcm_state_t state;@@ -59,6 +61,8 @@ struct snd_compr_runtime {u64 total_bytes_available;u64 total_bytes_transferred;wait_queue_head_t sleep; +wait_queue_head_t wait; +unsigned int drain_wake;void *private_data; };
include/sound/compress_driver.h static inline void snd_compr_fragment_elapsed(struct snd_compr_stream *stream) · line 180
Quoted 6 lines
include/sound/compress_driver.h +3
-0@@ -171,4 +175,12 @@ static inline void snd_compr_fragment_elapsed(struct snd_compr_stream *stream)wake_up (&stream->runtime->sleep); } +static inline void snd_compr_drain_notify (struct snd_compr_stream *stream) +{ +snd_BUG_ON (!stream);
Should return, otherwise you'll get Oops:
if (snd_BUG_ON(!stream)) return;
Quoted 33 lines
include/sound/compress_driver.h +5
-0+ + stream->runtime->drain_wake =1 ; + wake_up(&stream->runtime->wait); +} +#endif sound/core/compress_offload.c +6
-0@@ -123,6 +123,7 @@ static int snd_compr_open(struct inode *inode, struct file *f) } runtime->state =SNDRV_PCM_STATE_OPEN ;init_waitqueue_head (&runtime->sleep); +init_waitqueue_head (&runtime->wait); data->stream.runtime = runtime; f->private_data = (void *)data;mutex_lock (&compr->lock);@@ -682,12 +683,34 @@ static int snd_compr_stop(struct snd_compr_stream *stream)if (!retval) { stream->runtime->state =SNDRV_PCM_STATE_SETUP ;wake_up (&stream->runtime->sleep); +snd_compr_drain_notify (stream); stream->runtime->total_bytes_available =0 ; stream->runtime->total_bytes_transferred =0 ; }return retval; } +static int snd_compress_wait_for_drain (struct snd_compr_stream *stream) +{ +/* +* We are called with lock held. So drop the lock while we wait for
sound/core/compress_offload.c static int snd_compr_stop(struct snd_compr_stream *stream) · line 706
Quoted 10 lines
sound/core/compress_offload.c +10
-0+* drain complete notfication from the driver +* +* It is expected that driver will notify the drain completion and then +* stream will be moved to SETUP state, even if draining resulted in an +* error. We can trigger next track after this. +*/ + stream->runtime->state =SNDRV_PCM_STATE_DRAINING ; +mutex_unlock (&stream->device->lock); + +wait_event (stream->runtime->wait, stream->runtime->drain_wake);
Don't you really want the interruption?
Or use wait_event_interruptible() and return error appropriately.
Quoted 11 lines
sound/core/compress_offload.c +7
-0+ +wake_up (&stream->runtime->sleep); +mutex_lock (&stream->device->lock); + +return 0 ; +} +static int snd_compr_drain (struct snd_compr_stream *stream) {int retval;
sound/core/compress_offload.c static int snd_compr_drain(struct snd_compr_stream *stream) · line 725
Quoted 10 lines
sound/core/compress_offload.c +4
-2@@ -695,11 +718,17 @@ static int snd_compr_drain(struct snd_compr_stream *stream)if (stream->runtime->state ==SNDRV_PCM_STATE_PREPARED || stream->runtime->state ==SNDRV_PCM_STATE_SETUP )return -EPERM ; + + stream->runtime->drain_wake =0 ; retval = stream->ops->trigger (stream,SND_COMPR_TRIGGER_DRAIN ); -if (!retval) { - stream->runtime->state =SNDRV_PCM_STATE_DRAINING ; +if (retval) { +pr_err ("SND_COMPR_TRIGGER_DRAIN failed %d\n" , retval);
I don't think spamming such an error is good. The trigger error can happen often repeatedly once when it happens. Such a message is at most a debug message, so use pr_debug(), if you still want to keep it in the core code.
Quoted 10 lines
sound/core/compress_offload.c +4
-0wake_up (&stream->runtime->sleep); +return retval; } + + retval =snd_compress_wait_for_drain (stream); + stream->runtime->state =SNDRV_PCM_STATE_SETUP ;return retval; }
sound/core/compress_offload.c static int snd_compr_partial_drain(struct snd_compr_stream *stream) · line 770
Quoted 7 lines
sound/core/compress_offload.c +3
-0@@ -735,10 +764,16 @@ static int snd_compr_partial_drain(struct snd_compr_stream *stream)if (stream->next_track == false)return -EPERM ; + stream->runtime->drain_wake =0 ; retval = stream->ops->trigger (stream,SND_COMPR_TRIGGER_PARTIAL_DRAIN ); +if (retval) { +pr_err ("Partial drain returned failure\n" );
Ditto.
Quoted 13 lines
sound/core/compress_offload.c +4
-1+wake_up (&stream->runtime->sleep); +return retval; + } stream->next_track = false; -return retval; +return snd_compress_wait_for_drain (stream); }static long snd_compr_ioctl (struct file *f,unsigned int cmd,unsigned long arg)Signature · 1 line
1.7.0.4
Takashi
Quoted 56 lines · Takashi Iwai
At Thu, 24 Oct 2013 16:37:31 +0530,
Quoted 54 lines · Vinod Koul
The drain and
drain_notifycallback were blocked by low level driver untill the draining was complete. Due to this being invoked with big fat mutex held, others ops like reading timestamp, calling pause, drop were blocked.So to fix this we add a new
snd_compr_drain_notify()API. This would be required to be invoked by low level driver when drain or partial drain has been completed by the DSP. Thus we make the drain andpartial_draincallback as non blocking and driver returns immediately after notifying DSP.
The waiting is done while relasing the lock so that other ops can go ahead.Signed-off-by Vinod Koul <[email protected]>---
v3:
callsnd_compr_drain_notifyfromcompress_stop()rename draining -> drain_wakeadd some comments on state transistion after drain
v2:
fix the 80 line warn
move the state change tocompress_drain()include/sound/compress_driver.h | 12 +++++++++++ sound/core/compress_offload.c | 41 ++++++++++++++++++++++++++++++++++++-- 2 files changed, 50 insertions(+), 3 deletions(-)include/sound/compress_driver.h +4
-0@@ -48,6 +48,8 @@ struct snd_compr_ops;* the ring buffer * @total_bytes_transferred: cumulative bytes transferred by offload DSP * @sleep: poll sleep +* @wait: drain wait queue +* @drain_wake: condition for drain wake */ struct snd_compr_runtime {snd_pcm_state_t state;@@ -59,6 +61,8 @@ struct snd_compr_runtime {u64 total_bytes_available;u64 total_bytes_transferred;wait_queue_head_t sleep; +wait_queue_head_t wait; +unsigned int drain_wake;void *private_data; };@@ -171,4 +175,12 @@ static inline void snd_compr_fragment_elapsed(struct snd_compr_stream *stream)wake_up (&stream->runtime->sleep); }
Quoted 8 lines
include/sound/compress_driver.h static inline void snd_compr_fragment_elapsed(struct snd_compr_stream *stream) · line 180
Quoted 3 lines
include/sound/compress_driver.h +3
-0+static inline void snd_compr_drain_notify (struct snd_compr_stream *stream) +{ +snd_BUG_ON (!stream);Should return, otherwise you'll get Oops:
if (snd_BUG_ON(!stream)) return;
ah, missed that!
Quoted 38 lines
Quoted 38 lines
include/sound/compress_driver.h +5
-0+ + stream->runtime->drain_wake =1 ; + wake_up(&stream->runtime->wait); +} +#endif sound/core/compress_offload.c +11
-0@@ -123,6 +123,7 @@ static int snd_compr_open(struct inode *inode, struct file *f) } runtime->state =SNDRV_PCM_STATE_OPEN ;init_waitqueue_head (&runtime->sleep); +init_waitqueue_head (&runtime->wait); data->stream.runtime = runtime; f->private_data = (void *)data;mutex_lock (&compr->lock);@@ -682,12 +683,34 @@ static int snd_compr_stop(struct snd_compr_stream *stream)if (!retval) { stream->runtime->state =SNDRV_PCM_STATE_SETUP ;wake_up (&stream->runtime->sleep); +snd_compr_drain_notify (stream); stream->runtime->total_bytes_available =0 ; stream->runtime->total_bytes_transferred =0 ; }return retval; } +static int snd_compress_wait_for_drain (struct snd_compr_stream *stream) +{ +/* +* We are called with lock held. So drop the lock while we wait for +* drain complete notfication from the driver +* +* It is expected that driver will notify the drain completion and then +* stream will be moved to SETUP state, even if draining resulted in an +* error. We can trigger next track after this.
Quoted 8 lines
sound/core/compress_offload.c static int snd_compr_stop(struct snd_compr_stream *stream) · line 706
Quoted 5 lines
sound/core/compress_offload.c +5
-0+*/ + stream->runtime->state =SNDRV_PCM_STATE_DRAINING ; +mutex_unlock (&stream->device->lock); + +wait_event (stream->runtime->wait, stream->runtime->drain_wake);Don't you really want the interruption?
Or usewait_event_interruptible()and return error appropriately.
any interruption from usermode should trigger the compress_stop/compress_free which will unblock this. I dont see the need to have interruptible here
Quoted 18 lines
Quoted 18 lines
sound/core/compress_offload.c +9
-1+ +wake_up (&stream->runtime->sleep); +mutex_lock (&stream->device->lock); + +return 0 ; +} +static int snd_compr_drain (struct snd_compr_stream *stream) {int retval;@@ -695,11 +718,17 @@ static int snd_compr_drain(struct snd_compr_stream *stream)if (stream->runtime->state ==SNDRV_PCM_STATE_PREPARED || stream->runtime->state ==SNDRV_PCM_STATE_SETUP )return -EPERM ; + + stream->runtime->drain_wake =0 ; retval = stream->ops->trigger (stream,SND_COMPR_TRIGGER_DRAIN ); -if (!retval) {
Quoted 8 lines
sound/core/compress_offload.c static int snd_compr_drain(struct snd_compr_stream *stream) · line 725
Quoted 3 lines
sound/core/compress_offload.c +2
-1- stream->runtime->state =SNDRV_PCM_STATE_DRAINING ; +if (retval) { +pr_err ("SND_COMPR_TRIGGER_DRAIN failed %d\n" , retval);I don't think spamming such an error is good. The trigger error can happen often repeatedly once when it happens. Such a message is at most a debug message, so use
pr_debug(), if you still want to keep it in the core code.
Sure...
Thanks for the quick review, will send v4 before I hit the bed today!
--
~Vinod
Quoted 36 lines
Quoted 10 lines
sound/core/compress_offload.c +4
-0wake_up (&stream->runtime->sleep); +return retval; } + + retval =snd_compress_wait_for_drain (stream); + stream->runtime->state =SNDRV_PCM_STATE_SETUP ;return retval; }sound/core/compress_offload.c static int snd_compr_partial_drain(struct snd_compr_stream *stream) · line 770
Quoted 7 lines
sound/core/compress_offload.c +3
-0@@ -735,10 +764,16 @@ static int snd_compr_partial_drain(struct snd_compr_stream *stream)if (stream->next_track == false)return -EPERM ; + stream->runtime->drain_wake =0 ; retval = stream->ops->trigger (stream,SND_COMPR_TRIGGER_PARTIAL_DRAIN ); +if (retval) { +pr_err ("Partial drain returned failure\n" );Ditto.
Quoted 13 lines
sound/core/compress_offload.c +4
-1+wake_up (&stream->runtime->sleep); +return retval; + } stream->next_track = false; -return retval; +return snd_compress_wait_for_drain (stream); }static long snd_compr_ioctl (struct file *f,unsigned int cmd,unsigned long arg)Signature · 1 line
1.7.0.4Takashi
At Wed, 30 Oct 2013 20:28:11 +0530,
Quoted 114 lines · Vinod Koul
Quoted 56 lines · Takashi Iwai
At Thu, 24 Oct 2013 16:37:31 +0530,
Quoted 54 lines · Vinod Koul
The drain and
drain_notifycallback were blocked by low level driver untill the draining was complete. Due to this being invoked with big fat mutex held, others ops like reading timestamp, calling pause, drop were blocked.So to fix this we add a new
snd_compr_drain_notify()API. This would be required to be invoked by low level driver when drain or partial drain has been completed by the DSP. Thus we make the drain andpartial_draincallback as non blocking and driver returns immediately after notifying DSP.
The waiting is done while relasing the lock so that other ops can go ahead.Signed-off-by Vinod Koul <[email protected]>---
v3:
callsnd_compr_drain_notifyfromcompress_stop()rename draining -> drain_wakeadd some comments on state transistion after drain
v2:
fix the 80 line warn
move the state change tocompress_drain()include/sound/compress_driver.h | 12 +++++++++++ sound/core/compress_offload.c | 41 ++++++++++++++++++++++++++++++++++++-- 2 files changed, 50 insertions(+), 3 deletions(-)include/sound/compress_driver.h +4
-0@@ -48,6 +48,8 @@ struct snd_compr_ops;* the ring buffer * @total_bytes_transferred: cumulative bytes transferred by offload DSP * @sleep: poll sleep +* @wait: drain wait queue +* @drain_wake: condition for drain wake */ struct snd_compr_runtime {snd_pcm_state_t state;@@ -59,6 +61,8 @@ struct snd_compr_runtime {u64 total_bytes_available;u64 total_bytes_transferred;wait_queue_head_t sleep; +wait_queue_head_t wait; +unsigned int drain_wake;void *private_data; };@@ -171,4 +175,12 @@ static inline void snd_compr_fragment_elapsed(struct snd_compr_stream *stream)wake_up (&stream->runtime->sleep); }Quoted 8 lines
include/sound/compress_driver.h static inline void snd_compr_fragment_elapsed(struct snd_compr_stream *stream) · line 180
Quoted 3 lines
include/sound/compress_driver.h +3
-0+static inline void snd_compr_drain_notify (struct snd_compr_stream *stream) +{ +snd_BUG_ON (!stream);Should return, otherwise you'll get Oops:
if (snd_BUG_ON(!stream)) return;ah, missed that!
Quoted 38 lines
Quoted 38 lines
include/sound/compress_driver.h +5
-0+ + stream->runtime->drain_wake =1 ; + wake_up(&stream->runtime->wait); +} +#endif sound/core/compress_offload.c +11
-0@@ -123,6 +123,7 @@ static int snd_compr_open(struct inode *inode, struct file *f) } runtime->state =SNDRV_PCM_STATE_OPEN ;init_waitqueue_head (&runtime->sleep); +init_waitqueue_head (&runtime->wait); data->stream.runtime = runtime; f->private_data = (void *)data;mutex_lock (&compr->lock);@@ -682,12 +683,34 @@ static int snd_compr_stop(struct snd_compr_stream *stream)if (!retval) { stream->runtime->state =SNDRV_PCM_STATE_SETUP ;wake_up (&stream->runtime->sleep); +snd_compr_drain_notify (stream); stream->runtime->total_bytes_available =0 ; stream->runtime->total_bytes_transferred =0 ; }return retval; } +static int snd_compress_wait_for_drain (struct snd_compr_stream *stream) +{ +/* +* We are called with lock held. So drop the lock while we wait for +* drain complete notfication from the driver +* +* It is expected that driver will notify the drain completion and then +* stream will be moved to SETUP state, even if draining resulted in an +* error. We can trigger next track after this. Quoted 8 lines
sound/core/compress_offload.c static int snd_compr_stop(struct snd_compr_stream *stream) · line 706
Quoted 5 lines
sound/core/compress_offload.c +5
-0+*/ + stream->runtime->state =SNDRV_PCM_STATE_DRAINING ; +mutex_unlock (&stream->device->lock); + +wait_event (stream->runtime->wait, stream->runtime->drain_wake);Don't you really want the interruption?
Or usewait_event_interruptible()and return error appropriately.
Quoted 1 line
any interruption from usermode should trigger the compress_stop/compress_free
... but how?
Quoted 1 line
which will unblock this. I dont see the need to have interruptible here
Suppose you're running a single thread program. Then who triggers the stop?
Takashi
Quoted 17 lines · Takashi Iwai
Quoted 17 lines
Quoted 17 lines
Quoted 4 lines
sound/core/compress_offload.c +4
-0+static int snd_compress_wait_for_drain (struct snd_compr_stream *stream) +{ +/* +* We are called with lock held. So drop the lock while we wait for sound/core/compress_offload.c static int snd_compr_stop(struct snd_compr_stream *stream) · line 706
Quoted 10 lines
sound/core/compress_offload.c +10
-0+* drain complete notfication from the driver +* +* It is expected that driver will notify the drain completion and then +* stream will be moved to SETUP state, even if draining resulted in an +* error. We can trigger next track after this. +*/ + stream->runtime->state =SNDRV_PCM_STATE_DRAINING ; +mutex_unlock (&stream->device->lock); + +wait_event (stream->runtime->wait, stream->runtime->drain_wake);Don't you really want the interruption?
Or usewait_event_interruptible()and return error appropriately.
Quoted 8 lines
Quoted 1 line
any interruption from usermode should trigger the compress_stop/compress_free
... but how?
Quoted 1 line
which will unblock this. I dont see the need to have interruptible here
Suppose you're running a single thread program. Then who triggers the stop?
Your signal catcher which should be registered for signals intrested and then calls stop or free. Assuming this should be considered single threaded. IIRC, this is how you do in aplay, right?
Btw am okay to make it interruptible, but since can be done in usermode and closed which unblocks this I would prefer this way. Pls do let me know :) --
~Vinod
At Wed, 30 Oct 2013 20:48:40 +0530,
Quoted 24 lines · Vinod Koul
Quoted 21 lines · Takashi Iwai
Quoted 17 lines
Quoted 9 lines
Quoted 9 lines
sound/core/compress_offload.c +9
-0+static int snd_compress_wait_for_drain (struct snd_compr_stream *stream) +{ +/* +* We are called with lock held. So drop the lock while we wait for +* drain complete notfication from the driver +* +* It is expected that driver will notify the drain completion and then +* stream will be moved to SETUP state, even if draining resulted in an +* error. We can trigger next track after this. Quoted 8 lines
sound/core/compress_offload.c static int snd_compr_stop(struct snd_compr_stream *stream) · line 706
Quoted 5 lines
sound/core/compress_offload.c +5
-0+*/ + stream->runtime->state =SNDRV_PCM_STATE_DRAINING ; +mutex_unlock (&stream->device->lock); + +wait_event (stream->runtime->wait, stream->runtime->drain_wake);Don't you really want the interruption?
Or usewait_event_interruptible()and return error appropriately.Quoted 1 line
any interruption from usermode should trigger the compress_stop/compress_free
... but how?
Quoted 7 lines
Quoted 4 lines
Quoted 1 line
which will unblock this. I dont see the need to have interruptible here
Suppose you're running a single thread program. Then who triggers the stop?
Your signal catcher which should be registered for signals intrested and then calls stop or free. Assuming this should be considered single threaded. IIRC, this is how you do in aplay, right?
No, issuing an ioctl in a signal isn't guaranteed to work at all. You shouldn't code the kernel driver relying on that userspace behavior.
Takashi
Quoted 28 lines · Takashi Iwai
At Wed, 30 Oct 2013 20:48:40 +0530,
Quoted 26 lines · Vinod Koul
Quoted 23 lines · Takashi Iwai
Quoted 17 lines
Quoted 9 lines
Quoted 9 lines
sound/core/compress_offload.c +9
-0+static int snd_compress_wait_for_drain (struct snd_compr_stream *stream) +{ +/* +* We are called with lock held. So drop the lock while we wait for +* drain complete notfication from the driver +* +* It is expected that driver will notify the drain completion and then +* stream will be moved to SETUP state, even if draining resulted in an +* error. We can trigger next track after this. Quoted 8 lines
sound/core/compress_offload.c static int snd_compr_stop(struct snd_compr_stream *stream) · line 706
Quoted 5 lines
sound/core/compress_offload.c +5
-0+*/ + stream->runtime->state =SNDRV_PCM_STATE_DRAINING ; +mutex_unlock (&stream->device->lock); + +wait_event (stream->runtime->wait, stream->runtime->drain_wake);Don't you really want the interruption?
Or usewait_event_interruptible()and return error appropriately.Quoted 1 line
any interruption from usermode should trigger the compress_stop/compress_free
... but how?
Quoted 1 line
which will unblock this. I dont see the need to have interruptible here
Quoted 7 lines
Quoted 5 lines
Quoted 2 lines
Suppose you're running a single thread program. Then who triggers the stop?
Your signal catcher which should be registered for signals intrested and then calls stop or free. Assuming this should be considered single threaded. IIRC, this is how you do in aplay, right?
No, issuing an ioctl in a signal isn't guaranteed to work at all.
Okay thanks for pointing out, do you know why we have that limitation. Btw closing the fd should have same effect as free would trigger the stop internally
Quoted 2 lines
You shouldn't code the kernel driver relying on that userspace behavior.
Ok, will update it
--
~Vinod
At Wed, 30 Oct 2013 21:03:54 +0530,
Quoted 32 lines · Vinod Koul
Quoted 30 lines · Takashi Iwai
At Wed, 30 Oct 2013 20:48:40 +0530,
Quoted 28 lines · Vinod Koul
Quoted 25 lines · Takashi Iwai
Quoted 17 lines
Quoted 9 lines
Quoted 9 lines
(quoted diff) +9
-0+static int snd_compress_wait_for_drain(struct snd_compr_stream *stream) +{ + /* + * We are called with lock held. So drop the lock while we wait for + * drain complete notfication from the driver + * + * It is expected that driver will notify the drain completion and then + * stream will be moved to SETUP state, even if draining resulted in an + * error. We can trigger next track after this.Quoted 8 lines
Review
Quoted 5 lines
(quoted diff) +5
-0+ */ + stream->runtime->state = SNDRV_PCM_STATE_DRAINING; + mutex_unlock(&stream->device->lock); + + wait_event(stream->runtime->wait, stream->runtime->drain_wake);Don't you really want the interruption?
Or usewait_event_interruptible()and return error appropriately.Quoted 1 line
any interruption from usermode should trigger the compress_stop/compress_free
... but how?
Quoted 1 line
which will unblock this. I dont see the need to have interruptible here
Suppose you're running a single thread program. Then who triggers the stop?
Quoted 7 lines
Quoted 5 lines
Quoted 3 lines
Your signal catcher which should be registered for signals intrested and then calls stop or free. Assuming this should be considered single threaded. IIRC, this is how you do in aplay, right?
No, issuing an ioctl in a signal isn't guaranteed to work at all.
Okay thanks for pointing out, do you know why we have that limitation.
It's a long-standing limitation of POSIX.
Basically a signal handler can accept only very few functions that are asynchronous-safe. That's the reason why signal is practically useless in most use cases.
Quoted 2 lines
Btw
closing the fd should have same effect as free would trigger the stop internally
Well, better to test it before jumping into the conclusion :)
Quoted 3 lines
Quoted 2 lines
You shouldn't code the kernel driver relying on that userspace behavior.
Ok, will update it
OK.
thanks,
Takashi
PATCH v4 1/1 ALSA: compress: fix drain calls blocking other compress functions
In reply to Vinod KoulThe drain and drain_notify callback were blocked by low level driver untill the draining was complete. Due to this being invoked with big fat mutex held, others ops like reading timestamp, calling pause, drop were blocked.
So to fix this we add a new snd_compr_drain_notify() API. This would be required to be invoked by low level driver when drain or partial drain has been completed by the DSP. Thus we make the drain and partial_drain callback as non blocking and driver returns immediately after notifying DSP.
The waiting is done while relasing the lock so that other ops can go ahead.
---
v4:
move pr_err -> pr_debug to avoid spamming kernel log make wait in drain interruptible
v3:
call snd_compr_drain_notify from compress_stop()
rename draining -> drain_wake
add some comments on state transistion after drain
v2:
fix the 80 line warn
move the state change to compress_drain()
include/sound/compress_driver.h | 13 +++++++++ sound/core/compress_offload.c | 55 ++++++++++++++++++++++++++++++++++++-- 2 files changed, 65 insertions(+), 3 deletions(-)
include/sound/compress_driver.h +13 -0
@@ -48,6 +48,8 @@ struct snd_compr_ops;* the ring buffer * @total_bytes_transferred: cumulative bytes transferred by offload DSP * @sleep: poll sleep +* @wait: drain wait queue +* @drain_wake: condition for drain wake */ struct snd_compr_runtime {snd_pcm_state_t state;
@@ -59,6 +61,8 @@ struct snd_compr_runtime {u64 total_bytes_available;u64 total_bytes_transferred;wait_queue_head_t sleep; +wait_queue_head_t wait; +unsigned int drain_wake;void *private_data; };
@@ -171,4 +175,13 @@ static inline void snd_compr_fragment_elapsed(struct snd_compr_stream *stream) wake_up(&stream->runtime->sleep); } +static inline void snd_compr_drain_notify (struct snd_compr_stream *stream) +{ +if (snd_BUG_ON (!stream)) +return ; + + stream->runtime->drain_wake =1 ; +wake_up (&stream->runtime->wait); +} +#endif
sound/core/compress_offload.c +52 -3
91 lines not on this page · raw message
Signature · 1 line
1.7.0.4
At Wed, 30 Oct 2013 21:43:27 +0530,
Quoted 44 lines · Vinod Koul
The drain and
drain_notifycallback were blocked by low level driver untill the draining was complete. Due to this being invoked with big fat mutex held, others ops like reading timestamp, calling pause, drop were blocked.So to fix this we add a new
snd_compr_drain_notify()API. This would be required to be invoked by low level driver when drain or partial drain has been completed by the DSP. Thus we make the drain andpartial_draincallback as non blocking and driver returns immediately after notifying DSP.
The waiting is done while relasing the lock so that other ops can go ahead.Signed-off-by Vinod Koul <[email protected]>---
v4:
movepr_err->pr_debugto avoid spamming kernel log make wait in drain interruptible
v3:
callsnd_compr_drain_notifyfromcompress_stop()rename draining -> drain_wakeadd some comments on state transistion after drain
v2:
fix the 80 line warn
move the state change tocompress_drain()include/sound/compress_driver.h | 13 +++++++++ sound/core/compress_offload.c | 55 ++++++++++++++++++++++++++++++++++++-- 2 files changed, 65 insertions(+), 3 deletions(-)include/sound/compress_driver.h +2
-0@@ -48,6 +48,8 @@ struct snd_compr_ops;* the ring buffer * @total_bytes_transferred: cumulative bytes transferred by offload DSP * @sleep: poll sleep +* @wait: drain wait queue +* @drain_wake: condition for drain wake */ struct snd_compr_runtime {snd_pcm_state_t state;
include/sound/compress_driver.h struct snd_compr_runtime { · line 64
Quoted 4 lines
include/sound/compress_driver.h +1
-0@@ -59,6 +61,8 @@ struct snd_compr_runtime {u64 total_bytes_available;u64 total_bytes_transferred;wait_queue_head_t sleep; +wait_queue_head_t wait;
I took a look back at the code, and now wonder why you can't use the same wait queue (sleep) for drain? PCM code uses the same waitqueue.
include/sound/compress_driver.h struct snd_compr_runtime { · line 65
Quoted 1 line
include/sound/compress_driver.h +1
-0+unsigned int drain_wake;
Also, drain_wake can be omitted by checking the runtime state instead, e.g.
wait_event_interruptible(runtime->sleep, runtime->state != SNDRV_PCM_STATE_DRAINING);
while snd_compr_drain_notify() would be
static inline void snd_compr_drain_notify(struct snd_compr_stream *stream)
{
stream->runtime->state = SNDRV_PCM_STATE_SETUP;
wake_up(&stream->runtime->sleep);
}(And, if we still use drain_wake, it can be better bool instead of unsigned int.)
thanks,
Takashi
Quoted 124 lines
include/sound/compress_driver.h +9
-0void *private_data; };@@ -171,4 +175,13 @@ static inline void snd_compr_fragment_elapsed(struct snd_compr_stream *stream) wake_up(&stream->runtime->sleep); } +static inline void snd_compr_drain_notify (struct snd_compr_stream *stream) +{ +if (snd_BUG_ON (!stream)) +return ; + + stream->runtime->drain_wake =1 ; +wake_up (&stream->runtime->wait); +} +#endif sound/core/compress_offload.c +52
-3@@ -123,6 +123,7 @@ static int snd_compr_open(struct inode *inode, struct file *f) } runtime->state =SNDRV_PCM_STATE_OPEN ;init_waitqueue_head (&runtime->sleep); +init_waitqueue_head (&runtime->wait); data->stream.runtime = runtime; f->private_data = (void *)data;mutex_lock (&compr->lock);@@ -682,12 +683,48 @@ static int snd_compr_stop(struct snd_compr_stream *stream)if (!retval) { stream->runtime->state =SNDRV_PCM_STATE_SETUP ;wake_up (&stream->runtime->sleep); +snd_compr_drain_notify (stream); stream->runtime->total_bytes_available =0 ; stream->runtime->total_bytes_transferred =0 ; }return retval; } +static int snd_compress_wait_for_drain (struct snd_compr_stream *stream) +{ +int ret; + +/* +* We are called with lock held. So drop the lock while we wait for +* drain complete notfication from the driver +* +* It is expected that driver will notify the drain completion and then +* stream will be moved to SETUP state, even if draining resulted in an +* error. We can trigger next track after this. +*/ + stream->runtime->state =SNDRV_PCM_STATE_DRAINING ; +mutex_unlock (&stream->device->lock); + +/* we wait for drain to complete here, drain can return when +* interruption occurred, wait returned error or success. +* For the first two cases we don't do anything different here and +* return after waking up +*/ + + ret =wait_event_interruptible (stream->runtime->wait, + stream->runtime->drain_wake); +if (ret == -ERESTARTSYS ) +pr_debug ("wait aborted by a signal" ); +else if (ret) +pr_debug ("wait for drain failed with %d\n" , ret); + + +wake_up (&stream->runtime->sleep); +mutex_lock (&stream->device->lock); + +return ret; +} +static int snd_compr_drain (struct snd_compr_stream *stream) {int retval;@@ -695,11 +732,17 @@ static int snd_compr_drain(struct snd_compr_stream *stream)if (stream->runtime->state ==SNDRV_PCM_STATE_PREPARED || stream->runtime->state ==SNDRV_PCM_STATE_SETUP )return -EPERM ; + + stream->runtime->drain_wake =0 ; retval = stream->ops->trigger (stream,SND_COMPR_TRIGGER_DRAIN ); -if (!retval) { - stream->runtime->state =SNDRV_PCM_STATE_DRAINING ; +if (retval) { +pr_debug ("SND_COMPR_TRIGGER_DRAIN failed %d\n" , retval);wake_up (&stream->runtime->sleep); +return retval; } + + retval =snd_compress_wait_for_drain (stream); + stream->runtime->state =SNDRV_PCM_STATE_SETUP ;return retval; }@@ -735,10 +778,16 @@ static int snd_compr_partial_drain(struct snd_compr_stream *stream)if (stream->next_track == false)return -EPERM ; + stream->runtime->drain_wake =0 ; retval = stream->ops->trigger (stream,SND_COMPR_TRIGGER_PARTIAL_DRAIN ); +if (retval) { +pr_debug ("Partial drain returned failure\n" ); +wake_up (&stream->runtime->sleep); +return retval; + } stream->next_track = false; -return retval; +return snd_compress_wait_for_drain (stream); }static long snd_compr_ioctl (struct file *f,unsigned int cmd,unsigned long arg)Signature · 6 lines
1.7.0.4 _______________________________________________ Alsa-devel mailing list [email protected] http://mailman.alsa-project.org/mailman/listinfo/alsa-devel
Quoted 63 lines · Takashi Iwai
At Wed, 30 Oct 2013 21:43:27 +0530,
Quoted 44 lines · Vinod Koul
The drain and
drain_notifycallback were blocked by low level driver untill the draining was complete. Due to this being invoked with big fat mutex held, others ops like reading timestamp, calling pause, drop were blocked.So to fix this we add a new
snd_compr_drain_notify()API. This would be required to be invoked by low level driver when drain or partial drain has been completed by the DSP. Thus we make the drain andpartial_draincallback as non blocking and driver returns immediately after notifying DSP.
The waiting is done while relasing the lock so that other ops can go ahead.Signed-off-by Vinod Koul <[email protected]>---
v4:
movepr_err->pr_debugto avoid spamming kernel log make wait in drain interruptible
v3:
callsnd_compr_drain_notifyfromcompress_stop()rename draining -> drain_wakeadd some comments on state transistion after drain
v2:
fix the 80 line warn
move the state change tocompress_drain()include/sound/compress_driver.h | 13 +++++++++ sound/core/compress_offload.c | 55 ++++++++++++++++++++++++++++++++++++-- 2 files changed, 65 insertions(+), 3 deletions(-)include/sound/compress_driver.h +2
-0@@ -48,6 +48,8 @@ struct snd_compr_ops;* the ring buffer * @total_bytes_transferred: cumulative bytes transferred by offload DSP * @sleep: poll sleep +* @wait: drain wait queue +* @drain_wake: condition for drain wake */ struct snd_compr_runtime {snd_pcm_state_t state;include/sound/compress_driver.h struct snd_compr_runtime { · line 64
Quoted 4 lines
include/sound/compress_driver.h +1
-0@@ -59,6 +61,8 @@ struct snd_compr_runtime {u64 total_bytes_available;u64 total_bytes_transferred;wait_queue_head_t sleep; +wait_queue_head_t wait;I took a look back at the code, and now wonder why you can't use the same wait queue (sleep) for drain? PCM code uses the same waitqueue.
include/sound/compress_driver.h struct snd_compr_runtime { · line 65
Quoted 1 line
include/sound/compress_driver.h +1
-0+unsigned int drain_wake;Also,
drain_wakecan be omitted by checking the runtime state instead, e.g.wait_event_interruptible(runtime->sleep, runtime->state != SNDRV_PCM_STATE_DRAINING);while
snd_compr_drain_notify()would be
Quoted 8 lines
static inline void
snd_compr_drain_notify(struct snd_compr_stream *stream){ stream->runtime->state = SNDRV_PCM_STATE_SETUP; wake_up(&stream->runtime->sleep); }(And, if we still use
drain_wake, it can be better bool instead of unsigned int.)
Hmmm, it started out thinking we can reuse the existing sleep but then went ahead with a new one. I will test the changes and update this
--
~Vinod