Skip to contentSkip to conversation

ASoC: qcom: qdsp6: handle SMD FIFO -EAGAIN in q6asm playback

PATCH Superseded Vladimir Bely 3 participants 13 messages Nov 29 – Nov 30

Subsystems (5)
lore
Share & tools
lore
#0 lore raw

cover ASoC: qcom: qdsp6: handle SMD FIFO -EAGAIN in q6asm playback

On older QDSP6 SoCs with small SMD FIFOs (such as MSM8916), queuing a burst of playback periods in q6asm_dai_ack() via rpmsg_trysend() can temporarily fill the FIFO and return -EAGAIN, aborting playback right at start.

This 2-patch series fixes both sides of -EAGAIN handling:

- Patch 1 keeps ac->lock held across apr_send_pkt() in q6asm_write_async() and q6asm_read() and only advances port->dsp_buf when the packet is queued, so a failed send does not leave dsp_buf out of sync on retry.
- Patch 2 introduces a locked q6asm_dai_queue_playback() helper in q6asm-dai that stops cleanly on -EAGAIN (also handling runtime->boundary wrap-around and initializing prtd->lock on PCM open) and retries when the DSP completes a buffer (DATA_WRITE_DONE).

Tested on MSM8916 (Samsung Galaxy A3, SM-A300F).

Vladimir Bely (2):
ASoC: qcom: q6asm: don't advance dsp_buf on failed write or read
ASoC: qcom: q6asm-dai: retry playback queuing on -EAGAIN

 sound/soc/qcom/qdsp6/q6asm-dai.c | 58 +++++++++++++++++++++++++-------
 sound/soc/qcom/qdsp6/q6asm.c     | 30 ++++++++---------
 2 files changed, 61 insertions(+), 27 deletions(-)

base-commit: 626d2233d48884f22c16011a6e44f905824bd8e0

Signature · 1 line
2.43.0
#1 lore raw

PATCH 1/2 ASoC: qcom: q6asm: don't advance dsp_buf on failed write or read

Both q6asm_write_async() and q6asm_read() increment port->dsp_buf and release ac->lock before calling apr_send_pkt(). If apr_send_pkt() fails (for instance with -EAGAIN when the SMD FIFO is full), port->dsp_buf has already advanced past the unsent period, leaving subsequent retries permanently off by one period in the ring buffer.

apr_send_pkt() is already atomic-safe (it only takes the leaf adev->svc.lock around rpmsg_trysend()). Keep ac->lock held across apr_send_pkt() and advance port->dsp_buf only when the packet is successfully queued.

Fixes 68fd8480bb7b ("ASoC: qdsp6: q6asm: Add support to audio stream apis")
---
 sound/soc/qcom/qdsp6/q6asm.c | 30 +++++++++++++++---------------
 1 file changed, 15 insertions(+), 15 deletions(-)
sound/soc/qcom/qdsp6/q6asm.c +15 -15
index ee1930e41..d4c1cff3f 100644
@@ -1552,17 +1552,17 @@ int q6asm_read(struct audio_client *ac, uint32_t stream_id)
 	read->seq_id = port->dsp_buf;
 	pkt->hdr.token = port->dsp_buf;
 
-	port->dsp_buf++;
-
-	if (port->dsp_buf >= port->num_periods)
-		port->dsp_buf = 0;
-
-	spin_unlock_irqrestore(&ac->lock, flags);
 	rc = apr_send_pkt(ac->adev, pkt);
-	if (rc == pkt_size)
+	if (rc == pkt_size) {
 		rc = 0;
-	else
+		port->dsp_buf++;
+		if (port->dsp_buf >= port->num_periods)
+			port->dsp_buf = 0;
+	} else {
 		pr_err("read op[0x%x]rc[%d]\n", pkt->hdr.opcode, rc);
+	}
+
+	spin_unlock_irqrestore(&ac->lock, flags);
 
 	return rc;
 }
@@ -1673,15 +1673,15 @@ int q6asm_write_async(struct audio_client *ac, uint32_t stream_id, uint32_t len,
 
 	write->flags = wflags;
 
-	port->dsp_buf++;
-
-	if (port->dsp_buf >= port->num_periods)
-		port->dsp_buf = 0;
-
-	spin_unlock_irqrestore(&ac->lock, flags);
 	rc = apr_send_pkt(ac->adev, pkt);
-	if (rc == pkt_size)
+	if (rc == pkt_size) {
 		rc = 0;
+		port->dsp_buf++;
+		if (port->dsp_buf >= port->num_periods)
+			port->dsp_buf = 0;
+	}
+
+	spin_unlock_irqrestore(&ac->lock, flags);
 
 	return rc;
 }
Signature · 1 line
2.43.0
#2 lore raw

PATCH 2/2 ASoC: qcom: q6asm-dai: retry playback queuing on -EAGAIN

In reply to Vladimir Bely

q6asm_dai_ack() tries to queue every available playback period to the DSP in a single burst via q6asm_write_async(). Each period sends an APR packet using rpmsg_trysend(), which can fill the small SMD FIFO on older SoCs such as MSM8916 and return -EAGAIN. Currently q6asm_dai_ack() treats -EAGAIN as a fatal error and aborts playback right at start.

Extract a locked helper q6asm_dai_queue_playback() (initializing prtd->lock in q6asm_dai_open(), which was previously only initialized in q6asm_dai_compr_open()) that stops cleanly on -EAGAIN without advancing queue_ptr, and call it again from ASM_CLIENT_EVENT_DATA_WRITE_DONE once the DSP hands a buffer back. Also wrap queue_ptr against runtime->boundary so long-running playback does not underflow.

Tested on MSM8916 (Samsung Galaxy A3, SM-A300F).

Fixes e386c2cf3df2 ("ASoC: qcom: q6asm-dai: schedule all available frames to avoid dsp under-runs")
---
 sound/soc/qcom/qdsp6/q6asm-dai.c | 58 +++++++++++++++++++++++++-------
 1 file changed, 46 insertions(+), 12 deletions(-)
sound/soc/qcom/qdsp6/q6asm-dai.c +46 -12
index 57cbce2bb..a21b93c3e 100644
@@ -179,6 +179,42 @@ static const struct snd_compr_codec_caps q6asm_compr_caps = {
 	.descriptor[0].formats = 0,
 };
 
+/*
+ * Queue the periods the application has written so far. Each period is one
+ * APR packet sent with rpmsg_trysend(), and a burst can fill the small SMD
+ * FIFO (-EAGAIN). That is not an error: stop and retry when the DSP hands a
+ * buffer back (DATA_WRITE_DONE).
+ */
+static int q6asm_dai_queue_playback(struct q6asm_dai_rtd *prtd)
+{
+	struct snd_pcm_runtime *runtime = prtd->substream->runtime;
+	int ret;
+
+	guard(spinlock_irqsave)(&prtd->lock);
+
+	while (prtd->state == Q6ASM_STREAM_RUNNING) {
+		snd_pcm_sframes_t avail = runtime->control->appl_ptr - prtd->queue_ptr;
+
+		if (avail < 0)
+			avail += runtime->boundary;
+		if (avail < runtime->period_size)
+			break;
+
+		ret = q6asm_write_async(prtd->audio_client, prtd->stream_id,
+					prtd->pcm_count, 0, 0, 0);
+		if (ret == -EAGAIN)
+			break;
+		if (ret < 0)
+			return ret;
+
+		prtd->queue_ptr += runtime->period_size;
+		if (prtd->queue_ptr >= runtime->boundary)
+			prtd->queue_ptr -= runtime->boundary;
+	}
+
+	return 0;
+}
+
 static void event_handler(uint32_t opcode, uint32_t token,
 			  void *payload, void *priv)
 {
@@ -192,6 +228,9 @@ static void event_handler(uint32_t opcode, uint32_t token,
 		break;
 	case ASM_CLIENT_EVENT_DATA_WRITE_DONE:
 		snd_pcm_period_elapsed(substream);
+		/* Push the periods left behind by a full SMD FIFO */
+		if (q6asm_dai_queue_playback(prtd) < 0)
+			pr_err_ratelimited("q6asm-dai: Error queuing playback buffer\n");
 		break;
 	case ASM_CLIENT_EVENT_DATA_READ_DONE:
 		snd_pcm_period_elapsed(substream);
@@ -321,19 +360,13 @@ static int q6asm_dai_ack(struct snd_soc_component *component, struct snd_pcm_sub
 {
 	struct snd_pcm_runtime *runtime = substream->runtime;
 	struct q6asm_dai_rtd *prtd = runtime->private_data;
-	int i, ret = 0, avail_periods;
-
-	if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK && prtd->state == Q6ASM_STREAM_RUNNING) {
-		avail_periods = (runtime->control->appl_ptr - prtd->queue_ptr)/runtime->period_size;
-		for (i = 0; i < avail_periods; i++) {
-			ret = q6asm_write_async(prtd->audio_client, prtd->stream_id,
-					   prtd->pcm_count, 0, 0, 0);
+	int ret = 0;
 
-			if (ret < 0) {
-				dev_err(component->dev, "Error queuing playback buffer %d\n", ret);
-				return ret;
-			}
-			prtd->queue_ptr += runtime->period_size;
+	if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
+		ret = q6asm_dai_queue_playback(prtd);
+		if (ret < 0) {
+			dev_err(component->dev, "Error queuing playback buffer %d\n", ret);
+			return ret;
 		}
 	}
 
@@ -395,6 +428,7 @@ static int q6asm_dai_open(struct snd_soc_component *component,
 	if (prtd == NULL)
 		return -ENOMEM;
 
+	spin_lock_init(&prtd->lock);
 	prtd->substream = substream;
 	prtd->audio_client = q6asm_audio_client_alloc(dev,
 				(q6asm_cb)event_handler, prtd, stream_id,
Signature · 1 line
2.43.0
#3 lore raw

Re: [PATCH 2/2] ASoC: qcom: q6asm-dai: retry playback queuing on -EAGAIN

Thank you for your contribution! Sashiko AI review found 2 potential issues to consider: - [High] ASoC: qcom: q6asm-dai: race and stale state in q6asm_dai_queue_playback() - [High] ASoC: qcom…

Thank you for your contribution! Sashiko AI review found 2 potential issues to consider:
- [High] ASoC: qcom: q6asm-dai: race and stale state in q6asm_dai_queue_playback()
- [High] ASoC: qcom: q6asm-dai: unbounded avail wrap in q6asm_dai_queue_playback()
--

commit 18d37e91d4a0825eb46a5723e49ff2c41cb6815a
Author: Vladimir Bely <[email protected]>

ASoC: qcom: q6asm-dai: retry playback queuing on -EAGAIN

This commit extracts a locked q6asm_dai_queue_playback() helper that stops queuing playback periods on -EAGAIN and retries from the ASM_CLIENT_EVENT_DATA_WRITE_DONE callback when the DSP returns a buffer.

Quoted 18 lines
sound/soc/qcom/qdsp6/q6asm-dai.c +10 -0
index 57cbce2bb0cb7..a21b93c3e0246 100644
@@ -179,6 +179,42 @@ static const struct snd_compr_codec_caps q6asm_compr_caps = {
 	.descriptor[0].formats = 0,
 };
 
+/*
+ * Queue the periods the application has written so far. Each period is one
+ * APR packet sent with rpmsg_trysend(), and a burst can fill the small SMD
+ * FIFO (-EAGAIN). That is not an error: stop and retry when the DSP hands a
+ * buffer back (DATA_WRITE_DONE).
+ */
+static int q6asm_dai_queue_playback(struct q6asm_dai_rtd *prtd)
+{
+	struct snd_pcm_runtime *runtime = prtd->substream->runtime;
+	int ret;

sound/soc/qcom/qdsp6/q6asm-dai.c static const struct snd_compr_codec_caps q6asm_compr_caps = { · line 201

Quoted 10 lines
sound/soc/qcom/qdsp6/q6asm-dai.c +10 -0
+
+	guard(spinlock_irqsave)(&prtd->lock);
+
+	while (prtd->state == Q6ASM_STREAM_RUNNING) {
+		snd_pcm_sframes_t avail = runtime->control->appl_ptr - prtd->queue_ptr;
+
+		if (avail < 0)
+			avail += runtime->boundary;
+		if (avail < runtime->period_size)
+			break;

[Severity: High]
Can avail wrap to roughly runtime->boundary when runtime->control->appl_ptr moves behind prtd->queue_ptr?

If periods are already queued to the DSP (prtd->queue_ptr > runtime->status->hw_ptr) and userspace calls SNDRV_PCM_IOCTL_RESET, snd_pcm_post_reset() rewinds runtime->control->appl_ptr to runtime->status->hw_ptr without resetting prtd->queue_ptr. The same state occurs if q6asm_dai_ack() queues one or more periods and a later iteration fails with an error other than -EAGAIN, causing pcm_lib_apply_appl_ptr() to roll runtime->control->appl_ptr back to old_appl_ptr.

Once runtime->control->appl_ptr < prtd->queue_ptr without a boundary wrap, avail is negative and adding runtime->boundary yields a value near runtime->boundary. Because avail is not bounded by runtime->buffer_size, q6asm_dai_queue_playback() loops under spin_lock_irqsave(&prtd->lock) flooding the DSP with bogus q6asm_write_async() packets until the SMD FIFO fills with -EAGAIN, and repeats that burst on every subsequent ASM_CLIENT_EVENT_DATA_WRITE_DONE event.

Quoted 20 lines
sound/soc/qcom/qdsp6/q6asm-dai.c +16 -0
+
+		ret = q6asm_write_async(prtd->audio_client, prtd->stream_id,
+					prtd->pcm_count, 0, 0, 0);
+		if (ret == -EAGAIN)
+			break;
+		if (ret < 0)
+			return ret;
+
+		prtd->queue_ptr += runtime->period_size;
+		if (prtd->queue_ptr >= runtime->boundary)
+			prtd->queue_ptr -= runtime->boundary;
+	}
+
+	return 0;
+}
+
 static void event_handler(uint32_t opcode, uint32_t token,
 			  void *payload, void *priv)
 {

sound/soc/qcom/qdsp6/q6asm-dai.c static void event_handler(uint32_t opcode, uint32_t token, · line 234

Quoted 7 lines
sound/soc/qcom/qdsp6/q6asm-dai.c +3 -0
@@ -192,6 +228,9 @@ static void event_handler(uint32_t opcode, uint32_t token,
 		break;
 	case ASM_CLIENT_EVENT_DATA_WRITE_DONE:
 		snd_pcm_period_elapsed(substream);
+		/* Push the periods left behind by a full SMD FIFO */
+		if (q6asm_dai_queue_playback(prtd) < 0)
+			pr_err_ratelimited("q6asm-dai: Error queuing playback buffer\n");
 		break;

[Severity: High]
Can calling q6asm_dai_queue_playback() from event_handler() race with stream stop, re-prepare, or close and dereference a NULL port->buf in q6asm_write_async()?

q6asm_dai_queue_playback() runs outside pcm_stream_lock and only checks prtd->state == Q6ASM_STREAM_RUNNING under prtd->lock. However, q6asm_dai_prepare() and q6asm_dai_close() do not take prtd->lock and leave prtd->state set to Q6ASM_STREAM_RUNNING while closing the DSP stream and unmapping memory regions:

CPU0 CPU1 (APR callback)
q6asm_dai_prepare() / _close()
/* prtd->state is still RUNNING */
q6asm_cmd(..., CMD_CLOSE)
q6asm_unmap_memory_regions()

    kfree(port->buf)
    port->buf = NULL
                                      event_handler(DATA_WRITE_DONE)
                                        q6asm_dai_queue_playback(prtd)
                                          q6asm_write_async()
                                            ab = &port->buf[port->dsp_buf]
                                            write->buf_addr_lsw =
                                              lower_32_bits(ab->phys);
                                              <-- NULL pointer dereference

Even without a concurrent prepare or close, SNDRV_PCM_TRIGGER_STOP in q6asm_dai_trigger() sends CMD_EOS without changing prtd->state from Q6ASM_STREAM_RUNNING, and XRUN inside snd_pcm_period_elapsed() stops the ALSA stream while leaving runtime->control->appl_ptr > prtd->queue_ptr if an earlier burst hit -EAGAIN. In both cases, the next ASM_CLIENT_EVENT_DATA_WRITE_DONE event queues new write buffers to a stopped stream. In addition, q6asm_dai_prepare() sets prtd->state = Q6ASM_STREAM_RUNNING with prtd->queue_ptr = 0 before snd_pcm_post_prepare() resets runtime->control->appl_ptr to 0, leaving a window where q6asm_dai_queue_playback() can queue against the stale appl_ptr.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2
#4 lore raw

v2 cover ASoC: qcom: qdsp6: handle SMD FIFO -EAGAIN in q6asm playback

In reply to Vladimir Bely

On SoCs with a small SMD FIFO (such as MSM8916), a playback burst in q6asm_dai_ack() can fill the FIFO so rpmsg_trysend() returns -EAGAIN. Today q6asm_write_async() still advances port->dsp_buf on that failed send, and q6asm_dai_ack() fails the write with -EAGAIN.

Patch 1 keeps port->dsp_buf unchanged when apr_send_pkt() fails and protects port->buf teardown under ac->lock. Patch 2 stops queuing on -EAGAIN and retries on ASM_CLIENT_EVENT_DATA_WRITE_DONE under the PCM stream lock while the stream is running.

Changes in v2 (addressing Sashiko AI review):
- Patch 1: clear port->buf and port->dsp_buf under ac->lock in q6asm_audio_client_free_buf() and check !port->buf || !port->num_periods in q6asm_write_async() and q6asm_read().
- Patch 2: bound avail <= runtime->buffer_size so a rewind/reset of appl_ptr behind queue_ptr cannot wrap avail to ~runtime->boundary; serialize event_handler() with ack/stop/XRUN/prepare/close via pcm_stream_lock_irqsave and check snd_pcm_running(substream) instead of using prtd->lock.

Vladimir Bely (2):
ASoC: qcom: q6asm: don't advance dsp_buf on failed write or read
ASoC: qcom: q6asm-dai: retry playback queuing on -EAGAIN

 sound/soc/qcom/qdsp6/q6asm-dai.c | 61 +++++++++++++++++++++++++-------
 sound/soc/qcom/qdsp6/q6asm.c     | 44 ++++++++++++++---------
 2 files changed, 75 insertions(+), 30 deletions(-)
Signature · 1 line
2.43.0
#5 lore raw

v3 cover ASoC: qcom: qdsp6: handle SMD FIFO -EAGAIN in q6asm playback

On SoCs with a small SMD FIFO (such as MSM8916), a playback burst in q6asm_dai_ack() can fill the FIFO so rpmsg_trysend() returns -EAGAIN. Today q6asm_write_async() still advances port->dsp_buf on that failed send, and q6asm_dai_ack() fails the write with -EAGAIN.

Patch 1 keeps port->dsp_buf unchanged when apr_send_pkt() fails and protects port->buf teardown under ac->lock. Patch 2 defers queuing until trigger start, stops cleanly on -EAGAIN, and retries on CMD_RUN_DONE and DATA_WRITE_DONE under the PCM stream lock while the stream is running.

Changes in v3 (addressing Sashiko AI review):
- Patch 1: fix Fixes: trailer subjects and add Fixes: fdbb53d318aa.
- Patch 2:
- Defer playback queuing in q6asm_dai_ack() until snd_pcm_running(substream) and queue after q6asm_run_nowait() in q6asm_dai_trigger() (with CMD_RUN_DONE retry) so a full SMD FIFO before start cannot fail SNDRV_PCM_TRIGGER_START with -EAGAIN.
- Combine snd_pcm_period_elapsed_under_stream_lock() and q6asm_dai_queue_playback() under a single pcm_stream_lock_irqsave.
- Fix the Fixes: trailer subject.

Changes in v2:
- Patch 1: clear port->buf and port->dsp_buf under ac->lock in q6asm_audio_client_free_buf() and check !port->buf || !port->num_periods in q6asm_write_async() and q6asm_read().
- Patch 2: bound avail <= runtime->buffer_size; serialize event_handler() with ack/stop/XRUN/prepare/close via pcm_stream_lock_irqsave and snd_pcm_running(substream).

Vladimir Bely (2):
ASoC: qcom: q6asm: don't advance dsp_buf on failed write or read
ASoC: qcom: q6asm-dai: retry playback queuing on -EAGAIN

 sound/soc/qcom/qdsp6/q6asm-dai.c | 75 ++++++++++++++++++++++++++------
 sound/soc/qcom/qdsp6/q6asm.c     | 44 +++++++++++--------
 2 files changed, 88 insertions(+), 31 deletions(-)

base-commit: 626d2233d48884f22c16011a6e44f905824bd8e0

Signature · 1 line
2.43.0
#6 lore raw
Quoted 11 lines · Vladimir Bely

On SoCs with a small SMD FIFO (such as MSM8916), a playback burst in q6asm_dai_ack() can fill the FIFO so rpmsg_trysend() returns -EAGAIN. Today q6asm_write_async() still advances port->dsp_buf on that failed send, and q6asm_dai_ack() fails the write with -EAGAIN.

Patch 1 keeps port->dsp_buf unchanged when apr_send_pkt() fails and protects port->buf teardown under ac->lock. Patch 2 defers queuing until trigger start, stops cleanly on -EAGAIN, and retries on CMD_RUN_DONE and DATA_WRITE_DONE under the PCM stream lock while the stream is running.

Quoted 10 lines

Changes in v3 (addressing Sashiko AI review):
- Patch 1: fix Fixes: trailer subjects and add Fixes: fdbb53d318aa.
- Patch 2:
- Defer playback queuing in q6asm_dai_ack() until snd_pcm_running(substream) and queue after q6asm_run_nowait() in q6asm_dai_trigger() (with CMD_RUN_DONE retry) so a full SMD FIFO before start cannot fail SNDRV_PCM_TRIGGER_START with -EAGAIN.
- Combine snd_pcm_period_elapsed_under_stream_lock() and q6asm_dai_queue_playback() under a single pcm_stream_lock_irqsave.
- Fix the Fixes: trailer subject.

Friendly note: Please don't send new versions of a patchset in reply to the previous ones, that confuses people and tooling. Please also limit the frequency (1 a day is a good rule of thumb), as all the maintainers are under a flurry of emails anyway

Konrad

#7 lore raw

PATCH v3 1/2 ASoC: qcom: q6asm: don't advance dsp_buf on failed write or read

In reply to Vladimir Bely

q6asm_write_async() and q6asm_read() increment port->dsp_buf under ac->lock and release the lock before calling apr_send_pkt(). If apr_send_pkt() fails (for example with -EAGAIN when the SMD FIFO is full during a playback write burst on MSM8916), dsp_buf has already moved on even though the DSP never received that buffer. Subsequent writes then send out-of-sequence period addresses and tokens, and any retry skips the failed period.

Hold ac->lock across apr_send_pkt() (which is atomic and non-blocking via rpmsg_trysend()) and only advance port->dsp_buf when the packet was actually accepted. Also clear port->buf and port->dsp_buf under ac->lock in q6asm_audio_client_free_buf() (freeing the saved pointer after unlock) and reject q6asm_write_async()/q6asm_read() with -EINVAL when !port->buf or !port->num_periods, preventing races with concurrent unmap.

Fixes 68fd8480bb7b ("ASoC: qdsp6: q6asm: Add support to audio stream apis")
Fixes fdbb53d318aa ("ASoC: qdsp6: q6asm: do not sleep while atomic")

---
Changes in v3:
- Fix Fixes: trailer subject for 68fd8480bb7b and add Fixes: fdbb53d318aa (reported by Sashiko AI review).

Changes in v2:
- Clear port->buf and port->dsp_buf under ac->lock in q6asm_audio_client_free_buf() and reject q6asm_write_async()/q6asm_read() when !port->buf or !port->num_periods.

 sound/soc/qcom/qdsp6/q6asm.c | 44 ++++++++++++++++++++++--------------
 1 file changed, 27 insertions(+), 17 deletions(-)
sound/soc/qcom/qdsp6/q6asm.c +27 -17
index ee1930e41..54408fd64 100644
@@ -427,13 +427,16 @@ static int __q6asm_memory_unmap(struct audio_client *ac,
 static void q6asm_audio_client_free_buf(struct audio_client *ac,
 					struct audio_port_data *port)
 {
+	struct audio_buffer *buf;
 	unsigned long flags;
 
 	spin_lock_irqsave(&ac->lock, flags);
 	port->num_periods = 0;
-	spin_unlock_irqrestore(&ac->lock, flags);
-	kfree(port->buf);
+	port->dsp_buf = 0;
+	buf = port->buf;
 	port->buf = NULL;
+	spin_unlock_irqrestore(&ac->lock, flags);
+	kfree(buf);
 }
 
 /**
@@ -1541,6 +1544,10 @@ int q6asm_read(struct audio_client *ac, uint32_t stream_id)
 
 	spin_lock_irqsave(&ac->lock, flags);
 	port = &ac->port[SNDRV_PCM_STREAM_CAPTURE];
+	if (!port->buf || !port->num_periods) {
+		spin_unlock_irqrestore(&ac->lock, flags);
+		return -EINVAL;
+	}
 	q6asm_add_hdr(ac, &pkt->hdr, pkt_size, false, stream_id);
 	ab = &port->buf[port->dsp_buf];
 	pkt->hdr.opcode = ASM_DATA_CMD_READ_V2;
@@ -1552,16 +1559,16 @@ int q6asm_read(struct audio_client *ac, uint32_t stream_id)
 	read->seq_id = port->dsp_buf;
 	pkt->hdr.token = port->dsp_buf;
 
-	port->dsp_buf++;
-
-	if (port->dsp_buf >= port->num_periods)
-		port->dsp_buf = 0;
-
-	spin_unlock_irqrestore(&ac->lock, flags);
 	rc = apr_send_pkt(ac->adev, pkt);
-	if (rc == pkt_size)
+	if (rc == pkt_size) {
 		rc = 0;
-	else
+		port->dsp_buf++;
+		if (port->dsp_buf >= port->num_periods)
+			port->dsp_buf = 0;
+	}
+	spin_unlock_irqrestore(&ac->lock, flags);
+
+	if (rc)
 		pr_err("read op[0x%x]rc[%d]\n", pkt->hdr.opcode, rc);
 
 	return rc;
@@ -1657,6 +1664,10 @@ int q6asm_write_async(struct audio_client *ac, uint32_t stream_id, uint32_t len,
 
 	spin_lock_irqsave(&ac->lock, flags);
 	port = &ac->port[SNDRV_PCM_STREAM_PLAYBACK];
+	if (!port->buf || !port->num_periods) {
+		spin_unlock_irqrestore(&ac->lock, flags);
+		return -EINVAL;
+	}
 	q6asm_add_hdr(ac, &pkt->hdr, pkt_size, false, stream_id);
 
 	ab = &port->buf[port->dsp_buf];
@@ -1673,15 +1684,14 @@ int q6asm_write_async(struct audio_client *ac, uint32_t stream_id, uint32_t len,
 
 	write->flags = wflags;
 
-	port->dsp_buf++;
-
-	if (port->dsp_buf >= port->num_periods)
-		port->dsp_buf = 0;
-
-	spin_unlock_irqrestore(&ac->lock, flags);
 	rc = apr_send_pkt(ac->adev, pkt);
-	if (rc == pkt_size)
+	if (rc == pkt_size) {
 		rc = 0;
+		port->dsp_buf++;
+		if (port->dsp_buf >= port->num_periods)
+			port->dsp_buf = 0;
+	}
+	spin_unlock_irqrestore(&ac->lock, flags);
 
 	return rc;
 }
Signature · 1 line
2.43.0
#8 lore raw

PATCH v3 2/2 ASoC: qcom: q6asm-dai: retry playback queuing on -EAGAIN

In reply to Vladimir Bely

q6asm_dai_ack() sends one ASM_DATA_CMD_WRITE_V2 APR packet per available period in a tight loop. On SoCs with a small SMD FIFO (such as MSM8916), an initial playback write burst or a buffer with many periods can fill the FIFO, making rpmsg_trysend() return -EAGAIN. q6asm_dai_ack() currently aborts and propagates -EAGAIN to ALSA, causing userspace writes to fail and playback to stall.

Move playback period queuing into a helper q6asm_dai_queue_playback() called with the PCM stream lock held. Handle appl_ptr wrap around runtime->boundary, guard against appl_ptr moving behind queue_ptr (e.g. on SNDRV_PCM_IOCTL_RESET or appl_ptr rollback) by requiring avail <= runtime->buffer_size, and stop cleanly on -EAGAIN without returning an error.

Defer playback queuing in q6asm_dai_ack() until snd_pcm_running(substream) and queue initial periods in q6asm_dai_trigger() after q6asm_run_nowait() succeeds so a pre-start burst cannot exhaust the SMD FIFO and fail SNDRV_PCM_TRIGGER_START with -EAGAIN. Retry any remaining periods in event_handler() on ASM_CLIENT_EVENT_CMD_RUN_DONE and under a single pcm_stream_lock_irqsave critical section with snd_pcm_period_elapsed_under_stream_lock() on ASM_CLIENT_EVENT_DATA_WRITE_DONE.

Fixes e386c2cf3df2 ("ASoC: qcom: q6asm-dai: schedule all available frames to avoid dsp under-runs")

---
Changes in v3:
- Only queue in q6asm_dai_ack() when snd_pcm_running(substream), queue initial playback periods in q6asm_dai_trigger() after q6asm_run_nowait() succeeds, and retry on ASM_CLIENT_EVENT_CMD_RUN_DONE so a full SMD FIFO cannot fail SNDRV_PCM_TRIGGER_START with -EAGAIN (reported by Sashiko AI).
- Use snd_pcm_period_elapsed_under_stream_lock() inside the same pcm_stream_lock_irqsave guard in event_handler().
- Fix the Fixes: trailer subject.

Changes in v2:
- Bound avail <= runtime->buffer_size and serialize event_handler() via pcm_stream_lock_irqsave + snd_pcm_running(substream).

 sound/soc/qcom/qdsp6/q6asm-dai.c | 75 ++++++++++++++++++++++++++------
 1 file changed, 61 insertions(+), 14 deletions(-)
sound/soc/qcom/qdsp6/q6asm-dai.c +61 -14
index 57cbce2bb..b88f431de 100644
@@ -179,6 +179,40 @@ static const struct snd_compr_codec_caps q6asm_compr_caps = {
 	.descriptor[0].formats = 0,
 };
 
+/*
+ * Queue the periods the application has written so far. Each period is one
+ * APR packet sent with rpmsg_trysend(), and a burst can fill the small SMD
+ * FIFO (-EAGAIN). That is not an error: stop and retry when the DSP signals
+ * CMD_RUN_DONE or DATA_WRITE_DONE. Caller must hold the PCM stream lock.
+ */
+static int q6asm_dai_queue_playback(struct q6asm_dai_rtd *prtd)
+{
+	struct snd_pcm_runtime *runtime = prtd->substream->runtime;
+	int ret;
+
+	while (prtd->state == Q6ASM_STREAM_RUNNING) {
+		snd_pcm_sframes_t avail = runtime->control->appl_ptr - prtd->queue_ptr;
+
+		if (avail < 0)
+			avail += runtime->boundary;
+		if (avail < runtime->period_size || avail > runtime->buffer_size)
+			break;
+
+		ret = q6asm_write_async(prtd->audio_client, prtd->stream_id,
+					prtd->pcm_count, 0, 0, 0);
+		if (ret == -EAGAIN)
+			break;
+		if (ret < 0)
+			return ret;
+
+		prtd->queue_ptr += runtime->period_size;
+		if (prtd->queue_ptr >= runtime->boundary)
+			prtd->queue_ptr -= runtime->boundary;
+	}
+
+	return 0;
+}
+
 static void event_handler(uint32_t opcode, uint32_t token,
 			  void *payload, void *priv)
 {
@@ -187,11 +221,29 @@ static void event_handler(uint32_t opcode, uint32_t token,
 
 	switch (opcode) {
 	case ASM_CLIENT_EVENT_CMD_RUN_DONE:
+		if (substream &&
+		    substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
+			scoped_guard(pcm_stream_lock_irqsave, substream) {
+				if (substream->runtime &&
+				    snd_pcm_running(substream) &&
+				    q6asm_dai_queue_playback(prtd) < 0)
+					pr_err_ratelimited("q6asm-dai: Error queuing playback buffer\n");
+			}
+		}
 		break;
 	case ASM_CLIENT_EVENT_CMD_EOS_DONE:
 		break;
 	case ASM_CLIENT_EVENT_DATA_WRITE_DONE:
-		snd_pcm_period_elapsed(substream);
+		if (substream) {
+			scoped_guard(pcm_stream_lock_irqsave, substream) {
+				snd_pcm_period_elapsed_under_stream_lock(substream);
+				/* Push any periods left behind by a full SMD FIFO */
+				if (substream->runtime &&
+				    snd_pcm_running(substream) &&
+				    q6asm_dai_queue_playback(prtd) < 0)
+					pr_err_ratelimited("q6asm-dai: Error queuing playback buffer\n");
+			}
+		}
 		break;
 	case ASM_CLIENT_EVENT_DATA_READ_DONE:
 		snd_pcm_period_elapsed(substream);
@@ -321,20 +373,13 @@ static int q6asm_dai_ack(struct snd_soc_component *component, struct snd_pcm_sub
 {
 	struct snd_pcm_runtime *runtime = substream->runtime;
 	struct q6asm_dai_rtd *prtd = runtime->private_data;
-	int i, ret = 0, avail_periods;
-
-	if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK && prtd->state == Q6ASM_STREAM_RUNNING) {
-		avail_periods = (runtime->control->appl_ptr - prtd->queue_ptr)/runtime->period_size;
-		for (i = 0; i < avail_periods; i++) {
-			ret = q6asm_write_async(prtd->audio_client, prtd->stream_id,
-					   prtd->pcm_count, 0, 0, 0);
+	int ret = 0;
 
-			if (ret < 0) {
-				dev_err(component->dev, "Error queuing playback buffer %d\n", ret);
-				return ret;
-			}
-			prtd->queue_ptr += runtime->period_size;
-		}
+	if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
+	    snd_pcm_running(substream)) {
+		ret = q6asm_dai_queue_playback(prtd);
+		if (ret < 0)
+			dev_err(component->dev, "Error queuing playback buffer %d\n", ret);
 	}
 
 	return ret;
@@ -353,6 +398,8 @@ static int q6asm_dai_trigger(struct snd_soc_component *component,
 	case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
 		ret = q6asm_run_nowait(prtd->audio_client, prtd->stream_id,
 				       0, 0, 0);
+		if (!ret && substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
+			ret = q6asm_dai_queue_playback(prtd);
 		break;
 	case SNDRV_PCM_TRIGGER_STOP:
 		ret = q6asm_cmd_nowait(prtd->audio_client, prtd->stream_id,
Signature · 1 line
2.43.0
#9 lore raw

PATCH v2 1/2 ASoC: qcom: q6asm: don't advance dsp_buf on failed write or read

In reply to Vladimir Bely

q6asm_write_async() and q6asm_read() increment port->dsp_buf under ac->lock and release the lock before calling apr_send_pkt(). If apr_send_pkt() fails (for example with -EAGAIN when the SMD FIFO is full during a playback write burst on MSM8916), dsp_buf has already moved on even though the DSP never received that buffer. Subsequent writes then send out-of-sequence period addresses and tokens, and any retry skips the failed period.

Hold ac->lock across apr_send_pkt() (which is atomic and non-blocking via rpmsg_trysend()) and only advance port->dsp_buf when the packet was actually accepted. Also clear port->buf and port->dsp_buf under ac->lock in q6asm_audio_client_free_buf() (freeing the saved pointer after unlock) and reject q6asm_write_async()/q6asm_read() with -EINVAL when !port->buf or !port->num_periods, preventing races with concurrent unmap.

Fixes 68fd8480bb7b ("ASoC: qdsp6: q6asm: Add q6asm driver")

---
Changes in v2:
- Clear port->buf and port->dsp_buf under ac->lock in q6asm_audio_client_free_buf() (freeing the saved pointer after unlock) and reject q6asm_write_async()/q6asm_read() when !port->buf or !port->num_periods.

 sound/soc/qcom/qdsp6/q6asm.c | 44 ++++++++++++++++++++++--------------
 1 file changed, 27 insertions(+), 17 deletions(-)
sound/soc/qcom/qdsp6/q6asm.c +27 -17
index ee1930e41..54408fd64 100644
@@ -427,13 +427,16 @@ static int __q6asm_memory_unmap(struct audio_client *ac,
 static void q6asm_audio_client_free_buf(struct audio_client *ac,
 					struct audio_port_data *port)
 {
+	struct audio_buffer *buf;
 	unsigned long flags;
 
 	spin_lock_irqsave(&ac->lock, flags);
 	port->num_periods = 0;
-	spin_unlock_irqrestore(&ac->lock, flags);
-	kfree(port->buf);
+	port->dsp_buf = 0;
+	buf = port->buf;
 	port->buf = NULL;
+	spin_unlock_irqrestore(&ac->lock, flags);
+	kfree(buf);
 }
 
 /**
@@ -1541,6 +1544,10 @@ int q6asm_read(struct audio_client *ac, uint32_t stream_id)
 
 	spin_lock_irqsave(&ac->lock, flags);
 	port = &ac->port[SNDRV_PCM_STREAM_CAPTURE];
+	if (!port->buf || !port->num_periods) {
+		spin_unlock_irqrestore(&ac->lock, flags);
+		return -EINVAL;
+	}
 	q6asm_add_hdr(ac, &pkt->hdr, pkt_size, false, stream_id);
 	ab = &port->buf[port->dsp_buf];
 	pkt->hdr.opcode = ASM_DATA_CMD_READ_V2;
@@ -1552,16 +1559,16 @@ int q6asm_read(struct audio_client *ac, uint32_t stream_id)
 	read->seq_id = port->dsp_buf;
 	pkt->hdr.token = port->dsp_buf;
 
-	port->dsp_buf++;
-
-	if (port->dsp_buf >= port->num_periods)
-		port->dsp_buf = 0;
-
-	spin_unlock_irqrestore(&ac->lock, flags);
 	rc = apr_send_pkt(ac->adev, pkt);
-	if (rc == pkt_size)
+	if (rc == pkt_size) {
 		rc = 0;
-	else
+		port->dsp_buf++;
+		if (port->dsp_buf >= port->num_periods)
+			port->dsp_buf = 0;
+	}
+	spin_unlock_irqrestore(&ac->lock, flags);
+
+	if (rc)
 		pr_err("read op[0x%x]rc[%d]\n", pkt->hdr.opcode, rc);
 
 	return rc;
@@ -1657,6 +1664,10 @@ int q6asm_write_async(struct audio_client *ac, uint32_t stream_id, uint32_t len,
 
 	spin_lock_irqsave(&ac->lock, flags);
 	port = &ac->port[SNDRV_PCM_STREAM_PLAYBACK];
+	if (!port->buf || !port->num_periods) {
+		spin_unlock_irqrestore(&ac->lock, flags);
+		return -EINVAL;
+	}
 	q6asm_add_hdr(ac, &pkt->hdr, pkt_size, false, stream_id);
 
 	ab = &port->buf[port->dsp_buf];
@@ -1673,15 +1684,14 @@ int q6asm_write_async(struct audio_client *ac, uint32_t stream_id, uint32_t len,
 
 	write->flags = wflags;
 
-	port->dsp_buf++;
-
-	if (port->dsp_buf >= port->num_periods)
-		port->dsp_buf = 0;
-
-	spin_unlock_irqrestore(&ac->lock, flags);
 	rc = apr_send_pkt(ac->adev, pkt);
-	if (rc == pkt_size)
+	if (rc == pkt_size) {
 		rc = 0;
+		port->dsp_buf++;
+		if (port->dsp_buf >= port->num_periods)
+			port->dsp_buf = 0;
+	}
+	spin_unlock_irqrestore(&ac->lock, flags);
 
 	return rc;
 }
Signature · 1 line
2.43.0
#10 lore raw

Re: [PATCH v2 1/2] ASoC: qcom: q6asm: don't advance dsp_buf on failed write or read

Thank you for your contribution! Sashiko AI review found 1 potential issue to consider: - [Low] ASoC: qcom: q6asm: inaccurate Fixes trailer subject in commit message --

Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [Low] ASoC: qcom: q6asm: inaccurate Fixes trailer subject in commit message
--

commit a358d36ca367153380fac1d8e155a55b8e361b0c
Author: Vladimir Bely <[email protected]>

Quoted 17 lines

ASoC: qcom: q6asm: don't advance dsp_buf on failed write or read

q6asm_write_async() and q6asm_read() increment port->dsp_buf under ac->lock and release the lock before calling apr_send_pkt(). If apr_send_pkt() fails (for example with -EAGAIN when the SMD FIFO is full during a playback write burst on MSM8916), dsp_buf has already moved on even though the DSP never received that buffer. Subsequent writes then send out-of-sequence period addresses and tokens, and any retry skips the failed period.

Hold ac->lock across apr_send_pkt() (which is atomic and non-blocking via rpmsg_trysend()) and only advance port->dsp_buf when the packet was actually accepted. Also clear port->buf and port->dsp_buf under ac->lock in q6asm_audio_client_free_buf() (freeing the saved pointer after unlock) and reject q6asm_write_async()/q6asm_read() with -EINVAL when !port->buf or !port->num_periods, preventing races with concurrent unmap.

Quoted 3 lines
Fixes 68fd8480bb7b ("ASoC: qdsp6: q6asm: Add q6asm driver")

[Severity: Low]
The subject for commit 68fd8480bb7b is "ASoC: qdsp6: q6asm: Add support to audio stream apis" ("ASoC: qdsp6: q6asm: Add q6asm driver" is the subject of commit a13e872314ae), and the unlocked port->buf clear and kfree() in q6asm_audio_client_free_buf() came from commit fdbb53d318aa ("ASoC: qdsp6:
q6asm: do not sleep while atomic"). Should the Fixes tags be:

Fixes 68fd8480bb7b ("ASoC: qdsp6: q6asm: Add support to audio stream apis")
Fixes fdbb53d318aa ("ASoC: qdsp6: q6asm: do not sleep while atomic")
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
#11 lore raw

PATCH v2 2/2 ASoC: qcom: q6asm-dai: retry playback queuing on -EAGAIN

In reply to Vladimir Bely

q6asm_dai_ack() sends one ASM_DATA_CMD_WRITE_V2 APR packet per available period in a tight loop. On SoCs with a small SMD FIFO (such as MSM8916), an initial playback write burst or a buffer with many periods can fill the FIFO, making rpmsg_trysend() return -EAGAIN. q6asm_dai_ack() currently aborts and propagates -EAGAIN to ALSA, causing userspace writes to fail and playback to stall.

Move playback period queuing into a helper q6asm_dai_queue_playback() called with the PCM stream lock held. Handle appl_ptr wrap around runtime->boundary, guard against appl_ptr moving behind queue_ptr (e.g. on SNDRV_PCM_IOCTL_RESET or appl_ptr rollback) by requiring avail <= runtime->buffer_size, and stop cleanly on -EAGAIN without returning an error. Retry from event_handler() on ASM_CLIENT_EVENT_DATA_WRITE_DONE under pcm_stream_lock_irqsave only when snd_pcm_running(substream) is true, so queuing stays serialized with ack, stop, XRUN, prepare, and close.

Fixes e386c2cf3df2 ("ASoC: qdsp6: q6asm: Add q6asm dai driver")

---
Changes in v2:
- Bound avail <= runtime->buffer_size to avoid wrapping to ~runtime->boundary if appl_ptr moves behind queue_ptr on reset or appl_ptr rollback (reported by Sashiko AI review).
- Take pcm_stream_lock_irqsave in event_handler() and check substream->runtime && snd_pcm_running(substream) instead of using prtd->lock, serializing with ack, stop, XRUN, prepare, and close (reported by Sashiko AI review).

 sound/soc/qcom/qdsp6/q6asm-dai.c | 61 +++++++++++++++++++++++++-------
 1 file changed, 48 insertions(+), 13 deletions(-)
sound/soc/qcom/qdsp6/q6asm-dai.c +48 -13
index 57cbce2bb..b2856e7a2 100644
@@ -179,6 +179,40 @@ static const struct snd_compr_codec_caps q6asm_compr_caps = {
 	.descriptor[0].formats = 0,
 };
 
+/*
+ * Queue the periods the application has written so far. Each period is one
+ * APR packet sent with rpmsg_trysend(), and a burst can fill the small SMD
+ * FIFO (-EAGAIN). That is not an error: stop and retry when the DSP hands a
+ * buffer back (DATA_WRITE_DONE). Caller must hold the PCM stream lock.
+ */
+static int q6asm_dai_queue_playback(struct q6asm_dai_rtd *prtd)
+{
+	struct snd_pcm_runtime *runtime = prtd->substream->runtime;
+	int ret;
+
+	while (prtd->state == Q6ASM_STREAM_RUNNING) {
+		snd_pcm_sframes_t avail = runtime->control->appl_ptr - prtd->queue_ptr;
+
+		if (avail < 0)
+			avail += runtime->boundary;
+		if (avail < runtime->period_size || avail > runtime->buffer_size)
+			break;
+
+		ret = q6asm_write_async(prtd->audio_client, prtd->stream_id,
+					prtd->pcm_count, 0, 0, 0);
+		if (ret == -EAGAIN)
+			break;
+		if (ret < 0)
+			return ret;
+
+		prtd->queue_ptr += runtime->period_size;
+		if (prtd->queue_ptr >= runtime->boundary)
+			prtd->queue_ptr -= runtime->boundary;
+	}
+
+	return 0;
+}
+
 static void event_handler(uint32_t opcode, uint32_t token,
 			  void *payload, void *priv)
 {
@@ -192,6 +226,15 @@ static void event_handler(uint32_t opcode, uint32_t token,
 		break;
 	case ASM_CLIENT_EVENT_DATA_WRITE_DONE:
 		snd_pcm_period_elapsed(substream);
+		/* Push any periods left behind by a full SMD FIFO */
+		if (substream) {
+			scoped_guard(pcm_stream_lock_irqsave, substream) {
+				if (substream->runtime &&
+				    snd_pcm_running(substream) &&
+				    q6asm_dai_queue_playback(prtd) < 0)
+					pr_err_ratelimited("q6asm-dai: Error queuing playback buffer\n");
+			}
+		}
 		break;
 	case ASM_CLIENT_EVENT_DATA_READ_DONE:
 		snd_pcm_period_elapsed(substream);
@@ -321,20 +364,12 @@ static int q6asm_dai_ack(struct snd_soc_component *component, struct snd_pcm_sub
 {
 	struct snd_pcm_runtime *runtime = substream->runtime;
 	struct q6asm_dai_rtd *prtd = runtime->private_data;
-	int i, ret = 0, avail_periods;
-
-	if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK && prtd->state == Q6ASM_STREAM_RUNNING) {
-		avail_periods = (runtime->control->appl_ptr - prtd->queue_ptr)/runtime->period_size;
-		for (i = 0; i < avail_periods; i++) {
-			ret = q6asm_write_async(prtd->audio_client, prtd->stream_id,
-					   prtd->pcm_count, 0, 0, 0);
+	int ret = 0;
 
-			if (ret < 0) {
-				dev_err(component->dev, "Error queuing playback buffer %d\n", ret);
-				return ret;
-			}
-			prtd->queue_ptr += runtime->period_size;
-		}
+	if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
+		ret = q6asm_dai_queue_playback(prtd);
+		if (ret < 0)
+			dev_err(component->dev, "Error queuing playback buffer %d\n", ret);
 	}
 
 	return ret;
Signature · 1 line
2.43.0
#12 lore raw

Re: [PATCH v2 2/2] ASoC: qcom: q6asm-dai: retry playback queuing on -EAGAIN

Thank you for your contribution! Sashiko AI review found 3 potential issues to consider: - [High] ASoC: qcom: q6asm-dai: full SMD FIFO in q6asm_dai_ack() breaks trigger start - [Low] ASoC: qcom…

Thank you for your contribution! Sashiko AI review found 3 potential issues to consider:
- [High] ASoC: qcom: q6asm-dai: full SMD FIFO in q6asm_dai_ack() breaks trigger start
- [Low] ASoC: qcom: q6asm-dai: redundant stream lock cycle in event_handler()
- [Low] ASoC: qcom: q6asm-dai: wrong commit subject in Fixes trailer --

commit 2a93925c1ef34ca2638692fd522578e41fe20e98
Author: Vladimir Bely <[email protected]>

Quoted 19 lines

ASoC: qcom: q6asm-dai: retry playback queuing on -EAGAIN

q6asm_dai_ack() sends one ASM_DATA_CMD_WRITE_V2 APR packet per available period in a tight loop. On SoCs with a small SMD FIFO (such as MSM8916), an initial playback write burst or a buffer with many periods can fill the FIFO, making rpmsg_trysend() return -EAGAIN. q6asm_dai_ack() currently aborts and propagates -EAGAIN to ALSA, causing userspace writes to fail and playback to stall.

Move playback period queuing into a helper q6asm_dai_queue_playback() called with the PCM stream lock held. Handle appl_ptr wrap around runtime->boundary, guard against appl_ptr moving behind queue_ptr (e.g. on SNDRV_PCM_IOCTL_RESET or appl_ptr rollback) by requiring avail <= runtime->buffer_size, and stop cleanly on -EAGAIN without returning an error. Retry from event_handler() on ASM_CLIENT_EVENT_DATA_WRITE_DONE under pcm_stream_lock_irqsave only when snd_pcm_running(substream) is true, so queuing stays serialized with ack, stop, XRUN, prepare, and close.

Quoted 3 lines
Fixes e386c2cf3df2 ("ASoC: qdsp6: q6asm: Add q6asm dai driver")

[Severity: Low]
The subject in the Fixes: tag belongs to commit 2a9e92d371db ("ASoC:
qdsp6: q6asm: Add q6asm dai driver"). Should this be:

Fixes e386c2cf3df2 ("ASoC: qcom: q6asm-dai: schedule all available frames to avoid dsp under-runs")
Quoted 31 lines
sound/soc/qcom/qdsp6/q6asm-dai.c +23 -0
index 57cbce2bb0cb7..b2856e7a28e0d 100644
@@ -179,6 +179,40 @@ static const struct snd_compr_codec_caps q6asm_compr_caps = {
 	.descriptor[0].formats = 0,
 };
 
+/*
+ * Queue the periods the application has written so far. Each period is one
+ * APR packet sent with rpmsg_trysend(), and a burst can fill the small SMD
+ * FIFO (-EAGAIN). That is not an error: stop and retry when the DSP hands a
+ * buffer back (DATA_WRITE_DONE). Caller must hold the PCM stream lock.
+ */
+static int q6asm_dai_queue_playback(struct q6asm_dai_rtd *prtd)
+{
+	struct snd_pcm_runtime *runtime = prtd->substream->runtime;
+	int ret;
+
+	while (prtd->state == Q6ASM_STREAM_RUNNING) {
+		snd_pcm_sframes_t avail = runtime->control->appl_ptr - prtd->queue_ptr;
+
+		if (avail < 0)
+			avail += runtime->boundary;
+		if (avail < runtime->period_size || avail > runtime->buffer_size)
+			break;
+
+		ret = q6asm_write_async(prtd->audio_client, prtd->stream_id,
+					prtd->pcm_count, 0, 0, 0);
+		if (ret == -EAGAIN)
+			break;

sound/soc/qcom/qdsp6/q6asm-dai.c static const struct snd_compr_codec_caps q6asm_compr_caps = { · line 214

Quoted 10 lines
sound/soc/qcom/qdsp6/q6asm-dai.c +10 -0
+		if (ret < 0)
+			return ret;
+
+		prtd->queue_ptr += runtime->period_size;
+		if (prtd->queue_ptr >= runtime->boundary)
+			prtd->queue_ptr -= runtime->boundary;
+	}
+
+	return 0;
+}

[Severity: High]
Can filling the SMD FIFO in q6asm_dai_queue_playback() cause the subsequent SNDRV_PCM_TRIGGER_START to fail with -EAGAIN?

q6asm_dai_prepare() sets prtd->state = Q6ASM_STREAM_RUNNING while runtime->state is still SNDRV_PCM_STATE_PREPARED. When an initial write burst in __snd_pcm_lib_xfer() reaches runtime->start_threshold, pcm_lib_apply_appl_ptr() calls q6asm_dai_ack() -> q6asm_dai_queue_playback() before calling snd_pcm_start():

sound/core/pcm_lib.c:__snd_pcm_lib_xfer() {
    ...
		err = pcm_lib_apply_appl_ptr(substream, appl_ptr);
		if (err < 0)
			goto _end_unlock;
    ...
		if (is_playback &&
		    runtime->state == SNDRV_PCM_STATE_PREPARED &&
		    snd_pcm_playback_hw_avail(runtime) >= (snd_pcm_sframes_t)runtime->start_threshold) {
			err = snd_pcm_start(substream);
			if (err < 0)
				goto _end_unlock;
		}
    ...
}

If q6asm_dai_queue_playback() fills the SMD FIFO until q6asm_write_async() returns -EAGAIN and then returns 0, the immediately following snd_pcm_start() -> q6asm_dai_trigger(SNDRV_PCM_TRIGGER_START) -> q6asm_run_nowait() tries to send ASM_SESSION_CMD_RUN_V2 via rpmsg_trysend() on the same exhausted SMD FIFO and fails with -EAGAIN. snd_pcm_start() then runs snd_pcm_undo_start() and leaves runtime->state in SNDRV_PCM_STATE_PREPARED with prtd->queue_ptr and port->dsp_buf already advanced, while __snd_pcm_lib_xfer() returns the positive xfer count to userspace. Because the DSP stream never starts (or whenever -EAGAIN is hit with no buffers in flight on the DSP), ASM_CLIENT_EVENT_DATA_WRITE_DONE never fires and playback stalls permanently.

Quoted 4 lines
sound/soc/qcom/qdsp6/q6asm-dai.c +0 -0
@@ -192,6 +226,15 @@ static void event_handler(uint32_t opcode, uint32_t token,
 		break;
 	case ASM_CLIENT_EVENT_DATA_WRITE_DONE:
 		snd_pcm_period_elapsed(substream);

sound/soc/qcom/qdsp6/q6asm-dai.c static void event_handler(uint32_t opcode, uint32_t token, · line 238

Quoted 10 lines
sound/soc/qcom/qdsp6/q6asm-dai.c +9 -0
+		/* Push any periods left behind by a full SMD FIFO */
+		if (substream) {
+			scoped_guard(pcm_stream_lock_irqsave, substream) {
+				if (substream->runtime &&
+				    snd_pcm_running(substream) &&
+				    q6asm_dai_queue_playback(prtd) < 0)
+					pr_err_ratelimited("q6asm-dai: Error queuing playback buffer\n");
+			}
+		}
 		break;

[Severity: Low]
snd_pcm_period_elapsed() already takes and releases pcm_stream_lock_irqsave(substream) around snd_pcm_period_elapsed_under_stream_lock(). Calling snd_pcm_period_elapsed_under_stream_lock(substream) and q6asm_dai_queue_playback(prtd) under a single scoped_guard(pcm_stream_lock_irqsave, substream) (as q6apm-dai does) would avoid taking the stream lock twice on every period and queue pending periods before releasing the lock to woken userspace writers.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2