static const struct file_operations vfio_device_fops;
 
+/* true if the vfio_device has open_device() called but not close_device() */
+static bool vfio_assert_device_open(struct vfio_device *device)
+{
+       return !WARN_ON_ONCE(!READ_ONCE(device->open_count));
+}
+
 static int vfio_group_get_device_fd(struct vfio_group *group, char *buf)
 {
        struct vfio_device *device;
        struct vfio_device *device = filep->private_data;
 
        mutex_lock(&device->dev_set->lock);
-       if (!--device->open_count && device->ops->close_device)
+       vfio_assert_device_open(device);
+       if (device->open_count == 1 && device->ops->close_device)
                device->ops->close_device(device);
+       device->open_count--;
        mutex_unlock(&device->dev_set->lock);
 
        module_put(device->dev->driver->owner);
        struct vfio_iommu_driver *driver;
        int ret;
 
-       if (!user_pfn || !phys_pfn || !npage)
+       if (!user_pfn || !phys_pfn || !npage ||
+           !vfio_assert_device_open(device))
                return -EINVAL;
 
        if (npage > VFIO_PIN_PAGES_MAX_ENTRIES)
        if (group->dev_counter > 1)
                return -EINVAL;
 
-       ret = vfio_group_add_container_user(group);
-       if (ret)
-               return ret;
-
        container = group->container;
        driver = container->iommu_driver;
        if (likely(driver && driver->ops->pin_pages))
        else
                ret = -ENOTTY;
 
-       vfio_group_try_dissolve_container(group);
-
        return ret;
 }
 EXPORT_SYMBOL(vfio_pin_pages);
        struct vfio_iommu_driver *driver;
        int ret;
 
-       if (!user_pfn || !npage)
+       if (!user_pfn || !npage || !vfio_assert_device_open(device))
                return -EINVAL;
 
        if (npage > VFIO_PIN_PAGES_MAX_ENTRIES)
                return -E2BIG;
 
-       ret = vfio_group_add_container_user(device->group);
-       if (ret)
-               return ret;
-
        container = device->group->container;
        driver = container->iommu_driver;
        if (likely(driver && driver->ops->unpin_pages))
        else
                ret = -ENOTTY;
 
-       vfio_group_try_dissolve_container(device->group);
-
        return ret;
 }
 EXPORT_SYMBOL(vfio_unpin_pages);
        struct vfio_iommu_driver *driver;
        int ret = 0;
 
-       if (!data || len <= 0)
+       if (!data || len <= 0 || !vfio_assert_device_open(device))
                return -EINVAL;
 
-       ret = vfio_group_add_container_user(device->group);
-       if (ret)
-               return ret;
-
        container = device->group->container;
        driver = container->iommu_driver;
 
                                          user_iova, data, len, write);
        else
                ret = -ENOTTY;
-
-       vfio_group_try_dissolve_container(device->group);
-
        return ret;
 }
 EXPORT_SYMBOL(vfio_dma_rw);
        struct vfio_iommu_driver *driver;
        int ret;
 
-       ret = vfio_group_add_container_user(group);
-       if (ret)
-               return -EINVAL;
-
        container = group->container;
        driver = container->iommu_driver;
        if (likely(driver && driver->ops->register_notifier))
                                                     events, nb);
        else
                ret = -ENOTTY;
-
-       vfio_group_try_dissolve_container(group);
-
        return ret;
 }
 
        struct vfio_iommu_driver *driver;
        int ret;
 
-       ret = vfio_group_add_container_user(group);
-       if (ret)
-               return -EINVAL;
-
        container = group->container;
        driver = container->iommu_driver;
        if (likely(driver && driver->ops->unregister_notifier))
                                                       nb);
        else
                ret = -ENOTTY;
-
-       vfio_group_try_dissolve_container(group);
-
        return ret;
 }
 
        if (*events)
                return -EINVAL;
 
-       ret = vfio_group_add_container_user(group);
-       if (ret)
-               return -EINVAL;
-
        ret = blocking_notifier_chain_register(&group->notifier, nb);
 
        /*
        if (!ret && set_kvm && group->kvm)
                blocking_notifier_call_chain(&group->notifier,
                                        VFIO_GROUP_NOTIFY_SET_KVM, group->kvm);
-
-       vfio_group_try_dissolve_container(group);
-
-       return ret;
-}
-
-static int vfio_unregister_group_notifier(struct vfio_group *group,
-                                        struct notifier_block *nb)
-{
-       int ret;
-
-       ret = vfio_group_add_container_user(group);
-       if (ret)
-               return -EINVAL;
-
-       ret = blocking_notifier_chain_unregister(&group->notifier, nb);
-
-       vfio_group_try_dissolve_container(group);
-
        return ret;
 }
 
        struct vfio_group *group = device->group;
        int ret;
 
-       if (!nb || !events || (*events == 0))
+       if (!nb || !events || (*events == 0) ||
+           !vfio_assert_device_open(device))
                return -EINVAL;
 
        switch (type) {
        struct vfio_group *group = device->group;
        int ret;
 
-       if (!nb)
+       if (!nb || !vfio_assert_device_open(device))
                return -EINVAL;
 
        switch (type) {
                ret = vfio_unregister_iommu_notifier(group, nb);
                break;
        case VFIO_GROUP_NOTIFY:
-               ret = vfio_unregister_group_notifier(group, nb);
+               ret = blocking_notifier_chain_unregister(&group->notifier, nb);
                break;
        default:
                ret = -EINVAL;