From: sashiko-bot@kernel.org
To: "Hanxiang Xu" <xuhx@hust.edu.cn>
Cc: linux-sound@vger.kernel.org
Subject: Re: [PATCH] ALSA: core: protect card->devices against concurrent disconnect and free
Date: Wed, 02 Dec 2026 03:23:28 +0000 [thread overview]
Message-ID: <sashiko-outbox-244771@kernel.org> (raw)
In-Reply-To: <20261202101200.1-1-xuhx@hust.edu.cn>
Thank you for your contribution! Sashiko AI review found 3 potential
issues to consider:
- [High] ALSA: core: self-deadlock on devices_mutex in
snd_device_register_all()
- [Medium] ALSA: core: use-after-free race in
snd_device_disconnect_all()
- [Low] ALSA: core: duplicate __snd_device_free() logic in
snd_device_free()
--
commit be22969e90c9ca2d2e7f9f8946b975be9cdbafcd
Author: Hanxiang Xu <xuhx@hust.edu.cn>
ALSA: core: protect card->devices against concurrent disconnect and free
This commit adds card->devices_mutex to serialize manipulations and walks of
the card->devices list against concurrent sub-driver removal and card
disconnection.
> diff --git a/sound/core/device.c b/sound/core/device.c
> index 5f2aa476d89b3..8aca99857582b 100644
> --- a/sound/core/device.c
> +++ b/sound/core/device.c
[ ... ]
> @@ -66,11 +67,9 @@ static void __snd_device_disconnect(struct snd_device *dev)
> }
> }
>
> +/* the node must already be unlinked from card->devices */
> static void __snd_device_free(struct snd_device *dev)
> {
> - /* unlink */
> - list_del(&dev->list);
> -
> __snd_device_disconnect(dev);
> if (dev->ops->dev_free) {
> if (dev->ops->dev_free(dev))
[ ... ]
> @@ -133,12 +133,23 @@ void snd_device_free(struct snd_card *card, void *device_data)
>
> if (snd_BUG_ON(!card || !device_data))
> return;
> + mutex_lock(&card->devices_mutex);
> dev = look_for_dev(card, device_data);
> - if (dev)
> - __snd_device_free(dev);
> - else
> + if (!dev) {
> + mutex_unlock(&card->devices_mutex);
> dev_dbg(card->dev, "device free %p (from %pS), not found\n",
> device_data, __builtin_return_address(0));
> + return;
> + }
> + list_del(&dev->list);
> + mutex_unlock(&card->devices_mutex);
> +
> + __snd_device_disconnect(dev);
> + if (dev->ops->dev_free) {
> + if (dev->ops->dev_free(dev))
> + dev_err(dev->card->dev, "device free failure\n");
> + }
> + kfree(dev);
> }
> EXPORT_SYMBOL(snd_device_free);
[Severity: Low]
This isn't a bug, but since __snd_device_free() now expects an already
unlinked node, snd_device_free() can still call __snd_device_free(dev)
after dropping card->devices_mutex instead of duplicating its body.
> @@ -174,6 +185,7 @@ int snd_device_register(struct snd_card *card, void *device_data)
>
> if (snd_BUG_ON(!card || !device_data))
> return -ENXIO;
> + guard(mutex)(&card->devices_mutex);
> dev = look_for_dev(card, device_data);
> if (dev)
> return __snd_device_register(dev);
> @@ -193,6 +205,7 @@ int snd_device_register_all(struct snd_card *card)
>
> if (snd_BUG_ON(!card))
> return -ENXIO;
> + guard(mutex)(&card->devices_mutex);
> list_for_each_entry(dev, &card->devices, list) {
> err = __snd_device_register(dev);
> if (err < 0)
[Severity: High]
Does holding card->devices_mutex across __snd_device_register() cause a
self-deadlock when a dev_register callback creates or registers a child
device on the same card?
snd_card_register()
snd_device_register_all()
mutex_lock(&card->devices_mutex)
__snd_device_register()
snd_pcm_dev_register()
snd_pcm_timer_init()
snd_timer_new()
snd_device_new()
mutex_lock(&card->devices_mutex) <-- deadlocks
With CONFIG_SND_PCM_TIMER enabled, registering any card with a PCM device
hits this path (as does snd_rawmidi_dev_register() -> snd_seq_device_new()),
hanging card probe and mutating card->devices while
snd_device_register_all() is iterating it.
> @@ -211,8 +224,23 @@ void snd_device_disconnect_all(struct snd_card *card)
>
> if (snd_BUG_ON(!card))
> return;
> - list_for_each_entry_reverse(dev, &card->devices, list)
> +
> + for (;;) {
> + struct snd_device *iter;
> +
> + dev = NULL;
> + mutex_lock(&card->devices_mutex);
> + list_for_each_entry_reverse(iter, &card->devices, list) {
> + if (iter->state == SNDRV_DEV_REGISTERED) {
> + dev = iter;
> + break;
> + }
> + }
> + mutex_unlock(&card->devices_mutex);
> + if (!dev)
> + return;
> __snd_device_disconnect(dev);
> + }
> }
[Severity: Medium]
Can dev be freed by a concurrent snd_device_free() after
snd_device_disconnect_all() drops card->devices_mutex?
CPU0 CPU1
snd_device_disconnect_all()
mutex_lock(&card->devices_mutex)
dev = iter
mutex_unlock(&card->devices_mutex)
snd_device_free()
mutex_lock(&card->devices_mutex)
list_del(&dev->list)
mutex_unlock(&card->devices_mutex)
__snd_device_disconnect(dev)
dev->ops->dev_free(dev)
kfree(dev)
__snd_device_disconnect(dev) <-- use-after-free
Unlike snd_device_free_all(), snd_device_disconnect_all() leaves dev on
card->devices with dev->state still set to SNDRV_DEV_REGISTERED when it
unlocks card->devices_mutex, so a concurrent sub-driver removal calling
snd_device_free() can still find, unlink, and free dev before
__snd_device_disconnect(dev) runs on CPU0.
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261202101200.1-1-xuhx@hust.edu.cn?part=1
prev parent reply other threads:[~2026-12-02 3:23 UTC|newest]
Thread overview: 2+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-12-02 2:12 [PATCH] ALSA: core: protect card->devices against concurrent disconnect and free Hanxiang Xu
2026-12-02 3:23 ` sashiko-bot [this message]
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=sashiko-outbox-244771@kernel.org \
--to=sashiko-bot@kernel.org \
--cc=linux-sound@vger.kernel.org \
--cc=sashiko-reviews@lists.linux.dev \
--cc=xuhx@hust.edu.cn \
/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 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.