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 3/4] ALSA: pcm: Prohibit buffer (re-)allocation/free during mmap
Date: Thu, 03 Dec 2026 09:10:31 +0000	[thread overview]
Message-ID: <sashiko-outbox-247432@kernel.org> (raw)
In-Reply-To: <20261203084041.914026-4-tiwai@suse.de>

Thank you for your contribution! Sashiko AI review found 2 potential
issues to consider:
- [Medium] ALSA: cs46xx: unchecked snd_pcm_lib_free_pages() leaks DMA
  pages on mmap
- [Medium] ALSA: pcm: mmap_count check breaks emu10k1 iommu_workaround
  hw_params
--

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

ALSA: pcm: Prohibit buffer (re-)allocation/free during mmap

This commit adds mmap_count checks to snd_pcm_lib_malloc_pages() and
snd_pcm_lib_free_pages() so both return -EBADFD when the PCM buffer is
currently mapped.

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

> diff --git a/sound/core/pcm_memory.c b/sound/core/pcm_memory.c
> index cfe9f10e7359d..7aaa45f3bac14 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 (atomic_read(&substream->mmap_count))
> +		return -EBADFD;
> +
>  	if (runtime->dma_buffer_p) {
>  		/* perphaps, we might free the large DMA memory region
>  		   to save some space here, but the actual solution

[Severity: Medium]
Can this mmap_count check break same-size OSS hw_params updates on
emu10k1 playback streams when emu->iommu_workaround is enabled?

When an emu10k1 device is behind a non-passthrough IOMMU,
snd_emu10k1_playback_hw_params() adds EMUPAGESIZE before calling
snd_pcm_lib_malloc_pages() and subtracts it from runtime->dma_bytes
afterward:

sound/pci/emu10k1/emupcm.c:snd_emu10k1_playback_hw_params() {
	...
	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 subsequent same-size OSS hw_params call during mmap (such as
changing rate or channels), alloc_size is runtime->dma_bytes +
EMUPAGESIZE.  Even with commit "ALSA: pcm: Don't reallocate for the same
sized buffers" checking runtime->dma_bytes == size ahead of this check,
execution still hits -EBADFD instead of reusing runtime->dma_buffer_p.

Because snd_emu10k1_pcm_channel_alloc() has already reallocated
epcm->voices and snd_pcm_hw_params() skips hw_free cleanup while
mmap_count is non-zero, the stream is left with mismatched voice
allocations and runtime->oss.params stuck set until munmap().

> @@ -488,6 +491,8 @@ int snd_pcm_lib_free_pages(struct snd_pcm_substream *substream)
>  	runtime = substream->runtime;
>  	if (runtime->dma_area == NULL)
>  		return 0;
> +	if (atomic_read(&substream->mmap_count))
> +		return -EBADFD;
>  	if (runtime->dma_buffer_p != &substream->dma_buffer) {
>  		struct snd_card *card = substream->pcm->card;
>  

[Severity: Medium]
Can returning -EBADFD here cause cs46xx to permanently leak dynamically
allocated DMA buffers when an OSS client changes fragment counts while
mmapped?

snd_cs46xx_playback_hw_params() ignores the return value of
snd_pcm_lib_free_pages() before overwriting runtime->dma_buffer_p with
&cpcm->hw_buf, and clears the runtime buffer before calling
snd_pcm_lib_malloc_pages() on the reverse path:

sound/pci/cs46xx/cs46xx_lib.c:snd_cs46xx_playback_hw_params() {
	...
	if (params_periods(hw_params) == CS46XX_FRAGS) {
		if (runtime->dma_area != cpcm->hw_buf.area)
			snd_pcm_lib_free_pages(substream);
		snd_pcm_set_runtime_buffer(substream, &cpcm->hw_buf);
	...
	} else {
		if (runtime->dma_area == cpcm->hw_buf.area)
			snd_pcm_set_runtime_buffer(substream, NULL);
		err = snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(hw_params));
		if (err < 0)
			return err;
	...
}

When an OSS client keeps buffer_bytes unchanged and switches to
CS46XX_FRAGS periods during mmap, snd_pcm_lib_free_pages() fails without
freeing runtime->dma_buffer_p.  Overwriting it leaks the snd_dma_buffer,
its DMA pages, and card->total_pcm_alloc_bytes accounting.

On the reverse transition away from hw_buf, clearing the runtime buffer
makes snd_pcm_lib_malloc_pages() return -EBADFD even with commit "ALSA:
pcm: Don't reallocate for the same sized buffers", leaving the mmapped
stream with NULL dma_area and zero dma_bytes.

snd_cs46xx_capture_hw_params() has the same two issues.

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

  reply	other threads:[~2026-12-03  9:10 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 [this message]
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-247432@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