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 66f3eb14e9 ("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
This commit is contained in:
Christos Margiolis 2025-02-25 13:44:24 +02:00
parent 352aa9ad1d
commit fd906e47b1

View file

@ -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);
@ -388,7 +389,6 @@ pcm_init(device_t dev, void *devinfo)
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),
@ -428,8 +428,6 @@ pcm_register(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;
@ -442,17 +440,10 @@ pcm_register(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:
@ -466,6 +457,8 @@ pcm_register(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));
}