Skip to contentSkip to conversation

Move group specific code into group.c

PATCH In review Yi Liu 6 participants 28 messages Dec 1, 2022 – Dec 7, 2022

Subsystems (2)
lore
Share & tools
lore

Yi Liu

author
#0 lore raw

cover Move group specific code into group.c

With the introduction of iommufd[1], VFIO is towarding to provide device centric uAPI after adapting to iommufd. With this trend, existing VFIO group infrastructure is optional once VFIO converted to device centric.

This series moves the group specific code out of vfio_main.c, prepares for compiling group infrastructure out after adding vfio device cdev[2]

Complete code in below branch:

https://github.com/yiliu1765/iommufd/commits/vfio_group_split_v1

This is based on Jason's "Connect VFIO to IOMMUFD"[3] and my "Make mdev driver dma_unmap callback tolerant to unmaps come before device open"[4]

[1] https://lore.kernel.org/all/[email protected]/
[2] https://github.com/yiliu1765/iommufd/tree/wip/vfio_device_cdev
[3] https://lore.kernel.org/kvm/[email protected]/
[4] https://lore.kernel.org/kvm/[email protected]/

v1:
- Keep the iommufd code in vfio_main.c just move group code out (Jason)
- Add r-b from Kevin
- Reorder the patch sequence
- Drop "vfio: Make vfio_device_open() group agnostic"

rfcv2: https://lore.kernel.org/kvm/[email protected]/
- Remove device->group reference in vfio_main.c suggested by Jason.
- Cherry-pick the patches in Alex's vfio/next branch, and rebased this series on the top.

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

Regards,
Yi Liu

Jason Gunthorpe (2):
vfio: Simplify vfio_create_group()
vfio: Move the sanity check of the group to vfio_create_group()

Yi Liu (8):
vfio: Create wrappers for group register/unregister
vfio: Set device->group in helper function
vfio: Swap order of vfio_device_container_register() and open_device()
vfio: Move device open/close code to be helpfers
vfio: Refactor vfio_device open and close
vfio: Wrap vfio group module init/clean code into helpers
vfio: Refactor dma APIs for emulated devices
vfio: Move vfio group specific code into group.c

 drivers/vfio/Makefile    |   1 +
 drivers/vfio/container.c |  20 +-
 drivers/vfio/group.c     | 877 +++++++++++++++++++++++++++++++++++++
 drivers/vfio/vfio.h      |  54 ++-
 drivers/vfio/vfio_main.c | 909 +++------------------------------------
 5 files changed, 986 insertions(+), 875 deletions(-)
 create mode 100644 drivers/vfio/group.c
Signature · 1 line
2.34.1
author
#1 lore raw

PATCH 1/10 vfio: Simplify vfio_create_group()

From: Jason Gunthorpe <[email protected]>

The vfio.group_lock is now only used to serialize vfio_group creation and destruction, we don't need a micro-optimization of searching, unlocking, then allocating and searching again. Just hold the lock the whole time.

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

---
 drivers/vfio/vfio_main.c | 33 ++++++++++-----------------------
 1 file changed, 10 insertions(+), 23 deletions(-)
drivers/vfio/vfio_main.c +10 -23
index c1c78858b45e..0e66eb40992b 100644
@@ -130,10 +130,12 @@ static void vfio_release_device_set(struct vfio_device *device)
  * Group objects - create, release, get, put, search
  */
 static struct vfio_group *
-__vfio_group_get_from_iommu(struct iommu_group *iommu_group)
+vfio_group_get_from_iommu(struct iommu_group *iommu_group)
 {
 	struct vfio_group *group;
 
+	lockdep_assert_held(&vfio.group_lock);
+
 	/*
 	 * group->iommu_group from the vfio.group_list cannot be NULL
 	 * under the vfio.group_lock.
@@ -147,17 +149,6 @@ __vfio_group_get_from_iommu(struct iommu_group *iommu_group)
 	return NULL;
 }
 
-static struct vfio_group *
-vfio_group_get_from_iommu(struct iommu_group *iommu_group)
-{
-	struct vfio_group *group;
-
-	mutex_lock(&vfio.group_lock);
-	group = __vfio_group_get_from_iommu(iommu_group);
-	mutex_unlock(&vfio.group_lock);
-	return group;
-}
-
 static void vfio_group_release(struct device *dev)
 {
 	struct vfio_group *group = container_of(dev, struct vfio_group, dev);
@@ -212,6 +203,8 @@ static struct vfio_group *vfio_create_group(struct iommu_group *iommu_group,
 	struct vfio_group *ret;
 	int err;
 
+	lockdep_assert_held(&vfio.group_lock);
+
 	group = vfio_group_alloc(iommu_group, type);
 	if (IS_ERR(group))
 		return group;
@@ -224,26 +217,16 @@ static struct vfio_group *vfio_create_group(struct iommu_group *iommu_group,
 		goto err_put;
 	}
 
-	mutex_lock(&vfio.group_lock);
-
-	/* Did we race creating this group? */
-	ret = __vfio_group_get_from_iommu(iommu_group);
-	if (ret)
-		goto err_unlock;
-
 	err = cdev_device_add(&group->cdev, &group->dev);
 	if (err) {
 		ret = ERR_PTR(err);
-		goto err_unlock;
+		goto err_put;
 	}
 
 	list_add(&group->vfio_next, &vfio.group_list);
 
-	mutex_unlock(&vfio.group_lock);
 	return group;
 
-err_unlock:
-	mutex_unlock(&vfio.group_lock);
 err_put:
 	put_device(&group->dev);
 	return ret;
@@ -440,7 +423,9 @@ static struct vfio_group *vfio_noiommu_group_alloc(struct device *dev,
 	if (ret)
 		goto out_put_group;
 
+	mutex_lock(&vfio.group_lock);
 	group = vfio_create_group(iommu_group, type);
+	mutex_unlock(&vfio.group_lock);
 	if (IS_ERR(group)) {
 		ret = PTR_ERR(group);
 		goto out_remove_device;
@@ -489,9 +474,11 @@ static struct vfio_group *vfio_group_find_or_alloc(struct device *dev)
 		return ERR_PTR(-EINVAL);
 	}
 
+	mutex_lock(&vfio.group_lock);
 	group = vfio_group_get_from_iommu(iommu_group);
 	if (!group)
 		group = vfio_create_group(iommu_group, VFIO_IOMMU);
+	mutex_unlock(&vfio.group_lock);
 
 	/* The vfio_group holds a reference to the iommu_group */
 	iommu_group_put(iommu_group);
Signature · 1 line
2.34.1
author
#2 lore raw

PATCH 2/10 vfio: Move the sanity check of the group to vfio_create_group()

In reply to Yi Liu

From: Jason Gunthorpe <[email protected]>

This avoids opening group specific code in __vfio_register_dev() for the sanity check if an (existing) group is not corrupted by having two copies of the same struct device in it. It also simplifies the error unwind for this sanity check since the failure can be detected in the group allocation.

This also prepares for moving the group specific code into separate group.c.

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

---
 drivers/vfio/vfio_main.c | 62 ++++++++++++++++------------------------
 1 file changed, 25 insertions(+), 37 deletions(-)
drivers/vfio/vfio_main.c +25 -37
index 0e66eb40992b..9f05dab0fab5 100644
@@ -130,7 +130,7 @@ static void vfio_release_device_set(struct vfio_device *device)
  * Group objects - create, release, get, put, search
  */
 static struct vfio_group *
-vfio_group_get_from_iommu(struct iommu_group *iommu_group)
+vfio_group_find_from_iommu(struct iommu_group *iommu_group)
 {
 	struct vfio_group *group;
 
@@ -141,10 +141,8 @@ vfio_group_get_from_iommu(struct iommu_group *iommu_group)
 	 * under the vfio.group_lock.
 	 */
 	list_for_each_entry(group, &vfio.group_list, vfio_next) {
-		if (group->iommu_group == iommu_group) {
-			refcount_inc(&group->drivers);
+		if (group->iommu_group == iommu_group)
 			return group;
-		}
 	}
 	return NULL;
 }
@@ -294,23 +292,6 @@ static bool vfio_device_try_get_registration(struct vfio_device *device)
 	return refcount_inc_not_zero(&device->refcount);
 }
 
-static struct vfio_device *vfio_group_get_device(struct vfio_group *group,
-						 struct device *dev)
-{
-	struct vfio_device *device;
-
-	mutex_lock(&group->device_lock);
-	list_for_each_entry(device, &group->device_list, group_next) {
-		if (device->dev == dev &&
-		    vfio_device_try_get_registration(device)) {
-			mutex_unlock(&group->device_lock);
-			return device;
-		}
-	}
-	mutex_unlock(&group->device_lock);
-	return NULL;
-}
-
 /*
  * VFIO driver API
  */
@@ -440,6 +421,21 @@ static struct vfio_group *vfio_noiommu_group_alloc(struct device *dev,
 	return ERR_PTR(ret);
 }
 
+static bool vfio_group_has_device(struct vfio_group *group, struct device *dev)
+{
+	struct vfio_device *device;
+
+	mutex_lock(&group->device_lock);
+	list_for_each_entry(device, &group->device_list, group_next) {
+		if (device->dev == dev) {
+			mutex_unlock(&group->device_lock);
+			return true;
+		}
+	}
+	mutex_unlock(&group->device_lock);
+	return false;
+}
+
 static struct vfio_group *vfio_group_find_or_alloc(struct device *dev)
 {
 	struct iommu_group *iommu_group;
@@ -475,9 +471,15 @@ static struct vfio_group *vfio_group_find_or_alloc(struct device *dev)
 	}
 
 	mutex_lock(&vfio.group_lock);
-	group = vfio_group_get_from_iommu(iommu_group);
-	if (!group)
+	group = vfio_group_find_from_iommu(iommu_group);
+	if (group) {
+		if (WARN_ON(vfio_group_has_device(group, dev)))
+			group = ERR_PTR(-EINVAL);
+		else
+			refcount_inc(&group->drivers);
+	} else {
 		group = vfio_create_group(iommu_group, VFIO_IOMMU);
+	}
 	mutex_unlock(&vfio.group_lock);
 
 	/* The vfio_group holds a reference to the iommu_group */
@@ -488,7 +490,6 @@ static struct vfio_group *vfio_group_find_or_alloc(struct device *dev)
 static int __vfio_register_dev(struct vfio_device *device,
 		struct vfio_group *group)
 {
-	struct vfio_device *existing_device;
 	int ret;
 
 	/*
@@ -510,19 +511,6 @@ static int __vfio_register_dev(struct vfio_device *device,
 	if (!device->dev_set)
 		vfio_assign_device_set(device, device);
 
-	existing_device = vfio_group_get_device(group, device->dev);
-	if (existing_device) {
-		/*
-		 * group->iommu_group is non-NULL because we hold the drivers
-		 * refcount.
-		 */
-		dev_WARN(device->dev, "Device already exists on group %d\n",
-			 iommu_group_id(group->iommu_group));
-		vfio_device_put_registration(existing_device);
-		ret = -EBUSY;
-		goto err_out;
-	}
-
 	/* Our reference on group is moved to the device */
 	device->group = group;
 
Signature · 1 line
2.34.1
author
#3 lore raw

PATCH 3/10 vfio: Create wrappers for group register/unregister

In reply to Yi Liu

This avoids decoding group fields in the common functions used by vfio_device registration, and prepares for further moving the vfio group specific code into separate file.

---
 drivers/vfio/vfio_main.c | 23 ++++++++++++++++-------
 1 file changed, 16 insertions(+), 7 deletions(-)
drivers/vfio/vfio_main.c +16 -7
index 9f05dab0fab5..7017d3da6e6f 100644
@@ -487,6 +487,20 @@ static struct vfio_group *vfio_group_find_or_alloc(struct device *dev)
 	return group;
 }
 
+static void vfio_device_group_register(struct vfio_device *device)
+{
+	mutex_lock(&device->group->device_lock);
+	list_add(&device->group_next, &device->group->device_list);
+	mutex_unlock(&device->group->device_lock);
+}
+
+static void vfio_device_group_unregister(struct vfio_device *device)
+{
+	mutex_lock(&device->group->device_lock);
+	list_del(&device->group_next);
+	mutex_unlock(&device->group->device_lock);
+}
+
 static int __vfio_register_dev(struct vfio_device *device,
 		struct vfio_group *group)
 {
@@ -525,9 +539,7 @@ static int __vfio_register_dev(struct vfio_device *device,
 	/* Refcounting can't start until the driver calls register */
 	refcount_set(&device->refcount, 1);
 
-	mutex_lock(&group->device_lock);
-	list_add(&device->group_next, &group->device_list);
-	mutex_unlock(&group->device_lock);
+	vfio_device_group_register(device);
 
 	return 0;
 err_out:
@@ -587,7 +599,6 @@ static struct vfio_device *vfio_device_get_from_name(struct vfio_group *group,
  * removed.  Open file descriptors for the device... */
 void vfio_unregister_group_dev(struct vfio_device *device)
 {
-	struct vfio_group *group = device->group;
 	unsigned int i = 0;
 	bool interrupted = false;
 	long rc;
@@ -615,9 +626,7 @@ void vfio_unregister_group_dev(struct vfio_device *device)
 		}
 	}
 
-	mutex_lock(&group->device_lock);
-	list_del(&device->group_next);
-	mutex_unlock(&group->device_lock);
+	vfio_device_group_unregister(device);
 
 	/* Balances device_add in register path */
 	device_del(&device->device);
Signature · 1 line
2.34.1
author
#4 lore raw

PATCH 4/10 vfio: Set device->group in helper function

In reply to Yi Liu

This avoids referencing device->group in __vfio_register_dev().

---
 drivers/vfio/vfio_main.c | 41 +++++++++++++++++++++++++---------------
 1 file changed, 26 insertions(+), 15 deletions(-)
drivers/vfio/vfio_main.c +26 -15
index 7017d3da6e6f..d1e8e5d2a1bc 100644
@@ -501,18 +501,29 @@ static void vfio_device_group_unregister(struct vfio_device *device)
 	mutex_unlock(&device->group->device_lock);
 }
 
-static int __vfio_register_dev(struct vfio_device *device,
-		struct vfio_group *group)
+static int vfio_device_set_group(struct vfio_device *device,
+				 enum vfio_group_type type)
 {
-	int ret;
+	struct vfio_group *group;
+
+	if (type == VFIO_IOMMU)
+		group = vfio_group_find_or_alloc(device->dev);
+	else
+		group = vfio_noiommu_group_alloc(device->dev, type);
 
-	/*
-	 * In all cases group is the output of one of the group allocation
-	 * functions and we have group->drivers incremented for us.
-	 */
 	if (IS_ERR(group))
 		return PTR_ERR(group);
 
+	/* Our reference on group is moved to the device */
+	device->group = group;
+	return 0;
+}
+
+static int __vfio_register_dev(struct vfio_device *device,
+			       enum vfio_group_type type)
+{
+	int ret;
+
 	if (WARN_ON(device->ops->bind_iommufd &&
 		    (!device->ops->unbind_iommufd ||
 		     !device->ops->attach_ioas)))
@@ -525,12 +536,13 @@ static int __vfio_register_dev(struct vfio_device *device,
 	if (!device->dev_set)
 		vfio_assign_device_set(device, device);
 
-	/* Our reference on group is moved to the device */
-	device->group = group;
-
 	ret = dev_set_name(&device->device, "vfio%d", device->index);
 	if (ret)
-		goto err_out;
+		return ret;
+
+	ret = vfio_device_set_group(device, type);
+	if (ret)
+		return ret;
 
 	ret = device_add(&device->device);
 	if (ret)
@@ -549,8 +561,7 @@ static int __vfio_register_dev(struct vfio_device *device,
 
 int vfio_register_group_dev(struct vfio_device *device)
 {
-	return __vfio_register_dev(device,
-		vfio_group_find_or_alloc(device->dev));
+	return __vfio_register_dev(device, VFIO_IOMMU);
 }
 EXPORT_SYMBOL_GPL(vfio_register_group_dev);
 
@@ -560,8 +571,7 @@ EXPORT_SYMBOL_GPL(vfio_register_group_dev);
  */
 int vfio_register_emulated_iommu_dev(struct vfio_device *device)
 {
-	return __vfio_register_dev(device,
-		vfio_noiommu_group_alloc(device->dev, VFIO_EMULATED_IOMMU));
+	return __vfio_register_dev(device, VFIO_EMULATED_IOMMU);
 }
 EXPORT_SYMBOL_GPL(vfio_register_emulated_iommu_dev);
 
@@ -631,6 +641,7 @@ void vfio_unregister_group_dev(struct vfio_device *device)
 	/* Balances device_add in register path */
 	device_del(&device->device);
 
+	/* Balances vfio_device_set_group in register path */
 	vfio_device_remove_group(device);
 }
 EXPORT_SYMBOL_GPL(vfio_unregister_group_dev);
Signature · 1 line
2.34.1
author
#6 lore raw

PATCH 5/10 vfio: Swap order of vfio_device_container_register() and open_device()

In reply to Yi Liu

This makes the DMA unmap callback registration to container be consistent across the vfio iommufd compat mode and the legacy container mode.

In the vfio iommufd compat mode, this registration is done in the vfio_iommufd_bind() when creating access which has an unmap callback. This is prior to calling the open_device() op. The existing mdev drivers have been converted to be OK with this order. So it is ok to swap the order of vfio_device_container_register() and open_device() for legacy mode.

This also prepares for further moving group specific code into separate source file.

---
 drivers/vfio/vfio_main.c | 17 +++++++++--------
 1 file changed, 9 insertions(+), 8 deletions(-)
drivers/vfio/vfio_main.c +9 -8
index d1e8e5d2a1bc..4f2d32d4a3d0 100644
@@ -780,6 +780,7 @@ static int vfio_device_first_open(struct vfio_device *device)
 		ret = vfio_group_use_container(device->group);
 		if (ret)
 			goto err_module_put;
+		vfio_device_container_register(device);
 	} else if (device->group->iommufd) {
 		ret = vfio_iommufd_bind(device, device->group->iommufd);
 		if (ret)
@@ -792,17 +793,17 @@ static int vfio_device_first_open(struct vfio_device *device)
 		if (ret)
 			goto err_container;
 	}
-	if (device->group->container)
-		vfio_device_container_register(device);
 	mutex_unlock(&device->group->group_lock);
 	return 0;
 
 err_container:
 	device->kvm = NULL;
-	if (device->group->container)
+	if (device->group->container) {
+		vfio_device_container_unregister(device);
 		vfio_group_unuse_container(device->group);
-	else if (device->group->iommufd)
+	} else if (device->group->iommufd) {
 		vfio_iommufd_unbind(device);
+	}
 err_module_put:
 	mutex_unlock(&device->group->group_lock);
 	module_put(device->dev->driver->owner);
@@ -814,15 +815,15 @@ static void vfio_device_last_close(struct vfio_device *device)
 	lockdep_assert_held(&device->dev_set->lock);
 
 	mutex_lock(&device->group->group_lock);
-	if (device->group->container)
-		vfio_device_container_unregister(device);
 	if (device->ops->close_device)
 		device->ops->close_device(device);
 	device->kvm = NULL;
-	if (device->group->container)
+	if (device->group->container) {
+		vfio_device_container_unregister(device);
 		vfio_group_unuse_container(device->group);
-	else if (device->group->iommufd)
+	} else if (device->group->iommufd) {
 		vfio_iommufd_unbind(device);
+	}
 	mutex_unlock(&device->group->group_lock);
 	module_put(device->dev->driver->owner);
 }
Signature · 1 line
2.34.1
author
#7 lore raw

PATCH 6/10 vfio: Move device open/close code to be helpfers

In reply to Yi Liu

This makes vfio_device_open/close() to be the top level helpers for device open and close. It handles the open_count, and vfio_device_first_open() vfio_device_last_close() do the container/iommufd use/unuse and the device open.

Current vfio_device_open() handles the device open and the device file open which is group specific. After this change, the group specific code is in the vfio_device_open_file() which would be moved to separate group source code in future.

---
 drivers/vfio/vfio_main.c | 46 +++++++++++++++++++++++++---------------
 1 file changed, 29 insertions(+), 17 deletions(-)
drivers/vfio/vfio_main.c +29 -17
index 4f2d32d4a3d0..964d44a35624 100644
@@ -828,20 +828,41 @@ static void vfio_device_last_close(struct vfio_device *device)
 	module_put(device->dev->driver->owner);
 }
 
-static struct file *vfio_device_open(struct vfio_device *device)
+static int vfio_device_open(struct vfio_device *device)
 {
-	struct file *filep;
-	int ret;
+	int ret = 0;
 
 	mutex_lock(&device->dev_set->lock);
 	device->open_count++;
 	if (device->open_count == 1) {
 		ret = vfio_device_first_open(device);
 		if (ret)
-			goto err_unlock;
+			device->open_count--;
 	}
 	mutex_unlock(&device->dev_set->lock);
 
+	return ret;
+}
+
+static void vfio_device_close(struct vfio_device *device)
+{
+	mutex_lock(&device->dev_set->lock);
+	vfio_assert_device_open(device);
+	if (device->open_count == 1)
+		vfio_device_last_close(device);
+	device->open_count--;
+	mutex_unlock(&device->dev_set->lock);
+}
+
+static struct file *vfio_device_open_file(struct vfio_device *device)
+{
+	struct file *filep;
+	int ret;
+
+	ret = vfio_device_open(device);
+	if (ret)
+		goto err_out;
+
 	/*
 	 * We can't use anon_inode_getfd() because we need to modify
 	 * the f_mode flags directly to allow more than just ioctls
@@ -870,12 +891,8 @@ static struct file *vfio_device_open(struct vfio_device *device)
 	return filep;
 
 err_close_device:
-	mutex_lock(&device->dev_set->lock);
-	if (device->open_count == 1)
-		vfio_device_last_close(device);
-err_unlock:
-	device->open_count--;
-	mutex_unlock(&device->dev_set->lock);
+	vfio_device_close(device);
+err_out:
 	return ERR_PTR(ret);
 }
 
@@ -903,7 +920,7 @@ static int vfio_group_ioctl_get_device_fd(struct vfio_group *group,
 		goto err_put_device;
 	}
 
-	filep = vfio_device_open(device);
+	filep = vfio_device_open_file(device);
 	if (IS_ERR(filep)) {
 		ret = PTR_ERR(filep);
 		goto err_put_fdno;
@@ -1086,12 +1103,7 @@ static int vfio_device_fops_release(struct inode *inode, struct file *filep)
 {
 	struct vfio_device *device = filep->private_data;
 
-	mutex_lock(&device->dev_set->lock);
-	vfio_assert_device_open(device);
-	if (device->open_count == 1)
-		vfio_device_last_close(device);
-	device->open_count--;
-	mutex_unlock(&device->dev_set->lock);
+	vfio_device_close(device);
 
 	vfio_device_put_registration(device);
 
Signature · 1 line
2.34.1
Reviewed-by on v1 6/10
#8 lore raw

RE: [PATCH 06/10] vfio: Move device open/close code to be helpfers

Quoted 2 lines

From: Liu, Yi L <[email protected]>
Sent: Thursday, December 1, 2022 10:56 PM

subject: 'helpfers' -> 'helpers'

Quoted 11 lines

This makes vfio_device_open/close() to be the top level helpers for device open and close. It handles the open_count, and vfio_device_first_open() vfio_device_last_close() do the container/iommufd use/unuse and the device
open.

Current vfio_device_open() handles the device open and the device file open which is group specific. After this change, the group specific code is in the vfio_device_open_file() which would be moved to separate group source code in future.

It's clearer as below:
--
vfio: Make vfio_device_open() truly device specific

Then move group related logic into vfio_device_open_file(). Accordingly introduce a vfio_device_close() to pair up.
--

With above:

author
#9 lore raw

Re: [PATCH 06/10] vfio: Move device open/close code to be helpfers

Quoted 21 lines · Kevin
Quoted 2 lines

From: Liu, Yi L <[email protected]>
Sent: Thursday, December 1, 2022 10:56 PM

subject: 'helpfers' -> 'helpers'

Quoted 11 lines

This makes vfio_device_open/close() to be the top level helpers for device open and close. It handles the open_count, and vfio_device_first_open() vfio_device_last_close() do the container/iommufd use/unuse and the device
open.

Current vfio_device_open() handles the device open and the device file open which is group specific. After this change, the group specific code is in the vfio_device_open_file() which would be moved to separate group source code in future.

It's clearer as below:
--
vfio: Make vfio_device_open() truly device specific

Quoted 7 lines

Then move group related logic into vfio_device_open_file(). Accordingly introduce a vfio_device_close() to pair up.
--

With above:

thanks, I'll update your comments into the github branch.

Signature · 2 lines
Regards,
Yi Liu
author
#10 lore raw

PATCH 7/10 vfio: Refactor vfio_device open and close

In reply to Yi Liu

This refactor makes the vfio_device_open() to accept device, iommufd_ctx pointer and kvm pointer. These parameters are generic items in today's group path and furute device cdev path. Caller of vfio_device_open() should take care the necessary protections. e.g. the current group path need to hold the group_lock to ensure the iommufd_ctx and kvm pointer are valid.

This refactor also wraps the group spefcific codes in the device open and close paths to be paired helpers like:

- vfio_device_group_open/close(): call vfio_device_open/close()
- vfio_device_group_use/unuse_iommu(): call iommufd or container use/unuse per iommufd_ctx pointer. Called by vfio_device_first_open() and vfio_device_last_close()

Such helpers are supposed to be moved to group.c. While iommufd related codes will be kept in the generic helpers since future device cdev path also need to handle iommufd.

---
 drivers/vfio/vfio_main.c | 133 +++++++++++++++++++++++++--------------
 1 file changed, 87 insertions(+), 46 deletions(-)
drivers/vfio/vfio_main.c +87 -46
index 964d44a35624..6906aa099c0e 100644
@@ -756,7 +756,38 @@ static bool vfio_assert_device_open(struct vfio_device *device)
 	return !WARN_ON_ONCE(!READ_ONCE(device->open_count));
 }
 
-static int vfio_device_first_open(struct vfio_device *device)
+static int vfio_device_group_use_iommu(struct vfio_device *device)
+{
+	struct vfio_group *group = device->group;
+	int ret = 0;
+
+	lockdep_assert_held(&group->group_lock);
+
+	if (WARN_ON(!group->container))
+		return -EINVAL;
+
+	ret = vfio_group_use_container(group);
+	if (ret)
+		return ret;
+	vfio_device_container_register(device);
+	return 0;
+}
+
+static void vfio_device_group_unuse_iommu(struct vfio_device *device)
+{
+	struct vfio_group *group = device->group;
+
+	lockdep_assert_held(&group->group_lock);
+
+	if (WARN_ON(!group->container))
+		return;
+
+	vfio_device_container_unregister(device);
+	vfio_group_unuse_container(group);
+}
+
+static int vfio_device_first_open(struct vfio_device *device,
+				  struct iommufd_ctx *iommufd, struct kvm *kvm)
 {
 	int ret;
 
@@ -765,77 +796,56 @@ static int vfio_device_first_open(struct vfio_device *device)
 	if (!try_module_get(device->dev->driver->owner))
 		return -ENODEV;
 
-	/*
-	 * 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.
-	 */
-	mutex_lock(&device->group->group_lock);
-	if (!vfio_group_has_iommu(device->group)) {
-		ret = -EINVAL;
+	if (iommufd)
+		ret = vfio_iommufd_bind(device, iommufd);
+	else
+		ret = vfio_device_group_use_iommu(device);
+	if (ret)
 		goto err_module_put;
-	}
 
-	if (device->group->container) {
-		ret = vfio_group_use_container(device->group);
-		if (ret)
-			goto err_module_put;
-		vfio_device_container_register(device);
-	} else if (device->group->iommufd) {
-		ret = vfio_iommufd_bind(device, device->group->iommufd);
-		if (ret)
-			goto err_module_put;
-	}
-
-	device->kvm = device->group->kvm;
+	device->kvm = kvm;
 	if (device->ops->open_device) {
 		ret = device->ops->open_device(device);
 		if (ret)
-			goto err_container;
+			goto err_unuse_iommu;
 	}
-	mutex_unlock(&device->group->group_lock);
 	return 0;
 
-err_container:
+err_unuse_iommu:
 	device->kvm = NULL;
-	if (device->group->container) {
-		vfio_device_container_unregister(device);
-		vfio_group_unuse_container(device->group);
-	} else if (device->group->iommufd) {
+	if (iommufd)
 		vfio_iommufd_unbind(device);
-	}
+	else
+		vfio_device_group_unuse_iommu(device);
 err_module_put:
-	mutex_unlock(&device->group->group_lock);
 	module_put(device->dev->driver->owner);
 	return ret;
 }
 
-static void vfio_device_last_close(struct vfio_device *device)
+static void vfio_device_last_close(struct vfio_device *device,
+				   struct iommufd_ctx *iommufd)
 {
 	lockdep_assert_held(&device->dev_set->lock);
 
-	mutex_lock(&device->group->group_lock);
 	if (device->ops->close_device)
 		device->ops->close_device(device);
 	device->kvm = NULL;
-	if (device->group->container) {
-		vfio_device_container_unregister(device);
-		vfio_group_unuse_container(device->group);
-	} else if (device->group->iommufd) {
+	if (iommufd)
 		vfio_iommufd_unbind(device);
-	}
-	mutex_unlock(&device->group->group_lock);
+	else
+		vfio_device_group_unuse_iommu(device);
 	module_put(device->dev->driver->owner);
 }
 
-static int vfio_device_open(struct vfio_device *device)
+static int vfio_device_open(struct vfio_device *device,
+			    struct iommufd_ctx *iommufd, struct kvm *kvm)
 {
 	int ret = 0;
 
 	mutex_lock(&device->dev_set->lock);
 	device->open_count++;
 	if (device->open_count == 1) {
-		ret = vfio_device_first_open(device);
+		ret = vfio_device_first_open(device, iommufd, kvm);
 		if (ret)
 			device->open_count--;
 	}
@@ -844,22 +854,53 @@ static int vfio_device_open(struct vfio_device *device)
 	return ret;
 }
 
-static void vfio_device_close(struct vfio_device *device)
+static void vfio_device_close(struct vfio_device *device,
+			      struct iommufd_ctx *iommufd)
 {
 	mutex_lock(&device->dev_set->lock);
 	vfio_assert_device_open(device);
 	if (device->open_count == 1)
-		vfio_device_last_close(device);
+		vfio_device_last_close(device, iommufd);
 	device->open_count--;
 	mutex_unlock(&device->dev_set->lock);
 }
 
+static int vfio_device_group_open(struct vfio_device *device)
+{
+	int ret;
+
+	mutex_lock(&device->group->group_lock);
+	if (!vfio_group_has_iommu(device->group)) {
+		ret = -EINVAL;
+		goto out_unlock;
+	}
+
+	/*
+	 * 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);
+
+out_unlock:
+	mutex_unlock(&device->group->group_lock);
+	return ret;
+}
+
+static void vfio_device_group_close(struct vfio_device *device)
+{
+	mutex_lock(&device->group->group_lock);
+	vfio_device_close(device, device->group->iommufd);
+	mutex_unlock(&device->group->group_lock);
+}
+
 static struct file *vfio_device_open_file(struct vfio_device *device)
 {
 	struct file *filep;
 	int ret;
 
-	ret = vfio_device_open(device);
+	ret = vfio_device_group_open(device);
 	if (ret)
 		goto err_out;
 
@@ -891,7 +932,7 @@ static struct file *vfio_device_open_file(struct vfio_device *device)
 	return filep;
 
 err_close_device:
-	vfio_device_close(device);
+	vfio_device_group_close(device);
 err_out:
 	return ERR_PTR(ret);
 }
@@ -1103,7 +1144,7 @@ static int vfio_device_fops_release(struct inode *inode, struct file *filep)
 {
 	struct vfio_device *device = filep->private_data;
 
-	vfio_device_close(device);
+	vfio_device_group_close(device);
 
 	vfio_device_put_registration(device);
 
Signature · 1 line
2.34.1
Reviewed-by on v1 7/10
#11 lore raw

RE: [PATCH 07/10] vfio: Refactor vfio_device open and close

Quoted 9 lines

From: Liu, Yi L <[email protected]>
Sent: Thursday, December 1, 2022 10:56 PM

This refactor makes the vfio_device_open() to accept device, iommufd_ctx pointer and kvm pointer. These parameters are generic items in today's group path and furute device cdev path. Caller of vfio_device_open() should take care the necessary protections. e.g. the current group path need to hold the group_lock to ensure the iommufd_ctx and kvm pointer are valid.

Quoted 6 lines

This refactor also wraps the group spefcific codes in the device open and close paths to be paired helpers like:

- vfio_device_group_open/close(): call vfio_device_open/close()
- vfio_device_group_use/unuse_iommu(): call iommufd or container use/unuse

this pair is container specific. iommufd vs. container is selected in vfio_device_first_open().

Quoted 8 lines
drivers/vfio/vfio_main.c +0 -2
@@ -765,77 +796,56 @@ static int vfio_device_first_open(struct vfio_device
 *device)
 	if (!try_module_get(device->dev->driver->owner))
 		return -ENODEV;
 
-	/*
-	 * Here we pass the KVM pointer with the group under the lock.  If

the

drivers/vfio/vfio_main.c static int vfio_device_first_open(struct vfio_device *device) · line 800

Quoted 8 lines
drivers/vfio/vfio_main.c +2 -6
-	 * device driver will use it, it must obtain a reference and release it
-	 * during close_device.
-	 */
-	mutex_lock(&device->group->group_lock);
-	if (!vfio_group_has_iommu(device->group)) {
-		ret = -EINVAL;
+	if (iommufd)
+		ret = vfio_iommufd_bind(device, iommufd);

This probably should be renamed to vfio_device_iommufd_bind().

drivers/vfio/vfio_main.c static int vfio_device_first_open(struct vfio_device *device) · line 802

Quoted 2 lines
drivers/vfio/vfio_main.c +2 -0
+	else
+		ret = vfio_device_group_use_iommu(device);

what about vfio_device_container_bind()?

Apart from those nits:

author
#12 lore raw

Re: [PATCH 07/10] vfio: Refactor vfio_device open and close

Quoted 12 lines · Kevin
Quoted 12 lines

From: Liu, Yi L <[email protected]>
Sent: Thursday, December 1, 2022 10:56 PM

This refactor makes the vfio_device_open() to accept device, iommufd_ctx pointer and kvm pointer. These parameters are generic items in today's group path and furute device cdev path. Caller of vfio_device_open() should take care the necessary protections. e.g. the current group path need to hold the group_lock to ensure the iommufd_ctx and kvm pointer are valid.

This refactor also wraps the group spefcific codes in the device open and close paths to be paired helpers like:

Quoted 6 lines
Quoted 3 lines

- vfio_device_group_open/close(): call vfio_device_open/close()
- vfio_device_group_use/unuse_iommu(): call iommufd or container use/unuse

this pair is container specific. iommufd vs. container is selected in vfio_device_first_open().

got it.

Quoted 5 lines
Quoted 5 lines
drivers/vfio/vfio_main.c +0 -0
@@ -765,77 +796,56 @@ static int vfio_device_first_open(struct vfio_device
 *device)
  	if (!try_module_get(device->dev->driver->owner))
  		return -ENODEV;
 
Quoted 13 lines

drivers/vfio/vfio_main.c static int vfio_device_first_open(struct vfio_device *device) · line 800

Quoted 11 lines
(quoted diff) +0 -2
-	/*
-	 * Here we pass the KVM pointer with the group under the lock.  If

the

drivers/vfio/vfio_main.c +2 -6
-	 * device driver will use it, it must obtain a reference and release it
-	 * during close_device.
-	 */
-	mutex_lock(&device->group->group_lock);
-	if (!vfio_group_has_iommu(device->group)) {
-		ret = -EINVAL;
+	if (iommufd)
+		ret = vfio_iommufd_bind(device, iommufd);

This probably should be renamed to vfio_device_iommufd_bind().

I'd like to see if Jason wants to modify it or not as it is introduced in vfio compat series.

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

Quoted 5 lines

drivers/vfio/vfio_main.c static int vfio_device_first_open(struct vfio_device *device) · line 802

Quoted 3 lines
drivers/vfio/vfio_main.c +2 -0
+	else
+		ret = vfio_device_group_use_iommu(device);

what about vfio_device_container_bind()?

maybe use_iommu seems suit more. bind is more likely a kind of associating something with something. but this helper is more kind of using the container. so I chose use_iommu. But I see the value of using bind, it would make the two branches aligned.

Quoted 3 lines

Apart from those nits:

thanks.

Signature · 2 lines
Regards,
Yi Liu
#13 lore raw

Re: [PATCH 07/10] vfio: Refactor vfio_device open and close

Quoted 41 lines · Yi Liu
Quoted 12 lines · Kevin
Quoted 12 lines

From: Liu, Yi L <[email protected]>
Sent: Thursday, December 1, 2022 10:56 PM

This refactor makes the vfio_device_open() to accept device, iommufd_ctx pointer and kvm pointer. These parameters are generic items in today's group path and furute device cdev path. Caller of vfio_device_open() should take care the necessary protections. e.g. the current group path need to hold the group_lock to ensure the iommufd_ctx and kvm pointer are valid.

This refactor also wraps the group spefcific codes in the device open and close paths to be paired helpers like:

Quoted 6 lines
Quoted 3 lines

- vfio_device_group_open/close(): call vfio_device_open/close()
- vfio_device_group_use/unuse_iommu(): call iommufd or container use/unuse

this pair is container specific. iommufd vs. container is selected in vfio_device_first_open().

got it.

Quoted 5 lines
Quoted 5 lines
drivers/vfio/vfio_main.c +0 -0
@@ -765,77 +796,56 @@ static int vfio_device_first_open(struct vfio_device
 *device)
  	if (!try_module_get(device->dev->driver->owner))
  		return -ENODEV;
 
Quoted 13 lines

drivers/vfio/vfio_main.c static int vfio_device_first_open(struct vfio_device *device) · line 800

Quoted 11 lines
(quoted diff) +0 -2
-	/*
-	 * Here we pass the KVM pointer with the group under the lock.  If

the

drivers/vfio/vfio_main.c +2 -6
-	 * device driver will use it, it must obtain a reference and release it
-	 * during close_device.
-	 */
-	mutex_lock(&device->group->group_lock);
-	if (!vfio_group_has_iommu(device->group)) {
-		ret = -EINVAL;
+	if (iommufd)
+		ret = vfio_iommufd_bind(device, iommufd);

This probably should be renamed to vfio_device_iommufd_bind().

Quoted 4 lines

I'd like to see if Jason wants to modify it or not as it is introduced in vfio compat series.

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

Ah, it is a bit late, if we want to change it please make a patch

Quoted 10 lines
Quoted 5 lines

drivers/vfio/vfio_main.c static int vfio_device_first_open(struct vfio_device *device) · line 802

Quoted 3 lines
drivers/vfio/vfio_main.c +2 -0
+	else
+		ret = vfio_device_group_use_iommu(device);

what about vfio_device_container_bind()?

maybe use_iommu seems suit more. bind is more likely a kind of associating something with something. but this helper is more kind of using the container. so I chose use_iommu. But I see the value of using bind, it would make the two branches aligned.

I prefer use iommu, "bind" is so vauge

Jason

#14 lore raw

Re: [PATCH 07/10] vfio: Refactor vfio_device open and close

In reply to Yi Liu
Quoted 3 lines · On Thu, 1 Dec 2022 06:55:32 -0800 Yi Liu <[email protected]> wrote:

This refactor makes the vfio_device_open() to accept device, iommufd_ctx pointer and kvm pointer. These parameters are generic items in today's group path and furute device cdev path. Caller of vfio_device_open() should

Minor nit beyond what's already been mentioned here:

s/furute/future/

Thanks,
Alex

author
#15 lore raw

Re: [PATCH 07/10] vfio: Refactor vfio_device open and close

Quoted 11 lines · Alex Williamson
Quoted 3 lines · On Thu, 1 Dec 2022 06:55:32 -0800 Yi Liu <[email protected]> wrote:

This refactor makes the vfio_device_open() to accept device, iommufd_ctx pointer and kvm pointer. These parameters are generic items in today's group path and furute device cdev path. Caller of vfio_device_open() should

Minor nit beyond what's already been mentioned here:

s/furute/future/

got it.:-)

Signature · 2 lines
Regards,
Yi Liu
author
#16 lore raw

PATCH 8/10 vfio: Wrap vfio group module init/clean code into helpers

In reply to Yi Liu

This wraps the init/clean code of vfio group global variable to be helpers, and prepares for further moving vfio group specific code into separate file.

As container is used by group, so vfio_container_init/cleanup() is moved into vfio_group_init/cleanup().

---
 drivers/vfio/vfio_main.c | 56 ++++++++++++++++++++++++++--------------
 1 file changed, 36 insertions(+), 20 deletions(-)
drivers/vfio/vfio_main.c +36 -20
index 6906aa099c0e..6e7daed47e97 100644
@@ -2071,12 +2071,11 @@ static char *vfio_devnode(struct device *dev, umode_t *mode)
 	return kasprintf(GFP_KERNEL, "vfio/%s", dev_name(dev));
 }
 
-static int __init vfio_init(void)
+static int __init vfio_group_init(void)
 {
 	int ret;
 
 	ida_init(&vfio.group_ida);
-	ida_init(&vfio.device_ida);
 	mutex_init(&vfio.group_lock);
 	INIT_LIST_HEAD(&vfio.group_list);
 
@@ -2093,24 +2092,12 @@ static int __init vfio_init(void)
 
 	vfio.class->devnode = vfio_devnode;
 
-	/* /sys/class/vfio-dev/vfioX */
-	vfio.device_class = class_create(THIS_MODULE, "vfio-dev");
-	if (IS_ERR(vfio.device_class)) {
-		ret = PTR_ERR(vfio.device_class);
-		goto err_dev_class;
-	}
-
 	ret = alloc_chrdev_region(&vfio.group_devt, 0, MINORMASK + 1, "vfio");
 	if (ret)
 		goto err_alloc_chrdev;
-
-	pr_info(DRIVER_DESC " version: " DRIVER_VERSION "\n");
 	return 0;
 
 err_alloc_chrdev:
-	class_destroy(vfio.device_class);
-	vfio.device_class = NULL;
-err_dev_class:
 	class_destroy(vfio.class);
 	vfio.class = NULL;
 err_group_class:
@@ -2118,18 +2105,47 @@ static int __init vfio_init(void)
 	return ret;
 }
 
-static void __exit vfio_cleanup(void)
+static void vfio_group_cleanup(void)
 {
 	WARN_ON(!list_empty(&vfio.group_list));
-
-	ida_destroy(&vfio.device_ida);
 	ida_destroy(&vfio.group_ida);
 	unregister_chrdev_region(vfio.group_devt, MINORMASK + 1);
-	class_destroy(vfio.device_class);
-	vfio.device_class = NULL;
 	class_destroy(vfio.class);
-	vfio_container_cleanup();
 	vfio.class = NULL;
+	vfio_container_cleanup();
+}
+
+static int __init vfio_init(void)
+{
+	int ret;
+
+	ida_init(&vfio.device_ida);
+
+	ret = vfio_group_init();
+	if (ret)
+		return ret;
+
+	/* /sys/class/vfio-dev/vfioX */
+	vfio.device_class = class_create(THIS_MODULE, "vfio-dev");
+	if (IS_ERR(vfio.device_class)) {
+		ret = PTR_ERR(vfio.device_class);
+		goto err_dev_class;
+	}
+
+	pr_info(DRIVER_DESC " version: " DRIVER_VERSION "\n");
+	return 0;
+
+err_dev_class:
+	vfio_group_cleanup();
+	return ret;
+}
+
+static void __exit vfio_cleanup(void)
+{
+	ida_destroy(&vfio.device_ida);
+	class_destroy(vfio.device_class);
+	vfio.device_class = NULL;
+	vfio_group_cleanup();
 	xa_destroy(&vfio_device_set_xa);
 }
 
Signature · 1 line
2.34.1
author
#17 lore raw

PATCH 9/10 vfio: Refactor dma APIs for emulated devices

In reply to Yi Liu

To use group helpers instead of opening group related code in the API. This prepares moving group specific code out of vfio_main.c.

---
 drivers/vfio/container.c | 20 +++++++++++++-------
 drivers/vfio/vfio.h      | 32 ++++++++++++++++----------------
 drivers/vfio/vfio_main.c | 25 ++++++++++++++-----------
 3 files changed, 43 insertions(+), 34 deletions(-)
drivers/vfio/container.c +13 -7
index 6b362d97d682..b7a9560ab25e 100644
@@ -540,10 +540,12 @@ void vfio_group_unuse_container(struct vfio_group *group)
 	fput(group->opened_file);
 }
 
-int vfio_container_pin_pages(struct vfio_container *container,
-			     struct iommu_group *iommu_group, dma_addr_t iova,
-			     int npage, int prot, struct page **pages)
+int vfio_device_container_pin_pages(struct vfio_device *device,
+				    dma_addr_t iova, int npage,
+				    int prot, struct page **pages)
 {
+	struct vfio_container *container = device->group->container;
+	struct iommu_group *iommu_group = device->group->iommu_group;
 	struct vfio_iommu_driver *driver = container->iommu_driver;
 
 	if (npage > VFIO_PIN_PAGES_MAX_ENTRIES)
@@ -555,9 +557,11 @@ int vfio_container_pin_pages(struct vfio_container *container,
 				      npage, prot, pages);
 }
 
-void vfio_container_unpin_pages(struct vfio_container *container,
-				dma_addr_t iova, int npage)
+void vfio_device_container_unpin_pages(struct vfio_device *device,
+				       dma_addr_t iova, int npage)
 {
+	struct vfio_container *container = device->group->container;
+
 	if (WARN_ON(npage <= 0 || npage > VFIO_PIN_PAGES_MAX_ENTRIES))
 		return;
 
@@ -565,9 +569,11 @@ void vfio_container_unpin_pages(struct vfio_container *container,
 						  npage);
 }
 
-int vfio_container_dma_rw(struct vfio_container *container, dma_addr_t iova,
-			  void *data, size_t len, bool write)
+int vfio_device_container_dma_rw(struct vfio_device *device,
+				 dma_addr_t iova, void *data,
+				 size_t len, bool write)
 {
+	struct vfio_container *container = device->group->container;
 	struct vfio_iommu_driver *driver = container->iommu_driver;
 
 	if (unlikely(!driver || !driver->ops->dma_rw))
drivers/vfio/vfio.h +16 -16
index ce5fe3fc493b..a112e8f2b291 100644
@@ -122,13 +122,14 @@ int vfio_container_attach_group(struct vfio_container *container,
 void vfio_group_detach_container(struct vfio_group *group);
 void vfio_device_container_register(struct vfio_device *device);
 void vfio_device_container_unregister(struct vfio_device *device);
-int vfio_container_pin_pages(struct vfio_container *container,
-			     struct iommu_group *iommu_group, dma_addr_t iova,
-			     int npage, int prot, struct page **pages);
-void vfio_container_unpin_pages(struct vfio_container *container,
-				dma_addr_t iova, int npage);
-int vfio_container_dma_rw(struct vfio_container *container, dma_addr_t iova,
-			  void *data, size_t len, bool write);
+int vfio_device_container_pin_pages(struct vfio_device *device,
+				    dma_addr_t iova, int npage,
+				    int prot, struct page **pages);
+void vfio_device_container_unpin_pages(struct vfio_device *device,
+				       dma_addr_t iova, int npage);
+int vfio_device_container_dma_rw(struct vfio_device *device,
+				 dma_addr_t iova, void *data,
+				 size_t len, bool write);
 
 int __init vfio_container_init(void);
 void vfio_container_cleanup(void);
@@ -166,22 +167,21 @@ static inline void vfio_device_container_unregister(struct vfio_device *device)
 {
 }
 
-static inline int vfio_container_pin_pages(struct vfio_container *container,
-					   struct iommu_group *iommu_group,
-					   dma_addr_t iova, int npage, int prot,
-					   struct page **pages)
+static inline int vfio_device_container_pin_pages(struct vfio_device *device,
+						  dma_addr_t iova, int npage,
+						  int prot, struct page **pages)
 {
 	return -EOPNOTSUPP;
 }
 
-static inline void vfio_container_unpin_pages(struct vfio_container *container,
-					      dma_addr_t iova, int npage)
+static inline void vfio_device_container_unpin_pages(struct vfio_device *device,
+						     dma_addr_t iova, int npage)
 {
 }
 
-static inline int vfio_container_dma_rw(struct vfio_container *container,
-					dma_addr_t iova, void *data, size_t len,
-					bool write)
+static inline int vfio_device_container_dma_rw(struct vfio_device *device,
+					       dma_addr_t iova, void *data,
+					       size_t len, bool write)
 {
 	return -EOPNOTSUPP;
 }
drivers/vfio/vfio_main.c +14 -11
index 6e7daed47e97..11fe044e8703 100644
@@ -1943,6 +1943,11 @@ int vfio_set_irqs_validate_and_prepare(struct vfio_irq_set *hdr, int num_irqs,
 }
 EXPORT_SYMBOL(vfio_set_irqs_validate_and_prepare);
 
+static bool vfio_device_has_container(struct vfio_device *device)
+{
+	return device->group->container;
+}
+
 /*
  * Pin contiguous user pages and return their associated host pages for local
  * domain only.
@@ -1955,7 +1960,7 @@ EXPORT_SYMBOL(vfio_set_irqs_validate_and_prepare);
  * Return error or number of pages pinned.
  *
  * A driver may only call this function if the vfio_device was created
- * by vfio_register_emulated_iommu_dev() due to vfio_container_pin_pages().
+ * by vfio_register_emulated_iommu_dev() due to vfio_device_container_pin_pages().
  */
 int vfio_pin_pages(struct vfio_device *device, dma_addr_t iova,
 		   int npage, int prot, struct page **pages)
@@ -1963,10 +1968,9 @@ int vfio_pin_pages(struct vfio_device *device, dma_addr_t iova,
 	/* group->container cannot change while a vfio device is open */
 	if (!pages || !npage || WARN_ON(!vfio_assert_device_open(device)))
 		return -EINVAL;
-	if (device->group->container)
-		return vfio_container_pin_pages(device->group->container,
-						device->group->iommu_group,
-						iova, npage, prot, pages);
+	if (vfio_device_has_container(device))
+		return vfio_device_container_pin_pages(device, iova,
+						       npage, prot, pages);
 	if (device->iommufd_access) {
 		int ret;
 
@@ -2002,9 +2006,8 @@ void vfio_unpin_pages(struct vfio_device *device, dma_addr_t iova, int npage)
 	if (WARN_ON(!vfio_assert_device_open(device)))
 		return;
 
-	if (device->group->container) {
-		vfio_container_unpin_pages(device->group->container, iova,
-					   npage);
+	if (vfio_device_has_container(device)) {
+		vfio_device_container_unpin_pages(device, iova, npage);
 		return;
 	}
 	if (device->iommufd_access) {
@@ -2041,9 +2044,9 @@ int vfio_dma_rw(struct vfio_device *device, dma_addr_t iova, void *data,
 	if (!data || len <= 0 || !vfio_assert_device_open(device))
 		return -EINVAL;
 
-	if (device->group->container)
-		return vfio_container_dma_rw(device->group->container, iova,
-					     data, len, write);
+	if (vfio_device_has_container(device))
+		return vfio_device_container_dma_rw(device, iova,
+						    data, len, write);
 
 	if (device->iommufd_access) {
 		unsigned int flags = 0;
Signature · 1 line
2.34.1
author
#18 lore raw

PATCH 10/10 vfio: Move vfio group specific code into group.c

In reply to Yi Liu

This prepares for compiling out vfio group after vfio device cdev is added. No vfio_group decode code should be in vfio_main.c, and neither device->group reference should be in vfio_main.c.

No functional change is intended.

---
 drivers/vfio/Makefile    |   1 +
 drivers/vfio/group.c     | 877 +++++++++++++++++++++++++++++++++++++++
 drivers/vfio/vfio.h      |  22 +
 drivers/vfio/vfio_main.c | 877 +--------------------------------------
 4 files changed, 907 insertions(+), 870 deletions(-)
 create mode 100644 drivers/vfio/group.c
drivers/vfio/Makefile +1 -0
index b953517dc70f..3783db7e8082 100644
@@ -4,6 +4,7 @@ vfio_virqfd-y := virqfd.o
 obj-$(CONFIG_VFIO) += vfio.o
 
 vfio-y += vfio_main.o \
+	  group.o \
 	  iova_bitmap.o
 vfio-$(CONFIG_IOMMUFD) += iommufd.o
 vfio-$(CONFIG_VFIO_CONTAINER) += container.o
drivers/vfio/group.c new file +877 -0

877 lines not on this page · raw message

drivers/vfio/vfio.h +22 -0
index a112e8f2b291..2e05418fd18d 100644
@@ -6,6 +6,7 @@
 #ifndef __VFIO_VFIO_H__
 #define __VFIO_VFIO_H__
 
+#include <linux/file.h>
 #include <linux/device.h>
 #include <linux/cdev.h>
 #include <linux/module.h>
@@ -15,6 +16,15 @@ struct iommu_group;
 struct vfio_device;
 struct vfio_container;
 
+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);
+
+extern const struct file_operations vfio_device_fops;
+
 enum vfio_group_type {
 	/*
 	 * Physical device with IOMMU backing.
@@ -66,6 +76,18 @@ struct vfio_group {
 	struct iommufd_ctx		*iommufd;
 };
 
+int vfio_device_set_group(struct vfio_device *device,
+			  enum vfio_group_type type);
+void vfio_device_remove_group(struct vfio_device *device);
+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);
+bool vfio_device_has_container(struct vfio_device *device);
+int __init vfio_group_init(void);
+void vfio_group_cleanup(void);
+
 #if IS_ENABLED(CONFIG_VFIO_CONTAINER)
 /* events for the backend driver notify callback */
 enum vfio_iommu_notify_type {
drivers/vfio/vfio_main.c +7 -870

975 lines not on this page · raw message

Signature · 1 line
2.34.1
Reviewed-by on v1 10/10
#19 lore raw

RE: [PATCH 10/10] vfio: Move vfio group specific code into group.c

Quoted 10 lines

From: Liu, Yi L <[email protected]>
Sent: Thursday, December 1, 2022 10:56 PM

This prepares for compiling out vfio group after vfio device cdev is added. No vfio_group decode code should be in vfio_main.c, and neither device->group reference should be in vfio_main.c.

No functional change is intended.

Reviewed-by on the v1 series
#20 lore raw
In reply to Yi Liu
Quoted 11 lines · Yi Liu

With the introduction of iommufd[1], VFIO is towarding to provide device centric uAPI after adapting to iommufd. With this trend, existing VFIO group infrastructure is optional once VFIO converted to device centric.

This series moves the group specific code out of vfio_main.c, prepares for compiling group infrastructure out after adding vfio device cdev[2]

Complete code in below branch:

https://github.com/yiliu1765/iommufd/commits/vfio_group_split_v1

Quoted 7 lines

This is based on Jason's "Connect VFIO to IOMMUFD"[3] and my "Make mdev driver dma_unmap callback tolerant to unmaps come before device open"[4]

[1] https://lore.kernel.org/all/[email protected]/
[2] https://github.com/yiliu1765/iommufd/tree/wip/vfio_device_cdev
[3] https://lore.kernel.org/kvm/[email protected]/
[4] https://lore.kernel.org/kvm/[email protected]/

This looks good to me, and it applies OK to my branch here:

https://git.kernel.org/pub/scm/linux/kernel/git/jgg/iommufd.git/

Alex, if you ack this in the next few days I can include it in the iommufd PR, otherwise it can go into the vfio tree in January

Thanks,
Jason

author
#21 lore raw
Quoted 20 lines · Jason Gunthorpe
Quoted 11 lines · Yi Liu

With the introduction of iommufd[1], VFIO is towarding to provide device centric uAPI after adapting to iommufd. With this trend, existing VFIO group infrastructure is optional once VFIO converted to device centric.

This series moves the group specific code out of vfio_main.c, prepares for compiling group infrastructure out after adding vfio device cdev[2]

Complete code in below branch:

https://github.com/yiliu1765/iommufd/commits/vfio_group_split_v1

Quoted 7 lines

This is based on Jason's "Connect VFIO to IOMMUFD"[3] and my "Make mdev driver dma_unmap callback tolerant to unmaps come before device open"[4]

[1] https://lore.kernel.org/all/[email protected]/
[2] https://github.com/yiliu1765/iommufd/tree/wip/vfio_device_cdev
[3] https://lore.kernel.org/kvm/[email protected]/
[4] https://lore.kernel.org/kvm/[email protected]/

Quoted 9 lines

This looks good to me, and it applies OK to my branch here:

https://git.kernel.org/pub/scm/linux/kernel/git/jgg/iommufd.git/

Alex, if you ack this in the next few days I can include it in the iommufd PR, otherwise it can go into the vfio tree in January

thanks. btw. I've updated my github to incorporate Kevin's nit and also r-b from you and Kevin.

Signature · 2 lines
Regards,
Yi Liu
#22 lore raw
Quoted 25 lines · Yi Liu
Quoted 24 lines · Jason Gunthorpe
Quoted 11 lines · Yi Liu

With the introduction of iommufd[1], VFIO is towarding to provide device centric uAPI after adapting to iommufd. With this trend, existing VFIO group infrastructure is optional once VFIO converted to device centric.

This series moves the group specific code out of vfio_main.c, prepares for compiling group infrastructure out after adding vfio device cdev[2]

Complete code in below branch:

https://github.com/yiliu1765/iommufd/commits/vfio_group_split_v1

Quoted 7 lines

This is based on Jason's "Connect VFIO to IOMMUFD"[3] and my "Make mdev driver dma_unmap callback tolerant to unmaps come before device open"[4]

[1] https://lore.kernel.org/all/[email protected]/
[2] https://github.com/yiliu1765/iommufd/tree/wip/vfio_device_cdev
[3] https://lore.kernel.org/kvm/[email protected]/
[4] https://lore.kernel.org/kvm/[email protected]/

This looks good to me, and it applies OK to my branch here:

https://git.kernel.org/pub/scm/linux/kernel/git/jgg/iommufd.git/

Quoted 8 lines
Quoted 5 lines

Alex, if you ack this in the next few days I can include it in the iommufd PR, otherwise it can go into the vfio tree in January

thanks. btw. I've updated my github to incorporate Kevin's nit and also r-b from you and Kevin.

Please rebase it on the above branch also

Thanks,
Jason

Reviewed-by
#23 lore raw
Quoted 29 lines · On Fri, 2 Dec 2022 10:44:30 -0400 Jason Gunthorpe <[email protected]> wrote:
Quoted 28 lines · Yi Liu
Quoted 27 lines · Jason Gunthorpe
Quoted 11 lines · Yi Liu

With the introduction of iommufd[1], VFIO is towarding to provide device centric uAPI after adapting to iommufd. With this trend, existing VFIO group infrastructure is optional once VFIO converted to device centric.

This series moves the group specific code out of vfio_main.c, prepares for compiling group infrastructure out after adding vfio device cdev[2]

Complete code in below branch:

https://github.com/yiliu1765/iommufd/commits/vfio_group_split_v1

Quoted 7 lines

This is based on Jason's "Connect VFIO to IOMMUFD"[3] and my "Make mdev driver dma_unmap callback tolerant to unmaps come before device open"[4]

[1] https://lore.kernel.org/all/[email protected]/
[2] https://github.com/yiliu1765/iommufd/tree/wip/vfio_device_cdev
[3] https://lore.kernel.org/kvm/[email protected]/
[4] https://lore.kernel.org/kvm/[email protected]/

This looks good to me, and it applies OK to my branch here:

https://git.kernel.org/pub/scm/linux/kernel/git/jgg/iommufd.git/

Alex, if you ack this in the next few days I can include it in the iommufd PR, otherwise it can go into the vfio tree in January

Quoted 7 lines
Quoted 5 lines
Quoted 2 lines

thanks. btw. I've updated my github to incorporate Kevin's nit and also r-b from you and Kevin.

Please rebase it on the above branch also

It looks fine to me aside from the previous review comments and my own spelling nit. I also don't see that this adds any additional conflicts vs the existing iommufd integration for any outstanding vfio patches on the list, therefore, where there's not already a sign-off from me:

Thanks,
Alex

author
#24 lore raw
Quoted 32 lines · Alex Williamson
Quoted 29 lines · On Fri, 2 Dec 2022 10:44:30 -0400 Jason Gunthorpe <[email protected]> wrote:
Quoted 28 lines · Yi Liu
Quoted 27 lines · Jason Gunthorpe
Quoted 11 lines · Yi Liu

With the introduction of iommufd[1], VFIO is towarding to provide device centric uAPI after adapting to iommufd. With this trend, existing VFIO group infrastructure is optional once VFIO converted to device centric.

This series moves the group specific code out of vfio_main.c, prepares for compiling group infrastructure out after adding vfio device cdev[2]

Complete code in below branch:

https://github.com/yiliu1765/iommufd/commits/vfio_group_split_v1

Quoted 7 lines

This is based on Jason's "Connect VFIO to IOMMUFD"[3] and my "Make mdev driver dma_unmap callback tolerant to unmaps come before device open"[4]

[1] https://lore.kernel.org/all/[email protected]/
[2] https://github.com/yiliu1765/iommufd/tree/wip/vfio_device_cdev
[3] https://lore.kernel.org/kvm/[email protected]/
[4] https://lore.kernel.org/kvm/[email protected]/

This looks good to me, and it applies OK to my branch here:

https://git.kernel.org/pub/scm/linux/kernel/git/jgg/iommufd.git/

Alex, if you ack this in the next few days I can include it in the iommufd PR, otherwise it can go into the vfio tree in January

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

thanks. btw. I've updated my github to incorporate Kevin's nit and also r-b from you and Kevin.

Please rebase it on the above branch also

To Jason: done. Please fetch from below branch.

https://github.com/yiliu1765/iommufd/commits/for-jason/vfio_group_split

Quoted 6 lines

It looks fine to me aside from the previous review comments and my own spelling nit. I also don't see that this adds any additional conflicts vs the existing iommufd integration for any outstanding vfio patches on the list, therefore, where there's not already a sign-off from me:

To Alex: thanks. above branch is based on Jason's for-next. So may have one minor conflict with below commit in your next branch.

vfio: Remove vfio_free_device

Signature · 2 lines
Regards,
Yi Liu
#25 lore raw
Quoted 5 lines · Yi Liu
Quoted 1 line
Quoted 1 line

Please rebase it on the above branch also

To Jason: done. Please fetch from below branch.

https://github.com/yiliu1765/iommufd/commits/for-jason/vfio_group_split

Okay, it is merged in to the iommufd tree

Quoted 11 lines
Quoted 6 lines

It looks fine to me aside from the previous review comments and my own spelling nit. I also don't see that this adds any additional conflicts vs the existing iommufd integration for any outstanding vfio patches on the list, therefore, where there's not already a sign-off from me:

To Alex: thanks. above branch is based on Jason's for-next. So may have one minor conflict with below commit in your next branch.

vfio: Remove vfio_free_device

It seems OK, I did a test merge

Thanks,
Jason

Tested-by
#26 lore raw
In reply to Yi Liu
Quoted 49 lines · Yi Liu
Quoted 48 lines · Alex Williamson
Quoted 45 lines · On Fri, 2 Dec 2022 10:44:30 -0400 Jason Gunthorpe <[email protected]> wrote:
Quoted 43 lines · Yi Liu
Quoted 31 lines · Jason Gunthorpe
Quoted 26 lines · Yi Liu

With the introduction of iommufd[1], VFIO is towarding to provide device centric uAPI after adapting to iommufd. With this trend, existing VFIO group infrastructure is optional once VFIO converted to device centric.

This series moves the group specific code out of vfio_main.c, prepares for compiling group infrastructure out after adding vfio device cdev[2]

Complete code in below branch:

https://github.com/yiliu1765/iommufd/commits/vfio_group_split_v1

This is based on Jason's "Connect VFIO to IOMMUFD"[3] and my "Make mdev driver dma_unmap callback tolerant to unmaps come before device open"[4]

[1]
https://lore.kernel.org/all/0-v5-4001c2997bd0+30c-iommufd_jgg@nvi
dia.com/

[2]
https://github.com/yiliu1765/iommufd/tree/wip/vfio_device_cdev
[3]
https://lore.kernel.org/kvm/0-v4-42cd2eb0e3eb+335a-vfio_iommufd_j
[email protected]/

Quoted 3 lines

[4]
https://lore.kernel.org/kvm/20221129105831.466954-1-yi.l.liu@inte
l.com/

Quoted 8 lines

This looks good to me, and it applies OK to my branch here:

https://git.kernel.org/pub/scm/linux/kernel/git/jgg/iommufd.git/

Alex, if you ack this in the next few days I can include it in the iommufd PR, otherwise it can go into the vfio tree in January

thanks. btw. I've updated my github to incorporate Kevin's nit and also r-b from you and Kevin.

Quoted 6 lines
Quoted 1 line
Quoted 1 line

Please rebase it on the above branch also

To Jason: done. Please fetch from below branch.

https://github.com/yiliu1765/iommufd/commits/for-jason/vfio_group_spli
t

Tested NIC passthrough on Intel platform with above branch (Commit id: bf445e6747b588a774f5e45f2c9e0cdaa3140d7c). Result looks good hence,

Quoted 11 lines
Quoted 6 lines

It looks fine to me aside from the previous review comments and my own spelling nit. I also don't see that this adds any additional conflicts vs the existing iommufd integration for any outstanding vfio patches on the list, therefore, where there's not already a sign-off from me:

To Alex: thanks. above branch is based on Jason's for-next. So may have one minor conflict with below commit in your next branch.

vfio: Remove vfio_free_device

Best regards,
Lixiao Yang

Tested-by
#27 lore raw
Quoted 68 lines · Lixiao
Quoted 49 lines · Yi Liu
Quoted 48 lines · Alex Williamson
Quoted 45 lines · On Fri, 2 Dec 2022 10:44:30 -0400 Jason Gunthorpe <[email protected]> wrote:
Quoted 43 lines · Yi Liu
Quoted 31 lines · Jason Gunthorpe
Quoted 26 lines · Yi Liu

With the introduction of iommufd[1], VFIO is towarding to provide device centric uAPI after adapting to iommufd. With this trend, existing VFIO group infrastructure is optional once VFIO converted to device centric.

This series moves the group specific code out of vfio_main.c, prepares for compiling group infrastructure out after adding vfio device cdev[2]

Complete code in below branch:

https://github.com/yiliu1765/iommufd/commits/vfio_group_split_v1

This is based on Jason's "Connect VFIO to IOMMUFD"[3] and my "Make mdev driver dma_unmap callback tolerant to unmaps come before device open"[4]

[1]
https://lore.kernel.org/all/0-v5-4001c2997bd0+30c-iommufd_jgg@nvi
dia.com/

[2]
https://github.com/yiliu1765/iommufd/tree/wip/vfio_device_cdev
[3]
https://lore.kernel.org/kvm/0-v4-42cd2eb0e3eb+335a-vfio_iommufd_j
[email protected]/

Quoted 3 lines

[4]
https://lore.kernel.org/kvm/20221129105831.466954-1-yi.l.liu@inte
l.com/

Quoted 8 lines

This looks good to me, and it applies OK to my branch here:

https://git.kernel.org/pub/scm/linux/kernel/git/jgg/iommufd.git/

Alex, if you ack this in the next few days I can include it in the iommufd PR, otherwise it can go into the vfio tree in January

thanks. btw. I've updated my github to incorporate Kevin's nit and also r-b from you and Kevin.

Quoted 6 lines
Quoted 1 line
Quoted 1 line

Please rebase it on the above branch also

To Jason: done. Please fetch from below branch.

https://github.com/yiliu1765/iommufd/commits/for-jason/vfio_group_spli
t

Tested NIC passthrough on Intel platform with above branch (Commit id: bf445e6747b588a774f5e45f2c9e0cdaa3140d7c). Result looks good hence,

Quoted 6 lines
Quoted 5 lines

It looks fine to me aside from the previous review comments and my>>> own spelling nit.  I also don't see that this adds any additional conflicts vs the existing iommufd integration for any outstanding
vfio patches on the list, therefore, where there's not already a sign-off from me:

Quoted 7 lines
Quoted 4 lines

To Alex: thanks. above branch is based on Jason's for-next. So may have one minor conflict with below commit in your next branch.

vfio: Remove vfio_free_device

Best regards,
Lixiao Yang

GVT-g test passed with hybrid mode, GVT-d test passed with compat mode.

--

Best regards,
He,Yu