From: Takashi Iwai <tiwai@suse.de>
To: linux-sound@vger.kernel.org
Cc: linux-kernel@vger.kernel.org
Subject: [PATCH] ALSA: rawmidi: Add a proper disconnect handling
Date: Thu, 3 Sep 2026 12:36:58 +0200 [thread overview]
Message-ID: <20260903103728.1807281-1-tiwai@suse.de> (raw)
The rawmidi core detaches only exposed devices at disconnection, but
it doesn't deal with the pending bytes or gate the further unexpected
accesses, leaving naively to each driver dealing with such situations.
Let's try to restrict it in the core side for more safety: introduce
the disconnected flag to each substream, set it at disconnection call,
then trigger down & cancel the pending event work.
Signed-off-by: Takashi Iwai <tiwai@suse.de>
---
include/sound/rawmidi.h | 1 +
sound/core/rawmidi.c | 16 +++++++++++-----
2 files changed, 12 insertions(+), 5 deletions(-)
diff --git a/include/sound/rawmidi.h b/include/sound/rawmidi.h
index 4154035af414..d812f70725bd 100644
--- a/include/sound/rawmidi.h
+++ b/include/sound/rawmidi.h
@@ -86,6 +86,7 @@ struct snd_rawmidi_substream {
bool opened; /* open flag */
bool append; /* append flag (merge more streams) */
bool active_sensing; /* send active sensing when close */
+ bool disconnected; /* already disconnected */
unsigned int framing; /* whether to frame input data */
unsigned int clock_type; /* clock source to use for input framing */
int use_count; /* use counter (for output) */
diff --git a/sound/core/rawmidi.c b/sound/core/rawmidi.c
index 2617bb5b4faf..a9f812bd9b56 100644
--- a/sound/core/rawmidi.c
+++ b/sound/core/rawmidi.c
@@ -208,14 +208,14 @@ static int snd_rawmidi_runtime_free(struct snd_rawmidi_substream *substream)
static inline void snd_rawmidi_output_trigger(struct snd_rawmidi_substream *substream, int up)
{
- if (!substream->opened)
+ if (!substream->opened || substream->disconnected)
return;
substream->ops->trigger(substream, up);
}
static void snd_rawmidi_input_trigger(struct snd_rawmidi_substream *substream, int up)
{
- if (!substream->opened)
+ if (!substream->opened || substream->disconnected)
return;
substream->ops->trigger(substream, up);
if (!up)
@@ -254,7 +254,8 @@ int snd_rawmidi_drain_output(struct snd_rawmidi_substream *substream)
scoped_guard(spinlock_irq, &substream->lock) {
runtime = substream->runtime;
- if (!substream->opened || !runtime || !runtime->buffer)
+ if (!substream->opened || !runtime || !runtime->buffer ||
+ substream->disconnected)
return -EINVAL;
snd_rawmidi_buffer_ref(runtime);
runtime->drain = 1;
@@ -537,7 +538,7 @@ static void close_substream(struct snd_rawmidi *rmidi,
if (--substream->use_count)
return;
- if (cleanup) {
+ if (cleanup && !substream->disconnected) {
if (substream->stream == SNDRV_RAWMIDI_STREAM_INPUT)
snd_rawmidi_input_trigger(substream, 0);
else {
@@ -2063,8 +2064,13 @@ static int snd_rawmidi_dev_disconnect(struct snd_device *device)
struct snd_rawmidi_substream *s;
list_for_each_entry(s, &rmidi->streams[dir].substreams, list) {
- if (s->runtime)
+ scoped_guard(spinlock_irq, &s->lock)
+ s->disconnected = true;
+ if (s->runtime) {
+ s->ops->trigger(s, false);
+ cancel_work_sync(&s->runtime->event_work);
wake_up(&s->runtime->sleep);
+ }
}
}
--
2.55.0
reply other threads:[~2026-09-03 10:37 UTC|newest]
Thread overview: [no followups] expand[flat|nested] mbox.gz Atom feed
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=20260903103728.1807281-1-tiwai@suse.de \
--to=tiwai@suse.de \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-sound@vger.kernel.org \
/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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox