[PATCH v3 39/54] selftests/mm: hugetlb-mmap: add setup of HugeTLB pages

Mike Rapoport
2026-04-28T20:47:08Z

hugetlb-mmap test fails if there are no free huge pages prepared by a
wrapper script.

Add setup of HugeTLB pages to the test and make sure that the original
settings are restored on the test exit.

Signed-off-by: Mike Rapoport (Microsoft) <rppt@kernel.org>
---
tools/testing/selftests/mm/hugetlb-mmap.c | 13 +++++++++----
1 file changed, 9 insertions(+), 4 deletions(-)

diff --git a/tools/testing/selftests/mm/hugetlb-mmap.c b/tools/testing/selftests/mm/hugetlb-mmap.c
index 031085cb7d17..f12fc288ef4f 100644
--- a/tools/testing/selftests/mm/hugetlb-mmap.c
+++ b/tools/testing/selftests/mm/hugetlb-mmap.c
@@ -109,16 +109,16 @@ int main(int argc, char **argv)
{
size_t hugepage_size;
size_t length = LENGTH;
- int shift = 0;
+ int shift = 0, nr;

ksft_print_header();
- ksft_set_plan(2);

if (argc > 1)
length = atol(argv[1]) << 20;
if (argc > 2)
shift = atoi(argv[2]);

+ hugetlb_save_settings();
if (shift) {
hugepage_size = (1 << shift);
ksft_print_msg("%u kB hugepages\n", 1 << (shift - 10));
@@ -128,9 +128,14 @@ int main(int argc, char **argv)
}

/* munmap with fail if the length is not page aligned */
- if (hugepage_size > length)
- length = hugepage_size;
+ length = (length + hugepage_size - 1) & ~(hugepage_size - 1);
+ nr = length / hugepage_size;
+
+ hugetlb_set_nr_pages(hugepage_size, nr);
+ if (hugetlb_free_pages(hugepage_size) < nr)
+ ksft_exit_skip("Not enough %lu Kb pages\n", hugepage_size >> 10);

+ ksft_set_plan(2);
ksft_print_msg("Mapping %lu Mbytes\n", (unsigned long)length >> 20);

test_anon_mmap(length, shift);
--
2.53.0