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

From: Jason Gunthorpe
Date: Wed Jun 11 2025 - 19:51:35 EST


On Wed, Jun 11, 2025 at 04:43:00PM -0700, Nicolin Chen wrote:
> So, the test case sets an alignment with HUGEPAGE_SIZE=512MB while
> allocating buffer_size=64MB:
> rc = posix_memalign(&self->buffer, HUGEPAGE_SIZE, variant->buffer_size);
> vrc = mmap(self->buffer, variant->buffer_size, PROT_READ | PROT_WRITE,
> this gives the self->buffer a location that is 512MB aligned, but
> only mmap part of one 512MB huge page.
>
> On the other hand, _metadata->no_teardown was mmap() outside the
> range of the [self->buffer, self->buffer + 64MB), but within the
> range of [self->buffer, self->buffer + 512MB).
>
> E.g.
> _metadata->no_teardown = 0xfffbfc610000 // inside range2 below
> buffer=[0xfffbe0000000, fffbe4000000) // range1
> buffer=[0xfffbe0000000, fffc00000000) // range2
>
> Then ,the "vrc = mmap(..." overwrites the _metadata->no_teardown
> location to NULL..
>
> The following change can fix, though it feels odd that the buffer
> has to be preserved with the entire huge page:
> ---------------------------------------------------------------
> @@ -2024,3 +2027,4 @@ FIXTURE_SETUP(iommufd_dirty_tracking)
>
> - rc = posix_memalign(&self->buffer, HUGEPAGE_SIZE, variant->buffer_size);
> + rc = posix_memalign(&self->buffer, HUGEPAGE_SIZE,
> + __ALIGN_KERNEL(variant->buffer_size, HUGEPAGE_SIZE));
> if (rc || !self->buffer) {
> ---------------------------------------------------------------
>
> Any thought?

This seems like something, variant->buffer_size should not
be less than HUGEPAGE_SIZE I guess that is possible on 64K ARM64

But I still don't quite get it..

rc = posix_memalign(&self->buffer, HUGEPAGE_SIZE, variant->buffer_size);

Should allocate buffer_size

mmap_flags = MAP_SHARED | MAP_ANONYMOUS | MAP_FIXED;
mmap_flags |= MAP_HUGETLB | MAP_POPULATE;
vrc = mmap(self->buffer, variant->buffer_size, PROT_READ | PROT_WRITE,
mmap_flags, -1, 0);

Should fail if buffer_size is not a multiple of HUGEPAGE_SIZE?

It certainly shouldn't mmap past the provided buffer_size!!!

Are you seeing the above mmap succeed and also map beyond buffer -> buffer + buffer_size?

I think that would be a kernel bug in MAP_HUGETLB!

Jason