vfio: Split the container code into a clean layer and dedicated file
Subsystems (2)
v3 cover vfio: Split the container code into a clean layer and dedicated file
This creates an isolated layer around the container FD code and everything under it, including the VFIO iommu drivers. All this code is placed into container.c, along with the "struct vfio_container" to compartmentalize it.
Future patches will provide an iommufd based layer that gives the same API as the container layer and choose which layer to go to based on how userspace operates.
The patches continue to split up existing functions and finally the last patch just moves every function that is a "container" function to the new file and creates the global symbols to link them together.
Cross-file container functions are prefixed with vfio_container_* for clarity.
The last patch can be defered and queued during the merge window to manage conflicts. The earlier patches should be fine immediately conflicts wise.
This is the last big series I have to enable basic iommufd functionality. As part of the iommufd series the entire container.c becomes conditionally compiled:
https://github.com/jgunthorpe/linux/commits/vfio_iommufd
v3:
- Rebase over the vfio struct device series
v2: https://lore.kernel.org/r/[email protected]
- Rename s/vfio_container_detatch_group/vfio_group_detach_container/ s/vfio_container_register_device/vfio_device_container_register/ s/vfio_container_unregister_device/vfio_device_container_unregister/
- Change argument order of vfio_container_attach_group()
- Rebased onto merged patches
v1: https://lore.kernel.org/r/[email protected]
Jason Gunthorpe (8):
vfio: Add header guards and includes to drivers/vfio/vfio.h
vfio: Rename __vfio_group_unset_container()
vfio: Split the container logic into vfio_container_attach_group()
vfio: Remove #ifdefs around CONFIG_VFIO_NOIOMMU
vfio: Split out container code from the init/cleanup functions
vfio: Rename vfio_ioctl_check_extension()
vfio: Split the register_device ops call into functions
vfio: Move container code into drivers/vfio/container.c
drivers/vfio/Makefile | 1 + drivers/vfio/container.c | 680 +++++++++++++++++++++++++++++++++++++ drivers/vfio/vfio.h | 56 ++++ drivers/vfio/vfio_main.c | 708 ++------------------------------------- 4 files changed, 765 insertions(+), 680 deletions(-) create mode 100644 drivers/vfio/container.c
base-commit: 3c28a76124b25882411f005924be73795b6ef078
Signature · 1 line
2.37.3
PATCH v3 1/8 vfio: Add header guards and includes to drivers/vfio/vfio.h
As is normal for headers.
--- drivers/vfio/vfio.h | 10 ++++++++++ 1 file changed, 10 insertions(+)
drivers/vfio/vfio.h +10 -0
@@ -3,6 +3,14 @@* Copyright (C) 2012 Red Hat, Inc. All rights reserved. * Author: Alex Williamson <[email protected]> */ +#ifndef __VFIO_VFIO_H__ +#define __VFIO_VFIO_H__ + +#include <linux/device.h> +#include <linux/cdev.h> +#include <linux/module.h> + +struct iommu_group ; enum vfio_group_type {/*
@@ -69,3 +77,5 @@ struct vfio_iommu_driver_ops {int vfio_register_iommu_driver (const struct vfio_iommu_driver_ops *ops);void vfio_unregister_iommu_driver (const struct vfio_iommu_driver_ops *ops); + +#endif
Signature · 1 line
2.37.3
PATCH v3 2/8 vfio: Rename __vfio_group_unset_container()
In reply to Jason GunthorpeTo vfio_group_detach_container(). This function is really a container function.
Fold the WARN_ON() into it as a precondition assertion.
A following patch will move the vfio_container functions to their own .c file.
--- drivers/vfio/vfio_main.c | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-)
drivers/vfio/vfio_main.c +5 -6
@@ -1036,12 +1036,13 @@ static const struct file_operations vfio_fops = {/* * VFIO Group fd, /dev/vfio/$GROUP */ -static void __vfio_group_unset_container (struct vfio_group *group) +static void vfio_group_detach_container (struct vfio_group *group) {struct vfio_container *container = group->container;struct vfio_iommu_driver *driver;lockdep_assert_held_write (&group->group_rwsem); +WARN_ON (group->container_users !=1 );down_write (&container->group_lock);
@@ -1089,7 +1090,7 @@ static int vfio_group_ioctl_unset_container(struct vfio_group *group) ret = -EBUSY ; goto out_unlock; } -__vfio_group_unset_container (group); +vfio_group_detach_container (group); out_unlock:up_write (&group->group_rwsem);
@@ -1441,10 +1442,8 @@ static int vfio_group_fops_release(struct inode *inode, struct file *filep)* is only called when there are no open devices. */ WARN_ON (group->notifier.head); -if (group->container) { -WARN_ON (group->container_users !=1 ); -__vfio_group_unset_container (group); - } +if (group->container) +vfio_group_detach_container (group); group->opened_file =NULL ;up_write (&group->group_rwsem);
Signature · 1 line
2.37.3
PATCH v3 3/8 vfio: Split the container logic into vfio_container_attach_group()
In reply to Jason GunthorpeThis splits up the ioctl of vfio_group_ioctl_set_container() so it determines the type of file then invokes a type specific attachment function. Future patches will add iommufd to this function as an alternative type.
A following patch will move the vfio_container functions to their own .c file.
--- drivers/vfio/vfio_main.c | 78 ++++++++++++++++++++++++---------------- 1 file changed, 48 insertions(+), 30 deletions(-)
drivers/vfio/vfio_main.c +48 -30
@@ -1097,40 +1097,29 @@ static int vfio_group_ioctl_unset_container(struct vfio_group *group)return ret; } -static int vfio_group_ioctl_set_container (struct vfio_group *group, -int __user *arg) +static struct vfio_container *vfio_container_from_file (struct file *file) { -struct fd f;struct vfio_container *container; -struct vfio_iommu_driver *driver; -int container_fd; -int ret =0 ; - -if (group->type ==VFIO_NO_IOMMU && !capable (CAP_SYS_RAWIO )) -return -EPERM ; - -if (get_user (container_fd, arg)) -return -EFAULT ; -if (container_fd <0 ) -return -EINVAL ; - f =fdget (container_fd); -if (!f.file) -return -EBADF ;/* Sanity check, is this really our fd? */ -if (f.file->f_op != &vfio_fops) { - ret = -EINVAL ; - goto out_fdput; - } - container = f.file->private_data; +if (file->f_op != &vfio_fops) +return NULL ; + + container = file->private_data;WARN_ON (!container);/* fget ensures we don't race vfio_release */ +return container; +} -down_write (&group->group_rwsem); +static int vfio_container_attach_group (struct vfio_container *container, +struct vfio_group *group) +{ +struct vfio_iommu_driver *driver; +int ret =0 ; -if (group->container ||WARN_ON (group->container_users)) { - ret = -EINVAL ; - goto out_unlock_group; - } +lockdep_assert_held_write (&group->group_rwsem); + +if (group->type ==VFIO_NO_IOMMU && !capable (CAP_SYS_RAWIO )) +return -EPERM ;down_write (&container->group_lock);
@@ -1142,7 +1131,7 @@ static int vfio_group_ioctl_set_container(struct vfio_group *group, }if (group->type ==VFIO_IOMMU ) { - ret =iommu_group_claim_dma_owner (group->iommu_group, f.file); + ret =iommu_group_claim_dma_owner (group->iommu_group, group);if (ret) goto out_unlock_container; }
@@ -1170,9 +1159,38 @@ static int vfio_group_ioctl_set_container(struct vfio_group *group, out_unlock_container:up_write (&container->group_lock); -out_unlock_group: +return ret; +} + +static int vfio_group_ioctl_set_container (struct vfio_group *group, +int __user *arg) +{ +struct vfio_container *container; +struct fd f; +int ret; +int fd; + +if (get_user (fd, arg)) +return -EFAULT ; + + f =fdget (fd); +if (!f.file) +return -EBADF ; + +down_write (&group->group_rwsem); +if (group->container ||WARN_ON (group->container_users)) { + ret = -EINVAL ; + goto out_unlock; + } + container =vfio_container_from_file (f.file); + ret = -EINVAL ; +if (container) { + ret =vfio_container_attach_group (container, group); + goto out_unlock; + } + +out_unlock:up_write (&group->group_rwsem); -out_fdput:fdput (f);return ret; }
Signature · 1 line
2.37.3
PATCH v3 4/8 vfio: Remove #ifdefs around CONFIG_VFIO_NOIOMMU
In reply to Jason GunthorpeThis can all be accomplished using typical IS_ENABLED techniques, drop it all.
Also rename the variable to vfio_noiommu so this can be made global in following patches.
--- drivers/vfio/vfio_main.c | 43 ++++++++++++++++++---------------------- 1 file changed, 19 insertions(+), 24 deletions(-)
drivers/vfio/vfio_main.c +19 -24
@@ -86,10 +86,12 @@ struct vfio_group { };#ifdef CONFIG_VFIO_NOIOMMU -static bool noiommu__read_mostly ; +static bool vfio_noiommu__read_mostly ;module_param_named (enable_unsafe_noiommu_mode, - noiommu, bool,S_IRUGO |S_IWUSR ); + vfio_noiommu, bool,S_IRUGO |S_IWUSR );MODULE_PARM_DESC (enable_unsafe_noiommu_mode,"Enable UNSAFE, no-IOMMU mode. This mode provides no device isolation, no DMA translation, no host kernel protection, cannot be used for device assignment to virtual machines, requires RAWIO permissions, and will taint the kernel. If you do not know what this is for, step away. (default: false)" ); +#else +enum { vfio_noiommu = false };#endif static DEFINE_XARRAY (vfio_device_set_xa);
@@ -166,7 +168,6 @@ static void vfio_release_device_set(struct vfio_device *device)xa_unlock (&vfio_device_set_xa); } -#ifdef CONFIG_VFIO_NOIOMMU static void *vfio_noiommu_open (unsigned long arg) {if (arg !=VFIO_NOIOMMU_IOMMU )
@@ -185,7 +186,7 @@ static long vfio_noiommu_ioctl(void *iommu_data, unsigned int cmd, unsigned long arg) {if (cmd ==VFIO_CHECK_EXTENSION ) -return noiommu && (arg ==VFIO_NOIOMMU_IOMMU ) ?1 :0 ; +return vfio_noiommu && (arg ==VFIO_NOIOMMU_IOMMU ) ?1 :0 ;return -ENOTTY ; }
@@ -215,18 +216,13 @@ static const struct vfio_iommu_driver_ops vfio_noiommu_ops = {* Only noiommu containers can use vfio-noiommu and noiommu containers can only * use vfio-noiommu. */ -static inline bool vfio_iommu_driver_allowed (struct vfio_container *container, -const struct vfio_iommu_driver *driver) +static bool vfio_iommu_driver_allowed (struct vfio_container *container, +const struct vfio_iommu_driver *driver) { +if (!IS_ENABLED (CONFIG_VFIO_NOIOMMU )) +return true;return container->noiommu == (driver->ops == &vfio_noiommu_ops); } -#else -static inline bool vfio_iommu_driver_allowed (struct vfio_container *container, -const struct vfio_iommu_driver *driver) -{ -return true; -} -#endif /* CONFIG_VFIO_NOIOMMU */ /* * IOMMU driver registration
@@ -630,8 +626,7 @@ static struct vfio_group *vfio_group_find_or_alloc(struct device *dev)struct vfio_group *group; iommu_group =iommu_group_get (dev); -#ifdef CONFIG_VFIO_NOIOMMU -if (!iommu_group && noiommu) { +if (!iommu_group && vfio_noiommu) {/* * With noiommu enabled, create an IOMMU group for devices that * don't already have one, implying no IOMMU hardware/driver
@@ -645,7 +640,7 @@ static struct vfio_group *vfio_group_find_or_alloc(struct device *dev) }return group; } -#endif +if (!iommu_group)return ERR_PTR (-EINVAL );
@@ -2439,11 +2434,11 @@ static int __init vfio_init(void)if (ret) goto err_alloc_chrdev; -#ifdef CONFIG_VFIO_NOIOMMU - ret =vfio_register_iommu_driver (&vfio_noiommu_ops); -#endif -if (ret) - goto err_driver_register; +if (IS_ENABLED (CONFIG_VFIO_NOIOMMU )) { + ret =vfio_register_iommu_driver (&vfio_noiommu_ops); +if (ret) + goto err_driver_register; + }pr_info (DRIVER_DESC " version: " DRIVER_VERSION "\n" );return 0 ;
@@ -2465,9 +2460,9 @@ static void __exit vfio_cleanup(void) {WARN_ON (!list_empty (&vfio.group_list)); -#ifdef CONFIG_VFIO_NOIOMMU -vfio_unregister_iommu_driver (&vfio_noiommu_ops); -#endif +if (IS_ENABLED (CONFIG_VFIO_NOIOMMU )) +vfio_unregister_iommu_driver (&vfio_noiommu_ops); +ida_destroy (&vfio.device_ida);ida_destroy (&vfio.group_ida);unregister_chrdev_region (vfio.group_devt,MINORMASK +1 );
Signature · 1 line
2.37.3
PATCH v3 5/8 vfio: Split out container code from the init/cleanup functions
In reply to Jason GunthorpeThis miscdev, noiommu driver and a couple of globals are all container items. Move this init into its own functions.
A following patch will move the vfio_container functions to their own .c file.
--- drivers/vfio/vfio_main.c | 54 ++++++++++++++++++++++++++-------------- 1 file changed, 36 insertions(+), 18 deletions(-)
drivers/vfio/vfio_main.c +36 -18
@@ -2397,15 +2397,11 @@ static struct miscdevice vfio_dev = { .mode = S_IRUGO | S_IWUGO, }; -static int __init vfio_init (void ) +static int __init vfio_container_init (void ) {int ret; -ida_init (&vfio.group_ida); -ida_init (&vfio.device_ida); -mutex_init (&vfio.group_lock);mutex_init (&vfio.iommu_drivers_lock); -INIT_LIST_HEAD (&vfio.group_list);INIT_LIST_HEAD (&vfio.iommu_drivers_list); ret =misc_register (&vfio_dev);
@@ -2414,6 +2410,39 @@ static int __init vfio_init(void)return ret; } +if (IS_ENABLED (CONFIG_VFIO_NOIOMMU )) { + ret =vfio_register_iommu_driver (&vfio_noiommu_ops); +if (ret) + goto err_misc; + } +return 0 ; + +err_misc: +misc_deregister (&vfio_dev); +return ret; +} + +static void vfio_container_cleanup (void ) +{ +if (IS_ENABLED (CONFIG_VFIO_NOIOMMU )) +vfio_unregister_iommu_driver (&vfio_noiommu_ops); +misc_deregister (&vfio_dev); +mutex_destroy (&vfio.iommu_drivers_lock); +} + +static int __init vfio_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); + + ret =vfio_container_init (); +if (ret) +return ret; +/* /dev/vfio/$GROUP */ vfio.class =class_create (THIS_MODULE ,"vfio" );if (IS_ERR (vfio.class)) {
@@ -2434,17 +2463,9 @@ static int __init vfio_init(void)if (ret) goto err_alloc_chrdev; -if (IS_ENABLED (CONFIG_VFIO_NOIOMMU )) { - ret =vfio_register_iommu_driver (&vfio_noiommu_ops); -if (ret) - goto err_driver_register; - } -pr_info (DRIVER_DESC " version: " DRIVER_VERSION "\n" );return 0 ; -err_driver_register: -unregister_chrdev_region (vfio.group_devt,MINORMASK +1 ); err_alloc_chrdev:class_destroy (vfio.device_class); vfio.device_class =NULL ;
@@ -2452,7 +2473,7 @@ static int __init vfio_init(void)class_destroy (vfio.class); vfio.class =NULL ; err_group_class: -misc_deregister (&vfio_dev); +vfio_container_cleanup ();return ret; }
@@ -2460,17 +2481,14 @@ static void __exit vfio_cleanup(void) {WARN_ON (!list_empty (&vfio.group_list)); -if (IS_ENABLED (CONFIG_VFIO_NOIOMMU )) -vfio_unregister_iommu_driver (&vfio_noiommu_ops); -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 ; -misc_deregister (&vfio_dev);xa_destroy (&vfio_device_set_xa); }
Signature · 1 line
2.37.3
PATCH v3 6/8 vfio: Rename vfio_ioctl_check_extension()
In reply to Jason GunthorpeTo vfio_container_ioctl_check_extension().
A following patch will turn this into a non-static function, make it clear it is related to the container.
--- drivers/vfio/vfio_main.c | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-)
drivers/vfio/vfio_main.c +6 -5
@@ -813,8 +813,9 @@ EXPORT_SYMBOL_GPL(vfio_unregister_group_dev);/* * VFIO base fd, /dev/vfio/vfio */ -static long vfio_ioctl_check_extension (struct vfio_container *container, -unsigned long arg) +static long +vfio_container_ioctl_check_extension (struct vfio_container *container, +unsigned long arg) {struct vfio_iommu_driver *driver;long ret =0 ;
@@ -971,7 +972,7 @@ static long vfio_fops_unl_ioctl(struct file *filep, ret =VFIO_API_VERSION ;break ;case VFIO_CHECK_EXTENSION : - ret =vfio_ioctl_check_extension (container, arg); + ret =vfio_container_ioctl_check_extension (container, arg);break ;case VFIO_SET_IOMMU : ret =vfio_ioctl_set_iommu (container, arg);
@@ -2100,8 +2101,8 @@ bool vfio_file_enforced_coherent(struct file *file)down_read (&group->group_rwsem);if (group->container) { - ret =vfio_ioctl_check_extension (group->container, -VFIO_DMA_CC_IOMMU ); + ret =vfio_container_ioctl_check_extension (group->container, +VFIO_DMA_CC_IOMMU ); }else {/* * Since the coherency state is determined only once a container
Signature · 1 line
2.37.3
PATCH v3 7/8 vfio: Split the register_device ops call into functions
In reply to Jason GunthorpeThis is a container item.
A following patch will move the vfio_container functions to their own .c file.
--- drivers/vfio/vfio_main.c | 39 +++++++++++++++++++++++---------------- 1 file changed, 23 insertions(+), 16 deletions(-)
drivers/vfio/vfio_main.c +23 -16
@@ -1226,9 +1226,28 @@ static void vfio_device_unassign_container(struct vfio_device *device)up_write (&device->group->group_rwsem); } +static void vfio_device_container_register (struct vfio_device *device) +{ +struct vfio_iommu_driver *iommu_driver = + device->group->container->iommu_driver; + +if (iommu_driver && iommu_driver->ops->register_device) + iommu_driver->ops->register_device ( + device->group->container->iommu_data, device); +} + +static void vfio_device_container_unregister (struct vfio_device *device) +{ +struct vfio_iommu_driver *iommu_driver = + device->group->container->iommu_driver; + +if (iommu_driver && iommu_driver->ops->unregister_device) + iommu_driver->ops->unregister_device ( + device->group->container->iommu_data, device); +} +static struct file *vfio_device_open (struct vfio_device *device) { -struct vfio_iommu_driver *iommu_driver;struct file *filep;int ret;
@@ -1259,12 +1278,7 @@ static struct file *vfio_device_open(struct vfio_device *device)if (ret) goto err_undo_count; } - - iommu_driver = device->group->container->iommu_driver; -if (iommu_driver && iommu_driver->ops->register_device) - iommu_driver->ops->register_device ( - device->group->container->iommu_data, device); - +vfio_device_container_register (device);up_read (&device->group->group_rwsem); }mutex_unlock (&device->dev_set->lock);
@@ -1302,10 +1316,7 @@ static struct file *vfio_device_open(struct vfio_device *device)if (device->open_count ==1 && device->ops->close_device) { device->ops->close_device (device); - iommu_driver = device->group->container->iommu_driver; -if (iommu_driver && iommu_driver->ops->unregister_device) - iommu_driver->ops->unregister_device ( - device->group->container->iommu_data, device); +vfio_device_container_unregister (device); } err_undo_count:up_read (&device->group->group_rwsem);
@@ -1513,7 +1524,6 @@ static inline void vfio_device_pm_runtime_put(struct vfio_device *device)static int vfio_device_fops_release (struct inode *inode,struct file *filep) {struct vfio_device *device = filep->private_data; -struct vfio_iommu_driver *iommu_driver;mutex_lock (&device->dev_set->lock);vfio_assert_device_open (device);
@@ -1521,10 +1531,7 @@ static int vfio_device_fops_release(struct inode *inode, struct file *filep)if (device->open_count ==1 && device->ops->close_device) device->ops->close_device (device); - iommu_driver = device->group->container->iommu_driver; -if (iommu_driver && iommu_driver->ops->unregister_device) - iommu_driver->ops->unregister_device ( - device->group->container->iommu_data, device); +vfio_device_container_unregister (device);up_read (&device->group->group_rwsem); device->open_count--;if (device->open_count ==0 )
Signature · 1 line
2.37.3
PATCH v3 8/8 vfio: Move container code into drivers/vfio/container.c
In reply to Jason GunthorpeAll the functions that dereference struct vfio_container are moved into container.c.
Simple code motion, no functional change.
--- drivers/vfio/Makefile | 1 + drivers/vfio/container.c | 680 ++++++++++++++++++++++++++++++++++++++ drivers/vfio/vfio.h | 46 +++ drivers/vfio/vfio_main.c | 692 +-------------------------------------- 4 files changed, 728 insertions(+), 691 deletions(-) create mode 100644 drivers/vfio/container.c
drivers/vfio/Makefile +1 -0
@@ -5,6 +5,7 @@ obj-$(CONFIG_VFIO) += vfio.o vfio-y += vfio_main.o \ iova_bitmap.o \ + container.o obj-$(CONFIG_VFIO_VIRQFD) += vfio_virqfd.o obj-$(CONFIG_VFIO_IOMMU_TYPE1) += vfio_iommu_type1.o
drivers/vfio/container.c new file +680 -0
680 lines not on this page · raw message
drivers/vfio/vfio.h +46 -0
65 lines not on this page · raw message
drivers/vfio/vfio_main.c +1 -691
749 lines not on this page · raw message
Signature · 1 line
2.37.3
Alex Williamson
AppliedQuoted 19 lines · On Thu, 22 Sep 2022 16:20:18 -0300 Jason Gunthorpe <[email protected]> wrote:
This creates an isolated layer around the container FD code and everything under it, including the VFIO iommu drivers. All this code is placed into
container.c, along with the "structvfio_container" to compartmentalize it.Future patches will provide an iommufd based layer that gives the same API as the container layer and choose which layer to go to based on how userspace operates.
The patches continue to split up existing functions and finally the last patch just moves every function that is a "container" function to the new file and creates the global symbols to link them together.
Cross-file container functions are prefixed with vfio_container_* for clarity.
The last patch can be defered and queued during the merge window to manage conflicts. The earlier patches should be fine immediately conflicts wise.
Quoted 8 lines
This is the last big series I have to enable basic iommufd functionality. As part of the iommufd series the entire
container.cbecomes conditionally compiled:https://github.com/jgunthorpe/linux/commits/vfio_iommufd
v3:
- Rebase over the vfio struct device series
Applied to vfio next branch for v6.1. Thanks,
Alex