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

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

Every driver just emits a static string, simply add a field to the
mdev_type for the driver to fill out or fall back to the sysfs name and
provide a standard sysfs show function.

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>
Reviewed-by: Eric Farman <farman@linux.ibm.com>
Link: https://lore.kernel.org/r/20220923092652.100656-12-hch@lst.de
Signed-off-by: Alex Williamson <alex.williamson@redhat.com>

authored by

Christoph Hellwig and committed by
Alex Williamson
0bc79069 290aac5d

+27 -72
+1 -1
Documentation/driver-api/vfio-mediated-device.rst
··· 217 217 218 218 * name 219 219 220 - This attribute should show human readable name. This is optional attribute. 220 + This attribute shows a human readable name. 221 221 222 222 * description 223 223
-8
drivers/gpu/drm/i915/gvt/kvmgt.c
··· 138 138 type->conf->weight); 139 139 } 140 140 141 - static ssize_t name_show(struct mdev_type *mtype, 142 - struct mdev_type_attribute *attr, char *buf) 143 - { 144 - return sprintf(buf, "%s\n", mtype->sysfs_name); 145 - } 146 - 147 141 static MDEV_TYPE_ATTR_RO(available_instances); 148 142 static MDEV_TYPE_ATTR_RO(description); 149 - static MDEV_TYPE_ATTR_RO(name); 150 143 151 144 static const struct attribute *gvt_type_attrs[] = { 152 145 &mdev_type_attr_available_instances.attr, 153 146 &mdev_type_attr_description.attr, 154 - &mdev_type_attr_name.attr, 155 147 NULL, 156 148 }; 157 149
+1
drivers/s390/cio/vfio_ccw_drv.c
··· 221 221 dev_set_drvdata(&sch->dev, private); 222 222 223 223 private->mdev_type.sysfs_name = "io"; 224 + private->mdev_type.pretty_name = "I/O subchannel (Non-QDIO)"; 224 225 private->mdev_types[0] = &private->mdev_type; 225 226 ret = mdev_register_parent(&private->parent, &sch->dev, 226 227 &vfio_ccw_mdev_driver,
-8
drivers/s390/cio/vfio_ccw_ops.c
··· 44 44 vfio_ccw_mdev_reset(private); 45 45 } 46 46 47 - static ssize_t name_show(struct mdev_type *mtype, 48 - struct mdev_type_attribute *attr, char *buf) 49 - { 50 - return sprintf(buf, "I/O subchannel (Non-QDIO)\n"); 51 - } 52 - static MDEV_TYPE_ATTR_RO(name); 53 - 54 47 static ssize_t available_instances_show(struct mdev_type *mtype, 55 48 struct mdev_type_attribute *attr, 56 49 char *buf) ··· 55 62 static MDEV_TYPE_ATTR_RO(available_instances); 56 63 57 64 static const struct attribute *mdev_types_attrs[] = { 58 - &mdev_type_attr_name.attr, 59 65 &mdev_type_attr_available_instances.attr, 60 66 NULL, 61 67 };
+1 -9
drivers/s390/crypto/vfio_ap_ops.c
··· 790 790 vfio_put_device(&matrix_mdev->vdev); 791 791 } 792 792 793 - static ssize_t name_show(struct mdev_type *mtype, 794 - struct mdev_type_attribute *attr, char *buf) 795 - { 796 - return sprintf(buf, "%s\n", VFIO_AP_MDEV_NAME_HWVIRT); 797 - } 798 - 799 - static MDEV_TYPE_ATTR_RO(name); 800 - 801 793 static ssize_t available_instances_show(struct mdev_type *mtype, 802 794 struct mdev_type_attribute *attr, 803 795 char *buf) ··· 801 809 static MDEV_TYPE_ATTR_RO(available_instances); 802 810 803 811 static const struct attribute *vfio_ap_mdev_type_attrs[] = { 804 - &mdev_type_attr_name.attr, 805 812 &mdev_type_attr_available_instances.attr, 806 813 NULL, 807 814 }; ··· 1804 1813 return ret; 1805 1814 1806 1815 matrix_dev->mdev_type.sysfs_name = VFIO_AP_MDEV_TYPE_HWVIRT; 1816 + matrix_dev->mdev_type.pretty_name = VFIO_AP_MDEV_NAME_HWVIRT; 1807 1817 matrix_dev->mdev_types[0] = &matrix_dev->mdev_type; 1808 1818 ret = mdev_register_parent(&matrix_dev->parent, &matrix_dev->device, 1809 1819 &vfio_ap_matrix_driver,
+10
drivers/vfio/mdev/mdev_sysfs.c
··· 81 81 } 82 82 static MDEV_TYPE_ATTR_RO(device_api); 83 83 84 + static ssize_t name_show(struct mdev_type *mtype, 85 + struct mdev_type_attribute *attr, char *buf) 86 + { 87 + return sprintf(buf, "%s\n", 88 + mtype->pretty_name ? mtype->pretty_name : mtype->sysfs_name); 89 + } 90 + 91 + static MDEV_TYPE_ATTR_RO(name); 92 + 84 93 static struct attribute *mdev_types_core_attrs[] = { 85 94 &mdev_type_attr_create.attr, 86 95 &mdev_type_attr_device_api.attr, 96 + &mdev_type_attr_name.attr, 87 97 NULL, 88 98 }; 89 99
+1
include/linux/mdev.h
··· 26 26 struct mdev_type { 27 27 /* set by the driver before calling mdev_register parent: */ 28 28 const char *sysfs_name; 29 + const char *pretty_name; 29 30 30 31 /* set by the core, can be used drivers */ 31 32 struct mdev_parent *parent;
+4 -16
samples/vfio-mdev/mbochs.c
··· 101 101 102 102 static struct mbochs_type { 103 103 struct mdev_type type; 104 - const char *name; 105 104 u32 mbytes; 106 105 u32 max_x; 107 106 u32 max_y; 108 107 } mbochs_types[] = { 109 108 { 110 109 .type.sysfs_name = MBOCHS_TYPE_1, 111 - .name = MBOCHS_CLASS_NAME "-" MBOCHS_TYPE_1, 110 + .type.pretty_name = MBOCHS_CLASS_NAME "-" MBOCHS_TYPE_1, 112 111 .mbytes = 4, 113 112 .max_x = 800, 114 113 .max_y = 600, 115 114 }, { 116 115 .type.sysfs_name = MBOCHS_TYPE_2, 117 - .name = MBOCHS_CLASS_NAME "-" MBOCHS_TYPE_2, 116 + .type.pretty_name = MBOCHS_CLASS_NAME "-" MBOCHS_TYPE_2, 118 117 .mbytes = 16, 119 118 .max_x = 1920, 120 119 .max_y = 1440, 121 120 }, { 122 121 .type.sysfs_name = MBOCHS_TYPE_3, 123 - .name = MBOCHS_CLASS_NAME "-" MBOCHS_TYPE_3, 122 + .type.pretty_name = MBOCHS_CLASS_NAME "-" MBOCHS_TYPE_3, 124 123 .mbytes = 64, 125 124 .max_x = 0, 126 125 .max_y = 0, ··· 555 556 mbochs_reset(mdev_state); 556 557 557 558 dev_info(vdev->dev, "%s: %s, %d MB, %ld pages\n", __func__, 558 - type->name, type->mbytes, mdev_state->pagecount); 559 + type->type.pretty_name, type->mbytes, mdev_state->pagecount); 559 560 return 0; 560 561 561 562 err_vconfig: ··· 1350 1351 NULL, 1351 1352 }; 1352 1353 1353 - static ssize_t name_show(struct mdev_type *mtype, 1354 - struct mdev_type_attribute *attr, char *buf) 1355 - { 1356 - struct mbochs_type *type = 1357 - container_of(mtype, struct mbochs_type, type); 1358 - 1359 - return sprintf(buf, "%s\n", type->name); 1360 - } 1361 - static MDEV_TYPE_ATTR_RO(name); 1362 - 1363 1354 static ssize_t description_show(struct mdev_type *mtype, 1364 1355 struct mdev_type_attribute *attr, char *buf) 1365 1356 { ··· 1374 1385 static MDEV_TYPE_ATTR_RO(available_instances); 1375 1386 1376 1387 static const struct attribute *mdev_types_attrs[] = { 1377 - &mdev_type_attr_name.attr, 1378 1388 &mdev_type_attr_description.attr, 1379 1389 &mdev_type_attr_available_instances.attr, 1380 1390 NULL,
+5 -16
samples/vfio-mdev/mdpy.c
··· 53 53 54 54 static struct mdpy_type { 55 55 struct mdev_type type; 56 - const char *name; 57 56 u32 format; 58 57 u32 bytepp; 59 58 u32 width; ··· 60 61 } mdpy_types[] = { 61 62 { 62 63 .type.sysfs_name = MDPY_TYPE_1, 63 - .name = MDPY_CLASS_NAME "-" MDPY_TYPE_1, 64 + .type.pretty_name = MDPY_CLASS_NAME "-" MDPY_TYPE_1, 64 65 .format = DRM_FORMAT_XRGB8888, 65 66 .bytepp = 4, 66 67 .width = 640, 67 68 .height = 480, 68 69 }, { 69 70 .type.sysfs_name = MDPY_TYPE_2, 70 - .name = MDPY_CLASS_NAME "-" MDPY_TYPE_2, 71 + .type.pretty_name = MDPY_CLASS_NAME "-" MDPY_TYPE_2, 71 72 .format = DRM_FORMAT_XRGB8888, 72 73 .bytepp = 4, 73 74 .width = 1024, 74 75 .height = 768, 75 76 }, { 76 77 .type.sysfs_name = MDPY_TYPE_3, 77 - .name = MDPY_CLASS_NAME "-" MDPY_TYPE_3, 78 + .type.pretty_name = MDPY_CLASS_NAME "-" MDPY_TYPE_3, 78 79 .format = DRM_FORMAT_XRGB8888, 79 80 .bytepp = 4, 80 81 .width = 1920, ··· 255 256 mdpy_create_config_space(mdev_state); 256 257 mdpy_reset(mdev_state); 257 258 258 - dev_info(vdev->dev, "%s: %s (%dx%d)\n", __func__, type->name, type->width, 259 - type->height); 259 + dev_info(vdev->dev, "%s: %s (%dx%d)\n", __func__, type->type.pretty_name, 260 + type->width, type->height); 260 261 261 262 mdpy_count++; 262 263 return 0; ··· 661 662 NULL, 662 663 }; 663 664 664 - static ssize_t name_show(struct mdev_type *mtype, 665 - struct mdev_type_attribute *attr, char *buf) 666 - { 667 - struct mdpy_type *type = container_of(mtype, struct mdpy_type, type); 668 - 669 - return sprintf(buf, "%s\n", type->name); 670 - } 671 - static MDEV_TYPE_ATTR_RO(name); 672 - 673 665 static ssize_t description_show(struct mdev_type *mtype, 674 666 struct mdev_type_attribute *attr, char *buf) 675 667 { ··· 680 690 static MDEV_TYPE_ATTR_RO(available_instances); 681 691 682 692 static const struct attribute *mdev_types_attrs[] = { 683 - &mdev_type_attr_name.attr, 684 693 &mdev_type_attr_description.attr, 685 694 &mdev_type_attr_available_instances.attr, 686 695 NULL,
+4 -14
samples/vfio-mdev/mtty.c
··· 146 146 static struct mtty_type { 147 147 struct mdev_type type; 148 148 int nr_ports; 149 - const char *name; 150 149 } mtty_types[2] = { 151 - { .nr_ports = 1, .type.sysfs_name = "1", .name = "Single port serial" }, 152 - { .nr_ports = 2, .type.sysfs_name = "2", .name = "Dual port serial" }, 150 + { .nr_ports = 1, .type.sysfs_name = "1", 151 + .type.pretty_name = "Single port serial" }, 152 + { .nr_ports = 2, .type.sysfs_name = "2", 153 + .type.pretty_name = "Dual port serial" }, 153 154 }; 154 155 155 156 static struct mdev_type *mtty_mdev_types[] = { ··· 1256 1255 NULL, 1257 1256 }; 1258 1257 1259 - static ssize_t name_show(struct mdev_type *mtype, 1260 - struct mdev_type_attribute *attr, char *buf) 1261 - { 1262 - struct mtty_type *type = container_of(mtype, struct mtty_type, type); 1263 - 1264 - return sysfs_emit(buf, "%s\n", type->name); 1265 - } 1266 - 1267 - static MDEV_TYPE_ATTR_RO(name); 1268 - 1269 1258 static ssize_t available_instances_show(struct mdev_type *mtype, 1270 1259 struct mdev_type_attribute *attr, 1271 1260 char *buf) ··· 1269 1278 static MDEV_TYPE_ATTR_RO(available_instances); 1270 1279 1271 1280 static const struct attribute *mdev_types_attrs[] = { 1272 - &mdev_type_attr_name.attr, 1273 1281 &mdev_type_attr_available_instances.attr, 1274 1282 NULL, 1275 1283 };