Move group specific code into group.c
Subsystems (2)
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_rfcv1
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]/T/#t
[4] 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: Wrap group codes to be helpers for __vfio_register_dev() and
unregister
vfio: Make vfio_device_open() group agnostic
vfio: Move device open/close code to be helpfers
vfio: Swap order of vfio_device_container_register() and open_device()
vfio: Refactor vfio_device_first_open() and _last_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 | 834 +++++++++++++++++++++++++++++++++++++ drivers/vfio/vfio.h | 52 ++- drivers/vfio/vfio_main.c | 863 +++------------------------------------ 5 files changed, 942 insertions(+), 828 deletions(-) create mode 100644 drivers/vfio/group.c
Signature · 1 line
2.34.1
RFC 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
@@ -146,10 +146,12 @@ EXPORT_SYMBOL_GPL(vfio_device_set_open_count);* 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.
@@ -163,17 +165,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);
@@ -228,6 +219,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;
@@ -240,26 +233,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;
@@ -470,7 +453,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;
@@ -519,9 +504,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
RFC 2/10 vfio: Move the sanity check of the group to vfio_create_group()
In reply to Yi LiuFrom: 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
@@ -146,7 +146,7 @@ EXPORT_SYMBOL_GPL(vfio_device_set_open_count);* 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;
@@ -157,10 +157,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 ; }
@@ -310,23 +308,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 */
@@ -470,6 +451,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;
@@ -505,9 +501,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 */
@@ -518,7 +520,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;/*
@@ -540,19 +541,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
RFC 3/10 vfio: Wrap group codes to be helpers for __vfio_register_dev() and unregister
In reply to Yi LiuThis avoids to decode group fields in the common functions used by vfio_device registration, and prepares for further moving 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
@@ -517,6 +517,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) {
@@ -555,9 +569,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:
@@ -617,7 +629,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;
@@ -645,9 +656,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
RFC 4/10 vfio: Make vfio_device_open() group agnostic
In reply to Yi LiuThis prepares for moving group specific code to separate file.
--- drivers/vfio/vfio_main.c | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-)
drivers/vfio/vfio_main.c +4 -3
@@ -878,9 +878,6 @@ static struct file *vfio_device_open(struct vfio_device *device)*/ filep->f_mode |= (FMODE_PREAD |FMODE_PWRITE ); -if (device->group->type ==VFIO_NO_IOMMU ) -dev_warn (device->dev,"vfio-noiommu device opened by user " -"(%s:%d)\n" , current->comm,task_pid_nr (current));/* * On success the ref of device is moved to the file and * put in vfio_device_fops_release()
@@ -927,6 +924,10 @@ static int vfio_group_ioctl_get_device_fd(struct vfio_group *group, goto err_put_fdno; } +if (group->type ==VFIO_NO_IOMMU ) +dev_warn (device->dev,"vfio-noiommu device opened by user " +"(%s:%d)\n" , current->comm,task_pid_nr (current)); +fd_install (fdno, filep);return fdno;
Signature · 1 line
2.34.1
RFC 5/10 vfio: Move device open/close code to be helpfers
In reply to Yi LiuThis makes the device open and close be in paired helpers.
vfio_device_open(), and vfio_device_close() handles the open_count, and calls vfio_device_first_open(), and vfio_device_last_close() when open_count condition is met. This also helps to avoid open code for device in the vfio_group_ioctl_get_device_fd(), and prepares for further moving vfio group specific code into separate file.
--- drivers/vfio/vfio_main.c | 46 +++++++++++++++++++++++++--------------- 1 file changed, 29 insertions(+), 17 deletions(-)
drivers/vfio/vfio_main.c +29 -17
@@ -846,20 +846,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
@@ -885,12 +906,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); }
@@ -918,7 +935,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;
@@ -1105,12 +1122,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
RFC 6/10 vfio: Swap order of vfio_device_container_register() and open_device()
In reply to Yi LiuThis 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 provided by mdev driver. While, in the vfio legacy mode, the registration is done by vfio_device_container_register(), and it is after open_device(). By swapping the order of vfio_device_container_register() and open_device(), the two modes have the consistent order for the DMA unmap callback registration.
This also prepares for further splitting group specific code.
--- drivers/vfio/vfio_main.c | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-)
drivers/vfio/vfio_main.c +9 -8
@@ -799,6 +799,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)
@@ -811,17 +812,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);
@@ -833,15 +834,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
RFC 7/10 vfio: Refactor vfio_device_first_open() and _last_close()
In reply to Yi LiuTo prepare for moving group specific code out from vfio_main.c.
--- drivers/vfio/vfio_main.c | 91 +++++++++++++++++++++++++++------------- 1 file changed, 63 insertions(+), 28 deletions(-)
drivers/vfio/vfio_main.c +63 -28
@@ -775,14 +775,9 @@ 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) { -int ret; - -lockdep_assert_held (&device->dev_set->lock); - -if (!try_module_get (device->dev->driver->owner)) -return -ENODEV ; +int ret =0 ;/* * Here we pass the KVM pointer with the group under the lock. If the
@@ -792,39 +787,86 @@ static int vfio_device_first_open(struct vfio_device *device)mutex_lock (&device->group->group_lock);if (!vfio_group_has_iommu (device->group)) { ret = -EINVAL ; - goto err_module_put; + goto out_unlock; }if (device->group->container) { ret =vfio_group_use_container (device->group);if (ret) - goto err_module_put; + goto out_unlock;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; +out_unlock: +mutex_unlock (&device->group->group_lock); +return ret; +} + +static void vfio_device_group_unuse_iommu (struct vfio_device *device) +{ +mutex_lock (&device->group->group_lock); +if (device->group->container) { +vfio_device_container_unregister (device); +vfio_group_unuse_container (device->group); + }else if (device->group->iommufd) { +vfio_iommufd_unbind (device); + } +mutex_unlock (&device->group->group_lock); +} + +static struct kvm *vfio_group_get_kvm (struct vfio_group *group) +{ +mutex_lock (&group->group_lock); +if (!group->kvm) { +mutex_unlock (&group->group_lock); +return NULL ; + } +/* group_lock is released in the vfio_group_put_kvm() */ +return group->kvm; +} + +static void vfio_group_put_kvm (struct vfio_group *group) +{ +mutex_unlock (&group->group_lock); +} + +static int vfio_device_first_open (struct vfio_device *device) +{ +struct kvm *kvm; +int ret; + +lockdep_assert_held (&device->dev_set->lock); + +if (!try_module_get (device->dev->driver->owner)) +return -ENODEV ; + + ret =vfio_device_group_use_iommu (device); +if (ret) + goto err_module_put; + + kvm =vfio_group_get_kvm (device->group); +if (!kvm) { + ret = -EINVAL ; + goto err_unuse_iommu; + } + + device->kvm = kvm;if (device->ops->open_device) { ret = device->ops->open_device (device);if (ret) goto err_container; } -mutex_unlock (&device->group->group_lock); +vfio_group_put_kvm (device->group);return 0 ; err_container: device->kvm =NULL ; -if (device->group->container) { -vfio_device_container_unregister (device); -vfio_group_unuse_container (device->group); - }else if (device->group->iommufd) { -vfio_iommufd_unbind (device); - } +vfio_group_put_kvm (device->group); +err_unuse_iommu: +vfio_device_group_unuse_iommu (device); err_module_put: -mutex_unlock (&device->group->group_lock);module_put (device->dev->driver->owner);return ret; }
@@ -833,17 +875,10 @@ 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->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) { -vfio_iommufd_unbind (device); - } -mutex_unlock (&device->group->group_lock); +vfio_device_group_unuse_iommu (device);module_put (device->dev->driver->owner); }
Signature · 1 line
2.34.1
RFC 8/10 vfio: Wrap vfio group module init/clean code into helpers
In reply to Yi LiuThis 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
@@ -2053,12 +2053,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);
@@ -2075,24 +2074,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:
@@ -2100,18 +2087,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
RFC 9/10 vfio: Refactor dma APIs for emulated devices
In reply to Yi LiuTo 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 | 26 +++++++++++++++----------- 3 files changed, 44 insertions(+), 34 deletions(-)
drivers/vfio/container.c +13 -7
@@ -540,11 +540,13 @@ 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_group_container_pin_pages (struct vfio_group *group, +dma_addr_t iova,int npage, +int prot,struct page **pages) { +struct vfio_container *container = group->container;struct vfio_iommu_driver *driver = container->iommu_driver; +struct iommu_group *iommu_group = group->iommu_group;if (npage >VFIO_PIN_PAGES_MAX_ENTRIES )return -E2BIG ;
@@ -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_group_container_unpin_pages (struct vfio_group *group, +dma_addr_t iova,int npage) { +struct vfio_container *container = 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_group_container_dma_rw (struct vfio_group *group, +dma_addr_t iova,void *data, +size_t len,bool write) { +struct vfio_container *container = group->container;struct vfio_iommu_driver *driver = container->iommu_driver;if (unlikely (!driver || !driver->ops->dma_rw))
drivers/vfio/vfio.h +16 -16
@@ -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_group_container_pin_pages (struct vfio_group *group, +dma_addr_t iova,int npage, +int prot,struct page **pages); +void vfio_group_container_unpin_pages (struct vfio_group *group, +dma_addr_t iova,int npage); +int vfio_group_container_dma_rw (struct vfio_group *group, +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_group_container_pin_pages (struct vfio_group *group, +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_group_container_unpin_pages (struct vfio_group *group, +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_group_container_dma_rw (struct vfio_group *group, +dma_addr_t iova,void *data, +size_t len,bool write) {return -EOPNOTSUPP ; }
drivers/vfio/vfio_main.c +15 -11
@@ -1925,6 +1925,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_group_has_container (struct vfio_group *group) +{ +return group->container; +} +/* * Pin contiguous user pages and return their associated host pages for local * domain only.
@@ -1937,7 +1942,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_group_container_pin_pages(). */ int vfio_pin_pages (struct vfio_device *device,dma_addr_t iova,int npage,int prot,struct page **pages)
@@ -1945,10 +1950,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_group_has_container (device->group)) +return vfio_group_container_pin_pages (device->group, iova, + npage, prot, pages);if (device->iommufd_access) {int ret;
@@ -1984,9 +1988,9 @@ 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_group_has_container (device->group)) { +vfio_group_container_unpin_pages (device->group, iova, + npage);return ; }if (device->iommufd_access) {
@@ -2023,9 +2027,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_group_has_container (device->group)) +return vfio_group_container_dma_rw (device->group, iova, + data, len, write);if (device->iommufd_access) {unsigned int flags =0 ;
Signature · 1 line
2.34.1
Re: [RFC 09/10] vfio: Refactor dma APIs for emulated devices
Quoted 122 lines · 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.Signed-off-by Yi Liu <[email protected]>Signed-off-by Nicolin Chen <[email protected]>--- drivers/vfio/container.c | 20 +++++++++++++------- drivers/vfio/vfio.h | 32 ++++++++++++++++---------------- drivers/vfio/vfio_main.c | 26 +++++++++++++++----------- 3 files changed, 44 insertions(+), 34 deletions(-)drivers/vfio/container.c +13
-7@@ -540,11 +540,13 @@ 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_group_container_pin_pages (struct vfio_group *group, +dma_addr_t iova,int npage, +int prot,struct page **pages) { +struct vfio_container *container = group->container;struct vfio_iommu_driver *driver = container->iommu_driver; +struct iommu_group *iommu_group = group->iommu_group;if (npage >VFIO_PIN_PAGES_MAX_ENTRIES )return -E2BIG ;@@ -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_group_container_unpin_pages (struct vfio_group *group, +dma_addr_t iova,int npage) { +struct vfio_container *container = 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_group_container_dma_rw (struct vfio_group *group, +dma_addr_t iova,void *data, +size_t len,bool write) { +struct vfio_container *container = group->container;struct vfio_iommu_driver *driver = container->iommu_driver;if (unlikely (!driver || !driver->ops->dma_rw))drivers/vfio/vfio.h +16
-16@@ -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_group_container_pin_pages (struct vfio_group *group, +dma_addr_t iova,int npage, +int prot,struct page **pages); +void vfio_group_container_unpin_pages (struct vfio_group *group, +dma_addr_t iova,int npage); +int vfio_group_container_dma_rw (struct vfio_group *group, +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_group_container_pin_pages (struct vfio_group *group, +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_group_container_unpin_pages (struct vfio_group *group, +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_group_container_dma_rw (struct vfio_group *group, +dma_addr_t iova,void *data, +size_t len,bool write) {return -EOPNOTSUPP ; }drivers/vfio/vfio_main.c +0
-0
drivers/vfio/vfio_main.c int vfio_set_irqs_validate_and_prepare(struct vfio_irq_set *hdr, int num_irqs, · line 1931
Quoted 7 lines
drivers/vfio/vfio_main.c +4
-0@@ -1925,6 +1925,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_group_has_container (struct vfio_group *group) +{ +return group->container; +}
This should probably be
vfio_device_has_container(struct vfio_device *device)
And it just returns false if the group code is compiled out
Jason
Re: [RFC 09/10] vfio: Refactor dma APIs for emulated devices
Quoted 133 lines · Jason Gunthorpe
Quoted 122 lines · 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.Signed-off-by Yi Liu <[email protected]>Signed-off-by Nicolin Chen <[email protected]>--- drivers/vfio/container.c | 20 +++++++++++++------- drivers/vfio/vfio.h | 32 ++++++++++++++++---------------- drivers/vfio/vfio_main.c | 26 +++++++++++++++----------- 3 files changed, 44 insertions(+), 34 deletions(-)drivers/vfio/container.c +13
-7@@ -540,11 +540,13 @@ 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_group_container_pin_pages (struct vfio_group *group, +dma_addr_t iova,int npage, +int prot,struct page **pages) { +struct vfio_container *container = group->container;struct vfio_iommu_driver *driver = container->iommu_driver; +struct iommu_group *iommu_group = group->iommu_group;if (npage >VFIO_PIN_PAGES_MAX_ENTRIES )return -E2BIG ;@@ -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_group_container_unpin_pages (struct vfio_group *group, +dma_addr_t iova,int npage) { +struct vfio_container *container = 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_group_container_dma_rw (struct vfio_group *group, +dma_addr_t iova,void *data, +size_t len,bool write) { +struct vfio_container *container = group->container;struct vfio_iommu_driver *driver = container->iommu_driver;if (unlikely (!driver || !driver->ops->dma_rw))drivers/vfio/vfio.h +16
-16@@ -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_group_container_pin_pages (struct vfio_group *group, +dma_addr_t iova,int npage, +int prot,struct page **pages); +void vfio_group_container_unpin_pages (struct vfio_group *group, +dma_addr_t iova,int npage); +int vfio_group_container_dma_rw (struct vfio_group *group, +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_group_container_pin_pages (struct vfio_group *group, +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_group_container_unpin_pages (struct vfio_group *group, +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_group_container_dma_rw (struct vfio_group *group, +dma_addr_t iova,void *data, +size_t len,bool write) {return -EOPNOTSUPP ; }drivers/vfio/vfio_main.c +0
-0drivers/vfio/vfio_main.c int vfio_set_irqs_validate_and_prepare(struct vfio_irq_set *hdr, int num_irqs, · line 1931
Quoted 7 lines
drivers/vfio/vfio_main.c +4
-0@@ -1925,6 +1925,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_group_has_container (struct vfio_group *group) +{ +return group->container; +}This should probably be
Quoted 3 lines
vfio_device_has_container(struct vfio_device *device)And it just returns false if the group code is compiled out
sure.
Signature · 2 lines
Regards, Yi Liu
RFC 10/10 vfio: Move vfio group specific code into group.c
In reply to Yi LiuThis prepares for compiling out vfio group after vfio device cdev is added. No vfio_group decode code should be in vfio_main.c.
--- drivers/vfio/Makefile | 1 + drivers/vfio/group.c | 834 +++++++++++++++++++++++++++++++++++++++ drivers/vfio/vfio.h | 20 + drivers/vfio/vfio_main.c | 806 +------------------------------------ 4 files changed, 858 insertions(+), 803 deletions(-) create mode 100644 drivers/vfio/group.c
drivers/vfio/Makefile +1 -0
@@ -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 +834 -0
834 lines not on this page · raw message
drivers/vfio/vfio.h +20 -0
@@ -15,6 +15,10 @@ 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); +struct file *vfio_device_open_file (struct vfio_device *device); +enum vfio_group_type {/* * Physical device with IOMMU backing.
@@ -66,6 +70,22 @@ struct vfio_group {struct iommufd_ctx *iommufd; }; +void vfio_device_remove_group (struct vfio_device *device); +struct vfio_group *vfio_noiommu_group_alloc (struct device *dev, +enum vfio_group_type type); +struct vfio_group *vfio_group_find_or_alloc (struct device *dev); +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); +struct kvm *vfio_group_get_kvm (struct vfio_group *group); +void vfio_group_put_kvm (struct vfio_group *group); +void vfio_device_group_finalize_open (struct vfio_device *device); +void vfio_device_group_abort_open (struct vfio_device *device); +bool vfio_group_has_container (struct vfio_group *group); +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 +3 -803
887 lines not on this page · raw message
Signature · 1 line
2.34.1
Re: [RFC 10/10] vfio: Move vfio group specific code into group.c
drivers/vfio/group.c line 665
Quoted 7 lines · Yi Liu
drivers/vfio/group.c new file +7
-0+void vfio_device_group_abort_open (struct vfio_device *device) +{ +mutex_lock (&device->group->group_lock); +if (device->group->container) +vfio_device_container_unregister (device); +mutex_unlock (&device->group->group_lock); +}
I'm looking at your git branch and I don't see this called?
drivers/vfio/group.c:void vfio_device_group_abort_open(struct vfio_device *device) drivers/vfio/vfio.h:void vfio_device_group_abort_open(struct vfio_device *device);
?
Jason
Re: [RFC 10/10] vfio: Move vfio group specific code into group.c
Quoted 12 lines · Jason Gunthorpe
drivers/vfio/group.c line 665
Quoted 7 lines · Yi Liu
drivers/vfio/group.c new file +7
-0+void vfio_device_group_abort_open (struct vfio_device *device) +{ +mutex_lock (&device->group->group_lock); +if (device->group->container) +vfio_device_container_unregister (device); +mutex_unlock (&device->group->group_lock); +}I'm looking at your git branch and I don't see this called?
Quoted 3 lines
drivers/vfio/group.c:void
vfio_device_group_abort_open(struct vfio_device *device)drivers/vfio/vfio.h:voidvfio_device_group_abort_open(struct vfio_device *device);
sorry, a rebase mistake, they should be removed. thanks for spotting it.
Signature · 2 lines
Regards, 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_rfcv1
Quoted 7 lines
This is based on Jason's "Connect VFIO to IOMMUFD"[3] and my "Make mdev driver
dma_unmapcallback 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]/T/#t
[4] https://lore.kernel.org/kvm/[email protected]/T/#t
I looked at this for a while, I think you should squish the below into the series too.
A good goal is to make it so we can compile out vfio_device::group entirely when group.c is disabled. This makes the compile time checking stronger (adjust the cdev patch to do this). It means removing all device->group references from vfio_main.c, which the below does:
drivers/vfio/group.c +25 -23
@@ -476,8 +476,8 @@ void vfio_device_remove_group(struct vfio_device *device)put_device (&group->dev); } -struct vfio_group *vfio_noiommu_group_alloc (struct device *dev, -enum vfio_group_type type) +static struct vfio_group *vfio_noiommu_group_alloc (struct device *dev, +enum vfio_group_type type) {struct iommu_group *iommu_group;struct vfio_group *group;
@@ -526,7 +526,7 @@ static bool vfio_group_has_device(struct vfio_group *group, struct device *dev)return false; } -struct vfio_group *vfio_group_find_or_alloc (struct device *dev) +static struct vfio_group *vfio_group_find_or_alloc (struct device *dev) {struct iommu_group *iommu_group;struct vfio_group *group;
@@ -577,6 +577,22 @@ struct vfio_group *vfio_group_find_or_alloc(struct device *dev)return group; } +int vfio_device_set_group (struct vfio_device *device,enum vfio_group_type type) +{ +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); +if (IS_ERR (group)) +return PTR_ERR (group); + +/* Our reference on group is moved to the device */ + device->group = group; +return 0 ; +} +void vfio_device_group_register (struct vfio_device *device) {mutex_lock (&device->group->device_lock);
@@ -632,8 +648,10 @@ void vfio_device_group_unuse_iommu(struct vfio_device *device)mutex_unlock (&device->group->group_lock); } -struct kvm *vfio_group_get_kvm (struct vfio_group *group) +struct kvm *vfio_device_get_group_kvm (struct vfio_device *device) { +struct vfio_group *group = device->group; +mutex_lock (&group->group_lock);if (!group->kvm) {mutex_unlock (&group->group_lock);
@@ -643,24 +661,8 @@ struct kvm *vfio_group_get_kvm(struct vfio_group *group)return group->kvm; } -void vfio_group_put_kvm (struct vfio_group *group) -{ -mutex_unlock (&group->group_lock); -} - -void vfio_device_group_finalize_open (struct vfio_device *device) +void vfio_device_put_group_kvm (struct vfio_device *device) { -mutex_lock (&device->group->group_lock); -if (device->group->container) -vfio_device_container_register (device); -mutex_unlock (&device->group->group_lock); -} - -void vfio_device_group_abort_open (struct vfio_device *device) -{ -mutex_lock (&device->group->group_lock); -if (device->group->container) -vfio_device_container_unregister (device);mutex_unlock (&device->group->group_lock); }
@@ -779,9 +781,9 @@ bool vfio_file_has_dev(struct file *file, struct vfio_device *device) }EXPORT_SYMBOL_GPL (vfio_file_has_dev); -bool vfio_group_has_container (struct vfio_group *group) +bool vfio_device_has_container (struct vfio_device *device) { -return group->container; +return device->group->container; }static char *vfio_devnode (struct device *dev,umode_t *mode)
drivers/vfio/vfio.h +16 -19
@@ -70,19 +70,16 @@ 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); -struct vfio_group *vfio_noiommu_group_alloc (struct device *dev, -enum vfio_group_type type); -struct vfio_group *vfio_group_find_or_alloc (struct device *dev);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); -struct kvm *vfio_group_get_kvm (struct vfio_group *group); -void vfio_group_put_kvm (struct vfio_group *group); -void vfio_device_group_finalize_open (struct vfio_device *device); -void vfio_device_group_abort_open (struct vfio_device *device); -bool vfio_group_has_container (struct vfio_group *group); +struct kvm *vfio_device_get_group_kvm (struct vfio_device *device); +void vfio_device_put_group_kvm (struct vfio_device *device); +bool vfio_device_has_container (struct vfio_device *device);int __init vfio_group_init (void );void vfio_group_cleanup (void );
@@ -142,12 +139,12 @@ 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_group_container_pin_pages (struct vfio_group *group, +int vfio_device_container_pin_pages (struct vfio_device *device,dma_addr_t iova,int npage,int prot,struct page **pages); -void vfio_group_container_unpin_pages (struct vfio_group *group, +void vfio_device_container_unpin_pages (struct vfio_device *device,dma_addr_t iova,int npage); -int vfio_group_container_dma_rw (struct vfio_group *group, +int vfio_device_container_dma_rw (struct vfio_device *device,dma_addr_t iova,void *data,size_t len,bool write);
@@ -187,21 +184,21 @@ static inline void vfio_device_container_unregister(struct vfio_device *device) { } -static inline int vfio_group_container_pin_pages (struct vfio_group *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_group_container_unpin_pages (struct vfio_group *group, -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_group_container_dma_rw (struct vfio_group *group, -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 +23 -31
123 lines not on this page · raw message
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_rfcv1
Quoted 7 lines
This is based on Jason's "Connect VFIO to IOMMUFD"[3] and my "Make mdev driver
dma_unmapcallback 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]/T/#t
[4] https://lore.kernel.org/kvm/[email protected]/T/#t
Quoted 8 lines
I looked at this for a while, I think you should squish the below into the series too.
A good goal is to make it so we can compile out
vfio_device::groupentirely whengroup.cis disabled. This makes the compile time checking stronger (adjust the cdev patch to do this). It means removing alldevice->groupreferences fromvfio_main.c, which the below does:
sure, I'll refine the series with below hints. btw. If no device->group reference in vfio_main.c, then we may also make struct vfio_device::group defined optionally in header file. This may be done in later cdev series.
Quoted 308 lines
drivers/vfio/group.c +25
-23@@ -476,8 +476,8 @@ void vfio_device_remove_group(struct vfio_device *device)put_device (&group->dev); } -struct vfio_group *vfio_noiommu_group_alloc (struct device *dev, -enum vfio_group_type type) +static struct vfio_group *vfio_noiommu_group_alloc (struct device *dev, +enum vfio_group_type type) {struct iommu_group *iommu_group;struct vfio_group *group;@@ -526,7 +526,7 @@ static bool vfio_group_has_device(struct vfio_group *group, struct device *dev)return false; } -struct vfio_group *vfio_group_find_or_alloc (struct device *dev) +static struct vfio_group *vfio_group_find_or_alloc (struct device *dev) {struct iommu_group *iommu_group;struct vfio_group *group;@@ -577,6 +577,22 @@ struct vfio_group *vfio_group_find_or_alloc(struct device *dev)return group; } +int vfio_device_set_group (struct vfio_device *device,enum vfio_group_type type) +{ +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); +if (IS_ERR (group)) +return PTR_ERR (group); + +/* Our reference on group is moved to the device */ + device->group = group; +return 0 ; +} +void vfio_device_group_register (struct vfio_device *device) {mutex_lock (&device->group->device_lock);@@ -632,8 +648,10 @@ void vfio_device_group_unuse_iommu(struct vfio_device *device)mutex_unlock (&device->group->group_lock); } -struct kvm *vfio_group_get_kvm (struct vfio_group *group) +struct kvm *vfio_device_get_group_kvm (struct vfio_device *device) { +struct vfio_group *group = device->group; +mutex_lock (&group->group_lock);if (!group->kvm) {mutex_unlock (&group->group_lock);@@ -643,24 +661,8 @@ struct kvm *vfio_group_get_kvm(struct vfio_group *group)return group->kvm; } -void vfio_group_put_kvm (struct vfio_group *group) -{ -mutex_unlock (&group->group_lock); -} - -void vfio_device_group_finalize_open (struct vfio_device *device) +void vfio_device_put_group_kvm (struct vfio_device *device) { -mutex_lock (&device->group->group_lock); -if (device->group->container) -vfio_device_container_register (device); -mutex_unlock (&device->group->group_lock); -} - -void vfio_device_group_abort_open (struct vfio_device *device) -{ -mutex_lock (&device->group->group_lock); -if (device->group->container) -vfio_device_container_unregister (device);mutex_unlock (&device->group->group_lock); }@@ -779,9 +781,9 @@ bool vfio_file_has_dev(struct file *file, struct vfio_device *device) }EXPORT_SYMBOL_GPL (vfio_file_has_dev); -bool vfio_group_has_container (struct vfio_group *group) +bool vfio_device_has_container (struct vfio_device *device) { -return group->container; +return device->group->container; }static char *vfio_devnode (struct device *dev,umode_t *mode)drivers/vfio/vfio.h +16
-19@@ -70,19 +70,16 @@ 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); -struct vfio_group *vfio_noiommu_group_alloc (struct device *dev, -enum vfio_group_type type); -struct vfio_group *vfio_group_find_or_alloc (struct device *dev);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); -struct kvm *vfio_group_get_kvm (struct vfio_group *group); -void vfio_group_put_kvm (struct vfio_group *group); -void vfio_device_group_finalize_open (struct vfio_device *device); -void vfio_device_group_abort_open (struct vfio_device *device); -bool vfio_group_has_container (struct vfio_group *group); +struct kvm *vfio_device_get_group_kvm (struct vfio_device *device); +void vfio_device_put_group_kvm (struct vfio_device *device); +bool vfio_device_has_container (struct vfio_device *device);int __init vfio_group_init (void );void vfio_group_cleanup (void );@@ -142,12 +139,12 @@ 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_group_container_pin_pages (struct vfio_group *group, +int vfio_device_container_pin_pages (struct vfio_device *device,dma_addr_t iova,int npage,int prot,struct page **pages); -void vfio_group_container_unpin_pages (struct vfio_group *group, +void vfio_device_container_unpin_pages (struct vfio_device *device,dma_addr_t iova,int npage); -int vfio_group_container_dma_rw (struct vfio_group *group, +int vfio_device_container_dma_rw (struct vfio_device *device,dma_addr_t iova,void *data,size_t len,bool write);@@ -187,21 +184,21 @@ static inline void vfio_device_container_unregister(struct vfio_device *device) { } -static inline int vfio_group_container_pin_pages (struct vfio_group *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_group_container_unpin_pages (struct vfio_group *group, -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_group_container_dma_rw (struct vfio_group *group, -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 +23
-31@@ -260,17 +260,10 @@ void vfio_free_device(struct vfio_device *device)EXPORT_SYMBOL_GPL (vfio_free_device);static int __vfio_register_dev (struct vfio_device *device, -struct vfio_group *group) +enum vfio_group_type type) {int ret; -/* -* 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); -if (WARN_ON (device->ops->bind_iommufd && (!device->ops->unbind_iommufd || !device->ops->attach_ioas)))@@ -283,16 +276,19 @@ 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 =device_add (&device->device); + ret =vfio_device_set_group (device, type);if (ret) - goto err_out; +return ret; + + ret =device_add (&device->device); +if (ret) { +vfio_device_remove_group (device); +return ret; + }/* Refcounting can't start until the driver calls register */ refcount_set (&device->refcount,1 );@@ -300,15 +296,12 @@ static int __vfio_register_dev(struct vfio_device *device,vfio_device_group_register (device);return 0 ; -err_out: -vfio_device_remove_group (device); -return ret; }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);@@ -318,8 +311,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);@@ -386,7 +378,7 @@ static int vfio_device_first_open(struct vfio_device *device)if (ret) goto err_module_put; - kvm =vfio_group_get_kvm (device->group); + kvm =vfio_device_get_group_kvm (device);if (!kvm) { ret = -EINVAL ; goto err_unuse_iommu;@@ -398,12 +390,12 @@ static int vfio_device_first_open(struct vfio_device *device)if (ret) goto err_container; } -vfio_group_put_kvm (device->group); +vfio_device_put_group_kvm (device);return 0 ; err_container: device->kvm =NULL ; -vfio_group_put_kvm (device->group); +vfio_device_put_group_kvm (device); err_unuse_iommu:vfio_device_group_unuse_iommu (device); err_module_put:@@ -1199,8 +1191,8 @@ 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 (vfio_group_has_container (device->group)) -return vfio_group_container_pin_pages (device->group, iova, +if (vfio_device_has_container (device)) +return vfio_device_container_pin_pages (device, iova, npage, prot, pages);if (device->iommufd_access) {int ret;@@ -1237,8 +1229,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 (vfio_group_has_container (device->group)) { -vfio_group_container_unpin_pages (device->group, iova, +if (vfio_device_has_container (device)) { +vfio_device_container_unpin_pages (device, iova, npage);return ; }@@ -1276,9 +1268,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 (vfio_group_has_container (device->group)) -return vfio_group_container_dma_rw (device->group, 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 · 2 lines
Regards, Yi Liu