From: Andrea Parri <parri.andrea@gmail.com>
To: Jason Gunthorpe <jgg@ziepe.ca>, Kevin Tian <kevin.tian@intel.com>
Cc: Andrea Parri <parri.andrea@gmail.com>,
Joerg Roedel <joro@8bytes.org>, Will Deacon <will@kernel.org>,
Robin Murphy <robin.murphy@arm.com>,
Shuah Khan <shuah@kernel.org>,
Joao Martins <joao.m.martins@oracle.com>,
Alex Williamson <alex@shazbot.org>,
Yishai Hadas <yishaih@nvidia.com>,
iommu@lists.linux.dev, linux-kernel@vger.kernel.org,
linux-kselftest@vger.kernel.org
Subject: [PATCH 2/4] iommufd/selftest: Test dirty bitmap recording past the query
Date: Fri, 25 Sep 2026 18:11:38 +0200 [thread overview]
Message-ID: <20260925161142.189813-3-parri.andrea@gmail.com> (raw)
In-Reply-To: <20260925161142.189813-1-parri.andrea@gmail.com>
Add a dirty-tracking test that dirties a huge IOPTE and queries only part
of it, with a guard placed after an exactly sized bitmap.
The test maps hugetlb-backed memory, allocates a MOCK_IOMMUPT_HUGE
dirty-tracking HWPT, and dirties the first 1M IOPTE. It then queries a
128K window at the mock 2K page size, so the request covers 64 bits while
the tracker records 512. A guard follows the one-u64 bitmap in the same
pinned page.
Without the clip in iova_bitmap_set(), the kernel sets 56 bytes past the
bitmap and the guard check fails:
# iommufd.c:2470:get_dirty_bitmap_overrun:Expected 0xaaaaaaaaaaaaaaaaULL (12297829382473034410) == bitmap[bitmap_words + i] (18446744073709551615)
# FAIL iommufd_dirty_tracking.domain_dirty64M_huge.get_dirty_bitmap_overrun
With the fix the guard stays intact. The test runs only on the *_huge
variants and skips the rest. Tested on x86-64 under virtme-ng with
CONFIG_IOMMUFD_TEST=y and hugepages=128 on the kernel command line; the
whole iommufd_dirty_tracking fixture reports PASSED: 63/63, the seven
skips being this test on the non-huge variants.
Assisted-by: LLM
Signed-off-by: Andrea Parri <parri.andrea@gmail.com>
---
tools/testing/selftests/iommu/iommufd.c | 49 +++++++++++++++++++++++++
1 file changed, 49 insertions(+)
diff --git a/tools/testing/selftests/iommu/iommufd.c b/tools/testing/selftests/iommu/iommufd.c
index 44193d171ba98..47d861b3bdd7b 100644
--- a/tools/testing/selftests/iommu/iommufd.c
+++ b/tools/testing/selftests/iommu/iommufd.c
@@ -2423,6 +2423,55 @@ TEST_F(iommufd_dirty_tracking, get_dirty_bitmap_no_clear)
test_ioctl_destroy(hwpt_id);
}
+TEST_F(iommufd_dirty_tracking, get_dirty_bitmap_overrun)
+{
+ uint32_t ioas_id = self->ioas_id;
+ uint32_t hwpt_id;
+ size_t nbits = 64;
+ size_t query_len = nbits * self->page_size;
+ __u64 *bitmap = self->bitmap;
+ size_t bitmap_words =
+ DIV_ROUND_UP(nbits, BITS_PER_BYTE * sizeof(*bitmap));
+ __u64 set = 1;
+ __u64 nr_dirty;
+ size_t i;
+
+ if (!variant->hugepages)
+ SKIP(return, "test requires a huge IOPTE");
+
+ /* The canary sits in the same pinned page, right after the bitmap. */
+ memset(bitmap, 0, (bitmap_words + 8) * sizeof(*bitmap));
+ for (i = 0; i < 8; i++)
+ bitmap[bitmap_words + i] = 0xaaaaaaaaaaaaaaaaULL;
+
+ test_ioctl_ioas_map_fixed_id(ioas_id, self->buffer,
+ variant->buffer_size, MOCK_APERTURE_START);
+ test_cmd_hwpt_alloc_iommupt(self->idev_id, ioas_id,
+ IOMMU_HWPT_ALLOC_DIRTY_TRACKING,
+ MOCK_IOMMUPT_HUGE, &hwpt_id);
+ test_cmd_set_dirty_tracking(hwpt_id, true);
+
+ /* Dirty the whole first huge IOPTE */
+ test_cmd_mock_domain_set_dirty(self->fd, hwpt_id, MOCK_HUGE_PAGE_SIZE,
+ MOCK_APERTURE_START, MOCK_HUGE_PAGE_SIZE,
+ &set, &nr_dirty);
+ ASSERT_EQ(1, nr_dirty);
+
+ /* Query only part of it */
+ test_cmd_get_dirty_bitmap(self->fd, hwpt_id, query_len,
+ MOCK_APERTURE_START, self->page_size, bitmap,
+ 0);
+
+ /* The queried window is dirty */
+ ASSERT_EQ(~0ULL, bitmap[0]);
+
+ /* Bits past the queried bitmap must not be set */
+ for (i = 0; i < 8; i++)
+ ASSERT_EQ(0xaaaaaaaaaaaaaaaaULL, bitmap[bitmap_words + i]);
+
+ test_ioctl_destroy(hwpt_id);
+}
+
/* VFIO compatibility IOCTLs */
TEST_F(iommufd, simple_ioctls)
--
2.53.0
next prev parent reply other threads:[~2026-09-25 16:12 UTC|newest]
Thread overview: 5+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-09-25 16:11 [PATCH 0/4] iommufd: Keep IOMMU_HWPT_GET_DIRTY_BITMAP within the user bitmap Andrea Parri
2026-09-25 16:11 ` [PATCH 1/4] iommufd/iova_bitmap: Clip recorded ranges to the bitmap Andrea Parri
2026-09-25 16:11 ` Andrea Parri [this message]
2026-09-25 16:11 ` [PATCH 3/4] iommufd: Reject a zero-length dirty bitmap request Andrea Parri
2026-09-25 16:11 ` [PATCH 4/4] iommufd/selftest: Test " Andrea Parri
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20260925161142.189813-3-parri.andrea@gmail.com \
--to=parri.andrea@gmail.com \
--cc=alex@shazbot.org \
--cc=iommu@lists.linux.dev \
--cc=jgg@ziepe.ca \
--cc=joao.m.martins@oracle.com \
--cc=joro@8bytes.org \
--cc=kevin.tian@intel.com \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-kselftest@vger.kernel.org \
--cc=robin.murphy@arm.com \
--cc=shuah@kernel.org \
--cc=will@kernel.org \
--cc=yishaih@nvidia.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®