Skip to contentSkip to conversation

ALSA: core: Allow drivers to set R/W wait time.

PATCH Superseded Liam Girdwood 3 participants 8 messages Mar 14, 2018 – Mar 16, 2018 Sound

v1 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 ALSA: core: Allow drivers to set R/W wait time.

Currently ALSA core blocks userspace for about 10 seconds for PCM R/W IO. This needs to be configurable for modern hardware like DSPs where no pointer update in milliseconds can indicate terminal DSP errors.

Add a substream variable to set the wait time in ms. This allows userspace and drivers to recover more quickly from terminal DSP errors.

---
 include/sound/pcm.h  |  6 ++++++
 sound/core/pcm_lib.c | 15 ++++++++++-----
 2 files changed, 16 insertions(+), 5 deletions(-)
include/sound/pcm.h +6 -0
index e054c583d3b3..e4694684c524 100644
@@ -462,6 +462,7 @@ struct snd_pcm_substream {
         /* -- timer section -- */
 	struct snd_timer *timer;		/* timer */
 	unsigned timer_running: 1;	/* time is running */
+	unsigned wait_time;	/* time in ms for R/W to wait for avail */
 	/* -- next substream -- */
 	struct snd_pcm_substream *next;
 	/* -- linked substreams -- */
@@ -579,6 +580,11 @@ int snd_pcm_start(struct snd_pcm_substream *substream);
 int snd_pcm_stop(struct snd_pcm_substream *substream, snd_pcm_state_t status);
 int snd_pcm_drain_done(struct snd_pcm_substream *substream);
 int snd_pcm_stop_xrun(struct snd_pcm_substream *substream);
+static inline void snd_pcm_wait_time(struct snd_pcm_substream *substream,
+	unsigned wait_time)
+{
+	substream->wait_time = wait_time;
+}
 #ifdef CONFIG_PM
 int snd_pcm_suspend(struct snd_pcm_substream *substream);
 int snd_pcm_suspend_all(struct snd_pcm *pcm);
sound/core/pcm_lib.c +10 -5
index a83152e7d387..2ee76c70f55f 100644
@@ -1839,12 +1839,17 @@ static int wait_for_avail(struct snd_pcm_substream *substream,
 	if (runtime->no_period_wakeup)
 		wait_time = MAX_SCHEDULE_TIMEOUT;
 	else {
-		wait_time = 10;
-		if (runtime->rate) {
-			long t = runtime->period_size * 2 / runtime->rate;
-			wait_time = max(t, wait_time);
+		/* use wait time from substream if available */
+		if (substream->wait_time) {
+			wait_time = msecs_to_jiffies(substream->wait_time);
+		} else {
+			wait_time = 10;
+			if (runtime->rate) {
+				long t = runtime->period_size * 2 / runtime->rate;
+				wait_time = max(t, wait_time);
+			}
+			wait_time = msecs_to_jiffies(wait_time * 1000);
 		}
-		wait_time = msecs_to_jiffies(wait_time * 1000);
 	}
 
 	for (;;) {
Signature · 1 line
2.14.1
#1 lore raw
Quoted 9 lines · Liam Girdwood

Currently ALSA core blocks userspace for about 10 seconds for PCM R/W IO. This needs to be configurable for modern hardware like DSPs where no pointer update in milliseconds can indicate terminal DSP errors.

Add a substream variable to set the wait time in ms. This allows userspace and drivers to recover more quickly from terminal DSP errors.

The idea looks good, though, a bit of nitpicking:

Quoted 10 lines
---
 include/sound/pcm.h  |  6 ++++++
 sound/core/pcm_lib.c | 15 ++++++++++-----
 2 files changed, 16 insertions(+), 5 deletions(-)
include/sound/pcm.h +0 -0
index e054c583d3b3..e4694684c524 100644

include/sound/pcm.h struct snd_pcm_substream { · line 465

Quoted 4 lines
include/sound/pcm.h +1 -0
@@ -462,6 +462,7 @@ struct snd_pcm_substream {
         /* -- timer section -- */
 	struct snd_timer *timer;		/* timer */
 	unsigned timer_running: 1;	/* time is running */
+	unsigned wait_time;	/* time in ms for R/W to wait for avail */

I'd name wait_timeout, which is slightly clearer.

include/sound/pcm.h int snd_pcm_start(struct snd_pcm_substream *substream); · line 587

Quoted 12 lines
include/sound/pcm.h +5 -0
 	/* -- next substream -- */
 	struct snd_pcm_substream *next;
 	/* -- linked substreams -- */
@@ -579,6 +580,11 @@ int snd_pcm_start(struct snd_pcm_substream *substream);
 int snd_pcm_stop(struct snd_pcm_substream *substream, snd_pcm_state_t status);
 int snd_pcm_drain_done(struct snd_pcm_substream *substream);
 int snd_pcm_stop_xrun(struct snd_pcm_substream *substream);
+static inline void snd_pcm_wait_time(struct snd_pcm_substream *substream,
+	unsigned wait_time)
+{
+	substream->wait_time = wait_time;
+}

IMO, it's a simple one parameter, and no need wrapping with an inline function.

Quoted 17 lines
include/sound/pcm.h +0 -0
 #ifdef CONFIG_PM
 int snd_pcm_suspend(struct snd_pcm_substream *substream);
 int snd_pcm_suspend_all(struct snd_pcm *pcm);
sound/core/pcm_lib.c +2 -4
index a83152e7d387..2ee76c70f55f 100644
@@ -1839,12 +1839,17 @@ static int wait_for_avail(struct snd_pcm_substream *substream,
 	if (runtime->no_period_wakeup)
 		wait_time = MAX_SCHEDULE_TIMEOUT;
 	else {
-		wait_time = 10;
-		if (runtime->rate) {
-			long t = runtime->period_size * 2 / runtime->rate;
-			wait_time = max(t, wait_time);
+		/* use wait time from substream if available */
+		if (substream->wait_time) {

sound/core/pcm_lib.c static int wait_for_avail(struct snd_pcm_substream *substream, · line 1847

Quoted 10 lines
sound/core/pcm_lib.c +8 -1
+			wait_time = msecs_to_jiffies(substream->wait_time);
+		} else {
+			wait_time = 10;
+			if (runtime->rate) {
+				long t = runtime->period_size * 2 / runtime->rate;
+				wait_time = max(t, wait_time);
+			}
+			wait_time = msecs_to_jiffies(wait_time * 1000);
 		}
-		wait_time = msecs_to_jiffies(wait_time * 1000);

This can go bad when wait_time is shorter than the period time. Some validation is needed?

Also, how is user-space supposed to set the new parameter?

thanks,

Takashi

#2 lore raw
Quoted 33 lines · Takashi Iwai
Quoted 9 lines · Liam Girdwood

Currently ALSA core blocks userspace for about 10 seconds for PCM R/W IO. This needs to be configurable for modern hardware like DSPs where no pointer update in milliseconds can indicate terminal DSP errors.

Add a substream variable to set the wait time in ms. This allows userspace and drivers to recover more quickly from terminal DSP errors.

The idea looks good, though, a bit of nitpicking:

Quoted 10 lines
---
 include/sound/pcm.h  |  6 ++++++
 sound/core/pcm_lib.c | 15 ++++++++++-----
 2 files changed, 16 insertions(+), 5 deletions(-)
include/sound/pcm.h +0 -0
index e054c583d3b3..e4694684c524 100644

include/sound/pcm.h struct snd_pcm_substream { · line 466

Quoted 5 lines
include/sound/pcm.h +1 -0
@@ -462,6 +462,7 @@ struct snd_pcm_substream {
         /* -- timer section -- */
 	struct snd_timer *timer;		/* timer */
 	unsigned timer_running: 1;	/* time is running */
+	unsigned wait_time;	/* time in ms for R/W to wait for avail
 */

I'd name wait_timeout, which is slightly clearer.

Quoted 16 lines

include/sound/pcm.h int snd_pcm_start(struct snd_pcm_substream *substream); · line 588

Quoted 13 lines
include/sound/pcm.h +0 -0
 	/* -- next substream -- */
 	struct snd_pcm_substream *next;
include/sound/pcm.h +5 -0
 	/* -- linked substreams -- */
@@ -579,6 +580,11 @@ int snd_pcm_start(struct snd_pcm_substream *substream);
 int snd_pcm_stop(struct snd_pcm_substream *substream, snd_pcm_state_t
 status);
 int snd_pcm_drain_done(struct snd_pcm_substream *substream);
 int snd_pcm_stop_xrun(struct snd_pcm_substream *substream);
+static inline void snd_pcm_wait_time(struct snd_pcm_substream *substream,
+	unsigned wait_time)
+{
+	substream->wait_time = wait_time;
+}

IMO, it's a simple one parameter, and no need wrapping with an inline function.

This was for drivers to set the parameter according to HW capabilities.

Quoted 27 lines
Quoted 27 lines
include/sound/pcm.h +0 -0
 #ifdef CONFIG_PM
 int snd_pcm_suspend(struct snd_pcm_substream *substream);

int snd_pcm_suspend_all(struct snd_pcm *pcm);

sound/core/pcm_lib.c +7 -4
index a83152e7d387..2ee76c70f55f 100644
@@ -1839,12 +1839,17 @@ static int wait_for_avail(struct snd_pcm_substream
 *substream,
 	if (runtime->no_period_wakeup)
 		wait_time = MAX_SCHEDULE_TIMEOUT;
 	else {
-		wait_time = 10;
-		if (runtime->rate) {
-			long t = runtime->period_size * 2 / runtime->rate;
-			wait_time = max(t, wait_time);
+		/* use wait time from substream if available */
+		if (substream->wait_time) {
+			wait_time = msecs_to_jiffies(substream->wait_time);
+		} else {
+			wait_time = 10;
+			if (runtime->rate) {
+				long t = runtime->period_size * 2 /
 runtime->rate;
Quoted 9 lines

sound/core/pcm_lib.c static int wait_for_avail(struct snd_pcm_substream *substream, · line 1847

Quoted 5 lines
sound/core/pcm_lib.c +3 -1
+				wait_time = max(t, wait_time);
+			}
+			wait_time = msecs_to_jiffies(wait_time * 1000);
 		}
-		wait_time = msecs_to_jiffies(wait_time * 1000);

This can go bad when wait_time is shorter than the period time. Some validation is needed?

Yes, and we should also validate no irq mode somehow too, probably using buffer time.

Quoted 2 lines

Also, how is user-space supposed to set the new parameter?

It's not atm, as it was being set by the driver. Would probably mean an ABI change to PCM ops or a new ioctl ? The latter wont break the ABI and the default value would remain if the ioctl was not called.

Liam

Quoted 4 lines

thanks,

Takashi

#3 lore raw
Quoted 52 lines · Liam Girdwood
Quoted 33 lines · Takashi Iwai
Quoted 9 lines · Liam Girdwood

Currently ALSA core blocks userspace for about 10 seconds for PCM R/W IO. This needs to be configurable for modern hardware like DSPs where no pointer update in milliseconds can indicate terminal DSP errors.

Add a substream variable to set the wait time in ms. This allows userspace and drivers to recover more quickly from terminal DSP errors.

The idea looks good, though, a bit of nitpicking:

Quoted 10 lines
---
 include/sound/pcm.h  |  6 ++++++
 sound/core/pcm_lib.c | 15 ++++++++++-----
 2 files changed, 16 insertions(+), 5 deletions(-)
include/sound/pcm.h +0 -0
index e054c583d3b3..e4694684c524 100644

include/sound/pcm.h struct snd_pcm_substream { · line 466

Quoted 5 lines
include/sound/pcm.h +1 -0
@@ -462,6 +462,7 @@ struct snd_pcm_substream {
         /* -- timer section -- */
 	struct snd_timer *timer;		/* timer */
 	unsigned timer_running: 1;	/* time is running */
+	unsigned wait_time;	/* time in ms for R/W to wait for avail
 */

I'd name wait_timeout, which is slightly clearer.

Quoted 16 lines

include/sound/pcm.h int snd_pcm_start(struct snd_pcm_substream *substream); · line 588

Quoted 13 lines
include/sound/pcm.h +0 -0
 	/* -- next substream -- */
 	struct snd_pcm_substream *next;
include/sound/pcm.h +5 -0
 	/* -- linked substreams -- */
@@ -579,6 +580,11 @@ int snd_pcm_start(struct snd_pcm_substream *substream);
 int snd_pcm_stop(struct snd_pcm_substream *substream, snd_pcm_state_t
 status);
 int snd_pcm_drain_done(struct snd_pcm_substream *substream);
 int snd_pcm_stop_xrun(struct snd_pcm_substream *substream);
+static inline void snd_pcm_wait_time(struct snd_pcm_substream *substream,
+	unsigned wait_time)
+{
+	substream->wait_time = wait_time;
+}

IMO, it's a simple one parameter, and no need wrapping with an inline function.

Quoted 1 line

This was for drivers to set the parameter according to HW capabilities.

My point is that there is little merit to provide an extra API function just for setting this parameter. Each driver knows the snd_pcm_substream content clearly, and it can run "substream->wait_time = xxx" explicitly.

Of course, the situation would become different if this is seen as an exposed feature to user-space.

Quoted 34 lines
Quoted 24 lines
Quoted 24 lines
include/sound/pcm.h +0 -0
 #ifdef CONFIG_PM
 int snd_pcm_suspend(struct snd_pcm_substream *substream);

int snd_pcm_suspend_all(struct snd_pcm *pcm);

sound/core/pcm_lib.c +7 -4
index a83152e7d387..2ee76c70f55f 100644
@@ -1839,12 +1839,17 @@ static int wait_for_avail(struct snd_pcm_substream
 *substream,
 	if (runtime->no_period_wakeup)
 		wait_time = MAX_SCHEDULE_TIMEOUT;
 	else {
-		wait_time = 10;
-		if (runtime->rate) {
-			long t = runtime->period_size * 2 / runtime->rate;
-			wait_time = max(t, wait_time);
+		/* use wait time from substream if available */
+		if (substream->wait_time) {
+			wait_time = msecs_to_jiffies(substream->wait_time);
+		} else {
+			wait_time = 10;
+			if (runtime->rate) {
+				long t = runtime->period_size * 2 /
 runtime->rate;
Quoted 9 lines

sound/core/pcm_lib.c static int wait_for_avail(struct snd_pcm_substream *substream, · line 1847

Quoted 5 lines
sound/core/pcm_lib.c +3 -1
+				wait_time = max(t, wait_time);
+			}
+			wait_time = msecs_to_jiffies(wait_time * 1000);
 		}
-		wait_time = msecs_to_jiffies(wait_time * 1000);

This can go bad when wait_time is shorter than the period time. Some validation is needed?

Quoted 2 lines

Yes, and we should also validate no irq mode somehow too, probably using buffer time.

Ah, now I see what you have in mind somehow...

Quoted 6 lines
Quoted 2 lines

Also, how is user-space supposed to set the new parameter?

It's not atm, as it was being set by the driver. Would probably mean an ABI change to PCM ops or a new ioctl ? The latter wont break the ABI and the default value would remain if the ioctl was not called.

Basically this timeout is merely for a safety, wasn't considered as a part of the real functionality.

So, with your plan, this is exposed as a real PCM feature, as a part of API? For what kind of scenario / purpose?

thanks,

Takashi

#4 lore raw
Quoted 10 lines · Takashi Iwai
Quoted 4 lines

It's not atm, as it was being set by the driver. Would probably mean an ABI change to PCM ops or a new ioctl ? The latter wont break the ABI and the default
value would remain if the ioctl was not called.

Basically this timeout is merely for a safety, wasn't considered as a part of the real functionality.

So, with your plan, this is exposed as a real PCM feature, as a part of API? For what kind of scenario / purpose?

Use case is XRUN handling, DMA failure or FW crash detection. The shortened timeout means we can recover far faster leaving a smaller gap in any audio.

Liam

maintainer
#5 lore raw

Dne 15.3.2018 v 17:07 Liam Girdwood napsal(a):

Quoted 6 lines · Takashi Iwai
Quoted 5 lines
Quoted 4 lines

It's not atm, as it was being set by the driver. Would probably mean an ABI change to PCM ops or a new ioctl ? The latter wont break the ABI and the default
value would remain if the ioctl was not called.

Quoted 8 lines
Quoted 5 lines

Basically this timeout is merely for a safety, wasn't considered as a part of the real functionality.

So, with your plan, this is exposed as a real PCM feature, as a part of API? For what kind of scenario / purpose?

Use case is XRUN handling, DMA failure or FW crash detection. The shortened timeout means we can recover far faster leaving a smaller gap in any audio.

We have the non-blocking access mode for this purpose where the application can choose the state check time independently.

It seems to me, that you like to resolve something else with this. The 'error' checking should be handled at the driver level in my opinion.

I see only the possibility to reduce the timeout to a more appropriate value (we know all stream and buffering parameters to calculate the 'late' time limit). I agree that the current timeout is too big, but it's something outside the API as Takashi mentioned.

Jaroslav

Signature · 2 lines
Jaroslav Kysela <[email protected]>
Linux Sound Maintainer; ALSA Project; Red Hat, Inc.
#6 lore raw
Quoted 11 lines · Jaroslav Kysela

Dne 15.3.2018 v 17:07 Liam Girdwood napsal(a):

Quoted 10 lines · Takashi Iwai
Quoted 9 lines
Quoted 5 lines

It's not atm, as it was being set by the driver. Would probably mean an ABI
change to PCM ops or a new ioctl ? The latter wont break the ABI and the default
value would remain if the ioctl was not called.

Basically this timeout is merely for a safety, wasn't considered as a part of the real functionality.

Quoted 8 lines
Quoted 5 lines
Quoted 2 lines

So, with your plan, this is exposed as a real PCM feature, as a part of API? For what kind of scenario / purpose?

Use case is XRUN handling, DMA failure or FW crash detection. The shortened timeout means we can recover far faster leaving a smaller gap in any audio.

We have the non-blocking access mode for this purpose where the application can choose the state check time independently.

Yes, but unfortunately I'm coming across a lot of userspace who are using ALSA blocking IO as an audio synchronisation mechanism.

Quoted 4 lines

It seems to me, that you like to resolve something else with this. The 'error' checking should be handled at the driver level in my opinion.

It mostly is handled by the driver in this case, but userspace may care and is immediately notified by the IO failing. Fwiw, this is important in safety critical situations where any failure of audio could be deemed a safety risk (automotive use cases).

Quoted 4 lines

I see only the possibility to reduce the timeout to a more appropriate value (we know all stream and buffering parameters to calculate the 'late' time limit). I agree that the current timeout is too big, but it's something outside the API as Takashi mentioned.

ok, any objections if I change it directly (in the structure) from the SOF driver and have some core PCM hw_params() check that makes sure we are good for period/buffer time ?

Liam

Quoted 3 lines

Jaroslav

#7 lore raw
Quoted 35 lines · Liam Girdwood
Quoted 11 lines · Jaroslav Kysela

Dne 15.3.2018 v 17:07 Liam Girdwood napsal(a):

Quoted 10 lines · Takashi Iwai
Quoted 9 lines
Quoted 5 lines

It's not atm, as it was being set by the driver. Would probably mean an ABI
change to PCM ops or a new ioctl ? The latter wont break the ABI and the default
value would remain if the ioctl was not called.

Basically this timeout is merely for a safety, wasn't considered as a part of the real functionality.

Quoted 8 lines
Quoted 5 lines
Quoted 2 lines

So, with your plan, this is exposed as a real PCM feature, as a part of API? For what kind of scenario / purpose?

Use case is XRUN handling, DMA failure or FW crash detection. The shortened timeout means we can recover far faster leaving a smaller gap in any audio.

We have the non-blocking access mode for this purpose where the application can choose the state check time independently.

Yes, but unfortunately I'm coming across a lot of userspace who are using ALSA blocking IO as an audio synchronisation mechanism.

Quoted 4 lines

It seems to me, that you like to resolve something else with this. The 'error' checking should be handled at the driver level in my opinion.

It mostly is handled by the driver in this case, but userspace may care and is immediately notified by the IO failing. Fwiw, this is important in safety critical situations where any failure of audio could be deemed a safety risk (automotive use cases).

Quoted 8 lines
Quoted 4 lines

I see only the possibility to reduce the timeout to a more appropriate value (we know all stream and buffering parameters to calculate the 'late' time limit). I agree that the current timeout is too big, but it's something outside the API as Takashi mentioned.

ok, any objections if I change it directly (in the structure) from the SOF driver and have some core PCM hw_params() check that makes sure we are good for period/buffer time ?

I'm fine with that.

thanks,

Takashi