All the mail mirrored from lore.kernel.org
 help / color / mirror / Atom feed
* [LTP] [PATCH V2 00/14] Convert LTP kernel/mem/testcase to New API
@ 2017-04-10 10:05 Li Wang
  2017-04-10 10:05 ` [LTP] [PATCH V2 01/14] mem/lib: convert to new API Li Wang
                   ` (13 more replies)
  0 siblings, 14 replies; 25+ messages in thread
From: Li Wang @ 2017-04-10 10:05 UTC (permalink / raw
  To: ltp

v1 --> v2

 * take use of safe macro of LTP in testcase

 * add new SAFE_READ_MEMINFO macro in 
    mem/lib: convert to new API

 * fix OOM timeout issue by new commit:
    mem/oom: fix the timeout issue


-- 
2.9.3


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

* [LTP] [PATCH V2 01/14] mem/lib: convert to new API
  2017-04-10 10:05 [LTP] [PATCH V2 00/14] Convert LTP kernel/mem/testcase to New API Li Wang
@ 2017-04-10 10:05 ` Li Wang
  2017-04-10 10:05 ` [LTP] [PATCH V2 02/14] mem/oom: " Li Wang
                   ` (12 subsequent siblings)
  13 siblings, 0 replies; 25+ messages in thread
From: Li Wang @ 2017-04-10 10:05 UTC (permalink / raw
  To: ltp

Add new SAFE_READ_MEMINFO macro to replace function read_meminfo().

Signed-off-by: Li Wang <liwang@redhat.com>
---
 include/tst_safe_file_ops.h        |   6 +
 testcases/kernel/mem/include/mem.h |   9 +-
 testcases/kernel/mem/lib/mem.c     | 314 ++++++++++++++++---------------------
 3 files changed, 141 insertions(+), 188 deletions(-)

diff --git a/include/tst_safe_file_ops.h b/include/tst_safe_file_ops.h
index 4d19877..5c3fea4 100644
--- a/include/tst_safe_file_ops.h
+++ b/include/tst_safe_file_ops.h
@@ -38,6 +38,12 @@
 	file_lines_scanf(__FILE__, __LINE__, NULL, 1,\
 			(path), (fmt), ## __VA_ARGS__)
 
+#define SAFE_READ_MEMINFO(item) \
+       ({long tst_rval; \
+        SAFE_FILE_LINES_SCANF("/proc/meminfo", item " %ld", \
+                        &tst_rval); \
+        tst_rval;})
+
 #define FILE_PRINTF(path, fmt, ...) \
 	file_printf(__FILE__, __LINE__, \
 		    (path), (fmt), ## __VA_ARGS__)
diff --git a/testcases/kernel/mem/include/mem.h b/testcases/kernel/mem/include/mem.h
index f9522a5..377cc55 100644
--- a/testcases/kernel/mem/include/mem.h
+++ b/testcases/kernel/mem/include/mem.h
@@ -1,7 +1,7 @@
 #ifndef _MEM_H
 #define _MEM_H
 #include "config.h"
-#include "test.h"
+#include "tst_test.h"
 
 #if defined(__powerpc__) || defined(__powerpc64__)
 #define MAXNODES		256
@@ -84,16 +84,13 @@ void mount_mem(char *name, char *fs, char *options, char *path, char *path_new);
 void umount_mem(char *path, char *path_new);
 
 /* shared */
-unsigned int get_a_numa_node(void (*cleanup_fn)(void));
+unsigned int get_a_numa_node(void);
 int  path_exist(const char *path, ...);
-long read_meminfo(char *item);
 void set_sys_tune(char *sys_file, long tune, int check);
 long get_sys_tune(char *sys_file);
-void cleanup(void);
-void setup(void);
 
 void update_shm_size(size_t *shm_size);
 
 /* MMAP */
-int range_is_mapped(void (*cleanup_fn) (void), unsigned long low, unsigned long high);
+int range_is_mapped(unsigned long low, unsigned long high);
 #endif
diff --git a/testcases/kernel/mem/lib/mem.c b/testcases/kernel/mem/lib/mem.c
index 7e403b5..053737b 100644
--- a/testcases/kernel/mem/lib/mem.c
+++ b/testcases/kernel/mem/lib/mem.c
@@ -1,9 +1,12 @@
+#define TST_NO_DEFAULT_MAIN
+
 #include "config.h"
 #include <sys/types.h>
 #include <sys/mman.h>
 #include <sys/mount.h>
 #include <sys/stat.h>
 #include <sys/wait.h>
+#include <sys/param.h>
 #include <errno.h>
 #include <fcntl.h>
 #if HAVE_NUMA_H
@@ -16,10 +19,9 @@
 #include <stdarg.h>
 #include <stdio.h>
 #include <string.h>
+#include <stdlib.h>
 #include <unistd.h>
 
-#include "test.h"
-#include "safe_macros.h"
 #include "mem.h"
 #include "numa_helper.h"
 
@@ -31,7 +33,7 @@ static int alloc_mem(long int length, int testcase)
 	long i, pagesz = getpagesize();
 	int loop = 10;
 
-	tst_resm(TINFO, "thread (%lx), allocating %ld bytes.",
+	tst_res(TINFO, "thread (%lx), allocating %ld bytes.",
 		(unsigned long) pthread_self(), length);
 
 	s = mmap(NULL, length, PROT_READ | PROT_WRITE,
@@ -80,7 +82,7 @@ static void child_alloc(int testcase, int lite, int threads)
 
 	th = malloc(sizeof(pthread_t) * threads);
 	if (!th) {
-		tst_resm(TINFO | TERRNO, "malloc");
+		tst_res(TINFO | TERRNO, "malloc");
 		goto out;
 	}
 
@@ -88,7 +90,7 @@ static void child_alloc(int testcase, int lite, int threads)
 		TEST(pthread_create(&th[i], NULL, child_alloc_thread,
 			(void *)((long)testcase)));
 		if (TEST_RETURN) {
-			tst_resm(TINFO | TRERRNO, "pthread_create");
+			tst_res(TINFO | TRERRNO, "pthread_create");
 			/*
 			 * Keep going if thread other than first fails to
 			 * spawn due to lack of resources.
@@ -125,13 +127,13 @@ void oom(int testcase, int lite, int retcode, int allow_sigkill)
 	pid_t pid;
 	int status, threads;
 
-	switch (pid = fork()) {
+	switch (pid = SAFE_FORK()) {
 	case -1:
 		if (errno == retcode) {
-			tst_resm(TPASS | TERRNO, "fork");
+			tst_res(TPASS | TERRNO, "fork");
 			return;
 		}
-		tst_brkm(TBROK | TERRNO, cleanup, "fork");
+		tst_brk(TBROK | TERRNO, "fork");
 	case 0:
 		threads = MAX(1, tst_ncpus() - 1);
 		child_alloc(testcase, lite, threads);
@@ -139,31 +141,30 @@ void oom(int testcase, int lite, int retcode, int allow_sigkill)
 		break;
 	}
 
-	tst_resm(TINFO, "expected victim is %d.", pid);
-	if (waitpid(-1, &status, 0) == -1)
-		tst_brkm(TBROK | TERRNO, cleanup, "waitpid");
+	tst_res(TINFO, "expected victim is %d.", pid);
+	SAFE_WAITPID(-1, &status, 0);
 
 	if (WIFSIGNALED(status)) {
 		if (allow_sigkill && WTERMSIG(status) == SIGKILL) {
-			tst_resm(TPASS, "victim signalled: (%d) %s",
+			tst_res(TPASS, "victim signalled: (%d) %s",
 				SIGKILL,
 				tst_strsig(SIGKILL));
 		} else {
-			tst_resm(TFAIL, "victim signalled: (%d) %s",
+			tst_res(TFAIL, "victim signalled: (%d) %s",
 				WTERMSIG(status),
 				tst_strsig(WTERMSIG(status)));
 		}
 	} else if (WIFEXITED(status)) {
 		if (WEXITSTATUS(status) == retcode) {
-			tst_resm(TPASS, "victim retcode: (%d) %s",
+			tst_res(TPASS, "victim retcode: (%d) %s",
 				retcode, strerror(retcode));
 		} else {
-			tst_resm(TFAIL, "victim unexpectedly ended with "
+			tst_res(TFAIL, "victim unexpectedly ended with "
 				"retcode: %d, expected: %d",
 				WEXITSTATUS(status), retcode);
 		}
 	} else {
-		tst_resm(TFAIL, "victim unexpectedly ended");
+		tst_res(TFAIL, "victim unexpectedly ended");
 	}
 }
 
@@ -178,10 +179,9 @@ static void set_global_mempolicy(int mempolicy)
 	if (mempolicy) {
 		ret = get_allowed_nodes_arr(NH_MEMS|NH_CPUS, &num_nodes, &nodes);
 		if (ret != 0)
-			tst_brkm(TBROK|TERRNO, cleanup,
-				 "get_allowed_nodes_arr");
+			tst_brk(TBROK|TERRNO, "get_allowed_nodes_arr");
 		if (num_nodes < 2) {
-			tst_resm(TINFO, "mempolicy need NUMA system support");
+			tst_res(TINFO, "mempolicy need NUMA system support");
 			free(nodes);
 			return;
 		}
@@ -193,7 +193,7 @@ static void set_global_mempolicy(int mempolicy)
 		case MPOL_INTERLEAVE:
 		case MPOL_PREFERRED:
 			if (num_nodes == 2) {
-				tst_resm(TINFO, "The mempolicy need "
+				tst_res(TINFO, "The mempolicy need "
 					 "more than 2 numa nodes");
 				free(nodes);
 				return;
@@ -204,10 +204,10 @@ static void set_global_mempolicy(int mempolicy)
 			}
 			break;
 		default:
-			tst_brkm(TBROK|TERRNO, cleanup, "Bad mempolicy mode");
+			tst_brk(TBROK|TERRNO, "Bad mempolicy mode");
 		}
 		if (set_mempolicy(mempolicy, nmask, MAXNODES) == -1)
-			tst_brkm(TBROK|TERRNO, cleanup, "set_mempolicy");
+			tst_brk(TBROK|TERRNO, "set_mempolicy");
 	}
 #endif
 }
@@ -218,10 +218,10 @@ void testoom(int mempolicy, int lite, int retcode, int allow_sigkill)
 
 	set_global_mempolicy(mempolicy);
 
-	tst_resm(TINFO, "start normal OOM testing.");
+	tst_res(TINFO, "start normal OOM testing.");
 	oom(NORMAL, lite, retcode, allow_sigkill);
 
-	tst_resm(TINFO, "start OOM testing for mlocked pages.");
+	tst_res(TINFO, "start OOM testing for mlocked pages.");
 	oom(MLOCK, lite, retcode, allow_sigkill);
 
 	/*
@@ -229,14 +229,14 @@ void testoom(int mempolicy, int lite, int retcode, int allow_sigkill)
 	 * run to run, which isn't reliable for oom03 cgroup test.
 	 */
 	if (access(PATH_KSM, F_OK) == -1 || lite == 1) {
-		tst_resm(TINFO, "KSM is not configed or lite == 1, "
+		tst_res(TINFO, "KSM is not configed or lite == 1, "
 			 "skip OOM test for KSM pags");
 	} else {
-		tst_resm(TINFO, "start OOM testing for KSM pages.");
-		SAFE_FILE_SCANF(cleanup, PATH_KSM "run", "%d", &ksm_run_orig);
-		SAFE_FILE_PRINTF(cleanup, PATH_KSM "run", "1");
+		tst_res(TINFO, "start OOM testing for KSM pages.");
+		SAFE_FILE_SCANF(PATH_KSM "run", "%d", &ksm_run_orig);
+		SAFE_FILE_PRINTF(PATH_KSM "run", "1");
 		oom(KSM, lite, retcode, allow_sigkill);
-		SAFE_FILE_PRINTF(cleanup,PATH_KSM "run", "%d", ksm_run_orig);
+		SAFE_FILE_PRINTF(PATH_KSM "run", "%d", ksm_run_orig);
 	}
 }
 
@@ -247,8 +247,8 @@ static int max_page_sharing;
 void save_max_page_sharing(void)
 {
 	if (access(PATH_KSM "max_page_sharing", F_OK) == 0)
-	        SAFE_FILE_SCANF(NULL, PATH_KSM "max_page_sharing",
-	                        "%d", &max_page_sharing);
+		SAFE_FILE_SCANF(PATH_KSM "max_page_sharing",
+				"%d", &max_page_sharing);
 }
 
 void restore_max_page_sharing(void)
@@ -264,11 +264,11 @@ static void check(char *path, long int value)
 	long actual_val;
 
 	snprintf(fullpath, BUFSIZ, PATH_KSM "%s", path);
-	SAFE_FILE_SCANF(cleanup, fullpath, "%ld", &actual_val);
+	SAFE_FILE_SCANF(fullpath, "%ld", &actual_val);
 
-	tst_resm(TINFO, "%s is %ld.", path, actual_val);
+	tst_res(TINFO, "%s is %ld.", path, actual_val);
 	if (actual_val != value)
-		tst_resm(TFAIL, "%s is not %ld.", path, value);
+		tst_res(TFAIL, "%s is not %ld.", path, value);
 }
 
 static void wait_ksmd_done(void)
@@ -282,17 +282,10 @@ static void wait_ksmd_done(void)
 		sleep(10);
 		count++;
 
-		SAFE_FILE_SCANF(cleanup, PATH_KSM "pages_shared",
-				"%ld", &pages_shared);
-
-		SAFE_FILE_SCANF(cleanup, PATH_KSM "pages_sharing",
-				"%ld", &pages_sharing);
-
-		SAFE_FILE_SCANF(cleanup, PATH_KSM "pages_volatile",
-				"%ld", &pages_volatile);
-
-		SAFE_FILE_SCANF(cleanup, PATH_KSM "pages_unshared",
-				"%ld", &pages_unshared);
+		SAFE_FILE_SCANF(PATH_KSM "pages_shared", "%ld", &pages_shared);
+		SAFE_FILE_SCANF(PATH_KSM "pages_sharing", "%ld", &pages_sharing);
+		SAFE_FILE_SCANF(PATH_KSM "pages_volatile", "%ld", &pages_volatile);
+		SAFE_FILE_SCANF(PATH_KSM "pages_unshared", "%ld", &pages_unshared);
 
 		if (pages_shared != old_pages_shared ||
 		    pages_sharing != old_pages_sharing ||
@@ -307,7 +300,7 @@ static void wait_ksmd_done(void)
 		}
 	}
 
-	tst_resm(TINFO, "ksm daemon takes %ds to scan all mergeable pages",
+	tst_res(TINFO, "ksm daemon takes %ds to scan all mergeable pages",
 		 count * 10);
 }
 
@@ -318,7 +311,7 @@ static void group_check(int run, int pages_shared, int pages_sharing,
 	/* wait for ksm daemon to scan all mergeable pages. */
 	wait_ksmd_done();
 
-	tst_resm(TINFO, "check!");
+	tst_res(TINFO, "check!");
 	check("run", run);
 	check("pages_shared", pages_shared);
 	check("pages_sharing", pages_sharing);
@@ -334,18 +327,16 @@ static void verify(char **memory, char value, int proc,
 	int i, j;
 	void *s = NULL;
 
-	s = malloc((end - start) * (end2 - start2));
-	if (s == NULL)
-		tst_brkm(TBROK | TERRNO, tst_exit, "malloc");
+	s = SAFE_MALLOC((end - start) * (end2 - start2));
 
-	tst_resm(TINFO, "child %d verifies memory content.", proc);
+	tst_res(TINFO, "child %d verifies memory content.", proc);
 	memset(s, value, (end - start) * (end2 - start2));
 	if (memcmp(memory[start], s, (end - start) * (end2 - start2))
 	    != 0)
 		for (j = start; j < end; j++)
 			for (i = start2; i < end2; i++)
 				if (memory[j][i] != value)
-					tst_resm(TFAIL, "child %d has %c at "
+					tst_res(TFAIL, "child %d has %c@"
 						 "%d,%d,%d.",
 						 proc, memory[j][i], proc,
 						 j, i);
@@ -354,9 +345,9 @@ static void verify(char **memory, char value, int proc,
 
 void write_memcg(void)
 {
-	SAFE_FILE_PRINTF(NULL, MEMCG_LIMIT, "%ld", TESTMEM);
+	SAFE_FILE_PRINTF(MEMCG_LIMIT, "%ld", TESTMEM);
 
-	SAFE_FILE_PRINTF(NULL, MEMCG_PATH_NEW "/tasks", "%d", getpid());
+	SAFE_FILE_PRINTF(MEMCG_PATH_NEW "/tasks", "%d", getpid());
 }
 
 struct ksm_merge_data {
@@ -370,14 +361,14 @@ static void ksm_child_memset(int child_num, int size, int total_unit,
 	int i = 0, j;
 	int unit = size / total_unit;
 
-	tst_resm(TINFO, "child %d continues...", child_num);
+	tst_res(TINFO, "child %d continues...", child_num);
 
 	if (ksm_merge_data.mergeable_size == size * MB) {
-		tst_resm(TINFO, "child %d allocates %d MB filled with '%c'",
+		tst_res(TINFO, "child %d allocates %d MB filled with '%c'",
 			child_num, size, ksm_merge_data.data);
 
 	} else {
-		tst_resm(TINFO, "child %d allocates %d MB filled with '%c'"
+		tst_res(TINFO, "child %d allocates %d MB filled with '%c'"
 				" except one page with 'e'",
 				child_num, size, ksm_merge_data.data);
 	}
@@ -404,21 +395,19 @@ static void create_ksm_child(int child_num, int size, int unit,
 	total_unit = size / unit;
 
 	/* Apply for the space for memory */
-	memory = malloc(total_unit * sizeof(char *));
+	memory = SAFE_MALLOC(total_unit * sizeof(char *));
 	for (j = 0; j < total_unit; j++) {
-		memory[j] = mmap(NULL, unit * MB, PROT_READ|PROT_WRITE,
+		memory[j] = SAFE_MMAP(NULL, unit * MB, PROT_READ|PROT_WRITE,
 			MAP_ANONYMOUS|MAP_PRIVATE, -1, 0);
-		if (memory[j] == MAP_FAILED)
-			tst_brkm(TBROK|TERRNO, tst_exit, "mmap");
 #ifdef HAVE_MADV_MERGEABLE
 		if (madvise(memory[j], unit * MB, MADV_MERGEABLE) == -1)
-			tst_brkm(TBROK|TERRNO, tst_exit, "madvise");
+			tst_brk(TBROK|TERRNO, "madvise");
 #endif
 	}
 
-	tst_resm(TINFO, "child %d stops.", child_num);
+	tst_res(TINFO, "child %d stops.", child_num);
 	if (raise(SIGSTOP) == -1)
-		tst_brkm(TBROK|TERRNO, tst_exit, "kill");
+		tst_brk(TBROK|TERRNO, "kill");
 	fflush(stdout);
 
 	for (j = 0; j < 4; j++) {
@@ -428,9 +417,9 @@ static void create_ksm_child(int child_num, int size, int unit,
 
 		fflush(stdout);
 
-		tst_resm(TINFO, "child %d stops.", child_num);
+		tst_res(TINFO, "child %d stops.", child_num);
 		if (raise(SIGSTOP) == -1)
-			tst_brkm(TBROK|TERRNO, tst_exit, "kill");
+			tst_brk(TBROK|TERRNO, "kill");
 
 		if (ksm_merge_data[j].mergeable_size < size * MB) {
 			verify(memory, 'e', child_num, total_unit - 1,
@@ -443,19 +432,18 @@ static void create_ksm_child(int child_num, int size, int unit,
 		}
 	}
 
-	tst_resm(TINFO, "child %d finished.", child_num);
+	tst_res(TINFO, "child %d finished.", child_num);
 }
 
 static void stop_ksm_children(int *child, int num)
 {
 	int k, status;
 
-	tst_resm(TINFO, "wait for all children to stop.");
+	tst_res(TINFO, "wait for all children to stop.");
 	for (k = 0; k < num; k++) {
-		if (waitpid(child[k], &status, WUNTRACED) == -1)
-			tst_brkm(TBROK|TERRNO, cleanup, "waitpid");
+		SAFE_WAITPID(child[k], &status, WUNTRACED);
 		if (!WIFSTOPPED(status))
-			tst_brkm(TBROK, cleanup, "child %d was not stopped", k);
+			tst_brk(TBROK, "child %d was not stopped", k);
 	}
 }
 
@@ -463,11 +451,10 @@ static void resume_ksm_children(int *child, int num)
 {
 	int k;
 
-	tst_resm(TINFO, "resume all children.");
-	for (k = 0; k < num; k++) {
-		if (kill(child[k], SIGCONT) == -1)
-			tst_brkm(TBROK|TERRNO, cleanup, "kill child[%d]", k);
-	}
+	tst_res(TINFO, "resume all children.");
+	for (k = 0; k < num; k++)
+		SAFE_KILL(child[k], SIGCONT);
+
 	fflush(stdout);
 }
 
@@ -501,15 +488,11 @@ void create_same_memory(int size, int num, int unit)
 		}
 	}
 
-	child = malloc(num * sizeof(int));
-	if (child == NULL)
-		tst_brkm(TBROK | TERRNO, cleanup, "malloc");
+	child = SAFE_MALLOC(num * sizeof(int));
 
 	for (i = 0; i < num; i++) {
 		fflush(stdout);
-		switch (child[i] = fork()) {
-		case -1:
-			tst_brkm(TBROK|TERRNO, cleanup, "fork");
+		switch (child[i] = SAFE_FORK()) {
 		case 0:
 			if (i == 0) {
 				create_ksm_child(i, size, unit, ksm_data0);
@@ -529,14 +512,12 @@ void create_same_memory(int size, int num, int unit)
 
 	stop_ksm_children(child, num);
 
-	tst_resm(TINFO, "KSM merging...");
+	tst_res(TINFO, "KSM merging...");
 	if (access(PATH_KSM "max_page_sharing", F_OK) == 0)
-		SAFE_FILE_PRINTF(cleanup, PATH_KSM "max_page_sharing",
-				"%ld", size * pages * num);
-	SAFE_FILE_PRINTF(cleanup, PATH_KSM "run", "1");
-	SAFE_FILE_PRINTF(cleanup, PATH_KSM "pages_to_scan", "%ld",
-			 size * pages * num);
-	SAFE_FILE_PRINTF(cleanup, PATH_KSM "sleep_millisecs", "0");
+		SAFE_FILE_PRINTF(PATH_KSM "max_page_sharing", "%ld", size * pages * num);
+	SAFE_FILE_PRINTF(PATH_KSM "run", "1");
+	SAFE_FILE_PRINTF(PATH_KSM "pages_to_scan", "%ld", size * pages * num);
+	SAFE_FILE_PRINTF(PATH_KSM "sleep_millisecs", "0");
 
 	resume_ksm_children(child, num);
 	group_check(1, 2, size * num * pages - 2, 0, 0, 0, size * pages * num);
@@ -555,19 +536,19 @@ void create_same_memory(int size, int num, int unit)
 
 	stop_ksm_children(child, num);
 
-	tst_resm(TINFO, "KSM unmerging...");
-	SAFE_FILE_PRINTF(cleanup, PATH_KSM "run", "2");
+	tst_res(TINFO, "KSM unmerging...");
+	SAFE_FILE_PRINTF(PATH_KSM "run", "2");
 
 	resume_ksm_children(child, num);
 	group_check(2, 0, 0, 0, 0, 0, size * pages * num);
 
-	tst_resm(TINFO, "stop KSM.");
-	SAFE_FILE_PRINTF(cleanup, PATH_KSM "run", "0");
+	tst_res(TINFO, "stop KSM.");
+	SAFE_FILE_PRINTF(PATH_KSM "run", "0");
 	group_check(0, 0, 0, 0, 0, 0, size * pages * num);
 
 	while (waitpid(-1, &status, WUNTRACED | WCONTINUED) > 0)
 		if (WEXITSTATUS(status) != 0)
-			tst_resm(TFAIL, "child exit status is %d",
+			tst_res(TFAIL, "child exit status is %d",
 				 WEXITSTATUS(status));
 }
 
@@ -586,9 +567,9 @@ void test_ksm_merge_across_nodes(unsigned long nr_pages)
 
 	ret = get_allowed_nodes_arr(NH_MEMS|NH_CPUS, &num_nodes, &nodes);
 	if (ret != 0)
-		tst_brkm(TBROK|TERRNO, cleanup, "get_allowed_nodes_arr");
+		tst_brk(TBROK|TERRNO, "get_allowed_nodes_arr");
 	if (num_nodes < 2) {
-		tst_resm(TINFO, "need NUMA system support");
+		tst_res(TINFO, "need NUMA system support");
 		free(nodes);
 		return;
 	}
@@ -596,15 +577,13 @@ void test_ksm_merge_across_nodes(unsigned long nr_pages)
 	pagesize = sysconf(_SC_PAGE_SIZE);
 	length = nr_pages * pagesize;
 
-	memory = malloc(num_nodes * sizeof(char *));
+	memory = SAFE_MALLOC(num_nodes * sizeof(char *));
 	for (i = 0; i < num_nodes; i++) {
-		memory[i] = mmap(NULL, length, PROT_READ|PROT_WRITE,
+		memory[i] = SAFE_MMAP(NULL, length, PROT_READ|PROT_WRITE,
 			    MAP_ANONYMOUS|MAP_PRIVATE, -1, 0);
-		if (memory[i] == MAP_FAILED)
-			tst_brkm(TBROK|TERRNO, tst_exit, "mmap");
 #ifdef HAVE_MADV_MERGEABLE
 		if (madvise(memory[i], length, MADV_MERGEABLE) == -1)
-			tst_brkm(TBROK|TERRNO, tst_exit, "madvise");
+			tst_brk(TBROK|TERRNO, "madvise");
 #endif
 
 #if HAVE_NUMA_H && HAVE_LINUX_MEMPOLICY_H && HAVE_NUMAIF_H \
@@ -617,17 +596,17 @@ void test_ksm_merge_across_nodes(unsigned long nr_pages)
 		 */
 		ret = mbind(memory[i], length, MPOL_BIND, nmask, MAXNODES, 0);
 		if (ret == -1)
-			tst_brkm(TBROK|TERRNO, tst_exit, "mbind");
+			tst_brk(TBROK|TERRNO, "mbind");
 #endif
 
 		memset(memory[i], 10, length);
 	}
 
-	SAFE_FILE_PRINTF(cleanup, PATH_KSM "sleep_millisecs", "0");
-	SAFE_FILE_PRINTF(cleanup, PATH_KSM "pages_to_scan", "%ld",
+	SAFE_FILE_PRINTF(PATH_KSM "sleep_millisecs", "0");
+	SAFE_FILE_PRINTF(PATH_KSM "pages_to_scan", "%ld",
 			 nr_pages * num_nodes);
 	if (access(PATH_KSM "max_page_sharing", F_OK) == 0)
-		SAFE_FILE_PRINTF(cleanup, PATH_KSM "max_page_sharing",
+		SAFE_FILE_PRINTF(PATH_KSM "max_page_sharing",
 			"%ld", nr_pages * num_nodes);
 	/*
 	 * merge_across_nodes setting can be changed only when there
@@ -635,23 +614,23 @@ void test_ksm_merge_across_nodes(unsigned long nr_pages)
 	 * pages first, then to 1 after changing merge_across_nodes,
 	 * to remerge according to the new setting.
 	 */
-	SAFE_FILE_PRINTF(cleanup, PATH_KSM "run", "2");
+	SAFE_FILE_PRINTF(PATH_KSM "run", "2");
 	wait_ksmd_done();
-	tst_resm(TINFO, "Start to test KSM with merge_across_nodes=1");
-	SAFE_FILE_PRINTF(cleanup, PATH_KSM "merge_across_nodes", "1");
-	SAFE_FILE_PRINTF(cleanup, PATH_KSM "run", "1");
+	tst_res(TINFO, "Start to test KSM with merge_across_nodes=1");
+	SAFE_FILE_PRINTF(PATH_KSM "merge_across_nodes", "1");
+	SAFE_FILE_PRINTF(PATH_KSM "run", "1");
 	group_check(1, 1, nr_pages * num_nodes - 1, 0, 0, 0,
 		    nr_pages * num_nodes);
 
-	SAFE_FILE_PRINTF(cleanup, PATH_KSM "run", "2");
+	SAFE_FILE_PRINTF(PATH_KSM "run", "2");
 	wait_ksmd_done();
-	tst_resm(TINFO, "Start to test KSM with merge_across_nodes=0");
-	SAFE_FILE_PRINTF(cleanup, PATH_KSM "merge_across_nodes", "0");
-	SAFE_FILE_PRINTF(cleanup, PATH_KSM "run", "1");
+	tst_res(TINFO, "Start to test KSM with merge_across_nodes=0");
+	SAFE_FILE_PRINTF(PATH_KSM "merge_across_nodes", "0");
+	SAFE_FILE_PRINTF(PATH_KSM "run", "1");
 	group_check(1, num_nodes, nr_pages * num_nodes - num_nodes,
 		    0, 0, 0, nr_pages * num_nodes);
 
-	SAFE_FILE_PRINTF(cleanup, PATH_KSM "run", "2");
+	SAFE_FILE_PRINTF(PATH_KSM "run", "2");
 	wait_ksmd_done();
 }
 
@@ -660,22 +639,22 @@ void check_ksm_options(int *size, int *num, int *unit)
 	if (opt_size) {
 		*size = atoi(opt_sizestr);
 		if (*size < 1)
-			tst_brkm(TBROK, cleanup, "size cannot be less than 1.");
+			tst_brk(TBROK, "size cannot be less than 1.");
 	}
 	if (opt_unit) {
 		*unit = atoi(opt_unitstr);
 		if (*unit > *size)
-			tst_brkm(TBROK, cleanup,
+			tst_brk(TBROK,
 				 "unit cannot be greater than size.");
 		if (*size % *unit != 0)
-			tst_brkm(TBROK, cleanup,
+			tst_brk(TBROK,
 				 "the remainder of division of size by unit is "
 				 "not zero.");
 	}
 	if (opt_num) {
 		*num = atoi(opt_numstr);
 		if (*num < 3)
-			tst_brkm(TBROK, cleanup,
+			tst_brk(TBROK,
 				 "process number cannot be less 3.");
 	}
 }
@@ -712,7 +691,7 @@ static void gather_node_cpus(char *cpus, long nd)
 			 * be taken offline
 			 */
 			if (path_exist(path1)) {
-				SAFE_FILE_SCANF(cleanup, path1, "%ld", &online);
+				SAFE_FILE_SCANF(path1, "%ld", &online);
 				if (online == 0)
 					continue;
 			}
@@ -742,13 +721,12 @@ void read_cpuset_files(char *prefix, char *filename, char *retbuf)
 				 prefix, filename);
 			fd = open(path, O_RDONLY);
 			if (fd == -1)
-				tst_brkm(TBROK | TERRNO, cleanup,
-					 "open %s", path);
+				tst_brk(TBROK | TERRNO, "open %s", path);
 		} else
-			tst_brkm(TBROK | TERRNO, cleanup, "open %s", path);
+			tst_brk(TBROK | TERRNO, "open %s", path);
 	}
 	if (read(fd, retbuf, BUFSIZ) < 0)
-		tst_brkm(TBROK | TERRNO, cleanup, "read %s", path);
+		tst_brk(TBROK | TERRNO, "read %s", path);
 	close(fd);
 }
 
@@ -770,13 +748,11 @@ void write_cpuset_files(char *prefix, char *filename, char *buf)
 				 prefix, filename);
 			fd = open(path, O_WRONLY);
 			if (fd == -1)
-				tst_brkm(TBROK | TERRNO, cleanup,
-					 "open %s", path);
+				tst_brk(TBROK | TERRNO, "open %s", path);
 		} else
-			tst_brkm(TBROK | TERRNO, cleanup, "open %s", path);
+			tst_brk(TBROK | TERRNO, "open %s", path);
 	}
-	if (write(fd, buf, strlen(buf)) != (ssize_t)strlen(buf))
-		tst_brkm(TBROK | TERRNO, cleanup, "write %s", path);
+	SAFE_WRITE(1, fd, buf, strlen(buf));
 	close(fd);
 }
 
@@ -797,12 +773,12 @@ void write_cpusets(long nd)
 	if (strlen(cpus) != 0) {
 		write_cpuset_files(CPATH_NEW, "cpus", cpus);
 	} else {
-		tst_resm(TINFO, "No CPUs in the node%ld; "
+		tst_res(TINFO, "No CPUs in the node%ld; "
 				"using only CPU0", nd);
 		write_cpuset_files(CPATH_NEW, "cpus", "0");
 	}
 
-	SAFE_FILE_PRINTF(NULL, CPATH_NEW "/tasks", "%d", getpid());
+	SAFE_FILE_PRINTF(CPATH_NEW "/tasks", "%d", getpid());
 }
 
 void umount_mem(char *path, char *path_new)
@@ -815,53 +791,51 @@ void umount_mem(char *path, char *path_new)
 	sprintf(s, "%s/tasks", path);
 	fd = open(s, O_WRONLY);
 	if (fd == -1)
-		tst_resm(TWARN | TERRNO, "open %s", s);
+		tst_res(TWARN | TERRNO, "open %s", s);
 
 	snprintf(s_new, BUFSIZ, "%s/tasks", path_new);
 	fp = fopen(s_new, "r");
 	if (fp == NULL)
-		tst_resm(TWARN | TERRNO, "fopen %s", s_new);
+		tst_res(TWARN | TERRNO, "fopen %s", s_new);
 	if ((fd != -1) && (fp != NULL)) {
 		while (fgets(value, BUFSIZ, fp) != NULL)
 			if (write(fd, value, strlen(value) - 1)
 			    != (ssize_t)strlen(value) - 1)
-				tst_resm(TWARN | TERRNO, "write %s", s);
+				tst_res(TWARN | TERRNO, "write %s", s);
 	}
 	if (fd != -1)
 		close(fd);
 	if (fp != NULL)
 		fclose(fp);
 	if (rmdir(path_new) == -1)
-		tst_resm(TWARN | TERRNO, "rmdir %s", path_new);
+		tst_res(TWARN | TERRNO, "rmdir %s", path_new);
 	if (umount(path) == -1)
-		tst_resm(TWARN | TERRNO, "umount %s", path);
+		tst_res(TWARN | TERRNO, "umount %s", path);
 	if (rmdir(path) == -1)
-		tst_resm(TWARN | TERRNO, "rmdir %s", path);
+		tst_res(TWARN | TERRNO, "rmdir %s", path);
 }
 
 void mount_mem(char *name, char *fs, char *options, char *path, char *path_new)
 {
-	if (mkdir(path, 0777) == -1)
-		tst_brkm(TBROK | TERRNO, cleanup, "mkdir %s", path);
+	SAFE_MKDIR(path, 0777);
 	if (mount(name, path, fs, 0, options) == -1) {
 		if (errno == ENODEV) {
 			if (rmdir(path) == -1)
-				tst_resm(TWARN | TERRNO, "rmdir %s failed",
+				tst_res(TWARN | TERRNO, "rmdir %s failed",
 					 path);
-			tst_brkm(TCONF, NULL,
+			tst_brk(TCONF,
 				 "file system %s is not configured in kernel",
 				 fs);
 		}
-		tst_brkm(TBROK | TERRNO, cleanup, "mount %s", path);
+		tst_brk(TBROK | TERRNO, "mount %s", path);
 	}
-	if (mkdir(path_new, 0777) == -1)
-		tst_brkm(TBROK | TERRNO, cleanup, "mkdir %s", path_new);
+	SAFE_MKDIR(path_new, 0777);
 }
 
 /* shared */
 
 /* Warning: *DO NOT* use this function in child */
-unsigned int get_a_numa_node(void (*cleanup_fn) (void))
+unsigned int get_a_numa_node(void)
 {
 	unsigned int nd1, nd2;
 	int ret;
@@ -871,24 +845,24 @@ unsigned int get_a_numa_node(void (*cleanup_fn) (void))
 	case 0:
 		break;
 	case -3:
-		tst_brkm(TCONF, cleanup_fn, "requires a NUMA system.");
+		tst_brk(TCONF, "requires a NUMA system.");
 	default:
-		tst_brkm(TBROK | TERRNO, cleanup_fn, "1st get_allowed_nodes");
+		tst_brk(TBROK | TERRNO, "1st get_allowed_nodes");
 	}
 
 	ret = get_allowed_nodes(NH_MEMS | NH_CPUS, 1, &nd1);
 	switch (ret) {
 	case 0:
-		tst_resm(TINFO, "get node%u.", nd1);
+		tst_res(TINFO, "get node%u.", nd1);
 		return nd1;
 	case -3:
-		tst_brkm(TCONF, cleanup_fn, "requires a NUMA system that has "
+		tst_brk(TCONF, "requires a NUMA system that has "
 			 "at least one node with both memory and CPU "
 			 "available.");
 	default:
 		break;
 	}
-	tst_brkm(TBROK | TERRNO, cleanup_fn, "2nd get_allowed_nodes");
+	tst_brk(TBROK | TERRNO, "2nd get_allowed_nodes");
 }
 
 int path_exist(const char *path, ...)
@@ -903,44 +877,20 @@ int path_exist(const char *path, ...)
 	return access(pathbuf, F_OK) == 0;
 }
 
-long read_meminfo(char *item)
-{
-	FILE *fp;
-	char line[BUFSIZ], buf[BUFSIZ];
-	long val;
-
-	fp = fopen(PATH_MEMINFO, "r");
-	if (fp == NULL)
-		tst_brkm(TBROK | TERRNO, cleanup, "fopen %s", PATH_MEMINFO);
-
-	while (fgets(line, BUFSIZ, fp) != NULL) {
-		if (sscanf(line, "%64s %ld", buf, &val) == 2)
-			if (strcmp(buf, item) == 0) {
-				fclose(fp);
-				return val;
-			}
-		continue;
-	}
-	fclose(fp);
-
-	tst_brkm(TBROK, cleanup, "cannot find \"%s\" in %s",
-		 item, PATH_MEMINFO);
-}
-
 void set_sys_tune(char *sys_file, long tune, int check)
 {
 	long val;
 	char path[BUFSIZ];
 
-	tst_resm(TINFO, "set %s to %ld", sys_file, tune);
+	tst_res(TINFO, "set %s to %ld", sys_file, tune);
 
 	snprintf(path, BUFSIZ, PATH_SYSVM "%s", sys_file);
-	SAFE_FILE_PRINTF(NULL, path, "%ld", tune);
+	SAFE_FILE_PRINTF(path, "%ld", tune);
 
 	if (check) {
 		val = get_sys_tune(sys_file);
 		if (val != tune)
-			tst_brkm(TBROK, cleanup, "%s = %ld, but expect %ld",
+			tst_brk(TBROK, "%s = %ld, but expect %ld",
 				 sys_file, val, tune);
 	}
 }
@@ -951,7 +901,7 @@ long get_sys_tune(char *sys_file)
 	long tune;
 
 	snprintf(path, BUFSIZ, PATH_SYSVM "%s", sys_file);
-	SAFE_FILE_SCANF(NULL, path, "%ld", &tune);
+	SAFE_FILE_SCANF(path, "%ld", &tune);
 
 	return tune;
 }
@@ -960,20 +910,20 @@ void update_shm_size(size_t * shm_size)
 {
 	size_t shmmax;
 
-	SAFE_FILE_SCANF(cleanup, PATH_SHMMAX, "%ld", &shmmax);
+	SAFE_FILE_SCANF(PATH_SHMMAX, "%ld", &shmmax);
 	if (*shm_size > shmmax) {
-		tst_resm(TINFO, "Set shm_size to shmmax: %ld", shmmax);
+		tst_res(TINFO, "Set shm_size to shmmax: %ld", shmmax);
 		*shm_size = shmmax;
 	}
 }
 
-int range_is_mapped(void (*cleanup_fn) (void), unsigned long low, unsigned long high)
+int range_is_mapped(unsigned long low, unsigned long high)
 {
 	FILE *fp;
 
 	fp = fopen("/proc/self/maps", "r");
 	if (fp == NULL)
-		tst_brkm(TBROK | TERRNO, cleanup_fn, "Failed to open /proc/self/maps.");
+		tst_brk(TBROK | TERRNO, "Failed to open /proc/self/maps.");
 
 	while (!feof(fp)) {
 		unsigned long start, end;
@@ -982,7 +932,7 @@ int range_is_mapped(void (*cleanup_fn) (void), unsigned long low, unsigned long
 		ret = fscanf(fp, "%lx-%lx %*[^\n]\n", &start, &end);
 		if (ret != 2) {
 			fclose(fp);
-			tst_brkm(TBROK | TERRNO, cleanup_fn, "Couldn't parse /proc/self/maps line.");
+			tst_brk(TBROK | TERRNO, "Couldn't parse /proc/self/maps line.");
 		}
 
 		if ((start >= low) && (start < high)) {
-- 
2.9.3


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

* [LTP] [PATCH V2 02/14] mem/oom: convert to new API
  2017-04-10 10:05 [LTP] [PATCH V2 00/14] Convert LTP kernel/mem/testcase to New API Li Wang
  2017-04-10 10:05 ` [LTP] [PATCH V2 01/14] mem/lib: convert to new API Li Wang
@ 2017-04-10 10:05 ` Li Wang
  2017-04-10 10:05 ` [LTP] [PATCH V2 03/14] mem/oom: fix the timeout issue Li Wang
                   ` (11 subsequent siblings)
  13 siblings, 0 replies; 25+ messages in thread
From: Li Wang @ 2017-04-10 10:05 UTC (permalink / raw
  To: ltp

Signed-off-by: Li Wang <liwang@redhat.com>
---
 testcases/kernel/mem/oom/oom01.c |  83 ++++++++++---------------
 testcases/kernel/mem/oom/oom02.c |  83 +++++++++----------------
 testcases/kernel/mem/oom/oom03.c | 110 +++++++++++++--------------------
 testcases/kernel/mem/oom/oom04.c |  98 +++++++++++------------------
 testcases/kernel/mem/oom/oom05.c | 130 ++++++++++++++++-----------------------
 5 files changed, 196 insertions(+), 308 deletions(-)

diff --git a/testcases/kernel/mem/oom/oom01.c b/testcases/kernel/mem/oom/oom01.c
index e39394b..64a368e 100644
--- a/testcases/kernel/mem/oom/oom01.c
+++ b/testcases/kernel/mem/oom/oom01.c
@@ -7,27 +7,19 @@
  * behaviours. It simply increase the memory consumption 3G each time
  * until all the available memory is consumed and OOM is triggered.
  *
- * 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.
+ * Copyright (C) 2010-2017  Red Hat, Inc.
  *
- * 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.
+ * 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.
  *
- * 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.
+ * 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.
  */
+
 #include <sys/types.h>
 #include <sys/stat.h>
 #include <errno.h>
@@ -35,53 +27,42 @@
 #include <stdio.h>
 #include <stdlib.h>
 #include <unistd.h>
-#include "test.h"
 #include "mem.h"
 
-char *TCID = "oom01";
-int TST_TOTAL = 1;
-
-int main(int argc, char *argv[])
+static void verify_oom(void)
 {
-	int lc;
-
-	tst_parse_opts(argc, argv, NULL, NULL);
-
 #if __WORDSIZE == 32
-	tst_brkm(TCONF, NULL, "test is not designed for 32-bit system.");
+	tst_brk(TCONF, "test is not designed for 32-bit system.");
 #endif
 
-	setup();
+	/* we expect mmap to fail before OOM is hit */
+	set_sys_tune("overcommit_memory", 2, 1);
+	oom(NORMAL, 0, ENOMEM, 0);
 
-	for (lc = 0; TEST_LOOPING(lc); lc++) {
-		tst_count = 0;
+	/* with overcommit_memory set to 0 or 1 there's no
+	 * guarantee that mmap fails before OOM */
+	set_sys_tune("overcommit_memory", 0, 1);
+	oom(NORMAL, 0, ENOMEM, 1);
 
-		/* we expect mmap to fail before OOM is hit */
-		set_sys_tune("overcommit_memory", 2, 1);
-		oom(NORMAL, 0, ENOMEM, 0);
-
-		/* with overcommit_memory set to 0 or 1 there's no
-		 * guarantee that mmap fails before OOM */
-		set_sys_tune("overcommit_memory", 0, 1);
-		oom(NORMAL, 0, ENOMEM, 1);
-
-		set_sys_tune("overcommit_memory", 1, 1);
-		testoom(0, 0, ENOMEM, 1);
-	}
-	cleanup();
-	tst_exit();
+	set_sys_tune("overcommit_memory", 1, 1);
+	testoom(0, 0, ENOMEM, 1);
 }
 
-void setup(void)
+static void setup(void)
 {
-	tst_require_root();
-	tst_sig(FORK, DEF_HANDLER, cleanup);
-	TEST_PAUSE;
-
 	overcommit = get_sys_tune("overcommit_memory");
 }
 
-void cleanup(void)
+static void cleanup(void)
 {
 	set_sys_tune("overcommit_memory", overcommit, 0);
 }
+
+static struct tst_test test = {
+	.tid = "oom01",
+	.needs_root = 1,
+	.forks_child = 1,
+	.setup = setup,
+	.cleanup = cleanup,
+	.test_all = verify_oom,
+};
diff --git a/testcases/kernel/mem/oom/oom02.c b/testcases/kernel/mem/oom/oom02.c
index 8bbb18a..f315ee2 100644
--- a/testcases/kernel/mem/oom/oom02.c
+++ b/testcases/kernel/mem/oom/oom02.c
@@ -7,26 +7,17 @@
  * behaviours. It simply increase the memory consumption 3G each time
  * until all the available memory is consumed and OOM is triggered.
  *
- * 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.
+ * Copyright (C) 2010-2017  Red Hat, Inc.
  *
- * 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.
+ * 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.
  *
- * 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.
+ * 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.
  */
 
 #include "config.h"
@@ -36,64 +27,50 @@
 #include <fcntl.h>
 #include <stdio.h>
 #include "numa_helper.h"
-#include "test.h"
 #include "mem.h"
 
-char *TCID = "oom02";
-int TST_TOTAL = 1;
-
 #if HAVE_NUMA_H && HAVE_LINUX_MEMPOLICY_H && HAVE_NUMAIF_H \
 	&& HAVE_MPOL_CONSTANTS
 
-int main(int argc, char *argv[])
+static void verify_oom(void)
 {
-	int lc;
-
-	tst_parse_opts(argc, argv, NULL, NULL);
-
 #if __WORDSIZE == 32
-	tst_brkm(TCONF, NULL, "test is not designed for 32-bit system.");
+	tst_brk(TCONF, "test is not designed for 32-bit system.");
 #endif
 
-	setup();
-
-	for (lc = 0; TEST_LOOPING(lc); lc++) {
-		tst_count = 0;
+	tst_res(TINFO, "OOM on MPOL_BIND mempolicy...");
+	testoom(MPOL_BIND, 0, ENOMEM, 1);
 
-		tst_resm(TINFO, "OOM on MPOL_BIND mempolicy...");
-		testoom(MPOL_BIND, 0, ENOMEM, 1);
+	tst_res(TINFO, "OOM on MPOL_INTERLEAVE mempolicy...");
+	testoom(MPOL_INTERLEAVE, 0, ENOMEM, 1);
 
-		tst_resm(TINFO, "OOM on MPOL_INTERLEAVE mempolicy...");
-		testoom(MPOL_INTERLEAVE, 0, ENOMEM, 1);
-
-		tst_resm(TINFO, "OOM on MPOL_PREFERRED mempolicy...");
-		testoom(MPOL_PREFERRED, 0, ENOMEM, 1);
-	}
-	cleanup();
-	tst_exit();
+	tst_res(TINFO, "OOM on MPOL_PREFERRED mempolicy...");
+	testoom(MPOL_PREFERRED, 0, ENOMEM, 1);
 }
 
-void setup(void)
+static void setup(void)
 {
-	tst_require_root();
-	tst_sig(FORK, DEF_HANDLER, cleanup);
-	TEST_PAUSE;
-
 	if (!is_numa(NULL, NH_MEMS, 2))
-		tst_brkm(TCONF, NULL, "The case need a NUMA system.");
+		tst_brk(TCONF, "The case need a NUMA system.");
 
 	overcommit = get_sys_tune("overcommit_memory");
 	set_sys_tune("overcommit_memory", 1, 1);
 }
 
-void cleanup(void)
+static void cleanup(void)
 {
 	set_sys_tune("overcommit_memory", overcommit, 0);
 }
 
+static struct tst_test test = {
+	.tid = "oom02",
+	.needs_root = 1,
+	.forks_child = 1,
+	.setup = setup,
+	.cleanup = cleanup,
+	.test_all = verify_oom,
+};
+
 #else /* no NUMA */
-int main(void)
-{
-	tst_brkm(TCONF, NULL, "no NUMA development packages installed.");
-}
+	TST_TEST_TCONF("no NUMA development packages installed.");
 #endif
diff --git a/testcases/kernel/mem/oom/oom03.c b/testcases/kernel/mem/oom/oom03.c
index 879fdae..d3aed60 100644
--- a/testcases/kernel/mem/oom/oom03.c
+++ b/testcases/kernel/mem/oom/oom03.c
@@ -7,107 +7,83 @@
  * behaviours. It simply increase the memory consumption 3G each time
  * until all the available memory is consumed and OOM is triggered.
  *
- * 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.
+ * Copyright (C) 2010-2017  Red Hat, Inc.
  *
- * 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.
+ * 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.
  *
- * 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.
+ * 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.
  */
+
 #include <sys/types.h>
 #include <sys/stat.h>
 #include <errno.h>
 #include <fcntl.h>
 #include <stdio.h>
 #include "numa_helper.h"
-#include "test.h"
 #include "mem.h"
 
-char *TCID = "oom03";
-int TST_TOTAL = 1;
-
 #if HAVE_NUMA_H && HAVE_LINUX_MEMPOLICY_H && HAVE_NUMAIF_H \
 	&& HAVE_MPOL_CONSTANTS
 
-int main(int argc, char *argv[])
+static void verify_oom(void)
 {
-	int lc;
-
-	tst_parse_opts(argc, argv, NULL, NULL);
-
 #if __WORDSIZE == 32
-	tst_brkm(TCONF, NULL, "test is not designed for 32-bit system.");
+	tst_brk(TCONF, "test is not designed for 32-bit system.");
 #endif
 
-	setup();
-
-	for (lc = 0; TEST_LOOPING(lc); lc++) {
-		tst_count = 0;
+	SAFE_FILE_PRINTF(MEMCG_PATH_NEW "/tasks", "%d", getpid());
+	SAFE_FILE_PRINTF(MEMCG_LIMIT, "%ld", TESTMEM);
 
-		SAFE_FILE_PRINTF(cleanup, MEMCG_PATH_NEW "/tasks",
-				 "%d", getpid());
-		SAFE_FILE_PRINTF(cleanup, MEMCG_LIMIT, "%ld", TESTMEM);
+	testoom(0, 0, ENOMEM, 1);
 
-		testoom(0, 0, ENOMEM, 1);
-
-		if (access(MEMCG_SW_LIMIT, F_OK) == -1) {
-			if (errno == ENOENT)
-				tst_resm(TCONF,
-					 "memcg swap accounting is disabled");
-			else
-				tst_brkm(TBROK | TERRNO, cleanup, "access");
-		} else {
-			SAFE_FILE_PRINTF(cleanup, MEMCG_SW_LIMIT,
-					 "%ld", TESTMEM);
-			testoom(0, 1, ENOMEM, 1);
-		}
+	if (access(MEMCG_SW_LIMIT, F_OK) == -1) {
+		if (errno == ENOENT)
+			tst_res(TCONF,
+				"memcg swap accounting is disabled");
+		else
+			tst_brk(TBROK | TERRNO, "access");
+	} else {
+		SAFE_FILE_PRINTF(MEMCG_SW_LIMIT, "%ld", TESTMEM);
+		testoom(0, 1, ENOMEM, 1);
+	}
 
-		/* OOM for MEMCG with mempolicy */
-		if (is_numa(cleanup, NH_MEMS, 2)) {
-			tst_resm(TINFO, "OOM on MEMCG & mempolicy...");
-			testoom(MPOL_BIND, 0, ENOMEM, 1);
-			testoom(MPOL_INTERLEAVE, 0, ENOMEM, 1);
-			testoom(MPOL_PREFERRED, 0, ENOMEM, 1);
-		}
+	/* OOM for MEMCG with mempolicy */
+	if (is_numa(NULL, NH_MEMS, 2)) {
+		tst_res(TINFO, "OOM on MEMCG & mempolicy...");
+		testoom(MPOL_BIND, 0, ENOMEM, 1);
+		testoom(MPOL_INTERLEAVE, 0, ENOMEM, 1);
+		testoom(MPOL_PREFERRED, 0, ENOMEM, 1);
 	}
-	cleanup();
-	tst_exit();
 }
 
-void setup(void)
+static void setup(void)
 {
-	tst_require_root();
-	tst_sig(FORK, DEF_HANDLER, cleanup);
-	TEST_PAUSE;
-
 	overcommit = get_sys_tune("overcommit_memory");
 	set_sys_tune("overcommit_memory", 1, 1);
 	mount_mem("memcg", "cgroup", "memory", MEMCG_PATH, MEMCG_PATH_NEW);
 }
 
-void cleanup(void)
+static void cleanup(void)
 {
 	set_sys_tune("overcommit_memory", overcommit, 0);
 	umount_mem(MEMCG_PATH, MEMCG_PATH_NEW);
 }
 
+static struct tst_test test = {
+	.tid = "oom03",
+	.needs_root = 1,
+	.forks_child = 1,
+	.setup = setup,
+	.cleanup = cleanup,
+	.test_all = verify_oom,
+};
+
 #else
-int main(void)
-{
-	tst_brkm(TCONF, NULL, "no NUMA development packages installed.");
-}
+	TST_TEST_TCONF("no NUMA development packages installed.");
 #endif
diff --git a/testcases/kernel/mem/oom/oom04.c b/testcases/kernel/mem/oom/oom04.c
index 4829d95..afe35b2 100644
--- a/testcases/kernel/mem/oom/oom04.c
+++ b/testcases/kernel/mem/oom/oom04.c
@@ -7,26 +7,17 @@
  * behaviours. It simply increase the memory consumption 3G each time
  * until all the available memory is consumed and OOM is triggered.
  *
- * 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.
+ * Copyright (C) 2010-2017  Red Hat, Inc.
  *
- * 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.
+ * 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.
  *
- * 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.
+ * 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.
  */
 
 #include "config.h"
@@ -36,59 +27,38 @@
 #include <fcntl.h>
 #include <stdio.h>
 #include "numa_helper.h"
-#include "test.h"
 #include "mem.h"
 
-char *TCID = "oom04";
-int TST_TOTAL = 1;
-
 #if HAVE_NUMA_H && HAVE_LINUX_MEMPOLICY_H && HAVE_NUMAIF_H \
 	&& HAVE_MPOL_CONSTANTS
 
-int main(int argc, char *argv[])
+static void verify_oom(void)
 {
-	int lc;
-
-	tst_parse_opts(argc, argv, NULL, NULL);
-
 #if __WORDSIZE == 32
-	tst_brkm(TCONF, NULL, "test is not designed for 32-bit system.");
+	tst_brk(TCONF, "test is not designed for 32-bit system.");
 #endif
 
-	setup();
-
-	for (lc = 0; TEST_LOOPING(lc); lc++) {
-		tst_count = 0;
-
-		tst_resm(TINFO, "OOM on CPUSET...");
+	tst_res(TINFO, "OOM on CPUSET...");
+	testoom(0, 0, ENOMEM, 1);
+
+	if (is_numa(NULL, NH_MEMS, 2)) {
+		/*
+		 * Under NUMA system, the migration of cpuset's memory
+		 * is in charge of cpuset.memory_migrate, we can write
+		 * 1 to cpuset.memory_migrate to enable the migration.
+		 */
+		write_cpuset_files(CPATH_NEW, "memory_migrate", "1");
+		tst_res(TINFO, "OOM on CPUSET with mem migrate:");
 		testoom(0, 0, ENOMEM, 1);
-
-		if (is_numa(cleanup, NH_MEMS, 2)) {
-			/*
-			 * Under NUMA system, the migration of cpuset's memory
-			 * is in charge of cpuset.memory_migrate, we can write
-			 * 1 to cpuset.memory_migrate to enable the migration.
-			 */
-			write_cpuset_files(CPATH_NEW,
-					   "memory_migrate", "1");
-			tst_resm(TINFO, "OOM on CPUSET with mem migrate:");
-			testoom(0, 0, ENOMEM, 1);
-		}
 	}
-	cleanup();
-	tst_exit();
 }
 
-void setup(void)
+static void setup(void)
 {
 	int memnode, ret;
 
-	tst_require_root();
-	tst_sig(FORK, DEF_HANDLER, cleanup);
-	TEST_PAUSE;
-
 	if (!is_numa(NULL, NH_MEMS, 1))
-		tst_brkm(TCONF, NULL, "requires NUMA with at least 1 node");
+		tst_brk(TCONF, "requires NUMA with at least 1 node");
 
 	overcommit = get_sys_tune("overcommit_memory");
 	set_sys_tune("overcommit_memory", 1, 1);
@@ -103,20 +73,26 @@ void setup(void)
 	 */
 	ret = get_allowed_nodes(NH_MEMS, 1, &memnode);
 	if (ret < 0)
-		tst_brkm(TBROK, cleanup, "Failed to get a memory node "
-				      "using get_allowed_nodes()");
+		tst_brk(TBROK, "Failed to get a memory node "
+			      "using get_allowed_nodes()");
 	write_cpusets(memnode);
 }
 
-void cleanup(void)
+static void cleanup(void)
 {
 	set_sys_tune("overcommit_memory", overcommit, 0);
 	umount_mem(CPATH, CPATH_NEW);
 }
 
+static struct tst_test test = {
+	.tid = "oom04",
+	.needs_root = 1,
+	.forks_child = 1,
+	.setup = setup,
+	.cleanup = cleanup,
+	.test_all = verify_oom,
+};
+
 #else /* no NUMA */
-int main(void)
-{
-	tst_brkm(TCONF, NULL, "no NUMA development packages installed.");
-}
+	TST_TEST_TCONF("no NUMA development packages installed.");
 #endif
diff --git a/testcases/kernel/mem/oom/oom05.c b/testcases/kernel/mem/oom/oom05.c
index ccf456f..d97fe7d 100644
--- a/testcases/kernel/mem/oom/oom05.c
+++ b/testcases/kernel/mem/oom/oom05.c
@@ -7,26 +7,17 @@
  * behaviours. It simply increase the memory consumption 3G each time
  * until all the available memory is consumed and OOM is triggered.
  *
- * Copyright (C) 2013  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.
+ * Copyright (C) 2013-2017  Red Hat, Inc.
  *
- * 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.
+ * 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.
  *
- * 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.
+ * 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.
  */
 
 #include "config.h"
@@ -36,82 +27,63 @@
 #include <fcntl.h>
 #include <stdio.h>
 #include "numa_helper.h"
-#include "test.h"
 #include "mem.h"
 
-char *TCID = "oom05";
-int TST_TOTAL = 1;
-
 #if HAVE_NUMA_H && HAVE_LINUX_MEMPOLICY_H && HAVE_NUMAIF_H \
 	&& HAVE_MPOL_CONSTANTS
 
-int main(int argc, char *argv[])
+static void verify_oom(void)
 {
-	int lc;
 	int swap_acc_on = 1;
 
-	tst_parse_opts(argc, argv, NULL, NULL);
-
 #if __WORDSIZE == 32
-	tst_brkm(TCONF, NULL, "test is not designed for 32-bit system.");
+	tst_brk(TCONF, "test is not designed for 32-bit system.");
 #endif
 
-	setup();
 
-	for (lc = 0; TEST_LOOPING(lc); lc++) {
-		tst_count = 0;
+	tst_res(TINFO, "OOM on CPUSET & MEMCG...");
+	testoom(0, 0, ENOMEM, 1);
 
-		tst_resm(TINFO, "OOM on CPUSET & MEMCG...");
+	/*
+	 * Under NUMA system, the migration of cpuset's memory
+	 * is in charge of cpuset.memory_migrate, we can write
+	 * 1 to cpuset.memory_migrate to enable the migration.
+	 */
+	if (is_numa(NULL, NH_MEMS, 2)) {
+		write_cpuset_files(CPATH_NEW, "memory_migrate", "1");
+		tst_res(TINFO, "OOM on CPUSET & MEMCG with "
+				"cpuset.memory_migrate=1");
 		testoom(0, 0, ENOMEM, 1);
+	}
 
-		/*
-		 * Under NUMA system, the migration of cpuset's memory
-		 * is in charge of cpuset.memory_migrate, we can write
-		 * 1 to cpuset.memory_migrate to enable the migration.
-		 */
-		if (is_numa(cleanup, NH_MEMS, 2)) {
-			write_cpuset_files(CPATH_NEW, "memory_migrate", "1");
-			tst_resm(TINFO, "OOM on CPUSET & MEMCG with "
-					"cpuset.memory_migrate=1");
-			testoom(0, 0, ENOMEM, 1);
-		}
-
-		if (access(MEMCG_SW_LIMIT, F_OK) == -1) {
-			if (errno == ENOENT) {
-				tst_resm(TCONF,
-					 "memcg swap accounting is disabled");
-				swap_acc_on = 0;
-			} else
-				tst_brkm(TBROK|TERRNO, cleanup, "access");
-		}
-
-		if (swap_acc_on) {
-			tst_resm(TINFO, "OOM on CPUSET & MEMCG with "
-					"special memswap limitation:");
-			SAFE_FILE_PRINTF(cleanup, MEMCG_SW_LIMIT, "%ld", TESTMEM);
-			testoom(0, 0, ENOMEM, 1);
-
-			tst_resm(TINFO, "OOM on CPUSET & MEMCG with "
-					"disabled memswap limitation:");
-			SAFE_FILE_PRINTF(cleanup, MEMCG_SW_LIMIT, "-1");
-			testoom(0, 0, ENOMEM, 1);
-		}
+	if (access(MEMCG_SW_LIMIT, F_OK) == -1) {
+		if (errno == ENOENT) {
+			tst_res(TCONF,
+					"memcg swap accounting is disabled");
+			swap_acc_on = 0;
+		} else
+			tst_brk(TBROK|TERRNO, "access");
 	}
 
-	cleanup();
-	tst_exit();
+	if (swap_acc_on) {
+		tst_res(TINFO, "OOM on CPUSET & MEMCG with "
+				"special memswap limitation:");
+		SAFE_FILE_PRINTF(MEMCG_SW_LIMIT, "%ld", TESTMEM);
+		testoom(0, 0, ENOMEM, 1);
+
+		tst_res(TINFO, "OOM on CPUSET & MEMCG with "
+				"disabled memswap limitation:");
+		SAFE_FILE_PRINTF(MEMCG_SW_LIMIT, "-1");
+		testoom(0, 0, ENOMEM, 1);
+	}
 }
 
 void setup(void)
 {
 	int ret, memnode;
 
-	tst_require_root();
-	tst_sig(FORK, DEF_HANDLER, cleanup);
-	TEST_PAUSE;
-
 	if (!is_numa(NULL, NH_MEMS, 1))
-		tst_brkm(TCONF, NULL, "requires NUMA with at least 1 node");
+		tst_brk(TCONF, "requires NUMA with at least 1 node");
 
 	overcommit = get_sys_tune("overcommit_memory");
 	set_sys_tune("overcommit_memory", 1, 1);
@@ -128,8 +100,8 @@ void setup(void)
 	 */
 	ret = get_allowed_nodes(NH_MEMS, 1, &memnode);
 	if (ret < 0)
-		tst_brkm(TBROK, cleanup, "Failed to get a memory node "
-				      "using get_allowed_nodes()");
+		tst_brk(TBROK, "Failed to get a memory node "
+			      "using get_allowed_nodes()");
 	write_cpusets(memnode);
 }
 
@@ -140,9 +112,15 @@ void cleanup(void)
 	umount_mem(MEMCG_PATH, MEMCG_PATH_NEW);
 }
 
+static struct tst_test test = {
+	.tid = "oom05",
+	.needs_root = 1,
+	.forks_child = 1,
+	.setup = setup,
+	.cleanup = cleanup,
+	.test_all = verify_oom,
+};
+
 #else /* no NUMA */
-int main(void)
-{
-	tst_brkm(TCONF, NULL, "no NUMA development packages installed.");
-}
+	TST_TEST_TCONF("no NUMA development packages installed.");
 #endif
-- 
2.9.3


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

* [LTP] [PATCH V2 03/14] mem/oom: fix the timeout issue
  2017-04-10 10:05 [LTP] [PATCH V2 00/14] Convert LTP kernel/mem/testcase to New API Li Wang
  2017-04-10 10:05 ` [LTP] [PATCH V2 01/14] mem/lib: convert to new API Li Wang
  2017-04-10 10:05 ` [LTP] [PATCH V2 02/14] mem/oom: " Li Wang
@ 2017-04-10 10:05 ` Li Wang
  2017-07-07 15:11   ` Cyril Hrubis
  2017-04-10 10:05 ` [LTP] [PATCH V2 04/14] mem/ksm: convert to new API Li Wang
                   ` (10 subsequent siblings)
  13 siblings, 1 reply; 25+ messages in thread
From: Li Wang @ 2017-04-10 10:05 UTC (permalink / raw
  To: ltp

OOM spend time is depend on amount of RAM+Swap, it's hard to get a standard
to measure how long it takes, so that we cann't set tst_test->timeout easily.

In this patch, we divide the tests into two part:

1. OOM takes original way on small(mem_total <= 100G) RAM, and with setting
   the .timeout according to the worst spending on 100G system. Here gives
   double(.timeout = 1200) cost in order to make sure time is long enough.

2. If system RAM is larger than 100G, OOM goes to the cgroup limited way.

Signed-off-by: Li Wang <liwang@redhat.com>
---
 testcases/kernel/mem/oom/oom01.c | 12 ++++++++++++
 testcases/kernel/mem/oom/oom02.c | 12 ++++++++++++
 testcases/kernel/mem/oom/oom03.c |  1 +
 testcases/kernel/mem/oom/oom04.c | 11 +++++++++++
 testcases/kernel/mem/oom/oom05.c |  1 +
 5 files changed, 37 insertions(+)

diff --git a/testcases/kernel/mem/oom/oom01.c b/testcases/kernel/mem/oom/oom01.c
index 64a368e..056e02a 100644
--- a/testcases/kernel/mem/oom/oom01.c
+++ b/testcases/kernel/mem/oom/oom01.c
@@ -29,6 +29,8 @@
 #include <unistd.h>
 #include "mem.h"
 
+static unsigned long mem_total;
+
 static void verify_oom(void)
 {
 #if __WORDSIZE == 32
@@ -50,11 +52,20 @@ static void verify_oom(void)
 
 static void setup(void)
 {
+	mem_total = SAFE_READ_MEMINFO("MemTotal:");
+	if (mem_total > TESTMEM/1024*100L) {
+		tst_res(TINFO, "System RAM is larger than 100G, test in memory cgroup");
+		mount_mem("memcg", "cgroup", "memory", MEMCG_PATH, MEMCG_PATH_NEW);
+		write_memcg();
+	}
+
 	overcommit = get_sys_tune("overcommit_memory");
 }
 
 static void cleanup(void)
 {
+	if (mem_total > TESTMEM/1024*100L)
+		umount_mem(MEMCG_PATH, MEMCG_PATH_NEW);
 	set_sys_tune("overcommit_memory", overcommit, 0);
 }
 
@@ -62,6 +73,7 @@ static struct tst_test test = {
 	.tid = "oom01",
 	.needs_root = 1,
 	.forks_child = 1,
+	.timeout = 1200,
 	.setup = setup,
 	.cleanup = cleanup,
 	.test_all = verify_oom,
diff --git a/testcases/kernel/mem/oom/oom02.c b/testcases/kernel/mem/oom/oom02.c
index f315ee2..a1be806 100644
--- a/testcases/kernel/mem/oom/oom02.c
+++ b/testcases/kernel/mem/oom/oom02.c
@@ -32,6 +32,8 @@
 #if HAVE_NUMA_H && HAVE_LINUX_MEMPOLICY_H && HAVE_NUMAIF_H \
 	&& HAVE_MPOL_CONSTANTS
 
+static unsigned long mem_total;
+
 static void verify_oom(void)
 {
 #if __WORDSIZE == 32
@@ -50,6 +52,13 @@ static void verify_oom(void)
 
 static void setup(void)
 {
+	mem_total = SAFE_READ_MEMINFO("MemTotal:");
+	if (mem_total > TESTMEM/1024*100L) {
+		tst_res(TINFO, "System RAM is larger than 100G, test in memory cgroup");
+		mount_mem("memcg", "cgroup", "memory", MEMCG_PATH, MEMCG_PATH_NEW);
+		write_memcg();
+	}
+
 	if (!is_numa(NULL, NH_MEMS, 2))
 		tst_brk(TCONF, "The case need a NUMA system.");
 
@@ -59,6 +68,8 @@ static void setup(void)
 
 static void cleanup(void)
 {
+	if (mem_total > TESTMEM/1024*100L)
+		umount_mem(MEMCG_PATH, MEMCG_PATH_NEW);
 	set_sys_tune("overcommit_memory", overcommit, 0);
 }
 
@@ -66,6 +77,7 @@ static struct tst_test test = {
 	.tid = "oom02",
 	.needs_root = 1,
 	.forks_child = 1,
+	.timeout = 1200,
 	.setup = setup,
 	.cleanup = cleanup,
 	.test_all = verify_oom,
diff --git a/testcases/kernel/mem/oom/oom03.c b/testcases/kernel/mem/oom/oom03.c
index d3aed60..48c1ea5 100644
--- a/testcases/kernel/mem/oom/oom03.c
+++ b/testcases/kernel/mem/oom/oom03.c
@@ -79,6 +79,7 @@ static struct tst_test test = {
 	.tid = "oom03",
 	.needs_root = 1,
 	.forks_child = 1,
+	.timeout = 1200,
 	.setup = setup,
 	.cleanup = cleanup,
 	.test_all = verify_oom,
diff --git a/testcases/kernel/mem/oom/oom04.c b/testcases/kernel/mem/oom/oom04.c
index afe35b2..179a98a 100644
--- a/testcases/kernel/mem/oom/oom04.c
+++ b/testcases/kernel/mem/oom/oom04.c
@@ -32,6 +32,8 @@
 #if HAVE_NUMA_H && HAVE_LINUX_MEMPOLICY_H && HAVE_NUMAIF_H \
 	&& HAVE_MPOL_CONSTANTS
 
+static unsigned long mem_total;
+
 static void verify_oom(void)
 {
 #if __WORDSIZE == 32
@@ -63,6 +65,12 @@ static void setup(void)
 	overcommit = get_sys_tune("overcommit_memory");
 	set_sys_tune("overcommit_memory", 1, 1);
 
+	mem_total = SAFE_READ_MEMINFO("MemTotal:");
+	if (mem_total > TESTMEM/1024*100L) {
+		tst_res(TINFO, "System RAM is larger than 100G, test in memory cgroup");
+		mount_mem("memcg", "cgroup", "memory", MEMCG_PATH, MEMCG_PATH_NEW);
+		write_memcg();
+	}
 	mount_mem("cpuset", "cpuset", NULL, CPATH, CPATH_NEW);
 
 	/*
@@ -80,6 +88,8 @@ static void setup(void)
 
 static void cleanup(void)
 {
+	if (mem_total > TESTMEM/1024*100L)
+		umount_mem(MEMCG_PATH, MEMCG_PATH_NEW);
 	set_sys_tune("overcommit_memory", overcommit, 0);
 	umount_mem(CPATH, CPATH_NEW);
 }
@@ -88,6 +98,7 @@ static struct tst_test test = {
 	.tid = "oom04",
 	.needs_root = 1,
 	.forks_child = 1,
+	.timeout = 1200,
 	.setup = setup,
 	.cleanup = cleanup,
 	.test_all = verify_oom,
diff --git a/testcases/kernel/mem/oom/oom05.c b/testcases/kernel/mem/oom/oom05.c
index d97fe7d..39361a9 100644
--- a/testcases/kernel/mem/oom/oom05.c
+++ b/testcases/kernel/mem/oom/oom05.c
@@ -116,6 +116,7 @@ static struct tst_test test = {
 	.tid = "oom05",
 	.needs_root = 1,
 	.forks_child = 1,
+	.timeout = 1200,
 	.setup = setup,
 	.cleanup = cleanup,
 	.test_all = verify_oom,
-- 
2.9.3


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

* [LTP] [PATCH V2 04/14] mem/ksm: convert to new API
  2017-04-10 10:05 [LTP] [PATCH V2 00/14] Convert LTP kernel/mem/testcase to New API Li Wang
                   ` (2 preceding siblings ...)
  2017-04-10 10:05 ` [LTP] [PATCH V2 03/14] mem/oom: fix the timeout issue Li Wang
@ 2017-04-10 10:05 ` Li Wang
  2017-07-07 14:49   ` Cyril Hrubis
  2017-04-10 10:05 ` [LTP] [PATCH V2 05/14] mem/thp: " Li Wang
                   ` (9 subsequent siblings)
  13 siblings, 1 reply; 25+ messages in thread
From: Li Wang @ 2017-04-10 10:05 UTC (permalink / raw
  To: ltp

Recast ksm_options structure to support parameter transfer.

Signed-off-by: Li Wang <liwang@redhat.com>
---
 testcases/kernel/mem/include/mem.h |   9 ++-
 testcases/kernel/mem/ksm/ksm01.c   |  88 ++++++++++---------------
 testcases/kernel/mem/ksm/ksm02.c   | 123 ++++++++++++++---------------------
 testcases/kernel/mem/ksm/ksm03.c   |  90 ++++++++++---------------
 testcases/kernel/mem/ksm/ksm04.c   | 121 ++++++++++++++--------------------
 testcases/kernel/mem/ksm/ksm05.c   | 130 +++++++++++++++----------------------
 testcases/kernel/mem/ksm/ksm06.c   | 105 ++++++++++--------------------
 testcases/kernel/mem/lib/mem.c     |  16 ++---
 8 files changed, 266 insertions(+), 416 deletions(-)

diff --git a/testcases/kernel/mem/include/mem.h b/testcases/kernel/mem/include/mem.h
index 377cc55..bada549 100644
--- a/testcases/kernel/mem/include/mem.h
+++ b/testcases/kernel/mem/include/mem.h
@@ -44,6 +44,10 @@ void testoom(int mempolicy, int lite, int retcode, int allow_sigkill);
 
 #define PATH_KSM		"/sys/kernel/mm/ksm/"
 
+char *opt_numstr, *opt_sizestr, *opt_unitstr;
+
+void create_same_memory(int size, int num, int unit);
+void check_ksm_options(int *size, int *num, int *unit);
 void save_max_page_sharing(void);
 void restore_max_page_sharing(void);
 void test_ksm_merge_across_nodes(unsigned long nr_pages);
@@ -58,11 +62,6 @@ void test_ksm_merge_across_nodes(unsigned long nr_pages);
 #define PATH_SHMMAX		"/proc/sys/kernel/shmmax"
 
 void write_memcg(void);
-void create_same_memory(int size, int num, int unit);
-int  opt_num, opt_size, opt_unit;
-char *opt_numstr, *opt_sizestr, *opt_unitstr;
-void check_ksm_options(int *size, int *num, int *unit);
-void ksm_usage(void);
 
 /* cpuset/memcg */
 
diff --git a/testcases/kernel/mem/ksm/ksm01.c b/testcases/kernel/mem/ksm/ksm01.c
index 824881c..849d4d4 100644
--- a/testcases/kernel/mem/ksm/ksm01.c
+++ b/testcases/kernel/mem/ksm/ksm01.c
@@ -1,4 +1,16 @@
 /*
+ * Copyright (C) 2010-2017  Red Hat, Inc.
+ *
+ * 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.
+ *
  * Kernel Samepage Merging (KSM)
  *
  * Basic tests were to start several programs with same and different
@@ -31,28 +43,6 @@
  * - Change run setting to 2 - unmerging.
  * - Check ksm statistics and verify the content.
  * - Change run setting to 0 - stop.
- *
- * 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.
  */
 
 #include <sys/types.h>
@@ -66,45 +56,29 @@
 #include <stdlib.h>
 #include <string.h>
 #include <unistd.h>
-#include "test.h"
 #include "mem.h"
 
-char *TCID = "ksm01";
-int TST_TOTAL = 1;
-
 static int merge_across_nodes;
 
-option_t ksm_options[] = {
-	{"n:", &opt_num, &opt_numstr},
-	{"s:", &opt_size, &opt_sizestr},
-	{"u:", &opt_unit, &opt_unitstr},
+static struct tst_option ksm_options[] = {
+	{"n:", &opt_numstr,  "-n       Number of processes"},
+	{"s:", &opt_sizestr, "-s       Memory allocation size in MB"},
+	{"u:", &opt_unitstr, "-u       Memory allocation unit in MB"},
 	{NULL, NULL, NULL}
 };
 
-int main(int argc, char *argv[])
+static void verify_ksm(void)
 {
-	int lc;
 	int size = 128, num = 3, unit = 1;
 
-	tst_parse_opts(argc, argv, ksm_options, ksm_usage);
-	setup();
-	for (lc = 0; TEST_LOOPING(lc); lc++) {
-		tst_count = 0;
-		check_ksm_options(&size, &num, &unit);
-		create_same_memory(size, num, unit);
-	}
-	cleanup();
-	tst_exit();
+	check_ksm_options(&size, &num, &unit);
+	create_same_memory(size, num, unit);
 }
 
-void setup(void)
+static void setup(void)
 {
-	tst_require_root();
-
-	if (tst_kvercmp(2, 6, 32) < 0)
-		tst_brkm(TCONF, NULL, "2.6.32 or greater kernel required");
 	if (access(PATH_KSM, F_OK) == -1)
-		tst_brkm(TCONF, NULL, "KSM configuration is not enabled");
+		tst_brk(TCONF, "KSM configuration is not enabled");
 
 	save_max_page_sharing();
 
@@ -116,16 +90,13 @@ void setup(void)
 	 * it is enabled before testing.
 	 */
 	if (access(PATH_KSM "merge_across_nodes", F_OK) == 0) {
-		SAFE_FILE_SCANF(NULL, PATH_KSM "merge_across_nodes",
+		SAFE_FILE_SCANF(PATH_KSM "merge_across_nodes",
 				"%d", &merge_across_nodes);
-		SAFE_FILE_PRINTF(NULL, PATH_KSM "merge_across_nodes", "1");
+		SAFE_FILE_PRINTF(PATH_KSM "merge_across_nodes", "1");
 	}
-
-	tst_sig(FORK, DEF_HANDLER, NULL);
-	TEST_PAUSE;
 }
 
-void cleanup(void)
+static void cleanup(void)
 {
 	if (access(PATH_KSM "merge_across_nodes", F_OK) == 0)
 		FILE_PRINTF(PATH_KSM "merge_across_nodes",
@@ -133,3 +104,14 @@ void cleanup(void)
 
 	restore_max_page_sharing();
 }
+
+static struct tst_test test = {
+	.tid = "ksm01",
+	.needs_root = 1,
+	.forks_child = 1,
+	.options = ksm_options,
+	.setup = setup,
+	.cleanup = cleanup,
+	.test_all = verify_ksm,
+	.min_kver = "2.6.32",
+};
diff --git a/testcases/kernel/mem/ksm/ksm02.c b/testcases/kernel/mem/ksm/ksm02.c
index 6274f8a..eeebfef 100644
--- a/testcases/kernel/mem/ksm/ksm02.c
+++ b/testcases/kernel/mem/ksm/ksm02.c
@@ -1,5 +1,17 @@
 /*
- * Kernel Samepage Merging (KSM) for NUMA
+ * Copyright (C) 2010-2017  Red Hat, Inc.
+ *
+ * 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.
+ *
+ * Kernel Samepage Merging (KSM)
  *
  * Basic tests were to start several programs with same and different
  * memory contents and ensure only to merge the ones with the same
@@ -31,28 +43,6 @@
  * - Change run setting to 2 - unmerging.
  * - Check ksm statistics and verify the content.
  * - Change run setting to 0 - stop.
- *
- * 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.
  */
 
 #include "config.h"
@@ -66,60 +56,45 @@
 #include <signal.h>
 #include <stdio.h>
 #include <unistd.h>
-#include "test.h"
 #include "mem.h"
 
-char *TCID = "ksm02";
-int TST_TOTAL = 1;
+#if HAVE_NUMA_H && HAVE_LINUX_MEMPOLICY_H && HAVE_NUMAIF_H \
+	&& HAVE_MPOL_CONSTANTS
 
 static int merge_across_nodes;
 
-#if HAVE_NUMA_H && HAVE_LINUX_MEMPOLICY_H && HAVE_NUMAIF_H \
-	&& HAVE_MPOL_CONSTANTS
-option_t ksm_options[] = {
-	{"n:", &opt_num, &opt_numstr},
-	{"s:", &opt_size, &opt_sizestr},
-	{"u:", &opt_unit, &opt_unitstr},
+static struct tst_option ksm_options[] = {
+	{"n:", &opt_numstr,  "-n       Number of processes"},
+	{"s:", &opt_sizestr, "-s       Memory allocation size in MB"},
+	{"u:", &opt_unitstr, "-u       Memory allocation unit in MB"},
 	{NULL, NULL, NULL}
 };
 
-int main(int argc, char *argv[])
+static void verify_ksm(void)
 {
-	int lc;
 	int size = 128, num = 3, unit = 1;
 	unsigned long nmask[MAXNODES / BITS_PER_LONG] = { 0 };
 	unsigned int node;
 
-	tst_parse_opts(argc, argv, ksm_options, ksm_usage);
-
-	node = get_a_numa_node(tst_exit);
+	node = get_a_numa_node();
 	set_node(nmask, node);
 
-	setup();
-
-	for (lc = 0; TEST_LOOPING(lc); lc++) {
-		tst_count = 0;
-		check_ksm_options(&size, &num, &unit);
-
-		if (set_mempolicy(MPOL_BIND, nmask, MAXNODES) == -1) {
-			if (errno != ENOSYS)
-				tst_brkm(TBROK | TERRNO, cleanup,
-					 "set_mempolicy");
-			else
-				tst_brkm(TCONF, cleanup,
-					 "set_mempolicy syscall is not "
-					 "implemented on your system.");
-		}
-		create_same_memory(size, num, unit);
-
-		write_cpusets(node);
-		create_same_memory(size, num, unit);
+	check_ksm_options(&size, &num, &unit);
+
+	if (set_mempolicy(MPOL_BIND, nmask, MAXNODES) == -1) {
+		if (errno != ENOSYS)
+			tst_brk(TBROK | TERRNO, "set_mempolicy");
+		else
+			tst_brk(TCONF, "set_mempolicy syscall is not "
+				 "implemented on your system.");
 	}
-	cleanup();
-	tst_exit();
+	create_same_memory(size, num, unit);
+
+	write_cpusets(node);
+	create_same_memory(size, num, unit);
 }
 
-void cleanup(void)
+static void cleanup(void)
 {
 	if (access(PATH_KSM "merge_across_nodes", F_OK) == 0)
 		FILE_PRINTF(PATH_KSM "merge_across_nodes",
@@ -130,30 +105,32 @@ void cleanup(void)
 	umount_mem(CPATH, CPATH_NEW);
 }
 
-void setup(void)
+static void setup(void)
 {
-	tst_require_root();
-
-	if (tst_kvercmp(2, 6, 32) < 0)
-		tst_brkm(TCONF, NULL, "2.6.32 or greater kernel required");
 	if (access(PATH_KSM, F_OK) == -1)
-		tst_brkm(TCONF, NULL, "KSM configuration is not enabled");
+		tst_brk(TCONF, "KSM configuration is not enabled");
 	save_max_page_sharing();
 
 	if (access(PATH_KSM "merge_across_nodes", F_OK) == 0) {
-		SAFE_FILE_SCANF(NULL, PATH_KSM "merge_across_nodes",
+		SAFE_FILE_SCANF(PATH_KSM "merge_across_nodes",
 				"%d", &merge_across_nodes);
-		SAFE_FILE_PRINTF(NULL, PATH_KSM "merge_across_nodes", "1");
+		SAFE_FILE_PRINTF(PATH_KSM "merge_across_nodes", "1");
 	}
 
-	tst_sig(FORK, DEF_HANDLER, cleanup);
-	TEST_PAUSE;
 	mount_mem("cpuset", "cpuset", NULL, CPATH, CPATH_NEW);
 }
 
+static struct tst_test test = {
+	.tid = "ksm02",
+	.needs_root = 1,
+	.forks_child = 1,
+	.options = ksm_options,
+	.setup = setup,
+	.cleanup = cleanup,
+	.test_all = verify_ksm,
+	.min_kver = "2.6.32",
+};
+
 #else /* no NUMA */
-int main(void)
-{
-	tst_brkm(TCONF, NULL, "no NUMA development packages installed.");
-}
+	TST_TEST_TCONF("no NUMA development packages installed.");
 #endif
diff --git a/testcases/kernel/mem/ksm/ksm03.c b/testcases/kernel/mem/ksm/ksm03.c
index 979222a..9fd5aac 100644
--- a/testcases/kernel/mem/ksm/ksm03.c
+++ b/testcases/kernel/mem/ksm/ksm03.c
@@ -1,4 +1,16 @@
 /*
+ * Copyright (C) 2010-2017  Red Hat, Inc.
+ *
+ * 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.
+ *
  * Kernel Samepage Merging (KSM) for Memory Resource Controller
  *
  * Basic tests were to start several programs with same and different
@@ -31,28 +43,6 @@
  * - Change run setting to 2 - unmerging.
  * - Check ksm statistics and verify the content.
  * - Change run setting to 0 - stop.
- *
- * 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.
  */
 
 #include <sys/types.h>
@@ -66,61 +56,42 @@
 #include <stdlib.h>
 #include <string.h>
 #include <unistd.h>
-#include "test.h"
 #include "mem.h"
 
-char *TCID = "ksm03";
-int TST_TOTAL = 1;
-
 static int merge_across_nodes;
 
-option_t ksm_options[] = {
-	{"n:", &opt_num, &opt_numstr},
-	{"s:", &opt_size, &opt_sizestr},
-	{"u:", &opt_unit, &opt_unitstr},
+static struct tst_option ksm_options[] = {
+	{"n:", &opt_numstr,  "-n       Number of processes"},
+	{"s:", &opt_sizestr, "-s       Memory allocation size in MB"},
+	{"u:", &opt_unitstr, "-u       Memory allocation unit in MB"},
 	{NULL, NULL, NULL}
 };
 
-int main(int argc, char *argv[])
+static void verify_ksm(void)
 {
-	int lc;
 	int size = 128, num = 3, unit = 1;
 
-	tst_parse_opts(argc, argv, ksm_options, ksm_usage);
-	setup();
-	for (lc = 0; TEST_LOOPING(lc); lc++) {
-		tst_count = 0;
-		check_ksm_options(&size, &num, &unit);
-		write_memcg();
-		create_same_memory(size, num, unit);
-	}
-	cleanup();
-	tst_exit();
+	check_ksm_options(&size, &num, &unit);
+	write_memcg();
+	create_same_memory(size, num, unit);
 }
 
-void setup(void)
+static void setup(void)
 {
-	tst_require_root();
-
-	if (tst_kvercmp(2, 6, 32) < 0)
-		tst_brkm(TCONF, NULL, "2.6.32 or greater kernel required");
 	if (access(PATH_KSM, F_OK) == -1)
-		tst_brkm(TCONF, NULL, "KSM configuration is not enabled");
+		tst_brk(TCONF, "KSM configuration is not enabled");
 
 	if (access(PATH_KSM "merge_across_nodes", F_OK) == 0) {
-		SAFE_FILE_SCANF(NULL, PATH_KSM "merge_across_nodes",
+		SAFE_FILE_SCANF(PATH_KSM "merge_across_nodes",
 				"%d", &merge_across_nodes);
-		SAFE_FILE_PRINTF(NULL, PATH_KSM "merge_across_nodes", "1");
+		SAFE_FILE_PRINTF(PATH_KSM "merge_across_nodes", "1");
 	}
 
 	save_max_page_sharing();
-
 	mount_mem("memcg", "cgroup", "memory", MEMCG_PATH, MEMCG_PATH_NEW);
-	tst_sig(FORK, DEF_HANDLER, NULL);
-	TEST_PAUSE;
 }
 
-void cleanup(void)
+static void cleanup(void)
 {
 	if (access(PATH_KSM "merge_across_nodes", F_OK) == 0)
 		FILE_PRINTF(PATH_KSM "merge_across_nodes",
@@ -130,3 +101,14 @@ void cleanup(void)
 
 	umount_mem(MEMCG_PATH, MEMCG_PATH_NEW);
 }
+
+static struct tst_test test = {
+	.tid = "ksm03",
+	.needs_root = 1,
+	.forks_child = 1,
+	.options = ksm_options,
+	.setup = setup,
+	.cleanup = cleanup,
+	.test_all = verify_ksm,
+	.min_kver = "2.6.32",
+};
diff --git a/testcases/kernel/mem/ksm/ksm04.c b/testcases/kernel/mem/ksm/ksm04.c
index 4beeed6..29a5e2b 100644
--- a/testcases/kernel/mem/ksm/ksm04.c
+++ b/testcases/kernel/mem/ksm/ksm04.c
@@ -1,4 +1,16 @@
 /*
+ * Copyright (C) 2010-2017  Red Hat, Inc.
+ *
+ * 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.
+ *
  * Kernel Samepage Merging (KSM) for Memory Resource Controller and NUMA
  *
  * Basic tests were to start several programs with same and different
@@ -31,28 +43,6 @@
  * - Change run setting to 2 - unmerging.
  * - Check ksm statistics and verify the content.
  * - Change run setting to 0 - stop.
- *
- * 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.
  */
 
 #include "config.h"
@@ -66,62 +56,47 @@
 #include <signal.h>
 #include <stdio.h>
 #include <unistd.h>
-#include "test.h"
 #include "mem.h"
 
-char *TCID = "ksm04";
-int TST_TOTAL = 1;
+#if HAVE_NUMA_H && HAVE_LINUX_MEMPOLICY_H && HAVE_NUMAIF_H \
+	&& HAVE_MPOL_CONSTANTS
 
 static int merge_across_nodes;
 
-#if HAVE_NUMA_H && HAVE_LINUX_MEMPOLICY_H && HAVE_NUMAIF_H \
-	&& HAVE_MPOL_CONSTANTS
-option_t ksm_options[] = {
-	{"n:", &opt_num, &opt_numstr},
-	{"s:", &opt_size, &opt_sizestr},
-	{"u:", &opt_unit, &opt_unitstr},
+static struct tst_option ksm_options[] = {
+	{"n:", &opt_numstr,  "-n       Number of processes"},
+	{"s:", &opt_sizestr, "-s       Memory allocation size in MB"},
+	{"u:", &opt_unitstr, "-u       Memory allocation unit in MB"},
 	{NULL, NULL, NULL}
 };
 
-int main(int argc, char *argv[])
+static void verify_ksm(void)
 {
-	int lc;
 	int size = 128, num = 3, unit = 1;
 	unsigned long nmask[MAXNODES / BITS_PER_LONG] = { 0 };
 	unsigned int node;
 
-	tst_parse_opts(argc, argv, ksm_options, ksm_usage);
-
-	node = get_a_numa_node(tst_exit);
+	node = get_a_numa_node();
 	set_node(nmask, node);
 
-	setup();
-
-	for (lc = 0; TEST_LOOPING(lc); lc++) {
-		tst_count = 0;
-		check_ksm_options(&size, &num, &unit);
+	check_ksm_options(&size, &num, &unit);
 
-		write_memcg();
+	write_memcg();
 
-		if (set_mempolicy(MPOL_BIND, nmask, MAXNODES) == -1) {
-			if (errno != ENOSYS)
-				tst_brkm(TBROK | TERRNO, cleanup,
-					 "set_mempolicy");
-			else
-				tst_brkm(TCONF, cleanup,
-					 "set_mempolicy syscall is not "
-					 "implemented on your system.");
-		}
-		create_same_memory(size, num, unit);
-
-		write_cpusets(node);
-		create_same_memory(size, num, unit);
+	if (set_mempolicy(MPOL_BIND, nmask, MAXNODES) == -1) {
+		if (errno != ENOSYS)
+			tst_brk(TBROK | TERRNO, "set_mempolicy");
+		else
+			tst_brk(TCONF, "set_mempolicy syscall is not "
+					"implemented on your system.");
 	}
-	cleanup();
-	tst_exit();
+	create_same_memory(size, num, unit);
+
+	write_cpusets(node);
+	create_same_memory(size, num, unit);
 }
 
-void cleanup(void)
+static void cleanup(void)
 {
 	if (access(PATH_KSM "merge_across_nodes", F_OK) == 0)
 		FILE_PRINTF(PATH_KSM "merge_across_nodes",
@@ -133,32 +108,34 @@ void cleanup(void)
 	umount_mem(MEMCG_PATH, MEMCG_PATH_NEW);
 }
 
-void setup(void)
+static void setup(void)
 {
-	tst_require_root();
-
-	if (tst_kvercmp(2, 6, 32) < 0)
-		tst_brkm(TCONF, NULL, "2.6.32 or greater kernel required");
 	if (access(PATH_KSM, F_OK) == -1)
-		tst_brkm(TCONF, NULL, "KSM configuration is not enabled");
+		tst_brk(TCONF, "KSM configuration is not enabled");
 
 	if (access(PATH_KSM "merge_across_nodes", F_OK) == 0) {
-		SAFE_FILE_SCANF(NULL, PATH_KSM "merge_across_nodes",
+		SAFE_FILE_SCANF(PATH_KSM "merge_across_nodes",
 				"%d", &merge_across_nodes);
-		SAFE_FILE_PRINTF(NULL, PATH_KSM "merge_across_nodes", "1");
+		SAFE_FILE_PRINTF(PATH_KSM "merge_across_nodes", "1");
 	}
 
 	save_max_page_sharing();
 
-	tst_sig(FORK, DEF_HANDLER, cleanup);
-	TEST_PAUSE;
 	mount_mem("cpuset", "cpuset", NULL, CPATH, CPATH_NEW);
 	mount_mem("memcg", "cgroup", "memory", MEMCG_PATH, MEMCG_PATH_NEW);
 }
 
+static struct tst_test test = {
+	.tid = "ksm04",
+	.needs_root = 1,
+	.forks_child = 1,
+	.options = ksm_options,
+	.setup = setup,
+	.cleanup = cleanup,
+	.test_all = verify_ksm,
+	.min_kver = "2.6.32",
+};
+
 #else /* no NUMA */
-int main(void)
-{
-	tst_brkm(TCONF, NULL, "no NUMA development packages installed.");
-}
+	TST_TEST_TCONF("no NUMA development packages installed.");
 #endif
diff --git a/testcases/kernel/mem/ksm/ksm05.c b/testcases/kernel/mem/ksm/ksm05.c
index 59b2683..59c8f4b 100644
--- a/testcases/kernel/mem/ksm/ksm05.c
+++ b/testcases/kernel/mem/ksm/ksm05.c
@@ -1,4 +1,16 @@
 /*
+ * Copyright (C) 2011-2017  Red Hat, Inc.
+ *
+ * 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.
+ *
  * KSM - NULL pointer dereference in ksm_do_scan() (CVE-2011-2183)
  *
  * This is a testcase from upstream commit:
@@ -29,94 +41,55 @@
  * *) ksm and ksmtuned daemons need to be disabled. Otherwise, it could
  *    distrub the testing as they also change some ksm tunables depends
  *    on current workloads.
- *
- * Copyright (C) 2011  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.
  */
 
-#include <sys/types.h>
-#include <sys/mman.h>
-#include <sys/stat.h>
 #include <sys/wait.h>
-#include <errno.h>
-#include <fcntl.h>
 #include <signal.h>
-#include <stdio.h>
 #include <stdlib.h>
-#include <string.h>
-#include <unistd.h>
-#include "test.h"
+#include <errno.h>
+#include "tst_test.h"
 #include "mem.h"
 
-char *TCID = "ksm05";
-int TST_TOTAL = 1;
-
 #ifdef HAVE_MADV_MERGEABLE
 
 static int ksm_run_orig;
-
 static void sighandler(int sig);
 
-int main(int argc, char *argv[])
+static void test_ksm(void)
 {
-	int lc, status, ret;
+	int status, ret;
 	long ps;
 	pid_t pid;
 	void *ptr;
-
-	tst_parse_opts(argc, argv, NULL, NULL);
-
+	struct sigaction sa;
+
+	memset (&sa, '\0', sizeof(sa));
+	sa.sa_handler = sighandler;
+	sa.sa_flags = 0;
+	TEST(sigaction(SIGSEGV, &sa, NULL));
+	if (TEST_RETURN == -1)
+		tst_brk(TBROK | TRERRNO,
+				"SIGSEGV signal setup failed");
 	ps = sysconf(_SC_PAGESIZE);
-	setup();
-
-	for (lc = 0; TEST_LOOPING(lc); lc++) {
-		tst_count = 0;
-
-		switch (pid = fork()) {
-		case -1:
-			tst_brkm(TBROK | TERRNO, cleanup, "fork");
+	switch (pid = SAFE_FORK()) {
 		case 0:
 			ret = posix_memalign(&ptr, ps, ps);
 			if (ret) {
-				tst_brkm(TBROK, cleanup, "posix_memalign(): %s",
-				         tst_strerrno(ret));
+				tst_brk(TBROK, "posix_memalign(): %s",
+						tst_strerrno(ret));
 			}
 			if (madvise(ptr, ps, MADV_MERGEABLE) < 0)
-				tst_brkm(TBROK | TERRNO, cleanup, "madvise");
+				tst_brk(TBROK | TERRNO, "madvise");
 			*(char *)NULL = 0;	/* SIGSEGV occurs as expected. */
-			exit(0);
 		default:
 			break;
-		}
-		if (waitpid(pid, &status, WUNTRACED | WCONTINUED) == -1)
-			tst_brkm(TBROK | TERRNO, cleanup, "waitpid");
-		if (!WIFEXITED(status) || WEXITSTATUS(status) != 0)
-			tst_brkm(TBROK, cleanup,
-				 "invalid signal received: %d", status);
 	}
+	if (waitpid(pid, &status, WUNTRACED | WCONTINUED) == -1)
+		tst_brk(TBROK | TERRNO, "waitpid");
+	if (!WIFEXITED(status) || WEXITSTATUS(status) != 0)
+		tst_brk(TBROK, "invalid signal received: %d", status);
 
-	tst_resm(TPASS, "still alive.");
-	cleanup();
-	tst_exit();
+	tst_res(TPASS, "still alive.");
 }
 
 static void sighandler(int sig)
@@ -124,35 +97,34 @@ static void sighandler(int sig)
 	_exit((sig == SIGSEGV) ? 0 : sig);
 }
 
-void setup(void)
+static void setup(void)
 {
-	tst_require_root();
-
-	if (tst_kvercmp(2, 6, 32) < 0)
-		tst_brkm(TCONF, NULL, "2.6.32 or greater kernel required.");
-
 	if (access(PATH_KSM, F_OK) == -1)
-		tst_brkm(TCONF, NULL, "KSM configuration is not enabled");
-
-	tst_sig(FORK, sighandler, cleanup);
-
-	TEST_PAUSE;
+		tst_brk(TCONF, "KSM configuration is not enabled");
 
 	/* save original /sys/kernel/mm/ksm/run value */
-	SAFE_FILE_SCANF(NULL, PATH_KSM "run", "%d", &ksm_run_orig);
+	SAFE_FILE_SCANF(PATH_KSM "run", "%d", &ksm_run_orig);
 
 	/* echo 1 > /sys/kernel/mm/ksm/run */
-	SAFE_FILE_PRINTF(NULL, PATH_KSM "run", "1");
+	SAFE_FILE_PRINTF(PATH_KSM "run", "1");
 }
 
-void cleanup(void)
+static void cleanup(void)
 {
 	/* restore /sys/kernel/mm/ksm/run value */
 	FILE_PRINTF(PATH_KSM "run", "%d", ksm_run_orig);
 }
+
+static struct tst_test test = {
+	.tid = "ksm05",
+	.needs_root = 1,
+	.forks_child = 1,
+	.setup = setup,
+	.cleanup = cleanup,
+	.test_all = test_ksm,
+	.min_kver = "2.6.32",
+};
+
 #else
-int main(void)
-{
-	tst_brkm(TCONF, NULL, "no MADV_MERGEABLE found.");
-}
+	TST_TEST_TCONF("no MADV_MERGEABLE found.");
 #endif
diff --git a/testcases/kernel/mem/ksm/ksm06.c b/testcases/kernel/mem/ksm/ksm06.c
index 2092746..96c91b0 100644
--- a/testcases/kernel/mem/ksm/ksm06.c
+++ b/testcases/kernel/mem/ksm/ksm06.c
@@ -1,25 +1,15 @@
 /*
- * Copyright (C) 2013 Linux Test Project
+ * Copyright (C) 2013-2017  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 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 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.
+ * 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.
  */
 
 /*
@@ -35,6 +25,7 @@
 #include <sys/types.h>
 #include <sys/stat.h>
 #include <sys/mman.h>
+#include <limits.h>
 #include <errno.h>
 #include <fcntl.h>
 #if HAVE_NUMAIF_H
@@ -44,92 +35,68 @@
 #include <stdio.h>
 #include <unistd.h>
 #include "numa_helper.h"
-#include "test.h"
-#include "safe_macros.h"
 #include "mem.h"
 
-char *TCID = "ksm06";
-int TST_TOTAL = 1;
-
 #if HAVE_NUMA_H && HAVE_LINUX_MEMPOLICY_H && HAVE_NUMAIF_H \
 	&& HAVE_MPOL_CONSTANTS
 
 static int run;
 static int sleep_millisecs;
 static int merge_across_nodes;
-static int n_flag;
 static unsigned long nr_pages;
 
-static char *n_opt;
-option_t options[] = {
-	{ "n:", &n_flag, &n_opt },
-	{ NULL, NULL, NULL }
+char *n_opt;
+static struct tst_option ksm_options[] = {
+	{"n:", &n_opt,  "-n x    Allocate x pages memory per node"},
+	{NULL, NULL, NULL}
 };
-static void usage(void);
 
-int main(int argc, char *argv[])
+static void test_ksm(void)
 {
-	int lc;
-
-	tst_parse_opts(argc, argv, options, &usage);
-
-	if (n_flag)
-		nr_pages = SAFE_STRTOUL(NULL, n_opt, 0, ULONG_MAX);
+	if (n_opt)
+		nr_pages = SAFE_STRTOUL(n_opt, 0, ULONG_MAX);
 	else
 		nr_pages = 100;
 
-	setup();
-
-	for (lc = 0; TEST_LOOPING(lc); lc++) {
-		tst_count = 0;
-
-		test_ksm_merge_across_nodes(nr_pages);
-	}
-
-	cleanup();
-	tst_exit();
+	test_ksm_merge_across_nodes(nr_pages);
 }
 
-void setup(void)
+static void setup(void)
 {
 	if (access(PATH_KSM "merge_across_nodes", F_OK) == -1)
-		tst_brkm(TCONF, NULL, "no merge_across_nodes sysfs knob");
+		tst_brk(TCONF, "no merge_across_nodes sysfs knob");
 
 	if (!is_numa(NULL, NH_MEMS, 2))
-		tst_brkm(TCONF, NULL, "The case needs a NUMA system.");
+		tst_brk(TCONF, "The case needs a NUMA system.");
 
 	/* save the current value */
-	SAFE_FILE_SCANF(NULL, PATH_KSM "run", "%d", &run);
-	SAFE_FILE_SCANF(NULL, PATH_KSM "merge_across_nodes",
+	SAFE_FILE_SCANF(PATH_KSM "run", "%d", &run);
+	SAFE_FILE_SCANF(PATH_KSM "merge_across_nodes",
 			"%d", &merge_across_nodes);
-	SAFE_FILE_SCANF(NULL, PATH_KSM "sleep_millisecs",
+	SAFE_FILE_SCANF(PATH_KSM "sleep_millisecs",
 			"%d", &sleep_millisecs);
 
 	save_max_page_sharing();
-
-	tst_sig(FORK, DEF_HANDLER, cleanup);
-	TEST_PAUSE;
 }
 
-void cleanup(void)
+static void cleanup(void)
 {
-	FILE_PRINTF(PATH_KSM "merge_across_nodes",
-			 "%d", merge_across_nodes);
-	FILE_PRINTF(PATH_KSM "sleep_millisecs",
-			 "%d", sleep_millisecs);
+	FILE_PRINTF(PATH_KSM "merge_across_nodes", "%d", merge_across_nodes);
+	FILE_PRINTF(PATH_KSM "sleep_millisecs", "%d", sleep_millisecs);
 	FILE_PRINTF(PATH_KSM "run", "%d", run);
 
 	restore_max_page_sharing();
 }
 
-static void usage(void)
-{
-	printf("  -n x    Allocate x pages memory per node\n");
-}
+static struct tst_test test = {
+	.tid = "ksm06",
+	.needs_root = 1,
+	.options = ksm_options,
+	.setup = setup,
+	.cleanup = cleanup,
+	.test_all = test_ksm,
+};
 
 #else /* no NUMA */
-int main(void)
-{
-	tst_brkm(TCONF, NULL, "no NUMA development packages installed.");
-}
+	TST_TEST_TCONF("no NUMA development packages installed.");
 #endif
diff --git a/testcases/kernel/mem/lib/mem.c b/testcases/kernel/mem/lib/mem.c
index 053737b..f5d86c7 100644
--- a/testcases/kernel/mem/lib/mem.c
+++ b/testcases/kernel/mem/lib/mem.c
@@ -266,9 +266,10 @@ static void check(char *path, long int value)
 	snprintf(fullpath, BUFSIZ, PATH_KSM "%s", path);
 	SAFE_FILE_SCANF(fullpath, "%ld", &actual_val);
 
-	tst_res(TINFO, "%s is %ld.", path, actual_val);
 	if (actual_val != value)
 		tst_res(TFAIL, "%s is not %ld.", path, value);
+	else
+		tst_res(TPASS, "%s is %ld.", path, actual_val);
 }
 
 static void wait_ksmd_done(void)
@@ -636,12 +637,12 @@ void test_ksm_merge_across_nodes(unsigned long nr_pages)
 
 void check_ksm_options(int *size, int *num, int *unit)
 {
-	if (opt_size) {
+	if (opt_sizestr) {
 		*size = atoi(opt_sizestr);
 		if (*size < 1)
 			tst_brk(TBROK, "size cannot be less than 1.");
 	}
-	if (opt_unit) {
+	if (opt_unitstr) {
 		*unit = atoi(opt_unitstr);
 		if (*unit > *size)
 			tst_brk(TBROK,
@@ -651,7 +652,7 @@ void check_ksm_options(int *size, int *num, int *unit)
 				 "the remainder of division of size by unit is "
 				 "not zero.");
 	}
-	if (opt_num) {
+	if (opt_numstr) {
 		*num = atoi(opt_numstr);
 		if (*num < 3)
 			tst_brk(TBROK,
@@ -659,13 +660,6 @@ void check_ksm_options(int *size, int *num, int *unit)
 	}
 }
 
-void ksm_usage(void)
-{
-	printf("  -n      Number of processes\n");
-	printf("  -s      Memory allocation size in MB\n");
-	printf("  -u      Memory allocation unit in MB\n");
-}
-
 /* THP */
 
 /* cpuset/memcg */
-- 
2.9.3


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

* [LTP] [PATCH V2 05/14] mem/thp: convert to new API
  2017-04-10 10:05 [LTP] [PATCH V2 00/14] Convert LTP kernel/mem/testcase to New API Li Wang
                   ` (3 preceding siblings ...)
  2017-04-10 10:05 ` [LTP] [PATCH V2 04/14] mem/ksm: convert to new API Li Wang
@ 2017-04-10 10:05 ` Li Wang
  2017-07-07 15:27   ` Cyril Hrubis
  2017-04-10 10:05 ` [LTP] [PATCH V2 06/14] mem/hugetlb: " Li Wang
                   ` (8 subsequent siblings)
  13 siblings, 1 reply; 25+ messages in thread
From: Li Wang @ 2017-04-10 10:05 UTC (permalink / raw
  To: ltp

Signed-off-by: Li Wang <liwang@redhat.com>
---
 runtest/mm                       |  2 +-
 testcases/kernel/mem/thp/thp01.c | 85 +++++++++++++++----------------------
 testcases/kernel/mem/thp/thp02.c | 89 ++++++++++++--------------------------
 testcases/kernel/mem/thp/thp03.c | 92 +++++++++++++---------------------------
 4 files changed, 92 insertions(+), 176 deletions(-)

diff --git a/runtest/mm b/runtest/mm
index d108d18..fd37f1c 100644
--- a/runtest/mm
+++ b/runtest/mm
@@ -81,7 +81,7 @@ oom05 oom05
 
 swapping01 swapping01 -i 5
 
-thp01 thp01 -I 120
+thp01 thp01 -I 10
 thp02 thp02
 thp03 thp03
 
diff --git a/testcases/kernel/mem/thp/thp01.c b/testcases/kernel/mem/thp/thp01.c
index e1eaa50..37124f6 100644
--- a/testcases/kernel/mem/thp/thp01.c
+++ b/testcases/kernel/mem/thp/thp01.c
@@ -1,4 +1,18 @@
 /*
+ * Copyright (C) 2011-2017  Red Hat, Inc.
+ *
+ * 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.
+ *
+ * Description:
+ *
  * This is a reproducer of CVE-2011-0999, which fixed by mainline commit
  * a7d6e4ecdb7648478ddec76d30d87d03d6e22b31:
  *
@@ -13,27 +27,6 @@
  * invalid opcode: 0000 [#1] SMP
  * last sysfs file: /sys/devices/system/cpu/cpu23/cache/index2/shared_cpu_map
  * ....
- *
- * Copyright (C) 2011  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.
  */
 
 #include <sys/types.h>
@@ -41,11 +34,9 @@
 #include <sys/wait.h>
 #include <stdio.h>
 #include <string.h>
+#include <stdlib.h>
 #include <unistd.h>
-#include "test.h"
-
-char *TCID = "thp01";
-int TST_TOTAL = 1;
+#include "mem.h"
 
 #define ARRAY_SZ	256
 
@@ -58,22 +49,12 @@ static struct rlimit rl = {
 	.rlim_max = RLIM_INFINITY,
 };
 
-static void setup(void);
-static void cleanup(void);
-
-int main(int argc, char **argv)
+static void thp_test(void)
 {
-	int i, lc, st;
+	int i, st;
 	pid_t pid;
 
-	tst_parse_opts(argc, argv, NULL, NULL);
-
-	setup();
-
-	for (lc = 0; TEST_LOOPING(lc); lc++) {
-		switch (pid = fork()) {
-		case -1:
-			tst_brkm(TBROK | TERRNO, cleanup, "fork");
+	switch (pid = SAFE_FORK()) {
 		case 0:
 			memset(arg, 'c', length - 1);
 			arg[length - 1] = '\0';
@@ -91,29 +72,31 @@ int main(int argc, char **argv)
 			}
 		default:
 			if (waitpid(pid, &st, 0) == -1)
-				tst_brkm(TBROK | TERRNO, cleanup, "waitpid");
+				tst_brk(TBROK | TERRNO, "waitpid");
 			if (!WIFEXITED(st) || WEXITSTATUS(st) != 0)
-				tst_brkm(TBROK, cleanup,
-					 "child exited abnormally");
-		}
+				tst_brk(TBROK, "child exited abnormally");
 	}
-	tst_resm(TPASS, "system didn't crash, pass.");
-	cleanup();
-	tst_exit();
+
+	tst_res(TPASS, "system didn't crash, pass.");
 }
 
 static void setup(void)
 {
 	ps = sysconf(_SC_PAGESIZE);
 	length = 32 * ps;
-	arg = malloc(length);
-	if (arg == NULL)
-		tst_brkm(TBROK | TERRNO, NULL, "malloc");
-
-	tst_sig(FORK, DEF_HANDLER, cleanup);
-	TEST_PAUSE;
+	arg = SAFE_MALLOC(length);
 }
 
 static void cleanup(void)
 {
+	free(arg);
 }
+
+static struct tst_test test = {
+	.tid = "thp01",
+	.needs_root = 1,
+	.forks_child = 1,
+	.setup = setup,
+	.cleanup = cleanup,
+	.test_all = thp_test,
+};
diff --git a/testcases/kernel/mem/thp/thp02.c b/testcases/kernel/mem/thp/thp02.c
index eb919aa..45cfbe5 100644
--- a/testcases/kernel/mem/thp/thp02.c
+++ b/testcases/kernel/mem/thp/thp02.c
@@ -1,24 +1,15 @@
 /*
- * Copyright (C) 2011  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.
+ * Copyright (C) 2011-2017  Red Hat, Inc.
  *
- * 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.
+ * 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.
  *
- * 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.
+ * 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.
  *
  * thp02 - detect mremap bug when THP is enabled.
  *
@@ -52,36 +43,11 @@
 #include <unistd.h>
 #include "mem.h"
 
-char *TCID = "thp02";
-int TST_TOTAL = 1;
-
 #ifdef HAVE_MREMAP_FIXED
 static int ps;
 static long hps, size;
 static void *p, *p2, *p3, *p4;
 
-static void do_mremap(void);
-
-int main(int argc, char **argv)
-{
-	int lc;
-
-	tst_parse_opts(argc, argv, NULL, NULL);
-
-	setup();
-
-	for (lc = 0; TEST_LOOPING(lc); lc++) {
-		tst_count = 0;
-
-		do_mremap();
-	}
-	tst_resm(TPASS, "Still alive.");
-
-	cleanup();
-	tst_exit();
-
-}
-
 static void do_mremap(void)
 {
 	int i;
@@ -89,11 +55,11 @@ static void do_mremap(void)
 
 	for (i = 0; i < 4; i++) {
 		if (posix_memalign(&p, hps, size))
-			tst_brkm(TBROK | TERRNO, cleanup, "memalign p");
+			tst_brk(TBROK | TERRNO, "memalign p");
 		if (posix_memalign(&p2, hps, size))
-			tst_brkm(TBROK | TERRNO, cleanup, "memalign p2");
+			tst_brk(TBROK | TERRNO, "memalign p2");
 		if (posix_memalign(&p3, hps, size))
-			tst_brkm(TBROK | TERRNO, cleanup, "memalign p3");
+			tst_brk(TBROK | TERRNO, "memalign p3");
 
 		memset(p, 0xff, size);
 		memset(p2, 0xff, size);
@@ -108,37 +74,36 @@ static void do_mremap(void)
 		 */
 		old_addr = p + ps * (i >> 1);
 		new_addr = p3 + ps * (i & 1);
-		tst_resm(TINFO, "mremap %p to %p", old_addr, new_addr);
+		tst_res(TINFO, "mremap %p to %p", old_addr, new_addr);
 
 		p4 = mremap(old_addr, size - ps, size - ps,
 			    MREMAP_FIXED | MREMAP_MAYMOVE, new_addr);
 		if (p4 == MAP_FAILED)
-			tst_brkm(TBROK | TERRNO, cleanup, "mremap");
+			tst_brk(TBROK | TERRNO, "mremap");
 		if (memcmp(p4, p2, size - ps))
-			tst_brkm(TBROK, cleanup, "mremap bug");
+			tst_brk(TBROK, "mremap bug");
 	}
+
+	tst_res(TPASS, "Still alive.");
 }
 
-void setup(void)
+static void setup(void)
 {
 	if (access(PATH_THP, F_OK) == -1)
-		tst_brkm(TCONF, NULL, "THP not enabled in kernel?");
-
-	tst_sig(FORK, DEF_HANDLER, cleanup);
-	TEST_PAUSE;
+		tst_brk(TCONF, "THP not enabled in kernel?");
 
 	ps = sysconf(_SC_PAGESIZE);
-	hps = read_meminfo("Hugepagesize:") * 1024;
+	hps = SAFE_READ_MEMINFO("Hugepagesize:") * 1024;
 	size = hps * 4;
 }
 
-void cleanup(void)
-{
-}
+static struct tst_test test = {
+	.tid = "thp02",
+	.needs_root = 1,
+	.setup = setup,
+	.test_all = do_mremap,
+};
 
 #else
-int main(void)
-{
-	tst_brkm(TCONF, NULL, "MREMAP_FIXED not present in <sys/mman.h>");
-}
+	TST_TEST_TCONF("MREMAP_FIXED not present in <sys/mman.h>");
 #endif /* HAVE_MREMAP_FIXED */
diff --git a/testcases/kernel/mem/thp/thp03.c b/testcases/kernel/mem/thp/thp03.c
index 6b0f829..44541d9 100644
--- a/testcases/kernel/mem/thp/thp03.c
+++ b/testcases/kernel/mem/thp/thp03.c
@@ -1,24 +1,15 @@
 /*
- * Copyright (C) 2012  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.
+ * Copyright (C) 2012-2017  Red Hat, Inc.
  *
- * 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.
+ * 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.
  *
- * 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.
+ * 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.
  *
  * thp03 - Case for spliting unaligned memory.
  *       - System will panic if failed.
@@ -46,11 +37,6 @@
 #include <string.h>
 #include <errno.h>
 #include "mem.h"
-#include "safe_macros.h"
-#include "test.h"
-
-char *TCID = "thp03";
-int TST_TOTAL = 1;
 
 #ifdef MADV_MERGEABLE
 
@@ -60,24 +46,6 @@ static long hugepage_size;
 static long unaligned_size;
 static long page_size;
 
-int main(int argc, char **argv)
-{
-	int lc;
-
-	tst_parse_opts(argc, argv, NULL, NULL);
-
-	setup();
-
-	for (lc = 0; TEST_LOOPING(lc); lc++) {
-		tst_count = 0;
-
-		thp_test();
-	}
-	tst_resm(TPASS, "system didn't crash, pass.");
-	cleanup();
-	tst_exit();
-}
-
 static void thp_test(void)
 {
 	void *p;
@@ -85,53 +53,53 @@ static void thp_test(void)
 	p = mmap(NULL, unaligned_size, PROT_READ | PROT_WRITE,
 		 MAP_ANONYMOUS | MAP_PRIVATE, -1, 0);
 	if (p == MAP_FAILED)
-		tst_brkm(TBROK | TERRNO, cleanup, "mmap");
+		tst_brk(TBROK | TERRNO, "mmap");
 
 	memset(p, 0x00, unaligned_size);
 	if (mprotect(p, unaligned_size, PROT_NONE) == -1)
-		tst_brkm(TBROK | TERRNO, cleanup, "mprotect");
+		tst_brk(TBROK | TERRNO, "mprotect");
 
 	if (madvise(p + hugepage_size, page_size, MADV_MERGEABLE) == -1) {
 		if (errno == EINVAL) {
-			tst_brkm(TCONF, cleanup,
+			tst_brk(TCONF,
 			         "MADV_MERGEABLE is not enabled/supported");
 		} else {
-			tst_brkm(TBROK | TERRNO, cleanup, "madvise");
+			tst_brk(TBROK | TERRNO, "madvise");
 		}
 	}
 
-	switch (fork()) {
-	case -1:
-		tst_brkm(TBROK | TERRNO, cleanup, "fork");
+	switch (SAFE_FORK()) {
 	case 0:
 		exit(0);
 	default:
 		if (waitpid(-1, NULL, 0) == -1)
-			tst_brkm(TBROK | TERRNO, cleanup, "waitpid");
+			tst_brk(TBROK | TERRNO, "waitpid");
 	}
+
+	tst_res(TPASS, "system didn't crash, pass.");
+	munmap(p, unaligned_size);
 }
 
-void setup(void)
+static void setup(void)
 {
 	if (access(PATH_THP, F_OK) == -1)
-		tst_brkm(TCONF, NULL, "THP not enabled in kernel?");
+		tst_brk(TCONF, "THP not enabled in kernel?");
 
-	hugepage_size = read_meminfo("Hugepagesize:") * KB;
+	hugepage_size = SAFE_READ_MEMINFO("Hugepagesize:") * KB;
 	unaligned_size = hugepage_size * 4 - 1;
-	page_size = SAFE_SYSCONF(NULL, _SC_PAGESIZE);
-
-	tst_sig(FORK, DEF_HANDLER, cleanup);
-	TEST_PAUSE;
+	page_size = SAFE_SYSCONF(_SC_PAGESIZE);
 }
 
-void cleanup(void)
-{
-}
+static struct tst_test test = {
+	.tid = "thp03",
+	.needs_root = 1,
+	.forks_child = 1,
+	.setup = setup,
+	.test_all = thp_test,
+};
 
 #else
-int main(void)
-{
-	tst_brkm(TCONF, NULL, "Kernel doesn't support MADV_MERGEABLE"
+	TST_TEST_TCONF("Kernel doesn't support MADV_MERGEABLE"
 		 " or you need to update your glibc-headers");
 }
 #endif
-- 
2.9.3


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

* [LTP] [PATCH V2 06/14] mem/hugetlb: convert to new API
  2017-04-10 10:05 [LTP] [PATCH V2 00/14] Convert LTP kernel/mem/testcase to New API Li Wang
                   ` (4 preceding siblings ...)
  2017-04-10 10:05 ` [LTP] [PATCH V2 05/14] mem/thp: " Li Wang
@ 2017-04-10 10:05 ` Li Wang
  2017-04-10 10:05 ` [LTP] [PATCH V2 07/14] mem/hugemmap: " Li Wang
                   ` (7 subsequent siblings)
  13 siblings, 0 replies; 25+ messages in thread
From: Li Wang @ 2017-04-10 10:05 UTC (permalink / raw
  To: ltp

Signed-off-by: Li Wang <liwang@redhat.com>
---
 testcases/kernel/mem/hugetlb/lib/hugetlb.c | 19 ++++++++++---------
 testcases/kernel/mem/hugetlb/lib/hugetlb.h |  9 +++++----
 2 files changed, 15 insertions(+), 13 deletions(-)

diff --git a/testcases/kernel/mem/hugetlb/lib/hugetlb.c b/testcases/kernel/mem/hugetlb/lib/hugetlb.c
index 4a9d489..7afc7d4 100644
--- a/testcases/kernel/mem/hugetlb/lib/hugetlb.c
+++ b/testcases/kernel/mem/hugetlb/lib/hugetlb.c
@@ -32,6 +32,7 @@
  *	rm_shm()
  */
 
+#define TST_NO_DEFAULT_MAIN
 #include <sys/types.h>
 #include <sys/ipc.h>
 #include <sys/shm.h>
@@ -42,14 +43,14 @@
 void check_hugepage(void)
 {
 	if (access(PATH_HUGEPAGES, F_OK))
-		tst_brkm(TCONF, NULL, "Huge page is not supported.");
+		tst_brk(TCONF, "Huge page is not supported.");
 }
 
 /*
  * getipckey() - generates and returns a message key used by the "get"
  *		 calls to create an IPC resource.
  */
-int getipckey(void (*cleanup_fn) (void))
+int getipckey(void)
 {
 	const char a = 'a';
 	int ascii_a = (int)a;
@@ -60,7 +61,7 @@ int getipckey(void (*cleanup_fn) (void))
 
 	curdir = getcwd(curdir, size);
 	if (curdir == NULL)
-		tst_brkm(TBROK | TERRNO, cleanup_fn, "getcwd(curdir)");
+		tst_brk(TBROK | TERRNO, "getcwd(curdir)");
 
 	/*
 	 * Get a Sys V IPC key
@@ -79,7 +80,7 @@ int getipckey(void (*cleanup_fn) (void))
 
 	ipc_key = ftok(curdir, ascii_a + random() % 26);
 	if (ipc_key == -1)
-		tst_brkm(TBROK | TERRNO, cleanup_fn, "ftok");
+		tst_brk(TBROK | TERRNO, __func__);
 
 	return ipc_key;
 }
@@ -87,13 +88,13 @@ int getipckey(void (*cleanup_fn) (void))
 /*
  * getuserid() - return the integer value for the "user" id
  */
-int getuserid(void (*cleanup_fn) (void), char *user)
+int getuserid(char *user)
 {
 	struct passwd *ent;
 
 	ent = getpwnam(user);
 	if (ent == NULL)
-		tst_brkm(TBROK | TERRNO, cleanup_fn, "getpwnam");
+		tst_brk(TBROK | TERRNO, "getpwnam");
 
 	return ent->pw_uid;
 }
@@ -110,8 +111,8 @@ void rm_shm(int shm_id)
 	 * check for # of attaches ?
 	 */
 	if (shmctl(shm_id, IPC_RMID, NULL) == -1) {
-		tst_resm(TINFO, "WARNING: shared memory deletion failed.");
-		tst_resm(TINFO, "This could lead to IPC resource problems.");
-		tst_resm(TINFO, "id = %d", shm_id);
+		tst_res(TINFO, "WARNING: shared memory deletion failed.");
+		tst_res(TINFO, "This could lead to IPC resource problems.");
+		tst_res(TINFO, "id = %d", shm_id);
 	}
 }
diff --git a/testcases/kernel/mem/hugetlb/lib/hugetlb.h b/testcases/kernel/mem/hugetlb/lib/hugetlb.h
index 6b5c61e..1b5cc73 100644
--- a/testcases/kernel/mem/hugetlb/lib/hugetlb.h
+++ b/testcases/kernel/mem/hugetlb/lib/hugetlb.h
@@ -24,11 +24,12 @@
 #ifndef __HUGETLB_H
 #define __HUGETLB_H
 
+#include <stdlib.h>
 #include <sys/ipc.h>
 #include <sys/shm.h>
 #include <errno.h>
 #include <sys/wait.h>
-#include "test.h"
+#include "tst_test.h"
 
 #define SHM_RD	0400
 #define SHM_WR	0200
@@ -49,12 +50,12 @@ key_t shmkey;			/* an IPC key generated by ftok() */
 
 void check_hugepage(void);
 
-int getipckey(void (*cleanup_fn) (void));
-int getuserid(void (*cleanup_fn) (void), char *user);
+int getipckey(void);
+int getuserid(char *user);
 void rm_shm(int shm_id);
 
 char *nr_opt;
-int sflag;
+char *Hopt;
 long orig_hugepages;
 
 #endif /* hugetlb.h */
-- 
2.9.3


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

* [LTP] [PATCH V2 07/14] mem/hugemmap: convert to new API
  2017-04-10 10:05 [LTP] [PATCH V2 00/14] Convert LTP kernel/mem/testcase to New API Li Wang
                   ` (5 preceding siblings ...)
  2017-04-10 10:05 ` [LTP] [PATCH V2 06/14] mem/hugetlb: " Li Wang
@ 2017-04-10 10:05 ` Li Wang
  2017-04-10 10:05 ` [LTP] [PATCH V2 08/14] mem/hugeshmat: " Li Wang
                   ` (6 subsequent siblings)
  13 siblings, 0 replies; 25+ messages in thread
From: Li Wang @ 2017-04-10 10:05 UTC (permalink / raw
  To: ltp

Signed-off-by: Li Wang <liwang@redhat.com>
---
 testcases/kernel/mem/hugetlb/hugemmap/hugemmap01.c | 182 +++++--------
 testcases/kernel/mem/hugetlb/hugemmap/hugemmap02.c | 244 +++++++----------
 testcases/kernel/mem/hugetlb/hugemmap/hugemmap04.c | 192 +++++---------
 testcases/kernel/mem/hugetlb/hugemmap/hugemmap05.c | 291 ++++++++-------------
 testcases/kernel/mem/hugetlb/hugemmap/hugemmap06.c |  95 +++----
 testcases/kernel/mem/hugetlb/lib/hugetlb.h         |   2 +
 6 files changed, 369 insertions(+), 637 deletions(-)

diff --git a/testcases/kernel/mem/hugetlb/hugemmap/hugemmap01.c b/testcases/kernel/mem/hugetlb/hugemmap/hugemmap01.c
index da8bdb1..4e629f0 100644
--- a/testcases/kernel/mem/hugetlb/hugemmap/hugemmap01.c
+++ b/testcases/kernel/mem/hugetlb/hugemmap/hugemmap01.c
@@ -1,20 +1,16 @@
 /*
+ * Copyright (c) International Business Machines  Corp., 2004
+ * Copyright (c) Linux Test Project, 2004-2017
  *
- *   Copyright (c) International Business Machines  Corp., 2004
+ * 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 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
+ * 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.
  */
 
 /*
@@ -27,143 +23,86 @@
  *  mmap() should succeed returning the address of the hugetlb mapped region.
  *  The number of free huge pages should decrease.
  *
- * Algorithm:
- *  Setup:
- *   Setup signal handling.
- *   Pause for SIGUSR1 if option specified.
- *   Create temporary directory.
- *
  * Test:
  *  Loop if the proper options are given.
  *  Execute system call
  *  Check return code, if system call failed (return=-1)
  *  Log the errno and Issue a FAIL message.
- * Cleanup:
- *  Print timing stats if options given
- *  Delete the temporary directory created.
  *
  * HISTORY
  *  04/2004 Written by Robbie Williamson
  */
 
-#include <sys/types.h>
-#include <sys/mman.h>
-#include <sys/mount.h>
-#include <sys/stat.h>
-#include <errno.h>
-#include <fcntl.h>
-#include <signal.h>
-#include <stdint.h>
 #include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
-#include <unistd.h>
-
-#include "test.h"
-#include "hugetlb.h"
-#include "safe_macros.h"
+#include <sys/mount.h>
 #include "mem.h"
+#include "hugetlb.h"
 
-static char TEMPFILE[MAXPATHLEN];
+static struct tst_option options[] = {
+	{"H:", &Hopt,   "-H   /..  Location of hugetlbfs, i.e.  -H /var/hugetlbfs"},
+	{"s:", &nr_opt, "-s   num  Set the number of the been allocated hugepages"},
+	{NULL, NULL, NULL}
+};
 
-char *TCID = "hugemmap01";
-int TST_TOTAL = 1;
 static long *addr;
-static int fildes;
-static char *Hopt;
+static int  fildes;
 static long beforetest;
 static long aftertest;
 static long hugepagesmapped;
 static long hugepages = 128;
+static char TEMPFILE[MAXPATHLEN];
 
-static void help(void);
-
-int main(int ac, char **av)
+static void test_hugemmap(void)
 {
-	int lc;
-	int Hflag = 0;
 	long page_sz = 0;
-	int sflag = 0;
 
-	option_t options[] = {
-		{"H:", &Hflag, &Hopt},
-		{"s:", &sflag, &nr_opt},
-		{NULL, NULL, NULL}
-	};
+	/* Creat a temporary file used for mapping */
+	fildes = SAFE_OPEN(TEMPFILE, O_RDWR | O_CREAT, 0666);
 
-	tst_parse_opts(ac, av, options, &help);
+	/* Note the number of free huge pages BEFORE testing */
+	beforetest = SAFE_READ_MEMINFO("HugePages_Free:");
 
-	if (!Hflag) {
-		tst_tmpdir();
-		Hopt = tst_get_tmpdir();
-	}
-	if (sflag)
-		hugepages = SAFE_STRTOL(NULL, nr_opt, 0, LONG_MAX);
-
-	setup();
-
-	for (lc = 0; TEST_LOOPING(lc); lc++) {
-		/* Creat a temporary file used for mapping */
-		fildes = open(TEMPFILE, O_RDWR | O_CREAT, 0666);
-		if (fildes < 0)
-			tst_brkm(TFAIL | TERRNO, cleanup, "open %s failed",
-				 TEMPFILE);
-
-		tst_count = 0;
-
-		/* Note the number of free huge pages BEFORE testing */
-		beforetest = read_meminfo("HugePages_Free:");
-
-		/* Note the size of huge page size BEFORE testing */
-		page_sz = read_meminfo("Hugepagesize:") * 1024;
-
-		addr = mmap(NULL, page_sz, PROT_READ | PROT_WRITE,
-			    MAP_SHARED, fildes, 0);
-		if (addr == MAP_FAILED) {
-			tst_resm(TFAIL | TERRNO, "mmap() Failed on %s",
-				 TEMPFILE);
-			close(fildes);
-			continue;
-		} else {
-			close(fildes);
-			tst_resm(TPASS, "call succeeded");
-			/* force to allocate page and change HugePages_Free */
-			*(int *)addr = 0;
-		}
-
-		/*
-		 * Make sure the number of free huge pages
-		 * AFTER testing decreased
-		 */
-		aftertest = read_meminfo("HugePages_Free:");
-		hugepagesmapped = beforetest - aftertest;
-		if (hugepagesmapped < 1)
-			tst_resm(TWARN, "Number of HUGEPAGES_FREE stayed the"
-				 " same. Okay if multiple copies running due"
-				 " to test collision.");
+	/* Note the size of huge page size BEFORE testing */
+	page_sz = SAFE_READ_MEMINFO("Hugepagesize:") * 1024;
+
+	addr = mmap(NULL, page_sz, PROT_READ | PROT_WRITE,
+			MAP_SHARED, fildes, 0);
 
-		/* Clean up things in case we are looping */
-		/* Unmap the mapped memory */
-		if (munmap(addr, page_sz) != 0)
-			tst_brkm(TFAIL | TERRNO, NULL, "munmap failed");
+	if (addr == MAP_FAILED) {
+		tst_res(TFAIL | TERRNO, "mmap() Failed on %s",
+				TEMPFILE);
+	} else {
+		tst_res(TPASS, "call succeeded");
 
-		close(fildes);
+		/* force to allocate page and change HugePages_Free */
+		*(int *)addr = 0;
+		/* Make sure the number of free huge pages AFTER testing decreased */
+		aftertest = SAFE_READ_MEMINFO("HugePages_Free:");
+		hugepagesmapped = beforetest - aftertest;
+		if (hugepagesmapped < 1)
+			tst_res(TWARN, "Number of HUGEPAGES_FREE stayed the"
+					" same. Okay if multiple copies running due"
+					" to test collision.");
+		munmap(addr, page_sz);
 	}
 
-	cleanup();
-	tst_exit();
+	close(fildes);
 }
 
 void setup(void)
 {
-	TEST_PAUSE;
-	tst_require_root();
 	check_hugepage();
+	orig_hugepages = get_sys_tune("nr_hugepages");
+
+	if (!Hopt)
+		Hopt = tst_get_tmpdir();
 	if (mount("none", Hopt, "hugetlbfs", 0, NULL) < 0)
-		tst_brkm(TBROK | TERRNO, NULL, "mount failed on %s", Hopt);
+		tst_brk(TBROK | TERRNO, "mount failed on %s", Hopt);
 
-	orig_hugepages = get_sys_tune("nr_hugepages");
+	if (nr_opt)
+		hugepages = SAFE_STRTOL(nr_opt, 0, LONG_MAX);
 	set_sys_tune("nr_hugepages", hugepages, 1);
+
 	snprintf(TEMPFILE, sizeof(TEMPFILE), "%s/mmapfile%d", Hopt, getpid());
 }
 
@@ -173,11 +112,14 @@ void cleanup(void)
 	set_sys_tune("nr_hugepages", orig_hugepages, 0);
 
 	umount(Hopt);
-	tst_rmdir();
 }
 
-static void help(void)
-{
-	printf("    -H /..  Location of hugetlbfs, i.e. -H /var/hugetlbfs\n");
-	printf("    -s num  Set the number of the been allocated hugepages\n");
-}
+static struct tst_test test = {
+	.tid = "hugemmap01",
+	.needs_root = 1,
+	.needs_tmpdir = 1,
+	.options = options,
+	.setup = setup,
+	.cleanup = cleanup,
+	.test_all = test_hugemmap,
+};
diff --git a/testcases/kernel/mem/hugetlb/hugemmap/hugemmap02.c b/testcases/kernel/mem/hugetlb/hugemmap/hugemmap02.c
index d31609d..e3d6c4f 100644
--- a/testcases/kernel/mem/hugetlb/hugemmap/hugemmap02.c
+++ b/testcases/kernel/mem/hugetlb/hugemmap/hugemmap02.c
@@ -1,20 +1,16 @@
 /*
+ * Copyright (c) International Business Machines  Corp., 2004
+ * Copyright (c) Linux Test Project, 2004-2017
  *
- *   Copyright (c) International Business Machines  Corp., 2004
+ * 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 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
+ * 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.
  */
 
 /*
@@ -39,181 +35,137 @@
  *  04/2004 Written by Robbie Williamson
  */
 
-#include <sys/types.h>
-#include <sys/mman.h>
-#include <sys/mount.h>
-#include <sys/stat.h>
-#include <errno.h>
-#include <fcntl.h>
-#include <signal.h>
-#include <stdint.h>
 #include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
-#include <unistd.h>
-
-#include "test.h"
-#include "safe_macros.h"
+#include <sys/mount.h>
 #include "mem.h"
 #include "hugetlb.h"
 
 #define LOW_ADDR       0x80000000
 #define LOW_ADDR2      0x90000000
 
+static struct tst_option options[] = {
+	{"H:", &Hopt,   "-H   /..  Location of hugetlbfs, i.e.  -H /var/hugetlbfs"},
+	{"s:", &nr_opt, "-s   num  Set the number of the been allocated hugepages"},
+	{NULL, NULL, NULL}
+};
+
 static char TEMPFILE[MAXPATHLEN];
 
-char *TCID = "hugemmap02";
-int TST_TOTAL = 1;
 static unsigned long *addr;
 static unsigned long *addr2;
 static unsigned long low_addr = LOW_ADDR;
 static unsigned long low_addr2 = LOW_ADDR2;
 static unsigned long *addrlist[5];
-static int i;
 static int fildes;
 static int nfildes;
-static char *Hopt;
 static long hugepages = 128;
 
-static void help(void);
-
-int main(int ac, char **av)
+static void test_hugemmap(void)
 {
-	int lc;
-	int Hflag = 0;
+	int i;
 	long page_sz, map_sz;
-	int sflag = 0;
 
-	option_t options[] = {
-		{"H:", &Hflag, &Hopt},
-		{"s:", &sflag, &nr_opt},
-		{NULL, NULL, NULL}
-	};
+	page_sz = getpagesize();
+	map_sz = SAFE_READ_MEMINFO("Hugepagesize:") * 1024 * 2;
 
-	tst_parse_opts(ac, av, options, &help);
+	/* Creat a temporary file used for huge mapping */
+	fildes = SAFE_OPEN(TEMPFILE, O_RDWR | O_CREAT, 0666);
 
-	check_hugepage();
+	/* Creat a file used for normal mapping */
+	nfildes = SAFE_OPEN("/dev/zero", O_RDONLY, 0666);
 
-	if (!Hflag) {
-		tst_tmpdir();
-		Hopt = tst_get_tmpdir();
+	/* Call mmap on /dev/zero 5 times */
+	for (i = 0; i < 5; i++) {
+		addr = mmap(0, 256 * 1024 * 1024, PROT_READ,
+				MAP_SHARED, nfildes, 0);
+		addrlist[i] = addr;
 	}
-	if (sflag)
-		hugepages = SAFE_STRTOL(NULL, nr_opt, 0, LONG_MAX);
 
-	page_sz = getpagesize();
-	map_sz = read_meminfo("Hugepagesize:") * 1024 * 2;
-
-	setup();
-
-	for (lc = 0; TEST_LOOPING(lc); lc++) {
-		/* Creat a temporary file used for huge mapping */
-		fildes = open(TEMPFILE, O_RDWR | O_CREAT, 0666);
-		if (fildes < 0)
-			tst_brkm(TBROK | TERRNO, cleanup,
-				 "opening %s failed", TEMPFILE);
-
-		/* Creat a file used for normal mapping */
-		nfildes = open("/dev/zero", O_RDONLY, 0666);
-		if (nfildes < 0)
-			tst_brkm(TBROK | TERRNO, cleanup,
-				 "opening /dev/zero failed");
-
-		tst_count = 0;
-
-		/*
-		 * Call mmap on /dev/zero 5 times
-		 */
-		for (i = 0; i < 5; i++) {
-			addr = mmap(0, 256 * 1024 * 1024, PROT_READ,
-				    MAP_SHARED, nfildes, 0);
-			addrlist[i] = addr;
-		}
-
-		while (range_is_mapped(cleanup, low_addr, low_addr + map_sz) == 1) {
-			low_addr = low_addr + 0x10000000;
-
-			if (low_addr < LOW_ADDR)
-				tst_brkm(TBROK | TERRNO, cleanup,
-						"no empty region to use");
-		}
-		/* mmap using normal pages and a low memory address */
-		addr = mmap((void *)low_addr, page_sz, PROT_READ,
-			    MAP_SHARED | MAP_FIXED, nfildes, 0);
-		if (addr == MAP_FAILED)
-			tst_brkm(TBROK | TERRNO, cleanup,
-				 "mmap failed on nfildes");
-
-		while (range_is_mapped(cleanup, low_addr2, low_addr2 + map_sz) == 1) {
-			low_addr2 = low_addr2 + 0x10000000;
-
-			if (low_addr2 < LOW_ADDR2)
-				tst_brkm(TBROK | TERRNO, cleanup,
-						"no empty region to use");
-		}
-		/* Attempt to mmap a huge page into a low memory address */
-		addr2 = mmap((void *)low_addr2, map_sz, PROT_READ | PROT_WRITE,
-			     MAP_SHARED, fildes, 0);
-#if __WORDSIZE == 64		/* 64-bit process */
-		if (addr2 == MAP_FAILED) {
-			tst_resm(TFAIL | TERRNO, "huge mmap failed unexpectedly"
-				 " with %s (64-bit)", TEMPFILE);
-		} else {
-			tst_resm(TPASS, "huge mmap succeeded (64-bit)");
-		}
-#else /* 32-bit process */
-		if (addr2 == MAP_FAILED)
-			tst_resm(TFAIL | TERRNO, "huge mmap failed unexpectedly"
-				 " with %s (32-bit)", TEMPFILE);
-		else if (addr2 > 0) {
-			tst_resm(TCONF,
-				 "huge mmap failed to test the scenario");
-		} else if (addr == 0)
-			tst_resm(TPASS, "huge mmap succeeded (32-bit)");
-#endif
+	while (range_is_mapped(low_addr, low_addr + map_sz) == 1) {
+		low_addr = low_addr + 0x10000000;
 
-		/* Clean up things in case we are looping */
-		for (i = 0; i < 5; i++) {
-			if (munmap(addrlist[i], 256 * 1024 * 1024) == -1)
-				tst_resm(TBROK | TERRNO,
-					 "munmap of addrlist[%d] failed", i);
-		}
+		if (low_addr < LOW_ADDR)
+			tst_brk(TBROK | TERRNO, "no empty region to use");
+	}
+	/* mmap using normal pages and a low memory address */
+	addr = mmap((void *)low_addr, page_sz, PROT_READ,
+			MAP_SHARED | MAP_FIXED, nfildes, 0);
+	if (addr == MAP_FAILED)
+		tst_brk(TBROK | TERRNO, "mmap failed on nfildes");
 
-		if (munmap(addr2, map_sz) == -1)
-			tst_brkm(TFAIL | TERRNO, NULL, "huge munmap failed");
-		if (munmap(addr, page_sz) == -1)
-			tst_brkm(TFAIL | TERRNO, NULL, "munmap failed");
+	while (range_is_mapped(low_addr2, low_addr2 + map_sz) == 1) {
+		low_addr2 = low_addr2 + 0x10000000;
 
-		close(nfildes);
-		close(fildes);
+		if (low_addr2 < LOW_ADDR2)
+			tst_brk(TBROK | TERRNO, "no empty region to use");
+	}
+	/* Attempt to mmap a huge page into a low memory address */
+	addr2 = mmap((void *)low_addr2, map_sz, PROT_READ | PROT_WRITE,
+			MAP_SHARED, fildes, 0);
+#if __WORDSIZE == 64 /* 64-bit process */
+	if (addr2 == MAP_FAILED) {
+		tst_res(TFAIL | TERRNO, "huge mmap failed unexpectedly"
+				" with %s (64-bit)", TEMPFILE);
+	} else {
+		tst_res(TPASS, "huge mmap succeeded (64-bit)");
+	}
+#else /* 32-bit process */
+	if (addr2 == MAP_FAILED)
+		tst_res(TFAIL | TERRNO, "huge mmap failed unexpectedly"
+				" with %s (32-bit)", TEMPFILE);
+	else if (addr2 > 0) {
+		tst_res(TCONF,
+				"huge mmap failed to test the scenario");
+	} else if (addr == 0)
+		tst_res(TPASS, "huge mmap succeeded (32-bit)");
+#endif
+
+	for (i = 0; i < 5; i++) {
+		if (munmap(addrlist[i], 256 * 1024 * 1024) == -1)
+			tst_res(TFAIL | TERRNO,
+					"munmap of addrlist[%d] failed", i);
 	}
 
-	cleanup();
-	tst_exit();
+	if (munmap(addr2, map_sz) == -1)
+		tst_res(TFAIL | TERRNO, "huge munmap failed");
+	if (munmap(addr, page_sz) == -1)
+		tst_res(TFAIL | TERRNO, "munmap failed");
+
+	close(nfildes);
+	close(fildes);
 }
 
-void setup(void)
+static void setup(void)
 {
-	TEST_PAUSE;
-	tst_require_root();
-	if (mount("none", Hopt, "hugetlbfs", 0, NULL) < 0)
-		tst_brkm(TBROK | TERRNO, NULL, "mount failed on %s", Hopt);
+	check_hugepage();
 	orig_hugepages = get_sys_tune("nr_hugepages");
+
+	if (!Hopt)
+		Hopt = tst_get_tmpdir();
+	if (mount("none", Hopt, "hugetlbfs", 0, NULL) < 0)
+		tst_brk(TBROK | TERRNO, "mount failed on %s", Hopt);
+
+	if (nr_opt)
+		hugepages = SAFE_STRTOL(nr_opt, 0, LONG_MAX);
 	set_sys_tune("nr_hugepages", hugepages, 1);
+
 	snprintf(TEMPFILE, sizeof(TEMPFILE), "%s/mmapfile%d", Hopt, getpid());
 }
 
-void cleanup(void)
+static void cleanup(void)
 {
 	unlink(TEMPFILE);
 	set_sys_tune("nr_hugepages", orig_hugepages, 0);
 
 	umount(Hopt);
-	tst_rmdir();
 }
 
-static void help(void)
-{
-	printf("  -H /..  Location of hugetlbfs, i.e. -H /var/hugetlbfs\n");
-	printf("  -s num  Set the number of the been allocated hugepages\n");
-}
+static struct tst_test test = {
+	.tid = "hugemmap02",
+	.needs_root = 1,
+	.needs_tmpdir = 1,
+	.options = options,
+	.setup = setup,
+	.cleanup = cleanup,
+	.test_all = test_hugemmap,
+};
diff --git a/testcases/kernel/mem/hugetlb/hugemmap/hugemmap04.c b/testcases/kernel/mem/hugetlb/hugemmap/hugemmap04.c
index e1f2986..c3c7324 100644
--- a/testcases/kernel/mem/hugetlb/hugemmap/hugemmap04.c
+++ b/testcases/kernel/mem/hugetlb/hugemmap/hugemmap04.c
@@ -1,20 +1,16 @@
 /*
+ * Copyright (c) International Business Machines  Corp., 2004
+ * Copyright (c) Linux Test Project, 2004-2017
  *
- *   Copyright (c) International Business Machines  Corp., 2004
+ * 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 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
+ * 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.
  */
 
 /*
@@ -28,47 +24,29 @@
  *  mmap() should succeed returning the address of the hugetlb mapped region.
  *  The number of free huge pages should decrease.
  *
- * Algorithm:
- *  Setup:
- *   Setup signal handling.
- *   Pause for SIGUSR1 if option specified.
- *   Create temporary directory.
- *
  * Test:
  *  Loop if the proper options are given.
  *  Execute system call
  *  Check return code, if system call failed (return=-1)
  *  Log the errno and Issue a FAIL message.
- * Cleanup:
- *  Print timing stats if options given
- *  Delete the temporary directory created.
  *
  * HISTORY
  *  04/2004 Written by Robbie Williamson
  */
 
-#include <sys/types.h>
-#include <sys/mman.h>
 #include <sys/mount.h>
-#include <sys/stat.h>
-#include <errno.h>
-#include <fcntl.h>
-#include <signal.h>
-#include <stdint.h>
 #include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
-#include <unistd.h>
-
-#include "test.h"
-#include "safe_macros.h"
 #include "mem.h"
 #include "hugetlb.h"
 
+static struct tst_option options[] = {
+	{"H:", &Hopt,   "-H   /..  Location of hugetlbfs, i.e.  -H /var/hugetlbfs"},
+	{"s:", &nr_opt, "-s   num  Set the number of the been allocated hugepages"},
+	{NULL, NULL, NULL}
+};
+
 static char TEMPFILE[MAXPATHLEN];
 
-char *TCID = "hugemmap04";
-int TST_TOTAL = 1;
 static long *addr;
 static long long mapsize;
 static int fildes;
@@ -77,108 +55,68 @@ static long beforetest;
 static long aftertest;
 static long hugepagesmapped;
 static long hugepages = 128;
-static char *Hopt;
-
-static void help(void);
 
-int main(int ac, char **av)
+static void test_hugemmap(void)
 {
-	int lc;
-	int Hflag = 0;
-	int sflag = 0;
 	int huge_pagesize = 0;
 
-	option_t options[] = {
-		{"H:", &Hflag, &Hopt},
-		{"s:", &sflag, &nr_opt},
-		{NULL, NULL, NULL}
-	};
-
-	tst_parse_opts(ac, av, options, &help);
-
-	if (!Hflag) {
-		tst_tmpdir();
-		Hopt = tst_get_tmpdir();
-	}
-	if (sflag)
-		hugepages = SAFE_STRTOL(NULL, nr_opt, 0, LONG_MAX);
-
-	setup();
-
-	for (lc = 0; TEST_LOOPING(lc); lc++) {
-		/* Creat a temporary file used for huge mapping */
-		fildes = open(TEMPFILE, O_RDWR | O_CREAT, 0666);
-		if (fildes < 0)
-			tst_brkm(TFAIL | TERRNO, cleanup, "open %s failed",
-				 TEMPFILE);
+	/* Creat a temporary file used for huge mapping */
+	fildes = SAFE_OPEN(TEMPFILE, O_RDWR | O_CREAT, 0666);
 
-		tst_count = 0;
+	freepages = SAFE_READ_MEMINFO("HugePages_Free:");
+	beforetest = freepages;
 
-		/* Note the number of free huge pages BEFORE testing */
-		freepages = read_meminfo("HugePages_Free:");
-		beforetest = freepages;
-
-		/* Note the size of huge page size BEFORE testing */
-		huge_pagesize = read_meminfo("Hugepagesize:");
-		tst_resm(TINFO, "Size of huge pages is %d KB", huge_pagesize);
+	huge_pagesize = SAFE_READ_MEMINFO("Hugepagesize:");
+	tst_res(TINFO, "Size of huge pages is %d KB", huge_pagesize);
 
 #if __WORDSIZE == 32
-		tst_resm(TINFO, "Total amount of free huge pages is %d",
-			 freepages);
-		tst_resm(TINFO, "Max number allowed for 1 mmap file in"
-			 " 32-bits is 128");
-		if (freepages > 128)
-			freepages = 128;
+	tst_res(TINFO, "Total amount of free huge pages is %d",
+			freepages);
+	tst_res(TINFO, "Max number allowed for 1 mmap file in"
+			" 32-bits is 128");
+	if (freepages > 128)
+		freepages = 128;
 #endif
-		mapsize = (long long)freepages *huge_pagesize * 1024;
-		addr = mmap(NULL, mapsize, PROT_READ | PROT_WRITE,
-			    MAP_SHARED, fildes, 0);
-		sleep(2);
-		if (addr == MAP_FAILED) {
-			tst_resm(TFAIL | TERRNO, "mmap() Failed on %s",
-				 TEMPFILE);
-			close(fildes);
-			continue;
-		} else {
-			tst_resm(TPASS,
-				 "Succeeded mapping file using %ld pages",
-				 freepages);
-			/* force to allocate page and change HugePages_Free */
-			*(int *)addr = 0;
-		}
-
-		/*
-		 * Make sure the number of free huge pages
-		 * AFTER testing decreased
-		 */
-		aftertest = read_meminfo("HugePages_Free:");
+	mapsize = (long long)freepages * huge_pagesize * 1024;
+	addr = mmap(NULL, mapsize, PROT_READ | PROT_WRITE,
+			MAP_SHARED, fildes, 0);
+	if (addr == MAP_FAILED) {
+		tst_res(TFAIL | TERRNO, "mmap() Failed on %s",
+				TEMPFILE);
+	} else {
+		tst_res(TPASS,
+				"Succeeded mapping file using %ld pages",
+				freepages);
+
+		/* force to allocate page and change HugePages_Free */
+		*(int *)addr = 0;
+		/* Make sure the number of free huge pages AFTER testing decreased */
+		aftertest = SAFE_READ_MEMINFO("HugePages_Free:");
 		hugepagesmapped = beforetest - aftertest;
 		if (hugepagesmapped < 1)
-			tst_resm(TWARN, "Number of HUGEPAGES_FREE stayed the"
-				 " same. Okay if multiple copies running due"
-				 " to test collision.");
-
-		/* Clean up things in case we are looping */
-		/* Unmap the mapped memory */
-		if (munmap(addr, mapsize) != 0)
-			tst_brkm(TFAIL | TERRNO, NULL, "munmap failed");
-
-		close(fildes);
+			tst_res(TWARN, "Number of HUGEPAGES_FREE stayed the"
+					" same. Okay if multiple copies running due"
+					" to test collision.");
 	}
 
-	cleanup();
-	tst_exit();
+	munmap(addr, mapsize);
+	close(fildes);
 }
 
 void setup(void)
 {
-	TEST_PAUSE;
-	tst_require_root();
 	check_hugepage();
-	if (mount("none", Hopt, "hugetlbfs", 0, NULL) < 0)
-		tst_brkm(TBROK | TERRNO, NULL, "mount failed on %s", Hopt);
 	orig_hugepages = get_sys_tune("nr_hugepages");
+
+	if (!Hopt)
+		Hopt = tst_get_tmpdir();
+	if (mount("none", Hopt, "hugetlbfs", 0, NULL) < 0)
+		tst_brk(TBROK | TERRNO, "mount failed on %s", Hopt);
+
+	if (nr_opt)
+		hugepages = SAFE_STRTOL(nr_opt, 0, LONG_MAX);
 	set_sys_tune("nr_hugepages", hugepages, 1);
+
 	snprintf(TEMPFILE, sizeof(TEMPFILE), "%s/mmapfile%d", Hopt, getpid());
 }
 
@@ -188,10 +126,14 @@ void cleanup(void)
 	set_sys_tune("nr_hugepages", orig_hugepages, 0);
 
 	umount(Hopt);
-	tst_rmdir();
 }
 
-static void help(void)
-{
-	printf("  -H /..  Location of hugetlbfs, i.e. -H /var/hugetlbfs\n");
-}
+static struct tst_test test = {
+	.tid = "hugemmap04",
+	.needs_root = 1,
+	.needs_tmpdir = 1,
+	.options = options,
+	.setup = setup,
+	.cleanup = cleanup,
+	.test_all = test_hugemmap,
+};
diff --git a/testcases/kernel/mem/hugetlb/hugemmap/hugemmap05.c b/testcases/kernel/mem/hugetlb/hugemmap/hugemmap05.c
index b127566..128c22e 100644
--- a/testcases/kernel/mem/hugetlb/hugemmap/hugemmap05.c
+++ b/testcases/kernel/mem/hugetlb/hugemmap/hugemmap05.c
@@ -1,6 +1,19 @@
 /*
+ * Copyright (C) 2010-2017  Red Hat, Inc.
+ *
+ * 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.
  * overcommit hugetlbfs and check the statistics.
  *
+ * Description:
+ *
  * hugetlbfs allows to overcommit hugepages and there are tunables in
  * sysfs and procfs. The test here want to ensure it is possible to
  * overcommit by either mmap or shared memory. Also ensure those
@@ -11,44 +24,12 @@
  * Finally, it reads and writes every page. There are command options to
  * choose either to manage hugepages from sysfs or procfs, and reserve
  * them by mmap or shmget.
- *
- * 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.
  */
-#include <sys/mman.h>
-#include <sys/stat.h>
-#include <sys/types.h>
+
 #include <sys/mount.h>
-#include <sys/shm.h>
-#include <sys/ipc.h>
-#include <unistd.h>
-#include <errno.h>
-#include <stdio.h>
-#include <stdlib.h>
-#include <unistd.h>
-#include <fcntl.h>
-#include <string.h>
-#include <ctype.h>
-#include "test.h"
+#include "mem.h"
 #include "hugetlb.h"
+#include "tst_safe_stdio.h"
 
 #define PROTECTION		(PROT_READ | PROT_WRITE)
 #define PATH_MEMINFO		"/proc/meminfo"
@@ -80,66 +61,32 @@ char path_sys_sz_huge[BUFSIZ];
 #define SHM_HUGETLB 04000
 #endif
 
-char *TCID = "hugemmap05";
-int TST_TOTAL = 1, tst_count;
 static char nr_hugepages[BUFSIZ], nr_overcommit_hugepages[BUFSIZ];
 static char buf[BUFSIZ], line[BUFSIZ], path[BUFSIZ], pathover[BUFSIZ];
 static char shmmax[BUFSIZ];
-static char *opt_allocstr;
 static int hugepagesize;	/* in Bytes */
-static int opt_sysfs, opt_alloc;
 static int shmid = -1;
 static int restore_shmmax;
 static size_t size = 128, length = 384;
-static option_t options[] = {
-	{"s", &opt_sysfs, NULL},
-	{"m", &shmid, NULL},
-	{"a:", &opt_alloc, &opt_allocstr},
+
+char *opt_sysfs;
+char *opt_alloc;
+char *opt_shmid;
+static struct tst_option options[] = {
+	{"s",  &opt_sysfs, "-s        Setup hugepages from sysfs"},
+	{"m",  &opt_shmid, "-m        Reserve hugepages by shmget"},
+	{"a:", &opt_alloc, "-a        Number of overcommint hugepages"},
 	{NULL, NULL, NULL}
 };
 
-static void setup(void);
-static void cleanup(void);
-static void overcommit(void);
 static void write_bytes(void *addr);
 static void read_bytes(void *addr);
 static int lookup(char *line, char *pattern);
-static void usage(void);
 static int checkproc(FILE * fp, char *string, int value);
 static int checksys(char *path, char *pattern, int value);
-static void init_hugepagesize(void);
 static void init_sys_sz_paths(void);
 
-int main(int argc, char *argv[])
-{
-	int lc;
-
-	init_hugepagesize();
-	init_sys_sz_paths();
-
-	tst_parse_opts(argc, argv, options, usage);
-	if (opt_sysfs) {
-		strncpy(path, path_sys_sz_huge, strlen(path_sys_sz_huge) + 1);
-		strncpy(pathover, path_sys_sz_over,
-			strlen(path_sys_sz_over) + 1);
-	} else {
-		strncpy(path, PATH_PROC_HUGE, strlen(PATH_PROC_HUGE) + 1);
-		strncpy(pathover, PATH_PROC_OVER, strlen(PATH_PROC_OVER) + 1);
-	}
-	if (opt_alloc) {
-		size = atoi(opt_allocstr);
-		length = (int)(size + size * 0.5) * 2;
-	}
-	setup();
-	for (lc = 0; TEST_LOOPING(lc); lc++) {
-		tst_count = 0;
-		overcommit();
-	}
-	cleanup();
-	tst_exit();
-}
-
-static void overcommit(void)
+static void test_overcommit(void)
 {
 	void *addr = NULL, *shmaddr = NULL;
 	int fd = -1, key = -1;
@@ -150,27 +97,25 @@ static void overcommit(void)
 		/* Use /proc/meminfo to generate an IPC key. */
 		key = ftok(PATH_MEMINFO, strlen(PATH_MEMINFO));
 		if (key == -1)
-			tst_brkm(TBROK | TERRNO, cleanup, "ftok");
+			tst_brk(TBROK | TERRNO, "ftok");
 		shmid = shmget(key, (long)(length / 2 * hugepagesize),
 			       SHM_HUGETLB | IPC_CREAT | SHM_R | SHM_W);
 		if (shmid == -1)
-			tst_brkm(TBROK | TERRNO, cleanup, "shmget");
+			tst_brk(TBROK | TERRNO, "shmget");
 	} else {
 		/* XXX (garrcoop): memory leak. */
 		snprintf(s, BUFSIZ, "%s/hugemmap05/file", tst_get_tmpdir());
-		fd = open(s, O_CREAT | O_RDWR, 0755);
-		if (fd == -1)
-			tst_brkm(TBROK | TERRNO, cleanup, "open");
+		fd = SAFE_OPEN(s, O_CREAT | O_RDWR, 0755);
 		addr = mmap(ADDR, (long)(length / 2 * hugepagesize), PROTECTION,
 			    FLAGS, fd, 0);
 		if (addr == MAP_FAILED) {
 			close(fd);
-			tst_brkm(TBROK | TERRNO, cleanup, "mmap");
+			tst_brk(TBROK | TERRNO, "mmap");
 		}
 	}
 
 	if (opt_sysfs) {
-		tst_resm(TINFO, "check sysfs before allocation.");
+		tst_res(TINFO, "check sysfs before allocation.");
 		if (checksys(path_sys_sz_huge, "HugePages_Total",
 			     length / 2) != 0)
 			return;
@@ -184,10 +129,8 @@ static void overcommit(void)
 			     length / 2) != 0)
 			return;
 	} else {
-		tst_resm(TINFO, "check /proc/meminfo before allocation.");
-		fp = fopen(PATH_MEMINFO, "r");
-		if (fp == NULL)
-			tst_brkm(TBROK | TERRNO, cleanup, "fopen");
+		tst_res(TINFO, "check /proc/meminfo before allocation.");
+		fp = SAFE_FOPEN(PATH_MEMINFO, "r");
 		if (checkproc(fp, "HugePages_Total", length / 2) != 0)
 			return;
 		if (checkproc(fp, "HugePages_Free", length / 2) != 0)
@@ -199,10 +142,10 @@ static void overcommit(void)
 		fclose(fp);
 	}
 	if (shmid != -1) {
-		tst_resm(TINFO, "shmid: 0x%x", shmid);
+		tst_res(TINFO, "shmid: 0x%x", shmid);
 		shmaddr = shmat(shmid, ADDR, SHMAT_FLAGS);
 		if (shmaddr == (void *)-1)
-			tst_brkm(TBROK | TERRNO, cleanup, "shmat");
+			tst_brk(TBROK | TERRNO, "shmat");
 		write_bytes(shmaddr);
 		read_bytes(shmaddr);
 	} else {
@@ -210,7 +153,7 @@ static void overcommit(void)
 		read_bytes(addr);
 	}
 	if (opt_sysfs) {
-		tst_resm(TINFO, "check sysfs.");
+		tst_res(TINFO, "check sysfs.");
 		if (checksys(path_sys_sz_huge, "HugePages_Total",
 			     length / 2) != 0)
 			return;
@@ -224,10 +167,8 @@ static void overcommit(void)
 		    != 0)
 			return;
 	} else {
-		tst_resm(TINFO, "check /proc/meminfo.");
-		fp = fopen(PATH_MEMINFO, "r");
-		if (fp == NULL)
-			tst_brkm(TBROK | TERRNO, cleanup, "fopen");
+		tst_res(TINFO, "check /proc/meminfo.");
+		fp = SAFE_FOPEN(PATH_MEMINFO, "r");
 		if (checkproc(fp, "HugePages_Total", length / 2) != 0)
 			return;
 		if (checkproc(fp, "HugePages_Free", 0) != 0)
@@ -240,12 +181,14 @@ static void overcommit(void)
 	}
 	if (shmid != -1) {
 		if (shmdt(shmaddr) != 0)
-			tst_brkm(TBROK | TERRNO, cleanup, "shmdt");
+			tst_brk(TBROK | TERRNO, "shmdt");
 	} else {
 		munmap(addr, (long)(length / 2 * hugepagesize));
 		close(fd);
 		unlink(s);
 	}
+
+	tst_res(TPASS, "hugepages overcommit test pass");
 }
 
 static void cleanup(void)
@@ -255,39 +198,38 @@ static void cleanup(void)
 	if (restore_shmmax) {
 		fd = open(PATH_SHMMAX, O_WRONLY);
 		if (fd == -1)
-			tst_resm(TWARN | TERRNO, "open");
+			tst_res(TWARN | TERRNO, "open");
 		if (write(fd, shmmax, strlen(shmmax)) != (ssize_t)strlen(shmmax))
-			tst_resm(TWARN | TERRNO, "write");
+			tst_res(TWARN | TERRNO, "write");
 		close(fd);
 	}
 	fd = open(path, O_WRONLY);
 	if (fd == -1)
-		tst_resm(TWARN | TERRNO, "open");
-	tst_resm(TINFO, "restore nr_hugepages to %s.", nr_hugepages);
+		tst_res(TWARN | TERRNO, "open");
+	tst_res(TINFO, "restore nr_hugepages to %s.", nr_hugepages);
 	if (write(fd, nr_hugepages,
 		  strlen(nr_hugepages)) != (ssize_t)strlen(nr_hugepages))
-		tst_resm(TWARN | TERRNO, "write");
+		tst_res(TWARN | TERRNO, "write");
 	close(fd);
 
 	fd = open(pathover, O_WRONLY);
 	if (fd == -1)
-		tst_resm(TWARN | TERRNO, "open");
-	tst_resm(TINFO, "restore nr_overcommit_hugepages to %s.",
+		tst_res(TWARN | TERRNO, "open");
+	tst_res(TINFO, "restore nr_overcommit_hugepages to %s.",
 		 nr_overcommit_hugepages);
 	if (write(fd, nr_overcommit_hugepages, strlen(nr_overcommit_hugepages))
 	    != (ssize_t)strlen(nr_overcommit_hugepages))
-		tst_resm(TWARN | TERRNO, "write");
+		tst_res(TWARN | TERRNO, "write");
 	close(fd);
 
 	/* XXX (garrcoop): memory leak. */
 	snprintf(buf, BUFSIZ, "%s/hugemmap05", tst_get_tmpdir());
 	if (umount(buf) == -1)
-		tst_resm(TWARN | TERRNO, "umount");
+		tst_res(TWARN | TERRNO, "umount");
 	if (shmid != -1) {
-		tst_resm(TINFO, "shmdt cleaning");
+		tst_res(TINFO, "shmdt cleaning");
 		shmctl(shmid, IPC_RMID, NULL);
 	}
-	tst_rmdir();
 }
 
 static void setup(void)
@@ -296,93 +238,97 @@ static void setup(void)
 	int fd;
 	struct stat stat_buf;
 
-	tst_require_root();
+	hugepagesize = SAFE_READ_MEMINFO("Hugepagesize:") * 1024;
+	init_sys_sz_paths();
+
+	if (opt_shmid)
+		shmid = 0;
 
+	if (opt_sysfs) {
+		strncpy(path, path_sys_sz_huge, strlen(path_sys_sz_huge) + 1);
+		strncpy(pathover, path_sys_sz_over,
+			strlen(path_sys_sz_over) + 1);
+	} else {
+		strncpy(path, PATH_PROC_HUGE, strlen(PATH_PROC_HUGE) + 1);
+		strncpy(pathover, PATH_PROC_OVER, strlen(PATH_PROC_OVER) + 1);
+	}
+	if (opt_alloc) {
+		size = atoi(opt_alloc);
+		length = (int)(size + size * 0.5) * 2;
+	}
 	if (stat(pathover, &stat_buf) == -1) {
 		if (errno == ENOENT || errno == ENOTDIR)
-			tst_brkm(TCONF, NULL,
+			tst_brk(TCONF,
 				 "file %s does not exist in the system",
 				 pathover);
 	}
 
-	tst_sig(FORK, DEF_HANDLER, cleanup);
-	TEST_PAUSE;
-	tst_tmpdir();
-
 	if (shmid != -1) {
 		fp = fopen(PATH_SHMMAX, "r");
 		if (fp == NULL)
-			tst_brkm(TBROK | TERRNO, cleanup, "fopen");
+			tst_brk(TBROK | TERRNO, "fopen");
 		if (fgets(shmmax, BUFSIZ, fp) == NULL)
-			tst_brkm(TBROK | TERRNO, cleanup, "fgets");
+			tst_brk(TBROK | TERRNO, "fgets");
 		fclose(fp);
 
 		if (atol(shmmax) < (long)(length / 2 * hugepagesize)) {
 			restore_shmmax = 1;
 			fd = open(PATH_SHMMAX, O_RDWR);
 			if (fd == -1)
-				tst_brkm(TBROK | TERRNO, cleanup, "open");
+				tst_brk(TBROK | TERRNO, "open");
 			snprintf(buf, BUFSIZ, "%ld",
 				 (long)(length / 2 * hugepagesize));
 			if (write(fd, buf, strlen(buf)) != (ssize_t)strlen(buf))
-				tst_brkm(TBROK | TERRNO, cleanup,
+				tst_brk(TBROK | TERRNO,
 					 "failed to change shmmax.");
 		}
 	}
-	fp = fopen(path, "r+");
-	if (fp == NULL)
-		tst_brkm(TBROK | TERRNO, cleanup, "fopen");
+	fp = SAFE_FOPEN(path, "r+");
 	if (fgets(nr_hugepages, BUFSIZ, fp) == NULL)
-		tst_brkm(TBROK | TERRNO, cleanup, "fgets");
+		tst_brk(TBROK | TERRNO, "fgets");
 	fclose(fp);
 	/* Remove trailing newline. */
 	nr_hugepages[strlen(nr_hugepages) - 1] = '\0';
-	tst_resm(TINFO, "original nr_hugepages is %s", nr_hugepages);
+	tst_res(TINFO, "original nr_hugepages is %s", nr_hugepages);
 
-	fd = open(path, O_RDWR);
-	if (fd == -1)
-		tst_brkm(TBROK | TERRNO, cleanup, "open");
+	fd = SAFE_OPEN(path, O_RDWR);
 	/* Reset. */
-	if (write(fd, "0", 1) != 1)
-		tst_brkm(TBROK | TERRNO, cleanup, "write");
-	if (lseek(fd, 0, SEEK_SET) == -1)
-		tst_brkm(TBROK | TERRNO, cleanup, "lseek");
+	SAFE_WRITE(1, fd, "0", 1);
+	SAFE_LSEEK(fd, 0, SEEK_SET);
 	snprintf(buf, BUFSIZ, "%zd", size);
 	if (write(fd, buf, strlen(buf)) != (ssize_t)strlen(buf))
-		tst_brkm(TBROK | TERRNO, cleanup,
-			 "failed to change nr_hugepages.");
+		tst_brk(TBROK | TERRNO,
+			"failed to change nr_hugepages.");
 	close(fd);
 
-	fp = fopen(pathover, "r+");
-	if (fp == NULL)
-		tst_brkm(TBROK | TERRNO, cleanup, "fopen");
+	fp = SAFE_FOPEN(pathover, "r+");
 	if (fgets(nr_overcommit_hugepages, BUFSIZ, fp) == NULL)
-		tst_brkm(TBROK | TERRNO, cleanup, "fgets");
+		tst_brk(TBROK | TERRNO, "fgets");
 	fclose(fp);
 	nr_overcommit_hugepages[strlen(nr_overcommit_hugepages) - 1] = '\0';
-	tst_resm(TINFO, "original nr_overcommit_hugepages is %s",
+	tst_res(TINFO, "original nr_overcommit_hugepages is %s",
 		 nr_overcommit_hugepages);
 
 	fd = open(pathover, O_RDWR);
 	if (fd == -1)
-		tst_brkm(TBROK | TERRNO, cleanup, "open");
+		tst_brk(TBROK | TERRNO, "open");
 	/* Reset. */
 	if (write(fd, "0", 1) != 1)
-		tst_brkm(TBROK | TERRNO, cleanup, "write");
+		tst_brk(TBROK | TERRNO, "write");
 	if (lseek(fd, 0, SEEK_SET) == -1)
-		tst_brkm(TBROK | TERRNO, cleanup, "lseek");
+		tst_brk(TBROK | TERRNO, "lseek");
 	snprintf(buf, BUFSIZ, "%zd", size);
 	if (write(fd, buf, strlen(buf)) != (ssize_t)strlen(buf))
-		tst_brkm(TBROK | TERRNO, cleanup,
+		tst_brk(TBROK | TERRNO,
 			 "failed to change nr_hugepages.");
 	close(fd);
 
 	/* XXX (garrcoop): memory leak. */
 	snprintf(buf, BUFSIZ, "%s/hugemmap05", tst_get_tmpdir());
 	if (mkdir(buf, 0700) == -1)
-		tst_brkm(TBROK | TERRNO, cleanup, "mkdir");
+		tst_brk(TBROK | TERRNO, "mkdir");
 	if (mount(NULL, buf, "hugetlbfs", 0, NULL) == -1)
-		tst_brkm(TBROK | TERRNO, cleanup, "mount");
+		tst_brk(TBROK | TERRNO, "mount");
 }
 
 static void write_bytes(void *addr)
@@ -397,10 +343,10 @@ static void read_bytes(void *addr)
 {
 	long i;
 
-	tst_resm(TINFO, "First hex is %x", *((unsigned int *)addr));
+	tst_res(TINFO, "First hex is %x", *((unsigned int *)addr));
 	for (i = 0; i < (long)(length / 2 * hugepagesize); i++) {
 		if (((char *)addr)[i] != '\a') {
-			tst_resm(TFAIL, "mismatch at %ld", i);
+			tst_res(TFAIL, "mismatch@%ld", i);
 			break;
 		}
 	}
@@ -422,25 +368,16 @@ static int lookup(char *line, char *pattern)
 	return 1;
 }
 
-static void usage(void)
-{
-	printf("  -s      Setup hugepages from sysfs\n");
-	printf("  -m      Reserve hugepages by shmget\n");
-	printf("  -a      Number of overcommint hugepages\n");
-}
-
 static int checksys(char *path, char *string, int value)
 {
 	FILE *fp;
 
-	fp = fopen(path, "r");
-	if (fp == NULL)
-		tst_brkm(TBROK | TERRNO, cleanup, "fopen");
+	fp = SAFE_FOPEN(path, "r");
 	if (fgets(buf, BUFSIZ, fp) == NULL)
-		tst_brkm(TBROK | TERRNO, cleanup, "fgets");
-	tst_resm(TINFO, "%s is %d.", string, atoi(buf));
+		tst_brk(TBROK | TERRNO, "fgets");
+	tst_res(TINFO, "%s is %d.", string, atoi(buf));
 	if (atoi(buf) != value) {
-		tst_resm(TFAIL, "%s is not %d but %d.", string, value,
+		tst_res(TFAIL, "%s is not %d but %d.", string, value,
 			 atoi(buf));
 		fclose(fp);
 		return 1;
@@ -457,39 +394,15 @@ static int checkproc(FILE * fp, char *pattern, int value)
 		if (lookup(line, pattern))
 			break;
 
-	tst_resm(TINFO, "%s is %d.", pattern, atoi(buf));
+	tst_res(TINFO, "%s is %d.", pattern, atoi(buf));
 	if (atoi(buf) != value) {
-		tst_resm(TFAIL, "%s is not %d but %d.", pattern, value,
+		tst_res(TFAIL, "%s is not %d but %d.", pattern, value,
 			 atoi(buf));
 		return 1;
 	}
 	return 0;
 }
 
-static void init_hugepagesize(void)
-{
-	FILE *fp;
-
-	check_hugepage();
-	memset(buf, -1, BUFSIZ);
-	fp = fopen(PATH_MEMINFO, "r");
-	if (fp == NULL)
-		tst_brkm(TBROK, NULL, "can't open %s", PATH_MEMINFO);
-	while (fgets(line, BUFSIZ, fp) != NULL) {
-		if (lookup(line, "Hugepagesize")) {
-			tst_resm(TINFO, "Hugepagesize is %s kB", buf);
-			hugepagesize = atoi(buf) * 1024;
-			fclose(fp);
-			return;
-		}
-	}
-	tst_brkm(TBROK, NULL, "get Hugepagesize failed.");
-}
-
-/*
- * It's not easy to #define tunable file paths via sysfs,
- * use function init_hugepagesize and global variable instead.
- */
 static void init_sys_sz_paths(void)
 {
 	sprintf(path_sys_sz, "/sys/kernel/mm/hugepages/hugepages-%dkB",
@@ -500,3 +413,13 @@ static void init_sys_sz_paths(void)
 	sprintf(path_sys_sz_surp, "%s/surplus_hugepages", path_sys_sz);
 	sprintf(path_sys_sz_huge, "%s/nr_hugepages", path_sys_sz);
 }
+
+static struct tst_test test = {
+	.tid = "hugemmap05",
+	.needs_root = 1,
+	.needs_tmpdir = 1,
+	.options = options,
+	.setup = setup,
+	.cleanup = cleanup,
+	.test_all = test_overcommit,
+};
diff --git a/testcases/kernel/mem/hugetlb/hugemmap/hugemmap06.c b/testcases/kernel/mem/hugetlb/hugemmap/hugemmap06.c
index b061d5a..7254741 100644
--- a/testcases/kernel/mem/hugetlb/hugemmap/hugemmap06.c
+++ b/testcases/kernel/mem/hugetlb/hugemmap/hugemmap06.c
@@ -1,18 +1,15 @@
 /*
- *  Copyright (c) 2015 Red Hat, Inc.
+ * Copyright (c) 2015-2017 Red Hat, Inc.
  *
- * This program is free software: you can redistribute it and/or modify
+ * 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 3 of the License, or
+ * 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, see <http://www.gnu.org/licenses/>.
+ * 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.
  */
 
 /*
@@ -36,22 +33,12 @@
  */
 
 #define _GNU_SOURCE
-#include <errno.h>
 #include <pthread.h>
 #include <stdio.h>
-#include <stdlib.h>
-#include <sys/mman.h>
-#include <sys/types.h>
-#include <unistd.h>
-
-#include "test.h"
 #include "mem.h"
 #include "hugetlb.h"
 #include "lapi/mmap.h"
 
-char *TCID = "hugemmap06";
-int TST_TOTAL = 5;
-
 static long hpage_size;
 static long hugepages;
 
@@ -61,37 +48,29 @@ struct mp {
 };
 
 #define ARSZ 50
+#define LOOP 5
 
-void setup(void)
+static void setup(void)
 {
-	tst_require_root();
 	check_hugepage();
 
-	/* MAP_HUGETLB check */
-	if ((tst_kvercmp(2, 6, 32)) < 0) {
-		tst_brkm(TCONF, NULL, "This test can only run on kernels "
-			"that are 2.6.32 or higher");
-	}
-
-	hpage_size = read_meminfo("Hugepagesize:") * 1024;
+	hpage_size = SAFE_READ_MEMINFO("Hugepagesize:") * 1024;
 	orig_hugepages = get_sys_tune("nr_hugepages");
 
-	hugepages = (ARSZ + 1) * TST_TOTAL;
+	hugepages = (ARSZ + 1) * LOOP;
 
-	if (hugepages * read_meminfo("Hugepagesize:") > read_meminfo("MemTotal:"))
-		tst_brkm(TCONF, NULL, "System RAM is not enough to test.");
+	if (hugepages * SAFE_READ_MEMINFO("Hugepagesize:") > SAFE_READ_MEMINFO("MemTotal:"))
+		tst_brk(TCONF, "System RAM is not enough to test.");
 
 	set_sys_tune("nr_hugepages", hugepages, 1);
-
-	TEST_PAUSE;
 }
 
-void cleanup(void)
+static void cleanup(void)
 {
 	set_sys_tune("nr_hugepages", orig_hugepages, 0);
 }
 
-void *thr(void *arg)
+static void *thr(void *arg)
 {
 	struct mp *mmap_sz = arg;
 	int i, lim, a, b, c;
@@ -108,7 +87,7 @@ void *thr(void *arg)
 	return NULL;
 }
 
-void do_mmap(void)
+static void do_mmap(unsigned int j LTP_ATTRIBUTE_UNUSED)
 {
 	int i, sz = ARSZ + 1;
 	void *addr, *new_addr;
@@ -122,11 +101,11 @@ void do_mmap(void)
 
 	if (addr == MAP_FAILED) {
 		if (errno == ENOMEM) {
-			tst_brkm(TCONF, cleanup,
+			tst_brk(TCONF,
 				"Cannot allocate hugepage, memory too fragmented?");
 		}
 
-		tst_brkm(TBROK | TERRNO, cleanup, "Cannot allocate hugepage");
+		tst_brk(TBROK | TERRNO, "Cannot allocate hugepage");
 	}
 
 	for (i = 0; i < ARSZ; ++i, --sz) {
@@ -135,7 +114,7 @@ void do_mmap(void)
 
 		TEST(pthread_create(&tid[i], NULL, thr, &mmap_sz[i]));
 		if (TEST_RETURN)
-			tst_brkm(TBROK | TRERRNO, cleanup,
+			tst_brk(TBROK | TRERRNO,
 					"pthread_create failed");
 
 		new_addr = mmap(addr, (sz - 1) * hpage_size,
@@ -144,7 +123,7 @@ void do_mmap(void)
 				-1, 0);
 
 		if (new_addr == MAP_FAILED)
-			tst_brkm(TFAIL | TERRNO, cleanup, "mmap failed");
+			tst_brk(TFAIL | TERRNO, "mmap failed");
 
 		addr = new_addr;
 	}
@@ -152,31 +131,23 @@ void do_mmap(void)
 	for (i = 0; i < ARSZ; ++i) {
 		TEST(pthread_join(tid[i], NULL));
 		if (TEST_RETURN)
-			tst_brkm(TBROK | TRERRNO, cleanup,
+			tst_brk(TBROK | TRERRNO,
 					"pthread_join failed");
 	}
 
 	if (munmap(addr, sz * hpage_size) == -1)
-		tst_brkm(TFAIL | TERRNO, cleanup, "huge munmap failed");
-}
-
-int main(int ac, char **av)
-{
-	int lc, i;
+		tst_brk(TFAIL | TERRNO, "huge munmap failed");
 
-	tst_parse_opts(ac, av, NULL, NULL);
-
-	setup();
-
-	for (lc = 0; TEST_LOOPING(lc); lc++) {
-		tst_count = 0;
-
-		for (i = 0; i < TST_TOTAL; i++)
-			do_mmap();
-
-		tst_resm(TPASS, "No regression found.");
-	}
-
-	cleanup();
-	tst_exit();
+	tst_res(TPASS, "No regression found.");
 }
+
+static struct tst_test test = {
+	.tid = "hugemmap06",
+	.min_kver = "2.6.32",
+	.needs_root = 1,
+	.tcnt = LOOP,
+	.needs_tmpdir = 1,
+	.test = do_mmap,
+	.setup = setup,
+	.cleanup = cleanup,
+};
diff --git a/testcases/kernel/mem/hugetlb/lib/hugetlb.h b/testcases/kernel/mem/hugetlb/lib/hugetlb.h
index 1b5cc73..6f5cbb7 100644
--- a/testcases/kernel/mem/hugetlb/lib/hugetlb.h
+++ b/testcases/kernel/mem/hugetlb/lib/hugetlb.h
@@ -27,9 +27,11 @@
 #include <stdlib.h>
 #include <sys/ipc.h>
 #include <sys/shm.h>
+#include <sys/param.h>
 #include <errno.h>
 #include <sys/wait.h>
 #include "tst_test.h"
+#include "old_tmpdir.h"
 
 #define SHM_RD	0400
 #define SHM_WR	0200
-- 
2.9.3


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

* [LTP] [PATCH V2 08/14] mem/hugeshmat: convert to new API
  2017-04-10 10:05 [LTP] [PATCH V2 00/14] Convert LTP kernel/mem/testcase to New API Li Wang
                   ` (6 preceding siblings ...)
  2017-04-10 10:05 ` [LTP] [PATCH V2 07/14] mem/hugemmap: " Li Wang
@ 2017-04-10 10:05 ` Li Wang
  2017-04-10 10:05 ` [LTP] [PATCH V2 09/14] mem/hugeshmctl: " Li Wang
                   ` (5 subsequent siblings)
  13 siblings, 0 replies; 25+ messages in thread
From: Li Wang @ 2017-04-10 10:05 UTC (permalink / raw
  To: ltp

Signed-off-by: Li Wang <liwang@redhat.com>
---
 .../kernel/mem/hugetlb/hugeshmat/hugeshmat01.c     | 179 +++++++++------------
 .../kernel/mem/hugetlb/hugeshmat/hugeshmat02.c     | 133 ++++++---------
 .../kernel/mem/hugetlb/hugeshmat/hugeshmat03.c     | 132 ++++++---------
 .../kernel/mem/hugetlb/hugeshmat/hugeshmat04.c     | 118 +++++---------
 .../kernel/mem/hugetlb/hugeshmat/hugeshmat05.c     |  70 +++-----
 5 files changed, 239 insertions(+), 393 deletions(-)

diff --git a/testcases/kernel/mem/hugetlb/hugeshmat/hugeshmat01.c b/testcases/kernel/mem/hugetlb/hugeshmat/hugeshmat01.c
index cfb4d72..b892d57 100644
--- a/testcases/kernel/mem/hugetlb/hugeshmat/hugeshmat01.c
+++ b/testcases/kernel/mem/hugetlb/hugeshmat/hugeshmat01.c
@@ -1,26 +1,19 @@
 /*
+ * Copyright (c) International Business Machines  Corp., 2001
+ * Copyright (c) Linux Test Project, 2001-2017
  *
- *   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 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
+ * 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.
  */
 
 /*
- * NAME
- *	hugeshmat01.c
- *
  * DESCRIPTION
  *	hugeshmat01 - test that shmat() works correctly
  *
@@ -39,107 +32,73 @@
  *			issue a FAIL message
  *	call cleanup
  *
- * USAGE:  <for command-line>
- *  hugeshmat01 [-c n] [-f] [-i n] [-I x] [-P x] [-t]
- *     where,  -c n : Run n copies concurrently.
- *             -f   : Turn off functionality Testing.
- *	       -i n : Execute test n times.
- *	       -I x : Execute test for x seconds.
- *	       -P x : Pause for x seconds between iterations.
- *	       -t   : Turn on syscall timing.
- *
  * HISTORY
  *	03/2001 - Written by Wayne Boyer
  *	04/2004 - Updated by Robbie Williamson
- *
- * RESTRICTIONS
- *	none
  */
 
-#include "hugetlb.h"
-#include "safe_macros.h"
 #include "mem.h"
+#include "hugetlb.h"
 
-char *TCID = "hugeshmat01";
-int TST_TOTAL = 3;
-
-#define CASE0		10	/* values to write into the shared */
-#define CASE1		20	/* memory location.                */
+#define CASE0 10 /* values to write into the shared */
+#define CASE1 20 /* memory location.                */
 
 static size_t shm_size;
 static int shm_id_1 = -1;
 static void *addr;
 
 static long hugepages = 128;
-static option_t options[] = {
-	{"s:", &sflag, &nr_opt},
+
+static struct tst_option options[] = {
+	{"s:", &nr_opt, "-s   num  Set the number of the been allocated hugepages"},
 	{NULL, NULL, NULL}
 };
 
-struct test_case_t {
+static struct tcase {
 	int *shmid;
 	void *addr;
 	int flags;
-} TC[] = {
+} tcases[] = {
 	/* a straight forward read/write attach */
-	{
-	&shm_id_1, 0, 0},
-	    /*
-	     * an attach using non aligned memory
-	     * -1 will be replaced with an unaligned addr
-	     */
-	{
-	&shm_id_1, (void *)-1, SHM_RND},
-	    /* a read only attach */
-	{
-	&shm_id_1, 0, SHM_RDONLY}
+	{&shm_id_1, 0, 0},
+	/*
+	 * an attach using non aligned memory
+	 * -1 will be replaced with an unaligned addr
+	 */
+	{&shm_id_1, (void *)-1, SHM_RND},
+	/* a read only attach */
+	{&shm_id_1, 0, SHM_RDONLY}
 };
 
-static void check_functionality(int i);
+static void check_functionality(unsigned int i);
 
-int main(int ac, char **av)
+static void verify_hugeshmat(unsigned int i)
 {
-	int lc, i;
-
-	tst_parse_opts(ac, av, options, NULL);
-
-	if (sflag)
-		hugepages = SAFE_STRTOL(NULL, nr_opt, 0, LONG_MAX);
-
-	setup();
-
-	for (lc = 0; TEST_LOOPING(lc); lc++) {
-		tst_count = 0;
-
-		for (i = 0; i < TST_TOTAL; i++) {
-			addr = shmat(*(TC[i].shmid), TC[i].addr, TC[i].flags);
-			if (addr == (void *)-1) {
-				tst_brkm(TFAIL | TERRNO, cleanup, "shmat");
-			} else {
-				check_functionality(i);
-			}
-
-			/*
-			 * addr in TC[0] will be used to generate an unaligned
-			 * address for TC[1]
-			 */
-			if (i == 0 && addr != (void *)-1)
-				TC[1].addr = (void *)(((unsigned long)addr &
-						       ~(SHMLBA - 1)) + SHMLBA -
-						      1);
-			if (shmdt(addr) == -1)
-				tst_brkm(TBROK | TERRNO, cleanup, "shmdt");
-		}
+	struct tcase *tc = &tcases[i];
+
+	addr = shmat(*(tc->shmid), tc->addr, tc->flags);
+	if (addr == (void *)-1) {
+		tst_brk(TFAIL | TERRNO, "shmat");
+	} else {
+		check_functionality(i);
 	}
-	cleanup();
-	tst_exit();
+
+	/*
+	 * addr in tcases[0] will be used to generate an unaligned
+	 * address for tcases[1]
+	 */
+	if (i == 0 && addr != (void *)-1)
+		tc[1].addr = (void *)(((unsigned long)addr &
+					~(SHMLBA - 1)) + SHMLBA - 1);
+	if (shmdt(addr) == -1)
+		tst_brk(TBROK | TERRNO, "shmdt");
 }
 
 /*
  * check_functionality - check various conditions to make sure they
  *			 are correct.
  */
-static void check_functionality(int i)
+static void check_functionality(unsigned int i)
 {
 	void *orig_add;
 	int *shared;
@@ -149,17 +108,17 @@ static void check_functionality(int i)
 
 	/* stat the shared memory ID */
 	if (shmctl(shm_id_1, IPC_STAT, &buf) == -1)
-		tst_brkm(TBROK | TERRNO, cleanup, "shmctl");
+		tst_brk(TBROK | TERRNO, "shmctl");
 
 	/* check the number of attaches */
 	if (buf.shm_nattch != 1) {
-		tst_resm(TFAIL, "# of attaches is incorrect");
+		tst_res(TFAIL, "# of attaches is incorrect");
 		return;
 	}
 
 	/* check the size of the segment */
 	if (buf.shm_segsz != shm_size) {
-		tst_resm(TFAIL, "segment size is incorrect");
+		tst_res(TFAIL, "segment size is incorrect");
 		return;
 	}
 
@@ -182,9 +141,9 @@ static void check_functionality(int i)
 		 * specified in the man page.
 		 */
 		*shared = CASE1;
-		orig_add = addr + ((unsigned long)TC[i].addr % SHMLBA);
-		if (orig_add != TC[i].addr) {
-			tst_resm(TFAIL, "shared memory address is not "
+		orig_add = addr + ((unsigned long)tcases[i].addr % SHMLBA);
+		if (orig_add != tcases[i].addr) {
+			tst_res(TFAIL, "shared memory address is not "
 				 "correct");
 			return;
 		}
@@ -196,43 +155,49 @@ static void check_functionality(int i)
 		 * because shared memory is persistent.
 		 */
 		if (*shared != CASE1) {
-			tst_resm(TFAIL, "shared memory value isn't correct");
+			tst_res(TFAIL, "shared memory value isn't correct");
 			return;
 		}
 		break;
 	}
-	tst_resm(TPASS, "conditions and functionality are correct");
+	tst_res(TPASS, "conditions and functionality are correct");
 }
 
-void setup(void)
+static void setup(void)
 {
 	long hpage_size;
 
-	tst_require_root();
 	check_hugepage();
-	tst_sig(NOFORK, DEF_HANDLER, cleanup);
-	tst_tmpdir();
+	if (nr_opt)
+		hugepages = SAFE_STRTOL(nr_opt, 0, LONG_MAX);
 
 	orig_hugepages = get_sys_tune("nr_hugepages");
 	set_sys_tune("nr_hugepages", hugepages, 1);
-	hpage_size = read_meminfo("Hugepagesize:") * 1024;
+	hpage_size = SAFE_READ_MEMINFO("Hugepagesize:") * 1024;
 
 	shm_size = hpage_size * hugepages / 2;
 	update_shm_size(&shm_size);
-	shmkey = getipckey(cleanup);
+	shmkey = getipckey();
 	shm_id_1 = shmget(shmkey++, shm_size,
 			  SHM_HUGETLB | SHM_RW | IPC_CREAT | IPC_EXCL);
 	if (shm_id_1 == -1)
-		tst_brkm(TBROK | TERRNO, cleanup, "shmget");
+		tst_brk(TBROK | TERRNO, "shmget");
 
-	TEST_PAUSE;
 }
 
-void cleanup(void)
+static void cleanup(void)
 {
 	rm_shm(shm_id_1);
-
 	set_sys_tune("nr_hugepages", orig_hugepages, 0);
-
-	tst_rmdir();
 }
+
+static struct tst_test test = {
+	.tid = "hugeshmat01",
+	.needs_root = 1,
+	.needs_tmpdir = 1,
+	.options = options,
+	.tcnt = ARRAY_SIZE(tcases),
+	.test = verify_hugeshmat,
+	.setup = setup,
+	.cleanup = cleanup,
+};
diff --git a/testcases/kernel/mem/hugetlb/hugeshmat/hugeshmat02.c b/testcases/kernel/mem/hugetlb/hugeshmat/hugeshmat02.c
index e644178..ef26e95 100644
--- a/testcases/kernel/mem/hugetlb/hugeshmat/hugeshmat02.c
+++ b/testcases/kernel/mem/hugetlb/hugeshmat/hugeshmat02.c
@@ -1,26 +1,19 @@
 /*
+ * Copyright (c) International Business Machines  Corp., 2004
+ * Copyright (c) Linux Test Project, 2004-2017
  *
- *   Copyright (c) International Business Machines  Corp., 2004
+ * 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 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
+ * 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.
  */
 
 /*
- * NAME
- *	hugeshmat02.c
- *
  * DESCRIPTION
  *	hugeshmat02 - check for EINVAL and EACCES errors with hugetlb
  *
@@ -33,15 +26,6 @@
  *	    issue a FAIL message
  *	call cleanup
  *
- * USAGE:  <for command-line>
- *  hugeshmat02 [-c n] [-e] [-i n] [-I x] [-P x] [-t]
- *     where,  -c n : Run n copies concurrently.
- *             -e   : Turn on errno logging.
- *	       -i n : Execute test n times.
- *	       -I x : Execute test for x seconds.
- *	       -P x : Pause for x seconds between iterations.
- *	       -t   : Turn on syscall timing.
- *
  * HISTORY
  *	03/2001 - Written by Wayne Boyer
  *	04/2004 - Updated By Robbie Williamson
@@ -50,13 +34,8 @@
  *	Must be ran as root
  */
 
-#include <pwd.h>
-#include "hugetlb.h"
-#include "safe_macros.h"
 #include "mem.h"
-
-char *TCID = "hugeshmat02";
-int TST_TOTAL = 2;
+#include "hugetlb.h"
 
 #if __WORDSIZE == 64
 #define NADDR	0x10000000eef	/* a 64bit non alligned address value */
@@ -70,88 +49,80 @@ static int shm_id_2 = -1;
 static void *addr;
 
 static long hugepages = 128;
-static option_t options[] = {
-	{"s:", &sflag, &nr_opt},
+
+static struct tst_option options[] = {
+	{"s:", &nr_opt, "-s   num  Set the number of the been allocated hugepages"},
 	{NULL, NULL, NULL}
 };
 
-struct test_case_t {
+struct tcase {
 	int *shmid;
 	void *addr;
 	int error;
-} TC[] = {
+} tcases[] = {
 	/* EINVAL - the shared memory ID is not valid */
-	{
-	&shm_id_1, NULL, EINVAL},
-	    /* EINVAL - the address is not page aligned and SHM_RND is not given */
-	{
-&shm_id_2, (void *)NADDR, EINVAL},};
+	{&shm_id_1, NULL, EINVAL},
+	/* EINVAL - the address is not page aligned and SHM_RND is not given */
+	{&shm_id_2, (void *)NADDR, EINVAL}
+};
 
-int main(int ac, char **av)
+static void verify_hugeshmat(unsigned int i)
 {
-	int lc, i;
-
-	tst_parse_opts(ac, av, options, NULL);
-
-	if (sflag)
-		hugepages = SAFE_STRTOL(NULL, nr_opt, 0, LONG_MAX);
-
-	setup();
-
-	for (lc = 0; TEST_LOOPING(lc); lc++) {
-		tst_count = 0;
-
-		for (i = 0; i < TST_TOTAL; i++) {
-			addr = shmat(*(TC[i].shmid), TC[i].addr, 0);
-			if (addr != (void *)-1) {
-				tst_resm(TFAIL, "shmat suceeded unexpectedly");
-				continue;
-			}
-			if (errno == TC[i].error)
-				tst_resm(TPASS | TERRNO, "shmat failed as "
-					 "expected");
-			else
-				tst_resm(TFAIL | TERRNO, "shmat failed "
-					 "unexpectedly - expect errno=%d, "
-					 "got", TC[i].error);
-		}
+	struct tcase *tc = &tcases[i];
+
+	addr = shmat(*(tc->shmid), tc->addr, 0);
+	if (addr != (void *)-1) {
+		tst_res(TFAIL, "shmat suceeded unexpectedly");
+		return;
+	}
+
+	if (errno == tc->error) {
+		tst_res(TPASS | TERRNO, "shmat failed as "
+				"expected");
+	} else {
+		tst_res(TFAIL | TERRNO, "shmat failed "
+				"unexpectedly - expect errno=%d, "
+				"got", tc->error);
 	}
-	cleanup();
-	tst_exit();
 }
 
 void setup(void)
 {
 	long hpage_size;
 
-	tst_require_root();
 	check_hugepage();
-	tst_sig(NOFORK, DEF_HANDLER, cleanup);
-	tst_tmpdir();
+	if (nr_opt)
+		hugepages = SAFE_STRTOL(nr_opt, 0, LONG_MAX);
 
 	orig_hugepages = get_sys_tune("nr_hugepages");
 	set_sys_tune("nr_hugepages", hugepages, 1);
-	hpage_size = read_meminfo("Hugepagesize:") * 1024;
+	hpage_size = SAFE_READ_MEMINFO("Hugepagesize:") * 1024;
 
 	shm_size = hpage_size * hugepages / 2;
 	update_shm_size(&shm_size);
-	shmkey = getipckey(cleanup);
+	shmkey = getipckey();
 
 	/* create a shared memory resource with read and write permissions */
 	/* also post increment the shmkey for the next shmget call */
 	shm_id_2 = shmget(shmkey++, shm_size,
 			  SHM_HUGETLB | SHM_RW | IPC_CREAT | IPC_EXCL);
 	if (shm_id_2 == -1)
-		tst_brkm(TBROK | TERRNO, cleanup, "shmget");
-
-	TEST_PAUSE;
+		tst_brk(TBROK | TERRNO, "shmget");
 }
 
 void cleanup(void)
 {
 	rm_shm(shm_id_2);
-
 	set_sys_tune("nr_hugepages", orig_hugepages, 0);
-
-	tst_rmdir();
 }
+
+static struct tst_test test = {
+	.tid = "hugeshmat02",
+	.needs_root = 1,
+	.needs_tmpdir = 1,
+	.options = options,
+	.tcnt = ARRAY_SIZE(tcases),
+	.test = verify_hugeshmat,
+	.setup = setup,
+	.cleanup = cleanup,
+};
diff --git a/testcases/kernel/mem/hugetlb/hugeshmat/hugeshmat03.c b/testcases/kernel/mem/hugetlb/hugeshmat/hugeshmat03.c
index a0cea9a..8121e3c 100644
--- a/testcases/kernel/mem/hugetlb/hugeshmat/hugeshmat03.c
+++ b/testcases/kernel/mem/hugetlb/hugeshmat/hugeshmat03.c
@@ -1,26 +1,19 @@
 /*
+ * Copyright (c) International Business Machines  Corp., 2004
+ * Copyright (c) Linux Test Project, 2004-2017
  *
- *   Copyright (c) International Business Machines  Corp., 2004
+ * 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 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
+ * 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.
  */
 
 /*
- * NAME
- *	hugeshmat03.c
- *
  * DESCRIPTION
  *	hugeshmat03 - test for EACCES error
  *
@@ -40,15 +33,6 @@
  *	  wait for child to exit
  *	  remove the shared memory segment
  *
- * USAGE:  <for command-line>
- *  hugeshmat03 [-c n] [-e] [-i n] [-I x] [-P x] [-t]
- *     where,  -c n : Run n copies concurrently.
- *             -e   : Turn on errno logging.
- *	       -i n : Execute test n times.
- *	       -I x : Execute test for x seconds.
- *	       -P x : Pause for x seconds between iterations.
- *	       -t   : Turn on syscall timing.
- *
  * HISTORY
  *	03/2001 - Written by Wayne Boyer
  *	04/2004 - Updated by Robbie Williamson
@@ -57,12 +41,8 @@
  *	test must be run at root
  */
 
-#include "hugetlb.h"
-#include "safe_macros.h"
 #include "mem.h"
-
-char *TCID = "hugeshmat03";
-int TST_TOTAL = 1;
+#include "hugetlb.h"
 
 static size_t shm_size;
 static int shm_id_1 = -1;
@@ -71,92 +51,76 @@ static uid_t ltp_uid;
 static char *ltp_user = "nobody";
 
 static long hugepages = 128;
-static option_t options[] = {
-	{"s:", &sflag, &nr_opt},
+
+static struct tst_option options[] = {
+	{"s:", &nr_opt, "-s   num  Set the number of the been allocated hugepages"},
 	{NULL, NULL, NULL}
 };
 
-static void do_child(void);
-
-int main(int ac, char **av)
+static void verify_hugeshmat(void)
 {
 	int status;
 	pid_t pid;
 
-	tst_parse_opts(ac, av, options, NULL);
-
-	if (sflag)
-		hugepages = SAFE_STRTOL(NULL, nr_opt, 0, LONG_MAX);
-
-	setup();
-
-	switch (pid = fork()) {
-	case -1:
-		tst_brkm(TBROK | TERRNO, cleanup, "fork");
+	switch (pid = SAFE_FORK()) {
 	case 0:
 		if (setuid(ltp_uid) == -1)
-			tst_brkm(TBROK | TERRNO, cleanup, "setuid");
-		do_child();
-		tst_exit();
-	default:
-		if (waitpid(pid, &status, 0) == -1)
-			tst_brkm(TBROK | TERRNO, cleanup, "waitpid");
-	}
-	cleanup();
-	tst_exit();
-}
-
-static void do_child(void)
-{
-	int lc;
-
-	for (lc = 0; TEST_LOOPING(lc); lc++) {
-		tst_count = 0;
+			tst_brk(TBROK | TERRNO, "setuid");
 
 		addr = shmat(shm_id_1, NULL, 0);
 		if (addr != (void *)-1) {
-			tst_resm(TFAIL, "shmat succeeded unexpectedly");
-			continue;
+			tst_res(TFAIL, "shmat succeeded unexpectedly");
+			return;
+		}
+		if (errno == EACCES) {
+			tst_res(TPASS | TERRNO, "shmat failed as expected");
+		} else {
+			tst_res(TFAIL | TERRNO, "shmat failed unexpectedly "
+					"- expect errno=EACCES, got");
 		}
-		if (errno == EACCES)
-			tst_resm(TPASS | TERRNO, "shmat failed as expected");
-		else
-			tst_resm(TFAIL | TERRNO, "shmat failed unexpectedly "
-				 "- expect errno=EACCES, got");
+		break;
+	default:
+		if (waitpid(pid, &status, 0) == -1)
+			tst_brk(TBROK | TERRNO, "waitpid");
 	}
 }
 
-void setup(void)
+static void setup(void)
 {
 	long hpage_size;
 
-	tst_require_root();
 	check_hugepage();
-	tst_sig(FORK, DEF_HANDLER, cleanup);
-	tst_tmpdir();
+	if (nr_opt)
+		hugepages = SAFE_STRTOL(nr_opt, 0, LONG_MAX);
 
 	orig_hugepages = get_sys_tune("nr_hugepages");
 	set_sys_tune("nr_hugepages", hugepages, 1);
-	hpage_size = read_meminfo("Hugepagesize:") * 1024;
+	hpage_size = SAFE_READ_MEMINFO("Hugepagesize:") * 1024;
 
 	shm_size = hpage_size * hugepages / 2;
 	update_shm_size(&shm_size);
-	shmkey = getipckey(cleanup);
+	shmkey = getipckey();
 	shm_id_1 = shmget(shmkey, shm_size,
 			  SHM_HUGETLB | SHM_RW | IPC_CREAT | IPC_EXCL);
 	if (shm_id_1 == -1)
-		tst_brkm(TBROK | TERRNO, cleanup, "shmget");
-
-	ltp_uid = getuserid(cleanup, ltp_user);
+		tst_brk(TBROK | TERRNO, "shmget");
 
-	TEST_PAUSE;
+	ltp_uid = getuserid(ltp_user);
 }
 
-void cleanup(void)
+static void cleanup(void)
 {
 	rm_shm(shm_id_1);
-
 	set_sys_tune("nr_hugepages", orig_hugepages, 0);
-
-	tst_rmdir();
 }
+
+static struct tst_test test = {
+	.tid = "hugeshmat03",
+	.needs_root = 1,
+	.forks_child = 1,
+	.needs_tmpdir = 1,
+	.options = options,
+	.test_all = verify_hugeshmat,
+	.setup = setup,
+	.cleanup = cleanup,
+};
diff --git a/testcases/kernel/mem/hugetlb/hugeshmat/hugeshmat04.c b/testcases/kernel/mem/hugetlb/hugeshmat/hugeshmat04.c
index 7a71747..4239b2f 100644
--- a/testcases/kernel/mem/hugetlb/hugeshmat/hugeshmat04.c
+++ b/testcases/kernel/mem/hugetlb/hugeshmat/hugeshmat04.c
@@ -1,20 +1,15 @@
 /*
- *   Copyright (c) Linux Test Project, 2014
+ * Copyright (c) Linux Test Project, 2014-2017
  *
- *   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 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
+ * 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.
  */
 
 /*
@@ -35,26 +30,12 @@
  * 	test must be run at root
  */
 
-#include <stdio.h>
-#include <stdlib.h>
-#include <unistd.h>
-#include <fcntl.h>
-#include <string.h>
-#include <sys/mman.h>
-#include <sys/types.h>
-#include <sys/shm.h>
-#include <sys/wait.h>
-
-#include "test.h"
 #include "mem.h"
 #include "hugetlb.h"
 
 #define SIZE	(1024 * 1024 * 1024)
 #define BOUNDARY (1024 * 1024 * 1024)
 
-char *TCID = "hugeshmat04";
-int TST_TOTAL = 3;
-
 static long huge_free;
 static long huge_free2;
 static long hugepages;
@@ -62,36 +43,19 @@ static long orig_shmmax, new_shmmax;
 
 static void shared_hugepage(void);
 
-int main(int ac, char **av)
+static void test_hugeshmat(unsigned int i LTP_ATTRIBUTE_UNUSED)
 {
-	int lc, i;
-
-	tst_parse_opts(ac, av, NULL, NULL);
-
-	setup();
-
-	for (lc = 0; TEST_LOOPING(lc); lc++) {
-		tst_count = 0;
-
-		for (i = 0; i < TST_TOTAL; i++) {
-
-			huge_free = read_meminfo("HugePages_Free:");
-			shared_hugepage();
-			huge_free2 = read_meminfo("HugePages_Free:");
-
-			if (huge_free2 != huge_free)
-				tst_brkm(TFAIL, cleanup,
-				"Test failed. Hugepage leak inspection.");
-			else
-				tst_resm(TPASS, "No regression found.");
-		}
-	}
-
-	cleanup();
-	tst_exit();
+	huge_free = SAFE_READ_MEMINFO("HugePages_Free:");
+	shared_hugepage();
+	huge_free2 = SAFE_READ_MEMINFO("HugePages_Free:");
+
+	if (huge_free2 != huge_free)
+		tst_brk(TFAIL, "Test failed. Hugepage leak inspection.");
+	else
+		tst_res(TPASS, "No regression found.");
 }
 
-void shared_hugepage(void)
+static void shared_hugepage(void)
 {
 	pid_t pid;
 	int status, shmid;
@@ -100,56 +64,60 @@ void shared_hugepage(void)
 
 	shmid = shmget(IPC_PRIVATE, size, SHM_HUGETLB | IPC_CREAT | 0777);
 	if (shmid < 0)
-		tst_brkm(TBROK | TERRNO, cleanup, "shmget");
+		tst_brk(TBROK | TERRNO, "shmget");
 
 	buf = shmat(shmid, (void *)BOUNDARY, SHM_RND | 0777);
 	if (buf == (void *)-1) {
 		shmctl(shmid, IPC_RMID, NULL);
-		tst_brkm(TBROK | TERRNO, cleanup, "shmat");
+		tst_brk(TBROK | TERRNO, "shmat");
 	}
 
 	memset(buf, 2, size);
-	pid = fork();
-
+	pid = SAFE_FORK();
 	if (pid == 0)
 		exit(1);
-	else if (pid < 0)
-		tst_brkm(TBROK | TERRNO, cleanup, "fork");
 
 	wait(&status);
 	shmdt(buf);
 	shmctl(shmid, IPC_RMID, NULL);
 }
 
-void setup(void)
+static void setup(void)
 {
 	long mem_total, hpage_size;
 
-	tst_require_root();
 	check_hugepage();
-
-	mem_total = read_meminfo("MemTotal:");
-	SAFE_FILE_SCANF(NULL, PATH_SHMMAX, "%ld", &orig_shmmax);
-	SAFE_FILE_PRINTF(NULL, PATH_SHMMAX, "%ld", (long)SIZE);
-	SAFE_FILE_SCANF(NULL, PATH_SHMMAX, "%ld", &new_shmmax);
+	mem_total = SAFE_READ_MEMINFO("MemTotal:");
+	SAFE_FILE_SCANF(PATH_SHMMAX, "%ld", &orig_shmmax);
+	SAFE_FILE_PRINTF(PATH_SHMMAX, "%ld", (long)SIZE);
+	SAFE_FILE_SCANF(PATH_SHMMAX, "%ld", &new_shmmax);
 
 	if (mem_total < 2L*1024*1024)
-		tst_brkm(TCONF,	NULL, "Needed > 2GB RAM, have: %ld", mem_total);
+		tst_brk(TCONF,	"Needed > 2GB RAM, have: %ld", mem_total);
 
 	if (new_shmmax < SIZE)
-		tst_brkm(TCONF,	NULL, "shmmax too low, have: %ld", new_shmmax);
+		tst_brk(TCONF,	"shmmax too low, have: %ld", new_shmmax);
 
 	orig_hugepages = get_sys_tune("nr_hugepages");
-	hpage_size = read_meminfo("Hugepagesize:") * 1024;
+	hpage_size = SAFE_READ_MEMINFO("Hugepagesize:") * 1024;
 
 	hugepages = (orig_hugepages * hpage_size + SIZE) / hpage_size;
 	set_sys_tune("nr_hugepages", hugepages, 1);
-
-	TEST_PAUSE;
 }
 
-void cleanup(void)
+static void cleanup(void)
 {
 	set_sys_tune("nr_hugepages", orig_hugepages, 0);
-	SAFE_FILE_PRINTF(NULL, PATH_SHMMAX, "%ld", orig_shmmax);
+	SAFE_FILE_PRINTF(PATH_SHMMAX, "%ld", orig_shmmax);
 }
+
+static struct tst_test test = {
+	.tid = "hugeshmat04",
+	.needs_root = 1,
+	.forks_child = 1,
+	.needs_tmpdir = 1,
+	.tcnt = 3,
+	.test = test_hugeshmat,
+	.setup = setup,
+	.cleanup = cleanup,
+};
diff --git a/testcases/kernel/mem/hugetlb/hugeshmat/hugeshmat05.c b/testcases/kernel/mem/hugetlb/hugeshmat/hugeshmat05.c
index 8c17f21..23c1a3b 100644
--- a/testcases/kernel/mem/hugetlb/hugeshmat/hugeshmat05.c
+++ b/testcases/kernel/mem/hugetlb/hugeshmat/hugeshmat05.c
@@ -1,18 +1,15 @@
 /*
- * Copyright (c) 2015 Red Hat, Inc.
+ * Copyright (c) 2015-2017 Red Hat, Inc.
  *
- * This program is free software: you can redistribute it and/or modify
+ * 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 3 of the License, or
+ * 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, see <http://www.gnu.org/licenses/>.
+ * 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.
  */
 
 /*
@@ -39,21 +36,9 @@
  *
  */
 
-#include <stdlib.h>
-#include <stdio.h>
-#include <sys/types.h>
-#include <sys/ipc.h>
-#include <sys/shm.h>
-#include <sys/mman.h>
-#include <fcntl.h>
-
-#include "test.h"
 #include "mem.h"
 #include "hugetlb.h"
 
-char *TCID = "hugeshmat05";
-int TST_TOTAL = 1;
-
 static long page_size;
 static long hpage_size;
 static long hugepages;
@@ -62,17 +47,13 @@ static long hugepages;
 
 void setup(void)
 {
-	tst_require_root();
 	check_hugepage();
-
 	orig_hugepages = get_sys_tune("nr_hugepages");
 	page_size = getpagesize();
-	hpage_size = read_meminfo("Hugepagesize:") * 1024;
+	hpage_size = SAFE_READ_MEMINFO("Hugepagesize:") * 1024;
 
 	hugepages = N + 1;
 	set_sys_tune("nr_hugepages", hugepages, 1);
-
-	TEST_PAUSE;
 }
 
 void cleanup(void)
@@ -87,35 +68,30 @@ void shm_test(int size)
 
 	shmid = shmget(IPC_PRIVATE, size, 0600 | IPC_CREAT | SHM_HUGETLB);
 	if (shmid < 0)
-		tst_brkm(TBROK | TERRNO, cleanup, "shmget failed");
+		tst_brk(TBROK | TERRNO, "shmget failed");
 
 	shmaddr = shmat(shmid, 0, 0);
 	if (shmaddr == (char *)-1) {
 		shmctl(shmid, IPC_RMID, NULL);
-		tst_brkm(TFAIL | TERRNO, cleanup,
+		tst_brk(TFAIL | TERRNO,
 			 "Bug: shared memory attach failure.");
 	}
 
 	shmaddr[0] = 1;
-	tst_resm(TINFO, "allocated %d huge bytes", size);
+	tst_res(TINFO, "allocated %d huge bytes", size);
 
 	if (shmdt((const void *)shmaddr) != 0) {
 		shmctl(shmid, IPC_RMID, NULL);
-		tst_brkm(TFAIL | TERRNO, cleanup, "Detach failure.");
+		tst_brk(TFAIL | TERRNO, "Detach failure.");
 	}
 
 	shmctl(shmid, IPC_RMID, NULL);
 }
 
-int main(int ac, char **av)
+static void test_hugeshmat(void)
 {
-	int lc;
 	unsigned int i;
 
-	tst_parse_opts(ac, av, NULL, NULL);
-
-	setup();
-
 	const int tst_sizes[] = {
 		N * hpage_size - page_size,
 		N * hpage_size - page_size - 1,
@@ -123,15 +99,17 @@ int main(int ac, char **av)
 		hpage_size + 1
 	};
 
-	for (lc = 0; TEST_LOOPING(lc); lc++) {
-		tst_count = 0;
-
-		for (i = 0; i < ARRAY_SIZE(tst_sizes); ++i)
-			shm_test(tst_sizes[i]);
+	for (i = 0; i < ARRAY_SIZE(tst_sizes); ++i)
+		shm_test(tst_sizes[i]);
 
-		tst_resm(TPASS, "No regression found.");
-	}
-
-	cleanup();
-	tst_exit();
+	tst_res(TPASS, "No regression found.");
 }
+
+static struct tst_test test = {
+	.tid = "hugeshmat05",
+	.needs_root = 1,
+	.needs_tmpdir = 1,
+	.test_all = test_hugeshmat,
+	.setup = setup,
+	.cleanup = cleanup,
+};
-- 
2.9.3


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

* [LTP] [PATCH V2 09/14] mem/hugeshmctl: convert to new API
  2017-04-10 10:05 [LTP] [PATCH V2 00/14] Convert LTP kernel/mem/testcase to New API Li Wang
                   ` (7 preceding siblings ...)
  2017-04-10 10:05 ` [LTP] [PATCH V2 08/14] mem/hugeshmat: " Li Wang
@ 2017-04-10 10:05 ` Li Wang
  2017-04-10 10:05 ` [LTP] [PATCH V2 10/14] mm/hugeshmdt: " Li Wang
                   ` (4 subsequent siblings)
  13 siblings, 0 replies; 25+ messages in thread
From: Li Wang @ 2017-04-10 10:05 UTC (permalink / raw
  To: ltp

Signed-off-by: Li Wang <liwang@redhat.com>
---
 .../kernel/mem/hugetlb/hugeshmctl/hugeshmctl01.c   | 211 +++++++++------------
 .../kernel/mem/hugetlb/hugeshmctl/hugeshmctl02.c   | 153 ++++++---------
 .../kernel/mem/hugetlb/hugeshmctl/hugeshmctl03.c   | 152 ++++++---------
 3 files changed, 210 insertions(+), 306 deletions(-)

diff --git a/testcases/kernel/mem/hugetlb/hugeshmctl/hugeshmctl01.c b/testcases/kernel/mem/hugetlb/hugeshmctl/hugeshmctl01.c
index a54077a..17ef0b0 100644
--- a/testcases/kernel/mem/hugetlb/hugeshmctl/hugeshmctl01.c
+++ b/testcases/kernel/mem/hugetlb/hugeshmctl/hugeshmctl01.c
@@ -1,27 +1,19 @@
 /*
+ * Copyright (c) International Business Machines  Corp., 2004
+ * Copyright (c) Linux Test Project, 2004-2017
  *
- *   Copyright (c) International Business Machines  Corp., 2004
+ * 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 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
+ * 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.
  */
 
 /*
- * NAME
- *	hugeshmctl01.c
- *
  * DESCRIPTION
  *	hugeshmctl01 - test the IPC_STAT, IPC_SET and IPC_RMID commands as
  *		   they are used with shmctl()
@@ -44,31 +36,14 @@
  *	    issue a PASS message
  *	call cleanup
  *
- * USAGE:  <for command-line>
- *  hugeshmctl01 [-c n] [-f] [-i n] [-I x] [-P x] [-t]
- *     where,  -c n : Run n copies concurrently.
- *             -f   : Turn off functionality Testing.
- *	       -i n : Execute test n times.
- *	       -I x : Execute test for x seconds.
- *	       -P x : Pause for x seconds between iterations.
- *	       -t   : Turn on syscall timing.
- *
  * HISTORY
  *	03/2001 - Written by Wayne Boyer
  *	04/2004 - Updated by Robbie Williamson
- *
- * RESTRICTIONS
- *	none
  */
 
-#include "test.h"
-#include "safe_macros.h"
 #include "mem.h"
 #include "hugetlb.h"
 
-char *TCID = "hugeshmctl01";
-int TST_TOTAL = 4;
-
 #define FIRST		0
 #define SECOND		1
 #define N_ATTACH	4
@@ -92,67 +67,54 @@ static void func_rmid(void);
 static void *set_shmat(void);
 
 static long hugepages = 128;
-static option_t options[] = {
-	{"s:", &sflag, &nr_opt},
+
+static struct tst_option options[] = {
+	{"s:", &nr_opt, "-s   num  Set the number of the been allocated hugepages"},
 	{NULL, NULL, NULL}
 };
 
-struct test_case_t {
+struct tcase {
 	int cmd;
 	void (*func_test) (void);
 	void (*func_setup) (void);
-} TC[] = {
-	{
-	IPC_STAT, func_stat, stat_setup}, {
-	IPC_STAT, func_stat, stat_setup}, {
-	IPC_SET, func_set, set_setup}, {
-	IPC_RMID, func_rmid, NULL}
+} tcases[] = {
+	{IPC_STAT, func_stat, stat_setup},
+	{IPC_STAT, func_stat, stat_setup},
+	{IPC_SET,  func_set,  set_setup},
+	{IPC_RMID, func_rmid, NULL}
 };
 
-int main(int ac, char **av)
+static void test_hugeshmctl(void)
 {
-	int lc, i;
+	unsigned int i;
 
-	tst_parse_opts(ac, av, options, NULL);
+	/* initialize stat_time */
+	stat_time = FIRST;
 
-	if (sflag)
-		hugepages = SAFE_STRTOL(NULL, nr_opt, 0, LONG_MAX);
-
-	setup();
-
-	for (lc = 0; TEST_LOOPING(lc); lc++) {
-		tst_count = 0;
-
-		/* initialize stat_time */
-		stat_time = FIRST;
+	/*
+	 * Create a shared memory segment with read and write
+	 * permissions.  Do this here instead of in setup()
+	 * so that looping (-i) will work correctly.
+	 */
+	shm_id_1 = shmget(shmkey, shm_size,
+			SHM_HUGETLB | IPC_CREAT | IPC_EXCL | SHM_RW);
+	if (shm_id_1 == -1)
+		tst_brk(TBROK | TERRNO, "shmget #main");
 
+	for (i = 0; i < ARRAY_SIZE(tcases); i++) {
 		/*
-		 * Create a shared memory segment with read and write
-		 * permissions.  Do this here instead of in setup()
-		 * so that looping (-i) will work correctly.
+		 * if needed, set up any required conditions by
+		 * calling the appropriate setup function
 		 */
-		shm_id_1 = shmget(shmkey, shm_size,
-				  SHM_HUGETLB | IPC_CREAT | IPC_EXCL | SHM_RW);
-		if (shm_id_1 == -1)
-			tst_brkm(TBROK | TERRNO, cleanup, "shmget #main");
+		if (tcases[i].func_setup != NULL)
+			(*tcases[i].func_setup) ();
 
-		for (i = 0; i < TST_TOTAL; i++) {
-			/*
-			 * if needed, set up any required conditions by
-			 * calling the appropriate setup function
-			 */
-			if (TC[i].func_setup != NULL)
-				(*TC[i].func_setup) ();
-
-			if (shmctl(shm_id_1, TC[i].cmd, &buf) == -1) {
-				tst_resm(TFAIL | TERRNO, "shmctl #main");
-				continue;
-			}
-			(*TC[i].func_test) ();
+		if (shmctl(shm_id_1, tcases[i].cmd, &buf) == -1) {
+			tst_res(TFAIL | TERRNO, "shmctl #main");
+			continue;
 		}
+		(*tcases[i].func_test) ();
 	}
-	cleanup();
-	tst_exit();
 }
 
 /*
@@ -166,7 +128,7 @@ void *set_shmat(void)
 
 	rval = shmat(shm_id_1, 0, 0);
 	if (rval == (void *)-1)
-		tst_brkm(TBROK | TERRNO, cleanup, "set shmat");
+		tst_brk(TBROK | TERRNO, "set shmat");
 
 	return rval;
 }
@@ -182,7 +144,15 @@ static void stat_setup(void)
 	void *test;
 	pid_t pid;
 	sigset_t newmask, oldmask;
-
+	struct sigaction sa;
+
+	memset (&sa, '\0', sizeof(sa));
+	sa.sa_handler = sighandler;
+	sa.sa_flags = 0;
+	TEST(sigaction(SIGUSR1, &sa, NULL));
+	if (TEST_RETURN == -1)
+		tst_brk(TBROK | TRERRNO,
+				"SIGSEGV signal setup failed");
 	/*
 	 * The first time through, let the children attach the memory.
 	 * The second time through, attach the memory first and let
@@ -210,12 +180,10 @@ static void stat_setup(void)
 	sigemptyset(&newmask);
 	sigaddset(&newmask, SIGUSR1);
 	if (sigprocmask(SIG_BLOCK, &newmask, &oldmask) < 0)
-		tst_brkm(TBROK | TERRNO, cleanup, "block SIGUSR1 error");
+		tst_brk(TBROK | TERRNO, "block SIGUSR1 error");
 
 	for (i = 0; i < N_ATTACH; i++) {
-		switch (pid = fork()) {
-		case -1:
-			tst_brkm(TBROK | TERRNO, cleanup, "fork");
+		switch (pid = SAFE_FORK()) {
 		case 0:
 			test = (stat_time == FIRST) ? set_shmat() : set_shared;
 
@@ -229,19 +197,19 @@ static void stat_setup(void)
 			 */
 			rval = sigsuspend(&oldmask);
 			if (rval != -1)
-				tst_brkm(TBROK | TERRNO, cleanup, "sigsuspend");
+				tst_brk(TBROK | TERRNO, "sigsuspend");
 
 			/*
 			 * don't have to block SIGUSR1 any more,
 			 * recover the mask
 			 */
 			if (sigprocmask(SIG_SETMASK, &oldmask, NULL) < 0)
-				tst_brkm(TBROK | TERRNO, cleanup,
+				tst_brk(TBROK | TERRNO,
 					 "child sigprocmask");
 
 			/* now we're back - detach the memory and exit */
 			if (shmdt(test) == -1)
-				tst_brkm(TBROK | TERRNO, cleanup,
+				tst_brk(TBROK | TERRNO,
 					 "shmdt in stat_setup()");
 			exit(0);
 		default:
@@ -252,7 +220,7 @@ static void stat_setup(void)
 
 	/* parent doesn't have to block SIGUSR1, recover the mask */
 	if (sigprocmask(SIG_SETMASK, &oldmask, NULL) < 0)
-		tst_brkm(TBROK, cleanup, "parent sigprocmask");
+		tst_brk(TBROK, "parent sigprocmask");
 
 	usleep(250000);
 }
@@ -270,12 +238,12 @@ static void func_stat(void)
 	pid = getpid();
 
 	if (buf.shm_cpid != pid) {
-		tst_resm(TFAIL, "creator pid is incorrect");
+		tst_res(TFAIL, "creator pid is incorrect");
 		goto fail;
 	}
 
 	if (buf.shm_segsz != shm_size) {
-		tst_resm(TFAIL, "segment size is incorrect");
+		tst_res(TFAIL, "segment size is incorrect");
 		goto fail;
 	}
 
@@ -286,18 +254,18 @@ static void func_stat(void)
 	 * that memory so the attaches equal N_ATTACH + stat_time (1).
 	 */
 	if (buf.shm_nattch != N_ATTACH + stat_time) {
-		tst_resm(TFAIL, "# of attaches is incorrect - %lu",
+		tst_res(TFAIL, "# of attaches is incorrect - %lu",
 			 (unsigned long)buf.shm_nattch);
 		goto fail;
 	}
 
 	/* use MODE_MASK to make sure we are comparing the last 9 bits */
 	if ((buf.shm_perm.mode & MODE_MASK) != ((SHM_RW) & MODE_MASK)) {
-		tst_resm(TFAIL, "segment mode is incorrect");
+		tst_res(TFAIL, "segment mode is incorrect");
 		goto fail;
 	}
 
-	tst_resm(TPASS, "pid, size, # of attaches and mode are correct "
+	tst_res(TPASS, "pid, size, # of attaches and mode are correct "
 		 "- pass #%d", stat_time);
 
 fail:
@@ -319,15 +287,21 @@ static void stat_cleanup(void)
 	/* wake up the childern so they can detach the memory and exit */
 	for (i = 0; i < N_ATTACH; i++)
 		if (kill(pid_arr[i], SIGUSR1) == -1)
-			tst_brkm(TBROK | TERRNO, cleanup, "kill with SIGUSR1");
+			tst_brk(TBROK | TERRNO, "kill with SIGUSR1");
 
 	/* remove the parent's shared memory the second time through */
 	if (stat_time == SECOND)
 		if (shmdt(set_shared) == -1)
-			tst_resm(TBROK | TERRNO, "shmdt in stat_cleanup()");
+			tst_res(TBROK | TERRNO, "shmdt in stat_cleanup()");
 	stat_time++;
 }
 
+static void sighandler(int sig)
+{
+	if (sig != SIGUSR1)
+		tst_res(TFAIL, "received unexpected signal %d", sig);
+}
+
 /*
  * set_setup() - set up for the IPC_SET command with shmctl()
  */
@@ -347,21 +321,21 @@ static void func_set(void)
 {
 	/* first stat the shared memory to get the new data */
 	if (shmctl(shm_id_1, IPC_STAT, &buf) == -1) {
-		tst_resm(TBROK | TERRNO, "shmctl in func_set()");
+		tst_res(TBROK | TERRNO, "shmctl in func_set()");
 		return;
 	}
 
 	if ((buf.shm_perm.mode & MODE_MASK) != ((SHM_RW | NEWMODE) & MODE_MASK)) {
-		tst_resm(TFAIL, "new mode is incorrect");
+		tst_res(TFAIL, "new mode is incorrect");
 		return;
 	}
 
 	if (save_time >= buf.shm_ctime) {
-		tst_resm(TFAIL, "change time is incorrect");
+		tst_res(TFAIL, "change time is incorrect");
 		return;
 	}
 
-	tst_resm(TPASS, "new mode and change time are correct");
+	tst_res(TPASS, "new mode and change time are correct");
 }
 
 /*
@@ -371,48 +345,47 @@ static void func_rmid(void)
 {
 	/* Do another shmctl() - we should get EINVAL */
 	if (shmctl(shm_id_1, IPC_STAT, &buf) != -1)
-		tst_brkm(TBROK, cleanup, "shmctl in func_rmid() "
+		tst_brk(TBROK, "shmctl in func_rmid() "
 			 "succeeded unexpectedly");
 	if (errno != EINVAL)
-		tst_resm(TFAIL | TERRNO, "shmctl in func_rmid() failed "
+		tst_res(TFAIL | TERRNO, "shmctl in func_rmid() failed "
 			 "unexpectedly - expect errno=EINVAL, got");
 	else
-		tst_resm(TPASS, "shmctl in func_rmid() failed as expected, "
+		tst_res(TPASS, "shmctl in func_rmid() failed as expected, "
 			 "shared memory appears to be removed");
 	shm_id_1 = -1;
 }
 
-static void sighandler(int sig)
-{
-	if (sig != SIGUSR1)
-		tst_resm(TFAIL, "received unexpected signal %d", sig);
-}
-
 void setup(void)
 {
 	long hpage_size;
 
-	tst_require_root();
 	check_hugepage();
-	tst_sig(FORK, sighandler, cleanup);
-	tst_tmpdir();
+	if (nr_opt)
+		hugepages = SAFE_STRTOL(nr_opt, 0, LONG_MAX);
 
 	orig_hugepages = get_sys_tune("nr_hugepages");
 	set_sys_tune("nr_hugepages", hugepages, 1);
-	hpage_size = read_meminfo("Hugepagesize:") * 1024;
+	hpage_size = SAFE_READ_MEMINFO("Hugepagesize:") * 1024;
 
 	shm_size = hpage_size * hugepages / 2;
 	update_shm_size(&shm_size);
-	shmkey = getipckey(cleanup);
-
-	TEST_PAUSE;
+	shmkey = getipckey();
 }
 
 void cleanup(void)
 {
 	rm_shm(shm_id_1);
-
 	set_sys_tune("nr_hugepages", orig_hugepages, 0);
-
-	tst_rmdir();
 }
+
+static struct tst_test test = {
+	.tid = "hugeshmctl01",
+	.needs_root = 1,
+	.forks_child = 1,
+	.needs_tmpdir = 1,
+	.options = options,
+	.setup = setup,
+	.cleanup = cleanup,
+	.test_all = test_hugeshmctl,
+};
diff --git a/testcases/kernel/mem/hugetlb/hugeshmctl/hugeshmctl02.c b/testcases/kernel/mem/hugetlb/hugeshmctl/hugeshmctl02.c
index f5ae366..488cff5 100644
--- a/testcases/kernel/mem/hugetlb/hugeshmctl/hugeshmctl02.c
+++ b/testcases/kernel/mem/hugetlb/hugeshmctl/hugeshmctl02.c
@@ -1,26 +1,19 @@
 /*
+ * Copyright (c) International Business Machines  Corp., 2004
+ * Copyright (c) Linux Test Project, 2004-2017
  *
- *   Copyright (c) International Business Machines  Corp., 2004
+ * 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 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
+ * 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.
  */
 
 /*
- * NAME
- *	hugeshmctl02.c
- *
  * DESCRIPTION
  *	hugeshmctl02 - check for EACCES, EFAULT and EINVAL errors
  *
@@ -35,32 +28,14 @@
  *	    issue a FAIL message
  *	call cleanup
  *
- * USAGE:  <for command-line>
- *  hugeshmctl02 [-c n] [-e] [-i n] [-I x] [-P x] [-t]
- *     where,  -c n : Run n copies concurrently.
- *             -e   : Turn on errno logging.
- *	       -i n : Execute test n times.
- *	       -I x : Execute test for x seconds.
- *	       -P x : Pause for x seconds between iterations.
- *	       -t   : Turn on syscall timing.
- *
  * HISTORY
  *	03/2001 - Written by Wayne Boyer
  *	04/2004 - Updated by Robbie Williamson
- *
- * RESTRICTIONS
- *	none
  */
 
-#include <pwd.h>
-#include "test.h"
-#include "safe_macros.h"
 #include "mem.h"
 #include "hugetlb.h"
 
-char *TCID = "hugeshmctl02";
-int TST_TOTAL = 4;
-
 static size_t shm_size;
 static int shm_id_1 = -1;
 static int shm_id_2 = -1;
@@ -68,101 +43,91 @@ static int shm_id_3 = -1;
 static struct shmid_ds buf;
 
 static long hugepages = 128;
-static option_t options[] = {
-	{"s:", &sflag, &nr_opt},
+
+static struct tst_option options[] = {
+	{"s:", &nr_opt, "-s   num  Set the number of the been allocated hugepages"},
 	{NULL, NULL, NULL}
 };
 
-struct test_case_t {
+struct tcase {
 	int *shmid;
 	int cmd;
 	struct shmid_ds *sbuf;
 	int error;
-} TC[] = {
+} tcases[] = {
 	/* EFAULT - IPC_SET & buf isn't valid */
-	{
-	&shm_id_2, IPC_SET, (struct shmid_ds *)-1, EFAULT},
-	    /* EFAULT - IPC_STAT & buf isn't valid */
-	{
-	&shm_id_2, IPC_STAT, (struct shmid_ds *)-1, EFAULT},
-	    /* EINVAL - the shmid is not valid */
-	{
-	&shm_id_3, IPC_STAT, &buf, EINVAL},
-	    /* EINVAL - the command is not valid */
-	{
-&shm_id_2, -1, &buf, EINVAL},};
-
-int main(int ac, char **av)
+	{&shm_id_2, IPC_SET, (struct shmid_ds *)-1, EFAULT},
+	/* EFAULT - IPC_STAT & buf isn't valid */
+	{&shm_id_2, IPC_STAT, (struct shmid_ds *)-1, EFAULT},
+	/* EINVAL - the shmid is not valid */
+	{&shm_id_3, IPC_STAT, &buf, EINVAL},
+	/* EINVAL - the command is not valid */
+	{&shm_id_2, -1, &buf, EINVAL},
+};
+
+static void test_hugeshmctl(void)
 {
-	int lc, i;
-
-	tst_parse_opts(ac, av, options, NULL);
-
-	if (sflag)
-		hugepages = SAFE_STRTOL(NULL, nr_opt, 0, LONG_MAX);
-
-	setup();
-
-	for (lc = 0; TEST_LOOPING(lc); lc++) {
-		tst_count = 0;
-
-		for (i = 0; i < TST_TOTAL; i++) {
-			TEST(shmctl(*(TC[i].shmid), TC[i].cmd, TC[i].sbuf));
-			if (TEST_RETURN != -1) {
-				tst_resm(TFAIL, "shmctl succeeded "
-					 "unexpectedly");
-				continue;
-			}
-			if (TEST_ERRNO == TC[i].error)
-				tst_resm(TPASS | TTERRNO, "shmctl failed "
-					 "as expected");
-			else
-				tst_resm(TFAIL | TTERRNO, "shmctl failed "
-					 "unexpectedly - expect errno = "
-					 "%d, got", TC[i].error);
+	unsigned int i;
+
+	for (i = 0; i < ARRAY_SIZE(tcases); i++) {
+		TEST(shmctl(*(tcases[i].shmid), tcases[i].cmd, tcases[i].sbuf));
+		if (TEST_RETURN != -1) {
+			tst_res(TFAIL, "shmctl succeeded "
+					"unexpectedly");
+			continue;
+		}
+		if (TEST_ERRNO == tcases[i].error) {
+			tst_res(TPASS | TTERRNO, "shmctl failed "
+					"as expected");
+		} else {
+			tst_res(TFAIL | TTERRNO, "shmctl failed "
+					"unexpectedly - expect errno = "
+					"%d, got", tcases[i].error);
 		}
 	}
-	cleanup();
-	tst_exit();
 }
 
-void setup(void)
+static void setup(void)
 {
 	long hpage_size;
 
-	tst_require_root();
 	check_hugepage();
-	tst_sig(NOFORK, DEF_HANDLER, cleanup);
-	tst_tmpdir();
+	if (nr_opt)
+		hugepages = SAFE_STRTOL(nr_opt, 0, LONG_MAX);
 
 	orig_hugepages = get_sys_tune("nr_hugepages");
 	set_sys_tune("nr_hugepages", hugepages, 1);
-	hpage_size = read_meminfo("Hugepagesize:") * 1024;
+	hpage_size = SAFE_READ_MEMINFO("Hugepagesize:") * 1024;
 
 	shm_size = hpage_size * hugepages / 2;
 	update_shm_size(&shm_size);
-	shmkey = getipckey(cleanup);
+	shmkey = getipckey();
 
 	/* create a shared memory segment without read or write permissions */
 	shm_id_1 = shmget(shmkey, shm_size, SHM_HUGETLB | IPC_CREAT | IPC_EXCL);
 	if (shm_id_1 == -1)
-		tst_brkm(TBROK | TERRNO, cleanup, "shmget #1");
+		tst_brk(TBROK | TERRNO, "shmget #1");
 
 	/* create a shared memory segment with read and write permissions */
 	shm_id_2 = shmget(shmkey + 1, shm_size,
 			  SHM_HUGETLB | IPC_CREAT | IPC_EXCL | SHM_RW);
 	if (shm_id_2 == -1)
-		tst_brkm(TBROK | TERRNO, cleanup, "shmget #2");
-
-	TEST_PAUSE;
+		tst_brk(TBROK | TERRNO, "shmget #2");
 }
 
-void cleanup(void)
+static void cleanup(void)
 {
 	rm_shm(shm_id_1);
 	rm_shm(shm_id_2);
-
 	set_sys_tune("nr_hugepages", orig_hugepages, 0);
-
-	tst_rmdir();
 }
+
+static struct tst_test test = {
+	.tid = "hugeshmctl02",
+	.needs_root = 1,
+	.needs_tmpdir = 1,
+	.options = options,
+	.setup = setup,
+	.cleanup = cleanup,
+	.test_all = test_hugeshmctl,
+};
diff --git a/testcases/kernel/mem/hugetlb/hugeshmctl/hugeshmctl03.c b/testcases/kernel/mem/hugetlb/hugeshmctl/hugeshmctl03.c
index bb2b7e7..e3abba1 100644
--- a/testcases/kernel/mem/hugetlb/hugeshmctl/hugeshmctl03.c
+++ b/testcases/kernel/mem/hugetlb/hugeshmctl/hugeshmctl03.c
@@ -1,26 +1,19 @@
 /*
+ * Copyright (c) International Business Machines  Corp., 2004
+ * Copyright (c) Linux Test Project, 2004-2017
  *
- *   Copyright (c) International Business Machines  Corp., 2004
+ * 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 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
+ * 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.
  */
 
 /*
- * NAME
- *	hugeshmctl03.c
- *
  * DESCRIPTION
  *	hugeshmctl03 - check for EACCES, and EPERM errors
  *
@@ -41,15 +34,6 @@
  *	  wait for child to exit
  *	  remove the large shared memory segment
  *
- * USAGE:  <for command-line>
- *  hugeshmctl03 [-c n] [-e] [-i n] [-I x] [-P x] [-t]
- *     where,  -c n : Run n copies concurrently.
- *             -e   : Turn on errno logging.
- *	       -i n : Execute test n times.
- *	       -I x : Execute test for x seconds.
- *	       -P x : Pause for x seconds between iterations.
- *	       -t   : Turn on syscall timing.
- *
  * HISTORY
  *	03/2001 - Written by Wayne Boyer
  *	04/2004 - Updated by Robbie Williamson
@@ -58,16 +42,9 @@
  *	test must be run as root
  */
 
-#include <sys/types.h>
-#include <sys/wait.h>
-#include "test.h"
-#include "safe_macros.h"
 #include "mem.h"
 #include "hugetlb.h"
 
-char *TCID = "hugeshmctl03";
-int TST_TOTAL = 3;
-
 static size_t shm_size;
 static int shm_id_1 = -1;
 static struct shmid_ds buf;
@@ -75,80 +52,64 @@ static uid_t ltp_uid;
 static char *ltp_user = "nobody";
 
 static long hugepages = 128;
-static option_t options[] = {
-	{"s:", &sflag, &nr_opt},
+
+static struct tst_option options[] = {
+	{"s:", &nr_opt, "-s   num  Set the number of the been allocated hugepages"},
 	{NULL, NULL, NULL}
 };
 
-struct test_case_t {
+struct tcase {
 	int *shmid;
 	int cmd;
 	struct shmid_ds *sbuf;
 	int error;
-} TC[] = {
+} tcases[] = {
 	/* EACCES - child has no read permission for segment */
-	{
-	&shm_id_1, IPC_STAT, &buf, EACCES},
-	    /* EPERM - IPC_SET - child doesn't have permission to change segment */
-	{
-	&shm_id_1, IPC_SET, &buf, EPERM},
-	    /* EPERM - IPC_RMID - child can not remove the segment */
-	{
-&shm_id_1, IPC_RMID, &buf, EPERM},};
+	{&shm_id_1, IPC_STAT, &buf, EACCES},
+	/* EPERM - IPC_SET - child doesn't have permission to change segment */
+	{&shm_id_1, IPC_SET, &buf, EPERM},
+	/* EPERM - IPC_RMID - child can not remove the segment */
+	{&shm_id_1, IPC_RMID, &buf, EPERM},
+};
 
 static void do_child(void);
 
-int main(int ac, char **av)
+static void test_hugeshmctl(void)
 {
 	pid_t pid;
 	int status;
 
-	tst_parse_opts(ac, av, options, NULL);
-
-	if (sflag)
-		hugepages = SAFE_STRTOL(NULL, nr_opt, 0, LONG_MAX);
-
-	setup();
-
-	switch (pid = fork()) {
-	case -1:
-		tst_brkm(TBROK | TERRNO, cleanup, "fork");
+	switch (pid = SAFE_FORK()) {
 	case 0:
-		/* set  the user ID of the child to the non root user */
+		/* set the user ID of the child to the non root user */
 		if (setuid(ltp_uid) == -1)
-			tst_brkm(TBROK | TERRNO, cleanup, "setuid");
+			tst_brk(TBROK | TERRNO, "setuid");
 		do_child();
-		tst_exit();
+		exit(0);
 	default:
 		if (waitpid(pid, &status, 0) == -1)
-			tst_brkm(TBROK | TERRNO, cleanup, "waitpid");
+			tst_brk(TBROK | TERRNO, "waitpid");
 	}
-	cleanup();
-	tst_exit();
 }
 
 static void do_child(void)
 {
-	int i, lc;
-
-	for (lc = 0; TEST_LOOPING(lc); lc++) {
-		tst_count = 0;
-
-		for (i = 0; i < TST_TOTAL; i++) {
-			TEST(shmctl(*(TC[i].shmid), TC[i].cmd, TC[i].sbuf));
-			if (TEST_RETURN != -1) {
-				tst_resm(TFAIL, "shmctl succeeded "
-					 "unexpectedly");
-				continue;
-			}
-			if (TEST_ERRNO == TC[i].error)
-				tst_resm(TPASS | TTERRNO, "shmctl failed "
-					 "as expected");
-			else
-				tst_resm(TFAIL | TTERRNO, "shmctl failed "
-					 "unexpectedly - expect errno = "
-					 "%d, got", TC[i].error);
+	unsigned int i;
+
+	for (i = 0; i < ARRAY_SIZE(tcases); i++) {
+		TEST(shmctl(*(tcases[i].shmid), tcases[i].cmd, tcases[i].sbuf));
+		if (TEST_RETURN != -1) {
+			tst_res(TFAIL, "shmctl succeeded "
+					"unexpectedly");
+			continue;
 		}
+		if (TEST_ERRNO == tcases[i].error)
+			tst_res(TPASS | TTERRNO, "shmctl failed "
+					"as expected");
+		else
+			tst_res(TFAIL | TTERRNO, "shmctl failed "
+					"unexpectedly - expect errno = "
+					"%d, got", tcases[i].error);
 	}
 }
 
@@ -156,34 +117,39 @@ void setup(void)
 {
 	long hpage_size;
 
-	tst_require_root();
 	check_hugepage();
-	tst_sig(FORK, DEF_HANDLER, cleanup);
-	tst_tmpdir();
+	if (nr_opt)
+		hugepages = SAFE_STRTOL(nr_opt, 0, LONG_MAX);
 
 	orig_hugepages = get_sys_tune("nr_hugepages");
 	set_sys_tune("nr_hugepages", hugepages, 1);
-	hpage_size = read_meminfo("Hugepagesize:") * 1024;
+	hpage_size = SAFE_READ_MEMINFO("Hugepagesize:") * 1024;
 
 	shm_size = hpage_size * hugepages / 2;
 	update_shm_size(&shm_size);
-	shmkey = getipckey(cleanup);
+	shmkey = getipckey();
 	shm_id_1 = shmget(shmkey, shm_size,
 			  SHM_HUGETLB | IPC_CREAT | IPC_EXCL | SHM_RW);
 	if (shm_id_1 == -1)
-		tst_brkm(TBROK | TERRNO, cleanup, "shmget");
+		tst_brk(TBROK | TERRNO, "shmget");
 
 	/* get the userid for a non root user */
-	ltp_uid = getuserid(cleanup, ltp_user);
-
-	TEST_PAUSE;
+	ltp_uid = getuserid(ltp_user);
 }
 
 void cleanup(void)
 {
 	rm_shm(shm_id_1);
-
 	set_sys_tune("nr_hugepages", orig_hugepages, 0);
-
-	tst_rmdir();
 }
+
+static struct tst_test test = {
+	.tid = "hugeshmctl03",
+	.needs_root = 1,
+	.forks_child = 1,
+	.needs_tmpdir = 1,
+	.options = options,
+	.setup = setup,
+	.cleanup = cleanup,
+	.test_all = test_hugeshmctl,
+};
-- 
2.9.3


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

* [LTP] [PATCH V2 10/14] mm/hugeshmdt: convert to new API
  2017-04-10 10:05 [LTP] [PATCH V2 00/14] Convert LTP kernel/mem/testcase to New API Li Wang
                   ` (8 preceding siblings ...)
  2017-04-10 10:05 ` [LTP] [PATCH V2 09/14] mem/hugeshmctl: " Li Wang
@ 2017-04-10 10:05 ` Li Wang
  2017-04-10 10:05 ` [LTP] [PATCH V2 11/14] mem/hugeshmget: " Li Wang
                   ` (3 subsequent siblings)
  13 siblings, 0 replies; 25+ messages in thread
From: Li Wang @ 2017-04-10 10:05 UTC (permalink / raw
  To: ltp

Signed-off-by: Li Wang <liwang@redhat.com>
---
 .../kernel/mem/hugetlb/hugeshmdt/hugeshmdt01.c     | 124 ++++++++-------------
 1 file changed, 47 insertions(+), 77 deletions(-)

diff --git a/testcases/kernel/mem/hugetlb/hugeshmdt/hugeshmdt01.c b/testcases/kernel/mem/hugetlb/hugeshmdt/hugeshmdt01.c
index 0ebcca6..dc16b9c 100644
--- a/testcases/kernel/mem/hugetlb/hugeshmdt/hugeshmdt01.c
+++ b/testcases/kernel/mem/hugetlb/hugeshmdt/hugeshmdt01.c
@@ -1,26 +1,19 @@
 /*
+ * Copyright (c) International Business Machines  Corp., 2004
+ * Copyright (c) Linux Test Project, 2004-2017
  *
- *   Copyright (c) International Business Machines  Corp., 2004
+ * 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 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
+ * 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.
  */
 
 /*
- * NAME
- *	hugeshmdt01.c
- *
  * DESCRIPTION
  *	hugeshmdt01 - check that largr shared memory is detached correctly
  *
@@ -41,29 +34,14 @@
  *			issue a FAIL message
  *	call cleanup
  *
- * USAGE:  <for command-line>
- *  hugeshmdt01 [-c n] [-f] [-i n] [-I x] [-P x] [-t]
- *     where,  -c n : Run n copies concurrently.
- *             -f   : Turn off functionality Testing.
- *	       -I x : Execute test for x seconds.
- *	       -P x : Pause for x seconds between iterations.
- *	       -t   : Turn on syscall timing.
- *
  * HISTORY
  *	03/2001 - Written by Wayne Boyer
  *	04/2004 - Updated by Robbie Williamson
- *
- * RESTRICTIONS
- *	none
  */
 
 #include <setjmp.h>
-#include "hugetlb.h"
-#include "safe_macros.h"
 #include "mem.h"
-
-char *TCID = "hugeshmdt01";
-int TST_TOTAL = 1;
+#include "hugetlb.h"
 
 static size_t shm_size;
 static int shm_id_1 = -1;
@@ -73,54 +51,43 @@ static int pass;
 static sigjmp_buf env;
 
 static long hugepages = 128;
-static option_t options[] = {
-	{"s:", &sflag, &nr_opt},
+static struct tst_option options[] = {
+	{"s:", &nr_opt, "-s   num  Set the number of the been allocated hugepages"},
 	{NULL, NULL, NULL}
 };
 
 static void check_functionality(void);
 static void sighandler(int sig);
 
-int main(int ac, char **av)
+static void hugeshmdt_test(void)
 {
-	int lc;
-
-	tst_parse_opts(ac, av, options, NULL);
-
-	if (sflag)
-		hugepages = SAFE_STRTOL(NULL, nr_opt, 0, LONG_MAX);
+	struct sigaction sa;
 
-	setup();
+	sa.sa_handler = sighandler;
+	sigaction(SIGSEGV, &sa, NULL);
 
-	for (lc = 0; TEST_LOOPING(lc); lc++) {
-		tst_count = 0;
-
-		if (shmdt(shared) == -1) {
-			tst_resm(TFAIL | TERRNO, "shmdt");
-		} else {
-			check_functionality();
-		}
+	if (shmdt(shared) == -1)
+		tst_res(TFAIL | TERRNO, "shmdt");
+	else
+		check_functionality();
 
-		/* reattach the shared memory segment in case we are looping */
-		shared = shmat(shm_id_1, 0, 0);
-		if (shared == (void *)-1)
-			tst_brkm(TBROK | TERRNO, cleanup, "shmat #2: reattach");
+	/* reattach the shared memory segment in case we are looping */
+	shared = shmat(shm_id_1, 0, 0);
+	if (shared == (void *)-1)
+		tst_brk(TBROK | TERRNO, "shmat #2: reattach");
 
-		/* also reset pass */
-		pass = 0;
-	}
-	cleanup();
-	tst_exit();
+	/* also reset pass */
+	pass = 0;
 }
 
 static void check_functionality(void)
 {
 	/* stat the shared memory segment */
 	if (shmctl(shm_id_1, IPC_STAT, &buf) == -1)
-		tst_brkm(TBROK | TERRNO, cleanup, "shmctl");
+		tst_brk(TBROK | TERRNO, "shmctl");
 
 	if (buf.shm_nattch != 0) {
-		tst_resm(TFAIL, "# of attaches is incorrect");
+		tst_res(TFAIL, "# of attaches is incorrect");
 		return;
 	}
 
@@ -142,9 +109,9 @@ static void check_functionality(void)
 		*shared = 2;
 
 	if (pass)
-		tst_resm(TPASS, "huge shared memory detached correctly");
+		tst_res(TPASS, "huge shared memory detached correctly");
 	else
-		tst_resm(TFAIL, "huge shared memory was not detached "
+		tst_res(TFAIL, "huge shared memory was not detached "
 			 "correctly");
 }
 
@@ -156,7 +123,7 @@ static void sighandler(int sig)
 		pass = 1;
 		siglongjmp(env, 1);
 	} else {
-		tst_brkm(TBROK, cleanup, "unexpected signal received: %d", sig);
+		tst_brk(TBROK, "unexpected signal received: %d", sig);
 	}
 }
 
@@ -164,41 +131,44 @@ void setup(void)
 {
 	long hpage_size;
 
-	tst_require_root();
 	check_hugepage();
-	tst_sig(NOFORK, sighandler, cleanup);
-	tst_tmpdir();
+	if (nr_opt)
+		hugepages = SAFE_STRTOL(nr_opt, 0, LONG_MAX);
 
 	orig_hugepages = get_sys_tune("nr_hugepages");
 	set_sys_tune("nr_hugepages", hugepages, 1);
-	hpage_size = read_meminfo("Hugepagesize:") * 1024;
+	hpage_size = SAFE_READ_MEMINFO("Hugepagesize:") * 1024;
 
 	shm_size = hpage_size * hugepages / 2;
 	update_shm_size(&shm_size);
-	shmkey = getipckey(cleanup);
+	shmkey = getipckey();
 
 	/* create a shared memory resource with read and write permissions */
 	shm_id_1 = shmget(shmkey, shm_size,
 			  SHM_HUGETLB | SHM_RW | IPC_CREAT | IPC_EXCL);
 	if (shm_id_1 == -1)
-		tst_brkm(TBROK | TERRNO, cleanup, "shmget");
+		tst_brk(TBROK | TERRNO, "shmget");
 
 	/* attach the shared memory segment */
 	shared = shmat(shm_id_1, 0, 0);
 	if (shared == (void *)-1)
-		tst_brkm(TBROK | TERRNO, cleanup, "shmat #1");
+		tst_brk(TBROK | TERRNO, "shmat #1");
 
 	/* give a value to the shared memory integer */
 	*shared = 4;
-
-	TEST_PAUSE;
 }
 
 void cleanup(void)
 {
 	rm_shm(shm_id_1);
-
 	set_sys_tune("nr_hugepages", orig_hugepages, 0);
-
-	tst_rmdir();
 }
+
+static struct tst_test test = {
+	.tid = "hugeshmdt01",
+	.needs_root = 1,
+	.options = options,
+	.setup = setup,
+	.cleanup = cleanup,
+	.test_all = hugeshmdt_test,
+};
-- 
2.9.3


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

* [LTP] [PATCH V2 11/14] mem/hugeshmget: convert to new API
  2017-04-10 10:05 [LTP] [PATCH V2 00/14] Convert LTP kernel/mem/testcase to New API Li Wang
                   ` (9 preceding siblings ...)
  2017-04-10 10:05 ` [LTP] [PATCH V2 10/14] mm/hugeshmdt: " Li Wang
@ 2017-04-10 10:05 ` Li Wang
  2017-04-10 10:05 ` [LTP] [PATCH V2 12/14] mem/cpuset: " Li Wang
                   ` (2 subsequent siblings)
  13 siblings, 0 replies; 25+ messages in thread
From: Li Wang @ 2017-04-10 10:05 UTC (permalink / raw
  To: ltp

Signed-off-by: Li Wang <liwang@redhat.com>
---
 .../kernel/mem/hugetlb/hugeshmget/hugeshmget01.c   | 168 ++++++++-------------
 .../kernel/mem/hugetlb/hugeshmget/hugeshmget02.c   | 155 ++++++++-----------
 .../kernel/mem/hugetlb/hugeshmget/hugeshmget03.c   | 118 ++++++---------
 .../kernel/mem/hugetlb/hugeshmget/hugeshmget05.c   | 121 ++++++---------
 4 files changed, 206 insertions(+), 356 deletions(-)

diff --git a/testcases/kernel/mem/hugetlb/hugeshmget/hugeshmget01.c b/testcases/kernel/mem/hugetlb/hugeshmget/hugeshmget01.c
index 2367c4d..d1a3dea 100644
--- a/testcases/kernel/mem/hugetlb/hugeshmget/hugeshmget01.c
+++ b/testcases/kernel/mem/hugetlb/hugeshmget/hugeshmget01.c
@@ -1,26 +1,19 @@
 /*
+ * Copyright (c) International Business Machines  Corp., 2004
+ * Copyright (c) Linux Test Project, 2004-2017
  *
- *   Copyright (c) International Business Machines  Corp., 2004
+ * 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 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
+ * 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.
  */
 
 /*
- * NAME
- *	hugeshmget01.c
- *
  * DESCRIPTION
  *	hugeshmget01 - test that shmget() correctly creates a large
  *			shared memory segment
@@ -41,128 +34,93 @@
  *	  else issue a PASS message
  *	call cleanup
  *
- * USAGE:  <for command-line>
- *  shmget01 [-c n] [-f] [-i n] [-I x] [-P x] [-t]
- *     where,  -c n : Run n copies concurrently.
- *             -f   : Turn off functionality Testing.
- *	       -i n : Execute test n times.
- *	       -I x : Execute test for x seconds.
- *	       -P x : Pause for x seconds between iterations.
- *	       -t   : Turn on syscall timing.
- *
  * HISTORY
  *	03/2001 - Written by Wayne Boyer
  *	04/2004 - Updated by Robbie Williamson
- *
- * RESTRICTIONS
- *	none
  */
 
-#include "hugetlb.h"
-#include "safe_macros.h"
 #include "mem.h"
-
-char *TCID = "hugeshmget01";
-int TST_TOTAL = 1;
+#include "hugetlb.h"
 
 static size_t shm_size;
 static int shm_id_1 = -1;
 
 static long hugepages = 128;
-static option_t options[] = {
-	{"s:", &sflag, &nr_opt},
+static struct tst_option options[] = {
+	{"s:", &nr_opt, "-s   num  Set the number of the been allocated hugepages"},
 	{NULL, NULL, NULL}
 };
 
-int main(int ac, char **av)
+static void test_hugeshmget(void)
 {
-	int lc;
 	struct shmid_ds buf;
 
-	tst_parse_opts(ac, av, options, NULL);
-
-	if (sflag)
-		hugepages = SAFE_STRTOL(NULL, nr_opt, 0, LONG_MAX);
-
-	setup();
-
-	for (lc = 0; TEST_LOOPING(lc); lc++) {
-		tst_count = 0;
-
-		shm_id_1 = shmget(shmkey, shm_size,
-				  SHM_HUGETLB | IPC_CREAT | IPC_EXCL | SHM_RW);
-		if (shm_id_1 == -1) {
-			tst_resm(TFAIL | TERRNO, "shmget");
-		} else {
-			/* do a STAT and check some info */
-			if (shmctl(shm_id_1, IPC_STAT, &buf) == -1) {
-				tst_resm(TBROK | TERRNO,
-					 "shmctl(IPC_STAT)");
-				continue;
-			}
-			/* check the seqment size */
-			if (buf.shm_segsz != shm_size) {
-				tst_resm(TFAIL, "seqment size is not "
-					 "correct");
-				continue;
-			}
-			/* check the pid of the creator */
-			if (buf.shm_cpid != getpid()) {
-				tst_resm(TFAIL, "creator pid is not "
-					 "correct");
-				continue;
-			}
-			/*
-			 * check the mode of the seqment
-			 * mask out all but the lower 9 bits
-			 */
-			if ((buf.shm_perm.mode & MODE_MASK) !=
-			    ((SHM_RW) & MODE_MASK)) {
-				tst_resm(TFAIL, "segment mode is not "
-					 "correct");
-				continue;
-			}
-			/* if we get here, everything looks good */
-			tst_resm(TPASS, "size, pid & mode are correct");
-		}
-
-		/*
-		 * clean up things in case we are looping
-		 */
-		if (shmctl(shm_id_1, IPC_RMID, NULL) == -1)
-			tst_resm(TBROK | TERRNO, "shmctl(IPC_RMID)");
-		else
-			shm_id_1 = -1;
+	/* do a STAT and check some info */
+	if (shmctl(shm_id_1, IPC_STAT, &buf) == -1) {
+		tst_res(TFAIL | TERRNO,
+				"shmctl(IPC_STAT)");
+		return;
+	}
+	/* check the seqment size */
+	if (buf.shm_segsz != shm_size) {
+		tst_res(TFAIL, "seqment size is not "
+				"correct");
+		return;
+	}
+	/* check the pid of the creator */
+	if (buf.shm_cpid != getpid()) {
+		tst_res(TFAIL, "creator pid is not "
+				"correct");
+		return;
+	}
+	/*
+	 * check the mode of the seqment
+	 * mask out all but the lower 9 bits
+	 */
+	if ((buf.shm_perm.mode & MODE_MASK) !=
+			((SHM_RW) & MODE_MASK)) {
+		tst_res(TFAIL, "segment mode is not "
+				"correct");
+		return;
 	}
-	cleanup();
-	tst_exit();
+
+	/* if we get here, everything looks good */
+	tst_res(TPASS, "size, pid & mode are correct");
 }
 
-void setup(void)
+static void setup(void)
 {
 	long hpage_size;
 
-	tst_require_root();
 	check_hugepage();
-	tst_sig(NOFORK, DEF_HANDLER, cleanup);
-	tst_tmpdir();
+	if (nr_opt)
+		hugepages = SAFE_STRTOL(nr_opt, 0, LONG_MAX);
 
 	orig_hugepages = get_sys_tune("nr_hugepages");
 	set_sys_tune("nr_hugepages", hugepages, 1);
-	hpage_size = read_meminfo("Hugepagesize:") * 1024;
+	hpage_size = SAFE_READ_MEMINFO("Hugepagesize:") * 1024;
 
 	shm_size = hpage_size * hugepages / 2;
 	update_shm_size(&shm_size);
-	shmkey = getipckey(cleanup);
+	shmkey = getipckey();
 
-	TEST_PAUSE;
+	shm_id_1 = shmget(shmkey, shm_size,
+			SHM_HUGETLB | IPC_CREAT | IPC_EXCL | SHM_RW);
+	if (shm_id_1 == -1)
+		tst_brk(TBROK | TERRNO, "shmget");
 }
 
-void cleanup(void)
+static void cleanup(void)
 {
 	rm_shm(shm_id_1);
-
 	set_sys_tune("nr_hugepages", orig_hugepages, 0);
-
-	tst_rmdir();
 }
+
+static struct tst_test test = {
+	.tid = "hugeshmget01",
+	.needs_root = 1,
+	.options = options,
+	.setup = setup,
+	.cleanup = cleanup,
+	.test_all = test_hugeshmget,
+};
diff --git a/testcases/kernel/mem/hugetlb/hugeshmget/hugeshmget02.c b/testcases/kernel/mem/hugetlb/hugeshmget/hugeshmget02.c
index 5f2cb31..6296b91 100644
--- a/testcases/kernel/mem/hugetlb/hugeshmget/hugeshmget02.c
+++ b/testcases/kernel/mem/hugetlb/hugeshmget/hugeshmget02.c
@@ -1,26 +1,19 @@
 /*
+ * Copyright (c) International Business Machines  Corp., 2004
+ * Copyright (c) Linux Test Project, 2004-2017
  *
- *   Copyright (c) International Business Machines  Corp., 2004
+ * 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 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
+ * 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.
  */
 
 /*
- * NAME
- *	hugeshmget02.c
- *
  * DESCRIPTION
  *	hugeshmget02 - check for ENOENT, EEXIST and EINVAL errors
  *
@@ -34,29 +27,13 @@
  *	    issue a FAIL message
  *	call cleanup
  *
- * USAGE:  <for command-line>
- *  hugeshmget02 [-c n] [-e] [-i n] [-I x] [-P x] [-t]
- *     where,  -c n : Run n copies concurrently.
- *             -e   : Turn on errno logging.
- *	       -i n : Execute test n times.
- *	       -I x : Execute test for x seconds.
- *	       -P x : Pause for x seconds between iterations.
- *	       -t   : Turn on syscall timing.
- *
  * HISTORY
  *	03/2001 - Written by Wayne Boyer
  *	04/2004 - Updated by Robbie Williamson
- *
- * RESTRICTIONS
- *	none
  */
 
-#include "hugetlb.h"
-#include "safe_macros.h"
 #include "mem.h"
-
-char *TCID = "hugeshmget02";
-int TST_TOTAL = 4;
+#include "hugetlb.h"
 
 static size_t shm_size;
 static int shm_id_1 = -1;
@@ -64,105 +41,91 @@ static int shm_nonexistent_key = -1;
 static key_t shmkey2;
 
 static long hugepages = 128;
-static option_t options[] = {
-	{"s:", &sflag, &nr_opt},
+static struct tst_option options[] = {
+	{"s:", &nr_opt, "-s   num  Set the number of the been allocated hugepages"},
 	{NULL, NULL, NULL}
 };
 
-struct test_case_t {
+static struct tcase {
 	int *skey;
 	int size_coe;
 	int flags;
 	int error;
-} TC[] = {
+} tcases[] = {
 	/* EINVAL - size is 0 */
-	{
-	&shmkey2, 0, SHM_HUGETLB | IPC_CREAT | IPC_EXCL | SHM_RW, EINVAL},
-	    /* EINVAL - size is larger than created segment */
-	{
-	&shmkey, 2, SHM_HUGETLB | SHM_RW, EINVAL},
-	    /* EEXIST - the segment exists and IPC_CREAT | IPC_EXCL is given */
-	{
-	&shmkey, 1, SHM_HUGETLB | IPC_CREAT | IPC_EXCL | SHM_RW, EEXIST},
-	    /* ENOENT - no segment exists for the key and IPC_CREAT is not given */
-	    /* use shm_nonexistend_key (-1) as the key */
-	{
-	&shm_nonexistent_key, 1, SHM_HUGETLB | SHM_RW, ENOENT}
+	{&shmkey2, 0, SHM_HUGETLB | IPC_CREAT | IPC_EXCL | SHM_RW, EINVAL},
+	/* EINVAL - size is larger than created segment */
+	{&shmkey, 2, SHM_HUGETLB | SHM_RW, EINVAL},
+	/* EEXIST - the segment exists and IPC_CREAT | IPC_EXCL is given */
+	{&shmkey, 1, SHM_HUGETLB | IPC_CREAT | IPC_EXCL | SHM_RW, EEXIST},
+	/* ENOENT - no segment exists for the key and IPC_CREAT is not given */
+	/* use shm_nonexistend_key (-1) as the key */
+	{&shm_nonexistent_key, 1, SHM_HUGETLB | SHM_RW, ENOENT}
 };
 
-int main(int ac, char **av)
+static void test_hugeshmget(void)
 {
-	int lc, i;
+	unsigned int i;
 	int shm_id_2 = -1;
 
-	tst_parse_opts(ac, av, options, NULL);
-
-	if (sflag)
-		hugepages = SAFE_STRTOL(NULL, nr_opt, 0, LONG_MAX);
-
-	setup();
-
-	for (lc = 0; TEST_LOOPING(lc); lc++) {
-		tst_count = 0;
-
-		for (i = 0; i < TST_TOTAL; i++) {
-			/* If this key is existent, just remove it */
-			if (*TC[i].skey == -1) {
-				shm_id_2 = shmget(*(TC[i].skey), 0, 0);
-				if (shm_id_2 != -1)
-					shmctl(shm_id_2, IPC_RMID, NULL);
-			}
+	for (i = 0; i < ARRAY_SIZE(tcases); i++) {
+		/* If this key is existent, just remove it */
+		if (*tcases[i].skey == -1) {
+			shm_id_2 = shmget(*(tcases[i].skey), 0, 0);
+			if (shm_id_2 != -1)
+				shmctl(shm_id_2, IPC_RMID, NULL);
+		}
 
-			TEST(shmget(*(TC[i].skey), TC[i].size_coe * shm_size,
-				    TC[i].flags));
-			if (TEST_RETURN != -1) {
-				tst_resm(TFAIL, "shmget succeeded "
-					 "unexpectedly");
-				continue;
-			}
-			if (TEST_ERRNO == TC[i].error)
-				tst_resm(TPASS | TTERRNO, "shmget failed "
-					 "as expected");
-			else
-				tst_resm(TFAIL | TTERRNO, "shmget failed "
-					 "unexpectedly - expect errno=%d, "
-					 "got", TC[i].error);
+		TEST(shmget(*(tcases[i].skey), tcases[i].size_coe * shm_size,
+					tcases[i].flags));
+		if (TEST_RETURN != -1) {
+			tst_res(TFAIL, "shmget succeeded "
+					"unexpectedly");
+			continue;
 		}
+		if (TEST_ERRNO == tcases[i].error)
+			tst_res(TPASS | TTERRNO, "shmget failed "
+					"as expected");
+		else
+			tst_res(TFAIL | TTERRNO, "shmget failed "
+					"unexpectedly - expect errno=%d, "
+					"got", tcases[i].error);
 	}
-	cleanup();
-	tst_exit();
 }
 
 void setup(void)
 {
 	long hpage_size;
 
-	tst_require_root();
 	check_hugepage();
-	tst_sig(NOFORK, DEF_HANDLER, cleanup);
-	tst_tmpdir();
+	if (nr_opt)
+		hugepages = SAFE_STRTOL(nr_opt, 0, LONG_MAX);
 
 	orig_hugepages = get_sys_tune("nr_hugepages");
 	set_sys_tune("nr_hugepages", hugepages, 1);
-	hpage_size = read_meminfo("Hugepagesize:") * 1024;
+	hpage_size = SAFE_READ_MEMINFO("Hugepagesize:") * 1024;
 
 	shm_size = hpage_size * hugepages / 2;
 	update_shm_size(&shm_size);
 
-	shmkey = getipckey(cleanup);
+	shmkey = getipckey();
 	shmkey2 = shmkey + 1;
 	shm_id_1 = shmget(shmkey, shm_size, IPC_CREAT | IPC_EXCL | SHM_RW);
 	if (shm_id_1 == -1)
-		tst_brkm(TBROK | TERRNO, cleanup, "shmget #setup");
-
-	TEST_PAUSE;
+		tst_brk(TBROK | TERRNO, "shmget #setup");
 }
 
 void cleanup(void)
 {
 	rm_shm(shm_id_1);
-
 	set_sys_tune("nr_hugepages", orig_hugepages, 0);
-
-	tst_rmdir();
 }
+
+static struct tst_test test = {
+	.tid = "hugeshmget02",
+	.needs_root = 1,
+	.options = options,
+	.setup = setup,
+	.cleanup = cleanup,
+	.test_all = test_hugeshmget,
+};
diff --git a/testcases/kernel/mem/hugetlb/hugeshmget/hugeshmget03.c b/testcases/kernel/mem/hugetlb/hugeshmget/hugeshmget03.c
index 402313a..00a895e 100644
--- a/testcases/kernel/mem/hugetlb/hugeshmget/hugeshmget03.c
+++ b/testcases/kernel/mem/hugetlb/hugeshmget/hugeshmget03.c
@@ -1,26 +1,19 @@
 /*
+ * Copyright (c) International Business Machines  Corp., 2004
+ * Copyright (c) Linux Test Project, 2004-2017
  *
- *   Copyright (c) International Business Machines  Corp., 2004
+ * 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 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
+ * 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.
  */
 
 /*
- * NAME
- *	hugeshmget03.c
- *
  * DESCRIPTION
  *	hugeshmget03 - test for ENOSPC error
  *
@@ -35,29 +28,13 @@
  *	    issue a FAIL message
  *	call cleanup
  *
- * USAGE:  <for command-line>
- *  hugeshmget03 [-c n] [-e] [-i n] [-I x] [-P x] [-t]
- *     where,  -c n : Run n copies concurrently.
- *             -e   : Turn on errno logging.
- *	       -i n : Execute test n times.
- *	       -I x : Execute test for x seconds.
- *	       -P x : Pause for x seconds between iterations.
- *	       -t   : Turn on syscall timing.
- *
  * HISTORY
  *	03/2001 - Written by Wayne Boyer
  *	04/2004 - Updated by Robbie Williamson
- *
- * RESTRICTIONS
- *	none
  */
 
-#include "hugetlb.h"
-#include "safe_macros.h"
 #include "mem.h"
-
-char *TCID = "hugeshmget03";
-int TST_TOTAL = 1;
+#include "hugetlb.h"
 
 /*
  * The MAXIDS value is somewhat arbitrary and may need to be increased
@@ -73,57 +50,41 @@ static int shm_id_arr[MAXIDS];
 
 static long hugepages = 128;
 static long orig_shmmni;
-static option_t options[] = {
-	{"s:", &sflag, &nr_opt},
+static struct tst_option options[] = {
+	{"s:", &nr_opt, "-s   num  Set the number of the been allocated hugepages"},
 	{NULL, NULL, NULL}
 };
 
-int main(int ac, char **av)
+static void test_hugeshmget(void)
 {
-	int lc;
-
-	tst_parse_opts(ac, av, options, NULL);
-
-	if (sflag)
-		hugepages = SAFE_STRTOL(NULL, nr_opt, 0, LONG_MAX);
-
-	setup();
-
-	for (lc = 0; TEST_LOOPING(lc); lc++) {
-		tst_count = 0;
-
-		TEST(shmget(IPC_PRIVATE, shm_size,
-			    SHM_HUGETLB | IPC_CREAT | IPC_EXCL | SHM_RW));
-		if (TEST_RETURN != -1) {
-			tst_resm(TFAIL, "shmget succeeded unexpectedly");
-			continue;
-		}
-		if (TEST_ERRNO == ENOSPC)
-			tst_resm(TPASS | TTERRNO, "shmget failed as expected");
-		else
-			tst_resm(TFAIL | TTERRNO, "shmget failed unexpectedly "
-				 "- expect errno=ENOSPC, got");
+	TEST(shmget(IPC_PRIVATE, shm_size,
+				SHM_HUGETLB | IPC_CREAT | IPC_EXCL | SHM_RW));
+	if (TEST_RETURN != -1) {
+		tst_res(TFAIL, "shmget succeeded unexpectedly");
+		return;
 	}
-	cleanup();
-	tst_exit();
+	if (TEST_ERRNO == ENOSPC)
+		tst_res(TPASS | TTERRNO, "shmget failed as expected");
+	else
+		tst_res(TFAIL | TTERRNO, "shmget failed unexpectedly "
+				"- expect errno=ENOSPC, got");
 }
 
-void setup(void)
+static void setup(void)
 {
 	long hpage_size;
 
-	tst_require_root();
 	check_hugepage();
-	tst_sig(NOFORK, DEF_HANDLER, cleanup);
-	tst_tmpdir();
+	if (nr_opt)
+		hugepages = SAFE_STRTOL(nr_opt, 0, LONG_MAX);
 
 	orig_hugepages = get_sys_tune("nr_hugepages");
-	SAFE_FILE_SCANF(NULL, PATH_SHMMNI, "%ld", &orig_shmmni);
+	SAFE_FILE_SCANF(PATH_SHMMNI, "%ld", &orig_shmmni);
 
 	set_sys_tune("nr_hugepages", hugepages, 1);
-	SAFE_FILE_PRINTF(NULL, PATH_SHMMNI, "%ld", hugepages / 2);
+	SAFE_FILE_PRINTF(PATH_SHMMNI, "%ld", hugepages / 2);
 
-	hpage_size = read_meminfo("Hugepagesize:") * 1024;
+	hpage_size = SAFE_READ_MEMINFO("Hugepagesize:") * 1024;
 	shm_size = hpage_size;
 
 	/*
@@ -136,7 +97,7 @@ void setup(void)
 	while (shm_id_1 != -1) {
 		shm_id_arr[num_shms++] = shm_id_1;
 		if (num_shms == MAXIDS)
-			tst_brkm(TBROK, cleanup, "The maximum number of "
+			tst_brk(TBROK, "The maximum number of "
 				 "shared memory ID's has been reached. "
 				 "Please increase the MAXIDS value in "
 				 "the test.");
@@ -144,12 +105,10 @@ void setup(void)
 				  SHM_HUGETLB | IPC_CREAT | IPC_EXCL | SHM_RW);
 	}
 	if (errno != ENOSPC)
-		tst_brkm(TBROK | TERRNO, cleanup, "shmget #setup");
-
-	TEST_PAUSE;
+		tst_brk(TBROK | TERRNO, "shmget #setup");
 }
 
-void cleanup(void)
+static void cleanup(void)
 {
 	int i;
 
@@ -158,6 +117,13 @@ void cleanup(void)
 
 	FILE_PRINTF(PATH_SHMMNI, "%ld", orig_shmmni);
 	set_sys_tune("nr_hugepages", orig_hugepages, 0);
-
-	tst_rmdir();
 }
+
+static struct tst_test test = {
+	.tid = "hugeshmget03",
+	.needs_root = 1,
+	.options = options,
+	.setup = setup,
+	.cleanup = cleanup,
+	.test_all = test_hugeshmget,
+};
diff --git a/testcases/kernel/mem/hugetlb/hugeshmget/hugeshmget05.c b/testcases/kernel/mem/hugetlb/hugeshmget/hugeshmget05.c
index 1a96c3c..994b294 100644
--- a/testcases/kernel/mem/hugetlb/hugeshmget/hugeshmget05.c
+++ b/testcases/kernel/mem/hugetlb/hugeshmget/hugeshmget05.c
@@ -1,26 +1,19 @@
 /*
+ * Copyright (c) International Business Machines  Corp., 2004
+ * Copyright (c) Linux Test Project, 2004-2017
  *
- *   Copyright (c) International Business Machines  Corp., 2004
+ * 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 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
+ * 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.
  */
 
 /*
- * NAME
- *	hugeshmget05.c
- *
  * DESCRIPTION
  *	hugeshmget05 - test for EACCES error
  *
@@ -41,31 +34,13 @@
  *	  wait for child to exit
  *	  remove the shared memory segment
  *
- * USAGE:  <for command-line>
- *  hugeshmget05 [-c n] [-e] [-i n] [-I x] [-P x] [-t]
- *     where,  -c n : Run n copies concurrently.
- *             -e   : Turn on errno logging.
- *	       -i n : Execute test n times.
- *	       -I x : Execute test for x seconds.
- *	       -P x : Pause for x seconds between iterations.
- *	       -t   : Turn on syscall timing.
- *
  * HISTORY
  *	03/2001 - Written by Wayne Boyer
  *	04/2004 - Updated by Robbie Williamson
- *
- * RESTRICTIONS
- *	test must be run at root
  */
 
-#include <sys/types.h>
-#include <sys/wait.h>
-#include "hugetlb.h"
-#include "safe_macros.h"
 #include "mem.h"
-
-char *TCID = "hugeshmget05";
-int TST_TOTAL = 1;
+#include "hugetlb.h"
 
 static size_t shm_size;
 static int shm_id_1 = -1;
@@ -73,95 +48,83 @@ static uid_t ltp_uid;
 static char *ltp_user = "nobody";
 
 static long hugepages = 128;
-static option_t options[] = {
-	{"s:", &sflag, &nr_opt},
+static struct tst_option options[] = {
+	{"s:", &nr_opt, "-s   num  Set the number of the been allocated hugepages"},
 	{NULL, NULL, NULL}
 };
 
 static void do_child(void);
 
-int main(int ac, char **av)
+static void test_hugeshmget(void)
 {
 	pid_t pid;
 	int status;
 
-	tst_parse_opts(ac, av, options, NULL);
-
-	if (sflag)
-		hugepages = SAFE_STRTOL(NULL, nr_opt, 0, LONG_MAX);
-
-	setup();
-
 	switch (pid = fork()) {
 	case -1:
-		tst_brkm(TBROK | TERRNO, cleanup, "fork");
+		tst_brk(TBROK | TERRNO, "fork");
 	case 0:
 		/* set the user ID of the child to the non root user */
 		if (setuid(ltp_uid) == -1)
-			tst_brkm(TBROK | TERRNO, cleanup, "setuid");
+			tst_brk(TBROK | TERRNO, "setuid");
 		do_child();
-		tst_exit();
+		exit(0);
 	default:
 		/* wait for the child to return */
 		if (waitpid(pid, &status, 0) == -1)
-			tst_brkm(TBROK | TERRNO, cleanup, "waitpid");
+			tst_brk(TBROK | TERRNO, "waitpid");
 	}
-	cleanup();
-	tst_exit();
 }
 
 static void do_child(void)
 {
-	int lc;
-
-	for (lc = 0; TEST_LOOPING(lc); lc++) {
-		tst_count = 0;
-
-		TEST(shmget(shmkey, shm_size, SHM_HUGETLB | SHM_RW));
-		if (TEST_RETURN != -1) {
-			tst_resm(TFAIL, "shmget succeeded unexpectedly");
-			continue;
-		}
-		if (TEST_ERRNO == EACCES)
-			tst_resm(TPASS | TTERRNO, "shmget failed as expected");
-		else
-			tst_resm(TFAIL | TTERRNO, "shmget failed unexpectedly "
-				 "- expect errno=EACCES, got");
+	TEST(shmget(shmkey, shm_size, SHM_HUGETLB | SHM_RW));
+	if (TEST_RETURN != -1) {
+		tst_res(TFAIL, "shmget succeeded unexpectedly");
+		return;
 	}
+	if (TEST_ERRNO == EACCES)
+		tst_res(TPASS | TTERRNO, "shmget failed as expected");
+	else
+		tst_res(TFAIL | TTERRNO, "shmget failed unexpectedly "
+				"- expect errno=EACCES, got");
 }
 
 void setup(void)
 {
 	long hpage_size;
 
-	tst_require_root();
 	check_hugepage();
-	tst_sig(FORK, DEF_HANDLER, cleanup);
-	tst_tmpdir();
+	if (nr_opt)
+		hugepages = SAFE_STRTOL(nr_opt, 0, LONG_MAX);
 
 	orig_hugepages = get_sys_tune("nr_hugepages");
 	set_sys_tune("nr_hugepages", hugepages, 1);
-	hpage_size = read_meminfo("Hugepagesize:") * 1024;
+	hpage_size = SAFE_READ_MEMINFO("Hugepagesize:") * 1024;
 
 	shm_size = hpage_size * hugepages / 2;
 	update_shm_size(&shm_size);
-	shmkey = getipckey(cleanup);
+	shmkey = getipckey();
 	shm_id_1 = shmget(shmkey, shm_size,
 			  SHM_HUGETLB | SHM_RW | IPC_CREAT | IPC_EXCL);
 	if (shm_id_1 == -1)
-		tst_brkm(TBROK | TERRNO, cleanup, "shmget #setup");
+		tst_brk(TBROK | TERRNO, "shmget #setup");
 
 	/* get the userid for a non-root user */
-	ltp_uid = getuserid(cleanup, ltp_user);
-
-	TEST_PAUSE;
+	ltp_uid = getuserid(ltp_user);
 }
 
 void cleanup(void)
 {
 	rm_shm(shm_id_1);
-
 	set_sys_tune("nr_hugepages", orig_hugepages, 0);
-
-	tst_rmdir();
 }
+
+static struct tst_test test = {
+	.tid = "hugeshmget05",
+	.needs_root = 1,
+	.options = options,
+	.setup = setup,
+	.cleanup = cleanup,
+	.test_all = test_hugeshmget,
+};
-- 
2.9.3


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

* [LTP] [PATCH V2 12/14] mem/cpuset: convert to new API
  2017-04-10 10:05 [LTP] [PATCH V2 00/14] Convert LTP kernel/mem/testcase to New API Li Wang
                   ` (10 preceding siblings ...)
  2017-04-10 10:05 ` [LTP] [PATCH V2 11/14] mem/hugeshmget: " Li Wang
@ 2017-04-10 10:05 ` Li Wang
  2017-04-10 10:05 ` [LTP] [PATCH V2 13/14] mem/swapping: " Li Wang
  2017-04-10 10:05 ` [LTP] [PATCH V2 14/14] mem/tunable: " Li Wang
  13 siblings, 0 replies; 25+ messages in thread
From: Li Wang @ 2017-04-10 10:05 UTC (permalink / raw
  To: ltp

Signed-off-by: Li Wang <liwang@redhat.com>
---
 testcases/kernel/mem/cpuset/cpuset01.c | 155 ++++++++++++---------------------
 1 file changed, 58 insertions(+), 97 deletions(-)

diff --git a/testcases/kernel/mem/cpuset/cpuset01.c b/testcases/kernel/mem/cpuset/cpuset01.c
index 558420f..80c2112 100644
--- a/testcases/kernel/mem/cpuset/cpuset01.c
+++ b/testcases/kernel/mem/cpuset/cpuset01.c
@@ -1,4 +1,16 @@
 /*
+ * Copyright (C) 2010-2017  Red Hat, Inc.
+ *
+ * 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.
+ *
  * Out Of Memory when changing cpuset's mems on NUMA. There was a
  * problem reported upstream that the allocator may see an empty
  * nodemask when changing cpuset's mems.
@@ -6,55 +18,18 @@
  * http://lkml.org/lkml/2010/5/4/79
  * http://lkml.org/lkml/2010/5/4/80
  * This test is based on the reproducers for the above issue.
- *
- * 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.
  */
+
 #include "config.h"
-#include <sys/types.h>
-#include <sys/mman.h>
-#include <sys/mount.h>
-#include <sys/stat.h>
+#include <stdio.h>
 #include <sys/wait.h>
-#include <ctype.h>
-#include <err.h>
-#include <errno.h>
-#include <fcntl.h>
-#include <math.h>
 #if HAVE_NUMAIF_H
 #include <numaif.h>
 #endif
-#include <signal.h>
-#include <stdarg.h>
-#include <stdio.h>
-#include <stdlib.h>
-#include <unistd.h>
 
-#include "test.h"
 #include "mem.h"
 #include "numa_helper.h"
 
-char *TCID = "cpuset01";
-int TST_TOTAL = 1;
-
 #if HAVE_NUMA_H && HAVE_LINUX_MEMPOLICY_H && HAVE_NUMAIF_H \
 	&& HAVE_MPOL_CONSTANTS
 volatile int end;
@@ -62,32 +37,13 @@ static int *nodes;
 static int nnodes;
 static long ncpus;
 
-static void testcpuset(void);
 static void sighandler(int signo LTP_ATTRIBUTE_UNUSED);
 static int mem_hog(void);
 static int mem_hog_cpuset(int ntasks);
 static long count_cpu(void);
 
-int main(int argc, char *argv[])
-{
-
-	tst_parse_opts(argc, argv, NULL, NULL);
-
-	ncpus = count_cpu();
-	if (get_allowed_nodes_arr(NH_MEMS | NH_CPUS, &nnodes, &nodes) < 0)
-		tst_brkm(TBROK | TERRNO, NULL, "get_allowed_nodes_arr");
-	if (nnodes <= 1)
-		tst_brkm(TCONF, NULL, "requires a NUMA system.");
-
-	setup();
-	testcpuset();
-	cleanup();
-	tst_exit();
-}
-
-static void testcpuset(void)
+static void test_cpuset(void)
 {
-	int lc;
 	int child, i, status;
 	unsigned long nmask[MAXNODES / BITS_PER_LONG] = { 0 };
 	char mems[BUFSIZ], buf[BUFSIZ];
@@ -96,11 +52,11 @@ static void testcpuset(void)
 	write_cpuset_files(CPATH_NEW, "cpus", buf);
 	read_cpuset_files(CPATH, "mems", mems);
 	write_cpuset_files(CPATH_NEW, "mems", mems);
-	SAFE_FILE_PRINTF(cleanup, CPATH_NEW "/tasks", "%d", getpid());
+	SAFE_FILE_PRINTF(CPATH_NEW "/tasks", "%d", getpid());
 
 	switch (child = fork()) {
 	case -1:
-		tst_brkm(TBROK | TERRNO, cleanup, "fork");
+		tst_brk(TBROK | TERRNO, "fork");
 	case 0:
 		for (i = 0; i < nnodes; i++) {
 			if (nodes[i] >= MAXNODES)
@@ -108,37 +64,36 @@ static void testcpuset(void)
 			set_node(nmask, nodes[i]);
 		}
 		if (set_mempolicy(MPOL_BIND, nmask, MAXNODES) == -1)
-			tst_brkm(TBROK | TERRNO, cleanup, "set_mempolicy");
+			tst_brk(TBROK | TERRNO, "set_mempolicy");
 		exit(mem_hog_cpuset(ncpus > 1 ? ncpus : 1));
 	}
-	for (lc = 0; TEST_LOOPING(lc); lc++) {
-		tst_count = 0;
-		snprintf(buf, BUFSIZ, "%d", nodes[0]);
-		write_cpuset_files(CPATH_NEW, "mems", buf);
-		snprintf(buf, BUFSIZ, "%d", nodes[1]);
-		write_cpuset_files(CPATH_NEW, "mems", buf);
-	}
+
+	snprintf(buf, BUFSIZ, "%d", nodes[0]);
+	write_cpuset_files(CPATH_NEW, "mems", buf);
+	snprintf(buf, BUFSIZ, "%d", nodes[1]);
+	write_cpuset_files(CPATH_NEW, "mems", buf);
 
 	if (waitpid(child, &status, WUNTRACED | WCONTINUED) == -1)
-		tst_brkm(TBROK | TERRNO, cleanup, "waitpid");
-	if (WEXITSTATUS(status) != 0)
-		tst_resm(TFAIL, "child exit status is %d", WEXITSTATUS(status));
+		tst_brk(TBROK | TERRNO, "waitpid");
+	if (WEXITSTATUS(status) != 0) {
+		tst_res(TFAIL, "child exit status is %d", WEXITSTATUS(status));
+		return;
+	}
+
+	tst_res(TPASS, "cpuset test pass");
 }
 
-void setup(void)
+static void setup(void)
 {
-	tst_require_root();
-
-	if (tst_kvercmp(2, 6, 32) < 0)
-		tst_brkm(TCONF, NULL, "2.6.32 or greater kernel required");
-
-	tst_sig(FORK, DEF_HANDLER, cleanup);
-	TEST_PAUSE;
-
 	mount_mem("cpuset", "cpuset", NULL, CPATH, CPATH_NEW);
+	ncpus = count_cpu();
+	if (get_allowed_nodes_arr(NH_MEMS | NH_CPUS, &nnodes, &nodes) < 0)
+		tst_brk(TBROK | TERRNO, "get_allowed_nodes_arr");
+	if (nnodes <= 1)
+		tst_brk(TCONF, "requires a NUMA system.");
 }
 
-void cleanup(void)
+static void cleanup(void)
 {
 	umount_mem(CPATH, CPATH_NEW);
 }
@@ -160,7 +115,7 @@ static int mem_hog(void)
 			    MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
 		if (addr == MAP_FAILED) {
 			ret = 1;
-			tst_resm(TFAIL | TERRNO, "mmap");
+			tst_res(TFAIL | TERRNO, "mmap");
 			break;
 		}
 		memset(addr, 0xF7, pagesize * 10);
@@ -171,26 +126,26 @@ static int mem_hog(void)
 
 static int mem_hog_cpuset(int ntasks)
 {
-	int i, lc, status, ret = 0;
+	int i, status, ret = 0;
 	struct sigaction sa;
 	pid_t *pids;
 
 	if (ntasks <= 0)
-		tst_brkm(TBROK | TERRNO, cleanup, "ntasks is small.");
+		tst_brk(TBROK | TERRNO, "ntasks is small.");
 	sa.sa_handler = sighandler;
 	if (sigemptyset(&sa.sa_mask) < 0)
-		tst_brkm(TBROK | TERRNO, cleanup, "sigemptyset");
+		tst_brk(TBROK | TERRNO, "sigemptyset");
 	sa.sa_flags = 0;
 	if (sigaction(SIGUSR1, &sa, NULL) < 0)
-		tst_brkm(TBROK | TERRNO, cleanup, "sigaction");
+		tst_brk(TBROK | TERRNO, "sigaction");
 
 	pids = malloc(sizeof(pid_t) * ntasks);
 	if (pids == NULL)
-		tst_brkm(TBROK | TERRNO, cleanup, "malloc");
+		tst_brk(TBROK | TERRNO, "malloc");
 	for (i = 0; i < ntasks; i++) {
 		switch (pids[i] = fork()) {
 		case -1:
-			tst_resm(TFAIL | TERRNO, "fork %d", pids[i]);
+			tst_res(TFAIL | TERRNO, "fork %d", pids[i]);
 			ret = 1;
 			break;
 		case 0:
@@ -200,22 +155,22 @@ static int mem_hog_cpuset(int ntasks)
 			break;
 		}
 	}
-	for (lc = 0; TEST_LOOPING(lc); lc++) ;
+
 	while (i--) {
 		if (kill(pids[i], SIGUSR1) == -1) {
-			tst_resm(TFAIL | TERRNO, "kill %d", pids[i]);
+			tst_res(TFAIL | TERRNO, "kill %d", pids[i]);
 			ret = 1;
 		}
 	}
 	while (waitpid(-1, &status, WUNTRACED | WCONTINUED) > 0) {
 		if (WIFEXITED(status)) {
 			if (WEXITSTATUS(status) != 0) {
-				tst_resm(TFAIL, "child exit status is %d",
+				tst_res(TFAIL, "child exit status is %d",
 					 WEXITSTATUS(status));
 				ret = 1;
 			}
 		} else if (WIFSIGNALED(status)) {
-			tst_resm(TFAIL, "child caught signal %d",
+			tst_res(TFAIL, "child caught signal %d",
 				 WTERMSIG(status));
 			ret = 1;
 		}
@@ -233,9 +188,15 @@ static long count_cpu(void)
 	return ncpus;
 }
 
+static struct tst_test test = {
+	.tid = "cpuset01",
+	.needs_root = 1,
+	.setup = setup,
+	.cleanup = cleanup,
+	.test_all = test_cpuset,
+	.min_kver = "2.6.32",
+};
+
 #else /* no NUMA */
-int main(void)
-{
-	tst_brkm(TCONF, NULL, "no NUMA development packages installed.");
-}
+	TST_TEST_TCONF("no NUMA development packages installed.");
 #endif
-- 
2.9.3


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

* [LTP] [PATCH V2 13/14] mem/swapping: convert to new API
  2017-04-10 10:05 [LTP] [PATCH V2 00/14] Convert LTP kernel/mem/testcase to New API Li Wang
                   ` (11 preceding siblings ...)
  2017-04-10 10:05 ` [LTP] [PATCH V2 12/14] mem/cpuset: " Li Wang
@ 2017-04-10 10:05 ` Li Wang
  2017-04-10 10:05 ` [LTP] [PATCH V2 14/14] mem/tunable: " Li Wang
  13 siblings, 0 replies; 25+ messages in thread
From: Li Wang @ 2017-04-10 10:05 UTC (permalink / raw
  To: ltp

Signed-off-by: Li Wang <liwang@redhat.com>
---
 testcases/kernel/mem/swapping/swapping01.c | 106 +++++++++++------------------
 1 file changed, 38 insertions(+), 68 deletions(-)

diff --git a/testcases/kernel/mem/swapping/swapping01.c b/testcases/kernel/mem/swapping/swapping01.c
index 171edb3..9390e8a 100644
--- a/testcases/kernel/mem/swapping/swapping01.c
+++ b/testcases/kernel/mem/swapping/swapping01.c
@@ -1,24 +1,15 @@
 /*
- * Copyright (C) 2012  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.
+ * Copyright (C) 2012-2017  Red Hat, Inc.
  *
- * 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.
+ * 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.
  *
- * 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.
+ * 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.
  */
 /*
  * swapping01 - first time swap use results in heavy swapping
@@ -53,12 +44,8 @@
 #include <stdlib.h>
 #include <string.h>
 #include <unistd.h>
-#include "test.h"
 #include "mem.h"
 
-char *TCID = "swapping01";
-int TST_TOTAL = 1;
-
 /* allow swapping 1 * phy_mem in maximum */
 #define COE_DELTA       1
 /* will try to alloc 1.3 * phy_mem */
@@ -74,54 +61,42 @@ static long mem_over;
 static long mem_over_max;
 static pid_t pid;
 
-int main(int argc, char *argv[])
+static void test_swapping(void)
 {
-	int lc;
-
-	tst_parse_opts(argc, argv, NULL, NULL);
-
 #if __WORDSIZE == 32
-	tst_brkm(TCONF, NULL, "test is not designed for 32-bit system.");
+	tst_brk(TCONF, "test is not designed for 32-bit system.");
 #endif
 
-	setup();
-
-	for (lc = 0; TEST_LOOPING(lc); lc++) {
-		tst_count = 0;
-
-		init_meminfo();
+	init_meminfo();
 
-		switch (pid = fork()) {
+	switch (pid = fork()) {
 		case -1:
-			tst_brkm(TBROK | TERRNO, cleanup, "fork");
+			tst_brk(TBROK | TERRNO, "fork");
 		case 0:
 			do_alloc();
 			exit(0);
 		default:
 			check_swapping();
-		}
 	}
-	cleanup();
-	tst_exit();
 }
 
 static void init_meminfo(void)
 {
-	swap_free_init = read_meminfo("SwapFree:");
-	if (FILE_LINES_SCANF(cleanup, "/proc/meminfo", "MemAvailable: %ld",
+	swap_free_init = SAFE_READ_MEMINFO("SwapFree:");
+	if (FILE_LINES_SCANF("/proc/meminfo", "MemAvailable: %ld",
 		&mem_available_init)) {
-		mem_available_init = read_meminfo("MemFree:")
-			+ read_meminfo("Cached:");
+		mem_available_init = SAFE_READ_MEMINFO("MemFree:")
+			+ SAFE_READ_MEMINFO("Cached:");
 	}
 	mem_over = mem_available_init * COE_SLIGHT_OVER;
 	mem_over_max = mem_available_init * COE_DELTA;
 
 	/* at least 10MB available physical memory needed */
 	if (mem_available_init < 10240)
-		tst_brkm(TCONF, cleanup, "Not enough available mem to test.");
+		tst_brk(TCONF, "Not enough available mem to test.");
 
 	if (swap_free_init < mem_over_max)
-		tst_brkm(TCONF, cleanup, "Not enough swap space to test.");
+		tst_brk(TCONF, "Not enough swap space to test.");
 }
 
 static void do_alloc(void)
@@ -129,17 +104,17 @@ static void do_alloc(void)
 	long mem_count;
 	void *s;
 
-	tst_resm(TINFO, "available physical memory: %ld MB",
+	tst_res(TINFO, "available physical memory: %ld MB",
 		mem_available_init / 1024);
 	mem_count = mem_available_init + mem_over;
-	tst_resm(TINFO, "try to allocate: %ld MB", mem_count / 1024);
+	tst_res(TINFO, "try to allocate: %ld MB", mem_count / 1024);
 	s = malloc(mem_count * 1024);
 	if (s == NULL)
-		tst_brkm(TBROK | TERRNO, cleanup, "malloc");
+		tst_brk(TBROK | TERRNO, "malloc");
 	memset(s, 1, mem_count * 1024);
-	tst_resm(TINFO, "memory allocated: %ld MB", mem_count / 1024);
+	tst_res(TINFO, "memory allocated: %ld MB", mem_count / 1024);
 	if (raise(SIGSTOP) == -1)
-		tst_brkm(TBROK | TERRNO, tst_exit, "kill");
+		tst_brk(TBROK | TERRNO, "kill");
 	free(s);
 }
 
@@ -150,44 +125,39 @@ static void check_swapping(void)
 
 	/* wait child stop */
 	if (waitpid(pid, &status, WUNTRACED) == -1)
-		tst_brkm(TBROK | TERRNO, cleanup, "waitpid");
+		tst_brk(TBROK | TERRNO, "waitpid");
 	if (!WIFSTOPPED(status))
-		tst_brkm(TBROK, cleanup, "child was not stopped.");
+		tst_brk(TBROK, "child was not stopped.");
 
 	/* Still occupying memory, loop for a while */
 	i = 0;
 	while (i < 10) {
-		swap_free_now = read_meminfo("SwapFree:");
+		swap_free_now = SAFE_READ_MEMINFO("SwapFree:");
 		sleep(1);
-		if (abs(swap_free_now - read_meminfo("SwapFree:")) < 512)
+		if (abs(swap_free_now - SAFE_READ_MEMINFO("SwapFree:")) < 512)
 			break;
 
 		i++;
 	}
 
-	swap_free_now = read_meminfo("SwapFree:");
+	swap_free_now = SAFE_READ_MEMINFO("SwapFree:");
 	swapped = swap_free_init - swap_free_now;
 	if (swapped > mem_over_max) {
 		kill(pid, SIGCONT);
-		tst_brkm(TFAIL, cleanup, "heavy swapping detected: "
+		tst_brk(TFAIL, "heavy swapping detected: "
 				"%ld MB swapped.", swapped / 1024);
 	}
 
-	tst_resm(TPASS, "no heavy swapping detected, %ld MB swapped.",
+	tst_res(TPASS, "no heavy swapping detected, %ld MB swapped.",
 		 swapped / 1024);
 	kill(pid, SIGCONT);
 	/* wait child exit */
 	if (waitpid(pid, &status, 0) == -1)
-		tst_brkm(TBROK | TERRNO, cleanup, "waitpid");
+		tst_brk(TBROK | TERRNO, "waitpid");
 }
 
-void setup(void)
-{
-	tst_sig(FORK, DEF_HANDLER, cleanup);
-
-	TEST_PAUSE;
-}
-
-void cleanup(void)
-{
-}
+static struct tst_test test = {
+	.tid = "swapping01",
+	.needs_root = 1,
+	.test_all = test_swapping,
+};
-- 
2.9.3


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

* [LTP] [PATCH V2 14/14] mem/tunable: convert to new API
  2017-04-10 10:05 [LTP] [PATCH V2 00/14] Convert LTP kernel/mem/testcase to New API Li Wang
                   ` (12 preceding siblings ...)
  2017-04-10 10:05 ` [LTP] [PATCH V2 13/14] mem/swapping: " Li Wang
@ 2017-04-10 10:05 ` Li Wang
  13 siblings, 0 replies; 25+ messages in thread
From: Li Wang @ 2017-04-10 10:05 UTC (permalink / raw
  To: ltp

Note: Skip min_free_kbytes on large(total_mem > 100G) system, since
      .timeout is only set 1200s.

Signed-off-by: Li Wang <liwang@redhat.com>
---
 testcases/kernel/mem/tunable/max_map_count.c     | 107 ++++++---------
 testcases/kernel/mem/tunable/min_free_kbytes.c   | 128 ++++++++----------
 testcases/kernel/mem/tunable/overcommit_memory.c | 164 +++++++++--------------
 3 files changed, 156 insertions(+), 243 deletions(-)

diff --git a/testcases/kernel/mem/tunable/max_map_count.c b/testcases/kernel/mem/tunable/max_map_count.c
index 75a5098..421e0ab 100644
--- a/testcases/kernel/mem/tunable/max_map_count.c
+++ b/testcases/kernel/mem/tunable/max_map_count.c
@@ -1,4 +1,18 @@
 /*
+ * Copyright (C) 2012-2017  Red Hat, Inc.
+ *
+ * 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.
+ *
+ * Description:
+ *
  * The program is designed to test max_map_count tunable file
  *
  * The kernel Documentation say that:
@@ -24,32 +38,8 @@
  * ffffffffff600000-ffffffffff601000 r-xp 00000000 00:00 0   [vsyscall]
  *
  * so we ignore this line during /proc/[pid]/maps reading.
- *
- * ********************************************************************
- * Copyright (C) 2012 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.
- *
- * ********************************************************************
  */
+
 #define _GNU_SOURCE
 #include <sys/types.h>
 #include <sys/mman.h>
@@ -60,47 +50,19 @@
 #include <stdio.h>
 #include <stdlib.h>
 #include <sys/utsname.h>
-#include "test.h"
 #include "mem.h"
 
 #define MAP_COUNT_DEFAULT	1024
 #define MAX_MAP_COUNT		65536L
 
-char *TCID = "max_map_count";
-int TST_TOTAL = 1;
-
 static long old_max_map_count;
 static long old_overcommit;
 static struct utsname un;
 
-static long count_maps(pid_t pid);
-static void max_map_count_test(void);
-
-int main(int argc, char *argv[])
+static void setup(void)
 {
-	int lc;
-
-	tst_parse_opts(argc, argv, NULL, NULL);
-
-	setup();
-	for (lc = 0; TEST_LOOPING(lc); lc++) {
-		tst_count = 0;
-		max_map_count_test();
-	}
-
-	cleanup();
-	tst_exit();
-}
-
-void setup(void)
-{
-	tst_require_root();
-
-	tst_sig(FORK, DEF_HANDLER, cleanup);
-	TEST_PAUSE;
-
 	if (access(PATH_SYSVM "max_map_count", F_OK) != 0)
-		tst_brkm(TBROK | TERRNO, NULL,
+		tst_brk(TBROK | TERRNO,
 			 "Can't support to test max_map_count");
 
 	old_max_map_count = get_sys_tune("max_map_count");
@@ -108,10 +70,10 @@ void setup(void)
 	set_sys_tune("overcommit_memory", 2, 1);
 
 	if (uname(&un) != 0)
-		tst_brkm(TBROK | TERRNO, NULL, "uname error");
+		tst_brk(TBROK | TERRNO, "uname error");
 }
 
-void cleanup(void)
+static void cleanup(void)
 {
 	set_sys_tune("overcommit_memory", old_overcommit, 0);
 	set_sys_tune("max_map_count", old_max_map_count, 0);
@@ -162,7 +124,7 @@ static long count_maps(pid_t pid)
 	snprintf(buf, BUFSIZ, "/proc/%d/maps", pid);
 	fp = fopen(buf, "r");
 	if (fp == NULL)
-		tst_brkm(TBROK | TERRNO, cleanup, "fopen %s", buf);
+		tst_brk(TBROK | TERRNO, "fopen %s", buf);
 	while (getline(&line, &len, fp) != -1) {
 		/* exclude vdso and vsyscall */
 		if (filter_map(line))
@@ -200,7 +162,7 @@ static void max_map_count_test(void)
 	 *    step 1) will be used first.
 	 * Hope OOM-killer can be more stable oneday.
 	 */
-	memfree = read_meminfo("CommitLimit:") - read_meminfo("Committed_AS:");
+	memfree = SAFE_READ_MEMINFO("CommitLimit:") - SAFE_READ_MEMINFO("Committed_AS:");
 	/* 64 used as a bias to make sure no overflow happen */
 	max_iters = memfree / sysconf(_SC_PAGESIZE) * 1024 - 64;
 	if (max_iters > MAX_MAP_COUNT)
@@ -210,24 +172,22 @@ static void max_map_count_test(void)
 	while (max_maps <= max_iters) {
 		set_sys_tune("max_map_count", max_maps, 1);
 
-		switch (pid = tst_fork()) {
-		case -1:
-			tst_brkm(TBROK | TERRNO, cleanup, "fork");
+		switch (pid = SAFE_FORK()) {
 		case 0:
 			while (mmap(NULL, 1, PROT_READ,
 				    MAP_SHARED | MAP_ANONYMOUS, -1, 0)
 			       != MAP_FAILED) ;
 			if (raise(SIGSTOP) != 0)
-				tst_brkm(TBROK | TERRNO, tst_exit, "raise");
+				tst_brk(TBROK | TERRNO, "raise");
 			exit(0);
 		default:
 			break;
 		}
 		/* wait child done mmap and stop */
 		if (waitpid(pid, &status, WUNTRACED) == -1)
-			tst_brkm(TBROK | TERRNO, cleanup, "waitpid");
+			tst_brk(TBROK | TERRNO, "waitpid");
 		if (!WIFSTOPPED(status))
-			tst_brkm(TBROK, cleanup, "child did not stopped");
+			tst_brk(TBROK, "child did not stopped");
 
 		map_count = count_maps(pid);
 		/* Note max_maps will be exceeded by one for
@@ -236,18 +196,27 @@ static void max_map_count_test(void)
 		 * writing this COMMENT!
 		*/
 		if (map_count == (max_maps + 1))
-			tst_resm(TPASS, "%ld map entries in total "
+			tst_res(TPASS, "%ld map entries in total "
 				 "as expected.", max_maps);
 		else
-			tst_resm(TFAIL, "%ld map entries in total, but "
+			tst_res(TFAIL, "%ld map entries in total, but "
 				 "expected %ld entries", map_count, max_maps);
 
 		/* make child continue to exit */
 		if (kill(pid, SIGCONT) != 0)
-			tst_brkm(TBROK | TERRNO, cleanup, "kill");
+			tst_brk(TBROK | TERRNO, "kill");
 		if (waitpid(pid, &status, 0) == -1)
-			tst_brkm(TBROK | TERRNO, cleanup, "waitpid");
+			tst_brk(TBROK | TERRNO, "waitpid");
 
 		max_maps = max_maps << 1;
 	}
 }
+
+static struct tst_test test = {
+	.tid = "max_map_count",
+	.needs_root = 1,
+	.forks_child = 1,
+	.setup = setup,
+	.cleanup = cleanup,
+	.test_all = max_map_count_test,
+};
diff --git a/testcases/kernel/mem/tunable/min_free_kbytes.c b/testcases/kernel/mem/tunable/min_free_kbytes.c
index 493c37e..fe20154 100644
--- a/testcases/kernel/mem/tunable/min_free_kbytes.c
+++ b/testcases/kernel/mem/tunable/min_free_kbytes.c
@@ -1,4 +1,18 @@
 /*
+ * Copyright (C) 2012-2017  Red Hat, Inc.
+ *
+ * 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.
+ *
+ * Description:
+ *
  * The case is designed to test min_free_kbytes tunable.
  *
  * The tune is used to control free memory, and system always
@@ -12,31 +26,6 @@
  * a) default min_free_kbytes with all overcommit memory policy
  * b) 2x default value with all overcommit memory policy
  * c) 5% of MemFree or %2 MemTotal with all overcommit memory policy
- *
- ********************************************************************
- * Copyright (C) 2012 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.
- *
- * ********************************************************************
  */
 
 #include <sys/types.h>
@@ -47,14 +36,11 @@
 #include <signal.h>
 #include <stdio.h>
 #include <stdlib.h>
-#include "test.h"
 #include "mem.h"
 
 #define MAP_SIZE (1UL<<20)
 
 volatile int end;
-char *TCID = "min_free_kbytes";
-int TST_TOTAL = 1;
 static unsigned long default_tune;
 static unsigned long orig_overcommit;
 static unsigned long total_mem;
@@ -64,50 +50,38 @@ static int eatup_mem(unsigned long overcommit_policy);
 static void check_monitor(void);
 static void sighandler(int signo LTP_ATTRIBUTE_UNUSED);
 
-int main(int argc, char *argv[])
+static void min_free_kbytes_test(void)
 {
-	int lc, pid, status;
+	int pid, status;
 	struct sigaction sa;
 
-	tst_parse_opts(argc, argv, NULL, NULL);
-
 	sa.sa_handler = sighandler;
 	if (sigemptyset(&sa.sa_mask) < 0)
-		tst_brkm(TBROK | TERRNO, cleanup, "sigemptyset");
+		tst_brk(TBROK | TERRNO, "sigemptyset");
 	sa.sa_flags = 0;
 	if (sigaction(SIGUSR1, &sa, NULL) < 0)
-		tst_brkm(TBROK | TERRNO, cleanup, "sigaction");
-
-	setup();
-
-	switch (pid = fork()) {
-	case -1:
-		tst_brkm(TBROK | TERRNO, cleanup, "fork");
+		tst_brk(TBROK | TERRNO, "sigaction");
 
+	switch (pid = SAFE_FORK()) {
 	case 0:
 		/* startup the check monitor */
 		check_monitor();
 		exit(0);
 	}
 
-	for (lc = 0; TEST_LOOPING(lc); lc++) {
-		tst_count = 0;
-
-		test_tune(2);
-		test_tune(0);
-		test_tune(1);
-	}
+	test_tune(2);
+	test_tune(0);
+	test_tune(1);
 
 	if (kill(pid, SIGUSR1) == -1)
-		tst_brkm(TBROK | TERRNO, cleanup, "kill %d", pid);
+		tst_brk(TBROK | TERRNO, "kill %d", pid);
 	if (waitpid(pid, &status, WUNTRACED | WCONTINUED) == -1)
-		tst_brkm(TBROK | TERRNO, cleanup, "waitpid");
+		tst_brk(TBROK | TERRNO, "waitpid");
 	if (WIFEXITED(status) && WEXITSTATUS(status) != 0)
-		tst_resm(TFAIL,
+		tst_res(TFAIL,
 			 "check_monitor child exit with status: %d", status);
 
-	cleanup();
-	tst_exit();
+	tst_res(TPASS, "min_free_kbytes test pass");
 }
 
 static void test_tune(unsigned long overcommit_policy)
@@ -128,8 +102,8 @@ static void test_tune(unsigned long overcommit_policy)
 			set_sys_tune("min_free_kbytes", 2 * default_tune, 1);
 			/* case3 */
 		} else {
-			memfree = read_meminfo("MemFree:");
-			memtotal = read_meminfo("MemTotal:");
+			memfree = SAFE_READ_MEMINFO("MemFree:");
+			memtotal = SAFE_READ_MEMINFO("MemTotal:");
 			tune = memfree / 20;
 			if (tune > (memtotal / 50))
 				tune = memtotal / 50;
@@ -140,18 +114,18 @@ static void test_tune(unsigned long overcommit_policy)
 		fflush(stdout);
 		switch (pid[i] = fork()) {
 		case -1:
-			tst_brkm(TBROK | TERRNO, cleanup, "fork");
+			tst_brk(TBROK | TERRNO, "fork");
 		case 0:
 			ret = eatup_mem(overcommit_policy);
 			exit(ret);
 		}
 
 		if (waitpid(pid[i], &status, WUNTRACED | WCONTINUED) == -1)
-			tst_brkm(TBROK | TERRNO, cleanup, "waitpid");
+			tst_brk(TBROK | TERRNO, "waitpid");
 
 		if (overcommit_policy == 2) {
 			if (!WIFEXITED(status) || WEXITSTATUS(status) != 0)
-				tst_resm(TFAIL,
+				tst_res(TFAIL,
 					 "child unexpectedly failed: %d",
 					 status);
 		} else if (overcommit_policy == 1) {
@@ -160,25 +134,25 @@ static void test_tune(unsigned long overcommit_policy)
 			{
 				if (total_mem < 3145728UL)
 #endif
-					tst_resm(TFAIL,
+					tst_res(TFAIL,
 						 "child unexpectedly failed: %d",
 						 status);
 #if __WORDSIZE == 32
 				/* in 32-bit system, a process allocate about 3Gb memory@most */
 				else
-					tst_resm(TINFO, "Child can't allocate "
+					tst_res(TINFO, "Child can't allocate "
 						 ">3Gb memory in 32bit system");
 			}
 #endif
 		} else {
 			if (WIFEXITED(status)) {
 				if (WEXITSTATUS(status) != 0) {
-					tst_resm(TFAIL, "child unexpectedly "
+					tst_res(TFAIL, "child unexpectedly "
 						 "failed: %d", status);
 				}
 			} else if (!WIFSIGNALED(status) ||
 				   WTERMSIG(status) != SIGKILL) {
-				tst_resm(TFAIL,
+				tst_res(TFAIL,
 					 "child unexpectedly failed: %d",
 					 status);
 			}
@@ -192,7 +166,7 @@ static int eatup_mem(unsigned long overcommit_policy)
 	unsigned long memfree;
 	void *addrs;
 
-	memfree = read_meminfo("MemFree:");
+	memfree = SAFE_READ_MEMINFO("MemFree:");
 	printf("memfree is %lu kB before eatup mem\n", memfree);
 	while (1) {
 		addrs = mmap(NULL, MAP_SIZE, PROT_READ | PROT_WRITE,
@@ -206,7 +180,7 @@ static int eatup_mem(unsigned long overcommit_policy)
 		}
 		memset(addrs, 1, MAP_SIZE);
 	}
-	memfree = read_meminfo("MemFree:");
+	memfree = SAFE_READ_MEMINFO("MemFree:");
 	printf("memfree is %lu kB after eatup mem\n", memfree);
 
 	return ret;
@@ -218,13 +192,13 @@ static void check_monitor(void)
 	unsigned long memfree;
 
 	while (end) {
-		memfree = read_meminfo("MemFree:");
+		memfree = SAFE_READ_MEMINFO("MemFree:");
 		tune = get_sys_tune("min_free_kbytes");
 
 		if (memfree < tune) {
-			tst_resm(TINFO, "MemFree is %lu kB, "
+			tst_res(TINFO, "MemFree is %lu kB, "
 				 "min_free_kbytes is %lu kB", memfree, tune);
-			tst_resm(TFAIL, "MemFree < min_free_kbytes");
+			tst_res(TFAIL, "MemFree < min_free_kbytes");
 		}
 
 		sleep(2);
@@ -236,25 +210,31 @@ static void sighandler(int signo LTP_ATTRIBUTE_UNUSED)
 	end = 1;
 }
 
-void setup(void)
+static void setup(void)
 {
-	tst_require_root();
 	if (get_sys_tune("panic_on_oom")) {
-		tst_brkm(TCONF, NULL,
+		tst_brk(TCONF,
 			"panic_on_oom is set, disable it to run these testcases");
 	}
 
-	tst_sig(FORK, DEF_HANDLER, cleanup);
-	TEST_PAUSE;
-
-	total_mem = read_meminfo("MemTotal:") + read_meminfo("SwapTotal:");
+	total_mem = SAFE_READ_MEMINFO("MemTotal:") + SAFE_READ_MEMINFO("SwapTotal:");
 
 	default_tune = get_sys_tune("min_free_kbytes");
 	orig_overcommit = get_sys_tune("overcommit_memory");
 }
 
-void cleanup(void)
+static void cleanup(void)
 {
 	set_sys_tune("min_free_kbytes", default_tune, 0);
 	set_sys_tune("overcommit_memory", orig_overcommit, 0);
 }
+
+static struct tst_test test = {
+	.tid = "min_free_kbytes",
+	.needs_root = 1,
+	.forks_child = 1,
+	.timeout = 1200,
+	.setup = setup,
+	.cleanup = cleanup,
+	.test_all = min_free_kbytes_test,
+};
diff --git a/testcases/kernel/mem/tunable/overcommit_memory.c b/testcases/kernel/mem/tunable/overcommit_memory.c
index 79a3034..0b6d678 100644
--- a/testcases/kernel/mem/tunable/overcommit_memory.c
+++ b/testcases/kernel/mem/tunable/overcommit_memory.c
@@ -1,4 +1,18 @@
 /*
+ * Copyright (C) 2012-2017  Red Hat, Inc.
+ *
+ * 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.
+ *
+ * Descriptions:
+ *
  * There are two tunables overcommit_memory and overcommit_ratio under
  * /proc/sys/vm/, which can control memory overcommitment.
  *
@@ -49,45 +63,24 @@
  * References:
  * - Documentation/sysctl/vm.txt
  * - Documentation/vm/overcommit-accounting
- *
- * ********************************************************************
- * Copyright (C) 2012 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.
- *
- * ********************************************************************
  */
 
 #include <errno.h>
 #include <stdio.h>
 #include <stdlib.h>
-#include "test.h"
-#include "safe_macros.h"
+#include <limits.h>
 #include "mem.h"
 
 #define DEFAULT_OVER_RATIO	50L
 #define EXPECT_PASS		0
 #define EXPECT_FAIL		1
 
-char *TCID = "overcommit_memory";
+char *R_opt;
+static struct tst_option options[] = {
+	{"R:", &R_opt, "  -R n    Percentage of overcommitting memory"},
+	{NULL, NULL, NULL}
+};
+
 static long old_overcommit_memory;
 static long old_overcommit_ratio;
 static long overcommit_ratio;
@@ -95,102 +88,64 @@ static long sum_total;
 static long free_total;
 static long commit_limit;
 static long commit_left;
-static int R_flag;
-static char *R_opt;
-option_t options[] = {
-	{"R:", &R_flag, &R_opt},
-	{NULL, NULL, NULL}
-};
 
-static void overcommit_memory_test(void);
 static int heavy_malloc(long size);
 static void alloc_and_check(long size, int expect_result);
-static void usage(void);
 static void update_mem(void);
 
-int main(int argc, char *argv[])
-{
-	int lc;
-
-	tst_parse_opts(argc, argv, options, &usage);
-
-#if __WORDSIZE == 32
-	tst_brkm(TCONF, NULL, "test is not designed for 32-bit system.");
-#endif
-
-	if (R_flag)
-		overcommit_ratio = SAFE_STRTOL(NULL, R_opt, 0, LONG_MAX);
-	else
-		overcommit_ratio = DEFAULT_OVER_RATIO;
-
-	setup();
-
-	for (lc = 0; TEST_LOOPING(lc); lc++) {
-		tst_count = 0;
-
-		overcommit_memory_test();
-	}
-
-	cleanup();
-
-	tst_exit();
-}
-
-void setup(void)
+static void setup(void)
 {
 	long mem_total, swap_total;
 	struct rlimit lim;
 
-	tst_require_root();
-
-	tst_sig(NOFORK, DEF_HANDLER, cleanup);
-
-	TEST_PAUSE;
-
 	if (access(PATH_SYSVM "overcommit_memory", F_OK) == -1 ||
 	    access(PATH_SYSVM "overcommit_ratio", F_OK) == -1)
-		tst_brkm(TCONF, NULL, "The system "
+		tst_brk(TCONF, "The system "
 			 "can't support to test %s", TCID);
 
+	if (R_opt)
+		overcommit_ratio = SAFE_STRTOL(R_opt, 0, LONG_MAX);
+	else
+		overcommit_ratio = DEFAULT_OVER_RATIO;
+
 	old_overcommit_memory = get_sys_tune("overcommit_memory");
 	old_overcommit_ratio = get_sys_tune("overcommit_ratio");
 
-	mem_total = read_meminfo("MemTotal:");
-	tst_resm(TINFO, "MemTotal is %ld kB", mem_total);
-	swap_total = read_meminfo("SwapTotal:");
-	tst_resm(TINFO, "SwapTotal is %ld kB", swap_total);
+	mem_total = SAFE_READ_MEMINFO("MemTotal:");
+	tst_res(TINFO, "MemTotal is %ld kB", mem_total);
+	swap_total = SAFE_READ_MEMINFO("SwapTotal:");
+	tst_res(TINFO, "SwapTotal is %ld kB", swap_total);
 	sum_total = mem_total + swap_total;
 
-	commit_limit = read_meminfo("CommitLimit:");
-	tst_resm(TINFO, "CommitLimit is %ld kB", commit_limit);
+	commit_limit = SAFE_READ_MEMINFO("CommitLimit:");
+	tst_res(TINFO, "CommitLimit is %ld kB", commit_limit);
 
-	SAFE_GETRLIMIT(NULL, RLIMIT_AS, &lim);
+	SAFE_GETRLIMIT(RLIMIT_AS, &lim);
 
 	if (lim.rlim_cur != RLIM_INFINITY) {
 		lim.rlim_cur = RLIM_INFINITY;
 		lim.rlim_max = RLIM_INFINITY;
 
-		tst_resm(TINFO, "Increasing RLIM_AS to INFINITY");
+		tst_res(TINFO, "Increasing RLIM_AS to INFINITY");
 
-		SAFE_SETRLIMIT(NULL, RLIMIT_AS, &lim);
+		SAFE_SETRLIMIT(RLIMIT_AS, &lim);
 	}
 
 	set_sys_tune("overcommit_ratio", overcommit_ratio, 1);
 }
 
-void cleanup(void)
+static void cleanup(void)
 {
 	set_sys_tune("overcommit_memory", old_overcommit_memory, 0);
 	set_sys_tune("overcommit_ratio", old_overcommit_ratio, 0);
 }
 
-static void usage(void)
-{
-	printf("  -R n    Percentage of overcommitting memory\n");
-}
-
 static void overcommit_memory_test(void)
 {
+
+#if __WORDSIZE == 32
+	tst_brk(TCONF, "test is not designed for 32-bit system.");
+#endif
 	/* start to test overcommit_memory=2 */
 	set_sys_tune("overcommit_memory", 2, 1);
 
@@ -224,11 +179,11 @@ static int heavy_malloc(long size)
 
 	p = malloc(size * KB);
 	if (p != NULL) {
-		tst_resm(TINFO, "malloc %ld kB successfully", size);
+		tst_res(TINFO, "malloc %ld kB successfully", size);
 		free(p);
 		return 0;
 	} else {
-		tst_resm(TINFO, "malloc %ld kB failed", size);
+		tst_res(TINFO, "malloc %ld kB failed", size);
 		return 1;
 	}
 }
@@ -243,18 +198,18 @@ static void alloc_and_check(long size, int expect_result)
 	switch (expect_result) {
 	case EXPECT_PASS:
 		if (result == 0)
-			tst_resm(TPASS, "alloc passed as expected");
+			tst_res(TPASS, "alloc passed as expected");
 		else
-			tst_resm(TFAIL, "alloc failed, expected to pass");
+			tst_res(TFAIL, "alloc failed, expected to pass");
 		break;
 	case EXPECT_FAIL:
 		if (result != 0)
-			tst_resm(TPASS, "alloc failed as expected");
+			tst_res(TPASS, "alloc failed as expected");
 		else
-			tst_resm(TFAIL, "alloc passed, expected to fail");
+			tst_res(TFAIL, "alloc passed, expected to fail");
 		break;
 	default:
-		tst_brkm(TBROK, cleanup, "Invaild numbler parameter: %d",
+		tst_brk(TBROK, "Invaild numbler parameter: %d",
 			 expect_result);
 	}
 }
@@ -264,20 +219,29 @@ static void update_mem(void)
 	long mem_free, swap_free;
 	long committed;
 
-	mem_free = read_meminfo("MemFree:");
-	swap_free = read_meminfo("SwapFree:");
+	mem_free = SAFE_READ_MEMINFO("MemFree:");
+	swap_free = SAFE_READ_MEMINFO("SwapFree:");
 	free_total = mem_free + swap_free;
-	commit_limit = read_meminfo("CommitLimit:");
+	commit_limit = SAFE_READ_MEMINFO("CommitLimit:");
 
 	if (get_sys_tune("overcommit_memory") == 2) {
-		committed = read_meminfo("Committed_AS:");
+		committed = SAFE_READ_MEMINFO("Committed_AS:");
 		commit_left = commit_limit - committed;
 
 		if (commit_left < 0) {
-			tst_resm(TINFO, "CommitLimit is %ld, Committed_AS"
+			tst_res(TINFO, "CommitLimit is %ld, Committed_AS"
 				 " is %ld", commit_limit, committed);
-			tst_brkm(TBROK, cleanup, "Unexpected error: "
+			tst_brk(TBROK, "Unexpected error: "
 				 "CommitLimit < Committed_AS");
 		}
 	}
 }
+
+static struct tst_test test = {
+	.tid = "overcommit_memory",
+	.needs_root = 1,
+	.options = options,
+	.setup = setup,
+	.cleanup = cleanup,
+	.test_all = overcommit_memory_test,
+};
-- 
2.9.3


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

* [LTP] [PATCH V2 04/14] mem/ksm: convert to new API
  2017-04-10 10:05 ` [LTP] [PATCH V2 04/14] mem/ksm: convert to new API Li Wang
@ 2017-07-07 14:49   ` Cyril Hrubis
  2017-07-07 15:29     ` Cyril Hrubis
  2017-07-10  7:33     ` Li Wang
  0 siblings, 2 replies; 25+ messages in thread
From: Cyril Hrubis @ 2017-07-07 14:49 UTC (permalink / raw
  To: ltp

Hi!
> -	tst_parse_opts(argc, argv, ksm_options, ksm_usage);
> -	setup();
> -	for (lc = 0; TEST_LOOPING(lc); lc++) {
> -		tst_count = 0;
> -		check_ksm_options(&size, &num, &unit);
> -		create_same_memory(size, num, unit);
> -	}
> -	cleanup();
> -	tst_exit();
> +	check_ksm_options(&size, &num, &unit);

There is no need to parse the test options on every test iteration, this
should be called once in the test setup.

Moreover passing things around in global variables is messy. What about
we (ideally in a separate patch) created a function(s) to parse the KSM
options and pass the string values explicitily.

setup(void)
{
	...
	parse_ksm_options(str_size, &size, str_num, &num, str_unit, &unit);
	...
}

Also could we define the ksm_options array in one place instead of
repeating it in each test. What about putting it into ksh_common.h in
the mem/ksm/ directory and including it in each test?

-- 
Cyril Hrubis
chrubis@suse.cz

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

* [LTP] [PATCH V2 03/14] mem/oom: fix the timeout issue
  2017-04-10 10:05 ` [LTP] [PATCH V2 03/14] mem/oom: fix the timeout issue Li Wang
@ 2017-07-07 15:11   ` Cyril Hrubis
  2017-07-10  7:01     ` Li Wang
  2017-07-10  7:38     ` Jan Stancek
  0 siblings, 2 replies; 25+ messages in thread
From: Cyril Hrubis @ 2017-07-07 15:11 UTC (permalink / raw
  To: ltp

Hi!
> OOM spend time is depend on amount of RAM+Swap, it's hard to get a standard
> to measure how long it takes, so that we cann't set tst_test->timeout easily.
> 
> In this patch, we divide the tests into two part:
> 
> 1. OOM takes original way on small(mem_total <= 100G) RAM, and with setting
>    the .timeout according to the worst spending on 100G system. Here gives
>    double(.timeout = 1200) cost in order to make sure time is long enough.
> 
> 2. If system RAM is larger than 100G, OOM goes to the cgroup limited way.

I wonder if it's really OK to limit these tests to 100G of RAM. Since as
it is these tests can really stress the system on low memory conditions,
if we limit them to run inside of a cgroup we are testing cgroup
implementation instead, which is we should probably do as well.

I'm starting to think that OOM tests are special enough so that we can
add functionality to disable timeouts for these, i.e. set the .timeout
to -1 and change the test library so that the alarm() is not set in such
case.

Jan do you have anything to add?

> Signed-off-by: Li Wang <liwang@redhat.com>
> ---
>  testcases/kernel/mem/oom/oom01.c | 12 ++++++++++++
>  testcases/kernel/mem/oom/oom02.c | 12 ++++++++++++
>  testcases/kernel/mem/oom/oom03.c |  1 +
>  testcases/kernel/mem/oom/oom04.c | 11 +++++++++++
>  testcases/kernel/mem/oom/oom05.c |  1 +
>  5 files changed, 37 insertions(+)
> 
> diff --git a/testcases/kernel/mem/oom/oom01.c b/testcases/kernel/mem/oom/oom01.c
> index 64a368e..056e02a 100644
> --- a/testcases/kernel/mem/oom/oom01.c
> +++ b/testcases/kernel/mem/oom/oom01.c
> @@ -29,6 +29,8 @@
>  #include <unistd.h>
>  #include "mem.h"
>  
> +static unsigned long mem_total;
> +
>  static void verify_oom(void)
>  {
>  #if __WORDSIZE == 32
> @@ -50,11 +52,20 @@ static void verify_oom(void)
>  
>  static void setup(void)
>  {
> +	mem_total = SAFE_READ_MEMINFO("MemTotal:");
> +	if (mem_total > TESTMEM/1024*100L) {
> +		tst_res(TINFO, "System RAM is larger than 100G, test in memory cgroup");
> +		mount_mem("memcg", "cgroup", "memory", MEMCG_PATH, MEMCG_PATH_NEW);
> +		write_memcg();
> +	}
> +
>  	overcommit = get_sys_tune("overcommit_memory");
>  }
>  
>  static void cleanup(void)
>  {
> +	if (mem_total > TESTMEM/1024*100L)
> +		umount_mem(MEMCG_PATH, MEMCG_PATH_NEW);
>  	set_sys_tune("overcommit_memory", overcommit, 0);
>  }
>  
> @@ -62,6 +73,7 @@ static struct tst_test test = {
>  	.tid = "oom01",
>  	.needs_root = 1,
>  	.forks_child = 1,
> +	.timeout = 1200,
>  	.setup = setup,
>  	.cleanup = cleanup,
>  	.test_all = verify_oom,
> diff --git a/testcases/kernel/mem/oom/oom02.c b/testcases/kernel/mem/oom/oom02.c
> index f315ee2..a1be806 100644
> --- a/testcases/kernel/mem/oom/oom02.c
> +++ b/testcases/kernel/mem/oom/oom02.c
> @@ -32,6 +32,8 @@
>  #if HAVE_NUMA_H && HAVE_LINUX_MEMPOLICY_H && HAVE_NUMAIF_H \
>  	&& HAVE_MPOL_CONSTANTS
>  
> +static unsigned long mem_total;
> +
>  static void verify_oom(void)
>  {
>  #if __WORDSIZE == 32
> @@ -50,6 +52,13 @@ static void verify_oom(void)
>  
>  static void setup(void)
>  {
> +	mem_total = SAFE_READ_MEMINFO("MemTotal:");
> +	if (mem_total > TESTMEM/1024*100L) {
> +		tst_res(TINFO, "System RAM is larger than 100G, test in memory cgroup");
> +		mount_mem("memcg", "cgroup", "memory", MEMCG_PATH, MEMCG_PATH_NEW);
> +		write_memcg();
> +	}
> +
>  	if (!is_numa(NULL, NH_MEMS, 2))
>  		tst_brk(TCONF, "The case need a NUMA system.");
>  
> @@ -59,6 +68,8 @@ static void setup(void)
>  
>  static void cleanup(void)
>  {
> +	if (mem_total > TESTMEM/1024*100L)
> +		umount_mem(MEMCG_PATH, MEMCG_PATH_NEW);
>  	set_sys_tune("overcommit_memory", overcommit, 0);
>  }
>  
> @@ -66,6 +77,7 @@ static struct tst_test test = {
>  	.tid = "oom02",
>  	.needs_root = 1,
>  	.forks_child = 1,
> +	.timeout = 1200,
>  	.setup = setup,
>  	.cleanup = cleanup,
>  	.test_all = verify_oom,
> diff --git a/testcases/kernel/mem/oom/oom03.c b/testcases/kernel/mem/oom/oom03.c
> index d3aed60..48c1ea5 100644
> --- a/testcases/kernel/mem/oom/oom03.c
> +++ b/testcases/kernel/mem/oom/oom03.c
> @@ -79,6 +79,7 @@ static struct tst_test test = {
>  	.tid = "oom03",
>  	.needs_root = 1,
>  	.forks_child = 1,
> +	.timeout = 1200,
>  	.setup = setup,
>  	.cleanup = cleanup,
>  	.test_all = verify_oom,
> diff --git a/testcases/kernel/mem/oom/oom04.c b/testcases/kernel/mem/oom/oom04.c
> index afe35b2..179a98a 100644
> --- a/testcases/kernel/mem/oom/oom04.c
> +++ b/testcases/kernel/mem/oom/oom04.c
> @@ -32,6 +32,8 @@
>  #if HAVE_NUMA_H && HAVE_LINUX_MEMPOLICY_H && HAVE_NUMAIF_H \
>  	&& HAVE_MPOL_CONSTANTS
>  
> +static unsigned long mem_total;
> +
>  static void verify_oom(void)
>  {
>  #if __WORDSIZE == 32
> @@ -63,6 +65,12 @@ static void setup(void)
>  	overcommit = get_sys_tune("overcommit_memory");
>  	set_sys_tune("overcommit_memory", 1, 1);
>  
> +	mem_total = SAFE_READ_MEMINFO("MemTotal:");
> +	if (mem_total > TESTMEM/1024*100L) {
> +		tst_res(TINFO, "System RAM is larger than 100G, test in memory cgroup");
> +		mount_mem("memcg", "cgroup", "memory", MEMCG_PATH, MEMCG_PATH_NEW);
> +		write_memcg();
> +	}
>  	mount_mem("cpuset", "cpuset", NULL, CPATH, CPATH_NEW);
>  
>  	/*
> @@ -80,6 +88,8 @@ static void setup(void)
>  
>  static void cleanup(void)
>  {
> +	if (mem_total > TESTMEM/1024*100L)
> +		umount_mem(MEMCG_PATH, MEMCG_PATH_NEW);
>  	set_sys_tune("overcommit_memory", overcommit, 0);
>  	umount_mem(CPATH, CPATH_NEW);
>  }
> @@ -88,6 +98,7 @@ static struct tst_test test = {
>  	.tid = "oom04",
>  	.needs_root = 1,
>  	.forks_child = 1,
> +	.timeout = 1200,
>  	.setup = setup,
>  	.cleanup = cleanup,
>  	.test_all = verify_oom,
> diff --git a/testcases/kernel/mem/oom/oom05.c b/testcases/kernel/mem/oom/oom05.c
> index d97fe7d..39361a9 100644
> --- a/testcases/kernel/mem/oom/oom05.c
> +++ b/testcases/kernel/mem/oom/oom05.c
> @@ -116,6 +116,7 @@ static struct tst_test test = {
>  	.tid = "oom05",
>  	.needs_root = 1,
>  	.forks_child = 1,
> +	.timeout = 1200,
>  	.setup = setup,
>  	.cleanup = cleanup,
>  	.test_all = verify_oom,
> -- 
> 2.9.3
> 
> 
> -- 
> Mailing list info: https://lists.linux.it/listinfo/ltp

-- 
Cyril Hrubis
chrubis@suse.cz

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

* [LTP] [PATCH V2 05/14] mem/thp: convert to new API
  2017-04-10 10:05 ` [LTP] [PATCH V2 05/14] mem/thp: " Li Wang
@ 2017-07-07 15:27   ` Cyril Hrubis
  2017-07-11  4:32     ` Li Wang
  0 siblings, 1 reply; 25+ messages in thread
From: Cyril Hrubis @ 2017-07-07 15:27 UTC (permalink / raw
  To: ltp

Hi!
> +	switch (pid = SAFE_FORK()) {
>  		case 0:
>  			memset(arg, 'c', length - 1);
>  			arg[length - 1] = '\0';
> @@ -91,29 +72,31 @@ int main(int argc, char **argv)
>  			}
>  		default:
>  			if (waitpid(pid, &st, 0) == -1)
> -				tst_brkm(TBROK | TERRNO, cleanup, "waitpid");
> +				tst_brk(TBROK | TERRNO, "waitpid");
>  			if (!WIFEXITED(st) || WEXITSTATUS(st) != 0)
> -				tst_brkm(TBROK, cleanup,
> -					 "child exited abnormally");
> -		}
> +				tst_brk(TBROK, "child exited abnormally");
>  	}

We do have SAFE_WAITPID() as well, but in this case we can simply call
tst_reap_children() that would take care of the exit status as well.

> -	tst_resm(TPASS, "system didn't crash, pass.");
> -	cleanup();
> -	tst_exit();
> +
> +	tst_res(TPASS, "system didn't crash, pass.");
>  }
>  
>  static void setup(void)
>  {
>  	ps = sysconf(_SC_PAGESIZE);
>  	length = 32 * ps;
> -	arg = malloc(length);
> -	if (arg == NULL)
> -		tst_brkm(TBROK | TERRNO, NULL, "malloc");
> -
> -	tst_sig(FORK, DEF_HANDLER, cleanup);
> -	TEST_PAUSE;
> +	arg = SAFE_MALLOC(length);
>  }
>  
>  static void cleanup(void)
>  {
> +	free(arg);
>  }
> +
> +static struct tst_test test = {
> +	.tid = "thp01",
> +	.needs_root = 1,
> +	.forks_child = 1,
> +	.setup = setup,
> +	.cleanup = cleanup,
> +	.test_all = thp_test,
> +};
> diff --git a/testcases/kernel/mem/thp/thp02.c b/testcases/kernel/mem/thp/thp02.c
> index eb919aa..45cfbe5 100644
> --- a/testcases/kernel/mem/thp/thp02.c
> +++ b/testcases/kernel/mem/thp/thp02.c
> @@ -1,24 +1,15 @@
>  /*
> - * Copyright (C) 2011  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.
> + * Copyright (C) 2011-2017  Red Hat, Inc.
>   *
> - * 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.
> + * 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.
>   *
> - * 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.
> + * 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.
>   *
>   * thp02 - detect mremap bug when THP is enabled.
>   *
> @@ -52,36 +43,11 @@
>  #include <unistd.h>
>  #include "mem.h"
>  
> -char *TCID = "thp02";
> -int TST_TOTAL = 1;
> -
>  #ifdef HAVE_MREMAP_FIXED
>  static int ps;
>  static long hps, size;
>  static void *p, *p2, *p3, *p4;
>  
> -static void do_mremap(void);
> -
> -int main(int argc, char **argv)
> -{
> -	int lc;
> -
> -	tst_parse_opts(argc, argv, NULL, NULL);
> -
> -	setup();
> -
> -	for (lc = 0; TEST_LOOPING(lc); lc++) {
> -		tst_count = 0;
> -
> -		do_mremap();
> -	}
> -	tst_resm(TPASS, "Still alive.");
> -
> -	cleanup();
> -	tst_exit();
> -
> -}
> -
>  static void do_mremap(void)
>  {
>  	int i;
> @@ -89,11 +55,11 @@ static void do_mremap(void)
>  
>  	for (i = 0; i < 4; i++) {
>  		if (posix_memalign(&p, hps, size))
> -			tst_brkm(TBROK | TERRNO, cleanup, "memalign p");
> +			tst_brk(TBROK | TERRNO, "memalign p");
>  		if (posix_memalign(&p2, hps, size))
> -			tst_brkm(TBROK | TERRNO, cleanup, "memalign p2");
> +			tst_brk(TBROK | TERRNO, "memalign p2");
>  		if (posix_memalign(&p3, hps, size))
> -			tst_brkm(TBROK | TERRNO, cleanup, "memalign p3");
> +			tst_brk(TBROK | TERRNO, "memalign p3");

The posix_memalign() returns errno hence this will print bogus values
with TERRNO. We can either use SAFE_MEMALIGN() or possibly add
POSIX_MEMALIGN() to the safe_macros and use that one instead.

>  		memset(p, 0xff, size);
>  		memset(p2, 0xff, size);
> @@ -108,37 +74,36 @@ static void do_mremap(void)
>  		 */
>  		old_addr = p + ps * (i >> 1);
>  		new_addr = p3 + ps * (i & 1);
> -		tst_resm(TINFO, "mremap %p to %p", old_addr, new_addr);
> +		tst_res(TINFO, "mremap %p to %p", old_addr, new_addr);
>  
>  		p4 = mremap(old_addr, size - ps, size - ps,
>  			    MREMAP_FIXED | MREMAP_MAYMOVE, new_addr);
>  		if (p4 == MAP_FAILED)
> -			tst_brkm(TBROK | TERRNO, cleanup, "mremap");
> +			tst_brk(TBROK | TERRNO, "mremap");
>  		if (memcmp(p4, p2, size - ps))
> -			tst_brkm(TBROK, cleanup, "mremap bug");
> +			tst_brk(TBROK, "mremap bug");
>  	}
> +
> +	tst_res(TPASS, "Still alive.");
>  }
>  
> -void setup(void)
> +static void setup(void)
>  {
>  	if (access(PATH_THP, F_OK) == -1)
> -		tst_brkm(TCONF, NULL, "THP not enabled in kernel?");
> -
> -	tst_sig(FORK, DEF_HANDLER, cleanup);
> -	TEST_PAUSE;
> +		tst_brk(TCONF, "THP not enabled in kernel?");
>  
>  	ps = sysconf(_SC_PAGESIZE);
> -	hps = read_meminfo("Hugepagesize:") * 1024;
> +	hps = SAFE_READ_MEMINFO("Hugepagesize:") * 1024;
>  	size = hps * 4;
>  }
>  
> -void cleanup(void)
> -{
> -}
> +static struct tst_test test = {
> +	.tid = "thp02",
> +	.needs_root = 1,
> +	.setup = setup,
> +	.test_all = do_mremap,
> +};
>  
>  #else
> -int main(void)
> -{
> -	tst_brkm(TCONF, NULL, "MREMAP_FIXED not present in <sys/mman.h>");
> -}
> +	TST_TEST_TCONF("MREMAP_FIXED not present in <sys/mman.h>");
>  #endif /* HAVE_MREMAP_FIXED */
> diff --git a/testcases/kernel/mem/thp/thp03.c b/testcases/kernel/mem/thp/thp03.c
> index 6b0f829..44541d9 100644
> --- a/testcases/kernel/mem/thp/thp03.c
> +++ b/testcases/kernel/mem/thp/thp03.c
> @@ -1,24 +1,15 @@
>  /*
> - * Copyright (C) 2012  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.
> + * Copyright (C) 2012-2017  Red Hat, Inc.
>   *
> - * 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.
> + * 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.
>   *
> - * 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.
> + * 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.
>   *
>   * thp03 - Case for spliting unaligned memory.
>   *       - System will panic if failed.
> @@ -46,11 +37,6 @@
>  #include <string.h>
>  #include <errno.h>
>  #include "mem.h"
> -#include "safe_macros.h"
> -#include "test.h"
> -
> -char *TCID = "thp03";
> -int TST_TOTAL = 1;
>  
>  #ifdef MADV_MERGEABLE
>  
> @@ -60,24 +46,6 @@ static long hugepage_size;
>  static long unaligned_size;
>  static long page_size;
>  
> -int main(int argc, char **argv)
> -{
> -	int lc;
> -
> -	tst_parse_opts(argc, argv, NULL, NULL);
> -
> -	setup();
> -
> -	for (lc = 0; TEST_LOOPING(lc); lc++) {
> -		tst_count = 0;
> -
> -		thp_test();
> -	}
> -	tst_resm(TPASS, "system didn't crash, pass.");
> -	cleanup();
> -	tst_exit();
> -}
> -
>  static void thp_test(void)
>  {
>  	void *p;
> @@ -85,53 +53,53 @@ static void thp_test(void)
>  	p = mmap(NULL, unaligned_size, PROT_READ | PROT_WRITE,
>  		 MAP_ANONYMOUS | MAP_PRIVATE, -1, 0);
>  	if (p == MAP_FAILED)
> -		tst_brkm(TBROK | TERRNO, cleanup, "mmap");
> +		tst_brk(TBROK | TERRNO, "mmap");

Use SAFE_MMAP() here instead.

>  	memset(p, 0x00, unaligned_size);
>  	if (mprotect(p, unaligned_size, PROT_NONE) == -1)
> -		tst_brkm(TBROK | TERRNO, cleanup, "mprotect");
> +		tst_brk(TBROK | TERRNO, "mprotect");
>  
>  	if (madvise(p + hugepage_size, page_size, MADV_MERGEABLE) == -1) {
>  		if (errno == EINVAL) {
> -			tst_brkm(TCONF, cleanup,
> +			tst_brk(TCONF,
>  			         "MADV_MERGEABLE is not enabled/supported");
>  		} else {
> -			tst_brkm(TBROK | TERRNO, cleanup, "madvise");
> +			tst_brk(TBROK | TERRNO, "madvise");
>  		}
>  	}
>  
> -	switch (fork()) {
> -	case -1:
> -		tst_brkm(TBROK | TERRNO, cleanup, "fork");
> +	switch (SAFE_FORK()) {
>  	case 0:
>  		exit(0);
>  	default:
>  		if (waitpid(-1, NULL, 0) == -1)
> -			tst_brkm(TBROK | TERRNO, cleanup, "waitpid");
> +			tst_brk(TBROK | TERRNO, "waitpid");

And SAFE_WAITPID() here.

>  	}
> +
> +	tst_res(TPASS, "system didn't crash, pass.");
> +	munmap(p, unaligned_size);
>  }
>  
> -void setup(void)
> +static void setup(void)
>  {
>  	if (access(PATH_THP, F_OK) == -1)
> -		tst_brkm(TCONF, NULL, "THP not enabled in kernel?");
> +		tst_brk(TCONF, "THP not enabled in kernel?");
>  
> -	hugepage_size = read_meminfo("Hugepagesize:") * KB;
> +	hugepage_size = SAFE_READ_MEMINFO("Hugepagesize:") * KB;
>  	unaligned_size = hugepage_size * 4 - 1;
> -	page_size = SAFE_SYSCONF(NULL, _SC_PAGESIZE);
> -
> -	tst_sig(FORK, DEF_HANDLER, cleanup);
> -	TEST_PAUSE;
> +	page_size = SAFE_SYSCONF(_SC_PAGESIZE);
>  }
>  
> -void cleanup(void)
> -{
> -}
> +static struct tst_test test = {
> +	.tid = "thp03",
> +	.needs_root = 1,
> +	.forks_child = 1,
> +	.setup = setup,
> +	.test_all = thp_test,
> +};
>  
>  #else
> -int main(void)
> -{
> -	tst_brkm(TCONF, NULL, "Kernel doesn't support MADV_MERGEABLE"
> +	TST_TEST_TCONF("Kernel doesn't support MADV_MERGEABLE"
>  		 " or you need to update your glibc-headers");

Hmm, if we get here the problem was MADV_MERGEABLE missing in system
headers. The kernel part is checked at runtime (that is the EINVAL check
from madvise()).

And we have lapi/mmap.h with fallback definition for it, so what about
we get rid of the ifdefs and include the fallback header lapi/mmap.h
instead?

>  }
>  #endif

-- 
Cyril Hrubis
chrubis@suse.cz

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

* [LTP] [PATCH V2 04/14] mem/ksm: convert to new API
  2017-07-07 14:49   ` Cyril Hrubis
@ 2017-07-07 15:29     ` Cyril Hrubis
  2017-07-10  7:33     ` Li Wang
  1 sibling, 0 replies; 25+ messages in thread
From: Cyril Hrubis @ 2017-07-07 15:29 UTC (permalink / raw
  To: ltp

Hi!
> > -	tst_parse_opts(argc, argv, ksm_options, ksm_usage);
> > -	setup();
> > -	for (lc = 0; TEST_LOOPING(lc); lc++) {
> > -		tst_count = 0;
> > -		check_ksm_options(&size, &num, &unit);
> > -		create_same_memory(size, num, unit);
> > -	}
> > -	cleanup();
> > -	tst_exit();
> > +	check_ksm_options(&size, &num, &unit);
> 
> There is no need to parse the test options on every test iteration, this
> should be called once in the test setup.
> 
> Moreover passing things around in global variables is messy. What about
> we (ideally in a separate patch) created a function(s) to parse the KSM
> options and pass the string values explicitily.

Or since we are about to create ksm_common.h anyway, we can add static
inline functions to parse the test options there as well. And FYI we do
have option parsing helpers in the tst_test.h library that are much
better than plain old atoi().

> setup(void)
> {
> 	...
> 	parse_ksm_options(str_size, &size, str_num, &num, str_unit, &unit);
> 	...
> }
> 
> Also could we define the ksm_options array in one place instead of
> repeating it in each test. What about putting it into ksh_common.h in
> the mem/ksm/ directory and including it in each test?
> 
> -- 
> Cyril Hrubis
> chrubis@suse.cz
> 
> -- 
> Mailing list info: https://lists.linux.it/listinfo/ltp

-- 
Cyril Hrubis
chrubis@suse.cz

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

* [LTP] [PATCH V2 03/14] mem/oom: fix the timeout issue
  2017-07-07 15:11   ` Cyril Hrubis
@ 2017-07-10  7:01     ` Li Wang
  2017-07-13  9:06       ` Richard Palethorpe
  2017-07-10  7:38     ` Jan Stancek
  1 sibling, 1 reply; 25+ messages in thread
From: Li Wang @ 2017-07-10  7:01 UTC (permalink / raw
  To: ltp

On Fri, Jul 7, 2017 at 11:11 PM, Cyril Hrubis <chrubis@suse.cz> wrote:
> Hi!
>> OOM spend time is depend on amount of RAM+Swap, it's hard to get a standard
>> to measure how long it takes, so that we cann't set tst_test->timeout easily.
>>
>> In this patch, we divide the tests into two part:
>>
>> 1. OOM takes original way on small(mem_total <= 100G) RAM, and with setting
>>    the .timeout according to the worst spending on 100G system. Here gives
>>    double(.timeout = 1200) cost in order to make sure time is long enough.
>>
>> 2. If system RAM is larger than 100G, OOM goes to the cgroup limited way.
>
> I wonder if it's really OK to limit these tests to 100G of RAM. Since as
> it is these tests can really stress the system on low memory conditions,
> if we limit them to run inside of a cgroup we are testing cgroup
> implementation instead, which is we should probably do as well.

Sounds reasonable.

>
> I'm starting to think that OOM tests are special enough so that we can
> add functionality to disable timeouts for these, i.e. set the .timeout
> to -1 and change the test library so that the alarm() is not set in such
> case.

Have to say this idea is better. it keeps the tests original and simple enough.

I draft a patch for it:
-------------------------

diff --git a/lib/tst_test.c b/lib/tst_test.c
index 16ea64f..94d951b 100644
--- a/lib/tst_test.c
+++ b/lib/tst_test.c
@@ -917,10 +917,12 @@ void tst_run_tcases(int argc, char *argv[],
struct tst_test *self)
        SAFE_SIGNAL(SIGALRM, alarm_handler);
        SAFE_SIGNAL(SIGUSR1, heartbeat_handler);

-       if (tst_test->timeout)
-               tst_set_timeout(tst_test->timeout);
-       else
+       if (tst_test->timeout) {
+               if (tst_test->timeout > 0)
+                       tst_set_timeout(tst_test->timeout);
+       } else {
                tst_set_timeout(300);
+       }

        SAFE_SIGNAL(SIGINT, sigint_handler);

diff --git a/testcases/kernel/mem/oom/oom01.c b/testcases/kernel/mem/oom/oom01.c
index 64a368e..9bc5e57 100644
--- a/testcases/kernel/mem/oom/oom01.c
+++ b/testcases/kernel/mem/oom/oom01.c
@@ -62,6 +62,7 @@ static struct tst_test test = {
        .tid = "oom01",
        .needs_root = 1,
        .forks_child = 1,
+       .timeout = -1,
        .setup = setup,
        .cleanup = cleanup,
        .test_all = verify_oom,


>
> Jan do you have anything to add?



-- 
Li Wang
liwang@redhat.com

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

* [LTP] [PATCH V2 04/14] mem/ksm: convert to new API
  2017-07-07 14:49   ` Cyril Hrubis
  2017-07-07 15:29     ` Cyril Hrubis
@ 2017-07-10  7:33     ` Li Wang
  1 sibling, 0 replies; 25+ messages in thread
From: Li Wang @ 2017-07-10  7:33 UTC (permalink / raw
  To: ltp

On Fri, Jul 7, 2017 at 10:49 PM, Cyril Hrubis <chrubis@suse.cz> wrote:
> Hi!
>> -     tst_parse_opts(argc, argv, ksm_options, ksm_usage);
>> -     setup();
>> -     for (lc = 0; TEST_LOOPING(lc); lc++) {
>> -             tst_count = 0;
>> -             check_ksm_options(&size, &num, &unit);
>> -             create_same_memory(size, num, unit);
>> -     }
>> -     cleanup();
>> -     tst_exit();
>> +     check_ksm_options(&size, &num, &unit);
>
> There is no need to parse the test options on every test iteration, this
> should be called once in the test setup.
>
> Moreover passing things around in global variables is messy. What about
> we (ideally in a separate patch) created a function(s) to parse the KSM
> options and pass the string values explicitily.
>
> setup(void)
> {
>         ...
>         parse_ksm_options(str_size, &size, str_num, &num, str_unit, &unit);
>         ...
> }
>
> Also could we define the ksm_options array in one place instead of
> repeating it in each test. What about putting it into ksh_common.h in
> the mem/ksm/ directory and including it in each test?

No problem. Agree with all these comments.

Let me move the common code to ksm_common.h in a new PATCH.



-- 
Li Wang
liwang@redhat.com

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

* [LTP] [PATCH V2 03/14] mem/oom: fix the timeout issue
  2017-07-07 15:11   ` Cyril Hrubis
  2017-07-10  7:01     ` Li Wang
@ 2017-07-10  7:38     ` Jan Stancek
  1 sibling, 0 replies; 25+ messages in thread
From: Jan Stancek @ 2017-07-10  7:38 UTC (permalink / raw
  To: ltp


----- Original Message -----
> Hi!
> > OOM spend time is depend on amount of RAM+Swap, it's hard to get a standard
> > to measure how long it takes, so that we cann't set tst_test->timeout
> > easily.
> > 
> > In this patch, we divide the tests into two part:
> > 
> > 1. OOM takes original way on small(mem_total <= 100G) RAM, and with setting
> >    the .timeout according to the worst spending on 100G system. Here gives
> >    double(.timeout = 1200) cost in order to make sure time is long enough.
> > 
> > 2. If system RAM is larger than 100G, OOM goes to the cgroup limited way.
> 
> I wonder if it's really OK to limit these tests to 100G of RAM. Since as
> it is these tests can really stress the system on low memory conditions,
> if we limit them to run inside of a cgroup we are testing cgroup
> implementation instead, which is we should probably do as well.
> 
> I'm starting to think that OOM tests are special enough so that we can
> add functionality to disable timeouts for these, i.e. set the .timeout
> to -1 and change the test library so that the alarm() is not set in such
> case.
> 
> Jan do you have anything to add?

I'm OK with timeout -1. 

I'm thinking if we should introduce a parameter to TCONF on large RAM systems
anyway and split mm runtest file into "fast" and "stress". The "fast" one
would contain tests that can complete in ~minutes (including oom tests with
parameter) and then "stress" one would be for all those slow that can take
several hours (oom tests with no parameter) to complete.

Regards,
Jan

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

* [LTP] [PATCH V2 05/14] mem/thp: convert to new API
  2017-07-07 15:27   ` Cyril Hrubis
@ 2017-07-11  4:32     ` Li Wang
  0 siblings, 0 replies; 25+ messages in thread
From: Li Wang @ 2017-07-11  4:32 UTC (permalink / raw
  To: ltp

On Fri, Jul 7, 2017 at 11:27 PM, Cyril Hrubis <chrubis@suse.cz> wrote:
> Hi!
>> +     switch (pid = SAFE_FORK()) {
>>               case 0:
>>                       memset(arg, 'c', length - 1);
>>                       arg[length - 1] = '\0';
>> @@ -91,29 +72,31 @@ int main(int argc, char **argv)
>>                       }
>>               default:
>>                       if (waitpid(pid, &st, 0) == -1)
>> -                             tst_brkm(TBROK | TERRNO, cleanup, "waitpid");
>> +                             tst_brk(TBROK | TERRNO, "waitpid");
>>                       if (!WIFEXITED(st) || WEXITSTATUS(st) != 0)
>> -                             tst_brkm(TBROK, cleanup,
>> -                                      "child exited abnormally");
>> -             }
>> +                             tst_brk(TBROK, "child exited abnormally");
>>       }
>
> We do have SAFE_WAITPID() as well, but in this case we can simply call
> tst_reap_children() that would take care of the exit status as well.

Great function.

>>       for (i = 0; i < 4; i++) {
>>               if (posix_memalign(&p, hps, size))
>> -                     tst_brkm(TBROK | TERRNO, cleanup, "memalign p");
>> +                     tst_brk(TBROK | TERRNO, "memalign p");
>>               if (posix_memalign(&p2, hps, size))
>> -                     tst_brkm(TBROK | TERRNO, cleanup, "memalign p2");
>> +                     tst_brk(TBROK | TERRNO, "memalign p2");
>>               if (posix_memalign(&p3, hps, size))
>> -                     tst_brkm(TBROK | TERRNO, cleanup, "memalign p3");
>> +                     tst_brk(TBROK | TERRNO, "memalign p3");
>
> The posix_memalign() returns errno hence this will print bogus values
> with TERRNO. We can either use SAFE_MEMALIGN() or possibly add
> POSIX_MEMALIGN() to the safe_macros and use that one instead.

Sure, I choose the obsolete one here.
  p = SAFE_MEMALIGN(hps, size);


>>  #else
>> -int main(void)
>> -{
>> -     tst_brkm(TCONF, NULL, "Kernel doesn't support MADV_MERGEABLE"
>> +     TST_TEST_TCONF("Kernel doesn't support MADV_MERGEABLE"
>>                " or you need to update your glibc-headers");
>
> Hmm, if we get here the problem was MADV_MERGEABLE missing in system
> headers. The kernel part is checked at runtime (that is the EINVAL check
> from madvise()).
>
> And we have lapi/mmap.h with fallback definition for it, so what about
> we get rid of the ifdefs and include the fallback header lapi/mmap.h
> instead?

Good suggestion. And no objection to the rest comments.

Thanks for reviewing patiently.

-- 
Li Wang
liwang@redhat.com

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

* [LTP] [PATCH V2 03/14] mem/oom: fix the timeout issue
  2017-07-10  7:01     ` Li Wang
@ 2017-07-13  9:06       ` Richard Palethorpe
  2017-07-14  3:16         ` Li Wang
  0 siblings, 1 reply; 25+ messages in thread
From: Richard Palethorpe @ 2017-07-13  9:06 UTC (permalink / raw
  To: ltp


Li Wang writes:

> +       if (tst_test->timeout) {
> +               if (tst_test->timeout > 0)
> +                       tst_set_timeout(tst_test->timeout);
> +       } else {
>                 tst_set_timeout(300);
> +       }

Only a minor nit, but it is easier to read without nesting, so we could have:

if (tst_test->timeout > 0) {
   tst_set_timeout(tst_test->timeout);
} else if (tst_test->timeout == 0) {
   tst_set_timeout(300);
}

-- 
Thank you,
Richard.

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

* [LTP] [PATCH V2 03/14] mem/oom: fix the timeout issue
  2017-07-13  9:06       ` Richard Palethorpe
@ 2017-07-14  3:16         ` Li Wang
  0 siblings, 0 replies; 25+ messages in thread
From: Li Wang @ 2017-07-14  3:16 UTC (permalink / raw
  To: ltp

On Thu, Jul 13, 2017 at 5:06 PM, Richard Palethorpe <rpalethorpe@suse.de> wrote:
>
> Li Wang writes:
>
>> +       if (tst_test->timeout) {
>> +               if (tst_test->timeout > 0)
>> +                       tst_set_timeout(tst_test->timeout);
>> +       } else {
>>                 tst_set_timeout(300);
>> +       }
>
> Only a minor nit, but it is easier to read without nesting, so we could have:
>
> if (tst_test->timeout > 0) {
>    tst_set_timeout(tst_test->timeout);
> } else if (tst_test->timeout == 0) {
>    tst_set_timeout(300);
> }

Yes, seems the brace is also redundant.



-- 
Li Wang
liwang@redhat.com

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

end of thread, other threads:[~2017-07-14  3:16 UTC | newest]

Thread overview: 25+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2017-04-10 10:05 [LTP] [PATCH V2 00/14] Convert LTP kernel/mem/testcase to New API Li Wang
2017-04-10 10:05 ` [LTP] [PATCH V2 01/14] mem/lib: convert to new API Li Wang
2017-04-10 10:05 ` [LTP] [PATCH V2 02/14] mem/oom: " Li Wang
2017-04-10 10:05 ` [LTP] [PATCH V2 03/14] mem/oom: fix the timeout issue Li Wang
2017-07-07 15:11   ` Cyril Hrubis
2017-07-10  7:01     ` Li Wang
2017-07-13  9:06       ` Richard Palethorpe
2017-07-14  3:16         ` Li Wang
2017-07-10  7:38     ` Jan Stancek
2017-04-10 10:05 ` [LTP] [PATCH V2 04/14] mem/ksm: convert to new API Li Wang
2017-07-07 14:49   ` Cyril Hrubis
2017-07-07 15:29     ` Cyril Hrubis
2017-07-10  7:33     ` Li Wang
2017-04-10 10:05 ` [LTP] [PATCH V2 05/14] mem/thp: " Li Wang
2017-07-07 15:27   ` Cyril Hrubis
2017-07-11  4:32     ` Li Wang
2017-04-10 10:05 ` [LTP] [PATCH V2 06/14] mem/hugetlb: " Li Wang
2017-04-10 10:05 ` [LTP] [PATCH V2 07/14] mem/hugemmap: " Li Wang
2017-04-10 10:05 ` [LTP] [PATCH V2 08/14] mem/hugeshmat: " Li Wang
2017-04-10 10:05 ` [LTP] [PATCH V2 09/14] mem/hugeshmctl: " Li Wang
2017-04-10 10:05 ` [LTP] [PATCH V2 10/14] mm/hugeshmdt: " Li Wang
2017-04-10 10:05 ` [LTP] [PATCH V2 11/14] mem/hugeshmget: " Li Wang
2017-04-10 10:05 ` [LTP] [PATCH V2 12/14] mem/cpuset: " Li Wang
2017-04-10 10:05 ` [LTP] [PATCH V2 13/14] mem/swapping: " Li Wang
2017-04-10 10:05 ` [LTP] [PATCH V2 14/14] mem/tunable: " Li Wang

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.