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

Yet another respin of a series of fixes for attemping to paper over
the issues with OSS mmap.

v3 addressed a couple of issues reported by Sashiko, and dropped one
patch that turned out to be harmful.


Takashi

v1: https://lore.kernel.org/20261202155431.720872-1-tiwai@suse.de
v2: https://lore.kernel.org/20261203084041.914026-1-tiwai@suse.de

v2->v3: add the check of runtime->oss.params in snd_pcm_oss_prepare()
	more strict checks of buffer changes in hw_params for OSS mmap
	drop patch#4 (ALSA: pcm: Don't reallocate for the same sized buffers)

===

Takashi Iwai (3):
  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

 sound/core/oss/pcm_oss.c |  4 ++++
 sound/core/pcm_memory.c  |  5 +++++
 sound/core/pcm_native.c  | 16 ++++++++++++++--
 3 files changed, 23 insertions(+), 2 deletions(-)


base-commit: 9b7d2f4e334e15ed0b854bff18b7e6bcbe89f847
-- 
2.53.0


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

* [PATCH v3 1/3] ALSA: pcm: Avoid forcibly changing to OPEN state at hw_params errors
  2026-12-03 11:34 [PATCH v3 0/3] ALSA: pcm: Workarounds for OSS mmap issues Takashi Iwai
@ 2026-12-03 11:34 ` 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
  2 siblings, 1 reply; 6+ messages in thread
From: Takashi Iwai @ 2026-12-03 11:34 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.

Since there is a small window in snd_pcm_make_ready() between
snd_pcm_oss_change_param() and snd_pcm_oss_prepare(), an additional
check is put in snd_pcm_oss_prepare() to catch the failing hw_params
in the case above, too.

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/oss/pcm_oss.c | 4 ++++
 sound/core/pcm_native.c  | 2 +-
 2 files changed, 5 insertions(+), 1 deletion(-)

diff --git a/sound/core/oss/pcm_oss.c b/sound/core/oss/pcm_oss.c
index 3c2ac8966d5e..35a5fa67d6f9 100644
--- a/sound/core/oss/pcm_oss.c
+++ b/sound/core/oss/pcm_oss.c
@@ -1146,6 +1146,10 @@ static int snd_pcm_oss_prepare(struct snd_pcm_substream *substream)
 	int err;
 	struct snd_pcm_runtime *runtime = substream->runtime;
 
+	/* previous hw_params failed? */
+	if (runtime->oss.params)
+		return -EBADFD;
+
 	err = snd_pcm_kernel_ioctl(substream, SNDRV_PCM_IOCTL_PREPARE, NULL);
 	if (err < 0) {
 		pcm_dbg(substream->pcm,
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] 6+ messages in thread

* [PATCH v3 2/3] ALSA: pcm: Reject hw_params with buffer size changes while mmapped
  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 11:34 ` Takashi Iwai
  2026-12-03 11:34 ` [PATCH v3 3/3] ALSA: pcm: Prohibit buffer (re-)allocation/free during mmap Takashi Iwai
  2 siblings, 0 replies; 6+ messages in thread
From: Takashi Iwai @ 2026-12-03 11:34 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 shouldn't change;
when its is changed and the buffer is re-allocated, the mapped memory
may point to a bogus page.  Even a period size or number of periods
shouldn't be changed, either, as this can influence on the buffer
allocation in the driver side, too (e.g. cs46xx driver).

Add a sanity check to restrict the buffer changes 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 | 14 +++++++++++++-
 1 file changed, 13 insertions(+), 1 deletion(-)

diff --git a/sound/core/pcm_native.c b/sound/core/pcm_native.c
index 6d17be7f9beb..4b410061883a 100644
--- a/sound/core/pcm_native.c
+++ b/sound/core/pcm_native.c
@@ -826,7 +826,19 @@ static int snd_pcm_hw_params(struct snd_pcm_substream *substream,
 	if (err < 0)
 		goto _error;
 
-	if (substream->managed_buffer_alloc) {
+	/* OSS still allows hw_params change during mmap -- except for the case
+	 * where the buffer needs reallocation (that can go wrong with mmap)
+	 */
+	if (atomic_read(&substream->mmap_count)) {
+		if (params_format(params) != runtime->format ||
+		    params_channels(params) != runtime->channels ||
+		    params_period_size(params) != runtime->period_size ||
+		    params_buffer_size(params) != runtime->buffer_size) {
+			err = -EBADFD;
+			goto _error;
+		}
+		runtime->buffer_changed = 0;
+	} else if (substream->managed_buffer_alloc) {
 		err = snd_pcm_lib_malloc_pages(substream,
 					       params_buffer_bytes(params));
 		if (err < 0)
-- 
2.53.0


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

* [PATCH v3 3/3] ALSA: pcm: Prohibit buffer (re-)allocation/free during mmap
  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 11:34 ` [PATCH v3 2/3] ALSA: pcm: Reject hw_params with buffer size changes while mmapped Takashi Iwai
@ 2026-12-03 11:34 ` Takashi Iwai
  2026-12-03 12:00   ` sashiko-bot
  2 siblings, 1 reply; 6+ messages in thread
From: Takashi Iwai @ 2026-12-03 11:34 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] 6+ messages in thread

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

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

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

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

Thank you for your contribution! Sashiko AI review found 1 potential
issue to consider:
- [Medium] ALSA: pcm: snd_pcm_oss_make_ready() lock gap causes spurious
  -EBADFD
--

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

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

This commit skips the state reset and buffer release on snd_pcm_hw_params()
errors when the substream buffer is mmapped, and adds a runtime->oss.params
check in snd_pcm_oss_prepare() to catch failed hw_params calls.

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

> diff --git a/sound/core/oss/pcm_oss.c b/sound/core/oss/pcm_oss.c
> index 3c2ac8966d5e3..35a5fa67d6f9b 100644
> --- a/sound/core/oss/pcm_oss.c
> +++ b/sound/core/oss/pcm_oss.c
> @@ -1146,6 +1146,10 @@ static int snd_pcm_oss_prepare(struct snd_pcm_substream *substream)
>  	int err;
>  	struct snd_pcm_runtime *runtime = substream->runtime;
>  
> +	/* previous hw_params failed? */
> +	if (runtime->oss.params)
> +		return -EBADFD;
> +
>  	err = snd_pcm_kernel_ioctl(substream, SNDRV_PCM_IOCTL_PREPARE, NULL);
>  	if (err < 0) {
>  		pcm_dbg(substream->pcm,

[Severity: Medium]
Can a concurrent valid parameter-change ioctl set runtime->oss.params here
and cause snd_pcm_oss_prepare() to return a spurious -EBADFD?

snd_pcm_oss_make_ready() drops runtime->oss.params_lock between
snd_pcm_oss_change_params() and snd_pcm_oss_prepare(), so a concurrent
ioctl such as snd_pcm_oss_set_rate() can set runtime->oss.params = 1 in
that window:

CPU0                                 CPU1
snd_pcm_oss_make_ready()
  snd_pcm_oss_change_params()
    /* drops params_lock */
                                     snd_pcm_oss_set_rate()
                                       lock_params(runtime)
                                       runtime->oss.params = 1
                                       unlock(&runtime->oss.params_lock)
  lock(&runtime->oss.params_lock)
  snd_pcm_oss_prepare()
    if (runtime->oss.params)
      return -EBADFD;  <-- spurious error

Callers of snd_pcm_oss_make_ready() such as SNDCTL_DSP_SETTRIGGER then fail
with -EBADFD instead of applying the updated parameters, and in
snd_pcm_oss_poll() this aborts capture retrigger after
need_input_retrigger() has already cleared runtime->oss.trigger.

Checking (runtime->oss.params || runtime->oss.prepare) and acquiring
runtime->oss.params_lock once in snd_pcm_oss_make_ready() to call
snd_pcm_oss_make_ready_locked() would close the window and apply concurrent
parameter updates cleanly.

Separately, the commit message misspells snd_pcm_oss_make_ready() as
snd_pcm_make_ready() and snd_pcm_oss_change_params() as
snd_pcm_oss_change_param().

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

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

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

Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
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 is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox