From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=100.103.45.18 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796181809; b=JaWS6atXyte+Hx2pxSYvbtUvOE8WZh225oOp5qGgtzXCcwF3UaODh4jd1MiWiQiXXGTH spu3OGemutv2/jLHc6G53jijUCN3g0zV3tTZ2ri+oDSDEk74N4Ov3Y8hwBU9A5zrZxoI0 ho6pmkFGP2qsT1yoylI/Hlu+0HR1CUzRmpZ+Fd6Ns8d2mB25sC25+Gq0dJbvSJhkhzUgU hmE/KtkmzsuBBAYmgRwzhc2rhpKgTsN1CIRGFrx90yI7lWXS9ZJy9t4cw9HSaXAQpTdfK YBeOumXwiOtq0FtpiQJUAFEo0gRJk+PNb7soDDIyfIIWq+RSwCVGAU9OdAQePQDwVZQ== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796181809; h=Received:DKIM-Signature:Message-ID:From:Subject:Reply-To:To:Cc: In-Reply-To:References:Content-Type:Content-Transfer-Encoding:Date; bh=6hx3JD0AkL4S1UuSOIZhJCTf/rHhytWzNYqq4Z9i5+I=; b=v41Dk6rpXTqic36/AVVqvcVUtEVWfTeVQWl2pbq5Gou6CpvRTzeaTEEQfJefgmcBt+bQ lvNJ9yQmjPjkXOPNvDNgW9bnIrzMZZW8dSFpu+N5dvynvCu27L5rO1zY/ccIGpEBPCbR6 7xUxnfgPwEtxPa+vyFKnxY50hZ5zBpSkwM4qAVEK4U3ATKPs6KlSHWrgSmThW2N6SKbXI 6cQfUWa1Kd32X5rAtc9G3eK5geaAm3dxLvWd4v2qwPPdiZa8xtq0ekS8fr0m4apvGOGb+ rjpYN59Nq4oXBhQ8d6ArU6nEXbBknz2mumC6eBjjvTVT0AKi5raIUw/BOShRls00LTw== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b="AJeDEb/0"; arc=none smtp.remote-ip=100.103.45.18 Received: from smtp.kernel.org (quasi.space.kernel.org [100.103.45.18]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 426CA606A337 for ; Wed, 2 Dec 2026 03:23:28 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=AJeDEb/0 Received: by smtp.kernel.org (Postfix) with ESMTPSA id B2FCD1F000C1; Wed, 2 Dec 2026 03:23:28 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1796181808; bh=6hx3JD0AkL4S1UuSOIZhJCTf/rHhytWzNYqq4Z9i5+I=; h=From:Subject:Reply-To:To:Cc:In-Reply-To:References:Date; b=AJeDEb/04HENvJAuhysw60rHjJ9mUq/uNu7I2A2ppnFV2Tov9He5XZuc7UQLVboFc DD3MBimVaca4Dfh9CA8hdoGlb4lxKONEgQB6u1D2hq0Kbl+CKpm0gUhZ5xVDb2KGCK 1wn6hqQdhj1fohJbf0qmCW/tlD9anz9+UXKfuKVjnBcybBlOIk1Qs7E7Bch9ur0ZrQ A3rk6ZZfRSytebKrA/Vglxu6h6FtjIgUzjMfD9FU0brjqvE2gT0IxTDh3qlHhv5BZM sN2nNIzpJLkrtOTOdI4IwcMPf5Z9cpmOReE1vzbH4M+VI5WUs7PRNG4hCsLmNZtRz8 jIv6D/OniaGwA== Message-ID: From: sashiko-bot@kernel.org Subject: Re: [PATCH] ALSA: core: protect card->devices against concurrent disconnect and free Reply-To: sashiko-reviews@lists.linux.dev To: "Hanxiang Xu" Cc: linux-sound@vger.kernel.org In-Reply-To: <20261202101200.1-1-xuhx@hust.edu.cn> References: <20261202101200.1-1-xuhx@hust.edu.cn> Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable Date: Wed, 02 Dec 2026 03:23:28 +0000 X-Mailing-List: linux-sound@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list 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 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) > } > } > =20 > +/* 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 *d= evice_data) > =09 > if (snd_BUG_ON(!card || !device_data)) > return; > + mutex_lock(&card->devices_mutex); > dev =3D 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) > =20 > if (snd_BUG_ON(!card || !device_data)) > return -ENXIO; > + guard(mutex)(&card->devices_mutex); > dev =3D 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) > =09 > if (snd_BUG_ON(!card)) > return -ENXIO; > + guard(mutex)(&card->devices_mutex); > list_for_each_entry(dev, &card->devices, list) { > err =3D __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) > =20 > if (snd_BUG_ON(!card)) > return; > - list_for_each_entry_reverse(dev, &card->devices, list) > + > + for (;;) { > + struct snd_device *iter; > + > + dev =3D NULL; > + mutex_lock(&card->devices_mutex); > + list_for_each_entry_reverse(iter, &card->devices, list) { > + if (iter->state =3D=3D SNDRV_DEV_REGISTERED) { > + dev =3D 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 =3D 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. --=20 Sashiko AI review =C2=B7 https://sashiko.dev/#/patchset/20261202101200.1-1-= xuhx@hust.edu.cn?part=3D1