Re: [PATCH v4 09/14] selftests: harness: Move teardown conditional into test metadata

From: Nicolin Chen
Date: Fri Jun 13 2025 - 01:55:37 EST


On Thu, Jun 12, 2025 at 08:31:38PM -0300, Jason Gunthorpe wrote:
> On Thu, Jun 12, 2025 at 12:03:14PM -0700, Nicolin Chen wrote:
> > On Thu, Jun 12, 2025 at 03:56:13PM -0300, Jason Gunthorpe wrote:
> > > On Thu, Jun 12, 2025 at 11:53:24AM -0700, Nicolin Chen wrote:
> > > > @@ -2022,7 +2023,19 @@ FIXTURE_SETUP(iommufd_dirty_tracking)
> > > > self->fd = open("/dev/iommu", O_RDWR);
> > > > ASSERT_NE(-1, self->fd);
> > > >
> > > > - rc = posix_memalign(&self->buffer, HUGEPAGE_SIZE, variant->buffer_size);
> > > > + if (variant->hugepages) {
> > > > + /*
> > > > + * Allocation must be aligned to the HUGEPAGE_SIZE, because the
> > > > + * following mmap() will automatically align the length to be a
> > > > + * multiple of the underlying huge page size. Failing to do the
> > > > + * same at this allocation will result in a memory overwrite by
> > > > + * the mmap().
> > > > + */
> > > > + size = __ALIGN_KERNEL(variant->buffer_size, HUGEPAGE_SIZE);
> > > > + } else {
> > > > + size = variant->buffer_size;
> > > > + }
> > > > + rc = posix_memalign(&self->buffer, HUGEPAGE_SIZE, size);
> > > > if (rc || !self->buffer) {
> > > > SKIP(return, "Skipping buffer_size=%lu due to errno=%d",
> > > > variant->buffer_size, rc);
> > > >
> > > > It can just upsize the allocation, i.e. the test case will only
> > > > use the first 64M or 128MB out of the reserved 512MB huge page.
> > >
> > > The MAP_HUGETLBFS is required that is the whole point of what it is
> > > doing..
> >
> > I am not quite following this.. MAP_HUGETLB will be still set.
> >
> > And the underlying selftest case is using:
> > MOCK_HUGE_PAGE_SIZE = 512 * MOCK_IO_PAGE_SIZE
> >
> > Does it matter if the underlying allocation has an overshot?
>
> I expect munmap won't work with the wrong size and the test will OOM?
>
> You'd be better to correct the actual variant->buffer_size..

I saw test passing, before I posted that.

But you are certainly right: while mmap() handling MAP_HUGETLB will
align up the size, the munmap() doesn't. So, passing in to them the
same variant->buffer_size will result in a size mismatch.

I don't think we should change the variant->buffer_size, because it
affects the bitmap sizes in those dirty_tracking test cases. And if
we align up every single variant->buffer_size, the variants of 64MB
and 128Mb will be two duplicated 512MB cases, right?

I think we can just add this on top of that:

FIXTURE_TEARDOWN(iommufd_dirty_tracking)
{
- munmap(self->buffer, variant->buffer_size);
- munmap(self->bitmap, DIV_ROUND_UP(self->bitmap_size, BITS_PER_BYTE));
+ unsigned long size = variant->buffer_size;
+
+ if (variant->hugepages)
+ size = __ALIGN_KERNEL(size, HUGEPAGE_SIZE);
+ munmap(self->buffer, size);
+ free(self->buffer);
+ free(self->bitmap);
teardown_iommufd(self->fd, _metadata);
}

This FIXTURE_TEARDOWN() didn't free the memory allocated by the two
posix_memalign calls in the FIXTURE_SETUP()..

Thanks
Nicolin