Linux Sound subsystem development
 help / color / mirror / Atom feed
* [PATCH v2 0/4] ALSA: pcm: Workarounds for OSS mmap issues
@ 2026-12-03  8:40 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
                   ` (3 more replies)
  0 siblings, 4 replies; 9+ messages in thread
From: Takashi Iwai @ 2026-12-03  8:40 UTC (permalink / raw)
  To: linux-sound; +Cc: Ahmed Abdelmoemen

This is a revised series of fixes for attemping to paper over the
issues with OSS mmap.  I put the link to Ahmed's patch set in Link tag
for pointing to the actual issues, as well as his Reported-by tag.

v2 addressed a couple of issues reported by Sashiko, too.


Takashi


v1->v2: tag updates
	fix stale runtime->buffer_changed at hw_params
	check runtime->dma_area for a shortcut in snd_pcm_lib_malloc_pages()
===

Takashi Iwai (4):
  ALSA: pcm: Avoid forcibly changing to OPEN state at hw_params errors
  ALSA: pcm: Reject hw_params with buffer size changes while mmapped
  ALSA: pcm: Prohibit buffer (re-)allocation/free during mmap
  ALSA: pcm: Don't reallocate for the same sized buffers

 sound/core/pcm_memory.c |  8 ++++++++
 sound/core/pcm_native.c | 18 ++++++++++++++----
 2 files changed, 22 insertions(+), 4 deletions(-)


base-commit: 9b7d2f4e334e15ed0b854bff18b7e6bcbe89f847
-- 
2.53.0


^ permalink raw reply	[flat|nested] 9+ messages in thread

* [PATCH v2 1/4] ALSA: pcm: Avoid forcibly changing to OPEN state at hw_params errors
  2026-12-03  8:40 [PATCH v2 0/4] ALSA: pcm: Workarounds for OSS mmap issues Takashi Iwai
@ 2026-12-03  8:40 ` 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
                   ` (2 subsequent siblings)
  3 siblings, 1 reply; 9+ messages in thread
From: Takashi Iwai @ 2026-12-03  8:40 UTC (permalink / raw)
  To: linux-sound; +Cc: Ahmed Abdelmoemen

When an error happens at snd_pcm_hw_params() call, it tried to degrade
to SNDRV_PCM_STATE_OPEN, forcibly call hw_free and release memories.
This, however, shouldn't be done when the buffer is mmapped (happening
only in OSS); otherwise it may point to stray pages.

Add a check of the mmap at the error path and skips the state change
and the buffer release when mmap is used.  It'll be released at a
later point (at latest at releasing the file) in anyway.

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>
---
 sound/core/pcm_native.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/sound/core/pcm_native.c b/sound/core/pcm_native.c
index 897975e43a9b..6d17be7f9beb 100644
--- a/sound/core/pcm_native.c
+++ b/sound/core/pcm_native.c
@@ -893,7 +893,7 @@ static int snd_pcm_hw_params(struct snd_pcm_substream *substream,
 
 	err = 0;
  _error:
-	if (err) {
+	if (err && !atomic_read(&substream->mmap_count)) {
 		/* hardware might be unusable from this time,
 		 * so we force application to retry to set
 		 * the correct hardware parameter settings
-- 
2.53.0


^ permalink raw reply related	[flat|nested] 9+ messages in thread

* [PATCH v2 2/4] ALSA: pcm: Reject hw_params with buffer size changes while mmapped
  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  8:40 ` 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  8:40 ` [PATCH v2 4/4] ALSA: pcm: Don't reallocate for the same sized buffers Takashi Iwai
  3 siblings, 1 reply; 9+ messages in thread
From: Takashi Iwai @ 2026-12-03  8:40 UTC (permalink / raw)
  To: linux-sound; +Cc: Ahmed Abdelmoemen

Due to its design, OSS emulation allow switching the access type, etc,
during the buffer is mmapped, but the actual buffer size shouldn't
change; when it's changed and the buffer is re-allocated, the mapped
memory may point to a bogus page.

Add a sanity check to restrict the buffer size change with the mmap,
and avoid the buffer reallocation when mmapped.

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>
---
 sound/core/pcm_native.c | 16 +++++++++++++---
 1 file changed, 13 insertions(+), 3 deletions(-)

diff --git a/sound/core/pcm_native.c b/sound/core/pcm_native.c
index 6d17be7f9beb..7f0c47379c96 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,18 @@ 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;
+		}
+		runtime->buffer_changed = 0;
+	} 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;
-- 
2.53.0


^ permalink raw reply related	[flat|nested] 9+ messages in thread

* [PATCH v2 3/4] ALSA: pcm: Prohibit buffer (re-)allocation/free during mmap
  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  8:40 ` [PATCH v2 2/4] ALSA: pcm: Reject hw_params with buffer size changes while mmapped Takashi Iwai
@ 2026-12-03  8:40 ` 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
  3 siblings, 1 reply; 9+ messages in thread
From: Takashi Iwai @ 2026-12-03  8:40 UTC (permalink / raw)
  To: linux-sound; +Cc: Ahmed Abdelmoemen

It's not right to (re-)allocate or free the PCM buffer while it's
still being mapped.  Add sanity checks in snd_pcm_lib_malloc_pages()
and *_free_pages() helpers for more safety.

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>
---
 sound/core/pcm_memory.c | 5 +++++
 1 file changed, 5 insertions(+)

diff --git a/sound/core/pcm_memory.c b/sound/core/pcm_memory.c
index cfe9f10e7359..7aaa45f3bac1 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
@@ -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;
 
-- 
2.53.0


^ permalink raw reply related	[flat|nested] 9+ messages in thread

* [PATCH v2 4/4] ALSA: pcm: Don't reallocate for the same sized buffers
  2026-12-03  8:40 [PATCH v2 0/4] ALSA: pcm: Workarounds for OSS mmap issues Takashi Iwai
                   ` (2 preceding siblings ...)
  2026-12-03  8:40 ` [PATCH v2 3/4] ALSA: pcm: Prohibit buffer (re-)allocation/free during mmap Takashi Iwai
@ 2026-12-03  8:40 ` Takashi Iwai
  2026-12-03  9:09   ` sashiko-bot
  3 siblings, 1 reply; 9+ messages in thread
From: Takashi Iwai @ 2026-12-03  8:40 UTC (permalink / raw)
  To: linux-sound; +Cc: Ahmed Abdelmoemen

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>
---
 sound/core/pcm_memory.c | 3 +++
 1 file changed, 3 insertions(+)

diff --git a/sound/core/pcm_memory.c b/sound/core/pcm_memory.c
index 7aaa45f3bac1..2c610dc83044 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;
 
-- 
2.53.0


^ permalink raw reply related	[flat|nested] 9+ messages in thread

* Re: [PATCH v2 4/4] ALSA: pcm: Don't reallocate for the same sized buffers
  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
  0 siblings, 0 replies; 9+ messages in thread
From: sashiko-bot @ 2026-12-03  9:09 UTC (permalink / raw)
  To: Takashi Iwai, linux-sound

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

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [PATCH v2 1/4] ALSA: pcm: Avoid forcibly changing to OPEN state at hw_params errors
  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
  0 siblings, 0 replies; 9+ messages in thread
From: sashiko-bot @ 2026-12-03  9:09 UTC (permalink / raw)
  To: Takashi Iwai, linux-sound

Thank you for your contribution! Sashiko AI review found 1 potential
issue to consider:
- [High] ALSA: pcm: keeping SETUP state on mmapped hw_params() error
  breaks prepare()
--

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

ALSA: pcm: Avoid forcibly changing to OPEN state at hw_params errors

This commit skips transitioning the stream to SNDRV_PCM_STATE_OPEN, calling
hw_free, and freeing pages on snd_pcm_hw_params() errors when
substream->mmap_count is non-zero.

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 897975e43a9bd..6d17be7f9beb8 100644
> --- a/sound/core/pcm_native.c
> +++ b/sound/core/pcm_native.c
> @@ -893,7 +893,7 @@ static int snd_pcm_hw_params(struct snd_pcm_substream *substream,
>  
>  	err = 0;
>   _error:
> -	if (err) {
> +	if (err && !atomic_read(&substream->mmap_count)) {
>  		/* hardware might be unusable from this time,
>  		 * so we force application to retry to set
>  		 * the correct hardware parameter settings

[Severity: High]
Can skipping the transition to SNDRV_PCM_STATE_OPEN here allow
ops->prepare() to run on hardware state torn down by a failed
ops->hw_params()?

On a mapped OSS stream, snd_pcm_oss_make_ready() checks runtime->oss.params
and runtime->oss.prepare without holding runtime->oss.params_lock, then
acquires the lock and calls snd_pcm_oss_prepare() without rechecking
runtime->oss.params:

CPU0                              CPU1
snd_pcm_oss_post()
  snd_pcm_oss_make_ready()
    check oss.params (0)
    check oss.prepare (1)
                                  snd_pcm_oss_set_rate()
                                    snd_pcm_oss_change_params_locked()
                                      snd_pcm_hw_params()
                                        ops->hw_params() fails
                                        (state stays SETUP)
    lock(&oss.params_lock)
    snd_pcm_oss_prepare()
      ops->prepare()              <-- runs on torn-down hardware

Previously, snd_pcm_hw_params() moved the stream to SNDRV_PCM_STATE_OPEN on
error, so snd_pcm_pre_prepare() rejected the call with -EBADFD.  With the
stream left in SNDRV_PCM_STATE_SETUP, snd_pcm_pre_prepare() succeeds and
invokes ops->prepare() after the driver already released its resources.

For example, when snd_emu10k1_playback_hw_params() fails in
snd_emu10k1_pcm_channel_alloc() after freeing voices, the subsequent
snd_emu10k1_playback_prepare() dereferences NULL epcm->voices[0] in
snd_emu10k1_pcm_init_voice().

Should snd_pcm_oss_make_ready() check runtime->oss.params under
runtime->oss.params_lock and the PCM core still prevent ops->prepare() after
a failed ops->hw_params() when mmap_count is non-zero?

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

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [PATCH v2 3/4] ALSA: pcm: Prohibit buffer (re-)allocation/free during mmap
  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
  0 siblings, 0 replies; 9+ messages in thread
From: sashiko-bot @ 2026-12-03  9:10 UTC (permalink / raw)
  To: Takashi Iwai, linux-sound

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

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [PATCH v2 2/4] ALSA: pcm: Reject hw_params with buffer size changes while mmapped
  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
  0 siblings, 0 replies; 9+ messages in thread
From: sashiko-bot @ 2026-12-03  9:23 UTC (permalink / raw)
  To: Takashi Iwai, linux-sound

Thank you for your contribution! Sashiko AI review found 1 potential
issue to consider:
- [Medium] ASoC: __soc_pcm_hw_params() runs hw_free rollback while PCM
  is mmapped
--

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

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

This commit rejects snd_pcm_hw_params() calls with -EBADFD when a
substream is mmapped and the requested buffer size differs from the
current one, and skips managed buffer reallocation while 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 6d17be7f9beb8..7f0c47379c96e 100644
> --- a/sound/core/pcm_native.c
> +++ b/sound/core/pcm_native.c

[ ... ]

> @@ -826,9 +827,18 @@ 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;
> +		}
> +		runtime->buffer_changed = 0;
> +	} 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: Medium]
Can continuing to call substream->ops->hw_params() while mmapped leave
ASoC driver state torn down while runtime->state stays in
SNDRV_PCM_STATE_SETUP?

When substream->mmap_count is non-zero and buffer_bytes is unchanged,
snd_pcm_hw_params() still invokes substream->ops->hw_params().  On ASoC,
if __soc_pcm_hw_params() fails (for example on an OSS SNDCTL_DSP_SPEED
change), it unconditionally calls soc_pcm_hw_clean(rtd, substream, 1)
without checking substream->mmap_count.  Because the initial pre-mmap
hw_params already marked the links, components, and DAIs, rollback
executes their hw_free callbacks while the substream is still mmapped.

Combined with commit "ALSA: pcm: Avoid forcibly changing to OPEN state
at hw_params errors", the _error path in snd_pcm_hw_params() leaves
runtime->state in SNDRV_PCM_STATE_SETUP when mmap_count is non-zero.  A
concurrent thread in snd_pcm_oss_make_ready() that passed the unlocked
runtime->oss.params check can then prepare and start the stream after
ASoC has already torn down its resources:

CPU0                                CPU1
                                    snd_pcm_oss_make_ready()
                                      if (runtime->oss.params) /* false */
SNDCTL_DSP_SPEED (mmapped)
  snd_pcm_hw_params()
    __soc_pcm_hw_params() -> err
      soc_pcm_hw_clean(..., 1)
        __avs_dai_fe_hw_free()
          data->path = NULL
    /* mmap_count > 0: state stays
     * SNDRV_PCM_STATE_SETUP */
                                      mutex_lock(&runtime->oss.params_lock)
                                      snd_pcm_oss_prepare()
                                        avs_dai_prepare() -> 0 (!data->path)
                                      SNDRV_PCM_IOCTL_START
                                        avs_path_pause(NULL)  <-- NULL deref

On Intel AVS this results in a kernel NULL pointer dereference in
avs_path_pause() at path->owner; on drivers such as Samsung IDMA,
rollback directly clears runtime->dma_area and runtime->dma_bytes via
snd_pcm_set_runtime_buffer(substream, NULL), causing a racing prepare
to program DMA with a zero transfer size.

Should __soc_pcm_hw_params() skip soc_pcm_hw_clean() when
substream->mmap_count is non-zero, or should driver hw_params callbacks
be avoided while mmapped?

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

^ permalink raw reply	[flat|nested] 9+ messages in thread

end of thread, other threads:[~2026-12-03  9:23 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
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  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

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox