Skip to contentSkip to conversation

iommufd/selftest: Fix dirty_bitmap tests

PATCH In review Robin Murphy 4 participants 5 messages Nov 16, 2023 – Nov 21, 2023

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

PATCH 1/1 iommufd/selftest: Fix dirty_bitmap tests

The ASSERT_EQ() macro sneakily expands to two statements, so the loop here needs braces to ensure it captures both and actually terminates the test upon failure. Where these tests are currently failing on my arm64 machine, this reduces the number of logged lines from a rather unreasonable ~197,000 down to 10. While we're at it, we can also clean up the tautologous "count" calculations whose assertions can never fail unless mathematics and/or the C language become fundamentally broken.

Fixes a9af47e382a4 ("iommufd/selftest: Test IOMMU_HWPT_GET_DIRTY_BITMAP")
---
 tools/testing/selftests/iommu/iommufd_utils.h | 13 ++++++-------
 1 file changed, 6 insertions(+), 7 deletions(-)
tools/testing/selftests/iommu/iommufd_utils.h +6 -7
index 050e9751321c..ad9202335656 100644
@@ -293,15 +293,13 @@ static int _test_mock_dirty_bitmaps(int fd, __u32 hwpt_id, size_t length,
 				    __u64 bitmap_size, __u32 flags,
 				    struct __test_metadata *_metadata)
 {
-	unsigned long i, count, nbits = bitmap_size * BITS_PER_BYTE;
+	unsigned long i, nbits = bitmap_size * BITS_PER_BYTE;
 	unsigned long nr = nbits / 2;
 	__u64 out_dirty = 0;
 
 	/* Mark all even bits as dirty in the mock domain */
-	for (count = 0, i = 0; i < nbits; count += !(i % 2), i++)
-		if (!(i % 2))
-			set_bit(i, (unsigned long *)bitmap);
-	ASSERT_EQ(nr, count);
+	for (i = 0; i < nbits; i += 2)
+		set_bit(i, (unsigned long *)bitmap);
 
 	test_cmd_mock_domain_set_dirty(fd, hwpt_id, length, iova, page_size,
 				       bitmap, &out_dirty);
@@ -311,9 +309,10 @@ static int _test_mock_dirty_bitmaps(int fd, __u32 hwpt_id, size_t length,
 	memset(bitmap, 0, bitmap_size);
 	test_cmd_get_dirty_bitmap(fd, hwpt_id, length, iova, page_size, bitmap,
 				  flags);
-	for (count = 0, i = 0; i < nbits; count += !(i % 2), i++)
+	/* Beware ASSERT_EQ() is two statements -- braces are not redundant! */
+	for (i = 0; i < nbits; i++) {
 		ASSERT_EQ(!(i % 2), test_bit(i, (unsigned long *)bitmap));
-	ASSERT_EQ(count, out_dirty);
+	}
 
 	memset(bitmap, 0, bitmap_size);
 	test_cmd_get_dirty_bitmap(fd, hwpt_id, length, iova, page_size, bitmap,
Signature · 1 line
2.39.2.101.g768bb238c484.dirty
Reviewed-by Tested-by on v1 1/1
#1 lore raw
Quoted 3 lines · Robin Murphy

The ASSERT_EQ() macro sneakily expands to two statements, so the loop here needs braces to ensure it captures both and actually terminates the test upon failure.

Ugh

Quoted 8 lines

Where these tests are currently failing on my arm64
machine, this reduces the number of logged lines from a rather unreasonable ~197,000 down to 10. While we're at it, we can also clean up the tautologous "count" calculations whose assertions can never fail unless mathematics and/or the C language become fundamentally broken.

Fixes a9af47e382a4 ("iommufd/selftest: Test IOMMU_HWPT_GET_DIRTY_BITMAP")

I was going to say that the second assert is useful, but we are already test the number of bits we set against what the mock domain set after mock_domain_set_dirty(). So the second is redundantly testing the same, and can be removed as you are doing. Thanks for fixing this.

I would suggest the subject to:

iommufd/selftest: Fix _test_mock_dirty_bitmaps()

Because dirty-bitmap tests seems to imply the whole fixture, which covers more than the bitmaps.

Quoted 30 lines
---
 tools/testing/selftests/iommu/iommufd_utils.h | 13 ++++++-------
 1 file changed, 6 insertions(+), 7 deletions(-)
tools/testing/selftests/iommu/iommufd_utils.h +3 -5
index 050e9751321c..ad9202335656 100644
@@ -293,15 +293,13 @@ static int _test_mock_dirty_bitmaps(int fd, __u32 hwpt_id, size_t length,
 				    __u64 bitmap_size, __u32 flags,
 				    struct __test_metadata *_metadata)
 {
-	unsigned long i, count, nbits = bitmap_size * BITS_PER_BYTE;
+	unsigned long i, nbits = bitmap_size * BITS_PER_BYTE;
 	unsigned long nr = nbits / 2;
 	__u64 out_dirty = 0;
 
 	/* Mark all even bits as dirty in the mock domain */
-	for (count = 0, i = 0; i < nbits; count += !(i % 2), i++)
-		if (!(i % 2))
-			set_bit(i, (unsigned long *)bitmap);
-	ASSERT_EQ(nr, count);
+	for (i = 0; i < nbits; i += 2)
+		set_bit(i, (unsigned long *)bitmap);
 
 	test_cmd_mock_domain_set_dirty(fd, hwpt_id, length, iova, page_size,
 				       bitmap, &out_dirty);
@@ -311,9 +309,10 @@ static int _test_mock_dirty_bitmaps(int fd, __u32 hwpt_id, size_t length,
 	memset(bitmap, 0, bitmap_size);
 	test_cmd_get_dirty_bitmap(fd, hwpt_id, length, iova, page_size, bitmap,

tools/testing/selftests/iommu/iommufd_utils.h static int _test_mock_dirty_bitmaps(int fd, __u32 hwpt_id, size_t length, · line 318

Quoted 10 lines
tools/testing/selftests/iommu/iommufd_utils.h +3 -2
 				  flags);
-	for (count = 0, i = 0; i < nbits; count += !(i % 2), i++)
+	/* Beware ASSERT_EQ() is two statements -- braces are not redundant! */
+	for (i = 0; i < nbits; i++) {
 		ASSERT_EQ(!(i % 2), test_bit(i, (unsigned long *)bitmap));
-	ASSERT_EQ(count, out_dirty);
+	}
 
 	memset(bitmap, 0, bitmap_size);
 	test_cmd_get_dirty_bitmap(fd, hwpt_id, length, iova, page_size, bitmap,
#2 lore raw
Quoted 13 lines · Joao Martins
Quoted 3 lines · Robin Murphy

The ASSERT_EQ() macro sneakily expands to two statements, so the loop here needs braces to ensure it captures both and actually terminates the test upon failure.

Ugh

Quoted 6 lines

Where these tests are currently failing on my arm64
machine, this reduces the number of logged lines from a rather unreasonable ~197,000 down to 10. While we're at it, we can also clean up the tautologous "count" calculations whose assertions can never fail unless mathematics and/or the C language become fundamentally broken.

Quoted 7 lines
Quoted 2 lines
Fixes a9af47e382a4 ("iommufd/selftest: Test IOMMU_HWPT_GET_DIRTY_BITMAP")

I was going to say that the second assert is useful, but we are already test the number of bits we set against what the mock domain set after mock_domain_set_dirty(). So the second is redundantly testing the same, and can be removed as you are doing. Thanks for fixing this.

Yeah, it's still effectively just counting half the number of loop iterations executed, but since there's no control flow that could exit the loop early and still reach the assertion, it must always be true following the previous assertion that out_dirty == nr == nbits/2.

Quoted 6 lines

I would suggest the subject to:

iommufd/selftest: Fix _test_mock_dirty_bitmaps()

Because dirty-bitmap tests seems to imply the whole fixture, which covers more than the bitmaps.

Sure, that sounds reasonable. Jason, Kevin, would you want a v2 for that or could it be fixed up when applying?

Quoted 38 lines
Quoted 38 lines
---
  tools/testing/selftests/iommu/iommufd_utils.h | 13 ++++++-------
  1 file changed, 6 insertions(+), 7 deletions(-)
tools/testing/selftests/iommu/iommufd_utils.h +6 -7
index 050e9751321c..ad9202335656 100644
@@ -293,15 +293,13 @@ static int _test_mock_dirty_bitmaps(int fd, __u32 hwpt_id, size_t length,
  				    __u64 bitmap_size, __u32 flags,
  				    struct __test_metadata *_metadata)
  {
-	unsigned long i, count, nbits = bitmap_size * BITS_PER_BYTE;
+	unsigned long i, nbits = bitmap_size * BITS_PER_BYTE;
  	unsigned long nr = nbits / 2;
  	__u64 out_dirty = 0;
 
  	/* Mark all even bits as dirty in the mock domain */
-	for (count = 0, i = 0; i < nbits; count += !(i % 2), i++)
-		if (!(i % 2))
-			set_bit(i, (unsigned long *)bitmap);
-	ASSERT_EQ(nr, count);
+	for (i = 0; i < nbits; i += 2)
+		set_bit(i, (unsigned long *)bitmap);
 
  	test_cmd_mock_domain_set_dirty(fd, hwpt_id, length, iova, page_size,
  				       bitmap, &out_dirty);
@@ -311,9 +309,10 @@ static int _test_mock_dirty_bitmaps(int fd, __u32 hwpt_id, size_t length,
  	memset(bitmap, 0, bitmap_size);
  	test_cmd_get_dirty_bitmap(fd, hwpt_id, length, iova, page_size, bitmap,
  				  flags);
-	for (count = 0, i = 0; i < nbits; count += !(i % 2), i++)
+	/* Beware ASSERT_EQ() is two statements -- braces are not redundant! */
+	for (i = 0; i < nbits; i++) {
  		ASSERT_EQ(!(i % 2), test_bit(i, (unsigned long *)bitmap));
-	ASSERT_EQ(count, out_dirty);
+	}
 
Quoted 5 lines

tools/testing/selftests/iommu/iommufd_utils.h static int _test_mock_dirty_bitmaps(int fd, __u32 hwpt_id, size_t length, · line 318

Quoted 2 lines
tools/testing/selftests/iommu/iommufd_utils.h +0 -0
  	memset(bitmap, 0, bitmap_size);
  	test_cmd_get_dirty_bitmap(fd, hwpt_id, length, iova, page_size, bitmap,

Thanks!

Robin.

maintainer Reviewed-by
#3 lore raw
Quoted 35 lines

From: Robin Murphy <[email protected]>
Sent: Friday, November 17, 2023 1:44 AM

Quoted 14 lines · Joao Martins
Quoted 3 lines · Robin Murphy

The ASSERT_EQ() macro sneakily expands to two statements, so the loop here needs braces to ensure it captures both and actually terminates the test upon failure.

Ugh

Quoted 7 lines

Where these tests are currently failing on my arm64
machine, this reduces the number of logged lines from a rather unreasonable ~197,000 down to 10. While we're at it, we can also clean up the tautologous "count" calculations whose assertions can never fail unless mathematics and/or the C language become fundamentally broken.

Fixes a9af47e382a4 ("iommufd/selftest: Test

IOMMU_HWPT_GET_DIRTY_BITMAP")

Quoted 3 lines
Quoted 1 line

I was going to say that the second assert is useful, but we are already test

the

Quoted 2 lines

number of bits we set against what the mock domain set after mock_domain_set_dirty(). So the second is redundantly testing the same,

and can

Quoted 1 line

be removed as you are doing. Thanks for fixing this.

Yeah, it's still effectively just counting half the number of loop iterations executed, but since there's no control flow that could exit the loop early and still reach the assertion, it must always be true following the previous assertion that out_dirty == nr == nbits/2.

Quoted 2 lines

I would suggest the subject to:

Quoted 9 lines
Quoted 3 lines

iommufd/selftest: Fix _test_mock_dirty_bitmaps()

Because dirty-bitmap tests seems to imply the whole fixture, which covers

more

Quoted 1 line

than the bitmaps.

Sure, that sounds reasonable. Jason, Kevin, would you want a v2 for that or could it be fixed up when applying?

Jason can help fix it when applying.

#4 lore raw
Quoted 41 lines · Kevin
Quoted 41 lines

From: Robin Murphy <[email protected]>
Sent: Friday, November 17, 2023 1:44 AM

Quoted 14 lines · Joao Martins
Quoted 3 lines · Robin Murphy

The ASSERT_EQ() macro sneakily expands to two statements, so the loop here needs braces to ensure it captures both and actually terminates the test upon failure.

Ugh

Quoted 7 lines

Where these tests are currently failing on my arm64
machine, this reduces the number of logged lines from a rather unreasonable ~197,000 down to 10. While we're at it, we can also clean up the tautologous "count" calculations whose assertions can never fail unless mathematics and/or the C language become fundamentally broken.

Fixes a9af47e382a4 ("iommufd/selftest: Test

IOMMU_HWPT_GET_DIRTY_BITMAP")

Quoted 3 lines
Quoted 1 line

I was going to say that the second assert is useful, but we are already test

the

Quoted 2 lines

number of bits we set against what the mock domain set after mock_domain_set_dirty(). So the second is redundantly testing the same,

and can

Quoted 1 line

be removed as you are doing. Thanks for fixing this.

Yeah, it's still effectively just counting half the number of loop iterations executed, but since there's no control flow that could exit the loop early and still reach the assertion, it must always be true following the previous assertion that out_dirty == nr == nbits/2.

Quoted 5 lines

I would suggest the subject to:

iommufd/selftest: Fix _test_mock_dirty_bitmaps()

Because dirty-bitmap tests seems to imply the whole fixture, which covers

more

Quoted 1 line

than the bitmaps.

Quoted 7 lines
Quoted 3 lines

Sure, that sounds reasonable. Jason, Kevin, would you want a v2 for that or could it be fixed up when applying?

Jason can help fix it when applying.

Done

Thanks,
Jason