vfio/pci: Allow passing zero-length fd array in VFIO_DEVICE_PCI_HOT_RESET
[linux-2.6-microblaze.git] / drivers / vfio / pci / vfio_pci_core.c
index f4a0ea0..65cbada 100644 (file)
@@ -181,7 +181,8 @@ no_mmap:
 struct vfio_pci_group_info;
 static void vfio_pci_dev_set_try_reset(struct vfio_device_set *dev_set);
 static int vfio_pci_dev_set_hot_reset(struct vfio_device_set *dev_set,
-                                     struct vfio_pci_group_info *groups);
+                                     struct vfio_pci_group_info *groups,
+                                     struct iommufd_ctx *iommufd_ctx);
 
 /*
  * INTx masking requires the ability to disable INTx signaling via PCI_COMMAND
@@ -1329,8 +1330,7 @@ vfio_pci_ioctl_pci_hot_reset_groups(struct vfio_pci_core_device *vdev,
        if (ret)
                return ret;
 
-       /* Somewhere between 1 and count is OK */
-       if (!array_count || array_count > count)
+       if (array_count > count)
                return -EINVAL;
 
        group_fds = kcalloc(array_count, sizeof(*group_fds), GFP_KERNEL);
@@ -1379,7 +1379,7 @@ vfio_pci_ioctl_pci_hot_reset_groups(struct vfio_pci_core_device *vdev,
        info.count = array_count;
        info.files = files;
 
-       ret = vfio_pci_dev_set_hot_reset(vdev->vdev.dev_set, &info);
+       ret = vfio_pci_dev_set_hot_reset(vdev->vdev.dev_set, &info, NULL);
 
 hot_reset_release:
        for (file_idx--; file_idx >= 0; file_idx--)
@@ -1402,13 +1402,21 @@ static int vfio_pci_ioctl_pci_hot_reset(struct vfio_pci_core_device *vdev,
        if (hdr.argsz < minsz || hdr.flags)
                return -EINVAL;
 
+       /* zero-length array is only for cdev opened devices */
+       if (!!hdr.count == vfio_device_cdev_opened(&vdev->vdev))
+               return -EINVAL;
+
        /* Can we do a slot or bus reset or neither? */
        if (!pci_probe_reset_slot(vdev->pdev->slot))
                slot = true;
        else if (pci_probe_reset_bus(vdev->pdev->bus))
                return -ENODEV;
 
-       return vfio_pci_ioctl_pci_hot_reset_groups(vdev, hdr.count, slot, arg);
+       if (hdr.count)
+               return vfio_pci_ioctl_pci_hot_reset_groups(vdev, hdr.count, slot, arg);
+
+       return vfio_pci_dev_set_hot_reset(vdev->vdev.dev_set, NULL,
+                                         vfio_iommufd_device_ictx(&vdev->vdev));
 }
 
 static int vfio_pci_ioctl_ioeventfd(struct vfio_pci_core_device *vdev,
@@ -2376,13 +2384,16 @@ const struct pci_error_handlers vfio_pci_core_err_handlers = {
 };
 EXPORT_SYMBOL_GPL(vfio_pci_core_err_handlers);
 
-static bool vfio_dev_in_groups(struct vfio_pci_core_device *vdev,
+static bool vfio_dev_in_groups(struct vfio_device *vdev,
                               struct vfio_pci_group_info *groups)
 {
        unsigned int i;
 
+       if (!groups)
+               return false;
+
        for (i = 0; i < groups->count; i++)
-               if (vfio_file_has_dev(groups->files[i], &vdev->vdev))
+               if (vfio_file_has_dev(groups->files[i], vdev))
                        return true;
        return false;
 }
@@ -2458,7 +2469,8 @@ unwind:
  * get each memory_lock.
  */
 static int vfio_pci_dev_set_hot_reset(struct vfio_device_set *dev_set,
-                                     struct vfio_pci_group_info *groups)
+                                     struct vfio_pci_group_info *groups,
+                                     struct iommufd_ctx *iommufd_ctx)
 {
        struct vfio_pci_core_device *cur_mem;
        struct vfio_pci_core_device *cur_vma;
@@ -2488,11 +2500,38 @@ static int vfio_pci_dev_set_hot_reset(struct vfio_device_set *dev_set,
                goto err_unlock;
 
        list_for_each_entry(cur_vma, &dev_set->device_list, vdev.dev_set_list) {
+               bool owned;
+
                /*
-                * Test whether all the affected devices are contained by the
-                * set of groups provided by the user.
+                * Test whether all the affected devices can be reset by the
+                * user.
+                *
+                * If called from a group opened device and the user provides
+                * a set of groups, all the devices in the dev_set should be
+                * contained by the set of groups provided by the user.
+                *
+                * If called from a cdev opened device and the user provides
+                * a zero-length array, all the devices in the dev_set must
+                * be bound to the same iommufd_ctx as the input iommufd_ctx.
+                * If there is any device that has not been bound to any
+                * iommufd_ctx yet, check if its iommu_group has any device
+                * bound to the input iommufd_ctx.  Such devices can be
+                * considered owned by the input iommufd_ctx as the device
+                * cannot be owned by another iommufd_ctx when its iommu_group
+                * is owned.
+                *
+                * Otherwise, reset is not allowed.
                 */
-               if (!vfio_dev_in_groups(cur_vma, groups)) {
+               if (iommufd_ctx) {
+                       int devid = vfio_iommufd_get_dev_id(&cur_vma->vdev,
+                                                           iommufd_ctx);
+
+                       owned = (devid > 0 || devid == -ENOENT);
+               } else {
+                       owned = vfio_dev_in_groups(&cur_vma->vdev, groups);
+               }
+
+               if (!owned) {
                        ret = -EINVAL;
                        goto err_undo;
                }