* [PATCH] ALSA: vx: serialize pipe lifecycle against the threaded IRQ pipe walker
@ 2026-12-02 2:10 Hanxiang Xu
2026-12-02 3:25 ` sashiko-bot
0 siblings, 1 reply; 2+ messages in thread
From: Hanxiang Xu @ 2026-12-02 2:10 UTC (permalink / raw)
To: Takashi Iwai, Jaroslav Kysela; +Cc: linux-sound, linux-kernel
vx_pcm_playback_close()/vx_pcm_capture_close() unpublish their pipes
from chip->playback_pipes[]/chip->capture_pipes[] and kfree them, while
vx_pcm_update_intr() -- running from the threaded IRQ handler -- walks
the same arrays unlocked and dereferences the pipes
(pipe->substream, pipe->prepared, pipe->running, and the DMA path via
vx_pseudo_dma_write/read). pipe->prepared is set in vx_pcm_prepare()
and never cleared anywhere, so a merely prepared stream keeps the
transfer loop running across the close: the close side serializes with
the IRQ thread on chip->lock exactly at a chunk boundary, kfrees the
pipe there, and the next loop iteration reads the freed pipe.
Add a pcm_lock to struct vx_core and take it where pipes are
published, unpublished, freed, walked, and refcounted, with a uniform
pcm_lock -> chip->lock ordering (chip->lock is taken inside the held
pcm_lock by vx_send_msg() and the DMA transactions; nothing takes them
in the reverse order). Also clear pipe->prepared on trigger STOP so a
stopped stream no longer enters the transfer loop.
Cc: <stable@vger.kernel.org>
Signed-off-by: Hanxiang Xu <xuhx@hust.edu.cn>
---
include/sound/vx_core.h | 1 +
sound/drivers/vx/vx_core.c | 1 +
sound/drivers/vx/vx_pcm.c | 11 ++++++++++-
3 files changed, 12 insertions(+), 1 deletion(-)
diff --git a/include/sound/vx_core.h b/include/sound/vx_core.h
index ca87fa6..f6abe53 100644
--- a/include/sound/vx_core.h
+++ b/include/sound/vx_core.h
@@ -151,6 +151,7 @@ struct vx_core {
const struct snd_vx_ops *ops;
struct mutex lock;
+ struct mutex pcm_lock;
unsigned int chip_status;
unsigned int pcm_running;
diff --git a/sound/drivers/vx/vx_core.c b/sound/drivers/vx/vx_core.c
index 52b9340..8f34267 100644
--- a/sound/drivers/vx/vx_core.c
+++ b/sound/drivers/vx/vx_core.c
@@ -789,6 +789,7 @@ struct vx_core *snd_vx_create(struct snd_card *card,
if (!chip)
return NULL;
mutex_init(&chip->lock);
+ mutex_init(&chip->pcm_lock);
chip->irq = -1;
chip->hw = hw;
chip->type = hw->type;
diff --git a/sound/drivers/vx/vx_pcm.c b/sound/drivers/vx/vx_pcm.c
index d9b9458..8d4fdf6 100644
--- a/sound/drivers/vx/vx_pcm.c
+++ b/sound/drivers/vx/vx_pcm.c
@@ -525,7 +525,8 @@ static int vx_pcm_playback_open(struct snd_pcm_substream *subs)
audio = subs->pcm->device * 2;
if (snd_BUG_ON(audio >= chip->audio_outs))
return -EINVAL;
-
+
+ guard(mutex)(&chip->pcm_lock);
/* playback pipe may have been already allocated for monitoring */
pipe = chip->playback_pipes[audio];
if (! pipe) {
@@ -564,6 +565,7 @@ static int vx_pcm_playback_close(struct snd_pcm_substream *subs)
pipe = subs->runtime->private_data;
+ guard(mutex)(&chip->pcm_lock);
if (--pipe->references == 0) {
chip->playback_pipes[pipe->number] = NULL;
vx_free_pipe(chip, pipe);
@@ -749,6 +751,7 @@ static int vx_pcm_trigger(struct snd_pcm_substream *subs, int cmd)
vx_stop_pipe(chip, pipe);
vx_stop_stream(chip, pipe);
chip->pcm_running--;
+ pipe->prepared = 0;
pipe->running = 0;
break;
case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
@@ -901,6 +904,8 @@ static int vx_pcm_capture_open(struct snd_pcm_substream *subs)
audio = subs->pcm->device * 2;
if (snd_BUG_ON(audio >= chip->audio_ins))
return -EINVAL;
+
+ guard(mutex)(&chip->pcm_lock);
err = vx_alloc_pipe(chip, 1, audio, 2, &pipe);
if (err < 0)
return err;
@@ -954,6 +959,8 @@ static int vx_pcm_capture_close(struct snd_pcm_substream *subs)
if (! subs->runtime->private_data)
return -EINVAL;
pipe = subs->runtime->private_data;
+
+ guard(mutex)(&chip->pcm_lock);
chip->capture_pipes[pipe->number] = NULL;
pipe_out_monitoring = pipe->monitoring_pipe;
@@ -1084,6 +1091,8 @@ void vx_pcm_update_intr(struct vx_core *chip, unsigned int events)
unsigned int i;
struct vx_pipe *pipe;
+ guard(mutex)(&chip->pcm_lock);
+
#define EVENT_MASK (END_OF_BUFFER_EVENTS_PENDING|ASYNC_EVENTS_PENDING)
if (events & EVENT_MASK) {
--
2.43.0
^ permalink raw reply related [flat|nested] 2+ messages in thread
* Re: [PATCH] ALSA: vx: serialize pipe lifecycle against the threaded IRQ pipe walker
2026-12-02 2:10 [PATCH] ALSA: vx: serialize pipe lifecycle against the threaded IRQ pipe walker Hanxiang Xu
@ 2026-12-02 3:25 ` sashiko-bot
0 siblings, 0 replies; 2+ messages in thread
From: sashiko-bot @ 2026-12-02 3:25 UTC (permalink / raw)
To: Hanxiang Xu; +Cc: linux-sound
Thank you for your contribution! Sashiko AI review found 2 potential
issues to consider:
- [High] ALSA: vx: pipe->prepared not cleared for prepared streams on
hw_free or close
- [High] ALSA: vx: dangling pipe->substream in vx_pcm_playback_close()
with monitoring
--
commit 06ea28a410c475a5af72a771d6f540168a3d3d78
Author: Hanxiang Xu <xuhx@hust.edu.cn>
ALSA: vx: serialize pipe lifecycle against the threaded IRQ pipe walker
This commit adds chip->pcm_lock to serialize pipe allocation, freeing, and
threaded IRQ updates, and clears pipe->prepared on trigger STOP and SUSPEND.
> diff --git a/sound/drivers/vx/vx_pcm.c b/sound/drivers/vx/vx_pcm.c
> index d9b94580704aa..8d4fdf6c153b7 100644
> --- a/sound/drivers/vx/vx_pcm.c
> +++ b/sound/drivers/vx/vx_pcm.c
[ ... ]
> @@ -564,6 +565,7 @@ static int vx_pcm_playback_close(struct snd_pcm_substream *subs)
>
> pipe = subs->runtime->private_data;
>
> + guard(mutex)(&chip->pcm_lock);
> if (--pipe->references == 0) {
> chip->playback_pipes[pipe->number] = NULL;
> vx_free_pipe(chip, pipe);
[Severity: High]
When a playback pipe is shared with capture hardware monitoring, does
closing the playback substream leave a detached pipe->substream pointer
published in chip->playback_pipes[]?
When capture is open with monitoring enabled, pipe->references is 2 once
playback opens and sets pipe->substream = subs. If playback closes while
capture stays open, --pipe->references is 1, so pipe remains in
chip->playback_pipes[pipe->number] with pipe->substream still pointing at
subs. snd_pcm_release_substream() then calls snd_pcm_detach_substream(),
which frees subs->runtime and sets subs->runtime = NULL, so a subsequent
vx_pcm_update_intr() passing pipe && pipe->substream will dereference a
NULL subs->runtime in vx_pcm_playback_transfer().
Clearing pipe->substream = NULL and pipe->prepared = 0 under chip->pcm_lock
unconditionally in vx_pcm_playback_close() would avoid this.
> @@ -749,6 +751,7 @@ static int vx_pcm_trigger(struct snd_pcm_substream *subs, int cmd)
> vx_stop_pipe(chip, pipe);
> vx_stop_stream(chip, pipe);
> chip->pcm_running--;
> + pipe->prepared = 0;
> pipe->running = 0;
> break;
> case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
[Severity: High]
Can vx_pcm_update_intr() still read from a freed or NULL runtime->dma_area
when a stream in SNDRV_PCM_STATE_PREPARED is freed via hw_free or closed?
In the ALSA PCM core, snd_pcm_do_stop() only invokes
ops->trigger(SNDRV_PCM_TRIGGER_STOP) when snd_pcm_running(substream) is
true:
sound/core/pcm_native.c:snd_pcm_do_stop() {
if (substream->runtime->trigger_master == substream &&
snd_pcm_running(substream)) {
substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_STOP);
substream->runtime->stop_operating = true;
}
return 0; /* unconditionally stop all substreams */
}
If a stream was stopped and re-prepared (or if SNDRV_PCM_TRIGGER_START
failed after vx_pcm_playback_transfer() queued chunks), pipe->prepared is 1
while the stream is in SNDRV_PCM_STATE_PREPARED. On close (or
SNDRV_PCM_IOCTL_HW_FREE), do_hw_free() frees the managed vmalloc
runtime->dma_area buffer before vx_pcm_playback_close() takes
chip->pcm_lock:
CPU0 CPU1 (IRQ thread)
snd_pcm_release_substream()
snd_pcm_drop()
/* not running: trigger skipped */
do_hw_free()
snd_pcm_lib_free_pages()
/* vfrees runtime->dma_area */
vx_pcm_update_intr()
lock(&chip->pcm_lock)
vx_pcm_playback_transfer()
/* pipe->prepared == 1 */
vx_pseudo_dma_write()
/* reads freed dma_area */
vx_pcm_playback_close()
lock(&chip->pcm_lock)
Providing an .hw_free callback that clears pipe->prepared under
chip->pcm_lock (or gating transfers in vx_pcm_update_intr() on
pipe->running) would prevent vx_pcm_playback_transfer() from touching
runtime->dma_area once hw_free begins.
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261202101006.1-1-xuhx@hust.edu.cn?part=1
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2026-12-02 3:25 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-12-02 2:10 [PATCH] ALSA: vx: serialize pipe lifecycle against the threaded IRQ pipe walker Hanxiang Xu
2026-12-02 3:25 ` 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.