All of lore.kernel.org
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Takashi Iwai" <tiwai@suse.de>, linux-sound@vger.kernel.org
Subject: Re: [PATCH v3 3/3] ALSA: pcm: Prohibit buffer (re-)allocation/free during mmap
Date: Thu, 03 Dec 2026 12:00:02 +0000	[thread overview]
Message-ID: <sashiko-outbox-247688@kernel.org> (raw)
In-Reply-To: <20261203113424.1222171-4-tiwai@suse.de>

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

commit 1cac93d127922c61e69e21736ae3127c9af966f3
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 while the substream
buffer is 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 break OSS mmap hw_params updates on drivers that do not use
managed_buffer_alloc?

snd_pcm_hw_params() still calls ops->hw_params() for post-mmap OSS
updates (such as SNDCTL_DSP_SPEED) when format, channels, period_size,
and buffer_size are unchanged.  Non-managed drivers like emu10k1,
cs46xx, and mt8365-afe-pcm call snd_pcm_lib_malloc_pages() there.

Previously, those calls reused runtime->dma_buffer_p without reallocating.
Failing with -EBADFD before that check leaves earlier ops->hw_params()
changes unrolled back (snd_pcm_hw_params() skips hw_free during mmap) and
keeps OSS ioctls returning -EBADFD until munmap().

Would checking mmap_count only when reallocation is needed (after the
runtime->dma_buffer_p->bytes >= size check, without shrinking
runtime->dma_bytes while mmapped) avoid this regression?

> @@ -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 leak runtime->dma_buffer_p and its DMA pages
in snd_cs46xx_capture_hw_params()?

sound/pci/cs46xx/cs46xx_lib.c:snd_cs46xx_capture_hw_params() {
	...
	if (runtime->periods == CS46XX_FRAGS) {
		if (runtime->dma_area != chip->capt.hw_buf.area)
			snd_pcm_lib_free_pages(substream);
		snd_pcm_set_runtime_buffer(substream, &chip->capt.hw_buf);
		substream->ops = &snd_cs46xx_capture_ops;
	} else {
		if (runtime->dma_area == chip->capt.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;
		substream->ops = &snd_cs46xx_capture_indirect_ops;
	}
	...
}

Because snd_cs46xx_capture_hw_params() checks stale runtime->periods, an
initial setup with periods == CS46XX_FRAGS allocates in the else branch.
A later post-mmap OSS hw_params update takes the CS46XX_FRAGS branch,
ignores -EBADFD from snd_pcm_lib_free_pages(), and overwrites the buffer.

That swaps runtime->dma_area under an active mmap and, for a dynamically
allocated buffer (such as preallocate_dma=0), permanently leaks
struct snd_dma_buffer, its DMA pages, and card->total_pcm_alloc_bytes.

In the else branch, if runtime->dma_area == chip->capt.hw_buf.area,
snd_pcm_set_runtime_buffer(substream, NULL) also clears the buffer before
snd_pcm_lib_malloc_pages() fails with -EBADFD, leaving a live mmapped
stream with NULL runtime->dma_area and zero runtime->dma_bytes.

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

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

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-12-03 11:34 [PATCH v3 0/3] ALSA: pcm: Workarounds for OSS mmap issues Takashi Iwai
2026-12-03 11:34 ` [PATCH v3 1/3] ALSA: pcm: Avoid forcibly changing to OPEN state at hw_params errors Takashi Iwai
2026-12-03 12:09   ` sashiko-bot
2026-12-03 11:34 ` [PATCH v3 2/3] ALSA: pcm: Reject hw_params with buffer size changes while mmapped Takashi Iwai
2026-12-03 11:34 ` [PATCH v3 3/3] ALSA: pcm: Prohibit buffer (re-)allocation/free during mmap Takashi Iwai
2026-12-03 12:00   ` 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-247688@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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.