Skip to contentSkip to conversation

iommu: Free fwspec with the dev_iommu object

PATCH In review Jean-Philippe Brucker 2 participants 2 messages Apr 22, 2020 – May 1, 2020 IOMMU

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 iommu: Free fwspec with the dev_iommu object

Kmemleak reports that iommu_fwspec is leaked during device probe deferral. For example:

1. The first device probe allocates a dev_iommu and a fwspec, then returns with -EPROBE_DEFER (because the IOMMU hasn't been probed yet).

2. The IOMMU driver registers the IOMMU on the PCI bus, which calls iommu_probe_device(). However at this point the device doesn't have IOMMU ops associated, so add_device() fails and iommu_probe_device() calls dev_iommu_free(), which leaks the fwspec allocated in 1.

3. The deferred device probe allocates a new dev_iommu and fwspec, before calling iommu_probe_device() which succeeds this time.

Fix this leak by freeing the fwspec in dev_iommu_free().

Fixes 72acd9df18f1 ("iommu: Move iommu_fwspec to struct dev_iommu")
---
 drivers/iommu/iommu.c | 1 +
 1 file changed, 1 insertion(+)
drivers/iommu/iommu.c +1 -0
index 2b471419e26c3..54757c404866c 100644
@@ -170,6 +170,7 @@ static struct dev_iommu *dev_iommu_get(struct device *dev)
 
 static void dev_iommu_free(struct device *dev)
 {
+	iommu_fwspec_free(dev);
 	kfree(dev->iommu);
 	dev->iommu = NULL;
 }
Signature · 6 lines
2.26.0

_______________________________________________
iommu mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/iommu
maintainer
#1 lore raw

Hi Jean-Philippe,

Quoted 13 lines · Jean-Philippe Brucker

Fix this leak by freeing the fwspec in dev_iommu_free().

Fixes 72acd9df18f1 ("iommu: Move iommu_fwspec to struct dev_iommu")
---
 drivers/iommu/iommu.c | 1 +
 1 file changed, 1 insertion(+)
drivers/iommu/iommu.c +0 -0
index 2b471419e26c3..54757c404866c 100644

drivers/iommu/iommu.c static struct dev_iommu *dev_iommu_get(struct device *dev) · line 176

Quoted 7 lines
drivers/iommu/iommu.c +1 -0
@@ -170,6 +170,7 @@ static struct dev_iommu *dev_iommu_get(struct device *dev)
 
 static void dev_iommu_free(struct device *dev)
 {
+	iommu_fwspec_free(dev);
 	kfree(dev->iommu);
 	dev->iommu = NULL;
 }

Sorry, but [1] is already queued.

Thanks,

Joerg

[1] https://lore.kernel.org/r/[email protected]

_______________________________________________
iommu mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/iommu