diff mbox series

[v3,17/17] vfio/migration: Query device data size in vfio_save_pending()

Message ID 20221103161620.13120-18-avihaih@nvidia.com (mailing list archive)
State New, archived
Headers show
Series vfio/migration: Implement VFIO migration protocol v2 | expand

Commit Message

Avihai Horon Nov. 3, 2022, 4:16 p.m. UTC
Use VFIO_DEVICE_FEATURE_MIG_DATA_SIZE uAPI to query the device data size
and report it in vfio_save_pending() instead of the hardcoded value that
is currently used.

Signed-off-by: Avihai Horon <avihaih@nvidia.com>
---
 hw/vfio/migration.c        | 27 ++++++++++++++++++++-------
 linux-headers/linux/vfio.h | 13 +++++++++++++
 2 files changed, 33 insertions(+), 7 deletions(-)
diff mbox series

Patch

diff --git a/hw/vfio/migration.c b/hw/vfio/migration.c
index e8068b9147..8ade901383 100644
--- a/hw/vfio/migration.c
+++ b/hw/vfio/migration.c
@@ -225,14 +225,27 @@  static void vfio_save_pending(void *opaque, uint64_t threshold_size,
                               uint64_t *res_precopy, uint64_t *res_postcopy)
 {
     VFIODevice *vbasedev = opaque;
+    uint64_t buf[DIV_ROUND_UP(sizeof(struct vfio_device_feature) +
+                              sizeof(struct vfio_device_feature_mig_data_size),
+                              sizeof(uint64_t))] = {};
+    struct vfio_device_feature *feature = (void *)buf;
+    struct vfio_device_feature_mig_data_size *mig_data_size =
+        (void *)feature->data;
+
+    feature->argsz = sizeof(buf);
+    feature->flags =
+        VFIO_DEVICE_FEATURE_GET | VFIO_DEVICE_FEATURE_MIG_DATA_SIZE;
 
-    /*
-     * VFIO migration protocol v2 currently doesn't have an API to get pending
-     * device state size. Until such API is introduced, report some big
-     * arbitrary pending size so the device will be taken into account for
-     * downtime limit calculations.
-     */
-    *res_postcopy += VFIO_MIG_PENDING_SIZE;
+    if (ioctl(vbasedev->fd, VFIO_DEVICE_FEATURE, feature)) {
+        if (errno != ENOTTY) {
+            return;
+        }
+
+        /* Kernel doesn't support VFIO_DEVICE_FEATURE_MIG_DATA_SIZE */
+        *res_postcopy += VFIO_MIG_PENDING_SIZE;
+    } else {
+        *res_postcopy += mig_data_size->stop_copy_length;
+    }
 
     trace_vfio_save_pending(vbasedev->name, *res_precopy, *res_postcopy);
 }
diff --git a/linux-headers/linux/vfio.h b/linux-headers/linux/vfio.h
index ede44b5572..5c4ddf424f 100644
--- a/linux-headers/linux/vfio.h
+++ b/linux-headers/linux/vfio.h
@@ -986,6 +986,19 @@  enum vfio_device_mig_state {
 	VFIO_DEVICE_STATE_RUNNING_P2P = 5,
 };
 
+/*
+ * Upon VFIO_DEVICE_FEATURE_GET read back the estimated data length that will
+ * be required to complete stop copy.
+ *
+ * Note: Can be called on each device state.
+ */
+
+struct vfio_device_feature_mig_data_size {
+	__aligned_u64 stop_copy_length;
+};
+
+#define VFIO_DEVICE_FEATURE_MIG_DATA_SIZE 9
+
 /* -------- API for Type1 VFIO IOMMU -------- */
 
 /**