@@ -533,14 +533,17 @@ static bool mock_domain_capable(struct device *dev, enum iommu_cap cap)
533
533
534
534
static struct iopf_queue * mock_iommu_iopf_queue ;
535
535
536
- static struct iommu_device mock_iommu_device = {
537
- };
536
+ static struct mock_iommu_device {
537
+ struct iommu_device iommu_dev ;
538
+ struct completion complete ;
539
+ refcount_t users ;
540
+ } mock_iommu ;
538
541
539
542
static struct iommu_device * mock_probe_device (struct device * dev )
540
543
{
541
544
if (dev -> bus != & iommufd_mock_bus_type .bus )
542
545
return ERR_PTR (- ENODEV );
543
- return & mock_iommu_device ;
546
+ return & mock_iommu . iommu_dev ;
544
547
}
545
548
546
549
static void mock_domain_page_response (struct device * dev , struct iopf_fault * evt ,
@@ -1556,24 +1559,27 @@ int __init iommufd_test_init(void)
1556
1559
if (rc )
1557
1560
goto err_platform ;
1558
1561
1559
- rc = iommu_device_sysfs_add (& mock_iommu_device ,
1562
+ rc = iommu_device_sysfs_add (& mock_iommu . iommu_dev ,
1560
1563
& selftest_iommu_dev -> dev , NULL , "%s" ,
1561
1564
dev_name (& selftest_iommu_dev -> dev ));
1562
1565
if (rc )
1563
1566
goto err_bus ;
1564
1567
1565
- rc = iommu_device_register_bus (& mock_iommu_device , & mock_ops ,
1568
+ rc = iommu_device_register_bus (& mock_iommu . iommu_dev , & mock_ops ,
1566
1569
& iommufd_mock_bus_type .bus ,
1567
1570
& iommufd_mock_bus_type .nb );
1568
1571
if (rc )
1569
1572
goto err_sysfs ;
1570
1573
1574
+ refcount_set (& mock_iommu .users , 1 );
1575
+ init_completion (& mock_iommu .complete );
1576
+
1571
1577
mock_iommu_iopf_queue = iopf_queue_alloc ("mock-iopfq" );
1572
1578
1573
1579
return 0 ;
1574
1580
1575
1581
err_sysfs :
1576
- iommu_device_sysfs_remove (& mock_iommu_device );
1582
+ iommu_device_sysfs_remove (& mock_iommu . iommu_dev );
1577
1583
err_bus :
1578
1584
bus_unregister (& iommufd_mock_bus_type .bus );
1579
1585
err_platform :
@@ -1583,15 +1589,32 @@ int __init iommufd_test_init(void)
1583
1589
return rc ;
1584
1590
}
1585
1591
1592
+ static void iommufd_test_wait_for_users (void )
1593
+ {
1594
+ if (refcount_dec_and_test (& mock_iommu .users ))
1595
+ return ;
1596
+ /*
1597
+ * Time out waiting for iommu device user count to become 0.
1598
+ *
1599
+ * Note that this is just making an example here, since the selftest is
1600
+ * built into the iommufd module, i.e. it only unplugs the iommu device
1601
+ * when unloading the module. So, it is expected that this WARN_ON will
1602
+ * not trigger, as long as any iommufd FDs are open.
1603
+ */
1604
+ WARN_ON (!wait_for_completion_timeout (& mock_iommu .complete ,
1605
+ msecs_to_jiffies (10000 )));
1606
+ }
1607
+
1586
1608
void iommufd_test_exit (void )
1587
1609
{
1588
1610
if (mock_iommu_iopf_queue ) {
1589
1611
iopf_queue_free (mock_iommu_iopf_queue );
1590
1612
mock_iommu_iopf_queue = NULL ;
1591
1613
}
1592
1614
1593
- iommu_device_sysfs_remove (& mock_iommu_device );
1594
- iommu_device_unregister_bus (& mock_iommu_device ,
1615
+ iommufd_test_wait_for_users ();
1616
+ iommu_device_sysfs_remove (& mock_iommu .iommu_dev );
1617
+ iommu_device_unregister_bus (& mock_iommu .iommu_dev ,
1595
1618
& iommufd_mock_bus_type .bus ,
1596
1619
& iommufd_mock_bus_type .nb );
1597
1620
bus_unregister (& iommufd_mock_bus_type .bus );
0 commit comments