All the mail mirrored from lore.kernel.org
 help / color / mirror / Atom feed
From: Filippo Storniolo <fstornio@redhat.com>
To: ltp@lists.linux.it
Cc: Filippo Storniolo <fstornio@redhat.com>
Subject: [LTP] [PATCH] syscalls/mlock05: add mlock test for locking and pre-faulting of memory
Date: Fri, 26 Apr 2024 15:48:02 +0200	[thread overview]
Message-ID: <20240426134802.2607150-1-fstornio@redhat.com> (raw)

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

             reply	other threads:[~2024-04-29  9:52 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-04-26 13:48 Filippo Storniolo [this message]
2024-04-29 10:00 ` [LTP] [PATCH] syscalls/mlock05: add mlock test for locking and pre-faulting of memory Cyril Hrubis

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20240426134802.2607150-1-fstornio@redhat.com \
    --to=fstornio@redhat.com \
    --cc=ltp@lists.linux.it \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.