[PATCH] vfio: replace vfio->class with a const struct class

From: Jori Koolstra

Date: Fri Mar 06 2026 - 14:08:27 EST


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->class with a const struct class and drop the
class_create() call.

Compile tested and found no errors/warns in dmesg after enabling
VFIO_GROUP.

Link: https://lore.kernel.org/all/2023040244-duffel-pushpin-f738@gregkh/

Suggested-by: Greg Kroah-Hartman <gregkh@xxxxxxxxxxxxxxxxxxx>
Signed-off-by: Jori Koolstra <jkoolstra@xxxxxxxxx>
---
drivers/vfio/group.c | 23 +++++++++++------------
1 file changed, 11 insertions(+), 12 deletions(-)

diff --git a/drivers/vfio/group.c b/drivers/vfio/group.c
index 4f15016d2a5f..d4a53f8c5be0 100644
--- a/drivers/vfio/group.c
+++ b/drivers/vfio/group.c
@@ -15,8 +15,13 @@
#include <linux/anon_inodes.h>
#include "vfio.h"

+static char *vfio_devnode(const struct device *, umode_t *);
+static const struct class vfio_class = {
+ .name = "vfio",
+ .devnode = vfio_devnode
+};
+
static struct vfio {
- struct class *class;
struct list_head group_list;
struct mutex group_lock; /* locks group_list */
struct ida group_ida;
@@ -527,7 +532,7 @@ static struct vfio_group *vfio_group_alloc(struct iommu_group *iommu_group,

device_initialize(&group->dev);
group->dev.devt = MKDEV(MAJOR(vfio.group_devt), minor);
- group->dev.class = vfio.class;
+ group->dev.class = &vfio_class;
group->dev.release = vfio_group_release;
cdev_init(&group->cdev, &vfio_group_fops);
group->cdev.owner = THIS_MODULE;
@@ -901,13 +906,9 @@ int __init vfio_group_init(void)
return ret;

/* /dev/vfio/$GROUP */
- vfio.class = class_create("vfio");
- if (IS_ERR(vfio.class)) {
- ret = PTR_ERR(vfio.class);
+ ret = class_register(&vfio_class);
+ if (ret)
goto err_group_class;
- }
-
- vfio.class->devnode = vfio_devnode;

ret = alloc_chrdev_region(&vfio.group_devt, 0, MINORMASK + 1, "vfio");
if (ret)
@@ -915,8 +916,7 @@ int __init vfio_group_init(void)
return 0;

err_alloc_chrdev:
- class_destroy(vfio.class);
- vfio.class = NULL;
+ class_unregister(&vfio_class);
err_group_class:
vfio_container_cleanup();
return ret;
@@ -927,7 +927,6 @@ void vfio_group_cleanup(void)
WARN_ON(!list_empty(&vfio.group_list));
ida_destroy(&vfio.group_ida);
unregister_chrdev_region(vfio.group_devt, MINORMASK + 1);
- class_destroy(vfio.class);
- vfio.class = NULL;
+ class_unregister(&vfio_class);
vfio_container_cleanup();
}

base-commit: d466c332e106fe666d1e2f5a24d08e308bebbfa1
--
2.53.0