Skip to content

Commit 64965b8

Browse files
Alex Williamsonawilliam
authored andcommitted
vfio: replace vfio->device_class with a const struct class
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>
1 parent b0eab97 commit 64965b8

3 files changed

Lines changed: 19 additions & 20 deletions

File tree

drivers/vfio/device_cdev.c

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -293,14 +293,8 @@ int vfio_df_ioctl_detach_pt(struct vfio_device_file *df,
293293
return 0;
294294
}
295295

296-
static char *vfio_device_devnode(const struct device *dev, umode_t *mode)
296+
int vfio_cdev_init(void)
297297
{
298-
return kasprintf(GFP_KERNEL, "vfio/devices/%s", dev_name(dev));
299-
}
300-
301-
int vfio_cdev_init(struct class *device_class)
302-
{
303-
device_class->devnode = vfio_device_devnode;
304298
return alloc_chrdev_region(&device_devt, 0,
305299
MINORMASK + 1, "vfio-dev");
306300
}

drivers/vfio/vfio.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -377,7 +377,7 @@ int vfio_device_fops_cdev_open(struct inode *inode, struct file *filep);
377377
long vfio_df_ioctl_bind_iommufd(struct vfio_device_file *df,
378378
struct vfio_device_bind_iommufd __user *arg);
379379
void vfio_df_unbind_iommufd(struct vfio_device_file *df);
380-
int vfio_cdev_init(struct class *device_class);
380+
int vfio_cdev_init(void);
381381
void vfio_cdev_cleanup(void);
382382
#else
383383
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)
410410
{
411411
}
412412

413-
static inline int vfio_cdev_init(struct class *device_class)
413+
static inline int vfio_cdev_init(void)
414414
{
415415
return 0;
416416
}

drivers/vfio/vfio_main.c

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,6 @@
4949
#define VFIO_MAGIC 0x5646494f /* "VFIO" */
5050

5151
static struct vfio {
52-
struct class *device_class;
5352
struct ida device_ida;
5453
struct vfsmount *vfs_mount;
5554
int fs_count;
@@ -64,6 +63,16 @@ MODULE_PARM_DESC(enable_unsafe_noiommu_mode, "Enable UNSAFE, no-IOMMU mode. Thi
6463

6564
static DEFINE_XARRAY(vfio_device_set_xa);
6665

66+
static char *vfio_device_devnode(const struct device *dev, umode_t *mode)
67+
{
68+
return kasprintf(GFP_KERNEL, "vfio/devices/%s", dev_name(dev));
69+
}
70+
71+
static const struct class vfio_device_class = {
72+
.name = "vfio-dev",
73+
.devnode = vfio_device_devnode
74+
};
75+
6776
int vfio_assign_device_set(struct vfio_device *device, void *set_id)
6877
{
6978
unsigned long idx = (unsigned long)set_id;
@@ -299,7 +308,7 @@ static int vfio_init_device(struct vfio_device *device, struct device *dev,
299308

300309
device_initialize(&device->device);
301310
device->device.release = vfio_device_release;
302-
device->device.class = vfio.device_class;
311+
device->device.class = &vfio_device_class;
303312
device->device.parent = device->dev;
304313
return 0;
305314

@@ -1804,13 +1813,11 @@ static int __init vfio_init(void)
18041813
goto err_virqfd;
18051814

18061815
/* /sys/class/vfio-dev/vfioX */
1807-
vfio.device_class = class_create("vfio-dev");
1808-
if (IS_ERR(vfio.device_class)) {
1809-
ret = PTR_ERR(vfio.device_class);
1816+
ret = class_register(&vfio_device_class);
1817+
if (ret)
18101818
goto err_dev_class;
1811-
}
18121819

1813-
ret = vfio_cdev_init(vfio.device_class);
1820+
ret = vfio_cdev_init();
18141821
if (ret)
18151822
goto err_alloc_dev_chrdev;
18161823

@@ -1819,8 +1826,7 @@ static int __init vfio_init(void)
18191826
return 0;
18201827

18211828
err_alloc_dev_chrdev:
1822-
class_destroy(vfio.device_class);
1823-
vfio.device_class = NULL;
1829+
class_unregister(&vfio_device_class);
18241830
err_dev_class:
18251831
vfio_virqfd_exit();
18261832
err_virqfd:
@@ -1833,8 +1839,7 @@ static void __exit vfio_cleanup(void)
18331839
vfio_debugfs_remove_root();
18341840
ida_destroy(&vfio.device_ida);
18351841
vfio_cdev_cleanup();
1836-
class_destroy(vfio.device_class);
1837-
vfio.device_class = NULL;
1842+
class_unregister(&vfio_device_class);
18381843
vfio_virqfd_exit();
18391844
vfio_group_cleanup();
18401845
xa_destroy(&vfio_device_set_xa);

0 commit comments

Comments
 (0)