Skip to content

Commit 25f003d

Browse files
willdeaconjoergroedel
authored andcommitted
drivers/iommu: Take a ref to the IOMMU driver prior to ->add_device()
To avoid accidental removal of an active IOMMU driver module, take a reference to the driver module in 'iommu_probe_device()' immediately prior to invoking the '->add_device()' callback and hold it until the after the device has been removed by '->remove_device()'. Suggested-by: Joerg Roedel <joro@8bytes.org> Signed-off-by: Will Deacon <will@kernel.org> Tested-by: John Garry <john.garry@huawei.com> # smmu v3 Reviewed-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org> Signed-off-by: Joerg Roedel <jroedel@suse.de>
1 parent 1a373a7 commit 25f003d

File tree

2 files changed

+20
-3
lines changed

2 files changed

+20
-3
lines changed

drivers/iommu/iommu.c

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
#include <linux/bitops.h>
2323
#include <linux/property.h>
2424
#include <linux/fsl/mc.h>
25+
#include <linux/module.h>
2526
#include <trace/events/iommu.h>
2627

2728
static struct kset *iommu_group_kset;
@@ -185,10 +186,21 @@ int iommu_probe_device(struct device *dev)
185186
if (!iommu_get_dev_param(dev))
186187
return -ENOMEM;
187188

189+
if (!try_module_get(ops->owner)) {
190+
ret = -EINVAL;
191+
goto err_free_dev_param;
192+
}
193+
188194
ret = ops->add_device(dev);
189195
if (ret)
190-
iommu_free_dev_param(dev);
196+
goto err_module_put;
197+
198+
return 0;
191199

200+
err_module_put:
201+
module_put(ops->owner);
202+
err_free_dev_param:
203+
iommu_free_dev_param(dev);
192204
return ret;
193205
}
194206

@@ -199,7 +211,10 @@ void iommu_release_device(struct device *dev)
199211
if (dev->iommu_group)
200212
ops->remove_device(dev);
201213

202-
iommu_free_dev_param(dev);
214+
if (dev->iommu_param) {
215+
module_put(ops->owner);
216+
iommu_free_dev_param(dev);
217+
}
203218
}
204219

205220
static struct iommu_domain *__iommu_domain_alloc(struct bus_type *bus,

include/linux/iommu.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -246,9 +246,10 @@ struct iommu_iotlb_gather {
246246
* @sva_get_pasid: Get PASID associated to a SVA handle
247247
* @page_response: handle page request response
248248
* @cache_invalidate: invalidate translation caches
249-
* @pgsize_bitmap: bitmap of all possible supported page sizes
250249
* @sva_bind_gpasid: bind guest pasid and mm
251250
* @sva_unbind_gpasid: unbind guest pasid and mm
251+
* @pgsize_bitmap: bitmap of all possible supported page sizes
252+
* @owner: Driver module providing these ops
252253
*/
253254
struct iommu_ops {
254255
bool (*capable)(enum iommu_cap);
@@ -318,6 +319,7 @@ struct iommu_ops {
318319
int (*sva_unbind_gpasid)(struct device *dev, int pasid);
319320

320321
unsigned long pgsize_bitmap;
322+
struct module *owner;
321323
};
322324

323325
/**

0 commit comments

Comments
 (0)