Re: [RFC PATCH 7/7] vfio/selftests: Add observability test for IO page table usage

From: David Matlack

Date: Wed Nov 25 2026 - 19:33:41 EST


On Thu Oct 1, 2026 at 3:45 PM PDT, Pranjal Shrivastava wrote:
> Add a new selftest, vfio_nr_pages_test, to validate the iommu-nr-pages
> fdinfo metric across all IOMMU backends (Type 1 and IOMMUFD native/compat).

What's the status of this series? Should we expect another version
(e.g. after the discussion on the shrinker RFC and at LPC)?

nit: Please use "vfio: selftests:" as the shortlog prefix for VFIO
selftests (see Documentation/driver-api/vfio-selftests.rst).

I gave this a try in QEMU with an emulated Intel IOMMU (which uses
generic_pt, so it reports iommu-nr-pages), on top of this series. It
fails in every mode except iommufd, which skips. Details below.

[...]

> +static long read_nr_pages(struct iommu *iommu)
> +{
> + char path[256];
> + char line[128];
> + FILE *f;
> + long val = -1;
> + int fd = iommu->container_fd != -1 ? iommu->container_fd : iommu->iommufd;

In iommufd mode container_fd is 0, not -1, so this reads the fdinfo for
stdin, never finds the key, and the test skips with "iommu-nr-pages not
reported by this IOMMU". libvfio uses a non-zero iommufd to mean
iommufd mode, so:

int fd = iommu->iommufd ? iommu->iommufd : iommu->container_fd;

Also, consider moving this into the VFIO selftests library (lib/iommu.c
and libvfio/iommu.h), e.g. as iommu_nr_pages(), so other tests can use
it and the library can hide which fd to read.

[...]

> +FIXTURE_SETUP(vfio_nr_pages_test)
> +{
> + self->iommu = iommu_init(variant->iommu_mode);
> + if (!self->iommu)
> + SKIP(return, "IOMMU mode %s not supported", variant->iommu_mode);
> +
> + self->device = vfio_pci_device_init(device_bdf, self->iommu);
> + if (!self->device) {
> + iommu_cleanup(self->iommu);
> + SKIP(return, "Failed to initialize VFIO device");
> + }
> +}

iommu_init() and vfio_pci_device_init() assert on failure and never
return NULL, so these checks (and the matching ones in the teardown) can
be dropped. Please follow the setup in the other VFIO selftests, e.g.
vfio_dma_mapping_test.c.

[...]

> + if (!self->iommu || !self->device)
> + SKIP(return, "Fixture setup failed");

This can be dropped too. On top of the above, the harness doesn't run
the test body at all if FIXTURE_SETUP() skips or fails.

[...]

> + /* Map a page at start */
> + region1.size = SZ_4K;
> + region1.vaddr = mmap_reserve(region1.size, region1.size, 0);
> + ASSERT_NE(MAP_FAILED, region1.vaddr);
> + region1.iova = 0x100000;
> + iommu_map(self->iommu, &region1);

mmap_reserve() returns a PROT_NONE reservation (it's meant for reserving
aligned virtual address space, e.g. for mapping BARs), so there's
nothing for VFIO or iommufd to pin, and iommu_map() fails with -EFAULT:

# RUN vfio_nr_pages_test.vfio_type1_iommu.sparse_mapping_sticky_reclamation ...
# .../libvfio/iommu.h:42: Assertion Failure
# Expression: __iommu_map(iommu, region) == 0
# Observed: 0xfffffffffffffff2 == 0
# [errno: 14 - Bad address]

Please mmap() real anonymous memory instead (PROT_READ | PROT_WRITE,
MAP_PRIVATE | MAP_ANONYMOUS). With that and the fd fix above, the test
passes in all 5 modes.

Also, please allocate the IOVAs with iova_allocator rather than
hardcoding them, so the test doesn't trip over reserved IOVA ranges on
other platforms. e.g. allocate SZ_2G and use the start and start +
SZ_1G.

[...]

> + nr_pages_final = read_nr_pages(self->iommu);
> +
> + /* Page tables should remain allocated */
> + ASSERT_EQ(nr_pages_final, nr_pages_mapped);

This asserts the behavior that the cover letter describes as the
problem, and that the IO page table shrinker (or the clean(gap) approach
Jason suggested on that thread) is meant to change. The test will break
as soon as empty tables get freed.

I think it would be more useful to check that the accounting tracks the
page table in both directions. e.g. also map and unmap a range that
covers an entire table with a single unmap, which generic_pt frees
today, and check that iommu-nr-pages goes back down. For the sparse
case, ASSERT_GE(nr_pages_final, nr_pages_initial) would still catch
underflow without depending on when tables get freed.