All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH] ALSA: rawmidi: keep struct snd_rawmidi alive until the last open file is closed
@ 2026-12-02  2:31 Hanxiang Xu
  2026-12-02  3:23 ` sashiko-bot
  0 siblings, 1 reply; 2+ messages in thread
From: Hanxiang Xu @ 2026-12-02  2:31 UTC (permalink / raw)
  To: Takashi Iwai, Jaroslav Kysela; +Cc: linux-sound, linux-kernel

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


^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2026-12-02  3:23 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-12-02  2:31 [PATCH] ALSA: rawmidi: keep struct snd_rawmidi alive until the last open file is closed Hanxiang Xu
2026-12-02  3:23 ` sashiko-bot

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.