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

Configure Feed

Select the types of activity you want to include in your feed.

vfio/mdev: add mdev available instance checking to the core

Many of the mdev drivers use a simple counter for keeping track of the
available instances. Move this code to the core code and store the counter
in the mdev_parent. Implement it using correct locking, fixing mdpy.

Drivers just provide the value in the mdev_driver at registration time
and the core code takes care of maintaining it and exposing the value in
sysfs.

[hch: count instances per-parent instead of per-type, use an atomic_t
to avoid taking mdev_list_lock in the show method]

Signed-off-by: Jason Gunthorpe <jgg@nvidia.com>
Signed-off-by: Christoph Hellwig <hch@lst.de>
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-15-hch@lst.de
Signed-off-by: Alex Williamson <alex.williamson@redhat.com>

authored by

Jason Gunthorpe and committed by
Alex Williamson
9c799c22 685a1537

+32 -53
-1
drivers/s390/cio/vfio_ccw_drv.c
··· 141 141 INIT_LIST_HEAD(&private->crw); 142 142 INIT_WORK(&private->io_work, vfio_ccw_sch_io_todo); 143 143 INIT_WORK(&private->crw_work, vfio_ccw_crw_todo); 144 - atomic_set(&private->avail, 1); 145 144 146 145 private->cp.guest_cp = kcalloc(CCWCHAIN_LEN_MAX, sizeof(struct ccw1), 147 146 GFP_KERNEL);
+1 -14
drivers/s390/cio/vfio_ccw_ops.c
··· 44 44 vfio_ccw_mdev_reset(private); 45 45 } 46 46 47 - static unsigned int vfio_ccw_get_available(struct mdev_type *mtype) 48 - { 49 - struct vfio_ccw_private *private = dev_get_drvdata(mtype->parent->dev); 50 - 51 - return atomic_read(&private->avail); 52 - } 53 - 54 47 static int vfio_ccw_mdev_init_dev(struct vfio_device *vdev) 55 48 { 56 49 struct vfio_ccw_private *private = ··· 60 67 61 68 if (private->state == VFIO_CCW_STATE_NOT_OPER) 62 69 return -ENODEV; 63 - 64 - if (atomic_dec_if_positive(&private->avail) < 0) 65 - return -EPERM; 66 70 67 71 ret = vfio_init_device(&private->vdev, &mdev->dev, &vfio_ccw_dev_ops); 68 72 if (ret) ··· 78 88 79 89 err_put_vdev: 80 90 vfio_put_device(&private->vdev); 81 - atomic_inc(&private->avail); 82 91 return ret; 83 92 } 84 93 ··· 119 130 * cycle. 120 131 */ 121 132 wait_for_completion(&private->release_comp); 122 - 123 - atomic_inc(&private->avail); 124 133 } 125 134 126 135 static int vfio_ccw_mdev_open_device(struct vfio_device *vdev) ··· 592 605 593 606 struct mdev_driver vfio_ccw_mdev_driver = { 594 607 .device_api = VFIO_DEVICE_API_CCW_STRING, 608 + .max_instances = 1, 595 609 .driver = { 596 610 .name = "vfio_ccw_mdev", 597 611 .owner = THIS_MODULE, ··· 600 612 }, 601 613 .probe = vfio_ccw_mdev_probe, 602 614 .remove = vfio_ccw_mdev_remove, 603 - .get_available = vfio_ccw_get_available, 604 615 };
-2
drivers/s390/cio/vfio_ccw_private.h
··· 73 73 * @sch: pointer to the subchannel 74 74 * @state: internal state of the device 75 75 * @completion: synchronization helper of the I/O completion 76 - * @avail: available for creating a mediated device 77 76 * @io_region: MMIO region to input/output I/O arguments/results 78 77 * @io_mutex: protect against concurrent update of I/O regions 79 78 * @region: additional regions for other subchannel operations ··· 96 97 struct subchannel *sch; 97 98 int state; 98 99 struct completion *completion; 99 - atomic_t avail; 100 100 struct ccw_io_region *io_region; 101 101 struct mutex io_mutex; 102 102 struct vfio_ccw_region *region;
+1 -12
drivers/s390/crypto/vfio_ap_ops.c
··· 689 689 struct ap_matrix_mdev *matrix_mdev = 690 690 container_of(vdev, struct ap_matrix_mdev, vdev); 691 691 692 - if ((atomic_dec_if_positive(&matrix_dev->available_instances) < 0)) 693 - return -EPERM; 694 - 695 692 matrix_mdev->mdev = to_mdev_device(vdev->dev); 696 693 vfio_ap_matrix_init(&matrix_dev->info, &matrix_mdev->matrix); 697 694 matrix_mdev->pqap_hook = handle_pqap; ··· 767 770 768 771 static void vfio_ap_mdev_release_dev(struct vfio_device *vdev) 769 772 { 770 - atomic_inc(&matrix_dev->available_instances); 771 773 vfio_free_device(vdev); 772 774 } 773 775 ··· 784 788 mutex_unlock(&matrix_dev->mdevs_lock); 785 789 mutex_unlock(&matrix_dev->guests_lock); 786 790 vfio_put_device(&matrix_mdev->vdev); 787 - } 788 - 789 - static unsigned int vfio_ap_mdev_get_available(struct mdev_type *mtype) 790 - { 791 - return atomic_read(&matrix_dev->available_instances); 792 791 } 793 792 794 793 #define MDEV_SHARING_ERR "Userspace may not re-assign queue %02lx.%04lx " \ ··· 1763 1772 1764 1773 static struct mdev_driver vfio_ap_matrix_driver = { 1765 1774 .device_api = VFIO_DEVICE_API_AP_STRING, 1775 + .max_instances = MAX_ZDEV_ENTRIES_EXT, 1766 1776 .driver = { 1767 1777 .name = "vfio_ap_mdev", 1768 1778 .owner = THIS_MODULE, ··· 1772 1780 }, 1773 1781 .probe = vfio_ap_mdev_probe, 1774 1782 .remove = vfio_ap_mdev_remove, 1775 - .get_available = vfio_ap_mdev_get_available, 1776 1783 }; 1777 1784 1778 1785 int vfio_ap_mdev_register(void) 1779 1786 { 1780 1787 int ret; 1781 - 1782 - atomic_set(&matrix_dev->available_instances, MAX_ZDEV_ENTRIES_EXT); 1783 1788 1784 1789 ret = mdev_register_driver(&vfio_ap_matrix_driver); 1785 1790 if (ret)
-2
drivers/s390/crypto/vfio_ap_private.h
··· 29 29 * struct ap_matrix_dev - Contains the data for the matrix device. 30 30 * 31 31 * @device: generic device structure associated with the AP matrix device 32 - * @available_instances: number of mediated matrix devices that can be created 33 32 * @info: the struct containing the output from the PQAP(QCI) instruction 34 33 * @mdev_list: the list of mediated matrix devices created 35 34 * @mdevs_lock: mutex for locking the AP matrix device. This lock will be ··· 45 46 */ 46 47 struct ap_matrix_dev { 47 48 struct device device; 48 - atomic_t available_instances; 49 49 struct ap_config_info info; 50 50 struct list_head mdev_list; 51 51 struct mutex mdevs_lock; /* serializes access to each ap_matrix_mdev */
+19 -3
drivers/vfio/mdev/mdev_core.c
··· 70 70 parent->mdev_driver = mdev_driver; 71 71 parent->types = types; 72 72 parent->nr_types = nr_types; 73 + atomic_set(&parent->available_instances, mdev_driver->max_instances); 73 74 74 75 if (!mdev_bus_compat_class) { 75 76 mdev_bus_compat_class = class_compat_register("mdev_bus"); ··· 116 115 static void mdev_device_release(struct device *dev) 117 116 { 118 117 struct mdev_device *mdev = to_mdev_device(dev); 119 - 120 - /* Pairs with the get in mdev_device_create() */ 121 - kobject_put(&mdev->type->kobj); 118 + struct mdev_parent *parent = mdev->type->parent; 122 119 123 120 mutex_lock(&mdev_list_lock); 124 121 list_del(&mdev->next); 122 + if (!parent->mdev_driver->get_available) 123 + atomic_inc(&parent->available_instances); 125 124 mutex_unlock(&mdev_list_lock); 125 + 126 + /* Pairs with the get in mdev_device_create() */ 127 + kobject_put(&mdev->type->kobj); 126 128 127 129 dev_dbg(&mdev->dev, "MDEV: destroying\n"); 128 130 kfree(mdev); ··· 146 142 mutex_unlock(&mdev_list_lock); 147 143 return -EEXIST; 148 144 } 145 + } 146 + 147 + if (!drv->get_available) { 148 + /* 149 + * Note: that non-atomic read and dec is fine here because 150 + * all modifications are under mdev_list_lock. 151 + */ 152 + if (!atomic_read(&parent->available_instances)) { 153 + mutex_unlock(&mdev_list_lock); 154 + return -EUSERS; 155 + } 156 + atomic_dec(&parent->available_instances); 149 157 } 150 158 151 159 mdev = kzalloc(sizeof(*mdev), GFP_KERNEL);
+4 -1
drivers/vfio/mdev/mdev_sysfs.c
··· 108 108 { 109 109 struct mdev_driver *drv = mtype->parent->mdev_driver; 110 110 111 - return sysfs_emit(buf, "%u\n", drv->get_available(mtype)); 111 + if (drv->get_available) 112 + return sysfs_emit(buf, "%u\n", drv->get_available(mtype)); 113 + return sysfs_emit(buf, "%u\n", 114 + atomic_read(&mtype->parent->available_instances)); 112 115 } 113 116 static MDEV_TYPE_ATTR_RO(available_instances); 114 117
+3
include/linux/mdev.h
··· 45 45 struct rw_semaphore unreg_sem; 46 46 struct mdev_type **types; 47 47 unsigned int nr_types; 48 + atomic_t available_instances; 48 49 }; 49 50 50 51 static inline struct mdev_device *to_mdev_device(struct device *dev) ··· 56 55 /** 57 56 * struct mdev_driver - Mediated device driver 58 57 * @device_api: string to return for the device_api sysfs 58 + * @max_instances: maximum number of instances supported (optional) 59 59 * @probe: called when new device created 60 60 * @remove: called when device removed 61 61 * @get_available: Return the max number of instances that can be created ··· 65 63 **/ 66 64 struct mdev_driver { 67 65 const char *device_api; 66 + unsigned int max_instances; 68 67 int (*probe)(struct mdev_device *dev); 69 68 void (*remove)(struct mdev_device *dev); 70 69 unsigned int (*get_available)(struct mdev_type *mtype);
+4 -18
samples/vfio-mdev/mdpy.c
··· 42 42 43 43 MODULE_LICENSE("GPL v2"); 44 44 45 - static int max_devices = 4; 46 - module_param_named(count, max_devices, int, 0444); 47 - MODULE_PARM_DESC(count, "number of " MDPY_NAME " devices"); 48 - 49 - 50 45 #define MDPY_TYPE_1 "vga" 51 46 #define MDPY_TYPE_2 "xga" 52 47 #define MDPY_TYPE_3 "hd" ··· 88 93 static struct cdev mdpy_cdev; 89 94 static struct device mdpy_dev; 90 95 static struct mdev_parent mdpy_parent; 91 - static u32 mdpy_count; 92 96 static const struct vfio_device_ops mdpy_dev_ops; 93 97 94 98 /* State of each mdev device */ ··· 229 235 u32 fbsize; 230 236 int ret = -ENOMEM; 231 237 232 - if (mdpy_count >= max_devices) 233 - return ret; 234 - 235 238 mdev_state->vconfig = kzalloc(MDPY_CONFIG_SPACE_SIZE, GFP_KERNEL); 236 239 if (!mdev_state->vconfig) 237 240 return ret; ··· 248 257 249 258 dev_info(vdev->dev, "%s: %s (%dx%d)\n", __func__, type->type.pretty_name, 250 259 type->width, type->height); 251 - 252 - mdpy_count++; 253 260 return 0; 254 261 255 262 out_vconfig: ··· 281 292 struct mdev_state *mdev_state = 282 293 container_of(vdev, struct mdev_state, vdev); 283 294 284 - mdpy_count--; 285 295 vfree(mdev_state->memblk); 286 296 kfree(mdev_state->vconfig); 287 297 vfio_free_device(vdev); ··· 657 669 type->width, type->height); 658 670 } 659 671 660 - static unsigned int mdpy_get_available(struct mdev_type *mtype) 661 - { 662 - return max_devices - mdpy_count; 663 - } 664 - 665 672 static const struct vfio_device_ops mdpy_dev_ops = { 666 673 .init = mdpy_init_dev, 667 674 .release = mdpy_release_dev, ··· 668 685 669 686 static struct mdev_driver mdpy_driver = { 670 687 .device_api = VFIO_DEVICE_API_PCI_STRING, 688 + .max_instances = 4, 671 689 .driver = { 672 690 .name = "mdpy", 673 691 .owner = THIS_MODULE, ··· 677 693 }, 678 694 .probe = mdpy_probe, 679 695 .remove = mdpy_remove, 680 - .get_available = mdpy_get_available, 681 696 .show_description = mdpy_show_description, 682 697 }; 683 698 ··· 752 769 class_destroy(mdpy_class); 753 770 mdpy_class = NULL; 754 771 } 772 + 773 + module_param_named(count, mdpy_driver.max_instances, int, 0444); 774 + MODULE_PARM_DESC(count, "number of " MDPY_NAME " devices"); 755 775 756 776 module_init(mdpy_dev_init) 757 777 module_exit(mdpy_dev_exit)