* [PATCH] selftests/vm: Add tests validating mremap mirror functionality
@ 2017-10-30 3:18 ` Anshuman Khandual
0 siblings, 0 replies; 4+ messages in thread
From: Anshuman Khandual @ 2017-10-30 3:18 UTC (permalink / raw)
To: linux-mm, linux-kernel; +Cc: mike.kravetz, mhocko, shuahkh
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.
Suggested-by: Mike Kravetz <mike.kravetz@oracle.com>
Signed-off-by: Anshuman Khandual <khandual@linux.vnet.ibm.com>
Reviewed-by: Mike Kravetz <mike.kravetz@oracle.com>
---
Changes in V4:
- Folded these two test files into just one as per Mike
- Did some renaming of functions, cleans ups etc
Changes in V3: (https://patchwork.kernel.org/patch/10013469/)
- 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 | 1 +
tools/testing/selftests/vm/mremap_mirror.c | 96 ++++++++++++++++++++++++++++++
tools/testing/selftests/vm/run_vmtests | 10 ++++
3 files changed, 107 insertions(+)
create mode 100644 tools/testing/selftests/vm/mremap_mirror.c
diff --git a/tools/testing/selftests/vm/Makefile b/tools/testing/selftests/vm/Makefile
index cbb29e4..27f1967 100644
--- a/tools/testing/selftests/vm/Makefile
+++ b/tools/testing/selftests/vm/Makefile
@@ -17,6 +17,7 @@ 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
TEST_PROGS := run_vmtests
diff --git a/tools/testing/selftests/vm/mremap_mirror.c b/tools/testing/selftests/vm/mremap_mirror.c
new file mode 100644
index 0000000..41cdba5
--- /dev/null
+++ b/tools/testing/selftests/vm/mremap_mirror.c
@@ -0,0 +1,96 @@
+/*
+ * Test to verify mirror functionality with mremap() system
+ * call for shared and private anon mappings. In shared anon
+ * mapping case, the 'mirrored' buffer will match element to
+ * element with that of the original one. But any attempts
+ * to create a mirror buffer for an anon private one should
+ * just 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 test_mirror_shared(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 mirror_anon_shared(unsigned long alloc_size)
+{
+ char *ptr, *mirror_ptr;
+
+ 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_shared(ptr, mirror_ptr, alloc_size))
+ return 1;
+
+ return 0;
+}
+
+int mirror_anon_private(unsigned long alloc_size)
+{
+ char *ptr, *mirror_ptr;
+
+ 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;
+}
+
+int main(int argc, char *argv[])
+{
+ unsigned long alloc_size;
+ int ret;
+
+ alloc_size = sysconf(_SC_PAGESIZE) * NR_PAGES;
+ ret = mirror_anon_private(alloc_size);
+ if (ret)
+ return ret;
+
+ ret = mirror_anon_shared(alloc_size);
+ if (ret)
+ return ret;
+ return 0;
+}
diff --git a/tools/testing/selftests/vm/run_vmtests b/tools/testing/selftests/vm/run_vmtests
index 07548a1..7214aa2 100755
--- a/tools/testing/selftests/vm/run_vmtests
+++ b/tools/testing/selftests/vm/run_vmtests
@@ -176,4 +176,14 @@ else
echo "[PASS]"
fi
+echo "-----------------------------"
+echo "mremap_mirror"
+echo "-----------------------------"
+./mremap_mirror
+if [ $? -ne 0 ]; then
+ echo "[FAIL]"
+ exitcode=1
+else
+ echo "[PASS]"
+fi
exit $exitcode
--
1.8.5.2
^ permalink raw reply related [flat|nested] 4+ messages in thread
* [PATCH] selftests/vm: Add tests validating mremap mirror functionality
@ 2017-10-30 3:18 ` Anshuman Khandual
0 siblings, 0 replies; 4+ messages in thread
From: Anshuman Khandual @ 2017-10-30 3:18 UTC (permalink / raw)
To: linux-mm, linux-kernel; +Cc: mike.kravetz, mhocko, shuahkh
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.
Suggested-by: Mike Kravetz <mike.kravetz@oracle.com>
Signed-off-by: Anshuman Khandual <khandual@linux.vnet.ibm.com>
Reviewed-by: Mike Kravetz <mike.kravetz@oracle.com>
---
Changes in V4:
- Folded these two test files into just one as per Mike
- Did some renaming of functions, cleans ups etc
Changes in V3: (https://patchwork.kernel.org/patch/10013469/)
- 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 | 1 +
tools/testing/selftests/vm/mremap_mirror.c | 96 ++++++++++++++++++++++++++++++
tools/testing/selftests/vm/run_vmtests | 10 ++++
3 files changed, 107 insertions(+)
create mode 100644 tools/testing/selftests/vm/mremap_mirror.c
diff --git a/tools/testing/selftests/vm/Makefile b/tools/testing/selftests/vm/Makefile
index cbb29e4..27f1967 100644
--- a/tools/testing/selftests/vm/Makefile
+++ b/tools/testing/selftests/vm/Makefile
@@ -17,6 +17,7 @@ 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
TEST_PROGS := run_vmtests
diff --git a/tools/testing/selftests/vm/mremap_mirror.c b/tools/testing/selftests/vm/mremap_mirror.c
new file mode 100644
index 0000000..41cdba5
--- /dev/null
+++ b/tools/testing/selftests/vm/mremap_mirror.c
@@ -0,0 +1,96 @@
+/*
+ * Test to verify mirror functionality with mremap() system
+ * call for shared and private anon mappings. In shared anon
+ * mapping case, the 'mirrored' buffer will match element to
+ * element with that of the original one. But any attempts
+ * to create a mirror buffer for an anon private one should
+ * just 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 test_mirror_shared(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 mirror_anon_shared(unsigned long alloc_size)
+{
+ char *ptr, *mirror_ptr;
+
+ 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_shared(ptr, mirror_ptr, alloc_size))
+ return 1;
+
+ return 0;
+}
+
+int mirror_anon_private(unsigned long alloc_size)
+{
+ char *ptr, *mirror_ptr;
+
+ 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;
+}
+
+int main(int argc, char *argv[])
+{
+ unsigned long alloc_size;
+ int ret;
+
+ alloc_size = sysconf(_SC_PAGESIZE) * NR_PAGES;
+ ret = mirror_anon_private(alloc_size);
+ if (ret)
+ return ret;
+
+ ret = mirror_anon_shared(alloc_size);
+ if (ret)
+ return ret;
+ return 0;
+}
diff --git a/tools/testing/selftests/vm/run_vmtests b/tools/testing/selftests/vm/run_vmtests
index 07548a1..7214aa2 100755
--- a/tools/testing/selftests/vm/run_vmtests
+++ b/tools/testing/selftests/vm/run_vmtests
@@ -176,4 +176,14 @@ else
echo "[PASS]"
fi
+echo "-----------------------------"
+echo "mremap_mirror"
+echo "-----------------------------"
+./mremap_mirror
+if [ $? -ne 0 ]; then
+ echo "[FAIL]"
+ exitcode=1
+else
+ echo "[PASS]"
+fi
exit $exitcode
--
1.8.5.2
--
To unsubscribe, send a message with 'unsubscribe linux-mm' in
the body to majordomo@kvack.org. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"dont@kvack.org"> email@kvack.org </a>
^ permalink raw reply related [flat|nested] 4+ messages in thread
* Re: [PATCH] selftests/vm: Add tests validating mremap mirror functionality
2017-10-30 3:18 ` Anshuman Khandual
@ 2017-11-01 21:35 ` Shuah Khan
-1 siblings, 0 replies; 4+ messages in thread
From: Shuah Khan @ 2017-11-01 21:35 UTC (permalink / raw)
To: Anshuman Khandual, linux-mm, linux-kernel
Cc: mike.kravetz, mhocko, Shuah Khan, Shuah Khan
On 10/29/2017 09:18 PM, Anshuman Khandual wrote:
> 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.
>
> Suggested-by: Mike Kravetz <mike.kravetz@oracle.com>
> Signed-off-by: Anshuman Khandual <khandual@linux.vnet.ibm.com>
> Reviewed-by: Mike Kravetz <mike.kravetz@oracle.com>
> ---
> Changes in V4:
>
> - Folded these two test files into just one as per Mike
> - Did some renaming of functions, cleans ups etc
>
> Changes in V3: (https://patchwork.kernel.org/patch/10013469/)
>
> - 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/)
Thanks. I will queue this up for 4.15-rc1
-- Shuah
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: [PATCH] selftests/vm: Add tests validating mremap mirror functionality
@ 2017-11-01 21:35 ` Shuah Khan
0 siblings, 0 replies; 4+ messages in thread
From: Shuah Khan @ 2017-11-01 21:35 UTC (permalink / raw)
To: Anshuman Khandual, linux-mm, linux-kernel
Cc: mike.kravetz, mhocko, Shuah Khan, Shuah Khan
On 10/29/2017 09:18 PM, Anshuman Khandual wrote:
> 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.
>
> Suggested-by: Mike Kravetz <mike.kravetz@oracle.com>
> Signed-off-by: Anshuman Khandual <khandual@linux.vnet.ibm.com>
> Reviewed-by: Mike Kravetz <mike.kravetz@oracle.com>
> ---
> Changes in V4:
>
> - Folded these two test files into just one as per Mike
> - Did some renaming of functions, cleans ups etc
>
> Changes in V3: (https://patchwork.kernel.org/patch/10013469/)
>
> - 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/)
Thanks. I will queue this up for 4.15-rc1
-- Shuah
--
To unsubscribe, send a message with 'unsubscribe linux-mm' in
the body to majordomo@kvack.org. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"dont@kvack.org"> email@kvack.org </a>
^ permalink raw reply [flat|nested] 4+ messages in thread
end of thread, other threads:[~2017-11-01 21:35 UTC | newest]
Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2017-10-30 3:18 [PATCH] selftests/vm: Add tests validating mremap mirror functionality Anshuman Khandual
2017-10-30 3:18 ` Anshuman Khandual
2017-11-01 21:35 ` Shuah Khan
2017-11-01 21:35 ` Shuah Khan
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.