Skip to contentSkip to conversation

selftests/mm: move the HMM migration benchmark to tools/mm

PATCH New Balbir Singh 1 participant 1 message Nov 27

Subsystems (4)
lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 selftests/mm: move the HMM migration benchmark to tools/mm

The HMM selftest runs benchmark_thp_migration on every invocation. The benchmark repeatedly migrates buffers in both directions to measure latency and throughput with and without THP. Run it on demand so the regular suite spends its time checking correctness.

Move the benchmark to tools/mm alongside gup_bench. Share the device fixture, buffer helpers and migration ioctls with hmm-tests. To run it:

make -C tools/mm hmm-bench
sudo modprobe test_hmm
sudo tools/mm/hmm-bench

Use a size_t index to match the word count when initializing the buffer. Have make clean remove $(BUILD_TARGETS) so it picks up hmm-bench. Like gup_bench, hmm-bench is installed in /usr/sbin by default.

The tools/mm build enables -Wextra, exposing a signedness warning in detect_huge_zeropage(). Store the pread() result in ssize_t and cast it to size_t for the buffer size check after verifying it is positive.

Drop the 128-PMD buffer to bound the amount of memory migrated in one measurement. With 2 MiB PMDs, the largest buffer falls from 256 MiB to 16 MiB. The remaining sizes still cover buffers smaller than a PMD, a single PMD and multiple PMDs, with and without MADV_HUGEPAGE.

Call fflush(stdout) after printing the results for each buffer size. The kselftest harness ends the test child with _exit(0), so timing results still buffered in stdout can be lost when output is redirected to a file or pipe.

Cc Jason Gunthorpe <[email protected]>
Cc Leon Romanovsky <[email protected]>
Cc Andrew Morton <[email protected]>
Cc David Hildenbrand <[email protected]>
Cc "Liam R. Howlett" <[email protected]>
Cc Vlastimil Babka <[email protected]>
Cc Mike Rapoport <[email protected]>
Cc Michal Hocko <[email protected]>
Cc Shuah Khan <[email protected]>

---
Changes in v2:
- Move hmm-bench to tools/mm alongside gup_bench, as David suggested.
- Fix the signedness warning in detect_huge_zeropage() exposed by -Wextra.

On mm-everything-2026-11-24-10-33, hmm.hmm_device_private.migrate_fault fails with -ENOENT on the second migration to the device. I reproduced this in QEMU with the selftest binaries built before and after this patch. In the same guest, both binaries pass with test_hmm built from the parent of 5cbbd205947d ("lib/test_hmm: invalidate all mirrors on device-private migration") and both fail with test_hmm from that commit.

 MAINTAINERS                             |   1 +
 tools/mm/.gitignore                     |   1 +
 tools/mm/Makefile                       |  15 +-
 tools/mm/hmm-bench.c                    | 192 ++++++++++++
 tools/testing/selftests/mm/Makefile     |   1 +
 tools/testing/selftests/mm/hmm-common.h | 202 +++++++++++++
 tools/testing/selftests/mm/hmm-tests.c  | 384 +-----------------------
 tools/testing/selftests/mm/vm_util.c    |   4 +-
 8 files changed, 412 insertions(+), 388 deletions(-)
 create mode 100644 tools/mm/hmm-bench.c
 create mode 100644 tools/testing/selftests/mm/hmm-common.h
MAINTAINERS +1 -0
index e02fef18972d..11666eb9565a 100644
@@ -12199,6 +12199,7 @@ F:	Documentation/mm/hmm.rst
 F:	include/linux/hmm*
 F:	lib/test_hmm*
 F:	mm/hmm*
+F:	tools/mm/hmm-bench.c
 F:	tools/testing/selftests/mm/*hmm*
 
 HONEYWELL ABP2030PA PRESSURE SENSOR SERIES IIO DRIVER
tools/mm/.gitignore +1 -0
index 2d08f713b664..97e77de81f29 100644
@@ -5,4 +5,5 @@ page_owner_sort
 page_owner_filter
 thp_swap_allocator_test
 gup_bench
+hmm-bench
 memory-failure/hwpoison-panic
tools/mm/Makefile +12 -3
index c9a5bf234070..1f9c16e15c5a 100644
@@ -4,13 +4,19 @@
 include ../scripts/Makefile.include
 
 BUILD_TARGETS=page-types slabinfo page_owner_sort page_owner_filter
-BUILD_TARGETS += thp_swap_allocator_test gup_bench
+BUILD_TARGETS += thp_swap_allocator_test gup_bench hmm-bench
 BUILD_TARGETS+=memory-failure/hwpoison-panic
 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
+SELFTESTS = ../testing/selftests
+HMM_BENCH_SRCS = hmm-bench.c $(SELFTESTS)/mm/vm_util.c \
+		 ../lib/mm/hugepage_settings.c ../lib/mm/file_utils.c
+HMM_BENCH_HDRS = $(SELFTESTS)/mm/hmm-common.h $(SELFTESTS)/mm/vm_util.h \
+		 $(SELFTESTS)/kselftest_harness.h $(SELFTESTS)/kselftest.h \
+		 ../../lib/test_hmm_uapi.h ../lib/mm/hugepage_settings.h ../lib/mm/file_utils.h
 
 CFLAGS += -Wall -Wextra -I../lib/ -I../.. -pthread
 LDFLAGS += $(LIBS) -pthread
@@ -28,9 +34,12 @@ $(LIBS):
 gup_bench: $(GUP_BENCH_OBJS) $(LIBS)
 	$(CC) $(CFLAGS) -o $@ $(GUP_BENCH_OBJS) $(LDFLAGS)
 
+hmm-bench: $(HMM_BENCH_SRCS) $(HMM_BENCH_HDRS)
+	$(CC) $(CFLAGS) -D_GNU_SOURCE -I$(SELFTESTS) -isystem ../include/uapi \
+		-o $@ $(HMM_BENCH_SRCS) $(LDFLAGS)
+
 clean:
-	$(RM) page-types slabinfo page_owner_sort page_owner_filter thp_swap_allocator_test gup_bench
-	$(RM) memory-failure/hwpoison-panic
+	$(RM) $(BUILD_TARGETS)
 	make -C $(LIB_DIR) clean
 
 sbindir ?= /usr/sbin
tools/mm/hmm-bench.c new file +192 -0

192 lines not on this page · raw message

tools/testing/selftests/mm/Makefile +1 -0
index 675ec57d47de..c129ac2c635a 100644
@@ -6,6 +6,7 @@
 # If you do not YOUR TESTS WILL NOT RUN IN THE CI.
 
 LOCAL_HDRS += $(selfdir)/mm/local_config.h_gen $(top_srcdir)/mm/gup_test.h
+LOCAL_HDRS += $(selfdir)/mm/hmm-common.h
 LOCAL_HDRS += $(selfdir)/mm/mseal_helpers.h
 
 include local_config.mk
tools/testing/selftests/mm/hmm-common.h new file +202 -0

202 lines not on this page · raw message

tools/testing/selftests/mm/hmm-tests.c +1 -383

441 lines not on this page · raw message

tools/testing/selftests/mm/vm_util.c +2 -2
index b5485326fa0b..9643058472fb 100644
@@ -944,13 +944,13 @@ bool detect_huge_zeropage(void)
 		      O_RDONLY);
 	bool enabled = 0;
 	char buf[15];
-	int ret;
+	ssize_t ret;
 
 	if (fd < 0)
 		return 0;
 
 	ret = pread(fd, buf, sizeof(buf), 0);
-	if (ret > 0 && ret < sizeof(buf)) {
+	if (ret > 0 && (size_t)ret < sizeof(buf)) {
 		buf[ret] = 0;
 
 		if (strtoul(buf, NULL, 10) == 1)
Signature · 1 line
2.56.1