selftests/mm: pass filename as input param to VM_PFNMAP tests
Subsystems (3)
PATCH v3 1/1 selftests/mm: pass filename as input param to VM_PFNMAP tests
Enable these tests to be run on other pfnmap'ed memory like NVIDIA's EGM.
Add '--' as a separator to pass in file path. This allows passing of cmd line arguments to kselftest_harness. Use '/dev/mem' as default filename.
Existing test passes:
pfnmap
TAP version 13
1..6
# Starting 6 tests from 1 test cases.
# PASSED: 6 / 6 tests passed.
# Totals: pass:6 fail:0 xfail:0 xpass:0 skip:0 error:0
Pass params to kselftest_harness:
pfnmap -r pfnmap:mremap_fixed
TAP version 13
1..1
# Starting 1 tests from 1 test cases.
# RUN pfnmap.mremap_fixed ...
# OK pfnmap.mremap_fixed
ok 1 pfnmap.mremap_fixed
# PASSED: 1 / 1 tests passed.
# Totals: pass:1 fail:0 xfail:0 xpass:0 skip:0 error:0
Pass non-existent file name as input:
pfnmap -- /dev/blah
TAP version 13
1..6
# Starting 6 tests from 1 test cases.
# RUN pfnmap.madvise_disallowed ...
# SKIP Cannot open '/dev/blah'
Pass non pfnmap'ed file as input:
pfnmap -r pfnmap.madvise_disallowed -- randfile.txt TAP version 13
1..1
# Starting 1 tests from 1 test cases.
# RUN pfnmap.madvise_disallowed ...
# SKIP Invalid file: 'randfile.txt'. Not pfnmap'ed
---
v2 -> v3:
* Add check_vmflag_pfnmap func
* Re-use existing check_vmflag_io func
* Verify pfnmap using mmap addr
* Rename phys_addr to offset
v1 -> v2:
* Add verify_pfnmap func to sanity check the input param
* mmap with zero offset if filename != '/dev/mem'
--- tools/testing/selftests/mm/pfnmap.c | 48 ++++++++++++++++++++-------- tools/testing/selftests/mm/vm_util.c | 14 ++++++-- tools/testing/selftests/mm/vm_util.h | 1 + 3 files changed, 47 insertions(+), 16 deletions(-)
tools/testing/selftests/mm/pfnmap.c +34 -14
@@ -1,6 +1,7 @@// SPDX-License-Identifier: GPL-2.0-only /* -* Basic VM_PFNMAP tests relying on mmap() of '/dev/mem' +* Basic VM_PFNMAP tests relying on mmap() of input file provided. +* Use '/dev/mem' as default. * * Copyright 2025, Red Hat, Inc. *
@@ -25,6 +26,7 @@#include "vm_util.h" static sigjmp_buf sigjmp_buf_env; +static char *file ="/dev/mem" ;static void signal_handler (int sig) {
@@ -51,7 +53,7 @@ static int test_read_access(char *addr, size_t size, size_t pagesize)return ret; } -static int find_ram_target (off_t *phys_addr, +static int find_ram_target (off_t *offset,unsigned long long pagesize) {unsigned long long start, end;
@@ -91,7 +93,7 @@ static int find_ram_target(off_t *phys_addr,/* We need two pages. */ if (end > start +2 * pagesize) {fclose (file); - *phys_addr = start; + *offset = start;return 0 ; } }
@@ -100,7 +102,7 @@ static int find_ram_target(off_t *phys_addr,FIXTURE (pfnmap) { -off_t phys_addr; +off_t offset;size_t pagesize;int dev_mem_fd;char *addr1;
@@ -113,23 +115,31 @@ FIXTURE_SETUP(pfnmap) { self->pagesize =getpagesize (); -/* We'll require two physical pages throughout our tests ... */ -if (find_ram_target (&self->phys_addr, self->pagesize)) -SKIP (return,"Cannot find ram target in '/proc/iomem'\n" ); +if (strncmp (file,"/dev/mem" ,strlen ("/dev/mem" )) ==0 ) { +/* We'll require two physical pages throughout our tests ... */ +if (find_ram_target (&self->offset, self->pagesize)) +SKIP (return, +"Cannot find ram target in '/proc/iomem'\n" ); + }else { + self->offset =0 ; + } - self->dev_mem_fd =open ("/dev/mem" ,O_RDONLY ); + self->dev_mem_fd =open (file,O_RDONLY );if (self->dev_mem_fd <0 ) -SKIP (return,"Cannot open '/dev/mem'\n" ); +SKIP (return,"Cannot open '%s'\n" , file); self->size1 = self->pagesize *2 ; self->addr1 =mmap (NULL , self->size1,PROT_READ ,MAP_SHARED , - self->dev_mem_fd, self->phys_addr); + self->dev_mem_fd, self->offset);if (self->addr1 ==MAP_FAILED ) -SKIP (return,"Cannot mmap '/dev/mem'\n" ); +SKIP (return,"Cannot mmap '%s'\n" , file); + +if (!check_vmflag_pfnmap (self->addr1)) +SKIP (return,"Invalid file: '%s'. Not pfnmap'ed\n" , file);/* ... and want to be able to read from them. */ if (test_read_access (self->addr1, self->size1, self->pagesize)) -SKIP (return,"Cannot read-access mmap'ed '/dev/mem'\n" ); +SKIP (return,"Cannot read-access mmap'ed '%s'\n" , file); self->size2 =0 ; self->addr2 =MAP_FAILED ;
@@ -182,7 +192,7 @@ TEST_F(pfnmap, munmap_split)*/ self->size2 = self->pagesize; self->addr2 =mmap (NULL , self->pagesize,PROT_READ ,MAP_SHARED , - self->dev_mem_fd, self->phys_addr); + self->dev_mem_fd, self->offset);ASSERT_NE (self->addr2,MAP_FAILED ); }
@@ -246,4 +256,14 @@ TEST_F(pfnmap, fork)ASSERT_EQ (ret,0 ); } -TEST_HARNESS_MAIN +int main (int argc,char **argv) +{ +for (int i =1 ; i < argc; i++) { +if (strcmp (argv[i],"--" ) ==0 ) { +if (i +1 < argc &&strlen (argv[i +1 ]) >0 ) + file = argv[i +1 ]; +return test_harness_run (i, argv); + } + } +return test_harness_run (argc, argv); +}
tools/testing/selftests/mm/vm_util.c +12 -2
@@ -402,7 +402,7 @@ unsigned long get_free_hugepages(void)return fhp; } -bool check_vmflag_io (void *addr) +static bool check_vmflag (void *addr,const char *flag) {char buffer[MAX_LINE_LENGTH ];const char *flags;
@@ -419,13 +419,23 @@ bool check_vmflag_io(void *addr)if (!flaglen)return false; -if (flaglen ==strlen ("io" ) && !memcmp (flags,"io" , flaglen)) +if (flaglen ==strlen (flag) && !memcmp (flags, flag, flaglen))return true; flags += flaglen; } } +bool check_vmflag_io (void *addr) +{ +return check_vmflag (addr,"io" ); +} + +bool check_vmflag_pfnmap (void *addr) +{ +return check_vmflag (addr,"pf" ); +} +/* * Open an fd at /proc/$pid/maps and configure procmap_out ready for * PROCMAP_QUERY query. Returns 0 on success, or an error code otherwise.
tools/testing/selftests/mm/vm_util.h +1 -0
@@ -84,6 +84,7 @@ int uffd_register_with_ioctls(int uffd, void *addr, uint64_t len, bool miss, bool wp, bool minor, uint64_t *ioctls);unsigned long get_free_hugepages (void );bool check_vmflag_io (void *addr); +bool check_vmflag_pfnmap (void *addr);int open_procmap (pid_t pid,struct procmap_fd *procmap_out);int query_procmap (struct procmap_fd *procmap);bool find_vma_procmap (struct procmap_fd *procmap,void *address);
Signature · 1 line
2.50.1.565.gc32cd1483b-goog
Quoted 42 lines · Sudarsan Mahendran
Enable these tests to be run on other pfnmap'ed memory like NVIDIA's EGM.
Add '--' as a separator to pass in file path. This allows passing of cmd line arguments to
kselftest_harness. Use '/dev/mem' as default filename.Existing test passes:
pfnmap
TAP version 13
1..6
# Starting 6 tests from 1 test cases.
# PASSED: 6 / 6 tests passed.
# Totals: pass:6 fail:0 xfail:0 xpass:0 skip:0 error:0Pass params to
kselftest_harness:
pfnmap -r pfnmap:mremap_fixed
TAP version 13
1..1
# Starting 1 tests from 1 test cases.
# RUN pfnmap.mremap_fixed ...
# OK pfnmap.mremap_fixed
ok 1 pfnmap.mremap_fixed
# PASSED: 1 / 1 tests passed.
# Totals: pass:1 fail:0 xfail:0 xpass:0 skip:0 error:0Pass non-existent file name as input:
pfnmap --/dev/blah
TAP version 13
1..6
# Starting 6 tests from 1 test cases.
# RUN pfnmap.madvise_disallowed ...
# SKIP Cannot open '/dev/blah'Pass non pfnmap'ed file as input:
pfnmap -r pfnmap.madvise_disallowed -- randfile.txt TAP version 13
1..1
# Starting 1 tests from 1 test cases.
# RUN pfnmap.madvise_disallowed ...
# SKIP Invalid file: 'randfile.txt'. Not pfnmap'ed
Quoted 2 lines
Signed-off-by Sudarsan Mahendran <[email protected]>---
Thanks!
Signature · 3 lines
Cheers, David / dhildenb
Thanks David for your insightful review comments.
Quoted 56 lines · David Hildenbrand
Quoted 42 lines · Sudarsan Mahendran
Enable these tests to be run on other pfnmap'ed memory like NVIDIA's EGM.
Add '--' as a separator to pass in file path. This allows passing of cmd line arguments to
kselftest_harness. Use '/dev/mem' as default filename.Existing test passes:
pfnmap
TAP version 13
1..6
# Starting 6 tests from 1 test cases.
# PASSED: 6 / 6 tests passed.
# Totals: pass:6 fail:0 xfail:0 xpass:0 skip:0 error:0Pass params to
kselftest_harness:
pfnmap -r pfnmap:mremap_fixed
TAP version 13
1..1
# Starting 1 tests from 1 test cases.
# RUN pfnmap.mremap_fixed ...
# OK pfnmap.mremap_fixed
ok 1 pfnmap.mremap_fixed
# PASSED: 1 / 1 tests passed.
# Totals: pass:1 fail:0 xfail:0 xpass:0 skip:0 error:0Pass non-existent file name as input:
pfnmap --/dev/blah
TAP version 13
1..6
# Starting 6 tests from 1 test cases.
# RUN pfnmap.madvise_disallowed ...
# SKIP Cannot open '/dev/blah'Pass non pfnmap'ed file as input:
pfnmap -r pfnmap.madvise_disallowed -- randfile.txt TAP version 13
1..1
# Starting 1 tests from 1 test cases.
# RUN pfnmap.madvise_disallowed ...
# SKIP Invalid file: 'randfile.txt'. Not pfnmap'edQuoted 2 lines
Signed-off-by Sudarsan Mahendran <[email protected]>---
Thanks!
--
Cheers,David / dhildenb
Thanks David for your insightful review comments.
Quoted 56 lines · David Hildenbrand
Quoted 42 lines · Sudarsan Mahendran
Enable these tests to be run on other pfnmap'ed memory like NVIDIA's EGM.
Add '--' as a separator to pass in file path. This allows passing of cmd line arguments to
kselftest_harness. Use '/dev/mem' as default filename.Existing test passes:
pfnmap
TAP version 13
1..6
# Starting 6 tests from 1 test cases.
# PASSED: 6 / 6 tests passed.
# Totals: pass:6 fail:0 xfail:0 xpass:0 skip:0 error:0Pass params to
kselftest_harness:
pfnmap -r pfnmap:mremap_fixed
TAP version 13
1..1
# Starting 1 tests from 1 test cases.
# RUN pfnmap.mremap_fixed ...
# OK pfnmap.mremap_fixed
ok 1 pfnmap.mremap_fixed
# PASSED: 1 / 1 tests passed.
# Totals: pass:1 fail:0 xfail:0 xpass:0 skip:0 error:0Pass non-existent file name as input:
pfnmap --/dev/blah
TAP version 13
1..6
# Starting 6 tests from 1 test cases.
# RUN pfnmap.madvise_disallowed ...
# SKIP Cannot open '/dev/blah'Pass non pfnmap'ed file as input:
pfnmap -r pfnmap.madvise_disallowed -- randfile.txt TAP version 13
1..1
# Starting 1 tests from 1 test cases.
# RUN pfnmap.madvise_disallowed ...
# SKIP Invalid file: 'randfile.txt'. Not pfnmap'edQuoted 2 lines
Signed-off-by Sudarsan Mahendran <[email protected]>---
Thanks!
--
Cheers,David / dhildenb