git: cd4615c223b3 - stable/14 - sound: Simplify locking during device creation

From: Christos Margiolis <christos_at_FreeBSD.org>
Date: Tue, 04 Mar 2025 15:47:06 UTC
The branch stable/14 has been updated by christos:

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

commit cd4615c223b3f4ef1c892b682f57c83eebafde1c
Author:     Christos Margiolis <christos@FreeBSD.org>
AuthorDate: 2025-02-25 11:44:24 +0000
Commit:     Christos Margiolis <christos@FreeBSD.org>
CommitDate: 2025-03-04 15:46:06 +0000

    sound: Simplify locking during device creation
    
    The mechanism of acquiring SD_F_BUSY in pcm_init() and releasing it in
    pcm_register() is a leftover from the previous device creation scheme,
    where pcm_init() (previously pcm_register()) would create the sysctl
    nodes, as well as the device node. In this scenario, acquiring SD_F_BUSY
    was necessary, in order to avoid races in case the device was accessed
    before it was ready for use. Commit 66f3eb14e955 ("sound: Move sysctl
    and /dev/dspX creation to pcm_setstatus()") fixed this issue, so we can
    simplify things now. Only acquire SD_F_BUSY in pcm_addchan(), because
    chn_init() expects to be called with SD_F_BUSY acquired.
    
    While here, move the sndstat_register() call further down to be more
    robust.
    
    Sponsored by:   The FreeBSD Foundation
    MFC after:      1 week
    Reviewed by:    markj
    Differential Revision:  https://reviews.freebsd.org/D48482
    
    (cherry picked from commit fd906e47b18f53b09524647bf8431dc6170b8dfd)
---
 sys/dev/sound/pcm/sound.c | 17 +++++------------
 1 file changed, 5 insertions(+), 12 deletions(-)

diff --git a/sys/dev/sound/pcm/sound.c b/sys/dev/sound/pcm/sound.c
index 391c8e61dc19..9459b207bba0 100644
--- a/sys/dev/sound/pcm/sound.c
+++ b/sys/dev/sound/pcm/sound.c
@@ -136,9 +136,9 @@ pcm_addchan(device_t dev, int dir, kobj_class_t cls, void *devinfo)
 	struct snddev_info *d = device_get_softc(dev);
 	struct pcm_channel *ch;
 
-	PCM_BUSYASSERT(d);
-
 	PCM_LOCK(d);
+	PCM_WAIT(d);
+	PCM_ACQUIRE(d);
 	ch = chn_init(d, NULL, cls, dir, devinfo);
 	if (!ch) {
 		device_printf(d->dev, "chn_init(%s, %d, %p) failed\n",
@@ -146,6 +146,7 @@ pcm_addchan(device_t dev, int dir, kobj_class_t cls, void *devinfo)
 		PCM_UNLOCK(d);
 		return (ENODEV);
 	}
+	PCM_RELEASE(d);
 	PCM_UNLOCK(d);
 
 	return (0);
@@ -239,8 +240,6 @@ pcm_setstatus(device_t dev, char *str)
 	if (d->flags & SD_F_REGISTERED)
 		return (EINVAL);
 
-	PCM_BUSYASSERT(d);
-
 	if (d->playcount == 0 || d->reccount == 0)
 		d->flags |= SD_F_SIMPLEX;
 
@@ -253,17 +252,10 @@ pcm_setstatus(device_t dev, char *str)
 		d->flags |= SD_F_RVCHANS;
 
 	strlcpy(d->status, str, SND_STATUSLEN);
-	sndstat_register(dev, d->status);
-
-	PCM_LOCK(d);
 
 	/* Done, we're ready.. */
 	d->flags |= SD_F_REGISTERED;
 
-	PCM_RELEASE(d);
-
-	PCM_UNLOCK(d);
-
 	/*
 	 * Create all sysctls once SD_F_REGISTERED is set else
 	 * tunable sysctls won't work:
@@ -277,6 +269,8 @@ pcm_setstatus(device_t dev, char *str)
 	else if (snd_unit_auto == 1)
 		snd_unit = pcm_best_unit(snd_unit);
 
+	sndstat_register(dev, d->status);
+
 	return (dsp_make_dev(dev));
 }
 
@@ -435,7 +429,6 @@ pcm_register(device_t dev, void *devinfo, int numplay __unused,
 	d->dev = dev;
 	d->lock = snd_mtxcreate(device_get_nameunit(dev), "sound cdev");
 	cv_init(&d->cv, device_get_nameunit(dev));
-	PCM_ACQUIRE_QUICK(d);
 
 	i = 0;
 	if (resource_int_value(device_get_name(dev), device_get_unit(dev),