Skip to content

Commit 3832862

Browse files
rmurphy-armjoergroedel
authored andcommitted
iommu: Keep dev->iommu state consistent
At the moment, if of_iommu_configure() allocates dev->iommu itself via iommu_fwspec_init(), then suffers a DT parsing failure, it cleans up the fwspec but leaves the empty dev_iommu hanging around. So far this is benign (if a tiny bit wasteful), but we'd like to be able to reason about dev->iommu having a consistent and unambiguous lifecycle. Thus make sure that the of_iommu cleanup undoes precisely whatever it did. Signed-off-by: Robin Murphy <[email protected]> Reviewed-by: Jason Gunthorpe <[email protected]> Link: https://lore.kernel.org/r/d219663a3f23001f23d520a883ac622d70b4e642.1740753261.git.robin.murphy@arm.com Signed-off-by: Joerg Roedel <[email protected]>
1 parent fd598f7 commit 3832862

File tree

3 files changed

+8
-2
lines changed

3 files changed

+8
-2
lines changed

drivers/iommu/iommu-priv.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ static inline const struct iommu_ops *dev_iommu_ops(struct device *dev)
1717
return dev->iommu->iommu_dev->ops;
1818
}
1919

20+
void dev_iommu_free(struct device *dev);
21+
2022
const struct iommu_ops *iommu_ops_from_fwnode(const struct fwnode_handle *fwnode);
2123

2224
static inline const struct iommu_ops *iommu_fwspec_ops(struct iommu_fwspec *fwspec)

drivers/iommu/iommu.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -355,7 +355,7 @@ static struct dev_iommu *dev_iommu_get(struct device *dev)
355355
return param;
356356
}
357357

358-
static void dev_iommu_free(struct device *dev)
358+
void dev_iommu_free(struct device *dev)
359359
{
360360
struct dev_iommu *param = dev->iommu;
361361

drivers/iommu/of_iommu.c

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,7 @@ static void of_pci_check_device_ats(struct device *dev, struct device_node *np)
116116
int of_iommu_configure(struct device *dev, struct device_node *master_np,
117117
const u32 *id)
118118
{
119+
bool dev_iommu_present;
119120
int err;
120121

121122
if (!master_np)
@@ -127,6 +128,7 @@ int of_iommu_configure(struct device *dev, struct device_node *master_np,
127128
mutex_unlock(&iommu_probe_device_lock);
128129
return 0;
129130
}
131+
dev_iommu_present = dev->iommu;
130132

131133
/*
132134
* We don't currently walk up the tree looking for a parent IOMMU.
@@ -147,8 +149,10 @@ int of_iommu_configure(struct device *dev, struct device_node *master_np,
147149
err = of_iommu_configure_device(master_np, dev, id);
148150
}
149151

150-
if (err)
152+
if (err && dev_iommu_present)
151153
iommu_fwspec_free(dev);
154+
else if (err && dev->iommu)
155+
dev_iommu_free(dev);
152156
mutex_unlock(&iommu_probe_device_lock);
153157

154158
if (!err && dev->bus)

0 commit comments

Comments
 (0)