INIT_LIST_HEAD(&private->crw);
        INIT_WORK(&private->io_work, vfio_ccw_sch_io_todo);
        INIT_WORK(&private->crw_work, vfio_ccw_crw_todo);
-       atomic_set(&private->avail, 1);
 
        private->cp.guest_cp = kcalloc(CCWCHAIN_LEN_MAX, sizeof(struct ccw1),
                                       GFP_KERNEL);
 
        vfio_ccw_mdev_reset(private);
 }
 
-static unsigned int vfio_ccw_get_available(struct mdev_type *mtype)
-{
-       struct vfio_ccw_private *private = dev_get_drvdata(mtype->parent->dev);
-
-       return atomic_read(&private->avail);
-}
-
 static int vfio_ccw_mdev_init_dev(struct vfio_device *vdev)
 {
        struct vfio_ccw_private *private =
        if (private->state == VFIO_CCW_STATE_NOT_OPER)
                return -ENODEV;
 
-       if (atomic_dec_if_positive(&private->avail) < 0)
-               return -EPERM;
-
        ret = vfio_init_device(&private->vdev, &mdev->dev, &vfio_ccw_dev_ops);
        if (ret)
                return ret;
 
 err_put_vdev:
        vfio_put_device(&private->vdev);
-       atomic_inc(&private->avail);
        return ret;
 }
 
         * cycle.
         */
        wait_for_completion(&private->release_comp);
-
-       atomic_inc(&private->avail);
 }
 
 static int vfio_ccw_mdev_open_device(struct vfio_device *vdev)
 
 struct mdev_driver vfio_ccw_mdev_driver = {
        .device_api = VFIO_DEVICE_API_CCW_STRING,
+       .max_instances = 1,
        .driver = {
                .name = "vfio_ccw_mdev",
                .owner = THIS_MODULE,
        },
        .probe = vfio_ccw_mdev_probe,
        .remove = vfio_ccw_mdev_remove,
-       .get_available = vfio_ccw_get_available,
 };
 
  * @sch: pointer to the subchannel
  * @state: internal state of the device
  * @completion: synchronization helper of the I/O completion
- * @avail: available for creating a mediated device
  * @io_region: MMIO region to input/output I/O arguments/results
  * @io_mutex: protect against concurrent update of I/O regions
  * @region: additional regions for other subchannel operations
        struct subchannel       *sch;
        int                     state;
        struct completion       *completion;
-       atomic_t                avail;
        struct ccw_io_region    *io_region;
        struct mutex            io_mutex;
        struct vfio_ccw_region *region;
 
        struct ap_matrix_mdev *matrix_mdev =
                container_of(vdev, struct ap_matrix_mdev, vdev);
 
-       if ((atomic_dec_if_positive(&matrix_dev->available_instances) < 0))
-               return -EPERM;
-
        matrix_mdev->mdev = to_mdev_device(vdev->dev);
        vfio_ap_matrix_init(&matrix_dev->info, &matrix_mdev->matrix);
        matrix_mdev->pqap_hook = handle_pqap;
 
 static void vfio_ap_mdev_release_dev(struct vfio_device *vdev)
 {
-       atomic_inc(&matrix_dev->available_instances);
        vfio_free_device(vdev);
 }
 
        vfio_put_device(&matrix_mdev->vdev);
 }
 
-static unsigned int vfio_ap_mdev_get_available(struct mdev_type *mtype)
-{
-       return atomic_read(&matrix_dev->available_instances);
-}
-
 #define MDEV_SHARING_ERR "Userspace may not re-assign queue %02lx.%04lx " \
                         "already assigned to %s"
 
 
 static struct mdev_driver vfio_ap_matrix_driver = {
        .device_api = VFIO_DEVICE_API_AP_STRING,
+       .max_instances = MAX_ZDEV_ENTRIES_EXT,
        .driver = {
                .name = "vfio_ap_mdev",
                .owner = THIS_MODULE,
        },
        .probe = vfio_ap_mdev_probe,
        .remove = vfio_ap_mdev_remove,
-       .get_available = vfio_ap_mdev_get_available,
 };
 
 int vfio_ap_mdev_register(void)
 {
        int ret;
 
-       atomic_set(&matrix_dev->available_instances, MAX_ZDEV_ENTRIES_EXT);
-
        ret = mdev_register_driver(&vfio_ap_matrix_driver);
        if (ret)
                return ret;
 
  * struct ap_matrix_dev - Contains the data for the matrix device.
  *
  * @device:    generic device structure associated with the AP matrix device
- * @available_instances: number of mediated matrix devices that can be created
  * @info:      the struct containing the output from the PQAP(QCI) instruction
  * @mdev_list: the list of mediated matrix devices created
  * @mdevs_lock: mutex for locking the AP matrix device. This lock will be
  */
 struct ap_matrix_dev {
        struct device device;
-       atomic_t available_instances;
        struct ap_config_info info;
        struct list_head mdev_list;
        struct mutex mdevs_lock; /* serializes access to each ap_matrix_mdev */
 
        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");
 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);
 }
                }
        }
 
+       if (!drv->get_available) {
+               /*
+                * Note: that non-atomic read and dec is fine here because
+                * all modifications are under mdev_list_lock.
+                */
+               if (!atomic_read(&parent->available_instances)) {
+                       mutex_unlock(&mdev_list_lock);
+                       return -EUSERS;
+               }
+               atomic_dec(&parent->available_instances);
+       }
+
        mdev = kzalloc(sizeof(*mdev), GFP_KERNEL);
        if (!mdev) {
                mutex_unlock(&mdev_list_lock);
 
 {
        struct mdev_driver *drv = mtype->parent->mdev_driver;
 
-       return sysfs_emit(buf, "%u\n", drv->get_available(mtype));
+       if (drv->get_available)
+               return sysfs_emit(buf, "%u\n", drv->get_available(mtype));
+       return sysfs_emit(buf, "%u\n",
+                         atomic_read(&mtype->parent->available_instances));
 }
 static MDEV_TYPE_ATTR_RO(available_instances);
 
 
        struct rw_semaphore unreg_sem;
        struct mdev_type **types;
        unsigned int nr_types;
+       atomic_t available_instances;
 };
 
 static inline struct mdev_device *to_mdev_device(struct device *dev)
 /**
  * struct mdev_driver - Mediated device driver
  * @device_api: string to return for the device_api sysfs
+ * @max_instances: maximum number of instances supported (optional)
  * @probe: called when new device created
  * @remove: called when device removed
  * @get_available: Return the max number of instances that can be created
  **/
 struct mdev_driver {
        const char *device_api;
+       unsigned int max_instances;
        int (*probe)(struct mdev_device *dev);
        void (*remove)(struct mdev_device *dev);
        unsigned int (*get_available)(struct mdev_type *mtype);
 
 
 MODULE_LICENSE("GPL v2");
 
-static int max_devices = 4;
-module_param_named(count, max_devices, int, 0444);
-MODULE_PARM_DESC(count, "number of " MDPY_NAME " devices");
-
-
 #define MDPY_TYPE_1 "vga"
 #define MDPY_TYPE_2 "xga"
 #define MDPY_TYPE_3 "hd"
 static struct cdev     mdpy_cdev;
 static struct device   mdpy_dev;
 static struct mdev_parent mdpy_parent;
-static u32             mdpy_count;
 static const struct vfio_device_ops mdpy_dev_ops;
 
 /* State of each mdev device */
        u32 fbsize;
        int ret = -ENOMEM;
 
-       if (mdpy_count >= max_devices)
-               return ret;
-
        mdev_state->vconfig = kzalloc(MDPY_CONFIG_SPACE_SIZE, GFP_KERNEL);
        if (!mdev_state->vconfig)
                return ret;
 
        dev_info(vdev->dev, "%s: %s (%dx%d)\n", __func__, type->type.pretty_name,
                 type->width, type->height);
-
-       mdpy_count++;
        return 0;
 
 out_vconfig:
        struct mdev_state *mdev_state =
                container_of(vdev, struct mdev_state, vdev);
 
-       mdpy_count--;
        vfree(mdev_state->memblk);
        kfree(mdev_state->vconfig);
        vfio_free_device(vdev);
                       type->width, type->height);
 }
 
-static unsigned int mdpy_get_available(struct mdev_type *mtype)
-{
-       return max_devices - mdpy_count;
-}
-
 static const struct vfio_device_ops mdpy_dev_ops = {
        .init = mdpy_init_dev,
        .release = mdpy_release_dev,
 
 static struct mdev_driver mdpy_driver = {
        .device_api = VFIO_DEVICE_API_PCI_STRING,
+       .max_instances = 4,
        .driver = {
                .name = "mdpy",
                .owner = THIS_MODULE,
        },
        .probe = mdpy_probe,
        .remove = mdpy_remove,
-       .get_available = mdpy_get_available,
        .show_description = mdpy_show_description,
 };
 
        mdpy_class = NULL;
 }
 
+module_param_named(count, mdpy_driver.max_instances, int, 0444);
+MODULE_PARM_DESC(count, "number of " MDPY_NAME " devices");
+
 module_init(mdpy_dev_init)
 module_exit(mdpy_dev_exit)