A VMID is generated in an IOMMU driver, being called from this ->attach_group() callback. So call ->get_vmid() right after it creates a new VMID, and call ->set_vmid() before it, to let it reuse the same VMID. Signed-off-by: Nicolin Chen <nicolinc@xxxxxxxxxx> --- drivers/vfio/vfio.c | 12 ++++++++++++ include/linux/vfio.h | 2 ++ 2 files changed, 14 insertions(+) diff --git a/drivers/vfio/vfio.c b/drivers/vfio/vfio.c index c17b25c127a2..8b7442deca93 100644 --- a/drivers/vfio/vfio.c +++ b/drivers/vfio/vfio.c @@ -1080,9 +1080,21 @@ static int __vfio_container_attach_groups(struct vfio_container *container, int ret = -ENODEV; list_for_each_entry(group, &container->group_list, container_next) { + if (driver->ops->set_vmid && container->vmid != VFIO_IOMMU_VMID_INVALID) { + ret = driver->ops->set_vmid(data, container->vmid); + if (ret) + goto unwind; + } + ret = driver->ops->attach_group(data, group->iommu_group); if (ret) goto unwind; + + if (driver->ops->get_vmid && container->vmid == VFIO_IOMMU_VMID_INVALID) { + ret = driver->ops->get_vmid(data, &container->vmid); + if (ret) + goto unwind; + } } return ret; diff --git a/include/linux/vfio.h b/include/linux/vfio.h index b53a9557884a..b43e7cbef4ab 100644 --- a/include/linux/vfio.h +++ b/include/linux/vfio.h @@ -126,6 +126,8 @@ struct vfio_iommu_driver_ops { struct iommu_group *group); void (*notify)(void *iommu_data, enum vfio_iommu_notify_type event); + int (*set_vmid)(void *iommu_data, u32 vmid); + int (*get_vmid)(void *iommu_data, u32 *vmid); }; extern int vfio_register_iommu_driver(const struct vfio_iommu_driver_ops *ops); -- 2.17.1