Skip to content

Commit fd6b853

Browse files
nicolincjgunthorpe
authored andcommitted
iommufd/selftest: Add container_of helpers
Use these inline helpers to shorten those container_of lines. Note that one of them goes back and forth between iommu_domain and mock_iommu_domain, which isn't necessary. So drop its container_of. Link: https://patch.msgid.link/r/518ec64dae2e814eb29fd9f170f58a3aad56c81c.1730836219.git.nicolinc@nvidia.com Reviewed-by: Kevin Tian <[email protected]> Reviewed-by: Jason Gunthorpe <[email protected]> Signed-off-by: Nicolin Chen <[email protected]> Signed-off-by: Jason Gunthorpe <[email protected]>
1 parent 13a7501 commit fd6b853

File tree

1 file changed

+42
-33
lines changed

1 file changed

+42
-33
lines changed

drivers/iommu/iommufd/selftest.c

Lines changed: 42 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -126,12 +126,24 @@ struct mock_iommu_domain {
126126
struct xarray pfns;
127127
};
128128

129+
static inline struct mock_iommu_domain *
130+
to_mock_domain(struct iommu_domain *domain)
131+
{
132+
return container_of(domain, struct mock_iommu_domain, domain);
133+
}
134+
129135
struct mock_iommu_domain_nested {
130136
struct iommu_domain domain;
131137
struct mock_iommu_domain *parent;
132138
u32 iotlb[MOCK_NESTED_DOMAIN_IOTLB_NUM];
133139
};
134140

141+
static inline struct mock_iommu_domain_nested *
142+
to_mock_nested(struct iommu_domain *domain)
143+
{
144+
return container_of(domain, struct mock_iommu_domain_nested, domain);
145+
}
146+
135147
enum selftest_obj_type {
136148
TYPE_IDEV,
137149
};
@@ -142,6 +154,11 @@ struct mock_dev {
142154
int id;
143155
};
144156

157+
static inline struct mock_dev *to_mock_dev(struct device *dev)
158+
{
159+
return container_of(dev, struct mock_dev, dev);
160+
}
161+
145162
struct selftest_obj {
146163
struct iommufd_object obj;
147164
enum selftest_obj_type type;
@@ -155,10 +172,15 @@ struct selftest_obj {
155172
};
156173
};
157174

175+
static inline struct selftest_obj *to_selftest_obj(struct iommufd_object *obj)
176+
{
177+
return container_of(obj, struct selftest_obj, obj);
178+
}
179+
158180
static int mock_domain_nop_attach(struct iommu_domain *domain,
159181
struct device *dev)
160182
{
161-
struct mock_dev *mdev = container_of(dev, struct mock_dev, dev);
183+
struct mock_dev *mdev = to_mock_dev(dev);
162184

163185
if (domain->dirty_ops && (mdev->flags & MOCK_FLAGS_DEVICE_NO_DIRTY))
164186
return -EINVAL;
@@ -193,8 +215,7 @@ static void *mock_domain_hw_info(struct device *dev, u32 *length, u32 *type)
193215
static int mock_domain_set_dirty_tracking(struct iommu_domain *domain,
194216
bool enable)
195217
{
196-
struct mock_iommu_domain *mock =
197-
container_of(domain, struct mock_iommu_domain, domain);
218+
struct mock_iommu_domain *mock = to_mock_domain(domain);
198219
unsigned long flags = mock->flags;
199220

200221
if (enable && !domain->dirty_ops)
@@ -243,8 +264,7 @@ static int mock_domain_read_and_clear_dirty(struct iommu_domain *domain,
243264
unsigned long flags,
244265
struct iommu_dirty_bitmap *dirty)
245266
{
246-
struct mock_iommu_domain *mock =
247-
container_of(domain, struct mock_iommu_domain, domain);
267+
struct mock_iommu_domain *mock = to_mock_domain(domain);
248268
unsigned long end = iova + size;
249269
void *ent;
250270

@@ -281,7 +301,7 @@ static const struct iommu_dirty_ops dirty_ops = {
281301

282302
static struct iommu_domain *mock_domain_alloc_paging(struct device *dev)
283303
{
284-
struct mock_dev *mdev = container_of(dev, struct mock_dev, dev);
304+
struct mock_dev *mdev = to_mock_dev(dev);
285305
struct mock_iommu_domain *mock;
286306

287307
mock = kzalloc(sizeof(*mock), GFP_KERNEL);
@@ -327,7 +347,7 @@ mock_domain_alloc_user(struct device *dev, u32 flags,
327347

328348
/* must be mock_domain */
329349
if (!parent) {
330-
struct mock_dev *mdev = container_of(dev, struct mock_dev, dev);
350+
struct mock_dev *mdev = to_mock_dev(dev);
331351
bool has_dirty_flag = flags & IOMMU_HWPT_ALLOC_DIRTY_TRACKING;
332352
bool no_dirty_ops = mdev->flags & MOCK_FLAGS_DEVICE_NO_DIRTY;
333353
struct iommu_domain *domain;
@@ -341,8 +361,7 @@ mock_domain_alloc_user(struct device *dev, u32 flags,
341361
if (!domain)
342362
return ERR_PTR(-ENOMEM);
343363
if (has_dirty_flag)
344-
container_of(domain, struct mock_iommu_domain, domain)
345-
->domain.dirty_ops = &dirty_ops;
364+
domain->dirty_ops = &dirty_ops;
346365
return domain;
347366
}
348367

@@ -352,7 +371,7 @@ mock_domain_alloc_user(struct device *dev, u32 flags,
352371
if (!parent || parent->ops != mock_ops.default_domain_ops)
353372
return ERR_PTR(-EINVAL);
354373

355-
mock_parent = container_of(parent, struct mock_iommu_domain, domain);
374+
mock_parent = to_mock_domain(parent);
356375
if (!mock_parent)
357376
return ERR_PTR(-EINVAL);
358377

@@ -366,8 +385,7 @@ mock_domain_alloc_user(struct device *dev, u32 flags,
366385

367386
static void mock_domain_free(struct iommu_domain *domain)
368387
{
369-
struct mock_iommu_domain *mock =
370-
container_of(domain, struct mock_iommu_domain, domain);
388+
struct mock_iommu_domain *mock = to_mock_domain(domain);
371389

372390
WARN_ON(!xa_empty(&mock->pfns));
373391
kfree(mock);
@@ -378,8 +396,7 @@ static int mock_domain_map_pages(struct iommu_domain *domain,
378396
size_t pgsize, size_t pgcount, int prot,
379397
gfp_t gfp, size_t *mapped)
380398
{
381-
struct mock_iommu_domain *mock =
382-
container_of(domain, struct mock_iommu_domain, domain);
399+
struct mock_iommu_domain *mock = to_mock_domain(domain);
383400
unsigned long flags = MOCK_PFN_START_IOVA;
384401
unsigned long start_iova = iova;
385402

@@ -430,8 +447,7 @@ static size_t mock_domain_unmap_pages(struct iommu_domain *domain,
430447
size_t pgcount,
431448
struct iommu_iotlb_gather *iotlb_gather)
432449
{
433-
struct mock_iommu_domain *mock =
434-
container_of(domain, struct mock_iommu_domain, domain);
450+
struct mock_iommu_domain *mock = to_mock_domain(domain);
435451
bool first = true;
436452
size_t ret = 0;
437453
void *ent;
@@ -479,8 +495,7 @@ static size_t mock_domain_unmap_pages(struct iommu_domain *domain,
479495
static phys_addr_t mock_domain_iova_to_phys(struct iommu_domain *domain,
480496
dma_addr_t iova)
481497
{
482-
struct mock_iommu_domain *mock =
483-
container_of(domain, struct mock_iommu_domain, domain);
498+
struct mock_iommu_domain *mock = to_mock_domain(domain);
484499
void *ent;
485500

486501
WARN_ON(iova % MOCK_IO_PAGE_SIZE);
@@ -491,7 +506,7 @@ static phys_addr_t mock_domain_iova_to_phys(struct iommu_domain *domain,
491506

492507
static bool mock_domain_capable(struct device *dev, enum iommu_cap cap)
493508
{
494-
struct mock_dev *mdev = container_of(dev, struct mock_dev, dev);
509+
struct mock_dev *mdev = to_mock_dev(dev);
495510

496511
switch (cap) {
497512
case IOMMU_CAP_CACHE_COHERENCY:
@@ -571,18 +586,14 @@ static const struct iommu_ops mock_ops = {
571586

572587
static void mock_domain_free_nested(struct iommu_domain *domain)
573588
{
574-
struct mock_iommu_domain_nested *mock_nested =
575-
container_of(domain, struct mock_iommu_domain_nested, domain);
576-
577-
kfree(mock_nested);
589+
kfree(to_mock_nested(domain));
578590
}
579591

580592
static int
581593
mock_domain_cache_invalidate_user(struct iommu_domain *domain,
582594
struct iommu_user_data_array *array)
583595
{
584-
struct mock_iommu_domain_nested *mock_nested =
585-
container_of(domain, struct mock_iommu_domain_nested, domain);
596+
struct mock_iommu_domain_nested *mock_nested = to_mock_nested(domain);
586597
struct iommu_hwpt_invalidate_selftest inv;
587598
u32 processed = 0;
588599
int i = 0, j;
@@ -657,7 +668,7 @@ get_md_pagetable(struct iommufd_ucmd *ucmd, u32 mockpt_id,
657668
iommufd_put_object(ucmd->ictx, &hwpt->obj);
658669
return ERR_PTR(-EINVAL);
659670
}
660-
*mock = container_of(hwpt->domain, struct mock_iommu_domain, domain);
671+
*mock = to_mock_domain(hwpt->domain);
661672
return hwpt;
662673
}
663674

@@ -675,14 +686,13 @@ get_md_pagetable_nested(struct iommufd_ucmd *ucmd, u32 mockpt_id,
675686
iommufd_put_object(ucmd->ictx, &hwpt->obj);
676687
return ERR_PTR(-EINVAL);
677688
}
678-
*mock_nested = container_of(hwpt->domain,
679-
struct mock_iommu_domain_nested, domain);
689+
*mock_nested = to_mock_nested(hwpt->domain);
680690
return hwpt;
681691
}
682692

683693
static void mock_dev_release(struct device *dev)
684694
{
685-
struct mock_dev *mdev = container_of(dev, struct mock_dev, dev);
695+
struct mock_dev *mdev = to_mock_dev(dev);
686696

687697
ida_free(&mock_dev_ida, mdev->id);
688698
kfree(mdev);
@@ -813,7 +823,7 @@ static int iommufd_test_mock_domain_replace(struct iommufd_ucmd *ucmd,
813823
if (IS_ERR(dev_obj))
814824
return PTR_ERR(dev_obj);
815825

816-
sobj = container_of(dev_obj, struct selftest_obj, obj);
826+
sobj = to_selftest_obj(dev_obj);
817827
if (sobj->type != TYPE_IDEV) {
818828
rc = -EINVAL;
819829
goto out_dev_obj;
@@ -951,8 +961,7 @@ static int iommufd_test_md_check_iotlb(struct iommufd_ucmd *ucmd,
951961
if (IS_ERR(hwpt))
952962
return PTR_ERR(hwpt);
953963

954-
mock_nested = container_of(hwpt->domain,
955-
struct mock_iommu_domain_nested, domain);
964+
mock_nested = to_mock_nested(hwpt->domain);
956965

957966
if (iotlb_id > MOCK_NESTED_DOMAIN_IOTLB_ID_MAX ||
958967
mock_nested->iotlb[iotlb_id] != iotlb)
@@ -1431,7 +1440,7 @@ static int iommufd_test_trigger_iopf(struct iommufd_ucmd *ucmd,
14311440

14321441
void iommufd_selftest_destroy(struct iommufd_object *obj)
14331442
{
1434-
struct selftest_obj *sobj = container_of(obj, struct selftest_obj, obj);
1443+
struct selftest_obj *sobj = to_selftest_obj(obj);
14351444

14361445
switch (sobj->type) {
14371446
case TYPE_IDEV:

0 commit comments

Comments
 (0)