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=1796181775; b=3qmg79gJCA7AQnxSFWCgCQATLu3sZNWdsI1zXZFD06vwGX6voPR27hqmxJh2Jdoxi640 y3tPBl8djr+inyqBWq6rM5UEenFHZrnp53ejp5Pb6JhXesf6+wxrbWCc3+8awWnJfpL9z h5i1KowM/U0/duYOoam5lesk0wg+q/4KcNxWuyFrBh3IYaxnNZzPgSFeKZ85+atcuQ6Lf WxC+cZcrSml/UJCCDU1fS/QyF3QI7z5JDf8Q+BoBIRb8x/bikIVWxJoQ/KkkfKCpPKDLL GJAfnXo04MdzYrfZamWCDBl/v+/Wh/cedCd870uKg58EGmd2h/BLzsjVY6+CRhu4unQ== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796181775; h=Received:DKIM-Signature:Message-ID:From:Subject:Reply-To:To:Cc: In-Reply-To:References:Content-Type:Content-Transfer-Encoding:Date; bh=OLLEMdMue9baVhgb4COOBFHtGN3y1qSmQ7fHeDqitwQ=; b=yPbMzdhIlDUg8AzhRE5gDMa4Jcasg/BfM+QGo4NeWby8innWe2GVhGfbQ0OKQt0f3ncN rpJJea++zZNOLoukfjIsJtvkfdVZaKu8SADX6eGfncTbgMhWB9lPqJsF28JWdlVoVl/Du 1ygWXU6nGujmGwGUx3m3yYUG377Zto4GMRrC6/xctF1H3Q9fZfh5GVwdLZePlbNlUuQTw 03xlWNIoloFmXhvktoRbiropMDYSLRlKVPIvTouIXF2j0bZhvj3B3wys0PNhgRwMxg2tr Q6/+5N8TWJ0PVxNOc1SYyF47w92+SCAt+oKiovDFqGsEZuCqT7A+nzQ8o81XTW49x4w== 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=eICqy6Gt; 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 1718E606A32D for ; Wed, 2 Dec 2026 03:22:54 +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=eICqy6Gt Received: by smtp.kernel.org (Postfix) with ESMTPSA id A58161F000C1; Wed, 2 Dec 2026 03:22:54 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1796181774; bh=OLLEMdMue9baVhgb4COOBFHtGN3y1qSmQ7fHeDqitwQ=; h=From:Subject:Reply-To:To:Cc:In-Reply-To:References:Date; b=eICqy6Gtn7w/FWwNKkciOG+9cfZ12EwqqFCVACIkuBRC1W9QO+GXa23V/HAOdhPUc PGGPa7yHitDVi2fJZqP4O/EzPayP/hJYUT3oE8DUlwGeSZIAh5KsWl8ZilOmtIu4D6 c0GcY9JHyRNw1nli5neadBsmYKTFlz0RnLjCRcozjpBG3Di77r3iFLqZpOYoVdV6aW XGN1lSBCdKyzcjW0xZuFaTwd7bYu+paKPpLki/DvdPeK0pMoPjS/dHnX9Dfj8lCr6n rSYRDUxgG0OJYoO42AdZRCB4QJ9qwQ86F8RyKCetkpZNk8RiC9Facr46EaCjBUQBux lwh65Jf4nzeEg== Message-ID: From: sashiko-bot@kernel.org Subject: Re: [PATCH] ALSA: core: serialize card->devices operations with a re-entrant devices lock Reply-To: sashiko-reviews@lists.linux.dev To: "Hanxiang Xu" Cc: linux-sound@vger.kernel.org In-Reply-To: <20261202100602.1-1-xuhx@hust.edu.cn> References: <20261202100602.1-1-xuhx@hust.edu.cn> Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable Date: Wed, 02 Dec 2026 03:22:54 +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: 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 ALSA: core: serialize card->devices operations with a re-entrant devices lo= ck 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 > #include > =20 > +static void snd_devices_lock(struct snd_card *card) > +{ > + if (card->devices_owner =3D=3D current) { > + card->devices_recursion++; > + return; > + } > + mutex_lock(&card->devices_mutex); > + card->devices_owner =3D current; > + card->devices_recursion =3D 1; > +} > + > +static void snd_devices_unlock(struct snd_card *card) > +{ > + if (--card->devices_recursion) > + return; > + card->devices_owner =3D 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) > =20 > if (snd_BUG_ON(!card || !device_data)) > return -ENXIO; > + snd_devices_lock(card); > dev =3D 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 =3D 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 =3D=3D ...) <-- 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) > =20 > 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. --=20 Sashiko AI review =C2=B7 https://sashiko.dev/#/patchset/20261202100602.1-1-= xuhx@hust.edu.cn?part=3D1