* [PATCH v2 0/3] ALSA: PCM race and locking fixes
@ 2026-11-19 12:23 Takashi Iwai
2026-11-19 12:23 ` [PATCH v2 1/3] ALSA: pcm: Fix potential ABBA deadlock at snd_pcm_action_nonatomic() Takashi Iwai
` (2 more replies)
0 siblings, 3 replies; 6+ messages in thread
From: Takashi Iwai @ 2026-11-19 12:23 UTC (permalink / raw)
To: linux-sound
Hi,
this is a revised patch set for fixing some issues in ALSA PCM linked
stream handling. Now addressed a potential deadlock and a slight race
Sashiko suggested in the previous series:
https://lore.kernel.org/20261118135558.1928160-1-tiwai@suse.de
Takashi
===
Takashi Iwai (3):
ALSA: pcm: Fix potential ABBA deadlock at snd_pcm_action_nonatomic()
ALSA: pcm: Simplify PCM buffer access gate with auto-cleanup helpers
ALSA: pcm: Avoid hw_params/hw_refine changes during PCM buffer access
sound/core/pcm_compat.c | 3 +
sound/core/pcm_lib.c | 7 +--
sound/core/pcm_local.h | 26 +++++++++
sound/core/pcm_native.c | 125 ++++++++++++++++++++++++----------------
4 files changed, 106 insertions(+), 55 deletions(-)
--
2.53.0
^ permalink raw reply [flat|nested] 6+ messages in thread
* [PATCH v2 1/3] ALSA: pcm: Fix potential ABBA deadlock at snd_pcm_action_nonatomic()
2026-11-19 12:23 [PATCH v2 0/3] ALSA: PCM race and locking fixes Takashi Iwai
@ 2026-11-19 12:23 ` Takashi Iwai
2026-11-19 12:23 ` [PATCH v2 2/3] ALSA: pcm: Simplify PCM buffer access gate with auto-cleanup helpers Takashi Iwai
2026-11-19 12:23 ` [PATCH v2 3/3] ALSA: pcm: Avoid hw_params/hw_refine changes during PCM buffer access Takashi Iwai
2 siblings, 0 replies; 6+ messages in thread
From: Takashi Iwai @ 2026-11-19 12:23 UTC (permalink / raw)
To: linux-sound
snd_pcm_action_nonatomic() is called for PCM PREPARE and other
operations, and the function takes snd_pcm_buffer_access_lock() of the
given substream (that takes runtime->buffer_mutex internally) at
first, followed by the call of snd_pcm_action_group() that takes
buffer_mutex of other substreams in the same link group again.
Recently Sashiko suggested it may lead to an ABBA deadlock when two
different substreams in the same PCM link call it concurrently:
CPU0 CPU1
---- ----
lock(&A->runtime->buffer_mutex)
lock(&B->runtime->buffer_mutex)
lock(&B->runtime->buffer_mutex)
lock(&A->runtime->buffer_mutex)
This patch tries to address the potential deadlock. Instead of taking
the buffer_mutex of the target substream at first and processing the
rest in the loop, take the whole buffer_mutex for each in the loop.
This assures the order of the nested mutexes -- the linked list itself
is kept intact by the read_rwsem() around snd_pcm_action_nonatomic().
Also, this changes the action in the snd_pcm_action_group() loop from
the direct call of mutex_lock(buffer_mutex) to
snd_pcm_buffer_access_lock(). This prevents the unexpected state
transition or the buffer access during the action, too.
Since the target substream is no longer locked before the loop, the
lockdep subclass for buffer_mutex in the loop now starts from 0, so
that a link group of N substreams uses the subclasses 0..N-1 as
before. The subclass for the stream lock case still starts from 1,
as the caller holds the target stream lock at subclass 0.
Note that this change might have a slight side-effect, though: when
some concurrent operations happen during the PREPARE or RESET, now it
may return -EBUSY. But this can be seen rather as an expected
behavior that was overlooked before the fix.
Fixes: 3c3201f8c7bb ("ALSA: pcm: Fix races among concurrent prepare and hw_params/hw_free calls")
Reported-by: sashiko-bot@kernel.org
Closes: https://lore.kernel.org/sashiko-outbox-221145@kernel.org
Signed-off-by: Takashi Iwai <tiwai@suse.de>
---
v2: new patch in the series
sound/core/pcm_native.c | 63 +++++++++++++++++++++++++++--------------
1 file changed, 41 insertions(+), 22 deletions(-)
diff --git a/sound/core/pcm_native.c b/sound/core/pcm_native.c
index da2b789962c2..c6c2a7053923 100644
--- a/sound/core/pcm_native.c
+++ b/sound/core/pcm_native.c
@@ -718,14 +718,20 @@ static int snd_pcm_hw_params_choose(struct snd_pcm_substream *pcm,
/* acquire buffer_mutex; if it's in r/w operation, return -EBUSY, otherwise
* block the further r/w operations
*/
-static int snd_pcm_buffer_access_lock(struct snd_pcm_runtime *runtime)
+static int __snd_pcm_buffer_access_lock(struct snd_pcm_runtime *runtime,
+ int depth)
{
if (!atomic_dec_unless_positive(&runtime->buffer_accessing))
return -EBUSY;
- mutex_lock(&runtime->buffer_mutex);
+ mutex_lock_nested(&runtime->buffer_mutex, depth);
return 0; /* keep buffer_mutex, unlocked by below */
}
+static int snd_pcm_buffer_access_lock(struct snd_pcm_runtime *runtime)
+{
+ return __snd_pcm_buffer_access_lock(runtime, 0);
+}
+
/* release buffer_mutex and clear r/w access flag */
static void snd_pcm_buffer_access_unlock(struct snd_pcm_runtime *runtime)
{
@@ -1317,26 +1323,38 @@ struct action_ops {
* 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
+ *
+ * Locking:
+ * When @stream_lock is false, it takes buffer-access lock for all substreams.
+ * OTOH, when @stream_lock is true, takes substream lock except for the
+ * target substream (that should have taken its lock before calling this).
*/
static int snd_pcm_action_group(const struct action_ops *ops,
struct snd_pcm_substream *substream,
snd_pcm_state_t state,
bool stream_lock)
{
- struct snd_pcm_substream *s = NULL;
- struct snd_pcm_substream *s1;
- int res = 0, depth = 1;
+ struct snd_pcm_substream *s, *s1, *last;
+ int res = 0, depth;
+ /* the caller already holds the target stream lock at depth 0 */
+ depth = stream_lock ? 1 : 0;
+
+ last = NULL;
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 (!stream_lock) {
+ res = __snd_pcm_buffer_access_lock(s->runtime, depth);
+ if (res < 0)
+ goto _unlock;
+ depth++;
+ } else if (s != substream) {
+ if (s->pcm->nonatomic)
mutex_lock_nested(&s->self_group.mutex, depth);
else
spin_lock_nested(&s->self_group.lock, depth);
depth++;
}
+ last = s;
res = ops->pre_action(s, state);
if (res < 0)
goto _unlock;
@@ -1351,7 +1369,6 @@ static int snd_pcm_action_group(const struct action_ops *ops,
ops->undo_action(s1, state);
}
}
- s = NULL; /* unlock all */
goto _unlock;
}
}
@@ -1359,17 +1376,19 @@ static int snd_pcm_action_group(const struct action_ops *ops,
ops->post_action(s, state);
}
_unlock:
+ if (!last)
+ return res;
/* 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)
- mutex_unlock(&s1->self_group.mutex);
+ snd_pcm_group_for_each_entry(s, substream) {
+ if (!stream_lock) {
+ snd_pcm_buffer_access_unlock(s->runtime);
+ } else if (s != substream) {
+ if (s->pcm->nonatomic)
+ mutex_unlock(&s->self_group.mutex);
else
- spin_unlock(&s1->self_group.lock);
+ spin_unlock(&s->self_group.lock);
}
- if (s1 == s) /* end */
+ if (s == last) /* end */
break;
}
return res;
@@ -1496,13 +1515,13 @@ static int snd_pcm_action_nonatomic(const struct action_ops *ops,
/* Guarantee the group members won't change during non-atomic action */
guard(rwsem_read)(&snd_pcm_link_rwsem);
+ if (snd_pcm_stream_linked(substream))
+ return snd_pcm_action_group(ops, substream, state, false);
+
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);
- else
- res = snd_pcm_action_single(ops, substream, state);
+ res = snd_pcm_action_single(ops, substream, state);
snd_pcm_buffer_access_unlock(substream->runtime);
return res;
}
--
2.53.0
^ permalink raw reply related [flat|nested] 6+ messages in thread
* [PATCH v2 2/3] ALSA: pcm: Simplify PCM buffer access gate with auto-cleanup helpers
2026-11-19 12:23 [PATCH v2 0/3] ALSA: PCM race and locking fixes Takashi Iwai
2026-11-19 12:23 ` [PATCH v2 1/3] ALSA: pcm: Fix potential ABBA deadlock at snd_pcm_action_nonatomic() Takashi Iwai
@ 2026-11-19 12:23 ` Takashi Iwai
2026-11-19 12:23 ` [PATCH v2 3/3] ALSA: pcm: Avoid hw_params/hw_refine changes during PCM buffer access Takashi Iwai
2 siblings, 0 replies; 6+ messages in thread
From: Takashi Iwai @ 2026-11-19 12:23 UTC (permalink / raw)
To: linux-sound
There are a few places to grant the PCM buffer access with the gate
using runtime->buffer_accessing. They are paired like below in each
place:
if (!atomic_inc_unless_negative(&runtime->buffer_accessing))
return -EBUSY;
....
atomic_dec(&runtime->buffer_accessing);
which is not really intuitive and error-prone, as being open-coded.
This patch introduces helper macros to replace the code like the above
with auto-cleanup. With the new helper, the above will become
SND_PCM_BUFFER_ACQUIRE(runtime, lock);
if (SND_PCM_BUFFER_ACQUIRE_ERR(lock))
return -EBUSY;
....
and the counter-part (atomic_dec()) is dropped as it's automagically
done by the auto-cleanup.
Signed-off-by: Takashi Iwai <tiwai@suse.de>
---
v1->v2: no change
sound/core/pcm_lib.c | 7 +++---
sound/core/pcm_local.h | 26 +++++++++++++++++++++++
sound/core/pcm_native.c | 47 ++++++++++++++++-------------------------
3 files changed, 47 insertions(+), 33 deletions(-)
diff --git a/sound/core/pcm_lib.c b/sound/core/pcm_lib.c
index bb16738bb591..3b66677c037a 100644
--- a/sound/core/pcm_lib.c
+++ b/sound/core/pcm_lib.c
@@ -2377,10 +2377,9 @@ snd_pcm_sframes_t __snd_pcm_lib_xfer(struct snd_pcm_substream *substream,
err = -EINVAL;
goto _end_unlock;
}
- if (!atomic_inc_unless_negative(&runtime->buffer_accessing)) {
- err = -EBUSY;
+ err = snd_pcm_buffer_access_get(runtime);
+ if (err < 0)
goto _end_unlock;
- }
snd_pcm_stream_unlock_irq(substream);
if (!is_playback)
snd_pcm_dma_buffer_sync(substream, SNDRV_DMA_SYNC_CPU);
@@ -2389,7 +2388,7 @@ snd_pcm_sframes_t __snd_pcm_lib_xfer(struct snd_pcm_substream *substream,
if (is_playback)
snd_pcm_dma_buffer_sync(substream, SNDRV_DMA_SYNC_DEVICE);
snd_pcm_stream_lock_irq(substream);
- atomic_dec(&runtime->buffer_accessing);
+ snd_pcm_buffer_access_put(runtime);
if (err < 0)
goto _end_unlock;
err = pcm_accessible_state(substream);
diff --git a/sound/core/pcm_local.h b/sound/core/pcm_local.h
index 4b134d435c24..7762c89e0f9f 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;
+}
+
+static inline void snd_pcm_buffer_access_put(struct snd_pcm_runtime *runtime)
+{
+ atomic_dec(&runtime->buffer_accessing);
+}
+
+/* auto-cleanup macros */
+DEFINE_GUARD(snd_pcm_buffer_access, struct snd_pcm_runtime *,
+ snd_pcm_buffer_access_get(_T), snd_pcm_buffer_access_put(_T));
+DEFINE_GUARD_COND(snd_pcm_buffer_access, _try,
+ snd_pcm_buffer_access_get(_T), _RET == 0);
+
+#define SND_PCM_BUFFER_ACQUIRE(runtime, var) \
+ ACQUIRE(snd_pcm_buffer_access_try, var)(runtime)
+#define SND_PCM_BUFFER_ACQUIRE_ERR(var) \
+ ACQUIRE_ERR(snd_pcm_buffer_access_try, &(var))
+
#endif /* __SOUND_CORE_PCM_LOCAL_H */
diff --git a/sound/core/pcm_native.c b/sound/core/pcm_native.c
index c6c2a7053923..7d5e662b219b 100644
--- a/sound/core/pcm_native.c
+++ b/sound/core/pcm_native.c
@@ -3140,7 +3140,8 @@ static snd_pcm_sframes_t snd_pcm_rewind(struct snd_pcm_substream *substream,
return 0;
/* Keep the DMA buffer alive through the sync below. */
- if (!atomic_inc_unless_negative(&runtime->buffer_accessing))
+ SND_PCM_BUFFER_ACQUIRE(runtime, lock);
+ if (SND_PCM_BUFFER_ACQUIRE_ERR(lock))
return -EBUSY;
scoped_guard(pcm_stream_lock_irq, substream) {
ret = do_pcm_hwsync(substream);
@@ -3150,7 +3151,6 @@ static snd_pcm_sframes_t snd_pcm_rewind(struct snd_pcm_substream *substream,
}
if (ret >= 0)
snd_pcm_dma_buffer_sync(substream, SNDRV_DMA_SYNC_DEVICE);
- atomic_dec(&runtime->buffer_accessing);
return ret;
}
@@ -3164,7 +3164,8 @@ static snd_pcm_sframes_t snd_pcm_forward(struct snd_pcm_substream *substream,
return 0;
/* Keep the DMA buffer alive through the sync below. */
- if (!atomic_inc_unless_negative(&runtime->buffer_accessing))
+ SND_PCM_BUFFER_ACQUIRE(runtime, lock);
+ if (SND_PCM_BUFFER_ACQUIRE_ERR(lock))
return -EBUSY;
scoped_guard(pcm_stream_lock_irq, substream) {
ret = do_pcm_hwsync(substream);
@@ -3174,7 +3175,6 @@ static snd_pcm_sframes_t snd_pcm_forward(struct snd_pcm_substream *substream,
}
if (ret >= 0)
snd_pcm_dma_buffer_sync(substream, SNDRV_DMA_SYNC_DEVICE);
- atomic_dec(&runtime->buffer_accessing);
return ret;
}
@@ -3185,7 +3185,8 @@ static int snd_pcm_delay(struct snd_pcm_substream *substream,
int err;
/* Keep the DMA buffer alive through the sync below. */
- if (!atomic_inc_unless_negative(&runtime->buffer_accessing))
+ SND_PCM_BUFFER_ACQUIRE(runtime, lock);
+ if (SND_PCM_BUFFER_ACQUIRE_ERR(lock))
return -EBUSY;
scoped_guard(pcm_stream_lock_irq, substream) {
err = do_pcm_hwsync(substream);
@@ -3193,7 +3194,6 @@ static int snd_pcm_delay(struct snd_pcm_substream *substream,
*delay = snd_pcm_calc_delay(substream);
}
snd_pcm_dma_buffer_sync(substream, SNDRV_DMA_SYNC_CPU);
- atomic_dec(&runtime->buffer_accessing);
return err;
}
@@ -4097,32 +4097,23 @@ int snd_pcm_mmap_data(struct snd_pcm_substream *substream, struct file *file,
}
runtime = substream->runtime;
/* don't race with buffer reallocation in hw_params/hw_free */
- if (!atomic_inc_unless_negative(&runtime->buffer_accessing))
+ SND_PCM_BUFFER_ACQUIRE(runtime, lock);
+ if (SND_PCM_BUFFER_ACQUIRE_ERR(lock))
return -EBUSY;
- if (runtime->state == SNDRV_PCM_STATE_OPEN) {
- err = -EBADFD;
- goto out;
- }
- if (!(runtime->info & SNDRV_PCM_INFO_MMAP)) {
- err = -ENXIO;
- goto out;
- }
+ if (runtime->state == SNDRV_PCM_STATE_OPEN)
+ return -EBADFD;
+ if (!(runtime->info & SNDRV_PCM_INFO_MMAP))
+ return -ENXIO;
if (runtime->access == SNDRV_PCM_ACCESS_RW_INTERLEAVED ||
- runtime->access == SNDRV_PCM_ACCESS_RW_NONINTERLEAVED) {
- err = -EINVAL;
- goto out;
- }
+ runtime->access == SNDRV_PCM_ACCESS_RW_NONINTERLEAVED)
+ return -EINVAL;
size = area->vm_end - area->vm_start;
offset = area->vm_pgoff << PAGE_SHIFT;
dma_bytes = PAGE_ALIGN(runtime->dma_bytes);
- if ((size_t)size > dma_bytes) {
- err = -EINVAL;
- goto out;
- }
- if (offset > dma_bytes - size) {
- err = -EINVAL;
- goto out;
- }
+ if ((size_t)size > dma_bytes)
+ return -EINVAL;
+ if (offset > dma_bytes - size)
+ return -EINVAL;
area->vm_ops = &snd_pcm_vm_ops_data;
area->vm_private_data = substream;
@@ -4132,8 +4123,6 @@ int snd_pcm_mmap_data(struct snd_pcm_substream *substream, struct file *file,
err = snd_pcm_lib_default_mmap(substream, area);
if (!err)
atomic_inc(&substream->mmap_count);
-out:
- atomic_dec(&runtime->buffer_accessing);
return err;
}
EXPORT_SYMBOL(snd_pcm_mmap_data);
--
2.53.0
^ permalink raw reply related [flat|nested] 6+ messages in thread
* [PATCH v2 3/3] ALSA: pcm: Avoid hw_params/hw_refine changes during PCM buffer access
2026-11-19 12:23 [PATCH v2 0/3] ALSA: PCM race and locking fixes Takashi Iwai
2026-11-19 12:23 ` [PATCH v2 1/3] ALSA: pcm: Fix potential ABBA deadlock at snd_pcm_action_nonatomic() Takashi Iwai
2026-11-19 12:23 ` [PATCH v2 2/3] ALSA: pcm: Simplify PCM buffer access gate with auto-cleanup helpers Takashi Iwai
@ 2026-11-19 12:23 ` Takashi Iwai
2026-11-19 12:46 ` sashiko-bot
2 siblings, 1 reply; 6+ messages in thread
From: Takashi Iwai @ 2026-11-19 12:23 UTC (permalink / raw)
To: linux-sound
While we've already protected the concurrent hw_params or hw_free
changes during the PCM streaming in snd_pcm_lib_read*() and
snd_pcm_lib_write*(), those entry points aren't covered by the buffer
access gate yet, hence the associated PCM runtime instance may still
change concurrently. This can lead to the inconsistency, e.g. in
snd_pcm_xfern_frames_ioctl() or other places that refers to the
runtime attribute like runtime->channel; since the concurrent
hw_params may change the runtime->channel afterwards, this can result
in OOB access for the array allocated for a different size with the
former runtime->channels.
For avoiding such races, let's take the buffer access gate at those
ioctls and read/write ops from the beginning.
Reported-by: on Sim <tlatkdgus1@gmail.com>
Signed-off-by: Takashi Iwai <tiwai@suse.de>
---
v1->v2: cover buffer-lock more widely for state change races
sound/core/pcm_compat.c | 3 +++
sound/core/pcm_native.c | 15 +++++++++++++++
2 files changed, 18 insertions(+)
diff --git a/sound/core/pcm_compat.c b/sound/core/pcm_compat.c
index c54cb07304ad..c5931fdfe1f5 100644
--- a/sound/core/pcm_compat.c
+++ b/sound/core/pcm_compat.c
@@ -338,6 +338,9 @@ static int snd_pcm_ioctl_xfern_compat(struct snd_pcm_substream *substream,
return -ENOTTY;
if (substream->stream != dir)
return -EINVAL;
+ SND_PCM_BUFFER_ACQUIRE(substream->runtime, lock);
+ if (SND_PCM_BUFFER_ACQUIRE_ERR(lock))
+ return -EBUSY;
if (snd_pcm_get_state(substream) == SNDRV_PCM_STATE_OPEN)
return -EBADFD;
diff --git a/sound/core/pcm_native.c b/sound/core/pcm_native.c
index 7d5e662b219b..d028f03369e7 100644
--- a/sound/core/pcm_native.c
+++ b/sound/core/pcm_native.c
@@ -3434,6 +3434,9 @@ static int snd_pcm_xfern_frames_ioctl(struct snd_pcm_substream *substream,
void *bufs __free(kfree) = NULL;
snd_pcm_sframes_t result;
+ SND_PCM_BUFFER_ACQUIRE(runtime, lock);
+ if (SND_PCM_BUFFER_ACQUIRE_ERR(lock))
+ return -EBUSY;
if (snd_pcm_get_state(substream) == SNDRV_PCM_STATE_OPEN)
return -EBADFD;
if (runtime->channels > 128)
@@ -3673,6 +3676,9 @@ static ssize_t snd_pcm_read(struct file *file, char __user *buf, size_t count,
if (PCM_RUNTIME_CHECK(substream))
return -ENXIO;
runtime = substream->runtime;
+ SND_PCM_BUFFER_ACQUIRE(runtime, lock);
+ if (SND_PCM_BUFFER_ACQUIRE_ERR(lock))
+ return -EBUSY;
if (snd_pcm_state_open_or_disconnected(substream))
return -EBADFD;
if (!frame_aligned(runtime, count))
@@ -3697,6 +3703,9 @@ static ssize_t snd_pcm_write(struct file *file, const char __user *buf,
if (PCM_RUNTIME_CHECK(substream))
return -ENXIO;
runtime = substream->runtime;
+ SND_PCM_BUFFER_ACQUIRE(runtime, lock);
+ if (SND_PCM_BUFFER_ACQUIRE_ERR(lock))
+ return -EBUSY;
if (snd_pcm_state_open_or_disconnected(substream))
return -EBADFD;
if (!frame_aligned(runtime, count))
@@ -3723,6 +3732,9 @@ static ssize_t snd_pcm_readv(struct kiocb *iocb, struct iov_iter *to)
if (PCM_RUNTIME_CHECK(substream))
return -ENXIO;
runtime = substream->runtime;
+ SND_PCM_BUFFER_ACQUIRE(runtime, lock);
+ if (SND_PCM_BUFFER_ACQUIRE_ERR(lock))
+ return -EBUSY;
if (snd_pcm_state_open_or_disconnected(substream))
return -EBADFD;
if (!user_backed_iter(to))
@@ -3762,6 +3774,9 @@ static ssize_t snd_pcm_writev(struct kiocb *iocb, struct iov_iter *from)
if (PCM_RUNTIME_CHECK(substream))
return -ENXIO;
runtime = substream->runtime;
+ SND_PCM_BUFFER_ACQUIRE(runtime, lock);
+ if (SND_PCM_BUFFER_ACQUIRE_ERR(lock))
+ return -EBUSY;
if (snd_pcm_state_open_or_disconnected(substream))
return -EBADFD;
if (!user_backed_iter(from))
--
2.53.0
^ permalink raw reply related [flat|nested] 6+ messages in thread
* Re: [PATCH v2 3/3] ALSA: pcm: Avoid hw_params/hw_refine changes during PCM buffer access
2026-11-19 12:23 ` [PATCH v2 3/3] ALSA: pcm: Avoid hw_params/hw_refine changes during PCM buffer access Takashi Iwai
@ 2026-11-19 12:46 ` sashiko-bot
2026-11-19 13:21 ` Takashi Iwai
0 siblings, 1 reply; 6+ messages in thread
From: sashiko-bot @ 2026-11-19 12:46 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: missing buffer access gate in xferi frame ioctl
handlers
--
commit 1d0a5a9306bc1367f12d8e1ee79c3cdcad53aa1b
Author: Takashi Iwai <tiwai@suse.de>
ALSA: pcm: Avoid hw_params/hw_refine changes during PCM buffer access
This commit acquires the PCM buffer access gate at the entry of the
non-interleaved frame transfer ioctls and PCM read/write file operations
to prevent concurrent hw_params or hw_free updates while accessing
runtime state.
> diff --git a/sound/core/pcm_native.c b/sound/core/pcm_native.c
> index 7d5e662b219bd..d028f03369e74 100644
> --- a/sound/core/pcm_native.c
> +++ b/sound/core/pcm_native.c
> @@ -3434,6 +3434,9 @@ static int snd_pcm_xfern_frames_ioctl(struct snd_pcm_substream *substream,
> void *bufs __free(kfree) = NULL;
> snd_pcm_sframes_t result;
>
> + SND_PCM_BUFFER_ACQUIRE(runtime, lock);
> + if (SND_PCM_BUFFER_ACQUIRE_ERR(lock))
> + return -EBUSY;
> if (snd_pcm_get_state(substream) == SNDRV_PCM_STATE_OPEN)
> return -EBADFD;
> if (runtime->channels > 128)
[Severity: Medium]
Should snd_pcm_xferi_frames_ioctl() and snd_pcm_ioctl_xferi_compat() also
acquire the buffer access gate with SND_PCM_BUFFER_ACQUIRE()?
__snd_pcm_lib_xfer() only calls snd_pcm_buffer_access_get() inside its
inner transfer loop around writer(). Until then, a concurrent
snd_pcm_hw_free() or snd_pcm_hw_params() can free runtime->dma_area
before transitioning runtime->state to SNDRV_PCM_STATE_OPEN, causing
pcm_sanity_check() to hit snd_BUG_ON(!substream->ops->copy &&
!runtime->dma_area), or modify runtime->access, runtime->channels and
other runtime parameters while __snd_pcm_lib_xfer() validates them or
sleeps in wait_for_avail():
CPU0 CPU1
snd_pcm_xferi_frames_ioctl()
snd_pcm_get_state() != OPEN
snd_pcm_hw_free()
snd_pcm_buffer_access_lock()
substream->ops->hw_free()
runtime->dma_area = NULL
snd_pcm_lib_write()
__snd_pcm_lib_xfer()
pcm_sanity_check()
snd_BUG_ON(!substream->ops->copy &&
!runtime->dma_area) <-- WARN / race
runtime->state = SNDRV_PCM_STATE_OPEN
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261119122356.2134523-1-tiwai@suse.de?part=3
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: [PATCH v2 3/3] ALSA: pcm: Avoid hw_params/hw_refine changes during PCM buffer access
2026-11-19 12:46 ` sashiko-bot
@ 2026-11-19 13:21 ` Takashi Iwai
0 siblings, 0 replies; 6+ messages in thread
From: Takashi Iwai @ 2026-11-19 13:21 UTC (permalink / raw)
To: linux-sound
On Thu, 19 Nov 2026 13:46:04 +0100,
sashiko-bot@kernel.org wrote:
>
> Thank you for your contribution! Sashiko AI review found 1 potential
> issue to consider:
> - [Medium] ALSA: pcm: missing buffer access gate in xferi frame ioctl
> handlers
> --
>
> commit 1d0a5a9306bc1367f12d8e1ee79c3cdcad53aa1b
> Author: Takashi Iwai <tiwai@suse.de>
>
> ALSA: pcm: Avoid hw_params/hw_refine changes during PCM buffer access
>
> This commit acquires the PCM buffer access gate at the entry of the
> non-interleaved frame transfer ioctls and PCM read/write file operations
> to prevent concurrent hw_params or hw_free updates while accessing
> runtime state.
>
> > diff --git a/sound/core/pcm_native.c b/sound/core/pcm_native.c
> > index 7d5e662b219bd..d028f03369e74 100644
> > --- a/sound/core/pcm_native.c
> > +++ b/sound/core/pcm_native.c
> > @@ -3434,6 +3434,9 @@ static int snd_pcm_xfern_frames_ioctl(struct snd_pcm_substream *substream,
> > void *bufs __free(kfree) = NULL;
> > snd_pcm_sframes_t result;
> >
> > + SND_PCM_BUFFER_ACQUIRE(runtime, lock);
> > + if (SND_PCM_BUFFER_ACQUIRE_ERR(lock))
> > + return -EBUSY;
> > if (snd_pcm_get_state(substream) == SNDRV_PCM_STATE_OPEN)
> > return -EBADFD;
> > if (runtime->channels > 128)
>
> [Severity: Medium]
> Should snd_pcm_xferi_frames_ioctl() and snd_pcm_ioctl_xferi_compat() also
> acquire the buffer access gate with SND_PCM_BUFFER_ACQUIRE()?
>
> __snd_pcm_lib_xfer() only calls snd_pcm_buffer_access_get() inside its
> inner transfer loop around writer(). Until then, a concurrent
> snd_pcm_hw_free() or snd_pcm_hw_params() can free runtime->dma_area
> before transitioning runtime->state to SNDRV_PCM_STATE_OPEN, causing
> pcm_sanity_check() to hit snd_BUG_ON(!substream->ops->copy &&
> !runtime->dma_area), or modify runtime->access, runtime->channels and
> other runtime parameters while __snd_pcm_lib_xfer() validates them or
> sleeps in wait_for_avail():
>
> CPU0 CPU1
> snd_pcm_xferi_frames_ioctl()
> snd_pcm_get_state() != OPEN
> snd_pcm_hw_free()
> snd_pcm_buffer_access_lock()
> substream->ops->hw_free()
> runtime->dma_area = NULL
> snd_pcm_lib_write()
> __snd_pcm_lib_xfer()
> pcm_sanity_check()
> snd_BUG_ON(!substream->ops->copy &&
> !runtime->dma_area) <-- WARN / race
> runtime->state = SNDRV_PCM_STATE_OPEN
>
FWIW, the bug targeted by this patch is rather the runtime parameter
evaluation in snd_pcm__xfern_frames_ioctl and its compat version, and
others are just for a bit more safety. The suggested issue is rather
a pre-existing thing and maybe handled better in another patch to
restructure the state change check in the read/write operations.
Takashi
^ permalink raw reply [flat|nested] 6+ messages in thread
end of thread, other threads:[~2026-11-19 13:21 UTC | newest]
Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-19 12:23 [PATCH v2 0/3] ALSA: PCM race and locking fixes Takashi Iwai
2026-11-19 12:23 ` [PATCH v2 1/3] ALSA: pcm: Fix potential ABBA deadlock at snd_pcm_action_nonatomic() Takashi Iwai
2026-11-19 12:23 ` [PATCH v2 2/3] ALSA: pcm: Simplify PCM buffer access gate with auto-cleanup helpers Takashi Iwai
2026-11-19 12:23 ` [PATCH v2 3/3] ALSA: pcm: Avoid hw_params/hw_refine changes during PCM buffer access Takashi Iwai
2026-11-19 12:46 ` sashiko-bot
2026-11-19 13:21 ` Takashi Iwai
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox