git: f0a59c9c6b40 - main - sound: Drain the dsp cdev before freeing resources

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

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

commit f0a59c9c6b40c7590890a113f16d8008d78fe89b
Author:     Christos Margiolis <christos@FreeBSD.org>
AuthorDate: 2026-11-18 10:17:49 +0000
Commit:     Christos Margiolis <christos@FreeBSD.org>
CommitDate: 2026-11-18 10:17:49 +0000

    sound: Drain the dsp cdev before freeing resources
    
    pcm_unregister() freed various resources before destroying the dsp cdev.
    A thread that had already passed DSP_REGISTERED() could potentially
    dereference those after they were freed. This was easily triggered by
    the test case introduced in this commit.
    
    Factor out pcm_killchans()'s channel wakeup/drain loop into a new
    pcm_shutdownchans(), which is called before destroy_dev(), so that the
    channels are freed only when destroy_dev() has returned.
    
    Mark all channels dead, not only the active ones. An operation started
    on an idle channel after pcm_shutdownchans() returned would never
    complete, and destroy_dev() would wait for the thread forever.
    
    Check PCM_REGISTERED() in vchan_create(), since it is the only place
    where channels are created at runtime.
    
    MFC after:      2 weeks
    Sponsored by:   The FreeBSD Foundation
    Reviewed by:    markj
    Differential Revision:  https://reviews.freebsd.org/D60120
---
 sys/dev/sound/pcm/sound.c    | 36 ++++++++++++++++++++--------
 sys/dev/sound/pcm/vchan.c    | 10 ++++++++
 tests/sys/sound/Makefile     |  1 +
 tests/sys/sound/hotunload.sh | 57 ++++++++++++++++++++++++++++++++++++++++++++
 4 files changed, 94 insertions(+), 10 deletions(-)

diff --git a/sys/dev/sound/pcm/sound.c b/sys/dev/sound/pcm/sound.c
index f0db8554c950..a1595dd448f4 100644
--- a/sys/dev/sound/pcm/sound.c
+++ b/sys/dev/sound/pcm/sound.c
@@ -148,25 +148,25 @@ pcm_addchan(device_t dev, int dir, kobj_class_t cls, void *devinfo)
 }
 
 static void
-pcm_killchans(struct snddev_info *d)
+pcm_shutdownchans(struct snddev_info *d)
 {
 	struct pcm_channel *ch;
 	bool again;
 
 	PCM_BUSYASSERT(d);
-	KASSERT(!PCM_REGISTERED(d), ("%s(): still registered\n", __func__));
+	KASSERT(!PCM_REGISTERED(d), ("%s(): still registered", __func__));
 
 	for (;;) {
 		again = false;
-		/* Make sure all channels are stopped. */
+		/* Make sure all channels are woken up and stopped. */
 		CHN_FOREACH(ch, d, channels.pcm) {
 			CHN_LOCK(ch);
+			chn_shutdown(ch);
 			if (ch->inprog == 0 && ch->sleeping == 0 &&
 			    CHN_STOPPED(ch)) {
 				CHN_UNLOCK(ch);
 				continue;
 			}
-			chn_shutdown(ch);
 			if (ch->direction == PCMDIR_PLAY)
 				chn_flush(ch);
 			else
@@ -179,12 +179,20 @@ pcm_killchans(struct snddev_info *d)
 		 * again.
 		 */
 		if (again)
-			pause_sbt("pcmkillchans", mstosbt(5), 0, 0);
+			pause_sbt("pcmshutdownchans", mstosbt(5), 0, 0);
 		else
 			break;
 	}
+}
+
+static void
+pcm_killchans(struct snddev_info *d)
+{
+	struct pcm_channel *ch;
+
+	PCM_BUSYASSERT(d);
+	KASSERT(!PCM_REGISTERED(d), ("%s(): still registered", __func__));
 
-	/* All channels are finally dead. */
 	while (!CHN_EMPTY(d, channels.pcm)) {
 		ch = CHN_FIRST(d, channels.pcm);
 		chn_kill(ch);
@@ -471,16 +479,25 @@ pcm_unregister(device_t dev)
 
 	PCM_LOCK(d);
 	PCM_WAIT(d);
-
 	d->flags &= ~SD_F_REGISTERED;
-
 	PCM_ACQUIRE(d);
 	PCM_UNLOCK(d);
 
-	pcm_killchans(d);
+	pcm_shutdownchans(d);
 
 	PCM_RELEASE_QUICK(d);
 
+	/*
+	 * Destroy the dsp cdev before freeing resources, otherwise threads
+	 * still inside cdev methods will eventually dereference freed
+	 * resources.
+	 */
+	dsp_destroy_dev(dev);
+
+	PCM_ACQUIRE_QUICK(d);
+	pcm_killchans(d);
+	PCM_RELEASE_QUICK(d);
+
 	if (d->play_sysctl_tree != NULL) {
 		sysctl_ctx_free(&d->play_sysctl_ctx);
 		d->play_sysctl_tree = NULL;
@@ -493,7 +510,6 @@ pcm_unregister(device_t dev)
 	sndstat_unregister(dev);
 	if (d->mixer != NULL)
 		mixer_uninit(dev);
-	dsp_destroy_dev(dev);
 
 	cv_destroy(&d->cv);
 	mtx_destroy(&d->lock);
diff --git a/sys/dev/sound/pcm/vchan.c b/sys/dev/sound/pcm/vchan.c
index 2cfe6b9d97fa..78528ab78e55 100644
--- a/sys/dev/sound/pcm/vchan.c
+++ b/sys/dev/sound/pcm/vchan.c
@@ -623,6 +623,16 @@ vchan_create(struct pcm_channel *parent, struct pcm_channel **child)
 	CHN_UNLOCK(parent);
 	PCM_LOCK(d);
 
+	/*
+	 * This is the only place where channels are created at runtime, so do
+	 * not create any if we are tearing down.
+	 */
+	if (!PCM_REGISTERED(d)) {
+		PCM_UNLOCK(d);
+		CHN_LOCK(parent);
+		return (ENODEV);
+	}
+
 	if (parent->direction == PCMDIR_PLAY) {
 		direction = PCMDIR_PLAY_VIRTUAL;
 		vchanfmt = d->pvchanformat;
diff --git a/tests/sys/sound/Makefile b/tests/sys/sound/Makefile
index f534a8cb17e5..a7f3252e3740 100644
--- a/tests/sys/sound/Makefile
+++ b/tests/sys/sound/Makefile
@@ -6,6 +6,7 @@ ATF_TESTS_C+=	mmap
 ATF_TESTS_C+=	pcm_read_write
 ATF_TESTS_C+=	polling
 ATF_TESTS_C+=	sndstat
+ATF_TESTS_SH+=	hotunload
 
 CFLAGS+=	-I${SRCTOP}/sys
 LDFLAGS+=	-lnv
diff --git a/tests/sys/sound/hotunload.sh b/tests/sys/sound/hotunload.sh
new file mode 100644
index 000000000000..9e1558f7f957
--- /dev/null
+++ b/tests/sys/sound/hotunload.sh
@@ -0,0 +1,57 @@
+# SPDX-License-Identifier: BSD-2-Clause
+#
+# Copyright (c) 2026 The FreeBSD Foundation
+#
+# This software was developed by Christos Margiolis <christos@FreeBSD.org>
+# under sponsorship from the FreeBSD Foundation.
+#
+# Redistribution and use in source and binary forms, with or without
+# modification, are permitted provided that the following conditions
+# are met:
+# 1. Redistributions of source code must retain the above copyright
+#    notice, this list of conditions and the following disclaimer.
+# 2. Redistributions in binary form must reproduce the above copyright
+#    notice, this list of conditions and the following disclaimer in the
+#    documentation and/or other materials provided with the distribution.
+#
+# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
+# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+# ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
+# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+# SUCH DAMAGE.
+
+atf_test_case hotunload cleanup
+hotunload_head()
+{
+	atf_set "descr" "Test hot-unload"
+	# Detaches the driver the other tests also use, so it is not safe
+	# to run in parallel.
+	atf_set "is.exclusive" "true"
+	atf_set "require.kmods" "snd_dummy"
+	atf_set "require.user" "root"
+}
+hotunload_body()
+{
+	local loops=512
+
+	for i in $(seq 1 ${loops}); do
+		cat /dev/random >/dev/dsp.dummy &
+		cat /dev/dsp.dummy >/dev/null &
+	done
+	atf_check kldunload -n snd_dummy
+}
+hotunload_cleanup()
+{
+	kldload -n snd_dummy
+}
+
+atf_init_test_cases()
+{
+	atf_add_test_case hotunload
+}