selftests/vm: Set mmap()'s fd as -1 for MAP_ANONYMOUS flag
authorSeongJae Park <sj38.park@gmail.com>
Wed, 15 Mar 2017 13:19:26 +0000 (22:19 +0900)
committerShuah Khan <shuahkh@osg.samsung.com>
Tue, 11 Apr 2017 15:41:38 +0000 (09:41 -0600)
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.

Signed-off-by: SeongJae Park <sj38.park@gmail.com>
Acked-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Shuah Khan <shuahkh@osg.samsung.com>
tools/testing/selftests/vm/map_hugetlb.c
tools/testing/selftests/vm/mlock2-tests.c
tools/testing/selftests/vm/on-fault-limit.c
tools/testing/selftests/vm/thuge-gen.c

index addcd6fc1ecc674684bd8c68e4839faa134433f9..77687ab59f77321b3858128c553d73271ac1b618 100644 (file)
@@ -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);
index ff0cda2b19c97fd0ad7f75906e9c348094934a7b..e5dbc87b4297778520bf1cc4a2475774410f1bca 100644 (file)
@@ -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;
index 0ae458f32fdb90c0ad0ac7c05086c5e4f34d7ed4..7f96a5c2e2924ccfe0ec3b67b617ab014c56c81c 100644 (file)
@@ -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 {
index 0bc737a75150bf225f6b3508182341536b66c88e..88a2ab535e0129106dcdca2d7acb4235cd0557ce 100644 (file)
@@ -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);