Re: [PATCH] ALSA: line6: Fix endless MIDI transmit loop on closed substream
From: Takashi Iwai
Date: Sun Dec 06 2026 - 06:11:10 EST
On Fri, 04 Dec 2026 17:13:27 +0100,
YuanZhe Liu wrote:
>
> 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>
Applied to for-next branch. Thanks.
Takashi