Skip to contentSkip to conversation

Add tls parameter and flag:CLONE_SETTLS cover for clone and clone3 syscall

PATCH Superseded chunfuwen via ltp 1 participant 5 messages Apr 22, 2025 – Apr 28, 2025

v1 of 17 Superseded Latest is v17 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 Add tls parameter and flag:CLONE_SETTLS cover for clone and clone3 syscall

tls parameter and related flag:CLONE_SETTLS are missed in the testing, so add them into existed test case

---
 testcases/kernel/syscalls/clone/clone08.c   | 40 ++++++++++++++++++---
 testcases/kernel/syscalls/clone3/clone301.c | 36 ++++++++++++++++---
 2 files changed, 67 insertions(+), 9 deletions(-)
testcases/kernel/syscalls/clone/clone08.c +35 -5
index dd97f3ff1..f5a8119a8 100644
@@ -17,7 +17,11 @@
 #include "lapi/syscalls.h"
 #include "lapi/futex.h"
 
+#define TLS_SIZE 4096
+#define TLS_ALIGN 16
+
 static pid_t ptid, ctid, tgid;
+static void *tls_ptr;
 static void *child_stack;
 
 static void test_clone_parent(int t);
@@ -32,6 +36,9 @@ static int child_clone_parent_settid(void *);
 static void test_clone_thread(int t);
 static int child_clone_thread(void *);
 
+/* TLS variable to validate in child */
+static __thread int tls_test_var = 12345;
+
 /*
  * Children cloned with CLONE_VM should avoid using any functions that
  * might require dl_runtime_resolve, because they share thread-local
@@ -46,14 +53,14 @@ static struct test_case {
 	void (*testfunc)(int);
 	int (*do_child)(void *);
 } test_cases[] = {
-	{"CLONE_PARENT", CLONE_PARENT | SIGCHLD,
+	{"CLONE_PARENT", CLONE_PARENT | CLONE_SETTLS | SIGCHLD,
 	 test_clone_parent, child_clone_parent},
-	{"CLONE_CHILD_SETTID", CLONE_CHILD_SETTID | SIGCHLD,
+	{"CLONE_CHILD_SETTID", CLONE_CHILD_SETTID | CLONE_SETTLS | SIGCHLD,
 	 test_clone_tid, child_clone_child_settid},
-	{"CLONE_PARENT_SETTID", CLONE_PARENT_SETTID | CLONE_VM | SIGCHLD,
+	{"CLONE_PARENT_SETTID", CLONE_PARENT_SETTID | CLONE_VM | CLONE_SETTLS | SIGCHLD,
 	 test_clone_tid, child_clone_parent_settid},
 	{"CLONE_THREAD", CLONE_THREAD | CLONE_SIGHAND | CLONE_VM |
-	 CLONE_CHILD_CLEARTID | SIGCHLD,
+	 CLONE_CHILD_CLEARTID | CLONE_SETTLS | SIGCHLD,
 	 test_clone_thread, child_clone_thread},
 };
 
@@ -66,17 +73,24 @@ static void do_test(unsigned int i)
 static void setup(void)
 {
 	child_stack = SAFE_MALLOC(CHILD_STACK_SIZE);
+	tls_ptr = aligned_alloc(TLS_ALIGN, TLS_SIZE);
+	if (!tls_ptr) {
+		perror("aligned_alloc");
+		exit(EXIT_FAILURE);
+	}
+	memset(tls_ptr, 0, TLS_SIZE);
 }
 
 static void cleanup(void)
 {
 	free(child_stack);
+	free(tls_ptr);
 }
 
 static long clone_child(const struct test_case *t)
 {
 	TEST(ltp_clone7(t->flags, t->do_child, NULL, CHILD_STACK_SIZE,
-		child_stack, &ptid, NULL, &ctid));
+		child_stack, &ptid, tls_ptr, &ctid));
 
 	if (TST_RET == -1 && TTERRNO == ENOSYS)
 		tst_brk(TCONF, "clone does not support 7 args");
@@ -87,6 +101,14 @@ static long clone_child(const struct test_case *t)
 	return TST_RET;
 }
 
+static void validate_tls(void)
+{
+	if (tls_test_var == 12345)
+		tst_res(TPASS, "Child TLS variable has expected value");
+	else
+		tst_res(TFAIL, "Child TLS variable corrupted or not set");
+}
+
 static void test_clone_parent(int t)
 {
 	pid_t child;
@@ -102,6 +124,8 @@ static void test_clone_parent(int t)
 
 static int child_clone_parent(void *arg LTP_ATTRIBUTE_UNUSED)
 {
+	validate_tls();
+
 	if (parent_ppid == getppid()) {
 		tst_res(TPASS, "clone and forked child has the same parent");
 	} else {
@@ -120,6 +144,8 @@ static void test_clone_tid(int t)
 
 static int child_clone_child_settid(void *arg LTP_ATTRIBUTE_UNUSED)
 {
+	validate_tls();
+
 	if (ctid == tst_syscall(__NR_getpid))
 		tst_res(TPASS, "clone() correctly set ctid");
 	else
@@ -130,6 +156,8 @@ static int child_clone_child_settid(void *arg LTP_ATTRIBUTE_UNUSED)
 
 static int child_clone_parent_settid(void *arg LTP_ATTRIBUTE_UNUSED)
 {
+	validate_tls();
+
 	if (ptid == tst_syscall(__NR_getpid))
 		tst_res(TPASS, "clone() correctly set ptid");
 	else
@@ -175,6 +203,8 @@ static void test_clone_thread(int t)
 
 static int child_clone_thread(void *arg LTP_ATTRIBUTE_UNUSED)
 {
+	validate_tls();
+
 	if (tgid == tst_syscall(__NR_getpid))
 		tst_res(TPASS, "clone has the same thread id");
 	else
testcases/kernel/syscalls/clone3/clone301.c +32 -4
index deed30b9f..9571c9f5c 100644
@@ -18,22 +18,38 @@
 
 #define CHILD_SIGNAL	SIGUSR1
 #define DATA	777
+#define TLS_SIZE 4096
+#define TLS_ALIGN 16
 
 static int pidfd, child_tid, parent_tid, parent_received_signal;
 static volatile int child_received_signal, child_data;
 static struct clone_args *args;
 
+/* TLS variable to validate in child */
+static __thread int tls_test_var = 12345;
+
 static struct tcase {
 	uint64_t flags;
 	int exit_signal;
 } tcases[] = {
 	{0, SIGCHLD},
 	{0, SIGUSR2},
-	{CLONE_FS, SIGCHLD},
-	{CLONE_NEWPID, SIGCHLD},
-	{CLONE_PARENT_SETTID | CLONE_CHILD_SETTID | CLONE_PIDFD, SIGCHLD},
+	{CLONE_FS | CLONE_SETTLS, SIGCHLD},
+	{CLONE_NEWPID | CLONE_SETTLS, SIGCHLD},
+	{CLONE_PARENT_SETTID | CLONE_CHILD_SETTID | CLONE_PIDFD | CLONE_SETTLS, SIGCHLD},
 };
 
+static void *allocate_tls_region(void)
+{
+	void *tls_area = aligned_alloc(TLS_ALIGN, TLS_SIZE);
+	if (!tls_area) {
+		perror("aligned_alloc");
+		exit(EXIT_FAILURE);
+	}
+	memset(tls_area, 0, TLS_SIZE);
+	return tls_area;
+}
+
 static void parent_rx_signal(int sig)
 {
 	parent_received_signal = sig;
@@ -67,6 +83,13 @@ static void do_child(int clone_pidfd)
 {
 	int count = 1000;
 
+	/* Validate TLS usage */
+	if (tls_test_var == 12345) {
+		tst_res(TPASS, "Child TLS variable has expected value");
+	} else {
+		tst_res(TFAIL, "Child TLS variable corrupted or not set");
+	}
+
 	if (clone_pidfd) {
 		child_received_signal = 0;
 		child_data = 0;
@@ -111,6 +134,8 @@ static void run(unsigned int n)
 	int status, clone_pidfd = tc->flags & CLONE_PIDFD;
 	pid_t pid;
 
+	void *tls_ptr = allocate_tls_region();
+
 	args->flags = tc->flags;
 	args->pidfd = (uint64_t)(&pidfd);
 	args->child_tid = (uint64_t)(&child_tid);
@@ -118,7 +143,7 @@ static void run(unsigned int n)
 	args->exit_signal = tc->exit_signal;
 	args->stack = 0;
 	args->stack_size = 0;
-	args->tls = 0;
+	args->tls = (uint64_t)tls_ptr;
 
 	parent_received_signal = 0;
 	SAFE_SIGACTION(tc->exit_signal, &psig_action, NULL);
@@ -126,6 +151,7 @@ static void run(unsigned int n)
 	TEST(pid = clone3(args, sizeof(*args)));
 	if (pid < 0) {
 		tst_res(TFAIL | TTERRNO, "clone3() failed (%d)", n);
+		free(tls_ptr);
 		return;
 	}
 
@@ -139,11 +165,13 @@ static void run(unsigned int n)
 		TEST(pidfd_send_signal(pidfd, CHILD_SIGNAL, &uinfo, 0));
 		if (TST_RET != 0) {
 			tst_res(TFAIL | TTERRNO, "pidfd_send_signal() failed");
+			free(tls_ptr);
 			return;
 		}
 	}
 
 	SAFE_WAITPID(pid, &status, __WALL);
+	free(tls_ptr);
 
 	if (!parent_received_signal) {
 		tst_res(TFAIL, "Parent haven't got signal");
Signature · 5 lines
2.43.5


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

Hello Maintainers,

Could someone please drop a review of this patch?

Best Regards,
Chunfu Wen

Quoted 246 lines · chunfuwen via ltp

tls parameter and related flag:CLONE_SETTLS are missed in the testing, so add them into existed test case

---
 testcases/kernel/syscalls/clone/clone08.c   | 40 ++++++++++++++++++---
 testcases/kernel/syscalls/clone3/clone301.c | 36 ++++++++++++++++---
 2 files changed, 67 insertions(+), 9 deletions(-)
testcases/kernel/syscalls/clone/clone08.c +29 -5
index dd97f3ff1..f5a8119a8 100644
@@ -17,7 +17,11 @@
 #include "lapi/syscalls.h"
 #include "lapi/futex.h"
 
+#define TLS_SIZE 4096
+#define TLS_ALIGN 16
+
 static pid_t ptid, ctid, tgid;
+static void *tls_ptr;
 static void *child_stack;
 
 static void test_clone_parent(int t);
@@ -32,6 +36,9 @@ static int child_clone_parent_settid(void *);
 static void test_clone_thread(int t);
 static int child_clone_thread(void *);
 
+/* TLS variable to validate in child */
+static __thread int tls_test_var = 12345;
+
 /*
  * Children cloned with CLONE_VM should avoid using any functions that
  * might require dl_runtime_resolve, because they share thread-local
@@ -46,14 +53,14 @@ static struct test_case {
        void (*testfunc)(int);
        int (*do_child)(void *);
 } test_cases[] = {
-       {"CLONE_PARENT", CLONE_PARENT | SIGCHLD,
+       {"CLONE_PARENT", CLONE_PARENT | CLONE_SETTLS | SIGCHLD,
         test_clone_parent, child_clone_parent},
-       {"CLONE_CHILD_SETTID", CLONE_CHILD_SETTID | SIGCHLD,
+       {"CLONE_CHILD_SETTID", CLONE_CHILD_SETTID | CLONE_SETTLS | SIGCHLD,
         test_clone_tid, child_clone_child_settid},
-       {"CLONE_PARENT_SETTID", CLONE_PARENT_SETTID | CLONE_VM | SIGCHLD,
+       {"CLONE_PARENT_SETTID", CLONE_PARENT_SETTID | CLONE_VM |
 CLONE_SETTLS | SIGCHLD,
         test_clone_tid, child_clone_parent_settid},
        {"CLONE_THREAD", CLONE_THREAD | CLONE_SIGHAND | CLONE_VM |
-        CLONE_CHILD_CLEARTID | SIGCHLD,
+        CLONE_CHILD_CLEARTID | CLONE_SETTLS | SIGCHLD,
         test_clone_thread, child_clone_thread},
 };
@@ -66,17 +73,24 @@ static void do_test(unsigned int i)
 static void setup(void)
 {
        child_stack = SAFE_MALLOC(CHILD_STACK_SIZE);
+       tls_ptr = aligned_alloc(TLS_ALIGN, TLS_SIZE);
+       if (!tls_ptr) {
+               perror("aligned_alloc");
+               exit(EXIT_FAILURE);
+       }
+       memset(tls_ptr, 0, TLS_SIZE);
 }
 
 static void cleanup(void)
 {
        free(child_stack);
+       free(tls_ptr);
 }
 
 static long clone_child(const struct test_case *t)
 {
        TEST(ltp_clone7(t->flags, t->do_child, NULL, CHILD_STACK_SIZE,
-               child_stack, &ptid, NULL, &ctid));
+               child_stack, &ptid, tls_ptr, &ctid));
 
        if (TST_RET == -1 && TTERRNO == ENOSYS)
                tst_brk(TCONF, "clone does not support 7 args");
@@ -87,6 +101,14 @@ static long clone_child(const struct test_case *t)
        return TST_RET;
 }
 
+static void validate_tls(void)
+{
+       if (tls_test_var == 12345)
+               tst_res(TPASS, "Child TLS variable has expected value");
+       else
+               tst_res(TFAIL, "Child TLS variable corrupted or not set");
+}
+
 static void test_clone_parent(int t)
 {
        pid_t child;
@@ -102,6 +124,8 @@ static void test_clone_parent(int t)
 
 static int child_clone_parent(void *arg LTP_ATTRIBUTE_UNUSED)
 {
+       validate_tls();
+
        if (parent_ppid == getppid()) {
                tst_res(TPASS, "clone and forked child has the same
 parent");
        } else {
testcases/kernel/syscalls/clone/clone08.c +4 -0
@@ -120,6 +144,8 @@ static void test_clone_tid(int t)
 
 static int child_clone_child_settid(void *arg LTP_ATTRIBUTE_UNUSED)
 {
+       validate_tls();
+
        if (ctid == tst_syscall(__NR_getpid))
                tst_res(TPASS, "clone() correctly set ctid");
        else
@@ -130,6 +156,8 @@ static int child_clone_child_settid(void *arg
 LTP_ATTRIBUTE_UNUSED)
 
 static int child_clone_parent_settid(void *arg LTP_ATTRIBUTE_UNUSED)
 {
+       validate_tls();
+
        if (ptid == tst_syscall(__NR_getpid))
                tst_res(TPASS, "clone() correctly set ptid");

else

testcases/kernel/syscalls/clone/clone08.c +2 -0
@@ -175,6 +203,8 @@ static void test_clone_thread(int t)
 
 static int child_clone_thread(void *arg LTP_ATTRIBUTE_UNUSED)
 {
+       validate_tls();
+
        if (tgid == tst_syscall(__NR_getpid))
                tst_res(TPASS, "clone has the same thread id");
        else
testcases/kernel/syscalls/clone3/clone301.c +19 -3
index deed30b9f..9571c9f5c 100644
@@ -18,22 +18,38 @@
 
 #define CHILD_SIGNAL   SIGUSR1
 #define DATA   777
+#define TLS_SIZE 4096
+#define TLS_ALIGN 16
 
 static int pidfd, child_tid, parent_tid, parent_received_signal;
 static volatile int child_received_signal, child_data;
 static struct clone_args *args;
 
+/* TLS variable to validate in child */
+static __thread int tls_test_var = 12345;
+
 static struct tcase {
        uint64_t flags;
        int exit_signal;
 } tcases[] = {
        {0, SIGCHLD},
        {0, SIGUSR2},
-       {CLONE_FS, SIGCHLD},
-       {CLONE_NEWPID, SIGCHLD},
-       {CLONE_PARENT_SETTID | CLONE_CHILD_SETTID | CLONE_PIDFD, SIGCHLD},
+       {CLONE_FS | CLONE_SETTLS, SIGCHLD},
+       {CLONE_NEWPID | CLONE_SETTLS, SIGCHLD},
+       {CLONE_PARENT_SETTID | CLONE_CHILD_SETTID | CLONE_PIDFD |
 CLONE_SETTLS, SIGCHLD},
 };
 
+static void *allocate_tls_region(void)
+{
+       void *tls_area = aligned_alloc(TLS_ALIGN, TLS_SIZE);
+       if (!tls_area) {
+               perror("aligned_alloc");
+               exit(EXIT_FAILURE);
+       }
+       memset(tls_area, 0, TLS_SIZE);
+       return tls_area;
+}
+
 static void parent_rx_signal(int sig)
 {
        parent_received_signal = sig;
testcases/kernel/syscalls/clone3/clone301.c +13 -1
@@ -67,6 +83,13 @@ static void do_child(int clone_pidfd)
 {
        int count = 1000;
 
+       /* Validate TLS usage */
+       if (tls_test_var == 12345) {
+               tst_res(TPASS, "Child TLS variable has expected value");
+       } else {
+               tst_res(TFAIL, "Child TLS variable corrupted or not set");
+       }
+
        if (clone_pidfd) {
                child_received_signal = 0;
                child_data = 0;
@@ -111,6 +134,8 @@ static void run(unsigned int n)
        int status, clone_pidfd = tc->flags & CLONE_PIDFD;
        pid_t pid;
 
+       void *tls_ptr = allocate_tls_region();
+
        args->flags = tc->flags;
        args->pidfd = (uint64_t)(&pidfd);
        args->child_tid = (uint64_t)(&child_tid);
@@ -118,7 +143,7 @@ static void run(unsigned int n)
        args->exit_signal = tc->exit_signal;
        args->stack = 0;
        args->stack_size = 0;
-       args->tls = 0;
+       args->tls = (uint64_t)tls_ptr;
 
        parent_received_signal = 0;
        SAFE_SIGACTION(tc->exit_signal, &psig_action, NULL);
@@ -126,6 +151,7 @@ static void run(unsigned int n)
        TEST(pid = clone3(args, sizeof(*args)));
        if (pid < 0) {
                tst_res(TFAIL | TTERRNO, "clone3() failed (%d)", n);
+               free(tls_ptr);
                return;
        }
 
@@ -139,11 +165,13 @@ static void run(unsigned int n)
                TEST(pidfd_send_signal(pidfd, CHILD_SIGNAL, &uinfo, 0));
                if (TST_RET != 0) {
                        tst_res(TFAIL | TTERRNO, "pidfd_send_signal()
 failed");
+                       free(tls_ptr);
                        return;
                }
        }
 
        SAFE_WAITPID(pid, &status, __WALL);
+       free(tls_ptr);
 
        if (!parent_received_signal) {
                tst_res(TFAIL, "Parent haven't got signal");

--
2.43.5

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

Signature · 1 line
Mailing list info: https://lists.linux.it/listinfo/ltp
#2 lore raw
In reply to chunfuwen via ltp

Hi Chunfu,

According to the manual page:

"CLONE_SETTLS (since Linux 2.5.32):
The interpretation of tls and the resulting effect is architecture dependent.
On x86, tls is interpreted as a struct user_desc * (see set_thread_area(2)).
On x86-64 it is the new value to be set for the %fs base register (see the ARCH_SET_FS argument to arch_prctl(2)). On architectures with a dedicated TLS register, it is the new value of that register..."

I'm afraid we shouldn't test CLONE_SETTLS in this way, the tls interpretation
is arch-dependent, it could fail or behave incorrectly on 32-bit x86 or non-x86
systems.

And, clone08 was originally intended to test the basic behavior of clone (such as
TID settings and parent-child relationships) and should not introduce complex
architecture-related logic.

So, maybe you can create a new clone10.c file to write a dedicated test for CLONE_SETTLS and add architecture-specific handling.

Quoted 246 lines · chunfuwen via ltp

tls parameter and related flag:CLONE_SETTLS are missed in the testing, so add them into existed test case

---
 testcases/kernel/syscalls/clone/clone08.c   | 40 ++++++++++++++++++---
 testcases/kernel/syscalls/clone3/clone301.c | 36 ++++++++++++++++---
 2 files changed, 67 insertions(+), 9 deletions(-)
testcases/kernel/syscalls/clone/clone08.c +29 -5
index dd97f3ff1..f5a8119a8 100644
@@ -17,7 +17,11 @@
 #include "lapi/syscalls.h"
 #include "lapi/futex.h"
 
+#define TLS_SIZE 4096
+#define TLS_ALIGN 16
+
 static pid_t ptid, ctid, tgid;
+static void *tls_ptr;
 static void *child_stack;
 
 static void test_clone_parent(int t);
@@ -32,6 +36,9 @@ static int child_clone_parent_settid(void *);
 static void test_clone_thread(int t);
 static int child_clone_thread(void *);
 
+/* TLS variable to validate in child */
+static __thread int tls_test_var = 12345;
+
 /*
  * Children cloned with CLONE_VM should avoid using any functions that
  * might require dl_runtime_resolve, because they share thread-local
@@ -46,14 +53,14 @@ static struct test_case {
        void (*testfunc)(int);
        int (*do_child)(void *);
 } test_cases[] = {
-       {"CLONE_PARENT", CLONE_PARENT | SIGCHLD,
+       {"CLONE_PARENT", CLONE_PARENT | CLONE_SETTLS | SIGCHLD,
         test_clone_parent, child_clone_parent},
-       {"CLONE_CHILD_SETTID", CLONE_CHILD_SETTID | SIGCHLD,
+       {"CLONE_CHILD_SETTID", CLONE_CHILD_SETTID | CLONE_SETTLS | SIGCHLD,
         test_clone_tid, child_clone_child_settid},
-       {"CLONE_PARENT_SETTID", CLONE_PARENT_SETTID | CLONE_VM | SIGCHLD,
+       {"CLONE_PARENT_SETTID", CLONE_PARENT_SETTID | CLONE_VM |
 CLONE_SETTLS | SIGCHLD,
         test_clone_tid, child_clone_parent_settid},
        {"CLONE_THREAD", CLONE_THREAD | CLONE_SIGHAND | CLONE_VM |
-        CLONE_CHILD_CLEARTID | SIGCHLD,
+        CLONE_CHILD_CLEARTID | CLONE_SETTLS | SIGCHLD,
         test_clone_thread, child_clone_thread},
 };
@@ -66,17 +73,24 @@ static void do_test(unsigned int i)
 static void setup(void)
 {
        child_stack = SAFE_MALLOC(CHILD_STACK_SIZE);
+       tls_ptr = aligned_alloc(TLS_ALIGN, TLS_SIZE);
+       if (!tls_ptr) {
+               perror("aligned_alloc");
+               exit(EXIT_FAILURE);
+       }
+       memset(tls_ptr, 0, TLS_SIZE);
 }
 
 static void cleanup(void)
 {
        free(child_stack);
+       free(tls_ptr);
 }
 
 static long clone_child(const struct test_case *t)
 {
        TEST(ltp_clone7(t->flags, t->do_child, NULL, CHILD_STACK_SIZE,
-               child_stack, &ptid, NULL, &ctid));
+               child_stack, &ptid, tls_ptr, &ctid));
 
        if (TST_RET == -1 && TTERRNO == ENOSYS)
                tst_brk(TCONF, "clone does not support 7 args");
@@ -87,6 +101,14 @@ static long clone_child(const struct test_case *t)
        return TST_RET;
 }
 
+static void validate_tls(void)
+{
+       if (tls_test_var == 12345)
+               tst_res(TPASS, "Child TLS variable has expected value");
+       else
+               tst_res(TFAIL, "Child TLS variable corrupted or not set");
+}
+
 static void test_clone_parent(int t)
 {
        pid_t child;
@@ -102,6 +124,8 @@ static void test_clone_parent(int t)
 
 static int child_clone_parent(void *arg LTP_ATTRIBUTE_UNUSED)
 {
+       validate_tls();
+
        if (parent_ppid == getppid()) {
                tst_res(TPASS, "clone and forked child has the same
 parent");
        } else {
testcases/kernel/syscalls/clone/clone08.c +4 -0
@@ -120,6 +144,8 @@ static void test_clone_tid(int t)
 
 static int child_clone_child_settid(void *arg LTP_ATTRIBUTE_UNUSED)
 {
+       validate_tls();
+
        if (ctid == tst_syscall(__NR_getpid))
                tst_res(TPASS, "clone() correctly set ctid");
        else
@@ -130,6 +156,8 @@ static int child_clone_child_settid(void *arg
 LTP_ATTRIBUTE_UNUSED)
 
 static int child_clone_parent_settid(void *arg LTP_ATTRIBUTE_UNUSED)
 {
+       validate_tls();
+
        if (ptid == tst_syscall(__NR_getpid))
                tst_res(TPASS, "clone() correctly set ptid");

else

testcases/kernel/syscalls/clone/clone08.c +2 -0
@@ -175,6 +203,8 @@ static void test_clone_thread(int t)
 
 static int child_clone_thread(void *arg LTP_ATTRIBUTE_UNUSED)
 {
+       validate_tls();
+
        if (tgid == tst_syscall(__NR_getpid))
                tst_res(TPASS, "clone has the same thread id");
        else
testcases/kernel/syscalls/clone3/clone301.c +19 -3
index deed30b9f..9571c9f5c 100644
@@ -18,22 +18,38 @@
 
 #define CHILD_SIGNAL   SIGUSR1
 #define DATA   777
+#define TLS_SIZE 4096
+#define TLS_ALIGN 16
 
 static int pidfd, child_tid, parent_tid, parent_received_signal;
 static volatile int child_received_signal, child_data;
 static struct clone_args *args;
 
+/* TLS variable to validate in child */
+static __thread int tls_test_var = 12345;
+
 static struct tcase {
        uint64_t flags;
        int exit_signal;
 } tcases[] = {
        {0, SIGCHLD},
        {0, SIGUSR2},
-       {CLONE_FS, SIGCHLD},
-       {CLONE_NEWPID, SIGCHLD},
-       {CLONE_PARENT_SETTID | CLONE_CHILD_SETTID | CLONE_PIDFD, SIGCHLD},
+       {CLONE_FS | CLONE_SETTLS, SIGCHLD},
+       {CLONE_NEWPID | CLONE_SETTLS, SIGCHLD},
+       {CLONE_PARENT_SETTID | CLONE_CHILD_SETTID | CLONE_PIDFD |
 CLONE_SETTLS, SIGCHLD},
 };
 
+static void *allocate_tls_region(void)
+{
+       void *tls_area = aligned_alloc(TLS_ALIGN, TLS_SIZE);
+       if (!tls_area) {
+               perror("aligned_alloc");
+               exit(EXIT_FAILURE);
+       }
+       memset(tls_area, 0, TLS_SIZE);
+       return tls_area;
+}
+
 static void parent_rx_signal(int sig)
 {
        parent_received_signal = sig;
testcases/kernel/syscalls/clone3/clone301.c +13 -1
@@ -67,6 +83,13 @@ static void do_child(int clone_pidfd)
 {
        int count = 1000;
 
+       /* Validate TLS usage */
+       if (tls_test_var == 12345) {
+               tst_res(TPASS, "Child TLS variable has expected value");
+       } else {
+               tst_res(TFAIL, "Child TLS variable corrupted or not set");
+       }
+
        if (clone_pidfd) {
                child_received_signal = 0;
                child_data = 0;
@@ -111,6 +134,8 @@ static void run(unsigned int n)
        int status, clone_pidfd = tc->flags & CLONE_PIDFD;
        pid_t pid;
 
+       void *tls_ptr = allocate_tls_region();
+
        args->flags = tc->flags;
        args->pidfd = (uint64_t)(&pidfd);
        args->child_tid = (uint64_t)(&child_tid);
@@ -118,7 +143,7 @@ static void run(unsigned int n)
        args->exit_signal = tc->exit_signal;
        args->stack = 0;
        args->stack_size = 0;
-       args->tls = 0;
+       args->tls = (uint64_t)tls_ptr;
 
        parent_received_signal = 0;
        SAFE_SIGACTION(tc->exit_signal, &psig_action, NULL);
@@ -126,6 +151,7 @@ static void run(unsigned int n)
        TEST(pid = clone3(args, sizeof(*args)));
        if (pid < 0) {
                tst_res(TFAIL | TTERRNO, "clone3() failed (%d)", n);
+               free(tls_ptr);
                return;
        }
 
@@ -139,11 +165,13 @@ static void run(unsigned int n)
                TEST(pidfd_send_signal(pidfd, CHILD_SIGNAL, &uinfo, 0));
                if (TST_RET != 0) {
                        tst_res(TFAIL | TTERRNO, "pidfd_send_signal()
 failed");
+                       free(tls_ptr);
                        return;
                }
        }
 
        SAFE_WAITPID(pid, &status, __WALL);
+       free(tls_ptr);
 
        if (!parent_received_signal) {
                tst_res(TFAIL, "Parent haven't got signal");

--
2.43.5

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

Signature · 5 lines
Regards,
Li Wang

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

Hello,

Thanks for reviewing!
The below issues will be addressed in v2 patch.

Chunfu Wen

Quoted 284 lines · Li Wang

Hi Chunfu,

According to the manual page:

"CLONE_SETTLS (since Linux 2.5.32):
The interpretation of tls and the resulting effect is architecture dependent.
On x86, tls is interpreted as a struct user_desc * (see set_thread_area(2)).
On x86-64 it is the new value to be set for the %fs base register (see the ARCH_SET_FS argument to arch_prctl(2)). On architectures with a dedicated TLS register, it is the new value of that register..."

I'm afraid we shouldn't test CLONE_SETTLS in this way, the tls interpretation
is arch-dependent, it could fail or behave incorrectly on 32-bit x86 or non-x86
systems.

And, clone08 was originally intended to test the basic behavior of clone (such as
TID settings and parent-child relationships) and should not introduce complex
architecture-related logic.

So, maybe you can create a new clone10.c file to write a dedicated test for CLONE_SETTLS and add architecture-specific handling.

Quoted 239 lines · chunfuwen via ltp

tls parameter and related flag:CLONE_SETTLS are missed in the testing, so add them into existed test case

---
 testcases/kernel/syscalls/clone/clone08.c   | 40 ++++++++++++++++++---
 testcases/kernel/syscalls/clone3/clone301.c | 36 ++++++++++++++++---
 2 files changed, 67 insertions(+), 9 deletions(-)
testcases/kernel/syscalls/clone/clone08.c +11 -4
index dd97f3ff1..f5a8119a8 100644
@@ -17,7 +17,11 @@
 #include "lapi/syscalls.h"
 #include "lapi/futex.h"
 
+#define TLS_SIZE 4096
+#define TLS_ALIGN 16
+
 static pid_t ptid, ctid, tgid;
+static void *tls_ptr;
 static void *child_stack;
 
 static void test_clone_parent(int t);
@@ -32,6 +36,9 @@ static int child_clone_parent_settid(void *);
 static void test_clone_thread(int t);
 static int child_clone_thread(void *);
 
+/* TLS variable to validate in child */
+static __thread int tls_test_var = 12345;
+
 /*
  * Children cloned with CLONE_VM should avoid using any functions that
  * might require dl_runtime_resolve, because they share thread-local
@@ -46,14 +53,14 @@ static struct test_case {
        void (*testfunc)(int);
        int (*do_child)(void *);
 } test_cases[] = {
-       {"CLONE_PARENT", CLONE_PARENT | SIGCHLD,
+       {"CLONE_PARENT", CLONE_PARENT | CLONE_SETTLS | SIGCHLD,
         test_clone_parent, child_clone_parent},
-       {"CLONE_CHILD_SETTID", CLONE_CHILD_SETTID | SIGCHLD,
+       {"CLONE_CHILD_SETTID", CLONE_CHILD_SETTID | CLONE_SETTLS |
 SIGCHLD,
         test_clone_tid, child_clone_child_settid},
-       {"CLONE_PARENT_SETTID", CLONE_PARENT_SETTID | CLONE_VM | SIGCHLD,
+       {"CLONE_PARENT_SETTID", CLONE_PARENT_SETTID | CLONE_VM |
 CLONE_SETTLS | SIGCHLD,
         test_clone_tid, child_clone_parent_settid},
        {"CLONE_THREAD", CLONE_THREAD | CLONE_SIGHAND | CLONE_VM |
-        CLONE_CHILD_CLEARTID | SIGCHLD,
+        CLONE_CHILD_CLEARTID | CLONE_SETTLS | SIGCHLD,
         test_clone_thread, child_clone_thread},

};

testcases/kernel/syscalls/clone/clone08.c +18 -1
@@ -66,17 +73,24 @@ static void do_test(unsigned int i)
 static void setup(void)
 {
        child_stack = SAFE_MALLOC(CHILD_STACK_SIZE);
+       tls_ptr = aligned_alloc(TLS_ALIGN, TLS_SIZE);
+       if (!tls_ptr) {
+               perror("aligned_alloc");
+               exit(EXIT_FAILURE);
+       }
+       memset(tls_ptr, 0, TLS_SIZE);
 }
 
 static void cleanup(void)
 {
        free(child_stack);
+       free(tls_ptr);
 }
 
 static long clone_child(const struct test_case *t)
 {
        TEST(ltp_clone7(t->flags, t->do_child, NULL, CHILD_STACK_SIZE,
-               child_stack, &ptid, NULL, &ctid));
+               child_stack, &ptid, tls_ptr, &ctid));
 
        if (TST_RET == -1 && TTERRNO == ENOSYS)
                tst_brk(TCONF, "clone does not support 7 args");
@@ -87,6 +101,14 @@ static long clone_child(const struct test_case *t)
        return TST_RET;
 }
 
+static void validate_tls(void)
+{
+       if (tls_test_var == 12345)
+               tst_res(TPASS, "Child TLS variable has expected value");
+       else
+               tst_res(TFAIL, "Child TLS variable corrupted or not set");
+}
+
 static void test_clone_parent(int t)
 {
        pid_t child;
@@ -102,6 +124,8 @@ static void test_clone_parent(int t)
 
 static int child_clone_parent(void *arg LTP_ATTRIBUTE_UNUSED)
 {
+       validate_tls();
+
        if (parent_ppid == getppid()) {
                tst_res(TPASS, "clone and forked child has the same
 parent");
        } else {
testcases/kernel/syscalls/clone/clone08.c +4 -0
@@ -120,6 +144,8 @@ static void test_clone_tid(int t)
 
 static int child_clone_child_settid(void *arg LTP_ATTRIBUTE_UNUSED)
 {
+       validate_tls();
+
        if (ctid == tst_syscall(__NR_getpid))
                tst_res(TPASS, "clone() correctly set ctid");
        else
@@ -130,6 +156,8 @@ static int child_clone_child_settid(void *arg
 LTP_ATTRIBUTE_UNUSED)
 
 static int child_clone_parent_settid(void *arg LTP_ATTRIBUTE_UNUSED)
 {
+       validate_tls();
+
        if (ptid == tst_syscall(__NR_getpid))
                tst_res(TPASS, "clone() correctly set ptid");

else

testcases/kernel/syscalls/clone/clone08.c +2 -0
@@ -175,6 +203,8 @@ static void test_clone_thread(int t)
 
 static int child_clone_thread(void *arg LTP_ATTRIBUTE_UNUSED)
 {
+       validate_tls();
+
        if (tgid == tst_syscall(__NR_getpid))
                tst_res(TPASS, "clone has the same thread id");
        else
testcases/kernel/syscalls/clone3/clone301.c +19 -3
index deed30b9f..9571c9f5c 100644
@@ -18,22 +18,38 @@
 
 #define CHILD_SIGNAL   SIGUSR1
 #define DATA   777
+#define TLS_SIZE 4096
+#define TLS_ALIGN 16
 
 static int pidfd, child_tid, parent_tid, parent_received_signal;
 static volatile int child_received_signal, child_data;
 static struct clone_args *args;
 
+/* TLS variable to validate in child */
+static __thread int tls_test_var = 12345;
+
 static struct tcase {
        uint64_t flags;
        int exit_signal;
 } tcases[] = {
        {0, SIGCHLD},
        {0, SIGUSR2},
-       {CLONE_FS, SIGCHLD},
-       {CLONE_NEWPID, SIGCHLD},
-       {CLONE_PARENT_SETTID | CLONE_CHILD_SETTID | CLONE_PIDFD, SIGCHLD},
+       {CLONE_FS | CLONE_SETTLS, SIGCHLD},
+       {CLONE_NEWPID | CLONE_SETTLS, SIGCHLD},
+       {CLONE_PARENT_SETTID | CLONE_CHILD_SETTID | CLONE_PIDFD |
 CLONE_SETTLS, SIGCHLD},
 };
 
+static void *allocate_tls_region(void)
+{
+       void *tls_area = aligned_alloc(TLS_ALIGN, TLS_SIZE);
+       if (!tls_area) {
+               perror("aligned_alloc");
+               exit(EXIT_FAILURE);
+       }
+       memset(tls_area, 0, TLS_SIZE);
+       return tls_area;
+}
+
 static void parent_rx_signal(int sig)
 {
        parent_received_signal = sig;
testcases/kernel/syscalls/clone3/clone301.c +13 -1
@@ -67,6 +83,13 @@ static void do_child(int clone_pidfd)
 {
        int count = 1000;
 
+       /* Validate TLS usage */
+       if (tls_test_var == 12345) {
+               tst_res(TPASS, "Child TLS variable has expected value");
+       } else {
+               tst_res(TFAIL, "Child TLS variable corrupted or not set");
+       }
+
        if (clone_pidfd) {
                child_received_signal = 0;
                child_data = 0;
@@ -111,6 +134,8 @@ static void run(unsigned int n)
        int status, clone_pidfd = tc->flags & CLONE_PIDFD;
        pid_t pid;
 
+       void *tls_ptr = allocate_tls_region();
+
        args->flags = tc->flags;
        args->pidfd = (uint64_t)(&pidfd);
        args->child_tid = (uint64_t)(&child_tid);
@@ -118,7 +143,7 @@ static void run(unsigned int n)
        args->exit_signal = tc->exit_signal;
        args->stack = 0;
        args->stack_size = 0;
-       args->tls = 0;
+       args->tls = (uint64_t)tls_ptr;
 
        parent_received_signal = 0;
        SAFE_SIGACTION(tc->exit_signal, &psig_action, NULL);
@@ -126,6 +151,7 @@ static void run(unsigned int n)
        TEST(pid = clone3(args, sizeof(*args)));
        if (pid < 0) {
                tst_res(TFAIL | TTERRNO, "clone3() failed (%d)", n);
+               free(tls_ptr);
                return;
        }
 
@@ -139,11 +165,13 @@ static void run(unsigned int n)
                TEST(pidfd_send_signal(pidfd, CHILD_SIGNAL, &uinfo, 0));
                if (TST_RET != 0) {
                        tst_res(TFAIL | TTERRNO, "pidfd_send_signal()
 failed");
+                       free(tls_ptr);
                        return;
                }
        }
 
        SAFE_WAITPID(pid, &status, __WALL);
+       free(tls_ptr);
 
        if (!parent_received_signal) {
                tst_res(TFAIL, "Parent haven't got signal");
Quoted 8 lines

--
2.43.5

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

--
Regards,
Li Wang

Signature · 1 line
Mailing list info: https://lists.linux.it/listinfo/ltp
#4 lore raw

PATCH v2 1/1 Add tls parameter and flag:CLONE_SETTLS cover for clone and clone3 syscall

In reply to Li Wang via ltp

tls parameter and related flag:CLONE_SETTLS are missed in the testing, so add them into existed test case

---
 testcases/kernel/syscalls/clone/clone10.c   | 164 ++++++++++++++++++++
 testcases/kernel/syscalls/clone3/clone304.c | 164 ++++++++++++++++++++
 2 files changed, 328 insertions(+)
 create mode 100644 testcases/kernel/syscalls/clone/clone10.c
 create mode 100644 testcases/kernel/syscalls/clone3/clone304.c
testcases/kernel/syscalls/clone/clone10.c new file +164 -0
new file mode 100644
index 000000000..8ef0f73cb
@@ -0,0 +1,164 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * Copyright (c) 2025 Red Hat Inc. All Rights Reserved.
+ * Author: Chunfu Wen <[email protected]>
+ */
+
+/*\
+ * Add tls parameter and flag:CLONE_SETTLS cover for clone
+ */
+
+#define _GNU_SOURCE
+#include <stdlib.h>
+#include <stdio.h>
+#include <errno.h>
+#include <sched.h>
+#include <sys/wait.h>
+
+#include "tst_test.h"
+#include "clone_platform.h"
+#include "lapi/syscalls.h"
+#include "lapi/futex.h"
+
+#define TLS_SIZE 4096
+#define TLS_ALIGN 16
+
+static pid_t ptid, ctid;
+static void *tls_ptr;
+static void *child_stack;
+static struct user_desc *tls_desc;
+
+/* TLS variable to validate in child */
+static __thread int tls_test_var = 12345;
+
+static int test_flags[] = {
+	CLONE_SETTLS,
+	CLONE_PARENT | CLONE_SETTLS,
+	CLONE_CHILD_SETTID | CLONE_SETTLS,
+	CLONE_PARENT_SETTID | CLONE_VM | CLONE_SETTLS,
+	CLONE_THREAD | CLONE_SIGHAND | CLONE_VM | CLONE_CHILD_CLEARTID | CLONE_SETTLS | SIGCHLD,
+	CLONE_VM | CLONE_FS | CLONE_FILES | CLONE_VFORK | CLONE_SIGHAND | CLONE_SETTLS,
+};
+
+static void init_tls(void)
+{
+#if defined(__x86_64__)
+	/* x86-64: tls_ptr is the new %fs base address */
+	tls_ptr = aligned_alloc(TLS_ALIGN, TLS_SIZE);
+	if (!tls_ptr)
+		tst_brk(TBROK | TERRNO, "aligned_alloc for TLS failed");
+	memset(tls_ptr, 0, TLS_SIZE);
+
+#elif defined(__i386__)
+	/* x86 32-bit: TLS is a struct user_desc */
+	tls_desc = SAFE_MALLOC(sizeof(*tls_desc));
+	memset(tls_desc, 0, sizeof(*tls_desc));
+	tls_desc->entry_number = -1;
+	tls_desc->base_addr = (unsigned long)SAFE_MALLOC(TLS_SIZE);
+	tls_desc->limit = TLS_SIZE;
+	tls_desc->seg_32bit = 1;
+	tls_desc->contents = 0;
+	tls_desc->read_exec_only = 0;
+	tls_desc->limit_in_pages = 0;
+	tls_desc->seg_not_present = 0;
+	tls_desc->useable = 1;
+	tls_ptr = tls_desc;
+#elif defined(__aarch64__) || defined(__riscv) || defined(__powerpc64__) || defined(__s390x__) || defined(__loongarch__)
+	/*
+	 * Other architectures: detect if dedicated TLS register is available.
+	 * You don't manually touch TPIDR_EL0.
+	 * The kernel automatically writes it into the TPIDR_EL0 register for the new thread after clone() succeeds.
+	 */
+	tls_ptr = aligned_alloc(TLS_ALIGN, TLS_SIZE);
+	if (!tls_ptr)
+		tst_brk(TBROK | TERRNO, "aligned_alloc for TLS failed");
+	memset(tls_ptr, 0, TLS_SIZE);
+#else
+	tst_brk(TCONF, "This architecture does not support TLS");
+#endif
+}
+
+static void free_tls(void)
+{
+#if defined(__x86_64__)
+	free(tls_ptr);
+
+#elif defined(__i386__)
+	if (tls_desc) {
+		free((void *)(uintptr_t)tls_desc->base_addr);
+		free(tls_desc);
+	}
+
+#elif defined(__aarch64__) || defined(__powerpc64__) || defined(__s390x__)
+	free(tls_ptr);
+#endif
+}
+
+static void setup(void)
+{
+	child_stack = SAFE_MALLOC(CHILD_STACK_SIZE);
+	init_tls();
+}
+
+static void cleanup(void)
+{
+	free(child_stack);
+	free_tls();
+}
+
+static int check_child(void *arg LTP_ATTRIBUTE_UNUSED)
+{
+	if (tls_test_var == 12345)
+		tst_res(TPASS, "Child TLS variable has expected value");
+	else
+		tst_res(TFAIL, "Child TLS variable corrupted or not set");
+
+	tst_syscall(__NR_exit, 0);
+	return 0;
+}
+
+static long clone_child(int flags)
+{
+	TEST(ltp_clone7(flags, check_child, NULL, CHILD_STACK_SIZE,
+		child_stack, &ptid, tls_ptr, &ctid));
+
+	if (TST_RET == -1 && TTERRNO == ENOSYS)
+		tst_brk(TCONF, "clone parameters doesn't match");
+
+	if (TST_RET == -1)
+		tst_brk(TBROK | TTERRNO, "clone() failed");
+
+	return TST_RET;
+}
+
+static void verify_tls(unsigned int i)
+{
+	pid_t child;
+	ctid = -1;
+	struct timespec timeout = { 5 /* sec */, 0 };
+	child = SAFE_FORK();
+	if (!child) {
+		tst_syscall(__NR_getpid);
+		clone_child(test_flags[i]);
+		if (test_flags[i] & CLONE_THREAD) {
+			if (syscall(SYS_futex, &ctid, FUTEX_WAIT, -1, &timeout)) {
+				if (errno != EWOULDBLOCK || ctid == -1) {
+					tst_res(TFAIL | TERRNO,
+						"futex failed, ctid: %d", ctid);
+					exit(0);
+				}
+			}
+		}
+		exit(0);
+	}
+	sleep(1);
+	tst_reap_children();
+}
+
+static struct tst_test test = {
+	.tcnt = ARRAY_SIZE(test_flags),
+	.test = verify_tls,
+	.setup = setup,
+	.cleanup = cleanup,
+	.forks_child = 1
+};
testcases/kernel/syscalls/clone3/clone304.c new file +164 -0

164 lines not on this page · raw message

Signature · 5 lines
2.43.5


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