From: Hanxiang Xu <xuhx@hust.edu.cn>
To: Takashi Iwai <tiwai@suse.com>, Jaroslav Kysela <perex@perex.cz>
Cc: linux-sound@vger.kernel.org, linux-kernel@vger.kernel.org
Subject: [PATCH] ALSA: rawmidi: keep struct snd_rawmidi alive until the last open file is closed
Date: Tue, 02 Dec 2026 10:31:00 +0800 [thread overview]
Message-ID: <20261202103100.2-1-xuhx@hust.edu.cn> (raw)
When a sub-driver removes its rawmidi device from a live card with
snd_device_free(), snd_rawmidi_dev_disconnect() does not wait for open
files and snd_rawmidi_free() immediately frees the substreams, the
driver private data and struct snd_rawmidi itself. Open files keep
rawmidi_file->rmidi and ->output pointing at the freed objects, a
close() runs snd_rawmidi_release() on the freed rmidi (open_mutex,
rmidi->card), and a queued output work item can still reference the
freed substream.
Anchor the lifetime of the rawmidi memory on its embedded device: the
release callback now performs free_substreams()/private_free()/
kfree(rmidi) once the last reference is dropped, opening the device
(userspace and kernel variants) takes a reference and every
close/error path drops it again. snd_rawmidi_free() only releases
the registry reference, so the un-drained work item can no longer
touch freed memory.
Cc: <stable@vger.kernel.org>
Signed-off-by: Hanxiang Xu <xuhx@hust.edu.cn>
---
sound/core/rawmidi.c | 31 +++++++++++++++++++++++++------
1 file changed, 25 insertions(+), 6 deletions(-)
diff --git a/sound/core/rawmidi.c b/sound/core/rawmidi.c
index 630d9fc8e9da0a0e7d6a2eded6aed531b094b932..824c37a7773d92d387d39d66150d5bf1778c381a 100644
@@ -469,11 +469,14 @@
return -EINVAL;
if (!try_module_get(rmidi->card->module))
return -ENXIO;
+ get_device(rmidi->dev);
mutex_lock_nested(&rmidi->open_mutex, depth);
err = rawmidi_open_priv(rmidi, subdevice, mode, rfile);
- if (err < 0)
+ if (err < 0) {
module_put(rmidi->card->module);
+ put_device(rmidi->dev);
+ }
mutex_unlock(&rmidi->open_mutex);
return err;
}
@@ -508,8 +511,10 @@ static int snd_rawmidi_open(struct inode *inode, struct file *file)
if (rmidi == NULL)
return -ENODEV;
+ get_device(rmidi->dev);
if (!try_module_get(rmidi->card->module)) {
+ put_device(rmidi->dev);
snd_card_unref(rmidi->card);
return -ENXIO;
}
@@ -577,6 +582,7 @@ static int snd_rawmidi_open(struct inode *inode, struct file *file)
mutex_unlock(&rmidi->open_mutex);
module_put(rmidi->card->module);
snd_card_unref(rmidi->card);
+ put_device(rmidi->dev);
return err;
}
@@ -650,6 +656,7 @@ static void rawmidi_release_priv(struct snd_rawmidi_file *rfile)
rawmidi_release_priv(rfile);
mutex_unlock(&rmidi->open_mutex);
module_put(rmidi->card->module);
+ put_device(rmidi->dev);
return 0;
}
EXPORT_SYMBOL(snd_rawmidi_kernel_release_nested);
@@ -668,6 +675,7 @@ static int snd_rawmidi_release(struct inode *inode, struct file *f...
module = rmidi->card->module;
snd_card_file_remove(rmidi->card, file);
module_put(module);
+ put_device(rmidi->dev);
return 0;
}
@@ -1900,6 +1908,7 @@ static __poll_t snd_rawmidi_poll(struct file *file, poll_table *wait)
err = snd_device_alloc(&rmidi->dev, card);
if (err < 0)
return err;
+ rmidi->dev->release = snd_rawmidi_dev_release;
if (rawmidi_is_ump(rmidi))
dev_set_name(rmidi->dev, "umpC%iD%i", card->number, device);
else
@@ -1973,6 +1982,21 @@ static void snd_rawmidi_free_substreams(struct snd_rawmidi_str *st...
}
}
+/*
+ * The rawmidi memory is freed by the embedded device's release
+ * callback once the last reference (an open file) is dropped.
+ */
+static void snd_rawmidi_dev_release(struct device *dev)
+{
+ struct snd_rawmidi *rmidi = container_of(dev, struct snd_rawmidi, dev);
+
+ snd_rawmidi_free_substreams(&rmidi->streams[SNDRV_RAWMIDI_STREAM_INPUT]);
+ snd_rawmidi_free_substreams(&rmidi->streams[SNDRV_RAWMIDI_STREAM_OUTPUT]);
+ if (rmidi->private_free)
+ rmidi->private_free(rmidi);
+ kfree(rmidi);
+}
+
/* called from ump.c, too */
int snd_rawmidi_free(struct snd_rawmidi *rmidi)
{
@@ -1984,12 +2008,7 @@ int snd_rawmidi_free(struct snd_rawmidi *rmidi)
if (rmidi->ops && rmidi->ops->dev_unregister)
rmidi->ops->dev_unregister(rmidi);
- snd_rawmidi_free_substreams(&rmidi->streams[SNDRV_RAWMIDI_STREAM_INPUT]);
- snd_rawmidi_free_substreams(&rmidi->streams[SNDRV_RAWMIDI_STREAM_OUTPUT]);
- if (rmidi->private_free)
- rmidi->private_free(rmidi);
put_device(rmidi->dev);
- kfree(rmidi);
return 0;
}
EXPORT_SYMBOL_GPL(snd_rawmidi_free);
--
2.43.0
reply other threads:[~2026-12-02 3:06 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=20261202103100.2-1-xuhx@hust.edu.cn \
--to=xuhx@hust.edu.cn \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-sound@vger.kernel.org \
--cc=perex@perex.cz \
--cc=tiwai@suse.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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®