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 4/4] ALSA: pcm: Don't reallocate for the same sized buffers
Date: Thu, 03 Dec 2026 09:09:29 +0000	[thread overview]
Message-ID: <sashiko-outbox-247428@kernel.org> (raw)
In-Reply-To: <20261203084041.914026-5-tiwai@suse.de>

Thank you for your contribution! Sashiko AI review found 2 potential
issues to consider:
- [High] ALSA: pcm: same-size shortcut misses emu10k1 iommu_workaround
  on OSS mmap
- [Low] ALSA: pcm: commit message omits mmap_count ordering rationale
  for shortcut
--

commit 8fdd2c067f9f35e5db5a5b50be431e9113e37a29
Author: Takashi Iwai <tiwai@suse.de>

> ALSA: pcm: Don't reallocate for the same sized buffers
>
> When snd_pcm_lib_malloc_pages() is called with the same buffer size as
> the current one, we don't have to reallocate.  Go for a short cut.
>
> Link: https://lore.kernel.org/20261202140117.1164633-1-ahmedabdelmoumen05@gmail.com
> Reported-by: Ahmed Abdelmoemen <ahmedabdelmoumen05@gmail.com>
> Cc: <stable@vger.kernel.org>
> Signed-off-by: Takashi Iwai <tiwai@suse.de>

[Severity: Low]
Could the commit message mention the interaction with substream->mmap_count?

snd_pcm_lib_malloc_pages() already skips reallocation when
runtime->dma_buffer_p->bytes >= size.  Placing this check first is needed to
bypass the mmap_count check from commit "ALSA: pcm: Prohibit buffer
(re-)allocation/free during mmap" and preserve that ordering in backports.

> diff --git a/sound/core/pcm_memory.c b/sound/core/pcm_memory.c
> index 7aaa45f3bac14..2c610dc83044d 100644
> --- a/sound/core/pcm_memory.c
> +++ b/sound/core/pcm_memory.c
> @@ -431,6 +431,9 @@ int snd_pcm_lib_malloc_pages(struct snd_pcm_substream *substream, size_t size)
>  	runtime = substream->runtime;
>  	card = substream->pcm->card;
>  
> +	if (runtime->dma_area && runtime->dma_bytes == size)
> +		return 0; /* no need to change */
> +
>  	if (atomic_read(&substream->mmap_count))
>  		return -EBADFD;

[Severity: High]
Does the exact runtime->dma_bytes == size check miss
snd_emu10k1_playback_hw_params() when emu->iommu_workaround is enabled?

When the device is behind a non-passthrough IOMMU, emu10k1 pads the
allocation size by EMUPAGESIZE and trims runtime->dma_bytes afterwards:

sound/pci/emu10k1/emupcm.c:snd_emu10k1_playback_hw_params() {
    ...
	err = snd_emu10k1_pcm_channel_alloc(epcm, type, count, channels);
	if (err < 0)
		return err;

	alloc_size = params_buffer_bytes(hw_params);
	if (emu->iommu_workaround)
		alloc_size += EMUPAGESIZE;
	err = snd_pcm_lib_malloc_pages(substream, alloc_size);
	if (err < 0)
		return err;
	if (emu->iommu_workaround && runtime->dma_bytes >= EMUPAGESIZE)
		runtime->dma_bytes -= EMUPAGESIZE;
    ...
}

On a repeat OSS mmapped hw_params call with the same buffer size (for
example via SNDCTL_DSP_CHANNELS), runtime->dma_bytes is EMUPAGESIZE smaller
than size.  The shortcut misses and falls through to -EBADFD.

Because snd_emu10k1_pcm_channel_alloc() already replaced epcm->voices and
snd_pcm_hw_params() skips hw_free and state rollback while mmapped,
reducing channels from stereo to mono leaves runtime->channels stale at 2.

A later snd_emu10k1_playback_prepare() then initializes evoice + 1, causing
a NULL pointer dereference on (evoice + 1)->epcm or an out-of-bounds access
past emu->voices[].

Note that relaxing this check to runtime->dma_buffer_p->bytes >= size
without also setting runtime->dma_bytes = size would still break emupcm.c,
which unconditionally subtracts EMUPAGESIZE on any non-negative return.

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

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

Thread overview: 8+ 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  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 [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=sashiko-outbox-247428@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