git: 406358d55795 - main - sound: Fix a LOR between AIOGCAP and SNDCTL_DSP_SYNCGROUP

From: Christos Margiolis <christos_at_FreeBSD.org>
Date: Wed, 18 Nov 2026 10:24:45 UTC
The branch main has been updated by christos:

URL: https://cgit.FreeBSD.org/src/commit/?id=406358d55795a6f3ea302a5e4c0289a9a4ca8e7d

commit 406358d55795a6f3ea302a5e4c0289a9a4ca8e7d
Author:     Christos Margiolis <christos@FreeBSD.org>
AuthorDate: 2026-11-18 10:23:14 +0000
Commit:     Christos Margiolis <christos@FreeBSD.org>
CommitDate: 2026-11-18 10:23:14 +0000

    sound: Fix a LOR between AIOGCAP and SNDCTL_DSP_SYNCGROUP
    
    AIOGCAP locks a dsp_cdevpriv's record channel and then its play channel,
    while dsp_oss_syncgroup() locked them the other way round. Verified the
    LOR and deadlock by exercising both paths using a duplex FD
    concurrently.
    
    Lock the record channel first in dsp_oss_syncgroup() too, and document
    the order in sound.h.
    
    MFC after:      2 weeks
    Sponsored by:   The FreeBSD Foundation
    Reviewed by:    markj
    Differential Revision:  https://reviews.freebsd.org/D60519
---
 sys/dev/sound/pcm/dsp.c   | 10 +++++-----
 sys/dev/sound/pcm/sound.h |  2 ++
 2 files changed, 7 insertions(+), 5 deletions(-)

diff --git a/sys/dev/sound/pcm/dsp.c b/sys/dev/sound/pcm/dsp.c
index d1e9c1f7bc89..77faac3b5589 100644
--- a/sys/dev/sound/pcm/dsp.c
+++ b/sys/dev/sound/pcm/dsp.c
@@ -2487,16 +2487,16 @@ dsp_oss_syncgroup(struct pcm_channel *wrch, struct pcm_channel *rdch, oss_syncgr
 	/*
 	 * If device's channels are already mapped to a group, unmap them.
 	 */
-	if (wrch) {
-		CHN_LOCK(wrch);
-		sg_ids[0] = chn_syncdestroy(wrch);
-	}
-
 	if (rdch) {
 		CHN_LOCK(rdch);
 		sg_ids[1] = chn_syncdestroy(rdch);
 	}
 
+	if (wrch) {
+		CHN_LOCK(wrch);
+		sg_ids[0] = chn_syncdestroy(wrch);
+	}
+
 	/*
 	 * Verify that mode matches character device properites.
 	 *  - Bail if PCM_ENABLE_OUTPUT && wrch == NULL.
diff --git a/sys/dev/sound/pcm/sound.h b/sys/dev/sound/pcm/sound.h
index c09ea120bcf0..7e71d18f063f 100644
--- a/sys/dev/sound/pcm/sound.h
+++ b/sys/dev/sound/pcm/sound.h
@@ -181,6 +181,8 @@ int snd_setup_intr(device_t dev, struct resource *res, int flags,
  * Lock order:
  *
  * Giant -> PCM_ACQUIRE() -> PCM_LOCK() -> CHN_LOCK(parent) -> CHN_LOCK(child).
+ *
+ * A dsp_cdevpriv's record channel is locked before its play channel.
  */
 struct snddev_info {
 	struct {