On Mon, 2022-08-22 at 08:22 +0200, Christoph Hellwig wrote: > 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. > > Signed-off-by: Jason Gunthorpe <jgg@xxxxxxxxxx> > [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: Christoph Hellwig <hch@xxxxxx> > Reviewed-by: Kevin Tian <kevin.tian@xxxxxxxxx> > Reviewed-by: Kirti Wankhede <kwankhede@xxxxxxxxxx> ...snip... > diff --git a/drivers/vfio/mdev/mdev_core.c > b/drivers/vfio/mdev/mdev_core.c > index 93f8caf2e5f77..2d0302995d7b7 100644 > --- a/drivers/vfio/mdev/mdev_core.c > +++ b/drivers/vfio/mdev/mdev_core.c > @@ -70,6 +70,7 @@ int mdev_register_parent(struct mdev_parent > *parent, struct device *dev, > parent->mdev_driver = mdev_driver; > parent->types = types; > parent->nr_types = nr_types; > + atomic_set(&parent->available_instances, mdev_driver- > >max_instances); > > if (!mdev_bus_compat_class) { > mdev_bus_compat_class = > class_compat_register("mdev_bus"); > @@ -115,14 +116,17 @@ EXPORT_SYMBOL(mdev_unregister_parent); > static void mdev_device_release(struct device *dev) > { > struct mdev_device *mdev = to_mdev_device(dev); > - > - /* Pairs with the get in mdev_device_create() */ > - kobject_put(&mdev->type->kobj); > + struct mdev_parent *parent = mdev->type->parent; > > mutex_lock(&mdev_list_lock); > list_del(&mdev->next); > + if (!parent->mdev_driver->get_available) > + atomic_inc(&parent->available_instances); > mutex_unlock(&mdev_list_lock); > > + /* Pairs with the get in mdev_device_create() */ > + kobject_put(&mdev->type->kobj); > + > dev_dbg(&mdev->dev, "MDEV: destroying\n"); > kfree(mdev); > } > @@ -144,6 +148,13 @@ int mdev_device_create(struct mdev_type *type, > const guid_t *uuid) > } > } > > + if (!drv->get_available) { > + if (atomic_dec_and_test(&parent- > >available_instances)) { This is still broken. https://lore.kernel.org/r/20220719144808.GA21431@xxxxxx/ Thanks, Eric > + mutex_unlock(&mdev_list_lock); > + return -EUSERS; > + } > + } > +