git: bacd867aa986 - main - sound: Convert the SD_F_BUSY cv to an sx
- Go to: [ bottom of page ] [ top of archives ] [ this month ]
Date: Wed, 18 Nov 2026 10:24:32 UTC
The branch main has been updated by christos:
URL: https://cgit.FreeBSD.org/src/commit/?id=bacd867aa986d924fa7b73e54ef9ef216ab8123e
commit bacd867aa986d924fa7b73e54ef9ef216ab8123e
Author: Christos Margiolis <christos@FreeBSD.org>
AuthorDate: 2026-11-18 10:17:59 +0000
Commit: Christos Margiolis <christos@FreeBSD.org>
CommitDate: 2026-11-18 10:17:59 +0000
sound: Convert the SD_F_BUSY cv to an sx
SD_F_BUSY was essentially a custom sleepable lock, which was invisible
to WITNESS. Replace it with an sx lock. This simplifies and removes some
of the PCM macros, and gives us WITNESS coverage.
PCM_WAIT(), PCM_ACQUIRE() and PCM_RELEASE() all asserted that PCM_LOCK
is held, which forced callers to take the mutex just to use the macros.
chn_vpc_proc() and sysctl_hw_snd_feeder_rate_quality() are two such
callers. Simplify this.
Changes to snddev_info->flags are now serialized by the sx alone, which
makes the protocol uniform. Previously some writers held the busy flag,
some held busy and the mutex, and others just the mutex.
snddev_info->eqpreamp was consistent, but its only writer held the mutex
needlessly, so drop that too. Document this above the sx definition in
sound.h, and assert the flags protocol in pcm_setflags().
Name both locks after what they are rather than after the device unit,
so they are distinguishable in WITNESS.
MFC after: 2 weeks
Sponsored by: The FreeBSD Foundation
Reviewed by: markj
Differential Revision: https://reviews.freebsd.org/D60128
---
sys/dev/sound/pci/es137x.c | 10 +-
sys/dev/sound/pcm/channel.c | 3 -
sys/dev/sound/pcm/dsp.c | 220 +++++++++++++++++++---------------------
sys/dev/sound/pcm/feeder_eq.c | 34 ++-----
sys/dev/sound/pcm/feeder_rate.c | 3 -
sys/dev/sound/pcm/mixer.c | 4 +-
sys/dev/sound/pcm/sound.c | 43 +++-----
sys/dev/sound/pcm/sound.h | 66 ++++--------
sys/dev/sound/pcm/vchan.c | 74 ++++++--------
sys/dev/sound/sndstat.c | 12 +--
10 files changed, 189 insertions(+), 280 deletions(-)
diff --git a/sys/dev/sound/pci/es137x.c b/sys/dev/sound/pci/es137x.c
index 2e875672dbfa..ec1eab984906 100644
--- a/sys/dev/sound/pci/es137x.c
+++ b/sys/dev/sound/pci/es137x.c
@@ -1537,23 +1537,23 @@ sysctl_es137x_single_pcm_mixer(SYSCTL_HANDLER_ARGS)
return (EINVAL);
if (val == set)
return (0);
- PCM_ACQUIRE_QUICK(d);
+ PCM_ACQUIRE(d);
m = d->mixer;
if (m == NULL) {
- PCM_RELEASE_QUICK(d);
+ PCM_RELEASE(d);
return (ENODEV);
}
level = mix_get(m, SOUND_MIXER_PCM);
recsrc = mix_getrecsrc(m);
if (level < 0 || recsrc < 0) {
- PCM_RELEASE_QUICK(d);
+ PCM_RELEASE(d);
return (ENXIO);
}
ES_LOCK(es);
if (es->ctrl & (CTRL_ADC_EN | CTRL_DAC1_EN | CTRL_DAC2_EN)) {
ES_UNLOCK(es);
- PCM_RELEASE_QUICK(d);
+ PCM_RELEASE(d);
return (EBUSY);
}
if (val)
@@ -1583,7 +1583,7 @@ sysctl_es137x_single_pcm_mixer(SYSCTL_HANDLER_ARGS)
err = mix_setrecsrc(m, recsrc);
}
- PCM_RELEASE_QUICK(d);
+ PCM_RELEASE(d);
return (err);
}
diff --git a/sys/dev/sound/pcm/channel.c b/sys/dev/sound/pcm/channel.c
index 11aaa59ebd1c..ba9cd75109e4 100644
--- a/sys/dev/sound/pcm/channel.c
+++ b/sys/dev/sound/pcm/channel.c
@@ -138,8 +138,6 @@ chn_vpc_proc(int reset, int db)
d = devclass_get_softc(pcm_devclass, i);
if (!PCM_REGISTERED(d))
continue;
- PCM_LOCK(d);
- PCM_WAIT(d);
PCM_ACQUIRE(d);
CHN_FOREACH(c, d, channels.pcm) {
CHN_LOCK(c);
@@ -149,7 +147,6 @@ chn_vpc_proc(int reset, int db)
CHN_UNLOCK(c);
}
PCM_RELEASE(d);
- PCM_UNLOCK(d);
}
bus_topo_unlock();
}
diff --git a/sys/dev/sound/pcm/dsp.c b/sys/dev/sound/pcm/dsp.c
index 8f05139b3617..216bd3c68cae 100644
--- a/sys/dev/sound/pcm/dsp.c
+++ b/sys/dev/sound/pcm/dsp.c
@@ -265,9 +265,8 @@ dsp_close(void *data)
PCM_GIANT_ENTER(d);
- PCM_LOCK(d);
- PCM_WAIT(d);
PCM_ACQUIRE(d);
+ PCM_LOCK(d);
rdch = priv->rdch;
wrch = priv->wrch;
@@ -277,76 +276,71 @@ dsp_close(void *data)
if (wrch != NULL)
CHN_REMOVE(d, wrch, channels.pcm.opened);
- if (rdch != NULL || wrch != NULL) {
- PCM_UNLOCK(d);
- if (rdch != NULL) {
- /*
- * The channel itself need not be locked because:
- * a) Adding a channel to a syncgroup happens only
- * in dsp_ioctl(), which cannot run concurrently
- * to dsp_close().
- * b) The syncmember pointer (sm) is protected by
- * the global syncgroup list lock.
- * c) A channel can't just disappear, invalidating
- * pointers, unless it's closed/dereferenced
- * first.
- */
- PCM_SG_LOCK();
- sg_ids = chn_syncdestroy(rdch);
- PCM_SG_UNLOCK();
- if (sg_ids != 0)
- free_unr(pcmsg_unrhdr, sg_ids);
+ PCM_UNLOCK(d);
- /*
- * Go through the channel abort/flush path for both
- * primary and virtual channels to ensure that, in the
- * case of vchans, the stream is always properly
- * stopped, and the primary channels do not keep being
- * interrupted even if all vchans are gone.
- */
+ if (rdch != NULL) {
+ /*
+ * The channel itself need not be locked because:
+ * a) Adding a channel to a syncgroup happens only in
+ * dsp_ioctl(), which cannot run concurrently to
+ * dsp_close().
+ * b) The syncmember pointer (sm) is protected by the global
+ * syncgroup list lock.
+ * c) A channel can't just disappear, invalidating pointers,
+ * unless it's closed/dereferenced first.
+ */
+ PCM_SG_LOCK();
+ sg_ids = chn_syncdestroy(rdch);
+ PCM_SG_UNLOCK();
+ if (sg_ids != 0)
+ free_unr(pcmsg_unrhdr, sg_ids);
+
+ /*
+ * Go through the channel abort/flush path for both primary and
+ * virtual channels to ensure that, in the case of vchans, the
+ * stream is always properly stopped, and the primary channels
+ * do not keep being interrupted even if all vchans are gone.
+ */
+ CHN_LOCK(rdch);
+ chn_abort(rdch); /* won't sleep */
+ rdch->flags &= ~(CHN_F_RUNNING | CHN_F_MMAP |
+ CHN_F_DEAD | CHN_F_EXCLUSIVE | CHN_F_NBIO);
+ chn_reset(rdch, 0, 0);
+ chn_release(rdch);
+ if (rdch->flags & CHN_F_VIRTUAL) {
+ parent = rdch->parentchannel;
+ CHN_LOCK(parent);
CHN_LOCK(rdch);
- chn_abort(rdch); /* won't sleep */
- rdch->flags &= ~(CHN_F_RUNNING | CHN_F_MMAP |
- CHN_F_DEAD | CHN_F_EXCLUSIVE | CHN_F_NBIO);
- chn_reset(rdch, 0, 0);
- chn_release(rdch);
- if (rdch->flags & CHN_F_VIRTUAL) {
- parent = rdch->parentchannel;
- CHN_LOCK(parent);
- CHN_LOCK(rdch);
- vchan_destroy(rdch);
- CHN_UNLOCK(parent);
- }
+ vchan_destroy(rdch);
+ CHN_UNLOCK(parent);
}
- if (wrch != NULL) {
- /*
- * Please see block above.
- */
- PCM_SG_LOCK();
- sg_ids = chn_syncdestroy(wrch);
- PCM_SG_UNLOCK();
- if (sg_ids != 0)
- free_unr(pcmsg_unrhdr, sg_ids);
+ }
+ if (wrch != NULL) {
+ /*
+ * Please see block above.
+ */
+ PCM_SG_LOCK();
+ sg_ids = chn_syncdestroy(wrch);
+ PCM_SG_UNLOCK();
+ if (sg_ids != 0)
+ free_unr(pcmsg_unrhdr, sg_ids);
+ CHN_LOCK(wrch);
+ chn_flush(wrch); /* may sleep */
+ wrch->flags &= ~(CHN_F_RUNNING | CHN_F_MMAP |
+ CHN_F_DEAD | CHN_F_EXCLUSIVE | CHN_F_NBIO);
+ chn_reset(wrch, 0, 0);
+ chn_release(wrch);
+ if (wrch->flags & CHN_F_VIRTUAL) {
+ parent = wrch->parentchannel;
+ CHN_LOCK(parent);
CHN_LOCK(wrch);
- chn_flush(wrch); /* may sleep */
- wrch->flags &= ~(CHN_F_RUNNING | CHN_F_MMAP |
- CHN_F_DEAD | CHN_F_EXCLUSIVE | CHN_F_NBIO);
- chn_reset(wrch, 0, 0);
- chn_release(wrch);
- if (wrch->flags & CHN_F_VIRTUAL) {
- parent = wrch->parentchannel;
- CHN_LOCK(parent);
- CHN_LOCK(wrch);
- vchan_destroy(wrch);
- CHN_UNLOCK(parent);
- }
+ vchan_destroy(wrch);
+ CHN_UNLOCK(parent);
}
- PCM_LOCK(d);
}
PCM_RELEASE(d);
- PCM_UNLOCK(d);
PCM_GIANT_LEAVE(d);
skip:
@@ -383,8 +377,8 @@ dsp_open(struct cdev *i_dev, int flags, int mode, struct thread *td)
PCM_GIANT_ENTER(d);
/* Lock snddev so nobody else can monkey with it. */
+ PCM_ACQUIRE(d);
PCM_LOCK(d);
- PCM_WAIT(d);
error = 0;
if (!DSP_F_VALID(flags))
@@ -423,24 +417,18 @@ dsp_open(struct cdev *i_dev, int flags, int mode, struct thread *td)
error = ENOTSUP;
}
}
+ PCM_UNLOCK(d);
+
if (error != 0) {
- PCM_UNLOCK(d);
+ PCM_RELEASE(d);
PCM_GIANT_EXIT(d);
return (error);
}
- /*
- * That is just enough. Acquire and unlock pcm lock so
- * the other will just have to wait until we finish doing
- * everything.
- */
- PCM_ACQUIRE(d);
- PCM_UNLOCK(d);
-
if (DSP_F_WRITE(flags)) {
error = dsp_chn_alloc(d, &priv->wrch, PCMDIR_PLAY, flags, td);
if (error != 0) {
- PCM_RELEASE_QUICK(d);
+ PCM_RELEASE(d);
PCM_GIANT_EXIT(d);
return (error);
}
@@ -451,7 +439,7 @@ dsp_open(struct cdev *i_dev, int flags, int mode, struct thread *td)
if (DSP_F_READ(flags)) {
error = dsp_chn_alloc(d, &priv->rdch, PCMDIR_REC, flags, td);
if (error != 0) {
- PCM_RELEASE_QUICK(d);
+ PCM_RELEASE(d);
PCM_GIANT_EXIT(d);
return (error);
}
@@ -460,7 +448,7 @@ dsp_open(struct cdev *i_dev, int flags, int mode, struct thread *td)
PCM_UNLOCK(d);
}
- PCM_RELEASE_QUICK(d);
+ PCM_RELEASE(d);
PCM_GIANT_LEAVE(d);
return (0);
@@ -735,9 +723,9 @@ dsp_ioctl(struct cdev *i_dev, unsigned long cmd, caddr_t arg, int mode,
}
if (MIXER_REGISTERED(d->mixer)) {
- PCM_ACQUIRE_QUICK(d);
+ PCM_ACQUIRE(d);
ret = mixer_ioctl_cmd(d->mixer->cdev, cmd, arg, -1, td);
- PCM_RELEASE_QUICK(d);
+ PCM_RELEASE(d);
} else
ret = EBADF;
@@ -751,7 +739,7 @@ dsp_ioctl(struct cdev *i_dev, unsigned long cmd, caddr_t arg, int mode,
* and MIDI). Handle those special cases here.
*/
if (IOCGROUP(cmd) == 'X') {
- PCM_ACQUIRE_QUICK(d);
+ PCM_ACQUIRE(d);
switch(cmd) {
case SNDCTL_SYSINFO:
sound_oss_sysinfo((oss_sysinfo *)arg);
@@ -776,7 +764,7 @@ dsp_ioctl(struct cdev *i_dev, unsigned long cmd, caddr_t arg, int mode,
default:
ret = EINVAL;
}
- PCM_RELEASE_QUICK(d);
+ PCM_RELEASE(d);
PCM_GIANT_EXIT(d);
return (ret);
}
@@ -812,7 +800,7 @@ dsp_ioctl(struct cdev *i_dev, unsigned long cmd, caddr_t arg, int mode,
p->play_size = 0;
p->rec_size = 0;
- PCM_ACQUIRE_QUICK(d);
+ PCM_ACQUIRE(d);
if (wrch) {
CHN_LOCK(wrch);
chn_setblocksize(wrch, 2, p->play_size);
@@ -825,7 +813,7 @@ dsp_ioctl(struct cdev *i_dev, unsigned long cmd, caddr_t arg, int mode,
p->rec_size = rdch->bufsoft->blksz;
CHN_UNLOCK(rdch);
}
- PCM_RELEASE_QUICK(d);
+ PCM_RELEASE(d);
}
break;
case AIOGSIZE: /* get the current blocksize */
@@ -872,7 +860,7 @@ dsp_ioctl(struct cdev *i_dev, unsigned long cmd, caddr_t arg, int mode,
ret = EINVAL;
break;
}
- PCM_ACQUIRE_QUICK(d);
+ PCM_ACQUIRE(d);
if (wrch) {
CHN_LOCK(wrch);
if (cmd == AIOSFMT && p->play_format != 0) {
@@ -905,7 +893,7 @@ dsp_ioctl(struct cdev *i_dev, unsigned long cmd, caddr_t arg, int mode,
p->rec_rate = 0;
p->rec_format = 0;
}
- PCM_RELEASE_QUICK(d);
+ PCM_RELEASE(d);
#ifdef COMPAT_FREEBSD32
if (cmd == AIOSFMT32 || cmd == AIOGFMT32) {
p32->play_rate = p->play_rate;
@@ -1054,7 +1042,7 @@ dsp_ioctl(struct cdev *i_dev, unsigned long cmd, caddr_t arg, int mode,
case SNDCTL_DSP_SETBLKSIZE:
RANGE(*arg_i, 16, 65536);
- PCM_ACQUIRE_QUICK(d);
+ PCM_ACQUIRE(d);
if (wrch) {
CHN_LOCK(wrch);
chn_setblocksize(wrch, 2, *arg_i);
@@ -1065,7 +1053,7 @@ dsp_ioctl(struct cdev *i_dev, unsigned long cmd, caddr_t arg, int mode,
chn_setblocksize(rdch, 2, *arg_i);
CHN_UNLOCK(rdch);
}
- PCM_RELEASE_QUICK(d);
+ PCM_RELEASE(d);
break;
case SNDCTL_DSP_RESET:
@@ -1097,7 +1085,7 @@ dsp_ioctl(struct cdev *i_dev, unsigned long cmd, caddr_t arg, int mode,
case SNDCTL_DSP_SPEED:
/* chn_setspeed may sleep */
tmp = 0;
- PCM_ACQUIRE_QUICK(d);
+ PCM_ACQUIRE(d);
if (wrch) {
CHN_LOCK(wrch);
ret = chn_setspeed(wrch, *arg_i);
@@ -1111,7 +1099,7 @@ dsp_ioctl(struct cdev *i_dev, unsigned long cmd, caddr_t arg, int mode,
tmp = rdch->speed;
CHN_UNLOCK(rdch);
}
- PCM_RELEASE_QUICK(d);
+ PCM_RELEASE(d);
*arg_i = tmp;
break;
@@ -1130,7 +1118,7 @@ dsp_ioctl(struct cdev *i_dev, unsigned long cmd, caddr_t arg, int mode,
case SNDCTL_DSP_STEREO:
tmp = -1;
*arg_i = (*arg_i)? 2 : 1;
- PCM_ACQUIRE_QUICK(d);
+ PCM_ACQUIRE(d);
if (wrch) {
CHN_LOCK(wrch);
ret = chn_setformat(wrch,
@@ -1146,7 +1134,7 @@ dsp_ioctl(struct cdev *i_dev, unsigned long cmd, caddr_t arg, int mode,
tmp = (AFMT_CHANNEL(rdch->format) > 1)? 1 : 0;
CHN_UNLOCK(rdch);
}
- PCM_RELEASE_QUICK(d);
+ PCM_RELEASE(d);
*arg_i = tmp;
break;
@@ -1178,7 +1166,7 @@ dsp_ioctl(struct cdev *i_dev, unsigned long cmd, caddr_t arg, int mode,
ext = m->ext;
}
- PCM_ACQUIRE_QUICK(d);
+ PCM_ACQUIRE(d);
if (wrch) {
CHN_LOCK(wrch);
ret = chn_setformat(wrch,
@@ -1194,7 +1182,7 @@ dsp_ioctl(struct cdev *i_dev, unsigned long cmd, caddr_t arg, int mode,
tmp = AFMT_CHANNEL(rdch->format);
CHN_UNLOCK(rdch);
}
- PCM_RELEASE_QUICK(d);
+ PCM_RELEASE(d);
*arg_i = tmp;
} else {
chn = wrch ? wrch : rdch;
@@ -1231,7 +1219,7 @@ dsp_ioctl(struct cdev *i_dev, unsigned long cmd, caddr_t arg, int mode,
case SNDCTL_DSP_SETFMT: /* sets _one_ format */
if (*arg_i != AFMT_QUERY) {
tmp = 0;
- PCM_ACQUIRE_QUICK(d);
+ PCM_ACQUIRE(d);
if (wrch) {
CHN_LOCK(wrch);
ret = chn_setformat(wrch, SND_FORMAT(*arg_i,
@@ -1249,7 +1237,7 @@ dsp_ioctl(struct cdev *i_dev, unsigned long cmd, caddr_t arg, int mode,
tmp = rdch->format;
CHN_UNLOCK(rdch);
}
- PCM_RELEASE_QUICK(d);
+ PCM_RELEASE(d);
*arg_i = AFMT_ENCODING(tmp);
} else {
chn = wrch ? wrch : rdch;
@@ -1271,7 +1259,7 @@ dsp_ioctl(struct cdev *i_dev, unsigned long cmd, caddr_t arg, int mode,
fragsz = 1 << fragln;
DEB(printf("SNDCTL_DSP_SETFRAGMENT %d frags, %d sz\n", maxfrags, fragsz));
- PCM_ACQUIRE_QUICK(d);
+ PCM_ACQUIRE(d);
if (rdch) {
CHN_LOCK(rdch);
ret = chn_setblocksize(rdch,
@@ -1296,7 +1284,7 @@ dsp_ioctl(struct cdev *i_dev, unsigned long cmd, caddr_t arg, int mode,
maxfrags = r_maxfrags;
fragsz = r_fragsz;
}
- PCM_RELEASE_QUICK(d);
+ PCM_RELEASE(d);
fragln = 0;
while (fragsz > 1) {
@@ -1478,10 +1466,10 @@ dsp_ioctl(struct cdev *i_dev, unsigned long cmd, caddr_t arg, int mode,
* switch to full-duplex mode if card is in half-duplex
* mode and is able to work in full-duplex mode
*/
- PCM_LOCK(d);
+ PCM_ACQUIRE(d);
if (rdch && wrch && (pcm_getflags(d->dev) & SD_F_SIMPLEX))
pcm_setflags(d->dev, pcm_getflags(d->dev)^SD_F_SIMPLEX);
- PCM_UNLOCK(d);
+ PCM_RELEASE(d);
break;
/*
@@ -1520,9 +1508,9 @@ dsp_ioctl(struct cdev *i_dev, unsigned long cmd, caddr_t arg, int mode,
}
if (MIXER_REGISTERED(d->mixer)) {
- PCM_ACQUIRE_QUICK(d);
+ PCM_ACQUIRE(d);
ret = mixer_ioctl_cmd(d->mixer->cdev, xcmd, arg, -1, td);
- PCM_RELEASE_QUICK(d);
+ PCM_RELEASE(d);
} else
ret = ENOTSUP;
@@ -1532,9 +1520,9 @@ dsp_ioctl(struct cdev *i_dev, unsigned long cmd, caddr_t arg, int mode,
case SNDCTL_DSP_GET_RECSRC:
case SNDCTL_DSP_SET_RECSRC:
if (MIXER_REGISTERED(d->mixer)) {
- PCM_ACQUIRE_QUICK(d);
+ PCM_ACQUIRE(d);
ret = mixer_ioctl_cmd(d->mixer->cdev, cmd, arg, -1, td);
- PCM_RELEASE_QUICK(d);
+ PCM_RELEASE(d);
} else
ret = ENOTSUP;
break;
@@ -1741,43 +1729,43 @@ dsp_ioctl(struct cdev *i_dev, unsigned long cmd, caddr_t arg, int mode,
break;
case SNDCTL_DSP_SYNCGROUP:
- PCM_ACQUIRE_QUICK(d);
+ PCM_ACQUIRE(d);
ret = dsp_oss_syncgroup(wrch, rdch, (oss_syncgroup *)arg);
- PCM_RELEASE_QUICK(d);
+ PCM_RELEASE(d);
break;
case SNDCTL_DSP_SYNCSTART:
- PCM_ACQUIRE_QUICK(d);
+ PCM_ACQUIRE(d);
ret = dsp_oss_syncstart(*arg_i);
- PCM_RELEASE_QUICK(d);
+ PCM_RELEASE(d);
break;
case SNDCTL_DSP_POLICY:
- PCM_ACQUIRE_QUICK(d);
+ PCM_ACQUIRE(d);
ret = dsp_oss_policy(wrch, rdch, *arg_i);
- PCM_RELEASE_QUICK(d);
+ PCM_RELEASE(d);
break;
case SNDCTL_DSP_COOKEDMODE:
- PCM_ACQUIRE_QUICK(d);
+ PCM_ACQUIRE(d);
if (!(pcm_getflags(d->dev) & SD_F_BITPERFECT))
ret = dsp_oss_cookedmode(wrch, rdch, *arg_i);
- PCM_RELEASE_QUICK(d);
+ PCM_RELEASE(d);
break;
case SNDCTL_DSP_GET_CHNORDER:
- PCM_ACQUIRE_QUICK(d);
+ PCM_ACQUIRE(d);
ret = dsp_oss_getchnorder(wrch, rdch, (unsigned long long *)arg);
- PCM_RELEASE_QUICK(d);
+ PCM_RELEASE(d);
break;
case SNDCTL_DSP_SET_CHNORDER:
- PCM_ACQUIRE_QUICK(d);
+ PCM_ACQUIRE(d);
ret = dsp_oss_setchnorder(wrch, rdch, (unsigned long long *)arg);
- PCM_RELEASE_QUICK(d);
+ PCM_RELEASE(d);
break;
case SNDCTL_DSP_GETCHANNELMASK: /* XXX vlc */
- PCM_ACQUIRE_QUICK(d);
+ PCM_ACQUIRE(d);
ret = dsp_oss_getchannelmask(wrch, rdch, (int *)arg);
- PCM_RELEASE_QUICK(d);
+ PCM_RELEASE(d);
break;
case SNDCTL_DSP_BIND_CHANNEL: /* XXX what?!? */
ret = EINVAL;
diff --git a/sys/dev/sound/pcm/feeder_eq.c b/sys/dev/sound/pcm/feeder_eq.c
index 52e96fa91768..f23758634f13 100644
--- a/sys/dev/sound/pcm/feeder_eq.c
+++ b/sys/dev/sound/pcm/feeder_eq.c
@@ -445,35 +445,27 @@ sysctl_dev_pcm_eq(SYSCTL_HANDLER_ARGS)
if (!PCM_REGISTERED(d))
return (ENODEV);
- PCM_LOCK(d);
- PCM_WAIT(d);
+ PCM_ACQUIRE(d);
if (d->flags & SD_F_EQ)
val = 1;
else
val = 0;
- PCM_ACQUIRE(d);
- PCM_UNLOCK(d);
oval = val;
err = sysctl_handle_int(oidp, &val, 0, req);
if (err == 0 && req->newptr != NULL && val != oval) {
if (!(val == 0 || val == 1)) {
- PCM_RELEASE_QUICK(d);
+ PCM_RELEASE(d);
return (EINVAL);
}
- PCM_LOCK(d);
-
if (val == 1)
d->flags |= SD_F_EQ;
else
d->flags &= ~SD_F_EQ;
-
- PCM_RELEASE(d);
- PCM_UNLOCK(d);
- } else
- PCM_RELEASE_QUICK(d);
+ }
+ PCM_RELEASE(d);
return (err);
}
@@ -491,15 +483,12 @@ sysctl_dev_pcm_eq_preamp(SYSCTL_HANDLER_ARGS)
if (!PCM_REGISTERED(d))
return (ENODEV);
- PCM_LOCK(d);
- PCM_WAIT(d);
+ PCM_ACQUIRE(d);
val = d->eqpreamp;
bzero(buf, sizeof(buf));
(void)snprintf(buf, sizeof(buf), "%c%d.%ddB",
FEEDEQ_PREAMP_SIGNMARK(val), FEEDEQ_PREAMP_IPART(val),
FEEDEQ_PREAMP_FPART(val));
- PCM_ACQUIRE(d);
- PCM_UNLOCK(d);
oval = val;
err = sysctl_handle_string(oidp, buf, sizeof(buf), req);
@@ -507,12 +496,10 @@ sysctl_dev_pcm_eq_preamp(SYSCTL_HANDLER_ARGS)
if (err == 0 && req->newptr != NULL) {
val = feed_eq_scan_preamp_arg(buf);
if (val == FEEDEQ_PREAMP_INVALID) {
- PCM_RELEASE_QUICK(d);
+ PCM_RELEASE(d);
return (EINVAL);
}
- PCM_LOCK(d);
-
if (val != oval) {
if (val < FEEDEQ_PREAMP_MIN)
val = FEEDEQ_PREAMP_MIN;
@@ -521,6 +508,7 @@ sysctl_dev_pcm_eq_preamp(SYSCTL_HANDLER_ARGS)
d->eqpreamp = val;
+ PCM_LOCK(d);
CHN_FOREACH(c, d, channels.pcm.busy) {
CHN_LOCK(c);
f = feeder_find(c, FEEDER_EQ);
@@ -528,12 +516,10 @@ sysctl_dev_pcm_eq_preamp(SYSCTL_HANDLER_ARGS)
(void)FEEDER_SET(f, FEEDEQ_PREAMP, val);
CHN_UNLOCK(c);
}
+ PCM_UNLOCK(d);
}
-
- PCM_RELEASE(d);
- PCM_UNLOCK(d);
- } else
- PCM_RELEASE_QUICK(d);
+ }
+ PCM_RELEASE(d);
return (err);
}
diff --git a/sys/dev/sound/pcm/feeder_rate.c b/sys/dev/sound/pcm/feeder_rate.c
index ca7d21665a42..d77c61a201e7 100644
--- a/sys/dev/sound/pcm/feeder_rate.c
+++ b/sys/dev/sound/pcm/feeder_rate.c
@@ -249,8 +249,6 @@ sysctl_hw_snd_feeder_rate_quality(SYSCTL_HANDLER_ARGS)
d = devclass_get_softc(pcm_devclass, i);
if (!PCM_REGISTERED(d))
continue;
- PCM_LOCK(d);
- PCM_WAIT(d);
PCM_ACQUIRE(d);
CHN_FOREACH(c, d, channels.pcm) {
CHN_LOCK(c);
@@ -263,7 +261,6 @@ sysctl_hw_snd_feeder_rate_quality(SYSCTL_HANDLER_ARGS)
CHN_UNLOCK(c);
}
PCM_RELEASE(d);
- PCM_UNLOCK(d);
}
bus_topo_unlock();
diff --git a/sys/dev/sound/pcm/mixer.c b/sys/dev/sound/pcm/mixer.c
index c270d751dff5..aebbf87e53ba 100644
--- a/sys/dev/sound/pcm/mixer.c
+++ b/sys/dev/sound/pcm/mixer.c
@@ -887,11 +887,11 @@ mixer_ioctl(struct cdev *i_dev, unsigned long cmd, caddr_t arg, int mode,
return (EBADF);
PCM_GIANT_ENTER(d);
- PCM_ACQUIRE_QUICK(d);
+ PCM_ACQUIRE(d);
ret = mixer_ioctl_cmd(i_dev, cmd, arg, mode, td);
- PCM_RELEASE_QUICK(d);
+ PCM_RELEASE(d);
PCM_GIANT_LEAVE(d);
return (ret);
diff --git a/sys/dev/sound/pcm/sound.c b/sys/dev/sound/pcm/sound.c
index 6f2d23aed1e7..28c1abd2a91d 100644
--- a/sys/dev/sound/pcm/sound.c
+++ b/sys/dev/sound/pcm/sound.c
@@ -132,17 +132,16 @@ pcm_addchan(device_t dev, int dir, kobj_class_t cls, void *devinfo)
struct pcm_channel *ch;
int err = 0;
- PCM_LOCK(d);
- PCM_WAIT(d);
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_RELEASE(d);
PCM_UNLOCK(d);
+ PCM_RELEASE(d);
return (err);
}
@@ -235,6 +234,9 @@ pcm_setflags(device_t dev, uint32_t val)
{
struct snddev_info *d = device_get_softc(dev);
+ KASSERT(!PCM_REGISTERED(d) || sx_xlocked(&d->sx),
+ ("%s(): unserialized flag write", __func__));
+
d->flags = val;
}
@@ -283,29 +285,21 @@ sysctl_dev_pcm_bitperfect(SYSCTL_HANDLER_ARGS)
if (!PCM_REGISTERED(d))
return (ENODEV);
- PCM_LOCK(d);
- PCM_WAIT(d);
- val = (d->flags & SD_F_BITPERFECT) ? 1 : 0;
PCM_ACQUIRE(d);
- PCM_UNLOCK(d);
+ val = (d->flags & SD_F_BITPERFECT) ? 1 : 0;
err = sysctl_handle_int(oidp, &val, 0, req);
if (err == 0 && req->newptr != NULL) {
if (!(val == 0 || val == 1)) {
- PCM_RELEASE_QUICK(d);
+ PCM_RELEASE(d);
return (EINVAL);
}
- PCM_LOCK(d);
-
d->flags &= ~SD_F_BITPERFECT;
d->flags |= (val != 0) ? SD_F_BITPERFECT : 0;
-
- PCM_RELEASE(d);
- PCM_UNLOCK(d);
- } else
- PCM_RELEASE_QUICK(d);
+ }
+ PCM_RELEASE(d);
return (err);
}
@@ -344,8 +338,8 @@ pcm_init(device_t dev, void *devinfo)
d = device_get_softc(dev);
d->dev = dev;
- mtx_init(&d->lock, device_get_nameunit(dev), "sound cdev", MTX_DEF);
- cv_init(&d->cv, device_get_nameunit(dev));
+ mtx_init(&d->lock, "snddev lock", NULL, MTX_DEF);
+ sx_init(&d->sx, "snddev sx");
i = 0;
if (resource_int_value(device_get_name(dev), device_get_unit(dev),
@@ -462,15 +456,10 @@ pcm_unregister(device_t dev)
return (0);
}
- PCM_LOCK(d);
- PCM_WAIT(d);
- d->flags &= ~SD_F_REGISTERED;
PCM_ACQUIRE(d);
- PCM_UNLOCK(d);
-
+ d->flags &= ~SD_F_REGISTERED;
pcm_shutdownchans(d);
-
- PCM_RELEASE_QUICK(d);
+ PCM_RELEASE(d);
/*
* Destroy the dsp cdev before freeing resources, otherwise threads
@@ -479,9 +468,9 @@ pcm_unregister(device_t dev)
*/
dsp_destroy_dev(dev);
- PCM_ACQUIRE_QUICK(d);
+ PCM_ACQUIRE(d);
pcm_killchans(d);
- PCM_RELEASE_QUICK(d);
+ PCM_RELEASE(d);
if (d->play_sysctl_tree != NULL) {
sysctl_ctx_free(&d->play_sysctl_ctx);
@@ -496,7 +485,7 @@ pcm_unregister(device_t dev)
if (d->mixer != NULL)
mixer_uninit(dev);
- cv_destroy(&d->cv);
+ sx_destroy(&d->sx);
mtx_destroy(&d->lock);
d->dev = NULL;
diff --git a/sys/dev/sound/pcm/sound.h b/sys/dev/sound/pcm/sound.h
index 16110e46a152..5268f964d6f3 100644
--- a/sys/dev/sound/pcm/sound.h
+++ b/sys/dev/sound/pcm/sound.h
@@ -74,6 +74,7 @@
#include <sys/lock.h>
#include <sys/mutex.h>
#include <sys/condvar.h>
+#include <sys/sx.h>
struct pcm_channel;
struct pcm_feeder;
@@ -94,7 +95,7 @@ struct snd_mixer;
#define SD_F_SIMPLEX 0x00000001
/* unused 0x00000002 */
#define SD_F_SOFTPCMVOL 0x00000004
-#define SD_F_BUSY 0x00000008
+/* unused 0x00000008 */
#define SD_F_MPSAFE 0x00000010
#define SD_F_REGISTERED 0x00000020
#define SD_F_BITPERFECT 0x00000040
@@ -110,7 +111,7 @@ struct snd_mixer;
"\001SIMPLEX" \
/* "\002 */ \
"\003SOFTPCMVOL" \
- "\004BUSY" \
+ /* "\004 */ \
"\005MPSAFE" \
"\006REGISTERED" \
"\007BITPERFECT" \
@@ -177,6 +178,15 @@ struct snddev_info {
device_t dev;
char status[SND_STATUSLEN];
struct mtx lock;
+ /*
+ * Unlike the mutex, this can be held across sleeps, in order to guard
+ * long operations such as opening, closing and reconfiguring. It also
+ * makes sure pcm_unregister() blocks until the operation finishes
+ * before it tears down the channels. Drivers may change the flags
+ * without it while attaching, since the device is not yet visible to
+ * userland.
+ */
+ struct sx sx;
struct cdev *dsp_dev;
struct snd_mixer *mixer;
uint32_t pvchanrate, pvchanformat, pvchanmode;
@@ -184,7 +194,6 @@ struct snddev_info {
int32_t eqpreamp;
struct sysctl_ctx_list play_sysctl_ctx, rec_sysctl_ctx;
struct sysctl_oid *play_sysctl_tree, *rec_sysctl_tree;
- struct cv cv;
struct unrhdr *p_unr;
struct unrhdr *vp_unr;
struct unrhdr *r_unr;
@@ -205,54 +214,13 @@ int sound_oss_card_info(oss_card_info *);
#define PCM_LOCKASSERT(d) mtx_assert(&(d)->lock, MA_OWNED)
#define PCM_UNLOCKASSERT(d) mtx_assert(&(d)->lock, MA_NOTOWNED)
-/*
- * For PCM_[WAIT | ACQUIRE | RELEASE], be sure to surround these
- * with PCM_LOCK/UNLOCK() sequence, or I'll come to gnaw upon you!
- */
-#define PCM_WAIT(x) do { \
- PCM_LOCKASSERT(x); \
- while ((x)->flags & SD_F_BUSY) \
- cv_wait(&(x)->cv, &(x)->lock); \
-} while (0)
-
-#define PCM_ACQUIRE(x) do { \
- PCM_LOCKASSERT(x); \
- KASSERT(!((x)->flags & SD_F_BUSY), \
- ("%s(%d): [PCM ACQUIRE] Trying to acquire BUSY cv!", \
- __func__, __LINE__)); \
- (x)->flags |= SD_F_BUSY; \
-} while (0)
-
-#define PCM_RELEASE(x) do { \
- PCM_LOCKASSERT(x); \
- KASSERT((x)->flags & SD_F_BUSY, \
- ("%s(%d): [PCM RELEASE] Releasing non-BUSY cv!", \
- __func__, __LINE__)); \
- (x)->flags &= ~SD_F_BUSY; \
- cv_broadcast(&(x)->cv); \
-} while (0)
-
-/* Quick version, for shorter path. */
-#define PCM_ACQUIRE_QUICK(x) do { \
- PCM_UNLOCKASSERT(x); \
- PCM_LOCK(x); \
- PCM_WAIT(x); \
- PCM_ACQUIRE(x); \
- PCM_UNLOCK(x); \
-} while (0)
-
-#define PCM_RELEASE_QUICK(x) do { \
- PCM_UNLOCKASSERT(x); \
- PCM_LOCK(x); \
- PCM_RELEASE(x); \
- PCM_UNLOCK(x); \
+#define PCM_ACQUIRE(d) do { \
+ PCM_UNLOCKASSERT(d); \
+ sx_xlock(&(d)->sx); \
} while (0)
-#define PCM_BUSYASSERT(x) KASSERT(x != NULL && \
- ((x)->flags & SD_F_BUSY), \
- ("%s(%d): [PCM BUSYASSERT] " \
- "Failed, snddev_info=%p", \
- __func__, __LINE__, x))
+#define PCM_RELEASE(d) sx_xunlock(&(d)->sx)
+#define PCM_BUSYASSERT(d) sx_assert(&(d)->sx, SA_XLOCKED)
#define PCM_GIANT_ENTER(x) do { \
int _pcm_giant = 0; \
diff --git a/sys/dev/sound/pcm/vchan.c b/sys/dev/sound/pcm/vchan.c
index 78528ab78e55..47f32001fb01 100644
--- a/sys/dev/sound/pcm/vchan.c
+++ b/sys/dev/sound/pcm/vchan.c
@@ -260,38 +260,34 @@ sysctl_dev_pcm_vchans(SYSCTL_HANDLER_ARGS)
}
bus_topo_unlock();
- PCM_LOCK(d);
- PCM_WAIT(d);
+ PCM_ACQUIRE(d);
switch (VCHAN_SYSCTL_DIR(oidp->oid_arg1)) {
case VCHAN_PLAY:
/* Exit if we do not support this direction. */
if (d->playcount < 1) {
- PCM_UNLOCK(d);
+ PCM_RELEASE(d);
return (ENODEV);
}
flag = SD_F_PVCHANS;
break;
case VCHAN_REC:
if (d->reccount < 1) {
- PCM_UNLOCK(d);
+ PCM_RELEASE(d);
return (ENODEV);
}
flag = SD_F_RVCHANS;
break;
default:
- PCM_UNLOCK(d);
+ PCM_RELEASE(d);
*** 283 LINES SKIPPED ***