Re: [PATCH] ALSA: seq: midi: Serialize output teardown with event_input

From: Takashi Iwai

Date: Wed May 27 2026 - 01:34:41 EST


On Tue, 26 May 2026 17:26:08 +0200,
Zhang Cen wrote:
> @@ -125,31 +128,44 @@ static int event_process_midi(struct snd_seq_event *ev, int direct,
> struct seq_midisynth *msynth = private_data;
> unsigned char msg[10]; /* buffer for constructing midi messages */
> struct snd_rawmidi_substream *substream;
> + unsigned long flags;
> + int err = 0;
> int len;
>
> if (snd_BUG_ON(!msynth))
> return -EINVAL;
> +
> + spin_lock_irqsave(&msynth->output_lock, flags);
> substream = msynth->output_rfile.output;
> - if (substream == NULL)
> + if (substream)
> + snd_use_lock_use(&msynth->output_use_lock);
> + spin_unlock_irqrestore(&msynth->output_lock, flags);
> +
> + if (!substream)
> return -ENODEV;

Please use scoped_guard(). You can drop flags with it, and the return
-ENODEV can be right after substream assignment.

> @@ -229,12 +249,15 @@ static int midisynth_use(void *private_data, struct snd_seq_port_subscribe *info
> params.avail_min = 1;
> params.buffer_size = output_buffer_size;
> params.no_active_sensing = 1;
> - err = snd_rawmidi_output_params(msynth->output_rfile.output, &params);
> + err = snd_rawmidi_output_params(rfile.output, &params);
> if (err < 0) {
> - snd_rawmidi_kernel_release(&msynth->output_rfile);
> + snd_rawmidi_kernel_release(&rfile);
> return err;
> }
> snd_midi_event_reset_decode(msynth->parser);
> + spin_lock_irqsave(&msynth->output_lock, flags);
> + msynth->output_rfile = rfile;
> + spin_unlock_irqrestore(&msynth->output_lock, flags);
> return 0;

Ditto, use guard() or scoped_guard().

> @@ -242,11 +265,20 @@ static int midisynth_use(void *private_data, struct snd_seq_port_subscribe *info
> static int midisynth_unuse(void *private_data, struct snd_seq_port_subscribe *info)
> {
> struct seq_midisynth *msynth = private_data;
> + struct snd_rawmidi_file rfile = {};
> + unsigned long flags;
>
> - if (snd_BUG_ON(!msynth->output_rfile.output))
> + spin_lock_irqsave(&msynth->output_lock, flags);
> + rfile = msynth->output_rfile;
> + msynth->output_rfile = (struct snd_rawmidi_file){};
> + spin_unlock_irqrestore(&msynth->output_lock, flags);
> +
> + if (snd_BUG_ON(!rfile.output))
> return -EINVAL;

Same here, too.


thanks,

Takashi