git: 820403687783 - main - sound: Hold the topology lock across the remaining registration checks

From: Christos Margiolis <christos_at_FreeBSD.org>
Date: Wed, 18 Nov 2026 10:24:46 UTC
The branch main has been updated by christos:

URL: https://cgit.FreeBSD.org/src/commit/?id=820403687783a0abb2e88ca1862ad349057cd3ce

commit 820403687783a0abb2e88ca1862ad349057cd3ce
Author:     Christos Margiolis <christos@FreeBSD.org>
AuthorDate: 2026-11-18 10:23:34 +0000
Commit:     Christos Margiolis <christos@FreeBSD.org>
CommitDate: 2026-11-18 10:23:34 +0000

    sound: Hold the topology lock across the remaining registration checks
    
    sysctl_dev_pcm_bitperfect(), sysctl_dev_pcm_eq() and
    sysctl_dev_pcm_eq_preamp() check PCM_REGISTERED() and then busy the
    device with nothing held in between, so pcm_unregister() can run and
    destroy the sx before PCM_ACQUIRE() takes it. These sysctls are
    registered in the device's newbus context, so they outlive
    pcm_unregister().
    
    Hold the topology lock across the check and the busy, as the other
    handlers do.
    
    MFC after:      2 weeks
    Sponsored by:   The FreeBSD Foundation
    Differential Revision:  https://reviews.freebsd.org/D61224
---
 sys/dev/sound/pcm/feeder_eq.c | 24 ++++++++++++++++++++----
 sys/dev/sound/pcm/sound.c     | 12 ++++++++++--
 2 files changed, 30 insertions(+), 6 deletions(-)

diff --git a/sys/dev/sound/pcm/feeder_eq.c b/sys/dev/sound/pcm/feeder_eq.c
index f23758634f13..1731312c0a5d 100644
--- a/sys/dev/sound/pcm/feeder_eq.c
+++ b/sys/dev/sound/pcm/feeder_eq.c
@@ -442,10 +442,18 @@ sysctl_dev_pcm_eq(SYSCTL_HANDLER_ARGS)
 	int err, val, oval;
 
 	d = oidp->oid_arg1;
-	if (!PCM_REGISTERED(d))
+	bus_topo_lock();
+	if (!PCM_REGISTERED(d)) {
+		bus_topo_unlock();
 		return (ENODEV);
-
+	}
+	/*
+	 * Do not give up the topology lock until the device is busied, or it
+	 * might detach between unlocking and busying.
+	 */
 	PCM_ACQUIRE(d);
+	bus_topo_unlock();
+
 	if (d->flags & SD_F_EQ)
 		val = 1;
 	else
@@ -480,10 +488,18 @@ sysctl_dev_pcm_eq_preamp(SYSCTL_HANDLER_ARGS)
 	char buf[32];
 
 	d = oidp->oid_arg1;
-	if (!PCM_REGISTERED(d))
+	bus_topo_lock();
+	if (!PCM_REGISTERED(d)) {
+		bus_topo_unlock();
 		return (ENODEV);
-
+	}
+	/*
+	 * Do not give up the topology lock until the device is busied, or it
+	 * might detach between unlocking and busying.
+	 */
 	PCM_ACQUIRE(d);
+	bus_topo_unlock();
+
 	val = d->eqpreamp;
 	bzero(buf, sizeof(buf));
 	(void)snprintf(buf, sizeof(buf), "%c%d.%ddB",
diff --git a/sys/dev/sound/pcm/sound.c b/sys/dev/sound/pcm/sound.c
index 65b43a66d758..5f12d3a23795 100644
--- a/sys/dev/sound/pcm/sound.c
+++ b/sys/dev/sound/pcm/sound.c
@@ -234,10 +234,18 @@ sysctl_dev_pcm_bitperfect(SYSCTL_HANDLER_ARGS)
 	int err, val;
 
 	d = oidp->oid_arg1;
-	if (!PCM_REGISTERED(d))
+	bus_topo_lock();
+	if (!PCM_REGISTERED(d)) {
+		bus_topo_unlock();
 		return (ENODEV);
-
+	}
+	/*
+	 * Do not give up the topology lock until the device is busied, or it
+	 * might detach between unlocking and busying.
+	 */
 	PCM_ACQUIRE(d);
+	bus_topo_unlock();
+
 	val = (d->flags & SD_F_BITPERFECT) ? 1 : 0;
 
 	err = sysctl_handle_int(oidp, &val, 0, req);