Enhance the vfio-virtio driver to support live migration
Subsystems (5)
Yishai Hadas
maintainerv4 cover Enhance the vfio-virtio driver to support live migration
This series enhances the vfio-virtio driver to support live migration for virtio-net Virtual Functions (VFs) that are migration-capable.
This series follows the Virtio 1.4 specification to implement the necessary device parts commands, enabling a device to participate in the live migration process.
The key VFIO features implemented include: VFIO_MIGRATION_STOP_COPY, VFIO_MIGRATION_P2P, VFIO_MIGRATION_PRE_COPY.
The implementation integrates with the VFIO subsystem via vfio_pci_core and incorporates Virtio-specific logic to handle the migration process.
Migration functionality follows the definitions in uapi/vfio.h and uses the Virtio VF-to-PF admin queue command channel for executing the device parts related commands.
Patch Overview:
The first four patches focus on the Virtio layer and address the following:
- Define the layout of the device parts commands required as part of the migration process.
- Provide APIs to enable upper layers (e.g., VFIO, net) to execute the related device parts commands.
The last three patches focus on the VFIO layer:
- Extend the vfio-virtio driver to support live migration for Virtio-net VFs.
- Move legacy I/O operations to a separate file, which is compiled only when VIRTIO_PCI_ADMIN_LEGACY is configured, ensuring that live migration depends solely on VIRTIO_PCI.
Additional Notes:
- The kernel protocol between the source and target devices includes a header containing metadata such as record size, tag, and flags. The record size allows the target to read a complete image from the source before passing device part data. This follows the Virtio specification, which mandates that partial device parts are not supplied. The tag and flags serve as placeholders for future extensions to the kernel protocol between the source and target, ensuring backward and forward compatibility.
- Both the source and target comply with the Virtio specification by using a device part object with a unique ID during the migration process. As this resource is limited to a maximum of 255, its lifecycle is confined to periods when live migration is active.
- According to the Virtio specification, a device has only two states:
RUNNING and STOPPED. Consequently, certain VFIO transitions (e.g., RUNNING_P2P->STOP, STOP->RUNNING_P2P) are treated as no-ops. When transitioning to RUNNING_P2P, the device state is set to STOP and remains STOPPED until it transitions back from RUNNING_P2P->RUNNING, at which point it resumes its RUNNING state. During transition to STOP, the virtio device only stops initiating outgoing requests(e.g. DMA, MSIx, etc.) but still must accept incoming operations.
- Furthermore, the Virtio specification does not support reading partial or incremental device contexts. This means that during the PRE_COPY state, the vfio-virtio driver reads the full device state. This step is beneficial because it allows the device to send some "initial data" before moving to the STOP_COPY state, thus reducing downtime by preparing early and warming-up. As the device state can be changed and the benefit is highest when the pre copy data closely matches the final data we read it in a rate limiter mode and reporting no data available for some time interval after the previous call. With PRE_COPY enabled, we observed a downtime reduction of approximately 70-75% in various scenarios compared to when PRE_COPY was disabled, while keeping the total migration time nearly the same.
- Support for dirty page tracking during migration will be provided via the IOMMUFD framework.
- This series has been successfully tested on Virtio-net VF devices.
Changes from V3:
https://www.spinics.net/lists/kvm/msg363002.html
Vfio:
Patch #5:
- Fix an unwind flow as part of virtiovf_add_migration_pages() as noted by Alex.
- Fix an unwind flow as part of virtiovf_pci_save/resume_device_data() to not kfree the migf upon 'end' as it will be done as part of fput(migf->filp) -> virtiovf_release_file().
Changes from V2:
https://lore.kernel.org/kvm/[email protected]/T/
Vfio:
Patch #7:
- Remove the 'select IOMMUFD_DRIVER' clause.
As noted by Alex and Joao, IOMMUFD_DRIVER is only configuring in iova_bitmap support independent of IOMMUFD, which is not needed for this driver.
Changes from V1:
https://lore.kernel.org/kvm/[email protected]/T/
Virtio:
Patches #1-#4:
- Add Acked-by: Michael S. Tsirkin <[email protected]>
Vfio:
Patch #5:
- Set the driver 'ops' within probe(), including the call to virtiovf_set_migratable() right after allocating the virtiodev. Consequently, virtiovf_pci_init_device() is now only used for legacy IO access, where its specific initialization is performed. This approach was recommended by Alex to avoid modifying the 'ops' pointer after the core device has been allocated.
- Fix multi-line comment style.
Patch #6:
- Improve the rate limiter flow to account for the 'initial/dirty bytes' which potentially might not being fully read yet, as noted by Alex.
- Add a comment for the rate limiter uristic. As Alex mentioned this would be useful for future maintenance.
Patch #7:
- Add a new boolean Kconfig entry named CONFIG_VIRTIO_VFIO_PCI_ADMIN_LEGACY as a submenu item under the primary driver functionality for live migration. This new entry allows users to selectively enable legacy access functionality, providing a dedicated description for each feature.
This addition aligns with Alex's recommendation in this area.
Changes from V0:
https://lore.kernel.org/kvm/[email protected]/T/
Vfio:
Patch #5:
- Enhance the commit log to provide a clearer explanation of P2P behavior over Virtio devices, as discussed on the mailing list.
Patch #6:
- Implement the rate limiter mechanism as part of the PRE_COPY state, following Alex's suggestion.
- Update the commit log to include actual data demonstrating the impact of PRE_COPY, as requested by Alex.
Patch #7:
- Update the default driver operations (i.e., vfio_device_ops) to use the live migration set, and expand it to include the legacy I/O operations if they are compiled and supported.
Yishai
Yishai Hadas (7):
virtio_pci: Introduce device parts access commands
virtio: Extend the admin command to include the result size
virtio: Manage device and driver capabilities via the admin commands
virtio-pci: Introduce APIs to execute device parts admin commands vfio/virtio: Add support for the basic live migration functionality vfio/virtio: Add PRE_COPY support for live migration vfio/virtio: Enable live migration once VIRTIO_PCI was configured
drivers/vfio/pci/virtio/Kconfig | 42 +- drivers/vfio/pci/virtio/Makefile | 3 +- drivers/vfio/pci/virtio/common.h | 127 +++ drivers/vfio/pci/virtio/legacy_io.c | 418 +++++++++ drivers/vfio/pci/virtio/main.c | 476 ++-------- drivers/vfio/pci/virtio/migrate.c | 1337 +++++++++++++++++++++++++++ drivers/virtio/virtio_pci_common.h | 19 +- drivers/virtio/virtio_pci_modern.c | 457 ++++++++- include/linux/virtio.h | 1 + include/linux/virtio_pci_admin.h | 11 + include/uapi/linux/virtio_pci.h | 131 +++ 11 files changed, 2601 insertions(+), 421 deletions(-) create mode 100644 drivers/vfio/pci/virtio/common.h create mode 100644 drivers/vfio/pci/virtio/legacy_io.c create mode 100644 drivers/vfio/pci/virtio/migrate.c
Signature · 1 line
2.27.0
Yishai Hadas
maintainerPATCH v4 1/7 virtio_pci: Introduce device parts access commands
Introduce device parts access commands via the admin queue.
These commands and their structure adhere to the Virtio 1.4 specification.
--- include/uapi/linux/virtio_pci.h | 131 ++++++++++++++++++++++++++++++++ 1 file changed, 131 insertions(+)
include/uapi/linux/virtio_pci.h +131 -0
@@ -40,6 +40,7 @@#define _LINUX_VIRTIO_PCI_H#include <linux/types.h> +#include <linux/kernel.h> #ifndef VIRTIO_PCI_NO_LEGACY
@@ -240,6 +241,17 @@ struct virtio_pci_cfg_cap {#define VIRTIO_ADMIN_CMD_LEGACY_DEV_CFG_READ 0x5#define VIRTIO_ADMIN_CMD_LEGACY_NOTIFY_INFO 0x6 +/* Device parts access commands. */ +#define VIRTIO_ADMIN_CMD_CAP_ID_LIST_QUERY 0x7 +#define VIRTIO_ADMIN_CMD_DEVICE_CAP_GET 0x8 +#define VIRTIO_ADMIN_CMD_DRIVER_CAP_SET 0x9 +#define VIRTIO_ADMIN_CMD_RESOURCE_OBJ_CREATE 0xa +#define VIRTIO_ADMIN_CMD_RESOURCE_OBJ_DESTROY 0xd +#define VIRTIO_ADMIN_CMD_DEV_PARTS_METADATA_GET 0xe +#define VIRTIO_ADMIN_CMD_DEV_PARTS_GET 0xf +#define VIRTIO_ADMIN_CMD_DEV_PARTS_SET 0x10 +#define VIRTIO_ADMIN_CMD_DEV_MODE_SET 0x11 +struct virtio_admin_cmd_hdr {__le16 opcode;/*
@@ -286,4 +298,123 @@ struct virtio_admin_cmd_notify_info_result {struct virtio_admin_cmd_notify_info_data entries[VIRTIO_ADMIN_CMD_MAX_NOTIFY_INFO ]; }; +#define VIRTIO_DEV_PARTS_CAP 0x0000 + +struct virtio_dev_parts_cap { +__u8 get_parts_resource_objects_limit; +__u8 set_parts_resource_objects_limit; +}; + +#define MAX_CAP_ID __KERNEL_DIV_ROUND_UP(VIRTIO_DEV_PARTS_CAP + 1, 64) + +struct virtio_admin_cmd_query_cap_id_result { +__le64 supported_caps[MAX_CAP_ID ]; +}; + +struct virtio_admin_cmd_cap_get_data { +__le16 id; +__u8 reserved[6 ]; +}; + +struct virtio_admin_cmd_cap_set_data { +__le16 id; +__u8 reserved[6 ]; +__u8 cap_specific_data[]; +}; + +struct virtio_admin_cmd_resource_obj_cmd_hdr { +__le16 type; +__u8 reserved[2 ]; +__le32 id;/* Indicates unique resource object id per resource object type */ +}; + +struct virtio_admin_cmd_resource_obj_create_data { +struct virtio_admin_cmd_resource_obj_cmd_hdr hdr; +__le64 flags; +__u8 resource_obj_specific_data[]; +}; + +#define VIRTIO_RESOURCE_OBJ_DEV_PARTS 0 + +#define VIRTIO_RESOURCE_OBJ_DEV_PARTS_TYPE_GET 0 +#define VIRTIO_RESOURCE_OBJ_DEV_PARTS_TYPE_SET 1 + +struct virtio_resource_obj_dev_parts { +__u8 type; +__u8 reserved[7 ]; +}; + +#define VIRTIO_ADMIN_CMD_DEV_PARTS_METADATA_TYPE_SIZE 0 +#define VIRTIO_ADMIN_CMD_DEV_PARTS_METADATA_TYPE_COUNT 1 +#define VIRTIO_ADMIN_CMD_DEV_PARTS_METADATA_TYPE_LIST 2 + +struct virtio_admin_cmd_dev_parts_metadata_data { +struct virtio_admin_cmd_resource_obj_cmd_hdr hdr; +__u8 type; +__u8 reserved[7 ]; +}; + +#define VIRTIO_DEV_PART_F_OPTIONAL 0 + +struct virtio_dev_part_hdr { +__le16 part_type; +__u8 flags; +__u8 reserved; +union { +struct { +__le32 offset; +__le32 reserved; + } pci_common_cfg; +struct { +__le16 index; +__u8 reserved[6 ]; + } vq_index; + } selector; +__le32 length; +}; + +struct virtio_dev_part { +struct virtio_dev_part_hdr hdr; +__u8 value[]; +}; + +struct virtio_admin_cmd_dev_parts_metadata_result { +union { +struct { +__le32 size; +__le32 reserved; + } parts_size; +struct { +__le32 count; +__le32 reserved; + } hdr_list_count; +struct { +__le32 count; +__le32 reserved; +struct virtio_dev_part_hdr hdrs[]; + } hdr_list; + }; +}; + +#define VIRTIO_ADMIN_CMD_DEV_PARTS_GET_TYPE_SELECTED 0 +#define VIRTIO_ADMIN_CMD_DEV_PARTS_GET_TYPE_ALL 1 + +struct virtio_admin_cmd_dev_parts_get_data { +struct virtio_admin_cmd_resource_obj_cmd_hdr hdr; +__u8 type; +__u8 reserved[7 ]; +struct virtio_dev_part_hdr hdr_list[]; +}; + +struct virtio_admin_cmd_dev_parts_set_data { +struct virtio_admin_cmd_resource_obj_cmd_hdr hdr; +struct virtio_dev_part parts[]; +}; + +#define VIRTIO_ADMIN_CMD_DEV_MODE_F_STOPPED 0 + +struct virtio_admin_cmd_dev_mode_set_data { +__u8 flags; +}; +#endif
Signature · 1 line
2.27.0
Yishai Hadas
maintainerPATCH v4 2/7 virtio: Extend the admin command to include the result size
In reply to Yishai HadasExtend the admin command by incorporating a result size field.
This allows higher layers to determine the actual result size from the backend when this information is not included in the result_sg.
The additional information introduced here will be used in subsequent patches of this series.
--- drivers/virtio/virtio_pci_modern.c | 4 +++- include/linux/virtio.h | 1 + 2 files changed, 4 insertions(+), 1 deletion(-)
drivers/virtio/virtio_pci_modern.c +3 -1
@@ -64,8 +64,10 @@ void vp_modern_avq_done(struct virtqueue *vq)spin_lock_irqsave (&admin_vq->lock, flags);do {virtqueue_disable_cb (vq); -while ((cmd =virtqueue_get_buf (vq, &len))) +while ((cmd =virtqueue_get_buf (vq, &len))) { + cmd->result_sg_size = len;complete (&cmd->completion); + } }while (!virtqueue_enable_cb (vq));spin_unlock_irqrestore (&admin_vq->lock, flags); }
include/linux/virtio.h +1 -0
@@ -111,6 +111,7 @@ struct virtio_admin_cmd {struct scatterlist *data_sg;struct scatterlist *result_sg;struct completion completion; +u32 result_sg_size;int ret; };
Signature · 1 line
2.27.0
Yishai Hadas
maintainerPATCH v4 3/7 virtio: Manage device and driver capabilities via the admin commands
In reply to Yishai HadasManage device and driver capabilities via the admin commands.
The device exposes its supported features and resource object limits via an administrative command called VIRTIO_ADMIN_CMD_CAP_ID_LIST_QUERY, using the 'self group type.'
Each capability is identified by a unique ID, and the driver communicates the functionality and resource limits it plans to utilize.
The capability VIRTIO_DEV_PARTS_CAP specifically represents the device's parts resource object limit.
Manage the device's parts resource object ID using a common IDA for both get and set operations.
--- drivers/virtio/virtio_pci_common.h | 13 +++- drivers/virtio/virtio_pci_modern.c | 105 +++++++++++++++++++++++++++++ 2 files changed, 116 insertions(+), 2 deletions(-)
drivers/virtio/virtio_pci_common.h +11 -2
@@ -48,6 +48,9 @@ struct virtio_pci_admin_vq {/* Protects virtqueue access. */ spinlock_t lock;u64 supported_cmds; +u64 supported_caps; +u8 max_dev_parts_objects; +struct ida dev_parts_ida;/* Name of the admin queue: avq.$vq_index. */ char name[10 ];u16 vq_index;
@@ -167,15 +170,21 @@ struct virtio_device *virtio_pci_vf_get_pf_dev(struct pci_dev *pdev); BIT_ULL(VIRTIO_ADMIN_CMD_LEGACY_DEV_CFG_READ) | \ BIT_ULL(VIRTIO_ADMIN_CMD_LEGACY_NOTIFY_INFO)) +#define VIRTIO_DEV_PARTS_ADMIN_CMD_BITMAP \ + (BIT_ULL(VIRTIO_ADMIN_CMD_CAP_ID_LIST_QUERY) | \ + BIT_ULL(VIRTIO_ADMIN_CMD_DRIVER_CAP_SET) | \ + BIT_ULL(VIRTIO_ADMIN_CMD_DEVICE_CAP_GET)) +/* Unlike modern drivers which support hardware virtio devices, legacy drivers * assume software-based devices: e.g. they don't use proper memory barriers * on ARM, use big endian on PPC, etc. X86 drivers are mostly ok though, more * or less by chance. For now, only support legacy IO on X86. */ #ifdef CONFIG_VIRTIO_PCI_ADMIN_LEGACY -#define VIRTIO_ADMIN_CMD_BITMAP VIRTIO_LEGACY_ADMIN_CMD_BITMAP +#define VIRTIO_ADMIN_CMD_BITMAP (VIRTIO_LEGACY_ADMIN_CMD_BITMAP | \ + VIRTIO_DEV_PARTS_ADMIN_CMD_BITMAP)#else -#define VIRTIO_ADMIN_CMD_BITMAP 0 +#define VIRTIO_ADMIN_CMD_BITMAP VIRTIO_DEV_PARTS_ADMIN_CMD_BITMAP#endif void vp_modern_avq_done (struct virtqueue *vq);
drivers/virtio/virtio_pci_modern.c +105 -0
117 lines not on this page · raw message
Signature · 1 line
2.27.0
Yishai Hadas
maintainerPATCH v4 4/7 virtio-pci: Introduce APIs to execute device parts admin commands
In reply to Yishai HadasIntroduce APIs to handle the execution of device parts admin commands.
These APIs cover functionalities such as mode setting, object creation and destruction, and operations like parts get/set and metadata retrieval.
These APIs will be utilized in upcoming patches within this series.
--- drivers/virtio/virtio_pci_common.h | 8 +- drivers/virtio/virtio_pci_modern.c | 348 +++++++++++++++++++++++++++++ include/linux/virtio_pci_admin.h | 11 + 3 files changed, 366 insertions(+), 1 deletion(-)
drivers/virtio/virtio_pci_common.h +7 -1
@@ -173,7 +173,13 @@ struct virtio_device *virtio_pci_vf_get_pf_dev(struct pci_dev *pdev);#define VIRTIO_DEV_PARTS_ADMIN_CMD_BITMAP \ (BIT_ULL(VIRTIO_ADMIN_CMD_CAP_ID_LIST_QUERY) | \ BIT_ULL(VIRTIO_ADMIN_CMD_DRIVER_CAP_SET) | \ - BIT_ULL(VIRTIO_ADMIN_CMD_DEVICE_CAP_GET)) + BIT_ULL(VIRTIO_ADMIN_CMD_DEVICE_CAP_GET) | \ + BIT_ULL(VIRTIO_ADMIN_CMD_RESOURCE_OBJ_CREATE) | \ + BIT_ULL(VIRTIO_ADMIN_CMD_RESOURCE_OBJ_DESTROY) | \ + BIT_ULL(VIRTIO_ADMIN_CMD_DEV_PARTS_METADATA_GET) | \ + BIT_ULL(VIRTIO_ADMIN_CMD_DEV_PARTS_GET) | \ + BIT_ULL(VIRTIO_ADMIN_CMD_DEV_PARTS_SET) | \ + BIT_ULL(VIRTIO_ADMIN_CMD_DEV_MODE_SET))/* Unlike modern drivers which support hardware virtio devices, legacy drivers * assume software-based devices: e.g. they don't use proper memory barriers
drivers/virtio/virtio_pci_modern.c +348 -0
360 lines not on this page · raw message
include/linux/virtio_pci_admin.h +11 -0
@@ -20,4 +20,15 @@ int virtio_pci_admin_legacy_io_notify_info(struct pci_dev *pdev, u64 *bar_offset);#endif +bool virtio_pci_admin_has_dev_parts (struct pci_dev *pdev); +int virtio_pci_admin_mode_set (struct pci_dev *pdev,u8 mode); +int virtio_pci_admin_obj_create (struct pci_dev *pdev,u16 obj_type,u8 operation_type, +u32 *obj_id); +int virtio_pci_admin_obj_destroy (struct pci_dev *pdev,u16 obj_type,u32 id); +int virtio_pci_admin_dev_parts_metadata_get (struct pci_dev *pdev,u16 obj_type, +u32 id,u8 metadata_type,u32 *out); +int virtio_pci_admin_dev_parts_get (struct pci_dev *pdev,u16 obj_type,u32 id, +u8 get_type,struct scatterlist *res_sg,u32 *res_size); +int virtio_pci_admin_dev_parts_set (struct pci_dev *pdev,struct scatterlist *data_sg); +#endif /* _LINUX_VIRTIO_PCI_ADMIN_H */
Signature · 1 line
2.27.0
Yishai Hadas
maintainerPATCH v4 5/7 vfio/virtio: Add support for the basic live migration functionality
In reply to Yishai HadasAdd support for basic live migration functionality in VFIO over virtio-net devices, aligned with the virtio device specification 1.4.
This includes the following VFIO features:VFIO_MIGRATION_STOP_COPY, VFIO_MIGRATION_P2P.
The implementation registers with the VFIO subsystem using vfio_pci_core and then incorporates the virtio-specific logic for the migration process.
The migration follows the definitions in uapi/vfio.h and leverages the virtio VF-to-PF admin queue command channel for execution device parts related commands.
Additional Notes:
-----------------
The kernel protocol between the source and target devices contains a header with metadata, including record size, tag, and flags.
The record size allows the target to recognize and read a complete image from the source before passing the device part data. This adheres to the virtio device specification, which mandates that partial device parts cannot be supplied.
The tag and flags serve as placeholders for future extensions of the kernel protocol between the source and target, ensuring backward and forward compatibility.
Both the source and target comply with the virtio device specification by using a device part object with a unique ID as part of the migration process. Since this resource is limited to a maximum of 255, its lifecycle is confined to periods with an active live migration flow.
According to the virtio specification, a device has only two modes:
RUNNING and STOPPED. As a result, certain VFIO transitions (i.e., RUNNING_P2P->STOP, STOP->RUNNING_P2P) are treated as no-ops. When transitioning to RUNNING_P2P, the device state is set to STOP, and it will remain STOPPED until the transition out of RUNNING_P2P->RUNNING, at which point it returns to RUNNING. During transition to STOP, the virtio device only stops initiating outgoing requests(e.g. DMA, MSIx, etc.) but still must accept incoming operations.
--- drivers/vfio/pci/virtio/Makefile | 2 +- drivers/vfio/pci/virtio/common.h | 104 +++ drivers/vfio/pci/virtio/main.c | 82 ++- drivers/vfio/pci/virtio/migrate.c | 1122 +++++++++++++++++++++++++++++ 4 files changed, 1285 insertions(+), 25 deletions(-) create mode 100644 drivers/vfio/pci/virtio/common.h create mode 100644 drivers/vfio/pci/virtio/migrate.c
drivers/vfio/pci/virtio/Makefile +1 -1
@@ -1,3 +1,3 @@ # SPDX-License-Identifier: GPL-2.0-only obj-$(CONFIG_VIRTIO_VFIO_PCI) += virtio-vfio-pci.o -virtio-vfio-pci-y := main.o +virtio-vfio-pci-y := main.o migrate.o
drivers/vfio/pci/virtio/common.h new file +104 -0
104 lines not on this page · raw message
drivers/vfio/pci/virtio/main.c +58 -24
150 lines not on this page · raw message
drivers/vfio/pci/virtio/migrate.c new file +1122 -0
1,122 lines not on this page · raw message
Signature · 1 line
2.27.0
Re: [PATCH V4 vfio 5/7] vfio/virtio: Add support for the basic live migration functionality
Quoted 5 lines · On Wed, 13 Nov 2024 13:51:58 +0200 Yishai Hadas <[email protected]> wrote:
drivers/vfio/pci/virtio/migrate.c new file +0
-0
...
drivers/vfio/pci/virtio/migrate.c
Quoted 12 lines
drivers/vfio/pci/virtio/migrate.c new file +12
-0+static int virtiovf_add_migration_pages (struct virtiovf_data_buffer *buf, +unsigned int npages) +{ +unsigned int to_alloc = npages; +struct page **page_list; +unsigned long filled; +unsigned int to_fill; +int ret; +int i; + + to_fill = min_t(unsigned int, npages, PAGE_SIZE / sizeof(*page_list)); + page_list =kvzalloc (to_fill *sizeof (*page_list),GFP_KERNEL_ACCOUNT );
checkpatch spots the following:
WARNING: Prefer kvcalloc over kvzalloc with multiply #416: FILE: drivers/vfio/pci/virtio/migrate.c:71:
+ page_list = kvzalloc(to_fill * sizeof(*page_list), GFP_KERNEL_ACCOUNT);
With your approval I'll update with the following on commit:
drivers/vfio/pci/virtio/migrate.c +1 -1
@@ -68,7 +68,7 @@ static int virtiovf_add_migration_pages(struct virtiovf_data_buffer *buf,int i; to_fill = min_t(unsigned int, npages, PAGE_SIZE / sizeof(*page_list)); - page_list =kvzalloc (to_fill *sizeof (*page_list),GFP_KERNEL_ACCOUNT ); + page_list =kvcalloc (to_fill,sizeof (*page_list),GFP_KERNEL_ACCOUNT );if (!page_list)return -ENOMEM ;
Thanks,
Alex
Yishai Hadas
maintainerRe: [PATCH V4 vfio 5/7] vfio/virtio: Add support for the basic live migration functionality
Quoted 28 lines · Alex Williamson
Quoted 5 lines · On Wed, 13 Nov 2024 13:51:58 +0200 Yishai Hadas <[email protected]> wrote:
drivers/vfio/pci/virtio/migrate.c new file +0
-0...
drivers/vfio/pci/virtio/migrate.c
Quoted 12 lines
drivers/vfio/pci/virtio/migrate.c new file +12
-0+static int virtiovf_add_migration_pages (struct virtiovf_data_buffer *buf, +unsigned int npages) +{ +unsigned int to_alloc = npages; +struct page **page_list; +unsigned long filled; +unsigned int to_fill; +int ret; +int i; + + to_fill = min_t(unsigned int, npages, PAGE_SIZE / sizeof(*page_list)); + page_list =kvzalloc (to_fill *sizeof (*page_list),GFP_KERNEL_ACCOUNT );checkpatch spots the following:
WARNING: Prefer kvcalloc over kvzalloc with multiply #416: FILE:
drivers/vfio/pci/virtio/migrate.c:71:(quoted diff) +1
-0+ page_list = kvzalloc(to_fill * sizeof(*page_list), GFP_KERNEL_ACCOUNT);
Quoted 1 line
With your approval I'll update with the following on commit:
Sure, thanks.
Yishai
Quoted 14 lines
drivers/vfio/pci/virtio/migrate.c +1
-1@@ -68,7 +68,7 @@ static int virtiovf_add_migration_pages(struct virtiovf_data_buffer *buf,int i; to_fill = min_t(unsigned int, npages, PAGE_SIZE / sizeof(*page_list)); - page_list =kvzalloc (to_fill *sizeof (*page_list),GFP_KERNEL_ACCOUNT ); + page_list =kvcalloc (to_fill,sizeof (*page_list),GFP_KERNEL_ACCOUNT );if (!page_list)return -ENOMEM ;
Yishai Hadas
maintainerPATCH v4 6/7 vfio/virtio: Add PRE_COPY support for live migration
In reply to Yishai HadasAdd PRE_COPY support for live migration.
This functionality may reduce the downtime upon STOP_COPY as of letting the target machine to get some 'initial data' from the source once the machine is still in its RUNNING state and let it prepares itself pre-ahead to get the final STOP_COPY data.
As the Virtio specification does not support reading partial or incremental device contexts. This means that during the PRE_COPY state, the vfio-virtio driver reads the full device state.
As the device state can be changed and the benefit is highest when the pre copy data closely matches the final data we read it in a rate limiter mode.
This means we avoid reading new data from the device for a specified time interval after the last read.
With PRE_COPY enabled, we observed a downtime reduction of approximately 70-75% in various scenarios compared to when PRE_COPY was disabled, while keeping the total migration time nearly the same.
--- drivers/vfio/pci/virtio/common.h | 4 + drivers/vfio/pci/virtio/migrate.c | 231 ++++++++++++++++++++++++++++-- 2 files changed, 227 insertions(+), 8 deletions(-)
drivers/vfio/pci/virtio/common.h +4 -0
@@ -10,6 +10,8 @@enum virtiovf_migf_state {VIRTIOVF_MIGF_STATE_ERROR =1 , +VIRTIOVF_MIGF_STATE_PRECOPY =2 , +VIRTIOVF_MIGF_STATE_COMPLETE =3 , };enum virtiovf_load_state {
@@ -57,6 +59,8 @@ struct virtiovf_migration_file {/* synchronize access to the file state */ struct mutex lock;loff_t max_pos; +u64 pre_copy_initial_bytes; +struct ratelimit_state pre_copy_rl_state;u64 record_size;u32 record_tag; u8 has_obj_id:1;
drivers/vfio/pci/virtio/migrate.c +223 -8
325 lines not on this page · raw message
Signature · 1 line
2.27.0
Yishai Hadas
maintainerPATCH v4 7/7 vfio/virtio: Enable live migration once VIRTIO_PCI was configured
In reply to Yishai HadasNow that the driver supports live migration, only the legacy IO functionality depends on config VIRTIO_PCI_ADMIN_LEGACY.
As part of that we introduce a bool configuration option as a sub menu under the driver's main live migration feature named VIRTIO_VFIO_PCI_ADMIN_LEGACY, to control the legacy IO functionality.
This will let users configuring the kernel, know which features from the description might be available in the resulting driver.
As of that, move the legacy IO into a separate file to be compiled only once CONFIG_VIRTIO_VFIO_PCI_ADMIN_LEGACY was configured and let the live migration depends only on VIRTIO_PCI.
--- drivers/vfio/pci/virtio/Kconfig | 42 ++- drivers/vfio/pci/virtio/Makefile | 1 + drivers/vfio/pci/virtio/common.h | 19 ++ drivers/vfio/pci/virtio/legacy_io.c | 418 ++++++++++++++++++++++++++++ drivers/vfio/pci/virtio/main.c | 410 ++------------------------- 5 files changed, 489 insertions(+), 401 deletions(-) create mode 100644 drivers/vfio/pci/virtio/legacy_io.c
drivers/vfio/pci/virtio/Kconfig +29 -13
@@ -1,15 +1,31 @@ # SPDX-License-Identifier: GPL-2.0-only config VIRTIO_VFIO_PCI - tristate "VFIO support for VIRTIO NET PCI devices" - depends on VIRTIO_PCI && VIRTIO_PCI_ADMIN_LEGACY - select VFIO_PCI_CORE - help - This provides support for exposing VIRTIO NET VF devices which support - legacy IO access, using the VFIO framework that can work with a legacy - virtio driver in the guest. - Based on PCIe spec, VFs do not support I/O Space. - As of that this driver emulates I/O BAR in software to let a VF be - seen as a transitional device by its users and let it work with - a legacy driver. - - If you don't know what to do here, say N. + tristate "VFIO support for VIRTIO NET PCI VF devices" + depends on VIRTIO_PCI + select VFIO_PCI_CORE + help + This provides migration support for VIRTIO NET PCI VF devices + using the VFIO framework. Migration support requires the + SR-IOV PF device to support specific VIRTIO extensions, + otherwise this driver provides no additional functionality + beyond vfio-pci. + + Migration support in this driver relies on dirty page tracking + provided by the IOMMU hardware and exposed through IOMMUFD, any + other use cases are dis-recommended. + + If you don't know what to do here, say N. + +config VIRTIO_VFIO_PCI_ADMIN_LEGACY + bool "Legacy I/O support for VIRTIO NET PCI VF devices" + depends on VIRTIO_VFIO_PCI && VIRTIO_PCI_ADMIN_LEGACY + default y + help + This extends the virtio-vfio-pci driver to support legacy I/O + access, allowing use of legacy virtio drivers with VIRTIO NET + PCI VF devices. Legacy I/O support requires the SR-IOV PF + device to support and enable specific VIRTIO extensions, + otherwise this driver provides no additional functionality + beyond vfio-pci. + + If you don't know what to do here, say N.
drivers/vfio/pci/virtio/Makefile +1 -0
@@ -1,3 +1,4 @@ # SPDX-License-Identifier: GPL-2.0-only obj-$(CONFIG_VIRTIO_VFIO_PCI) += virtio-vfio-pci.o virtio-vfio-pci-y := main.o migrate.o +virtio-vfio-pci-$(CONFIG_VIRTIO_VFIO_PCI_ADMIN_LEGACY) += legacy_io.o
drivers/vfio/pci/virtio/common.h +19 -0
@@ -78,6 +78,7 @@ struct virtiovf_migration_file {struct virtiovf_pci_core_device {struct vfio_pci_core_device core_device; +#ifdef CONFIG_VIRTIO_VFIO_PCI_ADMIN_LEGACY u8 *bar0_virtual_buf;/* synchronize access to the virtual buf */ struct mutex bar_mutex;
@@ -87,6 +88,7 @@ struct virtiovf_pci_core_device {__le16 pci_cmd;u8 bar0_virtual_buf_size;u8 notify_bar; +#endif /* LM related */ u8 migrate_cap:1;
@@ -105,4 +107,21 @@ void virtiovf_open_migration(struct virtiovf_pci_core_device *virtvdev);void virtiovf_close_migration (struct virtiovf_pci_core_device *virtvdev);void virtiovf_migration_reset_done (struct pci_dev *pdev); +#ifdef CONFIG_VIRTIO_VFIO_PCI_ADMIN_LEGACY +int virtiovf_open_legacy_io (struct virtiovf_pci_core_device *virtvdev); +long virtiovf_vfio_pci_core_ioctl (struct vfio_device *core_vdev, +unsigned int cmd,unsigned long arg); +int virtiovf_pci_ioctl_get_region_info (struct vfio_device *core_vdev, +unsigned int cmd,unsigned long arg); +ssize_t virtiovf_pci_core_write (struct vfio_device *core_vdev, +const char __user *buf,size_t count, +loff_t *ppos); +ssize_t virtiovf_pci_core_read (struct vfio_device *core_vdev,char __user *buf, +size_t count,loff_t *ppos); +bool virtiovf_support_legacy_io (struct pci_dev *pdev); +int virtiovf_init_legacy_io (struct virtiovf_pci_core_device *virtvdev); +void virtiovf_release_legacy_io (struct virtiovf_pci_core_device *virtvdev); +void virtiovf_legacy_io_reset_done (struct pci_dev *pdev); +#endif +#endif /* VIRTIO_VFIO_COMMON_H */
drivers/vfio/pci/virtio/legacy_io.c new file +418 -0
418 lines not on this page · raw message
drivers/vfio/pci/virtio/main.c +22 -388
482 lines not on this page · raw message
Signature · 1 line
2.27.0
Alex Williamson
AppliedQuoted 2 lines · On Wed, 13 Nov 2024 13:51:53 +0200 Yishai Hadas <[email protected]> wrote:
This series enhances the vfio-virtio driver to support live migration for virtio-net Virtual Functions (VFs) that are migration-capable.
Applied to vfio next branch for v6.13. Thanks,
Alex