From nobody Tue Nov 26 14:48:56 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 4XyQSS2w41z5fYp5; Tue, 26 Nov 2024 14:48:56 +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 "R10" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 4XyQSS1j3zz4WW7; Tue, 26 Nov 2024 14:48:56 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1732632536; 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=sjlYeEtzAcgvRhCNpQHXqQ4vhjox24UFH+2mkLx9Opc=; b=K5tDvPDlKZ0bxSgVstiP+V5Pa+og/1Qmui0X8RYOcLbQt9vsjBY/jwAJ5D1hpxM0bWbs7O cS0EhsnRCRRe5F5EWscnRBH+OwZXCa5aBl0CX6lmyato9wr80AEhX4Bfw21VTzJm7QI1F/ jOzJ5qLwgJd4EkGlvNMEZFDaFzC6g6OXOQqGoEqsaL71juT/loFYmwRqYcnyJnSg84wEz9 YLuHp2HdNYsb1VhVSx5lCkzXzbrxUSv7zR0Jrmq9xVMc8g5zUamIc1B0PTDOmb+X8MlqGH bE89nOTO97BjJDcv5/+tM1DojCGGRK+GKDeVw+z8/F/oaICXWuQDbwXzdI7Tvw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1732632536; 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=sjlYeEtzAcgvRhCNpQHXqQ4vhjox24UFH+2mkLx9Opc=; b=pRtpDnvd0L6o9GdObJnAVhv90N+ZXKopHg/iOyoe8LMcvIjvgWkomyerRj+vrDqDj8LDR2 2yML7eNeEQpluWAFPRQCPuJlk5+CS88EGrGmq7ogbZoBpM/nUJ2jz5z7I5dSKxseslP5OQ GBmNmsg/lB/l11ZDQKhdcj+oGIqPWAak24EqazDyCHZa+jxa+eyXa4U7MRylEFSXWp1T1I 3PjW7G6aK+BAdFGXG8YpeG/FxZkCIShmXRM1zBGlSM8EKxziMvrNvGmN5wbRnkaKolKrnu RqhB9hdbsazyz7lzJt+VQBzvXU0ZJP/3pVgS0bH6HBHfBAi28aBF49mtjMCEJQ== ARC-Authentication-Results: i=1; mx1.freebsd.org; none ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1732632536; a=rsa-sha256; cv=none; b=xzxJlOmPmrcvNsasSPeYX0bfxueiq3NWcMnqBrBAi4fnWymFRuyzvqaLG2ygcs+wFC8tVy tKUTXaoY92Ii9LXkupka1v2Z3RKrVkD/YjXkCvEP7PHg1wsVFt66PFwDPIpcRvnacUNae+ lx+GIYhNTlxqz3Xap+VCFrjlUJqSuDzOYdNlJCc8e83etQV6o2vq43rpSK77cbrDaqHfy2 HiIgDIiE5aoWwxiPjPZeLrTjIyAVT9KDiJheYPcVt1wRoUu6UEBdTI2DjVhbp6WWNiSOD3 qzHy6EZcc6PRYW4fzaH+0twDInDFGTbK706ZCM8vza1Yx2usyMl1XX6bnrme2A== 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 4XyQSS1JJXzTPW; Tue, 26 Nov 2024 14:48:56 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from gitrepo.freebsd.org ([127.0.1.44]) by gitrepo.freebsd.org (8.18.1/8.18.1) with ESMTP id 4AQEmuAM084127; Tue, 26 Nov 2024 14:48:56 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.18.1/8.18.1/Submit) id 4AQEmuib084124; Tue, 26 Nov 2024 14:48:56 GMT (envelope-from git) Date: Tue, 26 Nov 2024 14:48:56 GMT Message-Id: <202411261448.4AQEmuib084124@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: 2839ad58dd8a - main - sound: Fix hot-unload panics 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/main X-Git-Reftype: branch X-Git-Commit: 2839ad58dd8a4cf5294180fc599800c437a8d4d8 Auto-Submitted: auto-generated The branch main has been updated by christos: URL: https://cgit.FreeBSD.org/src/commit/?id=2839ad58dd8a4cf5294180fc599800c437a8d4d8 commit 2839ad58dd8a4cf5294180fc599800c437a8d4d8 Author: Christos Margiolis AuthorDate: 2024-11-26 14:48:24 +0000 Commit: Christos Margiolis CommitDate: 2024-11-26 14:48:24 +0000 sound: Fix hot-unload panics This patch fixes multiple different panic scenarios occuring during hot-unload: 1. The channel is unlocked in chn_read()/chn_write() for uiomove(9) and in the meantime we enter pcm_killchans() and free it. By the time we have returned from userland and try to lock it back, the channel will have been freed. 2. The parent channel has been freed in pcm_killchans(), but at the same time, some yet-unstopped vchan's chn_read()/chn_write() calls chn_start(), which eventually calls vchan_trigger(), which references the freed parent. 3. PCM_WAIT() panics because it references a freed PCM lock. For scenarios 1 and 2, refactor pcm_killchans() to first make sure all channels have been stopped, and then proceed to free them one by one, as opposed to freeing the first free channel until all channels have been freed. This change makes the code more robust, but might introduce some performance overhead when many channels are allocated, since we continuously loop through the channel list until all of them are stopped, and then we loop one last time to free them. For scenario 3, restructure the code so that we can use destroy_dev(9) instead of destroy_dev_sched(9) in dsp_destroy_dev(). Because destroy_dev(9) blocks until all references to the device have went away, we ensure that the PCM cv and lock will be freed safely. While here, move the delete_unrhdr(9) calls to pcm_killchans() and re-order some lines. Sponsored by: The FreeBSD Foundation MFC after: 2 days Reviewed by: dev_submerge.ch Differential Revision: https://reviews.freebsd.org/D47462 --- sys/dev/sound/pcm/dsp.c | 2 +- sys/dev/sound/pcm/sound.c | 97 ++++++++++++++++++++--------------------------- 2 files changed, 43 insertions(+), 56 deletions(-) diff --git a/sys/dev/sound/pcm/dsp.c b/sys/dev/sound/pcm/dsp.c index 6a59bcfc1ade..0f19f064a227 100644 --- a/sys/dev/sound/pcm/dsp.c +++ b/sys/dev/sound/pcm/dsp.c @@ -137,7 +137,7 @@ dsp_destroy_dev(device_t dev) struct snddev_info *d; d = device_get_softc(dev); - destroy_dev_sched(d->dsp_dev); + destroy_dev(d->dsp_dev); } static void diff --git a/sys/dev/sound/pcm/sound.c b/sys/dev/sound/pcm/sound.c index fc9d13593b9e..c3e7fee41ab3 100644 --- a/sys/dev/sound/pcm/sound.c +++ b/sys/dev/sound/pcm/sound.c @@ -211,40 +211,53 @@ static void pcm_killchans(struct snddev_info *d) { struct pcm_channel *ch; - bool found; + bool again; PCM_BUSYASSERT(d); - do { - found = false; + KASSERT(!PCM_REGISTERED(d), ("%s(): still registered\n", __func__)); + + for (;;) { + again = false; + /* Make sure all channels are stopped. */ CHN_FOREACH(ch, d, channels.pcm) { CHN_LOCK(ch); - /* - * Make sure no channel has went to sleep in the - * meantime. - */ - chn_shutdown(ch); - /* - * We have to give a thread sleeping in chn_sleep() a - * chance to observe that the channel is dead. - */ - if ((ch->flags & CHN_F_SLEEPING) == 0) { - found = true; + if ((ch->flags & CHN_F_SLEEPING) == 0 && + CHN_STOPPED(ch) && ch->inprog == 0) { CHN_UNLOCK(ch); - break; + continue; } + chn_shutdown(ch); + if (ch->direction == PCMDIR_PLAY) + chn_flush(ch); + else + chn_abort(ch); CHN_UNLOCK(ch); + again = true; } - /* - * All channels are still sleeping. Sleep for a bit and try - * again to see if any of them is awake now. + * Some channels are still active. Sleep for a bit and try + * again. */ - if (!found) { - pause_sbt("pcmkillchans", SBT_1MS * 5, 0, 0); - continue; - } + if (again) + pause_sbt("pcmkillchans", mstosbt(5), 0, 0); + else + break; + } + + /* All channels are finally dead. */ + while (!CHN_EMPTY(d, channels.pcm)) { + ch = CHN_FIRST(d, channels.pcm); chn_kill(ch); - } while (!CHN_EMPTY(d, channels.pcm)); + } + + if (d->p_unr != NULL) + delete_unrhdr(d->p_unr); + if (d->vp_unr != NULL) + delete_unrhdr(d->vp_unr); + if (d->r_unr != NULL) + delete_unrhdr(d->r_unr); + if (d->vr_unr != NULL) + delete_unrhdr(d->vr_unr); } static int @@ -512,7 +525,6 @@ int pcm_unregister(device_t dev) { struct snddev_info *d; - struct pcm_channel *ch; d = device_get_softc(dev); @@ -525,28 +537,15 @@ pcm_unregister(device_t dev) PCM_WAIT(d); d->flags |= SD_F_DETACHING; + d->flags |= SD_F_DYING; + d->flags &= ~SD_F_REGISTERED; PCM_ACQUIRE(d); PCM_UNLOCK(d); - CHN_FOREACH(ch, d, channels.pcm) { - CHN_LOCK(ch); - /* - * Do not wait for the timeout in chn_read()/chn_write(). Wake - * up the sleeping thread and kill the channel. - */ - chn_shutdown(ch); - chn_abort(ch); - CHN_UNLOCK(ch); - } + pcm_killchans(d); - /* remove /dev/sndstat entry first */ - sndstat_unregister(dev); - - PCM_LOCK(d); - d->flags |= SD_F_DYING; - d->flags &= ~SD_F_REGISTERED; - PCM_UNLOCK(d); + PCM_RELEASE_QUICK(d); if (d->play_sysctl_tree != NULL) { sysctl_ctx_free(&d->play_sysctl_ctx); @@ -557,24 +556,12 @@ pcm_unregister(device_t dev) d->rec_sysctl_tree = NULL; } + sndstat_unregister(dev); + mixer_uninit(dev); dsp_destroy_dev(dev); - (void)mixer_uninit(dev); - - pcm_killchans(d); - PCM_LOCK(d); - PCM_RELEASE(d); cv_destroy(&d->cv); - PCM_UNLOCK(d); snd_mtxfree(d->lock); - if (d->p_unr != NULL) - delete_unrhdr(d->p_unr); - if (d->vp_unr != NULL) - delete_unrhdr(d->vp_unr); - if (d->r_unr != NULL) - delete_unrhdr(d->r_unr); - if (d->vr_unr != NULL) - delete_unrhdr(d->vr_unr); if (snd_unit == device_get_unit(dev)) { snd_unit = pcm_best_unit(-1);