From: Alexander Mikhalitsyn <alexander@mihalicyn.com>
To: qemu-devel@nongnu.org
Cc: "Manos Pitsidianakis" <manos.pitsidianakis@linaro.org>,
"Volker Rümelin" <vr_qemu@t-online.de>,
"Marc-André Lureau" <marcandre.lureau@redhat.com>,
"Gerd Hoffmann" <kraxel@redhat.com>,
"Daniel P . Berrangé" <berrange@redhat.com>,
"Alexander Mikhalitsyn" <alexander@mihalicyn.com>,
"Michael S. Tsirkin" <mst@redhat.com>,
"Stéphane Graber" <stgraber@stgraber.org>,
"Alexander Mikhalitsyn" <aleksandr.mikhalitsyn@futurfusion.io>
Subject: [PATCH v5 03/10] hw/audio/virtio-sound: remove command and stream mutexes
Date: Mon, 3 Aug 2026 10:11:52 +0200 [thread overview]
Message-ID: <20260803081159.91981-4-alexander@mihalicyn.com> (raw)
In-Reply-To: <20260803081159.91981-1-alexander@mihalicyn.com>
From: Volker Rümelin <vr_qemu@t-online.de>
All code in virtio-snd.c runs with the BQL held. Remove the
command queue mutex and the stream queue mutexes. The qatomic
functions are also not needed.
Signed-off-by: Volker Rümelin <vr_qemu@t-online.de>
[AM: there were too many conflicts, I did `git checkout --ours -- <.>`
and then reimplemented the patch idea
/AM]
Signed-off-by: Alexander Mikhalitsyn <aleksandr.mikhalitsyn@futurfusion.io>
Reviewed-by: Marc-André Lureau <marcandre.lureau@redhat.com>
Reviewed-by: Manos Pitsidianakis <manos.pitsidianakis@linaro.org>
---
hw/audio/virtio-snd.c | 246 +++++++++++++++-------------------
include/hw/audio/virtio-snd.h | 3 -
2 files changed, 110 insertions(+), 139 deletions(-)
diff --git a/hw/audio/virtio-snd.c b/hw/audio/virtio-snd.c
index 46d81cc9ec4..cbf739f8a7d 100644
--- a/hw/audio/virtio-snd.c
+++ b/hw/audio/virtio-snd.c
@@ -19,7 +19,6 @@
#include "qemu/iov.h"
#include "qemu/log.h"
#include "qemu/error-report.h"
-#include "qemu/lockable.h"
#include "system/runstate.h"
#include "trace.h"
#include "qapi/error.h"
@@ -442,7 +441,6 @@ static uint32_t virtio_snd_pcm_prepare(VirtIOSound *s, uint32_t stream_id)
stream->id = stream_id;
stream->s = s;
stream->latency_bytes = 0;
- qemu_mutex_init(&stream->queue_mutex);
QSIMPLEQ_INIT(&stream->queue);
/*
@@ -566,9 +564,7 @@ static void virtio_snd_handle_pcm_start_stop(VirtIOSound *s,
stream = virtio_snd_pcm_get_stream(s, stream_id);
if (stream) {
- WITH_QEMU_LOCK_GUARD(&stream->queue_mutex) {
- stream->active = start;
- }
+ stream->active = start;
if (stream->info.direction == VIRTIO_SND_D_OUTPUT) {
audio_be_set_active_out(s->audio_be, stream->voice.out, start);
} else {
@@ -592,10 +588,8 @@ static size_t virtio_snd_pcm_get_io_msgs_count(VirtIOSoundPCMStream *stream)
VirtIOSoundPCMBuffer *buffer, *next;
size_t count = 0;
- WITH_QEMU_LOCK_GUARD(&stream->queue_mutex) {
- QSIMPLEQ_FOREACH_SAFE(buffer, &stream->queue, entry, next) {
- count += 1;
- }
+ QSIMPLEQ_FOREACH_SAFE(buffer, &stream->queue, entry, next) {
+ count += 1;
}
return count;
}
@@ -737,23 +731,15 @@ static void virtio_snd_process_cmdq(VirtIOSound *s)
{
virtio_snd_ctrl_command *cmd;
- if (unlikely(qatomic_read(&s->processing_cmdq))) {
- return;
- }
-
- WITH_QEMU_LOCK_GUARD(&s->cmdq_mutex) {
- qatomic_set(&s->processing_cmdq, true);
- while (!QTAILQ_EMPTY(&s->cmdq)) {
- cmd = QTAILQ_FIRST(&s->cmdq);
+ while (!QTAILQ_EMPTY(&s->cmdq)) {
+ cmd = QTAILQ_FIRST(&s->cmdq);
- /* process command */
- process_cmd(s, cmd);
+ /* process command */
+ process_cmd(s, cmd);
- QTAILQ_REMOVE(&s->cmdq, cmd, next);
+ QTAILQ_REMOVE(&s->cmdq, cmd, next);
- virtio_snd_ctrl_cmd_free(cmd);
- }
- qatomic_set(&s->processing_cmdq, false);
+ virtio_snd_ctrl_cmd_free(cmd);
}
}
@@ -896,17 +882,16 @@ static void virtio_snd_handle_tx_xfer(VirtIODevice *vdev, VirtQueue *vq)
if (!g_size_checked_add(&tmp, sizeof(VirtIOSoundPCMBuffer), size)) {
goto tx_err;
}
- WITH_QEMU_LOCK_GUARD(&stream->queue_mutex) {
- buffer = g_malloc0(sizeof(VirtIOSoundPCMBuffer) + size);
- buffer->elem = elem;
- buffer->populated = false;
- buffer->vq = vq;
- buffer->size = size;
- buffer->offset = 0;
- stream->latency_bytes += size;
-
- QSIMPLEQ_INSERT_TAIL(&stream->queue, buffer, entry);
- }
+
+ buffer = g_malloc0(sizeof(VirtIOSoundPCMBuffer) + size);
+ buffer->elem = elem;
+ buffer->populated = false;
+ buffer->vq = vq;
+ buffer->size = size;
+ buffer->offset = 0;
+ stream->latency_bytes += size;
+
+ QSIMPLEQ_INSERT_TAIL(&stream->queue, buffer, entry);
continue;
tx_err:
@@ -983,14 +968,14 @@ static void virtio_snd_handle_rx_xfer(VirtIODevice *vdev, VirtQueue *vq)
if (!g_size_checked_add(&tmp, sizeof(VirtIOSoundPCMBuffer), size)) {
goto rx_err;
}
- WITH_QEMU_LOCK_GUARD(&stream->queue_mutex) {
- buffer = g_malloc0(sizeof(VirtIOSoundPCMBuffer) + size);
- buffer->elem = elem;
- buffer->vq = vq;
- buffer->size = 0;
- buffer->offset = 0;
- QSIMPLEQ_INSERT_TAIL(&stream->queue, buffer, entry);
- }
+
+ buffer = g_malloc0(sizeof(VirtIOSoundPCMBuffer) + size);
+ buffer->elem = elem;
+ buffer->vq = vq;
+ buffer->size = 0;
+ buffer->offset = 0;
+ QSIMPLEQ_INSERT_TAIL(&stream->queue, buffer, entry);
+
continue;
rx_err:
@@ -1094,7 +1079,6 @@ static void virtio_snd_realize(DeviceState *dev, Error **errp)
virtio_add_queue(vdev, 64, virtio_snd_handle_tx_xfer);
vsnd->queues[VIRTIO_SND_VQ_RX] =
virtio_add_queue(vdev, 64, virtio_snd_handle_rx_xfer);
- qemu_mutex_init(&vsnd->cmdq_mutex);
QTAILQ_INIT(&vsnd->cmdq);
QSIMPLEQ_INIT(&vsnd->invalid);
@@ -1162,52 +1146,50 @@ static void virtio_snd_pcm_out_cb(void *data, int available)
VirtIOSoundPCMBuffer *buffer;
size_t size;
- WITH_QEMU_LOCK_GUARD(&stream->queue_mutex) {
- while (!QSIMPLEQ_EMPTY(&stream->queue)) {
- buffer = QSIMPLEQ_FIRST(&stream->queue);
- if (!virtio_queue_ready(buffer->vq)) {
- return;
+ while (!QSIMPLEQ_EMPTY(&stream->queue)) {
+ buffer = QSIMPLEQ_FIRST(&stream->queue);
+ if (!virtio_queue_ready(buffer->vq)) {
+ return;
+ }
+ if (!stream->active) {
+ /* Stream has stopped, so do not perform audio_be_write. */
+ return_tx_buffer(stream, buffer);
+ continue;
+ }
+ if (!buffer->populated) {
+ iov_to_buf(buffer->elem->out_sg,
+ buffer->elem->out_num,
+ sizeof(virtio_snd_pcm_xfer),
+ buffer->data,
+ buffer->size);
+ buffer->populated = true;
+ }
+ for (;;) {
+ size = audio_be_write(stream->s->audio_be,
+ stream->voice.out,
+ buffer->data + buffer->offset,
+ MIN(buffer->size, available));
+ assert(size <= MIN(buffer->size, available));
+ if (size == 0) {
+ /* break out of both loops */
+ available = 0;
+ break;
}
- if (!stream->active) {
- /* Stream has stopped, so do not perform audio_be_write. */
+ buffer->size -= size;
+ buffer->offset += size;
+ available -= size;
+ update_latency(stream, size);
+ if (buffer->size < 1) {
return_tx_buffer(stream, buffer);
- continue;
- }
- if (!buffer->populated) {
- iov_to_buf(buffer->elem->out_sg,
- buffer->elem->out_num,
- sizeof(virtio_snd_pcm_xfer),
- buffer->data,
- buffer->size);
- buffer->populated = true;
- }
- for (;;) {
- size = audio_be_write(stream->s->audio_be,
- stream->voice.out,
- buffer->data + buffer->offset,
- MIN(buffer->size, available));
- assert(size <= MIN(buffer->size, available));
- if (size == 0) {
- /* break out of both loops */
- available = 0;
- break;
- }
- buffer->size -= size;
- buffer->offset += size;
- available -= size;
- update_latency(stream, size);
- if (buffer->size < 1) {
- return_tx_buffer(stream, buffer);
- break;
- }
- if (!available) {
- break;
- }
+ break;
}
if (!available) {
break;
}
}
+ if (!available) {
+ break;
+ }
}
}
@@ -1258,55 +1240,53 @@ static void virtio_snd_pcm_in_cb(void *data, int available)
VirtIOSoundPCMBuffer *buffer;
size_t size, max_size, to_read;
- WITH_QEMU_LOCK_GUARD(&stream->queue_mutex) {
- while (!QSIMPLEQ_EMPTY(&stream->queue)) {
- buffer = QSIMPLEQ_FIRST(&stream->queue);
- if (!virtio_queue_ready(buffer->vq)) {
- return;
- }
- if (!stream->active) {
- /* Stream has stopped, so do not perform audio_be_read. */
- return_rx_buffer(stream, buffer);
- continue;
- }
+ while (!QSIMPLEQ_EMPTY(&stream->queue)) {
+ buffer = QSIMPLEQ_FIRST(&stream->queue);
+ if (!virtio_queue_ready(buffer->vq)) {
+ return;
+ }
+ if (!stream->active) {
+ /* Stream has stopped, so do not perform audio_be_read. */
+ return_rx_buffer(stream, buffer);
+ continue;
+ }
- max_size = iov_size(buffer->elem->in_sg, buffer->elem->in_num);
- if (max_size <= sizeof(virtio_snd_pcm_status)) {
+ max_size = iov_size(buffer->elem->in_sg, buffer->elem->in_num);
+ if (max_size <= sizeof(virtio_snd_pcm_status)) {
+ return_rx_buffer(stream, buffer);
+ continue;
+ }
+ max_size -= sizeof(virtio_snd_pcm_status);
+
+ for (;;) {
+ if (buffer->size >= max_size) {
return_rx_buffer(stream, buffer);
- continue;
+ break;
}
- max_size -= sizeof(virtio_snd_pcm_status);
-
- for (;;) {
- if (buffer->size >= max_size) {
- return_rx_buffer(stream, buffer);
- break;
- }
- to_read = stream->params.period_bytes - buffer->size;
- to_read = MIN(to_read, available);
- to_read = MIN(to_read, max_size - buffer->size);
- size = audio_be_read(stream->s->audio_be,
- stream->voice.in,
- buffer->data + buffer->size,
- to_read);
- if (!size) {
- available = 0;
- break;
- }
- buffer->size += size;
- available -= size;
- if (buffer->size >= stream->params.period_bytes) {
- return_rx_buffer(stream, buffer);
- break;
- }
- if (!available) {
- break;
- }
+ to_read = stream->params.period_bytes - buffer->size;
+ to_read = MIN(to_read, available);
+ to_read = MIN(to_read, max_size - buffer->size);
+ size = audio_be_read(stream->s->audio_be,
+ stream->voice.in,
+ buffer->data + buffer->size,
+ to_read);
+ if (!size) {
+ available = 0;
+ break;
+ }
+ buffer->size += size;
+ available -= size;
+ if (buffer->size >= stream->params.period_bytes) {
+ return_rx_buffer(stream, buffer);
+ break;
}
if (!available) {
break;
}
}
+ if (!available) {
+ break;
+ }
}
}
@@ -1323,11 +1303,9 @@ static inline void virtio_snd_pcm_flush(VirtIOSoundPCMStream *stream)
(stream->info.direction == VIRTIO_SND_D_OUTPUT) ? return_tx_buffer :
return_rx_buffer;
- WITH_QEMU_LOCK_GUARD(&stream->queue_mutex) {
- while (!QSIMPLEQ_EMPTY(&stream->queue)) {
- buffer = QSIMPLEQ_FIRST(&stream->queue);
- cb(stream, buffer);
- }
+ while (!QSIMPLEQ_EMPTY(&stream->queue)) {
+ buffer = QSIMPLEQ_FIRST(&stream->queue);
+ cb(stream, buffer);
}
}
@@ -1346,14 +1324,12 @@ static void virtio_snd_unrealize(DeviceState *dev)
if (stream) {
virtio_snd_process_cmdq(stream->s);
virtio_snd_pcm_close(stream);
- qemu_mutex_destroy(&stream->queue_mutex);
g_free(stream);
}
}
g_free(vsnd->pcm.streams);
}
g_free(vsnd->pcm.pcm_params);
- qemu_mutex_destroy(&vsnd->cmdq_mutex);
virtio_delete_queue(vsnd->queues[VIRTIO_SND_VQ_CONTROL]);
virtio_delete_queue(vsnd->queues[VIRTIO_SND_VQ_EVENT]);
virtio_delete_queue(vsnd->queues[VIRTIO_SND_VQ_TX]);
@@ -1374,12 +1350,10 @@ static void virtio_snd_reset(VirtIODevice *vdev)
*/
g_assert(QSIMPLEQ_EMPTY(&vsnd->invalid));
- WITH_QEMU_LOCK_GUARD(&vsnd->cmdq_mutex) {
- while (!QTAILQ_EMPTY(&vsnd->cmdq)) {
- cmd = QTAILQ_FIRST(&vsnd->cmdq);
- QTAILQ_REMOVE(&vsnd->cmdq, cmd, next);
- virtio_snd_ctrl_cmd_free(cmd);
- }
+ while (!QTAILQ_EMPTY(&vsnd->cmdq)) {
+ cmd = QTAILQ_FIRST(&vsnd->cmdq);
+ QTAILQ_REMOVE(&vsnd->cmdq, cmd, next);
+ virtio_snd_ctrl_cmd_free(cmd);
}
}
diff --git a/include/hw/audio/virtio-snd.h b/include/hw/audio/virtio-snd.h
index b60b098ccd8..884c475ffd9 100644
--- a/include/hw/audio/virtio-snd.h
+++ b/include/hw/audio/virtio-snd.h
@@ -143,7 +143,6 @@ struct VirtIOSoundPCMStream {
SWVoiceIn *in;
SWVoiceOut *out;
} voice;
- QemuMutex queue_mutex;
bool active;
uint32_t latency_bytes;
QSIMPLEQ_HEAD(, VirtIOSoundPCMBuffer) queue;
@@ -215,9 +214,7 @@ struct VirtIOSound {
AudioBackend *audio_be;
VMChangeStateEntry *vmstate;
virtio_snd_config snd_conf;
- QemuMutex cmdq_mutex;
QTAILQ_HEAD(, virtio_snd_ctrl_command) cmdq;
- bool processing_cmdq;
/*
* Convenience queue to keep track of invalid tx/rx queue messages inside
* the tx/rx callbacks.
--
2.47.3
next prev parent reply other threads:[~2026-08-03 8:13 UTC|newest]
Thread overview: 11+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-08-03 8:11 [PATCH v5 00/10] hw/audio/virtio-sound: basic migration support Alexander Mikhalitsyn
2026-08-03 8:11 ` [PATCH v5 01/10] hw/audio/virtio-sound: remove channel positions field from VirtIOSoundPCMStream Alexander Mikhalitsyn
2026-08-03 8:11 ` [PATCH v5 02/10] hw/audio/virtio-sound: drop unused struct VirtIOSoundPCMStream.flushing field Alexander Mikhalitsyn
2026-08-03 8:11 ` Alexander Mikhalitsyn [this message]
2026-08-03 8:11 ` [PATCH v5 04/10] hw/audio/virtio-sound: allocate an array of streams Alexander Mikhalitsyn
2026-08-03 8:11 ` [PATCH v5 05/10] hw/audio/virtio-sound: free all stream buffers on reset Alexander Mikhalitsyn
2026-08-03 8:11 ` [PATCH v5 06/10] hw/audio/virtio-sound: split out virtio_snd_pcm_start_stop() Alexander Mikhalitsyn
2026-08-03 8:11 ` [PATCH v5 07/10] hw/audio/virtio-sound: add stream state variable Alexander Mikhalitsyn
2026-08-03 8:11 ` [PATCH v5 08/10] hw/audio/virtio-sound: introduce virtio_snd_pcm_open() Alexander Mikhalitsyn
2026-08-03 8:11 ` [PATCH v5 09/10] hw/audio/virtio-sound: introduce virtio_snd_set_active() Alexander Mikhalitsyn
2026-08-03 8:11 ` [PATCH v5 10/10] hw/audio/virtio-sound: add missing vmstate fields Alexander Mikhalitsyn
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20260803081159.91981-4-alexander@mihalicyn.com \
--to=alexander@mihalicyn.com \
--cc=aleksandr.mikhalitsyn@futurfusion.io \
--cc=berrange@redhat.com \
--cc=kraxel@redhat.com \
--cc=manos.pitsidianakis@linaro.org \
--cc=marcandre.lureau@redhat.com \
--cc=mst@redhat.com \
--cc=qemu-devel@nongnu.org \
--cc=stgraber@stgraber.org \
--cc=vr_qemu@t-online.de \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.