Skip to contentSkip to conversation

monitor: Rate-limit MEMORY_DEVICE_SIZE_CHANGE qapi events per device

PATCH In review David Hildenbrand 2 participants 5 messages Sep 29, 2021 – Sep 30, 2021

lore
Share & tools
lore
#0 lore raw

v3 cover monitor: Rate-limit MEMORY_DEVICE_SIZE_CHANGE qapi events per device

Some fixes for virtio-mem-pci, to properly handle MEMORY_DEVICE_SIZE_CHANGE events, especially not dropping events of some devices when rate-limiting.

v2 -> v3:
- Split up into two patches
- "const char * qom_path" -> "char *qom_path"
- Fix existing memory leak and avoid a new memory leak when creating the event

v1 -> v2:
- Add the qom-path and use that identifier to rate-limit per device
- Rephrase subject/description

Cc "Dr. David Alan Gilbert" <[email protected]>
Cc Markus Armbruster <[email protected]>
Cc Michael S. Tsirkin <[email protected]>
Cc Eric Blake <[email protected]>
Cc Igor Mammedov <[email protected]>
Cc Michal Privoznik <[email protected]>

David Hildenbrand (3):
virtio-mem-pci: Fix memory leak when creating
MEMORY_DEVICE_SIZE_CHANGE event
qapi: Include qom-path in MEMORY_DEVICE_SIZE_CHANGE qapi events
monitor: Rate-limit MEMORY_DEVICE_SIZE_CHANGE qapi events per device

 hw/virtio/virtio-mem-pci.c | 10 ++++------
 monitor/monitor.c          |  9 +++++++++
 qapi/machine.json          |  5 ++++-
 3 files changed, 17 insertions(+), 7 deletions(-)
Signature · 1 line
2.31.1
#1 lore raw

PATCH v3 1/3 virtio-mem-pci: Fix memory leak when creating MEMORY_DEVICE_SIZE_CHANGE event

Apparently, we don't have to duplicate the string.

Fixes 722a3c783ef4 ("virtio-pci: Send qapi events when the virtio-mem size changes")
---
 hw/virtio/virtio-mem-pci.c | 7 +------
 1 file changed, 1 insertion(+), 6 deletions(-)
hw/virtio/virtio-mem-pci.c +1 -6
index fa5395cd88..7e384b7397 100644
@@ -88,13 +88,8 @@ static void virtio_mem_pci_size_change_notify(Notifier *notifier, void *data)
                                          size_change_notifier);
     DeviceState *dev = DEVICE(pci_mem);
     const uint64_t * const size_p = data;
-    const char *id = NULL;
 
-    if (dev->id) {
-        id = g_strdup(dev->id);
-    }
-
-    qapi_event_send_memory_device_size_change(!!id, id, *size_p);
+    qapi_event_send_memory_device_size_change(!!dev->id, dev->id, *size_p);
 }
 
 static void virtio_mem_pci_class_init(ObjectClass *klass, void *data)
Signature · 1 line
2.31.1
#2 lore raw

PATCH v3 2/3 qapi: Include qom-path in MEMORY_DEVICE_SIZE_CHANGE qapi events

In reply to David Hildenbrand

As we might not always have a device id, it is impossible to always match MEMORY_DEVICE_SIZE_CHANGE events to an actual device. Let's include the qom-path in the event, which allows for reliable mapping of events to devices.

Fixes 722a3c783ef4 ("virtio-pci: Send qapi events when the virtio-mem size changes")
---
 hw/virtio/virtio-mem-pci.c | 5 ++++-
 qapi/machine.json          | 5 ++++-
 2 files changed, 8 insertions(+), 2 deletions(-)
hw/virtio/virtio-mem-pci.c +4 -1
index 7e384b7397..be2383b0c5 100644
@@ -87,9 +87,12 @@ static void virtio_mem_pci_size_change_notify(Notifier *notifier, void *data)
     VirtIOMEMPCI *pci_mem = container_of(notifier, VirtIOMEMPCI,
                                          size_change_notifier);
     DeviceState *dev = DEVICE(pci_mem);
+    char *qom_path = object_get_canonical_path(OBJECT(dev));
     const uint64_t * const size_p = data;
 
-    qapi_event_send_memory_device_size_change(!!dev->id, dev->id, *size_p);
+    qapi_event_send_memory_device_size_change(!!dev->id, dev->id, *size_p,
+                                              qom_path);
+    g_free(qom_path);
 }
 
 static void virtio_mem_pci_class_init(ObjectClass *klass, void *data)
qapi/machine.json +4 -1
index 32d47f4e35..8258b9ce3a 100644
@@ -1279,8 +1279,11 @@
 # action).
 #
 # @id: device's ID
+#
 # @size: the new size of memory that the device provides
 #
+# @qom-path: path to the device object in the QOM tree (since 6.2)
+#
 # Note: this event is rate-limited.
 #
 # Since: 5.1
@@ -1293,7 +1296,7 @@
 #
 ##
 { 'event': 'MEMORY_DEVICE_SIZE_CHANGE',
-  'data': { '*id': 'str', 'size': 'size' } }
+  'data': { '*id': 'str', 'size': 'size', 'qom-path' : 'str'} }
 
 
 ##
Signature · 1 line
2.31.1
#3 lore raw

PATCH v3 3/3 monitor: Rate-limit MEMORY_DEVICE_SIZE_CHANGE qapi events per device

In reply to David Hildenbrand

We want to rate-limit MEMORY_DEVICE_SIZE_CHANGE events per device, otherwise we can lose some events for devices. We can now use the qom-path to reliably map an event to a device and make rate-limiting device-aware.

This was noticed by starting a VM with two virtio-mem devices that each have a requested size > 0. The Linux guest will initialize both devices in parallel, resulting in losing MEMORY_DEVICE_SIZE_CHANGE events for one of the devices.

Fixes 722a3c783ef4 ("virtio-pci: Send qapi events when the virtio-mem size changes")
---
 monitor/monitor.c | 9 +++++++++
 1 file changed, 9 insertions(+)
monitor/monitor.c +9 -0
index 46a171bca6..21c7a68758 100644
@@ -474,6 +474,10 @@ static unsigned int qapi_event_throttle_hash(const void *key)
         hash += g_str_hash(qdict_get_str(evstate->data, "node-name"));
     }
 
+    if (evstate->event == QAPI_EVENT_MEMORY_DEVICE_SIZE_CHANGE) {
+        hash += g_str_hash(qdict_get_str(evstate->data, "qom-path"));
+    }
+
     return hash;
 }
 
@@ -496,6 +500,11 @@ static gboolean qapi_event_throttle_equal(const void *a, const void *b)
                        qdict_get_str(evb->data, "node-name"));
     }
 
+    if (eva->event == QAPI_EVENT_MEMORY_DEVICE_SIZE_CHANGE) {
+        return !strcmp(qdict_get_str(eva->data, "qom-path"),
+                       qdict_get_str(evb->data, "qom-path"));
+    }
+
     return TRUE;
 }
 
Signature · 1 line
2.31.1