From nobody Fri Nov 29 12:28:21 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 4Y0CBs2lfmz5f67w; Fri, 29 Nov 2024 12:28:21 +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 4Y0CBs2Ddrz47lj; Fri, 29 Nov 2024 12:28:21 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1732883301; 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=uCed4wCp4uBbi24nP+F+WTcV45Amd9yq81Gb/Pbtc+4=; b=vrnShhP3lv2N0iXlzSR3IaKh8LsCgKr1sGMzZP1qIqyvvumQthrk1HgbWN27Q0oadmRSbw bprZU6OHEhxKxXJ61l0TbOacWvSHgbNQoIBuTviv98XX4dLNiSWsqYJkpEmeu9krMwFpQE disfWBEd+8Kkrc6hMYQTFnH/VsiiLnfark9gdWdJ3Hs6UPbWBdjCCNY6mEwJrCL/m7t7mU I/sY8FDaW7Ty5NEeadsN5575sDgR47AMy78he+Lluew1kKD9DVM6MjTQ0EUIQkXKjt33K/ 9s1gYPT+0KHJdutXEiDJr7sDQFFqm0oEjSixici51wxEOiQSoAULUTrb6hzA3Q== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1732883301; 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=uCed4wCp4uBbi24nP+F+WTcV45Amd9yq81Gb/Pbtc+4=; b=ipVHlkLBm6/2S5T982ds0jjHLxZMiSHiNoz7npa9V1KYY8Voo0dWPAQmnuqi8l+dE637eo O6bpcbGSn7OubpbLNMq1ZJ4OXp2L9EbQoWUcIb7MkbSmJAOqXyL/0TqoN4vvb3a1XMFg0W A4vXK935R7LSoxg3eVsaAIztLbeAg+unsqlekdj3VgKMflzfehGDiyHQqfACEQ3nzVVENN peOlsTGEk8ncLqkhIZZs4DECF8BkiUTYsMkJYzcH7U5FfZNeRdfA9OLwrbG+vooEXgbCir 8HfcpPyrKz5dAFQmeYnyTYZWwf4Q5qe0lhl5YHIWaW06xVGsEGPyV1V0gaU7vQ== ARC-Authentication-Results: i=1; mx1.freebsd.org; none ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1732883301; a=rsa-sha256; cv=none; b=gbQGBhZJ42i2MlHdW8FyePomhpunqyY7BrMkN3WzxJ0Y0N/Iw8ajU8FEaO0jT4JkKX5TnU EKXd4qeJXlcuIDAqXhX9Un7E8e2nqzzi2V5JlzpPVnd2N0TG35vLM3gpH5c3Czn02HaWBf cPgHh8LnuPZD24gH2/NgvtJU/TlRFzyoNo5ZXEGMvskH6R60jE1JDqmkHa5iD1/4bNlWgJ pAXeszKMoeonE0jVVhTfx+L7WYhzA7QVH7rkLxBCLxi4PauF1FLwiCCmD2WFuMxTKmHjN8 XFY7DQAOBRtRtZR7UfRZIr4H9vJjgf8sX0h7wgRuP64JjaBnLZTfbe8gV3QVtA== 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 4Y0CBs0xZ1zNYb; Fri, 29 Nov 2024 12:28:21 +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 4ATCSLoS070247; Fri, 29 Nov 2024 12:28:21 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.18.1/8.18.1/Submit) id 4ATCSLJE070244; Fri, 29 Nov 2024 12:28:21 GMT (envelope-from git) Date: Fri, 29 Nov 2024 12:28:21 GMT Message-Id: <202411291228.4ATCSLJE070244@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: 2d1cfa142c00 - stable/14 - snd_dummy: Fix callout(9) races 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: 2d1cfa142c005949f22e3eefea88dc83a8d4c44b Auto-Submitted: auto-generated The branch stable/14 has been updated by christos: URL: https://cgit.FreeBSD.org/src/commit/?id=2d1cfa142c005949f22e3eefea88dc83a8d4c44b commit 2d1cfa142c005949f22e3eefea88dc83a8d4c44b Author: Christos Margiolis AuthorDate: 2024-11-26 14:48:02 +0000 Commit: Christos Margiolis CommitDate: 2024-11-29 12:28:06 +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 (cherry picked from commit 5bd08172b4150503c9cf60ffe3c97716c5bf6fa1) --- 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 c1cd4dff15f9..1584c489b5af 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) device_get_nameunit(device_get_parent(dev))); pcm_setstatus(dev, status); 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);