Skip to contentSkip to conversation

vfio: Add migration pre-copy support and device dirty tracking

PATCH Superseded Avihai Horon 5 participants 42 messages Jan 26, 2023 – Feb 15, 2023

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

cover vfio: Add migration pre-copy support and device dirty tracking

Hello,

This series is based on the previous one that added the basic VFIO migration protocol v2 implementation [1].

The first patch in the series starts by adding pre-copy support for VFIO migration protocol v2. Pre-copy support allows the VFIO device data to be transferred while the VM is running. This can improve performance and reduce migration downtime. Full description of it can be found here [2].

The series then moves on to implement device dirty page tracking. Device dirty page tracking allows the VFIO device to record its DMAs and report them back when needed. This is part of VFIO migration and is used during pre-copy phase of migration to track the RAM pages that the device has written to and mark those pages dirty, so they can later be re-sent to target.

Device dirty page tracking uses the DMA logging uAPI to discover device capabilities, to start and stop tracking, and to get dirty page bitmap report. Extra details and uAPI definition can be found here [3].

Device dirty page tracking operates in VFIOContainer scope. I.e., When dirty tracking is started, stopped or dirty page report is queried, all devices within a VFIOContainer are iterated and for each of them device dirty page tracking is started, stopped or dirty page report is queried, respectively.

Device dirty page tracking is used only if all devices within a VFIOContainer support it. Otherwise, VFIO IOMMU dirty page tracking is used, and if that is not supported as well, memory is perpetually marked dirty by QEMU. Note that since VFIO IOMMU dirty page tracking has no HW support, the last two usually have the same effect of perpetually marking all pages dirty.

Normally, when asked to start dirty tracking, all the currently DMA mapped ranges are tracked by device dirty page tracking. However, when vIOMMU is enabled IOVA ranges are DMA mapped/unmapped on the fly as the vIOMMU maps/unmaps them. These IOVA ranges can potentially be mapped anywhere in the vIOMMU IOVA space. Due to this dynamic nature of vIOMMU mapping/unmapping, tracking only the currently DMA mapped IOVA ranges doesn't work very well.

Thus, when vIOMMU is enabled, we try to track the entire vIOMMU IOVA space. If that fails (IOVA space can be rather big and we might hit HW limitation), we try to track smaller range while marking untracked ranges dirty.

Patch breakdown:
- Patch 1 adds VFIO migration pre-copy support.
- Patches 2-8 fix bugs and do some preparatory work required prior to adding device dirty page tracking.
- Patches 9-11 implement device dirty page tracking.
- Patches 12-16 add vIOMMU support to device dirty page tracking.
- Patches 17-18 enable device dirty page tracking and document it.

Thanks.

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

[2]
https://lore.kernel.org/kvm/[email protected]/

[3]
https://lore.kernel.org/netdev/[email protected]/

Avihai Horon (12):
vfio/migration: Add VFIO migration pre-copy support vfio/common: Fix error reporting in vfio_get_dirty_bitmap() vfio/common: Fix wrong %m usages
vfio/common: Abort migration if dirty log start/stop/sync fails vfio/common: Add VFIOBitmap and (de)alloc functions vfio/common: Extract code from vfio_get_dirty_bitmap() to new function vfio/common: Extract vIOMMU code from vfio_sync_dirty_bitmap() memory/iommu: Add IOMMU_ATTR_MAX_IOVA attribute
intel-iommu: Implement get_attr() method
vfio/common: Support device dirty page tracking with vIOMMU vfio/common: Optimize device dirty page tracking with vIOMMU docs/devel: Document VFIO device dirty page tracking

Joao Martins (6):
util: Add iova_tree_nnodes()
util: Extend iova_tree_foreach() to take data argument vfio/common: Record DMA mapped IOVA ranges vfio/common: Add device dirty page tracking start/stop vfio/common: Add device dirty page bitmap sync vfio/migration: Query device dirty page tracking support

 docs/devel/vfio-migration.rst |  79 ++--
 include/exec/memory.h         |   3 +-
 include/hw/vfio/vfio-common.h |  10 +
 include/qemu/iova-tree.h      |  19 +-
 hw/i386/intel_iommu.c         |  18 +
 hw/vfio/common.c              | 866 ++++++++++++++++++++++++++++++----
 hw/vfio/migration.c           | 127 ++++-
 util/iova-tree.c              |  23 +-
 hw/vfio/trace-events          |   5 +-
 9 files changed, 1006 insertions(+), 144 deletions(-)
Signature · 1 line
2.26.3
#1 lore raw

PATCH 1/18 vfio/migration: Add VFIO migration pre-copy support

Pre-copy support allows the VFIO device data to be transferred while the VM is running. This helps to accommodate VFIO devices that have a large amount of data that needs to be transferred, and it can reduce migration downtime.

Pre-copy support is optional in VFIO migration protocol v2. Implement pre-copy of VFIO migration protocol v2 and use it for devices that support it. Full description of it can be found here [1].

[1]
https://lore.kernel.org/kvm/[email protected]/

---
 docs/devel/vfio-migration.rst |  29 ++++++---
 include/hw/vfio/vfio-common.h |   3 +
 hw/vfio/common.c              |   8 ++-
 hw/vfio/migration.c           | 112 ++++++++++++++++++++++++++++++++--
 hw/vfio/trace-events          |   5 +-
 5 files changed, 140 insertions(+), 17 deletions(-)
docs/devel/vfio-migration.rst +20 -9
index 1d50c2fe5f..51f5e1a537 100644
@@ -7,12 +7,14 @@ the guest is running on source host and restoring this saved state on the
 destination host. This document details how saving and restoring of VFIO
 devices is done in QEMU.
 
-Migration of VFIO devices currently consists of a single stop-and-copy phase.
-During the stop-and-copy phase the guest is stopped and the entire VFIO device
-data is transferred to the destination.
-
-The pre-copy phase of migration is currently not supported for VFIO devices.
-Support for VFIO pre-copy will be added later on.
+Migration of VFIO devices consists of two phases: the optional pre-copy phase,
+and the stop-and-copy phase. The pre-copy phase is iterative and allows to
+accommodate VFIO devices that have a large amount of data that needs to be
+transferred. The iterative pre-copy phase of migration allows for the guest to
+continue whilst the VFIO device state is transferred to the destination, this
+helps to reduce the total downtime of the VM. VFIO devices can choose to skip
+the pre-copy phase of migration by not reporting the VFIO_MIGRATION_PRE_COPY
+flag in VFIO_DEVICE_FEATURE_MIGRATION ioctl.
 
 A detailed description of the UAPI for VFIO device migration can be found in
 the comment for the ``vfio_device_mig_state`` structure in the header file
@@ -29,6 +31,12 @@ VFIO implements the device hooks for the iterative approach as follows:
   driver, which indicates the amount of data that the vendor driver has yet to
   save for the VFIO device.
 
+* An ``is_active_iterate`` function that indicates ``save_live_iterate`` is
+  active only if the VFIO device is in pre-copy states.
+
+* A ``save_live_iterate`` function that reads the VFIO device's data from the
+  vendor driver during iterative phase.
+
 * A ``save_state`` function to save the device config space if it is present.
 
 * A ``save_live_complete_precopy`` function that sets the VFIO device in
@@ -91,8 +99,10 @@ Flow of state changes during Live migration
 ===========================================
 
 Below is the flow of state change during live migration.
-The values in the brackets represent the VM state, the migration state, and
+The values in the parentheses represent the VM state, the migration state, and
 the VFIO device state, respectively.
+The text in the square brackets represents the flow if the VFIO device supports
+pre-copy.
 
 Live migration save path
 ------------------------
@@ -104,11 +114,12 @@ Live migration save path
                                   |
                      migrate_init spawns migration_thread
                 Migration thread then calls each device's .save_setup()
-                       (RUNNING, _SETUP, _RUNNING)
+                  (RUNNING, _SETUP, _RUNNING [_PRE_COPY])
                                   |
-                      (RUNNING, _ACTIVE, _RUNNING)
+                  (RUNNING, _ACTIVE, _RUNNING [_PRE_COPY])
              If device is active, get pending_bytes by .save_live_pending()
           If total pending_bytes >= threshold_size, call .save_live_iterate()
+                  [Data of VFIO device for pre-copy phase is copied]
         Iterate till total pending bytes converge and are less than threshold
                                   |
   On migration completion, vCPU stops and calls .save_live_complete_precopy for
include/hw/vfio/vfio-common.h +3 -0
index 5f8e7a02fe..88c2194fb9 100644
@@ -67,7 +67,10 @@ typedef struct VFIOMigration {
     int data_fd;
     void *data_buffer;
     size_t data_buffer_size;
+    uint64_t mig_flags;
     uint64_t stop_copy_size;
+    uint64_t precopy_init_size;
+    uint64_t precopy_dirty_size;
 } VFIOMigration;
 
 typedef struct VFIOAddressSpace {
hw/vfio/common.c +6 -2
index 9a0dbee6b4..93b18c5e3d 100644
@@ -357,7 +357,9 @@ static bool vfio_devices_all_dirty_tracking(VFIOContainer *container)
 
             if ((vbasedev->pre_copy_dirty_page_tracking == ON_OFF_AUTO_OFF) &&
                 (migration->device_state == VFIO_DEVICE_STATE_RUNNING ||
-                 migration->device_state == VFIO_DEVICE_STATE_RUNNING_P2P)) {
+                 migration->device_state == VFIO_DEVICE_STATE_RUNNING_P2P ||
+                 migration->device_state == VFIO_DEVICE_STATE_PRE_COPY ||
+                 migration->device_state == VFIO_DEVICE_STATE_PRE_COPY_P2P)) {
                 return false;
             }
         }
@@ -387,7 +389,9 @@ static bool vfio_devices_all_running_and_mig_active(VFIOContainer *container)
             }
 
             if (migration->device_state == VFIO_DEVICE_STATE_RUNNING ||
-                migration->device_state == VFIO_DEVICE_STATE_RUNNING_P2P) {
+                migration->device_state == VFIO_DEVICE_STATE_RUNNING_P2P ||
+                migration->device_state == VFIO_DEVICE_STATE_PRE_COPY ||
+                migration->device_state == VFIO_DEVICE_STATE_PRE_COPY_P2P) {
                 continue;
             } else {
                 return false;
hw/vfio/migration.c +108 -4

193 lines not on this page · raw message

hw/vfio/trace-events +3 -2
index db9cb94952..37724579e3 100644
@@ -154,7 +154,7 @@ vfio_load_cleanup(const char *name) " (%s)"
 vfio_load_device_config_state(const char *name) " (%s)"
 vfio_load_state(const char *name, uint64_t data) " (%s) data 0x%"PRIx64
 vfio_load_state_device_data(const char *name, uint64_t data_size, int ret) " (%s) size 0x%"PRIx64" ret %d"
-vfio_migration_data_notifier(const char *name, uint64_t stopcopy_size) " (%s) stopcopy size 0x%"PRIx64
+vfio_migration_data_notifier(const char *name, uint64_t stopcopy_size, uint64_t precopy_init_size, uint64_t precopy_dirty_size) " (%s) stopcopy size 0x%"PRIx64" precopy initial size 0x%"PRIx64" precopy dirty size 0x%"PRIx64
 vfio_migration_probe(const char *name) " (%s)"
 vfio_migration_set_state(const char *name, const char *state) " (%s) state %s"
 vfio_migration_state_notifier(const char *name, const char *state) " (%s) state %s"
@@ -162,6 +162,7 @@ vfio_save_block(const char *name, int data_size) " (%s) data_size %d"
 vfio_save_cleanup(const char *name) " (%s)"
 vfio_save_complete_precopy(const char *name, int ret) " (%s) ret %d"
 vfio_save_device_config_state(const char *name) " (%s)"
-vfio_save_pending(const char *name, uint64_t precopy, uint64_t postcopy, uint64_t compatible, uint64_t stopcopy_size) " (%s) precopy 0x%"PRIx64" postcopy 0x%"PRIx64" compatible 0x%"PRIx64" stopcopy size 0x%"PRIx64
+vfio_save_iterate(const char *name) " (%s)"
+vfio_save_pending(const char *name, uint64_t precopy, uint64_t postcopy, uint64_t compatible, uint64_t stopcopy_size, uint64_t precopy_init_size, uint64_t precopy_dirty_size) " (%s) precopy 0x%"PRIx64" postcopy 0x%"PRIx64" compatible 0x%"PRIx64" stopcopy size 0x%"PRIx64" precopy initial size 0x%"PRIx64" precopy dirty size 0x%"PRIx64
 vfio_save_setup(const char *name, uint64_t data_buffer_size) " (%s) data buffer size 0x%"PRIx64
 vfio_vmstate_change(const char *name, int running, const char *reason, const char *dev_state) " (%s) running %d reason %s device state %s"
Signature · 1 line
2.26.3
#2 lore raw

Re: [PATCH 01/18] vfio/migration: Add VFIO migration pre-copy support

Quoted 106 lines · On Thu, 26 Jan 2023 20:49:31 +0200 Avihai Horon <[email protected]> wrote:

Pre-copy support allows the VFIO device data to be transferred while the VM is running. This helps to accommodate VFIO devices that have a large amount of data that needs to be transferred, and it can reduce migration downtime.

Pre-copy support is optional in VFIO migration protocol v2. Implement pre-copy of VFIO migration protocol v2 and use it for devices that support it. Full description of it can be found here [1].

[1]
https://lore.kernel.org/kvm/[email protected]/

---
 docs/devel/vfio-migration.rst |  29 ++++++---
 include/hw/vfio/vfio-common.h |   3 +
 hw/vfio/common.c              |   8 ++-
 hw/vfio/migration.c           | 112 ++++++++++++++++++++++++++++++++--
 hw/vfio/trace-events          |   5 +-
 5 files changed, 140 insertions(+), 17 deletions(-)
docs/devel/vfio-migration.rst +20 -9
index 1d50c2fe5f..51f5e1a537 100644
@@ -7,12 +7,14 @@ the guest is running on source host and restoring this saved state on the
 destination host. This document details how saving and restoring of VFIO
 devices is done in QEMU.
 
-Migration of VFIO devices currently consists of a single stop-and-copy phase.
-During the stop-and-copy phase the guest is stopped and the entire VFIO device
-data is transferred to the destination.
-
-The pre-copy phase of migration is currently not supported for VFIO devices.
-Support for VFIO pre-copy will be added later on.
+Migration of VFIO devices consists of two phases: the optional pre-copy phase,
+and the stop-and-copy phase. The pre-copy phase is iterative and allows to
+accommodate VFIO devices that have a large amount of data that needs to be
+transferred. The iterative pre-copy phase of migration allows for the guest to
+continue whilst the VFIO device state is transferred to the destination, this
+helps to reduce the total downtime of the VM. VFIO devices can choose to skip
+the pre-copy phase of migration by not reporting the VFIO_MIGRATION_PRE_COPY
+flag in VFIO_DEVICE_FEATURE_MIGRATION ioctl.
 
 A detailed description of the UAPI for VFIO device migration can be found in
 the comment for the ``vfio_device_mig_state`` structure in the header file
@@ -29,6 +31,12 @@ VFIO implements the device hooks for the iterative approach as follows:
   driver, which indicates the amount of data that the vendor driver has yet to
   save for the VFIO device.
 
+* An ``is_active_iterate`` function that indicates ``save_live_iterate`` is
+  active only if the VFIO device is in pre-copy states.
+
+* A ``save_live_iterate`` function that reads the VFIO device's data from the
+  vendor driver during iterative phase.
+
 * A ``save_state`` function to save the device config space if it is present.
 
 * A ``save_live_complete_precopy`` function that sets the VFIO device in
@@ -91,8 +99,10 @@ Flow of state changes during Live migration
 ===========================================
 
 Below is the flow of state change during live migration.
-The values in the brackets represent the VM state, the migration state, and
+The values in the parentheses represent the VM state, the migration state, and
 the VFIO device state, respectively.
+The text in the square brackets represents the flow if the VFIO device supports
+pre-copy.
 
 Live migration save path
 ------------------------
@@ -104,11 +114,12 @@ Live migration save path
                                   |
                      migrate_init spawns migration_thread
                 Migration thread then calls each device's .save_setup()
-                       (RUNNING, _SETUP, _RUNNING)
+                  (RUNNING, _SETUP, _RUNNING [_PRE_COPY])
                                   |
-                      (RUNNING, _ACTIVE, _RUNNING)
+                  (RUNNING, _ACTIVE, _RUNNING [_PRE_COPY])
              If device is active, get pending_bytes by .save_live_pending()
           If total pending_bytes >= threshold_size, call .save_live_iterate()
+                  [Data of VFIO device for pre-copy phase is copied]
         Iterate till total pending bytes converge and are less than threshold
                                   |
   On migration completion, vCPU stops and calls .save_live_complete_precopy for
include/hw/vfio/vfio-common.h +3 -0
index 5f8e7a02fe..88c2194fb9 100644
@@ -67,7 +67,10 @@ typedef struct VFIOMigration {
     int data_fd;
     void *data_buffer;
     size_t data_buffer_size;
+    uint64_t mig_flags;
     uint64_t stop_copy_size;
+    uint64_t precopy_init_size;
+    uint64_t precopy_dirty_size;
 } VFIOMigration;
 
 typedef struct VFIOAddressSpace {
hw/vfio/common.c +0 -0
index 9a0dbee6b4..93b18c5e3d 100644

hw/vfio/common.c static bool vfio_devices_all_dirty_tracking(VFIOContainer *container) · line 362

Quoted 7 lines
hw/vfio/common.c +3 -1
@@ -357,7 +357,9 @@ static bool vfio_devices_all_dirty_tracking(VFIOContainer *container)
 
             if ((vbasedev->pre_copy_dirty_page_tracking == ON_OFF_AUTO_OFF) &&
                 (migration->device_state == VFIO_DEVICE_STATE_RUNNING ||
-                 migration->device_state == VFIO_DEVICE_STATE_RUNNING_P2P)) {
+                 migration->device_state == VFIO_DEVICE_STATE_RUNNING_P2P ||
+                 migration->device_state == VFIO_DEVICE_STATE_PRE_COPY ||
+                 migration->device_state == VFIO_DEVICE_STATE_PRE_COPY_P2P)) {

Should this just turn into a test that we're not in STOP_COPY?

Quoted 4 lines
hw/vfio/common.c +0 -0
                 return false;
             }
         }

hw/vfio/common.c static bool vfio_devices_all_running_and_mig_active(VFIOContainer *container) · line 397

Quoted 10 lines
hw/vfio/common.c +3 -1
@@ -387,7 +389,9 @@ static bool vfio_devices_all_running_and_mig_active(VFIOContainer *container)
             }
 
             if (migration->device_state == VFIO_DEVICE_STATE_RUNNING ||
-                migration->device_state == VFIO_DEVICE_STATE_RUNNING_P2P) {
+                migration->device_state == VFIO_DEVICE_STATE_RUNNING_P2P ||
+                migration->device_state == VFIO_DEVICE_STATE_PRE_COPY ||
+                migration->device_state == VFIO_DEVICE_STATE_PRE_COPY_P2P) {
                 continue;
             } else {
                 return false;

Hmm, this only seems to highlight that between this series and the previous, we're adding tests for states that we never actually use, ie. these _P2P states.

IIRC, the reason we have these _P2P states is so that we can transition a set of devices, which may have active P2P DMA between them, to STOP, STOP_COPY, and even RUNNING states safely without lost data given that we cannot simultaneously transition all devices. That suggest that missing from both these series is support for bringing all devices to these _P2P states before we move any device to one of STOP, STOP_COPY, or RUNNING states (in the case of RESUMING).

Also, I recall discussions that we need to enforce configuration restrictions when not all devices support the _P2P states? For example adding a migration blocker when there are multiple vfio devices and at least one of them does not support _P2P migration states. Or perhaps initially, requiring support for _P2P states.

I think what's implemented here, where we don't make use of the _P2P states would require adding a migration blocker whenever there are multiple vfio devices, regardless of the device support for _P2P. Thanks,

Alex

#3 lore raw

Re: [PATCH 01/18] vfio/migration: Add VFIO migration pre-copy support

Quoted 113 lines · Alex Williamson

External email: Use caution opening links or attachments

Quoted 107 lines · On Thu, 26 Jan 2023 20:49:31 +0200 Avihai Horon <[email protected]> wrote:

Pre-copy support allows the VFIO device data to be transferred while the VM is running. This helps to accommodate VFIO devices that have a large amount of data that needs to be transferred, and it can reduce migration downtime.

Pre-copy support is optional in VFIO migration protocol v2. Implement pre-copy of VFIO migration protocol v2 and use it for devices that support it. Full description of it can be found here [1].

[1]
https://lore.kernel.org/kvm/[email protected]/

---
  docs/devel/vfio-migration.rst |  29 ++++++---
  include/hw/vfio/vfio-common.h |   3 +
  hw/vfio/common.c              |   8 ++-
  hw/vfio/migration.c           | 112 ++++++++++++++++++++++++++++++++--
  hw/vfio/trace-events          |   5 +-
  5 files changed, 140 insertions(+), 17 deletions(-)
docs/devel/vfio-migration.rst +20 -9
index 1d50c2fe5f..51f5e1a537 100644
@@ -7,12 +7,14 @@ the guest is running on source host and restoring this saved state on the
  destination host. This document details how saving and restoring of VFIO
  devices is done in QEMU.
 
-Migration of VFIO devices currently consists of a single stop-and-copy phase.
-During the stop-and-copy phase the guest is stopped and the entire VFIO device
-data is transferred to the destination.
-
-The pre-copy phase of migration is currently not supported for VFIO devices.
-Support for VFIO pre-copy will be added later on.
+Migration of VFIO devices consists of two phases: the optional pre-copy phase,
+and the stop-and-copy phase. The pre-copy phase is iterative and allows to
+accommodate VFIO devices that have a large amount of data that needs to be
+transferred. The iterative pre-copy phase of migration allows for the guest to
+continue whilst the VFIO device state is transferred to the destination, this
+helps to reduce the total downtime of the VM. VFIO devices can choose to skip
+the pre-copy phase of migration by not reporting the VFIO_MIGRATION_PRE_COPY
+flag in VFIO_DEVICE_FEATURE_MIGRATION ioctl.
 
  A detailed description of the UAPI for VFIO device migration can be found in
  the comment for the ``vfio_device_mig_state`` structure in the header file
@@ -29,6 +31,12 @@ VFIO implements the device hooks for the iterative approach as follows:
    driver, which indicates the amount of data that the vendor driver has yet to
    save for the VFIO device.
 
+* An ``is_active_iterate`` function that indicates ``save_live_iterate`` is
+  active only if the VFIO device is in pre-copy states.
+
+* A ``save_live_iterate`` function that reads the VFIO device's data from the
+  vendor driver during iterative phase.
+
  * A ``save_state`` function to save the device config space if it is present.
 
  * A ``save_live_complete_precopy`` function that sets the VFIO device in
@@ -91,8 +99,10 @@ Flow of state changes during Live migration
  ===========================================
 
  Below is the flow of state change during live migration.
-The values in the brackets represent the VM state, the migration state, and
+The values in the parentheses represent the VM state, the migration state, and
  the VFIO device state, respectively.
+The text in the square brackets represents the flow if the VFIO device supports
+pre-copy.
 
  Live migration save path
  ------------------------
@@ -104,11 +114,12 @@ Live migration save path
                                    |
                       migrate_init spawns migration_thread
                  Migration thread then calls each device's .save_setup()
-                       (RUNNING, _SETUP, _RUNNING)
+                  (RUNNING, _SETUP, _RUNNING [_PRE_COPY])
                                    |
-                      (RUNNING, _ACTIVE, _RUNNING)
+                  (RUNNING, _ACTIVE, _RUNNING [_PRE_COPY])
               If device is active, get pending_bytes by .save_live_pending()
            If total pending_bytes >= threshold_size, call .save_live_iterate()
+                  [Data of VFIO device for pre-copy phase is copied]
          Iterate till total pending bytes converge and are less than threshold
                                    |
    On migration completion, vCPU stops and calls .save_live_complete_precopy for
include/hw/vfio/vfio-common.h +3 -0
index 5f8e7a02fe..88c2194fb9 100644
@@ -67,7 +67,10 @@ typedef struct VFIOMigration {
      int data_fd;
      void *data_buffer;
      size_t data_buffer_size;
+    uint64_t mig_flags;
      uint64_t stop_copy_size;
+    uint64_t precopy_init_size;
+    uint64_t precopy_dirty_size;
  } VFIOMigration;
 
  typedef struct VFIOAddressSpace {
hw/vfio/common.c +0 -0
index 9a0dbee6b4..93b18c5e3d 100644
@@ -357,7 +357,9 @@ static bool vfio_devices_all_dirty_tracking(VFIOContainer *container)
 
Quoted 7 lines

hw/vfio/common.c static bool vfio_devices_all_dirty_tracking(VFIOContainer *container) · line 362

Quoted 6 lines
hw/vfio/common.c +3 -1
              if ((vbasedev->pre_copy_dirty_page_tracking == ON_OFF_AUTO_OFF) &&
                  (migration->device_state == VFIO_DEVICE_STATE_RUNNING ||
-                 migration->device_state == VFIO_DEVICE_STATE_RUNNING_P2P)) {
+                 migration->device_state == VFIO_DEVICE_STATE_RUNNING_P2P ||
+                 migration->device_state == VFIO_DEVICE_STATE_PRE_COPY ||
+                 migration->device_state == VFIO_DEVICE_STATE_PRE_COPY_P2P)) {

Should this just turn into a test that we're not in STOP_COPY?

Then we would need to check we are not in STOP_COPY and not in STOP.
The STOP check is for the case where PRE_COPY is not supported, since RAM will ask for dirty page sync when the device is in STOP state. Without the STOP check, the device will skip the final dirty page sync.

Quoted 33 lines
Quoted 5 lines
hw/vfio/common.c +0 -0
                  return false;
              }
          }

hw/vfio/common.c static bool vfio_devices_all_running_and_mig_active(VFIOContainer *container) · line 397

Quoted 10 lines
hw/vfio/common.c +3 -1
@@ -387,7 +389,9 @@ static bool vfio_devices_all_running_and_mig_active(VFIOContainer *container)
              }
 
              if (migration->device_state == VFIO_DEVICE_STATE_RUNNING ||
-                migration->device_state == VFIO_DEVICE_STATE_RUNNING_P2P) {
+                migration->device_state == VFIO_DEVICE_STATE_RUNNING_P2P ||
+                migration->device_state == VFIO_DEVICE_STATE_PRE_COPY ||
+                migration->device_state == VFIO_DEVICE_STATE_PRE_COPY_P2P) {
                  continue;
              } else {
                  return false;

Hmm, this only seems to highlight that between this series and the previous, we're adding tests for states that we never actually use, ie. these _P2P states.

IIRC, the reason we have these _P2P states is so that we can transition a set of devices, which may have active P2P DMA between them, to STOP, STOP_COPY, and even RUNNING states safely without lost data given that we cannot simultaneously transition all devices. That suggest that missing from both these series is support for bringing all devices to these _P2P states before we move any device to one of STOP, STOP_COPY, or RUNNING states (in the case of RESUMING).

Also, I recall discussions that we need to enforce configuration restrictions when not all devices support the _P2P states? For example adding a migration blocker when there are multiple vfio devices and at least one of them does not support _P2P migration states. Or perhaps initially, requiring support for _P2P states.

Quoted 3 lines

I think what's implemented here, where we don't make use of the _P2P states would require adding a migration blocker whenever there are multiple vfio devices, regardless of the device support for _P2P.

Yes, I think you are right.

I will add a migration blocker if there are multiple devices as part of v9 of the basic series. When P2P support is added, I will block migration of multiple devices if one or more of them doesn't support P2P.

Thanks.

#4 lore raw

Re: [PATCH 01/18] vfio/migration: Add VFIO migration pre-copy support

Quoted 164 lines · On Tue, 31 Jan 2023 14:44:54 +0200 Avihai Horon <[email protected]> wrote:
Quoted 113 lines · Alex Williamson

External email: Use caution opening links or attachments

Quoted 107 lines · On Thu, 26 Jan 2023 20:49:31 +0200 Avihai Horon <[email protected]> wrote:

Pre-copy support allows the VFIO device data to be transferred while the VM is running. This helps to accommodate VFIO devices that have a large amount of data that needs to be transferred, and it can reduce migration downtime.

Pre-copy support is optional in VFIO migration protocol v2. Implement pre-copy of VFIO migration protocol v2 and use it for devices that support it. Full description of it can be found here [1].

[1]
https://lore.kernel.org/kvm/[email protected]/

---
  docs/devel/vfio-migration.rst |  29 ++++++---
  include/hw/vfio/vfio-common.h |   3 +
  hw/vfio/common.c              |   8 ++-
  hw/vfio/migration.c           | 112 ++++++++++++++++++++++++++++++++--
  hw/vfio/trace-events          |   5 +-
  5 files changed, 140 insertions(+), 17 deletions(-)
docs/devel/vfio-migration.rst +20 -9
index 1d50c2fe5f..51f5e1a537 100644
@@ -7,12 +7,14 @@ the guest is running on source host and restoring this saved state on the
  destination host. This document details how saving and restoring of VFIO
  devices is done in QEMU.
 
-Migration of VFIO devices currently consists of a single stop-and-copy phase.
-During the stop-and-copy phase the guest is stopped and the entire VFIO device
-data is transferred to the destination.
-
-The pre-copy phase of migration is currently not supported for VFIO devices.
-Support for VFIO pre-copy will be added later on.
+Migration of VFIO devices consists of two phases: the optional pre-copy phase,
+and the stop-and-copy phase. The pre-copy phase is iterative and allows to
+accommodate VFIO devices that have a large amount of data that needs to be
+transferred. The iterative pre-copy phase of migration allows for the guest to
+continue whilst the VFIO device state is transferred to the destination, this
+helps to reduce the total downtime of the VM. VFIO devices can choose to skip
+the pre-copy phase of migration by not reporting the VFIO_MIGRATION_PRE_COPY
+flag in VFIO_DEVICE_FEATURE_MIGRATION ioctl.
 
  A detailed description of the UAPI for VFIO device migration can be found in
  the comment for the ``vfio_device_mig_state`` structure in the header file
@@ -29,6 +31,12 @@ VFIO implements the device hooks for the iterative approach as follows:
    driver, which indicates the amount of data that the vendor driver has yet to
    save for the VFIO device.
 
+* An ``is_active_iterate`` function that indicates ``save_live_iterate`` is
+  active only if the VFIO device is in pre-copy states.
+
+* A ``save_live_iterate`` function that reads the VFIO device's data from the
+  vendor driver during iterative phase.
+
  * A ``save_state`` function to save the device config space if it is present.
 
  * A ``save_live_complete_precopy`` function that sets the VFIO device in
@@ -91,8 +99,10 @@ Flow of state changes during Live migration
  ===========================================
 
  Below is the flow of state change during live migration.
-The values in the brackets represent the VM state, the migration state, and
+The values in the parentheses represent the VM state, the migration state, and
  the VFIO device state, respectively.
+The text in the square brackets represents the flow if the VFIO device supports
+pre-copy.
 
  Live migration save path
  ------------------------
@@ -104,11 +114,12 @@ Live migration save path
                                    |
                       migrate_init spawns migration_thread
                  Migration thread then calls each device's .save_setup()
-                       (RUNNING, _SETUP, _RUNNING)
+                  (RUNNING, _SETUP, _RUNNING [_PRE_COPY])
                                    |
-                      (RUNNING, _ACTIVE, _RUNNING)
+                  (RUNNING, _ACTIVE, _RUNNING [_PRE_COPY])
               If device is active, get pending_bytes by .save_live_pending()
            If total pending_bytes >= threshold_size, call .save_live_iterate()
+                  [Data of VFIO device for pre-copy phase is copied]
          Iterate till total pending bytes converge and are less than threshold
                                    |
    On migration completion, vCPU stops and calls .save_live_complete_precopy for
include/hw/vfio/vfio-common.h +3 -0
index 5f8e7a02fe..88c2194fb9 100644
@@ -67,7 +67,10 @@ typedef struct VFIOMigration {
      int data_fd;
      void *data_buffer;
      size_t data_buffer_size;
+    uint64_t mig_flags;
      uint64_t stop_copy_size;
+    uint64_t precopy_init_size;
+    uint64_t precopy_dirty_size;
  } VFIOMigration;
 
  typedef struct VFIOAddressSpace {
hw/vfio/common.c +0 -0
index 9a0dbee6b4..93b18c5e3d 100644
@@ -357,7 +357,9 @@ static bool vfio_devices_all_dirty_tracking(VFIOContainer *container)
 
Quoted 7 lines

hw/vfio/common.c static bool vfio_devices_all_dirty_tracking(VFIOContainer *container) · line 362

Quoted 6 lines
hw/vfio/common.c +3 -1
              if ((vbasedev->pre_copy_dirty_page_tracking == ON_OFF_AUTO_OFF) &&
                  (migration->device_state == VFIO_DEVICE_STATE_RUNNING ||
-                 migration->device_state == VFIO_DEVICE_STATE_RUNNING_P2P)) {
+                 migration->device_state == VFIO_DEVICE_STATE_RUNNING_P2P ||
+                 migration->device_state == VFIO_DEVICE_STATE_PRE_COPY ||
+                 migration->device_state == VFIO_DEVICE_STATE_PRE_COPY_P2P)) {  

Should this just turn into a test that we're not in STOP_COPY?

Then we would need to check we are not in STOP_COPY and not in STOP. The STOP check is for the case where PRE_COPY is not supported, since RAM will ask for dirty page sync when the device is in STOP state. Without the STOP check, the device will skip the final dirty page sync.

Quoted 33 lines
Quoted 5 lines
hw/vfio/common.c +0 -0
                  return false;
              }
          }

hw/vfio/common.c static bool vfio_devices_all_running_and_mig_active(VFIOContainer *container) · line 397

Quoted 10 lines
hw/vfio/common.c +3 -1
@@ -387,7 +389,9 @@ static bool vfio_devices_all_running_and_mig_active(VFIOContainer *container)
              }
 
              if (migration->device_state == VFIO_DEVICE_STATE_RUNNING ||
-                migration->device_state == VFIO_DEVICE_STATE_RUNNING_P2P) {
+                migration->device_state == VFIO_DEVICE_STATE_RUNNING_P2P ||
+                migration->device_state == VFIO_DEVICE_STATE_PRE_COPY ||
+                migration->device_state == VFIO_DEVICE_STATE_PRE_COPY_P2P) {
                  continue;
              } else {
                  return false;  

Hmm, this only seems to highlight that between this series and the previous, we're adding tests for states that we never actually use, ie. these _P2P states.

IIRC, the reason we have these _P2P states is so that we can transition a set of devices, which may have active P2P DMA between them, to STOP, STOP_COPY, and even RUNNING states safely without lost data given that we cannot simultaneously transition all devices. That suggest that missing from both these series is support for bringing all devices to these _P2P states before we move any device to one of STOP, STOP_COPY, or RUNNING states (in the case of RESUMING).

Also, I recall discussions that we need to enforce configuration restrictions when not all devices support the _P2P states? For example adding a migration blocker when there are multiple vfio devices and at least one of them does not support _P2P migration states. Or perhaps initially, requiring support for _P2P states.

Quoted 3 lines

I think what's implemented here, where we don't make use of the _P2P states would require adding a migration blocker whenever there are multiple vfio devices, regardless of the device support for _P2P.

Quoted 6 lines

Yes, I think you are right.

I will add a migration blocker if there are multiple devices as part of v9 of the basic series.
When P2P support is added, I will block migration of multiple devices if one or more of them doesn't support P2P.

How does this affect our path towards supported migration? I'm thinking about a user experience where QEMU supports migration if device A OR device B are attached, but not devices A and B attached to the same VM. We might have a device C where QEMU supports migration with B AND C, but not A AND C, nor A AND B AND C. This would be the case if device B and device C both supported P2P states, but device A did not. The user has no observability of this feature, so all of this looks effectively random to the user.

Even in the single device case, we need to make an assumption that a device that does not support P2P migration states (or when QEMU doesn't make use of P2P states) cannot be a DMA target, or otherwise have its MMIO space accessed while in a STOP state. Can we guarantee that when other devices have not yet transitioned to STOP?

We could disable the direct map MemoryRegions when we move to a STOP state, which would give QEMU visibility to those accesses, but besides pulling an abort should such an access occur, could we queue them in software, add them to the migration stream, and replay them after the device moves to the RUNNING state? We'd need to account for the lack of RESUMING_P2P states as well, trapping and queue accesses from devices already RUNNING to those still in RESUMING (not _P2P).

This all looks complicated. Is it better to start with requiring P2P state support? Thanks,

Alex

#5 lore raw

Re: [PATCH 01/18] vfio/migration: Add VFIO migration pre-copy support

Quoted 8 lines · Alex Williamson

How does this affect our path towards supported migration? I'm thinking about a user experience where QEMU supports migration if device A OR device B are attached, but not devices A and B attached to the same VM. We might have a device C where QEMU supports migration with B AND C, but not A AND C, nor A AND B AND C. This would be the case if device B and device C both supported P2P states, but device A did not. The user has no observability of this feature, so all of this looks effectively random to the user.

I think qemu should just log if it encounters a device without P2P support.

Quoted 5 lines

Even in the single device case, we need to make an assumption that a device that does not support P2P migration states (or when QEMU doesn't make use of P2P states) cannot be a DMA target, or otherwise have its MMIO space accessed while in a STOP state. Can we guarantee that when other devices have not yet transitioned to STOP?

You mean the software devices created by qemu?

Quoted 7 lines

We could disable the direct map MemoryRegions when we move to a STOP state, which would give QEMU visibility to those accesses, but besides pulling an abort should such an access occur, could we queue them in software, add them to the migration stream, and replay them after the device moves to the RUNNING state? We'd need to account for the lack of RESUMING_P2P states as well, trapping and queue accesses from devices already RUNNING to those still in RESUMING (not _P2P).

I think any internal SW devices should just fail all accesses to the P2P space, all the time.

qemu simply acts like a real system that doesn't support P2P.

IMHO this is generally the way forward to do multi-device as well, remove the MMIO from all the address maps: VFIO, SW access, all of them. Nothing can touch MMIO except for the vCPU.

Quoted 2 lines

This all looks complicated. Is it better to start with requiring P2P state support? Thanks,

People have built HW without it, so I don't see this as so good..

Jason

#6 lore raw

Re: [PATCH 01/18] vfio/migration: Add VFIO migration pre-copy support

Quoted 11 lines · On Tue, 31 Jan 2023 19:29:48 -0400 Jason Gunthorpe <[email protected]> wrote:
Quoted 8 lines · Alex Williamson

How does this affect our path towards supported migration? I'm thinking about a user experience where QEMU supports migration if device A OR device B are attached, but not devices A and B attached to the same VM. We might have a device C where QEMU supports migration with B AND C, but not A AND C, nor A AND B AND C. This would be the case if device B and device C both supported P2P states, but device A did not. The user has no observability of this feature, so all of this looks effectively random to the user.

Quoted 2 lines

I think qemu should just log if it encounters a device without P2P support.

Better for debugging, but still poor from a VM management perspective.

Quoted 7 lines
Quoted 5 lines

Even in the single device case, we need to make an assumption that a device that does not support P2P migration states (or when QEMU doesn't make use of P2P states) cannot be a DMA target, or otherwise have its MMIO space accessed while in a STOP state. Can we guarantee that when other devices have not yet transitioned to STOP?

You mean the software devices created by qemu?

Other devices, software or otherwise, yes.

Quoted 8 lines
Quoted 7 lines

We could disable the direct map MemoryRegions when we move to a STOP state, which would give QEMU visibility to those accesses, but besides pulling an abort should such an access occur, could we queue them in software, add them to the migration stream, and replay them after the device moves to the RUNNING state? We'd need to account for the lack of RESUMING_P2P states as well, trapping and queue accesses from devices already RUNNING to those still in RESUMING (not _P2P).

Quoted 8 lines

I think any internal SW devices should just fail all accesses to the P2P space, all the time.

qemu simply acts like a real system that doesn't support P2P.

IMHO this is generally the way forward to do multi-device as well, remove the MMIO from all the address maps: VFIO, SW access, all of them. Nothing can touch MMIO except for the vCPU.

Are you suggesting this relative to migration or in general? P2P is a feature with tangible benefits and real use cases. Real systems seem to be moving towards making P2P work better, so it would seem short sighted to move to and enforce only configurations w/o P2P in QEMU generally. Besides, this would require some sort of deprecation, so are we intending to make users choose between migration and P2P?

Quoted 4 lines
Quoted 2 lines

This all looks complicated. Is it better to start with requiring P2P state support? Thanks,

People have built HW without it, so I don't see this as so good..

Are we obliged to start with that hardware? I'm just trying to think about whether a single device restriction is sufficient to prevent any possible P2P or whether there might be an easier starting point for more capable hardware. There's no shortage of hardware that could support migration given sufficient effort. Thanks,

Alex

#7 lore raw

Re: [PATCH 01/18] vfio/migration: Add VFIO migration pre-copy support

Quoted 5 lines · Alex Williamson
Quoted 3 lines

IMHO this is generally the way forward to do multi-device as well, remove the MMIO from all the address maps: VFIO, SW access, all of them. Nothing can touch MMIO except for the vCPU.

Are you suggesting this relative to migration or in general?

I would suggest a general qemu p2p on/off option.

Quoted 4 lines

P2P is a feature with tangible benefits and real use cases. Real systems seem to be moving towards making P2P work better, so it would seem short sighted to move to and enforce only configurations w/o P2P in QEMU generally.

qemu needs to support it, but it should be a user option option.

Every system I've been involved with for enabling P2P into a VM has been a total nightmare. This is not something you just turn on and it works great :\ The whole thing was carefully engineered right down to the BIOS to be able to work safely.

P2P in baremetal is much easier compared to P2P inside a VM.

Quoted 2 lines

Besides, this would require some sort of deprecation, so are we intending to make users choose between migration and P2P?

Give qemu an option 'p2p on/p2p off' and default it to on for backwards compatability.

If p2p on and migration devices don't support P2P states then migration is disabled. The user made this choice when they bought un-capable HW.

Log warnings to make it more discoverable. I think with the cdev patches we can make it so libvirt can query the device FD for capabilities to be even cleaner.

If user sets 'p2p off' then migration works with all HW.

p2p on/off is a global switch. With p2p off nothing, no HW or SW or hybrid device, can touch the MMIO memory.

'p2p off' is a valuable option in its own right because this stuff doesn't work reliably and is actively dangerous. Did you know you can hard crash the bare metal from a guest on some platforms with P2P operations? Yikes. If you don't need to use it turn it off and don't take the risk.

Arguably for this reason 'p2p off' should trend toward the default as it is much safer.

Quoted 5 lines

Are we obliged to start with that hardware? I'm just trying to think about whether a single device restriction is sufficient to prevent any possible P2P or whether there might be an easier starting point for more capable hardware. There's no shortage of hardware that could support migration given sufficient effort. Thanks,

I think multi-device will likely have some use cases, so I'd like to see a path to have support for them. For this series I think it is probably fine since it is already 18 patches.

Jason

#8 lore raw

Re: [PATCH 01/18] vfio/migration: Add VFIO migration pre-copy support

Quoted 40 lines · On Wed, 1 Feb 2023 13:28:40 -0400 Jason Gunthorpe <[email protected]> wrote:
Quoted 5 lines · Alex Williamson
Quoted 3 lines

IMHO this is generally the way forward to do multi-device as well, remove the MMIO from all the address maps: VFIO, SW access, all of them. Nothing can touch MMIO except for the vCPU.

Are you suggesting this relative to migration or in general?

I would suggest a general qemu p2p on/off option.

Quoted 4 lines

P2P is a feature with tangible benefits and real use cases. Real systems seem to be moving towards making P2P work better, so it would seem short sighted to move to and enforce only configurations w/o P2P in QEMU generally.

qemu needs to support it, but it should be a user option option.

Every system I've been involved with for enabling P2P into a VM has been a total nightmare. This is not something you just turn on and it works great :\ The whole thing was carefully engineered right down to the BIOS to be able to work safely.

P2P in baremetal is much easier compared to P2P inside a VM.

Quoted 2 lines

Besides, this would require some sort of deprecation, so are we intending to make users choose between migration and P2P?

Give qemu an option 'p2p on/p2p off' and default it to on for backwards compatability.

If p2p on and migration devices don't support P2P states then migration is disabled. The user made this choice when they bought un-capable HW.

Log warnings to make it more discoverable. I think with the cdev patches we can make it so libvirt can query the device FD for capabilities to be even cleaner.

If user sets 'p2p off' then migration works with all HW.

Quoted 8 lines

p2p on/off is a global switch. With p2p off nothing, no HW or SW or hybrid device, can touch the MMIO memory.

'p2p off' is a valuable option in its own right because this stuff doesn't work reliably and is actively dangerous. Did you know you can hard crash the bare metal from a guest on some platforms with P2P operations? Yikes. If you don't need to use it turn it off and don't take the risk.

If we're honest, there are a number of cases of non-exceptional faults that an assigned device can generate that the platform might escalate to fatal errors.

Quoted 2 lines

Arguably for this reason 'p2p off' should trend toward the default as it is much safer.

Safety in the hands of the userspace to protect the host though? Shouldn't the opt-in be at the kernel level whether to allow p2p mappings? I don't have an issue if QEMU were to mirror this by creating a RAM-only AddressSpace for devices which would be used when p2p is disable (it'd save us some headaches for various unaligned devices as well), but we shouldn't pretend that actually protects the host. OTOH, QEMU could feel confident supporting migration of devices w/o support of the migration P2P states with that restriction.

Quoted 9 lines
Quoted 5 lines

Are we obliged to start with that hardware? I'm just trying to think about whether a single device restriction is sufficient to prevent any possible P2P or whether there might be an easier starting point for more capable hardware. There's no shortage of hardware that could support migration given sufficient effort. Thanks,

I think multi-device will likely have some use cases, so I'd like to see a path to have support for them. For this series I think it is probably fine since it is already 18 patches.

It might be fine for this series because it hasn't yet proposed to make migration non-experimental, but it's unclear where the goal post is that we can actually make that transition.

If we restrict migration to a single vfio device, is that enough? Theoretically it's not, but perhaps in practice... maybe?

Therefore, do we depend on QEMU implementing a new RAM-only AddressSpace for devices? What's the path to making it the default? Maybe there are other aspects of the VM from which we can infer a preference towards migration support, ex. 'host' CPU type.

Another option, as previously mentioned, is to start with requiring P2P migration support both at the device and QEMU, where we only restrict the set of devices that could initially support migration without modifying existing behavior of the VM.

In any case, it seems we're a bit further from being able to declare this as supported than some had hoped. Thanks,

Alex

#9 lore raw

Re: [PATCH 01/18] vfio/migration: Add VFIO migration pre-copy support

Quoted 9 lines · Alex Williamson
Quoted 5 lines

'p2p off' is a valuable option in its own right because this stuff doesn't work reliably and is actively dangerous. Did you know you can hard crash the bare metal from a guest on some platforms with P2P operations? Yikes. If you don't need to use it turn it off and don't take the risk.

If we're honest, there are a number of cases of non-exceptional faults that an assigned device can generate that the platform might escalate to fatal errors.

What I understand is that is true on some commodity hardware, but engineered systems to run as cloud hypervisors have these problems solved and VFIO is made safe.

Unfortunately there is no way to know if you have a safe or unsafe system from the OS.

Quoted 6 lines
Quoted 2 lines

Arguably for this reason 'p2p off' should trend toward the default as it is much safer.

Safety in the hands of the userspace to protect the host though? Shouldn't the opt-in be at the kernel level whether to allow p2p mappings?

I haven't seen anyone interested in doing this kind of work. The expectation seems to be that places seriously concerned about security either don't include VFIO at all in their environments or have engineered their platforms to make it safe.

Where this leaves the enterprise space, I don't know. I think they end up with systems that functionally work but possibly have DOS problems.

So, given this landscape I think a user option in qemu is the best we can do at the moment.

Quoted 6 lines

I don't have an issue if QEMU were to mirror this by creating a RAM-only AddressSpace for devices which would be used when p2p is disable (it'd save us some headaches for various unaligned devices as well), but we shouldn't pretend that actually protects the host. OTOH, QEMU could feel confident supporting migration of devices w/o support of the migration P2P states with that restriction.

It protects the host from a hostile VM, it does not fully protect the host from a compromised qemu. That is still an improvement.

Quoted 7 lines
Quoted 3 lines

I think multi-device will likely have some use cases, so I'd like to see a path to have support for them. For this series I think it is probably fine since it is already 18 patches.

It might be fine for this series because it hasn't yet proposed to make migration non-experimental, but it's unclear where the goal post is that we can actually make that transition.

IMHO non-experimental just means the solution works with whatever configuration limitations it comes with. It shouldn't mean every device or every configuration combination works.

So if you want to do single device, or just hard require P2P for now, those are both reasonable temporary choices IMHO.

But they are temporary and we should come with a remedy to allow non-P2P migration devices to work as well.

Given we merged a non-P2P kernel driver I prefer the single device option as it is more logically consistent with the kernel situation.

Jason

#10 lore raw

PATCH 2/18 vfio/common: Fix error reporting in vfio_get_dirty_bitmap()

In reply to Avihai Horon

Return -errno instead of -1 if VFIO_IOMMU_DIRTY_PAGES ioctl fails in vfio_get_dirty_bitmap().

---
 hw/vfio/common.c | 1 +
 1 file changed, 1 insertion(+)
hw/vfio/common.c +1 -0
index 93b18c5e3d..d892609cf1 100644
@@ -1288,6 +1288,7 @@ static int vfio_get_dirty_bitmap(VFIOContainer *container, uint64_t iova,
 
     ret = ioctl(container->fd, VFIO_IOMMU_DIRTY_PAGES, dbitmap);
     if (ret) {
+        ret = -errno;
         error_report("Failed to get dirty bitmap for iova: 0x%"PRIx64
                 " size: 0x%"PRIx64" err: %d", (uint64_t)range->iova,
                 (uint64_t)range->size, errno);
Signature · 1 line
2.26.3
Reviewed-by on v1 2/18
#11 lore raw

Re: [PATCH 02/18] vfio/common: Fix error reporting in vfio_get_dirty_bitmap()

Quoted 4 lines · Avihai Horon

Return -errno instead of -1 if VFIO_IOMMU_DIRTY_PAGES ioctl fails in vfio_get_dirty_bitmap().

Thanks,

C.

Quoted 16 lines
---
  hw/vfio/common.c | 1 +
  1 file changed, 1 insertion(+)
hw/vfio/common.c +1 -0
index 93b18c5e3d..d892609cf1 100644
@@ -1288,6 +1288,7 @@ static int vfio_get_dirty_bitmap(VFIOContainer *container, uint64_t iova,
 
      ret = ioctl(container->fd, VFIO_IOMMU_DIRTY_PAGES, dbitmap);
      if (ret) {
+        ret = -errno;
          error_report("Failed to get dirty bitmap for iova: 0x%"PRIx64
                  " size: 0x%"PRIx64" err: %d", (uint64_t)range->iova,
                  (uint64_t)range->size, errno);
#12 lore raw

PATCH 3/18 vfio/common: Fix wrong %m usages

In reply to Avihai Horon

There are several places where the %m conversion is used if one of vfio_dma_map(), vfio_dma_unmap() or vfio_get_dirty_bitmap() fail.

The %m usage in these places is wrong since %m relies on errno value while the above functions don't report errors via errno.

Fix it by using strerror() with the returned value instead.

---
 hw/vfio/common.c | 29 ++++++++++++++++-------------
 1 file changed, 16 insertions(+), 13 deletions(-)
hw/vfio/common.c +16 -13
index d892609cf1..643418f6f1 100644
@@ -656,17 +656,17 @@ static void vfio_iommu_map_notify(IOMMUNotifier *n, IOMMUTLBEntry *iotlb)
                            read_only);
         if (ret) {
             error_report("vfio_dma_map(%p, 0x%"HWADDR_PRIx", "
-                         "0x%"HWADDR_PRIx", %p) = %d (%m)",
+                         "0x%"HWADDR_PRIx", %p) = %d (%s)",
                          container, iova,
-                         iotlb->addr_mask + 1, vaddr, ret);
+                         iotlb->addr_mask + 1, vaddr, ret, strerror(-ret));
         }
     } else {
         ret = vfio_dma_unmap(container, iova, iotlb->addr_mask + 1, iotlb);
         if (ret) {
             error_report("vfio_dma_unmap(%p, 0x%"HWADDR_PRIx", "
-                         "0x%"HWADDR_PRIx") = %d (%m)",
+                         "0x%"HWADDR_PRIx") = %d (%s)",
                          container, iova,
-                         iotlb->addr_mask + 1, ret);
+                         iotlb->addr_mask + 1, ret, strerror(-ret));
         }
     }
 out:
@@ -1048,8 +1048,9 @@ static void vfio_listener_region_add(MemoryListener *listener,
                        vaddr, section->readonly);
     if (ret) {
         error_setg(&err, "vfio_dma_map(%p, 0x%"HWADDR_PRIx", "
-                   "0x%"HWADDR_PRIx", %p) = %d (%m)",
-                   container, iova, int128_get64(llsize), vaddr, ret);
+                   "0x%"HWADDR_PRIx", %p) = %d (%s)",
+                   container, iova, int128_get64(llsize), vaddr, ret,
+                   strerror(-ret));
         if (memory_region_is_ram_device(section->mr)) {
             /* Allow unexpected mappings not to be fatal for RAM devices */
             error_report_err(err);
@@ -1181,16 +1182,18 @@ static void vfio_listener_region_del(MemoryListener *listener,
             ret = vfio_dma_unmap(container, iova, int128_get64(llsize), NULL);
             if (ret) {
                 error_report("vfio_dma_unmap(%p, 0x%"HWADDR_PRIx", "
-                             "0x%"HWADDR_PRIx") = %d (%m)",
-                             container, iova, int128_get64(llsize), ret);
+                             "0x%"HWADDR_PRIx") = %d (%s)",
+                             container, iova, int128_get64(llsize), ret,
+                             strerror(-ret));
             }
             iova += int128_get64(llsize);
         }
         ret = vfio_dma_unmap(container, iova, int128_get64(llsize), NULL);
         if (ret) {
             error_report("vfio_dma_unmap(%p, 0x%"HWADDR_PRIx", "
-                         "0x%"HWADDR_PRIx") = %d (%m)",
-                         container, iova, int128_get64(llsize), ret);
+                         "0x%"HWADDR_PRIx") = %d (%s)",
+                         container, iova, int128_get64(llsize), ret,
+                         strerror(-ret));
         }
     }
 
@@ -1337,9 +1340,9 @@ static void vfio_iommu_map_dirty_notify(IOMMUNotifier *n, IOMMUTLBEntry *iotlb)
                                     translated_addr);
         if (ret) {
             error_report("vfio_iommu_map_dirty_notify(%p, 0x%"HWADDR_PRIx", "
-                         "0x%"HWADDR_PRIx") = %d (%m)",
-                         container, iova,
-                         iotlb->addr_mask + 1, ret);
+                         "0x%"HWADDR_PRIx") = %d (%s)",
+                         container, iova, iotlb->addr_mask + 1, ret,
+                         strerror(-ret));
         }
     }
     rcu_read_unlock();
Signature · 1 line
2.26.3
Reviewed-by on v1 3/18
#13 lore raw

Re: [PATCH 03/18] vfio/common: Fix wrong %m usages

Quoted 9 lines · Avihai Horon

There are several places where the %m conversion is used if one of vfio_dma_map(), vfio_dma_unmap() or vfio_get_dirty_bitmap() fail.

The %m usage in these places is wrong since %m relies on errno value while the above functions don't report errors via errno.

Fix it by using strerror() with the returned value instead.

Thanks,

C.

Quoted 78 lines
---
  hw/vfio/common.c | 29 ++++++++++++++++-------------
  1 file changed, 16 insertions(+), 13 deletions(-)
hw/vfio/common.c +16 -13
index d892609cf1..643418f6f1 100644
@@ -656,17 +656,17 @@ static void vfio_iommu_map_notify(IOMMUNotifier *n, IOMMUTLBEntry *iotlb)
                             read_only);
          if (ret) {
              error_report("vfio_dma_map(%p, 0x%"HWADDR_PRIx", "
-                         "0x%"HWADDR_PRIx", %p) = %d (%m)",
+                         "0x%"HWADDR_PRIx", %p) = %d (%s)",
                           container, iova,
-                         iotlb->addr_mask + 1, vaddr, ret);
+                         iotlb->addr_mask + 1, vaddr, ret, strerror(-ret));
          }
      } else {
          ret = vfio_dma_unmap(container, iova, iotlb->addr_mask + 1, iotlb);
          if (ret) {
              error_report("vfio_dma_unmap(%p, 0x%"HWADDR_PRIx", "
-                         "0x%"HWADDR_PRIx") = %d (%m)",
+                         "0x%"HWADDR_PRIx") = %d (%s)",
                           container, iova,
-                         iotlb->addr_mask + 1, ret);
+                         iotlb->addr_mask + 1, ret, strerror(-ret));
          }
      }
  out:
@@ -1048,8 +1048,9 @@ static void vfio_listener_region_add(MemoryListener *listener,
                         vaddr, section->readonly);
      if (ret) {
          error_setg(&err, "vfio_dma_map(%p, 0x%"HWADDR_PRIx", "
-                   "0x%"HWADDR_PRIx", %p) = %d (%m)",
-                   container, iova, int128_get64(llsize), vaddr, ret);
+                   "0x%"HWADDR_PRIx", %p) = %d (%s)",
+                   container, iova, int128_get64(llsize), vaddr, ret,
+                   strerror(-ret));
          if (memory_region_is_ram_device(section->mr)) {
              /* Allow unexpected mappings not to be fatal for RAM devices */
              error_report_err(err);
@@ -1181,16 +1182,18 @@ static void vfio_listener_region_del(MemoryListener *listener,
              ret = vfio_dma_unmap(container, iova, int128_get64(llsize), NULL);
              if (ret) {
                  error_report("vfio_dma_unmap(%p, 0x%"HWADDR_PRIx", "
-                             "0x%"HWADDR_PRIx") = %d (%m)",
-                             container, iova, int128_get64(llsize), ret);
+                             "0x%"HWADDR_PRIx") = %d (%s)",
+                             container, iova, int128_get64(llsize), ret,
+                             strerror(-ret));
              }
              iova += int128_get64(llsize);
          }
          ret = vfio_dma_unmap(container, iova, int128_get64(llsize), NULL);
          if (ret) {
              error_report("vfio_dma_unmap(%p, 0x%"HWADDR_PRIx", "
-                         "0x%"HWADDR_PRIx") = %d (%m)",
-                         container, iova, int128_get64(llsize), ret);
+                         "0x%"HWADDR_PRIx") = %d (%s)",
+                         container, iova, int128_get64(llsize), ret,
+                         strerror(-ret));
          }
      }
 
@@ -1337,9 +1340,9 @@ static void vfio_iommu_map_dirty_notify(IOMMUNotifier *n, IOMMUTLBEntry *iotlb)
                                      translated_addr);
          if (ret) {
              error_report("vfio_iommu_map_dirty_notify(%p, 0x%"HWADDR_PRIx", "
-                         "0x%"HWADDR_PRIx") = %d (%m)",
-                         container, iova,
-                         iotlb->addr_mask + 1, ret);
+                         "0x%"HWADDR_PRIx") = %d (%s)",
+                         container, iova, iotlb->addr_mask + 1, ret,
+                         strerror(-ret));
          }
      }
      rcu_read_unlock();
#14 lore raw

PATCH 4/18 vfio/common: Abort migration if dirty log start/stop/sync fails

In reply to Avihai Horon

If VFIO dirty pages log start/stop/sync fails during migration, migration should be aborted as pages dirtied by VFIO devices might not be reported properly.

This is not the case today, where in such scenario only an error is printed.

Fix it by aborting migration in the above scenario.

Fixes 758b96b61d5c ("vfio/migrate: Move switch of dirty tracking into vfio_memory_listener")
Fixes b6dd6504e303 ("vfio: Add vfio_listener_log_sync to mark dirty pages")
Fixes 9e7b0442f23a ("vfio: Add ioctl to get dirty pages bitmap during dma unmap")
---
 hw/vfio/common.c | 53 ++++++++++++++++++++++++++++++++++++++++--------
 1 file changed, 45 insertions(+), 8 deletions(-)
hw/vfio/common.c +45 -8
index 643418f6f1..8e8ffbc046 100644
@@ -41,6 +41,7 @@
 #include "qapi/error.h"
 #include "migration/migration.h"
 #include "migration/misc.h"
+#include "migration/qemu-file.h"
 #include "sysemu/tpm.h"
 
 VFIOGroupList vfio_group_list =
@@ -337,6 +338,19 @@ bool vfio_mig_active(void)
     return true;
 }
 
+static void vfio_set_migration_error(int err)
+{
+    MigrationState *ms = migrate_get_current();
+
+    if (migration_is_setup_or_active(ms->state)) {
+        WITH_QEMU_LOCK_GUARD(&ms->qemu_file_lock) {
+            if (ms->to_dst_file) {
+                qemu_file_set_error(ms->to_dst_file, err);
+            }
+        }
+    }
+}
+
 static bool vfio_devices_all_dirty_tracking(VFIOContainer *container)
 {
     VFIOGroup *group;
@@ -633,6 +647,7 @@ static void vfio_iommu_map_notify(IOMMUNotifier *n, IOMMUTLBEntry *iotlb)
     if (iotlb->target_as != &address_space_memory) {
         error_report("Wrong target AS \"%s\", only system memory is allowed",
                      iotlb->target_as->name ? iotlb->target_as->name : "none");
+        vfio_set_migration_error(-EINVAL);
         return;
     }
 
@@ -667,6 +682,7 @@ static void vfio_iommu_map_notify(IOMMUNotifier *n, IOMMUTLBEntry *iotlb)
                          "0x%"HWADDR_PRIx") = %d (%s)",
                          container, iova,
                          iotlb->addr_mask + 1, ret, strerror(-ret));
+            vfio_set_migration_error(ret);
         }
     }
 out:
@@ -1212,7 +1228,7 @@ static void vfio_listener_region_del(MemoryListener *listener,
     }
 }
 
-static void vfio_set_dirty_page_tracking(VFIOContainer *container, bool start)
+static int vfio_set_dirty_page_tracking(VFIOContainer *container, bool start)
 {
     int ret;
     struct vfio_iommu_type1_dirty_bitmap dirty = {
@@ -1220,7 +1236,7 @@ static void vfio_set_dirty_page_tracking(VFIOContainer *container, bool start)
     };
 
     if (!container->dirty_pages_supported) {
-        return;
+        return 0;
     }
 
     if (start) {
@@ -1231,23 +1247,34 @@ static void vfio_set_dirty_page_tracking(VFIOContainer *container, bool start)
 
     ret = ioctl(container->fd, VFIO_IOMMU_DIRTY_PAGES, &dirty);
     if (ret) {
+        ret = -errno;
         error_report("Failed to set dirty tracking flag 0x%x errno: %d",
                      dirty.flags, errno);
     }
+
+    return ret;
 }
 
 static void vfio_listener_log_global_start(MemoryListener *listener)
 {
     VFIOContainer *container = container_of(listener, VFIOContainer, listener);
+    int ret;
 
-    vfio_set_dirty_page_tracking(container, true);
+    ret = vfio_set_dirty_page_tracking(container, true);
+    if (ret) {
+        vfio_set_migration_error(ret);
+    }
 }
 
 static void vfio_listener_log_global_stop(MemoryListener *listener)
 {
     VFIOContainer *container = container_of(listener, VFIOContainer, listener);
+    int ret;
 
-    vfio_set_dirty_page_tracking(container, false);
+    ret = vfio_set_dirty_page_tracking(container, false);
+    if (ret) {
+        vfio_set_migration_error(ret);
+    }
 }
 
 static int vfio_get_dirty_bitmap(VFIOContainer *container, uint64_t iova,
@@ -1323,19 +1350,18 @@ static void vfio_iommu_map_dirty_notify(IOMMUNotifier *n, IOMMUTLBEntry *iotlb)
     VFIOContainer *container = giommu->container;
     hwaddr iova = iotlb->iova + giommu->iommu_offset;
     ram_addr_t translated_addr;
+    int ret = -EINVAL;
 
     trace_vfio_iommu_map_dirty_notify(iova, iova + iotlb->addr_mask);
 
     if (iotlb->target_as != &address_space_memory) {
         error_report("Wrong target AS \"%s\", only system memory is allowed",
                      iotlb->target_as->name ? iotlb->target_as->name : "none");
-        return;
+        goto out;
     }
 
     rcu_read_lock();
     if (vfio_get_xlat_addr(iotlb, NULL, &translated_addr, NULL)) {
-        int ret;
-
         ret = vfio_get_dirty_bitmap(container, iova, iotlb->addr_mask + 1,
                                     translated_addr);
         if (ret) {
@@ -1346,6 +1372,11 @@ static void vfio_iommu_map_dirty_notify(IOMMUNotifier *n, IOMMUTLBEntry *iotlb)
         }
     }
     rcu_read_unlock();
+
+out:
+    if (ret) {
+        vfio_set_migration_error(ret);
+    }
 }
 
 static int vfio_ram_discard_get_dirty_bitmap(MemoryRegionSection *section,
@@ -1438,13 +1469,19 @@ static void vfio_listener_log_sync(MemoryListener *listener,
         MemoryRegionSection *section)
 {
     VFIOContainer *container = container_of(listener, VFIOContainer, listener);
+    int ret;
 
     if (vfio_listener_skipped_section(section)) {
         return;
     }
 
     if (vfio_devices_all_dirty_tracking(container)) {
-        vfio_sync_dirty_bitmap(container, section);
+        ret = vfio_sync_dirty_bitmap(container, section);
+        if (ret) {
+            error_report("vfio: Failed to sync dirty bitmap, err: %d (%s)", ret,
+                         strerror(-ret));
+            vfio_set_migration_error(ret);
+        }
     }
 }
 
Signature · 1 line
2.26.3
Reviewed-by on v1 4/18
#15 lore raw

Re: [PATCH 04/18] vfio/common: Abort migration if dirty log start/stop/sync fails

Quoted 7 lines · Avihai Horon

If VFIO dirty pages log start/stop/sync fails during migration, migration should be aborted as pages dirtied by VFIO devices might not be reported properly.

This is not the case today, where in such scenario only an error is printed.

Quoted 6 lines

Fix it by aborting migration in the above scenario.

Fixes 758b96b61d5c ("vfio/migrate: Move switch of dirty tracking into vfio_memory_listener")
Fixes b6dd6504e303 ("vfio: Add vfio_listener_log_sync to mark dirty pages")
Fixes 9e7b0442f23a ("vfio: Add ioctl to get dirty pages bitmap during dma unmap")

Thanks,

C.

Quoted 162 lines
---
  hw/vfio/common.c | 53 ++++++++++++++++++++++++++++++++++++++++--------
  1 file changed, 45 insertions(+), 8 deletions(-)
hw/vfio/common.c +45 -8
index 643418f6f1..8e8ffbc046 100644
@@ -41,6 +41,7 @@
  #include "qapi/error.h"
  #include "migration/migration.h"
  #include "migration/misc.h"
+#include "migration/qemu-file.h"
  #include "sysemu/tpm.h"
 
  VFIOGroupList vfio_group_list =
@@ -337,6 +338,19 @@ bool vfio_mig_active(void)
      return true;
  }
 
+static void vfio_set_migration_error(int err)
+{
+    MigrationState *ms = migrate_get_current();
+
+    if (migration_is_setup_or_active(ms->state)) {
+        WITH_QEMU_LOCK_GUARD(&ms->qemu_file_lock) {
+            if (ms->to_dst_file) {
+                qemu_file_set_error(ms->to_dst_file, err);
+            }
+        }
+    }
+}
+
  static bool vfio_devices_all_dirty_tracking(VFIOContainer *container)
  {
      VFIOGroup *group;
@@ -633,6 +647,7 @@ static void vfio_iommu_map_notify(IOMMUNotifier *n, IOMMUTLBEntry *iotlb)
      if (iotlb->target_as != &address_space_memory) {
          error_report("Wrong target AS \"%s\", only system memory is allowed",
                       iotlb->target_as->name ? iotlb->target_as->name : "none");
+        vfio_set_migration_error(-EINVAL);
          return;
      }
 
@@ -667,6 +682,7 @@ static void vfio_iommu_map_notify(IOMMUNotifier *n, IOMMUTLBEntry *iotlb)
                           "0x%"HWADDR_PRIx") = %d (%s)",
                           container, iova,
                           iotlb->addr_mask + 1, ret, strerror(-ret));
+            vfio_set_migration_error(ret);
          }
      }
  out:
@@ -1212,7 +1228,7 @@ static void vfio_listener_region_del(MemoryListener *listener,
      }
  }
 
-static void vfio_set_dirty_page_tracking(VFIOContainer *container, bool start)
+static int vfio_set_dirty_page_tracking(VFIOContainer *container, bool start)
  {
      int ret;
      struct vfio_iommu_type1_dirty_bitmap dirty = {
@@ -1220,7 +1236,7 @@ static void vfio_set_dirty_page_tracking(VFIOContainer *container, bool start)
      };
 
      if (!container->dirty_pages_supported) {
-        return;
+        return 0;
      }
 
      if (start) {
@@ -1231,23 +1247,34 @@ static void vfio_set_dirty_page_tracking(VFIOContainer *container, bool start)
 
      ret = ioctl(container->fd, VFIO_IOMMU_DIRTY_PAGES, &dirty);
      if (ret) {
+        ret = -errno;
          error_report("Failed to set dirty tracking flag 0x%x errno: %d",
                       dirty.flags, errno);
      }
+
+    return ret;
  }
 
  static void vfio_listener_log_global_start(MemoryListener *listener)
  {
      VFIOContainer *container = container_of(listener, VFIOContainer, listener);
+    int ret;
 
-    vfio_set_dirty_page_tracking(container, true);
+    ret = vfio_set_dirty_page_tracking(container, true);
+    if (ret) {
+        vfio_set_migration_error(ret);
+    }
  }
 
  static void vfio_listener_log_global_stop(MemoryListener *listener)
  {
      VFIOContainer *container = container_of(listener, VFIOContainer, listener);
+    int ret;
 
-    vfio_set_dirty_page_tracking(container, false);
+    ret = vfio_set_dirty_page_tracking(container, false);
+    if (ret) {
+        vfio_set_migration_error(ret);
+    }
  }
 
  static int vfio_get_dirty_bitmap(VFIOContainer *container, uint64_t iova,
@@ -1323,19 +1350,18 @@ static void vfio_iommu_map_dirty_notify(IOMMUNotifier *n, IOMMUTLBEntry *iotlb)
      VFIOContainer *container = giommu->container;
      hwaddr iova = iotlb->iova + giommu->iommu_offset;
      ram_addr_t translated_addr;
+    int ret = -EINVAL;
 
      trace_vfio_iommu_map_dirty_notify(iova, iova + iotlb->addr_mask);
 
      if (iotlb->target_as != &address_space_memory) {
          error_report("Wrong target AS \"%s\", only system memory is allowed",
                       iotlb->target_as->name ? iotlb->target_as->name : "none");
-        return;
+        goto out;
      }
 
      rcu_read_lock();
      if (vfio_get_xlat_addr(iotlb, NULL, &translated_addr, NULL)) {
-        int ret;
-
          ret = vfio_get_dirty_bitmap(container, iova, iotlb->addr_mask + 1,
                                      translated_addr);
          if (ret) {
@@ -1346,6 +1372,11 @@ static void vfio_iommu_map_dirty_notify(IOMMUNotifier *n, IOMMUTLBEntry *iotlb)
          }
      }
      rcu_read_unlock();
+
+out:
+    if (ret) {
+        vfio_set_migration_error(ret);
+    }
  }
 
  static int vfio_ram_discard_get_dirty_bitmap(MemoryRegionSection *section,
@@ -1438,13 +1469,19 @@ static void vfio_listener_log_sync(MemoryListener *listener,
          MemoryRegionSection *section)
  {
      VFIOContainer *container = container_of(listener, VFIOContainer, listener);
+    int ret;
 
      if (vfio_listener_skipped_section(section)) {
          return;
      }
 
      if (vfio_devices_all_dirty_tracking(container)) {
-        vfio_sync_dirty_bitmap(container, section);
+        ret = vfio_sync_dirty_bitmap(container, section);
+        if (ret) {
+            error_report("vfio: Failed to sync dirty bitmap, err: %d (%s)", ret,
+                         strerror(-ret));
+            vfio_set_migration_error(ret);
+        }
      }
  }
 
#16 lore raw

PATCH 5/18 vfio/common: Add VFIOBitmap and (de)alloc functions

In reply to Avihai Horon

There are already two places where dirty page bitmap allocation and calculations are done in open code. With device dirty page tracking being added in next patches, there are going to be even more places.

To avoid code duplication, introduce VFIOBitmap struct and corresponding alloc and dealloc functions and use them where applicable.

---
 hw/vfio/common.c | 89 ++++++++++++++++++++++++++++++++----------------
 1 file changed, 60 insertions(+), 29 deletions(-)
hw/vfio/common.c +60 -29
index 8e8ffbc046..e554573eb5 100644
@@ -319,6 +319,41 @@ const MemoryRegionOps vfio_region_ops = {
  * Device state interfaces
  */
 
+typedef struct {
+    unsigned long *bitmap;
+    hwaddr size;
+    hwaddr pages;
+} VFIOBitmap;
+
+static VFIOBitmap *vfio_bitmap_alloc(hwaddr size)
+{
+    VFIOBitmap *vbmap = g_try_new0(VFIOBitmap, 1);
+    if (!vbmap) {
+        errno = ENOMEM;
+
+        return NULL;
+    }
+
+    vbmap->pages = REAL_HOST_PAGE_ALIGN(size) / qemu_real_host_page_size();
+    vbmap->size =  ROUND_UP(vbmap->pages, sizeof(__u64) * BITS_PER_BYTE) /
+                                          BITS_PER_BYTE;
+    vbmap->bitmap = g_try_malloc0(vbmap->size);
+    if (!vbmap->bitmap) {
+        g_free(vbmap);
+        errno = ENOMEM;
+
+        return NULL;
+    }
+
+    return vbmap;
+}
+
+static void vfio_bitmap_dealloc(VFIOBitmap *vbmap)
+{
+    g_free(vbmap->bitmap);
+    g_free(vbmap);
+}
+
 bool vfio_mig_active(void)
 {
     VFIOGroup *group;
@@ -421,9 +456,14 @@ static int vfio_dma_unmap_bitmap(VFIOContainer *container,
 {
     struct vfio_iommu_type1_dma_unmap *unmap;
     struct vfio_bitmap *bitmap;
-    uint64_t pages = REAL_HOST_PAGE_ALIGN(size) / qemu_real_host_page_size();
+    VFIOBitmap *vbmap;
     int ret;
 
+    vbmap = vfio_bitmap_alloc(size);
+    if (!vbmap) {
+        return -errno;
+    }
+
     unmap = g_malloc0(sizeof(*unmap) + sizeof(*bitmap));
 
     unmap->argsz = sizeof(*unmap) + sizeof(*bitmap);
@@ -437,35 +477,28 @@ static int vfio_dma_unmap_bitmap(VFIOContainer *container,
      * qemu_real_host_page_size to mark those dirty. Hence set bitmap_pgsize
      * to qemu_real_host_page_size.
      */
-
     bitmap->pgsize = qemu_real_host_page_size();
-    bitmap->size = ROUND_UP(pages, sizeof(__u64) * BITS_PER_BYTE) /
-                   BITS_PER_BYTE;
+    bitmap->size = vbmap->size;
+    bitmap->data = (__u64 *)vbmap->bitmap;
 
-    if (bitmap->size > container->max_dirty_bitmap_size) {
-        error_report("UNMAP: Size of bitmap too big 0x%"PRIx64,
-                     (uint64_t)bitmap->size);
+    if (vbmap->size > container->max_dirty_bitmap_size) {
+        error_report("UNMAP: Size of bitmap too big 0x%"PRIx64, vbmap->size);
         ret = -E2BIG;
         goto unmap_exit;
     }
 
-    bitmap->data = g_try_malloc0(bitmap->size);
-    if (!bitmap->data) {
-        ret = -ENOMEM;
-        goto unmap_exit;
-    }
-
     ret = ioctl(container->fd, VFIO_IOMMU_UNMAP_DMA, unmap);
     if (!ret) {
-        cpu_physical_memory_set_dirty_lebitmap((unsigned long *)bitmap->data,
-                iotlb->translated_addr, pages);
+        cpu_physical_memory_set_dirty_lebitmap(vbmap->bitmap,
+                iotlb->translated_addr, vbmap->pages);
     } else {
         error_report("VFIO_UNMAP_DMA with DIRTY_BITMAP : %m");
     }
 
-    g_free(bitmap->data);
 unmap_exit:
     g_free(unmap);
+    vfio_bitmap_dealloc(vbmap);
+
     return ret;
 }
 
@@ -1282,7 +1315,7 @@ static int vfio_get_dirty_bitmap(VFIOContainer *container, uint64_t iova,
 {
     struct vfio_iommu_type1_dirty_bitmap *dbitmap;
     struct vfio_iommu_type1_dirty_bitmap_get *range;
-    uint64_t pages;
+    VFIOBitmap *vbmap;
     int ret;
 
     if (!container->dirty_pages_supported) {
@@ -1292,6 +1325,11 @@ static int vfio_get_dirty_bitmap(VFIOContainer *container, uint64_t iova,
         return 0;
     }
 
+    vbmap = vfio_bitmap_alloc(size);
+    if (!vbmap) {
+        return -errno;
+    }
+
     dbitmap = g_malloc0(sizeof(*dbitmap) + sizeof(*range));
 
     dbitmap->argsz = sizeof(*dbitmap) + sizeof(*range);
@@ -1306,15 +1344,8 @@ static int vfio_get_dirty_bitmap(VFIOContainer *container, uint64_t iova,
      * to qemu_real_host_page_size.
      */
     range->bitmap.pgsize = qemu_real_host_page_size();
-
-    pages = REAL_HOST_PAGE_ALIGN(range->size) / qemu_real_host_page_size();
-    range->bitmap.size = ROUND_UP(pages, sizeof(__u64) * BITS_PER_BYTE) /
-                                         BITS_PER_BYTE;
-    range->bitmap.data = g_try_malloc0(range->bitmap.size);
-    if (!range->bitmap.data) {
-        ret = -ENOMEM;
-        goto err_out;
-    }
+    range->bitmap.size = vbmap->size;
+    range->bitmap.data = (__u64 *)vbmap->bitmap;
 
     ret = ioctl(container->fd, VFIO_IOMMU_DIRTY_PAGES, dbitmap);
     if (ret) {
@@ -1325,14 +1356,14 @@ static int vfio_get_dirty_bitmap(VFIOContainer *container, uint64_t iova,
         goto err_out;
     }
 
-    cpu_physical_memory_set_dirty_lebitmap((unsigned long *)range->bitmap.data,
-                                            ram_addr, pages);
+    cpu_physical_memory_set_dirty_lebitmap(vbmap->bitmap, ram_addr,
+                                           vbmap->pages);
 
     trace_vfio_get_dirty_bitmap(container->fd, range->iova, range->size,
                                 range->bitmap.size, ram_addr);
 err_out:
-    g_free(range->bitmap.data);
     g_free(dbitmap);
+    vfio_bitmap_dealloc(vbmap);
 
     return ret;
 }
Signature · 1 line
2.26.3
#17 lore raw

Re: [PATCH 05/18] vfio/common: Add VFIOBitmap and (de)alloc functions

Quoted 80 lines · On Thu, 26 Jan 2023 20:49:35 +0200 Avihai Horon <[email protected]> wrote:

There are already two places where dirty page bitmap allocation and calculations are done in open code. With device dirty page tracking being added in next patches, there are going to be even more places.

To avoid code duplication, introduce VFIOBitmap struct and corresponding alloc and dealloc functions and use them where applicable.

---
 hw/vfio/common.c | 89 ++++++++++++++++++++++++++++++++----------------
 1 file changed, 60 insertions(+), 29 deletions(-)
hw/vfio/common.c +41 -2
index 8e8ffbc046..e554573eb5 100644
@@ -319,6 +319,41 @@ const MemoryRegionOps vfio_region_ops = {
  * Device state interfaces
  */
 
+typedef struct {
+    unsigned long *bitmap;
+    hwaddr size;
+    hwaddr pages;
+} VFIOBitmap;
+
+static VFIOBitmap *vfio_bitmap_alloc(hwaddr size)
+{
+    VFIOBitmap *vbmap = g_try_new0(VFIOBitmap, 1);
+    if (!vbmap) {
+        errno = ENOMEM;
+
+        return NULL;
+    }
+
+    vbmap->pages = REAL_HOST_PAGE_ALIGN(size) / qemu_real_host_page_size();
+    vbmap->size =  ROUND_UP(vbmap->pages, sizeof(__u64) * BITS_PER_BYTE) /
+                                          BITS_PER_BYTE;
+    vbmap->bitmap = g_try_malloc0(vbmap->size);
+    if (!vbmap->bitmap) {
+        g_free(vbmap);
+        errno = ENOMEM;
+
+        return NULL;
+    }
+
+    return vbmap;
+}
+
+static void vfio_bitmap_dealloc(VFIOBitmap *vbmap)
+{
+    g_free(vbmap->bitmap);
+    g_free(vbmap);
+}
+
 bool vfio_mig_active(void)
 {
     VFIOGroup *group;
@@ -421,9 +456,14 @@ static int vfio_dma_unmap_bitmap(VFIOContainer *container,
 {
     struct vfio_iommu_type1_dma_unmap *unmap;
     struct vfio_bitmap *bitmap;
-    uint64_t pages = REAL_HOST_PAGE_ALIGN(size) / qemu_real_host_page_size();
+    VFIOBitmap *vbmap;
     int ret;
 
+    vbmap = vfio_bitmap_alloc(size);
+    if (!vbmap) {
+        return -errno;
+    }
+
     unmap = g_malloc0(sizeof(*unmap) + sizeof(*bitmap));
 
     unmap->argsz = sizeof(*unmap) + sizeof(*bitmap);
@@ -437,35 +477,28 @@ static int vfio_dma_unmap_bitmap(VFIOContainer *container,
      * qemu_real_host_page_size to mark those dirty. Hence set bitmap_pgsize
      * to qemu_real_host_page_size.
      */
-
     bitmap->pgsize = qemu_real_host_page_size();

hw/vfio/common.c static int vfio_dma_unmap_bitmap(VFIOContainer *container, · line 485

Quoted 10 lines
hw/vfio/common.c +4 -5
-    bitmap->size = ROUND_UP(pages, sizeof(__u64) * BITS_PER_BYTE) /
-                   BITS_PER_BYTE;
+    bitmap->size = vbmap->size;
+    bitmap->data = (__u64 *)vbmap->bitmap;
 
-    if (bitmap->size > container->max_dirty_bitmap_size) {
-        error_report("UNMAP: Size of bitmap too big 0x%"PRIx64,
-                     (uint64_t)bitmap->size);
+    if (vbmap->size > container->max_dirty_bitmap_size) {
+        error_report("UNMAP: Size of bitmap too big 0x%"PRIx64, vbmap->size);

Why not pass the container to the alloc function so we can test this consistently for each bitmap we allocate? Thanks,

Alex

#18 lore raw

Re: [PATCH 05/18] vfio/common: Add VFIOBitmap and (de)alloc functions

Quoted 91 lines · Alex Williamson

External email: Use caution opening links or attachments

Quoted 85 lines · On Thu, 26 Jan 2023 20:49:35 +0200 Avihai Horon <[email protected]> wrote:

There are already two places where dirty page bitmap allocation and calculations are done in open code. With device dirty page tracking being added in next patches, there are going to be even more places.

To avoid code duplication, introduce VFIOBitmap struct and corresponding alloc and dealloc functions and use them where applicable.

---
  hw/vfio/common.c | 89 ++++++++++++++++++++++++++++++++----------------
  1 file changed, 60 insertions(+), 29 deletions(-)
hw/vfio/common.c +43 -4
index 8e8ffbc046..e554573eb5 100644
@@ -319,6 +319,41 @@ const MemoryRegionOps vfio_region_ops = {
   * Device state interfaces
   */
 
+typedef struct {
+    unsigned long *bitmap;
+    hwaddr size;
+    hwaddr pages;
+} VFIOBitmap;
+
+static VFIOBitmap *vfio_bitmap_alloc(hwaddr size)
+{
+    VFIOBitmap *vbmap = g_try_new0(VFIOBitmap, 1);
+    if (!vbmap) {
+        errno = ENOMEM;
+
+        return NULL;
+    }
+
+    vbmap->pages = REAL_HOST_PAGE_ALIGN(size) / qemu_real_host_page_size();
+    vbmap->size =  ROUND_UP(vbmap->pages, sizeof(__u64) * BITS_PER_BYTE) /
+                                          BITS_PER_BYTE;
+    vbmap->bitmap = g_try_malloc0(vbmap->size);
+    if (!vbmap->bitmap) {
+        g_free(vbmap);
+        errno = ENOMEM;
+
+        return NULL;
+    }
+
+    return vbmap;
+}
+
+static void vfio_bitmap_dealloc(VFIOBitmap *vbmap)
+{
+    g_free(vbmap->bitmap);
+    g_free(vbmap);
+}
+
  bool vfio_mig_active(void)
  {
      VFIOGroup *group;
@@ -421,9 +456,14 @@ static int vfio_dma_unmap_bitmap(VFIOContainer *container,
  {
      struct vfio_iommu_type1_dma_unmap *unmap;
      struct vfio_bitmap *bitmap;
-    uint64_t pages = REAL_HOST_PAGE_ALIGN(size) / qemu_real_host_page_size();
+    VFIOBitmap *vbmap;
      int ret;
 
+    vbmap = vfio_bitmap_alloc(size);
+    if (!vbmap) {
+        return -errno;
+    }
+
      unmap = g_malloc0(sizeof(*unmap) + sizeof(*bitmap));
 
      unmap->argsz = sizeof(*unmap) + sizeof(*bitmap);
@@ -437,35 +477,28 @@ static int vfio_dma_unmap_bitmap(VFIOContainer *container,
       * qemu_real_host_page_size to mark those dirty. Hence set bitmap_pgsize
       * to qemu_real_host_page_size.
       */
-
      bitmap->pgsize = qemu_real_host_page_size();
-    bitmap->size = ROUND_UP(pages, sizeof(__u64) * BITS_PER_BYTE) /
-                   BITS_PER_BYTE;
+    bitmap->size = vbmap->size;
+    bitmap->data = (__u64 *)vbmap->bitmap;
 
Quoted 7 lines

hw/vfio/common.c static int vfio_dma_unmap_bitmap(VFIOContainer *container, · line 485

Quoted 5 lines
hw/vfio/common.c +2 -3
-    if (bitmap->size > container->max_dirty_bitmap_size) {
-        error_report("UNMAP: Size of bitmap too big 0x%"PRIx64,
-                     (uint64_t)bitmap->size);
+    if (vbmap->size > container->max_dirty_bitmap_size) {
+        error_report("UNMAP: Size of bitmap too big 0x%"PRIx64, vbmap->size);

Why not pass the container to the alloc function so we can test this consistently for each bitmap we allocate?

Hi, sorry for the delay.

This test is relevant only for VFIO IOMMU dirty tracking. With device dirty tracking it should be skipped. Do you think we should still move it to the alloc function?

Thanks.

#19 lore raw

Re: [PATCH 05/18] vfio/common: Add VFIOBitmap and (de)alloc functions

Quoted 100 lines · On Sun, 12 Feb 2023 17:36:49 +0200 Avihai Horon <[email protected]> wrote:
Quoted 91 lines · Alex Williamson

External email: Use caution opening links or attachments

Quoted 85 lines · On Thu, 26 Jan 2023 20:49:35 +0200 Avihai Horon <[email protected]> wrote:

There are already two places where dirty page bitmap allocation and calculations are done in open code. With device dirty page tracking being added in next patches, there are going to be even more places.

To avoid code duplication, introduce VFIOBitmap struct and corresponding alloc and dealloc functions and use them where applicable.

---
  hw/vfio/common.c | 89 ++++++++++++++++++++++++++++++++----------------
  1 file changed, 60 insertions(+), 29 deletions(-)
hw/vfio/common.c +43 -4
index 8e8ffbc046..e554573eb5 100644
@@ -319,6 +319,41 @@ const MemoryRegionOps vfio_region_ops = {
   * Device state interfaces
   */
 
+typedef struct {
+    unsigned long *bitmap;
+    hwaddr size;
+    hwaddr pages;
+} VFIOBitmap;
+
+static VFIOBitmap *vfio_bitmap_alloc(hwaddr size)
+{
+    VFIOBitmap *vbmap = g_try_new0(VFIOBitmap, 1);
+    if (!vbmap) {
+        errno = ENOMEM;
+
+        return NULL;
+    }
+
+    vbmap->pages = REAL_HOST_PAGE_ALIGN(size) / qemu_real_host_page_size();
+    vbmap->size =  ROUND_UP(vbmap->pages, sizeof(__u64) * BITS_PER_BYTE) /
+                                          BITS_PER_BYTE;
+    vbmap->bitmap = g_try_malloc0(vbmap->size);
+    if (!vbmap->bitmap) {
+        g_free(vbmap);
+        errno = ENOMEM;
+
+        return NULL;
+    }
+
+    return vbmap;
+}
+
+static void vfio_bitmap_dealloc(VFIOBitmap *vbmap)
+{
+    g_free(vbmap->bitmap);
+    g_free(vbmap);
+}
+
  bool vfio_mig_active(void)
  {
      VFIOGroup *group;
@@ -421,9 +456,14 @@ static int vfio_dma_unmap_bitmap(VFIOContainer *container,
  {
      struct vfio_iommu_type1_dma_unmap *unmap;
      struct vfio_bitmap *bitmap;
-    uint64_t pages = REAL_HOST_PAGE_ALIGN(size) / qemu_real_host_page_size();
+    VFIOBitmap *vbmap;
      int ret;
 
+    vbmap = vfio_bitmap_alloc(size);
+    if (!vbmap) {
+        return -errno;
+    }
+
      unmap = g_malloc0(sizeof(*unmap) + sizeof(*bitmap));
 
      unmap->argsz = sizeof(*unmap) + sizeof(*bitmap);
@@ -437,35 +477,28 @@ static int vfio_dma_unmap_bitmap(VFIOContainer *container,
       * qemu_real_host_page_size to mark those dirty. Hence set bitmap_pgsize
       * to qemu_real_host_page_size.
       */
-
      bitmap->pgsize = qemu_real_host_page_size();
-    bitmap->size = ROUND_UP(pages, sizeof(__u64) * BITS_PER_BYTE) /
-                   BITS_PER_BYTE;
+    bitmap->size = vbmap->size;
+    bitmap->data = (__u64 *)vbmap->bitmap;
 
Quoted 7 lines

hw/vfio/common.c static int vfio_dma_unmap_bitmap(VFIOContainer *container, · line 485

Quoted 5 lines
hw/vfio/common.c +2 -3
-    if (bitmap->size > container->max_dirty_bitmap_size) {
-        error_report("UNMAP: Size of bitmap too big 0x%"PRIx64,
-                     (uint64_t)bitmap->size);
+    if (vbmap->size > container->max_dirty_bitmap_size) {
+        error_report("UNMAP: Size of bitmap too big 0x%"PRIx64, vbmap->size);  

Why not pass the container to the alloc function so we can test this consistently for each bitmap we allocate?

Quoted 5 lines

Hi, sorry for the delay.

This test is relevant only for VFIO IOMMU dirty tracking. With device dirty tracking it should be skipped.
Do you think we should still move it to the alloc function?

Ah, ok. Sounds like we'll have to live with a separate test for the container path. Thanks,

Alex

#20 lore raw

PATCH 6/18 util: Add iova_tree_nnodes()

In reply to Avihai Horon

From: Joao Martins <[email protected]>

Add iova_tree_nnodes() which returns the number of nodes in the IOVA tree.

---
 include/qemu/iova-tree.h | 11 +++++++++++
 util/iova-tree.c         |  5 +++++
 2 files changed, 16 insertions(+)
include/qemu/iova-tree.h +11 -0
index 8528e5c98f..7bb80783ce 100644
@@ -164,4 +164,15 @@ int iova_tree_alloc_map(IOVATree *tree, DMAMap *map, hwaddr iova_begin,
  */
 void iova_tree_destroy(IOVATree *tree);
 
+/**
+ * iova_tree_nnodes:
+ *
+ * @tree: the iova tree to consult
+ *
+ * Returns the number of nodes in the iova tree
+ *
+ * Return: >=0 for the number of nodes.
+ */
+gint iova_tree_nnodes(IOVATree *tree);
+
 #endif
util/iova-tree.c +5 -0
index 536789797e..6141a6229b 100644
@@ -280,3 +280,8 @@ void iova_tree_destroy(IOVATree *tree)
     g_tree_destroy(tree->tree);
     g_free(tree);
 }
+
+gint iova_tree_nnodes(IOVATree *tree)
+{
+    return g_tree_nnodes(tree->tree);
+}
Signature · 1 line
2.26.3
#22 lore raw

PATCH 7/18 util: Extend iova_tree_foreach() to take data argument

In reply to Avihai Horon

From: Joao Martins <[email protected]>

Extend iova_tree_foreach() to take data argument to be passed and used by the iterator.

While at it, fix a documentation error:
The documentation says iova_tree_foreach() returns a value even though it is a void function.

---
 include/qemu/iova-tree.h |  8 +++++---
 util/iova-tree.c         | 18 ++++++++++++++----
 2 files changed, 19 insertions(+), 7 deletions(-)
include/qemu/iova-tree.h +5 -3
index 7bb80783ce..1332dce014 100644
@@ -38,7 +38,7 @@ typedef struct DMAMap {
     hwaddr size;                /* Inclusive */
     IOMMUAccessFlags perm;
 } QEMU_PACKED DMAMap;
-typedef gboolean (*iova_tree_iterator)(DMAMap *map);
+typedef gboolean (*iova_tree_iterator)(DMAMap *map, gpointer data);
 
 /**
  * iova_tree_new:
@@ -129,12 +129,14 @@ const DMAMap *iova_tree_find_address(const IOVATree *tree, hwaddr iova);
  *
  * @tree: the iova tree to iterate on
  * @iterator: the interator for the mappings, return true to stop
+ * @data: data to be passed to the iterator
  *
  * Iterate over the iova tree.
  *
- * Return: 1 if found any overlap, 0 if not, <0 if error.
+ * Return: None.
  */
-void iova_tree_foreach(IOVATree *tree, iova_tree_iterator iterator);
+void iova_tree_foreach(IOVATree *tree, iova_tree_iterator iterator,
+                       gpointer data);
 
 /**
  * iova_tree_alloc_map:
util/iova-tree.c +14 -4
index 6141a6229b..9845427b86 100644
@@ -42,6 +42,11 @@ typedef struct IOVATreeFindIOVAArgs {
     const DMAMap *result;
 } IOVATreeFindIOVAArgs;
 
+typedef struct IOVATreeIterator {
+    iova_tree_iterator fn;
+    gpointer data;
+} IOVATreeIterator;
+
 /**
  * Iterate args to the next hole
  *
@@ -151,17 +156,22 @@ int iova_tree_insert(IOVATree *tree, const DMAMap *map)
 static gboolean iova_tree_traverse(gpointer key, gpointer value,
                                 gpointer data)
 {
-    iova_tree_iterator iterator = data;
+    IOVATreeIterator *iterator = data;
     DMAMap *map = key;
 
     g_assert(key == value);
 
-    return iterator(map);
+    return iterator->fn(map, iterator->data);
 }
 
-void iova_tree_foreach(IOVATree *tree, iova_tree_iterator iterator)
+void iova_tree_foreach(IOVATree *tree, iova_tree_iterator iterator,
+                       gpointer data)
 {
-    g_tree_foreach(tree->tree, iova_tree_traverse, iterator);
+    IOVATreeIterator arg = {
+        .fn = iterator,
+        .data = data,
+    };
+    g_tree_foreach(tree->tree, iova_tree_traverse, &arg);
 }
 
 void iova_tree_remove(IOVATree *tree, DMAMap map)
Signature · 1 line
2.26.3
Acked-by on v1 7/18
#23 lore raw

Re: [PATCH 07/18] util: Extend iova_tree_foreach() to take data argument

Quoted 10 lines · Avihai Horon

From: Joao Martins <[email protected]>

Extend iova_tree_foreach() to take data argument to be passed and used by the iterator.

While at it, fix a documentation error:
The documentation says iova_tree_foreach() returns a value even though it is a void function.

Signature · 1 line
Peter Xu
#24 lore raw

PATCH 8/18 vfio/common: Record DMA mapped IOVA ranges

In reply to Avihai Horon

From: Joao Martins <[email protected]>

According to the device DMA logging uAPI, IOVA ranges to be logged by the device must be provided all at once upon DMA logging start.

As preparation for the following patches which will add device dirty page tracking, keep a record of all DMA mapped IOVA ranges so later they can be used for DMA logging start.

Note that when vIOMMU is enabled DMA mapped IOVA ranges are not tracked. This is due to the dynamic nature of vIOMMU DMA mapping/unmapping. Following patches will address the vIOMMU case specifically.

---
 include/hw/vfio/vfio-common.h |  3 ++
 hw/vfio/common.c              | 86 +++++++++++++++++++++++++++++++++--
 2 files changed, 86 insertions(+), 3 deletions(-)
include/hw/vfio/vfio-common.h +3 -0
index 88c2194fb9..d54000d7ae 100644
@@ -23,6 +23,7 @@
 
 #include "exec/memory.h"
 #include "qemu/queue.h"
+#include "qemu/iova-tree.h"
 #include "qemu/notify.h"
 #include "ui/console.h"
 #include "hw/display/ramfb.h"
@@ -94,6 +95,8 @@ typedef struct VFIOContainer {
     uint64_t max_dirty_bitmap_size;
     unsigned long pgsizes;
     unsigned int dma_max_mappings;
+    IOVATree *mappings;
+    QemuMutex mappings_mutex;
     QLIST_HEAD(, VFIOGuestIOMMU) giommu_list;
     QLIST_HEAD(, VFIOHostDMAWindow) hostwin_list;
     QLIST_HEAD(, VFIOGroup) group_list;
hw/vfio/common.c +83 -3

149 lines not on this page · raw message

Signature · 1 line
2.26.3
#25 lore raw

Re: [PATCH 08/18] vfio/common: Record DMA mapped IOVA ranges

Quoted 66 lines · On Thu, 26 Jan 2023 20:49:38 +0200 Avihai Horon <[email protected]> wrote:

From: Joao Martins <[email protected]>

According to the device DMA logging uAPI, IOVA ranges to be logged by the device must be provided all at once upon DMA logging start.

As preparation for the following patches which will add device dirty page tracking, keep a record of all DMA mapped IOVA ranges so later they can be used for DMA logging start.

Note that when vIOMMU is enabled DMA mapped IOVA ranges are not tracked. This is due to the dynamic nature of vIOMMU DMA mapping/unmapping. Following patches will address the vIOMMU case specifically.

---
 include/hw/vfio/vfio-common.h |  3 ++
 hw/vfio/common.c              | 86 +++++++++++++++++++++++++++++++++--
 2 files changed, 86 insertions(+), 3 deletions(-)
include/hw/vfio/vfio-common.h +3 -0
index 88c2194fb9..d54000d7ae 100644
@@ -23,6 +23,7 @@
 
 #include "exec/memory.h"
 #include "qemu/queue.h"
+#include "qemu/iova-tree.h"
 #include "qemu/notify.h"
 #include "ui/console.h"
 #include "hw/display/ramfb.h"
@@ -94,6 +95,8 @@ typedef struct VFIOContainer {
     uint64_t max_dirty_bitmap_size;
     unsigned long pgsizes;
     unsigned int dma_max_mappings;
+    IOVATree *mappings;
+    QemuMutex mappings_mutex;
     QLIST_HEAD(, VFIOGuestIOMMU) giommu_list;
     QLIST_HEAD(, VFIOHostDMAWindow) hostwin_list;
     QLIST_HEAD(, VFIOGroup) group_list;
hw/vfio/common.c +6 -0
index e554573eb5..fafc361cea 100644
@@ -43,6 +43,7 @@
 #include "migration/misc.h"
 #include "migration/qemu-file.h"
 #include "sysemu/tpm.h"
+#include "qemu/iova-tree.h"
 
 VFIOGroupList vfio_group_list =
     QLIST_HEAD_INITIALIZER(vfio_group_list);
@@ -373,6 +374,11 @@ bool vfio_mig_active(void)
     return true;
 }
 
+static bool vfio_have_giommu(VFIOContainer *container)
+{
+    return !QLIST_EMPTY(&container->giommu_list);
+}
+
 static void vfio_set_migration_error(int err)
 {
     MigrationState *ms = migrate_get_current();

hw/vfio/common.c static bool vfio_devices_all_running_and_mig_active(VFIOContainer *container) · line 464

Quoted 9 lines
hw/vfio/common.c +6 -0
@@ -450,6 +456,51 @@ static bool vfio_devices_all_running_and_mig_active(VFIOContainer *container)
     return true;
 }
 
+static int vfio_record_mapping(VFIOContainer *container, hwaddr iova,
+                               hwaddr size, bool readonly)
+{
+    DMAMap map = {
+        .iova = iova,
+        .size = size - 1, /* IOVATree is inclusive, so subtract 1 from size */

<facepalm>

#26 lore raw

Re: [PATCH 08/18] vfio/common: Record DMA mapped IOVA ranges

Quoted 75 lines · Alex Williamson

External email: Use caution opening links or attachments

Quoted 69 lines · On Thu, 26 Jan 2023 20:49:38 +0200 Avihai Horon <[email protected]> wrote:

From: Joao Martins <[email protected]>

According to the device DMA logging uAPI, IOVA ranges to be logged by the device must be provided all at once upon DMA logging start.

As preparation for the following patches which will add device dirty page tracking, keep a record of all DMA mapped IOVA ranges so later they can be used for DMA logging start.

Note that when vIOMMU is enabled DMA mapped IOVA ranges are not tracked. This is due to the dynamic nature of vIOMMU DMA mapping/unmapping. Following patches will address the vIOMMU case specifically.

---
  include/hw/vfio/vfio-common.h |  3 ++
  hw/vfio/common.c              | 86 +++++++++++++++++++++++++++++++++--
  2 files changed, 86 insertions(+), 3 deletions(-)
include/hw/vfio/vfio-common.h +3 -0
index 88c2194fb9..d54000d7ae 100644
@@ -23,6 +23,7 @@
 
  #include "exec/memory.h"
  #include "qemu/queue.h"
+#include "qemu/iova-tree.h"
  #include "qemu/notify.h"
  #include "ui/console.h"
  #include "hw/display/ramfb.h"
@@ -94,6 +95,8 @@ typedef struct VFIOContainer {
      uint64_t max_dirty_bitmap_size;
      unsigned long pgsizes;
      unsigned int dma_max_mappings;
+    IOVATree *mappings;
+    QemuMutex mappings_mutex;
      QLIST_HEAD(, VFIOGuestIOMMU) giommu_list;
      QLIST_HEAD(, VFIOHostDMAWindow) hostwin_list;
      QLIST_HEAD(, VFIOGroup) group_list;
hw/vfio/common.c +6 -0
index e554573eb5..fafc361cea 100644
@@ -43,6 +43,7 @@
  #include "migration/misc.h"
  #include "migration/qemu-file.h"
  #include "sysemu/tpm.h"
+#include "qemu/iova-tree.h"
 
  VFIOGroupList vfio_group_list =
      QLIST_HEAD_INITIALIZER(vfio_group_list);
@@ -373,6 +374,11 @@ bool vfio_mig_active(void)
      return true;
  }
 
+static bool vfio_have_giommu(VFIOContainer *container)
+{
+    return !QLIST_EMPTY(&container->giommu_list);
+}
+
  static void vfio_set_migration_error(int err)
  {
      MigrationState *ms = migrate_get_current();
@@ -450,6 +456,51 @@ static bool vfio_devices_all_running_and_mig_active(VFIOContainer *container)
      return true;
  }
 
Quoted 7 lines

hw/vfio/common.c static bool vfio_devices_all_running_and_mig_active(VFIOContainer *container) · line 464

Quoted 6 lines
hw/vfio/common.c +6 -0
+static int vfio_record_mapping(VFIOContainer *container, hwaddr iova,
+                               hwaddr size, bool readonly)
+{
+    DMAMap map = {
+        .iova = iova,
+        .size = size - 1, /* IOVATree is inclusive, so subtract 1 from size */

<facepalm>

I am not sure what's the error you are referring here. Is it because DMAMap.size is not actually the size? Something else?

Thanks.

#27 lore raw

Re: [PATCH 08/18] vfio/common: Record DMA mapped IOVA ranges

Quoted 84 lines · On Sun, 12 Feb 2023 17:40:06 +0200 Avihai Horon <[email protected]> wrote:
Quoted 75 lines · Alex Williamson

External email: Use caution opening links or attachments

Quoted 69 lines · On Thu, 26 Jan 2023 20:49:38 +0200 Avihai Horon <[email protected]> wrote:

From: Joao Martins <[email protected]>

According to the device DMA logging uAPI, IOVA ranges to be logged by the device must be provided all at once upon DMA logging start.

As preparation for the following patches which will add device dirty page tracking, keep a record of all DMA mapped IOVA ranges so later they can be used for DMA logging start.

Note that when vIOMMU is enabled DMA mapped IOVA ranges are not tracked. This is due to the dynamic nature of vIOMMU DMA mapping/unmapping. Following patches will address the vIOMMU case specifically.

---
  include/hw/vfio/vfio-common.h |  3 ++
  hw/vfio/common.c              | 86 +++++++++++++++++++++++++++++++++--
  2 files changed, 86 insertions(+), 3 deletions(-)
include/hw/vfio/vfio-common.h +3 -0
index 88c2194fb9..d54000d7ae 100644
@@ -23,6 +23,7 @@
 
  #include "exec/memory.h"
  #include "qemu/queue.h"
+#include "qemu/iova-tree.h"
  #include "qemu/notify.h"
  #include "ui/console.h"
  #include "hw/display/ramfb.h"
@@ -94,6 +95,8 @@ typedef struct VFIOContainer {
      uint64_t max_dirty_bitmap_size;
      unsigned long pgsizes;
      unsigned int dma_max_mappings;
+    IOVATree *mappings;
+    QemuMutex mappings_mutex;
      QLIST_HEAD(, VFIOGuestIOMMU) giommu_list;
      QLIST_HEAD(, VFIOHostDMAWindow) hostwin_list;
      QLIST_HEAD(, VFIOGroup) group_list;
hw/vfio/common.c +6 -0
index e554573eb5..fafc361cea 100644
@@ -43,6 +43,7 @@
  #include "migration/misc.h"
  #include "migration/qemu-file.h"
  #include "sysemu/tpm.h"
+#include "qemu/iova-tree.h"
 
  VFIOGroupList vfio_group_list =
      QLIST_HEAD_INITIALIZER(vfio_group_list);
@@ -373,6 +374,11 @@ bool vfio_mig_active(void)
      return true;
  }
 
+static bool vfio_have_giommu(VFIOContainer *container)
+{
+    return !QLIST_EMPTY(&container->giommu_list);
+}
+
  static void vfio_set_migration_error(int err)
  {
      MigrationState *ms = migrate_get_current();
@@ -450,6 +456,51 @@ static bool vfio_devices_all_running_and_mig_active(VFIOContainer *container)
      return true;
  }
 
Quoted 7 lines

hw/vfio/common.c static bool vfio_devices_all_running_and_mig_active(VFIOContainer *container) · line 464

Quoted 6 lines
hw/vfio/common.c +6 -0
+static int vfio_record_mapping(VFIOContainer *container, hwaddr iova,
+                               hwaddr size, bool readonly)
+{
+    DMAMap map = {
+        .iova = iova,
+        .size = size - 1, /* IOVATree is inclusive, so subtract 1 from size */  

<facepalm>

Quoted 3 lines

I am not sure what's the error you are referring here. Is it because DMAMap.size is not actually the size? Something else?

Sorry, I'm just lamenting what a terrible interface IOVATree provides with this inclusive range nonsense. Thanks,

Alex

#28 lore raw

PATCH 9/18 vfio/common: Add device dirty page tracking start/stop

In reply to Avihai Horon

From: Joao Martins <[email protected]>

Add device dirty page tracking start/stop functionality. This uses the device DMA logging uAPI to start and stop dirty page tracking by device.

Device dirty page tracking is used only if all devices within a container support device dirty page tracking.

---
 include/hw/vfio/vfio-common.h |   2 +
 hw/vfio/common.c              | 211 +++++++++++++++++++++++++++++++++-
 2 files changed, 211 insertions(+), 2 deletions(-)
include/hw/vfio/vfio-common.h +2 -0
index d54000d7ae..cde6ffb9d6 100644
@@ -151,6 +151,8 @@ typedef struct VFIODevice {
     VFIOMigration *migration;
     Error *migration_blocker;
     OnOffAuto pre_copy_dirty_page_tracking;
+    bool dirty_pages_supported;
+    bool dirty_tracking;
 } VFIODevice;
 
 struct VFIODeviceOps {
hw/vfio/common.c +209 -2

236 lines not on this page · raw message

Signature · 1 line
2.26.3
#29 lore raw

PATCH 10/18 vfio/common: Extract code from vfio_get_dirty_bitmap() to new function

In reply to Avihai Horon

Extract the VFIO_IOMMU_DIRTY_PAGES ioctl code in vfio_get_dirty_bitmap() to its own function.

This will help the code to be more readable after next patch will add device dirty page bitmap sync functionality.

---
 hw/vfio/common.c | 53 ++++++++++++++++++++++++++++++------------------
 1 file changed, 33 insertions(+), 20 deletions(-)
hw/vfio/common.c +33 -20
index 005c060c67..3caa73d6f7 100644
@@ -1584,26 +1584,13 @@ static void vfio_listener_log_global_stop(MemoryListener *listener)
     }
 }
 
-static int vfio_get_dirty_bitmap(VFIOContainer *container, uint64_t iova,
-                                 uint64_t size, ram_addr_t ram_addr)
+static int vfio_query_dirty_bitmap(VFIOContainer *container, VFIOBitmap *vbmap,
+                                   hwaddr iova, hwaddr size)
 {
     struct vfio_iommu_type1_dirty_bitmap *dbitmap;
     struct vfio_iommu_type1_dirty_bitmap_get *range;
-    VFIOBitmap *vbmap;
     int ret;
 
-    if (!container->dirty_pages_supported) {
-        cpu_physical_memory_set_dirty_range(ram_addr, size,
-                                            tcg_enabled() ? DIRTY_CLIENTS_ALL :
-                                            DIRTY_CLIENTS_NOCODE);
-        return 0;
-    }
-
-    vbmap = vfio_bitmap_alloc(size);
-    if (!vbmap) {
-        return -errno;
-    }
-
     dbitmap = g_malloc0(sizeof(*dbitmap) + sizeof(*range));
 
     dbitmap->argsz = sizeof(*dbitmap) + sizeof(*range);
@@ -1627,16 +1614,42 @@ static int vfio_get_dirty_bitmap(VFIOContainer *container, uint64_t iova,
         error_report("Failed to get dirty bitmap for iova: 0x%"PRIx64
                 " size: 0x%"PRIx64" err: %d", (uint64_t)range->iova,
                 (uint64_t)range->size, errno);
-        goto err_out;
+    }
+
+    g_free(dbitmap);
+
+    return ret;
+}
+
+static int vfio_get_dirty_bitmap(VFIOContainer *container, uint64_t iova,
+                                 uint64_t size, ram_addr_t ram_addr)
+{
+    VFIOBitmap *vbmap;
+    int ret;
+
+    if (!container->dirty_pages_supported) {
+        cpu_physical_memory_set_dirty_range(ram_addr, size,
+                                            tcg_enabled() ? DIRTY_CLIENTS_ALL :
+                                            DIRTY_CLIENTS_NOCODE);
+        return 0;
+    }
+
+    vbmap = vfio_bitmap_alloc(size);
+    if (!vbmap) {
+        return -errno;
+    }
+
+    ret = vfio_query_dirty_bitmap(container, vbmap, iova, size);
+    if (ret) {
+        goto out;
     }
 
     cpu_physical_memory_set_dirty_lebitmap(vbmap->bitmap, ram_addr,
                                            vbmap->pages);
 
-    trace_vfio_get_dirty_bitmap(container->fd, range->iova, range->size,
-                                range->bitmap.size, ram_addr);
-err_out:
-    g_free(dbitmap);
+    trace_vfio_get_dirty_bitmap(container->fd, iova, size, vbmap->size,
+                                ram_addr);
+out:
     vfio_bitmap_dealloc(vbmap);
 
     return ret;
Signature · 1 line
2.26.3
#30 lore raw

PATCH 11/18 vfio/common: Add device dirty page bitmap sync

In reply to Avihai Horon

From: Joao Martins <[email protected]>

Add device dirty page bitmap sync functionality. This uses the device DMA logging uAPI to sync dirty page bitmap from the device.

Device dirty page bitmap sync is used only if all devices within a container support device dirty page tracking.

---
 hw/vfio/common.c | 93 ++++++++++++++++++++++++++++++++++++++++++------
 1 file changed, 82 insertions(+), 11 deletions(-)
hw/vfio/common.c +82 -11
index 3caa73d6f7..0003f2421d 100644
@@ -355,6 +355,9 @@ static void vfio_bitmap_dealloc(VFIOBitmap *vbmap)
     g_free(vbmap);
 }
 
+static int vfio_get_dirty_bitmap(VFIOContainer *container, uint64_t iova,
+                                 uint64_t size, ram_addr_t ram_addr);
+
 bool vfio_mig_active(void)
 {
     VFIOGroup *group;
@@ -582,10 +585,19 @@ static int vfio_dma_unmap(VFIOContainer *container,
         .iova = iova,
         .size = size,
     };
+    int ret;
 
-    if (iotlb && container->dirty_pages_supported &&
-        vfio_devices_all_running_and_mig_active(container)) {
-        return vfio_dma_unmap_bitmap(container, iova, size, iotlb);
+    if (iotlb && vfio_devices_all_running_and_mig_active(container)) {
+        if (!vfio_devices_all_device_dirty_tracking(container) &&
+            container->dirty_pages_supported) {
+            return vfio_dma_unmap_bitmap(container, iova, size, iotlb);
+        }
+
+        ret = vfio_get_dirty_bitmap(container, iova, size,
+                                    iotlb->translated_addr);
+        if (ret) {
+            return ret;
+        }
     }
 
     while (ioctl(container->fd, VFIO_IOMMU_UNMAP_DMA, &unmap)) {
@@ -611,12 +623,6 @@ static int vfio_dma_unmap(VFIOContainer *container,
         return -errno;
     }
 
-    if (iotlb && vfio_devices_all_running_and_mig_active(container)) {
-        cpu_physical_memory_set_dirty_range(iotlb->translated_addr, size,
-                                            tcg_enabled() ? DIRTY_CLIENTS_ALL :
-                                            DIRTY_CLIENTS_NOCODE);
-    }
-
     vfio_erase_mapping(container, iova, size);
 
     return 0;
@@ -1584,6 +1590,65 @@ static void vfio_listener_log_global_stop(MemoryListener *listener)
     }
 }
 
+static int vfio_device_dma_logging_report(VFIODevice *vbasedev, hwaddr iova,
+                                          hwaddr size, void *bitmap)
+{
+    uint64_t buf[DIV_ROUND_UP(sizeof(struct vfio_device_feature) +
+                        sizeof(struct vfio_device_feature_dma_logging_report),
+                        sizeof(uint64_t))] = {};
+    struct vfio_device_feature *feature = (struct vfio_device_feature *)buf;
+    struct vfio_device_feature_dma_logging_report *report =
+        (struct vfio_device_feature_dma_logging_report *)feature->data;
+
+    report->iova = iova;
+    report->length = size;
+    report->page_size = qemu_real_host_page_size();
+    report->bitmap = (uint64_t)bitmap;
+
+    feature->argsz = sizeof(buf);
+    feature->flags =
+        VFIO_DEVICE_FEATURE_GET | VFIO_DEVICE_FEATURE_DMA_LOGGING_REPORT;
+
+    if (ioctl(vbasedev->fd, VFIO_DEVICE_FEATURE, feature)) {
+        return -errno;
+    }
+
+    return 0;
+}
+
+static int vfio_devices_query_dirty_bitmap(VFIOContainer *container,
+                                           VFIOBitmap *vbmap, hwaddr iova,
+                                           hwaddr size)
+{
+    VFIODevice *vbasedev;
+    VFIOGroup *group;
+    int ret;
+
+    if (vfio_have_giommu(container)) {
+        /* Device dirty page tracking currently doesn't support vIOMMU */
+        bitmap_set(vbmap->bitmap, 0, vbmap->pages);
+
+        return 0;
+    }
+
+    QLIST_FOREACH(group, &container->group_list, container_next) {
+        QLIST_FOREACH(vbasedev, &group->device_list, next) {
+            ret = vfio_device_dma_logging_report(vbasedev, iova, size,
+                                                 vbmap->bitmap);
+            if (ret) {
+                error_report("%s: Failed to get DMA logging report, iova: "
+                             "0x%" HWADDR_PRIx ", size: 0x%" HWADDR_PRIx
+                             ", err: %d (%s)",
+                             vbasedev->name, iova, size, ret, strerror(-ret));
+
+                return ret;
+            }
+        }
+    }
+
+    return 0;
+}
+
 static int vfio_query_dirty_bitmap(VFIOContainer *container, VFIOBitmap *vbmap,
                                    hwaddr iova, hwaddr size)
 {
@@ -1627,7 +1692,8 @@ static int vfio_get_dirty_bitmap(VFIOContainer *container, uint64_t iova,
     VFIOBitmap *vbmap;
     int ret;
 
-    if (!container->dirty_pages_supported) {
+    if (!container->dirty_pages_supported &&
+        !vfio_devices_all_device_dirty_tracking(container)) {
         cpu_physical_memory_set_dirty_range(ram_addr, size,
                                             tcg_enabled() ? DIRTY_CLIENTS_ALL :
                                             DIRTY_CLIENTS_NOCODE);
@@ -1639,7 +1705,12 @@ static int vfio_get_dirty_bitmap(VFIOContainer *container, uint64_t iova,
         return -errno;
     }
 
-    ret = vfio_query_dirty_bitmap(container, vbmap, iova, size);
+    if (vfio_devices_all_device_dirty_tracking(container)) {
+        ret = vfio_devices_query_dirty_bitmap(container, vbmap, iova, size);
+    } else {
+        ret = vfio_query_dirty_bitmap(container, vbmap, iova, size);
+    }
+
     if (ret) {
         goto out;
     }
Signature · 1 line
2.26.3
#31 lore raw

Re: [PATCH 11/18] vfio/common: Add device dirty page bitmap sync

Quoted 38 lines · On Thu, 26 Jan 2023 20:49:41 +0200 Avihai Horon <[email protected]> wrote:

From: Joao Martins <[email protected]>

Add device dirty page bitmap sync functionality. This uses the device DMA logging uAPI to sync dirty page bitmap from the device.

Device dirty page bitmap sync is used only if all devices within a container support device dirty page tracking.

---
 hw/vfio/common.c | 93 ++++++++++++++++++++++++++++++++++++++++++------
 1 file changed, 82 insertions(+), 11 deletions(-)
hw/vfio/common.c +5 -3
index 3caa73d6f7..0003f2421d 100644
@@ -355,6 +355,9 @@ static void vfio_bitmap_dealloc(VFIOBitmap *vbmap)
     g_free(vbmap);
 }
 
+static int vfio_get_dirty_bitmap(VFIOContainer *container, uint64_t iova,
+                                 uint64_t size, ram_addr_t ram_addr);
+
 bool vfio_mig_active(void)
 {
     VFIOGroup *group;
@@ -582,10 +585,19 @@ static int vfio_dma_unmap(VFIOContainer *container,
         .iova = iova,
         .size = size,
     };
+    int ret;
 
-    if (iotlb && container->dirty_pages_supported &&
-        vfio_devices_all_running_and_mig_active(container)) {
-        return vfio_dma_unmap_bitmap(container, iova, size, iotlb);
+    if (iotlb && vfio_devices_all_running_and_mig_active(container)) {

hw/vfio/common.c static int vfio_dma_unmap(VFIOContainer *container, · line 600

Quoted 10 lines
hw/vfio/common.c +10 -0
+        if (!vfio_devices_all_device_dirty_tracking(container) &&
+            container->dirty_pages_supported) {
+            return vfio_dma_unmap_bitmap(container, iova, size, iotlb);
+        }
+
+        ret = vfio_get_dirty_bitmap(container, iova, size,
+                                    iotlb->translated_addr);
+        if (ret) {
+            return ret;
+        }

Isn't the ordering backwards here? Only after the range is unmapped can we know that this container can no longer dirty pages within the range. Thanks,

Alex

#32 lore raw

Re: [PATCH 11/18] vfio/common: Add device dirty page bitmap sync

Quoted 49 lines · Alex Williamson

External email: Use caution opening links or attachments

Quoted 43 lines · On Thu, 26 Jan 2023 20:49:41 +0200 Avihai Horon <[email protected]> wrote:

From: Joao Martins <[email protected]>

Add device dirty page bitmap sync functionality. This uses the device DMA logging uAPI to sync dirty page bitmap from the device.

Device dirty page bitmap sync is used only if all devices within a container support device dirty page tracking.

---
  hw/vfio/common.c | 93 ++++++++++++++++++++++++++++++++++++++++++------
  1 file changed, 82 insertions(+), 11 deletions(-)
hw/vfio/common.c +10 -3
index 3caa73d6f7..0003f2421d 100644
@@ -355,6 +355,9 @@ static void vfio_bitmap_dealloc(VFIOBitmap *vbmap)
      g_free(vbmap);
  }
 
+static int vfio_get_dirty_bitmap(VFIOContainer *container, uint64_t iova,
+                                 uint64_t size, ram_addr_t ram_addr);
+
  bool vfio_mig_active(void)
  {
      VFIOGroup *group;
@@ -582,10 +585,19 @@ static int vfio_dma_unmap(VFIOContainer *container,
          .iova = iova,
          .size = size,
      };
+    int ret;
 
-    if (iotlb && container->dirty_pages_supported &&
-        vfio_devices_all_running_and_mig_active(container)) {
-        return vfio_dma_unmap_bitmap(container, iova, size, iotlb);
+    if (iotlb && vfio_devices_all_running_and_mig_active(container)) {
+        if (!vfio_devices_all_device_dirty_tracking(container) &&
+            container->dirty_pages_supported) {
+            return vfio_dma_unmap_bitmap(container, iova, size, iotlb);
+        }
+
Quoted 8 lines

hw/vfio/common.c static int vfio_dma_unmap(VFIOContainer *container, · line 600

Quoted 5 lines
hw/vfio/common.c +5 -0
+        ret = vfio_get_dirty_bitmap(container, iova, size,
+                                    iotlb->translated_addr);
+        if (ret) {
+            return ret;
+        }

Isn't the ordering backwards here? Only after the range is unmapped can we know that this container can no longer dirty pages within the range.

Oops, I thought that it's OK to query the dirty bitmap when we get the vIOMMU unmap notification. I will reverse the order.

Thanks.

#33 lore raw

PATCH 12/18 vfio/common: Extract vIOMMU code from vfio_sync_dirty_bitmap()

In reply to Avihai Horon

Extract vIOMMU code from vfio_sync_dirty_bitmap() to a new function and restructure the code.

This is done as preparation for the following patches which will add vIOMMU support to device dirty page tracking. No functional changes intended.

---
 hw/vfio/common.c | 63 +++++++++++++++++++++++++++++-------------------
 1 file changed, 38 insertions(+), 25 deletions(-)
hw/vfio/common.c +38 -25
index 0003f2421d..9792c2c935 100644
@@ -1811,37 +1811,50 @@ static int vfio_sync_ram_discard_listener_dirty_bitmap(VFIOContainer *container,
                                                 &vrdl);
 }
 
+static int vfio_sync_iommu_dirty_bitmap(VFIOContainer *container,
+                                        MemoryRegionSection *section)
+{
+    VFIOGuestIOMMU *giommu;
+    bool found = false;
+    Int128 llend;
+    vfio_giommu_dirty_notifier gdn;
+    int idx;
+
+    QLIST_FOREACH(giommu, &container->giommu_list, giommu_next) {
+        if (MEMORY_REGION(giommu->iommu_mr) == section->mr &&
+            giommu->n.start == section->offset_within_region) {
+            found = true;
+            break;
+        }
+    }
+
+    if (!found) {
+        return 0;
+    }
+
+    gdn.giommu = giommu;
+    idx = memory_region_iommu_attrs_to_index(giommu->iommu_mr,
+                                             MEMTXATTRS_UNSPECIFIED);
+
+    llend = int128_add(int128_make64(section->offset_within_region),
+                       section->size);
+    llend = int128_sub(llend, int128_one());
+
+    iommu_notifier_init(&gdn.n, vfio_iommu_map_dirty_notify, IOMMU_NOTIFIER_MAP,
+                        section->offset_within_region, int128_get64(llend),
+                        idx);
+    memory_region_iommu_replay(giommu->iommu_mr, &gdn.n);
+
+    return 0;
+}
+
 static int vfio_sync_dirty_bitmap(VFIOContainer *container,
                                   MemoryRegionSection *section)
 {
     ram_addr_t ram_addr;
 
     if (memory_region_is_iommu(section->mr)) {
-        VFIOGuestIOMMU *giommu;
-
-        QLIST_FOREACH(giommu, &container->giommu_list, giommu_next) {
-            if (MEMORY_REGION(giommu->iommu_mr) == section->mr &&
-                giommu->n.start == section->offset_within_region) {
-                Int128 llend;
-                vfio_giommu_dirty_notifier gdn = { .giommu = giommu };
-                int idx = memory_region_iommu_attrs_to_index(giommu->iommu_mr,
-                                                       MEMTXATTRS_UNSPECIFIED);
-
-                llend = int128_add(int128_make64(section->offset_within_region),
-                                   section->size);
-                llend = int128_sub(llend, int128_one());
-
-                iommu_notifier_init(&gdn.n,
-                                    vfio_iommu_map_dirty_notify,
-                                    IOMMU_NOTIFIER_MAP,
-                                    section->offset_within_region,
-                                    int128_get64(llend),
-                                    idx);
-                memory_region_iommu_replay(giommu->iommu_mr, &gdn.n);
-                break;
-            }
-        }
-        return 0;
+        return vfio_sync_iommu_dirty_bitmap(container, section);
     } else if (memory_region_has_ram_discard_manager(section->mr)) {
         return vfio_sync_ram_discard_listener_dirty_bitmap(container, section);
     }
Signature · 1 line
2.26.3
#34 lore raw

PATCH 13/18 memory/iommu: Add IOMMU_ATTR_MAX_IOVA attribute

In reply to Avihai Horon

Add a new IOMMU attribute IOMMU_ATTR_MAX_IOVA which indicates the maximal IOVA that an IOMMU can use.

This attribute will be used by VFIO device dirty page tracking so it can track the entire IOVA space when needed (i.e. when vIOMMU is enabled).

---
 include/exec/memory.h | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
include/exec/memory.h +2 -1
index c37ffdbcd1..910067a3a5 100644
@@ -290,7 +290,8 @@ typedef struct MemoryRegionClass {
 
 
 enum IOMMUMemoryRegionAttr {
-    IOMMU_ATTR_SPAPR_TCE_FD
+    IOMMU_ATTR_SPAPR_TCE_FD,
+    IOMMU_ATTR_MAX_IOVA,
 };
 
 /*
Signature · 1 line
2.26.3
Acked-by on v1 13/18
#35 lore raw

Re: [PATCH 13/18] memory/iommu: Add IOMMU_ATTR_MAX_IOVA attribute

Quoted 7 lines · Avihai Horon

Add a new IOMMU attribute IOMMU_ATTR_MAX_IOVA which indicates the maximal IOVA that an IOMMU can use.

This attribute will be used by VFIO device dirty page tracking so it can track the entire IOVA space when needed (i.e. when vIOMMU is enabled).

Signature · 1 line
Peter Xu
#36 lore raw

PATCH 14/18 intel-iommu: Implement get_attr() method

In reply to Avihai Horon

Implement get_attr() method and use the address width property to report the IOMMU_ATTR_MAX_IOVA attribute.

---
 hw/i386/intel_iommu.c | 18 ++++++++++++++++++
 1 file changed, 18 insertions(+)
hw/i386/intel_iommu.c +18 -0
index 98a5c304a7..b0068b0df4 100644
@@ -3841,6 +3841,23 @@ static void vtd_iommu_replay(IOMMUMemoryRegion *iommu_mr, IOMMUNotifier *n)
     return;
 }
 
+static int vtd_iommu_get_attr(IOMMUMemoryRegion *iommu_mr,
+                              enum IOMMUMemoryRegionAttr attr, void *data)
+{
+    VTDAddressSpace *vtd_as = container_of(iommu_mr, VTDAddressSpace, iommu);
+    IntelIOMMUState *s = vtd_as->iommu_state;
+
+    if (attr == IOMMU_ATTR_MAX_IOVA) {
+        hwaddr *max_iova = data;
+
+        *max_iova = (1ULL << s->aw_bits) - 1;
+
+        return 0;
+    }
+
+    return -EINVAL;
+}
+
 /* Do the initialization. It will also be called when reset, so pay
  * attention when adding new initialization stuff.
  */
@@ -4173,6 +4190,7 @@ static void vtd_iommu_memory_region_class_init(ObjectClass *klass,
     imrc->translate = vtd_iommu_translate;
     imrc->notify_flag_changed = vtd_iommu_notify_flag_changed;
     imrc->replay = vtd_iommu_replay;
+    imrc->get_attr = vtd_iommu_get_attr;
 }
 
 static const TypeInfo vtd_iommu_memory_region_info = {
Signature · 1 line
2.26.3
#38 lore raw

PATCH 15/18 vfio/common: Support device dirty page tracking with vIOMMU

In reply to Avihai Horon

Currently, device dirty page tracking with vIOMMU is not supported - RAM pages are perpetually marked dirty in this case.

When vIOMMU is used, IOVA ranges are DMA mapped/unmapped on the fly as the vIOMMU maps/unmaps them. These IOVA ranges can potentially be mapped anywhere in the vIOMMU IOVA space.

Due to this dynamic nature of vIOMMU mapping/unmapping, tracking only the currently mapped IOVA ranges, as done in the non-vIOMMU case, doesn't work very well.

Instead, to support device dirty tracking when vIOMMU is enabled, track the entire vIOMMU IOVA space. If that fails (IOVA space can be rather big and we might hit HW limitation), try tracking smaller range while marking untracked ranges dirty.

---
 include/hw/vfio/vfio-common.h |   2 +
 hw/vfio/common.c              | 153 ++++++++++++++++++++++++++++++----
 2 files changed, 138 insertions(+), 17 deletions(-)
include/hw/vfio/vfio-common.h +2 -0
index cde6ffb9d6..15109c311d 100644
@@ -97,6 +97,8 @@ typedef struct VFIOContainer {
     unsigned int dma_max_mappings;
     IOVATree *mappings;
     QemuMutex mappings_mutex;
+    /* Represents the range [0, giommu_tracked_range) not inclusive */
+    hwaddr giommu_tracked_range;
     QLIST_HEAD(, VFIOGuestIOMMU) giommu_list;
     QLIST_HEAD(, VFIOHostDMAWindow) hostwin_list;
     QLIST_HEAD(, VFIOGroup) group_list;
hw/vfio/common.c +136 -17

218 lines not on this page · raw message

Signature · 1 line
2.26.3
#39 lore raw

PATCH 16/18 vfio/common: Optimize device dirty page tracking with vIOMMU

In reply to Avihai Horon

When vIOMMU is enabled, syncing dirty page bitmaps is done by replaying the vIOMMU mappings and querying the dirty bitmap for each mapping.

With device dirty tracking this causes a lot of overhead, since the HW is queried many times (even with small idle guest this can end up with thousands of calls to HW).

Optimize this by de-coupling dirty bitmap query from vIOMMU replay. Now a single dirty bitmap is queried per vIOMMU MR section, which is then used for all corresponding vIOMMU mappings within that MR section.

---
 hw/vfio/common.c | 85 ++++++++++++++++++++++++++++++++++++++++++++++--
 1 file changed, 83 insertions(+), 2 deletions(-)
hw/vfio/common.c +83 -2
index c3a27cbbd5..4f27cd669f 100644
@@ -1848,8 +1848,42 @@ out:
 typedef struct {
     IOMMUNotifier n;
     VFIOGuestIOMMU *giommu;
+    VFIOBitmap *vbmap;
 } vfio_giommu_dirty_notifier;
 
+static int vfio_iommu_set_dirty_bitmap(VFIOContainer *container,
+                                       vfio_giommu_dirty_notifier *gdn,
+                                       hwaddr iova, hwaddr size,
+                                       ram_addr_t ram_addr)
+{
+    VFIOBitmap *vbmap = gdn->vbmap;
+    VFIOBitmap *dst_vbmap;
+    hwaddr start_iova = REAL_HOST_PAGE_ALIGN(gdn->n.start);
+    hwaddr copy_offset;
+
+    dst_vbmap = vfio_bitmap_alloc(size);
+    if (!dst_vbmap) {
+        return -errno;
+    }
+
+    if (!vfio_iommu_range_is_device_tracked(container, iova, size)) {
+        bitmap_set(dst_vbmap->bitmap, 0, dst_vbmap->pages);
+
+        goto out;
+    }
+
+    copy_offset = (iova - start_iova) / qemu_real_host_page_size();
+    bitmap_copy_with_src_offset(dst_vbmap->bitmap, vbmap->bitmap, copy_offset,
+                                dst_vbmap->pages);
+
+out:
+    cpu_physical_memory_set_dirty_lebitmap(dst_vbmap->bitmap, ram_addr,
+                                           dst_vbmap->pages);
+    vfio_bitmap_dealloc(dst_vbmap);
+
+    return 0;
+}
+
 static void vfio_iommu_map_dirty_notify(IOMMUNotifier *n, IOMMUTLBEntry *iotlb)
 {
     vfio_giommu_dirty_notifier *gdn = container_of(n,
@@ -1870,8 +1904,15 @@ static void vfio_iommu_map_dirty_notify(IOMMUNotifier *n, IOMMUTLBEntry *iotlb)
 
     rcu_read_lock();
     if (vfio_get_xlat_addr(iotlb, NULL, &translated_addr, NULL)) {
-        ret = vfio_get_dirty_bitmap(container, iova, iotlb->addr_mask + 1,
-                                    translated_addr);
+        if (gdn->vbmap) {
+            ret = vfio_iommu_set_dirty_bitmap(container, gdn, iova,
+                                              iotlb->addr_mask + 1,
+                                              translated_addr);
+        } else {
+            ret = vfio_get_dirty_bitmap(container, iova, iotlb->addr_mask + 1,
+                                        translated_addr);
+        }
+
         if (ret) {
             error_report("vfio_iommu_map_dirty_notify(%p, 0x%"HWADDR_PRIx", "
                          "0x%"HWADDR_PRIx") = %d (%s)",
@@ -1935,6 +1976,7 @@ static int vfio_sync_iommu_dirty_bitmap(VFIOContainer *container,
 {
     VFIOGuestIOMMU *giommu;
     bool found = false;
+    VFIOBitmap *vbmap = NULL;
     Int128 llend;
     vfio_giommu_dirty_notifier gdn;
     int idx;
@@ -1952,6 +1994,7 @@ static int vfio_sync_iommu_dirty_bitmap(VFIOContainer *container,
     }
 
     gdn.giommu = giommu;
+    gdn.vbmap = NULL;
     idx = memory_region_iommu_attrs_to_index(giommu->iommu_mr,
                                              MEMTXATTRS_UNSPECIFIED);
 
@@ -1959,11 +2002,49 @@ static int vfio_sync_iommu_dirty_bitmap(VFIOContainer *container,
                        section->size);
     llend = int128_sub(llend, int128_one());
 
+    /*
+     * Optimize device dirty tracking if the MR section is at least partially
+     * tracked. Optimization is done by querying a single dirty bitmap for the
+     * entire range instead of querying dirty bitmap for each vIOMMU mapping.
+     */
+    if (vfio_devices_all_device_dirty_tracking(container)) {
+        hwaddr start = REAL_HOST_PAGE_ALIGN(section->offset_within_region);
+        hwaddr end = int128_get64(llend);
+        hwaddr size;
+        int ret;
+
+        if (start >= container->giommu_tracked_range) {
+            goto notifier_init;
+        }
+
+        size = REAL_HOST_PAGE_ALIGN(
+            MIN(container->giommu_tracked_range - 1, end) - start);
+
+        vbmap = vfio_bitmap_alloc(size);
+        if (!vbmap) {
+            return -errno;
+        }
+
+        ret = vfio_devices_query_dirty_bitmap(container, vbmap, start, size);
+        if (ret) {
+            vfio_bitmap_dealloc(vbmap);
+
+            return ret;
+        }
+
+        gdn.vbmap = vbmap;
+    }
+
+notifier_init:
     iommu_notifier_init(&gdn.n, vfio_iommu_map_dirty_notify, IOMMU_NOTIFIER_MAP,
                         section->offset_within_region, int128_get64(llend),
                         idx);
     memory_region_iommu_replay(giommu->iommu_mr, &gdn.n);
 
+    if (vbmap) {
+        vfio_bitmap_dealloc(vbmap);
+    }
+
     return 0;
 }
 
Signature · 1 line
2.26.3
#40 lore raw

PATCH 17/18 vfio/migration: Query device dirty page tracking support

In reply to Avihai Horon

From: Joao Martins <[email protected]>

Now that everything has been set up for device dirty page tracking, query the device for device dirty page tracking support.

---
 hw/vfio/migration.c | 15 +++++++++++++++
 1 file changed, 15 insertions(+)
hw/vfio/migration.c +15 -0
index 2a0a663023..5aeda47345 100644
@@ -658,6 +658,19 @@ static int vfio_migration_query_flags(VFIODevice *vbasedev, uint64_t *mig_flags)
     return 0;
 }
 
+static bool vfio_dma_logging_supported(VFIODevice *vbasedev)
+{
+    uint64_t buf[DIV_ROUND_UP(sizeof(struct vfio_device_feature),
+                              sizeof(uint64_t))] = {};
+    struct vfio_device_feature *feature = (struct vfio_device_feature *)buf;
+
+    feature->argsz = sizeof(buf);
+    feature->flags =
+        VFIO_DEVICE_FEATURE_PROBE | VFIO_DEVICE_FEATURE_DMA_LOGGING_START;
+
+    return !ioctl(vbasedev->fd, VFIO_DEVICE_FEATURE, feature);
+}
+
 static int vfio_migration_init(VFIODevice *vbasedev)
 {
     int ret;
@@ -693,6 +706,8 @@ static int vfio_migration_init(VFIODevice *vbasedev)
     migration->data_fd = -1;
     migration->mig_flags = mig_flags;
 
+    vbasedev->dirty_pages_supported = vfio_dma_logging_supported(vbasedev);
+
     oid = vmstate_if_get_id(VMSTATE_IF(DEVICE(obj)));
     if (oid) {
         path = g_strdup_printf("%s/vfio", oid);
Signature · 1 line
2.26.3
#41 lore raw

PATCH 18/18 docs/devel: Document VFIO device dirty page tracking

In reply to Avihai Horon

Adjust the VFIO dirty page tracking documentation and add a section to describe device dirty page tracking.

---
 docs/devel/vfio-migration.rst | 50 ++++++++++++++++++++++-------------
 1 file changed, 32 insertions(+), 18 deletions(-)
docs/devel/vfio-migration.rst +32 -18
index 51f5e1a537..c02f62e534 100644
@@ -63,22 +63,37 @@ System memory dirty pages tracking
 ----------------------------------
 
 A ``log_global_start`` and ``log_global_stop`` memory listener callback informs
-the VFIO IOMMU module to start and stop dirty page tracking. A ``log_sync``
-memory listener callback marks those system memory pages as dirty which are
-used for DMA by the VFIO device. The dirty pages bitmap is queried per
-container. All pages pinned by the vendor driver through external APIs have to
-be marked as dirty during migration. When there are CPU writes, CPU dirty page
-tracking can identify dirtied pages, but any page pinned by the vendor driver
-can also be written by the device. There is currently no device or IOMMU
-support for dirty page tracking in hardware.
+the VFIO dirty tracking module to start and stop dirty page tracking. A
+``log_sync`` memory listener callback queries the dirty page bitmap from the
+dirty tracking module and marks system memory pages which were DMA-ed by the
+VFIO device as dirty. The dirty page bitmap is queried per container.
+
+Currently there are two ways dirty page tracking can be done:
+(1) Device dirty tracking:
+In this method the device is responsible to log and report its DMAs. This
+method can be used only if the device is capable of tracking its DMAs.
+Discovering device capability, starting and stopping dirty tracking, and
+syncing the dirty bitmaps from the device are done using the DMA logging uAPI.
+More info about the uAPI can be found in the comments of the
+``vfio_device_feature_dma_logging_control`` and
+``vfio_device_feature_dma_logging_report`` structures in the header file
+linux-headers/linux/vfio.h.
+
+(2) VFIO IOMMU module:
+In this method dirty tracking is done by IOMMU. However, there is currently no
+IOMMU support for dirty page tracking. For this reason, all pages are
+perpetually marked dirty, unless the device driver pins pages through external
+APIs in which case only those pinned pages are perpetually marked dirty.
+
+If the above two methods are not supported, all pages are perpetually marked
+dirty by QEMU.
 
 By default, dirty pages are tracked during pre-copy as well as stop-and-copy
-phase. So, a page pinned by the vendor driver will be copied to the destination
-in both phases. Copying dirty pages in pre-copy phase helps QEMU to predict if
-it can achieve its downtime tolerances. If QEMU during pre-copy phase keeps
-finding dirty pages continuously, then it understands that even in stop-and-copy
-phase, it is likely to find dirty pages and can predict the downtime
-accordingly.
+phase. So, a page marked as dirty will be copied to the destination in both
+phases. Copying dirty pages in pre-copy phase helps QEMU to predict if it can
+achieve its downtime tolerances. If QEMU during pre-copy phase keeps finding
+dirty pages continuously, then it understands that even in stop-and-copy phase,
+it is likely to find dirty pages and can predict the downtime accordingly.
 
 QEMU also provides a per device opt-out option ``pre-copy-dirty-page-tracking``
 which disables querying the dirty bitmap during pre-copy phase. If it is set to
@@ -89,10 +104,9 @@ System memory dirty pages tracking when vIOMMU is enabled
 ---------------------------------------------------------
 
 With vIOMMU, an IO virtual address range can get unmapped while in pre-copy
-phase of migration. In that case, the unmap ioctl returns any dirty pages in
-that range and QEMU reports corresponding guest physical pages dirty. During
-stop-and-copy phase, an IOMMU notifier is used to get a callback for mapped
-pages and then dirty pages bitmap is fetched from VFIO IOMMU modules for those
+phase of migration. In that case, dirty page bitmap for this range is queried
+and synced with QEMU. During stop-and-copy phase, an IOMMU notifier is used to
+get a callback for mapped pages and then dirty page bitmap is fetched for those
 mapped ranges.
 
 Flow of state changes during Live migration
Signature · 1 line
2.26.3