git: 39c51e9684f4 - main - sound: Stop taking PCM_LOCK to create and destroy channels

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

URL: https://cgit.FreeBSD.org/src/commit/?id=39c51e9684f47005f116d5ec995f8b7337dae46b

commit 39c51e9684f47005f116d5ec995f8b7337dae46b
Author:     Christos Margiolis <christos@FreeBSD.org>
AuthorDate: 2026-11-18 10:19:44 +0000
Commit:     Christos Margiolis <christos@FreeBSD.org>
CommitDate: 2026-11-18 10:19:44 +0000

    sound: Stop taking PCM_LOCK to create and destroy channels
    
    chn_init() and chn_kill() took PCM_LOCK around the channel lists and the
    per-direction counts, so that readers which cannot sleep could inspect
    them. There are none left anymore, so drop it. The few remaining
    unlocked readers never took the mutex in the first place.
    
    MFC after:      2 weeks
    Sponsored by:   The FreeBSD Foundation
    Reviewed by:    markj
    Differential Revision:  https://reviews.freebsd.org/D60512
---
 sys/dev/sound/pcm/channel.c | 6 ------
 sys/dev/sound/pcm/sound.c   | 2 --
 sys/dev/sound/pcm/vchan.c   | 4 ----
 3 files changed, 12 deletions(-)

diff --git a/sys/dev/sound/pcm/channel.c b/sys/dev/sound/pcm/channel.c
index 8e2de264bdd6..49c6908fc594 100644
--- a/sys/dev/sound/pcm/channel.c
+++ b/sys/dev/sound/pcm/channel.c
@@ -1151,7 +1151,6 @@ chn_init(struct snddev_info *d, struct pcm_channel *parent, kobj_class_t cls,
 	int err, i, direction, *vchanrate, *vchanformat;
 
 	PCM_BUSYASSERT(d);
-	PCM_LOCKASSERT(d);
 
 	switch (dir) {
 	case PCMDIR_PLAY:
@@ -1181,7 +1180,6 @@ chn_init(struct snddev_info *d, struct pcm_channel *parent, kobj_class_t cls,
 		return (NULL);
 	}
 
-	PCM_UNLOCK(d);
 	b = NULL;
 	bs = NULL;
 
@@ -1261,7 +1259,6 @@ chn_init(struct snddev_info *d, struct pcm_channel *parent, kobj_class_t cls,
 			goto fail;
 	}
 
-	PCM_LOCK(d);
 	CHN_INSERT_SORT_ASCEND(d, c, channels.pcm);
 	if ((c->flags & CHN_F_VIRTUAL) == 0) {
 		CHN_INSERT_SORT_ASCEND(d, c, channels.pcm.primary);
@@ -1274,7 +1271,6 @@ chn_init(struct snddev_info *d, struct pcm_channel *parent, kobj_class_t cls,
 
 fail:
 	chn_kill(c);
-	PCM_LOCK(d);
 
 	return (NULL);
 }
@@ -1288,7 +1284,6 @@ chn_kill(struct pcm_channel *c)
 
 	PCM_BUSYASSERT(c->parentsnddev);
 
-	PCM_LOCK(d);
 	CHN_REMOVE(d, c, channels.pcm);
 	if ((c->flags & CHN_F_VIRTUAL) == 0)
 		CHN_REMOVE(d, c, channels.pcm.primary);
@@ -1309,7 +1304,6 @@ chn_kill(struct pcm_channel *c)
 	default:
 		__assert_unreachable();
 	}
-	PCM_UNLOCK(d);
 
 	if (CHN_STARTED(c)) {
 		CHN_LOCK(c);
diff --git a/sys/dev/sound/pcm/sound.c b/sys/dev/sound/pcm/sound.c
index 22a9c25436ff..267d48f79ad9 100644
--- a/sys/dev/sound/pcm/sound.c
+++ b/sys/dev/sound/pcm/sound.c
@@ -133,14 +133,12 @@ pcm_addchan(device_t dev, int dir, kobj_class_t cls, void *devinfo)
 	int err = 0;
 
 	PCM_ACQUIRE(d);
-	PCM_LOCK(d);
 	ch = chn_init(d, NULL, cls, dir, devinfo);
 	if (!ch) {
 		device_printf(d->dev, "chn_init(%s, %d, %p) failed\n",
 		    cls->name, dir, devinfo);
 		err = ENODEV;
 	}
-	PCM_UNLOCK(d);
 	PCM_RELEASE(d);
 
 	return (err);
diff --git a/sys/dev/sound/pcm/vchan.c b/sys/dev/sound/pcm/vchan.c
index 31fedac458a8..43410d19d868 100644
--- a/sys/dev/sound/pcm/vchan.c
+++ b/sys/dev/sound/pcm/vchan.c
@@ -605,14 +605,12 @@ vchan_create(struct pcm_channel *parent, struct pcm_channel **child)
 		return (EINVAL);
 
 	CHN_UNLOCK(parent);
-	PCM_LOCK(d);
 
 	/*
 	 * This is the only place where channels are created at runtime, so do
 	 * not create any if we are tearing down.
 	 */
 	if (!PCM_REGISTERED(d)) {
-		PCM_UNLOCK(d);
 		CHN_LOCK(parent);
 		return (ENODEV);
 	}
@@ -630,11 +628,9 @@ vchan_create(struct pcm_channel *parent, struct pcm_channel **child)
 	/* create a new playback channel */
 	ch = chn_init(d, parent, &vchan_class, direction, parent);
 	if (ch == NULL) {
-		PCM_UNLOCK(d);
 		CHN_LOCK(parent);
 		return (ENODEV);
 	}
-	PCM_UNLOCK(d);
 
 	CHN_LOCK(parent);
 	CHN_INSERT_SORT_ASCEND(parent, ch, children);