Skip to contentSkip to conversation

getrusage03: mlock the memory area in consume_mb

PATCH In review Li Wang 2 participants 7 messages Feb 8, 2022 – Feb 21, 2022

lore
Share & tools
lore

Li Wang

author
#0 lore raw

PATCH 1/1 getrusage03: mlock the memory area in consume_mb

Seems it trying to test without swap happen to guarantee less disturbing for the ’->ru_maxrss‘ counting.

Therefore add mlock() to prevent that memory allocated by consume_mb from being paged to the swap area.

---
 testcases/kernel/syscalls/getrusage/getrusage03.h | 1 +
 1 file changed, 1 insertion(+)
testcases/kernel/syscalls/getrusage/getrusage03.h +1 -0
index f1bbe9be5..d46fdff85 100644
@@ -18,6 +18,7 @@ static void consume_mb(int consume_nr)
 
 	size = consume_nr * 1024 * 1024;
 	ptr = SAFE_MALLOC(size);
+	mlock(ptr, size);
 	memset(ptr, 0, size);
 
 	SAFE_FILE_LINES_SCANF("/proc/self/status", "VmSwap: %lu", &vmswap_size);
Signature · 5 lines
2.31.1


-- 
Mailing list info: https://lists.linux.it/listinfo/ltp
author
#1 lore raw
Quoted 17 lines · Li Wang

Seems it trying to test without swap happen to guarantee less disturbing for the ’->ru_maxrss‘ counting.

Therefore add mlock() to prevent that memory allocated by consume_mb from being paged to the swap area.

---
 testcases/kernel/syscalls/getrusage/getrusage03.h | 1 +
 1 file changed, 1 insertion(+)
testcases/kernel/syscalls/getrusage/getrusage03.h +0 -0
index f1bbe9be5..d46fdff85 100644

testcases/kernel/syscalls/getrusage/getrusage03.h static void consume_mb(int consume_nr) · line 22

Quoted 5 lines
testcases/kernel/syscalls/getrusage/getrusage03.h +1 -0
@@ -18,6 +18,7 @@ static void consume_mb(int consume_nr)
 
        size = consume_nr * 1024 * 1024;
        ptr = SAFE_MALLOC(size);
+       mlock(ptr, size);
 

Hmm, seems using mlockall() will be better than mlock().

Because that locks all pages mapped into the address space of the calling process.
(includes code, data, stack segment, shared libraries, etc).

Quoted 11 lines
testcases/kernel/syscalls/getrusage/getrusage03.h +0 -0
        memset(ptr, 0, size);
 
        SAFE_FILE_LINES_SCANF("/proc/self/status", "VmSwap: %lu",

&vmswap_size);
--
2.31.1

--
Mailing list info: https://lists.linux.it/listinfo/ltp

Signature · 2 lines
Regards,
Li Wang
Signature · 2 lines
-- 
Mailing list info: https://lists.linux.it/listinfo/ltp
author
#2 lore raw

PATCH v2 1/1 getrusage03: mlock all address space for process calling consume_mb

In reply to Li Wang

It is trying to test without swap happen to guarantee less disturbing for the ’->ru_maxrss‘ counting.

Therefore add mlockall() to prevent that memory address space of the calling process from being paged to the swap area.

Due to that lock needs CAP_IPC_LOCK capability and child can not inherit memory locks from parent, here add the definition and put the mlockall() in consume_mb.

---
 include/lapi/capability.h                         | 4 ++++
 testcases/kernel/syscalls/getrusage/getrusage03.c | 4 ++++
 testcases/kernel/syscalls/getrusage/getrusage03.h | 2 ++
 3 files changed, 10 insertions(+)
include/lapi/capability.h +4 -0
index 8cabd0f28..17ec107b1 100644
@@ -24,6 +24,10 @@
 # define CAP_NET_RAW          13
 #endif
 
+#ifndef CAP_IPC_LOCK
+# define CAP_IPC_LOCK         14
+#endif
+
 #ifndef CAP_SYS_CHROOT
 # define CAP_SYS_CHROOT       18
 #endif
testcases/kernel/syscalls/getrusage/getrusage03.c +4 -0
index 201d258fa..43fab0605 100644
@@ -193,4 +193,8 @@ static struct tst_test test = {
 	.setup = setup,
 	.test = run,
 	.tcnt = ARRAY_SIZE(testfunc_list),
+	.caps = (struct tst_cap []) {
+		TST_CAP(TST_CAP_REQ, CAP_IPC_LOCK),
+		{}
+	},
 };
testcases/kernel/syscalls/getrusage/getrusage03.h +2 -0
index f1bbe9be5..b28b9d4c3 100644
@@ -16,6 +16,8 @@ static void consume_mb(int consume_nr)
 	size_t size;
 	unsigned long vmswap_size;
 
+	mlockall(MCL_CURRENT|MCL_FUTURE);
+
 	size = consume_nr * 1024 * 1024;
 	ptr = SAFE_MALLOC(size);
 	memset(ptr, 0, size);
Signature · 5 lines
2.31.1


-- 
Mailing list info: https://lists.linux.it/listinfo/ltp
#3 lore raw

Re: [LTP] [PATCH v2] getrusage03: mlock all address space for process calling consume_mb

Hi!

testcases/kernel/syscalls/getrusage/getrusage03.c static struct tst_test test = { · line 200

Quoted 11 lines
testcases/kernel/syscalls/getrusage/getrusage03.c +4 -0
@@ -193,4 +193,8 @@ static struct tst_test test = {
 	.setup = setup,
 	.test = run,
 	.tcnt = ARRAY_SIZE(testfunc_list),
+	.caps = (struct tst_cap []) {
+		TST_CAP(TST_CAP_REQ, CAP_IPC_LOCK),
+		{}
+	},
 };

I wonder if we should start to track the capabilities like this instead of the .needs_root approach. I guess that it may be useful to keep things granular in the future.

Signature · 5 lines
Cyril Hrubis
[email protected]

-- 
Mailing list info: https://lists.linux.it/listinfo/ltp
author
#4 lore raw

Re: [LTP] [PATCH v2] getrusage03: mlock all address space for process calling consume_mb

Quoted 17 lines · Cyril Hrubis

Hi!

testcases/kernel/syscalls/getrusage/getrusage03.c static struct tst_test test = { · line 200

Quoted 11 lines
testcases/kernel/syscalls/getrusage/getrusage03.c +4 -0
@@ -193,4 +193,8 @@ static struct tst_test test = {
      .setup = setup,
      .test = run,
      .tcnt = ARRAY_SIZE(testfunc_list),
+     .caps = (struct tst_cap []) {
+             TST_CAP(TST_CAP_REQ, CAP_IPC_LOCK),
+             {}
+     },
 };

I wonder if we should start to track the capabilities like this instead of the .needs_root approach. I guess that it may be useful to keep things granular in the future.

I think yes, Linux Capabilities makes it possible for specific privileges to the test process, and avoid that .needs_root limited many tests scenario to skip directly with non-root users. Especially beneficial for running LTP in the docker with only part of root privileges for reasons like strict security
verification.

Signature · 2 lines
Regards,
Li Wang
Signature · 2 lines
-- 
Mailing list info: https://lists.linux.it/listinfo/ltp
Reviewed-by
#5 lore raw

Re: [LTP] [PATCH v2] getrusage03: mlock all address space for process calling consume_mb

Hi!

Quoted 6 lines

I think yes, Linux Capabilities makes it possible for specific privileges to the test process, and avoid that .needs_root limited many tests scenario to skip directly with non-root users. Especially beneficial for running LTP in the docker with only part of root privileges for reasons like strict security
verification.

Looks good then:

Signature · 5 lines
Cyril Hrubis
[email protected]

-- 
Mailing list info: https://lists.linux.it/listinfo/ltp
author
#6 lore raw

Re: [LTP] [PATCH v2] getrusage03: mlock all address space for process calling consume_mb

Pushed, thanks.

Signature · 2 lines
Regards,
Li Wang
Signature · 2 lines
-- 
Mailing list info: https://lists.linux.it/listinfo/ltp