linux/tools/testing/selftests/vm/on-fault-limit.c
SeongJae Park 70d1a4e668 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.

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>
2017-04-11 09:41:38 -06:00

48 lines
806 B
C

#include <sys/mman.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <sys/time.h>
#include <sys/resource.h>
#ifndef MCL_ONFAULT
#define MCL_ONFAULT (MCL_FUTURE << 1)
#endif
static int test_limit(void)
{
int ret = 1;
struct rlimit lims;
void *map;
if (getrlimit(RLIMIT_MEMLOCK, &lims)) {
perror("getrlimit");
return ret;
}
if (mlockall(MCL_ONFAULT | MCL_FUTURE)) {
perror("mlockall");
return ret;
}
map = mmap(NULL, 2 * lims.rlim_max, PROT_READ | PROT_WRITE,
MAP_PRIVATE | MAP_ANONYMOUS | MAP_POPULATE, -1, 0);
if (map != MAP_FAILED)
printf("mmap should have failed, but didn't\n");
else {
ret = 0;
munmap(map, 2 * lims.rlim_max);
}
munlockall();
return ret;
}
int main(int argc, char **argv)
{
int ret = 0;
ret += test_limit();
return ret;
}