selftests/mm: add simple VM_PFNMAP tests based on mmap'ing /dev/mem
authorDavid Hildenbrand <david@redhat.com>
Fri, 9 May 2025 15:30:32 +0000 (17:30 +0200)
committerAndrew Morton <akpm@linux-foundation.org>
Thu, 22 May 2025 21:55:36 +0000 (14:55 -0700)
commit2616b370323a953c437ed2bf40a277e9deaa3709
tree8a8b89481253fa41984fc4c55f305924fcf4b38f
parent3f12680913fda8de06c21e836dd5f246fe1684e5
selftests/mm: add simple VM_PFNMAP tests based on mmap'ing /dev/mem

Let's test some basic functionality using /dev/mem.  These tests will
implicitly cover some PAT (Page Attribute Handling) handling on x86.

These tests will only run when /dev/mem access to the first two pages in
physical address space is possible and allowed; otherwise, the tests are
skipped.

On current x86-64 with PAT inside a VM, all tests pass:

TAP version 13
1..6
# Starting 6 tests from 1 test cases.
#  RUN           pfnmap.madvise_disallowed ...
#            OK  pfnmap.madvise_disallowed
ok 1 pfnmap.madvise_disallowed
#  RUN           pfnmap.munmap_split ...
#            OK  pfnmap.munmap_split
ok 2 pfnmap.munmap_split
#  RUN           pfnmap.mremap_fixed ...
#            OK  pfnmap.mremap_fixed
ok 3 pfnmap.mremap_fixed
#  RUN           pfnmap.mremap_shrink ...
#            OK  pfnmap.mremap_shrink
ok 4 pfnmap.mremap_shrink
#  RUN           pfnmap.mremap_expand ...
#            OK  pfnmap.mremap_expand
ok 5 pfnmap.mremap_expand
#  RUN           pfnmap.fork ...
#            OK  pfnmap.fork
ok 6 pfnmap.fork
# PASSED: 6 / 6 tests passed.
# Totals: pass:6 fail:0 xfail:0 xpass:0 skip:0 error:0

However, we are able to trigger:

[   27.888251] x86/PAT: pfnmap:1790 freeing invalid memtype [mem 0x00000000-0x00000fff]

There are probably more things worth testing in the future, such as
MAP_PRIVATE handling.  But this set of tests is sufficient to cover most
of the things we will rework regarding PAT handling.

Link: https://lkml.kernel.org/r/20250509153033.952746-1-david@redhat.com
Signed-off-by: David Hildenbrand <david@redhat.com>
Reviewed-by: Lorenzo Stoakes <lorenzo.stoakes@oracle.com>
Cc: Shuah Khan <shuah@kernel.org>
Cc: Ingo Molnar <mingo@redhat.com>
Cc: Peter Xu <peterx@redhat.com>
Cc: Dev Jain <dev.jain@arm.com>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
tools/testing/selftests/mm/.gitignore
tools/testing/selftests/mm/Makefile
tools/testing/selftests/mm/pfnmap.c [new file with mode: 0644]
tools/testing/selftests/mm/run_vmtests.sh