* [LTP] [PATCH 1/4] lapi/syscalls: Add syscall numbers for mlock2
@ 2018-08-21 9:38 Xiao Yang
2018-08-21 9:38 ` [LTP] [PATCH 2/4] safe_macros: Add SAFE_MLOCK() and SAFE_MUNLOCK() macros Xiao Yang
` (3 more replies)
0 siblings, 4 replies; 16+ messages in thread
From: Xiao Yang @ 2018-08-21 9:38 UTC (permalink / raw)
To: ltp
Signed-off-by: Xiao Yang <yangx.jy@cn.fujitsu.com>
---
include/lapi/syscalls/aarch64.in | 1 +
include/lapi/syscalls/arm.in | 1 +
include/lapi/syscalls/hppa.in | 1 +
include/lapi/syscalls/i386.in | 1 +
include/lapi/syscalls/ia64.in | 1 +
include/lapi/syscalls/powerpc.in | 1 +
include/lapi/syscalls/powerpc64.in | 1 +
include/lapi/syscalls/s390.in | 1 +
include/lapi/syscalls/s390x.in | 1 +
include/lapi/syscalls/sh.in | 1 +
include/lapi/syscalls/sparc.in | 1 +
include/lapi/syscalls/sparc64.in | 1 +
include/lapi/syscalls/x86_64.in | 1 +
13 files changed, 13 insertions(+)
diff --git a/include/lapi/syscalls/aarch64.in b/include/lapi/syscalls/aarch64.in
index 2c8c589..9ac7f99 100644
--- a/include/lapi/syscalls/aarch64.in
+++ b/include/lapi/syscalls/aarch64.in
@@ -256,5 +256,6 @@ sendmmsg 269
kcmp 272
getrandom 278
memfd_create 279
+mlock2 284
copy_file_range 285
_sysctl 1078
diff --git a/include/lapi/syscalls/arm.in b/include/lapi/syscalls/arm.in
index c44adcd..774e1c7 100644
--- a/include/lapi/syscalls/arm.in
+++ b/include/lapi/syscalls/arm.in
@@ -341,4 +341,5 @@ renameat2 (__NR_SYSCALL_BASE+382)
getrandom (__NR_SYSCALL_BASE+384)
memfd_create (__NR_SYSCALL_BASE+385)
execveat (__NR_SYSCALL_BASE+387)
+mlock2 (__NR_SYSCALL_BASE+390)
copy_file_range (__NR_SYSCALL_BASE+391)
diff --git a/include/lapi/syscalls/hppa.in b/include/lapi/syscalls/hppa.in
index 92aee9f..3db9780 100644
--- a/include/lapi/syscalls/hppa.in
+++ b/include/lapi/syscalls/hppa.in
@@ -17,4 +17,5 @@ splice 291
tee 293
vmsplice 294
memfd_create 340
+mlock2 345
copy_file_range 346
diff --git a/include/lapi/syscalls/i386.in b/include/lapi/syscalls/i386.in
index 19f0148..6a72b43 100644
--- a/include/lapi/syscalls/i386.in
+++ b/include/lapi/syscalls/i386.in
@@ -341,4 +341,5 @@ renameat2 354
getrandom 355
memfd_create 356
execveat 358
+mlock2 376
copy_file_range 377
diff --git a/include/lapi/syscalls/ia64.in b/include/lapi/syscalls/ia64.in
index a3abc99..2788193 100644
--- a/include/lapi/syscalls/ia64.in
+++ b/include/lapi/syscalls/ia64.in
@@ -296,4 +296,5 @@ prlimit64 1325
renameat2 1338
getrandom 1339
memfd_create 1340
+mlock2 1346
copy_file_range 1347
diff --git a/include/lapi/syscalls/powerpc.in b/include/lapi/syscalls/powerpc.in
index 11ddca3..54c1941 100644
--- a/include/lapi/syscalls/powerpc.in
+++ b/include/lapi/syscalls/powerpc.in
@@ -347,4 +347,5 @@ sched_getattr 356
renameat2 357
getrandom 359
memfd_create 360
+mlock2 378
copy_file_range 379
diff --git a/include/lapi/syscalls/powerpc64.in b/include/lapi/syscalls/powerpc64.in
index 11ddca3..54c1941 100644
--- a/include/lapi/syscalls/powerpc64.in
+++ b/include/lapi/syscalls/powerpc64.in
@@ -347,4 +347,5 @@ sched_getattr 356
renameat2 357
getrandom 359
memfd_create 360
+mlock2 378
copy_file_range 379
diff --git a/include/lapi/syscalls/s390.in b/include/lapi/syscalls/s390.in
index d95b282..47a04de 100644
--- a/include/lapi/syscalls/s390.in
+++ b/include/lapi/syscalls/s390.in
@@ -332,4 +332,5 @@ renameat2 347
getrandom 349
memfd_create 350
execveat 354
+mlock2 374
copy_file_range 375
diff --git a/include/lapi/syscalls/s390x.in b/include/lapi/syscalls/s390x.in
index 98c861f..83732ff 100644
--- a/include/lapi/syscalls/s390x.in
+++ b/include/lapi/syscalls/s390x.in
@@ -331,4 +331,5 @@ sched_getattr 346
renameat2 347
getrandom 349
memfd_create 350
+mlock2 374
copy_file_range 375
diff --git a/include/lapi/syscalls/sh.in b/include/lapi/syscalls/sh.in
index c605b14..407be7a 100644
--- a/include/lapi/syscalls/sh.in
+++ b/include/lapi/syscalls/sh.in
@@ -364,4 +364,5 @@ fanotify_mark 368
prlimit64 369
kcmp 378
memfd_create 385
+mlock2 390
copy_file_range 391
diff --git a/include/lapi/syscalls/sparc.in b/include/lapi/syscalls/sparc.in
index 296d694..2b06a79 100644
--- a/include/lapi/syscalls/sparc.in
+++ b/include/lapi/syscalls/sparc.in
@@ -336,4 +336,5 @@ kcmp 341
renameat2 345
getrandom 347
memfd_create 348
+mlock2 356
copy_file_range 357
diff --git a/include/lapi/syscalls/sparc64.in b/include/lapi/syscalls/sparc64.in
index 169347a..8c8a866 100644
--- a/include/lapi/syscalls/sparc64.in
+++ b/include/lapi/syscalls/sparc64.in
@@ -312,4 +312,5 @@ kcmp 341
renameat2 345
getrandom 347
memfd_create 348
+mlock2 356
copy_file_range 357
diff --git a/include/lapi/syscalls/x86_64.in b/include/lapi/syscalls/x86_64.in
index 7907c31..dec9d3b 100644
--- a/include/lapi/syscalls/x86_64.in
+++ b/include/lapi/syscalls/x86_64.in
@@ -308,4 +308,5 @@ renameat2 316
getrandom 318
memfd_create 319
execveat 322
+mlock2 325
copy_file_range 326
--
1.8.3.1
^ permalink raw reply related [flat|nested] 16+ messages in thread* [LTP] [PATCH 2/4] safe_macros: Add SAFE_MLOCK() and SAFE_MUNLOCK() macros 2018-08-21 9:38 [LTP] [PATCH 1/4] lapi/syscalls: Add syscall numbers for mlock2 Xiao Yang @ 2018-08-21 9:38 ` Xiao Yang 2018-08-29 12:11 ` Jan Stancek 2018-08-21 9:38 ` [LTP] [PATCH 3/4] syscalls/mlock201: Add new testcase Xiao Yang ` (2 subsequent siblings) 3 siblings, 1 reply; 16+ messages in thread From: Xiao Yang @ 2018-08-21 9:38 UTC (permalink / raw) To: ltp Signed-off-by: Xiao Yang <yangx.jy@cn.fujitsu.com> --- include/tst_safe_macros.h | 8 ++++++++ lib/safe_macros.c | 28 ++++++++++++++++++++++++++++ 2 files changed, 36 insertions(+) diff --git a/include/tst_safe_macros.h b/include/tst_safe_macros.h index a754399..dc6c1f7 100644 --- a/include/tst_safe_macros.h +++ b/include/tst_safe_macros.h @@ -482,6 +482,14 @@ int safe_mknod(const char *file, const int lineno, const char *pathname, #define SAFE_MKNOD(pathname, mode, dev) \ safe_mknod(__FILE__, __LINE__, (pathname), (mode), (dev)) +int safe_mlock(const char *file, const int lineno, const char *addr, + size_t len); +#define SAFE_MLOCK(addr, len) safe_mlock(__FILE__, __LINE__, (addr), (len)) + +int safe_munlock(const char *file, const int lineno, const char *addr, + size_t len); +#define SAFE_MUNLOCK(addr, len) safe_munlock(__FILE__, __LINE__, (addr), (len)) + int safe_fanotify_init(const char *file, const int lineno, unsigned int flags, unsigned int event_f_flags); #define SAFE_FANOTIFY_INIT(fan, mode) \ diff --git a/lib/safe_macros.c b/lib/safe_macros.c index 03bd7aa..fa92a6f 100644 --- a/lib/safe_macros.c +++ b/lib/safe_macros.c @@ -1006,3 +1006,31 @@ int safe_mknod(const char *file, const int lineno, const char *pathname, return rval; } + +int safe_mlock(const char *file, const int lineno, const void *addr, + size_t len) +{ + int rval; + + rval = mlock(addr, len); + if (rval == -1) { + tst_brkm(TBROK | TERRNO, NULL, + "%s:%d: mlock() failed", file, lineno); + } + + return rval; +} + +int safe_munlock(const char *file, const int lineno, const void *addr, + size_t len) +{ + int rval; + + rval = munlock(addr, len); + if (rval == -1) { + tst_brkm(TBROK | TERRNO, NULL, + "%s:%d: munlock() failed", file, lineno); + } + + return rval; +} -- 1.8.3.1 ^ permalink raw reply related [flat|nested] 16+ messages in thread
* [LTP] [PATCH 2/4] safe_macros: Add SAFE_MLOCK() and SAFE_MUNLOCK() macros 2018-08-21 9:38 ` [LTP] [PATCH 2/4] safe_macros: Add SAFE_MLOCK() and SAFE_MUNLOCK() macros Xiao Yang @ 2018-08-29 12:11 ` Jan Stancek 0 siblings, 0 replies; 16+ messages in thread From: Jan Stancek @ 2018-08-29 12:11 UTC (permalink / raw) To: ltp ----- Original Message ----- > Signed-off-by: Xiao Yang <yangx.jy@cn.fujitsu.com> > --- > include/tst_safe_macros.h | 8 ++++++++ > lib/safe_macros.c | 28 ++++++++++++++++++++++++++++ > 2 files changed, 36 insertions(+) ACK ^ permalink raw reply [flat|nested] 16+ messages in thread
* [LTP] [PATCH 3/4] syscalls/mlock201: Add new testcase 2018-08-21 9:38 [LTP] [PATCH 1/4] lapi/syscalls: Add syscall numbers for mlock2 Xiao Yang 2018-08-21 9:38 ` [LTP] [PATCH 2/4] safe_macros: Add SAFE_MLOCK() and SAFE_MUNLOCK() macros Xiao Yang @ 2018-08-21 9:38 ` Xiao Yang 2018-08-29 12:19 ` Jan Stancek 2018-08-21 9:38 ` [LTP] [PATCH " Xiao Yang 2018-08-29 12:11 ` [LTP] [PATCH 1/4] lapi/syscalls: Add syscall numbers for mlock2 Jan Stancek 3 siblings, 1 reply; 16+ messages in thread From: Xiao Yang @ 2018-08-21 9:38 UTC (permalink / raw) To: ltp Check the basic functionality of mlock2(2) since kernel v2.6.9. Signed-off-by: Xiao Yang <yangx.jy@cn.fujitsu.com> --- include/lapi/mlock2.h | 16 +++++ runtest/ltplite | 2 + runtest/stress.part3 | 2 + runtest/syscalls | 2 + testcases/kernel/syscalls/mlock2/.gitignore | 1 + testcases/kernel/syscalls/mlock2/Makefile | 8 +++ testcases/kernel/syscalls/mlock2/mlock201.c | 103 ++++++++++++++++++++++++++++ 7 files changed, 134 insertions(+) create mode 100644 include/lapi/mlock2.h create mode 100644 testcases/kernel/syscalls/mlock2/.gitignore create mode 100644 testcases/kernel/syscalls/mlock2/Makefile create mode 100644 testcases/kernel/syscalls/mlock2/mlock201.c diff --git a/include/lapi/mlock2.h b/include/lapi/mlock2.h new file mode 100644 index 0000000..fa2b2de --- /dev/null +++ b/include/lapi/mlock2.h @@ -0,0 +1,16 @@ +// SPDX-License-Identifier: GPL-2.0-or-later +/* + * Copyright (c) 2018 FUJITSU LIMITED. All rights reserved. + * Author: Xiao Yang <yangx.jy@cn.fujitsu.com> + */ + +#ifndef LAPI_MLOCK2_H__ +# define LAPI_MLOCK2_H__ + +#include <linux/mman.h> + +#ifndef MLOCK_ONFAULT +# define MLOCK_ONFAULT 0x01 +#endif + +#endif /* LAPI_MLOCK2_H__ */ diff --git a/runtest/ltplite b/runtest/ltplite index 9ca6c42..270c649 100644 --- a/runtest/ltplite +++ b/runtest/ltplite @@ -443,6 +443,8 @@ mknod09 mknod09 mlock01 mlock01 mlock02 mlock02 +mlock201 mlock201 + qmm01 mmap001 -m 1 mmap01 mmap01 mmap02 mmap02 diff --git a/runtest/stress.part3 b/runtest/stress.part3 index ec18dcf..6850572 100644 --- a/runtest/stress.part3 +++ b/runtest/stress.part3 @@ -369,6 +369,8 @@ mknod09 mknod09 mlock01 mlock01 mlock02 mlock02 +mlock201 mlock201 + qmm01 mmap001 -m 1 mmap01 mmap01 mmap02 mmap02 diff --git a/runtest/syscalls b/runtest/syscalls index 3161d91..5d84d48 100644 --- a/runtest/syscalls +++ b/runtest/syscalls @@ -633,6 +633,8 @@ mlock02 mlock02 mlock03 mlock03 -i 20 mlock04 mlock04 +mlock201 mlock201 + qmm01 mmap001 -m 1 mmap01 mmap01 mmap02 mmap02 diff --git a/testcases/kernel/syscalls/mlock2/.gitignore b/testcases/kernel/syscalls/mlock2/.gitignore new file mode 100644 index 0000000..431eff8 --- /dev/null +++ b/testcases/kernel/syscalls/mlock2/.gitignore @@ -0,0 +1 @@ +/mlock201 diff --git a/testcases/kernel/syscalls/mlock2/Makefile b/testcases/kernel/syscalls/mlock2/Makefile new file mode 100644 index 0000000..427df06 --- /dev/null +++ b/testcases/kernel/syscalls/mlock2/Makefile @@ -0,0 +1,8 @@ +# SPDX-License-Identifier: GPL-2.0-or-later +# Copyright (c) 2018 FUJITSU LIMITED. All rights reserved. +# Author: Xiao Yang <yangx.jy@cn.fujitsu.com> + +top_srcdir ?= ../../../.. + +include $(top_srcdir)/include/mk/testcases.mk +include $(top_srcdir)/include/mk/generic_leaf_target.mk diff --git a/testcases/kernel/syscalls/mlock2/mlock201.c b/testcases/kernel/syscalls/mlock2/mlock201.c new file mode 100644 index 0000000..e0517e1 --- /dev/null +++ b/testcases/kernel/syscalls/mlock2/mlock201.c @@ -0,0 +1,103 @@ +// SPDX-License-Identifier: GPL-2.0-or-later +/* + * Copyright (c) 2018 FUJITSU LIMITED. All rights reserved. + * Author: Xiao Yang <yangx.jy@cn.fujitsu.com> + */ +/* + * Description: + * Check the basic functionality of the mlock2(2) since kernel v2.6.9: + * 1) Use mlock2(2) without MLOCK_ONFAULT to lock memory in the specified + * range that is multiples of page size or not, and the VmLck from + * /proc/PID/status shows correct size of memory that is locked by PID. + * 2) Use mlock2(2) with MLOCK_ONFAULT to lock memory in the specified + * range that is multiples of page size or not, and the VmLck from + * /proc/PID/status show correct size of memory that is locked by PID. + */ +#include <errno.h> +#include <unistd.h> +#include <sys/mman.h> +#include <linux/mman.h> + +#include "tst_test.h" +#include "lapi/syscalls.h" +#include "lapi/mlock2.h" + +#define PAGES 8 + +static unsigned long pgsz; +static char *addr; + +static struct tcase { + unsigned long mlock_pgs; + long len_off; + unsigned long exp_pgs; + int flag; +} tcases[] = { + {1, 0, 1, 0}, + {PAGES, 0, PAGES, 0}, + /* mlock2() locks 3 pages if the specified + * range is little more than 2 pages. + */ + {2, 1, 3, 0}, + /* mlock2() locks 2 pages if the specified + * range is little less than 2 pages. + */ + {2, -1, 2, 0}, + {1, 0, 1, MLOCK_ONFAULT}, + {PAGES, 0, PAGES, MLOCK_ONFAULT}, + {2, 1, 3, MLOCK_ONFAULT}, + {2, -1, 2, MLOCK_ONFAULT}, +}; + +static void verify_mlock2(unsigned int n) +{ + struct tcase *tc = &tcases[n]; + unsigned long bsize, asize, act_pgs; + + SAFE_FILE_LINES_SCANF("/proc/self/status", "VmLck: %lu", &bsize); + + TEST(tst_syscall(__NR_mlock2, addr, tc->mlock_pgs * pgsz + tc->len_off, + tc->flag)); + if (TST_RET != 0) { + if (tc->flag && TST_ERR == EINVAL) + tst_res(TCONF, "mlock2() didn't support MLOCK_ONFAULT"); + else + tst_res(TFAIL | TTERRNO, "mlock2() failed"); + return; + } + + SAFE_FILE_LINES_SCANF("/proc/self/status", "VmLck: %lu", &asize); + + act_pgs = (asize - bsize) * 1024 / pgsz; + if (tc->exp_pgs != act_pgs) { + tst_res(TFAIL, "mlock2() locked %lu pages, expected %lu", + tc->exp_pgs, act_pgs); + } else { + tst_res(TPASS, "mlock2() succeeded in locking %lu pages", + tc->exp_pgs); + } + + SAFE_MUNLOCK(addr, tc->mlock_pgs * pgsz + tc->len_off); +} + +static void setup(void) +{ + pgsz = getpagesize(); + addr = SAFE_MMAP(NULL, PAGES * pgsz, PROT_WRITE, + MAP_PRIVATE | MAP_ANONYMOUS, 0, 0); +} + +static void cleanup(void) +{ + if (addr) + SAFE_MUNMAP(addr, PAGES * pgsz); +} + +static struct tst_test test = { + .tcnt = ARRAY_SIZE(tcases), + .test = verify_mlock2, + .setup = setup, + .cleanup = cleanup, + .needs_root = 1, + .min_kver = "2.6.9", +}; -- 1.8.3.1 ^ permalink raw reply related [flat|nested] 16+ messages in thread
* [LTP] [PATCH 3/4] syscalls/mlock201: Add new testcase 2018-08-21 9:38 ` [LTP] [PATCH 3/4] syscalls/mlock201: Add new testcase Xiao Yang @ 2018-08-29 12:19 ` Jan Stancek 2018-08-29 12:42 ` Jan Stancek 2018-08-31 7:13 ` [LTP] [PATCH v2 " Xiao Yang 0 siblings, 2 replies; 16+ messages in thread From: Jan Stancek @ 2018-08-29 12:19 UTC (permalink / raw) To: ltp ----- Original Message ----- > Check the basic functionality of mlock2(2) since kernel v2.6.9. > > Signed-off-by: Xiao Yang <yangx.jy@cn.fujitsu.com> > + * Description: > + * Check the basic functionality of the mlock2(2) since kernel v2.6.9: > + * 1) Use mlock2(2) without MLOCK_ONFAULT to lock memory in the specified > + * range that is multiples of page size or not, and the VmLck from > + * /proc/PID/status shows correct size of memory that is locked by PID. > + * 2) Use mlock2(2) with MLOCK_ONFAULT to lock memory in the specified > + * range that is multiples of page size or not, and the VmLck from > + * /proc/PID/status show correct size of memory that is locked by PID. Can we also check if MLOCK_ONFAULT doesn't make non-present pages present, but it still locks present pages? (mincore(2) or /proc/pid/pagemap) Regards, Jan ^ permalink raw reply [flat|nested] 16+ messages in thread
* [LTP] [PATCH 3/4] syscalls/mlock201: Add new testcase 2018-08-29 12:19 ` Jan Stancek @ 2018-08-29 12:42 ` Jan Stancek 2018-08-30 1:17 ` Xiao Yang 2018-08-31 7:13 ` [LTP] [PATCH v2 " Xiao Yang 1 sibling, 1 reply; 16+ messages in thread From: Jan Stancek @ 2018-08-29 12:42 UTC (permalink / raw) To: ltp ----- Original Message ----- > > ----- Original Message ----- > > Check the basic functionality of mlock2(2) since kernel v2.6.9. Missed this one: mlock2() is available since Linux 4.4. > > > > Signed-off-by: Xiao Yang <yangx.jy@cn.fujitsu.com> > > > + * Description: > > + * Check the basic functionality of the mlock2(2) since kernel v2.6.9: > > + * 1) Use mlock2(2) without MLOCK_ONFAULT to lock memory in the specified > > + * range that is multiples of page size or not, and the VmLck from > > + * /proc/PID/status shows correct size of memory that is locked by PID. > > + * 2) Use mlock2(2) with MLOCK_ONFAULT to lock memory in the specified > > + * range that is multiples of page size or not, and the VmLck from > > + * /proc/PID/status show correct size of memory that is locked by PID. > > Can we also check if MLOCK_ONFAULT doesn't make non-present pages present, > but it still locks present pages? (mincore(2) or /proc/pid/pagemap) > > Regards, > Jan > > -- > Mailing list info: https://lists.linux.it/listinfo/ltp > ^ permalink raw reply [flat|nested] 16+ messages in thread
* [LTP] [PATCH 3/4] syscalls/mlock201: Add new testcase 2018-08-29 12:42 ` Jan Stancek @ 2018-08-30 1:17 ` Xiao Yang 0 siblings, 0 replies; 16+ messages in thread From: Xiao Yang @ 2018-08-30 1:17 UTC (permalink / raw) To: ltp On 2018/08/29 20:42, Jan Stancek wrote: > ----- Original Message ----- >> ----- Original Message ----- >>> Check the basic functionality of mlock2(2) since kernel v2.6.9. > Missed this one: > mlock2() is available since Linux 4.4. Hi Jan, Thanks for your review. It seems that mlock2() has been backported by some distros(e.g. RHEL7.6), so i use tst_syscall() instead of .min_kver to check if mlock2() is supported. From mlock2() manpage: Before Linux 2.6.8, a process must be privileged (CAP_IPC_LOCK) in order to lock memory and the RLIMIT_MEMLOCK soft resource limit defines a limit on how much memory the process may lock. Since Linux 2.6.9, no limits are placed on the amount of memory that a privileged process can lock and the RLIMIT_MEMLOCK soft resource limit instead defines a limit on how much memory an unprivileged\ process may lock. I just check the behavior of mlock2() since Linux 2.6.9. >>> Signed-off-by: Xiao Yang<yangx.jy@cn.fujitsu.com> >>> + * Description: >>> + * Check the basic functionality of the mlock2(2) since kernel v2.6.9: >>> + * 1) Use mlock2(2) without MLOCK_ONFAULT to lock memory in the specified >>> + * range that is multiples of page size or not, and the VmLck from >>> + * /proc/PID/status shows correct size of memory that is locked by PID. >>> + * 2) Use mlock2(2) with MLOCK_ONFAULT to lock memory in the specified >>> + * range that is multiples of page size or not, and the VmLck from >>> + * /proc/PID/status show correct size of memory that is locked by PID. >> Can we also check if MLOCK_ONFAULT doesn't make non-present pages present, >> but it still locks present pages? (mincore(2) or /proc/pid/pagemap) I will try to check this point as you suggested. Thanks, Xiao Yang >> Regards, >> Jan >> >> -- >> Mailing list info: https://lists.linux.it/listinfo/ltp >> > > ^ permalink raw reply [flat|nested] 16+ messages in thread
* [LTP] [PATCH v2 3/4] syscalls/mlock201: Add new testcase 2018-08-29 12:19 ` Jan Stancek 2018-08-29 12:42 ` Jan Stancek @ 2018-08-31 7:13 ` Xiao Yang 2018-08-30 12:41 ` Jan Stancek 2018-08-31 7:13 ` [LTP] [PATCH v2 4/4] syscalls/mlock202: " Xiao Yang 1 sibling, 2 replies; 16+ messages in thread From: Xiao Yang @ 2018-08-31 7:13 UTC (permalink / raw) To: ltp 1) Add SAFE_MINCORE() macro 2) Check the basic functionality of mlock2(2). Note: 1) We use tst_syscall() to check if mlock2() is supported. 2) since kernel v2.6.9, the limits and permissions of mlock2() changed, so we just check mlock2() since the version. Signed-off-by: Xiao Yang <yangx.jy@cn.fujitsu.com> --- include/lapi/mlock2.h | 16 ++++ include/tst_safe_macros.h | 5 + lib/safe_macros.c | 14 +++ runtest/ltplite | 2 + runtest/stress.part3 | 2 + runtest/syscalls | 2 + testcases/kernel/syscalls/mlock2/.gitignore | 1 + testcases/kernel/syscalls/mlock2/Makefile | 8 ++ testcases/kernel/syscalls/mlock2/mlock201.c | 144 ++++++++++++++++++++++++++++ 9 files changed, 194 insertions(+) create mode 100644 include/lapi/mlock2.h create mode 100644 testcases/kernel/syscalls/mlock2/.gitignore create mode 100644 testcases/kernel/syscalls/mlock2/Makefile create mode 100644 testcases/kernel/syscalls/mlock2/mlock201.c diff --git a/include/lapi/mlock2.h b/include/lapi/mlock2.h new file mode 100644 index 0000000..fa2b2de --- /dev/null +++ b/include/lapi/mlock2.h @@ -0,0 +1,16 @@ +// SPDX-License-Identifier: GPL-2.0-or-later +/* + * Copyright (c) 2018 FUJITSU LIMITED. All rights reserved. + * Author: Xiao Yang <yangx.jy@cn.fujitsu.com> + */ + +#ifndef LAPI_MLOCK2_H__ +# define LAPI_MLOCK2_H__ + +#include <linux/mman.h> + +#ifndef MLOCK_ONFAULT +# define MLOCK_ONFAULT 0x01 +#endif + +#endif /* LAPI_MLOCK2_H__ */ diff --git a/include/tst_safe_macros.h b/include/tst_safe_macros.h index dc6c1f7..03657a4 100644 --- a/include/tst_safe_macros.h +++ b/include/tst_safe_macros.h @@ -490,6 +490,11 @@ int safe_munlock(const char *file, const int lineno, const char *addr, size_t len); #define SAFE_MUNLOCK(addr, len) safe_munlock(__FILE__, __LINE__, (addr), (len)) +int safe_mincore(const char *file, const int lineno, void *start, + size_t length, unsigned char *vec); +#define SAFE_MINCORE(start, length, vec) \ + safe_mincore(__FILE__, __LINE__, (start), (length), (vec)) + int safe_fanotify_init(const char *file, const int lineno, unsigned int flags, unsigned int event_f_flags); #define SAFE_FANOTIFY_INIT(fan, mode) \ diff --git a/lib/safe_macros.c b/lib/safe_macros.c index fa92a6f..5cc80d0 100644 --- a/lib/safe_macros.c +++ b/lib/safe_macros.c @@ -1034,3 +1034,17 @@ int safe_munlock(const char *file, const int lineno, const void *addr, return rval; } + +int safe_mincore(const char *file, const int lineno, void *start, + size_t length, unsigned char *vec) +{ + int rval; + + rval = mincore(start, length, vec); + if (rval == -1) { + tst_brkm(TBROK | TERRNO, NULL, + "%s:%d: mincore() failed", file, lineno); + } + + return rval; +} diff --git a/runtest/ltplite b/runtest/ltplite index 9ca6c42..270c649 100644 --- a/runtest/ltplite +++ b/runtest/ltplite @@ -443,6 +443,8 @@ mknod09 mknod09 mlock01 mlock01 mlock02 mlock02 +mlock201 mlock201 + qmm01 mmap001 -m 1 mmap01 mmap01 mmap02 mmap02 diff --git a/runtest/stress.part3 b/runtest/stress.part3 index ec18dcf..6850572 100644 --- a/runtest/stress.part3 +++ b/runtest/stress.part3 @@ -369,6 +369,8 @@ mknod09 mknod09 mlock01 mlock01 mlock02 mlock02 +mlock201 mlock201 + qmm01 mmap001 -m 1 mmap01 mmap01 mmap02 mmap02 diff --git a/runtest/syscalls b/runtest/syscalls index 3161d91..5d84d48 100644 --- a/runtest/syscalls +++ b/runtest/syscalls @@ -633,6 +633,8 @@ mlock02 mlock02 mlock03 mlock03 -i 20 mlock04 mlock04 +mlock201 mlock201 + qmm01 mmap001 -m 1 mmap01 mmap01 mmap02 mmap02 diff --git a/testcases/kernel/syscalls/mlock2/.gitignore b/testcases/kernel/syscalls/mlock2/.gitignore new file mode 100644 index 0000000..431eff8 --- /dev/null +++ b/testcases/kernel/syscalls/mlock2/.gitignore @@ -0,0 +1 @@ +/mlock201 diff --git a/testcases/kernel/syscalls/mlock2/Makefile b/testcases/kernel/syscalls/mlock2/Makefile new file mode 100644 index 0000000..427df06 --- /dev/null +++ b/testcases/kernel/syscalls/mlock2/Makefile @@ -0,0 +1,8 @@ +# SPDX-License-Identifier: GPL-2.0-or-later +# Copyright (c) 2018 FUJITSU LIMITED. All rights reserved. +# Author: Xiao Yang <yangx.jy@cn.fujitsu.com> + +top_srcdir ?= ../../../.. + +include $(top_srcdir)/include/mk/testcases.mk +include $(top_srcdir)/include/mk/generic_leaf_target.mk diff --git a/testcases/kernel/syscalls/mlock2/mlock201.c b/testcases/kernel/syscalls/mlock2/mlock201.c new file mode 100644 index 0000000..d2090bc --- /dev/null +++ b/testcases/kernel/syscalls/mlock2/mlock201.c @@ -0,0 +1,144 @@ +// SPDX-License-Identifier: GPL-2.0-or-later +/* + * Copyright (c) 2018 FUJITSU LIMITED. All rights reserved. + * Author: Xiao Yang <yangx.jy@cn.fujitsu.com> + */ +/* + * Description: + * Check the basic functionality of the mlock2(2) since kernel v2.6.9: + * 1) When we use mlock2() without MLOCK_ONFAULT to lock memory in the + * specified range that is multiples of page size or not, we can + * show correct size of locked memory by VmLck from /proc/PID/status + * and lock all pages including non-present. + * 2) When we use mlock2() with MLOCK_ONFAULT to lock memory in the + * specified range that is multiples of page size or not, we can + * show correct size of locked memory by VmLck from /proc/PID/status + * and just lock present pages. + */ +#include <errno.h> +#include <unistd.h> +#include <string.h> +#include <stdlib.h> +#include <sys/mman.h> +#include <linux/mman.h> + +#include "tst_test.h" +#include "lapi/syscalls.h" +#include "lapi/mlock2.h" + +#define PAGES 8 +#define HPAGES (PAGES / 2) + +static size_t pgsz; +static unsigned char *vec; + +static struct tcase { + size_t pop_pgs; + size_t lock_pgs; + size_t offset; + size_t exp_vmlcks; + size_t exp_pgs; + int flag; +} tcases[] = { + {0, 1, 0, 1, 1, 0}, + {0, PAGES, 0, PAGES, PAGES, 0}, + /* mlock2() locks 3 pages if the specified + * range is little more than 2 pages. + */ + {0, 2, 1, 3, 3, 0}, + /* mlock2() locks 2 pages if the specified + * range is little less than 2 pages. + */ + {0, 2, -1, 2, 2, 0}, + /* mlock2() with MLOCK_ONFAULT just lock present + * pages populated by data. + */ + {0, 1, 0, 1, 0, MLOCK_ONFAULT}, + {HPAGES, PAGES, 0, PAGES, HPAGES, MLOCK_ONFAULT}, + {1, HPAGES, 1, HPAGES + 1, 1, MLOCK_ONFAULT}, + {HPAGES, HPAGES, -1, HPAGES, HPAGES, MLOCK_ONFAULT}, +}; + +static size_t check_locked_pages(char *addr, size_t len, size_t num_pgs) +{ + size_t n; + size_t act_pages = 0; + + SAFE_MINCORE(addr, len, vec); + + for (n = 0; n < num_pgs; n++) { + if (vec[n] & 1) + act_pages++; + } + + return act_pages; +} + +static void verify_mlock2(unsigned int n) +{ + struct tcase *tc = &tcases[n]; + size_t bsize, asize, act_vmlcks, act_pgs; + char *addr; + + addr = SAFE_MMAP(NULL, PAGES * pgsz, PROT_WRITE, + MAP_SHARED | MAP_ANONYMOUS, 0, 0); + + if (tc->pop_pgs) + memset(addr, 0, tc->pop_pgs * pgsz); + + SAFE_FILE_LINES_SCANF("/proc/self/status", "VmLck: %ld", &bsize); + + TEST(tst_syscall(__NR_mlock2, addr, tc->lock_pgs * pgsz + tc->offset, + tc->flag)); + if (TST_RET != 0) { + if (tc->flag && TST_ERR == EINVAL) + tst_res(TCONF, "mlock2() didn't support MLOCK_ONFAULT"); + else + tst_res(TFAIL | TTERRNO, "mlock2() failed"); + goto end2; + } + + SAFE_FILE_LINES_SCANF("/proc/self/status", "VmLck: %ld", &asize); + + act_vmlcks = (asize - bsize) * 1024 / pgsz; + if (tc->exp_vmlcks != act_vmlcks) { + tst_res(TFAIL, "VmLck showed wrong %ld pages, expected %ld", + act_vmlcks, tc->exp_vmlcks); + goto end1; + } + + act_pgs = check_locked_pages(addr, PAGES * pgsz, PAGES); + if (act_pgs != tc->exp_pgs) { + tst_res(TFAIL, "mlock2(%d) locked %ld pages, expected %ld", + tc->flag, act_pgs, tc->exp_pgs); + } else { + tst_res(TPASS, "mlock2(%d) succeeded in locking %ld pages", + tc->flag, tc->exp_pgs); + } + +end1: + SAFE_MUNLOCK(addr, tc->lock_pgs * pgsz + tc->offset); +end2: + SAFE_MUNMAP(addr, PAGES * pgsz); +} + +static void setup(void) +{ + pgsz = getpagesize(); + vec = SAFE_MALLOC((PAGES * pgsz + pgsz - 1) / pgsz); +} + +static void cleanup(void) +{ + if (vec) + free(vec); +} + +static struct tst_test test = { + .tcnt = ARRAY_SIZE(tcases), + .test = verify_mlock2, + .setup = setup, + .cleanup = cleanup, + .needs_root = 1, + .min_kver = "2.6.9", +}; -- 1.8.3.1 ^ permalink raw reply related [flat|nested] 16+ messages in thread
* [LTP] [PATCH v2 3/4] syscalls/mlock201: Add new testcase 2018-08-31 7:13 ` [LTP] [PATCH v2 " Xiao Yang @ 2018-08-30 12:41 ` Jan Stancek 2018-08-31 7:13 ` [LTP] [PATCH v2 4/4] syscalls/mlock202: " Xiao Yang 1 sibling, 0 replies; 16+ messages in thread From: Jan Stancek @ 2018-08-30 12:41 UTC (permalink / raw) To: ltp ----- Original Message ----- > 1) Add SAFE_MINCORE() macro > 2) Check the basic functionality of mlock2(2). > > Note: > 1) We use tst_syscall() to check if mlock2() is supported. > 2) since kernel v2.6.9, the limits and permissions of mlock2() > changed, so we just check mlock2() since the version. > > Signed-off-by: Xiao Yang <yangx.jy@cn.fujitsu.com> Looks good to me, some nits below (no need to re-post) > +#include <errno.h> > +#include <unistd.h> > +#include <string.h> > +#include <stdlib.h> > +#include <sys/mman.h> > +#include <linux/mman.h> > + > +#include "tst_test.h" > +#include "lapi/syscalls.h" > +#include "lapi/mlock2.h" > + > +#define PAGES 8 > +#define HPAGES (PAGES / 2) > + > +static size_t pgsz; > +static unsigned char *vec; > + > +static struct tcase { > + size_t pop_pgs; > + size_t lock_pgs; > + size_t offset; > + size_t exp_vmlcks; > + size_t exp_pgs; Some of these could be more descriptive or have comments. <snip> > +static void setup(void) > +{ > + pgsz = getpagesize(); > + vec = SAFE_MALLOC((PAGES * pgsz + pgsz - 1) / pgsz); This value is known at compile time, so malloc isn't needed. > +} > + > +static void cleanup(void) > +{ > + if (vec) > + free(vec); > +} > + > +static struct tst_test test = { > + .tcnt = ARRAY_SIZE(tcases), > + .test = verify_mlock2, > + .setup = setup, > + .cleanup = cleanup, > + .needs_root = 1, > + .min_kver = "2.6.9", Ok, "syscall can be backported to older kernels" is valid argument. Thanks, Jan ^ permalink raw reply [flat|nested] 16+ messages in thread
* [LTP] [PATCH v2 4/4] syscalls/mlock202: Add new testcase 2018-08-31 7:13 ` [LTP] [PATCH v2 " Xiao Yang 2018-08-30 12:41 ` Jan Stancek @ 2018-08-31 7:13 ` Xiao Yang 2018-08-30 12:43 ` Jan Stancek 1 sibling, 1 reply; 16+ messages in thread From: Xiao Yang @ 2018-08-31 7:13 UTC (permalink / raw) To: ltp Check various errnos for mlock2(2) since kernel v2.6.9. Note: 1) We use tst_syscall() to check if mlock2() is supported. 2) since kernel v2.6.9, the limits and permissions of mlock2() changed, so we just check mlock2() since the version. Signed-off-by: Xiao Yang <yangx.jy@cn.fujitsu.com> --- runtest/ltplite | 1 + runtest/stress.part3 | 1 + runtest/syscalls | 1 + testcases/kernel/syscalls/mlock2/.gitignore | 1 + testcases/kernel/syscalls/mlock2/mlock202.c | 113 ++++++++++++++++++++++++++++ 5 files changed, 117 insertions(+) create mode 100644 testcases/kernel/syscalls/mlock2/mlock202.c diff --git a/runtest/ltplite b/runtest/ltplite index 270c649..7ebe632 100644 --- a/runtest/ltplite +++ b/runtest/ltplite @@ -444,6 +444,7 @@ mlock01 mlock01 mlock02 mlock02 mlock201 mlock201 +mlock202 mlock202 qmm01 mmap001 -m 1 mmap01 mmap01 diff --git a/runtest/stress.part3 b/runtest/stress.part3 index 6850572..0804172 100644 --- a/runtest/stress.part3 +++ b/runtest/stress.part3 @@ -370,6 +370,7 @@ mlock01 mlock01 mlock02 mlock02 mlock201 mlock201 +mlock202 mlock202 qmm01 mmap001 -m 1 mmap01 mmap01 diff --git a/runtest/syscalls b/runtest/syscalls index 5d84d48..2615254 100644 --- a/runtest/syscalls +++ b/runtest/syscalls @@ -634,6 +634,7 @@ mlock03 mlock03 -i 20 mlock04 mlock04 mlock201 mlock201 +mlock202 mlock202 qmm01 mmap001 -m 1 mmap01 mmap01 diff --git a/testcases/kernel/syscalls/mlock2/.gitignore b/testcases/kernel/syscalls/mlock2/.gitignore index 431eff8..8daf0f9 100644 --- a/testcases/kernel/syscalls/mlock2/.gitignore +++ b/testcases/kernel/syscalls/mlock2/.gitignore @@ -1 +1,2 @@ /mlock201 +/mlock202 diff --git a/testcases/kernel/syscalls/mlock2/mlock202.c b/testcases/kernel/syscalls/mlock2/mlock202.c new file mode 100644 index 0000000..630da65 --- /dev/null +++ b/testcases/kernel/syscalls/mlock2/mlock202.c @@ -0,0 +1,113 @@ +// SPDX-License-Identifier: GPL-2.0-or-later +/* + * Copyright (c) 2018 FUJITSU LIMITED. All rights reserved. + * Author: Xiao Yang <yangx.jy@cn.fujitsu.com> + */ +/* + * Description: + * Check various errnos for mlock2(2) since kernel v2.6.9: + * 1) mlock2() fails and returns EINVAL if unknown flag is specified. + * 2) mlock2() fails and returns ENOMEM if the caller is not + * privileged(CAP_IPC_LOCK) and tries to lock more memory than the + * RLIMIT_MEMLOCK limit. + * 3) mlock2() fails and returns EPERM if the caller is not + * privileged(CAP_IPC_LOCK) and its RLIMIT_MEMLOCK limit is 0. + * 4) mlock2() fails and returns ENOMEM if some of the specified address + * range does not correspond to mapped pages in the address space + * of the caller. + */ +#include <errno.h> +#include <unistd.h> +#include <sys/mman.h> +#include <sys/time.h> +#include <sys/resource.h> +#include <pwd.h> + +#include "tst_test.h" +#include "lapi/syscalls.h" + +#define PAGES 8 + +static size_t pgsz; +static size_t max_sz1, max_sz2; +static char *addr, *unmapped_addr; +static struct passwd *nobody; + +static struct tcase { + char **taddr; + int flag; + size_t *max_size; + /* 1: nobody 0: root */ + int user; + int exp_err; +} tcases[] = { + {&addr, -1, NULL, 0, EINVAL}, + {&addr, 0, &max_sz1, 1, ENOMEM}, + {&addr, 0, &max_sz2, 1, EPERM}, + {&unmapped_addr, 0, NULL, 0, ENOMEM}, +}; + +static void verify_mlock2(unsigned int n) +{ + struct tcase *tc = &tcases[n]; + struct rlimit orig_limit, new_limit; + + if (tc->user) { + SAFE_GETRLIMIT(RLIMIT_MEMLOCK, &orig_limit); + new_limit.rlim_cur = *tc->max_size; + new_limit.rlim_max = *tc->max_size; + SAFE_SETRLIMIT(RLIMIT_MEMLOCK, &new_limit); + SAFE_SETEUID(nobody->pw_uid); + } + + TEST(tst_syscall(__NR_mlock2, *tc->taddr, pgsz, tc->flag)); + if (TST_RET != -1) { + tst_res(TFAIL, "mlock2() succeeded"); + SAFE_MUNLOCK(*tc->taddr, pgsz); + goto end; + } + + if (TST_ERR != tc->exp_err) { + tst_res(TFAIL | TTERRNO, + "mlock2() failed unexpectedly, expected %s", + tst_strerrno(tc->exp_err)); + } else { + tst_res(TPASS | TTERRNO, "mlock2() failed as expected"); + } + +end: + if (tc->user) { + SAFE_SETEUID(0); + SAFE_SETRLIMIT(RLIMIT_MEMLOCK, &orig_limit); + } +} + +static void setup(void) +{ + pgsz = getpagesize(); + nobody = SAFE_GETPWNAM("nobody"); + + addr = SAFE_MMAP(NULL, pgsz, PROT_WRITE, + MAP_PRIVATE | MAP_ANONYMOUS, 0, 0); + unmapped_addr = SAFE_MMAP(NULL, pgsz * PAGES, PROT_READ, + MAP_PRIVATE | MAP_ANONYMOUS, 0, 0); + SAFE_MUNMAP(unmapped_addr, pgsz * PAGES); + unmapped_addr = unmapped_addr + pgsz * PAGES / 2; + + max_sz1 = pgsz - 1; +} + +static void cleanup(void) +{ + if (addr) + SAFE_MUNMAP(addr, pgsz); +} + +static struct tst_test test = { + .tcnt = ARRAY_SIZE(tcases), + .test = verify_mlock2, + .setup = setup, + .cleanup = cleanup, + .needs_root = 1, + .min_kver = "2.6.9", +}; -- 1.8.3.1 ^ permalink raw reply related [flat|nested] 16+ messages in thread
* [LTP] [PATCH v2 4/4] syscalls/mlock202: Add new testcase 2018-08-31 7:13 ` [LTP] [PATCH v2 4/4] syscalls/mlock202: " Xiao Yang @ 2018-08-30 12:43 ` Jan Stancek 2018-09-04 8:10 ` Jan Stancek 0 siblings, 1 reply; 16+ messages in thread From: Jan Stancek @ 2018-08-30 12:43 UTC (permalink / raw) To: ltp ----- Original Message ----- > Check various errnos for mlock2(2) since kernel v2.6.9. > > Note: > 1) We use tst_syscall() to check if mlock2() is supported. > 2) since kernel v2.6.9, the limits and permissions of mlock2() > changed, so we just check mlock2() since the version. > > Signed-off-by: Xiao Yang <yangx.jy@cn.fujitsu.com> Ack ^ permalink raw reply [flat|nested] 16+ messages in thread
* [LTP] [PATCH v2 4/4] syscalls/mlock202: Add new testcase 2018-08-30 12:43 ` Jan Stancek @ 2018-09-04 8:10 ` Jan Stancek 0 siblings, 0 replies; 16+ messages in thread From: Jan Stancek @ 2018-09-04 8:10 UTC (permalink / raw) To: ltp ----- Original Message ----- > > > ----- Original Message ----- > > Check various errnos for mlock2(2) since kernel v2.6.9. > > > > Note: > > 1) We use tst_syscall() to check if mlock2() is supported. > > 2) since kernel v2.6.9, the limits and permissions of mlock2() > > changed, so we just check mlock2() since the version. > > > > Signed-off-by: Xiao Yang <yangx.jy@cn.fujitsu.com> > > Ack Series pushed with few tweaks to 3/4: - couple variables renamed - added comments - vec allocated statically, dropped cleanup() Thanks, Jan ^ permalink raw reply [flat|nested] 16+ messages in thread
* [LTP] [PATCH 4/4] syscalls/mlock202: Add new testcase 2018-08-21 9:38 [LTP] [PATCH 1/4] lapi/syscalls: Add syscall numbers for mlock2 Xiao Yang 2018-08-21 9:38 ` [LTP] [PATCH 2/4] safe_macros: Add SAFE_MLOCK() and SAFE_MUNLOCK() macros Xiao Yang 2018-08-21 9:38 ` [LTP] [PATCH 3/4] syscalls/mlock201: Add new testcase Xiao Yang @ 2018-08-21 9:38 ` Xiao Yang 2018-08-29 12:44 ` Jan Stancek 2018-08-29 12:11 ` [LTP] [PATCH 1/4] lapi/syscalls: Add syscall numbers for mlock2 Jan Stancek 3 siblings, 1 reply; 16+ messages in thread From: Xiao Yang @ 2018-08-21 9:38 UTC (permalink / raw) To: ltp Check various errnos for mlock2(2) since kernel v2.6.9. Signed-off-by: Xiao Yang <yangx.jy@cn.fujitsu.com> --- runtest/ltplite | 1 + runtest/stress.part3 | 1 + runtest/syscalls | 1 + testcases/kernel/syscalls/mlock2/.gitignore | 1 + testcases/kernel/syscalls/mlock2/mlock202.c | 110 ++++++++++++++++++++++++++++ 5 files changed, 114 insertions(+) create mode 100644 testcases/kernel/syscalls/mlock2/mlock202.c diff --git a/runtest/ltplite b/runtest/ltplite index 270c649..7ebe632 100644 --- a/runtest/ltplite +++ b/runtest/ltplite @@ -444,6 +444,7 @@ mlock01 mlock01 mlock02 mlock02 mlock201 mlock201 +mlock202 mlock202 qmm01 mmap001 -m 1 mmap01 mmap01 diff --git a/runtest/stress.part3 b/runtest/stress.part3 index 6850572..0804172 100644 --- a/runtest/stress.part3 +++ b/runtest/stress.part3 @@ -370,6 +370,7 @@ mlock01 mlock01 mlock02 mlock02 mlock201 mlock201 +mlock202 mlock202 qmm01 mmap001 -m 1 mmap01 mmap01 diff --git a/runtest/syscalls b/runtest/syscalls index 5d84d48..2615254 100644 --- a/runtest/syscalls +++ b/runtest/syscalls @@ -634,6 +634,7 @@ mlock03 mlock03 -i 20 mlock04 mlock04 mlock201 mlock201 +mlock202 mlock202 qmm01 mmap001 -m 1 mmap01 mmap01 diff --git a/testcases/kernel/syscalls/mlock2/.gitignore b/testcases/kernel/syscalls/mlock2/.gitignore index 431eff8..8daf0f9 100644 --- a/testcases/kernel/syscalls/mlock2/.gitignore +++ b/testcases/kernel/syscalls/mlock2/.gitignore @@ -1 +1,2 @@ /mlock201 +/mlock202 diff --git a/testcases/kernel/syscalls/mlock2/mlock202.c b/testcases/kernel/syscalls/mlock2/mlock202.c new file mode 100644 index 0000000..61dd29d --- /dev/null +++ b/testcases/kernel/syscalls/mlock2/mlock202.c @@ -0,0 +1,110 @@ +// SPDX-License-Identifier: GPL-2.0-or-later +/* + * Copyright (c) 2018 FUJITSU LIMITED. All rights reserved. + * Author: Xiao Yang <yangx.jy@cn.fujitsu.com> + */ +/* + * Description: + * Check various errnos for mlock2(2) since kernel v2.6.9: + * 1) mlock(2) fails and returns EINVAL if unknown flag is specified. + * 2) mlock(2) fails and returns ENOMEM if the caller is not + * privileged(CAP_IPC_LOCK) and tries to lock more memory than the + * RLIMIT_MEMLOCK limit. + * 3) mlock(2) fails and returns EPERM if the caller is not + * privileged(CAP_IPC_LOCK) and its RLIMIT_MEMLOCK limit is 0. + * 4) mlock(2) fails and returns ENOMEM if some of the specified address + * range does not correspond to mapped pages in the address space + * of the caller. + */ +#include <errno.h> +#include <unistd.h> +#include <sys/mman.h> +#include <sys/time.h> +#include <sys/resource.h> +#include <pwd.h> + +#include "tst_test.h" +#include "lapi/syscalls.h" + +static unsigned long pgsz; +static unsigned long max_sz1, max_sz2; +static char *addr, *unmapped_addr; +static struct passwd *nobody; + +static struct tcase { + char **taddr; + int flag; + unsigned long *max_size; + /* 1: nobody expected 0: root expected */ + int exp_user; + int exp_err; +} tcases[] = { + {&addr, -1, NULL, 0, EINVAL}, + {&addr, 0, &max_sz1, 1, ENOMEM}, + {&addr, 0, &max_sz2, 1, EPERM}, + {&unmapped_addr, 0, NULL, 0, ENOMEM}, +}; + +static void verify_mlock2(unsigned int n) +{ + struct tcase *tc = &tcases[n]; + struct rlimit orig_limit, new_limit; + + if (tc->exp_user) { + SAFE_GETRLIMIT(RLIMIT_MEMLOCK, &orig_limit); + new_limit.rlim_cur = *tc->max_size; + new_limit.rlim_max = *tc->max_size; + SAFE_SETRLIMIT(RLIMIT_MEMLOCK, &new_limit); + SAFE_SETEUID(nobody->pw_uid); + } + + TEST(tst_syscall(__NR_mlock2, *tc->taddr, pgsz, tc->flag)); + if (TST_RET != -1) { + tst_res(TFAIL, "mlock2() succeeded"); + SAFE_MUNLOCK(*tc->taddr, pgsz); + goto end; + } + + if (TST_ERR != tc->exp_err) { + tst_res(TFAIL | TTERRNO, + "mlock2() failed unexpectedly, expected %s", + tst_strerrno(tc->exp_err)); + } else { + tst_res(TPASS | TTERRNO, "mlock2() failed as expected"); + } + +end: + if (tc->exp_user) { + SAFE_SETEUID(0); + SAFE_SETRLIMIT(RLIMIT_MEMLOCK, &orig_limit); + } +} + +static void setup(void) +{ + pgsz = getpagesize(); + nobody = SAFE_GETPWNAM("nobody"); + + addr = SAFE_MMAP(NULL, pgsz, PROT_WRITE, + MAP_PRIVATE | MAP_ANONYMOUS, 0, 0); + unmapped_addr = SAFE_MMAP(NULL, pgsz, PROT_READ, + MAP_PRIVATE | MAP_ANONYMOUS, 0, 0); + SAFE_MUNMAP(unmapped_addr, pgsz); + + max_sz1 = pgsz - 1; +} + +static void cleanup(void) +{ + if (addr) + SAFE_MUNMAP(addr, pgsz); +} + +static struct tst_test test = { + .tcnt = ARRAY_SIZE(tcases), + .test = verify_mlock2, + .setup = setup, + .cleanup = cleanup, + .needs_root = 1, + .min_kver = "2.6.9", +}; -- 1.8.3.1 ^ permalink raw reply related [flat|nested] 16+ messages in thread
* [LTP] [PATCH 4/4] syscalls/mlock202: Add new testcase 2018-08-21 9:38 ` [LTP] [PATCH " Xiao Yang @ 2018-08-29 12:44 ` Jan Stancek 2018-08-30 7:41 ` Xiao Yang 0 siblings, 1 reply; 16+ messages in thread From: Jan Stancek @ 2018-08-29 12:44 UTC (permalink / raw) To: ltp ----- Original Message ----- > Check various errnos for mlock2(2) since kernel v2.6.9. mlock2() is available since Linux 4.4. > +static struct tcase { > + char **taddr; > + int flag; > + unsigned long *max_size; > + /* 1: nobody expected 0: root expected */ > + int exp_user; I'd drop the "expected" prefix here, this isn't something syscall provides as "output". > + int exp_err; > +} tcases[] = { > + {&addr, -1, NULL, 0, EINVAL}, > + {&addr, 0, &max_sz1, 1, ENOMEM}, > + {&addr, 0, &max_sz2, 1, EPERM}, > + {&unmapped_addr, 0, NULL, 0, ENOMEM}, > +}; > + .. > +static void setup(void) > +{ > + pgsz = getpagesize(); > + nobody = SAFE_GETPWNAM("nobody"); > + > + addr = SAFE_MMAP(NULL, pgsz, PROT_WRITE, > + MAP_PRIVATE | MAP_ANONYMOUS, 0, 0); > + unmapped_addr = SAFE_MMAP(NULL, pgsz, PROT_READ, > + MAP_PRIVATE | MAP_ANONYMOUS, 0, 0); > + SAFE_MUNMAP(unmapped_addr, pgsz); I suggest larger map here (couple M) and pick address in middle. Single page can easily be mapped by glibc for some buffer. > + > + max_sz1 = pgsz - 1; > +} > + Regards, Jan ^ permalink raw reply [flat|nested] 16+ messages in thread
* [LTP] [PATCH 4/4] syscalls/mlock202: Add new testcase 2018-08-29 12:44 ` Jan Stancek @ 2018-08-30 7:41 ` Xiao Yang 0 siblings, 0 replies; 16+ messages in thread From: Xiao Yang @ 2018-08-30 7:41 UTC (permalink / raw) To: ltp On 2018/08/29 20:44, Jan Stancek wrote: > ----- Original Message ----- >> Check various errnos for mlock2(2) since kernel v2.6.9. > mlock2() is available since Linux 4.4. > >> +static struct tcase { >> + char **taddr; >> + int flag; >> + unsigned long *max_size; >> + /* 1: nobody expected 0: root expected */ >> + int exp_user; > I'd drop the "expected" prefix here, this isn't something syscall provides as "output". Hi Jan, OK, I will drop it. >> + int exp_err; >> +} tcases[] = { >> + {&addr, -1, NULL, 0, EINVAL}, >> + {&addr, 0,&max_sz1, 1, ENOMEM}, >> + {&addr, 0,&max_sz2, 1, EPERM}, >> + {&unmapped_addr, 0, NULL, 0, ENOMEM}, >> +}; >> + > .. >> +static void setup(void) >> +{ >> + pgsz = getpagesize(); >> + nobody = SAFE_GETPWNAM("nobody"); >> + >> + addr = SAFE_MMAP(NULL, pgsz, PROT_WRITE, >> + MAP_PRIVATE | MAP_ANONYMOUS, 0, 0); >> + unmapped_addr = SAFE_MMAP(NULL, pgsz, PROT_READ, >> + MAP_PRIVATE | MAP_ANONYMOUS, 0, 0); >> + SAFE_MUNMAP(unmapped_addr, pgsz); > I suggest larger map here (couple M) and pick address in middle. > Single page can easily be mapped by glibc for some buffer. OK, i will map a larger one and pick address in middle. Thanks, Xiao Yang >> + >> + max_sz1 = pgsz - 1; >> +} >> + > Regards, > Jan > > > ^ permalink raw reply [flat|nested] 16+ messages in thread
* [LTP] [PATCH 1/4] lapi/syscalls: Add syscall numbers for mlock2 2018-08-21 9:38 [LTP] [PATCH 1/4] lapi/syscalls: Add syscall numbers for mlock2 Xiao Yang ` (2 preceding siblings ...) 2018-08-21 9:38 ` [LTP] [PATCH " Xiao Yang @ 2018-08-29 12:11 ` Jan Stancek 3 siblings, 0 replies; 16+ messages in thread From: Jan Stancek @ 2018-08-29 12:11 UTC (permalink / raw) To: ltp ----- Original Message ----- > Signed-off-by: Xiao Yang <yangx.jy@cn.fujitsu.com> > --- > include/lapi/syscalls/aarch64.in | 1 + > include/lapi/syscalls/arm.in | 1 + > include/lapi/syscalls/hppa.in | 1 + > include/lapi/syscalls/i386.in | 1 + > include/lapi/syscalls/ia64.in | 1 + > include/lapi/syscalls/powerpc.in | 1 + > include/lapi/syscalls/powerpc64.in | 1 + > include/lapi/syscalls/s390.in | 1 + > include/lapi/syscalls/s390x.in | 1 + > include/lapi/syscalls/sh.in | 1 + > include/lapi/syscalls/sparc.in | 1 + > include/lapi/syscalls/sparc64.in | 1 + > include/lapi/syscalls/x86_64.in | 1 + > 13 files changed, 13 insertions(+) ACK ^ permalink raw reply [flat|nested] 16+ messages in thread
end of thread, other threads:[~2018-09-04 8:10 UTC | newest] Thread overview: 16+ messages (download: mbox.gz follow: Atom feed -- links below jump to the message on this page -- 2018-08-21 9:38 [LTP] [PATCH 1/4] lapi/syscalls: Add syscall numbers for mlock2 Xiao Yang 2018-08-21 9:38 ` [LTP] [PATCH 2/4] safe_macros: Add SAFE_MLOCK() and SAFE_MUNLOCK() macros Xiao Yang 2018-08-29 12:11 ` Jan Stancek 2018-08-21 9:38 ` [LTP] [PATCH 3/4] syscalls/mlock201: Add new testcase Xiao Yang 2018-08-29 12:19 ` Jan Stancek 2018-08-29 12:42 ` Jan Stancek 2018-08-30 1:17 ` Xiao Yang 2018-08-31 7:13 ` [LTP] [PATCH v2 " Xiao Yang 2018-08-30 12:41 ` Jan Stancek 2018-08-31 7:13 ` [LTP] [PATCH v2 4/4] syscalls/mlock202: " Xiao Yang 2018-08-30 12:43 ` Jan Stancek 2018-09-04 8:10 ` Jan Stancek 2018-08-21 9:38 ` [LTP] [PATCH " Xiao Yang 2018-08-29 12:44 ` Jan Stancek 2018-08-30 7:41 ` Xiao Yang 2018-08-29 12:11 ` [LTP] [PATCH 1/4] lapi/syscalls: Add syscall numbers for mlock2 Jan Stancek
This is a public inbox, see mirroring instructions for how to clone and mirror all data and code used for this inbox