diff mbox series

[SRU,J:linux-bluefield,v2,7/7] UBUNTU: SAUCE: vfio/pci: add new ioctl for vfio dma buf

Message ID 20240906004038.800613-8-witu@nvidia.com
State New
Headers show
Series Add VFIO P2P support | expand

Commit Message

William Tu Sept. 6, 2024, 12:40 a.m. UTC
Introduce new ioctl number for VFIO P2P dmabuf, with ioctl input
struct vfio_device_p2p_dma_buf.

Signed-off-by: William Tu <witu@nvidia.com>
---
 drivers/vfio/pci/dma_buf.c       | 10 ++++------
 drivers/vfio/pci/vfio_pci_core.c | 12 ++++++++----
 drivers/vfio/pci/vfio_pci_priv.h | 10 ++++------
 include/uapi/linux/vfio.h        | 14 ++++++++++++--
 4 files changed, 28 insertions(+), 18 deletions(-)
diff mbox series

Patch

diff --git a/drivers/vfio/pci/dma_buf.c b/drivers/vfio/pci/dma_buf.c
index 367fe7077ebf..58fd3db9c249 100644
--- a/drivers/vfio/pci/dma_buf.c
+++ b/drivers/vfio/pci/dma_buf.c
@@ -161,17 +161,15 @@  static const struct dma_buf_ops vfio_pci_dmabuf_ops = {
 	.unmap_dma_buf = vfio_pci_dma_buf_unmap,
 };
 
-int vfio_pci_core_feature_dma_buf(struct vfio_pci_core_device *vdev, u32 flags,
-				  struct vfio_device_feature_dma_buf __user *arg,
-				  size_t argsz)
+int vfio_pci_core_feature_dma_buf(struct vfio_pci_core_device *vdev,
+				  struct vfio_device_p2p_dma_buf *p2p_dma_buf)
 {
-	struct vfio_device_feature_dma_buf get_dma_buf;
+	struct vfio_device_p2p_dma_buf get_dma_buf;
 	DEFINE_DMA_BUF_EXPORT_INFO(exp_info);
 	struct vfio_pci_dma_buf *priv;
 	int ret;
 
-	if (copy_from_user(&get_dma_buf, arg, sizeof(get_dma_buf)))
-		return -EFAULT;
+	memcpy(&get_dma_buf, p2p_dma_buf, sizeof(get_dma_buf));
 
 	/* For PCI the region_index is the BAR number like everything else */
 	if (get_dma_buf.region_index >= VFIO_PCI_ROM_REGION_INDEX)
diff --git a/drivers/vfio/pci/vfio_pci_core.c b/drivers/vfio/pci/vfio_pci_core.c
index 6c4c96fc5c54..096f8303a207 100644
--- a/drivers/vfio/pci/vfio_pci_core.c
+++ b/drivers/vfio/pci/vfio_pci_core.c
@@ -1195,13 +1195,17 @@  long vfio_pci_core_ioctl(struct vfio_device *core_vdev, unsigned int cmd,
 			mutex_unlock(&vdev->vf_token->lock);
 
 			return 0;
-		case VFIO_DEVICE_FEATURE_DMA_BUF:
-			return vfio_pci_core_feature_dma_buf(vdev, feature.flags,
-							     (void __user *)(arg + minsz),
-							     feature.argsz);
 		default:
 			return -ENOTTY;
 		}
+	} else if (cmd == VFIO_DEVICE_P2P_DMA_BUF) {
+		struct vfio_device_p2p_dma_buf p2p_dma_buf;
+
+		if (copy_from_user(&p2p_dma_buf, (void __user *)arg,
+				   sizeof(p2p_dma_buf)))
+			return -EFAULT;
+
+		return vfio_pci_core_feature_dma_buf(vdev, &p2p_dma_buf);
 	}
 
 	return -ENOTTY;
diff --git a/drivers/vfio/pci/vfio_pci_priv.h b/drivers/vfio/pci/vfio_pci_priv.h
index c1ea92c5ec1c..d60e9cb99140 100644
--- a/drivers/vfio/pci/vfio_pci_priv.h
+++ b/drivers/vfio/pci/vfio_pci_priv.h
@@ -5,16 +5,14 @@ 
 int vfio_pci_try_reset_function(struct vfio_pci_core_device *vdev);
 
 #ifdef CONFIG_DMA_SHARED_BUFFER
-int vfio_pci_core_feature_dma_buf(struct vfio_pci_core_device *vdev, u32 flags,
-				  struct vfio_device_feature_dma_buf __user *arg,
-				  size_t argsz);
+int vfio_pci_core_feature_dma_buf(struct vfio_pci_core_device *vdev,
+				  struct vfio_device_p2p_dma_buf *arg);
 void vfio_pci_dma_buf_cleanup(struct vfio_pci_core_device *vdev);
 void vfio_pci_dma_buf_move(struct vfio_pci_core_device *vdev, bool revoked);
 #else
 static int
-vfio_pci_core_feature_dma_buf(struct vfio_pci_core_device *vdev, u32 flags,
-			      struct vfio_device_feature_dma_buf __user *arg,
-			      size_t argsz)
+vfio_pci_core_feature_dma_buf(struct vfio_pci_core_device *vdev,
+			      struct vfio_device_p2p_dma_buf *arg)
 {
 	return -ENOTTY;
 }
diff --git a/include/uapi/linux/vfio.h b/include/uapi/linux/vfio.h
index ef716afc9dae..18f369426c13 100644
--- a/include/uapi/linux/vfio.h
+++ b/include/uapi/linux/vfio.h
@@ -1012,13 +1012,23 @@  struct vfio_device_feature {
  *
  * Return: The fd number on success, -1 and errno is set on failure.
  */
-struct vfio_device_feature_dma_buf {
+
+/**
+ * VFIO_DEVICE_P2P_DMA_BUF - _IORW(VFIO_TYPE, VFIO_BASE + 22,
+ *				   struct vfio_device_p2p_dma_buf)
+ *
+ * Set the region index, open flags, offset and length to create a dma_buf
+ * for p2p dma.
+ *
+ * Return 0 on success, -errno on failure.
+ */
+struct vfio_device_p2p_dma_buf {
 	__u32 region_index;
 	__u32 open_flags;
 	__u32 offset;
 	__u64 length;
 };
-#define VFIO_DEVICE_FEATURE_DMA_BUF 3 // FIXME numbers wrong
+#define VFIO_DEVICE_P2P_DMA_BUF	_IO(VFIO_TYPE, VFIO_BASE + 22)
 
 /* -------- API for Type1 VFIO IOMMU -------- */