Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > linux.kernel > #1472417
| From | wei.guo.simon@gmail.com |
|---|---|
| Newsgroups | linux.kernel |
| Subject | [PATCH 4/4] selftests/vm: add test for mlock() when areas are intersected. |
| Date | 2016-08-30 13:10 +0200 |
| Message-ID | <sbP5w-41d-19@gated-at.bofh.it> (permalink) |
| References | <sbP5v-41d-7@gated-at.bofh.it> |
| Organization | linux.* mail to news gateway |
From: Simon Guo <wei.guo.simon@gmail.com>
This patch adds mlock() test for multiple invocation on
the same address area, and verify it doesn't mess the
rlimit mlock limitation.
Signed-off-by: Simon Guo <wei.guo.simon@gmail.com>
---
tools/testing/selftests/vm/.gitignore | 1 +
tools/testing/selftests/vm/Makefile | 4 ++
tools/testing/selftests/vm/mlock-intersect-test.c | 76 +++++++++++++++++++++++
3 files changed, 81 insertions(+)
create mode 100644 tools/testing/selftests/vm/mlock-intersect-test.c
diff --git a/tools/testing/selftests/vm/.gitignore b/tools/testing/selftests/vm/.gitignore
index a937a9d..142c565 100644
--- a/tools/testing/selftests/vm/.gitignore
+++ b/tools/testing/selftests/vm/.gitignore
@@ -7,3 +7,4 @@ mlock2-tests
on-fault-limit
transhuge-stress
userfaultfd
+mlock-intersect-test
diff --git a/tools/testing/selftests/vm/Makefile b/tools/testing/selftests/vm/Makefile
index e4bb1de..a0412a8 100644
--- a/tools/testing/selftests/vm/Makefile
+++ b/tools/testing/selftests/vm/Makefile
@@ -10,6 +10,7 @@ BINARIES += on-fault-limit
BINARIES += thuge-gen
BINARIES += transhuge-stress
BINARIES += userfaultfd
+BINARIES += mlock-intersect-test
all: $(BINARIES)
%: %.c
@@ -17,6 +18,9 @@ all: $(BINARIES)
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
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 0000000..f78e68a
--- /dev/null
+++ b/tools/testing/selftests/vm/mlock-intersect-test.c
@@ -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;
+}
--
1.8.3.1
Back to linux.kernel | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
[PATCH 0/4] mm: mlock: fix some locked_vm counting issues wei.guo.simon@gmail.com - 2016-08-30 13:10 +0200
[PATCH 1/4] mm: mlock: check against vma for actual mlock() size wei.guo.simon@gmail.com - 2016-08-30 13:10 +0200
Re: [PATCH 1/4] mm: mlock: check against vma for actual mlock() size "Kirill A. Shutemov" <kirill@shutemov.name> - 2016-08-30 13:40 +0200
[PATCH 3/4] selftest: split mlock2_ funcs into separate mlock2.h wei.guo.simon@gmail.com - 2016-08-30 13:10 +0200
[PATCH 2/4] mm: mlock: avoid increase mm->locked_vm on mlock() when already mlock2(,MLOCK_ONFAULT) wei.guo.simon@gmail.com - 2016-08-30 13:10 +0200
Re: [PATCH 2/4] mm: mlock: avoid increase mm->locked_vm on mlock() when already mlock2(,MLOCK_ONFAULT) "Kirill A. Shutemov" <kirill@shutemov.name> - 2016-08-30 13:40 +0200
[PATCH 4/4] selftests/vm: add test for mlock() when areas are intersected. wei.guo.simon@gmail.com - 2016-08-30 13:10 +0200
Re: [PATCH 4/4] selftests/vm: add test for mlock() when areas are intersected. David Rientjes <rientjes@google.com> - 2016-09-01 01:20 +0200
Re: [PATCH 4/4] selftests/vm: add test for mlock() when areas are intersected. Simon Guo <wei.guo.simon@gmail.com> - 2016-09-01 09:20 +0200
csiph-web