selftests/mm: hugepage_settings: add APIs to get and set nr_hugepages

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.

Link: https://lore.kernel.org/20260511162840.375890-27-rppt@kernel.org
Signed-off-by: Mike Rapoport (Microsoft) <rppt@kernel.org>
Reviewed-by: Luiz Capitulino <luizcap@redhat.com>
Tested-by: Luiz Capitulino <luizcap@redhat.com>
Tested-by: Sarthak Sharma <sarthak.sharma@arm.com>
Cc: Baolin Wang <baolin.wang@linux.alibaba.com>
Cc: Barry Song <baohua@kernel.org>
Cc: David Hildenbrand <david@kernel.org>
Cc: Dev Jain <dev.jain@arm.com>
Cc: Donet Tom <donettom@linux.ibm.com>
Cc: Jason Gunthorpe <jgg@ziepe.ca>
Cc: John Hubbard <jhubbard@nvidia.com>
Cc: Lance Yang <lance.yang@linux.dev>
Cc: Leon Romanovsky <leon@kernel.org>
Cc: Liam Howlett <liam@infradead.org>
Cc: Li Wang <li.wang@linux.dev>
Cc: Lorenzo Stoakes <ljs@kernel.org>
Cc: Mark Brown <broonie@kernel.org>
Cc: Michal Hocko <mhocko@suse.com>
Cc: Nico Pache <npache@redhat.com>
Cc: Peter Xu <peterx@redhat.com>
Cc: Ryan Roberts <ryan.roberts@arm.com>
Cc: Shuah Khan <shuah@kernel.org>
Cc: Suren Baghdasaryan <surenb@google.com>
Cc: Vlastimil Babka <vbabka@kernel.org>
Cc: Zi Yan <ziy@nvidia.com>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
This commit is contained in:
Mike Rapoport (Microsoft) 2026-05-11 19:28:10 +03:00 committed by Andrew Morton
parent 0020a1f5ca
commit 27477b28b7
2 changed files with 48 additions and 0 deletions

View File

@ -463,3 +463,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);
}

View File

@ -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 /* __HUGEPAGE_SETTINGS_H__ */