From nobody Sun Mar 31 14:16:48 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 4V6x6836ksz5G7fx; Sun, 31 Mar 2024 14:16:48 +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 4V6x682WPvz4ZVK; Sun, 31 Mar 2024 14:16:48 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1711894608; 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=0aeEtaX3nM0QJyMFbYezkrvIeHPRfX2CU9yP06nLGwk=; b=IIWdLI/N4BjHaMKMmj5pWeOlDDkugVIuuqb5QnAt3dQ7HHVfoZ2nTsT9SVGH0AzSbxydyk LyjaTYgfKu65ReJNeQy9xREz1WGPC+s8jqz9SSBuduNE0E5Whis5S7n4nyERdug/P74dCU L/pbfW5Ke6nYxq/hfcf5cYM0LRUAez9bQJtkmmiMvPT0snMjcDV6Y7B70e+H5LlcYmjhA8 /mxC6/xF12qs9gviBOCRkCkfQFjiYB0bkHJgHp9LwuipSmyTC1U2xgaWT3UW2P6bDxFuWb HlGpjJnKYgf43ToVbI0OhnetVID1iPc87Vq3XidXcRiW72oaxNsft3ARwfZkzg== ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1711894608; a=rsa-sha256; cv=none; b=lEgMuhTJit1zdHGL54luyjTGiDRQH2dRSV+MolW3+UR2q9Y8xqsVtPO4COa3ae2uguuDSu DZQ/SS5krXWz8epwRmshZLq4v0F3d7MwzSteVXCsD21xTTbcOu9WlGNIoc43RUqWnrzooS vK8ons95sXsjefYIJl78smdPYA6a+HRC9wm3ApS2gFlqjQe6f61wvoSfw6r51FuMHRlx2Y uQlksbGBlqoFKiOzWw1L9VGsMLNTVXoxF8yFGjALIWjFcvLS6cy0mAXEmUN4wCFgveaRxX cq8jg44atg9jggPLXEFQYLrjcH7Ze3q47yne4/9blYECOn9R3VBfaOvjt0wGwQ== 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=1711894608; 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=0aeEtaX3nM0QJyMFbYezkrvIeHPRfX2CU9yP06nLGwk=; b=mqSOzf3QV1cCY8suwuZoez67fUlgeahm8Dw63KYN94be6F0qNhNf6wWZj9uC5UpY2WQRR5 SEtewCpYP9r1JRHxxuSb5J+duBfQ+gOr3trXa6c/jOeL7sg8HV/s+fWUXLm0jxodSCmu0U JnHrB24qXQUVhi+oyoEgbQl3HqWmSlBqel7rwttIjwQBv3Wg4nvnP9lbJt6FBjqtm4mThJ 01CL96C+6WUvyZYHIyD537lGJ/bHdqEsyj/8x7Lc/IOqabtJ+oyh2GseD9xNzWdh9rtmvp YUHIY+NUrxcIf90uEFs3AtJM3Q+7b9Rhr5U0394ibaLowrzydAVggcnaSISM/g== 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 4V6x6822LWzgyy; Sun, 31 Mar 2024 14:16:48 +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 42VEGmrE068328; Sun, 31 Mar 2024 14:16:48 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.17.1/8.17.1/Submit) id 42VEGmRA068325; Sun, 31 Mar 2024 14:16:48 GMT (envelope-from git) Date: Sun, 31 Mar 2024 14:16:48 GMT Message-Id: <202403311416.42VEGmRA068325@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: 9eff58c6d52b - 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: owner-dev-commits-src-main@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: 9eff58c6d52b66eb8abe7f724dabcd804a566df4 Auto-Submitted: auto-generated The branch main has been updated by christos: URL: https://cgit.FreeBSD.org/src/commit/?id=9eff58c6d52b66eb8abe7f724dabcd804a566df4 commit 9eff58c6d52b66eb8abe7f724dabcd804a566df4 Author: Christos Margiolis AuthorDate: 2024-03-31 14:13:43 +0000 Commit: Christos Margiolis CommitDate: 2024-03-31 14:13:43 +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, 278055, 202275, 220949, 272286 Reviewed by: dev_submerge.ch, 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 b9693908da43..754f09c7199e 100644 --- a/sys/dev/sound/pcm/dsp.c +++ b/sys/dev/sound/pcm/dsp.c @@ -295,7 +295,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 36a6f778e024..b45ca30dc984 100644 --- a/sys/dev/sound/pcm/sound.c +++ b/sys/dev/sound/pcm/sound.c @@ -1137,26 +1137,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