mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
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: core: serialize card->devices operations with a re-entrant devices lock
Date: Tue, 02 Dec 2026 10:06:02 +0800	[thread overview]
Message-ID: <20261202100602.1-1-xuhx@hust.edu.cn> (raw)

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


             reply	other threads:[~2026-12-02  3:10 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-12-02  2:06 Hanxiang Xu [this message]
2026-12-02  8:56 ` Takashi Iwai

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=20261202100602.1-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®