Linux-mm Archive on lore.kernel.org
 help / color / mirror / Atom feed
From: Balbir Singh <balbirs@nvidia.com>
To: akpm@linux-foundation.org, linux-mm@kvack.org
Cc: linux-kernel@vger.kernel.org, Balbir Singh <balbirs@nvidia.com>,
	Jason Gunthorpe <jgg@ziepe.ca>, Leon Romanovsky <leon@kernel.org>,
	David Hildenbrand <david@kernel.org>,
	"Liam R. Howlett" <liam@infradead.org>,
	Vlastimil Babka <vbabka@kernel.org>,
	Mike Rapoport <rppt@kernel.org>, Michal Hocko <mhocko@suse.com>,
	Shuah Khan <shuah@kernel.org>
Subject: [PATCH v2] selftests/mm: move the HMM migration benchmark to tools/mm
Date: Fri, 27 Nov 2026 12:44:30 +1100	[thread overview]
Message-ID: <20261127014430.2043154-1-balbirs@nvidia.com> (raw)

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.

Link: https://lore.kernel.org/r/20261124215730.0f6adc9ad36c7abc623a1dde@linux-foundation.org
Link: https://lore.kernel.org/r/fba23668-add0-4787-b027-bfba1f66e090@kernel.org
Cc: Jason Gunthorpe <jgg@ziepe.ca>
Cc: Leon Romanovsky <leon@kernel.org>
Cc: Andrew Morton <akpm@linux-foundation.org>
Cc: David Hildenbrand <david@kernel.org>
Cc: "Liam R. Howlett" <liam@infradead.org>
Cc: Vlastimil Babka <vbabka@kernel.org>
Cc: Mike Rapoport <rppt@kernel.org>
Cc: Michal Hocko <mhocko@suse.com>
Cc: Shuah Khan <shuah@kernel.org>
Signed-off-by: Balbir Singh <balbirs@nvidia.com>
---
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

diff --git a/MAINTAINERS b/MAINTAINERS
index e02fef18972d..11666eb9565a 100644
--- a/MAINTAINERS
+++ b/MAINTAINERS
@@ -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
diff --git a/tools/mm/.gitignore b/tools/mm/.gitignore
index 2d08f713b664..97e77de81f29 100644
--- a/tools/mm/.gitignore
+++ b/tools/mm/.gitignore
@@ -5,4 +5,5 @@ page_owner_sort
 page_owner_filter
 thp_swap_allocator_test
 gup_bench
+hmm-bench
 memory-failure/hwpoison-panic
diff --git a/tools/mm/Makefile b/tools/mm/Makefile
index c9a5bf234070..1f9c16e15c5a 100644
--- a/tools/mm/Makefile
+++ b/tools/mm/Makefile
@@ -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
diff --git a/tools/mm/hmm-bench.c b/tools/mm/hmm-bench.c
new file mode 100644
index 000000000000..ee23d8875e8c
--- /dev/null
+++ b/tools/mm/hmm-bench.c
@@ -0,0 +1,192 @@
+// SPDX-License-Identifier: GPL-2.0
+#include <limits.h>
+#include <sys/time.h>
+#include <mm/hmm-common.h>
+
+struct benchmark_results {
+	double sys_to_dev_time;
+	double dev_to_sys_time;
+	double throughput_s2d;
+	double throughput_d2s;
+};
+
+static double get_time_ms(void)
+{
+	struct timeval tv;
+
+	gettimeofday(&tv, NULL);
+	return (tv.tv_sec * 1000.0) + (tv.tv_usec / 1000.0);
+}
+
+static void print_benchmark_results(const char *test_name, size_t buffer_size,
+				     struct benchmark_results *thp,
+				     struct benchmark_results *regular)
+{
+	double s2d_improvement = ((regular->sys_to_dev_time - thp->sys_to_dev_time) /
+				 regular->sys_to_dev_time) * 100.0;
+	double d2s_improvement = ((regular->dev_to_sys_time - thp->dev_to_sys_time) /
+				 regular->dev_to_sys_time) * 100.0;
+	double throughput_s2d_improvement = ((thp->throughput_s2d - regular->throughput_s2d) /
+					    regular->throughput_s2d) * 100.0;
+	double throughput_d2s_improvement = ((thp->throughput_d2s - regular->throughput_d2s) /
+					    regular->throughput_d2s) * 100.0;
+
+	printf("\n=== %s (%.1f MB) ===\n", test_name, buffer_size / (1024.0 * 1024.0));
+	printf("                     | With THP        | Without THP     | Improvement\n");
+	printf("---------------------------------------------------------------------\n");
+	printf("Sys->Dev Migration   | %.3f ms        | %.3f ms        | %.1f%%\n",
+	       thp->sys_to_dev_time, regular->sys_to_dev_time, s2d_improvement);
+	printf("Dev->Sys Migration   | %.3f ms        | %.3f ms        | %.1f%%\n",
+	       thp->dev_to_sys_time, regular->dev_to_sys_time, d2s_improvement);
+	printf("S->D Throughput      | %.2f GB/s      | %.2f GB/s      | %.1f%%\n",
+	       thp->throughput_s2d, regular->throughput_s2d, throughput_s2d_improvement);
+	printf("D->S Throughput      | %.2f GB/s      | %.2f GB/s      | %.1f%%\n",
+	       thp->throughput_d2s, regular->throughput_d2s, throughput_d2s_improvement);
+	fflush(stdout);
+}
+
+/*
+ * Run a single migration benchmark
+ * fd: file descriptor for hmm device
+ * use_thp: whether to use THP
+ * buffer_size: size of buffer to allocate
+ * iterations: number of iterations
+ * results: where to store results
+ */
+static inline int run_migration_benchmark(int fd, int use_thp, size_t buffer_size,
+					   int iterations, struct benchmark_results *results)
+{
+	struct hmm_buffer *buffer;
+	unsigned long npages = buffer_size / sysconf(_SC_PAGESIZE);
+	double start, end;
+	double s2d_total = 0, d2s_total = 0;
+	size_t word;
+	int ret, i;
+	int *ptr;
+
+	buffer = hmm_buffer_alloc(buffer_size, buffer_size,
+				  PROT_READ | PROT_WRITE,
+				  MAP_PRIVATE | MAP_ANONYMOUS, -1);
+	if (!buffer) {
+		ret = -1;
+		goto cleanup;
+	}
+	memset(buffer->mirror, 0xFF, buffer_size);
+
+	/* Apply THP hint if requested */
+	if (use_thp)
+		ret = madvise(buffer->ptr, buffer_size, MADV_HUGEPAGE);
+	else
+		ret = madvise(buffer->ptr, buffer_size, MADV_NOHUGEPAGE);
+
+	if (ret)
+		goto cleanup;
+
+	/* Initialize memory to make sure pages are allocated */
+	ptr = (int *)buffer->ptr;
+	for (word = 0; word < buffer_size / sizeof(*ptr); word++)
+		ptr[word] = word & 0xFF;
+
+	/* Warmup iteration */
+	ret = hmm_migrate_sys_to_dev(fd, buffer, npages);
+	if (ret)
+		goto cleanup;
+
+	ret = hmm_migrate_dev_to_sys(fd, buffer, npages);
+	if (ret)
+		goto cleanup;
+
+	/* Benchmark iterations */
+	for (i = 0; i < iterations; i++) {
+		/* System to device migration */
+		start = get_time_ms();
+
+		ret = hmm_migrate_sys_to_dev(fd, buffer, npages);
+		if (ret)
+			goto cleanup;
+
+		end = get_time_ms();
+		s2d_total += (end - start);
+
+		/* Device to system migration */
+		start = get_time_ms();
+
+		ret = hmm_migrate_dev_to_sys(fd, buffer, npages);
+		if (ret)
+			goto cleanup;
+
+		end = get_time_ms();
+		d2s_total += (end - start);
+	}
+
+	/* Calculate average times and throughput */
+	results->sys_to_dev_time = s2d_total / iterations;
+	results->dev_to_sys_time = d2s_total / iterations;
+	results->throughput_s2d = (buffer_size / (1024.0 * 1024.0 * 1024.0)) /
+				 (results->sys_to_dev_time / 1000.0);
+	results->throughput_d2s = (buffer_size / (1024.0 * 1024.0 * 1024.0)) /
+				 (results->dev_to_sys_time / 1000.0);
+
+cleanup:
+	hmm_buffer_free(buffer);
+	return ret;
+}
+
+/*
+ * Benchmark THP migration with different buffer sizes
+ */
+TEST_F_TIMEOUT(hmm, benchmark_thp_migration, 120)
+{
+	struct benchmark_results thp_results, regular_results;
+	size_t thp_size = pmd_psize();
+	int iterations = 5;
+
+	if (!thp_size)
+		thp_size = TWOMEG;
+
+	printf("\nHMM THP Migration Benchmark\n");
+	printf("---------------------------\n");
+	printf("System page size: %ld bytes\n", sysconf(_SC_PAGESIZE));
+
+	/* Test different buffer sizes */
+	size_t test_sizes[] = {
+		thp_size / 4,      /* quarter THP */
+		thp_size / 2,      /* half THP */
+		thp_size,          /* single THP */
+		thp_size * 2,      /* two THPs */
+		thp_size * 4,      /* four THPs */
+		thp_size * 8,      /* eight THPs */
+	};
+
+	static const char *const test_names[] = {
+		"Small Buffer",
+		"Half THP Size",
+		"Single THP Size",
+		"Two THP Size",
+		"Four THP Size",
+		"Eight THP Size",
+	};
+
+	int num_tests = ARRAY_SIZE(test_sizes);
+
+	/* Run all tests */
+	for (int i = 0; i < num_tests; i++) {
+		/* Skip test sizes exceeding INT_MAX to avoid overflow */
+		if (test_sizes[i] > INT_MAX)
+			break;
+
+		/* Test with THP */
+		ASSERT_EQ(run_migration_benchmark(self->fd, 1, test_sizes[i],
+					iterations, &thp_results), 0);
+
+		/* Test without THP */
+		ASSERT_EQ(run_migration_benchmark(self->fd, 0, test_sizes[i],
+					iterations, &regular_results), 0);
+
+		/* Print results */
+		print_benchmark_results(test_names[i], test_sizes[i],
+					&thp_results, &regular_results);
+	}
+}
+
+TEST_HARNESS_MAIN
diff --git a/tools/testing/selftests/mm/Makefile b/tools/testing/selftests/mm/Makefile
index 675ec57d47de..c129ac2c635a 100644
--- a/tools/testing/selftests/mm/Makefile
+++ b/tools/testing/selftests/mm/Makefile
@@ -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
diff --git a/tools/testing/selftests/mm/hmm-common.h b/tools/testing/selftests/mm/hmm-common.h
new file mode 100644
index 000000000000..838d496b35fb
--- /dev/null
+++ b/tools/testing/selftests/mm/hmm-common.h
@@ -0,0 +1,202 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+#ifndef HMM_COMMON_H
+#define HMM_COMMON_H
+
+#include <errno.h>
+#include <fcntl.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <stdint.h>
+#include <unistd.h>
+#include <strings.h>
+#include <sys/ioctl.h>
+#include <sys/mman.h>
+#include "kselftest_harness.h"
+#include <mm/vm_util.h>
+
+/*
+ * This is a private UAPI to the kernel test module so it isn't exported
+ * in the usual include/uapi/... directory.
+ */
+#include <lib/test_hmm_uapi.h>
+
+struct hmm_buffer {
+	void		*ptr;
+	void		*mirror;
+	unsigned long	size;
+	int		fd;
+	uint64_t	cpages;
+	uint64_t	faults;
+};
+
+enum {
+	HMM_PRIVATE_DEVICE_ONE,
+	HMM_PRIVATE_DEVICE_TWO,
+	HMM_COHERENCE_DEVICE_ONE,
+	HMM_COHERENCE_DEVICE_TWO,
+};
+
+#define TWOMEG		(1 << 21)
+#define HMM_PATH_MAX    64
+
+FIXTURE(hmm)
+{
+	int		fd;
+	unsigned int	page_size;
+	unsigned int	page_shift;
+};
+
+FIXTURE_VARIANT(hmm)
+{
+	int     device_number;
+};
+
+FIXTURE_VARIANT_ADD(hmm, hmm_device_private)
+{
+	.device_number = HMM_PRIVATE_DEVICE_ONE,
+};
+
+FIXTURE_VARIANT_ADD(hmm, hmm_device_coherent)
+{
+	.device_number = HMM_COHERENCE_DEVICE_ONE,
+};
+
+static int hmm_open(int unit)
+{
+	char pathname[HMM_PATH_MAX];
+	int fd;
+
+	snprintf(pathname, sizeof(pathname), "/dev/hmm_dmirror%d", unit);
+	fd = open(pathname, O_RDWR, 0);
+	if (fd < 0)
+		fprintf(stderr, "could not open hmm dmirror driver (%s)\n",
+			pathname);
+	return fd;
+}
+
+static bool hmm_is_coherent_type(int dev_num)
+{
+	return (dev_num >= HMM_COHERENCE_DEVICE_ONE);
+}
+
+FIXTURE_SETUP(hmm)
+{
+	self->page_size = sysconf(_SC_PAGE_SIZE);
+	self->page_shift = ffs(self->page_size) - 1;
+
+	self->fd = hmm_open(variant->device_number);
+	if (self->fd < 0 && hmm_is_coherent_type(variant->device_number))
+		SKIP(return, "DEVICE_COHERENT not available");
+	ASSERT_GE(self->fd, 0);
+}
+
+FIXTURE_TEARDOWN(hmm)
+{
+	int ret = close(self->fd);
+
+	ASSERT_EQ(ret, 0);
+	self->fd = -1;
+}
+
+static int hmm_dmirror_cmd(int fd,
+			   unsigned long request,
+			   struct hmm_buffer *buffer,
+			   unsigned long npages)
+{
+	struct hmm_dmirror_cmd cmd;
+	int ret;
+
+	/* Simulate a device reading system memory. */
+	cmd.addr = ptr_to_u64(buffer->ptr);
+	cmd.ptr = ptr_to_u64(buffer->mirror);
+	cmd.npages = npages;
+
+	for (;;) {
+		ret = ioctl(fd, request, &cmd);
+		if (ret == 0)
+			break;
+		if (errno == EINTR)
+			continue;
+		return -errno;
+	}
+	buffer->cpages = cmd.cpages;
+	buffer->faults = cmd.faults;
+
+	return 0;
+}
+
+static void hmm_buffer_free(struct hmm_buffer *buffer)
+{
+	if (buffer == NULL)
+		return;
+
+	if (buffer->ptr) {
+		munmap(buffer->ptr, buffer->size);
+		buffer->ptr = NULL;
+	}
+	free(buffer->mirror);
+	free(buffer);
+}
+
+/*
+ * Allocate a buffer structure with memory mapping and mirror.
+ *
+ * @mmap_size:   total size of the mmap region (may differ from @mirror_size
+ *               for alignment padding in THP tests).
+ * @mirror_size: size of the mirror data buffer (the actual working set).
+ * @prot:        protection flags for the mmap (e.g. PROT_READ | PROT_WRITE).
+ * @flags:       flags for the mmap (e.g. MAP_PRIVATE, MAP_SHARED,
+ *               MAP_ANONYMOUS, MAP_HUGETLB).
+ * @fd:          file descriptor for the mmap; pass -1 for MAP_ANONYMOUS.
+ *
+ * All internal allocations are checked; returns NULL and cleans up on any
+ * failure. Caller must ASSERT_NE or otherwise check the return value.
+ */
+static struct hmm_buffer *hmm_buffer_alloc(unsigned long mmap_size,
+						unsigned long mirror_size,
+						int prot, int flags,
+						int fd)
+{
+	struct hmm_buffer *buffer;
+
+	buffer = malloc(sizeof(*buffer));
+	if (!buffer) {
+		perror("malloc buffer");
+		return NULL;
+	}
+
+	buffer->fd = fd;
+	buffer->size = mmap_size;
+	buffer->mirror = malloc(mirror_size);
+	if (!buffer->mirror) {
+		perror("malloc mirror");
+		free(buffer);
+		return NULL;
+	}
+
+	buffer->ptr = mmap(NULL, mmap_size, prot, flags, fd, 0);
+	if (buffer->ptr == MAP_FAILED) {
+		perror("mmap");
+		free(buffer->mirror);
+		free(buffer);
+		return NULL;
+	}
+
+	return buffer;
+}
+
+static int hmm_migrate_sys_to_dev(int fd,
+				   struct hmm_buffer *buffer,
+				   unsigned long npages)
+{
+	return hmm_dmirror_cmd(fd, HMM_DMIRROR_MIGRATE_TO_DEV, buffer, npages);
+}
+
+static int hmm_migrate_dev_to_sys(int fd,
+				   struct hmm_buffer *buffer,
+				   unsigned long npages)
+{
+	return hmm_dmirror_cmd(fd, HMM_DMIRROR_MIGRATE_TO_SYS, buffer, npages);
+}
+
+#endif /* HMM_COMMON_H */
diff --git a/tools/testing/selftests/mm/hmm-tests.c b/tools/testing/selftests/mm/hmm-tests.c
index fb46a3be8687..ed7aa96a404a 100644
--- a/tools/testing/selftests/mm/hmm-tests.c
+++ b/tools/testing/selftests/mm/hmm-tests.c
@@ -10,59 +10,22 @@
  * bugs.
  */
 
-#include <errno.h>
-#include <fcntl.h>
-#include <stdio.h>
-#include <stdlib.h>
-#include <stdint.h>
-#include <unistd.h>
-#include <strings.h>
 #include <time.h>
 #include <pthread.h>
-#include <limits.h>
 #include <linux/mman.h>
 #include <sys/types.h>
 #include <sys/stat.h>
-#include <sys/mman.h>
-#include <sys/ioctl.h>
-#include <sys/time.h>
 #include <sys/syscall.h>
 #include <sys/eventfd.h>
 #include <linux/userfaultfd.h>
 #include <poll.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
- * in the usual include/uapi/... directory.
- */
-#include <lib/test_hmm_uapi.h>
 #include <mm/gup_test.h>
-#include <mm/vm_util.h>
-
-struct hmm_buffer {
-	void		*ptr;
-	void		*mirror;
-	unsigned long	size;
-	int		fd;
-	uint64_t	cpages;
-	uint64_t	faults;
-};
-
-enum {
-	HMM_PRIVATE_DEVICE_ONE,
-	HMM_PRIVATE_DEVICE_TWO,
-	HMM_COHERENCE_DEVICE_ONE,
-	HMM_COHERENCE_DEVICE_TWO,
-};
+#include "hmm-common.h"
 
 #define ONEKB		(1 << 10)
 #define ONEMEG		(1 << 20)
-#define TWOMEG		(1 << 21)
 #define HMM_BUFFER_SIZE (1024 << 12)
-#define HMM_PATH_MAX    64
 #define NTIMES		10
 
 /* Just the flags we need, copied from mm.h: */
@@ -77,28 +40,6 @@ enum {
 
 HUGETLB_SETUP_DEFAULT_PAGES(1)
 
-FIXTURE(hmm)
-{
-	int		fd;
-	unsigned int	page_size;
-	unsigned int	page_shift;
-};
-
-FIXTURE_VARIANT(hmm)
-{
-	int     device_number;
-};
-
-FIXTURE_VARIANT_ADD(hmm, hmm_device_private)
-{
-	.device_number = HMM_PRIVATE_DEVICE_ONE,
-};
-
-FIXTURE_VARIANT_ADD(hmm, hmm_device_coherent)
-{
-	.device_number = HMM_COHERENCE_DEVICE_ONE,
-};
-
 FIXTURE(hmm2)
 {
 	int		fd0;
@@ -125,35 +66,6 @@ FIXTURE_VARIANT_ADD(hmm2, hmm2_device_coherent)
 	.device_number1 = HMM_COHERENCE_DEVICE_TWO,
 };
 
-static int hmm_open(int unit)
-{
-	char pathname[HMM_PATH_MAX];
-	int fd;
-
-	snprintf(pathname, sizeof(pathname), "/dev/hmm_dmirror%d", unit);
-	fd = open(pathname, O_RDWR, 0);
-	if (fd < 0)
-		fprintf(stderr, "could not open hmm dmirror driver (%s)\n",
-			pathname);
-	return fd;
-}
-
-static bool hmm_is_coherent_type(int dev_num)
-{
-	return (dev_num >= HMM_COHERENCE_DEVICE_ONE);
-}
-
-FIXTURE_SETUP(hmm)
-{
-	self->page_size = sysconf(_SC_PAGE_SIZE);
-	self->page_shift = ffs(self->page_size) - 1;
-
-	self->fd = hmm_open(variant->device_number);
-	if (self->fd < 0 && hmm_is_coherent_type(variant->device_number))
-		SKIP(return, "DEVICE_COHERENT not available");
-	ASSERT_GE(self->fd, 0);
-}
-
 FIXTURE_SETUP(hmm2)
 {
 	self->page_size = sysconf(_SC_PAGE_SIZE);
@@ -167,14 +79,6 @@ FIXTURE_SETUP(hmm2)
 	ASSERT_GE(self->fd1, 0);
 }
 
-FIXTURE_TEARDOWN(hmm)
-{
-	int ret = close(self->fd);
-
-	ASSERT_EQ(ret, 0);
-	self->fd = -1;
-}
-
 FIXTURE_TEARDOWN(hmm2)
 {
 	int ret = close(self->fd0);
@@ -187,93 +91,6 @@ FIXTURE_TEARDOWN(hmm2)
 	self->fd1 = -1;
 }
 
-static int hmm_dmirror_cmd(int fd,
-			   unsigned long request,
-			   struct hmm_buffer *buffer,
-			   unsigned long npages)
-{
-	struct hmm_dmirror_cmd cmd;
-	int ret;
-
-	/* Simulate a device reading system memory. */
-	cmd.addr = ptr_to_u64(buffer->ptr);
-	cmd.ptr = ptr_to_u64(buffer->mirror);
-	cmd.npages = npages;
-
-	for (;;) {
-		ret = ioctl(fd, request, &cmd);
-		if (ret == 0)
-			break;
-		if (errno == EINTR)
-			continue;
-		return -errno;
-	}
-	buffer->cpages = cmd.cpages;
-	buffer->faults = cmd.faults;
-
-	return 0;
-}
-
-static void hmm_buffer_free(struct hmm_buffer *buffer)
-{
-	if (buffer == NULL)
-		return;
-
-	if (buffer->ptr) {
-		munmap(buffer->ptr, buffer->size);
-		buffer->ptr = NULL;
-	}
-	free(buffer->mirror);
-	free(buffer);
-}
-
-/*
- * Allocate a buffer structure with memory mapping and mirror.
- *
- * @mmap_size:   total size of the mmap region (may differ from @mirror_size
- *               for alignment padding in THP tests).
- * @mirror_size: size of the mirror data buffer (the actual working set).
- * @prot:        protection flags for the mmap (e.g. PROT_READ | PROT_WRITE).
- * @flags:       flags for the mmap (e.g. MAP_PRIVATE, MAP_SHARED,
- *               MAP_ANONYMOUS, MAP_HUGETLB).
- * @fd:          file descriptor for the mmap; pass -1 for MAP_ANONYMOUS.
- *
- * All internal allocations are checked; returns NULL and cleans up on any
- * failure. Caller must ASSERT_NE or otherwise check the return value.
- */
-static struct hmm_buffer *hmm_buffer_alloc(unsigned long mmap_size,
-						unsigned long mirror_size,
-						int prot, int flags,
-						int fd)
-{
-	struct hmm_buffer *buffer;
-
-	buffer = malloc(sizeof(*buffer));
-	if (!buffer) {
-		perror("malloc buffer");
-		return NULL;
-	}
-
-	buffer->fd = fd;
-	buffer->size = mmap_size;
-	buffer->mirror = malloc(mirror_size);
-	if (!buffer->mirror) {
-		perror("malloc mirror");
-		free(buffer);
-		return NULL;
-	}
-
-	buffer->ptr = mmap(NULL, mmap_size, prot, flags, fd, 0);
-	if (buffer->ptr == MAP_FAILED) {
-		perror("mmap");
-		free(buffer->mirror);
-		free(buffer);
-		return NULL;
-	}
-
-	return buffer;
-}
-
 /*
  * Create a temporary file that will be deleted on close.
  */
@@ -326,20 +143,6 @@ static void hmm_nanosleep(unsigned int n)
 	nanosleep(&t, NULL);
 }
 
-static int hmm_migrate_sys_to_dev(int fd,
-				   struct hmm_buffer *buffer,
-				   unsigned long npages)
-{
-	return hmm_dmirror_cmd(fd, HMM_DMIRROR_MIGRATE_TO_DEV, buffer, npages);
-}
-
-static int hmm_migrate_dev_to_sys(int fd,
-				   struct hmm_buffer *buffer,
-				   unsigned long npages)
-{
-	return hmm_dmirror_cmd(fd, HMM_DMIRROR_MIGRATE_TO_SYS, buffer, npages);
-}
-
 /*
  * Simple NULL test of device open/close.
  */
@@ -2475,191 +2278,6 @@ TEST_F(hmm, migrate_anon_huge_zero_err)
 	hmm_buffer_free(buffer);
 }
 
-struct benchmark_results {
-	double sys_to_dev_time;
-	double dev_to_sys_time;
-	double throughput_s2d;
-	double throughput_d2s;
-};
-
-static double get_time_ms(void)
-{
-	struct timeval tv;
-
-	gettimeofday(&tv, NULL);
-	return (tv.tv_sec * 1000.0) + (tv.tv_usec / 1000.0);
-}
-
-static void print_benchmark_results(const char *test_name, size_t buffer_size,
-				     struct benchmark_results *thp,
-				     struct benchmark_results *regular)
-{
-	double s2d_improvement = ((regular->sys_to_dev_time - thp->sys_to_dev_time) /
-				 regular->sys_to_dev_time) * 100.0;
-	double d2s_improvement = ((regular->dev_to_sys_time - thp->dev_to_sys_time) /
-				 regular->dev_to_sys_time) * 100.0;
-	double throughput_s2d_improvement = ((thp->throughput_s2d - regular->throughput_s2d) /
-					    regular->throughput_s2d) * 100.0;
-	double throughput_d2s_improvement = ((thp->throughput_d2s - regular->throughput_d2s) /
-					    regular->throughput_d2s) * 100.0;
-
-	printf("\n=== %s (%.1f MB) ===\n", test_name, buffer_size / (1024.0 * 1024.0));
-	printf("                     | With THP        | Without THP     | Improvement\n");
-	printf("---------------------------------------------------------------------\n");
-	printf("Sys->Dev Migration   | %.3f ms        | %.3f ms        | %.1f%%\n",
-	       thp->sys_to_dev_time, regular->sys_to_dev_time, s2d_improvement);
-	printf("Dev->Sys Migration   | %.3f ms        | %.3f ms        | %.1f%%\n",
-	       thp->dev_to_sys_time, regular->dev_to_sys_time, d2s_improvement);
-	printf("S->D Throughput      | %.2f GB/s      | %.2f GB/s      | %.1f%%\n",
-	       thp->throughput_s2d, regular->throughput_s2d, throughput_s2d_improvement);
-	printf("D->S Throughput      | %.2f GB/s      | %.2f GB/s      | %.1f%%\n",
-	       thp->throughput_d2s, regular->throughput_d2s, throughput_d2s_improvement);
-}
-
-/*
- * Run a single migration benchmark
- * fd: file descriptor for hmm device
- * use_thp: whether to use THP
- * buffer_size: size of buffer to allocate
- * iterations: number of iterations
- * results: where to store results
- */
-static inline int run_migration_benchmark(int fd, int use_thp, size_t buffer_size,
-					   int iterations, struct benchmark_results *results)
-{
-	struct hmm_buffer *buffer;
-	unsigned long npages = buffer_size / sysconf(_SC_PAGESIZE);
-	double start, end;
-	double s2d_total = 0, d2s_total = 0;
-	int ret, i;
-	int *ptr;
-
-	buffer = hmm_buffer_alloc(buffer_size, buffer_size,
-				  PROT_READ | PROT_WRITE,
-				  MAP_PRIVATE | MAP_ANONYMOUS, -1);
-	if (!buffer) {
-		ret = -1;
-		goto cleanup;
-	}
-	memset(buffer->mirror, 0xFF, buffer_size);
-
-	/* Apply THP hint if requested */
-	if (use_thp)
-		ret = madvise(buffer->ptr, buffer_size, MADV_HUGEPAGE);
-	else
-		ret = madvise(buffer->ptr, buffer_size, MADV_NOHUGEPAGE);
-
-	if (ret)
-		goto cleanup;
-
-	/* Initialize memory to make sure pages are allocated */
-	ptr = (int *)buffer->ptr;
-	for (i = 0; i < buffer_size / sizeof(int); i++)
-		ptr[i] = i & 0xFF;
-
-	/* Warmup iteration */
-	ret = hmm_migrate_sys_to_dev(fd, buffer, npages);
-	if (ret)
-		goto cleanup;
-
-	ret = hmm_migrate_dev_to_sys(fd, buffer, npages);
-	if (ret)
-		goto cleanup;
-
-	/* Benchmark iterations */
-	for (i = 0; i < iterations; i++) {
-		/* System to device migration */
-		start = get_time_ms();
-
-		ret = hmm_migrate_sys_to_dev(fd, buffer, npages);
-		if (ret)
-			goto cleanup;
-
-		end = get_time_ms();
-		s2d_total += (end - start);
-
-		/* Device to system migration */
-		start = get_time_ms();
-
-		ret = hmm_migrate_dev_to_sys(fd, buffer, npages);
-		if (ret)
-			goto cleanup;
-
-		end = get_time_ms();
-		d2s_total += (end - start);
-	}
-
-	/* Calculate average times and throughput */
-	results->sys_to_dev_time = s2d_total / iterations;
-	results->dev_to_sys_time = d2s_total / iterations;
-	results->throughput_s2d = (buffer_size / (1024.0 * 1024.0 * 1024.0)) /
-				 (results->sys_to_dev_time / 1000.0);
-	results->throughput_d2s = (buffer_size / (1024.0 * 1024.0 * 1024.0)) /
-				 (results->dev_to_sys_time / 1000.0);
-
-cleanup:
-	hmm_buffer_free(buffer);
-	return ret;
-}
-
-/*
- * Benchmark THP migration with different buffer sizes
- */
-TEST_F_TIMEOUT(hmm, benchmark_thp_migration, 120)
-{
-	struct benchmark_results thp_results, regular_results;
-	size_t thp_size = pmd_psize();
-	int iterations = 5;
-
-	if (!thp_size)
-		thp_size = TWOMEG;
-
-	printf("\nHMM THP Migration Benchmark\n");
-	printf("---------------------------\n");
-	printf("System page size: %ld bytes\n", sysconf(_SC_PAGESIZE));
-
-	/* Test different buffer sizes */
-	size_t test_sizes[] = {
-		thp_size / 4,      /* quarter THP */
-		thp_size / 2,      /* half THP */
-		thp_size,          /* single THP */
-		thp_size * 2,      /* two THPs */
-		thp_size * 4,      /* four THPs */
-		thp_size * 8,      /* eight THPs */
-		thp_size * 128,    /* one twenty eight THPs */
-	};
-
-	static const char *const test_names[] = {
-		"Small Buffer",
-		"Half THP Size",
-		"Single THP Size",
-		"Two THP Size",
-		"Four THP Size",
-		"Eight THP Size",
-		"One twenty eight THP Size"
-	};
-
-	int num_tests = ARRAY_SIZE(test_sizes);
-
-	/* Run all tests */
-	for (int i = 0; i < num_tests; i++) {
-		/* Skip test sizes exceeding INT_MAX to avoid overflow */
-		if (test_sizes[i] > INT_MAX)
-			break;
-
-		/* Test with THP */
-		ASSERT_EQ(run_migration_benchmark(self->fd, 1, test_sizes[i],
-					iterations, &thp_results), 0);
-
-		/* Test without THP */
-		ASSERT_EQ(run_migration_benchmark(self->fd, 0, test_sizes[i],
-					iterations, &regular_results), 0);
-
-		/* Print results */
-		print_benchmark_results(test_names[i], test_sizes[i],
-					&thp_results, &regular_results);
-	}
-}
 /*
  * Test that HMM can fault in pages backed by userfaultfd using the
  * hmm_range_fault_unlocked_timeout() path with no timeout. This exercises
diff --git a/tools/testing/selftests/mm/vm_util.c b/tools/testing/selftests/mm/vm_util.c
index b5485326fa0b..9643058472fb 100644
--- a/tools/testing/selftests/mm/vm_util.c
+++ b/tools/testing/selftests/mm/vm_util.c
@@ -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)
-- 
2.56.1



                 reply	other threads:[~2026-11-27  1:44 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

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=20261127014430.2043154-1-balbirs@nvidia.com \
    --to=balbirs@nvidia.com \
    --cc=akpm@linux-foundation.org \
    --cc=david@kernel.org \
    --cc=jgg@ziepe.ca \
    --cc=leon@kernel.org \
    --cc=liam@infradead.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-mm@kvack.org \
    --cc=mhocko@suse.com \
    --cc=rppt@kernel.org \
    --cc=shuah@kernel.org \
    --cc=vbabka@kernel.org \
    /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