From nobody Thu Apr 18 12:39:57 2024 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 4VKy6628NGz5GmxR; Thu, 18 Apr 2024 12:39:58 +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 4VKy6617Ztz44KX; Thu, 18 Apr 2024 12:39:58 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1713443998; 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=2wqDgJV6sMOvc7MIc1lvLzRHp46wATZBiwIyhpH3z+8=; b=XMfDwwBhODE8ewlMN3D3ozEZEEeFczYbOE+UYbgVDxpEWVZnoWmnqOVKJBzm0gIeIJRL9I qEWWdNTAVj+Hzl9+GRFGGB56c6y9QiMPzfhAZPw9Okm7mNgCY6p3I1L2kH7C9aCMiVJKZP 3DzJ2Ggd9jZV3GMK9wPW8Dzm5FXR052fnEAilcQG98yzCdylqPdoluwFOamu5MMkvjvDKq 8yVQnXkMwhozdh71LqOLm+LpSiUHjty019puYNVe3+XwUUucOsC5aiF80iOFO8CcyI6Twx k72F71jWBiEhRG+0lmC8I8EwzrlEORjQlUzo9ygWdCm6N3GnWBoGQ27uGNsTuA== ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1713443998; a=rsa-sha256; cv=none; b=Ya4w2YmwDmKvHrRw5DCMBuio5VhAirxXEp3huoJbsRM4yBcS64qmFqhYmRZaK0J1pVT5CH ATCMyKPuOvHkf6S9eupbNnsJKjEgyIDofy9JNltxDwazFaEYy/8e3ZLevcM1AJvT2BIzEx OTvjIWimMHL0LJvEJQrkfhVaqzm117TE3dNdi4pEjXepHTipiRnuJGsxjKlNiyFTTSuMJC Cei2wuotGJUxKd1AuD1bCgbwu9oWU9+cTra9fDmneLXumTAh2nrgxJc+6q4ZJ9b6XrkvvM GVhkiVKEtkDTx3Xn+kgmwpM8o7tQqGEwqL2KdaVitQVqE8NtEsZEkTv0VFQDRw== 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=1713443998; 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=2wqDgJV6sMOvc7MIc1lvLzRHp46wATZBiwIyhpH3z+8=; b=R+H5JN+K5H1EPzfo0cbKqiWvyOGObJX0Zb43F5oQBfXHsxoe7NGWZRK8jYPIVGt/xqEJq4 R52SDIx1Khg77B15LArjdEKl9MUPk1OPq7vnrWazs9OLmC4gLT6WsV2+MEC942s8lwhu3R 8EV/5RpvCSI/tIJh/Jr78UzTdgD87wdsGo4DiWrW0ue7ICfPLFvAlHkY++wXRBUaVPbWh/ QffcI0L/x/GxoS4jLiq8NB/i+3uPJsamgO70t7pxrqP0haTb8nswgWtdU7WUVKdvT7xysS o5qa+6i7mg4pWUJH8deoXgaVL3oK/tYmO2o1Y6A7wATMQ8GU2phCa9SX1zlNjA== 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 4VKy660l6vz18KK; Thu, 18 Apr 2024 12:39:58 +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 43ICdwa4036194; Thu, 18 Apr 2024 12:39:58 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.17.1/8.17.1/Submit) id 43ICdv0W036191; Thu, 18 Apr 2024 12:39:57 GMT (envelope-from git) Date: Thu, 18 Apr 2024 12:39:57 GMT Message-Id: <202404181239.43ICdv0W036191@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-branches@FreeBSD.org From: Christos Margiolis Subject: git: d692c314d29a - stable/14 - sound: Implement asynchronous device detach List-Id: Commit messages for all branches of the src repository List-Archive: https://lists.freebsd.org/archives/dev-commits-src-all List-Help: List-Post: List-Subscribe: List-Unsubscribe: X-BeenThere: dev-commits-src-all@freebsd.org Sender: owner-dev-commits-src-all@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/stable/14 X-Git-Reftype: branch X-Git-Commit: d692c314d29a310efe875e9be05b0ccebe6b241d Auto-Submitted: auto-generated The branch stable/14 has been updated by christos: URL: https://cgit.FreeBSD.org/src/commit/?id=d692c314d29a310efe875e9be05b0ccebe6b241d commit d692c314d29a310efe875e9be05b0ccebe6b241d Author: Christos Margiolis AuthorDate: 2024-04-11 18:06:50 +0000 Commit: Christos Margiolis CommitDate: 2024-04-18 12:39:31 +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 (cherry picked from commit 44e128fe9d92c1a544b801cb56e907a66ef34691) --- 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