Re: [PATCH] selftests/vm: Add test to validate mirror functionality with mremap

2017-07-21 Thread Anshuman Khandual
On 07/21/2017 04:49 AM, Mike Kravetz wrote:
> On 07/20/2017 02:36 AM, Anshuman Khandual wrote:
>> This adds a test to validate mirror functionality with mremap()
>> system call on shared anon mappings.
>>
>> Suggested-by: Mike Kravetz 
>> Signed-off-by: Anshuman Khandual 
>> ---
>>  tools/testing/selftests/vm/Makefile|  1 +
>>  .../selftests/vm/mremap_mirror_shared_anon.c   | 54 
>> ++
> 
> This may be a better fit in LTP where there are already several other
> mremap tests.  I honestly do not know the best place for such a test.

Yeah but these days self tests try to target smaller functional
tests which can run quickly and validate something, hence thought
this may be appropriate as a self test.

> 
>>  2 files changed, 55 insertions(+)
>>  create mode 100644 tools/testing/selftests/vm/mremap_mirror_shared_anon.c
>>
>> diff --git a/tools/testing/selftests/vm/Makefile 
>> b/tools/testing/selftests/vm/Makefile
>> index cbb29e4..11657ff5 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_shared_anon
>>  
>>  TEST_PROGS := run_vmtests
>>  
>> diff --git a/tools/testing/selftests/vm/mremap_mirror_shared_anon.c 
>> b/tools/testing/selftests/vm/mremap_mirror_shared_anon.c
>> new file mode 100644
>> index 000..b0adbb2
>> --- /dev/null
>> +++ b/tools/testing/selftests/vm/mremap_mirror_shared_anon.c
>> @@ -0,0 +1,54 @@
>> +/*
>> + * Test to verify mirror functionality with mremap() system
>> + * call for shared anon mappings.
>> + *
>> + * Copyright (C) 2017 Anshuman Khandual, IBM Corporation
>> + *
>> + * Licensed under GPL V2
>> + */
>> +#include 
>> +#include 
>> +#include 
>> +#include 
>> +#include 
>> +#include 
>> +
>> +#define PATTERN 0xbe
>> +#define ALLOC_SIZE  0x1UL /* Works for 64K and 4K pages */
> 
> Why hardcode?  You could use sysconf to get page size and use some
> multiple of that.

Sure, will do that.

> 
>> +
>> +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[])
>> +{
>> +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, 1);
> 
> Why hardcode 1?  You really want the MREMAP_MAYMOVE flag.  Right?

Right, missed that. My bad.

> 
>> +if (mirror_ptr == MAP_FAILED) {
>> +perror("mremap() failed");
>> +return -1;
>> +}
>> +
>> +if (test_mirror(ptr, mirror_ptr, ALLOC_SIZE))
>> +return 1;
>> +return 0;
>> +}
> 
> You may want to expand the test to make sure mremap(old_size == 0)
> fails for private mappings.  Of course, this assumes my proposed
> patch gets in.  Until then, it will succeed and create a new unrelated
> mapping.

Even without your patch, the data in the new mapping still does
not match the original one. Anyway, will accommodate private
anon mapping as well.



Re: [PATCH] selftests/vm: Add test to validate mirror functionality with mremap

2017-07-21 Thread Anshuman Khandual
On 07/21/2017 04:49 AM, Mike Kravetz wrote:
> On 07/20/2017 02:36 AM, Anshuman Khandual wrote:
>> This adds a test to validate mirror functionality with mremap()
>> system call on shared anon mappings.
>>
>> Suggested-by: Mike Kravetz 
>> Signed-off-by: Anshuman Khandual 
>> ---
>>  tools/testing/selftests/vm/Makefile|  1 +
>>  .../selftests/vm/mremap_mirror_shared_anon.c   | 54 
>> ++
> 
> This may be a better fit in LTP where there are already several other
> mremap tests.  I honestly do not know the best place for such a test.

Yeah but these days self tests try to target smaller functional
tests which can run quickly and validate something, hence thought
this may be appropriate as a self test.

> 
>>  2 files changed, 55 insertions(+)
>>  create mode 100644 tools/testing/selftests/vm/mremap_mirror_shared_anon.c
>>
>> diff --git a/tools/testing/selftests/vm/Makefile 
>> b/tools/testing/selftests/vm/Makefile
>> index cbb29e4..11657ff5 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_shared_anon
>>  
>>  TEST_PROGS := run_vmtests
>>  
>> diff --git a/tools/testing/selftests/vm/mremap_mirror_shared_anon.c 
>> b/tools/testing/selftests/vm/mremap_mirror_shared_anon.c
>> new file mode 100644
>> index 000..b0adbb2
>> --- /dev/null
>> +++ b/tools/testing/selftests/vm/mremap_mirror_shared_anon.c
>> @@ -0,0 +1,54 @@
>> +/*
>> + * Test to verify mirror functionality with mremap() system
>> + * call for shared anon mappings.
>> + *
>> + * Copyright (C) 2017 Anshuman Khandual, IBM Corporation
>> + *
>> + * Licensed under GPL V2
>> + */
>> +#include 
>> +#include 
>> +#include 
>> +#include 
>> +#include 
>> +#include 
>> +
>> +#define PATTERN 0xbe
>> +#define ALLOC_SIZE  0x1UL /* Works for 64K and 4K pages */
> 
> Why hardcode?  You could use sysconf to get page size and use some
> multiple of that.

Sure, will do that.

> 
>> +
>> +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[])
>> +{
>> +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, 1);
> 
> Why hardcode 1?  You really want the MREMAP_MAYMOVE flag.  Right?

Right, missed that. My bad.

> 
>> +if (mirror_ptr == MAP_FAILED) {
>> +perror("mremap() failed");
>> +return -1;
>> +}
>> +
>> +if (test_mirror(ptr, mirror_ptr, ALLOC_SIZE))
>> +return 1;
>> +return 0;
>> +}
> 
> You may want to expand the test to make sure mremap(old_size == 0)
> fails for private mappings.  Of course, this assumes my proposed
> patch gets in.  Until then, it will succeed and create a new unrelated
> mapping.

Even without your patch, the data in the new mapping still does
not match the original one. Anyway, will accommodate private
anon mapping as well.



Re: [PATCH] selftests/vm: Add test to validate mirror functionality with mremap

2017-07-20 Thread Mike Kravetz
On 07/20/2017 02:36 AM, Anshuman Khandual wrote:
> This adds a test to validate mirror functionality with mremap()
> system call on shared anon mappings.
> 
> Suggested-by: Mike Kravetz 
> Signed-off-by: Anshuman Khandual 
> ---
>  tools/testing/selftests/vm/Makefile|  1 +
>  .../selftests/vm/mremap_mirror_shared_anon.c   | 54 
> ++

This may be a better fit in LTP where there are already several other
mremap tests.  I honestly do not know the best place for such a test.

>  2 files changed, 55 insertions(+)
>  create mode 100644 tools/testing/selftests/vm/mremap_mirror_shared_anon.c
> 
> diff --git a/tools/testing/selftests/vm/Makefile 
> b/tools/testing/selftests/vm/Makefile
> index cbb29e4..11657ff5 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_shared_anon
>  
>  TEST_PROGS := run_vmtests
>  
> diff --git a/tools/testing/selftests/vm/mremap_mirror_shared_anon.c 
> b/tools/testing/selftests/vm/mremap_mirror_shared_anon.c
> new file mode 100644
> index 000..b0adbb2
> --- /dev/null
> +++ b/tools/testing/selftests/vm/mremap_mirror_shared_anon.c
> @@ -0,0 +1,54 @@
> +/*
> + * Test to verify mirror functionality with mremap() system
> + * call for shared anon mappings.
> + *
> + * Copyright (C) 2017 Anshuman Khandual, IBM Corporation
> + *
> + * Licensed under GPL V2
> + */
> +#include 
> +#include 
> +#include 
> +#include 
> +#include 
> +#include 
> +
> +#define PATTERN  0xbe
> +#define ALLOC_SIZE   0x1UL /* Works for 64K and 4K pages */

Why hardcode?  You could use sysconf to get page size and use some
multiple of that.

> +
> +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[])
> +{
> + 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, 1);

Why hardcode 1?  You really want the MREMAP_MAYMOVE flag.  Right?

> + if (mirror_ptr == MAP_FAILED) {
> + perror("mremap() failed");
> + return -1;
> + }
> +
> + if (test_mirror(ptr, mirror_ptr, ALLOC_SIZE))
> + return 1;
> + return 0;
> +}

You may want to expand the test to make sure mremap(old_size == 0)
fails for private mappings.  Of course, this assumes my proposed
patch gets in.  Until then, it will succeed and create a new unrelated
mapping.

-- 
Mike Kravetz


Re: [PATCH] selftests/vm: Add test to validate mirror functionality with mremap

2017-07-20 Thread Mike Kravetz
On 07/20/2017 02:36 AM, Anshuman Khandual wrote:
> This adds a test to validate mirror functionality with mremap()
> system call on shared anon mappings.
> 
> Suggested-by: Mike Kravetz 
> Signed-off-by: Anshuman Khandual 
> ---
>  tools/testing/selftests/vm/Makefile|  1 +
>  .../selftests/vm/mremap_mirror_shared_anon.c   | 54 
> ++

This may be a better fit in LTP where there are already several other
mremap tests.  I honestly do not know the best place for such a test.

>  2 files changed, 55 insertions(+)
>  create mode 100644 tools/testing/selftests/vm/mremap_mirror_shared_anon.c
> 
> diff --git a/tools/testing/selftests/vm/Makefile 
> b/tools/testing/selftests/vm/Makefile
> index cbb29e4..11657ff5 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_shared_anon
>  
>  TEST_PROGS := run_vmtests
>  
> diff --git a/tools/testing/selftests/vm/mremap_mirror_shared_anon.c 
> b/tools/testing/selftests/vm/mremap_mirror_shared_anon.c
> new file mode 100644
> index 000..b0adbb2
> --- /dev/null
> +++ b/tools/testing/selftests/vm/mremap_mirror_shared_anon.c
> @@ -0,0 +1,54 @@
> +/*
> + * Test to verify mirror functionality with mremap() system
> + * call for shared anon mappings.
> + *
> + * Copyright (C) 2017 Anshuman Khandual, IBM Corporation
> + *
> + * Licensed under GPL V2
> + */
> +#include 
> +#include 
> +#include 
> +#include 
> +#include 
> +#include 
> +
> +#define PATTERN  0xbe
> +#define ALLOC_SIZE   0x1UL /* Works for 64K and 4K pages */

Why hardcode?  You could use sysconf to get page size and use some
multiple of that.

> +
> +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[])
> +{
> + 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, 1);

Why hardcode 1?  You really want the MREMAP_MAYMOVE flag.  Right?

> + if (mirror_ptr == MAP_FAILED) {
> + perror("mremap() failed");
> + return -1;
> + }
> +
> + if (test_mirror(ptr, mirror_ptr, ALLOC_SIZE))
> + return 1;
> + return 0;
> +}

You may want to expand the test to make sure mremap(old_size == 0)
fails for private mappings.  Of course, this assumes my proposed
patch gets in.  Until then, it will succeed and create a new unrelated
mapping.

-- 
Mike Kravetz


[PATCH] selftests/vm: Add test to validate mirror functionality with mremap

2017-07-20 Thread Anshuman Khandual
This adds a test to validate mirror functionality with mremap()
system call on shared anon mappings.

Suggested-by: Mike Kravetz 
Signed-off-by: Anshuman Khandual 
---
 tools/testing/selftests/vm/Makefile|  1 +
 .../selftests/vm/mremap_mirror_shared_anon.c   | 54 ++
 2 files changed, 55 insertions(+)
 create mode 100644 tools/testing/selftests/vm/mremap_mirror_shared_anon.c

diff --git a/tools/testing/selftests/vm/Makefile 
b/tools/testing/selftests/vm/Makefile
index cbb29e4..11657ff5 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_shared_anon
 
 TEST_PROGS := run_vmtests
 
diff --git a/tools/testing/selftests/vm/mremap_mirror_shared_anon.c 
b/tools/testing/selftests/vm/mremap_mirror_shared_anon.c
new file mode 100644
index 000..b0adbb2
--- /dev/null
+++ b/tools/testing/selftests/vm/mremap_mirror_shared_anon.c
@@ -0,0 +1,54 @@
+/*
+ * Test to verify mirror functionality with mremap() system
+ * call for shared anon mappings.
+ *
+ * Copyright (C) 2017 Anshuman Khandual, IBM Corporation
+ *
+ * Licensed under GPL V2
+ */
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+
+#define PATTERN0xbe
+#define ALLOC_SIZE 0x1UL /* Works for 64K and 4K pages */
+
+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[])
+{
+   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, 1);
+   if (mirror_ptr == MAP_FAILED) {
+   perror("mremap() failed");
+   return -1;
+   }
+
+   if (test_mirror(ptr, mirror_ptr, ALLOC_SIZE))
+   return 1;
+   return 0;
+}
-- 
1.8.5.2



[PATCH] selftests/vm: Add test to validate mirror functionality with mremap

2017-07-20 Thread Anshuman Khandual
This adds a test to validate mirror functionality with mremap()
system call on shared anon mappings.

Suggested-by: Mike Kravetz 
Signed-off-by: Anshuman Khandual 
---
 tools/testing/selftests/vm/Makefile|  1 +
 .../selftests/vm/mremap_mirror_shared_anon.c   | 54 ++
 2 files changed, 55 insertions(+)
 create mode 100644 tools/testing/selftests/vm/mremap_mirror_shared_anon.c

diff --git a/tools/testing/selftests/vm/Makefile 
b/tools/testing/selftests/vm/Makefile
index cbb29e4..11657ff5 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_shared_anon
 
 TEST_PROGS := run_vmtests
 
diff --git a/tools/testing/selftests/vm/mremap_mirror_shared_anon.c 
b/tools/testing/selftests/vm/mremap_mirror_shared_anon.c
new file mode 100644
index 000..b0adbb2
--- /dev/null
+++ b/tools/testing/selftests/vm/mremap_mirror_shared_anon.c
@@ -0,0 +1,54 @@
+/*
+ * Test to verify mirror functionality with mremap() system
+ * call for shared anon mappings.
+ *
+ * Copyright (C) 2017 Anshuman Khandual, IBM Corporation
+ *
+ * Licensed under GPL V2
+ */
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+
+#define PATTERN0xbe
+#define ALLOC_SIZE 0x1UL /* Works for 64K and 4K pages */
+
+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[])
+{
+   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, 1);
+   if (mirror_ptr == MAP_FAILED) {
+   perror("mremap() failed");
+   return -1;
+   }
+
+   if (test_mirror(ptr, mirror_ptr, ALLOC_SIZE))
+   return 1;
+   return 0;
+}
-- 
1.8.5.2