Skip to contentSkip to conversation

virtgpu: check if P2P is possiable or not

PATCH In review Julia Zhang 3 participants 7 messages Dec 7, 2024 – Dec 13, 2024

Subsystems (5)
lore
Share & tools
lore
#0 lore raw

cover virtgpu: check if P2P is possiable or not

To implement dGPU prime feature, virtgpu needs to import/export buffer between virtio iGPU and passthrough dGPU. Before that, virtgpu should check if P2P is possible or not. But calling function pci_p2pdma_distance in guest VM will only get virtual p2pdma_distance instead of real physical p2pdma_distance.

So this series introduce an implementation of virtgpu device_attach callback to get p2pdma_distance. And also adds a new virtgpu command to pass PCI notations from guest to host and a new xen privcmd to get physical p2pdma_distance according to the PCI notations in host.

Julia Zhang (3):
xen:get p2pdma_distance
virtgpu: get p2pdma_distance
drm/virtio: Implement device_attach

 drivers/gpu/drm/virtio/virtgpu_drv.h   | 17 ++++++++
 drivers/gpu/drm/virtio/virtgpu_prime.c | 59 +++++++++++++++++++++++++-
 drivers/gpu/drm/virtio/virtgpu_vq.c    | 53 +++++++++++++++++++++++
 drivers/xen/privcmd.c                  | 42 ++++++++++++++++++
 include/uapi/linux/virtio_gpu.h        | 19 +++++++++
 include/uapi/xen/privcmd.h             | 12 ++++++
 6 files changed, 201 insertions(+), 1 deletion(-)
Signature · 1 line
2.34.1
#1 lore raw

PATCH 1/3 xen:get p2pdma_distance

To get the p2pdma_distance, this create a new privcmd ioctl to calculate p2pdma_distance for two pci devices on the host with pci notations sent from guest virtgpu driver.

---
 drivers/xen/privcmd.c      | 42 ++++++++++++++++++++++++++++++++++++++
 include/uapi/xen/privcmd.h | 12 +++++++++++
 2 files changed, 54 insertions(+)
drivers/xen/privcmd.c +42 -0
index 72c161e94731..95f67815a2ef 100644
@@ -31,6 +31,9 @@
 #include <linux/miscdevice.h>
 #include <linux/moduleparam.h>
 #include <linux/virtio_mmio.h>
+#include <linux/pci.h>
+#include <linux/pci-p2pdma.h>
+#include <linux/dma-map-ops.h>
 
 #include <asm/xen/hypervisor.h>
 #include <asm/xen/hypercall.h>
@@ -977,6 +980,42 @@ static long privcmd_ioctl_map_hva_to_gpfns(struct file *file, void __user *udata
 	return ret;
 }
 
+static int privcmd_ioctl_p2pdma_distance(struct file *file, void __user *udata)
+{
+	struct privcmd_p2pdma_distance kdata;
+	struct pci_dev *provider = NULL;
+	struct pci_dev *client = NULL;
+	struct pci_dev *dev = NULL;
+	enum pci_p2pdma_map_type map;
+
+	if (copy_from_user(&kdata, udata, sizeof(kdata)))
+		return -EFAULT;
+
+	for_each_pci_dev(dev) {
+		if (dev->bus->number == kdata.provider_bus &&
+		    dev->devfn == PCI_DEVFN(kdata.provider_slot, kdata.provider_func)) {
+			provider = dev;
+		} else if (dev->bus->number == kdata.client_bus &&
+			   dev->devfn == PCI_DEVFN(kdata.client_slot, kdata.client_func)) {
+			client = dev;
+		} else {
+			continue;
+		}
+	}
+
+	if (!provider || !client) {
+		pr_err("%s fail to get provider or client.\n", __func__);
+		return -EINVAL;
+	}
+
+	kdata.distance = pci_p2pdma_distance(provider, &client->dev, false);
+
+	if (copy_to_user(udata, &kdata, sizeof(kdata)))
+		return -EFAULT;
+
+	return 0;
+}
+
 #ifdef CONFIG_XEN_PRIVCMD_EVENTFD
 /* Irqfd support */
 static struct workqueue_struct *irqfd_cleanup_wq;
@@ -1684,6 +1723,9 @@ static long privcmd_ioctl(struct file *file,
 		ret = privcmd_ioctl_map_hva_to_gpfns(file, udata);
 		break;
 
+	case IOCTL_PRIVCMD_P2PDMA_DISTANCE:
+		ret = privcmd_ioctl_p2pdma_distance(file, udata);
+		break;
 
 	default:
 		break;
include/uapi/xen/privcmd.h +12 -0
index d131002dd48f..a7ec3704519f 100644
@@ -141,6 +141,16 @@ struct privcmd_map_hva_to_gpfns {
 	int add_mapping;
 };
 
+struct privcmd_p2pdma_distance {
+	__u32 provider_bus;
+	__u32 provider_slot;
+	__u32 provider_func;
+	__u32 client_bus;
+	__u32 client_slot;
+	__u32 client_func;
+	__u32 distance;
+};
+
 /*
  * @cmd: IOCTL_PRIVCMD_HYPERCALL
  * @arg: &privcmd_hypercall_t
@@ -174,6 +184,8 @@ struct privcmd_map_hva_to_gpfns {
 	_IOW('P', 9, struct privcmd_ioeventfd)
 #define IOCTL_PRIVCMD_PCIDEV_GET_GSI				\
 	_IOC(_IOC_NONE, 'P', 10, sizeof(struct privcmd_pcidev_get_gsi))
+#define IOCTL_PRIVCMD_P2PDMA_DISTANCE                          \
+	_IOC(_IOC_NONE, 'P', 11, sizeof(struct privcmd_p2pdma_distance))
 #define IOCTL_PRIVCMD_MAP_HVA_TO_GPFNS                          \
 	_IOC(_IOC_NONE, 'P', 13, sizeof(struct privcmd_map_hva_to_gpfns))
 
Signature · 1 line
2.34.1
#2 lore raw

PATCH 2/3 virtgpu: get p2pdma_distance

In reply to Julia Zhang

virtgpu driver need to get the p2pdma_distance to decide if virtio iGPU buffer can be imported by passthrough dGPU, which is the main step implement dGPU prime feature.

To get the p2pdma_distance, this create a new virtgpu ioctl to send command of getting p2pdma_distance and pci notations of two pci devices from guest to host. Host will handle ioctl to calculate p2pdma_distance for this two pci devices on the host with pci notations sent from guest virtgpu driver.

---
 drivers/gpu/drm/virtio/virtgpu_drv.h   | 17 +++++++++
 drivers/gpu/drm/virtio/virtgpu_prime.c | 45 ++++++++++++++++++++++
 drivers/gpu/drm/virtio/virtgpu_vq.c    | 53 ++++++++++++++++++++++++++
 include/uapi/linux/virtio_gpu.h        | 19 +++++++++
 4 files changed, 134 insertions(+)
drivers/gpu/drm/virtio/virtgpu_drv.h +17 -0
index 64c236169db8..1bbe510c8410 100644
@@ -87,6 +87,18 @@ struct virtio_gpu_object_params {
 	uint64_t blob_id;
 };
 
+struct virtio_gpu_pci_notation {
+        uint32_t bus;
+        uint32_t slot;
+        uint32_t func;
+};
+
+struct virtio_gpu_p2pdma_distance {
+        atomic_t p2pdma_state;
+        uint32_t distance;
+};
+
+
 struct virtio_gpu_object {
 	struct drm_gem_shmem_object base;
 	uint32_t hw_res_handle;
@@ -488,4 +500,9 @@ void virtio_gpu_vram_unmap_dma_buf(struct device *dev,
 int virtio_gpu_execbuffer_ioctl(struct drm_device *dev, void *data,
 				struct drm_file *file);
 
+int virtio_gpu_cmd_p2pdma_distance(struct virtio_gpu_device *vgdev,
+                                   struct virtio_gpu_pci_notation *provider_params,
+                                   struct virtio_gpu_pci_notation *client_params,
+                                   struct virtio_gpu_p2pdma_distance *p2pdma_dist);
+
 #endif
drivers/gpu/drm/virtio/virtgpu_prime.c +45 -0

57 lines not on this page · raw message

drivers/gpu/drm/virtio/virtgpu_vq.c +53 -0

65 lines not on this page · raw message

include/uapi/linux/virtio_gpu.h +19 -0
index bf2c9cabd207..18313cffaa62 100644
@@ -95,6 +95,7 @@ enum virtio_gpu_ctrl_type {
 	VIRTIO_GPU_CMD_SUBMIT_3D,
 	VIRTIO_GPU_CMD_RESOURCE_MAP_BLOB,
 	VIRTIO_GPU_CMD_RESOURCE_UNMAP_BLOB,
+	VIRTIO_GPU_CMD_P2PDMA_DISTANCE,
 
 	/* cursor commands */
 	VIRTIO_GPU_CMD_UPDATE_CURSOR = 0x0300,
@@ -108,6 +109,7 @@ enum virtio_gpu_ctrl_type {
 	VIRTIO_GPU_RESP_OK_EDID,
 	VIRTIO_GPU_RESP_OK_RESOURCE_UUID,
 	VIRTIO_GPU_RESP_OK_MAP_INFO,
+	VIRTIO_GPU_RESP_OK_P2PDMA_DISTANCE,
 
 	/* error responses */
 	VIRTIO_GPU_RESP_ERR_UNSPEC = 0x1200,
@@ -429,6 +431,23 @@ struct virtio_gpu_set_scanout_blob {
 	__le32 offsets[4];
 };
 
+/* VIRTIO_GPU_CMD_P2PDMA_DISTANCE */
+struct virtio_gpu_device_p2pdma_distance {
+	struct virtio_gpu_ctrl_hdr hdr;
+	__le32 provider_bus;
+	__le32 provider_slot;
+	__le32 provider_func;
+	__le32 client_bus;
+	__le32 client_slot;
+	__le32 client_func;
+};
+
+/* VIRTIO_GPU_RESP_DISTANCE */
+struct virtio_gpu_resp_distance {
+	struct virtio_gpu_ctrl_hdr hdr;
+	__le32 distance;
+};
+
 /* VIRTIO_GPU_CMD_RESOURCE_MAP_BLOB */
 struct virtio_gpu_resource_map_blob {
 	struct virtio_gpu_ctrl_hdr hdr;
Signature · 1 line
2.34.1
#3 lore raw

PATCH 3/3 drm/virtio: Implement device_attach

In reply to Julia Zhang

As vram objects may be imported by other gpu drivers, peer2peer flag should be checked in dma_buf_ops->attach(). This reimplement virtio gpu dma_buf_ops->attach by adding a device_attach() function for virtio gpu. This function will get pci_p2pdma_distance and check attach->peer2peer before calling drm_gem_map_attach().

---
 drivers/gpu/drm/virtio/virtgpu_prime.c | 14 +++++++++++++-
 1 file changed, 13 insertions(+), 1 deletion(-)
drivers/gpu/drm/virtio/virtgpu_prime.c +13 -1
index 4960620eba02..4f6bce79e10e 100644
@@ -116,6 +116,18 @@ static int virtgpu_get_p2pdma_distance(struct dma_buf *dma_buf,
 	return 0;
 }
 
+static int virtgpu_gem_device_attach(struct dma_buf *dma_buf,
+				     struct dma_buf_attachment *attach)
+{
+	int ret = virtgpu_get_p2pdma_distance(dma_buf, attach);
+	if (ret)
+		return ret;
+	if (!attach->peer2peer)
+		return -EBUSY;
+
+	return drm_gem_map_attach(dma_buf, attach);
+}
+
 static const struct virtio_dma_buf_ops virtgpu_dmabuf_ops =  {
 	.ops = {
 		.cache_sgt_mapping = true,
@@ -128,7 +140,7 @@ static const struct virtio_dma_buf_ops virtgpu_dmabuf_ops =  {
 		.vmap = drm_gem_dmabuf_vmap,
 		.vunmap = drm_gem_dmabuf_vunmap,
 	},
-	.device_attach = drm_gem_map_attach,
+	.device_attach = virtgpu_gem_device_attach,
 	.get_uuid = virtgpu_virtio_get_uuid,
 };
 
Signature · 1 line
2.34.1
Acked-by on v1 3/3
#4 lore raw

Re: [PATCH 3/3] drm/virtio: Implement device_attach

Am 07.12.24 um 11:50 schrieb Julia Zhang:

Quoted 7 lines

As vram objects may be imported by other gpu drivers, peer2peer flag should be checked in dma_buf_ops->attach(). This reimplement virtio gpu dma_buf_ops->attach by adding a device_attach() function for virtio gpu. This function will get pci_p2pdma_distance and check attach->peer2peer before calling drm_gem_map_attach().

I can't judge that virtgpu or XEN code path, but that here looks like it should work.

Quoted 36 lines
---
  drivers/gpu/drm/virtio/virtgpu_prime.c | 14 +++++++++++++-
  1 file changed, 13 insertions(+), 1 deletion(-)
drivers/gpu/drm/virtio/virtgpu_prime.c +13 -1
index 4960620eba02..4f6bce79e10e 100644
@@ -116,6 +116,18 @@ static int virtgpu_get_p2pdma_distance(struct dma_buf *dma_buf,
  	return 0;
  }
 
+static int virtgpu_gem_device_attach(struct dma_buf *dma_buf,
+				     struct dma_buf_attachment *attach)
+{
+	int ret = virtgpu_get_p2pdma_distance(dma_buf, attach);
+	if (ret)
+		return ret;
+	if (!attach->peer2peer)
+		return -EBUSY;
+
+	return drm_gem_map_attach(dma_buf, attach);
+}
+
  static const struct virtio_dma_buf_ops virtgpu_dmabuf_ops =  {
  	.ops = {
  		.cache_sgt_mapping = true,
@@ -128,7 +140,7 @@ static const struct virtio_dma_buf_ops virtgpu_dmabuf_ops =  {
  		.vmap = drm_gem_dmabuf_vmap,
  		.vunmap = drm_gem_dmabuf_vunmap,
  	},
-	.device_attach = drm_gem_map_attach,
+	.device_attach = virtgpu_gem_device_attach,
  	.get_uuid = virtgpu_virtio_get_uuid,
  };
 
maintainer
#5 lore raw
In reply to Julia Zhang
Quoted 10 lines · Julia Zhang

To implement dGPU prime feature, virtgpu needs to import/export buffer between virtio iGPU and passthrough dGPU. Before that, virtgpu should check if P2P is possible or not. But calling function pci_p2pdma_distance in guest VM will only get virtual p2pdma_distance instead of real physical p2pdma_distance.

So this series introduce an implementation of virtgpu device_attach callback to get p2pdma_distance. And also adds a new virtgpu command to pass PCI notations from guest to host and a new xen privcmd to get physical p2pdma_distance according to the PCI notations in host.

It is hard to review this series without having a clear picture how this all is coming together.

I guess the virtgpu frontend will send a p2pdma_distance request to the backend, which is running in user mode of dom0. This backend will then call into the privcmd driver to obtain the needed information and sends it back to the frontend.

Can you please confirm my suspicion is correct?

Juergen

#6 lore raw
Quoted 17 lines · Juergen Gross
Quoted 4 lines · Julia Zhang

To implement dGPU prime feature, virtgpu needs to import/export buffer between virtio iGPU and passthrough dGPU. Before that, virtgpu should check if P2P is possible or not. But calling function pci_p2pdma_distance

Quoted 8 lines

in guest VM will only get virtual p2pdma_distance instead of real physical
p2pdma_distance.
  So this series introduce an implementation of virtgpu device_attach callback to get p2pdma_distance. And also adds a new virtgpu command to pass PCI notations from guest to host and a new xen privcmd to get physical
p2pdma_distance according to the PCI notations in host.

It is hard to review this series without having a clear picture how this all is coming together.

Quoted 6 lines

I guess the virtgpu frontend will send a p2pdma_distance request to the backend, which is running in user mode of dom0. This backend will then call into the privcmd driver to obtain the needed information and sends it back to the frontend.

Can you please confirm my suspicion is correct?

Yes, you are right.

Julia

Quoted 3 lines

Juergen