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 10/10] hw/audio/virtio-sound: add missing vmstate fields
Date: Mon, 3 Aug 2026 10:11:59 +0200 [thread overview]
Message-ID: <20260803081159.91981-11-alexander@mihalicyn.com> (raw)
In-Reply-To: <20260803081159.91981-1-alexander@mihalicyn.com>
From: Volker Rümelin <vr_qemu@t-online.de>
The virtio-sound device is currently not migratable. Add the
missing VMSTATE fields, enable migration and reconnect the audio
streams after migration.
The queue_inuse[] array variables mimic the inuse variable in
struct VirtQueue which is private. They are needed to restart
the virtio queues after migration.
Signed-off-by: Volker Rümelin <vr_qemu@t-online.de>
[AM: trivial rebase changes]
Signed-off-by: Alexander Mikhalitsyn <aleksandr.mikhalitsyn@futurfusion.io>
Reviewed-by: Marc-André Lureau <marcandre.lureau@redhat.com>
---
v4:
- added assert(. > 0) before decremeting s->queue_inuse[.]
(suggested by Manos Pitsidianakis)
v3:
- added latency_bytes field to VMStateDescription
As suggested by Marc-André Lureau:
- removed the "rc" variable from virtio_snd_post_load()
- dropped info field from VMStateDescription, because
it can't be modified by guest and initialized only from realize
- added minimum_version_id/version_id so we can extend VMStateDescription
without breaking compatibility in the future
---
hw/audio/virtio-snd.c | 83 +++++++++++++++++++++++++++++++----
include/hw/audio/virtio-snd.h | 1 +
2 files changed, 76 insertions(+), 8 deletions(-)
diff --git a/hw/audio/virtio-snd.c b/hw/audio/virtio-snd.c
index 12473862d24..6df7ea0eb04 100644
--- a/hw/audio/virtio-snd.c
+++ b/hw/audio/virtio-snd.c
@@ -24,7 +24,6 @@
#include "qapi/error.h"
#include "hw/audio/virtio-snd.h"
-#define VIRTIO_SOUND_VM_VERSION 1
#define VIRTIO_SOUND_JACK_DEFAULT 0
#define VIRTIO_SOUND_STREAM_DEFAULT 2
#define VIRTIO_SOUND_CHMAP_DEFAULT 0
@@ -74,17 +73,40 @@ static uint32_t supported_rates = BIT(VIRTIO_SND_PCM_RATE_5512)
| BIT(VIRTIO_SND_PCM_RATE_192000)
| BIT(VIRTIO_SND_PCM_RATE_384000);
+static const VMStateDescription vmstate_virtio_snd_stream = {
+ .name = "virtio-sound-stream",
+ .version_id = 1,
+ .minimum_version_id = 1,
+ .fields = (const VMStateField[]) {
+ VMSTATE_UINT32(state, VirtIOSoundPCMStream),
+ VMSTATE_UINT32(params.buffer_bytes, VirtIOSoundPCMStream),
+ VMSTATE_UINT32(params.period_bytes, VirtIOSoundPCMStream),
+ VMSTATE_UINT32(params.features, VirtIOSoundPCMStream),
+ VMSTATE_UINT8(params.channels, VirtIOSoundPCMStream),
+ VMSTATE_UINT8(params.format, VirtIOSoundPCMStream),
+ VMSTATE_UINT8(params.rate, VirtIOSoundPCMStream),
+ VMSTATE_UINT32(latency_bytes, VirtIOSoundPCMStream),
+ VMSTATE_END_OF_LIST()
+ },
+};
+
static const VMStateDescription vmstate_virtio_snd_device = {
- .name = TYPE_VIRTIO_SND,
- .version_id = VIRTIO_SOUND_VM_VERSION,
- .minimum_version_id = VIRTIO_SOUND_VM_VERSION,
+ .name = "virtio-sound-device",
+ .version_id = 1,
+ .minimum_version_id = 1,
+ .fields = (const VMStateField[]) {
+ VMSTATE_UINT32_ARRAY(queue_inuse, VirtIOSound, VIRTIO_SND_VQ_MAX),
+ VMSTATE_STRUCT_VARRAY_POINTER_UINT32(streams, VirtIOSound,
+ snd_conf.streams,
+ vmstate_virtio_snd_stream, VirtIOSoundPCMStream),
+ VMSTATE_END_OF_LIST()
+ },
};
static const VMStateDescription vmstate_virtio_snd = {
- .name = TYPE_VIRTIO_SND,
- .unmigratable = 1,
- .minimum_version_id = VIRTIO_SOUND_VM_VERSION,
- .version_id = VIRTIO_SOUND_VM_VERSION,
+ .name = "virtio-sound",
+ .version_id = 1,
+ .minimum_version_id = 1,
.fields = (const VMStateField[]) {
VMSTATE_VIRTIO_DEVICE,
VMSTATE_END_OF_LIST()
@@ -813,6 +835,8 @@ process_cmd(VirtIOSound *s, virtio_snd_ctrl_command *cmd)
sizeof(virtio_snd_hdr));
virtqueue_push(cmd->vq, cmd->elem,
sizeof(virtio_snd_hdr) + cmd->payload_size);
+ g_assert(s->queue_inuse[VIRTIO_SND_VQ_CONTROL] > 0);
+ s->queue_inuse[VIRTIO_SND_VQ_CONTROL] -= 1;
virtio_notify(VIRTIO_DEVICE(s), cmd->vq);
}
@@ -859,6 +883,7 @@ static void virtio_snd_handle_ctrl(VirtIODevice *vdev, VirtQueue *vq)
elem = virtqueue_pop(vq, sizeof(VirtQueueElement));
while (elem) {
+ s->queue_inuse[VIRTIO_SND_VQ_CONTROL] += 1;
cmd = g_new0(virtio_snd_ctrl_command, 1);
cmd->elem = elem;
cmd->vq = vq;
@@ -976,6 +1001,7 @@ static void virtio_snd_handle_tx_xfer(VirtIODevice *vdev, VirtQueue *vq)
goto tx_err;
}
+ vsnd->queue_inuse[VIRTIO_SND_VQ_TX] += 1;
buffer = g_malloc0(sizeof(VirtIOSoundPCMBuffer) + size);
buffer->elem = elem;
buffer->populated = false;
@@ -1060,6 +1086,7 @@ static void virtio_snd_handle_rx_xfer(VirtIODevice *vdev, VirtQueue *vq)
goto rx_err;
}
+ vsnd->queue_inuse[VIRTIO_SND_VQ_RX] += 1;
buffer = g_malloc0(sizeof(VirtIOSoundPCMBuffer) + size);
buffer->elem = elem;
buffer->vq = vq;
@@ -1200,6 +1227,8 @@ static inline void return_tx_buffer(VirtIOSoundPCMStream *stream,
virtqueue_push(buffer->vq,
buffer->elem,
sizeof(virtio_snd_pcm_status));
+ g_assert(stream->s->queue_inuse[VIRTIO_SND_VQ_TX] > 0);
+ stream->s->queue_inuse[VIRTIO_SND_VQ_TX] -= 1;
virtio_notify(VIRTIO_DEVICE(stream->s), buffer->vq);
QSIMPLEQ_REMOVE(&stream->queue,
buffer,
@@ -1293,6 +1322,8 @@ static inline void return_rx_buffer(VirtIOSoundPCMStream *stream,
virtqueue_push(buffer->vq,
buffer->elem,
sizeof(virtio_snd_pcm_status) + buffer->size);
+ g_assert(stream->s->queue_inuse[VIRTIO_SND_VQ_RX] > 0);
+ stream->s->queue_inuse[VIRTIO_SND_VQ_RX] -= 1;
virtio_notify(VIRTIO_DEVICE(stream->s), buffer->vq);
QSIMPLEQ_REMOVE(&stream->queue,
buffer,
@@ -1411,6 +1442,37 @@ static void virtio_snd_unrealize(DeviceState *dev)
virtio_cleanup(vdev);
}
+static int virtio_snd_post_load(VirtIODevice *vdev)
+{
+ VirtIOSound *s = VIRTIO_SND(vdev);
+ uint32_t i;
+
+ for (i = 0; i < s->snd_conf.streams; i++) {
+ struct VirtIOSoundPCMStream *stream;
+
+ stream = virtio_snd_pcm_get_stream(s, i);
+ if (virtio_snd_pcm_state_prepared(stream->state)) {
+ virtio_snd_pcm_open(stream);
+
+ if (stream->state == VIRTIO_SND_PCM_STATE_STARTED) {
+ virtio_snd_pcm_set_active(stream, true);
+ }
+ }
+ }
+
+ for (i = 0; i < VIRTIO_SND_VQ_MAX; i++) {
+ if (s->queue_inuse[i]) {
+ if (!virtqueue_rewind(s->queues[i], s->queue_inuse[i])) {
+ error_report(
+ "virtio-snd: could not rewind %u elements in queue %u",
+ s->queue_inuse[i], i);
+ }
+ s->queue_inuse[i] = 0;
+ }
+ }
+
+ return 0;
+}
static void virtio_snd_reset(VirtIODevice *vdev)
{
@@ -1442,6 +1504,10 @@ static void virtio_snd_reset(VirtIODevice *vdev)
virtio_snd_pcm_buffer_free(buffer);
}
}
+
+ for (uint32_t i = 0; i < VIRTIO_SND_VQ_MAX; i++) {
+ vsnd->queue_inuse[i] = 0;
+ }
}
static void virtio_snd_class_init(ObjectClass *klass, const void *data)
@@ -1455,6 +1521,7 @@ static void virtio_snd_class_init(ObjectClass *klass, const void *data)
dc->vmsd = &vmstate_virtio_snd;
vdc->vmsd = &vmstate_virtio_snd_device;
+ vdc->post_load = virtio_snd_post_load;
vdc->realize = virtio_snd_realize;
vdc->unrealize = virtio_snd_unrealize;
vdc->get_config = virtio_snd_get_config;
diff --git a/include/hw/audio/virtio-snd.h b/include/hw/audio/virtio-snd.h
index 85d5d7c8619..384d2868c19 100644
--- a/include/hw/audio/virtio-snd.h
+++ b/include/hw/audio/virtio-snd.h
@@ -194,6 +194,7 @@ struct VirtIOSound {
VirtIODevice parent_obj;
VirtQueue *queues[VIRTIO_SND_VQ_MAX];
+ uint32_t queue_inuse[VIRTIO_SND_VQ_MAX];
uint64_t features;
VirtIOSoundPCMStream *streams;
AudioBackend *audio_be;
--
2.47.3
prev parent reply other threads:[~2026-08-03 8:12 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 ` [PATCH v5 03/10] hw/audio/virtio-sound: remove command and stream mutexes Alexander Mikhalitsyn
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 ` Alexander Mikhalitsyn [this message]
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-11-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.