Skip to contentSkip to conversation

selftests/mm: separate GUP microbenchmarking from functional testing

PATCH Superseded Sarthak Sharma 3 participants 14 messages May 21 – May 26

Subsystems (8)

v3 of 11 Superseded Latest is v11 →

lore
Share & tools
lore
#0 lore raw

v3 cover selftests/mm: separate GUP microbenchmarking from functional testing

gup_test.c currently serves two distinct purposes: microbenchmarking (GUP_FAST_BENCHMARK, PIN_FAST_BENCHMARK, PIN_LONGTERM_BENCHMARK) and functional correctness testing (GUP_BASIC_TEST, PIN_BASIC_TEST, DUMP_USER_PAGES_TEST). Mixing these in a single binary means functional tests cannot be run or reported individually and run_vmtests.sh must invoke the binary multiple times with different flag combinations to cover all configurations.

This patch series separates the two concerns: tools/mm/gup_bench for benchmarking and tools/testing/selftests/mm/gup_test for functional testing. To avoid duplicating HugeTLB and related file helpers, the series first moves the common helper code to tools/lib/mm/ so it can be shared by both selftests and tools/mm.

Patch 1 adds tools/lib/mm/file_utils.[ch], moving read_file(), write_file(), read_num() and write_num() out of vm_util.c into a shared helper without a kselftest dependency. It also adds tools/lib/mm/ to the MEMORY MANAGEMENT - MISC entry in MAINTAINERS.

Patch 2 moves hugepage_settings.[ch] from selftests/mm to tools/lib/mm/, updates the selftests/mm users to include it from there and removes the remaining kselftest dependency from the implementation while dropping non-fatal hugetlb informational prints from the shared helper.

Patch 3 adds tools/mm/gup_bench.c, a standalone microbenchmark for GUP_FAST, PIN_FAST and PIN_LONGTERM via the CONFIG_GUP_TEST debugfs interface. It runs the same matrix of configurations as the old run_gup_matrix() shell function (all three commands, read/write, private/shared, four page counts, THP on/off, hugetlb), but as a standalone C program under tools/mm using the shared tools/lib/mm helpers.

Patch 4 rewrites gup_test.c as a kselftest harness-based selftest. It covers all five GUP kernel functions (get_user_pages, get_user_pages_fast, pin_user_pages, pin_user_pages_fast, pin_user_pages with FOLL_LONGTERM) plus DUMP_USER_PAGES_TEST, across 12 mapping configurations (THP on, THP off and hugetlb, each across private/shared and read/write variants) and four batch sizes (1, 512, 123, all pages). It also preserves the old sparse dump coverage for pages 0, 19 and 0x1000. Results are reported as standard TAP output with no command-line arguments required.

---
These patches apply on top of mm/mm-new.

Changes in v3:
- Address v2 feedback from Sashiko
- Add shared file_utils helpers under tools/lib/mm
- Move hugepage_settings out of selftests and into tools/lib/mm
- Convert gup_bench to use the shared tools/lib/mm helpers
- Guard against invalid thread counts in gup_bench
- Handle thread-array allocation failure cleanly in gup_bench
- Restore hugetlb settings on setup failure in gup_test
- Add sparse DUMP_USER_PAGES_TEST coverage for pages 0, 19 and 0x1000 in gup_test

Changes in v2:
- Address v1 feedback from Sashiko
- Add fast and longterm GUP/PUP coverage
- Sweep nr_pages_per_call over 1, 512, 123 and all pages
- Call madvise(MADV_NOHUGEPAGE) in non-THP variants
- Use 256 MB for hugetlb fixtures
- Use hugetlb_restore_settings() in FIXTURE_TEARDOWN instead of atexit()
- Add TH_LOG to report nr_pages_per_call for each iteration
- Update Documentation/core-api/pin_user_pages.rst unit testing section

Previous versions:
v2: https://lore.kernel.org/all/[email protected]/
v1: https://lore.kernel.org/all/[email protected]/

---
Sarthak Sharma (4):
tools/lib/mm: add shared file helpers
tools/lib/mm: move hugepage_settings out of selftests tools/mm: add a standalone GUP microbenchmark selftests/mm: rewrite gup_test as a standalone harness-based selftest

 Documentation/core-api/pin_user_pages.rst     |  12 +-
 MAINTAINERS                                   |   2 +
 tools/lib/mm/file_utils.c                     |  83 +++
 tools/lib/mm/file_utils.h                     |  12 +
 .../selftests => lib}/mm/hugepage_settings.c  |  14 +-
 .../selftests => lib}/mm/hugepage_settings.h  |   0
 tools/mm/.gitignore                           |   2 +
 tools/mm/Makefile                             |  10 +-
 tools/mm/gup_bench.c                          | 390 ++++++++++++
 tools/testing/selftests/mm/Makefile           |   8 +-
 tools/testing/selftests/mm/compaction_test.c  |   2 +-
 tools/testing/selftests/mm/cow.c              |   2 +-
 .../selftests/mm/folio_split_race_test.c      |   3 +-
 tools/testing/selftests/mm/guard-regions.c    |   3 +-
 tools/testing/selftests/mm/gup_longterm.c     |   2 +-
 tools/testing/selftests/mm/gup_test.c         | 587 +++++++++++-------
 tools/testing/selftests/mm/hmm-tests.c        |   6 +-
 tools/testing/selftests/mm/hugetlb-madvise.c  |   3 +-
 tools/testing/selftests/mm/hugetlb-mmap.c     |   3 +-
 tools/testing/selftests/mm/hugetlb-mremap.c   |   3 +-
 tools/testing/selftests/mm/hugetlb-shm.c      |   2 +-
 .../selftests/mm/hugetlb-soft-offline.c       |   2 +-
 tools/testing/selftests/mm/hugetlb-vmemmap.c  |   3 +-
 tools/testing/selftests/mm/hugetlb_dio.c      |   3 +-
 .../selftests/mm/hugetlb_fault_after_madv.c   |   2 +-
 .../selftests/mm/hugetlb_madv_vs_map.c        |   2 +-
 tools/testing/selftests/mm/khugepaged.c       |   3 +-
 tools/testing/selftests/mm/ksm_tests.c        |   2 +-
 tools/testing/selftests/mm/migration.c        |   6 +-
 tools/testing/selftests/mm/pagemap_ioctl.c    |   2 +-
 .../testing/selftests/mm/prctl_thp_disable.c  |   2 +-
 tools/testing/selftests/mm/protection_keys.c  |   2 +-
 tools/testing/selftests/mm/run_vmtests.sh     |  37 +-
 tools/testing/selftests/mm/soft-dirty.c       |   2 +-
 .../selftests/mm/split_huge_page_test.c       |   4 +-
 tools/testing/selftests/mm/thuge-gen.c        |   3 +-
 tools/testing/selftests/mm/transhuge-stress.c |   3 +-
 tools/testing/selftests/mm/uffd-common.h      |   2 +-
 tools/testing/selftests/mm/uffd-wp-mremap.c   |   3 +-
 .../selftests/mm/va_high_addr_switch.c        |   2 +-
 tools/testing/selftests/mm/vm_util.c          |  65 +-
 tools/testing/selftests/mm/vm_util.h          |   5 -
 42 files changed, 935 insertions(+), 369 deletions(-)
 create mode 100644 tools/lib/mm/file_utils.c
 create mode 100644 tools/lib/mm/file_utils.h
 rename tools/{testing/selftests => lib}/mm/hugepage_settings.c (98%)
 rename tools/{testing/selftests => lib}/mm/hugepage_settings.h (100%)
 create mode 100644 tools/mm/gup_bench.c

base-commit: 4c6512d033e5e9469cc76d07512c24a0ee0ac207 --
2.39.5

#1 lore raw

PATCH v3 1/4 tools/lib/mm: add shared file helpers

Move read_file(), write_file(), read_num(), and write_num() out of tools/testing/selftests/mm/vm_util.c into a new shared helper under tools/lib/mm/.

These helpers are used by mm selftests today and will also be needed by shared hugepage helpers in subsequent patches. Move them to a generic location and drop the kselftest-specific dependency from their implementation so they can be reused outside selftests as well.

Update the current mm selftest users to include the new header directly, and link the new helper into the selftests/mm build.

Add tools/lib/mm/ to the MEMORY MANAGEMENT - MISC entry in MAINTAINERS.

---
 MAINTAINERS                                   |  1 +
 tools/lib/mm/file_utils.c                     | 83 +++++++++++++++++++
 tools/lib/mm/file_utils.h                     | 12 +++
 tools/testing/selftests/mm/Makefile           |  6 +-
 .../testing/selftests/mm/hugepage_settings.c  |  3 +-
 tools/testing/selftests/mm/khugepaged.c       |  1 +
 .../selftests/mm/split_huge_page_test.c       |  2 +
 tools/testing/selftests/mm/vm_util.c          | 65 +--------------
 tools/testing/selftests/mm/vm_util.h          |  5 --
 9 files changed, 106 insertions(+), 72 deletions(-)
 create mode 100644 tools/lib/mm/file_utils.c
 create mode 100644 tools/lib/mm/file_utils.h
MAINTAINERS +1 -0
index 46ed0f0e76d8..7887a3263373 100644
@@ -16936,6 +16936,7 @@ F:	mm/memory-tiers.c
 F:	mm/page_idle.c
 F:	mm/pgalloc-track.h
 F:	mm/process_vm_access.c
+F:	tools/lib/mm/
 F:	tools/testing/selftests/mm/
 
 MEMORY MANAGEMENT - NUMA MEMBLOCKS AND NUMA EMULATION
tools/lib/mm/file_utils.c new file +83 -0

83 lines not on this page · raw message

tools/lib/mm/file_utils.h new file +12 -0
new file mode 100644
index 000000000000..060a84725c9d
@@ -0,0 +1,12 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+#ifndef __MM_FILE_UTILS_H__
+#define __MM_FILE_UTILS_H__
+
+#include <stddef.h>
+
+int read_file(const char *path, char *buf, size_t buflen);
+void write_file(const char *path, const char *buf, size_t buflen);
+unsigned long read_num(const char *path);
+void write_num(const char *path, unsigned long num);
+
+#endif
tools/testing/selftests/mm/Makefile +3 -3
index e6df968f0971..b5fb4b6ab31b 100644
@@ -37,7 +37,7 @@ endif
 # LDLIBS.
 MAKEFLAGS += --no-builtin-rules
 
-CFLAGS = -Wall -O2 -I $(top_srcdir) $(EXTRA_CFLAGS) $(KHDR_INCLUDES) $(TOOLS_INCLUDES)
+CFLAGS = -Wall -O2 -I $(top_srcdir) -I $(top_srcdir)/tools/lib $(EXTRA_CFLAGS) $(KHDR_INCLUDES) $(TOOLS_INCLUDES)
 CFLAGS += -Wunreachable-code
 LDLIBS = -lrt -lpthread -lm
 
@@ -187,8 +187,8 @@ TEST_FILES += write_hugetlb_memory.sh
 
 include ../lib.mk
 
-$(TEST_GEN_PROGS): vm_util.c hugepage_settings.c
-$(TEST_GEN_FILES): vm_util.c hugepage_settings.c
+$(TEST_GEN_PROGS): vm_util.c hugepage_settings.c $(top_srcdir)/tools/lib/mm/file_utils.c
+$(TEST_GEN_FILES): vm_util.c hugepage_settings.c $(top_srcdir)/tools/lib/mm/file_utils.c
 
 $(OUTPUT)/uffd-stress: uffd-common.c
 $(OUTPUT)/uffd-unit-tests: uffd-common.c
tools/testing/selftests/mm/hugepage_settings.c +2 -1
index 2eab2110ac6a..5e947abb7425 100644
@@ -8,8 +8,9 @@
 #include <stdlib.h>
 #include <string.h>
 #include <unistd.h>
+#include <mm/file_utils.h>
 
-#include "vm_util.h"
+#include "kselftest.h"
 #include "hugepage_settings.h"
 
 #define THP_SYSFS "/sys/kernel/mm/transparent_hugepage/"
tools/testing/selftests/mm/khugepaged.c +1 -0
index 10e8dedcb087..ae5945e2bfac 100644
@@ -18,6 +18,7 @@
 #include <sys/stat.h>
 #include <sys/sysmacros.h>
 #include <sys/vfs.h>
+#include <mm/file_utils.h>
 
 #include "linux/magic.h"
 
tools/testing/selftests/mm/split_huge_page_test.c +2 -0
index 50c80ceb4988..4a2ccc055c8b 100644
@@ -19,6 +19,8 @@
 #include <malloc.h>
 #include <stdbool.h>
 #include <time.h>
+#include <mm/file_utils.h>
+
 #include "vm_util.h"
 #include "kselftest.h"
 #include "hugepage_settings.h"
tools/testing/selftests/mm/vm_util.c +2 -63

77 lines not on this page · raw message

tools/testing/selftests/mm/vm_util.h +0 -5
index ea8fc8fdf0eb..d0932cdd9a34 100644
@@ -164,11 +164,6 @@ int unpoison_memory(unsigned long pfn);
 #define PAGEMAP_PRESENT(ent)	(((ent) & (1ull << 63)) != 0)
 #define PAGEMAP_PFN(ent)	((ent) & ((1ull << 55) - 1))
 
-void write_file(const char *path, const char *buf, size_t buflen);
-int read_file(const char *path, char *buf, size_t buflen);
-unsigned long read_num(const char *path);
-void write_num(const char *path, unsigned long num);
-
 void shm_limits_prepare(unsigned long length);
 void __shm_limits_restore(void);
 
Signature · 1 line
2.39.5
maintainer
#2 lore raw

Re: [PATCH v3 1/4] tools/lib/mm: add shared file helpers

On Thu, 21 May 2026 16:47:58 +0530, Sarthak Sharma <[email protected]> wrote:

Hi Sarthak,

Quoted 8 lines
tools/lib/mm/file_utils.c new file +0 -0
new file mode 100644
index 000000000000..0f9322f2cf41
@@ -0,0 +1,83 @@

[ ... skip 48 lines ... ]

tools/lib/mm/file_utils.c line 54

Quoted 6 lines
tools/lib/mm/file_utils.c new file +6 -0
+	saved_errno = errno;
+	close(fd);
+	errno = saved_errno;
+	if (numwritten < 0) {
+		fprintf(stderr, "%s write(%.*s) failed: %s\n",
+			path, (int)(buflen - 1), buf, strerror(errno));

This would break TAP formatting for selftests.

tools/lib/mm/file_utils.c

Quoted 1 line
tools/lib/mm/file_utils.c new file +1 -0
+		exit(EXIT_FAILURE);

and while EXIT_FAILURE == KSFT_FAIL I'm not sure it's robust enough.

tools/testing/selftests/mm/hugepage_settings.c line 12

Quoted 12 lines
tools/testing/selftests/mm/hugepage_settings.c +1 -1
index 2eab2110ac6a..5e947abb7425 100644
@@ -8,8 +8,9 @@
 #include <stdlib.h>
 #include <string.h>
 #include <unistd.h>
+#include <mm/file_utils.h>
 
-#include "vm_util.h"

I think it would be fine to include file_utils.h in vm_utils.h and avoid further churn.

Signature · 2 lines
Sincerely yours,
Mike.
#3 lore raw

Re: [PATCH v3 1/4] tools/lib/mm: add shared file helpers

Hi Mike!

Quoted 5 lines · Mike Rapoport

On Thu, 21 May 2026 16:47:58 +0530, Sarthak Sharma <[email protected]> wrote:

Hi Sarthak,

Quoted 1 line
Quoted 15 lines

tools/lib/mm/file_utils.c line 54

Quoted 13 lines
tools/lib/mm/file_utils.c new file +0 -0
new file mode 100644
index 000000000000..0f9322f2cf41
@@ -0,0 +1,83 @@

[ ... skip 48 lines ... ]

tools/lib/mm/file_utils.c new file +6 -0
+	saved_errno = errno;
+	close(fd);
+	errno = saved_errno;
+	if (numwritten < 0) {
+		fprintf(stderr, "%s write(%.*s) failed: %s\n",
+			path, (int)(buflen - 1), buf, strerror(errno));

This would break TAP formatting for selftests.

Yes, thanks for pointing it out.

Quoted 4 lines

tools/lib/mm/file_utils.c

Quoted 2 lines
tools/lib/mm/file_utils.c new file +1 -0
+		exit(EXIT_FAILURE);

and while EXIT_FAILURE == KSFT_FAIL I'm not sure it's robust enough.

I used EXIT_FAILURE here because the helper is moving out of selftests and should not include kselftest.h anymore. The helper already terminated the process on these paths, so I tried to preserve that behavior while removing the ksft dependency.

We can change this to return errors instead of calling exit() and update the selftest callers to report failures through the ksft_* helpers. I agree this is cleaner, but it would grow the series a bit.

If you feel strongly, I can include these changes in v4. Otherwise I feel we can handle it separately later to avoid growing this series.

Quoted 12 lines
Quoted 12 lines
tools/testing/selftests/mm/hugepage_settings.c +1 -0
index 2eab2110ac6a..5e947abb7425 100644
@@ -8,8 +8,9 @@
 #include <stdlib.h>
 #include <string.h>
 #include <unistd.h>
+#include <mm/file_utils.h>
 
Quoted 4 lines

tools/testing/selftests/mm/hugepage_settings.c line 12

Quoted 1 line
tools/testing/selftests/mm/hugepage_settings.c +0 -1
-#include "vm_util.h"

I think it would be fine to include file_utils.h in vm_utils.h and avoid further churn.

Okay, I'll change this.

maintainer
#4 lore raw

Re: [PATCH v3 1/4] tools/lib/mm: add shared file helpers

Quoted 27 lines · Sarthak Sharma

Hi Mike!

Quoted 5 lines · Mike Rapoport

On Thu, 21 May 2026 16:47:58 +0530, Sarthak Sharma <[email protected]> wrote:

Hi Sarthak,

Quoted 1 line
Quoted 15 lines

tools/lib/mm/file_utils.c line 54

Quoted 13 lines
tools/lib/mm/file_utils.c new file +0 -0
new file mode 100644
index 000000000000..0f9322f2cf41
@@ -0,0 +1,83 @@

[ ... skip 48 lines ... ]

tools/lib/mm/file_utils.c new file +6 -0
+	saved_errno = errno;
+	close(fd);
+	errno = saved_errno;
+	if (numwritten < 0) {
+		fprintf(stderr, "%s write(%.*s) failed: %s\n",
+			path, (int)(buflen - 1), buf, strerror(errno));

This would break TAP formatting for selftests.

Yes, thanks for pointing it out.

Quoted 1 line
Quoted 8 lines
Quoted 3 lines

tools/lib/mm/file_utils.c

Quoted 1 line
tools/lib/mm/file_utils.c new file +1 -0
+		exit(EXIT_FAILURE);

and while EXIT_FAILURE == KSFT_FAIL I'm not sure it's robust enough.

I used EXIT_FAILURE here because the helper is moving out of selftests and should not include kselftest.h anymore. The helper already terminated the process on these paths, so I tried to preserve that behavior while removing the ksft dependency.

In mm selftests a failure to update a /proc or /sysfs file meant there is no point to continue the test. But if we make it a generic helper for potentially broader use than mm selftests, exit() on failure is too harsh.

Quoted 6 lines

We can change this to return errors instead of calling exit() and update the selftest callers to report failures through the ksft_* helpers. I agree this is cleaner, but it would grow the series a bit.

If you feel strongly, I can include these changes in v4. Otherwise I feel we can handle it separately later to avoid growing this series.

There are not that many callers of write_file() and write_num(). I think a patch that makes them return an error rather than exit() can go before moving these functions to lib.

Quoted 17 lines
Quoted 11 lines
Quoted 11 lines
tools/testing/selftests/mm/hugepage_settings.c +1 -0
index 2eab2110ac6a..5e947abb7425 100644
@@ -8,8 +8,9 @@
 #include <stdlib.h>
 #include <string.h>
 #include <unistd.h>
+#include <mm/file_utils.h>
 
Quoted 4 lines

tools/testing/selftests/mm/hugepage_settings.c line 12

Quoted 1 line
tools/testing/selftests/mm/hugepage_settings.c +0 -1
-#include "vm_util.h"

I think it would be fine to include file_utils.h in vm_utils.h and avoid further churn.

Okay, I'll change this.

Signature · 2 lines
Sincerely yours,
Mike.
#5 lore raw

Re: [PATCH v3 1/4] tools/lib/mm: add shared file helpers

Quoted 32 lines · Mike Rapoport
Quoted 31 lines · Sarthak Sharma

Hi Mike!

Quoted 5 lines · Mike Rapoport

On Thu, 21 May 2026 16:47:58 +0530, Sarthak Sharma <[email protected]> wrote:

Hi Sarthak,

Quoted 1 line
Quoted 15 lines

tools/lib/mm/file_utils.c line 54

Quoted 13 lines
tools/lib/mm/file_utils.c new file +0 -0
new file mode 100644
index 000000000000..0f9322f2cf41
@@ -0,0 +1,83 @@

[ ... skip 48 lines ... ]

tools/lib/mm/file_utils.c new file +6 -0
+	saved_errno = errno;
+	close(fd);
+	errno = saved_errno;
+	if (numwritten < 0) {
+		fprintf(stderr, "%s write(%.*s) failed: %s\n",
+			path, (int)(buflen - 1), buf, strerror(errno));

This would break TAP formatting for selftests.

Yes, thanks for pointing it out.

Quoted 4 lines

tools/lib/mm/file_utils.c

Quoted 2 lines
tools/lib/mm/file_utils.c new file +1 -0
+		exit(EXIT_FAILURE);

and while EXIT_FAILURE == KSFT_FAIL I'm not sure it's robust enough.

Quoted 8 lines
Quoted 4 lines

I used EXIT_FAILURE here because the helper is moving out of selftests and should not include kselftest.h anymore. The helper already terminated the process on these paths, so I tried to preserve that behavior while removing the ksft dependency.

In mm selftests a failure to update a /proc or /sysfs file meant there is no point to continue the test. But if we make it a generic helper for potentially broader use than mm selftests, exit() on failure is too harsh.

Okay yeah, this makes sense.

Quoted 5 lines
Quoted 5 lines

We can change this to return errors instead of calling exit() and update the selftest callers to report failures through the ksft_* helpers. I agree this is cleaner, but it would grow the series a bit.

Quoted 7 lines
Quoted 2 lines

If you feel strongly, I can include these changes in v4. Otherwise I feel we can handle it separately later to avoid growing this series.

There are not that many callers of write_file() and write_num(). I think a patch that makes them return an error rather than exit() can go before moving these functions to lib.

So I will add a patch before the move that makes read_file(), write_file(), read_num() and write_num() return errors instead of exiting and update the existing selftest callers to report those failures via ksft_* helpers.

For hugepage_settings.c, I’d prefer to keep the existing fail fast behaviour in this series. After this series, the users are still mm selftests and the new tools/mm/gup_bench tool and for those users a failure to read/write THP or HugeTLB state is fatal to the operation being attempted.

Converting the full hugepage_settings API to return errors would be a larger follow-up, because many of its helpers are used throughout mm selftests. I can handle that in a separate series unless you think it should be folded into this one as well.

Quoted 18 lines
Quoted 17 lines
Quoted 11 lines
Quoted 11 lines
tools/testing/selftests/mm/hugepage_settings.c +1 -0
index 2eab2110ac6a..5e947abb7425 100644
@@ -8,8 +8,9 @@
 #include <stdlib.h>
 #include <string.h>
 #include <unistd.h>
+#include <mm/file_utils.h>
 
Quoted 4 lines

tools/testing/selftests/mm/hugepage_settings.c line 12

Quoted 1 line
tools/testing/selftests/mm/hugepage_settings.c +0 -1
-#include "vm_util.h"

I think it would be fine to include file_utils.h in vm_utils.h and avoid further churn.

Okay, I'll change this.

maintainer
#6 lore raw

Re: [PATCH v3 1/4] tools/lib/mm: add shared file helpers

Quoted 41 lines · Sarthak Sharma
Quoted 5 lines · Mike Rapoport
Quoted 4 lines · Sarthak Sharma
Quoted 2 lines · Mike Rapoport

and while EXIT_FAILURE == KSFT_FAIL I'm not sure it's robust enough.

Quoted 8 lines
Quoted 4 lines

I used EXIT_FAILURE here because the helper is moving out of selftests and should not include kselftest.h anymore. The helper already terminated the process on these paths, so I tried to preserve that behavior while removing the ksft dependency.

In mm selftests a failure to update a /proc or /sysfs file meant there is no point to continue the test. But if we make it a generic helper for potentially broader use than mm selftests, exit() on failure is too harsh.

Okay yeah, this makes sense.

Quoted 5 lines
Quoted 5 lines

We can change this to return errors instead of calling exit() and update the selftest callers to report failures through the ksft_* helpers. I agree this is cleaner, but it would grow the series a bit.

Quoted 7 lines
Quoted 2 lines

If you feel strongly, I can include these changes in v4. Otherwise I feel we can handle it separately later to avoid growing this series.

There are not that many callers of write_file() and write_num(). I think a patch that makes them return an error rather than exit() can go before moving these functions to lib.

So I will add a patch before the move that makes read_file(), write_file(), read_num() and write_num() return errors instead of exiting and update the existing selftest callers to report those failures via ksft_* helpers.

For hugepage_settings.c, I’d prefer to keep the existing fail fast behaviour in this series. After this series, the users are still mm selftests and the new tools/mm/gup_bench tool and for those users a failure to read/write THP or HugeTLB state is fatal to the operation being attempted.

Quoted 4 lines

Converting the full hugepage_settings API to return errors would be a larger follow-up, because many of its helpers are used throughout mm selftests. I can handle that in a separate series unless you think it should be folded into this one as well.

Keeping fail fast in hugepage_settings for now makes sense, they are not as generic as write_file().

Signature · 2 lines
Sincerely yours,
Mike.
#7 lore raw

PATCH v3 2/4 tools/lib/mm: move hugepage_settings out of selftests

In reply to Sarthak Sharma

Move hugepage_settings.[ch] from tools/testing/selftests/mm/ to tools/lib/mm/ so the THP and HugeTLB helpers can be shared more easily between selftests and other tools.

Update the mm selftest users to include the header from tools/lib/mm/ and adjust the selftests/mm build to compile the moved implementation from its new location.

Remove the remaining kselftest dependency from hugepage_settings.c by replacing ksft_print_msg() calls with plain fprintf() calls. Drop the non-fatal hugetlb informational prints from the shared helper, so it does not emit unwanted output during selftest runs.

---
 .../selftests => lib}/mm/hugepage_settings.c      | 15 +++++++++------
 .../selftests => lib}/mm/hugepage_settings.h      |  0
 tools/testing/selftests/mm/Makefile               |  6 ++++--
 tools/testing/selftests/mm/compaction_test.c      |  2 +-
 tools/testing/selftests/mm/cow.c                  |  2 +-
 .../testing/selftests/mm/folio_split_race_test.c  |  3 ++-
 tools/testing/selftests/mm/guard-regions.c        |  3 ++-
 tools/testing/selftests/mm/gup_longterm.c         |  2 +-
 tools/testing/selftests/mm/gup_test.c             |  3 ++-
 tools/testing/selftests/mm/hmm-tests.c            |  6 +++---
 tools/testing/selftests/mm/hugetlb-madvise.c      |  3 ++-
 tools/testing/selftests/mm/hugetlb-mmap.c         |  3 ++-
 tools/testing/selftests/mm/hugetlb-mremap.c       |  3 ++-
 tools/testing/selftests/mm/hugetlb-shm.c          |  2 +-
 tools/testing/selftests/mm/hugetlb-soft-offline.c |  2 +-
 tools/testing/selftests/mm/hugetlb-vmemmap.c      |  3 ++-
 tools/testing/selftests/mm/hugetlb_dio.c          |  3 ++-
 .../selftests/mm/hugetlb_fault_after_madv.c       |  2 +-
 tools/testing/selftests/mm/hugetlb_madv_vs_map.c  |  2 +-
 tools/testing/selftests/mm/khugepaged.c           |  2 +-
 tools/testing/selftests/mm/ksm_tests.c            |  2 +-
 tools/testing/selftests/mm/migration.c            |  6 +++---
 tools/testing/selftests/mm/pagemap_ioctl.c        |  2 +-
 tools/testing/selftests/mm/prctl_thp_disable.c    |  2 +-
 tools/testing/selftests/mm/protection_keys.c      |  2 +-
 tools/testing/selftests/mm/soft-dirty.c           |  2 +-
 tools/testing/selftests/mm/split_huge_page_test.c |  2 +-
 tools/testing/selftests/mm/thuge-gen.c            |  3 ++-
 tools/testing/selftests/mm/transhuge-stress.c     |  3 ++-
 tools/testing/selftests/mm/uffd-common.h          |  2 +-
 tools/testing/selftests/mm/uffd-wp-mremap.c       |  3 ++-
 tools/testing/selftests/mm/va_high_addr_switch.c  |  2 +-
 32 files changed, 57 insertions(+), 41 deletions(-)
 rename tools/{testing/selftests => lib}/mm/hugepage_settings.c (98%)
 rename tools/{testing/selftests => lib}/mm/hugepage_settings.h (100%)
tools/lib/mm/hugepage_settings.c renamed from tools/testing/selftests/mm/hugepage_settings.c +9 -6
similarity index 98%
rename from tools/testing/selftests/mm/hugepage_settings.c
rename to tools/lib/mm/hugepage_settings.c
index 5e947abb7425..b08b27776fc5 100644
@@ -8,13 +8,17 @@
 #include <stdlib.h>
 #include <string.h>
 #include <unistd.h>
-#include <mm/file_utils.h>
 
-#include "kselftest.h"
+#include "file_utils.h"
 #include "hugepage_settings.h"
 
 #define THP_SYSFS "/sys/kernel/mm/transparent_hugepage/"
 #define MAX_SETTINGS_DEPTH 4
+
+#ifndef ARRAY_SIZE
+#define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]))
+#endif
+
 static struct thp_settings settings_stack[MAX_SETTINGS_DEPTH];
 static int settings_index;
 static struct thp_settings saved_settings;
@@ -383,8 +387,6 @@ int detect_hugetlb_page_sizes(unsigned long sizes[], int max)
 		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;
@@ -503,7 +505,6 @@ unsigned long hugetlb_setup(unsigned long nr, unsigned long sizes[],
 		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;
 	}
 
@@ -575,8 +576,10 @@ static void hugepage_restore_settings_atexit(void)
 
 static void hugepage_restore_settings_sighandler(int sig)
 {
+	(void)sig;
+
 	/* exit() will invoke the hugepage_restore_settings_atexit handler. */
-	exit(KSFT_FAIL);
+	exit(EXIT_FAILURE);
 }
 
 void hugepage_save_settings(bool thp, bool hugetlb)
tools/lib/mm/hugepage_settings.h renamed from tools/testing/selftests/mm/hugepage_settings.h +0 -0
similarity index 100%
rename from tools/testing/selftests/mm/hugepage_settings.h
rename to tools/lib/mm/hugepage_settings.h
tools/testing/selftests/mm/Makefile +4 -2
index b5fb4b6ab31b..8a307b777630 100644
@@ -187,8 +187,10 @@ TEST_FILES += write_hugetlb_memory.sh
 
 include ../lib.mk
 
-$(TEST_GEN_PROGS): vm_util.c hugepage_settings.c $(top_srcdir)/tools/lib/mm/file_utils.c
-$(TEST_GEN_FILES): vm_util.c hugepage_settings.c $(top_srcdir)/tools/lib/mm/file_utils.c
+$(TEST_GEN_PROGS): vm_util.c $(top_srcdir)/tools/lib/mm/hugepage_settings.c \
+		   $(top_srcdir)/tools/lib/mm/file_utils.c
+$(TEST_GEN_FILES): vm_util.c $(top_srcdir)/tools/lib/mm/hugepage_settings.c \
+		   $(top_srcdir)/tools/lib/mm/file_utils.c
 
 $(OUTPUT)/uffd-stress: uffd-common.c
 $(OUTPUT)/uffd-unit-tests: uffd-common.c
tools/testing/selftests/mm/compaction_test.c +1 -1
index de0633f9a7e5..7c58506c0aa7 100644
@@ -15,9 +15,9 @@
 #include <errno.h>
 #include <unistd.h>
 #include <string.h>
+#include <mm/hugepage_settings.h>
 
 #include "kselftest.h"
-#include "hugepage_settings.h"
 
 #define MAP_SIZE_MB	100
 #define MAP_SIZE	(MAP_SIZE_MB * 1024 * 1024)
tools/testing/selftests/mm/cow.c +1 -1
index 0c627ea89ff7..7c29d3d2d887 100644
@@ -20,6 +20,7 @@
 #include <sys/ioctl.h>
 #include <sys/wait.h>
 #include <linux/memfd.h>
+#include <mm/hugepage_settings.h>
 
 #include "local_config.h"
 #ifdef LOCAL_CONFIG_HAVE_LIBURING
@@ -29,7 +30,6 @@
 #include "../../../../mm/gup_test.h"
 #include "kselftest.h"
 #include "vm_util.h"
-#include "hugepage_settings.h"
 
 static size_t pagesize;
 static int pagemap_fd;
tools/testing/selftests/mm/folio_split_race_test.c +2 -1
index 6329e37fff4c..ca9b95c39c16 100644
@@ -23,9 +23,10 @@
 #include <sys/mman.h>
 #include <signal.h>
 #include <unistd.h>
+#include <mm/hugepage_settings.h>
+
 #include "vm_util.h"
 #include "kselftest.h"
-#include "hugepage_settings.h"
 
 uint64_t page_size;
 uint64_t pmd_pagesize;
tools/testing/selftests/mm/guard-regions.c +2 -1
index b21df3040b1c..ccca432c8802 100644
@@ -20,8 +20,9 @@
 #include <sys/syscall.h>
 #include <sys/uio.h>
 #include <unistd.h>
+#include <mm/hugepage_settings.h>
+
 #include "vm_util.h"
-#include "hugepage_settings.h"
 
 #include "../pidfd/pidfd.h"
 
tools/testing/selftests/mm/gup_longterm.c +1 -1
index eb8963e9d98f..0cfc67fc5c8f 100644
@@ -20,6 +20,7 @@
 #include <sys/vfs.h>
 #include <linux/magic.h>
 #include <linux/memfd.h>
+#include <mm/hugepage_settings.h>
 
 #include "local_config.h"
 #ifdef LOCAL_CONFIG_HAVE_LIBURING
@@ -29,7 +30,6 @@
 #include "../../../../mm/gup_test.h"
 #include "kselftest.h"
 #include "vm_util.h"
-#include "hugepage_settings.h"
 
 static size_t pagesize;
 static int nr_hugetlbsizes;
tools/testing/selftests/mm/gup_test.c +2 -1
index 3f841a96f870..803ab829a841 100644
@@ -12,9 +12,10 @@
 #include <pthread.h>
 #include <assert.h>
 #include <mm/gup_test.h>
+#include <mm/hugepage_settings.h>
+
 #include "kselftest.h"
 #include "vm_util.h"
-#include "hugepage_settings.h"
 
 #define MB (1UL << 20)
 
tools/testing/selftests/mm/hmm-tests.c +3 -3
index e1c8a679a4cf..b7f85a144f03 100644
@@ -10,9 +10,6 @@
  * bugs.
  */
 
-#include "kselftest_harness.h"
-#include "hugepage_settings.h"
-
 #include <errno.h>
 #include <fcntl.h>
 #include <stdio.h>
@@ -27,6 +24,9 @@
 #include <sys/mman.h>
 #include <sys/ioctl.h>
 #include <sys/time.h>
+#include <mm/hugepage_settings.h>
+
+#include "kselftest_harness.h"
 
 /*
  * This is a private UAPI to the kernel test module so it isn't exported
tools/testing/selftests/mm/hugetlb-madvise.c +2 -1
index 555b4b3d1430..8e18b5027904 100644
@@ -12,9 +12,10 @@
 #include <unistd.h>
 #include <sys/mman.h>
 #include <fcntl.h>
+#include <mm/hugepage_settings.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 */
tools/testing/selftests/mm/hugetlb-mmap.c +2 -1
index 0f2aad1b7dbd..12184c1889ad 100644
@@ -16,9 +16,10 @@
 #include <sys/mman.h>
 #include <fcntl.h>
 #include <linux/memfd.h>
+#include <mm/hugepage_settings.h>
+
 #include "vm_util.h"
 #include "kselftest.h"
-#include "hugepage_settings.h"
 
 #define LENGTH (256UL*1024*1024)
 #define PROTECTION (PROT_READ | PROT_WRITE)
tools/testing/selftests/mm/hugetlb-mremap.c +2 -1
index d239905790dd..0b1a1fc9e766 100644
@@ -24,9 +24,10 @@
 #include <sys/ioctl.h>
 #include <string.h>
 #include <stdbool.h>
+#include <mm/hugepage_settings.h>
+
 #include "kselftest.h"
 #include "vm_util.h"
-#include "hugepage_settings.h"
 
 #define DEFAULT_LENGTH_MB 10UL
 #define MB_TO_BYTES(x) (x * 1024 * 1024)
tools/testing/selftests/mm/hugetlb-shm.c +1 -1
index 3ff7f062b7eb..25988f97057a 100644
@@ -27,9 +27,9 @@
 #include <sys/ipc.h>
 #include <sys/shm.h>
 #include <sys/mman.h>
+#include <mm/hugepage_settings.h>
 
 #include "vm_util.h"
-#include "hugepage_settings.h"
 
 #define LENGTH (256UL*1024*1024)
 
tools/testing/selftests/mm/hugetlb-soft-offline.c +1 -1
index bc202e4ed2bd..20864e7d4e0c 100644
@@ -21,9 +21,9 @@
 #include <sys/mman.h>
 #include <sys/statfs.h>
 #include <sys/types.h>
+#include <mm/hugepage_settings.h>
 
 #include "kselftest.h"
-#include "hugepage_settings.h"
 
 #ifndef MADV_SOFT_OFFLINE
 #define MADV_SOFT_OFFLINE 101
tools/testing/selftests/mm/hugetlb-vmemmap.c +2 -1
index 507df78a158d..ffecea89ffa1 100644
@@ -10,8 +10,9 @@
 #include <unistd.h>
 #include <sys/mman.h>
 #include <fcntl.h>
+#include <mm/hugepage_settings.h>
+
 #include "vm_util.h"
-#include "hugepage_settings.h"
 
 #define PAGE_COMPOUND_HEAD	(1UL << 15)
 #define PAGE_COMPOUND_TAIL	(1UL << 16)
tools/testing/selftests/mm/hugetlb_dio.c +2 -1
index fb4600570e13..896c6e3c09da 100644
@@ -18,9 +18,10 @@
 #include <string.h>
 #include <sys/mman.h>
 #include <sys/syscall.h>
+#include <mm/hugepage_settings.h>
+
 #include "vm_util.h"
 #include "kselftest.h"
-#include "hugepage_settings.h"
 
 #ifndef STATX_DIOALIGN
 #define STATX_DIOALIGN		0x00002000U
tools/testing/selftests/mm/hugetlb_fault_after_madv.c +1 -1
index 2dc158054f66..70e685f0b510 100644
@@ -7,10 +7,10 @@
 #include <unistd.h>
 #include <setjmp.h>
 #include <signal.h>
+#include <mm/hugepage_settings.h>
 
 #include "vm_util.h"
 #include "kselftest.h"
-#include "hugepage_settings.h"
 
 #define INLOOP_ITER 100
 
tools/testing/selftests/mm/hugetlb_madv_vs_map.c +1 -1
index f94549efcc6f..c8f6414e8a77 100644
@@ -23,9 +23,9 @@
 #include <sys/mman.h>
 #include <sys/types.h>
 #include <unistd.h>
+#include <mm/hugepage_settings.h>
 
 #include "vm_util.h"
-#include "hugepage_settings.h"
 
 #define INLOOP_ITER 100
 
tools/testing/selftests/mm/khugepaged.c +1 -1
index ae5945e2bfac..9bf1f8744431 100644
@@ -19,11 +19,11 @@
 #include <sys/sysmacros.h>
 #include <sys/vfs.h>
 #include <mm/file_utils.h>
+#include <mm/hugepage_settings.h>
 
 #include "linux/magic.h"
 
 #include "vm_util.h"
-#include "hugepage_settings.h"
 
 #define BASE_ADDR ((void *)(1UL << 30))
 static unsigned long hpage_pmd_size;
tools/testing/selftests/mm/ksm_tests.c +1 -1
index a050f4840cfa..4fda799a148d 100644
@@ -11,11 +11,11 @@
 #include <fcntl.h>
 #include <stdint.h>
 #include <err.h>
+#include <mm/hugepage_settings.h>
 
 #include "kselftest.h"
 #include <include/vdso/time64.h>
 #include "vm_util.h"
-#include "hugepage_settings.h"
 
 #define KSM_SYSFS_PATH "/sys/kernel/mm/ksm/"
 #define KSM_FP(s) (KSM_SYSFS_PATH s)
tools/testing/selftests/mm/migration.c +3 -3
index 29f7492453d4..c3e60ddcefa7 100644
@@ -4,9 +4,6 @@
  * paths in the kernel.
  */
 
-#include "kselftest_harness.h"
-#include "hugepage_settings.h"
-
 #include <strings.h>
 #include <pthread.h>
 #include <numa.h>
@@ -16,6 +13,9 @@
 #include <sys/types.h>
 #include <signal.h>
 #include <time.h>
+#include <mm/hugepage_settings.h>
+
+#include "kselftest_harness.h"
 #include "vm_util.h"
 
 #define TWOMEG		(2<<20)
tools/testing/selftests/mm/pagemap_ioctl.c +1 -1
index 762306177ad8..924308ead037 100644
@@ -20,10 +20,10 @@
 #include <assert.h>
 #include <sys/ipc.h>
 #include <sys/shm.h>
+#include <mm/hugepage_settings.h>
 
 #include "vm_util.h"
 #include "kselftest.h"
-#include "hugepage_settings.h"
 
 #define PAGEMAP_BITS_ALL		(PAGE_IS_WPALLOWED | PAGE_IS_WRITTEN |	\
 					 PAGE_IS_FILE | PAGE_IS_PRESENT |	\
tools/testing/selftests/mm/prctl_thp_disable.c +1 -1
index d8d9d1de57b8..db1807adf72e 100644
@@ -12,9 +12,9 @@
 #include <linux/mman.h>
 #include <sys/prctl.h>
 #include <sys/wait.h>
+#include <mm/hugepage_settings.h>
 
 #include "kselftest_harness.h"
-#include "hugepage_settings.h"
 #include "vm_util.h"
 
 #ifndef PR_THP_DISABLE_EXCEPT_ADVISED
tools/testing/selftests/mm/protection_keys.c +1 -1
index 9a6d954ee371..5ba2033e8a09 100644
@@ -45,8 +45,8 @@
 #include <unistd.h>
 #include <sys/ptrace.h>
 #include <setjmp.h>
+#include <mm/hugepage_settings.h>
 
-#include "hugepage_settings.h"
 #include "pkey-helpers.h"
 
 int iteration_nr = 1;
tools/testing/selftests/mm/soft-dirty.c +1 -1
index fb1864a68e1c..852cc0a9743f 100644
@@ -6,10 +6,10 @@
 #include <stdint.h>
 #include <malloc.h>
 #include <sys/mman.h>
+#include <mm/hugepage_settings.h>
 
 #include "kselftest.h"
 #include "vm_util.h"
-#include "hugepage_settings.h"
 
 #define PAGEMAP_FILE_PATH "/proc/self/pagemap"
 #define TEST_ITERATIONS 10000
tools/testing/selftests/mm/split_huge_page_test.c +1 -1
index 4a2ccc055c8b..b4ff8ed0b446 100644
@@ -20,10 +20,10 @@
 #include <stdbool.h>
 #include <time.h>
 #include <mm/file_utils.h>
+#include <mm/hugepage_settings.h>
 
 #include "vm_util.h"
 #include "kselftest.h"
-#include "hugepage_settings.h"
 
 uint64_t pagesize;
 unsigned int pageshift;
tools/testing/selftests/mm/thuge-gen.c +2 -1
index 22b9c2f1c35d..5556a1b89393 100644
@@ -12,9 +12,10 @@
 #include <unistd.h>
 #include <stdarg.h>
 #include <string.h>
+#include <mm/hugepage_settings.h>
+
 #include "vm_util.h"
 #include "kselftest.h"
-#include "hugepage_settings.h"
 
 #if !defined(MAP_HUGETLB)
 #define MAP_HUGETLB	0x40000
tools/testing/selftests/mm/transhuge-stress.c +2 -1
index 8eb0c5630e7e..ed78b2142ac1 100644
@@ -15,9 +15,10 @@
 #include <fcntl.h>
 #include <string.h>
 #include <sys/mman.h>
+#include <mm/hugepage_settings.h>
+
 #include "vm_util.h"
 #include "kselftest.h"
-#include "hugepage_settings.h"
 
 int backing_fd = -1;
 int mmap_flags = MAP_ANONYMOUS | MAP_NORESERVE | MAP_PRIVATE;
tools/testing/selftests/mm/uffd-common.h +1 -1
index 92a21b97f745..b8e25a96381d 100644
@@ -34,10 +34,10 @@
 #include <stdint.h>
 #include <sys/random.h>
 #include <stdatomic.h>
+#include <mm/hugepage_settings.h>
 
 #include "kselftest.h"
 #include "vm_util.h"
-#include "hugepage_settings.h"
 
 #define UFFD_FLAGS	(O_CLOEXEC | O_NONBLOCK | UFFD_USER_MODE_ONLY)
 
tools/testing/selftests/mm/uffd-wp-mremap.c +2 -1
index 90ac410c6c6f..dcd7ac35deba 100644
@@ -7,8 +7,9 @@
 #include <assert.h>
 #include <linux/mman.h>
 #include <sys/mman.h>
+#include <mm/hugepage_settings.h>
+
 #include "kselftest.h"
-#include "hugepage_settings.h"
 #include "uffd-common.h"
 
 static int pagemap_fd;
tools/testing/selftests/mm/va_high_addr_switch.c +1 -1
index e24d7ba00b44..07fcb03316cc 100644
@@ -8,10 +8,10 @@
 #include <stdio.h>
 #include <sys/mman.h>
 #include <string.h>
+#include <mm/hugepage_settings.h>
 
 #include "vm_util.h"
 #include "kselftest.h"
-#include "hugepage_settings.h"
 
 /*
  * The hint addr value is used to allocate addresses
Signature · 1 line
2.39.5
maintainer
#8 lore raw

Re: [PATCH v3 2/4] tools/lib/mm: move hugepage_settings out of selftests

On Thu, 21 May 2026 16:47:59 +0530, Sarthak Sharma <[email protected]> wrote:

Hi Sarthak,

Quoted 9 lines
tools/lib/mm/hugepage_settings.c renamed from tools/testing/selftests/mm/hugepage_settings.c +0 -0
similarity index 98%
rename from tools/testing/selftests/mm/hugepage_settings.c
rename to tools/lib/mm/hugepage_settings.c
index 5e947abb7425..b08b27776fc5 100644

tools/lib/mm/hugepage_settings.c int detect_hugetlb_page_sizes(unsigned long sizes[], int max) · line 387

Quoted 5 lines
tools/lib/mm/hugepage_settings.c renamed from tools/testing/selftests/mm/hugepage_settings.c +0 -2
@@ -383,8 +387,6 @@ int detect_hugetlb_page_sizes(unsigned long sizes[], int max)
 		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);

I believe this message is useful for debugging.

tools/lib/mm/hugepage_settings.c unsigned long hugetlb_setup(unsigned long nr, unsigned long sizes[], · line 506

Quoted 5 lines
tools/lib/mm/hugepage_settings.c renamed from tools/testing/selftests/mm/hugepage_settings.c +0 -1
@@ -503,7 +505,6 @@ unsigned long hugetlb_setup(unsigned long nr, unsigned long sizes[],
 		return 0;
 
 	if (nr_enabled > max) {
-		ksft_print_msg("detected %d huge page sizes, will only test %d\n", nr_enabled, max);

And this one as well.

tools/testing/selftests/mm/compaction_test.c line 18

Quoted 10 lines
tools/testing/selftests/mm/compaction_test.c +1 -0
index de0633f9a7e5..7c58506c0aa7 100644
@@ -15,9 +15,9 @@
 #include <errno.h>
 #include <unistd.h>
 #include <string.h>
+#include <mm/hugepage_settings.h>

As with file_utils.h this can also go to vm_utils.h IMHO.

Signature · 2 lines
Sincerely yours,
Mike.
#9 lore raw

Re: [PATCH v3 2/4] tools/lib/mm: move hugepage_settings out of selftests

Hi Mike!

Quoted 5 lines · Mike Rapoport

On Thu, 21 May 2026 16:47:59 +0530, Sarthak Sharma <[email protected]> wrote:

Hi Sarthak,

Quoted 1 line
Quoted 15 lines

tools/lib/mm/hugepage_settings.c int detect_hugetlb_page_sizes(unsigned long sizes[], int max) · line 387

Quoted 13 lines
tools/lib/mm/hugepage_settings.c renamed from tools/testing/selftests/mm/hugepage_settings.c +0 -2
similarity index 98%
rename from tools/testing/selftests/mm/hugepage_settings.c
rename to tools/lib/mm/hugepage_settings.c
index 5e947abb7425..b08b27776fc5 100644
@@ -383,8 +387,6 @@ int detect_hugetlb_page_sizes(unsigned long sizes[], int max)
 		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);

I believe this message is useful for debugging.

I removed this because hugepage_settings.c is now moving out of selftests, and converting the ksft_print_msg() calls to plain printf() would break TAP output for selftests.

I can add these diagnostic lines back, but since this is now a shared helper, in order to preserve TAP compatibility, I'll have to do a fprintf(stderr, "# ..."). I feel this would look a bit odd from a non-selftest invocation though.

Another option is to add a logging hook so selftests can wire this to ksft_print_msg() while non-selftest users can use normal stderr, but that feels too much for 2 print statements and would extend the scope of this series.

Please let me know if you have a preference here.

Quoted 10 lines

tools/lib/mm/hugepage_settings.c unsigned long hugetlb_setup(unsigned long nr, unsigned long sizes[], · line 506

Quoted 6 lines
tools/lib/mm/hugepage_settings.c renamed from tools/testing/selftests/mm/hugepage_settings.c +0 -1
@@ -503,7 +505,6 @@ unsigned long hugetlb_setup(unsigned long nr, unsigned long sizes[],
 		return 0;
 
 	if (nr_enabled > max) {
-		ksft_print_msg("detected %d huge page sizes, will only test %d\n", nr_enabled, max);

And this one as well.

Quoted 1 line
Quoted 11 lines

tools/testing/selftests/mm/compaction_test.c line 18

Quoted 9 lines
tools/testing/selftests/mm/compaction_test.c +1 -0
index de0633f9a7e5..7c58506c0aa7 100644
@@ -15,9 +15,9 @@
 #include <errno.h>
 #include <unistd.h>
 #include <string.h>
+#include <mm/hugepage_settings.h>

As with file_utils.h this can also go to vm_utils.h IMHO.

Yeah I can do this for files that already include vm_util.h.

maintainer
#10 lore raw

Re: [PATCH v3 2/4] tools/lib/mm: move hugepage_settings out of selftests

Quoted 24 lines · Sarthak Sharma

Hi Mike!

Quoted 5 lines · Mike Rapoport

On Thu, 21 May 2026 16:47:59 +0530, Sarthak Sharma <[email protected]> wrote:

Hi Sarthak,

Quoted 1 line
Quoted 15 lines

tools/lib/mm/hugepage_settings.c int detect_hugetlb_page_sizes(unsigned long sizes[], int max) · line 387

Quoted 13 lines
tools/lib/mm/hugepage_settings.c renamed from tools/testing/selftests/mm/hugepage_settings.c +0 -2
similarity index 98%
rename from tools/testing/selftests/mm/hugepage_settings.c
rename to tools/lib/mm/hugepage_settings.c
index 5e947abb7425..b08b27776fc5 100644
@@ -383,8 +387,6 @@ int detect_hugetlb_page_sizes(unsigned long sizes[], int max)
 		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);

I believe this message is useful for debugging.

Quoted 8 lines

I removed this because hugepage_settings.c is now moving out of selftests, and converting the ksft_print_msg() calls to plain printf() would break TAP output for selftests.

I can add these diagnostic lines back, but since this is now a shared helper, in order to preserve TAP compatibility, I'll have to do a fprintf(stderr, "# ..."). I feel this would look a bit odd from a non-selftest invocation though.

I think "# ... " is not that bad to begin with :)

Quoted 6 lines

Another option is to add a logging hook so selftests can wire this to ksft_print_msg() while non-selftest users can use normal stderr, but that feels too much for 2 print statements and would extend the scope of this series.

Please let me know if you have a preference here.

Signature · 2 lines
Sincerely yours,
Mike.
#11 lore raw

PATCH v3 3/4 tools/mm: add a standalone GUP microbenchmark

In reply to Sarthak Sharma

Add a command-line tool for benchmarking get_user_pages fast-path (GUP_FAST), pin_user_pages fast-path (PIN_FAST), and pin_user_pages longterm (PIN_LONGTERM) via the CONFIG_GUP_TEST debugfs interface.

When invoked without arguments, gup_bench runs the same matrix of configurations as run_gup_matrix() in run_vmtests.sh: all three GUP commands across read/write, private/shared mappings, and a range of page counts, with THP on/off for regular mappings and hugetlb for huge page mappings.

This tool is a mix of reused and new logic. The mapping/setup path comes from selftests/mm/gup_test.c, while the default benchmark matrix matches run_gup_matrix() in run_vmtests.sh. The standalone CLI and tools/mm integration are added here and the HugeTLB setup code is shared via tools/lib/mm.

Add gup_bench to BUILD_TARGETS and INSTALL_TARGETS in tools/mm/Makefile, link it against the shared tools/lib/mm hugepage helpers, and ignore the resulting binary in tools/mm/.gitignore. While here, also add the missing thp_swap_allocator_test entry to .gitignore.

Add tools/mm/gup_bench.c to the GUP entry in MAINTAINERS.

---
 MAINTAINERS          |   1 +
 tools/mm/.gitignore  |   2 +
 tools/mm/Makefile    |  10 +-
 tools/mm/gup_bench.c | 390 +++++++++++++++++++++++++++++++++++++++++++
 4 files changed, 400 insertions(+), 3 deletions(-)
 create mode 100644 tools/mm/gup_bench.c
MAINTAINERS +1 -0
index 7887a3263373..0dbb90247a75 100644
@@ -16839,6 +16839,7 @@ T:	git git://git.kernel.org/pub/scm/linux/kernel/git/akpm/mm
 F:	mm/gup.c
 F:	mm/gup_test.c
 F:	mm/gup_test.h
+F:	tools/mm/gup_bench.c
 F:	tools/testing/selftests/mm/gup_longterm.c
 F:	tools/testing/selftests/mm/gup_test.c
 
tools/mm/.gitignore +2 -0
index 922879f93fc8..154d740be02e 100644
@@ -2,3 +2,5 @@
 slabinfo
 page-types
 page_owner_sort
+thp_swap_allocator_test
+gup_bench
tools/mm/Makefile +7 -3
index f5725b5c23aa..d82cc8c43ee0 100644
@@ -3,13 +3,14 @@
 #
 include ../scripts/Makefile.include
 
-BUILD_TARGETS=page-types slabinfo page_owner_sort thp_swap_allocator_test
+BUILD_TARGETS=page-types slabinfo page_owner_sort thp_swap_allocator_test gup_bench
 INSTALL_TARGETS = $(BUILD_TARGETS) thpmaps
 
 LIB_DIR = ../lib/api
 LIBS = $(LIB_DIR)/libapi.a
+GUP_BENCH_OBJS = gup_bench.c ../lib/mm/hugepage_settings.c ../lib/mm/file_utils.c
 
-CFLAGS += -Wall -Wextra -I../lib/ -pthread
+CFLAGS += -Wall -Wextra -I../lib/ -I../.. -pthread
 LDFLAGS += $(LIBS) -pthread
 
 all: $(BUILD_TARGETS)
@@ -22,8 +23,11 @@ $(LIBS):
 %: %.c
 	$(CC) $(CFLAGS) -o $@ $< $(LDFLAGS)
 
+gup_bench: $(GUP_BENCH_OBJS) $(LIBS)
+	$(CC) $(CFLAGS) -o $@ $(GUP_BENCH_OBJS) $(LDFLAGS)
+
 clean:
-	$(RM) page-types slabinfo page_owner_sort thp_swap_allocator_test
+	$(RM) page-types slabinfo page_owner_sort thp_swap_allocator_test gup_bench
 	make -C $(LIB_DIR) clean
 
 sbindir ?= /usr/sbin
tools/mm/gup_bench.c new file +390 -0

390 lines not on this page · raw message

Signature · 1 line
2.39.5
#12 lore raw

PATCH v3 4/4 selftests/mm: rewrite gup_test as a standalone harness-based selftest

In reply to Sarthak Sharma

Rewrite gup_test.c using kselftest_harness.h. The new test covers 12 mapping configurations: THP on, THP off and hugetlb, each across private/shared and read/write variants. It runs seven test cases per
variant: get_user_pages, get_user_pages_fast, pin_user_pages, pin_user_pages_fast, pin_user_pages_longterm, and DUMP_USER_PAGES_TEST via both get and pin.

Each test case sweeps four nr_pages_per_call values: 1, 512, 123, and all pages. This preserves the old run_gup_matrix() sweep: 12 mapping combinations x 5 GUP/PUP operations x 4 batch sizes = 240 ioctl sweeps. It also expands DUMP_USER_PAGES_TEST coverage from one standalone invocation to 12 variants x 2 dump modes x 4 batch sizes = 96 additional sweeps.

Preserve the old sparse dump coverage from run_vmtests.sh with one standalone test that exercises DUMP_USER_PAGES_TEST with page indices 0, 19 and 0x1000. This brings the total to 85 TAP-reported cases (12 x 7 + 1) and 337 ioctl sweeps (240 + 96 + 1).

On a Radxa Orion O6 board, ./gup_test completes in 5.39s on average over 10 runs (range: 5.33s - 5.48s).

Update run_vmtests.sh: remove run_gup_matrix() and the multiple flagged invocations of gup_test, replacing them with a single unconditional invocation. Benchmark functionality is handled by tools/mm/gup_bench introduced in the previous patch.

Update Documentation/core-api/pin_user_pages.rst to reflect the new harness-based gup_test interface rather than command-line flag invocations.

---
 Documentation/core-api/pin_user_pages.rst |  12 +-
 tools/testing/selftests/mm/gup_test.c     | 584 ++++++++++++++--------
 tools/testing/selftests/mm/run_vmtests.sh |  37 +-
 3 files changed, 376 insertions(+), 257 deletions(-)
Documentation/core-api/pin_user_pages.rst +9 -3
index c16ca163b55e..ea722adf22cc 100644
@@ -230,10 +230,16 @@ This file::
 
  tools/testing/selftests/mm/gup_test.c
 
-has the following new calls to exercise the new pin*() wrapper functions:
+contains the following test cases to exercise pin_user_pages*():
 
-* PIN_FAST_BENCHMARK (./gup_test -a)
-* PIN_BASIC_TEST (./gup_test -b)
+* pin_user_pages via PIN_BASIC_TEST
+* pin_user_pages_fast via PIN_FAST_BENCHMARK
+* pin_user_pages_longterm via PIN_LONGTERM_BENCHMARK
+
+Run with::
+
+  make -C tools/testing/selftests/mm
+  ./tools/testing/selftests/mm/gup_test
 
 You can monitor how many total dma-pinned pages have been acquired and released
 since the system was booted, via two new /proc/vmstat entries: ::
tools/testing/selftests/mm/gup_test.c +366 -218

634 lines not on this page · raw message

tools/testing/selftests/mm/run_vmtests.sh +1 -36
index 043aa3ed2596..65a4ef0f3748 100755
@@ -130,30 +130,6 @@ test_selected() {
 	fi
 }
 
-run_gup_matrix() {
-    # -t: thp=on, -T: thp=off, -H: hugetlb=on
-    local hugetlb_mb=256
-
-    for huge in -t -T "-H -m $hugetlb_mb"; do
-        # -u: gup-fast, -U: gup-basic, -a: pin-fast, -b: pin-basic, -L: pin-longterm
-        for test_cmd in -u -U -a -b -L; do
-            # -w: write=1, -W: write=0
-            for write in -w -W; do
-                # -S: shared
-                for share in -S " "; do
-                    # -n: How many pages to fetch together?  512 is special
-                    # because it's default thp size (or 2M on x86), 123 to
-                    # just test partial gup when hit a huge in whatever form
-                    for num in "-n 1" "-n 512" "-n 123" "-n -1"; do
-                        CATEGORY="gup_test" run_test ./gup_test \
-                                $huge $test_cmd $write $share $num
-                    done
-                done
-            done
-        done
-    done
-}
-
 # filter 64bit architectures
 ARCH64STR="arm64 mips64 parisc64 ppc64 ppc64le riscv64 s390x sparc64 x86_64"
 if [ -z "$ARCH" ]; then
@@ -239,18 +215,7 @@ fi
 
 CATEGORY="mmap" run_test ./map_fixed_noreplace
 
-if $RUN_ALL; then
-    run_gup_matrix
-else
-    # get_user_pages_fast() benchmark
-    CATEGORY="gup_test" run_test ./gup_test -u -n 1
-    CATEGORY="gup_test" run_test ./gup_test -u -n -1
-    # pin_user_pages_fast() benchmark
-    CATEGORY="gup_test" run_test ./gup_test -a -n 1
-    CATEGORY="gup_test" run_test ./gup_test -a -n -1
-fi
-# Dump pages 0, 19, and 4096, using pin_user_pages:
-CATEGORY="gup_test" run_test ./gup_test -ct -F 0x1 0 19 0x1000
+CATEGORY="gup_test" run_test ./gup_test
 CATEGORY="gup_test" run_test ./gup_longterm
 
 CATEGORY="userfaultfd" run_test ./uffd-unit-tests
Signature · 1 line
2.39.5
maintainer
#13 lore raw
In reply to Sarthak Sharma
Quoted 8 lines · On Thu, 21 May 2026 16:47:57 +0530 Sarthak Sharma <[email protected]>…

gup_test.c currently serves two distinct purposes: microbenchmarking (GUP_FAST_BENCHMARK, PIN_FAST_BENCHMARK, PIN_LONGTERM_BENCHMARK) and functional correctness testing (GUP_BASIC_TEST, PIN_BASIC_TEST, DUMP_USER_PAGES_TEST). Mixing these in a single binary means functional tests cannot be run or reported individually and run_vmtests.sh must invoke the binary multiple times with different flag combinations to cover all configurations.

Quoted 8 lines

This patch series separates the two concerns: tools/mm/gup_bench for benchmarking and tools/testing/selftests/mm/gup_test for functional testing. To avoid duplicating HugeTLB and related file helpers, the series first moves the common helper code to tools/lib/mm/ so it can be shared by both selftests and tools/mm.

...

Thanks.

Quoted 4 lines

These patches apply on top of mm/mm-new.

Changes in v3:
- Address v2 feedback from Sashiko

Sashiko has thoughts on v3:

	https://sashiko.dev/#/patchset/[email protected]

Probably that's incorrect/ignored material which also appeared against the v2 series. But sometimes it finds new things so please scan the output.

I'll hold off on this series for now, shall see what reviewers have to say.