public inbox for ltp@lists.linux.it
 help / color / mirror / Atom feed
From: Avinesh Kumar <akumar@suse.de>
To: ltp@lists.linux.it
Subject: [LTP] [PATCH] syscalls/mmap21: Add new test for mmap's ENOMEM case
Date: Thu, 31 Aug 2023 16:49:33 +0530	[thread overview]
Message-ID: <20230831112629.21510-1-akumar@suse.de> (raw)

To be more precise in this test and checking for ENOMEM on exact mapping
when limit is crossed, I could only think of counting the existing
mappings from /proc/$pid/maps and counting the remaining possible
mappings. Is there any better approach if we want to test this for exact
case?

Signed-off-by: Avinesh Kumar <akumar@suse.de>
---
 testcases/kernel/syscalls/mmap/mmap21.c | 70 +++++++++++++++++++++++++
 1 file changed, 70 insertions(+)
 create mode 100644 testcases/kernel/syscalls/mmap/mmap21.c

diff --git a/testcases/kernel/syscalls/mmap/mmap21.c b/testcases/kernel/syscalls/mmap/mmap21.c
new file mode 100644
index 000000000..76002edb3
--- /dev/null
+++ b/testcases/kernel/syscalls/mmap/mmap21.c
@@ -0,0 +1,70 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * Copyright (c) 2023 SUSE LLC Avinesh Kumar <avinesh.kumar@suse.com>
+ */
+
+/*\
+ * [Description]
+ *
+ * Verify that, mmap(2) fails with errno ENOMEM when process's
+ * maximum number of mappings would have been exceeded.
+ */
+
+#include <stdio.h>
+#include "tst_test.h"
+
+#define TEMPFILE "mmapfile"
+static int fd;
+static size_t page_sz;
+static int max_map;
+
+static void setup(void)
+{
+	page_sz = getpagesize();
+
+	fd = SAFE_OPEN(TEMPFILE, O_RDWR | O_CREAT, 0666);
+
+	SAFE_FILE_SCANF("/proc/sys/vm/max_map_count", "%d   ", &max_map);
+	tst_res(TINFO, "max_map_count: %d", max_map);
+}
+
+static void run(void)
+{
+	int i;
+	char *addr[max_map];
+
+	for (i = 0; i < max_map; i++) {
+		TESTPTR(mmap(0, page_sz, PROT_READ | PROT_WRITE, MAP_FILE | MAP_SHARED, fd, 0));
+		if (TST_RET_PTR != MAP_FAILED) {
+			addr[i] = TST_RET_PTR;
+		} else {
+			if (TST_ERR == ENOMEM) {
+				tst_res(TINFO, "New successful mappings before exhausting the limit: %d", i);
+				tst_res(TPASS, "mmap() failed with ENOMEM");
+			} else {
+				tst_res(TINFO, "New successful mappings before mmap() failed: %d", i);
+				tst_res(TFAIL | TERRNO, "mmap() failed with unexpected errno");
+			}
+			break;
+		}
+	}
+
+	if (i == max_map)
+		tst_res(TFAIL, "mmap() crossed max_map_count limit, no of new mmapings: %d", i);
+
+	while (--i >= 0)
+		SAFE_MUNMAP(addr[i], page_sz);
+}
+
+static void cleanup(void)
+{
+	if (fd > 0)
+		SAFE_CLOSE(fd);
+}
+
+static struct tst_test test = {
+	.setup = setup,
+	.cleanup = cleanup,
+	.test_all = run,
+	.needs_tmpdir = 1
+};
-- 
2.41.0


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

             reply	other threads:[~2023-08-31 11:26 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-08-31 11:19 Avinesh Kumar [this message]
2023-08-31 12:42 ` [LTP] [PATCH] syscalls/mmap21: Add new test for mmap's ENOMEM case Cyril Hrubis
2023-09-08  9:45   ` Richard Palethorpe

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=20230831112629.21510-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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox