[PATCH 1/3] ALSA: core: serialize card disconnect against concurrent free
From: Hanxiang Xu
Date: Tue Dec 01 2026 - 22:01:56 EST
snd_card_disconnect() early-returns when card->shutdown is already
set, and otherwise runs its whole teardown body with no serialization
and no reference held on the card. A concurrent snd_card_free() can
therefore act on a half-disconnected card, and the release phase
(snd_card_do_free() -> snd_device_free_all() -> kfree(card)) can fire
while the disconnect loop is still walking card->devices, because the
last card reference is dropped by a device_del() inside the disconnect
itself.
Add card->disconnect_mutex and hold it for the whole
snd_card_disconnect() body, so a concurrent caller waits for an
in-flight disconnect to finish instead of acting on a half-state, and
pin the card with get_device()/put_device() for the duration of the
teardown.
Cc: <stable@xxxxxxxxxxxxxxx>
Signed-off-by: Hanxiang Xu <xuhx@xxxxxxxxxxx>
---
include/sound/core.h | 1 +
sound/core/init.c | 47 +++++++++++++++++++++++++++++------------------
diff --git a/include/sound/core.h b/include/sound/core.h
index d658496..3d98cff 100644
--- a/include/sound/core.h
+++ b/include/sound/core.h
@@ -140,6 +140,7 @@
state */
spinlock_t files_lock; /* lock the files for this card */
int shutdown; /* this card is going down */
+ struct mutex disconnect_mutex; /* serializes disconnect/free/register */
struct completion *release_completion;
struct device *dev; /* device assigned to this card */
struct device card_dev; /* cardX object for sysfs */
diff --git a/sound/core/init.c b/sound/core/init.c
index d518cb8..df0c759 100644
--- a/sound/core/init.c
+++ b/sound/core/init.c
@@ -318,6 +318,7 @@
xa_init(&card->ctl_hash);
#endif
spin_lock_init(&card->files_lock);
+ mutex_init(&card->disconnect_mutex);
INIT_LIST_HEAD(&card->files_list);
mutex_init(&card->memory_mutex);
#ifdef CONFIG_PM
@@ -493,24 +494,32 @@
if (!card)
return;
- scoped_guard(spinlock, &card->files_lock) {
- if (card->shutdown)
- return;
- card->shutdown = 1;
-
- /* replace file->f_op with special dummy operations */
- list_for_each_entry(mfile, &card->files_list, list) {
- /* it's critical part, use endless loop */
- /* we have no room to fail */
- mfile->disconnected_f_op = mfile->file->f_op;
-
- scoped_guard(spinlock, &shutdown_lock)
- list_add(&mfile->shutdown_list, &shutdown_files);
-
- mfile->file->f_op = &snd_shutdown_f_ops;
- fops_get(mfile->file->f_op);
- }
- }
+ mutex_lock(&card->disconnect_mutex);
+ get_device(&card->card_dev);
+
+ spin_lock(&card->files_lock);
+ if (card->shutdown) {
+ spin_unlock(&card->files_lock);
+ put_device(&card->card_dev);
+ mutex_unlock(&card->disconnect_mutex);
+ return;
+ }
+ card->shutdown = 1;
+
+ /* replace file->f_op with special dummy operations */
+ list_for_each_entry(mfile, &card->files_list, list) {
+ /* it's critical part, use endless loop */
+ /* we have no room to fail */
+ mfile->disconnected_f_op = mfile->file->f_op;
+
+ spin_lock(&shutdown_lock);
+ list_add(&mfile->shutdown_list, &shutdown_files);
+ spin_unlock(&shutdown_lock);
+
+ mfile->file->f_op = &snd_shutdown_f_ops;
+ fops_get(mfile->file->f_op);
+ }
+ spin_unlock(&card->files_lock);
#ifdef CONFIG_PM
/* wake up sleepers here before other callbacks for avoiding potential
@@ -553,6 +562,8 @@
/* order card->shutdown store against power_ref read */
smp_mb();
snd_power_sync_ref(card);
+ put_device(&card->card_dev);
+ mutex_unlock(&card->disconnect_mutex);
}
EXPORT_SYMBOL(snd_card_disconnect);
--
2.43.0