]> git.ipfire.org Git - thirdparty/kernel/linux.git/commitdiff
vfio: replace vfio->device_class with a const struct class
authorAlex Williamson <alex.williamson@nvidia.com>
Fri, 17 Apr 2026 15:28:12 +0000 (09:28 -0600)
committerAlex Williamson <alex@shazbot.org>
Tue, 21 Apr 2026 18:01:21 +0000 (12:01 -0600)
The class_create() call has been deprecated in favor of class_register()
as the driver core now allows for a struct class to be in read-only
memory. Replace vfio->device_class with a const struct class and drop
the class_create() call.

Compile tested with both CONFIG_VFIO_DEVICE_CDEV on and off (and
CONFIG_VFIO on); found no errors/warns in dmesg.

Link: https://lore.kernel.org/all/2023040244-duffel-pushpin-f738@gregkh/
Suggested-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
Signed-off-by: Jori Koolstra <jkoolstra@xs4all.nl>
[Remove unused vfio_cdev_init() args]
Signed-off-by: Alex Williamson <alex.williamson@nvidia.com>
Link: https://lore.kernel.org/r/20260417152814.18026-1-alex.williamson@nvidia.com
Signed-off-by: Alex Williamson <alex@shazbot.org>
drivers/vfio/device_cdev.c
drivers/vfio/vfio.h
drivers/vfio/vfio_main.c

index 8ceca24ac136c66beec4340ce2a651c18da9edad..54abf312cf0496afcc71a4657a377fd14ffdd6fc 100644 (file)
@@ -293,14 +293,8 @@ int vfio_df_ioctl_detach_pt(struct vfio_device_file *df,
        return 0;
 }
 
-static char *vfio_device_devnode(const struct device *dev, umode_t *mode)
+int vfio_cdev_init(void)
 {
-       return kasprintf(GFP_KERNEL, "vfio/devices/%s", dev_name(dev));
-}
-
-int vfio_cdev_init(struct class *device_class)
-{
-       device_class->devnode = vfio_device_devnode;
        return alloc_chrdev_region(&device_devt, 0,
                                   MINORMASK + 1, "vfio-dev");
 }
index 0854f3fa1a22083b620e882be32d3d3a30a49fcb..e4b72e79b7e3e162d8bd35786c17399423ae2e22 100644 (file)
@@ -377,7 +377,7 @@ int vfio_device_fops_cdev_open(struct inode *inode, struct file *filep);
 long vfio_df_ioctl_bind_iommufd(struct vfio_device_file *df,
                                struct vfio_device_bind_iommufd __user *arg);
 void vfio_df_unbind_iommufd(struct vfio_device_file *df);
-int vfio_cdev_init(struct class *device_class);
+int vfio_cdev_init(void);
 void vfio_cdev_cleanup(void);
 #else
 static inline void vfio_init_device_cdev(struct vfio_device *device)
@@ -410,7 +410,7 @@ static inline void vfio_df_unbind_iommufd(struct vfio_device_file *df)
 {
 }
 
-static inline int vfio_cdev_init(struct class *device_class)
+static inline int vfio_cdev_init(void)
 {
        return 0;
 }
index 8666f35fb3f0dbbed14eb39389f3741472bd1e38..6222376ab6ab5582a875aa9524427c86e89ec58a 100644 (file)
@@ -49,7 +49,6 @@
 #define VFIO_MAGIC 0x5646494f /* "VFIO" */
 
 static struct vfio {
-       struct class                    *device_class;
        struct ida                      device_ida;
        struct vfsmount                 *vfs_mount;
        int                             fs_count;
@@ -64,6 +63,16 @@ MODULE_PARM_DESC(enable_unsafe_noiommu_mode, "Enable UNSAFE, no-IOMMU mode.  Thi
 
 static DEFINE_XARRAY(vfio_device_set_xa);
 
+static char *vfio_device_devnode(const struct device *dev, umode_t *mode)
+{
+       return kasprintf(GFP_KERNEL, "vfio/devices/%s", dev_name(dev));
+}
+
+static const struct class vfio_device_class = {
+       .name           = "vfio-dev",
+       .devnode        = vfio_device_devnode
+};
+
 int vfio_assign_device_set(struct vfio_device *device, void *set_id)
 {
        unsigned long idx = (unsigned long)set_id;
@@ -299,7 +308,7 @@ static int vfio_init_device(struct vfio_device *device, struct device *dev,
 
        device_initialize(&device->device);
        device->device.release = vfio_device_release;
-       device->device.class = vfio.device_class;
+       device->device.class = &vfio_device_class;
        device->device.parent = device->dev;
        return 0;
 
@@ -1804,13 +1813,11 @@ static int __init vfio_init(void)
                goto err_virqfd;
 
        /* /sys/class/vfio-dev/vfioX */
-       vfio.device_class = class_create("vfio-dev");
-       if (IS_ERR(vfio.device_class)) {
-               ret = PTR_ERR(vfio.device_class);
+       ret = class_register(&vfio_device_class);
+       if (ret)
                goto err_dev_class;
-       }
 
-       ret = vfio_cdev_init(vfio.device_class);
+       ret = vfio_cdev_init();
        if (ret)
                goto err_alloc_dev_chrdev;
 
@@ -1819,8 +1826,7 @@ static int __init vfio_init(void)
        return 0;
 
 err_alloc_dev_chrdev:
-       class_destroy(vfio.device_class);
-       vfio.device_class = NULL;
+       class_unregister(&vfio_device_class);
 err_dev_class:
        vfio_virqfd_exit();
 err_virqfd:
@@ -1833,8 +1839,7 @@ static void __exit vfio_cleanup(void)
        vfio_debugfs_remove_root();
        ida_destroy(&vfio.device_ida);
        vfio_cdev_cleanup();
-       class_destroy(vfio.device_class);
-       vfio.device_class = NULL;
+       class_unregister(&vfio_device_class);
        vfio_virqfd_exit();
        vfio_group_cleanup();
        xa_destroy(&vfio_device_set_xa);