Skip to contentSkip to conversation

vfio/hisilicon: add acc live migration driver

RFC PATCH In review Longfang Liu 5 participants 32 messages Apr 13, 2021 – May 27, 2021

Subsystems (3)
lore
Share & tools
lore

Longfang Liu

author maintainer
#0 lore raw

cover vfio/hisilicon: add acc live migration driver

The live migration solution relies on the vfio_device_migration_info protocol. The structure vfio_device_migration_info is placed at the 0th offset of the VFIO_REGION_SUBTYPE_MIGRATION region to get and set VFIO device related migration information. Field accesses from this structure are only supported at their native width and alignment. Otherwise, the result is undefined and vendor drivers should return an error.

(1).The driver framework is based on vfio_pci_register_dev_region() of vfio-pci, and then a new live migration region is added, and the live migration is realized through the ops of this region.

(2).In order to ensure the compatibility of the devices before and after the migration, the device compatibility information check will be performed in the Pre-copy stage. If the check fails, an error will be returned and the source VM will exit the migration function.

(3).After the compatibility check is passed, it will enter the Stop-and-copy stage. At this time, all the live migration data will be copied, and then saved to the VF device of the destination, and then the VF device of the destination will be started and the VM of the source will be exited.

Longfang Liu (3):
vfio/hisilicon: add acc live migration driver
vfio/hisilicon: register the driver to vfio
vfio/hisilicom: add debugfs for driver

 drivers/vfio/pci/Kconfig                      |    8 +
 drivers/vfio/pci/Makefile                     |    1 +
 drivers/vfio/pci/hisilicon/acc_vf_migration.c | 1337 +++++++++++++++++++++++++
 drivers/vfio/pci/hisilicon/acc_vf_migration.h |  170 ++++
 drivers/vfio/pci/vfio_pci.c                   |   11 +
 drivers/vfio/pci/vfio_pci_private.h           |    9 +
 6 files changed, 1536 insertions(+)
 create mode 100644 drivers/vfio/pci/hisilicon/acc_vf_migration.c
 create mode 100644 drivers/vfio/pci/hisilicon/acc_vf_migration.h
Signature · 1 line
2.8.1
author maintainer
#1 lore raw

RFC PATCH 1/3 vfio/hisilicon: add acc live migration driver

This driver adds the code required by Hisilicon accelerator device to realize the live migration function. It mainly includes the following functions:
(1).Match the accelerator device with the vfio-pci driver framework.
(2).Processing of the status of the live migration function and processing of the migration data. (3).Operation and data processing of accelerator hardware devices

---
 drivers/vfio/pci/Kconfig                      |    8 +
 drivers/vfio/pci/Makefile                     |    1 +
 drivers/vfio/pci/hisilicon/acc_vf_migration.c | 1144 +++++++++++++++++++++++++
 drivers/vfio/pci/hisilicon/acc_vf_migration.h |  168 ++++
 4 files changed, 1321 insertions(+)
 create mode 100644 drivers/vfio/pci/hisilicon/acc_vf_migration.c
 create mode 100644 drivers/vfio/pci/hisilicon/acc_vf_migration.h
drivers/vfio/pci/Kconfig +8 -0
index 4abddbe..c1b181f 100644
@@ -45,3 +45,11 @@ config VFIO_PCI_NVLINK2
 	depends on VFIO_PCI && PPC_POWERNV && SPAPR_TCE_IOMMU
 	help
 	  VFIO PCI support for P9 Witherspoon machine with NVIDIA V100 GPUs
+
+config VFIO_PCI_HISI_MIGRATION
+	bool "Support for Hisilicon Live Migaration"
+	depends on ARM64 && ACPI
+	select PCI_IOV
+	default y
+	help
+	  Support for HiSilicon vfio pci to support VF live migration.
drivers/vfio/pci/Makefile +1 -0
index eff97a7..d17cec4 100644
@@ -4,5 +4,6 @@ vfio-pci-y := vfio_pci.o vfio_pci_intrs.o vfio_pci_rdwr.o vfio_pci_config.o
 vfio-pci-$(CONFIG_VFIO_PCI_IGD) += vfio_pci_igd.o
 vfio-pci-$(CONFIG_VFIO_PCI_NVLINK2) += vfio_pci_nvlink2.o
 vfio-pci-$(CONFIG_S390) += vfio_pci_zdev.o
+vfio-pci-$(CONFIG_VFIO_PCI_HISI_MIGRATION) += hisilicon/acc_vf_migration.o
 
 obj-$(CONFIG_VFIO_PCI) += vfio-pci.o
drivers/vfio/pci/hisilicon/acc_vf_migration.c new file +1144 -0

1,144 lines not on this page · raw message

drivers/vfio/pci/hisilicon/acc_vf_migration.h new file +168 -0

168 lines not on this page · raw message

Signature · 1 line
2.8.1
author maintainer
#2 lore raw

RFC PATCH 2/3 vfio/hisilicon: register the driver to vfio

In reply to Longfang Liu

Register the live migration driver of the accelerator module to vfio

---
 drivers/vfio/pci/vfio_pci.c         | 11 +++++++++++
 drivers/vfio/pci/vfio_pci_private.h |  9 +++++++++
 2 files changed, 20 insertions(+)
drivers/vfio/pci/vfio_pci.c +11 -0
index 65e7e6b..e1b0e37 100644
@@ -407,6 +407,17 @@ static int vfio_pci_enable(struct vfio_pci_device *vdev)
 		}
 	}
 
+	if (pdev->vendor == PCI_VENDOR_ID_HUAWEI &&
+	    IS_ENABLED(CONFIG_VFIO_PCI_HISI_MIGRATION)) {
+		ret = vfio_pci_hisilicon_acc_init(vdev);
+		if (ret && ret != -ENODEV) {
+			dev_warn(&vdev->pdev->dev,
+				 "Failed to setup Hisilicon ACC region\n");
+			vfio_pci_disable(vdev);
+			return ret;
+		}
+	}
+
 	vfio_pci_probe_mmaps(vdev);
 
 	return 0;
drivers/vfio/pci/vfio_pci_private.h +9 -0
index 9cd1882..83c51be 100644
@@ -214,6 +214,15 @@ static inline int vfio_pci_ibm_npu2_init(struct vfio_pci_device *vdev)
 }
 #endif
 
+#ifdef CONFIG_VFIO_PCI_HISI_MIGRATION
+extern int vfio_pci_hisilicon_acc_init(struct vfio_pci_device *vdev);
+#else
+static inline int vfio_pci_hisilicon_acc_init(struct vfio_pci_device *vdev)
+{
+	return -ENODEV;
+}
+#endif
+
 #ifdef CONFIG_S390
 extern int vfio_pci_info_zdev_add_caps(struct vfio_pci_device *vdev,
 				       struct vfio_info_cap *caps);
Signature · 1 line
2.8.1
#3 lore raw

Re: [RFC PATCH 2/3] vfio/hisilicon: register the driver to vfio

Quoted 11 lines · Longfang Liu

Register the live migration driver of the accelerator module to vfio

 drivers/vfio/pci/vfio_pci.c         | 11 +++++++++++
 drivers/vfio/pci/vfio_pci_private.h |  9 +++++++++
 2 files changed, 20 insertions(+)
drivers/vfio/pci/vfio_pci.c +1 -0
index 65e7e6b..e1b0e37 100644
+++ b/drivers/vfio/pci/vfio_pci.c

drivers/vfio/pci/vfio_pci.c static int vfio_pci_enable(struct vfio_pci_device *vdev) · line 412

Quoted 6 lines
drivers/vfio/pci/vfio_pci.c +3 -0
@@ -407,6 +407,17 @@ static int vfio_pci_enable(struct vfio_pci_device *vdev)
 		}
 	}
 
+	if (pdev->vendor == PCI_VENDOR_ID_HUAWEI &&
+	    IS_ENABLED(CONFIG_VFIO_PCI_HISI_MIGRATION)) {
+		ret = vfio_pci_hisilicon_acc_init(vdev);

This is exactly what we want to avoid with the work we are doing on the vfio-pci modularity.

It is a complete mess to just keep adding more stuff here, and as we've discussed to death really ugly to have such a ridiculously wide ID match.

You should be working with us on that project and base your work on top of Max's series.. Alex given the interest here I think we should find some way forward while we work on completed version of the mlx5 pci vfio driver..

I'm also confused how this works securely at all, as a general rule a VFIO PCI driver cannot access the MMIO memory of the function it is planning to assign to the guest. There is a lot of danger that the guest could access that MMIO space one way or another.

Here I see the driver obtaining a devm_ioremap() of the same pdev it is going to assign (and I really wonder why pci_release_mem_regions() exists at all..)

This is why the mlx5 RFC was showing how to juggle two PCI devices via the aux device connector.

Jason

author maintainer
#4 lore raw

Re: [RFC PATCH 2/3] vfio/hisilicon: register the driver to vfio

Quoted 31 lines · Jason Gunthorpe
Quoted 11 lines · Longfang Liu

Register the live migration driver of the accelerator module to vfio

 drivers/vfio/pci/vfio_pci.c         | 11 +++++++++++
 drivers/vfio/pci/vfio_pci_private.h |  9 +++++++++
 2 files changed, 20 insertions(+)
drivers/vfio/pci/vfio_pci.c +1 -0
index 65e7e6b..e1b0e37 100644
+++ b/drivers/vfio/pci/vfio_pci.c

drivers/vfio/pci/vfio_pci.c static int vfio_pci_enable(struct vfio_pci_device *vdev) · line 412

Quoted 6 lines
drivers/vfio/pci/vfio_pci.c +3 -0
@@ -407,6 +407,17 @@ static int vfio_pci_enable(struct vfio_pci_device *vdev)
 		}
 	}
 
+	if (pdev->vendor == PCI_VENDOR_ID_HUAWEI &&
+	    IS_ENABLED(CONFIG_VFIO_PCI_HISI_MIGRATION)) {
+		ret = vfio_pci_hisilicon_acc_init(vdev);

This is exactly what we want to avoid with the work we are doing on the vfio-pci modularity.

It is a complete mess to just keep adding more stuff here, and as we've discussed to death really ugly to have such a ridiculously wide ID match.

You should be working with us on that project and base your work on top of Max's series.. Alex given the interest here I think we should find some way forward while we work on completed version of the mlx5 pci vfio driver..

Quoted 5 lines

I'm also confused how this works securely at all, as a general rule a VFIO PCI driver cannot access the MMIO memory of the function it is planning to assign to the guest. There is a lot of danger that the guest could access that MMIO space one way or another.

VF's MMIO memory is divided into two parts, one is the guest part, and the other is the live migration part. They do not affect each other, so there is no security problem.

Quoted 4 lines

Here I see the driver obtaining a devm_ioremap() of the same pdev it is going to assign (and I really wonder why pci_release_mem_regions() exists at all..)

The driver here obtains the VF's MMIO memory address through devm_ioremap(), and adding pci_release_mem_regions() is to get the MMIO memory address in the guest after the driver here obtains the MMIO memory address.

If pci_release_mem_regions() is not added here,
The guests using pci_request_mem_regions() will return an error. Then, the guest will not be able to obtain the MMIO address of the VF.

Quoted 3 lines

This is why the mlx5 RFC was showing how to juggle two PCI devices via the aux device connector.

I also noticed the mlx5 RFC, but it will take some time to analyze it. If the analysis process goes well. I will import the mlx5 RFC into our driver and use this solution to test and verify our live migration function.

If we use the mlx5 RFC, what should we pay attention to?

Quoted 3 lines

Jason
.

Thanks.
Longfang.

#5 lore raw

Re: [RFC PATCH 2/3] vfio/hisilicon: register the driver to vfio

Quoted 8 lines · liulongfang
Quoted 4 lines

I'm also confused how this works securely at all, as a general rule a VFIO PCI driver cannot access the MMIO memory of the function it is planning to assign to the guest. There is a lot of danger that the guest could access that MMIO space one way or another.

VF's MMIO memory is divided into two parts, one is the guest part, and the other is the live migration part. They do not affect each other, so there is no security problem.

AFAIK there are several scenarios where a guest can access this MMIO memory using DMA even if it is not mapped into the guest for CPU access.

Quoted 3 lines

If pci_release_mem_regions() is not added here,
The guests using pci_request_mem_regions() will return an error. Then, the guest will not be able to obtain the MMIO address of the VF.

Which is why VFIO has this protection to prevent sharing MMIO regions on the VF assigned to the guest

Jason

author maintainer
#6 lore raw

Re: [RFC PATCH 2/3] vfio/hisilicon: register the driver to vfio

Quoted 7 lines · Jason Gunthorpe
Quoted 5 lines · liulongfang
Quoted 4 lines

I'm also confused how this works securely at all, as a general rule a VFIO PCI driver cannot access the MMIO memory of the function it is planning to assign to the guest. There is a lot of danger that the guest could access that MMIO space one way or another.

Quoted 8 lines
Quoted 3 lines

VF's MMIO memory is divided into two parts, one is the guest part, and the other is the live migration part. They do not affect each other, so there is no security problem.

AFAIK there are several scenarios where a guest can access this MMIO memory using DMA even if it is not mapped into the guest for CPU access.

The hardware divides VF's MMIO memory into two parts. The live migration driver in the host uses the live migration part, and the device driver in the guest uses the guest part. They obtain the address of VF's MMIO memory in their respective drivers, although these two parts The memory is continuous on the hardware device, but due to the needs of the drive function, they will not perform operations on another part of the memory, and the device hardware also independently responds to the operation commands of the two parts.
So, I still don't understand what the security risk you are talking about is, and what do you think the security design should look like?
Can you elaborate on it?

Quoted 10 lines
Quoted 3 lines

If pci_release_mem_regions() is not added here,
The guests using pci_request_mem_regions() will return an error. Then, the guest will not be able to obtain the MMIO address of the VF.

Which is why VFIO has this protection to prevent sharing MMIO regions on the VF assigned to the guest

Jason
.

Thanks.
Longfang.

#7 lore raw

Re: [RFC PATCH 2/3] vfio/hisilicon: register the driver to vfio

Quoted 16 lines · liulongfang
Quoted 7 lines · Jason Gunthorpe
Quoted 5 lines · liulongfang
Quoted 4 lines

I'm also confused how this works securely at all, as a general rule a VFIO PCI driver cannot access the MMIO memory of the function it is planning to assign to the guest. There is a lot of danger that the guest could access that MMIO space one way or another.

Quoted 8 lines
Quoted 3 lines

VF's MMIO memory is divided into two parts, one is the guest part, and the other is the live migration part. They do not affect each other, so there is no security problem.

AFAIK there are several scenarios where a guest can access this MMIO memory using DMA even if it is not mapped into the guest for CPU access.

Quoted 8 lines

The hardware divides VF's MMIO memory into two parts. The live migration driver in the host uses the live migration part, and the device driver in the guest uses the guest part. They obtain the address of VF's MMIO memory in their respective drivers, although these two parts The memory is continuous on the hardware device, but due to the needs of the drive function, they will not perform operations on another part of the memory, and the device hardware also independently responds to the operation commands of the two parts.

It doesn't matter, the memory is still under the same PCI BDF and VFIO supports scenarios where devices in the same IOMMU group are not isolated from each other.

This is why the granual of isolation is a PCI BDF - VFIO directly blocks kernel drivers from attaching to PCI BDFs that are not completely isolated from VFIO BDF.

Bypassing this prevention and attaching a kernel driver directly to the same BDF being exposed to the guest breaks that isolation model.

Quoted 3 lines

So, I still don't understand what the security risk you are talking about is, and what do you think the security design should look like?
Can you elaborate on it?

Each security domain must have its own PCI BDF.

The migration control registers must be on a different VF from the VF being plugged into a guest and the two VFs have to be in different IOMMU groups to ensure they are isolated from each other.

Jason

author maintainer
#8 lore raw

Re: [RFC PATCH 2/3] vfio/hisilicon: register the driver to vfio

Quoted 34 lines · Jason Gunthorpe
Quoted 16 lines · liulongfang
Quoted 7 lines · Jason Gunthorpe
Quoted 5 lines · liulongfang
Quoted 4 lines

I'm also confused how this works securely at all, as a general rule a VFIO PCI driver cannot access the MMIO memory of the function it is planning to assign to the guest. There is a lot of danger that the guest could access that MMIO space one way or another.

Quoted 8 lines
Quoted 3 lines

VF's MMIO memory is divided into two parts, one is the guest part, and the other is the live migration part. They do not affect each other, so there is no security problem.

AFAIK there are several scenarios where a guest can access this MMIO memory using DMA even if it is not mapped into the guest for CPU access.

Quoted 8 lines

The hardware divides VF's MMIO memory into two parts. The live migration driver in the host uses the live migration part, and the device driver in the guest uses the guest part. They obtain the address of VF's MMIO memory in their respective drivers, although these two parts The memory is continuous on the hardware device, but due to the needs of the drive function, they will not perform operations on another part of the memory, and the device hardware also independently responds to the operation commands of the two parts.

It doesn't matter, the memory is still under the same PCI BDF and VFIO supports scenarios where devices in the same IOMMU group are not isolated from each other.

This is why the granual of isolation is a PCI BDF - VFIO directly blocks kernel drivers from attaching to PCI BDFs that are not completely isolated from VFIO BDF.

Quoted 9 lines

Bypassing this prevention and attaching a kernel driver directly to the same BDF being exposed to the guest breaks that isolation model.

Quoted 3 lines

So, I still don't understand what the security risk you are talking about is, and what do you think the security design should look like?
Can you elaborate on it?

Each security domain must have its own PCI BDF.

The basic unit to perform the live migration function is the VF, and the basic function of the VF is the business function of the device. If the live migration function and the business function are completely separated, and they are bound to their respective VFs, it will result in the ability to execute the business in the guest A functional VF cannot perform live migration, and a VF with a live migration function cannot perform business functions in the guest.

In fact, the scenario that requires live migration is to migrate its business functions to the VFs of other hosts when the VF's business functions are heavily loaded.
This usage scenario itself requires that the VF needs to have these two functions at the same time.

Quoted 7 lines

The migration control registers must be on a different VF from the VF being plugged into a guest and the two VFs have to be in different IOMMU groups to ensure they are isolated from each other.

Jason
.

Thanks.
Longfang.

#9 lore raw

Re: [RFC PATCH 2/3] vfio/hisilicon: register the driver to vfio

Quoted 13 lines · liulongfang
Quoted 6 lines
Quoted 3 lines

So, I still don't understand what the security risk you are talking about is, and what do you think the security design should look like?
Can you elaborate on it?

Each security domain must have its own PCI BDF.

The basic unit to perform the live migration function is the VF, and the basic function of the VF is the business function of the device. If the live migration function and the business function are completely separated, and they are bound to their respective VFs, it will result in the ability to execute the business in the guest A functional VF cannot perform live migration, and a VF with a live migration function cannot perform business functions in the guest.

Quoted 5 lines

In fact, the scenario that requires live migration is to migrate its business functions to the VFs of other hosts when the VF's business functions are heavily loaded.
This usage scenario itself requires that the VF needs to have these two functions at the same time.

Other vendors are managing, it is not an unsolvable problem.

I think these patches can't advance in any form without somehow addressing the isolation issue.

Jason

#10 lore raw

Re: [RFC PATCH 2/3] vfio/hisilicon: register the driver to vfio

In reply to Jason Gunthorpe
Quoted 41 lines · On Tue, 20 Apr 2021 09:59:57 -0300 Jason Gunthorpe <[email protected]> wrote:
Quoted 16 lines · liulongfang
Quoted 7 lines · Jason Gunthorpe
Quoted 5 lines · liulongfang
Quoted 4 lines

I'm also confused how this works securely at all, as a general rule a VFIO PCI driver cannot access the MMIO memory of the function it is planning to assign to the guest. There is a lot of danger that the guest could access that MMIO space one way or another.

Quoted 8 lines
Quoted 3 lines

VF's MMIO memory is divided into two parts, one is the guest part, and the other is the live migration part. They do not affect each other, so there is no security problem.

AFAIK there are several scenarios where a guest can access this MMIO memory using DMA even if it is not mapped into the guest for CPU access.

Quoted 8 lines

The hardware divides VF's MMIO memory into two parts. The live migration driver in the host uses the live migration part, and the device driver in the guest uses the guest part. They obtain the address of VF's MMIO memory in their respective drivers, although these two parts The memory is continuous on the hardware device, but due to the needs of the drive function, they will not perform operations on another part of the memory, and the device hardware also independently responds to the operation commands of the two parts.

It doesn't matter, the memory is still under the same PCI BDF and VFIO supports scenarios where devices in the same IOMMU group are not isolated from each other.

This is why the granual of isolation is a PCI BDF - VFIO directly blocks kernel drivers from attaching to PCI BDFs that are not completely isolated from VFIO BDF.

Bypassing this prevention and attaching a kernel driver directly to the same BDF being exposed to the guest breaks that isolation model.

Quoted 3 lines

So, I still don't understand what the security risk you are talking about is, and what do you think the security design should look like?
Can you elaborate on it?

Quoted 5 lines

Each security domain must have its own PCI BDF.

The migration control registers must be on a different VF from the VF being plugged into a guest and the two VFs have to be in different IOMMU groups to ensure they are isolated from each other.

I think that's a solution, I don't know if it's the only solution. AIUI, the issue here is that we have a device specific kernel driver extending vfio-pci with migration support for this device by using an MMIO region of the same device. This is susceptible to DMA
manipulation by the user device. Whether that's a security issue or
not depends on how the user can break the device. If the scope is limited to breaking their own device, they can do that any number of ways and it's not very interesting. If the user can manipulate device state in order to trigger an exploit of the host-side kernel driver, that's obviously more of a problem.

The other side of this is that if migration support can be implemented entirely within the VF using this portion of the device MMIO space, why do we need the host kernel to support this rather than implementing it in userspace? For example, QEMU could know about this device, manipulate the BAR size to expose only the operational portion of MMIO to the VM and use the remainder to support migration itself. I'm afraid that just like mdev, the vfio migration uAPI is going to be used as an excuse to create kernel drivers simply to be able to make use of that uAPI. I haven't looked at this driver to know if it has some other reason to exist beyond what could be done through vfio-pci and userspace migration support. Thanks,

Alex

#11 lore raw

Re: [RFC PATCH 2/3] vfio/hisilicon: register the driver to vfio

Quoted 5 lines · Alex Williamson
Quoted 3 lines

The migration control registers must be on a different VF from the VF being plugged into a guest and the two VFs have to be in different IOMMU groups to ensure they are isolated from each other.

I think that's a solution, I don't know if it's the only solution.

Maybe, but that approach does offer DMA access for the migration. For instance to implement something that needs a lot of data like migrating a complicated device state, or dirty page tracking or whatver.

This driver seems very simple - it has only 17 state elements - and doesn't use DMA.

I can't quite tell, but does this pass the hypervisor BAR into the guest anyhow? That would certainly be an adquate statement that it is safe, assuming someone did a good security analysis.

Quoted 3 lines

ways and it's not very interesting. If the user can manipulate device state in order to trigger an exploit of the host-side kernel driver, that's obviously more of a problem.

Well, for instance, we have an implementation of (VFIO_DEVICE_STATE_SAVING | VFIO_DEVICE_STATE_RUNNING) which means the guest CPUs are still running and a hostile guest can be manipulating the device.

But this driver is running code, like vf_qm_state_pre_save() in this state. Looks very suspicious.

One quick attack I can imagine is to use the guest CPU to DOS the migration and permanently block it, eg by causing qm_mb() or other looping functions to fail.

There may be worse things possible, it is a bit hard to tell just from the code.

.. also drivers should not be open coding ARM assembly as in qm_mb_write()

.. and also, code can not randomly call pci_get_drvdata() on a struct device it isn't attached to haven't verified the right driver is bound, or locked correctly.

Quoted 5 lines

manipulate the BAR size to expose only the operational portion of MMIO to the VM and use the remainder to support migration itself. I'm afraid that just like mdev, the vfio migration uAPI is going to be used as an excuse to create kernel drivers simply to be able to make use of that uAPI.

I thought that is the general direction people had agreed on during the IDXD mdev discussion?

People want the IOCTLs from VFIO to be the single API to program all the VMMs to and to not implement user space drivers..

This actually seems like a great candidate for a userspace driver.

I would like to know we are still settled on this direction as the mlx5 drivers we are working on also have some complicated option to be user space only.

Jason

author maintainer
#12 lore raw

Re: [RFC PATCH 2/3] vfio/hisilicon: register the driver to vfio

Quoted 8 lines · Jason Gunthorpe
Quoted 5 lines · Alex Williamson
Quoted 3 lines

The migration control registers must be on a different VF from the VF being plugged into a guest and the two VFs have to be in different IOMMU groups to ensure they are isolated from each other.

I think that's a solution, I don't know if it's the only solution.

Quoted 8 lines

Maybe, but that approach does offer DMA access for the migration. For instance to implement something that needs a lot of data like migrating a complicated device state, or dirty page tracking or whatver.

This driver seems very simple - it has only 17 state elements - and doesn't use DMA.

Yes,the operating address of this driver is the MMIO address, not the DMA address, but the internal hardware DMA address is used as data for migration.

Quoted 20 lines

I can't quite tell, but does this pass the hypervisor BAR into the guest anyhow? That would certainly be an adquate statement that it is safe, assuming someone did a good security analysis.

Quoted 3 lines

ways and it's not very interesting. If the user can manipulate device state in order to trigger an exploit of the host-side kernel driver, that's obviously more of a problem.

Well, for instance, we have an implementation of (VFIO_DEVICE_STATE_SAVING | VFIO_DEVICE_STATE_RUNNING) which means the guest CPUs are still running and a hostile guest can be manipulating the device.

But this driver is running code, like vf_qm_state_pre_save() in this state. Looks very suspicious.

One quick attack I can imagine is to use the guest CPU to DOS the migration and permanently block it, eg by causing qm_mb() or other looping functions to fail.

Quoted 6 lines

There may be worse things possible, it is a bit hard to tell just from the code.

.. also drivers should not be open coding ARM assembly as in qm_mb_write()

OK, these codes need to be encapsulated and should not be presented in this driver.

Quoted 4 lines

.. and also, code can not randomly call pci_get_drvdata() on a struct device it isn't attached to haven't verified the right driver is bound, or locked correctly.

Yes, This call needs to be placed in an encapsulation interface, and access protection needs to be added.

Quoted 21 lines
Quoted 5 lines

manipulate the BAR size to expose only the operational portion of MMIO to the VM and use the remainder to support migration itself. I'm afraid that just like mdev, the vfio migration uAPI is going to be used as an excuse to create kernel drivers simply to be able to make use of that uAPI.

I thought that is the general direction people had agreed on during the IDXD mdev discussion?

People want the IOCTLs from VFIO to be the single API to program all the VMMs to and to not implement user space drivers..

This actually seems like a great candidate for a userspace driver.

I would like to know we are still settled on this direction as the mlx5 drivers we are working on also have some complicated option to be user space only.

Jason
.

author maintainer
#13 lore raw

Re: [RFC PATCH 2/3] vfio/hisilicon: register the driver to vfio

In reply to Alex Williamson
Quoted 46 lines · Alex Williamson
Quoted 43 lines · On Tue, 20 Apr 2021 09:59:57 -0300 Jason Gunthorpe <[email protected]> wrote:
Quoted 16 lines · liulongfang
Quoted 7 lines · Jason Gunthorpe
Quoted 5 lines · liulongfang
Quoted 4 lines

I'm also confused how this works securely at all, as a general rule a VFIO PCI driver cannot access the MMIO memory of the function it is planning to assign to the guest. There is a lot of danger that the guest could access that MMIO space one way or another.

Quoted 8 lines
Quoted 3 lines

VF's MMIO memory is divided into two parts, one is the guest part, and the other is the live migration part. They do not affect each other, so there is no security problem.

AFAIK there are several scenarios where a guest can access this MMIO memory using DMA even if it is not mapped into the guest for CPU access.

Quoted 8 lines

The hardware divides VF's MMIO memory into two parts. The live migration driver in the host uses the live migration part, and the device driver in the guest uses the guest part. They obtain the address of VF's MMIO memory in their respective drivers, although these two parts The memory is continuous on the hardware device, but due to the needs of the drive function, they will not perform operations on another part of the memory, and the device hardware also independently responds to the operation commands of the two parts.

It doesn't matter, the memory is still under the same PCI BDF and VFIO supports scenarios where devices in the same IOMMU group are not isolated from each other.

This is why the granual of isolation is a PCI BDF - VFIO directly blocks kernel drivers from attaching to PCI BDFs that are not completely isolated from VFIO BDF.

Bypassing this prevention and attaching a kernel driver directly to the same BDF being exposed to the guest breaks that isolation model.

Quoted 3 lines

So, I still don't understand what the security risk you are talking about is, and what do you think the security design should look like?
Can you elaborate on it?

Each security domain must have its own PCI BDF.

Quoted 7 lines
Quoted 3 lines

The migration control registers must be on a different VF from the VF being plugged into a guest and the two VFs have to be in different IOMMU groups to ensure they are isolated from each other.

I think that's a solution, I don't know if it's the only solution. AIUI, the issue here is that we have a device specific kernel driver extending vfio-pci with migration support for this device by using an

If the two parts of the MMIO region are split into different BAR spaces on the device, the MMIO region of ​​the business function is still placed in BAR2, and the MMIO region of ​​the live migration function is moved to BAR4. Only BAR2 is mapped in the guest. only BAR4 is mapped in the host. This can solve this security issue.

Quoted 6 lines

MMIO region of the same device. This is susceptible to DMA> manipulation by the user device. Whether that's a security issue or> not depends on how the user can break the device. If the scope is
limited to breaking their own device, they can do that any number of ways and it's not very interesting. If the user can manipulate device state in order to trigger an exploit of the host-side kernel driver, that's obviously more of a problem.

Quoted 9 lines

The other side of this is that if migration support can be implemented entirely within the VF using this portion of the device MMIO space, why do we need the host kernel to support this rather than implementing it in userspace? For example, QEMU could know about this device, manipulate the BAR size to expose only the operational portion of MMIO to the VM and use the remainder to support migration itself. I'm afraid that just like mdev, the vfio migration uAPI is going to be used as an excuse to create kernel drivers simply to be able to make use of that uAPI. I haven't looked at this driver to know if it has some

When the accelerator device is designed to support the live migration function, it is based on the uAPI of the migration region to realize the live migration function, so the live migration function requires a driver that connects to this uAPI.
Is this set of interfaces not open to us now?

Quoted 7 lines

other reason to exist beyond what could be done through vfio-pci and userspace migration support. Thanks,

Alex

.

Thanks,
Longfang.

#14 lore raw

Re: [RFC PATCH 2/3] vfio/hisilicon: register the driver to vfio

Quoted 55 lines · On Wed, 21 Apr 2021 17:59:02 +0800 liulongfang <[email protected]> wrote:
Quoted 46 lines · Alex Williamson
Quoted 43 lines · On Tue, 20 Apr 2021 09:59:57 -0300 Jason Gunthorpe <[email protected]> wrote:
Quoted 16 lines · liulongfang
Quoted 7 lines · Jason Gunthorpe
Quoted 5 lines · liulongfang
Quoted 4 lines

I'm also confused how this works securely at all, as a general rule a VFIO PCI driver cannot access the MMIO memory of the function it is planning to assign to the guest. There is a lot of danger that the guest could access that MMIO space one way or another.

Quoted 8 lines
Quoted 3 lines

VF's MMIO memory is divided into two parts, one is the guest part, and the other is the live migration part. They do not affect each other, so there is no security problem.

AFAIK there are several scenarios where a guest can access this MMIO memory using DMA even if it is not mapped into the guest for CPU access.

Quoted 8 lines

The hardware divides VF's MMIO memory into two parts. The live migration driver in the host uses the live migration part, and the device driver in the guest uses the guest part. They obtain the address of VF's MMIO memory in their respective drivers, although these two parts The memory is continuous on the hardware device, but due to the needs of the drive function, they will not perform operations on another part of the memory, and the device hardware also independently responds to the operation commands of the two parts.

It doesn't matter, the memory is still under the same PCI BDF and VFIO supports scenarios where devices in the same IOMMU group are not isolated from each other.

This is why the granual of isolation is a PCI BDF - VFIO directly blocks kernel drivers from attaching to PCI BDFs that are not completely isolated from VFIO BDF.

Bypassing this prevention and attaching a kernel driver directly to the same BDF being exposed to the guest breaks that isolation model.

Quoted 3 lines

So, I still don't understand what the security risk you are talking about is, and what do you think the security design should look like?
Can you elaborate on it?

Each security domain must have its own PCI BDF.

Quoted 7 lines
Quoted 3 lines

The migration control registers must be on a different VF from the VF being plugged into a guest and the two VFs have to be in different IOMMU groups to ensure they are isolated from each other.

I think that's a solution, I don't know if it's the only solution. AIUI, the issue here is that we have a device specific kernel driver extending vfio-pci with migration support for this device by using an

Quoted 5 lines

If the two parts of the MMIO region are split into different BAR spaces on the device, the MMIO region of ​​the business function is still placed in BAR2, and the MMIO region of ​​the live migration function is moved to BAR4. Only BAR2 is mapped in the guest. only BAR4 is mapped in the host. This can solve this security issue.

The concern is really the "on the device" part rather than whether the resources are within the same BAR or not. We need to assume that a user driver can generate a DMA targeting any address in the system, including in this case the user driver could generate a DMA targeting this migration BAR. Ideally this would be routed upstream to the IOMMU where it would be blocked for lack of a translation entry. However, because this range resides on the same PCIe requester ID, it's logically more similar to a two-function device where the functions are considered non-isolated and are therefore exposed within the same IOMMU group. We would not allow a kernel driver for one of those functions and a userspace driver for the other. In this case those drivers are strongly related, but we still need to consider to what extent a malicious user driver can interfere with or exploit the kernel side driver.

Quoted 16 lines
Quoted 6 lines

MMIO region of the same device. This is susceptible to DMA> manipulation by the user device. Whether that's a security issue or> not depends on how the user can break the device. If the scope is
limited to breaking their own device, they can do that any number of ways and it's not very interesting. If the user can manipulate device state in order to trigger an exploit of the host-side kernel driver, that's obviously more of a problem.

Quoted 9 lines

The other side of this is that if migration support can be implemented entirely within the VF using this portion of the device MMIO space, why do we need the host kernel to support this rather than implementing it in userspace? For example, QEMU could know about this device, manipulate the BAR size to expose only the operational portion of MMIO to the VM and use the remainder to support migration itself. I'm afraid that just like mdev, the vfio migration uAPI is going to be used as an excuse to create kernel drivers simply to be able to make use of that uAPI. I haven't looked at this driver to know if it has some

Quoted 5 lines

When the accelerator device is designed to support the live migration function, it is based on the uAPI of the migration region to realize the live migration function, so the live migration function requires a driver that connects to this uAPI.
Is this set of interfaces not open to us now?

In your model, if both BARs are exposed to userspace and a device specific extension in QEMU claims the migration BAR rather than exposing it to the VM, could that driver mimic the migration region uAPI from userspace? For example, you don't need page pinning to interact with the IOMMU, you don't need resources beyond the scope of the endpoint device itself, and the migration info BAR is safe for userspace to manage? If so, then a kernel-based driver to expose a migration uAPI seems like it's only a risk for the kernel, ie. moving what could be a userspace driver into the kernel for the convenience of re-using a kernel uAPI. Thanks,

Alex

author maintainer
#15 lore raw

Re: [RFC PATCH 2/3] vfio/hisilicon: register the driver to vfio

Quoted 113 lines · Alex Williamson
Quoted 55 lines · On Wed, 21 Apr 2021 17:59:02 +0800 liulongfang <[email protected]> wrote:
Quoted 46 lines · Alex Williamson
Quoted 43 lines · On Tue, 20 Apr 2021 09:59:57 -0300 Jason Gunthorpe <[email protected]> wrote:
Quoted 16 lines · liulongfang
Quoted 7 lines · Jason Gunthorpe
Quoted 5 lines · liulongfang
Quoted 4 lines

I'm also confused how this works securely at all, as a general rule a VFIO PCI driver cannot access the MMIO memory of the function it is planning to assign to the guest. There is a lot of danger that the guest could access that MMIO space one way or another.

Quoted 8 lines
Quoted 3 lines

VF's MMIO memory is divided into two parts, one is the guest part, and the other is the live migration part. They do not affect each other, so there is no security problem.

AFAIK there are several scenarios where a guest can access this MMIO memory using DMA even if it is not mapped into the guest for CPU access.

Quoted 8 lines

The hardware divides VF's MMIO memory into two parts. The live migration driver in the host uses the live migration part, and the device driver in the guest uses the guest part. They obtain the address of VF's MMIO memory in their respective drivers, although these two parts The memory is continuous on the hardware device, but due to the needs of the drive function, they will not perform operations on another part of the memory, and the device hardware also independently responds to the operation commands of the two parts.

It doesn't matter, the memory is still under the same PCI BDF and VFIO supports scenarios where devices in the same IOMMU group are not isolated from each other.

This is why the granual of isolation is a PCI BDF - VFIO directly blocks kernel drivers from attaching to PCI BDFs that are not completely isolated from VFIO BDF.

Bypassing this prevention and attaching a kernel driver directly to the same BDF being exposed to the guest breaks that isolation model.

Quoted 3 lines

So, I still don't understand what the security risk you are talking about is, and what do you think the security design should look like?
Can you elaborate on it?

Each security domain must have its own PCI BDF.

Quoted 7 lines
Quoted 3 lines

The migration control registers must be on a different VF from the VF being plugged into a guest and the two VFs have to be in different IOMMU groups to ensure they are isolated from each other.

I think that's a solution, I don't know if it's the only solution. AIUI, the issue here is that we have a device specific kernel driver extending vfio-pci with migration support for this device by using an

Quoted 5 lines

If the two parts of the MMIO region are split into different BAR spaces on the device, the MMIO region of ​​the business function is still placed in BAR2, and the MMIO region of ​​the live migration function is moved to BAR4. Only BAR2 is mapped in the guest. only BAR4 is mapped in the host. This can solve this security issue.

The concern is really the "on the device" part rather than whether the resources are within the same BAR or not. We need to assume that a user driver can generate a DMA targeting any address in the system, including in this case the user driver could generate a DMA targeting this migration BAR. Ideally this would be routed upstream to the IOMMU where it would be blocked for lack of a translation entry. However, because this range resides on the same PCIe requester ID, it's logically more similar to a two-function device where the functions are considered non-isolated and are therefore exposed within the same IOMMU group. We would not allow a kernel driver for one of those functions and a userspace driver for the other. In this case those drivers are strongly related, but we still need to consider to what extent a malicious user driver can interfere with or exploit the kernel side driver.

Quoted 16 lines
Quoted 6 lines

MMIO region of the same device. This is susceptible to DMA> manipulation by the user device. Whether that's a security issue or> not depends on how the user can break the device. If the scope is
limited to breaking their own device, they can do that any number of ways and it's not very interesting. If the user can manipulate device state in order to trigger an exploit of the host-side kernel driver, that's obviously more of a problem.

Quoted 9 lines

The other side of this is that if migration support can be implemented entirely within the VF using this portion of the device MMIO space, why do we need the host kernel to support this rather than implementing it in userspace? For example, QEMU could know about this device, manipulate the BAR size to expose only the operational portion of MMIO to the VM and use the remainder to support migration itself. I'm afraid that just like mdev, the vfio migration uAPI is going to be used as an excuse to create kernel drivers simply to be able to make use of that uAPI. I haven't looked at this driver to know if it has some

Quoted 5 lines

When the accelerator device is designed to support the live migration function, it is based on the uAPI of the migration region to realize the live migration function, so the live migration function requires a driver that connects to this uAPI.
Is this set of interfaces not open to us now?

In your model, if both BARs are exposed to userspace and a device specific extension in QEMU claims the migration BAR rather than exposing it to the VM, could that driver mimic the migration region uAPI from userspace? For example, you don't need page pinning to interact with the IOMMU, you don't need resources beyond the scope of the endpoint device itself, and the migration info BAR is safe for userspace to manage? If so, then a kernel-based driver to expose a migration uAPI seems like it's only a risk for the kernel, ie. moving what could be a userspace driver into the kernel for the convenience of re-using a kernel uAPI. Thanks,

Quoted 4 lines

Alex

.

Thanks for your review and suggestions.

We discussed the security issues you mentioned and put forward some new solutions. These new solutions are now being implemented. After they are tested successfully, they will be issued in the second version of the RFC.

Thanks,
Longfang.

author maintainer
#16 lore raw

Re: [RFC PATCH 2/3] vfio/hisilicon: register the driver to vfio

In reply to Alex Williamson

On 2021/4/22 2:12, Alex Williamson Wrote:

Quoted 64 lines
Quoted 55 lines · On Wed, 21 Apr 2021 17:59:02 +0800 liulongfang <[email protected]> wrote:
Quoted 46 lines · Alex Williamson
Quoted 43 lines · On Tue, 20 Apr 2021 09:59:57 -0300 Jason Gunthorpe <[email protected]> wrote:
Quoted 16 lines · liulongfang
Quoted 7 lines · Jason Gunthorpe
Quoted 5 lines · liulongfang
Quoted 4 lines

I'm also confused how this works securely at all, as a general rule a VFIO PCI driver cannot access the MMIO memory of the function it is planning to assign to the guest. There is a lot of danger that the guest could access that MMIO space one way or another.

Quoted 8 lines
Quoted 3 lines

VF's MMIO memory is divided into two parts, one is the guest part, and the other is the live migration part. They do not affect each other, so there is no security problem.

AFAIK there are several scenarios where a guest can access this MMIO memory using DMA even if it is not mapped into the guest for CPU access.

Quoted 8 lines

The hardware divides VF's MMIO memory into two parts. The live migration driver in the host uses the live migration part, and the device driver in the guest uses the guest part. They obtain the address of VF's MMIO memory in their respective drivers, although these two parts The memory is continuous on the hardware device, but due to the needs of the drive function, they will not perform operations on another part of the memory, and the device hardware also independently responds to the operation commands of the two parts.

It doesn't matter, the memory is still under the same PCI BDF and VFIO supports scenarios where devices in the same IOMMU group are not isolated from each other.

This is why the granual of isolation is a PCI BDF - VFIO directly blocks kernel drivers from attaching to PCI BDFs that are not completely isolated from VFIO BDF.

Bypassing this prevention and attaching a kernel driver directly to the same BDF being exposed to the guest breaks that isolation model.

Quoted 3 lines

So, I still don't understand what the security risk you are talking about is, and what do you think the security design should look like?
Can you elaborate on it?

Each security domain must have its own PCI BDF.

Quoted 7 lines
Quoted 3 lines

The migration control registers must be on a different VF from the VF being plugged into a guest and the two VFs have to be in different IOMMU groups to ensure they are isolated from each other.

I think that's a solution, I don't know if it's the only solution. AIUI, the issue here is that we have a device specific kernel driver extending vfio-pci with migration support for this device by using an

Quoted 5 lines

If the two parts of the MMIO region are split into different BAR spaces on the device, the MMIO region of ​​the business function is still placed in BAR2, and the MMIO region of ​​the live migration function is moved to BAR4. Only BAR2 is mapped in the guest. only BAR4 is mapped in the host. This can solve this security issue.

Quoted 15 lines

The concern is really the "on the device" part rather than whether the resources are within the same BAR or not. We need to assume that a user driver can generate a DMA targeting any address in the system, including in this case the user driver could generate a DMA targeting this migration BAR. Ideally this would be routed upstream to the IOMMU where it would be blocked for lack of a translation entry. However, because this range resides on the same PCIe requester ID, it's logically more similar to a two-function device where the functions are considered non-isolated and are therefore exposed within the same IOMMU group. We would not allow a kernel driver for one of those functions and a userspace driver for the other. In this case those drivers are strongly related, but we still need to consider to what extent a malicious user driver can interfere with or exploit the kernel side driver.

Hello Alex! When updating the drive scheme, we tested the method of dividing the BAR configuration space in VFIO, the length of the BAR configuration space is limited to 0x2000, so that the guest cannot obtain the configuration of the migration region (it is after 0x2000), and then in the guest when trying to access the configuration space beyond 0x2000 in the driver, the test found that the OS in the Guest will directly report a memory access error.

Therefore, this method of limiting the length of the BAR configuration space can prevent unsafe operations of the memory.

Quoted 34 lines
Quoted 17 lines
Quoted 7 lines

MMIO region of the same device. This is susceptible to DMA> manipulation by the user device. Whether that's a security issue or> not depends on how the user can break the device. If the scope is
limited to breaking their own device, they can do that any number of ways and it's not very interesting. If the user can manipulate device state in order to trigger an exploit of the host-side kernel driver, that's obviously more of a problem.

Quoted 9 lines

The other side of this is that if migration support can be implemented entirely within the VF using this portion of the device MMIO space, why do we need the host kernel to support this rather than implementing it in userspace? For example, QEMU could know about this device, manipulate the BAR size to expose only the operational portion of MMIO to the VM and use the remainder to support migration itself. I'm afraid that just like mdev, the vfio migration uAPI is going to be used as an excuse to create kernel drivers simply to be able to make use of that uAPI. I haven't looked at this driver to know if it has some

Quoted 5 lines

When the accelerator device is designed to support the live migration function, it is based on the uAPI of the migration region to realize the live migration function, so the live migration function requires a driver that connects to this uAPI.
Is this set of interfaces not open to us now?

In your model, if both BARs are exposed to userspace and a device specific extension in QEMU claims the migration BAR rather than exposing it to the VM, could that driver mimic the migration region uAPI from userspace? For example, you don't need page pinning to interact with the IOMMU, you don't need resources beyond the scope of the endpoint device itself, and the migration info BAR is safe for userspace to manage? If so, then a kernel-based driver to expose a migration uAPI seems like it's only a risk for the kernel, ie. moving what could be a userspace driver into the kernel for the convenience of re-using a kernel uAPI. Thanks,

Quoted 4 lines

Alex

.

Thanks.
Longfang

#17 lore raw

Re: [RFC PATCH 2/3] vfio/hisilicon: register the driver to vfio

Quoted 2 lines · liulongfang

Therefore, this method of limiting the length of the BAR configuration space can prevent unsafe operations of the memory.

The issue is DMA controlled by the guest accessing the secure BAR area, not the guest CPU.

Jason

author maintainer
#18 lore raw

Re: [RFC PATCH 2/3] vfio/hisilicon: register the driver to vfio

Quoted 11 lines · Jason Gunthorpe
Quoted 2 lines · liulongfang

Therefore, this method of limiting the length of the BAR configuration space can prevent unsafe operations of the memory.

The issue is DMA controlled by the guest accessing the secure BAR area, not the guest CPU.

Jason
.

This secure BAR area is not presented to the Guest,
which makes it impossible for the Guest to obtain the secure BAR area when establishing the DMA mapping of the configuration space. If the DMA controller accesses the secure BAR area, the access will be blocked by the SMMU.

Thanks
Longfang.

#19 lore raw

Re: [RFC PATCH 2/3] vfio/hisilicon: register the driver to vfio

Quoted 9 lines · liulongfang
Quoted 8 lines · Jason Gunthorpe
Quoted 2 lines · liulongfang

Therefore, this method of limiting the length of the BAR configuration space can prevent unsafe operations of the memory.

The issue is DMA controlled by the guest accessing the secure BAR area, not the guest CPU.

Quoted 8 lines
Quoted 3 lines

Jason
.

This secure BAR area is not presented to the Guest,
which makes it impossible for the Guest to obtain the secure BAR area when establishing the DMA mapping of the configuration space. If the DMA controller accesses the secure BAR area, the access will be blocked by the SMMU.

There are scenarios where this is not true.

At a minimum the mdev driver should refuse to work in those cases.

Jason

#20 lore raw

RE: [Linuxarm] Re: [RFC PATCH 2/3] vfio/hisilicon: register the driver to vfio

Quoted 30 lines

-----Original Message-----
From: Jason Gunthorpe [mailto:[email protected]]
Sent: 13 May 2021 14:44
To: liulongfang <[email protected]>
Cc: Alex Williamson <[email protected]>; [email protected]; [email protected]; [email protected]
Subject: [Linuxarm] Re: [RFC PATCH 2/3] vfio/hisilicon: register the driver to vfio

Quoted 9 lines · liulongfang
Quoted 8 lines · Jason Gunthorpe
Quoted 2 lines · liulongfang

Therefore, this method of limiting the length of the BAR configuration space can prevent unsafe operations of the memory.

The issue is DMA controlled by the guest accessing the secure BAR area, not the guest CPU.

Quoted 8 lines
Quoted 3 lines

Jason
.

This secure BAR area is not presented to the Guest,
which makes it impossible for the Guest to obtain the secure BAR area when establishing the DMA mapping of the configuration space. If the DMA controller accesses the secure BAR area, the access will be blocked by the SMMU.

Quoted 4 lines

There are scenarios where this is not true.

At a minimum the mdev driver should refuse to work in those cases.

Hi,

I think the idea here is not a generic solution, but a quirk for this specific dev.

Something like,

drivers/vfio/pci/vfio_pci.c +6 -1
@@ -866,7 +866,12 @@ static long vfio_pci_ioctl(struct vfio_device *core_vdev,
                        break;
                case VFIO_PCI_BAR0_REGION_INDEX ... VFIO_PCI_BAR5_REGION_INDEX:
                        info.offset = VFIO_PCI_INDEX_TO_OFFSET(info.index);
-                       info.size = pci_resource_len(pdev, info.index);
+
+                       if (check_hisi_acc_quirk(pdev, info))
+                               info.size = new_size;// BAR is limited without migration region.
+                       else
+                               info.size = pci_resource_len(pdev, info.index);
+
                        if (!info.size) {
                                info.flags = 0;
                                break;

Is this an acceptable/workable solution here?

Thanks,
Shameer

#21 lore raw

Re: [Linuxarm] Re: [RFC PATCH 2/3] vfio/hisilicon: register the driver to vfio

Quoted 56 lines · On Thu, 13 May 2021 15:49:25 +0000 Shameerali Kolothum Thodi…
Quoted 28 lines

-----Original Message-----
From: Jason Gunthorpe [mailto:[email protected]]
Sent: 13 May 2021 14:44
To: liulongfang <[email protected]>
Cc: Alex Williamson <[email protected]>; [email protected]; [email protected]; [email protected]
Subject: [Linuxarm] Re: [RFC PATCH 2/3] vfio/hisilicon: register the driver to vfio

Quoted 9 lines · liulongfang
Quoted 8 lines · Jason Gunthorpe
Quoted 2 lines · liulongfang

Therefore, this method of limiting the length of the BAR configuration space can prevent unsafe operations of the memory.

The issue is DMA controlled by the guest accessing the secure BAR area, not the guest CPU.

Quoted 8 lines
Quoted 3 lines

Jason
.

This secure BAR area is not presented to the Guest,
which makes it impossible for the Guest to obtain the secure BAR area when establishing the DMA mapping of the configuration space. If the DMA controller accesses the secure BAR area, the access will be blocked by the SMMU.

Quoted 4 lines

There are scenarios where this is not true.

At a minimum the mdev driver should refuse to work in those cases.

Hi,

I think the idea here is not a generic solution, but a quirk for this specific dev.

Something like,

drivers/vfio/pci/vfio_pci.c +6 -1
@@ -866,7 +866,12 @@ static long vfio_pci_ioctl(struct vfio_device *core_vdev,
                        break;
                case VFIO_PCI_BAR0_REGION_INDEX ... VFIO_PCI_BAR5_REGION_INDEX:
                        info.offset = VFIO_PCI_INDEX_TO_OFFSET(info.index);
-                       info.size = pci_resource_len(pdev, info.index);
+
+                       if (check_hisi_acc_quirk(pdev, info))
+                               info.size = new_size;// BAR is limited without migration region.
+                       else
+                               info.size = pci_resource_len(pdev, info.index);
+
                        if (!info.size) {
                                info.flags = 0;
                                break;
Quoted 1 line

Is this an acceptable/workable solution here?

As Jason says, this only restricts CPU access to the BAR, the issue is DMA access. As the hardware vendor you may be able to guarantee that a DMA transaction generated by the device targeting the remainder of the BAR will always go upstream, but can you guarantee the routing between the device and the SMMU? For instance if this device can be implemented as a plugin card, then it can be installed into a downstream port that may not support ACS. That downstream port may implement request redirection allowing the transaction to reflect back to the device without IOMMU translation. At that point the userspace driver can target the kernel driver half of the BAR and potentially expose a security risk. Thanks,

Alex

#22 lore raw

RE: [Linuxarm] Re: [RFC PATCH 2/3] vfio/hisilicon: register the driver to vfio

Hi Alex,

Quoted 77 lines

-----Original Message-----
From: Alex Williamson [mailto:[email protected]]
Sent: 13 May 2021 18:04
To: Shameerali Kolothum Thodi <[email protected]>
Cc: Jason Gunthorpe <[email protected]>; liulongfang <[email protected]>; [email protected]; [email protected]; [email protected]
Subject: [Linuxarm] Re: [RFC PATCH 2/3] vfio/hisilicon: register the driver to vfio

drivers/vfio/pci/vfio_pci.c static long vfio_pci_ioctl(struct vfio_device

Quoted 42 lines · On Thu, 13 May 2021 15:49:25 +0000 Shameerali Kolothum Thodi…
Quoted 28 lines

-----Original Message-----
From: Jason Gunthorpe [mailto:[email protected]]
Sent: 13 May 2021 14:44
To: liulongfang <[email protected]>
Cc: Alex Williamson <[email protected]>; [email protected]; [email protected]; [email protected]
Subject: [Linuxarm] Re: [RFC PATCH 2/3] vfio/hisilicon: register the driver to vfio

Quoted 9 lines · liulongfang
Quoted 8 lines · Jason Gunthorpe
Quoted 2 lines · liulongfang

Therefore, this method of limiting the length of the BAR configuration space can prevent unsafe operations of the memory.

The issue is DMA controlled by the guest accessing the secure BAR area, not the guest CPU.

Quoted 8 lines
Quoted 3 lines

Jason
.

This secure BAR area is not presented to the Guest,
which makes it impossible for the Guest to obtain the secure BAR area when establishing the DMA mapping of the configuration space. If the DMA controller accesses the secure BAR area, the access will be blocked by the SMMU.

Quoted 4 lines

There are scenarios where this is not true.

At a minimum the mdev driver should refuse to work in those cases.

Hi,

I think the idea here is not a generic solution, but a quirk for this specific dev.

Something like,

drivers/vfio/pci/vfio_pci.c +0 -0
@@ -866,7 +866,12 @@ static long vfio_pci_ioctl(struct vfio_device

*core_vdev,

drivers/vfio/pci/vfio_pci.c static long vfio_pci_ioctl(struct vfio_device · line 867

Quoted 2 lines
drivers/vfio/pci/vfio_pci.c +0 -0
                        break;
                case VFIO_PCI_BAR0_REGION_INDEX ...

VFIO_PCI_BAR5_REGION_INDEX:

drivers/vfio/pci/vfio_pci.c static long vfio_pci_ioctl(struct vfio_device · line 868

Quoted 1 line
drivers/vfio/pci/vfio_pci.c +0 -0
                        info.offset =

VFIO_PCI_INDEX_TO_OFFSET(info.index);

drivers/vfio/pci/vfio_pci.c static long vfio_pci_ioctl(struct vfio_device *core_vdev,

Quoted 4 lines
drivers/vfio/pci/vfio_pci.c +3 -1
-                       info.size = pci_resource_len(pdev, info.index);
+
+                       if (check_hisi_acc_quirk(pdev, info))
+                               info.size = new_size;// BAR is limited

without migration region.

drivers/vfio/pci/vfio_pci.c static long vfio_pci_ioctl(struct vfio_device · line 873

Quoted 2 lines
drivers/vfio/pci/vfio_pci.c +2 -0
+                       else
+                               info.size = pci_resource_len(pdev,

info.index);

Quoted 6 lines
drivers/vfio/pci/vfio_pci.c +1 -0
+
                        if (!info.size) {
                                info.flags = 0;
                                break;

Is this an acceptable/workable solution here?

Quoted 11 lines

As Jason says, this only restricts CPU access to the BAR, the issue is DMA access. As the hardware vendor you may be able to guarantee that a DMA transaction generated by the device targeting the remainder of the BAR will always go upstream, but can you guarantee the routing between the device and the SMMU? For instance if this device can be implemented as a plugin card, then it can be installed into a downstream port that may not support ACS. That downstream port may implement request redirection allowing the transaction to reflect back to the device without IOMMU translation. At that point the userspace driver can target the kernel driver half of the BAR and potentially expose a security risk. Thanks,

The ACC devices on this platform are not pluggable devices. They are exposed as integrated endpoint devices. So I am not sure the above concern is valid in this case.

I had a look at the userspace driver approach you suggested. But unfortunately the migration state change for the vf has to check some of the pf registers for confirming the state. So even if we move the implementation to Qemu, we still may have to use the migration uAPI to access the pf device registers.

Since the devices we are concerned here are all integrated endpoints and if the above quirk is an acceptable one, then we can use the uAPI as done in this series without overly complicating things here.

Please let me know your thoughts,

Thanks,
Shameer

Quoted 3 lines

_______________________________________________
Linuxarm mailing list -- [email protected]
To unsubscribe send an email to [email protected]

#23 lore raw

Re: [Linuxarm] Re: [RFC PATCH 2/3] vfio/hisilicon: register the driver to vfio

Quoted 95 lines · On Thu, 13 May 2021 17:52:56 +0000 Shameerali Kolothum Thodi…

Hi Alex,

Quoted 77 lines

-----Original Message-----
From: Alex Williamson [mailto:[email protected]]
Sent: 13 May 2021 18:04
To: Shameerali Kolothum Thodi <[email protected]>
Cc: Jason Gunthorpe <[email protected]>; liulongfang <[email protected]>; [email protected]; [email protected]; [email protected]
Subject: [Linuxarm] Re: [RFC PATCH 2/3] vfio/hisilicon: register the driver to vfio

drivers/vfio/pci/vfio_pci.c static long vfio_pci_ioctl(struct vfio_device

Quoted 42 lines · On Thu, 13 May 2021 15:49:25 +0000 Shameerali Kolothum Thodi…
Quoted 28 lines

-----Original Message-----
From: Jason Gunthorpe [mailto:[email protected]]
Sent: 13 May 2021 14:44
To: liulongfang <[email protected]>
Cc: Alex Williamson <[email protected]>; [email protected]; [email protected]; [email protected]
Subject: [Linuxarm] Re: [RFC PATCH 2/3] vfio/hisilicon: register the driver to vfio

Quoted 9 lines · liulongfang
Quoted 8 lines · Jason Gunthorpe
Quoted 2 lines · liulongfang

Therefore, this method of limiting the length of the BAR configuration space can prevent unsafe operations of the memory.

The issue is DMA controlled by the guest accessing the secure BAR area, not the guest CPU.

Quoted 8 lines
Quoted 3 lines

Jason
.

This secure BAR area is not presented to the Guest,
which makes it impossible for the Guest to obtain the secure BAR area when establishing the DMA mapping of the configuration space. If the DMA controller accesses the secure BAR area, the access will be blocked by the SMMU.

Quoted 4 lines

There are scenarios where this is not true.

At a minimum the mdev driver should refuse to work in those cases.

Hi,

I think the idea here is not a generic solution, but a quirk for this specific dev.

Something like,

drivers/vfio/pci/vfio_pci.c +0 -0
@@ -866,7 +866,12 @@ static long vfio_pci_ioctl(struct vfio_device  

*core_vdev,

drivers/vfio/pci/vfio_pci.c static long vfio_pci_ioctl(struct vfio_device · line 867

Quoted 2 lines
drivers/vfio/pci/vfio_pci.c +0 -0
                        break;
                case VFIO_PCI_BAR0_REGION_INDEX ...  

VFIO_PCI_BAR5_REGION_INDEX:

drivers/vfio/pci/vfio_pci.c static long vfio_pci_ioctl(struct vfio_device · line 868

Quoted 1 line
drivers/vfio/pci/vfio_pci.c +0 -0
                        info.offset =  

VFIO_PCI_INDEX_TO_OFFSET(info.index);

drivers/vfio/pci/vfio_pci.c static long vfio_pci_ioctl(struct vfio_device *core_vdev,

Quoted 4 lines
drivers/vfio/pci/vfio_pci.c +3 -1
-                       info.size = pci_resource_len(pdev, info.index);
+
+                       if (check_hisi_acc_quirk(pdev, info))
+                               info.size = new_size;// BAR is limited  

without migration region.

drivers/vfio/pci/vfio_pci.c static long vfio_pci_ioctl(struct vfio_device · line 873

Quoted 2 lines
drivers/vfio/pci/vfio_pci.c +2 -0
+                       else
+                               info.size = pci_resource_len(pdev,  

info.index);

Quoted 6 lines
drivers/vfio/pci/vfio_pci.c +1 -0
+
                        if (!info.size) {
                                info.flags = 0;
                                break;

Is this an acceptable/workable solution here?

Quoted 11 lines

As Jason says, this only restricts CPU access to the BAR, the issue is DMA access. As the hardware vendor you may be able to guarantee that a DMA transaction generated by the device targeting the remainder of the BAR will always go upstream, but can you guarantee the routing between the device and the SMMU? For instance if this device can be implemented as a plugin card, then it can be installed into a downstream port that may not support ACS. That downstream port may implement request redirection allowing the transaction to reflect back to the device without IOMMU translation. At that point the userspace driver can target the kernel driver half of the BAR and potentially expose a security risk. Thanks,

The ACC devices on this platform are not pluggable devices. They are exposed as integrated endpoint devices. So I am not sure the above concern is valid in this case.

Quoted 8 lines

I had a look at the userspace driver approach you suggested. But unfortunately the migration state change for the vf has to check some of the pf registers for confirming the state. So even if we move the implementation to Qemu, we still may have to use the migration uAPI to access the pf device registers.

Since the devices we are concerned here are all integrated endpoints and if the above quirk is an acceptable one, then we can use the uAPI as done in this series without overly complicating things here.

If you expect this device to appear only as an integrated endpoint, then I think Jason's suggestion above is correct. Your driver that supports migration can refuse to load for devices there the topology is other than expected and you're effectively guaranteeing DMA isolation of the user and in-kernel drivers by hardware DMA semantics and topology.

Requiring access to the PF to support the migration protocol also suggests that an in-kernel driver to support migration is our best option. Thanks,

Alex

#24 lore raw

RE: [Linuxarm] Re: [RFC PATCH 2/3] vfio/hisilicon: register the driver to vfio

Quoted 126 lines

-----Original Message-----
From: Alex Williamson [mailto:[email protected]]
Sent: 13 May 2021 19:23
To: Shameerali Kolothum Thodi <[email protected]>
Cc: Jason Gunthorpe <[email protected]>; liulongfang <[email protected]>; [email protected]; [email protected]; [email protected]
Subject: Re: [Linuxarm] Re: [RFC PATCH 2/3] vfio/hisilicon: register the driver to vfio

Quoted 6 lines · On Thu, 13 May 2021 17:52:56 +0000 Shameerali Kolothum Thodi…

Hi Alex,

Quoted 4 lines

-----Original Message-----
From: Alex Williamson [mailto:[email protected]]
Sent: 13 May 2021 18:04
To: Shameerali Kolothum Thodi

<[email protected]>

Quoted 10 lines
Quoted 10 lines

Cc: Jason Gunthorpe <[email protected]>; liulongfang <[email protected]>; [email protected]; [email protected]; [email protected]
Subject: [Linuxarm] Re: [RFC PATCH 2/3] vfio/hisilicon: register the driver to vfio

On Thu, 13 May 2021 15:49:25 +0000
Shameerali Kolothum Thodi <[email protected]> wrote:

Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines

-----Original Message-----
From: Jason Gunthorpe [mailto:[email protected]]
Sent: 13 May 2021 14:44
To: liulongfang <[email protected]>
Cc: Alex Williamson <[email protected]>;

[email protected];

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

[email protected]; [email protected]
Subject: [Linuxarm] Re: [RFC PATCH 2/3] vfio/hisilicon: register the

driver to

Quoted 21 lines
Quoted 21 lines
Quoted 21 lines
Quoted 21 lines

vfio

Quoted 17 lines · liulongfang
Quoted 11 lines · Jason Gunthorpe
Quoted 2 lines · liulongfang

Therefore, this method of limiting the length of the BAR configuration space can prevent unsafe operations of the memory.

The issue is DMA controlled by the guest accessing the secure BAR area, not the guest CPU.

Jason
.

This secure BAR area is not presented to the Guest,
which makes it impossible for the Guest to obtain the secure BAR area when establishing the DMA mapping of the configuration space. If the DMA controller accesses the secure BAR area, the access will be blocked by the SMMU.

Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 4 lines

There are scenarios where this is not true.

At a minimum the mdev driver should refuse to work in those cases.

Hi,

I think the idea here is not a generic solution, but a quirk for this specific

dev.

Quoted 9 lines
Quoted 9 lines

drivers/vfio/pci/vfio_pci.c static long vfio_pci_ioctl(struct vfio_device

Quoted 6 lines

Something like,

drivers/vfio/pci/vfio_pci.c +0 -0
@@ -866,7 +866,12 @@ static long vfio_pci_ioctl(struct vfio_device

*core_vdev,

Quoted 2 lines
drivers/vfio/pci/vfio_pci.c +0 -0
                        break;
                case VFIO_PCI_BAR0_REGION_INDEX ...
Quoted 7 lines
Quoted 7 lines

VFIO_PCI_BAR5_REGION_INDEX:

drivers/vfio/pci/vfio_pci.c static long vfio_pci_ioctl(struct vfio_device · line 868

Quoted 1 line
drivers/vfio/pci/vfio_pci.c +0 -0
                        info.offset =

VFIO_PCI_INDEX_TO_OFFSET(info.index);

Quoted 4 lines
drivers/vfio/pci/vfio_pci.c +3 -1
-                       info.size = pci_resource_len(pdev, info.index);
+
+                       if (check_hisi_acc_quirk(pdev, info))
+                               info.size = new_size;// BAR is

limited

Quoted 23 lines
Quoted 11 lines

without migration region.

drivers/vfio/pci/vfio_pci.c static long vfio_pci_ioctl(struct vfio_device · line 873

Quoted 2 lines
drivers/vfio/pci/vfio_pci.c +2 -0
+                       else
+                               info.size = pci_resource_len(pdev,

info.index);

Quoted 6 lines
drivers/vfio/pci/vfio_pci.c +1 -0
+
                        if (!info.size) {
                                info.flags = 0;
                                break;

Is this an acceptable/workable solution here?

Quoted 11 lines

As Jason says, this only restricts CPU access to the BAR, the issue is DMA access. As the hardware vendor you may be able to guarantee that a DMA transaction generated by the device targeting the remainder of the BAR will always go upstream, but can you guarantee the routing between the device and the SMMU? For instance if this device can be implemented as a plugin card, then it can be installed into a downstream port that may not support ACS. That downstream port may implement request redirection allowing the transaction to reflect back to the device without IOMMU translation. At that point the userspace driver can target the kernel driver half of the BAR and potentially expose a security risk. Thanks,

Quoted 1 line

The ACC devices on this platform are not pluggable devices. They are

exposed

Quoted 1 line

as integrated endpoint devices. So I am not sure the above concern is valid in

this

Quoted 3 lines

case.

I had a look at the userspace driver approach you suggested. But

unfortunately

Quoted 5 lines

the migration state change for the vf has to check some of the pf registers for confirming the state. So even if we move the implementation to Qemu, we still may have to use the migration uAPI to access the pf device registers.

Since the devices we are concerned here are all integrated endpoints and if

the

Quoted 8 lines
Quoted 2 lines

above quirk is an acceptable one, then we can use the uAPI as done in this series without overly complicating things here.

If you expect this device to appear only as an integrated endpoint, then I think Jason's suggestion above is correct. Your driver that supports migration can refuse to load for devices there the topology is other than expected and you're effectively guaranteeing DMA isolation of the user and in-kernel drivers by hardware DMA semantics and topology.

Ok. Will take a look at the recommendation.

Quoted 3 lines

Requiring access to the PF to support the migration protocol also suggests that an in-kernel driver to support migration is our best option.

Thanks,
Shameer

#25 lore raw

Re: [Linuxarm] Re: [RFC PATCH 2/3] vfio/hisilicon: register the driver to vfio

In reply to Alex Williamson
Quoted 5 lines · Alex Williamson

If you expect this device to appear only as an integrated endpoint, then I think Jason's suggestion above is correct. Your driver that supports migration can refuse to load for devices there the topology is other than expected and you're effectively guaranteeing DMA isolation of the user and in-kernel drivers by hardware DMA semantics and topology.

Some kind of VFIO api 'vfio_is_this_pci_dev_is_safe_to_share()' seems appropriate here.

I saw Intel doing the same thing in one of their VDPA driver proposals.

Jason

#26 lore raw

Re: [Linuxarm] Re: [RFC PATCH 2/3] vfio/hisilicon: register the driver to vfio

In reply to Shameerali Kolothum Thodi
Quoted 3 lines · Shameerali Kolothum Thodi

Since the devices we are concerned here are all integrated endpoints and if the above quirk is an acceptable one, then we can use the uAPI as done in this series without overly complicating things here.

IMHO such a quirk in the core code should not be done. You need to override this in your own driver like Max was showing.

I think we are very close to having worked out a great solution to the lingering questions on Max's last RFC, hopefully we can post an updated version soon

Jason

#27 lore raw

RE: [Linuxarm] Re: [RFC PATCH 2/3] vfio/hisilicon: register the driver to vfio

Quoted 15 lines

-----Original Message-----
From: Jason Gunthorpe [mailto:[email protected]]
Sent: 13 May 2021 19:24
To: Shameerali Kolothum Thodi <[email protected]>
Cc: Alex Williamson <[email protected]>; liulongfang <[email protected]>; [email protected]; [email protected]; [email protected]
Subject: Re: [Linuxarm] Re: [RFC PATCH 2/3] vfio/hisilicon: register the driver to vfio

On Thu, May 13, 2021 at 05:52:56PM +0000, Shameerali Kolothum Thodi wrote:

Quoted 7 lines
Quoted 1 line

Since the devices we are concerned here are all integrated endpoints and if

the

Quoted 2 lines

above quirk is an acceptable one, then we can use the uAPI as done in this series without overly complicating things here.

IMHO such a quirk in the core code should not be done. You need to override this in your own driver like Max was showing.

Ok. Sure, will take a look at that.

Quoted 3 lines

I think we are very close to having worked out a great solution to the lingering questions on Max's last RFC, hopefully we can post an updated version soon

Cool. If possible kindly CC us when it happens.

Thanks,
Shameer

#28 lore raw

RE: [Linuxarm] Re: [RFC PATCH 2/3] vfio/hisilicon: register the driver to vfio

In reply to Jason Gunthorpe

[+]

Hi,

Quoted 18 lines

-----Original Message-----
From: Jason Gunthorpe [mailto:[email protected]]
Sent: 13 May 2021 19:24
To: Shameerali Kolothum Thodi <[email protected]>
Cc: Alex Williamson <[email protected]>; liulongfang <[email protected]>; [email protected]; [email protected]; [email protected]
Subject: Re: [Linuxarm] Re: [RFC PATCH 2/3] vfio/hisilicon: register the driver to vfio

Quoted 1 line · Shameerali Kolothum Thodi

Since the devices we are concerned here are all integrated endpoints and if

the

Quoted 2 lines

above quirk is an acceptable one, then we can use the uAPI as done in this series without overly complicating things here.

Quoted 6 lines

IMHO such a quirk in the core code should not be done. You need to override this in your own driver like Max was showing.

I think we are very close to having worked out a great solution to the lingering questions on Max's last RFC, hopefully we can post an updated version soon

We have now integrated this HiSilicon ACC live migration driver into the proposed[0] vfio-pci-core subsystem framework. Basically now have
a new vendor extension driver based on this framework. It indeed makes it easy to do the registration to vfio core and overriding of functions if required. Performed some basic sanity tests with the prototype and it seems to be working.

Also, we now managed to get rid of any access to Guest VF dev MMIO space during the (VFIO_DEVICE_STATE_SAVING | VFIO_DEVICE_STATE_RUNNING) state. However we still need to access the Guest VF MMIO space during few other device states eg; VFIO_DEVICE_STATE_SAVING, VFIO_DEVICE_STATE_STOP etc. These accesses are to save and restore device register states. As per our analysis the Guest vCPUs are in stopped state at these device states. So I hope we don't have any security related holes with these accesses.

Please let us know if we miss something or anything else to be taken care of with this approach.

We are planning to send out a revised RFC soon, and if there is a plan to post an updated one to Max's series as mentioned above, will base it on that one. Please let us know.

Thanks,
Shameer
[0] https://lore.kernel.org/lkml/[email protected]/T/

#29 lore raw

Re: [Linuxarm] Re: [RFC PATCH 2/3] vfio/hisilicon: register the driver to vfio

Quoted 27 lines · Shameerali Kolothum Thodi

[+]

Hi,

Quoted 15 lines

-----Original Message-----
From: Jason Gunthorpe [mailto:[email protected]]
Sent: 13 May 2021 19:24
To: Shameerali Kolothum Thodi <[email protected]>
Cc: Alex Williamson <[email protected]>; liulongfang <[email protected]>; [email protected]; [email protected]; [email protected]
Subject: Re: [Linuxarm] Re: [RFC PATCH 2/3] vfio/hisilicon: register the driver to vfio

Quoted 1 line · Shameerali Kolothum Thodi

Since the devices we are concerned here are all integrated endpoints and if

the

Quoted 8 lines
Quoted 2 lines

above quirk is an acceptable one, then we can use the uAPI as done in this series without overly complicating things here.

IMHO such a quirk in the core code should not be done. You need to override this in your own driver like Max was showing.

I think we are very close to having worked out a great solution to the lingering questions on Max's last RFC, hopefully we can post an updated version soon

Quoted 6 lines

We have now integrated this HiSilicon ACC live migration driver into the proposed[0] vfio-pci-core subsystem framework. Basically now have
a new vendor extension driver based on this framework. It indeed makes it easy to do the registration to vfio core and overriding of functions if required. Performed some basic sanity tests with the prototype and it seems to be working.

Great news.

Quoted 8 lines

Also, we now managed to get rid of any access to Guest VF dev MMIO space during the (VFIO_DEVICE_STATE_SAVING | VFIO_DEVICE_STATE_RUNNING) state. However we still need to access the Guest VF MMIO space during few other device states eg; VFIO_DEVICE_STATE_SAVING, VFIO_DEVICE_STATE_STOP etc. These accesses are to save and restore device register states. As per our analysis the Guest vCPUs are in stopped state at these device states. So I hope we don't have any security related holes with these accesses.

We can start talking about more API's that are missing.

Quoted 7 lines

Please let us know if we miss something or anything else to be taken care of with this approach.

We are planning to send out a revised RFC soon, and if there is a plan to post an updated one to Max's series as mentioned above, will base it on that one. Please let us know.

I'll send a new version of the series soon with many improvements that we'll taken from previous discussion.

I'll add you as CC.

Quoted 6 lines

Thanks,
Shameer
[0] https://lore.kernel.org/lkml/[email protected]/T/

author maintainer
#30 lore raw

RFC PATCH 3/3 vfio/hisilicom: add debugfs for driver

In reply to Longfang Liu

Add debugfs debugging interface to live migration driver

---
 drivers/vfio/pci/hisilicon/acc_vf_migration.c | 193 ++++++++++++++++++++++++++
 drivers/vfio/pci/hisilicon/acc_vf_migration.h |   2 +
 2 files changed, 195 insertions(+)
drivers/vfio/pci/hisilicon/acc_vf_migration.c +193 -0
index 5d8650d..d4eacaf 100644
@@ -16,6 +16,9 @@
 
 #define VDM_OFFSET(x) offsetof(struct vfio_device_migration_info, x)
 void vfio_pci_hisilicon_acc_uninit(struct acc_vf_migration *acc_vf_dev);
+static void vf_debugfs_exit(struct acc_vf_migration *acc_vf_dev);
+static struct dentry *mig_debugfs_root;
+static int mig_root_ref;
 
 /* return 0 mailbox ready, -ETIMEDOUT hardware timeout */
 static int qm_wait_mb_ready(struct hisi_qm *qm)
@@ -933,6 +936,193 @@ static const struct vfio_pci_regops vfio_pci_acc_regops = {
 	.add_capability = acc_vf_migration_add_capability,
 };
 
+static ssize_t acc_vf_debug_read(struct file *filp, char __user *buffer,
+			   size_t count, loff_t *pos)
+{
+	char buf[VFIO_DEV_DBG_LEN];
+	int len;
+
+	len = scnprintf(buf, VFIO_DEV_DBG_LEN, "%s\n",
+			"echo 0: test vf data store\n"
+			"echo 1: test vf data writeback\n"
+			"echo 2: test vf send mailbox\n"
+			"echo 3: dump vf dev data\n"
+			"echo 4: dump migration state\n");
+
+	return simple_read_from_buffer(buffer, count, pos, buf, len);
+}
+
+static ssize_t acc_vf_debug_write(struct file *filp, const char __user *buffer,
+			    size_t count, loff_t *pos)
+{
+	struct acc_vf_migration *acc_vf_dev = filp->private_data;
+	struct device *dev = &acc_vf_dev->vf_dev->dev;
+	struct hisi_qm *qm = acc_vf_dev->vf_qm;
+	char tbuf[VFIO_DEV_DBG_LEN];
+	unsigned long val;
+	u64 data;
+	int len, ret;
+
+	if (*pos)
+		return 0;
+
+	if (count >= VFIO_DEV_DBG_LEN)
+		return -ENOSPC;
+
+	len = simple_write_to_buffer(tbuf, VFIO_DEV_DBG_LEN - 1,
+					pos, buffer, count);
+	if (len < 0)
+		return len;
+	tbuf[len] = '\0';
+	if (kstrtoul(tbuf, 0, &val))
+		return -EFAULT;
+
+	switch (val) {
+	case STATE_SAVE:
+		ret = vf_qm_state_save(qm, acc_vf_dev);
+		if (ret)
+			return -EINVAL;
+		break;
+	case STATE_RESUME:
+		ret = vf_qm_state_resume(qm, acc_vf_dev);
+		if (ret)
+			return -EINVAL;
+		break;
+	case MB_TEST:
+		data = readl(qm->io_base + QM_MB_CMD_SEND_BASE);
+		dev_info(dev, "debug mailbox addr: 0x%lx, mailbox val: 0x%llx\n",
+			 (uintptr_t)qm->io_base, data);
+		break;
+	case MIG_DATA_DUMP:
+		dev_info(dev, "dumped vf migration data:\n");
+		print_hex_dump(KERN_INFO, "Mig Data:", DUMP_PREFIX_OFFSET,
+				VFIO_DBG_LOG_LEN, 1,
+				(unsigned char *)acc_vf_dev->vf_data,
+				sizeof(struct acc_vf_data), false);
+		break;
+	case MIG_DEV_SHOW:
+		if (!acc_vf_dev->mig_ctl)
+			dev_info(dev, "migration region have release!\n");
+		else
+			dev_info(dev,
+				 "device  state: %u\n"
+				 "pending bytes: %llu\n"
+				 "data   offset: %llu\n"
+				 "data     size: %llu\n"
+				 "data     addr: 0x%lx\n",
+				 acc_vf_dev->mig_ctl->device_state,
+				 acc_vf_dev->mig_ctl->pending_bytes,
+				 acc_vf_dev->mig_ctl->data_offset,
+				 acc_vf_dev->mig_ctl->data_size,
+				 (uintptr_t)acc_vf_dev->vf_data);
+		break;
+	default:
+		return -EINVAL;
+	}
+
+	return count;
+}
+
+static const struct file_operations acc_vf_debug_fops = {
+	.owner = THIS_MODULE,
+	.open = simple_open,
+	.read = acc_vf_debug_read,
+	.write = acc_vf_debug_write,
+};
+
+static ssize_t acc_vf_state_read(struct file *filp, char __user *buffer,
+			   size_t count, loff_t *pos)
+{
+	struct acc_vf_migration *acc_vf_dev = filp->private_data;
+	char buf[VFIO_DEV_DBG_LEN];
+	u32 state;
+	int len;
+
+	if (!acc_vf_dev->mig_ctl) {
+		len = scnprintf(buf, VFIO_DEV_DBG_LEN, "%s\n", "Invalid\n");
+	} else {
+		state = acc_vf_dev->mig_ctl->device_state;
+		switch (state) {
+		case VFIO_DEVICE_STATE_RUNNING:
+			len = scnprintf(buf, VFIO_DEV_DBG_LEN, "%s\n",
+				"RUNNING\n");
+			break;
+		case VFIO_DEVICE_STATE_SAVING | VFIO_DEVICE_STATE_RUNNING:
+			len = scnprintf(buf, VFIO_DEV_DBG_LEN, "%s\n",
+				"SAVING and RUNNING\n");
+			break;
+		case VFIO_DEVICE_STATE_SAVING:
+			len = scnprintf(buf, VFIO_DEV_DBG_LEN, "%s\n",
+				"SAVING\n");
+			break;
+		case VFIO_DEVICE_STATE_STOP:
+			len = scnprintf(buf, VFIO_DEV_DBG_LEN, "%s\n",
+				"STOP\n");
+			break;
+		case VFIO_DEVICE_STATE_RESUMING:
+			len = scnprintf(buf, VFIO_DEV_DBG_LEN, "%s\n",
+				"RESUMING\n");
+			break;
+		default:
+			len = scnprintf(buf, VFIO_DEV_DBG_LEN, "%s\n",
+				"Error\n");
+		}
+	}
+
+	return simple_read_from_buffer(buffer, count, pos, buf, len);
+}
+
+static const struct file_operations acc_vf_state_fops = {
+	.owner = THIS_MODULE,
+	.open = simple_open,
+	.read = acc_vf_state_read,
+};
+
+static void vf_debugfs_init(struct acc_vf_migration *acc_vf_dev)
+{
+	char name[VFIO_DEV_DBG_LEN];
+	int node_id;
+
+	if (!mig_root_ref)
+		mig_debugfs_root = debugfs_create_dir("vfio_acc", NULL);
+	mutex_lock(&acc_vf_dev->reflock);
+	mig_root_ref++;
+	mutex_unlock(&acc_vf_dev->reflock);
+
+	node_id = dev_to_node(&acc_vf_dev->vf_dev->dev);
+	if (node_id < 0)
+		node_id = 0;
+
+	if (acc_vf_dev->acc_type == HISI_SEC)
+		scnprintf(name, VFIO_DEV_DBG_LEN, "sec_vf%d-%d",
+			  node_id, acc_vf_dev->vf_id);
+	else if (acc_vf_dev->acc_type == HISI_HPRE)
+		scnprintf(name, VFIO_DEV_DBG_LEN, "hpre_vf%d-%d",
+			  node_id, acc_vf_dev->vf_id);
+	else
+		scnprintf(name, VFIO_DEV_DBG_LEN, "zip_vf%d-%d",
+			  node_id, acc_vf_dev->vf_id);
+
+	acc_vf_dev->debug_root = debugfs_create_dir(name, mig_debugfs_root);
+
+	debugfs_create_file("debug", 0644, acc_vf_dev->debug_root,
+			      acc_vf_dev, &acc_vf_debug_fops);
+	debugfs_create_file("state", 0444, acc_vf_dev->debug_root,
+			      acc_vf_dev, &acc_vf_state_fops);
+}
+
+static void vf_debugfs_exit(struct acc_vf_migration *acc_vf_dev)
+{
+	debugfs_remove_recursive(acc_vf_dev->debug_root);
+
+	mutex_lock(&acc_vf_dev->reflock);
+	mig_root_ref--;
+	mutex_unlock(&acc_vf_dev->reflock);
+
+	if (!mig_root_ref)
+		debugfs_remove_recursive(mig_debugfs_root);
+}
+
 static int qm_acc_type_init(struct acc_vf_migration *acc_vf_dev)
 {
 	struct pci_dev *pdev = acc_vf_dev->vf_dev;
@@ -1117,6 +1307,8 @@ int vfio_pci_hisilicon_acc_init(struct vfio_pci_device *vdev)
 		goto register_error;
 	}
 
+	vf_debugfs_init(acc_vf_dev);
+
 	return 0;
 
 register_error:
@@ -1139,6 +1331,7 @@ void vfio_pci_hisilicon_acc_uninit(struct acc_vf_migration *acc_vf_dev)
 	acc_vf_dev->regions = NULL;
 	acc_vf_dev->num_regions = 0;
 
+	vf_debugfs_exit(acc_vf_dev);
 	kfree(acc_vf_dev);
 }
 
drivers/vfio/pci/hisilicon/acc_vf_migration.h +2 -0
index e8b116be..0a5438f 100644
@@ -157,11 +157,13 @@ struct acc_vf_migration {
 	struct hisi_qm			*vf_qm;
 	int				vf_id;
 	u8				acc_type;
+	struct mutex			reflock;
 
 	struct vfio_device_migration_info *mig_ctl;
 	struct acc_vf_data		*vf_data;
 	struct vfio_pci_region		*regions;
 	int				num_regions;
+	struct dentry			*debug_root;
 };
 
 #endif /* ACC_MIG_H */
Signature · 1 line
2.8.1
#31 lore raw
In reply to Longfang Liu

[Cc+ NVIDIA folks both from migration and vfio-pci-core discussion]

Quoted 36 lines · On Tue, 13 Apr 2021 11:36:20 +0800 Longfang Liu <[email protected]> wrote:

The live migration solution relies on the vfio_device_migration_info protocol. The structure vfio_device_migration_info is placed at the 0th offset of the VFIO_REGION_SUBTYPE_MIGRATION region to get and set VFIO device related migration information. Field accesses from this structure are only supported at their native width and alignment. Otherwise, the result is undefined and vendor drivers should return an error.

(1).The driver framework is based on vfio_pci_register_dev_region() of vfio-pci, and then a new live migration region is added, and the live migration is realized through the ops of this region.

(2).In order to ensure the compatibility of the devices before and after the migration, the device compatibility information check will be performed in the Pre-copy stage. If the check fails, an error will be returned and the source VM will exit the migration function.

(3).After the compatibility check is passed, it will enter the Stop-and-copy stage. At this time, all the live migration data will be copied, and then saved to the VF device of the destination, and then the VF device of the destination will be started and the VM of the source will be exited.

Longfang Liu (3):
vfio/hisilicon: add acc live migration driver
vfio/hisilicon: register the driver to vfio
vfio/hisilicom: add debugfs for driver

 drivers/vfio/pci/Kconfig                      |    8 +
 drivers/vfio/pci/Makefile                     |    1 +
 drivers/vfio/pci/hisilicon/acc_vf_migration.c | 1337 +++++++++++++++++++++++++
 drivers/vfio/pci/hisilicon/acc_vf_migration.h |  170 ++++
 drivers/vfio/pci/vfio_pci.c                   |   11 +
 drivers/vfio/pci/vfio_pci_private.h           |    9 +
 6 files changed, 1536 insertions(+)
 create mode 100644 drivers/vfio/pci/hisilicon/acc_vf_migration.c
 create mode 100644 drivers/vfio/pci/hisilicon/acc_vf_migration.h