diff --git a/sys/dev/sound/pcm/buffer.c b/sys/dev/sound/pcm/buffer.c index 14c035281db5..2b3021480700 100644 --- a/sys/dev/sound/pcm/buffer.c +++ b/sys/dev/sound/pcm/buffer.c @@ -90,6 +90,24 @@ sndbuf_rele(struct snd_dbuf *b) } } +static bool +sndbuf_busy(struct snd_dbuf *b) +{ + CHN_LOCKASSERT(b->channel); + + return (refcount_load(&b->refcount) > 1 || + (b->flags & SNDBUF_F_DETACHED) != 0 || + /* + * XXX-MJ CHN_F_MMAP is not reliable, but for software buffers the + * refcount accurately reflects whether the buffer is mapped. + * Ensure that there is no in-flight DMA transfer by checking + * CHN_F_TRIGGERED. This should really be checked at a higher + * layer, but this is tricky since chn_resizebuf() and + * sndbuf_resize() drop and reacquire the channel lock. + */ + (b->channel->flags & (CHN_F_MMAP | CHN_F_TRIGGERED)) != 0); +} + static void sndbuf_setmap(void *arg, bus_dma_segment_t *segs, int nseg, int error) { @@ -205,8 +223,7 @@ sndbuf_resize(struct snd_dbuf *b, unsigned int blkcnt, unsigned int blksz) if (bufsize > b->allocsize || bufsize < (b->allocsize >> SNDBUF_CACHE_SHIFT)) { - if (refcount_load(&b->refcount) > 1 || - (b->flags & SNDBUF_F_DETACHED) != 0) { + if (sndbuf_busy(b)) { CHN_UNLOCK(b->channel); return (EBUSY); } @@ -214,6 +231,11 @@ sndbuf_resize(struct snd_dbuf *b, unsigned int blkcnt, unsigned int blksz) CHN_UNLOCK(b->channel); tmpbuf = malloc(allocsize, M_DEVBUF, M_WAITOK); CHN_LOCK(b->channel); + if (sndbuf_busy(b)) { + CHN_UNLOCK(b->channel); + free(tmpbuf, M_DEVBUF); + return (EBUSY); + } if (snd_verbose > 3) printf("%s(): b=%p %p -> %p [%d -> %d : %d]\n", __func__, b, b->tmpbuf, tmpbuf, @@ -250,8 +272,7 @@ sndbuf_remalloc(struct snd_dbuf *b, unsigned int blkcnt, unsigned int blksz) if (bufsize > b->allocsize || bufsize < (b->allocsize >> SNDBUF_CACHE_SHIFT)) { - if (refcount_load(&b->refcount) > 1 || - (b->flags & SNDBUF_F_DETACHED) != 0) + if (sndbuf_busy(b)) return (EBUSY); allocsize = round_page(bufsize); CHN_UNLOCK(b->channel); @@ -259,6 +280,12 @@ sndbuf_remalloc(struct snd_dbuf *b, unsigned int blkcnt, unsigned int blksz) tmpbuf = malloc(allocsize, M_DEVBUF, M_WAITOK); shadbuf = malloc(allocsize, M_DEVBUF, M_WAITOK); CHN_LOCK(b->channel); + if (sndbuf_busy(b)) { + free(buf, M_DEVBUF); + free(tmpbuf, M_DEVBUF); + free(shadbuf, M_DEVBUF); + return (EBUSY); + } free(b->buf, M_DEVBUF); b->buf = buf; free(b->tmpbuf, M_DEVBUF); diff --git a/sys/dev/sound/pcm/channel.c b/sys/dev/sound/pcm/channel.c index 24877c319b35..c79761ccc22e 100644 --- a/sys/dev/sound/pcm/channel.c +++ b/sys/dev/sound/pcm/channel.c @@ -1841,8 +1841,9 @@ chn_resizebuf(struct pcm_channel *c, int latency, CHN_LOCKASSERT(c); - if ((c->flags & (CHN_F_MMAP | CHN_F_TRIGGERED)) || - !(c->direction == PCMDIR_PLAY || c->direction == PCMDIR_REC)) + if (!(c->direction == PCMDIR_PLAY || c->direction == PCMDIR_REC)) + return EINVAL; + if ((c->flags & (CHN_F_MMAP | CHN_F_TRIGGERED)) != 0) return EINVAL; if (latency == -1) { @@ -1900,6 +1901,9 @@ chn_resizebuf(struct pcm_channel *c, int latency, chn_notify(c->parentchannel, CHN_N_BLOCKSIZE); CHN_UNLOCK(c->parentchannel); CHN_LOCK(c); + /* Revalidate. */ + if ((c->flags & (CHN_F_MMAP | CHN_F_TRIGGERED)) != 0) + return EINVAL; if (c->direction == PCMDIR_PLAY) { limit = (pb != NULL) ? sndbuf_xbytes(pb->bufsize, pb, bs) : 0; @@ -1965,6 +1969,9 @@ chn_resizebuf(struct pcm_channel *c, int latency, b->blksz = nblksz; } CHN_LOCK(c); + /* Revalidate. */ + if ((c->flags & (CHN_F_MMAP | CHN_F_TRIGGERED)) != 0) + return EINVAL; if (!CHN_EMPTY(c, children)) { nsblksz = round_blksz( diff --git a/tests/sys/sound/mmap.c b/tests/sys/sound/mmap.c index 6ebfd2f7dec5..95ed9a483274 100644 --- a/tests/sys/sound/mmap.c +++ b/tests/sys/sound/mmap.c @@ -129,11 +129,69 @@ ATF_TC_BODY(mmap_reject_prot_exec, tc) close(fd); } +/* + * Verify that an attempt to resize a channel's buffer while it is mmapped + * fails. + */ +ATF_TC(mmap_resize_fails); +ATF_TC_HEAD(mmap_resize_fails, tc) +{ + atf_tc_set_md_var(tc, "descr", + "Resizing a mmapped channel buffer fails"); + atf_tc_set_md_var(tc, "require.kmods", "snd_dummy"); +} +ATF_TC_BODY(mmap_resize_fails, tc) +{ + audio_buf_info abi; + uint8_t *buf; + size_t len; + int fd, arg, ret; + + fd = open("/dev/dsp.dummy", O_RDWR); + ATF_REQUIRE_MSG(fd >= 0, FMT_ERR("open")); + + arg = (2 << 16) | 14; /* 2 * 16KB */ + ATF_REQUIRE_MSG(ioctl(fd, SNDCTL_DSP_SETFRAGMENT, &arg) == 0, + FMT_ERR("SNDCTL_DSP_SETFRAGMENT")); + ATF_REQUIRE_MSG(ioctl(fd, SNDCTL_DSP_GETOSPACE, &abi) == 0, + FMT_ERR("SNDCTL_DSP_GETOSPACE")); + + len = abi.fragstotal * abi.fragsize; + ATF_REQUIRE_MSG(len >= PAGE_SIZE, "buffer too small: %zu", len); + + buf = mmap(NULL, len, PROT_WRITE, MAP_SHARED, fd, 0); + ATF_REQUIRE_MSG(buf != MAP_FAILED, FMT_ERR("mmap")); + + /* Try to resize the buffer; this should fail. */ + arg = (4 << 16) | 12; /* 4 * 4KB */ + ret = ioctl(fd, SNDCTL_DSP_SETFRAGMENT, &arg); + ATF_REQUIRE_MSG(ret != 0, "resize of mmapped buffer should fail"); + ATF_REQUIRE_MSG(errno == EINVAL, + "expected EINVAL, got %s", strerror(errno)); + + ATF_REQUIRE(munmap(buf, len) == 0); + + /* + * CHN_F_MMAP is only cleared when the fd is closed, so resizing still + * fails after munmap. This is probably a bug, but we can at least + * verify that the resize succeeds after the fd is closed and reopened. + */ + ATF_REQUIRE(close(fd) == 0); + fd = open("/dev/dsp.dummy", O_RDWR); + ATF_REQUIRE_MSG(fd >= 0, FMT_ERR("open")); + arg = (4 << 16) | 12; /* 4 * 4KB */ + ATF_REQUIRE_MSG(ioctl(fd, SNDCTL_DSP_SETFRAGMENT, &arg) == 0, + FMT_ERR("SNDCTL_DSP_SETFRAGMENT after reopen")); + + ATF_REQUIRE(close(fd) == 0); +} + ATF_TP_ADD_TCS(tp) { ATF_TP_ADD_TC(tp, mmap_offset_overflow); ATF_TP_ADD_TC(tp, mmap_buffer_lifetime); ATF_TP_ADD_TC(tp, mmap_reject_prot_exec); + ATF_TP_ADD_TC(tp, mmap_resize_fails); return (atf_no_error()); }