From: Takashi Iwai <tiwai@suse.de>
To: 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 11:47:29 +0100 [thread overview]
Message-ID: <87ld66vdxa.wl-tiwai@suse.de> (raw)
In-Reply-To: <sashiko-outbox-247428@kernel.org>
On Thu, 03 Dec 2026 10:09:29 +0100,
sashiko-bot@kernel.org wrote:
>
> 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.
Indeed the hack in emu10k1 driver makes this check difficult.
And I noticed that this short-cut isn't really needed, as we cover
already by the check in runtime->dma_buffer_p->bytes below that point.
So I'll drop this in v3.
Takashi
prev parent reply other threads:[~2026-12-03 10:47 UTC|newest]
Thread overview: 11+ 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 10:44 ` Takashi Iwai
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
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
2026-12-03 10:47 ` 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=87ld66vdxa.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