[PATCH] ALSA: vx: serialize pipe lifecycle against the threaded IRQ pipe walker
From: Hanxiang Xu
Date: Tue Dec 01 2026 - 22:11:54 EST
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@xxxxxxxxxxxxxxx>
Signed-off-by: Hanxiang Xu <xuhx@xxxxxxxxxxx>
---
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