From nobody Tue Nov 26 14:48:53 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 4XyQSQ0dCmz5fYp3; Tue, 26 Nov 2024 14:48:54 +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 4XyQSQ09V7z4WKX; Tue, 26 Nov 2024 14:48:54 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1732632534; 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=ybMOpP/NCJh9AkLKuc6sY8dAVu3uWrYKYIbYJNltxHY=; b=GYavFab0OTRNB06ywXdiJNyG0G5Wkyx6UXzIImlcCFDKFudBo/AiQGpqstuxKy5UocO1fT w10AoH+Lf60ieQZz+EfNxxc2bqhL4pNo0AK25ETIcYSpF6UZcDVeDjyQpINvxR/WG/83p7 0lYTBxm9QQzQyrrWJbLBPpN3uueHvjZWOn+Xom8QCdPZd7qKC8MD/0rsDZqtR43HAtu194 PeYhuTVkcdA9ERuN/v/UC/6jOBZCCL+ZSm+onVCxmcu7jrYFAicykr+QhX2SwWY3HhzlLa WFncYl7Xv5mbvzXe2lPQtLaG1BFW88GpP2iGU5aZoYDa26N88Q6cLFoV/uRxyw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1732632534; 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=ybMOpP/NCJh9AkLKuc6sY8dAVu3uWrYKYIbYJNltxHY=; b=FnbL91gCL21Qc7IKh5NXoPiSbjEDF5NaYCVyTwVQyp0w49PpQ3Dz7mTZGMi418BrPp+RPj hqqF2zt6G2IEcxVMEZ2atndrZ5y1gyuPvRCvAcVWK/IcaQz2ppP9DEffMk0JvWzZjDaes3 zWO/On3Ccy9LlxKjCAbHcA4O4RGmE1oCLn0HFh54ManFTEWs4DhHB80X33mobvVArrTwid XBxMz4tN96WV5Dfi5v3totswyFHYAyztLXvHkKxKfmecqFcUAa/SaSmz1DCbm8cdhNhM3j LQsJ5wPew+sHDUg+n237z5tIaJCW4AJs7HiBVvjrUzGmtHwq36bN4U82NC1Qdw== ARC-Authentication-Results: i=1; mx1.freebsd.org; none ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1732632534; a=rsa-sha256; cv=none; b=uKaKSbzP6esipP46tFksTZVZwkqIOpGgKFuIKzMBLuI3y0XxKkeJoAcZZabDA5Ei0o3hRQ Vr77HdAukdM9yit6iy5qcK2449KMmCnfm19nRsmN1pX7KH7f5OoqtchPEb4E1none1TqGX nvDewUGXHYXMVueje0YClUYEuXsGROEu/SIus4H6TGVcgmZR2DbnVvWJZXZcuDRyVLpHRo iawlA4HFGBi+Om4+Fq7j3t1vy+VNoVdljb5HMoxeNA3wkyw9PrUhl4BFXWbAHqlDuJRNDX EI92FKxVWVnKy8QnnZMYXfsedV3XjZACwLTNHqr8/iiRoBcBBAkhtvpYGgtzqA== 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 4XyQSP6tqmzTrs; Tue, 26 Nov 2024 14:48:53 +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 4AQEmr5F084036; Tue, 26 Nov 2024 14:48:53 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.18.1/8.18.1/Submit) id 4AQEmrRw084033; Tue, 26 Nov 2024 14:48:53 GMT (envelope-from git) Date: Tue, 26 Nov 2024 14:48:53 GMT Message-Id: <202411261448.4AQEmrRw084033@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: 5bd08172b415 - main - snd_dummy: Fix callout(9) races 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: X-BeenThere: dev-commits-src-main@freebsd.org Sender: owner-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: 5bd08172b4150503c9cf60ffe3c97716c5bf6fa1 Auto-Submitted: auto-generated The branch main has been updated by christos: URL: https://cgit.FreeBSD.org/src/commit/?id=5bd08172b4150503c9cf60ffe3c97716c5bf6fa1 commit 5bd08172b4150503c9cf60ffe3c97716c5bf6fa1 Author: Christos Margiolis AuthorDate: 2024-11-26 14:48:02 +0000 Commit: Christos Margiolis CommitDate: 2024-11-26 14:48:02 +0000 snd_dummy: Fix callout(9) races Use callout_init_mtx(9) to associate the callback with the driver's lock. Also make sure the callout is stopped properly during detach. While here, introduce a dummy_active() function to know when it's appropriate to stop or not reschedule the callout. Sponsored by: The FreeBSD Foundation MFC after: 2 days Reviewed by: dev_submerge.ch, markj Differential Revision: https://reviews.freebsd.org/D47459 --- sys/dev/sound/dummy.c | 34 ++++++++++++++++++++++++++-------- 1 file changed, 26 insertions(+), 8 deletions(-) diff --git a/sys/dev/sound/dummy.c b/sys/dev/sound/dummy.c index 9013fd023a7e..b637171d82e4 100644 --- a/sys/dev/sound/dummy.c +++ b/sys/dev/sound/dummy.c @@ -67,6 +67,24 @@ struct dummy_softc { struct mtx *lock; }; +static bool +dummy_active(struct dummy_softc *sc) +{ + struct dummy_chan *ch; + int i; + + snd_mtxassert(sc->lock); + + for (i = 0; i < sc->chnum; i++) { + ch = &sc->chans[i]; + if (ch->run) + return (true); + } + + /* No channel is running at the moment. */ + return (false); +} + static void dummy_chan_io(void *arg) { @@ -74,7 +92,9 @@ dummy_chan_io(void *arg) struct dummy_chan *ch; int i = 0; - snd_mtxlock(sc->lock); + /* Do not reschedule if no channel is running. */ + if (!dummy_active(sc)) + return; for (i = 0; i < sc->chnum; i++) { ch = &sc->chans[i]; @@ -89,8 +109,6 @@ dummy_chan_io(void *arg) snd_mtxlock(sc->lock); } callout_schedule(&sc->callout, 1); - - snd_mtxunlock(sc->lock); } static int @@ -179,15 +197,15 @@ dummy_chan_trigger(kobj_t obj, void *data, int go) switch (go) { case PCMTRIG_START: - if (!callout_active(&sc->callout)) - callout_reset(&sc->callout, 1, dummy_chan_io, sc); ch->ptr = 0; ch->run = 1; + callout_reset(&sc->callout, 1, dummy_chan_io, sc); break; case PCMTRIG_STOP: case PCMTRIG_ABORT: ch->run = 0; - if (callout_active(&sc->callout)) + /* If all channels are stopped, stop the callout as well. */ + if (!dummy_active(sc)) callout_stop(&sc->callout); default: break; @@ -292,6 +310,7 @@ dummy_attach(device_t dev) sc = device_get_softc(dev); sc->dev = dev; sc->lock = snd_mtxcreate(device_get_nameunit(dev), "snd_dummy softc"); + callout_init_mtx(&sc->callout, sc->lock, 0); sc->cap_fmts[0] = SND_FORMAT(AFMT_S32_LE, 2, 0); sc->cap_fmts[1] = SND_FORMAT(AFMT_S24_LE, 2, 0); @@ -316,7 +335,6 @@ dummy_attach(device_t dev) if (pcm_register(dev, status)) return (ENXIO); mixer_init(dev, &dummy_mixer_class, sc); - callout_init(&sc->callout, 1); return (0); } @@ -327,8 +345,8 @@ dummy_detach(device_t dev) struct dummy_softc *sc = device_get_softc(dev); int err; - callout_drain(&sc->callout); err = pcm_unregister(dev); + callout_drain(&sc->callout); snd_mtxfree(sc->lock); return (err);