Skip to contentSkip to conversation

Rewrite exit_group01 test

PATCH Superseded Andrea Cervesato 2 participants 4 messages Nov 21, 2023 – Nov 22, 2023 Library code

v4 of 5 Superseded Latest is v5 →

lore
Share & tools
lore
#0 lore raw

v4 cover Rewrite exit_group01 test

From: Andrea Cervesato <[email protected]>

exit_group01 test has been completely rewritten and tst_gettid() has been added to support missing gettid() syscall from GLIBC

Andrea Cervesato (2):
Add tst_gettid wrapper around gettid syscall
Refactor exit_group01 using new API

 include/tst_pid.h                             |   8 +
 lib/tst_pid.c                                 |   5 +
 testcases/kernel/syscalls/exit_group/Makefile |   2 +
 .../kernel/syscalls/exit_group/exit_group01.c | 144 +++++++++++-------
 4 files changed, 108 insertions(+), 51 deletions(-)
Signature · 5 lines
2.35.3


-- 
Mailing list info: https://lists.linux.it/listinfo/ltp
#1 lore raw

PATCH v4 1/2 Add tst_gettid wrapper around gettid syscall

From: Andrea Cervesato <[email protected]>

---
 include/tst_pid.h | 8 ++++++++
 lib/tst_pid.c     | 5 +++++
 2 files changed, 13 insertions(+)
include/tst_pid.h +8 -0
index 774c845ce..951138abc 100644
@@ -50,4 +50,12 @@ static inline int tst_get_free_pids(void (*cleanup_fn)(void))
  */
 pid_t tst_getpid(void);
 
+/*
+ * Direct gettid() syscall. Some glibc versions cache gettid() return value
+ * which can cause confusing issues for example in processes created by
+ * direct clone() syscall (without using the glibc wrapper). Use this function
+ * whenever the current process may be a thread of the main test process.
+ */
+pid_t tst_gettid(void);
+
 #endif /* TST_PID_H__ */
lib/tst_pid.c +5 -0
index cfaa5db36..4e9dc7a52 100644
@@ -166,3 +166,8 @@ pid_t tst_getpid(void)
 {
 	return syscall(SYS_getpid);
 }
+
+pid_t tst_gettid(void)
+{
+	return syscall(SYS_gettid);
+}
Signature · 5 lines
2.35.3


-- 
Mailing list info: https://lists.linux.it/listinfo/ltp
#2 lore raw

PATCH v4 2/2 Refactor exit_group01 using new API

In reply to Andrea Cervesato

From: Andrea Cervesato <[email protected]>

We provided a different approach to exit_group() testing, spawning multiple threads inside the child and checking if they get killed with the parent process.

---
 testcases/kernel/syscalls/exit_group/Makefile |   2 +
 .../kernel/syscalls/exit_group/exit_group01.c | 144 +++++++++++-------
 2 files changed, 95 insertions(+), 51 deletions(-)
testcases/kernel/syscalls/exit_group/Makefile +2 -0
index 1273a4e9c..adbac3c51 100644
@@ -3,6 +3,8 @@
 
 top_srcdir		?= ../../../..
 
+exit_group01: CFLAGS+=-pthread
+
 include $(top_srcdir)/include/mk/testcases.mk
 
 include $(top_srcdir)/include/mk/generic_leaf_target.mk
testcases/kernel/syscalls/exit_group/exit_group01.c +93 -51

161 lines not on this page · raw message

Signature · 5 lines
2.35.3


-- 
Mailing list info: https://lists.linux.it/listinfo/ltp
#3 lore raw

Re: [LTP] [PATCH v4 2/2] Refactor exit_group01 using new API

Hello,

Quoted 72 lines · Andrea Cervesato

From: Andrea Cervesato <[email protected]>

We provided a different approach to exit_group() testing, spawning multiple threads inside the child and checking if they get killed with the parent process.

---
 testcases/kernel/syscalls/exit_group/Makefile |   2 +
 .../kernel/syscalls/exit_group/exit_group01.c | 144 +++++++++++-------
 2 files changed, 95 insertions(+), 51 deletions(-)
testcases/kernel/syscalls/exit_group/Makefile +2 -0
index 1273a4e9c..adbac3c51 100644
@@ -3,6 +3,8 @@
 
 top_srcdir		?= ../../../..
 
+exit_group01: CFLAGS+=-pthread
+
 include $(top_srcdir)/include/mk/testcases.mk
 
 include $(top_srcdir)/include/mk/generic_leaf_target.mk
testcases/kernel/syscalls/exit_group/exit_group01.c +14 -26
index 5bf5b0218..9c557eedf 100644
@@ -1,68 +1,110 @@
-/******************************************************************************
- * Copyright (c) Crackerjack Project., 2007                                   *
- * Ported to LTP by Manas Kumar Nayak <[email protected]>                   *
- * Copyright (C) 2015 Cyril Hrubis <[email protected]>                          *
- *                                                                            *
- * 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           *
- *                                                                            *
- ******************************************************************************/
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * Copyright (c) Crackerjack Project., 2007
+ * Ported to LTP by Manas Kumar Nayak <[email protected]>
+ * Copyright (c) 2015 Linux Test Project
+ * Copyright (C) 2015 Cyril Hrubis <[email protected]>
+ * Copyright (C) 2023 SUSE LLC Andrea Cervesato <[email protected]>
+ */
 
-#include <stdio.h>
-#include <errno.h>
-#include <linux/unistd.h>
-#include <sys/wait.h>
+/*\
+ * [Description]
+ *
+ * This test checks if exit_group() correctly ends a spawned child and all its
+ * running threads.
+ */
 
-#include "test.h"
-#include "safe_macros.h"

testcases/kernel/syscalls/exit_group/exit_group01.c line 23

Quoted 10 lines
testcases/kernel/syscalls/exit_group/exit_group01.c +5 -3
+#include <stdio.h>
+#include <stdlib.h>
+#include "tst_test.h"
 #include "lapi/syscalls.h"
+#include "tst_safe_pthread.h"
 
-char *TCID = "exit_group01";
-int testno;
-int TST_TOTAL = 1;
+#define THREADS_NUM 10

It occured to me while looking at the mmap code that this test is far more likely to find something if we at least set the number of threads to max(2, tst_ncpus). It also would be better for embedded systems with one CPU.

Taking that even further, if every thread has its affinity set to run on a unique core then this will force a lot of cross CPU communication. However that could go in another test.

Quoted 4 lines
testcases/kernel/syscalls/exit_group/exit_group01.c +2 -1
-static void verify_exit_group(void)
+static pid_t *tids;
+static int *counter;

testcases/kernel/syscalls/exit_group/exit_group01.c line 35

Quoted 10 lines
testcases/kernel/syscalls/exit_group/exit_group01.c +8 -1
+
+static void *worker(void *arg)
 {
-	pid_t cpid, w;
+	int i = *((int *)arg);
+
+	tids[i] = tst_gettid();
+
+	while (1) {
+		++(*counter);

The atomic function can be used when updating or reading counter.

testcases/kernel/syscalls/exit_group/exit_group01.c line 36

Quoted 1 line
testcases/kernel/syscalls/exit_group/exit_group01.c +1 -0
+		usleep(0);

nit: sched_yield better shows the intent

Quoted 69 lines
testcases/kernel/syscalls/exit_group/exit_group01.c +44 -17
+	}
+
+	return arg;
+}
+
+static void spawn_threads(void)
+{
+	pthread_t threads[THREADS_NUM];
+
+	for (int i = 0; i < THREADS_NUM; i++)
+		SAFE_PTHREAD_CREATE(&threads[i], NULL, worker, (void *)&i);
+}
+
+static void run(void)
+{
+	pid_t pid;
 	int status;
 
-	cpid = fork();
-	if (cpid == -1)
-		tst_brkm(TFAIL | TERRNO, NULL, "fork failed");
+	*counter = 0;
+
+	pid = SAFE_FORK();
+	if (!pid) {
+		spawn_threads();
 
-	if (cpid == 0) {
 		TEST(tst_syscall(__NR_exit_group, 4));
-	} else {
-		w = SAFE_WAIT(NULL, &status);
-
-		if (WIFEXITED(status) && (WEXITSTATUS(status) == 4)) {
-			tst_resm(TPASS, "exit_group() succeeded");
-		} else {
-			tst_resm(TFAIL | TERRNO,
-				 "exit_group() failed (wait status = %d)", w);
-		}
+		if (TST_RET == -1)
+			tst_brk(TBROK | TERRNO, "exit_group() error");
+
+		return;
 	}
+
+	SAFE_WAITPID(pid, &status, 0);
+
+	TST_EXP_EXPR(WIFEXITED(status) && WEXITSTATUS(status) == 4,
+		"exit_group() succeeded");
+
+	int old_counter = *counter;
+
+	tst_res(TINFO, "Checking if threads are still running");
+	usleep(1000000);
+
+	TST_EXP_EXPR(old_counter == *counter, "Counter value hasn't changed");
 }
 
-int main(int ac, char **av)
+static void setup(void)
 {
-	int lc;
-
-	tst_parse_opts(ac, av, NULL, NULL);
+	tids = SAFE_MMAP(
+		NULL,
+		sizeof(pid_t) * THREADS_NUM,
+		PROT_READ | PROT_WRITE,
+		MAP_SHARED | MAP_ANONYMOUS,
+		-1, 0);

testcases/kernel/syscalls/exit_group/exit_group01.c line 96

Quoted 10 lines
testcases/kernel/syscalls/exit_group/exit_group01.c +7 -2
 
-	for (lc = 0; TEST_LOOPING(lc); lc++)
-		verify_exit_group();
+	counter = SAFE_MMAP(
+		NULL,
+		sizeof(int),
+		PROT_READ | PROT_WRITE,
+		MAP_SHARED | MAP_ANONYMOUS,
+		-1, 0);
+}

nit; mmap always maps at least a page, so calling it twice is wasteful.

If we are setting up shared memory with more than one variable in then the cleanest way to do it is to create a struct with the variables in e.g.

struct shm {
       int counter;
       pid_t pid;
};

struct shm *shm = SAFE_MMAP(0, 2 * sizeof(struct shm) * tst_ncpus(), ...); struct shm *copy = shm + tst_ncpus()

Also if each thread has its own counter then we won't have all the CPUs fighting to atomically update the memory which could be an issue on a machine with >128 cores.

Then you can memcpy the whole shm and memcmp it after the delay.

Quoted 17 lines
testcases/kernel/syscalls/exit_group/exit_group01.c +12 -1
-	tst_exit();
+static void cleanup(void)
+{
+	SAFE_MUNMAP(tids, sizeof(pid_t) * THREADS_NUM);
+	SAFE_MUNMAP(counter, sizeof(int));
 }
+
+static struct tst_test test = {
+	.setup = setup,
+	.cleanup = cleanup,
+	.test_all = run,
+	.forks_child = 1,
+	.needs_checkpoints = 1,
+};
Signature · 1 line
2.35.3
Signature · 5 lines
Thank you,
Richard.

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