public inbox for ltp@lists.linux.it
 help / color / mirror / Atom feed
* [LTP] [PATCH V3 1/3] ltp/lapi: add some madvices definition
@ 2016-05-25  6:29 Li Wang
  2016-05-25  6:29 ` [LTP] [PATCH V3 2/3] syscalls/madvise01: reconstruct and convert to new API Li Wang
  2016-06-01 14:22 ` [LTP] [PATCH V3 1/3] ltp/lapi: add some madvices definition Cyril Hrubis
  0 siblings, 2 replies; 6+ messages in thread
From: Li Wang @ 2016-05-25  6:29 UTC (permalink / raw)
  To: ltp

Signed-off-by: Li Wang <liwang@redhat.com>
---
 include/lapi/mmap.h | 32 ++++++++++++++++++++++++++++++++
 1 file changed, 32 insertions(+)

diff --git a/include/lapi/mmap.h b/include/lapi/mmap.h
index e8e5917..b121e43 100644
--- a/include/lapi/mmap.h
+++ b/include/lapi/mmap.h
@@ -23,4 +23,36 @@
 #define MAP_HUGETLB 0x40000
 #endif
 
+#ifndef MADV_REMOVE
+#define MADV_REMOVE 9
+#endif
+
+#ifndef MADV_DONTFORK
+#define MADV_DONTFORK 10
+#define MADV_DOFORK   11
+#endif
+
+#ifndef MADV_HWPOISON
+#define MADV_HWPOISON 100
+#endif
+
+#ifndef MADV_SOFT_OFFLINE
+#define MADV_SOFT_OFFLINE 101
+#endif
+
+#ifndef MADV_MERGEABLE
+#define MADV_MERGEABLE   12
+#define MADV_UNMERGEABLE 13
+#endif
+
+#ifndef MADV_HUGEPAGE
+#define MADV_HUGEPAGE   14
+#define MADV_NOHUGEPAGE 15
+#endif
+
+#ifndef MADV_DONTDUMP
+#define MADV_DONTDUMP 16
+#define MADV_DODUMP   17
+#endif
+
 #endif /* __LAPI_MMAP_H__ */
-- 
1.8.3.1


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

* [LTP] [PATCH V3 2/3] syscalls/madvise01: reconstruct and convert to new API
  2016-05-25  6:29 [LTP] [PATCH V3 1/3] ltp/lapi: add some madvices definition Li Wang
@ 2016-05-25  6:29 ` Li Wang
  2016-05-25  6:29   ` [LTP] [PATCH V3 3/3] syscalls/madvise02: " Li Wang
  2016-06-01 14:24   ` [LTP] [PATCH V3 2/3] syscalls/madvise01: " Cyril Hrubis
  2016-06-01 14:22 ` [LTP] [PATCH V3 1/3] ltp/lapi: add some madvices definition Cyril Hrubis
  1 sibling, 2 replies; 6+ messages in thread
From: Li Wang @ 2016-05-25  6:29 UTC (permalink / raw)
  To: ltp

* take use of some SAFE Marcos
* move the intialization operations to setup()
* merge madvise0[3|4] into madvise01
* create the test file on tmpfs
* create new array madvise_opt[]
* add "MADV_HWPOISON, MADV_HUGEPAGE, MADV_NOHUGEPAGE
  MADV_MERGEABLE, MADV_UNMERGEABLE" in test option

Signed-off-by: Li Wang <liwang@redhat.com>
---
 runtest/syscalls                              |   2 -
 testcases/kernel/syscalls/madvise/madvise01.c | 194 ++++++++++++--------------
 testcases/kernel/syscalls/madvise/madvise03.c | 182 ------------------------
 testcases/kernel/syscalls/madvise/madvise04.c | 138 ------------------
 4 files changed, 92 insertions(+), 424 deletions(-)
 delete mode 100644 testcases/kernel/syscalls/madvise/madvise03.c
 delete mode 100644 testcases/kernel/syscalls/madvise/madvise04.c

diff --git a/runtest/syscalls b/runtest/syscalls
index 6af3dad..96f398d 100644
--- a/runtest/syscalls
+++ b/runtest/syscalls
@@ -741,8 +741,6 @@ mincore02 mincore02
 
 madvise01 madvise01
 madvise02 madvise02
-madvise03 madvise03
-madvise04 madvise04
 madvise05 madvise05
 madvise06 madvise06
 
diff --git a/testcases/kernel/syscalls/madvise/madvise01.c b/testcases/kernel/syscalls/madvise/madvise01.c
index 4b1286d..dc08bcd 100644
--- a/testcases/kernel/syscalls/madvise/madvise01.c
+++ b/testcases/kernel/syscalls/madvise/madvise01.c
@@ -1,20 +1,16 @@
 /*
  *  Copyright (c) International Business Machines  Corp., 2004
- *  Copyright (c) Linux Test Project, 2013
+ *  Copyright (c) Linux Test Project, 2013-2016
  *
- *  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 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 Library 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.
+ * 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.
  */
 
 /*
@@ -26,113 +22,107 @@
 #include <sys/types.h>
 #include <sys/mman.h>
 #include <sys/stat.h>
+#include <sys/mount.h>
 #include <errno.h>
 #include <fcntl.h>
 #include <stdio.h>
 #include <string.h>
 #include <unistd.h>
 
-#include "test.h"
+#include "tst_test.h"
+#include "tst_kvercmp.h"
+#include "lapi/mmap.h"
+
+#define TMP_DIR "tmp_madvise"
+#define TEST_FILE TMP_DIR"/testfile"
+#define KSM_SYS_DIR "/sys/kernel/mm/ksm"
+#define STR "abcdefghijklmnopqrstuvwxyz12345\n"
+
+static char *sfile;
+static char *pfile;
+static struct stat st;
+
+static struct tcase {
+	int advice;
+	char *name;
+	char **addr;
+} tcases[] = {
+	{MADV_NORMAL,      "MADV_NORMAL",      &sfile},
+	{MADV_RANDOM,      "MADV_RANDOM",      &sfile},
+	{MADV_SEQUENTIAL,  "MADV_SEQUENTIAL",  &sfile},
+	{MADV_WILLNEED,    "MADV_WILLNEED",    &sfile},
+	{MADV_DONTNEED,    "MADV_DONTNEED",    &sfile},
+	{MADV_REMOVE,      "MADV_REMOVE",      &sfile}, /* since Linux 2.6.16 */
+	{MADV_DONTFORK,    "MADV_DONTFORK",    &sfile}, /* since Linux 2.6.16 */
+	{MADV_DOFORK,      "MADV_DOFORK",      &sfile}, /* since Linux 2.6.16 */
+	{MADV_HWPOISON,    "MADV_HWPOISON",    &sfile}, /* since Linux 2.6.32 */
+	{MADV_MERGEABLE,   "MADV_MERGEABLE",   &sfile}, /* since Linux 2.6.32 */
+	{MADV_UNMERGEABLE, "MADV_UNMERGEABLE", &sfile}, /* since Linux 2.6.32 */
+	{MADV_HUGEPAGE,    "MADV_HUGEPAGE",    &pfile}, /* since Linux 2.6.38 */
+	{MADV_NOHUGEPAGE,  "MADV_NOHUGEPAGE",  &pfile}, /* since Linux 2.6.38 */
+	{MADV_DONTDUMP,    "MADV_DONTDUMP",    &sfile}, /* since Linux 3.4 */
+	{MADV_DODUMP,      "MADV_DODUMP",      &sfile}  /* since Linux 3.4 */
+};
 
-static void setup(void);
-static void cleanup(void);
-static void check_and_print(char *advice);
+static void setup(void)
+{
+	unsigned int i;
+	int fd;
 
-char *TCID = "madvise01";
-int TST_TOTAL = 5;
+	SAFE_MKDIR(TMP_DIR, 0664);
+	SAFE_MOUNT(TMP_DIR, TMP_DIR, "tmpfs", 0, NULL);
 
-int main(int argc, char *argv[])
-{
-	int lc, fd;
-	int i = 0;
-	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");
-#ifdef DEBUG
-		tst_resm(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");
-
-		/* (1) Test case for MADV_NORMAL */
-		TEST(madvise(file, stat.st_size, MADV_NORMAL));
-		check_and_print("MADV_NORMAL");
-
-		/* (2) Test case for MADV_RANDOM */
-		TEST(madvise(file, stat.st_size, MADV_RANDOM));
-		check_and_print("MADV_RANDOM");
-
-		/* (3) Test case for MADV_SEQUENTIAL */
-		TEST(madvise(file, stat.st_size, MADV_SEQUENTIAL));
-		check_and_print("MADV_SEQUENTIAL");
-
-		/* (4) Test case for MADV_WILLNEED */
-		TEST(madvise(file, stat.st_size, MADV_WILLNEED));
-		check_and_print("MADV_WILLNEED");
-
-		/* (5) Test case for MADV_DONTNEED */
-		TEST(madvise(file, stat.st_size, MADV_DONTNEED));
-		check_and_print("MADV_DONTNEED");
-
-		if (munmap(file, stat.st_size) == -1)
-			tst_brkm(TBROK | TERRNO, cleanup, "munmap failed");
-
-		close(fd);
-	}
-
-	cleanup();
-	tst_exit();
-}
+	fd = SAFE_OPEN(TEST_FILE, O_RDWR | O_CREAT, 0664);
 
-static void setup(void)
-{
+	/* Writing 40 KB of random data into this file [32 * 1280 = 40960] */
+	for (i = 0; i < 1280; i++)
+		SAFE_WRITE(1, fd, STR, strlen(STR));
+
+	SAFE_FSTAT(fd, &st);
 
-	tst_sig(NOFORK, DEF_HANDLER, cleanup);
+	/* Map the input file into shared memory */
+	sfile = SAFE_MMAP(NULL, st.st_size,
+			PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
 
-	TEST_PAUSE;
+	/* Map the input file into private memory. MADV_HUGEPAGE only works
+	 * with private anonymous pages */
+	pfile = SAFE_MMAP(NULL, st.st_size,
+			PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, fd, 0);
 
-	tst_tmpdir();
+	SAFE_CLOSE(fd);
 }
 
 static void cleanup(void)
 {
-	tst_rmdir();
-
+	munmap(sfile, st.st_size);
+	munmap(pfile, st.st_size);
+	umount(TMP_DIR);
+	rmdir(TMP_DIR);
 }
 
-static void check_and_print(char *advice)
+static void verify_madvise(unsigned int i)
 {
-	if (TEST_RETURN == -1)
-		tst_resm(TFAIL | TTERRNO, "madvise test for %s failed", advice);
-	else
-		tst_resm(TPASS, "madvise test for %s PASSED", advice);
+	struct tcase *tc = &tcases[i];
+
+	TEST(madvise(*(tc->addr), st.st_size, tc->advice));
+
+	if (TEST_RETURN == -1) {
+		if (TEST_ERRNO == EINVAL)
+			tst_res(TCONF, "%s is not supported", tc->name);
+		else
+			tst_res(TFAIL, "madvise test for %s failed with "
+					"return = %ld, errno = %d : %s",
+					tc->name, TEST_RETURN, TEST_ERRNO,
+					tst_strerrno(TFAIL | TTERRNO));
+	} else
+		tst_res(TPASS, "madvise test for %s PASSED", tc->name);
 }
+
+static struct tst_test test = {
+	.tid = "madvise01",
+	.tcnt = ARRAY_SIZE(tcases),
+	.test = verify_madvise,
+	.needs_tmpdir = 1,
+	.setup = setup,
+	.cleanup = cleanup,
+};
diff --git a/testcases/kernel/syscalls/madvise/madvise03.c b/testcases/kernel/syscalls/madvise/madvise03.c
deleted file mode 100644
index 3da2bb2..0000000
--- a/testcases/kernel/syscalls/madvise/madvise03.c
+++ /dev/null
@@ -1,182 +0,0 @@
-/*
- *  Copyright (c) International Business Machines  Corp., 2004
- *  Copyright (c) 2012 Cyril Hrubis <chrubis@suse.cz>
- *
- *  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 Library 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.
- */
-
-/*
- * This is a test case for madvise(2) system call. No error should be returned.
- */
-
-#include <sys/types.h>
-#include <sys/mman.h>
-#include <sys/shm.h>
-#include <sys/stat.h>
-#include <errno.h>
-#include <fcntl.h>
-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
-#include <unistd.h>
-
-#include "test.h"
-
-char *TCID = "madvise03";
-
-#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 int shmid1;
-
-int main(int argc, char *argv[])
-{
-	int lc, fd;
-	int i;
-	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");
-#ifdef DEBUG
-		tst_resm(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");
-
-		file = mmap(NULL, stat.st_size, PROT_READ, MAP_SHARED, fd, 0);
-		if (file == MAP_FAILED)
-			tst_brkm(TBROK | TERRNO, cleanup, "mmap failed");
-
-		/* 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");
-
-		/* 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");
-
-		/* (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");
-
-		/* (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");
-
-		close(fd);
-	}
-
-	cleanup();
-	tst_exit();
-}
-
-static void setup(void)
-{
-
-	tst_sig(NOFORK, DEF_HANDLER, cleanup);
-
-	TEST_PAUSE;
-
-	tst_tmpdir();
-}
-
-static void cleanup(void)
-{
-	if (shmid1 != -1)
-		if (shmctl(shmid1, IPC_RMID, 0) < 0)
-			tst_resm(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);
-
-	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");
-}
-#endif
diff --git a/testcases/kernel/syscalls/madvise/madvise04.c b/testcases/kernel/syscalls/madvise/madvise04.c
deleted file mode 100644
index e89babc..0000000
--- a/testcases/kernel/syscalls/madvise/madvise04.c
+++ /dev/null
@@ -1,138 +0,0 @@
-/*
- *  Copyright (C) 2012 FUJITSU LIMITED.
- *
- *  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 Library 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.
- */
-
-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
-#include <errno.h>
-#include <sys/types.h>
-#include <sys/stat.h>
-#include <unistd.h>
-#include <sys/shm.h>
-#include <sys/mman.h>
-#include <fcntl.h>
-
-#include "test.h"
-
-char *TCID = "madvise04";
-
-#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);
-
-int main(int argc, char *argv[])
-{
-	int lc, fd;
-	int i;
-	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");
-#ifdef DEBUG
-		tst_resm(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");
-
-		file = mmap(NULL, stat.st_size, PROT_READ, MAP_SHARED, fd, 0);
-		if (file == MAP_FAILED)
-			tst_brkm(TBROK | TERRNO, cleanup, "mmap failed");
-
-		/* (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");
-
-		/* Finally Unmapping the whole file */
-		if (munmap(file, stat.st_size) < 0)
-			tst_brkm(TBROK | TERRNO, cleanup, "munmap failed");
-
-		close(fd);
-	}
-
-	cleanup();
-	tst_exit();
-}
-
-static void setup(void)
-{
-	tst_sig(NOFORK, DEF_HANDLER, cleanup);
-	TEST_PAUSE;
-	tst_tmpdir();
-}
-
-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);
-	}
-}
-
-#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");
-}
-#endif
-- 
1.8.3.1


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

* [LTP] [PATCH V3 3/3] syscalls/madvise02: reconstruct and convert to new API
  2016-05-25  6:29 ` [LTP] [PATCH V3 2/3] syscalls/madvise01: reconstruct and convert to new API Li Wang
@ 2016-05-25  6:29   ` Li Wang
  2016-06-06 13:03     ` Cyril Hrubis
  2016-06-01 14:24   ` [LTP] [PATCH V3 2/3] syscalls/madvise01: " Cyril Hrubis
  1 sibling, 1 reply; 6+ messages in thread
From: Li Wang @ 2016-05-25  6:29 UTC (permalink / raw)
  To: ltp

* take use of some SAFE Marcos
* move the intialization operations to setup()
* create new struct array madvise_opt[]
* add enable as a global variable

Signed-off-by: Li Wang <liwang@redhat.com>
---
 testcases/kernel/syscalls/madvise/madvise02.c | 365 +++++++++-----------------
 1 file changed, 127 insertions(+), 238 deletions(-)

diff --git a/testcases/kernel/syscalls/madvise/madvise02.c b/testcases/kernel/syscalls/madvise/madvise02.c
index b9eb77d..2c7ecce 100644
--- a/testcases/kernel/syscalls/madvise/madvise02.c
+++ b/testcases/kernel/syscalls/madvise/madvise02.c
@@ -1,20 +1,16 @@
 /*
  *  Copyright (c) International Business Machines  Corp., 2004
- *  Copyright (c) Linux Test Project, 2013-2014
+ *  Copyright (c) Linux Test Project, 2013-2016
  *
- *  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 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 Library 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.
+ * 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.
  */
 
 /*
@@ -48,263 +44,156 @@
 #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"
+#include "lapi/mmap.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)
-static void test_lock_einval(void);
-#endif /* if !defined(UCLINUX) */
-#if defined(MADV_MERGEABLE)
-static void test_mergeable_einval(void);
-#endif
-#if defined(MADV_UNMERGEABLE)
-static void test_unmergeable_einval(void);
-#endif
-static void test_enomem(void);
-static void test_ebadf(void);
-
-static void (*test_func[])(void) = {
-	test_addr_einval,
-	test_advice_einval,
-#if !defined(UCLINUX)
-	test_lock_einval,
-#endif /* if !defined(UCLINUX) */
-#if defined(MADV_MERGEABLE)
-	test_mergeable_einval,
-#endif
-#if defined(MADV_UNMERGEABLE)
-	test_unmergeable_einval,
-#endif
-	test_enomem,
-	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 int enable;
+static char *file;
+static char *tmp_file;
+static char *ptr_addr;
+static char *tmp_addr;
+
+static struct tcase {
+	int advice;
+	char *name;
+	char **addr;
+	int exp_errno;
+} tcases[] = {
+	{MADV_NORMAL,      "MADV_NORMAL",      &file, EINVAL},
+	{1212,             "MADV_NORMAL",      &file, EINVAL},
+	{MADV_DONTNEED,    "MADV_DONTNEED",    &file, EINVAL},
+	{MADV_MERGEABLE,   "MADV_MERGEABLE",   &file, EINVAL},
+	{MADV_UNMERGEABLE, "MADV_UNMERGEABLE", &file, EINVAL},
+	{MADV_WILLNEED,    "MADV_WILLNEED,",   &file,  EBADF},
+	{MADV_NORMAL,      "MADV_NORMAL",      &file, ENOMEM}
+};
 
 static void setup(void)
 {
-	int i;
-
-	tst_sig(NOFORK, DEF_HANDLER, cleanup);
+	int i, fd;
 
-	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);
-}
+	SAFE_FSTAT(fd, &st);
 
-static void cleanup(void)
-{
-	if (fd && close(fd) < 0)
-		tst_resm(TWARN | TERRNO, "close failed");
+	file = SAFE_MMAP(0, st.st_size, PROT_READ, MAP_SHARED, fd, 0);
 
-	tst_rmdir();
-}
+	tmp_file = file;
 
-static void check_and_print(int expected_errno)
-{
-	if (TEST_RETURN == -1) {
-		if (TEST_ERRNO == expected_errno)
-			tst_resm(TPASS | TTERRNO, "failed as expected");
-		else
-			tst_resm(TFAIL | TTERRNO,
-				 "failed unexpectedly; expected - %d : %s",
-				 expected_errno, strerror(expected_errno));
-	} else {
-		tst_resm(TFAIL, "madvise succeeded unexpectedly");
-	}
+	SAFE_CLOSE(fd);
 }
 
-static void test_addr_einval(void)
+static void tcases_filter(unsigned int i)
 {
-	char *file;
-
-	file = SAFE_MMAP(cleanup, 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);
-}
-
-static void test_advice_einval(void)
-{
-	char *file;
-
-	file = SAFE_MMAP(cleanup, 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);
-}
-
-#if !defined(UCLINUX)
-static void test_lock_einval(void)
-{
-	char *file;
-
-	file = SAFE_MMAP(cleanup, 0, st.st_size, PROT_READ,
-					 MAP_SHARED, fd, 0);
-
-	if (mlock(file, st.st_size) < 0)
-		tst_brkm(TBROK | TERRNO, cleanup, "mlock failed");
-
-	TEST(madvise(file, st.st_size, MADV_DONTNEED));
-	check_and_print(EINVAL);
-
-	SAFE_MUNMAP(cleanup, file, st.st_size);
-}
-#endif /* if !defined(UCLINUX) */
-
-#if defined(MADV_MERGEABLE)
-static void test_mergeable_einval(void)
-{
-	char *file;
-
-	if (access(KSM_SYS_DIR, F_OK) >= 0) {
-		tst_resm(TCONF, "kernel configured with CONFIG_KSM, "
-				 "skip EINVAL test for MADV_MERGEABLE.");
-		return;
+	enable = 1;
+	file = tmp_file;
+
+	switch (i) {
+	case 0:
+		/* Test for addr EINVAL */
+		file = tmp_file + 100;
+		break;
+
+	case 1:
+		/* Test for advice EINVAL */
+		break;
+
+	case 2:
+		enable = 0;
+		/* Test for lock EINVAL */
+		#if !defined(UCLINUX)
+		if (mlock(file, st.st_size) < 0)
+			tst_brk(TBROK | TERRNO, "mlock failed");
+		enable = 1;
+		#endif /* if !defined(UCLINUX) */
+		break;
+
+	case 3:
+	case 4:
+		/* Test for MADV_MERGEABLE EINVAL */
+		if ((tst_kvercmp(2, 6, 32)) < 0)
+			enable = 0;
+
+		if (access(KSM_SYS_DIR, F_OK) == 0)
+			/* kernel configured with CONFIG_KSM,
+			 * skip EINVAL test for MADV_MERGEABLE. */
+			enable = 0;
+		break;
+
+	case 5:
+		/* Test for MADV_WILLNEED EBADF */
+		ptr_addr = SAFE_MALLOC(st.st_size);
+		/* Take temporary pointer for later use, freeing up the
+		 * original one. */
+		tmp_addr = ptr_addr;
+		tmp_addr =
+			(char *)(((unsigned long)tmp_addr + pagesize - 1) & ~(pagesize - 1));
+		file = tmp_addr;
+
+		/* In kernel commit 1998cc0, madvise(MADV_WILLNEED) to
+		 * anon mem doesn't return -EBADF now, as now we support
+		 * swap prefretch. */
+		if ((tst_kvercmp(3, 9, 0)) > 0)
+			enable = 0;
+		break;
+
+	case 6:
+		/* Test for MADV_NORMAL ENOMEM */
+		SAFE_MUNMAP(file + st.st_size - pagesize, pagesize);
+		break;
 	}
-
-	file = SAFE_MMAP(cleanup, 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);
 }
-#endif
 
-#if defined(MADV_UNMERGEABLE)
-static void test_unmergeable_einval(void)
+static void advice_test(unsigned int i)
 {
-	char *file;
-
-	if (access(KSM_SYS_DIR, F_OK) >= 0) {
-		tst_resm(TCONF, "kernel configured with CONFIG_KSM, "
-				 "skip EINVAL test for MADV_UNMERGEABLE.");
-		return;
+	struct tcase *tc = &tcases[i];
+
+	tcases_filter(i);
+
+	if (enable == 1) {
+		TEST(madvise(*(tc->addr), st.st_size, tc->advice));
+		if (TEST_RETURN == -1)
+			if (TEST_ERRNO == tc->exp_errno)
+				tst_res(TPASS | TTERRNO, "failed as expected");
+			else
+				tst_res(TFAIL | TTERRNO,
+						"failed unexpectedly; expected - %d : %s",
+						tc->exp_errno, tst_strerrno(TFAIL | TTERRNO));
+		else
+			tst_res(TFAIL, "madvise succeeded unexpectedly");
+	} else {
+		tst_res(TCONF, "%s is not supported", tc->name);
 	}
-
-	file = SAFE_MMAP(cleanup, 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);
 }
-#endif
 
-static void test_enomem(void)
+static void cleanup(void)
 {
-	char *low;
-	char *high;
-	unsigned long len;
-
-	low = SAFE_MMAP(cleanup, 0, st.st_size / 2, PROT_READ,
-					MAP_SHARED, fd, 0);
-
-	high = SAFE_MMAP(cleanup, 0, st.st_size / 2, PROT_READ,
-					 MAP_SHARED, fd, st.st_size / 2);
-
-	/* Swap if necessary to make low < high */
-	if (low > high) {
-		char *tmp;
-		tmp = high;
-		high = low;
-		low = tmp;
-	}
-
-	len = (high - low) + pagesize;
-
-	SAFE_MUNMAP(cleanup, high, st.st_size / 2);
-
-	TEST(madvise(low, len, MADV_NORMAL));
-	check_and_print(ENOMEM);
-
-	SAFE_MUNMAP(cleanup, low, st.st_size / 2);
+	free(ptr_addr);
+	munmap(file, st.st_size - pagesize);
 }
 
-static void test_ebadf(void)
-{
-	char *ptr_memory_allocated = NULL;
-	char *tmp_memory_allocated = NULL;
-
-	/* Create one memory segment using malloc */
-	ptr_memory_allocated = malloc(5 * pagesize);
-	/*
-	 * Take temporary pointer for later use, freeing up the
-	 * original one.
-	 */
-	tmp_memory_allocated = ptr_memory_allocated;
-	tmp_memory_allocated =
-		(char *)(((unsigned long)tmp_memory_allocated +
-			pagesize - 1) & ~(pagesize - 1));
-
-	TEST(madvise(tmp_memory_allocated, 5 * pagesize, MADV_WILLNEED));
-	if (tst_kvercmp(3, 9, 0) < 0) {
-		check_and_print(EBADF);
-	/* in kernel commit 1998cc0, madvise(MADV_WILLNEED) to anon
-	 * mem doesn't return -EBADF now, as now we support swap
-	 * prefretch.
-	 */
-	} else {
-		tst_resm(TPASS, "madvise succeeded as expected, see "
-				"kernel commit 1998cc0 for details.");
-	}
-
-	free(ptr_memory_allocated);
-}
+static struct tst_test test = {
+	.tid = "madvise02",
+	.tcnt = ARRAY_SIZE(tcases),
+	.test = advice_test,
+	.needs_tmpdir = 1,
+	.setup = setup,
+	.cleanup = cleanup,
+};
-- 
1.8.3.1


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

* [LTP] [PATCH V3 1/3] ltp/lapi: add some madvices definition
  2016-05-25  6:29 [LTP] [PATCH V3 1/3] ltp/lapi: add some madvices definition Li Wang
  2016-05-25  6:29 ` [LTP] [PATCH V3 2/3] syscalls/madvise01: reconstruct and convert to new API Li Wang
@ 2016-06-01 14:22 ` Cyril Hrubis
  1 sibling, 0 replies; 6+ messages in thread
From: Cyril Hrubis @ 2016-06-01 14:22 UTC (permalink / raw)
  To: ltp

Hi!
Pushed with minor tweaks, thanks.

-- 
Cyril Hrubis
chrubis@suse.cz

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

* [LTP] [PATCH V3 2/3] syscalls/madvise01: reconstruct and convert to new API
  2016-05-25  6:29 ` [LTP] [PATCH V3 2/3] syscalls/madvise01: reconstruct and convert to new API Li Wang
  2016-05-25  6:29   ` [LTP] [PATCH V3 3/3] syscalls/madvise02: " Li Wang
@ 2016-06-01 14:24   ` Cyril Hrubis
  1 sibling, 0 replies; 6+ messages in thread
From: Cyril Hrubis @ 2016-06-01 14:24 UTC (permalink / raw)
  To: ltp

Hi!
Pushed with minor changes, thanks.

* Added needs_root = 1, since both mounting tmpfs and some of the
  madvise flags require it

* Added curly braces around multiline else branch
  (LKML style prefers it that way)

-- 
Cyril Hrubis
chrubis@suse.cz

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

* [LTP] [PATCH V3 3/3] syscalls/madvise02: reconstruct and convert to new API
  2016-05-25  6:29   ` [LTP] [PATCH V3 3/3] syscalls/madvise02: " Li Wang
@ 2016-06-06 13:03     ` Cyril Hrubis
  0 siblings, 0 replies; 6+ messages in thread
From: Cyril Hrubis @ 2016-06-06 13:03 UTC (permalink / raw)
  To: ltp

Hi!
> -#if defined(MADV_MERGEABLE)
> -static void test_mergeable_einval(void)
> -{
> -	char *file;
> -
> -	if (access(KSM_SYS_DIR, F_OK) >= 0) {
> -		tst_resm(TCONF, "kernel configured with CONFIG_KSM, "
> -				 "skip EINVAL test for MADV_MERGEABLE.");
> -		return;
> +	enable = 1;
> +	file = tmp_file;
> +
> +	switch (i) {
> +	case 0:
> +		/* Test for addr EINVAL */
> +		file = tmp_file + 100;
> +		break;
> +
> +	case 1:
> +		/* Test for advice EINVAL */
> +		break;
> +
> +	case 2:
> +		enable = 0;
> +		/* Test for lock EINVAL */
> +		#if !defined(UCLINUX)
> +		if (mlock(file, st.st_size) < 0)
> +			tst_brk(TBROK | TERRNO, "mlock failed");
> +		enable = 1;
> +		#endif /* if !defined(UCLINUX) */
> +		break;
> +
> +	case 3:
> +	case 4:
> +		/* Test for MADV_MERGEABLE EINVAL */
> +		if ((tst_kvercmp(2, 6, 32)) < 0)
> +			enable = 0;
> +
> +		if (access(KSM_SYS_DIR, F_OK) == 0)
> +			/* kernel configured with CONFIG_KSM,
> +			 * skip EINVAL test for MADV_MERGEABLE. */
> +			enable = 0;
> +		break;
> +
> +	case 5:
> +		/* Test for MADV_WILLNEED EBADF */
> +		ptr_addr = SAFE_MALLOC(st.st_size);
> +		/* Take temporary pointer for later use, freeing up the
> +		 * original one. */
> +		tmp_addr = ptr_addr;
> +		tmp_addr =
> +			(char *)(((unsigned long)tmp_addr + pagesize - 1) & ~(pagesize - 1));
> +		file = tmp_addr;
> +
> +		/* In kernel commit 1998cc0, madvise(MADV_WILLNEED) to
> +		 * anon mem doesn't return -EBADF now, as now we support
> +		 * swap prefretch. */
> +		if ((tst_kvercmp(3, 9, 0)) > 0)
> +			enable = 0;
> +		break;
> +
> +	case 6:
> +		/* Test for MADV_NORMAL ENOMEM */
> +		SAFE_MUNMAP(file + st.st_size - pagesize, pagesize);
> +		break;

If you need to do the testcase filter, you should:

1) Do the switch() over tc->advice
2) Call it once in the setup() and set tc->skipp flag rather
   than doing this on each iteration.

-- 
Cyril Hrubis
chrubis@suse.cz

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

end of thread, other threads:[~2016-06-06 13:03 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2016-05-25  6:29 [LTP] [PATCH V3 1/3] ltp/lapi: add some madvices definition Li Wang
2016-05-25  6:29 ` [LTP] [PATCH V3 2/3] syscalls/madvise01: reconstruct and convert to new API Li Wang
2016-05-25  6:29   ` [LTP] [PATCH V3 3/3] syscalls/madvise02: " Li Wang
2016-06-06 13:03     ` Cyril Hrubis
2016-06-01 14:24   ` [LTP] [PATCH V3 2/3] syscalls/madvise01: " Cyril Hrubis
2016-06-01 14:22 ` [LTP] [PATCH V3 1/3] ltp/lapi: add some madvices definition Cyril Hrubis

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