aboutsummaryrefslogtreecommitdiffstats
path: root/tools/testing/selftests/vm
diff options
context:
space:
mode:
authorSimon Guo <wei.guo.simon@gmail.com>2016-10-07 19:59:46 -0400
committerLinus Torvalds <torvalds@linux-foundation.org>2016-10-07 21:46:28 -0400
commit1448d4d8931c53a9b965a6883055a4c6150f859a (patch)
tree892c23af8c93274a60a60048e39f6c1e64cbe849 /tools/testing/selftests/vm
parentc7f032bbe4cacb57e49d5c48bf06de8dc28a449f (diff)
selftests/vm: add test for mlock() when areas are intersected
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/1472554781-9835-5-git-send-email-wei.guo.simon@gmail.com Signed-off-by: Simon Guo <wei.guo.simon@gmail.com> Cc: Alexey Klimov <klimov.linux@gmail.com> Cc: Eric B Munson <emunson@akamai.com> Cc: Geert Uytterhoeven <geert@linux-m68k.org> Cc: "Kirill A. Shutemov" <kirill.shutemov@linux.intel.com> Cc: Mel Gorman <mgorman@techsingularity.net> Cc: Michal Hocko <mhocko@suse.com> Cc: Shuah Khan <shuah@kernel.org> Cc: Simon Guo <wei.guo.simon@gmail.com> Cc: Thierry Reding <treding@nvidia.com> Cc: Vlastimil Babka <vbabka@suse.cz> Cc: David Rientjes <rientjes@google.com> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
Diffstat (limited to 'tools/testing/selftests/vm')
-rw-r--r--tools/testing/selftests/vm/.gitignore1
-rw-r--r--tools/testing/selftests/vm/Makefile4
-rw-r--r--tools/testing/selftests/vm/mlock-intersect-test.c76
3 files changed, 81 insertions, 0 deletions
diff --git a/tools/testing/selftests/vm/.gitignore b/tools/testing/selftests/vm/.gitignore
index a937a9d26b60..142c565bb351 100644
--- a/tools/testing/selftests/vm/.gitignore
+++ b/tools/testing/selftests/vm/.gitignore
@@ -7,3 +7,4 @@ mlock2-tests
7on-fault-limit 7on-fault-limit
8transhuge-stress 8transhuge-stress
9userfaultfd 9userfaultfd
10mlock-intersect-test
diff --git a/tools/testing/selftests/vm/Makefile b/tools/testing/selftests/vm/Makefile
index e4bb1de1d526..a0412a80b679 100644
--- a/tools/testing/selftests/vm/Makefile
+++ b/tools/testing/selftests/vm/Makefile
@@ -10,6 +10,7 @@ BINARIES += on-fault-limit
10BINARIES += thuge-gen 10BINARIES += thuge-gen
11BINARIES += transhuge-stress 11BINARIES += transhuge-stress
12BINARIES += userfaultfd 12BINARIES += userfaultfd
13BINARIES += mlock-intersect-test
13 14
14all: $(BINARIES) 15all: $(BINARIES)
15%: %.c 16%: %.c
@@ -17,6 +18,9 @@ all: $(BINARIES)
17userfaultfd: userfaultfd.c ../../../../usr/include/linux/kernel.h 18userfaultfd: userfaultfd.c ../../../../usr/include/linux/kernel.h
18 $(CC) $(CFLAGS) -O2 -o $@ $< -lpthread 19 $(CC) $(CFLAGS) -O2 -o $@ $< -lpthread
19 20
21mlock-intersect-test: mlock-intersect-test.c
22 $(CC) $(CFLAGS) -o $@ $< -lcap
23
20../../../../usr/include/linux/kernel.h: 24../../../../usr/include/linux/kernel.h:
21 make -C ../../../.. headers_install 25 make -C ../../../.. headers_install
22 26
diff --git a/tools/testing/selftests/vm/mlock-intersect-test.c b/tools/testing/selftests/vm/mlock-intersect-test.c
new file mode 100644
index 000000000000..f78e68a0967c
--- /dev/null
+++ b/tools/testing/selftests/vm/mlock-intersect-test.c
@@ -0,0 +1,76 @@
1/*
2 * It tests the duplicate mlock result:
3 * - the ulimit of lock page is 64k
4 * - allocate address area 64k starting from p
5 * - mlock [p -- p + 30k]
6 * - Then mlock address [ p -- p + 40k ]
7 *
8 * It should succeed since totally we locked
9 * 40k < 64k limitation.
10 *
11 * It should not be run with CAP_IPC_LOCK.
12 */
13#include <stdlib.h>
14#include <stdio.h>
15#include <unistd.h>
16#include <sys/resource.h>
17#include <sys/capability.h>
18#include <sys/mman.h>
19#include "mlock2.h"
20
21int main(int argc, char **argv)
22{
23 struct rlimit new;
24 char *p = NULL;
25 cap_t cap = cap_init();
26 int i;
27
28 /* drop capabilities including CAP_IPC_LOCK */
29 if (cap_set_proc(cap))
30 return -1;
31
32 /* set mlock limits to 64k */
33 new.rlim_cur = 65536;
34 new.rlim_max = 65536;
35 setrlimit(RLIMIT_MEMLOCK, &new);
36
37 /* test VM_LOCK */
38 p = malloc(1024 * 64);
39 if (mlock(p, 1024 * 30)) {
40 printf("mlock() 30k return failure.\n");
41 return -1;
42 }
43 for (i = 0; i < 10; i++) {
44 if (mlock(p, 1024 * 40)) {
45 printf("mlock() #%d 40k returns failure.\n", i);
46 return -1;
47 }
48 }
49 for (i = 0; i < 10; i++) {
50 if (mlock2_(p, 1024 * 40, MLOCK_ONFAULT)) {
51 printf("mlock2_() #%d 40k returns failure.\n", i);
52 return -1;
53 }
54 }
55 free(p);
56
57 /* Test VM_LOCKONFAULT */
58 p = malloc(1024 * 64);
59 if (mlock2_(p, 1024 * 30, MLOCK_ONFAULT)) {
60 printf("mlock2_() 30k return failure.\n");
61 return -1;
62 }
63 for (i = 0; i < 10; i++) {
64 if (mlock2_(p, 1024 * 40, MLOCK_ONFAULT)) {
65 printf("mlock2_() #%d 40k returns failure.\n", i);
66 return -1;
67 }
68 }
69 for (i = 0; i < 10; i++) {
70 if (mlock(p, 1024 * 40)) {
71 printf("mlock() #%d 40k returns failure.\n", i);
72 return -1;
73 }
74 }
75 return 0;
76}