Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

vfio/mdev: consolidate all the description sysfs into the core code

Every driver just emits a string, simply add a method to the mdev_driver
to return it and provide a standard sysfs show function.

Remove the now unused types_attrs field in struct mdev_driver and the
support code for it.

Signed-off-by: Christoph Hellwig <hch@lst.de>
Reviewed-by: Jason Gunthorpe <jgg@nvidia.com>
Reviewed-by: Kevin Tian <kevin.tian@intel.com>
Reviewed-by: Kirti Wankhede <kwankhede@nvidia.com>
Link: https://lore.kernel.org/r/20220923092652.100656-14-hch@lst.de
Signed-off-by: Alex Williamson <alex.williamson@redhat.com>

authored by

Christoph Hellwig and committed by
Alex Williamson
685a1537 f2fbc72e

+46 -59
+2 -2
Documentation/driver-api/vfio-mediated-device.rst
··· 104 104 int (*probe) (struct mdev_device *dev); 105 105 void (*remove) (struct mdev_device *dev); 106 106 unsigned int (*get_available)(struct mdev_type *mtype); 107 - const struct attribute * const *types_attrs; 107 + ssize_t (*show_description)(struct mdev_type *mtype, char *buf); 108 108 struct device_driver driver; 109 109 }; 110 110 ··· 222 222 223 223 * description 224 224 225 - This attribute should show brief features/description of the type. This is 225 + This attribute can show brief features/description of the type. This is an 226 226 optional attribute. 227 227 228 228 Directories and Files Under the sysfs for Each mdev Device
+5 -13
drivers/gpu/drm/i915/gvt/kvmgt.c
··· 113 113 struct kvm_memory_slot *slot, 114 114 struct kvm_page_track_notifier_node *node); 115 115 116 - static ssize_t description_show(struct mdev_type *mtype, 117 - struct mdev_type_attribute *attr, char *buf) 116 + static ssize_t intel_vgpu_show_description(struct mdev_type *mtype, char *buf) 118 117 { 119 118 struct intel_vgpu_type *type = 120 119 container_of(mtype, struct intel_vgpu_type, type); ··· 126 127 type->conf->fence, vgpu_edid_str(type->conf->edid), 127 128 type->conf->weight); 128 129 } 129 - 130 - static MDEV_TYPE_ATTR_RO(description); 131 - 132 - static const struct attribute *gvt_type_attrs[] = { 133 - &mdev_type_attr_description.attr, 134 - NULL, 135 - }; 136 130 137 131 static void gvt_unpin_guest_page(struct intel_vgpu *vgpu, unsigned long gfn, 138 132 unsigned long size) ··· 1541 1549 .owner = THIS_MODULE, 1542 1550 .dev_groups = intel_vgpu_groups, 1543 1551 }, 1544 - .probe = intel_vgpu_probe, 1545 - .remove = intel_vgpu_remove, 1546 - .get_available = intel_vgpu_get_available, 1547 - .types_attrs = gvt_type_attrs, 1552 + .probe = intel_vgpu_probe, 1553 + .remove = intel_vgpu_remove, 1554 + .get_available = intel_vgpu_get_available, 1555 + .show_description = intel_vgpu_show_description, 1548 1556 }; 1549 1557 1550 1558 int intel_gvt_page_track_add(struct intel_vgpu *info, u64 gfn)
+1 -1
drivers/vfio/mdev/mdev_driver.c
··· 55 55 **/ 56 56 int mdev_register_driver(struct mdev_driver *drv) 57 57 { 58 - if (!drv->types_attrs || !drv->device_api) 58 + if (!drv->device_api) 59 59 return -EINVAL; 60 60 61 61 /* initialize common driver fields */
+32 -8
drivers/vfio/mdev/mdev_sysfs.c
··· 14 14 15 15 #include "mdev_private.h" 16 16 17 - /* Static functions */ 17 + struct mdev_type_attribute { 18 + struct attribute attr; 19 + ssize_t (*show)(struct mdev_type *mtype, 20 + struct mdev_type_attribute *attr, char *buf); 21 + ssize_t (*store)(struct mdev_type *mtype, 22 + struct mdev_type_attribute *attr, const char *buf, 23 + size_t count); 24 + }; 25 + 26 + #define MDEV_TYPE_ATTR_RO(_name) \ 27 + struct mdev_type_attribute mdev_type_attr_##_name = __ATTR_RO(_name) 28 + #define MDEV_TYPE_ATTR_WO(_name) \ 29 + struct mdev_type_attribute mdev_type_attr_##_name = __ATTR_WO(_name) 18 30 19 31 static ssize_t mdev_type_attr_show(struct kobject *kobj, 20 32 struct attribute *__attr, char *buf) ··· 112 100 } 113 101 static MDEV_TYPE_ATTR_RO(available_instances); 114 102 103 + static ssize_t description_show(struct mdev_type *mtype, 104 + struct mdev_type_attribute *attr, 105 + char *buf) 106 + { 107 + return mtype->parent->mdev_driver->show_description(mtype, buf); 108 + } 109 + static MDEV_TYPE_ATTR_RO(description); 110 + 115 111 static struct attribute *mdev_types_core_attrs[] = { 116 112 &mdev_type_attr_create.attr, 117 113 &mdev_type_attr_device_api.attr, 118 114 &mdev_type_attr_name.attr, 119 115 &mdev_type_attr_available_instances.attr, 116 + &mdev_type_attr_description.attr, 120 117 NULL, 121 118 }; 122 119 120 + static umode_t mdev_types_core_is_visible(struct kobject *kobj, 121 + struct attribute *attr, int n) 122 + { 123 + if (attr == &mdev_type_attr_description.attr && 124 + !to_mdev_type(kobj)->parent->mdev_driver->show_description) 125 + return 0; 126 + return attr->mode; 127 + } 128 + 123 129 static struct attribute_group mdev_type_core_group = { 124 130 .attrs = mdev_types_core_attrs, 131 + .is_visible = mdev_types_core_is_visible, 125 132 }; 126 133 127 134 static const struct attribute_group *mdev_type_groups[] = { ··· 186 155 goto attr_devices_failed; 187 156 } 188 157 189 - ret = sysfs_create_files(&type->kobj, parent->mdev_driver->types_attrs); 190 - if (ret) 191 - goto attrs_failed; 192 158 return 0; 193 159 194 - attrs_failed: 195 - kobject_put(type->devices_kobj); 196 160 attr_devices_failed: 197 161 kobject_del(&type->kobj); 198 162 kobject_put(&type->kobj); ··· 196 170 197 171 static void mdev_type_remove(struct mdev_type *type) 198 172 { 199 - sysfs_remove_files(&type->kobj, type->parent->mdev_driver->types_attrs); 200 - 201 173 kobject_put(type->devices_kobj); 202 174 kobject_del(&type->kobj); 203 175 kobject_put(&type->kobj);
+2 -17
include/linux/mdev.h
··· 52 52 return container_of(dev, struct mdev_device, dev); 53 53 } 54 54 55 - /* interface for exporting mdev supported type attributes */ 56 - struct mdev_type_attribute { 57 - struct attribute attr; 58 - ssize_t (*show)(struct mdev_type *mtype, 59 - struct mdev_type_attribute *attr, char *buf); 60 - ssize_t (*store)(struct mdev_type *mtype, 61 - struct mdev_type_attribute *attr, const char *buf, 62 - size_t count); 63 - }; 64 - 65 - #define MDEV_TYPE_ATTR_RO(_name) \ 66 - struct mdev_type_attribute mdev_type_attr_##_name = __ATTR_RO(_name) 67 - #define MDEV_TYPE_ATTR_WO(_name) \ 68 - struct mdev_type_attribute mdev_type_attr_##_name = __ATTR_WO(_name) 69 - 70 55 /** 71 56 * struct mdev_driver - Mediated device driver 72 57 * @device_api: string to return for the device_api sysfs 73 58 * @probe: called when new device created 74 59 * @remove: called when device removed 75 60 * @get_available: Return the max number of instances that can be created 76 - * @types_attrs: attributes to the type kobjects. 61 + * @show_description: Print a description of the mtype 77 62 * @driver: device driver structure 78 63 **/ 79 64 struct mdev_driver { ··· 66 81 int (*probe)(struct mdev_device *dev); 67 82 void (*remove)(struct mdev_device *dev); 68 83 unsigned int (*get_available)(struct mdev_type *mtype); 69 - const struct attribute * const *types_attrs; 84 + ssize_t (*show_description)(struct mdev_type *mtype, char *buf); 70 85 struct device_driver driver; 71 86 }; 72 87
+2 -9
samples/vfio-mdev/mbochs.c
··· 1350 1350 NULL, 1351 1351 }; 1352 1352 1353 - static ssize_t description_show(struct mdev_type *mtype, 1354 - struct mdev_type_attribute *attr, char *buf) 1353 + static ssize_t mbochs_show_description(struct mdev_type *mtype, char *buf) 1355 1354 { 1356 1355 struct mbochs_type *type = 1357 1356 container_of(mtype, struct mbochs_type, type); ··· 1358 1359 return sprintf(buf, "virtual display, %d MB video memory\n", 1359 1360 type ? type->mbytes : 0); 1360 1361 } 1361 - static MDEV_TYPE_ATTR_RO(description); 1362 1362 1363 1363 static unsigned int mbochs_get_available(struct mdev_type *mtype) 1364 1364 { ··· 1366 1368 1367 1369 return atomic_read(&mbochs_avail_mbytes) / type->mbytes; 1368 1370 } 1369 - 1370 - static const struct attribute *mdev_types_attrs[] = { 1371 - &mdev_type_attr_description.attr, 1372 - NULL, 1373 - }; 1374 1371 1375 1372 static const struct vfio_device_ops mbochs_dev_ops = { 1376 1373 .close_device = mbochs_close_device, ··· 1388 1395 .probe = mbochs_probe, 1389 1396 .remove = mbochs_remove, 1390 1397 .get_available = mbochs_get_available, 1391 - .types_attrs = mdev_types_attrs, 1398 + .show_description = mbochs_show_description, 1392 1399 }; 1393 1400 1394 1401 static const struct file_operations vd_fops = {
+2 -9
samples/vfio-mdev/mdpy.c
··· 661 661 NULL, 662 662 }; 663 663 664 - static ssize_t description_show(struct mdev_type *mtype, 665 - struct mdev_type_attribute *attr, char *buf) 664 + static ssize_t mdpy_show_description(struct mdev_type *mtype, char *buf) 666 665 { 667 666 struct mdpy_type *type = container_of(mtype, struct mdpy_type, type); 668 667 669 668 return sprintf(buf, "virtual display, %dx%d framebuffer\n", 670 669 type->width, type->height); 671 670 } 672 - static MDEV_TYPE_ATTR_RO(description); 673 671 674 672 static unsigned int mdpy_get_available(struct mdev_type *mtype) 675 673 { 676 674 return max_devices - mdpy_count; 677 675 } 678 - 679 - static const struct attribute *mdev_types_attrs[] = { 680 - &mdev_type_attr_description.attr, 681 - NULL, 682 - }; 683 676 684 677 static const struct vfio_device_ops mdpy_dev_ops = { 685 678 .init = mdpy_init_dev, ··· 694 701 .probe = mdpy_probe, 695 702 .remove = mdpy_remove, 696 703 .get_available = mdpy_get_available, 697 - .types_attrs = mdev_types_attrs, 704 + .show_description = mdpy_show_description, 698 705 }; 699 706 700 707 static const struct file_operations vd_fops = {