Skip to contentSkip to conversation

selftests/mm: separate GUP microbenchmarking from functional testing

PATCH Superseded Sarthak Sharma 5 participants 18 messages Jul 16 – Jul 27

Subsystems (8)

v5 of 11 Superseded Latest is v11 →

lore
Share & tools
lore
#0 lore raw

v5 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 that 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 series separates the two concerns: tools/mm/gup_bench handles benchmarking, while tools/testing/selftests/mm/gup_test handles functional testing. To avoid duplicating the HugeTLB and related file helpers, the series first prepares the existing helpers for sharing, then moves the common code to tools/lib/mm/ for use by both selftests and tools/mm.

Patch 1 makes read_file(), write_file(), read_num() and write_num() in vm_util.c return errors to callers rather than exiting. It also makes read_num() reject negative, malformed and out-of-range values. Existing mm selftest callers are updated to report failures through kselftest helpers. This avoids carrying behavior specific to selftests into the shared helper implementation introduced in the next patch.

Patch 2 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 keeps the helpers available to mm selftests through vm_util.h, updates the explicit x86 protection_keys build rules to preserve full prerequisite paths, and adds tools/lib/mm/ to the MEMORY MANAGEMENT - MISC entry in MAINTAINERS.

Patch 3 moves hugepage_settings.[ch] from selftests/mm to tools/lib/mm/. It keeps the helpers available to selftests through vm_util.h where possible, uses direct <mm/hugepage_settings.h> includes for users that do not include vm_util.h, and removes the remaining kselftest dependency from the implementation. The existing HugeTLB diagnostic messages are preserved as TAP-compatible printf() diagnostics.

Patch 4 adds tools/mm/gup_bench.c, a standalone microbenchmark for GUP_FAST, PIN_FAST and PIN_LONGTERM through the CONFIG_GUP_TEST debugfs interface. It runs the benchmark subset of the configuration matrix previously covered by run_gup_matrix(), covering all three benchmark commands, read and write GUP access, private and shared mappings, four page counts, and THP, non-THP and hugetlb mappings. It also restores HugeTLB settings after each hugetlb benchmark run and validates command-line arguments, including their numeric ranges.

Patch 5 rewrites gup_test.c to use the kselftest harness. It covers all five GUP/PUP kernel functions (get_user_pages, get_user_pages_fast, pin_user_pages, pin_user_pages_fast, and pin_user_pages with FOLL_LONGTERM), plus DUMP_USER_PAGES_TEST, across 12 mapping configurations. These configurations cover THP, non-THP and hugetlb mappings, each with private and shared mappings and read and write access. Each configuration is tested with four batch sizes (1, 512, 123, and all pages). The patch also preserves the old sparse dump coverage for pages 0, 19 and 0x1000. Results are reported in standard TAP format, with no command-line arguments required.

---
Changes in v5:
- Rebase onto v7.2-rc3
- Address feedback from Mike, John, Dev and Sashiko
- Make file helper diagnostics TAP-compatible
- Validate read_num() input and propagate errors while restoring shared memory limits
- Preserve build bisectability for the x86 protection_keys test
- Preserve HugeTLB diagnostics using TAP-compatible printf() output
- Validate the remaining numeric arguments accepted by gup_bench

Changes in v4:
- Address review feedback from Mike and Sashiko
- Add a preparatory patch so shared file helpers return errors instead of exiting
- Reduce include churn by keeping shared helpers exposed through vm_util.h
- Preserve HugeTLB diagnostics and restore HugeTLB state more carefully
- Fix selftests/mm build details after moving helpers to tools/lib/mm
- Tighten gup_bench argument handling and gup_test setup/sparse-dump coverage

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:
v4: lore.kernel.org/all/[email protected]/
v3: lore.kernel.org/all/[email protected]/
v2: lore.kernel.org/all/[email protected]/
v1: lore.kernel.org/all/[email protected]/

---
Sarthak Sharma (5):
selftests/mm: make file helpers return errors 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                     | 119 ++++
 tools/lib/mm/file_utils.h                     |  12 +
 .../selftests => lib}/mm/hugepage_settings.c  |  81 ++-
 .../selftests => lib}/mm/hugepage_settings.h  |   0
 tools/mm/.gitignore                           |   2 +
 tools/mm/Makefile                             |  10 +-
 tools/mm/gup_bench.c                          | 482 ++++++++++++++
 tools/testing/selftests/mm/Makefile           |  13 +-
 tools/testing/selftests/mm/compaction_test.c  |   2 +-
 tools/testing/selftests/mm/cow.c              |   1 -
 .../selftests/mm/folio_split_race_test.c      |   1 -
 tools/testing/selftests/mm/guard-regions.c    |   1 -
 tools/testing/selftests/mm/gup_longterm.c     |   1 -
 tools/testing/selftests/mm/gup_test.c         | 606 +++++++++++-------
 tools/testing/selftests/mm/hmm-tests.c        |   6 +-
 tools/testing/selftests/mm/hugetlb-madvise.c  |   1 -
 tools/testing/selftests/mm/hugetlb-mmap.c     |   1 -
 tools/testing/selftests/mm/hugetlb-mremap.c   |   1 -
 tools/testing/selftests/mm/hugetlb-shm.c      |   1 -
 .../selftests/mm/hugetlb-soft-offline.c       |   2 +-
 tools/testing/selftests/mm/hugetlb-vmemmap.c  |   1 -
 tools/testing/selftests/mm/hugetlb_dio.c      |   1 -
 .../selftests/mm/hugetlb_fault_after_madv.c   |   1 -
 .../selftests/mm/hugetlb_madv_vs_map.c        |   1 -
 tools/testing/selftests/mm/khugepaged.c       |  14 +-
 tools/testing/selftests/mm/ksm_tests.c        |   1 -
 tools/testing/selftests/mm/migration.c        |   5 +-
 tools/testing/selftests/mm/pagemap_ioctl.c    |   1 -
 .../testing/selftests/mm/prctl_thp_disable.c  |   1 -
 tools/testing/selftests/mm/protection_keys.c  |   2 +-
 tools/testing/selftests/mm/run_vmtests.sh     |  37 +-
 tools/testing/selftests/mm/soft-dirty.c       |   1 -
 .../selftests/mm/split_huge_page_test.c       |   5 +-
 tools/testing/selftests/mm/thuge-gen.c        |   1 -
 tools/testing/selftests/mm/transhuge-stress.c |   1 -
 tools/testing/selftests/mm/uffd-common.h      |   1 -
 tools/testing/selftests/mm/uffd-wp-mremap.c   |   2 +-
 .../selftests/mm/va_high_addr_switch.c        |   1 -
 tools/testing/selftests/mm/vm_util.c          |  88 +--
 tools/testing/selftests/mm/vm_util.h          |   7 +-
 42 files changed, 1131 insertions(+), 398 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 (91%)
 rename tools/{testing/selftests => lib}/mm/hugepage_settings.h (100%)
 create mode 100644 tools/mm/gup_bench.c

base-commit: a13c140cc289c0b7b3770bce5b3ad42ab35074aa --
2.39.5

#1 lore raw

PATCH v5 1/5 selftests/mm: make file helpers return errors

Change read_file(), write_file(), read_num() and write_num() in vm_util.c to report failures to callers instead of exiting from the helper.

Update the helpers to print TAP compatible diagnostics before returning errors and update their callers to handle those errors. This prepares the helpers to be moved to tools/lib/mm without carrying selftest-specific process-exit behaviour into the shared implementation.

Also, make read_file() return a negative errno on failure instead of 0, so callers can distinguish a successful read from an I/O error. Make read_num() reject negative and malformed values.

---
 .../testing/selftests/mm/hugepage_settings.c  |  64 +++++++---
 tools/testing/selftests/mm/khugepaged.c       |  13 +-
 .../selftests/mm/split_huge_page_test.c       |   4 +-
 tools/testing/selftests/mm/vm_util.c          | 115 +++++++++++++-----
 tools/testing/selftests/mm/vm_util.h          |   6 +-
 5 files changed, 150 insertions(+), 52 deletions(-)
tools/testing/selftests/mm/hugepage_settings.c +50 -14
index 2eab2110ac6a..202c8e6b0246 100644
@@ -61,10 +61,9 @@ int thp_read_string(const char *name, const char * const strings[])
 		exit(EXIT_FAILURE);
 	}
 
-	if (!read_file(path, buf, sizeof(buf))) {
-		perror(path);
+	ret = read_file(path, buf, sizeof(buf));
+	if (ret < 0)
 		exit(EXIT_FAILURE);
-	}
 
 	c = strchr(buf, '[');
 	if (!c) {
@@ -103,12 +102,15 @@ void thp_write_string(const char *name, const char *val)
 		printf("%s: Pathname is too long\n", __func__);
 		exit(EXIT_FAILURE);
 	}
-	write_file(path, val, strlen(val) + 1);
+	ret = write_file(path, val, strlen(val) + 1);
+	if (ret < 0)
+		exit(EXIT_FAILURE);
 }
 
 unsigned long thp_read_num(const char *name)
 {
 	char path[PATH_MAX];
+	unsigned long num;
 	int ret;
 
 	ret = snprintf(path, PATH_MAX, THP_SYSFS "%s", name);
@@ -116,7 +118,11 @@ unsigned long thp_read_num(const char *name)
 		printf("%s: Pathname is too long\n", __func__);
 		exit(EXIT_FAILURE);
 	}
-	return read_num(path);
+	ret = read_num(path, &num);
+	if (ret < 0)
+		exit(EXIT_FAILURE);
+
+	return num;
 }
 
 void thp_write_num(const char *name, unsigned long num)
@@ -129,7 +135,9 @@ void thp_write_num(const char *name, unsigned long num)
 		printf("%s: Pathname is too long\n", __func__);
 		exit(EXIT_FAILURE);
 	}
-	write_num(path, num);
+	ret = write_num(path, num);
+	if (ret < 0)
+		exit(EXIT_FAILURE);
 }
 
 void thp_read_settings(struct thp_settings *settings)
@@ -157,8 +165,13 @@ void thp_read_settings(struct thp_settings *settings)
 		.max_ptes_shared = thp_read_num("khugepaged/max_ptes_shared"),
 		.pages_to_scan = thp_read_num("khugepaged/pages_to_scan"),
 	};
-	if (dev_queue_read_ahead_path[0])
-		settings->read_ahead_kb = read_num(dev_queue_read_ahead_path);
+	if (dev_queue_read_ahead_path[0]) {
+		int ret = read_num(dev_queue_read_ahead_path,
+				   &settings->read_ahead_kb);
+
+		if (ret < 0)
+			exit(EXIT_FAILURE);
+	}
 
 	for (i = 0; i < NR_ORDERS; i++) {
 		if (!((1 << i) & orders)) {
@@ -208,8 +221,13 @@ void thp_write_settings(struct thp_settings *settings)
 	thp_write_num("khugepaged/max_ptes_shared", khugepaged->max_ptes_shared);
 	thp_write_num("khugepaged/pages_to_scan", khugepaged->pages_to_scan);
 
-	if (dev_queue_read_ahead_path[0])
-		write_num(dev_queue_read_ahead_path, settings->read_ahead_kb);
+	if (dev_queue_read_ahead_path[0]) {
+		int ret = write_num(dev_queue_read_ahead_path,
+				    settings->read_ahead_kb);
+
+		if (ret < 0)
+			exit(EXIT_FAILURE);
+	}
 
 	for (i = 0; i < NR_ORDERS; i++) {
 		if (!((1 << i) & orders))
@@ -306,8 +324,11 @@ static unsigned long __thp_supported_orders(bool is_shmem)
 			exit(EXIT_FAILURE);
 		}
 
+		if (access(path, F_OK))
+			continue;
+
 		ret = read_file(path, buf, sizeof(buf));
-		if (ret)
+		if (ret > 0)
 			orders |= 1UL << i;
 	}
 
@@ -425,28 +446,43 @@ static void hugetlb_sysfs_path(char *buf, size_t buflen,
 unsigned long hugetlb_nr_pages(unsigned long size)
 {
 	char path[PATH_MAX];
+	unsigned long nr;
+	int ret;
 
 	hugetlb_sysfs_path(path, sizeof(path), size, "nr_hugepages");
 
-	return read_num(path);
+	ret = read_num(path, &nr);
+	if (ret < 0)
+		exit(EXIT_FAILURE);
+
+	return nr;
 }
 
 void hugetlb_set_nr_pages(unsigned long size, unsigned long nr)
 {
 	char path[PATH_MAX];
+	int ret;
 
 	hugetlb_sysfs_path(path, sizeof(path), size, "nr_hugepages");
 
-	write_num(path, nr);
+	ret = write_num(path, nr);
+	if (ret < 0)
+		exit(EXIT_FAILURE);
 }
 
 unsigned long hugetlb_free_pages(unsigned long size)
 {
 	char path[PATH_MAX];
+	unsigned long nr;
+	int ret;
 
 	hugetlb_sysfs_path(path, sizeof(path), size, "free_hugepages");
 
-	return read_num(path);
+	ret = read_num(path, &nr);
+	if (ret < 0)
+		exit(EXIT_FAILURE);
+
+	return nr;
 }
 
 static bool __hugetlb_setup(unsigned long size, unsigned long nr)
tools/testing/selftests/mm/khugepaged.c +9 -4
index 10e8dedcb087..5eac27dd9683 100644
@@ -119,6 +119,7 @@ static void get_finfo(const char *dir)
 	char buf[1 << 10];
 	char path[PATH_MAX];
 	char *str, *end;
+	int ret;
 
 	finfo.dir = dir;
 	stat(finfo.dir, &path_stat);
@@ -138,8 +139,9 @@ static void get_finfo(const char *dir)
 		     major(path_stat.st_dev), minor(path_stat.st_dev))
 	    >= sizeof(path))
 		ksft_exit_fail_msg("%s: Pathname is too long\n", __func__);
-	if (read_file(path, buf, sizeof(buf)) < 0)
-		ksft_exit_fail_perror("read_file(read_num)");
+	ret = read_file(path, buf, sizeof(buf));
+	if (ret < 0)
+		ksft_exit_fail();
 	if (strstr(buf, "DEVTYPE=disk")) {
 		/* Found it */
 		if (snprintf(finfo.dev_queue_read_ahead_path,
@@ -319,7 +321,7 @@ static void *file_setup_area_common(int nr_hpages, enum file_setup_ops setup)
 {
 	const int open_opt = setup == FILE_SETUP_READ_ONLY_FS ? O_RDONLY : O_RDWR;
 	const int mmap_prot = setup == FILE_SETUP_READ_ONLY_FS ? PROT_READ : (PROT_READ | PROT_WRITE);
-	int fd;
+	int fd, ret;
 	void *p;
 	unsigned long size;
 
@@ -363,7 +365,10 @@ static void *file_setup_area_common(int nr_hpages, enum file_setup_ops setup)
 		ksft_exit_fail_perror("mmap()");
 
 	/* Drop page cache */
-	write_file("/proc/sys/vm/drop_caches", "3", 2);
+	ret = write_file("/proc/sys/vm/drop_caches", "3", 2);
+	if (ret)
+		ksft_exit_fail();
+
 	success("OK");
 	return p;
 }
tools/testing/selftests/mm/split_huge_page_test.c +3 -1
index 32b991472f74..83a75001518b 100644
@@ -269,7 +269,9 @@ static void write_debugfs(const char *fmt, ...)
 	if (ret >= INPUT_MAX)
 		ksft_exit_fail_msg("%s: Debugfs input is too long\n", __func__);
 
-	write_file(SPLIT_DEBUGFS, input, ret + 1);
+	ret = write_file(SPLIT_DEBUGFS, input, ret + 1);
+	if (ret)
+		ksft_exit_fail();
 }
 
 static char *allocate_zero_filled_hugepage(size_t len)
tools/testing/selftests/mm/vm_util.c +85 -30

172 lines not on this page · raw message

tools/testing/selftests/mm/vm_util.h +3 -3
index ea8fc8fdf0eb..28c3d7c1faed 100644
@@ -164,10 +164,10 @@ 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);
+int write_file(const char *path, const char *buf, size_t buflen);
+int read_num(const char *path, unsigned long *num);
+int 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
#2 lore raw

Re: [PATCH v5 1/5] selftests/mm: make file helpers return errors

Quoted 14 lines · Sarthak Sharma

Change read_file(), write_file(), read_num() and write_num() in vm_util.c to report failures to callers instead of exiting from the helper.

Update the helpers to print TAP compatible diagnostics before returning errors and update their callers to handle those errors. This prepares the helpers to be moved to tools/lib/mm without carrying selftest-specific process-exit behaviour into the shared implementation.

Also, make read_file() return a negative errno on failure instead of 0, so callers can distinguish a successful read from an I/O error. Make read_num() reject negative and malformed values.

---

[...]

tools/testing/selftests/mm/vm_util.c int read_file(const char *path, char *buf, size_t buflen) · line 710

Quoted 8 lines
tools/testing/selftests/mm/vm_util.c +4 -2
 	fd = open(path, O_RDONLY);
-	if (fd == -1)
-		return 0;
+	if (fd == -1) {
+		int err = errno;
+
+		printf("# %s: %s (%d)\n", path, strerror(err), err);

Shouldn't we be using

ksft_print_msg()

That does the magic "# " for us.

[...]

Quoted 5 lines
tools/testing/selftests/mm/vm_util.h +0 -0
index ea8fc8fdf0eb..28c3d7c1faed 100644

tools/testing/selftests/mm/vm_util.h int unpoison_memory(unsigned long pfn); · line 170

Quoted 10 lines
tools/testing/selftests/mm/vm_util.h +3 -3
@@ -164,10 +164,10 @@ 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);
+int write_file(const char *path, const char *buf, size_t buflen);
+int read_num(const char *path, unsigned long *num);
+int write_num(const char *path, unsigned long num);

In mm-unstable there is now write_num_ignore_einval(), where we want to ignore -EINVAL and also not print any information.

Which makes me wonder whether we really need the diagnostic information, or if we could just use return values that are clear enough when printing+failing in the caller?

Signature · 3 lines
Cheers,

David
#3 lore raw

Re: [PATCH v5 1/5] selftests/mm: make file helpers return errors

Quoted 12 lines · David Hildenbrand (Arm)

tools/testing/selftests/mm/vm_util.c int read_file(const char *path, char *buf, size_t buflen) · line 710

Quoted 7 lines · Sarthak Sharma
tools/testing/selftests/mm/vm_util.c +4 -2
 	fd = open(path, O_RDONLY);
-	if (fd == -1)
-		return 0;
+	if (fd == -1) {
+		int err = errno;
+
+		printf("# %s: %s (%d)\n", path, strerror(err), err);

Shouldn't we be using

Quoted 3 lines

ksft_print_msg()

That does the magic "# " for us.

Or ksft_perror().

#4 lore raw

Re: [PATCH v5 1/5] selftests/mm: make file helpers return errors

Quoted 13 lines · Mark Brown
Quoted 11 lines · David Hildenbrand (Arm)

tools/testing/selftests/mm/vm_util.c int read_file(const char *path, char *buf, size_t buflen) · line 710

Quoted 7 lines · Sarthak Sharma
tools/testing/selftests/mm/vm_util.c +4 -2
 	fd = open(path, O_RDONLY);
-	if (fd == -1)
-		return 0;
+	if (fd == -1) {
+		int err = errno;
+
+		printf("# %s: %s (%d)\n", path, strerror(err), err);

Shouldn't we be using

Quoted 5 lines
Quoted 3 lines

ksft_print_msg()

That does the magic "# " for us.

Or ksft_perror().

Right, and if we want to move this to tools/lib/mm later, maybe we can just not print anything and instead only return expressive errnos.

Signature · 3 lines
Cheers,

David
#5 lore raw

Re: [PATCH v5 1/5] selftests/mm: make file helpers return errors

Hi David and Mark!

Quoted 16 lines · David Hildenbrand (Arm)
Quoted 15 lines · Mark Brown
Quoted 13 lines · David Hildenbrand (Arm)

tools/testing/selftests/mm/vm_util.c int read_file(const char *path, char *buf, size_t buflen) · line 710

Quoted 7 lines · Sarthak Sharma
tools/testing/selftests/mm/vm_util.c +4 -2
 	fd = open(path, O_RDONLY);
-	if (fd == -1)
-		return 0;
+	if (fd == -1) {
+		int err = errno;
+
+		printf("# %s: %s (%d)\n", path, strerror(err), err);

Shouldn't we be using

ksft_print_msg()

Quoted 8 lines
Quoted 3 lines
Quoted 1 line

That does the magic "# " for us.

Or ksft_perror().

Right, and if we want to move this to tools/lib/mm later, maybe we can just not print anything and instead only return expressive errnos.

Yes, we are not using ksft_* prints here because these functions move to tools/lib/mm later in the series.

Printing errors from the helpers was suggested by Mike [1]. I also think this is useful since the diagnostic handling stays at one place, instead of duplicating it in every caller.

If we want helpers to be silent, I can change them to return errors and let the callers handle the diagnostics. Please let me know how I should proceed here.

[1]
https://lore.kernel.org/all/178048086818.472368.16811711474077698399.b4-review@b4/

reviewer
#6 lore raw

Re: [PATCH v5 1/5] selftests/mm: make file helpers return errors

Quoted 28 lines · Sarthak Sharma

Hi David and Mark!

Quoted 16 lines · David Hildenbrand (Arm)
Quoted 15 lines · Mark Brown
Quoted 13 lines · David Hildenbrand (Arm)

tools/testing/selftests/mm/vm_util.c int read_file(const char *path, char *buf, size_t buflen) · line 710

Quoted 7 lines · Sarthak Sharma
tools/testing/selftests/mm/vm_util.c +4 -2
 	fd = open(path, O_RDONLY);
-	if (fd == -1)
-		return 0;
+	if (fd == -1) {
+		int err = errno;
+
+		printf("# %s: %s (%d)\n", path, strerror(err), err);

Shouldn't we be using

ksft_print_msg()

Quoted 8 lines
Quoted 3 lines
Quoted 1 line

That does the magic "# " for us.

Or ksft_perror().

Right, and if we want to move this to tools/lib/mm later, maybe we can just not print anything and instead only return expressive errnos.

Quoted 6 lines

Yes, we are not using ksft_* prints here because these functions move to tools/lib/mm later in the series.

Printing errors from the helpers was suggested by Mike [1]. I also think this is useful since the diagnostic handling stays at one place, instead of duplicating it in every caller.

This is a common pitfall. In fact, the lower ("leaf") routines, *especially* library-like functions that are called by disparate callers, should be usually be silent.

That's because printing is a policy choice, and libraries must refrain from imposing that policy onto their callers.

Higher up the call stack, the code has more context, and is able to make more informed choices about whether or not to print something. And when or if they do print, they may include some of that extra context, and so you can now see that the printing code is potentially different at each call site, and now you haven't actually saved any duplication after all by trying to centralize it.>

Quoted 6 lines

If we want helpers to be silent, I can change them to return errors and let the callers handle the diagnostics. Please let me know how I should proceed here.

[1]
https://lore.kernel.org/all/178048086818.472368.16811711474077698399.b4-review@b4/

thanks,

Signature · 1 line
John Hubbard
#7 lore raw

Re: [PATCH v5 1/5] selftests/mm: make file helpers return errors

Quoted 13 lines · John Hubbard
Quoted 12 lines · Sarthak Sharma

Hi David and Mark!

Quoted 5 lines · David Hildenbrand (Arm)

Right, and if we want to move this to tools/lib/mm later, maybe we can just not print anything and instead only return expressive errnos.

Yes, we are not using ksft_* prints here because these functions move to tools/lib/mm later in the series.

Quoted 7 lines
Quoted 3 lines

Printing errors from the helpers was suggested by Mike [1]. I also think this is useful since the diagnostic handling stays at one place, instead of duplicating it in every caller.

This is a common pitfall. In fact, the lower ("leaf") routines, *especially* library-like functions that are called by disparate callers, should be usually be silent.

Yes, agreed!

Signature · 3 lines
Cheers,

David
#8 lore raw

PATCH v5 2/5 tools/lib/mm: add shared file helpers

In reply to Sarthak Sharma

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 so they can be reused outside selftests as well.

Keep the helpers exposed to mm selftests through vm_util.h by including the new shared header there, and link the new helper into the selftests/mm build.

Update the explicit x86 protection_keys 32-bit and 64-bit build rules to preserve prerequisite paths, now that file_utils.c is built from tools/lib/mm.

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

---
 MAINTAINERS                          |   1 +
 tools/lib/mm/file_utils.c            | 119 +++++++++++++++++++++++++++
 tools/lib/mm/file_utils.h            |  12 +++
 tools/testing/selftests/mm/Makefile  |  11 +--
 tools/testing/selftests/mm/vm_util.c | 109 ------------------------
 tools/testing/selftests/mm/vm_util.h |   6 +-
 6 files changed, 139 insertions(+), 119 deletions(-)
 create mode 100644 tools/lib/mm/file_utils.c
 create mode 100644 tools/lib/mm/file_utils.h
MAINTAINERS +1 -0
index 806bd2d80d15..26b06d80ab45 100644
@@ -17128,6 +17128,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 +119 -0

119 lines not on this page · raw message

tools/lib/mm/file_utils.h new file +12 -0
new file mode 100644
index 000000000000..38576790a342
@@ -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);
+int write_file(const char *path, const char *buf, size_t buflen);
+int read_num(const char *path, unsigned long *num);
+int write_num(const char *path, unsigned long num);
+
+#endif
tools/testing/selftests/mm/Makefile +6 -5
index e6df968f0971..70d6a9ccf9c9 100644
@@ -37,7 +37,8 @@ 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
+CFLAGS += $(EXTRA_CFLAGS) $(KHDR_INCLUDES) $(TOOLS_INCLUDES)
 CFLAGS += -Wunreachable-code
 LDLIBS = -lrt -lpthread -lm
 
@@ -187,8 +188,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
@@ -217,7 +218,7 @@ $(BINARIES_32): CFLAGS += -m32 -mxsave
 $(BINARIES_32): LDLIBS += -lrt -ldl -lm
 $(BINARIES_32): $(OUTPUT)/%_32: %.c
 	$(call msg,CC,,$@)
-	$(Q)$(CC) $(CFLAGS) $(EXTRA_CFLAGS) $(notdir $^) $(LDLIBS) -o $@
+	$(Q)$(CC) $(CFLAGS) $(EXTRA_CFLAGS) $^ $(LDLIBS) -o $@
 $(foreach t,$(VMTARGETS),$(eval $(call gen-target-rule-32,$(t))))
 endif
 
@@ -226,7 +227,7 @@ $(BINARIES_64): CFLAGS += -m64 -mxsave
 $(BINARIES_64): LDLIBS += -lrt -ldl
 $(BINARIES_64): $(OUTPUT)/%_64: %.c
 	$(call msg,CC,,$@)
-	$(Q)$(CC) $(CFLAGS) $(EXTRA_CFLAGS) $(notdir $^) $(LDLIBS) -o $@
+	$(Q)$(CC) $(CFLAGS) $(EXTRA_CFLAGS) $^ $(LDLIBS) -o $@
 $(foreach t,$(VMTARGETS),$(eval $(call gen-target-rule-64,$(t))))
 endif
 
tools/testing/selftests/mm/vm_util.c +0 -109

115 lines not on this page · raw message

tools/testing/selftests/mm/vm_util.h +1 -5
index 28c3d7c1faed..d45135283732 100644
@@ -8,6 +8,7 @@
 #include <unistd.h> /* _SC_PAGESIZE */
 #include "kselftest.h"
 #include <linux/fs.h>
+#include <mm/file_utils.h>
 
 #define BIT_ULL(nr)                   (1ULL << (nr))
 #define PM_SOFT_DIRTY                 BIT_ULL(55)
@@ -164,11 +165,6 @@ int unpoison_memory(unsigned long pfn);
 #define PAGEMAP_PRESENT(ent)	(((ent) & (1ull << 63)) != 0)
 #define PAGEMAP_PFN(ent)	((ent) & ((1ull << 55) - 1))
 
-int read_file(const char *path, char *buf, size_t buflen);
-int write_file(const char *path, const char *buf, size_t buflen);
-int read_num(const char *path, unsigned long *num);
-int 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
#9 lore raw

PATCH v5 3/5 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.

Keep the helpers exposed to mm selftests through vm_util.h where possible, and use direct <mm/hugepage_settings.h> includes for users that do not include vm_util.h. 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 TAP comment style printf() diagnostics.

---
 .../selftests => lib}/mm/hugepage_settings.c    | 17 ++++++++++++-----
 .../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                |  1 -
 .../selftests/mm/folio_split_race_test.c        |  1 -
 tools/testing/selftests/mm/guard-regions.c      |  1 -
 tools/testing/selftests/mm/gup_longterm.c       |  1 -
 tools/testing/selftests/mm/gup_test.c           |  1 -
 tools/testing/selftests/mm/hmm-tests.c          |  6 +++---
 tools/testing/selftests/mm/hugetlb-madvise.c    |  1 -
 tools/testing/selftests/mm/hugetlb-mmap.c       |  1 -
 tools/testing/selftests/mm/hugetlb-mremap.c     |  1 -
 tools/testing/selftests/mm/hugetlb-shm.c        |  1 -
 .../testing/selftests/mm/hugetlb-soft-offline.c |  2 +-
 tools/testing/selftests/mm/hugetlb-vmemmap.c    |  1 -
 tools/testing/selftests/mm/hugetlb_dio.c        |  1 -
 .../selftests/mm/hugetlb_fault_after_madv.c     |  1 -
 .../testing/selftests/mm/hugetlb_madv_vs_map.c  |  1 -
 tools/testing/selftests/mm/khugepaged.c         |  1 -
 tools/testing/selftests/mm/ksm_tests.c          |  1 -
 tools/testing/selftests/mm/migration.c          |  5 ++---
 tools/testing/selftests/mm/pagemap_ioctl.c      |  1 -
 tools/testing/selftests/mm/prctl_thp_disable.c  |  1 -
 tools/testing/selftests/mm/protection_keys.c    |  2 +-
 tools/testing/selftests/mm/soft-dirty.c         |  1 -
 .../testing/selftests/mm/split_huge_page_test.c |  1 -
 tools/testing/selftests/mm/thuge-gen.c          |  1 -
 tools/testing/selftests/mm/transhuge-stress.c   |  1 -
 tools/testing/selftests/mm/uffd-common.h        |  1 -
 tools/testing/selftests/mm/uffd-wp-mremap.c     |  2 +-
 .../testing/selftests/mm/va_high_addr_switch.c  |  1 -
 tools/testing/selftests/mm/vm_util.h            |  1 +
 33 files changed, 26 insertions(+), 40 deletions(-)
 rename tools/{testing/selftests => lib}/mm/hugepage_settings.c (97%)
 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 +12 -5
similarity index 97%
rename from tools/testing/selftests/mm/hugepage_settings.c
rename to tools/lib/mm/hugepage_settings.c
index 202c8e6b0246..8d9e44727edb 100644
@@ -9,11 +9,16 @@
 #include <string.h>
 #include <unistd.h>
 
-#include "vm_util.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;
@@ -403,8 +408,7 @@ 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);
+		printf("# [INFO] detected hugetlb page size: %zu KiB\n", kb);
 	}
 	closedir(dir);
 	return count;
@@ -538,7 +542,8 @@ 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);
+		printf("# detected %d huge page sizes, will only test %d\n",
+		       nr_enabled, max);
 		nr_enabled = max;
 	}
 
@@ -610,8 +615,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 70d6a9ccf9c9..e77c4cc41b47 100644
@@ -188,8 +188,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 5b582588e015..b2eaa490e7c2 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 +0 -1
index 0c627ea89ff7..3c3d129c91b2 100644
@@ -29,7 +29,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 +0 -1
index 6329e37fff4c..496b74b37476 100644
@@ -25,7 +25,6 @@
 #include <unistd.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 +0 -1
index b21df3040b1c..ed967b3c7f37 100644
@@ -21,7 +21,6 @@
 #include <sys/uio.h>
 #include <unistd.h>
 #include "vm_util.h"
-#include "hugepage_settings.h"
 
 #include "../pidfd/pidfd.h"
 
tools/testing/selftests/mm/gup_longterm.c +0 -1
index eb8963e9d98f..68c840bae082 100644
@@ -29,7 +29,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 +0 -1
index 3f841a96f870..5f44761dbec0 100644
@@ -14,7 +14,6 @@
 #include <mm/gup_test.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 2f2b9879d100..e1bb031019f2 100644
@@ -10,9 +10,6 @@
  * bugs.
  */
 
-#include "kselftest_harness.h"
-#include "hugepage_settings.h"
-
 #include <errno.h>
 #include <fcntl.h>
 #include <stdio.h>
@@ -29,6 +26,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 +0 -1
index 555b4b3d1430..57cf790ca478 100644
@@ -14,7 +14,6 @@
 #include <fcntl.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 +0 -1
index 0f2aad1b7dbd..a458becf5832 100644
@@ -18,7 +18,6 @@
 #include <linux/memfd.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 +0 -1
index ed3d92e862d8..9b724af66e93 100644
@@ -26,7 +26,6 @@
 #include <stdbool.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 +0 -1
index 3ff7f062b7eb..f4514da49e1d 100644
@@ -29,7 +29,6 @@
 #include <sys/mman.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 +0 -1
index 507df78a158d..c46a656c25a0 100644
@@ -11,7 +11,6 @@
 #include <sys/mman.h>
 #include <fcntl.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 +0 -1
index fb4600570e13..9495974eccbe 100644
@@ -20,7 +20,6 @@
 #include <sys/syscall.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 +0 -1
index 2dc158054f66..56c5a8533e9d 100644
@@ -10,7 +10,6 @@
 
 #include "vm_util.h"
 #include "kselftest.h"
-#include "hugepage_settings.h"
 
 #define INLOOP_ITER 100
 
tools/testing/selftests/mm/hugetlb_madv_vs_map.c +0 -1
index f94549efcc6f..2532a42b98df 100644
@@ -25,7 +25,6 @@
 #include <unistd.h>
 
 #include "vm_util.h"
-#include "hugepage_settings.h"
 
 #define INLOOP_ITER 100
 
tools/testing/selftests/mm/khugepaged.c +0 -1
index 5eac27dd9683..2382973b2f55 100644
@@ -22,7 +22,6 @@
 #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 +0 -1
index a050f4840cfa..cc37492cf32c 100644
@@ -15,7 +15,6 @@
 #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 +2 -3
index 29f7492453d4..4fe97c033261 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,8 @@
 #include <sys/types.h>
 #include <signal.h>
 #include <time.h>
+
+#include "kselftest_harness.h"
 #include "vm_util.h"
 
 #define TWOMEG		(2<<20)
tools/testing/selftests/mm/pagemap_ioctl.c +0 -1
index 6f8971d5b3ce..804372803080 100644
@@ -23,7 +23,6 @@
 
 #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 +0 -1
index d8d9d1de57b8..a4f8451791cb 100644
@@ -14,7 +14,6 @@
 #include <sys/wait.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 +0 -1
index fb1864a68e1c..6c22ac9e93db 100644
@@ -9,7 +9,6 @@
 
 #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 +0 -1
index 83a75001518b..e744c9434e7d 100644
@@ -21,7 +21,6 @@
 #include <time.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 +0 -1
index 22b9c2f1c35d..0da15d530a1f 100644
@@ -14,7 +14,6 @@
 #include <string.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 +0 -1
index 8eb0c5630e7e..96f72898ebe0 100644
@@ -17,7 +17,6 @@
 #include <sys/mman.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 +0 -1
index 92a21b97f745..0723843a7626 100644
@@ -37,7 +37,6 @@
 
 #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 +1 -1
index c973d6722720..eb4b2433b00e 100644
@@ -7,8 +7,8 @@
 #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 +0 -1
index e24d7ba00b44..5a354a664d1f 100644
@@ -11,7 +11,6 @@
 
 #include "vm_util.h"
 #include "kselftest.h"
-#include "hugepage_settings.h"
 
 /*
  * The hint addr value is used to allocate addresses
tools/testing/selftests/mm/vm_util.h +1 -0
index d45135283732..aa856f0d32d6 100644
@@ -9,6 +9,7 @@
 #include "kselftest.h"
 #include <linux/fs.h>
 #include <mm/file_utils.h>
+#include <mm/hugepage_settings.h>
 
 #define BIT_ULL(nr)                   (1ULL << (nr))
 #define PM_SOFT_DIRTY                 BIT_ULL(55)
Signature · 1 line
2.39.5
#10 lore raw

PATCH v5 4/5 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 benchmark subset of configurations covered by 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. Restore HugeTLB settings after each hugetlb benchmark run. Validate numeric command-line arguments instead of relying on atoi().

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 the benchmark subset of 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 | 482 +++++++++++++++++++++++++++++++++++++++++++
 4 files changed, 492 insertions(+), 3 deletions(-)
 create mode 100644 tools/mm/gup_bench.c
MAINTAINERS +1 -0
index 26b06d80ab45..7d121dee9f0f 100644
@@ -17024,6 +17024,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 +482 -0

482 lines not on this page · raw message

Signature · 1 line
2.39.5
#11 lore raw

Re: [PATCH v5 4/5] tools/mm: add a standalone GUP microbenchmark

Quoted 6 lines · 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 benchmark subset of configurations covered by run_gup_matrix() in run_vmtests.sh: all

To we really need the matrix in there? The matrix was good for selftests, but not when using it as a benchmarking tool, no?

Quoted 46 lines

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. Restore HugeTLB settings after each hugetlb benchmark run. Validate numeric command-line arguments instead of relying on atoi().

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 the benchmark subset of 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 | 482 +++++++++++++++++++++++++++++++++++++++++++
 4 files changed, 492 insertions(+), 3 deletions(-)
 create mode 100644 tools/mm/gup_bench.c
MAINTAINERS +1 -0
index 26b06d80ab45..7d121dee9f0f 100644
@@ -17024,6 +17024,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 +0 -0
index 922879f93fc8..154d740be02e 100644

tools/mm/.gitignore line 5

Quoted 4 lines
tools/mm/.gitignore +1 -0
@@ -2,3 +2,5 @@
 slabinfo
 page-types
 page_owner_sort
+thp_swap_allocator_test

^ Does that belong into this patch?

tools/mm/.gitignore line 6

Quoted 1 line
tools/mm/.gitignore +1 -0
+gup_bench

I wonder if it makes sense to "git mv" the existing tool (only slightly fixing header inclusion to make it compile), essentially removing the old benchmark.

So it would be

1) Move tools, fixing up includes and removing the gup testing logic+matrix to make it compile

2) Extend tool by XYZ

3) Add completely new selftest

Because looking at patch #5, there is not really any code reuse there, but I suspect there should be some in this patch here when moving instead of copying.

That would make this a lot easier to review.

Signature · 3 lines
Cheers,

David
#12 lore raw

Re: [PATCH v5 4/5] tools/mm: add a standalone GUP microbenchmark

Hi David!

Quoted 10 lines · David Hildenbrand (Arm)
Quoted 6 lines · 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 benchmark subset of configurations covered by run_gup_matrix() in run_vmtests.sh: all

To we really need the matrix in there? The matrix was good for selftests, but not when using it as a benchmarking tool, no?

Yeah makes sense, I'll remove the matrix part.

Quoted 42 lines
Quoted 42 lines

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. Restore HugeTLB settings after each hugetlb benchmark run. Validate numeric command-line arguments instead of relying on atoi().

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 the benchmark subset of 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 | 482 +++++++++++++++++++++++++++++++++++++++++++
 4 files changed, 492 insertions(+), 3 deletions(-)
 create mode 100644 tools/mm/gup_bench.c
MAINTAINERS +1 -0
index 26b06d80ab45..7d121dee9f0f 100644
@@ -17024,6 +17024,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
 
Quoted 11 lines

tools/mm/.gitignore line 5

Quoted 9 lines
tools/mm/.gitignore +1 -0
index 922879f93fc8..154d740be02e 100644
@@ -2,3 +2,5 @@
 slabinfo
 page-types
 page_owner_sort
+thp_swap_allocator_test

^ Does that belong into this patch?

No it's just that when I was working on v1 of this patchset, I saw that this binary hadn't been added to .gitignore so I added this, since I was already modifying .gitignore to add gup_bench. It has stuck around since then :). Will drop this from here and send a patch separately for this.

Quoted 11 lines

tools/mm/.gitignore line 6

Quoted 2 lines
tools/mm/.gitignore +1 -0
+gup_bench

I wonder if it makes sense to "git mv" the existing tool (only slightly fixing header inclusion to make it compile), essentially removing the old benchmark.

So it would be

1) Move tools, fixing up includes and removing the gup testing logic+matrix to make it compile

Quoted 9 lines

2) Extend tool by XYZ

3) Add completely new selftest

Because looking at patch #5, there is not really any code reuse there, but I suspect there should be some in this patch here when moving instead of copying.

That would make this a lot easier to review.

I agree that this would make it easier to review, but then there would be an intermediate commit where there is no gup_test selftest. Is this fine from a bisectability point of view?

#13 lore raw

Re: [PATCH v5 4/5] tools/mm: add a standalone GUP microbenchmark

Quoted 70 lines · Sarthak Sharma

Hi David!

Quoted 10 lines · David Hildenbrand (Arm)
Quoted 6 lines · 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 benchmark subset of configurations covered by run_gup_matrix() in run_vmtests.sh: all

To we really need the matrix in there? The matrix was good for selftests, but not when using it as a benchmarking tool, no?

Yeah makes sense, I'll remove the matrix part.

Quoted 42 lines
Quoted 42 lines

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. Restore HugeTLB settings after each hugetlb benchmark run. Validate numeric command-line arguments instead of relying on atoi().

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 the benchmark subset of 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 | 482 +++++++++++++++++++++++++++++++++++++++++++
 4 files changed, 492 insertions(+), 3 deletions(-)
 create mode 100644 tools/mm/gup_bench.c
MAINTAINERS +1 -0
index 26b06d80ab45..7d121dee9f0f 100644
@@ -17024,6 +17024,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
 
Quoted 11 lines

tools/mm/.gitignore line 5

Quoted 9 lines
tools/mm/.gitignore +1 -0
index 922879f93fc8..154d740be02e 100644
@@ -2,3 +2,5 @@
 slabinfo
 page-types
 page_owner_sort
+thp_swap_allocator_test

^ Does that belong into this patch?

Quoted 4 lines

No it's just that when I was working on v1 of this patchset, I saw that this binary hadn't been added to .gitignore so I added this, since I was already modifying .gitignore to add gup_bench. It has stuck around since then :). Will drop this from here and send a patch separately for this.

Yes please, thanks!

Quoted 19 lines
Quoted 19 lines

tools/mm/.gitignore line 6

Quoted 3 lines
tools/mm/.gitignore +1 -0
+gup_bench

I wonder if it makes sense to "git mv" the existing tool (only slightly fixing header inclusion to make it compile), essentially removing the old benchmark.

So it would be

1) Move tools, fixing up includes and removing the gup testing logic+matrix to make it compile

2) Extend tool by XYZ

3) Add completely new selftest

Because looking at patch #5, there is not really any code reuse there, but I suspect there should be some in this patch here when moving instead of copying.

Quoted 6 lines
Quoted 2 lines

That would make this a lot easier to review.

I agree that this would make it easier to review, but then there would be an intermediate commit where there is no gup_test selftest. Is this fine from a bisectability point of view?

Perfectly fine I think!

Signature · 3 lines
Cheers,

David
reviewer
#14 lore raw

Re: [PATCH v5 4/5] tools/mm: add a standalone GUP microbenchmark

In reply to David Hildenbrand (Arm)
Quoted 66 lines · David Hildenbrand (Arm)
Quoted 6 lines · 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 benchmark subset of configurations covered by run_gup_matrix() in run_vmtests.sh: all

To we really need the matrix in there? The matrix was good for selftests, but not when using it as a benchmarking tool, no?

Quoted 46 lines

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. Restore HugeTLB settings after each hugetlb benchmark run. Validate numeric command-line arguments instead of relying on atoi().

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 the benchmark subset of 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 | 482 +++++++++++++++++++++++++++++++++++++++++++
 4 files changed, 492 insertions(+), 3 deletions(-)
 create mode 100644 tools/mm/gup_bench.c
MAINTAINERS +1 -0
index 26b06d80ab45..7d121dee9f0f 100644
@@ -17024,6 +17024,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 +0 -0
index 922879f93fc8..154d740be02e 100644

tools/mm/.gitignore line 5

Quoted 4 lines
tools/mm/.gitignore +1 -0
@@ -2,3 +2,5 @@
 slabinfo
 page-types
 page_owner_sort
+thp_swap_allocator_test

^ Does that belong into this patch?

tools/mm/.gitignore line 6

Quoted 1 line
tools/mm/.gitignore +1 -0
+gup_bench
Quoted 7 lines

I wonder if it makes sense to "git mv" the existing tool (only slightly fixing header inclusion to make it compile), essentially removing the old benchmark.

So it would be

1) Move tools, fixing up includes and removing the gup testing logic+matrix to make it compile

I like your idea, it would reduce churn. But is it okay that there will be an intermediate state in which gup_test is not there? Wouldn't we call this a "regression"?

Quoted 10 lines

2) Extend tool by XYZ

3) Add completely new selftest

Because looking at patch #5, there is not really any code reuse there, but I suspect there should be some in this patch here when moving instead of copying.

That would make this a lot easier to review.

#15 lore raw

Re: [PATCH v5 4/5] tools/mm: add a standalone GUP microbenchmark

Quoted 72 lines · Dev Jain
Quoted 69 lines · David Hildenbrand (Arm)
Quoted 6 lines · 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 benchmark subset of configurations covered by run_gup_matrix() in run_vmtests.sh: all

To we really need the matrix in there? The matrix was good for selftests, but not when using it as a benchmarking tool, no?

Quoted 46 lines

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. Restore HugeTLB settings after each hugetlb benchmark run. Validate numeric command-line arguments instead of relying on atoi().

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 the benchmark subset of 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 | 482 +++++++++++++++++++++++++++++++++++++++++++
 4 files changed, 492 insertions(+), 3 deletions(-)
 create mode 100644 tools/mm/gup_bench.c
MAINTAINERS +1 -0
index 26b06d80ab45..7d121dee9f0f 100644
@@ -17024,6 +17024,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 +0 -0
index 922879f93fc8..154d740be02e 100644

tools/mm/.gitignore line 5

Quoted 4 lines
tools/mm/.gitignore +1 -0
@@ -2,3 +2,5 @@
 slabinfo
 page-types
 page_owner_sort
+thp_swap_allocator_test

^ Does that belong into this patch?

tools/mm/.gitignore line 6

Quoted 1 line
tools/mm/.gitignore +1 -0
+gup_bench

I wonder if it makes sense to "git mv" the existing tool (only slightly fixing header inclusion to make it compile), essentially removing the old benchmark.

Quoted 8 lines
Quoted 4 lines

So it would be

1) Move tools, fixing up includes and removing the gup testing logic+matrix to make it compile

I like your idea, it would reduce churn. But is it okay that there will be an intermediate state in which gup_test is not there? Wouldn't we call this a "regression"?

I don't really see why that as a problem as we are effectively removing the old test and replacing it by a new one?

In fact, while at it, we can just call the new test "gup.c", as the "_test" is superfluous.

Signature · 3 lines
Cheers,

David
#16 lore raw

PATCH v5 5/5 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, fetching the full mapping in one ioctl call. 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     | 605 ++++++++++++++--------
 tools/testing/selftests/mm/run_vmtests.sh |  37 +-
 3 files changed, 397 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 +387 -218

661 lines not on this page · raw message

tools/testing/selftests/mm/run_vmtests.sh +1 -36
index 8c296dedf047..56df0dcc9640 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
@@ -276,18 +252,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
#17 lore raw
In reply to Sarthak Sharma

Hi Andrew!

Quoted 8 lines · Sarthak Sharma

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 that 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 6 lines

This series separates the two concerns: tools/mm/gup_bench handles benchmarking, while tools/testing/selftests/mm/gup_test handles functional testing. To avoid duplicating the HugeTLB and related file helpers, the series first prepares the existing helpers for sharing, then moves the common code to tools/lib/mm/ for use by both selftests and tools/mm.

I went through Sashiko's review on this patchset [1]. There are two concerns that it raises:

1. Patch 4: gup_thread() does not print an error when the ioctl() fails. This is a valid point and it would be helpful to print it, but it is not a functional bug.

2. Patch 5: This concern is a false positive. ksefltest_harness keeps fixture teardown disabled while FIXTURE_SETUP() is running and enables it only when setup succeeds. Therefore, FIXTURE_TEARDOWN() won't run if setup fails.

I'll wait for people to review this patchset and if nothing else comes up, I can send a fixup for the first concern. If there are other things to address, I can include everything in a v6. Please let me know if you require a different approach.

[1]
https://sashiko.dev/#/patchset/20260716123226.197736-1-sarthak.sharma%40arm.com