Linux Sound subsystem development
 help / color / mirror / Atom feed
* [PATCH] ALSA: core: serialize card->devices operations with a re-entrant devices lock
@ 2026-12-02  2:06 Hanxiang Xu
  2026-12-02  3:22 ` sashiko-bot
  0 siblings, 1 reply; 2+ messages in thread
From: Hanxiang Xu @ 2026-12-02  2:06 UTC (permalink / raw)
  To: Takashi Iwai, Jaroslav Kysela; +Cc: linux-sound, linux-kernel

The card->devices list is manipulated with no serialization:
snd_device_free() does list_del() and kfree(dev) while
snd_device_disconnect_all() walks the list dereferencing every entry,
so an independently removable sub-driver racing a card disconnect makes
the walker read freed memory or advance through LIST_POISON2 and Oops.

Add a devices_mutex to struct snd_card and take it in
snd_device_new()/snd_device_free()/snd_device_disconnect()/
snd_device_register()/snd_device_register_all()/
snd_device_disconnect_all()/snd_device_free_all().  The lock tracks its
owner so the legitimate re-entrancy keeps working: disconnect callbacks
run under the lock and may themselves call snd_device_free() for
sibling devices (the sequencer sub-driver remove path).

Cc: <stable@vger.kernel.org>
Signed-off-by: Hanxiang Xu <xuhx@hust.edu.cn>
---
 include/sound/core.h | 3 +++
 sound/core/device.c  | 45 ++++++++++++++++++++++++++++++++++++++++++---
 sound/core/init.c    | 1 +
 3 files changed, 46 insertions(+), 3 deletions(-)

diff --git a/include/sound/core.h b/include/sound/core.h
index d658496..50ed297 100644
--- a/include/sound/core.h
+++ b/include/sound/core.h
@@ -117,6 +117,9 @@ struct snd_card {
 	void (*private_free) (struct snd_card *card); /* callback for freeing of
 								private data */
 	struct list_head devices;	/* devices */
+	struct mutex devices_mutex;	/* protects the devices list */
+	struct task_struct *devices_owner;
+	int devices_recursion;
 
 	struct device *ctl_dev;		/* control device */
 	unsigned int last_numid;	/* last used numeric ID */
diff --git a/sound/core/device.c b/sound/core/device.c
index 5f2aa47..421e804 100644
--- a/sound/core/device.c
+++ b/sound/core/device.c
@@ -10,6 +10,25 @@
 #include <linux/errno.h>
 #include <sound/core.h>
 
+static void snd_devices_lock(struct snd_card *card)
+{
+	if (card->devices_owner == current) {
+		card->devices_recursion++;
+		return;
+	}
+	mutex_lock(&card->devices_mutex);
+	card->devices_owner = current;
+	card->devices_recursion = 1;
+}
+
+static void snd_devices_unlock(struct snd_card *card)
+{
+	if (--card->devices_recursion)
+		return;
+	card->devices_owner = NULL;
+	mutex_unlock(&card->devices_mutex);
+}
+
 /**
  * snd_device_new - create an ALSA device component
  * @card: the card instance
@@ -34,9 +53,12 @@ int snd_device_new(struct snd_card *card, enum snd_device_type type,
 
 	if (snd_BUG_ON(!card || !device_data || !ops))
 		return -ENXIO;
+	snd_devices_lock(card);
 	dev = kzalloc_obj(*dev);
-	if (!dev)
+	if (!dev) {
+		snd_devices_unlock(card);
 		return -ENOMEM;
+	}
 	INIT_LIST_HEAD(&dev->list);
 	dev->card = card;
 	dev->type = type;
@@ -52,6 +74,7 @@ int snd_device_new(struct snd_card *card, enum snd_device_type type,
 	}
 
 	list_add(&dev->list, p);
+	snd_devices_unlock(card);
 	return 0;
 }
 EXPORT_SYMBOL(snd_device_new);
@@ -109,12 +132,14 @@ void snd_device_disconnect(struct snd_card *card, void *device_data)
 
 	if (snd_BUG_ON(!card || !device_data))
 		return;
+	snd_devices_lock(card);
 	dev = look_for_dev(card, device_data);
 	if (dev)
 		__snd_device_disconnect(dev);
 	else
 		dev_dbg(card->dev, "device disconnect %p (from %pS), not found\n",
 			device_data, __builtin_return_address(0));
+	snd_devices_unlock(card);
 }
 EXPORT_SYMBOL_GPL(snd_device_disconnect);
 
@@ -133,12 +158,14 @@ void snd_device_free(struct snd_card *card, void *device_data)
 	
 	if (snd_BUG_ON(!card || !device_data))
 		return;
+	snd_devices_lock(card);
 	dev = look_for_dev(card, device_data);
 	if (dev)
 		__snd_device_free(dev);
 	else
 		dev_dbg(card->dev, "device free %p (from %pS), not found\n",
 			device_data, __builtin_return_address(0));
+	snd_devices_unlock(card);
 }
 EXPORT_SYMBOL(snd_device_free);
 
@@ -174,9 +201,13 @@ int snd_device_register(struct snd_card *card, void *device_data)
 
 	if (snd_BUG_ON(!card || !device_data))
 		return -ENXIO;
+	snd_devices_lock(card);
 	dev = look_for_dev(card, device_data);
-	if (dev)
+	if (dev) {
+		snd_devices_unlock(card);
 		return __snd_device_register(dev);
+	}
+	snd_devices_unlock(card);
 	snd_BUG();
 	return -ENXIO;
 }
@@ -193,11 +224,15 @@ int snd_device_register_all(struct snd_card *card)
 	
 	if (snd_BUG_ON(!card))
 		return -ENXIO;
+	snd_devices_lock(card);
 	list_for_each_entry(dev, &card->devices, list) {
 		err = __snd_device_register(dev);
-		if (err < 0)
+		if (err < 0) {
+			snd_devices_unlock(card);
 			return err;
+		}
 	}
+	snd_devices_unlock(card);
 	return 0;
 }
 
@@ -211,8 +246,10 @@ void snd_device_disconnect_all(struct snd_card *card)
 
 	if (snd_BUG_ON(!card))
 		return;
+	snd_devices_lock(card);
 	list_for_each_entry_reverse(dev, &card->devices, list)
 		__snd_device_disconnect(dev);
+	snd_devices_unlock(card);
 }
 
 /*
@@ -225,6 +262,7 @@ void snd_device_free_all(struct snd_card *card)
 
 	if (snd_BUG_ON(!card))
 		return;
+	snd_devices_lock(card);
 	list_for_each_entry_safe_reverse(dev, next, &card->devices, list) {
 		/* exception: free ctl and lowlevel stuff later */
 		if (dev->type == SNDRV_DEV_CONTROL ||
@@ -236,4 +274,5 @@ void snd_device_free_all(struct snd_card *card)
 	/* free all */
 	list_for_each_entry_safe_reverse(dev, next, &card->devices, list)
 		__snd_device_free(dev);
+	snd_devices_unlock(card);
 }
diff --git a/sound/core/init.c b/sound/core/init.c
index d518cb8..5f22ae0 100644
--- a/sound/core/init.c
+++ b/sound/core/init.c
@@ -320,6 +320,7 @@ static int snd_card_init(struct snd_card *card, struct device *parent,
 	spin_lock_init(&card->files_lock);
 	INIT_LIST_HEAD(&card->files_list);
 	mutex_init(&card->memory_mutex);
+	mutex_init(&card->devices_mutex);
 #ifdef CONFIG_PM
 	init_waitqueue_head(&card->power_sleep);
 	snd_refcount_init(&card->power_ref);
-- 
2.43.0


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

end of thread, other threads:[~2026-12-02  3:22 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:06 [PATCH] ALSA: core: serialize card->devices operations with a re-entrant devices lock Hanxiang Xu
2026-12-02  3:22 ` sashiko-bot

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox