Linux Sound subsystem development
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Hanxiang Xu" <xuhx@hust.edu.cn>
Cc: linux-sound@vger.kernel.org
Subject: Re: [PATCH] ALSA: core: serialize card->devices operations with a re-entrant devices lock
Date: Wed, 02 Dec 2026 03:22:54 +0000	[thread overview]
Message-ID: <sashiko-outbox-244766@kernel.org> (raw)
In-Reply-To: <20261202100602.1-1-xuhx@hust.edu.cn>

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

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

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
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 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-244766@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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox