Re: [PATCH v3 23/54] selftests/mm: move HugeTLB helpers to hugepage_settings

Sarthak Sharma
2026-05-06T09:16:19Z

On 4/29/26 2:12 AM, Mike Rapoport wrote:
> From: "Mike Rapoport (Microsoft)" <rppt@kernel.org>
>
> Move library functions that abstract HugeTLB /proc and /sysfs access
> from vm_util to hugepage_settings.
>
> This will help creating common helpers that save and restore HugeTLB and
> THP settings.
>
> Signed-off-by: Mike Rapoport (Microsoft) <rppt@kernel.org>

Looks good to me.

Reviewed-by: Sarthak Sharma <sarthak.sharma@arm.com>

> ---
> tools/testing/selftests/mm/gup_longterm.c | 1 +
> tools/testing/selftests/mm/hmm-tests.c | 2 +-
> .../testing/selftests/mm/hugepage_settings.c | 68 +++++++++++++++++++
> .../testing/selftests/mm/hugepage_settings.h | 8 +++
> tools/testing/selftests/mm/hugetlb-madvise.c | 1 +
> tools/testing/selftests/mm/hugetlb-mmap.c | 1 +
> tools/testing/selftests/mm/hugetlb-vmemmap.c | 1 +
> tools/testing/selftests/mm/hugetlb_dio.c | 1 +
> .../selftests/mm/hugetlb_fault_after_madv.c | 1 +
> .../selftests/mm/hugetlb_madv_vs_map.c | 1 +
> tools/testing/selftests/mm/thuge-gen.c | 1 +
> tools/testing/selftests/mm/uffd-common.h | 1 +
> .../selftests/mm/va_high_addr_switch.c | 1 +
> tools/testing/selftests/mm/vm_util.c | 66 ------------------
> tools/testing/selftests/mm/vm_util.h | 3 -
> 15 files changed, 87 insertions(+), 70 deletions(-)
>
> diff --git a/tools/testing/selftests/mm/gup_longterm.c b/tools/testing/selftests/mm/gup_longterm.c
> index f61150d28eb2..ab4eaf4feb7c 100644
> --- a/tools/testing/selftests/mm/gup_longterm.c
> +++ b/tools/testing/selftests/mm/gup_longterm.c
> @@ -29,6 +29,7 @@
> #include "../../../../mm/gup_test.h"
> #include "kselftest.h"
> #include "vm_util.h"
> +#include "hugepage_settings.h"
>
> static size_t pagesize;
> static int nr_hugetlbsizes;
> diff --git a/tools/testing/selftests/mm/hmm-tests.c b/tools/testing/selftests/mm/hmm-tests.c
> index 788689497e92..409b11cad4bc 100644
> --- a/tools/testing/selftests/mm/hmm-tests.c
> +++ b/tools/testing/selftests/mm/hmm-tests.c
> @@ -11,6 +11,7 @@
> */
>
> #include "kselftest_harness.h"
> +#include "hugepage_settings.h"
>
> #include <errno.h>
> #include <fcntl.h>
> @@ -27,7 +28,6 @@
> #include <sys/ioctl.h>
> #include <sys/time.h>
>
> -
> /*
> * This is a private UAPI to the kernel test module so it isn't exported
> * in the usual include/uapi/... directory.
> diff --git a/tools/testing/selftests/mm/hugepage_settings.c b/tools/testing/selftests/mm/hugepage_settings.c
> index 87ecb309b430..ebaa621c45c6 100644
> --- a/tools/testing/selftests/mm/hugepage_settings.c
> +++ b/tools/testing/selftests/mm/hugepage_settings.c
> @@ -1,7 +1,9 @@
> // SPDX-License-Identifier: GPL-2.0
> +#include <dirent.h>
> #include <fcntl.h>
> #include <limits.h>
> #include <signal.h>
> +#include <stddef.h>
> #include <stdio.h>
> #include <stdlib.h>
> #include <string.h>
> @@ -396,3 +398,69 @@ bool thp_is_enabled(void)
> /* THP is considered enabled if it's either "always" or "madvise" */
> return mode == 1 || mode == 3;
> }
> +
> +int detect_hugetlb_page_sizes(size_t sizes[], int max)
> +{
> + DIR *dir = opendir("/sys/kernel/mm/hugepages/");
> + int count = 0;
> +
> + if (!dir)
> + return 0;
> +
> + while (count < max) {
> + struct dirent *entry = readdir(dir);
> + size_t kb;
> +
> + if (!entry)
> + break;
> + if (entry->d_type != DT_DIR)
> + continue;
> + if (sscanf(entry->d_name, "hugepages-%zukB", &kb) != 1)
> + continue;
> + sizes[count++] = kb * 1024;
> + ksft_print_msg("[INFO] detected hugetlb page size: %zu KiB\n",
> + kb);
> + }
> + closedir(dir);
> + return count;
> +}
> +
> +unsigned long default_huge_page_size(void)
> +{
> + unsigned long hps = 0;
> + char *line = NULL;
> + size_t linelen = 0;
> + FILE *f = fopen("/proc/meminfo", "r");
> +
> + if (!f)
> + return 0;
> + while (getline(&line, &linelen, f) > 0) {
> + if (sscanf(line, "Hugepagesize: %lu kB", &hps) == 1) {
> + hps <<= 10;
> + break;
> + }
> + }
> +
> + free(line);
> + fclose(f);
> + return hps;
> +}
> +
> +unsigned long get_free_hugepages(void)
> +{
> + unsigned long fhp = 0;
> + char *line = NULL;
> + size_t linelen = 0;
> + FILE *f = fopen("/proc/meminfo", "r");
> +
> + if (!f)
> + return fhp;
> + while (getline(&line, &linelen, f) > 0) {
> + if (sscanf(line, "HugePages_Free: %lu", &fhp) == 1)
> + break;
> + }
> +
> + free(line);
> + fclose(f);
> + return fhp;
> +}
> diff --git a/tools/testing/selftests/mm/hugepage_settings.h b/tools/testing/selftests/mm/hugepage_settings.h
> index 7748a9009191..320b97e768e8 100644
> --- a/tools/testing/selftests/mm/hugepage_settings.h
> +++ b/tools/testing/selftests/mm/hugepage_settings.h
> @@ -6,6 +6,8 @@
> #include <stddef.h>
> #include <stdint.h>
>
> +/* Transparent Huge Pages (THP) */
> +
> enum thp_enabled {
> THP_NEVER,
> THP_ALWAYS,
> @@ -86,4 +88,10 @@ unsigned long thp_shmem_supported_orders(void);
> bool thp_available(void);
> bool thp_is_enabled(void);
>
> +/* HugeTLB */
> +
> +int detect_hugetlb_page_sizes(size_t sizes[], int max);
> +unsigned long default_huge_page_size(void);
> +unsigned long get_free_hugepages(void);
> +
> #endif /* __THP_SETTINGS_H__ */
> diff --git a/tools/testing/selftests/mm/hugetlb-madvise.c b/tools/testing/selftests/mm/hugetlb-madvise.c
> index 4c6c346a3af5..316384d919db 100644
> --- a/tools/testing/selftests/mm/hugetlb-madvise.c
> +++ b/tools/testing/selftests/mm/hugetlb-madvise.c
> @@ -20,6 +20,7 @@
> #include <fcntl.h>
> #include "vm_util.h"
> #include "kselftest.h"
> +#include "hugepage_settings.h"
>
> #define MIN_FREE_PAGES 20
> #define NR_HUGE_PAGES 10 /* common number of pages to map/allocate */
> diff --git a/tools/testing/selftests/mm/hugetlb-mmap.c b/tools/testing/selftests/mm/hugetlb-mmap.c
> index 9f5ea8d6d656..031085cb7d17 100644
> --- a/tools/testing/selftests/mm/hugetlb-mmap.c
> +++ b/tools/testing/selftests/mm/hugetlb-mmap.c
> @@ -18,6 +18,7 @@
> #include <linux/memfd.h>
> #include "vm_util.h"
> #include "kselftest.h"
> +#include "hugepage_settings.h"
>
> #define LENGTH (256UL*1024*1024)
> #define PROTECTION (PROT_READ | PROT_WRITE)
> diff --git a/tools/testing/selftests/mm/hugetlb-vmemmap.c b/tools/testing/selftests/mm/hugetlb-vmemmap.c
> index 485a6978b40f..af5786bebfd1 100644
> --- a/tools/testing/selftests/mm/hugetlb-vmemmap.c
> +++ b/tools/testing/selftests/mm/hugetlb-vmemmap.c
> @@ -11,6 +11,7 @@
> #include <sys/mman.h>
> #include <fcntl.h>
> #include "vm_util.h"
> +#include "hugepage_settings.h"
>
> #define PAGE_COMPOUND_HEAD (1UL << 15)
> #define PAGE_COMPOUND_TAIL (1UL << 16)
> diff --git a/tools/testing/selftests/mm/hugetlb_dio.c b/tools/testing/selftests/mm/hugetlb_dio.c
> index 31a054fa8134..81e3f7bc8e76 100644
> --- a/tools/testing/selftests/mm/hugetlb_dio.c
> +++ b/tools/testing/selftests/mm/hugetlb_dio.c
> @@ -20,6 +20,7 @@
> #include <sys/syscall.h>
> #include "vm_util.h"
> #include "kselftest.h"
> +#include "hugepage_settings.h"
>
> #ifndef STATX_DIOALIGN
> #define STATX_DIOALIGN 0x00002000U
> diff --git a/tools/testing/selftests/mm/hugetlb_fault_after_madv.c b/tools/testing/selftests/mm/hugetlb_fault_after_madv.c
> index b4b257775b74..abc3904c5268 100644
> --- a/tools/testing/selftests/mm/hugetlb_fault_after_madv.c
> +++ b/tools/testing/selftests/mm/hugetlb_fault_after_madv.c
> @@ -10,6 +10,7 @@
>
> #include "vm_util.h"
> #include "kselftest.h"
> +#include "hugepage_settings.h"
>
> #define INLOOP_ITER 100
>
> diff --git a/tools/testing/selftests/mm/hugetlb_madv_vs_map.c b/tools/testing/selftests/mm/hugetlb_madv_vs_map.c
> index c7105c6d319b..ac60b4f18784 100644
> --- a/tools/testing/selftests/mm/hugetlb_madv_vs_map.c
> +++ b/tools/testing/selftests/mm/hugetlb_madv_vs_map.c
> @@ -25,6 +25,7 @@
> #include <unistd.h>
>
> #include "vm_util.h"
> +#include "hugepage_settings.h"
>
> #define INLOOP_ITER 100
>
> diff --git a/tools/testing/selftests/mm/thuge-gen.c b/tools/testing/selftests/mm/thuge-gen.c
> index 77813d34dcc2..1007bc8aa57c 100644
> --- a/tools/testing/selftests/mm/thuge-gen.c
> +++ b/tools/testing/selftests/mm/thuge-gen.c
> @@ -28,6 +28,7 @@
> #include <string.h>
> #include "vm_util.h"
> #include "kselftest.h"
> +#include "hugepage_settings.h"
>
> #if !defined(MAP_HUGETLB)
> #define MAP_HUGETLB 0x40000
> diff --git a/tools/testing/selftests/mm/uffd-common.h b/tools/testing/selftests/mm/uffd-common.h
> index 844a85ab31eb..6c5aca9cb63e 100644
> --- a/tools/testing/selftests/mm/uffd-common.h
> +++ b/tools/testing/selftests/mm/uffd-common.h
> @@ -37,6 +37,7 @@
>
> #include "kselftest.h"
> #include "vm_util.h"
> +#include "hugepage_settings.h"
>
> #define UFFD_FLAGS (O_CLOEXEC | O_NONBLOCK | UFFD_USER_MODE_ONLY)
>
> diff --git a/tools/testing/selftests/mm/va_high_addr_switch.c b/tools/testing/selftests/mm/va_high_addr_switch.c
> index 5d38735ea60e..0b69bd4b901d 100644
> --- a/tools/testing/selftests/mm/va_high_addr_switch.c
> +++ b/tools/testing/selftests/mm/va_high_addr_switch.c
> @@ -11,6 +11,7 @@
>
> #include "vm_util.h"
> #include "kselftest.h"
> +#include "hugepage_settings.h"
>
> /*
> * The hint addr value is used to allocate addresses
> diff --git a/tools/testing/selftests/mm/vm_util.c b/tools/testing/selftests/mm/vm_util.c
> index db94564f4431..e62e2a473123 100644
> --- a/tools/testing/selftests/mm/vm_util.c
> +++ b/tools/testing/selftests/mm/vm_util.c
> @@ -291,53 +291,6 @@ int64_t allocate_transhuge(void *ptr, int pagemap_fd)
> return -1;
> }
>
> -unsigned long default_huge_page_size(void)
> -{
> - unsigned long hps = 0;
> - char *line = NULL;
> - size_t linelen = 0;
> - FILE *f = fopen("/proc/meminfo", "r");
> -
> - if (!f)
> - return 0;
> - while (getline(&line, &linelen, f) > 0) {
> - if (sscanf(line, "Hugepagesize: %lu kB", &hps) == 1) {
> - hps <<= 10;
> - break;
> - }
> - }
> -
> - free(line);
> - fclose(f);
> - return hps;
> -}
> -
> -int detect_hugetlb_page_sizes(size_t sizes[], int max)
> -{
> - DIR *dir = opendir("/sys/kernel/mm/hugepages/");
> - int count = 0;
> -
> - if (!dir)
> - return 0;
> -
> - while (count < max) {
> - struct dirent *entry = readdir(dir);
> - size_t kb;
> -
> - if (!entry)
> - break;
> - if (entry->d_type != DT_DIR)
> - continue;
> - if (sscanf(entry->d_name, "hugepages-%zukB", &kb) != 1)
> - continue;
> - sizes[count++] = kb * 1024;
> - ksft_print_msg("[INFO] detected hugetlb page size: %zu KiB\n",
> - kb);
> - }
> - closedir(dir);
> - return count;
> -}
> -
> int pageflags_get(unsigned long pfn, int kpageflags_fd, uint64_t *flags)
> {
> size_t count;
> @@ -396,25 +349,6 @@ int uffd_unregister(int uffd, void *addr, uint64_t len)
> return ret;
> }
>
> -unsigned long get_free_hugepages(void)
> -{
> - unsigned long fhp = 0;
> - char *line = NULL;
> - size_t linelen = 0;
> - FILE *f = fopen("/proc/meminfo", "r");
> -
> - if (!f)
> - return fhp;
> - while (getline(&line, &linelen, f) > 0) {
> - if (sscanf(line, "HugePages_Free: %lu", &fhp) == 1)
> - break;
> - }
> -
> - free(line);
> - fclose(f);
> - return fhp;
> -}
> -
> static bool check_vmflag(void *addr, const char *flag)
> {
> char buffer[MAX_LINE_LENGTH];
> diff --git a/tools/testing/selftests/mm/vm_util.h b/tools/testing/selftests/mm/vm_util.h
> index 1a07305ceff4..195bf2e26792 100644
> --- a/tools/testing/selftests/mm/vm_util.h
> +++ b/tools/testing/selftests/mm/vm_util.h
> @@ -94,8 +94,6 @@ bool check_huge_anon(void *addr, int nr_hpages, uint64_t hpage_size);
> bool check_huge_file(void *addr, int nr_hpages, uint64_t hpage_size);
> bool check_huge_shmem(void *addr, int nr_hpages, uint64_t hpage_size);
> int64_t allocate_transhuge(void *ptr, int pagemap_fd);
> -unsigned long default_huge_page_size(void);
> -int detect_hugetlb_page_sizes(size_t sizes[], int max);
> int pageflags_get(unsigned long pfn, int kpageflags_fd, uint64_t *flags);
>
> int uffd_register(int uffd, void *addr, uint64_t len,
> @@ -103,7 +101,6 @@ int uffd_register(int uffd, void *addr, uint64_t len,
> int uffd_unregister(int uffd, void *addr, uint64_t len);
> int uffd_register_with_ioctls(int uffd, void *addr, uint64_t len,
> bool miss, bool wp, bool minor, uint64_t *ioctls);
> -unsigned long get_free_hugepages(void);
> bool check_vmflag_io(void *addr);
> bool check_vmflag_pfnmap(void *addr);
> bool check_vmflag_guard(void *addr);