Skip to contentSkip to conversation

syscalls/madvise01: Convert to new test API

PATCH New Li Wang 2 participants 14 messages May 12, 2016 – May 19, 2016

lore
Share & tools
lore

Li Wang

author
#0 lore raw

PATCH 1/6 syscalls/madvise01: Convert to new test API

---
 testcases/kernel/syscalls/madvise/madvise01.c | 134 ++++++++++----------------
 1 file changed, 53 insertions(+), 81 deletions(-)
testcases/kernel/syscalls/madvise/madvise01.c +53 -81
index 4b1286d..53468dd 100644
@@ -32,107 +32,79 @@
 #include <string.h>
 #include <unistd.h>
 
-#include "test.h"
+#include "tst_test.h"
 
-static void setup(void);
-static void cleanup(void);
-static void check_and_print(char *advice);
+static char filename[64];
 
-char *TCID = "madvise01";
-int TST_TOTAL = 5;
+static void setup(void)
+{
+	sprintf(filename, "madvise01.%d", getpid());
+}
 
-int main(int argc, char *argv[])
+static void check_and_print(char *advice)
+{
+	if (TEST_RETURN == -1)
+		tst_res(TFAIL | TTERRNO, "madvise test for %s failed", advice);
+	else
+		tst_res(TPASS, "madvise test for %s PASSED", advice);
+}
+
+static void verify_madvise(void)
 {
-	int lc, fd;
 	int i = 0;
+	int fd;
 	char *file = NULL;
 	struct stat stat;
-	char filename[64];
-	char *progname = NULL;
 	char *str_for_file = "abcdefghijklmnopqrstuvwxyz12345\n";
 
-	tst_parse_opts(argc, argv, NULL, NULL);
-
-	setup();
-
-	progname = *argv;
-	sprintf(filename, "%s-out.%d", progname, getpid());
-
-	for (lc = 0; TEST_LOOPING(lc); lc++) {
-		tst_count = 0;
-
-		fd = open(filename, O_RDWR | O_CREAT, 0664);
-		if (fd < 0)
-			tst_brkm(TBROK | TERRNO, cleanup, "open failed");
+	fd = open(filename, O_RDWR | O_CREAT, 0664);
+	if (fd < 0)
+		tst_brk(TBROK | TERRNO, "open failed");
 #ifdef DEBUG
-		tst_resm(TINFO, "filename = %s opened successfully", filename);
+	tst_res(TINFO, "filename = %s opened successfully", filename);
 #endif
 
-		/* Writing 40 KB of random data into this file
-		   [32 * 1280 = 40960] */
-		for (i = 0; i < 1280; i++)
-			if (write(fd, str_for_file, strlen(str_for_file)) == -1)
-				tst_brkm(TBROK | TERRNO, cleanup,
-					 "write failed");
-
-		if (fstat(fd, &stat) == -1)
-			tst_brkm(TBROK, cleanup, "fstat failed");
-
-		/* Map the input file into memory */
-		file = mmap(NULL, stat.st_size, PROT_READ, MAP_SHARED, fd, 0);
-		if (file == MAP_FAILED)
-			tst_brkm(TBROK, cleanup, "mmap failed");
+	/* Writing 40 KB of random data into this file
+	   [32 * 1280 = 40960] */
+	for (i = 0; i < 1280; i++)
+		if (write(fd, str_for_file, strlen(str_for_file)) == -1)
+			tst_brk(TBROK | TERRNO, "write failed");
 
-		/* (1) Test case for MADV_NORMAL */
-		TEST(madvise(file, stat.st_size, MADV_NORMAL));
-		check_and_print("MADV_NORMAL");
+	if (fstat(fd, &stat) == -1)
+		tst_brk(TBROK, "fstat failed");
 
-		/* (2) Test case for MADV_RANDOM */
-		TEST(madvise(file, stat.st_size, MADV_RANDOM));
-		check_and_print("MADV_RANDOM");
+	/* Map the input file into memory */
+	file = SAFE_MMAP(NULL, stat.st_size, PROT_READ, MAP_SHARED, fd, 0);
 
-		/* (3) Test case for MADV_SEQUENTIAL */
-		TEST(madvise(file, stat.st_size, MADV_SEQUENTIAL));
-		check_and_print("MADV_SEQUENTIAL");
+	/* (1) Test case for MADV_NORMAL */
+	TEST(madvise(file, stat.st_size, MADV_NORMAL));
+	check_and_print("MADV_NORMAL");
 
-		/* (4) Test case for MADV_WILLNEED */
-		TEST(madvise(file, stat.st_size, MADV_WILLNEED));
-		check_and_print("MADV_WILLNEED");
+	/* (2) Test case for MADV_RANDOM */
+	TEST(madvise(file, stat.st_size, MADV_RANDOM));
+	check_and_print("MADV_RANDOM");
 
-		/* (5) Test case for MADV_DONTNEED */
-		TEST(madvise(file, stat.st_size, MADV_DONTNEED));
-		check_and_print("MADV_DONTNEED");
+	/* (3) Test case for MADV_SEQUENTIAL */
+	TEST(madvise(file, stat.st_size, MADV_SEQUENTIAL));
+	check_and_print("MADV_SEQUENTIAL");
 
-		if (munmap(file, stat.st_size) == -1)
-			tst_brkm(TBROK | TERRNO, cleanup, "munmap failed");
+	/* (4) Test case for MADV_WILLNEED */
+	TEST(madvise(file, stat.st_size, MADV_WILLNEED));
+	check_and_print("MADV_WILLNEED");
 
-		close(fd);
-	}
+	/* (5) Test case for MADV_DONTNEED */
+	TEST(madvise(file, stat.st_size, MADV_DONTNEED));
+	check_and_print("MADV_DONTNEED");
 
-	cleanup();
-	tst_exit();
-}
-
-static void setup(void)
-{
+	if (munmap(file, stat.st_size) == -1)
+		tst_brk(TBROK | TERRNO, "munmap failed");
 
-	tst_sig(NOFORK, DEF_HANDLER, cleanup);
-
-	TEST_PAUSE;
-
-	tst_tmpdir();
+	close(fd);
 }
 
-static void cleanup(void)
-{
-	tst_rmdir();
-
-}
-
-static void check_and_print(char *advice)
-{
-	if (TEST_RETURN == -1)
-		tst_resm(TFAIL | TTERRNO, "madvise test for %s failed", advice);
-	else
-		tst_resm(TPASS, "madvise test for %s PASSED", advice);
-}
+static struct tst_test test = {
+	.tid = "madvise01",
+	.test_all = verify_madvise,
+	.needs_tmpdir = 1,
+	.setup = setup,
+};
Signature · 1 line
1.8.3.1
author
#1 lore raw

PATCH 2/6 syscalls/madvise02: Convert to new test API

---
 testcases/kernel/syscalls/madvise/madvise02.c | 117 +++++++++++---------------
 1 file changed, 48 insertions(+), 69 deletions(-)
testcases/kernel/syscalls/madvise/madvise02.c +48 -69
index b9eb77d..4cf61bf 100644
@@ -48,20 +48,16 @@
 #include <errno.h>
 #include <fcntl.h>
 #include <stdio.h>
+#include <stdlib.h>
 #include <string.h>
 #include <unistd.h>
-#include "test.h"
-#include "safe_macros.h"
+#include "tst_kvercmp.h"
+#include "tst_test.h"
 
 #define TEST_FILE "testfile"
 #define STR "abcdefghijklmnopqrstuvwxyz12345\n"
-
 #define KSM_SYS_DIR	"/sys/kernel/mm/ksm"
 
-static void setup(void);
-static void cleanup(void);
-static void check_and_print(int expected_errno);
-
 static void test_addr_einval(void);
 static void test_advice_einval(void);
 #if !defined(UCLINUX)
@@ -92,73 +88,36 @@ static void (*test_func[])(void) = {
 	test_ebadf,
 };
 
-char *TCID = "madvise02";
-int TST_TOTAL = ARRAY_SIZE(test_func);
-
 static int fd;
 static struct stat st;
 static int pagesize;
 
-int main(int argc, char *argv[])
-{
-	int lc;
-	int i;
-
-	tst_parse_opts(argc, argv, NULL, NULL);
-
-	setup();
-
-	for (lc = 0; TEST_LOOPING(lc); lc++) {
-		tst_count = 0;
-
-		for (i = 0; i < TST_TOTAL; i++)
-			(*test_func[i])();
-	}
-
-	cleanup();
-	tst_exit();
-}
-
 static void setup(void)
 {
 	int i;
 
-	tst_sig(NOFORK, DEF_HANDLER, cleanup);
-
-	TEST_PAUSE;
-
-	tst_tmpdir();
-
-	fd = SAFE_OPEN(cleanup, TEST_FILE, O_RDWR | O_CREAT, 0664);
+	fd = SAFE_OPEN(TEST_FILE, O_RDWR | O_CREAT, 0664);
 
 	pagesize = getpagesize();
 
 	/* Writing 16 pages of random data into this file */
 	for (i = 0; i < (pagesize / 2); i++)
-		SAFE_WRITE(cleanup, 1, fd, STR, sizeof(STR) - 1);
+		SAFE_WRITE(1, fd, STR, sizeof(STR) - 1);
 
-	SAFE_FSTAT(cleanup, fd, &st);
-}
-
-static void cleanup(void)
-{
-	if (fd && close(fd) < 0)
-		tst_resm(TWARN | TERRNO, "close failed");
-
-	tst_rmdir();
+	SAFE_FSTAT(fd, &st);
 }
 
 static void check_and_print(int expected_errno)
 {
 	if (TEST_RETURN == -1) {
 		if (TEST_ERRNO == expected_errno)
-			tst_resm(TPASS | TTERRNO, "failed as expected");
+			tst_res(TPASS | TTERRNO, "failed as expected");
 		else
-			tst_resm(TFAIL | TTERRNO,
+			tst_res(TFAIL | TTERRNO,
 				 "failed unexpectedly; expected - %d : %s",
 				 expected_errno, strerror(expected_errno));
 	} else {
-		tst_resm(TFAIL, "madvise succeeded unexpectedly");
+		tst_res(TFAIL, "madvise succeeded unexpectedly");
 	}
 }
 
@@ -166,26 +125,26 @@ static void test_addr_einval(void)
 {
 	char *file;
 
-	file = SAFE_MMAP(cleanup, 0, st.st_size, PROT_READ,
+	file = SAFE_MMAP(0, st.st_size, PROT_READ,
 					 MAP_SHARED, fd, 0);
 
 	TEST(madvise(file + 100, st.st_size, MADV_NORMAL));
 	check_and_print(EINVAL);
 
-	SAFE_MUNMAP(cleanup, file, st.st_size);
+	SAFE_MUNMAP(file, st.st_size);
 }
 
 static void test_advice_einval(void)
 {
 	char *file;
 
-	file = SAFE_MMAP(cleanup, 0, st.st_size, PROT_READ,
+	file = SAFE_MMAP(0, st.st_size, PROT_READ,
 					 MAP_SHARED, fd, 0);
 
 	TEST(madvise(file, st.st_size, 1212));
 	check_and_print(EINVAL);
 
-	SAFE_MUNMAP(cleanup, file, st.st_size);
+	SAFE_MUNMAP(file, st.st_size);
 }
 
 #if !defined(UCLINUX)
@@ -193,16 +152,16 @@ static void test_lock_einval(void)
 {
 	char *file;
 
-	file = SAFE_MMAP(cleanup, 0, st.st_size, PROT_READ,
+	file = SAFE_MMAP(0, st.st_size, PROT_READ,
 					 MAP_SHARED, fd, 0);
 
 	if (mlock(file, st.st_size) < 0)
-		tst_brkm(TBROK | TERRNO, cleanup, "mlock failed");
+		tst_brk(TBROK | TERRNO, "mlock failed");
 
 	TEST(madvise(file, st.st_size, MADV_DONTNEED));
 	check_and_print(EINVAL);
 
-	SAFE_MUNMAP(cleanup, file, st.st_size);
+	SAFE_MUNMAP(file, st.st_size);
 }
 #endif /* if !defined(UCLINUX) */
 
@@ -212,18 +171,18 @@ static void test_mergeable_einval(void)
 	char *file;
 
 	if (access(KSM_SYS_DIR, F_OK) >= 0) {
-		tst_resm(TCONF, "kernel configured with CONFIG_KSM, "
+		tst_res(TCONF, "kernel configured with CONFIG_KSM, "
 				 "skip EINVAL test for MADV_MERGEABLE.");
 		return;
 	}
 
-	file = SAFE_MMAP(cleanup, 0, st.st_size, PROT_READ,
-					 MAP_SHARED, fd, 0);
+	file = SAFE_MMAP(0, st.st_size, PROT_READ,
+				 MAP_SHARED, fd, 0);
 
 	TEST(madvise(file, st.st_size, MADV_MERGEABLE));
 	check_and_print(EINVAL);
 
-	SAFE_MUNMAP(cleanup, file, st.st_size);
+	SAFE_MUNMAP(file, st.st_size);
 }
 #endif
 
@@ -233,18 +192,18 @@ static void test_unmergeable_einval(void)
 	char *file;
 
 	if (access(KSM_SYS_DIR, F_OK) >= 0) {
-		tst_resm(TCONF, "kernel configured with CONFIG_KSM, "
+		tst_res(TCONF, "kernel configured with CONFIG_KSM, "
 				 "skip EINVAL test for MADV_UNMERGEABLE.");
 		return;
 	}
 
-	file = SAFE_MMAP(cleanup, 0, st.st_size, PROT_READ,
+	file = SAFE_MMAP(0, st.st_size, PROT_READ,
 					 MAP_SHARED, fd, 0);
 
 	TEST(madvise(file, st.st_size, MADV_UNMERGEABLE));
 	check_and_print(EINVAL);
 
-	SAFE_MUNMAP(cleanup, file, st.st_size);
+	SAFE_MUNMAP(file, st.st_size);
 }
 #endif
 
@@ -254,10 +213,10 @@ static void test_enomem(void)
 	char *high;
 	unsigned long len;
 
-	low = SAFE_MMAP(cleanup, 0, st.st_size / 2, PROT_READ,
+	low = SAFE_MMAP(0, st.st_size / 2, PROT_READ,
 					MAP_SHARED, fd, 0);
 
-	high = SAFE_MMAP(cleanup, 0, st.st_size / 2, PROT_READ,
+	high = SAFE_MMAP(0, st.st_size / 2, PROT_READ,
 					 MAP_SHARED, fd, st.st_size / 2);
 
 	/* Swap if necessary to make low < high */
@@ -270,12 +229,12 @@ static void test_enomem(void)
 
 	len = (high - low) + pagesize;
 
-	SAFE_MUNMAP(cleanup, high, st.st_size / 2);
+	SAFE_MUNMAP(high, st.st_size / 2);
 
 	TEST(madvise(low, len, MADV_NORMAL));
 	check_and_print(ENOMEM);
 
-	SAFE_MUNMAP(cleanup, low, st.st_size / 2);
+	SAFE_MUNMAP(low, st.st_size / 2);
 }
 
 static void test_ebadf(void)
@@ -302,9 +261,29 @@ static void test_ebadf(void)
 	 * prefretch.
 	 */
 	} else {
-		tst_resm(TPASS, "madvise succeeded as expected, see "
+		tst_res(TPASS, "madvise succeeded as expected, see "
 				"kernel commit 1998cc0 for details.");
 	}
 
 	free(ptr_memory_allocated);
 }
+
+static void verify_madvise(unsigned int i)
+{
+	(*test_func[i])();
+}
+
+static void cleanup(void)
+{
+	if (fd && close(fd) < 0)
+		tst_res(TWARN | TERRNO, "close failed");
+}
+
+static struct tst_test test = {
+	.tid = "madvise02",
+	.tcnt = ARRAY_SIZE(test_func),
+	.test = verify_madvise,
+	.needs_tmpdir = 1,
+	.setup = setup,
+	.cleanup = cleanup,
+};
Signature · 1 line
1.8.3.1
author
#2 lore raw

PATCH 3/6 syscalls/madvise03: Convert to new test API

---
 testcases/kernel/syscalls/madvise/madvise03.c | 186 +++++++++++---------------
 1 file changed, 78 insertions(+), 108 deletions(-)
testcases/kernel/syscalls/madvise/madvise03.c +78 -108
index 3da2bb2..b414e89 100644
@@ -32,151 +32,121 @@
 #include <string.h>
 #include <unistd.h>
 
-#include "test.h"
-
-char *TCID = "madvise03";
+#include "tst_test.h"
 
 #ifdef MADV_REMOVE
 
-int TST_TOTAL = 3;
-
-static void setup(void);
-static void cleanup(void);
-static void check_and_print(char *advice);
-static long get_shmmax(void);
 
+static char filename[64];
 static int shmid1;
 
-int main(int argc, char *argv[])
+static void setup(void)
+{
+	sprintf(filename, "madvise03.%d", getpid());
+}
+
+static void check_and_print(char *advice)
+{
+	if (TEST_RETURN == -1) {
+		if (TEST_ERRNO == ENOTSUP && !strcmp(advice, "MADV_REMOVE")) {
+			tst_res(TCONF, "madvise MADV_REMOVE returned ENOTSUP"
+				 " CONFIG_TMPFS=y not in kernel .config?");
+			return;
+		}
+		tst_res(TFAIL,
+			 "madvise test for %s failed with "
+			 "return = %ld, errno = %d : %s",
+			 advice, TEST_RETURN, TEST_ERRNO, strerror(TEST_ERRNO));
+	} else {
+		tst_res(TPASS, "madvise test for %s PASSED", advice);
+	}
+}
+
+static long get_shmmax(void)
+{
+	long maxsize;
+
+	SAFE_FILE_SCANF("/proc/sys/kernel/shmmax", "%ld", &maxsize);
+
+	return maxsize;
+}
+
+static void verify_madvise(void)
 {
-	int lc, fd;
-	int i;
+	int i, fd;
 	char *file = NULL;
 	struct stat stat;
 	void *addr1;
 	long shm_size = 0;
-	char filename[64];
-	char *progname = NULL;
 	char *str_for_file = "abcdefghijklmnopqrstuvwxyz12345\n";
 
-	tst_parse_opts(argc, argv, NULL, NULL);
-
-	setup();
-
-	progname = *argv;
-	sprintf(filename, "%s-out.%d", progname, getpid());
-
-	for (lc = 0; TEST_LOOPING(lc); lc++) {
-		tst_count = 0;
-
-		fd = open(filename, O_RDWR | O_CREAT, 0664);
-		if (fd < 0)
-			tst_brkm(TBROK, cleanup, "open failed");
+	fd = open(filename, O_RDWR | O_CREAT, 0664);
+	if (fd < 0)
+		tst_brk(TBROK, "open failed");
 #ifdef DEBUG
-		tst_resm(TINFO, "filename = %s opened successfully", filename);
+	tst_res(TINFO, "filename = %s opened successfully", filename);
 #endif
 
-		/* Writing 40 KB of random data into this file
-		   [32 * 1280 = 40960] */
-		for (i = 0; i < 1280; i++)
-			if (write(fd, str_for_file, strlen(str_for_file)) == -1)
-				tst_brkm(TBROK | TERRNO, cleanup,
-					 "write failed");
+	/* Writing 40 KB of random data into this file
+	   [32 * 1280 = 40960] */
+	for (i = 0; i < 1280; i++)
+		if (write(fd, str_for_file, strlen(str_for_file)) == -1)
+			tst_brk(TBROK | TERRNO, "write failed");
 
-		if (fstat(fd, &stat) == -1)
-			tst_brkm(TBROK, cleanup, "fstat failed");
+	if (fstat(fd, &stat) == -1)
+		tst_brk(TBROK, "fstat failed");
 
-		file = mmap(NULL, stat.st_size, PROT_READ, MAP_SHARED, fd, 0);
-		if (file == MAP_FAILED)
-			tst_brkm(TBROK | TERRNO, cleanup, "mmap failed");
+	file = SAFE_MMAP(NULL, stat.st_size, PROT_READ, MAP_SHARED, fd, 0);
 
-		/* Allocate shared memory segment */
-		shm_size = get_shmmax();
+	/* Allocate shared memory segment */
+	shm_size = get_shmmax();
 
 #define min(a, b) ((a) < (b) ? @ : (b))
-		shmid1 = shmget(IPC_PRIVATE, min(1024 * 1024, shm_size),
-				IPC_CREAT | IPC_EXCL | 0701);
-		if (shmid1 == -1)
-			tst_brkm(TBROK, cleanup, "shmget failed");
+	shmid1 = shmget(IPC_PRIVATE, min(1024 * 1024, shm_size),
+			IPC_CREAT | IPC_EXCL | 0701);
+	if (shmid1 == -1)
+		tst_brk(TBROK, "shmget failed");
 
-		/* Attach shared memory segment to an address selected by the system */
-		addr1 = shmat(shmid1, NULL, 0);
-		if (addr1 == (void *)-1)
-			tst_brkm(TBROK, cleanup, "shmat error");
+	/* Attach shared memory segment to an address selected by the system */
+	addr1 = shmat(shmid1, NULL, 0);
+	if (addr1 == (void *)-1)
+		tst_brk(TBROK, "shmat error");
 
-		/* (1) Test case for MADV_REMOVE */
-		TEST(madvise(addr1, 4096, MADV_REMOVE));
-		check_and_print("MADV_REMOVE");
+	/* (1) Test case for MADV_REMOVE */
+	TEST(madvise(addr1, 4096, MADV_REMOVE));
+	check_and_print("MADV_REMOVE");
 
-		/* (2) Test case for MADV_DONTFORK */
-		TEST(madvise(file, (stat.st_size / 2), MADV_DONTFORK));
-		check_and_print("MADV_DONTFORK");
+	/* (2) Test case for MADV_DONTFORK */
+	TEST(madvise(file, (stat.st_size / 2), MADV_DONTFORK));
+	check_and_print("MADV_DONTFORK");
 
-		/* (3) Test case for MADV_DOFORK */
-		TEST(madvise(file, (stat.st_size / 2), MADV_DOFORK));
-		check_and_print("MADV_DOFORK");
+	/* (3) Test case for MADV_DOFORK */
+	TEST(madvise(file, (stat.st_size / 2), MADV_DOFORK));
+	check_and_print("MADV_DOFORK");
 
-		/* Finally Unmapping the whole file */
-		if (munmap(file, stat.st_size) < 0)
-			tst_brkm(TBROK | TERRNO, cleanup, "munmap failed");
+	/* Finally Unmapping the whole file */
+	SAFE_MUNMAP(file, stat.st_size);
 
-		close(fd);
-	}
-
-	cleanup();
-	tst_exit();
-}
-
-static void setup(void)
-{
-
-	tst_sig(NOFORK, DEF_HANDLER, cleanup);
-
-	TEST_PAUSE;
-
-	tst_tmpdir();
+	close(fd);
 }
 
 static void cleanup(void)
 {
 	if (shmid1 != -1)
 		if (shmctl(shmid1, IPC_RMID, 0) < 0)
-			tst_resm(TBROK | TERRNO,
+			tst_res(TBROK | TERRNO,
 				 "shmctl(.., IPC_RMID, ..) failed");
-
-	tst_rmdir();
-}
-
-static void check_and_print(char *advice)
-{
-	if (TEST_RETURN == -1) {
-		if (TEST_ERRNO == ENOTSUP && !strcmp(advice, "MADV_REMOVE")) {
-			tst_resm(TCONF, "madvise MADV_REMOVE returned ENOTSUP"
-				 " CONFIG_TMPFS=y not in kernel .config?");
-			return;
-		}
-		tst_resm(TFAIL,
-			 "madvise test for %s failed with "
-			 "return = %ld, errno = %d : %s",
-			 advice, TEST_RETURN, TEST_ERRNO, strerror(TEST_ERRNO));
-	} else {
-		tst_resm(TPASS, "madvise test for %s PASSED", advice);
-	}
 }
 
-static long get_shmmax(void)
-{
-	long maxsize;
-
-	SAFE_FILE_SCANF(cleanup, "/proc/sys/kernel/shmmax", "%ld", &maxsize);
+static struct tst_test test = {
+	.tid = "madvise03",
+	.test_all = verify_madvise,
+	.needs_tmpdir = 1,
+	.setup = setup,
+	.cleanup = cleanup,
+};
 
-	return maxsize;
-}
 #else
-int main(void)
-{
 	/* "Requires 2.6.16+" were the original comments */
-	tst_brkm(TCONF, NULL,
-		 "this system doesn't have required madvise support");
-}
+	TST_TEST_TCONF("this system doesn't have required madvise support");
 #endif
Signature · 1 line
1.8.3.1
author
#3 lore raw

PATCH 4/6 syscalls/madvise04: Convert to new test API

---
 testcases/kernel/syscalls/madvise/madvise04.c | 137 ++++++++++----------------
 1 file changed, 53 insertions(+), 84 deletions(-)
testcases/kernel/syscalls/madvise/madvise04.c +53 -84
index e89babc..07af2dc 100644
@@ -27,112 +27,81 @@
 #include <sys/mman.h>
 #include <fcntl.h>
 
-#include "test.h"
-
-char *TCID = "madvise04";
+#include "tst_test.h"
 
 #ifdef MADV_DONTDUMP
 
-int TST_TOTAL = 2;
-
 #define BUFFER_SIZE  256
 
-static void setup(void);
-static void cleanup(void);
-static void check_and_print(char *advice);
+static char filename[64];
+
+static void setup(void)
+{
+	sprintf(filename, "madvise04.%d", getpid());
+}
+
+static void check_and_print(char *advice)
+{
+	if (TEST_RETURN == -1) {
+		tst_res(TFAIL,
+			 "madvise test for %s failed with "
+			 "return = %ld, errno = %d : %s",
+			 advice, TEST_RETURN, TEST_ERRNO, strerror(TEST_ERRNO));
+	} else {
+		tst_res(TPASS, "madvise test for %s PASSED", advice);
+	}
+}
 
-int main(int argc, char *argv[])
+static void verify_madvise(void)
 {
-	int lc, fd;
-	int i;
+	int i, fd;
 	char *file = NULL;
 	struct stat stat;
-	char filename[64];
-	char *progname = NULL;
 	char *str_for_file = "abcdefghijklmnopqrstuvwxyz12345\n";
 
-	tst_parse_opts(argc, argv, NULL, NULL);
-
-	setup();
-
-	progname = *argv;
-	sprintf(filename, "%s-out.%d", progname, getpid());
-
-	for (lc = 0; TEST_LOOPING(lc); lc++) {
-		tst_count = 0;
-
-		fd = open(filename, O_RDWR | O_CREAT, 0664);
-		if (fd < 0)
-			tst_brkm(TBROK, cleanup, "open failed");
+	fd = open(filename, O_RDWR | O_CREAT, 0664);
+	if (fd < 0)
+		tst_brk(TBROK, "open failed");
 #ifdef DEBUG
-		tst_resm(TINFO, "filename = %s opened successfully", filename);
+	tst_res(TINFO, "filename = %s opened successfully", filename);
 #endif
 
-		/* Writing 40 KB of random data into this file
-		   [32 * 1280 = 40960] */
-		for (i = 0; i < 1280; i++)
-			if (write(fd, str_for_file, strlen(str_for_file)) == -1)
-				tst_brkm(TBROK | TERRNO, cleanup,
-					 "write failed");
+	/* Writing 40 KB of random data into this file
+	   [32 * 1280 = 40960] */
+	for (i = 0; i < 1280; i++)
+		if (write(fd, str_for_file, strlen(str_for_file)) == -1)
+			tst_brk(TBROK | TERRNO, "write failed");
 
-		if (fstat(fd, &stat) == -1)
-			tst_brkm(TBROK, cleanup, "fstat failed");
+	if (fstat(fd, &stat) == -1)
+		tst_brk(TBROK, "fstat failed");
 
-		file = mmap(NULL, stat.st_size, PROT_READ, MAP_SHARED, fd, 0);
-		if (file == MAP_FAILED)
-			tst_brkm(TBROK | TERRNO, cleanup, "mmap failed");
+	file = SAFE_MMAP(NULL, stat.st_size, PROT_READ, MAP_SHARED, fd, 0);
 
-		/* (1) Test case for MADV_DONTDUMP */
-		TEST(madvise(file, stat.st_size, MADV_DONTDUMP));
-		check_and_print("MADV_DONTDUMP");
+	/* (1) Test case for MADV_DONTDUMP */
+	TEST(madvise(file, stat.st_size, MADV_DONTDUMP));
+	check_and_print("MADV_DONTDUMP");
 
-		/* (2) Test case for MADV_DODUMP */
-		TEST(madvise(file, stat.st_size, MADV_DODUMP));
-		check_and_print("MADV_DODUMP");
+	/* (2) Test case for MADV_DODUMP */
+	TEST(madvise(file, stat.st_size, MADV_DODUMP));
+	check_and_print("MADV_DODUMP");
 
-		/* Finally Unmapping the whole file */
-		if (munmap(file, stat.st_size) < 0)
-			tst_brkm(TBROK | TERRNO, cleanup, "munmap failed");
+	/* Finally Unmapping the whole file */
+	SAFE_MUNMAP(file, stat.st_size);
 
-		close(fd);
-	}
-
-	cleanup();
-	tst_exit();
-}
-
-static void setup(void)
-{
-	tst_sig(NOFORK, DEF_HANDLER, cleanup);
-	TEST_PAUSE;
-	tst_tmpdir();
+	close(fd);
 }
 
-static void cleanup(void)
-{
-	tst_rmdir();
-}
-
-static void check_and_print(char *advice)
-{
-	if (TEST_RETURN == -1) {
-		tst_resm(TFAIL,
-			 "madvise test for %s failed with "
-			 "return = %ld, errno = %d : %s",
-			 advice, TEST_RETURN, TEST_ERRNO, strerror(TEST_ERRNO));
-	} else {
-		tst_resm(TPASS, "madvise test for %s PASSED", advice);
-	}
-}
+static struct tst_test test = {
+	.tid = "madvise04",
+	.test_all = verify_madvise,
+	.needs_tmpdir = 1,
+	.setup = setup,
+};
 
 #else
-int main(void)
-{
-	/* Requires kernel version >= 3.4 */
-	tst_brkm(TCONF, NULL,
-		 "This system doesn't have required madvise support, "
-		 "MADV_DONTDUMP and MADV_DODUMP were added from 3.4. "
-		 "If your kernel version >= 3.4, maybe you need updating "
-		 "your glibc-headers");
-}
+/* Requires kernel version >= 3.4 */
+TST_TEST_TCONF("This system doesn't have required madvise support, "
+		"MADV_DONTDUMP and MADV_DODUMP were added from 3.4. "
+		"If your kernel version >= 3.4, maybe you need updating "
+		"your glibc-headers");
 #endif
Signature · 1 line
1.8.3.1
author
#4 lore raw

PATCH 5/6 syscalls/madvise05: Convert to new test API

---
 testcases/kernel/syscalls/madvise/madvise05.c | 64 +++++++++------------------
 1 file changed, 20 insertions(+), 44 deletions(-)
testcases/kernel/syscalls/madvise/madvise05.c +20 -44
index cdbc26f..612c967 100644
@@ -26,60 +26,36 @@
 
 #include <sys/mman.h>
 #include <errno.h>
-
-#include "test.h"
-#include "safe_macros.h"
+#include "tst_test.h"
 
 #define ALLOC_SIZE (32 * 1024 * 1024)
 
-static void setup(void);
-static void cleanup(void);
-
-char *TCID = "madvise05";
-int TST_TOTAL = 1;
-
-int main(int argc, char *argv[])
+static void verify_madvise(void)
 {
-	int lc;
 	void *p;
 
-	tst_parse_opts(argc, argv, NULL, NULL);
-
-	setup();
-
-	for (lc = 0; TEST_LOOPING(lc); lc++) {
-		p = SAFE_MMAP(cleanup, NULL, ALLOC_SIZE, PROT_READ,
+	p = SAFE_MMAP(NULL, ALLOC_SIZE, PROT_READ,
 			MAP_PRIVATE | MAP_ANONYMOUS | MAP_POPULATE, -1, 0);
-		TEST(mprotect(p, ALLOC_SIZE, PROT_NONE));
-		if (TEST_RETURN == -1)
-			tst_brkm(TBROK | TTERRNO, cleanup, "mprotect failed");
-		TEST(madvise(p, ALLOC_SIZE, MADV_WILLNEED));
-		SAFE_MUNMAP(cleanup, p, ALLOC_SIZE);
 
-		if (TEST_RETURN == 0)
-			continue;
+	TEST(mprotect(p, ALLOC_SIZE, PROT_NONE));
+	if (TEST_RETURN == -1)
+		tst_brk(TBROK | TTERRNO, "mprotect failed");
+	TEST(madvise(p, ALLOC_SIZE, MADV_WILLNEED));
+	SAFE_MUNMAP(p, ALLOC_SIZE);
 
-		if (TEST_ERRNO == EBADF)
-			tst_brkm(TCONF, cleanup, "CONFIG_SWAP=n");
-		else
-			tst_brkm(TBROK | TTERRNO, cleanup, "madvise failed");
+	if (TEST_RETURN == 0) {
+		tst_res(TPASS, "issue has not been reproduced");
+		return;
 	}
 
-	tst_resm(TPASS, "issue has not been reproduced");
-
-	cleanup();
-	tst_exit();
+	if (TEST_ERRNO == EBADF)
+		tst_brk(TCONF, "CONFIG_SWAP=n");
+	else
+		tst_brk(TBROK | TTERRNO, "madvise failed");
 }
 
-static void setup(void)
-{
-	tst_sig(NOFORK, DEF_HANDLER, cleanup);
-	if (tst_kvercmp(3, 9, 0) < 0)
-		tst_brkm(TCONF, NULL, "madvise(MADV_WILLNEED) swap file "
-			"prefetch available only since 3.9");
-	TEST_PAUSE;
-}
-
-static void cleanup(void)
-{
-}
+static struct tst_test test = {
+	.tid = "madvice05",
+	.min_kver = "3.9.0",
+	.test_all = verify_madvise,
+};
Signature · 1 line
1.8.3.1
author
#5 lore raw

PATCH 6/6 syscalls/madvise06: Convert to new test API

---
 testcases/kernel/syscalls/madvise/madvise06.c | 64 +++++++++------------------
 1 file changed, 21 insertions(+), 43 deletions(-)
testcases/kernel/syscalls/madvise/madvise06.c +21 -43
index 366827c..6b081fd 100644
@@ -33,39 +33,15 @@
  *       mm: madvise: fix MADV_WILLNEED on shmem swapouts
  */
 
-#include <stdio.h>
 #include <errno.h>
 #include <sys/sysinfo.h>
-
-#include "test.h"
-#include "safe_macros.h"
-
-char *TCID = "madvise06";
-int TST_TOTAL = 1;
+#include "tst_test.h"
 
 #define GB_SZ  (1024*1024*1024)
 
 static long dst_max;
 static int pg_sz;
 
-static void setup(void);
-static int  get_page_fault_num(void);
-static void test_advice_willneed(void);
-
-int main(int argc, char *argv[])
-{
-	int lc;
-
-	tst_parse_opts(argc, argv, NULL, NULL);
-
-	setup();
-
-	for (lc = 0; TEST_LOOPING(lc); lc++)
-		test_advice_willneed();
-
-	tst_exit();
-}
-
 static void setup(void)
 {
 	struct sysinfo sys_buf;
@@ -73,25 +49,21 @@ static void setup(void)
 	sysinfo(&sys_buf);
 
 	if (sys_buf.totalram < 2L * GB_SZ)
-		tst_brkm(TCONF, NULL, "Test requires more than 2GB of RAM");
+		tst_brk(TCONF, "Test requires more than 2GB of RAM");
 	if (sys_buf.totalram > 100L * GB_SZ)
-		tst_brkm(TCONF, NULL, "System RAM is too large, skip test");
+		tst_brk(TCONF, "System RAM is too large, skip test");
 
 	dst_max = sys_buf.totalram / GB_SZ;
-	tst_resm(TINFO, "dst_max = %ld", dst_max);
+	tst_res(TINFO, "dst_max = %ld", dst_max);
 
 	pg_sz = getpagesize();
-
-	tst_sig(NOFORK, DEF_HANDLER, NULL);
-
-	TEST_PAUSE;
 }
 
 static int get_page_fault_num(void)
 {
 	int pg;
 
-	SAFE_FILE_SCANF(NULL, "/proc/self/stat",
+	SAFE_FILE_SCANF("/proc/self/stat",
 			"%*s %*s %*s %*s %*s %*s %*s %*s %*s %*s %*s %d",
 			&pg);
 
@@ -107,13 +79,13 @@ static void test_advice_willneed(void)
 	int page_fault_num_2;
 
 	/* allocate source memory (1GB only) */
-	src = SAFE_MMAP(NULL, NULL, 1 * GB_SZ, PROT_READ | PROT_WRITE,
+	src = SAFE_MMAP(NULL, 1 * GB_SZ, PROT_READ | PROT_WRITE,
 			MAP_SHARED | MAP_ANONYMOUS,
 			-1, 0);
 
 	/* allocate destination memory (array) */
 	for (i = 0; i < dst_max; ++i)
-		dst[i] = SAFE_MMAP(NULL, NULL, 1 * GB_SZ,
+		dst[i] = SAFE_MMAP(NULL, 1 * GB_SZ,
 				PROT_READ | PROT_WRITE,
 				MAP_SHARED | MAP_ANONYMOUS,
 				-1, 0);
@@ -122,28 +94,34 @@ static void test_advice_willneed(void)
 	for (i = 0; i < dst_max; ++i)
 		memmove(dst[i], src, 1 * GB_SZ);
 
-	tst_resm(TINFO, "PageFault(no madvice): %d", get_page_fault_num());
+	tst_res(TINFO, "PageFault(no madvice): %d", get_page_fault_num());
 
 	/* Do madvice() to dst[0] */
 	TEST(madvise(dst[0], pg_sz, MADV_WILLNEED));
 	if (TEST_RETURN == -1)
-		tst_brkm(TBROK | TERRNO, NULL, "madvise failed");
+		tst_brk(TBROK | TERRNO, "madvise failed");
 
 	page_fault_num_1 = get_page_fault_num();
-	tst_resm(TINFO, "PageFault(madvice / no mem access): %d",
+	tst_res(TINFO, "PageFault(madvice / no mem access): %d",
 			page_fault_num_1);
 
 	*dst[0] = 'a';
 	page_fault_num_2 = get_page_fault_num();
-	tst_resm(TINFO, "PageFault(madvice / mem access): %d",
+	tst_res(TINFO, "PageFault(madvice / mem access): %d",
 			page_fault_num_2);
 
 	if (page_fault_num_1 != page_fault_num_2)
-		tst_resm(TFAIL, "Bug has been reproduced");
+		tst_res(TFAIL, "Bug has been reproduced");
 	else
-		tst_resm(TPASS, "Regression test pass");
+		tst_res(TPASS, "Regression test pass");
 
-	SAFE_MUNMAP(NULL, src, 1 * GB_SZ);
+	SAFE_MUNMAP(src, 1 * GB_SZ);
 	for (i = 0; i < dst_max; ++i)
-		SAFE_MUNMAP(NULL, dst[i], 1 * GB_SZ);
+		SAFE_MUNMAP(dst[i], 1 * GB_SZ);
 }
+
+static struct tst_test test = {
+	.tid = "madvice06",
+	.test_all = test_advice_willneed,
+	.setup = setup,
+};
Signature · 1 line
1.8.3.1
#8 lore raw

PATCH 3/6 syscalls/madvise03: Convert to new test API

In reply to Li Wang

Hi!

Quoted 5 lines
testcases/kernel/syscalls/madvise/madvise03.c +0 -0
index 3da2bb2..b414e89 100644

testcases/kernel/syscalls/madvise/madvise03.c line 37

Quoted 9 lines
testcases/kernel/syscalls/madvise/madvise03.c +1 -3
@@ -32,151 +32,121 @@
 #include <string.h>
 #include <unistd.h>
 
-#include "test.h"
-
-char *TCID = "madvise03";
+#include "tst_test.h"
 
 #ifdef MADV_REMOVE

Just remove this ifdef and add a fallback MADV_REMOVE definition into the include/lapi/mmap.h instead.

Quoted 118 lines
testcases/kernel/syscalls/madvise/madvise03.c +53 -46
-int TST_TOTAL = 3;
-
-static void setup(void);
-static void cleanup(void);
-static void check_and_print(char *advice);
-static long get_shmmax(void);
 
+static char filename[64];
 static int shmid1;
 
-int main(int argc, char *argv[])
+static void setup(void)
+{
+	sprintf(filename, "madvise03.%d", getpid());
+}
+
+static void check_and_print(char *advice)
+{
+	if (TEST_RETURN == -1) {
+		if (TEST_ERRNO == ENOTSUP && !strcmp(advice, "MADV_REMOVE")) {
+			tst_res(TCONF, "madvise MADV_REMOVE returned ENOTSUP"
+				 " CONFIG_TMPFS=y not in kernel .config?");
+			return;
+		}
+		tst_res(TFAIL,
+			 "madvise test for %s failed with "
+			 "return = %ld, errno = %d : %s",
+			 advice, TEST_RETURN, TEST_ERRNO, strerror(TEST_ERRNO));
+	} else {
+		tst_res(TPASS, "madvise test for %s PASSED", advice);
+	}
+}
+
+static long get_shmmax(void)
+{
+	long maxsize;
+
+	SAFE_FILE_SCANF("/proc/sys/kernel/shmmax", "%ld", &maxsize);
+
+	return maxsize;
+}
+
+static void verify_madvise(void)
 {
-	int lc, fd;
-	int i;
+	int i, fd;
 	char *file = NULL;
 	struct stat stat;
 	void *addr1;
 	long shm_size = 0;
-	char filename[64];
-	char *progname = NULL;
 	char *str_for_file = "abcdefghijklmnopqrstuvwxyz12345\n";
 
-	tst_parse_opts(argc, argv, NULL, NULL);
-
-	setup();
-
-	progname = *argv;
-	sprintf(filename, "%s-out.%d", progname, getpid());
-
-	for (lc = 0; TEST_LOOPING(lc); lc++) {
-		tst_count = 0;
-
-		fd = open(filename, O_RDWR | O_CREAT, 0664);
-		if (fd < 0)
-			tst_brkm(TBROK, cleanup, "open failed");
+	fd = open(filename, O_RDWR | O_CREAT, 0664);
+	if (fd < 0)
+		tst_brk(TBROK, "open failed");
 #ifdef DEBUG
-		tst_resm(TINFO, "filename = %s opened successfully", filename);
+	tst_res(TINFO, "filename = %s opened successfully", filename);
 #endif
 
-		/* Writing 40 KB of random data into this file
-		   [32 * 1280 = 40960] */
-		for (i = 0; i < 1280; i++)
-			if (write(fd, str_for_file, strlen(str_for_file)) == -1)
-				tst_brkm(TBROK | TERRNO, cleanup,
-					 "write failed");
+	/* Writing 40 KB of random data into this file
+	   [32 * 1280 = 40960] */
+	for (i = 0; i < 1280; i++)
+		if (write(fd, str_for_file, strlen(str_for_file)) == -1)
+			tst_brk(TBROK | TERRNO, "write failed");
 
-		if (fstat(fd, &stat) == -1)
-			tst_brkm(TBROK, cleanup, "fstat failed");
+	if (fstat(fd, &stat) == -1)
+		tst_brk(TBROK, "fstat failed");
 
-		file = mmap(NULL, stat.st_size, PROT_READ, MAP_SHARED, fd, 0);
-		if (file == MAP_FAILED)
-			tst_brkm(TBROK | TERRNO, cleanup, "mmap failed");
+	file = SAFE_MMAP(NULL, stat.st_size, PROT_READ, MAP_SHARED, fd, 0);
 
-		/* Allocate shared memory segment */
-		shm_size = get_shmmax();
+	/* Allocate shared memory segment */
+	shm_size = get_shmmax();
 
 #define min(a, b) ((a) < (b) ? @ : (b))
-		shmid1 = shmget(IPC_PRIVATE, min(1024 * 1024, shm_size),
-				IPC_CREAT | IPC_EXCL | 0701);
-		if (shmid1 == -1)
-			tst_brkm(TBROK, cleanup, "shmget failed");
+	shmid1 = shmget(IPC_PRIVATE, min(1024 * 1024, shm_size),
+			IPC_CREAT | IPC_EXCL | 0701);
+	if (shmid1 == -1)
+		tst_brk(TBROK, "shmget failed");
 
-		/* Attach shared memory segment to an address selected by the system */
-		addr1 = shmat(shmid1, NULL, 0);
-		if (addr1 == (void *)-1)
-			tst_brkm(TBROK, cleanup, "shmat error");
+	/* Attach shared memory segment to an address selected by the system */

testcases/kernel/syscalls/madvise/madvise03.c line 117

Quoted 10 lines
testcases/kernel/syscalls/madvise/madvise03.c +6 -3
+	addr1 = shmat(shmid1, NULL, 0);
+	if (addr1 == (void *)-1)
+		tst_brk(TBROK, "shmat error");
 
-		/* (1) Test case for MADV_REMOVE */
-		TEST(madvise(addr1, 4096, MADV_REMOVE));
-		check_and_print("MADV_REMOVE");
+	/* (1) Test case for MADV_REMOVE */
+	TEST(madvise(addr1, 4096, MADV_REMOVE));
+	check_and_print("MADV_REMOVE");

You don't need the shm memory for MADV_REMOVE, just mapping mapped PROT_WRITE as well.

Quoted 12 lines
testcases/kernel/syscalls/madvise/madvise03.c +5 -6
-		/* (2) Test case for MADV_DONTFORK */
-		TEST(madvise(file, (stat.st_size / 2), MADV_DONTFORK));
-		check_and_print("MADV_DONTFORK");
+	/* (2) Test case for MADV_DONTFORK */
+	TEST(madvise(file, (stat.st_size / 2), MADV_DONTFORK));
+	check_and_print("MADV_DONTFORK");
 
-		/* (3) Test case for MADV_DOFORK */
-		TEST(madvise(file, (stat.st_size / 2), MADV_DOFORK));
-		check_and_print("MADV_DOFORK");
+	/* (3) Test case for MADV_DOFORK */
+	TEST(madvise(file, (stat.st_size / 2), MADV_DOFORK));

testcases/kernel/syscalls/madvise/madvise03.c line 124

Quoted 10 lines
testcases/kernel/syscalls/madvise/madvise03.c +3 -5
+	check_and_print("MADV_DOFORK");
 
-		/* Finally Unmapping the whole file */
-		if (munmap(file, stat.st_size) < 0)
-			tst_brkm(TBROK | TERRNO, cleanup, "munmap failed");
+	/* Finally Unmapping the whole file */
+	SAFE_MUNMAP(file, stat.st_size);
 
-		close(fd);
-	}

I also wonder if these testcases could be moved to madvise01.

Signature · 2 lines
Cyril Hrubis
[email protected]
author
#9 lore raw

PATCH 3/6 syscalls/madvise03: Convert to new test API

Quoted 3 lines · Cyril Hrubis

Hi!

I also wonder if these testcases could be moved to madvise01.

Good idea! I'd like to merge madvise0[3|4] into madvise01. But for madvise02.c, there probably should be taken more consideration. If that mix up the codes, I'd prefer to keep it individual.

Additionally, thanks for the comments upon, all accepted.

Li Wang

Quoted 4 lines
Signature · 3 lines
-- 
Cyril Hrubis
[email protected]
author
#10 lore raw

PATCH 3/6 syscalls/madvise03: Convert to new test API

In reply to Cyril Hrubis

Hi Cyril,

Quoted 6 lines · Cyril Hrubis

testcases/kernel/syscalls/madvise/madvise03.c line 117

Quoted 3 lines
testcases/kernel/syscalls/madvise/madvise03.c +3 -0
+	/* (1) Test case for MADV_REMOVE */
+	TEST(madvise(addr1, 4096, MADV_REMOVE));
+	check_and_print("MADV_REMOVE");

You don't need the shm memory for MADV_REMOVE, just mapping mapped PROT_WRITE as well.

There are different ways to support this on different kernels. If just mapping mmapped as 'RW' shared memory, it will not work on kernel < 3.5. It seems like we have to keep the orignal method which based on shmfs.

From Linux Programmer's Manual:
"MADV_REMOVE (since Linux 2.6.16)
Free up a given range of pages and its associated backing store. This is equivalent to punching a hole in the corresponding byte range of the backing store (see fallocate(2)). Subsequent accesses in the specified address range will see bytes containing zero.

The specified address range must be mapped shared and writable. This flag cannot be applied to locked pages, Huge TLB pages, or VM_PFNMAP pages.

In the initial implementation, only shmfs/tmpfs supported MADV_REMOVE; but since Linux 3.5, any filesystem which supports the fallocate(2) FALLOC_FL_PUNCH_HOLE mode also supports MADV_REMOVE. Hugetlbfs will fail with the error EINVAL and other filesystems fail with the error EOPNOTSUPP."

A simple way is to create the mmaped file under '/dev/shm/' in setup()

	sprintf(filename, "/dev/shm/madvise01.%d", getpid());

and remove it in cleanup(). Or using tmpfs.

What do you think?

Regards,
Li Wang

Quoted 5 lines
Signature · 3 lines
-- 
Cyril Hrubis
[email protected]
#11 lore raw

PATCH 3/6 syscalls/madvise03: Convert to new test API

Hi!

Quoted 23 lines

There are different ways to support this on different kernels. If just mapping mmapped as 'RW' shared memory, it will not work on kernel < 3.5. It seems like we have to keep the orignal method which based on shmfs.

From Linux Programmer's Manual:
"MADV_REMOVE (since Linux 2.6.16)
Free up a given range of pages and its associated backing store. This is equivalent to punching a hole in the corresponding byte range of the backing store (see fallocate(2)). Subsequent accesses in the specified address range will see bytes containing zero.

The specified address range must be mapped shared and writable. This flag cannot be applied to locked pages, Huge TLB pages, or VM_PFNMAP pages.

In the initial implementation, only shmfs/tmpfs supported MADV_REMOVE; but since Linux 3.5, any filesystem which supports the fallocate(2) FALLOC_FL_PUNCH_HOLE mode also supports MADV_REMOVE. Hugetlbfs will fail with the error EINVAL and other filesystems fail with the error EOPNOTSUPP."

Quoted 5 lines

A simple way is to create the mmaped file under '/dev/shm/' in setup()

	sprintf(filename, "/dev/shm/madvise01.%d", getpid());

and remove it in cleanup(). Or using tmpfs.

What do you think?

Or we may use the POSIX IPC instead of the System V one. And if we do shm_open(), ftruncate(), mmap(), shm_unlink(), and close() in setup() everything will be freed once the mapping is released which will happen automatically when the test exits. Technically this is the same as opening file in /dev/shm/ but I would rather use the shm_open() instead.

Signature · 2 lines
Cyril Hrubis
[email protected]
#12 lore raw

PATCH 2/6 syscalls/madvise02: Convert to new test API

In reply to Li Wang

Hi!

Quoted 21 lines

static void check_and_print(int expected_errno)
{

testcases/kernel/syscalls/madvise/madvise02.c +3 -3
 	if (TEST_RETURN == -1) {
 		if (TEST_ERRNO == expected_errno)
-			tst_resm(TPASS | TTERRNO, "failed as expected");
+			tst_res(TPASS | TTERRNO, "failed as expected");
 		else
-			tst_resm(TFAIL | TTERRNO,
+			tst_res(TFAIL | TTERRNO,
 				 "failed unexpectedly; expected - %d : %s",
 				 expected_errno, strerror(expected_errno));
 	} else {
-		tst_resm(TFAIL, "madvise succeeded unexpectedly");
+		tst_res(TFAIL, "madvise succeeded unexpectedly");
 	}
 }
@@ -166,26 +125,26 @@ static void test_addr_einval(void)
 {
 	char *file;
 

testcases/kernel/syscalls/madvise/madvise02.c static void test_addr_einval(void) · line 135

Quoted 10 lines
testcases/kernel/syscalls/madvise/madvise02.c +2 -2
-	file = SAFE_MMAP(cleanup, 0, st.st_size, PROT_READ,
+	file = SAFE_MMAP(0, st.st_size, PROT_READ,
 					 MAP_SHARED, fd, 0);
 
 	TEST(madvise(file + 100, st.st_size, MADV_NORMAL));
 	check_and_print(EINVAL);
 
-	SAFE_MUNMAP(cleanup, file, st.st_size);
+	SAFE_MUNMAP(file, st.st_size);
 }

There is no readson to mmap the and unmap the file for each test, it should be done once in the setup.

Also the testcases could be easily converted to a aray of structures describing the test with one verify function taking a pointer to a testcase describing structure.

Quoted 95 lines
testcases/kernel/syscalls/madvise/madvise02.c +14 -14
 static void test_advice_einval(void)
 {
 	char *file;
 
-	file = SAFE_MMAP(cleanup, 0, st.st_size, PROT_READ,
+	file = SAFE_MMAP(0, st.st_size, PROT_READ,
 					 MAP_SHARED, fd, 0);
 
 	TEST(madvise(file, st.st_size, 1212));
 	check_and_print(EINVAL);
 
-	SAFE_MUNMAP(cleanup, file, st.st_size);
+	SAFE_MUNMAP(file, st.st_size);
 }
 
 #if !defined(UCLINUX)
@@ -193,16 +152,16 @@ static void test_lock_einval(void)
 {
 	char *file;
 
-	file = SAFE_MMAP(cleanup, 0, st.st_size, PROT_READ,
+	file = SAFE_MMAP(0, st.st_size, PROT_READ,
 					 MAP_SHARED, fd, 0);
 
 	if (mlock(file, st.st_size) < 0)
-		tst_brkm(TBROK | TERRNO, cleanup, "mlock failed");
+		tst_brk(TBROK | TERRNO, "mlock failed");
 
 	TEST(madvise(file, st.st_size, MADV_DONTNEED));
 	check_and_print(EINVAL);
 
-	SAFE_MUNMAP(cleanup, file, st.st_size);
+	SAFE_MUNMAP(file, st.st_size);
 }
 #endif /* if !defined(UCLINUX) */
 
@@ -212,18 +171,18 @@ static void test_mergeable_einval(void)
 	char *file;
 
 	if (access(KSM_SYS_DIR, F_OK) >= 0) {
-		tst_resm(TCONF, "kernel configured with CONFIG_KSM, "
+		tst_res(TCONF, "kernel configured with CONFIG_KSM, "
 				 "skip EINVAL test for MADV_MERGEABLE.");
 		return;
 	}
 
-	file = SAFE_MMAP(cleanup, 0, st.st_size, PROT_READ,
-					 MAP_SHARED, fd, 0);
+	file = SAFE_MMAP(0, st.st_size, PROT_READ,
+				 MAP_SHARED, fd, 0);
 
 	TEST(madvise(file, st.st_size, MADV_MERGEABLE));
 	check_and_print(EINVAL);
 
-	SAFE_MUNMAP(cleanup, file, st.st_size);
+	SAFE_MUNMAP(file, st.st_size);
 }
 #endif
 
@@ -233,18 +192,18 @@ static void test_unmergeable_einval(void)
 	char *file;
 
 	if (access(KSM_SYS_DIR, F_OK) >= 0) {
-		tst_resm(TCONF, "kernel configured with CONFIG_KSM, "
+		tst_res(TCONF, "kernel configured with CONFIG_KSM, "
 				 "skip EINVAL test for MADV_UNMERGEABLE.");
 		return;
 	}
 
-	file = SAFE_MMAP(cleanup, 0, st.st_size, PROT_READ,
+	file = SAFE_MMAP(0, st.st_size, PROT_READ,
 					 MAP_SHARED, fd, 0);
 
 	TEST(madvise(file, st.st_size, MADV_UNMERGEABLE));
 	check_and_print(EINVAL);
 
-	SAFE_MUNMAP(cleanup, file, st.st_size);
+	SAFE_MUNMAP(file, st.st_size);
 }
 #endif
 
@@ -254,10 +213,10 @@ static void test_enomem(void)
 	char *high;
 	unsigned long len;
 
-	low = SAFE_MMAP(cleanup, 0, st.st_size / 2, PROT_READ,
+	low = SAFE_MMAP(0, st.st_size / 2, PROT_READ,
 					MAP_SHARED, fd, 0);
 
-	high = SAFE_MMAP(cleanup, 0, st.st_size / 2, PROT_READ,
+	high = SAFE_MMAP(0, st.st_size / 2, PROT_READ,
 					 MAP_SHARED, fd, st.st_size / 2);
 
 	/* Swap if necessary to make low < high */

testcases/kernel/syscalls/madvise/madvise02.c static void test_enomem(void) · line 232

Quoted 5 lines
testcases/kernel/syscalls/madvise/madvise02.c +1 -1
@@ -270,12 +229,12 @@ static void test_enomem(void)
 
 	len = (high - low) + pagesize;
 
-	SAFE_MUNMAP(cleanup, high, st.st_size / 2);
+	SAFE_MUNMAP(high, st.st_size / 2);

Well this whole part can be just done in two steps:

* mmap() the file as in the rest of the testcases
* unmap() last page of the mapping
* call madvise as in the rest of the testcases

testcases/kernel/syscalls/madvise/madvise02.c static void test_ebadf(void) · line 262

Quoted 12 lines
testcases/kernel/syscalls/madvise/madvise02.c +1 -1
 	TEST(madvise(low, len, MADV_NORMAL));
 	check_and_print(ENOMEM);
 
-	SAFE_MUNMAP(cleanup, low, st.st_size / 2);
+	SAFE_MUNMAP(low, st.st_size / 2);
 
 }
 
 static void test_ebadf(void)
@@ -302,9 +261,29 @@ static void test_ebadf(void)
 	 * prefretch.
 	 */

The malloc() mess here could be changed to mmap() with MAP_ANOYMOUS.

Quoted 34 lines
testcases/kernel/syscalls/madvise/madvise02.c +21 -1
 	} else {
-		tst_resm(TPASS, "madvise succeeded as expected, see "
+		tst_res(TPASS, "madvise succeeded as expected, see "
 				"kernel commit 1998cc0 for details.");
 	}
 
 	free(ptr_memory_allocated);
 }
+
+static void verify_madvise(unsigned int i)
+{
+	(*test_func[i])();
+}
+
+static void cleanup(void)
+{
+	if (fd && close(fd) < 0)
+		tst_res(TWARN | TERRNO, "close failed");
+}
+
+static struct tst_test test = {
+	.tid = "madvise02",
+	.tcnt = ARRAY_SIZE(test_func),
+	.test = verify_madvise,
+	.needs_tmpdir = 1,
+	.setup = setup,
+	.cleanup = cleanup,
+};
Signature · 5 lines
1.8.3.1


-- 
Mailing list info: https://lists.linux.it/listinfo/ltp
Signature · 2 lines
Cyril Hrubis
[email protected]
#13 lore raw

PATCH 1/6 syscalls/madvise01: Convert to new test API

In reply to Li Wang

Hi!

Quoted 45 lines
testcases/kernel/syscalls/madvise/madvise01.c +16 -16
@@ -32,107 +32,79 @@
 #include <string.h>
 #include <unistd.h>
 
-#include "test.h"
+#include "tst_test.h"
 
-static void setup(void);
-static void cleanup(void);
-static void check_and_print(char *advice);
+static char filename[64];
 
-char *TCID = "madvise01";
-int TST_TOTAL = 5;
+static void setup(void)
+{
+	sprintf(filename, "madvise01.%d", getpid());
+}
 
-int main(int argc, char *argv[])
+static void check_and_print(char *advice)
+{
+	if (TEST_RETURN == -1)
+		tst_res(TFAIL | TTERRNO, "madvise test for %s failed", advice);
+	else
+		tst_res(TPASS, "madvise test for %s PASSED", advice);
+}
+
+static void verify_madvise(void)
 {
-	int lc, fd;
 	int i = 0;
+	int fd;
 	char *file = NULL;
 	struct stat stat;
-	char filename[64];
-	char *progname = NULL;
 	char *str_for_file = "abcdefghijklmnopqrstuvwxyz12345\n";
 
-	tst_parse_opts(argc, argv, NULL, NULL);
-
-	setup();
-
-	progname = *argv;
-	sprintf(filename, "%s-out.%d", progname, getpid());

testcases/kernel/syscalls/madvise/madvise01.c line 62

Quoted 10 lines
testcases/kernel/syscalls/madvise/madvise01.c +3 -7
-
-	for (lc = 0; TEST_LOOPING(lc); lc++) {
-		tst_count = 0;
-
-		fd = open(filename, O_RDWR | O_CREAT, 0664);
-		if (fd < 0)
-			tst_brkm(TBROK | TERRNO, cleanup, "open failed");
+	fd = open(filename, O_RDWR | O_CREAT, 0664);
+	if (fd < 0)
+		tst_brk(TBROK | TERRNO, "open failed");

SAFE_OPEN()

Quoted 14 lines
testcases/kernel/syscalls/madvise/madvise01.c +1 -10
 #ifdef DEBUG
-		tst_resm(TINFO, "filename = %s opened successfully", filename);
+	tst_res(TINFO, "filename = %s opened successfully", filename);
 #endif
 
-		/* Writing 40 KB of random data into this file
-		   [32 * 1280 = 40960] */
-		for (i = 0; i < 1280; i++)
-			if (write(fd, str_for_file, strlen(str_for_file)) == -1)
-				tst_brkm(TBROK | TERRNO, cleanup,
-					 "write failed");
-
-		if (fstat(fd, &stat) == -1)
-			tst_brkm(TBROK, cleanup, "fstat failed");

testcases/kernel/syscalls/madvise/madvise01.c line 71

Quoted 10 lines
testcases/kernel/syscalls/madvise/madvise01.c +5 -5
-
-		/* Map the input file into memory */
-		file = mmap(NULL, stat.st_size, PROT_READ, MAP_SHARED, fd, 0);
-		if (file == MAP_FAILED)
-			tst_brkm(TBROK, cleanup, "mmap failed");
+	/* Writing 40 KB of random data into this file
+	   [32 * 1280 = 40960] */
+	for (i = 0; i < 1280; i++)
+		if (write(fd, str_for_file, strlen(str_for_file)) == -1)
+			tst_brk(TBROK | TERRNO, "write failed");

SAFE_WRITE()

testcases/kernel/syscalls/madvise/madvise01.c line 74

Quoted 5 lines
testcases/kernel/syscalls/madvise/madvise01.c +2 -3
-		/* (1) Test case for MADV_NORMAL */
-		TEST(madvise(file, stat.st_size, MADV_NORMAL));
-		check_and_print("MADV_NORMAL");
+	if (fstat(fd, &stat) == -1)
+		tst_brk(TBROK, "fstat failed");

SAFE_STAT()

testcases/kernel/syscalls/madvise/madvise01.c line 77

Quoted 5 lines
testcases/kernel/syscalls/madvise/madvise01.c +2 -3
-		/* (2) Test case for MADV_RANDOM */
-		TEST(madvise(file, stat.st_size, MADV_RANDOM));
-		check_and_print("MADV_RANDOM");
+	/* Map the input file into memory */
+	file = SAFE_MMAP(NULL, stat.st_size, PROT_READ, MAP_SHARED, fd, 0);

SAFE_MMAP()

Moreover the whole intialization up to this point should be done in setup().

Quoted 22 lines
testcases/kernel/syscalls/madvise/madvise01.c +9 -10
-		/* (3) Test case for MADV_SEQUENTIAL */
-		TEST(madvise(file, stat.st_size, MADV_SEQUENTIAL));
-		check_and_print("MADV_SEQUENTIAL");
+	/* (1) Test case for MADV_NORMAL */
+	TEST(madvise(file, stat.st_size, MADV_NORMAL));
+	check_and_print("MADV_NORMAL");
 
-		/* (4) Test case for MADV_WILLNEED */
-		TEST(madvise(file, stat.st_size, MADV_WILLNEED));
-		check_and_print("MADV_WILLNEED");
+	/* (2) Test case for MADV_RANDOM */
+	TEST(madvise(file, stat.st_size, MADV_RANDOM));
+	check_and_print("MADV_RANDOM");
 
-		/* (5) Test case for MADV_DONTNEED */
-		TEST(madvise(file, stat.st_size, MADV_DONTNEED));
-		check_and_print("MADV_DONTNEED");
+	/* (3) Test case for MADV_SEQUENTIAL */
+	TEST(madvise(file, stat.st_size, MADV_SEQUENTIAL));
+	check_and_print("MADV_SEQUENTIAL");
 
-		if (munmap(file, stat.st_size) == -1)

testcases/kernel/syscalls/madvise/madvise01.c line 97

Quoted 10 lines
testcases/kernel/syscalls/madvise/madvise01.c +6 -3
-			tst_brkm(TBROK | TERRNO, cleanup, "munmap failed");
+	/* (4) Test case for MADV_WILLNEED */
+	TEST(madvise(file, stat.st_size, MADV_WILLNEED));
+	check_and_print("MADV_WILLNEED");
 
-		close(fd);
-	}
+	/* (5) Test case for MADV_DONTNEED */
+	TEST(madvise(file, stat.st_size, MADV_DONTNEED));
+	check_and_print("MADV_DONTNEED");

This can be easily converted to an array of structures describing the test and exported as separate tests in the struct tst_test

testcases/kernel/syscalls/madvise/madvise01.c line 100

Quoted 8 lines
testcases/kernel/syscalls/madvise/madvise01.c +2 -6
-	cleanup();
-	tst_exit();
-}
-
-static void setup(void)
-{
+	if (munmap(file, stat.st_size) == -1)
+		tst_brk(TBROK | TERRNO, "munmap failed");

The munmap() should rather be done in cleanup but only if the file has been mmaped().

testcases/kernel/syscalls/madvise/madvise01.c line 102

Quoted 6 lines
testcases/kernel/syscalls/madvise/madvise01.c +1 -5
-	tst_sig(NOFORK, DEF_HANDLER, cleanup);
-
-	TEST_PAUSE;
-
-	tst_tmpdir();
+	close(fd);

The fd can be closed right after the mmap().

Quoted 27 lines
testcases/kernel/syscalls/madvise/madvise01.c +6 -13
 }
 
-static void cleanup(void)
-{
-	tst_rmdir();
-
-}
-
-static void check_and_print(char *advice)
-{
-	if (TEST_RETURN == -1)
-		tst_resm(TFAIL | TTERRNO, "madvise test for %s failed", advice);
-	else
-		tst_resm(TPASS, "madvise test for %s PASSED", advice);
-}
+static struct tst_test test = {
+	.tid = "madvise01",
+	.test_all = verify_madvise,
+	.needs_tmpdir = 1,
+	.setup = setup,
+};
Signature · 5 lines
1.8.3.1


-- 
Mailing list info: https://lists.linux.it/listinfo/ltp
Signature · 2 lines
Cyril Hrubis
[email protected]