Skip to contentSkip to conversation

Introduce a vfio driver over virtio devices

PATCH In review Yishai Hadas 10 participants 182 messages Sep 21, 2023 – Oct 16, 2023 Page 1 of 2

Subsystems (7)
lore
Share & tools
lore

Yishai Hadas

author maintainer
#0 lore raw

cover Introduce a vfio driver over virtio devices

This series introduce a vfio driver over virtio devices to support the legacy interface functionality for VFs.

Background, from the virtio spec [1].
--------------------------------------------------------------------
In some systems, there is a need to support a virtio legacy driver with a device that does not directly support the legacy interface. In such scenarios, a group owner device can provide the legacy interface functionality for the group member devices. The driver of the owner device can then access the legacy interface of a member device on behalf of the legacy member device driver.

For example, with the SR-IOV group type, group members (VFs) can not present the legacy interface in an I/O BAR in BAR0 as expected by the legacy pci driver. If the legacy driver is running inside a virtual machine, the hypervisor executing the virtual machine can present a virtual device with an I/O BAR in BAR0. The hypervisor intercepts the legacy driver accesses to this I/O BAR and forwards them to the group owner device (PF) using group administration commands.
--------------------------------------------------------------------

The first 7 patches are in the virtio area and handle the below:
- Introduce the admin virtqueue infrastcture.
- Expose APIs to enable upper layers as of vfio, net, etc to execute admin commands.
- Expose the layout of the commands that should be used for supporting the legacy access.

The above follows the virtio spec that was lastly accepted in that area [1].

The last 4 patches are in the vfio area and handle the below:
- Expose some APIs from vfio/pci to be used by the vfio/virtio driver.
- Expose admin commands over virtio device.
- Introduce a vfio driver over virtio devices to support the legacy interface functionality for VFs.

The series was tested successfully over virtio-net VFs in the host, while running in the guest both modern and legacy drivers.

[1]
https://github.com/oasis-tcs/virtio-spec/commit/03c2d32e5093ca9f2a17797242fbef88efe94b8c

Yishai

Feng Liu (7):
virtio-pci: Use virtio pci device layer vq info instead of generic one
virtio: Define feature bit for administration virtqueue
virtio-pci: Introduce admin virtqueue
virtio: Expose the synchronous command helper function
virtio-pci: Introduce admin command sending function
virtio-pci: Introduce API to get PF virtio device from VF PCI device
virtio-pci: Introduce admin commands

Yishai Hadas (4):
vfio/pci: Expose vfio_pci_core_setup_barmap() vfio/pci: Expose vfio_pci_iowrite/read##size() vfio/virtio: Expose admin commands over virtio device vfio/virtio: Introduce a vfio driver over virtio devices

 MAINTAINERS                            |   6 +
 drivers/net/virtio_net.c               |  21 +-
 drivers/vfio/pci/Kconfig               |   2 +
 drivers/vfio/pci/Makefile              |   2 +
 drivers/vfio/pci/vfio_pci_core.c       |  25 ++
 drivers/vfio/pci/vfio_pci_rdwr.c       |  38 +-
 drivers/vfio/pci/virtio/Kconfig        |  15 +
 drivers/vfio/pci/virtio/Makefile       |   4 +
 drivers/vfio/pci/virtio/cmd.c          | 146 +++++++
 drivers/vfio/pci/virtio/cmd.h          |  35 ++
 drivers/vfio/pci/virtio/main.c         | 546 +++++++++++++++++++++++++
 drivers/virtio/Makefile                |   2 +-
 drivers/virtio/virtio.c                |  44 +-
 drivers/virtio/virtio_pci_common.c     |  24 +-
 drivers/virtio/virtio_pci_common.h     |  17 +-
 drivers/virtio/virtio_pci_modern.c     |  12 +-
 drivers/virtio/virtio_pci_modern_avq.c | 138 +++++++
 drivers/virtio/virtio_ring.c           |  27 ++
 include/linux/vfio_pci_core.h          |  20 +
 include/linux/virtio.h                 |  19 +
 include/linux/virtio_config.h          |   7 +
 include/linux/virtio_pci_modern.h      |   3 +
 include/uapi/linux/virtio_config.h     |   8 +-
 include/uapi/linux/virtio_pci.h        |  66 +++
 24 files changed, 1171 insertions(+), 56 deletions(-)
 create mode 100644 drivers/vfio/pci/virtio/Kconfig
 create mode 100644 drivers/vfio/pci/virtio/Makefile
 create mode 100644 drivers/vfio/pci/virtio/cmd.c
 create mode 100644 drivers/vfio/pci/virtio/cmd.h
 create mode 100644 drivers/vfio/pci/virtio/main.c
 create mode 100644 drivers/virtio/virtio_pci_modern_avq.c
Signature · 6 lines
2.27.0

_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization
author maintainer
#1 lore raw

PATCH 1/11 virtio-pci: Use virtio pci device layer vq info instead of generic one

From: Feng Liu <[email protected]>

Currently VQ deletion callback vp_del_vqs() processes generic virtio_device level VQ list instead of VQ information available at PCI layer.

To adhere to the layering, use the pci device level VQ information stored in the virtqueues or vqs.

This also prepares the code to handle PCI layer admin vq life cycle to be managed within the pci layer and thereby avoid undesired deletion of admin vq by upper layer drivers (net, console, vfio), in the del_vqs() callback.

---
 drivers/virtio/virtio_pci_common.c | 12 +++++++++---
 drivers/virtio/virtio_pci_common.h |  1 +
 2 files changed, 10 insertions(+), 3 deletions(-)
drivers/virtio/virtio_pci_common.c +9 -3
index c2524a7207cf..7a3e6edc4dd6 100644
@@ -232,12 +232,16 @@ static void vp_del_vq(struct virtqueue *vq)
 void vp_del_vqs(struct virtio_device *vdev)
 {
 	struct virtio_pci_device *vp_dev = to_vp_device(vdev);
-	struct virtqueue *vq, *n;
+	struct virtqueue *vq;
 	int i;
 
-	list_for_each_entry_safe(vq, n, &vdev->vqs, list) {
+	for (i = 0; i < vp_dev->nvqs; i++) {
+		if (!vp_dev->vqs[i])
+			continue;
+
+		vq = vp_dev->vqs[i]->vq;
 		if (vp_dev->per_vq_vectors) {
-			int v = vp_dev->vqs[vq->index]->msix_vector;
+			int v = vp_dev->vqs[i]->msix_vector;
 
 			if (v != VIRTIO_MSI_NO_VECTOR) {
 				int irq = pci_irq_vector(vp_dev->pci_dev, v);
@@ -294,6 +298,7 @@ static int vp_find_vqs_msix(struct virtio_device *vdev, unsigned int nvqs,
 	vp_dev->vqs = kcalloc(nvqs, sizeof(*vp_dev->vqs), GFP_KERNEL);
 	if (!vp_dev->vqs)
 		return -ENOMEM;
+	vp_dev->nvqs = nvqs;
 
 	if (per_vq_vectors) {
 		/* Best option: one for change interrupt, one per vq. */
@@ -365,6 +370,7 @@ static int vp_find_vqs_intx(struct virtio_device *vdev, unsigned int nvqs,
 	vp_dev->vqs = kcalloc(nvqs, sizeof(*vp_dev->vqs), GFP_KERNEL);
 	if (!vp_dev->vqs)
 		return -ENOMEM;
+	vp_dev->nvqs = nvqs;
 
 	err = request_irq(vp_dev->pci_dev->irq, vp_interrupt, IRQF_SHARED,
 			dev_name(&vdev->dev), vp_dev);
drivers/virtio/virtio_pci_common.h +1 -0
index 4b773bd7c58c..602021967aaa 100644
@@ -60,6 +60,7 @@ struct virtio_pci_device {
 
 	/* array of all queues for house-keeping */
 	struct virtio_pci_vq_info **vqs;
+	u32 nvqs;
 
 	/* MSI-X support */
 	int msix_enabled;
Signature · 6 lines
2.27.0

_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization
#2 lore raw

Re: [PATCH vfio 01/11] virtio-pci: Use virtio pci device layer vq info instead of generic one

Quoted 68 lines · Yishai Hadas

From: Feng Liu <[email protected]>

Currently VQ deletion callback vp_del_vqs() processes generic virtio_device level VQ list instead of VQ information available at PCI layer.

To adhere to the layering, use the pci device level VQ information stored in the virtqueues or vqs.

This also prepares the code to handle PCI layer admin vq life cycle to be managed within the pci layer and thereby avoid undesired deletion of admin vq by upper layer drivers (net, console, vfio), in the del_vqs() callback.

---
 drivers/virtio/virtio_pci_common.c | 12 +++++++++---
 drivers/virtio/virtio_pci_common.h |  1 +
 2 files changed, 10 insertions(+), 3 deletions(-)
drivers/virtio/virtio_pci_common.c +9 -3
index c2524a7207cf..7a3e6edc4dd6 100644
@@ -232,12 +232,16 @@ static void vp_del_vq(struct virtqueue *vq)
 void vp_del_vqs(struct virtio_device *vdev)
 {
 	struct virtio_pci_device *vp_dev = to_vp_device(vdev);
-	struct virtqueue *vq, *n;
+	struct virtqueue *vq;
 	int i;
 
-	list_for_each_entry_safe(vq, n, &vdev->vqs, list) {
+	for (i = 0; i < vp_dev->nvqs; i++) {
+		if (!vp_dev->vqs[i])
+			continue;
+
+		vq = vp_dev->vqs[i]->vq;
 		if (vp_dev->per_vq_vectors) {
-			int v = vp_dev->vqs[vq->index]->msix_vector;
+			int v = vp_dev->vqs[i]->msix_vector;
 
 			if (v != VIRTIO_MSI_NO_VECTOR) {
 				int irq = pci_irq_vector(vp_dev->pci_dev, v);
@@ -294,6 +298,7 @@ static int vp_find_vqs_msix(struct virtio_device *vdev, unsigned int nvqs,
 	vp_dev->vqs = kcalloc(nvqs, sizeof(*vp_dev->vqs), GFP_KERNEL);
 	if (!vp_dev->vqs)
 		return -ENOMEM;
+	vp_dev->nvqs = nvqs;
 
 	if (per_vq_vectors) {
 		/* Best option: one for change interrupt, one per vq. */
@@ -365,6 +370,7 @@ static int vp_find_vqs_intx(struct virtio_device *vdev, unsigned int nvqs,
 	vp_dev->vqs = kcalloc(nvqs, sizeof(*vp_dev->vqs), GFP_KERNEL);
 	if (!vp_dev->vqs)
 		return -ENOMEM;
+	vp_dev->nvqs = nvqs;
 
 	err = request_irq(vp_dev->pci_dev->irq, vp_interrupt, IRQF_SHARED,
 			dev_name(&vdev->dev), vp_dev);
drivers/virtio/virtio_pci_common.h +0 -0
index 4b773bd7c58c..602021967aaa 100644

drivers/virtio/virtio_pci_common.h struct virtio_pci_device { · line 63

Quoted 4 lines
drivers/virtio/virtio_pci_common.h +1 -0
@@ -60,6 +60,7 @@ struct virtio_pci_device {
 
 	/* array of all queues for house-keeping */
 	struct virtio_pci_vq_info **vqs;
+	u32 nvqs;

I don't much like it that we are adding more duplicated info here. In fact, we tried removing the vqs array in 5c34d002dcc7a6dd665a19d098b4f4cd5501ba1a - there was some bug in that patch and the author didn't have the time to debug
so I reverted but I don't really think we need to add to that.

Quoted 5 lines
drivers/virtio/virtio_pci_common.h +0 -0
 	/* MSI-X support */
 	int msix_enabled;
Signature · 1 line
2.27.0

_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization

#3 lore raw

Re: [PATCH vfio 01/11] virtio-pci: Use virtio pci device layer vq info instead of generic one

Quoted 73 lines · Michael S. Tsirkin

External email: Use caution opening links or attachments

Quoted 69 lines · Yishai Hadas

From: Feng Liu <[email protected]>

Currently VQ deletion callback vp_del_vqs() processes generic virtio_device level VQ list instead of VQ information available at PCI layer.

To adhere to the layering, use the pci device level VQ information stored in the virtqueues or vqs.

This also prepares the code to handle PCI layer admin vq life cycle to be managed within the pci layer and thereby avoid undesired deletion of admin vq by upper layer drivers (net, console, vfio), in the del_vqs() callback.

---
  drivers/virtio/virtio_pci_common.c | 12 +++++++++---
  drivers/virtio/virtio_pci_common.h |  1 +
  2 files changed, 10 insertions(+), 3 deletions(-)
drivers/virtio/virtio_pci_common.c +9 -3
index c2524a7207cf..7a3e6edc4dd6 100644
@@ -232,12 +232,16 @@ static void vp_del_vq(struct virtqueue *vq)
  void vp_del_vqs(struct virtio_device *vdev)
  {
       struct virtio_pci_device *vp_dev = to_vp_device(vdev);
-     struct virtqueue *vq, *n;
+     struct virtqueue *vq;
       int i;
 
-     list_for_each_entry_safe(vq, n, &vdev->vqs, list) {
+     for (i = 0; i < vp_dev->nvqs; i++) {
+             if (!vp_dev->vqs[i])
+                     continue;
+
+             vq = vp_dev->vqs[i]->vq;
               if (vp_dev->per_vq_vectors) {
-                     int v = vp_dev->vqs[vq->index]->msix_vector;
+                     int v = vp_dev->vqs[i]->msix_vector;
 
                       if (v != VIRTIO_MSI_NO_VECTOR) {
                               int irq = pci_irq_vector(vp_dev->pci_dev, v);
@@ -294,6 +298,7 @@ static int vp_find_vqs_msix(struct virtio_device *vdev, unsigned int nvqs,
       vp_dev->vqs = kcalloc(nvqs, sizeof(*vp_dev->vqs), GFP_KERNEL);
       if (!vp_dev->vqs)
               return -ENOMEM;
+     vp_dev->nvqs = nvqs;
 
       if (per_vq_vectors) {
               /* Best option: one for change interrupt, one per vq. */
@@ -365,6 +370,7 @@ static int vp_find_vqs_intx(struct virtio_device *vdev, unsigned int nvqs,
       vp_dev->vqs = kcalloc(nvqs, sizeof(*vp_dev->vqs), GFP_KERNEL);
       if (!vp_dev->vqs)
               return -ENOMEM;
+     vp_dev->nvqs = nvqs;
 
       err = request_irq(vp_dev->pci_dev->irq, vp_interrupt, IRQF_SHARED,
                       dev_name(&vdev->dev), vp_dev);
drivers/virtio/virtio_pci_common.h +0 -0
index 4b773bd7c58c..602021967aaa 100644
@@ -60,6 +60,7 @@ struct virtio_pci_device {
 
Quoted 10 lines

drivers/virtio/virtio_pci_common.h struct virtio_pci_device { · line 63

Quoted 3 lines
drivers/virtio/virtio_pci_common.h +1 -0
       /* array of all queues for house-keeping */
       struct virtio_pci_vq_info **vqs;
+     u32 nvqs;

I don't much like it that we are adding more duplicated info here. In fact, we tried removing the vqs array in 5c34d002dcc7a6dd665a19d098b4f4cd5501ba1a - there was some bug in that patch and the author didn't have the time to debug
so I reverted but I don't really think we need to add to that.

Hi Michael

As explained in commit message, this patch is mainly to prepare for the subsequent admin vq patches.

The admin vq is also established using the common mechanism of vring, and is added to vdev->vqs in __vring_new_virtqueue(). So vdev->vqs contains all virtqueues, including rxq, txq, ctrlvq and admin vq.

admin vq should be managed by the virito_pci layer and should not be created or deleted by upper driver (net, blk);
When the upper driver was unloaded, it will call del_vqs() interface, which wll call vp_del_vqs(), and vp_del_vqs() should not delete the admin vq, but only delete the virtqueues created by the upper driver such as rxq, txq, and ctrlq.

vp_dev->vqs[] array only contains virtqueues created by upper driver such as rxq, txq, ctrlq. Traversing vp_dev->vqs array can only delete the upper virtqueues, without the admin vq. Use the vdev->vqs linked list cannot meet the needs.

Can such an explanation be explained clearly? Or do you have any other alternative methods?

Quoted 6 lines
Quoted 5 lines
drivers/virtio/virtio_pci_common.h +0 -1
       /* MSI-X support */
       int msix_enabled;
--

2.27.0

#4 lore raw

Re: [PATCH vfio 01/11] virtio-pci: Use virtio pci device layer vq info instead of generic one

Quoted 64 lines · Feng Liu via Virtualization

External email: Use caution opening links or attachments

Quoted 32 lines · Michael S. Tsirkin

External email: Use caution opening links or attachments

Quoted 28 lines · Yishai Hadas

From: Feng Liu <[email protected]>

pci_irq_vector(vp_dev->pci_dev, v);

drivers/virtio/virtio_pci_common.c +1 -0
@@ -294,6 +298,7 @@ static int vp_find_vqs_msix(struct virtio_device 
 *vdev, unsigned int nvqs,
        vp_dev->vqs = kcalloc(nvqs, sizeof(*vp_dev->vqs), GFP_KERNEL);
        if (!vp_dev->vqs)
                return -ENOMEM;
+     vp_dev->nvqs = nvqs;
 
        if (per_vq_vectors) {

               /* Best option: one for change interrupt, one per vq. */

drivers/virtio/virtio_pci_common.c +1 -0
@@ -365,6 +370,7 @@ static int vp_find_vqs_intx(struct virtio_device 
 *vdev, unsigned int nvqs,
        vp_dev->vqs = kcalloc(nvqs, sizeof(*vp_dev->vqs), GFP_KERNEL);
        if (!vp_dev->vqs)
                return -ENOMEM;
+     vp_dev->nvqs = nvqs;
 
        err = request_irq(vp_dev->pci_dev->irq, vp_interrupt, IRQF_SHARED,

                       dev_name(&vdev->dev), vp_dev);

drivers/virtio/virtio_pci_common.h +0 -0
index 4b773bd7c58c..602021967aaa 100644
@@ -60,6 +60,7 @@ struct virtio_pci_device {
 
Quoted 10 lines

drivers/virtio/virtio_pci_common.h struct virtio_pci_device { · line 63

Quoted 3 lines
drivers/virtio/virtio_pci_common.h +1 -0
        /* array of all queues for house-keeping */
        struct virtio_pci_vq_info **vqs;
+     u32 nvqs;

I don't much like it that we are adding more duplicated info here. In fact, we tried removing the vqs array in 5c34d002dcc7a6dd665a19d098b4f4cd5501ba1a - there was some bug in that patch and the author didn't have the time to debug
so I reverted but I don't really think we need to add to that.

Hi Michael

As explained in commit message, this patch is mainly to prepare for the subsequent admin vq patches.

The admin vq is also established using the common mechanism of vring, and is added to vdev->vqs in __vring_new_virtqueue(). So vdev->vqs contains all virtqueues, including rxq, txq, ctrlvq and admin vq.

admin vq should be managed by the virito_pci layer and should not be created or deleted by upper driver (net, blk);
When the upper driver was unloaded, it will call del_vqs() interface, which wll call vp_del_vqs(), and vp_del_vqs() should not delete the admin vq, but only delete the virtqueues created by the upper driver such as rxq, txq, and ctrlq.

Quoted 9 lines

vp_dev->vqs[] array only contains virtqueues created by upper driver such as rxq, txq, ctrlq. Traversing vp_dev->vqs array can only delete the upper virtqueues, without the admin vq. Use the vdev->vqs linked list cannot meet the needs.

Can such an explanation be explained clearly? Or do you have any other alternative methods?

Hi, Michael
Is the above explanations OK to you?

Thanks
Feng

Quoted 10 lines
Quoted 6 lines
Quoted 5 lines
drivers/virtio/virtio_pci_common.h +0 -0
        /* MSI-X support */
        int msix_enabled;
Signature · 1 line
2.27.0

_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization

#5 lore raw

Re: [PATCH vfio 01/11] virtio-pci: Use virtio pci device layer vq info instead of generic one

Quoted 79 lines · Feng Liu
Quoted 66 lines · Feng Liu via Virtualization

External email: Use caution opening links or attachments

Quoted 34 lines · Michael S. Tsirkin

External email: Use caution opening links or attachments

Quoted 30 lines · Yishai Hadas

From: Feng Liu <[email protected]>

pci_irq_vector(vp_dev->pci_dev, v);

drivers/virtio/virtio_pci_common.c +1 -0
@@ -294,6 +298,7 @@ static int vp_find_vqs_msix(struct
 virtio_device *vdev, unsigned int nvqs,
        vp_dev->vqs = kcalloc(nvqs, sizeof(*vp_dev->vqs), GFP_KERNEL);
        if (!vp_dev->vqs)
                return -ENOMEM;
+     vp_dev->nvqs = nvqs;
 
        if (per_vq_vectors) {

               /* Best option: one for change interrupt, one per vq. */

drivers/virtio/virtio_pci_common.c +1 -0
@@ -365,6 +370,7 @@ static int vp_find_vqs_intx(struct
 virtio_device *vdev, unsigned int nvqs,
        vp_dev->vqs = kcalloc(nvqs, sizeof(*vp_dev->vqs), GFP_KERNEL);
        if (!vp_dev->vqs)
                return -ENOMEM;
+     vp_dev->nvqs = nvqs;
 
        err = request_irq(vp_dev->pci_dev->irq, vp_interrupt,

IRQF_SHARED,
                       dev_name(&vdev->dev), vp_dev);

drivers/virtio/virtio_pci_common.h +0 -0
index 4b773bd7c58c..602021967aaa 100644
@@ -60,6 +60,7 @@ struct virtio_pci_device {
 
Quoted 10 lines

drivers/virtio/virtio_pci_common.h struct virtio_pci_device { · line 63

Quoted 3 lines
drivers/virtio/virtio_pci_common.h +1 -0
        /* array of all queues for house-keeping */
        struct virtio_pci_vq_info **vqs;
+     u32 nvqs;

I don't much like it that we are adding more duplicated info here. In fact, we tried removing the vqs array in 5c34d002dcc7a6dd665a19d098b4f4cd5501ba1a - there was some bug in that patch and the author didn't have the time to debug
so I reverted but I don't really think we need to add to that.

Hi Michael

As explained in commit message, this patch is mainly to prepare for the subsequent admin vq patches.

The admin vq is also established using the common mechanism of vring, and is added to vdev->vqs in __vring_new_virtqueue(). So vdev->vqs contains all virtqueues, including rxq, txq, ctrlvq and admin vq.

admin vq should be managed by the virito_pci layer and should not be created or deleted by upper driver (net, blk);
When the upper driver was unloaded, it will call del_vqs() interface, which wll call vp_del_vqs(), and vp_del_vqs() should not delete the admin vq, but only delete the virtqueues created by the upper driver such as rxq, txq, and ctrlq.

Quoted 9 lines

vp_dev->vqs[] array only contains virtqueues created by upper driver such as rxq, txq, ctrlq. Traversing vp_dev->vqs array can only delete the upper virtqueues, without the admin vq. Use the vdev->vqs linked list cannot meet the needs.

Can such an explanation be explained clearly? Or do you have any other alternative methods?

Quoted 5 lines

Hi, Michael
Is the above explanations OK to you?

Thanks
Feng

First, the patch only addresses pci. Second, yes driver unload calls del_vqs but doesn't it also reset the device? If this happens while vfio tries to send commands to it then you have other problems. And, for the baroque need of admin vq which
most devices don't have you are duplicating logic and wasting memory for everyone.

What is a sane solution? virtio core was never designed to allow two drivers accessing the same device. So don't try, add the logic of device access in virtio core. I feel the problem won't even exist if instead of just exposing the device pointer you expose a sane interface.

Quoted 10 lines
Quoted 10 lines
Quoted 6 lines
Quoted 5 lines
drivers/virtio/virtio_pci_common.h +0 -0
        /* MSI-X support */
        int msix_enabled;
Signature · 1 line
2.27.0

_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization

_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization

author maintainer
#6 lore raw

PATCH 2/11 virtio: Define feature bit for administration virtqueue

In reply to Yishai Hadas

From: Feng Liu <[email protected]>

Introduce VIRTIO_F_ADMIN_VQ which is used for administration virtqueue support.

---
 include/uapi/linux/virtio_config.h | 8 +++++++-
 1 file changed, 7 insertions(+), 1 deletion(-)
include/uapi/linux/virtio_config.h +7 -1
index 2c712c654165..09d694968b14 100644
@@ -52,7 +52,7 @@
  * rest are per-device feature bits.
  */
 #define VIRTIO_TRANSPORT_F_START	28
-#define VIRTIO_TRANSPORT_F_END		41
+#define VIRTIO_TRANSPORT_F_END		42
 
 #ifndef VIRTIO_CONFIG_NO_LEGACY
 /* Do we get callbacks when the ring is completely used, even if we've
@@ -109,4 +109,10 @@
  * This feature indicates that the driver can reset a queue individually.
  */
 #define VIRTIO_F_RING_RESET		40
+
+/*
+ * This feature indicates that the device support administration virtqueues.
+ */
+#define VIRTIO_F_ADMIN_VQ		41
+
 #endif /* _UAPI_LINUX_VIRTIO_CONFIG_H */
Signature · 6 lines
2.27.0

_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization
author maintainer
#7 lore raw

PATCH 3/11 virtio-pci: Introduce admin virtqueue

In reply to Yishai Hadas

From: Feng Liu <[email protected]>

Introduce support for the admin virtqueue. By negotiating VIRTIO_F_ADMIN_VQ feature, driver detects capability and creates one administration virtqueue. Administration virtqueue implementation in virtio pci generic layer, enables multiple types of upper layer drivers such as vfio, net, blk to utilize it.

---
 drivers/virtio/Makefile                |  2 +-
 drivers/virtio/virtio.c                | 37 +++++++++++++--
 drivers/virtio/virtio_pci_common.h     | 15 +++++-
 drivers/virtio/virtio_pci_modern.c     | 10 +++-
 drivers/virtio/virtio_pci_modern_avq.c | 65 ++++++++++++++++++++++++++
 include/linux/virtio_config.h          |  4 ++
 include/linux/virtio_pci_modern.h      |  3 ++
 7 files changed, 129 insertions(+), 7 deletions(-)
 create mode 100644 drivers/virtio/virtio_pci_modern_avq.c
drivers/virtio/Makefile +1 -1
index 8e98d24917cc..dcc535b5b4d9 100644
@@ -5,7 +5,7 @@ obj-$(CONFIG_VIRTIO_PCI_LIB) += virtio_pci_modern_dev.o
 obj-$(CONFIG_VIRTIO_PCI_LIB_LEGACY) += virtio_pci_legacy_dev.o
 obj-$(CONFIG_VIRTIO_MMIO) += virtio_mmio.o
 obj-$(CONFIG_VIRTIO_PCI) += virtio_pci.o
-virtio_pci-y := virtio_pci_modern.o virtio_pci_common.o
+virtio_pci-y := virtio_pci_modern.o virtio_pci_common.o virtio_pci_modern_avq.o
 virtio_pci-$(CONFIG_VIRTIO_PCI_LEGACY) += virtio_pci_legacy.o
 obj-$(CONFIG_VIRTIO_BALLOON) += virtio_balloon.o
 obj-$(CONFIG_VIRTIO_INPUT) += virtio_input.o
drivers/virtio/virtio.c +33 -4
index 3893dc29eb26..f4080692b351 100644
@@ -302,9 +302,15 @@ static int virtio_dev_probe(struct device *_d)
 	if (err)
 		goto err;
 
+	if (dev->config->create_avq) {
+		err = dev->config->create_avq(dev);
+		if (err)
+			goto err;
+	}
+
 	err = drv->probe(dev);
 	if (err)
-		goto err;
+		goto err_probe;
 
 	/* If probe didn't do it, mark device DRIVER_OK ourselves. */
 	if (!(dev->config->get_status(dev) & VIRTIO_CONFIG_S_DRIVER_OK))
@@ -316,6 +322,10 @@ static int virtio_dev_probe(struct device *_d)
 	virtio_config_enable(dev);
 
 	return 0;
+
+err_probe:
+	if (dev->config->destroy_avq)
+		dev->config->destroy_avq(dev);
 err:
 	virtio_add_status(dev, VIRTIO_CONFIG_S_FAILED);
 	return err;
@@ -331,6 +341,9 @@ static void virtio_dev_remove(struct device *_d)
 
 	drv->remove(dev);
 
+	if (dev->config->destroy_avq)
+		dev->config->destroy_avq(dev);
+
 	/* Driver should have reset device. */
 	WARN_ON_ONCE(dev->config->get_status(dev));
 
@@ -489,13 +502,20 @@ EXPORT_SYMBOL_GPL(unregister_virtio_device);
 int virtio_device_freeze(struct virtio_device *dev)
 {
 	struct virtio_driver *drv = drv_to_virtio(dev->dev.driver);
+	int ret;
 
 	virtio_config_disable(dev);
 
 	dev->failed = dev->config->get_status(dev) & VIRTIO_CONFIG_S_FAILED;
 
-	if (drv && drv->freeze)
-		return drv->freeze(dev);
+	if (drv && drv->freeze) {
+		ret = drv->freeze(dev);
+		if (ret)
+			return ret;
+	}
+
+	if (dev->config->destroy_avq)
+		dev->config->destroy_avq(dev);
 
 	return 0;
 }
@@ -532,10 +552,16 @@ int virtio_device_restore(struct virtio_device *dev)
 	if (ret)
 		goto err;
 
+	if (dev->config->create_avq) {
+		ret = dev->config->create_avq(dev);
+		if (ret)
+			goto err;
+	}
+
 	if (drv->restore) {
 		ret = drv->restore(dev);
 		if (ret)
-			goto err;
+			goto err_restore;
 	}
 
 	/* If restore didn't do it, mark device DRIVER_OK ourselves. */
@@ -546,6 +572,9 @@ int virtio_device_restore(struct virtio_device *dev)
 
 	return 0;
 
+err_restore:
+	if (dev->config->destroy_avq)
+		dev->config->destroy_avq(dev);
 err:
 	virtio_add_status(dev, VIRTIO_CONFIG_S_FAILED);
 	return ret;
drivers/virtio/virtio_pci_common.h +14 -1
index 602021967aaa..9bffa95274b6 100644
@@ -41,6 +41,14 @@ struct virtio_pci_vq_info {
 	unsigned int msix_vector;
 };
 
+struct virtio_avq {
+	/* Virtqueue info associated with this admin queue. */
+	struct virtio_pci_vq_info info;
+	/* Name of the admin queue: avq.$index. */
+	char name[10];
+	u16 vq_index;
+};
+
 /* Our device structure */
 struct virtio_pci_device {
 	struct virtio_device vdev;
@@ -58,10 +66,13 @@ struct virtio_pci_device {
 	spinlock_t lock;
 	struct list_head virtqueues;
 
-	/* array of all queues for house-keeping */
+	/* Array of all virtqueues reported in the
+	 * PCI common config num_queues field
+	 */
 	struct virtio_pci_vq_info **vqs;
 	u32 nvqs;
 
+	struct virtio_avq *admin;
 	/* MSI-X support */
 	int msix_enabled;
 	int intx_enabled;
@@ -115,6 +126,8 @@ int vp_find_vqs(struct virtio_device *vdev, unsigned int nvqs,
 		const char * const names[], const bool *ctx,
 		struct irq_affinity *desc);
 const char *vp_bus_name(struct virtio_device *vdev);
+void vp_destroy_avq(struct virtio_device *vdev);
+int vp_create_avq(struct virtio_device *vdev);
 
 /* Setup the affinity for a virtqueue:
  * - force the affinity for per vq vector
drivers/virtio/virtio_pci_modern.c +9 -1
index d6bb68ba84e5..a72c87687196 100644
@@ -37,6 +37,9 @@ static void vp_transport_features(struct virtio_device *vdev, u64 features)
 
 	if (features & BIT_ULL(VIRTIO_F_RING_RESET))
 		__virtio_set_bit(vdev, VIRTIO_F_RING_RESET);
+
+	if (features & BIT_ULL(VIRTIO_F_ADMIN_VQ))
+		__virtio_set_bit(vdev, VIRTIO_F_ADMIN_VQ);
 }
 
 /* virtio config->finalize_features() implementation */
@@ -317,7 +320,8 @@ static struct virtqueue *setup_vq(struct virtio_pci_device *vp_dev,
 	else
 		notify = vp_notify;
 
-	if (index >= vp_modern_get_num_queues(mdev))
+	if (!((index < vp_modern_get_num_queues(mdev) ||
+	      (vp_dev->admin && vp_dev->admin->vq_index == index))))
 		return ERR_PTR(-EINVAL);
 
 	/* Check if queue is either not available or already active. */
@@ -509,6 +513,8 @@ static const struct virtio_config_ops virtio_pci_config_nodev_ops = {
 	.get_shm_region  = vp_get_shm_region,
 	.disable_vq_and_reset = vp_modern_disable_vq_and_reset,
 	.enable_vq_after_reset = vp_modern_enable_vq_after_reset,
+	.create_avq = vp_create_avq,
+	.destroy_avq = vp_destroy_avq,
 };
 
 static const struct virtio_config_ops virtio_pci_config_ops = {
@@ -529,6 +535,8 @@ static const struct virtio_config_ops virtio_pci_config_ops = {
 	.get_shm_region  = vp_get_shm_region,
 	.disable_vq_and_reset = vp_modern_disable_vq_and_reset,
 	.enable_vq_after_reset = vp_modern_enable_vq_after_reset,
+	.create_avq = vp_create_avq,
+	.destroy_avq = vp_destroy_avq,
 };
 
 /* the PCI probing function */
drivers/virtio/virtio_pci_modern_avq.c new file +65 -0

65 lines not on this page · raw message

include/linux/virtio_config.h +4 -0
index 2b3438de2c4d..028c51ea90ee 100644
@@ -93,6 +93,8 @@ typedef void vq_callback_t(struct virtqueue *);
  *	Returns 0 on success or error status
  *	If disable_vq_and_reset is set, then enable_vq_after_reset must also be
  *	set.
+ * @create_avq: initialize admin virtqueue resource.
+ * @destroy_avq: destroy admin virtqueue resource.
  */
 struct virtio_config_ops {
 	void (*get)(struct virtio_device *vdev, unsigned offset,
@@ -120,6 +122,8 @@ struct virtio_config_ops {
 			       struct virtio_shm_region *region, u8 id);
 	int (*disable_vq_and_reset)(struct virtqueue *vq);
 	int (*enable_vq_after_reset)(struct virtqueue *vq);
+	int (*create_avq)(struct virtio_device *vdev);
+	void (*destroy_avq)(struct virtio_device *vdev);
 };
 
 /* If driver didn't advertise the feature, it will never appear. */
include/linux/virtio_pci_modern.h +3 -0
index 067ac1d789bc..f6cb13d858fd 100644
@@ -10,6 +10,9 @@ struct virtio_pci_modern_common_cfg {
 
 	__le16 queue_notify_data;	/* read-write */
 	__le16 queue_reset;		/* read-write */
+
+	__le16 admin_queue_index;	/* read-only */
+	__le16 admin_queue_num;		/* read-only */
 };
 
 struct virtio_pci_modern_device {
Signature · 6 lines
2.27.0

_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization
#8 lore raw

Re: [PATCH vfio 03/11] virtio-pci: Introduce admin virtqueue

Quoted 12 lines · Yishai Hadas

From: Feng Liu <[email protected]>

Introduce support for the admin virtqueue. By negotiating VIRTIO_F_ADMIN_VQ feature, driver detects capability and creates one administration virtqueue. Administration virtqueue implementation in virtio pci generic layer, enables multiple types of upper layer drivers such as vfio, net, blk to utilize it.

Quoted 6 lines
---
 drivers/virtio/Makefile                |  2 +-
 drivers/virtio/virtio.c                | 37 +++++++++++++--
 drivers/virtio/virtio_pci_common.h     | 15 +++++-
 drivers/virtio/virtio_pci_modern.c     | 10 +++-
 drivers/virtio/virtio_pci_modern_avq.c | 65 ++++++++++++++++++++++++++

if you have a .c file without a .h file you know there's something fishy. Just add this inside drivers/virtio/virtio_pci_modern.c ?

Quoted 116 lines
 include/linux/virtio_config.h          |  4 ++
 include/linux/virtio_pci_modern.h      |  3 ++
 7 files changed, 129 insertions(+), 7 deletions(-)
 create mode 100644 drivers/virtio/virtio_pci_modern_avq.c
drivers/virtio/Makefile +1 -1
index 8e98d24917cc..dcc535b5b4d9 100644
@@ -5,7 +5,7 @@ obj-$(CONFIG_VIRTIO_PCI_LIB) += virtio_pci_modern_dev.o
 obj-$(CONFIG_VIRTIO_PCI_LIB_LEGACY) += virtio_pci_legacy_dev.o
 obj-$(CONFIG_VIRTIO_MMIO) += virtio_mmio.o
 obj-$(CONFIG_VIRTIO_PCI) += virtio_pci.o
-virtio_pci-y := virtio_pci_modern.o virtio_pci_common.o
+virtio_pci-y := virtio_pci_modern.o virtio_pci_common.o virtio_pci_modern_avq.o
 virtio_pci-$(CONFIG_VIRTIO_PCI_LEGACY) += virtio_pci_legacy.o
 obj-$(CONFIG_VIRTIO_BALLOON) += virtio_balloon.o
 obj-$(CONFIG_VIRTIO_INPUT) += virtio_input.o
drivers/virtio/virtio.c +33 -4
index 3893dc29eb26..f4080692b351 100644
@@ -302,9 +302,15 @@ static int virtio_dev_probe(struct device *_d)
 	if (err)
 		goto err;
 
+	if (dev->config->create_avq) {
+		err = dev->config->create_avq(dev);
+		if (err)
+			goto err;
+	}
+
 	err = drv->probe(dev);
 	if (err)
-		goto err;
+		goto err_probe;
 
 	/* If probe didn't do it, mark device DRIVER_OK ourselves. */
 	if (!(dev->config->get_status(dev) & VIRTIO_CONFIG_S_DRIVER_OK))
@@ -316,6 +322,10 @@ static int virtio_dev_probe(struct device *_d)
 	virtio_config_enable(dev);
 
 	return 0;
+
+err_probe:
+	if (dev->config->destroy_avq)
+		dev->config->destroy_avq(dev);
 err:
 	virtio_add_status(dev, VIRTIO_CONFIG_S_FAILED);
 	return err;
@@ -331,6 +341,9 @@ static void virtio_dev_remove(struct device *_d)
 
 	drv->remove(dev);
 
+	if (dev->config->destroy_avq)
+		dev->config->destroy_avq(dev);
+
 	/* Driver should have reset device. */
 	WARN_ON_ONCE(dev->config->get_status(dev));
 
@@ -489,13 +502,20 @@ EXPORT_SYMBOL_GPL(unregister_virtio_device);
 int virtio_device_freeze(struct virtio_device *dev)
 {
 	struct virtio_driver *drv = drv_to_virtio(dev->dev.driver);
+	int ret;
 
 	virtio_config_disable(dev);
 
 	dev->failed = dev->config->get_status(dev) & VIRTIO_CONFIG_S_FAILED;
 
-	if (drv && drv->freeze)
-		return drv->freeze(dev);
+	if (drv && drv->freeze) {
+		ret = drv->freeze(dev);
+		if (ret)
+			return ret;
+	}
+
+	if (dev->config->destroy_avq)
+		dev->config->destroy_avq(dev);
 
 	return 0;
 }
@@ -532,10 +552,16 @@ int virtio_device_restore(struct virtio_device *dev)
 	if (ret)
 		goto err;
 
+	if (dev->config->create_avq) {
+		ret = dev->config->create_avq(dev);
+		if (ret)
+			goto err;
+	}
+
 	if (drv->restore) {
 		ret = drv->restore(dev);
 		if (ret)
-			goto err;
+			goto err_restore;
 	}
 
 	/* If restore didn't do it, mark device DRIVER_OK ourselves. */
@@ -546,6 +572,9 @@ int virtio_device_restore(struct virtio_device *dev)
 
 	return 0;
 
+err_restore:
+	if (dev->config->destroy_avq)
+		dev->config->destroy_avq(dev);
 err:
 	virtio_add_status(dev, VIRTIO_CONFIG_S_FAILED);
 	return ret;
drivers/virtio/virtio_pci_common.h +0 -0
index 602021967aaa..9bffa95274b6 100644

drivers/virtio/virtio_pci_common.h struct virtio_pci_vq_info { · line 44

Quoted 4 lines
drivers/virtio/virtio_pci_common.h +1 -0
@@ -41,6 +41,14 @@ struct virtio_pci_vq_info {
 	unsigned int msix_vector;
 };
 
+struct virtio_avq {

admin_vq would be better. and this is pci specific yes? so virtio_pci_

Quoted 12 lines
drivers/virtio/virtio_pci_common.h +7 -0
+	/* Virtqueue info associated with this admin queue. */
+	struct virtio_pci_vq_info info;
+	/* Name of the admin queue: avq.$index. */
+	char name[10];
+	u16 vq_index;
+};
+
 /* Our device structure */
 struct virtio_pci_device {
 	struct virtio_device vdev;
@@ -58,10 +66,13 @@ struct virtio_pci_device {
 	spinlock_t lock;

drivers/virtio/virtio_pci_common.h struct virtio_pci_device { · line 75

Quoted 10 lines
drivers/virtio/virtio_pci_common.h +4 -1
 	struct list_head virtqueues;
 
-	/* array of all queues for house-keeping */
+	/* Array of all virtqueues reported in the
+	 * PCI common config num_queues field
+	 */
 	struct virtio_pci_vq_info **vqs;
 	u32 nvqs;
 
+	struct virtio_avq *admin;

and this could be thinkably admin_vq.

Quoted 152 lines
drivers/virtio/virtio_pci_common.h +2 -0
 	/* MSI-X support */
 	int msix_enabled;
 	int intx_enabled;
@@ -115,6 +126,8 @@ int vp_find_vqs(struct virtio_device *vdev, unsigned int nvqs,
 		const char * const names[], const bool *ctx,
 		struct irq_affinity *desc);
 const char *vp_bus_name(struct virtio_device *vdev);
+void vp_destroy_avq(struct virtio_device *vdev);
+int vp_create_avq(struct virtio_device *vdev);
 
 /* Setup the affinity for a virtqueue:
  * - force the affinity for per vq vector
drivers/virtio/virtio_pci_modern.c +9 -1
index d6bb68ba84e5..a72c87687196 100644
@@ -37,6 +37,9 @@ static void vp_transport_features(struct virtio_device *vdev, u64 features)
 
 	if (features & BIT_ULL(VIRTIO_F_RING_RESET))
 		__virtio_set_bit(vdev, VIRTIO_F_RING_RESET);
+
+	if (features & BIT_ULL(VIRTIO_F_ADMIN_VQ))
+		__virtio_set_bit(vdev, VIRTIO_F_ADMIN_VQ);
 }
 
 /* virtio config->finalize_features() implementation */
@@ -317,7 +320,8 @@ static struct virtqueue *setup_vq(struct virtio_pci_device *vp_dev,
 	else
 		notify = vp_notify;
 
-	if (index >= vp_modern_get_num_queues(mdev))
+	if (!((index < vp_modern_get_num_queues(mdev) ||
+	      (vp_dev->admin && vp_dev->admin->vq_index == index))))
 		return ERR_PTR(-EINVAL);
 
 	/* Check if queue is either not available or already active. */
@@ -509,6 +513,8 @@ static const struct virtio_config_ops virtio_pci_config_nodev_ops = {
 	.get_shm_region  = vp_get_shm_region,
 	.disable_vq_and_reset = vp_modern_disable_vq_and_reset,
 	.enable_vq_after_reset = vp_modern_enable_vq_after_reset,
+	.create_avq = vp_create_avq,
+	.destroy_avq = vp_destroy_avq,
 };
 
 static const struct virtio_config_ops virtio_pci_config_ops = {
@@ -529,6 +535,8 @@ static const struct virtio_config_ops virtio_pci_config_ops = {
 	.get_shm_region  = vp_get_shm_region,
 	.disable_vq_and_reset = vp_modern_disable_vq_and_reset,
 	.enable_vq_after_reset = vp_modern_enable_vq_after_reset,
+	.create_avq = vp_create_avq,
+	.destroy_avq = vp_destroy_avq,
 };
 
 /* the PCI probing function */
drivers/virtio/virtio_pci_modern_avq.c new file +65 -0
new file mode 100644
index 000000000000..114579ad788f
@@ -0,0 +1,65 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+
+#include <linux/virtio.h>
+#include "virtio_pci_common.h"
+
+static u16 vp_modern_avq_num(struct virtio_pci_modern_device *mdev)
+{
+	struct virtio_pci_modern_common_cfg __iomem *cfg;
+
+	cfg = (struct virtio_pci_modern_common_cfg __iomem *)mdev->common;
+	return vp_ioread16(&cfg->admin_queue_num);
+}
+
+static u16 vp_modern_avq_index(struct virtio_pci_modern_device *mdev)
+{
+	struct virtio_pci_modern_common_cfg __iomem *cfg;
+
+	cfg = (struct virtio_pci_modern_common_cfg __iomem *)mdev->common;
+	return vp_ioread16(&cfg->admin_queue_index);
+}
+
+int vp_create_avq(struct virtio_device *vdev)
+{
+	struct virtio_pci_device *vp_dev = to_vp_device(vdev);
+	struct virtio_avq *avq;
+	struct virtqueue *vq;
+	u16 admin_q_num;
+
+	if (!virtio_has_feature(vdev, VIRTIO_F_ADMIN_VQ))
+		return 0;
+
+	admin_q_num = vp_modern_avq_num(&vp_dev->mdev);
+	if (!admin_q_num)
+		return -EINVAL;
+
+	vp_dev->admin = kzalloc(sizeof(*vp_dev->admin), GFP_KERNEL);
+	if (!vp_dev->admin)
+		return -ENOMEM;
+
+	avq = vp_dev->admin;
+	avq->vq_index = vp_modern_avq_index(&vp_dev->mdev);
+	sprintf(avq->name, "avq.%u", avq->vq_index);
+	vq = vp_dev->setup_vq(vp_dev, &vp_dev->admin->info, avq->vq_index, NULL,
+			      avq->name, NULL, VIRTIO_MSI_NO_VECTOR);
+	if (IS_ERR(vq)) {
+		dev_err(&vdev->dev, "failed to setup admin virtqueue");
+		kfree(vp_dev->admin);
+		return PTR_ERR(vq);
+	}
+
+	vp_dev->admin->info.vq = vq;
+	vp_modern_set_queue_enable(&vp_dev->mdev, avq->info.vq->index, true);
+	return 0;
+}
+
+void vp_destroy_avq(struct virtio_device *vdev)
+{
+	struct virtio_pci_device *vp_dev = to_vp_device(vdev);
+
+	if (!vp_dev->admin)
+		return;
+
+	vp_dev->del_vq(&vp_dev->admin->info);
+	kfree(vp_dev->admin);
+}
include/linux/virtio_config.h +4 -0
index 2b3438de2c4d..028c51ea90ee 100644
@@ -93,6 +93,8 @@ typedef void vq_callback_t(struct virtqueue *);
  *	Returns 0 on success or error status
  *	If disable_vq_and_reset is set, then enable_vq_after_reset must also be
  *	set.
+ * @create_avq: initialize admin virtqueue resource.
+ * @destroy_avq: destroy admin virtqueue resource.
  */
 struct virtio_config_ops {
 	void (*get)(struct virtio_device *vdev, unsigned offset,
@@ -120,6 +122,8 @@ struct virtio_config_ops {
 			       struct virtio_shm_region *region, u8 id);
 	int (*disable_vq_and_reset)(struct virtqueue *vq);
 	int (*enable_vq_after_reset)(struct virtqueue *vq);
+	int (*create_avq)(struct virtio_device *vdev);
+	void (*destroy_avq)(struct virtio_device *vdev);
 };
 
 /* If driver didn't advertise the feature, it will never appear. */
include/linux/virtio_pci_modern.h +0 -0
index 067ac1d789bc..f6cb13d858fd 100644

include/linux/virtio_pci_modern.h struct virtio_pci_modern_common_cfg { · line 16

Quoted 7 lines
include/linux/virtio_pci_modern.h +3 -0
@@ -10,6 +10,9 @@ struct virtio_pci_modern_common_cfg {
 
 	__le16 queue_notify_data;	/* read-write */
 	__le16 queue_reset;		/* read-write */
+
+	__le16 admin_queue_index;	/* read-only */
+	__le16 admin_queue_num;		/* read-only */
 };

ouch.
actually there's a problem

mdev->common = vp_modern_map_capability(mdev, common,
sizeof(struct virtio_pci_common_cfg), 4, 0, sizeof(struct virtio_pci_common_cfg), NULL, NULL);

extending this structure means some calls will start failing on existing devices.

even more of an ouch, when we added queue_notify_data and queue_reset we also possibly broke some devices. well hopefully not since no one reported failures but we really need to fix that.

Quoted 4 lines
include/linux/virtio_pci_modern.h +0 -0
 struct virtio_pci_modern_device {
Signature · 1 line
2.27.0

_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization

#9 lore raw

Re: [PATCH vfio 03/11] virtio-pci: Introduce admin virtqueue

Quoted 23 lines · Michael S. Tsirkin

External email: Use caution opening links or attachments

Quoted 12 lines · Yishai Hadas

From: Feng Liu <[email protected]>

Introduce support for the admin virtqueue. By negotiating VIRTIO_F_ADMIN_VQ feature, driver detects capability and creates one administration virtqueue. Administration virtqueue implementation in virtio pci generic layer, enables multiple types of upper layer drivers such as vfio, net, blk to utilize it.

Quoted 6 lines
---
  drivers/virtio/Makefile                |  2 +-
  drivers/virtio/virtio.c                | 37 +++++++++++++--
  drivers/virtio/virtio_pci_common.h     | 15 +++++-
  drivers/virtio/virtio_pci_modern.c     | 10 +++-
  drivers/virtio/virtio_pci_modern_avq.c | 65 ++++++++++++++++++++++++++
Quoted 3 lines

if you have a .c file without a .h file you know there's something fishy. Just add this inside drivers/virtio/virtio_pci_modern.c ?

Will do.

Quoted 119 lines
Quoted 119 lines
  include/linux/virtio_config.h          |  4 ++
  include/linux/virtio_pci_modern.h      |  3 ++
  7 files changed, 129 insertions(+), 7 deletions(-)

create mode 100644 drivers/virtio/virtio_pci_modern_avq.c

drivers/virtio/Makefile +1 -1
index 8e98d24917cc..dcc535b5b4d9 100644
@@ -5,7 +5,7 @@ obj-$(CONFIG_VIRTIO_PCI_LIB) += virtio_pci_modern_dev.o
  obj-$(CONFIG_VIRTIO_PCI_LIB_LEGACY) += virtio_pci_legacy_dev.o
  obj-$(CONFIG_VIRTIO_MMIO) += virtio_mmio.o
  obj-$(CONFIG_VIRTIO_PCI) += virtio_pci.o
-virtio_pci-y := virtio_pci_modern.o virtio_pci_common.o
+virtio_pci-y := virtio_pci_modern.o virtio_pci_common.o virtio_pci_modern_avq.o
  virtio_pci-$(CONFIG_VIRTIO_PCI_LEGACY) += virtio_pci_legacy.o
  obj-$(CONFIG_VIRTIO_BALLOON) += virtio_balloon.o
  obj-$(CONFIG_VIRTIO_INPUT) += virtio_input.o
drivers/virtio/virtio.c +33 -4
index 3893dc29eb26..f4080692b351 100644
@@ -302,9 +302,15 @@ static int virtio_dev_probe(struct device *_d)
       if (err)
               goto err;
 
+     if (dev->config->create_avq) {
+             err = dev->config->create_avq(dev);
+             if (err)
+                     goto err;
+     }
+
       err = drv->probe(dev);
       if (err)
-             goto err;
+             goto err_probe;
 
       /* If probe didn't do it, mark device DRIVER_OK ourselves. */
       if (!(dev->config->get_status(dev) & VIRTIO_CONFIG_S_DRIVER_OK))
@@ -316,6 +322,10 @@ static int virtio_dev_probe(struct device *_d)
       virtio_config_enable(dev);
 
       return 0;
+
+err_probe:
+     if (dev->config->destroy_avq)
+             dev->config->destroy_avq(dev);
  err:
       virtio_add_status(dev, VIRTIO_CONFIG_S_FAILED);
       return err;
@@ -331,6 +341,9 @@ static void virtio_dev_remove(struct device *_d)
 
       drv->remove(dev);
 
+     if (dev->config->destroy_avq)
+             dev->config->destroy_avq(dev);
+
       /* Driver should have reset device. */
       WARN_ON_ONCE(dev->config->get_status(dev));
 
@@ -489,13 +502,20 @@ EXPORT_SYMBOL_GPL(unregister_virtio_device);
  int virtio_device_freeze(struct virtio_device *dev)
  {
       struct virtio_driver *drv = drv_to_virtio(dev->dev.driver);
+     int ret;
 
       virtio_config_disable(dev);
 
       dev->failed = dev->config->get_status(dev) & VIRTIO_CONFIG_S_FAILED;
 
-     if (drv && drv->freeze)
-             return drv->freeze(dev);
+     if (drv && drv->freeze) {
+             ret = drv->freeze(dev);
+             if (ret)
+                     return ret;
+     }
+
+     if (dev->config->destroy_avq)
+             dev->config->destroy_avq(dev);
 
       return 0;
  }
@@ -532,10 +552,16 @@ int virtio_device_restore(struct virtio_device *dev)
       if (ret)
               goto err;
 
+     if (dev->config->create_avq) {
+             ret = dev->config->create_avq(dev);
+             if (ret)
+                     goto err;
+     }
+
       if (drv->restore) {
               ret = drv->restore(dev);
               if (ret)
-                     goto err;
+                     goto err_restore;
       }
 
       /* If restore didn't do it, mark device DRIVER_OK ourselves. */
@@ -546,6 +572,9 @@ int virtio_device_restore(struct virtio_device *dev)
 
       return 0;
 
+err_restore:
+     if (dev->config->destroy_avq)
+             dev->config->destroy_avq(dev);
  err:
       virtio_add_status(dev, VIRTIO_CONFIG_S_FAILED);
       return ret;
drivers/virtio/virtio_pci_common.h +0 -0
index 602021967aaa..9bffa95274b6 100644
@@ -41,6 +41,14 @@ struct virtio_pci_vq_info {
       unsigned int msix_vector;
  };
 
Quoted 4 lines

drivers/virtio/virtio_pci_common.h struct virtio_pci_vq_info { · line 44

Quoted 1 line
drivers/virtio/virtio_pci_common.h +1 -0
+struct virtio_avq {

admin_vq would be better. and this is pci specific yes? so virtio_pci_

Will do.

Quoted 21 lines
Quoted 21 lines
drivers/virtio/virtio_pci_common.h +10 -1
+     /* Virtqueue info associated with this admin queue. */
+     struct virtio_pci_vq_info info;
+     /* Name of the admin queue: avq.$index. */
+     char name[10];
+     u16 vq_index;
+};
+
  /* Our device structure */
  struct virtio_pci_device {
       struct virtio_device vdev;
@@ -58,10 +66,13 @@ struct virtio_pci_device {
       spinlock_t lock;
       struct list_head virtqueues;
 
-     /* array of all queues for house-keeping */
+     /* Array of all virtqueues reported in the
+      * PCI common config num_queues field
+      */
       struct virtio_pci_vq_info **vqs;
       u32 nvqs;
 
Quoted 4 lines

drivers/virtio/virtio_pci_common.h struct virtio_pci_device { · line 75

Quoted 1 line
drivers/virtio/virtio_pci_common.h +1 -0
+     struct virtio_avq *admin;

and this could be thinkably admin_vq.

Will do.

Quoted 169 lines
Quoted 152 lines
drivers/virtio/virtio_pci_common.h +2 -0
       /* MSI-X support */
       int msix_enabled;
       int intx_enabled;
@@ -115,6 +126,8 @@ int vp_find_vqs(struct virtio_device *vdev, unsigned int nvqs,
               const char * const names[], const bool *ctx,
               struct irq_affinity *desc);
  const char *vp_bus_name(struct virtio_device *vdev);
+void vp_destroy_avq(struct virtio_device *vdev);
+int vp_create_avq(struct virtio_device *vdev);
 
  /* Setup the affinity for a virtqueue:
   * - force the affinity for per vq vector
drivers/virtio/virtio_pci_modern.c +9 -1
index d6bb68ba84e5..a72c87687196 100644
@@ -37,6 +37,9 @@ static void vp_transport_features(struct virtio_device *vdev, u64 features)
 
       if (features & BIT_ULL(VIRTIO_F_RING_RESET))
               __virtio_set_bit(vdev, VIRTIO_F_RING_RESET);
+
+     if (features & BIT_ULL(VIRTIO_F_ADMIN_VQ))
+             __virtio_set_bit(vdev, VIRTIO_F_ADMIN_VQ);
  }
 
  /* virtio config->finalize_features() implementation */
@@ -317,7 +320,8 @@ static struct virtqueue *setup_vq(struct virtio_pci_device *vp_dev,
       else
               notify = vp_notify;
 
-     if (index >= vp_modern_get_num_queues(mdev))
+     if (!((index < vp_modern_get_num_queues(mdev) ||
+           (vp_dev->admin && vp_dev->admin->vq_index == index))))
               return ERR_PTR(-EINVAL);
 
       /* Check if queue is either not available or already active. */
@@ -509,6 +513,8 @@ static const struct virtio_config_ops virtio_pci_config_nodev_ops = {
       .get_shm_region  = vp_get_shm_region,
       .disable_vq_and_reset = vp_modern_disable_vq_and_reset,
       .enable_vq_after_reset = vp_modern_enable_vq_after_reset,
+     .create_avq = vp_create_avq,
+     .destroy_avq = vp_destroy_avq,
  };
 
  static const struct virtio_config_ops virtio_pci_config_ops = {
@@ -529,6 +535,8 @@ static const struct virtio_config_ops virtio_pci_config_ops = {
       .get_shm_region  = vp_get_shm_region,
       .disable_vq_and_reset = vp_modern_disable_vq_and_reset,
       .enable_vq_after_reset = vp_modern_enable_vq_after_reset,
+     .create_avq = vp_create_avq,
+     .destroy_avq = vp_destroy_avq,
  };
 
  /* the PCI probing function */
drivers/virtio/virtio_pci_modern_avq.c new file +65 -0
new file mode 100644
index 000000000000..114579ad788f
@@ -0,0 +1,65 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+
+#include <linux/virtio.h>
+#include "virtio_pci_common.h"
+
+static u16 vp_modern_avq_num(struct virtio_pci_modern_device *mdev)
+{
+     struct virtio_pci_modern_common_cfg __iomem *cfg;
+
+     cfg = (struct virtio_pci_modern_common_cfg __iomem *)mdev->common;
+     return vp_ioread16(&cfg->admin_queue_num);
+}
+
+static u16 vp_modern_avq_index(struct virtio_pci_modern_device *mdev)
+{
+     struct virtio_pci_modern_common_cfg __iomem *cfg;
+
+     cfg = (struct virtio_pci_modern_common_cfg __iomem *)mdev->common;
+     return vp_ioread16(&cfg->admin_queue_index);
+}
+
+int vp_create_avq(struct virtio_device *vdev)
+{
+     struct virtio_pci_device *vp_dev = to_vp_device(vdev);
+     struct virtio_avq *avq;
+     struct virtqueue *vq;
+     u16 admin_q_num;
+
+     if (!virtio_has_feature(vdev, VIRTIO_F_ADMIN_VQ))
+             return 0;
+
+     admin_q_num = vp_modern_avq_num(&vp_dev->mdev);
+     if (!admin_q_num)
+             return -EINVAL;
+
+     vp_dev->admin = kzalloc(sizeof(*vp_dev->admin), GFP_KERNEL);
+     if (!vp_dev->admin)
+             return -ENOMEM;
+
+     avq = vp_dev->admin;
+     avq->vq_index = vp_modern_avq_index(&vp_dev->mdev);
+     sprintf(avq->name, "avq.%u", avq->vq_index);
+     vq = vp_dev->setup_vq(vp_dev, &vp_dev->admin->info, avq->vq_index, NULL,
+                           avq->name, NULL, VIRTIO_MSI_NO_VECTOR);
+     if (IS_ERR(vq)) {
+             dev_err(&vdev->dev, "failed to setup admin virtqueue");
+             kfree(vp_dev->admin);
+             return PTR_ERR(vq);
+     }
+
+     vp_dev->admin->info.vq = vq;
+     vp_modern_set_queue_enable(&vp_dev->mdev, avq->info.vq->index, true);
+     return 0;
+}
+
+void vp_destroy_avq(struct virtio_device *vdev)
+{
+     struct virtio_pci_device *vp_dev = to_vp_device(vdev);
+
+     if (!vp_dev->admin)
+             return;
+
+     vp_dev->del_vq(&vp_dev->admin->info);
+     kfree(vp_dev->admin);
+}
include/linux/virtio_config.h +4 -0
index 2b3438de2c4d..028c51ea90ee 100644
@@ -93,6 +93,8 @@ typedef void vq_callback_t(struct virtqueue *);
   *   Returns 0 on success or error status
   *   If disable_vq_and_reset is set, then enable_vq_after_reset must also be
   *   set.
+ * @create_avq: initialize admin virtqueue resource.
+ * @destroy_avq: destroy admin virtqueue resource.
   */
  struct virtio_config_ops {
       void (*get)(struct virtio_device *vdev, unsigned offset,
@@ -120,6 +122,8 @@ struct virtio_config_ops {
                              struct virtio_shm_region *region, u8 id);
       int (*disable_vq_and_reset)(struct virtqueue *vq);
       int (*enable_vq_after_reset)(struct virtqueue *vq);
+     int (*create_avq)(struct virtio_device *vdev);
+     void (*destroy_avq)(struct virtio_device *vdev);
  };
 
  /* If driver didn't advertise the feature, it will never appear. */
include/linux/virtio_pci_modern.h +0 -0
index 067ac1d789bc..f6cb13d858fd 100644

include/linux/virtio_pci_modern.h struct virtio_pci_modern_common_cfg { · line 16

Quoted 7 lines
include/linux/virtio_pci_modern.h +3 -0
@@ -10,6 +10,9 @@ struct virtio_pci_modern_common_cfg {
 
       __le16 queue_notify_data;       /* read-write */
       __le16 queue_reset;             /* read-write */
+
+     __le16 admin_queue_index;       /* read-only */
+     __le16 admin_queue_num;         /* read-only */
  };

ouch.
actually there's a problem

mdev->common = vp_modern_map_capability(mdev, common,
sizeof(struct virtio_pci_common_cfg), 4, 0, sizeof(struct virtio_pci_common_cfg), NULL, NULL);

Quoted 8 lines

extending this structure means some calls will start failing on existing devices.

even more of an ouch, when we added queue_notify_data and queue_reset we also possibly broke some devices. well hopefully not since no one reported failures but we really need to fix that.

Hi Michael

I didn’t see the fail in vp_modern_map_capability(), and vp_modern_map_capability() only read and map pci memory. The length of the memory mapping will increase as the struct virtio_pci_common_cfg increases. No errors are seen.

And according to the existing code, new pci configuration space members can only be added in struct virtio_pci_modern_common_cfg.

Every single entry added here is protected by feature bit, there is no bug AFAIK.

Could you help to explain it more detail? Where and why it will fall if we add new member in struct virtio_pci_modern_common_cfg.

Quoted 5 lines
Quoted 4 lines
include/linux/virtio_pci_modern.h +0 -1
  struct virtio_pci_modern_device {
--

2.27.0

#10 lore raw

Re: [PATCH vfio 03/11] virtio-pci: Introduce admin virtqueue

Quoted 74 lines · Feng Liu via Virtualization

External email: Use caution opening links or attachments

Quoted 12 lines · Michael S. Tsirkin

External email: Use caution opening links or attachments

Quoted 4 lines · Yishai Hadas

From: Feng Liu <[email protected]>

  drivers/virtio/virtio_pci_modern_avq.c | 65 ++++++++++++++++++++++++++

if you have a .c file without a .h file you know there's something fishy. Just add this inside drivers/virtio/virtio_pci_modern.c ?

Will do.

Quoted 4 lines
Quoted 1 line

+struct virtio_avq {

admin_vq would be better. and this is pci specific yes? so virtio_pci_

Will do.

Quoted 5 lines
Quoted 2 lines

+     struct virtio_avq *admin;

and this could be thinkably admin_vq.

Will do.

Quoted 24 lines
Quoted 8 lines

  /* If driver didn't advertise the feature, it will never appear. */

include/linux/virtio_pci_modern.h +0 -0
index 067ac1d789bc..f6cb13d858fd 100644
@@ -10,6 +10,9 @@ struct virtio_pci_modern_common_cfg {
 
Quoted 6 lines

       __le16 queue_notify_data;       /* read-write */ __le16 queue_reset;             /* read-write */ +
+     __le16 admin_queue_index;       /* read-only */ +     __le16 admin_queue_num;         /* read-only */ };

ouch.
actually there's a problem

         mdev->common = vp_modern_map_capability(mdev, common, sizeof(struct virtio_pci_common_cfg), 4, 0, sizeof(struct virtio_pci_common_cfg), NULL, NULL);

Quoted 8 lines

extending this structure means some calls will start failing on existing devices.

even more of an ouch, when we added queue_notify_data and queue_reset we also possibly broke some devices. well hopefully not since no one reported failures but we really need to fix that.

Hi Michael

I didn’t see the fail in vp_modern_map_capability(), and vp_modern_map_capability() only read and map pci memory. The length of the memory mapping will increase as the struct virtio_pci_common_cfg increases. No errors are seen.

Quoted 10 lines

And according to the existing code, new pci configuration space members can only be added in struct virtio_pci_modern_common_cfg.

Every single entry added here is protected by feature bit, there is no bug AFAIK.

Could you help to explain it more detail?  Where and why it will fall if we add new member in struct virtio_pci_modern_common_cfg.

Hi, Michael
Any comments about this?
Thanks
Feng

Quoted 9 lines
Quoted 5 lines
Quoted 4 lines

  struct virtio_pci_modern_device {

Signature · 1 line
2.27.0

_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization

#11 lore raw

Re: [PATCH vfio 03/11] virtio-pci: Introduce admin virtqueue

Quoted 86 lines · Feng Liu
Quoted 83 lines · Feng Liu via Virtualization

External email: Use caution opening links or attachments

Quoted 12 lines · Michael S. Tsirkin

External email: Use caution opening links or attachments

Quoted 4 lines · Yishai Hadas

From: Feng Liu <[email protected]>

  drivers/virtio/virtio_pci_modern_avq.c | 65 ++++++++++++++++++++++++++

if you have a .c file without a .h file you know there's something fishy. Just add this inside drivers/virtio/virtio_pci_modern.c ?

Will do.

Quoted 4 lines
Quoted 1 line

+struct virtio_avq {

admin_vq would be better. and this is pci specific yes? so virtio_pci_

Will do.

Quoted 5 lines
Quoted 2 lines

+     struct virtio_avq *admin;

and this could be thinkably admin_vq.

Will do.

Quoted 27 lines
Quoted 9 lines

  /* If driver didn't advertise the feature, it will never appear. */

include/linux/virtio_pci_modern.h +0 -0
index 067ac1d789bc..f6cb13d858fd 100644
@@ -10,6 +10,9 @@ struct virtio_pci_modern_common_cfg {
 
Quoted 6 lines

       __le16 queue_notify_data;       /* read-write */ __le16 queue_reset;             /* read-write */ +
+     __le16 admin_queue_index;       /* read-only */ +     __le16 admin_queue_num;         /* read-only */ };

ouch.
actually there's a problem

         mdev->common = vp_modern_map_capability(mdev, common, sizeof(struct virtio_pci_common_cfg), 4,
                                       0, sizeof(struct virtio_pci_common_cfg),
                                       NULL, NULL);

Quoted 8 lines

extending this structure means some calls will start failing on existing devices.

even more of an ouch, when we added queue_notify_data and queue_reset we also possibly broke some devices. well hopefully not since no one reported failures but we really need to fix that.

Hi Michael

I didn’t see the fail in vp_modern_map_capability(), and vp_modern_map_capability() only read and map pci memory. The length of the memory mapping will increase as the struct virtio_pci_common_cfg increases. No errors are seen.

And according to the existing code, new pci configuration space members can only be added in struct virtio_pci_modern_common_cfg.

Every single entry added here is protected by feature bit, there is no bug AFAIK.

Quoted 8 lines
Quoted 4 lines

Could you help to explain it more detail?  Where and why it will fall if we add new member in struct virtio_pci_modern_common_cfg.

Hi, Michael
Any comments about this?
Thanks
Feng

If an existing device exposes a small
capability matching old size, then you change size then the check will fail on the existing device and driver won't load.

All this happens way before feature bit checks.

Quoted 9 lines
Quoted 9 lines
Quoted 5 lines
Quoted 4 lines

  struct virtio_pci_modern_device {

Signature · 1 line
2.27.0

_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization

_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization

#12 lore raw

Re: [PATCH vfio 03/11] virtio-pci: Introduce admin virtqueue

Quoted 8 lines · Michael S. Tsirkin

If an existing device exposes a small
capability matching old size, then you change size then the check will fail on the existing device and driver won't load.

All this happens way before feature bit checks.

Will do

Thanks
Feng

Quoted 10 lines
Quoted 9 lines
Quoted 9 lines
Quoted 5 lines
Quoted 4 lines
   struct virtio_pci_modern_device {

--
2.27.0

_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization

author maintainer
#13 lore raw

PATCH 4/11 virtio: Expose the synchronous command helper function

In reply to Yishai Hadas

From: Feng Liu <[email protected]>

Synchronous command helper function is exposed at virtio layer, so that ctrl virtqueue and admin virtqueues can reuse this helper function to send synchronous commands.

---
 drivers/net/virtio_net.c     | 21 ++++++---------------
 drivers/virtio/virtio_ring.c | 27 +++++++++++++++++++++++++++
 include/linux/virtio.h       |  7 +++++++
 3 files changed, 40 insertions(+), 15 deletions(-)
drivers/net/virtio_net.c +6 -15
index fe7f314d65c9..65c210b0fb9e 100644
@@ -2451,7 +2451,7 @@ static bool virtnet_send_command(struct virtnet_info *vi, u8 class, u8 cmd,
 				 struct scatterlist *out)
 {
 	struct scatterlist *sgs[4], hdr, stat;
-	unsigned out_num = 0, tmp;
+	unsigned int out_num = 0;
 	int ret;
 
 	/* Caller should know better */
@@ -2472,23 +2472,14 @@ static bool virtnet_send_command(struct virtnet_info *vi, u8 class, u8 cmd,
 	sgs[out_num] = &stat;
 
 	BUG_ON(out_num + 1 > ARRAY_SIZE(sgs));
-	ret = virtqueue_add_sgs(vi->cvq, sgs, out_num, 1, vi, GFP_ATOMIC);
-	if (ret < 0) {
-		dev_warn(&vi->vdev->dev,
-			 "Failed to add sgs for command vq: %d\n.", ret);
+	ret = virtqueue_exec_cmd(vi->cvq, sgs, out_num, 1, vi, GFP_ATOMIC);
+	if (ret) {
+		dev_err(&vi->vdev->dev,
+			"Failed to exec command vq(%s,%d): %d\n",
+			vi->cvq->name, vi->cvq->index, ret);
 		return false;
 	}
 
-	if (unlikely(!virtqueue_kick(vi->cvq)))
-		return vi->ctrl->status == VIRTIO_NET_OK;
-
-	/* Spin for a response, the kick causes an ioport write, trapping
-	 * into the hypervisor, so the request should be handled immediately.
-	 */
-	while (!virtqueue_get_buf(vi->cvq, &tmp) &&
-	       !virtqueue_is_broken(vi->cvq))
-		cpu_relax();
-
 	return vi->ctrl->status == VIRTIO_NET_OK;
 }
 
drivers/virtio/virtio_ring.c +27 -0
index 51d8f3299c10..253905c0b008 100644
@@ -3251,4 +3251,31 @@ void virtqueue_dma_sync_single_range_for_device(struct virtqueue *_vq,
 }
 EXPORT_SYMBOL_GPL(virtqueue_dma_sync_single_range_for_device);
 
+int virtqueue_exec_cmd(struct virtqueue *vq,
+		       struct scatterlist **sgs,
+		       unsigned int out_num,
+		       unsigned int in_num,
+		       void *data,
+		       gfp_t gfp)
+{
+	int ret, len;
+
+	ret = virtqueue_add_sgs(vq, sgs, out_num, in_num, data, gfp);
+	if (ret < 0)
+		return ret;
+
+	if (unlikely(!virtqueue_kick(vq)))
+		return -EIO;
+
+	/* Spin for a response, the kick causes an ioport write, trapping
+	 * into the hypervisor, so the request should be handled immediately.
+	 */
+	while (!virtqueue_get_buf(vq, &len) &&
+	       !virtqueue_is_broken(vq))
+		cpu_relax();
+
+	return 0;
+}
+EXPORT_SYMBOL_GPL(virtqueue_exec_cmd);
+
 MODULE_LICENSE("GPL");
include/linux/virtio.h +7 -0
index 4cc614a38376..9d39706bed10 100644
@@ -103,6 +103,13 @@ int virtqueue_resize(struct virtqueue *vq, u32 num,
 int virtqueue_reset(struct virtqueue *vq,
 		    void (*recycle)(struct virtqueue *vq, void *buf));
 
+int virtqueue_exec_cmd(struct virtqueue *vq,
+		       struct scatterlist **sgs,
+		       unsigned int out_num,
+		       unsigned int in_num,
+		       void *data,
+		       gfp_t gfp);
+
 /**
  * struct virtio_device - representation of a device using virtio
  * @index: unique position on the virtio bus
Signature · 6 lines
2.27.0

_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization
author maintainer
#14 lore raw

PATCH 5/11 virtio-pci: Introduce admin command sending function

In reply to Yishai Hadas

From: Feng Liu <[email protected]>

Add support for sending admin command through admin virtqueue interface, and expose generic API to execute virtio admin command. Reuse the send synchronous command helper function at virtio transport layer. In addition, add new result state of admin command and admin commands range definitions.

---
 drivers/virtio/virtio.c                |  7 +++
 drivers/virtio/virtio_pci_common.h     |  1 +
 drivers/virtio/virtio_pci_modern.c     |  2 +
 drivers/virtio/virtio_pci_modern_avq.c | 73 ++++++++++++++++++++++++++
 include/linux/virtio.h                 | 11 ++++
 include/linux/virtio_config.h          |  3 ++
 include/uapi/linux/virtio_pci.h        | 22 ++++++++
 7 files changed, 119 insertions(+)
drivers/virtio/virtio.c +7 -0
index f4080692b351..dd71f584a1bd 100644
@@ -582,6 +582,13 @@ int virtio_device_restore(struct virtio_device *dev)
 EXPORT_SYMBOL_GPL(virtio_device_restore);
 #endif
 
+int virtio_admin_cmd_exec(struct virtio_device *vdev,
+			  struct virtio_admin_cmd *cmd)
+{
+	return vdev->config->exec_admin_cmd(vdev, cmd);
+}
+EXPORT_SYMBOL_GPL(virtio_admin_cmd_exec);
+
 static int virtio_init(void)
 {
 	if (bus_register(&virtio_bus) != 0)
drivers/virtio/virtio_pci_common.h +1 -0
index 9bffa95274b6..a579f1338263 100644
@@ -128,6 +128,7 @@ int vp_find_vqs(struct virtio_device *vdev, unsigned int nvqs,
 const char *vp_bus_name(struct virtio_device *vdev);
 void vp_destroy_avq(struct virtio_device *vdev);
 int vp_create_avq(struct virtio_device *vdev);
+int vp_avq_cmd_exec(struct virtio_device *vdev, struct virtio_admin_cmd *cmd);
 
 /* Setup the affinity for a virtqueue:
  * - force the affinity for per vq vector
drivers/virtio/virtio_pci_modern.c +2 -0
index a72c87687196..cac18872b088 100644
@@ -515,6 +515,7 @@ static const struct virtio_config_ops virtio_pci_config_nodev_ops = {
 	.enable_vq_after_reset = vp_modern_enable_vq_after_reset,
 	.create_avq = vp_create_avq,
 	.destroy_avq = vp_destroy_avq,
+	.exec_admin_cmd = vp_avq_cmd_exec,
 };
 
 static const struct virtio_config_ops virtio_pci_config_ops = {
@@ -537,6 +538,7 @@ static const struct virtio_config_ops virtio_pci_config_ops = {
 	.enable_vq_after_reset = vp_modern_enable_vq_after_reset,
 	.create_avq = vp_create_avq,
 	.destroy_avq = vp_destroy_avq,
+	.exec_admin_cmd = vp_avq_cmd_exec,
 };
 
 /* the PCI probing function */
drivers/virtio/virtio_pci_modern_avq.c +73 -0

79 lines not on this page · raw message

include/linux/virtio.h +11 -0
index 9d39706bed10..094a2ef1c8b8 100644
@@ -110,6 +110,14 @@ int virtqueue_exec_cmd(struct virtqueue *vq,
 		       void *data,
 		       gfp_t gfp);
 
+struct virtio_admin_cmd {
+	__le16 opcode;
+	__le16 group_type;
+	__le64 group_member_id;
+	struct scatterlist *data_sg;
+	struct scatterlist *result_sg;
+};
+
 /**
  * struct virtio_device - representation of a device using virtio
  * @index: unique position on the virtio bus
@@ -207,6 +215,9 @@ static inline struct virtio_driver *drv_to_virtio(struct device_driver *drv)
 	return container_of(drv, struct virtio_driver, driver);
 }
 
+int virtio_admin_cmd_exec(struct virtio_device *vdev,
+			  struct virtio_admin_cmd *cmd);
+
 int register_virtio_driver(struct virtio_driver *drv);
 void unregister_virtio_driver(struct virtio_driver *drv);
 
include/linux/virtio_config.h +3 -0
index 028c51ea90ee..e213173e1291 100644
@@ -95,6 +95,7 @@ typedef void vq_callback_t(struct virtqueue *);
  *	set.
  * @create_avq: initialize admin virtqueue resource.
  * @destroy_avq: destroy admin virtqueue resource.
+ * @exec_admin_cmd: Send admin command and get result.
  */
 struct virtio_config_ops {
 	void (*get)(struct virtio_device *vdev, unsigned offset,
@@ -124,6 +125,8 @@ struct virtio_config_ops {
 	int (*enable_vq_after_reset)(struct virtqueue *vq);
 	int (*create_avq)(struct virtio_device *vdev);
 	void (*destroy_avq)(struct virtio_device *vdev);
+	int (*exec_admin_cmd)(struct virtio_device *vdev,
+			      struct virtio_admin_cmd *cmd);
 };
 
 /* If driver didn't advertise the feature, it will never appear. */
include/uapi/linux/virtio_pci.h +22 -0
index f703afc7ad31..1f1ac6ac07df 100644
@@ -207,4 +207,26 @@ struct virtio_pci_cfg_cap {
 
 #endif /* VIRTIO_PCI_NO_MODERN */
 
+/* Admin command status. */
+#define VIRTIO_ADMIN_STATUS_OK		0
+
+struct virtio_admin_cmd_hdr {
+	__le16 opcode;
+	/*
+	 * 1 - SR-IOV
+	 * 2-65535 - reserved
+	 */
+	__le16 group_type;
+	/* Unused, reserved for future extensions. */
+	__u8 reserved1[12];
+	__le64 group_member_id;
+} __packed;
+
+struct virtio_admin_cmd_status {
+	__le16 status;
+	__le16 status_qualifier;
+	/* Unused, reserved for future extensions. */
+	__u8 reserved2[4];
+} __packed;
+
 #endif
Signature · 6 lines
2.27.0

_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization
author maintainer
#15 lore raw

PATCH 6/11 virtio-pci: Introduce API to get PF virtio device from VF PCI device

In reply to Yishai Hadas

From: Feng Liu <[email protected]>

Introduce API to get PF virtio device from the given VF PCI device so that other modules such as vfio in subsequent patch can use it.

---
 drivers/virtio/virtio_pci_common.c | 12 ++++++++++++
 include/linux/virtio.h             |  1 +
 2 files changed, 13 insertions(+)
drivers/virtio/virtio_pci_common.c +12 -0
index 7a3e6edc4dd6..c64484cd5b13 100644
@@ -648,6 +648,18 @@ static struct pci_driver virtio_pci_driver = {
 	.sriov_configure = virtio_pci_sriov_configure,
 };
 
+struct virtio_device *virtio_pci_vf_get_pf_dev(struct pci_dev *pdev)
+{
+	struct virtio_pci_device *pf_vp_dev;
+
+	pf_vp_dev = pci_iov_get_pf_drvdata(pdev, &virtio_pci_driver);
+	if (IS_ERR(pf_vp_dev))
+		return NULL;
+
+	return &pf_vp_dev->vdev;
+}
+EXPORT_SYMBOL_GPL(virtio_pci_vf_get_pf_dev);
+
 module_pci_driver(virtio_pci_driver);
 
 MODULE_AUTHOR("Anthony Liguori <[email protected]>");
include/linux/virtio.h +1 -0
index 094a2ef1c8b8..4ae088ea9299 100644
@@ -217,6 +217,7 @@ static inline struct virtio_driver *drv_to_virtio(struct device_driver *drv)
 
 int virtio_admin_cmd_exec(struct virtio_device *vdev,
 			  struct virtio_admin_cmd *cmd);
+struct virtio_device *virtio_pci_vf_get_pf_dev(struct pci_dev *pdev);
 
 int register_virtio_driver(struct virtio_driver *drv);
 void unregister_virtio_driver(struct virtio_driver *drv);
Signature · 6 lines
2.27.0

_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization
author maintainer
#16 lore raw

PATCH 7/11 virtio-pci: Introduce admin commands

In reply to Yishai Hadas

From: Feng Liu <[email protected]>

Introduces admin commands, as follow:

The "list query" command can be used by the driver to query the set of admin commands supported by the virtio device. The "list use" command is used to inform the virtio device which admin commands the driver will use.
The "legacy common cfg rd/wr" commands are used to read from/write into the legacy common configuration structure.
The "legacy dev cfg rd/wr" commands are used to read from/write into the legacy device configuration structure.
The "notify info" command is used to query the notification region information.

---
 include/uapi/linux/virtio_pci.h | 44 +++++++++++++++++++++++++++++++++
 1 file changed, 44 insertions(+)
include/uapi/linux/virtio_pci.h +44 -0
index 1f1ac6ac07df..2bf275ad0f20 100644
@@ -210,6 +210,23 @@ struct virtio_pci_cfg_cap {
 /* Admin command status. */
 #define VIRTIO_ADMIN_STATUS_OK		0
 
+/* Admin command opcode. */
+#define VIRTIO_ADMIN_CMD_LIST_QUERY	0x0
+#define VIRTIO_ADMIN_CMD_LIST_USE	0x1
+
+/* Admin command group type. */
+#define VIRTIO_ADMIN_GROUP_TYPE_SRIOV	0x1
+
+/* Transitional device admin command. */
+#define VIRTIO_ADMIN_CMD_LEGACY_COMMON_CFG_WRITE	0x2
+#define VIRTIO_ADMIN_CMD_LEGACY_COMMON_CFG_READ		0x3
+#define VIRTIO_ADMIN_CMD_LEGACY_DEV_CFG_WRITE		0x4
+#define VIRTIO_ADMIN_CMD_LEGACY_DEV_CFG_READ		0x5
+#define VIRTIO_ADMIN_CMD_LEGACY_NOTIFY_INFO		0x6
+
+/* Increment MAX_OPCODE to next value when new opcode is added */
+#define VIRTIO_ADMIN_MAX_CMD_OPCODE			0x6
+
 struct virtio_admin_cmd_hdr {
 	__le16 opcode;
 	/*
@@ -229,4 +246,31 @@ struct virtio_admin_cmd_status {
 	__u8 reserved2[4];
 } __packed;
 
+struct virtio_admin_cmd_legacy_wr_data {
+	u8 offset; /* Starting offset of the register(s) to write. */
+	u8 reserved[7];
+	u8 registers[];
+} __packed;
+
+struct virtio_admin_cmd_legacy_rd_data {
+	u8 offset; /* Starting offset of the register(s) to read. */
+} __packed;
+
+#define VIRTIO_ADMIN_CMD_NOTIFY_INFO_FLAGS_END 0
+#define VIRTIO_ADMIN_CMD_NOTIFY_INFO_FLAGS_OWNER_DEV 0x1
+#define VIRTIO_ADMIN_CMD_NOTIFY_INFO_FLAGS_OWNER_MEM 0x2
+
+#define VIRTIO_ADMIN_CMD_MAX_NOTIFY_INFO 4
+
+struct virtio_admin_cmd_notify_info_data {
+	u8 flags; /* 0 = end of list, 1 = owner device, 2 = member device */
+	u8 bar; /* BAR of the member or the owner device */
+	u8 padding[6];
+	__le64 offset; /* Offset within bar. */
+}; __packed
+
+struct virtio_admin_cmd_notify_info_result {
+	struct virtio_admin_cmd_notify_info_data entries[VIRTIO_ADMIN_CMD_MAX_NOTIFY_INFO];
+};
+
 #endif
Signature · 6 lines
2.27.0

_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization
#17 lore raw

Re: [PATCH vfio 07/11] virtio-pci: Introduce admin commands

kernel test robot noticed the following build errors:

Hi Yishai,

kernel test robot noticed the following build errors:

[auto build test ERROR on awilliam-vfio/for-linus]
[also build test ERROR on mst-vhost/linux-next linus/master v6.6-rc2 next-20230921] [cannot apply to awilliam-vfio/next]
[If your patch is applied to the wrong git tree, kindly drop us a note. And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch#_base_tree_information]

url:    https://github.com/intel-lab-lkp/linux/commits/Yishai-Hadas/virtio-pci-Use-virtio-pci-device-layer-vq-info-instead-of-generic-one/20230922-062611
base:   https://github.com/awilliam/linux-vfio.git for-linus
patch link:    https://lore.kernel.org/r/20230921124040.145386-8-yishaih%40nvidia.com
patch subject: [PATCH vfio 07/11] virtio-pci: Introduce admin commands
config: i386-randconfig-012-20230924 (https://download.01.org/0day-ci/archive/20230924/[email protected]/config)
compiler: gcc-12 (Debian 12.2.0-14) 12.2.0
reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20230924/[email protected]/reproduce)

If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/

All errors (new ones prefixed by >>):

In file included from <command-line>:

>> ./usr/include/linux/virtio_pci.h:250:9: error: unknown type name 'u8'
     250 |         u8 offset; /* Starting offset of the register(s) to write. */
         |         ^~
   ./usr/include/linux/virtio_pci.h:251:9: error: unknown type name 'u8'
     251 |         u8 reserved[7];
         |         ^~
   ./usr/include/linux/virtio_pci.h:252:9: error: unknown type name 'u8'
     252 |         u8 registers[];
         |         ^~
   ./usr/include/linux/virtio_pci.h:256:9: error: unknown type name 'u8'
     256 |         u8 offset; /* Starting offset of the register(s) to read. */
         |         ^~
   ./usr/include/linux/virtio_pci.h:266:9: error: unknown type name 'u8'
     266 |         u8 flags; /* 0 = end of list, 1 = owner device, 2 = member device */
         |         ^~
   ./usr/include/linux/virtio_pci.h:267:9: error: unknown type name 'u8'
     267 |         u8 bar; /* BAR of the member or the owner device */
         |         ^~
   ./usr/include/linux/virtio_pci.h:268:9: error: unknown type name 'u8'
     268 |         u8 padding[6];
         |         ^~
Signature · 2 lines
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki
#18 lore raw

Re: [PATCH vfio 07/11] virtio-pci: Introduce admin commands

In reply to Yishai Hadas
kernel test robot noticed the following build warnings:

Hi Yishai,

kernel test robot noticed the following build warnings:

[auto build test WARNING on awilliam-vfio/for-linus]
[also build test WARNING on linus/master v6.6-rc3 next-20230921] [cannot apply to awilliam-vfio/next]
[If your patch is applied to the wrong git tree, kindly drop us a note. And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch#_base_tree_information]

url:    https://github.com/intel-lab-lkp/linux/commits/Yishai-Hadas/virtio-pci-Use-virtio-pci-device-layer-vq-info-instead-of-generic-one/20230922-062611
base:   https://github.com/awilliam/linux-vfio.git for-linus
patch link:    https://lore.kernel.org/r/20230921124040.145386-8-yishaih%40nvidia.com
patch subject: [PATCH vfio 07/11] virtio-pci: Introduce admin commands
config: x86_64-rhel-8.3-rust (https://download.01.org/0day-ci/archive/20230925/[email protected]/config)
compiler: clang version 16.0.4 (https://github.com/llvm/llvm-project.git ae42196bc493ffe877a7e3dff8be32035dea4d07)
reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20230925/[email protected]/reproduce)

If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/

All warnings (new ones prefixed by >>):

In file included from drivers/virtio/virtio_pci_modern_dev.c:3:
In file included from include/linux/virtio_pci_modern.h:6:

>> include/uapi/linux/virtio_pci.h:270:4: warning: attribute '__packed__' is ignored, place it after "struct" to apply attribute to type declaration [-Wignored-attributes]
   }; __packed
      ^
   include/linux/compiler_attributes.h:304:56: note: expanded from macro '__packed'
   #define __packed                        __attribute__((__packed__))
                                                          ^

1 warning generated.

vim +270 include/uapi/linux/virtio_pci.h

264
265 struct virtio_admin_cmd_notify_info_data {
266 u8 flags; /* 0 = end of list, 1 = owner device, 2 = member device */
267 u8 bar; /* BAR of the member or the owner device */
268 u8 padding[6];
269 __le64 offset; /* Offset within bar. */

 > 270	}; __packed

271

Signature · 2 lines
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki
author maintainer
#19 lore raw

PATCH 8/11 vfio/pci: Expose vfio_pci_core_setup_barmap()

In reply to Yishai Hadas

Expose vfio_pci_core_setup_barmap() to be used by drivers.

This will let drivers to mmap a BAR and re-use it from both vfio and the driver when it's applicable.

This API will be used in the next patches by the vfio/virtio coming driver.

---
 drivers/vfio/pci/vfio_pci_core.c | 25 +++++++++++++++++++++++++
 drivers/vfio/pci/vfio_pci_rdwr.c | 28 ++--------------------------
 include/linux/vfio_pci_core.h    |  1 +
 3 files changed, 28 insertions(+), 26 deletions(-)
drivers/vfio/pci/vfio_pci_core.c +25 -0
index 1929103ee59a..b56111ed8a8c 100644
@@ -684,6 +684,31 @@ void vfio_pci_core_disable(struct vfio_pci_core_device *vdev)
 }
 EXPORT_SYMBOL_GPL(vfio_pci_core_disable);
 
+int vfio_pci_core_setup_barmap(struct vfio_pci_core_device *vdev, int bar)
+{
+	struct pci_dev *pdev = vdev->pdev;
+	void __iomem *io;
+	int ret;
+
+	if (vdev->barmap[bar])
+		return 0;
+
+	ret = pci_request_selected_regions(pdev, 1 << bar, "vfio");
+	if (ret)
+		return ret;
+
+	io = pci_iomap(pdev, bar, 0);
+	if (!io) {
+		pci_release_selected_regions(pdev, 1 << bar);
+		return -ENOMEM;
+	}
+
+	vdev->barmap[bar] = io;
+
+	return 0;
+}
+EXPORT_SYMBOL(vfio_pci_core_setup_barmap);
+
 void vfio_pci_core_close_device(struct vfio_device *core_vdev)
 {
 	struct vfio_pci_core_device *vdev =
drivers/vfio/pci/vfio_pci_rdwr.c +2 -26
index e27de61ac9fe..6f08b3ecbb89 100644
@@ -200,30 +200,6 @@ static ssize_t do_io_rw(struct vfio_pci_core_device *vdev, bool test_mem,
 	return done;
 }
 
-static int vfio_pci_setup_barmap(struct vfio_pci_core_device *vdev, int bar)
-{
-	struct pci_dev *pdev = vdev->pdev;
-	int ret;
-	void __iomem *io;
-
-	if (vdev->barmap[bar])
-		return 0;
-
-	ret = pci_request_selected_regions(pdev, 1 << bar, "vfio");
-	if (ret)
-		return ret;
-
-	io = pci_iomap(pdev, bar, 0);
-	if (!io) {
-		pci_release_selected_regions(pdev, 1 << bar);
-		return -ENOMEM;
-	}
-
-	vdev->barmap[bar] = io;
-
-	return 0;
-}
-
 ssize_t vfio_pci_bar_rw(struct vfio_pci_core_device *vdev, char __user *buf,
 			size_t count, loff_t *ppos, bool iswrite)
 {
@@ -262,7 +238,7 @@ ssize_t vfio_pci_bar_rw(struct vfio_pci_core_device *vdev, char __user *buf,
 		}
 		x_end = end;
 	} else {
-		int ret = vfio_pci_setup_barmap(vdev, bar);
+		int ret = vfio_pci_core_setup_barmap(vdev, bar);
 		if (ret) {
 			done = ret;
 			goto out;
@@ -438,7 +414,7 @@ int vfio_pci_ioeventfd(struct vfio_pci_core_device *vdev, loff_t offset,
 		return -EINVAL;
 #endif
 
-	ret = vfio_pci_setup_barmap(vdev, bar);
+	ret = vfio_pci_core_setup_barmap(vdev, bar);
 	if (ret)
 		return ret;
 
include/linux/vfio_pci_core.h +1 -0
index 562e8754869d..67ac58e20e1d 100644
@@ -127,6 +127,7 @@ int vfio_pci_core_match(struct vfio_device *core_vdev, char *buf);
 int vfio_pci_core_enable(struct vfio_pci_core_device *vdev);
 void vfio_pci_core_disable(struct vfio_pci_core_device *vdev);
 void vfio_pci_core_finish_enable(struct vfio_pci_core_device *vdev);
+int vfio_pci_core_setup_barmap(struct vfio_pci_core_device *vdev, int bar);
 pci_ers_result_t vfio_pci_core_aer_err_detected(struct pci_dev *pdev,
 						pci_channel_state_t state);
 
Signature · 6 lines
2.27.0

_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization
#20 lore raw

Re: [PATCH vfio 08/11] vfio/pci: Expose vfio_pci_core_setup_barmap()

Quoted 37 lines · On Thu, 21 Sep 2023 15:40:37 +0300 Yishai Hadas <[email protected]> wrote:

Expose vfio_pci_core_setup_barmap() to be used by drivers.

This will let drivers to mmap a BAR and re-use it from both vfio and the driver when it's applicable.

This API will be used in the next patches by the vfio/virtio coming driver.

---
 drivers/vfio/pci/vfio_pci_core.c | 25 +++++++++++++++++++++++++
 drivers/vfio/pci/vfio_pci_rdwr.c | 28 ++--------------------------
 include/linux/vfio_pci_core.h    |  1 +
 3 files changed, 28 insertions(+), 26 deletions(-)
drivers/vfio/pci/vfio_pci_core.c +14 -0
index 1929103ee59a..b56111ed8a8c 100644
@@ -684,6 +684,31 @@ void vfio_pci_core_disable(struct vfio_pci_core_device *vdev)
 }
 EXPORT_SYMBOL_GPL(vfio_pci_core_disable);
 
+int vfio_pci_core_setup_barmap(struct vfio_pci_core_device *vdev, int bar)
+{
+	struct pci_dev *pdev = vdev->pdev;
+	void __iomem *io;
+	int ret;
+
+	if (vdev->barmap[bar])
+		return 0;
+
+	ret = pci_request_selected_regions(pdev, 1 << bar, "vfio");
+	if (ret)
+		return ret;
+
+	io = pci_iomap(pdev, bar, 0);

drivers/vfio/pci/vfio_pci_core.c void vfio_pci_core_disable(struct vfio_pci_core_device *vdev) · line 710

Quoted 10 lines
drivers/vfio/pci/vfio_pci_core.c +10 -0
+	if (!io) {
+		pci_release_selected_regions(pdev, 1 << bar);
+		return -ENOMEM;
+	}
+
+	vdev->barmap[bar] = io;
+
+	return 0;
+}
+EXPORT_SYMBOL(vfio_pci_core_setup_barmap);

Not to endorse the rest of this yet, but minimally _GPL, same for the following patch. Thanks,

Alex

Quoted 62 lines
drivers/vfio/pci/vfio_pci_core.c +1 -0
+
 void vfio_pci_core_close_device(struct vfio_device *core_vdev)
 {
 	struct vfio_pci_core_device *vdev =
drivers/vfio/pci/vfio_pci_rdwr.c +2 -26
index e27de61ac9fe..6f08b3ecbb89 100644
@@ -200,30 +200,6 @@ static ssize_t do_io_rw(struct vfio_pci_core_device *vdev, bool test_mem,
 	return done;
 }
 
-static int vfio_pci_setup_barmap(struct vfio_pci_core_device *vdev, int bar)
-{
-	struct pci_dev *pdev = vdev->pdev;
-	int ret;
-	void __iomem *io;
-
-	if (vdev->barmap[bar])
-		return 0;
-
-	ret = pci_request_selected_regions(pdev, 1 << bar, "vfio");
-	if (ret)
-		return ret;
-
-	io = pci_iomap(pdev, bar, 0);
-	if (!io) {
-		pci_release_selected_regions(pdev, 1 << bar);
-		return -ENOMEM;
-	}
-
-	vdev->barmap[bar] = io;
-
-	return 0;
-}
-
 ssize_t vfio_pci_bar_rw(struct vfio_pci_core_device *vdev, char __user *buf,
 			size_t count, loff_t *ppos, bool iswrite)
 {
@@ -262,7 +238,7 @@ ssize_t vfio_pci_bar_rw(struct vfio_pci_core_device *vdev, char __user *buf,
 		}
 		x_end = end;
 	} else {
-		int ret = vfio_pci_setup_barmap(vdev, bar);
+		int ret = vfio_pci_core_setup_barmap(vdev, bar);
 		if (ret) {
 			done = ret;
 			goto out;
@@ -438,7 +414,7 @@ int vfio_pci_ioeventfd(struct vfio_pci_core_device *vdev, loff_t offset,
 		return -EINVAL;
 #endif
 
-	ret = vfio_pci_setup_barmap(vdev, bar);
+	ret = vfio_pci_core_setup_barmap(vdev, bar);
 	if (ret)
 		return ret;
 
include/linux/vfio_pci_core.h +0 -0
index 562e8754869d..67ac58e20e1d 100644

include/linux/vfio_pci_core.h int vfio_pci_core_match(struct vfio_device *core_vdev, char *buf); · line 133

Quoted 7 lines
include/linux/vfio_pci_core.h +1 -0
@@ -127,6 +127,7 @@ int vfio_pci_core_match(struct vfio_device *core_vdev, char *buf);
 int vfio_pci_core_enable(struct vfio_pci_core_device *vdev);
 void vfio_pci_core_disable(struct vfio_pci_core_device *vdev);
 void vfio_pci_core_finish_enable(struct vfio_pci_core_device *vdev);
+int vfio_pci_core_setup_barmap(struct vfio_pci_core_device *vdev, int bar);
 pci_ers_result_t vfio_pci_core_aer_err_detected(struct pci_dev *pdev,
 						pci_channel_state_t state);
 

_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization

author maintainer
#21 lore raw

Re: [PATCH vfio 08/11] vfio/pci: Expose vfio_pci_core_setup_barmap()

Quoted 53 lines · Alex Williamson
Quoted 37 lines · On Thu, 21 Sep 2023 15:40:37 +0300 Yishai Hadas <[email protected]> wrote:

Expose vfio_pci_core_setup_barmap() to be used by drivers.

This will let drivers to mmap a BAR and re-use it from both vfio and the driver when it's applicable.

This API will be used in the next patches by the vfio/virtio coming driver.

---
  drivers/vfio/pci/vfio_pci_core.c | 25 +++++++++++++++++++++++++
  drivers/vfio/pci/vfio_pci_rdwr.c | 28 ++--------------------------
  include/linux/vfio_pci_core.h    |  1 +
  3 files changed, 28 insertions(+), 26 deletions(-)
drivers/vfio/pci/vfio_pci_core.c +14 -0
index 1929103ee59a..b56111ed8a8c 100644
@@ -684,6 +684,31 @@ void vfio_pci_core_disable(struct vfio_pci_core_device *vdev)
  }
  EXPORT_SYMBOL_GPL(vfio_pci_core_disable);
 
+int vfio_pci_core_setup_barmap(struct vfio_pci_core_device *vdev, int bar)
+{
+	struct pci_dev *pdev = vdev->pdev;
+	void __iomem *io;
+	int ret;
+
+	if (vdev->barmap[bar])
+		return 0;
+
+	ret = pci_request_selected_regions(pdev, 1 << bar, "vfio");
+	if (ret)
+		return ret;
+
+	io = pci_iomap(pdev, bar, 0);

drivers/vfio/pci/vfio_pci_core.c void vfio_pci_core_disable(struct vfio_pci_core_device *vdev) · line 710

Quoted 10 lines
drivers/vfio/pci/vfio_pci_core.c +10 -0
+	if (!io) {
+		pci_release_selected_regions(pdev, 1 << bar);
+		return -ENOMEM;
+	}
+
+	vdev->barmap[bar] = io;
+
+	return 0;
+}
+EXPORT_SYMBOL(vfio_pci_core_setup_barmap);

Not to endorse the rest of this yet, but minimally _GPL, same for the following patch. Thanks,

Quoted 1 line

Alex

Sure, will change to EXPORT_SYMBOL_GPL as part of V1.

Yishai

Quoted 69 lines
Quoted 69 lines
drivers/vfio/pci/vfio_pci_core.c +1 -0
+
  void vfio_pci_core_close_device(struct vfio_device *core_vdev)
  {
  	struct vfio_pci_core_device *vdev =
drivers/vfio/pci/vfio_pci_rdwr.c +2 -26
index e27de61ac9fe..6f08b3ecbb89 100644
@@ -200,30 +200,6 @@ static ssize_t do_io_rw(struct vfio_pci_core_device *vdev, bool test_mem,
  	return done;
  }
 
-static int vfio_pci_setup_barmap(struct vfio_pci_core_device *vdev, int bar)
-{
-	struct pci_dev *pdev = vdev->pdev;
-	int ret;
-	void __iomem *io;
-
-	if (vdev->barmap[bar])
-		return 0;
-
-	ret = pci_request_selected_regions(pdev, 1 << bar, "vfio");
-	if (ret)
-		return ret;
-
-	io = pci_iomap(pdev, bar, 0);
-	if (!io) {
-		pci_release_selected_regions(pdev, 1 << bar);
-		return -ENOMEM;
-	}
-
-	vdev->barmap[bar] = io;
-
-	return 0;
-}
-
  ssize_t vfio_pci_bar_rw(struct vfio_pci_core_device *vdev, char __user *buf,
  			size_t count, loff_t *ppos, bool iswrite)
  {
@@ -262,7 +238,7 @@ ssize_t vfio_pci_bar_rw(struct vfio_pci_core_device *vdev, char __user *buf,
  		}
  		x_end = end;
  	} else {
-		int ret = vfio_pci_setup_barmap(vdev, bar);
+		int ret = vfio_pci_core_setup_barmap(vdev, bar);
  		if (ret) {
  			done = ret;
  			goto out;
@@ -438,7 +414,7 @@ int vfio_pci_ioeventfd(struct vfio_pci_core_device *vdev, loff_t offset,
  		return -EINVAL;
  #endif
 
-	ret = vfio_pci_setup_barmap(vdev, bar);
+	ret = vfio_pci_core_setup_barmap(vdev, bar);
  	if (ret)
  		return ret;
 
include/linux/vfio_pci_core.h +1 -0
index 562e8754869d..67ac58e20e1d 100644
@@ -127,6 +127,7 @@ int vfio_pci_core_match(struct vfio_device *core_vdev, char *buf);
  int vfio_pci_core_enable(struct vfio_pci_core_device *vdev);
  void vfio_pci_core_disable(struct vfio_pci_core_device *vdev);
  void vfio_pci_core_finish_enable(struct vfio_pci_core_device *vdev);
+int vfio_pci_core_setup_barmap(struct vfio_pci_core_device *vdev, int bar);
  pci_ers_result_t vfio_pci_core_aer_err_detected(struct pci_dev *pdev,
  						pci_channel_state_t state);
 
author maintainer
#22 lore raw

PATCH 9/11 vfio/pci: Expose vfio_pci_iowrite/read##size()

In reply to Yishai Hadas

Expose vfio_pci_iowrite/read##size() to let it be used by drivers.

This functionality is needed to enable direct access to some physical BAR of the device with the proper locks/checks in place.

The next patches from this series will use this functionality on a data path flow when a direct access to the BAR is needed.

---
 drivers/vfio/pci/vfio_pci_rdwr.c | 10 ++++++----
 include/linux/vfio_pci_core.h    | 19 +++++++++++++++++++
 2 files changed, 25 insertions(+), 4 deletions(-)
drivers/vfio/pci/vfio_pci_rdwr.c +6 -4
index 6f08b3ecbb89..5d84bad7d30c 100644
@@ -38,7 +38,7 @@
 #define vfio_iowrite8	iowrite8
 
 #define VFIO_IOWRITE(size) \
-static int vfio_pci_iowrite##size(struct vfio_pci_core_device *vdev,		\
+int vfio_pci_iowrite##size(struct vfio_pci_core_device *vdev,		\
 			bool test_mem, u##size val, void __iomem *io)	\
 {									\
 	if (test_mem) {							\
@@ -55,7 +55,8 @@ static int vfio_pci_iowrite##size(struct vfio_pci_core_device *vdev,		\
 		up_read(&vdev->memory_lock);				\
 									\
 	return 0;							\
-}
+}									\
+EXPORT_SYMBOL(vfio_pci_iowrite##size);
 
 VFIO_IOWRITE(8)
 VFIO_IOWRITE(16)
@@ -65,7 +66,7 @@ VFIO_IOWRITE(64)
 #endif
 
 #define VFIO_IOREAD(size) \
-static int vfio_pci_ioread##size(struct vfio_pci_core_device *vdev,		\
+int vfio_pci_ioread##size(struct vfio_pci_core_device *vdev,		\
 			bool test_mem, u##size *val, void __iomem *io)	\
 {									\
 	if (test_mem) {							\
@@ -82,7 +83,8 @@ static int vfio_pci_ioread##size(struct vfio_pci_core_device *vdev,		\
 		up_read(&vdev->memory_lock);				\
 									\
 	return 0;							\
-}
+}									\
+EXPORT_SYMBOL(vfio_pci_ioread##size);
 
 VFIO_IOREAD(8)
 VFIO_IOREAD(16)
include/linux/vfio_pci_core.h +19 -0
index 67ac58e20e1d..22c915317788 100644
@@ -131,4 +131,23 @@ int vfio_pci_core_setup_barmap(struct vfio_pci_core_device *vdev, int bar);
 pci_ers_result_t vfio_pci_core_aer_err_detected(struct pci_dev *pdev,
 						pci_channel_state_t state);
 
+#define VFIO_IOWRITE_DECLATION(size) \
+int vfio_pci_iowrite##size(struct vfio_pci_core_device *vdev,		\
+			bool test_mem, u##size val, void __iomem *io);
+
+VFIO_IOWRITE_DECLATION(8)
+VFIO_IOWRITE_DECLATION(16)
+VFIO_IOWRITE_DECLATION(32)
+#ifdef iowrite64
+VFIO_IOWRITE_DECLATION(64)
+#endif
+
+#define VFIO_IOREAD_DECLATION(size) \
+int vfio_pci_ioread##size(struct vfio_pci_core_device *vdev,		\
+			bool test_mem, u##size *val, void __iomem *io);
+
+VFIO_IOREAD_DECLATION(8)
+VFIO_IOREAD_DECLATION(16)
+VFIO_IOREAD_DECLATION(32)
+
 #endif /* VFIO_PCI_CORE_H */
Signature · 6 lines
2.27.0

_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization
author maintainer
#23 lore raw

PATCH 10/11 vfio/virtio: Expose admin commands over virtio device

In reply to Yishai Hadas

Expose admin commands over the virtio device, to be used by the vfio-virtio driver in the next patches.

It includes: list query/use, legacy write/read, read notify_info.

---
 drivers/vfio/pci/virtio/cmd.c | 146 ++++++++++++++++++++++++++++++++++
 drivers/vfio/pci/virtio/cmd.h |  27 +++++++
 2 files changed, 173 insertions(+)
 create mode 100644 drivers/vfio/pci/virtio/cmd.c
 create mode 100644 drivers/vfio/pci/virtio/cmd.h
drivers/vfio/pci/virtio/cmd.c new file +146 -0
new file mode 100644
index 000000000000..f068239cdbb0
@@ -0,0 +1,146 @@
+// SPDX-License-Identifier: GPL-2.0 OR Linux-OpenIB
+/*
+ * Copyright (c) 2023, NVIDIA CORPORATION & AFFILIATES. All rights reserved
+ */
+
+#include "cmd.h"
+
+int virtiovf_cmd_list_query(struct pci_dev *pdev, u8 *buf, int buf_size)
+{
+	struct virtio_device *virtio_dev = virtio_pci_vf_get_pf_dev(pdev);
+	struct scatterlist out_sg;
+	struct virtio_admin_cmd cmd = {};
+
+	if (!virtio_dev)
+		return -ENOTCONN;
+
+	sg_init_one(&out_sg, buf, buf_size);
+	cmd.opcode = VIRTIO_ADMIN_CMD_LIST_QUERY;
+	cmd.group_type = VIRTIO_ADMIN_GROUP_TYPE_SRIOV;
+	cmd.result_sg = &out_sg;
+
+	return virtio_admin_cmd_exec(virtio_dev, &cmd);
+}
+
+int virtiovf_cmd_list_use(struct pci_dev *pdev, u8 *buf, int buf_size)
+{
+	struct virtio_device *virtio_dev = virtio_pci_vf_get_pf_dev(pdev);
+	struct scatterlist in_sg;
+	struct virtio_admin_cmd cmd = {};
+
+	if (!virtio_dev)
+		return -ENOTCONN;
+
+	sg_init_one(&in_sg, buf, buf_size);
+	cmd.opcode = VIRTIO_ADMIN_CMD_LIST_USE;
+	cmd.group_type = VIRTIO_ADMIN_GROUP_TYPE_SRIOV;
+	cmd.data_sg = &in_sg;
+
+	return virtio_admin_cmd_exec(virtio_dev, &cmd);
+}
+
+int virtiovf_cmd_lr_write(struct virtiovf_pci_core_device *virtvdev, u16 opcode,
+			  u8 offset, u8 size, u8 *buf)
+{
+	struct virtio_device *virtio_dev =
+		virtio_pci_vf_get_pf_dev(virtvdev->core_device.pdev);
+	struct virtio_admin_cmd_data_lr_write *in;
+	struct scatterlist in_sg;
+	struct virtio_admin_cmd cmd = {};
+	int ret;
+
+	if (!virtio_dev)
+		return -ENOTCONN;
+
+	in = kzalloc(sizeof(*in) + size, GFP_KERNEL);
+	if (!in)
+		return -ENOMEM;
+
+	in->offset = offset;
+	memcpy(in->registers, buf, size);
+	sg_init_one(&in_sg, in, sizeof(*in) + size);
+	cmd.opcode = opcode;
+	cmd.group_type = VIRTIO_ADMIN_GROUP_TYPE_SRIOV;
+	cmd.group_member_id = virtvdev->vf_id + 1;
+	cmd.data_sg = &in_sg;
+	ret = virtio_admin_cmd_exec(virtio_dev, &cmd);
+
+	kfree(in);
+	return ret;
+}
+
+int virtiovf_cmd_lr_read(struct virtiovf_pci_core_device *virtvdev, u16 opcode,
+			 u8 offset, u8 size, u8 *buf)
+{
+	struct virtio_device *virtio_dev =
+		virtio_pci_vf_get_pf_dev(virtvdev->core_device.pdev);
+	struct virtio_admin_cmd_data_lr_read *in;
+	struct scatterlist in_sg, out_sg;
+	struct virtio_admin_cmd cmd = {};
+	int ret;
+
+	if (!virtio_dev)
+		return -ENOTCONN;
+
+	in = kzalloc(sizeof(*in), GFP_KERNEL);
+	if (!in)
+		return -ENOMEM;
+
+	in->offset = offset;
+	sg_init_one(&in_sg, in, sizeof(*in));
+	sg_init_one(&out_sg, buf, size);
+	cmd.opcode = opcode;
+	cmd.group_type = VIRTIO_ADMIN_GROUP_TYPE_SRIOV;
+	cmd.data_sg = &in_sg;
+	cmd.result_sg = &out_sg;
+	cmd.group_member_id = virtvdev->vf_id + 1;
+	ret = virtio_admin_cmd_exec(virtio_dev, &cmd);
+
+	kfree(in);
+	return ret;
+}
+
+int virtiovf_cmd_lq_read_notify(struct virtiovf_pci_core_device *virtvdev,
+				u8 req_bar_flags, u8 *bar, u64 *bar_offset)
+{
+	struct virtio_device *virtio_dev =
+		virtio_pci_vf_get_pf_dev(virtvdev->core_device.pdev);
+	struct virtio_admin_cmd_notify_info_result *out;
+	struct scatterlist out_sg;
+	struct virtio_admin_cmd cmd = {};
+	int ret;
+
+	if (!virtio_dev)
+		return -ENOTCONN;
+
+	out = kzalloc(sizeof(*out), GFP_KERNEL);
+	if (!out)
+		return -ENOMEM;
+
+	sg_init_one(&out_sg, out, sizeof(*out));
+	cmd.opcode = VIRTIO_ADMIN_CMD_LEGACY_NOTIFY_INFO;
+	cmd.group_type = VIRTIO_ADMIN_GROUP_TYPE_SRIOV;
+	cmd.result_sg = &out_sg;
+	cmd.group_member_id = virtvdev->vf_id + 1;
+	ret = virtio_admin_cmd_exec(virtio_dev, &cmd);
+	if (!ret) {
+		struct virtio_admin_cmd_notify_info_data *entry;
+		int i;
+
+		ret = -ENOENT;
+		for (i = 0; i < VIRTIO_ADMIN_CMD_MAX_NOTIFY_INFO; i++) {
+			entry = &out->entries[i];
+			if (entry->flags == VIRTIO_ADMIN_CMD_NOTIFY_INFO_FLAGS_END)
+				break;
+			if (entry->flags != req_bar_flags)
+				continue;
+			*bar = entry->bar;
+			*bar_offset = le64_to_cpu(entry->offset);
+			ret = 0;
+			break;
+		}
+	}
+
+	kfree(out);
+	return ret;
+}
drivers/vfio/pci/virtio/cmd.h new file +27 -0
new file mode 100644
index 000000000000..c2a3645f4b90
@@ -0,0 +1,27 @@
+// SPDX-License-Identifier: GPL-2.0 OR Linux-OpenIB
+/*
+ * Copyright (c) 2023, NVIDIA CORPORATION & AFFILIATES. All rights reserved.
+ */
+
+#ifndef VIRTIO_VFIO_CMD_H
+#define VIRTIO_VFIO_CMD_H
+
+#include <linux/kernel.h>
+#include <linux/virtio.h>
+#include <linux/vfio_pci_core.h>
+#include <linux/virtio_pci.h>
+
+struct virtiovf_pci_core_device {
+	struct vfio_pci_core_device core_device;
+	int vf_id;
+};
+
+int virtiovf_cmd_list_query(struct pci_dev *pdev, u8 *buf, int buf_size);
+int virtiovf_cmd_list_use(struct pci_dev *pdev, u8 *buf, int buf_size);
+int virtiovf_cmd_lr_write(struct virtiovf_pci_core_device *virtvdev, u16 opcode,
+			  u8 offset, u8 size, u8 *buf);
+int virtiovf_cmd_lr_read(struct virtiovf_pci_core_device *virtvdev, u16 opcode,
+			 u8 offset, u8 size, u8 *buf);
+int virtiovf_cmd_lq_read_notify(struct virtiovf_pci_core_device *virtvdev,
+				u8 req_bar_flags, u8 *bar, u64 *bar_offset);
+#endif /* VIRTIO_VFIO_CMD_H */
Signature · 6 lines
2.27.0

_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization
#24 lore raw

Re: [PATCH vfio 10/11] vfio/virtio: Expose admin commands over virtio device

Quoted 6 lines · Yishai Hadas

Expose admin commands over the virtio device, to be used by the vfio-virtio driver in the next patches.

It includes: list query/use, legacy write/read, read notify_info.

I don't get the motivation for this and the next patch. We already have vdpa that seems to do exactly this:
drive virtio from userspace. Why do we need these extra 1000 lines of code in vfio - just because we can?
Not to talk about user confusion all this will cause.

Quoted 194 lines
---
 drivers/vfio/pci/virtio/cmd.c | 146 ++++++++++++++++++++++++++++++++++
 drivers/vfio/pci/virtio/cmd.h |  27 +++++++
 2 files changed, 173 insertions(+)
 create mode 100644 drivers/vfio/pci/virtio/cmd.c
 create mode 100644 drivers/vfio/pci/virtio/cmd.h
drivers/vfio/pci/virtio/cmd.c new file +146 -0
new file mode 100644
index 000000000000..f068239cdbb0
@@ -0,0 +1,146 @@
+// SPDX-License-Identifier: GPL-2.0 OR Linux-OpenIB
+/*
+ * Copyright (c) 2023, NVIDIA CORPORATION & AFFILIATES. All rights reserved
+ */
+
+#include "cmd.h"
+
+int virtiovf_cmd_list_query(struct pci_dev *pdev, u8 *buf, int buf_size)
+{
+	struct virtio_device *virtio_dev = virtio_pci_vf_get_pf_dev(pdev);
+	struct scatterlist out_sg;
+	struct virtio_admin_cmd cmd = {};
+
+	if (!virtio_dev)
+		return -ENOTCONN;
+
+	sg_init_one(&out_sg, buf, buf_size);
+	cmd.opcode = VIRTIO_ADMIN_CMD_LIST_QUERY;
+	cmd.group_type = VIRTIO_ADMIN_GROUP_TYPE_SRIOV;
+	cmd.result_sg = &out_sg;
+
+	return virtio_admin_cmd_exec(virtio_dev, &cmd);
+}
+
+int virtiovf_cmd_list_use(struct pci_dev *pdev, u8 *buf, int buf_size)
+{
+	struct virtio_device *virtio_dev = virtio_pci_vf_get_pf_dev(pdev);
+	struct scatterlist in_sg;
+	struct virtio_admin_cmd cmd = {};
+
+	if (!virtio_dev)
+		return -ENOTCONN;
+
+	sg_init_one(&in_sg, buf, buf_size);
+	cmd.opcode = VIRTIO_ADMIN_CMD_LIST_USE;
+	cmd.group_type = VIRTIO_ADMIN_GROUP_TYPE_SRIOV;
+	cmd.data_sg = &in_sg;
+
+	return virtio_admin_cmd_exec(virtio_dev, &cmd);
+}
+
+int virtiovf_cmd_lr_write(struct virtiovf_pci_core_device *virtvdev, u16 opcode,
+			  u8 offset, u8 size, u8 *buf)
+{
+	struct virtio_device *virtio_dev =
+		virtio_pci_vf_get_pf_dev(virtvdev->core_device.pdev);
+	struct virtio_admin_cmd_data_lr_write *in;
+	struct scatterlist in_sg;
+	struct virtio_admin_cmd cmd = {};
+	int ret;
+
+	if (!virtio_dev)
+		return -ENOTCONN;
+
+	in = kzalloc(sizeof(*in) + size, GFP_KERNEL);
+	if (!in)
+		return -ENOMEM;
+
+	in->offset = offset;
+	memcpy(in->registers, buf, size);
+	sg_init_one(&in_sg, in, sizeof(*in) + size);
+	cmd.opcode = opcode;
+	cmd.group_type = VIRTIO_ADMIN_GROUP_TYPE_SRIOV;
+	cmd.group_member_id = virtvdev->vf_id + 1;
+	cmd.data_sg = &in_sg;
+	ret = virtio_admin_cmd_exec(virtio_dev, &cmd);
+
+	kfree(in);
+	return ret;
+}
+
+int virtiovf_cmd_lr_read(struct virtiovf_pci_core_device *virtvdev, u16 opcode,
+			 u8 offset, u8 size, u8 *buf)
+{
+	struct virtio_device *virtio_dev =
+		virtio_pci_vf_get_pf_dev(virtvdev->core_device.pdev);
+	struct virtio_admin_cmd_data_lr_read *in;
+	struct scatterlist in_sg, out_sg;
+	struct virtio_admin_cmd cmd = {};
+	int ret;
+
+	if (!virtio_dev)
+		return -ENOTCONN;
+
+	in = kzalloc(sizeof(*in), GFP_KERNEL);
+	if (!in)
+		return -ENOMEM;
+
+	in->offset = offset;
+	sg_init_one(&in_sg, in, sizeof(*in));
+	sg_init_one(&out_sg, buf, size);
+	cmd.opcode = opcode;
+	cmd.group_type = VIRTIO_ADMIN_GROUP_TYPE_SRIOV;
+	cmd.data_sg = &in_sg;
+	cmd.result_sg = &out_sg;
+	cmd.group_member_id = virtvdev->vf_id + 1;
+	ret = virtio_admin_cmd_exec(virtio_dev, &cmd);
+
+	kfree(in);
+	return ret;
+}
+
+int virtiovf_cmd_lq_read_notify(struct virtiovf_pci_core_device *virtvdev,
+				u8 req_bar_flags, u8 *bar, u64 *bar_offset)
+{
+	struct virtio_device *virtio_dev =
+		virtio_pci_vf_get_pf_dev(virtvdev->core_device.pdev);
+	struct virtio_admin_cmd_notify_info_result *out;
+	struct scatterlist out_sg;
+	struct virtio_admin_cmd cmd = {};
+	int ret;
+
+	if (!virtio_dev)
+		return -ENOTCONN;
+
+	out = kzalloc(sizeof(*out), GFP_KERNEL);
+	if (!out)
+		return -ENOMEM;
+
+	sg_init_one(&out_sg, out, sizeof(*out));
+	cmd.opcode = VIRTIO_ADMIN_CMD_LEGACY_NOTIFY_INFO;
+	cmd.group_type = VIRTIO_ADMIN_GROUP_TYPE_SRIOV;
+	cmd.result_sg = &out_sg;
+	cmd.group_member_id = virtvdev->vf_id + 1;
+	ret = virtio_admin_cmd_exec(virtio_dev, &cmd);
+	if (!ret) {
+		struct virtio_admin_cmd_notify_info_data *entry;
+		int i;
+
+		ret = -ENOENT;
+		for (i = 0; i < VIRTIO_ADMIN_CMD_MAX_NOTIFY_INFO; i++) {
+			entry = &out->entries[i];
+			if (entry->flags == VIRTIO_ADMIN_CMD_NOTIFY_INFO_FLAGS_END)
+				break;
+			if (entry->flags != req_bar_flags)
+				continue;
+			*bar = entry->bar;
+			*bar_offset = le64_to_cpu(entry->offset);
+			ret = 0;
+			break;
+		}
+	}
+
+	kfree(out);
+	return ret;
+}
drivers/vfio/pci/virtio/cmd.h new file +27 -0
new file mode 100644
index 000000000000..c2a3645f4b90
@@ -0,0 +1,27 @@
+// SPDX-License-Identifier: GPL-2.0 OR Linux-OpenIB
+/*
+ * Copyright (c) 2023, NVIDIA CORPORATION & AFFILIATES. All rights reserved.
+ */
+
+#ifndef VIRTIO_VFIO_CMD_H
+#define VIRTIO_VFIO_CMD_H
+
+#include <linux/kernel.h>
+#include <linux/virtio.h>
+#include <linux/vfio_pci_core.h>
+#include <linux/virtio_pci.h>
+
+struct virtiovf_pci_core_device {
+	struct vfio_pci_core_device core_device;
+	int vf_id;
+};
+
+int virtiovf_cmd_list_query(struct pci_dev *pdev, u8 *buf, int buf_size);
+int virtiovf_cmd_list_use(struct pci_dev *pdev, u8 *buf, int buf_size);
+int virtiovf_cmd_lr_write(struct virtiovf_pci_core_device *virtvdev, u16 opcode,
+			  u8 offset, u8 size, u8 *buf);
+int virtiovf_cmd_lr_read(struct virtiovf_pci_core_device *virtvdev, u16 opcode,
+			 u8 offset, u8 size, u8 *buf);
+int virtiovf_cmd_lq_read_notify(struct virtiovf_pci_core_device *virtvdev,
+				u8 req_bar_flags, u8 *bar, u64 *bar_offset);
+#endif /* VIRTIO_VFIO_CMD_H */
Signature · 1 line
2.27.0

_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization

#25 lore raw

Re: [PATCH vfio 10/11] vfio/virtio: Expose admin commands over virtio device

In reply to Yishai Hadas
Quoted 33 lines · Yishai Hadas

Expose admin commands over the virtio device, to be used by the vfio-virtio driver in the next patches.

It includes: list query/use, legacy write/read, read notify_info.

---
 drivers/vfio/pci/virtio/cmd.c | 146 ++++++++++++++++++++++++++++++++++
 drivers/vfio/pci/virtio/cmd.h |  27 +++++++
 2 files changed, 173 insertions(+)
 create mode 100644 drivers/vfio/pci/virtio/cmd.c
 create mode 100644 drivers/vfio/pci/virtio/cmd.h
drivers/vfio/pci/virtio/cmd.c new file +14 -0
new file mode 100644
index 000000000000..f068239cdbb0
@@ -0,0 +1,146 @@
+// SPDX-License-Identifier: GPL-2.0 OR Linux-OpenIB
+/*
+ * Copyright (c) 2023, NVIDIA CORPORATION & AFFILIATES. All rights reserved
+ */
+
+#include "cmd.h"
+
+int virtiovf_cmd_list_query(struct pci_dev *pdev, u8 *buf, int buf_size)
+{
+	struct virtio_device *virtio_dev = virtio_pci_vf_get_pf_dev(pdev);
+	struct scatterlist out_sg;
+	struct virtio_admin_cmd cmd = {};
+
+	if (!virtio_dev)

drivers/vfio/pci/virtio/cmd.c line 24

Quoted 10 lines
drivers/vfio/pci/virtio/cmd.c new file +10 -0
+		return -ENOTCONN;
+
+	sg_init_one(&out_sg, buf, buf_size);
+	cmd.opcode = VIRTIO_ADMIN_CMD_LIST_QUERY;
+	cmd.group_type = VIRTIO_ADMIN_GROUP_TYPE_SRIOV;
+	cmd.result_sg = &out_sg;
+
+	return virtio_admin_cmd_exec(virtio_dev, &cmd);
+}
+

in/out seem all wrong here. In virtio terminology, in means from device to driver, out means from driver to device.

Quoted 8 lines
drivers/vfio/pci/virtio/cmd.c new file +8 -0
+int virtiovf_cmd_list_use(struct pci_dev *pdev, u8 *buf, int buf_size)
+{
+	struct virtio_device *virtio_dev = virtio_pci_vf_get_pf_dev(pdev);
+	struct scatterlist in_sg;
+	struct virtio_admin_cmd cmd = {};
+
+	if (!virtio_dev)
+		return -ENOTCONN;

drivers/vfio/pci/virtio/cmd.c line 42

Quoted 10 lines
drivers/vfio/pci/virtio/cmd.c new file +10 -0
+
+	sg_init_one(&in_sg, buf, buf_size);
+	cmd.opcode = VIRTIO_ADMIN_CMD_LIST_USE;
+	cmd.group_type = VIRTIO_ADMIN_GROUP_TYPE_SRIOV;
+	cmd.data_sg = &in_sg;
+
+	return virtio_admin_cmd_exec(virtio_dev, &cmd);
+}
+
+int virtiovf_cmd_lr_write(struct virtiovf_pci_core_device *virtvdev, u16 opcode,

what is _lr short for?

Quoted 12 lines
drivers/vfio/pci/virtio/cmd.c new file +12 -0
+			  u8 offset, u8 size, u8 *buf)
+{
+	struct virtio_device *virtio_dev =
+		virtio_pci_vf_get_pf_dev(virtvdev->core_device.pdev);
+	struct virtio_admin_cmd_data_lr_write *in;
+	struct scatterlist in_sg;
+	struct virtio_admin_cmd cmd = {};
+	int ret;
+
+	if (!virtio_dev)
+		return -ENOTCONN;
+

drivers/vfio/pci/virtio/cmd.c line 64

Quoted 10 lines
drivers/vfio/pci/virtio/cmd.c new file +10 -0
+	in = kzalloc(sizeof(*in) + size, GFP_KERNEL);
+	if (!in)
+		return -ENOMEM;
+
+	in->offset = offset;
+	memcpy(in->registers, buf, size);
+	sg_init_one(&in_sg, in, sizeof(*in) + size);
+	cmd.opcode = opcode;
+	cmd.group_type = VIRTIO_ADMIN_GROUP_TYPE_SRIOV;
+	cmd.group_member_id = virtvdev->vf_id + 1;

weird. why + 1?

drivers/vfio/pci/virtio/cmd.c line 70

Quoted 6 lines
drivers/vfio/pci/virtio/cmd.c new file +6 -0
+	cmd.data_sg = &in_sg;
+	ret = virtio_admin_cmd_exec(virtio_dev, &cmd);
+
+	kfree(in);
+	return ret;
+}

How do you know it's safe to send this command, in particular at this time? This seems to be doing zero checks, and zero synchronization with the PF driver.

Quoted 23 lines
drivers/vfio/pci/virtio/cmd.c new file +23 -0
+
+int virtiovf_cmd_lr_read(struct virtiovf_pci_core_device *virtvdev, u16 opcode,
+			 u8 offset, u8 size, u8 *buf)
+{
+	struct virtio_device *virtio_dev =
+		virtio_pci_vf_get_pf_dev(virtvdev->core_device.pdev);
+	struct virtio_admin_cmd_data_lr_read *in;
+	struct scatterlist in_sg, out_sg;
+	struct virtio_admin_cmd cmd = {};
+	int ret;
+
+	if (!virtio_dev)
+		return -ENOTCONN;
+
+	in = kzalloc(sizeof(*in), GFP_KERNEL);
+	if (!in)
+		return -ENOMEM;
+
+	in->offset = offset;
+	sg_init_one(&in_sg, in, sizeof(*in));
+	sg_init_one(&out_sg, buf, size);
+	cmd.opcode = opcode;
+	cmd.group_type = VIRTIO_ADMIN_GROUP_TYPE_SRIOV;

drivers/vfio/pci/virtio/cmd.c line 103

Quoted 10 lines
drivers/vfio/pci/virtio/cmd.c new file +10 -0
+	cmd.data_sg = &in_sg;
+	cmd.result_sg = &out_sg;
+	cmd.group_member_id = virtvdev->vf_id + 1;
+	ret = virtio_admin_cmd_exec(virtio_dev, &cmd);
+
+	kfree(in);
+	return ret;
+}
+
+int virtiovf_cmd_lq_read_notify(struct virtiovf_pci_core_device *virtvdev,

and what is lq short for?

Quoted 78 lines
drivers/vfio/pci/virtio/cmd.c new file +43 -0
+				u8 req_bar_flags, u8 *bar, u64 *bar_offset)
+{
+	struct virtio_device *virtio_dev =
+		virtio_pci_vf_get_pf_dev(virtvdev->core_device.pdev);
+	struct virtio_admin_cmd_notify_info_result *out;
+	struct scatterlist out_sg;
+	struct virtio_admin_cmd cmd = {};
+	int ret;
+
+	if (!virtio_dev)
+		return -ENOTCONN;
+
+	out = kzalloc(sizeof(*out), GFP_KERNEL);
+	if (!out)
+		return -ENOMEM;
+
+	sg_init_one(&out_sg, out, sizeof(*out));
+	cmd.opcode = VIRTIO_ADMIN_CMD_LEGACY_NOTIFY_INFO;
+	cmd.group_type = VIRTIO_ADMIN_GROUP_TYPE_SRIOV;
+	cmd.result_sg = &out_sg;
+	cmd.group_member_id = virtvdev->vf_id + 1;
+	ret = virtio_admin_cmd_exec(virtio_dev, &cmd);
+	if (!ret) {
+		struct virtio_admin_cmd_notify_info_data *entry;
+		int i;
+
+		ret = -ENOENT;
+		for (i = 0; i < VIRTIO_ADMIN_CMD_MAX_NOTIFY_INFO; i++) {
+			entry = &out->entries[i];
+			if (entry->flags == VIRTIO_ADMIN_CMD_NOTIFY_INFO_FLAGS_END)
+				break;
+			if (entry->flags != req_bar_flags)
+				continue;
+			*bar = entry->bar;
+			*bar_offset = le64_to_cpu(entry->offset);
+			ret = 0;
+			break;
+		}
+	}
+
+	kfree(out);
+	return ret;
+}
drivers/vfio/pci/virtio/cmd.h new file +27 -0
new file mode 100644
index 000000000000..c2a3645f4b90
@@ -0,0 +1,27 @@
+// SPDX-License-Identifier: GPL-2.0 OR Linux-OpenIB
+/*
+ * Copyright (c) 2023, NVIDIA CORPORATION & AFFILIATES. All rights reserved.
+ */
+
+#ifndef VIRTIO_VFIO_CMD_H
+#define VIRTIO_VFIO_CMD_H
+
+#include <linux/kernel.h>
+#include <linux/virtio.h>
+#include <linux/vfio_pci_core.h>
+#include <linux/virtio_pci.h>
+
+struct virtiovf_pci_core_device {
+	struct vfio_pci_core_device core_device;
+	int vf_id;
+};
+
+int virtiovf_cmd_list_query(struct pci_dev *pdev, u8 *buf, int buf_size);
+int virtiovf_cmd_list_use(struct pci_dev *pdev, u8 *buf, int buf_size);
+int virtiovf_cmd_lr_write(struct virtiovf_pci_core_device *virtvdev, u16 opcode,
+			  u8 offset, u8 size, u8 *buf);
+int virtiovf_cmd_lr_read(struct virtiovf_pci_core_device *virtvdev, u16 opcode,
+			 u8 offset, u8 size, u8 *buf);
+int virtiovf_cmd_lq_read_notify(struct virtiovf_pci_core_device *virtvdev,
+				u8 req_bar_flags, u8 *bar, u64 *bar_offset);
+#endif /* VIRTIO_VFIO_CMD_H */
Signature · 1 line
2.27.0

_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization

author maintainer
#26 lore raw

Re: [PATCH vfio 10/11] vfio/virtio: Expose admin commands over virtio device

Quoted 38 lines · Michael S. Tsirkin
Quoted 37 lines · Yishai Hadas

Expose admin commands over the virtio device, to be used by the vfio-virtio driver in the next patches.

It includes: list query/use, legacy write/read, read notify_info.

---
  drivers/vfio/pci/virtio/cmd.c | 146 ++++++++++++++++++++++++++++++++++
  drivers/vfio/pci/virtio/cmd.h |  27 +++++++
  2 files changed, 173 insertions(+)

create mode 100644 drivers/vfio/pci/virtio/cmd.c create mode 100644 drivers/vfio/pci/virtio/cmd.h

drivers/vfio/pci/virtio/cmd.c new file +18 -0
new file mode 100644
index 000000000000..f068239cdbb0
@@ -0,0 +1,146 @@
+// SPDX-License-Identifier: GPL-2.0 OR Linux-OpenIB
+/*
+ * Copyright (c) 2023, NVIDIA CORPORATION & AFFILIATES. All rights reserved
+ */
+
+#include "cmd.h"
+
+int virtiovf_cmd_list_query(struct pci_dev *pdev, u8 *buf, int buf_size)
+{
+	struct virtio_device *virtio_dev = virtio_pci_vf_get_pf_dev(pdev);
+	struct scatterlist out_sg;
+	struct virtio_admin_cmd cmd = {};
+
+	if (!virtio_dev)
+		return -ENOTCONN;
+
+	sg_init_one(&out_sg, buf, buf_size);
+	cmd.opcode = VIRTIO_ADMIN_CMD_LIST_QUERY;
Quoted 8 lines

drivers/vfio/pci/virtio/cmd.c line 24

Quoted 6 lines
drivers/vfio/pci/virtio/cmd.c new file +6 -0
+	cmd.group_type = VIRTIO_ADMIN_GROUP_TYPE_SRIOV;
+	cmd.result_sg = &out_sg;
+
+	return virtio_admin_cmd_exec(virtio_dev, &cmd);
+}
+

in/out seem all wrong here. In virtio terminology, in means from device to driver, out means from driver to device.

I referred here to in/out from vfio POV who prepares the command.

However, I can replace it to follow the virtio terminology as you suggested if this more makes sense.

Please see also my coming answer on your suggestion to put all of this in the virtio layer.

Quoted 12 lines
Quoted 12 lines
drivers/vfio/pci/virtio/cmd.c new file +12 -0
+int virtiovf_cmd_list_use(struct pci_dev *pdev, u8 *buf, int buf_size)
+{
+	struct virtio_device *virtio_dev = virtio_pci_vf_get_pf_dev(pdev);
+	struct scatterlist in_sg;
+	struct virtio_admin_cmd cmd = {};
+
+	if (!virtio_dev)
+		return -ENOTCONN;
+
+	sg_init_one(&in_sg, buf, buf_size);
+	cmd.opcode = VIRTIO_ADMIN_CMD_LIST_USE;
+	cmd.group_type = VIRTIO_ADMIN_GROUP_TYPE_SRIOV;
Quoted 8 lines

drivers/vfio/pci/virtio/cmd.c line 42

Quoted 6 lines
drivers/vfio/pci/virtio/cmd.c new file +6 -0
+	cmd.data_sg = &in_sg;
+
+	return virtio_admin_cmd_exec(virtio_dev, &cmd);
+}
+
+int virtiovf_cmd_lr_write(struct virtiovf_pci_core_device *virtvdev, u16 opcode,

what is _lr short for?

This was an acronym to legacy_read.

The actual command is according to the given opcode which can be one among LEGACY_COMMON_CFG_READ, LEGACY_DEV_CFG_READ.

I can rename it to '_legacy_read' (i.e. virtiovf_issue_legacy_read_cmd) to be clearer.

Quoted 16 lines
Quoted 16 lines
drivers/vfio/pci/virtio/cmd.c new file +15 -0
+			  u8 offset, u8 size, u8 *buf)
+{
+	struct virtio_device *virtio_dev =
+		virtio_pci_vf_get_pf_dev(virtvdev->core_device.pdev);
+	struct virtio_admin_cmd_data_lr_write *in;
+	struct scatterlist in_sg;
+	struct virtio_admin_cmd cmd = {};
+	int ret;
+
+	if (!virtio_dev)
+		return -ENOTCONN;
+
+	in = kzalloc(sizeof(*in) + size, GFP_KERNEL);
+	if (!in)
+		return -ENOMEM;
Quoted 8 lines

drivers/vfio/pci/virtio/cmd.c line 64

Quoted 7 lines
drivers/vfio/pci/virtio/cmd.c new file +7 -0
+
+	in->offset = offset;
+	memcpy(in->registers, buf, size);
+	sg_init_one(&in_sg, in, sizeof(*in) + size);
+	cmd.opcode = opcode;
+	cmd.group_type = VIRTIO_ADMIN_GROUP_TYPE_SRIOV;
+	cmd.group_member_id = virtvdev->vf_id + 1;

weird. why + 1?

This follows the virtio spec in that area.

"When sending commands with the SR-IOV group type, the driver specify a value for group_member_id between 1 and NumVFs inclusive."

The 'virtvdev->vf_id' was set upon vfio/virtio driver initialization by pci_iov_vf_id() which its first index is 0.

Quoted 10 lines

drivers/vfio/pci/virtio/cmd.c line 70

Quoted 6 lines
drivers/vfio/pci/virtio/cmd.c new file +6 -0
+	cmd.data_sg = &in_sg;
+	ret = virtio_admin_cmd_exec(virtio_dev, &cmd);
+
+	kfree(in);
+	return ret;
+}

How do you know it's safe to send this command, in particular at this time? This seems to be doing zero checks, and zero synchronization with the PF driver.

The virtiovf_cmd_lr_read()/other gets a virtio VF and it gets its PF by calling virtio_pci_vf_get_pf_dev().

The VF can't gone by 'disable sriov' as it's owned/used by vfio.

The PF can't gone by rmmod/modprobe -r of virtio, as of the 'module in use'/dependencies between VFIO to VIRTIO.

The below check [1] was done only from a clean code perspective, which might theoretically fail in case the given VF doesn't use a virtio driver.

[1] if (!virtio_dev)
        return -ENOTCONN;

So, it looks safe as is.

Quoted 26 lines
Quoted 26 lines
drivers/vfio/pci/virtio/cmd.c new file +26 -0
+
+int virtiovf_cmd_lr_read(struct virtiovf_pci_core_device *virtvdev, u16 opcode,
+			 u8 offset, u8 size, u8 *buf)
+{
+	struct virtio_device *virtio_dev =
+		virtio_pci_vf_get_pf_dev(virtvdev->core_device.pdev);
+	struct virtio_admin_cmd_data_lr_read *in;
+	struct scatterlist in_sg, out_sg;
+	struct virtio_admin_cmd cmd = {};
+	int ret;
+
+	if (!virtio_dev)
+		return -ENOTCONN;
+
+	in = kzalloc(sizeof(*in), GFP_KERNEL);
+	if (!in)
+		return -ENOMEM;
+
+	in->offset = offset;
+	sg_init_one(&in_sg, in, sizeof(*in));
+	sg_init_one(&out_sg, buf, size);
+	cmd.opcode = opcode;
+	cmd.group_type = VIRTIO_ADMIN_GROUP_TYPE_SRIOV;
+	cmd.data_sg = &in_sg;
+	cmd.result_sg = &out_sg;
+	cmd.group_member_id = virtvdev->vf_id + 1;
Quoted 8 lines

drivers/vfio/pci/virtio/cmd.c line 103

Quoted 7 lines
drivers/vfio/pci/virtio/cmd.c new file +7 -0
+	ret = virtio_admin_cmd_exec(virtio_dev, &cmd);
+
+	kfree(in);
+	return ret;
+}
+
+int virtiovf_cmd_lq_read_notify(struct virtiovf_pci_core_device *virtvdev,

and what is lq short for?

To be more explicit, I may replace to virtiovf_cmd_legacy_notify_info() to follow the spec opcode.

Yishai

Quoted 79 lines
Quoted 79 lines
drivers/vfio/pci/virtio/cmd.c new file +43 -0
+				u8 req_bar_flags, u8 *bar, u64 *bar_offset)
+{
+	struct virtio_device *virtio_dev =
+		virtio_pci_vf_get_pf_dev(virtvdev->core_device.pdev);
+	struct virtio_admin_cmd_notify_info_result *out;
+	struct scatterlist out_sg;
+	struct virtio_admin_cmd cmd = {};
+	int ret;
+
+	if (!virtio_dev)
+		return -ENOTCONN;
+
+	out = kzalloc(sizeof(*out), GFP_KERNEL);
+	if (!out)
+		return -ENOMEM;
+
+	sg_init_one(&out_sg, out, sizeof(*out));
+	cmd.opcode = VIRTIO_ADMIN_CMD_LEGACY_NOTIFY_INFO;
+	cmd.group_type = VIRTIO_ADMIN_GROUP_TYPE_SRIOV;
+	cmd.result_sg = &out_sg;
+	cmd.group_member_id = virtvdev->vf_id + 1;
+	ret = virtio_admin_cmd_exec(virtio_dev, &cmd);
+	if (!ret) {
+		struct virtio_admin_cmd_notify_info_data *entry;
+		int i;
+
+		ret = -ENOENT;
+		for (i = 0; i < VIRTIO_ADMIN_CMD_MAX_NOTIFY_INFO; i++) {
+			entry = &out->entries[i];
+			if (entry->flags == VIRTIO_ADMIN_CMD_NOTIFY_INFO_FLAGS_END)
+				break;
+			if (entry->flags != req_bar_flags)
+				continue;
+			*bar = entry->bar;
+			*bar_offset = le64_to_cpu(entry->offset);
+			ret = 0;
+			break;
+		}
+	}
+
+	kfree(out);
+	return ret;
+}
drivers/vfio/pci/virtio/cmd.h new file +27 -0
new file mode 100644
index 000000000000..c2a3645f4b90
@@ -0,0 +1,27 @@
+// SPDX-License-Identifier: GPL-2.0 OR Linux-OpenIB
+/*
+ * Copyright (c) 2023, NVIDIA CORPORATION & AFFILIATES. All rights reserved.
+ */
+
+#ifndef VIRTIO_VFIO_CMD_H
+#define VIRTIO_VFIO_CMD_H
+
+#include <linux/kernel.h>
+#include <linux/virtio.h>
+#include <linux/vfio_pci_core.h>
+#include <linux/virtio_pci.h>
+
+struct virtiovf_pci_core_device {
+	struct vfio_pci_core_device core_device;
+	int vf_id;
+};
+
+int virtiovf_cmd_list_query(struct pci_dev *pdev, u8 *buf, int buf_size);
+int virtiovf_cmd_list_use(struct pci_dev *pdev, u8 *buf, int buf_size);
+int virtiovf_cmd_lr_write(struct virtiovf_pci_core_device *virtvdev, u16 opcode,
+			  u8 offset, u8 size, u8 *buf);
+int virtiovf_cmd_lr_read(struct virtiovf_pci_core_device *virtvdev, u16 opcode,
+			 u8 offset, u8 size, u8 *buf);
+int virtiovf_cmd_lq_read_notify(struct virtiovf_pci_core_device *virtvdev,
+				u8 req_bar_flags, u8 *bar, u64 *bar_offset);
+#endif /* VIRTIO_VFIO_CMD_H */
Signature · 1 line
2.27.0
#27 lore raw

Re: [PATCH vfio 10/11] vfio/virtio: Expose admin commands over virtio device

Quoted 109 lines · Yishai Hadas
Quoted 38 lines · Michael S. Tsirkin
Quoted 37 lines · Yishai Hadas

Expose admin commands over the virtio device, to be used by the vfio-virtio driver in the next patches.

It includes: list query/use, legacy write/read, read notify_info.

---
  drivers/vfio/pci/virtio/cmd.c | 146 ++++++++++++++++++++++++++++++++++
  drivers/vfio/pci/virtio/cmd.h |  27 +++++++
  2 files changed, 173 insertions(+)

create mode 100644 drivers/vfio/pci/virtio/cmd.c create mode 100644 drivers/vfio/pci/virtio/cmd.h

drivers/vfio/pci/virtio/cmd.c new file +18 -0
new file mode 100644
index 000000000000..f068239cdbb0
@@ -0,0 +1,146 @@
+// SPDX-License-Identifier: GPL-2.0 OR Linux-OpenIB
+/*
+ * Copyright (c) 2023, NVIDIA CORPORATION & AFFILIATES. All rights reserved
+ */
+
+#include "cmd.h"
+
+int virtiovf_cmd_list_query(struct pci_dev *pdev, u8 *buf, int buf_size)
+{
+	struct virtio_device *virtio_dev = virtio_pci_vf_get_pf_dev(pdev);
+	struct scatterlist out_sg;
+	struct virtio_admin_cmd cmd = {};
+
+	if (!virtio_dev)
+		return -ENOTCONN;
+
+	sg_init_one(&out_sg, buf, buf_size);
+	cmd.opcode = VIRTIO_ADMIN_CMD_LIST_QUERY;
Quoted 8 lines

drivers/vfio/pci/virtio/cmd.c line 24

Quoted 6 lines
drivers/vfio/pci/virtio/cmd.c new file +6 -0
+	cmd.group_type = VIRTIO_ADMIN_GROUP_TYPE_SRIOV;
+	cmd.result_sg = &out_sg;
+
+	return virtio_admin_cmd_exec(virtio_dev, &cmd);
+}
+

in/out seem all wrong here. In virtio terminology, in means from device to driver, out means from driver to device.

I referred here to in/out from vfio POV who prepares the command.

However, I can replace it to follow the virtio terminology as you suggested if this more makes sense.

Please see also my coming answer on your suggestion to put all of this in the virtio layer.

Quoted 12 lines
Quoted 12 lines
drivers/vfio/pci/virtio/cmd.c new file +12 -0
+int virtiovf_cmd_list_use(struct pci_dev *pdev, u8 *buf, int buf_size)
+{
+	struct virtio_device *virtio_dev = virtio_pci_vf_get_pf_dev(pdev);
+	struct scatterlist in_sg;
+	struct virtio_admin_cmd cmd = {};
+
+	if (!virtio_dev)
+		return -ENOTCONN;
+
+	sg_init_one(&in_sg, buf, buf_size);
+	cmd.opcode = VIRTIO_ADMIN_CMD_LIST_USE;
+	cmd.group_type = VIRTIO_ADMIN_GROUP_TYPE_SRIOV;
Quoted 8 lines

drivers/vfio/pci/virtio/cmd.c line 42

Quoted 6 lines
drivers/vfio/pci/virtio/cmd.c new file +6 -0
+	cmd.data_sg = &in_sg;
+
+	return virtio_admin_cmd_exec(virtio_dev, &cmd);
+}
+
+int virtiovf_cmd_lr_write(struct virtiovf_pci_core_device *virtvdev, u16 opcode,

what is _lr short for?

This was an acronym to legacy_read.

The actual command is according to the given opcode which can be one among LEGACY_COMMON_CFG_READ, LEGACY_DEV_CFG_READ.

I can rename it to '_legacy_read' (i.e. virtiovf_issue_legacy_read_cmd) to be clearer.

Quoted 16 lines
Quoted 16 lines
drivers/vfio/pci/virtio/cmd.c new file +15 -0
+			  u8 offset, u8 size, u8 *buf)
+{
+	struct virtio_device *virtio_dev =
+		virtio_pci_vf_get_pf_dev(virtvdev->core_device.pdev);
+	struct virtio_admin_cmd_data_lr_write *in;
+	struct scatterlist in_sg;
+	struct virtio_admin_cmd cmd = {};
+	int ret;
+
+	if (!virtio_dev)
+		return -ENOTCONN;
+
+	in = kzalloc(sizeof(*in) + size, GFP_KERNEL);
+	if (!in)
+		return -ENOMEM;
Quoted 8 lines

drivers/vfio/pci/virtio/cmd.c line 64

Quoted 7 lines
drivers/vfio/pci/virtio/cmd.c new file +7 -0
+
+	in->offset = offset;
+	memcpy(in->registers, buf, size);
+	sg_init_one(&in_sg, in, sizeof(*in) + size);
+	cmd.opcode = opcode;
+	cmd.group_type = VIRTIO_ADMIN_GROUP_TYPE_SRIOV;
+	cmd.group_member_id = virtvdev->vf_id + 1;

weird. why + 1?

Quoted 5 lines

This follows the virtio spec in that area.

"When sending commands with the SR-IOV group type, the driver specify a value for group_member_id
between 1 and NumVFs inclusive."

Ah, I get it. Pls add a comment.

Quoted 21 lines

The 'virtvdev->vf_id' was set upon vfio/virtio driver initialization by pci_iov_vf_id() which its first index is 0.

Quoted 10 lines

drivers/vfio/pci/virtio/cmd.c line 70

Quoted 6 lines
drivers/vfio/pci/virtio/cmd.c new file +6 -0
+	cmd.data_sg = &in_sg;
+	ret = virtio_admin_cmd_exec(virtio_dev, &cmd);
+
+	kfree(in);
+	return ret;
+}

How do you know it's safe to send this command, in particular at this time? This seems to be doing zero checks, and zero synchronization with the PF driver.

The virtiovf_cmd_lr_read()/other gets a virtio VF and it gets its PF by calling virtio_pci_vf_get_pf_dev().

The VF can't gone by 'disable sriov' as it's owned/used by vfio.

The PF can't gone by rmmod/modprobe -r of virtio, as of the 'module in use'/dependencies between VFIO to VIRTIO.

Quoted 7 lines

The below check [1] was done only from a clean code perspective, which might theoretically fail in case the given VF doesn't use a virtio driver.

[1] if (!virtio_dev)
        return -ENOTCONN;

So, it looks safe as is.

Can the device can be unbound from module right after you did the check? What about suspend - can this be called while suspend is in progress?

More importantly, virtio can decide to reset the device for its
own internal reasons (e.g. to recover from an error).
We used to do it when attaching XDP, and we can start doing it again. That's one of the reasons why I want all this code under virtio, so we'll remember.

Quoted 111 lines
Quoted 26 lines
Quoted 26 lines
drivers/vfio/pci/virtio/cmd.c new file +26 -0
+
+int virtiovf_cmd_lr_read(struct virtiovf_pci_core_device *virtvdev, u16 opcode,
+			 u8 offset, u8 size, u8 *buf)
+{
+	struct virtio_device *virtio_dev =
+		virtio_pci_vf_get_pf_dev(virtvdev->core_device.pdev);
+	struct virtio_admin_cmd_data_lr_read *in;
+	struct scatterlist in_sg, out_sg;
+	struct virtio_admin_cmd cmd = {};
+	int ret;
+
+	if (!virtio_dev)
+		return -ENOTCONN;
+
+	in = kzalloc(sizeof(*in), GFP_KERNEL);
+	if (!in)
+		return -ENOMEM;
+
+	in->offset = offset;
+	sg_init_one(&in_sg, in, sizeof(*in));
+	sg_init_one(&out_sg, buf, size);
+	cmd.opcode = opcode;
+	cmd.group_type = VIRTIO_ADMIN_GROUP_TYPE_SRIOV;
+	cmd.data_sg = &in_sg;
+	cmd.result_sg = &out_sg;
+	cmd.group_member_id = virtvdev->vf_id + 1;
Quoted 8 lines

drivers/vfio/pci/virtio/cmd.c line 103

Quoted 7 lines
drivers/vfio/pci/virtio/cmd.c new file +7 -0
+	ret = virtio_admin_cmd_exec(virtio_dev, &cmd);
+
+	kfree(in);
+	return ret;
+}
+
+int virtiovf_cmd_lq_read_notify(struct virtiovf_pci_core_device *virtvdev,

and what is lq short for?

To be more explicit, I may replace to virtiovf_cmd_legacy_notify_info() to follow the spec opcode.

Yishai

Quoted 71 lines
Quoted 71 lines
drivers/vfio/pci/virtio/cmd.c new file +43 -0
+				u8 req_bar_flags, u8 *bar, u64 *bar_offset)
+{
+	struct virtio_device *virtio_dev =
+		virtio_pci_vf_get_pf_dev(virtvdev->core_device.pdev);
+	struct virtio_admin_cmd_notify_info_result *out;
+	struct scatterlist out_sg;
+	struct virtio_admin_cmd cmd = {};
+	int ret;
+
+	if (!virtio_dev)
+		return -ENOTCONN;
+
+	out = kzalloc(sizeof(*out), GFP_KERNEL);
+	if (!out)
+		return -ENOMEM;
+
+	sg_init_one(&out_sg, out, sizeof(*out));
+	cmd.opcode = VIRTIO_ADMIN_CMD_LEGACY_NOTIFY_INFO;
+	cmd.group_type = VIRTIO_ADMIN_GROUP_TYPE_SRIOV;
+	cmd.result_sg = &out_sg;
+	cmd.group_member_id = virtvdev->vf_id + 1;
+	ret = virtio_admin_cmd_exec(virtio_dev, &cmd);
+	if (!ret) {
+		struct virtio_admin_cmd_notify_info_data *entry;
+		int i;
+
+		ret = -ENOENT;
+		for (i = 0; i < VIRTIO_ADMIN_CMD_MAX_NOTIFY_INFO; i++) {
+			entry = &out->entries[i];
+			if (entry->flags == VIRTIO_ADMIN_CMD_NOTIFY_INFO_FLAGS_END)
+				break;
+			if (entry->flags != req_bar_flags)
+				continue;
+			*bar = entry->bar;
+			*bar_offset = le64_to_cpu(entry->offset);
+			ret = 0;
+			break;
+		}
+	}
+
+	kfree(out);
+	return ret;
+}
drivers/vfio/pci/virtio/cmd.h new file +21 -0
new file mode 100644
index 000000000000..c2a3645f4b90
@@ -0,0 +1,27 @@
+// SPDX-License-Identifier: GPL-2.0 OR Linux-OpenIB
+/*
+ * Copyright (c) 2023, NVIDIA CORPORATION & AFFILIATES. All rights reserved.
+ */
+
+#ifndef VIRTIO_VFIO_CMD_H
+#define VIRTIO_VFIO_CMD_H
+
+#include <linux/kernel.h>
+#include <linux/virtio.h>
+#include <linux/vfio_pci_core.h>
+#include <linux/virtio_pci.h>
+
+struct virtiovf_pci_core_device {
+	struct vfio_pci_core_device core_device;
+	int vf_id;
+};
+
+int virtiovf_cmd_list_query(struct pci_dev *pdev, u8 *buf, int buf_size);
+int virtiovf_cmd_list_use(struct pci_dev *pdev, u8 *buf, int buf_size);
+int virtiovf_cmd_lr_write(struct virtiovf_pci_core_device *virtvdev, u16 opcode,
Quoted 9 lines
Quoted 8 lines
Quoted 8 lines
drivers/vfio/pci/virtio/cmd.h new file +6 -0
+			  u8 offset, u8 size, u8 *buf);
+int virtiovf_cmd_lr_read(struct virtiovf_pci_core_device *virtvdev, u16 opcode,
+			 u8 offset, u8 size, u8 *buf);
+int virtiovf_cmd_lq_read_notify(struct virtiovf_pci_core_device *virtvdev,
+				u8 req_bar_flags, u8 *bar, u64 *bar_offset);
+#endif /* VIRTIO_VFIO_CMD_H */
Signature · 1 line
2.27.0

_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization

#28 lore raw

Re: [PATCH vfio 10/11] vfio/virtio: Expose admin commands over virtio device

In reply to Yishai Hadas
Quoted 6 lines · Yishai Hadas

Expose admin commands over the virtio device, to be used by the vfio-virtio driver in the next patches.

It includes: list query/use, legacy write/read, read notify_info.

This stuff is pure virtio spec. I think it should live under drivers/virtio, too.

Quoted 194 lines
---
 drivers/vfio/pci/virtio/cmd.c | 146 ++++++++++++++++++++++++++++++++++
 drivers/vfio/pci/virtio/cmd.h |  27 +++++++
 2 files changed, 173 insertions(+)
 create mode 100644 drivers/vfio/pci/virtio/cmd.c
 create mode 100644 drivers/vfio/pci/virtio/cmd.h
drivers/vfio/pci/virtio/cmd.c new file +146 -0
new file mode 100644
index 000000000000..f068239cdbb0
@@ -0,0 +1,146 @@
+// SPDX-License-Identifier: GPL-2.0 OR Linux-OpenIB
+/*
+ * Copyright (c) 2023, NVIDIA CORPORATION & AFFILIATES. All rights reserved
+ */
+
+#include "cmd.h"
+
+int virtiovf_cmd_list_query(struct pci_dev *pdev, u8 *buf, int buf_size)
+{
+	struct virtio_device *virtio_dev = virtio_pci_vf_get_pf_dev(pdev);
+	struct scatterlist out_sg;
+	struct virtio_admin_cmd cmd = {};
+
+	if (!virtio_dev)
+		return -ENOTCONN;
+
+	sg_init_one(&out_sg, buf, buf_size);
+	cmd.opcode = VIRTIO_ADMIN_CMD_LIST_QUERY;
+	cmd.group_type = VIRTIO_ADMIN_GROUP_TYPE_SRIOV;
+	cmd.result_sg = &out_sg;
+
+	return virtio_admin_cmd_exec(virtio_dev, &cmd);
+}
+
+int virtiovf_cmd_list_use(struct pci_dev *pdev, u8 *buf, int buf_size)
+{
+	struct virtio_device *virtio_dev = virtio_pci_vf_get_pf_dev(pdev);
+	struct scatterlist in_sg;
+	struct virtio_admin_cmd cmd = {};
+
+	if (!virtio_dev)
+		return -ENOTCONN;
+
+	sg_init_one(&in_sg, buf, buf_size);
+	cmd.opcode = VIRTIO_ADMIN_CMD_LIST_USE;
+	cmd.group_type = VIRTIO_ADMIN_GROUP_TYPE_SRIOV;
+	cmd.data_sg = &in_sg;
+
+	return virtio_admin_cmd_exec(virtio_dev, &cmd);
+}
+
+int virtiovf_cmd_lr_write(struct virtiovf_pci_core_device *virtvdev, u16 opcode,
+			  u8 offset, u8 size, u8 *buf)
+{
+	struct virtio_device *virtio_dev =
+		virtio_pci_vf_get_pf_dev(virtvdev->core_device.pdev);
+	struct virtio_admin_cmd_data_lr_write *in;
+	struct scatterlist in_sg;
+	struct virtio_admin_cmd cmd = {};
+	int ret;
+
+	if (!virtio_dev)
+		return -ENOTCONN;
+
+	in = kzalloc(sizeof(*in) + size, GFP_KERNEL);
+	if (!in)
+		return -ENOMEM;
+
+	in->offset = offset;
+	memcpy(in->registers, buf, size);
+	sg_init_one(&in_sg, in, sizeof(*in) + size);
+	cmd.opcode = opcode;
+	cmd.group_type = VIRTIO_ADMIN_GROUP_TYPE_SRIOV;
+	cmd.group_member_id = virtvdev->vf_id + 1;
+	cmd.data_sg = &in_sg;
+	ret = virtio_admin_cmd_exec(virtio_dev, &cmd);
+
+	kfree(in);
+	return ret;
+}
+
+int virtiovf_cmd_lr_read(struct virtiovf_pci_core_device *virtvdev, u16 opcode,
+			 u8 offset, u8 size, u8 *buf)
+{
+	struct virtio_device *virtio_dev =
+		virtio_pci_vf_get_pf_dev(virtvdev->core_device.pdev);
+	struct virtio_admin_cmd_data_lr_read *in;
+	struct scatterlist in_sg, out_sg;
+	struct virtio_admin_cmd cmd = {};
+	int ret;
+
+	if (!virtio_dev)
+		return -ENOTCONN;
+
+	in = kzalloc(sizeof(*in), GFP_KERNEL);
+	if (!in)
+		return -ENOMEM;
+
+	in->offset = offset;
+	sg_init_one(&in_sg, in, sizeof(*in));
+	sg_init_one(&out_sg, buf, size);
+	cmd.opcode = opcode;
+	cmd.group_type = VIRTIO_ADMIN_GROUP_TYPE_SRIOV;
+	cmd.data_sg = &in_sg;
+	cmd.result_sg = &out_sg;
+	cmd.group_member_id = virtvdev->vf_id + 1;
+	ret = virtio_admin_cmd_exec(virtio_dev, &cmd);
+
+	kfree(in);
+	return ret;
+}
+
+int virtiovf_cmd_lq_read_notify(struct virtiovf_pci_core_device *virtvdev,
+				u8 req_bar_flags, u8 *bar, u64 *bar_offset)
+{
+	struct virtio_device *virtio_dev =
+		virtio_pci_vf_get_pf_dev(virtvdev->core_device.pdev);
+	struct virtio_admin_cmd_notify_info_result *out;
+	struct scatterlist out_sg;
+	struct virtio_admin_cmd cmd = {};
+	int ret;
+
+	if (!virtio_dev)
+		return -ENOTCONN;
+
+	out = kzalloc(sizeof(*out), GFP_KERNEL);
+	if (!out)
+		return -ENOMEM;
+
+	sg_init_one(&out_sg, out, sizeof(*out));
+	cmd.opcode = VIRTIO_ADMIN_CMD_LEGACY_NOTIFY_INFO;
+	cmd.group_type = VIRTIO_ADMIN_GROUP_TYPE_SRIOV;
+	cmd.result_sg = &out_sg;
+	cmd.group_member_id = virtvdev->vf_id + 1;
+	ret = virtio_admin_cmd_exec(virtio_dev, &cmd);
+	if (!ret) {
+		struct virtio_admin_cmd_notify_info_data *entry;
+		int i;
+
+		ret = -ENOENT;
+		for (i = 0; i < VIRTIO_ADMIN_CMD_MAX_NOTIFY_INFO; i++) {
+			entry = &out->entries[i];
+			if (entry->flags == VIRTIO_ADMIN_CMD_NOTIFY_INFO_FLAGS_END)
+				break;
+			if (entry->flags != req_bar_flags)
+				continue;
+			*bar = entry->bar;
+			*bar_offset = le64_to_cpu(entry->offset);
+			ret = 0;
+			break;
+		}
+	}
+
+	kfree(out);
+	return ret;
+}
drivers/vfio/pci/virtio/cmd.h new file +27 -0
new file mode 100644
index 000000000000..c2a3645f4b90
@@ -0,0 +1,27 @@
+// SPDX-License-Identifier: GPL-2.0 OR Linux-OpenIB
+/*
+ * Copyright (c) 2023, NVIDIA CORPORATION & AFFILIATES. All rights reserved.
+ */
+
+#ifndef VIRTIO_VFIO_CMD_H
+#define VIRTIO_VFIO_CMD_H
+
+#include <linux/kernel.h>
+#include <linux/virtio.h>
+#include <linux/vfio_pci_core.h>
+#include <linux/virtio_pci.h>
+
+struct virtiovf_pci_core_device {
+	struct vfio_pci_core_device core_device;
+	int vf_id;
+};
+
+int virtiovf_cmd_list_query(struct pci_dev *pdev, u8 *buf, int buf_size);
+int virtiovf_cmd_list_use(struct pci_dev *pdev, u8 *buf, int buf_size);
+int virtiovf_cmd_lr_write(struct virtiovf_pci_core_device *virtvdev, u16 opcode,
+			  u8 offset, u8 size, u8 *buf);
+int virtiovf_cmd_lr_read(struct virtiovf_pci_core_device *virtvdev, u16 opcode,
+			 u8 offset, u8 size, u8 *buf);
+int virtiovf_cmd_lq_read_notify(struct virtiovf_pci_core_device *virtvdev,
+				u8 req_bar_flags, u8 *bar, u64 *bar_offset);
+#endif /* VIRTIO_VFIO_CMD_H */
Signature · 1 line
2.27.0

_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization

author maintainer
#29 lore raw

Re: [PATCH vfio 10/11] vfio/virtio: Expose admin commands over virtio device

Quoted 10 lines · Michael S. Tsirkin
Quoted 6 lines · Yishai Hadas

Expose admin commands over the virtio device, to be used by the vfio-virtio driver in the next patches.

It includes: list query/use, legacy write/read, read notify_info.

This stuff is pure virtio spec. I think it should live under drivers/virtio, too.

The motivation to put it in the vfio layer was from the below main reasons:

1) Having it inside virtio may require to export a symbol/function per command.

   This will end up today by 5 and in the future (e.g. live migration) with much more exported symbols.

   With current code we export only 2 generic symbols virtio_pci_vf_get_pf_dev(), virtio_admin_cmd_exec() which may fit for any further extension.

2) For now there is no logic in this vfio layer, however, in the future we may have some DMA/other logic that should better fit to the caller/client layer (i.e. vfio).

By the way, this follows what was done already between vfio/mlx5 to mlx5_core modules where mlx5_core exposes generic APIs to execute a command and to get the a PF from a given mlx5 VF.

This way, we can enable further commands to be added/extended easily/cleanly.

See for example here [1, 2].

[1] https://elixir.bootlin.com/linux/v6.6-rc3/source/drivers/vfio/pci/mlx5/cmd.c#L210

[2] https://elixir.bootlin.com/linux/v6.6-rc3/source/drivers/vfio/pci/mlx5/cmd.c#L683

Yishai

Quoted 195 lines
Quoted 195 lines
---
  drivers/vfio/pci/virtio/cmd.c | 146 ++++++++++++++++++++++++++++++++++
  drivers/vfio/pci/virtio/cmd.h |  27 +++++++
  2 files changed, 173 insertions(+)

create mode 100644 drivers/vfio/pci/virtio/cmd.c create mode 100644 drivers/vfio/pci/virtio/cmd.h

drivers/vfio/pci/virtio/cmd.c new file +146 -0
new file mode 100644
index 000000000000..f068239cdbb0
@@ -0,0 +1,146 @@
+// SPDX-License-Identifier: GPL-2.0 OR Linux-OpenIB
+/*
+ * Copyright (c) 2023, NVIDIA CORPORATION & AFFILIATES. All rights reserved
+ */
+
+#include "cmd.h"
+
+int virtiovf_cmd_list_query(struct pci_dev *pdev, u8 *buf, int buf_size)
+{
+	struct virtio_device *virtio_dev = virtio_pci_vf_get_pf_dev(pdev);
+	struct scatterlist out_sg;
+	struct virtio_admin_cmd cmd = {};
+
+	if (!virtio_dev)
+		return -ENOTCONN;
+
+	sg_init_one(&out_sg, buf, buf_size);
+	cmd.opcode = VIRTIO_ADMIN_CMD_LIST_QUERY;
+	cmd.group_type = VIRTIO_ADMIN_GROUP_TYPE_SRIOV;
+	cmd.result_sg = &out_sg;
+
+	return virtio_admin_cmd_exec(virtio_dev, &cmd);
+}
+
+int virtiovf_cmd_list_use(struct pci_dev *pdev, u8 *buf, int buf_size)
+{
+	struct virtio_device *virtio_dev = virtio_pci_vf_get_pf_dev(pdev);
+	struct scatterlist in_sg;
+	struct virtio_admin_cmd cmd = {};
+
+	if (!virtio_dev)
+		return -ENOTCONN;
+
+	sg_init_one(&in_sg, buf, buf_size);
+	cmd.opcode = VIRTIO_ADMIN_CMD_LIST_USE;
+	cmd.group_type = VIRTIO_ADMIN_GROUP_TYPE_SRIOV;
+	cmd.data_sg = &in_sg;
+
+	return virtio_admin_cmd_exec(virtio_dev, &cmd);
+}
+
+int virtiovf_cmd_lr_write(struct virtiovf_pci_core_device *virtvdev, u16 opcode,
+			  u8 offset, u8 size, u8 *buf)
+{
+	struct virtio_device *virtio_dev =
+		virtio_pci_vf_get_pf_dev(virtvdev->core_device.pdev);
+	struct virtio_admin_cmd_data_lr_write *in;
+	struct scatterlist in_sg;
+	struct virtio_admin_cmd cmd = {};
+	int ret;
+
+	if (!virtio_dev)
+		return -ENOTCONN;
+
+	in = kzalloc(sizeof(*in) + size, GFP_KERNEL);
+	if (!in)
+		return -ENOMEM;
+
+	in->offset = offset;
+	memcpy(in->registers, buf, size);
+	sg_init_one(&in_sg, in, sizeof(*in) + size);
+	cmd.opcode = opcode;
+	cmd.group_type = VIRTIO_ADMIN_GROUP_TYPE_SRIOV;
+	cmd.group_member_id = virtvdev->vf_id + 1;
+	cmd.data_sg = &in_sg;
+	ret = virtio_admin_cmd_exec(virtio_dev, &cmd);
+
+	kfree(in);
+	return ret;
+}
+
+int virtiovf_cmd_lr_read(struct virtiovf_pci_core_device *virtvdev, u16 opcode,
+			 u8 offset, u8 size, u8 *buf)
+{
+	struct virtio_device *virtio_dev =
+		virtio_pci_vf_get_pf_dev(virtvdev->core_device.pdev);
+	struct virtio_admin_cmd_data_lr_read *in;
+	struct scatterlist in_sg, out_sg;
+	struct virtio_admin_cmd cmd = {};
+	int ret;
+
+	if (!virtio_dev)
+		return -ENOTCONN;
+
+	in = kzalloc(sizeof(*in), GFP_KERNEL);
+	if (!in)
+		return -ENOMEM;
+
+	in->offset = offset;
+	sg_init_one(&in_sg, in, sizeof(*in));
+	sg_init_one(&out_sg, buf, size);
+	cmd.opcode = opcode;
+	cmd.group_type = VIRTIO_ADMIN_GROUP_TYPE_SRIOV;
+	cmd.data_sg = &in_sg;
+	cmd.result_sg = &out_sg;
+	cmd.group_member_id = virtvdev->vf_id + 1;
+	ret = virtio_admin_cmd_exec(virtio_dev, &cmd);
+
+	kfree(in);
+	return ret;
+}
+
+int virtiovf_cmd_lq_read_notify(struct virtiovf_pci_core_device *virtvdev,
+				u8 req_bar_flags, u8 *bar, u64 *bar_offset)
+{
+	struct virtio_device *virtio_dev =
+		virtio_pci_vf_get_pf_dev(virtvdev->core_device.pdev);
+	struct virtio_admin_cmd_notify_info_result *out;
+	struct scatterlist out_sg;
+	struct virtio_admin_cmd cmd = {};
+	int ret;
+
+	if (!virtio_dev)
+		return -ENOTCONN;
+
+	out = kzalloc(sizeof(*out), GFP_KERNEL);
+	if (!out)
+		return -ENOMEM;
+
+	sg_init_one(&out_sg, out, sizeof(*out));
+	cmd.opcode = VIRTIO_ADMIN_CMD_LEGACY_NOTIFY_INFO;
+	cmd.group_type = VIRTIO_ADMIN_GROUP_TYPE_SRIOV;
+	cmd.result_sg = &out_sg;
+	cmd.group_member_id = virtvdev->vf_id + 1;
+	ret = virtio_admin_cmd_exec(virtio_dev, &cmd);
+	if (!ret) {
+		struct virtio_admin_cmd_notify_info_data *entry;
+		int i;
+
+		ret = -ENOENT;
+		for (i = 0; i < VIRTIO_ADMIN_CMD_MAX_NOTIFY_INFO; i++) {
+			entry = &out->entries[i];
+			if (entry->flags == VIRTIO_ADMIN_CMD_NOTIFY_INFO_FLAGS_END)
+				break;
+			if (entry->flags != req_bar_flags)
+				continue;
+			*bar = entry->bar;
+			*bar_offset = le64_to_cpu(entry->offset);
+			ret = 0;
+			break;
+		}
+	}
+
+	kfree(out);
+	return ret;
+}
drivers/vfio/pci/virtio/cmd.h new file +27 -0
new file mode 100644
index 000000000000..c2a3645f4b90
@@ -0,0 +1,27 @@
+// SPDX-License-Identifier: GPL-2.0 OR Linux-OpenIB
+/*
+ * Copyright (c) 2023, NVIDIA CORPORATION & AFFILIATES. All rights reserved.
+ */
+
+#ifndef VIRTIO_VFIO_CMD_H
+#define VIRTIO_VFIO_CMD_H
+
+#include <linux/kernel.h>
+#include <linux/virtio.h>
+#include <linux/vfio_pci_core.h>
+#include <linux/virtio_pci.h>
+
+struct virtiovf_pci_core_device {
+	struct vfio_pci_core_device core_device;
+	int vf_id;
+};
+
+int virtiovf_cmd_list_query(struct pci_dev *pdev, u8 *buf, int buf_size);
+int virtiovf_cmd_list_use(struct pci_dev *pdev, u8 *buf, int buf_size);
+int virtiovf_cmd_lr_write(struct virtiovf_pci_core_device *virtvdev, u16 opcode,
+			  u8 offset, u8 size, u8 *buf);
+int virtiovf_cmd_lr_read(struct virtiovf_pci_core_device *virtvdev, u16 opcode,
+			 u8 offset, u8 size, u8 *buf);
+int virtiovf_cmd_lq_read_notify(struct virtiovf_pci_core_device *virtvdev,
+				u8 req_bar_flags, u8 *bar, u64 *bar_offset);
+#endif /* VIRTIO_VFIO_CMD_H */
Signature · 1 line
2.27.0
#30 lore raw

Re: [PATCH vfio 10/11] vfio/virtio: Expose admin commands over virtio device

Quoted 17 lines · Yishai Hadas
Quoted 10 lines · Michael S. Tsirkin
Quoted 6 lines · Yishai Hadas

Expose admin commands over the virtio device, to be used by the vfio-virtio driver in the next patches.

It includes: list query/use, legacy write/read, read notify_info.

This stuff is pure virtio spec. I think it should live under drivers/virtio, too.

The motivation to put it in the vfio layer was from the below main reasons:

1) Having it inside virtio may require to export a symbol/function per command.

Quoted 6 lines

   This will end up today by 5 and in the future (e.g. live migration) with much more exported symbols.

   With current code we export only 2 generic symbols virtio_pci_vf_get_pf_dev(), virtio_admin_cmd_exec() which may fit for any further extension.

Except, there's no reasonable way for virtio to know what is done with the device then. You are not using just 2 symbols at all, instead you are using the rich vq API which was explicitly designed for the driver running the device being responsible for serializing accesses. Which is actually loaded and running. And I *think* your use won't conflict ATM mostly by luck. Witness the hack in patch 01 as exhibit 1 - nothing at all even hints at the fact that the reason for the complicated dance is because another driver pokes at some of the vqs.

Quoted 3 lines

2) For now there is no logic in this vfio layer, however, in the future we may have some DMA/other logic that should better fit to the caller/client layer (i.e. vfio).

You are poking at the device without any locks etc. Maybe it looks like no logic to you but it does not look like that from where I stand.

Quoted 3 lines

By the way, this follows what was done already between vfio/mlx5 to mlx5_core modules where mlx5_core exposes generic APIs to execute a command and to get the a PF from a given mlx5 VF.

This is up to mlx5 maintainers. In particular they only need to worry that their patches work with specific hardware which they likely have. virtio has to work with multiple vendors - hardware and software - and exposing a low level API that I can't test on my laptop is not at all my ideal.

Quoted 2 lines

This way, we can enable further commands to be added/extended easily/cleanly.

Something for vfio maintainer to consider in case it was assumed that it's just this one weird thing
but otherwise it's all generic vfio. It's not going to stop there, will it? The duplication of functionality with vdpa will continue :(

I am much more interested in adding reusable functionality that everyone benefits from than in vfio poking at the device in its own weird ways that only benefit specific hardware.

Quoted 197 lines

See for example here [1, 2].

[1] https://elixir.bootlin.com/linux/v6.6-rc3/source/drivers/vfio/pci/mlx5/cmd.c#L210

[2] https://elixir.bootlin.com/linux/v6.6-rc3/source/drivers/vfio/pci/mlx5/cmd.c#L683

Yishai

Quoted 187 lines
Quoted 187 lines
---
  drivers/vfio/pci/virtio/cmd.c | 146 ++++++++++++++++++++++++++++++++++
  drivers/vfio/pci/virtio/cmd.h |  27 +++++++
  2 files changed, 173 insertions(+)

create mode 100644 drivers/vfio/pci/virtio/cmd.c create mode 100644 drivers/vfio/pci/virtio/cmd.h

drivers/vfio/pci/virtio/cmd.c new file +146 -0
new file mode 100644
index 000000000000..f068239cdbb0
@@ -0,0 +1,146 @@
+// SPDX-License-Identifier: GPL-2.0 OR Linux-OpenIB
+/*
+ * Copyright (c) 2023, NVIDIA CORPORATION & AFFILIATES. All rights reserved
+ */
+
+#include "cmd.h"
+
+int virtiovf_cmd_list_query(struct pci_dev *pdev, u8 *buf, int buf_size)
+{
+	struct virtio_device *virtio_dev = virtio_pci_vf_get_pf_dev(pdev);
+	struct scatterlist out_sg;
+	struct virtio_admin_cmd cmd = {};
+
+	if (!virtio_dev)
+		return -ENOTCONN;
+
+	sg_init_one(&out_sg, buf, buf_size);
+	cmd.opcode = VIRTIO_ADMIN_CMD_LIST_QUERY;
+	cmd.group_type = VIRTIO_ADMIN_GROUP_TYPE_SRIOV;
+	cmd.result_sg = &out_sg;
+
+	return virtio_admin_cmd_exec(virtio_dev, &cmd);
+}
+
+int virtiovf_cmd_list_use(struct pci_dev *pdev, u8 *buf, int buf_size)
+{
+	struct virtio_device *virtio_dev = virtio_pci_vf_get_pf_dev(pdev);
+	struct scatterlist in_sg;
+	struct virtio_admin_cmd cmd = {};
+
+	if (!virtio_dev)
+		return -ENOTCONN;
+
+	sg_init_one(&in_sg, buf, buf_size);
+	cmd.opcode = VIRTIO_ADMIN_CMD_LIST_USE;
+	cmd.group_type = VIRTIO_ADMIN_GROUP_TYPE_SRIOV;
+	cmd.data_sg = &in_sg;
+
+	return virtio_admin_cmd_exec(virtio_dev, &cmd);
+}
+
+int virtiovf_cmd_lr_write(struct virtiovf_pci_core_device *virtvdev, u16 opcode,
+			  u8 offset, u8 size, u8 *buf)
+{
+	struct virtio_device *virtio_dev =
+		virtio_pci_vf_get_pf_dev(virtvdev->core_device.pdev);
+	struct virtio_admin_cmd_data_lr_write *in;
+	struct scatterlist in_sg;
+	struct virtio_admin_cmd cmd = {};
+	int ret;
+
+	if (!virtio_dev)
+		return -ENOTCONN;
+
+	in = kzalloc(sizeof(*in) + size, GFP_KERNEL);
+	if (!in)
+		return -ENOMEM;
+
+	in->offset = offset;
+	memcpy(in->registers, buf, size);
+	sg_init_one(&in_sg, in, sizeof(*in) + size);
+	cmd.opcode = opcode;
+	cmd.group_type = VIRTIO_ADMIN_GROUP_TYPE_SRIOV;
+	cmd.group_member_id = virtvdev->vf_id + 1;
+	cmd.data_sg = &in_sg;
+	ret = virtio_admin_cmd_exec(virtio_dev, &cmd);
+
+	kfree(in);
+	return ret;
+}
+
+int virtiovf_cmd_lr_read(struct virtiovf_pci_core_device *virtvdev, u16 opcode,
+			 u8 offset, u8 size, u8 *buf)
+{
+	struct virtio_device *virtio_dev =
+		virtio_pci_vf_get_pf_dev(virtvdev->core_device.pdev);
+	struct virtio_admin_cmd_data_lr_read *in;
+	struct scatterlist in_sg, out_sg;
+	struct virtio_admin_cmd cmd = {};
+	int ret;
+
+	if (!virtio_dev)
+		return -ENOTCONN;
+
+	in = kzalloc(sizeof(*in), GFP_KERNEL);
+	if (!in)
+		return -ENOMEM;
+
+	in->offset = offset;
+	sg_init_one(&in_sg, in, sizeof(*in));
+	sg_init_one(&out_sg, buf, size);
+	cmd.opcode = opcode;
+	cmd.group_type = VIRTIO_ADMIN_GROUP_TYPE_SRIOV;
+	cmd.data_sg = &in_sg;
+	cmd.result_sg = &out_sg;
+	cmd.group_member_id = virtvdev->vf_id + 1;
+	ret = virtio_admin_cmd_exec(virtio_dev, &cmd);
+
+	kfree(in);
+	return ret;
+}
+
+int virtiovf_cmd_lq_read_notify(struct virtiovf_pci_core_device *virtvdev,
+				u8 req_bar_flags, u8 *bar, u64 *bar_offset)
+{
+	struct virtio_device *virtio_dev =
+		virtio_pci_vf_get_pf_dev(virtvdev->core_device.pdev);
+	struct virtio_admin_cmd_notify_info_result *out;
+	struct scatterlist out_sg;
+	struct virtio_admin_cmd cmd = {};
+	int ret;
+
+	if (!virtio_dev)
+		return -ENOTCONN;
+
+	out = kzalloc(sizeof(*out), GFP_KERNEL);
+	if (!out)
+		return -ENOMEM;
+
+	sg_init_one(&out_sg, out, sizeof(*out));
+	cmd.opcode = VIRTIO_ADMIN_CMD_LEGACY_NOTIFY_INFO;
+	cmd.group_type = VIRTIO_ADMIN_GROUP_TYPE_SRIOV;
+	cmd.result_sg = &out_sg;
+	cmd.group_member_id = virtvdev->vf_id + 1;
+	ret = virtio_admin_cmd_exec(virtio_dev, &cmd);
+	if (!ret) {
+		struct virtio_admin_cmd_notify_info_data *entry;
+		int i;
+
+		ret = -ENOENT;
+		for (i = 0; i < VIRTIO_ADMIN_CMD_MAX_NOTIFY_INFO; i++) {
+			entry = &out->entries[i];
+			if (entry->flags == VIRTIO_ADMIN_CMD_NOTIFY_INFO_FLAGS_END)
+				break;
+			if (entry->flags != req_bar_flags)
+				continue;
+			*bar = entry->bar;
+			*bar_offset = le64_to_cpu(entry->offset);
+			ret = 0;
+			break;
+		}
+	}
+
+	kfree(out);
+	return ret;
+}
drivers/vfio/pci/virtio/cmd.h new file +21 -0
new file mode 100644
index 000000000000..c2a3645f4b90
@@ -0,0 +1,27 @@
+// SPDX-License-Identifier: GPL-2.0 OR Linux-OpenIB
+/*
+ * Copyright (c) 2023, NVIDIA CORPORATION & AFFILIATES. All rights reserved.
+ */
+
+#ifndef VIRTIO_VFIO_CMD_H
+#define VIRTIO_VFIO_CMD_H
+
+#include <linux/kernel.h>
+#include <linux/virtio.h>
+#include <linux/vfio_pci_core.h>
+#include <linux/virtio_pci.h>
+
+struct virtiovf_pci_core_device {
+	struct vfio_pci_core_device core_device;
+	int vf_id;
+};
+
+int virtiovf_cmd_list_query(struct pci_dev *pdev, u8 *buf, int buf_size);
+int virtiovf_cmd_list_use(struct pci_dev *pdev, u8 *buf, int buf_size);
+int virtiovf_cmd_lr_write(struct virtiovf_pci_core_device *virtvdev, u16 opcode,
Quoted 9 lines
Quoted 8 lines
Quoted 8 lines
drivers/vfio/pci/virtio/cmd.h new file +6 -0
+			  u8 offset, u8 size, u8 *buf);
+int virtiovf_cmd_lr_read(struct virtiovf_pci_core_device *virtvdev, u16 opcode,
+			 u8 offset, u8 size, u8 *buf);
+int virtiovf_cmd_lq_read_notify(struct virtiovf_pci_core_device *virtvdev,
+				u8 req_bar_flags, u8 *bar, u64 *bar_offset);
+#endif /* VIRTIO_VFIO_CMD_H */
Signature · 1 line
2.27.0

_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization

#31 lore raw

Re: [PATCH vfio 10/11] vfio/virtio: Expose admin commands over virtio device

Quoted 9 lines · Michael S. Tsirkin
Quoted 3 lines

By the way, this follows what was done already between vfio/mlx5 to mlx5_core modules where mlx5_core exposes generic APIs to execute a command and to get the a PF from a given mlx5 VF.

This is up to mlx5 maintainers. In particular they only need to worry that their patches work with specific hardware which they likely have. virtio has to work with multiple vendors - hardware and software - and exposing a low level API that I can't test on my laptop is not at all my ideal.

mlx5 has a reasonable API from the lower level that allows the vfio driver to safely issue commands. The API provides all the safety and locking you have been questioning here.

Then the vfio driver can form the commands directly and in the way it needs. This avoids spewing code into the core modules that is only used by vfio - which has been a key design consideration for our driver layering.

I suggest following the same design here as it has been well proven. Provide a solid API to operate the admin queue and let VFIO use it. One of the main purposes of the admin queue is to deliver commands on behalf of the VF driver, so this is a logical and reasonable place to put an API.

Quoted 7 lines
Quoted 2 lines

This way, we can enable further commands to be added/extended easily/cleanly.

Something for vfio maintainer to consider in case it was assumed that it's just this one weird thing
but otherwise it's all generic vfio. It's not going to stop there, will it? The duplication of functionality with vdpa will continue :(

VFIO live migration is expected to come as well once OASIS completes its work.

Parav, are there other things?

Jason

#32 lore raw

Re: [PATCH vfio 10/11] vfio/virtio: Expose admin commands over virtio device

Quoted 21 lines · Jason Gunthorpe
Quoted 9 lines · Michael S. Tsirkin
Quoted 3 lines

By the way, this follows what was done already between vfio/mlx5 to mlx5_core modules where mlx5_core exposes generic APIs to execute a command and to get the a PF from a given mlx5 VF.

This is up to mlx5 maintainers. In particular they only need to worry that their patches work with specific hardware which they likely have. virtio has to work with multiple vendors - hardware and software - and exposing a low level API that I can't test on my laptop is not at all my ideal.

mlx5 has a reasonable API from the lower level that allows the vfio driver to safely issue commands. The API provides all the safety and locking you have been questioning here.

Then the vfio driver can form the commands directly and in the way it needs. This avoids spewing code into the core modules that is only used by vfio - which has been a key design consideration for our driver layering.

Quoted 5 lines

I suggest following the same design here as it has been well proven. Provide a solid API to operate the admin queue and let VFIO use it. One of the main purposes of the admin queue is to deliver commands on behalf of the VF driver, so this is a logical and reasonable place to put an API.

Not the way virtio is designed now. I guess mlx5 is designed in a way that makes it safe.

Quoted 10 lines
Quoted 7 lines
Quoted 2 lines

This way, we can enable further commands to be added/extended easily/cleanly.

Something for vfio maintainer to consider in case it was assumed that it's just this one weird thing
but otherwise it's all generic vfio. It's not going to stop there, will it? The duplication of functionality with vdpa will continue :(

VFIO live migration is expected to come as well once OASIS completes its work.

Exactly. Is there doubt vdpa will want to support live migration? Put this code in a library please.

Quoted 3 lines

Parav, are there other things?

Jason

_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization

#33 lore raw

Re: [PATCH vfio 10/11] vfio/virtio: Expose admin commands over virtio device

Quoted 22 lines · Michael S. Tsirkin
Quoted 21 lines · Jason Gunthorpe
Quoted 9 lines · Michael S. Tsirkin
Quoted 3 lines

By the way, this follows what was done already between vfio/mlx5 to mlx5_core modules where mlx5_core exposes generic APIs to execute a command and to get the a PF from a given mlx5 VF.

This is up to mlx5 maintainers. In particular they only need to worry that their patches work with specific hardware which they likely have. virtio has to work with multiple vendors - hardware and software - and exposing a low level API that I can't test on my laptop is not at all my ideal.

mlx5 has a reasonable API from the lower level that allows the vfio driver to safely issue commands. The API provides all the safety and locking you have been questioning here.

Then the vfio driver can form the commands directly and in the way it needs. This avoids spewing code into the core modules that is only used by vfio - which has been a key design consideration for our driver layering.

Quoted 8 lines
Quoted 5 lines

I suggest following the same design here as it has been well proven. Provide a solid API to operate the admin queue and let VFIO use it. One of the main purposes of the admin queue is to deliver commands on behalf of the VF driver, so this is a logical and reasonable place to put an API.

Not the way virtio is designed now. I guess mlx5 is designed in a way that makes it safe.

If you can't reliably issue commmands from the VF at all it doesn't really matter where you put the code. Once that is established up then an admin command execution interface is a nice cut point for modularity.

The locking in mlx5 to make this safe is not too complex, if Feng missed some items for virtio then he can work to fix it up.

Quoted 5 lines
Quoted 2 lines

VFIO live migration is expected to come as well once OASIS completes its work.

Exactly. Is there doubt vdpa will want to support live migration? Put this code in a library please.

I have a doubt, you both said vdpa already does live migration, so what will it even do with a live migration interface to a PCI function?

It already has to use full mediation to operate a physical virtio function, so it seems like it shouldn't need the migration interface?

Regardless, it is better kernel development hygiene to put the code where it is used and wait for a second user to consolidate it than to guess.

Jason

#34 lore raw

Re: [PATCH vfio 10/11] vfio/virtio: Expose admin commands over virtio device

Quoted 32 lines · Jason Gunthorpe
Quoted 22 lines · Michael S. Tsirkin
Quoted 21 lines · Jason Gunthorpe
Quoted 9 lines · Michael S. Tsirkin
Quoted 3 lines

By the way, this follows what was done already between vfio/mlx5 to mlx5_core modules where mlx5_core exposes generic APIs to execute a command and to get the a PF from a given mlx5 VF.

This is up to mlx5 maintainers. In particular they only need to worry that their patches work with specific hardware which they likely have. virtio has to work with multiple vendors - hardware and software - and exposing a low level API that I can't test on my laptop is not at all my ideal.

mlx5 has a reasonable API from the lower level that allows the vfio driver to safely issue commands. The API provides all the safety and locking you have been questioning here.

Then the vfio driver can form the commands directly and in the way it needs. This avoids spewing code into the core modules that is only used by vfio - which has been a key design consideration for our driver layering.

Quoted 8 lines
Quoted 5 lines

I suggest following the same design here as it has been well proven. Provide a solid API to operate the admin queue and let VFIO use it. One of the main purposes of the admin queue is to deliver commands on behalf of the VF driver, so this is a logical and reasonable place to put an API.

Not the way virtio is designed now. I guess mlx5 is designed in a way that makes it safe.

Quoted 7 lines

If you can't reliably issue commmands from the VF at all it doesn't really matter where you put the code. Once that is established up then an admin command execution interface is a nice cut point for modularity.

The locking in mlx5 to make this safe is not too complex, if Feng missed some items for virtio then he can work to fix it up.

Above two paragraphs don't make sense to me at all. VF issues no commands and there's no locking.

Quoted 9 lines
Quoted 5 lines
Quoted 2 lines

VFIO live migration is expected to come as well once OASIS completes its work.

Exactly. Is there doubt vdpa will want to support live migration? Put this code in a library please.

I have a doubt, you both said vdpa already does live migration, so what will it even do with a live migration interface to a PCI function?

This is not the thread to explain how vdpa live migration works now and why it needs new interfaces, sorry. Suffice is to say right now on virtio tc Parav from nvidia is arguing for vdpa to use admin commands for migration.

Quoted 8 lines

It already has to use full mediation to operate a physical virtio function, so it seems like it shouldn't need the migration interface?

Regardless, it is better kernel development hygiene to put the code where it is used and wait for a second user to consolidate it than to guess.

Jason

Sorry no time right now to argue philosophy. I gave some hints on how to make the virtio changes behave in a way that I'm ok with maintaining. Hope they help.

Signature · 6 lines
MST

_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization
#35 lore raw

Re: [PATCH vfio 10/11] vfio/virtio: Expose admin commands over virtio device

In reply to Michael S. Tsirkin
Quoted 9 lines · Michael S. Tsirkin

Except, there's no reasonable way for virtio to know what is done with the device then. You are not using just 2 symbols at all, instead you are using the rich vq API which was explicitly designed for the driver running the device being responsible for serializing accesses. Which is actually loaded and running. And I *think* your use won't conflict ATM mostly by luck. Witness the hack in patch 01 as exhibit 1 - nothing at all even hints at the fact that the reason for the complicated dance is because another driver pokes at some of the vqs.

Fully agreed. The smart nic vendors are trying to do the same mess in nvme, and we really need to stop them and agree on proper standarized live migration features implemented in the core virtio/nvme code.

#36 lore raw

Re: [PATCH vfio 10/11] vfio/virtio: Expose admin commands over virtio device

Quoted 11 lines · Christoph Hellwig
Quoted 9 lines · Michael S. Tsirkin

Except, there's no reasonable way for virtio to know what is done with the device then. You are not using just 2 symbols at all, instead you are using the rich vq API which was explicitly designed for the driver running the device being responsible for serializing accesses. Which is actually loaded and running. And I *think* your use won't conflict ATM mostly by luck. Witness the hack in patch 01 as exhibit 1 - nothing at all even hints at the fact that the reason for the complicated dance is because another driver pokes at some of the vqs.

Quoted 3 lines

Fully agreed. The smart nic vendors are trying to do the same mess in nvme, and we really need to stop them and agree on proper standarized live migration features implemented in the core virtio/nvme code.

??? This patch series is an implementation of changes that OASIS approved.

The live migration work is going to OASIS first no patches have been presented.

This thread is arguing about how to split up the code for the implementatin of the standard given that VFIO owns the VF and the virtio core owns the PF. The standard defined that PF admin queue operations are needed to do operations on behalf of the VF.

Jason

#37 lore raw

Re: [PATCH vfio 10/11] vfio/virtio: Expose admin commands over virtio device

Quoted 2 lines · Jason Gunthorpe

??? This patch series is an implementation of changes that OASIS approved.

I think you are fundamentally missing my point. This is not about who publish a spec, but how we struture Linux code.

And the problem is that we trea vfio as a separate thing, and not an integral part of the driver. vfio being separate totally makes sense for the original purpose of vfio, that is a a no-op passthrough of a device to userspace.

But for all the augmented vfio use cases it doesn't, for them the augmented vfio functionality is an integral part of the core driver. That is true for nvme, virtio and I'd argue mlx5 as well.

So we need to stop registering separate pci_drivers for this kind of functionality, and instead have an interface to the driver to switch to certain functionalities.

E.g. for this case there should be no new vfio-virtio device, but instead you should be able to switch the virtio device to an fake-legacy vfio mode.

Assuming the whole thing actually makes sense, as the use case seems a bit fishy to start with, but I'll leave that argument to the virtio maintainers.

Similarly for nvme. We'll never accept a separate nvme-live migration vfio driver. This functionality needs to be part of the nvme driver, probed there and fully controlled there.
_______________________________________________
Virtualization mailing list [email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization

#38 lore raw

Re: [PATCH vfio 10/11] vfio/virtio: Expose admin commands over virtio device

Quoted 3 lines · Christoph Hellwig

But for all the augmented vfio use cases it doesn't, for them the augmented vfio functionality is an integral part of the core driver. That is true for nvme, virtio and I'd argue mlx5 as well.

I don't agree with this. I see the extra functionality as being an integral part of the VF and VFIO. The PF driver is only providing a proxied communication channel.

It is a limitation of PCI that the PF must act as a proxy.

Quoted 3 lines

So we need to stop registering separate pci_drivers for this kind of functionality, and instead have an interface to the driver to switch to certain functionalities.

?? We must bind something to the VF's pci_driver, what do you imagine that is?

Quoted 3 lines

E.g. for this case there should be no new vfio-virtio device, but instead you should be able to switch the virtio device to an fake-legacy vfio mode.

Are you aruging about how we reach to vfio_register_XX() and what directory the file lives?

I don't know what "fake-legacy" even means, VFIO is not legacy.

There is alot of code in VFIO and the VMM side to take a VF and turn it into a vPCI function. You can't just trivially duplicate VFIO in a dozen drivers without creating a giant mess.

Further, userspace wants consistent ways to operate this stuff. If we need a dozen ways to activate VFIO for every kind of driver that is not a positive direction.

Basically, I don't know what you are suggesting here. We talked about this before, and my position is still the same. Continuing to have /dev/vfio/XX be the kernel uAPI for the VMM to work with non-mediated vPCI functions with live migration is the technically correct thing to do.

Why wouldn't it be?

Quoted 3 lines

Similarly for nvme. We'll never accept a separate nvme-live migration vfio driver. This functionality needs to be part of the nvme driver, probed there and fully controlled there.

We can debate where to put the files when the standard is done, but the end of the day it needs to create /dev/vfio/XXX.

Jason

#39 lore raw

Re: [PATCH vfio 10/11] vfio/virtio: Expose admin commands over virtio device

Quoted 9 lines · Jason Gunthorpe
Quoted 3 lines

But for all the augmented vfio use cases it doesn't, for them the augmented vfio functionality is an integral part of the core driver. That is true for nvme, virtio and I'd argue mlx5 as well.

I don't agree with this. I see the extra functionality as being an integral part of the VF and VFIO. The PF driver is only providing a proxied communication channel.

It is a limitation of PCI that the PF must act as a proxy.

For anything live migration it very fundamentally is not, as a function that is visible to a guest by definition can't drive the migration itself. That isn't really a limitation in PCI, but follows form the fact that something else must control a live migration that is transparent to the guest.

Quoted 7 lines
Quoted 4 lines

So we need to stop registering separate pci_drivers for this kind of functionality, and instead have an interface to the driver to switch to certain functionalities.

?? We must bind something to the VF's pci_driver, what do you imagine that is?

The driver that knows this hardware. In this case the virtio subsystem, in case of nvme the nvme driver, and in case of mlx5 the mlx5 driver.

Quoted 6 lines
Quoted 3 lines

E.g. for this case there should be no new vfio-virtio device, but instead you should be able to switch the virtio device to an fake-legacy vfio mode.

Are you aruging about how we reach to vfio_register_XX() and what directory the file lives?

No. That layout logically follows from what codebase the functionality is part of, though.

Quoted 1 line

I don't know what "fake-legacy" even means, VFIO is not legacy.

The driver we're talking about in this thread fakes up a virtio_pci legacy devie to the guest on top of a "modern" virtio_pci device.

Quoted 3 lines

There is alot of code in VFIO and the VMM side to take a VF and turn it into a vPCI function. You can't just trivially duplicate VFIO in a dozen drivers without creating a giant mess.

I do not advocate for duplicating it. But the code that calls this functionality belongs into the driver that deals with the compound device that we're doing this work for.

Quoted 3 lines

Further, userspace wants consistent ways to operate this stuff. If we need a dozen ways to activate VFIO for every kind of driver that is not a positive direction.

We don't need a dozen ways. We just need a single attribute on the pci (or $OTHERBUS) devide that switches it to vfio mode.

#40 lore raw

Re: [PATCH vfio 10/11] vfio/virtio: Expose admin commands over virtio device

Quoted 9 lines · Christoph Hellwig
Quoted 8 lines · Jason Gunthorpe
Quoted 3 lines

But for all the augmented vfio use cases it doesn't, for them the augmented vfio functionality is an integral part of the core driver. That is true for nvme, virtio and I'd argue mlx5 as well.

I don't agree with this. I see the extra functionality as being an integral part of the VF and VFIO. The PF driver is only providing a proxied communication channel.

Quoted 7 lines
Quoted 1 line

It is a limitation of PCI that the PF must act as a proxy.

For anything live migration it very fundamentally is not, as a function that is visible to a guest by definition can't drive the migration itself. That isn't really a limitation in PCI, but follows form the fact that something else must control a live migration that is transparent to the guest.

We've talked around ideas like allowing the VF config space to do some of the work. For simple devices we could get away with 1 VF config space register. (VF config space is owned by the hypervisor, not the guest)

Devices that need DMA as part of their migration could be imagined to co-opt a VF PASID or something. eg using ENQCMD.

SIOVr2 is discussing more a flexible RID mapping - there is a possible route where a "VF" could actually have two RIDs, a hypervisor RID and a guest RID.

It really is PCI limitations that force this design of making a PF driver do dual duty as a fully functionally normal device and act as a communication channel proxy to make a back channel into a SRIOV VF.

My view has always been that the VFIO live migration operations are executed logically within the VF as they only effect the VF.

So we have a logical design seperation where VFIO world owns the commands and the PF driver supplies the communication channel. This works well for devices that already have a robust RPC interface to their device FW.

Quoted 5 lines
Quoted 2 lines

?? We must bind something to the VF's pci_driver, what do you imagine that is?

The driver that knows this hardware. In this case the virtio subsystem, in case of nvme the nvme driver, and in case of mlx5 the mlx5 driver.

But those are drivers operating the HW to create kernel devices. Here we need a VFIO device. They can't co-exist, if you switch mlx5 from normal to vfio you have to tear down the entire normal driver.

Quoted 9 lines
Quoted 6 lines
Quoted 3 lines

E.g. for this case there should be no new vfio-virtio device, but instead you should be able to switch the virtio device to an fake-legacy vfio mode.

Are you aruging about how we reach to vfio_register_XX() and what directory the file lives?

No. That layout logically follows from what codebase the functionality is part of, though.

I don't understand what we are talking about really. Where do you imagine the vfio_register_XX() goes?

Quoted 4 lines
Quoted 1 line

I don't know what "fake-legacy" even means, VFIO is not legacy.

The driver we're talking about in this thread fakes up a virtio_pci legacy devie to the guest on top of a "modern" virtio_pci device.

I'm not sure I'd use the word fake, inb/outb are always trapped operations in VMs. If the device provided a real IO BAR then VFIO common code would trap and relay inb/outb to the device.

All this is doing is changing the inb/outb relay from using a physical IO BAR to a DMA command ring.

The motivation is simply because normal IO BAR space is incredibly limited and you can't get enough SRIOV functions when using it.

Quoted 7 lines
Quoted 3 lines

There is alot of code in VFIO and the VMM side to take a VF and turn it into a vPCI function. You can't just trivially duplicate VFIO in a dozen drivers without creating a giant mess.

I do not advocate for duplicating it. But the code that calls this functionality belongs into the driver that deals with the compound device that we're doing this work for.

On one hand, I don't really care - we can put the code where people like.

However - the Intel GPU VFIO driver is such a bad experiance I don't want to encourage people to make VFIO drivers, or code that is only used by VFIO drivers, that are not under drivers/vfio review.

Quoted 6 lines
Quoted 3 lines

Further, userspace wants consistent ways to operate this stuff. If we need a dozen ways to activate VFIO for every kind of driver that is not a positive direction.

We don't need a dozen ways. We just need a single attribute on the pci (or $OTHERBUS) devide that switches it to vfio mode.

Well, we sort of do these days, it is just a convoluted bind thing.

Realistically switching modes requires unprobing the entire normal VF driver. Having this be linked to the driver core probe/unprobe flows is a good code reuse thing, IMHO.

We already spent alot of effort making this quite general from the userspace perspective. Nobody yet came up with an idea to avoid the ugly unbind/bind flow.

Be aware, there is a significant performance concern here. If you want to create 1000 VFIO devices (this is a real thing), we *can't* probe a normal driver first, it is too slow. We need a path that goes directly from creating the RIDs to turning those RIDs into VFIO.

mlx5 takes *seconds* to complete its normal probe. We must avoid this.

Looking a few years into the future, with SIOVr1/2, the flow I want to target is some uAPI commands:
'create a PCI RID with params XYZ and attach a normal/VFIO/etc driver' 'destroy a PCI RID'

We need to get away from this scheme of SRIOV where you bulk create a bunch of empty VFs at one time and then have to somehow provision them.

Jason

#41 lore raw

Re: [PATCH vfio 10/11] vfio/virtio: Expose admin commands over virtio device

Quoted 8 lines · Jason Gunthorpe
Quoted 7 lines
Quoted 3 lines

There is alot of code in VFIO and the VMM side to take a VF and turn it into a vPCI function. You can't just trivially duplicate VFIO in a dozen drivers without creating a giant mess.

I do not advocate for duplicating it. But the code that calls this functionality belongs into the driver that deals with the compound device that we're doing this work for.

Quoted 6 lines

On one hand, I don't really care - we can put the code where people like.

However - the Intel GPU VFIO driver is such a bad experiance I don't want to encourage people to make VFIO drivers, or code that is only used by VFIO drivers, that are not under drivers/vfio review.

So if Alex feels it makes sense to add some virtio functionality to vfio and is happy to maintain or let you maintain the UAPI then why would I say no? But we never expected devices to have two drivers like this does, so just exposing device pointer and saying "use regular virtio APIs for the rest" does not cut it, the new APIs have to make sense
so virtio drivers can develop normally without fear of stepping on the toes of this admin driver.

Signature · 6 lines
MST

_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization
#42 lore raw

Re: [PATCH vfio 10/11] vfio/virtio: Expose admin commands over virtio device

Quoted 4 lines · Michael S. Tsirkin
Quoted 3 lines

However - the Intel GPU VFIO driver is such a bad experiance I don't want to encourage people to make VFIO drivers, or code that is only used by VFIO drivers, that are not under drivers/vfio review.

Quoted 8 lines

So if Alex feels it makes sense to add some virtio functionality to vfio and is happy to maintain or let you maintain the UAPI then why would I say no? But we never expected devices to have two drivers like this does, so just exposing device pointer and saying "use regular virtio APIs for the rest" does not cut it, the new APIs have to make sense
so virtio drivers can develop normally without fear of stepping on the toes of this admin driver.

Please work with Yishai to get something that make sense to you. He can post a v2 with the accumulated comments addressed so far and then go over what the API between the drivers is.

Thanks,
Jason

#43 lore raw

Re: [PATCH vfio 10/11] vfio/virtio: Expose admin commands over virtio device

Quoted 15 lines · Jason Gunthorpe
Quoted 4 lines · Michael S. Tsirkin
Quoted 3 lines

However - the Intel GPU VFIO driver is such a bad experiance I don't want to encourage people to make VFIO drivers, or code that is only used by VFIO drivers, that are not under drivers/vfio review.

Quoted 8 lines

So if Alex feels it makes sense to add some virtio functionality to vfio and is happy to maintain or let you maintain the UAPI then why would I say no? But we never expected devices to have two drivers like this does, so just exposing device pointer and saying "use regular virtio APIs for the rest" does not cut it, the new APIs have to make sense
so virtio drivers can develop normally without fear of stepping on the toes of this admin driver.

Quoted 6 lines

Please work with Yishai to get something that make sense to you. He can post a v2 with the accumulated comments addressed so far and then go over what the API between the drivers is.

Thanks,
Jason

/me shrugs. I pretty much posted suggestions already. Should not be hard. Anything unclear - post on list.

Signature · 6 lines
MST

_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization
author maintainer
#44 lore raw

Re: [PATCH vfio 10/11] vfio/virtio: Expose admin commands over virtio device

Quoted 14 lines · Michael S. Tsirkin
Quoted 13 lines · Jason Gunthorpe
Quoted 11 lines · Michael S. Tsirkin
Quoted 3 lines

However - the Intel GPU VFIO driver is such a bad experiance I don't want to encourage people to make VFIO drivers, or code that is only used by VFIO drivers, that are not under drivers/vfio review.

So if Alex feels it makes sense to add some virtio functionality to vfio and is happy to maintain or let you maintain the UAPI then why would I say no? But we never expected devices to have two drivers like this does, so just exposing device pointer and saying "use regular virtio APIs for the rest" does not cut it, the new APIs have to make sense
so virtio drivers can develop normally without fear of stepping on the toes of this admin driver.

Quoted 9 lines
Quoted 6 lines

Please work with Yishai to get something that make sense to you. He can post a v2 with the accumulated comments addressed so far and then go over what the API between the drivers is.

Thanks,
Jason

/me shrugs. I pretty much posted suggestions already. Should not be hard. Anything unclear - post on list.

Yes, this is the plan.

We are working to address the comments that we got so far in both VFIO & VIRTIO, retest and send the next version.

Re the API between the modules, It looks like we have the below alternatives.

1) Proceed with current approach where we exposed a generic API to execute any admin command, however, make it much more solid inside VIRTIO.
2) Expose extra APIs from VIRTIO for commands that we can consider future client usage of them as of LIST_QUERY/LIST_USE, however still have the generic execute admin command for others.
3) Expose API per command from VIRTIO and fully drop the generic execute admin command.

Few notes:
Option #1 looks the most generic one, it drops the need to expose multiple symbols / APIs per command and for now we have a single client for them (i.e. VFIO).
Options #2 & #3, may still require to expose the virtio_pci_vf_get_pf_dev() API to let VFIO get the VIRTIO PF (struct virtio_device *) from its PCI device, each command will get it as its first argument.

Michael,
What do you suggest here ?

Thanks,
Yishai

#45 lore raw

Re: [PATCH vfio 10/11] vfio/virtio: Expose admin commands over virtio device

Quoted 48 lines · Yishai Hadas
Quoted 14 lines · Michael S. Tsirkin
Quoted 13 lines · Jason Gunthorpe
Quoted 11 lines · Michael S. Tsirkin
Quoted 3 lines

However - the Intel GPU VFIO driver is such a bad experiance I don't want to encourage people to make VFIO drivers, or code that is only used by VFIO drivers, that are not under drivers/vfio review.

So if Alex feels it makes sense to add some virtio functionality to vfio and is happy to maintain or let you maintain the UAPI then why would I say no? But we never expected devices to have two drivers like this does, so just exposing device pointer and saying "use regular virtio APIs for the rest" does not cut it, the new APIs have to make sense
so virtio drivers can develop normally without fear of stepping on the toes of this admin driver.

Quoted 9 lines
Quoted 6 lines

Please work with Yishai to get something that make sense to you. He can post a v2 with the accumulated comments addressed so far and then go over what the API between the drivers is.

Thanks,
Jason

/me shrugs. I pretty much posted suggestions already. Should not be hard. Anything unclear - post on list.

Yes, this is the plan.

We are working to address the comments that we got so far in both VFIO & VIRTIO, retest and send the next version.

Re the API between the modules, It looks like we have the below alternatives.

1) Proceed with current approach where we exposed a generic API to execute any admin command, however, make it much more solid inside VIRTIO.
2) Expose extra APIs from VIRTIO for commands that we can consider future client usage of them as of LIST_QUERY/LIST_USE, however still have the generic execute admin command for others.
3) Expose API per command from VIRTIO and fully drop the generic execute admin command.

Few notes:
Option #1 looks the most generic one, it drops the need to expose multiple symbols / APIs per command and for now we have a single client for them (i.e. VFIO).
Options #2 & #3, may still require to expose the virtio_pci_vf_get_pf_dev() API to let VFIO get the VIRTIO PF (struct virtio_device *) from its PCI device, each command will get it as its first argument.

Quoted 5 lines

Michael,
What do you suggest here ?

Thanks,
Yishai

I suggest 3 but call it on the VF. commands will switch to PF internally as needed. For example, intel might be interested in exposing admin commands through a memory BAR of VF itself.

Signature · 6 lines
MST

_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization
author maintainer
#46 lore raw

Re: [PATCH vfio 10/11] vfio/virtio: Expose admin commands over virtio device

Quoted 49 lines · Michael S. Tsirkin
Quoted 48 lines · Yishai Hadas
Quoted 14 lines · Michael S. Tsirkin
Quoted 13 lines · Jason Gunthorpe
Quoted 11 lines · Michael S. Tsirkin
Quoted 3 lines

However - the Intel GPU VFIO driver is such a bad experiance I don't want to encourage people to make VFIO drivers, or code that is only used by VFIO drivers, that are not under drivers/vfio review.

So if Alex feels it makes sense to add some virtio functionality to vfio and is happy to maintain or let you maintain the UAPI then why would I say no? But we never expected devices to have two drivers like this does, so just exposing device pointer and saying "use regular virtio APIs for the rest" does not cut it, the new APIs have to make sense
so virtio drivers can develop normally without fear of stepping on the toes of this admin driver.

Quoted 9 lines
Quoted 6 lines

Please work with Yishai to get something that make sense to you. He can post a v2 with the accumulated comments addressed so far and then go over what the API between the drivers is.

Thanks,
Jason

/me shrugs. I pretty much posted suggestions already. Should not be hard. Anything unclear - post on list.

Yes, this is the plan.

We are working to address the comments that we got so far in both VFIO & VIRTIO, retest and send the next version.

Re the API between the modules, It looks like we have the below alternatives.

1) Proceed with current approach where we exposed a generic API to execute any admin command, however, make it much more solid inside VIRTIO.
2) Expose extra APIs from VIRTIO for commands that we can consider future client usage of them as of LIST_QUERY/LIST_USE, however still have the generic execute admin command for others.
3) Expose API per command from VIRTIO and fully drop the generic execute admin command.

Few notes:
Option #1 looks the most generic one, it drops the need to expose multiple symbols / APIs per command and for now we have a single client for them (i.e. VFIO).
Options #2 & #3, may still require to expose the virtio_pci_vf_get_pf_dev() API to let VFIO get the VIRTIO PF (struct virtio_device *) from its PCI device, each command will get it as its first argument.

Quoted 9 lines
Quoted 5 lines

Michael,
What do you suggest here ?

Thanks,
Yishai

I suggest 3 but call it on the VF. commands will switch to PF internally as needed. For example, intel might be interested in exposing admin commands through a memory BAR of VF itself.

The driver who owns the VF is VFIO, it's not a VIRTIO one.

The ability to get the VIRTIO PF is from the PCI device (i.e. struct pci_dev).

In addition,
virtio_pci_vf_get_pf_dev() was implemented for now in virtio-pci as it worked on pci_dev.
Assuming that we'll put each command inside virtio as the generic layer, we won't be able to call/use this API internally to get the PF as of cyclic dependencies between the modules, link will fail.

So in option #3 we may still need to get outside into VFIO the VIRTIO PF and give it as pointer to VIRTIO upon each command.

Does it work for you ?

Yishai

#47 lore raw

RE: [PATCH vfio 10/11] vfio/virtio: Expose admin commands over virtio device

Quoted 76 lines

From: Yishai Hadas <[email protected]>
Sent: Tuesday, October 10, 2023 9:14 PM

Quoted 26 lines · Michael S. Tsirkin
Quoted 25 lines · Yishai Hadas
Quoted 13 lines · Michael S. Tsirkin
Quoted 12 lines · Jason Gunthorpe
Quoted 10 lines · Michael S. Tsirkin
Quoted 3 lines

However - the Intel GPU VFIO driver is such a bad experiance I don't want to encourage people to make VFIO drivers, or code that is only used by VFIO drivers, that are not under drivers/vfio review.

So if Alex feels it makes sense to add some virtio functionality to vfio and is happy to maintain or let you maintain the UAPI then why would I say no? But we never expected devices to have two drivers like this does, so just exposing device pointer and saying "use regular virtio APIs for the rest" does not cut it, the new APIs have to make sense so virtio drivers can develop normally without fear of stepping on the toes of this admin driver.

Quoted 9 lines
Quoted 6 lines

Please work with Yishai to get something that make sense to you. He can post a v2 with the accumulated comments addressed so far and then go over what the API between the drivers is.

Thanks,
Jason

/me shrugs. I pretty much posted suggestions already. Should not be hard. Anything unclear - post on list.

Yes, this is the plan.

Quoted 8 lines
Quoted 8 lines

We are working to address the comments that we got so far in both VFIO & VIRTIO, retest and send the next version.

Re the API between the modules, It looks like we have the below alternatives.

1) Proceed with current approach where we exposed a generic API to execute any admin command, however, make it much more solid inside

VIRTIO.

Quoted 6 lines
Quoted 6 lines

2) Expose extra APIs from VIRTIO for commands that we can consider future client usage of them as of LIST_QUERY/LIST_USE, however still have the generic execute admin command for others.
3) Expose API per command from VIRTIO and fully drop the generic execute admin command.

Quoted 7 lines
Quoted 7 lines

Few notes:
Option #1 looks the most generic one, it drops the need to expose multiple symbols / APIs per command and for now we have a single client for them (i.e. VFIO).
Options #2 & #3, may still require to expose the virtio_pci_vf_get_pf_dev() API to let VFIO get the VIRTIO PF (struct virtio_device *) from its PCI device, each command will get it as its first

argument.

Quoted 10 lines
Quoted 6 lines

Michael,
What do you suggest here ?

Thanks,
Yishai

I suggest 3 but call it on the VF. commands will switch to PF internally as needed. For example, intel might be interested in exposing admin commands through a memory BAR of VF itself.

The driver who owns the VF is VFIO, it's not a VIRTIO one.

The ability to get the VIRTIO PF is from the PCI device (i.e. struct pci_dev).

In addition,
virtio_pci_vf_get_pf_dev() was implemented for now in virtio-pci as it worked on pci_dev.
Assuming that we'll put each command inside virtio as the generic layer, we won't be able to call/use this API internally to get the PF as of cyclic dependencies between the modules, link will fail.

Quoted 3 lines

So in option #3 we may still need to get outside into VFIO the VIRTIO PF and give it as pointer to VIRTIO upon each command.

I think,
For #3 the virtio level API signature should be,

virtio_admin_legacy_xyz_cmd(struct virtio_device *dev, u64 group_member_id, ....);

This maintains the right abstraction needed between vfio, generic virtio and virtio transport (pci) layer.

#48 lore raw

Re: [PATCH vfio 10/11] vfio/virtio: Expose admin commands over virtio device

In reply to Yishai Hadas
Quoted 59 lines · Yishai Hadas
Quoted 49 lines · Michael S. Tsirkin
Quoted 48 lines · Yishai Hadas
Quoted 14 lines · Michael S. Tsirkin
Quoted 13 lines · Jason Gunthorpe
Quoted 11 lines · Michael S. Tsirkin
Quoted 3 lines

However - the Intel GPU VFIO driver is such a bad experiance I don't want to encourage people to make VFIO drivers, or code that is only used by VFIO drivers, that are not under drivers/vfio review.

So if Alex feels it makes sense to add some virtio functionality to vfio and is happy to maintain or let you maintain the UAPI then why would I say no? But we never expected devices to have two drivers like this does, so just exposing device pointer and saying "use regular virtio APIs for the rest" does not cut it, the new APIs have to make sense
so virtio drivers can develop normally without fear of stepping on the toes of this admin driver.

Quoted 9 lines
Quoted 6 lines

Please work with Yishai to get something that make sense to you. He can post a v2 with the accumulated comments addressed so far and then go over what the API between the drivers is.

Thanks,
Jason

/me shrugs. I pretty much posted suggestions already. Should not be hard. Anything unclear - post on list.

Yes, this is the plan.

We are working to address the comments that we got so far in both VFIO & VIRTIO, retest and send the next version.

Re the API between the modules, It looks like we have the below alternatives.

1) Proceed with current approach where we exposed a generic API to execute any admin command, however, make it much more solid inside VIRTIO.
2) Expose extra APIs from VIRTIO for commands that we can consider future client usage of them as of LIST_QUERY/LIST_USE, however still have the generic execute admin command for others.
3) Expose API per command from VIRTIO and fully drop the generic execute admin command.

Few notes:
Option #1 looks the most generic one, it drops the need to expose multiple symbols / APIs per command and for now we have a single client for them (i.e. VFIO).
Options #2 & #3, may still require to expose the virtio_pci_vf_get_pf_dev() API to let VFIO get the VIRTIO PF (struct virtio_device *) from its PCI device, each command will get it as its first argument.

Quoted 9 lines
Quoted 5 lines

Michael,
What do you suggest here ?

Thanks,
Yishai

I suggest 3 but call it on the VF. commands will switch to PF internally as needed. For example, intel might be interested in exposing admin commands through a memory BAR of VF itself.

Quoted 8 lines

The driver who owns the VF is VFIO, it's not a VIRTIO one.

The ability to get the VIRTIO PF is from the PCI device (i.e. struct pci_dev).

In addition,
virtio_pci_vf_get_pf_dev() was implemented for now in virtio-pci as it worked on pci_dev.

On pci_dev of vf, yes? So again just move this into each command, that's all. I.e. pass pci_dev to each.

Quoted 10 lines

Assuming that we'll put each command inside virtio as the generic layer, we won't be able to call/use this API internally to get the PF as of cyclic dependencies between the modules, link will fail.

So in option #3 we may still need to get outside into VFIO the VIRTIO PF and give it as pointer to VIRTIO upon each command.

Does it work for you ?

Yishai

_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization

author maintainer
#49 lore raw

Re: [PATCH vfio 10/11] vfio/virtio: Expose admin commands over virtio device

Quoted 62 lines · Michael S. Tsirkin
Quoted 61 lines · Yishai Hadas
Quoted 49 lines · Michael S. Tsirkin
Quoted 48 lines · Yishai Hadas
Quoted 14 lines · Michael S. Tsirkin
Quoted 13 lines · Jason Gunthorpe
Quoted 11 lines · Michael S. Tsirkin
Quoted 3 lines

However - the Intel GPU VFIO driver is such a bad experiance I don't want to encourage people to make VFIO drivers, or code that is only used by VFIO drivers, that are not under drivers/vfio review.

So if Alex feels it makes sense to add some virtio functionality to vfio and is happy to maintain or let you maintain the UAPI then why would I say no? But we never expected devices to have two drivers like this does, so just exposing device pointer and saying "use regular virtio APIs for the rest" does not cut it, the new APIs have to make sense
so virtio drivers can develop normally without fear of stepping on the toes of this admin driver.

Quoted 9 lines
Quoted 6 lines

Please work with Yishai to get something that make sense to you. He can post a v2 with the accumulated comments addressed so far and then go over what the API between the drivers is.

Thanks,
Jason

/me shrugs. I pretty much posted suggestions already. Should not be hard. Anything unclear - post on list.

Yes, this is the plan.

We are working to address the comments that we got so far in both VFIO & VIRTIO, retest and send the next version.

Re the API between the modules, It looks like we have the below alternatives.

1) Proceed with current approach where we exposed a generic API to execute any admin command, however, make it much more solid inside VIRTIO.
2) Expose extra APIs from VIRTIO for commands that we can consider future client usage of them as of LIST_QUERY/LIST_USE, however still have the generic execute admin command for others.
3) Expose API per command from VIRTIO and fully drop the generic execute admin command.

Few notes:
Option #1 looks the most generic one, it drops the need to expose multiple symbols / APIs per command and for now we have a single client for them (i.e. VFIO).
Options #2 & #3, may still require to expose the virtio_pci_vf_get_pf_dev() API to let VFIO get the VIRTIO PF (struct virtio_device *) from its PCI device, each command will get it as its first argument.

Quoted 9 lines
Quoted 5 lines

Michael,
What do you suggest here ?

Thanks,
Yishai

I suggest 3 but call it on the VF. commands will switch to PF internally as needed. For example, intel might be interested in exposing admin commands through a memory BAR of VF itself.

The driver who owns the VF is VFIO, it's not a VIRTIO one.

Quoted 8 lines
Quoted 6 lines

The ability to get the VIRTIO PF is from the PCI device (i.e. struct pci_dev).

In addition,
virtio_pci_vf_get_pf_dev() was implemented for now in virtio-pci as it worked on pci_dev.

On pci_dev of vf, yes? So again just move this into each command, that's all. I.e. pass pci_dev to each.

How about the cyclic dependencies issue inside VIRTIO that I mentioned  below ?

In my suggestion it's fine, VFIO will get the PF and give it to VIRTIO per command.

Yishai

Quoted 10 lines
Quoted 10 lines

Assuming that we'll put each command inside virtio as the generic layer, we won't be able to call/use this API internally to get the PF as of cyclic dependencies between the modules, link will fail.

So in option #3 we may still need to get outside into VFIO the VIRTIO PF and give it as pointer to VIRTIO upon each command.

Does it work for you ?

Yishai

_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization

#50 lore raw

Re: [PATCH vfio 10/11] vfio/virtio: Expose admin commands over virtio device

Quoted 4 lines · Yishai Hadas
Quoted 4 lines
Quoted 4 lines

Assuming that we'll put each command inside virtio as the generic layer, we won't be able to call/use this API internally to get the PF as of cyclic dependencies between the modules, link will fail.

I just mean:
virtio_admin_legacy_io_write(sruct pci_device *, ....)

internally it starts from vf gets the pf (or vf itself or whatever the transport is) sends command gets status returns.

what is cyclic here?

Signature · 6 lines
MST

_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization
author maintainer
#51 lore raw

Re: [PATCH vfio 10/11] vfio/virtio: Expose admin commands over virtio device

Quoted 4 lines · Michael S. Tsirkin
Quoted 3 lines · Yishai Hadas
Quoted 3 lines
Quoted 3 lines

Assuming that we'll put each command inside virtio as the generic layer, we won't be able to call/use this API internally to get the PF as of cyclic dependencies between the modules, link will fail.

Quoted 9 lines

I just mean:
virtio_admin_legacy_io_write(sruct pci_device *, ....)

internally it starts from vf gets the pf (or vf itself or whatever the transport is) sends command gets status returns.

what is cyclic here?

virtio-pci depends on virtio [1].

If we put the commands in the generic layer as we expect it to be (i.e. virtio), then trying to call internally call for virtio_pci_vf_get_pf_dev() to get the PF from the VF will end-up by a linker cyclic error as of below [2].

As of that, someone can suggest to put the commands in virtio-pci, however this will fully bypass the generic layer of virtio and future clients won't be able to use it.

In addition, passing in the VF PCI pointer instead of the VF group member ID + the VIRTIO PF device, will require in the future to duplicate each command once we'll use SIOV devices.

Instead, we suggest the below API for the above example.

virtio_admin_legacy_io_write(virtio_device *virtio_dev,  u64 group_member_id,  ....)

[1]

[yishaih@reg-l-vrt-209 linux]$ modinfo virtio-pci
filename: /lib/modules/6.6.0-rc2+/kernel/drivers/virtio/virtio_pci.ko version:        1
license:        GPL
description:    virtio-pci
author:         Anthony Liguori <[email protected]>
srcversion:     7355EAC9408D38891938391
alias:          pci:v00001AF4d*sv*sd*bc*sc*i*
depends: virtio_pci_modern_dev,virtio,virtio_ring,virtio_pci_legacy_dev retpoline:      Y
intree:         Y
name:           virtio_pci
vermagic:       6.6.0-rc2+ SMP preempt mod_unload modversions
parm:           force_legacy:Force legacy mode for transitional virtio 1 devices (bool)

[2]

depmod: ERROR: Cycle detected: virtio -> virtio_pci -> virtio
depmod: ERROR: Found 2 modules in dependency cycles!
make[2]: *** [scripts/Makefile.modinst:128: depmod] Error 1
make[1]: *** [/images/yishaih/src/kernel/linux/Makefile:1821: modules_install] Error 2

Yishai

_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization

#52 lore raw

Re: [PATCH vfio 10/11] vfio/virtio: Expose admin commands over virtio device

Quoted 16 lines · Yishai Hadas
Quoted 4 lines · Michael S. Tsirkin
Quoted 3 lines · Yishai Hadas
Quoted 3 lines
Quoted 3 lines

Assuming that we'll put each command inside virtio as the generic layer, we won't be able to call/use this API internally to get the PF as of cyclic dependencies between the modules, link will fail.

Quoted 9 lines

I just mean:
virtio_admin_legacy_io_write(sruct pci_device *, ....)

internally it starts from vf gets the pf (or vf itself or whatever the transport is) sends command gets status returns.

what is cyclic here?

virtio-pci depends on virtio [1].

Quoted 8 lines

If we put the commands in the generic layer as we expect it to be (i.e. virtio), then trying to call internally call for virtio_pci_vf_get_pf_dev() to get the PF from the VF will end-up by a linker cyclic error as of below [2].

As of that, someone can suggest to put the commands in virtio-pci, however this will fully bypass the generic layer of virtio and future clients won't be able to use it.

virtio_pci would get pci device.
virtio pci convers that to virtio device of owner + group member id and calls virtio.

no cycles and minimal transport specific code, right?

Quoted 3 lines

In addition, passing in the VF PCI pointer instead of the VF group member ID
+ the VIRTIO PF device, will require in the future to duplicate each command once we'll use SIOV devices.

I don't think anyone knows how will SIOV look. But shuffling APIs around is not a big deal. We'll see.

Quoted 25 lines

Instead, we suggest the below API for the above example.

virtio_admin_legacy_io_write(virtio_device *virtio_dev,  u64 group_member_id,  ....)

[1]

[yishaih@reg-l-vrt-209 linux]$ modinfo virtio-pci
filename: /lib/modules/6.6.0-rc2+/kernel/drivers/virtio/virtio_pci.ko version:        1
license:        GPL
description:    virtio-pci
author:         Anthony Liguori <[email protected]> srcversion:     7355EAC9408D38891938391
alias:          pci:v00001AF4d*sv*sd*bc*sc*i*
depends: virtio_pci_modern_dev,virtio,virtio_ring,virtio_pci_legacy_dev retpoline:      Y
intree:         Y
name:           virtio_pci
vermagic:       6.6.0-rc2+ SMP preempt mod_unload modversions parm:           force_legacy:Force legacy mode for transitional virtio 1 devices (bool)

[2]

Quoted 7 lines

depmod: ERROR: Cycle detected: virtio -> virtio_pci -> virtio
depmod: ERROR: Found 2 modules in dependency cycles! make[2]: *** [scripts/Makefile.modinst:128: depmod] Error 1 make[1]: *** [/images/yishaih/src/kernel/linux/Makefile:1821:
modules_install] Error 2

Yishai

virtio absolutely must not depend on virtio pci, it is used on systems without pci at all.

_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization

author maintainer
#53 lore raw

Re: [PATCH vfio 10/11] vfio/virtio: Expose admin commands over virtio device

Quoted 22 lines · Michael S. Tsirkin
Quoted 21 lines · Yishai Hadas
Quoted 4 lines · Michael S. Tsirkin
Quoted 3 lines · Yishai Hadas
Quoted 3 lines
Quoted 3 lines

Assuming that we'll put each command inside virtio as the generic layer, we won't be able to call/use this API internally to get the PF as of cyclic dependencies between the modules, link will fail.

Quoted 9 lines

I just mean:
virtio_admin_legacy_io_write(sruct pci_device *, ....)

internally it starts from vf gets the pf (or vf itself or whatever the transport is) sends command gets status returns.

what is cyclic here?

virtio-pci depends on virtio [1].

If we put the commands in the generic layer as we expect it to be (i.e. virtio), then trying to call internally call for virtio_pci_vf_get_pf_dev() to get the PF from the VF will end-up by a linker cyclic error as of below [2].

Quoted 5 lines
Quoted 3 lines

As of that, someone can suggest to put the commands in virtio-pci, however this will fully bypass the generic layer of virtio and future clients won't be able to use it.

virtio_pci would get pci device.
virtio pci convers that to virtio device of owner + group member id and calls virtio.

Do you suggest another set of exported symbols (i.e per command ) in virtio which will get the owner device + group member + the extra specific command parameters ?

This will end-up duplicating the number of export symbols per command.

Quoted 1 line

no cycles and minimal transport specific code, right?

See my above note, if we may just call virtio without any further work on the command's input, than YES.

If so, virtio will prepare the command by setting the relevant SG lists and other data and finally will call:

vdev->config->exec_admin_cmd(vdev, cmd);

Was that your plan ?

Quoted 6 lines
Quoted 4 lines

In addition, passing in the VF PCI pointer instead of the VF group member ID
+ the VIRTIO PF device, will require in the future to duplicate each command once we'll use SIOV devices.

I don't think anyone knows how will SIOV look. But shuffling APIs around is not a big deal. We'll see.

As you are the maintainer it's up-to-you, just need to consider another further duplication here.

Yishai

Quoted 35 lines
Quoted 25 lines

Instead, we suggest the below API for the above example.

virtio_admin_legacy_io_write(virtio_device *virtio_dev,  u64 group_member_id,  ....)

[1]
[yishaih@reg-l-vrt-209 linux]$ modinfo virtio-pci
filename: /lib/modules/6.6.0-rc2+/kernel/drivers/virtio/virtio_pci.ko version:        1
license:        GPL
description:    virtio-pci
author:         Anthony Liguori <[email protected]> srcversion:     7355EAC9408D38891938391
alias:          pci:v00001AF4d*sv*sd*bc*sc*i*
depends: virtio_pci_modern_dev,virtio,virtio_ring,virtio_pci_legacy_dev retpoline:      Y
intree:         Y
name:           virtio_pci
vermagic:       6.6.0-rc2+ SMP preempt mod_unload modversions parm:           force_legacy:Force legacy mode for transitional virtio 1 devices (bool)

[2]

Quoted 7 lines

depmod: ERROR: Cycle detected: virtio -> virtio_pci -> virtio
depmod: ERROR: Found 2 modules in dependency cycles! make[2]: *** [scripts/Makefile.modinst:128: depmod] Error 1 make[1]: *** [/images/yishaih/src/kernel/linux/Makefile:1821:
modules_install] Error 2

Yishai

virtio absolutely must not depend on virtio pci, it is used on systems without pci at all.

#54 lore raw

Re: [PATCH vfio 10/11] vfio/virtio: Expose admin commands over virtio device

Quoted 26 lines · Yishai Hadas
Quoted 25 lines · Michael S. Tsirkin
Quoted 24 lines · Yishai Hadas
Quoted 4 lines · Michael S. Tsirkin
Quoted 3 lines · Yishai Hadas
Quoted 3 lines
Quoted 3 lines

Assuming that we'll put each command inside virtio as the generic layer, we won't be able to call/use this API internally to get the PF as of cyclic dependencies between the modules, link will fail.

Quoted 9 lines

I just mean:
virtio_admin_legacy_io_write(sruct pci_device *, ....)

internally it starts from vf gets the pf (or vf itself or whatever the transport is) sends command gets status returns.

what is cyclic here?

virtio-pci depends on virtio [1].

If we put the commands in the generic layer as we expect it to be (i.e. virtio), then trying to call internally call for virtio_pci_vf_get_pf_dev() to get the PF from the VF will end-up by a linker cyclic error as of below [2].

As of that, someone can suggest to put the commands in virtio-pci, however this will fully bypass the generic layer of virtio and future clients won't be able to use it.

Quoted 8 lines
Quoted 2 lines

virtio_pci would get pci device.
virtio pci convers that to virtio device of owner + group member id and calls virtio.

Do you suggest another set of exported symbols (i.e per command ) in virtio which will get the owner device + group member + the extra specific command parameters ?

This will end-up duplicating the number of export symbols per command.

Or make them inline.
Or maybe actually even the specific commands should live inside virtio pci they are pci specific after all.

Quoted 11 lines
Quoted 1 line

no cycles and minimal transport specific code, right?

See my above note, if we may just call virtio without any further work on the command's input, than YES.

If so, virtio will prepare the command by setting the relevant SG lists and other data and finally will call:

vdev->config->exec_admin_cmd(vdev, cmd);

Was that your plan ?

is vdev the pf? then it won't support the transport where commands are submitted through bar0 of vf itself.

Quoted 4 lines
Quoted 4 lines
Quoted 4 lines

In addition, passing in the VF PCI pointer instead of the VF group member ID
+ the VIRTIO PF device, will require in the future to duplicate each command once we'll use SIOV devices.

Quoted 43 lines
Quoted 2 lines

I don't think anyone knows how will SIOV look. But shuffling APIs around is not a big deal. We'll see.

As you are the maintainer it's up-to-you, just need to consider another further duplication here.

Yishai

Quoted 35 lines
Quoted 25 lines

Instead, we suggest the below API for the above example.

virtio_admin_legacy_io_write(virtio_device *virtio_dev,  u64 group_member_id,  ....)

[1]
[yishaih@reg-l-vrt-209 linux]$ modinfo virtio-pci
filename: /lib/modules/6.6.0-rc2+/kernel/drivers/virtio/virtio_pci.ko version:        1
license:        GPL
description:    virtio-pci
author:         Anthony Liguori <[email protected]> srcversion:     7355EAC9408D38891938391
alias:          pci:v00001AF4d*sv*sd*bc*sc*i*
depends: virtio_pci_modern_dev,virtio,virtio_ring,virtio_pci_legacy_dev retpoline:      Y
intree:         Y
name:           virtio_pci
vermagic:       6.6.0-rc2+ SMP preempt mod_unload modversions parm:           force_legacy:Force legacy mode for transitional virtio 1 devices (bool)

[2]

Quoted 7 lines

depmod: ERROR: Cycle detected: virtio -> virtio_pci -> virtio
depmod: ERROR: Found 2 modules in dependency cycles! make[2]: *** [scripts/Makefile.modinst:128: depmod] Error 1 make[1]: *** [/images/yishaih/src/kernel/linux/Makefile:1821:
modules_install] Error 2

Yishai

virtio absolutely must not depend on virtio pci, it is used on systems without pci at all.

_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization

author maintainer
#55 lore raw

Re: [PATCH vfio 10/11] vfio/virtio: Expose admin commands over virtio device

Quoted 29 lines · Michael S. Tsirkin
Quoted 28 lines · Yishai Hadas
Quoted 27 lines · Michael S. Tsirkin
Quoted 16 lines · Yishai Hadas
Quoted 4 lines · Michael S. Tsirkin
Quoted 3 lines · Yishai Hadas
Quoted 3 lines
Quoted 3 lines

Assuming that we'll put each command inside virtio as the generic layer, we won't be able to call/use this API internally to get the PF as of cyclic dependencies between the modules, link will fail.

Quoted 9 lines

I just mean:
virtio_admin_legacy_io_write(sruct pci_device *, ....)

internally it starts from vf gets the pf (or vf itself or whatever the transport is) sends command gets status returns.

what is cyclic here?

virtio-pci depends on virtio [1].

Quoted 8 lines

If we put the commands in the generic layer as we expect it to be (i.e. virtio), then trying to call internally call for virtio_pci_vf_get_pf_dev() to get the PF from the VF will end-up by a linker cyclic error as of below [2].

As of that, someone can suggest to put the commands in virtio-pci, however this will fully bypass the generic layer of virtio and future clients won't be able to use it.

virtio_pci would get pci device.
virtio pci convers that to virtio device of owner + group member id and calls virtio.

Quoted 8 lines
Quoted 5 lines

Do you suggest another set of exported symbols (i.e per command ) in virtio which will get the owner device + group member + the extra specific command parameters ?

This will end-up duplicating the number of export symbols per command.

Or make them inline.
Or maybe actually even the specific commands should live inside virtio pci they are pci specific after all.

OK, let's leave them in virtio-pci as you suggested here.

You can see below [1] some scheme of how a specific command will look like.

Few notes:
- virtio_pci_vf_get_pf_dev() will become a static function.

- The commands will be placed inside virtio_pci_common.c and will use locally the above static function to get the owner PF.

- Post of preparing the command we may call directly to vp_avq_cmd_exec() which is part of vfio-pci and not to virtio.

- vp_avq_cmd_exec() will be part of virtio_pci_modern.c as you asked in the ML.

- The AQ creation/destruction will still be called upon probing virtio as was in V0, it will use the underlay config->create/destroy_avq() ops if exist.

- virtio_admin_cmd_exec() won't be exported any more outside virtio, we'll have an exported symbol in virtio-pci per command.

Is the above fine for you ?

By the way, from API namespace POV, are you fine with virtio_admin_legacy_io_write() or maybe let's have '_pci' as part of the name ? (i.e. virtio_pci_admin_legacy_io_write)

[1]

int virtio_admin_legacy_io_write(struct pci_dev *pdev, u16 opcode, u8 offset, u8 size, u8 *buf)
{
    struct virtio_device *virtio_dev = virtio_pci_vf_get_pf_dev(pdev); struct virtio_admin_cmd_legacy_wr_data *in;
    struct virtio_admin_cmd cmd = {};
    struct scatterlist in_sg;
    int ret;
    int vf_id;

    if (!virtio_dev)
        return -ENODEV;

    vf_id = pci_iov_vf_id(pdev);
    if (vf_id < 0)
        return -EINVAL;

    in = kzalloc(sizeof(*in) + size, GFP_KERNEL);
    if (!in)
        return -ENOMEM;

    in->offset = offset; memcpy(in->registers, buf, size); sg_init_one(&in_sg, in, sizeof(*in) + size); cmd.opcode = opcode;
    cmd.group_type = VIRTIO_ADMIN_GROUP_TYPE_SRIOV; cmd.group_member_id = vf_id + 1; cmd.data_sg = &in_sg;
    ret = vp_avq_cmd_exec(virtio_dev, &cmd);

    kfree(in);
    return ret;
} EXPORT_SYMBOL_GPL(virtio_admin_legacy_io_write);

Quoted 5 lines
Quoted 5 lines
Quoted 2 lines

no cycles and minimal transport specific code, right?

See my above note, if we may just call virtio without any further work on the command's input, than YES.

Quoted 8 lines
Quoted 6 lines

If so, virtio will prepare the command by setting the relevant SG lists and other data and finally will call:

vdev->config->exec_admin_cmd(vdev, cmd);

Was that your plan ?

is vdev the pf? then it won't support the transport where commands are submitted through bar0 of vf itself.

Yes, it's a PF.
Based on current spec for the existing admin commands we issue commands only on the PF.

In any case, moving to the above suggested scheme to handle per command and to get the VF PCI as the first argument we now have a full control for any future command.

Yishai

Quoted 44 lines
Quoted 44 lines
Quoted 5 lines
Quoted 3 lines

In addition, passing in the VF PCI pointer instead of the VF group member ID
+ the VIRTIO PF device, will require in the future to duplicate each command once we'll use SIOV devices.

I don't think anyone knows how will SIOV look. But shuffling APIs around is not a big deal. We'll see.

As you are the maintainer it's up-to-you, just need to consider another further duplication here.

Yishai

Quoted 34 lines
Quoted 24 lines

Instead, we suggest the below API for the above example.

virtio_admin_legacy_io_write(virtio_device *virtio_dev,  u64 group_member_id,  ....)

[1]
[yishaih@reg-l-vrt-209 linux]$ modinfo virtio-pci
filename: /lib/modules/6.6.0-rc2+/kernel/drivers/virtio/virtio_pci.ko version:        1
license:        GPL
description:    virtio-pci
author:         Anthony Liguori <[email protected]> srcversion:     7355EAC9408D38891938391
alias:          pci:v00001AF4d*sv*sd*bc*sc*i*
depends: virtio_pci_modern_dev,virtio,virtio_ring,virtio_pci_legacy_dev retpoline:      Y
intree:         Y
name:           virtio_pci
vermagic:       6.6.0-rc2+ SMP preempt mod_unload modversions parm:           force_legacy:Force legacy mode for transitional virtio 1 devices (bool)

[2]

Quoted 7 lines

depmod: ERROR: Cycle detected: virtio -> virtio_pci -> virtio
depmod: ERROR: Found 2 modules in dependency cycles! make[2]: *** [scripts/Makefile.modinst:128: depmod] Error 1 make[1]: *** [/images/yishaih/src/kernel/linux/Makefile:1821:
modules_install] Error 2

Yishai

virtio absolutely must not depend on virtio pci, it is used on systems without pci at all.

#56 lore raw

Re: [PATCH vfio 10/11] vfio/virtio: Expose admin commands over virtio device

In reply to Yishai Hadas
Quoted 5 lines · Yishai Hadas
Quoted 4 lines

I suggest 3 but call it on the VF. commands will switch to PF internally as needed. For example, intel might be interested in exposing admin commands through a memory BAR of VF itself.

The driver who owns the VF is VFIO, it's not a VIRTIO one.

And to loop back into my previous discussion: that's the fundamental problem here. If it is owned by the virtio subsystem, which just calls into vfio we would not have this problem, including the circular loops and exposed APIs.

#57 lore raw

Re: [PATCH vfio 10/11] vfio/virtio: Expose admin commands over virtio device

In reply to Michael S. Tsirkin
Quoted 3 lines · Michael S. Tsirkin

I suggest 3 but call it on the VF. commands will switch to PF internally as needed. For example, intel might be interested in exposing admin commands through a memory BAR of VF itself.

FWIW, we have been pushing back on such things in VFIO, so it will have to be very carefully security justified.

Probably since that is not standard it should just live in under some intel-only vfio driver behavior, not in virtio land.

It is also costly to switch between pf/vf, it should not be done pointlessly on the fast path.

Jason

#58 lore raw

Re: [PATCH vfio 10/11] vfio/virtio: Expose admin commands over virtio device

Quoted 9 lines · Jason Gunthorpe
Quoted 3 lines · Michael S. Tsirkin

I suggest 3 but call it on the VF. commands will switch to PF internally as needed. For example, intel might be interested in exposing admin commands through a memory BAR of VF itself.

FWIW, we have been pushing back on such things in VFIO, so it will have to be very carefully security justified.

Quoted 7 lines

Probably since that is not standard it should just live in under some intel-only vfio driver behavior, not in virtio land.

It is also costly to switch between pf/vf, it should not be done pointlessly on the fast path.

Jason

Currently, the switch seems to be just a cast of private data. I am suggesting keeping that cast inside virtio. Why is that expensive?

_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization

#59 lore raw

Re: [PATCH vfio 10/11] vfio/virtio: Expose admin commands over virtio device

Quoted 13 lines · Michael S. Tsirkin
Quoted 12 lines · Jason Gunthorpe
Quoted 3 lines · Michael S. Tsirkin

I suggest 3 but call it on the VF. commands will switch to PF internally as needed. For example, intel might be interested in exposing admin commands through a memory BAR of VF itself.

FWIW, we have been pushing back on such things in VFIO, so it will have to be very carefully security justified.

Probably since that is not standard it should just live in under some intel-only vfio driver behavior, not in virtio land.

Quoted 8 lines
Quoted 4 lines

It is also costly to switch between pf/vf, it should not be done pointlessly on the fast path.

Jason

Currently, the switch seems to be just a cast of private data. I am suggesting keeping that cast inside virtio. Why is that expensive?

pci_iov_get_pf_drvdata() does a bunch of sanity checks and function calls. It was not intended to be used on a fast path.

Jason

#60 lore raw

RE: [PATCH vfio 10/11] vfio/virtio: Expose admin commands over virtio device

Quoted 11 lines

From: Jason Gunthorpe <[email protected]>
Sent: Tuesday, October 10, 2023 9:37 PM

Quoted 6 lines · Michael S. Tsirkin
Quoted 5 lines · Jason Gunthorpe
Quoted 3 lines · Michael S. Tsirkin

I suggest 3 but call it on the VF. commands will switch to PF internally as needed. For example, intel might be interested in exposing admin commands through a memory BAR of VF itself.

If in the future if one does admin command on the VF memory BAR, there is no need of cast either. vfio-virtio-pci driver can do on the pci vf device directly.

(though per VF memory registers would be anti-scale design for real hw; to discuss in other forum).

#61 lore raw

Re: [PATCH vfio 10/11] vfio/virtio: Expose admin commands over virtio device

Quoted 6 lines · Parav Pandit
Quoted 6 lines

From: Jason Gunthorpe <[email protected]>
Sent: Tuesday, October 10, 2023 9:37 PM

Quoted 1 line · Michael S. Tsirkin

On Tue, Oct 10, 2023 at 12:59:37PM -0300, Jason Gunthorpe wrote:

Quoted 8 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines · Michael S. Tsirkin
Quoted 4 lines

I suggest 3 but call it on the VF. commands will switch to PF internally as needed. For example, intel might be interested in exposing admin commands through a memory BAR of VF itself.

If in the future if one does admin command on the VF memory BAR, there is no need of cast either. vfio-virtio-pci driver can do on the pci vf device directly.

this is why I want the API to get the VF pci device as a parameter. I don't get what is cyclic about it, yet.

Quoted 1 line

(though per VF memory registers would be anti-scale design for real hw; to discuss in other forum).

up to hardware vendor really.

#62 lore raw

Re: [PATCH vfio 10/11] vfio/virtio: Expose admin commands over virtio device

In reply to Jason Gunthorpe
Quoted 11 lines · Jason Gunthorpe
Quoted 3 lines · Michael S. Tsirkin

I suggest 3 but call it on the VF. commands will switch to PF internally as needed. For example, intel might be interested in exposing admin commands through a memory BAR of VF itself.

FWIW, we have been pushing back on such things in VFIO, so it will have to be very carefully security justified.

Probably since that is not standard it should just live in under some intel-only vfio driver behavior, not in virtio land.

Btw, what is that intel thing everyone is talking about? And why would the virtio core support vendor specific behavior like that?

#63 lore raw

Re: [PATCH vfio 10/11] vfio/virtio: Expose admin commands over virtio device

Quoted 7 lines · Christoph Hellwig
Quoted 6 lines · Jason Gunthorpe
Quoted 3 lines · Michael S. Tsirkin

I suggest 3 but call it on the VF. commands will switch to PF internally as needed. For example, intel might be interested in exposing admin commands through a memory BAR of VF itself.

Quoted 8 lines
Quoted 5 lines

FWIW, we have been pushing back on such things in VFIO, so it will have to be very carefully security justified.

Probably since that is not standard it should just live in under some intel-only vfio driver behavior, not in virtio land.

Btw, what is that intel thing everyone is talking about? And why would the virtio core support vendor specific behavior like that?

It's not a thing it's Zhu Lingshan :) intel is just one of the vendors that implemented vdpa support and so Zhu Lingshan from intel is working on vdpa and has also proposed virtio spec extensions for migration. intel's driver is called ifcvf. vdpa composes all this stuff that is added to vfio in userspace, so it's a different approach.

Signature · 6 lines
MST

_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization
#64 lore raw

Re: [PATCH vfio 10/11] vfio/virtio: Expose admin commands over virtio device

Quoted 8 lines · Michael S. Tsirkin
Quoted 2 lines

Btw, what is that intel thing everyone is talking about? And why would the virtio core support vendor specific behavior like that?

It's not a thing it's Zhu Lingshan :) intel is just one of the vendors that implemented vdpa support and so Zhu Lingshan from intel is working on vdpa and has also proposed virtio spec extensions for migration. intel's driver is called ifcvf. vdpa composes all this stuff that is added to vfio in userspace, so it's a different approach.

Well, so let's call it virtio live migration instead of intel.

And please work all together in the virtio committee that you have one way of communication between controlling and controlled functions. If one extension does it one way and the other a different way that's just creating a giant mess.

#65 lore raw

RE: [PATCH vfio 10/11] vfio/virtio: Expose admin commands over virtio device

Hi Christoph,

Quoted 13 lines

From: Christoph Hellwig <[email protected]>
Sent: Wednesday, October 11, 2023 12:29 PM

Quoted 8 lines · Michael S. Tsirkin
Quoted 2 lines

Btw, what is that intel thing everyone is talking about? And why would the virtio core support vendor specific behavior like that?

It's not a thing it's Zhu Lingshan :) intel is just one of the vendors that implemented vdpa support and so Zhu Lingshan from intel is working on vdpa and has also proposed virtio spec extensions for migration. intel's driver is called ifcvf. vdpa composes all this stuff that is added to vfio in userspace, so it's a different approach.

Quoted 6 lines

Well, so let's call it virtio live migration instead of intel.

And please work all together in the virtio committee that you have one way of communication between controlling and controlled functions.
If one extension does it one way and the other a different way that's just creating a giant mess.

We in virtio committee are working on VF device migration where:
VF = controlled function
PF = controlling function

The second proposal is what Michael mentioned from Intel that somehow combine controlled and controlling function as single entity on VF.

The main reasons I find it weird are:
1. it must always need to do mediation to do fake the device reset, and flr flows
2. dma cannot work as you explained for complex device state
3. it needs constant knowledge of each tiny things for each virtio device type

Such single entity appears a bit very weird to me but maybe it is just me.

#66 lore raw

Re: [PATCH vfio 10/11] vfio/virtio: Expose admin commands over virtio device

Quoted 26 lines · Parav Pandit

Hi Christoph,

Quoted 13 lines

From: Christoph Hellwig <[email protected]>
Sent: Wednesday, October 11, 2023 12:29 PM

Quoted 8 lines · Michael S. Tsirkin
Quoted 2 lines

Btw, what is that intel thing everyone is talking about? And why would the virtio core support vendor specific behavior like that?

It's not a thing it's Zhu Lingshan :) intel is just one of the vendors that implemented vdpa support and so Zhu Lingshan from intel is working on vdpa and has also proposed virtio spec extensions for migration. intel's driver is called ifcvf. vdpa composes all this stuff that is added to vfio in userspace, so it's a different approach.

Quoted 6 lines

Well, so let's call it virtio live migration instead of intel.

And please work all together in the virtio committee that you have one way of communication between controlling and controlled functions.
If one extension does it one way and the other a different way that's just creating a giant mess.

We in virtio committee are working on VF device migration where:
VF = controlled function
PF = controlling function

Quoted 8 lines

The second proposal is what Michael mentioned from Intel that somehow combine controlled and controlling function as single entity on VF.

The main reasons I find it weird are:
1. it must always need to do mediation to do fake the device reset, and flr flows
2. dma cannot work as you explained for complex device state
3. it needs constant knowledge of each tiny things for each virtio device type

Such single entity appears a bit very weird to me but maybe it is just me.

Yea it appears to include everyone from nvidia. Others are used to it - this is exactly what happens with virtio generally. E.g. vhost processes fast path in the kernel and control path is in userspace. vdpa has been largely modeled after that, for better or worse.

Signature · 6 lines
MST

_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization
#67 lore raw

Re: [PATCH vfio 10/11] vfio/virtio: Expose admin commands over virtio device

Quoted 34 lines · Michael S. Tsirkin
Quoted 33 lines · Parav Pandit

Hi Christoph,

Quoted 13 lines

From: Christoph Hellwig <[email protected]>
Sent: Wednesday, October 11, 2023 12:29 PM

Quoted 8 lines · Michael S. Tsirkin
Quoted 2 lines

Btw, what is that intel thing everyone is talking about? And why would the virtio core support vendor specific behavior like that?

It's not a thing it's Zhu Lingshan :) intel is just one of the vendors that implemented vdpa support and so Zhu Lingshan from intel is working on vdpa and has also proposed virtio spec extensions for migration. intel's driver is called ifcvf. vdpa composes all this stuff that is added to vfio in userspace, so it's a different approach.

Quoted 6 lines

Well, so let's call it virtio live migration instead of intel.

And please work all together in the virtio committee that you have one way of communication between controlling and controlled functions.
If one extension does it one way and the other a different way that's just creating a giant mess.

We in virtio committee are working on VF device migration where:
VF = controlled function
PF = controlling function

The second proposal is what Michael mentioned from Intel that somehow combine controlled and controlling function as single entity on VF.

The main reasons I find it weird are:
1. it must always need to do mediation to do fake the device reset, and flr flows
2. dma cannot work as you explained for complex device state
3. it needs constant knowledge of each tiny things for each virtio device type

Quoted 6 lines
Quoted 1 line

Such single entity appears a bit very weird to me but maybe it is just me.

Yea it appears to include everyone from nvidia. Others are used to it - this is exactly what happens with virtio generally. E.g. vhost processes fast path in the kernel and control path is in userspace. vdpa has been largely modeled after that, for better or worse.

As Parav says, you can't use DMA for any migration flows, and you open a single VF scheme up to PCI P2P attacks from the VM. It is a pretty bad design.

vfio reviewers will reject things like this that are not secure - we just did for Intel E800, for instance.

With VDPA doing the same stuff as vfio I'm not sure who is auditing it for security.

The simple way to be sure is to never touch the PCI function that has DMA assigned to a VM from the hypervisor, except through config space.

Beyond that.. Well, think carefully about security.

IMHO the single-VF approach is not suitable for standardization.

Jason

#68 lore raw

Re: [PATCH vfio 10/11] vfio/virtio: Expose admin commands over virtio device

Quoted 2 lines · Jason Gunthorpe

The simple way to be sure is to never touch the PCI function that has DMA assigned to a VM from the hypervisor, except through config space.

What makes config space different that it's safe though? Isn't this more of a "we can't avoid touching config space" than that it's safe? The line doesn't look that bright to me - if there's e.g. a memory area designed explicitly for hypervisor to poke at, that seems fine.

Signature · 6 lines
MST

_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization
#69 lore raw

Re: [PATCH vfio 10/11] vfio/virtio: Expose admin commands over virtio device

Quoted 5 lines · Michael S. Tsirkin
Quoted 2 lines · Jason Gunthorpe

The simple way to be sure is to never touch the PCI function that has DMA assigned to a VM from the hypervisor, except through config space.

What makes config space different that it's safe though?

Hypervisor fully mediates it and it is not accessible to P2P attacks.

Quoted 4 lines

Isn't this more of a "we can't avoid touching config space" than that it's safe? The line doesn't look that bright to me - if there's e.g. a memory area designed explicitly for hypervisor to poke at, that seems fine.

It is not.

Jason

#70 lore raw

Re: [PATCH vfio 10/11] vfio/virtio: Expose admin commands over virtio device

In reply to Jason Gunthorpe
Quoted 2 lines · Jason Gunthorpe

With VDPA doing the same stuff as vfio I'm not sure who is auditing it for security.

Check the signed off tags and who sends the pull requests if you want to know.

Signature · 6 lines
MST

_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization
#71 lore raw

Re: [PATCH vfio 10/11] vfio/virtio: Expose admin commands over virtio device

In reply to Parav Pandit
Quoted 23 lines · Parav Pandit via Virtualization

Hi Christoph,

Quoted 11 lines

From: Christoph Hellwig <[email protected]>
Sent: Wednesday, October 11, 2023 12:29 PM

Quoted 7 lines · Michael S. Tsirkin
Quoted 2 lines

Btw, what is that intel thing everyone is talking about? And why would the virtio core support vendor specific behavior like that?

It's not a thing it's Zhu Lingshan :) intel is just one of the vendors that implemented vdpa support and so Zhu Lingshan from intel is working on vdpa and has also proposed virtio spec extensions for migration. intel's driver is called ifcvf. vdpa composes all this stuff that is added to vfio in userspace, so it's a different approach.

Quoted 6 lines

Well, so let's call it virtio live migration instead of intel.

And please work all together in the virtio committee that you have one way of communication between controlling and controlled functions.
If one extension does it one way and the other a different way that's just creating a giant mess.

We in virtio committee are working on VF device migration where:
VF = controlled function
PF = controlling function

Quoted 8 lines

The second proposal is what Michael mentioned from Intel that somehow combine controlled and controlling function as single entity on VF.

The main reasons I find it weird are:
1. it must always need to do mediation to do fake the device reset, and flr flows
2. dma cannot work as you explained for complex device state
3. it needs constant knowledge of each tiny things for each virtio device type

Such single entity appears a bit very weird to me but maybe it is just me.

sorry for the late reply, we have discussed this for weeks in virtio mailing list. I have proposed a live migration solution which is a config space solution.

We(me, Jason and Eugenio) have been working on this solution for more than two years and we are implementing virtio live migration basic facilities.

The implementation is transport specific, e.g., for PCI we implement new or extend registers which work as other config space registers do.

The reason we are arguing is:
I am not sure admin vq based live migration solution is a good choice, because:
1) it does not work for nested
2) it does not work for bare metal
3) QOS problem
4) security leaks.

Sorry to span the discussions here.

Thanks,
Zhu Lingshan

Quoted 4 lines

_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization

_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization

#72 lore raw

Re: [PATCH vfio 10/11] vfio/virtio: Expose admin commands over virtio device

Quoted 2 lines · Lingshan

sorry for the late reply, we have discussed this for weeks in virtio mailing list. I have proposed a live migration solution which is a config space solution.

I'm sorry that can't be a serious proposal - config space can't do DMA, it is not suitable.

Jason

#73 lore raw

Re: [PATCH vfio 10/11] vfio/virtio: Expose admin commands over virtio device

Quoted 6 lines · Jason Gunthorpe
Quoted 2 lines · Lingshan

sorry for the late reply, we have discussed this for weeks in virtio mailing list. I have proposed a live migration solution which is a config space solution.

I'm sorry that can't be a serious proposal - config space can't do DMA, it is not suitable.

config space only controls the live migration process and config the related facilities. We don't use config space to transfer data.

The new added registers work like queue_enable or features.

For example, we use DMA to report dirty pages and MMIO to fetch the dirty data.

I remember in another thread you said:"you can't use DMA for any migration flows"

And I agree to that statement, so we use config space registers to control the flow.

Thanks,
Zhu Lingshan

Quoted 2 lines

Jason

_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization

#74 lore raw

Re: [PATCH vfio 10/11] vfio/virtio: Expose admin commands over virtio device

Quoted 24 lines · Lingshan

On 10/12/2023 9:27 PM, Jason Gunthorpe wrote:

On Thu, Oct 12, 2023 at 06:29:47PM +0800, Zhu, Lingshan wrote:

sorry for the late reply, we have discussed this for weeks in virtio mailing list. I have proposed a live migration solution which is a config space solution.

I'm sorry that can't be a serious proposal - config space can't do DMA, it is not suitable.

config space only controls the live migration process and config the related facilities.
We don't use config space to transfer data.

The new added registers work like queue_enable or features.

For example, we use DMA to report dirty pages and MMIO to fetch the dirty data.

I remember in another thread you said:"you can't use DMA for any migration flows"

Quoted 9 lines

And I agree to that statement, so we use config space registers to control the flow.

Thanks,
Zhu Lingshan

Jason

If you are using dma then I don't see what's wrong with admin vq. dma is all it does.

_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization

#75 lore raw

Re: [PATCH vfio 10/11] vfio/virtio: Expose admin commands over virtio device

Quoted 27 lines · Michael S. Tsirkin
Quoted 26 lines · Lingshan

On 10/12/2023 9:27 PM, Jason Gunthorpe wrote:

On Thu, Oct 12, 2023 at 06:29:47PM +0800, Zhu, Lingshan wrote:

sorry for the late reply, we have discussed this for weeks in virtio mailing list. I have proposed a live migration solution which is a config space solution.

I'm sorry that can't be a serious proposal - config space can't do DMA, it is not suitable.

config space only controls the live migration process and config the related facilities.
We don't use config space to transfer data.

The new added registers work like queue_enable or features.

For example, we use DMA to report dirty pages and MMIO to fetch the dirty data.

I remember in another thread you said:"you can't use DMA for any migration flows"

And I agree to that statement, so we use config space registers to control the flow.

Quoted 8 lines
Quoted 6 lines

Thanks,
Zhu Lingshan

Jason

If you are using dma then I don't see what's wrong with admin vq. dma is all it does.

dma != admin vq,

and I think we have discussed many details in pros and cons
in admin vq live migration proposal in virtio-comment.
I am not sure we should span the discussions here, repeat them over again.

Thanks

Quoted 1 line
#76 lore raw

Re: [PATCH vfio 10/11] vfio/virtio: Expose admin commands over virtio device

Quoted 34 lines · Lingshan
Quoted 31 lines · Michael S. Tsirkin
Quoted 30 lines · Lingshan

On 10/12/2023 9:27 PM, Jason Gunthorpe wrote:

On Thu, Oct 12, 2023 at 06:29:47PM +0800, Zhu, Lingshan wrote:

sorry for the late reply, we have discussed this for weeks in virtio mailing list. I have proposed a live migration solution which is a config space solution.

I'm sorry that can't be a serious proposal - config space can't do DMA, it is not suitable.

config space only controls the live migration process and config the related facilities.
We don't use config space to transfer data.

The new added registers work like queue_enable or features.

For example, we use DMA to report dirty pages and MMIO to fetch the dirty data.

I remember in another thread you said:"you can't use DMA for any migration flows"

And I agree to that statement, so we use config space registers to control the flow.

Thanks,
Zhu Lingshan

Quoted 5 lines
Quoted 4 lines
Quoted 2 lines

Jason

If you are using dma then I don't see what's wrong with admin vq. dma is all it does.

dma != admin vq,

Well they share the same issue that they don't work for nesting because DMA can not be intercepted.

Quoted 6 lines

and I think we have discussed many details in pros and cons
in admin vq live migration proposal in virtio-comment.
I am not sure we should span the discussions here, repeat them over again.

Thanks

Quoted 1 line

Yea let's not.

Signature · 6 lines
MST

_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization
#77 lore raw

Re: [PATCH vfio 10/11] vfio/virtio: Expose admin commands over virtio device

Quoted 33 lines · Michael S. Tsirkin
Quoted 32 lines · Lingshan
Quoted 30 lines · Michael S. Tsirkin
Quoted 29 lines · Lingshan

On 10/12/2023 9:27 PM, Jason Gunthorpe wrote:

On Thu, Oct 12, 2023 at 06:29:47PM +0800, Zhu, Lingshan wrote:

sorry for the late reply, we have discussed this for weeks in virtio mailing list. I have proposed a live migration solution which is a config space solution.

I'm sorry that can't be a serious proposal - config space can't do DMA, it is not suitable.

config space only controls the live migration process and config the related facilities.
We don't use config space to transfer data.

The new added registers work like queue_enable or features.

For example, we use DMA to report dirty pages and MMIO to fetch the dirty data.

I remember in another thread you said:"you can't use DMA for any migration flows"

And I agree to that statement, so we use config space registers to control the flow.

Thanks,
Zhu Lingshan

Quoted 7 lines
Quoted 5 lines
Quoted 4 lines
Quoted 2 lines

Jason

If you are using dma then I don't see what's wrong with admin vq. dma is all it does.

dma != admin vq,

Well they share the same issue that they don't work for nesting because DMA can not be intercepted.

(hope this is not a spam to virtualization list and I try to keep this short) only use dma for host memory access, e.g., dirty page bitmap, no need to intercepted.

Quoted 8 lines
Quoted 6 lines

and I think we have discussed many details in pros and cons
in admin vq live migration proposal in virtio-comment.
I am not sure we should span the discussions here, repeat them over again.

Thanks

Yea let's not.

#78 lore raw

Re: [PATCH vfio 10/11] vfio/virtio: Expose admin commands over virtio device

In reply to Christoph Hellwig
Quoted 10 lines · Christoph Hellwig
Quoted 8 lines · Michael S. Tsirkin
Quoted 2 lines

Btw, what is that intel thing everyone is talking about? And why would the virtio core support vendor specific behavior like that?

It's not a thing it's Zhu Lingshan :) intel is just one of the vendors that implemented vdpa support and so Zhu Lingshan from intel is working on vdpa and has also proposed virtio spec extensions for migration. intel's driver is called ifcvf. vdpa composes all this stuff that is added to vfio in userspace, so it's a different approach.

Quoted 6 lines

Well, so let's call it virtio live migration instead of intel.

And please work all together in the virtio committee that you have one way of communication between controlling and controlled functions. If one extension does it one way and the other a different way that's just creating a giant mess.

Absolutely, this is exactly what I keep suggesting. Thanks for bringing this up, will help me drive the point home!

Signature · 6 lines
MST

_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization
#79 lore raw

Re: [PATCH vfio 10/11] vfio/virtio: Expose admin commands over virtio device

In reply to Christoph Hellwig
Quoted 8 lines · Christoph Hellwig
Quoted 7 lines · Michael S. Tsirkin
Quoted 2 lines

Btw, what is that intel thing everyone is talking about? And why would the virtio core support vendor specific behavior like that?

It's not a thing it's Zhu Lingshan :) intel is just one of the vendors that implemented vdpa support and so Zhu Lingshan from intel is working on vdpa and has also proposed virtio spec extensions for migration. intel's driver is called ifcvf. vdpa composes all this stuff that is added to vfio in userspace, so it's a different approach.

Quoted 6 lines

Well, so let's call it virtio live migration instead of intel.

And please work all together in the virtio committee that you have one way of communication between controlling and controlled functions. If one extension does it one way and the other a different way that's just creating a giant mess.

I hope so, Jason Wang has proposed a solution to cooperate, but sadly rejected...

Quoted 5 lines

_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization

#80 lore raw

Re: [PATCH vfio 10/11] vfio/virtio: Expose admin commands over virtio device

In reply to Jason Gunthorpe
Quoted 4 lines · Jason Gunthorpe

We've talked around ideas like allowing the VF config space to do some of the work. For simple devices we could get away with 1 VF config space register. (VF config space is owned by the hypervisor, not the guest)

Which assumes you're actually using VFs and not multiple PFs, which is a very limiting assumption. It also limits your from actually using DMA during the live migration process, which again is major limitation once you have a non-tivial amount of state.

Quoted 3 lines

SIOVr2 is discussing more a flexible RID mapping - there is a possible route where a "VF" could actually have two RIDs, a hypervisor RID and a guest RID.

Well, then you go down the SIOV route, which requires a complex driver actually presenting the guest visible device anyway.

Quoted 11 lines

It really is PCI limitations that force this design of making a PF driver do dual duty as a fully functionally normal device and act as a communication channel proxy to make a back channel into a SRIOV VF.

My view has always been that the VFIO live migration operations are executed logically within the VF as they only effect the VF.

So we have a logical design seperation where VFIO world owns the commands and the PF driver supplies the communication channel. This works well for devices that already have a robust RPC interface to their device FW.

Independent of my above points on the doubts on VF-controlled live migration for PCe device I absolutely agree with your that the Linux abstraction and user interface should be VF based. Which further reinforeces my point that the VFIO driver for the controlled function (PF or VF) and the Linux driver for the controlling function (better be a PF in practice) must be very tightly integrated. And the best way to do that is to export the vfio nodes from the Linux driver that knowns the hardware and not split out into a separate one.

Quoted 6 lines
Quoted 2 lines

The driver that knows this hardware. In this case the virtio subsystem, in case of nvme the nvme driver, and in case of mlx5 the mlx5 driver.

But those are drivers operating the HW to create kernel devices. Here we need a VFIO device. They can't co-exist, if you switch mlx5 from normal to vfio you have to tear down the entire normal driver.

Yes, absolutey. And if we're smart enough we structure it in a way that we never even initialize the bits of the driver only needed for the normal kernel consumers.

Quoted 5 lines
Quoted 2 lines

No. That layout logically follows from what codebase the functionality is part of, though.

I don't understand what we are talking about really. Where do you imagine the vfio_register_XX() goes?

In the driver controlling the hardware. E.g. for virtio in driver/virtio/ and for nvme in drivers/nvme/ and for mlx5 in the mlx5 driver directory.

Quoted 9 lines
Quoted 4 lines
Quoted 1 line

I don't know what "fake-legacy" even means, VFIO is not legacy.

The driver we're talking about in this thread fakes up a virtio_pci legacy devie to the guest on top of a "modern" virtio_pci device.

I'm not sure I'd use the word fake, inb/outb are always trapped operations in VMs. If the device provided a real IO BAR then VFIO common code would trap and relay inb/outb to the device.

Quoted 5 lines

All this is doing is changing the inb/outb relay from using a physical IO BAR to a DMA command ring.

The motivation is simply because normal IO BAR space is incredibly limited and you can't get enough SRIOV functions when using it.

The fake is not meant as a judgement. But it creates a virtio-legacy device that in this form does not exist in hardware. That's what I call fake. If you prefer a different term that's fine with me too.

Quoted 8 lines
Quoted 7 lines
Quoted 3 lines

There is alot of code in VFIO and the VMM side to take a VF and turn it into a vPCI function. You can't just trivially duplicate VFIO in a dozen drivers without creating a giant mess.

I do not advocate for duplicating it. But the code that calls this functionality belongs into the driver that deals with the compound device that we're doing this work for.

Quoted 6 lines

On one hand, I don't really care - we can put the code where people like.

However - the Intel GPU VFIO driver is such a bad experiance I don't want to encourage people to make VFIO drivers, or code that is only used by VFIO drivers, that are not under drivers/vfio review.

We can and should require vfio review for users of the vfio API. But to be honest code placement was not the problem with i915. The problem was that the mdev APIs (under drivers/vfio) were a complete trainwreck when it was written, and that the driver had a horrible hypervisor API abstraction.

Quoted 4 lines

Be aware, there is a significant performance concern here. If you want to create 1000 VFIO devices (this is a real thing), we *can't* probe a normal driver first, it is too slow. We need a path that goes directly from creating the RIDs to turning those RIDs into VFIO.

And by calling the vfio funtions from mlx5 you get this easily.

But I think you're totally mixing things up here anyway.

For mdev/SIOV like flows you must call vfio APIs from the main driver anyway, as there is no pci_dev to probe on anyway. That's what i915 does btw.

For "classic" vfio that requires a pci_dev (or $otherbus_dev) we need to have a similar flow. And I think the best way is to have the bus-level attribute on the device and/or a device-specific side band protocol to device how new functions are probed. With that you avoid all the duplicate PCI IDs for the binding, and actually allow to sanely establush a communication channel between the functions. Because without that there is no way to know how any two functions related. The driver might think they know, but there's all kinds of whacky PCI passthough schemes that will break such a logic.

#81 lore raw

Re: [PATCH vfio 10/11] vfio/virtio: Expose admin commands over virtio device

Quoted 8 lines · Christoph Hellwig
Quoted 4 lines · Jason Gunthorpe

We've talked around ideas like allowing the VF config space to do some of the work. For simple devices we could get away with 1 VF config space register. (VF config space is owned by the hypervisor, not the guest)

Which assumes you're actually using VFs and not multiple PFs, which is a very limiting assumption.

? It doesn't matter VF/PF, the same functions config space could do simple migration.

Quoted 3 lines

It also limits your from actually
using DMA during the live migration process, which again is major limitation once you have a non-tivial amount of state.

Yes, this is a dealbreaker for big cases. But we do see several smaller/simpler devices that don't use DMA in their migration.

Quoted 6 lines
Quoted 3 lines

SIOVr2 is discussing more a flexible RID mapping - there is a possible route where a "VF" could actually have two RIDs, a hypervisor RID and a guest RID.

Well, then you go down the SIOV route, which requires a complex driver actually presenting the guest visible device anyway.

Yep

Quoted 8 lines

Independent of my above points on the doubts on VF-controlled live migration for PCe device I absolutely agree with your that the Linux abstraction and user interface should be VF based. Which further reinforeces my point that the VFIO driver for the controlled function (PF or VF) and the Linux driver for the controlling function (better be a PF in practice) must be very tightly integrated. And the best way to do that is to export the vfio nodes from the Linux driver that knowns the hardware and not split out into a separate one.

I'm not sure how we get to "very tightly integrated". We have many examples of live migration vfio drivers now and they do not seem to require tight integration. The PF driver only has to provide a way to execute a small number of proxied operations.

Regardless, I'm not too fussed about what directory the implementation lives in, though I do prefer the current arrangement where VFIO only stuff is in drivers/vfio. I like the process we have where subsystems are responsible for the code that implements the subsystem ops.

Quoted 9 lines
Quoted 3 lines

However - the Intel GPU VFIO driver is such a bad experiance I don't want to encourage people to make VFIO drivers, or code that is only used by VFIO drivers, that are not under drivers/vfio review.

We can and should require vfio review for users of the vfio API. But to be honest code placement was not the problem with i915. The problem was that the mdev APIs (under drivers/vfio) were a complete trainwreck when it was written, and that the driver had a horrible hypervisor API abstraction.

E800 also made some significant security mistakes that VFIO side caught. I think would have been missed if it went into a netdev tree.

Even unrelated to mdev, Intel GPU is still not using the vfio side properly, and the way it hacked into KVM to try to get page tracking is totally logically wrong (but Works For Me (tm))

Aside from technical concerns, I do have a big process worry here. vfio is responsible for the security side of the review of things implementing its ops.

Quoted 6 lines
Quoted 4 lines

Be aware, there is a significant performance concern here. If you want to create 1000 VFIO devices (this is a real thing), we *can't* probe a normal driver first, it is too slow. We need a path that goes directly from creating the RIDs to turning those RIDs into VFIO.

And by calling the vfio funtions from mlx5 you get this easily.

"easily" I don't know about that :)

Quoted 3 lines

For mdev/SIOV like flows you must call vfio APIs from the main driver anyway, as there is no pci_dev to probe on anyway. That's what i915 does btw.

IMHO i915 is not an good example to copy.

mlx5 is already much closer to your ideal, and I would hold up as the right general direction for SIOV/mdev/etc, as we basically already do a lot of SIOV ideas.

mlx5 is a multi-subsystem device. It has driver components in net, VDPA and infiniband. It can create non-PCI "functions".

It is not feasible, process wise, for all of this to live under one directory. We *want* the driver split up by subystem and subsystem maintainer.

So, we created the auxiliary_device stuff to manage this. It can do what you are imagining, I think.

The core PF/VF driver is in charge of what to carve off to a sub system driver. IIRC mlx5 uses netlink to deliver commands to trigger this (eg create a VDPA device). An auxilary_device is created and the target subsystem driver probes to that and autoloads. eg see drivers/vdpa/mlx5/net/mlx5_vnet.c

They are not 'tightly coupled', the opposite really. The auxilary_device comes along with a mlx5 API that allows all the subsystem to do what they need on the HW mostly independently. For mlx5 this is mostly a way to execute FW RPC commands.

So, if you want to turn the VFIO stuff inside out, I'd still suggest to have the VFIO driver part under drivers/vfio and probe to an auxilary_device that represents the aspect of the HW to turn into VFIO (or VPDA, or whatever). The 'core' driver can provide an appropriate API between its VFIO part and its core part.

We lack a common uAPI to trigger this creation, but otherwise the infrastructure exists and works well now. It allows subsystems to remain together and complex devices to spread their functionality to multiple subsystems.

The current pci_iov_get_pf_drvdata() hack in VFIO is really a short cut to doing the auxilary_device stuff. (actually we tried to build this with auxilary_device first, it did not work out, needs more driver core infastructure).

I can easially imagine all the current VFIO drivers probing to an auxilary_device and obtinaing the VF pci_device and the handle for the core functionalitty directly without the pci_iov_get_pf_drvdata() approach.

Quoted 9 lines

For "classic" vfio that requires a pci_dev (or $otherbus_dev) we need to have a similar flow. And I think the best way is to have the bus-level attribute on the device and/or a device-specific side band protocol to device how new functions are probed. With that you avoid all the duplicate PCI IDs for the binding, and actually allow to sanely establush a communication channel between the functions. Because without that there is no way to know how any two functions related. The driver might think they know, but there's all kinds of whacky PCI passthough schemes that will break such a logic.

Yes, if things are not simple PF/VF then Linux struggles at the driver core level. auxilary_devices are a way out of that since one spot can figure out how to assemble the multi-component device and then delegate portions of the HW to other subsystems.

If something wants to probe its own driver to a PF/VF to assemble the components it can do that and then bundle it up into an aux device and trigger a VFIO/etc driver to run on that bundle of resources.

We don't *need* to put all the VFIO code someplace else to put the control over slicing the HW into a shared core driver. mlx5 and several other drivers now already demonstrates all of this.

Jason

#82 lore raw

Re: [PATCH vfio 10/11] vfio/virtio: Expose admin commands over virtio device

Quoted 9 lines · Jason Gunthorpe
Quoted 8 lines

Independent of my above points on the doubts on VF-controlled live migration for PCe device I absolutely agree with your that the Linux abstraction and user interface should be VF based. Which further reinforeces my point that the VFIO driver for the controlled function (PF or VF) and the Linux driver for the controlling function (better be a PF in practice) must be very tightly integrated. And the best way to do that is to export the vfio nodes from the Linux driver that knowns the hardware and not split out into a separate one.

Quoted 4 lines

I'm not sure how we get to "very tightly integrated". We have many examples of live migration vfio drivers now and they do not seem to require tight integration. The PF driver only has to provide a way to execute a small number of proxied operations.

Yes. And for that I need to know what VF it actually is dealing with. Which is tight integration in my book.

Quoted 4 lines

Regardless, I'm not too fussed about what directory the implementation lives in, though I do prefer the current arrangement where VFIO only stuff is in drivers/vfio. I like the process we have where subsystems are responsible for the code that implements the subsystem ops.

I really don't care about where the code lives (in the directory tree) either. But as you see with virtio trying to split it out into an arbitrary module causes all kinds of pain.

Quoted 5 lines

E800 also made some significant security mistakes that VFIO side caught. I think would have been missed if it went into a netdev tree.

Quoted 7 lines

Even unrelated to mdev, Intel GPU is still not using the vfio side properly, and the way it hacked into KVM to try to get page tracking is totally logically wrong (but Works For Me (tm))

Aside from technical concerns, I do have a big process worry here. vfio is responsible for the security side of the review of things implementing its ops.

Yes, anytjing exposing a vfio node needs vfio review, period. And I don't think where the code lived was the i915 problem. The problem was they they were the first open user of the mdev API, which was just a badly deisgned hook for never published code at that time, and they then shoehorned it into a weird hypervisor abstraction. There's no good way to succeed with that.

#83 lore raw

Re: [PATCH vfio 10/11] vfio/virtio: Expose admin commands over virtio device

Quoted 10 lines · Christoph Hellwig
Quoted 9 lines · Jason Gunthorpe
Quoted 8 lines

Independent of my above points on the doubts on VF-controlled live migration for PCe device I absolutely agree with your that the Linux abstraction and user interface should be VF based. Which further reinforeces my point that the VFIO driver for the controlled function (PF or VF) and the Linux driver for the controlling function (better be a PF in practice) must be very tightly integrated. And the best way to do that is to export the vfio nodes from the Linux driver that knowns the hardware and not split out into a separate one.

Quoted 7 lines
Quoted 4 lines

I'm not sure how we get to "very tightly integrated". We have many examples of live migration vfio drivers now and they do not seem to require tight integration. The PF driver only has to provide a way to execute a small number of proxied operations.

Yes. And for that I need to know what VF it actually is dealing with. Which is tight integration in my book.

Well, I see two modalities here

Simple devices with a fixed PF/VF relationship use a VF pci_device for VFIO and pci_iov_get_pf_drvdata()/related APIs to assemble their parts. This is very limited (and kind of hacky).

Complex devices can use an auxiliary_device for VFIO and assemble their parts however they like.

After probe is done the VFIO code operates effectively identically regardless of how the components were found.

Intel is going to submit their IDXD SIOV driver "soon" and I'd like to pause there and have a real discussion about how to manage VFIO lifecycle and dynamic "function" creation in this brave new world.

Ideally we can get a lifecycle API that works uniformly for PCI VFs too. Then maybe this gets more resolved.

In my mind at least, definately no mdevs and that sysfs GUID junk. :(

Quoted 3 lines

I really don't care about where the code lives (in the directory tree) either. But as you see with virtio trying to split it out into an arbitrary module causes all kinds of pain.

Trying to put VFIO-only code in virtio is what causes all the issues. If you mis-design the API boundary everything will be painful, no matter where you put the code.

Jason

#84 lore raw

Re: [PATCH vfio 10/11] vfio/virtio: Expose admin commands over virtio device

Quoted 3 lines · Jason Gunthorpe

Trying to put VFIO-only code in virtio is what causes all the issues. If you mis-design the API boundary everything will be painful, no matter where you put the code.

Are you implying the whole idea of adding these legacy virtio admin commands to virtio spec was a design mistake?
It was nvidia guys who proposed it, so I'm surprised to hear you say this.

Signature · 6 lines
MST

_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization
#85 lore raw

Re: [PATCH vfio 10/11] vfio/virtio: Expose admin commands over virtio device

Quoted 7 lines · Michael S. Tsirkin
Quoted 3 lines · Jason Gunthorpe

Trying to put VFIO-only code in virtio is what causes all the issues. If you mis-design the API boundary everything will be painful, no matter where you put the code.

Are you implying the whole idea of adding these legacy virtio admin commands to virtio spec was a design mistake?

No, I'm saying again that trying to relocate all the vfio code into drivers/virtio is a mistake

Jason

#86 lore raw

Re: [PATCH vfio 10/11] vfio/virtio: Expose admin commands over virtio device

Quoted 6 lines · Jason Gunthorpe
Quoted 5 lines · Michael S. Tsirkin
Quoted 3 lines · Jason Gunthorpe

Trying to put VFIO-only code in virtio is what causes all the issues. If you mis-design the API boundary everything will be painful, no matter where you put the code.

Quoted 7 lines
Quoted 2 lines

Are you implying the whole idea of adding these legacy virtio admin commands to virtio spec was a design mistake?

No, I'm saying again that trying to relocate all the vfio code into drivers/virtio is a mistake

Jason

Yea please don't. And by the same token, please do not put implementations of virtio spec under drivers/vfio.

Signature · 6 lines
MST

_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization
author maintainer
#87 lore raw

PATCH 11/11 vfio/virtio: Introduce a vfio driver over virtio devices

In reply to Yishai Hadas

Introduce a vfio driver over virtio devices to support the legacy interface functionality for VFs.

Background, from the virtio spec [1].
--------------------------------------------------------------------
In some systems, there is a need to support a virtio legacy driver with a device that does not directly support the legacy interface. In such scenarios, a group owner device can provide the legacy interface functionality for the group member devices. The driver of the owner device can then access the legacy interface of a member device on behalf of the legacy member device driver.

For example, with the SR-IOV group type, group members (VFs) can not present the legacy interface in an I/O BAR in BAR0 as expected by the legacy pci driver. If the legacy driver is running inside a virtual machine, the hypervisor executing the virtual machine can present a virtual device with an I/O BAR in BAR0. The hypervisor intercepts the legacy driver accesses to this I/O BAR and forwards them to the group owner device (PF) using group administration commands.
--------------------------------------------------------------------

Specifically, this driver adds support for a virtio-net VF to be exposed as a transitional device to a guest driver and allows the legacy IO BAR functionality on top.

This allows a VM which uses a legacy virtio-net driver in the guest to work transparently over a VF which its driver in the host is that new driver.

The driver can be extended easily to support some other types of virtio devices (e.g virtio-blk), by adding in a few places the specific type properties as was done for virtio-net.

For now, only the virtio-net use case was tested and as such we introduce the support only for such a device.

Practically,
Upon probing a VF for a virtio-net device, in case its PF supports legacy access over the virtio admin commands and the VF doesn't have BAR 0, we set some specific 'vfio_device_ops' to be able to simulate in SW a transitional device with I/O BAR in BAR 0.

The existence of the simulated I/O bar is reported later on by overwriting the VFIO_DEVICE_GET_REGION_INFO command and the device exposes itself as a transitional device by overwriting some properties upon reading its config space.

Once we report the existence of I/O BAR as BAR 0 a legacy driver in the guest may use it via read/write calls according to the virtio specification.

Any read/write towards the control parts of the BAR will be captured by the new driver and will be translated into admin commands towards the device.

Any data path read/write access (i.e. virtio driver notifications) will be forwarded to the physical BAR which its properties were supplied by the command VIRTIO_PCI_QUEUE_NOTIFY upon the probing/init flow.

With that code in place a legacy driver in the guest has the look and feel as if having a transitional device with legacy support for both its control and data path flows.

[1]
https://github.com/oasis-tcs/virtio-spec/commit/03c2d32e5093ca9f2a17797242fbef88efe94b8c

---
 MAINTAINERS                      |   6 +
 drivers/vfio/pci/Kconfig         |   2 +
 drivers/vfio/pci/Makefile        |   2 +
 drivers/vfio/pci/virtio/Kconfig  |  15 +
 drivers/vfio/pci/virtio/Makefile |   4 +
 drivers/vfio/pci/virtio/cmd.c    |   4 +-
 drivers/vfio/pci/virtio/cmd.h    |   8 +
 drivers/vfio/pci/virtio/main.c   | 546 +++++++++++++++++++++++++++++++
 8 files changed, 585 insertions(+), 2 deletions(-)
 create mode 100644 drivers/vfio/pci/virtio/Kconfig
 create mode 100644 drivers/vfio/pci/virtio/Makefile
 create mode 100644 drivers/vfio/pci/virtio/main.c
MAINTAINERS +6 -0
index bf0f54c24f81..5098418c8389 100644
@@ -22624,6 +22624,12 @@ L:	[email protected]
 S:	Maintained
 F:	drivers/vfio/pci/mlx5/
 
+VFIO VIRTIO PCI DRIVER
+M:	Yishai Hadas <[email protected]>
+L:	[email protected]
+S:	Maintained
+F:	drivers/vfio/pci/virtio
+
 VFIO PCI DEVICE SPECIFIC DRIVERS
 R:	Jason Gunthorpe <[email protected]>
 R:	Yishai Hadas <[email protected]>
drivers/vfio/pci/Kconfig +2 -0
index 8125e5f37832..18c397df566d 100644
@@ -65,4 +65,6 @@ source "drivers/vfio/pci/hisilicon/Kconfig"
 
 source "drivers/vfio/pci/pds/Kconfig"
 
+source "drivers/vfio/pci/virtio/Kconfig"
+
 endmenu
drivers/vfio/pci/Makefile +2 -0
index 45167be462d8..046139a4eca5 100644
@@ -13,3 +13,5 @@ obj-$(CONFIG_MLX5_VFIO_PCI)           += mlx5/
 obj-$(CONFIG_HISI_ACC_VFIO_PCI) += hisilicon/
 
 obj-$(CONFIG_PDS_VFIO_PCI) += pds/
+
+obj-$(CONFIG_VIRTIO_VFIO_PCI) += virtio/
drivers/vfio/pci/virtio/Kconfig new file +15 -0
new file mode 100644
index 000000000000..89eddce8b1bd
@@ -0,0 +1,15 @@
+# SPDX-License-Identifier: GPL-2.0-only
+config VIRTIO_VFIO_PCI
+        tristate "VFIO support for VIRTIO PCI devices"
+        depends on VIRTIO_PCI
+        select VFIO_PCI_CORE
+        help
+          This provides support for exposing VIRTIO VF devices using the VFIO
+          framework that can work with a legacy virtio driver in the guest.
+          Based on PCIe spec, VFs do not support I/O Space; thus, VF BARs shall
+          not indicate I/O Space.
+          As of that this driver emulated I/O BAR in software to let a VF be
+          seen as a transitional device in the guest and let it work with
+          a legacy driver.
+
+          If you don't know what to do here, say N.
drivers/vfio/pci/virtio/Makefile new file +4 -0
new file mode 100644
index 000000000000..584372648a03
@@ -0,0 +1,4 @@
+# SPDX-License-Identifier: GPL-2.0-only
+obj-$(CONFIG_VIRTIO_VFIO_PCI) += virtio-vfio-pci.o
+virtio-vfio-pci-y := main.o cmd.o
+
drivers/vfio/pci/virtio/cmd.c +2 -2
index f068239cdbb0..aea9d25fbf1d 100644
@@ -44,7 +44,7 @@ int virtiovf_cmd_lr_write(struct virtiovf_pci_core_device *virtvdev, u16 opcode,
 {
 	struct virtio_device *virtio_dev =
 		virtio_pci_vf_get_pf_dev(virtvdev->core_device.pdev);
-	struct virtio_admin_cmd_data_lr_write *in;
+	struct virtio_admin_cmd_legacy_wr_data *in;
 	struct scatterlist in_sg;
 	struct virtio_admin_cmd cmd = {};
 	int ret;
@@ -74,7 +74,7 @@ int virtiovf_cmd_lr_read(struct virtiovf_pci_core_device *virtvdev, u16 opcode,
 {
 	struct virtio_device *virtio_dev =
 		virtio_pci_vf_get_pf_dev(virtvdev->core_device.pdev);
-	struct virtio_admin_cmd_data_lr_read *in;
+	struct virtio_admin_cmd_legacy_rd_data *in;
 	struct scatterlist in_sg, out_sg;
 	struct virtio_admin_cmd cmd = {};
 	int ret;
drivers/vfio/pci/virtio/cmd.h +8 -0
index c2a3645f4b90..347b1dc85570 100644
@@ -13,7 +13,15 @@
 
 struct virtiovf_pci_core_device {
 	struct vfio_pci_core_device core_device;
+	u8 bar0_virtual_buf_size;
+	u8 *bar0_virtual_buf;
+	/* synchronize access to the virtual buf */
+	struct mutex bar_mutex;
 	int vf_id;
+	void __iomem *notify_addr;
+	u32 notify_offset;
+	u8 notify_bar;
+	u8 pci_cmd_io :1;
 };
 
 int virtiovf_cmd_list_query(struct pci_dev *pdev, u8 *buf, int buf_size);
drivers/vfio/pci/virtio/main.c new file +546 -0

546 lines not on this page · raw message

Signature · 6 lines
2.27.0

_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization
#88 lore raw

Re: [PATCH vfio 11/11] vfio/virtio: Introduce a vfio driver over virtio devices

Quoted 21 lines
 MAINTAINERS                      |   6 +
 drivers/vfio/pci/Kconfig         |   2 +
 drivers/vfio/pci/Makefile        |   2 +
 drivers/vfio/pci/virtio/Kconfig  |  15 +
 drivers/vfio/pci/virtio/Makefile |   4 +
 drivers/vfio/pci/virtio/cmd.c    |   4 +-
 drivers/vfio/pci/virtio/cmd.h    |   8 +
 drivers/vfio/pci/virtio/main.c   | 546 +++++++++++++++++++++++++++++++
 8 files changed, 585 insertions(+), 2 deletions(-)
 create mode 100644 drivers/vfio/pci/virtio/Kconfig
 create mode 100644 drivers/vfio/pci/virtio/Makefile
 create mode 100644 drivers/vfio/pci/virtio/main.c
MAINTAINERS +0 -0
index bf0f54c24f81..5098418c8389 100644
@@ -22624,6 +22624,12 @@ L:	[email protected]
 S:	Maintained
 F:	drivers/vfio/pci/mlx5/

MAINTAINERS L: [email protected] · line 22635

Quoted 10 lines
MAINTAINERS +6 -0
 
+VFIO VIRTIO PCI DRIVER
+M:	Yishai Hadas <[email protected]>
+L:	[email protected]
+S:	Maintained
+F:	drivers/vfio/pci/virtio
+
 VFIO PCI DEVICE SPECIFIC DRIVERS
 R:	Jason Gunthorpe <[email protected]>
 R:	Yishai Hadas <[email protected]>

Tying two subsystems together like this is going to cause pain when merging. God forbid there's something e.g. virtio net specific (and there's going to be for sure) - now we are talking 3 subsystems.

Case in point all other virtio drivers are nicely grouped, have a common mailing list etc etc. This one is completely separate to the point where people won't even remember to copy the virtio mailing list.

drivers/vfio/pci/virtio/Kconfig new file +15 -0
new file mode 100644
index 000000000000..89eddce8b1bd
@@ -0,0 +1,15 @@
+# SPDX-License-Identifier: GPL-2.0-only
+config VIRTIO_VFIO_PCI
+        tristate "VFIO support for VIRTIO PCI devices"
+        depends on VIRTIO_PCI
+        select VFIO_PCI_CORE
+        help
+          This provides support for exposing VIRTIO VF devices using the VFIO
+          framework that can work with a legacy virtio driver in the guest.
+          Based on PCIe spec, VFs do not support I/O Space; thus, VF BARs shall
+          not indicate I/O Space.
+          As of that this driver emulated I/O BAR in software to let a VF be
+          seen as a transitional device in the guest and let it work with
+          a legacy driver.
+
+          If you don't know what to do here, say N.

I don't promise we'll remember to poke at vfio if we tweak something in the virtio kconfig.

Signature · 6 lines
MST

_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization
#89 lore raw

Re: [PATCH vfio 11/11] vfio/virtio: Introduce a vfio driver over virtio devices

Quoted 8 lines · Michael S. Tsirkin
Quoted 8 lines
MAINTAINERS +0 -0
index bf0f54c24f81..5098418c8389 100644
@@ -22624,6 +22624,12 @@ L:	[email protected]
 S:	Maintained
 F:	drivers/vfio/pci/mlx5/
 
Quoted 14 lines

MAINTAINERS L: [email protected] · line 22635

Quoted 9 lines
MAINTAINERS +6 -0
+VFIO VIRTIO PCI DRIVER
+M:	Yishai Hadas <[email protected]>
+L:	[email protected]
+S:	Maintained
+F:	drivers/vfio/pci/virtio
+
 VFIO PCI DEVICE SPECIFIC DRIVERS
 R:	Jason Gunthorpe <[email protected]>
 R:	Yishai Hadas <[email protected]>

Tying two subsystems together like this is going to cause pain when merging. God forbid there's something e.g. virtio net specific (and there's going to be for sure) - now we are talking 3 subsystems.

Cross subsystem stuff is normal in the kernel. Drivers should be placed in their most logical spot - this driver exposes a VFIO interface so it belongs here.

Your exact argument works the same from the VFIO perspective, someone has to have code that belongs to them outside their little sphere here.

Quoted 3 lines

Case in point all other virtio drivers are nicely grouped, have a common mailing list etc etc. This one is completely separate to the point where people won't even remember to copy the virtio mailing list.

The virtio mailing list should probably be added to the maintainers enry

Jason

#90 lore raw

Re: [PATCH vfio 11/11] vfio/virtio: Introduce a vfio driver over virtio devices

Quoted 25 lines · Jason Gunthorpe
Quoted 8 lines · Michael S. Tsirkin
Quoted 8 lines
MAINTAINERS +0 -0
index bf0f54c24f81..5098418c8389 100644
@@ -22624,6 +22624,12 @@ L:	[email protected]
 S:	Maintained
 F:	drivers/vfio/pci/mlx5/
 
Quoted 14 lines

MAINTAINERS L: [email protected] · line 22635

Quoted 9 lines
MAINTAINERS +6 -0
+VFIO VIRTIO PCI DRIVER
+M:	Yishai Hadas <[email protected]>
+L:	[email protected]
+S:	Maintained
+F:	drivers/vfio/pci/virtio
+
 VFIO PCI DEVICE SPECIFIC DRIVERS
 R:	Jason Gunthorpe <[email protected]>
 R:	Yishai Hadas <[email protected]>

Tying two subsystems together like this is going to cause pain when merging. God forbid there's something e.g. virtio net specific (and there's going to be for sure) - now we are talking 3 subsystems.

Quoted 1 line

Cross subsystem stuff is normal in the kernel.

Yea. But it's completely spurious here - virtio has its own way to work with userspace which is vdpa and let's just use that. Keeps things nice and contained.

Quoted 8 lines

Drivers should be
placed in their most logical spot - this driver exposes a VFIO interface so it belongs here.

Your exact argument works the same from the VFIO perspective, someone has to have code that belongs to them outside their little sphere here.

Quoted 8 lines
Quoted 3 lines

Case in point all other virtio drivers are nicely grouped, have a common mailing list etc etc. This one is completely separate to the point where people won't even remember to copy the virtio mailing list.

The virtio mailing list should probably be added to the maintainers enry

Jason

_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization

#91 lore raw

Re: [PATCH vfio 11/11] vfio/virtio: Introduce a vfio driver over virtio devices

Quoted 26 lines · Michael S. Tsirkin
Quoted 25 lines · Jason Gunthorpe
Quoted 22 lines · Michael S. Tsirkin
Quoted 7 lines
MAINTAINERS +0 -0
index bf0f54c24f81..5098418c8389 100644
@@ -22624,6 +22624,12 @@ L:	[email protected]
 S:	Maintained
 F:	drivers/vfio/pci/mlx5/

MAINTAINERS L: [email protected] · line 22635

Quoted 10 lines
MAINTAINERS +6 -0
 
+VFIO VIRTIO PCI DRIVER
+M:	Yishai Hadas <[email protected]>
+L:	[email protected]
+S:	Maintained
+F:	drivers/vfio/pci/virtio
+
 VFIO PCI DEVICE SPECIFIC DRIVERS
 R:	Jason Gunthorpe <[email protected]>
 R:	Yishai Hadas <[email protected]>

Tying two subsystems together like this is going to cause pain when merging. God forbid there's something e.g. virtio net specific (and there's going to be for sure) - now we are talking 3 subsystems.

Quoted 5 lines
Quoted 1 line

Cross subsystem stuff is normal in the kernel.

Yea. But it's completely spurious here - virtio has its own way to work with userspace which is vdpa and let's just use that. Keeps things nice and contained.

vdpa is not vfio, I don't know how you can suggest vdpa is a replacement for a vfio driver. They are completely different things.

Each side has its own strengths, and vfio especially is accelerating in its capability in way that vpda is not. eg if an iommufd conversion had been done by now for vdpa I might be more sympathetic. Asking for someone else to do a huge amount of pointless work to improve vdpa just to level of this vfio driver already is at is ridiculous.

vdpa is great for certain kinds of HW, let it focus on that, don't try to paint it as an alternative to vfio. It isn't.

Jason

#92 lore raw

Re: [PATCH vfio 11/11] vfio/virtio: Introduce a vfio driver over virtio devices

Quoted 33 lines · Jason Gunthorpe
Quoted 26 lines · Michael S. Tsirkin
Quoted 25 lines · Jason Gunthorpe
Quoted 22 lines · Michael S. Tsirkin
Quoted 7 lines
MAINTAINERS +0 -0
index bf0f54c24f81..5098418c8389 100644
@@ -22624,6 +22624,12 @@ L:	[email protected]
 S:	Maintained
 F:	drivers/vfio/pci/mlx5/

MAINTAINERS L: [email protected] · line 22635

Quoted 10 lines
MAINTAINERS +6 -0
 
+VFIO VIRTIO PCI DRIVER
+M:	Yishai Hadas <[email protected]>
+L:	[email protected]
+S:	Maintained
+F:	drivers/vfio/pci/virtio
+
 VFIO PCI DEVICE SPECIFIC DRIVERS
 R:	Jason Gunthorpe <[email protected]>
 R:	Yishai Hadas <[email protected]>

Tying two subsystems together like this is going to cause pain when merging. God forbid there's something e.g. virtio net specific (and there's going to be for sure) - now we are talking 3 subsystems.

Quoted 5 lines
Quoted 1 line

Cross subsystem stuff is normal in the kernel.

Yea. But it's completely spurious here - virtio has its own way to work with userspace which is vdpa and let's just use that. Keeps things nice and contained.

Quoted 6 lines

vdpa is not vfio, I don't know how you can suggest vdpa is a replacement for a vfio driver. They are completely different things.
Each side has its own strengths, and vfio especially is accelerating in its capability in way that vpda is not. eg if an iommufd conversion had been done by now for vdpa I might be more sympathetic.

Yea, I agree iommufd is a big problem with vdpa right now. Cindy was sick and I didn't know and kept assuming she's working on this. I don't think it's a huge amount of work though. I'll take a look. Is there anything else though? Do tell.

Quoted 8 lines

Asking for
someone else to do a huge amount of pointless work to improve vdpa just to level of this vfio driver already is at is ridiculous.

vdpa is great for certain kinds of HW, let it focus on that, don't try to paint it as an alternative to vfio. It isn't.

Jason

There are a bunch of things that I think are important for virtio that are completely out of scope for vfio, such as migrating cross-vendor. What is the huge amount of work am I asking to do?

Signature · 6 lines
MST

_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization
#93 lore raw

Re: [PATCH vfio 11/11] vfio/virtio: Introduce a vfio driver over virtio devices

Quoted 11 lines · Michael S. Tsirkin
Quoted 6 lines

vdpa is not vfio, I don't know how you can suggest vdpa is a replacement for a vfio driver. They are completely different things.
Each side has its own strengths, and vfio especially is accelerating in its capability in way that vpda is not. eg if an iommufd conversion had been done by now for vdpa I might be more sympathetic.

Yea, I agree iommufd is a big problem with vdpa right now. Cindy was sick and I didn't know and kept assuming she's working on this. I don't think it's a huge amount of work though. I'll take a look. Is there anything else though? Do tell.

Confidential compute will never work with VDPA's approach.

Quoted 3 lines

There are a bunch of things that I think are important for virtio that are completely out of scope for vfio, such as migrating cross-vendor.

VFIO supports migration, if you want to have cross-vendor migration then make a standard that describes the VFIO migration data format for virtio devices.

Quoted 1 line

What is the huge amount of work am I asking to do?

You are asking us to invest in the complexity of VDPA through out (keep it working, keep it secure, invest time in deploying and debugging in the field)

When it doesn't provide *ANY* value to the solution.

The starting point is a completely working vfio PCI function and the end goal is to put that function into a VM. That is VFIO, not VDPA.

VPDA is fine for what it does, but it is not a reasonable replacement for VFIO.

Jason

#94 lore raw

Re: [PATCH vfio 11/11] vfio/virtio: Introduce a vfio driver over virtio devices

Quoted 8 lines · Jason Gunthorpe
Quoted 7 lines · Michael S. Tsirkin
Quoted 6 lines

vdpa is not vfio, I don't know how you can suggest vdpa is a replacement for a vfio driver. They are completely different things.
Each side has its own strengths, and vfio especially is accelerating in its capability in way that vpda is not. eg if an iommufd conversion had been done by now for vdpa I might be more sympathetic.

Quoted 6 lines
Quoted 4 lines

Yea, I agree iommufd is a big problem with vdpa right now. Cindy was sick and I didn't know and kept assuming she's working on this. I don't think it's a huge amount of work though. I'll take a look. Is there anything else though? Do tell.

Confidential compute will never work with VDPA's approach.

I don't see how what this patchset is doing is different wrt to Confidential compute - you trap IO accesses and emulate. Care to elaborate?

Quoted 7 lines
Quoted 3 lines

There are a bunch of things that I think are important for virtio that are completely out of scope for vfio, such as migrating cross-vendor.

VFIO supports migration, if you want to have cross-vendor migration then make a standard that describes the VFIO migration data format for virtio devices.

This has nothing to do with data formats - you need two devices to behave identically. Which is what VDPA is about really.

Quoted 7 lines
Quoted 1 line

What is the huge amount of work am I asking to do?

You are asking us to invest in the complexity of VDPA through out (keep it working, keep it secure, invest time in deploying and debugging in the field)

When it doesn't provide *ANY* value to the solution.

There's no "the solution" - this sounds like a vendor only caring about solutions that involve that vendor's hardware exclusively, a little.

Quoted 7 lines

The starting point is a completely working vfio PCI function and the end goal is to put that function into a VM. That is VFIO, not VDPA.

VPDA is fine for what it does, but it is not a reasonable replacement for VFIO.

Jason

VDPA basically should be a kind of "VFIO for virtio".

Signature · 6 lines
MST

_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization
#95 lore raw

Re: [PATCH vfio 11/11] vfio/virtio: Introduce a vfio driver over virtio devices

Quoted 14 lines · Michael S. Tsirkin
Quoted 13 lines · Jason Gunthorpe
Quoted 11 lines · Michael S. Tsirkin
Quoted 6 lines

vdpa is not vfio, I don't know how you can suggest vdpa is a replacement for a vfio driver. They are completely different things.
Each side has its own strengths, and vfio especially is accelerating in its capability in way that vpda is not. eg if an iommufd conversion had been done by now for vdpa I might be more sympathetic.

Yea, I agree iommufd is a big problem with vdpa right now. Cindy was sick and I didn't know and kept assuming she's working on this. I don't think it's a huge amount of work though. I'll take a look. Is there anything else though? Do tell.

Quoted 5 lines
Quoted 1 line

Confidential compute will never work with VDPA's approach.

I don't see how what this patchset is doing is different wrt to Confidential compute - you trap IO accesses and emulate. Care to elaborate?

This patch series isn't about confidential compute, you asked about the future. VFIO will support confidential compute in the future, VDPA will not.

Quoted 10 lines
Quoted 7 lines
Quoted 3 lines

There are a bunch of things that I think are important for virtio that are completely out of scope for vfio, such as migrating cross-vendor.

VFIO supports migration, if you want to have cross-vendor migration then make a standard that describes the VFIO migration data format for virtio devices.

This has nothing to do with data formats - you need two devices to behave identically. Which is what VDPA is about really.

We've been looking at VFIO live migration extensively. Device mediation, like VDPA does, is one legitimate approach for live migration. It suites a certain type of heterogeneous environment well.

But, it is equally legitimate to make the devices behave the same and have them process a common migration data.

This can happen in public with standards, or it can happen in private within a cloud operator's "private-standard" environment.

To date, in most of my discussions, I have not seen a strong appetite for such public standards. In part due to the complexity.

Regardles, it is not the kernel communities job to insist on one approach or the other.

Quoted 7 lines
Quoted 5 lines

You are asking us to invest in the complexity of VDPA through out (keep it working, keep it secure, invest time in deploying and debugging in the field)

When it doesn't provide *ANY* value to the solution.

There's no "the solution"

Nonsense.

Quoted 2 lines

this sounds like a vendor only caring about solutions that involve that vendor's hardware exclusively, a little.

Not really.

Understand the DPU provider is not the vendor here. The DPU provider gives a cloud operator a SDK to build these things. The operator is the vendor from your perspective.

In many cases live migration never leaves the operator's confines in the first place.

Even when it does, there is no real use case to live migrate a virtio-net function from, say, AWS to GCP.

You are pushing for a lot of complexity and software that solves a problem people in this space don't actually have.

As I said, VDPA is fine for the scenarios it addresses. It is an alternative, not a replacement, for VFIO.

Jason

#96 lore raw

Re: [PATCH vfio 11/11] vfio/virtio: Introduce a vfio driver over virtio devices

Quoted 17 lines · Jason Gunthorpe
Quoted 16 lines · Michael S. Tsirkin
Quoted 14 lines · Jason Gunthorpe
Quoted 11 lines · Michael S. Tsirkin
Quoted 6 lines

vdpa is not vfio, I don't know how you can suggest vdpa is a replacement for a vfio driver. They are completely different things.
Each side has its own strengths, and vfio especially is accelerating in its capability in way that vpda is not. eg if an iommufd conversion had been done by now for vdpa I might be more sympathetic.

Yea, I agree iommufd is a big problem with vdpa right now. Cindy was sick and I didn't know and kept assuming she's working on this. I don't think it's a huge amount of work though. I'll take a look. Is there anything else though? Do tell.

Confidential compute will never work with VDPA's approach.

Quoted 7 lines
Quoted 3 lines

I don't see how what this patchset is doing is different wrt to Confidential compute - you trap IO accesses and emulate. Care to elaborate?

This patch series isn't about confidential compute, you asked about the future. VFIO will support confidential compute in the future, VDPA will not.

Nonsense it already works.

But I did not ask about the future since I do not believe it can be confidently predicted. I asked what is missing in VDPA now for you to add this feature there and not in VFIO.

Quoted 31 lines
Quoted 10 lines
Quoted 7 lines
Quoted 3 lines

There are a bunch of things that I think are important for virtio that are completely out of scope for vfio, such as migrating cross-vendor.

VFIO supports migration, if you want to have cross-vendor migration then make a standard that describes the VFIO migration data format for virtio devices.

This has nothing to do with data formats - you need two devices to behave identically. Which is what VDPA is about really.

We've been looking at VFIO live migration extensively. Device mediation, like VDPA does, is one legitimate approach for live migration. It suites a certain type of heterogeneous environment well.

But, it is equally legitimate to make the devices behave the same and have them process a common migration data.

This can happen in public with standards, or it can happen in private within a cloud operator's "private-standard" environment.

To date, in most of my discussions, I have not seen a strong appetite for such public standards. In part due to the complexity.

Regardles, it is not the kernel communities job to insist on one approach or the other.

Quoted 4 lines
Quoted 4 lines

You are asking us to invest in the complexity of VDPA through out (keep it working, keep it secure, invest time in deploying and debugging in the field)

Quoted 5 lines
Quoted 3 lines
Quoted 1 line

When it doesn't provide *ANY* value to the solution.

There's no "the solution"

Nonsense.

what there's only one solution that you use the definite article?

Quoted 15 lines
Quoted 2 lines

this sounds like a vendor only caring about solutions that involve that vendor's hardware exclusively, a little.

Not really.

Understand the DPU provider is not the vendor here. The DPU provider gives a cloud operator a SDK to build these things. The operator is the vendor from your perspective.

In many cases live migration never leaves the operator's confines in the first place.

Even when it does, there is no real use case to live migrate a virtio-net function from, say, AWS to GCP.

Quoted 7 lines

You are pushing for a lot of complexity and software that solves a problem people in this space don't actually have.

As I said, VDPA is fine for the scenarios it addresses. It is an alternative, not a replacement, for VFIO.

Jason

yea, VDPA does trap and emulate for config accesses. which is exactly what this patch does? so why does it belong in vfio muddying up its passthrough model is beyond me, except that apparently there's some specific deployment that happens to use vfio so now whatever that deployment needs has to go into vfio whether it belongs there or not.

Signature · 6 lines
MST

_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization
#97 lore raw

Re: [PATCH vfio 11/11] vfio/virtio: Introduce a vfio driver over virtio devices

Quoted 22 lines · Michael S. Tsirkin
Quoted 21 lines · Jason Gunthorpe
Quoted 19 lines · Michael S. Tsirkin
Quoted 8 lines · Jason Gunthorpe
Quoted 7 lines · Michael S. Tsirkin
Quoted 6 lines

vdpa is not vfio, I don't know how you can suggest vdpa is a replacement for a vfio driver. They are completely different things.
Each side has its own strengths, and vfio especially is accelerating in its capability in way that vpda is not. eg if an iommufd conversion had been done by now for vdpa I might be more sympathetic.

Quoted 6 lines
Quoted 4 lines

Yea, I agree iommufd is a big problem with vdpa right now. Cindy was sick and I didn't know and kept assuming she's working on this. I don't think it's a huge amount of work though. I'll take a look. Is there anything else though? Do tell.

Confidential compute will never work with VDPA's approach.

I don't see how what this patchset is doing is different wrt to Confidential compute - you trap IO accesses and emulate. Care to elaborate?

Quoted 5 lines
Quoted 3 lines

This patch series isn't about confidential compute, you asked about the future. VFIO will support confidential compute in the future, VDPA will not.

Nonsense it already works.

That isn't what I'm talking about. With a real PCI function and TDISP we can actually DMA directly from the guest's memory without needing the ugly bounce buffer hack. Then you can get decent performance.

Quoted 3 lines

But I did not ask about the future since I do not believe it can be confidently predicted. I asked what is missing in VDPA now for you to add this feature there and not in VFIO.

I don't see that VDPA needs this, VDPA should process the IO BAR on its own with its own logic, just like everything else it does.

This is specifically about avoiding mediation by relaying directly the IO BAR operations to the device itself.

That is the entire irony, this whole scheme was designed and standardized *specifically* to avoid complex mediation and here you are saying we should just use mediation.

Jason

#98 lore raw

Re: [PATCH vfio 11/11] vfio/virtio: Introduce a vfio driver over virtio devices

Quoted 20 lines · Jason Gunthorpe
Quoted 18 lines · Michael S. Tsirkin
Quoted 17 lines · Jason Gunthorpe
Quoted 16 lines · Michael S. Tsirkin
Quoted 14 lines · Jason Gunthorpe
Quoted 11 lines · Michael S. Tsirkin
Quoted 6 lines

vdpa is not vfio, I don't know how you can suggest vdpa is a replacement for a vfio driver. They are completely different things.
Each side has its own strengths, and vfio especially is accelerating in its capability in way that vpda is not. eg if an iommufd conversion had been done by now for vdpa I might be more sympathetic.

Yea, I agree iommufd is a big problem with vdpa right now. Cindy was sick and I didn't know and kept assuming she's working on this. I don't think it's a huge amount of work though. I'll take a look. Is there anything else though? Do tell.

Confidential compute will never work with VDPA's approach.

Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 3 lines

I don't see how what this patchset is doing is different wrt to Confidential compute - you trap IO accesses and emulate. Care to elaborate?

This patch series isn't about confidential compute, you asked about the future. VFIO will support confidential compute in the future, VDPA will not.

What blocks vDPA from supporting that?

Quoted 6 lines
Quoted 2 lines

Nonsense it already works.

That isn't what I'm talking about. With a real PCI function and TDISP we can actually DMA directly from the guest's memory without needing the ugly bounce buffer hack. Then you can get decent performance.

This series requires the trapping in the legacy I/O BAR in VFIO. Why can TDISP work when trapping in VFIO but not vDPA? If neither, how can TDISP affect here?

Quoted 10 lines
Quoted 4 lines

But I did not ask about the future since I do not believe it can be confidently predicted. I asked what is missing in VDPA now for you to add this feature there and not in VFIO.

I don't see that VDPA needs this, VDPA should process the IO BAR on its own with its own logic, just like everything else it does.

This is specifically about avoiding mediation by relaying directly the IO BAR operations to the device itself.

So we had:

1) a new virtio specific driver for VFIO
2) the existing vp_vdpa driver

How much differences between them in the context of the mediation or relaying? Or is it hard to introduce admin commands in the vDPA bus?

Quoted 3 lines

That is the entire irony, this whole scheme was designed and standardized *specifically* to avoid complex mediation and here you are saying we should just use mediation.

No, using "simple VFIO passthrough" is just fine.

Thanks

Quoted 3 lines

Jason

_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization

#99 lore raw

Re: [PATCH vfio 11/11] vfio/virtio: Introduce a vfio driver over virtio devices

In reply to Jason Gunthorpe
Quoted 27 lines · Jason Gunthorpe
Quoted 26 lines · Michael S. Tsirkin
Quoted 24 lines · Jason Gunthorpe
Quoted 14 lines · Michael S. Tsirkin
Quoted 13 lines · Jason Gunthorpe
Quoted 11 lines · Michael S. Tsirkin
Quoted 6 lines

vdpa is not vfio, I don't know how you can suggest vdpa is a replacement for a vfio driver. They are completely different things.
Each side has its own strengths, and vfio especially is accelerating in its capability in way that vpda is not. eg if an iommufd conversion had been done by now for vdpa I might be more sympathetic.

Yea, I agree iommufd is a big problem with vdpa right now. Cindy was sick and I didn't know and kept assuming she's working on this. I don't think it's a huge amount of work though. I'll take a look. Is there anything else though? Do tell.

Quoted 5 lines
Quoted 1 line

Confidential compute will never work with VDPA's approach.

I don't see how what this patchset is doing is different wrt to Confidential compute - you trap IO accesses and emulate. Care to elaborate?

This patch series isn't about confidential compute, you asked about the future. VFIO will support confidential compute in the future, VDPA will not.

Quoted 5 lines
Quoted 1 line

Nonsense it already works.

That isn't what I'm talking about. With a real PCI function and TDISP we can actually DMA directly from the guest's memory without needing the ugly bounce buffer hack. Then you can get decent performance.

Aha, TDISP. But that one clearly does not need and can not use this kind of hack?

Quoted 6 lines
Quoted 3 lines

But I did not ask about the future since I do not believe it can be confidently predicted. I asked what is missing in VDPA now for you to add this feature there and not in VFIO.

I don't see that VDPA needs this, VDPA should process the IO BAR on its own with its own logic, just like everything else it does.

First there's some logic here such as translating legacy IO offsets to modern ones that could be reused.

But also, this is not just IO BAR, that indeed can be easily done in software. When a device operates in legacy mode there are subtle differences with modern mode such as a different header size for the net device.

Quoted 8 lines

This is specifically about avoiding mediation by relaying directly the IO BAR operations to the device itself.

That is the entire irony, this whole scheme was designed and standardized *specifically* to avoid complex mediation and here you are saying we should just use mediation.

Jason

Not exactly. What I had in mind is just having the logic in the vdpa module so users don't need to know what does the device support and what it doesn't. If we can we bypass mediation (to simplify the software stack) if we can not we do not.

Looking at it from user's POV, it is just super confusing that card ABC would need to be used with VDPA to drive legacy while card DEF needs to be used with VFIO. And both VFIO and VDPA will happily bind, too. Oh man ...

Signature · 6 lines
MST

_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization
#100 lore raw

Re: [PATCH vfio 11/11] vfio/virtio: Introduce a vfio driver over virtio devices

Quoted 6 lines · Michael S. Tsirkin

On Thu, Sep 21, 2023 at 07:55:26PM -0300, Jason Gunthorpe wrote:

Looking at it from user's POV, it is just super confusing that card ABC would need to be used with VDPA to drive legacy while card DEF needs to be used with VFIO. And both VFIO and VDPA will happily bind, too. Oh man ...

It is standard VFIO stuff. If you don't attach vfio then you get a normal kernel vfio-net driver. If you turn that into VDPA then you get that.

If you attach VFIO to the PCI function then you get VFIO.

There is nothing special here, we have good infrastructure to support doing this already.

User gets to pick. I don't understand why you think the kernel side should deny this choice.

Jason

#101 lore raw

Re: [PATCH vfio 11/11] vfio/virtio: Introduce a vfio driver over virtio devices

In reply to Jason Gunthorpe
Quoted 72 lines · Jason Gunthorpe
Quoted 14 lines · Michael S. Tsirkin
Quoted 13 lines · Jason Gunthorpe
Quoted 11 lines · Michael S. Tsirkin
Quoted 6 lines

vdpa is not vfio, I don't know how you can suggest vdpa is a replacement for a vfio driver. They are completely different things.
Each side has its own strengths, and vfio especially is accelerating in its capability in way that vpda is not. eg if an iommufd conversion had been done by now for vdpa I might be more sympathetic.

Yea, I agree iommufd is a big problem with vdpa right now. Cindy was sick and I didn't know and kept assuming she's working on this. I don't think it's a huge amount of work though. I'll take a look. Is there anything else though? Do tell.

Quoted 5 lines
Quoted 1 line

Confidential compute will never work with VDPA's approach.

I don't see how what this patchset is doing is different wrt to Confidential compute - you trap IO accesses and emulate. Care to elaborate?

This patch series isn't about confidential compute, you asked about the future. VFIO will support confidential compute in the future, VDPA will not.

Quoted 10 lines
Quoted 7 lines
Quoted 3 lines

There are a bunch of things that I think are important for virtio that are completely out of scope for vfio, such as migrating cross-vendor.

VFIO supports migration, if you want to have cross-vendor migration then make a standard that describes the VFIO migration data format for virtio devices.

This has nothing to do with data formats - you need two devices to behave identically. Which is what VDPA is about really.

We've been looking at VFIO live migration extensively. Device mediation, like VDPA does, is one legitimate approach for live migration. It suites a certain type of heterogeneous environment well.

But, it is equally legitimate to make the devices behave the same and have them process a common migration data.

This can happen in public with standards, or it can happen in private within a cloud operator's "private-standard" environment.

To date, in most of my discussions, I have not seen a strong appetite for such public standards. In part due to the complexity.

Regardles, it is not the kernel communities job to insist on one approach or the other.

Quoted 7 lines
Quoted 5 lines

You are asking us to invest in the complexity of VDPA through out (keep it working, keep it secure, invest time in deploying and debugging in the field)

When it doesn't provide *ANY* value to the solution.

There's no "the solution"

Nonsense.

Quoted 2 lines

this sounds like a vendor only caring about solutions that involve that vendor's hardware exclusively, a little.

Not really.

Understand the DPU provider is not the vendor here. The DPU provider gives a cloud operator a SDK to build these things. The operator is the vendor from your perspective.

Quoted 5 lines

In many cases live migration never leaves the operator's confines in the first place.

Even when it does, there is no real use case to live migrate a virtio-net function from, say, AWS to GCP.

It can happen inside a single cloud vendor. For some reasons, DPU must be purchased from different vendors. And vDPA has been used in that case.

I've asked them to present this probably somewhere like KVM Forum.

Quoted 6 lines

You are pushing for a lot of complexity and software that solves a problem people in this space don't actually have.

As I said, VDPA is fine for the scenarios it addresses. It is an alternative, not a replacement, for VFIO.

We never try to replace VFIO. I don't see any problem by just using the current VFIO to assign a virtio-pci device to the guest.

The problem is the mediation (or what you called relaying) layer you've invented.

Thanks

Quoted 3 lines

Jason

_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization

#102 lore raw

Re: [PATCH vfio 11/11] vfio/virtio: Introduce a vfio driver over virtio devices

Quoted 6 lines · Jason Wang
Quoted 2 lines

Even when it does, there is no real use case to live migrate a virtio-net function from, say, AWS to GCP.

It can happen inside a single cloud vendor. For some reasons, DPU must be purchased from different vendors. And vDPA has been used in that case.

Nope, you misunderstand the DPU scenario.

Look at something like vmware DPU enablement. vmware runs the software side of the DPU and all their supported DPU HW, from every vendor, generates the same PCI functions on the x86. They are the same because the same software on the DPU side is creating them.

There is no reason to put a mediation layer in the x86 if you also control the DPU.

Cloud vendors will similarly use DPUs to create a PCI functions that meet the cloud vendor's internal specification. Regardless of DPU vendor.

Fundamentally if you control the DPU SW and the hypervisor software you do not need hypervisor meditation because everything you could do in hypervisor mediation can just be done in the DPU. Putting it in the DPU is better in every regard.

So, as I keep saying, in this scenario the goal is no mediation in the hypervisor. It is pointless, everything you think you need to do there is actually already being done in the DPU.

Once you commit to this configuration you are committed to VFIO in the hypervisor. eg your DPU is likely also making NVMe and other PCI functions too.

Quoted 2 lines

The problem is the mediation (or what you called relaying) layer you've invented.

It is not mediation, it is implementing the OASIS spec for VFIO support of IO BAR.

Jason

#103 lore raw

Re: [PATCH vfio 11/11] vfio/virtio: Introduce a vfio driver over virtio devices

Quoted 17 lines · Jason Gunthorpe
Quoted 6 lines · Jason Wang
Quoted 2 lines

Even when it does, there is no real use case to live migrate a virtio-net function from, say, AWS to GCP.

It can happen inside a single cloud vendor. For some reasons, DPU must be purchased from different vendors. And vDPA has been used in that case.

Nope, you misunderstand the DPU scenario.

Look at something like vmware DPU enablement. vmware runs the software side of the DPU and all their supported DPU HW, from every vendor, generates the same PCI functions on the x86. They are the same because the same software on the DPU side is creating them.

Quoted 5 lines

There is no reason to put a mediation layer in the x86 if you also control the DPU.

Cloud vendors will similarly use DPUs to create a PCI functions that meet the cloud vendor's internal specification.

This can only work if:

1) the internal specification has finer garin than virtio spec
2) so it can define what is not implemented in the virtio spec (like migration and compatibility)

All of the above doesn't seem to be possible or realistic now, and it actually has a risk to be not compatible with virtio spec. In the future when virtio has live migration supported, they want to be able to migrate between virtio and vDPA.

As I said, vDPA has been used for cross vendor live migration for a while.

Quoted 10 lines

Regardless of DPU
vendor.

Fundamentally if you control the DPU SW and the hypervisor software you do not need hypervisor meditation because everything you could do in hypervisor mediation can just be done in the DPU. Putting it in the DPU is better in every regard.

So, as I keep saying, in this scenario the goal is no mediation in the hypervisor.

That's pretty fine, but I don't think trapping + relying is not mediation. Does it really matter what happens after trapping?

Quoted 2 lines

It is pointless, everything you think you need to do there is actually already being done in the DPU.

Well, migration or even Qemu could be offloaded to DPU as well. If that's the direction that's pretty fine.

Thanks

Quoted 3 lines

Jason

_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization

#104 lore raw

Re: [PATCH vfio 11/11] vfio/virtio: Introduce a vfio driver over virtio devices

Quoted 8 lines · Jason Wang
Quoted 2 lines

Cloud vendors will similarly use DPUs to create a PCI functions that meet the cloud vendor's internal specification.

This can only work if:

1) the internal specification has finer garin than virtio spec
2) so it can define what is not implemented in the virtio spec (like migration and compatibility)

Yes, and that is what is happening. Realistically the "spec" isjust a piece of software that the Cloud vendor owns which is simply ported to multiple DPU vendors.

It is the same as VDPA. If VDPA can make multiple NIC vendors consistent then why do you have a hard time believing we can do the same thing just on the ARM side of a DPU?

Quoted 4 lines

All of the above doesn't seem to be possible or realistic now, and it actually has a risk to be not compatible with virtio spec. In the future when virtio has live migration supported, they want to be able to migrate between virtio and vDPA.

Well, that is for the spec to design.

Quoted 5 lines
Quoted 2 lines

So, as I keep saying, in this scenario the goal is no mediation in the hypervisor.

That's pretty fine, but I don't think trapping + relying is not mediation. Does it really matter what happens after trapping?

It is not mediation in the sense that the kernel driver does not in any way make decisions on the behavior of the device. It simply transforms an IO operation into a device command and relays it to the device. The device still fully controls its own behavior.

VDPA is very different from this. You might call them both mediation, sure, but then you need another word to describe the additional changes VPDA is doing.

Quoted 5 lines
Quoted 2 lines

It is pointless, everything you think you need to do there is actually already being done in the DPU.

Well, migration or even Qemu could be offloaded to DPU as well. If that's the direction that's pretty fine.

That's silly, of course qemu/kvm can't run in the DPU.

However, we can empty qemu and the hypervisor out so all it does is run kvm and run vfio. In this model the DPU does all the OVS, storage, "VPDA", etc. qemu is just a passive relay of the DPU PCI functions into VM's vPCI functions.

So, everything VDPA was doing in the environment is migrated into the DPU.

In this model the DPU is an extension of the hypervisor/qemu environment and we shift code from x86 side to arm side to increase security, save power and increase total system performance.

Jason

#105 lore raw

Re: [PATCH vfio 11/11] vfio/virtio: Introduce a vfio driver over virtio devices

Quoted 6 lines · Jason Gunthorpe
Quoted 5 lines
Quoted 2 lines

So, as I keep saying, in this scenario the goal is no mediation in the hypervisor.

That's pretty fine, but I don't think trapping + relying is not mediation. Does it really matter what happens after trapping?

Quoted 8 lines

It is not mediation in the sense that the kernel driver does not in any way make decisions on the behavior of the device. It simply transforms an IO operation into a device command and relays it to the device. The device still fully controls its own behavior.

VDPA is very different from this. You might call them both mediation, sure, but then you need another word to describe the additional changes VPDA is doing.

Sorry about hijacking the thread a little bit, but could you call out some of the changes that are the most problematic for you?

Quoted 13 lines
Quoted 5 lines
Quoted 2 lines

It is pointless, everything you think you need to do there is actually already being done in the DPU.

Well, migration or even Qemu could be offloaded to DPU as well. If that's the direction that's pretty fine.

That's silly, of course qemu/kvm can't run in the DPU.

However, we can empty qemu and the hypervisor out so all it does is run kvm and run vfio. In this model the DPU does all the OVS, storage, "VPDA", etc. qemu is just a passive relay of the DPU PCI functions into VM's vPCI functions.

Quoted 8 lines

So, everything VDPA was doing in the environment is migrated into the DPU.

In this model the DPU is an extension of the hypervisor/qemu environment and we shift code from x86 side to arm side to increase security, save power and increase total system performance.

Jason

I think I begin to understand. On the DPU you have some virtio devices but also some non-virtio devices. So you have to use VFIO to talk to the DPU. Reusing VFIO to talk to virtio devices too, simplifies things for you. If guests will see vendor-specific devices from the DPU anyway, it will be impossible to migrate such guests away from the DPU so the cross-vendor migration capability is less important in this use-case. Is this a good summary?

Signature · 6 lines
MST

_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization
#106 lore raw

Re: [PATCH vfio 11/11] vfio/virtio: Introduce a vfio driver over virtio devices

Quoted 7 lines · Michael S. Tsirkin
Quoted 3 lines

VDPA is very different from this. You might call them both mediation, sure, but then you need another word to describe the additional changes VPDA is doing.

Sorry about hijacking the thread a little bit, but could you call out some of the changes that are the most problematic for you?

I don't really know these details. The operators have an existing virtio world that is ABI toward the VM for them, and they do not want *anything* to change. The VM should be unware if the virtio device is created by old hypervisor software or new DPU software. It presents exactly the same ABI.

So the challenge really is to convince that VDPA delivers that, and frankly, I don't think it does. ABI toward the VM is very important here.

Quoted 8 lines
Quoted 3 lines

In this model the DPU is an extension of the hypervisor/qemu environment and we shift code from x86 side to arm side to increase security, save power and increase total system performance.

I think I begin to understand. On the DPU you have some virtio devices but also some non-virtio devices. So you have to use VFIO to talk to the DPU. Reusing VFIO to talk to virtio devices too, simplifies things for you.

Yes

Quoted 4 lines

If guests will see vendor-specific devices from the DPU anyway, it will be impossible to migrate such guests away from the DPU so the cross-vendor migration capability is less important in this use-case. Is this a good summary?

Well, sort of. As I said before, the vendor here is the cloud operator, not the DPU supplier. The guest will see an AWS virtio-net function, for example.

The operator will ensure that all their different implementations of this function will interwork for migration.

So within the closed world of a single operator live migration will work just fine.

Since the hypervisor's controlled by the operator only migrate within the operators own environment anyhow, it is an already solved problem.

Jason

#107 lore raw

Re: [PATCH vfio 11/11] vfio/virtio: Introduce a vfio driver over virtio devices

Quoted 15 lines · Jason Gunthorpe
Quoted 7 lines · Michael S. Tsirkin
Quoted 3 lines

VDPA is very different from this. You might call them both mediation, sure, but then you need another word to describe the additional changes VPDA is doing.

Sorry about hijacking the thread a little bit, but could you call out some of the changes that are the most problematic for you?

I don't really know these details. The operators have an existing virtio world that is ABI toward the VM for them, and they do not want *anything* to change. The VM should be unware if the virtio device is created by old hypervisor software or new DPU software. It presents exactly the same ABI.

Quoted 3 lines

So the challenge really is to convince that VDPA delivers that, and frankly, I don't think it does. ABI toward the VM is very important here.

And to complete the picture, it is the DPU software/firmware that is resposible for maintaining this ABI in your ideal world?

Quoted 23 lines
Quoted 8 lines
Quoted 3 lines

In this model the DPU is an extension of the hypervisor/qemu environment and we shift code from x86 side to arm side to increase security, save power and increase total system performance.

I think I begin to understand. On the DPU you have some virtio devices but also some non-virtio devices. So you have to use VFIO to talk to the DPU. Reusing VFIO to talk to virtio devices too, simplifies things for you.

Yes

Quoted 4 lines

If guests will see vendor-specific devices from the DPU anyway, it will be impossible to migrate such guests away from the DPU so the cross-vendor migration capability is less important in this use-case. Is this a good summary?

Well, sort of. As I said before, the vendor here is the cloud operator, not the DPU supplier. The guest will see an AWS virtio-net function, for example.

The operator will ensure that all their different implementations of this function will interwork for migration.

Quoted 7 lines

So within the closed world of a single operator live migration will work just fine.

Since the hypervisor's controlled by the operator only migrate within the operators own environment anyhow, it is an already solved problem.

Jason

Okay the picture emerges I think. Thanks! I'll try to summarize later for everyone's benefit.

Signature · 6 lines
MST

_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization
#108 lore raw

Re: [PATCH vfio 11/11] vfio/virtio: Introduce a vfio driver over virtio devices

In reply to Jason Gunthorpe
Quoted 10 lines · Jason Gunthorpe
Quoted 7 lines · Michael S. Tsirkin
Quoted 3 lines

VDPA is very different from this. You might call them both mediation, sure, but then you need another word to describe the additional changes VPDA is doing.

Sorry about hijacking the thread a little bit, but could you call out some of the changes that are the most problematic for you?

I don't really know these details.

Maybe, you then should desist from saying things like "It entirely fails to achieve the most important thing it needs to do!" You are not making any new friends with saying this about a piece of software without knowing the details.

Signature · 6 lines
MST

_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization
#109 lore raw

Re: [PATCH vfio 11/11] vfio/virtio: Introduce a vfio driver over virtio devices

Quoted 10 lines · Michael S. Tsirkin
Quoted 9 lines · Jason Gunthorpe
Quoted 7 lines · Michael S. Tsirkin
Quoted 3 lines

VDPA is very different from this. You might call them both mediation, sure, but then you need another word to describe the additional changes VPDA is doing.

Sorry about hijacking the thread a little bit, but could you call out some of the changes that are the most problematic for you?

Quoted 6 lines
Quoted 1 line

I don't really know these details.

Maybe, you then should desist from saying things like "It entirely fails to achieve the most important thing it needs to do!" You are not making any new friends with saying this about a piece of software without knowing the details.

I can't tell you what cloud operators are doing, but I can say with confidence that it is not the same as VDPA. As I said, if you want to know more details you need to ask a cloud operator.

Jason

#110 lore raw

Re: [PATCH vfio 11/11] vfio/virtio: Introduce a vfio driver over virtio devices

Quoted 18 lines · Jason Gunthorpe
Quoted 10 lines · Michael S. Tsirkin
Quoted 9 lines · Jason Gunthorpe
Quoted 7 lines · Michael S. Tsirkin
Quoted 3 lines

VDPA is very different from this. You might call them both mediation, sure, but then you need another word to describe the additional changes VPDA is doing.

Sorry about hijacking the thread a little bit, but could you call out some of the changes that are the most problematic for you?

Quoted 6 lines
Quoted 1 line

I don't really know these details.

Maybe, you then should desist from saying things like "It entirely fails to achieve the most important thing it needs to do!" You are not making any new friends with saying this about a piece of software without knowing the details.

Quoted 5 lines

I can't tell you what cloud operators are doing, but I can say with confidence that it is not the same as VDPA. As I said, if you want to know more details you need to ask a cloud operator.

Jason

So it's not the changes that are problematic, it's that you have customers who are not using vdpa. The "most important thing" that vdpa fails at is simply converting your customers from vfio to vdpa.

Signature · 6 lines
MST

_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization
#111 lore raw

Re: [PATCH vfio 11/11] vfio/virtio: Introduce a vfio driver over virtio devices

Quoted 19 lines · Michael S. Tsirkin
Quoted 18 lines · Jason Gunthorpe
Quoted 16 lines · Michael S. Tsirkin
Quoted 10 lines · Jason Gunthorpe
Quoted 7 lines · Michael S. Tsirkin
Quoted 3 lines

VDPA is very different from this. You might call them both mediation, sure, but then you need another word to describe the additional changes VPDA is doing.

Sorry about hijacking the thread a little bit, but could you call out some of the changes that are the most problematic for you?

I don't really know these details.

Maybe, you then should desist from saying things like "It entirely fails to achieve the most important thing it needs to do!" You are not making any new friends with saying this about a piece of software without knowing the details.

Quoted 7 lines
Quoted 3 lines

I can't tell you what cloud operators are doing, but I can say with confidence that it is not the same as VDPA. As I said, if you want to know more details you need to ask a cloud operator.

So it's not the changes that are problematic, it's that you have customers who are not using vdpa. The "most important thing" that vdpa fails at is simply converting your customers from vfio to vdpa.

I said the most important thing was that VFIO presents exactly the same virtio device to the VM as the baremetal. Do you dispute that, technically, VDPA does not actually achieve that?

Then why is it so surprising that people don't want a solution that changes the vPCI ABI they worked hard to create in the first place?

I'm still baffled why you think everyone should use vdpa..

Jason

#112 lore raw

Re: [PATCH vfio 11/11] vfio/virtio: Introduce a vfio driver over virtio devices

Quoted 24 lines · Jason Gunthorpe
Quoted 23 lines · Michael S. Tsirkin
Quoted 21 lines · Jason Gunthorpe
Quoted 10 lines · Michael S. Tsirkin
Quoted 9 lines · Jason Gunthorpe
Quoted 7 lines · Michael S. Tsirkin
Quoted 3 lines

VDPA is very different from this. You might call them both mediation, sure, but then you need another word to describe the additional changes VPDA is doing.

Sorry about hijacking the thread a little bit, but could you call out some of the changes that are the most problematic for you?

Quoted 6 lines
Quoted 1 line

I don't really know these details.

Maybe, you then should desist from saying things like "It entirely fails to achieve the most important thing it needs to do!" You are not making any new friends with saying this about a piece of software without knowing the details.

I can't tell you what cloud operators are doing, but I can say with confidence that it is not the same as VDPA. As I said, if you want to know more details you need to ask a cloud operator.

Quoted 7 lines
Quoted 3 lines

So it's not the changes that are problematic, it's that you have customers who are not using vdpa. The "most important thing" that vdpa fails at is simply converting your customers from vfio to vdpa.

I said the most important thing was that VFIO presents exactly the same virtio device to the VM as the baremetal. Do you dispute that, technically, VDPA does not actually achieve that?

I dispute that it is the most important. The important thing is to have guests work.

Quoted 6 lines

Then why is it so surprising that people don't want a solution that changes the vPCI ABI they worked hard to create in the first place?

I'm still baffled why you think everyone should use vdpa..

Jason

They shouldn't. If you want proprietary extensions then vfio is the way to go, I don't think vdpa will support that.

Signature · 6 lines
MST

_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization
#113 lore raw

Re: [PATCH vfio 11/11] vfio/virtio: Introduce a vfio driver over virtio devices

In reply to Jason Gunthorpe
Quoted 12 lines · Jason Gunthorpe
Quoted 8 lines · Jason Wang
Quoted 2 lines

Cloud vendors will similarly use DPUs to create a PCI functions that meet the cloud vendor's internal specification.

This can only work if:

1) the internal specification has finer garin than virtio spec
2) so it can define what is not implemented in the virtio spec (like migration and compatibility)

Quoted 7 lines

Yes, and that is what is happening. Realistically the "spec" isjust a piece of software that the Cloud vendor owns which is simply ported to multiple DPU vendors.

It is the same as VDPA. If VDPA can make multiple NIC vendors consistent then why do you have a hard time believing we can do the same thing just on the ARM side of a DPU?

I don't. We all know vDPA can do more than virtio.

Quoted 7 lines
Quoted 5 lines

All of the above doesn't seem to be possible or realistic now, and it actually has a risk to be not compatible with virtio spec. In the future when virtio has live migration supported, they want to be able to migrate between virtio and vDPA.

Well, that is for the spec to design.

Right, so if we'd consider migration from virtio to vDPA, it needs to be designed in a way that allows more involvement from hypervisor other than coupling it with a specific interface (like admin virtqueues).

Quoted 16 lines
Quoted 6 lines
Quoted 3 lines

So, as I keep saying, in this scenario the goal is no mediation in the hypervisor.

That's pretty fine, but I don't think trapping + relying is not mediation. Does it really matter what happens after trapping?

It is not mediation in the sense that the kernel driver does not in any way make decisions on the behavior of the device. It simply transforms an IO operation into a device command and relays it to the device. The device still fully controls its own behavior.

VDPA is very different from this. You might call them both mediation, sure, but then you need another word to describe the additional changes VPDA is doing.

Quoted 7 lines
Quoted 5 lines
Quoted 2 lines

It is pointless, everything you think you need to do there is actually already being done in the DPU.

Well, migration or even Qemu could be offloaded to DPU as well. If that's the direction that's pretty fine.

That's silly, of course qemu/kvm can't run in the DPU.

KVM can't for sure but part of Qemu could. This model has been used.

Quoted 8 lines

However, we can empty qemu and the hypervisor out so all it does is run kvm and run vfio. In this model the DPU does all the OVS, storage, "VPDA", etc. qemu is just a passive relay of the DPU PCI functions into VM's vPCI functions.

So, everything VDPA was doing in the environment is migrated into the DPU.

It really depends on the use cases. For example, in the case of DPU what if we want to provide multiple virtio devices through a single VF?

Quoted 4 lines

In this model the DPU is an extension of the hypervisor/qemu environment and we shift code from x86 side to arm side to increase security, save power and increase total system performance.

That's pretty fine.

Thanks

Quoted 3 lines

Jason

_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization

#114 lore raw

RE: [PATCH vfio 11/11] vfio/virtio: Introduce a vfio driver over virtio devices

Quoted 8 lines

From: Jason Wang <[email protected]>
Sent: Tuesday, September 26, 2023 10:08 AM

Right, so if we'd consider migration from virtio to vDPA, it needs to be designed in a way that allows more involvement from hypervisor other than coupling it with a specific interface (like admin virtqueues).

It is not attached to the admin virtqueues.
One way to use it using admin commands at [1].
One can define without admin command by explaining the technical difficulties in admin command may/cannot work.

[1] https://lists.oasis-open.org/archives/virtio-comment/202309/msg00061.html

#115 lore raw

Re: [PATCH vfio 11/11] vfio/virtio: Introduce a vfio driver over virtio devices

In reply to Jason Gunthorpe
Quoted 5 lines · Jason Gunthorpe
Quoted 1 line

What is the huge amount of work am I asking to do?

You are asking us to invest in the complexity of VDPA through out (keep it working, keep it secure, invest time in deploying and debugging in the field)

I'm asking you to do nothing of the kind - I am saying that this code will have to be duplicated in vdpa, and so I am asking what exactly is missing to just keep it all there. So far you said iommufd and note I didn't ask you to add iommufd to vdpa though that would be nice ;) I just said I'll look into it in the next several days.

Signature · 6 lines
MST

_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization
#116 lore raw

Re: [PATCH vfio 11/11] vfio/virtio: Introduce a vfio driver over virtio devices

Quoted 9 lines · Michael S. Tsirkin
Quoted 5 lines · Jason Gunthorpe
Quoted 1 line

What is the huge amount of work am I asking to do?

You are asking us to invest in the complexity of VDPA through out (keep it working, keep it secure, invest time in deploying and debugging in the field)

I'm asking you to do nothing of the kind - I am saying that this code will have to be duplicated in vdpa,

Why would that be needed?

Quoted 2 lines

and so I am asking what exactly is missing to just keep it all there.

VFIO. Seriously, we don't want unnecessary mediation in this path at all.

Quoted 2 lines

note I didn't ask you to add iommufd to vdpa though that would be nice ;)

I did once send someone to look.. It didn't succeed :(

Jason

#117 lore raw

Re: [PATCH vfio 11/11] vfio/virtio: Introduce a vfio driver over virtio devices

Quoted 4 lines · Jason Gunthorpe
Quoted 3 lines · Michael S. Tsirkin
Quoted 2 lines · Jason Gunthorpe
Quoted 1 line

What is the huge amount of work am I asking to do?

Quoted 8 lines
Quoted 6 lines
Quoted 3 lines

You are asking us to invest in the complexity of VDPA through out (keep it working, keep it secure, invest time in deploying and debugging in the field)

I'm asking you to do nothing of the kind - I am saying that this code will have to be duplicated in vdpa,

Why would that be needed?

For the same reason it was developed in the 1st place - presumably because it adds efficient legacy guest support with the right card? I get it, you specifically don't need VDPA functionality, but I don't see why is this universal, or common.

Quoted 5 lines
Quoted 2 lines

and so I am asking what exactly is missing to just keep it all there.

VFIO. Seriously, we don't want unnecessary mediation in this path at all.

But which mediation is necessary is exactly up to the specific use-case. I have no idea why would you want all of VFIO to e.g. pass access to random config registers to the guest when it's a virtio device and the config registers are all nicely listed in the spec. I know nvidia hardware is so great, it has super robust cards with less security holes than the vdpa driver, but I very much doubt this is universal for all virtio offload cards.

Quoted 6 lines
Quoted 2 lines

note I didn't ask you to add iommufd to vdpa though that would be nice ;)

I did once send someone to look.. It didn't succeed :(

Jason

Pity. Maybe there's some big difficulty blocking this? I'd like to know.

Signature · 6 lines
MST

_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization
#118 lore raw

Re: [PATCH vfio 11/11] vfio/virtio: Introduce a vfio driver over virtio devices

Quoted 7 lines · Michael S. Tsirkin

But which mediation is necessary is exactly up to the specific use-case. I have no idea why would you want all of VFIO to e.g. pass access to random config registers to the guest when it's a virtio device and the config registers are all nicely listed in the spec. I know nvidia hardware is so great, it has super robust cards with less security holes than the vdpa driver, but I very much doubt this is universal for all virtio offload cards.

The great thing about choice is that people can choose the configuration that best meets their situation and needs.

Jason

#119 lore raw

Re: [PATCH vfio 11/11] vfio/virtio: Introduce a vfio driver over virtio devices

In reply to Michael S. Tsirkin
Quoted 20 lines · Michael S. Tsirkin
Quoted 9 lines · Jason Gunthorpe
Quoted 7 lines · Michael S. Tsirkin
Quoted 4 lines · Jason Gunthorpe
Quoted 1 line

What is the huge amount of work am I asking to do?

You are asking us to invest in the complexity of VDPA through out (keep it working, keep it secure, invest time in deploying and debugging in the field)

I'm asking you to do nothing of the kind - I am saying that this code will have to be duplicated in vdpa,

Why would that be needed?

For the same reason it was developed in the 1st place - presumably because it adds efficient legacy guest support with the right card? I get it, you specifically don't need VDPA functionality, but I don't see why is this universal, or common.

Quoted 4 lines
Quoted 2 lines

and so I am asking what exactly is missing to just keep it all there.

VFIO. Seriously, we don't want unnecessary mediation in this path at all.

Quoted 7 lines

But which mediation is necessary is exactly up to the specific use-case. I have no idea why would you want all of VFIO to e.g. pass access to random config registers to the guest when it's a virtio device and the config registers are all nicely listed in the spec. I know nvidia hardware is so great, it has super robust cards with less security holes than the vdpa driver, but I very much doubt this is universal for all virtio offload cards.

I agree with MST.

Quoted 7 lines
Quoted 5 lines
Quoted 2 lines

note I didn't ask you to add iommufd to vdpa though that would be nice ;)

I did once send someone to look.. It didn't succeed :(

Jason

Pity. Maybe there's some big difficulty blocking this? I'd like to know.

#120 lore raw

Re: [PATCH vfio 11/11] vfio/virtio: Introduce a vfio driver over virtio devices

In reply to Jason Gunthorpe
Quoted 7 lines · Jason Gunthorpe
Quoted 6 lines · Michael S. Tsirkin
Quoted 6 lines

vdpa is not vfio, I don't know how you can suggest vdpa is a replacement for a vfio driver. They are completely different things.
Each side has its own strengths, and vfio especially is accelerating in its capability in way that vpda is not. eg if an iommufd conversion had been done by now for vdpa I might be more sympathetic.

Quoted 5 lines
Quoted 4 lines

Yea, I agree iommufd is a big problem with vdpa right now. Cindy was sick and I didn't know and kept assuming she's working on this. I don't think it's a huge amount of work though. I'll take a look. Is there anything else though? Do tell.

Confidential compute will never work with VDPA's approach.

I don't understand why vDPA can not and will never support Confidential computing?

Do you see any blockers?

Quoted 15 lines
Quoted 4 lines

There are a bunch of things that I think are important for virtio that are completely out of scope for vfio, such as migrating cross-vendor.

VFIO supports migration, if you want to have cross-vendor migration then make a standard that describes the VFIO migration data format for virtio devices.

Quoted 1 line

What is the huge amount of work am I asking to do?

You are asking us to invest in the complexity of VDPA through out (keep it working, keep it secure, invest time in deploying and debugging in the field)

When it doesn't provide *ANY* value to the solution.

Quoted 7 lines

The starting point is a completely working vfio PCI function and the end goal is to put that function into a VM. That is VFIO, not VDPA.

VPDA is fine for what it does, but it is not a reasonable replacement for VFIO.

Jason

_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization

#121 lore raw

Re: [PATCH vfio 11/11] vfio/virtio: Introduce a vfio driver over virtio devices

In reply to Yishai Hadas

drivers/vfio/pci/virtio/main.c line 22

Quoted 2 lines · Yishai Hadas
drivers/vfio/pci/virtio/main.c new file +2 -0
+#define VIRTIO_LEGACY_IO_BAR_HEADER_LEN 20
+#define VIRTIO_LEGACY_IO_BAR_MSIX_HEADER_LEN 4

This is exactly part of VIRTIO_PCI_CONFIG_OFF duplicated.

Signature · 6 lines
MST

_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization
#122 lore raw

Re: [PATCH vfio 11/11] vfio/virtio: Introduce a vfio driver over virtio devices

In reply to Yishai Hadas
Quoted 55 lines · On Thu, 21 Sep 2023 15:40:40 +0300 Yishai Hadas <[email protected]> wrote:

Introduce a vfio driver over virtio devices to support the legacy interface functionality for VFs.

Background, from the virtio spec [1].
--------------------------------------------------------------------
In some systems, there is a need to support a virtio legacy driver with a device that does not directly support the legacy interface. In such scenarios, a group owner device can provide the legacy interface functionality for the group member devices. The driver of the owner device can then access the legacy interface of a member device on behalf of the legacy member device driver.

For example, with the SR-IOV group type, group members (VFs) can not present the legacy interface in an I/O BAR in BAR0 as expected by the legacy pci driver. If the legacy driver is running inside a virtual machine, the hypervisor executing the virtual machine can present a virtual device with an I/O BAR in BAR0. The hypervisor intercepts the legacy driver accesses to this I/O BAR and forwards them to the group owner device (PF) using group administration commands.
--------------------------------------------------------------------

Specifically, this driver adds support for a virtio-net VF to be exposed as a transitional device to a guest driver and allows the legacy IO BAR functionality on top.

This allows a VM which uses a legacy virtio-net driver in the guest to work transparently over a VF which its driver in the host is that new driver.

The driver can be extended easily to support some other types of virtio devices (e.g virtio-blk), by adding in a few places the specific type properties as was done for virtio-net.

For now, only the virtio-net use case was tested and as such we introduce the support only for such a device.

Practically,
Upon probing a VF for a virtio-net device, in case its PF supports legacy access over the virtio admin commands and the VF doesn't have BAR 0, we set some specific 'vfio_device_ops' to be able to simulate in SW a transitional device with I/O BAR in BAR 0.

The existence of the simulated I/O bar is reported later on by overwriting the VFIO_DEVICE_GET_REGION_INFO command and the device exposes itself as a transitional device by overwriting some properties upon reading its config space.

Once we report the existence of I/O BAR as BAR 0 a legacy driver in the guest may use it via read/write calls according to the virtio specification.

Any read/write towards the control parts of the BAR will be captured by the new driver and will be translated into admin commands towards the device.

Quoted 7 lines

Any data path read/write access (i.e. virtio driver notifications) will be forwarded to the physical BAR which its properties were supplied by the command VIRTIO_PCI_QUEUE_NOTIFY upon the probing/init flow.

With that code in place a legacy driver in the guest has the look and feel as if having a transitional device with legacy support for both its control and data path flows.

Why do we need to enable a "legacy" driver in the guest? The very name suggests there's an alternative driver that perhaps doesn't require this I/O BAR. Why don't we just require the non-legacy driver in the guest rather than increase our maintenance burden? Thanks,

Alex

Quoted 672 lines

[1]
https://github.com/oasis-tcs/virtio-spec/commit/03c2d32e5093ca9f2a17797242fbef88efe94b8c

---
 MAINTAINERS                      |   6 +
 drivers/vfio/pci/Kconfig         |   2 +
 drivers/vfio/pci/Makefile        |   2 +
 drivers/vfio/pci/virtio/Kconfig  |  15 +
 drivers/vfio/pci/virtio/Makefile |   4 +
 drivers/vfio/pci/virtio/cmd.c    |   4 +-
 drivers/vfio/pci/virtio/cmd.h    |   8 +
 drivers/vfio/pci/virtio/main.c   | 546 +++++++++++++++++++++++++++++++
 8 files changed, 585 insertions(+), 2 deletions(-)
 create mode 100644 drivers/vfio/pci/virtio/Kconfig
 create mode 100644 drivers/vfio/pci/virtio/Makefile
 create mode 100644 drivers/vfio/pci/virtio/main.c
MAINTAINERS +6 -0
index bf0f54c24f81..5098418c8389 100644
@@ -22624,6 +22624,12 @@ L:	[email protected]
 S:	Maintained
 F:	drivers/vfio/pci/mlx5/
 
+VFIO VIRTIO PCI DRIVER
+M:	Yishai Hadas <[email protected]>
+L:	[email protected]
+S:	Maintained
+F:	drivers/vfio/pci/virtio
+
 VFIO PCI DEVICE SPECIFIC DRIVERS
 R:	Jason Gunthorpe <[email protected]>
 R:	Yishai Hadas <[email protected]>
drivers/vfio/pci/Kconfig +2 -0
index 8125e5f37832..18c397df566d 100644
@@ -65,4 +65,6 @@ source "drivers/vfio/pci/hisilicon/Kconfig"
 
 source "drivers/vfio/pci/pds/Kconfig"
 
+source "drivers/vfio/pci/virtio/Kconfig"
+
 endmenu
drivers/vfio/pci/Makefile +2 -0
index 45167be462d8..046139a4eca5 100644
@@ -13,3 +13,5 @@ obj-$(CONFIG_MLX5_VFIO_PCI)           += mlx5/
 obj-$(CONFIG_HISI_ACC_VFIO_PCI) += hisilicon/
 
 obj-$(CONFIG_PDS_VFIO_PCI) += pds/
+
+obj-$(CONFIG_VIRTIO_VFIO_PCI) += virtio/
drivers/vfio/pci/virtio/Kconfig new file +15 -0
new file mode 100644
index 000000000000..89eddce8b1bd
@@ -0,0 +1,15 @@
+# SPDX-License-Identifier: GPL-2.0-only
+config VIRTIO_VFIO_PCI
+        tristate "VFIO support for VIRTIO PCI devices"
+        depends on VIRTIO_PCI
+        select VFIO_PCI_CORE
+        help
+          This provides support for exposing VIRTIO VF devices using the VFIO
+          framework that can work with a legacy virtio driver in the guest.
+          Based on PCIe spec, VFs do not support I/O Space; thus, VF BARs shall
+          not indicate I/O Space.
+          As of that this driver emulated I/O BAR in software to let a VF be
+          seen as a transitional device in the guest and let it work with
+          a legacy driver.
+
+          If you don't know what to do here, say N.
drivers/vfio/pci/virtio/Makefile new file +4 -0
new file mode 100644
index 000000000000..584372648a03
@@ -0,0 +1,4 @@
+# SPDX-License-Identifier: GPL-2.0-only
+obj-$(CONFIG_VIRTIO_VFIO_PCI) += virtio-vfio-pci.o
+virtio-vfio-pci-y := main.o cmd.o
+
drivers/vfio/pci/virtio/cmd.c +2 -2
index f068239cdbb0..aea9d25fbf1d 100644
@@ -44,7 +44,7 @@ int virtiovf_cmd_lr_write(struct virtiovf_pci_core_device *virtvdev, u16 opcode,
 {
 	struct virtio_device *virtio_dev =
 		virtio_pci_vf_get_pf_dev(virtvdev->core_device.pdev);
-	struct virtio_admin_cmd_data_lr_write *in;
+	struct virtio_admin_cmd_legacy_wr_data *in;
 	struct scatterlist in_sg;
 	struct virtio_admin_cmd cmd = {};
 	int ret;
@@ -74,7 +74,7 @@ int virtiovf_cmd_lr_read(struct virtiovf_pci_core_device *virtvdev, u16 opcode,
 {
 	struct virtio_device *virtio_dev =
 		virtio_pci_vf_get_pf_dev(virtvdev->core_device.pdev);
-	struct virtio_admin_cmd_data_lr_read *in;
+	struct virtio_admin_cmd_legacy_rd_data *in;
 	struct scatterlist in_sg, out_sg;
 	struct virtio_admin_cmd cmd = {};
 	int ret;
drivers/vfio/pci/virtio/cmd.h +8 -0
index c2a3645f4b90..347b1dc85570 100644
@@ -13,7 +13,15 @@
 
 struct virtiovf_pci_core_device {
 	struct vfio_pci_core_device core_device;
+	u8 bar0_virtual_buf_size;
+	u8 *bar0_virtual_buf;
+	/* synchronize access to the virtual buf */
+	struct mutex bar_mutex;
 	int vf_id;
+	void __iomem *notify_addr;
+	u32 notify_offset;
+	u8 notify_bar;
+	u8 pci_cmd_io :1;
 };
 
 int virtiovf_cmd_list_query(struct pci_dev *pdev, u8 *buf, int buf_size);
drivers/vfio/pci/virtio/main.c new file +536 -0
new file mode 100644
index 000000000000..2486991c49f3
@@ -0,0 +1,546 @@
+// SPDX-License-Identifier: GPL-2.0-only
+/*
+ * Copyright (c) 2023, NVIDIA CORPORATION & AFFILIATES. All rights reserved
+ */
+
+#include <linux/device.h>
+#include <linux/module.h>
+#include <linux/mutex.h>
+#include <linux/pci.h>
+#include <linux/pm_runtime.h>
+#include <linux/types.h>
+#include <linux/uaccess.h>
+#include <linux/vfio.h>
+#include <linux/vfio_pci_core.h>
+#include <linux/virtio_pci.h>
+#include <linux/virtio_net.h>
+#include <linux/virtio_pci_modern.h>
+
+#include "cmd.h"
+
+#define VIRTIO_LEGACY_IO_BAR_HEADER_LEN 20
+#define VIRTIO_LEGACY_IO_BAR_MSIX_HEADER_LEN 4
+
+static int virtiovf_issue_lr_cmd(struct virtiovf_pci_core_device *virtvdev,
+				 loff_t pos, char __user *buf,
+				 size_t count, bool read)
+{
+	u8 *bar0_buf = virtvdev->bar0_virtual_buf;
+	u16 opcode;
+	int ret;
+
+	mutex_lock(&virtvdev->bar_mutex);
+	if (read) {
+		opcode = (pos < VIRTIO_PCI_CONFIG_OFF(true)) ?
+			VIRTIO_ADMIN_CMD_LEGACY_COMMON_CFG_READ :
+			VIRTIO_ADMIN_CMD_LEGACY_DEV_CFG_READ;
+		ret = virtiovf_cmd_lr_read(virtvdev, opcode, pos,
+					   count, bar0_buf + pos);
+		if (ret)
+			goto out;
+		if (copy_to_user(buf, bar0_buf + pos, count))
+			ret = -EFAULT;
+		goto out;
+	}
+
+	if (copy_from_user(bar0_buf + pos, buf, count)) {
+		ret = -EFAULT;
+		goto out;
+	}
+
+	opcode = (pos < VIRTIO_PCI_CONFIG_OFF(true)) ?
+			VIRTIO_ADMIN_CMD_LEGACY_COMMON_CFG_WRITE :
+			VIRTIO_ADMIN_CMD_LEGACY_DEV_CFG_WRITE;
+	ret = virtiovf_cmd_lr_write(virtvdev, opcode, pos, count,
+				    bar0_buf + pos);
+out:
+	mutex_unlock(&virtvdev->bar_mutex);
+	return ret;
+}
+
+static int
+translate_io_bar_to_mem_bar(struct virtiovf_pci_core_device *virtvdev,
+			    loff_t pos, char __user *buf,
+			    size_t count, bool read)
+{
+	struct vfio_pci_core_device *core_device = &virtvdev->core_device;
+	u16 queue_notify;
+	int ret;
+
+	if (pos + count > virtvdev->bar0_virtual_buf_size)
+		return -EINVAL;
+
+	switch (pos) {
+	case VIRTIO_PCI_QUEUE_NOTIFY:
+		if (count != sizeof(queue_notify))
+			return -EINVAL;
+		if (read) {
+			ret = vfio_pci_ioread16(core_device, true, &queue_notify,
+						virtvdev->notify_addr);
+			if (ret)
+				return ret;
+			if (copy_to_user(buf, &queue_notify,
+					 sizeof(queue_notify)))
+				return -EFAULT;
+			break;
+		}
+
+		if (copy_from_user(&queue_notify, buf, count))
+			return -EFAULT;
+
+		ret = vfio_pci_iowrite16(core_device, true, queue_notify,
+					 virtvdev->notify_addr);
+		break;
+	default:
+		ret = virtiovf_issue_lr_cmd(virtvdev, pos, buf, count, read);
+	}
+
+	return ret ? ret : count;
+}
+
+static bool range_contains_range(loff_t range1_start, size_t count1,
+				 loff_t range2_start, size_t count2,
+				 loff_t *start_offset)
+{
+	if (range1_start <= range2_start &&
+	    range1_start + count1 >= range2_start + count2) {
+		*start_offset = range2_start - range1_start;
+		return true;
+	}
+	return false;
+}
+
+static ssize_t virtiovf_pci_read_config(struct vfio_device *core_vdev,
+					char __user *buf, size_t count,
+					loff_t *ppos)
+{
+	struct virtiovf_pci_core_device *virtvdev = container_of(
+		core_vdev, struct virtiovf_pci_core_device, core_device.vdev);
+	loff_t pos = *ppos & VFIO_PCI_OFFSET_MASK;
+	loff_t copy_offset;
+	__le32 val32;
+	__le16 val16;
+	u8 val8;
+	int ret;
+
+	ret = vfio_pci_core_read(core_vdev, buf, count, ppos);
+	if (ret < 0)
+		return ret;
+
+	if (range_contains_range(pos, count, PCI_DEVICE_ID, sizeof(val16),
+				 &copy_offset)) {
+		val16 = cpu_to_le16(0x1000);
+		if (copy_to_user(buf + copy_offset, &val16, sizeof(val16)))
+			return -EFAULT;
+	}
+
+	if (virtvdev->pci_cmd_io &&
+	    range_contains_range(pos, count, PCI_COMMAND, sizeof(val16),
+				 &copy_offset)) {
+		if (copy_from_user(&val16, buf, sizeof(val16)))
+			return -EFAULT;
+		val16 |= cpu_to_le16(PCI_COMMAND_IO);
+		if (copy_to_user(buf + copy_offset, &val16, sizeof(val16)))
+			return -EFAULT;
+	}
+
+	if (range_contains_range(pos, count, PCI_REVISION_ID, sizeof(val8),
+				 &copy_offset)) {
+		/* Transional needs to have revision 0 */
+		val8 = 0;
+		if (copy_to_user(buf + copy_offset, &val8, sizeof(val8)))
+			return -EFAULT;
+	}
+
+	if (range_contains_range(pos, count, PCI_BASE_ADDRESS_0, sizeof(val32),
+				 &copy_offset)) {
+		val32 = cpu_to_le32(PCI_BASE_ADDRESS_SPACE_IO);
+		if (copy_to_user(buf + copy_offset, &val32, sizeof(val32)))
+			return -EFAULT;
+	}
+
+	if (range_contains_range(pos, count, PCI_SUBSYSTEM_ID, sizeof(val16),
+				 &copy_offset)) {
+		/* Transitional devices use the PCI subsystem device id as
+		 * virtio device id, same as legacy driver always did.
+		 */
+		val16 = cpu_to_le16(VIRTIO_ID_NET);
+		if (copy_to_user(buf + copy_offset, &val16, sizeof(val16)))
+			return -EFAULT;
+	}
+
+	return count;
+}
+
+static ssize_t
+virtiovf_pci_core_read(struct vfio_device *core_vdev, char __user *buf,
+		       size_t count, loff_t *ppos)
+{
+	struct virtiovf_pci_core_device *virtvdev = container_of(
+		core_vdev, struct virtiovf_pci_core_device, core_device.vdev);
+	struct pci_dev *pdev = virtvdev->core_device.pdev;
+	unsigned int index = VFIO_PCI_OFFSET_TO_INDEX(*ppos);
+	loff_t pos = *ppos & VFIO_PCI_OFFSET_MASK;
+	int ret;
+
+	if (!count)
+		return 0;
+
+	if (index == VFIO_PCI_CONFIG_REGION_INDEX)
+		return virtiovf_pci_read_config(core_vdev, buf, count, ppos);
+
+	if (index != VFIO_PCI_BAR0_REGION_INDEX)
+		return vfio_pci_core_read(core_vdev, buf, count, ppos);
+
+	ret = pm_runtime_resume_and_get(&pdev->dev);
+	if (ret) {
+		pci_info_ratelimited(pdev, "runtime resume failed %d\n",
+				     ret);
+		return -EIO;
+	}
+
+	ret = translate_io_bar_to_mem_bar(virtvdev, pos, buf, count, true);
+	pm_runtime_put(&pdev->dev);
+	return ret;
+}
+
+static ssize_t
+virtiovf_pci_core_write(struct vfio_device *core_vdev, const char __user *buf,
+			size_t count, loff_t *ppos)
+{
+	struct virtiovf_pci_core_device *virtvdev = container_of(
+		core_vdev, struct virtiovf_pci_core_device, core_device.vdev);
+	struct pci_dev *pdev = virtvdev->core_device.pdev;
+	unsigned int index = VFIO_PCI_OFFSET_TO_INDEX(*ppos);
+	loff_t pos = *ppos & VFIO_PCI_OFFSET_MASK;
+	int ret;
+
+	if (!count)
+		return 0;
+
+	if (index == VFIO_PCI_CONFIG_REGION_INDEX) {
+		loff_t copy_offset;
+		u16 cmd;
+
+		if (range_contains_range(pos, count, PCI_COMMAND, sizeof(cmd),
+					 &copy_offset)) {
+			if (copy_from_user(&cmd, buf + copy_offset, sizeof(cmd)))
+				return -EFAULT;
+			virtvdev->pci_cmd_io = (cmd & PCI_COMMAND_IO);
+		}
+	}
+
+	if (index != VFIO_PCI_BAR0_REGION_INDEX)
+		return vfio_pci_core_write(core_vdev, buf, count, ppos);
+
+	ret = pm_runtime_resume_and_get(&pdev->dev);
+	if (ret) {
+		pci_info_ratelimited(pdev, "runtime resume failed %d\n", ret);
+		return -EIO;
+	}
+
+	ret = translate_io_bar_to_mem_bar(virtvdev, pos, (char __user *)buf, count, false);
+	pm_runtime_put(&pdev->dev);
+	return ret;
+}
+
+static int
+virtiovf_pci_ioctl_get_region_info(struct vfio_device *core_vdev,
+				   unsigned int cmd, unsigned long arg)
+{
+	struct virtiovf_pci_core_device *virtvdev = container_of(
+		core_vdev, struct virtiovf_pci_core_device, core_device.vdev);
+	unsigned long minsz = offsetofend(struct vfio_region_info, offset);
+	void __user *uarg = (void __user *)arg;
+	struct vfio_region_info info = {};
+
+	if (copy_from_user(&info, uarg, minsz))
+		return -EFAULT;
+
+	if (info.argsz < minsz)
+		return -EINVAL;
+
+	switch (info.index) {
+	case VFIO_PCI_BAR0_REGION_INDEX:
+		info.offset = VFIO_PCI_INDEX_TO_OFFSET(info.index);
+		info.size = virtvdev->bar0_virtual_buf_size;
+		info.flags = VFIO_REGION_INFO_FLAG_READ |
+			     VFIO_REGION_INFO_FLAG_WRITE;
+		return copy_to_user(uarg, &info, minsz) ? -EFAULT : 0;
+	default:
+		return vfio_pci_core_ioctl(core_vdev, cmd, arg);
+	}
+}
+
+static long
+virtiovf_vfio_pci_core_ioctl(struct vfio_device *core_vdev, unsigned int cmd,
+			     unsigned long arg)
+{
+	switch (cmd) {
+	case VFIO_DEVICE_GET_REGION_INFO:
+		return virtiovf_pci_ioctl_get_region_info(core_vdev, cmd, arg);
+	default:
+		return vfio_pci_core_ioctl(core_vdev, cmd, arg);
+	}
+}
+
+static int
+virtiovf_set_notify_addr(struct virtiovf_pci_core_device *virtvdev)
+{
+	struct vfio_pci_core_device *core_device = &virtvdev->core_device;
+	int ret;
+
+	/* Setup the BAR where the 'notify' exists to be used by vfio as well
+	 * This will let us mmap it only once and use it when needed.
+	 */
+	ret = vfio_pci_core_setup_barmap(core_device,
+					 virtvdev->notify_bar);
+	if (ret)
+		return ret;
+
+	virtvdev->notify_addr = core_device->barmap[virtvdev->notify_bar] +
+			virtvdev->notify_offset;
+	return 0;
+}
+
+static int virtiovf_pci_open_device(struct vfio_device *core_vdev)
+{
+	struct virtiovf_pci_core_device *virtvdev = container_of(
+		core_vdev, struct virtiovf_pci_core_device, core_device.vdev);
+	struct vfio_pci_core_device *vdev = &virtvdev->core_device;
+	int ret;
+
+	ret = vfio_pci_core_enable(vdev);
+	if (ret)
+		return ret;
+
+	if (virtvdev->bar0_virtual_buf) {
+		/* upon close_device() the vfio_pci_core_disable() is called
+		 * and will close all the previous mmaps, so it seems that the
+		 * valid life cycle for the 'notify' addr is per open/close.
+		 */
+		ret = virtiovf_set_notify_addr(virtvdev);
+		if (ret) {
+			vfio_pci_core_disable(vdev);
+			return ret;
+		}
+	}
+
+	vfio_pci_core_finish_enable(vdev);
+	return 0;
+}
+
+static void virtiovf_pci_close_device(struct vfio_device *core_vdev)
+{
+	vfio_pci_core_close_device(core_vdev);
+}
+
+static int virtiovf_get_device_config_size(unsigned short device)
+{
+	switch (device) {
+	case 0x1041:
+		/* network card */
+		return offsetofend(struct virtio_net_config, status);
+	default:
+		return 0;
+	}
+}
+
+static int virtiovf_read_notify_info(struct virtiovf_pci_core_device *virtvdev)
+{
+	u64 offset;
+	int ret;
+	u8 bar;
+
+	ret = virtiovf_cmd_lq_read_notify(virtvdev,
+				VIRTIO_ADMIN_CMD_NOTIFY_INFO_FLAGS_OWNER_MEM,
+				&bar, &offset);
+	if (ret)
+		return ret;
+
+	virtvdev->notify_bar = bar;
+	virtvdev->notify_offset = offset;
+	return 0;
+}
+
+static int virtiovf_pci_init_device(struct vfio_device *core_vdev)
+{
+	struct virtiovf_pci_core_device *virtvdev = container_of(
+		core_vdev, struct virtiovf_pci_core_device, core_device.vdev);
+	struct pci_dev *pdev;
+	int ret;
+
+	ret = vfio_pci_core_init_dev(core_vdev);
+	if (ret)
+		return ret;
+
+	pdev = virtvdev->core_device.pdev;
+	virtvdev->vf_id = pci_iov_vf_id(pdev);
+	if (virtvdev->vf_id < 0)
+		return -EINVAL;
+
+	ret = virtiovf_read_notify_info(virtvdev);
+	if (ret)
+		return ret;
+
+	virtvdev->bar0_virtual_buf_size = VIRTIO_LEGACY_IO_BAR_HEADER_LEN +
+		VIRTIO_LEGACY_IO_BAR_MSIX_HEADER_LEN +
+		virtiovf_get_device_config_size(pdev->device);
+	virtvdev->bar0_virtual_buf = kzalloc(virtvdev->bar0_virtual_buf_size,
+					     GFP_KERNEL);
+	if (!virtvdev->bar0_virtual_buf)
+		return -ENOMEM;
+	mutex_init(&virtvdev->bar_mutex);
+	return 0;
+}
+
+static void virtiovf_pci_core_release_dev(struct vfio_device *core_vdev)
+{
+	struct virtiovf_pci_core_device *virtvdev = container_of(
+		core_vdev, struct virtiovf_pci_core_device, core_device.vdev);
+
+	kfree(virtvdev->bar0_virtual_buf);
+	vfio_pci_core_release_dev(core_vdev);
+}
+
+static const struct vfio_device_ops virtiovf_acc_vfio_pci_tran_ops = {
+	.name = "virtio-transitional-vfio-pci",
+	.init = virtiovf_pci_init_device,
+	.release = virtiovf_pci_core_release_dev,
+	.open_device = virtiovf_pci_open_device,
+	.close_device = virtiovf_pci_close_device,
+	.ioctl = virtiovf_vfio_pci_core_ioctl,
+	.read = virtiovf_pci_core_read,
+	.write = virtiovf_pci_core_write,
+	.mmap = vfio_pci_core_mmap,
+	.request = vfio_pci_core_request,
+	.match = vfio_pci_core_match,
+	.bind_iommufd = vfio_iommufd_physical_bind,
+	.unbind_iommufd = vfio_iommufd_physical_unbind,
+	.attach_ioas = vfio_iommufd_physical_attach_ioas,
+};
+
+static const struct vfio_device_ops virtiovf_acc_vfio_pci_ops = {
+	.name = "virtio-acc-vfio-pci",
+	.init = vfio_pci_core_init_dev,
+	.release = vfio_pci_core_release_dev,
+	.open_device = virtiovf_pci_open_device,
+	.close_device = virtiovf_pci_close_device,
+	.ioctl = vfio_pci_core_ioctl,
+	.device_feature = vfio_pci_core_ioctl_feature,
+	.read = vfio_pci_core_read,
+	.write = vfio_pci_core_write,
+	.mmap = vfio_pci_core_mmap,
+	.request = vfio_pci_core_request,
+	.match = vfio_pci_core_match,
+	.bind_iommufd = vfio_iommufd_physical_bind,
+	.unbind_iommufd = vfio_iommufd_physical_unbind,
+	.attach_ioas = vfio_iommufd_physical_attach_ioas,
+};
+
+static bool virtiovf_bar0_exists(struct pci_dev *pdev)
+{
+	struct resource *res = pdev->resource;
+
+	return res->flags ? true : false;
+}
+
+#define VIRTIOVF_USE_ADMIN_CMD_BITMAP \
+	(BIT_ULL(VIRTIO_ADMIN_CMD_LIST_QUERY) | \
+	 BIT_ULL(VIRTIO_ADMIN_CMD_LIST_USE) | \
+	 BIT_ULL(VIRTIO_ADMIN_CMD_LEGACY_COMMON_CFG_WRITE) | \
+	 BIT_ULL(VIRTIO_ADMIN_CMD_LEGACY_COMMON_CFG_READ) | \
+	 BIT_ULL(VIRTIO_ADMIN_CMD_LEGACY_DEV_CFG_WRITE) | \
+	 BIT_ULL(VIRTIO_ADMIN_CMD_LEGACY_DEV_CFG_READ) | \
+	 BIT_ULL(VIRTIO_ADMIN_CMD_LEGACY_NOTIFY_INFO))
+
+static bool virtiovf_support_legacy_access(struct pci_dev *pdev)
+{
+	int buf_size = DIV_ROUND_UP(VIRTIO_ADMIN_MAX_CMD_OPCODE, 64) * 8;
+	u8 *buf;
+	int ret;
+
+	/* Only virtio-net is supported/tested so far */
+	if (pdev->device != 0x1041)
+		return false;
+
+	buf = kzalloc(buf_size, GFP_KERNEL);
+	if (!buf)
+		return false;
+
+	ret = virtiovf_cmd_list_query(pdev, buf, buf_size);
+	if (ret)
+		goto end;
+
+	if ((le64_to_cpup((__le64 *)buf) & VIRTIOVF_USE_ADMIN_CMD_BITMAP) !=
+		VIRTIOVF_USE_ADMIN_CMD_BITMAP) {
+		ret = -EOPNOTSUPP;
+		goto end;
+	}
+
+	/* confirm the used commands */
+	memset(buf, 0, buf_size);
+	*(__le64 *)buf = cpu_to_le64(VIRTIOVF_USE_ADMIN_CMD_BITMAP);
+	ret = virtiovf_cmd_list_use(pdev, buf, buf_size);
+
+end:
+	kfree(buf);
+	return ret ? false : true;
+}
+
+static int virtiovf_pci_probe(struct pci_dev *pdev,
+			      const struct pci_device_id *id)
+{
+	const struct vfio_device_ops *ops = &virtiovf_acc_vfio_pci_ops;
+	struct virtiovf_pci_core_device *virtvdev;
+	int ret;
+
+	if (pdev->is_virtfn && virtiovf_support_legacy_access(pdev) &&
+	    !virtiovf_bar0_exists(pdev) && pdev->msix_cap)
+		ops = &virtiovf_acc_vfio_pci_tran_ops;
+
+	virtvdev = vfio_alloc_device(virtiovf_pci_core_device, core_device.vdev,
+				     &pdev->dev, ops);
+	if (IS_ERR(virtvdev))
+		return PTR_ERR(virtvdev);
+
+	dev_set_drvdata(&pdev->dev, &virtvdev->core_device);
+	ret = vfio_pci_core_register_device(&virtvdev->core_device);
+	if (ret)
+		goto out;
+	return 0;
+out:
+	vfio_put_device(&virtvdev->core_device.vdev);
+	return ret;
+}
+
+static void virtiovf_pci_remove(struct pci_dev *pdev)
+{
+	struct virtiovf_pci_core_device *virtvdev = dev_get_drvdata(&pdev->dev);
+
+	vfio_pci_core_unregister_device(&virtvdev->core_device);
+	vfio_put_device(&virtvdev->core_device.vdev);
+}
+
+static const struct pci_device_id virtiovf_pci_table[] = {
+	{ PCI_DRIVER_OVERRIDE_DEVICE_VFIO(PCI_VENDOR_ID_REDHAT_QUMRANET, PCI_ANY_ID) },
+	{}
+};
+
+MODULE_DEVICE_TABLE(pci, virtiovf_pci_table);
+
+static struct pci_driver virtiovf_pci_driver = {
+	.name = KBUILD_MODNAME,
+	.id_table = virtiovf_pci_table,
+	.probe = virtiovf_pci_probe,
+	.remove = virtiovf_pci_remove,

drivers/vfio/pci/virtio/main.c line 546

Quoted 10 lines
drivers/vfio/pci/virtio/main.c new file +10 -0
+	.err_handler = &vfio_pci_core_err_handlers,
+	.driver_managed_dma = true,
+};
+
+module_pci_driver(virtiovf_pci_driver);
+
+MODULE_LICENSE("GPL");
+MODULE_AUTHOR("Yishai Hadas <[email protected]>");
+MODULE_DESCRIPTION(
+	"VIRTIO VFIO PCI - User Level meta-driver for VIRTIO device family");

_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization

#123 lore raw

Re: [PATCH vfio 11/11] vfio/virtio: Introduce a vfio driver over virtio devices

Quoted 8 lines · Alex Williamson
Quoted 3 lines

With that code in place a legacy driver in the guest has the look and feel as if having a transitional device with legacy support for both its control and data path flows.

Why do we need to enable a "legacy" driver in the guest? The very name suggests there's an alternative driver that perhaps doesn't require this I/O BAR. Why don't we just require the non-legacy driver in the guest rather than increase our maintenance burden? Thanks,

It was my reaction also.

Apparently there is a big deployed base of people using old guest VMs with old drivers and they do not want to update their VMs. It is the same basic reason why qemu supports all those weird old machine types and HW emulations. The desire is to support these old devices so that old VMs can work unchanged.

Jason

#124 lore raw

Re: [PATCH vfio 11/11] vfio/virtio: Introduce a vfio driver over virtio devices

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

With that code in place a legacy driver in the guest has the look and feel as if having a transitional device with legacy support for both its control and data path flows.

Why do we need to enable a "legacy" driver in the guest? The very name suggests there's an alternative driver that perhaps doesn't require this I/O BAR. Why don't we just require the non-legacy driver in the guest rather than increase our maintenance burden? Thanks,

It was my reaction also.

Quoted 7 lines

Apparently there is a big deployed base of people using old guest VMs with old drivers and they do not want to update their VMs. It is the same basic reason why qemu supports all those weird old machine types and HW emulations. The desire is to support these old devices so that old VMs can work unchanged.

Jason

And you are saying all these very old VMs use such a large number of legacy devices that over-counting of locked memory due to vdpa not correctly using iommufd is a problem that urgently needs to be solved otherwise the solution has no value?

Another question I'm interested in is whether there's actually a performance benefit to using this as compared to just software vhost. I note there's a VM exit on each IO access, so ... perhaps? Would be nice to see some numbers.

Signature · 6 lines
MST

_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization
#125 lore raw

RE: [PATCH vfio 11/11] vfio/virtio: Introduce a vfio driver over virtio devices

Quoted 9 lines

From: Michael S. Tsirkin <[email protected]>
Sent: Thursday, September 21, 2023 10:31 PM

Another question I'm interested in is whether there's actually a performance benefit to using this as compared to just software vhost. I note there's a VM exit on each IO access, so ... perhaps?
Would be nice to see some numbers.

Packet rate and bandwidth are close are only 10% lower than modern device due to the batching of driver notification. Bw tested with iperf with one and multiple queues.
Packet rate tested with testpmd.
_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization

#126 lore raw

Re: [PATCH vfio 11/11] vfio/virtio: Introduce a vfio driver over virtio devices

Quoted 5 lines · Parav Pandit
Quoted 4 lines

From: Michael S. Tsirkin <[email protected]>
Sent: Thursday, September 21, 2023 10:31 PM

Quoted 8 lines
Quoted 4 lines

Another question I'm interested in is whether there's actually a performance benefit to using this as compared to just software vhost. I note there's a VM exit on each IO access, so ... perhaps?
Would be nice to see some numbers.

Packet rate and bandwidth are close are only 10% lower than modern device due to the batching of driver notification. Bw tested with iperf with one and multiple queues.
Packet rate tested with testpmd.

Nice, good to know. Could you compare this with vdpa with shadow vq enabled? That's probably the closest equivalent that needs no kernel or hardware work.

Signature · 6 lines
MST

_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization
#127 lore raw

Re: [PATCH vfio 11/11] vfio/virtio: Introduce a vfio driver over virtio devices

In reply to Michael S. Tsirkin
Quoted 20 lines · Michael S. Tsirkin
Quoted 19 lines · Jason Gunthorpe
Quoted 8 lines · Alex Williamson
Quoted 3 lines

With that code in place a legacy driver in the guest has the look and feel as if having a transitional device with legacy support for both its control and data path flows.

Why do we need to enable a "legacy" driver in the guest? The very name suggests there's an alternative driver that perhaps doesn't require this I/O BAR. Why don't we just require the non-legacy driver in the guest rather than increase our maintenance burden? Thanks,

It was my reaction also.

Apparently there is a big deployed base of people using old guest VMs with old drivers and they do not want to update their VMs. It is the same basic reason why qemu supports all those weird old machine types and HW emulations. The desire is to support these old devices so that old VMs can work unchanged.

Quoted 6 lines
Quoted 1 line

Jason

And you are saying all these very old VMs use such a large number of legacy devices that over-counting of locked memory due to vdpa not correctly using iommufd is a problem that urgently needs to be solved otherwise the solution has no value?

No one has said that.

iommufd is gaining alot more functions than just pinned memory accounting.

Quoted 4 lines

Another question I'm interested in is whether there's actually a performance benefit to using this as compared to just software vhost. I note there's a VM exit on each IO access, so ... perhaps? Would be nice to see some numbers.

At least a single trap compared with an entire per-packet SW flow undoubtably uses alot less CPU power in the hypervisor.

Jason

#128 lore raw

Re: [PATCH vfio 11/11] vfio/virtio: Introduce a vfio driver over virtio devices

Quoted 28 lines · Jason Gunthorpe
Quoted 20 lines · Michael S. Tsirkin
Quoted 19 lines · Jason Gunthorpe
Quoted 8 lines · Alex Williamson
Quoted 3 lines

With that code in place a legacy driver in the guest has the look and feel as if having a transitional device with legacy support for both its control and data path flows.

Why do we need to enable a "legacy" driver in the guest? The very name suggests there's an alternative driver that perhaps doesn't require this I/O BAR. Why don't we just require the non-legacy driver in the guest rather than increase our maintenance burden? Thanks,

It was my reaction also.

Apparently there is a big deployed base of people using old guest VMs with old drivers and they do not want to update their VMs. It is the same basic reason why qemu supports all those weird old machine types and HW emulations. The desire is to support these old devices so that old VMs can work unchanged.

Quoted 6 lines
Quoted 1 line

Jason

And you are saying all these very old VMs use such a large number of legacy devices that over-counting of locked memory due to vdpa not correctly using iommufd is a problem that urgently needs to be solved otherwise the solution has no value?

Quoted 4 lines

No one has said that.

iommufd is gaining alot more functions than just pinned memory accounting.

Yea it's very useful - it's also useful for vdpa whether this patchset goes in or not. At some level, if vdpa can't keep up then maybe going the vfio route is justified. I'm not sure why didn't anyone fix iommufd yet - looks like a small amount of work. I'll see if I can address it quickly because we already have virtio accelerators under vdpa and it seems confusing to people to use vdpa for some and vfio for others, with overlapping but slightly incompatible functionality. I'll get back next week, in either case. I am however genuinely curious whether all the new functionality is actually useful for these legacy guests.

Quoted 9 lines
Quoted 4 lines

Another question I'm interested in is whether there's actually a performance benefit to using this as compared to just software vhost. I note there's a VM exit on each IO access, so ... perhaps? Would be nice to see some numbers.

At least a single trap compared with an entire per-packet SW flow undoubtably uses alot less CPU power in the hypervisor.

Jason

Something like the shadow vq thing will be more or less equivalent then? That's upstream in qemu and needs no hardware support. Worth comparing against. Anyway, there's presumably actual hardware this was tested with, so why guess? Just test and post numbers.

Signature · 6 lines
MST

_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization
#129 lore raw

Re: [PATCH vfio 11/11] vfio/virtio: Introduce a vfio driver over virtio devices

Quoted 9 lines · Michael S. Tsirkin

Yea it's very useful - it's also useful for vdpa whether this patchset goes in or not. At some level, if vdpa can't keep up then maybe going the vfio route is justified. I'm not sure why didn't anyone fix iommufd yet - looks like a small amount of work. I'll see if I can address it quickly because we already have virtio accelerators under vdpa and it seems confusing to people to use vdpa for some and vfio for others, with overlapping but slightly incompatible functionality. I'll get back next week, in either case. I am however genuinely curious whether all the new functionality is actually useful for these legacy guests.

It doesn't have much to do with the guests - this is new hypervisor functionality to make the hypervisor do more things. This stuff can still work with old VMs.

Quoted 10 lines
Quoted 7 lines
Quoted 4 lines

Another question I'm interested in is whether there's actually a performance benefit to using this as compared to just software vhost. I note there's a VM exit on each IO access, so ... perhaps? Would be nice to see some numbers.

At least a single trap compared with an entire per-packet SW flow undoubtably uses alot less CPU power in the hypervisor.

Something like the shadow vq thing will be more or less equivalent then?

Huh? It still has the entire netdev stack to go through on every packet before it reaches the real virtio device.

Quoted 3 lines

That's upstream in qemu and needs no hardware support. Worth comparing against. Anyway, there's presumably actual hardware this was tested with, so why guess? Just test and post numbers.

Our prior benchmarking put our VPDA/VFIO solutions at something like 2x-3x improvement over the qemu SW path it replaces.

Parav said 10% is lost, so 10% of 3x is still 3x better :)

I thought we all agreed on this when vdpa was created in the first place, the all SW path was hopeless to get high performance out of?

Jason

#130 lore raw

Re: [PATCH vfio 11/11] vfio/virtio: Introduce a vfio driver over virtio devices

Quoted 20 lines · Jason Gunthorpe
Quoted 9 lines · Michael S. Tsirkin

Yea it's very useful - it's also useful for vdpa whether this patchset goes in or not. At some level, if vdpa can't keep up then maybe going the vfio route is justified. I'm not sure why didn't anyone fix iommufd yet - looks like a small amount of work. I'll see if I can address it quickly because we already have virtio accelerators under vdpa and it seems confusing to people to use vdpa for some and vfio for others, with overlapping but slightly incompatible functionality. I'll get back next week, in either case. I am however genuinely curious whether all the new functionality is actually useful for these legacy guests.

It doesn't have much to do with the guests - this is new hypervisor functionality to make the hypervisor do more things. This stuff can still work with old VMs.

Quoted 5 lines
Quoted 5 lines
Quoted 4 lines

Another question I'm interested in is whether there's actually a performance benefit to using this as compared to just software vhost. I note there's a VM exit on each IO access, so ... perhaps? Would be nice to see some numbers.

Quoted 8 lines
Quoted 5 lines
Quoted 2 lines

At least a single trap compared with an entire per-packet SW flow undoubtably uses alot less CPU power in the hypervisor.

Something like the shadow vq thing will be more or less equivalent then?

Huh? It still has the entire netdev stack to go through on every packet before it reaches the real virtio device.

No - shadow vq just tweaks the descriptor and forwards it to the modern vdpa hardware. No net stack involved.

Quoted 4 lines
Quoted 3 lines

That's upstream in qemu and needs no hardware support. Worth comparing against. Anyway, there's presumably actual hardware this was tested with, so why guess? Just test and post numbers.

Quoted 8 lines

Our prior benchmarking put our VPDA/VFIO solutions at something like 2x-3x improvement over the qemu SW path it replaces. Parav said 10% is lost, so 10% of 3x is still 3x better :)

I thought we all agreed on this when vdpa was created in the first place, the all SW path was hopeless to get high performance out of?

Jason

That's not what I'm asking about though - not what shadow vq does, shadow vq is a vdpa feature.

_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization

#131 lore raw

Re: [PATCH vfio 11/11] vfio/virtio: Introduce a vfio driver over virtio devices

Quoted 2 lines · Michael S. Tsirkin

That's not what I'm asking about though - not what shadow vq does, shadow vq is a vdpa feature.

That's just VDPA then. We already talked about why VDPA is not a replacement for VFIO.

I agree you can probably get decent performance out of choosing VDPA over VFIO. That doesn't justify VDPA as a replacement for VFIO.

Jason

#132 lore raw

Re: [PATCH vfio 11/11] vfio/virtio: Introduce a vfio driver over virtio devices

Quoted 7 lines · Jason Gunthorpe
Quoted 2 lines · Michael S. Tsirkin

That's not what I'm asking about though - not what shadow vq does, shadow vq is a vdpa feature.

That's just VDPA then. We already talked about why VDPA is not a replacement for VFIO.

It does however work universally, by software, without any special hardware support. Which is kind of why I am curious - if VDPA needs this proxy code because shadow vq is slower then that's an argument for not having it in two places, and trying to improve vdpa to use iommufd if that's easy/practical. If instead VDPA gives the same speed with just shadow vq then keeping this hack in vfio seems like less of a problem. Finally if VDPA is faster then maybe you will reconsider using it ;)

Signature · 6 lines
MST

_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization
#133 lore raw

Re: [PATCH vfio 11/11] vfio/virtio: Introduce a vfio driver over virtio devices

Quoted 3 lines · Michael S. Tsirkin

that's easy/practical. If instead VDPA gives the same speed with just shadow vq then keeping this hack in vfio seems like less of a problem. Finally if VDPA is faster then maybe you will reconsider using it ;)

It is not all about the speed.

VDPA presents another large and complex software stack in the hypervisor that can be eliminated by simply using VFIO. VFIO is already required for other scenarios.

This is about reducing complexity, reducing attack surface and increasing maintainability of the hypervisor environment.

Jason

#134 lore raw

Re: [PATCH vfio 11/11] vfio/virtio: Introduce a vfio driver over virtio devices

Quoted 10 lines · Jason Gunthorpe
Quoted 3 lines · Michael S. Tsirkin

that's easy/practical. If instead VDPA gives the same speed with just shadow vq then keeping this hack in vfio seems like less of a problem. Finally if VDPA is faster then maybe you will reconsider using it ;)

It is not all about the speed.

VDPA presents another large and complex software stack in the hypervisor that can be eliminated by simply using VFIO.

If all you want is passing through your card to guest then yes this can be addressed "by simply using VFIO".

And let me give you a simple example just from this patchset:
it assumes guest uses MSIX and just breaks if it doesn't. As VDPA emulates it can emulate INT#x for guest while doing MSI on the host side. Yea modern guests use MSIX but this is about legacy yes?

Quoted 2 lines

VFIO is
already required for other scenarios.

Required ... by some people? Most VMs I run don't use anything outside of virtio.

Quoted 4 lines

This is about reducing complexity, reducing attack surface and increasing maintainability of the hypervisor environment.

Jason

Generally you get better security if you don't let guests poke at hardware when they don't have to. But sure, matter of preference - use VFIO, it's great. I am worried about the specific patchset though. It seems to deal with emulating virtio which seems more like a vdpa thing. If you start adding virtio emulation to vfio then won't you just end up with another vdpa? And if no why not?
And I don't buy the "we already invested in this vfio based solution", sorry - that's not a reason upstream has to maintain it.

Signature · 6 lines
MST

_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization
#135 lore raw

Re: [PATCH vfio 11/11] vfio/virtio: Introduce a vfio driver over virtio devices

Quoted 7 lines · Michael S. Tsirkin
Quoted 6 lines · Jason Gunthorpe
Quoted 3 lines · Michael S. Tsirkin

that's easy/practical. If instead VDPA gives the same speed with just shadow vq then keeping this hack in vfio seems like less of a problem. Finally if VDPA is faster then maybe you will reconsider using it ;)

Quoted 7 lines
Quoted 4 lines

It is not all about the speed.

VDPA presents another large and complex software stack in the hypervisor that can be eliminated by simply using VFIO.

If all you want is passing through your card to guest then yes this can be addressed "by simply using VFIO".

That is pretty much the goal, yes.

Quoted 2 lines

And let me give you a simple example just from this patchset:
it assumes guest uses MSIX and just breaks if it doesn't.

It does? Really? Where did you see that?

Quoted 5 lines
Quoted 2 lines

VFIO is
already required for other scenarios.

Required ... by some people? Most VMs I run don't use anything outside of virtio.

Yes, some people. The sorts of people who run large data centers.

Quoted 2 lines

It seems to deal with emulating virtio which seems more like a vdpa thing.

Alex described it right, it creates an SW trapped IO bar that relays the doorbell to an admin queue command.

Quoted 4 lines

If you start adding virtio emulation to vfio then won't
you just end up with another vdpa? And if no why not?
And I don't buy the "we already invested in this vfio based solution", sorry - that's not a reason upstream has to maintain it.

I think you would be well justified to object to actual mediation, like processing queues in VFIO or otherwise complex things.

Fortunately there is no need to do that with DPU HW. The legacy IO BAR is a weird quirk that just cannot be done without a software trap, and the OASIS standardization effort was for exactly this kind of simplistic transformation.

I also don't buy the "upstream has to maintain it" line. The team that submitted it will maintain it just fine, thank you.

Jason

#136 lore raw

Re: [PATCH vfio 11/11] vfio/virtio: Introduce a vfio driver over virtio devices

Quoted 16 lines · Jason Gunthorpe
Quoted 7 lines · Michael S. Tsirkin
Quoted 6 lines · Jason Gunthorpe
Quoted 3 lines · Michael S. Tsirkin

that's easy/practical. If instead VDPA gives the same speed with just shadow vq then keeping this hack in vfio seems like less of a problem. Finally if VDPA is faster then maybe you will reconsider using it ;)

Quoted 7 lines
Quoted 4 lines

It is not all about the speed.

VDPA presents another large and complex software stack in the hypervisor that can be eliminated by simply using VFIO.

If all you want is passing through your card to guest then yes this can be addressed "by simply using VFIO".

Quoted 6 lines

That is pretty much the goal, yes.

Quoted 2 lines

And let me give you a simple example just from this patchset:
it assumes guest uses MSIX and just breaks if it doesn't.

It does? Really? Where did you see that?

This thing apparently:

+ opcode = (pos < VIRTIO_PCI_CONFIG_OFF(true)) ?
+ VIRTIO_ADMIN_CMD_LEGACY_COMMON_CFG_READ :
+ VIRTIO_ADMIN_CMD_LEGACY_DEV_CFG_READ;

That "true" is supposed to be whether guest enabled MSI or not.

Quoted 14 lines
Quoted 5 lines
Quoted 2 lines

VFIO is
already required for other scenarios.

Required ... by some people? Most VMs I run don't use anything outside of virtio.

Yes, some people. The sorts of people who run large data centers.

Quoted 2 lines

It seems to deal with emulating virtio which seems more like a vdpa thing.

Alex described it right, it creates an SW trapped IO bar that relays the doorbell to an admin queue command.

Quoted 7 lines
Quoted 4 lines

If you start adding virtio emulation to vfio then won't
you just end up with another vdpa? And if no why not?
And I don't buy the "we already invested in this vfio based solution", sorry - that's not a reason upstream has to maintain it.

I think you would be well justified to object to actual mediation, like processing queues in VFIO or otherwise complex things.

This mediation is kind of smallish, I agree. Not completely devoid of logic though.

Quoted 7 lines

Fortunately there is no need to do that with DPU HW. The legacy IO BAR is a weird quirk that just cannot be done without a software trap, and the OASIS standardization effort was for exactly this kind of simplistic transformation.

I also don't buy the "upstream has to maintain it" line. The team that submitted it will maintain it just fine, thank you.

it will require maintainance effort when virtio changes are made. For example it pokes at the device state - I don't see specific races right now but in the past we did e.g. reset the device to recover from errors and we might start doing it again.

If more of the logic is under virtio directory where we'll remember to keep it in loop, and will be able to reuse it from vdpa down the road, I would be more sympathetic.

Signature · 6 lines
MST

_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization
#137 lore raw

Re: [PATCH vfio 11/11] vfio/virtio: Introduce a vfio driver over virtio devices

Quoted 8 lines · Michael S. Tsirkin

it will require maintainance effort when virtio changes are made. For example it pokes at the device state - I don't see specific races right now but in the past we did e.g. reset the device to recover from errors and we might start doing it again.

If more of the logic is under virtio directory where we'll remember to keep it in loop, and will be able to reuse it from vdpa down the road, I would be more sympathetic.

This is inevitable, the VFIO live migration driver will need all this infrastructure too.

Jason

#138 lore raw

Re: [PATCH vfio 11/11] vfio/virtio: Introduce a vfio driver over virtio devices

Quoted 7 lines · Jason Gunthorpe
Quoted 5 lines · Michael S. Tsirkin

it will require maintainance effort when virtio changes are made. For example it pokes at the device state - I don't see specific races right now but in the past we did e.g. reset the device to recover from errors and we might start doing it again.

Quoted 9 lines
Quoted 3 lines

If more of the logic is under virtio directory where we'll remember to keep it in loop, and will be able to reuse it from vdpa down the road, I would be more sympathetic.

This is inevitable, the VFIO live migration driver will need all this infrastructure too.

Jason

I am not sure what you are saying and what is inevitable. VDPA for sure will want live migration support. I am not at all sympathetic to efforts that want to duplicate that support for virtio under VFIO. Put it in a library under the virtio directory, with a sane will documented interface.
I don't maintain VFIO and Alex can merge what he wants, but I won't merge patches that export virtio internals in a way that will make virtio maintainance harder.

Signature · 6 lines
MST

_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization
#139 lore raw

Re: [PATCH vfio 11/11] vfio/virtio: Introduce a vfio driver over virtio devices

In reply to Michael S. Tsirkin
Quoted 8 lines · Michael S. Tsirkin
Quoted 6 lines · Jason Gunthorpe
Quoted 3 lines · Michael S. Tsirkin

that's easy/practical. If instead VDPA gives the same speed with just shadow vq then keeping this hack in vfio seems like less of a problem. Finally if VDPA is faster then maybe you will reconsider using it ;)

Quoted 7 lines
Quoted 4 lines

It is not all about the speed.

VDPA presents another large and complex software stack in the hypervisor that can be eliminated by simply using VFIO.

If all you want is passing through your card to guest then yes this can be addressed "by simply using VFIO".

+1.

And what's more, using MMIO BAR0 then it can work for legacy.

I have a handy virtio hardware from one vendor that works like this, and I see it is done by a lot of other vendors.

Thanks

_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization

#140 lore raw

Re: [PATCH vfio 11/11] vfio/virtio: Introduce a vfio driver over virtio devices

Quoted 1 line · Jason Wang

And what's more, using MMIO BAR0 then it can work for legacy.

Oh? How? Our team didn't think so.

Jason

#141 lore raw

RE: [PATCH vfio 11/11] vfio/virtio: Introduce a vfio driver over virtio devices

Quoted 8 lines

From: Jason Gunthorpe <[email protected]>
Sent: Friday, September 22, 2023 5:53 PM

Quoted 1 line

And what's more, using MMIO BAR0 then it can work for legacy.

Oh? How? Our team didn't think so.

It does not. It was already discussed.
The device reset in legacy is not synchronous.
The drivers do not wait for reset to complete; it was written for the sw backend. Hence MMIO BAR0 is not the best option in real implementations.

#142 lore raw

Re: [PATCH vfio 11/11] vfio/virtio: Introduce a vfio driver over virtio devices

Quoted 5 lines · Parav Pandit
Quoted 3 lines

From: Jason Gunthorpe <[email protected]>
Sent: Friday, September 22, 2023 5:53 PM

Quoted 8 lines
Quoted 3 lines
Quoted 1 line

And what's more, using MMIO BAR0 then it can work for legacy.

Oh? How? Our team didn't think so.

It does not. It was already discussed.
The device reset in legacy is not synchronous.
The drivers do not wait for reset to complete; it was written for the sw backend. Hence MMIO BAR0 is not the best option in real implementations.

Or maybe they made it synchronous in hardware, that's all.
After all same is true for the IO BAR0 e.g. for the PF: IO writes are posted anyway.

Whether that's possible would depend on the hardware architecture.

Signature · 6 lines
MST

_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization
#143 lore raw

Re: [PATCH vfio 11/11] vfio/virtio: Introduce a vfio driver over virtio devices

Quoted 10 lines · Michael S. Tsirkin
Quoted 9 lines · Parav Pandit
Quoted 8 lines

From: Jason Gunthorpe <[email protected]>
Sent: Friday, September 22, 2023 5:53 PM

Quoted 1 line

And what's more, using MMIO BAR0 then it can work for legacy.

Oh? How? Our team didn't think so.

Quoted 8 lines
Quoted 4 lines

It does not. It was already discussed.
The device reset in legacy is not synchronous.
The drivers do not wait for reset to complete; it was written for the sw backend. Hence MMIO BAR0 is not the best option in real implementations.

Or maybe they made it synchronous in hardware, that's all. After all same is true for the IO BAR0 e.g. for the PF: IO writes are posted anyway.

IO writes are not posted in PCI.

Jason

#144 lore raw

Re: [PATCH vfio 11/11] vfio/virtio: Introduce a vfio driver over virtio devices

Quoted 16 lines · Jason Gunthorpe
Quoted 15 lines · Michael S. Tsirkin
Quoted 13 lines · Parav Pandit
Quoted 8 lines

From: Jason Gunthorpe <[email protected]>
Sent: Friday, September 22, 2023 5:53 PM

Quoted 1 line

And what's more, using MMIO BAR0 then it can work for legacy.

Oh? How? Our team didn't think so.

It does not. It was already discussed.
The device reset in legacy is not synchronous.
The drivers do not wait for reset to complete; it was written for the sw backend. Hence MMIO BAR0 is not the best option in real implementations.

Quoted 5 lines
Quoted 3 lines

Or maybe they made it synchronous in hardware, that's all. After all same is true for the IO BAR0 e.g. for the PF: IO writes are posted anyway.

IO writes are not posted in PCI.

Aha, I was confused. Thanks for the correction. I guess you just buffer subsequent transactions while reset is going on and reset quickly enough for it to be seemless then?

Signature · 6 lines
MST

_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization
#145 lore raw

Re: [PATCH vfio 11/11] vfio/virtio: Introduce a vfio driver over virtio devices

Quoted 21 lines · Michael S. Tsirkin
Quoted 20 lines · Jason Gunthorpe
Quoted 18 lines · Michael S. Tsirkin
Quoted 5 lines · Parav Pandit
Quoted 3 lines

From: Jason Gunthorpe <[email protected]>
Sent: Friday, September 22, 2023 5:53 PM

Quoted 8 lines
Quoted 3 lines
Quoted 1 line

And what's more, using MMIO BAR0 then it can work for legacy.

Oh? How? Our team didn't think so.

It does not. It was already discussed.
The device reset in legacy is not synchronous.
The drivers do not wait for reset to complete; it was written for the sw backend. Hence MMIO BAR0 is not the best option in real implementations.

Or maybe they made it synchronous in hardware, that's all. After all same is true for the IO BAR0 e.g. for the PF: IO writes are posted anyway.

Quoted 5 lines
Quoted 1 line

IO writes are not posted in PCI.

Aha, I was confused. Thanks for the correction. I guess you just buffer subsequent transactions while reset is going on and reset quickly enough for it to be seemless then?

From a hardware perspective the CPU issues an non-posted IO write and then it stops processing until the far side returns an IO completion.

Using that you can emulate what the SW virtio model did and delay the CPU from restarting until the reset is completed.

Since MMIO is always posted, this is not possible to emulate directly using MMIO.

Converting IO into non-posted admin commands is a fairly close recreation to what actual HW would do.

Jason

#146 lore raw

Re: [PATCH vfio 11/11] vfio/virtio: Introduce a vfio driver over virtio devices

Quoted 34 lines · Jason Gunthorpe
Quoted 21 lines · Michael S. Tsirkin
Quoted 20 lines · Jason Gunthorpe
Quoted 18 lines · Michael S. Tsirkin
Quoted 5 lines · Parav Pandit
Quoted 3 lines

From: Jason Gunthorpe <[email protected]>
Sent: Friday, September 22, 2023 5:53 PM

Quoted 8 lines
Quoted 3 lines
Quoted 1 line

And what's more, using MMIO BAR0 then it can work for legacy.

Oh? How? Our team didn't think so.

It does not. It was already discussed.
The device reset in legacy is not synchronous.
The drivers do not wait for reset to complete; it was written for the sw backend. Hence MMIO BAR0 is not the best option in real implementations.

Or maybe they made it synchronous in hardware, that's all. After all same is true for the IO BAR0 e.g. for the PF: IO writes are posted anyway.

Quoted 7 lines
Quoted 1 line

IO writes are not posted in PCI.

Aha, I was confused. Thanks for the correction. I guess you just buffer subsequent transactions while reset is going on and reset quickly enough for it to be seemless then?

From a hardware perspective the CPU issues an non-posted IO write and

then it stops processing until the far side returns an IO completion.

Using that you can emulate what the SW virtio model did and delay the CPU from restarting until the reset is completed.

Quoted 7 lines

Since MMIO is always posted, this is not possible to emulate directly using MMIO.

Converting IO into non-posted admin commands is a fairly close recreation to what actual HW would do.

Jason

I thought you asked how it is possible for hardware to support reset if all it does is replace IO BAR with memory BAR. The answer is that since 2011 the reset is followed by read of the status field (which isn't much older than MSIX support from 2009 - which this code assumes). If one uses a Linux driver from 2011 and on then all you need to do is defer response to this read until after the reset is complete.

If you are using older drivers or other OSes then reset using a posted write after device has operated for a while might not be safe, so e.g. you might trigger races if you remove drivers from system or trigger hot unplug. For example:

	static void virtio_pci_remove(struct pci_dev *pci_dev)
	{

	....

		unregister_virtio_device(&vp_dev->vdev);

	^^^^ triggers reset, then releases memory

	....

		pci_disable_device(pci_dev);

	^^^ blocks DMA by clearing bus master

	}

here you could see some DMA into memory that has just been released.

As Jason mentions hardware exists that is used under one of these two restrictions on the guest (Linux since 2011 or no resets while DMA is going on), and it works fine with these existing guests.

Given the restrictions, virtio TC didn't elect to standardize this approach and instead opted for the heavier approach of converting IO into non-posted admin commands in software.

Signature · 6 lines
MST

_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization
#147 lore raw

Re: [PATCH vfio 11/11] vfio/virtio: Introduce a vfio driver over virtio devices

In reply to Parav Pandit
Quoted 6 lines · Parav Pandit
Quoted 4 lines

From: Jason Gunthorpe <[email protected]>
Sent: Friday, September 22, 2023 5:53 PM

Quoted 6 lines
Quoted 3 lines
Quoted 1 line

And what's more, using MMIO BAR0 then it can work for legacy.

Oh? How? Our team didn't think so.

It does not. It was already discussed.
The device reset in legacy is not synchronous.

How do you know this?

Quoted 1 line

The drivers do not wait for reset to complete; it was written for the sw backend.

Do you see there's a flush after reset in the legacy driver?

static void vp_reset(struct virtio_device *vdev)

{
        struct virtio_pci_device *vp_dev = to_vp_device(vdev);
        /* 0 status means a reset. */
        vp_legacy_set_status(&vp_dev->ldev, 0);
        /* Flush out the status write, and flush in device writes,
         * including MSi-X interrupts, if any. */
        vp_legacy_get_status(&vp_dev->ldev);
        /* Flush pending VQ/configuration callbacks. */
        vp_synchronize_vectors(vdev);
}

Thanks

Quoted 2 lines

Hence MMIO BAR0 is not the best option in real implementations.

_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization

#148 lore raw

RE: [PATCH vfio 11/11] vfio/virtio: Introduce a vfio driver over virtio devices

Quoted 12 lines

From: Jason Wang <[email protected]>
Sent: Monday, September 25, 2023 8:00 AM

Quoted 6 lines · Parav Pandit
Quoted 4 lines

From: Jason Gunthorpe <[email protected]>
Sent: Friday, September 22, 2023 5:53 PM

Quoted 9 lines
Quoted 6 lines
Quoted 3 lines
Quoted 1 line

And what's more, using MMIO BAR0 then it can work for legacy.

Oh? How? Our team didn't think so.

It does not. It was already discussed.
The device reset in legacy is not synchronous.

How do you know this?

Not sure the motivation of same discussion done in the OASIS with you and others in past.

Anyways, please find the answer below.

About reset,
The legacy device specification has not enforced below cited 1.0 driver requirement of 1.0.

"The driver SHOULD consider a driver-initiated reset complete when it reads device status as 0."

[1] https://ozlabs.org/~rusty/virtio-spec/virtio-0.9.5.pdf

Quoted 5 lines
Quoted 1 line

The drivers do not wait for reset to complete; it was written for the sw

backend.

Do you see there's a flush after reset in the legacy driver?

Yes. it only flushes the write by reading it. The driver does not get _wait_ for the reset to complete within the device like above.

Please see the reset flow of 1.x device as below.
In fact the comment of the 1.x device also needs to be updated to indicate that driver need to wait for the device to finish the reset. I will send separate patch for improving this comment of vp_reset() to match the spec.

static void vp_reset(struct virtio_device *vdev)

{
        struct virtio_pci_device *vp_dev = to_vp_device(vdev);
        struct virtio_pci_modern_device *mdev = &vp_dev->mdev;

        /* 0 status means a reset. */
        vp_modern_set_status(mdev, 0);
        /* After writing 0 to device_status, the driver MUST wait for a read of
         * device_status to return 0 before reinitializing the device.
         * This will flush out the status write, and flush in device writes,
         * including MSI-X interrupts, if any.
         */
        while (vp_modern_get_status(mdev))
                msleep(1);
        /* Flush pending VQ/configuration callbacks. */
        vp_synchronize_vectors(vdev);
}
Quoted 17 lines
static void vp_reset(struct virtio_device *vdev) {
        struct virtio_pci_device *vp_dev = to_vp_device(vdev);
        /* 0 status means a reset. */
        vp_legacy_set_status(&vp_dev->ldev, 0);
        /* Flush out the status write, and flush in device writes,
         * including MSi-X interrupts, if any. */
        vp_legacy_get_status(&vp_dev->ldev);
        /* Flush pending VQ/configuration callbacks. */
        vp_synchronize_vectors(vdev);
}

Thanks

Quoted 2 lines

Hence MMIO BAR0 is not the best option in real implementations.

#149 lore raw

Re: [PATCH vfio 11/11] vfio/virtio: Introduce a vfio driver over virtio devices

Quoted 23 lines · Parav Pandit
Quoted 12 lines

From: Jason Wang <[email protected]>
Sent: Monday, September 25, 2023 8:00 AM

Quoted 6 lines · Parav Pandit
Quoted 4 lines

From: Jason Gunthorpe <[email protected]>
Sent: Friday, September 22, 2023 5:53 PM

Quoted 9 lines
Quoted 6 lines
Quoted 3 lines
Quoted 1 line

And what's more, using MMIO BAR0 then it can work for legacy.

Oh? How? Our team didn't think so.

It does not. It was already discussed.
The device reset in legacy is not synchronous.

How do you know this?

Not sure the motivation of same discussion done in the OASIS with you and others in past.

Quoted 8 lines

Anyways, please find the answer below.

About reset,
The legacy device specification has not enforced below cited 1.0 driver requirement of 1.0.

"The driver SHOULD consider a driver-initiated reset complete when it reads device status as 0."

[1] https://ozlabs.org/~rusty/virtio-spec/virtio-0.9.5.pdf

Basically, I think any drivers that did not read status (linux pre 2011) before freeing memory under DMA have a reset path that is racy wrt DMA, since memory writes are posted and IO writes while not posted have completion that does not order posted transactions, e.g. from pci express spec:
D2b
An I/O or Configuration Write Completion 37 is permitted to pass a Posted Request.
having said that there were a ton of driver races discovered on this
path in the years since, I suspect if one cares about this then
just avoiding stress on reset is wise.

Quoted 6 lines
Quoted 5 lines
Quoted 1 line

The drivers do not wait for reset to complete; it was written for the sw

backend.

Do you see there's a flush after reset in the legacy driver?

Yes. it only flushes the write by reading it. The driver does not get _wait_ for the reset to complete within the device like above.

One can thinkably do that wait in hardware, though. Just defer completion until read is done.

Quoted 5 lines

Please see the reset flow of 1.x device as below.
In fact the comment of the 1.x device also needs to be updated to indicate that driver need to wait for the device to finish the reset. I will send separate patch for improving this comment of vp_reset() to match the spec.

static void vp_reset(struct virtio_device *vdev)

Quoted 36 lines
{
        struct virtio_pci_device *vp_dev = to_vp_device(vdev);
        struct virtio_pci_modern_device *mdev = &vp_dev->mdev;

        /* 0 status means a reset. */
        vp_modern_set_status(mdev, 0);
        /* After writing 0 to device_status, the driver MUST wait for a read of
         * device_status to return 0 before reinitializing the device.
         * This will flush out the status write, and flush in device writes,
         * including MSI-X interrupts, if any.
         */
        while (vp_modern_get_status(mdev))
                msleep(1);
        /* Flush pending VQ/configuration callbacks. */
        vp_synchronize_vectors(vdev);
}
Quoted 17 lines
static void vp_reset(struct virtio_device *vdev) {
        struct virtio_pci_device *vp_dev = to_vp_device(vdev);
        /* 0 status means a reset. */
        vp_legacy_set_status(&vp_dev->ldev, 0);
        /* Flush out the status write, and flush in device writes,
         * including MSi-X interrupts, if any. */
        vp_legacy_get_status(&vp_dev->ldev);
        /* Flush pending VQ/configuration callbacks. */
        vp_synchronize_vectors(vdev);
}

Thanks

Quoted 2 lines

Hence MMIO BAR0 is not the best option in real implementations.

_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization

#150 lore raw

Re: [PATCH vfio 11/11] vfio/virtio: Introduce a vfio driver over virtio devices

Quoted 38 lines · Michael S. Tsirkin
Quoted 17 lines · Parav Pandit
Quoted 6 lines

From: Jason Wang <[email protected]>
Sent: Monday, September 25, 2023 8:00 AM

Quoted 1 line · Parav Pandit
Quoted 9 lines
Quoted 7 lines
Quoted 5 lines

From: Jason Gunthorpe <[email protected]>
Sent: Friday, September 22, 2023 5:53 PM

Quoted 1 line

And what's more, using MMIO BAR0 then it can work for legacy.

Oh? How? Our team didn't think so.

It does not. It was already discussed.
The device reset in legacy is not synchronous.

How do you know this?

Not sure the motivation of same discussion done in the OASIS with you and others in past.

Quoted 8 lines

Anyways, please find the answer below.

About reset,
The legacy device specification has not enforced below cited 1.0 driver requirement of 1.0.

"The driver SHOULD consider a driver-initiated reset complete when it reads device status as 0."

[1] https://ozlabs.org/~rusty/virtio-spec/virtio-0.9.5.pdf

Basically, I think any drivers that did not read status (linux pre 2011) before freeing memory under DMA have a reset path that is racy wrt DMA, since memory writes are posted and IO writes while not posted have completion
that does not order posted transactions, e.g. from pci express spec:
D2b
An I/O or Configuration Write Completion 37 is permitted to pass a Posted Request.
having said that there were a ton of driver races discovered on this
path in the years since, I suspect if one cares about this then
just avoiding stress on reset is wise.

Quoted 8 lines
Quoted 6 lines
Quoted 5 lines
Quoted 1 line

The drivers do not wait for reset to complete; it was written for the sw

backend.

Do you see there's a flush after reset in the legacy driver?

Yes. it only flushes the write by reading it. The driver does not get _wait_ for the reset to complete within the device like above.

One can thinkably do that wait in hardware, though. Just defer completion until read is done.

I agree with MST. At least Intel devices work fine with vfio-pci and legacy driver without any changes. So far so good.

Thanks
Zhu Lingshan

Quoted 45 lines
Quoted 6 lines

Please see the reset flow of 1.x device as below.
In fact the comment of the 1.x device also needs to be updated to indicate that driver need to wait for the device to finish the reset. I will send separate patch for improving this comment of vp_reset() to match the spec.

static void vp_reset(struct virtio_device *vdev)

Quoted 35 lines
{
         struct virtio_pci_device *vp_dev = to_vp_device(vdev);
         struct virtio_pci_modern_device *mdev = &vp_dev->mdev;

         /* 0 status means a reset. */
         vp_modern_set_status(mdev, 0);
         /* After writing 0 to device_status, the driver MUST wait for a read of
          * device_status to return 0 before reinitializing the device.
          * This will flush out the status write, and flush in device writes,
          * including MSI-X interrupts, if any.
          */
         while (vp_modern_get_status(mdev))
                 msleep(1);
         /* Flush pending VQ/configuration callbacks. */
         vp_synchronize_vectors(vdev);
}
Quoted 17 lines
static void vp_reset(struct virtio_device *vdev) {
         struct virtio_pci_device *vp_dev = to_vp_device(vdev);
         /* 0 status means a reset. */
         vp_legacy_set_status(&vp_dev->ldev, 0);
         /* Flush out the status write, and flush in device writes,
          * including MSi-X interrupts, if any. */
         vp_legacy_get_status(&vp_dev->ldev);
         /* Flush pending VQ/configuration callbacks. */
         vp_synchronize_vectors(vdev);
}

Thanks

Quoted 2 lines

Hence MMIO BAR0 is not the best option in real implementations.

_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization

#151 lore raw

RE: [PATCH vfio 11/11] vfio/virtio: Introduce a vfio driver over virtio devices

In reply to Michael S. Tsirkin
Quoted 8 lines

From: Michael S. Tsirkin <[email protected]>
Sent: Tuesday, September 26, 2023 12:06 AM

One can thinkably do that wait in hardware, though. Just defer completion until read is done.

Once OASIS does such new interface and if some hw vendor _actually_ wants to do such complex hw, may be vfio driver can adopt to it. When we worked with you, we discussed that there such hw does not have enough returns and hence technical committee choose to proceed with admin commands. I will skip re-discussing all over it again here.

The current virto spec is delivering the best trade-offs of functionality, performance and light weight implementation with future forward path towards more features as Jason explained such as migration. All with near zero driver, qemu and sw involvement for rapidly growing feature set...
_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization

#152 lore raw

Re: [PATCH vfio 11/11] vfio/virtio: Introduce a vfio driver over virtio devices

Quoted 10 lines · Parav Pandit
Quoted 9 lines

From: Michael S. Tsirkin <[email protected]>
Sent: Tuesday, September 26, 2023 12:06 AM

One can thinkably do that wait in hardware, though. Just defer completion until read is done.

Once OASIS does such new interface and if some hw vendor _actually_ wants to do such complex hw, may be vfio driver can adopt to it.

It is you that tries to revive legacy in the spec. We all know legacy is tricky but work.

Quoted 1 line

When we worked with you, we discussed that there such hw does not have enough returns and hence technical committee choose to proceed with admin commands.

I don't think my questions regarding the legacy transport get good answers at that time. What's more, we all know spec allows to fix, workaround or even deprecate a feature.

Thanks

_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization

#153 lore raw

Re: [PATCH vfio 11/11] vfio/virtio: Introduce a vfio driver over virtio devices

In reply to Parav Pandit
Quoted 9 lines · Parav Pandit
Quoted 8 lines

From: Michael S. Tsirkin <[email protected]>
Sent: Tuesday, September 26, 2023 12:06 AM

One can thinkably do that wait in hardware, though. Just defer completion until read is done.

Once OASIS does such new interface and if some hw vendor _actually_ wants to do such complex hw, may be vfio driver can adopt to it.

The reset behaviour I describe is already in the spec. What else do you want OASIS to standardize? Virtio currently is just a register map it does not yet include suggestions on how exactly do pci express transactions look. You feel we should add that?

Signature · 6 lines
MST

_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization
#154 lore raw

RE: [PATCH vfio 11/11] vfio/virtio: Introduce a vfio driver over virtio devices

Quoted 13 lines

From: Michael S. Tsirkin <[email protected]>
Sent: Thursday, October 12, 2023 4:23 PM

Quoted 8 lines · Parav Pandit
Quoted 8 lines

From: Michael S. Tsirkin <[email protected]>
Sent: Tuesday, September 26, 2023 12:06 AM

One can thinkably do that wait in hardware, though. Just defer completion until read is done.

Quoted 7 lines
Quoted 1 line

Once OASIS does such new interface and if some hw vendor _actually_ wants

to do such complex hw, may be vfio driver can adopt to it.

The reset behaviour I describe is already in the spec. What else do you want OASIS to standardize? Virtio currently is just a register map it does not yet include suggestions on how exactly do pci express transactions look. You feel we should add that?

The reset behavior in the spec for modern as listed in [1] and [2] is just fine.

What I meant is in context of having MMIO based legacy registers to "defer completion until read is done". I think you meant, "Just differ read completion, until reset is done".
This means the hw needs to finish the device reset for thousands of devices within the read completion timeout of the pci. So when if OASIS does such standardization, someone can implement it.

What I recollect, is OASIS didn't not standardize such anti-scale approach and took the admin command approach which achieve better scale. Hope I clarified.

I am not expecting OASIS to do anything extra for legacy registers.

[1] The device MUST reset when 0 is written to device_status, and present a 0 in device_status once that is done.
[2] After writing 0 to device_status, the driver MUST wait for a read of device_status to return 0 before reinitializing the device.

#155 lore raw

Re: [PATCH vfio 11/11] vfio/virtio: Introduce a vfio driver over virtio devices

Quoted 21 lines · Parav Pandit
Quoted 13 lines

From: Michael S. Tsirkin <[email protected]>
Sent: Thursday, October 12, 2023 4:23 PM

Quoted 8 lines · Parav Pandit
Quoted 8 lines

From: Michael S. Tsirkin <[email protected]>
Sent: Tuesday, September 26, 2023 12:06 AM

One can thinkably do that wait in hardware, though. Just defer completion until read is done.

Quoted 7 lines
Quoted 1 line

Once OASIS does such new interface and if some hw vendor _actually_ wants

to do such complex hw, may be vfio driver can adopt to it.

The reset behaviour I describe is already in the spec. What else do you want OASIS to standardize? Virtio currently is just a register map it does not yet include suggestions on how exactly do pci express transactions look. You feel we should add that?

Quoted 4 lines

The reset behavior in the spec for modern as listed in [1] and [2] is just fine.

What I meant is in context of having MMIO based legacy registers to "defer completion until read is done". I think you meant, "Just differ read completion, until reset is done".

yes

Quoted 1 line

This means the hw needs to finish the device reset for thousands of devices within the read completion timeout of the pci.

no, each device does it's own reset.

Quoted 4 lines

So when if OASIS does such standardization, someone can implement it.

What I recollect, is OASIS didn't not standardize such anti-scale approach and took the admin command approach which achieve better scale. Hope I clarified.

You are talking about the extension for trap and emulate. I am instead talking about devices that work with existing legacy linux drivers with no traps.

Quoted 5 lines

I am not expecting OASIS to do anything extra for legacy registers.

[1] The device MUST reset when 0 is written to device_status, and present a 0 in device_status once that is done.
[2] After writing 0 to device_status, the driver MUST wait for a read of device_status to return 0 before reinitializing the device.

We can add a note explaining that legacy drivers do not wait after doing reset, that is not a problem.
If someone wants to make a device that works with existing legacy linux drivers, they can do that.
Won't work with all drivers though, which is why oasis did not want to standardize this.

Signature · 6 lines
MST

_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization
#156 lore raw

RE: [PATCH vfio 11/11] vfio/virtio: Introduce a vfio driver over virtio devices

Quoted 7 lines

From: Michael S. Tsirkin <[email protected]>
Sent: Thursday, October 12, 2023 5:00 PM

I am instead talking about devices that work with existing legacy linux drivers with no traps.

Yep, I understood.

Quoted 7 lines
Quoted 3 lines

I am not expecting OASIS to do anything extra for legacy registers.

[1] The device MUST reset when 0 is written to device_status, and present a 0

in device_status once that is done.

Quoted 2 lines

[2] After writing 0 to device_status, the driver MUST wait for a read of device_status to return 0 before reinitializing the device.

Quoted 6 lines

We can add a note explaining that legacy drivers do not wait after doing reset, that is not a problem.
If someone wants to make a device that works with existing legacy linux drivers, they can do that.
Won't work with all drivers though, which is why oasis did not want to standardize this.

Ok. thanks.

#157 lore raw

Re: [PATCH vfio 11/11] vfio/virtio: Introduce a vfio driver over virtio devices

In reply to Parav Pandit
Quoted 15 lines · Parav Pandit
Quoted 15 lines

From: Jason Wang <[email protected]>
Sent: Monday, September 25, 2023 8:00 AM

Quoted 8 lines · Parav Pandit
Quoted 8 lines

From: Jason Gunthorpe <[email protected]>
Sent: Friday, September 22, 2023 5:53 PM

Quoted 1 line

And what's more, using MMIO BAR0 then it can work for legacy.

Quoted 8 lines
Quoted 7 lines
Quoted 4 lines
Quoted 1 line

Oh? How? Our team didn't think so.

It does not. It was already discussed.
The device reset in legacy is not synchronous.

How do you know this?

Not sure the motivation of same discussion done in the OASIS with you and others in past.

That is exactly the same point.

It's too late to define the legacy behaviour accurately in the spec so people will be lost in the legacy maze easily.

Quoted 7 lines

Anyways, please find the answer below.

About reset,
The legacy device specification has not enforced below cited 1.0 driver requirement of 1.0.

"The driver SHOULD consider a driver-initiated reset complete when it reads device status as 0."

We are talking about how to make devices work for legacy drivers. So it has nothing related to 1.0.

Quoted 9 lines

[1] https://ozlabs.org/~rusty/virtio-spec/virtio-0.9.5.pdf

Quoted 5 lines
Quoted 1 line

The drivers do not wait for reset to complete; it was written for the sw

backend.

Do you see there's a flush after reset in the legacy driver?

Yes. it only flushes the write by reading it. The driver does not get _wait_ for the reset to complete within the device like above.

It's the implementation details in legacy. The device needs to make sure (reset) the driver can work (is done before get_status return).

That's all.

_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization

#158 lore raw

RE: [PATCH vfio 11/11] vfio/virtio: Introduce a vfio driver over virtio devices

Quoted 7 lines

From: Jason Wang <[email protected]>
Sent: Tuesday, September 26, 2023 8:03 AM

It's the implementation details in legacy. The device needs to make sure (reset) the driver can work (is done before get_status return).

It is part of the 0.9.5 and 1.x specification as I quoted those text above.

#159 lore raw

Re: [PATCH vfio 11/11] vfio/virtio: Introduce a vfio driver over virtio devices

Quoted 9 lines · Parav Pandit
Quoted 8 lines

From: Jason Wang <[email protected]>
Sent: Tuesday, September 26, 2023 8:03 AM

It's the implementation details in legacy. The device needs to make sure (reset) the driver can work (is done before get_status return).

It is part of the 0.9.5 and 1.x specification as I quoted those text above.

What I meant is: legacy devices need to find their way to make legacy drivers work. That's how legacy works.

It's too late to add any normative to the 0.95 spec. So the device behaviour is actually defined by the legacy drivers. That is why it is tricky.

If you can't find a way to make legacy drivers work, use modern.

That's it.

Thanks

_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization

#160 lore raw

RE: [PATCH vfio 11/11] vfio/virtio: Introduce a vfio driver over virtio devices

Quoted 9 lines

From: Jason Wang <[email protected]>
Sent: Tuesday, September 26, 2023 10:07 AM

If you can't find a way to make legacy drivers work, use modern.

Understood.
This vfio series make the legacy drivers work.
Thanks.

Quoted 3 lines

That's it.

Thanks

#161 lore raw

Re: [PATCH vfio 11/11] vfio/virtio: Introduce a vfio driver over virtio devices

In reply to Jason Wang
Quoted 2 lines · Jason Wang

It's the implementation details in legacy. The device needs to make sure (reset) the driver can work (is done before get_status return).

I think that there's no way to make it reliably work for all legacy drivers.

They just assumed a software backend and did not bother with DMA ordering. You can try to avoid resets, they are not that common so things will tend to mostly work if you don't stress them to much with things like hot plug/unplug in a loop. Or you can try to use a driver after 2011 which is more aware of hardware ordering and flushes the reset write with a read. One of these two tricks, I think, is the magic behind the device exposing memory bar 0 that you mention.

Signature · 6 lines
MST

_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization
#162 lore raw

Re: [PATCH vfio 11/11] vfio/virtio: Introduce a vfio driver over virtio devices

Quoted 6 lines · Michael S. Tsirkin
Quoted 2 lines · Jason Wang

It's the implementation details in legacy. The device needs to make sure (reset) the driver can work (is done before get_status return).

I think that there's no way to make it reliably work for all legacy drivers.

Yes, we may have ancient drivers.

Quoted 8 lines

They just assumed a software backend and did not bother with DMA ordering. You can try to avoid resets, they are not that common so things will tend to mostly work if you don't stress them to much with things like hot plug/unplug in a loop. Or you can try to use a driver after 2011 which is more aware of hardware ordering and flushes the reset write with a read. One of these two tricks, I think, is the magic behind the device exposing memory bar 0 that you mention.

Right this is what I see for hardware legacy devices shipped by some cloud vendors.

Thanks

Quoted 4 lines

--
MST

_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization

#163 lore raw

Re: [PATCH vfio 11/11] vfio/virtio: Introduce a vfio driver over virtio devices

In reply to Jason Gunthorpe
Quoted 11 lines · Jason Gunthorpe
Quoted 3 lines · Michael S. Tsirkin

that's easy/practical. If instead VDPA gives the same speed with just shadow vq then keeping this hack in vfio seems like less of a problem. Finally if VDPA is faster then maybe you will reconsider using it ;)

It is not all about the speed.

VDPA presents another large and complex software stack in the hypervisor that can be eliminated by simply using VFIO.

vDPA supports standard virtio devices so how did you define complexity?

From the view of the application, what it wants is a simple virtio device but not virtio-pci devices. That is what vDPA tries to present.

By simply counting LOCs: vdpa + vhost + vp_vdpa is much less code than what VFIO had. It's not hard to expect, it will still be much less even if iommufd is done.

Thanks

Quoted 9 lines

VFIO is
already required for other scenarios.

This is about reducing complexity, reducing attack surface and increasing maintainability of the hypervisor environment.

Jason

_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization

#164 lore raw

Re: [PATCH vfio 11/11] vfio/virtio: Introduce a vfio driver over virtio devices

Quoted 8 lines · Jason Wang
Quoted 7 lines · Jason Gunthorpe
Quoted 3 lines · Michael S. Tsirkin

that's easy/practical. If instead VDPA gives the same speed with just shadow vq then keeping this hack in vfio seems like less of a problem. Finally if VDPA is faster then maybe you will reconsider using it ;)

Quoted 7 lines
Quoted 4 lines

It is not all about the speed.

VDPA presents another large and complex software stack in the hypervisor that can be eliminated by simply using VFIO.

vDPA supports standard virtio devices so how did you define complexity?

As I said, VFIO is already required for other devices in these VMs. So anything incremental over base-line vfio-pci is complexity to minimize.

Everything vdpa does is either redundant or unnecessary compared to VFIO in these environments.

Jason

#165 lore raw

Re: [PATCH vfio 11/11] vfio/virtio: Introduce a vfio driver over virtio devices

Quoted 17 lines · Jason Gunthorpe
Quoted 8 lines · Jason Wang
Quoted 7 lines · Jason Gunthorpe
Quoted 3 lines · Michael S. Tsirkin

that's easy/practical. If instead VDPA gives the same speed with just shadow vq then keeping this hack in vfio seems like less of a problem. Finally if VDPA is faster then maybe you will reconsider using it ;)

Quoted 7 lines
Quoted 4 lines

It is not all about the speed.

VDPA presents another large and complex software stack in the hypervisor that can be eliminated by simply using VFIO.

vDPA supports standard virtio devices so how did you define complexity?

Quoted 8 lines

As I said, VFIO is already required for other devices in these VMs. So anything incremental over base-line vfio-pci is complexity to minimize.

Everything vdpa does is either redundant or unnecessary compared to VFIO in these environments.

Jason

Yes but you know. There are all kind of environments. I guess you consider yours the most mainstream and important, and are sure it will always stay like this. But if there's a driver that does what you need then you use that. You really should be explaining what vdpa *does not* do that you need.

But anyway, if Alex wants to maintain this it's not too bad, but I would like to see more code move into a library living under the virtio directory. As it is structured now it will make virtio core development harder.

Signature · 6 lines
MST

_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization
#166 lore raw

Re: [PATCH vfio 11/11] vfio/virtio: Introduce a vfio driver over virtio devices

Quoted 25 lines · Michael S. Tsirkin
Quoted 24 lines · Jason Gunthorpe
Quoted 8 lines · Jason Wang
Quoted 7 lines · Jason Gunthorpe
Quoted 3 lines · Michael S. Tsirkin

that's easy/practical. If instead VDPA gives the same speed with just shadow vq then keeping this hack in vfio seems like less of a problem. Finally if VDPA is faster then maybe you will reconsider using it ;)

Quoted 7 lines
Quoted 4 lines

It is not all about the speed.

VDPA presents another large and complex software stack in the hypervisor that can be eliminated by simply using VFIO.

vDPA supports standard virtio devices so how did you define complexity?

As I said, VFIO is already required for other devices in these VMs. So anything incremental over base-line vfio-pci is complexity to minimize.

Everything vdpa does is either redundant or unnecessary compared to VFIO in these environments.

Quoted 6 lines
Quoted 1 line

Jason

Yes but you know. There are all kind of environments. I guess you consider yours the most mainstream and important, and are sure it will always stay like this. But if there's a driver that does what you need then you use that.

Come on, you are the one saying we cannot do things in the best way possible because you want your way of doing things to be the only way allowed. Which of us thinks "yours the most mainstream and important" ??

I'm not telling you to throw away VPDA, I'm saying there are legimitate real world use cases where VFIO is the appropriate interface, not VDPA.

I want choice, not dogmatic exclusion that there is Only One True Way.

Quoted 2 lines

You really should be explaining what vdpa *does not* do that you need.

I think I've done that enough, but if you have been following my explanation you should see that the entire point of this design is to allow a virtio device to be created inside a DPU to a specific detailed specification (eg an AWS virtio-net device, for instance)

The implementation is in the DPU, and only the DPU.

At the end of the day VDPA uses mediation and creates some RedHat/VDPA/Qemu virtio-net device in the guest. It is emphatically NOT a perfect recreation of the "AWS virtio-net" we started out with.

It entirely fails to achieve the most important thing it needs to do!

Yishai will rework the series with your remarks, we can look again on v2, thanks for all the input!

Jason

#167 lore raw

Re: [PATCH vfio 11/11] vfio/virtio: Introduce a vfio driver over virtio devices

Quoted 37 lines · Jason Gunthorpe
Quoted 25 lines · Michael S. Tsirkin
Quoted 24 lines · Jason Gunthorpe
Quoted 8 lines · Jason Wang
Quoted 7 lines · Jason Gunthorpe
Quoted 3 lines · Michael S. Tsirkin

that's easy/practical. If instead VDPA gives the same speed with just shadow vq then keeping this hack in vfio seems like less of a problem. Finally if VDPA is faster then maybe you will reconsider using it ;)

Quoted 7 lines
Quoted 4 lines

It is not all about the speed.

VDPA presents another large and complex software stack in the hypervisor that can be eliminated by simply using VFIO.

vDPA supports standard virtio devices so how did you define complexity?

As I said, VFIO is already required for other devices in these VMs. So anything incremental over base-line vfio-pci is complexity to minimize.

Everything vdpa does is either redundant or unnecessary compared to VFIO in these environments.

Quoted 6 lines
Quoted 1 line

Jason

Yes but you know. There are all kind of environments. I guess you consider yours the most mainstream and important, and are sure it will always stay like this. But if there's a driver that does what you need then you use that.

Come on, you are the one saying we cannot do things in the best way possible because you want your way of doing things to be the only way allowed. Which of us thinks "yours the most mainstream and important" ??

Quoted 5 lines

I'm not telling you to throw away VPDA, I'm saying there are legimitate real world use cases where VFIO is the appropriate interface, not VDPA.

I want choice, not dogmatic exclusion that there is Only One True Way.

I don't particularly think there's only one way, vfio is already there. I am specifically thinking about this patch, for example it muddies the waters a bit: normally I think vfio exposed device with the same ID, suddenly it changes the ID as visible to the guest. But again, whether doing this kind of thing is OK is more up to Alex than me.

I do want to understand if there's a use-case that vdpa does not address simply because it might be worth while to extend it to do so, and a bunch of people working on it are at Red Hat and I might have some input into how that labor is allocated. But if the use-case is simply "has to be vfio and not vdpa" then I guess not.

Quoted 8 lines
Quoted 2 lines

You really should be explaining what vdpa *does not* do that you need.

I think I've done that enough, but if you have been following my explanation you should see that the entire point of this design is to allow a virtio device to be created inside a DPU to a specific detailed specification (eg an AWS virtio-net device, for instance)

Quoted 7 lines

The implementation is in the DPU, and only the DPU.

At the end of the day VDPA uses mediation and creates some RedHat/VDPA/Qemu virtio-net device in the guest. It is emphatically NOT a perfect recreation of the "AWS virtio-net" we started out with.

It entirely fails to achieve the most important thing it needs to do!

It could be that we are using mediation differently - in my world it's when there's some host software on the path between guest and hardware, and this qualifies. The difference between what this patch does and what vdpa does seems quantitative, not qualitative. Which might be enough to motivate this work, I don't mind. But you seem to feel it is qualitative and I am genuinely curious about it, because if yes then it might lead e.g. the virtio standard in new directions.

I can *imagine* all kind of reasons to want to use vfio as compared to vdpa; here are some examples I came up with, quickly:
- maybe you have drivers that poke at registers not in virtio spec:
vfio allows that, vdpa by design does not
- maybe you are using vfio with a lot of devices already and don't want to special-case handling for virtio devices on the host
do any of the above motivations ring the bell? Some of the things you said seem to hint at that. If yes maybe include this in the cover letter.

There is also a question of capability. Specifically iommufd support is lacking in vdpa (though there are finally some RFC patches to address that). All this is fine, could be enough to motivate a work like this one. But I am very curious to know if there is any other capability lacking in vdpa. I asked already and you didn't answer so I guess not?

Quoted 4 lines

Yishai will rework the series with your remarks, we can look again on v2, thanks for all the input!

Jason

_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization

#168 lore raw

Re: [PATCH vfio 11/11] vfio/virtio: Introduce a vfio driver over virtio devices

Quoted 5 lines · Michael S. Tsirkin

I do want to understand if there's a use-case that vdpa does not address simply because it might be worth while to extend it to do so, and a bunch of people working on it are at Red Hat and I might have some input into how that labor is allocated. But if the use-case is simply "has to be vfio and not vdpa" then I guess not.

If you strip away all the philisophical arguing VDPA has no way to isolate the control and data virtqs to different IOMMU configurations with this single PCI function.

The existing HW VDPA drivers provided device specific ways to handle this.

Without DMA isolation you can't assign the high speed data virtq's to the VM without mediating them as well.

Quoted 3 lines

It could be that we are using mediation differently - in my world it's when there's some host software on the path between guest and hardware, and this qualifies.

That is pretty general. As I said to Jason, if you want to use it that way then you need to make up a new word to describe what VDPA does as there is a clear difference in scope between this VFIO patch (relay IO commands to the device) and VDPA (intercept all the control plane, control virtq and bring it to a RedHat/qemu standard common behavior)

Quoted 4 lines

There is also a question of capability. Specifically iommufd support is lacking in vdpa (though there are finally some RFC patches to address that). All this is fine, could be enough to motivate a work like this one.

I've answered many times, you just don't semm to like the answers or dismiss them as not relevant to you.

Jason

#169 lore raw

Re: [PATCH vfio 11/11] vfio/virtio: Introduce a vfio driver over virtio devices

Quoted 11 lines · Jason Gunthorpe
Quoted 5 lines · Michael S. Tsirkin

I do want to understand if there's a use-case that vdpa does not address simply because it might be worth while to extend it to do so, and a bunch of people working on it are at Red Hat and I might have some input into how that labor is allocated. But if the use-case is simply "has to be vfio and not vdpa" then I guess not.

If you strip away all the philisophical arguing VDPA has no way to isolate the control and data virtqs to different IOMMU configurations with this single PCI function.

Aha, so address space/PASID support then?

Quoted 10 lines

The existing HW VDPA drivers provided device specific ways to handle this.

Without DMA isolation you can't assign the high speed data virtq's to the VM without mediating them as well.

Quoted 3 lines

It could be that we are using mediation differently - in my world it's when there's some host software on the path between guest and hardware, and this qualifies.

Quoted 5 lines

That is pretty general. As I said to Jason, if you want to use it that way then you need to make up a new word to describe what VDPA does as there is a clear difference in scope between this VFIO patch (relay IO commands to the device) and VDPA (intercept all the control plane, control virtq and bring it to a RedHat/qemu standard common behavior)

IIUC VDPA itself does not really bring it to either RedHat or qemu standard, it just allows userspace to control behaviour - if userspace is qemu then it's qemu deciding how it behaves. Which I guess this doesn't. Right? RedHat's not in the picture at all I think.

Quoted 9 lines
Quoted 4 lines

There is also a question of capability. Specifically iommufd support is lacking in vdpa (though there are finally some RFC patches to address that). All this is fine, could be enough to motivate a work like this one.

I've answered many times, you just don't semm to like the answers or dismiss them as not relevant to you.

Jason

Not really I think I lack some of the picture so I don't fully understand. Or maybe I missed something else.

Signature · 6 lines
MST

_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization
#170 lore raw

Re: [PATCH vfio 11/11] vfio/virtio: Introduce a vfio driver over virtio devices

In reply to Yishai Hadas
Quoted 323 lines · On Thu, 21 Sep 2023 15:40:40 +0300 Yishai Hadas <[email protected]> wrote:

Introduce a vfio driver over virtio devices to support the legacy interface functionality for VFs.

Background, from the virtio spec [1].
--------------------------------------------------------------------
In some systems, there is a need to support a virtio legacy driver with a device that does not directly support the legacy interface. In such scenarios, a group owner device can provide the legacy interface functionality for the group member devices. The driver of the owner device can then access the legacy interface of a member device on behalf of the legacy member device driver.

For example, with the SR-IOV group type, group members (VFs) can not present the legacy interface in an I/O BAR in BAR0 as expected by the legacy pci driver. If the legacy driver is running inside a virtual machine, the hypervisor executing the virtual machine can present a virtual device with an I/O BAR in BAR0. The hypervisor intercepts the legacy driver accesses to this I/O BAR and forwards them to the group owner device (PF) using group administration commands.
--------------------------------------------------------------------

Specifically, this driver adds support for a virtio-net VF to be exposed as a transitional device to a guest driver and allows the legacy IO BAR functionality on top.

This allows a VM which uses a legacy virtio-net driver in the guest to work transparently over a VF which its driver in the host is that new driver.

The driver can be extended easily to support some other types of virtio devices (e.g virtio-blk), by adding in a few places the specific type properties as was done for virtio-net.

For now, only the virtio-net use case was tested and as such we introduce the support only for such a device.

Practically,
Upon probing a VF for a virtio-net device, in case its PF supports legacy access over the virtio admin commands and the VF doesn't have BAR 0, we set some specific 'vfio_device_ops' to be able to simulate in SW a transitional device with I/O BAR in BAR 0.

The existence of the simulated I/O bar is reported later on by overwriting the VFIO_DEVICE_GET_REGION_INFO command and the device exposes itself as a transitional device by overwriting some properties upon reading its config space.

Once we report the existence of I/O BAR as BAR 0 a legacy driver in the guest may use it via read/write calls according to the virtio specification.

Any read/write towards the control parts of the BAR will be captured by the new driver and will be translated into admin commands towards the device.

Any data path read/write access (i.e. virtio driver notifications) will be forwarded to the physical BAR which its properties were supplied by the command VIRTIO_PCI_QUEUE_NOTIFY upon the probing/init flow.

With that code in place a legacy driver in the guest has the look and feel as if having a transitional device with legacy support for both its control and data path flows.

[1]
https://github.com/oasis-tcs/virtio-spec/commit/03c2d32e5093ca9f2a17797242fbef88efe94b8c

---
 MAINTAINERS                      |   6 +
 drivers/vfio/pci/Kconfig         |   2 +
 drivers/vfio/pci/Makefile        |   2 +
 drivers/vfio/pci/virtio/Kconfig  |  15 +
 drivers/vfio/pci/virtio/Makefile |   4 +
 drivers/vfio/pci/virtio/cmd.c    |   4 +-
 drivers/vfio/pci/virtio/cmd.h    |   8 +
 drivers/vfio/pci/virtio/main.c   | 546 +++++++++++++++++++++++++++++++
 8 files changed, 585 insertions(+), 2 deletions(-)
 create mode 100644 drivers/vfio/pci/virtio/Kconfig
 create mode 100644 drivers/vfio/pci/virtio/Makefile
 create mode 100644 drivers/vfio/pci/virtio/main.c
MAINTAINERS +6 -0
index bf0f54c24f81..5098418c8389 100644
@@ -22624,6 +22624,12 @@ L:	[email protected]
 S:	Maintained
 F:	drivers/vfio/pci/mlx5/
 
+VFIO VIRTIO PCI DRIVER
+M:	Yishai Hadas <[email protected]>
+L:	[email protected]
+S:	Maintained
+F:	drivers/vfio/pci/virtio
+
 VFIO PCI DEVICE SPECIFIC DRIVERS
 R:	Jason Gunthorpe <[email protected]>
 R:	Yishai Hadas <[email protected]>
drivers/vfio/pci/Kconfig +2 -0
index 8125e5f37832..18c397df566d 100644
@@ -65,4 +65,6 @@ source "drivers/vfio/pci/hisilicon/Kconfig"
 
 source "drivers/vfio/pci/pds/Kconfig"
 
+source "drivers/vfio/pci/virtio/Kconfig"
+
 endmenu
drivers/vfio/pci/Makefile +2 -0
index 45167be462d8..046139a4eca5 100644
@@ -13,3 +13,5 @@ obj-$(CONFIG_MLX5_VFIO_PCI)           += mlx5/
 obj-$(CONFIG_HISI_ACC_VFIO_PCI) += hisilicon/
 
 obj-$(CONFIG_PDS_VFIO_PCI) += pds/
+
+obj-$(CONFIG_VIRTIO_VFIO_PCI) += virtio/
drivers/vfio/pci/virtio/Kconfig new file +15 -0
new file mode 100644
index 000000000000..89eddce8b1bd
@@ -0,0 +1,15 @@
+# SPDX-License-Identifier: GPL-2.0-only
+config VIRTIO_VFIO_PCI
+        tristate "VFIO support for VIRTIO PCI devices"
+        depends on VIRTIO_PCI
+        select VFIO_PCI_CORE
+        help
+          This provides support for exposing VIRTIO VF devices using the VFIO
+          framework that can work with a legacy virtio driver in the guest.
+          Based on PCIe spec, VFs do not support I/O Space; thus, VF BARs shall
+          not indicate I/O Space.
+          As of that this driver emulated I/O BAR in software to let a VF be
+          seen as a transitional device in the guest and let it work with
+          a legacy driver.
+
+          If you don't know what to do here, say N.
drivers/vfio/pci/virtio/Makefile new file +4 -0
new file mode 100644
index 000000000000..584372648a03
@@ -0,0 +1,4 @@
+# SPDX-License-Identifier: GPL-2.0-only
+obj-$(CONFIG_VIRTIO_VFIO_PCI) += virtio-vfio-pci.o
+virtio-vfio-pci-y := main.o cmd.o
+
drivers/vfio/pci/virtio/cmd.c +2 -2
index f068239cdbb0..aea9d25fbf1d 100644
@@ -44,7 +44,7 @@ int virtiovf_cmd_lr_write(struct virtiovf_pci_core_device *virtvdev, u16 opcode,
 {
 	struct virtio_device *virtio_dev =
 		virtio_pci_vf_get_pf_dev(virtvdev->core_device.pdev);
-	struct virtio_admin_cmd_data_lr_write *in;
+	struct virtio_admin_cmd_legacy_wr_data *in;
 	struct scatterlist in_sg;
 	struct virtio_admin_cmd cmd = {};
 	int ret;
@@ -74,7 +74,7 @@ int virtiovf_cmd_lr_read(struct virtiovf_pci_core_device *virtvdev, u16 opcode,
 {
 	struct virtio_device *virtio_dev =
 		virtio_pci_vf_get_pf_dev(virtvdev->core_device.pdev);
-	struct virtio_admin_cmd_data_lr_read *in;
+	struct virtio_admin_cmd_legacy_rd_data *in;
 	struct scatterlist in_sg, out_sg;
 	struct virtio_admin_cmd cmd = {};
 	int ret;
drivers/vfio/pci/virtio/cmd.h +8 -0
index c2a3645f4b90..347b1dc85570 100644
@@ -13,7 +13,15 @@
 
 struct virtiovf_pci_core_device {
 	struct vfio_pci_core_device core_device;
+	u8 bar0_virtual_buf_size;
+	u8 *bar0_virtual_buf;
+	/* synchronize access to the virtual buf */
+	struct mutex bar_mutex;
 	int vf_id;
+	void __iomem *notify_addr;
+	u32 notify_offset;
+	u8 notify_bar;
+	u8 pci_cmd_io :1;
 };
 
 int virtiovf_cmd_list_query(struct pci_dev *pdev, u8 *buf, int buf_size);
drivers/vfio/pci/virtio/main.c new file +125 -0
new file mode 100644
index 000000000000..2486991c49f3
@@ -0,0 +1,546 @@
+// SPDX-License-Identifier: GPL-2.0-only
+/*
+ * Copyright (c) 2023, NVIDIA CORPORATION & AFFILIATES. All rights reserved
+ */
+
+#include <linux/device.h>
+#include <linux/module.h>
+#include <linux/mutex.h>
+#include <linux/pci.h>
+#include <linux/pm_runtime.h>
+#include <linux/types.h>
+#include <linux/uaccess.h>
+#include <linux/vfio.h>
+#include <linux/vfio_pci_core.h>
+#include <linux/virtio_pci.h>
+#include <linux/virtio_net.h>
+#include <linux/virtio_pci_modern.h>
+
+#include "cmd.h"
+
+#define VIRTIO_LEGACY_IO_BAR_HEADER_LEN 20
+#define VIRTIO_LEGACY_IO_BAR_MSIX_HEADER_LEN 4
+
+static int virtiovf_issue_lr_cmd(struct virtiovf_pci_core_device *virtvdev,
+				 loff_t pos, char __user *buf,
+				 size_t count, bool read)
+{
+	u8 *bar0_buf = virtvdev->bar0_virtual_buf;
+	u16 opcode;
+	int ret;
+
+	mutex_lock(&virtvdev->bar_mutex);
+	if (read) {
+		opcode = (pos < VIRTIO_PCI_CONFIG_OFF(true)) ?
+			VIRTIO_ADMIN_CMD_LEGACY_COMMON_CFG_READ :
+			VIRTIO_ADMIN_CMD_LEGACY_DEV_CFG_READ;
+		ret = virtiovf_cmd_lr_read(virtvdev, opcode, pos,
+					   count, bar0_buf + pos);
+		if (ret)
+			goto out;
+		if (copy_to_user(buf, bar0_buf + pos, count))
+			ret = -EFAULT;
+		goto out;
+	}
+
+	if (copy_from_user(bar0_buf + pos, buf, count)) {
+		ret = -EFAULT;
+		goto out;
+	}
+
+	opcode = (pos < VIRTIO_PCI_CONFIG_OFF(true)) ?
+			VIRTIO_ADMIN_CMD_LEGACY_COMMON_CFG_WRITE :
+			VIRTIO_ADMIN_CMD_LEGACY_DEV_CFG_WRITE;
+	ret = virtiovf_cmd_lr_write(virtvdev, opcode, pos, count,
+				    bar0_buf + pos);
+out:
+	mutex_unlock(&virtvdev->bar_mutex);
+	return ret;
+}
+
+static int
+translate_io_bar_to_mem_bar(struct virtiovf_pci_core_device *virtvdev,
+			    loff_t pos, char __user *buf,
+			    size_t count, bool read)
+{
+	struct vfio_pci_core_device *core_device = &virtvdev->core_device;
+	u16 queue_notify;
+	int ret;
+
+	if (pos + count > virtvdev->bar0_virtual_buf_size)
+		return -EINVAL;
+
+	switch (pos) {
+	case VIRTIO_PCI_QUEUE_NOTIFY:
+		if (count != sizeof(queue_notify))
+			return -EINVAL;
+		if (read) {
+			ret = vfio_pci_ioread16(core_device, true, &queue_notify,
+						virtvdev->notify_addr);
+			if (ret)
+				return ret;
+			if (copy_to_user(buf, &queue_notify,
+					 sizeof(queue_notify)))
+				return -EFAULT;
+			break;
+		}
+
+		if (copy_from_user(&queue_notify, buf, count))
+			return -EFAULT;
+
+		ret = vfio_pci_iowrite16(core_device, true, queue_notify,
+					 virtvdev->notify_addr);
+		break;
+	default:
+		ret = virtiovf_issue_lr_cmd(virtvdev, pos, buf, count, read);
+	}
+
+	return ret ? ret : count;
+}
+
+static bool range_contains_range(loff_t range1_start, size_t count1,
+				 loff_t range2_start, size_t count2,
+				 loff_t *start_offset)
+{
+	if (range1_start <= range2_start &&
+	    range1_start + count1 >= range2_start + count2) {
+		*start_offset = range2_start - range1_start;
+		return true;
+	}
+	return false;
+}
+
+static ssize_t virtiovf_pci_read_config(struct vfio_device *core_vdev,
+					char __user *buf, size_t count,
+					loff_t *ppos)
+{
+	struct virtiovf_pci_core_device *virtvdev = container_of(
+		core_vdev, struct virtiovf_pci_core_device, core_device.vdev);
+	loff_t pos = *ppos & VFIO_PCI_OFFSET_MASK;
+	loff_t copy_offset;
+	__le32 val32;
+	__le16 val16;
+	u8 val8;
+	int ret;
+

drivers/vfio/pci/virtio/main.c line 135

Quoted 10 lines
drivers/vfio/pci/virtio/main.c new file +10 -0
+	ret = vfio_pci_core_read(core_vdev, buf, count, ppos);
+	if (ret < 0)
+		return ret;
+
+	if (range_contains_range(pos, count, PCI_DEVICE_ID, sizeof(val16),
+				 &copy_offset)) {
+		val16 = cpu_to_le16(0x1000);
+		if (copy_to_user(buf + copy_offset, &val16, sizeof(val16)))
+			return -EFAULT;
+	}

So we take a 0x1041 ("Virtio 1.0 network device") and turn it into a 0x1000 ("Virtio network device"). Are there no features implied by the device ID? NB, a byte-wise access would read the real device ID.

drivers/vfio/pci/virtio/main.c line 145

Quoted 10 lines
drivers/vfio/pci/virtio/main.c new file +10 -0
+
+	if (virtvdev->pci_cmd_io &&
+	    range_contains_range(pos, count, PCI_COMMAND, sizeof(val16),
+				 &copy_offset)) {
+		if (copy_from_user(&val16, buf, sizeof(val16)))
+			return -EFAULT;
+		val16 |= cpu_to_le16(PCI_COMMAND_IO);
+		if (copy_to_user(buf + copy_offset, &val16, sizeof(val16)))
+			return -EFAULT;
+	}

So we can't turn off I/O memory.

drivers/vfio/pci/virtio/main.c line 153

Quoted 8 lines
drivers/vfio/pci/virtio/main.c new file +8 -0
+
+	if (range_contains_range(pos, count, PCI_REVISION_ID, sizeof(val8),
+				 &copy_offset)) {
+		/* Transional needs to have revision 0 */
+		val8 = 0;
+		if (copy_to_user(buf + copy_offset, &val8, sizeof(val8)))
+			return -EFAULT;
+	}

Surely some driver cares about this, right? How is this supposed to work in a world where libvirt parses modules.alias and automatically loads this driver rather than vfio-pci for all 0x1041 devices? We'd need to denylist this driver to ever see the device for what it is.

drivers/vfio/pci/virtio/main.c line 160

Quoted 7 lines
drivers/vfio/pci/virtio/main.c new file +7 -0
+
+	if (range_contains_range(pos, count, PCI_BASE_ADDRESS_0, sizeof(val32),
+				 &copy_offset)) {
+		val32 = cpu_to_le32(PCI_BASE_ADDRESS_SPACE_IO);
+		if (copy_to_user(buf + copy_offset, &val32, sizeof(val32)))
+			return -EFAULT;
+	}

Sloppy BAR emulation compared to the real BARs. QEMU obviously doesn't care.

drivers/vfio/pci/virtio/main.c line 166

Quoted 6 lines
drivers/vfio/pci/virtio/main.c new file +6 -0
+
+	if (range_contains_range(pos, count, PCI_SUBSYSTEM_ID, sizeof(val16),
+				 &copy_offset)) {
+		/* Transitional devices use the PCI subsystem device id as
+		 * virtio device id, same as legacy driver always did.
+		 */

Non-networking multi-line comment style throughout please.

Quoted 26 lines
drivers/vfio/pci/virtio/main.c new file +26 -0
+		val16 = cpu_to_le16(VIRTIO_ID_NET);
+		if (copy_to_user(buf + copy_offset, &val16, sizeof(val16)))
+			return -EFAULT;
+	}
+
+	return count;
+}
+
+static ssize_t
+virtiovf_pci_core_read(struct vfio_device *core_vdev, char __user *buf,
+		       size_t count, loff_t *ppos)
+{
+	struct virtiovf_pci_core_device *virtvdev = container_of(
+		core_vdev, struct virtiovf_pci_core_device, core_device.vdev);
+	struct pci_dev *pdev = virtvdev->core_device.pdev;
+	unsigned int index = VFIO_PCI_OFFSET_TO_INDEX(*ppos);
+	loff_t pos = *ppos & VFIO_PCI_OFFSET_MASK;
+	int ret;
+
+	if (!count)
+		return 0;
+
+	if (index == VFIO_PCI_CONFIG_REGION_INDEX)
+		return virtiovf_pci_read_config(core_vdev, buf, count, ppos);
+
+	if (index != VFIO_PCI_BAR0_REGION_INDEX)

drivers/vfio/pci/virtio/main.c line 202

Quoted 10 lines
drivers/vfio/pci/virtio/main.c new file +10 -0
+		return vfio_pci_core_read(core_vdev, buf, count, ppos);
+
+	ret = pm_runtime_resume_and_get(&pdev->dev);
+	if (ret) {
+		pci_info_ratelimited(pdev, "runtime resume failed %d\n",
+				     ret);
+		return -EIO;
+	}
+
+	ret = translate_io_bar_to_mem_bar(virtvdev, pos, buf, count, true);

If the heart of this driver is simply pretending to have an I/O BAR where I/O accesses into that BAR are translated to accesses in the MMIO BAR, why can't this be done in the VMM, ie. QEMU? Could I/O to MMIO translation in QEMU improve performance (ex. if the MMIO is mmap'd and can be accessed without bouncing back into kernel code)?

Quoted 17 lines
drivers/vfio/pci/virtio/main.c new file +17 -0
+	pm_runtime_put(&pdev->dev);
+	return ret;
+}
+
+static ssize_t
+virtiovf_pci_core_write(struct vfio_device *core_vdev, const char __user *buf,
+			size_t count, loff_t *ppos)
+{
+	struct virtiovf_pci_core_device *virtvdev = container_of(
+		core_vdev, struct virtiovf_pci_core_device, core_device.vdev);
+	struct pci_dev *pdev = virtvdev->core_device.pdev;
+	unsigned int index = VFIO_PCI_OFFSET_TO_INDEX(*ppos);
+	loff_t pos = *ppos & VFIO_PCI_OFFSET_MASK;
+	int ret;
+
+	if (!count)
+		return 0;

drivers/vfio/pci/virtio/main.c line 229

Quoted 10 lines
drivers/vfio/pci/virtio/main.c new file +10 -0
+
+	if (index == VFIO_PCI_CONFIG_REGION_INDEX) {
+		loff_t copy_offset;
+		u16 cmd;
+
+		if (range_contains_range(pos, count, PCI_COMMAND, sizeof(cmd),
+					 &copy_offset)) {
+			if (copy_from_user(&cmd, buf + copy_offset, sizeof(cmd)))
+				return -EFAULT;
+			virtvdev->pci_cmd_io = (cmd & PCI_COMMAND_IO);

If we're tracking writes to PCI_COMMAND_IO, why did we statically report I/O enabled in the read function previously?

Quoted 97 lines
drivers/vfio/pci/virtio/main.c new file +97 -0
+		}
+	}
+
+	if (index != VFIO_PCI_BAR0_REGION_INDEX)
+		return vfio_pci_core_write(core_vdev, buf, count, ppos);
+
+	ret = pm_runtime_resume_and_get(&pdev->dev);
+	if (ret) {
+		pci_info_ratelimited(pdev, "runtime resume failed %d\n", ret);
+		return -EIO;
+	}
+
+	ret = translate_io_bar_to_mem_bar(virtvdev, pos, (char __user *)buf, count, false);
+	pm_runtime_put(&pdev->dev);
+	return ret;
+}
+
+static int
+virtiovf_pci_ioctl_get_region_info(struct vfio_device *core_vdev,
+				   unsigned int cmd, unsigned long arg)
+{
+	struct virtiovf_pci_core_device *virtvdev = container_of(
+		core_vdev, struct virtiovf_pci_core_device, core_device.vdev);
+	unsigned long minsz = offsetofend(struct vfio_region_info, offset);
+	void __user *uarg = (void __user *)arg;
+	struct vfio_region_info info = {};
+
+	if (copy_from_user(&info, uarg, minsz))
+		return -EFAULT;
+
+	if (info.argsz < minsz)
+		return -EINVAL;
+
+	switch (info.index) {
+	case VFIO_PCI_BAR0_REGION_INDEX:
+		info.offset = VFIO_PCI_INDEX_TO_OFFSET(info.index);
+		info.size = virtvdev->bar0_virtual_buf_size;
+		info.flags = VFIO_REGION_INFO_FLAG_READ |
+			     VFIO_REGION_INFO_FLAG_WRITE;
+		return copy_to_user(uarg, &info, minsz) ? -EFAULT : 0;
+	default:
+		return vfio_pci_core_ioctl(core_vdev, cmd, arg);
+	}
+}
+
+static long
+virtiovf_vfio_pci_core_ioctl(struct vfio_device *core_vdev, unsigned int cmd,
+			     unsigned long arg)
+{
+	switch (cmd) {
+	case VFIO_DEVICE_GET_REGION_INFO:
+		return virtiovf_pci_ioctl_get_region_info(core_vdev, cmd, arg);
+	default:
+		return vfio_pci_core_ioctl(core_vdev, cmd, arg);
+	}
+}
+
+static int
+virtiovf_set_notify_addr(struct virtiovf_pci_core_device *virtvdev)
+{
+	struct vfio_pci_core_device *core_device = &virtvdev->core_device;
+	int ret;
+
+	/* Setup the BAR where the 'notify' exists to be used by vfio as well
+	 * This will let us mmap it only once and use it when needed.
+	 */
+	ret = vfio_pci_core_setup_barmap(core_device,
+					 virtvdev->notify_bar);
+	if (ret)
+		return ret;
+
+	virtvdev->notify_addr = core_device->barmap[virtvdev->notify_bar] +
+			virtvdev->notify_offset;
+	return 0;
+}
+
+static int virtiovf_pci_open_device(struct vfio_device *core_vdev)
+{
+	struct virtiovf_pci_core_device *virtvdev = container_of(
+		core_vdev, struct virtiovf_pci_core_device, core_device.vdev);
+	struct vfio_pci_core_device *vdev = &virtvdev->core_device;
+	int ret;
+
+	ret = vfio_pci_core_enable(vdev);
+	if (ret)
+		return ret;
+
+	if (virtvdev->bar0_virtual_buf) {
+		/* upon close_device() the vfio_pci_core_disable() is called
+		 * and will close all the previous mmaps, so it seems that the
+		 * valid life cycle for the 'notify' addr is per open/close.
+		 */
+		ret = virtiovf_set_notify_addr(virtvdev);
+		if (ret) {
+			vfio_pci_core_disable(vdev);
+			return ret;
+		}

drivers/vfio/pci/virtio/main.c line 336

Quoted 10 lines
drivers/vfio/pci/virtio/main.c new file +10 -0
+	}
+
+	vfio_pci_core_finish_enable(vdev);
+	return 0;
+}
+
+static void virtiovf_pci_close_device(struct vfio_device *core_vdev)
+{
+	vfio_pci_core_close_device(core_vdev);
+}

Why does this function exist?

Quoted 34 lines
drivers/vfio/pci/virtio/main.c new file +34 -0
+
+static int virtiovf_get_device_config_size(unsigned short device)
+{
+	switch (device) {
+	case 0x1041:
+		/* network card */
+		return offsetofend(struct virtio_net_config, status);
+	default:
+		return 0;
+	}
+}
+
+static int virtiovf_read_notify_info(struct virtiovf_pci_core_device *virtvdev)
+{
+	u64 offset;
+	int ret;
+	u8 bar;
+
+	ret = virtiovf_cmd_lq_read_notify(virtvdev,
+				VIRTIO_ADMIN_CMD_NOTIFY_INFO_FLAGS_OWNER_MEM,
+				&bar, &offset);
+	if (ret)
+		return ret;
+
+	virtvdev->notify_bar = bar;
+	virtvdev->notify_offset = offset;
+	return 0;
+}
+
+static int virtiovf_pci_init_device(struct vfio_device *core_vdev)
+{
+	struct virtiovf_pci_core_device *virtvdev = container_of(
+		core_vdev, struct virtiovf_pci_core_device, core_device.vdev);
+	struct pci_dev *pdev;

drivers/vfio/pci/virtio/main.c line 380

Quoted 10 lines
drivers/vfio/pci/virtio/main.c new file +10 -0
+	int ret;
+
+	ret = vfio_pci_core_init_dev(core_vdev);
+	if (ret)
+		return ret;
+
+	pdev = virtvdev->core_device.pdev;
+	virtvdev->vf_id = pci_iov_vf_id(pdev);
+	if (virtvdev->vf_id < 0)
+		return -EINVAL;

vf_id is never used.

Quoted 49 lines
drivers/vfio/pci/virtio/main.c new file +49 -0
+
+	ret = virtiovf_read_notify_info(virtvdev);
+	if (ret)
+		return ret;
+
+	virtvdev->bar0_virtual_buf_size = VIRTIO_LEGACY_IO_BAR_HEADER_LEN +
+		VIRTIO_LEGACY_IO_BAR_MSIX_HEADER_LEN +
+		virtiovf_get_device_config_size(pdev->device);
+	virtvdev->bar0_virtual_buf = kzalloc(virtvdev->bar0_virtual_buf_size,
+					     GFP_KERNEL);
+	if (!virtvdev->bar0_virtual_buf)
+		return -ENOMEM;
+	mutex_init(&virtvdev->bar_mutex);
+	return 0;
+}
+
+static void virtiovf_pci_core_release_dev(struct vfio_device *core_vdev)
+{
+	struct virtiovf_pci_core_device *virtvdev = container_of(
+		core_vdev, struct virtiovf_pci_core_device, core_device.vdev);
+
+	kfree(virtvdev->bar0_virtual_buf);
+	vfio_pci_core_release_dev(core_vdev);
+}
+
+static const struct vfio_device_ops virtiovf_acc_vfio_pci_tran_ops = {
+	.name = "virtio-transitional-vfio-pci",
+	.init = virtiovf_pci_init_device,
+	.release = virtiovf_pci_core_release_dev,
+	.open_device = virtiovf_pci_open_device,
+	.close_device = virtiovf_pci_close_device,
+	.ioctl = virtiovf_vfio_pci_core_ioctl,
+	.read = virtiovf_pci_core_read,
+	.write = virtiovf_pci_core_write,
+	.mmap = vfio_pci_core_mmap,
+	.request = vfio_pci_core_request,
+	.match = vfio_pci_core_match,
+	.bind_iommufd = vfio_iommufd_physical_bind,
+	.unbind_iommufd = vfio_iommufd_physical_unbind,
+	.attach_ioas = vfio_iommufd_physical_attach_ioas,
+};
+
+static const struct vfio_device_ops virtiovf_acc_vfio_pci_ops = {
+	.name = "virtio-acc-vfio-pci",
+	.init = vfio_pci_core_init_dev,
+	.release = vfio_pci_core_release_dev,
+	.open_device = virtiovf_pci_open_device,
+	.close_device = virtiovf_pci_close_device,
+	.ioctl = vfio_pci_core_ioctl,

drivers/vfio/pci/virtio/main.c line 439

Quoted 10 lines
drivers/vfio/pci/virtio/main.c new file +10 -0
+	.device_feature = vfio_pci_core_ioctl_feature,
+	.read = vfio_pci_core_read,
+	.write = vfio_pci_core_write,
+	.mmap = vfio_pci_core_mmap,
+	.request = vfio_pci_core_request,
+	.match = vfio_pci_core_match,
+	.bind_iommufd = vfio_iommufd_physical_bind,
+	.unbind_iommufd = vfio_iommufd_physical_unbind,
+	.attach_ioas = vfio_iommufd_physical_attach_ioas,
+};

Why are we claiming devices that should just use vfio-pci instead?

Quoted 16 lines
drivers/vfio/pci/virtio/main.c new file +16 -0
+
+static bool virtiovf_bar0_exists(struct pci_dev *pdev)
+{
+	struct resource *res = pdev->resource;
+
+	return res->flags ? true : false;
+}
+
+#define VIRTIOVF_USE_ADMIN_CMD_BITMAP \
+	(BIT_ULL(VIRTIO_ADMIN_CMD_LIST_QUERY) | \
+	 BIT_ULL(VIRTIO_ADMIN_CMD_LIST_USE) | \
+	 BIT_ULL(VIRTIO_ADMIN_CMD_LEGACY_COMMON_CFG_WRITE) | \
+	 BIT_ULL(VIRTIO_ADMIN_CMD_LEGACY_COMMON_CFG_READ) | \
+	 BIT_ULL(VIRTIO_ADMIN_CMD_LEGACY_DEV_CFG_WRITE) | \
+	 BIT_ULL(VIRTIO_ADMIN_CMD_LEGACY_DEV_CFG_READ) | \
+	 BIT_ULL(VIRTIO_ADMIN_CMD_LEGACY_NOTIFY_INFO))

drivers/vfio/pci/virtio/main.c line 465

Quoted 10 lines
drivers/vfio/pci/virtio/main.c new file +10 -0
+
+static bool virtiovf_support_legacy_access(struct pci_dev *pdev)
+{
+	int buf_size = DIV_ROUND_UP(VIRTIO_ADMIN_MAX_CMD_OPCODE, 64) * 8;
+	u8 *buf;
+	int ret;
+
+	/* Only virtio-net is supported/tested so far */
+	if (pdev->device != 0x1041)
+		return false;

Seems like the ID table should handle this, why are we preemptively claiming all virtio devices... or actually all 0x1af4 devices, which might not even be virtio, ex. the non-virtio ivshmem devices is 0x1110.

Quoted 51 lines
drivers/vfio/pci/virtio/main.c new file +51 -0
+
+	buf = kzalloc(buf_size, GFP_KERNEL);
+	if (!buf)
+		return false;
+
+	ret = virtiovf_cmd_list_query(pdev, buf, buf_size);
+	if (ret)
+		goto end;
+
+	if ((le64_to_cpup((__le64 *)buf) & VIRTIOVF_USE_ADMIN_CMD_BITMAP) !=
+		VIRTIOVF_USE_ADMIN_CMD_BITMAP) {
+		ret = -EOPNOTSUPP;
+		goto end;
+	}
+
+	/* confirm the used commands */
+	memset(buf, 0, buf_size);
+	*(__le64 *)buf = cpu_to_le64(VIRTIOVF_USE_ADMIN_CMD_BITMAP);
+	ret = virtiovf_cmd_list_use(pdev, buf, buf_size);
+
+end:
+	kfree(buf);
+	return ret ? false : true;
+}
+
+static int virtiovf_pci_probe(struct pci_dev *pdev,
+			      const struct pci_device_id *id)
+{
+	const struct vfio_device_ops *ops = &virtiovf_acc_vfio_pci_ops;
+	struct virtiovf_pci_core_device *virtvdev;
+	int ret;
+
+	if (pdev->is_virtfn && virtiovf_support_legacy_access(pdev) &&
+	    !virtiovf_bar0_exists(pdev) && pdev->msix_cap)
+		ops = &virtiovf_acc_vfio_pci_tran_ops;
+
+	virtvdev = vfio_alloc_device(virtiovf_pci_core_device, core_device.vdev,
+				     &pdev->dev, ops);
+	if (IS_ERR(virtvdev))
+		return PTR_ERR(virtvdev);
+
+	dev_set_drvdata(&pdev->dev, &virtvdev->core_device);
+	ret = vfio_pci_core_register_device(&virtvdev->core_device);
+	if (ret)
+		goto out;
+	return 0;
+out:
+	vfio_put_device(&virtvdev->core_device.vdev);
+	return ret;
+}
+

drivers/vfio/pci/virtio/main.c line 526

Quoted 10 lines
drivers/vfio/pci/virtio/main.c new file +10 -0
+static void virtiovf_pci_remove(struct pci_dev *pdev)
+{
+	struct virtiovf_pci_core_device *virtvdev = dev_get_drvdata(&pdev->dev);
+
+	vfio_pci_core_unregister_device(&virtvdev->core_device);
+	vfio_put_device(&virtvdev->core_device.vdev);
+}
+
+static const struct pci_device_id virtiovf_pci_table[] = {
+	{ PCI_DRIVER_OVERRIDE_DEVICE_VFIO(PCI_VENDOR_ID_REDHAT_QUMRANET, PCI_ANY_ID) },

libvirt will blindly use this driver for all devices matching this as we've discussed how it should make use of modules.alias. I don't think this driver should be squatting on devices where it doesn't add value and it's not clear whether this is adding or subtracting value in all cases for the one NIC that it modifies. How should libvirt choose when and where to use this driver? What regressions are we going to see with VMs that previously saw "modern" virtio-net devices and now see a legacy compatible device? Thanks,

Alex

Quoted 10 lines
drivers/vfio/pci/virtio/main.c new file +10 -0
+	{}
+};
+
+MODULE_DEVICE_TABLE(pci, virtiovf_pci_table);
+
+static struct pci_driver virtiovf_pci_driver = {
+	.name = KBUILD_MODNAME,
+	.id_table = virtiovf_pci_table,
+	.probe = virtiovf_pci_probe,
+	.remove = virtiovf_pci_remove,

drivers/vfio/pci/virtio/main.c line 546

Quoted 10 lines
drivers/vfio/pci/virtio/main.c new file +10 -0
+	.err_handler = &vfio_pci_core_err_handlers,
+	.driver_managed_dma = true,
+};
+
+module_pci_driver(virtiovf_pci_driver);
+
+MODULE_LICENSE("GPL");
+MODULE_AUTHOR("Yishai Hadas <[email protected]>");
+MODULE_DESCRIPTION(
+	"VIRTIO VFIO PCI - User Level meta-driver for VIRTIO device family");

_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization

#171 lore raw

Re: [PATCH vfio 11/11] vfio/virtio: Introduce a vfio driver over virtio devices

Quoted 11 lines · Alex Williamson

drivers/vfio/pci/virtio/main.c line 526

Quoted 2 lines
drivers/vfio/pci/virtio/main.c new file +2 -0
+static const struct pci_device_id virtiovf_pci_table[] = {
+	{ PCI_DRIVER_OVERRIDE_DEVICE_VFIO(PCI_VENDOR_ID_REDHAT_QUMRANET, PCI_ANY_ID) },

libvirt will blindly use this driver for all devices matching this as we've discussed how it should make use of modules.alias. I don't think this driver should be squatting on devices where it doesn't add value and it's not clear whether this is adding or subtracting value in all cases for the one NIC that it modifies. How should libvirt choose when and where to use this driver? What regressions are we going to see with VMs that previously saw "modern" virtio-net devices and now see a legacy compatible device? Thanks,

Maybe this approach needs to use a subsystem ID match?

Jason

#172 lore raw

Re: [PATCH vfio 11/11] vfio/virtio: Introduce a vfio driver over virtio devices

Quoted 14 lines · Jason Gunthorpe
Quoted 11 lines · Alex Williamson

drivers/vfio/pci/virtio/main.c line 526

Quoted 2 lines
drivers/vfio/pci/virtio/main.c new file +2 -0
+static const struct pci_device_id virtiovf_pci_table[] = {
+	{ PCI_DRIVER_OVERRIDE_DEVICE_VFIO(PCI_VENDOR_ID_REDHAT_QUMRANET, PCI_ANY_ID) },

libvirt will blindly use this driver for all devices matching this as we've discussed how it should make use of modules.alias. I don't think this driver should be squatting on devices where it doesn't add value and it's not clear whether this is adding or subtracting value in all cases for the one NIC that it modifies. How should libvirt choose when and where to use this driver? What regressions are we going to see with VMs that previously saw "modern" virtio-net devices and now see a legacy compatible device? Thanks,

Quoted 3 lines

Maybe this approach needs to use a subsystem ID match?

Jason

Maybe make users load it manually?

Please don't bind to virtio by default, you will break all guests.

Signature · 6 lines
MST

_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization
#173 lore raw

Re: [PATCH vfio 11/11] vfio/virtio: Introduce a vfio driver over virtio devices

Quoted 15 lines · On Thu, 21 Sep 2023 16:20:59 -0400 "Michael S. Tsirkin" <[email protected]> wrote:
Quoted 14 lines · Jason Gunthorpe
Quoted 11 lines · Alex Williamson

drivers/vfio/pci/virtio/main.c line 526

Quoted 2 lines
drivers/vfio/pci/virtio/main.c new file +2 -0
+static const struct pci_device_id virtiovf_pci_table[] = {
+	{ PCI_DRIVER_OVERRIDE_DEVICE_VFIO(PCI_VENDOR_ID_REDHAT_QUMRANET, PCI_ANY_ID) },  

libvirt will blindly use this driver for all devices matching this as we've discussed how it should make use of modules.alias. I don't think this driver should be squatting on devices where it doesn't add value and it's not clear whether this is adding or subtracting value in all cases for the one NIC that it modifies. How should libvirt choose when and where to use this driver? What regressions are we going to see with VMs that previously saw "modern" virtio-net devices and now see a legacy compatible device? Thanks,

Quoted 8 lines
Quoted 3 lines

Maybe this approach needs to use a subsystem ID match?

Jason

Maybe make users load it manually?

Please don't bind to virtio by default, you will break all guests.

This would never bind by default, it's only bound as a vfio override driver, but if libvirt were trying to determine the correct driver to use with vfio for a 0x1af4 device, it'd land on this one. Thanks,

Alex

_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization