Skip to contentSkip to conversation

hw/virtio: reset virtio balloon stats on machine reset

PATCH Superseded Daniel P. Berrangé 2 participants 8 messages Dec 18, 2024 – Feb 4, 2025

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 hw/virtio: reset virtio balloon stats on machine reset

When a machine is first booted, all virtio balloon stats are initialized to their default value -1 (18446744073709551615 when represented as unsigned).

They remain that way while the firmware is loading, and early phase of guest OS boot, until the virtio-balloon driver is activated. Thereafter the reported stats reflect the guest OS activity.

When a machine reset is performed, however, the virtio-balloon stats are left unchanged by QEMU, despite the guest OS no longer updating them, nor indeed even still existing.

IOW, the mgmt app keeps getting stale stats until the guest OS starts once more and loads the virtio-balloon driver (if ever). At that point the app will see a discontinuity in the reported values as they sudden jump from the stale value to the new value. This jump is indigituishable from a valid data update.

While there is an "last-updated" field to report on the freshness of the stats, that does not unambiguously tell the mgmt app whether the stats are still conceptually relevant to the current running workload.

It is more conceptually useful to reset the stats to their default values on machine reset, given that the previous guest workload the stats reflect no longer exists. The mgmt app can now clearly identify that there are is no stats information available from the current executing workload.

The 'last-updated' time is also reset back to 0.

IOW, on every machine reset, the virtio stats are in the same clean state they were when the macine first powered on.

A functional test is added to validate this behaviour with a real world guest OS.

---

One side-thought I have, is whether it makes sense to add a 'reset-count' field in the virtio stats, alongside the 'last-updated' field. While apps can infer a reset from seeing the stats all go back to their defaults, an explicit flag is simpler...

 MAINTAINERS                             |   1 +
 hw/virtio/virtio-balloon.c              |  30 ++++-
 include/hw/virtio/virtio-balloon.h      |   4 +
 tests/functional/test_virtio_balloon.py | 161 ++++++++++++++++++++++++
 4 files changed, 195 insertions(+), 1 deletion(-)
 create mode 100755 tests/functional/test_virtio_balloon.py
MAINTAINERS +1 -0
index 822f34344b..1380d53d03 100644
@@ -2234,6 +2234,7 @@ F: include/hw/virtio/virtio-balloon.h
 F: system/balloon.c
 F: include/sysemu/balloon.h
 F: tests/qtest/virtio-balloon-test.c
+F: tests/functional/test_virtio_balloon.py
 
 virtio-9p
 M: Greg Kurz <[email protected]>
hw/virtio/virtio-balloon.c +29 -1
index ab2ee30475..fe0854e198 100644
@@ -31,7 +31,7 @@
 #include "trace.h"
 #include "qemu/error-report.h"
 #include "migration/misc.h"
-
+#include "sysemu/reset.h"
 #include "hw/virtio/virtio-bus.h"
 #include "hw/virtio/virtio-access.h"
 
@@ -910,6 +910,8 @@ static void virtio_balloon_device_realize(DeviceState *dev, Error **errp)
     }
 
     reset_stats(s);
+    s->stats_last_update = 0;
+    qemu_register_resettable(OBJECT(dev));
 }
 
 static void virtio_balloon_device_unrealize(DeviceState *dev)
@@ -917,6 +919,7 @@ static void virtio_balloon_device_unrealize(DeviceState *dev)
     VirtIODevice *vdev = VIRTIO_DEVICE(dev);
     VirtIOBalloon *s = VIRTIO_BALLOON(dev);
 
+    qemu_unregister_resettable(OBJECT(dev));
     if (s->free_page_bh) {
         qemu_bh_delete(s->free_page_bh);
         object_unref(OBJECT(s->iothread));
@@ -987,6 +990,27 @@ static void virtio_balloon_set_status(VirtIODevice *vdev, uint8_t status)
     }
 }
 
+static ResettableState *virtio_balloon_get_reset_state(Object *obj)
+{
+    VirtIOBalloon *s = VIRTIO_BALLOON(obj);
+    return &s->reset_state;
+}
+
+static void virtio_balloon_system_reset_enter(Object *obj, ResetType type)
+{
+    VirtIOBalloon *s = VIRTIO_BALLOON(obj);
+
+    /*
+     * When waking up from standby/suspend-to-ram, do not reset stats.
+     */
+    if (type == RESET_TYPE_WAKEUP) {
+        return;
+    }
+
+    reset_stats(s);
+    s->stats_last_update = 0;
+}
+
 static void virtio_balloon_instance_init(Object *obj)
 {
     VirtIOBalloon *s = VIRTIO_BALLOON(obj);
@@ -1039,6 +1063,7 @@ static void virtio_balloon_class_init(ObjectClass *klass, void *data)
 {
     DeviceClass *dc = DEVICE_CLASS(klass);
     VirtioDeviceClass *vdc = VIRTIO_DEVICE_CLASS(klass);
+    ResettableClass *rc = RESETTABLE_CLASS(klass);
 
     device_class_set_props(dc, virtio_balloon_properties);
     dc->vmsd = &vmstate_virtio_balloon;
@@ -1051,6 +1076,9 @@ static void virtio_balloon_class_init(ObjectClass *klass, void *data)
     vdc->get_features = virtio_balloon_get_features;
     vdc->set_status = virtio_balloon_set_status;
     vdc->vmsd = &vmstate_virtio_balloon_device;
+
+    rc->get_state = virtio_balloon_get_reset_state;
+    rc->phases.enter = virtio_balloon_system_reset_enter;
 }
 
 static const TypeInfo virtio_balloon_info = {
include/hw/virtio/virtio-balloon.h +4 -0
index 5139cf8ab6..e8a4dcbee2 100644
@@ -16,6 +16,7 @@
 #define QEMU_VIRTIO_BALLOON_H
 
 #include "standard-headers/linux/virtio_balloon.h"
+#include "hw/resettable.h"
 #include "hw/virtio/virtio.h"
 #include "sysemu/iothread.h"
 #include "qom/object.h"
@@ -71,6 +72,9 @@ struct VirtIOBalloon {
 
     bool qemu_4_0_config_size;
     uint32_t poison_val;
+
+    /* State of the resettable container */
+    ResettableState reset_state;
 };
 
 #endif
tests/functional/test_virtio_balloon.py new file +161 -0

161 lines not on this page · raw message

Signature · 1 line
2.46.0
#1 lore raw
Quoted 94 lines · Daniel P. Berrangé

When a machine is first booted, all virtio balloon stats are initialized to their default value -1 (18446744073709551615 when represented as unsigned).

They remain that way while the firmware is loading, and early phase of guest OS boot, until the virtio-balloon driver is activated. Thereafter the reported stats reflect the guest OS activity.

When a machine reset is performed, however, the virtio-balloon stats are left unchanged by QEMU, despite the guest OS no longer updating them, nor indeed even still existing.

IOW, the mgmt app keeps getting stale stats until the guest OS starts once more and loads the virtio-balloon driver (if ever). At that point the app will see a discontinuity in the reported values as they sudden jump from the stale value to the new value. This jump is indigituishable from a valid data update.

While there is an "last-updated" field to report on the freshness of the stats, that does not unambiguously tell the mgmt app whether the stats are still conceptually relevant to the current running workload.

It is more conceptually useful to reset the stats to their default values on machine reset, given that the previous guest workload the stats reflect no longer exists. The mgmt app can now clearly identify that there are is no stats information available from the current executing workload.

The 'last-updated' time is also reset back to 0.

IOW, on every machine reset, the virtio stats are in the same clean state they were when the macine first powered on.

A functional test is added to validate this behaviour with a real world guest OS.

Quoted 1 line

---

One side-thought I have, is whether it makes sense to add a 'reset-count' field in the virtio stats, alongside the 'last-updated' field. While apps can infer a reset from seeing the stats all go back to their defaults, an explicit flag is simpler...

  MAINTAINERS                             |   1 +
  hw/virtio/virtio-balloon.c              |  30 ++++-
  include/hw/virtio/virtio-balloon.h      |   4 +
  tests/functional/test_virtio_balloon.py | 161 ++++++++++++++++++++++++
  4 files changed, 195 insertions(+), 1 deletion(-)

create mode 100755 tests/functional/test_virtio_balloon.py

MAINTAINERS +1 -0
index 822f34344b..1380d53d03 100644
@@ -2234,6 +2234,7 @@ F: include/hw/virtio/virtio-balloon.h
  F: system/balloon.c
  F: include/sysemu/balloon.h
  F: tests/qtest/virtio-balloon-test.c
+F: tests/functional/test_virtio_balloon.py
 
  virtio-9p
  M: Greg Kurz <[email protected]>
hw/virtio/virtio-balloon.c +4 -1
index ab2ee30475..fe0854e198 100644
@@ -31,7 +31,7 @@
  #include "trace.h"
  #include "qemu/error-report.h"
  #include "migration/misc.h"
-
+#include "sysemu/reset.h"
  #include "hw/virtio/virtio-bus.h"
  #include "hw/virtio/virtio-access.h"
 
@@ -910,6 +910,8 @@ static void virtio_balloon_device_realize(DeviceState *dev, Error **errp)
      }
 
      reset_stats(s);
+    s->stats_last_update = 0;
+    qemu_register_resettable(OBJECT(dev));
  }
 
  static void virtio_balloon_device_unrealize(DeviceState *dev)
@@ -917,6 +919,7 @@ static void virtio_balloon_device_unrealize(DeviceState *dev)
      VirtIODevice *vdev = VIRTIO_DEVICE(dev);
      VirtIOBalloon *s = VIRTIO_BALLOON(dev);
 
+    qemu_unregister_resettable(OBJECT(dev));
      if (s->free_page_bh) {
          qemu_bh_delete(s->free_page_bh);
          object_unref(OBJECT(s->iothread));

hw/virtio/virtio-balloon.c static void virtio_balloon_set_status(VirtIODevice *vdev, uint8_t status) · line 991

Quoted 2 lines
hw/virtio/virtio-balloon.c +0 -0
@@ -987,6 +990,27 @@ static void virtio_balloon_set_status(VirtIODevice *vdev, uint8_t status)
      }
  }

Using qemu_register_resettable() can have unfortunate side effects that this code is triggered when the device is reset, not necessarily when the complete machine.

For virtio-mem at least that's an issue, and here is how I'll fix it:

https://lore.kernel.org/qemu-devel/[email protected]/

Signature · 3 lines
Cheers,

David / dhildenb
#3 lore raw
In reply to Daniel P. Berrangé
Quoted 98 lines · Daniel P. Berrangé
Quoted 97 lines · David Hildenbrand
Quoted 89 lines · Daniel P. Berrangé

When a machine is first booted, all virtio balloon stats are initialized to their default value -1 (18446744073709551615 when represented as unsigned).

They remain that way while the firmware is loading, and early phase of guest OS boot, until the virtio-balloon driver is activated. Thereafter the reported stats reflect the guest OS activity.

When a machine reset is performed, however, the virtio-balloon stats are left unchanged by QEMU, despite the guest OS no longer updating them, nor indeed even still existing.

IOW, the mgmt app keeps getting stale stats until the guest OS starts once more and loads the virtio-balloon driver (if ever). At that point the app will see a discontinuity in the reported values as they sudden jump from the stale value to the new value. This jump is indigituishable from a valid data update.

While there is an "last-updated" field to report on the freshness of the stats, that does not unambiguously tell the mgmt app whether the stats are still conceptually relevant to the current running workload.

It is more conceptually useful to reset the stats to their default values on machine reset, given that the previous guest workload the stats reflect no longer exists. The mgmt app can now clearly identify that there are is no stats information available from the current executing workload.

The 'last-updated' time is also reset back to 0.

IOW, on every machine reset, the virtio stats are in the same clean state they were when the macine first powered on.

A functional test is added to validate this behaviour with a real world guest OS.

Quoted 1 line

---

One side-thought I have, is whether it makes sense to add a 'reset-count' field in the virtio stats, alongside the 'last-updated' field. While apps can infer a reset from seeing the stats all go back to their defaults, an explicit flag is simpler...

   MAINTAINERS                             |   1 +
   hw/virtio/virtio-balloon.c              |  30 ++++-
   include/hw/virtio/virtio-balloon.h      |   4 +
   tests/functional/test_virtio_balloon.py | 161 ++++++++++++++++++++++++
   4 files changed, 195 insertions(+), 1 deletion(-)

create mode 100755 tests/functional/test_virtio_balloon.py

MAINTAINERS +1 -0
index 822f34344b..1380d53d03 100644
@@ -2234,6 +2234,7 @@ F: include/hw/virtio/virtio-balloon.h
   F: system/balloon.c
   F: include/sysemu/balloon.h
   F: tests/qtest/virtio-balloon-test.c
+F: tests/functional/test_virtio_balloon.py
   virtio-9p
   M: Greg Kurz <[email protected]>
hw/virtio/virtio-balloon.c +4 -1
index ab2ee30475..fe0854e198 100644
@@ -31,7 +31,7 @@
   #include "trace.h"
   #include "qemu/error-report.h"
   #include "migration/misc.h"
-
+#include "sysemu/reset.h"
   #include "hw/virtio/virtio-bus.h"
   #include "hw/virtio/virtio-access.h"
@@ -910,6 +910,8 @@ static void virtio_balloon_device_realize(DeviceState *dev, Error **errp)
       }
       reset_stats(s);
+    s->stats_last_update = 0;
+    qemu_register_resettable(OBJECT(dev));
   }
   static void virtio_balloon_device_unrealize(DeviceState *dev)
@@ -917,6 +919,7 @@ static void virtio_balloon_device_unrealize(DeviceState *dev)
       VirtIODevice *vdev = VIRTIO_DEVICE(dev);
       VirtIOBalloon *s = VIRTIO_BALLOON(dev);
+    qemu_unregister_resettable(OBJECT(dev));
       if (s->free_page_bh) {
           qemu_bh_delete(s->free_page_bh);
           object_unref(OBJECT(s->iothread));

hw/virtio/virtio-balloon.c static void virtio_balloon_set_status(VirtIODevice *vdev, uint8_t status) · line 991

Quoted 2 lines
hw/virtio/virtio-balloon.c +0 -0
@@ -987,6 +990,27 @@ static void virtio_balloon_set_status(VirtIODevice *vdev, uint8_t status)
       }
   }

Using qemu_register_resettable() can have unfortunate side effects that this code is triggered when the device is reset, not necessarily when the complete machine.

Quoted 8 lines
Quoted 3 lines

For virtio-mem at least that's an issue, and here is how I'll fix it:

https://lore.kernel.org/qemu-devel/[email protected]/

Urgh, that's a rather horrible situation. While your patch works around it quite effectively, it is pretty heavy weight, and of course relies on maintainers knowing this scenario exists - they won't learn this easily from the Resettable API design, nor its docs :-(

Yes.

Quoted 3 lines

Shouldn't we put to extend the Resettable design to make this scenario more explicity distinguishable in the Resettable callback implementations.

I'd be happy if I wouldn't need that heavy-weight stuff in virtio-mem code as well, so if you have ideas on how to avoid that in the future, that'd be great.

Signature · 3 lines
Cheers,

David / dhildenb
#4 lore raw
In reply to David Hildenbrand
Quoted 97 lines · David Hildenbrand
Quoted 89 lines · Daniel P. Berrangé

When a machine is first booted, all virtio balloon stats are initialized to their default value -1 (18446744073709551615 when represented as unsigned).

They remain that way while the firmware is loading, and early phase of guest OS boot, until the virtio-balloon driver is activated. Thereafter the reported stats reflect the guest OS activity.

When a machine reset is performed, however, the virtio-balloon stats are left unchanged by QEMU, despite the guest OS no longer updating them, nor indeed even still existing.

IOW, the mgmt app keeps getting stale stats until the guest OS starts once more and loads the virtio-balloon driver (if ever). At that point the app will see a discontinuity in the reported values as they sudden jump from the stale value to the new value. This jump is indigituishable from a valid data update.

While there is an "last-updated" field to report on the freshness of the stats, that does not unambiguously tell the mgmt app whether the stats are still conceptually relevant to the current running workload.

It is more conceptually useful to reset the stats to their default values on machine reset, given that the previous guest workload the stats reflect no longer exists. The mgmt app can now clearly identify that there are is no stats information available from the current executing workload.

The 'last-updated' time is also reset back to 0.

IOW, on every machine reset, the virtio stats are in the same clean state they were when the macine first powered on.

A functional test is added to validate this behaviour with a real world guest OS.

Quoted 1 line

---

One side-thought I have, is whether it makes sense to add a 'reset-count' field in the virtio stats, alongside the 'last-updated' field. While apps can infer a reset from seeing the stats all go back to their defaults, an explicit flag is simpler...

  MAINTAINERS                             |   1 +
  hw/virtio/virtio-balloon.c              |  30 ++++-
  include/hw/virtio/virtio-balloon.h      |   4 +
  tests/functional/test_virtio_balloon.py | 161 ++++++++++++++++++++++++
  4 files changed, 195 insertions(+), 1 deletion(-)

create mode 100755 tests/functional/test_virtio_balloon.py

MAINTAINERS +1 -0
index 822f34344b..1380d53d03 100644
@@ -2234,6 +2234,7 @@ F: include/hw/virtio/virtio-balloon.h
  F: system/balloon.c
  F: include/sysemu/balloon.h
  F: tests/qtest/virtio-balloon-test.c
+F: tests/functional/test_virtio_balloon.py
  virtio-9p
  M: Greg Kurz <[email protected]>
hw/virtio/virtio-balloon.c +4 -1
index ab2ee30475..fe0854e198 100644
@@ -31,7 +31,7 @@
  #include "trace.h"
  #include "qemu/error-report.h"
  #include "migration/misc.h"
-
+#include "sysemu/reset.h"
  #include "hw/virtio/virtio-bus.h"
  #include "hw/virtio/virtio-access.h"
@@ -910,6 +910,8 @@ static void virtio_balloon_device_realize(DeviceState *dev, Error **errp)
      }
      reset_stats(s);
+    s->stats_last_update = 0;
+    qemu_register_resettable(OBJECT(dev));
  }
  static void virtio_balloon_device_unrealize(DeviceState *dev)
@@ -917,6 +919,7 @@ static void virtio_balloon_device_unrealize(DeviceState *dev)
      VirtIODevice *vdev = VIRTIO_DEVICE(dev);
      VirtIOBalloon *s = VIRTIO_BALLOON(dev);
+    qemu_unregister_resettable(OBJECT(dev));
      if (s->free_page_bh) {
          qemu_bh_delete(s->free_page_bh);
          object_unref(OBJECT(s->iothread));

hw/virtio/virtio-balloon.c static void virtio_balloon_set_status(VirtIODevice *vdev, uint8_t status) · line 991

Quoted 2 lines
hw/virtio/virtio-balloon.c +0 -0
@@ -987,6 +990,27 @@ static void virtio_balloon_set_status(VirtIODevice *vdev, uint8_t status)
      }
  }

Using qemu_register_resettable() can have unfortunate side effects that this code is triggered when the device is reset, not necessarily when the complete machine.

Quoted 3 lines

For virtio-mem at least that's an issue, and here is how I'll fix it:

https://lore.kernel.org/qemu-devel/[email protected]/

IIUC, the scenario wrt kDump on s390x will be OK in this scenario. If you're resetting the balloon device across a kDump, I think it is likely desirable for the virtio balloon stats to be reset. The OS which populated those stats no longer exists, and has been replaced by the kExec kernel performing the kDump IIUC.

With regards,
Daniel

Signature · 3 lines
|: https://berrange.com      -o-    https://www.flickr.com/photos/dberrange :|
|: https://libvirt.org         -o-            https://fstop138.berrange.com :|
|: https://entangle-photo.org    -o-    https://www.instagram.com/dberrange :|
#2 lore raw
In reply to David Hildenbrand
Quoted 97 lines · David Hildenbrand
Quoted 89 lines · Daniel P. Berrangé

When a machine is first booted, all virtio balloon stats are initialized to their default value -1 (18446744073709551615 when represented as unsigned).

They remain that way while the firmware is loading, and early phase of guest OS boot, until the virtio-balloon driver is activated. Thereafter the reported stats reflect the guest OS activity.

When a machine reset is performed, however, the virtio-balloon stats are left unchanged by QEMU, despite the guest OS no longer updating them, nor indeed even still existing.

IOW, the mgmt app keeps getting stale stats until the guest OS starts once more and loads the virtio-balloon driver (if ever). At that point the app will see a discontinuity in the reported values as they sudden jump from the stale value to the new value. This jump is indigituishable from a valid data update.

While there is an "last-updated" field to report on the freshness of the stats, that does not unambiguously tell the mgmt app whether the stats are still conceptually relevant to the current running workload.

It is more conceptually useful to reset the stats to their default values on machine reset, given that the previous guest workload the stats reflect no longer exists. The mgmt app can now clearly identify that there are is no stats information available from the current executing workload.

The 'last-updated' time is also reset back to 0.

IOW, on every machine reset, the virtio stats are in the same clean state they were when the macine first powered on.

A functional test is added to validate this behaviour with a real world guest OS.

Quoted 1 line

---

One side-thought I have, is whether it makes sense to add a 'reset-count' field in the virtio stats, alongside the 'last-updated' field. While apps can infer a reset from seeing the stats all go back to their defaults, an explicit flag is simpler...

  MAINTAINERS                             |   1 +
  hw/virtio/virtio-balloon.c              |  30 ++++-
  include/hw/virtio/virtio-balloon.h      |   4 +
  tests/functional/test_virtio_balloon.py | 161 ++++++++++++++++++++++++
  4 files changed, 195 insertions(+), 1 deletion(-)

create mode 100755 tests/functional/test_virtio_balloon.py

MAINTAINERS +1 -0
index 822f34344b..1380d53d03 100644
@@ -2234,6 +2234,7 @@ F: include/hw/virtio/virtio-balloon.h
  F: system/balloon.c
  F: include/sysemu/balloon.h
  F: tests/qtest/virtio-balloon-test.c
+F: tests/functional/test_virtio_balloon.py
  virtio-9p
  M: Greg Kurz <[email protected]>
hw/virtio/virtio-balloon.c +4 -1
index ab2ee30475..fe0854e198 100644
@@ -31,7 +31,7 @@
  #include "trace.h"
  #include "qemu/error-report.h"
  #include "migration/misc.h"
-
+#include "sysemu/reset.h"
  #include "hw/virtio/virtio-bus.h"
  #include "hw/virtio/virtio-access.h"
@@ -910,6 +910,8 @@ static void virtio_balloon_device_realize(DeviceState *dev, Error **errp)
      }
      reset_stats(s);
+    s->stats_last_update = 0;
+    qemu_register_resettable(OBJECT(dev));
  }
  static void virtio_balloon_device_unrealize(DeviceState *dev)
@@ -917,6 +919,7 @@ static void virtio_balloon_device_unrealize(DeviceState *dev)
      VirtIODevice *vdev = VIRTIO_DEVICE(dev);
      VirtIOBalloon *s = VIRTIO_BALLOON(dev);
+    qemu_unregister_resettable(OBJECT(dev));
      if (s->free_page_bh) {
          qemu_bh_delete(s->free_page_bh);
          object_unref(OBJECT(s->iothread));

hw/virtio/virtio-balloon.c static void virtio_balloon_set_status(VirtIODevice *vdev, uint8_t status) · line 991

Quoted 2 lines
hw/virtio/virtio-balloon.c +0 -0
@@ -987,6 +990,27 @@ static void virtio_balloon_set_status(VirtIODevice *vdev, uint8_t status)
      }
  }

Using qemu_register_resettable() can have unfortunate side effects that this code is triggered when the device is reset, not necessarily when the complete machine.

Quoted 3 lines

For virtio-mem at least that's an issue, and here is how I'll fix it:

https://lore.kernel.org/qemu-devel/[email protected]/

Urgh, that's a rather horrible situation. While your patch works around it quite effectively, it is pretty heavy weight, and of course relies on maintainers knowing this scenario exists - they won't learn this easily from the Resettable API design, nor its docs :-(

Shouldn't we put to extend the Resettable design to make this scenario more explicity distinguishable in the Resettable callback implementations.

With regards,
Daniel

Signature · 3 lines
|: https://berrange.com      -o-    https://www.flickr.com/photos/dberrange :|
|: https://libvirt.org         -o-            https://fstop138.berrange.com :|
|: https://entangle-photo.org    -o-    https://www.instagram.com/dberrange :|
#5 lore raw
In reply to Daniel P. Berrangé
Quoted 100 lines · Daniel P. Berrangé
Quoted 99 lines · David Hildenbrand
Quoted 89 lines · Daniel P. Berrangé

When a machine is first booted, all virtio balloon stats are initialized to their default value -1 (18446744073709551615 when represented as unsigned).

They remain that way while the firmware is loading, and early phase of guest OS boot, until the virtio-balloon driver is activated. Thereafter the reported stats reflect the guest OS activity.

When a machine reset is performed, however, the virtio-balloon stats are left unchanged by QEMU, despite the guest OS no longer updating them, nor indeed even still existing.

IOW, the mgmt app keeps getting stale stats until the guest OS starts once more and loads the virtio-balloon driver (if ever). At that point the app will see a discontinuity in the reported values as they sudden jump from the stale value to the new value. This jump is indigituishable from a valid data update.

While there is an "last-updated" field to report on the freshness of the stats, that does not unambiguously tell the mgmt app whether the stats are still conceptually relevant to the current running workload.

It is more conceptually useful to reset the stats to their default values on machine reset, given that the previous guest workload the stats reflect no longer exists. The mgmt app can now clearly identify that there are is no stats information available from the current executing workload.

The 'last-updated' time is also reset back to 0.

IOW, on every machine reset, the virtio stats are in the same clean state they were when the macine first powered on.

A functional test is added to validate this behaviour with a real world guest OS.

Quoted 1 line

---

One side-thought I have, is whether it makes sense to add a 'reset-count' field in the virtio stats, alongside the 'last-updated' field. While apps can infer a reset from seeing the stats all go back to their defaults, an explicit flag is simpler...

   MAINTAINERS                             |   1 +
   hw/virtio/virtio-balloon.c              |  30 ++++-
   include/hw/virtio/virtio-balloon.h      |   4 +
   tests/functional/test_virtio_balloon.py | 161 ++++++++++++++++++++++++
   4 files changed, 195 insertions(+), 1 deletion(-)

create mode 100755 tests/functional/test_virtio_balloon.py

MAINTAINERS +1 -0
index 822f34344b..1380d53d03 100644
@@ -2234,6 +2234,7 @@ F: include/hw/virtio/virtio-balloon.h
   F: system/balloon.c
   F: include/sysemu/balloon.h
   F: tests/qtest/virtio-balloon-test.c
+F: tests/functional/test_virtio_balloon.py
   virtio-9p
   M: Greg Kurz <[email protected]>
hw/virtio/virtio-balloon.c +4 -1
index ab2ee30475..fe0854e198 100644
@@ -31,7 +31,7 @@
   #include "trace.h"
   #include "qemu/error-report.h"
   #include "migration/misc.h"
-
+#include "sysemu/reset.h"
   #include "hw/virtio/virtio-bus.h"
   #include "hw/virtio/virtio-access.h"
@@ -910,6 +910,8 @@ static void virtio_balloon_device_realize(DeviceState *dev, Error **errp)
       }
       reset_stats(s);
+    s->stats_last_update = 0;
+    qemu_register_resettable(OBJECT(dev));
   }
   static void virtio_balloon_device_unrealize(DeviceState *dev)
@@ -917,6 +919,7 @@ static void virtio_balloon_device_unrealize(DeviceState *dev)
       VirtIODevice *vdev = VIRTIO_DEVICE(dev);
       VirtIOBalloon *s = VIRTIO_BALLOON(dev);
+    qemu_unregister_resettable(OBJECT(dev));
       if (s->free_page_bh) {
           qemu_bh_delete(s->free_page_bh);
           object_unref(OBJECT(s->iothread));

hw/virtio/virtio-balloon.c static void virtio_balloon_set_status(VirtIODevice *vdev, uint8_t status) · line 991

Quoted 2 lines
hw/virtio/virtio-balloon.c +0 -0
@@ -987,6 +990,27 @@ static void virtio_balloon_set_status(VirtIODevice *vdev, uint8_t status)
       }
   }

Using qemu_register_resettable() can have unfortunate side effects that this code is triggered when the device is reset, not necessarily when the complete machine.

For virtio-mem at least that's an issue, and here is how I'll fix it:

Quoted 7 lines
Quoted 1 line

https://lore.kernel.org/qemu-devel/[email protected]/

IIUC, the scenario wrt kDump on s390x will be OK in this scenario. If you're resetting the balloon device across a kDump, I think it is likely desirable for the virtio balloon stats to be reset. The OS which populated those stats no longer exists, and has been replaced by the kExec kernel performing the kDump IIUC.

Likely. Hopefully there are no other device resets of that kind.

Hopefully the kexec kernel has the balloon driver somehow disabled, just imagine if it would try inflating the balloon when the kexec kernel usually only has access to a carved out memory region ...

I did not investigate how other archs besides x86,s390x,arm64,riscv behave with resets, because virtio-mem only supports these.

But yes, likely resetting stats that the VM will update soon again won't be very harmful.

Signature · 3 lines
Cheers,

David / dhildenb
#6 lore raw
In reply to David Hildenbrand
Quoted 97 lines · David Hildenbrand
Quoted 89 lines · Daniel P. Berrangé

When a machine is first booted, all virtio balloon stats are initialized to their default value -1 (18446744073709551615 when represented as unsigned).

They remain that way while the firmware is loading, and early phase of guest OS boot, until the virtio-balloon driver is activated. Thereafter the reported stats reflect the guest OS activity.

When a machine reset is performed, however, the virtio-balloon stats are left unchanged by QEMU, despite the guest OS no longer updating them, nor indeed even still existing.

IOW, the mgmt app keeps getting stale stats until the guest OS starts once more and loads the virtio-balloon driver (if ever). At that point the app will see a discontinuity in the reported values as they sudden jump from the stale value to the new value. This jump is indigituishable from a valid data update.

While there is an "last-updated" field to report on the freshness of the stats, that does not unambiguously tell the mgmt app whether the stats are still conceptually relevant to the current running workload.

It is more conceptually useful to reset the stats to their default values on machine reset, given that the previous guest workload the stats reflect no longer exists. The mgmt app can now clearly identify that there are is no stats information available from the current executing workload.

The 'last-updated' time is also reset back to 0.

IOW, on every machine reset, the virtio stats are in the same clean state they were when the macine first powered on.

A functional test is added to validate this behaviour with a real world guest OS.

Quoted 1 line

---

One side-thought I have, is whether it makes sense to add a 'reset-count' field in the virtio stats, alongside the 'last-updated' field. While apps can infer a reset from seeing the stats all go back to their defaults, an explicit flag is simpler...

  MAINTAINERS                             |   1 +
  hw/virtio/virtio-balloon.c              |  30 ++++-
  include/hw/virtio/virtio-balloon.h      |   4 +
  tests/functional/test_virtio_balloon.py | 161 ++++++++++++++++++++++++
  4 files changed, 195 insertions(+), 1 deletion(-)

create mode 100755 tests/functional/test_virtio_balloon.py

MAINTAINERS +1 -0
index 822f34344b..1380d53d03 100644
@@ -2234,6 +2234,7 @@ F: include/hw/virtio/virtio-balloon.h
  F: system/balloon.c
  F: include/sysemu/balloon.h
  F: tests/qtest/virtio-balloon-test.c
+F: tests/functional/test_virtio_balloon.py
  virtio-9p
  M: Greg Kurz <[email protected]>
hw/virtio/virtio-balloon.c +4 -1
index ab2ee30475..fe0854e198 100644
@@ -31,7 +31,7 @@
  #include "trace.h"
  #include "qemu/error-report.h"
  #include "migration/misc.h"
-
+#include "sysemu/reset.h"
  #include "hw/virtio/virtio-bus.h"
  #include "hw/virtio/virtio-access.h"
@@ -910,6 +910,8 @@ static void virtio_balloon_device_realize(DeviceState *dev, Error **errp)
      }
      reset_stats(s);
+    s->stats_last_update = 0;
+    qemu_register_resettable(OBJECT(dev));
  }
  static void virtio_balloon_device_unrealize(DeviceState *dev)
@@ -917,6 +919,7 @@ static void virtio_balloon_device_unrealize(DeviceState *dev)
      VirtIODevice *vdev = VIRTIO_DEVICE(dev);
      VirtIOBalloon *s = VIRTIO_BALLOON(dev);
+    qemu_unregister_resettable(OBJECT(dev));
      if (s->free_page_bh) {
          qemu_bh_delete(s->free_page_bh);
          object_unref(OBJECT(s->iothread));

hw/virtio/virtio-balloon.c static void virtio_balloon_set_status(VirtIODevice *vdev, uint8_t status) · line 991

Quoted 2 lines
hw/virtio/virtio-balloon.c +0 -0
@@ -987,6 +990,27 @@ static void virtio_balloon_set_status(VirtIODevice *vdev, uint8_t status)
      }
  }

Using qemu_register_resettable() can have unfortunate side effects that this code is triggered when the device is reset, not necessarily when the complete machine.

Quoted 3 lines

For virtio-mem at least that's an issue, and here is how I'll fix it:

https://lore.kernel.org/qemu-devel/[email protected]/

So having looked at it, I'm not convinced this is a problem for the virtio-balloon scenario.

Is there anything you'd like changed on this patch, or is it acceptable as is ?

With regards,
Daniel

Signature · 3 lines
|: https://berrange.com      -o-    https://www.flickr.com/photos/dberrange :|
|: https://libvirt.org         -o-            https://fstop138.berrange.com :|
|: https://entangle-photo.org    -o-    https://www.instagram.com/dberrange :|
#7 lore raw
Quoted 100 lines · Daniel P. Berrangé
Quoted 99 lines · David Hildenbrand
Quoted 89 lines · Daniel P. Berrangé

When a machine is first booted, all virtio balloon stats are initialized to their default value -1 (18446744073709551615 when represented as unsigned).

They remain that way while the firmware is loading, and early phase of guest OS boot, until the virtio-balloon driver is activated. Thereafter the reported stats reflect the guest OS activity.

When a machine reset is performed, however, the virtio-balloon stats are left unchanged by QEMU, despite the guest OS no longer updating them, nor indeed even still existing.

IOW, the mgmt app keeps getting stale stats until the guest OS starts once more and loads the virtio-balloon driver (if ever). At that point the app will see a discontinuity in the reported values as they sudden jump from the stale value to the new value. This jump is indigituishable from a valid data update.

While there is an "last-updated" field to report on the freshness of the stats, that does not unambiguously tell the mgmt app whether the stats are still conceptually relevant to the current running workload.

It is more conceptually useful to reset the stats to their default values on machine reset, given that the previous guest workload the stats reflect no longer exists. The mgmt app can now clearly identify that there are is no stats information available from the current executing workload.

The 'last-updated' time is also reset back to 0.

IOW, on every machine reset, the virtio stats are in the same clean state they were when the macine first powered on.

A functional test is added to validate this behaviour with a real world guest OS.

Quoted 1 line

---

One side-thought I have, is whether it makes sense to add a 'reset-count' field in the virtio stats, alongside the 'last-updated' field. While apps can infer a reset from seeing the stats all go back to their defaults, an explicit flag is simpler...

   MAINTAINERS                             |   1 +
   hw/virtio/virtio-balloon.c              |  30 ++++-
   include/hw/virtio/virtio-balloon.h      |   4 +
   tests/functional/test_virtio_balloon.py | 161 ++++++++++++++++++++++++
   4 files changed, 195 insertions(+), 1 deletion(-)

create mode 100755 tests/functional/test_virtio_balloon.py

MAINTAINERS +1 -0
index 822f34344b..1380d53d03 100644
@@ -2234,6 +2234,7 @@ F: include/hw/virtio/virtio-balloon.h
   F: system/balloon.c
   F: include/sysemu/balloon.h
   F: tests/qtest/virtio-balloon-test.c
+F: tests/functional/test_virtio_balloon.py
   virtio-9p
   M: Greg Kurz <[email protected]>
hw/virtio/virtio-balloon.c +4 -1
index ab2ee30475..fe0854e198 100644
@@ -31,7 +31,7 @@
   #include "trace.h"
   #include "qemu/error-report.h"
   #include "migration/misc.h"
-
+#include "sysemu/reset.h"
   #include "hw/virtio/virtio-bus.h"
   #include "hw/virtio/virtio-access.h"
@@ -910,6 +910,8 @@ static void virtio_balloon_device_realize(DeviceState *dev, Error **errp)
       }
       reset_stats(s);
+    s->stats_last_update = 0;
+    qemu_register_resettable(OBJECT(dev));
   }
   static void virtio_balloon_device_unrealize(DeviceState *dev)
@@ -917,6 +919,7 @@ static void virtio_balloon_device_unrealize(DeviceState *dev)
       VirtIODevice *vdev = VIRTIO_DEVICE(dev);
       VirtIOBalloon *s = VIRTIO_BALLOON(dev);
+    qemu_unregister_resettable(OBJECT(dev));
       if (s->free_page_bh) {
           qemu_bh_delete(s->free_page_bh);
           object_unref(OBJECT(s->iothread));

hw/virtio/virtio-balloon.c static void virtio_balloon_set_status(VirtIODevice *vdev, uint8_t status) · line 991

Quoted 2 lines
hw/virtio/virtio-balloon.c +0 -0
@@ -987,6 +990,27 @@ static void virtio_balloon_set_status(VirtIODevice *vdev, uint8_t status)
       }
   }

Using qemu_register_resettable() can have unfortunate side effects that this code is triggered when the device is reset, not necessarily when the complete machine.

For virtio-mem at least that's an issue, and here is how I'll fix it:

Quoted 7 lines
Quoted 1 line

https://lore.kernel.org/qemu-devel/[email protected]/

So having looked at it, I'm not convinced this is a problem for the virtio-balloon scenario.

Is there anything you'd like changed on this patch, or is it acceptable as is ?

Probably "_system_reset" should be called differently, to make it clearer that there are other cases.

Apart from that, I agree that virtio-balloon is likely fine.

Signature · 3 lines
Cheers,

David / dhildenb