]> git.ipfire.org Git - thirdparty/kernel/linux.git/commitdiff
selftests/mm: hugepage_settings: add APIs to get and set nr_hugepages
authorMike Rapoport (Microsoft) <rppt@kernel.org>
Mon, 11 May 2026 16:28:10 +0000 (19:28 +0300)
committerAndrew Morton <akpm@linux-foundation.org>
Sun, 21 Jun 2026 18:37:24 +0000 (11:37 -0700)
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>
tools/testing/selftests/mm/hugepage_settings.c
tools/testing/selftests/mm/hugepage_settings.h

index fa635667aabb991f24556048b196427a30ef93d7..99402cfccb6d3df7277d044511992bbf9e7b761e 100644 (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);
+}
index f49bd7fba51257481f8b537f7b8fa7a501511e0b..e66302926377762bda6668cdfdcc80ec87a4d636 100644 (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__ */