Linux Test Project
 help / color / mirror / Atom feed
* [LTP] [PATCH v2 0/8] Cleanup the mmap testing suite
@ 2025-02-10  9:07 Andrea Cervesato
  2025-02-10  9:07 ` [LTP] [PATCH v2 1/8] Refactor mmap03 test Andrea Cervesato
                   ` (7 more replies)
  0 siblings, 8 replies; 19+ messages in thread
From: Andrea Cervesato @ 2025-02-10  9:07 UTC (permalink / raw)
  To: ltp; +Cc: rbm

Some of the tests in the mmap testing suites are using old API, having
documentation which is not linked to the metadata or need to be cleaned
up a bit. This patch-set is meant to fix these issues. mmap11 is for
IA64 only and that will require a separate patch and discussion.

Signed-off-by: Andrea Cervesato <andrea.cervesato@suse.com>
---
Changes in v2:
- mmap21: verify SIGSEGV
- Link to v1: https://lore.kernel.org/r/20250207-mmap_suite_refactoring-v1-0-d006d921e4d5@suse.com

---
Andrea Cervesato (8):
      Refactor mmap03 test
      Refactor mmap10 test
      Cleanup mmap12 test
      Cleanup mmap17 test
      Cleanup mmap18 test
      Cleanup mmap19 test
      Cleanup mmap20 test
      Refactor mmap001 test and move it to mmap21

 runtest/mm                                |   8 +-
 runtest/syscalls                          |   2 +-
 testcases/kernel/syscalls/mmap/.gitignore |   2 +-
 testcases/kernel/syscalls/mmap/mmap001.c  | 183 ---------------------
 testcases/kernel/syscalls/mmap/mmap03.c   | 265 ++++++++----------------------
 testcases/kernel/syscalls/mmap/mmap10.c   | 261 ++++++++++-------------------
 testcases/kernel/syscalls/mmap/mmap12.c   |  30 +---
 testcases/kernel/syscalls/mmap/mmap17.c   |  23 +--
 testcases/kernel/syscalls/mmap/mmap18.c   |  96 ++++++-----
 testcases/kernel/syscalls/mmap/mmap19.c   |   2 +-
 testcases/kernel/syscalls/mmap/mmap20.c   |   3 -
 testcases/kernel/syscalls/mmap/mmap21.c   | 108 ++++++++++++
 12 files changed, 337 insertions(+), 646 deletions(-)
---
base-commit: 2c7100e851308c4e65525b66f1f6749d52791e43
change-id: 20250205-mmap_suite_refactoring-322042abb356

Best regards,
-- 
Andrea Cervesato <andrea.cervesato@suse.com>


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

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

* [LTP] [PATCH v2 1/8] Refactor mmap03 test
  2025-02-10  9:07 [LTP] [PATCH v2 0/8] Cleanup the mmap testing suite Andrea Cervesato
@ 2025-02-10  9:07 ` Andrea Cervesato
  2025-02-10  9:41   ` Petr Vorel
  2025-02-10  9:07 ` [LTP] [PATCH v2 2/8] Refactor mmap10 test Andrea Cervesato
                   ` (6 subsequent siblings)
  7 siblings, 1 reply; 19+ messages in thread
From: Andrea Cervesato @ 2025-02-10  9:07 UTC (permalink / raw)
  To: ltp; +Cc: rbm

From: Andrea Cervesato <andrea.cervesato@suse.com>

Signed-off-by: Andrea Cervesato <andrea.cervesato@suse.com>
---
 testcases/kernel/syscalls/mmap/mmap03.c | 265 +++++++++-----------------------
 1 file changed, 71 insertions(+), 194 deletions(-)

diff --git a/testcases/kernel/syscalls/mmap/mmap03.c b/testcases/kernel/syscalls/mmap/mmap03.c
index 9d94d2653661387d22f811cd959a87b8112c1167..f22365b259dac4e99bf8ced7f952bbc3696da509 100644
--- a/testcases/kernel/syscalls/mmap/mmap03.c
+++ b/testcases/kernel/syscalls/mmap/mmap03.c
@@ -1,230 +1,107 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
 /*
  * Copyright (c) International Business Machines  Corp., 2001
- *
- * 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
+ *               07/2001 Ported by Wayne Boyer
+ * Copyright (C) 2025 SUSE LLC Andrea Cervesato <andrea.cervesato@suse.com>
  */
 
-/*
- * Test Description:
- *  Call mmap() to map a file creating a mapped region with execute access
- *  under the following conditions -
- *	- The prot parameter is set to PROT_EXE
- *	- The file descriptor is open for read
- *	- The file being mapped has execute permission bit set.
- *	- The minimum file permissions should be 0555.
+/*\
+ * [Description]
+ *
+ * Map a file with mmap() syscall, creating a mapped region with execute access
+ * under the following conditions:
  *
- *  The call should succeed to map the file creating mapped memory with the
- *  required attributes.
+ * - file descriptor is open for read
+ * - minimum file permissions should be 0555
+ * - file being mapped has PROT_EXEC execute permission bit set
  *
- * Expected Result:
- *  mmap() should succeed returning the address of the mapped region,
- *  and the mapped region should contain the contents of the mapped file.
- *  but with ia64 and PARISC/hppa,
- *  an attempt to access the contents of the mapped region should give
- *  rise to the signal SIGSEGV.
+ * mmap() should succeed returning the address of the mapped region
+ * and the mapped region should contain the contents of the mapped file.
  *
- * HISTORY
- *	07/2001 Ported by Wayne Boyer
+ * With ia64 and PARISC/hppa, an attempt to access the contents of the
+ * mapped region should rise signal SIGSEGV.
  */
-#include <stdio.h>
-#include <stdlib.h>
-#include <sys/types.h>
-#include <errno.h>
-#include <unistd.h>
-#include <fcntl.h>
-#include <string.h>
-#include <signal.h>
-#include <sys/stat.h>
-#include <sys/mman.h>
-#include <setjmp.h>
 
-#include "test.h"
+#include "tst_test.h"
 
-#define TEMPFILE	"mmapfile"
-
-char *TCID = "mmap03";
-int TST_TOTAL = 1;
+#define TEMPFILE "mmapfile"
 
+static void *addr;
+static char *tmpdata;
 static size_t page_sz;
-static char *addr;
-static char *dummy;
-static int fildes;
-static volatile int pass = 0;
-static sigjmp_buf env;
-
-static void setup(void);
-static void cleanup(void);
-static void sig_handler(int sig);
+static int fdesc = -1;
 
-int main(int ac, char **av)
+static void run_child(void)
 {
-	int lc;
-
-	tst_parse_opts(ac, av, NULL, NULL);
-
-	setup();
-
-	for (lc = 0; TEST_LOOPING(lc); lc++) {
-
-		tst_count = 0;
-
-		/*
-		 * Call mmap to map the temporary file 'TEMPFILE'
-		 * with execute access.
-		 */
-		errno = 0;
-		addr = mmap(0, page_sz, PROT_EXEC,
-			    MAP_FILE | MAP_SHARED, fildes, 0);
-
-		/* Check for the return value of mmap() */
-		if (addr == MAP_FAILED) {
-			tst_resm(TFAIL | TERRNO, "mmap() failed on %s",
-				 TEMPFILE);
-			continue;
-		}
-
-		/*
-		 * Read the file contents into the dummy
-		 * variable.
-		 */
-		if (read(fildes, dummy, page_sz) < 0) {
-			tst_brkm(TFAIL | TERRNO, cleanup,
-				 "reading %s failed", TEMPFILE);
-		}
-
-		/*
-		 * Check whether the mapped memory region
-		 * has the file contents.
-		 *
-		 * with ia64 and PARISC/hppa, this should
-		 * generate a SIGSEGV which will be caught below.
-		 *
-		 */
-
-		if (sigsetjmp(env, 1) == 0) {
-			if (memcmp(dummy, addr, page_sz)) {
-				tst_resm(TFAIL,
-					 "mapped memory region "
-					 "contains invalid data");
-			} else {
-				tst_resm(TPASS,
-					 "mmap() functionality is "
-					 "correct");
-			}
-		}
-#if defined(__ia64__) || defined(__hppa__) || defined(__mips__)
-		if (pass) {
-			tst_resm(TPASS, "Got SIGSEGV as expected");
-		} else {
-			tst_resm(TFAIL, "Mapped memory region with NO "
-				 "access is accessible");
-		}
-#endif
+	char *data;
 
-		/* Clean up things in case we are looping */
-		/* Unmap the mapped memory */
-		if (munmap(addr, page_sz) != 0) {
-			tst_brkm(TFAIL | TERRNO, cleanup,
-				 "failed to unmap the mmapped pages");
-		}
-		pass = 0;
+	tst_res(TINFO, "Map temporary file in memory with PROT_EXEC");
 
-	}
-
-	cleanup();
-	tst_exit();
-}
+	addr = SAFE_MMAP(0, page_sz, PROT_EXEC,
+		  MAP_FILE | MAP_SHARED, fdesc, 0);
 
-static void setup(void)
-{
-	char *tst_buff;
+	data = SAFE_MALLOC(page_sz);
+	memset(data, 0, page_sz);
 
-	tst_sig(NOFORK, sig_handler, cleanup);
+	tst_res(TINFO, "Read data back from mapped file");
 
-	TEST_PAUSE;
-
-	page_sz = getpagesize();
-
-	/* Allocate space for the test buffer */
-	if ((tst_buff = calloc(page_sz, sizeof(char))) == NULL) {
-		tst_brkm(TFAIL, NULL, "calloc failed (tst_buff)");
-	}
+	SAFE_READ(0, fdesc, data, page_sz);
+	SAFE_LSEEK(fdesc, 0, SEEK_SET);
 
-	/* Fill the test buffer with the known data */
-	memset(tst_buff, 'A', page_sz);
+	TST_EXP_EQ_LI(memcmp(data, tmpdata, page_sz), 0);
 
-	tst_tmpdir();
+	SAFE_MUNMAP(addr, page_sz);
+	free(data);
+}
 
-	/* Creat a temporary file used for mapping */
-	if ((fildes = open(TEMPFILE, O_WRONLY | O_CREAT, 0666)) < 0) {
-		free(tst_buff);
-		tst_brkm(TFAIL | TERRNO, cleanup, "opening %s failed",
-			 TEMPFILE);
-	}
+static void run(void)
+{
+	pid_t pid;
+	int status;
 
-	/* Write test buffer contents into temporary file */
-	if (write(fildes, tst_buff, page_sz) < (long)page_sz) {
-		free(tst_buff);
-		tst_brkm(TFAIL | TERRNO, cleanup, "writing to %s failed",
-			 TEMPFILE);
+	pid = SAFE_FORK();
+	if (!pid) {
+		run_child();
+		exit(0);
 	}
 
-	/* Free the memory allocated for test buffer */
-	free(tst_buff);
+	SAFE_WAITPID(pid, &status, 0);
 
-	/* Make sure proper permissions set on file */
-	if (fchmod(fildes, 0555) < 0) {
-		tst_brkm(TFAIL, cleanup, "fchmod of %s failed", TEMPFILE);
+#if defined(__ia64__) || defined(__hppa__) || defined(__mips__)
+	if (WIFSIGNALED(status) && WTERMSIG(status) == SIGSEGV) {
+		tst_res(TPASS, "SIGSEGV has been received");
+	} else {
+		tst_res(TFAIL, "Mapped memory region with NO "
+			"access is accessible");
 	}
+#endif
+}
 
-	/* Close the temporary file opened for write */
-	if (close(fildes) < 0) {
-		tst_brkm(TFAIL | TERRNO, cleanup, "closing %s failed",
-			 TEMPFILE);
-	}
+static void setup(void)
+{
+	page_sz = getpagesize();
 
-	/* Allocate and initialize dummy string of system page size bytes */
-	if ((dummy = calloc(page_sz, sizeof(char))) == NULL) {
-		tst_brkm(TFAIL, cleanup, "calloc failed (dummy)");
-	}
+	tmpdata = SAFE_MALLOC(page_sz);
+	memset(tmpdata, 'a', page_sz);
 
-	/* Open the temporary file again for reading */
-	if ((fildes = open(TEMPFILE, O_RDONLY)) < 0) {
-		tst_brkm(TFAIL | TERRNO, cleanup,
-			 "opening %s read-only failed", TEMPFILE);
-	}
-}
+	tst_res(TINFO, "Create temporary file");
 
-/*
- *   This function gets executed when the test process receives
- *   the signal SIGSEGV while trying to access the contents of memory which
- *   is not accessible.
- */
-static void sig_handler(int sig)
-{
-	if (sig == SIGSEGV) {
-		/* set the global variable and jump back */
-		pass = 1;
-		siglongjmp(env, 1);
-	} else
-		tst_brkm(TBROK, cleanup, "received an unexpected signal");
+	tst_fill_file(TEMPFILE, 'a', page_sz, 1);
+	fdesc = SAFE_OPEN(TEMPFILE, O_RDONLY, 0555);
 }
 
 static void cleanup(void)
 {
-	close(fildes);
-	free(dummy);
-	tst_rmdir();
+	free(tmpdata);
+
+	if (fdesc != -1)
+		SAFE_CLOSE(fdesc);
 }
+
+static struct tst_test test = {
+	.test_all = run,
+	.setup = setup,
+	.cleanup = cleanup,
+	.needs_tmpdir = 1,
+	.forks_child = 1,
+};

-- 
2.43.0


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

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

* [LTP] [PATCH v2 2/8] Refactor mmap10 test
  2025-02-10  9:07 [LTP] [PATCH v2 0/8] Cleanup the mmap testing suite Andrea Cervesato
  2025-02-10  9:07 ` [LTP] [PATCH v2 1/8] Refactor mmap03 test Andrea Cervesato
@ 2025-02-10  9:07 ` Andrea Cervesato
  2025-02-24 16:57   ` Cyril Hrubis
  2025-02-10  9:07 ` [LTP] [PATCH v2 3/8] Cleanup mmap12 test Andrea Cervesato
                   ` (5 subsequent siblings)
  7 siblings, 1 reply; 19+ messages in thread
From: Andrea Cervesato @ 2025-02-10  9:07 UTC (permalink / raw)
  To: ltp; +Cc: rbm

From: Andrea Cervesato <andrea.cervesato@suse.com>

Signed-off-by: Andrea Cervesato <andrea.cervesato@suse.com>
---
 testcases/kernel/syscalls/mmap/mmap10.c | 261 +++++++++++---------------------
 1 file changed, 90 insertions(+), 171 deletions(-)

diff --git a/testcases/kernel/syscalls/mmap/mmap10.c b/testcases/kernel/syscalls/mmap/mmap10.c
index b844af07fd78d69c5cf5afc3039a54685c982776..3d9f73abf1e8a453fe6137d588c0e7c1240d8ca7 100644
--- a/testcases/kernel/syscalls/mmap/mmap10.c
+++ b/testcases/kernel/syscalls/mmap/mmap10.c
@@ -1,206 +1,125 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
 /*
  * Copyright (C) 2010  Red Hat, Inc.
- * This program is free software; you can redistribute it and/or
- * modify it under the terms of version 2 of the GNU General Public
- * License as published by the Free Software Foundation.
- *
- * This program is distributed in the hope that it would be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
- *
- * Further, this software is distributed without any warranty that it
- * is free of the rightful claim of any third person regarding
- * infringement or the like.  Any license provided herein, whether
- * implied or otherwise, applies only to this software file.  Patent
- * licenses, if any, provided herein do not apply to combinations of
- * this program with other software, or any other product whatsoever.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write the Free Software
- * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
- * 02110-1301, USA.
+ * Copyright (C) 2025 SUSE LLC Andrea Cervesato <andrea.cervesato@suse.com>
  */
 
-/*
- * mmap/munmap /dev/zero: a common way of malloc()/free() anonymous
- * memory on Solaris.
+/*\
+ * [Description]
+ *
+ * This test examines the functionality of mapping and unmapping /dev/zero,
+ * which is a common method for allocating anonymous memory in Solaris.
  *
- * The basic purpose of this is a to test if it is possible to map and
- * unmap /dev/zero, and to read and write the mapping. Being inspired
- * by two bugs in the past, the design of the test was added some
- * variations based on the reproducers for them. It also accept an
- * option to mmap/munmap anonymous pages.
+ * The primary objective is to determine whether it is possible to successfully
+ * map and unmap /dev/zero, as well as to read from and write to the mapped
+ * memory. The design of this test is inspired by two previous bugs,
+ * incorporating variations based on their reproducers. Additionally, the test
+ * accepts an option to mmap/munmap anonymous pages.
  *
- * One is to trigger panic with transparent hugepage feature that
- * split_huge_page is very strict in checking the rmap walk was
- * perfect. Keep it strict because if page_mapcount isn't stable and
- * just right, the __split_huge_page_refcount that follows the rmap
- * walk could lead to erratic page_count()s for the subpages. The bug
- * in fork lead to the rmap walk finding the parent huge-pmd twice
- * instead of just one, because the anon_vma_chain objects of the
- * child vma still point to the vma->vm_mm of the parent. That trips
- * on the split_huge_page mapcount vs page_mapcount check leading to a
- * BUG_ON.
+ * One of the bugs aims to trigger a panic related to the transparent hugepage
+ * feature. The split_huge_page function is particularly strict in verifying
+ * that the reverse mapping (rmap) walk is accurate. This strictness is crucial
+ * because if the page_mapcount is not stable or correct, the subsequent
+ * __split_huge_page_refcount operation could lead to inconsistent page_count()
+ * values for the subpages. A bug related to fork caused the rmap walk to find
+ * the parent huge-pmd twice instead of once, due to the anon_vma_chain objects
+ * of the child VMA still pointing to the parent's vma->vm_mm. This
+ * inconsistency triggers a failure in the split_huge_page mapcount versus
+ * page_mapcount check, resulting in a BUG_ON.
  *
- * The other bug is mmap() of /dev/zero results in calling map_zero()
- * which on RHEL5 maps the ZERO_PAGE in every PTE within that virtual
- * address range. Since the application which maps a region from 5M to
- * 16M in size is also multi-threaded the subsequent munmap() of
- * /dev/zero results is TLB shootdowns to all other CPUs. When this
- * happens thousands or millions of times the application performance
- * is terrible. The mapping ZERO_PAGE in every pte within that virtual
- * address range was an optimization to make the subsequent pagefault
- * times faster on RHEL5 that has been removed/changed upstream.
+ * The second bug involves the mmap() operation on /dev/zero, which invokes
+ * map_zero(). On RHEL5, this function maps the ZERO_PAGE into every page table
+ * entry (PTE) within the specified virtual address range. When an application
+ * maps a region from 5M to 16M and operates in a multi-threaded environment,
+ * the subsequent munmap() of /dev/zero leads to TLB shootdowns across all CPUs.
+ * When this occurs thousands or millions of times, it severely degrades
+ * application performance. The optimization of mapping the ZERO_PAGE in every
+ * PTE within that virtual address range was intended to enhance page fault
+ * handling times on RHEL5 but has since been modified or removed in upstream
+ * versions.
  */
-#include <sys/types.h>
-#include <sys/stat.h>
-#include <sys/wait.h>
-#include <sys/mman.h>
-#include <errno.h>
-#include <unistd.h>
-#include <stdlib.h>
-#include <stdio.h>
-#include <fcntl.h>
-#include "test.h"
-#include "config.h"
-
-#define SIZE (5*1024*1024)
-#define PATH_KSM "/sys/kernel/mm/ksm/"
-
-char *TCID = "mmap10";
-int TST_TOTAL = 1;
-
-static int fd, opt_anon, opt_ksm;
-static long ps;
-static char *x;
-
-void setup(void);
-void cleanup(void);
-void mmapzero(void);
-void help(void);
-
-static option_t options[] = {
-	{"a", &opt_anon, NULL},
-	{"s", &opt_ksm, NULL},
-	{NULL, NULL, NULL}
-};
-
-int main(int argc, char *argv[])
-{
-	int lc;
-
-	tst_parse_opts(argc, argv, options, help);
-
-	if (opt_ksm) {
-		if (access(PATH_KSM, F_OK) == -1)
-			tst_brkm(TCONF, NULL,
-				 "KSM configuration is not enabled");
-#ifdef HAVE_DECL_MADV_MERGEABLE
-		tst_resm(TINFO, "add to KSM regions.");
-#else
-		tst_brkm(TCONF, NULL, "MADV_MERGEABLE missing in sys/mman.h");
-#endif
-	}
-	if (opt_anon)
-		tst_resm(TINFO, "use anonymous pages.");
-	else
-		tst_resm(TINFO, "use /dev/zero.");
 
-	setup();
+#include "tst_test.h"
 
-	tst_resm(TINFO, "start tests.");
-	for (lc = 0; TEST_LOOPING(lc); lc++) {
-		tst_count = 0;
-		mmapzero();
-	}
+#define SIZE (5 * TST_MB)
+#define PATH_KSM "/sys/kernel/mm/ksm/"
 
-	cleanup();
-	tst_exit();
-}
+static char *opt_ksm;
+static char *opt_anon;
+static size_t page_sz;
+static char *memory;
 
-void mmapzero(void)
+static void run(void)
 {
-	int n;
+	int fd = -1;
 
 	if (opt_anon) {
-		x = mmap(NULL, SIZE + SIZE - ps, PROT_READ | PROT_WRITE,
-			 MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
+		memory = SAFE_MMAP(NULL, SIZE + SIZE - page_sz,
+			PROT_READ | PROT_WRITE,
+			MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
 	} else {
-		if ((fd = open("/dev/zero", O_RDWR, 0666)) < 0)
-			tst_brkm(TBROK | TERRNO, cleanup, "open");
-		x = mmap(NULL, SIZE + SIZE - ps, PROT_READ | PROT_WRITE,
-			 MAP_PRIVATE, fd, 0);
+		fd = SAFE_OPEN("/dev/zero", O_RDWR, 0666);
+
+		memory = SAFE_MMAP(NULL, SIZE + SIZE - page_sz,
+			PROT_READ | PROT_WRITE,
+			MAP_PRIVATE, fd, 0);
 	}
-	if (x == MAP_FAILED)
-		tst_brkm(TFAIL | TERRNO, cleanup, "mmap");
+
 #ifdef HAVE_DECL_MADV_MERGEABLE
 	if (opt_ksm) {
-		if (madvise(x, SIZE + SIZE - ps, MADV_MERGEABLE) == -1)
-			tst_brkm(TBROK | TERRNO, cleanup, "madvise");
+		if (madvise(memory, SIZE + SIZE - page_sz, MADV_MERGEABLE) == -1)
+			tst_brk(TBROK | TERRNO, "madvise error");
 	}
 #endif
-	x[SIZE] = 0;
-
-	switch (n = fork()) {
-	case -1:
-		tst_brkm(TBROK | TERRNO, cleanup, "fork");
-	case 0:
-		if (munmap(x + SIZE + ps, SIZE - ps - ps) == -1)
-			tst_brkm(TFAIL | TERRNO, cleanup, "munmap");
-		exit(0);
-	default:
-		break;
-	}
 
-	switch (n = fork()) {
-	case -1:
-		tst_brkm(TBROK | TERRNO, cleanup, "fork");
-	case 0:
-		if (munmap(x + SIZE + ps, SIZE - ps - ps) == -1)
-			tst_brkm(TFAIL | TERRNO, cleanup,
-				 "subsequent munmap #1");
-		exit(0);
-	default:
-		switch (n = fork()) {
-		case -1:
-			tst_brkm(TBROK | TERRNO, cleanup, "fork");
-		case 0:
-			if (munmap(x + SIZE + ps, SIZE - ps - ps) == -1)
-				tst_brkm(TFAIL | TERRNO, cleanup,
-					 "subsequent munmap #2");
+	memory[SIZE] = 0;
+
+	for (int i = 0; i < 3; i++) {
+		if (!SAFE_FORK()) {
+			SAFE_MUNMAP(memory + SIZE + page_sz, SIZE - page_sz * 2);
 			exit(0);
-		default:
-			break;
 		}
-		break;
 	}
 
-	if (munmap(x, SIZE + SIZE - ps) == -1)
-		tst_resm(TFAIL | TERRNO, "munmap all");
+	SAFE_MUNMAP(memory, SIZE + SIZE - page_sz);
 
-	while (waitpid(-1, &n, WUNTRACED | WCONTINUED) > 0)
-		if (WEXITSTATUS(n) != 0)
-			tst_resm(TFAIL, "child exit status is %d",
-				 WEXITSTATUS(n));
-}
+	tst_reap_children();
 
-void cleanup(void)
-{
+	tst_res(TPASS, "All memory has been released");
+
+	if (fd != -1)
+		SAFE_CLOSE(fd);
 }
 
-void setup(void)
+static void setup(void)
 {
-	tst_require_root();
+	if (opt_ksm) {
+		if (access(PATH_KSM, F_OK) == -1)
+			tst_brk(TCONF, "KSM configuration is not enabled");
 
-	tst_sig(FORK, DEF_HANDLER, cleanup);
-	TEST_PAUSE;
+#ifdef HAVE_DECL_MADV_MERGEABLE
+		tst_res(TINFO, "Add to KSM regions");
+#else
+		tst_brk(TCONF, "MADV_MERGEABLE missing in sys/mman.h");
+#endif
+	}
 
-	if ((ps = sysconf(_SC_PAGESIZE)) == -1)
-		tst_brkm(TBROK | TERRNO, cleanup, "sysconf(_SC_PAGESIZE)");
-}
+	if (opt_anon)
+		tst_res(TINFO, "Use anonymous pages");
+	else
+		tst_res(TINFO, "Use /dev/zero device");
 
-void help(void)
-{
-	printf("  -a      Test anonymous pages\n");
-	printf("  -s      Add to KSM regions\n");
+	page_sz = SAFE_SYSCONF(_SC_PAGESIZE);
 }
+
+static struct tst_test test = {
+	.test_all = run,
+	.setup = setup,
+	.needs_root = 1,
+	.forks_child = 1,
+	.options = (struct tst_option[]) {
+		{"a", &opt_anon, "Test anonymous pages"},
+		{"s", &opt_ksm, "Add to KSM regions"},
+		{}
+	},
+};

-- 
2.43.0


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

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

* [LTP] [PATCH v2 3/8] Cleanup mmap12 test
  2025-02-10  9:07 [LTP] [PATCH v2 0/8] Cleanup the mmap testing suite Andrea Cervesato
  2025-02-10  9:07 ` [LTP] [PATCH v2 1/8] Refactor mmap03 test Andrea Cervesato
  2025-02-10  9:07 ` [LTP] [PATCH v2 2/8] Refactor mmap10 test Andrea Cervesato
@ 2025-02-10  9:07 ` Andrea Cervesato
  2025-02-25 10:17   ` Cyril Hrubis
  2025-02-10  9:07 ` [LTP] [PATCH v2 4/8] Cleanup mmap17 test Andrea Cervesato
                   ` (4 subsequent siblings)
  7 siblings, 1 reply; 19+ messages in thread
From: Andrea Cervesato @ 2025-02-10  9:07 UTC (permalink / raw)
  To: ltp; +Cc: rbm

From: Andrea Cervesato <andrea.cervesato@suse.com>

Signed-off-by: Andrea Cervesato <andrea.cervesato@suse.com>
---
 testcases/kernel/syscalls/mmap/mmap12.c | 30 +++++++-----------------------
 1 file changed, 7 insertions(+), 23 deletions(-)

diff --git a/testcases/kernel/syscalls/mmap/mmap12.c b/testcases/kernel/syscalls/mmap/mmap12.c
index 995a2bab091a745dd7332fe919c1ea38b25641e3..7a3dd2e1731efcfde499b7ae72d6805475889b27 100644
--- a/testcases/kernel/syscalls/mmap/mmap12.c
+++ b/testcases/kernel/syscalls/mmap/mmap12.c
@@ -1,31 +1,15 @@
 // SPDX-License-Identifier: GPL-2.0-or-later
 /*
  * Copyright (c) 2013 FNST, DAN LI <li.dan@cn.fujitsu.com>
+ * Copyright (C) 2025 SUSE LLC Andrea Cervesato <andrea.cervesato@suse.com>
  */
 
-/*
- * Test Description:
- *  Verify MAP_POPULATE works fine.
- *  "For a file mapping, this causes read-ahead on the file.
- *   Later accesses to the mapping will not be blocked by page faults"
+/*\
+ * [Description]
  *
- * Expected Result:
- *  mmap() with MAP_POPULATE should succeed returning the address of the
- *  mapped region and this file has been read into RAM, so pages should
- *  be present.
+ * Verify that mmap() with MAP_POPULATE succeed returning the address of the
+ * mapped region. The file should be read into RAM, and pages should be present.
  */
-#include <stdio.h>
-#include <stdlib.h>
-#include <sys/types.h>
-#include <errno.h>
-#include <unistd.h>
-#include <fcntl.h>
-#include <string.h>
-#include <signal.h>
-#include <stdint.h>
-#include <sys/stat.h>
-#include <sys/mman.h>
-#include <sys/shm.h>
 
 #include "tst_test.h"
 
@@ -60,9 +44,9 @@ static void page_check(void)
 			tst_res(TCONF | TERRNO,
 				"don't have permission to open dev pagemap");
 			return;
-		} else {
-			tst_brk(TFAIL | TERRNO, "pen dev pagemap failed");
 		}
+
+		tst_brk(TFAIL | TERRNO, "pen dev pagemap failed");
 	}
 
 	offset = SAFE_LSEEK(pm, index, SEEK_SET);

-- 
2.43.0


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

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

* [LTP] [PATCH v2 4/8] Cleanup mmap17 test
  2025-02-10  9:07 [LTP] [PATCH v2 0/8] Cleanup the mmap testing suite Andrea Cervesato
                   ` (2 preceding siblings ...)
  2025-02-10  9:07 ` [LTP] [PATCH v2 3/8] Cleanup mmap12 test Andrea Cervesato
@ 2025-02-10  9:07 ` Andrea Cervesato
  2025-02-25 15:02   ` Cyril Hrubis
  2025-02-10  9:07 ` [LTP] [PATCH v2 5/8] Cleanup mmap18 test Andrea Cervesato
                   ` (3 subsequent siblings)
  7 siblings, 1 reply; 19+ messages in thread
From: Andrea Cervesato @ 2025-02-10  9:07 UTC (permalink / raw)
  To: ltp; +Cc: rbm

From: Andrea Cervesato <andrea.cervesato@suse.com>

Signed-off-by: Andrea Cervesato <andrea.cervesato@suse.com>
---
 testcases/kernel/syscalls/mmap/mmap17.c | 23 +++++++++--------------
 1 file changed, 9 insertions(+), 14 deletions(-)

diff --git a/testcases/kernel/syscalls/mmap/mmap17.c b/testcases/kernel/syscalls/mmap/mmap17.c
index 39703fbd397d33fe549b1c9a52db62f763e146dd..0d2ed9ccf7928f960f8bfa5e31604e840c6939a8 100644
--- a/testcases/kernel/syscalls/mmap/mmap17.c
+++ b/testcases/kernel/syscalls/mmap/mmap17.c
@@ -1,28 +1,23 @@
 // SPDX-License-Identifier: GPL-2.0-or-later
 /*
  * Copyright (c) Zilogic Systems Pvt. Ltd., 2020
- * Email: code@zilogic.com
+ *	Email: code@zilogic.com
+ * Copyright (C) 2025 SUSE LLC Andrea Cervesato <andrea.cervesato@suse.com>
  */
 
-/*
- * Test mmap with MAP_FIXED_NOREPLACE flag
+/*\
+ * [Description]
  *
- * We are testing the MAP_FIXED_NOREPLACE flag of mmap() syscall. To check
+ * Verify MAP_FIXED_NOREPLACE flag for the mmap() syscall and check
  * if an attempt to mmap at an exisiting mapping fails with EEXIST.
+ *
+ * [Algorithm]
+ *
  * The code allocates a free address by passing NULL to first mmap call
- * Then tries to mmap with the same address using MAP_FIXED_NOREPLACE flag
+ * then tries to mmap with the same address using MAP_FIXED_NOREPLACE flag
  * and the mapping fails as expected.
  */
 
-#include <stdio.h>
-#include <fcntl.h>
-#include <sys/types.h>
-#include <sys/stat.h>
-#include <unistd.h>
-#include <errno.h>
-#include <string.h>
-#include <stdlib.h>
-#include "lapi/mmap.h"
 #include "tst_test.h"
 
 static int fd_file1;

-- 
2.43.0


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

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

* [LTP] [PATCH v2 5/8] Cleanup mmap18 test
  2025-02-10  9:07 [LTP] [PATCH v2 0/8] Cleanup the mmap testing suite Andrea Cervesato
                   ` (3 preceding siblings ...)
  2025-02-10  9:07 ` [LTP] [PATCH v2 4/8] Cleanup mmap17 test Andrea Cervesato
@ 2025-02-10  9:07 ` Andrea Cervesato
  2025-02-25 15:20   ` Cyril Hrubis
  2025-02-10  9:07 ` [LTP] [PATCH v2 6/8] Cleanup mmap19 test Andrea Cervesato
                   ` (2 subsequent siblings)
  7 siblings, 1 reply; 19+ messages in thread
From: Andrea Cervesato @ 2025-02-10  9:07 UTC (permalink / raw)
  To: ltp; +Cc: rbm

From: Andrea Cervesato <andrea.cervesato@suse.com>

Signed-off-by: Andrea Cervesato <andrea.cervesato@suse.com>
---
 testcases/kernel/syscalls/mmap/mmap18.c | 96 ++++++++++++++++-----------------
 1 file changed, 47 insertions(+), 49 deletions(-)

diff --git a/testcases/kernel/syscalls/mmap/mmap18.c b/testcases/kernel/syscalls/mmap/mmap18.c
index b37b29890ca009ea671b29e81e02fc1e42b44dbb..d920a50ae33badfb005143eb06c5aad17c1e72a3 100644
--- a/testcases/kernel/syscalls/mmap/mmap18.c
+++ b/testcases/kernel/syscalls/mmap/mmap18.c
@@ -1,74 +1,72 @@
 // SPDX-License-Identifier: GPL-2.0-or-later
 /*
  * Copyright (c) Zilogic Systems Pvt. Ltd., 2020
- * Email: code@zilogic.com
+ *	Email: code@zilogic.com
+ * Copyright (C) 2025 SUSE LLC Andrea Cervesato <andrea.cervesato@suse.com>
  */
 
-/*
- * Test mmap() MAP_GROWSDOWN flag
+/*\
+ * [Description]
+ *
+ * Verify mmap() syscall using MAP_GROWSDOWN flag.
  *
- * # Test1:
+ * [Algorithm]
  *
- *   We assign the memory region partially allocated with MAP_GROWSDOWN flag to
- *   a thread as a stack and expect the mapping to grow when we touch the
- *   guard page by calling a recusive function in the thread that uses the
- *   growable mapping as a stack.
+ * **Test 1**
  *
- *   The kernel only grows the memory region when the stack pointer is within
- *   guard page when the guard page is touched so simply faulting the guard
- *   page will not cause the mapping to grow.
+ * We assign the memory region partially allocated with MAP_GROWSDOWN flag to
+ * a thread as a stack and expect the mapping to grow when we touch the
+ * guard page by calling a recusive function in the thread that uses the
+ * growable mapping as a stack.
  *
- *   Newer kernels does not allow a MAP_GROWSDOWN mapping to grow closer than
- *   'stack_guard_gap' pages to an existing mapping. So when we map the stack we
- *   make sure there is enough of free address space before the lowest stack
- *   address.
+ * The kernel only grows the memory region when the stack pointer is within
+ * guard page when the guard page is touched so simply faulting the guard
+ * page will not cause the mapping to grow.
  *
- *   Kernel default 'stack_guard_gap' size is '256 * getpagesize()'.
+ * Newer kernels does not allow a MAP_GROWSDOWN mapping to grow closer than
+ * 'stack_guard_gap' pages to an existing mapping. So when we map the stack we
+ * make sure there is enough of free address space before the lowest stack
+ * address.
  *
- *   The stack memory map would look like:
+ * Kernel default `stack_guard_gap` size is `256 * getpagesize()`.
  *
- *   |  -  -  -   reserved  size   -  -  -  |
+ * The stack memory map would look like:
  *
- *   +-- - - - --+------------+-------------+
- *   | 256 pages |  unmapped  |   mapped    |
- *   +-- - - - --+------------+-------------+
- *                            | mapped size |
- *   ^           |  -  -  stack size  -  -  |
- *   start
- *               ^                          ^
- *               stack bottom       stack top
+ * |  -  -  -   reserved  size   -  -  -  |
  *
- * # Test2:
+ * +-- - - - --+------------+-------------+
+ * | 256 pages |  unmapped  |   mapped    |
+ * +-- - - - --+------------+-------------+
+ *                          | mapped size |
+ * ^           |  -  -  stack size  -  -  |
+ * start
+ *             ^                          ^
+ *             stack bottom       stack top
  *
- *   We allocate stack as we do in the first test but we mmap a page in the
- *   space the stack is supposed to grow into and we expect the thread to
- *   segfault when the guard page is faulted.
+ * **Test 2**
+ *
+ * We allocate stack as we do in the first test but we mmap a page in the
+ * space the stack is supposed to grow into and we expect the thread to
+ * segfault when the guard page is faulted.
  */
 
-#include <unistd.h>
 #include <pthread.h>
-#include <sys/mman.h>
-#include <sys/wait.h>
-#include <sys/types.h>
-#include <stdlib.h>
-#include <stdbool.h>
-
 #include "tst_test.h"
 #include "tst_safe_pthread.h"
 
 static long page_size;
 
-static bool __attribute__((noinline)) check_stackgrow_up(void)
+static bool __attribute_noinline__ check_stackgrow_up(void)
 {
 	char local_var;
 	static char *addr;
 
-       if (!addr) {
-               addr = &local_var;
-               return check_stackgrow_up();
-       }
+	if (!addr) {
+		addr = &local_var;
+		return check_stackgrow_up();
+	}
 
-       return (addr < &local_var);
+	return (addr < &local_var);
 }
 
 static void setup(void)
@@ -90,7 +88,7 @@ static void *allocate_stack(size_t stack_size, size_t mapped_size)
 	long reserved_size = 256 * page_size + stack_size;
 
 	start = SAFE_MMAP(NULL, reserved_size, PROT_READ | PROT_WRITE,
-	                  MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
+			MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
 	SAFE_MUNMAP(start, reserved_size);
 
 	SAFE_MMAP((start + reserved_size - mapped_size), mapped_size, PROT_READ | PROT_WRITE,
@@ -103,12 +101,12 @@ static void *allocate_stack(size_t stack_size, size_t mapped_size)
 	tst_res(TINFO, "start = %p, stack_top = %p, stack bottom = %p",
 		start, stack_top, stack_bottom);
 	tst_res(TINFO, "mapped pages %zu, stack pages %zu",
-	        mapped_size/page_size, stack_size/page_size);
+		mapped_size/page_size, stack_size/page_size);
 
 	return stack_bottom;
 }
 
-static __attribute__((noinline)) void *check_depth_recursive(void *limit)
+static __attribute_noinline__ void *check_depth_recursive(void *limit)
 {
 	if ((off_t) &limit < (off_t) limit) {
 		tst_res(TINFO, "&limit = %p, limit = %p", &limit, limit);
@@ -192,10 +190,10 @@ static void grow_stack_fail(size_t stack_size, size_t mapped_size)
 	}
 
 	SAFE_WAIT(&wstatus);
-        if (WIFSIGNALED(wstatus) && WTERMSIG(wstatus) == SIGSEGV)
+	if (WIFSIGNALED(wstatus) && WTERMSIG(wstatus) == SIGSEGV)
 		tst_res(TPASS, "Child killed by %s as expected", tst_strsig(SIGSEGV));
-        else
-                tst_res(TFAIL, "Child: %s", tst_strstatus(wstatus));
+	else
+		tst_res(TFAIL, "Child: %s", tst_strstatus(wstatus));
 }
 
 static void run_test(void)

-- 
2.43.0


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

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

* [LTP] [PATCH v2 6/8] Cleanup mmap19 test
  2025-02-10  9:07 [LTP] [PATCH v2 0/8] Cleanup the mmap testing suite Andrea Cervesato
                   ` (4 preceding siblings ...)
  2025-02-10  9:07 ` [LTP] [PATCH v2 5/8] Cleanup mmap18 test Andrea Cervesato
@ 2025-02-10  9:07 ` Andrea Cervesato
  2025-02-25 15:21   ` Cyril Hrubis
  2025-02-10  9:07 ` [LTP] [PATCH v2 7/8] Cleanup mmap20 test Andrea Cervesato
  2025-02-10  9:07 ` [LTP] [PATCH v2 8/8] Refactor mmap001 test and move it to mmap21 Andrea Cervesato
  7 siblings, 1 reply; 19+ messages in thread
From: Andrea Cervesato @ 2025-02-10  9:07 UTC (permalink / raw)
  To: ltp; +Cc: rbm

From: Andrea Cervesato <andrea.cervesato@suse.com>

Signed-off-by: Andrea Cervesato <andrea.cervesato@suse.com>
---
 testcases/kernel/syscalls/mmap/mmap19.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/testcases/kernel/syscalls/mmap/mmap19.c b/testcases/kernel/syscalls/mmap/mmap19.c
index 90b3f45b1f6368ad287125db1e2b2e15fffff601..23cb2be62ced846b151d2578acf76db7de527b25 100644
--- a/testcases/kernel/syscalls/mmap/mmap19.c
+++ b/testcases/kernel/syscalls/mmap/mmap19.c
@@ -24,7 +24,7 @@
 #define LEN 64
 
 static int f1 = -1, f2 = -1;
-static char *mm1 = NULL, *mm2 = NULL;
+static char *mm1, *mm2;
 
 static const char tmp1[] = "testfile1";
 static const char tmp2[] = "testfile2";

-- 
2.43.0


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

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

* [LTP] [PATCH v2 7/8] Cleanup mmap20 test
  2025-02-10  9:07 [LTP] [PATCH v2 0/8] Cleanup the mmap testing suite Andrea Cervesato
                   ` (5 preceding siblings ...)
  2025-02-10  9:07 ` [LTP] [PATCH v2 6/8] Cleanup mmap19 test Andrea Cervesato
@ 2025-02-10  9:07 ` Andrea Cervesato
  2025-02-25 15:21   ` Cyril Hrubis
  2025-02-10  9:07 ` [LTP] [PATCH v2 8/8] Refactor mmap001 test and move it to mmap21 Andrea Cervesato
  7 siblings, 1 reply; 19+ messages in thread
From: Andrea Cervesato @ 2025-02-10  9:07 UTC (permalink / raw)
  To: ltp; +Cc: rbm

From: Andrea Cervesato <andrea.cervesato@suse.com>

Signed-off-by: Andrea Cervesato <andrea.cervesato@suse.com>
---
 testcases/kernel/syscalls/mmap/mmap20.c | 3 ---
 1 file changed, 3 deletions(-)

diff --git a/testcases/kernel/syscalls/mmap/mmap20.c b/testcases/kernel/syscalls/mmap/mmap20.c
index 02d150e45759a719f396ff941e200313f9a6b4c5..c1fd3d91027d5dd976e646a68c26456c275faf67 100644
--- a/testcases/kernel/syscalls/mmap/mmap20.c
+++ b/testcases/kernel/syscalls/mmap/mmap20.c
@@ -12,9 +12,6 @@
  * flag and invalid flag.
  */
 
-#include <errno.h>
-#include <stdio.h>
-#include <sys/types.h>
 #include "tst_test.h"
 #include "lapi/mmap.h"
 

-- 
2.43.0


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

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

* [LTP] [PATCH v2 8/8] Refactor mmap001 test and move it to mmap21
  2025-02-10  9:07 [LTP] [PATCH v2 0/8] Cleanup the mmap testing suite Andrea Cervesato
                   ` (6 preceding siblings ...)
  2025-02-10  9:07 ` [LTP] [PATCH v2 7/8] Cleanup mmap20 test Andrea Cervesato
@ 2025-02-10  9:07 ` Andrea Cervesato
  2025-02-25 15:51   ` Cyril Hrubis
  7 siblings, 1 reply; 19+ messages in thread
From: Andrea Cervesato @ 2025-02-10  9:07 UTC (permalink / raw)
  To: ltp; +Cc: rbm

From: Andrea Cervesato <andrea.cervesato@suse.com>

Signed-off-by: Andrea Cervesato <andrea.cervesato@suse.com>
---
 runtest/mm                                |   8 +-
 runtest/syscalls                          |   2 +-
 testcases/kernel/syscalls/mmap/.gitignore |   2 +-
 testcases/kernel/syscalls/mmap/mmap001.c  | 183 ------------------------------
 testcases/kernel/syscalls/mmap/mmap21.c   | 108 ++++++++++++++++++
 5 files changed, 112 insertions(+), 191 deletions(-)

diff --git a/runtest/mm b/runtest/mm
index d8e62af81fcbf9381a5419ab713139774d081c44..fd64d9a82f65ab1afc886ac1ae86955f48ef5031 100644
--- a/runtest/mm
+++ b/runtest/mm
@@ -1,13 +1,9 @@
 #DESCRIPTION:Memory Mgmt tests
-mm01 mmap001 -m 10000
+mm01 mmap021 -m 10000
 # 40 Mb mmap() test.
 # Creates a 10000 page mmap, touches all of the map, sync's it, and
 # munmap()s it.
-mm02 mmap001
-# simple mmap() test.
-#mm03 mmap001 -i 0 -I 1 -m 100
-# repetitive mmapping test.
-# Creates a one page map repetitively for one minute.
+mm02 mmap21
 
 mtest01 mtest01 -p80
 mtest01w mtest01 -p80 -w
diff --git a/runtest/syscalls b/runtest/syscalls
index 4ab8436d30ca5ffee52d9777729ec1ec09d0bf1d..6aaef356fd617bac5617a3a652c66016892b07eb 100644
--- a/runtest/syscalls
+++ b/runtest/syscalls
@@ -822,7 +822,7 @@ mlock201 mlock201
 mlock202 mlock202
 mlock203 mlock203
 
-qmm01 mmap001 -m 1
+qmm01 mmap21 -m 1
 mmap01 mmap01
 mmap02 mmap02
 mmap03 mmap03
diff --git a/testcases/kernel/syscalls/mmap/.gitignore b/testcases/kernel/syscalls/mmap/.gitignore
index 4591fdbb9b71d5edb534c3d99f1a66e0e42ce6b6..850284d86616e29674df89b8107a5939c25723da 100644
--- a/testcases/kernel/syscalls/mmap/.gitignore
+++ b/testcases/kernel/syscalls/mmap/.gitignore
@@ -1,4 +1,3 @@
-/mmap001
 /mmap01
 /mmap02
 /mmap03
@@ -18,3 +17,4 @@
 /mmap18
 /mmap19
 /mmap20
+/mmap21
diff --git a/testcases/kernel/syscalls/mmap/mmap001.c b/testcases/kernel/syscalls/mmap/mmap001.c
deleted file mode 100644
index dabb7d1e4998b1097e179abe23555926f5841117..0000000000000000000000000000000000000000
--- a/testcases/kernel/syscalls/mmap/mmap001.c
+++ /dev/null
@@ -1,183 +0,0 @@
-/*
- * Copyright (C) 2000 Juan Quintela <quintela@fi.udc.es>
- *                    Aaron Laffin <alaffin@sgi.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., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
- *
- * mmap001.c - Tests mmapping a big file and writing it once
- */
-#include <sys/types.h>
-#include <sys/stat.h>
-#include <fcntl.h>
-#include <sys/mman.h>
-#include <stdlib.h>
-#include <stdio.h>
-#include <unistd.h>
-#include <errno.h>
-#include <string.h>
-
-#include "test.h"
-
-char *TCID = "mmap001";
-int TST_TOTAL = 5;
-static char *filename = NULL;
-static int m_opt = 0;
-static char *m_copt;
-
-static void cleanup(void)
-{
-	free(filename);
-
-	tst_rmdir();
-}
-
-static void setup(void)
-{
-	char buf[1024];
-	/*
-	 * setup a default signal hander and a
-	 * temporary working directory.
-	 */
-	tst_sig(FORK, DEF_HANDLER, cleanup);
-
-	TEST_PAUSE;
-
-	tst_tmpdir();
-
-	snprintf(buf, 1024, "testfile.%d", getpid());
-
-	if ((filename = strdup(buf)) == NULL) {
-		tst_brkm(TBROK | TERRNO, cleanup, "strdup failed");
-	}
-
-}
-
-static void help(void)
-{
-	printf("  -m x    size of mmap in pages (default 1000)\n");
-}
-
-/*
- * add the -m option whose parameter is the
- * pages that should be mapped.
- */
-option_t options[] = {
-	{"m:", &m_opt, &m_copt},
-	{NULL, NULL, NULL}
-};
-
-int main(int argc, char *argv[])
-{
-	char *array;
-	int lc;
-	unsigned int i;
-	int fd;
-	unsigned int pages, memsize;
-
-	tst_parse_opts(argc, argv, options, help);
-
-	if (m_opt) {
-		memsize = pages = atoi(m_copt);
-
-		if (memsize < 1) {
-			tst_brkm(TBROK, cleanup, "Invalid arg for -m: %s",
-				 m_copt);
-		}
-
-		memsize *= getpagesize();	/* N PAGES */
-
-	} else {
-		/*
-		 * default size 1000 pages;
-		 */
-		memsize = pages = 1000;
-		memsize *= getpagesize();
-	}
-
-	tst_resm(TINFO, "mmap()ing file of %u pages or %u bytes", pages,
-		 memsize);
-
-	setup();
-
-	for (lc = 0; TEST_LOOPING(lc); lc++) {
-		tst_count = 0;
-
-		fd = open(filename, O_RDWR | O_CREAT, 0666);
-		if ((fd == -1))
-			tst_brkm(TBROK | TERRNO, cleanup,
-				 "opening %s failed", filename);
-
-		if (lseek(fd, memsize, SEEK_SET) != memsize) {
-			TEST_ERRNO = errno;
-			close(fd);
-			tst_brkm(TBROK | TTERRNO, cleanup, "lseek failed");
-		}
-
-		if (write(fd, "\0", 1) != 1) {
-			TEST_ERRNO = errno;
-			close(fd);
-			tst_brkm(TBROK | TTERRNO, cleanup,
-				 "writing to %s failed", filename);
-		}
-
-		array = mmap(0, memsize, PROT_WRITE, MAP_SHARED, fd, 0);
-		if (array == MAP_FAILED) {
-			TEST_ERRNO = errno;
-			close(fd);
-			tst_brkm(TBROK | TTERRNO, cleanup,
-				 "mmapping %s failed", filename);
-		} else {
-			tst_resm(TPASS, "mmap() completed successfully.");
-		}
-
-		tst_resm(TINFO, "touching mmaped memory");
-
-		for (i = 0; i < memsize; i++) {
-			array[i] = (char)i;
-		}
-
-		/*
-		 * seems that if the map area was bad, we'd get SEGV,
-		 * hence we can indicate a PASS.
-		 */
-		tst_resm(TPASS,
-			 "we're still here, mmaped area must be good");
-
-		TEST(msync(array, memsize, MS_SYNC));
-
-		if (TEST_RETURN == -1) {
-			tst_resm(TFAIL | TTERRNO,
-				 "synchronizing mmapped page failed");
-		} else {
-			tst_resm(TPASS,
-				 "synchronizing mmapped page passed");
-		}
-
-		TEST(munmap(array, memsize));
-
-		if (TEST_RETURN == -1) {
-			tst_resm(TFAIL | TTERRNO,
-				 "munmapping %s failed", filename);
-		} else {
-			tst_resm(TPASS, "munmapping %s successful", filename);
-		}
-
-		close(fd);
-		unlink(filename);
-
-	}
-	cleanup();
-	tst_exit();
-}
diff --git a/testcases/kernel/syscalls/mmap/mmap21.c b/testcases/kernel/syscalls/mmap/mmap21.c
new file mode 100644
index 0000000000000000000000000000000000000000..895f86d36afe32082205f0b391bfa19070170d87
--- /dev/null
+++ b/testcases/kernel/syscalls/mmap/mmap21.c
@@ -0,0 +1,108 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * Copyright (C) 2000 Juan Quintela <quintela@fi.udc.es>
+ *                    Aaron Laffin <alaffin@sgi.com>
+ * Copyright (C) 2025 SUSE LLC Andrea Cervesato <andrea.cervesato@suse.com>
+ */
+
+/*\
+ * [Description]
+ *
+ * Verify that we can use mmap() to map a large file, write to it via memory
+ * access, and read back the data from the file. This test also verifies that
+ * accessing memory won't end up killing the test with SIGSEGV.
+ */
+
+#include "tst_test.h"
+
+static char filename[1024];
+static char *str_pages;
+static long long pages = 1000;
+static long long memory_size;
+static char *memory_data;
+
+static void run(void)
+{
+	int fd;
+	pid_t pid;
+	int status;
+	char *buff = NULL;
+
+	tst_res(TINFO, "mmap()ing file of %llu bytes", memory_size);
+
+	fd = SAFE_OPEN(filename, O_RDWR | O_CREAT, 0666);
+	SAFE_LSEEK(fd, memory_size, SEEK_SET);
+	SAFE_WRITE(SAFE_WRITE_ALL, fd, "\0", 1);
+
+	memory_data = SAFE_MMAP(0, memory_size, PROT_WRITE, MAP_SHARED, fd, 0);
+
+	pid = SAFE_FORK();
+	if (!pid) {
+		tst_res(TINFO, "Touching mapped memory");
+
+		for (int i = 0; i < memory_size; i++)
+			memory_data[i] = (char)i;
+
+		exit(0);
+	}
+
+	SAFE_WAITPID(pid, &status, 0);
+
+	if (WIFSIGNALED(status) && WTERMSIG(status) == SIGSEGV) {
+		tst_res(TFAIL, "Touching memory caused SIGSEGV");
+		goto exit;
+	}
+
+	SAFE_MSYNC(memory_data, memory_size, MS_SYNC);
+
+	buff = SAFE_MALLOC(memory_size);
+	memset(buff, 0, memory_size);
+
+	SAFE_LSEEK(fd, 0, SEEK_SET);
+	SAFE_READ(0, fd, buff, memory_size);
+	SAFE_CLOSE(fd);
+
+	for (int i = 0; i < memory_size; i++) {
+		if (buff[i] != (char)i) {
+			tst_res(TFAIL, "Mapped file has not been updated at byte %d", i);
+			goto exit;
+		}
+	}
+
+	tst_res(TPASS, "Mapped file has been updated");
+
+exit:
+	if (buff)
+		free(buff);
+
+	SAFE_MUNMAP(memory_data, memory_size);
+	SAFE_UNLINK(filename);
+}
+
+static void setup(void)
+{
+	if (tst_parse_filesize(str_pages, &pages, 1, LLONG_MAX))
+		tst_brk(TBROK, "Invalid number of pages: %s", str_pages);
+
+	memory_size = pages * getpagesize();
+
+	snprintf(filename, 1024, "testfile.%d", getpid());
+}
+
+static void cleanup(void)
+{
+	if (memory_data)
+		SAFE_MUNMAP(memory_data, memory_size);
+}
+
+static struct tst_test test = {
+	.test_all = run,
+	.setup = setup,
+	.cleanup = cleanup,
+	.needs_tmpdir = 1,
+	.forks_child = 1,
+	.options = (struct tst_option[]) {
+		{"m:", &str_pages, "Number of pages (default 1000)"},
+		{}
+	},
+};

-- 
2.43.0


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

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

* Re: [LTP] [PATCH v2 1/8] Refactor mmap03 test
  2025-02-10  9:07 ` [LTP] [PATCH v2 1/8] Refactor mmap03 test Andrea Cervesato
@ 2025-02-10  9:41   ` Petr Vorel
  2025-02-10 11:46     ` Ricardo B. Marlière
  2025-02-24 16:56     ` Cyril Hrubis
  0 siblings, 2 replies; 19+ messages in thread
From: Petr Vorel @ 2025-02-10  9:41 UTC (permalink / raw)
  To: Andrea Cervesato; +Cc: rbm, ltp

Hi Andrea, all,

...
> - *	07/2001 Ported by Wayne Boyer
> + * With ia64 and PARISC/hppa, an attempt to access the contents of the
> + * mapped region should rise signal SIGSEGV.

You kept this:
 * With ia64 and PARISC/hppa, an attempt to access the contents of the
 * mapped region should rise signal SIGSEGV.

You kept checking SIGSEGV, but not sigsetjmp() which should cause it (IMHO it
will not work).

I wonder if we should care about these old archs. Both ia64 and hppa are
dropped. The only still living arch is __mips__. But looking at change when it
mips was added it's quite recent - 2021:
c7f979b3d ("syscalls/mmap03: Fix SegFault on MIPS")

@Cyril? Should we keep only mips? And I strongly suspect nobody is testing LTP
on mips, but we should probably not kill a still living arch.
If all these archs removed whole fork could be removed.

...
> +	tst_res(TINFO, "Map temporary file in memory with PROT_EXEC");
nit: I would remove it. In case of the problem SAFE_MMAP() will notify,
otherwise not important.

Also, can we move SAFE_MALLOC() to setup and free to cleanup()

data = SAFE_MALLOC(page_sz);

Kind regards,
Petr

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

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

* Re: [LTP] [PATCH v2 1/8] Refactor mmap03 test
  2025-02-10  9:41   ` Petr Vorel
@ 2025-02-10 11:46     ` Ricardo B. Marlière
  2025-02-24 16:56     ` Cyril Hrubis
  1 sibling, 0 replies; 19+ messages in thread
From: Ricardo B. Marlière @ 2025-02-10 11:46 UTC (permalink / raw)
  To: Petr Vorel, Andrea Cervesato; +Cc: ltp

On Mon Feb 10, 2025 at 6:41 AM -03, Petr Vorel wrote:
> Hi Andrea, all,
>
> ...
>> - *	07/2001 Ported by Wayne Boyer
>> + * With ia64 and PARISC/hppa, an attempt to access the contents of the
>> + * mapped region should rise signal SIGSEGV.
>
> You kept this:
>  * With ia64 and PARISC/hppa, an attempt to access the contents of the
>  * mapped region should rise signal SIGSEGV.
>
> You kept checking SIGSEGV, but not sigsetjmp() which should cause it (IMHO it
> will not work).

In my initial testing [1], I hit a situation where SIGSEGV was raised in
my Tumbleweed SUT. I eventually found out that it was due to
CONFIG_X86_INTEL_MEMORY_PROTECTION_KEYS=y in openSUSE kernels.

[1]: https://lore.kernel.org/ltp/20250114-conversions-v1-2-7869a9f786d0@suse.com/

I was playing with the following diff here but was unable to catch any
signal in the current version:

diff --git a/testcases/kernel/syscalls/mmap/mmap03.c b/testcases/kernel/syscalls/mmap/mmap03.c
index f22365b259da..25f9c5ce4d18 100644
--- a/testcases/kernel/syscalls/mmap/mmap03.c
+++ b/testcases/kernel/syscalls/mmap/mmap03.c
@@ -22,6 +22,7 @@
  * mapped region should rise signal SIGSEGV.
  */
 
+#include "tst_kconfig.h"
 #include "tst_test.h"
 
 #define TEMPFILE "mmapfile"
@@ -67,7 +68,14 @@ static void run(void)
 
 	SAFE_WAITPID(pid, &status, 0);
 
-#if defined(__ia64__) || defined(__hppa__) || defined(__mips__)
+#if defined(__ia64__) || defined(__hppa__) || defined(__mips__) || defined(__x86_64__)
+	struct tst_kconfig_var kconfig =
+		TST_KCONFIG_INIT("CONFIG_X86_INTEL_MEMORY_PROTECTION_KEYS");
+	tst_kconfig_read(&kconfig, 1);
+	if (kconfig.choice == 'y')
+		tst_res(TINFO, "CONFIG_X86_INTEL_MEMORY_PROTECTION_KEYS=y");
+
+	tst_res(TINFO, "%d == 11 (SEGV)?", WTERMSIG(status));
 	if (WIFSIGNALED(status) && WTERMSIG(status) == SIGSEGV) {
 		tst_res(TPASS, "SIGSEGV has been received");
 	} else {


BTW, thanks for the mmap cleanup, Andrea!

>
> I wonder if we should care about these old archs. Both ia64 and hppa are
> dropped. The only still living arch is __mips__. But looking at change when it
> mips was added it's quite recent - 2021:
> c7f979b3d ("syscalls/mmap03: Fix SegFault on MIPS")

TIL, thanks :)

-    Ricardo.



>
> @Cyril? Should we keep only mips? And I strongly suspect nobody is testing LTP
> on mips, but we should probably not kill a still living arch.
> If all these archs removed whole fork could be removed.
>
> ...
>> +	tst_res(TINFO, "Map temporary file in memory with PROT_EXEC");
> nit: I would remove it. In case of the problem SAFE_MMAP() will notify,
> otherwise not important.
>
> Also, can we move SAFE_MALLOC() to setup and free to cleanup()
>
> data = SAFE_MALLOC(page_sz);
>
> Kind regards,
> Petr


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

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

* Re: [LTP] [PATCH v2 1/8] Refactor mmap03 test
  2025-02-10  9:41   ` Petr Vorel
  2025-02-10 11:46     ` Ricardo B. Marlière
@ 2025-02-24 16:56     ` Cyril Hrubis
  1 sibling, 0 replies; 19+ messages in thread
From: Cyril Hrubis @ 2025-02-24 16:56 UTC (permalink / raw)
  To: Petr Vorel; +Cc: ltp, rbm

Hi!
> @Cyril? Should we keep only mips? And I strongly suspect nobody is testing LTP
> on mips, but we should probably not kill a still living arch.
> If all these archs removed whole fork could be removed.

The fact that memory mapped with PROT_EXEC is readable is an
implementation detail for an architecture and not guaranteed. So the
best result is either to expect that the child returns with 0 or dies
with SIGSEGV, either one is correct behavior.

-- 
Cyril Hrubis
chrubis@suse.cz

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

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

* Re: [LTP] [PATCH v2 2/8] Refactor mmap10 test
  2025-02-10  9:07 ` [LTP] [PATCH v2 2/8] Refactor mmap10 test Andrea Cervesato
@ 2025-02-24 16:57   ` Cyril Hrubis
  0 siblings, 0 replies; 19+ messages in thread
From: Cyril Hrubis @ 2025-02-24 16:57 UTC (permalink / raw)
  To: Andrea Cervesato; +Cc: rbm, ltp

Hi!
I've missed the v2 and commented on v1 but the points I raised there
stil apply.

-- 
Cyril Hrubis
chrubis@suse.cz

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

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

* Re: [LTP] [PATCH v2 3/8] Cleanup mmap12 test
  2025-02-10  9:07 ` [LTP] [PATCH v2 3/8] Cleanup mmap12 test Andrea Cervesato
@ 2025-02-25 10:17   ` Cyril Hrubis
  0 siblings, 0 replies; 19+ messages in thread
From: Cyril Hrubis @ 2025-02-25 10:17 UTC (permalink / raw)
  To: Andrea Cervesato; +Cc: rbm, ltp

Hi!
> +/*\
> + * [Description]

This shouldn't be there anymore.

Otherwise it looks fine:

Reviewed-by: Cyril Hrubis <chrubis@suse.cz>

-- 
Cyril Hrubis
chrubis@suse.cz

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

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

* Re: [LTP] [PATCH v2 4/8] Cleanup mmap17 test
  2025-02-10  9:07 ` [LTP] [PATCH v2 4/8] Cleanup mmap17 test Andrea Cervesato
@ 2025-02-25 15:02   ` Cyril Hrubis
  0 siblings, 0 replies; 19+ messages in thread
From: Cyril Hrubis @ 2025-02-25 15:02 UTC (permalink / raw)
  To: Andrea Cervesato; +Cc: rbm, ltp

Hi!
> -/*
> - * Test mmap with MAP_FIXED_NOREPLACE flag
> +/*\
> + * [Description]

This shouldn't be there I now.

> - * We are testing the MAP_FIXED_NOREPLACE flag of mmap() syscall. To check
> + * Verify MAP_FIXED_NOREPLACE flag for the mmap() syscall and check
>   * if an attempt to mmap at an exisiting mapping fails with EEXIST.

This should be really just:

Verify that MAP_FIXED_NOREPLACE does not replace existing mapping and
fails with EEXIST.

> + * [Algorithm]
> + *
>   * The code allocates a free address by passing NULL to first mmap call
> - * Then tries to mmap with the same address using MAP_FIXED_NOREPLACE flag
> + * then tries to mmap with the same address using MAP_FIXED_NOREPLACE flag
>   * and the mapping fails as expected.

Do we really need that part? I think that the test itself is so simple
that the single sentence above describes it completely.

>   */
>  
> -#include <stdio.h>
> -#include <fcntl.h>
> -#include <sys/types.h>
> -#include <sys/stat.h>
> -#include <unistd.h>
> -#include <errno.h>
> -#include <string.h>
> -#include <stdlib.h>
> -#include "lapi/mmap.h"
>  #include "tst_test.h"
>  
>  static int fd_file1;

And since you are touching the test we do not free the mapped address in
the case of -i parameter. So need to move the SAFE_MUNMAP() to the test
setup().

-- 
Cyril Hrubis
chrubis@suse.cz

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

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

* Re: [LTP] [PATCH v2 5/8] Cleanup mmap18 test
  2025-02-10  9:07 ` [LTP] [PATCH v2 5/8] Cleanup mmap18 test Andrea Cervesato
@ 2025-02-25 15:20   ` Cyril Hrubis
  0 siblings, 0 replies; 19+ messages in thread
From: Cyril Hrubis @ 2025-02-25 15:20 UTC (permalink / raw)
  To: Andrea Cervesato; +Cc: rbm, ltp

Hi!
> +/*\
> + * [Description]
> + *
> + * Verify mmap() syscall using MAP_GROWSDOWN flag.
>   *
> - * # Test1:
> + * [Algorithm]
>   *
> - *   We assign the memory region partially allocated with MAP_GROWSDOWN flag to
> - *   a thread as a stack and expect the mapping to grow when we touch the
> - *   guard page by calling a recusive function in the thread that uses the
> - *   growable mapping as a stack.
> + * **Test 1**
>   *
> - *   The kernel only grows the memory region when the stack pointer is within
> - *   guard page when the guard page is touched so simply faulting the guard
> - *   page will not cause the mapping to grow.
> + * We assign the memory region partially allocated with MAP_GROWSDOWN flag to
> + * a thread as a stack and expect the mapping to grow when we touch the
> + * guard page by calling a recusive function in the thread that uses the
> + * growable mapping as a stack.
>   *
> - *   Newer kernels does not allow a MAP_GROWSDOWN mapping to grow closer than
> - *   'stack_guard_gap' pages to an existing mapping. So when we map the stack we
> - *   make sure there is enough of free address space before the lowest stack
> - *   address.
> + * The kernel only grows the memory region when the stack pointer is within
> + * guard page when the guard page is touched so simply faulting the guard
> + * page will not cause the mapping to grow.
>   *
> - *   Kernel default 'stack_guard_gap' size is '256 * getpagesize()'.
> + * Newer kernels does not allow a MAP_GROWSDOWN mapping to grow closer than
> + * 'stack_guard_gap' pages to an existing mapping. So when we map the stack we
> + * make sure there is enough of free address space before the lowest stack
> + * address.
>   *
> - *   The stack memory map would look like:
> + * Kernel default `stack_guard_gap` size is `256 * getpagesize()`.
>   *
> - *   |  -  -  -   reserved  size   -  -  -  |
> + * The stack memory map would look like:
>   *
> - *   +-- - - - --+------------+-------------+
> - *   | 256 pages |  unmapped  |   mapped    |
> - *   +-- - - - --+------------+-------------+
> - *                            | mapped size |
> - *   ^           |  -  -  stack size  -  -  |
> - *   start
> - *               ^                          ^
> - *               stack bottom       stack top
> + * |  -  -  -   reserved  size   -  -  -  |
>   *
> - * # Test2:
> + * +-- - - - --+------------+-------------+
> + * | 256 pages |  unmapped  |   mapped    |
> + * +-- - - - --+------------+-------------+
> + *                          | mapped size |
> + * ^           |  -  -  stack size  -  -  |
> + * start
> + *             ^                          ^
> + *             stack bottom       stack top
>   *
> - *   We allocate stack as we do in the first test but we mmap a page in the
> - *   space the stack is supposed to grow into and we expect the thread to
> - *   segfault when the guard page is faulted.
> + * **Test 2**
> + *
> + * We allocate stack as we do in the first test but we mmap a page in the
> + * space the stack is supposed to grow into and we expect the thread to
> + * segfault when the guard page is faulted.
>   */
>  
> -#include <unistd.h>
>  #include <pthread.h>
> -#include <sys/mman.h>
> -#include <sys/wait.h>
> -#include <sys/types.h>
> -#include <stdlib.h>
> -#include <stdbool.h>
> -
>  #include "tst_test.h"
>  #include "tst_safe_pthread.h"
>  
>  static long page_size;
>  
> -static bool __attribute__((noinline)) check_stackgrow_up(void)
> +static bool __attribute_noinline__ check_stackgrow_up(void)

This does not look correct, __attribute_noinline__ is glibc macro, the
code probably does not compile on other libc implementations after this
change.

>  {
>  	char local_var;
>  	static char *addr;
>  
> -       if (!addr) {
> -               addr = &local_var;
> -               return check_stackgrow_up();
> -       }
> +	if (!addr) {
> +		addr = &local_var;
> +		return check_stackgrow_up();
> +	}
>  
> -       return (addr < &local_var);
> +	return (addr < &local_var);
>  }
>  
>  static void setup(void)
> @@ -90,7 +88,7 @@ static void *allocate_stack(size_t stack_size, size_t mapped_size)
>  	long reserved_size = 256 * page_size + stack_size;
>  
>  	start = SAFE_MMAP(NULL, reserved_size, PROT_READ | PROT_WRITE,
> -	                  MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
> +			MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
>  	SAFE_MUNMAP(start, reserved_size);
>  
>  	SAFE_MMAP((start + reserved_size - mapped_size), mapped_size, PROT_READ | PROT_WRITE,
> @@ -103,12 +101,12 @@ static void *allocate_stack(size_t stack_size, size_t mapped_size)
>  	tst_res(TINFO, "start = %p, stack_top = %p, stack bottom = %p",
>  		start, stack_top, stack_bottom);
>  	tst_res(TINFO, "mapped pages %zu, stack pages %zu",
> -	        mapped_size/page_size, stack_size/page_size);
> +		mapped_size/page_size, stack_size/page_size);
>  
>  	return stack_bottom;
>  }
>  
> -static __attribute__((noinline)) void *check_depth_recursive(void *limit)
> +static __attribute_noinline__ void *check_depth_recursive(void *limit)
>  {
>  	if ((off_t) &limit < (off_t) limit) {
>  		tst_res(TINFO, "&limit = %p, limit = %p", &limit, limit);
> @@ -192,10 +190,10 @@ static void grow_stack_fail(size_t stack_size, size_t mapped_size)
>  	}
>  
>  	SAFE_WAIT(&wstatus);
> -        if (WIFSIGNALED(wstatus) && WTERMSIG(wstatus) == SIGSEGV)
> +	if (WIFSIGNALED(wstatus) && WTERMSIG(wstatus) == SIGSEGV)
>  		tst_res(TPASS, "Child killed by %s as expected", tst_strsig(SIGSEGV));
> -        else
> -                tst_res(TFAIL, "Child: %s", tst_strstatus(wstatus));
> +	else
> +		tst_res(TFAIL, "Child: %s", tst_strstatus(wstatus));
>  }
>  
>  static void run_test(void)
> 
> -- 
> 2.43.0
> 
> 
> -- 
> Mailing list info: https://lists.linux.it/listinfo/ltp

-- 
Cyril Hrubis
chrubis@suse.cz

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

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

* Re: [LTP] [PATCH v2 6/8] Cleanup mmap19 test
  2025-02-10  9:07 ` [LTP] [PATCH v2 6/8] Cleanup mmap19 test Andrea Cervesato
@ 2025-02-25 15:21   ` Cyril Hrubis
  0 siblings, 0 replies; 19+ messages in thread
From: Cyril Hrubis @ 2025-02-25 15:21 UTC (permalink / raw)
  To: Andrea Cervesato; +Cc: rbm, ltp

Hi!
Obviously correct.

Reviewed-by: Cyril Hrubis <chrubis@suse.cz>

-- 
Cyril Hrubis
chrubis@suse.cz

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

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

* Re: [LTP] [PATCH v2 7/8] Cleanup mmap20 test
  2025-02-10  9:07 ` [LTP] [PATCH v2 7/8] Cleanup mmap20 test Andrea Cervesato
@ 2025-02-25 15:21   ` Cyril Hrubis
  0 siblings, 0 replies; 19+ messages in thread
From: Cyril Hrubis @ 2025-02-25 15:21 UTC (permalink / raw)
  To: Andrea Cervesato; +Cc: rbm, ltp

Hi!
Reviewed-by: Cyril Hrubis <chrubis@suse.cz>

-- 
Cyril Hrubis
chrubis@suse.cz

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

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

* Re: [LTP] [PATCH v2 8/8] Refactor mmap001 test and move it to mmap21
  2025-02-10  9:07 ` [LTP] [PATCH v2 8/8] Refactor mmap001 test and move it to mmap21 Andrea Cervesato
@ 2025-02-25 15:51   ` Cyril Hrubis
  0 siblings, 0 replies; 19+ messages in thread
From: Cyril Hrubis @ 2025-02-25 15:51 UTC (permalink / raw)
  To: Andrea Cervesato; +Cc: rbm, ltp

Hi!
> +mm01 mmap021 -m 10000

If we are renaming the test we may as well rename the tag.

And you have a typo there it's mmap21 not mmap021

>  # 40 Mb mmap() test.
>  # Creates a 10000 page mmap, touches all of the map, sync's it, and
>  # munmap()s it.
> -mm02 mmap001
> -# simple mmap() test.
> -#mm03 mmap001 -i 0 -I 1 -m 100
> -# repetitive mmapping test.
> -# Creates a one page map repetitively for one minute.
> +mm02 mmap21

Here as well. I would propose to change the tag to mmap21_02 here and to
mmap21_01 above.

>  mtest01 mtest01 -p80
>  mtest01w mtest01 -p80 -w
> diff --git a/runtest/syscalls b/runtest/syscalls
> index 4ab8436d30ca5ffee52d9777729ec1ec09d0bf1d..6aaef356fd617bac5617a3a652c66016892b07eb 100644
> --- a/runtest/syscalls
> +++ b/runtest/syscalls
> @@ -822,7 +822,7 @@ mlock201 mlock201
>  mlock202 mlock202
>  mlock203 mlock203
>  
> -qmm01 mmap001 -m 1
> +qmm01 mmap21 -m 1

Here as well.

>  mmap01 mmap01
>  mmap02 mmap02
>  mmap03 mmap03
> diff --git a/testcases/kernel/syscalls/mmap/.gitignore b/testcases/kernel/syscalls/mmap/.gitignore
> index 4591fdbb9b71d5edb534c3d99f1a66e0e42ce6b6..850284d86616e29674df89b8107a5939c25723da 100644
> --- a/testcases/kernel/syscalls/mmap/.gitignore
> +++ b/testcases/kernel/syscalls/mmap/.gitignore
> @@ -1,4 +1,3 @@
> -/mmap001
>  /mmap01
>  /mmap02
>  /mmap03
> @@ -18,3 +17,4 @@
>  /mmap18
>  /mmap19
>  /mmap20
> +/mmap21
> diff --git a/testcases/kernel/syscalls/mmap/mmap001.c b/testcases/kernel/syscalls/mmap/mmap001.c
> deleted file mode 100644
> index dabb7d1e4998b1097e179abe23555926f5841117..0000000000000000000000000000000000000000
> --- a/testcases/kernel/syscalls/mmap/mmap001.c
> +++ /dev/null
> @@ -1,183 +0,0 @@
> -/*
> - * Copyright (C) 2000 Juan Quintela <quintela@fi.udc.es>
> - *                    Aaron Laffin <alaffin@sgi.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., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
> - *
> - * mmap001.c - Tests mmapping a big file and writing it once
> - */
> -#include <sys/types.h>
> -#include <sys/stat.h>
> -#include <fcntl.h>
> -#include <sys/mman.h>
> -#include <stdlib.h>
> -#include <stdio.h>
> -#include <unistd.h>
> -#include <errno.h>
> -#include <string.h>
> -
> -#include "test.h"
> -
> -char *TCID = "mmap001";
> -int TST_TOTAL = 5;
> -static char *filename = NULL;
> -static int m_opt = 0;
> -static char *m_copt;
> -
> -static void cleanup(void)
> -{
> -	free(filename);
> -
> -	tst_rmdir();
> -}
> -
> -static void setup(void)
> -{
> -	char buf[1024];
> -	/*
> -	 * setup a default signal hander and a
> -	 * temporary working directory.
> -	 */
> -	tst_sig(FORK, DEF_HANDLER, cleanup);
> -
> -	TEST_PAUSE;
> -
> -	tst_tmpdir();
> -
> -	snprintf(buf, 1024, "testfile.%d", getpid());
> -
> -	if ((filename = strdup(buf)) == NULL) {
> -		tst_brkm(TBROK | TERRNO, cleanup, "strdup failed");
> -	}
> -
> -}
> -
> -static void help(void)
> -{
> -	printf("  -m x    size of mmap in pages (default 1000)\n");
> -}
> -
> -/*
> - * add the -m option whose parameter is the
> - * pages that should be mapped.
> - */
> -option_t options[] = {
> -	{"m:", &m_opt, &m_copt},
> -	{NULL, NULL, NULL}
> -};
> -
> -int main(int argc, char *argv[])
> -{
> -	char *array;
> -	int lc;
> -	unsigned int i;
> -	int fd;
> -	unsigned int pages, memsize;
> -
> -	tst_parse_opts(argc, argv, options, help);
> -
> -	if (m_opt) {
> -		memsize = pages = atoi(m_copt);
> -
> -		if (memsize < 1) {
> -			tst_brkm(TBROK, cleanup, "Invalid arg for -m: %s",
> -				 m_copt);
> -		}
> -
> -		memsize *= getpagesize();	/* N PAGES */
> -
> -	} else {
> -		/*
> -		 * default size 1000 pages;
> -		 */
> -		memsize = pages = 1000;
> -		memsize *= getpagesize();
> -	}
> -
> -	tst_resm(TINFO, "mmap()ing file of %u pages or %u bytes", pages,
> -		 memsize);
> -
> -	setup();
> -
> -	for (lc = 0; TEST_LOOPING(lc); lc++) {
> -		tst_count = 0;
> -
> -		fd = open(filename, O_RDWR | O_CREAT, 0666);
> -		if ((fd == -1))
> -			tst_brkm(TBROK | TERRNO, cleanup,
> -				 "opening %s failed", filename);
> -
> -		if (lseek(fd, memsize, SEEK_SET) != memsize) {
> -			TEST_ERRNO = errno;
> -			close(fd);
> -			tst_brkm(TBROK | TTERRNO, cleanup, "lseek failed");
> -		}
> -
> -		if (write(fd, "\0", 1) != 1) {
> -			TEST_ERRNO = errno;
> -			close(fd);
> -			tst_brkm(TBROK | TTERRNO, cleanup,
> -				 "writing to %s failed", filename);
> -		}
> -
> -		array = mmap(0, memsize, PROT_WRITE, MAP_SHARED, fd, 0);
> -		if (array == MAP_FAILED) {
> -			TEST_ERRNO = errno;
> -			close(fd);
> -			tst_brkm(TBROK | TTERRNO, cleanup,
> -				 "mmapping %s failed", filename);
> -		} else {
> -			tst_resm(TPASS, "mmap() completed successfully.");
> -		}
> -
> -		tst_resm(TINFO, "touching mmaped memory");
> -
> -		for (i = 0; i < memsize; i++) {
> -			array[i] = (char)i;
> -		}
> -
> -		/*
> -		 * seems that if the map area was bad, we'd get SEGV,
> -		 * hence we can indicate a PASS.
> -		 */
> -		tst_resm(TPASS,
> -			 "we're still here, mmaped area must be good");
> -
> -		TEST(msync(array, memsize, MS_SYNC));
> -
> -		if (TEST_RETURN == -1) {
> -			tst_resm(TFAIL | TTERRNO,
> -				 "synchronizing mmapped page failed");
> -		} else {
> -			tst_resm(TPASS,
> -				 "synchronizing mmapped page passed");
> -		}
> -
> -		TEST(munmap(array, memsize));
> -
> -		if (TEST_RETURN == -1) {
> -			tst_resm(TFAIL | TTERRNO,
> -				 "munmapping %s failed", filename);
> -		} else {
> -			tst_resm(TPASS, "munmapping %s successful", filename);
> -		}
> -
> -		close(fd);
> -		unlink(filename);
> -
> -	}
> -	cleanup();
> -	tst_exit();
> -}
> diff --git a/testcases/kernel/syscalls/mmap/mmap21.c b/testcases/kernel/syscalls/mmap/mmap21.c
> new file mode 100644
> index 0000000000000000000000000000000000000000..895f86d36afe32082205f0b391bfa19070170d87
> --- /dev/null
> +++ b/testcases/kernel/syscalls/mmap/mmap21.c
> @@ -0,0 +1,108 @@
> +// SPDX-License-Identifier: GPL-2.0-or-later
> +/*
> + * Copyright (C) 2000 Juan Quintela <quintela@fi.udc.es>
> + *                    Aaron Laffin <alaffin@sgi.com>
> + * Copyright (C) 2025 SUSE LLC Andrea Cervesato <andrea.cervesato@suse.com>
> + */
> +
> +/*\
> + * [Description]
> + *
> + * Verify that we can use mmap() to map a large file, write to it via memory
> + * access, and read back the data from the file. This test also verifies that
> + * accessing memory won't end up killing the test with SIGSEGV.
> + */
> +
> +#include "tst_test.h"
> +
> +static char filename[1024];
> +static char *str_pages;
> +static long long pages = 1000;
> +static long long memory_size;
> +static char *memory_data;
> +
> +static void run(void)
> +{
> +	int fd;
> +	pid_t pid;
> +	int status;
> +	char *buff = NULL;
> +
> +	tst_res(TINFO, "mmap()ing file of %llu bytes", memory_size);
> +
> +	fd = SAFE_OPEN(filename, O_RDWR | O_CREAT, 0666);
> +	SAFE_LSEEK(fd, memory_size, SEEK_SET);
> +	SAFE_WRITE(SAFE_WRITE_ALL, fd, "\0", 1);
> +
> +	memory_data = SAFE_MMAP(0, memory_size, PROT_WRITE, MAP_SHARED, fd, 0);
> +
> +	pid = SAFE_FORK();
> +	if (!pid) {
> +		tst_res(TINFO, "Touching mapped memory");
> +
> +		for (int i = 0; i < memory_size; i++)
> +			memory_data[i] = (char)i;
> +
> +		exit(0);
> +	}
> +
> +	SAFE_WAITPID(pid, &status, 0);
> +
> +	if (WIFSIGNALED(status) && WTERMSIG(status) == SIGSEGV) {
> +		tst_res(TFAIL, "Touching memory caused SIGSEGV");
> +		goto exit;
> +	}

The child here is not going to crash unless there is a serious bug in
the kernel. And with such bug I would expect that the machine wouldn't
even boot.

So I wouldn't wait the child here and just let the test library collect
it.

> +	SAFE_MSYNC(memory_data, memory_size, MS_SYNC);
> +
> +	buff = SAFE_MALLOC(memory_size);

This buffer can be allocated once in the test setup and freed once in
the test cleanup.

> +	memset(buff, 0, memory_size);
> +
> +	SAFE_LSEEK(fd, 0, SEEK_SET);
> +	SAFE_READ(0, fd, buff, memory_size);
> +	SAFE_CLOSE(fd);
> +
> +	for (int i = 0; i < memory_size; i++) {
> +		if (buff[i] != (char)i) {
> +			tst_res(TFAIL, "Mapped file has not been updated at byte %d", i);
> +			goto exit;
> +		}
> +	}
> +
> +	tst_res(TPASS, "Mapped file has been updated");
> +
> +exit:
> +	if (buff)
> +		free(buff);
> +
> +	SAFE_MUNMAP(memory_data, memory_size);
> +	SAFE_UNLINK(filename);
> +}
> +
> +static void setup(void)
> +{
> +	if (tst_parse_filesize(str_pages, &pages, 1, LLONG_MAX))
> +		tst_brk(TBROK, "Invalid number of pages: %s", str_pages);
> +
> +	memory_size = pages * getpagesize();
> +
> +	snprintf(filename, 1024, "testfile.%d", getpid());

Appending $pid to the name is a leftover from days where the tests
didn't run in temporary directory. We do not need that anymore, we can
just hardcode any static name in the open() call above.

> +}
> +
> +static void cleanup(void)
> +{
> +	if (memory_data)
> +		SAFE_MUNMAP(memory_data, memory_size);
> +}
> +
> +static struct tst_test test = {
> +	.test_all = run,
> +	.setup = setup,
> +	.cleanup = cleanup,
> +	.needs_tmpdir = 1,
> +	.forks_child = 1,
> +	.options = (struct tst_option[]) {
> +		{"m:", &str_pages, "Number of pages (default 1000)"},
> +		{}
> +	},
> +};
> 
> -- 
> 2.43.0
> 
> 
> -- 
> Mailing list info: https://lists.linux.it/listinfo/ltp

-- 
Cyril Hrubis
chrubis@suse.cz

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

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

end of thread, other threads:[~2025-02-25 15:51 UTC | newest]

Thread overview: 19+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-02-10  9:07 [LTP] [PATCH v2 0/8] Cleanup the mmap testing suite Andrea Cervesato
2025-02-10  9:07 ` [LTP] [PATCH v2 1/8] Refactor mmap03 test Andrea Cervesato
2025-02-10  9:41   ` Petr Vorel
2025-02-10 11:46     ` Ricardo B. Marlière
2025-02-24 16:56     ` Cyril Hrubis
2025-02-10  9:07 ` [LTP] [PATCH v2 2/8] Refactor mmap10 test Andrea Cervesato
2025-02-24 16:57   ` Cyril Hrubis
2025-02-10  9:07 ` [LTP] [PATCH v2 3/8] Cleanup mmap12 test Andrea Cervesato
2025-02-25 10:17   ` Cyril Hrubis
2025-02-10  9:07 ` [LTP] [PATCH v2 4/8] Cleanup mmap17 test Andrea Cervesato
2025-02-25 15:02   ` Cyril Hrubis
2025-02-10  9:07 ` [LTP] [PATCH v2 5/8] Cleanup mmap18 test Andrea Cervesato
2025-02-25 15:20   ` Cyril Hrubis
2025-02-10  9:07 ` [LTP] [PATCH v2 6/8] Cleanup mmap19 test Andrea Cervesato
2025-02-25 15:21   ` Cyril Hrubis
2025-02-10  9:07 ` [LTP] [PATCH v2 7/8] Cleanup mmap20 test Andrea Cervesato
2025-02-25 15:21   ` Cyril Hrubis
2025-02-10  9:07 ` [LTP] [PATCH v2 8/8] Refactor mmap001 test and move it to mmap21 Andrea Cervesato
2025-02-25 15:51   ` Cyril Hrubis

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