[PATCH 5/5] selftests/xsk: Introduce mmap_size in umem struct

Tushar Vyavahare
2026-04-28T08:46:24Z

UMEM teardown currently recomputes the `munmap()` length
from frame geometry (`num_frames * frame_size`, shared-UMEM
multiplier, and hugepage rounding).
That duplicates setup logic in cleanup and makes teardown depend on
re-deriving values instead of using the exact mapping metadata.

Store the finalized mapping length in `xsk_umem_info` as `mmap_size` when
the UMEM mapping is created, and use that value during teardown.

This removes duplicated size arithmetic in cleanup and guarantees that
`munmap()` uses the same canonical size that was established at setup time.

Signed-off-by: Magnus Karlsson <magnus.karlsson@intel.com>
Signed-off-by: Tushar Vyavahare <tushar.vyavahare@intel.com>
---
tools/testing/selftests/bpf/prog_tests/test_xsk.c | 9 +++------
tools/testing/selftests/bpf/prog_tests/test_xsk.h | 1 +
2 files changed, 4 insertions(+), 6 deletions(-)

diff --git a/tools/testing/selftests/bpf/prog_tests/test_xsk.c b/tools/testing/selftests/bpf/prog_tests/test_xsk.c
index ba4768f83eb6..ba1bcfc6b74b 100644
--- a/tools/testing/selftests/bpf/prog_tests/test_xsk.c
+++ b/tools/testing/selftests/bpf/prog_tests/test_xsk.c
@@ -1594,6 +1594,8 @@ static int thread_common_ops(struct test_spec *test, struct ifobject *ifobject)
if (bufs == MAP_FAILED)
return -errno;

+ umem->mmap_size = ceil_u64(umem_sz, HUGEPAGE_SIZE) * HUGEPAGE_SIZE;
+
ret = xsk_configure_umem(ifobject, umem, bufs, umem_sz);
if (ret)
return ret;
@@ -1700,14 +1702,9 @@ void *worker_testapp_validate_rx(void *arg)
static void testapp_clean_xsk_umem(struct ifobject *ifobj)
{
struct xsk_umem_info *umem = ifobj->xsk->umem;
- u64 umem_sz = umem_size(umem);
-
- if (ifobj->shared_umem)
- umem_sz *= 2;

- umem_sz = ceil_u64(umem_sz, HUGEPAGE_SIZE) * HUGEPAGE_SIZE;
xsk_umem__delete(umem->umem);
- munmap(umem->buffer, umem_sz);
+ munmap(umem->buffer, umem->mmap_size);
}

static void handler(int signum)
diff --git a/tools/testing/selftests/bpf/prog_tests/test_xsk.h b/tools/testing/selftests/bpf/prog_tests/test_xsk.h
index accda2456fe6..0355a9d06b4e 100644
--- a/tools/testing/selftests/bpf/prog_tests/test_xsk.h
+++ b/tools/testing/selftests/bpf/prog_tests/test_xsk.h
@@ -85,6 +85,7 @@ struct xsk_umem_info {
struct xsk_ring_cons cq;
struct xsk_umem *umem;
u64 next_buffer;
+ u64 mmap_size;
u32 num_frames;
u32 frame_headroom;
void *buffer;
--
2.43.0