Skip to content

Commit 3f6634d

Browse files
LuBaolujoergroedel
authored andcommitted
iommu: Use right way to retrieve iommu_ops
The common iommu_ops is hooked to both device and domain. When a helper has both device and domain pointer, the way to get the iommu_ops looks messy in iommu core. This sorts out the way to get iommu_ops. The device related helpers go through device pointer, while the domain related ones go through domain pointer. Signed-off-by: Lu Baolu <[email protected]> Reviewed-by: Christoph Hellwig <[email protected]> Reviewed-by: Jason Gunthorpe <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Joerg Roedel <[email protected]>
1 parent 7eef7f6 commit 3f6634d

File tree

2 files changed

+36
-25
lines changed

2 files changed

+36
-25
lines changed

drivers/iommu/iommu.c

Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -323,13 +323,14 @@ int iommu_probe_device(struct device *dev)
323323

324324
void iommu_release_device(struct device *dev)
325325
{
326-
const struct iommu_ops *ops = dev->bus->iommu_ops;
326+
const struct iommu_ops *ops;
327327

328328
if (!dev->iommu)
329329
return;
330330

331331
iommu_device_unlink(dev->iommu->iommu_dev, dev);
332332

333+
ops = dev_iommu_ops(dev);
333334
ops->release_device(dev);
334335

335336
iommu_group_remove_device(dev);
@@ -833,8 +834,10 @@ static int iommu_create_device_direct_mappings(struct iommu_group *group,
833834
static bool iommu_is_attach_deferred(struct iommu_domain *domain,
834835
struct device *dev)
835836
{
836-
if (domain->ops->is_attach_deferred)
837-
return domain->ops->is_attach_deferred(domain, dev);
837+
const struct iommu_ops *ops = dev_iommu_ops(dev);
838+
839+
if (ops->is_attach_deferred)
840+
return ops->is_attach_deferred(domain, dev);
838841

839842
return false;
840843
}
@@ -1252,10 +1255,10 @@ int iommu_page_response(struct device *dev,
12521255
struct iommu_fault_event *evt;
12531256
struct iommu_fault_page_request *prm;
12541257
struct dev_iommu *param = dev->iommu;
1258+
const struct iommu_ops *ops = dev_iommu_ops(dev);
12551259
bool has_pasid = msg->flags & IOMMU_PAGE_RESP_PASID_VALID;
1256-
struct iommu_domain *domain = iommu_get_domain_for_dev(dev);
12571260

1258-
if (!domain || !domain->ops->page_response)
1261+
if (!ops->page_response)
12591262
return -ENODEV;
12601263

12611264
if (!param || !param->fault_param)
@@ -1296,7 +1299,7 @@ int iommu_page_response(struct device *dev,
12961299
msg->pasid = 0;
12971300
}
12981301

1299-
ret = domain->ops->page_response(dev, evt, msg);
1302+
ret = ops->page_response(dev, evt, msg);
13001303
list_del(&evt->list);
13011304
kfree(evt);
13021305
break;
@@ -1521,7 +1524,7 @@ EXPORT_SYMBOL_GPL(fsl_mc_device_group);
15211524

15221525
static int iommu_get_def_domain_type(struct device *dev)
15231526
{
1524-
const struct iommu_ops *ops = dev->bus->iommu_ops;
1527+
const struct iommu_ops *ops = dev_iommu_ops(dev);
15251528

15261529
if (dev_is_pci(dev) && to_pci_dev(dev)->untrusted)
15271530
return IOMMU_DOMAIN_DMA;
@@ -1580,17 +1583,14 @@ static int iommu_alloc_default_domain(struct iommu_group *group,
15801583
*/
15811584
static struct iommu_group *iommu_group_get_for_dev(struct device *dev)
15821585
{
1583-
const struct iommu_ops *ops = dev->bus->iommu_ops;
1586+
const struct iommu_ops *ops = dev_iommu_ops(dev);
15841587
struct iommu_group *group;
15851588
int ret;
15861589

15871590
group = iommu_group_get(dev);
15881591
if (group)
15891592
return group;
15901593

1591-
if (!ops)
1592-
return ERR_PTR(-EINVAL);
1593-
15941594
group = ops->device_group(dev);
15951595
if (WARN_ON_ONCE(group == NULL))
15961596
return ERR_PTR(-EINVAL);
@@ -1759,10 +1759,10 @@ static int __iommu_group_dma_attach(struct iommu_group *group)
17591759

17601760
static int iommu_group_do_probe_finalize(struct device *dev, void *data)
17611761
{
1762-
struct iommu_domain *domain = data;
1762+
const struct iommu_ops *ops = dev_iommu_ops(dev);
17631763

1764-
if (domain->ops->probe_finalize)
1765-
domain->ops->probe_finalize(dev);
1764+
if (ops->probe_finalize)
1765+
ops->probe_finalize(dev);
17661766

17671767
return 0;
17681768
}
@@ -2020,7 +2020,7 @@ EXPORT_SYMBOL_GPL(iommu_attach_device);
20202020

20212021
int iommu_deferred_attach(struct device *dev, struct iommu_domain *domain)
20222022
{
2023-
const struct iommu_ops *ops = domain->ops;
2023+
const struct iommu_ops *ops = dev_iommu_ops(dev);
20242024

20252025
if (ops->is_attach_deferred && ops->is_attach_deferred(domain, dev))
20262026
return __iommu_attach_device(domain, dev);
@@ -2579,17 +2579,17 @@ EXPORT_SYMBOL_GPL(iommu_set_pgtable_quirks);
25792579

25802580
void iommu_get_resv_regions(struct device *dev, struct list_head *list)
25812581
{
2582-
const struct iommu_ops *ops = dev->bus->iommu_ops;
2582+
const struct iommu_ops *ops = dev_iommu_ops(dev);
25832583

2584-
if (ops && ops->get_resv_regions)
2584+
if (ops->get_resv_regions)
25852585
ops->get_resv_regions(dev, list);
25862586
}
25872587

25882588
void iommu_put_resv_regions(struct device *dev, struct list_head *list)
25892589
{
2590-
const struct iommu_ops *ops = dev->bus->iommu_ops;
2590+
const struct iommu_ops *ops = dev_iommu_ops(dev);
25912591

2592-
if (ops && ops->put_resv_regions)
2592+
if (ops->put_resv_regions)
25932593
ops->put_resv_regions(dev, list);
25942594
}
25952595

@@ -2794,9 +2794,9 @@ iommu_sva_bind_device(struct device *dev, struct mm_struct *mm, void *drvdata)
27942794
{
27952795
struct iommu_group *group;
27962796
struct iommu_sva *handle = ERR_PTR(-EINVAL);
2797-
const struct iommu_ops *ops = dev->bus->iommu_ops;
2797+
const struct iommu_ops *ops = dev_iommu_ops(dev);
27982798

2799-
if (!ops || !ops->sva_bind)
2799+
if (!ops->sva_bind)
28002800
return ERR_PTR(-ENODEV);
28012801

28022802
group = iommu_group_get(dev);
@@ -2837,9 +2837,9 @@ void iommu_sva_unbind_device(struct iommu_sva *handle)
28372837
{
28382838
struct iommu_group *group;
28392839
struct device *dev = handle->dev;
2840-
const struct iommu_ops *ops = dev->bus->iommu_ops;
2840+
const struct iommu_ops *ops = dev_iommu_ops(dev);
28412841

2842-
if (!ops || !ops->sva_unbind)
2842+
if (!ops->sva_unbind)
28432843
return;
28442844

28452845
group = iommu_group_get(dev);
@@ -2856,9 +2856,9 @@ EXPORT_SYMBOL_GPL(iommu_sva_unbind_device);
28562856

28572857
u32 iommu_sva_get_pasid(struct iommu_sva *handle)
28582858
{
2859-
const struct iommu_ops *ops = handle->dev->bus->iommu_ops;
2859+
const struct iommu_ops *ops = dev_iommu_ops(handle->dev);
28602860

2861-
if (!ops || !ops->sva_get_pasid)
2861+
if (!ops->sva_get_pasid)
28622862
return IOMMU_PASID_INVALID;
28632863

28642864
return ops->sva_get_pasid(handle);

include/linux/iommu.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -381,6 +381,17 @@ static inline void iommu_iotlb_gather_init(struct iommu_iotlb_gather *gather)
381381
};
382382
}
383383

384+
static inline const struct iommu_ops *dev_iommu_ops(struct device *dev)
385+
{
386+
/*
387+
* Assume that valid ops must be installed if iommu_probe_device()
388+
* has succeeded. The device ops are essentially for internal use
389+
* within the IOMMU subsystem itself, so we should be able to trust
390+
* ourselves not to misuse the helper.
391+
*/
392+
return dev->iommu->iommu_dev->ops;
393+
}
394+
384395
#define IOMMU_GROUP_NOTIFY_ADD_DEVICE 1 /* Device added */
385396
#define IOMMU_GROUP_NOTIFY_DEL_DEVICE 2 /* Pre Device removed */
386397
#define IOMMU_GROUP_NOTIFY_BIND_DRIVER 3 /* Pre Driver bind */

0 commit comments

Comments
 (0)