From nobody Wed Nov 18 10:24:30 2026 X-Original-To: dev-commits-src-all@mlmmj.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mlmmj.nyi.freebsd.org (Postfix) with ESMTP id 4jTyQ65sm0z71JBS for ; Wed, 18 Nov 2026 10:24:30 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from mxrelay.nyi.freebsd.org (mxrelay.nyi.freebsd.org [IPv6:2610:1c1:1:606c::19:3]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange x25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by mx1.freebsd.org (Postfix) with ESMTPS id 4jTyQ644Z1z4fRX for ; Wed, 18 Nov 2026 10:24:30 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1794997470; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=03yFVTHvcR4HvELFhK22kIZsJ5U7QgDJKFMsn8FSUvk=; b=YlEbG8AuvetEXu+5OtUtMnGZYJXinbLboJtQflNuHR8hKcfCw/wk2S8j/ZvU4DzrnXnZ3r Xl0qDkq0A7qTNwZs25eyHY20LZYFQhZVnwbipYNYsNYRinOW1QoY0KGCg+UNOqiI/3O9/o yhY+0MAll8n83fG8STUI4RP/hzz/Lt1gB4mM+u4AaujtU13iaeP/DpvYp2hhliF46Larh+ ZGII8eVQouBhVOudA7JBI7+TE9FZmTeTnFfyUUnjjvsuxkD8gyW5/asntw6K8rOVzXjJf5 4Doex/zU62U3TtN0nEbqiOkNN7CmskXxODjzxvGV8mxsnfKU2rym5mRMC1MuAA== ARC-Seal: i=1; a=rsa-sha256; d=freebsd.org; s=dkim; cv=none; t=1794997470; b=EJ23Z4GTpt/yVUAyLMROc1qm/8YwB59c3jxlLRf/dwH6+FBh/pmqRPTezpL4hFWm395Pbv b+4utiC9GlAqjmZKv2x0eX/u4VA3+bmZgVBxF023RnzX7d0oYAjxWzMj7vNYYOBBK6fOoO XXEHuzV/+SWR4ZrfhQqSTTmkRmJxGcmsZbcR1gYM1Oh07kgBAUpRURo1LXTWNWGAJa57o3 nIXEXwEcBBugiP8V5JUYjJUSrEpRaTtpTe8ACM1OpAcqvrrmSyqn+LxI+8bh/3c7X8XZ/G 0iwB5t404BG/+vZ08iowpv7D2tw06FU70Sb/TDb0kKKI6CdNRiI6ODBDHVSF3w== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1794997470; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=03yFVTHvcR4HvELFhK22kIZsJ5U7QgDJKFMsn8FSUvk=; b=Yo7yQ0qJ34rK/4YatT7xCvL8I9ToCLV6jVvDf39yLGEAtE/QCdw/nJclXjdFDkN85JA8B3 gPDUAPbJB5Fmf9qmeM78AaHpMJhH/Fes+mgbpN/EdFhnM9QA7NXo0eCZDkn1g7d6pJl887 hT8AtMLon3yjhWDIF/KwvPLDCN9ZX3MVBr6aoJnUOZ3X5LHHj5bqxmm/dxbJplB3iSEaQo 1TM6m6X87gexXOdecBu+UuHAdbHyrrgauDhLohNP5bEFVUCy+NYCkA8WNegyT50ZVW0lsE CNpZ7jrTKlP1Duktd/NlfY/s4NHL03PJss8I1s6q62CJec4Gtfv0ZBjINDYlrA== ARC-Authentication-Results: i=1; mx1.freebsd.org; none Received: from gitrepo.freebsd.org (gitrepo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:5]) by mxrelay.nyi.freebsd.org (Postfix) with ESMTP id 4jTyQ63kTYz1PDf for ; Wed, 18 Nov 2026 10:24:30 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from git (uid 1279) (envelope-from git@FreeBSD.org) id 52a4 by gitrepo.freebsd.org (DragonFly Mail Agent v0.13+ on gitrepo.freebsd.org); Wed, 18 Nov 2026 10:24:30 +0000 To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-main@FreeBSD.org From: Christos Margiolis Subject: git: f0a59c9c6b40 - main - sound: Drain the dsp cdev before freeing resources List-Archive: https://lists.freebsd.org/archives/dev-commits-src-all X-BeenThere: dev-commits-src-all@freebsd.org List-Id: Commit messages for all branches of the src repository List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Git-Committer: christos X-Git-Repository: src X-Git-Refname: refs/heads/main X-Git-Reftype: branch X-Git-Commit: f0a59c9c6b40c7590890a113f16d8008d78fe89b Auto-Submitted: auto-generated Date: Wed, 18 Nov 2026 10:24:30 +0000 Message-Id: <6afd7cde.52a4.5bbcf5f8@gitrepo.freebsd.org> The branch main has been updated by christos: URL: https://cgit.FreeBSD.org/src/commit/?id=f0a59c9c6b40c7590890a113f16d8008d78fe89b commit f0a59c9c6b40c7590890a113f16d8008d78fe89b Author: Christos Margiolis AuthorDate: 2026-11-18 10:17:49 +0000 Commit: Christos Margiolis 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 +# 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 +}