Linux Sound subsystem development
 help / color / mirror / Atom feed
* [PATCH v3] ALSA: line6: Kill URBs before releasing the PCM buffer
@ 2026-11-21 10:40 YuanZhe Liu
  2026-11-22  8:16 ` Takashi Iwai
  0 siblings, 1 reply; 6+ messages in thread
From: YuanZhe Liu @ 2026-11-21 10:40 UTC (permalink / raw)
  To: Takashi Iwai, Jaroslav Kysela
  Cc: linux-sound, linux-kernel, stable, Bin Yu, Cheng Wen

line6_buffer_release() frees the stream buffer after
line6_wait_clear_audio_urbs() gave up waiting for the URBs that are still
active.  The buffer is still referenced by those URBs, and the completion
callback of such an URB resubmits it from submit_audio_out_urb() with
line6pcm->out.buffer already set to NULL, which ends up in a NULL pointer
dereference in the memset() in submit_audio_out_urb().

While the URBs remain queued, the host controller can also write the
received isochronous data into the freed buffer.  Such a write does not go
through the KASAN checks, so it silently corrupts the freed slab object;
the corruption was observed as a bogus KASAN quarantine entry that later
led to a general protection fault in qlist_free_all().

The active_urbs bitmap cannot prove that the URBs are gone:
line6_unlink_audio_urbs() is called outside the stream lock and only marks
the URBs that are active at that moment, so an URB resubmitted by a
completion callback right after the scan is never unlinked and keeps being
resubmitted after the stream was stopped.

Set the unlink bit for every currently active URB under the stream lock,
so that the completion callbacks cannot resubmit them, and then kill all
URBs synchronously.  usb_kill_urb() waits for the giveback, hence the
buffer is no longer referenced by any URB when it is freed.  Drop the
unlink marks afterwards: line6_unlink_audio_urbs() can leave a stale mark
on an URB that completed and was not resubmitted, and such a mark makes
line6_stream_start() skip submitting the URBs.

Fixes: 63e20df1e5b2 ("ALSA: line6: Reorganize PCM stream handling")
Cc: stable@vger.kernel.org
Co-developed-by: Bin Yu <byu@xidian.edu.cn>
Signed-off-by: Bin Yu <byu@xidian.edu.cn>
Co-developed-by: Cheng Wen <wencheng@xidian.edu.cn>
Signed-off-by: Cheng Wen <wencheng@xidian.edu.cn>
Signed-off-by: YuanZhe Liu <25031212351@stu.xidian.edu.cn>
---
v3:
- Kill the URBs unconditionally instead of rechecking active_urbs outside
  the stream lock, and clear the unlink marks after the kills.

v2:
- Rebased on sound/for-next.  v1 was generated against v7.3.1 and no longer
  applied, because line6_buffer_release() gained the prev_fbuf clearing
  block in the meantime.  No code change.
---
 sound/usb/line6/pcm.c | 32 +++++++++++++++++++++++++++++++-
 1 file changed, 31 insertions(+), 1 deletion(-)

diff --git a/sound/usb/line6/pcm.c b/sound/usb/line6/pcm.c
index d17baf7761c6..4be3b41a98a2 100644
--- a/sound/usb/line6/pcm.c
+++ b/sound/usb/line6/pcm.c
@@ -134,6 +134,36 @@ static void line6_wait_clear_audio_urbs(struct snd_line6_pcm *line6pcm,
 			"timeout: still %d active urbs..\n", alive);
 }
 
+/*
+ * Mark all currently active URBs for unlinking and kill them.
+ *
+ * The unlink bit is set under the stream lock, so the completion
+ * callbacks cannot resubmit these URBs any longer.  usb_kill_urb() then
+ * waits for the giveback of every URB, hence the stream buffer is no
+ * longer referenced by the host controller.  The unlink marks are
+ * cleared afterwards, so that a later stream start can submit the URBs
+ * again.  This function must be called from a sleepable context.
+ */
+static void line6_kill_audio_urbs(struct snd_line6_pcm *line6pcm,
+				  struct line6_pcm_stream *pcms)
+{
+	int i;
+
+	scoped_guard(spinlock_irqsave, &pcms->lock) {
+		for (i = 0; i < line6pcm->line6->iso_buffers; i++)
+			if (test_bit(i, &pcms->active_urbs))
+				set_bit(i, &pcms->unlink_urbs);
+	}
+
+	for (i = 0; i < line6pcm->line6->iso_buffers; i++)
+		if (pcms->urbs[i])
+			usb_kill_urb(pcms->urbs[i]);
+
+	scoped_guard(spinlock_irqsave, &pcms->lock) {
+		pcms->unlink_urbs = 0;
+	}
+}
+
 static inline struct line6_pcm_stream *
 get_stream(struct snd_line6_pcm *line6pcm, int direction)
 {
@@ -172,7 +202,7 @@ static void line6_buffer_release(struct snd_line6_pcm *line6pcm,
 {
 	clear_bit(type, &pstr->opened);
 	if (!pstr->opened) {
-		line6_wait_clear_audio_urbs(line6pcm, pstr);
+		line6_kill_audio_urbs(line6pcm, pstr);
 		if (pstr == &line6pcm->in) {
 			/* prev_fbuf points into the capture buffer we are about
 			 * to free; drop it while no capture URB is in flight.
-- 
2.34.1


^ permalink raw reply related	[flat|nested] 6+ messages in thread

* Re: [PATCH v3] ALSA: line6: Kill URBs before releasing the PCM buffer
  2026-11-21 10:40 [PATCH v3] ALSA: line6: Kill URBs before releasing the PCM buffer YuanZhe Liu
@ 2026-11-22  8:16 ` Takashi Iwai
  2026-11-22  9:04   ` [PATCH v4] ALSA: line6: Stop URBs synchronously via sync_stop callback YuanZhe Liu
  2026-11-22  9:34   ` [PATCH v5] " YuanZhe Liu
  0 siblings, 2 replies; 6+ messages in thread
From: Takashi Iwai @ 2026-11-22  8:16 UTC (permalink / raw)
  To: YuanZhe Liu
  Cc: Takashi Iwai, Jaroslav Kysela, linux-sound, linux-kernel, stable,
	Bin Yu, Cheng Wen

On Sat, 21 Nov 2026 11:40:00 +0100,
YuanZhe Liu wrote:
> 
> line6_buffer_release() frees the stream buffer after
> line6_wait_clear_audio_urbs() gave up waiting for the URBs that are still
> active.  The buffer is still referenced by those URBs, and the completion
> callback of such an URB resubmits it from submit_audio_out_urb() with
> line6pcm->out.buffer already set to NULL, which ends up in a NULL pointer
> dereference in the memset() in submit_audio_out_urb().
> 
> While the URBs remain queued, the host controller can also write the
> received isochronous data into the freed buffer.  Such a write does not go
> through the KASAN checks, so it silently corrupts the freed slab object;
> the corruption was observed as a bogus KASAN quarantine entry that later
> led to a general protection fault in qlist_free_all().
> 
> The active_urbs bitmap cannot prove that the URBs are gone:
> line6_unlink_audio_urbs() is called outside the stream lock and only marks
> the URBs that are active at that moment, so an URB resubmitted by a
> completion callback right after the scan is never unlinked and keeps being
> resubmitted after the stream was stopped.
> 
> Set the unlink bit for every currently active URB under the stream lock,
> so that the completion callbacks cannot resubmit them, and then kill all
> URBs synchronously.  usb_kill_urb() waits for the giveback, hence the
> buffer is no longer referenced by any URB when it is freed.  Drop the
> unlink marks afterwards: line6_unlink_audio_urbs() can leave a stale mark
> on an URB that completed and was not resubmitted, and such a mark makes
> line6_stream_start() skip submitting the URBs.
> 
> Fixes: 63e20df1e5b2 ("ALSA: line6: Reorganize PCM stream handling")
> Cc: stable@vger.kernel.org
> Co-developed-by: Bin Yu <byu@xidian.edu.cn>
> Signed-off-by: Bin Yu <byu@xidian.edu.cn>
> Co-developed-by: Cheng Wen <wencheng@xidian.edu.cn>
> Signed-off-by: Cheng Wen <wencheng@xidian.edu.cn>
> Signed-off-by: YuanZhe Liu <25031212351@stu.xidian.edu.cn>

Thanks for the patch.

But, this kind of synchronization should be rather done in sync_stop
PCM ops that seems missing in the current LINE6 driver.
 
I guess line6_wait_clear_audio_urbs() could be replaced with
usb_kill_urb() as you've done, and move it to sync_stop callback.
Then it can be dropped from snd_line6_prepare() and
line6_pcm_acquire().  line6_pcm_disconnect() still might need it,
though.


Takashi

> v3:
> - Kill the URBs unconditionally instead of rechecking active_urbs outside
>   the stream lock, and clear the unlink marks after the kills.
> 
> v2:
> - Rebased on sound/for-next.  v1 was generated against v7.3.1 and no longer
>   applied, because line6_buffer_release() gained the prev_fbuf clearing
>   block in the meantime.  No code change.
> ---
>  sound/usb/line6/pcm.c | 32 +++++++++++++++++++++++++++++++-
>  1 file changed, 31 insertions(+), 1 deletion(-)
> 
> diff --git a/sound/usb/line6/pcm.c b/sound/usb/line6/pcm.c
> index d17baf7761c6..4be3b41a98a2 100644
> --- a/sound/usb/line6/pcm.c
> +++ b/sound/usb/line6/pcm.c
> @@ -134,6 +134,36 @@ static void line6_wait_clear_audio_urbs(struct snd_line6_pcm *line6pcm,
>  			"timeout: still %d active urbs..\n", alive);
>  }
>  
> +/*
> + * Mark all currently active URBs for unlinking and kill them.
> + *
> + * The unlink bit is set under the stream lock, so the completion
> + * callbacks cannot resubmit these URBs any longer.  usb_kill_urb() then
> + * waits for the giveback of every URB, hence the stream buffer is no
> + * longer referenced by the host controller.  The unlink marks are
> + * cleared afterwards, so that a later stream start can submit the URBs
> + * again.  This function must be called from a sleepable context.
> + */
> +static void line6_kill_audio_urbs(struct snd_line6_pcm *line6pcm,
> +				  struct line6_pcm_stream *pcms)
> +{
> +	int i;
> +
> +	scoped_guard(spinlock_irqsave, &pcms->lock) {
> +		for (i = 0; i < line6pcm->line6->iso_buffers; i++)
> +			if (test_bit(i, &pcms->active_urbs))
> +				set_bit(i, &pcms->unlink_urbs);
> +	}
> +
> +	for (i = 0; i < line6pcm->line6->iso_buffers; i++)
> +		if (pcms->urbs[i])
> +			usb_kill_urb(pcms->urbs[i]);
> +
> +	scoped_guard(spinlock_irqsave, &pcms->lock) {
> +		pcms->unlink_urbs = 0;
> +	}
> +}
> +
>  static inline struct line6_pcm_stream *
>  get_stream(struct snd_line6_pcm *line6pcm, int direction)
>  {
> @@ -172,7 +202,7 @@ static void line6_buffer_release(struct snd_line6_pcm *line6pcm,
>  {
>  	clear_bit(type, &pstr->opened);
>  	if (!pstr->opened) {
> -		line6_wait_clear_audio_urbs(line6pcm, pstr);
> +		line6_kill_audio_urbs(line6pcm, pstr);
>  		if (pstr == &line6pcm->in) {
>  			/* prev_fbuf points into the capture buffer we are about
>  			 * to free; drop it while no capture URB is in flight.
> -- 
> 2.34.1
> 

^ permalink raw reply	[flat|nested] 6+ messages in thread

* [PATCH v4] ALSA: line6: Stop URBs synchronously via sync_stop callback
  2026-11-22  8:16 ` Takashi Iwai
@ 2026-11-22  9:04   ` YuanZhe Liu
  2026-11-22  9:20     ` sashiko-bot
  2026-11-22  9:34   ` [PATCH v5] " YuanZhe Liu
  1 sibling, 1 reply; 6+ messages in thread
From: YuanZhe Liu @ 2026-11-22  9:04 UTC (permalink / raw)
  To: Takashi Iwai, Jaroslav Kysela
  Cc: linux-sound, linux-kernel, stable, Bin Yu, Cheng Wen

line6_buffer_release() frees the stream buffer after
line6_wait_clear_audio_urbs() gave up waiting for the URBs that are still
active.  The buffer is still referenced by those URBs, and the completion
callback of such an URB resubmits it from submit_audio_out_urb() with
line6pcm->out.buffer already set to NULL, which ends up in a NULL pointer
dereference in the memset() in submit_audio_out_urb().

While the URBs remain queued, the host controller can also write the
received isochronous data into the freed buffer.  Such a write does not go
through the KASAN checks, so it silently corrupts the freed slab object;
the corruption was observed as a bogus KASAN quarantine entry that later
led to a general protection fault in qlist_free_all().

The active_urbs bitmap cannot prove that the URBs are gone:
line6_unlink_audio_urbs() is called outside the stream lock and only marks
the URBs that are active at that moment, so an URB resubmitted by a
completion callback right after the scan is never unlinked and keeps being
resubmitted after the stream was stopped.

Stop the URBs synchronously instead of polling for them: mark all
currently active URBs for unlinking under the stream lock, so that the
completion callbacks cannot resubmit them, and kill them with
usb_kill_urb(), which waits for the giveback.  Expose this through the
sync_stop PCM op, which ALSA core invokes before hw_free(), hw_params()
and prepare() once the stream was stopped, so the buffer is guaranteed to
be unused when it is released.

Replace line6_wait_clear_audio_urbs() with this helper accordingly, and
drop its now redundant calls from snd_line6_prepare() and
line6_pcm_acquire().  line6_pcm_disconnect() keeps stopping the URBs
synchronously, since the device may be gone already.

The monitor and impulse streams are started via line6_pcm_acquire() and
released via line6_pcm_release() without going through the PCM ops, so
line6_buffer_release() still stops the URBs before it frees the buffer.

Fixes: 63e20df1e5b2 ("ALSA: line6: Reorganize PCM stream handling")
Cc: stable@vger.kernel.org
Co-developed-by: Bin Yu <byu@xidian.edu.cn>
Signed-off-by: Bin Yu <byu@xidian.edu.cn>
Co-developed-by: Cheng Wen <wencheng@xidian.edu.cn>
Signed-off-by: Cheng Wen <wencheng@xidian.edu.cn>
Signed-off-by: YuanZhe Liu <25031212351@stu.xidian.edu.cn>
---
v4:
- Add the sync_stop PCM op and drop the waits from snd_line6_prepare()
  and line6_pcm_acquire(), as suggested by Takashi Iwai.

v3:
- Kill the URBs unconditionally instead of rechecking active_urbs outside
  the stream lock, and clear the unlink marks after the kills.

v2:
- Rebased on sound/for-next.  v1 was generated against v7.3.1 and no longer
  applied, because line6_buffer_release() gained the prev_fbuf clearing
  block in the meantime.  No code change.
---
 sound/usb/line6/capture.c  |  1 +
 sound/usb/line6/pcm.c      | 66 +++++++++++++++++++++-----------------
 sound/usb/line6/pcm.h      |  1 +
 sound/usb/line6/playback.c |  1 +
 4 files changed, 40 insertions(+), 29 deletions(-)

diff --git a/sound/usb/line6/capture.c b/sound/usb/line6/capture.c
index 626274e503c7..fa98c26cf77d 100644
--- a/sound/usb/line6/capture.c
+++ b/sound/usb/line6/capture.c
@@ -284,6 +284,7 @@ const struct snd_pcm_ops snd_line6_capture_ops = {
 	.hw_free = snd_line6_hw_free,
 	.prepare = snd_line6_prepare,
 	.trigger = snd_line6_trigger,
+	.sync_stop = snd_line6_sync_stop,
 	.pointer = snd_line6_pointer,
 };
 
diff --git a/sound/usb/line6/pcm.c b/sound/usb/line6/pcm.c
index d17baf7761c6..dc850aa2ebd0 100644
--- a/sound/usb/line6/pcm.c
+++ b/sound/usb/line6/pcm.c
@@ -109,29 +109,33 @@ static void line6_unlink_audio_urbs(struct snd_line6_pcm *line6pcm,
 }
 
 /*
-	Wait until unlinking of all currently active URBs has been finished.
-*/
-static void line6_wait_clear_audio_urbs(struct snd_line6_pcm *line6pcm,
-					struct line6_pcm_stream *pcms)
+ * Mark all currently active URBs for unlinking and kill them.
+ *
+ * The unlink bit is set under the stream lock, so the completion
+ * callbacks cannot resubmit these URBs any longer.  usb_kill_urb() then
+ * waits for the giveback of every URB, hence the stream buffer is no
+ * longer referenced by the host controller.  The unlink marks are
+ * cleared afterwards, so that a later stream start can submit the URBs
+ * again.  This function must be called from a sleepable context.
+ */
+static void line6_kill_audio_urbs(struct snd_line6_pcm *line6pcm,
+				  struct line6_pcm_stream *pcms)
 {
-	int timeout = HZ;
 	int i;
-	int alive;
 
-	do {
-		alive = 0;
-		for (i = 0; i < line6pcm->line6->iso_buffers; i++) {
+	scoped_guard(spinlock_irqsave, &pcms->lock) {
+		for (i = 0; i < line6pcm->line6->iso_buffers; i++)
 			if (test_bit(i, &pcms->active_urbs))
-				alive++;
-		}
-		if (!alive)
-			break;
-		set_current_state(TASK_UNINTERRUPTIBLE);
-		schedule_timeout(1);
-	} while (--timeout > 0);
-	if (alive)
-		dev_err(line6pcm->line6->ifcdev,
-			"timeout: still %d active urbs..\n", alive);
+				set_bit(i, &pcms->unlink_urbs);
+	}
+
+	for (i = 0; i < line6pcm->line6->iso_buffers; i++)
+		if (pcms->urbs[i])
+			usb_kill_urb(pcms->urbs[i]);
+
+	scoped_guard(spinlock_irqsave, &pcms->lock) {
+		pcms->unlink_urbs = 0;
+	}
 }
 
 static inline struct line6_pcm_stream *
@@ -172,7 +176,7 @@ static void line6_buffer_release(struct snd_line6_pcm *line6pcm,
 {
 	clear_bit(type, &pstr->opened);
 	if (!pstr->opened) {
-		line6_wait_clear_audio_urbs(line6pcm, pstr);
+		line6_kill_audio_urbs(line6pcm, pstr);
 		if (pstr == &line6pcm->in) {
 			/* prev_fbuf points into the capture buffer we are about
 			 * to free; drop it while no capture URB is in flight.
@@ -337,8 +341,6 @@ int line6_pcm_acquire(struct snd_line6_pcm *line6pcm, int type, bool start)
 		ret = line6_buffer_acquire(line6pcm, pstr, dir, type);
 		if (ret < 0)
 			goto error;
-		if (!pstr->running)
-			line6_wait_clear_audio_urbs(line6pcm, pstr);
 	}
 	if (start) {
 		for (dir = 0; dir < 2; dir++) {
@@ -510,10 +512,8 @@ static int snd_line6_new_pcm(struct usb_line6 *line6, struct snd_pcm **pcm_ret)
 */
 void line6_pcm_disconnect(struct snd_line6_pcm *line6pcm)
 {
-	line6_unlink_audio_urbs(line6pcm, &line6pcm->out);
-	line6_unlink_audio_urbs(line6pcm, &line6pcm->in);
-	line6_wait_clear_audio_urbs(line6pcm, &line6pcm->out);
-	line6_wait_clear_audio_urbs(line6pcm, &line6pcm->in);
+	line6_kill_audio_urbs(line6pcm, &line6pcm->out);
+	line6_kill_audio_urbs(line6pcm, &line6pcm->in);
 }
 
 /*
@@ -592,15 +592,23 @@ int line6_init_pcm(struct usb_line6 *line6,
 }
 EXPORT_SYMBOL_GPL(line6_init_pcm);
 
+/* common PCM sync_stop callback */
+int snd_line6_sync_stop(struct snd_pcm_substream *substream)
+{
+	struct snd_line6_pcm *line6pcm = snd_pcm_substream_chip(substream);
+	struct line6_pcm_stream *pstr = get_stream(line6pcm, substream->stream);
+
+	guard(mutex)(&line6pcm->state_mutex);
+	line6_kill_audio_urbs(line6pcm, pstr);
+	return 0;
+}
+
 /* prepare pcm callback */
 int snd_line6_prepare(struct snd_pcm_substream *substream)
 {
 	struct snd_line6_pcm *line6pcm = snd_pcm_substream_chip(substream);
-	struct line6_pcm_stream *pstr = get_stream(line6pcm, substream->stream);
 
 	guard(mutex)(&line6pcm->state_mutex);
-	if (!pstr->running)
-		line6_wait_clear_audio_urbs(line6pcm, pstr);
 
 	if (!test_and_set_bit(LINE6_FLAG_PREPARED, &line6pcm->flags)) {
 		line6pcm->out.count = 0;
diff --git a/sound/usb/line6/pcm.h b/sound/usb/line6/pcm.h
index a15913bf2a7a..21e550948cd8 100644
--- a/sound/usb/line6/pcm.h
+++ b/sound/usb/line6/pcm.h
@@ -183,6 +183,7 @@ extern int line6_init_pcm(struct usb_line6 *line6,
 			  struct line6_pcm_properties *properties);
 extern int snd_line6_trigger(struct snd_pcm_substream *substream, int cmd);
 extern int snd_line6_prepare(struct snd_pcm_substream *substream);
+extern int snd_line6_sync_stop(struct snd_pcm_substream *substream);
 extern int snd_line6_hw_params(struct snd_pcm_substream *substream,
 			       struct snd_pcm_hw_params *hw_params);
 extern int snd_line6_hw_free(struct snd_pcm_substream *substream);
diff --git a/sound/usb/line6/playback.c b/sound/usb/line6/playback.c
index e93a16f6958e..10e273f4b77a 100644
--- a/sound/usb/line6/playback.c
+++ b/sound/usb/line6/playback.c
@@ -414,6 +414,7 @@ const struct snd_pcm_ops snd_line6_playback_ops = {
 	.hw_free = snd_line6_hw_free,
 	.prepare = snd_line6_prepare,
 	.trigger = snd_line6_trigger,
+	.sync_stop = snd_line6_sync_stop,
 	.pointer = snd_line6_pointer,
 };
 
-- 
2.34.1


^ permalink raw reply related	[flat|nested] 6+ messages in thread

* Re: [PATCH v4] ALSA: line6: Stop URBs synchronously via sync_stop callback
  2026-11-22  9:04   ` [PATCH v4] ALSA: line6: Stop URBs synchronously via sync_stop callback YuanZhe Liu
@ 2026-11-22  9:20     ` sashiko-bot
  0 siblings, 0 replies; 6+ messages in thread
From: sashiko-bot @ 2026-11-22  9:20 UTC (permalink / raw)
  To: YuanZhe Liu; +Cc: linux-sound

Thank you for your contribution! Sashiko AI review found 4 potential
issues to consider:
- [High] ALSA: line6: snd_line6_sync_stop() kills URBs while shared
  stream is running
- [Medium] ALSA: line6: snd_line6_sync_stop() misses helper playback
  stream on capture stop
- [Medium] ALSA: line6: unsynchronized URB unlink when pstr->opened
  remains non-zero
- [Medium] ALSA: line6: NULL pointer dereference in
  line6_kill_audio_urbs() on probe error
--

commit 03ea0b5c4363156759700a216da2dd67752857be
Author: YuanZhe Liu <25031212351@stu.xidian.edu.cn>

ALSA: line6: Stop URBs synchronously via sync_stop callback

This commit replaces line6_wait_clear_audio_urbs() with a synchronous
line6_kill_audio_urbs() helper and wires it into the PCM sync_stop
callback while removing the URB wait calls from snd_line6_prepare() and
line6_pcm_acquire().

> diff --git a/sound/usb/line6/pcm.c b/sound/usb/line6/pcm.c
> index d17baf7761c67..dc850aa2ebd09 100644
> --- a/sound/usb/line6/pcm.c
> +++ b/sound/usb/line6/pcm.c
> @@ -109,29 +109,33 @@ static void line6_unlink_audio_urbs(struct snd_line6_pcm *line6pcm,
>  }
>  
>  /*
> -	Wait until unlinking of all currently active URBs has been finished.
> -*/
> -static void line6_wait_clear_audio_urbs(struct snd_line6_pcm *line6pcm,
> -					struct line6_pcm_stream *pcms)
> + * Mark all currently active URBs for unlinking and kill them.
> + *
> + * The unlink bit is set under the stream lock, so the completion
> + * callbacks cannot resubmit these URBs any longer.  usb_kill_urb() then
> + * waits for the giveback of every URB, hence the stream buffer is no
> + * longer referenced by the host controller.  The unlink marks are
> + * cleared afterwards, so that a later stream start can submit the URBs
> + * again.  This function must be called from a sleepable context.
> + */
> +static void line6_kill_audio_urbs(struct snd_line6_pcm *line6pcm,
> +				  struct line6_pcm_stream *pcms)
>  {
> -	int timeout = HZ;
>  	int i;
> -	int alive;
>  
> -	do {
> -		alive = 0;
> -		for (i = 0; i < line6pcm->line6->iso_buffers; i++) {
> +	scoped_guard(spinlock_irqsave, &pcms->lock) {
> +		for (i = 0; i < line6pcm->line6->iso_buffers; i++)
>  			if (test_bit(i, &pcms->active_urbs))
> -				alive++;
> -		}
> -		if (!alive)
> -			break;
> -		set_current_state(TASK_UNINTERRUPTIBLE);
> -		schedule_timeout(1);
> -	} while (--timeout > 0);
> -	if (alive)
> -		dev_err(line6pcm->line6->ifcdev,
> -			"timeout: still %d active urbs..\n", alive);
> +				set_bit(i, &pcms->unlink_urbs);
> +	}
> +
> +	for (i = 0; i < line6pcm->line6->iso_buffers; i++)
> +		if (pcms->urbs[i])
> +			usb_kill_urb(pcms->urbs[i]);

[Severity: Medium]
Can pcms->urbs[i] dereference a NULL pcms->urbs pointer when
line6_init_pcm() fails during probe?

line6_init_pcm() publishes line6->line6pcm before checking
max_packet_size_in/out and before allocating out.urbs and in.urbs.  If one
of those steps fails, line6_probe() unwinds via line6_disconnect() ->
line6_pcm_disconnect() -> line6_kill_audio_urbs() while pcms->urbs is still
NULL.  Checking if (!pcms->urbs) before the loop (as cleanup_urbs() does)
would avoid this.

[ ... ]

> @@ -337,8 +341,6 @@ int line6_pcm_acquire(struct snd_line6_pcm *line6pcm, int type, bool start)
>  		ret = line6_buffer_acquire(line6pcm, pstr, dir, type);
>  		if (ret < 0)
>  			goto error;
> -		if (!pstr->running)
> -			line6_wait_clear_audio_urbs(line6pcm, pstr);
>  	}

[Severity: Medium]
Does dropping the !pstr->running synchronization from line6_pcm_acquire()
and snd_line6_prepare() leave URBs only asynchronously unlinked when
pstr->opened remains non-zero?

When a monitor or impulse stream is stopped via line6_pcm_release(),
__line6_pcm_release() calls line6_stream_stop() (which only runs the
unlocked, asynchronous line6_unlink_audio_urbs()) and then
line6_buffer_release(), which only calls line6_kill_audio_urbs() when
pstr->opened reaches 0.  If an idle PCM substream already holds a bit in
pstr->opened (for example, snd_line6_capture_open() sets
LINE6_STREAM_CAPTURE_HELPER in both out.opened and in.opened),
line6_kill_audio_urbs() is skipped and ALSA core will not invoke
snd_line6_sync_stop() because the PCM substream itself was not running.

If the stream is then started again via line6_pcm_acquire() or
snd_line6_trigger() while active_urbs or unlink_urbs is still non-zero
from the asynchronous unlink, line6_stream_start() sees
(pstr->active_urbs || pstr->unlink_urbs) non-zero, sets the bit in
pstr->running without submitting URBs, and leaves the stream stalled once
the unlinking URBs complete:

sound/usb/line6/pcm.c:line6_stream_start() {
	...
	if (!test_and_set_bit(type, &pstr->running) &&
	    !(pstr->active_urbs || pstr->unlink_urbs)) {
		pstr->count = 0;
		ret = line6_submit_audio_urbs(line6pcm, pstr);
	}
	...
}

[ ... ]

> @@ -592,15 +592,23 @@ int line6_init_pcm(struct usb_line6 *line6,
>  }
>  EXPORT_SYMBOL_GPL(line6_init_pcm);
>  
> +/* common PCM sync_stop callback */
> +int snd_line6_sync_stop(struct snd_pcm_substream *substream)
> +{
> +	struct snd_line6_pcm *line6pcm = snd_pcm_substream_chip(substream);
> +	struct line6_pcm_stream *pstr = get_stream(line6pcm, substream->stream);
> +
> +	guard(mutex)(&line6pcm->state_mutex);
> +	line6_kill_audio_urbs(line6pcm, pstr);
> +	return 0;
> +}

[Severity: High]
Does snd_line6_sync_stop() kill in-flight URBs on pstr even when another
stream type in pstr->running is still using the stream?

pstr->running is a bitmask shared across LINE6_STREAM_PCM,
LINE6_STREAM_MONITOR, LINE6_STREAM_IMPULSE, and
LINE6_STREAM_CAPTURE_HELPER.  When a PCM substream stops,
line6_stream_stop() clears LINE6_STREAM_PCM and returns early if another
bit in pstr->running is still set so the URBs keep running:

sound/usb/line6/pcm.c:line6_stream_stop() {
	scoped_guard(spinlock_irqsave, &pstr->lock) {
		clear_bit(type, &pstr->running);
		if (pstr->running)
			return;
	}

	line6_unlink_audio_urbs(line6pcm, pstr);
}

Because snd_line6_sync_stop() does not check if (!pstr->running) before
calling line6_kill_audio_urbs(), stopping a PCM playback or capture stream
(on drop, prepare, hw_free, or close) kills the URBs while software
monitoring (enabled at startup on TonePort devices) or capture on
LINE6_CAP_IN_NEEDS_OUT devices is still active.  With the other stream's
bit still set in pstr->running, later line6_stream_start() calls for that
type see test_and_set_bit(type, &pstr->running) already set and cannot
restart the URBs.

[Severity: Medium]
On devices with LINE6_CAP_IN_NEEDS_OUT, does snd_line6_sync_stop() also
need to synchronize &line6pcm->out when stopping a capture substream?

In snd_line6_trigger(), stopping SNDRV_PCM_STREAM_CAPTURE on a
LINE6_CAP_IN_NEEDS_OUT device also stops LINE6_STREAM_CAPTURE_HELPER on
SNDRV_PCM_STREAM_PLAYBACK via line6_stream_stop(), which only performs an
asynchronous line6_unlink_audio_urbs() on &line6pcm->out.  Because
snd_line6_sync_stop() only kills URBs on get_stream(line6pcm,
substream->stream) (&line6pcm->in) and the wait in snd_line6_prepare() is
removed, re-preparing and restarting capture while &line6pcm->out URBs are
still unlinking causes line6_stream_start() for
LINE6_STREAM_CAPTURE_HELPER to see active_urbs or unlink_urbs non-zero and
skip submitting playback URBs, stalling capture.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261122090454.6217-1-25031212351@stu.xidian.edu.cn?part=1

^ permalink raw reply	[flat|nested] 6+ messages in thread

* [PATCH v5] ALSA: line6: Stop URBs synchronously via sync_stop callback
  2026-11-22  8:16 ` Takashi Iwai
  2026-11-22  9:04   ` [PATCH v4] ALSA: line6: Stop URBs synchronously via sync_stop callback YuanZhe Liu
@ 2026-11-22  9:34   ` YuanZhe Liu
  2026-11-22  9:51     ` sashiko-bot
  1 sibling, 1 reply; 6+ messages in thread
From: YuanZhe Liu @ 2026-11-22  9:34 UTC (permalink / raw)
  To: Takashi Iwai, Jaroslav Kysela
  Cc: linux-sound, linux-kernel, stable, Bin Yu, Cheng Wen

line6_buffer_release() frees the stream buffer after
line6_wait_clear_audio_urbs() gave up waiting for the URBs that are still
active.  The buffer is still referenced by those URBs, and the completion
callback of such an URB resubmits it from submit_audio_out_urb() with
line6pcm->out.buffer already set to NULL, which ends up in a NULL pointer
dereference in the memset() in submit_audio_out_urb().

While the URBs remain queued, the host controller can also write the
received isochronous data into the freed buffer.  Such a write does not go
through the KASAN checks, so it silently corrupts the freed slab object;
the corruption was observed as a bogus KASAN quarantine entry that later
led to a general protection fault in qlist_free_all().

The active_urbs bitmap cannot prove that the URBs are gone:
line6_unlink_audio_urbs() is called outside the stream lock and only marks
the URBs that are active at that moment, so an URB resubmitted by a
completion callback right after the scan is never unlinked and keeps being
resubmitted after the stream was stopped.

Stop the URBs synchronously instead of polling for them: mark all
currently active URBs for unlinking under the stream lock, so that the
completion callbacks cannot resubmit them, and kill them with
usb_kill_urb(), which waits for the giveback.  Expose this through the
sync_stop PCM op, which ALSA core invokes before hw_free(), hw_params()
and prepare() once the stream was stopped, so the buffer is guaranteed to
be unused when it is released.

The URBs are stopped in sync_stop() only while no other stream type is
running on the same stream, since software monitoring or a capture helper
may keep them running.  When the capture stream is stopped, the helper
playback stream is stopped together in snd_line6_trigger(), so its
leftover URBs are stopped as well unless another playback stream runs.

Replace line6_wait_clear_audio_urbs() with this helper accordingly, and
drop the now redundant wait from snd_line6_prepare();
line6_pcm_disconnect() and line6_pcm_acquire() keep stopping the URBs
synchronously, as the device may be gone already and the monitor and
capture-helper streams are started without going through the PCM ops.
The helper skips the loop when the URBs were not created, as
line6_pcm_disconnect() may run after a failed probe.

Fixes: 63e20df1e5b2 ("ALSA: line6: Reorganize PCM stream handling")
Cc: stable@vger.kernel.org
Co-developed-by: Bin Yu <byu@xidian.edu.cn>
Signed-off-by: Bin Yu <byu@xidian.edu.cn>
Co-developed-by: Cheng Wen <wencheng@xidian.edu.cn>
Signed-off-by: Cheng Wen <wencheng@xidian.edu.cn>
Signed-off-by: YuanZhe Liu <25031212351@stu.xidian.edu.cn>
---
v5:
- Skip line6_kill_audio_urbs() when the URBs are not created yet, as
  line6_pcm_disconnect() can be called after a failed probe.
- Stop the URBs in sync_stop() only when no other stream type is running
  on the same stream, and stop the leftover playback URBs of the capture
  helper as well.
- Keep the synchronization in line6_pcm_acquire(), now killing the URBs
  instead of polling: the monitor and capture-helper streams are started
  there without going through the PCM ops.

v4:
- Add the sync_stop PCM op and drop the waits from snd_line6_prepare()
  and line6_pcm_acquire(), as suggested by Takashi Iwai.

v3:
- Kill the URBs unconditionally instead of rechecking active_urbs outside
  the stream lock, and clear the unlink marks after the kills.

v2:
- Rebased on sound/for-next.  v1 was generated against v7.3.1 and no longer
  applied, because line6_buffer_release() gained the prev_fbuf clearing
  block in the meantime.  No code change.
---
 sound/usb/line6/capture.c  |  1 +
 sound/usb/line6/pcm.c      | 83 +++++++++++++++++++++++++-------------
 sound/usb/line6/pcm.h      |  1 +
 sound/usb/line6/playback.c |  1 +
 4 files changed, 58 insertions(+), 28 deletions(-)

diff --git a/sound/usb/line6/capture.c b/sound/usb/line6/capture.c
index 626274e503c7..fa98c26cf77d 100644
--- a/sound/usb/line6/capture.c
+++ b/sound/usb/line6/capture.c
@@ -284,6 +284,7 @@ const struct snd_pcm_ops snd_line6_capture_ops = {
 	.hw_free = snd_line6_hw_free,
 	.prepare = snd_line6_prepare,
 	.trigger = snd_line6_trigger,
+	.sync_stop = snd_line6_sync_stop,
 	.pointer = snd_line6_pointer,
 };
 
diff --git a/sound/usb/line6/pcm.c b/sound/usb/line6/pcm.c
index d17baf7761c6..16e6f9900514 100644
--- a/sound/usb/line6/pcm.c
+++ b/sound/usb/line6/pcm.c
@@ -109,29 +109,37 @@ static void line6_unlink_audio_urbs(struct snd_line6_pcm *line6pcm,
 }
 
 /*
-	Wait until unlinking of all currently active URBs has been finished.
-*/
-static void line6_wait_clear_audio_urbs(struct snd_line6_pcm *line6pcm,
-					struct line6_pcm_stream *pcms)
+ * Mark all currently active URBs for unlinking and kill them.
+ *
+ * The unlink bit is set under the stream lock, so the completion
+ * callbacks cannot resubmit these URBs any longer.  usb_kill_urb() then
+ * waits for the giveback of every URB, hence the stream buffer is no
+ * longer referenced by the host controller.  The unlink marks are
+ * cleared afterwards, so that a later stream start can submit the URBs
+ * again.  This function must be called from a sleepable context.
+ */
+static void line6_kill_audio_urbs(struct snd_line6_pcm *line6pcm,
+				  struct line6_pcm_stream *pcms)
 {
-	int timeout = HZ;
 	int i;
-	int alive;
 
-	do {
-		alive = 0;
-		for (i = 0; i < line6pcm->line6->iso_buffers; i++) {
+	/* May be called when the URB creation failed during the probe */
+	if (!pcms->urbs)
+		return;
+
+	scoped_guard(spinlock_irqsave, &pcms->lock) {
+		for (i = 0; i < line6pcm->line6->iso_buffers; i++)
 			if (test_bit(i, &pcms->active_urbs))
-				alive++;
-		}
-		if (!alive)
-			break;
-		set_current_state(TASK_UNINTERRUPTIBLE);
-		schedule_timeout(1);
-	} while (--timeout > 0);
-	if (alive)
-		dev_err(line6pcm->line6->ifcdev,
-			"timeout: still %d active urbs..\n", alive);
+				set_bit(i, &pcms->unlink_urbs);
+	}
+
+	for (i = 0; i < line6pcm->line6->iso_buffers; i++)
+		if (pcms->urbs[i])
+			usb_kill_urb(pcms->urbs[i]);
+
+	scoped_guard(spinlock_irqsave, &pcms->lock) {
+		pcms->unlink_urbs = 0;
+	}
 }
 
 static inline struct line6_pcm_stream *
@@ -172,7 +180,7 @@ static void line6_buffer_release(struct snd_line6_pcm *line6pcm,
 {
 	clear_bit(type, &pstr->opened);
 	if (!pstr->opened) {
-		line6_wait_clear_audio_urbs(line6pcm, pstr);
+		line6_kill_audio_urbs(line6pcm, pstr);
 		if (pstr == &line6pcm->in) {
 			/* prev_fbuf points into the capture buffer we are about
 			 * to free; drop it while no capture URB is in flight.
@@ -338,7 +346,7 @@ int line6_pcm_acquire(struct snd_line6_pcm *line6pcm, int type, bool start)
 		if (ret < 0)
 			goto error;
 		if (!pstr->running)
-			line6_wait_clear_audio_urbs(line6pcm, pstr);
+			line6_kill_audio_urbs(line6pcm, pstr);
 	}
 	if (start) {
 		for (dir = 0; dir < 2; dir++) {
@@ -510,10 +518,8 @@ static int snd_line6_new_pcm(struct usb_line6 *line6, struct snd_pcm **pcm_ret)
 */
 void line6_pcm_disconnect(struct snd_line6_pcm *line6pcm)
 {
-	line6_unlink_audio_urbs(line6pcm, &line6pcm->out);
-	line6_unlink_audio_urbs(line6pcm, &line6pcm->in);
-	line6_wait_clear_audio_urbs(line6pcm, &line6pcm->out);
-	line6_wait_clear_audio_urbs(line6pcm, &line6pcm->in);
+	line6_kill_audio_urbs(line6pcm, &line6pcm->out);
+	line6_kill_audio_urbs(line6pcm, &line6pcm->in);
 }
 
 /*
@@ -592,15 +598,36 @@ int line6_init_pcm(struct usb_line6 *line6,
 }
 EXPORT_SYMBOL_GPL(line6_init_pcm);
 
-/* prepare pcm callback */
-int snd_line6_prepare(struct snd_pcm_substream *substream)
+/* common PCM sync_stop callback */
+int snd_line6_sync_stop(struct snd_pcm_substream *substream)
 {
 	struct snd_line6_pcm *line6pcm = snd_pcm_substream_chip(substream);
 	struct line6_pcm_stream *pstr = get_stream(line6pcm, substream->stream);
 
 	guard(mutex)(&line6pcm->state_mutex);
 	if (!pstr->running)
-		line6_wait_clear_audio_urbs(line6pcm, pstr);
+		line6_kill_audio_urbs(line6pcm, pstr);
+	/*
+	 * A capture stream may use the playback stream as a helper, and
+	 * snd_line6_trigger() stops it together with the capture stream.
+	 * Stop the leftover playback URBs here as well, so that the helper
+	 * stream can be restarted at the next trigger; skip it while
+	 * another playback stream is still running.
+	 */
+	if (substream->stream == SNDRV_PCM_STREAM_CAPTURE &&
+	    (line6pcm->line6->properties->capabilities &
+	     LINE6_CAP_IN_NEEDS_OUT) &&
+	    !line6pcm->out.running)
+		line6_kill_audio_urbs(line6pcm, &line6pcm->out);
+	return 0;
+}
+
+/* prepare pcm callback */
+int snd_line6_prepare(struct snd_pcm_substream *substream)
+{
+	struct snd_line6_pcm *line6pcm = snd_pcm_substream_chip(substream);
+
+	guard(mutex)(&line6pcm->state_mutex);
 
 	if (!test_and_set_bit(LINE6_FLAG_PREPARED, &line6pcm->flags)) {
 		line6pcm->out.count = 0;
diff --git a/sound/usb/line6/pcm.h b/sound/usb/line6/pcm.h
index a15913bf2a7a..21e550948cd8 100644
--- a/sound/usb/line6/pcm.h
+++ b/sound/usb/line6/pcm.h
@@ -183,6 +183,7 @@ extern int line6_init_pcm(struct usb_line6 *line6,
 			  struct line6_pcm_properties *properties);
 extern int snd_line6_trigger(struct snd_pcm_substream *substream, int cmd);
 extern int snd_line6_prepare(struct snd_pcm_substream *substream);
+extern int snd_line6_sync_stop(struct snd_pcm_substream *substream);
 extern int snd_line6_hw_params(struct snd_pcm_substream *substream,
 			       struct snd_pcm_hw_params *hw_params);
 extern int snd_line6_hw_free(struct snd_pcm_substream *substream);
diff --git a/sound/usb/line6/playback.c b/sound/usb/line6/playback.c
index e93a16f6958e..10e273f4b77a 100644
--- a/sound/usb/line6/playback.c
+++ b/sound/usb/line6/playback.c
@@ -414,6 +414,7 @@ const struct snd_pcm_ops snd_line6_playback_ops = {
 	.hw_free = snd_line6_hw_free,
 	.prepare = snd_line6_prepare,
 	.trigger = snd_line6_trigger,
+	.sync_stop = snd_line6_sync_stop,
 	.pointer = snd_line6_pointer,
 };
 
-- 
2.34.1


^ permalink raw reply related	[flat|nested] 6+ messages in thread

* Re: [PATCH v5] ALSA: line6: Stop URBs synchronously via sync_stop callback
  2026-11-22  9:34   ` [PATCH v5] " YuanZhe Liu
@ 2026-11-22  9:51     ` sashiko-bot
  0 siblings, 0 replies; 6+ messages in thread
From: sashiko-bot @ 2026-11-22  9:51 UTC (permalink / raw)
  To: YuanZhe Liu; +Cc: linux-sound

Thank you for your contribution! Sashiko AI review found 2 potential
issues to consider:
- [Medium] ALSA: line6: missing URB sync on shared stream stop before
  snd_line6_prepare()
- [Medium] ALSA: line6: race between line6_kill_audio_urbs() and
  snd_line6_trigger()
--

commit 61dfc4a9ef88afb11aedb1393a3ece64bb26365b
Author: YuanZhe Liu <25031212351@stu.xidian.edu.cn>

ALSA: line6: Stop URBs synchronously via sync_stop callback

This commit replaces the polling in line6_wait_clear_audio_urbs() with
synchronous URB termination via usb_kill_urb() in line6_kill_audio_urbs(),
wires it up to the PCM sync_stop callback, and drops the wait from
snd_line6_prepare().

> diff --git a/sound/usb/line6/pcm.c b/sound/usb/line6/pcm.c
> index d17baf7761c67..16e6f9900514b 100644
> --- a/sound/usb/line6/pcm.c
> +++ b/sound/usb/line6/pcm.c
> @@ -109,29 +109,37 @@ static void line6_unlink_audio_urbs(struct snd_line6_pcm *line6pcm,
>  }
>  
>  /*
> -	Wait until unlinking of all currently active URBs has been finished.
> -*/
> -static void line6_wait_clear_audio_urbs(struct snd_line6_pcm *line6pcm,
> -					struct line6_pcm_stream *pcms)
> + * Mark all currently active URBs for unlinking and kill them.
> + *
> + * The unlink bit is set under the stream lock, so the completion
> + * callbacks cannot resubmit these URBs any longer.  usb_kill_urb() then
> + * waits for the giveback of every URB, hence the stream buffer is no
> + * longer referenced by the host controller.  The unlink marks are
> + * cleared afterwards, so that a later stream start can submit the URBs
> + * again.  This function must be called from a sleepable context.
> + */
> +static void line6_kill_audio_urbs(struct snd_line6_pcm *line6pcm,
> +				  struct line6_pcm_stream *pcms)
>  {
> -	int timeout = HZ;
>  	int i;
> -	int alive;
>  
> -	do {
> -		alive = 0;
> -		for (i = 0; i < line6pcm->line6->iso_buffers; i++) {
> +	/* May be called when the URB creation failed during the probe */
> +	if (!pcms->urbs)
> +		return;
> +
> +	scoped_guard(spinlock_irqsave, &pcms->lock) {
> +		for (i = 0; i < line6pcm->line6->iso_buffers; i++)
>  			if (test_bit(i, &pcms->active_urbs))
> -				alive++;
> -		}
> -		if (!alive)
> -			break;
> -		set_current_state(TASK_UNINTERRUPTIBLE);
> -		schedule_timeout(1);
> -	} while (--timeout > 0);
> -	if (alive)
> -		dev_err(line6pcm->line6->ifcdev,
> -			"timeout: still %d active urbs..\n", alive);
> +				set_bit(i, &pcms->unlink_urbs);
> +	}
> +
> +	for (i = 0; i < line6pcm->line6->iso_buffers; i++)
> +		if (pcms->urbs[i])
> +			usb_kill_urb(pcms->urbs[i]);
> +
> +	scoped_guard(spinlock_irqsave, &pcms->lock) {
> +		pcms->unlink_urbs = 0;
> +	}
>  }

[Severity: Medium]
Can line6_kill_audio_urbs() race with a concurrent snd_line6_trigger() on
a shared line6_pcm_stream?

line6_pcm_acquire() (called by snd_line6_capture_open() for both out and
in, and by mixer controls for monitor/impulse) and capture's
snd_line6_sync_stop() (which also kills URBs on line6pcm->out for
LINE6_CAP_IN_NEEDS_OUT devices) hold only line6pcm->state_mutex, check
!pstr->running outside pstr->lock, and call line6_kill_audio_urbs().
Meanwhile, an already-prepared PCM substream on that direction can enter
snd_line6_trigger(SNDRV_PCM_TRIGGER_START) -> line6_stream_start(), which
only takes pstr->lock:

CPU0                                  CPU1
snd_line6_capture_open()
  line6_pcm_acquire()
    lock(&line6pcm->state_mutex)
    if (!out.running)
      line6_kill_audio_urbs(..., &out)
        lock(&out.lock)
        /* active_urbs == 0 */
        unlock(&out.lock)
                                      snd_line6_trigger(PLAYBACK, START)
                                        line6_stream_start(..., &out)
                                          lock(&out.lock)
                                          set_bit(..., &out.running)
                                          line6_submit_audio_out_all_urbs()
                                          unlock(&out.lock)
        for (i = 0; i < iso_buffers; i++)
          usb_kill_urb(out.urbs[i])  <-- kills newly submitted playback URBs

Because line6_kill_audio_urbs() does not recheck pcms->running under
pcms->lock and drops pcms->lock across the unconditional usb_kill_urb()
loop, a concurrent playback start either has its newly submitted URBs
killed (leaving playback running with zero active URBs) or sees
active_urbs || unlink_urbs non-zero in line6_stream_start() and skips URB
submission.  In addition, if a stream starts and stops before the final
pcms->unlink_urbs = 0 assignment, its newly set unlink bits are cleared
and completion callbacks can resubmit URBs after stop.

[ ... ]

> @@ -592,15 +598,36 @@ int line6_init_pcm(struct usb_line6 *line6,
>  }
>  EXPORT_SYMBOL_GPL(line6_init_pcm);
>  
> -/* prepare pcm callback */
> -int snd_line6_prepare(struct snd_pcm_substream *substream)
> +/* common PCM sync_stop callback */
> +int snd_line6_sync_stop(struct snd_pcm_substream *substream)
>  {
>  	struct snd_line6_pcm *line6pcm = snd_pcm_substream_chip(substream);
>  	struct line6_pcm_stream *pstr = get_stream(line6pcm, substream->stream);
>  
>  	guard(mutex)(&line6pcm->state_mutex);
>  	if (!pstr->running)
> -		line6_wait_clear_audio_urbs(line6pcm, pstr);
> +		line6_kill_audio_urbs(line6pcm, pstr);
> +	/*
> +	 * A capture stream may use the playback stream as a helper, and
> +	 * snd_line6_trigger() stops it together with the capture stream.
> +	 * Stop the leftover playback URBs here as well, so that the helper
> +	 * stream can be restarted at the next trigger; skip it while
> +	 * another playback stream is still running.
> +	 */
> +	if (substream->stream == SNDRV_PCM_STREAM_CAPTURE &&
> +	    (line6pcm->line6->properties->capabilities &
> +	     LINE6_CAP_IN_NEEDS_OUT) &&
> +	    !line6pcm->out.running)
> +		line6_kill_audio_urbs(line6pcm, &line6pcm->out);
> +	return 0;
> +}
> +
> +/* prepare pcm callback */
> +int snd_line6_prepare(struct snd_pcm_substream *substream)
> +{
> +	struct snd_line6_pcm *line6pcm = snd_pcm_substream_chip(substream);
> +
> +	guard(mutex)(&line6pcm->state_mutex);

[Severity: Medium]
Does dropping the !pstr->running URB synchronization from
snd_line6_prepare() leave unlinking URBs unsynchronized when another
stream sharing pstr stops after this substream?

snd_pcm_sync_stop() only invokes snd_line6_sync_stop() when
substream->runtime->stop_operating is set, and clears stop_operating
immediately.  Two cases then reach snd_line6_prepare() and
line6_stream_start() with pstr->running == 0 without ever calling
line6_kill_audio_urbs():

1. A PCM substream stops first while another stream type (such as
   LINE6_STREAM_MONITOR, LINE6_STREAM_IMPULSE, or a companion PCM/capture
   helper stream) is still running on pstr.  Its snd_line6_sync_stop()
   consumes stop_operating but skips line6_kill_audio_urbs() because
   pstr->running is still non-zero.  When the other stream later stops
   (for example via XRUN in trigger(STOP) -> line6_stream_stop(), or via
   __line6_pcm_release()), pstr->running drops to 0 with only an
   asynchronous line6_unlink_audio_urbs() call.
2. LINE6_STREAM_MONITOR or LINE6_STREAM_IMPULSE is stopped via
   __line6_pcm_release() while an open PCM substream holds a bit in
   pstr->opened (such as BIT(LINE6_STREAM_PCM) after hw_params or
   BIT(LINE6_STREAM_CAPTURE_HELPER) after capture open).
   line6_buffer_release() skips line6_kill_audio_urbs() because
   pstr->opened is non-zero, and stopping monitor/impulse does not set
   stop_operating on the PCM substream.

When the PCM substream is then prepared and started, line6_stream_start()
can see pstr->active_urbs || pstr->unlink_urbs still non-zero:

sound/usb/line6/pcm.c:line6_stream_start() {
	guard(spinlock_irqsave)(&pstr->lock);
	if (!test_and_set_bit(type, &pstr->running) &&
	    !(pstr->active_urbs || pstr->unlink_urbs)) {
		pstr->count = 0;
		...
}

line6_stream_start() sets BIT(type) in pstr->running and skips URB
submission, leaving the stream stalled with no active URBs once the
unlinked URBs complete (or allowing an URB that raced with the unlocked
line6_unlink_audio_urbs() scan to keep resubmitting while stopped).

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261122093454.14422-1-25031212351@stu.xidian.edu.cn?part=1

^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2026-11-22  9:51 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-21 10:40 [PATCH v3] ALSA: line6: Kill URBs before releasing the PCM buffer YuanZhe Liu
2026-11-22  8:16 ` Takashi Iwai
2026-11-22  9:04   ` [PATCH v4] ALSA: line6: Stop URBs synchronously via sync_stop callback YuanZhe Liu
2026-11-22  9:20     ` sashiko-bot
2026-11-22  9:34   ` [PATCH v5] " YuanZhe Liu
2026-11-22  9:51     ` sashiko-bot

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox