Linux Sound subsystem development
 help / color / mirror / Atom feed
* [PATCH 0/2] ASoC: qcom: q6apm-dai: stop the playback queue from wedging the DSP
@ 2026-11-20 19:47 Perry Dale
  2026-11-20 19:47 ` [PATCH 1/2] ASoC: qcom: q6apm-dai: queue playback buffers only while the graph runs Perry Dale
  2026-11-20 19:47 ` [PATCH 2/2] ASoC: qcom: q6apm-dai: keep a playback stream running across a PCM reset Perry Dale
  0 siblings, 2 replies; 4+ messages in thread
From: Perry Dale @ 2026-11-20 19:47 UTC (permalink / raw)
  To: Srinivas Kandagatla, Liam Girdwood, Mark Brown, Jaroslav Kysela,
	Takashi Iwai
  Cc: linux-sound, linux-arm-msm, linux-kernel, Perry Dale

On the AYN Odin 2 Portal (SM8550) the audio DSP stops answering GPR
commands when a game pauses its stream, and every later prepare times
out until a reboot. q6apm_dai_ack() queues one write per period of the
whole appl_ptr after a trigger stop has reset queue_ptr.

Patch 1 is the fix and is marked for stable. It queues only while the
graph runs and bounds what one call can queue. With it alone, a PCM
reset on a running stream past the first ring stops that stream until
its next prepare, and the DSP keeps answering.

Patch 2 carries a running stream across a PCM reset. It counts the
buffers the DSP holds, queues from the write-done event as well as from
.ack, and stops the stream with an xrun when a send fails. It is not
needed for stable.

Both were built as a module for 7.4-rc1 and run on the Portal with a
test program of ten cases. The unpatched driver fails the first drop
round of that program. Each changelog lists what was run and what was
not, and describes the use of AI coding tools.

Perry Dale (2):
  ASoC: qcom: q6apm-dai: queue playback buffers only while the graph
    runs
  ASoC: qcom: q6apm-dai: keep a playback stream running across a PCM
    reset

 sound/soc/qcom/qdsp6/q6apm-dai.c | 166 ++++++++++++++++++++++++++++---
 1 file changed, 152 insertions(+), 14 deletions(-)


base-commit: 6628e7d4c6b25c8d18f1345e3a20d21829aae7e4
-- 
2.56.0


^ permalink raw reply	[flat|nested] 4+ messages in thread

* [PATCH 1/2] ASoC: qcom: q6apm-dai: queue playback buffers only while the graph runs
  2026-11-20 19:47 [PATCH 0/2] ASoC: qcom: q6apm-dai: stop the playback queue from wedging the DSP Perry Dale
@ 2026-11-20 19:47 ` Perry Dale
  2026-11-20 19:47 ` [PATCH 2/2] ASoC: qcom: q6apm-dai: keep a playback stream running across a PCM reset Perry Dale
  1 sibling, 0 replies; 4+ messages in thread
From: Perry Dale @ 2026-11-20 19:47 UTC (permalink / raw)
  To: Srinivas Kandagatla, Liam Girdwood, Mark Brown, Jaroslav Kysela,
	Takashi Iwai
  Cc: linux-sound, linux-arm-msm, linux-kernel, Perry Dale, stable,
	Srinivas Kandagatla

Commit cab45ab95ce7 ("ASoC: qcom: q6apm-dai: reset queue ptr on trigger
stop") resets queue_ptr to 0 on SNDRV_PCM_TRIGGER_STOP, but ALSA leaves
appl_ptr unchanged until the next prepare. ALSA calls .ack when a
write, a forward or SNDRV_PCM_IOCTL_SYNC_PTR moves appl_ptr, and
SYNC_PTR can do so in the SETUP and XRUN states after a stop.
q6apm_dai_ack() then computes avail_periods from the full appl_ptr and
issues one q6apm_write_async() per period. After 530 seconds of playback
at 48 kHz with 480-frame periods that is about 53000 writes in one call.
The glink channel runs out of remote intents, the write fails with
-EBUSY, and the DSP stops answering GPR commands. Every later prepare
times out with -ETIMEDOUT until a reboot:

  q6apm-dai ...: Error queuing playback buffer -16
  qcom-apm gprsvc:service:2:1: CMD timeout for [1001003] opcode
  q6apm-lpass-dais ...: Failed to set media format -110

On the AYN Odin 2 Portal (SM8550) PipeWire triggers this when a game
pauses its stream. A test program reproduces it by playing one second
on the PCM, calling snd_pcm_drop() and moving appl_ptr forward one
period with SNDRV_PCM_IOCTL_SYNC_PTR.

Queue only while the graph is running, which prepare sets once the
graph is started and trigger stop clears, and only in the PREPARED,
RUNNING, PAUSED and DRAINING states. A drop before start does not reach
the trigger callback, so the driver state alone does not cover it.
Reset queue_ptr in prepare, where ALSA resets appl_ptr. Prepare runs
without the stream lock and a second prepare finds the stream running,
so mark it stopped under the stream lock before the graph and its
buffers are replaced.

The same unsigned difference is wrong in two more cases.
SNDRV_PCM_IOCTL_RESET moves appl_ptr back to hw_ptr without a call to
.ack and leaves queue_ptr ahead of it, and appl_ptr wraps at the
boundary while queue_ptr did not. Both can queue a burst of writes.
Wrap queue_ptr at the boundary, take the difference modulo the
boundary, and queue nothing when it is more than ALSA holds for
playback. That also covers an appl_ptr moved more than a ring past
hw_ptr.

After a reset on a running stream whose hw_ptr has reached the end of
the first ring, or on a paused or suspended stream that far along, the
stream no longer advances until its next prepare, and the DSP keeps
answering. The next patch carries the stream across a reset. It is not
needed for stable.

An .ack between the end of the driver's prepare and the point where
ALSA resets appl_ptr can still queue stale data, as before, now at most
one ring in total.

Tool use: drafted with Claude Opus 5.5 and revised with Claude Fable
5.2 (Anthropic), run in Claude Code, from a prompt asking for the cause
of the audio DSP failures during games on the Portal. The tools
analysed the kernel logs and wrote the test program, the change and
this changelog. Built as a module for 7.4-rc1 and run on the Portal
with the test program: plain playback, drop followed by appl_ptr
updates before and after start, resets while running and before start,
underrun recovery, 30 drained streams, and 40 resets and prepares while
a second thread moves appl_ptr with SYNC_PTR. The DSP kept answering
through every run. The resets on a running stream stopped it as
described above. The unpatched driver fails the first drop round of
the same program with the messages above. The boundary wrap was not
exercised.

Fixes: cab45ab95ce7 ("ASoC: qcom: q6apm-dai: reset queue ptr on trigger stop")
Fixes: 3d4a4411aa8b ("ASoC: q6apm-dai: schedule all available frames to avoid dsp under-runs")
Cc: stable@vger.kernel.org
Cc: Srinivas Kandagatla <srinivas.kandagatla@oss.qualcomm.com>
Signed-off-by: Perry Dale <techluddite@outlook.com>
---
 sound/soc/qcom/qdsp6/q6apm-dai.c | 52 +++++++++++++++++++++++++++++++-
 1 file changed, 51 insertions(+), 1 deletion(-)

diff --git a/sound/soc/qcom/qdsp6/q6apm-dai.c b/sound/soc/qcom/qdsp6/q6apm-dai.c
index 58beaf906..d0d19fc38 100644
--- a/sound/soc/qcom/qdsp6/q6apm-dai.c
+++ b/sound/soc/qcom/qdsp6/q6apm-dai.c
@@ -345,6 +345,17 @@ static int q6apm_dai_prepare(struct snd_soc_component *component,
 	cfg.bit_width = prtd->bits_per_sample;
 	cfg.fmt = SND_AUDIOCODEC_PCM;
 	audioreach_set_default_channel_mapping(cfg.channel_map, runtime->channels);
+	/*
+	 * A second prepare finds the stream running. Stop .ack from queueing
+	 * while the graph and its buffers are replaced.
+	 */
+	snd_pcm_stream_lock_irq(substream);
+	if (prtd->state == Q6APM_STREAM_RUNNING)
+		prtd->state = Q6APM_STREAM_STOPPED;
+	/* ALSA sets appl_ptr to 0 after a successful prepare */
+	prtd->queue_ptr = 0;
+	snd_pcm_stream_unlock_irq(substream);
+
 	if (prtd->state) {
 		/* clear the previous setup if any  */
 		q6apm_graph_stop(prtd->graph);
@@ -415,7 +426,9 @@ static int q6apm_dai_prepare(struct snd_soc_component *component,
 	}
 
 	/* Now that graph as been prepared and started update the internal state accordingly */
+	snd_pcm_stream_lock_irq(substream);
 	prtd->state = Q6APM_STREAM_RUNNING;
+	snd_pcm_stream_unlock_irq(substream);
 
 	return 0;
 }
@@ -429,8 +442,43 @@ static int q6apm_dai_ack(struct snd_soc_component *component, struct snd_pcm_sub
 	if (q6apm_is_graph_in_push_pull_mode(prtd->graph))
 		return 0;
 
+	/*
+	 * ALSA calls .ack whenever appl_ptr moves, also after a stop and
+	 * before the next prepare. The graph has no queue to fill then, and
+	 * queue_ptr is out of step with appl_ptr until prepare resets both.
+	 */
+	if (prtd->state != Q6APM_STREAM_RUNNING)
+		return 0;
+
+	/* A drop before start does not reach the trigger callback. */
+	switch (runtime->state) {
+	case SNDRV_PCM_STATE_PREPARED:
+	case SNDRV_PCM_STATE_RUNNING:
+	case SNDRV_PCM_STATE_PAUSED:
+	case SNDRV_PCM_STATE_DRAINING:
+		break;
+	default:
+		return 0;
+	}
+
 	if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
-		avail_periods = (runtime->control->appl_ptr - prtd->queue_ptr)/runtime->period_size;
+		snd_pcm_sframes_t pending = runtime->control->appl_ptr - prtd->queue_ptr;
+
+		/* appl_ptr wraps at the boundary before queue_ptr does. */
+		if (pending < 0)
+			pending += runtime->boundary;
+
+		/*
+		 * What is left to queue is part of what ALSA holds for
+		 * playback. It is more when queue_ptr is ahead of appl_ptr,
+		 * as after SNDRV_PCM_IOCTL_RESET, or when appl_ptr was moved
+		 * more than a ring past hw_ptr.
+		 */
+		if (pending > snd_pcm_playback_hw_avail(runtime))
+			return 0;
+
+		avail_periods = min_t(snd_pcm_sframes_t, pending / runtime->period_size,
+				      runtime->periods);
 		for (i = 0; i < avail_periods; i++) {
 			ret = q6apm_write_async(prtd->graph, prtd->pcm_count, 0, 0, NO_TIMESTAMP);
 			if (ret < 0) {
@@ -438,6 +486,8 @@ static int q6apm_dai_ack(struct snd_soc_component *component, struct snd_pcm_sub
 				return ret;
 			}
 			prtd->queue_ptr += runtime->period_size;
+			if (prtd->queue_ptr >= runtime->boundary)
+				prtd->queue_ptr -= runtime->boundary;
 		}
 	}
 
-- 
2.56.0


^ permalink raw reply related	[flat|nested] 4+ messages in thread

* [PATCH 2/2] ASoC: qcom: q6apm-dai: keep a playback stream running across a PCM reset
  2026-11-20 19:47 [PATCH 0/2] ASoC: qcom: q6apm-dai: stop the playback queue from wedging the DSP Perry Dale
  2026-11-20 19:47 ` [PATCH 1/2] ASoC: qcom: q6apm-dai: queue playback buffers only while the graph runs Perry Dale
@ 2026-11-20 19:47 ` Perry Dale
  2026-11-20 20:17   ` sashiko-bot
  1 sibling, 1 reply; 4+ messages in thread
From: Perry Dale @ 2026-11-20 19:47 UTC (permalink / raw)
  To: Srinivas Kandagatla, Liam Girdwood, Mark Brown, Jaroslav Kysela,
	Takashi Iwai
  Cc: linux-sound, linux-arm-msm, linux-kernel, Perry Dale,
	Srinivas Kandagatla

SNDRV_PCM_IOCTL_RESET sets appl_ptr to hw_ptr. On a running stream it
first reduces hw_ptr modulo the buffer size, otherwise it sets hw_ptr
to 0. The period buffers already given to the DSP stay with it, and
queue_ptr keeps its value from before the reset. Once that value is
at least a ring away from the new hw_ptr, q6apm_dai_ack() queues
nothing, the application fills the ring and the stream stops advancing
until its next prepare.

Count the buffers the DSP holds. A buffer is added when it is queued
and taken off by the write-done event, under the stream lock and in the
same section that reports the elapsed period. Prepare clears the count
when it replaces the buffers.

Handle SNDRV_PCM_IOCTL1_RESET in an ioctl callback. After
snd_pcm_lib_ioctl() has moved hw_ptr, give queue_ptr the ring offset it
had, relative to the new hw_ptr. The next buffer handed to the DSP is
then still the one after the buffers it holds. That offset is 0 both
for an empty and for a full queue. A running stream with every buffer
queued continues with those buffers, and what the application writes
over them is not queued a second time. In every other case with offset
0 the queue is taken as empty, and what is written from there on is
queued as buffers come back.

Move the queueing into a helper that both .ack and the write-done event
call. One call queues at most the buffers the DSP does not hold. The
limit of one ring per call did not account for buffers already queued.
With that limit a write can find no free buffer, so the event queues
what is waiting when a buffer comes back. Without it the data waited
for the next write, which never comes at the end of a stream.

The helper measures queue_ptr against hw_ptr and aligns it in the same
way when it is not within one ring ahead. hw_ptr passes queue_ptr after
a reset before start with every buffer queued, and a reset on a paused
or suspended stream can leave the two apart as well.

A send that fails now stops the stream with an xrun. The core has
advanced its buffer index by then, and only a prepare brings it back in
step with queue_ptr. Before, the error went to the writer and the two
stayed out of step.

Not changed here: the graph keeps playing while the stream is paused,
so a reset on a paused stream can skip the first period written after
it, and a write-done event that the core drops leaves the count one too
high until the next prepare.

Tool use: written with Claude Fable 5.2 (Anthropic), run in Claude
Code, from a prompt asking to carry a running stream across a reset.
The tool wrote the change, the test program and this changelog, and
revised them after reviews by Claude Opus 5.5 and GPT (Codex). Built as
a module for 7.4-rc1 and run on the AYN Odin 2 Portal (SM8550) with the
test program of the previous patch. Five resets in a row on a running
stream, a reset with the queue played out in the second lap, a reset
before start with two periods written, and a reset and rewrite with a
full ring queued before start all continued. The cases of the previous
patch passed, and the DSP kept answering through every run. A debug
build that failed two sends during PipeWire playback stopped the stream
each time, and PipeWire restarted it.
The test checks that the stream advances. It does not check which
samples the DSP plays for the buffers it held at a reset, and it does
not exercise the boundary wrap or a paused stream.

Cc: Srinivas Kandagatla <srinivas.kandagatla@oss.qualcomm.com>
Signed-off-by: Perry Dale <techluddite@outlook.com>
---
 sound/soc/qcom/qdsp6/q6apm-dai.c | 156 ++++++++++++++++++++++++-------
 1 file changed, 122 insertions(+), 34 deletions(-)

diff --git a/sound/soc/qcom/qdsp6/q6apm-dai.c b/sound/soc/qcom/qdsp6/q6apm-dai.c
index d0d19fc38..e4a6817ee 100644
--- a/sound/soc/qcom/qdsp6/q6apm-dai.c
+++ b/sound/soc/qcom/qdsp6/q6apm-dai.c
@@ -91,6 +91,8 @@ struct q6apm_dai_rtd {
 	uint64_t copied_total;
 	uint16_t bits_per_sample;
 	snd_pcm_uframes_t queue_ptr;
+	unsigned int in_flight;
+	struct device *dev;
 	bool next_track;
 	enum stream_state state;
 	struct q6apm_graph *graph;
@@ -236,6 +238,8 @@ static const struct snd_pcm_hardware q6apm_dai_hardware_playback = {
 	.fifo_size =            0,
 };
 
+static int q6apm_dai_queue_playback(struct snd_pcm_substream *substream);
+
 static void event_handler(uint32_t opcode, uint32_t token, void *payload, void *priv)
 {
 	struct q6apm_dai_rtd *prtd = priv;
@@ -249,7 +253,13 @@ static void event_handler(uint32_t opcode, uint32_t token, void *payload, void *
 		prtd->state = Q6APM_STREAM_STOPPED;
 		break;
 	case APM_CLIENT_EVENT_DATA_WRITE_DONE:
-		snd_pcm_period_elapsed(substream);
+		scoped_guard(pcm_stream_lock_irqsave, substream) {
+			if (prtd->in_flight)
+				prtd->in_flight--;
+			snd_pcm_period_elapsed_under_stream_lock(substream);
+			/* The returned buffer may be needed for data already written. */
+			q6apm_dai_queue_playback(substream);
+		}
 
 		break;
 	case APM_CLIENT_EVENT_DATA_READ_DONE:
@@ -363,6 +373,10 @@ static int q6apm_dai_prepare(struct snd_soc_component *component,
 	}
 
 	prtd->last_pos_index = 0;
+	/* The graph gets a new set of buffers and starts with none queued. */
+	snd_pcm_stream_lock_irq(substream);
+	prtd->in_flight = 0;
+	snd_pcm_stream_unlock_irq(substream);
 	prtd->pcm_count = snd_pcm_lib_period_bytes(substream);
 	if (q6apm_is_graph_in_push_pull_mode(prtd->graph)) {
 		if (prtd->pcm_size != prtd->push_pull_size) {
@@ -433,14 +447,39 @@ static int q6apm_dai_prepare(struct snd_soc_component *component,
 	return 0;
 }
 
-static int q6apm_dai_ack(struct snd_soc_component *component, struct snd_pcm_substream *substream)
+/*
+ * Give queue_ptr its ring offset back relative to hw_ptr. Called with the
+ * stream lock held after hw_ptr moved without it.
+ */
+static void q6apm_dai_align_queue(struct snd_pcm_substream *substream)
 {
 	struct snd_pcm_runtime *runtime = substream->runtime;
 	struct q6apm_dai_rtd *prtd = runtime->private_data;
-	int i, ret = 0, avail_periods;
+	snd_pcm_uframes_t hw_ptr = runtime->status->hw_ptr;
+	snd_pcm_uframes_t ahead;
 
-	if (q6apm_is_graph_in_push_pull_mode(prtd->graph))
-		return 0;
+	ahead = (prtd->queue_ptr % runtime->buffer_size + runtime->buffer_size -
+		 hw_ptr % runtime->buffer_size) % runtime->buffer_size;
+	/*
+	 * The offset is 0 both for an empty and for a full queue. A running
+	 * stream with every buffer queued continues with the buffers the DSP
+	 * holds. A paused stream can have them played before its hw_ptr moves
+	 * again, so a stream that is not running queues what is written from
+	 * here on.
+	 */
+	if (!ahead && snd_pcm_running(substream) && prtd->in_flight == runtime->periods)
+		ahead = runtime->buffer_size;
+
+	prtd->queue_ptr = (hw_ptr + ahead) % runtime->boundary;
+}
+
+/* Queue the periods written and not yet queued. Called with the stream lock held. */
+static int q6apm_dai_queue_playback(struct snd_pcm_substream *substream)
+{
+	struct snd_pcm_runtime *runtime = substream->runtime;
+	struct q6apm_dai_rtd *prtd = runtime->private_data;
+	snd_pcm_sframes_t ahead, pending;
+	int i, ret, avail_periods;
 
 	/*
 	 * ALSA calls .ack whenever appl_ptr moves, also after a stop and
@@ -461,37 +500,84 @@ static int q6apm_dai_ack(struct snd_soc_component *component, struct snd_pcm_sub
 		return 0;
 	}
 
-	if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
-		snd_pcm_sframes_t pending = runtime->control->appl_ptr - prtd->queue_ptr;
-
-		/* appl_ptr wraps at the boundary before queue_ptr does. */
-		if (pending < 0)
-			pending += runtime->boundary;
-
-		/*
-		 * What is left to queue is part of what ALSA holds for
-		 * playback. It is more when queue_ptr is ahead of appl_ptr,
-		 * as after SNDRV_PCM_IOCTL_RESET, or when appl_ptr was moved
-		 * more than a ring past hw_ptr.
-		 */
-		if (pending > snd_pcm_playback_hw_avail(runtime))
-			return 0;
-
-		avail_periods = min_t(snd_pcm_sframes_t, pending / runtime->period_size,
-				      runtime->periods);
-		for (i = 0; i < avail_periods; i++) {
-			ret = q6apm_write_async(prtd->graph, prtd->pcm_count, 0, 0, NO_TIMESTAMP);
-			if (ret < 0) {
-				dev_err(component->dev, "Error queuing playback buffer %d\n", ret);
-				return ret;
-			}
-			prtd->queue_ptr += runtime->period_size;
-			if (prtd->queue_ptr >= runtime->boundary)
-				prtd->queue_ptr -= runtime->boundary;
-		}
+	/* Frames queued that hw_ptr has not passed yet, at most one ring. */
+	ahead = prtd->queue_ptr - runtime->status->hw_ptr;
+	if (ahead < 0)
+		ahead += runtime->boundary;
+	if (ahead > runtime->buffer_size) {
+		q6apm_dai_align_queue(substream);
+		ahead = prtd->queue_ptr - runtime->status->hw_ptr;
+		if (ahead < 0)
+			ahead += runtime->boundary;
 	}
 
-	return ret;
+	/* Nothing to queue until appl_ptr has passed queue_ptr. */
+	pending = snd_pcm_playback_hw_avail(runtime) - ahead;
+	if (pending <= 0)
+		return 0;
+
+	/* Leave the buffers the DSP still holds alone. */
+	avail_periods = min_t(snd_pcm_sframes_t, pending / runtime->period_size,
+			      (snd_pcm_sframes_t)runtime->periods -
+			      (snd_pcm_sframes_t)prtd->in_flight);
+	for (i = 0; i < avail_periods; i++) {
+		ret = q6apm_write_async(prtd->graph, prtd->pcm_count, 0, 0, NO_TIMESTAMP);
+		if (ret < 0) {
+			dev_err(prtd->dev, "Error queuing playback buffer %d\n", ret);
+			/*
+			 * The core has moved on to its next buffer. Only a
+			 * prepare brings queue_ptr back in step with it.
+			 */
+			snd_pcm_stop(substream, SNDRV_PCM_STATE_XRUN);
+			return ret;
+		}
+		prtd->in_flight++;
+		prtd->queue_ptr += runtime->period_size;
+		if (prtd->queue_ptr >= runtime->boundary)
+			prtd->queue_ptr -= runtime->boundary;
+	}
+
+	return 0;
+}
+
+static int q6apm_dai_ack(struct snd_soc_component *component, struct snd_pcm_substream *substream)
+{
+	struct snd_pcm_runtime *runtime = substream->runtime;
+	struct q6apm_dai_rtd *prtd = runtime->private_data;
+
+	if (q6apm_is_graph_in_push_pull_mode(prtd->graph))
+		return 0;
+
+	if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
+		return q6apm_dai_queue_playback(substream);
+
+	return 0;
+}
+
+/*
+ * A PCM reset, which ALSA also runs at the end of every prepare, moves
+ * hw_ptr: modulo the buffer size on a running stream, to 0 otherwise.
+ * appl_ptr follows it without a call to .ack. The buffers the DSP holds
+ * stay with it, so keep queue_ptr in step with the ring.
+ */
+static int q6apm_dai_ioctl(struct snd_soc_component *component,
+			   struct snd_pcm_substream *substream,
+			   unsigned int cmd, void *arg)
+{
+	struct snd_pcm_runtime *runtime = substream->runtime;
+	struct q6apm_dai_rtd *prtd = runtime->private_data;
+	int ret;
+
+	ret = snd_pcm_lib_ioctl(substream, cmd, arg);
+	if (ret < 0 || cmd != SNDRV_PCM_IOCTL1_RESET ||
+	    substream->stream != SNDRV_PCM_STREAM_PLAYBACK ||
+	    q6apm_is_graph_in_push_pull_mode(prtd->graph))
+		return ret;
+
+	guard(pcm_stream_lock_irq)(substream);
+	q6apm_dai_align_queue(substream);
+
+	return 0;
 }
 
 static int q6apm_dai_trigger(struct snd_soc_component *component,
@@ -547,6 +633,7 @@ static int q6apm_dai_open(struct snd_soc_component *component,
 		return -ENOMEM;
 
 	spin_lock_init(&prtd->lock);
+	prtd->dev = dev;
 	prtd->substream = substream;
 	prtd->graph = q6apm_graph_open(dev, event_handler, prtd, graph_id, substream->stream);
 	if (IS_ERR(prtd->graph)) {
@@ -1229,6 +1316,7 @@ static const struct snd_soc_component_driver q6apm_fe_dai_component = {
 	.pointer	= q6apm_dai_pointer,
 	.trigger	= q6apm_dai_trigger,
 	.ack		= q6apm_dai_ack,
+	.ioctl		= q6apm_dai_ioctl,
 	.compress_ops	= &q6apm_dai_compress_ops,
 	.use_dai_pcm_id = true,
 	.remove_order   = SND_SOC_COMP_ORDER_EARLY,
-- 
2.56.0


^ permalink raw reply related	[flat|nested] 4+ messages in thread

* Re: [PATCH 2/2] ASoC: qcom: q6apm-dai: keep a playback stream running across a PCM reset
  2026-11-20 19:47 ` [PATCH 2/2] ASoC: qcom: q6apm-dai: keep a playback stream running across a PCM reset Perry Dale
@ 2026-11-20 20:17   ` sashiko-bot
  0 siblings, 0 replies; 4+ messages in thread
From: sashiko-bot @ 2026-11-20 20:17 UTC (permalink / raw)
  To: Perry Dale; +Cc: linux-sound

Thank you for your contribution! Sashiko AI review found 3 potential
issues to consider:
- [High] ASoC: qcom: q6apm-dai: queue_ptr desync and stall on
  non-running reset
- [Medium] ASoC: qcom: q6apm-dai: reset lock-gap race in
  q6apm_dai_ioctl()
- [Medium] ASoC: qcom: q6apm-dai: snd_pcm_stop() while paused skips stop
  trigger
--

commit 76e6dbc3559b40e7770b67301512b5eb160e3c6d
Author: Perry Dale <techluddite@outlook.com>

ASoC: qcom: q6apm-dai: keep a playback stream running across a PCM reset

This commit tracks the number of playback buffers in flight with the DSP
and realigns queue_ptr against hw_ptr on SNDRV_PCM_IOCTL1_RESET and in a
shared queueing helper called from .ack and APM_CLIENT_EVENT_DATA_WRITE_DONE.

> diff --git a/sound/soc/qcom/qdsp6/q6apm-dai.c b/sound/soc/qcom/qdsp6/q6apm-dai.c
> index d0d19fc38e8be..e4a6817ee17bb 100644
> --- a/sound/soc/qcom/qdsp6/q6apm-dai.c
> +++ b/sound/soc/qcom/qdsp6/q6apm-dai.c
[ ... ]
> @@ -433,14 +447,39 @@ static int q6apm_dai_prepare(struct snd_soc_component *component,
>  	return 0;
>  }
>  
> -static int q6apm_dai_ack(struct snd_soc_component *component, struct snd_pcm_substream *substream)
> +/*
> + * Give queue_ptr its ring offset back relative to hw_ptr. Called with the
> + * stream lock held after hw_ptr moved without it.
> + */
> +static void q6apm_dai_align_queue(struct snd_pcm_substream *substream)
>  {
>  	struct snd_pcm_runtime *runtime = substream->runtime;
>  	struct q6apm_dai_rtd *prtd = runtime->private_data;
> -	int i, ret = 0, avail_periods;
> +	snd_pcm_uframes_t hw_ptr = runtime->status->hw_ptr;
> +	snd_pcm_uframes_t ahead;
>  
> -	if (q6apm_is_graph_in_push_pull_mode(prtd->graph))
> -		return 0;
> +	ahead = (prtd->queue_ptr % runtime->buffer_size + runtime->buffer_size -
> +		 hw_ptr % runtime->buffer_size) % runtime->buffer_size;
> +	/*
> +	 * The offset is 0 both for an empty and for a full queue. A running
> +	 * stream with every buffer queued continues with the buffers the DSP
> +	 * holds. A paused stream can have them played before its hw_ptr moves
> +	 * again, so a stream that is not running queues what is written from
> +	 * here on.
> +	 */
> +	if (!ahead && snd_pcm_running(substream) && prtd->in_flight == runtime->periods)
> +		ahead = runtime->buffer_size;
> +
> +	prtd->queue_ptr = (hw_ptr + ahead) % runtime->boundary;
> +}

[Severity: High]
Can q6apm_dai_align_queue() desynchronize prtd->queue_ptr from
prtd->in_flight and stall playback when SNDRV_PCM_IOCTL_RESET runs on a
prepared or paused stream?

Because q6apm_dai_prepare() starts the DSP graph immediately and
q6apm_dai_trigger() does not stop or pause the graph, the DSP continues
consuming queued periods while !snd_pcm_running(substream).  In that state
event_handler() decrements prtd->in_flight down to 0, while
snd_pcm_period_elapsed_under_stream_lock() leaves runtime->status->hw_ptr
unchanged.

If the application queues k periods (0 < k < runtime->periods) while
prepared or paused, waits for the DSP to finish them (prtd->in_flight == 0),
and calls SNDRV_PCM_IOCTL_RESET, snd_pcm_lib_ioctl_reset() sets
runtime->status->hw_ptr = 0 and appl_ptr = 0.  q6apm_dai_align_queue()
then computes ahead = k * runtime->period_size and leaves prtd->queue_ptr
at k * runtime->period_size even though no buffers are in flight.  If the
application writes m <= k periods and starts or unpauses the stream,
q6apm_dai_queue_playback() sees pending = (m - k) * runtime->period_size <= 0
and queues nothing, stalling playback permanently with prtd->in_flight == 0.

Conversely, if a full ring is queued while prepared (prtd->in_flight ==
runtime->periods, prtd->queue_ptr == runtime->buffer_size) and reset before
start, q6apm_dai_align_queue() sets prtd->queue_ptr = 0.  If the application
rewrites K < runtime->periods periods and starts the stream, .ack cannot
queue anything while prtd->in_flight == runtime->periods.  When the first
in-flight buffer finishes, snd_pcm_period_elapsed_under_stream_lock()
advances runtime->status->hw_ptr from 0 to runtime->period_size before
calling q6apm_dai_queue_playback().  With prtd->queue_ptr (0) now behind
runtime->status->hw_ptr (runtime->period_size), ahead wraps past
runtime->buffer_size and q6apm_dai_align_queue() sets prtd->queue_ptr to
runtime->buffer_size (past appl_ptr = K * runtime->period_size), so the K
rewritten periods are never queued.

>  
> +/* Queue the periods written and not yet queued. Called with the stream lock held. */
> +static int q6apm_dai_queue_playback(struct snd_pcm_substream *substream)
> +{
> +	struct snd_pcm_runtime *runtime = substream->runtime;
> +	struct q6apm_dai_rtd *prtd = runtime->private_data;
> +	snd_pcm_sframes_t ahead, pending;
> +	int i, ret, avail_periods;
> +
>  	/*
>  	 * ALSA calls .ack whenever appl_ptr moves, also after a stop and
[ ... ]
> @@ -461,37 +500,84 @@ static int q6apm_dai_ack(struct snd_soc_component *component, struct snd_pcm_sub
>  		return 0;
>  	}
>  
> -	if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
[ ... ]
> +	for (i = 0; i < avail_periods; i++) {
> +		ret = q6apm_write_async(prtd->graph, prtd->pcm_count, 0, 0, NO_TIMESTAMP);
> +		if (ret < 0) {
> +			dev_err(prtd->dev, "Error queuing playback buffer %d\n", ret);
> +			/*
> +			 * The core has moved on to its next buffer. Only a
> +			 * prepare brings queue_ptr back in step with it.
> +			 */
> +			snd_pcm_stop(substream, SNDRV_PCM_STATE_XRUN);
> +			return ret;
>  		}

[Severity: Medium]
What happens when snd_pcm_stop(substream, SNDRV_PCM_STATE_XRUN) is called
here while runtime->state is SNDRV_PCM_STATE_PREPARED or
SNDRV_PCM_STATE_PAUSED?

q6apm_dai_queue_playback() runs in both states, where
snd_pcm_running(substream) is false.  In sound/core/pcm_native.c,
snd_pcm_do_stop() only invokes the SNDRV_PCM_TRIGGER_STOP callback when
snd_pcm_running(substream) is true, while snd_pcm_post_stop()
unconditionally sets runtime->state to SNDRV_PCM_STATE_XRUN.  That leaves
prtd->state as Q6APM_STREAM_RUNNING, and when called from
SNDRV_PCM_STATE_PAUSED it also causes later snd_pcm_prepare() and
snd_pcm_drop() calls to miss their runtime->state == SNDRV_PCM_STATE_PAUSED
check and skip snd_pcm_pause(substream, false), leaking fe_pause and
be_pause on the DPCM runtimes.

[ ... ]

> +/*
> + * A PCM reset, which ALSA also runs at the end of every prepare, moves
> + * hw_ptr: modulo the buffer size on a running stream, to 0 otherwise.
> + * appl_ptr follows it without a call to .ack. The buffers the DSP holds
> + * stay with it, so keep queue_ptr in step with the ring.
> + */
> +static int q6apm_dai_ioctl(struct snd_soc_component *component,
> +			   struct snd_pcm_substream *substream,
> +			   unsigned int cmd, void *arg)
> +{
> +	struct snd_pcm_runtime *runtime = substream->runtime;
> +	struct q6apm_dai_rtd *prtd = runtime->private_data;
> +	int ret;
> +
> +	ret = snd_pcm_lib_ioctl(substream, cmd, arg);
> +	if (ret < 0 || cmd != SNDRV_PCM_IOCTL1_RESET ||
> +	    substream->stream != SNDRV_PCM_STREAM_PLAYBACK ||
> +	    q6apm_is_graph_in_push_pull_mode(prtd->graph))
> +		return ret;
> +
> +	guard(pcm_stream_lock_irq)(substream);
> +	q6apm_dai_align_queue(substream);
> +
> +	return 0;
>  }

[Severity: Medium]
Can an APM_CLIENT_EVENT_DATA_WRITE_DONE event race with the unlocked windows
around q6apm_dai_ioctl() during SNDRV_PCM_IOCTL_RESET?

During snd_pcm_reset(), the PCM stream lock is acquired and dropped four
separate times across snd_pcm_lib_ioctl_reset(), q6apm_dai_ioctl(),
snd_pcm_do_reset(), and snd_pcm_post_reset():

CPU0                                    CPU1 (kworker)
snd_pcm_reset()
  snd_pcm_do_reset()
    snd_pcm_lib_ioctl_reset()
      lock(pcm_stream_lock)
      runtime->status->hw_ptr %=
        runtime->buffer_size;
      unlock(pcm_stream_lock)
    q6apm_dai_ioctl()
      lock(pcm_stream_lock)
      q6apm_dai_align_queue()
      unlock(pcm_stream_lock)
                                        event_handler()
                                          lock(pcm_stream_lock)
                                          prtd->in_flight--;
                                          snd_pcm_period_elapsed_...()
                                          q6apm_dai_queue_playback()
                                            pending = hw_avail - ahead;
                                            q6apm_write_async()
                                            /* queues stale periods */
                                          unlock(pcm_stream_lock)
    lock(pcm_stream_lock)
    runtime->hw_ptr_base = 0;
    unlock(pcm_stream_lock)
  snd_pcm_post_reset()
    lock(pcm_stream_lock)
    runtime->control->appl_ptr =
      runtime->status->hw_ptr;
    unlock(pcm_stream_lock)

If event_handler() runs on a stream in its second lap before
snd_pcm_post_reset() updates runtime->control->appl_ptr,
runtime->status->hw_ptr and prtd->queue_ptr have already been reduced into
the first ring lap while runtime->control->appl_ptr still holds its
pre-reset value.  snd_pcm_playback_hw_avail(runtime) can then evaluate to up
to runtime->buffer_size instead of 0, causing q6apm_dai_queue_playback() to
queue stale DMA periods to the DSP and advance prtd->queue_ptr ahead of the
appl_ptr that snd_pcm_post_reset() is about to set.

Similarly, if event_handler() runs between snd_pcm_lib_ioctl_reset() and
snd_pcm_do_reset(), runtime->status->hw_ptr has already been reduced while
runtime->hw_ptr_base is still >= runtime->buffer_size, so
snd_pcm_update_hw_ptr0() sees a delta >= runtime->buffer_size and either
drops the hw_ptr update after prtd->in_flight was decremented or restores
runtime->status->hw_ptr to >= runtime->buffer_size right before
snd_pcm_do_reset() zeroes runtime->hw_ptr_base.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/LV5PR84MB40268B01B9F458296BE453ACA7782@LV5PR84MB4026.NAMPRD84.PROD.OUTLOOK.COM?part=2

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2026-11-20 20:17 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-20 19:47 [PATCH 0/2] ASoC: qcom: q6apm-dai: stop the playback queue from wedging the DSP Perry Dale
2026-11-20 19:47 ` [PATCH 1/2] ASoC: qcom: q6apm-dai: queue playback buffers only while the graph runs Perry Dale
2026-11-20 19:47 ` [PATCH 2/2] ASoC: qcom: q6apm-dai: keep a playback stream running across a PCM reset Perry Dale
2026-11-20 20:17   ` sashiko-bot

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox