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/4] ALSA: pcm: Reject hw_params with buffer size changes while mmapped
Date: Wed, 02 Dec 2026 16:11:36 +0000	[thread overview]
Message-ID: <sashiko-outbox-246201@kernel.org> (raw)
In-Reply-To: <20261202155431.720872-3-tiwai@suse.de>

Thank you for your contribution! Sashiko AI review found 2 potential
issues to consider:
- [Medium] ALSA: pcm: unreachable mmap_count check in
  snd_pcm_hw_params()
- [Low] ALSA: pcm: stale runtime->buffer_changed in snd_pcm_hw_params()
  when mmapped
--

commit 29d0ead2858402673b9e78ea39c345b9d60787f7
Author: Takashi Iwai <tiwai@suse.de>

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

This commit restricts snd_pcm_hw_params() from changing the buffer size or
reallocating managed DMA buffers while the substream buffer is 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 8967a6f649343..d306340ae9190 100644
> --- a/sound/core/pcm_native.c
> +++ b/sound/core/pcm_native.c
> @@ -800,6 +800,7 @@ static int snd_pcm_hw_params(struct snd_pcm_substream *substream,
>  	int err;
>  	unsigned int bits;
>  	snd_pcm_uframes_t frames;
> +	size_t buffer_bytes;
>  
>  	if (PCM_RUNTIME_CHECK(substream))
>  		return -ENXIO;
> @@ -826,9 +827,17 @@ 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;
> +		}

[Severity: Medium]
Can execution ever reach this substream->mmap_count check with a non-zero
count?

At the start of snd_pcm_hw_params(), hw_params_state_check(substream, true)
is called while holding snd_pcm_buffer_access_lock(runtime), which prevents
concurrent mmaps from starting.  Since commit de57f471f664 ("ALSA: pcm:
Prevent PCM hw_params & co during mmap on OSS, too") removed the
!is_oss_stream(substream) exemption, hw_params_state_check()
unconditionally returns -EBADFD whenever substream->mmap_count is non-zero:

sound/core/pcm_native.c:hw_params_state_check() {
	...
	case SNDRV_PCM_STATE_OPEN:
	case SNDRV_PCM_STATE_SETUP:
	case SNDRV_PCM_STATE_PREPARED:
		if (check_mmap &&
		    atomic_read(&substream->mmap_count))
			return -EBADFD;
		return 0;
	...
}

As a result, OSS streams with an active mmap are rejected at the top of
snd_pcm_hw_params() even when the buffer size is unchanged, and this branch
is dead code.

> +	} 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: Low]
When the substream->mmap_count branch is taken (for example, on a stable
tree without de57f471f664 ("ALSA: pcm: Prevent PCM hw_params & co during
mmap on OSS, too") or if hw_params_state_check() is updated to allow OSS
streams), does skipping the managed_buffer_alloc branch leave
runtime->buffer_changed stale?

The initial pre-mmap allocation sets runtime->buffer_changed = 1, and
nothing clears it when snd_pcm_lib_malloc_pages() is skipped.  Driver
ops->hw_params callbacks that check runtime->buffer_changed (such as
sof_pcm_hw_params()) would then treat the unchanged mmapped buffer as
newly reallocated.

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

  reply	other threads:[~2026-12-02 16:11 UTC|newest]

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