* [PATCH] iommufd/selftest: Fix dirty_bitmap tests
@ 2023-11-16 16:52 Robin Murphy
2023-11-16 17:28 ` Joao Martins
0 siblings, 1 reply; 5+ messages in thread
From: Robin Murphy @ 2023-11-16 16:52 UTC (permalink / raw)
To: kevin.tian, jgg; +Cc: joao.m.martins, iommu, linux-kernel
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")
Signed-off-by: Robin Murphy <robin.murphy@arm.com>
---
tools/testing/selftests/iommu/iommufd_utils.h | 13 ++++++-------
1 file changed, 6 insertions(+), 7 deletions(-)
diff --git a/tools/testing/selftests/iommu/iommufd_utils.h b/tools/testing/selftests/iommu/iommufd_utils.h
index 050e9751321c..ad9202335656 100644
--- a/tools/testing/selftests/iommu/iommufd_utils.h
+++ b/tools/testing/selftests/iommu/iommufd_utils.h
@@ -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,
--
2.39.2.101.g768bb238c484.dirty
^ permalink raw reply related [flat|nested] 5+ messages in thread
* Re: [PATCH] iommufd/selftest: Fix dirty_bitmap tests
2023-11-16 16:52 [PATCH] iommufd/selftest: Fix dirty_bitmap tests Robin Murphy
@ 2023-11-16 17:28 ` Joao Martins
2023-11-16 17:43 ` Robin Murphy
0 siblings, 1 reply; 5+ messages in thread
From: Joao Martins @ 2023-11-16 17:28 UTC (permalink / raw)
To: Robin Murphy, kevin.tian, jgg; +Cc: iommu, linux-kernel
On 16/11/2023 16:52, Robin Murphy wrote:
> 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
> 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")
> Signed-off-by: Robin Murphy <robin.murphy@arm.com>
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.
> ---
> tools/testing/selftests/iommu/iommufd_utils.h | 13 ++++++-------
> 1 file changed, 6 insertions(+), 7 deletions(-)
>
> diff --git a/tools/testing/selftests/iommu/iommufd_utils.h b/tools/testing/selftests/iommu/iommufd_utils.h
> index 050e9751321c..ad9202335656 100644
> --- a/tools/testing/selftests/iommu/iommufd_utils.h
> +++ b/tools/testing/selftests/iommu/iommufd_utils.h
> @@ -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,
Reviewed-by: Joao Martins <joao.m.martins@oracle.com>
Tested-by: Joao Martins <joao.m.martins@oracle.com>
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [PATCH] iommufd/selftest: Fix dirty_bitmap tests
2023-11-16 17:28 ` Joao Martins
@ 2023-11-16 17:43 ` Robin Murphy
2023-11-17 3:15 ` Tian, Kevin
0 siblings, 1 reply; 5+ messages in thread
From: Robin Murphy @ 2023-11-16 17:43 UTC (permalink / raw)
To: Joao Martins, kevin.tian, jgg; +Cc: iommu, linux-kernel
On 16/11/2023 5:28 pm, Joao Martins wrote:
> On 16/11/2023 16:52, Robin Murphy wrote:
>> 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
>
>> 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")
>> Signed-off-by: Robin Murphy <robin.murphy@arm.com>
>
> 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.
> 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?
>> ---
>> tools/testing/selftests/iommu/iommufd_utils.h | 13 ++++++-------
>> 1 file changed, 6 insertions(+), 7 deletions(-)
>>
>> diff --git a/tools/testing/selftests/iommu/iommufd_utils.h b/tools/testing/selftests/iommu/iommufd_utils.h
>> index 050e9751321c..ad9202335656 100644
>> --- a/tools/testing/selftests/iommu/iommufd_utils.h
>> +++ b/tools/testing/selftests/iommu/iommufd_utils.h
>> @@ -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,
>
> Reviewed-by: Joao Martins <joao.m.martins@oracle.com>
> Tested-by: Joao Martins <joao.m.martins@oracle.com>
Thanks!
Robin.
^ permalink raw reply [flat|nested] 5+ messages in thread
* RE: [PATCH] iommufd/selftest: Fix dirty_bitmap tests
2023-11-16 17:43 ` Robin Murphy
@ 2023-11-17 3:15 ` Tian, Kevin
2023-11-21 1:39 ` Jason Gunthorpe
0 siblings, 1 reply; 5+ messages in thread
From: Tian, Kevin @ 2023-11-17 3:15 UTC (permalink / raw)
To: Robin Murphy, Joao Martins, jgg@ziepe.ca
Cc: iommu@lists.linux.dev, linux-kernel@vger.kernel.org
> From: Robin Murphy <robin.murphy@arm.com>
> Sent: Friday, November 17, 2023 1:44 AM
>
> On 16/11/2023 5:28 pm, Joao Martins wrote:
> > On 16/11/2023 16:52, Robin Murphy wrote:
> >> 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
> >
> >> 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")
> >> Signed-off-by: Robin Murphy <robin.murphy@arm.com>
> >
> > 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.
>
> > 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?
>
Jason can help fix it when applying.
Reviewed-by: Kevin Tian <kevin.tian@intel.com>
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [PATCH] iommufd/selftest: Fix dirty_bitmap tests
2023-11-17 3:15 ` Tian, Kevin
@ 2023-11-21 1:39 ` Jason Gunthorpe
0 siblings, 0 replies; 5+ messages in thread
From: Jason Gunthorpe @ 2023-11-21 1:39 UTC (permalink / raw)
To: Tian, Kevin
Cc: Robin Murphy, Joao Martins, iommu@lists.linux.dev,
linux-kernel@vger.kernel.org
On Fri, Nov 17, 2023 at 03:15:25AM +0000, Tian, Kevin wrote:
> > From: Robin Murphy <robin.murphy@arm.com>
> > Sent: Friday, November 17, 2023 1:44 AM
> >
> > On 16/11/2023 5:28 pm, Joao Martins wrote:
> > > On 16/11/2023 16:52, Robin Murphy wrote:
> > >> 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
> > >
> > >> 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")
> > >> Signed-off-by: Robin Murphy <robin.murphy@arm.com>
> > >
> > > 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.
> >
> > > 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?
> >
>
> Jason can help fix it when applying.
>
> Reviewed-by: Kevin Tian <kevin.tian@intel.com>
Done
Thanks,
Jason
^ permalink raw reply [flat|nested] 5+ messages in thread
end of thread, other threads:[~2023-11-21 1:39 UTC | newest]
Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2023-11-16 16:52 [PATCH] iommufd/selftest: Fix dirty_bitmap tests Robin Murphy
2023-11-16 17:28 ` Joao Martins
2023-11-16 17:43 ` Robin Murphy
2023-11-17 3:15 ` Tian, Kevin
2023-11-21 1:39 ` Jason Gunthorpe
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox