Skip to content

Commit 407fc18

Browse files
jpemartinsjgunthorpe
authored andcommitted
iommufd/selftest: Refactor dirty bitmap tests
Rework the functions that test and set the bitmaps to receive a new parameter (the pte_page_size) that reflects the expected PTE size in the page tables. The same scheme is still used i.e. even bits are dirty and odd page indexes aren't dirty. Here it just refactors to consider the size of the PTE rather than hardcoded to IOMMU mock base page assumptions. While at it, refactor dirty bitmap tests to use the idev_id created by the fixture instead of creating a new one. This is in preparation for doing tests with IOMMU hugepages where multiple bits set as part of recording a whole hugepage as dirty and thus the pte_page_size will vary depending on io hugepages or io base pages. Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Joao Martins <[email protected]> Signed-off-by: Jason Gunthorpe <[email protected]>
1 parent 2780025 commit 407fc18

File tree

2 files changed

+36
-31
lines changed

2 files changed

+36
-31
lines changed

tools/testing/selftests/iommu/iommufd.c

Lines changed: 11 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1849,7 +1849,7 @@ TEST_F(iommufd_dirty_tracking, device_dirty_capability)
18491849

18501850
TEST_F(iommufd_dirty_tracking, get_dirty_bitmap)
18511851
{
1852-
uint32_t stddev_id;
1852+
uint32_t page_size = MOCK_PAGE_SIZE;
18531853
uint32_t hwpt_id;
18541854
uint32_t ioas_id;
18551855

@@ -1859,34 +1859,31 @@ TEST_F(iommufd_dirty_tracking, get_dirty_bitmap)
18591859

18601860
test_cmd_hwpt_alloc(self->idev_id, ioas_id,
18611861
IOMMU_HWPT_ALLOC_DIRTY_TRACKING, &hwpt_id);
1862-
test_cmd_mock_domain(hwpt_id, &stddev_id, NULL, NULL);
18631862

18641863
test_cmd_set_dirty_tracking(hwpt_id, true);
18651864

18661865
test_mock_dirty_bitmaps(hwpt_id, variant->buffer_size,
1867-
MOCK_APERTURE_START, self->page_size,
1866+
MOCK_APERTURE_START, self->page_size, page_size,
18681867
self->bitmap, self->bitmap_size, 0, _metadata);
18691868

18701869
/* PAGE_SIZE unaligned bitmap */
18711870
test_mock_dirty_bitmaps(hwpt_id, variant->buffer_size,
1872-
MOCK_APERTURE_START, self->page_size,
1871+
MOCK_APERTURE_START, self->page_size, page_size,
18731872
self->bitmap + MOCK_PAGE_SIZE,
18741873
self->bitmap_size, 0, _metadata);
18751874

18761875
/* u64 unaligned bitmap */
18771876
test_mock_dirty_bitmaps(hwpt_id, variant->buffer_size,
1878-
MOCK_APERTURE_START, self->page_size,
1879-
self->bitmap + 0xff1,
1880-
self->bitmap_size, 0, _metadata);
1881-
1877+
MOCK_APERTURE_START, self->page_size, page_size,
1878+
self->bitmap + 0xff1, self->bitmap_size, 0,
1879+
_metadata);
18821880

1883-
test_ioctl_destroy(stddev_id);
18841881
test_ioctl_destroy(hwpt_id);
18851882
}
18861883

18871884
TEST_F(iommufd_dirty_tracking, get_dirty_bitmap_no_clear)
18881885
{
1889-
uint32_t stddev_id;
1886+
uint32_t page_size = MOCK_PAGE_SIZE;
18901887
uint32_t hwpt_id;
18911888
uint32_t ioas_id;
18921889

@@ -1896,33 +1893,30 @@ TEST_F(iommufd_dirty_tracking, get_dirty_bitmap_no_clear)
18961893

18971894
test_cmd_hwpt_alloc(self->idev_id, ioas_id,
18981895
IOMMU_HWPT_ALLOC_DIRTY_TRACKING, &hwpt_id);
1899-
test_cmd_mock_domain(hwpt_id, &stddev_id, NULL, NULL);
19001896

19011897
test_cmd_set_dirty_tracking(hwpt_id, true);
19021898

19031899
test_mock_dirty_bitmaps(hwpt_id, variant->buffer_size,
1904-
MOCK_APERTURE_START, self->page_size,
1900+
MOCK_APERTURE_START, self->page_size, page_size,
19051901
self->bitmap, self->bitmap_size,
19061902
IOMMU_HWPT_GET_DIRTY_BITMAP_NO_CLEAR,
19071903
_metadata);
19081904

19091905
/* Unaligned bitmap */
19101906
test_mock_dirty_bitmaps(hwpt_id, variant->buffer_size,
1911-
MOCK_APERTURE_START, self->page_size,
1907+
MOCK_APERTURE_START, self->page_size, page_size,
19121908
self->bitmap + MOCK_PAGE_SIZE,
19131909
self->bitmap_size,
19141910
IOMMU_HWPT_GET_DIRTY_BITMAP_NO_CLEAR,
19151911
_metadata);
19161912

19171913
/* u64 unaligned bitmap */
19181914
test_mock_dirty_bitmaps(hwpt_id, variant->buffer_size,
1919-
MOCK_APERTURE_START, self->page_size,
1920-
self->bitmap + 0xff1,
1921-
self->bitmap_size,
1915+
MOCK_APERTURE_START, self->page_size, page_size,
1916+
self->bitmap + 0xff1, self->bitmap_size,
19221917
IOMMU_HWPT_GET_DIRTY_BITMAP_NO_CLEAR,
19231918
_metadata);
19241919

1925-
test_ioctl_destroy(stddev_id);
19261920
test_ioctl_destroy(hwpt_id);
19271921
}
19281922

tools/testing/selftests/iommu/iommufd_utils.h

Lines changed: 25 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -344,16 +344,19 @@ static int _test_cmd_mock_domain_set_dirty(int fd, __u32 hwpt_id, size_t length,
344344
page_size, bitmap, nr))
345345

346346
static int _test_mock_dirty_bitmaps(int fd, __u32 hwpt_id, size_t length,
347-
__u64 iova, size_t page_size, __u64 *bitmap,
347+
__u64 iova, size_t page_size,
348+
size_t pte_page_size, __u64 *bitmap,
348349
__u64 bitmap_size, __u32 flags,
349350
struct __test_metadata *_metadata)
350351
{
351-
unsigned long i, nbits = bitmap_size * BITS_PER_BYTE;
352-
unsigned long nr = nbits / 2;
352+
unsigned long npte = pte_page_size / page_size, pteset = 2 * npte;
353+
unsigned long nbits = bitmap_size * BITS_PER_BYTE;
354+
unsigned long j, i, nr = nbits / pteset ?: 1;
353355
__u64 out_dirty = 0;
354356

355357
/* Mark all even bits as dirty in the mock domain */
356-
for (i = 0; i < nbits; i += 2)
358+
memset(bitmap, 0, bitmap_size);
359+
for (i = 0; i < nbits; i += pteset)
357360
set_bit(i, (unsigned long *)bitmap);
358361

359362
test_cmd_mock_domain_set_dirty(fd, hwpt_id, length, iova, page_size,
@@ -365,28 +368,36 @@ static int _test_mock_dirty_bitmaps(int fd, __u32 hwpt_id, size_t length,
365368
test_cmd_get_dirty_bitmap(fd, hwpt_id, length, iova, page_size, bitmap,
366369
flags);
367370
/* Beware ASSERT_EQ() is two statements -- braces are not redundant! */
368-
for (i = 0; i < nbits; i++) {
369-
ASSERT_EQ(!(i % 2), test_bit(i, (unsigned long *)bitmap));
371+
for (i = 0; i < nbits; i += pteset) {
372+
for (j = 0; j < pteset; j++) {
373+
ASSERT_EQ(j < npte,
374+
test_bit(i + j, (unsigned long *)bitmap));
375+
}
376+
ASSERT_EQ(!(i % pteset), test_bit(i, (unsigned long *)bitmap));
370377
}
371378

372379
memset(bitmap, 0, bitmap_size);
373380
test_cmd_get_dirty_bitmap(fd, hwpt_id, length, iova, page_size, bitmap,
374381
flags);
375382

376383
/* It as read already -- expect all zeroes */
377-
for (i = 0; i < nbits; i++) {
378-
ASSERT_EQ(!(i % 2) && (flags &
379-
IOMMU_HWPT_GET_DIRTY_BITMAP_NO_CLEAR),
380-
test_bit(i, (unsigned long *)bitmap));
384+
for (i = 0; i < nbits; i += pteset) {
385+
for (j = 0; j < pteset; j++) {
386+
ASSERT_EQ(
387+
(j < npte) &&
388+
(flags &
389+
IOMMU_HWPT_GET_DIRTY_BITMAP_NO_CLEAR),
390+
test_bit(i + j, (unsigned long *)bitmap));
391+
}
381392
}
382393

383394
return 0;
384395
}
385-
#define test_mock_dirty_bitmaps(hwpt_id, length, iova, page_size, bitmap, \
386-
bitmap_size, flags, _metadata) \
396+
#define test_mock_dirty_bitmaps(hwpt_id, length, iova, page_size, pte_size,\
397+
bitmap, bitmap_size, flags, _metadata) \
387398
ASSERT_EQ(0, _test_mock_dirty_bitmaps(self->fd, hwpt_id, length, iova, \
388-
page_size, bitmap, bitmap_size, \
389-
flags, _metadata))
399+
page_size, pte_size, bitmap, \
400+
bitmap_size, flags, _metadata))
390401

391402
static int _test_cmd_create_access(int fd, unsigned int ioas_id,
392403
__u32 *access_id, unsigned int flags)

0 commit comments

Comments
 (0)