Linux Sound subsystem development
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Takashi Iwai" <tiwai@suse.de>, linux-sound@vger.kernel.org
Subject: Re: [PATCH 2/2] ALSA: pcm: Avoid hw_params/hw_refine changes during PCM buffer access
Date: Wed, 18 Nov 2026 14:29:37 +0000	[thread overview]
Message-ID: <sashiko-outbox-221208@kernel.org> (raw)
In-Reply-To: <20261118135558.1928160-2-tiwai@suse.de>

Thank you for your contribution! Sashiko AI review found 1 potential
issue to consider:
- [Medium] ALSA: pcm: state check before SND_PCM_BUFFER_ACQUIRE() races
  with hw_free
--

commit 307843a70c44558cff1055cb6881e2bf43f018d0
Author: Takashi Iwai <tiwai@suse.de>

ALSA: pcm: Avoid hw_params/hw_refine changes during PCM buffer access

This commit acquires the PCM buffer access gate at the start of PCM read,
write, readv, writev, and xfern ioctl paths to prevent concurrent
hw_params or hw_free calls from changing runtime attributes mid-operation.

> diff --git a/sound/core/pcm_compat.c b/sound/core/pcm_compat.c
> index c54cb07304ad4..0321d4241ec5b 100644
> --- a/sound/core/pcm_compat.c
> +++ b/sound/core/pcm_compat.c
> @@ -341,6 +341,9 @@ static int snd_pcm_ioctl_xfern_compat(struct snd_pcm_substream *substream,
>  	if (snd_pcm_get_state(substream) == SNDRV_PCM_STATE_OPEN)
>  		return -EBADFD;
>  
> +	SND_PCM_BUFFER_ACQUIRE(substream->runtime, lock);
> +	if (SND_PCM_BUFFER_ACQUIRE_ERR(lock))
> +		return -EBUSY;
>  	ch = substream->runtime->channels;
>  	if (ch > 128)
>  		return -EINVAL;
> diff --git a/sound/core/pcm_native.c b/sound/core/pcm_native.c
> index 61bd4f4af81a4..22cf138981c30 100644
> --- a/sound/core/pcm_native.c
> +++ b/sound/core/pcm_native.c
> @@ -3417,6 +3417,9 @@ static int snd_pcm_xfern_frames_ioctl(struct snd_pcm_substream *substream,
>  
>  	if (snd_pcm_get_state(substream) == SNDRV_PCM_STATE_OPEN)
>  		return -EBADFD;
> +	SND_PCM_BUFFER_ACQUIRE(runtime, lock);
> +	if (SND_PCM_BUFFER_ACQUIRE_ERR(lock))
> +		return -EBUSY;
>  	if (runtime->channels > 128)
>  		return -EINVAL;
>  	if (put_user(0, &_xfern->result))
> @@ -3656,6 +3659,9 @@ static ssize_t snd_pcm_read(struct file *file, char __user *buf, size_t count,
>  	runtime = substream->runtime;
>  	if (snd_pcm_state_open_or_disconnected(substream))
>  		return -EBADFD;
> +	SND_PCM_BUFFER_ACQUIRE(runtime, lock);
> +	if (SND_PCM_BUFFER_ACQUIRE_ERR(lock))
> +		return -EBUSY;
>  	if (!frame_aligned(runtime, count))
>  		return -EINVAL;
>  	count = bytes_to_frames(runtime, count);
> @@ -3680,6 +3686,9 @@ static ssize_t snd_pcm_write(struct file *file, const char __user *buf,
>  	runtime = substream->runtime;
>  	if (snd_pcm_state_open_or_disconnected(substream))
>  		return -EBADFD;
> +	SND_PCM_BUFFER_ACQUIRE(runtime, lock);
> +	if (SND_PCM_BUFFER_ACQUIRE_ERR(lock))
> +		return -EBUSY;
>  	if (!frame_aligned(runtime, count))
>  		return -EINVAL;
>  	count = bytes_to_frames(runtime, count);
> @@ -3708,6 +3717,9 @@ static ssize_t snd_pcm_readv(struct kiocb *iocb, struct iov_iter *to)
>  		return -EBADFD;
>  	if (!user_backed_iter(to))
>  		return -EINVAL;
> +	SND_PCM_BUFFER_ACQUIRE(runtime, lock);
> +	if (SND_PCM_BUFFER_ACQUIRE_ERR(lock))
> +		return -EBUSY;
>  	if (to->nr_segs > 1024 || to->nr_segs != runtime->channels)
>  		return -EINVAL;
>  	if (!frame_aligned(runtime, iov->iov_len))
> @@ -3747,6 +3759,9 @@ static ssize_t snd_pcm_writev(struct kiocb *iocb, struct iov_iter *from)
>  		return -EBADFD;
>  	if (!user_backed_iter(from))
>  		return -EINVAL;
> +	SND_PCM_BUFFER_ACQUIRE(runtime, lock);
> +	if (SND_PCM_BUFFER_ACQUIRE_ERR(lock))
> +		return -EBUSY;

[Severity: Medium]
Can snd_pcm_hw_free() run between the state check and
SND_PCM_BUFFER_ACQUIRE() in these six paths?

CPU0                                CPU1
snd_pcm_write()
  snd_pcm_state_open_or_disconnected()
    (passes in STATE_SETUP)
                                    snd_pcm_hw_free()
                                      snd_pcm_buffer_access_lock(runtime)
                                      do_hw_free()
                                        runtime->dma_area = NULL
                                      runtime->state = SNDRV_PCM_STATE_OPEN
                                      snd_pcm_buffer_access_unlock(runtime)
  SND_PCM_BUFFER_ACQUIRE(runtime, lock)
  snd_pcm_lib_write()
    __snd_pcm_lib_xfer()
      pcm_sanity_check()            <-- hits snd_BUG_ON()

Because pcm_sanity_check() evaluates snd_BUG_ON(!substream->ops->copy &&
!runtime->dma_area) before checking whether runtime->state is
SNDRV_PCM_STATE_OPEN:

sound/core/pcm_lib.c:pcm_sanity_check() {
	...
	if (snd_BUG_ON(!substream->ops->copy && !runtime->dma_area))
		return -EINVAL;
	if (runtime->state == SNDRV_PCM_STATE_OPEN)
		return -EBADFD;
	...
}

losing this race triggers a kernel warning (when CONFIG_SND_DEBUG is
enabled) and returns -EINVAL instead of -EBADFD.  Acquiring
SND_PCM_BUFFER_ACQUIRE() before checking the substream state, as
snd_pcm_mmap_data() does, would prevent transitions into
SNDRV_PCM_STATE_OPEN after the check.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261118135558.1928160-1-tiwai@suse.de?part=2

  reply	other threads:[~2026-11-18 14:29 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-18 13:55 [PATCH 1/2] ALSA: pcm: Simplify PCM buffer access gate with auto-cleanup helpers Takashi Iwai
2026-11-18 13:55 ` [PATCH 2/2] ALSA: pcm: Avoid hw_params/hw_refine changes during PCM buffer access Takashi Iwai
2026-11-18 14:29   ` sashiko-bot [this message]
2026-11-18 14:05 ` [PATCH 1/2] ALSA: pcm: Simplify PCM buffer access gate with auto-cleanup helpers 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-221208@kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=linux-sound@vger.kernel.org \
    --cc=sashiko-reviews@lists.linux.dev \
    --cc=tiwai@suse.de \
    /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