git: 7c3106d35db0 - main - sound: Busy devices before giving up the topology lock

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

URL: https://cgit.FreeBSD.org/src/commit/?id=7c3106d35db06605a86e7fc761a2d92462fb5b6e

commit 7c3106d35db06605a86e7fc761a2d92462fb5b6e
Author:     Christos Margiolis <christos@FreeBSD.org>
AuthorDate: 2026-11-18 10:18:50 +0000
Commit:     Christos Margiolis <christos@FreeBSD.org>
CommitDate: 2026-11-18 10:18:50 +0000

    sound: Busy devices before giving up the topology lock
    
    The SNDCTL_*INFO ioctls can report info about any device in the system,
    not only the one they were issued on. Both the dsp and mixer dispatchers
    unnecessarily busy the caller's device first. The caller's device is
    kept alive anyway, until destroy_dev() drains the cdev threadcount.
    
    Busying the wrong device also prevents the handlers from busying the one
    they actually want to report info on, because this would recurse on the
    sx whenever the two were the same, so they walk the channel lists under
    PCM_LOCK instead.
    
    Busy each device as it is being inspected, and give up the topology lock
    once it is busied rather than holding it across the walks.
    
    The vchan sysctls have the same problem. They look the device up under
    the topology lock, drop it, and then busy the device, leaving a window
    where the device can disappear. Busy it before dropping the lock there
    too.
    
    MFC after:      2 weeks
    Sponsored by:   The FreeBSD Foundation
    Differential Revision:  https://reviews.freebsd.org/D60507
---
 sys/dev/sound/pcm/dsp.c   | 65 +++++++++++++++++++++++------------------------
 sys/dev/sound/pcm/mixer.c | 12 +++++++++
 sys/dev/sound/pcm/sound.c | 29 ++++++++++++---------
 sys/dev/sound/pcm/vchan.c | 16 ++++++------
 4 files changed, 69 insertions(+), 53 deletions(-)

diff --git a/sys/dev/sound/pcm/dsp.c b/sys/dev/sound/pcm/dsp.c
index 216bd3c68cae..4dfb66075a9b 100644
--- a/sys/dev/sound/pcm/dsp.c
+++ b/sys/dev/sound/pcm/dsp.c
@@ -739,7 +739,6 @@ dsp_ioctl(struct cdev *i_dev, unsigned long cmd, caddr_t arg, int mode,
 	 * and MIDI).  Handle those special cases here.
 	 */
 	if (IOCGROUP(cmd) == 'X') {
-		PCM_ACQUIRE(d);
 		switch(cmd) {
 		case SNDCTL_SYSINFO:
 			sound_oss_sysinfo((oss_sysinfo *)arg);
@@ -764,7 +763,6 @@ dsp_ioctl(struct cdev *i_dev, unsigned long cmd, caddr_t arg, int mode,
 		default:
 			ret = EINVAL;
 		}
-		PCM_RELEASE(d);
 		PCM_GIANT_EXIT(d);
 		return (ret);
 	}
@@ -2117,27 +2115,24 @@ dsp_oss_audioinfo(struct cdev *i_dev, oss_audioinfo *ai, bool ex)
 			}
 		}
 
-		PCM_UNLOCKASSERT(d);
-		PCM_LOCK(d);
 		if ((ai->dev == -1 && d->dsp_dev == i_dev) ||
-		    (ai->dev == unit)) {
-			PCM_UNLOCK(d);
+		    (ai->dev == unit))
 			break;
-		} else {
-			PCM_UNLOCK(d);
-			d = NULL;
-		}
+		d = NULL;
 	}
-	bus_topo_unlock();
 
-	/* Exhausted the search -- nothing is locked, so return. */
-	if (d == NULL)
+	/* Exhausted the search. */
+	if (d == NULL) {
+		bus_topo_unlock();
 		return (EINVAL);
+	}
 
-	/* XXX Need Giant magic entry ??? */
-
-	PCM_UNLOCKASSERT(d);
-	PCM_LOCK(d);
+	/*
+	 * 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();
 
 	bzero((void *)ai, sizeof(oss_audioinfo));
 	ai->dev = unit;
@@ -2223,7 +2218,7 @@ dsp_oss_audioinfo(struct cdev *i_dev, oss_audioinfo *ai, bool ex)
 	if (ai->min_channels == INT_MAX)
 		ai->min_channels = 0;
 
-	PCM_UNLOCK(d);
+	PCM_RELEASE(d);
 
 	return (0);
 }
@@ -2285,18 +2280,24 @@ dsp_oss_engineinfo(struct cdev *i_dev, oss_audioinfo *ai)
 	 * Search for the requested audio device (channel).  Start by
 	 * iterating over pcm devices.
 	 */
-	bus_topo_lock();
-	for (unit = 0; pcm_devclass != NULL &&
-	    unit < devclass_get_maxunit(pcm_devclass); unit++) {
+	for (unit = 0; ; unit++) {
+		bus_topo_lock();
+		if (pcm_devclass == NULL ||
+		    unit >= devclass_get_maxunit(pcm_devclass)) {
+			bus_topo_unlock();
+			break;
+		}
 		d = devclass_get_softc(pcm_devclass, unit);
-		if (!PCM_REGISTERED(d))
+		if (!PCM_REGISTERED(d)) {
+			bus_topo_unlock();
 			continue;
-
-		/* XXX Need Giant magic entry ??? */
-
-		/* See the note in function docblock */
-		PCM_UNLOCKASSERT(d);
-		PCM_LOCK(d);
+		}
+		/*
+		 * 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();
 
 		CHN_FOREACH(ch, d, channels.pcm) {
 			CHN_UNLOCKASSERT(ch);
@@ -2310,7 +2311,7 @@ dsp_oss_engineinfo(struct cdev *i_dev, oss_audioinfo *ai)
 		}
 
 		if (ch == NULL) {
-			PCM_UNLOCK(d);
+			PCM_RELEASE(d);
 			continue;
 		}
 
@@ -2434,14 +2435,12 @@ dsp_oss_engineinfo(struct cdev *i_dev, oss_audioinfo *ai)
 		ai->next_rec_engine = 0;
 
 		CHN_UNLOCK(ch);
-		PCM_UNLOCK(d);
-		bus_topo_unlock();
+		PCM_RELEASE(d);
 
 		return (0);
 	}
-	bus_topo_unlock();
 
-	/* Exhausted the search -- nothing is locked, so return. */
+	/* Exhausted the search. */
 	return (EINVAL);
 }
 
diff --git a/sys/dev/sound/pcm/mixer.c b/sys/dev/sound/pcm/mixer.c
index aebbf87e53ba..47411870ed75 100644
--- a/sys/dev/sound/pcm/mixer.c
+++ b/sys/dev/sound/pcm/mixer.c
@@ -887,6 +887,18 @@ mixer_ioctl(struct cdev *i_dev, unsigned long cmd, caddr_t arg, int mode,
 		return (EBADF);
 
 	PCM_GIANT_ENTER(d);
+
+	/*
+	 * This group of ioctls can report info about any device in the system,
+	 * and they busy each one as they inspect it, so they must not run with
+	 * this device already busied, otherwise we will recurse on the sx.
+	 */
+	if (IOCGROUP(cmd) == 'X') {
+		ret = mixer_ioctl_cmd(i_dev, cmd, arg, mode, td);
+		PCM_GIANT_EXIT(d);
+		return (ret);
+	}
+
 	PCM_ACQUIRE(d);
 
 	ret = mixer_ioctl_cmd(i_dev, cmd, arg, mode, td);
diff --git a/sys/dev/sound/pcm/sound.c b/sys/dev/sound/pcm/sound.c
index 7e44deab1abf..3438b03d1ef1 100644
--- a/sys/dev/sound/pcm/sound.c
+++ b/sys/dev/sound/pcm/sound.c
@@ -510,18 +510,24 @@ sound_oss_sysinfo(oss_sysinfo *si)
 
 	j = 0;
 
-	bus_topo_lock();
-	for (i = 0; pcm_devclass != NULL &&
-	    i < devclass_get_maxunit(pcm_devclass); i++) {
+	for (i = 0; ; i++) {
+		bus_topo_lock();
+		if (pcm_devclass == NULL ||
+		    i >= devclass_get_maxunit(pcm_devclass)) {
+			bus_topo_unlock();
+			break;
+		}
 		d = devclass_get_softc(pcm_devclass, i);
-		if (!PCM_REGISTERED(d))
+		if (!PCM_REGISTERED(d)) {
+			bus_topo_unlock();
 			continue;
-
-		/* XXX Need Giant magic entry ??? */
-
-		/* See note in function's docblock */
-		PCM_UNLOCKASSERT(d);
-		PCM_LOCK(d);
+		}
+		/*
+		 * 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();
 
 		si->numaudioengines += PCM_CHANCOUNT(d);
 
@@ -535,9 +541,8 @@ sound_oss_sysinfo(oss_sysinfo *si)
 			j++;
 		}
 
-		PCM_UNLOCK(d);
+		PCM_RELEASE(d);
 	}
-	bus_topo_unlock();
 
 	si->numsynths = 0;	/* OSSv4 docs:  this field is obsolete */
 	/**
diff --git a/sys/dev/sound/pcm/vchan.c b/sys/dev/sound/pcm/vchan.c
index 47f32001fb01..31fedac458a8 100644
--- a/sys/dev/sound/pcm/vchan.c
+++ b/sys/dev/sound/pcm/vchan.c
@@ -258,9 +258,12 @@ sysctl_dev_pcm_vchans(SYSCTL_HANDLER_ARGS)
 		bus_topo_unlock();
 		return (EINVAL);
 	}
-	bus_topo_unlock();
-
+	/*
+	 * 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();
 
 	switch (VCHAN_SYSCTL_DIR(oidp->oid_arg1)) {
 	case VCHAN_PLAY:
@@ -316,9 +319,8 @@ sysctl_dev_pcm_vchanmode(SYSCTL_HANDLER_ARGS)
 		bus_topo_unlock();
 		return (EINVAL);
 	}
-	bus_topo_unlock();
-
 	PCM_ACQUIRE(d);
+	bus_topo_unlock();
 
 	switch (VCHAN_SYSCTL_DIR(oidp->oid_arg1)) {
 	case VCHAN_PLAY:
@@ -406,9 +408,8 @@ sysctl_dev_pcm_vchanrate(SYSCTL_HANDLER_ARGS)
 		bus_topo_unlock();
 		return (EINVAL);
 	}
-	bus_topo_unlock();
-
 	PCM_ACQUIRE(d);
+	bus_topo_unlock();
 
 	switch (VCHAN_SYSCTL_DIR(oidp->oid_arg1)) {
 	case VCHAN_PLAY:
@@ -498,9 +499,8 @@ sysctl_dev_pcm_vchanformat(SYSCTL_HANDLER_ARGS)
 		bus_topo_unlock();
 		return (EINVAL);
 	}
-	bus_topo_unlock();
-
 	PCM_ACQUIRE(d);
+	bus_topo_unlock();
 
 	switch (VCHAN_SYSCTL_DIR(oidp->oid_arg1)) {
 	case VCHAN_PLAY: