From 06ed87b3197e9b80dccac0b764fdffe44192e010 Mon Sep 17 00:00:00 2001 From: Jori Koolstra Date: Sun, 8 Mar 2026 22:49:39 +0100 Subject: vfio: mdev: replace mtty_dev->vd_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 mtty_dev->vd_class with a const struct class and drop the class_create() call. Compile tested and found no errors/warns in dmesg after enabling CONFIG_VFIO and CONFIG_SAMPLE_VFIO_MDEV_MTTY. Link: https://lore.kernel.org/all/2023040244-duffel-pushpin-f738@gregkh/ Suggested-by: Greg Kroah-Hartman Signed-off-by: Jori Koolstra Acked-by: Greg Kroah-Hartman Link: https://lore.kernel.org/r/20260308214939.1215682-1-jkoolstra@xs4all.nl Signed-off-by: Alex Williamson --- samples/vfio-mdev/mtty.c | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) (limited to 'samples') diff --git a/samples/vfio-mdev/mtty.c b/samples/vfio-mdev/mtty.c index bd92c38379b8..01a9db84c4ab 100644 --- a/samples/vfio-mdev/mtty.c +++ b/samples/vfio-mdev/mtty.c @@ -68,9 +68,12 @@ * Global Structures */ +static const struct class mtty_class = { + .name = MTTY_CLASS_NAME +}; + static struct mtty_dev { dev_t vd_devt; - struct class *vd_class; struct cdev vd_cdev; struct idr vd_idr; struct device dev; @@ -1980,15 +1983,14 @@ static int __init mtty_dev_init(void) if (ret) goto err_cdev; - mtty_dev.vd_class = class_create(MTTY_CLASS_NAME); + ret = class_register(&mtty_class); - if (IS_ERR(mtty_dev.vd_class)) { + if (ret) { pr_err("Error: failed to register mtty_dev class\n"); - ret = PTR_ERR(mtty_dev.vd_class); goto err_driver; } - mtty_dev.dev.class = mtty_dev.vd_class; + mtty_dev.dev.class = &mtty_class; mtty_dev.dev.release = mtty_device_release; dev_set_name(&mtty_dev.dev, "%s", MTTY_NAME); @@ -2007,7 +2009,7 @@ err_device: device_del(&mtty_dev.dev); err_put: put_device(&mtty_dev.dev); - class_destroy(mtty_dev.vd_class); + class_unregister(&mtty_class); err_driver: mdev_unregister_driver(&mtty_driver); err_cdev: @@ -2026,8 +2028,7 @@ static void __exit mtty_dev_exit(void) mdev_unregister_driver(&mtty_driver); cdev_del(&mtty_dev.vd_cdev); unregister_chrdev_region(mtty_dev.vd_devt, MINORMASK + 1); - class_destroy(mtty_dev.vd_class); - mtty_dev.vd_class = NULL; + class_unregister(&mtty_class); pr_info("mtty_dev: Unloaded!\n"); } -- cgit v1.2.3 From c995498636c704641c9e809c31b59445b48f7adc Mon Sep 17 00:00:00 2001 From: Yishai Hadas Date: Tue, 17 Mar 2026 18:17:50 +0200 Subject: vfio: Adapt drivers to use the core helper vfio_check_precopy_ioctl Introduce a core helper function for VFIO_MIG_GET_PRECOPY_INFO and adapt all drivers to use it. It centralizes the common code and ensures that output flags are cleared on entry, in case user opts in to VFIO_DEVICE_FEATURE_MIG_PRECOPY_INFOv2. This preventing any unintended echoing of userspace data back to userspace. Signed-off-by: Yishai Hadas Link: https://lore.kernel.org/r/20260317161753.18964-4-yishaih@nvidia.com Signed-off-by: Alex Williamson --- samples/vfio-mdev/mtty.c | 16 +++++----------- 1 file changed, 5 insertions(+), 11 deletions(-) (limited to 'samples') diff --git a/samples/vfio-mdev/mtty.c b/samples/vfio-mdev/mtty.c index 01a9db84c4ab..69b6d9defbce 100644 --- a/samples/vfio-mdev/mtty.c +++ b/samples/vfio-mdev/mtty.c @@ -840,18 +840,11 @@ static long mtty_precopy_ioctl(struct file *filp, unsigned int cmd, struct mdev_state *mdev_state = migf->mdev_state; loff_t *pos = &filp->f_pos; struct vfio_precopy_info info = {}; - unsigned long minsz; int ret; - if (cmd != VFIO_MIG_GET_PRECOPY_INFO) - return -ENOTTY; - - minsz = offsetofend(struct vfio_precopy_info, dirty_bytes); - - if (copy_from_user(&info, (void __user *)arg, minsz)) - return -EFAULT; - if (info.argsz < minsz) - return -EINVAL; + ret = vfio_check_precopy_ioctl(&mdev_state->vdev, cmd, arg, &info); + if (ret) + return ret; mutex_lock(&mdev_state->state_mutex); if (mdev_state->state != VFIO_DEVICE_STATE_PRE_COPY && @@ -878,7 +871,8 @@ static long mtty_precopy_ioctl(struct file *filp, unsigned int cmd, info.initial_bytes = migf->filled_size - *pos; mutex_unlock(&migf->lock); - ret = copy_to_user((void __user *)arg, &info, minsz) ? -EFAULT : 0; + ret = copy_to_user((void __user *)arg, &info, + offsetofend(struct vfio_precopy_info, dirty_bytes)) ? -EFAULT : 0; unlock: mtty_state_mutex_unlock(mdev_state); return ret; -- cgit v1.2.3