Linux Sound subsystem development
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Joonho Lee" <mynameisjune111@gmail.com>, linux-sound@vger.kernel.org
Cc: virtualization@lists.linux.dev,
	"Michael S. Tsirkin" <mst@redhat.com>,
	"Eugenio Perez" <eperezma@redhat.com>
Subject: Re: [PATCH v10 12/14] ALSA: pcm: add an optional reset operation under the stream lock
Date: Fri, 30 Oct 2026 05:54:43 +0000	[thread overview]
Message-ID: <sashiko-outbox-190579@kernel.org> (raw)
In-Reply-To: <20261030052629.27742-13-mynameisjune111@gmail.com>

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Medium] ALSA: pcm: unlocked appl_ptr overwrite in snd_pcm_post_prepare() with reset op
--

commit 963c7726f3bbfa33b12ff37026f791cf2e45509e
Author: Joonho Lee <mynameisjune111@gmail.com>

ALSA: pcm: add an optional reset operation under the stream lock

This commit adds an optional reset callback to struct snd_pcm_ops that runs
under the substream's PCM stream lock, and exposes snd_pcm_lib_reset() for
drivers to retain the common pointer update logic.

> diff --git a/sound/core/pcm_native.c b/sound/core/pcm_native.c
> index a2ea44c3c1d29..41c691512f54c 100644
> --- a/sound/core/pcm_native.c
> +++ b/sound/core/pcm_native.c
> @@ -1988,7 +1988,31 @@ static int snd_pcm_do_reset(struct snd_pcm_substream *substream,
>  			    snd_pcm_state_t state)
>  {
>  	struct snd_pcm_runtime *runtime = substream->runtime;
> -	int err = snd_pcm_ops_ioctl(substream, SNDRV_PCM_IOCTL1_RESET, NULL);
> +	int err;
> +
> +	if (substream->ops->reset) {
> +		/*
> +		 * Complete this member before releasing its PCM lock. A later
> +		 * linked member may fail; RESET has no group rollback operation.
> +		 */
> +		guard(pcm_stream_lock_irq)(substream);
> +
> +		err = substream->ops->reset(substream);
> +		if (err < 0)
> +			return err;
> +		runtime->hw_ptr_base = 0;
> +		runtime->hw_ptr_interrupt = runtime->status->hw_ptr -
> +			runtime->status->hw_ptr % runtime->period_size;
> +		runtime->silence_start = runtime->status->hw_ptr;
> +		runtime->silence_filled = 0;
> +		runtime->control->appl_ptr = runtime->status->hw_ptr;
> +		if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
> +		    runtime->silence_size > 0)
> +			snd_pcm_playback_silence(substream, ULONG_MAX);
> +		return 0;
> +	}
> +
> +	err = snd_pcm_ops_ioctl(substream, SNDRV_PCM_IOCTL1_RESET, NULL);
>  	if (err < 0)
>  		return err;
>  	guard(pcm_stream_lock_irq)(substream);
> @@ -2004,6 +2028,9 @@ static void snd_pcm_post_reset(struct snd_pcm_substream *substream,
>  			       snd_pcm_state_t state)
>  {
>  	struct snd_pcm_runtime *runtime = substream->runtime;
> +
> +	if (substream->ops->reset)
> +		return;

[Severity: Medium]
Does snd_pcm_post_prepare() also need to account for substream->ops->reset?

In sound/core/pcm_native.c, the prepare path runs:

  snd_pcm_prepare()
    -> snd_pcm_action_nonatomic(&snd_pcm_action_prepare, ...)
      -> snd_pcm_do_prepare()
        -> snd_pcm_do_reset()
      -> snd_pcm_post_prepare()

When substream->ops->reset is implemented, snd_pcm_do_reset() rebases
runtime->control->appl_ptr = runtime->status->hw_ptr under
guard(pcm_stream_lock_irq)(substream) and then releases the lock. While
snd_pcm_post_reset() skips the redundant assignment when substream->ops->reset
is set, snd_pcm_post_prepare() was not updated:

  static void snd_pcm_post_prepare(struct snd_pcm_substream *substream,
                                   snd_pcm_state_t state)
  {
          struct snd_pcm_runtime *runtime = substream->runtime;
          runtime->control->appl_ptr = runtime->status->hw_ptr;
          snd_pcm_set_state(substream, SNDRV_PCM_STATE_PREPARED);
  }

Between snd_pcm_do_reset() releasing pcm_stream_lock_irq and
snd_pcm_post_prepare() running (for example, while subsequent linked group
members execute snd_pcm_do_prepare()), a concurrent SNDRV_PCM_IOCTL_SYNC_PTR in
snd_pcm_sync_ptr() or a transfer/rewind/forward on another linked member that
was already in SNDRV_PCM_STATE_PREPARED can take pcm_stream_lock_irq and update
runtime->control->appl_ptr via pcm_lib_apply_appl_ptr(). Afterward,
snd_pcm_post_prepare() overwrites runtime->control->appl_ptr without holding
pcm_stream_lock_irq, which can desynchronize runtime->control->appl_ptr from
driver state.

In addition, because snd_pcm_do_prepare() calls snd_pcm_do_reset(), calling
snd_pcm_playback_silence(substream, ULONG_MAX) inside snd_pcm_do_reset() when
substream->ops->reset is set causes prepare on a playback stream with
runtime->silence_size > 0 to fill silence during do_prepare before the stream
transitions to SNDRV_PCM_STATE_PREPARED, whereas the legacy path only runs
snd_pcm_playback_silence() in snd_pcm_post_reset() and snd_pcm_post_start().

Should snd_pcm_post_prepare() skip the appl_ptr assignment when
substream->ops->reset is set, and should snd_pcm_do_reset() avoid calling
snd_pcm_playback_silence() when invoked from snd_pcm_do_prepare()?

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261030052629.27742-1-mynameisjune111@gmail.com?part=12

  reply	other threads:[~2026-10-30  5:54 UTC|newest]

Thread overview: 19+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-10-30  5:26 [PATCH v10 00/14] ALSA: virtio: staged capture recovery and PCM RESET synchronization Joonho Lee
2026-10-30  5:26 ` [PATCH v10 01/14] ALSA: virtio: Propagate PCM send failures through ACK Joonho Lee
2026-10-30  5:26 ` [PATCH v10 02/14] ALSA: virtio: Recover PCM state after a failed start Joonho Lee
2026-10-30  5:26 ` [PATCH v10 03/14] ALSA: virtio: Retain queued messages through device reset Joonho Lee
2026-10-30  5:26 ` [PATCH v10 04/14] ALSA: virtio: Keep PCM DMA buffers alive through device ownership Joonho Lee
2026-10-30  5:56   ` sashiko-bot
2026-10-30  5:26 ` [PATCH v10 05/14] ALSA: virtio: Keep control payloads alive through DMA completion Joonho Lee
2026-10-30  5:26 ` [PATCH v10 06/14] ALSA: virtio: wait through signals for PCM teardown Joonho Lee
2026-10-30  5:26 ` [PATCH v10 07/14] ALSA: pcm: mark stop-threshold accesses for capture policy sampling Joonho Lee
2026-10-30  5:26 ` [PATCH v10 08/14] ALSA: pcm: report partial progress under the stream lock Joonho Lee
2026-10-30  5:26 ` [PATCH v10 09/14] ALSA: virtio: add staged capture accounting and lifecycle recovery Joonho Lee
2026-10-30  6:38   ` sashiko-bot
2026-10-30  5:26 ` [PATCH v10 10/14] ALSA: virtio: prioritize staged capture period work Joonho Lee
2026-10-30  5:26 ` [PATCH v10 11/14] docs: describe virtio-sound capture options Joonho Lee
2026-10-30  5:26 ` [PATCH v10 12/14] ALSA: pcm: add an optional reset operation under the stream lock Joonho Lee
2026-10-30  5:54   ` sashiko-bot [this message]
2026-10-30  5:26 ` [PATCH v10 13/14] ALSA: virtio: synchronize staged capture accounting during RESET Joonho Lee
2026-10-30  5:26 ` [PATCH v10 14/14] ALSA: virtio: rebase ordinary capture accounting on RESET Joonho Lee
2026-10-30  6:35   ` 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-190579@kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=eperezma@redhat.com \
    --cc=linux-sound@vger.kernel.org \
    --cc=mst@redhat.com \
    --cc=mynameisjune111@gmail.com \
    --cc=sashiko-reviews@lists.linux.dev \
    --cc=virtualization@lists.linux.dev \
    /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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox