Skip to content

Commit 89db316

Browse files
nicolincjgunthorpe
authored andcommitted
iommufd: Derive iommufd_hwpt_paging from iommufd_hw_pagetable
To prepare for IOMMUFD_OBJ_HWPT_NESTED, derive struct iommufd_hwpt_paging from struct iommufd_hw_pagetable, by leaving the common members in struct iommufd_hw_pagetable. Add a __iommufd_object_alloc and to_hwpt_paging() helpers for the new structure. Then, update "hwpt" to "hwpt_paging" throughout the files, accordingly. Link: https://lore.kernel.org/r/20231026043938.63898-5-yi.l.liu@intel.com Suggested-by: Jason Gunthorpe <jgg@nvidia.com> Signed-off-by: Nicolin Chen <nicolinc@nvidia.com> Signed-off-by: Yi Liu <yi.l.liu@intel.com> Reviewed-by: Kevin Tian <kevin.tian@intel.com> Reviewed-by: Jason Gunthorpe <jgg@nvidia.com> Signed-off-by: Jason Gunthorpe <jgg@nvidia.com>
1 parent 58d84f4 commit 89db316

File tree

5 files changed

+148
-108
lines changed

5 files changed

+148
-108
lines changed

drivers/iommu/iommufd/device.c

Lines changed: 45 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -293,7 +293,7 @@ u32 iommufd_device_to_id(struct iommufd_device *idev)
293293
EXPORT_SYMBOL_NS_GPL(iommufd_device_to_id, IOMMUFD);
294294

295295
static int iommufd_group_setup_msi(struct iommufd_group *igroup,
296-
struct iommufd_hw_pagetable *hwpt)
296+
struct iommufd_hwpt_paging *hwpt_paging)
297297
{
298298
phys_addr_t sw_msi_start = igroup->sw_msi_start;
299299
int rc;
@@ -311,36 +311,39 @@ static int iommufd_group_setup_msi(struct iommufd_group *igroup,
311311
* matches what the IRQ layer actually expects in a newly created
312312
* domain.
313313
*/
314-
if (sw_msi_start != PHYS_ADDR_MAX && !hwpt->msi_cookie) {
315-
rc = iommu_get_msi_cookie(hwpt->domain, sw_msi_start);
314+
if (sw_msi_start != PHYS_ADDR_MAX && !hwpt_paging->msi_cookie) {
315+
rc = iommu_get_msi_cookie(hwpt_paging->common.domain,
316+
sw_msi_start);
316317
if (rc)
317318
return rc;
318319

319320
/*
320321
* iommu_get_msi_cookie() can only be called once per domain,
321322
* it returns -EBUSY on later calls.
322323
*/
323-
hwpt->msi_cookie = true;
324+
hwpt_paging->msi_cookie = true;
324325
}
325326
return 0;
326327
}
327328

328-
static int iommufd_hwpt_paging_attach(struct iommufd_hw_pagetable *hwpt,
329+
static int iommufd_hwpt_paging_attach(struct iommufd_hwpt_paging *hwpt_paging,
329330
struct iommufd_device *idev)
330331
{
331332
int rc;
332333

333334
lockdep_assert_held(&idev->igroup->lock);
334335

335-
rc = iopt_table_enforce_dev_resv_regions(&hwpt->ioas->iopt, idev->dev,
336+
rc = iopt_table_enforce_dev_resv_regions(&hwpt_paging->ioas->iopt,
337+
idev->dev,
336338
&idev->igroup->sw_msi_start);
337339
if (rc)
338340
return rc;
339341

340342
if (list_empty(&idev->igroup->device_list)) {
341-
rc = iommufd_group_setup_msi(idev->igroup, hwpt);
343+
rc = iommufd_group_setup_msi(idev->igroup, hwpt_paging);
342344
if (rc) {
343-
iopt_remove_reserved_iova(&hwpt->ioas->iopt, idev->dev);
345+
iopt_remove_reserved_iova(&hwpt_paging->ioas->iopt,
346+
idev->dev);
344347
return rc;
345348
}
346349
}
@@ -360,7 +363,7 @@ int iommufd_hw_pagetable_attach(struct iommufd_hw_pagetable *hwpt,
360363
}
361364

362365
if (hwpt_is_paging(hwpt)) {
363-
rc = iommufd_hwpt_paging_attach(hwpt, idev);
366+
rc = iommufd_hwpt_paging_attach(to_hwpt_paging(hwpt), idev);
364367
if (rc)
365368
goto err_unlock;
366369
}
@@ -384,7 +387,8 @@ int iommufd_hw_pagetable_attach(struct iommufd_hw_pagetable *hwpt,
384387
return 0;
385388
err_unresv:
386389
if (hwpt_is_paging(hwpt))
387-
iopt_remove_reserved_iova(&hwpt->ioas->iopt, idev->dev);
390+
iopt_remove_reserved_iova(&to_hwpt_paging(hwpt)->ioas->iopt,
391+
idev->dev);
388392
err_unlock:
389393
mutex_unlock(&idev->igroup->lock);
390394
return rc;
@@ -402,7 +406,8 @@ iommufd_hw_pagetable_detach(struct iommufd_device *idev)
402406
idev->igroup->hwpt = NULL;
403407
}
404408
if (hwpt_is_paging(hwpt))
405-
iopt_remove_reserved_iova(&hwpt->ioas->iopt, idev->dev);
409+
iopt_remove_reserved_iova(&to_hwpt_paging(hwpt)->ioas->iopt,
410+
idev->dev);
406411
mutex_unlock(&idev->igroup->lock);
407412

408413
/* Caller must destroy hwpt */
@@ -423,41 +428,43 @@ iommufd_device_do_attach(struct iommufd_device *idev,
423428

424429
static void
425430
iommufd_group_remove_reserved_iova(struct iommufd_group *igroup,
426-
struct iommufd_hw_pagetable *hwpt)
431+
struct iommufd_hwpt_paging *hwpt_paging)
427432
{
428433
struct iommufd_device *cur;
429434

430435
lockdep_assert_held(&igroup->lock);
431436

432437
list_for_each_entry(cur, &igroup->device_list, group_item)
433-
iopt_remove_reserved_iova(&hwpt->ioas->iopt, cur->dev);
438+
iopt_remove_reserved_iova(&hwpt_paging->ioas->iopt, cur->dev);
434439
}
435440

436-
static int iommufd_group_do_replace_paging(struct iommufd_group *igroup,
437-
struct iommufd_hw_pagetable *hwpt)
441+
static int
442+
iommufd_group_do_replace_paging(struct iommufd_group *igroup,
443+
struct iommufd_hwpt_paging *hwpt_paging)
438444
{
439445
struct iommufd_hw_pagetable *old_hwpt = igroup->hwpt;
440446
struct iommufd_device *cur;
441447
int rc;
442448

443449
lockdep_assert_held(&igroup->lock);
444450

445-
if (!hwpt_is_paging(old_hwpt) || hwpt->ioas != old_hwpt->ioas) {
451+
if (!hwpt_is_paging(old_hwpt) ||
452+
hwpt_paging->ioas != to_hwpt_paging(old_hwpt)->ioas) {
446453
list_for_each_entry(cur, &igroup->device_list, group_item) {
447454
rc = iopt_table_enforce_dev_resv_regions(
448-
&hwpt->ioas->iopt, cur->dev, NULL);
455+
&hwpt_paging->ioas->iopt, cur->dev, NULL);
449456
if (rc)
450457
goto err_unresv;
451458
}
452459
}
453460

454-
rc = iommufd_group_setup_msi(igroup, hwpt);
461+
rc = iommufd_group_setup_msi(igroup, hwpt_paging);
455462
if (rc)
456463
goto err_unresv;
457464
return 0;
458465

459466
err_unresv:
460-
iommufd_group_remove_reserved_iova(igroup, hwpt);
467+
iommufd_group_remove_reserved_iova(igroup, hwpt_paging);
461468
return rc;
462469
}
463470

@@ -482,8 +489,10 @@ iommufd_device_do_replace(struct iommufd_device *idev,
482489
return NULL;
483490
}
484491

492+
old_hwpt = igroup->hwpt;
485493
if (hwpt_is_paging(hwpt)) {
486-
rc = iommufd_group_do_replace_paging(igroup, hwpt);
494+
rc = iommufd_group_do_replace_paging(igroup,
495+
to_hwpt_paging(hwpt));
487496
if (rc)
488497
goto err_unlock;
489498
}
@@ -492,10 +501,11 @@ iommufd_device_do_replace(struct iommufd_device *idev,
492501
if (rc)
493502
goto err_unresv;
494503

495-
old_hwpt = igroup->hwpt;
496504
if (hwpt_is_paging(old_hwpt) &&
497-
(!hwpt_is_paging(hwpt) || hwpt->ioas != old_hwpt->ioas))
498-
iommufd_group_remove_reserved_iova(igroup, old_hwpt);
505+
(!hwpt_is_paging(hwpt) ||
506+
to_hwpt_paging(hwpt)->ioas != to_hwpt_paging(old_hwpt)->ioas))
507+
iommufd_group_remove_reserved_iova(igroup,
508+
to_hwpt_paging(old_hwpt));
499509

500510
igroup->hwpt = hwpt;
501511

@@ -514,7 +524,8 @@ iommufd_device_do_replace(struct iommufd_device *idev,
514524
return old_hwpt;
515525
err_unresv:
516526
if (hwpt_is_paging(hwpt))
517-
iommufd_group_remove_reserved_iova(igroup, hwpt);
527+
iommufd_group_remove_reserved_iova(igroup,
528+
to_hwpt_paging(old_hwpt));
518529
err_unlock:
519530
mutex_unlock(&idev->igroup->lock);
520531
return ERR_PTR(rc);
@@ -542,6 +553,7 @@ iommufd_device_auto_get_domain(struct iommufd_device *idev,
542553
*/
543554
bool immediate_attach = do_attach == iommufd_device_do_attach;
544555
struct iommufd_hw_pagetable *destroy_hwpt;
556+
struct iommufd_hwpt_paging *hwpt_paging;
545557
struct iommufd_hw_pagetable *hwpt;
546558

547559
/*
@@ -550,10 +562,11 @@ iommufd_device_auto_get_domain(struct iommufd_device *idev,
550562
* other.
551563
*/
552564
mutex_lock(&ioas->mutex);
553-
list_for_each_entry(hwpt, &ioas->hwpt_list, hwpt_item) {
554-
if (!hwpt->auto_domain)
565+
list_for_each_entry(hwpt_paging, &ioas->hwpt_list, hwpt_item) {
566+
if (!hwpt_paging->auto_domain)
555567
continue;
556568

569+
hwpt = &hwpt_paging->common;
557570
if (!iommufd_lock_obj(&hwpt->obj))
558571
continue;
559572
destroy_hwpt = (*do_attach)(idev, hwpt);
@@ -574,12 +587,13 @@ iommufd_device_auto_get_domain(struct iommufd_device *idev,
574587
goto out_unlock;
575588
}
576589

577-
hwpt = iommufd_hw_pagetable_alloc(idev->ictx, ioas, idev,
578-
0, immediate_attach);
579-
if (IS_ERR(hwpt)) {
580-
destroy_hwpt = ERR_CAST(hwpt);
590+
hwpt_paging = iommufd_hwpt_paging_alloc(idev->ictx, ioas, idev, 0,
591+
immediate_attach);
592+
if (IS_ERR(hwpt_paging)) {
593+
destroy_hwpt = ERR_CAST(hwpt_paging);
581594
goto out_unlock;
582595
}
596+
hwpt = &hwpt_paging->common;
583597

584598
if (!immediate_attach) {
585599
destroy_hwpt = (*do_attach)(idev, hwpt);
@@ -589,7 +603,7 @@ iommufd_device_auto_get_domain(struct iommufd_device *idev,
589603
destroy_hwpt = NULL;
590604
}
591605

592-
hwpt->auto_domain = true;
606+
hwpt_paging->auto_domain = true;
593607
*pt_id = hwpt->obj.id;
594608

595609
iommufd_object_finalize(idev->ictx, &hwpt->obj);

0 commit comments

Comments
 (0)