From: Avinesh Kumar <akumar@suse.de>
To: ltp@lists.linux.it
Subject: [LTP] [PATCH v2] syscalls/mmap14: Rewrite test using new LTP API
Date: Tue, 5 Sep 2023 18:58:45 +0530 [thread overview]
Message-ID: <20230905133118.23912-1-akumar@suse.de> (raw)
In-Reply-To: <20230825063932.30875-4-akumar@suse.de>
Signed-off-by: Avinesh Kumar <akumar@suse.de>
---
Changes in v2:
- Check RLIMIT_MEMLOCK before trying to map locked pages
testcases/kernel/syscalls/mmap/mmap14.c | 138 ++++++++----------------
1 file changed, 46 insertions(+), 92 deletions(-)
diff --git a/testcases/kernel/syscalls/mmap/mmap14.c b/testcases/kernel/syscalls/mmap/mmap14.c
index 31632601b..fba07ef3c 100644
--- a/testcases/kernel/syscalls/mmap/mmap14.c
+++ b/testcases/kernel/syscalls/mmap/mmap14.c
@@ -1,124 +1,78 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
/*
* Copyright (c) 2013 FNST, DAN LI <li.dan@cn.fujitsu.com>
- *
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or
- * (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
- * the GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+ * Copyright (c) 2023 SUSE LLC Avinesh Kumar <avinesh.kumar@suse.com>
*/
-/*
- * Test Description:
- * Verify MAP_LOCKED works fine.
- * "Lock the pages of the mapped region into memory in the manner of mlock(2)."
+/*\
+ * [Description]
*
- * Expected Result:
- * mmap() should succeed returning the address of the mapped region,
- * and this region should be locked into memory.
+ * Verify that, mmap() call with MAP_LOCKED flag successfully locks
+ * the mapped pages into memory.
*/
-#include <stdio.h>
-#include <sys/mman.h>
-#include "test.h"
+#include <stdio.h>
+#include "tst_test.h"
-#define TEMPFILE "mmapfile"
-#define MMAPSIZE (1UL<<20)
+#define MMAPSIZE (1UL<<21)
#define LINELEN 256
-char *TCID = "mmap14";
-int TST_TOTAL = 1;
-
static char *addr;
-
static void getvmlck(unsigned int *lock_sz);
-static void setup(void);
-static void cleanup(void);
+static struct rlimit rlim;
-int main(int argc, char *argv[])
+static void setup(void)
{
- int lc;
- unsigned int sz_before;
- unsigned int sz_after;
- unsigned int sz_ch;
-
- tst_parse_opts(argc, argv, NULL, NULL);
-
- setup();
-
- for (lc = 0; TEST_LOOPING(lc); lc++) {
-
- tst_count = 0;
-
- getvmlck(&sz_before);
-
- addr = mmap(NULL, MMAPSIZE, PROT_READ | PROT_WRITE,
- MAP_PRIVATE | MAP_LOCKED | MAP_ANONYMOUS,
- -1, 0);
-
- if (addr == MAP_FAILED) {
- tst_resm(TFAIL | TERRNO, "mmap of %s failed", TEMPFILE);
- continue;
- }
-
- getvmlck(&sz_after);
-
- sz_ch = sz_after - sz_before;
- if (sz_ch == MMAPSIZE / 1024) {
- tst_resm(TPASS, "Functionality of mmap() "
- "successful");
- } else {
- tst_resm(TFAIL, "Expected %luK locked, "
- "get %uK locked",
- MMAPSIZE / 1024, sz_ch);
- }
-
- if (munmap(addr, MMAPSIZE) != 0)
- tst_brkm(TFAIL | TERRNO, NULL, "munmap failed");
- }
-
- cleanup();
- tst_exit();
+ SAFE_GETRLIMIT(RLIMIT_MEMLOCK, &rlim);
}
void getvmlck(unsigned int *lock_sz)
{
- int ret;
char line[LINELEN];
- FILE *fstatus = NULL;
+ FILE *fp = NULL;
- fstatus = fopen("/proc/self/status", "r");
- if (fstatus == NULL)
- tst_brkm(TFAIL | TERRNO, NULL, "Open dev status failed");
+ fp = fopen("/proc/self/status", "r");
+ if (fp == NULL)
+ tst_brk(TFAIL | TERRNO, "could not open status file");
- while (fgets(line, LINELEN, fstatus) != NULL)
+ while (fgets(line, LINELEN, fp) != NULL) {
if (strstr(line, "VmLck") != NULL)
break;
+ }
- ret = sscanf(line, "%*[^0-9]%d%*[^0-9]", lock_sz);
- if (ret != 1)
- tst_brkm(TFAIL | TERRNO, NULL, "Get lock size failed");
+ if (sscanf(line, "%*[^0-9]%d%*[^0-9]", lock_sz) != 1)
+ tst_brk(TFAIL | TERRNO, "Getting locked memory size failed");
- fclose(fstatus);
+ fclose(fp);
}
-static void setup(void)
+static void run(void)
{
- tst_require_root();
+ unsigned int sz_before, sz_after, sz_diff;
- tst_sig(FORK, DEF_HANDLER, cleanup);
+ getvmlck(&sz_before);
- TEST_PAUSE;
-}
+ if (((sz_before * 1024) + MMAPSIZE) > rlim.rlim_cur)
+ tst_brk(TBROK, "Trying to exceed RLIMIT_MEMLOCK limit");
-static void cleanup(void)
-{
+ addr = mmap(NULL, MMAPSIZE, PROT_READ | PROT_WRITE,
+ MAP_PRIVATE | MAP_LOCKED | MAP_ANONYMOUS, -1, 0);
+
+ if (addr != MAP_FAILED) {
+ tst_res(TPASS, "mmap() with MAP_LOCKED flag passed");
+ } else {
+ tst_res(TFAIL | TERRNO, "mmap() failed");
+ return;
+ }
+
+ getvmlck(&sz_after);
+ sz_diff = sz_after - sz_before;
+ TST_EXP_EQ_LU(MMAPSIZE / 1024, sz_diff);
+
+ SAFE_MUNMAP(addr, MMAPSIZE);
}
+
+static struct tst_test test = {
+ .setup = setup,
+ .test_all = run
+};
--
2.41.0
--
Mailing list info: https://lists.linux.it/listinfo/ltp
next prev parent reply other threads:[~2023-09-05 13:31 UTC|newest]
Thread overview: 20+ messages / expand[flat|nested] mbox.gz Atom feed top
2023-08-25 6:38 [LTP] [PATCH 1/5] syscalls/mmap08: Rewrite the test using new LTP API Avinesh Kumar
2023-08-25 6:38 ` [LTP] [PATCH 2/5] syscalls/mmap09: " Avinesh Kumar
2023-09-01 8:27 ` Richard Palethorpe
2023-09-01 9:04 ` Cyril Hrubis
2023-09-01 9:11 ` Richard Palethorpe
2023-08-25 6:38 ` [LTP] [PATCH 3/5] syscalls/mmap13: Rewrite the test using new API Avinesh Kumar
2023-09-04 8:54 ` Richard Palethorpe
2023-08-25 6:38 ` [LTP] [PATCH 4/5] syscalls/mmap14: Rewrite test using new LTP API Avinesh Kumar
2023-09-01 9:23 ` Richard Palethorpe
2023-09-05 13:28 ` Avinesh Kumar [this message]
2023-10-09 10:59 ` [LTP] [PATCH v2] " Richard Palethorpe
2023-10-31 15:39 ` Cyril Hrubis
2023-08-25 6:38 ` [LTP] [PATCH 5/5] syscalls/mmap15: " Avinesh Kumar
2023-09-04 9:23 ` Richard Palethorpe
2023-09-05 16:01 ` [LTP] [PATCH v2] " Avinesh Kumar
2023-10-31 15:31 ` Cyril Hrubis
2023-12-11 20:49 ` [LTP] [PATCH v3] " Avinesh Kumar
2024-01-09 17:15 ` Petr Vorel
2024-01-10 9:41 ` Avinesh Kumar
2023-08-30 12:51 ` [LTP] [PATCH 1/5] syscalls/mmap08: Rewrite the " 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=20230905133118.23912-1-akumar@suse.de \
--to=akumar@suse.de \
--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.