[v6,4/6] iommufd/selftest: Add mock_domain_cache_invalidate_user support

Message ID 20231117130717.19875-5-yi.l.liu@intel.com
State New
Headers
Series iommufd: Add nesting infrastructure (part 2/2) |

Commit Message

Yi Liu Nov. 17, 2023, 1:07 p.m. UTC
  From: Nicolin Chen <nicolinc@nvidia.com>

Add mock_domain_cache_invalidate_user() data structure to support user
space selftest program to cover user cache invalidation pathway.

Signed-off-by: Nicolin Chen <nicolinc@nvidia.com>
Signed-off-by: Yi Liu <yi.l.liu@intel.com>
---
 drivers/iommu/iommufd/iommufd_test.h | 17 +++++++++++
 drivers/iommu/iommufd/selftest.c     | 43 ++++++++++++++++++++++++++++
 2 files changed, 60 insertions(+)
  

Comments

Jason Gunthorpe Dec. 6, 2023, 6:16 p.m. UTC | #1
On Fri, Nov 17, 2023 at 05:07:15AM -0800, Yi Liu wrote:
> +static int
> +mock_domain_cache_invalidate_user(struct iommu_domain *domain,
> +				  struct iommu_user_data_array *array,
> +				  u32 *error_code)
> +{
> +	struct mock_iommu_domain_nested *mock_nested =
> +		container_of(domain, struct mock_iommu_domain_nested, domain);
> +	struct iommu_hwpt_invalidate_selftest inv;
> +	int rc = 0;
> +	int i, j;
> +
> +	if (domain->type != IOMMU_DOMAIN_NESTED)
> +		return -EINVAL;

The core code already checked this, and it is only present on
domain_nested_ops so it is checked twice already..

Jason
  
Yi Liu Dec. 11, 2023, 11:21 a.m. UTC | #2
On 2023/12/7 02:16, Jason Gunthorpe wrote:
> On Fri, Nov 17, 2023 at 05:07:15AM -0800, Yi Liu wrote:
>> +static int
>> +mock_domain_cache_invalidate_user(struct iommu_domain *domain,
>> +				  struct iommu_user_data_array *array,
>> +				  u32 *error_code)
>> +{
>> +	struct mock_iommu_domain_nested *mock_nested =
>> +		container_of(domain, struct mock_iommu_domain_nested, domain);
>> +	struct iommu_hwpt_invalidate_selftest inv;
>> +	int rc = 0;
>> +	int i, j;
>> +
>> +	if (domain->type != IOMMU_DOMAIN_NESTED)
>> +		return -EINVAL;
> 
> The core code already checked this, and it is only present on
> domain_nested_ops so it is checked twice already..

would drop it. thanks.
  

Patch

diff --git a/drivers/iommu/iommufd/iommufd_test.h b/drivers/iommu/iommufd/iommufd_test.h
index 7910fbe1962d..0000f58dcda3 100644
--- a/drivers/iommu/iommufd/iommufd_test.h
+++ b/drivers/iommu/iommufd/iommufd_test.h
@@ -148,4 +148,21 @@  struct iommu_hwpt_selftest {
 	__u32 iotlb;
 };
 
+/**
+ * struct iommu_hwpt_invalidate_selftest
+ *
+ * @flags: invalidate flags
+ * @iotlb_id: invalidate iotlb entry index
+ *
+ * If IOMMU_TEST_INVALIDATE_ALL is set in @flags, @iotlb_id will be ignored
+ */
+struct iommu_hwpt_invalidate_selftest {
+#define IOMMU_TEST_INVALIDATE_ALL	(1ULL << 0)
+	__u32 flags;
+	__u32 iotlb_id;
+};
+
+#define IOMMU_TEST_INVALIDATE_ERR_FETCH	0xdeadbeee
+#define IOMMU_TEST_INVALIDATE_ERR_REQ	0xdeadbeef
+
 #endif
diff --git a/drivers/iommu/iommufd/selftest.c b/drivers/iommu/iommufd/selftest.c
index 5d93434003d8..2a9b970ca84e 100644
--- a/drivers/iommu/iommufd/selftest.c
+++ b/drivers/iommu/iommufd/selftest.c
@@ -473,9 +473,52 @@  static void mock_domain_free_nested(struct iommu_domain *domain)
 	kfree(mock_nested);
 }
 
+static int
+mock_domain_cache_invalidate_user(struct iommu_domain *domain,
+				  struct iommu_user_data_array *array,
+				  u32 *error_code)
+{
+	struct mock_iommu_domain_nested *mock_nested =
+		container_of(domain, struct mock_iommu_domain_nested, domain);
+	struct iommu_hwpt_invalidate_selftest inv;
+	int rc = 0;
+	int i, j;
+
+	if (domain->type != IOMMU_DOMAIN_NESTED)
+		return -EINVAL;
+
+	for (i = 0; i < array->entry_num; i++) {
+		rc = iommu_copy_struct_from_user_array(&inv, array,
+						       IOMMU_HWPT_DATA_SELFTEST,
+						       i, iotlb_id);
+		if (rc) {
+			*error_code = IOMMU_TEST_INVALIDATE_ERR_FETCH;
+			goto err;
+		}
+		/* Invalidate all mock iotlb entries and ignore iotlb_id */
+		if (inv.flags & IOMMU_TEST_INVALIDATE_ALL) {
+			for (j = 0; j < MOCK_NESTED_DOMAIN_IOTLB_NUM; j++)
+				mock_nested->iotlb[j] = 0;
+			continue;
+		}
+		/* Treat out-of-boundry iotlb_id as a request error */
+		if (inv.iotlb_id > MOCK_NESTED_DOMAIN_IOTLB_ID_MAX) {
+			*error_code = IOMMU_TEST_INVALIDATE_ERR_REQ;
+			rc = -EINVAL;
+			goto err;
+		}
+		mock_nested->iotlb[inv.iotlb_id] = 0;
+	}
+
+err:
+	array->entry_num = i;
+	return rc;
+}
+
 static struct iommu_domain_ops domain_nested_ops = {
 	.free = mock_domain_free_nested,
 	.attach_dev = mock_domain_nop_attach,
+	.cache_invalidate_user = mock_domain_cache_invalidate_user,
 };
 
 static inline struct iommufd_hw_pagetable *