From nobody Wed Jul 10 16:49:00 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 4WK3j84w66z5QmsZ; Wed, 10 Jul 2024 16:49:00 +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 "R11" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 4WK3j82cxnz4g59; Wed, 10 Jul 2024 16:49:00 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1720630140; 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=LvYPkIHwsTMS9/1rnG6A/IlGTEzNnUeLguFkpN9dn0U=; b=DjmzsaZgqynvrhWppMMUb2SHh593vDgSFzt4g2rCsNOJlSNGcNO/jgK/NCuQGexu/k7HBA VpcrxqGzQXnJHWQCeQwA2q07JkwSaqFfdd2MTPUrmul1xUwKoKcPgbVamOsbCTgAMIQFlu zQHEOFxyHfnjsIha0FLp6ihYSaPxV/JfrmHtOWz6cZdEeDLxI95BEa2siizrMEvALodDPG 7ZFoVt1rpeht7fl4A68tFu3WQ/ZLfiZKVN3XddUfc1MaEI18giblt2GAdTIEOWPsqAxQph jfRLTaaaKu/c5ilkMMWFEuJ47uAYNo61JdPY6Ty4EoiL3labjDC5o/6rPj1y5A== ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1720630140; a=rsa-sha256; cv=none; b=SN5c0pcT0/dhJT2U7QZ1+oueVVhmbL74E9W3kdoxRe9w1fFnIU9tKTqfiIkWV9x6/l1X63 Yy+nbm9tnk8hOyogGJ2hIzhm1RSjtMTdqP7RIxWIIh8qwazfmf+j/Moz+elxn3bUdZPVVS 4Ad3467SpkkLg5Fo46PZ4QVhrlHuG0cmBn9Wk6L21fe57EULMCKZoDTNYYrTKt59Kokbm8 VfT8FqoOQkxYxeS7NXc7Pnc+z0fEzT7dpPRgbUI5aCaZEcnR+AgWjiOdFjjsJxQ3eU4xd3 Xm7w1vR+znPxmp7mU+aXwybwi9xIrWqbUlJJmvvq6PkOLT5q3BA3gJzvcuKYJA== 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=1720630140; 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=LvYPkIHwsTMS9/1rnG6A/IlGTEzNnUeLguFkpN9dn0U=; b=CiuTAW3PpUn0p967QcKSNmhjrc4ls079sIeob5+naP7qcBdzFANTWw0u6lOa9L5scx2h3f XqgwlsxXMdbR22OTbzcAMlZ8jnCwZsh4I+l3FCPoLErNQlydzyD+A8JhzjOIDKN4h39N4D 8oYVPhmRMgYGe4Ds+8AzCjbOPK6hfYIu9KNskMWPNRU/CSKUBLFB0po4WhyPC8TlaQvTQ3 wWy0iGUkCn1YjWlpamqlWm3tYiW+b9fXiF0ZYAiVAz4WkOI+iEtnG8NXwcs7zmfs+OE1w9 Fk0nu+SiI5za5y82iYcGlF0OZU7qaoeKwgeEKMLkcoICaEDqLSS//KIsxfiDFQ== 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 4WK3j82FKFzPmX; Wed, 10 Jul 2024 16:49:00 +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 46AGn02V034715; Wed, 10 Jul 2024 16:49:00 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.18.1/8.18.1/Submit) id 46AGn0Da034712; Wed, 10 Jul 2024 16:49:00 GMT (envelope-from git) Date: Wed, 10 Jul 2024 16:49:00 GMT Message-Id: <202407101649.46AGn0Da034712@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: c9d9975cd4dd - stable/14 - sound: Fix lock order reversals in mseq_open() 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: c9d9975cd4dd85414c4bc1e5cafb118544d5b4e7 Auto-Submitted: auto-generated The branch stable/14 has been updated by christos: URL: https://cgit.FreeBSD.org/src/commit/?id=c9d9975cd4dd85414c4bc1e5cafb118544d5b4e7 commit c9d9975cd4dd85414c4bc1e5cafb118544d5b4e7 Author: Christos Margiolis AuthorDate: 2024-07-06 18:22:21 +0000 Commit: Christos Margiolis CommitDate: 2024-07-10 16:48:12 +0000 sound: Fix lock order reversals in mseq_open() Opening /dev/sequencer after a clean reboot yields: lock order reversal: (sleepable after non-sleepable) 1st 0xfffffe004a2c2c08 seqflq (seqflq, sleep mutex) @ /mnt/src/sys/dev/sound/midi/sequencer.c:754 2nd 0xffffffff84197ed8 midistat lock (midistat lock, sx) @ /mnt/src/sys/dev/sound/midi/midi.c:1478 lock order seqflq -> midistat lock attempted at: 0xffffffff811c9029 at witness_checkorder+0x12b9 0xffffffff810f18a7 at _sx_xlock+0xf7 0xffffffff8417f992 at midimapper_open+0x22 0xffffffff84182770 at mseq_open+0xf0 0xffffffff80e3380f at devfs_open+0x30f 0xffffffff81b8b4b7 at VOP_OPEN_APV+0x57 0xffffffff812da1e7 at vn_open_vnode+0x397 0xffffffff812d96b3 at vn_open_cred+0xb23 0xffffffff812c2c6b at openatfp+0x52b 0xffffffff812c2711 at sys_openat+0x81 0xffffffff84110579 at filemon_wrapper_openat+0x19 0xffffffff81a223ae at amd64_syscall+0x39e 0xffffffff819dd0eb at fast_syscall_common+0xf8 Expose midistat_lock to midi/midi.c so that we can acquire the lock from mseq_open() before we lock seq_lock, and introduce _locked variants of midimapper_open() and midimapper_fetch_synth(). Sponsored by: The FreeBSD Foundation MFC after: 2 days Reviewed by: dev_submerge.ch, emaste Differential Revision: https://reviews.freebsd.org/D45770 (cherry picked from commit fc76e24e583d45a3a59fd7ad4e603c0679eaf572) --- sys/dev/sound/midi/midi.c | 41 ++++++++++++++++++++++++++++++++--------- sys/dev/sound/midi/midi.h | 4 ++++ sys/dev/sound/midi/sequencer.c | 8 ++++++-- 3 files changed, 42 insertions(+), 11 deletions(-) diff --git a/sys/dev/sound/midi/midi.c b/sys/dev/sound/midi/midi.c index 8dd4546d58c5..dab8fe559a0d 100644 --- a/sys/dev/sound/midi/midi.c +++ b/sys/dev/sound/midi/midi.c @@ -182,7 +182,8 @@ TAILQ_HEAD(, snd_midi) midi_devs; * /dev/midistat variables and declarations, protected by midistat_lock */ -static struct sx midistat_lock; +struct sx midistat_lock; + static int midistat_isopen = 0; static struct sbuf midistat_sbuf; static struct cdev *midistat_dev; @@ -1471,16 +1472,28 @@ midimapper_addseq(void *arg1, int *unit, void **cookie) } int -midimapper_open(void *arg1, void **cookie) +midimapper_open_locked(void *arg1, void **cookie) { int retval = 0; struct snd_midi *m; - sx_xlock(&midistat_lock); + sx_assert(&midistat_lock, SX_XLOCKED); TAILQ_FOREACH(m, &midi_devs, link) { retval++; } + + return retval; +} + +int +midimapper_open(void *arg1, void **cookie) +{ + int retval; + + sx_xlock(&midistat_lock); + retval = midimapper_open_locked(arg1, cookie); sx_xunlock(&midistat_lock); + return retval; } @@ -1491,22 +1504,32 @@ midimapper_close(void *arg1, void *cookie) } kobj_t -midimapper_fetch_synth(void *arg, void *cookie, int unit) +midimapper_fetch_synth_locked(void *arg, void *cookie, int unit) { struct snd_midi *m; int retval = 0; - sx_xlock(&midistat_lock); + sx_assert(&midistat_lock, SX_XLOCKED); TAILQ_FOREACH(m, &midi_devs, link) { - if (unit == retval) { - sx_xunlock(&midistat_lock); + if (unit == retval) return (kobj_t)m->synth; - } retval++; } - sx_xunlock(&midistat_lock); + return NULL; } +kobj_t +midimapper_fetch_synth(void *arg, void *cookie, int unit) +{ + kobj_t synth; + + sx_xlock(&midistat_lock); + synth = midimapper_fetch_synth_locked(arg, cookie, unit); + sx_xunlock(&midistat_lock); + + return synth; +} + DEV_MODULE(midi, midi_modevent, NULL); MODULE_VERSION(midi, 1); diff --git a/sys/dev/sound/midi/midi.h b/sys/dev/sound/midi/midi.h index 567279d1e654..b200eed9bc74 100644 --- a/sys/dev/sound/midi/midi.h +++ b/sys/dev/sound/midi/midi.h @@ -41,6 +41,8 @@ MALLOC_DECLARE(M_MIDI); #define MIDI_TYPE unsigned char +extern struct sx midistat_lock; + struct snd_midi; struct snd_midi * @@ -50,8 +52,10 @@ int midi_out(struct snd_midi *_m, MIDI_TYPE *_buf, int _size); int midi_in(struct snd_midi *_m, MIDI_TYPE *_buf, int _size); kobj_t midimapper_addseq(void *arg1, int *unit, void **cookie); +int midimapper_open_locked(void *arg1, void **cookie); int midimapper_open(void *arg1, void **cookie); int midimapper_close(void *arg1, void *cookie); +kobj_t midimapper_fetch_synth_locked(void *arg, void *cookie, int unit); kobj_t midimapper_fetch_synth(void *arg, void *cookie, int unit); #endif diff --git a/sys/dev/sound/midi/sequencer.c b/sys/dev/sound/midi/sequencer.c index a943e02030aa..3e4c7cf3edf8 100644 --- a/sys/dev/sound/midi/sequencer.c +++ b/sys/dev/sound/midi/sequencer.c @@ -65,6 +65,7 @@ #include #include #include +#include #ifdef HAVE_KERNEL_OPTION_HEADERS #include "opt_snd.h" @@ -752,6 +753,7 @@ mseq_open(struct cdev *i_dev, int flags, int mode, struct thread *td) * Mark this device busy. */ + sx_xlock(&midistat_lock); mtx_lock(&scp->seq_lock); if (scp->busy) { mtx_unlock(&scp->seq_lock); @@ -769,14 +771,15 @@ mseq_open(struct cdev *i_dev, int flags, int mode, struct thread *td) * Enumerate the available midi devices */ scp->midi_number = 0; - scp->maxunits = midimapper_open(scp->mapper, &scp->mapper_cookie); + scp->maxunits = midimapper_open_locked(scp->mapper, &scp->mapper_cookie); if (scp->maxunits == 0) SEQ_DEBUG(2, printf("seq_open: no midi devices\n")); for (i = 0; i < scp->maxunits; i++) { scp->midis[scp->midi_number] = - midimapper_fetch_synth(scp->mapper, scp->mapper_cookie, i); + midimapper_fetch_synth_locked(scp->mapper, + scp->mapper_cookie, i); if (scp->midis[scp->midi_number]) { if (SYNTH_OPEN(scp->midis[scp->midi_number], scp, scp->fflags) != 0) @@ -788,6 +791,7 @@ mseq_open(struct cdev *i_dev, int flags, int mode, struct thread *td) } } } + sx_xunlock(&midistat_lock); timer_setvals(scp, 60, 100);