[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-Id: <20250529053513.1592088-11-yilun.xu@linux.intel.com>
Date: Thu, 29 May 2025 13:34:53 +0800
From: Xu Yilun <yilun.xu@...ux.intel.com>
To: kvm@...r.kernel.org,
sumit.semwal@...aro.org,
christian.koenig@....com,
pbonzini@...hat.com,
seanjc@...gle.com,
alex.williamson@...hat.com,
jgg@...dia.com,
dan.j.williams@...el.com,
aik@....com,
linux-coco@...ts.linux.dev
Cc: dri-devel@...ts.freedesktop.org,
linux-media@...r.kernel.org,
linaro-mm-sig@...ts.linaro.org,
vivek.kasireddy@...el.com,
yilun.xu@...el.com,
yilun.xu@...ux.intel.com,
linux-kernel@...r.kernel.org,
lukas@...ner.de,
yan.y.zhao@...el.com,
daniel.vetter@...ll.ch,
leon@...nel.org,
baolu.lu@...ux.intel.com,
zhenzhong.duan@...el.com,
tao1.su@...el.com,
linux-pci@...r.kernel.org,
zhiw@...dia.com,
simona.vetter@...ll.ch,
shameerali.kolothum.thodi@...wei.com,
aneesh.kumar@...nel.org,
iommu@...ts.linux.dev,
kevin.tian@...el.com
Subject: [RFC PATCH 10/30] vfio/pci: Export vfio dma-buf specific info for importers
Export vfio dma-buf specific info by attaching vfio_dma_buf_data in
struct dma_buf::priv. Provide a helper vfio_dma_buf_get_data() for
importers to fetch these data. Exporters identify VFIO dma-buf by
successfully getting these data.
VFIO dma-buf supports disabling host access to these exported MMIO
regions when the device is converted to private. Exporters like KVM
need to identify this type of dma-buf to decide if it is good to use.
KVM only allows host unaccessible MMIO regions been mapped in private
roots.
Export struct kvm * handler attached to the vfio device. This
allows KVM to do another sanity check. MMIO should only be assigned to
a CoCo VM if its owner device is already assigned to the same VM.
Signed-off-by: Xu Yilun <yilun.xu@...ux.intel.com>
---
drivers/vfio/pci/vfio_pci_dmabuf.c | 18 ++++++++++++++++++
include/linux/vfio.h | 18 ++++++++++++++++++
2 files changed, 36 insertions(+)
diff --git a/drivers/vfio/pci/vfio_pci_dmabuf.c b/drivers/vfio/pci/vfio_pci_dmabuf.c
index cf9a90448856..4011545db3ad 100644
--- a/drivers/vfio/pci/vfio_pci_dmabuf.c
+++ b/drivers/vfio/pci/vfio_pci_dmabuf.c
@@ -10,6 +10,8 @@
MODULE_IMPORT_NS("DMA_BUF");
struct vfio_pci_dma_buf {
+ struct vfio_dma_buf_data export_data;
+
struct dma_buf *dmabuf;
struct vfio_pci_core_device *vdev;
struct list_head dmabufs_elm;
@@ -300,6 +302,8 @@ int vfio_pci_core_feature_dma_buf(struct vfio_pci_core_device *vdev, u32 flags,
priv->nr_ranges = get_dma_buf.nr_ranges;
priv->dma_ranges = dma_ranges;
+ priv->export_data.kvm = vdev->vdev.kvm;
+
ret = check_dma_ranges(priv, &dmabuf_size);
if (ret)
goto err_free_priv;
@@ -391,3 +395,17 @@ void vfio_pci_dma_buf_cleanup(struct vfio_pci_core_device *vdev)
}
up_write(&vdev->memory_lock);
}
+
+/*
+ * Only vfio/pci implements this, so put the helper here for now.
+ */
+struct vfio_dma_buf_data *vfio_dma_buf_get_data(struct dma_buf *dmabuf)
+{
+ struct vfio_pci_dma_buf *priv = dmabuf->priv;
+
+ if (dmabuf->ops != &vfio_pci_dmabuf_ops)
+ return ERR_PTR(-EINVAL);
+
+ return &priv->export_data;
+}
+EXPORT_SYMBOL_GPL(vfio_dma_buf_get_data);
diff --git a/include/linux/vfio.h b/include/linux/vfio.h
index ba65bbdffd0b..d521d2c01a92 100644
--- a/include/linux/vfio.h
+++ b/include/linux/vfio.h
@@ -9,6 +9,7 @@
#define VFIO_H
+#include <linux/dma-buf.h>
#include <linux/iommu.h>
#include <linux/mm.h>
#include <linux/workqueue.h>
@@ -383,4 +384,21 @@ int vfio_virqfd_enable(void *opaque, int (*handler)(void *, void *),
void vfio_virqfd_disable(struct virqfd **pvirqfd);
void vfio_virqfd_flush_thread(struct virqfd **pvirqfd);
+/*
+ * DMA-buf - generic
+ */
+struct vfio_dma_buf_data {
+ struct kvm *kvm;
+};
+
+#if IS_ENABLED(CONFIG_DMA_SHARED_BUFFER) && IS_ENABLED(CONFIG_VFIO_PCI_CORE)
+struct vfio_dma_buf_data *vfio_dma_buf_get_data(struct dma_buf *dmabuf);
+#else
+static inline
+struct vfio_dma_buf_data *vfio_dma_buf_get_data(struct dma_buf *dmabuf)
+{
+ return NULL;
+}
+#endif
+
#endif /* VFIO_H */
--
2.25.1
Powered by blists - more mailing lists