From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=100.103.45.18 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1795912461; b=qVjpItJADwKNJUsS3YsroaO/WHTMVLhYcJpdw5YNtZZYWdSVV0u7oIXrwv1uCzWbU3QY 9wVJ1MsNHhBjybUls3POQWUVkPyZ1ZV6HVm9RvUpwLe3MkxE8gPUiTD/OHFE5Zc4e3Cnr o8o+MHlGDGTM6lP9DfvbL9h0lsXogBWivXPa00qT9a/f8FQ5+2tG2jh1gn//P/pWh39j6 nHsTLIWrf2fXEbsBAdH8dv5jCqUBU35r+G0gprSH/ZJv2ViTf65L1tWxF4Klzt8eb7ACK wjX6q9S3gfRXudZePBFZxULyIu1hfURFHZd26eGBPsXz6E9JIrp9d0IZ5a9dq0p5CZg== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1795912461; h=Received:DKIM-Signature:Message-ID:From:Subject:Reply-To:To:Cc: In-Reply-To:References:Content-Type:Content-Transfer-Encoding:Date; bh=xYH076reNAb2RVJCGi+liyCIiVohfkDiX/iKwMASzac=; b=jn5aP6UwIoJXVwPeVC0RZvBEzsrkGhOUXpe2Z9RooHZuKlmcnO06JMKQbJOXpr7gl8KI sNIxix+zD4S/p4byYaQzne0hhnzgbhZ5AOGij4trtqVEKGaIZqXb7loffBbKkjXyKMhEo NZ7rxUNXqg64GpYgJN+CoG1IOvCUzdZOXJL1KPEH7MGz5tQZYL/sVJ6ISlzELHVDF3zhE rysp0tEIlw9loKTKd7z8p2sYWvwKSjS3zwSDIsgdyRC7pUGzeVuwg9qhg8M4dzrxyOW61 u+6zjKwixsts5Ppn2JX3pro1M/wGjkKMzXxY4ITUBXU6aWihdb1wOoSAyNaqzkWTZyw== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=Atf6VEax; arc=none smtp.remote-ip=100.103.45.18 Received: from smtp.kernel.org (quasi.space.kernel.org [100.103.45.18]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 82C0F606A31C for ; Sun, 29 Nov 2026 00:34:21 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key, unprotected) header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=Atf6VEax Received: by smtp.kernel.org (Postfix) with ESMTPSA id 11CD51F00897; Sun, 29 Nov 2026 00:34:21 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1795912461; bh=xYH076reNAb2RVJCGi+liyCIiVohfkDiX/iKwMASzac=; h=From:Subject:Reply-To:To:Cc:In-Reply-To:References:Date; b=Atf6VEaxQ7IBuwivSK+pmah4Kf70s/fkrNvfUTvPxX9WEuIpXwA0z1NjH3qMrYeM+ FXSg48nJh6hGC0KNx2MnEnJVX1OjRT4x+0tJHiN4v0EkiaoFeht0+hVsStJNPnTCnX OaJsTPMYzJVscdxzxhBr06xKq5bdUq+TUuZSxySEtORffEerPSD7Ca4UOjiJzDvw7A lZdaFrBMAz+y6iIZg6AlAGkqHCvxjoFcJ0HiDjB51Oq2nb4n6/qHHKQ/SX6aWiznUX kKe42IcDvStuV1mqhn46mNAvpHYA73qXOP5JclbeS6v+h1cr4lUbi/dEXhRA7QTn9A 7FMwiQrKdsb/A== Message-ID: From: sashiko-bot@kernel.org Subject: Re: [PATCH 2/2] ASoC: qcom: q6asm-dai: retry playback queuing on -EAGAIN Reply-To: sashiko-reviews@lists.linux.dev To: "Vladimir Bely" Cc: linux-sound@vger.kernel.org In-Reply-To: <20261129001951.991080-3-vlwwwwww@gmail.com> References: <20261129001951.991080-1-vlwwwwww@gmail.com> <20261129001951.991080-3-vlwwwwww@gmail.com> Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable Date: Sun, 29 Nov 2026 00:34:20 +0000 X-Mailing-List: linux-sound@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list 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 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/q6as= m-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 =3D { > .descriptor[0].formats =3D 0, > }; > =20 > +/* > + * 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 han= ds a > + * buffer back (DATA_WRITE_DONE). > + */ > +static int q6asm_dai_queue_playback(struct q6asm_dai_rtd *prtd) > +{ > + struct snd_pcm_runtime *runtime =3D prtd->substream->runtime; > + int ret; > + > + guard(spinlock_irqsave)(&prtd->lock); > + > + while (prtd->state =3D=3D Q6ASM_STREAM_RUNNING) { > + snd_pcm_sframes_t avail =3D runtime->control->appl_ptr - prtd->queue_p= tr; > + > + if (avail < 0) > + avail +=3D 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 =3D q6asm_write_async(prtd->audio_client, prtd->stream_id, > + prtd->pcm_count, 0, 0, 0); > + if (ret =3D=3D -EAGAIN) > + break; > + if (ret < 0) > + return ret; > + > + prtd->queue_ptr +=3D runtime->period_size; > + if (prtd->queue_ptr >=3D runtime->boundary) > + prtd->queue_ptr -=3D 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 t= oken, > 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 =3D=3D 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 =3D NULL event_handler(DATA_WRITE_DONE) q6asm_dai_queue_playback(prtd) q6asm_write_async() ab =3D &port->buf[port->dsp_buf] write->buf_addr_lsw =3D 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 =3D Q6ASM_STREAM_RUNNING with prtd->queue_ptr =3D 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. --=20 Sashiko AI review =C2=B7 https://sashiko.dev/#/patchset/20261129001951.9910= 80-1-vlwwwwww@gmail.com?part=3D2