selftests/vm: Add tests validating mremap mirror functionality
Subsystems (2)
PATCH v3 1/1 selftests/vm: Add tests validating mremap mirror functionality
This adds two tests to validate mirror functionality with mremap() system call on shared and private anon mappings. After the commit dba58d3b8c5 ("mm/mremap: fail map duplication attempts for private mappings"), any attempt to mirror private anon mapping will fail.
---
Changes in V3:
- Fail any attempts to mirror an existing anon private mapping
- Updated run_vmtests to include these new mremap tests
- Updated the commit message
Changes in V2: (https://patchwork.kernel.org/patch/9861259/)
- Added a test for private anon mappings
- Used sysconf(_SC_PAGESIZE) instead of hard coding page size
- Used MREMAP_MAYMOVE instead of hard coding the flag value 1
Original V1: (https://patchwork.kernel.org/patch/9854415/)
tools/testing/selftests/vm/Makefile | 2 + .../selftests/vm/mremap_mirror_private_anon.c | 41 +++++++++++++++ .../selftests/vm/mremap_mirror_shared_anon.c | 58 ++++++++++++++++++++++ tools/testing/selftests/vm/run_vmtests | 22 ++++++++ 4 files changed, 123 insertions(+) create mode 100644 tools/testing/selftests/vm/mremap_mirror_private_anon.c create mode 100644 tools/testing/selftests/vm/mremap_mirror_shared_anon.c
tools/testing/selftests/vm/Makefile +2 -0
@@ -17,6 +17,8 @@ TEST_GEN_FILES += transhuge-stress TEST_GEN_FILES += userfaultfd TEST_GEN_FILES += mlock-random-test TEST_GEN_FILES += virtual_address_range +TEST_GEN_FILES += mremap_mirror_shared_anon +TEST_GEN_FILES += mremap_mirror_private_anon TEST_PROGS := run_vmtests
tools/testing/selftests/vm/mremap_mirror_private_anon.c new file +41 -0
41 lines not on this page · raw message
tools/testing/selftests/vm/mremap_mirror_shared_anon.c new file +58 -0
58 lines not on this page · raw message
tools/testing/selftests/vm/run_vmtests +22 -0
@@ -176,4 +176,26 @@ else echo "[PASS]" fi +echo "-----------------------------" +echo "mremap_mirror_private_anon" +echo "-----------------------------" +./mremap_mirror_private_anon +if [ $? -ne 0 ]; then + echo "[FAIL]" + exitcode=1 +else + echo "[PASS]" +fi + +echo "-----------------------------" +echo "mremap_mirror_shared_anon" +echo "-----------------------------" +./mremap_mirror_shared_anon +if [ $? -ne 0 ]; then + echo "[FAIL]" + exitcode=1 +else + echo "[PASS]" +fi + exit $exitcode
Signature · 1 line
1.8.5.2
Quoted 7 lines · Anshuman Khandual
This adds two tests to validate mirror functionality with
mremap()system call on shared and private anon mappings. After the commitdba58d3b8c5("mm/mremap: fail map duplication attempts for private mappings"), any attempt to mirror private anon mapping will fail.Suggested-by Mike Kravetz <[email protected]>Signed-off-by Anshuman Khandual <[email protected]>
The tests themselves look fine. However, they are pretty simple and could very easily be combined into one 'mremap_mirror.c' file. I would prefer that they be combined, but it is not a deal breaker.
Quoted 180 lines
---
Changes in V3:
- Fail any attempts to mirror an existing anon private mapping
- Updatedrun_vmteststo include these new mremap tests
- Updated the commit messageChanges in V2: (https://patchwork.kernel.org/patch/9861259/)
- Added a test for private anon mappings
- Usedsysconf(_SC_PAGESIZE)instead of hard coding page size
- UsedMREMAP_MAYMOVEinstead of hard coding the flag value 1Original V1: (https://patchwork.kernel.org/patch/9854415/)
tools/testing/selftests/vm/Makefile | 2 + .../selftests/vm/mremap_mirror_private_anon.c | 41 +++++++++++++++ .../selftests/vm/mremap_mirror_shared_anon.c | 58 ++++++++++++++++++++++ tools/testing/selftests/vm/run_vmtests | 22 ++++++++ 4 files changed, 123 insertions(+) create mode 100644 tools/testing/selftests/vm/mremap_mirror_private_anon.c create mode 100644 tools/testing/selftests/vm/mremap_mirror_shared_anon.ctools/testing/selftests/vm/Makefile +2
-0@@ -17,6 +17,8 @@ TEST_GEN_FILES += transhuge-stress TEST_GEN_FILES += userfaultfd TEST_GEN_FILES += mlock-random-test TEST_GEN_FILES += virtual_address_range +TEST_GEN_FILES += mremap_mirror_shared_anon +TEST_GEN_FILES += mremap_mirror_private_anon TEST_PROGS := run_vmteststools/testing/selftests/vm/mremap_mirror_private_anon.c new file +41
-0@@ -0,0 +1,41 @@ +/* +* Test to verify mirror functionality with mremap() system +* call for private anon mappings. Any attempt to create a +* mirror mapping for an anon private one should fail. +* +* Copyright (C) 2017 Anshuman Khandual, IBM Corporation +* +* Licensed under GPL V2 +*/ +#define _GNU_SOURCE +#include <stdio.h> +#include <string.h> +#include <unistd.h> +#include <errno.h> +#include <sys/mman.h> +#include <sys/time.h> + +#define PATTERN 0xbe +#define NR_PAGES 10 + +int main (int argc,char *argv[]) +{ +unsigned long alloc_size; +char *ptr, *mirror_ptr; + + alloc_size =sysconf (_SC_PAGESIZE) *NR_PAGES ; + ptr =mmap (NULL , alloc_size,PROT_READ |PROT_WRITE , +MAP_PRIVATE |MAP_ANONYMOUS ,-1 ,0 ); +if (ptr ==MAP_FAILED ) { +perror ("map() failed" ); +return -1 ; + } +memset (ptr,PATTERN , alloc_size); + + mirror_ptr = (char *)mremap (ptr,0 , alloc_size,MREMAP_MAYMOVE ); +if (mirror_ptr ==MAP_FAILED ) +return 0 ; + +printf ("Mirror attempt on private anon mapping should have failed\n" ); +return 1 ; +}tools/testing/selftests/vm/mremap_mirror_shared_anon.c new file +58
-0@@ -0,0 +1,58 @@ +/* +* Test to verify mirror functionality with mremap() system +* call for shared anon mappings. The 'mirrored' buffer will +* match element to element with that of the original one. +* +* Copyright (C) 2017 Anshuman Khandual, IBM Corporation +* +* Licensed under GPL V2 +*/ +#define _GNU_SOURCE +#include <stdio.h> +#include <string.h> +#include <unistd.h> +#include <errno.h> +#include <sys/mman.h> +#include <sys/time.h> + +#define PATTERN 0xbe +#define NR_PAGES 10 + +int test_mirror (char *old,char *new,unsigned long size) +{ +unsigned long i; + +for (i =0 ; i < size; i++) { +if (new[i] != old[i]) { +printf ("Mismatch at new[%lu] expected \ +%d received %d\n" , i, old[i], new[i]); +return 1 ; + } + } +return 0 ; +} + +int main (int argc,char *argv[]) +{ +unsigned long alloc_size; +char *ptr, *mirror_ptr; + + alloc_size =sysconf (_SC_PAGESIZE) *NR_PAGES ; + ptr =mmap (NULL , alloc_size,PROT_READ |PROT_WRITE , +MAP_SHARED |MAP_ANONYMOUS ,-1 ,0 ); +if (ptr ==MAP_FAILED ) { +perror ("map() failed" ); +return -1 ; + } +memset (ptr,PATTERN , alloc_size); + + mirror_ptr = (char *)mremap (ptr,0 , alloc_size,MREMAP_MAYMOVE ); +if (mirror_ptr ==MAP_FAILED ) { +perror ("mremap() failed" ); +return -1 ; + } + +if (test_mirror (ptr, mirror_ptr, alloc_size)) +return 1 ; +return 0 ; +}tools/testing/selftests/vm/run_vmtests +22
-0@@ -176,4 +176,26 @@ else echo "[PASS]" fi +echo "-----------------------------" +echo "mremap_mirror_private_anon" +echo "-----------------------------" +./mremap_mirror_private_anon +if [ $? -ne 0 ]; then + echo "[FAIL]" + exitcode=1 +else + echo "[PASS]" +fi + +echo "-----------------------------" +echo "mremap_mirror_shared_anon" +echo "-----------------------------" +./mremap_mirror_shared_anon +if [ $? -ne 0 ]; then + echo "[FAIL]" + exitcode=1 +else + echo "[PASS]" +fi + exit $exitcode
Signature · 7 lines
Mike Kravetz -- To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM, see: http://www.linux-mm.org/ . Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
Quoted 6 lines · Mike Kravetz
Quoted 5 lines · Anshuman Khandual
This adds two tests to validate mirror functionality with
mremap()system call on shared and private anon mappings. After the commitdba58d3b8c5("mm/mremap: fail map duplication attempts for private mappings"), any attempt to mirror private anon mapping will fail.
Quoted 8 lines
Quoted 2 lines
Suggested-by Mike Kravetz <[email protected]>Signed-off-by Anshuman Khandual <[email protected]>The tests themselves look fine. However, they are pretty simple and could very easily be combined into one '
mremap_mirror.c' file. I would prefer that they be combined, but it is not a deal breaker.Reviewed-by Mike Kravetz <[email protected]>
Hello Andrew/Shuah,
Is this okay or should I resend this patch with both tests folded into one test case file ?
- Anshuman