Skip to contentSkip to conversation

Hyper-V Dynamic Memory Protocol driver (hv-balloon 🎈️)

PATCH Superseded Maciej S. Szmigiero 3 participants 18 messages Aug 28, 2023 – Oct 18, 2023 Arm/Amlogic Meson SoC support

v7 of 8 Superseded Latest is v8 →

lore
Share & tools
lore
#0 lore raw

v7 cover Hyper-V Dynamic Memory Protocol driver (hv-balloon 🎈️)

From: "Maciej S. Szmigiero" <[email protected]>

This is a continuation of the v6 of the patch series located here:
https://lore.kernel.org/qemu-devel/[email protected]/

Changes from v6:
* Split the hv-balloon driver implementation into multiple files holding particular data structures and their methods in order to make the driver easier to understand.

* Split out the PC machine necessary plumbing for the driver and its final activation into a separate patch.

* Make sure that patches that bring QAPI-related changes also implement these in the driver in the same patch.

* Add a "query-hv-balloon-status-report" QMP command to query the data from the last received HV_BALLOON_STATUS_REPORT event.

* Rate limit the HV_BALLOON_STATUS_REPORT QMP event.

* Replace "TBD" in QAPI changes with the actual targeted QEMU version.

* Spelling and formatting fixes in QAPI changes.

* Rebase onto the latest David's patch series.

Based-on: <[email protected]>
Based-on-Repo-Commit: https://github.com/davidhildenbrand/qemu/tree/virtio-mem-memslots b65df116f8a8

David Hildenbrand (2):
memory-device: Support empty memory devices
memory-device: Drop size alignment check

Maciej S. Szmigiero (5):
Add Hyper-V Dynamic Memory Protocol definitions
Add a Hyper-V Dynamic Memory Protocol driver (hv-balloon)
qapi: Add query-memory-devices support to hv-balloon
qapi: Add HV_BALLOON_STATUS_REPORT event and its QMP query command hw/i386/pc: Support hv-balloon

 Kconfig.host                              |    3 +
 hw/core/machine-hmp-cmds.c                |   15 +
 hw/hyperv/Kconfig                         |   10 +
 hw/hyperv/hv-balloon-internal.h           |   33 +
 hw/hyperv/hv-balloon-our_range_memslots.c |  201 +++
 hw/hyperv/hv-balloon-our_range_memslots.h |  110 ++
 hw/hyperv/hv-balloon-page_range_tree.c    |  228 +++
 hw/hyperv/hv-balloon-page_range_tree.h    |  118 ++
 hw/hyperv/hv-balloon.c                    | 1767 +++++++++++++++++++++
 hw/hyperv/meson.build                     |    1 +
 hw/hyperv/trace-events                    |   18 +
 hw/i386/Kconfig                           |    1 +
 hw/i386/pc.c                              |   22 +
 hw/mem/memory-device.c                    |   49 +-
 include/hw/hyperv/dynmem-proto.h          |  423 +++++
 include/hw/hyperv/hv-balloon.h            |   18 +
 include/hw/mem/memory-device.h            |    7 +-
 meson.build                               |   28 +-
 meson_options.txt                         |    2 +
 monitor/monitor.c                         |    1 +
 qapi/machine.json                         |  103 +-
 scripts/meson-buildoptions.sh             |    3 +
 22 files changed, 3148 insertions(+), 13 deletions(-)
 create mode 100644 hw/hyperv/hv-balloon-internal.h
 create mode 100644 hw/hyperv/hv-balloon-our_range_memslots.c
 create mode 100644 hw/hyperv/hv-balloon-our_range_memslots.h
 create mode 100644 hw/hyperv/hv-balloon-page_range_tree.c
 create mode 100644 hw/hyperv/hv-balloon-page_range_tree.h
 create mode 100644 hw/hyperv/hv-balloon.c
 create mode 100644 include/hw/hyperv/dynmem-proto.h
 create mode 100644 include/hw/hyperv/hv-balloon.h
#1 lore raw

PATCH v7 1/7 memory-device: Support empty memory devices

From: David Hildenbrand <[email protected]>

Let's support empty memory devices -- memory devices that don't have a memory device region in the current configuration. hv-balloon with an optional memdev is the primary use case.

---
 hw/mem/memory-device.c         | 43 +++++++++++++++++++++++++++++++---
 include/hw/mem/memory-device.h |  7 +++++-
 2 files changed, 46 insertions(+), 4 deletions(-)
hw/mem/memory-device.c +40 -3
index 005e9b3a930c..8d0c166b3189 100644
@@ -20,6 +20,22 @@
 #include "exec/address-spaces.h"
 #include "trace.h"
 
+static bool memory_device_is_empty(const MemoryDeviceState *md)
+{
+    const MemoryDeviceClass *mdc = MEMORY_DEVICE_GET_CLASS(md);
+    Error *local_err = NULL;
+    MemoryRegion *mr;
+
+    /* dropping const here is fine as we don't touch the memory region */
+    mr = mdc->get_memory_region((MemoryDeviceState *)md, &local_err);
+    if (local_err) {
+        /* Not empty, we'll report errors later when ontaining the MR again. */
+        error_free(local_err);
+        return false;
+    }
+    return !mr;
+}
+
 static gint memory_device_addr_sort(gconstpointer a, gconstpointer b)
 {
     const MemoryDeviceState *md_a = MEMORY_DEVICE(a);
@@ -245,6 +261,10 @@ static uint64_t memory_device_get_free_addr(MachineState *ms,
         uint64_t next_addr;
         Range tmp;
 
+        if (memory_device_is_empty(md)) {
+            continue;
+        }
+
         range_init_nofail(&tmp, mdc->get_addr(md),
                           memory_device_get_region_size(md, &error_abort));
 
@@ -288,6 +308,7 @@ MemoryDeviceInfoList *qmp_memory_device_list(void)
         const MemoryDeviceClass *mdc = MEMORY_DEVICE_GET_CLASS(item->data);
         MemoryDeviceInfo *info = g_new0(MemoryDeviceInfo, 1);
 
+        /* Let's query infotmation even for empty memory devices. */
         mdc->fill_device_info(md, info);
 
         QAPI_LIST_APPEND(tail, info);
@@ -307,7 +328,7 @@ static int memory_device_plugged_size(Object *obj, void *opaque)
         const MemoryDeviceState *md = MEMORY_DEVICE(obj);
         const MemoryDeviceClass *mdc = MEMORY_DEVICE_GET_CLASS(obj);
 
-        if (dev->realized) {
+        if (dev->realized && !memory_device_is_empty(md)) {
             *size += mdc->get_plugged_size(md, &error_abort);
         }
     }
@@ -333,6 +354,11 @@ void memory_device_pre_plug(MemoryDeviceState *md, MachineState *ms,
     uint64_t addr, align = 0;
     MemoryRegion *mr;
 
+    /* We support empty memory devices even without device memory. */
+    if (memory_device_is_empty(md)) {
+        return;
+    }
+
     if (!ms->device_memory) {
         error_setg(errp, "the configuration is not prepared for memory devices"
                          " (e.g., for memory hotplug), consider specifying the"
@@ -376,10 +402,17 @@ out:
 void memory_device_plug(MemoryDeviceState *md, MachineState *ms)
 {
     const MemoryDeviceClass *mdc = MEMORY_DEVICE_GET_CLASS(md);
-    const unsigned int memslots = memory_device_get_memslots(md);
-    const uint64_t addr = mdc->get_addr(md);
+    unsigned int memslots;
+    uint64_t addr;
     MemoryRegion *mr;
 
+    if (memory_device_is_empty(md)) {
+        return;
+    }
+
+    memslots = memory_device_get_memslots(md);
+    addr = mdc->get_addr(md);
+
     /*
      * We expect that a previous call to memory_device_pre_plug() succeeded, so
      * it can't fail at this point.
@@ -404,6 +437,10 @@ void memory_device_unplug(MemoryDeviceState *md, MachineState *ms)
     const unsigned int memslots = memory_device_get_memslots(md);
     MemoryRegion *mr;
 
+    if (memory_device_is_empty(md)) {
+        return;
+    }
+
     /*
      * We expect that a previous call to memory_device_pre_plug() succeeded, so
      * it can't fail at this point.
include/hw/mem/memory-device.h +6 -1
index 3354d6c1667e..a1d62cc551ab 100644
@@ -38,6 +38,10 @@ typedef struct MemoryDeviceState MemoryDeviceState;
  * address in guest physical memory can either be specified explicitly
  * or get assigned automatically.
  *
+ * Some memory device might not own a memory region in certain device
+ * configurations. Such devices can logically get (un)plugged, however,
+ * empty memory devices are mostly ignored by the memory device code.
+ *
  * Conceptually, memory devices only span one memory region. If multiple
  * successive memory regions are used, a covering memory region has to
  * be provided. Scattered memory regions are not supported for single
@@ -91,7 +95,8 @@ struct MemoryDeviceClass {
     uint64_t (*get_plugged_size)(const MemoryDeviceState *md, Error **errp);
 
     /*
-     * Return the memory region of the memory device.
+     * Return the memory region of the memory device. If the device is
+     * completely empty, returns NULL without an error.
      *
      * Called when (un)plugging the memory device, to (un)map the
      * memory region in guest physical memory, but also to detect the
#2 lore raw

PATCH v7 2/7 memory-device: Drop size alignment check

In reply to Maciej S. Szmigiero

From: David Hildenbrand <[email protected]>

There is no strong requirement that the size has to be multiples of the requested alignment, let's drop it. This is a preparation for hv-baloon.

---
 hw/mem/memory-device.c | 6 ------
 1 file changed, 6 deletions(-)
hw/mem/memory-device.c +0 -6
index 8d0c166b3189..91b35836b7b5 100644
@@ -232,12 +232,6 @@ static uint64_t memory_device_get_free_addr(MachineState *ms,
         return 0;
     }
 
-    if (!QEMU_IS_ALIGNED(size, align)) {
-        error_setg(errp, "backend memory size must be multiple of 0x%"
-                   PRIx64, align);
-        return 0;
-    }
-
     if (hint) {
         if (range_init(&new, *hint, size) || !range_contains_range(&as, &new)) {
             error_setg(errp, "can't add memory device [0x%" PRIx64 ":0x%" PRIx64
#3 lore raw

PATCH v7 3/7 Add Hyper-V Dynamic Memory Protocol definitions

In reply to Maciej S. Szmigiero

From: "Maciej S. Szmigiero" <[email protected]>

This commit adds Hyper-V Dynamic Memory Protocol definitions, taken from hv_balloon Linux kernel driver, adapted to the QEMU coding style and definitions.

---
 include/hw/hyperv/dynmem-proto.h | 423 +++++++++++++++++++++++++++++++
 1 file changed, 423 insertions(+)
 create mode 100644 include/hw/hyperv/dynmem-proto.h
include/hw/hyperv/dynmem-proto.h new file +423 -0
new file mode 100644
index 000000000000..d0f9090ac489
@@ -0,0 +1,423 @@
+#ifndef HW_HYPERV_DYNMEM_PROTO_H
+#define HW_HYPERV_DYNMEM_PROTO_H
+
+/*
+ * Hyper-V Dynamic Memory Protocol definitions
+ *
+ * Copyright (C) 2020-2023 Oracle and/or its affiliates.
+ *
+ * Based on drivers/hv/hv_balloon.c from Linux kernel:
+ * Copyright (c) 2012, Microsoft Corporation.
+ *
+ * Author: K. Y. Srinivasan <[email protected]>
+ *
+ * This work is licensed under the terms of the GNU GPL, version 2.
+ * See the COPYING file in the top-level directory.
+ */
+
+/*
+ * Protocol versions. The low word is the minor version, the high word the major
+ * version.
+ *
+ * History:
+ * Initial version 1.0
+ * Changed to 0.1 on 2009/03/25
+ * Changes to 0.2 on 2009/05/14
+ * Changes to 0.3 on 2009/12/03
+ * Changed to 1.0 on 2011/04/05
+ * Changed to 2.0 on 2019/12/10
+ */
+
+#define DYNMEM_MAKE_VERSION(Major, Minor) ((uint32_t)(((Major) << 16) | (Minor)))
+#define DYNMEM_MAJOR_VERSION(Version) ((uint32_t)(Version) >> 16)
+#define DYNMEM_MINOR_VERSION(Version) ((uint32_t)(Version) & 0xff)
+
+enum {
+    DYNMEM_PROTOCOL_VERSION_1 = DYNMEM_MAKE_VERSION(0, 3),
+    DYNMEM_PROTOCOL_VERSION_2 = DYNMEM_MAKE_VERSION(1, 0),
+    DYNMEM_PROTOCOL_VERSION_3 = DYNMEM_MAKE_VERSION(2, 0),
+
+    DYNMEM_PROTOCOL_VERSION_WIN7 = DYNMEM_PROTOCOL_VERSION_1,
+    DYNMEM_PROTOCOL_VERSION_WIN8 = DYNMEM_PROTOCOL_VERSION_2,
+    DYNMEM_PROTOCOL_VERSION_WIN10 = DYNMEM_PROTOCOL_VERSION_3,
+
+    DYNMEM_PROTOCOL_VERSION_CURRENT = DYNMEM_PROTOCOL_VERSION_WIN10
+};
+
+
+
+/*
+ * Message Types
+ */
+
+enum dm_message_type {
+    /*
+     * Version 0.3
+     */
+    DM_ERROR = 0,
+    DM_VERSION_REQUEST = 1,
+    DM_VERSION_RESPONSE = 2,
+    DM_CAPABILITIES_REPORT = 3,
+    DM_CAPABILITIES_RESPONSE = 4,
+    DM_STATUS_REPORT = 5,
+    DM_BALLOON_REQUEST = 6,
+    DM_BALLOON_RESPONSE = 7,
+    DM_UNBALLOON_REQUEST = 8,
+    DM_UNBALLOON_RESPONSE = 9,
+    DM_MEM_HOT_ADD_REQUEST = 10,
+    DM_MEM_HOT_ADD_RESPONSE = 11,
+    DM_VERSION_03_MAX = 11,
+    /*
+     * Version 1.0.
+     */
+    DM_INFO_MESSAGE = 12,
+    DM_VERSION_1_MAX = 12,
+
+    /*
+     * Version 2.0
+     */
+    DM_MEM_HOT_REMOVE_REQUEST = 13,
+    DM_MEM_HOT_REMOVE_RESPONSE = 14
+};
+
+
+/*
+ * Structures defining the dynamic memory management
+ * protocol.
+ */
+
+union dm_version {
+    struct {
+        uint16_t minor_version;
+        uint16_t major_version;
+    };
+    uint32_t version;
+} QEMU_PACKED;
+
+
+union dm_caps {
+    struct {
+        uint64_t balloon:1;
+        uint64_t hot_add:1;
+        /*
+         * To support guests that may have alignment
+         * limitations on hot-add, the guest can specify
+         * its alignment requirements; a value of n
+         * represents an alignment of 2^n in mega bytes.
+         */
+        uint64_t hot_add_alignment:4;
+        uint64_t hot_remove:1;
+        uint64_t reservedz:57;
+    } cap_bits;
+    uint64_t caps;
+} QEMU_PACKED;
+
+union dm_mem_page_range {
+    struct  {
+        /*
+         * The PFN number of the first page in the range.
+         * 40 bits is the architectural limit of a PFN
+         * number for AMD64.
+         */
+        uint64_t start_page:40;
+        /*
+         * The number of pages in the range.
+         */
+        uint64_t page_cnt:24;
+    } finfo;
+    uint64_t  page_range;
+} QEMU_PACKED;
+
+
+
+/*
+ * The header for all dynamic memory messages:
+ *
+ * type: Type of the message.
+ * size: Size of the message in bytes; including the header.
+ * trans_id: The guest is responsible for manufacturing this ID.
+ */
+
+struct dm_header {
+    uint16_t type;
+    uint16_t size;
+    uint32_t trans_id;
+} QEMU_PACKED;
+
+/*
+ * A generic message format for dynamic memory.
+ * Specific message formats are defined later in the file.
+ */
+
+struct dm_message {
+    struct dm_header hdr;
+    uint8_t data[]; /* enclosed message */
+} QEMU_PACKED;
+
+
+/*
+ * Specific message types supporting the dynamic memory protocol.
+ */
+
+/*
+ * Version negotiation message. Sent from the guest to the host.
+ * The guest is free to try different versions until the host
+ * accepts the version.
+ *
+ * dm_version: The protocol version requested.
+ * is_last_attempt: If TRUE, this is the last version guest will request.
+ * reservedz: Reserved field, set to zero.
+ */
+
+struct dm_version_request {
+    struct dm_header hdr;
+    union dm_version version;
+    uint32_t is_last_attempt:1;
+    uint32_t reservedz:31;
+} QEMU_PACKED;
+
+/*
+ * Version response message; Host to Guest and indicates
+ * if the host has accepted the version sent by the guest.
+ *
+ * is_accepted: If TRUE, host has accepted the version and the guest
+ * should proceed to the next stage of the protocol. FALSE indicates that
+ * guest should re-try with a different version.
+ *
+ * reservedz: Reserved field, set to zero.
+ */
+
+struct dm_version_response {
+    struct dm_header hdr;
+    uint64_t is_accepted:1;
+    uint64_t reservedz:63;
+} QEMU_PACKED;
+
+/*
+ * Message reporting capabilities. This is sent from the guest to the
+ * host.
+ */
+
+struct dm_capabilities {
+    struct dm_header hdr;
+    union dm_caps caps;
+    uint64_t min_page_cnt;
+    uint64_t max_page_number;
+} QEMU_PACKED;
+
+/*
+ * Response to the capabilities message. This is sent from the host to the
+ * guest. This message notifies if the host has accepted the guest's
+ * capabilities. If the host has not accepted, the guest must shutdown
+ * the service.
+ *
+ * is_accepted: Indicates if the host has accepted guest's capabilities.
+ * reservedz: Must be 0.
+ */
+
+struct dm_capabilities_resp_msg {
+    struct dm_header hdr;
+    uint64_t is_accepted:1;
+    uint64_t hot_remove:1;
+    uint64_t suppress_pressure_reports:1;
+    uint64_t reservedz:61;
+} QEMU_PACKED;
+
+/*
+ * This message is used to report memory pressure from the guest.
+ * This message is not part of any transaction and there is no
+ * response to this message.
+ *
+ * num_avail: Available memory in pages.
+ * num_committed: Committed memory in pages.
+ * page_file_size: The accumulated size of all page files
+ *                 in the system in pages.
+ * zero_free: The nunber of zero and free pages.
+ * page_file_writes: The writes to the page file in pages.
+ * io_diff: An indicator of file cache efficiency or page file activity,
+ *          calculated as File Cache Page Fault Count - Page Read Count.
+ *          This value is in pages.
+ *
+ * Some of these metrics are Windows specific and fortunately
+ * the algorithm on the host side that computes the guest memory
+ * pressure only uses num_committed value.
+ */
+
+struct dm_status {
+    struct dm_header hdr;
+    uint64_t num_avail;
+    uint64_t num_committed;
+    uint64_t page_file_size;
+    uint64_t zero_free;
+    uint32_t page_file_writes;
+    uint32_t io_diff;
+} QEMU_PACKED;
+
+
+/*
+ * Message to ask the guest to allocate memory - balloon up message.
+ * This message is sent from the host to the guest. The guest may not be
+ * able to allocate as much memory as requested.
+ *
+ * num_pages: number of pages to allocate.
+ */
+
+struct dm_balloon {
+    struct dm_header hdr;
+    uint32_t num_pages;
+    uint32_t reservedz;
+} QEMU_PACKED;
+
+
+/*
+ * Balloon response message; this message is sent from the guest
+ * to the host in response to the balloon message.
+ *
+ * reservedz: Reserved; must be set to zero.
+ * more_pages: If FALSE, this is the last message of the transaction.
+ * if TRUE there will atleast one more message from the guest.
+ *
+ * range_count: The number of ranges in the range array.
+ *
+ * range_array: An array of page ranges returned to the host.
+ *
+ */
+
+struct dm_balloon_response {
+    struct dm_header hdr;
+    uint32_t reservedz;
+    uint32_t more_pages:1;
+    uint32_t range_count:31;
+    union dm_mem_page_range range_array[];
+} QEMU_PACKED;
+
+/*
+ * Un-balloon message; this message is sent from the host
+ * to the guest to give guest more memory.
+ *
+ * more_pages: If FALSE, this is the last message of the transaction.
+ * if TRUE there will atleast one more message from the guest.
+ *
+ * reservedz: Reserved; must be set to zero.
+ *
+ * range_count: The number of ranges in the range array.
+ *
+ * range_array: An array of page ranges returned to the host.
+ *
+ */
+
+struct dm_unballoon_request {
+    struct dm_header hdr;
+    uint32_t more_pages:1;
+    uint32_t reservedz:31;
+    uint32_t range_count;
+    union dm_mem_page_range range_array[];
+} QEMU_PACKED;
+
+/*
+ * Un-balloon response message; this message is sent from the guest
+ * to the host in response to an unballoon request.
+ *
+ */
+
+struct dm_unballoon_response {
+    struct dm_header hdr;
+} QEMU_PACKED;
+
+
+/*
+ * Hot add request message. Message sent from the host to the guest.
+ *
+ * mem_range: Memory range to hot add.
+ *
+ */
+
+struct dm_hot_add {
+    struct dm_header hdr;
+    union dm_mem_page_range range;
+} QEMU_PACKED;
+
+/*
+ * Hot add response message.
+ * This message is sent by the guest to report the status of a hot add request.
+ * If page_count is less than the requested page count, then the host should
+ * assume all further hot add requests will fail, since this indicates that
+ * the guest has hit an upper physical memory barrier.
+ *
+ * Hot adds may also fail due to low resources; in this case, the guest must
+ * not complete this message until the hot add can succeed, and the host must
+ * not send a new hot add request until the response is sent.
+ * If VSC fails to hot add memory DYNMEM_NUMBER_OF_UNSUCCESSFUL_HOTADD_ATTEMPTS
+ * times it fails the request.
+ *
+ *
+ * page_count: number of pages that were successfully hot added.
+ *
+ * result: result of the operation 1: success, 0: failure.
+ *
+ */
+
+struct dm_hot_add_response {
+    struct dm_header hdr;
+    uint32_t page_count;
+    uint32_t result;
+} QEMU_PACKED;
+
+struct dm_hot_remove {
+    struct dm_header hdr;
+    uint32_t virtual_node;
+    uint32_t page_count;
+    uint32_t qos_flags;
+    uint32_t reservedZ;
+} QEMU_PACKED;
+
+struct dm_hot_remove_response {
+    struct dm_header hdr;
+    uint32_t result;
+    uint32_t range_count;
+    uint64_t more_pages:1;
+    uint64_t reservedz:63;
+    union dm_mem_page_range range_array[];
+} QEMU_PACKED;
+
+#define DM_REMOVE_QOS_LARGE (1 << 0)
+#define DM_REMOVE_QOS_LOCAL (1 << 1)
+#define DM_REMOVE_QOS_MASK (0x3)
+
+/*
+ * Types of information sent from host to the guest.
+ */
+
+enum dm_info_type {
+    INFO_TYPE_MAX_PAGE_CNT = 0,
+    MAX_INFO_TYPE
+};
+
+
+/*
+ * Header for the information message.
+ */
+
+struct dm_info_header {
+    enum dm_info_type type;
+    uint32_t data_size;
+    uint8_t  data[];
+} QEMU_PACKED;
+
+/*
+ * This message is sent from the host to the guest to pass
+ * some relevant information (win8 addition).
+ *
+ * reserved: no used.
+ * info_size: size of the information blob.
+ * info: information blob.
+ */
+
+struct dm_info_msg {
+    struct dm_header hdr;
+    uint32_t reserved;
+    uint32_t info_size;
+    uint8_t  info[];
+};
+
+#endif
Acked-by on v7 3/7
#4 lore raw

Re: [PATCH v7 3/7] Add Hyper-V Dynamic Memory Protocol definitions

Quoted 8 lines · Maciej S. Szmigiero

From: "Maciej S. Szmigiero" <[email protected]>

This commit adds Hyper-V Dynamic Memory Protocol definitions, taken from hv_balloon Linux kernel driver, adapted to the QEMU coding style and definitions.

---

Signature · 3 lines
Cheers,

David / dhildenb
#5 lore raw

PATCH v7 4/7 Add a Hyper-V Dynamic Memory Protocol driver (hv-balloon)

In reply to Maciej S. Szmigiero

From: "Maciej S. Szmigiero" <[email protected]>

This driver is like virtio-balloon on steroids: it allows both changing the guest memory allocation via ballooning and inserting pieces of extra RAM into it on demand from a provided memory backend.

One of advantages of these over ACPI-based PC DIMM hotplug is that such memory can be hotplugged in much smaller granularity because the ACPI DIMM slot limit does not apply.

In order to enable hot-adding additional memory a new memory backend needs to be created and provided to the driver via the "memdev" parameter. This can be achieved by, for example, adding
"-object memory-backend-ram,id=mem1,size=32G" to the QEMU command line and then instantiating the driver with "memdev=mem1" parameter.

The device will try to use multiple memslots to cover the memory backend in order to reduce the size of metadata for the not-yet-hot-added part of the memory backend.

In contrast with ACPI DIMM hotplug where one can only request to unplug a whole DIMM stick this driver allows removing memory from guest in single page (4k) units via ballooning.

The actual resizing is done via ballooning interface (for example, via the "balloon" HMP command)
This includes resizing the guest past its boot size - that is, hot-adding additional memory in granularity limited only by the guest alignment requirements.

After a VM reboot the guest is back to its original (boot) size.

In the future, the guest boot memory size might be changed on reboot instead, taking into account the effective size that VM had before that reboot (much like Hyper-V does).

For performance reasons, the guest-released memory is tracked in a few range trees, as a series of (start, count) ranges.
Each time a new page range is inserted into such tree its neighbors are checked as candidates for possible merging with it.

Besides performance reasons, the Dynamic Memory protocol itself uses page ranges as the data structure in its messages, so relevant pages need to be merged into such ranges anyway.

One has to be careful when tracking the guest-released pages, since the guest can maliciously report returning pages outside its current address space, which later clash with the address range of newly added memory. Similarly, the guest can report freeing the same page twice.

The above design results in much better ballooning performance than when using virtio-balloon with the same guest: 230 GB / minute with this driver versus 70 GB / minute with virtio-balloon.

During a ballooning operation most of time is spent waiting for the guest to come up with newly freed page ranges, processing the received ranges on the host side (in QEMU and KVM) is nearly instantaneous.

The unballoon operation is also pretty much instantaneous:
thanks to the merging of the ballooned out page ranges 200 GB of memory can be returned to the guest in about 1 second.
With virtio-balloon this operation takes about 2.5 minutes.

These tests were done against a Windows Server 2019 guest running on a Xeon E5-2699, after dirtying the whole memory inside guest before each balloon operation.

Using a range tree instead of a bitmap to track the removed memory also means that the solution scales well with the guest size: even a 1 TB range takes just a few bytes of such metadata.

Since the required GTree operations aren't present in every Glib version a check for them was added to the meson build script, together with new "--enable-hv-balloon" and "--disable-hv-balloon" configure arguments. If these GTree operations are missing in the system's Glib version this driver will be skipped during QEMU build.

An optional "status-report=on" device parameter requests memory status events from the guest (typically sent every second), which allow the host to learn both the guest memory available and the guest memory in use counts.

Following commits will add support for their external emission as "HV_BALLOON_STATUS_REPORT" QMP events.

The driver is named hv-balloon since the Linux kernel client driver for the Dynamic Memory Protocol is named as such and to follow the naming pattern established by the virtio-balloon driver.
The whole protocol runs over Hyper-V VMBus.

The driver was tested against Windows Server 2012 R2, Windows Server 2016 and Windows Server 2019 guests and obeys the guest alignment requirements reported to the host via DM_CAPABILITIES_REPORT message.

---
 Kconfig.host                              |    3 +
 hw/hyperv/Kconfig                         |   10 +
 hw/hyperv/hv-balloon-internal.h           |   33 +
 hw/hyperv/hv-balloon-our_range_memslots.c |  201 +++
 hw/hyperv/hv-balloon-our_range_memslots.h |  110 ++
 hw/hyperv/hv-balloon-page_range_tree.c    |  228 +++
 hw/hyperv/hv-balloon-page_range_tree.h    |  118 ++
 hw/hyperv/hv-balloon.c                    | 1713 +++++++++++++++++++++
 hw/hyperv/meson.build                     |    1 +
 hw/hyperv/trace-events                    |   18 +
 include/hw/hyperv/hv-balloon.h            |   18 +
 meson.build                               |   28 +-
 meson_options.txt                         |    2 +
 scripts/meson-buildoptions.sh             |    3 +
 14 files changed, 2485 insertions(+), 1 deletion(-)
 create mode 100644 hw/hyperv/hv-balloon-internal.h
 create mode 100644 hw/hyperv/hv-balloon-our_range_memslots.c
 create mode 100644 hw/hyperv/hv-balloon-our_range_memslots.h
 create mode 100644 hw/hyperv/hv-balloon-page_range_tree.c
 create mode 100644 hw/hyperv/hv-balloon-page_range_tree.h
 create mode 100644 hw/hyperv/hv-balloon.c
 create mode 100644 include/hw/hyperv/hv-balloon.h
Kconfig.host +3 -0
index d763d892693c..2ee71578f38f 100644
@@ -46,3 +46,6 @@ config FUZZ
 config VFIO_USER_SERVER_ALLOWED
     bool
     imply VFIO_USER_SERVER
+
+config HV_BALLOON_POSSIBLE
+    bool
hw/hyperv/Kconfig +10 -0
index fcf65903bd05..41dd827c841b 100644
@@ -16,3 +16,13 @@ config SYNDBG
     bool
     default y
     depends on VMBUS
+
+config HV_BALLOON_SUPPORTED
+    bool
+
+config HV_BALLOON
+    bool
+    default y
+    depends on VMBUS
+    depends on HV_BALLOON_POSSIBLE
+    depends on HV_BALLOON_SUPPORTED
hw/hyperv/hv-balloon-internal.h new file +33 -0
new file mode 100644
index 000000000000..164c2e582539
@@ -0,0 +1,33 @@
+/*
+ * QEMU Hyper-V Dynamic Memory Protocol driver
+ *
+ * Copyright (C) 2020-2023 Oracle and/or its affiliates.
+ *
+ * This work is licensed under the terms of the GNU GPL, version 2 or later.
+ * See the COPYING file in the top-level directory.
+ */
+
+#ifndef HW_HYPERV_HV_BALLOON_INTERNAL_H
+#define HW_HYPERV_HV_BALLOON_INTERNAL_H
+
+#include "qemu/osdep.h"
+
+#define HV_BALLOON_PFN_SHIFT 12
+#define HV_BALLOON_PAGE_SIZE (1 << HV_BALLOON_PFN_SHIFT)
+
+#define SUM_OVERFLOW_U64(in1, in2) ((in1) > UINT64_MAX - (in2))
+#define SUM_SATURATE_U64(in1, in2)              \
+    ({                                          \
+        uint64_t _in1 = (in1), _in2 = (in2);    \
+        uint64_t _result;                       \
+                                                \
+        if (!SUM_OVERFLOW_U64(_in1, _in2)) {    \
+            _result = _in1 + _in2;              \
+        } else {                                \
+            _result = UINT64_MAX;               \
+        }                                       \
+                                                \
+        _result;                                \
+    })
+
+#endif
hw/hyperv/hv-balloon-our_range_memslots.c new file +201 -0

201 lines not on this page · raw message

hw/hyperv/hv-balloon-our_range_memslots.h new file +110 -0

110 lines not on this page · raw message

hw/hyperv/hv-balloon-page_range_tree.c new file +228 -0

228 lines not on this page · raw message

hw/hyperv/hv-balloon-page_range_tree.h new file +118 -0

118 lines not on this page · raw message

hw/hyperv/hv-balloon.c new file +1713 -0

1,713 lines not on this page · raw message

hw/hyperv/meson.build +1 -0
index b43f119ea56c..852d4f4a2ee2 100644
@@ -2,3 +2,4 @@ specific_ss.add(when: 'CONFIG_HYPERV', if_true: files('hyperv.c'))
 specific_ss.add(when: 'CONFIG_HYPERV_TESTDEV', if_true: files('hyperv_testdev.c'))
 specific_ss.add(when: 'CONFIG_VMBUS', if_true: files('vmbus.c'))
 specific_ss.add(when: 'CONFIG_SYNDBG', if_true: files('syndbg.c'))
+specific_ss.add(when: 'CONFIG_HV_BALLOON', if_true: files('hv-balloon.c', 'hv-balloon-page_range_tree.c', 'hv-balloon-our_range_memslots.c'))
hw/hyperv/trace-events +18 -0
index b4c35ca8e377..7963c215b1c5 100644
@@ -16,3 +16,21 @@ vmbus_gpadl_torndown(uint32_t gpadl_id) "gpadl #%d"
 vmbus_open_channel(uint32_t chan_id, uint32_t gpadl_id, uint32_t target_vp) "channel #%d gpadl #%d target vp %d"
 vmbus_channel_open(uint32_t chan_id, uint32_t status) "channel #%d status %d"
 vmbus_close_channel(uint32_t chan_id) "channel #%d"
+
+# hv-balloon
+hv_balloon_state_change(const char *tostr) "-> %s"
+hv_balloon_incoming_version(uint16_t major, uint16_t minor) "incoming proto version %u.%u"
+hv_balloon_incoming_caps(uint32_t caps) "incoming caps 0x%x"
+hv_balloon_outgoing_unballoon(uint32_t trans_id, uint64_t count, uint64_t start, uint64_t rempages) "posting unballoon %"PRIu32" for %"PRIu64" @ 0x%"PRIx64", remaining %"PRIu64
+hv_balloon_incoming_unballoon(uint32_t trans_id) "incoming unballoon response %"PRIu32
+hv_balloon_outgoing_hot_add(uint32_t trans_id, uint64_t count, uint64_t start) "posting hot add %"PRIu32" for %"PRIu64" @ 0x%"PRIx64
+hv_balloon_incoming_hot_add(uint32_t trans_id, uint32_t result, uint32_t count) "incoming hot add response %"PRIu32", result %"PRIu32", count %"PRIu32
+hv_balloon_outgoing_balloon(uint32_t trans_id, uint64_t count, uint64_t rempages) "posting balloon %"PRIu32" for %"PRIu64", remaining %"PRIu64
+hv_balloon_incoming_balloon(uint32_t trans_id, uint32_t range_count, uint32_t more_pages) "incoming balloon response %"PRIu32", ranges %"PRIu32", more %"PRIu32
+hv_balloon_our_range_add(uint64_t count, uint64_t start) "adding our range %"PRIu64" @ 0x%"PRIx64
+hv_balloon_remove_response(uint64_t count, uint64_t start, unsigned int both) "processing remove response range %"PRIu64" @ 0x%"PRIx64", both %u"
+hv_balloon_remove_response_hole(uint64_t counthole, uint64_t starthole, uint64_t countrange, uint64_t startrange, uint64_t starthpr, unsigned int both) "response range hole %"PRIu64" @ 0x%"PRIx64" from range %"PRIu64" @ 0x%"PRIx64", before our start 0x%"PRIx64", both %u"
+hv_balloon_remove_response_common(uint64_t countcommon, uint64_t startcommon, uint64_t countrange, uint64_t startrange, uint64_t counthpr, uint64_t starthpr, uint64_t removed, unsigned int both) "response common range %"PRIu64" @ 0x%"PRIx64" from range %"PRIu64" @ 0x%"PRIx64" with our %"PRIu64" @ 0x%"PRIx64", removed %"PRIu64", both %u"
+hv_balloon_remove_response_remainder(uint64_t count, uint64_t start, unsigned int both) "remove response remaining range %"PRIu64" @ 0x%"PRIx64", both %u"
+hv_balloon_map_slot(unsigned int idx, unsigned int total_slots, uint64_t offset) "mapping memslot %u / %u @ 0x%"PRIx64
+hv_balloon_unmap_slot(unsigned int idx, unsigned int total_slots, uint64_t offset) "unmapping memslot %u / %u @ 0x%"PRIx64
include/hw/hyperv/hv-balloon.h new file +18 -0
new file mode 100644
index 000000000000..c1efe70fc23f
@@ -0,0 +1,18 @@
+/*
+ * QEMU Hyper-V Dynamic Memory Protocol driver
+ *
+ * Copyright (C) 2020-2023 Oracle and/or its affiliates.
+ *
+ * This work is licensed under the terms of the GNU GPL, version 2 or later.
+ * See the COPYING file in the top-level directory.
+ */
+
+#ifndef HW_HV_BALLOON_H
+#define HW_HV_BALLOON_H
+
+#include "qom/object.h"
+
+#define TYPE_HV_BALLOON "hv-balloon"
+OBJECT_DECLARE_SIMPLE_TYPE(HvBalloon, HV_BALLOON)
+
+#endif
meson.build +27 -1
index 98e68ef0b1e1..410d6ab6a982 100644
@@ -1340,6 +1340,30 @@ if not get_option('glusterfs').auto() or have_block
   endif
 endif
 
+hv_balloon = false
+if get_option('hv_balloon').allowed() and have_system
+  if cc.links('''
+    #include <string.h>
+    #include <gmodule.h>
+    int main(void) {
+        GTree *tree;
+
+        tree = g_tree_new((GCompareFunc)strcmp);
+        (void)g_tree_node_first(tree);
+        g_tree_destroy(tree);
+        return 0;
+    }
+  ''', dependencies: glib)
+    hv_balloon = true
+  else
+    if get_option('hv_balloon').enabled()
+      error('could not enable hv-balloon, update your glib')
+    else
+      warning('could not find glib support for hv-balloon, disabling')
+    endif
+  endif
+endif
+
 libssh = not_found
 if not get_option('libssh').auto() or have_block
   libssh = dependency('libssh', version: '>=0.8.7',
@@ -2844,7 +2868,8 @@ host_kconfig = \
   ('CONFIG_LINUX' in config_host ? ['CONFIG_LINUX=y'] : []) + \
   (have_pvrdma ? ['CONFIG_PVRDMA=y'] : []) + \
   (multiprocess_allowed ? ['CONFIG_MULTIPROCESS_ALLOWED=y'] : []) + \
-  (vfio_user_server_allowed ? ['CONFIG_VFIO_USER_SERVER_ALLOWED=y'] : [])
+  (vfio_user_server_allowed ? ['CONFIG_VFIO_USER_SERVER_ALLOWED=y'] : []) + \
+  (hv_balloon ? ['CONFIG_HV_BALLOON_POSSIBLE=y'] : [])
 
 ignored = [ 'TARGET_XML_FILES', 'TARGET_ABI_DIR', 'TARGET_ARCH' ]
 
@@ -4299,6 +4324,7 @@ if targetos == 'windows'
 endif
 summary_info += {'seccomp support':   seccomp}
 summary_info += {'GlusterFS support': glusterfs}
+summary_info += {'hv-balloon support': hv_balloon}
 summary_info += {'TPM support':       have_tpm}
 summary_info += {'libssh support':    libssh}
 summary_info += {'lzo support':       lzo}
meson_options.txt +2 -0
index aaea5ddd7794..82559ca75bad 100644
@@ -142,6 +142,8 @@ option('gio', type : 'feature', value : 'auto',
        description: 'use libgio for D-Bus support')
 option('glusterfs', type : 'feature', value : 'auto',
        description: 'Glusterfs block device driver')
+option('hv_balloon', type : 'feature', value : 'auto',
+       description: 'hv-balloon driver (requires Glib 2.68+ GTree API)')
 option('libdw', type : 'feature', value : 'auto',
        description: 'debuginfo support')
 option('libiscsi', type : 'feature', value : 'auto',
scripts/meson-buildoptions.sh +3 -0
index 9da3fe299b75..2f88108deb53 100644
@@ -111,6 +111,7 @@ meson_options_help() {
   printf "%s\n" '  guest-agent     Build QEMU Guest Agent'
   printf "%s\n" '  guest-agent-msi Build MSI package for the QEMU Guest Agent'
   printf "%s\n" '  hax             HAX acceleration support'
+  printf "%s\n" '  hv-balloon      hv-balloon driver (requires Glib 2.68+ GTree API)'
   printf "%s\n" '  hvf             HVF acceleration support'
   printf "%s\n" '  iconv           Font glyph conversion support'
   printf "%s\n" '  jack            JACK sound support'
@@ -316,6 +317,8 @@ _meson_option_parse() {
     --disable-hax) printf "%s" -Dhax=disabled ;;
     --enable-hexagon-idef-parser) printf "%s" -Dhexagon_idef_parser=true ;;
     --disable-hexagon-idef-parser) printf "%s" -Dhexagon_idef_parser=false ;;
+    --enable-hv-balloon) printf "%s" -Dhv_balloon=enabled ;;
+    --disable-hv-balloon) printf "%s" -Dhv_balloon=disabled ;;
     --enable-hvf) printf "%s" -Dhvf=enabled ;;
     --disable-hvf) printf "%s" -Dhvf=disabled ;;
     --iasl=*) quote_sh "-Diasl=$2" ;;
#6 lore raw

PATCH v7 5/7 qapi: Add query-memory-devices support to hv-balloon

In reply to Maciej S. Szmigiero

From: "Maciej S. Szmigiero" <[email protected]>

Used by the driver to report its provided memory state information.

---
 hw/core/machine-hmp-cmds.c | 15 +++++++++++++++
 hw/hyperv/hv-balloon.c     | 27 +++++++++++++++++++++++++-
 qapi/machine.json          | 39 ++++++++++++++++++++++++++++++++++++--
 3 files changed, 78 insertions(+), 3 deletions(-)
hw/core/machine-hmp-cmds.c +15 -0
index c3e55ef9e9cd..7b06ed35decb 100644
@@ -247,6 +247,7 @@ void hmp_info_memory_devices(Monitor *mon, const QDict *qdict)
     MemoryDeviceInfo *value;
     PCDIMMDeviceInfo *di;
     SgxEPCDeviceInfo *se;
+    HvBalloonDeviceInfo *hi;
 
     for (info = info_list; info; info = info->next) {
         value = info->value;
@@ -304,6 +305,20 @@ void hmp_info_memory_devices(Monitor *mon, const QDict *qdict)
                 monitor_printf(mon, "  node: %" PRId64 "\n", se->node);
                 monitor_printf(mon, "  memdev: %s\n", se->memdev);
                 break;
+            case MEMORY_DEVICE_INFO_KIND_HV_BALLOON:
+                hi = value->u.hv_balloon.data;
+                monitor_printf(mon, "Memory device [%s]: \"%s\"\n",
+                               MemoryDeviceInfoKind_str(value->type),
+                               hi->id ? hi->id : "");
+                if (hi->has_memaddr) {
+                    monitor_printf(mon, "  memaddr: 0x%" PRIx64 "\n",
+                                   hi->memaddr);
+                }
+                monitor_printf(mon, "  max-size: %" PRIu64 "\n", hi->max_size);
+                if (hi->memdev) {
+                    monitor_printf(mon, "  memdev: %s\n", hi->memdev);
+                }
+                break;
             default:
                 g_assert_not_reached();
             }
hw/hyperv/hv-balloon.c +26 -1
index 4d87f99375b5..c384f23a3b5e 100644
@@ -1622,6 +1622,31 @@ static MemoryRegion *hv_balloon_md_get_memory_region(MemoryDeviceState *md,
     return balloon->mr;
 }
 
+static void hv_balloon_md_fill_device_info(const MemoryDeviceState *md,
+                                           MemoryDeviceInfo *info)
+{
+    HvBalloonDeviceInfo *hi = g_new0(HvBalloonDeviceInfo, 1);
+    const HvBalloon *balloon = HV_BALLOON(md);
+    DeviceState *dev = DEVICE(md);
+
+    if (dev->id) {
+        hi->id = g_strdup(dev->id);
+    }
+
+    if (balloon->hostmem) {
+        hi->memdev = object_get_canonical_path(OBJECT(balloon->hostmem));
+        hi->memaddr = balloon->addr;
+        hi->has_memaddr = true;
+        hi->max_size = memory_region_size(balloon->mr);
+        /* TODO: expose current provided size or something else? */
+    } else {
+        hi->max_size = 0;
+    }
+
+    info->u.hv_balloon.data = hi;
+    info->type = MEMORY_DEVICE_INFO_KIND_HV_BALLOON;
+}
+
 static void hv_balloon_decide_memslots(MemoryDeviceState *md,
                                        unsigned int limit)
 {
@@ -1709,5 +1734,5 @@ static void hv_balloon_class_init(ObjectClass *klass, void *data)
     mdc->get_memory_region = hv_balloon_md_get_memory_region;
     mdc->decide_memslots = hv_balloon_decide_memslots;
     mdc->get_memslots = hv_balloon_get_memslots;
-    /* implement fill_device_info */
+    mdc->fill_device_info = hv_balloon_md_fill_device_info;
 }
qapi/machine.json +37 -2
index a08b6576cac6..5ede977cf2bc 100644
@@ -1265,6 +1265,29 @@
           }
 }
 
+##
+# @HvBalloonDeviceInfo:
+#
+# hv-balloon provided memory state information
+#
+# @id: device's ID
+#
+# @memaddr: physical address in memory, where device is mapped
+#
+# @max-size: the maximum size of memory that the device can provide
+#
+# @memdev: memory backend linked with device
+#
+# Since: 8.2
+##
+{ 'struct': 'HvBalloonDeviceInfo',
+  'data': { '*id': 'str',
+            '*memaddr': 'size',
+            'max-size': 'size',
+            '*memdev': 'str'
+          }
+}
+
 ##
 # @MemoryDeviceInfoKind:
 #
@@ -1276,10 +1299,13 @@
 #
 # @sgx-epc: since 6.2.
 #
+# @hv-balloon: since 8.2.
+#
 # Since: 2.1
 ##
 { 'enum': 'MemoryDeviceInfoKind',
-  'data': [ 'dimm', 'nvdimm', 'virtio-pmem', 'virtio-mem', 'sgx-epc' ] }
+  'data': [ 'dimm', 'nvdimm', 'virtio-pmem', 'virtio-mem', 'sgx-epc',
+            'hv-balloon' ] }
 
 ##
 # @PCDIMMDeviceInfoWrapper:
@@ -1313,6 +1339,14 @@
 { 'struct': 'SgxEPCDeviceInfoWrapper',
   'data': { 'data': 'SgxEPCDeviceInfo' } }
 
+##
+# @HvBalloonDeviceInfoWrapper:
+#
+# Since: 8.2
+##
+{ 'struct': 'HvBalloonDeviceInfoWrapper',
+  'data': { 'data': 'HvBalloonDeviceInfo' } }
+
 ##
 # @MemoryDeviceInfo:
 #
@@ -1327,7 +1361,8 @@
             'nvdimm': 'PCDIMMDeviceInfoWrapper',
             'virtio-pmem': 'VirtioPMEMDeviceInfoWrapper',
             'virtio-mem': 'VirtioMEMDeviceInfoWrapper',
-            'sgx-epc': 'SgxEPCDeviceInfoWrapper'
+            'sgx-epc': 'SgxEPCDeviceInfoWrapper',
+            'hv-balloon': 'HvBalloonDeviceInfoWrapper'
           }
 }
 
Acked-by on v7 5/7
#7 lore raw

Re: [PATCH v7 5/7] qapi: Add query-memory-devices support to hv-balloon

Quoted 6 lines · Maciej S. Szmigiero

From: "Maciej S. Szmigiero" <[email protected]>

Used by the driver to report its provided memory state information.

[...]

Quoted 35 lines
qapi/machine.json +23 -0
index a08b6576cac6..5ede977cf2bc 100644
@@ -1265,6 +1265,29 @@
           }
 }
 
+##
+# @HvBalloonDeviceInfo:
+#
+# hv-balloon provided memory state information
+#
+# @id: device's ID
+#
+# @memaddr: physical address in memory, where device is mapped
+#
+# @max-size: the maximum size of memory that the device can provide
+#
+# @memdev: memory backend linked with device
+#
+# Since: 8.2
+##
+{ 'struct': 'HvBalloonDeviceInfo',
+  'data': { '*id': 'str',
+            '*memaddr': 'size',
+            'max-size': 'size',
+            '*memdev': 'str'
+          }
+}
+
 ##
 # @MemoryDeviceInfoKind:
 #

qapi/machine.json line 1303

Quoted 5 lines
qapi/machine.json +2 -0
@@ -1276,10 +1299,13 @@
 #
 # @sgx-epc: since 6.2.
 #
+# @hv-balloon: since 8.2.
+#

Not this patch's fault: documentation linking enum values and device types would be nice.

Quoted 25 lines
qapi/machine.json +10 -1
 # Since: 2.1
 ##
 { 'enum': 'MemoryDeviceInfoKind',
-  'data': [ 'dimm', 'nvdimm', 'virtio-pmem', 'virtio-mem', 'sgx-epc' ] }
+  'data': [ 'dimm', 'nvdimm', 'virtio-pmem', 'virtio-mem', 'sgx-epc',
+            'hv-balloon' ] }
 
 ##
 # @PCDIMMDeviceInfoWrapper:
@@ -1313,6 +1339,14 @@
 { 'struct': 'SgxEPCDeviceInfoWrapper',
   'data': { 'data': 'SgxEPCDeviceInfo' } }
 
+##
+# @HvBalloonDeviceInfoWrapper:
+#
+# Since: 8.2
+##
+{ 'struct': 'HvBalloonDeviceInfoWrapper',
+  'data': { 'data': 'HvBalloonDeviceInfo' } }
+
 ##
 # @MemoryDeviceInfo:
 #

qapi/machine.json line 1368

Quoted 9 lines
qapi/machine.json +2 -1
@@ -1327,7 +1361,8 @@
             'nvdimm': 'PCDIMMDeviceInfoWrapper',
             'virtio-pmem': 'VirtioPMEMDeviceInfoWrapper',
             'virtio-mem': 'VirtioMEMDeviceInfoWrapper',
-            'sgx-epc': 'SgxEPCDeviceInfoWrapper'
+            'sgx-epc': 'SgxEPCDeviceInfoWrapper',
+            'hv-balloon': 'HvBalloonDeviceInfoWrapper'
           }
 }
 
Reviewed-by on v7 5/7
#8 lore raw

Re: [PATCH v7 5/7] qapi: Add query-memory-devices support to hv-balloon

In reply to Maciej S. Szmigiero
Quoted 154 lines · Maciej S. Szmigiero

From: "Maciej S. Szmigiero" <[email protected]>

Used by the driver to report its provided memory state information.

---
  hw/core/machine-hmp-cmds.c | 15 +++++++++++++++
  hw/hyperv/hv-balloon.c     | 27 +++++++++++++++++++++++++-
  qapi/machine.json          | 39 ++++++++++++++++++++++++++++++++++++--
  3 files changed, 78 insertions(+), 3 deletions(-)
hw/core/machine-hmp-cmds.c +15 -0
index c3e55ef9e9cd..7b06ed35decb 100644
@@ -247,6 +247,7 @@ void hmp_info_memory_devices(Monitor *mon, const QDict *qdict)
      MemoryDeviceInfo *value;
      PCDIMMDeviceInfo *di;
      SgxEPCDeviceInfo *se;
+    HvBalloonDeviceInfo *hi;
 
      for (info = info_list; info; info = info->next) {
          value = info->value;
@@ -304,6 +305,20 @@ void hmp_info_memory_devices(Monitor *mon, const QDict *qdict)
                  monitor_printf(mon, "  node: %" PRId64 "\n", se->node);
                  monitor_printf(mon, "  memdev: %s\n", se->memdev);
                  break;
+            case MEMORY_DEVICE_INFO_KIND_HV_BALLOON:
+                hi = value->u.hv_balloon.data;
+                monitor_printf(mon, "Memory device [%s]: \"%s\"\n",
+                               MemoryDeviceInfoKind_str(value->type),
+                               hi->id ? hi->id : "");
+                if (hi->has_memaddr) {
+                    monitor_printf(mon, "  memaddr: 0x%" PRIx64 "\n",
+                                   hi->memaddr);
+                }
+                monitor_printf(mon, "  max-size: %" PRIu64 "\n", hi->max_size);
+                if (hi->memdev) {
+                    monitor_printf(mon, "  memdev: %s\n", hi->memdev);
+                }
+                break;
              default:
                  g_assert_not_reached();
              }
hw/hyperv/hv-balloon.c +26 -1
index 4d87f99375b5..c384f23a3b5e 100644
@@ -1622,6 +1622,31 @@ static MemoryRegion *hv_balloon_md_get_memory_region(MemoryDeviceState *md,
      return balloon->mr;
  }
 
+static void hv_balloon_md_fill_device_info(const MemoryDeviceState *md,
+                                           MemoryDeviceInfo *info)
+{
+    HvBalloonDeviceInfo *hi = g_new0(HvBalloonDeviceInfo, 1);
+    const HvBalloon *balloon = HV_BALLOON(md);
+    DeviceState *dev = DEVICE(md);
+
+    if (dev->id) {
+        hi->id = g_strdup(dev->id);
+    }
+
+    if (balloon->hostmem) {
+        hi->memdev = object_get_canonical_path(OBJECT(balloon->hostmem));
+        hi->memaddr = balloon->addr;
+        hi->has_memaddr = true;
+        hi->max_size = memory_region_size(balloon->mr);
+        /* TODO: expose current provided size or something else? */
+    } else {
+        hi->max_size = 0;
+    }
+
+    info->u.hv_balloon.data = hi;
+    info->type = MEMORY_DEVICE_INFO_KIND_HV_BALLOON;
+}
+
  static void hv_balloon_decide_memslots(MemoryDeviceState *md,
                                         unsigned int limit)
  {
@@ -1709,5 +1734,5 @@ static void hv_balloon_class_init(ObjectClass *klass, void *data)
      mdc->get_memory_region = hv_balloon_md_get_memory_region;
      mdc->decide_memslots = hv_balloon_decide_memslots;
      mdc->get_memslots = hv_balloon_get_memslots;
-    /* implement fill_device_info */
+    mdc->fill_device_info = hv_balloon_md_fill_device_info;
  }
qapi/machine.json +35 -1
index a08b6576cac6..5ede977cf2bc 100644
@@ -1265,6 +1265,29 @@
            }
  }
 
+##
+# @HvBalloonDeviceInfo:
+#
+# hv-balloon provided memory state information
+#
+# @id: device's ID
+#
+# @memaddr: physical address in memory, where device is mapped
+#
+# @max-size: the maximum size of memory that the device can provide
+#
+# @memdev: memory backend linked with device
+#
+# Since: 8.2
+##
+{ 'struct': 'HvBalloonDeviceInfo',
+  'data': { '*id': 'str',
+            '*memaddr': 'size',
+            'max-size': 'size',
+            '*memdev': 'str'
+          }
+}
+
  ##
  # @MemoryDeviceInfoKind:
  #
@@ -1276,10 +1299,13 @@
  #
  # @sgx-epc: since 6.2.
  #
+# @hv-balloon: since 8.2.
+#
  # Since: 2.1
  ##
  { 'enum': 'MemoryDeviceInfoKind',
-  'data': [ 'dimm', 'nvdimm', 'virtio-pmem', 'virtio-mem', 'sgx-epc' ] }
+  'data': [ 'dimm', 'nvdimm', 'virtio-pmem', 'virtio-mem', 'sgx-epc',
+            'hv-balloon' ] }
 
  ##
  # @PCDIMMDeviceInfoWrapper:
@@ -1313,6 +1339,14 @@
  { 'struct': 'SgxEPCDeviceInfoWrapper',
    'data': { 'data': 'SgxEPCDeviceInfo' } }
 
+##
+# @HvBalloonDeviceInfoWrapper:
+#
+# Since: 8.2
+##
+{ 'struct': 'HvBalloonDeviceInfoWrapper',
+  'data': { 'data': 'HvBalloonDeviceInfo' } }
+
  ##
  # @MemoryDeviceInfo:
  #

qapi/machine.json line 1368

Quoted 9 lines
qapi/machine.json +2 -1
@@ -1327,7 +1361,8 @@
              'nvdimm': 'PCDIMMDeviceInfoWrapper',
              'virtio-pmem': 'VirtioPMEMDeviceInfoWrapper',
              'virtio-mem': 'VirtioMEMDeviceInfoWrapper',
-            'sgx-epc': 'SgxEPCDeviceInfoWrapper'
+            'sgx-epc': 'SgxEPCDeviceInfoWrapper',
+            'hv-balloon': 'HvBalloonDeviceInfoWrapper'
            }
  }
 
Signature · 3 lines
Cheers,

David / dhildenb
#9 lore raw

PATCH v7 6/7 qapi: Add HV_BALLOON_STATUS_REPORT event and its QMP query command

In reply to Maciej S. Szmigiero

From: "Maciej S. Szmigiero" <[email protected]>

Used by the hv-balloon driver for (optional) guest memory status reports.

---
 hw/hyperv/hv-balloon.c | 31 +++++++++++++++++++-
 monitor/monitor.c      |  1 +
 qapi/machine.json      | 64 ++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 95 insertions(+), 1 deletion(-)
hw/hyperv/hv-balloon.c +30 -1
index c384f23a3b5e..d3f24721248a 100644
@@ -1099,7 +1099,36 @@ static void hv_balloon_handle_status_report(HvBalloon *balloon,
     balloon->status_report.available *= HV_BALLOON_PAGE_SIZE;
     balloon->status_report.received = true;
 
-    /* report event */
+    qapi_event_send_hv_balloon_status_report(balloon->status_report.committed,
+                                             balloon->status_report.available);
+}
+
+HvBalloonInfo *qmp_query_hv_balloon_status_report(Error **errp)
+{
+    HvBalloon *balloon;
+    HvBalloonInfo *info;
+
+    balloon = HV_BALLOON(object_resolve_path_type("", TYPE_HV_BALLOON, NULL));
+    if (!balloon) {
+        error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
+                  "no %s device present", TYPE_HV_BALLOON);
+        return NULL;
+    }
+
+    if (!balloon->status_report.enabled) {
+        error_setg(errp, "guest memory status reporting not enabled");
+        return NULL;
+    }
+
+    if (!balloon->status_report.received) {
+        error_setg(errp, "no guest memory status report received yet");
+        return NULL;
+    }
+
+    info = g_malloc0(sizeof(*info));
+    info->committed = balloon->status_report.committed;
+    info->available = balloon->status_report.available;
+    return info;
 }
 
 static void hv_balloon_handle_unballoon_response(HvBalloon *balloon,
monitor/monitor.c +1 -0
index dc352f9e9d95..81513c642691 100644
@@ -315,6 +315,7 @@ static MonitorQAPIEventConf monitor_qapi_event_conf[QAPI_EVENT__MAX] = {
     [QAPI_EVENT_QUORUM_FAILURE]    = { 1000 * SCALE_MS },
     [QAPI_EVENT_VSERPORT_CHANGE]   = { 1000 * SCALE_MS },
     [QAPI_EVENT_MEMORY_DEVICE_SIZE_CHANGE] = { 1000 * SCALE_MS },
+    [QAPI_EVENT_HV_BALLOON_STATUS_REPORT] = { 1000 * SCALE_MS },
 };
 
 /*
qapi/machine.json +64 -0

70 lines not on this page · raw message

#10 lore raw

Re: [PATCH v7 6/7] qapi: Add HV_BALLOON_STATUS_REPORT event and its QMP query command

Quoted 5 lines · Maciej S. Szmigiero

From: "Maciej S. Szmigiero" <[email protected]>

Used by the hv-balloon driver for (optional) guest memory status reports.

[...]

Quoted 38 lines

static void hv_balloon_handle_unballoon_response(HvBalloon *balloon,

monitor/monitor.c +1 -0
index dc352f9e9d95..81513c642691 100644
@@ -315,6 +315,7 @@ static MonitorQAPIEventConf monitor_qapi_event_conf[QAPI_EVENT__MAX] = {
     [QAPI_EVENT_QUORUM_FAILURE]    = { 1000 * SCALE_MS },
     [QAPI_EVENT_VSERPORT_CHANGE]   = { 1000 * SCALE_MS },
     [QAPI_EVENT_MEMORY_DEVICE_SIZE_CHANGE] = { 1000 * SCALE_MS },
+    [QAPI_EVENT_HV_BALLOON_STATUS_REPORT] = { 1000 * SCALE_MS },
 };
 
 /*
qapi/machine.json +17 -0
index 5ede977cf2bc..f592876964af 100644
@@ -1113,6 +1113,70 @@
 { 'event': 'BALLOON_CHANGE',
   'data': { 'actual': 'int' } }
 
+##
+# @HvBalloonInfo:
+#
+# hv-balloon guest-provided memory status information.
+#
+# @committed: the amount of memory in use inside the guest plus the
+#     amount of the memory unusable inside the guest (ballooned out,
+#     offline, etc.)
+#
+# @available: the amount of the memory inside the guest available for
+#     new allocations ("free")
+#
+# Since: 8.2
+##
+{ 'struct': 'HvBalloonInfo', 'data': { 'committed': 'size', 'available': 'size' } }
+
+##

qapi/machine.json line 1142

Quoted 10 lines
qapi/machine.json +10 -0
+# @query-hv-balloon-status-report:
+#
+# Returns the hv-balloon driver data contained in the last received "STATUS"
+# message from the guest.
+#
+# Returns:
+# - @HvBalloonInfo on success
+# - If no hv-balloon device is present, DeviceNotFound
+# - If guest memory status reporting not enabled or no guest memory status
+#     report received yet, GenericError

Indentation is off, confusing Sphinx.

Do you actually need to tell the two failures apart?

Do me a favour and break the lines a bit earlier, like

# Returns the hv-balloon driver data contained in the last received # "STATUS" message from the guest.
#
# Returns:
# - @HvBalloonInfo on success
# - If no hv-balloon device is present, DeviceNotFound
# - If guest memory status reporting not enabled or no guest memory
# status report received yet, GenericError

Quoted 11 lines
qapi/machine.json +11 -0
+#
+# Since: 8.2
+#
+# Example:
+#
+# -> { "execute": "query-hv-balloon-status-report" }
+# <- { "return": {
+#          "committed": 816640000,
+#          "available": 3333054464
+#       }
+#    }

qapi/machine.json line 1163

Quoted 10 lines
qapi/machine.json +10 -0
+##
+{ 'command': 'query-hv-balloon-status-report', 'returns': 'HvBalloonInfo' }
+
+##
+# @HV_BALLOON_STATUS_REPORT:
+#
+# Emitted when the hv-balloon driver receives a "STATUS" message from
+# the guest.
+#
+# @data - a @HvBalloonInfo equivalent to the one returned by the

This needs to be

# @data: ... text ...

to be rendered correctly.

Suggest

# @data: contents of "STATUS" message received from the guest.

Quoted 19 lines
qapi/machine.json +16 -0
+#     'query-hv-balloon-status-report' command.
+#
+# Note: this event is rate-limited.
+#
+# Since: 8.2
+#
+# Example:
+#
+# <- { "event": "HV_BALLOON_STATUS_REPORT",
+#      "data": { "committed": 816640000, "available": 3333054464 },
+#      "timestamp": { "seconds": 1600295492, "microseconds": 661044 } }
+#
+##
+{ 'event': 'HV_BALLOON_STATUS_REPORT',
+  'data': 'HvBalloonInfo' }
+
 ##
 # @MemoryInfo:
 #
#11 lore raw

Re: [PATCH v7 6/7] qapi: Add HV_BALLOON_STATUS_REPORT event and its QMP query command

Quoted 61 lines · Markus Armbruster
Quoted 5 lines · Maciej S. Szmigiero

From: "Maciej S. Szmigiero" <[email protected]>

Used by the hv-balloon driver for (optional) guest memory status reports.

[...]

Quoted 38 lines

static void hv_balloon_handle_unballoon_response(HvBalloon *balloon,

monitor/monitor.c +1 -0
index dc352f9e9d95..81513c642691 100644
@@ -315,6 +315,7 @@ static MonitorQAPIEventConf monitor_qapi_event_conf[QAPI_EVENT__MAX] = {
      [QAPI_EVENT_QUORUM_FAILURE]    = { 1000 * SCALE_MS },
      [QAPI_EVENT_VSERPORT_CHANGE]   = { 1000 * SCALE_MS },
      [QAPI_EVENT_MEMORY_DEVICE_SIZE_CHANGE] = { 1000 * SCALE_MS },
+    [QAPI_EVENT_HV_BALLOON_STATUS_REPORT] = { 1000 * SCALE_MS },
  };
 
  /*
qapi/machine.json +17 -0
index 5ede977cf2bc..f592876964af 100644
@@ -1113,6 +1113,70 @@
  { 'event': 'BALLOON_CHANGE',
    'data': { 'actual': 'int' } }
 
+##
+# @HvBalloonInfo:
+#
+# hv-balloon guest-provided memory status information.
+#
+# @committed: the amount of memory in use inside the guest plus the
+#     amount of the memory unusable inside the guest (ballooned out,
+#     offline, etc.)
+#
+# @available: the amount of the memory inside the guest available for
+#     new allocations ("free")
+#
+# Since: 8.2
+##
+{ 'struct': 'HvBalloonInfo', 'data': { 'committed': 'size', 'available': 'size' } }
+
+##

qapi/machine.json line 1142

Quoted 10 lines
qapi/machine.json +10 -0
+# @query-hv-balloon-status-report:
+#
+# Returns the hv-balloon driver data contained in the last received "STATUS"
+# message from the guest.
+#
+# Returns:
+# - @HvBalloonInfo on success
+# - If no hv-balloon device is present, DeviceNotFound
+# - If guest memory status reporting not enabled or no guest memory status
+#     report received yet, GenericError

Indentation is off, confusing Sphinx.

Quoted 1 line

Do you actually need to tell the two failures apart?

Technically no, it's just for the API consumer convenience.

Quoted 10 lines

Do me a favour and break the lines a bit earlier, like

# Returns the hv-balloon driver data contained in the last received # "STATUS" message from the guest.
#
# Returns:
# - @HvBalloonInfo on success
# - If no hv-balloon device is present, DeviceNotFound
# - If guest memory status reporting not enabled or no guest memory
# status report received yet, GenericError

Will do.

Quoted 24 lines
Quoted 11 lines
qapi/machine.json +11 -0
+#
+# Since: 8.2
+#
+# Example:
+#
+# -> { "execute": "query-hv-balloon-status-report" }
+# <- { "return": {
+#          "committed": 816640000,
+#          "available": 3333054464
+#       }
+#    }

qapi/machine.json line 1163

Quoted 10 lines
qapi/machine.json +10 -0
+##
+{ 'command': 'query-hv-balloon-status-report', 'returns': 'HvBalloonInfo' }
+
+##
+# @HV_BALLOON_STATUS_REPORT:
+#
+# Emitted when the hv-balloon driver receives a "STATUS" message from
+# the guest.
+#
+# @data - a @HvBalloonInfo equivalent to the one returned by the

This needs to be

Quoted 7 lines
    # @data: ... text ...

to be rendered correctly.

Suggest

# @data: contents of "STATUS" message received from the guest.

Will do.

Thanks,
Maciej

#12 lore raw

Re: [PATCH v7 6/7] qapi: Add HV_BALLOON_STATUS_REPORT event and its QMP query command

Quoted 36 lines · Maciej S. Szmigiero
Quoted 35 lines · Markus Armbruster
Quoted 5 lines · Maciej S. Szmigiero

From: "Maciej S. Szmigiero" <[email protected]>

Used by the hv-balloon driver for (optional) guest memory status reports.

[...]

Quoted 26 lines

static void hv_balloon_handle_unballoon_response(HvBalloon *balloon,

monitor/monitor.c +1 -0
index dc352f9e9d95..81513c642691 100644
@@ -315,6 +315,7 @@ static MonitorQAPIEventConf monitor_qapi_event_conf[QAPI_EVENT__MAX] = {
      [QAPI_EVENT_QUORUM_FAILURE]    = { 1000 * SCALE_MS },
      [QAPI_EVENT_VSERPORT_CHANGE]   = { 1000 * SCALE_MS },
      [QAPI_EVENT_MEMORY_DEVICE_SIZE_CHANGE] = { 1000 * SCALE_MS },
+    [QAPI_EVENT_HV_BALLOON_STATUS_REPORT] = { 1000 * SCALE_MS },
  };
    /*
qapi/machine.json +7 -0
index 5ede977cf2bc..f592876964af 100644
@@ -1113,6 +1113,70 @@
 { 'event': 'BALLOON_CHANGE',
   'data': { 'actual': 'int' } }
+##
+# @HvBalloonInfo:
+#
+# hv-balloon guest-provided memory status information.
+#
+# @committed: the amount of memory in use inside the guest plus the
+#     amount of the memory unusable inside the guest (ballooned out,
Quoted 8 lines
Quoted 8 lines

qapi/machine.json line 1130

Quoted 8 lines
qapi/machine.json +8 -0
+#     offline, etc.)
+#
+# @available: the amount of the memory inside the guest available for
+#     new allocations ("free")
+#
+# Since: 8.2
+##
+{ 'struct': 'HvBalloonInfo', 'data': { 'committed': 'size', 'available': 'size' } }

Long line. Wrap it like

{ 'struct': 'HvBalloonInfo',
'data': { 'committed': 'size', 'available': 'size' } }

Quoted 15 lines
Quoted 15 lines

qapi/machine.json line 1142

Quoted 12 lines
qapi/machine.json +12 -0
+
+##
+# @query-hv-balloon-status-report:
+#
+# Returns the hv-balloon driver data contained in the last received "STATUS"
+# message from the guest.
+#
+# Returns:
+# - @HvBalloonInfo on success
+# - If no hv-balloon device is present, DeviceNotFound
+# - If guest memory status reporting not enabled or no guest memory status
+#     report received yet, GenericError

Indentation is off, confusing Sphinx.

Quoted 3 lines
Quoted 1 line

Do you actually need to tell the two failures apart?

Technically no, it's just for the API consumer convenience.

Error classes are remnants of a failed error reporting design ("rich" error objects), and new code should stick to GenericError. Exceptions can be made when there's a proven need for distinguishing errors, or for consistency, say when extending an existing interface.

The commit burying "rich" errors:

de253f1491 qmp: switch to the new error format on the wire

Followup commits:

6ec46ad541 block: Fix block-set-write-threshold not to use funky error class f3cf80e805 vnc: Fix QMP change not to use funky error class 5b347c5410 block: Fix blockdev-backup not to use funky error class 71568864c4 qapi: Fix up references to long gone error classes

[...]

#13 lore raw

PATCH v7 7/7 hw/i386/pc: Support hv-balloon

In reply to Maciej S. Szmigiero

From: "Maciej S. Szmigiero" <[email protected]>

Add the necessary plumbing for the hv-balloon driver to the PC machine.

---
 hw/i386/Kconfig |  1 +
 hw/i386/pc.c    | 22 ++++++++++++++++++++++
 2 files changed, 23 insertions(+)
hw/i386/Kconfig +1 -0
index 9051083c1e78..349cf0d32fad 100644
@@ -45,6 +45,7 @@ config PC
     select ACPI_VMGENID
     select VIRTIO_PMEM_SUPPORTED
     select VIRTIO_MEM_SUPPORTED
+    select HV_BALLOON_SUPPORTED
 
 config PC_PCI
     bool
hw/i386/pc.c +22 -0
index 54838c0c411d..d979479cab5e 100644
@@ -28,6 +28,7 @@
 #include "hw/i386/pc.h"
 #include "hw/char/serial.h"
 #include "hw/char/parallel.h"
+#include "hw/hyperv/hv-balloon.h"
 #include "hw/i386/topology.h"
 #include "hw/i386/fw_cfg.h"
 #include "hw/i386/vmport.h"
@@ -93,6 +94,7 @@
 #include "hw/i386/kvm/xen_evtchn.h"
 #include "hw/i386/kvm/xen_gnttab.h"
 #include "hw/i386/kvm/xen_xenstore.h"
+#include "hw/mem/memory-device.h"
 #include "sysemu/replay.h"
 #include "target/i386/cpu.h"
 #include "e820_memory_layout.h"
@@ -1494,6 +1496,21 @@ static void pc_memory_unplug(HotplugHandler *hotplug_dev,
     error_propagate(errp, local_err);
 }
 
+static void pc_hv_balloon_pre_plug(HotplugHandler *hotplug_dev,
+                                   DeviceState *dev, Error **errp)
+{
+    /* The vmbus handler has no hotplug handler; we should never end up here. */
+    g_assert(!dev->hotplugged);
+    memory_device_pre_plug(MEMORY_DEVICE(dev), MACHINE(hotplug_dev), NULL,
+                           errp);
+}
+
+static void pc_hv_balloon_plug(HotplugHandler *hotplug_dev,
+                               DeviceState *dev, Error **errp)
+{
+    memory_device_plug(MEMORY_DEVICE(dev), MACHINE(hotplug_dev));
+}
+
 static void pc_machine_device_pre_plug_cb(HotplugHandler *hotplug_dev,
                                           DeviceState *dev, Error **errp)
 {
@@ -1524,6 +1541,8 @@ static void pc_machine_device_pre_plug_cb(HotplugHandler *hotplug_dev,
             return;
         }
         pcms->iommu = dev;
+    } else if (object_dynamic_cast(OBJECT(dev), TYPE_HV_BALLOON)) {
+        pc_hv_balloon_pre_plug(hotplug_dev, dev, errp);
     }
 }
 
@@ -1536,6 +1555,8 @@ static void pc_machine_device_plug_cb(HotplugHandler *hotplug_dev,
         x86_cpu_plug(hotplug_dev, dev, errp);
     } else if (object_dynamic_cast(OBJECT(dev), TYPE_VIRTIO_MD_PCI)) {
         virtio_md_pci_plug(VIRTIO_MD_PCI(dev), MACHINE(hotplug_dev), errp);
+    } else if (object_dynamic_cast(OBJECT(dev), TYPE_HV_BALLOON)) {
+        pc_hv_balloon_plug(hotplug_dev, dev, errp);
     }
 }
 
@@ -1577,6 +1598,7 @@ static HotplugHandler *pc_get_hotplug_handler(MachineState *machine,
         object_dynamic_cast(OBJECT(dev), TYPE_CPU) ||
         object_dynamic_cast(OBJECT(dev), TYPE_VIRTIO_MD_PCI) ||
         object_dynamic_cast(OBJECT(dev), TYPE_VIRTIO_IOMMU_PCI) ||
+        object_dynamic_cast(OBJECT(dev), TYPE_HV_BALLOON) ||
         object_dynamic_cast(OBJECT(dev), TYPE_X86_IOMMU_DEVICE)) {
         return HOTPLUG_HANDLER(machine);
     }
Reviewed-by on v7 7/7
#14 lore raw

Re: [PATCH v7 7/7] hw/i386/pc: Support hv-balloon

Quoted 52 lines · Maciej S. Szmigiero

From: "Maciej S. Szmigiero" <[email protected]>

Add the necessary plumbing for the hv-balloon driver to the PC machine.

---
  hw/i386/Kconfig |  1 +
  hw/i386/pc.c    | 22 ++++++++++++++++++++++
  2 files changed, 23 insertions(+)
hw/i386/Kconfig +1 -0
index 9051083c1e78..349cf0d32fad 100644
@@ -45,6 +45,7 @@ config PC
      select ACPI_VMGENID
      select VIRTIO_PMEM_SUPPORTED
      select VIRTIO_MEM_SUPPORTED
+    select HV_BALLOON_SUPPORTED
 
  config PC_PCI
      bool
hw/i386/pc.c +7 -0
index 54838c0c411d..d979479cab5e 100644
@@ -28,6 +28,7 @@
  #include "hw/i386/pc.h"
  #include "hw/char/serial.h"
  #include "hw/char/parallel.h"
+#include "hw/hyperv/hv-balloon.h"
  #include "hw/i386/topology.h"
  #include "hw/i386/fw_cfg.h"
  #include "hw/i386/vmport.h"
@@ -93,6 +94,7 @@
  #include "hw/i386/kvm/xen_evtchn.h"
  #include "hw/i386/kvm/xen_gnttab.h"
  #include "hw/i386/kvm/xen_xenstore.h"
+#include "hw/mem/memory-device.h"
  #include "sysemu/replay.h"
  #include "target/i386/cpu.h"
  #include "e820_memory_layout.h"
@@ -1494,6 +1496,21 @@ static void pc_memory_unplug(HotplugHandler *hotplug_dev,
      error_propagate(errp, local_err);
  }
 
+static void pc_hv_balloon_pre_plug(HotplugHandler *hotplug_dev,
+                                   DeviceState *dev, Error **errp)
+{
+    /* The vmbus handler has no hotplug handler; we should never end up here. */
+    g_assert(!dev->hotplugged);

hw/i386/pc.c static void pc_memory_unplug(HotplugHandler *hotplug_dev, · line 1513

Quoted 10 lines
hw/i386/pc.c +10 -0
+    memory_device_pre_plug(MEMORY_DEVICE(dev), MACHINE(hotplug_dev), NULL,
+                           errp);
+}
+
+static void pc_hv_balloon_plug(HotplugHandler *hotplug_dev,
+                               DeviceState *dev, Error **errp)
+{
+    memory_device_plug(MEMORY_DEVICE(dev), MACHINE(hotplug_dev));
+}
+

Maybe we want to have hv_balloon_pre_plug() and hv_balloon_plug(), but this here should be good enough for now.

Signature · 3 lines
Cheers,

David / dhildenb
#15 lore raw

Re: [PATCH v7 0/7] Hyper-V Dynamic Memory Protocol driver (hv-balloon 🎈)

In reply to Maciej S. Szmigiero
Quoted 24 lines · Maciej S. Szmigiero

From: "Maciej S. Szmigiero" <[email protected]>

This is a continuation of the v6 of the patch series located here:
https://lore.kernel.org/qemu-devel/[email protected]/

Changes from v6:
* Split the hv-balloon driver implementation into multiple files holding particular data structures and their methods in order to make the driver easier to understand.

* Split out the PC machine necessary plumbing for the driver and its final activation into a separate patch.

* Make sure that patches that bring QAPI-related changes also implement these in the driver in the same patch.

* Add a "query-hv-balloon-status-report" QMP command to query the data from the last received HV_BALLOON_STATUS_REPORT event.

* Rate limit the HV_BALLOON_STATUS_REPORT QMP event.

* Replace "TBD" in QAPI changes with the actual targeted QEMU version.

Quoted 7 lines

* Spelling and formatting fixes in QAPI changes.

* Rebase onto the latest David's patch series.

Based-on: <[email protected]>
Based-on-Repo-Commit: https://github.com/davidhildenbrand/qemu/tree/virtio-mem-memslots b65df116f8a8

That is upstream now. Do you have a new version in the works that further splits up #4? I recall we discussed somewhere separating the hotplug changes from the pure memory ballonning changes if possible.

Signature · 3 lines
Cheers,

David / dhildenb
#16 lore raw

Re: [PATCH v7 0/7] Hyper-V Dynamic Memory Protocol driver (hv-balloon 🎈)

Quoted 27 lines · David Hildenbrand
Quoted 26 lines · Maciej S. Szmigiero

From: "Maciej S. Szmigiero" <[email protected]>

This is a continuation of the v6 of the patch series located here:
https://lore.kernel.org/qemu-devel/[email protected]/

Changes from v6:
* Split the hv-balloon driver implementation into multiple files holding particular data structures and their methods in order to make the driver easier to understand.

* Split out the PC machine necessary plumbing for the driver and its final activation into a separate patch.

* Make sure that patches that bring QAPI-related changes also implement these in the driver in the same patch.

* Add a "query-hv-balloon-status-report" QMP command to query the data from the last received HV_BALLOON_STATUS_REPORT event.

* Rate limit the HV_BALLOON_STATUS_REPORT QMP event.

* Replace "TBD" in QAPI changes with the actual targeted QEMU version.

* Spelling and formatting fixes in QAPI changes.

Quoted 7 lines
Quoted 5 lines

* Rebase onto the latest David's patch series.

Based-on: <[email protected]>
Based-on-Repo-Commit: https://github.com/davidhildenbrand/qemu/tree/virtio-mem-memslots b65df116f8a8

That is upstream now.

That's great - I see it was pulled into QEMU git on Monday.

Quoted 2 lines

Do you have a new version in the works that further splits up #4?
I recall we discussed somewhere separating the hotplug changes from the pure memory ballonning changes if possible.

I will try to prepare an updated patch set next week, since unfortunately I am like 120% busy right now.

Thanks,
Maciej

#17 lore raw

Re: [PATCH v7 0/7] Hyper-V Dynamic Memory Protocol driver (hv-balloon 🎈)

Quoted 36 lines · Maciej S. Szmigiero
Quoted 27 lines · David Hildenbrand
Quoted 26 lines · Maciej S. Szmigiero

From: "Maciej S. Szmigiero" <[email protected]>

This is a continuation of the v6 of the patch series located here:
https://lore.kernel.org/qemu-devel/[email protected]/

Changes from v6:
* Split the hv-balloon driver implementation into multiple files holding particular data structures and their methods in order to make the driver easier to understand.

* Split out the PC machine necessary plumbing for the driver and its final activation into a separate patch.

* Make sure that patches that bring QAPI-related changes also implement these in the driver in the same patch.

* Add a "query-hv-balloon-status-report" QMP command to query the data from the last received HV_BALLOON_STATUS_REPORT event.

* Rate limit the HV_BALLOON_STATUS_REPORT QMP event.

* Replace "TBD" in QAPI changes with the actual targeted QEMU version.

* Spelling and formatting fixes in QAPI changes.

Quoted 7 lines
Quoted 5 lines

* Rebase onto the latest David's patch series.

Based-on: <[email protected]>
Based-on-Repo-Commit: https://github.com/davidhildenbrand/qemu/tree/virtio-mem-memslots b65df116f8a8

That is upstream now.

Quoted 7 lines

That's great - I see it was pulled into QEMU git on Monday.

Quoted 2 lines

Do you have a new version in the works that further splits up #4?
I recall we discussed somewhere separating the hotplug changes from the pure memory ballonning changes if possible.

I will try to prepare an updated patch set next week, since unfortunately I am like 120% busy right now.

I feel you. Soft-freeze starts on 2023-11-07, so there is still some time. In the worst case, 9.0 development should open around 2023-12-19.

Signature · 3 lines
Cheers,

David / dhildenb