Fetch VFIO specific dma-buf data to see if the dma-buf is eligible to be assigned to CoCo VM as private MMIO. KVM expects host unaccessible dma-buf for private MMIO mapping. So need the exporter provide this information. VFIO also provides the struct kvm *kvm handler for KVM to check if the owner device of the MMIO region is already assigned to the same CoCo VM. Signed-off-by: Xu Yilun <yilun.xu@xxxxxxxxxxxxxxx> --- virt/kvm/vfio_dmabuf.c | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/virt/kvm/vfio_dmabuf.c b/virt/kvm/vfio_dmabuf.c index c427ab39c68a..26e01b815ebf 100644 --- a/virt/kvm/vfio_dmabuf.c +++ b/virt/kvm/vfio_dmabuf.c @@ -12,6 +12,22 @@ struct kvm_vfio_dmabuf { struct kvm_memory_slot *slot; }; +static struct vfio_dma_buf_data *kvm_vfio_dma_buf_get_data(struct dma_buf *dmabuf) +{ + struct vfio_dma_buf_data *(*fn)(struct dma_buf *dmabuf); + struct vfio_dma_buf_data *ret; + + fn = symbol_get(vfio_dma_buf_get_data); + if (!fn) + return ERR_PTR(-ENOENT); + + ret = fn(dmabuf); + + symbol_put(vfio_dma_buf_get_data); + + return ret; +} + static void kv_dmabuf_move_notify(struct dma_buf_attachment *attach) { struct kvm_vfio_dmabuf *kv_dmabuf = attach->importer_priv; @@ -48,6 +64,7 @@ int kvm_vfio_dmabuf_bind(struct kvm *kvm, struct kvm_memory_slot *slot, size_t size = slot->npages << PAGE_SHIFT; struct dma_buf_attachment *attach; struct kvm_vfio_dmabuf *kv_dmabuf; + struct vfio_dma_buf_data *data; struct dma_buf *dmabuf; int ret; @@ -60,6 +77,15 @@ int kvm_vfio_dmabuf_bind(struct kvm *kvm, struct kvm_memory_slot *slot, goto err_dmabuf; } + data = kvm_vfio_dma_buf_get_data(dmabuf); + if (IS_ERR(data)) + goto err_dmabuf; + + if (!data->is_private || data->kvm != kvm) { + ret = -EINVAL; + goto err_dmabuf; + } + kv_dmabuf = kzalloc(sizeof(*kv_dmabuf), GFP_KERNEL); if (!kv_dmabuf) { ret = -ENOMEM; -- 2.25.1