All of lore.kernel.org
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Vladimir Bely" <vlwwwwww@gmail.com>
Cc: linux-sound@vger.kernel.org
Subject: Re: [PATCH 2/2] ASoC: qcom: q6asm-dai: retry playback queuing on -EAGAIN
Date: Sun, 29 Nov 2026 00:34:20 +0000	[thread overview]
Message-ID: <sashiko-outbox-239198@kernel.org> (raw)
In-Reply-To: <20261129001951.991080-3-vlwwwwww@gmail.com>

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 <vlwwwwww@gmail.com>

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.

> diff --git a/sound/soc/qcom/qdsp6/q6asm-dai.c b/sound/soc/qcom/qdsp6/q6asm-dai.c
> index 57cbce2bb0cb7..a21b93c3e0246 100644
> --- a/sound/soc/qcom/qdsp6/q6asm-dai.c
> +++ b/sound/soc/qcom/qdsp6/q6asm-dai.c
> @@ -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;

[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.

> +
> +		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;

[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.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261129001951.991080-1-vlwwwwww@gmail.com?part=2

  reply	other threads:[~2026-11-29  0:34 UTC|newest]

Thread overview: 13+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-29  0:19 [PATCH 0/2] ASoC: qcom: qdsp6: handle SMD FIFO -EAGAIN in q6asm playback Vladimir Bely
2026-11-29  0:19 ` [PATCH 1/2] ASoC: qcom: q6asm: don't advance dsp_buf on failed write or read Vladimir Bely
2026-11-29  0:19 ` [PATCH 2/2] ASoC: qcom: q6asm-dai: retry playback queuing on -EAGAIN Vladimir Bely
2026-11-29  0:34   ` sashiko-bot [this message]
2026-11-29  1:18 ` [PATCH v2 0/2] ASoC: qcom: qdsp6: handle SMD FIFO -EAGAIN in q6asm playback Vladimir Bely
2026-11-29  9:20   ` [PATCH v3 " Vladimir Bely
2026-11-30  7:49     ` Konrad Dybcio
2026-11-29  9:20   ` [PATCH v3 1/2] ASoC: qcom: q6asm: don't advance dsp_buf on failed write or read Vladimir Bely
2026-11-29  9:20   ` [PATCH v3 2/2] ASoC: qcom: q6asm-dai: retry playback queuing on -EAGAIN Vladimir Bely
2026-11-29  1:18 ` [PATCH v2 1/2] ASoC: qcom: q6asm: don't advance dsp_buf on failed write or read Vladimir Bely
2026-11-29  1:23   ` sashiko-bot
2026-11-29  1:18 ` [PATCH v2 2/2] ASoC: qcom: q6asm-dai: retry playback queuing on -EAGAIN Vladimir Bely
2026-11-29  1:31   ` sashiko-bot

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=sashiko-outbox-239198@kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=linux-sound@vger.kernel.org \
    --cc=sashiko-reviews@lists.linux.dev \
    --cc=vlwwwwww@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.