selftests/vm: Set mmap()'s fd as -1 for MAP_ANONYMOUS flag
PATCH 1/1 selftests/vm: Set mmap()'s fd as -1 for MAP_ANONYMOUS flag
Man page of mmap() says that portable applications should ensure fd argument to be -1 if MAP_ANONYMOUS flag is set as below:
``` The mapping is not backed by any file; its contents are initialized to zero. The fd and offset arguments are ignored; however, some implementations require fd to be -1 if MAP_ANONYMOUS (or MAP_ANON) is specified, and portable applications should ensure this. ```
However, few mmap() calls under selftests/vm/ uses 0 as fd though they use MAP_ANONYMOUS flag. This commit changes the argument to be -1 as recommended.
--- tools/testing/selftests/vm/map_hugetlb.c | 2 +- tools/testing/selftests/vm/mlock2-tests.c | 12 ++++++------ tools/testing/selftests/vm/on-fault-limit.c | 2 +- tools/testing/selftests/vm/thuge-gen.c | 2 +- 4 files changed, 9 insertions(+), 9 deletions(-)
tools/testing/selftests/vm/map_hugetlb.c +1 -1
@@ -62,7 +62,7 @@ int main(void)void *addr;int ret; - addr =mmap (ADDR ,LENGTH ,PROTECTION ,FLAGS ,0 ,0 ); + addr =mmap (ADDR ,LENGTH ,PROTECTION ,FLAGS ,-1 ,0 );if (addr ==MAP_FAILED ) {perror ("mmap" );exit (1 );
tools/testing/selftests/vm/mlock2-tests.c +6 -6
@@ -293,7 +293,7 @@ static int test_mlock_lock()unsigned long page_size =getpagesize (); map =mmap (NULL ,2 * page_size,PROT_READ |PROT_WRITE , -MAP_ANONYMOUS |MAP_PRIVATE ,0 ,0 ); +MAP_ANONYMOUS |MAP_PRIVATE ,-1 ,0 );if (map ==MAP_FAILED ) {perror ("test_mlock_locked mmap" ); goto out;
@@ -402,7 +402,7 @@ static int test_mlock_onfault()unsigned long page_size =getpagesize (); map =mmap (NULL ,2 * page_size,PROT_READ |PROT_WRITE , -MAP_ANONYMOUS |MAP_PRIVATE ,0 ,0 ); +MAP_ANONYMOUS |MAP_PRIVATE ,-1 ,0 );if (map ==MAP_FAILED ) {perror ("test_mlock_locked mmap" ); goto out;
@@ -445,7 +445,7 @@ static int test_lock_onfault_of_present()uint64_t page1_flags, page2_flags; map =mmap (NULL ,2 * page_size,PROT_READ |PROT_WRITE , -MAP_ANONYMOUS |MAP_PRIVATE ,0 ,0 ); +MAP_ANONYMOUS |MAP_PRIVATE ,-1 ,0 );if (map ==MAP_FAILED ) {perror ("test_mlock_locked mmap" ); goto out;
@@ -492,7 +492,7 @@ static int test_munlockall()unsigned long page_size =getpagesize (); map =mmap (NULL ,2 * page_size,PROT_READ |PROT_WRITE , -MAP_ANONYMOUS |MAP_PRIVATE ,0 ,0 ); +MAP_ANONYMOUS |MAP_PRIVATE ,-1 ,0 );if (map ==MAP_FAILED ) {perror ("test_munlockall mmap" );
@@ -518,7 +518,7 @@ static int test_munlockall()munmap (map,2 * page_size); map =mmap (NULL ,2 * page_size,PROT_READ |PROT_WRITE , -MAP_ANONYMOUS |MAP_PRIVATE ,0 ,0 ); +MAP_ANONYMOUS |MAP_PRIVATE ,-1 ,0 );if (map ==MAP_FAILED ) {perror ("test_munlockall second mmap" );
@@ -573,7 +573,7 @@ static int test_vma_management(bool call_mlock)struct vm_boundaries page3; map =mmap (NULL ,3 * page_size,PROT_READ |PROT_WRITE , -MAP_ANONYMOUS |MAP_PRIVATE ,0 ,0 ); +MAP_ANONYMOUS |MAP_PRIVATE ,-1 ,0 );if (map ==MAP_FAILED ) {perror ("mmap()" );return ret;
tools/testing/selftests/vm/on-fault-limit.c +1 -1
@@ -26,7 +26,7 @@ static int test_limit(void) } map =mmap (NULL ,2 * lims.rlim_max,PROT_READ |PROT_WRITE , -MAP_PRIVATE |MAP_ANONYMOUS |MAP_POPULATE ,0 ,0 ); +MAP_PRIVATE |MAP_ANONYMOUS |MAP_POPULATE ,-1 ,0 );if (map !=MAP_FAILED )printf ("mmap should have failed, but didn't\n" );else {
tools/testing/selftests/vm/thuge-gen.c +1 -1
@@ -146,7 +146,7 @@ void test_mmap(unsigned long size, unsigned flags) before =read_free (size); map =mmap (NULL , size*NUM_PAGES ,PROT_READ |PROT_WRITE , -MAP_PRIVATE |MAP_ANONYMOUS |MAP_HUGETLB |flags,0 ,0 ); +MAP_PRIVATE |MAP_ANONYMOUS |MAP_HUGETLB |flags,-1 ,0 );if (map == (char *)-1 )err ("mmap" );memset (map,0xff , size*NUM_PAGES );
Signature · 1 line
2.10.0
Hello Shuah,
May I ask some comments?
Thanks,
SeongJae Park
Quoted 122 lines · SeongJae Park
Man page of
mmap()says that portable applications should ensure fd argument to be -1 ifMAP_ANONYMOUSflag is set as below:``` The mapping is not backed by any file; its contents are initialized to zero. The fd and offset arguments are ignored; however, some implementations require fd to be -1 if MAP_ANONYMOUS (or MAP_ANON) is specified, and portable applications should ensure this. ```However, few
mmap()calls under selftests/vm/ uses 0 as fd though they useMAP_ANONYMOUSflag. This commit changes the argument to be -1 as recommended.Signed-off-by SeongJae Park <[email protected]>--- tools/testing/selftests/vm/map_hugetlb.c | 2 +- tools/testing/selftests/vm/mlock2-tests.c | 12 ++++++------ tools/testing/selftests/vm/on-fault-limit.c | 2 +- tools/testing/selftests/vm/thuge-gen.c | 2 +- 4 files changed, 9 insertions(+), 9 deletions(-)tools/testing/selftests/vm/map_hugetlb.c +1
-1@@ -62,7 +62,7 @@ int main(void)void *addr;int ret; - addr =mmap (ADDR ,LENGTH ,PROTECTION ,FLAGS ,0 ,0 ); + addr =mmap (ADDR ,LENGTH ,PROTECTION ,FLAGS ,-1 ,0 );if (addr ==MAP_FAILED ) {perror ("mmap" );exit (1 );tools/testing/selftests/vm/mlock2-tests.c +6
-6@@ -293,7 +293,7 @@ static int test_mlock_lock()unsigned long page_size =getpagesize (); map =mmap (NULL ,2 * page_size,PROT_READ |PROT_WRITE , -MAP_ANONYMOUS |MAP_PRIVATE ,0 ,0 ); +MAP_ANONYMOUS |MAP_PRIVATE ,-1 ,0 );if (map ==MAP_FAILED ) {perror ("test_mlock_locked mmap" ); goto out;@@ -402,7 +402,7 @@ static int test_mlock_onfault()unsigned long page_size =getpagesize (); map =mmap (NULL ,2 * page_size,PROT_READ |PROT_WRITE , -MAP_ANONYMOUS |MAP_PRIVATE ,0 ,0 ); +MAP_ANONYMOUS |MAP_PRIVATE ,-1 ,0 );if (map ==MAP_FAILED ) {perror ("test_mlock_locked mmap" ); goto out;@@ -445,7 +445,7 @@ static int test_lock_onfault_of_present()uint64_t page1_flags, page2_flags; map =mmap (NULL ,2 * page_size,PROT_READ |PROT_WRITE , -MAP_ANONYMOUS |MAP_PRIVATE ,0 ,0 ); +MAP_ANONYMOUS |MAP_PRIVATE ,-1 ,0 );if (map ==MAP_FAILED ) {perror ("test_mlock_locked mmap" ); goto out;@@ -492,7 +492,7 @@ static int test_munlockall()unsigned long page_size =getpagesize (); map =mmap (NULL ,2 * page_size,PROT_READ |PROT_WRITE , -MAP_ANONYMOUS |MAP_PRIVATE ,0 ,0 ); +MAP_ANONYMOUS |MAP_PRIVATE ,-1 ,0 );if (map ==MAP_FAILED ) {perror ("test_munlockall mmap" );@@ -518,7 +518,7 @@ static int test_munlockall()munmap (map,2 * page_size); map =mmap (NULL ,2 * page_size,PROT_READ |PROT_WRITE , -MAP_ANONYMOUS |MAP_PRIVATE ,0 ,0 ); +MAP_ANONYMOUS |MAP_PRIVATE ,-1 ,0 );if (map ==MAP_FAILED ) {perror ("test_munlockall second mmap" );@@ -573,7 +573,7 @@ static int test_vma_management(bool call_mlock)struct vm_boundaries page3; map =mmap (NULL ,3 * page_size,PROT_READ |PROT_WRITE , -MAP_ANONYMOUS |MAP_PRIVATE ,0 ,0 ); +MAP_ANONYMOUS |MAP_PRIVATE ,-1 ,0 );if (map ==MAP_FAILED ) {perror ("mmap()" );return ret;tools/testing/selftests/vm/on-fault-limit.c +1
-1@@ -26,7 +26,7 @@ static int test_limit(void) } map =mmap (NULL ,2 * lims.rlim_max,PROT_READ |PROT_WRITE , -MAP_PRIVATE |MAP_ANONYMOUS |MAP_POPULATE ,0 ,0 ); +MAP_PRIVATE |MAP_ANONYMOUS |MAP_POPULATE ,-1 ,0 );if (map !=MAP_FAILED )printf ("mmap should have failed, but didn't\n" );else {tools/testing/selftests/vm/thuge-gen.c +1
-2@@ -146,7 +146,7 @@ void test_mmap(unsigned long size, unsigned flags) before =read_free (size); map =mmap (NULL , size*NUM_PAGES ,PROT_READ |PROT_WRITE , -MAP_PRIVATE |MAP_ANONYMOUS |MAP_HUGETLB |flags,0 ,0 ); +MAP_PRIVATE |MAP_ANONYMOUS |MAP_HUGETLB |flags,-1 ,0 );if (map == (char *)-1 )err ("mmap" );memset (map,0xff , size*NUM_PAGES ); --2.10.0
Shuah Khan
maintainerQuoted 23 lines · SeongJae Park
Hello Shuah,
May I ask some comments?
Thanks,
SeongJae ParkQuoted 13 lines · SeongJae Park
Man page of
mmap()says that portable applications should ensure fd argument to be -1 ifMAP_ANONYMOUSflag is set as below:``` The mapping is not backed by any file; its contents are initialized to zero. The fd and offset arguments are ignored; however, some implementations require fd to be -1 if MAP_ANONYMOUS (or MAP_ANON) is specified, and portable applications should ensure this. ```However, few
mmap()calls under selftests/vm/ uses 0 as fd though they useMAP_ANONYMOUSflag. This commit changes the argument to be -1 as recommended.
Hi Andrew,
Do you have comments on this patch? Please Ack it if you would like me to take it thorugh ksefltest tree.
thanks,
-- Shuah
Quoted 111 lines
Quoted 109 lines
Signed-off-by SeongJae Park <[email protected]>--- tools/testing/selftests/vm/map_hugetlb.c | 2 +- tools/testing/selftests/vm/mlock2-tests.c | 12 ++++++------ tools/testing/selftests/vm/on-fault-limit.c | 2 +- tools/testing/selftests/vm/thuge-gen.c | 2 +- 4 files changed, 9 insertions(+), 9 deletions(-)tools/testing/selftests/vm/map_hugetlb.c +1
-1@@ -62,7 +62,7 @@ int main(void)void *addr;int ret; - addr =mmap (ADDR ,LENGTH ,PROTECTION ,FLAGS ,0 ,0 ); + addr =mmap (ADDR ,LENGTH ,PROTECTION ,FLAGS ,-1 ,0 );if (addr ==MAP_FAILED ) {perror ("mmap" );exit (1 );tools/testing/selftests/vm/mlock2-tests.c +6
-6@@ -293,7 +293,7 @@ static int test_mlock_lock()unsigned long page_size =getpagesize (); map =mmap (NULL ,2 * page_size,PROT_READ |PROT_WRITE , -MAP_ANONYMOUS |MAP_PRIVATE ,0 ,0 ); +MAP_ANONYMOUS |MAP_PRIVATE ,-1 ,0 );if (map ==MAP_FAILED ) {perror ("test_mlock_locked mmap" ); goto out;@@ -402,7 +402,7 @@ static int test_mlock_onfault()unsigned long page_size =getpagesize (); map =mmap (NULL ,2 * page_size,PROT_READ |PROT_WRITE , -MAP_ANONYMOUS |MAP_PRIVATE ,0 ,0 ); +MAP_ANONYMOUS |MAP_PRIVATE ,-1 ,0 );if (map ==MAP_FAILED ) {perror ("test_mlock_locked mmap" ); goto out;@@ -445,7 +445,7 @@ static int test_lock_onfault_of_present()uint64_t page1_flags, page2_flags; map =mmap (NULL ,2 * page_size,PROT_READ |PROT_WRITE , -MAP_ANONYMOUS |MAP_PRIVATE ,0 ,0 ); +MAP_ANONYMOUS |MAP_PRIVATE ,-1 ,0 );if (map ==MAP_FAILED ) {perror ("test_mlock_locked mmap" ); goto out;@@ -492,7 +492,7 @@ static int test_munlockall()unsigned long page_size =getpagesize (); map =mmap (NULL ,2 * page_size,PROT_READ |PROT_WRITE , -MAP_ANONYMOUS |MAP_PRIVATE ,0 ,0 ); +MAP_ANONYMOUS |MAP_PRIVATE ,-1 ,0 );if (map ==MAP_FAILED ) {perror ("test_munlockall mmap" );@@ -518,7 +518,7 @@ static int test_munlockall()munmap (map,2 * page_size); map =mmap (NULL ,2 * page_size,PROT_READ |PROT_WRITE , -MAP_ANONYMOUS |MAP_PRIVATE ,0 ,0 ); +MAP_ANONYMOUS |MAP_PRIVATE ,-1 ,0 );if (map ==MAP_FAILED ) {perror ("test_munlockall second mmap" );@@ -573,7 +573,7 @@ static int test_vma_management(bool call_mlock)struct vm_boundaries page3; map =mmap (NULL ,3 * page_size,PROT_READ |PROT_WRITE , -MAP_ANONYMOUS |MAP_PRIVATE ,0 ,0 ); +MAP_ANONYMOUS |MAP_PRIVATE ,-1 ,0 );if (map ==MAP_FAILED ) {perror ("mmap()" );return ret;tools/testing/selftests/vm/on-fault-limit.c +1
-1@@ -26,7 +26,7 @@ static int test_limit(void) } map =mmap (NULL ,2 * lims.rlim_max,PROT_READ |PROT_WRITE , -MAP_PRIVATE |MAP_ANONYMOUS |MAP_POPULATE ,0 ,0 ); +MAP_PRIVATE |MAP_ANONYMOUS |MAP_POPULATE ,-1 ,0 );if (map !=MAP_FAILED )printf ("mmap should have failed, but didn't\n" );else {tools/testing/selftests/vm/thuge-gen.c +1
-2@@ -146,7 +146,7 @@ void test_mmap(unsigned long size, unsigned flags) before =read_free (size); map =mmap (NULL , size*NUM_PAGES ,PROT_READ |PROT_WRITE , -MAP_PRIVATE |MAP_ANONYMOUS |MAP_HUGETLB |flags,0 ,0 ); +MAP_PRIVATE |MAP_ANONYMOUS |MAP_HUGETLB |flags,-1 ,0 );if (map == (char *)-1 )err ("mmap" );memset (map,0xff , size*NUM_PAGES ); --2.10.0
Andrew Morton
Acked-byQuoted 11 lines · On Fri, 24 Mar 2017 10:47:55 -0600 Shuah Khan <[email protected]> wrote:
Quoted 11 lines · SeongJae Park
Quoted 10 lines
Man page of
mmap()says that portable applications should ensure fd argument to be -1 ifMAP_ANONYMOUSflag is set as below:``` The mapping is not backed by any file; its contents are initialized to zero. The fd and offset arguments are ignored; however, some implementations require fd to be -1 if MAP_ANONYMOUS (or MAP_ANON) is specified, and portable applications should ensure this. ```
Quoted 8 lines
Quoted 3 lines
Quoted 3 lines
However, few
mmap()calls under selftests/vm/ uses 0 as fd though they useMAP_ANONYMOUSflag. This commit changes the argument to be -1 as recommended.Hi Andrew,
Do you have comments on this patch? Please Ack it if you would like me to take it thorugh ksefltest tree.
Shuah Khan
maintainerQuoted 17 lines · Andrew Morton
Quoted 15 lines · On Fri, 24 Mar 2017 10:47:55 -0600 Shuah Khan <[email protected]> wrote:
Quoted 14 lines · SeongJae Park
Quoted 13 lines
Man page of
mmap()says that portable applications should ensure fd argument to be -1 ifMAP_ANONYMOUSflag is set as below:``` The mapping is not backed by any file; its contents are initialized to zero. The fd and offset arguments are ignored; however, some implementations require fd to be -1 if MAP_ANONYMOUS (or MAP_ANON) is specified, and portable applications should ensure this. ```However, few
mmap()calls under selftests/vm/ uses 0 as fd though they useMAP_ANONYMOUSflag. This commit changes the argument to be -1 as recommended.
Quoted 8 lines
Quoted 4 lines
Hi Andrew,
Do you have comments on this patch? Please Ack it if you would like me to take it thorugh ksefltest tree.
Acked-by Andrew Morton <[email protected]>
Thanks. Applied to linux-kselftest next
-- Shuah