Linux Sound subsystem development
 help / color / mirror / Atom feed
From: Takashi Iwai <tiwai@suse.de>
To: linux-sound@vger.kernel.org
Subject: Re: [PATCH v2 3/3] ALSA: pcm: Avoid hw_params/hw_refine changes during PCM buffer access
Date: Thu, 19 Nov 2026 14:21:18 +0100	[thread overview]
Message-ID: <87mrr5ufch.wl-tiwai@suse.de> (raw)
In-Reply-To: <sashiko-outbox-223115@kernel.org>

On Thu, 19 Nov 2026 13:46:04 +0100,
sashiko-bot@kernel.org wrote:
> 
> Thank you for your contribution! Sashiko AI review found 1 potential
> issue to consider:
> - [Medium] ALSA: pcm: missing buffer access gate in xferi frame ioctl
>   handlers
> --
> 
> commit 1d0a5a9306bc1367f12d8e1ee79c3cdcad53aa1b
> 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 entry of the
> non-interleaved frame transfer ioctls and PCM read/write file operations
> to prevent concurrent hw_params or hw_free updates while accessing
> runtime state.
> 
> > diff --git a/sound/core/pcm_native.c b/sound/core/pcm_native.c
> > index 7d5e662b219bd..d028f03369e74 100644
> > --- a/sound/core/pcm_native.c
> > +++ b/sound/core/pcm_native.c
> > @@ -3434,6 +3434,9 @@ static int snd_pcm_xfern_frames_ioctl(struct snd_pcm_substream *substream,
> >  	void *bufs __free(kfree) = NULL;
> >  	snd_pcm_sframes_t result;
> >  
> > +	SND_PCM_BUFFER_ACQUIRE(runtime, lock);
> > +	if (SND_PCM_BUFFER_ACQUIRE_ERR(lock))
> > +		return -EBUSY;
> >  	if (snd_pcm_get_state(substream) == SNDRV_PCM_STATE_OPEN)
> >  		return -EBADFD;
> >  	if (runtime->channels > 128)
> 
> [Severity: Medium]
> Should snd_pcm_xferi_frames_ioctl() and snd_pcm_ioctl_xferi_compat() also
> acquire the buffer access gate with SND_PCM_BUFFER_ACQUIRE()?
> 
> __snd_pcm_lib_xfer() only calls snd_pcm_buffer_access_get() inside its
> inner transfer loop around writer().  Until then, a concurrent
> snd_pcm_hw_free() or snd_pcm_hw_params() can free runtime->dma_area
> before transitioning runtime->state to SNDRV_PCM_STATE_OPEN, causing
> pcm_sanity_check() to hit snd_BUG_ON(!substream->ops->copy &&
> !runtime->dma_area), or modify runtime->access, runtime->channels and
> other runtime parameters while __snd_pcm_lib_xfer() validates them or
> sleeps in wait_for_avail():
> 
> CPU0                                  CPU1
> snd_pcm_xferi_frames_ioctl()
>   snd_pcm_get_state() != OPEN
>                                       snd_pcm_hw_free()
>                                         snd_pcm_buffer_access_lock()
>                                         substream->ops->hw_free()
>                                           runtime->dma_area = NULL
>   snd_pcm_lib_write()
>     __snd_pcm_lib_xfer()
>       pcm_sanity_check()
>         snd_BUG_ON(!substream->ops->copy &&
>                    !runtime->dma_area)  <-- WARN / race
>                                         runtime->state = SNDRV_PCM_STATE_OPEN
> 

FWIW, the bug targeted by this patch is rather the runtime parameter
evaluation in snd_pcm__xfern_frames_ioctl and its compat version, and
others are just for a bit more safety.  The suggested issue is rather
a pre-existing thing and maybe handled better in another patch to
restructure the state change check in the read/write operations.


Takashi

      reply	other threads:[~2026-11-19 13:21 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-19 12:23 [PATCH v2 0/3] ALSA: PCM race and locking fixes Takashi Iwai
2026-11-19 12:23 ` [PATCH v2 1/3] ALSA: pcm: Fix potential ABBA deadlock at snd_pcm_action_nonatomic() Takashi Iwai
2026-11-19 12:23 ` [PATCH v2 2/3] ALSA: pcm: Simplify PCM buffer access gate with auto-cleanup helpers Takashi Iwai
2026-11-19 12:23 ` [PATCH v2 3/3] ALSA: pcm: Avoid hw_params/hw_refine changes during PCM buffer access Takashi Iwai
2026-11-19 12:46   ` sashiko-bot
2026-11-19 13:21     ` Takashi Iwai [this message]

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=87mrr5ufch.wl-tiwai@suse.de \
    --to=tiwai@suse.de \
    --cc=linux-sound@vger.kernel.org \
    /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