Skip to content

Commit

Permalink
selftests/vm: add test for mlock() when areas are intersected
Browse files Browse the repository at this point in the history
This patch adds mlock() test for multiple invocation on the same address
area, and verify it doesn't mess the rlimit mlock limitation.

Link: http://lkml.kernel.org/r/[email protected]
Signed-off-by: Simon Guo <[email protected]>
Cc: Alexey Klimov <[email protected]>
Cc: Eric B Munson <[email protected]>
Cc: Geert Uytterhoeven <[email protected]>
Cc: "Kirill A. Shutemov" <[email protected]>
Cc: Mel Gorman <[email protected]>
Cc: Michal Hocko <[email protected]>
Cc: Shuah Khan <[email protected]>
Cc: Simon Guo <[email protected]>
Cc: Thierry Reding <[email protected]>
Cc: Vlastimil Babka <[email protected]>
Cc: David Rientjes <[email protected]>
Signed-off-by: Andrew Morton <[email protected]>
Signed-off-by: Linus Torvalds <[email protected]>
  • Loading branch information
justdoitqd authored and torvalds committed Oct 8, 2016
1 parent c7f032b commit 1448d4d
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 0 deletions.
1 change: 1 addition & 0 deletions tools/testing/selftests/vm/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@ mlock2-tests
on-fault-limit
transhuge-stress
userfaultfd
mlock-intersect-test
4 changes: 4 additions & 0 deletions tools/testing/selftests/vm/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,17 @@ BINARIES += on-fault-limit
BINARIES += thuge-gen
BINARIES += transhuge-stress
BINARIES += userfaultfd
BINARIES += mlock-intersect-test

all: $(BINARIES)
%: %.c
$(CC) $(CFLAGS) -o $@ $^ -lrt
userfaultfd: userfaultfd.c ../../../../usr/include/linux/kernel.h
$(CC) $(CFLAGS) -O2 -o $@ $< -lpthread

mlock-intersect-test: mlock-intersect-test.c
$(CC) $(CFLAGS) -o $@ $< -lcap

../../../../usr/include/linux/kernel.h:
make -C ../../../.. headers_install

Expand Down
76 changes: 76 additions & 0 deletions tools/testing/selftests/vm/mlock-intersect-test.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
/*
* It tests the duplicate mlock result:
* - the ulimit of lock page is 64k
* - allocate address area 64k starting from p
* - mlock [p -- p + 30k]
* - Then mlock address [ p -- p + 40k ]
*
* It should succeed since totally we locked
* 40k < 64k limitation.
*
* It should not be run with CAP_IPC_LOCK.
*/
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <sys/resource.h>
#include <sys/capability.h>
#include <sys/mman.h>
#include "mlock2.h"

int main(int argc, char **argv)
{
struct rlimit new;
char *p = NULL;
cap_t cap = cap_init();
int i;

/* drop capabilities including CAP_IPC_LOCK */
if (cap_set_proc(cap))
return -1;

/* set mlock limits to 64k */
new.rlim_cur = 65536;
new.rlim_max = 65536;
setrlimit(RLIMIT_MEMLOCK, &new);

/* test VM_LOCK */
p = malloc(1024 * 64);
if (mlock(p, 1024 * 30)) {
printf("mlock() 30k return failure.\n");
return -1;
}
for (i = 0; i < 10; i++) {
if (mlock(p, 1024 * 40)) {
printf("mlock() #%d 40k returns failure.\n", i);
return -1;
}
}
for (i = 0; i < 10; i++) {
if (mlock2_(p, 1024 * 40, MLOCK_ONFAULT)) {
printf("mlock2_() #%d 40k returns failure.\n", i);
return -1;
}
}
free(p);

/* Test VM_LOCKONFAULT */
p = malloc(1024 * 64);
if (mlock2_(p, 1024 * 30, MLOCK_ONFAULT)) {
printf("mlock2_() 30k return failure.\n");
return -1;
}
for (i = 0; i < 10; i++) {
if (mlock2_(p, 1024 * 40, MLOCK_ONFAULT)) {
printf("mlock2_() #%d 40k returns failure.\n", i);
return -1;
}
}
for (i = 0; i < 10; i++) {
if (mlock(p, 1024 * 40)) {
printf("mlock() #%d 40k returns failure.\n", i);
return -1;
}
}
return 0;
}

0 comments on commit 1448d4d

Please sign in to comment.