From: Joonho Lee <mynameisjune111@gmail.com>
To: linux-sound@vger.kernel.org
Cc: tiwai@suse.com, perex@perex.cz, anton.yakovlev@opensynergy.com,
mst@redhat.com, virtualization@lists.linux.dev,
linux-kernel@vger.kernel.org, corbet@lwn.net,
skhan@linuxfoundation.org, rdunlap@infradead.org,
linux-doc@vger.kernel.org, Joonho Lee <mynameisjune111@gmail.com>
Subject: [PATCH v29 05/17] ALSA: pcm: serialize linked buffer actions
Date: Thu, 12 Nov 2026 14:34:34 +0900 [thread overview]
Message-ID: <20261112053446.73504-6-mynameisjune111@gmail.com> (raw)
In-Reply-To: <20261112053446.73504-1-mynameisjune111@gmail.com>
Concurrent non-atomic actions on different members of a linked PCM group
currently lock their own buffer first, then the other members. That
acquisition order can differ between callers. Sibling buffer mutexes
also do not close their buffer-access admission counters.
Close buffer access for every member before taking any buffer mutex, and
acquire those mutexes in the stable linked-list order while group
membership is protected. Release every access gate on success and on
failure, including when a member already has an active buffer access.
The group action then uses the buffers already held by its caller.
The PCM stream and group locks are not held across sleeping driver
prepare callbacks. Single-stream non-atomic actions keep their existing
buffer-access locking path.
Fixes: 3c3201f8c7bb ("ALSA: pcm: Fix races among concurrent prepare and hw_params/hw_free calls")
Fixes: bc55cfd5718c ("ALSA: pcm: Fix potential AB/BA lock with buffer_mutex and mmap_lock")
Assisted-by: LLM
Signed-off-by: Joonho Lee <mynameisjune111@gmail.com>
---
sound/core/pcm_native.c | 71 +++++++++++++++++++++++++++++++++++++----------
1 file changed, 56 insertions(+), 15 deletions(-)
diff --git a/sound/core/pcm_native.c b/sound/core/pcm_native.c
--- a/sound/core/pcm_native.c
+++ b/sound/core/pcm_native.c
@@ -731,6 +731,44 @@
{
mutex_unlock(&runtime->buffer_mutex);
atomic_inc(&runtime->buffer_accessing);
+}
+
+/* Gate all linked buffers before taking their mutexes in group-list order. */
+static int snd_pcm_group_buffer_access_lock(struct snd_pcm_substream *substream)
+{
+ struct snd_pcm_substream *s, *s1;
+ int depth = 0;
+
+ snd_pcm_group_for_each_entry(s, substream) {
+ if (!atomic_dec_unless_positive(&s->runtime->buffer_accessing)) {
+ snd_pcm_group_for_each_entry(s1, substream) {
+ if (s1 == s)
+ break;
+ atomic_inc(&s1->runtime->buffer_accessing);
+ }
+ return -EBUSY;
+ }
+ }
+
+ snd_pcm_group_for_each_entry(s, substream) {
+ if (depth == 0)
+ mutex_lock(&s->runtime->buffer_mutex);
+ else
+ mutex_lock_nested(&s->runtime->buffer_mutex, depth);
+ depth++;
+ }
+
+ return 0;
+}
+
+static void snd_pcm_group_buffer_access_unlock(struct snd_pcm_substream *substream)
+{
+ struct snd_pcm_substream *s;
+
+ list_for_each_entry_reverse(s, &substream->group->substreams, link_list)
+ mutex_unlock(&s->runtime->buffer_mutex);
+ snd_pcm_group_for_each_entry(s, substream)
+ atomic_inc(&s->runtime->buffer_accessing);
}
/* fill the PCM buffer with the current silence format; called from pcm_oss.c */
@@ -1317,9 +1355,9 @@
};
/*
- * this functions is core for handling of linked stream
- * Note: the stream state might be changed also on failure
- * Note2: call with calling stream lock + link lock
+ * Handle an action on all linked streams.
+ * When stream_lock is true, the caller holds the calling stream and group
+ * locks. Otherwise, the caller holds link_rwsem and every group buffer_mutex.
*/
static int snd_pcm_action_group(const struct action_ops *ops,
struct snd_pcm_substream *substream,
@@ -1331,10 +1369,8 @@
int res = 0, depth = 1;
snd_pcm_group_for_each_entry(s, substream) {
- if (s != substream) {
- if (!stream_lock)
- mutex_lock_nested(&s->runtime->buffer_mutex, depth);
- else if (s->pcm->nonatomic)
+ if (s != substream && stream_lock) {
+ if (s->pcm->nonatomic)
mutex_lock_nested(&s->self_group.mutex, depth);
else
spin_lock_nested(&s->self_group.lock, depth);
@@ -1371,10 +1407,8 @@
_unlock:
/* unlock streams */
snd_pcm_group_for_each_entry(s1, substream) {
- if (s1 != substream) {
- if (!stream_lock)
- mutex_unlock(&s1->runtime->buffer_mutex);
- else if (s1->pcm->nonatomic)
+ if (s1 != substream && stream_lock) {
+ if (s1->pcm->nonatomic)
mutex_unlock(&s1->self_group.mutex);
else
spin_unlock(&s1->self_group.lock);
@@ -1512,18 +1546,25 @@
struct snd_pcm_substream *substream,
snd_pcm_state_t state)
{
+ bool linked;
int res;
/* Guarantee the group members won't change during non-atomic action */
guard(rwsem_read)(&snd_pcm_link_rwsem);
- res = snd_pcm_buffer_access_lock(substream->runtime);
+ linked = snd_pcm_stream_linked(substream);
+ if (linked)
+ res = snd_pcm_group_buffer_access_lock(substream);
+ else
+ res = snd_pcm_buffer_access_lock(substream->runtime);
if (res < 0)
return res;
- if (snd_pcm_stream_linked(substream))
+ if (linked) {
res = snd_pcm_action_group(ops, substream, state, false);
- else
+ snd_pcm_group_buffer_access_unlock(substream);
+ } else {
res = snd_pcm_action_single(ops, substream, state, false);
- snd_pcm_buffer_access_unlock(substream->runtime);
+ snd_pcm_buffer_access_unlock(substream->runtime);
+ }
return res;
}
next prev parent reply other threads:[~2026-11-12 5:35 UTC|newest]
Thread overview: 35+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-12 5:34 [PATCH v29 00/17] ALSA: virtio: preserve PCM progress Joonho Lee
2026-11-12 5:34 ` [PATCH v29 01/17] ALSA: pcm: mark stop-threshold accesses for capture policy sampling Joonho Lee
2026-11-12 5:39 ` sashiko-bot
2026-11-12 5:34 ` [PATCH v29 02/17] ALSA: pcm: report partial progress under the stream lock Joonho Lee
2026-11-12 5:46 ` sashiko-bot
2026-11-12 5:34 ` [PATCH v29 03/17] ALSA: pcm: add an optional reset operation " Joonho Lee
2026-11-12 5:44 ` sashiko-bot
2026-11-12 5:34 ` [PATCH v29 04/17] ALSA: pcm: notify drivers after linked prepare completes Joonho Lee
2026-11-12 5:53 ` sashiko-bot
2026-11-12 5:34 ` Joonho Lee [this message]
2026-11-12 5:43 ` [PATCH v29 05/17] ALSA: pcm: serialize linked buffer actions sashiko-bot
2026-11-12 5:34 ` [PATCH v29 06/17] ALSA: virtio: propagate playback send failures through ACK Joonho Lee
2026-11-12 6:02 ` sashiko-bot
2026-11-12 5:34 ` [PATCH v29 07/17] ALSA: virtio: Recover PCM state after a failed start Joonho Lee
2026-11-12 6:13 ` sashiko-bot
2026-11-12 5:34 ` [PATCH v29 08/17] ALSA: virtio: Retain queued messages through device reset Joonho Lee
2026-11-12 6:00 ` sashiko-bot
2026-11-12 5:34 ` [PATCH v29 09/17] ALSA: virtio: bound PCM transfers and retain DMA buffers Joonho Lee
2026-11-12 6:00 ` sashiko-bot
2026-11-12 5:34 ` [PATCH v29 10/17] ALSA: virtio: Keep control payloads alive through DMA completion Joonho Lee
2026-11-12 5:44 ` sashiko-bot
2026-11-12 5:34 ` [PATCH v29 11/17] ALSA: virtio: wait through signals for PCM teardown Joonho Lee
2026-11-12 5:49 ` sashiko-bot
2026-11-12 5:34 ` [PATCH v29 12/17] ALSA: virtio: use request-owned capture and serialize recovery Joonho Lee
2026-11-12 6:08 ` sashiko-bot
2026-11-12 5:34 ` [PATCH v29 13/17] ALSA: virtio: prioritize capture period work Joonho Lee
2026-11-12 5:46 ` sashiko-bot
2026-11-12 5:34 ` [PATCH v29 14/17] docs: describe virtio-sound capture options Joonho Lee
2026-11-12 5:46 ` sashiko-bot
2026-11-12 5:34 ` [PATCH v29 15/17] ALSA: virtio: reject invalid playback completions Joonho Lee
2026-11-12 5:53 ` sashiko-bot
2026-11-12 5:34 ` [PATCH v29 16/17] ALSA: pcm: add an explicit application pointer to indirect playback transfer Joonho Lee
2026-11-12 5:51 ` sashiko-bot
2026-11-12 5:34 ` [PATCH v29 17/17] ALSA: virtio: preserve playback progress across pause and buffer access Joonho Lee
2026-11-12 6:04 ` sashiko-bot
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=20261112053446.73504-6-mynameisjune111@gmail.com \
--to=mynameisjune111@gmail.com \
--cc=anton.yakovlev@opensynergy.com \
--cc=corbet@lwn.net \
--cc=linux-doc@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-sound@vger.kernel.org \
--cc=mst@redhat.com \
--cc=perex@perex.cz \
--cc=rdunlap@infradead.org \
--cc=skhan@linuxfoundation.org \
--cc=tiwai@suse.com \
--cc=virtualization@lists.linux.dev \
/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