Skip to contentSkip to conversation

Add vfio_device cdev for iommufd support

PATCH Superseded Yi Liu 5 participants 80 messages Jan 17, 2023 – Feb 8, 2023

Subsystems (4)

v1 of 15 Superseded Latest is v15 →

lore
Share & tools
lore

Yi Liu

author
#0 lore raw

cover Add vfio_device cdev for iommufd support

Existing VFIO provides group-centric user APIs for userspace. Userspace opens the /dev/vfio/$group_id first before getting device fd and hence getting access to device. This is not the desired model for iommufd. Per the conclusion of community discussion[1], iommufd provides device-centric kAPIs and requires its consumer (like VFIO) to be device-centric user APIs. Such user APIs are used to associate device with iommufd and also the I/O address spaces managed by the iommufd.

This series first introduces a per device file structure to be prepared for further enhancement and refactors the kvm-vfio code to be prepared for accepting device file from userspace. Then refactors the vfio to be able to handle iommufd binding. This refactor includes the mechanism of blocking device access before iommufd bind, making vfio_device_open() be exclusive between the group path and the cdev path. Eventually, adds the cdev support for vfio device, and makes group infrastructure optional as it is not needed when vfio device cdev is compiled.

This is also a base for further support iommu nesting for vfio device[2].

The complete code can be found in below branch, simple test done with the legacy group path and the cdev path. Draft QEMU branch can be found at[3]

https://github.com/yiliu1765/iommufd/tree/vfio_device_cdev_v1
(config CONFIG_IOMMUFD=y)

[1] https://lore.kernel.org/kvm/BN9PR11MB5433B1E4AE5B0480369F97178C189@BN9PR11MB5433.namprd11.prod.outlook.com/
[2] https://github.com/yiliu1765/iommufd/tree/wip/iommufd-v6.2-rc4-nesting
[3] https://github.com/yiliu1765/qemu/tree/wip/qemu-iommufd-6.2-rc4 (need further cleanup)

Change log:

v1:
- Fix the circular refcount between kvm struct and device file reference. (JasonG)
- Address comments from KevinT
- Remained the ioctl for detach, needs to Alex's taste
(https://lore.kernel.org/kvm/BN9PR11MB5276BE9F4B0613EE859317028CFF9@BN9PR11MB5276.namprd11.prod.outlook.com/)

rfc: https://lore.kernel.org/kvm/[email protected]/

Thanks,
Yi Liu

Yi Liu (13):
vfio: Allocate per device file structure
vfio: Refine vfio file kAPIs
vfio: Accept vfio device file in the driver facing kAPI kvm/vfio: Rename kvm_vfio_group to prepare for accepting vfio device
fd
kvm/vfio: Provide struct kvm_device_ops::release() insted of

    ::destroy()

kvm/vfio: Accept vfio device file from userspace
vfio: Pass struct vfio_device_file * to vfio_device_open/close()
vfio: Block device access via device fd until device is opened
vfio: Add infrastructure for bind_iommufd and attach
vfio: Make vfio_device_open() exclusive between group path and device
cdev path
vfio: Add cdev for vfio_device
vfio: Add ioctls for device cdev iommufd
vfio: Compile group optionally

 Documentation/virt/kvm/devices/vfio.rst |  32 +-
 drivers/vfio/Kconfig                    |  17 +
 drivers/vfio/Makefile                   |   3 +-
 drivers/vfio/group.c                    | 131 +++---
 drivers/vfio/iommufd.c                  |  79 +++-
 drivers/vfio/pci/vfio_pci_core.c        |   4 +-
 drivers/vfio/vfio.h                     | 109 ++++-
 drivers/vfio/vfio_main.c                | 506 ++++++++++++++++++++++--
 include/linux/vfio.h                    |  21 +-
 include/uapi/linux/iommufd.h            |   2 +
 include/uapi/linux/kvm.h                |  23 +-
 include/uapi/linux/vfio.h               |  64 +++
 virt/kvm/vfio.c                         | 145 +++----
 13 files changed, 909 insertions(+), 227 deletions(-)
Signature · 1 line
2.34.1
author
#1 lore raw

PATCH 1/13 vfio: Allocate per device file structure

This is preparation for adding vfio device cdev support. vfio device cdev requires:
1) a per device file memory to store the kvm pointer set by KVM. It will be propagated to vfio_device:kvm after the device cdev file is bound to an iommufd
2) a mechanism to block device access through device cdev fd before it is bound to an iommufd

To address above requirements, this adds a per device file structure named vfio_device_file. For now, it's only a wrapper of struct vfio_device pointer. Other fields will be added to this per file structure in future commits.

---
 drivers/vfio/group.c     | 13 +++++++++++--
 drivers/vfio/vfio.h      |  6 ++++++
 drivers/vfio/vfio_main.c | 31 ++++++++++++++++++++++++++-----
 3 files changed, 43 insertions(+), 7 deletions(-)
drivers/vfio/group.c +11 -2
index bb24b2f0271e..8fdb7e35b0a6 100644
@@ -186,19 +186,26 @@ void vfio_device_group_close(struct vfio_device *device)
 
 static struct file *vfio_device_open_file(struct vfio_device *device)
 {
+	struct vfio_device_file *df;
 	struct file *filep;
 	int ret;
 
+	df = vfio_allocate_device_file(device);
+	if (IS_ERR(df)) {
+		ret = PTR_ERR(df);
+		goto err_out;
+	}
+
 	ret = vfio_device_group_open(device);
 	if (ret)
-		goto err_out;
+		goto err_free;
 
 	/*
 	 * We can't use anon_inode_getfd() because we need to modify
 	 * the f_mode flags directly to allow more than just ioctls
 	 */
 	filep = anon_inode_getfile("[vfio-device]", &vfio_device_fops,
-				   device, O_RDWR);
+				   df, O_RDWR);
 	if (IS_ERR(filep)) {
 		ret = PTR_ERR(filep);
 		goto err_close_device;
@@ -222,6 +229,8 @@ static struct file *vfio_device_open_file(struct vfio_device *device)
 
 err_close_device:
 	vfio_device_group_close(device);
+err_free:
+	kfree(df);
 err_out:
 	return ERR_PTR(ret);
 }
drivers/vfio/vfio.h +6 -0
index f8219a438bfb..1091806bc89d 100644
@@ -16,12 +16,18 @@ struct iommu_group;
 struct vfio_device;
 struct vfio_container;
 
+struct vfio_device_file {
+	struct vfio_device *device;
+};
+
 void vfio_device_put_registration(struct vfio_device *device);
 bool vfio_device_try_get_registration(struct vfio_device *device);
 int vfio_device_open(struct vfio_device *device,
 		     struct iommufd_ctx *iommufd, struct kvm *kvm);
 void vfio_device_close(struct vfio_device *device,
 		       struct iommufd_ctx *iommufd);
+struct vfio_device_file *
+vfio_allocate_device_file(struct vfio_device *device);
 
 extern const struct file_operations vfio_device_fops;
 
drivers/vfio/vfio_main.c +26 -5
index 5177bb061b17..ee54c9ae0af4 100644
@@ -344,6 +344,20 @@ static bool vfio_assert_device_open(struct vfio_device *device)
 	return !WARN_ON_ONCE(!READ_ONCE(device->open_count));
 }
 
+struct vfio_device_file *
+vfio_allocate_device_file(struct vfio_device *device)
+{
+	struct vfio_device_file *df;
+
+	df = kzalloc(sizeof(*df), GFP_KERNEL_ACCOUNT);
+	if (!df)
+		return ERR_PTR(-ENOMEM);
+
+	df->device = device;
+
+	return df;
+}
+
 static int vfio_device_first_open(struct vfio_device *device,
 				  struct iommufd_ctx *iommufd, struct kvm *kvm)
 {
@@ -461,12 +475,15 @@ static inline void vfio_device_pm_runtime_put(struct vfio_device *device)
  */
 static int vfio_device_fops_release(struct inode *inode, struct file *filep)
 {
-	struct vfio_device *device = filep->private_data;
+	struct vfio_device_file *df = filep->private_data;
+	struct vfio_device *device = df->device;
 
 	vfio_device_group_close(device);
 
 	vfio_device_put_registration(device);
 
+	kfree(df);
+
 	return 0;
 }
 
@@ -1031,7 +1048,8 @@ static int vfio_ioctl_device_feature(struct vfio_device *device,
 static long vfio_device_fops_unl_ioctl(struct file *filep,
 				       unsigned int cmd, unsigned long arg)
 {
-	struct vfio_device *device = filep->private_data;
+	struct vfio_device_file *df = filep->private_data;
+	struct vfio_device *device = df->device;
 	int ret;
 
 	ret = vfio_device_pm_runtime_get(device);
@@ -1058,7 +1076,8 @@ static long vfio_device_fops_unl_ioctl(struct file *filep,
 static ssize_t vfio_device_fops_read(struct file *filep, char __user *buf,
 				     size_t count, loff_t *ppos)
 {
-	struct vfio_device *device = filep->private_data;
+	struct vfio_device_file *df = filep->private_data;
+	struct vfio_device *device = df->device;
 
 	if (unlikely(!device->ops->read))
 		return -EINVAL;
@@ -1070,7 +1089,8 @@ static ssize_t vfio_device_fops_write(struct file *filep,
 				      const char __user *buf,
 				      size_t count, loff_t *ppos)
 {
-	struct vfio_device *device = filep->private_data;
+	struct vfio_device_file *df = filep->private_data;
+	struct vfio_device *device = df->device;
 
 	if (unlikely(!device->ops->write))
 		return -EINVAL;
@@ -1080,7 +1100,8 @@ static ssize_t vfio_device_fops_write(struct file *filep,
 
 static int vfio_device_fops_mmap(struct file *filep, struct vm_area_struct *vma)
 {
-	struct vfio_device *device = filep->private_data;
+	struct vfio_device_file *df = filep->private_data;
+	struct vfio_device *device = df->device;
 
 	if (unlikely(!device->ops->mmap))
 		return -EINVAL;
Signature · 1 line
2.34.1
maintainer Reviewed-by on v1 1/13
#2 lore raw

RE: [PATCH 01/13] vfio: Allocate per device file structure

Quoted 11 lines

From: Liu, Yi L <[email protected]>
Sent: Tuesday, January 17, 2023 9:50 PM

This is preparation for adding vfio device cdev support. vfio device cdev requires:
1) a per device file memory to store the kvm pointer set by KVM. It will be propagated to vfio_device:kvm after the device cdev file is bound to an iommufd
2) a mechanism to block device access through device cdev fd before it is bound to an iommufd

Quoted 6 lines

To address above requirements, this adds a per device file structure named vfio_device_file. For now, it's only a wrapper of struct vfio_device pointer. Other fields will be added to this per file structure in future commits.

Reviewed-by on v1 1/13
#3 lore raw

Re: [PATCH 01/13] vfio: Allocate per device file structure

In reply to Yi Liu

Hi Yi,

Quoted 158 lines · Yi Liu

This is preparation for adding vfio device cdev support. vfio device cdev requires:
1) a per device file memory to store the kvm pointer set by KVM. It will be propagated to vfio_device:kvm after the device cdev file is bound to an iommufd
2) a mechanism to block device access through device cdev fd before it is bound to an iommufd

To address above requirements, this adds a per device file structure named vfio_device_file. For now, it's only a wrapper of struct vfio_device pointer. Other fields will be added to this per file structure in future commits.

---
 drivers/vfio/group.c     | 13 +++++++++++--
 drivers/vfio/vfio.h      |  6 ++++++
 drivers/vfio/vfio_main.c | 31 ++++++++++++++++++++++++++-----
 3 files changed, 43 insertions(+), 7 deletions(-)
drivers/vfio/group.c +11 -2
index bb24b2f0271e..8fdb7e35b0a6 100644
@@ -186,19 +186,26 @@ void vfio_device_group_close(struct vfio_device *device)
 
 static struct file *vfio_device_open_file(struct vfio_device *device)
 {
+	struct vfio_device_file *df;
 	struct file *filep;
 	int ret;
 
+	df = vfio_allocate_device_file(device);
+	if (IS_ERR(df)) {
+		ret = PTR_ERR(df);
+		goto err_out;
+	}
+
 	ret = vfio_device_group_open(device);
 	if (ret)
-		goto err_out;
+		goto err_free;
 
 	/*
 	 * We can't use anon_inode_getfd() because we need to modify
 	 * the f_mode flags directly to allow more than just ioctls
 	 */
 	filep = anon_inode_getfile("[vfio-device]", &vfio_device_fops,
-				   device, O_RDWR);
+				   df, O_RDWR);
 	if (IS_ERR(filep)) {
 		ret = PTR_ERR(filep);
 		goto err_close_device;
@@ -222,6 +229,8 @@ static struct file *vfio_device_open_file(struct vfio_device *device)
 
 err_close_device:
 	vfio_device_group_close(device);
+err_free:
+	kfree(df);
 err_out:
 	return ERR_PTR(ret);
 }
drivers/vfio/vfio.h +6 -0
index f8219a438bfb..1091806bc89d 100644
@@ -16,12 +16,18 @@ struct iommu_group;
 struct vfio_device;
 struct vfio_container;
 
+struct vfio_device_file {
+	struct vfio_device *device;
+};
+
 void vfio_device_put_registration(struct vfio_device *device);
 bool vfio_device_try_get_registration(struct vfio_device *device);
 int vfio_device_open(struct vfio_device *device,
 		     struct iommufd_ctx *iommufd, struct kvm *kvm);
 void vfio_device_close(struct vfio_device *device,
 		       struct iommufd_ctx *iommufd);
+struct vfio_device_file *
+vfio_allocate_device_file(struct vfio_device *device);
 
 extern const struct file_operations vfio_device_fops;
 
drivers/vfio/vfio_main.c +24 -4
index 5177bb061b17..ee54c9ae0af4 100644
@@ -344,6 +344,20 @@ static bool vfio_assert_device_open(struct vfio_device *device)
 	return !WARN_ON_ONCE(!READ_ONCE(device->open_count));
 }
 
+struct vfio_device_file *
+vfio_allocate_device_file(struct vfio_device *device)
+{
+	struct vfio_device_file *df;
+
+	df = kzalloc(sizeof(*df), GFP_KERNEL_ACCOUNT);
+	if (!df)
+		return ERR_PTR(-ENOMEM);
+
+	df->device = device;
+
+	return df;
+}
+
 static int vfio_device_first_open(struct vfio_device *device,
 				  struct iommufd_ctx *iommufd, struct kvm *kvm)
 {
@@ -461,12 +475,15 @@ static inline void vfio_device_pm_runtime_put(struct vfio_device *device)
  */
 static int vfio_device_fops_release(struct inode *inode, struct file *filep)
 {
-	struct vfio_device *device = filep->private_data;
+	struct vfio_device_file *df = filep->private_data;
+	struct vfio_device *device = df->device;
 
 	vfio_device_group_close(device);
 
 	vfio_device_put_registration(device);
 
+	kfree(df);
+
 	return 0;
 }
 
@@ -1031,7 +1048,8 @@ static int vfio_ioctl_device_feature(struct vfio_device *device,
 static long vfio_device_fops_unl_ioctl(struct file *filep,
 				       unsigned int cmd, unsigned long arg)
 {
-	struct vfio_device *device = filep->private_data;
+	struct vfio_device_file *df = filep->private_data;
+	struct vfio_device *device = df->device;
 	int ret;
 
 	ret = vfio_device_pm_runtime_get(device);
@@ -1058,7 +1076,8 @@ static long vfio_device_fops_unl_ioctl(struct file *filep,
 static ssize_t vfio_device_fops_read(struct file *filep, char __user *buf,
 				     size_t count, loff_t *ppos)
 {
-	struct vfio_device *device = filep->private_data;
+	struct vfio_device_file *df = filep->private_data;
+	struct vfio_device *device = df->device;
 
 	if (unlikely(!device->ops->read))
 		return -EINVAL;
@@ -1070,7 +1089,8 @@ static ssize_t vfio_device_fops_write(struct file *filep,
 				      const char __user *buf,
 				      size_t count, loff_t *ppos)
 {
-	struct vfio_device *device = filep->private_data;
+	struct vfio_device_file *df = filep->private_data;
+	struct vfio_device *device = df->device;
 
 	if (unlikely(!device->ops->write))
 		return -EINVAL;

drivers/vfio/vfio_main.c static ssize_t vfio_device_fops_write(struct file *filep, · line 1107

Quoted 9 lines
drivers/vfio/vfio_main.c +2 -1
@@ -1080,7 +1100,8 @@ static ssize_t vfio_device_fops_write(struct file *filep,
 
 static int vfio_device_fops_mmap(struct file *filep, struct vm_area_struct *vma)
 {
-	struct vfio_device *device = filep->private_data;
+	struct vfio_device_file *df = filep->private_data;
+	struct vfio_device *device = df->device;
 
 	if (unlikely(!device->ops->mmap))
 		return -EINVAL;

Thanks

Eric

author
#4 lore raw

PATCH 2/13 vfio: Refine vfio file kAPIs

In reply to Yi Liu

This prepares for making the below kAPIs to accept both group file and device file instead of only vfio group file.

  bool vfio_file_enforced_coherent(struct file *file);
  void vfio_file_set_kvm(struct file *file, struct kvm *kvm);
  bool vfio_file_has_dev(struct file *file, struct vfio_device *device);

Besides above change, vfio_file_is_group() is renamed to be vfio_file_is_valid().

---
 drivers/vfio/group.c             | 74 ++++++++------------------------
 drivers/vfio/pci/vfio_pci_core.c |  4 +-
 drivers/vfio/vfio.h              |  4 ++
 drivers/vfio/vfio_main.c         | 62 ++++++++++++++++++++++++++
 include/linux/vfio.h             |  2 +-
 virt/kvm/vfio.c                  | 10 ++---
 6 files changed, 92 insertions(+), 64 deletions(-)
drivers/vfio/group.c +18 -56
index 8fdb7e35b0a6..d83cf069d290 100644
@@ -721,6 +721,15 @@ bool vfio_device_has_container(struct vfio_device *device)
 	return device->group->container;
 }
 
+struct vfio_group *vfio_group_from_file(struct file *file)
+{
+	struct vfio_group *group = file->private_data;
+
+	if (file->f_op != &vfio_group_fops)
+		return NULL;
+	return group;
+}
+
 /**
  * vfio_file_iommu_group - Return the struct iommu_group for the vfio group file
  * @file: VFIO group file
@@ -731,13 +740,13 @@ bool vfio_device_has_container(struct vfio_device *device)
  */
 struct iommu_group *vfio_file_iommu_group(struct file *file)
 {
-	struct vfio_group *group = file->private_data;
+	struct vfio_group *group = vfio_group_from_file(file);
 	struct iommu_group *iommu_group = NULL;
 
 	if (!IS_ENABLED(CONFIG_SPAPR_TCE_IOMMU))
 		return NULL;
 
-	if (!vfio_file_is_group(file))
+	if (!group)
 		return NULL;
 
 	mutex_lock(&group->group_lock);
@@ -750,34 +759,11 @@ struct iommu_group *vfio_file_iommu_group(struct file *file)
 }
 EXPORT_SYMBOL_GPL(vfio_file_iommu_group);
 
-/**
- * vfio_file_is_group - True if the file is usable with VFIO aPIS
- * @file: VFIO group file
- */
-bool vfio_file_is_group(struct file *file)
-{
-	return file->f_op == &vfio_group_fops;
-}
-EXPORT_SYMBOL_GPL(vfio_file_is_group);
-
-/**
- * vfio_file_enforced_coherent - True if the DMA associated with the VFIO file
- *        is always CPU cache coherent
- * @file: VFIO group file
- *
- * Enforced coherency means that the IOMMU ignores things like the PCIe no-snoop
- * bit in DMA transactions. A return of false indicates that the user has
- * rights to access additional instructions such as wbinvd on x86.
- */
-bool vfio_file_enforced_coherent(struct file *file)
+bool vfio_group_enforced_coherent(struct vfio_group *group)
 {
-	struct vfio_group *group = file->private_data;
 	struct vfio_device *device;
 	bool ret = true;
 
-	if (!vfio_file_is_group(file))
-		return true;
-
 	/*
 	 * If the device does not have IOMMU_CAP_ENFORCE_CACHE_COHERENCY then
 	 * any domain later attached to it will also not support it. If the cap
@@ -795,46 +781,22 @@ bool vfio_file_enforced_coherent(struct file *file)
 	mutex_unlock(&group->device_lock);
 	return ret;
 }
-EXPORT_SYMBOL_GPL(vfio_file_enforced_coherent);
 
-/**
- * vfio_file_set_kvm - Link a kvm with VFIO drivers
- * @file: VFIO group file
- * @kvm: KVM to link
- *
- * When a VFIO device is first opened the KVM will be available in
- * device->kvm if one was associated with the group.
- */
-void vfio_file_set_kvm(struct file *file, struct kvm *kvm)
+void vfio_group_set_kvm(struct vfio_group *group, struct kvm *kvm)
 {
-	struct vfio_group *group = file->private_data;
-
-	if (!vfio_file_is_group(file))
-		return;
-
+	/*
+	 * When a VFIO device is first opened the KVM will be available in
+	 * device->kvm if one was associated with the group.
+	 */
 	mutex_lock(&group->group_lock);
 	group->kvm = kvm;
 	mutex_unlock(&group->group_lock);
 }
-EXPORT_SYMBOL_GPL(vfio_file_set_kvm);
 
-/**
- * vfio_file_has_dev - True if the VFIO file is a handle for device
- * @file: VFIO file to check
- * @device: Device that must be part of the file
- *
- * Returns true if given file has permission to manipulate the given device.
- */
-bool vfio_file_has_dev(struct file *file, struct vfio_device *device)
+bool vfio_group_has_dev(struct vfio_group *group, struct vfio_device *device)
 {
-	struct vfio_group *group = file->private_data;
-
-	if (!vfio_file_is_group(file))
-		return false;
-
 	return group == device->group;
 }
-EXPORT_SYMBOL_GPL(vfio_file_has_dev);
 
 static char *vfio_devnode(const struct device *dev, umode_t *mode)
 {
drivers/vfio/pci/vfio_pci_core.c +2 -2
index 26a541cc64d1..985c6184a587 100644
@@ -1319,8 +1319,8 @@ static int vfio_pci_ioctl_pci_hot_reset(struct vfio_pci_core_device *vdev,
 			break;
 		}
 
-		/* Ensure the FD is a vfio group FD.*/
-		if (!vfio_file_is_group(file)) {
+		/* Ensure the FD is a vfio FD.*/
+		if (!vfio_file_is_valid(file)) {
 			fput(file);
 			ret = -EINVAL;
 			break;
drivers/vfio/vfio.h +4 -0
index 1091806bc89d..ef5de2872983 100644
@@ -90,6 +90,10 @@ void vfio_device_group_unregister(struct vfio_device *device);
 int vfio_device_group_use_iommu(struct vfio_device *device);
 void vfio_device_group_unuse_iommu(struct vfio_device *device);
 void vfio_device_group_close(struct vfio_device *device);
+struct vfio_group *vfio_group_from_file(struct file *file);
+bool vfio_group_enforced_coherent(struct vfio_group *group);
+void vfio_group_set_kvm(struct vfio_group *group, struct kvm *kvm);
+bool vfio_group_has_dev(struct vfio_group *group, struct vfio_device *device);
 bool vfio_device_has_container(struct vfio_device *device);
 int __init vfio_group_init(void);
 void vfio_group_cleanup(void);
drivers/vfio/vfio_main.c +62 -0

68 lines not on this page · raw message

include/linux/vfio.h +1 -1
index 35be78e9ae57..46edd6e6c0ba 100644
@@ -241,7 +241,7 @@ int vfio_mig_get_next_state(struct vfio_device *device,
  * External user API
  */
 struct iommu_group *vfio_file_iommu_group(struct file *file);
-bool vfio_file_is_group(struct file *file);
+bool vfio_file_is_valid(struct file *file);
 bool vfio_file_enforced_coherent(struct file *file);
 void vfio_file_set_kvm(struct file *file, struct kvm *kvm);
 bool vfio_file_has_dev(struct file *file, struct vfio_device *device);
virt/kvm/vfio.c +5 -5
index 495ceabffe88..868930c7a59b 100644
@@ -64,18 +64,18 @@ static bool kvm_vfio_file_enforced_coherent(struct file *file)
 	return ret;
 }
 
-static bool kvm_vfio_file_is_group(struct file *file)
+static bool kvm_vfio_file_is_valid(struct file *file)
 {
 	bool (*fn)(struct file *file);
 	bool ret;
 
-	fn = symbol_get(vfio_file_is_group);
+	fn = symbol_get(vfio_file_is_valid);
 	if (!fn)
 		return false;
 
 	ret = fn(file);
 
-	symbol_put(vfio_file_is_group);
+	symbol_put(vfio_file_is_valid);
 
 	return ret;
 }
@@ -154,8 +154,8 @@ static int kvm_vfio_group_add(struct kvm_device *dev, unsigned int fd)
 	if (!filp)
 		return -EBADF;
 
-	/* Ensure the FD is a vfio group FD.*/
-	if (!kvm_vfio_file_is_group(filp)) {
+	/* Ensure the FD is a vfio FD.*/
+	if (!kvm_vfio_file_is_valid(filp)) {
 		ret = -EINVAL;
 		goto err_fput;
 	}
Signature · 1 line
2.34.1
maintainer Reviewed-by on v1 2/13
#5 lore raw

RE: [PATCH 02/13] vfio: Refine vfio file kAPIs

Quoted 5 lines

From: Liu, Yi L <[email protected]>
Sent: Tuesday, January 17, 2023 9:50 PM

+/**
+ * vfio_file_is_valid - True if the file is usable with VFIO aPIS

s/aPIS/API/

Quoted 6 lines
drivers/vfio/vfio_main.c +3 -0
+
+/**
+ * vfio_file_enforced_coherent - True if the DMA associated with the VFIO

file
+ * is always CPU cache coherent
+ * @file: VFIO group or device file

to on par with other places, "VFIO group file or device file"

Reviewed-by on v1 2/13
#6 lore raw

Re: [PATCH 02/13] vfio: Refine vfio file kAPIs

In reply to Yi Liu

Hi Yi,

Quoted 179 lines · Yi Liu

This prepares for making the below kAPIs to accept both group file and device file instead of only vfio group file.
bool vfio_file_enforced_coherent(struct file *file);
void vfio_file_set_kvm(struct file *file, struct kvm *kvm); bool vfio_file_has_dev(struct file *file, struct vfio_device *device);

Besides above change, vfio_file_is_group() is renamed to be vfio_file_is_valid().

---
 drivers/vfio/group.c             | 74 ++++++++------------------------
 drivers/vfio/pci/vfio_pci_core.c |  4 +-
 drivers/vfio/vfio.h              |  4 ++
 drivers/vfio/vfio_main.c         | 62 ++++++++++++++++++++++++++
 include/linux/vfio.h             |  2 +-
 virt/kvm/vfio.c                  | 10 ++---
 6 files changed, 92 insertions(+), 64 deletions(-)
drivers/vfio/group.c +18 -56
index 8fdb7e35b0a6..d83cf069d290 100644
@@ -721,6 +721,15 @@ bool vfio_device_has_container(struct vfio_device *device)
 	return device->group->container;
 }
 
+struct vfio_group *vfio_group_from_file(struct file *file)
+{
+	struct vfio_group *group = file->private_data;
+
+	if (file->f_op != &vfio_group_fops)
+		return NULL;
+	return group;
+}
+
 /**
  * vfio_file_iommu_group - Return the struct iommu_group for the vfio group file
  * @file: VFIO group file
@@ -731,13 +740,13 @@ bool vfio_device_has_container(struct vfio_device *device)
  */
 struct iommu_group *vfio_file_iommu_group(struct file *file)
 {
-	struct vfio_group *group = file->private_data;
+	struct vfio_group *group = vfio_group_from_file(file);
 	struct iommu_group *iommu_group = NULL;
 
 	if (!IS_ENABLED(CONFIG_SPAPR_TCE_IOMMU))
 		return NULL;
 
-	if (!vfio_file_is_group(file))
+	if (!group)
 		return NULL;
 
 	mutex_lock(&group->group_lock);
@@ -750,34 +759,11 @@ struct iommu_group *vfio_file_iommu_group(struct file *file)
 }
 EXPORT_SYMBOL_GPL(vfio_file_iommu_group);
 
-/**
- * vfio_file_is_group - True if the file is usable with VFIO aPIS
- * @file: VFIO group file
- */
-bool vfio_file_is_group(struct file *file)
-{
-	return file->f_op == &vfio_group_fops;
-}
-EXPORT_SYMBOL_GPL(vfio_file_is_group);
-
-/**
- * vfio_file_enforced_coherent - True if the DMA associated with the VFIO file
- *        is always CPU cache coherent
- * @file: VFIO group file
- *
- * Enforced coherency means that the IOMMU ignores things like the PCIe no-snoop
- * bit in DMA transactions. A return of false indicates that the user has
- * rights to access additional instructions such as wbinvd on x86.
- */
-bool vfio_file_enforced_coherent(struct file *file)
+bool vfio_group_enforced_coherent(struct vfio_group *group)
 {
-	struct vfio_group *group = file->private_data;
 	struct vfio_device *device;
 	bool ret = true;
 
-	if (!vfio_file_is_group(file))
-		return true;
-
 	/*
 	 * If the device does not have IOMMU_CAP_ENFORCE_CACHE_COHERENCY then
 	 * any domain later attached to it will also not support it. If the cap
@@ -795,46 +781,22 @@ bool vfio_file_enforced_coherent(struct file *file)
 	mutex_unlock(&group->device_lock);
 	return ret;
 }
-EXPORT_SYMBOL_GPL(vfio_file_enforced_coherent);
 
-/**
- * vfio_file_set_kvm - Link a kvm with VFIO drivers
- * @file: VFIO group file
- * @kvm: KVM to link
- *
- * When a VFIO device is first opened the KVM will be available in
- * device->kvm if one was associated with the group.
- */
-void vfio_file_set_kvm(struct file *file, struct kvm *kvm)
+void vfio_group_set_kvm(struct vfio_group *group, struct kvm *kvm)
 {
-	struct vfio_group *group = file->private_data;
-
-	if (!vfio_file_is_group(file))
-		return;
-
+	/*
+	 * When a VFIO device is first opened the KVM will be available in
+	 * device->kvm if one was associated with the group.
+	 */
 	mutex_lock(&group->group_lock);
 	group->kvm = kvm;
 	mutex_unlock(&group->group_lock);
 }
-EXPORT_SYMBOL_GPL(vfio_file_set_kvm);
 
-/**
- * vfio_file_has_dev - True if the VFIO file is a handle for device
- * @file: VFIO file to check
- * @device: Device that must be part of the file
- *
- * Returns true if given file has permission to manipulate the given device.
- */
-bool vfio_file_has_dev(struct file *file, struct vfio_device *device)
+bool vfio_group_has_dev(struct vfio_group *group, struct vfio_device *device)
 {
-	struct vfio_group *group = file->private_data;
-
-	if (!vfio_file_is_group(file))
-		return false;
-
 	return group == device->group;
 }
-EXPORT_SYMBOL_GPL(vfio_file_has_dev);
 
 static char *vfio_devnode(const struct device *dev, umode_t *mode)
 {
drivers/vfio/pci/vfio_pci_core.c +2 -2
index 26a541cc64d1..985c6184a587 100644
@@ -1319,8 +1319,8 @@ static int vfio_pci_ioctl_pci_hot_reset(struct vfio_pci_core_device *vdev,
 			break;
 		}
 
-		/* Ensure the FD is a vfio group FD.*/
-		if (!vfio_file_is_group(file)) {
+		/* Ensure the FD is a vfio FD.*/
+		if (!vfio_file_is_valid(file)) {
 			fput(file);
 			ret = -EINVAL;
 			break;
drivers/vfio/vfio.h +4 -0
index 1091806bc89d..ef5de2872983 100644
@@ -90,6 +90,10 @@ void vfio_device_group_unregister(struct vfio_device *device);
 int vfio_device_group_use_iommu(struct vfio_device *device);
 void vfio_device_group_unuse_iommu(struct vfio_device *device);
 void vfio_device_group_close(struct vfio_device *device);
+struct vfio_group *vfio_group_from_file(struct file *file);
+bool vfio_group_enforced_coherent(struct vfio_group *group);
+void vfio_group_set_kvm(struct vfio_group *group, struct kvm *kvm);
+bool vfio_group_has_dev(struct vfio_group *group, struct vfio_device *device);
 bool vfio_device_has_container(struct vfio_device *device);
 int __init vfio_group_init(void);
 void vfio_group_cleanup(void);
drivers/vfio/vfio_main.c +0 -0
index ee54c9ae0af4..1aedfbd15ca0 100644

drivers/vfio/vfio_main.c const struct file_operations vfio_device_fops = { · line 1128

Quoted 10 lines
drivers/vfio/vfio_main.c +7 -0
@@ -1119,6 +1119,68 @@ const struct file_operations vfio_device_fops = {
 	.mmap		= vfio_device_fops_mmap,
 };
 
+/**
+ * vfio_file_is_valid - True if the file is usable with VFIO aPIS
+ * @file: VFIO group file or VFIO device file
+ */
+bool vfio_file_is_valid(struct file *file)
+{
+	return vfio_group_from_file(file);

is this implicit conversion from ptr to bool always safe?

Quoted 30 lines
drivers/vfio/vfio_main.c +30 -0
+}
+EXPORT_SYMBOL_GPL(vfio_file_is_valid);
+
+/**
+ * vfio_file_enforced_coherent - True if the DMA associated with the VFIO file
+ *        is always CPU cache coherent
+ * @file: VFIO group or device file
+ *
+ * Enforced coherency means that the IOMMU ignores things like the PCIe no-snoop
+ * bit in DMA transactions. A return of false indicates that the user has
+ * rights to access additional instructions such as wbinvd on x86.
+ */
+bool vfio_file_enforced_coherent(struct file *file)
+{
+	struct vfio_group *group = vfio_group_from_file(file);
+
+	if (group)
+		return vfio_group_enforced_coherent(group);
+
+	return true;
+}
+EXPORT_SYMBOL_GPL(vfio_file_enforced_coherent);
+
+/**
+ * vfio_file_set_kvm - Link a kvm with VFIO drivers
+ * @file: VFIO group file or device file
+ * @kvm: KVM to link
+ *
+ */
+void vfio_file_set_kvm(struct file *file, struct kvm *kvm)

drivers/vfio/vfio_main.c const struct file_operations vfio_device_fops = { · line 1168

Quoted 10 lines
drivers/vfio/vfio_main.c +10 -0
+{
+	struct vfio_group *group = vfio_group_from_file(file);
+
+	if (group)
+		vfio_group_set_kvm(group, kvm);
+}
+EXPORT_SYMBOL_GPL(vfio_file_set_kvm);
+
+/**
+ * vfio_file_has_dev - True if the VFIO file is a handle for device

This original description sounds weird because originally it aimed at figuring whether the device belonged to that vfio group fd, no? And since it will handle both group fd and device fd it still sounds weird to me.

Quoted 58 lines
drivers/vfio/vfio_main.c +15 -0
+ * @file: VFIO file to check
+ * @device: Device that must be part of the file
+ *
+ * Returns true if given file has permission to manipulate the given device.
+ */
+bool vfio_file_has_dev(struct file *file, struct vfio_device *device)
+{
+	struct vfio_group *group = vfio_group_from_file(file);
+
+	if (group)
+		return vfio_group_has_dev(group, device);
+	return false;
+}
+EXPORT_SYMBOL_GPL(vfio_file_has_dev);
+
 /*
  * Sub-module support
  */
include/linux/vfio.h +1 -1
index 35be78e9ae57..46edd6e6c0ba 100644
@@ -241,7 +241,7 @@ int vfio_mig_get_next_state(struct vfio_device *device,
  * External user API
  */
 struct iommu_group *vfio_file_iommu_group(struct file *file);
-bool vfio_file_is_group(struct file *file);
+bool vfio_file_is_valid(struct file *file);
 bool vfio_file_enforced_coherent(struct file *file);
 void vfio_file_set_kvm(struct file *file, struct kvm *kvm);
 bool vfio_file_has_dev(struct file *file, struct vfio_device *device);
virt/kvm/vfio.c +3 -3
index 495ceabffe88..868930c7a59b 100644
@@ -64,18 +64,18 @@ static bool kvm_vfio_file_enforced_coherent(struct file *file)
 	return ret;
 }
 
-static bool kvm_vfio_file_is_group(struct file *file)
+static bool kvm_vfio_file_is_valid(struct file *file)
 {
 	bool (*fn)(struct file *file);
 	bool ret;
 
-	fn = symbol_get(vfio_file_is_group);
+	fn = symbol_get(vfio_file_is_valid);
 	if (!fn)
 		return false;
 
 	ret = fn(file);
 
-	symbol_put(vfio_file_is_group);
+	symbol_put(vfio_file_is_valid);
 
 	return ret;
 }

virt/kvm/vfio.c static int kvm_vfio_group_add(struct kvm_device *dev, unsigned int fd) · line 161

Quoted 10 lines
virt/kvm/vfio.c +2 -2
@@ -154,8 +154,8 @@ static int kvm_vfio_group_add(struct kvm_device *dev, unsigned int fd)
 	if (!filp)
 		return -EBADF;
 
-	/* Ensure the FD is a vfio group FD.*/
-	if (!kvm_vfio_file_is_group(filp)) {
+	/* Ensure the FD is a vfio FD.*/
+	if (!kvm_vfio_file_is_valid(filp)) {
 		ret = -EINVAL;
 		goto err_fput;
 	}

Besides

Thanks

Eric

author
#7 lore raw

RE: [PATCH 02/13] vfio: Refine vfio file kAPIs

Hi Eric,

Quoted 9 lines

From: Eric Auger <[email protected]>
Sent: Wednesday, January 18, 2023 10:37 PM

Quoted 2 lines

+/**
+ * vfio_file_has_dev - True if the VFIO file is a handle for device

This original description sounds weird because originally it aimed at figuring whether the device belonged to that vfio group fd, no? And since it will handle both group fd and device fd it still sounds weird to me.

Yes. It is to check if a device belongs to the input vfio group specified by the group fd. And after this commit, it means if the input file is competent to be handle for the device either due to belong to group or it is just a device fd for the device. I don’t have a better naming so far. How about you?

Regards,
Yi Liu

author
#8 lore raw

PATCH 3/13 vfio: Accept vfio device file in the driver facing kAPI

In reply to Yi Liu

This makes the vfio file kAPIs to accepte vfio device files, also a preparation for vfio device cdev support.

---
 drivers/vfio/vfio.h      |  1 +
 drivers/vfio/vfio_main.c | 51 ++++++++++++++++++++++++++++++++++++----
 2 files changed, 48 insertions(+), 4 deletions(-)
drivers/vfio/vfio.h +1 -0
index ef5de2872983..53af6e3ea214 100644
@@ -18,6 +18,7 @@ struct vfio_container;
 
 struct vfio_device_file {
 	struct vfio_device *device;
+	struct kvm *kvm;
 };
 
 void vfio_device_put_registration(struct vfio_device *device);
drivers/vfio/vfio_main.c +47 -4

95 lines not on this page · raw message

Signature · 1 line
2.34.1
#10 lore raw

Re: [PATCH 03/13] vfio: Accept vfio device file in the driver facing kAPI

In reply to Yi Liu

Hi Yi,

Quoted 72 lines · Yi Liu

This makes the vfio file kAPIs to accepte vfio device files, also a preparation for vfio device cdev support.

---
 drivers/vfio/vfio.h      |  1 +
 drivers/vfio/vfio_main.c | 51 ++++++++++++++++++++++++++++++++++++----
 2 files changed, 48 insertions(+), 4 deletions(-)
drivers/vfio/vfio.h +1 -0
index ef5de2872983..53af6e3ea214 100644
@@ -18,6 +18,7 @@ struct vfio_container;
 
 struct vfio_device_file {
 	struct vfio_device *device;
+	struct kvm *kvm;
 };
 
 void vfio_device_put_registration(struct vfio_device *device);
drivers/vfio/vfio_main.c +20 -2
index 1aedfbd15ca0..dc08d5dd62cc 100644
@@ -1119,13 +1119,23 @@ const struct file_operations vfio_device_fops = {
 	.mmap		= vfio_device_fops_mmap,
 };
 
+static struct vfio_device *vfio_device_from_file(struct file *file)
+{
+	struct vfio_device_file *df = file->private_data;
+
+	if (file->f_op != &vfio_device_fops)
+		return NULL;
+	return df->device;
+}
+
 /**
  * vfio_file_is_valid - True if the file is usable with VFIO aPIS
  * @file: VFIO group file or VFIO device file
  */
 bool vfio_file_is_valid(struct file *file)
 {
-	return vfio_group_from_file(file);
+	return vfio_group_from_file(file) ||
+	       vfio_device_from_file(file);
 }
 EXPORT_SYMBOL_GPL(vfio_file_is_valid);
 
@@ -1140,15 +1150,37 @@ EXPORT_SYMBOL_GPL(vfio_file_is_valid);
  */
 bool vfio_file_enforced_coherent(struct file *file)
 {
-	struct vfio_group *group = vfio_group_from_file(file);
+	struct vfio_group *group;
+	struct vfio_device *device;
 
+	group = vfio_group_from_file(file);
 	if (group)
 		return vfio_group_enforced_coherent(group);
 
+	device = vfio_device_from_file(file);
+	if (device)
+		return device_iommu_capable(device->dev,
+					    IOMMU_CAP_ENFORCE_CACHE_COHERENCY);
+
 	return true;
 }
 EXPORT_SYMBOL_GPL(vfio_file_enforced_coherent);
 
+static void vfio_device_file_set_kvm(struct file *file, struct kvm *kvm)

drivers/vfio/vfio_main.c EXPORT_SYMBOL_GPL(vfio_file_is_valid); · line 1179

Quoted 10 lines
drivers/vfio/vfio_main.c +10 -0
+{
+	struct vfio_device_file *df = file->private_data;
+	struct vfio_device *device = df->device;
+
+	/*
+	 * The kvm is first recorded in the df, and will be propagated
+	 * to vfio_device::kvm when the file binds iommufd successfully in
+	 * the vfio device cdev path.
+	 */
+	mutex_lock(&device->dev_set->lock);

it is not totally obvious to me why the

device->dev_set->lock needs to be held here and why that lock in particular. Isn't supposed to protect the vfio_device_set. The header just mentions "the VFIO core will provide a lock that is held around open_device()/close_device() for all devices in the set."

Quoted 32 lines
drivers/vfio/vfio_main.c +12 -2
+	df->kvm = kvm;
+	mutex_unlock(&device->dev_set->lock);
+}
+
 /**
  * vfio_file_set_kvm - Link a kvm with VFIO drivers
  * @file: VFIO group file or device file
@@ -1157,10 +1189,14 @@ EXPORT_SYMBOL_GPL(vfio_file_enforced_coherent);
  */
 void vfio_file_set_kvm(struct file *file, struct kvm *kvm)
 {
-	struct vfio_group *group = vfio_group_from_file(file);
+	struct vfio_group *group;
 
+	group = vfio_group_from_file(file);
 	if (group)
 		vfio_group_set_kvm(group, kvm);
+
+	if (vfio_device_from_file(file))
+		vfio_device_file_set_kvm(file, kvm);
 }
 EXPORT_SYMBOL_GPL(vfio_file_set_kvm);
 
@@ -1173,10 +1209,17 @@ EXPORT_SYMBOL_GPL(vfio_file_set_kvm);
  */
 bool vfio_file_has_dev(struct file *file, struct vfio_device *device)
 {
-	struct vfio_group *group = vfio_group_from_file(file);
+	struct vfio_group *group;
+	struct vfio_device *vdev;
 
+	group = vfio_group_from_file(file);

drivers/vfio/vfio_main.c EXPORT_SYMBOL_GPL(vfio_file_set_kvm); · line 1225

Quoted 10 lines
drivers/vfio/vfio_main.c +5 -0
 	if (group)
 		return vfio_group_has_dev(group, device);
+
+	vdev = vfio_device_from_file(file);
+	if (device)
+		return vdev == device;
+
 	return false;
 }
 EXPORT_SYMBOL_GPL(vfio_file_has_dev);

Thanks

Eric

author
#11 lore raw

RE: [PATCH 03/13] vfio: Accept vfio device file in the driver facing kAPI

Hi Eric,

Quoted 93 lines

From: Eric Auger <[email protected]>
Sent: Thursday, January 19, 2023 12:11 AM

Hi Yi,

Quoted 7 lines · Yi Liu

This makes the vfio file kAPIs to accepte vfio device files, also a preparation for vfio device cdev support.

---
 drivers/vfio/vfio.h      |  1 +
 drivers/vfio/vfio_main.c | 51

++++++++++++++++++++++++++++++++++++----

drivers/vfio/vfio_main.c const struct file_operations vfio_device_fops

Quoted 19 lines
 2 files changed, 48 insertions(+), 4 deletions(-)
drivers/vfio/vfio.h +1 -0
index ef5de2872983..53af6e3ea214 100644
@@ -18,6 +18,7 @@ struct vfio_container;
 
 struct vfio_device_file {
 	struct vfio_device *device;
+	struct kvm *kvm;
 };
 
 void vfio_device_put_registration(struct vfio_device *device);
drivers/vfio/vfio_main.c +0 -0
index 1aedfbd15ca0..dc08d5dd62cc 100644
@@ -1119,13 +1119,23 @@ const struct file_operations vfio_device_fops

= {

Quoted 30 lines
drivers/vfio/vfio_main.c +12 -2
 	.mmap		= vfio_device_fops_mmap,
 };
 
+static struct vfio_device *vfio_device_from_file(struct file *file)
+{
+	struct vfio_device_file *df = file->private_data;
+
+	if (file->f_op != &vfio_device_fops)
+		return NULL;
+	return df->device;
+}
+
 /**
  * vfio_file_is_valid - True if the file is usable with VFIO aPIS
  * @file: VFIO group file or VFIO device file
  */
 bool vfio_file_is_valid(struct file *file)
 {
-	return vfio_group_from_file(file);
+	return vfio_group_from_file(file) ||
+	       vfio_device_from_file(file);
 }
 EXPORT_SYMBOL_GPL(vfio_file_is_valid);
 
@@ -1140,15 +1150,37 @@ EXPORT_SYMBOL_GPL(vfio_file_is_valid);
  */
 bool vfio_file_enforced_coherent(struct file *file)
 {
-	struct vfio_group *group = vfio_group_from_file(file);
+	struct vfio_group *group;

drivers/vfio/vfio_main.c EXPORT_SYMBOL_GPL(vfio_file_is_valid); · line 1163

Quoted 10 lines
drivers/vfio/vfio_main.c +6 -0
+	struct vfio_device *device;
 
+	group = vfio_group_from_file(file);
 	if (group)
 		return vfio_group_enforced_coherent(group);
 
+	device = vfio_device_from_file(file);
+	if (device)
+		return device_iommu_capable(device->dev,
+

IOMMU_CAP_ENFORCE_CACHE_COHERENCY);

Quoted 6 lines
drivers/vfio/vfio_main.c +2 -0
+
 	return true;
 }
 EXPORT_SYMBOL_GPL(vfio_file_enforced_coherent);
 
+static void vfio_device_file_set_kvm(struct file *file, struct kvm *kvm)

drivers/vfio/vfio_main.c EXPORT_SYMBOL_GPL(vfio_file_is_valid); · line 1179

Quoted 10 lines
drivers/vfio/vfio_main.c +10 -0
+{
+	struct vfio_device_file *df = file->private_data;
+	struct vfio_device *device = df->device;
+
+	/*
+	 * The kvm is first recorded in the df, and will be propagated
+	 * to vfio_device::kvm when the file binds iommufd successfully in
+	 * the vfio device cdev path.
+	 */
+	mutex_lock(&device->dev_set->lock);

it is not totally obvious to me why the

Quoted 4 lines

device->dev_set->lock needs to be held here and why that lock in particular. Isn't supposed to protect the vfio_device_set. The header just mentions "the VFIO core will provide a lock that is held around open_device()/close_device() for all devices in the set."

The reason is the df->kvm was referenced in vfio_device_first_open() in the below commit. To avoid race, a common lock is needed between the set_kvm thread and the open thread. For group path, group_lock is used. However, for cdev path, there may be no group_lock compiled, so need to use another one. And dev_set->lock happens to be used in the open path, so use it avoids to adding another specific lock.

https://lore.kernel.org/kvm/[email protected]/

Quoted 2 lines

drivers/vfio/vfio_main.c EXPORT_SYMBOL_GPL(vfio_file_is_valid); · line 1181

Quoted 2 lines
drivers/vfio/vfio_main.c +2 -0
+	df->kvm = kvm;
+	mutex_unlock(&device->dev_set->lock);

Regards,
Yi Liu

maintainer
#12 lore raw

Re: [PATCH 03/13] vfio: Accept vfio device file in the driver facing kAPI

Quoted 6 lines · Yi L

The reason is the df->kvm was referenced in vfio_device_first_open() in the below commit. To avoid race, a common lock is needed between the set_kvm thread and the open thread. For group path, group_lock is used. However, for cdev path, there may be no group_lock compiled, so need to use another one. And dev_set->lock happens to be used in the open path, so use it avoids to adding another specific lock.

Add a comment around the kvm pointer in the struct that it is weirdly locked

Jason

author
#13 lore raw

PATCH 4/13 kvm/vfio: Rename kvm_vfio_group to prepare for accepting vfio device fd

In reply to Yi Liu

Meanwhile, rename related helpers. No functional change is intended.

---
 virt/kvm/vfio.c | 115 ++++++++++++++++++++++++------------------------
 1 file changed, 58 insertions(+), 57 deletions(-)
virt/kvm/vfio.c +58 -57
index 868930c7a59b..0f54b9d308d7 100644
@@ -21,7 +21,7 @@
 #include <asm/kvm_ppc.h>
 #endif
 
-struct kvm_vfio_group {
+struct kvm_vfio_file {
 	struct list_head node;
 	struct file *file;
 #ifdef CONFIG_SPAPR_TCE_IOMMU
@@ -30,7 +30,7 @@ struct kvm_vfio_group {
 };
 
 struct kvm_vfio {
-	struct list_head group_list;
+	struct list_head file_list;
 	struct mutex lock;
 	bool noncoherent;
 };
@@ -98,34 +98,35 @@ static struct iommu_group *kvm_vfio_file_iommu_group(struct file *file)
 }
 
 static void kvm_spapr_tce_release_vfio_group(struct kvm *kvm,
-					     struct kvm_vfio_group *kvg)
+					     struct kvm_vfio_file *kvf)
 {
-	if (WARN_ON_ONCE(!kvg->iommu_group))
+	if (WARN_ON_ONCE(!kvf->iommu_group))
 		return;
 
-	kvm_spapr_tce_release_iommu_group(kvm, kvg->iommu_group);
-	iommu_group_put(kvg->iommu_group);
-	kvg->iommu_group = NULL;
+	kvm_spapr_tce_release_iommu_group(kvm, kvf->iommu_group);
+	iommu_group_put(kvf->iommu_group);
+	kvf->iommu_group = NULL;
 }
 #endif
 
 /*
- * Groups can use the same or different IOMMU domains.  If the same then
- * adding a new group may change the coherency of groups we've previously
- * been told about.  We don't want to care about any of that so we retest
- * each group and bail as soon as we find one that's noncoherent.  This
- * means we only ever [un]register_noncoherent_dma once for the whole device.
+ * Groups/devices can use the same or different IOMMU domains.  If the same
+ * then adding a new group/device may change the coherency of groups/devices
+ * we've previously been told about.  We don't want to care about any of
+ * that so we retest each group/device and bail as soon as we find one that's
+ * noncoherent.  This means we only ever [un]register_noncoherent_dma once
+ * for the whole device.
  */
 static void kvm_vfio_update_coherency(struct kvm_device *dev)
 {
 	struct kvm_vfio *kv = dev->private;
 	bool noncoherent = false;
-	struct kvm_vfio_group *kvg;
+	struct kvm_vfio_file *kvf;
 
 	mutex_lock(&kv->lock);
 
-	list_for_each_entry(kvg, &kv->group_list, node) {
-		if (!kvm_vfio_file_enforced_coherent(kvg->file)) {
+	list_for_each_entry(kvf, &kv->file_list, node) {
+		if (!kvm_vfio_file_enforced_coherent(kvf->file)) {
 			noncoherent = true;
 			break;
 		}
@@ -143,10 +144,10 @@ static void kvm_vfio_update_coherency(struct kvm_device *dev)
 	mutex_unlock(&kv->lock);
 }
 
-static int kvm_vfio_group_add(struct kvm_device *dev, unsigned int fd)
+static int kvm_vfio_file_add(struct kvm_device *dev, unsigned int fd)
 {
 	struct kvm_vfio *kv = dev->private;
-	struct kvm_vfio_group *kvg;
+	struct kvm_vfio_file *kvf;
 	struct file *filp;
 	int ret;
 
@@ -162,27 +163,27 @@ static int kvm_vfio_group_add(struct kvm_device *dev, unsigned int fd)
 
 	mutex_lock(&kv->lock);
 
-	list_for_each_entry(kvg, &kv->group_list, node) {
-		if (kvg->file == filp) {
+	list_for_each_entry(kvf, &kv->file_list, node) {
+		if (kvf->file == filp) {
 			ret = -EEXIST;
 			goto err_unlock;
 		}
 	}
 
-	kvg = kzalloc(sizeof(*kvg), GFP_KERNEL_ACCOUNT);
-	if (!kvg) {
+	kvf = kzalloc(sizeof(*kvf), GFP_KERNEL_ACCOUNT);
+	if (!kvf) {
 		ret = -ENOMEM;
 		goto err_unlock;
 	}
 
-	kvg->file = filp;
-	list_add_tail(&kvg->node, &kv->group_list);
+	kvf->file = filp;
+	list_add_tail(&kvf->node, &kv->file_list);
 
 	kvm_arch_start_assignment(dev->kvm);
 
 	mutex_unlock(&kv->lock);
 
-	kvm_vfio_file_set_kvm(kvg->file, dev->kvm);
+	kvm_vfio_file_set_kvm(kvf->file, dev->kvm);
 	kvm_vfio_update_coherency(dev);
 
 	return 0;
@@ -193,10 +194,10 @@ static int kvm_vfio_group_add(struct kvm_device *dev, unsigned int fd)
 	return ret;
 }
 
-static int kvm_vfio_group_del(struct kvm_device *dev, unsigned int fd)
+static int kvm_vfio_file_del(struct kvm_device *dev, unsigned int fd)
 {
 	struct kvm_vfio *kv = dev->private;
-	struct kvm_vfio_group *kvg;
+	struct kvm_vfio_file *kvf;
 	struct fd f;
 	int ret;
 
@@ -208,18 +209,18 @@ static int kvm_vfio_group_del(struct kvm_device *dev, unsigned int fd)
 
 	mutex_lock(&kv->lock);
 
-	list_for_each_entry(kvg, &kv->group_list, node) {
-		if (kvg->file != f.file)
+	list_for_each_entry(kvf, &kv->file_list, node) {
+		if (kvf->file != f.file)
 			continue;
 
-		list_del(&kvg->node);
+		list_del(&kvf->node);
 		kvm_arch_end_assignment(dev->kvm);
 #ifdef CONFIG_SPAPR_TCE_IOMMU
-		kvm_spapr_tce_release_vfio_group(dev->kvm, kvg);
+		kvm_spapr_tce_release_vfio_group(dev->kvm, kvf);
 #endif
-		kvm_vfio_file_set_kvm(kvg->file, NULL);
-		fput(kvg->file);
-		kfree(kvg);
+		kvm_vfio_file_set_kvm(kvf->file, NULL);
+		fput(kvf->file);
+		kfree(kvf);
 		ret = 0;
 		break;
 	}
@@ -234,12 +235,12 @@ static int kvm_vfio_group_del(struct kvm_device *dev, unsigned int fd)
 }
 
 #ifdef CONFIG_SPAPR_TCE_IOMMU
-static int kvm_vfio_group_set_spapr_tce(struct kvm_device *dev,
-					void __user *arg)
+static int kvm_vfio_file_set_spapr_tce(struct kvm_device *dev,
+				       void __user *arg)
 {
 	struct kvm_vfio_spapr_tce param;
 	struct kvm_vfio *kv = dev->private;
-	struct kvm_vfio_group *kvg;
+	struct kvm_vfio_file *kvf;
 	struct fd f;
 	int ret;
 
@@ -254,20 +255,20 @@ static int kvm_vfio_group_set_spapr_tce(struct kvm_device *dev,
 
 	mutex_lock(&kv->lock);
 
-	list_for_each_entry(kvg, &kv->group_list, node) {
-		if (kvg->file != f.file)
+	list_for_each_entry(kvf, &kv->file_list, node) {
+		if (kvf->file != f.file)
 			continue;
 
-		if (!kvg->iommu_group) {
-			kvg->iommu_group = kvm_vfio_file_iommu_group(kvg->file);
-			if (WARN_ON_ONCE(!kvg->iommu_group)) {
+		if (!kvf->iommu_group) {
+			kvf->iommu_group = kvm_vfio_file_iommu_group(kvf->file);
+			if (WARN_ON_ONCE(!kvf->iommu_group)) {
 				ret = -EIO;
 				goto err_fdput;
 			}
 		}
 
 		ret = kvm_spapr_tce_attach_iommu_group(dev->kvm, param.tablefd,
-						       kvg->iommu_group);
+						       kvf->iommu_group);
 		break;
 	}
 
@@ -278,8 +279,8 @@ static int kvm_vfio_group_set_spapr_tce(struct kvm_device *dev,
 }
 #endif
 
-static int kvm_vfio_set_group(struct kvm_device *dev, long attr,
-			      void __user *arg)
+static int kvm_vfio_set_file(struct kvm_device *dev, long attr,
+			     void __user *arg)
 {
 	int32_t __user *argp = arg;
 	int32_t fd;
@@ -288,16 +289,16 @@ static int kvm_vfio_set_group(struct kvm_device *dev, long attr,
 	case KVM_DEV_VFIO_GROUP_ADD:
 		if (get_user(fd, argp))
 			return -EFAULT;
-		return kvm_vfio_group_add(dev, fd);
+		return kvm_vfio_file_add(dev, fd);
 
 	case KVM_DEV_VFIO_GROUP_DEL:
 		if (get_user(fd, argp))
 			return -EFAULT;
-		return kvm_vfio_group_del(dev, fd);
+		return kvm_vfio_file_del(dev, fd);
 
 #ifdef CONFIG_SPAPR_TCE_IOMMU
 	case KVM_DEV_VFIO_GROUP_SET_SPAPR_TCE:
-		return kvm_vfio_group_set_spapr_tce(dev, arg);
+		return kvm_vfio_file_set_spapr_tce(dev, arg);
 #endif
 	}
 
@@ -309,8 +310,8 @@ static int kvm_vfio_set_attr(struct kvm_device *dev,
 {
 	switch (attr->group) {
 	case KVM_DEV_VFIO_GROUP:
-		return kvm_vfio_set_group(dev, attr->attr,
-					  u64_to_user_ptr(attr->addr));
+		return kvm_vfio_set_file(dev, attr->attr,
+					 u64_to_user_ptr(attr->addr));
 	}
 
 	return -ENXIO;
@@ -339,16 +340,16 @@ static int kvm_vfio_has_attr(struct kvm_device *dev,
 static void kvm_vfio_destroy(struct kvm_device *dev)
 {
 	struct kvm_vfio *kv = dev->private;
-	struct kvm_vfio_group *kvg, *tmp;
+	struct kvm_vfio_file *kvf, *tmp;
 
-	list_for_each_entry_safe(kvg, tmp, &kv->group_list, node) {
+	list_for_each_entry_safe(kvf, tmp, &kv->file_list, node) {
 #ifdef CONFIG_SPAPR_TCE_IOMMU
-		kvm_spapr_tce_release_vfio_group(dev->kvm, kvg);
+		kvm_spapr_tce_release_vfio_group(dev->kvm, kvf);
 #endif
-		kvm_vfio_file_set_kvm(kvg->file, NULL);
-		fput(kvg->file);
-		list_del(&kvg->node);
-		kfree(kvg);
+		kvm_vfio_file_set_kvm(kvf->file, NULL);
+		fput(kvf->file);
+		list_del(&kvf->node);
+		kfree(kvf);
 		kvm_arch_end_assignment(dev->kvm);
 	}
 
@@ -382,7 +383,7 @@ static int kvm_vfio_create(struct kvm_device *dev, u32 type)
 	if (!kv)
 		return -ENOMEM;
 
-	INIT_LIST_HEAD(&kv->group_list);
+	INIT_LIST_HEAD(&kv->file_list);
 	mutex_init(&kv->lock);
 
 	dev->private = kv;
Signature · 1 line
2.34.1
Reviewed-by on v1 4/13
#15 lore raw

Re: [PATCH 04/13] kvm/vfio: Rename kvm_vfio_group to prepare for accepting vfio device fd

In reply to Yi Liu
Quoted 275 lines · Yi Liu

Meanwhile, rename related helpers. No functional change is intended.

---
 virt/kvm/vfio.c | 115 ++++++++++++++++++++++++------------------------
 1 file changed, 58 insertions(+), 57 deletions(-)
virt/kvm/vfio.c +57 -56
index 868930c7a59b..0f54b9d308d7 100644
@@ -21,7 +21,7 @@
 #include <asm/kvm_ppc.h>
 #endif
 
-struct kvm_vfio_group {
+struct kvm_vfio_file {
 	struct list_head node;
 	struct file *file;
 #ifdef CONFIG_SPAPR_TCE_IOMMU
@@ -30,7 +30,7 @@ struct kvm_vfio_group {
 };
 
 struct kvm_vfio {
-	struct list_head group_list;
+	struct list_head file_list;
 	struct mutex lock;
 	bool noncoherent;
 };
@@ -98,34 +98,35 @@ static struct iommu_group *kvm_vfio_file_iommu_group(struct file *file)
 }
 
 static void kvm_spapr_tce_release_vfio_group(struct kvm *kvm,
-					     struct kvm_vfio_group *kvg)
+					     struct kvm_vfio_file *kvf)
 {
-	if (WARN_ON_ONCE(!kvg->iommu_group))
+	if (WARN_ON_ONCE(!kvf->iommu_group))
 		return;
 
-	kvm_spapr_tce_release_iommu_group(kvm, kvg->iommu_group);
-	iommu_group_put(kvg->iommu_group);
-	kvg->iommu_group = NULL;
+	kvm_spapr_tce_release_iommu_group(kvm, kvf->iommu_group);
+	iommu_group_put(kvf->iommu_group);
+	kvf->iommu_group = NULL;
 }
 #endif
 
 /*
- * Groups can use the same or different IOMMU domains.  If the same then
- * adding a new group may change the coherency of groups we've previously
- * been told about.  We don't want to care about any of that so we retest
- * each group and bail as soon as we find one that's noncoherent.  This
- * means we only ever [un]register_noncoherent_dma once for the whole device.
+ * Groups/devices can use the same or different IOMMU domains.  If the same
+ * then adding a new group/device may change the coherency of groups/devices
+ * we've previously been told about.  We don't want to care about any of
+ * that so we retest each group/device and bail as soon as we find one that's
+ * noncoherent.  This means we only ever [un]register_noncoherent_dma once
+ * for the whole device.
  */
 static void kvm_vfio_update_coherency(struct kvm_device *dev)
 {
 	struct kvm_vfio *kv = dev->private;
 	bool noncoherent = false;
-	struct kvm_vfio_group *kvg;
+	struct kvm_vfio_file *kvf;
 
 	mutex_lock(&kv->lock);
 
-	list_for_each_entry(kvg, &kv->group_list, node) {
-		if (!kvm_vfio_file_enforced_coherent(kvg->file)) {
+	list_for_each_entry(kvf, &kv->file_list, node) {
+		if (!kvm_vfio_file_enforced_coherent(kvf->file)) {
 			noncoherent = true;
 			break;
 		}
@@ -143,10 +144,10 @@ static void kvm_vfio_update_coherency(struct kvm_device *dev)
 	mutex_unlock(&kv->lock);
 }
 
-static int kvm_vfio_group_add(struct kvm_device *dev, unsigned int fd)
+static int kvm_vfio_file_add(struct kvm_device *dev, unsigned int fd)
 {
 	struct kvm_vfio *kv = dev->private;
-	struct kvm_vfio_group *kvg;
+	struct kvm_vfio_file *kvf;
 	struct file *filp;
 	int ret;
 
@@ -162,27 +163,27 @@ static int kvm_vfio_group_add(struct kvm_device *dev, unsigned int fd)
 
 	mutex_lock(&kv->lock);
 
-	list_for_each_entry(kvg, &kv->group_list, node) {
-		if (kvg->file == filp) {
+	list_for_each_entry(kvf, &kv->file_list, node) {
+		if (kvf->file == filp) {
 			ret = -EEXIST;
 			goto err_unlock;
 		}
 	}
 
-	kvg = kzalloc(sizeof(*kvg), GFP_KERNEL_ACCOUNT);
-	if (!kvg) {
+	kvf = kzalloc(sizeof(*kvf), GFP_KERNEL_ACCOUNT);
+	if (!kvf) {
 		ret = -ENOMEM;
 		goto err_unlock;
 	}
 
-	kvg->file = filp;
-	list_add_tail(&kvg->node, &kv->group_list);
+	kvf->file = filp;
+	list_add_tail(&kvf->node, &kv->file_list);
 
 	kvm_arch_start_assignment(dev->kvm);
 
 	mutex_unlock(&kv->lock);
 
-	kvm_vfio_file_set_kvm(kvg->file, dev->kvm);
+	kvm_vfio_file_set_kvm(kvf->file, dev->kvm);
 	kvm_vfio_update_coherency(dev);
 
 	return 0;
@@ -193,10 +194,10 @@ static int kvm_vfio_group_add(struct kvm_device *dev, unsigned int fd)
 	return ret;
 }
 
-static int kvm_vfio_group_del(struct kvm_device *dev, unsigned int fd)
+static int kvm_vfio_file_del(struct kvm_device *dev, unsigned int fd)
 {
 	struct kvm_vfio *kv = dev->private;
-	struct kvm_vfio_group *kvg;
+	struct kvm_vfio_file *kvf;
 	struct fd f;
 	int ret;
 
@@ -208,18 +209,18 @@ static int kvm_vfio_group_del(struct kvm_device *dev, unsigned int fd)
 
 	mutex_lock(&kv->lock);
 
-	list_for_each_entry(kvg, &kv->group_list, node) {
-		if (kvg->file != f.file)
+	list_for_each_entry(kvf, &kv->file_list, node) {
+		if (kvf->file != f.file)
 			continue;
 
-		list_del(&kvg->node);
+		list_del(&kvf->node);
 		kvm_arch_end_assignment(dev->kvm);
 #ifdef CONFIG_SPAPR_TCE_IOMMU
-		kvm_spapr_tce_release_vfio_group(dev->kvm, kvg);
+		kvm_spapr_tce_release_vfio_group(dev->kvm, kvf);
 #endif
-		kvm_vfio_file_set_kvm(kvg->file, NULL);
-		fput(kvg->file);
-		kfree(kvg);
+		kvm_vfio_file_set_kvm(kvf->file, NULL);
+		fput(kvf->file);
+		kfree(kvf);
 		ret = 0;
 		break;
 	}
@@ -234,12 +235,12 @@ static int kvm_vfio_group_del(struct kvm_device *dev, unsigned int fd)
 }
 
 #ifdef CONFIG_SPAPR_TCE_IOMMU
-static int kvm_vfio_group_set_spapr_tce(struct kvm_device *dev,
-					void __user *arg)
+static int kvm_vfio_file_set_spapr_tce(struct kvm_device *dev,
+				       void __user *arg)
 {
 	struct kvm_vfio_spapr_tce param;
 	struct kvm_vfio *kv = dev->private;
-	struct kvm_vfio_group *kvg;
+	struct kvm_vfio_file *kvf;
 	struct fd f;
 	int ret;
 
@@ -254,20 +255,20 @@ static int kvm_vfio_group_set_spapr_tce(struct kvm_device *dev,
 
 	mutex_lock(&kv->lock);
 
-	list_for_each_entry(kvg, &kv->group_list, node) {
-		if (kvg->file != f.file)
+	list_for_each_entry(kvf, &kv->file_list, node) {
+		if (kvf->file != f.file)
 			continue;
 
-		if (!kvg->iommu_group) {
-			kvg->iommu_group = kvm_vfio_file_iommu_group(kvg->file);
-			if (WARN_ON_ONCE(!kvg->iommu_group)) {
+		if (!kvf->iommu_group) {
+			kvf->iommu_group = kvm_vfio_file_iommu_group(kvf->file);
+			if (WARN_ON_ONCE(!kvf->iommu_group)) {
 				ret = -EIO;
 				goto err_fdput;
 			}
 		}
 
 		ret = kvm_spapr_tce_attach_iommu_group(dev->kvm, param.tablefd,
-						       kvg->iommu_group);
+						       kvf->iommu_group);
 		break;
 	}
 
@@ -278,8 +279,8 @@ static int kvm_vfio_group_set_spapr_tce(struct kvm_device *dev,
 }
 #endif
 
-static int kvm_vfio_set_group(struct kvm_device *dev, long attr,
-			      void __user *arg)
+static int kvm_vfio_set_file(struct kvm_device *dev, long attr,
+			     void __user *arg)
 {
 	int32_t __user *argp = arg;
 	int32_t fd;
@@ -288,16 +289,16 @@ static int kvm_vfio_set_group(struct kvm_device *dev, long attr,
 	case KVM_DEV_VFIO_GROUP_ADD:
 		if (get_user(fd, argp))
 			return -EFAULT;
-		return kvm_vfio_group_add(dev, fd);
+		return kvm_vfio_file_add(dev, fd);
 
 	case KVM_DEV_VFIO_GROUP_DEL:
 		if (get_user(fd, argp))
 			return -EFAULT;
-		return kvm_vfio_group_del(dev, fd);
+		return kvm_vfio_file_del(dev, fd);
 
 #ifdef CONFIG_SPAPR_TCE_IOMMU
 	case KVM_DEV_VFIO_GROUP_SET_SPAPR_TCE:
-		return kvm_vfio_group_set_spapr_tce(dev, arg);
+		return kvm_vfio_file_set_spapr_tce(dev, arg);
 #endif
 	}
 
@@ -309,8 +310,8 @@ static int kvm_vfio_set_attr(struct kvm_device *dev,
 {
 	switch (attr->group) {
 	case KVM_DEV_VFIO_GROUP:
-		return kvm_vfio_set_group(dev, attr->attr,
-					  u64_to_user_ptr(attr->addr));
+		return kvm_vfio_set_file(dev, attr->attr,
+					 u64_to_user_ptr(attr->addr));
 	}
 
 	return -ENXIO;
@@ -339,16 +340,16 @@ static int kvm_vfio_has_attr(struct kvm_device *dev,
 static void kvm_vfio_destroy(struct kvm_device *dev)
 {
 	struct kvm_vfio *kv = dev->private;
-	struct kvm_vfio_group *kvg, *tmp;
+	struct kvm_vfio_file *kvf, *tmp;
 
-	list_for_each_entry_safe(kvg, tmp, &kv->group_list, node) {
+	list_for_each_entry_safe(kvf, tmp, &kv->file_list, node) {
 #ifdef CONFIG_SPAPR_TCE_IOMMU
-		kvm_spapr_tce_release_vfio_group(dev->kvm, kvg);
+		kvm_spapr_tce_release_vfio_group(dev->kvm, kvf);
 #endif
-		kvm_vfio_file_set_kvm(kvg->file, NULL);
-		fput(kvg->file);
-		list_del(&kvg->node);
-		kfree(kvg);
+		kvm_vfio_file_set_kvm(kvf->file, NULL);
+		fput(kvf->file);
+		list_del(&kvf->node);
+		kfree(kvf);
 		kvm_arch_end_assignment(dev->kvm);
 	}
 

virt/kvm/vfio.c static int kvm_vfio_create(struct kvm_device *dev, u32 type) · line 389

Quoted 8 lines
virt/kvm/vfio.c +1 -1
@@ -382,7 +383,7 @@ static int kvm_vfio_create(struct kvm_device *dev, u32 type)
 	if (!kv)
 		return -ENOMEM;
 
-	INIT_LIST_HEAD(&kv->group_list);
+	INIT_LIST_HEAD(&kv->file_list);
 	mutex_init(&kv->lock);
 
 	dev->private = kv;

Eric

author
#16 lore raw

PATCH 5/13 kvm/vfio: Provide struct kvm_device_ops::release() insted of ::destroy()

In reply to Yi Liu

This is to avoid a circular refcount problem between the kvm struct and the device file. KVM modules holds device/group file reference when the device/group is added and releases it per removal or the last kvm reference is released. This reference model is ok for the group since there is no kvm reference in the group paths.

But it is a problem for device file since the vfio devices may get kvm reference in the device open path and put it in the device file release. e.g. Intel kvmgt. This would result in a circular issue since the kvm side won't put the device file reference if kvm reference is not 0, while the vfio device side needs to put kvm reference in the release callback.

To solve this problem for device file, let vfio provide release() which would be called once kvm file is closed, it won't depend on the last kvm reference. Hence avoid circular refcount problem.

---
 virt/kvm/vfio.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
virt/kvm/vfio.c +1 -1
index 0f54b9d308d7..525efe37ab6d 100644
@@ -364,7 +364,7 @@ static int kvm_vfio_create(struct kvm_device *dev, u32 type);
 static struct kvm_device_ops kvm_vfio_ops = {
 	.name = "kvm-vfio",
 	.create = kvm_vfio_create,
-	.destroy = kvm_vfio_destroy,
+	.release = kvm_vfio_destroy,
 	.set_attr = kvm_vfio_set_attr,
 	.has_attr = kvm_vfio_has_attr,
 };
Signature · 1 line
2.34.1
maintainer
#17 lore raw

RE: [PATCH 05/13] kvm/vfio: Provide struct kvm_device_ops::release() insted of ::destroy()

virt/kvm/vfio.c static int kvm_vfio_create(struct kvm_device *dev, · line 368

Quoted 10 lines

From: Liu, Yi L <[email protected]>
Sent: Tuesday, January 17, 2023 9:50 PM

virt/kvm/vfio.c +1 -1
@@ -364,7 +364,7 @@ static int kvm_vfio_create(struct kvm_device *dev,
 u32 type);
 static struct kvm_device_ops kvm_vfio_ops = {
 	.name = "kvm-vfio",
 	.create = kvm_vfio_create,
-	.destroy = kvm_vfio_destroy,
+	.release = kvm_vfio_destroy,

Also rename to kvm_vfio_release.

#18 lore raw

Re: [PATCH 05/13] kvm/vfio: Provide struct kvm_device_ops::release() insted of ::destroy()

In reply to Yi Liu

Hi Yi,

Quoted 27 lines · Yi Liu

This is to avoid a circular refcount problem between the kvm struct and the device file. KVM modules holds device/group file reference when the device/group is added and releases it per removal or the last kvm reference is released. This reference model is ok for the group since there is no kvm reference in the group paths.

But it is a problem for device file since the vfio devices may get kvm reference in the device open path and put it in the device file release. e.g. Intel kvmgt. This would result in a circular issue since the kvm side won't put the device file reference if kvm reference is not 0, while the vfio device side needs to put kvm reference in the release callback.

To solve this problem for device file, let vfio provide release() which would be called once kvm file is closed, it won't depend on the last kvm reference. Hence avoid circular refcount problem.

---
 virt/kvm/vfio.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
virt/kvm/vfio.c +0 -0
index 0f54b9d308d7..525efe37ab6d 100644

virt/kvm/vfio.c static int kvm_vfio_create(struct kvm_device *dev, u32 type); · line 367

Quoted 4 lines
virt/kvm/vfio.c +0 -1
@@ -364,7 +364,7 @@ static int kvm_vfio_create(struct kvm_device *dev, u32 type);
 static struct kvm_device_ops kvm_vfio_ops = {
 	.name = "kvm-vfio",
 	.create = kvm_vfio_create,
-	.destroy = kvm_vfio_destroy,

Is it safe to simply remove the destroy cb as it is called from kvm_destroy_vm/kvm_destroy_devices?

Thanks

Eric

Quoted 4 lines
virt/kvm/vfio.c +1 -0
+	.release = kvm_vfio_destroy,
 	.set_attr = kvm_vfio_set_attr,
 	.has_attr = kvm_vfio_has_attr,
 };
maintainer
#19 lore raw

RE: [PATCH 05/13] kvm/vfio: Provide struct kvm_device_ops::release() insted of ::destroy()

Quoted 34 lines

From: Eric Auger <[email protected]>
Sent: Thursday, January 19, 2023 5:13 PM

Hi Yi,

virt/kvm/vfio.c static int kvm_vfio_create(struct kvm_device *dev,

Quoted 27 lines · Yi Liu

This is to avoid a circular refcount problem between the kvm struct and the device file. KVM modules holds device/group file reference when the device/group is added and releases it per removal or the last kvm reference is released. This reference model is ok for the group since there is no kvm reference in the group paths.

But it is a problem for device file since the vfio devices may get kvm reference in the device open path and put it in the device file release. e.g. Intel kvmgt. This would result in a circular issue since the kvm side won't put the device file reference if kvm reference is not 0, while the vfio device side needs to put kvm reference in the release callback.

To solve this problem for device file, let vfio provide release() which would be called once kvm file is closed, it won't depend on the last kvm reference. Hence avoid circular refcount problem.

---
 virt/kvm/vfio.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
virt/kvm/vfio.c +0 -0
index 0f54b9d308d7..525efe37ab6d 100644
@@ -364,7 +364,7 @@ static int kvm_vfio_create(struct kvm_device *dev,

u32 type);

Quoted 7 lines

virt/kvm/vfio.c static int kvm_vfio_create(struct kvm_device *dev, u32 type); · line 367

Quoted 4 lines
virt/kvm/vfio.c +0 -1
 static struct kvm_device_ops kvm_vfio_ops = {
 	.name = "kvm-vfio",
 	.create = kvm_vfio_create,
-	.destroy = kvm_vfio_destroy,

Is it safe to simply remove the destroy cb as it is called from kvm_destroy_vm/kvm_destroy_devices?

According to the definition .release is considered as an alternative method to free the device:

/*
* Destroy is responsible for freeing dev.
*
* Destroy may be called before or after destructors are called
* on emulated I/O regions, depending on whether a reference is
* held by a vcpu or other kvm component that gets destroyed
* after the emulated I/O.
*/
void (*destroy)(struct kvm_device *dev);

/*
* Release is an alternative method to free the device. It is
* called when the device file descriptor is closed. Once
* release is called, the destroy method will not be called
* anymore as the device is removed from the device list of
* the VM. kvm->lock is held.
*/
void (*release)(struct kvm_device *dev);

Did you see any specific problem of moving this stuff to release?

author
#20 lore raw

RE: [PATCH 05/13] kvm/vfio: Provide struct kvm_device_ops::release() insted of ::destroy()

Quoted 33 lines

From: Tian, Kevin <[email protected]>
Sent: Thursday, January 19, 2023 5:30 PM

Quoted 8 lines

From: Eric Auger <[email protected]>
Sent: Thursday, January 19, 2023 5:13 PM

Hi Yi,

Quoted 2 lines · Yi Liu

This is to avoid a circular refcount problem between the kvm struct and the device file. KVM modules holds device/group file reference when

the

Quoted 1 line
Quoted 1 line

device/group is added and releases it per removal or the last kvm

reference

Quoted 19 lines
Quoted 19 lines

is released. This reference model is ok for the group since there is no kvm reference in the group paths.

But it is a problem for device file since the vfio devices may get kvm reference in the device open path and put it in the device file release. e.g. Intel kvmgt. This would result in a circular issue since the kvm side won't put the device file reference if kvm reference is not 0, while the vfio device side needs to put kvm reference in the release callback.

To solve this problem for device file, let vfio provide release() which would be called once kvm file is closed, it won't depend on the last kvm reference. Hence avoid circular refcount problem.

---
 virt/kvm/vfio.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
Quoted 14 lines
Quoted 5 lines

virt/kvm/vfio.c static int kvm_vfio_create(struct kvm_device

Quoted 5 lines
virt/kvm/vfio.c +0 -0
index 0f54b9d308d7..525efe37ab6d 100644
@@ -364,7 +364,7 @@ static int kvm_vfio_create(struct kvm_device

*dev,

Quoted 8 lines

u32 type);

virt/kvm/vfio.c static int kvm_vfio_create(struct kvm_device *dev, u32 type); · line 367

Quoted 4 lines
virt/kvm/vfio.c +0 -1
 static struct kvm_device_ops kvm_vfio_ops = {
 	.name = "kvm-vfio",
 	.create = kvm_vfio_create,
-	.destroy = kvm_vfio_destroy,

Is it safe to simply remove the destroy cb as it is called from kvm_destroy_vm/kvm_destroy_devices?

Perhaps better to keep it. kvm_vfio_device is only one kind of kvm_device_type For kvm_vfio_device, it is now considered to better provide a release cb. While other kvm_device may better to have destroy cb.

Quoted 23 lines

According to the definition .release is considered as an alternative method to free the device:

/*
* Destroy is responsible for freeing dev.
*
* Destroy may be called before or after destructors are called
* on emulated I/O regions, depending on whether a reference is
* held by a vcpu or other kvm component that gets destroyed
* after the emulated I/O.
*/
void (*destroy)(struct kvm_device *dev);

/*
* Release is an alternative method to free the device. It is
* called when the device file descriptor is closed. Once
* release is called, the destroy method will not be called
* anymore as the device is removed from the device list of
* the VM. kvm->lock is held.
*/
void (*release)(struct kvm_device *dev);

Quoted 1 line

Did you see any specific problem of moving this stuff to release?

It should only affect kvm_vfio_device itself. 😊

Regards,
Yi Liu

maintainer Reviewed-by on v1 5/13
#21 lore raw

Re: [PATCH 05/13] kvm/vfio: Provide struct kvm_device_ops::release() insted of ::destroy()

In reply to Yi Liu
Quoted 16 lines · Yi Liu

This is to avoid a circular refcount problem between the kvm struct and the device file. KVM modules holds device/group file reference when the device/group is added and releases it per removal or the last kvm reference is released. This reference model is ok for the group since there is no kvm reference in the group paths.

But it is a problem for device file since the vfio devices may get kvm reference in the device open path and put it in the device file release. e.g. Intel kvmgt. This would result in a circular issue since the kvm side won't put the device file reference if kvm reference is not 0, while the vfio device side needs to put kvm reference in the release callback.

To solve this problem for device file, let vfio provide release() which would be called once kvm file is closed, it won't depend on the last kvm reference. Hence avoid circular refcount problem.

Quoted 5 lines
---
 virt/kvm/vfio.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

From Alex's remarks please revise the commit message and add a Fixes line of some kind that this solves the deadlock Matthew was working on, and send it stand alone right away

Jason

#22 lore raw

Re: [PATCH 05/13] kvm/vfio: Provide struct kvm_device_ops::release() insted of ::destroy()

Quoted 23 lines · On Thu, 19 Jan 2023 15:07:01 -0400 Jason Gunthorpe <[email protected]> wrote:
Quoted 16 lines · Yi Liu

This is to avoid a circular refcount problem between the kvm struct and the device file. KVM modules holds device/group file reference when the device/group is added and releases it per removal or the last kvm reference is released. This reference model is ok for the group since there is no kvm reference in the group paths.

But it is a problem for device file since the vfio devices may get kvm reference in the device open path and put it in the device file release. e.g. Intel kvmgt. This would result in a circular issue since the kvm side won't put the device file reference if kvm reference is not 0, while the vfio device side needs to put kvm reference in the release callback.

To solve this problem for device file, let vfio provide release() which would be called once kvm file is closed, it won't depend on the last kvm reference. Hence avoid circular refcount problem.

Quoted 5 lines
---
 virt/kvm/vfio.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)  
Quoted 5 lines

From Alex's remarks please revise the commit message and add a Fixes line of some kind that this solves the deadlock Matthew was working on, and send it stand alone right away

Also revise the commit log since we'll be taking a reference in the group model as well. The function and comments should also be updated s/destroy/release/. Thanks,

Alex

author
#23 lore raw

RE: [PATCH 05/13] kvm/vfio: Provide struct kvm_device_ops::release() insted of ::destroy()

In reply to Jason Gunthorpe
Quoted 27 lines

From: Jason Gunthorpe <[email protected]>
Sent: Friday, January 20, 2023 3:07 AM

Quoted 3 lines · Yi Liu

This is to avoid a circular refcount problem between the kvm struct and the device file. KVM modules holds device/group file reference when the device/group is added and releases it per removal or the last kvm

reference

Quoted 13 lines

is released. This reference model is ok for the group since there is no kvm reference in the group paths.

But it is a problem for device file since the vfio devices may get kvm reference in the device open path and put it in the device file release. e.g. Intel kvmgt. This would result in a circular issue since the kvm side won't put the device file reference if kvm reference is not 0, while the vfio device side needs to put kvm reference in the release callback.

To solve this problem for device file, let vfio provide release() which would be called once kvm file is closed, it won't depend on the last kvm reference. Hence avoid circular refcount problem.

Quoted 5 lines
---
 virt/kvm/vfio.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
Quoted 5 lines

From Alex's remarks please revise the commit message and add a Fixes line of some kind that this solves the deadlock Matthew was working on, and send it stand alone right away

Sure. 😊 I'll rename the patch subject and commit message.

Regards,
Yi Liu

author
#24 lore raw

RE: [PATCH 05/13] kvm/vfio: Provide struct kvm_device_ops::release() insted of ::destroy()

In reply to Jason Gunthorpe
Quoted 27 lines

From: Jason Gunthorpe <[email protected]>
Sent: Friday, January 20, 2023 3:07 AM

Quoted 3 lines · Yi Liu

This is to avoid a circular refcount problem between the kvm struct and the device file. KVM modules holds device/group file reference when the device/group is added and releases it per removal or the last kvm

reference

Quoted 13 lines

is released. This reference model is ok for the group since there is no kvm reference in the group paths.

But it is a problem for device file since the vfio devices may get kvm reference in the device open path and put it in the device file release. e.g. Intel kvmgt. This would result in a circular issue since the kvm side won't put the device file reference if kvm reference is not 0, while the vfio device side needs to put kvm reference in the release callback.

To solve this problem for device file, let vfio provide release() which would be called once kvm file is closed, it won't depend on the last kvm reference. Hence avoid circular refcount problem.

Quoted 5 lines
---
 virt/kvm/vfio.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
Quoted 5 lines

From Alex's remarks please revise the commit message and add a Fixes line of some kind that this solves the deadlock Matthew was working on, and send it stand alone right away

Hi Kevin, Jason,

I got a minor question. Let me check your opinions.

So after this change. Say we have thread A, which is the kvm-vfio device release. It will hold the kvm_lock and delete the kvm-vfio device from the kvm-device list. It will also call into vfio to set KVM==NULL. So it will try to hold group_lock. The locking order is kvm_lock -> group_lock.

Say in the same time, we have thread B closes device, it will hold group_lock first and then calls kvm_put_kvm() which is the last reference, then it would loop the kvm-device list. Currently, it is not holding kvm_lock. But it also manipulating the kvm-device list, should it hold kvm_lock? If yes, then the locking order is group_lock -> kvm_lock. Then we will have A-B B-A lock issue.

Regards,
Yi Liu

maintainer
#25 lore raw

Re: [PATCH 05/13] kvm/vfio: Provide struct kvm_device_ops::release() insted of ::destroy()

Quoted 5 lines · Yi L

Say in the same time, we have thread B closes device, it will hold group_lock first and then calls kvm_put_kvm() which is the last reference, then it would loop the kvm-device list. Currently, it is not holding kvm_lock. But it also manipulating the kvm-device list, should it hold kvm_lock?

No. When using refcounts if the refcount is 0 it guarantees there are no other threads that can possibly touch this memory, so any locks internal to the memory are not required.

Jason

author
#26 lore raw

RE: [PATCH 05/13] kvm/vfio: Provide struct kvm_device_ops::release() insted of ::destroy()

Quoted 10 lines

From: Jason Gunthorpe <[email protected]>
Sent: Friday, January 20, 2023 10:34 PM

Quoted 5 lines · Yi L

Say in the same time, we have thread B closes device, it will hold group_lock first and then calls kvm_put_kvm() which is the last reference, then it would loop the kvm-device list. Currently, it is not holding kvm_lock. But it also manipulating the kvm-device list, should it hold kvm_lock?

Quoted 3 lines

No. When using refcounts if the refcount is 0 it guarantees there are no other threads that can possibly touch this memory, so any locks internal to the memory are not required.

Ok. The patch has been sent out standalone.

https://lore.kernel.org/kvm/[email protected]/T/#u

Regards,
Yi Liu

author
#27 lore raw

RE: [PATCH 05/13] kvm/vfio: Provide struct kvm_device_ops::release() insted of ::destroy()

Quoted 13 lines

From: Liu, Yi L <[email protected]>
Sent: Friday, January 20, 2023 11:10 PM

Quoted 10 lines

From: Jason Gunthorpe <[email protected]>
Sent: Friday, January 20, 2023 10:34 PM

Quoted 5 lines · Yi L

Say in the same time, we have thread B closes device, it will hold group_lock first and then calls kvm_put_kvm() which is the last reference, then it would loop the kvm-device list. Currently, it is not holding kvm_lock. But it also manipulating the kvm-device list, should it hold kvm_lock?

Quoted 8 lines
Quoted 3 lines

No. When using refcounts if the refcount is 0 it guarantees there are no other threads that can possibly touch this memory, so any locks internal to the memory are not required.

Ok. The patch has been sent out standalone.

https://lore.kernel.org/kvm/20230114000351.115444-1-
[email protected]/T/#u

Wrong link. Below is the correct one. 😊

https://lore.kernel.org/kvm/[email protected]/T/#u

Regards,
Yi Liu

author
#28 lore raw

PATCH 6/13 kvm/vfio: Accept vfio device file from userspace

In reply to Yi Liu

This defines KVM_DEV_VFIO_FILE* and make alias with KVM_DEV_VFIO_GROUP*. Old userspace uses KVM_DEV_VFIO_GROUP* works as well.

---
 Documentation/virt/kvm/devices/vfio.rst | 32 ++++++++++++-------------
 include/uapi/linux/kvm.h                | 23 +++++++++++++-----
 virt/kvm/vfio.c                         | 18 +++++++-------
 3 files changed, 42 insertions(+), 31 deletions(-)
Documentation/virt/kvm/devices/vfio.rst +16 -16
index 2d20dc561069..ac4300ded398 100644
@@ -9,23 +9,23 @@ Device types supported:
   - KVM_DEV_TYPE_VFIO
 
 Only one VFIO instance may be created per VM.  The created device
-tracks VFIO groups in use by the VM and features of those groups
-important to the correctness and acceleration of the VM.  As groups
-are enabled and disabled for use by the VM, KVM should be updated
-about their presence.  When registered with KVM, a reference to the
-VFIO-group is held by KVM.
-
-Groups:
-  KVM_DEV_VFIO_GROUP
-
-KVM_DEV_VFIO_GROUP attributes:
-  KVM_DEV_VFIO_GROUP_ADD: Add a VFIO group to VFIO-KVM device tracking
-	kvm_device_attr.addr points to an int32_t file descriptor
+tracks VFIO files (group or device) in use by the VM and features
+of those groups/devices important to the correctness and acceleration
+of the VM.  As groups/device are enabled and disabled for use by the
+VM, KVM should be updated about their presence.  When registered with
+KVM, a reference to the VFIO file is held by KVM.
+
+VFIO Files:
+  KVM_DEV_VFIO_FILE
+
+KVM_DEV_VFIO_FILE attributes:
+  KVM_DEV_VFIO_FILE_ADD: Add a VFIO file (group/device) to VFIO-KVM device
+	tracking kvm_device_attr.addr points to an int32_t file descriptor
+	for the VFIO file.
+  KVM_DEV_VFIO_FILE_DEL: Remove a VFIO file (group/device) from VFIO-KVM device
+	tracking kvm_device_attr.addr points to an int32_t file descriptor
 	for the VFIO group.
-  KVM_DEV_VFIO_GROUP_DEL: Remove a VFIO group from VFIO-KVM device tracking
-	kvm_device_attr.addr points to an int32_t file descriptor
-	for the VFIO group.
-  KVM_DEV_VFIO_GROUP_SET_SPAPR_TCE: attaches a guest visible TCE table
+  KVM_DEV_VFIO_FILE_SET_SPAPR_TCE: attaches a guest visible TCE table
 	allocated by sPAPR KVM.
 	kvm_device_attr.addr points to a struct::
 
include/uapi/linux/kvm.h +17 -6
index 55155e262646..ad36e144a41d 100644
@@ -1396,15 +1396,26 @@ struct kvm_create_device {
 
 struct kvm_device_attr {
 	__u32	flags;		/* no flags currently defined */
-	__u32	group;		/* device-defined */
-	__u64	attr;		/* group-defined */
+	union {
+		__u32	group;
+		__u32	file;
+	}; /* device-defined */
+	__u64	attr;		/* VFIO-file-defined or group-defined */
 	__u64	addr;		/* userspace address of attr data */
 };
 
-#define  KVM_DEV_VFIO_GROUP			1
-#define   KVM_DEV_VFIO_GROUP_ADD			1
-#define   KVM_DEV_VFIO_GROUP_DEL			2
-#define   KVM_DEV_VFIO_GROUP_SET_SPAPR_TCE		3
+#define  KVM_DEV_VFIO_FILE	1
+
+#define   KVM_DEV_VFIO_FILE_ADD			1
+#define   KVM_DEV_VFIO_FILE_DEL			2
+#define   KVM_DEV_VFIO_FILE_SET_SPAPR_TCE	3
+
+/* Group aliases are for compile time uapi compatibility */
+#define  KVM_DEV_VFIO_GROUP	KVM_DEV_VFIO_FILE
+
+#define   KVM_DEV_VFIO_GROUP_ADD	KVM_DEV_VFIO_FILE_ADD
+#define   KVM_DEV_VFIO_GROUP_DEL	KVM_DEV_VFIO_FILE_DEL
+#define   KVM_DEV_VFIO_GROUP_SET_SPAPR_TCE	KVM_DEV_VFIO_FILE_SET_SPAPR_TCE
 
 enum kvm_device_type {
 	KVM_DEV_TYPE_FSL_MPIC_20	= 1,
virt/kvm/vfio.c +9 -9
index 525efe37ab6d..e73ca60af3ae 100644
@@ -286,18 +286,18 @@ static int kvm_vfio_set_file(struct kvm_device *dev, long attr,
 	int32_t fd;
 
 	switch (attr) {
-	case KVM_DEV_VFIO_GROUP_ADD:
+	case KVM_DEV_VFIO_FILE_ADD:
 		if (get_user(fd, argp))
 			return -EFAULT;
 		return kvm_vfio_file_add(dev, fd);
 
-	case KVM_DEV_VFIO_GROUP_DEL:
+	case KVM_DEV_VFIO_FILE_DEL:
 		if (get_user(fd, argp))
 			return -EFAULT;
 		return kvm_vfio_file_del(dev, fd);
 
 #ifdef CONFIG_SPAPR_TCE_IOMMU
-	case KVM_DEV_VFIO_GROUP_SET_SPAPR_TCE:
+	case KVM_DEV_VFIO_FILE_SET_SPAPR_TCE:
 		return kvm_vfio_file_set_spapr_tce(dev, arg);
 #endif
 	}
@@ -309,7 +309,7 @@ static int kvm_vfio_set_attr(struct kvm_device *dev,
 			     struct kvm_device_attr *attr)
 {
 	switch (attr->group) {
-	case KVM_DEV_VFIO_GROUP:
+	case KVM_DEV_VFIO_FILE:
 		return kvm_vfio_set_file(dev, attr->attr,
 					 u64_to_user_ptr(attr->addr));
 	}
@@ -320,13 +320,13 @@ static int kvm_vfio_set_attr(struct kvm_device *dev,
 static int kvm_vfio_has_attr(struct kvm_device *dev,
 			     struct kvm_device_attr *attr)
 {
-	switch (attr->group) {
-	case KVM_DEV_VFIO_GROUP:
+	switch (attr->file) {
+	case KVM_DEV_VFIO_FILE:
 		switch (attr->attr) {
-		case KVM_DEV_VFIO_GROUP_ADD:
-		case KVM_DEV_VFIO_GROUP_DEL:
+		case KVM_DEV_VFIO_FILE_ADD:
+		case KVM_DEV_VFIO_FILE_DEL:
 #ifdef CONFIG_SPAPR_TCE_IOMMU
-		case KVM_DEV_VFIO_GROUP_SET_SPAPR_TCE:
+		case KVM_DEV_VFIO_FILE_SET_SPAPR_TCE:
 #endif
 			return 0;
 		}
Signature · 1 line
2.34.1
maintainer
#29 lore raw

RE: [PATCH 06/13] kvm/vfio: Accept vfio device file from userspace

Documentation/virt/kvm/devices/vfio.rst Device types supported: · line 14

Quoted 6 lines

From: Liu, Yi L <[email protected]>
Sent: Tuesday, January 17, 2023 9:50 PM

Documentation/virt/kvm/devices/vfio.rst +3 -0
+tracks VFIO files (group or device) in use by the VM and features
+of those groups/devices important to the correctness and acceleration
+of the VM.  As groups/device are enabled and disabled for use by the

"groups/devices"

Review

Quoted 4 lines

+ KVM_DEV_VFIO_FILE_DEL: Remove a VFIO file (group/device) from VFIO- KVM device

(quoted diff) +1 -0
+	tracking kvm_device_attr.addr points to an int32_t file descriptor
 	for the VFIO group.

"for the VFIO file"

Documentation/virt/kvm/devices/vfio.rst Device types supported: · line 30

Quoted 9 lines

- KVM_DEV_VFIO_GROUP_DEL: Remove a VFIO group from VFIO-KVM device tracking

Documentation/virt/kvm/devices/vfio.rst +0 -3
-	kvm_device_attr.addr points to an int32_t file descriptor
-	for the VFIO group.
-  KVM_DEV_VFIO_GROUP_SET_SPAPR_TCE: attaches a guest visible TCE

table

Documentation/virt/kvm/devices/vfio.rst +1 -0
+  KVM_DEV_VFIO_FILE_SET_SPAPR_TCE: attaches a guest visible TCE table
 	allocated by sPAPR KVM.
 	kvm_device_attr.addr points to a struct::

btw do we want to mention the GROUP cmd alias here instead of simply removing them?

include/uapi/linux/kvm.h struct kvm_create_device { · line 1403

Quoted 11 lines
include/uapi/linux/kvm.h +5 -2
@@ -1396,15 +1396,26 @@ struct kvm_create_device {
 
 struct kvm_device_attr {
 	__u32	flags;		/* no flags currently defined */
-	__u32	group;		/* device-defined */
-	__u64	attr;		/* group-defined */
+	union {
+		__u32	group;
+		__u32	file;
+	}; /* device-defined */
+	__u64	attr;		/* VFIO-file-defined or group-defined */

remove "VFIO-"

#30 lore raw

Re: [PATCH 06/13] kvm/vfio: Accept vfio device file from userspace

In reply to Yi Liu

Hi Yi,

Quoted 59 lines · Yi Liu

This defines KVM_DEV_VFIO_FILE* and make alias with KVM_DEV_VFIO_GROUP*. Old userspace uses KVM_DEV_VFIO_GROUP* works as well.

---
 Documentation/virt/kvm/devices/vfio.rst | 32 ++++++++++++-------------
 include/uapi/linux/kvm.h                | 23 +++++++++++++-----
 virt/kvm/vfio.c                         | 18 +++++++-------
 3 files changed, 42 insertions(+), 31 deletions(-)
Documentation/virt/kvm/devices/vfio.rst +16 -16
index 2d20dc561069..ac4300ded398 100644
@@ -9,23 +9,23 @@ Device types supported:
   - KVM_DEV_TYPE_VFIO
 
 Only one VFIO instance may be created per VM.  The created device
-tracks VFIO groups in use by the VM and features of those groups
-important to the correctness and acceleration of the VM.  As groups
-are enabled and disabled for use by the VM, KVM should be updated
-about their presence.  When registered with KVM, a reference to the
-VFIO-group is held by KVM.
-
-Groups:
-  KVM_DEV_VFIO_GROUP
-
-KVM_DEV_VFIO_GROUP attributes:
-  KVM_DEV_VFIO_GROUP_ADD: Add a VFIO group to VFIO-KVM device tracking
-	kvm_device_attr.addr points to an int32_t file descriptor
+tracks VFIO files (group or device) in use by the VM and features
+of those groups/devices important to the correctness and acceleration
+of the VM.  As groups/device are enabled and disabled for use by the
+VM, KVM should be updated about their presence.  When registered with
+KVM, a reference to the VFIO file is held by KVM.
+
+VFIO Files:
+  KVM_DEV_VFIO_FILE
+
+KVM_DEV_VFIO_FILE attributes:
+  KVM_DEV_VFIO_FILE_ADD: Add a VFIO file (group/device) to VFIO-KVM device
+	tracking kvm_device_attr.addr points to an int32_t file descriptor
+	for the VFIO file.
+  KVM_DEV_VFIO_FILE_DEL: Remove a VFIO file (group/device) from VFIO-KVM device
+	tracking kvm_device_attr.addr points to an int32_t file descriptor
 	for the VFIO group.
-  KVM_DEV_VFIO_GROUP_DEL: Remove a VFIO group from VFIO-KVM device tracking
-	kvm_device_attr.addr points to an int32_t file descriptor
-	for the VFIO group.
-  KVM_DEV_VFIO_GROUP_SET_SPAPR_TCE: attaches a guest visible TCE table
+  KVM_DEV_VFIO_FILE_SET_SPAPR_TCE: attaches a guest visible TCE table
 	allocated by sPAPR KVM.
 	kvm_device_attr.addr points to a struct::
 
include/uapi/linux/kvm.h +0 -0
index 55155e262646..ad36e144a41d 100644

include/uapi/linux/kvm.h struct kvm_create_device { · line 1403

Quoted 10 lines
include/uapi/linux/kvm.h +5 -2
@@ -1396,15 +1396,26 @@ struct kvm_create_device {
 
 struct kvm_device_attr {
 	__u32	flags;		/* no flags currently defined */
-	__u32	group;		/* device-defined */
-	__u64	attr;		/* group-defined */
+	union {
+		__u32	group;
+		__u32	file;
+	}; /* device-defined */
+	__u64	attr;		/* VFIO-file-defined or group-defined */

I think there is a confusion here between the 'VFIO group' terminology and the 'kvm device group' terminology. Commands for kvm devices are gathered in groups and within groups you have sub-commands called attributes.

See Documentation/virt/kvm/devices/arm-vgic-v3.rst for instance. So to me this shall be left unchanged.

Quoted 66 lines
include/uapi/linux/kvm.h +12 -4
 	__u64	addr;		/* userspace address of attr data */
 };
 
-#define  KVM_DEV_VFIO_GROUP			1
-#define   KVM_DEV_VFIO_GROUP_ADD			1
-#define   KVM_DEV_VFIO_GROUP_DEL			2
-#define   KVM_DEV_VFIO_GROUP_SET_SPAPR_TCE		3
+#define  KVM_DEV_VFIO_FILE	1
+
+#define   KVM_DEV_VFIO_FILE_ADD			1
+#define   KVM_DEV_VFIO_FILE_DEL			2
+#define   KVM_DEV_VFIO_FILE_SET_SPAPR_TCE	3
+
+/* Group aliases are for compile time uapi compatibility */
+#define  KVM_DEV_VFIO_GROUP	KVM_DEV_VFIO_FILE
+
+#define   KVM_DEV_VFIO_GROUP_ADD	KVM_DEV_VFIO_FILE_ADD
+#define   KVM_DEV_VFIO_GROUP_DEL	KVM_DEV_VFIO_FILE_DEL
+#define   KVM_DEV_VFIO_GROUP_SET_SPAPR_TCE	KVM_DEV_VFIO_FILE_SET_SPAPR_TCE
 
 enum kvm_device_type {
 	KVM_DEV_TYPE_FSL_MPIC_20	= 1,
virt/kvm/vfio.c +6 -6
index 525efe37ab6d..e73ca60af3ae 100644
@@ -286,18 +286,18 @@ static int kvm_vfio_set_file(struct kvm_device *dev, long attr,
 	int32_t fd;
 
 	switch (attr) {
-	case KVM_DEV_VFIO_GROUP_ADD:
+	case KVM_DEV_VFIO_FILE_ADD:
 		if (get_user(fd, argp))
 			return -EFAULT;
 		return kvm_vfio_file_add(dev, fd);
 
-	case KVM_DEV_VFIO_GROUP_DEL:
+	case KVM_DEV_VFIO_FILE_DEL:
 		if (get_user(fd, argp))
 			return -EFAULT;
 		return kvm_vfio_file_del(dev, fd);
 
 #ifdef CONFIG_SPAPR_TCE_IOMMU
-	case KVM_DEV_VFIO_GROUP_SET_SPAPR_TCE:
+	case KVM_DEV_VFIO_FILE_SET_SPAPR_TCE:
 		return kvm_vfio_file_set_spapr_tce(dev, arg);
 #endif
 	}
@@ -309,7 +309,7 @@ static int kvm_vfio_set_attr(struct kvm_device *dev,
 			     struct kvm_device_attr *attr)
 {
 	switch (attr->group) {
-	case KVM_DEV_VFIO_GROUP:
+	case KVM_DEV_VFIO_FILE:
 		return kvm_vfio_set_file(dev, attr->attr,
 					 u64_to_user_ptr(attr->addr));
 	}
@@ -320,13 +320,13 @@ static int kvm_vfio_set_attr(struct kvm_device *dev,
 static int kvm_vfio_has_attr(struct kvm_device *dev,
 			     struct kvm_device_attr *attr)
 {
-	switch (attr->group) {
-	case KVM_DEV_VFIO_GROUP:
+	switch (attr->file) {
+	case KVM_DEV_VFIO_FILE:
 		switch (attr->attr) {

virt/kvm/vfio.c static int kvm_vfio_set_attr(struct kvm_device *dev, · line 332

Quoted 10 lines
virt/kvm/vfio.c +3 -3
-		case KVM_DEV_VFIO_GROUP_ADD:
-		case KVM_DEV_VFIO_GROUP_DEL:
+		case KVM_DEV_VFIO_FILE_ADD:
+		case KVM_DEV_VFIO_FILE_DEL:
 #ifdef CONFIG_SPAPR_TCE_IOMMU
-		case KVM_DEV_VFIO_GROUP_SET_SPAPR_TCE:
+		case KVM_DEV_VFIO_FILE_SET_SPAPR_TCE:
 #endif
 			return 0;
 		}

Thanks

Eric

author
#31 lore raw

RE: [PATCH 06/13] kvm/vfio: Accept vfio device file from userspace

Quoted 18 lines

From: Eric Auger <[email protected]>
Sent: Thursday, January 19, 2023 5:35 PM
Hi Yi,

Quoted 1 line · Yi Liu

This defines KVM_DEV_VFIO_FILE* and make alias with

KVM_DEV_VFIO_GROUP*.

Quoted 11 lines

Old userspace uses KVM_DEV_VFIO_GROUP* works as well.

---

include/uapi/linux/kvm.h +0 -0
index 55155e262646..ad36e144a41d 100644
@@ -1396,15 +1396,26 @@ struct kvm_create_device {
 
Quoted 13 lines

include/uapi/linux/kvm.h struct kvm_create_device { · line 1403

Quoted 9 lines
include/uapi/linux/kvm.h +5 -2
 struct kvm_device_attr {
 	__u32	flags;		/* no flags currently defined */
-	__u32	group;		/* device-defined */
-	__u64	attr;		/* group-defined */
+	union {
+		__u32	group;
+		__u32	file;
+	}; /* device-defined */
+	__u64	attr;		/* VFIO-file-defined or group-defined */

I think there is a confusion here between the 'VFIO group' terminology and the 'kvm device group' terminology. Commands for kvm devices are gathered in groups and within groups you have sub-commands called attributes.

You are right 😊 will fix it in next version. So even the union is not needed.

Regards,
Yi Liu

author
#32 lore raw

PATCH 7/13 vfio: Pass struct vfio_device_file * to vfio_device_open/close()

In reply to Yi Liu

This avoids passing struct kvm * and struct iommufd_ctx * in multiple functions. vfio_device_open() becomes to be a locked helper.

---
 drivers/vfio/group.c     | 34 +++++++++++++++++++++++++---------
 drivers/vfio/vfio.h      | 10 +++++-----
 drivers/vfio/vfio_main.c | 40 ++++++++++++++++++++++++----------------
 3 files changed, 54 insertions(+), 30 deletions(-)
drivers/vfio/group.c +25 -9
index d83cf069d290..7200304663e5 100644
@@ -154,33 +154,49 @@ static int vfio_group_ioctl_set_container(struct vfio_group *group,
 	return ret;
 }
 
-static int vfio_device_group_open(struct vfio_device *device)
+static int vfio_device_group_open(struct vfio_device_file *df)
 {
+	struct vfio_device *device = df->device;
 	int ret;
 
 	mutex_lock(&device->group->group_lock);
 	if (!vfio_group_has_iommu(device->group)) {
 		ret = -EINVAL;
-		goto out_unlock;
+		goto err_unlock_group;
 	}
 
+	mutex_lock(&device->dev_set->lock);
 	/*
 	 * Here we pass the KVM pointer with the group under the lock.  If the
 	 * device driver will use it, it must obtain a reference and release it
 	 * during close_device.
 	 */
-	ret = vfio_device_open(device, device->group->iommufd,
-			       device->group->kvm);
+	df->kvm = device->group->kvm;
+	df->iommufd = device->group->iommufd;
+
+	ret = vfio_device_open(df);
+	if (ret)
+		goto err_unlock_device;
+	mutex_unlock(&device->dev_set->lock);
 
-out_unlock:
+	mutex_unlock(&device->group->group_lock);
+	return 0;
+
+err_unlock_device:
+	df->kvm = NULL;
+	df->iommufd = NULL;
+	mutex_unlock(&device->dev_set->lock);
+err_unlock_group:
 	mutex_unlock(&device->group->group_lock);
 	return ret;
 }
 
-void vfio_device_group_close(struct vfio_device *device)
+void vfio_device_group_close(struct vfio_device_file *df)
 {
+	struct vfio_device *device = df->device;
+
 	mutex_lock(&device->group->group_lock);
-	vfio_device_close(device, device->group->iommufd);
+	vfio_device_close(df);
 	mutex_unlock(&device->group->group_lock);
 }
 
@@ -196,7 +212,7 @@ static struct file *vfio_device_open_file(struct vfio_device *device)
 		goto err_out;
 	}
 
-	ret = vfio_device_group_open(device);
+	ret = vfio_device_group_open(df);
 	if (ret)
 		goto err_free;
 
@@ -228,7 +244,7 @@ static struct file *vfio_device_open_file(struct vfio_device *device)
 	return filep;
 
 err_close_device:
-	vfio_device_group_close(device);
+	vfio_device_group_close(df);
 err_free:
 	kfree(df);
 err_out:
drivers/vfio/vfio.h +5 -5
index 53af6e3ea214..3d8ba165146c 100644
@@ -19,14 +19,14 @@ struct vfio_container;
 struct vfio_device_file {
 	struct vfio_device *device;
 	struct kvm *kvm;
+	struct iommufd_ctx *iommufd;
 };
 
 void vfio_device_put_registration(struct vfio_device *device);
 bool vfio_device_try_get_registration(struct vfio_device *device);
-int vfio_device_open(struct vfio_device *device,
-		     struct iommufd_ctx *iommufd, struct kvm *kvm);
-void vfio_device_close(struct vfio_device *device,
-		       struct iommufd_ctx *iommufd);
+int vfio_device_open(struct vfio_device_file *df);
+void vfio_device_close(struct vfio_device_file *device);
+
 struct vfio_device_file *
 vfio_allocate_device_file(struct vfio_device *device);
 
@@ -90,7 +90,7 @@ void vfio_device_group_register(struct vfio_device *device);
 void vfio_device_group_unregister(struct vfio_device *device);
 int vfio_device_group_use_iommu(struct vfio_device *device);
 void vfio_device_group_unuse_iommu(struct vfio_device *device);
-void vfio_device_group_close(struct vfio_device *device);
+void vfio_device_group_close(struct vfio_device_file *df);
 struct vfio_group *vfio_group_from_file(struct file *file);
 bool vfio_group_enforced_coherent(struct vfio_group *group);
 void vfio_group_set_kvm(struct vfio_group *group, struct kvm *kvm);
drivers/vfio/vfio_main.c +24 -16
index dc08d5dd62cc..3df71bd9cd1e 100644
@@ -358,9 +358,11 @@ vfio_allocate_device_file(struct vfio_device *device)
 	return df;
 }
 
-static int vfio_device_first_open(struct vfio_device *device,
-				  struct iommufd_ctx *iommufd, struct kvm *kvm)
+static int vfio_device_first_open(struct vfio_device_file *df)
 {
+	struct vfio_device *device = df->device;
+	struct iommufd_ctx *iommufd = df->iommufd;
+	struct kvm *kvm = df->kvm;
 	int ret;
 
 	lockdep_assert_held(&device->dev_set->lock);
@@ -394,9 +396,11 @@ static int vfio_device_first_open(struct vfio_device *device,
 	return ret;
 }
 
-static void vfio_device_last_close(struct vfio_device *device,
-				   struct iommufd_ctx *iommufd)
+static void vfio_device_last_close(struct vfio_device_file *df)
 {
+	struct vfio_device *device = df->device;
+	struct iommufd_ctx *iommufd = df->iommufd;
+
 	lockdep_assert_held(&device->dev_set->lock);
 
 	if (device->ops->close_device)
@@ -409,30 +413,34 @@ static void vfio_device_last_close(struct vfio_device *device,
 	module_put(device->dev->driver->owner);
 }
 
-int vfio_device_open(struct vfio_device *device,
-		     struct iommufd_ctx *iommufd, struct kvm *kvm)
+int vfio_device_open(struct vfio_device_file *df)
 {
-	int ret = 0;
+	struct vfio_device *device = df->device;
+
+	lockdep_assert_held(&device->dev_set->lock);
 
-	mutex_lock(&device->dev_set->lock);
 	device->open_count++;
 	if (device->open_count == 1) {
-		ret = vfio_device_first_open(device, iommufd, kvm);
-		if (ret)
+		int ret;
+
+		ret = vfio_device_first_open(df);
+		if (ret) {
 			device->open_count--;
+			return ret;
+		}
 	}
-	mutex_unlock(&device->dev_set->lock);
 
-	return ret;
+	return 0;
 }
 
-void vfio_device_close(struct vfio_device *device,
-		       struct iommufd_ctx *iommufd)
+void vfio_device_close(struct vfio_device_file *df)
 {
+	struct vfio_device *device = df->device;
+
 	mutex_lock(&device->dev_set->lock);
 	vfio_assert_device_open(device);
 	if (device->open_count == 1)
-		vfio_device_last_close(device, iommufd);
+		vfio_device_last_close(df);
 	device->open_count--;
 	mutex_unlock(&device->dev_set->lock);
 }
@@ -478,7 +486,7 @@ static int vfio_device_fops_release(struct inode *inode, struct file *filep)
 	struct vfio_device_file *df = filep->private_data;
 	struct vfio_device *device = df->device;
 
-	vfio_device_group_close(device);
+	vfio_device_group_close(df);
 
 	vfio_device_put_registration(device);
 
Signature · 1 line
2.34.1
maintainer Reviewed-by on v1 7/13
#33 lore raw

RE: [PATCH 07/13] vfio: Pass struct vfio_device_file * to vfio_device_open/close()

Quoted 5 lines

From: Liu, Yi L <[email protected]>
Sent: Tuesday, January 17, 2023 9:50 PM

This avoids passing struct kvm * and struct iommufd_ctx * in multiple functions. vfio_device_open() becomes to be a locked helper.

remove "to be"

Quoted 2 lines
#34 lore raw

Re: [PATCH 07/13] vfio: Pass struct vfio_device_file * to vfio_device_open/close()

In reply to Yi Liu

Hi Yi,

Quoted 2 lines · Yi Liu

This avoids passing struct kvm * and struct iommufd_ctx * in multiple functions. vfio_device_open() becomes to be a locked helper.

why? because dev_set lock now protects vfio_device_file fields? worth to explain.
do we need to update the comment in vfio.h related to struct vfio_device_set?

Quoted 20 lines
---
 drivers/vfio/group.c     | 34 +++++++++++++++++++++++++---------
 drivers/vfio/vfio.h      | 10 +++++-----
 drivers/vfio/vfio_main.c | 40 ++++++++++++++++++++++++----------------
 3 files changed, 54 insertions(+), 30 deletions(-)
drivers/vfio/group.c +2 -1
index d83cf069d290..7200304663e5 100644
@@ -154,33 +154,49 @@ static int vfio_group_ioctl_set_container(struct vfio_group *group,
 	return ret;
 }
 
-static int vfio_device_group_open(struct vfio_device *device)
+static int vfio_device_group_open(struct vfio_device_file *df)
 {
+	struct vfio_device *device = df->device;

drivers/vfio/group.c static int vfio_group_ioctl_set_container(struct vfio_group *group, · line 168

Quoted 10 lines
drivers/vfio/group.c +2 -1
 	int ret;
 
 	mutex_lock(&device->group->group_lock);
 	if (!vfio_group_has_iommu(device->group)) {
 		ret = -EINVAL;
-		goto out_unlock;
+		goto err_unlock_group;
 	}
 
+	mutex_lock(&device->dev_set->lock);

is there an explanation somewhere about locking order b/w group_lock, dev_set lock?

drivers/vfio/group.c static int vfio_group_ioctl_set_container(struct vfio_group *group, · line 173

Quoted 5 lines
drivers/vfio/group.c +0 -0
 	/*
 	 * Here we pass the KVM pointer with the group under the lock.  If the
 	 * device driver will use it, it must obtain a reference and release it
 	 * during close_device.
 	 */

May be the opportunity to rephrase the above comment. I am not a native english speaker but the time concordance seems weird + clarify a reference to what.

Quoted 130 lines
drivers/vfio/group.c +21 -7
-	ret = vfio_device_open(device, device->group->iommufd,
-			       device->group->kvm);
+	df->kvm = device->group->kvm;
+	df->iommufd = device->group->iommufd;
+
+	ret = vfio_device_open(df);
+	if (ret)
+		goto err_unlock_device;
+	mutex_unlock(&device->dev_set->lock);
 
-out_unlock:
+	mutex_unlock(&device->group->group_lock);
+	return 0;
+
+err_unlock_device:
+	df->kvm = NULL;
+	df->iommufd = NULL;
+	mutex_unlock(&device->dev_set->lock);
+err_unlock_group:
 	mutex_unlock(&device->group->group_lock);
 	return ret;
 }
 
-void vfio_device_group_close(struct vfio_device *device)
+void vfio_device_group_close(struct vfio_device_file *df)
 {
+	struct vfio_device *device = df->device;
+
 	mutex_lock(&device->group->group_lock);
-	vfio_device_close(device, device->group->iommufd);
+	vfio_device_close(df);
 	mutex_unlock(&device->group->group_lock);
 }
 
@@ -196,7 +212,7 @@ static struct file *vfio_device_open_file(struct vfio_device *device)
 		goto err_out;
 	}
 
-	ret = vfio_device_group_open(device);
+	ret = vfio_device_group_open(df);
 	if (ret)
 		goto err_free;
 
@@ -228,7 +244,7 @@ static struct file *vfio_device_open_file(struct vfio_device *device)
 	return filep;
 
 err_close_device:
-	vfio_device_group_close(device);
+	vfio_device_group_close(df);
 err_free:
 	kfree(df);
 err_out:
drivers/vfio/vfio.h +5 -5
index 53af6e3ea214..3d8ba165146c 100644
@@ -19,14 +19,14 @@ struct vfio_container;
 struct vfio_device_file {
 	struct vfio_device *device;
 	struct kvm *kvm;
+	struct iommufd_ctx *iommufd;
 };
 
 void vfio_device_put_registration(struct vfio_device *device);
 bool vfio_device_try_get_registration(struct vfio_device *device);
-int vfio_device_open(struct vfio_device *device,
-		     struct iommufd_ctx *iommufd, struct kvm *kvm);
-void vfio_device_close(struct vfio_device *device,
-		       struct iommufd_ctx *iommufd);
+int vfio_device_open(struct vfio_device_file *df);
+void vfio_device_close(struct vfio_device_file *device);
+
 struct vfio_device_file *
 vfio_allocate_device_file(struct vfio_device *device);
 
@@ -90,7 +90,7 @@ void vfio_device_group_register(struct vfio_device *device);
 void vfio_device_group_unregister(struct vfio_device *device);
 int vfio_device_group_use_iommu(struct vfio_device *device);
 void vfio_device_group_unuse_iommu(struct vfio_device *device);
-void vfio_device_group_close(struct vfio_device *device);
+void vfio_device_group_close(struct vfio_device_file *df);
 struct vfio_group *vfio_group_from_file(struct file *file);
 bool vfio_group_enforced_coherent(struct vfio_group *group);
 void vfio_group_set_kvm(struct vfio_group *group, struct kvm *kvm);
drivers/vfio/vfio_main.c +12 -8
index dc08d5dd62cc..3df71bd9cd1e 100644
@@ -358,9 +358,11 @@ vfio_allocate_device_file(struct vfio_device *device)
 	return df;
 }
 
-static int vfio_device_first_open(struct vfio_device *device,
-				  struct iommufd_ctx *iommufd, struct kvm *kvm)
+static int vfio_device_first_open(struct vfio_device_file *df)
 {
+	struct vfio_device *device = df->device;
+	struct iommufd_ctx *iommufd = df->iommufd;
+	struct kvm *kvm = df->kvm;
 	int ret;
 
 	lockdep_assert_held(&device->dev_set->lock);
@@ -394,9 +396,11 @@ static int vfio_device_first_open(struct vfio_device *device,
 	return ret;
 }
 
-static void vfio_device_last_close(struct vfio_device *device,
-				   struct iommufd_ctx *iommufd)
+static void vfio_device_last_close(struct vfio_device_file *df)
 {
+	struct vfio_device *device = df->device;
+	struct iommufd_ctx *iommufd = df->iommufd;
+
 	lockdep_assert_held(&device->dev_set->lock);
 
 	if (device->ops->close_device)
@@ -409,30 +413,34 @@ static void vfio_device_last_close(struct vfio_device *device,
 	module_put(device->dev->driver->owner);
 }
 
-int vfio_device_open(struct vfio_device *device,
-		     struct iommufd_ctx *iommufd, struct kvm *kvm)
+int vfio_device_open(struct vfio_device_file *df)
 {
-	int ret = 0;
+	struct vfio_device *device = df->device;
+
+	lockdep_assert_held(&device->dev_set->lock);
 
-	mutex_lock(&device->dev_set->lock);

drivers/vfio/vfio_main.c static void vfio_device_last_close(struct vfio_device *device, · line 429

Quoted 10 lines
drivers/vfio/vfio_main.c +5 -2
 	device->open_count++;
 	if (device->open_count == 1) {
-		ret = vfio_device_first_open(device, iommufd, kvm);
-		if (ret)
+		int ret;
+
+		ret = vfio_device_first_open(df);
+		if (ret) {
 			device->open_count--;
+			return ret;

nit: the original ret init and return was good enough, no need to change it?

Quoted 23 lines
drivers/vfio/vfio_main.c +6 -5
+		}
 	}
-	mutex_unlock(&device->dev_set->lock);
 
-	return ret;
+	return 0;
 }
 
-void vfio_device_close(struct vfio_device *device,
-		       struct iommufd_ctx *iommufd)
+void vfio_device_close(struct vfio_device_file *df)
 {
+	struct vfio_device *device = df->device;
+
 	mutex_lock(&device->dev_set->lock);
 	vfio_assert_device_open(device);
 	if (device->open_count == 1)
-		vfio_device_last_close(device, iommufd);
+		vfio_device_last_close(df);
 	device->open_count--;
 	mutex_unlock(&device->dev_set->lock);
 }

drivers/vfio/vfio_main.c static int vfio_device_fops_release(struct inode *inode, struct file *filep) · line 492

Quoted 8 lines
drivers/vfio/vfio_main.c +1 -1
@@ -478,7 +486,7 @@ static int vfio_device_fops_release(struct inode *inode, struct file *filep)
 	struct vfio_device_file *df = filep->private_data;
 	struct vfio_device *device = df->device;
 
-	vfio_device_group_close(device);
+	vfio_device_group_close(df);
 
 	vfio_device_put_registration(device);
 

Thanks

Eric

#35 lore raw

Re: [PATCH 07/13] vfio: Pass struct vfio_device_file * to vfio_device_open/close()

Quoted 198 lines · On Thu, 19 Jan 2023 12:01:59 +0100 Eric Auger <[email protected]> wrote:

Hi Yi,

Quoted 2 lines · Yi Liu

This avoids passing struct kvm * and struct iommufd_ctx * in multiple functions. vfio_device_open() becomes to be a locked helper.

why? because dev_set lock now protects vfio_device_file fields? worth to explain.
do we need to update the comment in vfio.h related to struct vfio_device_set?

Quoted 20 lines
---
 drivers/vfio/group.c     | 34 +++++++++++++++++++++++++---------
 drivers/vfio/vfio.h      | 10 +++++-----
 drivers/vfio/vfio_main.c | 40 ++++++++++++++++++++++++----------------
 3 files changed, 54 insertions(+), 30 deletions(-)
drivers/vfio/group.c +2 -1
index d83cf069d290..7200304663e5 100644
@@ -154,33 +154,49 @@ static int vfio_group_ioctl_set_container(struct vfio_group *group,
 	return ret;
 }
 
-static int vfio_device_group_open(struct vfio_device *device)
+static int vfio_device_group_open(struct vfio_device_file *df)
 {
+	struct vfio_device *device = df->device;

drivers/vfio/group.c static int vfio_group_ioctl_set_container(struct vfio_group *group, · line 168

Quoted 10 lines
drivers/vfio/group.c +2 -1
 	int ret;
 
 	mutex_lock(&device->group->group_lock);
 	if (!vfio_group_has_iommu(device->group)) {
 		ret = -EINVAL;
-		goto out_unlock;
+		goto err_unlock_group;
 	}
 
+	mutex_lock(&device->dev_set->lock);  

is there an explanation somewhere about locking order b/w group_lock, dev_set lock?

drivers/vfio/group.c static int vfio_group_ioctl_set_container(struct vfio_group *group, · line 173

Quoted 5 lines
drivers/vfio/group.c +0 -0
 	/*
 	 * Here we pass the KVM pointer with the group under the lock.  If the
 	 * device driver will use it, it must obtain a reference and release it
 	 * during close_device.
 	 */  

May be the opportunity to rephrase the above comment. I am not a native english speaker but the time concordance seems weird + clarify a reference to what.

Quoted 130 lines
drivers/vfio/group.c +21 -7
-	ret = vfio_device_open(device, device->group->iommufd,
-			       device->group->kvm);
+	df->kvm = device->group->kvm;
+	df->iommufd = device->group->iommufd;
+
+	ret = vfio_device_open(df);
+	if (ret)
+		goto err_unlock_device;
+	mutex_unlock(&device->dev_set->lock);
 
-out_unlock:
+	mutex_unlock(&device->group->group_lock);
+	return 0;
+
+err_unlock_device:
+	df->kvm = NULL;
+	df->iommufd = NULL;
+	mutex_unlock(&device->dev_set->lock);
+err_unlock_group:
 	mutex_unlock(&device->group->group_lock);
 	return ret;
 }
 
-void vfio_device_group_close(struct vfio_device *device)
+void vfio_device_group_close(struct vfio_device_file *df)
 {
+	struct vfio_device *device = df->device;
+
 	mutex_lock(&device->group->group_lock);
-	vfio_device_close(device, device->group->iommufd);
+	vfio_device_close(df);
 	mutex_unlock(&device->group->group_lock);
 }
 
@@ -196,7 +212,7 @@ static struct file *vfio_device_open_file(struct vfio_device *device)
 		goto err_out;
 	}
 
-	ret = vfio_device_group_open(device);
+	ret = vfio_device_group_open(df);
 	if (ret)
 		goto err_free;
 
@@ -228,7 +244,7 @@ static struct file *vfio_device_open_file(struct vfio_device *device)
 	return filep;
 
 err_close_device:
-	vfio_device_group_close(device);
+	vfio_device_group_close(df);
 err_free:
 	kfree(df);
 err_out:
drivers/vfio/vfio.h +5 -5
index 53af6e3ea214..3d8ba165146c 100644
@@ -19,14 +19,14 @@ struct vfio_container;
 struct vfio_device_file {
 	struct vfio_device *device;
 	struct kvm *kvm;
+	struct iommufd_ctx *iommufd;
 };
 
 void vfio_device_put_registration(struct vfio_device *device);
 bool vfio_device_try_get_registration(struct vfio_device *device);
-int vfio_device_open(struct vfio_device *device,
-		     struct iommufd_ctx *iommufd, struct kvm *kvm);
-void vfio_device_close(struct vfio_device *device,
-		       struct iommufd_ctx *iommufd);
+int vfio_device_open(struct vfio_device_file *df);
+void vfio_device_close(struct vfio_device_file *device);
+
 struct vfio_device_file *
 vfio_allocate_device_file(struct vfio_device *device);
 
@@ -90,7 +90,7 @@ void vfio_device_group_register(struct vfio_device *device);
 void vfio_device_group_unregister(struct vfio_device *device);
 int vfio_device_group_use_iommu(struct vfio_device *device);
 void vfio_device_group_unuse_iommu(struct vfio_device *device);
-void vfio_device_group_close(struct vfio_device *device);
+void vfio_device_group_close(struct vfio_device_file *df);
 struct vfio_group *vfio_group_from_file(struct file *file);
 bool vfio_group_enforced_coherent(struct vfio_group *group);
 void vfio_group_set_kvm(struct vfio_group *group, struct kvm *kvm);
drivers/vfio/vfio_main.c +12 -8
index dc08d5dd62cc..3df71bd9cd1e 100644
@@ -358,9 +358,11 @@ vfio_allocate_device_file(struct vfio_device *device)
 	return df;
 }
 
-static int vfio_device_first_open(struct vfio_device *device,
-				  struct iommufd_ctx *iommufd, struct kvm *kvm)
+static int vfio_device_first_open(struct vfio_device_file *df)
 {
+	struct vfio_device *device = df->device;
+	struct iommufd_ctx *iommufd = df->iommufd;
+	struct kvm *kvm = df->kvm;
 	int ret;
 
 	lockdep_assert_held(&device->dev_set->lock);
@@ -394,9 +396,11 @@ static int vfio_device_first_open(struct vfio_device *device,
 	return ret;
 }
 
-static void vfio_device_last_close(struct vfio_device *device,
-				   struct iommufd_ctx *iommufd)
+static void vfio_device_last_close(struct vfio_device_file *df)
 {
+	struct vfio_device *device = df->device;
+	struct iommufd_ctx *iommufd = df->iommufd;
+
 	lockdep_assert_held(&device->dev_set->lock);
 
 	if (device->ops->close_device)
@@ -409,30 +413,34 @@ static void vfio_device_last_close(struct vfio_device *device,
 	module_put(device->dev->driver->owner);
 }
 
-int vfio_device_open(struct vfio_device *device,
-		     struct iommufd_ctx *iommufd, struct kvm *kvm)
+int vfio_device_open(struct vfio_device_file *df)
 {
-	int ret = 0;
+	struct vfio_device *device = df->device;
+
+	lockdep_assert_held(&device->dev_set->lock);
 
-	mutex_lock(&device->dev_set->lock);

drivers/vfio/vfio_main.c static void vfio_device_last_close(struct vfio_device *device, · line 429

Quoted 10 lines
drivers/vfio/vfio_main.c +5 -2
 	device->open_count++;
 	if (device->open_count == 1) {
-		ret = vfio_device_first_open(device, iommufd, kvm);
-		if (ret)
+		int ret;
+
+		ret = vfio_device_first_open(df);
+		if (ret) {
 			device->open_count--;
+			return ret;  

nit: the original ret init and return was good enough, no need to change it?

Quoted 8 lines
drivers/vfio/vfio_main.c +2 -2
+		}
 	}
-	mutex_unlock(&device->dev_set->lock);
 
-	return ret;
+	return 0;
 }
 
Quoted 14 lines
Quoted 4 lines
drivers/vfio/vfio_main.c +1 -2
-void vfio_device_close(struct vfio_device *device,
-		       struct iommufd_ctx *iommufd)
+void vfio_device_close(struct vfio_device_file *df)
 {

drivers/vfio/vfio_main.c static void vfio_device_last_close(struct vfio_device *device, · line 446

Quoted 10 lines
drivers/vfio/vfio_main.c +3 -1
+	struct vfio_device *device = df->device;
+
 	mutex_lock(&device->dev_set->lock);
 	vfio_assert_device_open(device);
 	if (device->open_count == 1)
-		vfio_device_last_close(device, iommufd);
+		vfio_device_last_close(df);
 	device->open_count--;
 	mutex_unlock(&device->dev_set->lock);
 }

I find it strange that the dev_set->lock has been moved to the caller for open, but not for close. Like Eric suggests, this seems to be because vfio_device_file is usurping dev_set->lock to protect its own fields, but then those fields are set on open, cleared on the open error path, but not on close?? Thanks,

Alex

author
#36 lore raw

RE: [PATCH 07/13] vfio: Pass struct vfio_device_file * to vfio_device_open/close()

Hi Alex,

Quoted 16 lines

From: Alex Williamson <[email protected]>
Sent: Friday, January 20, 2023 4:36 AM

Quoted 10 lines · On Thu, 19 Jan 2023 12:01:59 +0100 Eric Auger <[email protected]> wrote:
Quoted 10 lines
drivers/vfio/vfio_main.c +3 -2
-void vfio_device_close(struct vfio_device *device,
-		       struct iommufd_ctx *iommufd)
+void vfio_device_close(struct vfio_device_file *df)
 {
+	struct vfio_device *device = df->device;
+
 	mutex_lock(&device->dev_set->lock);
 	vfio_assert_device_open(device);
 	if (device->open_count == 1)
Quoted 8 lines
Quoted 5 lines

drivers/vfio/vfio_main.c static void vfio_device_last_close(struct vfio_device *device, · line 446

Quoted 5 lines
drivers/vfio/vfio_main.c +1 -1
-		vfio_device_last_close(device, iommufd);
+		vfio_device_last_close(df);
 	device->open_count--;
 	mutex_unlock(&device->dev_set->lock);
 }

I find it strange that the dev_set->lock has been moved to the caller for open, but not for close.

There is dev_set->lock for close. Is it? just as the above code snippet. dev_set->lock is held before calling vfio_device_last_close.

Quoted 4 lines

Like Eric suggests, this seems to be
because vfio_device_file is usurping dev_set->lock to protect its own fields, but then those fields are set on open, cleared on the open error path, but not on close?? Thanks,

Yeah. The on close, the vfio_device_file will be freed. So there is no clear on the vfio_device_file fields.

Regards,
Yi Liu

author
#37 lore raw

RE: [PATCH 07/13] vfio: Pass struct vfio_device_file * to vfio_device_open/close()

In reply to Eric Auger

Hi Eric,

Quoted 10 lines

From: Eric Auger <[email protected]>
Sent: Thursday, January 19, 2023 7:02 PM

Hi Yi,

Quoted 2 lines · Yi Liu

This avoids passing struct kvm * and struct iommufd_ctx * in multiple functions. vfio_device_open() becomes to be a locked helper.

why? because dev_set lock now protects vfio_device_file fields? worth to explain.

Yeah, this is because the major reference of the vfio_device_file fields are under dev_set lock.

Quoted 2 lines

do we need to update the comment in vfio.h related to struct vfio_device_set?

Yes.

Quoted 29 lines
Quoted 29 lines
---
 drivers/vfio/group.c     | 34 +++++++++++++++++++++++++---------
 drivers/vfio/vfio.h      | 10 +++++-----
 drivers/vfio/vfio_main.c | 40 ++++++++++++++++++++++++----------------
 3 files changed, 54 insertions(+), 30 deletions(-)
drivers/vfio/group.c +3 -2
index d83cf069d290..7200304663e5 100644
@@ -154,33 +154,49 @@ static int vfio_group_ioctl_set_container(struct vfio_group *group,
 	return ret;
 }
 
-static int vfio_device_group_open(struct vfio_device *device)
+static int vfio_device_group_open(struct vfio_device_file *df)
 {
+	struct vfio_device *device = df->device;
 	int ret;
 
 	mutex_lock(&device->group->group_lock);
 	if (!vfio_group_has_iommu(device->group)) {
 		ret = -EINVAL;
-		goto out_unlock;
+		goto err_unlock_group;
 	}
 
Quoted 3 lines

drivers/vfio/group.c static int vfio_group_ioctl_set_container(struct vfio_group *group, · line 168

Quoted 1 line
drivers/vfio/group.c +1 -0
+	mutex_lock(&device->dev_set->lock);

is there an explanation somewhere about locking order b/w group_lock, dev_set lock?

In the before, dev_set lock is held prior to group_lock. But now, group_lock is held firstly and then dev_set lock if the group_lock is compiled. E.g. the group open path.

Quoted 9 lines

drivers/vfio/group.c static int vfio_group_ioctl_set_container(struct vfio_group *group, · line 170

Quoted 2 lines
drivers/vfio/group.c +0 -0
 	/*
 	 * Here we pass the KVM pointer with the group under the lock.  If

the

drivers/vfio/group.c static int vfio_group_ioctl_set_container(struct vfio_group *group, · line 173

Quoted 3 lines
drivers/vfio/group.c +0 -0
 	 * device driver will use it, it must obtain a reference and release it
 	 * during close_device.
 	 */

May be the opportunity to rephrase the above comment. I am not a native english speaker but the time concordance seems weird + clarify a reference to what.

Oh, it's a reference to kvm pointer.

Quoted 136 lines

drivers/vfio/group.c static struct file *vfio_device_open_file(struct

Quoted 35 lines
drivers/vfio/group.c +19 -5
-	ret = vfio_device_open(device, device->group->iommufd,
-			       device->group->kvm);
+	df->kvm = device->group->kvm;
+	df->iommufd = device->group->iommufd;
+
+	ret = vfio_device_open(df);
+	if (ret)
+		goto err_unlock_device;
+	mutex_unlock(&device->dev_set->lock);
 
-out_unlock:
+	mutex_unlock(&device->group->group_lock);
+	return 0;
+
+err_unlock_device:
+	df->kvm = NULL;
+	df->iommufd = NULL;
+	mutex_unlock(&device->dev_set->lock);
+err_unlock_group:
 	mutex_unlock(&device->group->group_lock);
 	return ret;
 }
 
-void vfio_device_group_close(struct vfio_device *device)
+void vfio_device_group_close(struct vfio_device_file *df)
 {
+	struct vfio_device *device = df->device;
+
 	mutex_lock(&device->group->group_lock);
-	vfio_device_close(device, device->group->iommufd);
+	vfio_device_close(df);
 	mutex_unlock(&device->group->group_lock);
 }
 
@@ -196,7 +212,7 @@ static struct file *vfio_device_open_file(struct

vfio_device *device)

drivers/vfio/group.c static struct file *vfio_device_open_file(struct

Quoted 9 lines
drivers/vfio/group.c +1 -1
 		goto err_out;
 	}
 
-	ret = vfio_device_group_open(device);
+	ret = vfio_device_group_open(df);
 	if (ret)
 		goto err_free;
 
@@ -228,7 +244,7 @@ static struct file *vfio_device_open_file(struct

vfio_device *device)

drivers/vfio/vfio.h void vfio_device_group_register(struct vfio_device

Quoted 32 lines
drivers/vfio/group.c +1 -1
 	return filep;
 
 err_close_device:
-	vfio_device_group_close(device);
+	vfio_device_group_close(df);
 err_free:
 	kfree(df);
 err_out:
drivers/vfio/vfio.h +4 -4
index 53af6e3ea214..3d8ba165146c 100644
@@ -19,14 +19,14 @@ struct vfio_container;
 struct vfio_device_file {
 	struct vfio_device *device;
 	struct kvm *kvm;
+	struct iommufd_ctx *iommufd;
 };
 
 void vfio_device_put_registration(struct vfio_device *device);
 bool vfio_device_try_get_registration(struct vfio_device *device);
-int vfio_device_open(struct vfio_device *device,
-		     struct iommufd_ctx *iommufd, struct kvm *kvm);
-void vfio_device_close(struct vfio_device *device,
-		       struct iommufd_ctx *iommufd);
+int vfio_device_open(struct vfio_device_file *df);
+void vfio_device_close(struct vfio_device_file *device);
+
 struct vfio_device_file *
 vfio_allocate_device_file(struct vfio_device *device);
 
@@ -90,7 +90,7 @@ void vfio_device_group_register(struct vfio_device

*device);

drivers/vfio/vfio_main.c vfio_allocate_device_file(struct vfio_device

Quoted 13 lines
drivers/vfio/vfio.h +1 -1
 void vfio_device_group_unregister(struct vfio_device *device);
 int vfio_device_group_use_iommu(struct vfio_device *device);
 void vfio_device_group_unuse_iommu(struct vfio_device *device);
-void vfio_device_group_close(struct vfio_device *device);
+void vfio_device_group_close(struct vfio_device_file *df);
 struct vfio_group *vfio_group_from_file(struct file *file);
 bool vfio_group_enforced_coherent(struct vfio_group *group);
 void vfio_group_set_kvm(struct vfio_group *group, struct kvm *kvm);
drivers/vfio/vfio_main.c +0 -0
index dc08d5dd62cc..3df71bd9cd1e 100644
@@ -358,9 +358,11 @@ vfio_allocate_device_file(struct vfio_device

*device)

drivers/vfio/vfio_main.c vfio_allocate_device_file(struct vfio_device *device)

Quoted 5 lines
drivers/vfio/vfio_main.c +0 -2
 	return df;
 }
 
-static int vfio_device_first_open(struct vfio_device *device,
-				  struct iommufd_ctx *iommufd, struct kvm

*kvm)

drivers/vfio/vfio_main.c static int vfio_device_first_open(struct

Quoted 9 lines
drivers/vfio/vfio_main.c +4 -0
+static int vfio_device_first_open(struct vfio_device_file *df)
 {
+	struct vfio_device *device = df->device;
+	struct iommufd_ctx *iommufd = df->iommufd;
+	struct kvm *kvm = df->kvm;
 	int ret;
 
 	lockdep_assert_held(&device->dev_set->lock);
@@ -394,9 +396,11 @@ static int vfio_device_first_open(struct

vfio_device *device,

drivers/vfio/vfio_main.c static void vfio_device_last_close(struct

Quoted 14 lines
drivers/vfio/vfio_main.c +4 -2
 	return ret;
 }
 
-static void vfio_device_last_close(struct vfio_device *device,
-				   struct iommufd_ctx *iommufd)
+static void vfio_device_last_close(struct vfio_device_file *df)
 {
+	struct vfio_device *device = df->device;
+	struct iommufd_ctx *iommufd = df->iommufd;
+
 	lockdep_assert_held(&device->dev_set->lock);
 
 	if (device->ops->close_device)
@@ -409,30 +413,34 @@ static void vfio_device_last_close(struct

vfio_device *device,

Quoted 12 lines
drivers/vfio/vfio_main.c +4 -3
 	module_put(device->dev->driver->owner);
 }
 
-int vfio_device_open(struct vfio_device *device,
-		     struct iommufd_ctx *iommufd, struct kvm *kvm)
+int vfio_device_open(struct vfio_device_file *df)
 {
-	int ret = 0;
+	struct vfio_device *device = df->device;
+
+	lockdep_assert_held(&device->dev_set->lock);
 
Quoted 12 lines

drivers/vfio/vfio_main.c static void vfio_device_last_close(struct vfio_device *device, · line 429

Quoted 11 lines
drivers/vfio/vfio_main.c +5 -3
-	mutex_lock(&device->dev_set->lock);
 	device->open_count++;
 	if (device->open_count == 1) {
-		ret = vfio_device_first_open(device, iommufd, kvm);
-		if (ret)
+		int ret;
+
+		ret = vfio_device_first_open(df);
+		if (ret) {
 			device->open_count--;
+			return ret;

nit: the original ret init and return was good enough, no need to change it?

This is a change needed in a later commit to be success-oriented.

https://lore.kernel.org/kvm/[email protected]/

but I guess it is not needed here. So you are right. May just keep the existing ret init and return.

Quoted 24 lines

drivers/vfio/vfio_main.c static int vfio_device_fops_release(struct inode

Quoted 23 lines
drivers/vfio/vfio_main.c +6 -5
+		}
 	}
-	mutex_unlock(&device->dev_set->lock);
 
-	return ret;
+	return 0;
 }
 
-void vfio_device_close(struct vfio_device *device,
-		       struct iommufd_ctx *iommufd)
+void vfio_device_close(struct vfio_device_file *df)
 {
+	struct vfio_device *device = df->device;
+
 	mutex_lock(&device->dev_set->lock);
 	vfio_assert_device_open(device);
 	if (device->open_count == 1)
-		vfio_device_last_close(device, iommufd);
+		vfio_device_last_close(df);
 	device->open_count--;
 	mutex_unlock(&device->dev_set->lock);
 }
@@ -478,7 +486,7 @@ static int vfio_device_fops_release(struct inode

*inode, struct file *filep)

Quoted 8 lines

drivers/vfio/vfio_main.c static int vfio_device_fops_release(struct inode *inode, struct file *filep) · line 492

Quoted 8 lines
drivers/vfio/vfio_main.c +1 -1
 	struct vfio_device_file *df = filep->private_data;
 	struct vfio_device *device = df->device;
 
-	vfio_device_group_close(device);
+	vfio_device_group_close(df);
 
 	vfio_device_put_registration(device);
 

Regards,
Yi Liu

author
#38 lore raw

PATCH 8/13 vfio: Block device access via device fd until device is opened

In reply to Yi Liu

Allow the vfio_device file to be in a state where the device FD is opened but the device cannot be used by userspace (i.e. its .open_device() hasn't been called). This inbetween state is not used when the device FD is spawned from the group FD, however when we create the device FD directly by opening a cdev it will be opened in the blocked state.

In the blocked state, currently only the bind operation is allowed, other device accesses are not allowed. Completing bind will allow user to further access the device.

This is implemented by adding a flag in struct vfio_device_file to mark the blocked state and using a simple smp_load_acquire() to obtain the flag value and serialize all the device setup with the thread accessing this device.

Due to this scheme it is not possible to unbind the FD, once it is bound, it remains bound until the FD is closed.

---
 drivers/vfio/vfio.h      |  1 +
 drivers/vfio/vfio_main.c | 29 +++++++++++++++++++++++++++++
 2 files changed, 30 insertions(+)
drivers/vfio/vfio.h +1 -0
index 3d8ba165146c..c69a9902ea84 100644
@@ -20,6 +20,7 @@ struct vfio_device_file {
 	struct vfio_device *device;
 	struct kvm *kvm;
 	struct iommufd_ctx *iommufd;
+	bool access_granted;
 };
 
 void vfio_device_put_registration(struct vfio_device *device);
drivers/vfio/vfio_main.c +29 -0
index 3df71bd9cd1e..d442ebaa4b21 100644
@@ -430,6 +430,11 @@ int vfio_device_open(struct vfio_device_file *df)
 		}
 	}
 
+	/*
+	 * Paired with smp_load_acquire() in vfio_device_fops::ioctl/
+	 * read/write/mmap
+	 */
+	smp_store_release(&df->access_granted, true);
 	return 0;
 }
 
@@ -1058,8 +1063,14 @@ static long vfio_device_fops_unl_ioctl(struct file *filep,
 {
 	struct vfio_device_file *df = filep->private_data;
 	struct vfio_device *device = df->device;
+	bool access;
 	int ret;
 
+	/* Paired with smp_store_release() in vfio_device_open() */
+	access = smp_load_acquire(&df->access_granted);
+	if (!access)
+		return -EINVAL;
+
 	ret = vfio_device_pm_runtime_get(device);
 	if (ret)
 		return ret;
@@ -1086,6 +1097,12 @@ static ssize_t vfio_device_fops_read(struct file *filep, char __user *buf,
 {
 	struct vfio_device_file *df = filep->private_data;
 	struct vfio_device *device = df->device;
+	bool access;
+
+	/* Paired with smp_store_release() in vfio_device_open() */
+	access = smp_load_acquire(&df->access_granted);
+	if (!access)
+		return -EINVAL;
 
 	if (unlikely(!device->ops->read))
 		return -EINVAL;
@@ -1099,6 +1116,12 @@ static ssize_t vfio_device_fops_write(struct file *filep,
 {
 	struct vfio_device_file *df = filep->private_data;
 	struct vfio_device *device = df->device;
+	bool access;
+
+	/* Paired with smp_store_release() in vfio_device_open() */
+	access = smp_load_acquire(&df->access_granted);
+	if (!access)
+		return -EINVAL;
 
 	if (unlikely(!device->ops->write))
 		return -EINVAL;
@@ -1110,6 +1133,12 @@ static int vfio_device_fops_mmap(struct file *filep, struct vm_area_struct *vma)
 {
 	struct vfio_device_file *df = filep->private_data;
 	struct vfio_device *device = df->device;
+	bool access;
+
+	/* Paired with smp_store_release() in vfio_device_open() */
+	access = smp_load_acquire(&df->access_granted);
+	if (!access)
+		return -EINVAL;
 
 	if (unlikely(!device->ops->mmap))
 		return -EINVAL;
Signature · 1 line
2.34.1
maintainer
#39 lore raw

RE: [PATCH 08/13] vfio: Block device access via device fd until device is opened

Quoted 13 lines

From: Liu, Yi L <[email protected]>
Sent: Tuesday, January 17, 2023 9:50 PM

Allow the vfio_device file to be in a state where the device FD is opened but the device cannot be used by userspace (i.e. its .open_device() hasn't been called). This inbetween state is not used when the device FD is spawned from the group FD, however when we create the device FD directly by opening a cdev it will be opened in the blocked state.

In the blocked state, currently only the bind operation is allowed, other device accesses are not allowed. Completing bind will allow user to further access the device.

Quoted 8 lines

This is implemented by adding a flag in struct vfio_device_file to mark the blocked state and using a simple smp_load_acquire() to obtain the flag value and serialize all the device setup with the thread accessing this device.

Due to this scheme it is not possible to unbind the FD, once it is bound, it remains bound until the FD is closed.

My question to the last version was not answered...

Can you elaborate why it is impossible to unbind? Is it more an implementation choice or conceptual restriction?

maintainer
#40 lore raw

Re: [PATCH 08/13] vfio: Block device access via device fd until device is opened

Quoted 18 lines · Kevin
Quoted 18 lines

From: Liu, Yi L <[email protected]>
Sent: Tuesday, January 17, 2023 9:50 PM

Allow the vfio_device file to be in a state where the device FD is opened but the device cannot be used by userspace (i.e. its .open_device() hasn't been called). This inbetween state is not used when the device FD is spawned from the group FD, however when we create the device FD directly by opening a cdev it will be opened in the blocked state.

In the blocked state, currently only the bind operation is allowed, other device accesses are not allowed. Completing bind will allow user to further access the device.

This is implemented by adding a flag in struct vfio_device_file to mark the blocked state and using a simple smp_load_acquire() to obtain the flag value and serialize all the device setup with the thread accessing this device.

Quoted 8 lines
Quoted 3 lines

Due to this scheme it is not possible to unbind the FD, once it is bound, it remains bound until the FD is closed.

My question to the last version was not answered...

Can you elaborate why it is impossible to unbind? Is it more an implementation choice or conceptual restriction?

At least for the implementation it is due to the use of the lockless test for bind.

It can safely handle unbind->bind but it cannot handle bind->unbind. To allows this we'd need to add a lock on all the vfio ioctls which seems costly.

Jason

maintainer
#41 lore raw

RE: [PATCH 08/13] vfio: Block device access via device fd until device is opened

Quoted 31 lines

From: Jason Gunthorpe <[email protected]>
Sent: Wednesday, January 18, 2023 9:52 PM

Quoted 18 lines · Kevin
Quoted 18 lines

From: Liu, Yi L <[email protected]>
Sent: Tuesday, January 17, 2023 9:50 PM

Allow the vfio_device file to be in a state where the device FD is opened but the device cannot be used by userspace (i.e. its .open_device() hasn't been called). This inbetween state is not used when the device FD is spawned from the group FD, however when we create the device FD directly by opening a cdev it will be opened in the blocked state.

In the blocked state, currently only the bind operation is allowed, other device accesses are not allowed. Completing bind will allow user to further access the device.

This is implemented by adding a flag in struct vfio_device_file to mark the blocked state and using a simple smp_load_acquire() to obtain the flag value and serialize all the device setup with the thread accessing this device.

Quoted 8 lines
Quoted 3 lines

Due to this scheme it is not possible to unbind the FD, once it is bound, it remains bound until the FD is closed.

My question to the last version was not answered...

Can you elaborate why it is impossible to unbind? Is it more an implementation choice or conceptual restriction?

Quoted 7 lines

At least for the implementation it is due to the use of the lockless test for bind.

It can safely handle unbind->bind but it cannot handle bind->unbind. To allows this we'd need to add a lock on all the vfio ioctls which seems costly.

OK, it makes sense. Yi, can you add this message in next version?

author
#42 lore raw

RE: [PATCH 08/13] vfio: Block device access via device fd until device is opened

Quoted 11 lines

From: Tian, Kevin <[email protected]>
Sent: Thursday, January 19, 2023 11:42 AM

Quoted 8 lines
Quoted 4 lines

My question to the last version was not answered...

Can you elaborate why it is impossible to unbind? Is it more an implementation choice or conceptual restriction?

At least for the implementation it is due to the use of the lockless test for bind.

Quoted 6 lines
Quoted 4 lines

It can safely handle unbind->bind but it cannot handle bind->unbind. To allows this we'd need to add a lock on all the vfio ioctls which seems costly.

OK, it makes sense. Yi, can you add this message in next version?

Yeah. 😊

Regards,
Yi Liu

#43 lore raw

Re: [PATCH 08/13] vfio: Block device access via device fd until device is opened

In reply to Yi Liu

Hi Yi,

Quoted 5 lines · Yi Liu

Allow the vfio_device file to be in a state where the device FD is opened but the device cannot be used by userspace (i.e. its .open_device() hasn't been called). This inbetween state is not used when the device FD is spawned from the group FD, however when we create the device FD directly by opening a cdev it will be opened in the blocked state.

Please explain why this is needed in the commit message (although you evoked the rationale in the cover letter).

Eric

Quoted 102 lines

In the blocked state, currently only the bind operation is allowed, other device accesses are not allowed. Completing bind will allow user to further access the device.

This is implemented by adding a flag in struct vfio_device_file to mark the blocked state and using a simple smp_load_acquire() to obtain the flag value and serialize all the device setup with the thread accessing this device.

Due to this scheme it is not possible to unbind the FD, once it is bound, it remains bound until the FD is closed.

---
 drivers/vfio/vfio.h      |  1 +
 drivers/vfio/vfio_main.c | 29 +++++++++++++++++++++++++++++
 2 files changed, 30 insertions(+)
drivers/vfio/vfio.h +1 -0
index 3d8ba165146c..c69a9902ea84 100644
@@ -20,6 +20,7 @@ struct vfio_device_file {
 	struct vfio_device *device;
 	struct kvm *kvm;
 	struct iommufd_ctx *iommufd;
+	bool access_granted;
 };
 
 void vfio_device_put_registration(struct vfio_device *device);
drivers/vfio/vfio_main.c +29 -0
index 3df71bd9cd1e..d442ebaa4b21 100644
@@ -430,6 +430,11 @@ int vfio_device_open(struct vfio_device_file *df)
 		}
 	}
 
+	/*
+	 * Paired with smp_load_acquire() in vfio_device_fops::ioctl/
+	 * read/write/mmap
+	 */
+	smp_store_release(&df->access_granted, true);
 	return 0;
 }
 
@@ -1058,8 +1063,14 @@ static long vfio_device_fops_unl_ioctl(struct file *filep,
 {
 	struct vfio_device_file *df = filep->private_data;
 	struct vfio_device *device = df->device;
+	bool access;
 	int ret;
 
+	/* Paired with smp_store_release() in vfio_device_open() */
+	access = smp_load_acquire(&df->access_granted);
+	if (!access)
+		return -EINVAL;
+
 	ret = vfio_device_pm_runtime_get(device);
 	if (ret)
 		return ret;
@@ -1086,6 +1097,12 @@ static ssize_t vfio_device_fops_read(struct file *filep, char __user *buf,
 {
 	struct vfio_device_file *df = filep->private_data;
 	struct vfio_device *device = df->device;
+	bool access;
+
+	/* Paired with smp_store_release() in vfio_device_open() */
+	access = smp_load_acquire(&df->access_granted);
+	if (!access)
+		return -EINVAL;
 
 	if (unlikely(!device->ops->read))
 		return -EINVAL;
@@ -1099,6 +1116,12 @@ static ssize_t vfio_device_fops_write(struct file *filep,
 {
 	struct vfio_device_file *df = filep->private_data;
 	struct vfio_device *device = df->device;
+	bool access;
+
+	/* Paired with smp_store_release() in vfio_device_open() */
+	access = smp_load_acquire(&df->access_granted);
+	if (!access)
+		return -EINVAL;
 
 	if (unlikely(!device->ops->write))
 		return -EINVAL;
@@ -1110,6 +1133,12 @@ static int vfio_device_fops_mmap(struct file *filep, struct vm_area_struct *vma)
 {
 	struct vfio_device_file *df = filep->private_data;
 	struct vfio_device *device = df->device;
+	bool access;
+
+	/* Paired with smp_store_release() in vfio_device_open() */
+	access = smp_load_acquire(&df->access_granted);
+	if (!access)
+		return -EINVAL;
 
 	if (unlikely(!device->ops->mmap))
 		return -EINVAL;
author
#44 lore raw

RE: [PATCH 08/13] vfio: Block device access via device fd until device is opened

Quoted 6 lines

From: Eric Auger <[email protected]>
Sent: Thursday, January 19, 2023 10:01 PM

Hi Yi,

On 1/17/23 14:49, Yi Liu wrote:

Quoted 8 lines
Quoted 2 lines

Allow the vfio_device file to be in a state where the device FD is opened but the device cannot be used by userspace (i.e.

its .open_device()

Quoted 3 lines

hasn't been called). This inbetween state is not used when the device FD is spawned from the group FD, however when we create the device FD directly by opening a cdev it will be opened in the blocked state.

Please explain why this is needed in the commit message (although you evoked the rationale in the cover letter).

Sure.

Regards,
Yi Liu

#45 lore raw

Re: [PATCH 08/13] vfio: Block device access via device fd until device is opened

In reply to Yi Liu
Quoted 42 lines · On Tue, 17 Jan 2023 05:49:37 -0800 Yi Liu <[email protected]> wrote:

Allow the vfio_device file to be in a state where the device FD is opened but the device cannot be used by userspace (i.e. its .open_device() hasn't been called). This inbetween state is not used when the device FD is spawned from the group FD, however when we create the device FD directly by opening a cdev it will be opened in the blocked state.

In the blocked state, currently only the bind operation is allowed, other device accesses are not allowed. Completing bind will allow user to further access the device.

This is implemented by adding a flag in struct vfio_device_file to mark the blocked state and using a simple smp_load_acquire() to obtain the flag value and serialize all the device setup with the thread accessing this device.

Due to this scheme it is not possible to unbind the FD, once it is bound, it remains bound until the FD is closed.

---
 drivers/vfio/vfio.h      |  1 +
 drivers/vfio/vfio_main.c | 29 +++++++++++++++++++++++++++++
 2 files changed, 30 insertions(+)
drivers/vfio/vfio.h +1 -0
index 3d8ba165146c..c69a9902ea84 100644
@@ -20,6 +20,7 @@ struct vfio_device_file {
 	struct vfio_device *device;
 	struct kvm *kvm;
 	struct iommufd_ctx *iommufd;
+	bool access_granted;
 };
 
 void vfio_device_put_registration(struct vfio_device *device);
drivers/vfio/vfio_main.c +0 -0
index 3df71bd9cd1e..d442ebaa4b21 100644

drivers/vfio/vfio_main.c int vfio_device_open(struct vfio_device_file *df) · line 437

Quoted 8 lines
drivers/vfio/vfio_main.c +5 -0
@@ -430,6 +430,11 @@ int vfio_device_open(struct vfio_device_file *df)
 		}
 	}
 
+	/*
+	 * Paired with smp_load_acquire() in vfio_device_fops::ioctl/
+	 * read/write/mmap
+	 */
+	smp_store_release(&df->access_granted, true);

Why is this happening outside of the first-open branch? Thanks,

Alex

author
#46 lore raw

RE: [PATCH 08/13] vfio: Block device access via device fd until device is opened

Quoted 52 lines

From: Alex Williamson <[email protected]>
Sent: Friday, January 20, 2023 4:47 AM

Quoted 2 lines · On Tue, 17 Jan 2023 05:49:37 -0800 Yi Liu <[email protected]> wrote:

Allow the vfio_device file to be in a state where the device FD is opened but the device cannot be used by userspace (i.e.

its .open_device()

Quoted 43 lines

hasn't been called). This inbetween state is not used when the device FD is spawned from the group FD, however when we create the device FD directly by opening a cdev it will be opened in the blocked state.

In the blocked state, currently only the bind operation is allowed, other device accesses are not allowed. Completing bind will allow user to further access the device.

This is implemented by adding a flag in struct vfio_device_file to mark the blocked state and using a simple smp_load_acquire() to obtain the flag value and serialize all the device setup with the thread accessing this device.

Due to this scheme it is not possible to unbind the FD, once it is bound, it remains bound until the FD is closed.

---
 drivers/vfio/vfio.h      |  1 +
 drivers/vfio/vfio_main.c | 29 +++++++++++++++++++++++++++++
 2 files changed, 30 insertions(+)
drivers/vfio/vfio.h +1 -0
index 3d8ba165146c..c69a9902ea84 100644
@@ -20,6 +20,7 @@ struct vfio_device_file {
 	struct vfio_device *device;
 	struct kvm *kvm;
 	struct iommufd_ctx *iommufd;
+	bool access_granted;
 };
 
 void vfio_device_put_registration(struct vfio_device *device);
drivers/vfio/vfio_main.c +0 -0
index 3df71bd9cd1e..d442ebaa4b21 100644
@@ -430,6 +430,11 @@ int vfio_device_open(struct vfio_device_file *df)
 		}
 	}
 
Quoted 7 lines

drivers/vfio/vfio_main.c int vfio_device_open(struct vfio_device_file *df) · line 437

Quoted 5 lines
drivers/vfio/vfio_main.c +5 -0
+	/*
+	 * Paired with smp_load_acquire() in vfio_device_fops::ioctl/
+	 * read/write/mmap
+	 */
+	smp_store_release(&df->access_granted, true);

Why is this happening outside of the first-open branch? Thanks,

The reason is the group path allows multiple device fds. But only the first device fd open instance will trigger the first-open branch. But all the device fd open instances need to set the access_granted flag. Surely for the cdev path, this can be moved to the first-open branch as cdev path only allows one device fd open.

Regards,
Yi Liu

author
#47 lore raw

PATCH 9/13 vfio: Add infrastructure for bind_iommufd and attach

In reply to Yi Liu

This prepares to add ioctls for device cdev fd. This infrastructure includes:
- add vfio_iommufd_attach() to support iommufd pgtable attach after bind_iommufd. A NULL pt_id indicates detach.
- let vfio_iommufd_bind() to accept pt_id, e.g. the comapt_ioas_id in the legacy group path, and also return back dev_id if caller requires it.

---
 drivers/vfio/group.c     | 12 +++++-
 drivers/vfio/iommufd.c   | 79 ++++++++++++++++++++++++++++++----------
 drivers/vfio/vfio.h      | 15 ++++++--
 drivers/vfio/vfio_main.c | 10 +++--
 4 files changed, 88 insertions(+), 28 deletions(-)
drivers/vfio/group.c +11 -1
index 7200304663e5..9484bb1c54a9 100644
@@ -157,6 +157,8 @@ static int vfio_group_ioctl_set_container(struct vfio_group *group,
 static int vfio_device_group_open(struct vfio_device_file *df)
 {
 	struct vfio_device *device = df->device;
+	u32 ioas_id;
+	u32 *pt_id = NULL;
 	int ret;
 
 	mutex_lock(&device->group->group_lock);
@@ -165,6 +167,14 @@ static int vfio_device_group_open(struct vfio_device_file *df)
 		goto err_unlock_group;
 	}
 
+	if (device->group->iommufd) {
+		ret = iommufd_vfio_compat_ioas_id(device->group->iommufd,
+						  &ioas_id);
+		if (ret)
+			goto err_unlock_group;
+		pt_id = &ioas_id;
+	}
+
 	mutex_lock(&device->dev_set->lock);
 	/*
 	 * Here we pass the KVM pointer with the group under the lock.  If the
@@ -174,7 +184,7 @@ static int vfio_device_group_open(struct vfio_device_file *df)
 	df->kvm = device->group->kvm;
 	df->iommufd = device->group->iommufd;
 
-	ret = vfio_device_open(df);
+	ret = vfio_device_open(df, NULL, pt_id);
 	if (ret)
 		goto err_unlock_device;
 	mutex_unlock(&device->dev_set->lock);
drivers/vfio/iommufd.c +59 -20

125 lines not on this page · raw message

drivers/vfio/vfio.h +12 -3
index c69a9902ea84..fe0fcfa78710 100644
@@ -25,7 +25,8 @@ struct vfio_device_file {
 
 void vfio_device_put_registration(struct vfio_device *device);
 bool vfio_device_try_get_registration(struct vfio_device *device);
-int vfio_device_open(struct vfio_device_file *df);
+int vfio_device_open(struct vfio_device_file *df,
+		     u32 *dev_id, u32 *pt_id);
 void vfio_device_close(struct vfio_device_file *device);
 
 struct vfio_device_file *
@@ -230,11 +231,14 @@ static inline void vfio_container_cleanup(void)
 #endif
 
 #if IS_ENABLED(CONFIG_IOMMUFD)
-int vfio_iommufd_bind(struct vfio_device *device, struct iommufd_ctx *ictx);
+int vfio_iommufd_bind(struct vfio_device *device, struct iommufd_ctx *ictx,
+		      u32 *dev_id, u32 *pt_id);
 void vfio_iommufd_unbind(struct vfio_device *device);
+int vfio_iommufd_attach(struct vfio_device *vdev, u32 *pt_id);
 #else
 static inline int vfio_iommufd_bind(struct vfio_device *device,
-				    struct iommufd_ctx *ictx)
+				    struct iommufd_ctx *ictx,
+				    u32 *dev_id, u32 *pt_id)
 {
 	return -EOPNOTSUPP;
 }
@@ -242,6 +246,11 @@ static inline int vfio_iommufd_bind(struct vfio_device *device,
 static inline void vfio_iommufd_unbind(struct vfio_device *device)
 {
 }
+
+static inline int vfio_iommufd_attach(struct vfio_device *vdev, u32 *pt_id)
+{
+	return -EOPNOTSUPP;
+}
 #endif
 
 #if IS_ENABLED(CONFIG_VFIO_VIRQFD)
drivers/vfio/vfio_main.c +6 -4
index d442ebaa4b21..90174a9015c4 100644
@@ -358,7 +358,8 @@ vfio_allocate_device_file(struct vfio_device *device)
 	return df;
 }
 
-static int vfio_device_first_open(struct vfio_device_file *df)
+static int vfio_device_first_open(struct vfio_device_file *df,
+				  u32 *dev_id, u32 *pt_id)
 {
 	struct vfio_device *device = df->device;
 	struct iommufd_ctx *iommufd = df->iommufd;
@@ -371,7 +372,7 @@ static int vfio_device_first_open(struct vfio_device_file *df)
 		return -ENODEV;
 
 	if (iommufd)
-		ret = vfio_iommufd_bind(device, iommufd);
+		ret = vfio_iommufd_bind(device, iommufd, dev_id, pt_id);
 	else
 		ret = vfio_device_group_use_iommu(device);
 	if (ret)
@@ -413,7 +414,8 @@ static void vfio_device_last_close(struct vfio_device_file *df)
 	module_put(device->dev->driver->owner);
 }
 
-int vfio_device_open(struct vfio_device_file *df)
+int vfio_device_open(struct vfio_device_file *df,
+		     u32 *dev_id, u32 *pt_id)
 {
 	struct vfio_device *device = df->device;
 
@@ -423,7 +425,7 @@ int vfio_device_open(struct vfio_device_file *df)
 	if (device->open_count == 1) {
 		int ret;
 
-		ret = vfio_device_first_open(df);
+		ret = vfio_device_first_open(df, dev_id, pt_id);
 		if (ret) {
 			device->open_count--;
 			return ret;
Signature · 1 line
2.34.1
maintainer
#48 lore raw

RE: [PATCH 09/13] vfio: Add infrastructure for bind_iommufd and attach

Quoted 29 lines

From: Liu, Yi L <[email protected]>
Sent: Tuesday, January 17, 2023 9:50 PM
static int vfio_device_group_open(struct vfio_device_file *df) {

drivers/vfio/group.c +10 -0
 	struct vfio_device *device = df->device;
+	u32 ioas_id;
+	u32 *pt_id = NULL;
 	int ret;
 
 	mutex_lock(&device->group->group_lock);
@@ -165,6 +167,14 @@ static int vfio_device_group_open(struct
 vfio_device_file *df)
 		goto err_unlock_group;
 	}
 
+	if (device->group->iommufd) {
+		ret = iommufd_vfio_compat_ioas_id(device->group-
 >iommufd,
+						  &ioas_id);
+		if (ret)
+			goto err_unlock_group;
+		pt_id = &ioas_id;
+	}
+
 	mutex_lock(&device->dev_set->lock);
(quoted diff) +0 -0
 	/*
 	 * Here we pass the KVM pointer with the group under the lock.  If

the

drivers/vfio/group.c static int vfio_device_group_open(struct · line 188

Quoted 6 lines
drivers/vfio/group.c +1 -1
@@ -174,7 +184,7 @@ static int vfio_device_group_open(struct
 vfio_device_file *df)
 	df->kvm = device->group->kvm;
 	df->iommufd = device->group->iommufd;
 
-	ret = vfio_device_open(df);
+	ret = vfio_device_open(df, NULL, pt_id);

having both ioas_id and pt_id in one function is a bit confusing.

Does it read better with below?

if (device->group->iommufd)

	ret = vfio_device_open(df, NULL, &ioas_id);
else
	ret = vfio_device_open(df, NULL, NULL);

drivers/vfio/iommufd.c line 19

Quoted 7 lines
drivers/vfio/iommufd.c +7 -0
+/* @pt_id == NULL implies detach */
+int vfio_iommufd_attach(struct vfio_device *vdev, u32 *pt_id)
+{
+	lockdep_assert_held(&vdev->dev_set->lock);
+
+	return vdev->ops->attach_ioas(vdev, pt_id);
+}

what benefit does this one-line wrapper give actually?

especially pt_id==NULL is checked in the callback instead of in this wrapper.

author
#49 lore raw

RE: [PATCH 09/13] vfio: Add infrastructure for bind_iommufd and attach

Quoted 41 lines

From: Tian, Kevin <[email protected]>
Sent: Thursday, January 19, 2023 5:45 PM

Quoted 29 lines

From: Liu, Yi L <[email protected]>
Sent: Tuesday, January 17, 2023 9:50 PM
static int vfio_device_group_open(struct vfio_device_file *df) {

drivers/vfio/group.c +10 -0
 	struct vfio_device *device = df->device;
+	u32 ioas_id;
+	u32 *pt_id = NULL;
 	int ret;
 
 	mutex_lock(&device->group->group_lock);
@@ -165,6 +167,14 @@ static int vfio_device_group_open(struct
 vfio_device_file *df)
 		goto err_unlock_group;
 	}
 
+	if (device->group->iommufd) {
+		ret = iommufd_vfio_compat_ioas_id(device->group-
 >iommufd,
+						  &ioas_id);
+		if (ret)
+			goto err_unlock_group;
+		pt_id = &ioas_id;
+	}
+
 	mutex_lock(&device->dev_set->lock);
(quoted diff) +0 -0
 	/*
 	 * Here we pass the KVM pointer with the group under the lock.  If

the

drivers/vfio/group.c static int vfio_device_group_open(struct · line 188

Quoted 6 lines
drivers/vfio/group.c +1 -1
@@ -174,7 +184,7 @@ static int vfio_device_group_open(struct
 vfio_device_file *df)
 	df->kvm = device->group->kvm;
 	df->iommufd = device->group->iommufd;
 
-	ret = vfio_device_open(df);
+	ret = vfio_device_open(df, NULL, pt_id);

having both ioas_id and pt_id in one function is a bit confusing.

Quoted 6 lines

Does it read better with below?

if (device->group->iommufd)

	ret = vfio_device_open(df, NULL, &ioas_id);
else
	ret = vfio_device_open(df, NULL, NULL);

Yes. 😊

Quoted 10 lines

drivers/vfio/iommufd.c line 19

Quoted 7 lines
drivers/vfio/iommufd.c +7 -0
+/* @pt_id == NULL implies detach */
+int vfio_iommufd_attach(struct vfio_device *vdev, u32 *pt_id)
+{
+	lockdep_assert_held(&vdev->dev_set->lock);
+
+	return vdev->ops->attach_ioas(vdev, pt_id);
+}

what benefit does this one-line wrapper give actually?

Quoted 2 lines

especially pt_id==NULL is checked in the callback instead of in this wrapper.

Yep, will just open code in the caller.

Regards,
Yi Liu

#50 lore raw

Re: [PATCH 09/13] vfio: Add infrastructure for bind_iommufd and attach

In reply to Yi Liu
Quoted 57 lines · On Tue, 17 Jan 2023 05:49:38 -0800 Yi Liu <[email protected]> wrote:

This prepares to add ioctls for device cdev fd. This infrastructure includes:
- add vfio_iommufd_attach() to support iommufd pgtable attach after bind_iommufd. A NULL pt_id indicates detach.
- let vfio_iommufd_bind() to accept pt_id, e.g. the comapt_ioas_id in the legacy group path, and also return back dev_id if caller requires it.

---
 drivers/vfio/group.c     | 12 +++++-
 drivers/vfio/iommufd.c   | 79 ++++++++++++++++++++++++++++++----------
 drivers/vfio/vfio.h      | 15 ++++++--
 drivers/vfio/vfio_main.c | 10 +++--
 4 files changed, 88 insertions(+), 28 deletions(-)
drivers/vfio/group.c +11 -1
index 7200304663e5..9484bb1c54a9 100644
@@ -157,6 +157,8 @@ static int vfio_group_ioctl_set_container(struct vfio_group *group,
 static int vfio_device_group_open(struct vfio_device_file *df)
 {
 	struct vfio_device *device = df->device;
+	u32 ioas_id;
+	u32 *pt_id = NULL;
 	int ret;
 
 	mutex_lock(&device->group->group_lock);
@@ -165,6 +167,14 @@ static int vfio_device_group_open(struct vfio_device_file *df)
 		goto err_unlock_group;
 	}
 
+	if (device->group->iommufd) {
+		ret = iommufd_vfio_compat_ioas_id(device->group->iommufd,
+						  &ioas_id);
+		if (ret)
+			goto err_unlock_group;
+		pt_id = &ioas_id;
+	}
+
 	mutex_lock(&device->dev_set->lock);
 	/*
 	 * Here we pass the KVM pointer with the group under the lock.  If the
@@ -174,7 +184,7 @@ static int vfio_device_group_open(struct vfio_device_file *df)
 	df->kvm = device->group->kvm;
 	df->iommufd = device->group->iommufd;
 
-	ret = vfio_device_open(df);
+	ret = vfio_device_open(df, NULL, pt_id);
 	if (ret)
 		goto err_unlock_device;
 	mutex_unlock(&device->dev_set->lock);
drivers/vfio/iommufd.c +0 -0
index 4f82a6fa7c6c..412644fdbf16 100644
@@ -10,9 +10,17 @@
 MODULE_IMPORT_NS(IOMMUFD);

drivers/vfio/iommufd.c line 19

Quoted 10 lines
drivers/vfio/iommufd.c +7 -1
 MODULE_IMPORT_NS(IOMMUFD_VFIO);
 
-int vfio_iommufd_bind(struct vfio_device *vdev, struct iommufd_ctx *ictx)
+/* @pt_id == NULL implies detach */
+int vfio_iommufd_attach(struct vfio_device *vdev, u32 *pt_id)
+{
+	lockdep_assert_held(&vdev->dev_set->lock);
+
+	return vdev->ops->attach_ioas(vdev, pt_id);
+}

I find this patch pretty confusing, I think it's rooted in all these multiplexed interfaces, which extend all the way out to userspace with a magic, reserved page table ID to detach a device from an IOAS. It seems like it would be simpler to make a 'detach' API, a detach_ioas callback on the vfio_device_ops, and certainly not an vfio_iommufd_attach() function that does a detach provided the correct args while also introducing a __vfio_iommufd_detach() function.

This series is also missing an update to Documentation/driver-api/vfio.rst, which is already behind relative to the iommufd interfaces. Thanks,

Alex

author
#51 lore raw

RE: [PATCH 09/13] vfio: Add infrastructure for bind_iommufd and attach

Quoted 73 lines

From: Alex Williamson <[email protected]>
Sent: Friday, January 20, 2023 7:05 AM

Quoted 4 lines · On Tue, 17 Jan 2023 05:49:38 -0800 Yi Liu <[email protected]> wrote:

This prepares to add ioctls for device cdev fd. This infrastructure includes:
- add vfio_iommufd_attach() to support iommufd pgtable attach after bind_iommufd. A NULL pt_id indicates detach.
- let vfio_iommufd_bind() to accept pt_id, e.g. the comapt_ioas_id in

the

Quoted 6 lines

legacy group path, and also return back dev_id if caller requires it.

---
 drivers/vfio/group.c     | 12 +++++-
 drivers/vfio/iommufd.c   | 79 ++++++++++++++++++++++++++++++------

----

drivers/vfio/group.c static int vfio_group_ioctl_set_container(struct

Quoted 9 lines
 drivers/vfio/vfio.h      | 15 ++++++--
 drivers/vfio/vfio_main.c | 10 +++--
 4 files changed, 88 insertions(+), 28 deletions(-)
drivers/vfio/group.c +0 -0
index 7200304663e5..9484bb1c54a9 100644
@@ -157,6 +157,8 @@ static int vfio_group_ioctl_set_container(struct

vfio_group *group,

drivers/vfio/group.c static int vfio_device_group_open(struct

Quoted 9 lines
drivers/vfio/group.c +2 -0
 static int vfio_device_group_open(struct vfio_device_file *df)
 {
 	struct vfio_device *device = df->device;
+	u32 ioas_id;
+	u32 *pt_id = NULL;
 	int ret;
 
 	mutex_lock(&device->group->group_lock);
@@ -165,6 +167,14 @@ static int vfio_device_group_open(struct

vfio_device_file *df)

Quoted 14 lines
drivers/vfio/group.c +8 -0
 		goto err_unlock_group;
 	}
 
+	if (device->group->iommufd) {
+		ret = iommufd_vfio_compat_ioas_id(device->group-
 iommufd,
+						  &ioas_id);
+		if (ret)
+			goto err_unlock_group;
+		pt_id = &ioas_id;
+	}
+
 	mutex_lock(&device->dev_set->lock);
 	/*
Quoted 1 line

* Here we pass the KVM pointer with the group under the lock. If

the

drivers/vfio/group.c static int vfio_device_group_open(struct

Quoted 1 line
drivers/vfio/group.c +0 -0
@@ -174,7 +184,7 @@ static int vfio_device_group_open(struct

vfio_device_file *df)

Quoted 13 lines
drivers/vfio/group.c +1 -1
 	df->kvm = device->group->kvm;
 	df->iommufd = device->group->iommufd;
 
-	ret = vfio_device_open(df);
+	ret = vfio_device_open(df, NULL, pt_id);
 	if (ret)
 		goto err_unlock_device;
 	mutex_unlock(&device->dev_set->lock);
drivers/vfio/iommufd.c +0 -0
index 4f82a6fa7c6c..412644fdbf16 100644

drivers/vfio/iommufd.c line 13

Quoted 4 lines
drivers/vfio/iommufd.c +0 -1
@@ -10,9 +10,17 @@
 MODULE_IMPORT_NS(IOMMUFD);
 MODULE_IMPORT_NS(IOMMUFD_VFIO);
 
-int vfio_iommufd_bind(struct vfio_device *vdev, struct iommufd_ctx

*ictx)

Quoted 16 lines

drivers/vfio/iommufd.c line 19

Quoted 7 lines
drivers/vfio/iommufd.c +7 -0
+/* @pt_id == NULL implies detach */
+int vfio_iommufd_attach(struct vfio_device *vdev, u32 *pt_id)
+{
+	lockdep_assert_held(&vdev->dev_set->lock);
+
+	return vdev->ops->attach_ioas(vdev, pt_id);
+}

I find this patch pretty confusing, I think it's rooted in all these multiplexed interfaces, which extend all the way out to userspace with a magic, reserved page table ID to detach a device from an IOAS. It seems like it would be simpler to make a 'detach' API, a detach_ioas callback on the vfio_device_ops, and certainly not an vfio_iommufd_attach() function that does a detach provided the correct args while also introducing a __vfio_iommufd_detach() function.

Sure. Will change it.

Quoted 4 lines

This series is also missing an update to Documentation/driver-api/vfio.rst, which is already behind relative to the iommufd interfaces. Thanks,

Yeah, the vfio.rst is already a bit out-of-date. Will try to update it as well.

Regards,
Yi Liu

author
#52 lore raw

PATCH 10/13 vfio: Make vfio_device_open() exclusive between group path and device cdev path

In reply to Yi Liu

VFIO group has historically allowed multi-open of the device FD. This was made secure because the "open" was executed via an ioctl to the group FD which is itself only single open.

No know use of multiple device FDs is known. It is kind of a strange thing to do because new device FDs can naturally be created via dup().

When we implement the new device uAPI there is no natural way to allow the device itself from being multi-opened in a secure manner. Without the group FD we cannot prove the security context of the opener.

Thus, when moving to the new uAPI we block the ability to multi-open the device. This also makes the cdev path exclusive with group path.

The main logic is in the vfio_device_open(). It needs to sustain both the legacy behavior i.e. multi-open in the group path and the new behavior i.e. single-open in the cdev path. This mixture leads to the introduction of a new single_open flag stored both in struct vfio_device and vfio_device_file. vfio_device_file::single_open is set per the vfio_device_file allocation. Its value is propagated to struct vfio_device after device is opened successfully.

---
 drivers/vfio/group.c     |  2 +-
 drivers/vfio/vfio.h      |  6 +++++-
 drivers/vfio/vfio_main.c | 25 ++++++++++++++++++++++---
 include/linux/vfio.h     |  1 +
 4 files changed, 29 insertions(+), 5 deletions(-)
drivers/vfio/group.c +1 -1
index 9484bb1c54a9..57ebe5e1a7e6 100644
@@ -216,7 +216,7 @@ static struct file *vfio_device_open_file(struct vfio_device *device)
 	struct file *filep;
 	int ret;
 
-	df = vfio_allocate_device_file(device);
+	df = vfio_allocate_device_file(device, false);
 	if (IS_ERR(df)) {
 		ret = PTR_ERR(df);
 		goto err_out;
drivers/vfio/vfio.h +5 -1
index fe0fcfa78710..bdcf9762521d 100644
@@ -17,7 +17,11 @@ struct vfio_device;
 struct vfio_container;
 
 struct vfio_device_file {
+	/* static fields, init per allocation */
 	struct vfio_device *device;
+	bool single_open;
+
+	/* fields set after allocation */
 	struct kvm *kvm;
 	struct iommufd_ctx *iommufd;
 	bool access_granted;
@@ -30,7 +34,7 @@ int vfio_device_open(struct vfio_device_file *df,
 void vfio_device_close(struct vfio_device_file *device);
 
 struct vfio_device_file *
-vfio_allocate_device_file(struct vfio_device *device);
+vfio_allocate_device_file(struct vfio_device *device, bool single_open);
 
 extern const struct file_operations vfio_device_fops;
 
drivers/vfio/vfio_main.c +22 -3
index 90174a9015c4..78725c28b933 100644
@@ -345,7 +345,7 @@ static bool vfio_assert_device_open(struct vfio_device *device)
 }
 
 struct vfio_device_file *
-vfio_allocate_device_file(struct vfio_device *device)
+vfio_allocate_device_file(struct vfio_device *device, bool single_open)
 {
 	struct vfio_device_file *df;
 
@@ -354,6 +354,7 @@ vfio_allocate_device_file(struct vfio_device *device)
 		return ERR_PTR(-ENOMEM);
 
 	df->device = device;
+	df->single_open = single_open;
 
 	return df;
 }
@@ -421,6 +422,16 @@ int vfio_device_open(struct vfio_device_file *df,
 
 	lockdep_assert_held(&device->dev_set->lock);
 
+	/*
+	 * Device cdev path cannot support multiple device open since
+	 * it doesn't have a secure way for it. So a second device
+	 * open attempt should be failed if the caller is from a cdev
+	 * path or the device has already been opened by a cdev path.
+	 */
+	if (device->open_count != 0 &&
+	    (df->single_open || device->single_open))
+		return -EINVAL;
+
 	device->open_count++;
 	if (device->open_count == 1) {
 		int ret;
@@ -430,6 +441,7 @@ int vfio_device_open(struct vfio_device_file *df,
 			device->open_count--;
 			return ret;
 		}
+		device->single_open = df->single_open;
 	}
 
 	/*
@@ -446,8 +458,10 @@ void vfio_device_close(struct vfio_device_file *df)
 
 	mutex_lock(&device->dev_set->lock);
 	vfio_assert_device_open(device);
-	if (device->open_count == 1)
+	if (device->open_count == 1) {
 		vfio_device_last_close(df);
+		device->single_open = false;
+	}
 	device->open_count--;
 	mutex_unlock(&device->dev_set->lock);
 }
@@ -493,7 +507,12 @@ static int vfio_device_fops_release(struct inode *inode, struct file *filep)
 	struct vfio_device_file *df = filep->private_data;
 	struct vfio_device *device = df->device;
 
-	vfio_device_group_close(df);
+	/*
+	 * group path supports multiple device open, while cdev doesn't.
+	 * So use vfio_device_group_close() for !singel_open case.
+	 */
+	if (!df->single_open)
+		vfio_device_group_close(df);
 
 	vfio_device_put_registration(device);
 
include/linux/vfio.h +1 -0
index 46edd6e6c0ba..300318f0d448 100644
@@ -63,6 +63,7 @@ struct vfio_device {
 	struct iommufd_ctx *iommufd_ictx;
 	bool iommufd_attached;
 #endif
+	bool single_open;
 };
 
 /**
Signature · 1 line
2.34.1
#53 lore raw

Re: [PATCH 10/13] vfio: Make vfio_device_open() exclusive between group path and device cdev path

Quoted 5 lines · On Tue, 17 Jan 2023 05:49:39 -0800 Yi Liu <[email protected]> wrote:

VFIO group has historically allowed multi-open of the device FD. This was made secure because the "open" was executed via an ioctl to the group FD which is itself only single open.

No know use of multiple device FDs is known. It is kind of a strange

^^ ^^^^ ^^^^^

Quoted 77 lines

thing to do because new device FDs can naturally be created via dup().

When we implement the new device uAPI there is no natural way to allow the device itself from being multi-opened in a secure manner. Without the group FD we cannot prove the security context of the opener.

Thus, when moving to the new uAPI we block the ability to multi-open the device. This also makes the cdev path exclusive with group path.

The main logic is in the vfio_device_open(). It needs to sustain both the legacy behavior i.e. multi-open in the group path and the new behavior i.e. single-open in the cdev path. This mixture leads to the introduction of a new single_open flag stored both in struct vfio_device and vfio_device_file. vfio_device_file::single_open is set per the vfio_device_file allocation. Its value is propagated to struct vfio_device after device is opened successfully.

---
 drivers/vfio/group.c     |  2 +-
 drivers/vfio/vfio.h      |  6 +++++-
 drivers/vfio/vfio_main.c | 25 ++++++++++++++++++++++---
 include/linux/vfio.h     |  1 +
 4 files changed, 29 insertions(+), 5 deletions(-)
drivers/vfio/group.c +1 -1
index 9484bb1c54a9..57ebe5e1a7e6 100644
@@ -216,7 +216,7 @@ static struct file *vfio_device_open_file(struct vfio_device *device)
 	struct file *filep;
 	int ret;
 
-	df = vfio_allocate_device_file(device);
+	df = vfio_allocate_device_file(device, false);
 	if (IS_ERR(df)) {
 		ret = PTR_ERR(df);
 		goto err_out;
drivers/vfio/vfio.h +5 -1
index fe0fcfa78710..bdcf9762521d 100644
@@ -17,7 +17,11 @@ struct vfio_device;
 struct vfio_container;
 
 struct vfio_device_file {
+	/* static fields, init per allocation */
 	struct vfio_device *device;
+	bool single_open;
+
+	/* fields set after allocation */
 	struct kvm *kvm;
 	struct iommufd_ctx *iommufd;
 	bool access_granted;
@@ -30,7 +34,7 @@ int vfio_device_open(struct vfio_device_file *df,
 void vfio_device_close(struct vfio_device_file *device);
 
 struct vfio_device_file *
-vfio_allocate_device_file(struct vfio_device *device);
+vfio_allocate_device_file(struct vfio_device *device, bool single_open);
 
 extern const struct file_operations vfio_device_fops;
 
drivers/vfio/vfio_main.c +1 -1
index 90174a9015c4..78725c28b933 100644
@@ -345,7 +345,7 @@ static bool vfio_assert_device_open(struct vfio_device *device)
 }
 
 struct vfio_device_file *
-vfio_allocate_device_file(struct vfio_device *device)
+vfio_allocate_device_file(struct vfio_device *device, bool single_open)
 {
 	struct vfio_device_file *df;
 

drivers/vfio/vfio_main.c vfio_allocate_device_file(struct vfio_device *device) · line 357

Quoted 4 lines
drivers/vfio/vfio_main.c +1 -0
@@ -354,6 +354,7 @@ vfio_allocate_device_file(struct vfio_device *device)
 		return ERR_PTR(-ENOMEM);
 
 	df->device = device;
+	df->single_open = single_open;

It doesn't make sense to me to convolute the definition of this function with an unmemorable bool arg when the one caller that sets the value true could simply open code it.

Quoted 6 lines
drivers/vfio/vfio_main.c +0 -0
 	return df;
 }
@@ -421,6 +422,16 @@ int vfio_device_open(struct vfio_device_file *df,
 
 	lockdep_assert_held(&device->dev_set->lock);

drivers/vfio/vfio_main.c int vfio_device_open(struct vfio_device_file *df, · line 433

Quoted 10 lines
drivers/vfio/vfio_main.c +9 -0
 
+	/*
+	 * Device cdev path cannot support multiple device open since
+	 * it doesn't have a secure way for it. So a second device
+	 * open attempt should be failed if the caller is from a cdev
+	 * path or the device has already been opened by a cdev path.
+	 */
+	if (device->open_count != 0 &&
+	    (df->single_open || device->single_open))
+		return -EINVAL;

IIUC, the reason this exists is that we let the user open the device cdev arbitrarily, but only one instance can call ioctl(VFIO_DEVICE_BIND_IOMMUFD). Why do we bother to let the user create those other file instances? What expectations are we setting for the user by allowing them to open the device but not use it?

Clearly we're thinking about a case here where the device has been opened via the group path and the user is now attempting to bind the same device via the cdev path. That seems wrong to even allow and I'm surprised it gets this far. In fact, where do we block a user from opening one device in a group via cdev and another via the group?

Quoted 25 lines
drivers/vfio/vfio_main.c +5 -1
+
 	device->open_count++;
 	if (device->open_count == 1) {
 		int ret;
@@ -430,6 +441,7 @@ int vfio_device_open(struct vfio_device_file *df,
 			device->open_count--;
 			return ret;
 		}
+		device->single_open = df->single_open;
 	}
 
 	/*
@@ -446,8 +458,10 @@ void vfio_device_close(struct vfio_device_file *df)
 
 	mutex_lock(&device->dev_set->lock);
 	vfio_assert_device_open(device);
-	if (device->open_count == 1)
+	if (device->open_count == 1) {
 		vfio_device_last_close(df);
+		device->single_open = false;
+	}
 	device->open_count--;
 	mutex_unlock(&device->dev_set->lock);
 }

drivers/vfio/vfio_main.c static int vfio_device_fops_release(struct inode *inode, struct file *filep) · line 515

Quoted 10 lines
drivers/vfio/vfio_main.c +6 -1
@@ -493,7 +507,12 @@ static int vfio_device_fops_release(struct inode *inode, struct file *filep)
 	struct vfio_device_file *df = filep->private_data;
 	struct vfio_device *device = df->device;
 
-	vfio_device_group_close(df);
+	/*
+	 * group path supports multiple device open, while cdev doesn't.
+	 * So use vfio_device_group_close() for !singel_open case.
+	 */
+	if (!df->single_open)
+		vfio_device_group_close(df);

If we're going to use this to differentiate group vs cdev use cases, then let's name it something to reflect that rather than pretending it only limits the number of opens, ex. is_cdev_device. Thanks,

Alex

Quoted 15 lines
drivers/vfio/vfio_main.c +0 -0
 	vfio_device_put_registration(device);
 
include/linux/vfio.h +1 -0
index 46edd6e6c0ba..300318f0d448 100644
@@ -63,6 +63,7 @@ struct vfio_device {
 	struct iommufd_ctx *iommufd_ictx;
 	bool iommufd_attached;
 #endif
+	bool single_open;
 };
 
 /**
author
#54 lore raw

RE: [PATCH 10/13] vfio: Make vfio_device_open() exclusive between group path and device cdev path

Quoted 6 lines

From: Alex Williamson <[email protected]>
Sent: Friday, January 20, 2023 7:52 AM

On Tue, 17 Jan 2023 05:49:39 -0800
Yi Liu <[email protected]> wrote:

Quoted 6 lines
Quoted 5 lines

VFIO group has historically allowed multi-open of the device FD. This was made secure because the "open" was executed via an ioctl to the group FD which is itself only single open.

No know use of multiple device FDs is known. It is kind of a strange

^^ ^^^^ ^^^^^

How about "No known use of multiple device FDs today"

Quoted 80 lines

drivers/vfio/group.c static struct file *vfio_device_open_file(struct

Quoted 30 lines

thing to do because new device FDs can naturally be created via dup().

When we implement the new device uAPI there is no natural way to allow the device itself from being multi-opened in a secure manner. Without the group FD we cannot prove the security context of the opener.

Thus, when moving to the new uAPI we block the ability to multi-open the device. This also makes the cdev path exclusive with group path.

The main logic is in the vfio_device_open(). It needs to sustain both the legacy behavior i.e. multi-open in the group path and the new behavior i.e. single-open in the cdev path. This mixture leads to the introduction of a new single_open flag stored both in struct vfio_device and vfio_device_file. vfio_device_file::single_open is set per the vfio_device_file allocation. Its value is propagated to struct vfio_device after device is opened successfully.

---
 drivers/vfio/group.c     |  2 +-
 drivers/vfio/vfio.h      |  6 +++++-
 drivers/vfio/vfio_main.c | 25 ++++++++++++++++++++++---
 include/linux/vfio.h     |  1 +
 4 files changed, 29 insertions(+), 5 deletions(-)
drivers/vfio/group.c +0 -0
index 9484bb1c54a9..57ebe5e1a7e6 100644
@@ -216,7 +216,7 @@ static struct file *vfio_device_open_file(struct

vfio_device *device)

drivers/vfio/vfio_main.c static bool vfio_assert_device_open(struct

Quoted 38 lines
drivers/vfio/group.c +1 -1
 	struct file *filep;
 	int ret;
 
-	df = vfio_allocate_device_file(device);
+	df = vfio_allocate_device_file(device, false);
 	if (IS_ERR(df)) {
 		ret = PTR_ERR(df);
 		goto err_out;
drivers/vfio/vfio.h +5 -1
index fe0fcfa78710..bdcf9762521d 100644
@@ -17,7 +17,11 @@ struct vfio_device;
 struct vfio_container;
 
 struct vfio_device_file {
+	/* static fields, init per allocation */
 	struct vfio_device *device;
+	bool single_open;
+
+	/* fields set after allocation */
 	struct kvm *kvm;
 	struct iommufd_ctx *iommufd;
 	bool access_granted;
@@ -30,7 +34,7 @@ int vfio_device_open(struct vfio_device_file *df,
 void vfio_device_close(struct vfio_device_file *device);
 
 struct vfio_device_file *
-vfio_allocate_device_file(struct vfio_device *device);
+vfio_allocate_device_file(struct vfio_device *device, bool single_open);
 
 extern const struct file_operations vfio_device_fops;
 
drivers/vfio/vfio_main.c +0 -0
index 90174a9015c4..78725c28b933 100644
@@ -345,7 +345,7 @@ static bool vfio_assert_device_open(struct

vfio_device *device)

drivers/vfio/vfio_main.c vfio_allocate_device_file(struct vfio_device

Quoted 9 lines
drivers/vfio/vfio_main.c +1 -1
 }
 
 struct vfio_device_file *
-vfio_allocate_device_file(struct vfio_device *device)
+vfio_allocate_device_file(struct vfio_device *device, bool single_open)
 {
 	struct vfio_device_file *df;
 
@@ -354,6 +354,7 @@ vfio_allocate_device_file(struct vfio_device

*device)

Quoted 8 lines

drivers/vfio/vfio_main.c vfio_allocate_device_file(struct vfio_device *device) · line 357

Quoted 4 lines
drivers/vfio/vfio_main.c +1 -0
 		return ERR_PTR(-ENOMEM);
 
 	df->device = device;
+	df->single_open = single_open;

It doesn't make sense to me to convolute the definition of this function with an unmemorable bool arg when the one caller that sets the value true could simply open code it.

Yeah, how about renaming it just like Kevin's suggestion?

https://lore.kernel.org/kvm/BN9PR11MB52769CBCA68CD25DAC96B33B8CC49@BN9PR11MB5276.namprd11.prod.outlook.com/

Quoted 8 lines
Quoted 8 lines
drivers/vfio/vfio_main.c +0 -0
 	return df;
 }
@@ -421,6 +422,16 @@ int vfio_device_open(struct vfio_device_file *df,
 
 	lockdep_assert_held(&device->dev_set->lock);
 
Quoted 15 lines

drivers/vfio/vfio_main.c int vfio_device_open(struct vfio_device_file *df, · line 433

Quoted 9 lines
drivers/vfio/vfio_main.c +9 -0
+	/*
+	 * Device cdev path cannot support multiple device open since
+	 * it doesn't have a secure way for it. So a second device
+	 * open attempt should be failed if the caller is from a cdev
+	 * path or the device has already been opened by a cdev path.
+	 */
+	if (device->open_count != 0 &&
+	    (df->single_open || device->single_open))
+		return -EINVAL;

IIUC, the reason this exists is that we let the user open the device cdev arbitrarily, but only one instance can call ioctl(VFIO_DEVICE_BIND_IOMMUFD). Why do we bother to let the user create those other file instances? What expectations are we setting for the user by allowing them to open the device but not use it?

It won't be able to access device as such device fd is not bound to an iommufd.

Quoted 3 lines

Clearly we're thinking about a case here where the device has been opened via the group path and the user is now attempting to bind the same device via the cdev path.

This shall fail as the group path would inc the device->open_count. Then the cdev path will be failed as the path would have df->single_open==true.

Quoted 3 lines

That seems wrong to even allow and I'm
surprised it gets this far. In fact, where do we block a user from opening one device in a group via cdev and another via the group?

such scenario would be failed by the DMA owner.

The two paths would be excluded when claiming DMA ownership in such scenario. The group path uses the vfio_group pointer as DMA owner marker. While the cdev path uses the iommufd_ctx pointer. But one group only allows one DMA owner.

Quoted 30 lines

drivers/vfio/vfio_main.c static int vfio_device_fops_release(struct inode

Quoted 26 lines
drivers/vfio/vfio_main.c +5 -1
+
 	device->open_count++;
 	if (device->open_count == 1) {
 		int ret;
@@ -430,6 +441,7 @@ int vfio_device_open(struct vfio_device_file *df,
 			device->open_count--;
 			return ret;
 		}
+		device->single_open = df->single_open;
 	}
 
 	/*
@@ -446,8 +458,10 @@ void vfio_device_close(struct vfio_device_file *df)
 
 	mutex_lock(&device->dev_set->lock);
 	vfio_assert_device_open(device);
-	if (device->open_count == 1)
+	if (device->open_count == 1) {
 		vfio_device_last_close(df);
+		device->single_open = false;
+	}
 	device->open_count--;
 	mutex_unlock(&device->dev_set->lock);
 }
@@ -493,7 +507,12 @@ static int vfio_device_fops_release(struct inode

*inode, struct file *filep)

Quoted 3 lines
drivers/vfio/vfio_main.c +0 -0
 	struct vfio_device_file *df = filep->private_data;
 	struct vfio_device *device = df->device;
 
Quoted 11 lines

drivers/vfio/vfio_main.c static int vfio_device_fops_release(struct inode *inode, struct file *filep) · line 515

Quoted 7 lines
drivers/vfio/vfio_main.c +6 -1
-	vfio_device_group_close(df);
+	/*
+	 * group path supports multiple device open, while cdev doesn't.
+	 * So use vfio_device_group_close() for !singel_open case.
+	 */
+	if (!df->single_open)
+		vfio_device_group_close(df);

If we're going to use this to differentiate group vs cdev use cases, then let's name it something to reflect that rather than pretending it only limits the number of opens, ex. is_cdev_device. Thanks,

Yes. I'd follow it. Kevin has a similar comment on it.

Regards,
Yi Liu

author
#55 lore raw

RE: [PATCH 10/13] vfio: Make vfio_device_open() exclusive between group path and device cdev path

Hi Alex,

Quoted 153 lines

From: Liu, Yi L <[email protected]>
Sent: Monday, January 30, 2023 8:15 PM

Quoted 6 lines

From: Alex Williamson <[email protected]>
Sent: Friday, January 20, 2023 7:52 AM

On Tue, 17 Jan 2023 05:49:39 -0800
Yi Liu <[email protected]> wrote:

Quoted 6 lines
Quoted 5 lines

VFIO group has historically allowed multi-open of the device FD. This was made secure because the "open" was executed via an ioctl to the group FD which is itself only single open.

No know use of multiple device FDs is known. It is kind of a strange

^^ ^^^^ ^^^^^

How about "No known use of multiple device FDs today"

Quoted 3 lines
Quoted 3 lines

thing to do because new device FDs can naturally be created via dup().

When we implement the new device uAPI there is no natural way to

allow

Quoted 55 lines

drivers/vfio/group.c static struct file *vfio_device_open_file(struct

Quoted 27 lines

the device itself from being multi-opened in a secure manner. Without the group FD we cannot prove the security context of the opener.

Thus, when moving to the new uAPI we block the ability to multi-open the device. This also makes the cdev path exclusive with group path.

The main logic is in the vfio_device_open(). It needs to sustain both the legacy behavior i.e. multi-open in the group path and the new behavior i.e. single-open in the cdev path. This mixture leads to the introduction of a new single_open flag stored both in struct vfio_device and vfio_device_file. vfio_device_file::single_open is set per the vfio_device_file allocation. Its value is propagated to struct vfio_device after device is opened successfully.

---
 drivers/vfio/group.c     |  2 +-
 drivers/vfio/vfio.h      |  6 +++++-
 drivers/vfio/vfio_main.c | 25 ++++++++++++++++++++++---
 include/linux/vfio.h     |  1 +
 4 files changed, 29 insertions(+), 5 deletions(-)
drivers/vfio/group.c +0 -0
index 9484bb1c54a9..57ebe5e1a7e6 100644
@@ -216,7 +216,7 @@ static struct file *vfio_device_open_file(struct

vfio_device *device)

Quoted 27 lines
drivers/vfio/group.c +1 -1
 	struct file *filep;
 	int ret;
 
-	df = vfio_allocate_device_file(device);
+	df = vfio_allocate_device_file(device, false);
 	if (IS_ERR(df)) {
 		ret = PTR_ERR(df);
 		goto err_out;
drivers/vfio/vfio.h +4 -0
index fe0fcfa78710..bdcf9762521d 100644
@@ -17,7 +17,11 @@ struct vfio_device;
 struct vfio_container;
 
 struct vfio_device_file {
+	/* static fields, init per allocation */
 	struct vfio_device *device;
+	bool single_open;
+
+	/* fields set after allocation */
 	struct kvm *kvm;
 	struct iommufd_ctx *iommufd;
 	bool access_granted;
@@ -30,7 +34,7 @@ int vfio_device_open(struct vfio_device_file *df,
 void vfio_device_close(struct vfio_device_file *device);
 
Quoted 3 lines

drivers/vfio/vfio.h int vfio_device_open(struct vfio_device_file *df,

Quoted 3 lines
drivers/vfio/vfio.h +1 -1
 struct vfio_device_file *
-vfio_allocate_device_file(struct vfio_device *device);
+vfio_allocate_device_file(struct vfio_device *device, bool

single_open);

Quoted 19 lines

drivers/vfio/vfio_main.c static bool vfio_assert_device_open(struct

Quoted 8 lines
drivers/vfio/vfio.h +0 -0
 extern const struct file_operations vfio_device_fops;
 
drivers/vfio/vfio_main.c +0 -0
index 90174a9015c4..78725c28b933 100644
@@ -345,7 +345,7 @@ static bool vfio_assert_device_open(struct

vfio_device *device)

drivers/vfio/vfio_main.c vfio_allocate_device_file(struct vfio_device

Quoted 9 lines
drivers/vfio/vfio_main.c +1 -1
 }
 
 struct vfio_device_file *
-vfio_allocate_device_file(struct vfio_device *device)
+vfio_allocate_device_file(struct vfio_device *device, bool single_open)
 {
 	struct vfio_device_file *df;
 
@@ -354,6 +354,7 @@ vfio_allocate_device_file(struct vfio_device

*device)

Quoted 8 lines

drivers/vfio/vfio_main.c vfio_allocate_device_file(struct vfio_device *device) · line 357

Quoted 4 lines
drivers/vfio/vfio_main.c +1 -0
 		return ERR_PTR(-ENOMEM);
 
 	df->device = device;
+	df->single_open = single_open;

It doesn't make sense to me to convolute the definition of this function with an unmemorable bool arg when the one caller that sets the value true could simply open code it.

Yeah, how about renaming it just like Kevin's suggestion?

https://lore.kernel.org/kvm/BN9PR11MB52769CBCA68CD25DAC96B33B8CC
[email protected]/

Quoted 5 lines

drivers/vfio/vfio_main.c int vfio_device_open(struct vfio_device_file

Quoted 5 lines
drivers/vfio/vfio_main.c +0 -0
 	return df;
 }
@@ -421,6 +422,16 @@ int vfio_device_open(struct vfio_device_file

*df,

Quoted 18 lines

drivers/vfio/vfio_main.c int vfio_device_open(struct vfio_device_file *df, · line 433

Quoted 12 lines
drivers/vfio/vfio_main.c +9 -0
 	lockdep_assert_held(&device->dev_set->lock);
 
+	/*
+	 * Device cdev path cannot support multiple device open since
+	 * it doesn't have a secure way for it. So a second device
+	 * open attempt should be failed if the caller is from a cdev
+	 * path or the device has already been opened by a cdev path.
+	 */
+	if (device->open_count != 0 &&
+	    (df->single_open || device->single_open))
+		return -EINVAL;

IIUC, the reason this exists is that we let the user open the device cdev arbitrarily, but only one instance can call ioctl(VFIO_DEVICE_BIND_IOMMUFD). Why do we bother to let the user create those other file instances? What expectations are we setting for the user by allowing them to open the device but not use it?

It won't be able to access device as such device fd is not bound to an iommufd.

Quoted 3 lines

Clearly we're thinking about a case here where the device has been opened via the group path and the user is now attempting to bind the same device via the cdev path.

This shall fail as the group path would inc the device->open_count. Then the cdev path will be failed as the path would have df->single_open==true.

Quoted 3 lines

That seems wrong to even allow and I'm
surprised it gets this far. In fact, where do we block a user from opening one device in a group via cdev and another via the group?

Quoted 6 lines

such scenario would be failed by the DMA owner.

The two paths would be excluded when claiming DMA ownership in such scenario. The group path uses the vfio_group pointer as DMA owner marker. While the cdev path uses the iommufd_ctx pointer. But one group only allows one DMA owner.

However, there is one possibility that the group path and cdev path have the same DMA marker. If the group path is the vfio iommufd compat mode, iommufd is used as container fd, and its DMA marker is also iommufd_ctx pointer, so it is possible that two devices in the the same group may be opened by different paths (the vfio compat mode group path and the cdev path).

This seems to be ok. The group path will attach the group to an auto-allocated iommu_domain, while the cdev path actually waits for userspace to attach it to an IOAS. Userspace should take care of it. It should ensure the devices in the same group should be attached to the same domain.

This seems to be no much difference with userspace opening two devices (from the same group) via cdev, userspace needs to attach them to the same domain as well.

Also, there is a work from Nicolin to support domain replacement. So even
the group path has attached the group to an iommu_domain, userspace can
later replace it with the domain it desired (maybe still an auto-allocated domain during the IOAS attachment or a userspace allocated domain this is
in my nesting series https://github.com/yiliu1765/iommufd/commits/wip/iommufd-v6.2-rc5-nesting).

So for a VFIO device, we use the single_open flag to exclude opening from the group path and cdev path. While for different devices in the same group, normally userspace may not open devices in different paths. Even it does, seems to be fine as above statement.

Regards,
Yi Liu

maintainer
#56 lore raw

Re: [PATCH 10/13] vfio: Make vfio_device_open() exclusive between group path and device cdev path

Quoted 4 lines · Yi L

This seems to be ok. The group path will attach the group to an auto-allocated iommu_domain, while the cdev path actually waits for userspace to attach it to an IOAS. Userspace should take care of it. It should ensure the devices in the same group should be attached to the same domain.

Aren't there problems when someone closes the group or device FD while the other one is still open though?

Jason

author
#57 lore raw

RE: [PATCH 10/13] vfio: Make vfio_device_open() exclusive between group path and device cdev path

Quoted 5 lines

From: Jason Gunthorpe <[email protected]>
Sent: Saturday, February 4, 2023 1:41 AM

On Thu, Feb 02, 2023 at 05:34:15AM +0000, Liu, Yi L wrote:

Quoted 8 lines
Quoted 1 line

This seems to be ok. The group path will attach the group to an auto-

allocated

Quoted 3 lines

iommu_domain, while the cdev path actually waits for userspace to attach it to an IOAS. Userspace should take care of it. It should ensure the devices in the same group should be attached to the same domain.

Aren't there problems when someone closes the group or device FD while the other one is still open though?

Guess no. userspace can only open devices from the same group by both group path and cdev path when the group path is iommufd compat mode and uses the same iommufd as cdev path. This means the attach API are the same in the two paths. I think iommufd attach API is able to manage one device is closed while other devices are still attached.

Regards,
Yi Liu

maintainer
#58 lore raw

RE: [PATCH 10/13] vfio: Make vfio_device_open() exclusive between group path and device cdev path

Quoted 14 lines

From: Liu, Yi L <[email protected]>
Sent: Monday, February 6, 2023 12:31 PM

Quoted 11 lines

From: Jason Gunthorpe <[email protected]>
Sent: Saturday, February 4, 2023 1:41 AM

Quoted 1 line · Yi L

This seems to be ok. The group path will attach the group to an auto-

allocated

Quoted 3 lines

iommu_domain, while the cdev path actually waits for userspace to attach it to an IOAS. Userspace should take care of it. It should ensure the devices in the same group should be attached to the same domain.

Quoted 9 lines
Quoted 2 lines

Aren't there problems when someone closes the group or device FD while the other one is still open though?

Guess no. userspace can only open devices from the same group by both group path and cdev path when the group path is iommufd compat mode and uses the same iommufd as cdev path. This means the attach API are the same in the two paths. I think iommufd attach API is able to manage one device is closed while other devices are still attached.

I guess the problem is on DMA ownership.

iommu_group_release_dma_owner() just blindly set group->owner_cnt to 0. So if someone closes the group FD while cdev FD is still open, the ownership model is completely broken.

IMHO using iommufd_ctx to mark DMA ownership for iommufd compat doesn't sound right. The group claim/release helpers are not designed to be exclusive but due to sharing internal logic with device claim/release we then allow group/cdev to share ownership when a same owner is marked.

It's probably simpler if we always mark DMA owner with vfio_group for the group path, no matter vfio type1 or iommufd compat is used. This should avoid all the tricky corner cases between the two paths.

Thanks
Kevin

maintainer
#59 lore raw

Re: [PATCH 10/13] vfio: Make vfio_device_open() exclusive between group path and device cdev path

Quoted 3 lines · Kevin

It's probably simpler if we always mark DMA owner with vfio_group for the group path, no matter vfio type1 or iommufd compat is used. This should avoid all the tricky corner cases between the two paths.

Yes

Jason

author
#60 lore raw

RE: [PATCH 10/13] vfio: Make vfio_device_open() exclusive between group path and device cdev path

Quoted 9 lines

From: Jason Gunthorpe <[email protected]>
Sent: Monday, February 6, 2023 11:11 PM

Quoted 3 lines · Kevin

It's probably simpler if we always mark DMA owner with vfio_group for the group path, no matter vfio type1 or iommufd compat is used. This should avoid all the tricky corner cases between the two paths.

Yes

Then, we have two choices:

1) extend iommufd_device_bind() to allow a caller-specified DMA marker
2) claim DMA owner before calling iommufd_device_bind(), still need to extend iommufd_device_bind() to accept a flag to bypass DMA owner claim

which one would be better? or do we have a third choice?

Regards,
Yi Liu

maintainer
#61 lore raw

RE: [PATCH 10/13] vfio: Make vfio_device_open() exclusive between group path and device cdev path

Quoted 13 lines

From: Liu, Yi L <[email protected]>
Sent: Monday, February 6, 2023 11:51 PM

Quoted 9 lines

From: Jason Gunthorpe <[email protected]>
Sent: Monday, February 6, 2023 11:11 PM

Quoted 3 lines · Kevin

It's probably simpler if we always mark DMA owner with vfio_group for the group path, no matter vfio type1 or iommufd compat is used. This should avoid all the tricky corner cases between the two paths.

Yes

Quoted 9 lines

Then, we have two choices:

1) extend iommufd_device_bind() to allow a caller-specified DMA marker
2) claim DMA owner before calling iommufd_device_bind(), still need to extend iommufd_device_bind() to accept a flag to bypass DMA owner
claim

which one would be better? or do we have a third choice?

first one

maintainer
#62 lore raw

Re: [PATCH 10/13] vfio: Make vfio_device_open() exclusive between group path and device cdev path

Quoted 20 lines · Kevin
Quoted 20 lines

From: Liu, Yi L <[email protected]>
Sent: Monday, February 6, 2023 11:51 PM

Quoted 9 lines

From: Jason Gunthorpe <[email protected]>
Sent: Monday, February 6, 2023 11:11 PM

Quoted 3 lines · Kevin

It's probably simpler if we always mark DMA owner with vfio_group for the group path, no matter vfio type1 or iommufd compat is used. This should avoid all the tricky corner cases between the two paths.

Yes

Then, we have two choices:

1) extend iommufd_device_bind() to allow a caller-specified DMA marker
2) claim DMA owner before calling iommufd_device_bind(), still need to extend iommufd_device_bind() to accept a flag to bypass DMA owner
claim

Quoted 4 lines
Quoted 2 lines

which one would be better? or do we have a third choice?

first one

Why can't this all be handled in vfio??

Jason

author
#63 lore raw

RE: [PATCH 10/13] vfio: Make vfio_device_open() exclusive between group path and device cdev path

Quoted 24 lines

From: Jason Gunthorpe <[email protected]>
Sent: Tuesday, February 7, 2023 9:13 PM

Quoted 11 lines · Kevin
Quoted 11 lines

From: Liu, Yi L <[email protected]>
Sent: Monday, February 6, 2023 11:51 PM

Quoted 8 lines

From: Jason Gunthorpe <[email protected]>
Sent: Monday, February 6, 2023 11:11 PM

Quoted 3 lines · Kevin

It's probably simpler if we always mark DMA owner with vfio_group for the group path, no matter vfio type1 or iommufd compat is used. This should avoid all the tricky corner cases between the two paths.

Quoted 5 lines
Quoted 5 lines
Quoted 1 line

Yes

Then, we have two choices:

1) extend iommufd_device_bind() to allow a caller-specified DMA

marker

Quoted 2 lines
Quoted 2 lines

2) claim DMA owner before calling iommufd_device_bind(), still need to extend iommufd_device_bind() to accept a flag to bypass DMA

owner

Quoted 8 lines
Quoted 6 lines
Quoted 4 lines

claim

which one would be better? or do we have a third choice?

first one

Why can't this all be handled in vfio??

Are you preferring the second one? Surely VFIO can claim DMA owner by itself. But it is the vfio iommufd compat mode, so it still needs to call iommufd_device_bind(). And it should bypass DMA owner claim since it's already done.

Regards,
Yi Liu

maintainer
#64 lore raw

Re: [PATCH 10/13] vfio: Make vfio_device_open() exclusive between group path and device cdev path

Quoted 29 lines · Yi L
Quoted 29 lines

From: Jason Gunthorpe <[email protected]>
Sent: Tuesday, February 7, 2023 9:13 PM

Quoted 11 lines · Kevin
Quoted 11 lines

From: Liu, Yi L <[email protected]>
Sent: Monday, February 6, 2023 11:51 PM

Quoted 8 lines

From: Jason Gunthorpe <[email protected]>
Sent: Monday, February 6, 2023 11:11 PM

Quoted 3 lines · Kevin

It's probably simpler if we always mark DMA owner with vfio_group for the group path, no matter vfio type1 or iommufd compat is used. This should avoid all the tricky corner cases between the two paths.

Quoted 5 lines
Quoted 5 lines
Quoted 1 line

Yes

Then, we have two choices:

1) extend iommufd_device_bind() to allow a caller-specified DMA

marker

Quoted 2 lines
Quoted 2 lines

2) claim DMA owner before calling iommufd_device_bind(), still need to extend iommufd_device_bind() to accept a flag to bypass DMA

owner

Quoted 5 lines
Quoted 4 lines

claim

which one would be better? or do we have a third choice?

Quoted 8 lines
Quoted 3 lines
Quoted 1 line

first one

Why can't this all be handled in vfio??

Are you preferring the second one? Surely VFIO can claim DMA owner by itself. But it is the vfio iommufd compat mode, so it still needs to call iommufd_device_bind(). And it should bypass DMA owner claim since it's already done.

No, I mean why can't vfio just call iommufd exactly once regardless of what mode it is running in?

Jason

author
#65 lore raw

RE: [PATCH 10/13] vfio: Make vfio_device_open() exclusive between group path and device cdev path

Quoted 41 lines

From: Jason Gunthorpe <[email protected]>
Sent: Tuesday, February 7, 2023 9:20 PM

Quoted 4 lines · Yi L
Quoted 4 lines

From: Jason Gunthorpe <[email protected]>
Sent: Tuesday, February 7, 2023 9:13 PM

On Tue, Feb 07, 2023 at 12:35:48AM +0000, Tian, Kevin wrote:

Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines

From: Liu, Yi L <[email protected]>
Sent: Monday, February 6, 2023 11:51 PM

Quoted 5 lines

From: Jason Gunthorpe <[email protected]>
Sent: Monday, February 6, 2023 11:11 PM

Quoted 1 line · Kevin

It's probably simpler if we always mark DMA owner with

vfio_group

Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

for the group path, no matter vfio type1 or iommufd compat is

used.

Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

This should avoid all the tricky corner cases between the two

paths.

Quoted 8 lines
Quoted 8 lines
Quoted 6 lines
Quoted 6 lines
Quoted 2 lines

Yes

Then, we have two choices:

1) extend iommufd_device_bind() to allow a caller-specified DMA

marker

Quoted 1 line
Quoted 1 line

2) claim DMA owner before calling iommufd_device_bind(), still

need to

Quoted 11 lines
Quoted 10 lines
Quoted 1 line
Quoted 1 line

extend iommufd_device_bind() to accept a flag to bypass DMA

owner

Quoted 6 lines
Quoted 4 lines

claim

which one would be better? or do we have a third choice?

first one

Why can't this all be handled in vfio??

Quoted 7 lines
Quoted 4 lines

Are you preferring the second one? Surely VFIO can claim DMA owner by itself. But it is the vfio iommufd compat mode, so it still needs to call iommufd_device_bind(). And it should bypass DMA owner claim since it's already done.

No, I mean why can't vfio just call iommufd exactly once regardless of what mode it is running in?

This seems to be moving the DMA owner claim out of iommufd_device_bind(). Is it? Then either group and cdev can claim DMA owner with their own DMA marker.

Regards,
Yi Liu

maintainer
#66 lore raw

Re: [PATCH 10/13] vfio: Make vfio_device_open() exclusive between group path and device cdev path

Quoted 46 lines · Yi L
Quoted 46 lines

From: Jason Gunthorpe <[email protected]>
Sent: Tuesday, February 7, 2023 9:20 PM

Quoted 4 lines · Yi L
Quoted 4 lines

From: Jason Gunthorpe <[email protected]>
Sent: Tuesday, February 7, 2023 9:13 PM

On Tue, Feb 07, 2023 at 12:35:48AM +0000, Tian, Kevin wrote:

Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines

From: Liu, Yi L <[email protected]>
Sent: Monday, February 6, 2023 11:51 PM

Quoted 5 lines

From: Jason Gunthorpe <[email protected]>
Sent: Monday, February 6, 2023 11:11 PM

Quoted 1 line · Kevin

It's probably simpler if we always mark DMA owner with

vfio_group

Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

for the group path, no matter vfio type1 or iommufd compat is

used.

Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

This should avoid all the tricky corner cases between the two

paths.

Quoted 8 lines
Quoted 8 lines
Quoted 6 lines
Quoted 6 lines
Quoted 2 lines

Yes

Then, we have two choices:

1) extend iommufd_device_bind() to allow a caller-specified DMA

marker

Quoted 1 line
Quoted 1 line

2) claim DMA owner before calling iommufd_device_bind(), still

need to

Quoted 15 lines
Quoted 10 lines
Quoted 1 line
Quoted 1 line

extend iommufd_device_bind() to accept a flag to bypass DMA

owner

Quoted 6 lines
Quoted 4 lines

claim

which one would be better? or do we have a third choice?

first one

Why can't this all be handled in vfio??

Are you preferring the second one? Surely VFIO can claim DMA owner by itself. But it is the vfio iommufd compat mode, so it still needs to call iommufd_device_bind(). And it should bypass DMA owner claim since it's already done.

Quoted 6 lines
Quoted 2 lines

No, I mean why can't vfio just call iommufd exactly once regardless of what mode it is running in?

This seems to be moving the DMA owner claim out of iommufd_device_bind(). Is it? Then either group and cdev can claim DMA owner with their own DMA marker.

No, it has nothing to do with DMA ownership

Just keep a flag in vfio saying it is in group mode or device mode and act accordingly.

The iommufd DMA owner check is *only* to be used for protecting against two unrelated drivers trying to claim the same device.

Jason

author
#67 lore raw

RE: [PATCH 10/13] vfio: Make vfio_device_open() exclusive between group path and device cdev path

Quoted 56 lines

From: Jason Gunthorpe <[email protected]>
Sent: Tuesday, February 7, 2023 9:27 PM

Quoted 11 lines · Yi L
Quoted 11 lines

From: Jason Gunthorpe <[email protected]>
Sent: Tuesday, February 7, 2023 9:20 PM

Quoted 7 lines · Yi L
Quoted 7 lines

From: Jason Gunthorpe <[email protected]>
Sent: Tuesday, February 7, 2023 9:13 PM

Quoted 3 lines · Kevin
Quoted 3 lines

From: Liu, Yi L <[email protected]>
Sent: Monday, February 6, 2023 11:51 PM

Quoted 7 lines
Quoted 7 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines

From: Jason Gunthorpe <[email protected]>
Sent: Monday, February 6, 2023 11:11 PM

Quoted 1 line · Kevin

It's probably simpler if we always mark DMA owner with

vfio_group

Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

for the group path, no matter vfio type1 or iommufd compat

is

Quoted 9 lines
Quoted 9 lines

used.

Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

This should avoid all the tricky corner cases between the two

paths.

Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 2 lines

Yes

Then, we have two choices:

1) extend iommufd_device_bind() to allow a caller-specified

DMA

Quoted 4 lines
Quoted 4 lines
Quoted 2 lines
Quoted 2 lines

marker

Quoted 1 line
Quoted 1 line

2) claim DMA owner before calling iommufd_device_bind(), still

need to

Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

extend iommufd_device_bind() to accept a flag to bypass

DMA

Quoted 18 lines
Quoted 17 lines
Quoted 6 lines
Quoted 6 lines

owner

Quoted 5 lines
Quoted 4 lines

claim

which one would be better? or do we have a third choice?

Quoted 8 lines
Quoted 3 lines
Quoted 1 line

first one

Why can't this all be handled in vfio??

Are you preferring the second one? Surely VFIO can claim DMA owner by itself. But it is the vfio iommufd compat mode, so it still needs to call iommufd_device_bind(). And it should bypass DMA owner claim since it's already done.

No, I mean why can't vfio just call iommufd exactly once regardless of what mode it is running in?

Quoted 7 lines
Quoted 1 line

This seems to be moving the DMA owner claim out of

iommufd_device_bind().

Quoted 1 line

Is it? Then either group and cdev can claim DMA owner with their own

DMA

Quoted 1 line

marker.

No, it has nothing to do with DMA ownership

sorry, I'm a bit lost here. Back to Kevin's suggestion. He suggested below.

"It's probably simpler if we always mark DMA owner with vfio_group for the group path, no matter vfio type1 or iommufd compat is used. This should avoid all the tricky corner cases between the two paths."

This means to enforce the group path uses vfio_group as DMA ownership marker, while cdev path uses iommufd as DMA marker. Then there will be no possibility that the vfio iommufd compat mode group path can share the same DMA ownerhsip marker with cdev path. With this the devices within the same group can only be opened either by group path or cdev path, but no mixture.

Regards,
Yi Liu

maintainer
#68 lore raw

RE: [PATCH 10/13] vfio: Make vfio_device_open() exclusive between group path and device cdev path

In reply to Jason Gunthorpe
Quoted 8 lines

From: Jason Gunthorpe <[email protected]>
Sent: Tuesday, February 7, 2023 9:27 PM

Quoted 5 lines
Quoted 3 lines

No, I mean why can't vfio just call iommufd exactly once regardless of what mode it is running in?

This seems to be moving the DMA owner claim out of

iommufd_device_bind().

Quoted 8 lines
Quoted 1 line

Is it? Then either group and cdev can claim DMA owner with their own

DMA

Quoted 1 line

marker.

No, it has nothing to do with DMA ownership

Just keep a flag in vfio saying it is in group mode or device mode and act accordingly.

It cannot be a simple flag. needs to be a refcnt since multiple devices in the group might be opened via cdev so the device mode should be cleared only when the last device via cdev is closed.

Yi actually did implement such a flavor before, kind of introducing a vfio_group->cdev_opened_cnt field.

Then cdev bind_iommufd checks whether vfio_group->opened_file has been set in the group open path. If not then increment vfio_group->cdev_opened_cnt.

cdev close decrements vfio_group->cdev_opened_cnt.

group open checks whether vfio_group->cdev_opened_cnt has been set. If not go to set vfio_group->opened_file.

In this case only one path can claim DMA ownership.

Is above what you expect?

Quoted 4 lines

The iommufd DMA owner check is *only* to be used for protecting against two unrelated drivers trying to claim the same device.

this is just one implementation choice. I don't see why it cannot be extended to allow one driver to protect against two internal paths. Just simply allow the driver to assign an owner instead of assuming iommufd_ctx.

maintainer
#69 lore raw

Re: [PATCH 10/13] vfio: Make vfio_device_open() exclusive between group path and device cdev path

Quoted 28 lines · Kevin
Quoted 8 lines

From: Jason Gunthorpe <[email protected]>
Sent: Tuesday, February 7, 2023 9:27 PM

Quoted 5 lines
Quoted 3 lines

No, I mean why can't vfio just call iommufd exactly once regardless of what mode it is running in?

This seems to be moving the DMA owner claim out of

iommufd_device_bind().

Quoted 8 lines
Quoted 1 line

Is it? Then either group and cdev can claim DMA owner with their own

DMA

Quoted 1 line

marker.

No, it has nothing to do with DMA ownership

Just keep a flag in vfio saying it is in group mode or device mode and act accordingly.

It cannot be a simple flag. needs to be a refcnt since multiple devices in the group might be opened via cdev so the device mode should be cleared only when the last device via cdev is closed.

Yi actually did implement such a flavor before, kind of introducing a vfio_group->cdev_opened_cnt field.

Then cdev bind_iommufd checks whether vfio_group->opened_file has been set in the group open path. If not then increment vfio_group->cdev_opened_cnt.

Quoted 8 lines

cdev close decrements vfio_group->cdev_opened_cnt.

group open checks whether vfio_group->cdev_opened_cnt has been set. If not go to set vfio_group->opened_file.

In this case only one path can claim DMA ownership.

Is above what you expect?

It seems appropriate

You could also sweep the device list to see how the indivudal devices are open to decice what to do.

Quoted 8 lines
Quoted 3 lines

The iommufd DMA owner check is *only* to be used for protecting against two unrelated drivers trying to claim the same device.

this is just one implementation choice. I don't see why it cannot be extended to allow one driver to protect against two internal paths. Just simply allow the driver to assign an owner instead of assuming iommufd_ctx.

It is really not what it is for, and the owner thing is so ugly I don't like the pattern.

Jason

maintainer
#70 lore raw

RE: [PATCH 10/13] vfio: Make vfio_device_open() exclusive between group path and device cdev path

In reply to Yi Liu

drivers/vfio/vfio.h struct vfio_device; · line 22

Quoted 10 lines

From: Liu, Yi L <[email protected]>
Sent: Tuesday, January 17, 2023 9:50 PM

drivers/vfio/vfio.h +2 -0
@@ -17,7 +17,11 @@ struct vfio_device;
 struct vfio_container;
 
 struct vfio_device_file {
+	/* static fields, init per allocation */
 	struct vfio_device *device;
+	bool single_open;

I wonder whether the readability is better by renaming this to 'cdev', e.g.:

	/*
	 * Device cdev path cannot support multiple device open since
	 * it doesn't have a secure way for it. So a second device
	 * open attempt should be failed if the caller is from a cdev
	 * path or the device has already been opened by a cdev path.
	 */
	if (device->open_count != 0 &&
	    (df->cdev || device->single_open))
		return -EINVAL;

	/*
	 * group path supports multiple device open, while cdev doesn't.
	 * So use vfio_device_group_close() for !singel_open case.
	 */
	if (!df->cdev)
		vfio_device_group_close(df);

because from device file p.o.v we just want to differentiate cdev vs. group interface. With this change we even don't need the comment for the last condition check.

it's fine to have device->single_open as it's kind of a status bit set in the cdev path to prevent more opens on this device.

author
#71 lore raw

RE: [PATCH 10/13] vfio: Make vfio_device_open() exclusive between group path and device cdev path

Quoted 34 lines

From: Tian, Kevin <[email protected]>
Sent: Thursday, January 19, 2023 5:55 PM

drivers/vfio/vfio.h struct vfio_device; · line 22

Quoted 10 lines

From: Liu, Yi L <[email protected]>
Sent: Tuesday, January 17, 2023 9:50 PM

drivers/vfio/vfio.h +2 -0
@@ -17,7 +17,11 @@ struct vfio_device;
 struct vfio_container;
 
 struct vfio_device_file {
+	/* static fields, init per allocation */
 	struct vfio_device *device;
+	bool single_open;

I wonder whether the readability is better by renaming this to 'cdev', e.g.:

	/*
	 * Device cdev path cannot support multiple device open since
	 * it doesn't have a secure way for it. So a second device
	 * open attempt should be failed if the caller is from a cdev
	 * path or the device has already been opened by a cdev path.
	 */
	if (device->open_count != 0 &&
	    (df->cdev || device->single_open))
		return -EINVAL;

	/*
	 * group path supports multiple device open, while cdev doesn't.
	 * So use vfio_device_group_close() for !singel_open case.
	 */
	if (!df->cdev)
		vfio_device_group_close(df);
Quoted 6 lines

because from device file p.o.v we just want to differentiate cdev vs. group interface. With this change we even don't need the comment for the last condition check.

it's fine to have device->single_open as it's kind of a status bit set in the cdev path to prevent more opens on this device.

Ok.

Regards,
Yi Liu

author
#72 lore raw

PATCH 11/13 vfio: Add cdev for vfio_device

In reply to Yi Liu

This allows user to directly open a vfio device w/o using the legacy container/group interface, as a prerequisite for supporting new iommu features like nested translation.

The device fd opened in this manner doesn't have the capability to access the device as the fops open() doesn't open the device until the successful BIND_IOMMUFD which be added in next patch.

With this patch, devices registered to vfio core have both group and device interface created.

- group interface : /dev/vfio/$groupID
- device interface: /dev/vfio/devices/vfioX (X is the minor number and is unique across devices)

Given a vfio device the user can identify the matching vfioX by checking the sysfs path of the device. Take PCI device (0000:6a:01.0) for example, /sys/bus/pci/devices/0000\:6a\:01.0/vfio-dev/vfio0/dev contains the major:minor of the matching vfioX.

Userspace then opens the /dev/vfio/devices/vfioX and checks with fstat that the major:minor matches.

The vfio_device cdev logic in this patch:
*) __vfio_register_dev() path ends up doing cdev_device_add() for each
vfio_device;
*) vfio_unregister_group_dev() path does cdev_device_del();

---
 drivers/vfio/vfio_main.c | 103 ++++++++++++++++++++++++++++++++++++---
 include/linux/vfio.h     |   7 ++-
 2 files changed, 102 insertions(+), 8 deletions(-)
drivers/vfio/vfio_main.c +96 -7
index 78725c28b933..6068ffb7c6b7 100644
@@ -43,6 +43,9 @@
 static struct vfio {
 	struct class			*device_class;
 	struct ida			device_ida;
+#if IS_ENABLED(CONFIG_IOMMUFD)
+	dev_t                           device_devt;
+#endif
 } vfio;
 
 static DEFINE_XARRAY(vfio_device_set_xa);
@@ -156,7 +159,11 @@ static void vfio_device_release(struct device *dev)
 			container_of(dev, struct vfio_device, device);
 
 	vfio_release_device_set(device);
+#if IS_ENABLED(CONFIG_IOMMUFD)
+	ida_free(&vfio.device_ida, MINOR(device->device.devt));
+#else
 	ida_free(&vfio.device_ida, device->index);
+#endif
 
 	if (device->ops->release)
 		device->ops->release(device);
@@ -209,15 +216,16 @@ EXPORT_SYMBOL_GPL(_vfio_alloc_device);
 static int vfio_init_device(struct vfio_device *device, struct device *dev,
 			    const struct vfio_device_ops *ops)
 {
+	unsigned int minor;
 	int ret;
 
 	ret = ida_alloc_max(&vfio.device_ida, MINORMASK, GFP_KERNEL);
 	if (ret < 0) {
-		dev_dbg(dev, "Error to alloc index\n");
+		dev_dbg(dev, "Error to alloc minor\n");
 		return ret;
 	}
 
-	device->index = ret;
+	minor = ret;
 	init_completion(&device->comp);
 	device->dev = dev;
 	device->ops = ops;
@@ -232,17 +240,25 @@ static int vfio_init_device(struct vfio_device *device, struct device *dev,
 	device->device.release = vfio_device_release;
 	device->device.class = vfio.device_class;
 	device->device.parent = device->dev;
+#if IS_ENABLED(CONFIG_IOMMUFD)
+	device->device.devt = MKDEV(MAJOR(vfio.device_devt), minor);
+	cdev_init(&device->cdev, &vfio_device_fops);
+	device->cdev.owner = THIS_MODULE;
+#else
+	device->index = minor;
+#endif
 	return 0;
 
 out_uninit:
 	vfio_release_device_set(device);
-	ida_free(&vfio.device_ida, device->index);
+	ida_free(&vfio.device_ida, minor);
 	return ret;
 }
 
 static int __vfio_register_dev(struct vfio_device *device,
 			       enum vfio_group_type type)
 {
+	unsigned int minor;
 	int ret;
 
 	if (WARN_ON(device->ops->bind_iommufd &&
@@ -257,7 +273,12 @@ static int __vfio_register_dev(struct vfio_device *device,
 	if (!device->dev_set)
 		vfio_assign_device_set(device, device);
 
-	ret = dev_set_name(&device->device, "vfio%d", device->index);
+#if IS_ENABLED(CONFIG_IOMMUFD)
+	minor = MINOR(device->device.devt);
+#else
+	minor = device->index;
+#endif
+	ret = dev_set_name(&device->device, "vfio%d", minor);
 	if (ret)
 		return ret;
 
@@ -265,7 +286,11 @@ static int __vfio_register_dev(struct vfio_device *device,
 	if (ret)
 		return ret;
 
+#if IS_ENABLED(CONFIG_IOMMUFD)
+	ret = cdev_device_add(&device->cdev, &device->device);
+#else
 	ret = device_add(&device->device);
+#endif
 	if (ret)
 		goto err_out;
 
@@ -305,6 +330,17 @@ void vfio_unregister_group_dev(struct vfio_device *device)
 	bool interrupted = false;
 	long rc;
 
+#if IS_ENABLED(CONFIG_IOMMUFD)
+	/*
+	 * Balances device_add in register path. Putting it as the first
+	 * operation in unregister to prevent registration refcount from
+	 * incrementing per cdev open.
+	 */
+	cdev_device_del(&device->cdev, &device->device);
+#else
+	device_del(&device->device);
+#endif
+
 	vfio_device_put_registration(device);
 	rc = try_wait_for_completion(&device->comp);
 	while (rc <= 0) {
@@ -330,9 +366,6 @@ void vfio_unregister_group_dev(struct vfio_device *device)
 
 	vfio_device_group_unregister(device);
 
-	/* Balances device_add in register path */
-	device_del(&device->device);
-
 	/* Balances vfio_device_set_group in register path */
 	vfio_device_remove_group(device);
 }
@@ -502,6 +535,37 @@ static inline void vfio_device_pm_runtime_put(struct vfio_device *device)
 /*
  * VFIO Device fd
  */
+#if IS_ENABLED(CONFIG_IOMMUFD)
+static int vfio_device_fops_open(struct inode *inode, struct file *filep)
+{
+	struct vfio_device *device = container_of(inode->i_cdev,
+						  struct vfio_device, cdev);
+	struct vfio_device_file *df;
+	int ret;
+
+	if (!vfio_device_try_get_registration(device))
+		return -ENODEV;
+
+	/*
+	 * device access is blocked until .open_device() is called
+	 * in BIND_IOMMUFD.
+	 */
+	df = vfio_allocate_device_file(device, true);
+	if (IS_ERR(df)) {
+		ret = PTR_ERR(df);
+		goto err_put_registration;
+	}
+
+	filep->private_data = df;
+
+	return 0;
+
+err_put_registration:
+	vfio_device_put_registration(device);
+	return ret;
+}
+#endif
+
 static int vfio_device_fops_release(struct inode *inode, struct file *filep)
 {
 	struct vfio_device_file *df = filep->private_data;
@@ -1169,6 +1233,9 @@ static int vfio_device_fops_mmap(struct file *filep, struct vm_area_struct *vma)
 
 const struct file_operations vfio_device_fops = {
 	.owner		= THIS_MODULE,
+#if IS_ENABLED(CONFIG_IOMMUFD)
+	.open		= vfio_device_fops_open,
+#endif
 	.release	= vfio_device_fops_release,
 	.read		= vfio_device_fops_read,
 	.write		= vfio_device_fops_write,
@@ -1522,6 +1589,13 @@ EXPORT_SYMBOL(vfio_dma_rw);
 /*
  * Module/class support
  */
+#if IS_ENABLED(CONFIG_IOMMUFD)
+static char *vfio_device_devnode(const struct device *dev, umode_t *mode)
+{
+	return kasprintf(GFP_KERNEL, "vfio/devices/%s", dev_name(dev));
+}
+#endif
+
 static int __init vfio_init(void)
 {
 	int ret;
@@ -1543,9 +1617,21 @@ static int __init vfio_init(void)
 		goto err_dev_class;
 	}
 
+#if IS_ENABLED(CONFIG_IOMMUFD)
+	vfio.device_class->devnode = vfio_device_devnode;
+	ret = alloc_chrdev_region(&vfio.device_devt, 0,
+				  MINORMASK + 1, "vfio-dev");
+	if (ret)
+		goto err_alloc_dev_chrdev;
+#endif
 	pr_info(DRIVER_DESC " version: " DRIVER_VERSION "\n");
 	return 0;
 
+#if IS_ENABLED(CONFIG_IOMMUFD)
+err_alloc_dev_chrdev:
+	class_destroy(vfio.device_class);
+	vfio.device_class = NULL;
+#endif
 err_dev_class:
 	vfio_virqfd_exit();
 err_virqfd:
@@ -1556,6 +1642,9 @@ static int __init vfio_init(void)
 static void __exit vfio_cleanup(void)
 {
 	ida_destroy(&vfio.device_ida);
+#if IS_ENABLED(CONFIG_IOMMUFD)
+	unregister_chrdev_region(vfio.device_devt, MINORMASK + 1);
+#endif
 	class_destroy(vfio.device_class);
 	vfio.device_class = NULL;
 	vfio_virqfd_exit();
include/linux/vfio.h +6 -1
index 300318f0d448..4a31842ebe0b 100644
@@ -13,6 +13,7 @@
 #include <linux/mm.h>
 #include <linux/workqueue.h>
 #include <linux/poll.h>
+#include <linux/cdev.h>
 #include <uapi/linux/vfio.h>
 #include <linux/iova_bitmap.h>
 
@@ -50,8 +51,12 @@ struct vfio_device {
 	struct kvm *kvm;
 
 	/* Members below here are private, not for driver use */
-	unsigned int index;
 	struct device device;	/* device.kref covers object life circle */
+#if IS_ENABLED(CONFIG_IOMMUFD)
+	struct cdev cdev;
+#else
+	unsigned int index;
+#endif
 	refcount_t refcount;	/* user count on registered device*/
 	unsigned int open_count;
 	struct completion comp;
Signature · 1 line
2.34.1
maintainer
#73 lore raw

RE: [PATCH 11/13] vfio: Add cdev for vfio_device

drivers/vfio/vfio_main.c static void vfio_device_release(struct device *dev) · line 166

Quoted 12 lines

From: Liu, Yi L <[email protected]>
Sent: Tuesday, January 17, 2023 9:50 PM

drivers/vfio/vfio_main.c +4 -0
@@ -156,7 +159,11 @@ static void vfio_device_release(struct device *dev)
 			container_of(dev, struct vfio_device, device);
 
 	vfio_release_device_set(device);
+#if IS_ENABLED(CONFIG_IOMMUFD)
+	ida_free(&vfio.device_ida, MINOR(device->device.devt));
+#else
 	ida_free(&vfio.device_ida, device->index);
+#endif

There are many #if in this patch, leading to bad readability.

for this what about letting device->index always storing the minor value? then here it could just be:

	ida_free(&vfio.device_ida, device->index);

drivers/vfio/vfio_main.c static int vfio_init_device(struct vfio_device · line 250

Quoted 12 lines
drivers/vfio/vfio_main.c +7 -0
@@ -232,17 +240,25 @@ static int vfio_init_device(struct vfio_device
 *device, struct device *dev,
 	device->device.release = vfio_device_release;
 	device->device.class = vfio.device_class;
 	device->device.parent = device->dev;
+#if IS_ENABLED(CONFIG_IOMMUFD)
+	device->device.devt = MKDEV(MAJOR(vfio.device_devt), minor);
+	cdev_init(&device->cdev, &vfio_device_fops);
+	device->cdev.owner = THIS_MODULE;
+#else
+	device->index = minor;
+#endif

Probably we can have a vfio_init_device_cdev() in iommufd.c and let it be empty if !CONFIG_IOMMUFD. Then here could be:

	device->index = minor;
	vfio_init_device_cdev(device, vfio.device_devt, minor);

drivers/vfio/vfio_main.c static int __vfio_register_dev(struct vfio_device · line 281

Quoted 11 lines
drivers/vfio/vfio_main.c +5 -1
@@ -257,7 +273,12 @@ static int __vfio_register_dev(struct vfio_device
 *device,
 	if (!device->dev_set)
 		vfio_assign_device_set(device, device);
 
-	ret = dev_set_name(&device->device, "vfio%d", device->index);
+#if IS_ENABLED(CONFIG_IOMMUFD)
+	minor = MINOR(device->device.devt);
+#else
+	minor = device->index;
+#endif

then just "minor = device->index"

drivers/vfio/vfio_main.c static int __vfio_register_dev(struct vfio_device *device, · line 293

Quoted 6 lines
drivers/vfio/vfio_main.c +4 -0
+#if IS_ENABLED(CONFIG_IOMMUFD)
+	ret = cdev_device_add(&device->cdev, &device->device);
+#else
 	ret = device_add(&device->device);
+#endif

also add a wrapper vfio_register_device_cdev() which does cdev_device_add() if CONFIG_IOMMUFD and device_add() otherwise.

drivers/vfio/vfio_main.c void vfio_unregister_group_dev(struct vfio_device *device) · line 342

Quoted 10 lines
drivers/vfio/vfio_main.c +10 -0
+#if IS_ENABLED(CONFIG_IOMMUFD)
+	/*
+	 * Balances device_add in register path. Putting it as the first
+	 * operation in unregister to prevent registration refcount from
+	 * incrementing per cdev open.
+	 */
+	cdev_device_del(&device->cdev, &device->device);
+#else
+	device_del(&device->device);
+#endif

ditto

Quoted 20 lines
drivers/vfio/vfio_main.c +20 -0
+#if IS_ENABLED(CONFIG_IOMMUFD)
+static int vfio_device_fops_open(struct inode *inode, struct file *filep)
+{
+	struct vfio_device *device = container_of(inode->i_cdev,
+						  struct vfio_device, cdev);
+	struct vfio_device_file *df;
+	int ret;
+
+	if (!vfio_device_try_get_registration(device))
+		return -ENODEV;
+
+	/*
+	 * device access is blocked until .open_device() is called
+	 * in BIND_IOMMUFD.
+	 */
+	df = vfio_allocate_device_file(device, true);
+	if (IS_ERR(df)) {
+		ret = PTR_ERR(df);
+		goto err_put_registration;
+	}

drivers/vfio/vfio_main.c static inline void vfio_device_pm_runtime_put(struct vfio_device *device) · line 567

Quoted 10 lines
drivers/vfio/vfio_main.c +10 -0
+
+	filep->private_data = df;
+
+	return 0;
+
+err_put_registration:
+	vfio_device_put_registration(device);
+	return ret;
+}
+#endif

move to iommufd.c

drivers/vfio/vfio_main.c EXPORT_SYMBOL(vfio_dma_rw); · line 1597

Quoted 6 lines
drivers/vfio/vfio_main.c +6 -0
+#if IS_ENABLED(CONFIG_IOMMUFD)
+static char *vfio_device_devnode(const struct device *dev, umode_t *mode)
+{
+	return kasprintf(GFP_KERNEL, "vfio/devices/%s", dev_name(dev));
+}
+#endif

ditto

drivers/vfio/vfio_main.c static int __init vfio_init(void) · line 1626

Quoted 11 lines
drivers/vfio/vfio_main.c +7 -0
@@ -1543,9 +1617,21 @@ static int __init vfio_init(void)
 		goto err_dev_class;
 	}
 
+#if IS_ENABLED(CONFIG_IOMMUFD)
+	vfio.device_class->devnode = vfio_device_devnode;
+	ret = alloc_chrdev_region(&vfio.device_devt, 0,
+				  MINORMASK + 1, "vfio-dev");
+	if (ret)
+		goto err_alloc_dev_chrdev;
+#endif

vfio_cdev_init()

drivers/vfio/vfio_main.c static int __init vfio_init(void) · line 1647

Quoted 6 lines
drivers/vfio/vfio_main.c +3 -0
 static void __exit vfio_cleanup(void)
 {
 	ida_destroy(&vfio.device_ida);
+#if IS_ENABLED(CONFIG_IOMMUFD)
+	unregister_chrdev_region(vfio.device_devt, MINORMASK + 1);
+#endif

vfio_cdev_cleanup()

author
#74 lore raw

RE: [PATCH 11/13] vfio: Add cdev for vfio_device

Quoted 18 lines

From: Tian, Kevin <[email protected]>
Sent: Friday, January 20, 2023 3:27 PM

drivers/vfio/vfio_main.c static void vfio_device_release(struct device *dev) · line 166

Quoted 12 lines

From: Liu, Yi L <[email protected]>
Sent: Tuesday, January 17, 2023 9:50 PM

drivers/vfio/vfio_main.c +4 -0
@@ -156,7 +159,11 @@ static void vfio_device_release(struct device *dev)
 			container_of(dev, struct vfio_device, device);
 
 	vfio_release_device_set(device);
+#if IS_ENABLED(CONFIG_IOMMUFD)
+	ida_free(&vfio.device_ida, MINOR(device->device.devt));
+#else
 	ida_free(&vfio.device_ida, device->index);
+#endif

There are many #if in this patch, leading to bad readability.

Quoted 4 lines

for this what about letting device->index always storing the minor value? then here it could just be:

	ida_free(&vfio.device_ida, device->index);

Yes.

Quoted 7 lines
Quoted 7 lines
drivers/vfio/vfio_main.c +2 -0
@@ -232,17 +240,25 @@ static int vfio_init_device(struct vfio_device
 *device, struct device *dev,
 	device->device.release = vfio_device_release;
 	device->device.class = vfio.device_class;
 	device->device.parent = device->dev;
+#if IS_ENABLED(CONFIG_IOMMUFD)
+	device->device.devt = MKDEV(MAJOR(vfio.device_devt), minor);
Quoted 8 lines

drivers/vfio/vfio_main.c static int vfio_init_device(struct vfio_device *device, struct device *dev, · line 249

Quoted 5 lines
drivers/vfio/vfio_main.c +5 -0
+	cdev_init(&device->cdev, &vfio_device_fops);
+	device->cdev.owner = THIS_MODULE;
+#else
+	device->index = minor;
+#endif

Probably we can have a vfio_init_device_cdev() in iommufd.c and let it be empty if !CONFIG_IOMMUFD. Then here could be:

Yes. Btw. Will adding another device_cdev.c better than reusing iommufd.c?

Quoted 9 lines
	device->index = minor;
	vfio_init_device_cdev(device, vfio.device_devt, minor);
Quoted 5 lines
drivers/vfio/vfio_main.c +0 -0
@@ -257,7 +273,12 @@ static int __vfio_register_dev(struct vfio_device
 *device,
 	if (!device->dev_set)
 		vfio_assign_device_set(device, device);
 
Quoted 8 lines

drivers/vfio/vfio_main.c static int __vfio_register_dev(struct vfio_device *device, · line 280

Quoted 6 lines
drivers/vfio/vfio_main.c +5 -1
-	ret = dev_set_name(&device->device, "vfio%d", device->index);
+#if IS_ENABLED(CONFIG_IOMMUFD)
+	minor = MINOR(device->device.devt);
+#else
+	minor = device->index;
+#endif

then just "minor = device->index"

Yes.

Quoted 9 lines

drivers/vfio/vfio_main.c static int __vfio_register_dev(struct vfio_device *device, · line 293

Quoted 6 lines
drivers/vfio/vfio_main.c +4 -0
+#if IS_ENABLED(CONFIG_IOMMUFD)
+	ret = cdev_device_add(&device->cdev, &device->device);
+#else
 	ret = device_add(&device->device);
+#endif

also add a wrapper vfio_register_device_cdev() which does cdev_device_add() if CONFIG_IOMMUFD and device_add() otherwise.

Got it.

Quoted 71 lines

drivers/vfio/vfio_main.c void vfio_unregister_group_dev(struct vfio_device *device) · line 342

Quoted 11 lines
drivers/vfio/vfio_main.c +10 -0
+#if IS_ENABLED(CONFIG_IOMMUFD)
+	/*
+	 * Balances device_add in register path. Putting it as the first
+	 * operation in unregister to prevent registration refcount from
+	 * incrementing per cdev open.
+	 */
+	cdev_device_del(&device->cdev, &device->device);
+#else
+	device_del(&device->device);
+#endif

ditto

Quoted 20 lines
drivers/vfio/vfio_main.c +20 -0
+#if IS_ENABLED(CONFIG_IOMMUFD)
+static int vfio_device_fops_open(struct inode *inode, struct file *filep)
+{
+	struct vfio_device *device = container_of(inode->i_cdev,
+						  struct vfio_device, cdev);
+	struct vfio_device_file *df;
+	int ret;
+
+	if (!vfio_device_try_get_registration(device))
+		return -ENODEV;
+
+	/*
+	 * device access is blocked until .open_device() is called
+	 * in BIND_IOMMUFD.
+	 */
+	df = vfio_allocate_device_file(device, true);
+	if (IS_ERR(df)) {
+		ret = PTR_ERR(df);
+		goto err_put_registration;
+	}

drivers/vfio/vfio_main.c static inline void vfio_device_pm_runtime_put(struct vfio_device *device) · line 567

Quoted 10 lines
drivers/vfio/vfio_main.c +10 -0
+
+	filep->private_data = df;
+
+	return 0;
+
+err_put_registration:
+	vfio_device_put_registration(device);
+	return ret;
+}
+#endif

move to iommufd.c

Review

Quoted 2 lines
(quoted diff) +2 -0
+#if IS_ENABLED(CONFIG_IOMMUFD)
+static char *vfio_device_devnode(const struct device *dev, umode_t

*mode)

drivers/vfio/vfio_main.c EXPORT_SYMBOL(vfio_dma_rw); · line 1597

Quoted 4 lines
drivers/vfio/vfio_main.c +4 -0
+{
+	return kasprintf(GFP_KERNEL, "vfio/devices/%s", dev_name(dev));
+}
+#endif

ditto

drivers/vfio/vfio_main.c static int __init vfio_init(void) · line 1626

Quoted 11 lines
drivers/vfio/vfio_main.c +7 -0
@@ -1543,9 +1617,21 @@ static int __init vfio_init(void)
 		goto err_dev_class;
 	}
 
+#if IS_ENABLED(CONFIG_IOMMUFD)
+	vfio.device_class->devnode = vfio_device_devnode;
+	ret = alloc_chrdev_region(&vfio.device_devt, 0,
+				  MINORMASK + 1, "vfio-dev");
+	if (ret)
+		goto err_alloc_dev_chrdev;
+#endif

vfio_cdev_init()

Quoted 8 lines

drivers/vfio/vfio_main.c static int __init vfio_init(void) · line 1647

Quoted 6 lines
drivers/vfio/vfio_main.c +3 -0
 static void __exit vfio_cleanup(void)
 {
 	ida_destroy(&vfio.device_ida);
+#if IS_ENABLED(CONFIG_IOMMUFD)
+	unregister_chrdev_region(vfio.device_devt, MINORMASK + 1);
+#endif

vfio_cdev_cleanup()

All above comments got.

Regards,
Yi Liu

maintainer
#75 lore raw

Re: [PATCH 11/13] vfio: Add cdev for vfio_device

In reply to Yi Liu
Quoted 40 lines · Yi Liu

This allows user to directly open a vfio device w/o using the legacy container/group interface, as a prerequisite for supporting new iommu features like nested translation.

The device fd opened in this manner doesn't have the capability to access the device as the fops open() doesn't open the device until the successful BIND_IOMMUFD which be added in next patch.

With this patch, devices registered to vfio core have both group and device interface created.

- group interface : /dev/vfio/$groupID
- device interface: /dev/vfio/devices/vfioX (X is the minor number and is unique across devices)

Given a vfio device the user can identify the matching vfioX by checking the sysfs path of the device. Take PCI device (0000:6a:01.0) for example, /sys/bus/pci/devices/0000\:6a\:01.0/vfio-dev/vfio0/dev contains the major:minor of the matching vfioX.

Userspace then opens the /dev/vfio/devices/vfioX and checks with fstat that the major:minor matches.

The vfio_device cdev logic in this patch:
*) __vfio_register_dev() path ends up doing cdev_device_add() for each
vfio_device;
*) vfio_unregister_group_dev() path does cdev_device_del();

---
 drivers/vfio/vfio_main.c | 103 ++++++++++++++++++++++++++++++++++++---
 include/linux/vfio.h     |   7 ++-
 2 files changed, 102 insertions(+), 8 deletions(-)
drivers/vfio/vfio_main.c +0 -0
index 78725c28b933..6068ffb7c6b7 100644

drivers/vfio/vfio_main.c line 48

Quoted 6 lines
drivers/vfio/vfio_main.c +3 -0
@@ -43,6 +43,9 @@
 static struct vfio {
 	struct class			*device_class;
 	struct ida			device_ida;
+#if IS_ENABLED(CONFIG_IOMMUFD)
+	dev_t                           device_devt;
+#endif

It is a bit strange to see this called CONFIG_IOMMUFD, maybe we should have a CONFIG_VFIO_DEVICE_FILE that depends on IOMMUFD?

Please try to use a plain 'if (IS_ENABLED())' for more of these

It probably isn't worth saving a few bytes in memory to complicate all the code, so maybe just always include things like this.

drivers/vfio/vfio_main.c static void vfio_device_release(struct device *dev) · line 166

Quoted 9 lines
drivers/vfio/vfio_main.c +4 -0
@@ -156,7 +159,11 @@ static void vfio_device_release(struct device *dev)
 			container_of(dev, struct vfio_device, device);
 
 	vfio_release_device_set(device);
+#if IS_ENABLED(CONFIG_IOMMUFD)
+	ida_free(&vfio.device_ida, MINOR(device->device.devt));
+#else
 	ida_free(&vfio.device_ida, device->index);
+#endif

A vfio_device_get_index() would help this

Jason

author
#76 lore raw

PATCH 12/13 vfio: Add ioctls for device cdev iommufd

In reply to Yi Liu

This adds two vfio device ioctls for userspace using iommufd on vfio devices.

VFIO_DEVICE_BIND_IOMMUFD: bind device to an iommufd, hence gain DMA
control provided by the iommufd. VFIO no iommu is indicated by passing a minus fd value.
VFIO_DEVICE_ATTACH_IOMMUFD_PT: attach device to ioas, page tables
managed by iommufd. Attach can be undo by passing IOMMUFD_INVALID_ID to kernel.

The ioctls introduced here are just on par with existing VFIO.

---
 drivers/vfio/vfio.h          |   1 +
 drivers/vfio/vfio_main.c     | 175 ++++++++++++++++++++++++++++++++++-
 include/uapi/linux/iommufd.h |   2 +
 include/uapi/linux/vfio.h    |  64 +++++++++++++
 4 files changed, 237 insertions(+), 5 deletions(-)
drivers/vfio/vfio.h +1 -0
index bdcf9762521d..444be924c915 100644
@@ -25,6 +25,7 @@ struct vfio_device_file {
 	struct kvm *kvm;
 	struct iommufd_ctx *iommufd;
 	bool access_granted;
+	bool noiommu;
 };
 
 void vfio_device_put_registration(struct vfio_device *device);
drivers/vfio/vfio_main.c +170 -5

243 lines not on this page · raw message

include/uapi/linux/iommufd.h +2 -0
index 98ebba80cfa1..87680274c01b 100644
@@ -9,6 +9,8 @@
 
 #define IOMMUFD_TYPE (';')
 
+#define IOMMUFD_INVALID_ID 0  /* valid ID starts from 1 */
+
 /**
  * DOC: General ioctl format
  *
include/uapi/linux/vfio.h +64 -0

70 lines not on this page · raw message

Signature · 1 line
2.34.1
maintainer
#77 lore raw

RE: [PATCH 12/13] vfio: Add ioctls for device cdev iommufd

Quoted 11 lines

From: Liu, Yi L <[email protected]>
Sent: Tuesday, January 17, 2023 9:50 PM

This adds two vfio device ioctls for userspace using iommufd on vfio devices.

VFIO_DEVICE_BIND_IOMMUFD: bind device to an iommufd, hence gain
DMA
control provided by the iommufd. VFIO no iommu is indicated by passing a minus fd value.

Can't this be a flag bit for better readability than using a special value?

Quoted 4 lines

VFIO_DEVICE_ATTACH_IOMMUFD_PT: attach device to ioas, page tables
managed by iommufd. Attach can be undo by passing IOMMUFD_INVALID_ID to kernel.

With Alex' remark we need a separate DETACH cmd now.

Quoted 21 lines
drivers/vfio/vfio_main.c +10 -0
+	/*
+	 * For group path, iommufd pointer is NULL when comes into this
+	 * helper. Its noiommu support is in container.c.
+	 *
+	 * For iommufd compat mode, iommufd pointer here is a valid value.
+	 * Its noiommu support is supposed to be in vfio_iommufd_bind().
+	 *
+	 * For device cdev path, iommufd pointer here is a valid value for
+	 * normal cases, but it is NULL if it's noiommu. The reason is
+	 * that userspace uses iommufd==-1 to indicate noiommu mode in

this

drivers/vfio/vfio_main.c +4 -0
+	 * path. So caller of this helper will pass in a NULL iommufd
+	 * pointer. To differentiate it from the group path which also
+	 * passes NULL iommufd pointer in, df->noiommu is used. For cdev
+	 * noiommu, df->noiommu would be set to mark noiommu case for

cdev

drivers/vfio/vfio_main.c +4 -0
+	 * path.
+	 *
+	 * So if df->noiommu is set then this helper just goes ahead to
+	 * open device. If not, it depends on if iommufd pointer is NULL

drivers/vfio/vfio_main.c static int vfio_device_first_open(struct vfio_device_file *df, · line 439

Quoted 10 lines
drivers/vfio/vfio_main.c +4 -1
+	 * to handle the group path, iommufd compat mode, normal cases in
+	 * the cdev path.
+	 */
 	if (iommufd)
 		ret = vfio_iommufd_bind(device, iommufd, dev_id, pt_id);
-	else
+	else if (!df->noiommu)
 		ret = vfio_device_group_use_iommu(device);
 	if (ret)
 		goto err_module_put;

Isn't 'ret' uninitialized when df->noiommu is true?

Quoted 6 lines
drivers/vfio/vfio_main.c +6 -0
+static int vfio_ioctl_device_attach(struct vfio_device *device,
+				    struct vfio_device_feature __user *arg)
+{
+	struct vfio_device_attach_iommufd_pt attach;
+	int ret;
+	bool is_attach;

drivers/vfio/vfio_main.c static int vfio_ioctl_device_feature(struct vfio_device *device, · line 1279

Quoted 10 lines
drivers/vfio/vfio_main.c +10 -0
+
+	if (copy_from_user(&attach, (void __user *)arg, sizeof(attach)))
+		return -EFAULT;
+
+	if (attach.flags)
+		return -EINVAL;
+
+	if (!device->ops->bind_iommufd)
+		return -ENODEV;
+

this should fail if noiommu is true.

author
#78 lore raw

RE: [PATCH 12/13] vfio: Add ioctls for device cdev iommufd

Quoted 17 lines

From: Tian, Kevin <[email protected]>
Sent: Friday, January 20, 2023 4:03 PM

Quoted 11 lines

From: Liu, Yi L <[email protected]>
Sent: Tuesday, January 17, 2023 9:50 PM

This adds two vfio device ioctls for userspace using iommufd on vfio devices.

VFIO_DEVICE_BIND_IOMMUFD: bind device to an iommufd, hence gain
DMA
control provided by the iommufd. VFIO no iommu is indicated by passing a minus fd value.

Can't this be a flag bit for better readability than using a special value?

Quoted 7 lines
Quoted 1 line

VFIO_DEVICE_ATTACH_IOMMUFD_PT: attach device to ioas, page

tables

Quoted 3 lines

managed by iommufd. Attach can be undo by passing IOMMUFD_INVALID_ID to kernel.

With Alex' remark we need a separate DETACH cmd now.

Yes.

Quoted 23 lines
Quoted 17 lines
drivers/vfio/vfio_main.c +10 -0
+	/*
+	 * For group path, iommufd pointer is NULL when comes into this
+	 * helper. Its noiommu support is in container.c.
+	 *
+	 * For iommufd compat mode, iommufd pointer here is a valid value.
+	 * Its noiommu support is supposed to be in vfio_iommufd_bind().
+	 *
+	 * For device cdev path, iommufd pointer here is a valid value for
+	 * normal cases, but it is NULL if it's noiommu. The reason is
+	 * that userspace uses iommufd==-1 to indicate noiommu mode in

this

drivers/vfio/vfio_main.c +4 -0
+	 * path. So caller of this helper will pass in a NULL iommufd
+	 * pointer. To differentiate it from the group path which also
+	 * passes NULL iommufd pointer in, df->noiommu is used. For cdev
+	 * noiommu, df->noiommu would be set to mark noiommu case for

cdev

drivers/vfio/vfio_main.c static int vfio_device_first_open(struct vfio_device_file *df,

Quoted 5 lines
drivers/vfio/vfio_main.c +5 -0
+	 * path.
+	 *
+	 * So if df->noiommu is set then this helper just goes ahead to
+	 * open device. If not, it depends on if iommufd pointer is NULL
+	 * to handle the group path, iommufd compat mode, normal cases

in

Quoted 11 lines

drivers/vfio/vfio_main.c static int vfio_device_first_open(struct vfio_device_file *df, · line 439

Quoted 9 lines
drivers/vfio/vfio_main.c +3 -1
+	 * the cdev path.
+	 */
 	if (iommufd)
 		ret = vfio_iommufd_bind(device, iommufd, dev_id, pt_id);
-	else
+	else if (!df->noiommu)
 		ret = vfio_device_group_use_iommu(device);
 	if (ret)
 		goto err_module_put;

Isn't 'ret' uninitialized when df->noiommu is true?

Done.

Quoted 10 lines
Quoted 10 lines
drivers/vfio/vfio_main.c +10 -0
+static int vfio_ioctl_device_attach(struct vfio_device *device,
+				    struct vfio_device_feature __user *arg)
+{
+	struct vfio_device_attach_iommufd_pt attach;
+	int ret;
+	bool is_attach;
+
+	if (copy_from_user(&attach, (void __user *)arg, sizeof(attach)))
+		return -EFAULT;
+
Quoted 8 lines

drivers/vfio/vfio_main.c static int vfio_ioctl_device_feature(struct vfio_device *device, · line 1279

Quoted 6 lines
drivers/vfio/vfio_main.c +6 -0
+	if (attach.flags)
+		return -EINVAL;
+
+	if (!device->ops->bind_iommufd)
+		return -ENODEV;
+

this should fail if noiommu is true.

Yes.

Regards,
Yi Liu

author
#79 lore raw

PATCH 13/13 vfio: Compile group optionally

In reply to Yi Liu

group code is not needed for vfio device cdev, so with vfio device cdev introduced, the group infrastructures can be compiled out.

---
 drivers/vfio/Kconfig  | 17 +++++++++++
 drivers/vfio/Makefile |  3 +-
 drivers/vfio/vfio.h   | 69 +++++++++++++++++++++++++++++++++++++++++++
 include/linux/vfio.h  | 11 +++++++
 4 files changed, 99 insertions(+), 1 deletion(-)
drivers/vfio/Kconfig +17 -0
index a8f544629467..7e3f6249fa15 100644
@@ -12,9 +12,26 @@ menuconfig VFIO
 	  If you don't know what to do here, say N.
 
 if VFIO
+config VFIO_ENABLE_GROUP
+	bool
+	default !IOMMUFD
+
+config VFIO_GROUP
+	bool "Support for the VFIO group /dev/vfio/$group_id"
+	select VFIO_ENABLE_GROUP
+	default y
+	help
+	   VFIO group is legacy interface for userspace. For userspaces
+	   adapted to iommufd and vfio device cdev, this can be N. For
+	   now, before iommufd is ready and userspace applications fully
+	   converted to iommufd and vfio device cdev, this should be Y.
+
+	   If you don't know what to do here, say Y.
+
 config VFIO_CONTAINER
 	bool "Support for the VFIO container /dev/vfio/vfio"
 	select VFIO_IOMMU_TYPE1 if MMU && (X86 || S390 || ARM || ARM64)
+	depends on VFIO_ENABLE_GROUP
 	default y
 	help
 	  The VFIO container is the classic interface to VFIO for establishing
drivers/vfio/Makefile +2 -1
index 70e7dcb302ef..bb3fec9ea6bf 100644
@@ -2,8 +2,9 @@
 obj-$(CONFIG_VFIO) += vfio.o
 
 vfio-y += vfio_main.o \
-	  group.o \
 	  iova_bitmap.o
+
+vfio-$(CONFIG_VFIO_ENABLE_GROUP) += group.o
 vfio-$(CONFIG_IOMMUFD) += iommufd.o
 vfio-$(CONFIG_VFIO_CONTAINER) += container.o
 vfio-$(CONFIG_VFIO_VIRQFD) += virqfd.o
drivers/vfio/vfio.h +69 -0

81 lines not on this page · raw message

include/linux/vfio.h +11 -0
index 4a31842ebe0b..eb4dc3dfab03 100644
@@ -43,7 +43,9 @@ struct vfio_device {
 	 */
 	const struct vfio_migration_ops *mig_ops;
 	const struct vfio_log_ops *log_ops;
+#if IS_ENABLED(CONFIG_VFIO_ENABLE_GROUP)
 	struct vfio_group *group;
+#endif
 	struct vfio_device_set *dev_set;
 	struct list_head dev_set_list;
 	unsigned int migration_flags;
@@ -60,8 +62,10 @@ struct vfio_device {
 	refcount_t refcount;	/* user count on registered device*/
 	unsigned int open_count;
 	struct completion comp;
+#if IS_ENABLED(CONFIG_VFIO_ENABLE_GROUP)
 	struct list_head group_next;
 	struct list_head iommu_entry;
+#endif
 	struct iommufd_access *iommufd_access;
 #if IS_ENABLED(CONFIG_IOMMUFD)
 	struct iommufd_device *iommufd_device;
@@ -246,7 +250,14 @@ int vfio_mig_get_next_state(struct vfio_device *device,
 /*
  * External user API
  */
+#if IS_ENABLED(CONFIG_VFIO_ENABLE_GROUP)
 struct iommu_group *vfio_file_iommu_group(struct file *file);
+#else
+static inline struct iommu_group *vfio_file_iommu_group(struct file *file)
+{
+	return NULL;
+}
+#endif
 bool vfio_file_is_valid(struct file *file);
 bool vfio_file_enforced_coherent(struct file *file);
 void vfio_file_set_kvm(struct file *file, struct kvm *kvm);
Signature · 1 line
2.34.1