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 v2 2/4] ALSA: pcm: Reject hw_params with buffer size changes while mmapped
Date: Thu, 03 Dec 2026 09:23:07 +0000	[thread overview]
Message-ID: <sashiko-outbox-247452@kernel.org> (raw)
In-Reply-To: <20261203084041.914026-3-tiwai@suse.de>

Thank you for your contribution! Sashiko AI review found 1 potential
issue to consider:
- [Medium] ASoC: __soc_pcm_hw_params() runs hw_free rollback while PCM
  is mmapped
--

commit 2730ca1aecc2de34e1b87564c9fe83017af5300d
Author: Takashi Iwai <tiwai@suse.de>

ALSA: pcm: Reject hw_params with buffer size changes while mmapped

This commit rejects snd_pcm_hw_params() calls with -EBADFD when a
substream is mmapped and the requested buffer size differs from the
current one, and skips managed buffer reallocation while mmapped.

Link: https://lore.kernel.org/20261202140117.1164633-1-ahmedabdelmoumen05@gmail.com

> diff --git a/sound/core/pcm_native.c b/sound/core/pcm_native.c
> index 6d17be7f9beb8..7f0c47379c96e 100644
> --- a/sound/core/pcm_native.c
> +++ b/sound/core/pcm_native.c

[ ... ]

> @@ -826,9 +827,18 @@ static int snd_pcm_hw_params(struct snd_pcm_substream *substream,
>  	if (err < 0)
>  		goto _error;
>  
> -	if (substream->managed_buffer_alloc) {
> -		err = snd_pcm_lib_malloc_pages(substream,
> -					       params_buffer_bytes(params));
> +	/* OSS still allows hw_params change during mmap -- except for the case
> +	 * where the buffer size actually changes (that can go wrong with mmap).
> +	 */
> +	buffer_bytes = params_buffer_bytes(params);
> +	if (atomic_read(&substream->mmap_count)) {
> +		if (buffer_bytes != frames_to_bytes(runtime, runtime->buffer_size)) {
> +			err = -EBADFD;
> +			goto _error;
> +		}
> +		runtime->buffer_changed = 0;
> +	} else if (substream->managed_buffer_alloc) {
> +		err = snd_pcm_lib_malloc_pages(substream, buffer_bytes);
>  		if (err < 0)
>  			goto _error;
>  		runtime->buffer_changed = err > 0;

[Severity: Medium]
Can continuing to call substream->ops->hw_params() while mmapped leave
ASoC driver state torn down while runtime->state stays in
SNDRV_PCM_STATE_SETUP?

When substream->mmap_count is non-zero and buffer_bytes is unchanged,
snd_pcm_hw_params() still invokes substream->ops->hw_params().  On ASoC,
if __soc_pcm_hw_params() fails (for example on an OSS SNDCTL_DSP_SPEED
change), it unconditionally calls soc_pcm_hw_clean(rtd, substream, 1)
without checking substream->mmap_count.  Because the initial pre-mmap
hw_params already marked the links, components, and DAIs, rollback
executes their hw_free callbacks while the substream is still mmapped.

Combined with commit "ALSA: pcm: Avoid forcibly changing to OPEN state
at hw_params errors", the _error path in snd_pcm_hw_params() leaves
runtime->state in SNDRV_PCM_STATE_SETUP when mmap_count is non-zero.  A
concurrent thread in snd_pcm_oss_make_ready() that passed the unlocked
runtime->oss.params check can then prepare and start the stream after
ASoC has already torn down its resources:

CPU0                                CPU1
                                    snd_pcm_oss_make_ready()
                                      if (runtime->oss.params) /* false */
SNDCTL_DSP_SPEED (mmapped)
  snd_pcm_hw_params()
    __soc_pcm_hw_params() -> err
      soc_pcm_hw_clean(..., 1)
        __avs_dai_fe_hw_free()
          data->path = NULL
    /* mmap_count > 0: state stays
     * SNDRV_PCM_STATE_SETUP */
                                      mutex_lock(&runtime->oss.params_lock)
                                      snd_pcm_oss_prepare()
                                        avs_dai_prepare() -> 0 (!data->path)
                                      SNDRV_PCM_IOCTL_START
                                        avs_path_pause(NULL)  <-- NULL deref

On Intel AVS this results in a kernel NULL pointer dereference in
avs_path_pause() at path->owner; on drivers such as Samsung IDMA,
rollback directly clears runtime->dma_area and runtime->dma_bytes via
snd_pcm_set_runtime_buffer(substream, NULL), causing a racing prepare
to program DMA with a zero transfer size.

Should __soc_pcm_hw_params() skip soc_pcm_hw_clean() when
substream->mmap_count is non-zero, or should driver hw_params callbacks
be avoided while mmapped?

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

  reply	other threads:[~2026-12-03  9:23 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-12-03  8:40 [PATCH v2 0/4] ALSA: pcm: Workarounds for OSS mmap issues Takashi Iwai
2026-12-03  8:40 ` [PATCH v2 1/4] ALSA: pcm: Avoid forcibly changing to OPEN state at hw_params errors Takashi Iwai
2026-12-03  9:09   ` sashiko-bot
2026-12-03  8:40 ` [PATCH v2 2/4] ALSA: pcm: Reject hw_params with buffer size changes while mmapped Takashi Iwai
2026-12-03  9:23   ` sashiko-bot [this message]
2026-12-03  8:40 ` [PATCH v2 3/4] ALSA: pcm: Prohibit buffer (re-)allocation/free during mmap Takashi Iwai
2026-12-03  9:10   ` sashiko-bot
2026-12-03  8:40 ` [PATCH v2 4/4] ALSA: pcm: Don't reallocate for the same sized buffers Takashi Iwai
2026-12-03  9:09   ` 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-247452@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