Skip to content

Commit ec61f82

Browse files
jpemartinsjgunthorpe
authored andcommitted
iommufd/selftest: Fix dirty bitmap tests with u8 bitmaps
With 64k base pages, the first 128k iova length test requires less than a byte for a bitmap, exposing a bug in the tests that assume that bitmaps are at least a byte. Rather than dealing with bytes, have _test_mock_dirty_bitmaps() pass the number of bits. The caller functions are adjusted to also use bits as well, and converting to bytes when clearing, allocating and freeing the bitmap. Link: https://lore.kernel.org/r/[email protected] Reported-by: Matt Ochs <[email protected]> Fixes: a9af47e ("iommufd/selftest: Test IOMMU_HWPT_GET_DIRTY_BITMAP") Signed-off-by: Joao Martins <[email protected]> Reviewed-by: Kevin Tian <[email protected]> Tested-by: Matt Ochs <[email protected]> Signed-off-by: Jason Gunthorpe <[email protected]>
1 parent f266106 commit ec61f82

File tree

2 files changed

+9
-7
lines changed

2 files changed

+9
-7
lines changed

tools/testing/selftests/iommu/iommufd.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1722,6 +1722,7 @@ FIXTURE_VARIANT(iommufd_dirty_tracking)
17221722

17231723
FIXTURE_SETUP(iommufd_dirty_tracking)
17241724
{
1725+
unsigned long size;
17251726
int mmap_flags;
17261727
void *vrc;
17271728
int rc;
@@ -1749,12 +1750,11 @@ FIXTURE_SETUP(iommufd_dirty_tracking)
17491750
assert(vrc == self->buffer);
17501751

17511752
self->page_size = MOCK_PAGE_SIZE;
1752-
self->bitmap_size =
1753-
variant->buffer_size / self->page_size / BITS_PER_BYTE;
1753+
self->bitmap_size = variant->buffer_size / self->page_size;
17541754

17551755
/* Provision with an extra (PAGE_SIZE) for the unaligned case */
1756-
rc = posix_memalign(&self->bitmap, PAGE_SIZE,
1757-
self->bitmap_size + PAGE_SIZE);
1756+
size = DIV_ROUND_UP(self->bitmap_size, BITS_PER_BYTE);
1757+
rc = posix_memalign(&self->bitmap, PAGE_SIZE, size + PAGE_SIZE);
17581758
assert(!rc);
17591759
assert(self->bitmap);
17601760
assert((uintptr_t)self->bitmap % PAGE_SIZE == 0);
@@ -1775,7 +1775,7 @@ FIXTURE_SETUP(iommufd_dirty_tracking)
17751775
FIXTURE_TEARDOWN(iommufd_dirty_tracking)
17761776
{
17771777
munmap(self->buffer, variant->buffer_size);
1778-
munmap(self->bitmap, self->bitmap_size);
1778+
munmap(self->bitmap, DIV_ROUND_UP(self->bitmap_size, BITS_PER_BYTE));
17791779
teardown_iommufd(self->fd, _metadata);
17801780
}
17811781

tools/testing/selftests/iommu/iommufd_utils.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222
#define BIT_MASK(nr) (1UL << ((nr) % __BITS_PER_LONG))
2323
#define BIT_WORD(nr) ((nr) / __BITS_PER_LONG)
2424

25+
#define DIV_ROUND_UP(n, d) (((n) + (d) - 1) / (d))
26+
2527
static inline void set_bit(unsigned int nr, unsigned long *addr)
2628
{
2729
unsigned long mask = BIT_MASK(nr);
@@ -346,12 +348,12 @@ static int _test_cmd_mock_domain_set_dirty(int fd, __u32 hwpt_id, size_t length,
346348
static int _test_mock_dirty_bitmaps(int fd, __u32 hwpt_id, size_t length,
347349
__u64 iova, size_t page_size,
348350
size_t pte_page_size, __u64 *bitmap,
349-
__u64 bitmap_size, __u32 flags,
351+
__u64 nbits, __u32 flags,
350352
struct __test_metadata *_metadata)
351353
{
352354
unsigned long npte = pte_page_size / page_size, pteset = 2 * npte;
353-
unsigned long nbits = bitmap_size * BITS_PER_BYTE;
354355
unsigned long j, i, nr = nbits / pteset ?: 1;
356+
unsigned long bitmap_size = DIV_ROUND_UP(nbits, BITS_PER_BYTE);
355357
__u64 out_dirty = 0;
356358

357359
/* Mark all even bits as dirty in the mock domain */

0 commit comments

Comments
 (0)