public inbox for ltp@lists.linux.it
 help / color / mirror / Atom feed
* [LTP] [PATCH] syscalls/mlock05: add mlock test for locking and pre-faulting of memory
@ 2024-04-26 13:48 Filippo Storniolo
  2024-04-29 10:00 ` Cyril Hrubis
  0 siblings, 1 reply; 2+ messages in thread
From: Filippo Storniolo @ 2024-04-26 13:48 UTC (permalink / raw)
  To: ltp; +Cc: Filippo Storniolo

check VmRSS and VmLck variables from /proc/$pid/status.
VmRSS size should at least as big as the memory allocation.
VmLck size should be equal to the size of the memory allocation.

Co-developed-by: Dennis Brendel <dbrendel@redhat.com>
Signed-off-by: Filippo Storniolo <fstornio@redhat.com>
---
 runtest/syscalls                           |  1 +
 testcases/kernel/syscalls/mlock/.gitignore |  1 +
 testcases/kernel/syscalls/mlock/mlock05.c  | 69 ++++++++++++++++++++++
 3 files changed, 71 insertions(+)
 create mode 100644 testcases/kernel/syscalls/mlock/mlock05.c

diff --git a/runtest/syscalls b/runtest/syscalls
index 252123d8b..05a52fc8f 100644
--- a/runtest/syscalls
+++ b/runtest/syscalls
@@ -781,6 +781,7 @@ mlock01 mlock01
 mlock02 mlock02
 mlock03 mlock03 -i 20
 mlock04 mlock04
+mlock05 mlock05
 
 mlock201 mlock201
 mlock202 mlock202
diff --git a/testcases/kernel/syscalls/mlock/.gitignore b/testcases/kernel/syscalls/mlock/.gitignore
index 306574bbc..1872229b8 100644
--- a/testcases/kernel/syscalls/mlock/.gitignore
+++ b/testcases/kernel/syscalls/mlock/.gitignore
@@ -2,3 +2,4 @@
 /mlock02
 /mlock03
 /mlock04
+/mlock05
diff --git a/testcases/kernel/syscalls/mlock/mlock05.c b/testcases/kernel/syscalls/mlock/mlock05.c
new file mode 100644
index 000000000..e67cd1e03
--- /dev/null
+++ b/testcases/kernel/syscalls/mlock/mlock05.c
@@ -0,0 +1,69 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * Copyright Red Hat
+ * Author: Filippo Storniolo <fstornio@redhat.com>
+ */
+
+/*\
+ * [Description]
+ *
+ * Verify mlock() causes pre-faulting of PTEs and prevent memory to be swapped out.
+ *
+ * Checks the variables VmRSS and VmLck in /proc/$pid/status after the
+ * mlock syscall:
+ * - VmRSS size should be at least as big as the memory allocation
+ * - VmLck size should be equal to the size of the memory allocation
+ */
+
+#include "tst_test.h"
+
+#define MMAPLEN			(1UL<<20)
+
+static void verify_mlock(void)
+{
+	unsigned long VmRSS_before;
+	unsigned long VmRSS_after;
+	unsigned long VmLck_before;
+	unsigned long VmLck_after;
+	unsigned long VmRSS;
+	unsigned long VmLck;
+	char *buf;
+
+	buf = SAFE_MMAP(NULL, MMAPLEN, PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
+
+	SAFE_FILE_LINES_SCANF("/proc/self/status", "VmRSS: %lu", &VmRSS_before);
+	SAFE_FILE_LINES_SCANF("/proc/self/status", "VmLck: %lu", &VmLck_before);
+
+	SAFE_MLOCK(buf, MMAPLEN);
+
+	SAFE_FILE_LINES_SCANF("/proc/self/status", "VmRSS: %lu", &VmRSS_after);
+	SAFE_FILE_LINES_SCANF("/proc/self/status", "VmLck: %lu", &VmLck_after);
+
+	VmRSS = VmRSS_after - VmRSS_before;
+	VmLck = VmLck_after - VmLck_before;
+
+	// Convertion from KiB to B
+	VmRSS *= 1024;
+	VmLck *= 1024;
+
+	if (VmRSS >= MMAPLEN) {
+		tst_res(TPASS, "Pre-allocation functionality of mlock() successful");
+	} else {
+		tst_brk(TBROK, "Expected pre-allocation of %lu bytes, "
+					"get %lu pre-allocated", MMAPLEN, VmRSS);
+	}
+
+	if (VmLck == MMAPLEN) {
+		tst_res(TPASS, "Locking functionality of mlock() successful");
+	} else {
+		tst_brk(TBROK, "Expected locking of %lu bytes, "
+					"get %lu locked", MMAPLEN, VmLck);
+	}
+
+	SAFE_MUNLOCK(buf, MMAPLEN);
+	SAFE_MUNMAP(buf, MMAPLEN);
+}
+
+static struct tst_test test = {
+	.test_all = verify_mlock,
+};
-- 
2.44.0


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

^ permalink raw reply related	[flat|nested] 2+ messages in thread

* Re: [LTP] [PATCH] syscalls/mlock05: add mlock test for locking and pre-faulting of memory
  2024-04-26 13:48 [LTP] [PATCH] syscalls/mlock05: add mlock test for locking and pre-faulting of memory Filippo Storniolo
@ 2024-04-29 10:00 ` Cyril Hrubis
  0 siblings, 0 replies; 2+ messages in thread
From: Cyril Hrubis @ 2024-04-29 10:00 UTC (permalink / raw)
  To: Filippo Storniolo; +Cc: ltp

Hi!
> +// SPDX-License-Identifier: GPL-2.0-or-later
> +/*
> + * Copyright Red Hat
> + * Author: Filippo Storniolo <fstornio@redhat.com>
> + */
> +
> +/*\
> + * [Description]
> + *
> + * Verify mlock() causes pre-faulting of PTEs and prevent memory to be swapped out.
> + *
> + * Checks the variables VmRSS and VmLck in /proc/$pid/status after the
> + * mlock syscall:
> + * - VmRSS size should be at least as big as the memory allocation
> + * - VmLck size should be equal to the size of the memory allocation
> + */
> +
> +#include "tst_test.h"
> +
> +#define MMAPLEN			(1UL<<20)
> +
> +static void verify_mlock(void)
> +{
> +	unsigned long VmRSS_before;
> +	unsigned long VmRSS_after;
> +	unsigned long VmLck_before;
> +	unsigned long VmLck_after;
> +	unsigned long VmRSS;
> +	unsigned long VmLck;
> +	char *buf;
> +
> +	buf = SAFE_MMAP(NULL, MMAPLEN, PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
> +
> +	SAFE_FILE_LINES_SCANF("/proc/self/status", "VmRSS: %lu", &VmRSS_before);
> +	SAFE_FILE_LINES_SCANF("/proc/self/status", "VmLck: %lu", &VmLck_before);
> +
> +	SAFE_MLOCK(buf, MMAPLEN);
> +
> +	SAFE_FILE_LINES_SCANF("/proc/self/status", "VmRSS: %lu", &VmRSS_after);
> +	SAFE_FILE_LINES_SCANF("/proc/self/status", "VmLck: %lu", &VmLck_after);
> +
> +	VmRSS = VmRSS_after - VmRSS_before;
> +	VmLck = VmLck_after - VmLck_before;
> +
> +	// Convertion from KiB to B
> +	VmRSS *= 1024;
> +	VmLck *= 1024;
> +
> +	if (VmRSS >= MMAPLEN) {
> +		tst_res(TPASS, "Pre-allocation functionality of mlock() successful");
> +	} else {
> +		tst_brk(TBROK, "Expected pre-allocation of %lu bytes, "
> +					"get %lu pre-allocated", MMAPLEN, VmRSS);

This is TFAIL. TBROK is used when a test is broken because a test setup
has failed, not when test a assertion wasn't true.

Also simplify this to TST_EXP_EXPR(VmRSS >= MMAPLEN);

> +	}
> +
> +	if (VmLck == MMAPLEN) {
> +		tst_res(TPASS, "Locking functionality of mlock() successful");
> +	} else {
> +		tst_brk(TBROK, "Expected locking of %lu bytes, "
> +					"get %lu locked", MMAPLEN, VmLck);
> +	}

This could be simplified to a single TST_EXP_EQ_LU(VmLCK, MMAPLEN);

> +	SAFE_MUNLOCK(buf, MMAPLEN);
> +	SAFE_MUNMAP(buf, MMAPLEN);
> +}
> +
> +static struct tst_test test = {
> +	.test_all = verify_mlock,
> +};

The rest looks good.

-- 
Cyril Hrubis
chrubis@suse.cz

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

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2024-04-29 10:01 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2024-04-26 13:48 [LTP] [PATCH] syscalls/mlock05: add mlock test for locking and pre-faulting of memory Filippo Storniolo
2024-04-29 10:00 ` Cyril Hrubis

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox