Re: [PATCH v3 25/54] selftests/mm: hugepage_settings: add APIs to get and set nr_hugepages

Luiz Capitulino
2026-04-30T17:33:42Z

On 2026-04-28 16:42, Mike Rapoport wrote:
> From: "Mike Rapoport (Microsoft)" <rppt@kernel.org>
>
> Add APIs that allow reading and writing of
>
> /sys/kernel/mm/hugepages/hugepages-NkB/nr_hugepages
>
> to detect and change the amount of HugeTLB pages of different sizes.
>
> Signed-off-by: Mike Rapoport (Microsoft) <rppt@kernel.org>

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

> ---
> .../testing/selftests/mm/hugepage_settings.c | 25 +++++++++++++++++++
> .../testing/selftests/mm/hugepage_settings.h | 23 +++++++++++++++++
> 2 files changed, 48 insertions(+)
>
> diff --git a/tools/testing/selftests/mm/hugepage_settings.c b/tools/testing/selftests/mm/hugepage_settings.c
> index 8f563315b1fc..5961229b9931 100644
> --- a/tools/testing/selftests/mm/hugepage_settings.c
> +++ b/tools/testing/selftests/mm/hugepage_settings.c
> @@ -464,3 +464,28 @@ unsigned long get_free_hugepages(void)
> fclose(f);
> return fhp;
> }
> +
> +static void hugetlb_sysfs_path(char *buf, size_t buflen,
> + unsigned long size, const char *attr)
> +{
> + snprintf(buf, buflen, "/sys/kernel/mm/hugepages/hugepages-%lukB/%s",
> + size / 1024, attr);
> +}
> +
> +unsigned long hugetlb_nr_pages(unsigned long size)
> +{
> + char path[PATH_MAX];
> +
> + hugetlb_sysfs_path(path, sizeof(path), size, "nr_hugepages");
> +
> + return read_num(path);
> +}
> +
> +void hugetlb_set_nr_pages(unsigned long size, unsigned long nr)
> +{
> + char path[PATH_MAX];
> +
> + hugetlb_sysfs_path(path, sizeof(path), size, "nr_hugepages");
> +
> + write_num(path, nr);
> +}
> diff --git a/tools/testing/selftests/mm/hugepage_settings.h b/tools/testing/selftests/mm/hugepage_settings.h
> index 9357d07e6c1b..d6f41a45e8ee 100644
> --- a/tools/testing/selftests/mm/hugepage_settings.h
> +++ b/tools/testing/selftests/mm/hugepage_settings.h
> @@ -94,4 +94,27 @@ int detect_hugetlb_page_sizes(unsigned long sizes[], int max);
> unsigned long default_huge_page_size(void);
> unsigned long get_free_hugepages(void);
>
> +unsigned long hugetlb_nr_pages(unsigned long size);
> +void hugetlb_set_nr_pages(unsigned long size, unsigned long nr);
> +
> +static inline unsigned long hugetlb_nr_default_pages(void)
> +{
> + unsigned long size = default_huge_page_size();
> +
> + if (!size)
> + return 0;
> +
> + return hugetlb_nr_pages(size);
> +}
> +
> +static inline void hugetlb_set_nr_default_pages(unsigned long nr)
> +{
> + unsigned long size = default_huge_page_size();
> +
> + if (!size)
> + return;
> +
> + hugetlb_set_nr_pages(size, nr);
> +}
> +
> #endif /* __THP_SETTINGS_H__ */