[PATCH v2 1/2] ASoC: qcom: q6asm: don't advance dsp_buf on failed write or read
From: Vladimir Bely
Date: Sat Nov 28 2026 - 20:18:15 EST
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")
Cc: stable@xxxxxxxxxxxxxxx
Signed-off-by: Vladimir Bely <vlwwwwww@xxxxxxxxx>
---
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(-)
diff --git a/sound/soc/qcom/qdsp6/q6asm.c b/sound/soc/qcom/qdsp6/q6asm.c
index ee1930e41..54408fd64 100644
--- a/sound/soc/qcom/qdsp6/q6asm.c
+++ b/sound/soc/qcom/qdsp6/q6asm.c
@@ -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;
}
--
2.43.0