Linux Sound subsystem development
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Takashi Iwai" <tiwai@suse.de>, linux-sound@vger.kernel.org
Subject: Re: [PATCH 1/2] ALSA: pcm: Simplify PCM buffer access gate with auto-cleanup helpers
Date: Wed, 18 Nov 2026 14:05:57 +0000	[thread overview]
Message-ID: <sashiko-outbox-221145@kernel.org> (raw)
In-Reply-To: <20261118135558.1928160-1-tiwai@suse.de>

Thank you for your contribution! Sashiko AI review found no new issues
in this patch, but noticed 1 related pre-existing issue it does not
introduce:
- [High] ALSA: pcm: ABBA buffer_mutex deadlock in snd_pcm_action_group()
  [1]
--

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

ALSA: pcm: Simplify PCM buffer access gate with auto-cleanup helpers

This commit introduces helper functions and auto-cleanup macros for the
runtime->buffer_accessing gate and replaces open-coded
atomic_inc_unless_negative() and atomic_dec() pairs with them.

> diff --git a/sound/core/pcm_local.h b/sound/core/pcm_local.h
> index 4b134d435c24f..7762c89e0f9ff 100644
> --- a/sound/core/pcm_local.h
> +++ b/sound/core/pcm_local.h
> @@ -86,4 +86,30 @@ static inline void snd_pcm_dma_buffer_sync(struct snd_pcm_substream *substream,
>  #define is_oss_stream(substream)	false
>  #endif
>  
> +/*
> + * helpers for restricting hw_params / hw_refine changes
> + */
> +static inline int snd_pcm_buffer_access_get(struct snd_pcm_runtime *runtime)
> +{
> +	if (!atomic_inc_unless_negative(&runtime->buffer_accessing))
> +		return -EBUSY;
> +	return 0;
> +}

[Severity: High]
This problem wasn't introduced by this patch, but on the paired writer side
[1] can concurrent non-atomic group actions on two substreams in the
same linked group cause an ABBA deadlock on runtime->buffer_mutex, while
leaving runtime->buffer_accessing open on the other linked substreams?

sound/core/pcm_native.c:snd_pcm_action_nonatomic() {
	guard(rwsem_read)(&snd_pcm_link_rwsem);
	res = snd_pcm_buffer_access_lock(substream->runtime);
	if (res < 0)
		return res;
	if (snd_pcm_stream_linked(substream))
		res = snd_pcm_action_group(ops, substream, state, false);
	...
}

sound/core/pcm_native.c:snd_pcm_action_group() {
	snd_pcm_group_for_each_entry(s, substream) {
		if (s != substream) {
			if (!stream_lock)
				mutex_lock_nested(&s->runtime->buffer_mutex, depth);
	...
}

snd_pcm_action_nonatomic() only takes a read lock on snd_pcm_link_rwsem,
locks the caller's own runtime->buffer_mutex first via
snd_pcm_buffer_access_lock(), and then locks the remaining group members in
snd_pcm_action_group():

CPU0                                   CPU1
----                                   ----
down_read(&snd_pcm_link_rwsem)
                                       down_read(&snd_pcm_link_rwsem)
lock(&A->runtime->buffer_mutex)
                                       lock(&B->runtime->buffer_mutex)
lock(&B->runtime->buffer_mutex)
                                       lock(&A->runtime->buffer_mutex)

 *** DEADLOCK ***

CPU0: snd_pcm_prepare(A) -> snd_pcm_action_nonatomic() -> snd_pcm_action_group()
CPU1: snd_pcm_prepare(B) -> snd_pcm_action_nonatomic() -> snd_pcm_action_group()

Because both deadlocked tasks hold snd_pcm_link_rwsem for reading, any
later attempt to link, unlink, or close a linked PCM stream across the
system also hangs waiting for the write lock.

In addition, snd_pcm_action_group() only takes s->runtime->buffer_mutex on
the other linked substreams instead of calling
snd_pcm_buffer_access_lock(), leaving s->runtime->buffer_accessing unset on
those substreams during prepare or reset.

[1] https://sashiko.dev/#/bug/linux-a75ec171-40b4-4d70-8ffc-fddd01a5c9ca

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

      parent reply	other threads:[~2026-11-18 14:05 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-18 13:55 [PATCH 1/2] ALSA: pcm: Simplify PCM buffer access gate with auto-cleanup helpers Takashi Iwai
2026-11-18 13:55 ` [PATCH 2/2] ALSA: pcm: Avoid hw_params/hw_refine changes during PCM buffer access Takashi Iwai
2026-11-18 14:29   ` sashiko-bot
2026-11-18 14:05 ` 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-221145@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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox