Skip to contentSkip to conversation

iommufd/selftest: Add missing driver_ops for AMDV1 mock domain

PATCH In review Pranjal Shrivastava 3 participants 5 messages Mar 25 – Mar 27

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 iommufd/selftest: Add missing driver_ops for AMDV1 mock domain

syzbot found that allocating a mock domain with AMDV1 format could cause a WARN_ON because the selftest enabled DYNAMIC_TOP without providing the required driver_ops.

Initialize a spinlock (top_lock) and dummy callbacks in the mock driver to satisfy the generic PT requirements.

Fixes dcd6a011a8d5 ("iommupt: Add map_pages op")
Reported-by [email protected]
---
 drivers/iommu/iommufd/selftest.c | 23 +++++++++++++++++++++++
 1 file changed, 23 insertions(+)
drivers/iommu/iommufd/selftest.c +23 -0
index 9607416f8069..960c5aee4a09 100644
@@ -119,6 +119,7 @@ struct mock_iommu_domain {
 		struct pt_iommu_amdv1 amdv1;
 	};
 	unsigned long flags;
+	spinlock_t top_lock;
 };
 PT_IOMMU_CHECK_DOMAIN(struct mock_iommu_domain, iommu, domain);
 PT_IOMMU_CHECK_DOMAIN(struct mock_iommu_domain, amdv1.iommu, domain);
@@ -129,6 +130,26 @@ to_mock_domain(struct iommu_domain *domain)
 	return container_of(domain, struct mock_iommu_domain, domain);
 }
 
+static void mock_domain_change_top(struct pt_iommu *iommu_table,
+				   phys_addr_t top_paddr,
+				   unsigned int top_level)
+{
+	/* The selftest doesn't have real hardware */
+}
+
+static spinlock_t *mock_domain_get_top_lock(struct pt_iommu *iommu_table)
+{
+	struct mock_iommu_domain *mock =
+		container_of(iommu_table, struct mock_iommu_domain, iommu);
+
+	return &mock->top_lock;
+}
+
+static const struct pt_iommu_driver_ops mock_driver_ops = {
+	.change_top = &mock_domain_change_top,
+	.get_top_lock = &mock_domain_get_top_lock,
+};
+
 struct mock_iommu_domain_nested {
 	struct iommu_domain domain;
 	struct mock_viommu *mock_viommu;
@@ -445,6 +466,8 @@ mock_domain_alloc_pgtable(struct device *dev,
 	if (!mock)
 		return ERR_PTR(-ENOMEM);
 	mock->domain.type = IOMMU_DOMAIN_UNMANAGED;
+	spin_lock_init(&mock->top_lock);
+	mock->amdv1.iommu.driver_ops = &mock_driver_ops;
 
 	mock->amdv1.iommu.nid = NUMA_NO_NODE;
 
Signature · 1 line
2.53.0.1018.g2bb0e51243-goog
Reviewed-by on v1 1/1
#1 lore raw
Quoted 66 lines · Pranjal Shrivastava

syzbot found that allocating a mock domain with AMDV1 format could cause a WARN_ON because the selftest enabled DYNAMIC_TOP without providing the required driver_ops.

Initialize a spinlock (top_lock) and dummy callbacks in the mock driver to satisfy the generic PT requirements.

Fixes dcd6a011a8d5 ("iommupt: Add map_pages op")
Reported-by [email protected]

---
drivers/iommu/iommufd/selftest.c | 23 +++++++++++++++++++++++

1 file changed, 23 insertions(+)
drivers/iommu/iommufd/selftest.c +23 -0
index 9607416f8069..960c5aee4a09 100644
@@ -119,6 +119,7 @@ struct mock_iommu_domain {
 		struct pt_iommu_amdv1 amdv1;
 	};
 	unsigned long flags;
+	spinlock_t top_lock;
 };
 PT_IOMMU_CHECK_DOMAIN(struct mock_iommu_domain, iommu, domain);
 PT_IOMMU_CHECK_DOMAIN(struct mock_iommu_domain, amdv1.iommu, domain);
@@ -129,6 +130,26 @@ to_mock_domain(struct iommu_domain *domain)
 	return container_of(domain, struct mock_iommu_domain, domain);
 }
 
+static void mock_domain_change_top(struct pt_iommu *iommu_table,
+				   phys_addr_t top_paddr,
+				   unsigned int top_level)
+{
+	/* The selftest doesn't have real hardware */
+}
+
+static spinlock_t *mock_domain_get_top_lock(struct pt_iommu *iommu_table)
+{
+	struct mock_iommu_domain *mock =
+		container_of(iommu_table, struct mock_iommu_domain, iommu);
+
+	return &mock->top_lock;
+}
+
+static const struct pt_iommu_driver_ops mock_driver_ops = {
+	.change_top = &mock_domain_change_top,
+	.get_top_lock = &mock_domain_get_top_lock,
+};
+
 struct mock_iommu_domain_nested {
 	struct iommu_domain domain;
 	struct mock_viommu *mock_viommu;
@@ -445,6 +466,8 @@ mock_domain_alloc_pgtable(struct device *dev,
 	if (!mock)
 		return ERR_PTR(-ENOMEM);
 	mock->domain.type = IOMMU_DOMAIN_UNMANAGED;
+	spin_lock_init(&mock->top_lock);
+	mock->amdv1.iommu.driver_ops = &mock_driver_ops;
 
 	mock->amdv1.iommu.nid = NUMA_NO_NODE;
 
Signature · 1 line
2.53.0.1018.g2bb0e51243-goog
#2 lore raw
In reply to Pranjal Shrivastava
Quoted 6 lines · Pranjal Shrivastava

syzbot found that allocating a mock domain with AMDV1 format could cause a WARN_ON because the selftest enabled DYNAMIC_TOP without providing the required driver_ops.

Initialize a spinlock (top_lock) and dummy callbacks in the mock driver to satisfy the generic PT requirements.

I'm inclined to delete the AMDv1 option entirely, I was thinking about extending the sefltest to use it but I don't have time..

Syzkaller is hitting this because it found it can use a parameter that the selftest never uses.

Jason

#3 lore raw
Quoted 11 lines · Jason Gunthorpe
Quoted 6 lines · Pranjal Shrivastava

syzbot found that allocating a mock domain with AMDV1 format could cause a WARN_ON because the selftest enabled DYNAMIC_TOP without providing the required driver_ops.

Initialize a spinlock (top_lock) and dummy callbacks in the mock driver to satisfy the generic PT requirements.

I'm inclined to delete the AMDv1 option entirely, I was thinking about extending the sefltest to use it but I don't have time..

Ack. so should we entirely remove MOCK_IOMMUPT_AMDV1? as in from the uAPI header as well?

Quoted 4 lines

Syzkaller is hitting this because it found it can use a parameter that the selftest never uses.

Jason

Thanks,
Praan

#4 lore raw
Quoted 9 lines · Pranjal Shrivastava
Quoted 8 lines · Jason Gunthorpe
Quoted 6 lines · Pranjal Shrivastava

syzbot found that allocating a mock domain with AMDV1 format could cause a WARN_ON because the selftest enabled DYNAMIC_TOP without providing the required driver_ops.

Initialize a spinlock (top_lock) and dummy callbacks in the mock driver to satisfy the generic PT requirements.

Quoted 6 lines
Quoted 3 lines

I'm inclined to delete the AMDv1 option entirely, I was thinking about extending the sefltest to use it but I don't have time..

Ack. so should we entirely remove MOCK_IOMMUPT_AMDV1? as in from the uAPI header as well?

Yes

Jason