mm/selftest: Add MAP_POPULATE test
PATCH 1/1 mm/selftest: Add MAP_POPULATE test
As many other projects, we use some shmalloc allocator.
At some point we need to make a part of allocated pages back private to process. And it should be populated straight away.
Check that (MAP_PRIVATE | MAP_POPULATE) actually copies the private page.
--- tools/testing/selftests/vm/.gitignore | 1 + tools/testing/selftests/vm/Makefile | 1 + tools/testing/selftests/vm/map_populate.c | 113 ++++++++++++++++++++++++++++++ tools/testing/selftests/vm/run_vmtests | 11 +++ 4 files changed, 126 insertions(+) create mode 100644 tools/testing/selftests/vm/map_populate.c
tools/testing/selftests/vm/.gitignore +1 -0
@@ -1,6 +1,7 @@ hugepage-mmap hugepage-shm map_hugetlb +map_populate thuge-gen compaction_test mlock2-tests
tools/testing/selftests/vm/Makefile +1 -0
@@ -12,6 +12,7 @@ TEST_GEN_FILES += gup_benchmark TEST_GEN_FILES += hugepage-mmap TEST_GEN_FILES += hugepage-shm TEST_GEN_FILES += map_hugetlb +TEST_GEN_FILES += map_populate TEST_GEN_FILES += mlock-random-test TEST_GEN_FILES += mlock2-tests TEST_GEN_FILES += on-fault-limit
tools/testing/selftests/vm/map_populate.c new file +113 -0
113 lines not on this page · raw message
tools/testing/selftests/vm/run_vmtests +11 -0
@@ -168,6 +168,17 @@ else fi echo "--------------------" +echo "running map_populate" +echo "--------------------" +./map_populate +if [ $? -ne 0 ]; then + echo "[FAIL]" + exitcode=1 +else + echo "[PASS]" +fi + +echo "--------------------" echo "running mlock2-tests" echo "--------------------" ./mlock2-tests
Signature · 6 lines
2.13.6 -- To unsubscribe from this list: send the line "unsubscribe linux-kselftest" in the body of a message to majordomo at vger.kernel.org More majordomo info at http://vger.kernel.org/majordomo-info.html
Shuah Khan
maintainerHi Dmitry,
Thanks for the test. A few comments below.
Quoted 4 lines · Dmitry Safonov
As many other projects, we use some shmalloc allocator.
At some point we need to make a part of allocated pages back private to process. And it should be populated straight away.
Check that (MAP_PRIVATE|MAP_POPULATE) actually copies the private page.
It is not clear from this commit log what this test is doing? It would be helpful to rephrase.
"MAP_POPULATE | MAP_PRIVATE memory maps should COW VMA pages. This test is for verifying that it does" something along the lines??
Quoted 84 lines
Cc Andrew Morton <akpm at linux-foundation.org>Cc Dmitry Safonov <0x7f454c46 at gmail.com>Cc Hua Zhong <hzhong at arista.com>Cc Shuah Khan <shuah at kernel.org>Cc Stuart Ritchie <sritchie at arista.com>Cc linux-kselftest at vger.kernel.orgSigned-off-by Dmitry Safonov <dima at arista.com>--- tools/testing/selftests/vm/.gitignore | 1 + tools/testing/selftests/vm/Makefile | 1 + tools/testing/selftests/vm/map_populate.c | 113 ++++++++++++++++++++++++++++++ tools/testing/selftests/vm/run_vmtests | 11 +++ 4 files changed, 126 insertions(+) create mode 100644 tools/testing/selftests/vm/map_populate.ctools/testing/selftests/vm/.gitignore +1
-0@@ -1,6 +1,7 @@ hugepage-mmap hugepage-shm map_hugetlb +map_populate thuge-gen compaction_test mlock2-teststools/testing/selftests/vm/Makefile +1
-0@@ -12,6 +12,7 @@ TEST_GEN_FILES += gup_benchmark TEST_GEN_FILES += hugepage-mmap TEST_GEN_FILES += hugepage-shm TEST_GEN_FILES += map_hugetlb +TEST_GEN_FILES += map_populate TEST_GEN_FILES += mlock-random-test TEST_GEN_FILES += mlock2-tests TEST_GEN_FILES += on-fault-limittools/testing/selftests/vm/map_populate.c new file +38
-0@@ -0,0 +1,113 @@ +// SPDX-License-Identifier: GPL-2.0 +/* +* Copyright (c) 2018 Dmitry Safonov, Arista Networks +* +* MAP_POPULATE | MAP_PRIVATE should COW VMA pages. +*/ + +#define _GNU_SOURCE +#include <errno.h> +#include <fcntl.h> +#include <sys/mman.h> +#include <sys/socket.h> +#include <sys/types.h> +#include <sys/wait.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <unistd.h> + +#ifndef MMAP_SZ +#define MMAP_SZ 4096 +#endif + +#define BUG_ON (condition, description) \ + do { \ + if (condition) { \ + fprintf(stderr, "[FAIL]\t%s:%d\t%s:%s\n", __func__, \ + __LINE__, (description), strerror(errno)); \ + exit(1); \ + } \ + } while (0) + +static int parent_f (int sock,unsigned long *smap,int child) +{ +int status, ret; + + ret =read (sock, &status,sizeof (int )); +BUG_ON (ret <=0 ,"read(sock)" );
tools/testing/selftests/vm/map_populate.c line 48
Quoted 10 lines
tools/testing/selftests/vm/map_populate.c new file +10
-0+ + *smap =0x22222BAD ; + ret =msync (smap,MMAP_SZ ,MS_SYNC ); +BUG_ON (ret,"msync()" ); + + ret =write (sock, &status,sizeof (int )); +BUG_ON (ret <=0 ,"write(sock)" ); + +waitpid (child, &status,0 ); +BUG_ON (!WIFEXITED (status),"child hasn't suicide by own will" );
What does this mean? Could you rephrase this please?
tools/testing/selftests/vm/map_populate.c line 61
Quoted 13 lines
tools/testing/selftests/vm/map_populate.c new file +13
-0+ +return WEXITSTATUS (status); +} + +static int child_f (int sock,unsigned long *smap,int fd) +{ +int ret, buf =0 ; + + smap =mmap (0 ,MMAP_SZ ,PROT_READ |PROT_WRITE , +MAP_PRIVATE |MAP_POPULATE , fd,0 ); +BUG_ON (smap ==MAP_FAILED ,"mmap()" ); + +BUG_ON (*smap !=0xdeadbabe ,"MAP_PRIVATE | MAP_POPULATE changed file" );
What is expected here? What is expected?
Quoted 65 lines
tools/testing/selftests/vm/map_populate.c new file +52
-0+ + ret =write (sock, &buf,sizeof (int )); +BUG_ON (ret <=0 ,"write(sock)" ); + + ret =read (sock, &buf,sizeof (int )); +BUG_ON (ret <=0 ,"read(sock)" ); + +BUG_ON (*smap ==0x22222BAD ,"MAP_POPULATE didn't COW private page" ); +BUG_ON (*smap !=0xdeadbabe ,"mapping was corrupted" ); + +return 0 ; +} + +int main (int argc,char **argv) +{ +int sock[2 ], child, ret; +FILE *ftmp; +unsigned long *smap; + + ftmp =tmpfile (); +BUG_ON (ftmp ==0 ,"tmpfile()" ); + + ret =ftruncate (fileno (ftmp),MMAP_SZ ); +BUG_ON (ret,"ftruncate()" ); + + smap =mmap (0 ,MMAP_SZ ,PROT_READ |PROT_WRITE , +MAP_SHARED ,fileno (ftmp),0 ); +BUG_ON (smap ==MAP_FAILED ,"mmap()" ); + + *smap =0xdeadbabe ; +/* Probably unnecessary, but let it be. */ + ret =msync (smap,MMAP_SZ ,MS_SYNC ); +BUG_ON (ret,"msync()" ); + + ret =socketpair (PF_LOCAL ,SOCK_SEQPACKET ,0 , sock); +BUG_ON (ret,"socketpair()" ); + + child =fork (); +BUG_ON (child ==-1 ,"fork()" ); + +if (child) { + ret =close (sock[0 ]); +BUG_ON (ret,"close()" ); + +return parent_f (sock[1 ], smap, child); + } + + ret =close (sock[1 ]); +BUG_ON (ret,"close()" ); + +return child_f (sock[0 ], smap,fileno (ftmp)); +}tools/testing/selftests/vm/run_vmtests +4
-0@@ -168,6 +168,17 @@ else fi echo "--------------------" +echo "running map_populate" +echo "--------------------" +./map_populate +if [ $? -ne 0 ]; then
tools/testing/selftests/vm/run_vmtests else · line 184
Quoted 10 lines
tools/testing/selftests/vm/run_vmtests +7
-0+ echo "[FAIL]" + exitcode=1 +else + echo "[PASS]" +fi + +echo "--------------------" echo "running mlock2-tests" echo "--------------------" ./mlock2-tests
thanks,
-- Shuah
--
To unsubscribe from this list: send the line "unsubscribe linux-kselftest" in the body of a message to majordomo at vger.kernel.org
More majordomo info at http://vger.kernel.org/majordomo-info.html
Quoted 1 line · Shuah Khan
Hi Dmitry,
Hi Shuah,
Thanks for the review,
Quoted 11 lines
Thanks for the test. A few comments below.
Quoted 6 lines · Dmitry Safonov
As many other projects, we use some shmalloc allocator. At some point we need to make a part of allocated pages back private to
process. And it should be populated straight away.
Check that (MAP_PRIVATE|MAP_POPULATE) actually copies the private page.
Quoted 7 lines
It is not clear from this commit log what this test is doing? It would
be helpful to rephrase."
MAP_POPULATE|MAP_PRIVATEmemory maps should COW VMA pages. This test is
for verifying that it does" something along the lines??
Uhm, no. COW VMA is just MAP_PRIVATE.
IOW, if you do mmap(MAP_PRIVATE), you need to actually "touch" (call write() on) the pages to get a copy. Which might be not handy if one has hundreds of pages in a VMA.
But mmap(MAP_PRIVATE | MAP_POPULATE) should return mapping with already pre-copied pages, not the COW.
Quoted 101 lines
Quoted 101 lines
Cc Andrew Morton <akpm at linux-foundation.org>Cc Dmitry Safonov <0x7f454c46 at gmail.com>Cc Hua Zhong <hzhong at arista.com>Cc Shuah Khan <shuah at kernel.org>Cc Stuart Ritchie <sritchie at arista.com>Cc linux-kselftest at vger.kernel.orgSigned-off-by Dmitry Safonov <dima at arista.com>--- tools/testing/selftests/vm/.gitignore | 1 + tools/testing/selftests/vm/Makefile | 1 + tools/testing/selftests/vm/map_populate.c | 113 ++++++++++++++++++++++++++++++ tools/testing/selftests/vm/run_vmtests | 11 +++ 4 files changed, 126 insertions(+) create mode 100644 tools/testing/selftests/vm/map_populate.ctools/testing/selftests/vm/.gitignore +1
-0@@ -1,6 +1,7 @@ hugepage-mmap hugepage-shm map_hugetlb +map_populate thuge-gen compaction_test mlock2-teststools/testing/selftests/vm/Makefile +1
-0@@ -12,6 +12,7 @@ TEST_GEN_FILES += gup_benchmark TEST_GEN_FILES += hugepage-mmap TEST_GEN_FILES += hugepage-shm TEST_GEN_FILES += map_hugetlb +TEST_GEN_FILES += map_populate TEST_GEN_FILES += mlock-random-test TEST_GEN_FILES += mlock2-tests TEST_GEN_FILES += on-fault-limittools/testing/selftests/vm/map_populate.c new file +24
-0@@ -0,0 +1,113 @@ +// SPDX-License-Identifier: GPL-2.0 +/* +* Copyright (c) 2018 Dmitry Safonov, Arista Networks +* +* MAP_POPULATE | MAP_PRIVATE should COW VMA pages. +*/ + +#define _GNU_SOURCE +#include <errno.h> +#include <fcntl.h> +#include <sys/mman.h> +#include <sys/socket.h> +#include <sys/types.h> +#include <sys/wait.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <unistd.h> + +#ifndef MMAP_SZ +#define MMAP_SZ 4096 +#endif + +#define BUG_ON (condition, description)\tools/testing/selftests/vm/map_populate.c new file +1
-0+ do {\tools/testing/selftests/vm/map_populate.c new file +1
-0+if (condition) {\
tools/testing/selftests/vm/map_populate.c new file +1
-0+ fprintf(stderr, "[FAIL]\t%s:%d\t%s:%s\n",
__func__, \tools/testing/selftests/vm/map_populate.c new file +1
-0+ __LINE__, (description),strerror(errno)); \
tools/testing/selftests/vm/map_populate.c new file +1
-0+exit (1 );\
tools/testing/selftests/vm/map_populate.c new file +1
-0+ }\
tools/testing/selftests/vm/map_populate.c new file +13
-0+ }while (0 ) + +static int parent_f(int sock, unsigned long *smap, int child) +{ +int status, ret; + + ret =read (sock, &status,sizeof (int )); +BUG_ON (ret <=0 ,"read(sock)" ); + + *smap =0x22222BAD ; + ret =msync (smap,MMAP_SZ ,MS_SYNC ); +BUG_ON (ret,"msync()" ); +
Quoted 8 lines
Quoted 6 lines
tools/testing/selftests/vm/map_populate.c new file +5
-0+ ret =write (sock, &status,sizeof (int )); +BUG_ON (ret <=0 ,"write(sock)" ); + +waitpid (child, &status,0 ); + BUG_ON(!WIFEXITED(status), "child hasn't suicide by ownwill");
What does this mean? Could you rephrase this please?
Sure, I didn't want to write "child hasn't exited" because it sounds like it still alive.. I can change it to "child in unexpected state" if that sounds any better?
Or I can check WIFSIGNALED(), WIFSIGNALED(), WIFSTOPPED() too and write what happened to the child, but it will needlessly blow the code.
Quoted 17 lines
Quoted 14 lines
tools/testing/selftests/vm/map_populate.c new file +13
-0+ +return WEXITSTATUS (status); +} + +static int child_f (int sock,unsigned long *smap,int fd) +{ +int ret, buf =0 ; + + smap =mmap (0 ,MMAP_SZ ,PROT_READ |PROT_WRITE , +MAP_PRIVATE |MAP_POPULATE , fd,0 ); +BUG_ON (smap ==MAP_FAILED ,"mmap()" ); + + BUG_ON(*smap != 0xdeadbabe, "MAP_PRIVATE | MAP_POPULATEQuoted 1 line
changed file");
What is expected here? What is expected?
It's expected that the value in file is the same as before remapping it MAP_PRIVATE (same as parent set initially).
Quoted 73 lines
Quoted 73 lines
tools/testing/selftests/vm/map_populate.c new file +8
-0+ + ret =write (sock, &buf,sizeof (int )); +BUG_ON (ret <=0 ,"write(sock)" ); + + ret =read (sock, &buf,sizeof (int )); +BUG_ON (ret <=0 ,"read(sock)" ); + + BUG_ON(*smap == 0x22222BAD, "MAP_POPULATE didn't COWprivate page");
tools/testing/selftests/vm/map_populate.c new file +44
-0+BUG_ON (*smap !=0xdeadbabe ,"mapping was corrupted" ); + +return 0 ; +} + +int main (int argc,char **argv) +{ +int sock[2 ], child, ret; +FILE *ftmp; +unsigned long *smap; + + ftmp =tmpfile (); +BUG_ON (ftmp ==0 ,"tmpfile()" ); + + ret =ftruncate (fileno (ftmp),MMAP_SZ ); +BUG_ON (ret,"ftruncate()" ); + + smap =mmap (0 ,MMAP_SZ ,PROT_READ |PROT_WRITE , +MAP_SHARED ,fileno (ftmp),0 ); +BUG_ON (smap ==MAP_FAILED ,"mmap()" ); + + *smap =0xdeadbabe ; +/* Probably unnecessary, but let it be. */ + ret =msync (smap,MMAP_SZ ,MS_SYNC ); +BUG_ON (ret,"msync()" ); + + ret =socketpair (PF_LOCAL ,SOCK_SEQPACKET ,0 , sock); +BUG_ON (ret,"socketpair()" ); + + child =fork (); +BUG_ON (child ==-1 ,"fork()" ); + +if (child) { + ret =close (sock[0 ]); +BUG_ON (ret,"close()" ); + +return parent_f (sock[1 ], smap, child); + } + + ret =close (sock[1 ]); +BUG_ON (ret,"close()" ); + +return child_f (sock[0 ], smap,fileno (ftmp)); +}tools/testing/selftests/vm/run_vmtests +10
-0@@ -168,6 +168,17 @@ else fi echo "--------------------" +echo "running map_populate" +echo "--------------------" +./map_populate +if [ $? -ne 0 ]; then + echo "[FAIL]" + exitcode=1 +else + echo "[PASS]" +fi +
Quoted 8 lines
tools/testing/selftests/vm/run_vmtests else · line 184
Quoted 5 lines
tools/testing/selftests/vm/run_vmtests +1
-0+echo "--------------------" echo "running mlock2-tests" echo "--------------------" ./mlock2-teststhanks,
-- Shuah
--
To unsubscribe from this list: send the line "unsubscribe linux-kselftest" in the body of a message to majordomo at vger.kernel.org
More majordomo info at http://vger.kernel.org/majordomo-info.html
Quoted 10 lines · On Thu, 2 Aug 2018 00:36:36 +0100 Dmitry Safonov <[email protected]> wrote:
As many other projects, we use some shmalloc allocator.
At some point we need to make a part of allocated pages back private to process. And it should be populated straight away.
Check that (MAP_PRIVATE|MAP_POPULATE) actually copies the private page....
tools/testing/selftests/vm/map_populate.c new file +0
-0
tools/testing/selftests/vm/map_populate.c line 31
Quoted 10 lines
tools/testing/selftests/vm/map_populate.c new file +8
-0+#define BUG_ON (condition, description) \ + do { \ + if (condition) { \ + fprintf(stderr, "[FAIL]\t%s:%d\t%s:%s\n", __func__, \ + __LINE__, (description), strerror(errno)); \ + exit(1); \ + } \ + } while (0)
This is userspace. Why not use assert()?
tools/testing/selftests/vm/map_populate.c line 81
Quoted 10 lines
...
tools/testing/selftests/vm/map_populate.c new file +7
-0+int main (int argc,char **argv) +{ +int sock[2 ], child, ret; +FILE *ftmp; +unsigned long *smap; + + ftmp =tmpfile ();
Seems odd to putz around with stdio when you just want the fd. mkstemp(), maybe?
Quoted 25 lines
tools/testing/selftests/vm/map_populate.c new file +24
-0+BUG_ON (ftmp ==0 ,"tmpfile()" ); + + ret =ftruncate (fileno (ftmp),MMAP_SZ ); +BUG_ON (ret,"ftruncate()" ); + + smap =mmap (0 ,MMAP_SZ ,PROT_READ |PROT_WRITE , +MAP_SHARED ,fileno (ftmp),0 ); +BUG_ON (smap ==MAP_FAILED ,"mmap()" ); + + *smap =0xdeadbabe ; +/* Probably unnecessary, but let it be. */ + ret =msync (smap,MMAP_SZ ,MS_SYNC ); +BUG_ON (ret,"msync()" ); + + ret =socketpair (PF_LOCAL ,SOCK_SEQPACKET ,0 , sock); +BUG_ON (ret,"socketpair()" ); + + child =fork (); +BUG_ON (child ==-1 ,"fork()" ); + +if (child) { + ret =close (sock[0 ]); +BUG_ON (ret,"close()" ); +
tools/testing/selftests/vm/map_populate.c
Quoted 10 lines
tools/testing/selftests/vm/map_populate.c new file +8
-0+return parent_f (sock[1 ], smap, child); + } + + ret =close (sock[1 ]); +BUG_ON (ret,"close()" ); + +return child_f (sock[0 ], smap,fileno (ftmp)); +}
--
To unsubscribe from this list: send the line "unsubscribe linux-kselftest" in the body of a message to majordomo at vger.kernel.org
More majordomo info at http://vger.kernel.org/majordomo-info.html
Quoted 26 lines · Andrew Morton
Quoted 23 lines · On Thu, 2 Aug 2018 00:36:36 +0100 Dmitry Safonov <dima at arista.com> wrote:
As many other projects, we use some shmalloc allocator. At some point we need to make a part of allocated pages back private to
process. And it should be populated straight away.
Check that (MAP_PRIVATE|MAP_POPULATE) actually copies the private page....
tools/testing/selftests/vm/map_populate.c new file +1
-0+#define BUG_ON (condition, description)\tools/testing/selftests/vm/map_populate.c new file +1
-0+ do {\tools/testing/selftests/vm/map_populate.c new file +1
-0+if (condition) {\
tools/testing/selftests/vm/map_populate.c new file +1
-0+ fprintf(stderr, "[FAIL]\t%s:%d\t%s:%s\n",
__func__, \tools/testing/selftests/vm/map_populate.c new file +1
-0+ __LINE__, (description),
Quoted 8 lines
tools/testing/selftests/vm/map_populate.c line 31
Quoted 6 lines
strerror(errno)); \
tools/testing/selftests/vm/map_populate.c new file +1
-0+exit (1 );\
tools/testing/selftests/vm/map_populate.c new file +1
-0+ }\
tools/testing/selftests/vm/map_populate.c new file +1
-0+ }while (0 )This is userspace. Why not use
assert()?
I wanted to print errno too. Of cause it's possible to get it from coredump - but I think it's easier if you get buildbot reply to see what the error is from the log.
Quoted 4 lines
Quoted 4 lines
...
Quoted 10 lines
tools/testing/selftests/vm/map_populate.c line 81
Quoted 7 lines
tools/testing/selftests/vm/map_populate.c new file +7
-0+int main (int argc,char **argv) +{ +int sock[2 ], child, ret; +FILE *ftmp; +unsigned long *smap; + + ftmp =tmpfile ();Seems odd to putz around with stdio when you just want the fd.
mkstemp(), maybe?
It needs a template and unlink() call.. I can do it.
I initially thought about memfd_create(), can change it that way. The only downside is that it will be syscall(NR_memfd_create,...)
Quoted 34 lines
Quoted 34 lines
tools/testing/selftests/vm/map_populate.c new file +32
-0+BUG_ON (ftmp ==0 ,"tmpfile()" ); + + ret =ftruncate (fileno (ftmp),MMAP_SZ ); +BUG_ON (ret,"ftruncate()" ); + + smap =mmap (0 ,MMAP_SZ ,PROT_READ |PROT_WRITE , +MAP_SHARED ,fileno (ftmp),0 ); +BUG_ON (smap ==MAP_FAILED ,"mmap()" ); + + *smap =0xdeadbabe ; +/* Probably unnecessary, but let it be. */ + ret =msync (smap,MMAP_SZ ,MS_SYNC ); +BUG_ON (ret,"msync()" ); + + ret =socketpair (PF_LOCAL ,SOCK_SEQPACKET ,0 , sock); +BUG_ON (ret,"socketpair()" ); + + child =fork (); +BUG_ON (child ==-1 ,"fork()" ); + +if (child) { + ret =close (sock[0 ]); +BUG_ON (ret,"close()" ); + +return parent_f (sock[1 ], smap, child); + } + + ret =close (sock[1 ]); +BUG_ON (ret,"close()" ); + +return child_f (sock[0 ], smap,fileno (ftmp)); +}
Quoted 2 lines
Quoted 2 lines
...
--
To unsubscribe from this list: send the line "unsubscribe linux-kselftest" in the body of a message to majordomo at vger.kernel.org
More majordomo info at http://vger.kernel.org/majordomo-info.html