selftests/mm: separate GUP microbenchmarking from functional testing
Subsystems (9)
v11 cover selftests/mm: separate GUP microbenchmarking from functional testing
gup_test.c currently serves two separate purposes: benchmarking (GUP_FAST_BENCHMARK, PIN_FAST_BENCHMARK and PIN_LONGTERM_BENCHMARK) and functional testing (GUP_BASIC_TEST, PIN_BASIC_TEST and DUMP_USER_PAGES_TEST). Keeping both in one program makes the functional tests harder to run and report individually, while run_vmtests.sh has to invoke the program repeatedly with different options.
Separate these roles into tools/mm/gup_bench for benchmarking and tools/testing/selftests/mm/gup for functional testing. Move the shared file and hugepage helpers to tools/lib/mm/ so both programs can use them without duplicating the implementation.
Patch 1 makes read_file(), write_file(), read_num(), write_num() and write_num_ignore_einval() return errors to their callers instead of exiting. It also makes read_num() reject negative and malformed values and updates the existing callers to handle failures.
Patch 2 moves these file helpers from vm_util.c to tools/lib/mm/. It keeps them available to the mm selftests through vm_util.h and adjusts the selftests build accordingly.
Patch 3 moves hugepage_settings.[ch] from selftests/mm to tools/lib/mm/. It also removes its kselftest dependency while preserving TAP-compatible diagnostics for selftest users.
Patch 4 moves the existing gup_test implementation from selftests/mm to tools/mm as gup_bench. This keeps the code movement separate from the subsequent changes and makes it easier to review.
Patch 5 removes the functional test modes and kselftest dependency from gup_bench. When run without arguments, it performs one GUP_FAST benchmark using the existing defaults instead of running the whole matrix. Other benchmark configurations can be selected through command-line options.
Patch 6 adds a new harness-based GUP selftest. It covers THP, non-THP and HugeTLB mappings across private/shared and read/write variants. For each variant, it tests get_user_pages(), get_user_pages_fast(), pin_user_pages(), pin_user_pages_fast() and long-term pinning modes using four batch sizes. The HugeTLB variants share a one-time setup of two hugeTLB pages.
Changes in v11:
- Rename NR_HUGE_PAGES to NR_HUGETLB_PAGES
- Address style comments in gup.c
Changes in v10:
- Use anonymous mappings instead of /dev/zero backed ones
- Use two HugeTLB pages in GUP selftest
- Remove dump_page() description from mm/Kconfig
- Check effective UID before running gup_bench
Changes in v9:
- Handle read_num() and write_num() calls in hugetlb-soft-offline.c
- Require GUP tests to pin all requested pages
- Setup HugeTLB pages after prerequisite checks, choose the count using a 256 MiB target and skip variants when setup fails
Changes in v8:
- Add a comment to clarify read_num() input validation
- Remove duplicate exit code in gup_bench
- Drop DUMP_USER_PAGES_TEST coverage from gup
- Update Documentation and Kconfig
Changes in v7:
- Rebase onto mm-unstable
- Address feedback from Mike and Sashiko
- Make read_file() return 0 on success and tighten read_num() parsing
- Move write_num_ignore_einval() with the shared file helpers
- Keep the existing gup_bench argument parsing and avoid unrelated checks
- Set up 128 HugeTLB pages once for all HugeTLB selftest variants
Changes in v6:
- Rebase onto v7.2-rc5
- Address feedback from David, Mark, John, Dev and Sashiko
- Keep the shared file helpers silent and let callers report errors
- Move the existing gup_test into tools/mm before changing its behaviour
- Report ioctl failures from gup_bench worker threads
- Remove the default benchmark matrix and run one default benchmark instead
- Add the new harness-based functional selftest in a separate final patch
- Drop the unrelated thp_swap_allocator_test .gitignore change
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
Changes in v2:
- Address v1 feedback from Sashiko
- Add fast and long-term 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
- Restore HugeTLB settings from fixture teardown
- Report nr_pages_per_call for each iteration
- Update the pin_user_pages unit-testing documentation
Previous versions:
v10: lore.kernel.org/all/[email protected]/
v9: lore.kernel.org/all/[email protected]/
v8: lore.kernel.org/all/[email protected]/
v7: lore.kernel.org/all/[email protected]/
v6: lore.kernel.org/all/[email protected]/
v5: lore.kernel.org/all/[email protected]/
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 (6):
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: move gup_test from selftests/mm to tools/mm tools/mm: make gup_bench a benchmark only tool selftests/mm: add a GUP selftest
Documentation/core-api/pin_user_pages.rst | 9 -
MAINTAINERS | 4 +-
mm/Kconfig | 19 +-
tools/lib/mm/file_utils.c | 106 +++++++
tools/lib/mm/file_utils.h | 13 +
.../selftests => lib}/mm/hugepage_settings.c | 109 ++++++--
.../selftests => lib}/mm/hugepage_settings.h | 0
tools/mm/.gitignore | 1 +
tools/mm/Makefile | 11 +-
.../mm/gup_test.c => mm/gup_bench.c} | 190 ++++++-------
tools/testing/selftests/mm/Makefile | 15 +-
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.c | 262 ++++++++++++++++++
tools/testing/selftests/mm/gup_longterm.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 -
.../selftests/mm/hugetlb-soft-offline.c | 18 +-
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 | 15 +-
tools/testing/selftests/mm/ksm_tests.c | 1 -
tools/testing/selftests/mm/migration.c | 2 +-
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 | 6 +-
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 | 121 +++-----
tools/testing/selftests/mm/vm_util.h | 8 +-
42 files changed, 649 insertions(+), 328 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 (87%)
rename tools/{testing/selftests => lib}/mm/hugepage_settings.h (100%)
rename tools/{testing/selftests/mm/gup_test.c => mm/gup_bench.c} (50%)
create mode 100644 tools/testing/selftests/mm/gup.cSignature · 1 line
2.53.0
PATCH v11 1/6 selftests/mm: make file helpers return errors
Change read_file(), write_file(), read_num(), write_num() and write_num_ignore_einval() in vm_util.c to report failures to callers instead of exiting from the helper.
Make read_file() return a negative errno on failure and 0 on success, so callers can distinguish a successful read from an I/O error. Also make read_num() reject negative and malformed values.
Keep write_num_ignore_einval() silent for -EINVAL while returning other errors to its caller.
Update callers to print diagnostics and fail wherever required. Modify a comment which implies write_num() uses ksft_exit_fail_msg(). Also add a helper print_file_access_error() in hugepage_settings.c to print TAP-compatible errors without a kselftest dependency. This prepares the helpers to be moved to tools/lib/mm without a kselftest dependency.
--- .../testing/selftests/mm/hugepage_settings.c | 98 +++++++++++--- .../selftests/mm/hugetlb-soft-offline.c | 16 ++- tools/testing/selftests/mm/khugepaged.c | 14 +- .../selftests/mm/split_huge_page_test.c | 5 +- tools/testing/selftests/mm/vm_util.c | 122 ++++++++++++------ tools/testing/selftests/mm/vm_util.h | 8 +- 6 files changed, 192 insertions(+), 71 deletions(-)
tools/testing/selftests/mm/hugepage_settings.c +81 -17
@@ -8,6 +8,7 @@#include <stdlib.h> #include <string.h> #include <unistd.h> +#include <errno.h> #include "vm_util.h" #include "hugepage_settings.h"
@@ -48,6 +49,11 @@ static const char * const shmem_enabled_strings[] = { NULL }; +static void print_file_access_error (const char *path,int ret) +{ +printf ("# %s: %s (%d)\n" , path,strerror (-ret), -ret); +} +int thp_read_string (const char *name,const char *const strings[]) {char path[PATH_MAX ];
@@ -61,8 +67,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) { +print_file_access_error (path, ret);exit (EXIT_FAILURE ); }
@@ -103,12 +110,17 @@ 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) { +print_file_access_error (path, ret); +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 +128,13 @@ 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) { +print_file_access_error (path, ret); +exit (EXIT_FAILURE ); + } + +return num; }void thp_write_num (const char *name,unsigned long num)
@@ -129,7 +147,11 @@ 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) { +print_file_access_error (path, ret); +exit (EXIT_FAILURE ); + } }void thp_read_settings (struct thp_settings *settings)
@@ -157,8 +179,15 @@ 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) { +print_file_access_error (dev_queue_read_ahead_path, ret); +exit (EXIT_FAILURE ); + } + }for (i =0 ; i <NR_ORDERS ; i++) {if (!((1 << i) & orders)) {
@@ -208,8 +237,15 @@ 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) { +print_file_access_error (dev_queue_read_ahead_path, ret); +exit (EXIT_FAILURE ); + } + }for (i =0 ; i <NR_ORDERS ; i++) {if (!((1 << i) & orders))
@@ -307,8 +343,15 @@ static unsigned long __thp_supported_orders(bool is_shmem) } ret =read_file (path, buf,sizeof (buf)); -if (ret) - orders |=1UL << i; +if (ret) { +if (ret != -ENOENT ) { +print_file_access_error (path, ret); +exit (EXIT_FAILURE ); + } +continue ; + } + + orders |=1UL << i; }return orders;
@@ -382,8 +425,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;
@@ -425,28 +467,49 @@ 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) { +print_file_access_error (path, ret); +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_ignore_einval (path, nr); + ret =write_num_ignore_einval (path, nr); +if (ret) { +print_file_access_error (path, ret); +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) { +print_file_access_error (path, ret); +exit (EXIT_FAILURE ); + } + +return nr; }unsigned long hugetlb_nr_resv_pages (unsigned long size)
@@ -511,7 +574,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; }
tools/testing/selftests/mm/hugetlb-soft-offline.c +12 -4
@@ -85,8 +85,7 @@ static unsigned long orig_enable_soft_offline = -1UL;/* * Runs from an atexit handler, so it must not call anything that -* exits on failure: write_num() would re-enter exit() through -* ksft_exit_fail_msg(). +* exits on failure. */ static void restore_enable_soft_offline (void ) {
@@ -152,7 +151,10 @@ static void test_soft_offline_common(int enable_soft_offline) hugepagesize_kb = file_stat.f_bsize /1024 ;ksft_print_msg ("Hugepagesize is %ldkB\n" , hugepagesize_kb); -write_num (ENABLE_SOFT_OFFLINE_PATH , enable_soft_offline); + ret =write_num (ENABLE_SOFT_OFFLINE_PATH , enable_soft_offline); +if (ret) +ksft_exit_fail_msg ("Failed to write to %s: %s\n" , +ENABLE_SOFT_OFFLINE_PATH ,strerror (-ret)); nr_hugepages_before =hugetlb_nr_default_pages ();
@@ -189,6 +191,8 @@ static void test_soft_offline_common(int enable_soft_offline)int main (int argc,char **argv) { +int ret; +ksft_print_header ();if (!hugetlb_setup_default (8 ))
@@ -196,7 +200,11 @@ int main(int argc, char **argv)ksft_set_plan (2 ); - orig_enable_soft_offline =read_num (ENABLE_SOFT_OFFLINE_PATH ); + ret =read_num (ENABLE_SOFT_OFFLINE_PATH , &orig_enable_soft_offline); +if (ret) +ksft_exit_fail_msg ("Failed to read %s: %s\n" , +ENABLE_SOFT_OFFLINE_PATH ,strerror (-ret)); +atexit (restore_enable_soft_offline);test_soft_offline_common (1 );
tools/testing/selftests/mm/khugepaged.c +10 -4
@@ -122,6 +122,7 @@ static void get_finfo(const char *dir)char buf[1 <<10 ];char path[PATH_MAX ];char *str, *end; +int ret; finfo.dir = dir;if (stat (finfo.dir, &path_stat))
@@ -142,8 +143,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))) -ksft_exit_fail_perror ("read_file(uevent)" ); + ret =read_file (path, buf,sizeof (buf)); +if (ret) +ksft_exit_fail_msg ("read_file(%s): %s\n" , path,strerror (-ret));if (strstr (buf,"DEVTYPE=disk" )) {/* Found it */ if (snprintf(finfo.dev_queue_read_ahead_path,
@@ -324,7 +326,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;
@@ -362,7 +364,11 @@ 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_msg ("write_file(drop_caches): %s\n" , +strerror (-ret)); +success ("OK" );return p; }
tools/testing/selftests/mm/split_huge_page_test.c +4 -1
@@ -145,7 +145,10 @@ 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_msg ("write_file(%s): %s\n" ,SPLIT_DEBUGFS , +strerror (-ret)); }static char *allocate_zero_filled_hugepage (size_t len)
tools/testing/selftests/mm/vm_util.c +81 -41
190 lines not on this page · raw message
tools/testing/selftests/mm/vm_util.h +4 -4
@@ -166,11 +166,11 @@ 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 write_file (const char *path,const char *buf,size_t buflen);int read_file (const char *path,char *buf,size_t buflen); -unsigned long read_num (const char *path); -void write_num (const char *path,unsigned long num); -void write_num_ignore_einval (const char *path,unsigned long num); +int read_num (const char *path,unsigned long *num); +int write_num (const char *path,unsigned long num); +int write_num_ignore_einval (const char *path,unsigned long num);void shm_limits_prepare (unsigned long length);void __shm_limits_restore (void );
Signature · 1 line
2.53.0
Re: [PATCH v11 1/6] selftests/mm: make file helpers return errors
Quoted 17 lines · Sarthak Sharma
Change
read_file(),write_file(),read_num(),write_num()andwrite_num_ignore_einval()invm_util.cto report failures to callers instead of exiting from the helper.Make
read_file()return a negative errno on failure and 0 on success, so callers can distinguish a successful read from an I/O error. Also makeread_num()reject negative and malformed values.Keep
write_num_ignore_einval()silent for-EINVALwhile returning other errors to its caller.Update callers to print diagnostics and fail wherever required. Modify a comment which implies
write_num()usesksft_exit_fail_msg(). Also add a helperprint_file_access_error()inhugepage_settings.cto print TAP-compatible errors without a kselftest dependency. This prepares the helpers to be moved totools/lib/mmwithout a kselftest dependency.
Quoted 5 lines
Tested-by Muhammad Usama Anjum <[email protected]>Signed-off-by Sarthak Sharma <[email protected]>---
Hi Andrew!
This fixes an MM selftest build failure in patch 1/6: read_num() changed its interface, but hugetlb_nr_resv_pages() was missed during the caller conversion. I'm sorry for missing this.
Can you please apply this fixlet after this patch and before the next one? Sorry for the inconvenience again.
From f7cfae95a1d0554ea03df320d91641979a9ac473 Mon Sep 17 00:00:00 2001
From: Sarthak Sharma <[email protected]>
Date: Fri, 18 Sep 2026 17:40:50 +0530
Subject: [PATCH] fixup! selftests/mm: make file helpers return errors
Convert hugetlb_nr_resv_pages(), which was missed when read_num() changed to return an error and store the parsed value through an output pointer.
--- tools/testing/selftests/mm/hugepage_settings.c | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-)
(quoted diff) +0 -0
b/tools/testing/selftests/mm/hugepage_settings.c
index 9a63420d0744..6f3abd357385 100644
tools/testing/selftests/mm/hugepage_settings.c +9 -1
@@ -515,10 +515,18 @@ unsigned long hugetlb_free_pages(unsigned long size)unsigned long hugetlb_nr_resv_pages (unsigned long size) {char path[PATH_MAX ]; +unsigned long nr; +int ret;hugetlb_sysfs_path (path,sizeof (path), size,"resv_hugepages" ); -return read_num (path); + ret =read_num (path, &nr); +if (ret) { +print_file_access_error (path, ret); +exit (EXIT_FAILURE ); + } + +return nr; }static bool __hugetlb_setup (unsigned long size,unsigned long nr)
Signature · 1 line
2.53.0
PATCH v11 2/6 tools/lib/mm: add shared file helpers
In reply to Sarthak SharmaMove read_file(), write_file(), read_num(), write_num() and write_num_ignore_einval() 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 | 106 +++++++++++++++++++++++++++ tools/lib/mm/file_utils.h | 13 ++++ tools/testing/selftests/mm/Makefile | 11 +-- tools/testing/selftests/mm/vm_util.c | 97 ------------------------ tools/testing/selftests/mm/vm_util.h | 7 +- 6 files changed, 127 insertions(+), 108 deletions(-) create mode 100644 tools/lib/mm/file_utils.c create mode 100644 tools/lib/mm/file_utils.h
MAINTAINERS +1 -0
@@ -17295,6 +17295,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 +106 -0
106 lines not on this page · raw message
tools/lib/mm/file_utils.h new file +13 -0
@@ -0,0 +1,13 @@ +/* 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); +int write_num_ignore_einval (const char *path,unsigned long num); + +#endif
tools/testing/selftests/mm/Makefile +6 -5
@@ -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
@@ -184,8 +185,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
@@ -214,7 +215,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
@@ -223,7 +224,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 -97
103 lines not on this page · raw message
tools/testing/selftests/mm/vm_util.h +1 -6
@@ -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)
@@ -166,12 +167,6 @@ int unpoison_memory(unsigned long pfn);#define PAGEMAP_PRESENT (ent) (((ent) & (1ull << 63)) != 0)#define PAGEMAP_PFN (ent) ((ent) & ((1ull << 55) - 1)) -int write_file (const char *path,const char *buf,size_t buflen); -int read_file (const char *path,char *buf,size_t buflen); -int read_num (const char *path,unsigned long *num); -int write_num (const char *path,unsigned long num); -int write_num_ignore_einval (const char *path,unsigned long num); -void shm_limits_prepare (unsigned long length);void __shm_limits_restore (void );
Signature · 1 line
2.53.0
PATCH v11 3/6 tools/lib/mm: move hugepage_settings out of selftests
In reply to Sarthak SharmaMove 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 files 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 by including file_utils.h directly and using EXIT_FAILURE in the signal handler.
---
.../{testing/selftests => lib}/mm/hugepage_settings.c | 11 +++++++++--
.../{testing/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 -
tools/testing/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 -
tools/testing/selftests/mm/hugetlb-soft-offline.c | 2 +-
tools/testing/selftests/mm/hugetlb_dio.c | 1 -
tools/testing/selftests/mm/hugetlb_fault_after_madv.c | 1 -
tools/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 | 2 +-
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 -
tools/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 +-
tools/testing/selftests/mm/va_high_addr_switch.c | 1 -
tools/testing/selftests/mm/vm_util.h | 1 +
32 files changed, 22 insertions(+), 34 deletions(-)
rename tools/{testing/selftests => lib}/mm/hugepage_settings.c (99%)
rename tools/{testing/selftests => lib}/mm/hugepage_settings.h (100%)tools/lib/mm/hugepage_settings.c renamed from tools/testing/selftests/mm/hugepage_settings.c +9 -2
@@ -10,11 +10,16 @@#include <unistd.h> #include <errno.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;
@@ -647,8 +652,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
tools/testing/selftests/mm/Makefile +4 -2
@@ -185,8 +185,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
@@ -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
@@ -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
@@ -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
@@ -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
@@ -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
@@ -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
@@ -10,9 +10,6 @@* bugs. */ -#include "kselftest_harness.h" -#include "hugepage_settings.h" -#include <errno.h> #include <fcntl.h> #include <stdio.h>
@@ -33,6 +30,9 @@#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
tools/testing/selftests/mm/hugetlb-madvise.c +0 -1
@@ -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
@@ -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
@@ -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
@@ -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
@@ -22,10 +22,10 @@#include <sys/mman.h> #include <sys/statfs.h> #include <sys/types.h> +#include <mm/hugepage_settings.h> #include "kselftest.h" #include "vm_util.h" -#include "hugepage_settings.h" #ifndef MADV_SOFT_OFFLINE #define MADV_SOFT_OFFLINE 101
tools/testing/selftests/mm/hugetlb_dio.c +0 -1
@@ -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
@@ -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
@@ -14,7 +14,6 @@#include <unistd.h> #include "vm_util.h" -#include "hugepage_settings.h" #define INLOOP_ITER 100
tools/testing/selftests/mm/khugepaged.c +0 -1
@@ -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
@@ -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 +1 -1
@@ -5,7 +5,6 @@*/ #include "kselftest_harness.h" -#include "hugepage_settings.h" #include <string.h> #include <pthread.h>
@@ -16,6 +15,7 @@#include <sys/types.h> #include <signal.h> #include <time.h> +#include "vm_util.h" #define TWOMEG (2<<20)
tools/testing/selftests/mm/pagemap_ioctl.c +0 -1
@@ -24,7 +24,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
@@ -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
@@ -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" u64 shadow_pkey_reg;
tools/testing/selftests/mm/soft-dirty.c +0 -1
@@ -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
@@ -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
@@ -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
@@ -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
@@ -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
@@ -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
@@ -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
@@ -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.53.0
PATCH v11 4/6 tools/mm: move gup_test from selftests/mm to tools/mm
In reply to Sarthak SharmaMove tools/testing/selftests/mm/gup_test.c to tools/mm/gup_bench.c. This is the first step in separating its benchmarking and functional testing components. Later patches will make this a purely benchmarking tool and introduce a new functional selftest under selftests/mm.
Include hugepage_settings.h directly instead of vm_util.h and use getpagesize() instead of psize().
Adjust the Makefiles in both locations and add gup_bench to tools/mm/.gitignore. Remove the gup_test invocations from run_vmtests.sh and update MAINTAINERS.
Also remove the gup_test reference from Documentation/core-api/pin_user_pages.rst. The selftest added later in the series is standalone and does not need per command documentation here.
---
Documentation/core-api/pin_user_pages.rst | 9 -----
MAINTAINERS | 2 +-
tools/mm/.gitignore | 1 +
tools/mm/Makefile | 11 ++++--
.../mm/gup_test.c => mm/gup_bench.c} | 8 ++---
tools/testing/selftests/mm/Makefile | 1 -
tools/testing/selftests/mm/run_vmtests.sh | 36 -------------------
7 files changed, 14 insertions(+), 54 deletions(-)
rename tools/{testing/selftests/mm/gup_test.c => mm/gup_bench.c} (97%)Documentation/core-api/pin_user_pages.rst +0 -9
@@ -226,15 +226,6 @@ will be pinned longterm, and whose data will be accessed. Unit testing ============ -This file:: - - tools/testing/selftests/mm/gup_test.c - -has the following new calls to exercise the new pin*() wrapper functions: - -* PIN_FAST_BENCHMARK (./gup_test -a) -* PIN_BASIC_TEST (./gup_test -b) - 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: ::
MAINTAINERS +1 -1
@@ -17187,8 +17187,8 @@ 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 MEMORY MANAGEMENT - KSM (Kernel Samepage Merging) M: Andrew Morton <[email protected]>
tools/mm/.gitignore +1 -0
@@ -3,3 +3,4 @@ slabinfo page-types page_owner_sort thp_swap_allocator_test +gup_bench
tools/mm/Makefile +8 -3
@@ -3,13 +3,15 @@ # include ../scripts/Makefile.include -BUILD_TARGETS=page-types slabinfo page_owner_sort page_owner_filter thp_swap_allocator_test +BUILD_TARGETS=page-types slabinfo page_owner_sort page_owner_filter +BUILD_TARGETS += 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 +24,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 page_owner_filter thp_swap_allocator_test + $(RM) page-types slabinfo page_owner_sort page_owner_filter thp_swap_allocator_test gup_bench make -C $(LIB_DIR) clean sbindir ?= /usr/sbin
tools/mm/gup_bench.c renamed from tools/testing/selftests/mm/gup_test.c +4 -4
@@ -12,8 +12,8 @@#include <pthread.h> #include <assert.h> #include <mm/gup_test.h> -#include "kselftest.h" -#include "vm_util.h" +#include <mm/hugepage_settings.h> +#include "../testing/selftests/kselftest.h" #define MB (1UL << 20)
@@ -140,7 +140,7 @@ int main(int argc, char **argv)case 'n' : nr_pages =atoi (optarg);if (nr_pages <0 ) - nr_pages = size /psize (); + nr_pages = size /getpagesize ();break ;case 't' : thp =1 ;
@@ -254,7 +254,7 @@ int main(int argc, char **argv)madvise (p, size,MADV_NOHUGEPAGE );/* Fault them in here, from user space. */ -for (; (unsigned long )p < gup.addr + size; p +=psize ()) +for (; (unsigned long )p < gup.addr + size; p +=getpagesize ()) p[0 ] =0 ; tid =malloc (sizeof (pthread_t) * nthreads);
tools/testing/selftests/mm/Makefile +0 -1
@@ -59,7 +59,6 @@ endif TEST_GEN_FILES = cow TEST_GEN_FILES += compaction_test TEST_GEN_FILES += gup_longterm -TEST_GEN_FILES += gup_test TEST_GEN_FILES += hmm-tests TEST_GEN_FILES += hugetlb-madvise TEST_GEN_FILES += hugetlb-mmap
tools/testing/selftests/mm/run_vmtests.sh +0 -36
@@ -148,30 +148,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
@@ -293,18 +269,6 @@ 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_longterm CATEGORY="userfaultfd" run_test ./uffd-unit-tests
Signature · 1 line
2.53.0
PATCH v11 5/6 tools/mm: make gup_bench a benchmark only tool
In reply to Sarthak SharmaRemove the functional modes (GUP_BASIC_TEST, PIN_BASIC_TEST and DUMP_USER_PAGES_TEST) from gup_bench. Drop the kselftest dependency and use normal diagnostics and exit statuses.
When no arguments are supplied, run a single GUP_FAST_BENCHMARK with the existing default values. Let users select other configurations through command-line options. Report ioctl failures and handle errors without relying on assert().
--- tools/mm/gup_bench.c | 183 ++++++++++++++++++------------------------- 1 file changed, 78 insertions(+), 105 deletions(-)
tools/mm/gup_bench.c +78 -105
@@ -10,10 +10,10 @@#include <sys/stat.h> #include <sys/types.h> #include <pthread.h> -#include <assert.h> +#include <stdbool.h> +#include <string.h> #include <mm/gup_test.h> #include <mm/hugepage_settings.h> -#include "../testing/selftests/kselftest.h" #define MB (1UL << 20)
@@ -37,12 +37,6 @@ static char *cmd_to_str(unsigned long cmd)return "PIN_FAST_BENCHMARK" ;case PIN_LONGTERM_BENCHMARK :return "PIN_LONGTERM_BENCHMARK" ; -case GUP_BASIC_TEST : -return "GUP_BASIC_TEST" ; -case PIN_BASIC_TEST : -return "PIN_BASIC_TEST" ; -case DUMP_USER_PAGES_TEST : -return "DUMP_USER_PAGES_TEST" ; }return "Unknown command" ; }
@@ -52,39 +46,29 @@ void *gup_thread(void *data)struct gup_test gup = *(struct gup_test *)data;int i, status; -/* Only report timing information on the *_BENCHMARK commands: */ -if ((cmd ==PIN_FAST_BENCHMARK ) || (cmd ==GUP_FAST_BENCHMARK ) || - (cmd ==PIN_LONGTERM_BENCHMARK )) { -for (i =0 ; i < repeats; i++) { - gup.size = size; - status =ioctl (gup_fd, cmd, &gup); -if (status) -break ; +for (i =0 ; i < repeats; i++) { + gup.size = size; + status =ioctl (gup_fd, cmd, &gup); +if (status) { +int err = errno;pthread_mutex_lock (&print_mutex); -ksft_print_msg ("%s: Time: get:%lld put:%lld us" , -cmd_to_str (cmd), gup.get_delta_usec, - gup.put_delta_usec); -if (gup.size != size) -ksft_print_msg (", truncated (size: %lld)" , gup.size); -ksft_print_msg ("\n" ); +fprintf (stderr,"%s ioctl failed: %s\n" ,cmd_to_str (cmd), +strerror (err));pthread_mutex_unlock (&print_mutex); +return data; } - }else { - gup.size = size; - status =ioctl (gup_fd, cmd, &gup); -if (status) - goto return_;pthread_mutex_lock (&print_mutex); -ksft_print_msg ("%s: done\n" ,cmd_to_str (cmd)); +printf ("%s: Time: get:%lld put:%lld us" , +cmd_to_str (cmd), gup.get_delta_usec, + gup.put_delta_usec);if (gup.size != size) -ksft_print_msg ("Truncated (size: %lld)\n" , gup.size); +printf (", truncated (size: %lld)" , gup.size); +printf ("\n" );pthread_mutex_unlock (&print_mutex); } -return_: -ksft_test_result (!status,"ioctl status %d\n" , status);return NULL ; }
@@ -92,38 +76,21 @@ int main(int argc, char **argv) {struct gup_test gup = {0 };int filed, i, opt, nr_pages =1 , thp =-1 , write =1 , nthreads =1 , ret; -int flags =MAP_PRIVATE ; +int flags =MAP_PRIVATE , started_threads =0 , exit_status =1 ;char *file ="/dev/zero" ; -bool hugetlb = false; +bool hugetlb = false, thread_error = false; +void *thread_result;pthread_t *tid;char *p; -while ((opt =getopt (argc, argv,"m:r:n:F:f:abcj:tTLUuwWSHpz" )) !=-1 ) { +while ((opt =getopt (argc, argv,"m:r:n:F:f:aj:tTLuwWSH" )) !=-1 ) {switch (opt) {case 'a' : cmd =PIN_FAST_BENCHMARK ;break ; -case 'b' : - cmd =PIN_BASIC_TEST ; -break ;case 'L' : cmd =PIN_LONGTERM_BENCHMARK ;break ; -case 'c' : - cmd =DUMP_USER_PAGES_TEST ; -/* -* Dump page 0 (index 1). May be overridden later, by -* user's non-option arguments. -* -* .which_pages is zero-based, so that zero can mean "do -* nothing". -*/ - gup.which_pages[0 ] =1 ; -break ; -case 'p' : -/* works only with DUMP_USER_PAGES_TEST */ - gup.test_flags |=GUP_TEST_FLAG_DUMP_PAGES_USE_PIN ; -break ;case 'F' :/* strtol, so you can pass flags in hex form */ gup.gup_flags =strtol (optarg,0 ,0 );
@@ -148,9 +115,6 @@ int main(int argc, char **argv)case 'T' : thp =0 ;break ; -case 'U' : - cmd =GUP_BASIC_TEST ; -break ;case 'u' : cmd =GUP_FAST_BENCHMARK ;break ;
@@ -172,52 +136,41 @@ int main(int argc, char **argv) hugetlb = true;break ;default : -ksft_exit_fail_msg ("Wrong argument\n" ); +fprintf (stderr,"Wrong argument\n" ); +exit (1 ); } } -if (optind < argc) { -int extra_arg_count =0 ; -/* -* For example: -* -* ./gup_test -c 0 1 0x1001 -* -* ...to dump pages 0, 1, and 4097 -*/ - -while ((optind < argc) && - (extra_arg_count <GUP_TEST_MAX_PAGES_TO_DUMP )) { -/* -* Do the 1-based indexing here, so that the user can -* use normal 0-based indexing on the command line. -*/ -long page_index =strtol (argv[optind],0 ,0 ) +1 ; - - gup.which_pages[extra_arg_count] = page_index; - extra_arg_count++; - optind++; - } +if (optind != argc) { +fprintf (stderr,"Unexpected argument '%s'\n" , argv[optind]); +exit (1 ); } -ksft_print_header (); +if (geteuid ()) { +fprintf (stderr,"Please run this test as root\n" ); +exit (1 ); + }if (hugetlb) {unsigned long hp_size =default_huge_page_size (); -if (!hp_size) -ksft_exit_skip ("HugeTLB is unavailable\n" ); +if (!hp_size) { +fprintf (stderr,"Could not determine huge page size\n" ); +return 1 ; + } size = (size + hp_size -1 ) & ~(hp_size -1 ); -if (!hugetlb_setup_default (size / hp_size)) -ksft_exit_skip ("Not enough huge pages\n" ); +if (!hugetlb_setup_default (size / hp_size)) { +fprintf (stderr,"Not enough huge pages\n" ); +return 1 ; + } } -ksft_set_plan (nthreads); - filed =open (file,O_RDWR |O_CREAT ,0664 ); -if (filed <0 ) -ksft_exit_fail_msg ("Unable to open %s: %s\n" , file,strerror (errno)); +if (filed <0 ) { +fprintf (stderr,"Unable to open %s: %s\n" , file,strerror (errno)); +return 1 ; + } gup.nr_pages_per_call = nr_pages;if (write)
@@ -226,26 +179,24 @@ int main(int argc, char **argv) gup_fd =open (GUP_TEST_FILE ,O_RDWR );if (gup_fd ==-1 ) {switch (errno) { -case EACCES : -if (getuid ()) -ksft_print_msg ("Please run this test as root\n" ); -break ;case ENOENT :if (opendir ("/sys/kernel/debug" ) ==NULL ) -ksft_print_msg ("mount debugfs at /sys/kernel/debug\n" ); -ksft_print_msg ("check if CONFIG_GUP_TEST is enabled in kernel config\n" ); +fprintf (stderr,"mount debugfs at /sys/kernel/debug\n" ); +fprintf (stderr,"check if CONFIG_GUP_TEST is enabled in kernel config\n" );break ;default : -ksft_print_msg ("failed to open %s: %s\n" ,GUP_TEST_FILE ,strerror (errno)); +fprintf (stderr,"failed to open %s: %s\n" ,GUP_TEST_FILE , +strerror (errno));break ; } -ksft_test_result_skip ("Please run this test as root\n" ); -ksft_exit_pass (); + goto err_close_filed; } p =mmap (NULL , size,PROT_READ |PROT_WRITE , flags, filed,0 ); -if (p ==MAP_FAILED ) -ksft_exit_fail_msg ("mmap: %s\n" ,strerror (errno)); +if (p ==MAP_FAILED ) { +fprintf (stderr,"mmap: %s\n" ,strerror (errno)); + goto err_close_gup_fd; + } gup.addr = (unsigned long )p;if (thp ==1 )
@@ -258,17 +209,39 @@ int main(int argc, char **argv) p[0 ] =0 ; tid =malloc (sizeof (pthread_t) * nthreads); -assert (tid); +if (!tid) { +fprintf (stderr,"Failed to allocate %d threads: %s\n" , + nthreads,strerror (errno)); + goto err_unmap; + } +for (i =0 ; i < nthreads; i++) { ret =pthread_create (&tid[i],NULL , gup_thread, &gup); -assert (ret ==0 ); +if (ret) { +fprintf (stderr,"pthread_create failed: %s\n" ,strerror (ret)); + thread_error = true; +break ; + } + started_threads++; } -for (i =0 ; i < nthreads; i++) { - ret =pthread_join (tid[i],NULL ); -assert (ret ==0 ); +for (i =0 ; i < started_threads; i++) { + ret =pthread_join (tid[i], &thread_result); +if (ret) { +fprintf (stderr,"pthread_join failed: %s\n" ,strerror (ret)); + thread_error = true; + }else if (thread_result) + thread_error = true; }free (tid); - -ksft_exit_pass (); +if (!thread_error) + exit_status =0 ; + +err_unmap: +munmap ((void *)gup.addr, size); +err_close_gup_fd: +close (gup_fd); +err_close_filed: +close (filed); +return exit_status; }
Signature · 1 line
2.53.0
PATCH v11 6/6 selftests/mm: add a GUP selftest
In reply to Sarthak SharmaAdd a new GUP selftest which uses kselftest_harness.h. Cover 12 mapping configurations: THP enabled, THP disabled and HugeTLB, each across private/shared mappings and with/without FOLL_WRITE. Run 5 test cases for every variant: get_user_pages, get_user_pages_fast, pin_user_pages, pin_user_pages_fast and pin_user_pages_longterm.
Use two default hugeTLB pages and derive the mapping size from their size. This exercises GUP both within a single HugeTLB page and across a HugeTLB boundary, without reserving an excessive number of pages.
Sweep four nr_pages_per_call values for each test: 1, 512, 123 and all pages. This preserves the coverage previously provided by run_gup_matrix(): 12 mapping combinations x 5 GUP/PUP operations x 4 batch sizes. In total the selftest reports 60 TAP cases and issues 240 ioctls.
Do not carry DUMP_USER_PAGES_TEST into the new selftest because its output is written to the kernel log and the selftest does not verify that output.
Add the new gup binary to the selftests/mm build, run_vmtests.sh and MAINTAINERS. Update mm/Kconfig to describe the benchmark and selftest split.
--- MAINTAINERS | 1 + mm/Kconfig | 19 +- tools/testing/selftests/mm/Makefile | 1 + tools/testing/selftests/mm/gup.c | 262 ++++++++++++++++++++++ tools/testing/selftests/mm/run_vmtests.sh | 1 + 5 files changed, 272 insertions(+), 12 deletions(-) create mode 100644 tools/testing/selftests/mm/gup.c
MAINTAINERS +1 -0
@@ -17188,6 +17188,7 @@ 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.c F: tools/testing/selftests/mm/gup_longterm.c MEMORY MANAGEMENT - KSM (Kernel Samepage Merging)
mm/Kconfig +7 -12
@@ -1292,24 +1292,19 @@ config PERCPU_STATS be used to help understand percpu memory usage. config GUP_TEST - bool "Enable infrastructure for get_user_pages()-related unit tests" + bool "Enable infrastructure for get_user_pages()-related unit tests and benchmarks" depends on DEBUG_FS help Provides /sys/kernel/debug/gup_test, which in turn provides a way - to make ioctl calls that can launch kernel-based unit tests for - the get_user_pages*() and pin_user_pages*() family of API calls. + to make ioctl calls that can launch kernel-based unit tests and + benchmarks for the get_user_pages*() and pin_user_pages*() families + of API calls. - These tests include benchmark testing of the _fast variants of - get_user_pages*() and pin_user_pages*(), as well as smoke tests of + These include benchmark testing of the _fast variants of + get_user_pages*() and pin_user_pages*(), as well as tests of the non-_fast variants. - There is also a sub-test that allows running dump_page() on any - of up to eight pages (selected by command line args) within the - range of user-space addresses. These pages are either pinned via - pin_user_pages*(), or pinned via get_user_pages*(), as specified - by other command line arguments. - - See tools/testing/selftests/mm/gup_test.c + See tools/testing/selftests/mm/gup.c and tools/mm/gup_bench.c. comment "GUP_TEST needs to have DEBUG_FS enabled" depends on !GUP_TEST && !DEBUG_FS
tools/testing/selftests/mm/Makefile +1 -0
@@ -58,6 +58,7 @@ endif TEST_GEN_FILES = cow TEST_GEN_FILES += compaction_test +TEST_GEN_FILES += gup TEST_GEN_FILES += gup_longterm TEST_GEN_FILES += hmm-tests TEST_GEN_FILES += hugetlb-madvise
tools/testing/selftests/mm/gup.c new file +262 -0
262 lines not on this page · raw message
tools/testing/selftests/mm/run_vmtests.sh +1 -0
@@ -269,6 +269,7 @@ fi CATEGORY="mmap" run_test ./map_fixed_noreplace +CATEGORY="gup_test" run_test ./gup CATEGORY="gup_test" run_test ./gup_longterm CATEGORY="userfaultfd" run_test ./uffd-unit-tests
Signature · 1 line
2.53.0
Andrew Morton
maintainerQuoted 11 lines · On Fri, 18 Sep 2026 16:52:28 +0530 Sarthak Sharma <[email protected]>…
gup_test.ccurrently serves two separate purposes: benchmarking (GUP_FAST_BENCHMARK,PIN_FAST_BENCHMARKandPIN_LONGTERM_BENCHMARK) and functional testing (GUP_BASIC_TEST,PIN_BASIC_TESTandDUMP_USER_PAGES_TEST). Keeping both in one program makes the functional tests harder to run and report individually, whilerun_vmtests.shhas to invoke the program repeatedly with different options.Separate these roles into
tools/mm/gup_benchfor benchmarking andtools/testing/selftests/mm/gupfor functional testing. Move the shared file and hugepage helpers totools/lib/mm/so both programs can use them without duplicating the implementation.
Thanks, all queued up. Including [1/6]'s read_num() fix.