[PATCH v2] ALSA: pcm: Serialize snd_pcm_suspend_all() with open_mutex

Cássio Gabriel posted 1 patch 6 days ago
sound/core/pcm_native.c | 6 +++++-
1 file changed, 5 insertions(+), 1 deletion(-)
[PATCH v2] ALSA: pcm: Serialize snd_pcm_suspend_all() with open_mutex
Posted by Cássio Gabriel 6 days ago
snd_pcm_suspend_all() walks all PCM substreams and uses a lockless
runtime check to skip closed streams. It then calls snd_pcm_suspend()
for each remaining substream and finally runs snd_pcm_sync_stop() in a
second pass.

The runtime lifetime is still controlled by pcm->open_mutex in the
open/release path. That means a concurrent close can clear or free
substream->runtime after the initial check in snd_pcm_suspend_all(),
leaving the later suspend or sync-stop path to dereference a stale or
NULL runtime pointer.

Serialize snd_pcm_suspend_all() with pcm->open_mutex so the runtime
pointer stays stable across both loops. This matches the existing PCM
runtime lifetime rule already used by other core paths that access
substream->runtime outside the stream lock.

Suggested-by: Takashi Iwai <tiwai@suse.com>
Signed-off-by: Cássio Gabriel <cassiogabrielcontato@gmail.com>
---
Changes in v2:
- Update funtion description (updates kerneldoc).
- Link to v1: https://lore.kernel.org/r/20260320-alsa-pcm-suspend-open-close-lock-v1-1-b5992a120335@gmail.com
---
 sound/core/pcm_native.c | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)

diff --git a/sound/core/pcm_native.c b/sound/core/pcm_native.c
index 674b50c7c5f6..a541bb235cfa 100644
--- a/sound/core/pcm_native.c
+++ b/sound/core/pcm_native.c
@@ -1780,6 +1780,9 @@ static int snd_pcm_suspend(struct snd_pcm_substream *substream)
  * snd_pcm_suspend_all - trigger SUSPEND to all substreams in the given pcm
  * @pcm: the PCM instance
  *
+ * Takes and releases pcm->open_mutex to serialize against
+ * concurrent open/close while walking the substreams.
+ *
  * After this call, all streams are changed to SUSPENDED state.
  *
  * Return: Zero if successful (or @pcm is %NULL), or a negative error code.
@@ -1792,8 +1795,9 @@ int snd_pcm_suspend_all(struct snd_pcm *pcm)
 	if (! pcm)
 		return 0;
 
+	guard(mutex)(&pcm->open_mutex);
+
 	for_each_pcm_substream(pcm, stream, substream) {
-		/* FIXME: the open/close code should lock this as well */
 		if (!substream->runtime)
 			continue;
 

---
base-commit: 2b04091afcbd212e2e0c994d3262b0c61b1778f0
change-id: 20260320-alsa-pcm-suspend-open-close-lock-56907d40df44

Best regards,
--  
Cássio Gabriel <cassiogabrielcontato@gmail.com>

Re: [PATCH v2] ALSA: pcm: Serialize snd_pcm_suspend_all() with open_mutex
Posted by Takashi Iwai 6 days ago
On Fri, 27 Mar 2026 14:59:45 +0100,
Cássio Gabriel wrote:
> 
> snd_pcm_suspend_all() walks all PCM substreams and uses a lockless
> runtime check to skip closed streams. It then calls snd_pcm_suspend()
> for each remaining substream and finally runs snd_pcm_sync_stop() in a
> second pass.
> 
> The runtime lifetime is still controlled by pcm->open_mutex in the
> open/release path. That means a concurrent close can clear or free
> substream->runtime after the initial check in snd_pcm_suspend_all(),
> leaving the later suspend or sync-stop path to dereference a stale or
> NULL runtime pointer.
> 
> Serialize snd_pcm_suspend_all() with pcm->open_mutex so the runtime
> pointer stays stable across both loops. This matches the existing PCM
> runtime lifetime rule already used by other core paths that access
> substream->runtime outside the stream lock.
> 
> Suggested-by: Takashi Iwai <tiwai@suse.com>
> Signed-off-by: Cássio Gabriel <cassiogabrielcontato@gmail.com>
> ---
> Changes in v2:
> - Update funtion description (updates kerneldoc).
> - Link to v1: https://lore.kernel.org/r/20260320-alsa-pcm-suspend-open-close-lock-v1-1-b5992a120335@gmail.com

Applied to for-next branch now.  Thanks.


Takashi