Skip to contentSkip to conversation

iommu/arm-smmu-v3: Fixes reported by Sashiko

PATCH Superseded Mostafa Saleh 3 participants 25 messages Aug 28 – Sep 24

Subsystems (2)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

cover iommu/arm-smmu-v3: Fixes reported by Sashiko

While running sashiko locally on the pKVM SMMUv3 driver patches, it reported some pre-existing issues in the SMMUv3 driver (the RB tree corruption and a missing NULL check) and given that this is not the first time a pre-existing issue appears in the driver from sashiko, I went ahead and passed the whole driver to Sashiko which reported a dozen of other issues(except the last one in this series), I included fixes for bugs I believe are true or worth fixing (others were mostly missing NULL checks or bizarre HW/FW configs I don’t think are needed).

I also sorted the patches based on severity from the most critical to the least, so it is easier to apply a prefix if some bugs are too theoretical

Mostafa Saleh (7):
iommu/arm-smmu-v3: Ensure L2 tables are visible before L1 ptrs iommu/arm-smmu-v3: Prevent rbtree corruption from duplicate streams iommu/arm-smmu-v3-iommufd: Fix error path in
arm_vsmmu_cache_invalidate()
iommu/arm-smmu-v3-test: Fix arm_smmu_v3_test_debug_print_used_bits() iommu/arm-smmu-v3-test: Add missing error checks for inv array iommu/arm-smmu-v3-test: Fix OOB in arm_smmu_v3_invs_test_verify() iommu/arm-smmu-v3-test: Fix UBSAN error

 .../arm/arm-smmu-v3/arm-smmu-v3-iommufd.c     | 12 +++++-----
 .../iommu/arm/arm-smmu-v3/arm-smmu-v3-test.c  | 19 +++++++++++++---
 drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c   | 22 ++++++++++++++-----
 3 files changed, 39 insertions(+), 14 deletions(-)
Signature · 1 line
2.55.0.897.gb25b4bd76c-goog
#1 lore raw

PATCH 1/7 iommu/arm-smmu-v3: Ensure L2 tables are visible before L1 ptrs

Commit 6fabce53f6b9 ("iommu/arm-smmu-v3: Add a missing dma_wmb() for hitless STE update") adds a dma_wmb() to arm_smmu_write_entry() to make sure stream tables
and context descriptors are observed first.

However, STE L1 table descriptors are configured directly via WRITE_ONCE(), where before that they were zeroed with memset() inside dma_direct_alloc() then written to abort in via memset() also in arm_smmu_init_initial_stes() without a barrier in both cases which means that the SMMUv3 can observe the allocated table before the written descriptors causing it to fetch random data.

Similarly in arm_smmu_write_cd_l1_desc() where the L1 CD is written after dma_alloc_coherent() with no barriers.

Add dma_wmb() in both cases.

Fixes 48ec83bcbcf5 ("iommu/arm-smmu: Add initial driver support for ARM SMMUv3 devices")
Reported-by Sashiko <>
---
 drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c | 6 ++++++
 1 file changed, 6 insertions(+)
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c +6 -0
index 5732f3ba0122..494bbfd2869f 100644
@@ -1515,6 +1515,9 @@ static void arm_smmu_write_cd_l1_desc(struct arm_smmu_cdtab_l1 *dst,
 {
 	u64 val = (l2ptr_dma & CTXDESC_L1_DESC_L2PTR_MASK) | CTXDESC_L1_DESC_V;
 
+	/* Ensure the zero-cleared L2 table is fully visible. */
+	dma_wmb();
+
 	/* The HW has 64 bit atomicity with stores to the L2 CD table */
 	WRITE_ONCE(dst->l2ptr, cpu_to_le64(val));
 }
@@ -1804,6 +1807,9 @@ static void arm_smmu_write_strtab_l1_desc(struct arm_smmu_strtab_l1 *dst,
 	val |= FIELD_PREP(STRTAB_L1_DESC_SPAN, STRTAB_SPLIT + 1);
 	val |= l2ptr_dma & STRTAB_L1_DESC_L2PTR_MASK;
 
+	/* Ensure the new L2 table is fully visible. */
+	dma_wmb();
+
 	/* The HW has 64 bit atomicity with stores to the L2 STE table */
 	WRITE_ONCE(dst->l2ptr, cpu_to_le64(val));
 }
Signature · 1 line
2.55.0.897.gb25b4bd76c-goog
Reviewed-by on v1 1/7
#2 lore raw

Re: [PATCH 1/7] iommu/arm-smmu-v3: Ensure L2 tables are visible before L1 ptrs

Quoted 4 lines · Mostafa Saleh

Commit 6fabce53f6b9 ("iommu/arm-smmu-v3: Add a missing dma_wmb() for hitless STE update") adds a dma_wmb() to arm_smmu_write_entry() to make sure stream tables
and context descriptors are observed first.

Quoted 8 lines

However, STE L1 table descriptors are configured directly via WRITE_ONCE(), where before that they were zeroed with memset() inside dma_direct_alloc() then written to abort in via memset() also in arm_smmu_init_initial_stes() without a barrier in both cases which means that the SMMUv3 can observe the allocated table before the written descriptors causing it to fetch random data.

[...]

Signature · 1 line
Jason
Reviewed-by on v1 1/7
#3 lore raw

Re: [PATCH 1/7] iommu/arm-smmu-v3: Ensure L2 tables are visible before L1 ptrs

In reply to Mostafa Saleh
Quoted 11 lines · Mostafa Saleh

Commit 6fabce53f6b9 ("iommu/arm-smmu-v3: Add a missing dma_wmb() for hitless STE update") adds a dma_wmb() to arm_smmu_write_entry() to make sure stream tables
and context descriptors are observed first.

However, STE L1 table descriptors are configured directly via WRITE_ONCE(), where before that they were zeroed with memset() inside dma_direct_alloc() then written to abort in via memset() also in arm_smmu_init_initial_stes() without a barrier in both cases which means that the SMMUv3 can observe the allocated table before the written descriptors causing it to fetch random data.

Quoted 8 lines

Similarly in arm_smmu_write_cd_l1_desc() where the L1 CD is written after dma_alloc_coherent() with no barriers.

Add dma_wmb() in both cases.

Fixes 48ec83bcbcf5 ("iommu/arm-smmu: Add initial driver support for ARM SMMUv3 devices")
Reported-by Sashiko <>
#4 lore raw

PATCH 2/7 iommu/arm-smmu-v3: Prevent rbtree corruption from duplicate streams

In reply to Mostafa Saleh

In handling PCI devices with duplicate IDs, arm_smmu_insert_master() will continue the loop skipping the duplicate sids insertion in the rbtree.
However, in the error path of arm_smmu_insert_master() and in arm_smmu_remove_master(), the code loops over all fwspec->num_ids and calls rb_erase() unconditionally.

For the duplicate streams, the node is zero allocated including the parent pointer "__rb_parent_color". That means rb_erase() will think that this node is root and it will corrupt the tree which includes other masters not being removed.

Fix this by initializing those nodes with RB_CLEAR_NODE() and check if they are empty before erasing.

Fixes cdf315f907d4 ("iommu/arm-smmu-v3: Maintain a SID->device structure")
Reported-by Sashiko <>
---
 drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c | 16 +++++++++++-----
 1 file changed, 11 insertions(+), 5 deletions(-)
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c +11 -5
index 494bbfd2869f..36e3778c971e 100644
@@ -4147,8 +4147,10 @@ static int arm_smmu_insert_master(struct arm_smmu_device *smmu,
 					->master;
 
 			/* Bridged PCI devices may end up with duplicated IDs */
-			if (existing_master == master)
+			if (existing_master == master) {
+				RB_CLEAR_NODE(&new_stream->node);
 				continue;
+			}
 
 			dev_warn(master->dev,
 				 "Aliasing StreamID 0x%x (from %s) unsupported, expect DMA to be broken\n",
@@ -4159,8 +4161,10 @@ static int arm_smmu_insert_master(struct arm_smmu_device *smmu,
 	}
 
 	if (ret) {
-		for (i--; i >= 0; i--)
-			rb_erase(&master->streams[i].node, &smmu->streams);
+		for (i--; i >= 0; i--) {
+			if (!RB_EMPTY_NODE(&master->streams[i].node))
+				rb_erase(&master->streams[i].node, &smmu->streams);
+		}
 		kfree(master->streams);
 		kfree(master->build_invs);
 	}
@@ -4179,8 +4183,10 @@ static void arm_smmu_remove_master(struct arm_smmu_master *master)
 		return;
 
 	mutex_lock(&smmu->streams_mutex);
-	for (i = 0; i < fwspec->num_ids; i++)
-		rb_erase(&master->streams[i].node, &smmu->streams);
+	for (i = 0; i < fwspec->num_ids; i++) {
+		if (!RB_EMPTY_NODE(&master->streams[i].node))
+			rb_erase(&master->streams[i].node, &smmu->streams);
+	}
 	mutex_unlock(&smmu->streams_mutex);
 
 	kfree(master->streams);
Signature · 1 line
2.55.0.897.gb25b4bd76c-goog
Reviewed-by on v1 2/7
#5 lore raw

Re: [PATCH 2/7] iommu/arm-smmu-v3: Prevent rbtree corruption from duplicate streams

Quoted 8 lines · Mostafa Saleh

In handling PCI devices with duplicate IDs, arm_smmu_insert_master() will continue the loop skipping the duplicate sids insertion in the rbtree.
However, in the error path of arm_smmu_insert_master() and in arm_smmu_remove_master(), the code loops over all fwspec->num_ids and calls rb_erase() unconditionally.

[...]

Signature · 1 line
Jason
#6 lore raw

Re: [PATCH 2/7] iommu/arm-smmu-v3: Prevent rbtree corruption from duplicate streams

In reply to Mostafa Saleh
Quoted 12 lines · Mostafa Saleh

In handling PCI devices with duplicate IDs, arm_smmu_insert_master() will continue the loop skipping the duplicate sids insertion in the rbtree.
However, in the error path of arm_smmu_insert_master() and in arm_smmu_remove_master(), the code loops over all fwspec->num_ids and calls rb_erase() unconditionally.

For the duplicate streams, the node is zero allocated including the parent pointer "__rb_parent_color". That means rb_erase() will think that this node is root and it will corrupt the tree which includes other masters not being removed.

Quoted 6 lines

Fix this by initializing those nodes with RB_CLEAR_NODE() and check if they are empty before erasing.

Fixes cdf315f907d4 ("iommu/arm-smmu-v3: Maintain a SID->device structure")
Reported-by Sashiko <>

I submitted a similar patch a while ago :)
https://lore.kernel.org/linux-iommu/9136477b9c737a50cecc38f2ee6704a01510169a.1783044582.git.nicolinc@nvidia.com/

And I think the fix tag, as mine posted, should be:

Fixes b00d24997a11 ("iommu/arm-smmu-v3: Fix iommu_device_probe bug due to duplicated stream ids")

Before that commit, a duplicated SID gets -EINVAL and errors out.

Thanks
Nicolin

#7 lore raw

Re: [PATCH 2/7] iommu/arm-smmu-v3: Prevent rbtree corruption from duplicate streams

Quoted 16 lines · Nicolin Chen
Quoted 15 lines · Mostafa Saleh

In handling PCI devices with duplicate IDs, arm_smmu_insert_master() will continue the loop skipping the duplicate sids insertion in the rbtree.
However, in the error path of arm_smmu_insert_master() and in arm_smmu_remove_master(), the code loops over all fwspec->num_ids and calls rb_erase() unconditionally.

For the duplicate streams, the node is zero allocated including the parent pointer "__rb_parent_color". That means rb_erase() will think that this node is root and it will corrupt the tree which includes other masters not being removed.

Fix this by initializing those nodes with RB_CLEAR_NODE() and check if they are empty before erasing.

Quoted 6 lines
Quoted 3 lines
Fixes cdf315f907d4 ("iommu/arm-smmu-v3: Maintain a SID->device structure")
Reported-by Sashiko <>

I submitted a similar patch a while ago :)
https://lore.kernel.org/linux-iommu/9136477b9c737a50cecc38f2ee6704a01510169a.1783044582.git.nicolinc@nvidia.com/

Oh, I missed that, I haven't looked at this series. I beleive this bug exists in the current tree so it is better to send separately.

Anyway, I ok dropping this patch if you want to re-send yours.

Thanks,
Mostafa

Quoted 8 lines

And I think the fix tag, as mine posted, should be:

Fixes b00d24997a11 ("iommu/arm-smmu-v3: Fix iommu_device_probe bug due to duplicated stream ids")

Before that commit, a duplicated SID gets -EINVAL and errors out.

Thanks
Nicolin

#8 lore raw

PATCH 3/7 iommu/arm-smmu-v3-iommufd: Fix error path in arm_vsmmu_cache_invalidate()

In reply to Mostafa Saleh

arm_vsmmu_cache_invalidate() issues commands in batch size. However, if arm_vsmmu_convert_user_cmd() fails, it will return to the user that it processed "cur - cmds" which may be part of a batch not issued yet.

Fix this by calculating the returned entry_num solely based on the "last" that tracks the last batch that was successfully issued.

Also, fix the number of commands for early failing condition.

Fixes d68beb276ba2 ("iommu/arm-smmu-v3: Support IOMMU_HWPT_INVALIDATE using a VIOMMU object")
Reported-by Sashiko <>
---
 drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3-iommufd.c | 12 ++++++------
 1 file changed, 6 insertions(+), 6 deletions(-)
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3-iommufd.c +6 -6
index 25982bdbcbd9..91776c555f0e 100644
@@ -379,10 +379,13 @@ int arm_vsmmu_cache_invalidate(struct iommufd_viommu *viommu,
 	int ret;
 
 	cmds = kzalloc_objs(*cmds, array->entry_num);
-	if (!cmds)
+	if (!cmds) {
+		array->entry_num = 0;
 		return -ENOMEM;
+	}
 	cur = cmds;
 	end = cmds + array->entry_num;
+	last = cmds;
 
 	static_assert(sizeof(*cmds) == 2 * sizeof(u64));
 	ret = iommu_copy_struct_from_full_user_array(
@@ -391,7 +394,6 @@ int arm_vsmmu_cache_invalidate(struct iommufd_viommu *viommu,
 	if (ret)
 		goto out;
 
-	last = cmds;
 	while (cur != end) {
 		ret = arm_vsmmu_convert_user_cmd(vsmmu, cur);
 		if (ret)
@@ -405,14 +407,12 @@ int arm_vsmmu_cache_invalidate(struct iommufd_viommu *viommu,
 		/* FIXME always uses the main cmdq rather than trying to group by type */
 		ret = __arm_smmu_cmdq_issue_cmdlist(smmu, &smmu->cmdq, &last->cmd,
 						    cur - last, true);
-		if (ret) {
-			cur--;
+		if (ret)
 			goto out;
-		}
 		last = cur;
 	}
 out:
-	array->entry_num = cur - cmds;
+	array->entry_num = last - cmds;
 	kfree(cmds);
 	return ret;
 }
Signature · 1 line
2.55.0.897.gb25b4bd76c-goog
#9 lore raw

Re: [PATCH 3/7] iommu/arm-smmu-v3-iommufd: Fix error path in arm_vsmmu_cache_invalidate()

Quoted 5 lines

arm_vsmmu_cache_invalidate() issues commands in batch size. However, if arm_vsmmu_convert_user_cmd() fails, it will return to the user that it processed "cur - cmds" which may be part of a batch not issued yet.

Quoted 8 lines

Fix this by calculating the returned entry_num solely based on the "last" that tracks the last batch that was successfully issued.

Also, fix the number of commands for early failing condition.

Fixes d68beb276ba2 ("iommu/arm-smmu-v3: Support IOMMU_HWPT_INVALIDATE using a VIOMMU object")
Reported-by Sashiko <>

Nicolin's series fixes this and the other sashiko note about over allocation:

https://lore.kernel.org/all/[email protected]/

I think it is in good shape, I would rather merge it than hack on this further.

Signature · 1 line
Jason
#10 lore raw

Re: [PATCH 3/7] iommu/arm-smmu-v3-iommufd: Fix error path in arm_vsmmu_cache_invalidate()

Quoted 10 lines · Jason Gunthorpe
Quoted 10 lines

arm_vsmmu_cache_invalidate() issues commands in batch size. However, if arm_vsmmu_convert_user_cmd() fails, it will return to the user that it processed "cur - cmds" which may be part of a batch not issued yet.

Fix this by calculating the returned entry_num solely based on the "last" that tracks the last batch that was successfully issued.

Also, fix the number of commands for early failing condition.

Quoted 9 lines
Quoted 3 lines
Fixes d68beb276ba2 ("iommu/arm-smmu-v3: Support IOMMU_HWPT_INVALIDATE using a VIOMMU object")
Reported-by Sashiko <>

Nicolin's series fixes this and the other sashiko note about over allocation:

https://lore.kernel.org/all/[email protected]/

I see, I will add it to my review list. Hopefully I can get to it by this week.

Thanks,
Mostafa

Quoted 5 lines

I think it is in good shape, I would rather merge it than hack on this further.

Signature · 1 line
Jason
#11 lore raw

PATCH 4/7 iommu/arm-smmu-v3-test: Fix arm_smmu_v3_test_debug_print_used_bits()

In reply to Mostafa Saleh

arm_smmu_v3_test_debug_print_used_bits() hardcodes arm_smmu_get_ste_used() instead of using the provided writer->ops->get_used() callback. This caused Context Descriptors to be incorrectly parsed as STEs.

Fixes 56e1a4cc2588 ("iommu/arm-smmu-v3: Add unit tests for arm_smmu_write_entry")
Reported-by Sashiko <>
---
 drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3-test.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3-test.c +2 -2
index add671363c82..eae08d4d77ec 100644
@@ -125,8 +125,8 @@ arm_smmu_v3_test_debug_print_used_bits(struct arm_smmu_entry_writer *writer,
 {
 	__le64 used_bits[NUM_ENTRY_QWORDS] = {};
 
-	arm_smmu_get_ste_used(ste, used_bits);
-	pr_debug("STE used bits: ");
+	writer->ops->get_used(ste, used_bits);
+	pr_debug("Entry used bits: ");
 	print_hex_dump_debug("    ", DUMP_PREFIX_NONE, 16, 8, used_bits,
 			     sizeof(used_bits), false);
 }
Signature · 1 line
2.55.0.897.gb25b4bd76c-goog
Reviewed-by on v1 4/7
#12 lore raw

Re: [PATCH 4/7] iommu/arm-smmu-v3-test: Fix arm_smmu_v3_test_debug_print_used_bits()

Quoted 3 lines · Mostafa Saleh

arm_smmu_v3_test_debug_print_used_bits() hardcodes arm_smmu_get_ste_used() instead of using the provided writer->ops->get_used() callback. This caused Context Descriptors to be incorrectly parsed as STEs.

Signature · 1 line
Jason
Reviewed-by on v1 4/7
#13 lore raw

Re: [PATCH 4/7] iommu/arm-smmu-v3-test: Fix arm_smmu_v3_test_debug_print_used_bits()

In reply to Mostafa Saleh
Quoted 16 lines · Mostafa Saleh

arm_smmu_v3_test_debug_print_used_bits() hardcodes arm_smmu_get_ste_used() instead of using the provided writer->ops->get_used() callback. This caused Context Descriptors to be incorrectly parsed as STEs.

Fixes 56e1a4cc2588 ("iommu/arm-smmu-v3: Add unit tests for arm_smmu_write_entry")
Reported-by Sashiko <>
---
 drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3-test.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3-test.c +0 -0
index add671363c82..eae08d4d77ec 100644

drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3-test.c arm_smmu_v3_test_debug_print_used_bits(struct arm_smmu_entry_writer *writer, · line 129

Quoted 7 lines
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3-test.c +2 -2
@@ -125,8 +125,8 @@ arm_smmu_v3_test_debug_print_used_bits(struct arm_smmu_entry_writer *writer,
 {
 	__le64 used_bits[NUM_ENTRY_QWORDS] = {};
 
-	arm_smmu_get_ste_used(ste, used_bits);
-	pr_debug("STE used bits: ");
+	writer->ops->get_used(ste, used_bits);
+	pr_debug("Entry used bits: ");

Nit: the name of the 2nd function parameter is still "ste" being used at writer->ops->get_used. Maybe it should change to "ent"?

Otherwise,

#14 lore raw

PATCH 5/7 iommu/arm-smmu-v3-test: Add missing error checks for inv array

In reply to Mostafa Saleh

arm_smmu_invs_merge() and arm_smmu_invs_alloc() can return NULL or errors which are checked by the driver but not the test.

Add KUNIT_ASSERT_NOT_ERR_OR_NULL() after calling them to fail the test instead of accessing NULL or ERR pointers.

Fixes 15a2a5645ad7 ("iommu/arm-smmu-v3: Introduce a per-domain arm_smmu_invs array")
Reported-by Sashiko <>
---
 drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3-test.c | 8 ++++++++
 1 file changed, 8 insertions(+)
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3-test.c +8 -0
index eae08d4d77ec..366dcb2b5554 100644
@@ -704,17 +704,20 @@ static void arm_smmu_v3_invs_test(struct kunit *test)
 
 	/* New array */
 	test_a = arm_smmu_invs_alloc(0);
+	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, test_a);
 	KUNIT_EXPECT_EQ(test, test_a->num_invs, 0);
 
 	/* Test1: merge invs1 (new array) */
 	test_b = arm_smmu_invs_merge(test_a, &invs1);
 	kfree(test_a);
+	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, test_b);
 	arm_smmu_v3_invs_test_verify(test, test_b, ARRAY_SIZE(results1[0]), 0,
 				     results1[0], results1[1], results1[2]);
 
 	/* Test2: merge invs2 (new array) */
 	test_a = arm_smmu_invs_merge(test_b, &invs2);
 	kfree(test_b);
+	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, test_a);
 	arm_smmu_v3_invs_test_verify(test, test_a, ARRAY_SIZE(results2[0]), 0,
 				     results2[0], results2[1], results2[2]);
 
@@ -726,6 +729,7 @@ static void arm_smmu_v3_invs_test(struct kunit *test)
 	/* Test4: merge invs3 (new array) */
 	test_b = arm_smmu_invs_merge(test_a, &invs3);
 	kfree(test_a);
+	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, test_b);
 	arm_smmu_v3_invs_test_verify(test, test_b, ARRAY_SIZE(results4[0]), 0,
 				     results4[0], results4[1], results4[2]);
 
@@ -737,6 +741,7 @@ static void arm_smmu_v3_invs_test(struct kunit *test)
 	/* Test6: purge test_b (new array) */
 	test_a = arm_smmu_invs_purge(test_b);
 	kfree(test_b);
+	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, test_a);
 	arm_smmu_v3_invs_test_verify(test, test_a, ARRAY_SIZE(results6[0]), 0,
 				     results6[0], results6[1], results6[2]);
 
@@ -748,12 +753,14 @@ static void arm_smmu_v3_invs_test(struct kunit *test)
 	/* Test8: merge invs4 (new array) */
 	test_b = arm_smmu_invs_merge(test_a, &invs4);
 	kfree(test_a);
+	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, test_b);
 	arm_smmu_v3_invs_test_verify(test, test_b, ARRAY_SIZE(results7[0]), 0,
 				     results7[0], results7[1], results7[2]);
 
 	/* Test9: merge invs5 (new array) */
 	test_a = arm_smmu_invs_merge(test_b, &invs5);
 	kfree(test_b);
+	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, test_a);
 	arm_smmu_v3_invs_test_verify(test, test_a, ARRAY_SIZE(results8[0]), 0,
 				     results8[0], results8[1], results8[2]);
 
@@ -765,6 +772,7 @@ static void arm_smmu_v3_invs_test(struct kunit *test)
 	/* Test11: purge test_a (new array) */
 	test_b = arm_smmu_invs_purge(test_a);
 	kfree(test_a);
+	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, test_b);
 	arm_smmu_v3_invs_test_verify(test, test_b, ARRAY_SIZE(results10[0]), 0,
 				     results10[0], results10[1], results10[2]);
 
Signature · 1 line
2.55.0.897.gb25b4bd76c-goog
#15 lore raw

Re: [PATCH 5/7] iommu/arm-smmu-v3-test: Add missing error checks for inv array

Quoted 5 lines

arm_smmu_invs_merge() and arm_smmu_invs_alloc() can return NULL or errors which are checked by the driver but not the test.

Add KUNIT_ASSERT_NOT_ERR_OR_NULL() after calling them to fail the test instead of accessing NULL or ERR pointers.

I've generally had the opinion that if kunit tests hit bugs I don't actually care what happens next so long as the bug is evident.. Many tests can't clean up when they get longjump'd out like this too.

So, IDK, it is fine but I also think this is in a category that maybe sashiko prompts should address instead somehow.

Signature · 1 line
Jason
#16 lore raw

Re: [PATCH 5/7] iommu/arm-smmu-v3-test: Add missing error checks for inv array

Quoted 9 lines · Jason Gunthorpe
Quoted 5 lines

arm_smmu_invs_merge() and arm_smmu_invs_alloc() can return NULL or errors which are checked by the driver but not the test.

Add KUNIT_ASSERT_NOT_ERR_OR_NULL() after calling them to fail the test instead of accessing NULL or ERR pointers.

I've generally had the opinion that if kunit tests hit bugs I don't actually care what happens next so long as the bug is evident.. Many tests can't clean up when they get longjump'd out like this too.

I tend to agree as that's how I use kunit also, but I'd be extra careful around crashing the kernel from kunit as it can be triggered from userspace.

Thanks,
Mostafa

Quoted 6 lines

So, IDK, it is fine but I also think this is in a category that maybe sashiko prompts should address instead somehow.

Signature · 1 line
Jason
#17 lore raw

PATCH 6/7 iommu/arm-smmu-v3-test: Fix OOB in arm_smmu_v3_invs_test_verify()

In reply to Mostafa Saleh

arm_smmu_v3_invs_test_verify() validates the array bounds using KUNIT_EXPECT_EQ(), which triggers a failure and continues execution. If invs->num_invs was smaller than expected, the next loop over num_invs would blindly read past the end of the invs->inv[] array. Switch to KUNIT_ASSERT_EQ() to fail and stop the test on bound errors.

Fixes 15a2a5645ad7 ("iommu/arm-smmu-v3: Introduce a per-domain arm_smmu_invs array")
Reported-by Sashiko <>
---
 drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3-test.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3-test.c +1 -1
index 366dcb2b5554..244cf34e5a0b 100644
@@ -643,7 +643,7 @@ static void arm_smmu_v3_invs_test_verify(struct kunit *test,
 					 const int *ids, const int *users,
 					 const int *ssids)
 {
-	KUNIT_EXPECT_EQ(test, invs->num_invs, num_invs);
+	KUNIT_ASSERT_EQ(test, invs->num_invs, num_invs);
 	KUNIT_EXPECT_EQ(test, invs->num_trashes, num_trashes);
 	while (num_invs--) {
 		KUNIT_EXPECT_EQ(test, invs->inv[num_invs].id, ids[num_invs]);
Signature · 1 line
2.55.0.897.gb25b4bd76c-goog
Reviewed-by on v1 6/7
#18 lore raw

Re: [PATCH 6/7] iommu/arm-smmu-v3-test: Fix OOB in arm_smmu_v3_invs_test_verify()

Quoted 8 lines · Mostafa Saleh

arm_smmu_v3_invs_test_verify() validates the array bounds using KUNIT_EXPECT_EQ(), which triggers a failure and continues execution. If invs->num_invs was smaller than expected, the next loop over num_invs would blindly read past the end of the invs->inv[] array. Switch to KUNIT_ASSERT_EQ() to fail and stop the test on bound errors.

[...]

Signature · 1 line
Jason
Reviewed-by on v1 6/7
#19 lore raw

Re: [PATCH 6/7] iommu/arm-smmu-v3-test: Fix OOB in arm_smmu_v3_invs_test_verify()

In reply to Mostafa Saleh
Quoted 9 lines · Mostafa Saleh

arm_smmu_v3_invs_test_verify() validates the array bounds using KUNIT_EXPECT_EQ(), which triggers a failure and continues execution. If invs->num_invs was smaller than expected, the next loop over num_invs would blindly read past the end of the invs->inv[] array. Switch to KUNIT_ASSERT_EQ() to fail and stop the test on bound errors.

Fixes 15a2a5645ad7 ("iommu/arm-smmu-v3: Introduce a per-domain arm_smmu_invs array")
Reported-by Sashiko <>
#20 lore raw

PATCH 7/7 iommu/arm-smmu-v3-test: Fix UBSAN error

In reply to Mostafa Saleh

struct arm_smmu_invs marks its flexible array member inv[] with the __counted_by(max_invs).

arm_smmu_v3_invs_test() uses invs1 to invs5 which has num_invs = 3 but omit max_invs.

This causes the following UBSAN error:

[    2.333240] UBSAN: array-index-out-of-bounds in drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c:1116:21
[    2.333604]     # arm_smmu_v3_invs_test: lib/ubsan.c:228: array-index-out-of-bounds in drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c
[    2.334129] index 1 is out of range for type 'struct arm_smmu_inv[] __counted_by(max_invs)' (aka 'struct arm_smmu_inv[]')
[    2.335375] CPU: 6 UID: 0 PID: 155 Comm: kunit_try_catch Tainted: G                 N  7.2.0-g8dcb331fbb72 #2 PREEMPT
[    2.335759] Tainted: [N]=TEST
[    2.335791] Hardware name: linux,dummy-virt (DT)
[    2.336254] Call trace:
[    2.336932]  show_stack+0x18/0x24 (C)
[    2.338443]  __dump_stack+0x28/0x38
[    2.338494]  dump_stack_lvl+0x54/0x6c
[    2.338518]  dump_stack+0x18/0x24
[    2.338541]  ubsan_epilogue+0x10/0x44
[    2.338565]  __ubsan_handle_out_of_bounds+0xb8/0xbc
[    2.338834]  arm_smmu_invs_merge+0x688/0x8ac
[    2.338862]  arm_smmu_v3_invs_test+0xd4/0x598
[    2.338890]  kunit_try_run_case+0x64/0x160
[    2.338914]  kunit_generic_run_threadfn_adapter+0x28/0x4c
[    2.338955]  kthread+0x10c/0x12c
[    2.338984]  ret_from_fork+0x10/0x20
[    2.339433] ---[ end trace ]---

Explicitly set max_invs = 3 on the test arrays to match the number of elements.

Fixes 15a2a5645ad7 ("iommu/arm-smmu-v3: Introduce a per-domain arm_smmu_invs array")
---
 drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3-test.c | 5 +++++
 1 file changed, 5 insertions(+)
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3-test.c +5 -0
index 244cf34e5a0b..e52a7d95c919 100644
@@ -654,6 +654,7 @@ static void arm_smmu_v3_invs_test_verify(struct kunit *test,
 }
 
 static struct arm_smmu_invs invs1 = {
+	.max_invs = 3,
 	.num_invs = 3,
 	.inv = { { .type = INV_TYPE_S2_VMID, .id = 1, },
 		 { .type = INV_TYPE_S2_VMID_S1_CLEAR, .id = 1, },
@@ -661,6 +662,7 @@ static struct arm_smmu_invs invs1 = {
 };
 
 static struct arm_smmu_invs invs2 = {
+	.max_invs = 3,
 	.num_invs = 3,
 	.inv = { { .type = INV_TYPE_S2_VMID, .id = 1, }, /* duplicated */
 		 { .type = INV_TYPE_ATS, .id = 4, },
@@ -668,6 +670,7 @@ static struct arm_smmu_invs invs2 = {
 };
 
 static struct arm_smmu_invs invs3 = {
+	.max_invs = 3,
 	.num_invs = 3,
 	.inv = { { .type = INV_TYPE_S2_VMID, .id = 1, }, /* duplicated */
 		 { .type = INV_TYPE_ATS, .id = 5, }, /* recover a trash */
@@ -675,6 +678,7 @@ static struct arm_smmu_invs invs3 = {
 };
 
 static struct arm_smmu_invs invs4 = {
+	.max_invs = 3,
 	.num_invs = 3,
 	.inv = { { .type = INV_TYPE_ATS, .id = 10, .ssid = 1 },
 		 { .type = INV_TYPE_ATS, .id = 10, .ssid = 3 },
@@ -682,6 +686,7 @@ static struct arm_smmu_invs invs4 = {
 };
 
 static struct arm_smmu_invs invs5 = {
+	.max_invs = 3,
 	.num_invs = 3,
 	.inv = { { .type = INV_TYPE_ATS, .id = 10, .ssid = 2 },
 		 { .type = INV_TYPE_ATS, .id = 10, .ssid = 3 }, /* duplicate */
Signature · 1 line
2.55.0.897.gb25b4bd76c-goog
Reviewed-by on v1 7/7
#21 lore raw

Re: [PATCH 7/7] iommu/arm-smmu-v3-test: Fix UBSAN error

Quoted 28 lines

struct arm_smmu_invs marks its flexible array member inv[] with the __counted_by(max_invs).

arm_smmu_v3_invs_test() uses invs1 to invs5 which has num_invs = 3 but omit max_invs.

This causes the following UBSAN error:

[    2.333240] UBSAN: array-index-out-of-bounds in drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c:1116:21
[    2.333604]     # arm_smmu_v3_invs_test: lib/ubsan.c:228: array-index-out-of-bounds in drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c
[    2.334129] index 1 is out of range for type 'struct arm_smmu_inv[] __counted_by(max_invs)' (aka 'struct arm_smmu_inv[]')
[    2.335375] CPU: 6 UID: 0 PID: 155 Comm: kunit_try_catch Tainted: G                 N  7.2.0-g8dcb331fbb72 #2 PREEMPT
[    2.335759] Tainted: [N]=TEST
[    2.335791] Hardware name: linux,dummy-virt (DT)
[    2.336254] Call trace:
[    2.336932]  show_stack+0x18/0x24 (C)
[    2.338443]  __dump_stack+0x28/0x38
[    2.338494]  dump_stack_lvl+0x54/0x6c
[    2.338518]  dump_stack+0x18/0x24
[    2.338541]  ubsan_epilogue+0x10/0x44
[    2.338565]  __ubsan_handle_out_of_bounds+0xb8/0xbc
[    2.338834]  arm_smmu_invs_merge+0x688/0x8ac
[    2.338862]  arm_smmu_v3_invs_test+0xd4/0x598
[    2.338890]  kunit_try_run_case+0x64/0x160
[    2.338914]  kunit_generic_run_threadfn_adapter+0x28/0x4c
[    2.338955]  kthread+0x10c/0x12c
[    2.338984]  ret_from_fork+0x10/0x20
[    2.339433] ---[ end trace ]---
Quoted 5 lines

Explicitly set max_invs = 3 on the test arrays to match the number of elements.

Fixes 15a2a5645ad7 ("iommu/arm-smmu-v3: Introduce a per-domain arm_smmu_invs array")

I have the same fix sitting in my branch too

Signature · 1 line
Jason
Reviewed-by on v1 7/7
#22 lore raw

Re: [PATCH 7/7] iommu/arm-smmu-v3-test: Fix UBSAN error

In reply to Mostafa Saleh
Quoted 28 lines · Mostafa Saleh

struct arm_smmu_invs marks its flexible array member inv[] with the __counted_by(max_invs).

arm_smmu_v3_invs_test() uses invs1 to invs5 which has num_invs = 3 but omit max_invs.

This causes the following UBSAN error:

[    2.333240] UBSAN: array-index-out-of-bounds in drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c:1116:21
[    2.333604]     # arm_smmu_v3_invs_test: lib/ubsan.c:228: array-index-out-of-bounds in drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c
[    2.334129] index 1 is out of range for type 'struct arm_smmu_inv[] __counted_by(max_invs)' (aka 'struct arm_smmu_inv[]')
[    2.335375] CPU: 6 UID: 0 PID: 155 Comm: kunit_try_catch Tainted: G                 N  7.2.0-g8dcb331fbb72 #2 PREEMPT
[    2.335759] Tainted: [N]=TEST
[    2.335791] Hardware name: linux,dummy-virt (DT)
[    2.336254] Call trace:
[    2.336932]  show_stack+0x18/0x24 (C)
[    2.338443]  __dump_stack+0x28/0x38
[    2.338494]  dump_stack_lvl+0x54/0x6c
[    2.338518]  dump_stack+0x18/0x24
[    2.338541]  ubsan_epilogue+0x10/0x44
[    2.338565]  __ubsan_handle_out_of_bounds+0xb8/0xbc
[    2.338834]  arm_smmu_invs_merge+0x688/0x8ac
[    2.338862]  arm_smmu_v3_invs_test+0xd4/0x598
[    2.338890]  kunit_try_run_case+0x64/0x160
[    2.338914]  kunit_generic_run_threadfn_adapter+0x28/0x4c
[    2.338955]  kthread+0x10c/0x12c
[    2.338984]  ret_from_fork+0x10/0x20
[    2.339433] ---[ end trace ]---
Quoted 5 lines

Explicitly set max_invs = 3 on the test arrays to match the number of elements.

Fixes 15a2a5645ad7 ("iommu/arm-smmu-v3: Introduce a per-domain arm_smmu_invs array")
#23 lore raw
In reply to Mostafa Saleh
Quoted 13 lines · Mostafa Saleh

While running sashiko locally on the pKVM SMMUv3 driver patches, it reported some pre-existing issues in the SMMUv3 driver (the RB tree corruption and a missing NULL check) and given that this is not the first time a pre-existing issue appears in the driver from sashiko, I went ahead and passed the whole driver to Sashiko which reported a dozen of other issues(except the last one in this series), I included fixes for bugs I believe are true or worth fixing (others were mostly missing NULL checks or bizarre HW/FW configs I don’t think are needed).

I also sorted the patches based on severity from the most critical to the least, so it is easier to apply a prefix if some bugs are too theoretical

Quoted 9 lines

Mostafa Saleh (7):
iommu/arm-smmu-v3: Ensure L2 tables are visible before L1 ptrs iommu/arm-smmu-v3: Prevent rbtree corruption from duplicate streams iommu/arm-smmu-v3-iommufd: Fix error path in
arm_vsmmu_cache_invalidate()
iommu/arm-smmu-v3-test: Fix arm_smmu_v3_test_debug_print_used_bits() iommu/arm-smmu-v3-test: Add missing error checks for inv array iommu/arm-smmu-v3-test: Fix OOB in arm_smmu_v3_invs_test_verify() iommu/arm-smmu-v3-test: Fix UBSAN error

Does this need a respin? At least the 3rd patch there should no go as the iommufd tree has a different version of that fix now

Jason

#24 lore raw
Quoted 24 lines · Jason Gunthorpe
Quoted 13 lines · Mostafa Saleh

While running sashiko locally on the pKVM SMMUv3 driver patches, it reported some pre-existing issues in the SMMUv3 driver (the RB tree corruption and a missing NULL check) and given that this is not the first time a pre-existing issue appears in the driver from sashiko, I went ahead and passed the whole driver to Sashiko which reported a dozen of other issues(except the last one in this series), I included fixes for bugs I believe are true or worth fixing (others were mostly missing NULL checks or bizarre HW/FW configs I don’t think are needed).

I also sorted the patches based on severity from the most critical to the least, so it is easier to apply a prefix if some bugs are too theoretical

Quoted 9 lines

Mostafa Saleh (7):
iommu/arm-smmu-v3: Ensure L2 tables are visible before L1 ptrs iommu/arm-smmu-v3: Prevent rbtree corruption from duplicate streams iommu/arm-smmu-v3-iommufd: Fix error path in
arm_vsmmu_cache_invalidate()
iommu/arm-smmu-v3-test: Fix arm_smmu_v3_test_debug_print_used_bits() iommu/arm-smmu-v3-test: Add missing error checks for inv array iommu/arm-smmu-v3-test: Fix OOB in arm_smmu_v3_invs_test_verify() iommu/arm-smmu-v3-test: Fix UBSAN error

Quoted 2 lines

Does this need a respin? At least the 3rd patch there should no go as the iommufd tree has a different version of that fix now

Yes, I will respin shortly, I will also drop patch 2 as Nicolin posted a new version of his patch.

Thanks,
Mostafa

Quoted 2 lines

Jason