All of lore.kernel.org
 help / color / mirror / Atom feed
From: Takashi Iwai <tiwai@suse.de>
To: Cen Zhang <zzzccc427@gmail.com>
Cc: perex@perex.cz, tiwai@suse.com, phucduc.bui@gmail.com,
	len.bao@gmx.us, hariconscious@gmail.com, sammiee5311@gmail.com,
	kees@kernel.org, akhilarul324@gmail.com,
	cezary.rojewski@intel.com, linux-sound@vger.kernel.org,
	linux-kernel@vger.kernel.org, baijiaju1990@gmail.com,
	jjzuming@gmail.com
Subject: Re: [PATCH] ALSA: rawmidi: Drain input work before freeing the runtime
Date: Tue, 13 Oct 2026 11:42:27 +0200	[thread overview]
Message-ID: <87a4oigcn0.wl-tiwai@suse.de> (raw)
In-Reply-To: <pm-pmb-98489078ab56528d8e4a-v2-9408ce8c8f3c70b5c10d@gmail.com>

On Tue, 13 Oct 2026 09:54:35 +0200,
Cen Zhang wrote:
> 
> The input runtime embeds event_work, so the work must be drained before
> close_substream() frees the runtime. The cancellation in
> snd_rawmidi_input_trigger() runs before close clears substream->opened,
> leaving a window for snd_rawmidi_receive() to enqueue more work.
> 
> With a sequencer subscription to a USB MIDI input port, an in-flight
> completion can overlap the last subscription's midisynth_unsubscribe():
> 
> 1. snd_usbmidi_input_data() observes input_triggered and is delayed
>    before calling snd_rawmidi_receive().
> 2. Unsubscribe enters close_substream(), clears input_triggered through
>    the provider trigger callback and returns from cancel_work_sync().
>    The substream is still opened.
> 3. The completion resumes. snd_rawmidi_receive() takes substream->lock,
>    sees opened set and queues runtime->event_work before dropping the
>    lock.
> 4. Close clears opened under that lock, closes the provider and frees
>    the runtime. Stopping USB URBs does not drain the raw-MIDI work.
> 5. The queued work runs and snd_rawmidi_input_event_work() reads event
>    or substream from the freed runtime, causing a use-after-free.
> 
> Cancel input event_work again after clearing opened, before closing the
> provider or releasing the runtime's private data. Receive holds
> substream->lock through its opened check and work enqueue, so once close
> has cleared opened under that lock, no further receive can queue work.
> The final cancellation therefore drains any late work before the
> runtime is freed. Keep it outside the spinlock so a running callback can
> finish.
> 
> Fixes: b3c705aa9e91 ("ALSA: rawmidi - Use workq for event handling")
> Assisted-by: LLM
> Signed-off-by: Cen Zhang <zzzccc427@gmail.com>

Applied to for-next branch now.  Thanks.


Takashi

      reply	other threads:[~2026-10-13  9:42 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-10-13  7:54 [PATCH] ALSA: rawmidi: Drain input work before freeing the runtime Cen Zhang
2026-10-13  9:42 ` Takashi Iwai [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=87a4oigcn0.wl-tiwai@suse.de \
    --to=tiwai@suse.de \
    --cc=akhilarul324@gmail.com \
    --cc=baijiaju1990@gmail.com \
    --cc=cezary.rojewski@intel.com \
    --cc=hariconscious@gmail.com \
    --cc=jjzuming@gmail.com \
    --cc=kees@kernel.org \
    --cc=len.bao@gmx.us \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-sound@vger.kernel.org \
    --cc=perex@perex.cz \
    --cc=phucduc.bui@gmail.com \
    --cc=sammiee5311@gmail.com \
    --cc=tiwai@suse.com \
    --cc=zzzccc427@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.