From nobody Wed Nov 18 10:24:36 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 4jTyQF4cxxz71JH4 for ; Wed, 18 Nov 2026 10:24:37 +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 4jTyQD6nQrz4fdW for ; Wed, 18 Nov 2026 10:24:36 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1794997477; 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=u8CV/FMmhdqPOttsW2AcI2ohgkpLCtrmGUYa7nW5880=; b=oQfceg1PRRkPmrTcr6O/pZqVmlVRBTISpZGLuzg1soLMTLtWB5zcCtcKvtLoOANM3rCv3r a0MMK1+9vwJA8yaIIJVtz8DmAhceOnC+ZHxn4RYqium3LEqdMCbjTpEXmFfAa80RZeTnXn Jz0CJ+8rZ6m2INO3OTtuLOEwEsG2SS2iXeuibZ8xWu9bFDKrrja3egIIsr1weRjTWwvr0e qcWYN40Pka9tPJDLjn9IccnldWUezlTcf6FnlnL6slCP/IP5PfpGmzgHe94dxJdlsLNSOQ EOUhs226X49YK0sSH2mlmKCxDCx/ujk7DdHWe4QDUyKvcoYPi2F0ZQvDLbSReQ== ARC-Seal: i=1; a=rsa-sha256; d=freebsd.org; s=dkim; cv=none; t=1794997477; b=Cuf8odPJw0fsx84RBtHZwO0PeEqBTcrwngoGP7h2TkGFSsuudBZ4swFoCsDcD2uBzh3gwp 1nAuTnbPJMAYJws7vu3AB+xtkj3Oxw99GBRwYZ+GUN11ypbq23lPJ3X1yedLGvF0hOlrPf s1eKZt4J4Gd5CY6sitvfOTI3vpkRnQmDsuse4GknsJvyKi+GVvftEw5tYyaJfwGmzWo/UZ ofa1XHsfkX2YDZWRS7/9RDFwxi/MDS26frB5RucAGfJTGV0y5ze2g4piFRZkWQEToHjALI Iz6S2yY40gw2yraw89vrtVUSFB9j19onXHmygl3dh6MJLkgZE/IL66nVZJTjdw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1794997477; 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=u8CV/FMmhdqPOttsW2AcI2ohgkpLCtrmGUYa7nW5880=; b=PDECKSjZhft2Wh87Jf3P6/LKQiQ8K05wV8Nj2AWhtGOSyU5VbIhaNUuXu2NwfF0rvi7gdI ux3Mupm+4qD4QKkRlrYkl6gskxdches6iDaZeHZE/gJyNDUBZIrGZjtgv5bLwTcYoXFWTz fyVxotpqaH/XKtSl+JpmAzpWxeomXTnM/blDh5vCFv7T8M3n/sERGgv6sDrRXK8yx7d8E/ Z4/bzlAVTQZPa3FQvAZrG3Vt+maMwKhgWpkKI8Z9I6KsFcEPqBm19uKpwr4sQiVZa7xzYF chnnO34uTKTl6DYhlpA4fN+EUneO1/8+gHGLp+tli9NH0bqM/BMZi1LYNnwfMQ== 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 4jTyQD62Bbz1PDF for ; Wed, 18 Nov 2026 10:24:36 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from git (uid 1279) (envelope-from git@FreeBSD.org) id 58c9 by gitrepo.freebsd.org (DragonFly Mail Agent v0.13+ on gitrepo.freebsd.org); Wed, 18 Nov 2026 10:24:36 +0000 To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-main@FreeBSD.org From: Christos Margiolis Subject: git: 7c3106d35db0 - main - sound: Busy devices before giving up the topology lock 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: 7c3106d35db06605a86e7fc761a2d92462fb5b6e Auto-Submitted: auto-generated Date: Wed, 18 Nov 2026 10:24:36 +0000 Message-Id: <6afd7ce4.58c9.239fc975@gitrepo.freebsd.org> The branch main has been updated by christos: URL: https://cgit.FreeBSD.org/src/commit/?id=7c3106d35db06605a86e7fc761a2d92462fb5b6e commit 7c3106d35db06605a86e7fc761a2d92462fb5b6e Author: Christos Margiolis AuthorDate: 2026-11-18 10:18:50 +0000 Commit: Christos Margiolis 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: