From nobody Thu Apr 11 18:07:05 2024 X-Original-To: dev-commits-src-main@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 4VFnhp0mvZz5HqRd; Thu, 11 Apr 2024 18:07:06 +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 client-signature RSA-PSS (4096 bits) client-digest SHA256) (Client CN "mxrelay.nyi.freebsd.org", Issuer "R3" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 4VFnhn5qkdz4c7V; Thu, 11 Apr 2024 18:07:05 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1712858825; 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=VYfT+khZbM7Z1/oPrq7N8mCpY/gN+koV9QkI36Z/3Jc=; b=pXKRjNCW9bAzldGaPQMPV1XjUe5mDvM6995qmAlR9BnT7b7lvuoVJYFhWcOf/+kbAnEC7p Q5NXxNIx2zK4u0D1W3A5hSLVy1/c2gk8Acb3kEJXwPydmzuHgc+cpzJSi6Fcojd3nAlt4c 6s5OAGy+uyj0YymdtohQBtUwbkKd/DshlzsDHFeAMZxhT+V1m1yhKBAD7+4Jb8KHyeKUmU +mp4hcuTQX/7RSbndM7HPTz9LYAmmBF4F9f7Xcx8bxEQfkWZ9byWX3zflP5wNy/g2tdkll CsGThU3VWK7BLsQbfsgBC/HydLqI7I5A+01GJHRD1qA65i9Ropa1infb33iN1A== ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1712858825; a=rsa-sha256; cv=none; b=RVFjO3GWjrwJnbDNaIdL7aFSbW93OVQQPM2EmFujEHpyIR0l6bzdeWN9lIiyihp/6g4ZKB WTvQyxsBCZCwG6l3DZxEtgyRtHLLPiizVk99meS920Tx/mGFGFQgR2MUT/M35x5e3+Y1/3 gaWQNe/WF0H61CTqq/y/sMHzDHhSFM7UNs/eVt/YrY+KCn0CvCaQ5DPLRRKNPnrZf64c8Z gs5Iqss8YG23wIWDZjM9d4CBNdU/CGxr6COsIi/YCInyC8km5VhKkDpcugkTNT5E1E4HUC vUeg2oHRmYPbUyGm4rzsCYmbGyP8kc3qGv/Td4fTEH3JV//SiLi/sByaZVD0dw== ARC-Authentication-Results: i=1; mx1.freebsd.org; none ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1712858825; 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=VYfT+khZbM7Z1/oPrq7N8mCpY/gN+koV9QkI36Z/3Jc=; b=Kyjdu2nwjTLZKmuOVS7AGEzI8OrYlTVidDZ0WQnXxP0kkZQSJkF51XiEsmPFhRPK3K+GcG 8pJ5RHO6/+y0+R9YrmdtJIRKDWNNh/gXOE8hIAYfvLNW1/3h5nO+5QCWaA7VgRhA1TQRPo o7qt8LHkgvEsQsdrg2d0XEbE06tIQIpB1cUp+giujGE47s73q/UI5GJvXOjDDD1PvTntK2 LXpcLlwhpnXuQxifnZZjkHXTxnBCq/tJ7Dfb0BEWE6SGz5mjmX8XRylO6nQA2BDa9+wuap 5bKxbjU5BO2v8z4v34vjWjz5Pr2KK/FUTmYZrK58zW/TrzIwKT9UFnKzERHD4w== Received: from gitrepo.freebsd.org (gitrepo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:5]) (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) (Client did not present a certificate) by mxrelay.nyi.freebsd.org (Postfix) with ESMTPS id 4VFnhn58QCzGZV; Thu, 11 Apr 2024 18:07:05 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from gitrepo.freebsd.org ([127.0.1.44]) by gitrepo.freebsd.org (8.17.1/8.17.1) with ESMTP id 43BI75Ad022853; Thu, 11 Apr 2024 18:07:05 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.17.1/8.17.1/Submit) id 43BI759M022850; Thu, 11 Apr 2024 18:07:05 GMT (envelope-from git) Date: Thu, 11 Apr 2024 18:07:05 GMT Message-Id: <202404111807.43BI759M022850@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-main@FreeBSD.org From: Christos Margiolis Subject: git: 44e128fe9d92 - main - sound: Implement asynchronous device detach List-Id: Commit messages for the main branch of the src repository List-Archive: https://lists.freebsd.org/archives/dev-commits-src-main List-Help: List-Post: List-Subscribe: List-Unsubscribe: Sender: dev-commits-src-main+owner@freebsd.org X-BeenThere: dev-commits-src-main@freebsd.org 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: 44e128fe9d92c1a544b801cb56e907a66ef34691 Auto-Submitted: auto-generated The branch main has been updated by christos: URL: https://cgit.FreeBSD.org/src/commit/?id=44e128fe9d92c1a544b801cb56e907a66ef34691 commit 44e128fe9d92c1a544b801cb56e907a66ef34691 Author: Christos Margiolis AuthorDate: 2024-04-11 18:06:50 +0000 Commit: Christos Margiolis CommitDate: 2024-04-11 18:06:50 +0000 sound: Implement asynchronous device detach Hot-unplugging a sound device, such as a USB sound card, whilst being consumed by an application, results in an infinite loop until either the application closes the device's file descriptor, or the channel automatically times out after hw.snd.timeout seconds. In the case of a detach however, the timeout approach is still not ideal, since we want all resources to be released immediatelly, without waiting for N seconds until we can use the bus again. The timeout mechanism works by calling chn_sleep() in chn_read() and chn_write() (see pcm/channel.c) in order to send the thread to sleep, using cv_timedwait_sig(). Since chn_sleep() sets the CHN_F_SLEEPING flag while waiting for cv_timedwait_sig() to return, we can test this flag in pcm_unregister() (called during detach) and wakeup the sleeping thread(s) to immediately kill the channel(s) being consumed. Sponsored by: The FreeBSD Foundation MFC after: 2 months PR: 194727 Reviewed by: dev_submerge.ch, bapt, markj Differential Revision: https://reviews.freebsd.org/D43545 --- share/man/man4/snd_uaudio.4 | 11 +---------- sys/dev/sound/pcm/dsp.c | 2 +- sys/dev/sound/pcm/mixer.c | 11 ----------- sys/dev/sound/pcm/sound.c | 24 ++++++++++-------------- sys/dev/sound/usb/uaudio.c | 13 +++---------- 5 files changed, 15 insertions(+), 46 deletions(-) diff --git a/share/man/man4/snd_uaudio.4 b/share/man/man4/snd_uaudio.4 index b6a6c06a2312..6e2509b8f2ac 100644 --- a/share/man/man4/snd_uaudio.4 +++ b/share/man/man4/snd_uaudio.4 @@ -27,7 +27,7 @@ .\" ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE .\" POSSIBILITY OF SUCH DAMAGE. .\" -.Dd January 29, 2024 +.Dd March 26, 2024 .Dt SND_UAUDIO 4 .Os .Sh NAME @@ -156,15 +156,6 @@ and modified for by .An Hiten Pandya Aq Mt hmp@FreeBSD.org . .Sh BUGS -The PCM framework in -.Fx -only supports synchronous device detach. -That means all mixer and DSP character devices belonging to a given -USB audio device must be closed when receiving an error on a DSP read, -a DSP write or a DSP IOCTL request. -Else the USB audio driver will wait for this to happen, preventing -enumeration of new devices on the parenting USB controller. -.Pp Some USB audio devices might refuse to work properly unless the sample rate is configured the same for both recording and playback, even if only simplex is used. diff --git a/sys/dev/sound/pcm/dsp.c b/sys/dev/sound/pcm/dsp.c index 003e57446a69..4112e87ba527 100644 --- a/sys/dev/sound/pcm/dsp.c +++ b/sys/dev/sound/pcm/dsp.c @@ -274,7 +274,7 @@ dsp_close(void *data) d = priv->sc; /* At this point pcm_unregister() will destroy all channels anyway. */ - if (!PCM_REGISTERED(d)) + if (PCM_DETACHING(d)) goto skip; PCM_GIANT_ENTER(d); diff --git a/sys/dev/sound/pcm/mixer.c b/sys/dev/sound/pcm/mixer.c index ee1ed11a8ed0..cc8cf5b1ceea 100644 --- a/sys/dev/sound/pcm/mixer.c +++ b/sys/dev/sound/pcm/mixer.c @@ -817,17 +817,6 @@ mixer_uninit(device_t dev) KASSERT(m->type == MIXER_TYPE_PRIMARY, ("%s(): illegal mixer type=%d", __func__, m->type)); - snd_mtxlock(m->lock); - - if (m->busy) { - snd_mtxunlock(m->lock); - return EBUSY; - } - - /* destroy dev can sleep --hps */ - - snd_mtxunlock(m->lock); - pdev->si_drv1 = NULL; destroy_dev(pdev); diff --git a/sys/dev/sound/pcm/sound.c b/sys/dev/sound/pcm/sound.c index 53cde49c4905..1517126210c4 100644 --- a/sys/dev/sound/pcm/sound.c +++ b/sys/dev/sound/pcm/sound.c @@ -1001,26 +1001,22 @@ pcm_unregister(device_t dev) CHN_FOREACH(ch, d, channels.pcm) { CHN_LOCK(ch); - if (ch->refcount > 0) { - device_printf(dev, - "unregister: channel %s busy (pid %d)\n", - ch->name, ch->pid); - CHN_UNLOCK(ch); - PCM_RELEASE_QUICK(d); - return (EBUSY); + if (ch->flags & CHN_F_SLEEPING) { + /* + * We are detaching, so do not wait for the timeout in + * chn_read()/chn_write(). Wake up the thread and kill + * the channel immediately. + */ + CHN_BROADCAST(&ch->intr_cv); + ch->flags |= CHN_F_DEAD; } + chn_abort(ch); CHN_UNLOCK(ch); } dsp_destroy_dev(dev); - if (mixer_uninit(dev) == EBUSY) { - device_printf(dev, "unregister: mixer busy\n"); - PCM_LOCK(d); - PCM_RELEASE(d); - PCM_UNLOCK(d); - return (EBUSY); - } + (void)mixer_uninit(dev); /* remove /dev/sndstat entry first */ sndstat_unregister(dev); diff --git a/sys/dev/sound/usb/uaudio.c b/sys/dev/sound/usb/uaudio.c index 5d7396c527e0..2351c2522021 100644 --- a/sys/dev/sound/usb/uaudio.c +++ b/sys/dev/sound/usb/uaudio.c @@ -1255,20 +1255,13 @@ uaudio_detach_sub(device_t dev) unsigned i = uaudio_get_child_index_by_dev(sc, dev); int error = 0; -repeat: if (sc->sc_child[i].pcm_registered) { error = pcm_unregister(dev); - } else { - if (sc->sc_child[i].mixer_init) - error = mixer_uninit(dev); + } else if (sc->sc_child[i].mixer_init) { + error = mixer_uninit(dev); } - if (error) { - device_printf(dev, "Waiting for sound application to exit!\n"); - usb_pause_mtx(NULL, 2 * hz); - goto repeat; /* try again */ - } - return (0); /* success */ + return (error); } static int