[PATCH] ALSA: line6: Fix endless MIDI transmit loop on closed substream

From: YuanZhe Liu

Date: Fri Dec 04 2026 - 11:13:42 EST


line6_midi_transmit() moves data from the rawmidi output buffer into the
driver's MIDI buffer in a loop:

for (;;) {
...
done = snd_rawmidi_transmit_peek(substream, chunk, req);
if (done == 0)
break;
line6_midibuf_write(mb, chunk, done);
snd_rawmidi_transmit_ack(substream, done);
}

snd_rawmidi_transmit_peek() returns -EBADFD when the substream has been
closed or its runtime released, but the loop exits only on a zero return.
line6_midibuf_write() turns the negative length into a no-op and
snd_rawmidi_transmit_ack() fails without advancing the buffer pointers,
so the loop spins forever.

That state is reachable when userspace closes the rawmidi output while a
MIDI URB is still in flight. snd_rawmidi_drain_output() skips the
driver's drain callback when the wait is interrupted by a signal, and
line6_midi_output_close() does not stop pending URBs. When such an URB
completes, midi_sent() calls line6_midi_transmit() for the already closed
substream, and the CPU stays in the URB completion softirq without ever
reporting a quiescent state.

Break out of the loop on any non-positive return, treating a negative
error as "no data left", as was already done for the receive path in
commit d683469b3c93 ("ALSA: line6: Fix endless MIDI read loop").

Fixes: 463a20fd3481 ("ALSA: rawmidi: Check stream state at exported functions")
Cc: stable@xxxxxxxxxxxxxxx
Co-developed-by: Bin Yu <byu@xxxxxxxxxxxxx>
Signed-off-by: Bin Yu <byu@xxxxxxxxxxxxx>
Co-developed-by: Cheng Wen <wencheng@xxxxxxxxxxxxx>
Signed-off-by: Cheng Wen <wencheng@xxxxxxxxxxxxx>
Signed-off-by: YuanZhe Liu <25031212351@xxxxxxxxxxxxxxxxx>
---
sound/usb/line6/midi.c | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/sound/usb/line6/midi.c b/sound/usb/line6/midi.c
index af01b649393c..9a0234834eaa 100644
--- a/sound/usb/line6/midi.c
+++ b/sound/usb/line6/midi.c
@@ -48,7 +48,7 @@ static void line6_midi_transmit(struct snd_rawmidi_substream *substream)
LINE6_FALLBACK_MAXPACKETSIZE);
done = snd_rawmidi_transmit_peek(substream, chunk, req);

- if (done == 0)
+ if (done <= 0)
break;

line6_midibuf_write(mb, chunk, done);
--
2.34.1