summaryrefslogtreecommitdiffstats
path: root/sys/dev/sound/pcm/vchan.c
diff options
context:
space:
mode:
authortruckman <truckman@FreeBSD.org>2004-01-28 08:02:15 +0000
committertruckman <truckman@FreeBSD.org>2004-01-28 08:02:15 +0000
commit860115ff61133d494820140aaffe13b6bd0d0256 (patch)
treea74abb2113dfe2f0c468f9e6bf902217071b53f1 /sys/dev/sound/pcm/vchan.c
parent0004b4717fe5160dc86420ca1eca3b19178d4fd7 (diff)
downloadFreeBSD-src-860115ff61133d494820140aaffe13b6bd0d0256.zip
FreeBSD-src-860115ff61133d494820140aaffe13b6bd0d0256.tar.gz
Change KASSERT() in feed_vchan16() into an explicit test and call to
panic() so that the buffer overflow just beyond this point is always caught, even when the code is not compiled with INVARIANTS. Change chn_setblocksize() buffer reallocation code to attempt to avoid the feed_vchan16() buffer overflow by attempting to always keep the bufsoft buffer at least as large as the bufhard buffer. Print a diagnositic message Danger! %s bufsoft size increasing from %d to %d after CHANNEL_SETBLOCKSIZE() if our best attempts fail. If feed_vchan16() were to be called by the interrupt handler while locks are dropped in chn_setblocksize() to increase the size bufsoft to match the size of bufhard, the panic() code in feed_vchan16() will be triggered. If the diagnostic message is printed, it is a warning that a panic is possible if the system were to see events in an "unlucky" order. Change the locking code to avoid the need for MTX_RECURSIVE mutexes. Add the MTX_DUPOK option to the channel mutexes and change the locking sequence to always lock the parent channel before its children to avoid the possibility of deadlock. Actually implement locking assertions for the channel mutexes and fix the problems found by the resulting assertion violations. Clean up the locking code in dsp_ioctl(). Allocate the channel buffers using the malloc() M_WAITOK option instead of M_NOWAIT so that buffer allocation won't fail. Drop locks across the malloc() calls. Add/modify KASSERTS() in attempt to detect problems early. Abuse layering by adding a pointer to the snd_dbuf structure that points back to the pcm_channel that owns it. This allows sndbuf_resize() to do proper locking without having to change the its API, which is used by the hardware drivers. Don't dereference a NULL pointer when setting hw.snd.maxautovchans if a hardware driver is not loaded. Noticed by Ryan Sommers <ryans at gamersimpact.com>. Tested by: Stefan Ehmann <shoesoft AT gmx.net> Tested by: matk (Mathew Kanner) Tested by: Gordon Bergling <gbergling AT 0xfce3.net>
Diffstat (limited to 'sys/dev/sound/pcm/vchan.c')
-rw-r--r--sys/dev/sound/pcm/vchan.c29
1 files changed, 25 insertions, 4 deletions
diff --git a/sys/dev/sound/pcm/vchan.c b/sys/dev/sound/pcm/vchan.c
index 0c5867b..ce67fb1 100644
--- a/sys/dev/sound/pcm/vchan.c
+++ b/sys/dev/sound/pcm/vchan.c
@@ -77,7 +77,9 @@ feed_vchan_s16(struct pcm_feeder *f, struct pcm_channel *c, u_int8_t *b, u_int32
int16_t *tmp, *dst;
unsigned int cnt;
- KASSERT(sndbuf_getsize(src) >= count, ("bad bufsize"));
+ if (sndbuf_getsize(src) < count)
+ panic("feed_vchan_s16(%s): tmp buffer size %d < count %d, flags = 0x%x",
+ c->name, sndbuf_getsize(src), count, c->flags);
count &= ~1;
bzero(b, count);
@@ -92,12 +94,14 @@ feed_vchan_s16(struct pcm_feeder *f, struct pcm_channel *c, u_int8_t *b, u_int32
bzero(tmp, count);
SLIST_FOREACH(cce, &c->children, link) {
ch = cce->channel;
+ CHN_LOCK(ch);
if (ch->flags & CHN_F_TRIGGERED) {
if (ch->flags & CHN_F_MAPPED)
sndbuf_acquire(ch->bufsoft, NULL, sndbuf_getfree(ch->bufsoft));
cnt = FEEDER_FEED(ch->feeder, ch, (u_int8_t *)tmp, count, ch->bufsoft);
vchan_mix_s16(dst, tmp, cnt / 2);
}
+ CHN_UNLOCK(ch);
}
return count;
@@ -145,13 +149,16 @@ vchan_setformat(kobj_t obj, void *data, u_int32_t format)
{
struct vchinfo *ch = data;
struct pcm_channel *parent = ch->parent;
+ struct pcm_channel *channel = ch->channel;
ch->fmt = format;
ch->bps = 1;
ch->bps <<= (ch->fmt & AFMT_STEREO)? 1 : 0;
ch->bps <<= (ch->fmt & AFMT_16BIT)? 1 : 0;
ch->bps <<= (ch->fmt & AFMT_32BIT)? 2 : 0;
+ CHN_UNLOCK(channel);
chn_notify(parent, CHN_N_FORMAT);
+ CHN_LOCK(channel);
return 0;
}
@@ -160,9 +167,12 @@ vchan_setspeed(kobj_t obj, void *data, u_int32_t speed)
{
struct vchinfo *ch = data;
struct pcm_channel *parent = ch->parent;
+ struct pcm_channel *channel = ch->channel;
ch->spd = speed;
+ CHN_UNLOCK(channel);
chn_notify(parent, CHN_N_RATE);
+ CHN_LOCK(channel);
return speed;
}
@@ -171,14 +181,19 @@ vchan_setblocksize(kobj_t obj, void *data, u_int32_t blocksize)
{
struct vchinfo *ch = data;
struct pcm_channel *parent = ch->parent;
+ /* struct pcm_channel *channel = ch->channel; */
int prate, crate;
ch->blksz = blocksize;
+ /* CHN_UNLOCK(channel); */
chn_notify(parent, CHN_N_BLOCKSIZE);
+ CHN_LOCK(parent);
+ /* CHN_LOCK(channel); */
crate = ch->spd * ch->bps;
prate = sndbuf_getspd(parent->bufhard) * sndbuf_getbps(parent->bufhard);
blocksize = sndbuf_getblksz(parent->bufhard);
+ CHN_UNLOCK(parent);
blocksize *= prate;
blocksize /= crate;
@@ -190,12 +205,15 @@ vchan_trigger(kobj_t obj, void *data, int go)
{
struct vchinfo *ch = data;
struct pcm_channel *parent = ch->parent;
+ struct pcm_channel *channel = ch->channel;
if (go == PCMTRIG_EMLDMAWR || go == PCMTRIG_EMLDMARD)
return 0;
ch->run = (go == PCMTRIG_START)? 1 : 0;
+ CHN_UNLOCK(channel);
chn_notify(parent, CHN_N_TRIGGER);
+ CHN_LOCK(channel);
return 0;
}
@@ -235,8 +253,11 @@ vchan_create(struct pcm_channel *parent)
struct pcm_channel *child;
int err, first;
+ CHN_UNLOCK(parent);
+
pce = malloc(sizeof(*pce), M_DEVBUF, M_WAITOK | M_ZERO);
if (!pce) {
+ CHN_LOCK(parent);
return ENOMEM;
}
@@ -244,14 +265,13 @@ vchan_create(struct pcm_channel *parent)
child = pcm_chn_create(d, parent, &vchan_class, PCMDIR_VIRTUAL, parent);
if (!child) {
free(pce, M_DEVBUF);
+ CHN_LOCK(parent);
return ENODEV;
}
CHN_LOCK(parent);
- if (!(parent->flags & CHN_F_BUSY)) {
- CHN_UNLOCK(parent);
+ if (!(parent->flags & CHN_F_BUSY))
return EBUSY;
- }
first = SLIST_EMPTY(&parent->children);
/* add us to our parent channel's children */
@@ -269,6 +289,7 @@ vchan_create(struct pcm_channel *parent)
free(pce, M_DEVBUF);
}
+ CHN_LOCK(parent);
/* XXX gross ugly hack, murder death kill */
if (first && !err) {
err = chn_reset(parent, AFMT_STEREO | AFMT_S16_LE);
OpenPOWER on IntegriCloud