Skip to contentSkip to conversation

virtio-snd: snapshot prep cleanups

PATCH In review Roman Kiryanov 2 participants 6 messages Mar 10 – Mar 25

lore
Share & tools
lore
#0 lore raw

cover virtio-snd: snapshot prep cleanups

I am currently working on adding snapshot support for the virtio-snd device. While implementing the VMStateDescription, I noticed that the device currently retains more internal state than is strictly necessary, including unused fields and full transport-specific layouts.

This patch series cleans up the internal PCM state to reduce the overall allocation footprint and simplify the upcoming snapshotting and migration handling code.

Roman Kiryanov (3):
virtio-snd: remove the unused field
virtio-snd: remove virtio_snd_pcm_set_params from VirtIOSoundPCMStream
virtio-snd: introduce VirtIOPcmParams

 hw/audio/virtio-snd.c         | 21 +++++++++------------
 include/hw/audio/virtio-snd.h | 17 +++++++++++++----
 2 files changed, 22 insertions(+), 16 deletions(-)
Signature · 1 line
2.53.0.473.g4a7958ca14-goog
#1 lore raw

PATCH 1/3 virtio-snd: remove the unused field

VirtIOSoundPCMStream::positions is set but
never read from. It can be added back once
required.

---
 hw/audio/virtio-snd.c         | 3 ---
 include/hw/audio/virtio-snd.h | 2 --
 2 files changed, 5 deletions(-)
hw/audio/virtio-snd.c +0 -3
index fb5cff3866..bd6a5d71bb 100644
@@ -462,9 +462,6 @@ static uint32_t virtio_snd_pcm_prepare(VirtIOSound *s, uint32_t stream_id)
     stream->info.formats = supported_formats;
     stream->info.rates = supported_rates;
     stream->params = *params;
-
-    stream->positions[0] = VIRTIO_SND_CHMAP_FL;
-    stream->positions[1] = VIRTIO_SND_CHMAP_FR;
     stream->as = as;
 
     if (stream->info.direction == VIRTIO_SND_D_OUTPUT) {
include/hw/audio/virtio-snd.h +0 -2
index e28f1be5db..f8bb5c95b9 100644
@@ -137,8 +137,6 @@ struct VirtIOSoundPCMStream {
     virtio_snd_pcm_info info;
     virtio_snd_pcm_set_params params;
     uint32_t id;
-    /* channel position values (VIRTIO_SND_CHMAP_XXX) */
-    uint8_t positions[VIRTIO_SND_CHMAP_MAX_SIZE];
     VirtIOSound *s;
     bool flushing;
     audsettings as;
Signature · 1 line
2.53.0.473.g4a7958ca14-goog
#2 lore raw

PATCH 2/3 virtio-snd: remove virtio_snd_pcm_set_params from VirtIOSoundPCMStream

In reply to Roman Kiryanov

The virtio_snd_pcm_set_params values are latched into a VirtIOSoundPCMStream instance during the PREPARE request where they are converted into audsettings. Only the period_bytes value is used afterwards.

Having both audsettings and virtio_snd_pcm_set_params next to each other grows sizeof and adds confusion to snapshot handling.

---
 hw/audio/virtio-snd.c         | 6 +++---
 include/hw/audio/virtio-snd.h | 2 +-
 2 files changed, 4 insertions(+), 4 deletions(-)
hw/audio/virtio-snd.c +3 -3
index bd6a5d71bb..6d820b56a6 100644
@@ -461,7 +461,7 @@ static uint32_t virtio_snd_pcm_prepare(VirtIOSound *s, uint32_t stream_id)
     stream->info.channels_max = as.nchannels;
     stream->info.formats = supported_formats;
     stream->info.rates = supported_rates;
-    stream->params = *params;
+    stream->period_bytes = params->period_bytes;
     stream->as = as;
 
     if (stream->info.direction == VIRTIO_SND_D_OUTPUT) {
@@ -1271,7 +1271,7 @@ static void virtio_snd_pcm_in_cb(void *data, int available)
                     return_rx_buffer(stream, buffer);
                     break;
                 }
-                to_read = stream->params.period_bytes - buffer->size;
+                to_read = stream->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,
@@ -1284,7 +1284,7 @@ static void virtio_snd_pcm_in_cb(void *data, int available)
                 }
                 buffer->size += size;
                 available -= size;
-                if (buffer->size >= stream->params.period_bytes) {
+                if (buffer->size >= stream->period_bytes) {
                     return_rx_buffer(stream, buffer);
                     break;
                 }
include/hw/audio/virtio-snd.h +1 -1
index f8bb5c95b9..a767f68301 100644
@@ -135,8 +135,8 @@ struct VirtIOSoundPCM {
 
 struct VirtIOSoundPCMStream {
     virtio_snd_pcm_info info;
-    virtio_snd_pcm_set_params params;
     uint32_t id;
+    uint32_t period_bytes;  /* from virtio_snd_pcm_set_params */
     VirtIOSound *s;
     bool flushing;
     audsettings as;
Signature · 1 line
2.53.0.473.g4a7958ca14-goog
#3 lore raw

PATCH 3/3 virtio-snd: introduce VirtIOPcmParams

In reply to Roman Kiryanov

Introduce the VirtIOPcmParams struct to represent the payload of virtio_snd_pcm_set_params without the VirtIO transport-specific header fields.

By decoupling the internal PCM state from the transport layout, VirtIOPcmParams simplifies the snapshot and migration handling code and slightly reduces the allocation footprint per stream.

---
 hw/audio/virtio-snd.c         | 12 ++++++------
 include/hw/audio/virtio-snd.h | 13 ++++++++++++-
 2 files changed, 18 insertions(+), 7 deletions(-)
hw/audio/virtio-snd.c +6 -6
index 6d820b56a6..8899eb7d5e 100644
@@ -139,8 +139,8 @@ static VirtIOSoundPCMStream *virtio_snd_pcm_get_stream(VirtIOSound *s,
  * @s: VirtIOSound device
  * @stream_id: stream id
  */
-static virtio_snd_pcm_set_params *virtio_snd_pcm_get_params(VirtIOSound *s,
-                                                            uint32_t stream_id)
+static VirtIOPcmParams *virtio_snd_pcm_get_params(VirtIOSound *s,
+                                                  uint32_t stream_id)
 {
     return stream_id >= s->snd_conf.streams ? NULL
         : &s->pcm.pcm_params[stream_id];
@@ -258,7 +258,7 @@ uint32_t virtio_snd_set_pcm_params(VirtIOSound *s,
                                    uint32_t stream_id,
                                    virtio_snd_pcm_set_params *params)
 {
-    virtio_snd_pcm_set_params *st_params;
+    VirtIOPcmParams *st_params;
 
     if (stream_id >= s->snd_conf.streams || s->pcm.pcm_params == NULL) {
         virtio_error(VIRTIO_DEVICE(s), "Streams have not been initialized.\n");
@@ -384,7 +384,7 @@ static uint32_t virtio_snd_get_qemu_freq(uint32_t rate)
  * params.
  */
 static void virtio_snd_get_qemu_audsettings(audsettings *as,
-                                            virtio_snd_pcm_set_params *params)
+                                            VirtIOPcmParams *params)
 {
     as->nchannels = MIN(AUDIO_MAX_CHANNELS, params->channels);
     as->fmt = virtio_snd_get_qemu_format(params->format);
@@ -421,7 +421,7 @@ static void virtio_snd_pcm_close(VirtIOSoundPCMStream *stream)
 static uint32_t virtio_snd_pcm_prepare(VirtIOSound *s, uint32_t stream_id)
 {
     audsettings as;
-    virtio_snd_pcm_set_params *params;
+    VirtIOPcmParams *params;
     VirtIOSoundPCMStream *stream;
 
     if (s->pcm.streams == NULL ||
@@ -1063,7 +1063,7 @@ static void virtio_snd_realize(DeviceState *dev, Error **errp)
     vsnd->pcm.streams =
         g_new0(VirtIOSoundPCMStream *, vsnd->snd_conf.streams);
     vsnd->pcm.pcm_params =
-        g_new0(virtio_snd_pcm_set_params, vsnd->snd_conf.streams);
+        g_new0(VirtIOPcmParams, vsnd->snd_conf.streams);
 
     virtio_init(vdev, VIRTIO_ID_SOUND, sizeof(virtio_snd_config));
     virtio_add_feature(&vsnd->features, VIRTIO_F_VERSION_1);
include/hw/audio/virtio-snd.h +12 -1
index a767f68301..e9fa9c620d 100644
@@ -71,6 +71,8 @@ typedef struct virtio_snd_pcm_status virtio_snd_pcm_status;
 
 typedef struct VirtIOSound VirtIOSound;
 
+typedef struct VirtIOPcmParams VirtIOPcmParams;
+
 typedef struct VirtIOSoundPCMStream VirtIOSoundPCMStream;
 
 typedef struct virtio_snd_ctrl_command virtio_snd_ctrl_command;
@@ -121,6 +123,15 @@ struct VirtIOSoundPCMBuffer {
     uint8_t data[];
 };
 
+struct VirtIOPcmParams {
+    uint32_t buffer_bytes;
+    uint32_t period_bytes;
+    uint32_t features;
+    uint8_t channels;
+    uint8_t format;
+    uint8_t rate;
+};
+
 struct VirtIOSoundPCM {
     /*
      * PCM parameters are a separate field instead of a VirtIOSoundPCMStream
@@ -129,7 +140,7 @@ struct VirtIOSoundPCM {
      * means that some times we get parameters without having an allocated
      * stream yet.
      */
-    virtio_snd_pcm_set_params *pcm_params;
+    VirtIOPcmParams *pcm_params;
     VirtIOSoundPCMStream **streams;
 };
 
Signature · 1 line
2.53.0.473.g4a7958ca14-goog
#4 lore raw
In reply to Roman Kiryanov
Quoted 3 lines · Roman Kiryanov

I am currently working on adding snapshot support for the virtio-snd device.

This patch series cleans up the internal PCM state ...

Hi everyone, just a quick ping on this cleanup series.

Let me know if anyone has had a chance to look over these patch series or if there are any concerns.

Thanks, Roman

Reviewed-by on the v1 series
#5 lore raw
In reply to Roman Kiryanov

Hi

Quoted 10 lines · Roman Kiryanov

I am currently working on adding snapshot support for the virtio-snd device. While implementing the VMStateDescription, I noticed that the device currently retains more internal state than is strictly necessary, including unused fields and full transport-specific layouts.

This patch series cleans up the internal PCM state to reduce the overall allocation footprint and simplify the upcoming snapshotting and migration handling code.

Quoted 8 lines

Roman Kiryanov (3):
virtio-snd: remove the unused field
virtio-snd: remove virtio_snd_pcm_set_params from VirtIOSoundPCMStream
virtio-snd: introduce VirtIOPcmParams

 hw/audio/virtio-snd.c         | 21 +++++++++------------
 include/hw/audio/virtio-snd.h | 17 +++++++++++++----
 2 files changed, 22 insertions(+), 16 deletions(-)

For the series:

Signature · 1 line
Marc-André Lureau