* [PATCH v2] ALSA: line6: Kill URBs before releasing the PCM buffer
@ 2026-11-21 9:56 YuanZhe Liu
2026-11-21 10:21 ` sashiko-bot
0 siblings, 1 reply; 2+ messages in thread
From: YuanZhe Liu @ 2026-11-21 9:56 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 them
synchronously. usb_kill_urb() waits for the giveback, hence the buffer is
no longer referenced by any URB when it is freed.
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>
---
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 | 27 ++++++++++++++++++++++++++-
1 file changed, 26 insertions(+), 1 deletion(-)
diff --git a/sound/usb/line6/pcm.c b/sound/usb/line6/pcm.c
index d17baf7761c6..6a8983507a39 100644
--- a/sound/usb/line6/pcm.c
+++ b/sound/usb/line6/pcm.c
@@ -134,6 +134,31 @@ 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 until each of them is given back and hence the stream buffer is
+ * no longer referenced by the host controller. 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 (test_bit(i, &pcms->active_urbs))
+ usb_kill_urb(pcms->urbs[i]);
+}
+
static inline struct line6_pcm_stream *
get_stream(struct snd_line6_pcm *line6pcm, int direction)
{
@@ -172,7 +197,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] 2+ messages in thread
* Re: [PATCH v2] ALSA: line6: Kill URBs before releasing the PCM buffer
2026-11-21 9:56 [PATCH v2] ALSA: line6: Kill URBs before releasing the PCM buffer YuanZhe Liu
@ 2026-11-21 10:21 ` sashiko-bot
0 siblings, 0 replies; 2+ messages in thread
From: sashiko-bot @ 2026-11-21 10:21 UTC (permalink / raw)
To: YuanZhe Liu; +Cc: linux-sound
Thank you for your contribution! Sashiko AI review found 1 potential
issue to consider:
- [High] ALSA: line6: race and stale unlink_urbs in
line6_kill_audio_urbs()
--
commit c5d2d5929b697407f8e00e71a2f7fa35ef3b8431
Author: YuanZhe Liu <25031212351@stu.xidian.edu.cn>
ALSA: line6: Kill URBs before releasing the PCM buffer
This commit introduces line6_kill_audio_urbs() and calls it from
line6_buffer_release() instead of line6_wait_clear_audio_urbs() to stop and
wait for active URBs before freeing the PCM stream buffer.
> diff --git a/sound/usb/line6/pcm.c b/sound/usb/line6/pcm.c
> index d17baf7761c67..6a8983507a390 100644
> --- a/sound/usb/line6/pcm.c
> +++ b/sound/usb/line6/pcm.c
> @@ -134,6 +134,31 @@ 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 until each of them is given back and hence the stream buffer is
> + * no longer referenced by the host controller. 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 (test_bit(i, &pcms->active_urbs))
> + usb_kill_urb(pcms->urbs[i]);
> +}
[Severity: High]
Can line6_kill_audio_urbs() skip usb_kill_urb() while an URB completion
callback is still running, and leave stale bits in pcms->unlink_urbs?
In audio_out_callback() and handle_audio_in(), clear_bit() clears the bit
in active_urbs inside the completion handler before unlink_urbs is tested
and before __usb_hcd_giveback_urb() finishes:
CPU0 CPU1
line6_buffer_release()
line6_kill_audio_urbs()
lock(&pcms->lock)
set_bit(i, &pcms->unlink_urbs)
unlock(&pcms->lock)
__usb_hcd_giveback_urb()
audio_out_callback()
lock(&pcms->lock)
clear_bit(i, &pcms->active_urbs)
if (test_bit(i, &pcms->active_urbs))
/* false: skips usb_kill_urb() */
kfree(pstr->buffer)
...
atomic_dec(&urb->use_count)
Because the second loop checks active_urbs outside pcms->lock, CPU0 sees
active_urbs cleared and returns from line6_kill_audio_urbs() while the
callback and URB giveback are still executing on CPU1.
In addition, line6_stream_stop() runs line6_unlink_audio_urbs() without
holding pcms->lock before line6_buffer_release() is called. If URB k
completes right after line6_unlink_audio_urbs() tests active_urbs and its
callback resubmits using a lower free index (say URB 0),
line6_unlink_audio_urbs() sets bit k in unlink_urbs on the now-idle URB k.
line6_kill_audio_urbs() only kills URBs that are currently in active_urbs
and never clears pcms->unlink_urbs, so bit k remains set and subsequent
calls to line6_stream_start() see pstr->unlink_urbs != 0 and never submit
URBs again.
Calling usb_kill_urb(pcms->urbs[i]) unconditionally for all iso_buffers and
clearing pcms->unlink_urbs afterward would avoid both issues.
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261121095643.8196-1-25031212351@stu.xidian.edu.cn?part=1
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2026-11-21 10:21 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-21 9:56 [PATCH v2] ALSA: line6: Kill URBs before releasing the PCM buffer YuanZhe Liu
2026-11-21 10:21 ` sashiko-bot
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox