All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH 0/4] ALSA: pcm: Workarounds for OSS mmap issues
@ 2026-12-02 15:54 Takashi Iwai
  2026-12-02 15:54 ` [PATCH 1/4] ALSA: pcm: Avoid forcibly changing to OPEN state at hw_params errors Takashi Iwai
                   ` (3 more replies)
  0 siblings, 4 replies; 7+ messages in thread
From: Takashi Iwai @ 2026-12-02 15:54 UTC (permalink / raw)
  To: linux-sound; +Cc: Ahmed Abdelmoemen

Here is another patch set 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.

Ahmed, could you check whether this can cover enough for you?


thanks,

Takashi

===

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 | 17 +++++++++++++----
 2 files changed, 21 insertions(+), 4 deletions(-)

-- 
2.53.0


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

* [PATCH 1/4] ALSA: pcm: Avoid forcibly changing to OPEN state at hw_params errors
  2026-12-02 15:54 [PATCH 0/4] ALSA: pcm: Workarounds for OSS mmap issues Takashi Iwai
@ 2026-12-02 15:54 ` Takashi Iwai
  2026-12-02 15:54 ` [PATCH 2/4] ALSA: pcm: Reject hw_params with buffer size changes while mmapped Takashi Iwai
                   ` (2 subsequent siblings)
  3 siblings, 0 replies; 7+ messages in thread
From: Takashi Iwai @ 2026-12-02 15:54 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
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] 7+ messages in thread

* [PATCH 2/4] ALSA: pcm: Reject hw_params with buffer size changes while mmapped
  2026-12-02 15:54 [PATCH 0/4] ALSA: pcm: Workarounds for OSS mmap issues Takashi Iwai
  2026-12-02 15:54 ` [PATCH 1/4] ALSA: pcm: Avoid forcibly changing to OPEN state at hw_params errors Takashi Iwai
@ 2026-12-02 15:54 ` Takashi Iwai
  2026-12-02 16:11   ` sashiko-bot
  2026-12-02 15:54 ` [PATCH 3/4] ALSA: pcm: Prohibit buffer (re-)allocation/free during mmap Takashi Iwai
  2026-12-02 15:54 ` [PATCH 4/4] ALSA: pcm: Don't reallocate for the same sized buffers Takashi Iwai
  3 siblings, 1 reply; 7+ messages in thread
From: Takashi Iwai @ 2026-12-02 15:54 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
Signed-off-by: Takashi Iwai <tiwai@suse.de>
---
 sound/core/pcm_native.c | 15 ++++++++++++---
 1 file changed, 12 insertions(+), 3 deletions(-)

diff --git a/sound/core/pcm_native.c b/sound/core/pcm_native.c
index 6d17be7f9beb..7366788de367 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,17 @@ 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;
+		}
+	} 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] 7+ messages in thread

* [PATCH 3/4] ALSA: pcm: Prohibit buffer (re-)allocation/free during mmap
  2026-12-02 15:54 [PATCH 0/4] ALSA: pcm: Workarounds for OSS mmap issues Takashi Iwai
  2026-12-02 15:54 ` [PATCH 1/4] ALSA: pcm: Avoid forcibly changing to OPEN state at hw_params errors Takashi Iwai
  2026-12-02 15:54 ` [PATCH 2/4] ALSA: pcm: Reject hw_params with buffer size changes while mmapped Takashi Iwai
@ 2026-12-02 15:54 ` Takashi Iwai
  2026-12-02 15:54 ` [PATCH 4/4] ALSA: pcm: Don't reallocate for the same sized buffers Takashi Iwai
  3 siblings, 0 replies; 7+ messages in thread
From: Takashi Iwai @ 2026-12-02 15:54 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
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] 7+ messages in thread

* [PATCH 4/4] ALSA: pcm: Don't reallocate for the same sized buffers
  2026-12-02 15:54 [PATCH 0/4] ALSA: pcm: Workarounds for OSS mmap issues Takashi Iwai
                   ` (2 preceding siblings ...)
  2026-12-02 15:54 ` [PATCH 3/4] ALSA: pcm: Prohibit buffer (re-)allocation/free during mmap Takashi Iwai
@ 2026-12-02 15:54 ` Takashi Iwai
  2026-12-02 16:04   ` sashiko-bot
  3 siblings, 1 reply; 7+ messages in thread
From: Takashi Iwai @ 2026-12-02 15:54 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.

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..7418da669409 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_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] 7+ messages in thread

* Re: [PATCH 4/4] ALSA: pcm: Don't reallocate for the same sized buffers
  2026-12-02 15:54 ` [PATCH 4/4] ALSA: pcm: Don't reallocate for the same sized buffers Takashi Iwai
@ 2026-12-02 16:04   ` sashiko-bot
  0 siblings, 0 replies; 7+ messages in thread
From: sashiko-bot @ 2026-12-02 16:04 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: skipped buffer setup in snd_pcm_lib_malloc_pages()
--

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

ALSA: pcm: Don't reallocate for the same sized buffers

This commit skips buffer reallocation in snd_pcm_lib_malloc_pages() when
the requested buffer size matches runtime->dma_bytes.

> diff --git a/sound/core/pcm_memory.c b/sound/core/pcm_memory.c
> index 7aaa45f3bac14..7418da6694094 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_bytes == size)
> +		return 0; /* no need to change */
> +
>  	if (atomic_read(&substream->mmap_count))
>  		return -EBADFD;
>  

[Severity: High]
Can this skip snd_pcm_set_runtime_buffer() on the initial hw_params call
when a driver presets runtime->dma_bytes during open()?

Some drivers using managed buffers via snd_pcm_set_fixed_buffer_all()
initialize runtime->dma_bytes in their open callback while
runtime->dma_buffer_p and runtime->dma_area are still NULL:

sound/soc/qcom/lpass-platform.c:lpass_platform_pcmops_open() {
	...
		snd_soc_set_runtime_hwparams(substream, &lpass_platform_pcm_hardware);
		runtime->dma_bytes = lpass_platform_pcm_hardware.buffer_bytes_max;
		break;
	...
}

When userspace requests a buffer of that size in snd_pcm_hw_params(),
snd_pcm_lib_malloc_pages() now returns 0 before attaching
substream->dma_buffer to runtime.  That leaves runtime->dma_area NULL and
causes a NULL pointer dereference in lpass_platform_copy() during PCM data
transfers.

Should this check also require runtime->dma_buffer_p to be non-NULL?

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

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

* Re: [PATCH 2/4] ALSA: pcm: Reject hw_params with buffer size changes while mmapped
  2026-12-02 15:54 ` [PATCH 2/4] ALSA: pcm: Reject hw_params with buffer size changes while mmapped Takashi Iwai
@ 2026-12-02 16:11   ` sashiko-bot
  0 siblings, 0 replies; 7+ messages in thread
From: sashiko-bot @ 2026-12-02 16:11 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: unreachable mmap_count check in
  snd_pcm_hw_params()
- [Low] ALSA: pcm: stale runtime->buffer_changed in snd_pcm_hw_params()
  when mmapped
--

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

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

This commit restricts snd_pcm_hw_params() from changing the buffer size or
reallocating managed DMA buffers while the substream buffer is 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 8967a6f649343..d306340ae9190 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,17 @@ 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;
> +		}

[Severity: Medium]
Can execution ever reach this substream->mmap_count check with a non-zero
count?

At the start of snd_pcm_hw_params(), hw_params_state_check(substream, true)
is called while holding snd_pcm_buffer_access_lock(runtime), which prevents
concurrent mmaps from starting.  Since commit de57f471f664 ("ALSA: pcm:
Prevent PCM hw_params & co during mmap on OSS, too") removed the
!is_oss_stream(substream) exemption, hw_params_state_check()
unconditionally returns -EBADFD whenever substream->mmap_count is non-zero:

sound/core/pcm_native.c:hw_params_state_check() {
	...
	case SNDRV_PCM_STATE_OPEN:
	case SNDRV_PCM_STATE_SETUP:
	case SNDRV_PCM_STATE_PREPARED:
		if (check_mmap &&
		    atomic_read(&substream->mmap_count))
			return -EBADFD;
		return 0;
	...
}

As a result, OSS streams with an active mmap are rejected at the top of
snd_pcm_hw_params() even when the buffer size is unchanged, and this branch
is dead code.

> +	} 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: Low]
When the substream->mmap_count branch is taken (for example, on a stable
tree without de57f471f664 ("ALSA: pcm: Prevent PCM hw_params & co during
mmap on OSS, too") or if hw_params_state_check() is updated to allow OSS
streams), does skipping the managed_buffer_alloc branch leave
runtime->buffer_changed stale?

The initial pre-mmap allocation sets runtime->buffer_changed = 1, and
nothing clears it when snd_pcm_lib_malloc_pages() is skipped.  Driver
ops->hw_params callbacks that check runtime->buffer_changed (such as
sof_pcm_hw_params()) would then treat the unchanged mmapped buffer as
newly reallocated.

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

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

end of thread, other threads:[~2026-12-02 16:11 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-12-02 15:54 [PATCH 0/4] ALSA: pcm: Workarounds for OSS mmap issues Takashi Iwai
2026-12-02 15:54 ` [PATCH 1/4] ALSA: pcm: Avoid forcibly changing to OPEN state at hw_params errors Takashi Iwai
2026-12-02 15:54 ` [PATCH 2/4] ALSA: pcm: Reject hw_params with buffer size changes while mmapped Takashi Iwai
2026-12-02 16:11   ` sashiko-bot
2026-12-02 15:54 ` [PATCH 3/4] ALSA: pcm: Prohibit buffer (re-)allocation/free during mmap Takashi Iwai
2026-12-02 15:54 ` [PATCH 4/4] ALSA: pcm: Don't reallocate for the same sized buffers Takashi Iwai
2026-12-02 16:04   ` sashiko-bot

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.