Skip to content

Commit 7654a88

Browse files
committed
Merge branches 'v5.20/vfio/migration-enhancements-v3', 'v5.20/vfio/simplify-bus_type-determination-v3', 'v5.20/vfio/check-vfio_register_iommu_driver-return-v2', 'v5.20/vfio/check-iommu_group_set_name_return-v1', 'v5.20/vfio/clear-caps-buf-v3', 'v5.20/vfio/remove-useless-judgement-v1' and 'v5.20/vfio/move-device_open-count-v2' into v5.20/vfio/next
7 parents 6e97eba + 3b498b6 + a13b1e4 + 1c61d51 + 6641085 + ffed051 + 330c179 commit 7654a88

File tree

2 files changed

+36
-32
lines changed

2 files changed

+36
-32
lines changed

drivers/vfio/vfio.c

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -504,7 +504,9 @@ static struct vfio_group *vfio_noiommu_group_alloc(struct device *dev,
504504
if (IS_ERR(iommu_group))
505505
return ERR_CAST(iommu_group);
506506

507-
iommu_group_set_name(iommu_group, "vfio-noiommu");
507+
ret = iommu_group_set_name(iommu_group, "vfio-noiommu");
508+
if (ret)
509+
goto out_put_group;
508510
ret = iommu_group_add_device(iommu_group, dev);
509511
if (ret)
510512
goto out_put_group;
@@ -605,7 +607,7 @@ int vfio_register_group_dev(struct vfio_device *device)
605607
* VFIO always sets IOMMU_CACHE because we offer no way for userspace to
606608
* restore cache coherency.
607609
*/
608-
if (!iommu_capable(device->dev->bus, IOMMU_CAP_CACHE_COHERENCY))
610+
if (!device_iommu_capable(device->dev, IOMMU_CAP_CACHE_COHERENCY))
609611
return -EINVAL;
610612

611613
return __vfio_register_dev(device,
@@ -1146,10 +1148,10 @@ static struct file *vfio_device_open(struct vfio_device *device)
11461148
if (device->open_count == 1 && device->ops->close_device)
11471149
device->ops->close_device(device);
11481150
err_undo_count:
1151+
up_read(&device->group->group_rwsem);
11491152
device->open_count--;
11501153
if (device->open_count == 0 && device->kvm)
11511154
device->kvm = NULL;
1152-
up_read(&device->group->group_rwsem);
11531155
mutex_unlock(&device->dev_set->lock);
11541156
module_put(device->dev->driver->owner);
11551157
err_unassign_container:
@@ -1811,6 +1813,7 @@ struct vfio_info_cap_header *vfio_info_cap_add(struct vfio_info_cap *caps,
18111813
buf = krealloc(caps->buf, caps->size + size, GFP_KERNEL);
18121814
if (!buf) {
18131815
kfree(caps->buf);
1816+
caps->buf = NULL;
18141817
caps->size = 0;
18151818
return ERR_PTR(-ENOMEM);
18161819
}
@@ -2155,13 +2158,17 @@ static int __init vfio_init(void)
21552158
if (ret)
21562159
goto err_alloc_chrdev;
21572160

2158-
pr_info(DRIVER_DESC " version: " DRIVER_VERSION "\n");
2159-
21602161
#ifdef CONFIG_VFIO_NOIOMMU
2161-
vfio_register_iommu_driver(&vfio_noiommu_ops);
2162+
ret = vfio_register_iommu_driver(&vfio_noiommu_ops);
21622163
#endif
2164+
if (ret)
2165+
goto err_driver_register;
2166+
2167+
pr_info(DRIVER_DESC " version: " DRIVER_VERSION "\n");
21632168
return 0;
21642169

2170+
err_driver_register:
2171+
unregister_chrdev_region(vfio.group_devt, MINORMASK + 1);
21652172
err_alloc_chrdev:
21662173
class_destroy(vfio.class);
21672174
vfio.class = NULL;

drivers/vfio/vfio_iommu_type1.c

Lines changed: 23 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1377,12 +1377,6 @@ static int vfio_dma_do_unmap(struct vfio_iommu *iommu,
13771377

13781378
if (!iommu->v2 && iova > dma->iova)
13791379
break;
1380-
/*
1381-
* Task with same address space who mapped this iova range is
1382-
* allowed to unmap the iova range.
1383-
*/
1384-
if (dma->task->mm != current->mm)
1385-
break;
13861380

13871381
if (invalidate_vaddr) {
13881382
if (dma->vaddr_invalid) {
@@ -1679,18 +1673,6 @@ static int vfio_dma_do_map(struct vfio_iommu *iommu,
16791673
return ret;
16801674
}
16811675

1682-
static int vfio_bus_type(struct device *dev, void *data)
1683-
{
1684-
struct bus_type **bus = data;
1685-
1686-
if (*bus && *bus != dev->bus)
1687-
return -EINVAL;
1688-
1689-
*bus = dev->bus;
1690-
1691-
return 0;
1692-
}
1693-
16941676
static int vfio_iommu_replay(struct vfio_iommu *iommu,
16951677
struct vfio_domain *domain)
16961678
{
@@ -2153,13 +2135,26 @@ static void vfio_iommu_iova_insert_copy(struct vfio_iommu *iommu,
21532135
list_splice_tail(iova_copy, iova);
21542136
}
21552137

2138+
/* Redundantly walks non-present capabilities to simplify caller */
2139+
static int vfio_iommu_device_capable(struct device *dev, void *data)
2140+
{
2141+
return device_iommu_capable(dev, (enum iommu_cap)data);
2142+
}
2143+
2144+
static int vfio_iommu_domain_alloc(struct device *dev, void *data)
2145+
{
2146+
struct iommu_domain **domain = data;
2147+
2148+
*domain = iommu_domain_alloc(dev->bus);
2149+
return 1; /* Don't iterate */
2150+
}
2151+
21562152
static int vfio_iommu_type1_attach_group(void *iommu_data,
21572153
struct iommu_group *iommu_group, enum vfio_group_type type)
21582154
{
21592155
struct vfio_iommu *iommu = iommu_data;
21602156
struct vfio_iommu_group *group;
21612157
struct vfio_domain *domain, *d;
2162-
struct bus_type *bus = NULL;
21632158
bool resv_msi, msi_remap;
21642159
phys_addr_t resv_msi_base = 0;
21652160
struct iommu_domain_geometry *geo;
@@ -2192,18 +2187,19 @@ static int vfio_iommu_type1_attach_group(void *iommu_data,
21922187
goto out_unlock;
21932188
}
21942189

2195-
/* Determine bus_type in order to allocate a domain */
2196-
ret = iommu_group_for_each_dev(iommu_group, &bus, vfio_bus_type);
2197-
if (ret)
2198-
goto out_free_group;
2199-
22002190
ret = -ENOMEM;
22012191
domain = kzalloc(sizeof(*domain), GFP_KERNEL);
22022192
if (!domain)
22032193
goto out_free_group;
22042194

2195+
/*
2196+
* Going via the iommu_group iterator avoids races, and trivially gives
2197+
* us a representative device for the IOMMU API call. We don't actually
2198+
* want to iterate beyond the first device (if any).
2199+
*/
22052200
ret = -EIO;
2206-
domain->domain = iommu_domain_alloc(bus);
2201+
iommu_group_for_each_dev(iommu_group, &domain->domain,
2202+
vfio_iommu_domain_alloc);
22072203
if (!domain->domain)
22082204
goto out_free_domain;
22092205

@@ -2258,7 +2254,8 @@ static int vfio_iommu_type1_attach_group(void *iommu_data,
22582254
list_add(&group->next, &domain->group_list);
22592255

22602256
msi_remap = irq_domain_check_msi_remap() ||
2261-
iommu_capable(bus, IOMMU_CAP_INTR_REMAP);
2257+
iommu_group_for_each_dev(iommu_group, (void *)IOMMU_CAP_INTR_REMAP,
2258+
vfio_iommu_device_capable);
22622259

22632260
if (!allow_unsafe_interrupts && !msi_remap) {
22642261
pr_warn("%s: No interrupt remapping support. Use the module param \"allow_unsafe_interrupts\" to enable VFIO IOMMU support on this platform\n",

0 commit comments

Comments
 (0)