From: Luiz Capitulino <luizcap@redhat.com>
To: Mike Rapoport <rppt@kernel.org>,
Andrew Morton <akpm@linux-foundation.org>,
David Hildenbrand <david@kernel.org>
Cc: Baolin Wang <baolin.wang@linux.alibaba.com>,
Barry Song <baohua@kernel.org>, Dev Jain <dev.jain@arm.com>,
Donet Tom <donettom@linux.ibm.com>,
Jason Gunthorpe <jgg@ziepe.ca>,
John Hubbard <jhubbard@nvidia.com>,
"Liam R. Howlett" <Liam.Howlett@oracle.com>,
Lance Yang <lance.yang@linux.dev>, Li Wang <li.wang@linux.dev>,
Leon Romanovsky <leon@kernel.org>,
Lorenzo Stoakes <ljs@kernel.org>, Mark Brown <broonie@kernel.org>,
Michal Hocko <mhocko@suse.com>, Nico Pache <npache@redhat.com>,
Peter Xu <peterx@redhat.com>, Ryan Roberts <ryan.roberts@arm.com>,
Sarthak Sharma <sarthak.sharma@arm.com>,
Shuah Khan <shuah@kernel.org>,
Suren Baghdasaryan <surenb@google.com>,
Vlastimil Babka <vbabka@kernel.org>, Zi Yan <ziy@nvidia.com>,
linux-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org,
linux-mm@kvack.org
Subject: Re: [PATCH v4 28/55] selftests/mm: hugepage_settings: add APIs for HugeTLB setup and teardown
Date: Wed, 13 May 2026 15:50:39 -0400 [thread overview]
Message-ID: <635a6440-9339-49fb-ad9b-ee2f4b285ef6@redhat.com> (raw)
In-Reply-To: <20260511162840.375890-29-rppt@kernel.org>
On 2026-05-11 12:28, Mike Rapoport wrote:
> From: "Mike Rapoport (Microsoft)" <rppt@kernel.org>
>
> A lot of tests require free HugeTLB pages. Some need just a few default
> huge pages, some need a certain amount of memory available as HugeTLB, and
> some just skip lots of tests if huge pages of all supported sizes are not
> available.
>
> This all resulted in a huge mess in run_vmtests.sh that sets up some huge
> pages, adjusts them later and restores some of the settings if the stars
> align.
>
> Add APIs that allow saving the state of HugeTLB and setting up the desired
> amount of HugeTLB pages.
>
> Saving the state also registers atexit() callback and signal handler that
> will ensure restoration of HugeTLB state.
>
> Since many tests use both HugeTLB and THP, the atexit() callbacks and
> signal handler are restoring both.
>
> For kselftest_harness tests that run fixture setups and test in child
> processes add a constructor that will save and restore settings in the main
> process.
>
> Tested-by: Luiz Capitulino <luizcap@redhat.com>
> Tested-by: Sarthak Sharma <sarthak.sharma@arm.com>
> Signed-off-by: Mike Rapoport (Microsoft) <rppt@kernel.org>
> ---
> .../testing/selftests/mm/hugepage_settings.c | 205 ++++++++++++++++--
> .../testing/selftests/mm/hugepage_settings.h | 31 ++-
> 2 files changed, 213 insertions(+), 23 deletions(-)
>
> diff --git a/tools/testing/selftests/mm/hugepage_settings.c b/tools/testing/selftests/mm/hugepage_settings.c
> index 3c944d28d14a..01a557f372d1 100644
> --- a/tools/testing/selftests/mm/hugepage_settings.c
> +++ b/tools/testing/selftests/mm/hugepage_settings.c
> @@ -306,31 +306,16 @@ void thp_restore_settings(void)
> thp_write_settings(&saved_settings);
> }
>
> -static void thp_restore_settings_atexit(void)
> +static void __thp_save_settings(void)
> {
> - thp_restore_settings();
> -}
> + if (!thp_available())
> + return;
>
> -static void thp_restore_settings_sighandler(int sig)
> -{
> - /* exit() will invoke the thp_restore_settings_atexit handler. */
> - exit(KSFT_FAIL);
> -}
> + if (thp_settings_saved)
> + return;
>
> -void thp_save_settings(void)
> -{
> thp_read_settings(&saved_settings);
> thp_settings_saved = true;
> -
> - /*
> - * setup exit hooks to make sure THP settings are restored on graceful
> - * and error exits and signals
> - */
> - atexit(thp_restore_settings_atexit);
> - signal(SIGTERM, thp_restore_settings_sighandler);
> - signal(SIGINT, thp_restore_settings_sighandler);
> - signal(SIGHUP, thp_restore_settings_sighandler);
> - signal(SIGQUIT, thp_restore_settings_sighandler);
> }
>
> void thp_set_read_ahead_path(char *path)
> @@ -398,11 +383,32 @@ bool thp_is_enabled(void)
> return mode == 1 || mode == 3;
> }
>
> +#define HUGETLB_MAX_NR_PAGESIZES 10
> +struct hugetlb_settings {
> + unsigned long nr_hugepages[HUGETLB_MAX_NR_PAGESIZES];
> + unsigned long sizes[HUGETLB_MAX_NR_PAGESIZES];
> + unsigned long default_size;
> + int nr_sizes;
> +};
> +
> +static struct hugetlb_settings hugetlb_saved_settings;
> +static bool hugetlb_settings_saved;
> +
> int detect_hugetlb_page_sizes(unsigned long sizes[], int max)
> {
> - DIR *dir = opendir("/sys/kernel/mm/hugepages/");
> + static struct hugetlb_settings *settings = &hugetlb_saved_settings;
> + DIR *dir;
> int count = 0;
>
> + if (settings->nr_sizes) {
> + if (settings->nr_sizes < max)
> + max = settings->nr_sizes;
> + for (count = 0; count < max; count++)
> + sizes[count] = settings->sizes[count];
> + return count;
> + }
> +
> + dir = opendir("/sys/kernel/mm/hugepages/");
> if (!dir)
> return 0;
>
> @@ -426,11 +432,16 @@ int detect_hugetlb_page_sizes(unsigned long sizes[], int max)
>
> unsigned long default_huge_page_size(void)
> {
> + static struct hugetlb_settings *settings = &hugetlb_saved_settings;
> unsigned long hps = 0;
> char *line = NULL;
> size_t linelen = 0;
> - FILE *f = fopen("/proc/meminfo", "r");
> + FILE *f;
> +
> + if (settings->default_size)
> + return settings->default_size;
>
> + f = fopen("/proc/meminfo", "r");
> if (!f)
> return 0;
> while (getline(&line, &linelen, f) > 0) {
> @@ -478,3 +489,153 @@ unsigned long hugetlb_free_pages(unsigned long size)
>
> return read_num(path);
> }
> +
> +static bool __hugetlb_setup(unsigned long size, unsigned long nr)
> +{
> + unsigned long free = hugetlb_free_pages(size);
> + unsigned long total = hugetlb_nr_pages(size);
> +
> + if (free >= nr)
> + return true;
> +
> + hugetlb_set_nr_pages(size, total + (nr - free));
We're using 'total' to preserve the HugeTLB reservation that's already
there, right? I guess this won't have the desirable effect because the
tests calling hugetlb_setup_default_exact() will override this anyways.
Having said that, this is just a minor nit and not a bug, so:
Reviewed-by: Luiz Capitulino <luizcap@redhat.com>
> +
> + return hugetlb_free_pages(size) >= nr;
> +}
> +
> +bool hugetlb_setup_default(unsigned long nr)
> +{
> + unsigned long size;
> +
> + hugetlb_save_settings();
> + size = default_huge_page_size();
> + if (!size)
> + return false;
> +
> + return __hugetlb_setup(size, nr);
> +}
> +
> +bool hugetlb_setup_default_exact(unsigned long nr)
> +{
> + unsigned long size;
> +
> + hugetlb_save_settings();
> + size = default_huge_page_size();
> + if (!size)
> + return false;
> +
> + hugetlb_set_nr_pages(size, nr);
> +
> + return hugetlb_free_pages(size) == nr;
> +}
> +
> +unsigned long hugetlb_setup(unsigned long nr, unsigned long sizes[],
> + int max)
> +{
> + unsigned long enabled[10];
> + int nr_sizes = 0;
> + int nr_enabled;
> +
> + hugetlb_save_settings();
> +
> + nr_enabled = detect_hugetlb_page_sizes(enabled, ARRAY_SIZE(enabled));
> + if (!nr_enabled)
> + return 0;
> +
> + if (nr_enabled > max) {
> + ksft_print_msg("detected %d huge page sizes, will only test %d\n", nr_enabled, max);
> + nr_enabled = max;
> + }
> +
> + /* request nr HugeTLB pages of every size. */
> + for (int i = 0; i < nr_enabled; i++) {
> + if (!__hugetlb_setup(enabled[i], nr))
> + continue;
> + sizes[nr_sizes++] = enabled[i];
> + }
> +
> + return nr_sizes;
> +}
> +
> +static void __hugetlb_save_settings(void)
> +{
> + struct hugetlb_settings *settings = &hugetlb_saved_settings;
> + int nr_sizes;
> +
> + if (hugetlb_settings_saved)
> + return;
> +
> + settings->default_size = default_huge_page_size();
> + if (!settings->default_size)
> + return;
> +
> + nr_sizes = detect_hugetlb_page_sizes(settings->sizes,
> + HUGETLB_MAX_NR_PAGESIZES);
> + if (!nr_sizes) {
> + settings->default_size = 0;
> + return;
> + }
> +
> + for (int i = 0; i < nr_sizes; i++) {
> + unsigned long sz = settings->sizes[i];
> +
> + if (!sz)
> + continue;
> + settings->nr_hugepages[i] = hugetlb_nr_pages(sz);
> + }
> +
> + settings->nr_sizes = nr_sizes;
> + hugetlb_settings_saved = true;
> +}
> +
> +void hugetlb_restore_settings(void)
> +{
> + struct hugetlb_settings *settings = &hugetlb_saved_settings;
> +
> + if (!hugetlb_settings_saved || !settings->default_size)
> + return;
> +
> + for (int i = 0; i < HUGETLB_MAX_NR_PAGESIZES; i++) {
> + unsigned long sz = settings->sizes[i];
> +
> + if (!sz)
> + continue;
> +
> + hugetlb_set_nr_pages(sz, settings->nr_hugepages[i]);
> + }
> +}
> +
> +static void hugepage_restore_settings_atexit(void)
> +{
> + if (thp_settings_saved)
> + thp_restore_settings();
> + if (hugetlb_settings_saved)
> + hugetlb_restore_settings();
> +}
> +
> +static void hugepage_restore_settings_sighandler(int sig)
> +{
> + /* exit() will invoke the hugepage_restore_settings_atexit handler. */
> + exit(KSFT_FAIL);
> +}
> +
> +void hugepage_save_settings(bool thp, bool hugetlb)
> +{
> + if (!thp && !hugetlb)
> + return;
> +
> + if (thp)
> + __thp_save_settings();
> + if (hugetlb)
> + __hugetlb_save_settings();
> +
> + /*
> + * setup exit hooks to make sure THP and HugeTLB settings are
> + * restored on graceful and error exits and signals
> + */
> + atexit(hugepage_restore_settings_atexit);
> + signal(SIGTERM, hugepage_restore_settings_sighandler);
> + signal(SIGINT, hugepage_restore_settings_sighandler);
> + signal(SIGHUP, hugepage_restore_settings_sighandler);
> + signal(SIGQUIT, hugepage_restore_settings_sighandler);
> +}
> diff --git a/tools/testing/selftests/mm/hugepage_settings.h b/tools/testing/selftests/mm/hugepage_settings.h
> index 436f4ce02984..c07722b7f102 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>
>
> +void hugepage_save_settings(bool thp, bool hugetlb);
> +
> /* Transparent Huge Pages (THP) */
>
> enum thp_enabled {
> @@ -79,7 +81,11 @@ struct thp_settings *thp_current_settings(void);
> void thp_push_settings(struct thp_settings *settings);
> void thp_pop_settings(void);
> void thp_restore_settings(void);
> -void thp_save_settings(void);
> +
> +static inline void thp_save_settings(void)
> +{
> + hugepage_save_settings(/* thp = */ true, /* hugetlb = */ false);
> +}
>
> void thp_set_read_ahead_path(char *path);
> unsigned long thp_supported_orders(void);
> @@ -97,6 +103,13 @@ unsigned long hugetlb_nr_pages(unsigned long size);
> void hugetlb_set_nr_pages(unsigned long size, unsigned long nr);
> unsigned long hugetlb_free_pages(unsigned long size);
>
> +static inline void hugetlb_save_settings(void)
> +{
> + hugepage_save_settings(/* thp = */ false, /* hugetlb = */ true);
> +}
> +
> +void hugetlb_restore_settings(void);
> +
> static inline unsigned long hugetlb_nr_default_pages(void)
> {
> unsigned long size = default_huge_page_size();
> @@ -127,4 +140,20 @@ static inline unsigned long hugetlb_free_default_pages(void)
> return hugetlb_free_pages(size);
> }
>
> +static inline bool hugetlb_available(void)
> +{
> + return default_huge_page_size() != 0;
> +}
> +
> +bool hugetlb_setup_default(unsigned long nr);
> +bool hugetlb_setup_default_exact(unsigned long nr);
> +unsigned long hugetlb_setup(unsigned long nr, unsigned long sizes[],
> + int max);
> +
> +#define HUGETLB_SETUP_DEFAULT_PAGES(nr_pages) \
> +static void __attribute__((constructor)) __hugetlb_setup_default(void) \
> +{ \
> + hugetlb_setup_default((nr_pages)); \
> +}
> +
> #endif /* __HUGEPAGE_SETTINGS_H__ */
next prev parent reply other threads:[~2026-05-13 19:50 UTC|newest]
Thread overview: 61+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-05-11 16:27 [PATCH v4 00/55] make MM selftests more CI friendly Mike Rapoport
2026-05-11 16:27 ` [PATCH v4 01/55] selftests/mm: hugetlb-read-hwpoison: add SIGBUS handler Mike Rapoport
2026-05-11 16:27 ` [PATCH v4 02/55] selftests/mm: migration: don't assume huge page is TWOMEG Mike Rapoport
2026-05-11 16:27 ` [PATCH v4 03/55] selftests/mm: migration: make nthreads represent number of working threads Mike Rapoport
2026-05-11 16:27 ` [PATCH v4 04/55] selftests/mm: migration: properly cleanup fork()ed processes Mike Rapoport
2026-05-13 14:18 ` Luiz Capitulino
2026-05-11 16:27 ` [PATCH v4 05/55] selftests/mm: run_vmtests.sh: don't gate THP and KSM tests on HAVE_HUGEPAGES Mike Rapoport
2026-05-11 16:27 ` [PATCH v4 06/55] selftests/mm: merge map_hugetlb into hugepage-mmap Mike Rapoport
2026-05-11 16:27 ` [PATCH v4 07/55] selftests/mm: rename hugepage-* tests to hugetlb-* Mike Rapoport
2026-05-11 16:27 ` [PATCH v4 08/55] selftests/mm: hugetlb-shm: use kselftest framework Mike Rapoport
2026-05-11 16:27 ` [PATCH v4 09/55] selftests/mm: hugetlb-vmemmap: " Mike Rapoport
2026-05-11 16:27 ` [PATCH v4 10/55] selftests/mm: hugetlb-madvise: " Mike Rapoport
2026-05-11 16:27 ` [PATCH v4 11/55] selftests/mm: hugetlb_madv_vs_map: " Mike Rapoport
2026-05-11 16:27 ` [PATCH v4 12/55] selftests/mm: hugetlb-read-hwpoison: " Mike Rapoport
2026-05-11 16:27 ` [PATCH v4 13/55] selftests/mm: khugepaged: group tests in an array Mike Rapoport
2026-05-11 16:27 ` [PATCH v4 14/55] selftests/mm: khugepaged: use ksefltest framework Mike Rapoport
2026-05-11 16:27 ` [PATCH v4 15/55] selftests/mm: ksm_tests: use kselftest framework Mike Rapoport
2026-05-11 16:28 ` [PATCH v4 16/55] selftests/mm: protection_keys: use descriptive test names in the output Mike Rapoport
2026-05-11 16:28 ` [PATCH v4 17/55] selftests/mm: protection_keys: use kselftest framework Mike Rapoport
2026-05-11 16:28 ` [PATCH v4 18/55] selftests/mm: uffd-common: " Mike Rapoport
2026-05-11 16:28 ` [PATCH v4 19/55] selftests/mm: uffd-stress: " Mike Rapoport
2026-05-11 16:28 ` [PATCH v4 20/55] selftests/mm: uffd-unit-tests: " Mike Rapoport
2026-05-11 16:28 ` [PATCH v4 21/55] selftests/mm: va_high_addr_switch: " Mike Rapoport
2026-05-11 16:28 ` [PATCH v4 22/55] selftests/mm: add atexit() and signal handlers to thp_settings Mike Rapoport
2026-05-11 16:28 ` [PATCH v4 23/55] selftests/mm: rename thp_settings.[ch] to hugepage_settings.[ch] Mike Rapoport
2026-05-11 16:28 ` [PATCH v4 24/55] selftests/mm: move HugeTLB helpers to hugepage_settings Mike Rapoport
2026-05-11 16:28 ` [PATCH v4 25/55] selftests/mm: hugepage_settings: use unsigned long in detect_hugetlb_page_size Mike Rapoport
2026-05-11 16:28 ` [PATCH v4 26/55] selftests/mm: hugepage_settings: add APIs to get and set nr_hugepages Mike Rapoport
2026-05-11 16:28 ` [PATCH v4 27/55] selftests/mm: hugepage_settings: rename and rework get_free_hugepages() Mike Rapoport
2026-05-11 16:28 ` [PATCH v4 28/55] selftests/mm: hugepage_settings: add APIs for HugeTLB setup and teardown Mike Rapoport
2026-05-13 19:50 ` Luiz Capitulino [this message]
2026-05-14 10:26 ` Mike Rapoport
2026-05-11 16:28 ` [PATCH v4 29/55] selftests/mm: move read_file(), read_num() and write_num() to vm_util Mike Rapoport
2026-05-11 16:28 ` [PATCH v4 30/55] selftests/mm: vm_util: add helpers to set and restore shm limits Mike Rapoport
2026-05-11 16:28 ` [PATCH v4 31/55] selftests/mm: compaction_test: use HugeTLB helpers Mike Rapoport
2026-05-11 16:28 ` [PATCH v4 32/55] selftests/mm: cow: add setup of HugeTLB pages Mike Rapoport
2026-05-11 16:28 ` [PATCH v4 33/55] selftests/mm: gup_longterm: " Mike Rapoport
2026-05-11 16:28 ` [PATCH v4 34/55] selftests/mm: gup_test: " Mike Rapoport
2026-05-11 16:28 ` [PATCH v4 35/55] selftests/mm: hmm-tests: " Mike Rapoport
2026-05-11 16:28 ` [PATCH v4 36/55] selftests/mm: hugepage_dio: " Mike Rapoport
2026-05-11 16:28 ` [PATCH v4 37/55] selftests/mm: hugetlb_fault_after_madv: " Mike Rapoport
2026-05-11 16:28 ` [PATCH v4 38/55] selftests/mm: hugetlb-madvise: " Mike Rapoport
2026-05-11 16:28 ` [PATCH v4 39/55] selftests/mm: hugetlb_madv_vs_map: " Mike Rapoport
2026-05-11 16:28 ` [PATCH v4 40/55] selftests/mm: hugetlb-mmap: " Mike Rapoport
2026-05-11 16:28 ` [PATCH v4 41/55] selftests/mm: hugetlb-mremap: " Mike Rapoport
2026-05-11 16:28 ` [PATCH v4 42/55] selftests/mm: hugetlb-shm: " Mike Rapoport
2026-05-11 16:28 ` [PATCH v4 43/55] selftests/mm: hugetlb-soft-offline: " Mike Rapoport
2026-05-11 16:28 ` [PATCH v4 44/55] selftests/mm: hugetlb-vmemmap: " Mike Rapoport
2026-05-11 16:28 ` [PATCH v4 45/55] selftests/mm: migration: " Mike Rapoport
2026-05-11 16:28 ` [PATCH v4 46/55] selftests/mm: pagemap_ioctl: " Mike Rapoport
2026-05-11 16:28 ` [PATCH v4 47/55] selftests/mm: protection_keys: use library code for HugeTLB setup Mike Rapoport
2026-05-11 16:28 ` [PATCH v4 48/55] selftests/mm: thuge-gen: add setup of HugeTLB pages Mike Rapoport
2026-05-11 16:28 ` [PATCH v4 49/55] selftests/mm: uffd-stress: use hugetlb_save and alloc huge pages Mike Rapoport
2026-05-11 16:28 ` [PATCH v4 50/55] selftests/mm: uffd-unit-tests: add setup of HugeTLB pages Mike Rapoport
2026-05-11 16:28 ` [PATCH v4 51/55] selftests/mm: uffd-wp-mremap: " Mike Rapoport
2026-05-11 16:28 ` [PATCH v4 52/55] selftests/mm: va_high_addr_switch: " Mike Rapoport
2026-05-11 16:28 ` [PATCH v4 53/55] selftests/mm: va_high_addr_switch.sh: drop huge pages setup Mike Rapoport
2026-05-11 16:28 ` [PATCH v4 54/55] selftests/mm: run_vmtests.sh: free memory if available memory is low Mike Rapoport
2026-05-11 16:28 ` [PATCH v4 55/55] selftests/mm: run_vmtests.sh: drop detection and setup of HugeTLB Mike Rapoport
2026-05-11 21:34 ` [PATCH v4 00/55] make MM selftests more CI friendly Andrew Morton
2026-05-13 19:53 ` Luiz Capitulino
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=635a6440-9339-49fb-ad9b-ee2f4b285ef6@redhat.com \
--to=luizcap@redhat.com \
--cc=Liam.Howlett@oracle.com \
--cc=akpm@linux-foundation.org \
--cc=baohua@kernel.org \
--cc=baolin.wang@linux.alibaba.com \
--cc=broonie@kernel.org \
--cc=david@kernel.org \
--cc=dev.jain@arm.com \
--cc=donettom@linux.ibm.com \
--cc=jgg@ziepe.ca \
--cc=jhubbard@nvidia.com \
--cc=lance.yang@linux.dev \
--cc=leon@kernel.org \
--cc=li.wang@linux.dev \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-kselftest@vger.kernel.org \
--cc=linux-mm@kvack.org \
--cc=ljs@kernel.org \
--cc=mhocko@suse.com \
--cc=npache@redhat.com \
--cc=peterx@redhat.com \
--cc=rppt@kernel.org \
--cc=ryan.roberts@arm.com \
--cc=sarthak.sharma@arm.com \
--cc=shuah@kernel.org \
--cc=surenb@google.com \
--cc=vbabka@kernel.org \
--cc=ziy@nvidia.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox