diff mbox series

syscalls/mlock05: add mlock test for locking and pre-faulting of memory

Message ID 20240426134802.2607150-1-fstornio@redhat.com
State Changes Requested
Headers show
Series syscalls/mlock05: add mlock test for locking and pre-faulting of memory | expand

Commit Message

Filippo Storniolo April 26, 2024, 1:48 p.m. UTC
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

Comments

Cyril Hrubis April 29, 2024, 10 a.m. UTC | #1
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.
diff mbox series

Patch

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,
+};