* [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
2026-12-02 8:56 ` Takashi Iwai
0 siblings, 2 replies; 3+ 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] 3+ messages in thread* Re: [PATCH] ALSA: core: serialize card->devices operations with a re-entrant devices lock
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
2026-12-02 8:56 ` Takashi Iwai
1 sibling, 0 replies; 3+ messages in thread
From: sashiko-bot @ 2026-12-02 3:22 UTC (permalink / raw)
To: Hanxiang Xu; +Cc: linux-sound
Thank you for your contribution! Sashiko AI review found 3 potential
issues to consider:
- [High] ALSA: core: ABBA deadlock from holding devices_mutex across
device callbacks
- [High] ALSA: core: premature unlock in snd_device_register() before
callback
- [Low] ALSA: core: data race on card->devices_owner in
snd_devices_lock()
--
commit ae64ab97b56ec936442cfeab8bb6f01ecdcb2e0a
Author: Hanxiang Xu <xuhx@hust.edu.cn>
ALSA: core: serialize card->devices operations with a re-entrant devices lock
This commit adds a re-entrant card->devices_mutex to struct snd_card and
takes it across snd_device_*() operations to serialize access to the
card->devices list.
> diff --git a/sound/core/device.c b/sound/core/device.c
> index 5f2aa476d89b3..421e804b6d9f4 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);
> +}
[Severity: Low]
Does the unlocked read of card->devices_owner in snd_devices_lock() race
with the plain stores to card->devices_owner by the lock holder? Using
READ_ONCE() and WRITE_ONCE() on card->devices_owner would avoid a KCSAN
data race when two tasks call snd_devices_lock() and snd_devices_unlock()
concurrently.
[ ... ]
> @@ -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;
> }
[Severity: High]
Can dropping card->devices_mutex before __snd_device_register(dev) cause a
use-after-free on dev?
CPU0 CPU1
snd_device_register()
snd_devices_lock(card)
dev = look_for_dev(...)
snd_devices_unlock(card)
snd_device_free_all()
snd_devices_lock(card)
__snd_device_free(dev)
kfree(dev)
snd_devices_unlock(card)
__snd_device_register(dev)
if (dev->state == ...) <-- use-after-free
struct snd_device has no reference count, so once a top-level
snd_device_register() (such as from snd_emu8000_pcm_new() during sequencer
sub-driver probe) unlocks card->devices_mutex, a concurrent
snd_device_free() or snd_device_free_all() can free dev before
__snd_device_register() dereferences it.
Running __snd_device_register(dev) unlocked also leaves dev->state
unprotected against concurrent snd_device_disconnect_all() or
snd_device_register_all(), and inverts lock ordering between
card->devices_mutex and register_mutex in snd_pcm_dev_register() (which
calls snd_pcm_timer_init() -> snd_timer_new() -> snd_device_new() under
register_mutex, whereas snd_device_register_all() holds
card->devices_mutex before taking register_mutex).
[ ... ]
> @@ -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);
> }
[Severity: High]
Can holding card->devices_mutex across the device callbacks in
snd_device_disconnect_all(), snd_device_register_all(), and
snd_device_free_all() cause an ABBA deadlock against the driver-core
device lock?
CPU0 CPU1
---- ----
lock(&card->devices_mutex)
lock(&sdev->dev.mutex)
lock(&sdev->dev.mutex)
lock(&card->devices_mutex)
*** DEADLOCK ***
CPU0: snd_card_disconnect() -> snd_device_disconnect_all() ->
__snd_device_disconnect() -> snd_seq_device_dev_disconnect() ->
device_del()
CPU1: driver_detach() -> device_release_driver_internal() ->
snd_seq_bus_remove() -> snd_emu8000_remove() -> snd_device_free()
When an independently removable sequencer sub-driver (such as
emu8000_driver or snd_emu10k1_synth_driver) is unbound or unloaded on CPU1
while card disconnect runs on CPU0, the driver core already holds
sdev->dev.mutex before calling the sub-driver's remove callback, which
then calls snd_device_free() and waits for card->devices_mutex. Because
card->devices_owner is CPU0 rather than CPU1, the recursion check does not
trigger, and both tasks block on each other.
The same lock inversion occurs with snd_seq_device_dev_free() calling
cancel_work_sync(&autoload_work) under card->devices_mutex in
snd_device_free_all() while autoload_drivers() probes sequencer drivers
that acquire card->devices_mutex, and with snd_hda_codec_dev_free()
calling device_del(&codec->core.dev) under card->devices_mutex while
hda_codec_driver_probe() and hda_codec_driver_remove() take
card->devices_mutex under codec->core.dev.mutex and
codec->bus->core.codec_bind_lock.
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261202100602.1-1-xuhx@hust.edu.cn?part=1
^ permalink raw reply [flat|nested] 3+ messages in thread* Re: [PATCH] ALSA: core: serialize card->devices operations with a re-entrant devices lock
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
@ 2026-12-02 8:56 ` Takashi Iwai
1 sibling, 0 replies; 3+ messages in thread
From: Takashi Iwai @ 2026-12-02 8:56 UTC (permalink / raw)
To: Hanxiang Xu; +Cc: Takashi Iwai, Jaroslav Kysela, linux-sound, linux-kernel
On Wed, 02 Dec 2026 03:06:02 +0100,
Hanxiang Xu wrote:
>
> 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>
This change conflicts with your other patch?
Manage your own patches better at first.
Takashi
^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2026-12-02 8:56 UTC | newest]
Thread overview: 3+ 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
2026-12-02 8:56 ` Takashi Iwai
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.