Re: [PATCH v3 06/54] selftests/mm: merge map_hugetlb into hugepage-mmap

Luiz Capitulino
2026-04-30T15:26:37Z

On 2026-04-28 16:41, Mike Rapoport wrote:
> From: "Mike Rapoport (Microsoft)" <rppt@kernel.org>
>
> Both tests create a hugettlb mapping, fill it with data and verify the
> data, the only difference is that one uses file-backed memory and another
> one uses anonymous memory.
>
> Merge both tests into a single file.
>
> Signed-off-by: Mike Rapoport (Microsoft) <rppt@kernel.org>

Reviewed-by: Luiz Capitulino <luizcap@redhat.com>

> ---
> tools/testing/selftests/mm/Makefile | 1 -
> tools/testing/selftests/mm/hugepage-mmap.c | 111 ++++++++++++++++-----
> tools/testing/selftests/mm/map_hugetlb.c | 88 ----------------
> tools/testing/selftests/mm/run_vmtests.sh | 1 -
> 4 files changed, 86 insertions(+), 115 deletions(-)
> delete mode 100644 tools/testing/selftests/mm/map_hugetlb.c
>
> diff --git a/tools/testing/selftests/mm/Makefile b/tools/testing/selftests/mm/Makefile
> index cd24596cdd27..cbda989f6b6a 100644
> --- a/tools/testing/selftests/mm/Makefile
> +++ b/tools/testing/selftests/mm/Makefile
> @@ -70,7 +70,6 @@ TEST_GEN_FILES += hugepage-vmemmap
> TEST_GEN_FILES += khugepaged
> TEST_GEN_FILES += madv_populate
> TEST_GEN_FILES += map_fixed_noreplace
> -TEST_GEN_FILES += map_hugetlb
> TEST_GEN_FILES += map_populate
> ifneq (,$(filter $(ARCH),arm64 riscv riscv64 x86 x86_64 loongarch32 loongarch64))
> TEST_GEN_FILES += memfd_secret
> diff --git a/tools/testing/selftests/mm/hugepage-mmap.c b/tools/testing/selftests/mm/hugepage-mmap.c
> index d543419de040..fa886ec148c2 100644
> --- a/tools/testing/selftests/mm/hugepage-mmap.c
> +++ b/tools/testing/selftests/mm/hugepage-mmap.c
> @@ -15,6 +15,8 @@
> #include <unistd.h>
> #include <sys/mman.h>
> #include <fcntl.h>
> +#include <linux/memfd.h>
> +#include "vm_util.h"
> #include "kselftest.h"
>
> #define LENGTH (256UL*1024*1024)
> @@ -25,54 +27,113 @@ static void check_bytes(char *addr)
> ksft_print_msg("First hex is %x\n", *((unsigned int *)addr));
> }
>
> -static void write_bytes(char *addr)
> +static void write_bytes(char *addr, size_t length)
> {
> unsigned long i;
>
> - for (i = 0; i < LENGTH; i++)
> + for (i = 0; i < length; i++)
> *(addr + i) = (char)i;
> }
>
> -static int read_bytes(char *addr)
> +static bool verify_bytes(char *addr, size_t length)
> {
> unsigned long i;
>
> check_bytes(addr);
> - for (i = 0; i < LENGTH; i++)
> + for (i = 0; i < length; i++)
> if (*(addr + i) != (char)i) {
> - ksft_print_msg("Error: Mismatch at %lu\n", i);
> - return 1;
> + ksft_print_msg("Error: Mismatch at %lu(%p)\n", i, addr);
> + return false;
> }
> - return 0;
> +
> + return true;
> }
>
> -int main(void)
> +static bool test_mmap(size_t length, int mmap_flags, int fd,
> + const char *test_name)
> {
> void *addr;
> - int fd, ret;
>
> - ksft_print_header();
> - ksft_set_plan(1);
> + addr = mmap(NULL, length, PROTECTION, mmap_flags, fd, 0);
> + if (addr == MAP_FAILED)
> + ksft_exit_fail_perror("mmap");
> +
> + ksft_print_msg("Returned address is %p\n", addr);
> + check_bytes(addr);
> + write_bytes(addr, length);
> + if (!verify_bytes(addr, length))
> + ksft_exit_fail_msg("%s\n", test_name);
> +
> + /* munmap() length of MAP_HUGETLB memory must be hugepage aligned */
> + if (munmap(addr, length))
> + ksft_exit_fail_perror("munmap");
> +
> + return true;
> +}
> +
> +static void test_anon_mmap(size_t length, int shift)
> +{
> + const char *test_name = "hugetlb anonymous mmap";
> + int mmap_flags = MAP_PRIVATE | MAP_ANONYMOUS | MAP_HUGETLB;
> + bool passed;
> +
> + if (shift)
> + mmap_flags |= (shift & MAP_HUGE_MASK) << MAP_HUGE_SHIFT;
> +
> + passed = test_mmap(length, mmap_flags, -1, test_name);
> + ksft_test_result(passed, "%s\n", test_name);
> +}
> +
> +static void test_file_mmap(size_t length, int shift)
> +{
> + const char *test_name = "hugetlb file mmap";
> + int mfd_flags = MFD_HUGETLB;
> + bool passed;
> + int fd;
> +
> + if (shift)
> + mfd_flags |= (shift & MFD_HUGE_MASK) << MFD_HUGE_SHIFT;
>
> - fd = memfd_create("hugepage-mmap", MFD_HUGETLB);
> + fd = memfd_create("hugetlb-mmap", mfd_flags);
> if (fd < 0)
> - ksft_exit_fail_msg("memfd_create() failed: %s\n", strerror(errno));
> + ksft_exit_fail_perror("memfd_create");
> +
> + passed = test_mmap(length, MAP_SHARED, fd, test_name);
> +
> + close(fd);
> + ksft_test_result(passed, "%s\n", test_name);
> +}
> +
> +int main(int argc, char **argv)
> +{
> + size_t hugepage_size;
> + size_t length = LENGTH;
> + int shift = 0;
> +
> + ksft_print_header();
> + ksft_set_plan(2);
> +
> + if (argc > 1)
> + length = atol(argv[1]) << 20;
> + if (argc > 2)
> + shift = atoi(argv[2]);
>
> - addr = mmap(NULL, LENGTH, PROTECTION, MAP_SHARED, fd, 0);
> - if (addr == MAP_FAILED) {
> - close(fd);
> - ksft_exit_fail_msg("mmap(): %s\n", strerror(errno));
> + if (shift) {
> + hugepage_size = (1 << shift);
> + ksft_print_msg("%u kB hugepages\n", 1 << (shift - 10));
> + } else {
> + hugepage_size = default_huge_page_size();
> + ksft_print_msg("Default size hugepages (%lu Kb)\n", hugepage_size >> 10);
> }
>
> - ksft_print_msg("Returned address is %p\n", addr);
> - check_bytes(addr);
> - write_bytes(addr);
> - ret = read_bytes(addr);
> + /* munmap with fail if the length is not page aligned */
> + if (hugepage_size > length)
> + length = hugepage_size;
>
> - munmap(addr, LENGTH);
> - close(fd);
> + ksft_print_msg("Mapping %lu Mbytes\n", (unsigned long)length >> 20);
>
> - ksft_test_result(!ret, "Read same data\n");
> + test_anon_mmap(length, shift);
> + test_file_mmap(length, shift);
>
> - ksft_exit(!ret);
> + ksft_finished();
> }
> diff --git a/tools/testing/selftests/mm/map_hugetlb.c b/tools/testing/selftests/mm/map_hugetlb.c
> deleted file mode 100644
> index aa409107611b..000000000000
> --- a/tools/testing/selftests/mm/map_hugetlb.c
> +++ /dev/null
> @@ -1,88 +0,0 @@
> -// SPDX-License-Identifier: GPL-2.0
> -/*
> - * Example of using hugepage memory in a user application using the mmap
> - * system call with MAP_HUGETLB flag. Before running this program make
> - * sure the administrator has allocated enough default sized huge pages
> - * to cover the 256 MB allocation.
> - */
> -#include <stdlib.h>
> -#include <stdio.h>
> -#include <unistd.h>
> -#include <sys/mman.h>
> -#include <fcntl.h>
> -#include "vm_util.h"
> -#include "kselftest.h"
> -
> -#define LENGTH (256UL*1024*1024)
> -#define PROTECTION (PROT_READ | PROT_WRITE)
> -
> -static void check_bytes(char *addr)
> -{
> - ksft_print_msg("First hex is %x\n", *((unsigned int *)addr));
> -}
> -
> -static void write_bytes(char *addr, size_t length)
> -{
> - unsigned long i;
> -
> - for (i = 0; i < length; i++)
> - *(addr + i) = (char)i;
> -}
> -
> -static void read_bytes(char *addr, size_t length)
> -{
> - unsigned long i;
> -
> - check_bytes(addr);
> - for (i = 0; i < length; i++)
> - if (*(addr + i) != (char)i)
> - ksft_exit_fail_msg("Mismatch at %lu\n", i);
> -
> - ksft_test_result_pass("Read correct data\n");
> -}
> -
> -int main(int argc, char **argv)
> -{
> - void *addr;
> - size_t hugepage_size;
> - size_t length = LENGTH;
> - int flags = MAP_PRIVATE | MAP_ANONYMOUS | MAP_HUGETLB;
> - int shift = 0;
> -
> - hugepage_size = default_huge_page_size();
> - /* munmap with fail if the length is not page aligned */
> - if (hugepage_size > length)
> - length = hugepage_size;
> -
> - ksft_print_header();
> - ksft_set_plan(1);
> -
> - if (argc > 1)
> - length = atol(argv[1]) << 20;
> - if (argc > 2) {
> - shift = atoi(argv[2]);
> - if (shift)
> - flags |= (shift & MAP_HUGE_MASK) << MAP_HUGE_SHIFT;
> - }
> -
> - if (shift)
> - ksft_print_msg("%u kB hugepages\n", 1 << (shift - 10));
> - else
> - ksft_print_msg("Default size hugepages\n");
> - ksft_print_msg("Mapping %lu Mbytes\n", (unsigned long)length >> 20);
> -
> - addr = mmap(NULL, length, PROTECTION, flags, -1, 0);
> - if (addr == MAP_FAILED)
> - ksft_exit_fail_msg("mmap: %s\n", strerror(errno));
> -
> - ksft_print_msg("Returned address is %p\n", addr);
> - check_bytes(addr);
> - write_bytes(addr, length);
> - read_bytes(addr, length);
> -
> - /* munmap() length of MAP_HUGETLB memory must be hugepage aligned */
> - if (munmap(addr, length))
> - ksft_exit_fail_msg("munmap: %s\n", strerror(errno));
> -
> - ksft_finished();
> -}
> diff --git a/tools/testing/selftests/mm/run_vmtests.sh b/tools/testing/selftests/mm/run_vmtests.sh
> index e2dc9ac87bfc..61b450032af8 100755
> --- a/tools/testing/selftests/mm/run_vmtests.sh
> +++ b/tools/testing/selftests/mm/run_vmtests.sh
> @@ -292,7 +292,6 @@ CATEGORY="hugetlb" run_test ./hugepage-shm
> echo "$shmmax" > /proc/sys/kernel/shmmax
> echo "$shmall" > /proc/sys/kernel/shmall
>
> -CATEGORY="hugetlb" run_test ./map_hugetlb
> CATEGORY="hugetlb" run_test ./hugepage-mremap
> CATEGORY="hugetlb" run_test ./hugepage-vmemmap
> CATEGORY="hugetlb" run_test ./hugetlb-madvise