Skip to contentSkip to conversation

Fix CLONE_SETTLS with clone3

PATCH In review Amanieu d'Antras 6 participants 31 messages Jan 2, 2020 – Jan 17, 2020

Subsystems (13)
lore
Share & tools
lore
#0 lore raw

cover Fix CLONE_SETTLS with clone3

The clone3 syscall is currently broken when used with CLONE_SETTLS on all architectures that don't have an implementation of copy_thread_tls. The old copy_thread function handles CLONE_SETTLS by reading the new TLS value from pt_regs containing the clone syscall parameters. Since clone3 passes the TLS value in clone_args, this results in the TLS register being initialized to a garbage value.

This patch series implements copy_thread_tls on all architectures that currently define __ARCH_WANT_SYS_CLONE3 and adds a compile-time check to ensure that any architecture that enables clone3 in the future also implements copy_thread_tls.

I have also included a minor fix for the arm64 uapi headers which caused __NR_clone3 to be missing from the exported user headers.

I have only tested this on arm64, but the copy_thread_tls implementations for the various architectures are fairly straightforward.

Amanieu d'Antras (7):
arm64: Move __ARCH_WANT_SYS_CLONE3 definition to uapi headers
arm64: Implement copy_thread_tls
arm: Implement copy_thread_tls
parisc: Implement copy_thread_tls
riscv: Implement copy_thread_tls
xtensa: Implement copy_thread_tls
clone3: ensure copy_thread_tls is implemented

 arch/arm/Kconfig                     |  1 +
 arch/arm/kernel/process.c            |  6 +++---
 arch/arm64/Kconfig                   |  1 +
 arch/arm64/include/asm/unistd.h      |  1 -
 arch/arm64/include/uapi/asm/unistd.h |  1 +
 arch/arm64/kernel/process.c          | 10 +++++-----
 arch/parisc/Kconfig                  |  1 +
 arch/parisc/kernel/process.c         |  8 ++++----
 arch/riscv/Kconfig                   |  1 +
 arch/riscv/kernel/process.c          |  6 +++---
 arch/xtensa/Kconfig                  |  1 +
 arch/xtensa/kernel/process.c         |  8 ++++----
 kernel/fork.c                        | 10 ++++++++++
 13 files changed, 35 insertions(+), 20 deletions(-)
Signature · 1 line
2.24.1
#1 lore raw

PATCH 1/7 arm64: Move __ARCH_WANT_SYS_CLONE3 definition to uapi headers

Previously this was only defined in the internal headers which resulted in __NR_clone3 not being defined in the user headers.

---
 arch/arm64/include/asm/unistd.h      | 1 -
 arch/arm64/include/uapi/asm/unistd.h | 1 +
 2 files changed, 1 insertion(+), 1 deletion(-)
arch/arm64/include/asm/unistd.h +0 -1
index 2629a68b8724..5af82587909e 100644
@@ -42,7 +42,6 @@
 #endif
 
 #define __ARCH_WANT_SYS_CLONE
-#define __ARCH_WANT_SYS_CLONE3
 
 #ifndef __COMPAT_SYSCALL_NR
 #include <uapi/asm/unistd.h>
arch/arm64/include/uapi/asm/unistd.h +1 -0
index 4703d218663a..f83a70e07df8 100644
@@ -19,5 +19,6 @@
 #define __ARCH_WANT_NEW_STAT
 #define __ARCH_WANT_SET_GET_RLIMIT
 #define __ARCH_WANT_TIME32_SYSCALLS
+#define __ARCH_WANT_SYS_CLONE3
 
 #include <asm-generic/unistd.h>
Signature · 1 line
2.24.1
#2 lore raw

Re: [PATCH 1/7] arm64: Move __ARCH_WANT_SYS_CLONE3 definition to uapi headers

[Cc Arnd. I'd like his Ack on this]

Quoted 37 lines · Amanieu d'Antras

Previously this was only defined in the internal headers which resulted in __NR_clone3 not being defined in the user headers.

---
 arch/arm64/include/asm/unistd.h      | 1 -
 arch/arm64/include/uapi/asm/unistd.h | 1 +
 2 files changed, 1 insertion(+), 1 deletion(-)
arch/arm64/include/asm/unistd.h +0 -1
index 2629a68b8724..5af82587909e 100644
@@ -42,7 +42,6 @@
 #endif
 
 #define __ARCH_WANT_SYS_CLONE
-#define __ARCH_WANT_SYS_CLONE3
 
 #ifndef __COMPAT_SYSCALL_NR
 #include <uapi/asm/unistd.h>
arch/arm64/include/uapi/asm/unistd.h +1 -0
index 4703d218663a..f83a70e07df8 100644
@@ -19,5 +19,6 @@
 #define __ARCH_WANT_NEW_STAT
 #define __ARCH_WANT_SET_GET_RLIMIT
 #define __ARCH_WANT_TIME32_SYSCALLS
+#define __ARCH_WANT_SYS_CLONE3
 
 #include <asm-generic/unistd.h>
Signature · 1 line
2.24.1
#3 lore raw

Re: [PATCH 1/7] arm64: Move __ARCH_WANT_SYS_CLONE3 definition to uapi headers

Quoted 11 lines · Christian Brauner
Quoted 10 lines · Amanieu d'Antras

Previously this was only defined in the internal headers which resulted in __NR_clone3 not being defined in the user headers.

---
 arch/arm64/include/asm/unistd.h      | 1 -
 arch/arm64/include/uapi/asm/unistd.h | 1 +
 2 files changed, 1 insertion(+), 1 deletion(-)

Good catch, this is clearly needed, but please make the patch change every copy of asm/unistd.h that defines this, not just the arm64 one.

Arnd

#4 lore raw

Re: [PATCH 1/7] arm64: Move __ARCH_WANT_SYS_CLONE3 definition to uapi headers

Quoted 14 lines · Arnd Bergmann
Quoted 11 lines · Christian Brauner
Quoted 10 lines · Amanieu d'Antras

Previously this was only defined in the internal headers which resulted in __NR_clone3 not being defined in the user headers.

---
 arch/arm64/include/asm/unistd.h      | 1 -
 arch/arm64/include/uapi/asm/unistd.h | 1 +
 2 files changed, 1 insertion(+), 1 deletion(-)
Quoted 2 lines

Good catch, this is clearly needed, but please make the patch change every copy of asm/unistd.h that defines this, not just the arm64 one.

Actually __ARCH_WANT_SYS_CLONE3 only needs to be in the uapi headers for architectures that use the asm-generic/unistd.h header, which uses it to guard the definition of __NR_clone3. Architectures not using the asm-generic header don't need this define to export __NR_clone3. The only other architecture with clone3 that uses the asm-generic header is riscv, which already defines __ARCH_WANT_SYS_CLONE3 in the uapi headers.

Reviewed-by
#5 lore raw

Re: [PATCH 1/7] arm64: Move __ARCH_WANT_SYS_CLONE3 definition to uapi headers

Quoted 19 lines · Amanieu d'Antras
Quoted 14 lines · Arnd Bergmann
Quoted 11 lines · Christian Brauner
Quoted 10 lines · Amanieu d'Antras

Previously this was only defined in the internal headers which resulted in __NR_clone3 not being defined in the user headers.

---
 arch/arm64/include/asm/unistd.h      | 1 -
 arch/arm64/include/uapi/asm/unistd.h | 1 +
 2 files changed, 1 insertion(+), 1 deletion(-)
Quoted 2 lines

Good catch, this is clearly needed, but please make the patch change every copy of asm/unistd.h that defines this, not just the arm64 one.

Quoted 7 lines

Actually __ARCH_WANT_SYS_CLONE3 only needs to be in the uapi headers for architectures that use the asm-generic/unistd.h header, which uses it to guard the definition of __NR_clone3. Architectures not using the asm-generic header don't need this define to export __NR_clone3. The only other architecture with clone3 that uses the asm-generic header is riscv, which already defines __ARCH_WANT_SYS_CLONE3 in the uapi headers.

Ah, of course. The patch looks fine to me then.

#6 lore raw

PATCH 2/7 arm64: Implement copy_thread_tls

In reply to Amanieu d'Antras

This is required for clone3 which passes the TLS value through a struct rather than a register.

---
 arch/arm64/Kconfig          |  1 +
 arch/arm64/kernel/process.c | 10 +++++-----
 2 files changed, 6 insertions(+), 5 deletions(-)
arch/arm64/Kconfig +1 -0
index b1b4476ddb83..e688dfad0b72 100644
@@ -138,6 +138,7 @@ config ARM64
 	select HAVE_CMPXCHG_DOUBLE
 	select HAVE_CMPXCHG_LOCAL
 	select HAVE_CONTEXT_TRACKING
+	select HAVE_COPY_THREAD_TLS
 	select HAVE_DEBUG_BUGVERBOSE
 	select HAVE_DEBUG_KMEMLEAK
 	select HAVE_DMA_CONTIGUOUS
arch/arm64/kernel/process.c +5 -5
index 71f788cd2b18..d54586d5b031 100644
@@ -360,8 +360,8 @@ int arch_dup_task_struct(struct task_struct *dst, struct task_struct *src)
 
 asmlinkage void ret_from_fork(void) asm("ret_from_fork");
 
-int copy_thread(unsigned long clone_flags, unsigned long stack_start,
-		unsigned long stk_sz, struct task_struct *p)
+int copy_thread_tls(unsigned long clone_flags, unsigned long stack_start,
+		unsigned long stk_sz, struct task_struct *p, unsigned long tls)
 {
 	struct pt_regs *childregs = task_pt_regs(p);
 
@@ -394,11 +394,11 @@ int copy_thread(unsigned long clone_flags, unsigned long stack_start,
 		}
 
 		/*
-		 * If a TLS pointer was passed to clone (4th argument), use it
-		 * for the new thread.
+		 * If a TLS pointer was passed to clone, use it for the new
+		 * thread.
 		 */
 		if (clone_flags & CLONE_SETTLS)
-			p->thread.uw.tp_value = childregs->regs[3];
+			p->thread.uw.tp_value = tls;
 	} else {
 		memset(childregs, 0, sizeof(struct pt_regs));
 		childregs->pstate = PSR_MODE_EL1h;
Signature · 1 line
2.24.1
Acked-by on v1 2/7
#7 lore raw

Re: [PATCH 2/7] arm64: Implement copy_thread_tls

Quoted 6 lines · Amanieu d'Antras

This is required for clone3 which passes the TLS value through a struct rather than a register.

This looks sane to me but I'd like an ack from someone who knows his arm from his arse before taking this. :)

Quoted 50 lines
---
 arch/arm64/Kconfig          |  1 +
 arch/arm64/kernel/process.c | 10 +++++-----
 2 files changed, 6 insertions(+), 5 deletions(-)
arch/arm64/Kconfig +1 -0
index b1b4476ddb83..e688dfad0b72 100644
@@ -138,6 +138,7 @@ config ARM64
 	select HAVE_CMPXCHG_DOUBLE
 	select HAVE_CMPXCHG_LOCAL
 	select HAVE_CONTEXT_TRACKING
+	select HAVE_COPY_THREAD_TLS
 	select HAVE_DEBUG_BUGVERBOSE
 	select HAVE_DEBUG_KMEMLEAK
 	select HAVE_DMA_CONTIGUOUS
arch/arm64/kernel/process.c +5 -5
index 71f788cd2b18..d54586d5b031 100644
@@ -360,8 +360,8 @@ int arch_dup_task_struct(struct task_struct *dst, struct task_struct *src)
 
 asmlinkage void ret_from_fork(void) asm("ret_from_fork");
 
-int copy_thread(unsigned long clone_flags, unsigned long stack_start,
-		unsigned long stk_sz, struct task_struct *p)
+int copy_thread_tls(unsigned long clone_flags, unsigned long stack_start,
+		unsigned long stk_sz, struct task_struct *p, unsigned long tls)
 {
 	struct pt_regs *childregs = task_pt_regs(p);
 
@@ -394,11 +394,11 @@ int copy_thread(unsigned long clone_flags, unsigned long stack_start,
 		}
 
 		/*
-		 * If a TLS pointer was passed to clone (4th argument), use it
-		 * for the new thread.
+		 * If a TLS pointer was passed to clone, use it for the new
+		 * thread.
 		 */
 		if (clone_flags & CLONE_SETTLS)
-			p->thread.uw.tp_value = childregs->regs[3];
+			p->thread.uw.tp_value = tls;
 	} else {
 		memset(childregs, 0, sizeof(struct pt_regs));
 		childregs->pstate = PSR_MODE_EL1h;
Signature · 1 line
2.24.1
maintainer Acked-by
#8 lore raw

Re: [PATCH 2/7] arm64: Implement copy_thread_tls

Quoted 10 lines · Christian Brauner
Quoted 6 lines · Amanieu d'Antras

This is required for clone3 which passes the TLS value through a struct rather than a register.

This looks sane to me but I'd like an ack from someone who knows his arm from his arse before taking this. :)

That's *ME*! Code looks fine:

I also ran the native and compat selftests but, unfortunately, they all pass even without this patch. Do you reckon it would be possible to update them to check the tls pointer?

Will

#9 lore raw

Re: [PATCH 2/7] arm64: Implement copy_thread_tls

Quoted 3 lines · Will Deacon

I also ran the native and compat selftests but, unfortunately, they all pass even without this patch. Do you reckon it would be possible to update them to check the tls pointer?

Here's the program I used for testing on arm64. I considered adding it to the selftests but there is no portable way of reading the TLS register on all architectures.

#include <sys/syscall.h>
#include <unistd.h>
#include <stdio.h>
#include <stdint.h>

#define __NR_clone3 435

struct clone_args {
    uint64_t flags;
    uint64_t pidfd;
    uint64_t child_tid;
    uint64_t parent_tid;
    uint64_t exit_signal;
    uint64_t stack;
    uint64_t stack_size;
    uint64_t tls;
};

#define USE_CLONE3

int main() {
    printf("Before fork: tp = %p\n", __builtin_thread_pointer());
#ifdef USE_CLONE3
    struct clone_args args = {
        .flags = CLONE_SETTLS,
        .tls = (uint64_t)__builtin_thread_pointer(),
    };
    int ret = syscall(__NR_clone3, &args, sizeof(args));
#else
    int ret = syscall(__NR_clone, CLONE_SETTLS, 0, 0,
__builtin_thread_pointer(), 0);
#endif
    printf("Fork returned %d, tp = %p\n", ret, __builtin_thread_pointer());
}
#10 lore raw

Re: [PATCH 2/7] arm64: Implement copy_thread_tls

[Cc Kees in case he knows something about where arch specific tests live
or whether we have a framework for this]

Quoted 8 lines · Amanieu d'Antras
Quoted 3 lines · Will Deacon

I also ran the native and compat selftests but, unfortunately, they all pass even without this patch. Do you reckon it would be possible to update them to check the tls pointer?

Here's the program I used for testing on arm64. I considered adding it to the selftests but there is no portable way of reading the TLS register on all architectures.

I'm not saying you need to do this right now.
It feels like we must've run into the "this is architecture specific"-and-we-want-to-test-this issue before... Do we have a place where architecture specific selftests live?

Quoted 34 lines

#include <sys/syscall.h>
#include <unistd.h>
#include <stdio.h>
#include <stdint.h>

#define __NR_clone3 435

struct clone_args {
    uint64_t flags;
    uint64_t pidfd;
    uint64_t child_tid;
    uint64_t parent_tid;
    uint64_t exit_signal;
    uint64_t stack;
    uint64_t stack_size;
    uint64_t tls;
};

#define USE_CLONE3

int main() {
    printf("Before fork: tp = %p\n", __builtin_thread_pointer());
#ifdef USE_CLONE3
    struct clone_args args = {
        .flags = CLONE_SETTLS,
        .tls = (uint64_t)__builtin_thread_pointer(),
    };
    int ret = syscall(__NR_clone3, &args, sizeof(args));
#else
    int ret = syscall(__NR_clone, CLONE_SETTLS, 0, 0,
__builtin_thread_pointer(), 0);
#endif
    printf("Fork returned %d, tp = %p\n", ret, __builtin_thread_pointer());
}
maintainer
#11 lore raw

Re: [PATCH 2/7] arm64: Implement copy_thread_tls

Quoted 9 lines · Christian Brauner

[Cc Kees in case he knows something about where arch specific tests live
or whether we have a framework for this]

Quoted 5 lines · Amanieu d'Antras
Quoted 3 lines · Will Deacon

I also ran the native and compat selftests but, unfortunately, they all pass even without this patch. Do you reckon it would be possible to update them to check the tls pointer?

Quoted 5 lines
Quoted 3 lines

Here's the program I used for testing on arm64. I considered adding it to the selftests but there is no portable way of reading the TLS register on all architectures.

I'm not saying you need to do this right now.

Agreed, these patches should be merged in their current state and my ack stands for that.

Quoted 3 lines

It feels like we must've run into the "this is architecture specific"-and-we-want-to-test-this issue before... Do we have a place where architecture specific selftests live?

For arch-specific selftests there are tools/testing/selftests/$ARCH directories, although in this case maybe it's better to have an #ifdef in a header so that architectures with __builtin_thread_pointer can use that.

Will

#12 lore raw

Re: [PATCH 2/7] arm64: Implement copy_thread_tls

Quoted 8 lines · Will Deacon
Quoted 6 lines · Christian Brauner

[Cc Kees in case he knows something about where arch specific tests live
or whether we have a framework for this]
[...]
It feels like we must've run into the "this is architecture specific"-and-we-want-to-test-this issue before... Do we have a place where architecture specific selftests live?

Quoted 4 lines

For arch-specific selftests there are tools/testing/selftests/$ARCH directories, although in this case maybe it's better to have an #ifdef in a header so that architectures with __builtin_thread_pointer can use that.

Yup, I agree: that's the current best-practice for arch-specific selftests.

Signature · 1 line
Kees Cook
#13 lore raw

Re: [PATCH 2/7] arm64: Implement copy_thread_tls

Quoted 9 lines · Kees Cook
Quoted 8 lines · Will Deacon
Quoted 6 lines · Christian Brauner

[Cc Kees in case he knows something about where arch specific tests live
or whether we have a framework for this]
[...]
It feels like we must've run into the "this is architecture specific"-and-we-want-to-test-this issue before... Do we have a place where architecture specific selftests live?

Quoted 7 lines
Quoted 4 lines

For arch-specific selftests there are tools/testing/selftests/$ARCH directories, although in this case maybe it's better to have an #ifdef in a header so that architectures with __builtin_thread_pointer can use that.

Yup, I agree: that's the current best-practice for arch-specific selftests.

Thanks! I think using #ifdef in this case with __builtin_thread_pointer sounds good.
So the tests can be moved into the clone3() test-suite for those architectures.

Christian

#14 lore raw

Re: [PATCH 2/7] arm64: Implement copy_thread_tls

In reply to Will Deacon
Quoted 10 lines · Will Deacon
Quoted 9 lines · Christian Brauner

[Cc Kees in case he knows something about where arch specific tests live
or whether we have a framework for this]

Quoted 5 lines · Amanieu d'Antras
Quoted 3 lines · Will Deacon

I also ran the native and compat selftests but, unfortunately, they all pass even without this patch. Do you reckon it would be possible to update them to check the tls pointer?

Quoted 8 lines
Quoted 5 lines
Quoted 3 lines

Here's the program I used for testing on arm64. I considered adding it to the selftests but there is no portable way of reading the TLS register on all architectures.

I'm not saying you need to do this right now.

Agreed, these patches should be merged in their current state and my ack stands for that.

Oh yeah, that's how I took your Ack.
Thanks! :)

Quoted 9 lines
Quoted 4 lines

It feels like we must've run into the "this is architecture specific"-and-we-want-to-test-this issue before... Do we have a place where architecture specific selftests live?

For arch-specific selftests there are tools/testing/selftests/$ARCH directories, although in this case maybe it's better to have an #ifdef in a header so that architectures with __builtin_thread_pointer can use that.

Yeah, I think the #ifdef approach might make the most sense.

Christian

#15 lore raw

PATCH 3/7 arm: Implement copy_thread_tls

In reply to Amanieu d'Antras

This is required for clone3 which passes the TLS value through a struct rather than a register.

---
 arch/arm/Kconfig          | 1 +
 arch/arm/kernel/process.c | 6 +++---
 2 files changed, 4 insertions(+), 3 deletions(-)
arch/arm/Kconfig +1 -0
index ba75e3661a41..96dab76da3b3 100644
@@ -72,6 +72,7 @@ config ARM
 	select HAVE_ARM_SMCCC if CPU_V7
 	select HAVE_EBPF_JIT if !CPU_ENDIAN_BE32
 	select HAVE_CONTEXT_TRACKING
+	select HAVE_COPY_THREAD_TLS
 	select HAVE_C_RECORDMCOUNT
 	select HAVE_DEBUG_KMEMLEAK
 	select HAVE_DMA_CONTIGUOUS if MMU
arch/arm/kernel/process.c +3 -3
index cea1c27c29cb..46e478fb5ea2 100644
@@ -226,8 +226,8 @@ void release_thread(struct task_struct *dead_task)
 asmlinkage void ret_from_fork(void) __asm__("ret_from_fork");
 
 int
-copy_thread(unsigned long clone_flags, unsigned long stack_start,
-	    unsigned long stk_sz, struct task_struct *p)
+copy_thread_tls(unsigned long clone_flags, unsigned long stack_start,
+	    unsigned long stk_sz, struct task_struct *p, unsigned long tls)
 {
 	struct thread_info *thread = task_thread_info(p);
 	struct pt_regs *childregs = task_pt_regs(p);
@@ -261,7 +261,7 @@ copy_thread(unsigned long clone_flags, unsigned long stack_start,
 	clear_ptrace_hw_breakpoint(p);
 
 	if (clone_flags & CLONE_SETTLS)
-		thread->tp_value[0] = childregs->ARM_r3;
+		thread->tp_value[0] = tls;
 	thread->tp_value[1] = get_tpuser();
 
 	thread_notify(THREAD_NOTIFY_COPY, thread);
Signature · 1 line
2.24.1
Acked-by on v1 3/7
#16 lore raw

Re: [PATCH 3/7] arm: Implement copy_thread_tls

Quoted 6 lines · Amanieu d'Antras

This is required for clone3 which passes the TLS value through a struct rather than a register.

Again, looks good to me but I'd like an ack from someone closer to the architecture itself.

Quoted 44 lines
---
 arch/arm/Kconfig          | 1 +
 arch/arm/kernel/process.c | 6 +++---
 2 files changed, 4 insertions(+), 3 deletions(-)
arch/arm/Kconfig +1 -0
index ba75e3661a41..96dab76da3b3 100644
@@ -72,6 +72,7 @@ config ARM
 	select HAVE_ARM_SMCCC if CPU_V7
 	select HAVE_EBPF_JIT if !CPU_ENDIAN_BE32
 	select HAVE_CONTEXT_TRACKING
+	select HAVE_COPY_THREAD_TLS
 	select HAVE_C_RECORDMCOUNT
 	select HAVE_DEBUG_KMEMLEAK
 	select HAVE_DMA_CONTIGUOUS if MMU
arch/arm/kernel/process.c +3 -3
index cea1c27c29cb..46e478fb5ea2 100644
@@ -226,8 +226,8 @@ void release_thread(struct task_struct *dead_task)
 asmlinkage void ret_from_fork(void) __asm__("ret_from_fork");
 
 int
-copy_thread(unsigned long clone_flags, unsigned long stack_start,
-	    unsigned long stk_sz, struct task_struct *p)
+copy_thread_tls(unsigned long clone_flags, unsigned long stack_start,
+	    unsigned long stk_sz, struct task_struct *p, unsigned long tls)
 {
 	struct thread_info *thread = task_thread_info(p);
 	struct pt_regs *childregs = task_pt_regs(p);
@@ -261,7 +261,7 @@ copy_thread(unsigned long clone_flags, unsigned long stack_start,
 	clear_ptrace_hw_breakpoint(p);
 
 	if (clone_flags & CLONE_SETTLS)
-		thread->tp_value[0] = childregs->ARM_r3;
+		thread->tp_value[0] = tls;
 	thread->tp_value[1] = get_tpuser();
 
 	thread_notify(THREAD_NOTIFY_COPY, thread);
Signature · 1 line
2.24.1
#17 lore raw

PATCH 4/7 parisc: Implement copy_thread_tls

In reply to Amanieu d'Antras

This is required for clone3 which passes the TLS value through a struct rather than a register.

---
 arch/parisc/Kconfig          | 1 +
 arch/parisc/kernel/process.c | 8 ++++----
 2 files changed, 5 insertions(+), 4 deletions(-)
arch/parisc/Kconfig +1 -0
index b16237c95ea3..0c29d6cb2c8d 100644
@@ -62,6 +62,7 @@ config PARISC
 	select HAVE_FTRACE_MCOUNT_RECORD if HAVE_DYNAMIC_FTRACE
 	select HAVE_KPROBES_ON_FTRACE
 	select HAVE_DYNAMIC_FTRACE_WITH_REGS
+	select HAVE_COPY_THREAD_TLS
 
 	help
 	  The PA-RISC microprocessor is designed by Hewlett-Packard and used
arch/parisc/kernel/process.c +4 -4
index ecc5c2771208..230a6422b99f 100644
@@ -208,8 +208,8 @@ arch_initcall(parisc_idle_init);
  * Copy architecture-specific thread state
  */
 int
-copy_thread(unsigned long clone_flags, unsigned long usp,
-	    unsigned long kthread_arg, struct task_struct *p)
+copy_thread_tls(unsigned long clone_flags, unsigned long usp,
+	    unsigned long kthread_arg, struct task_struct *p, unsigned long tls)
 {
 	struct pt_regs *cregs = &(p->thread.regs);
 	void *stack = task_stack_page(p);
@@ -254,9 +254,9 @@ copy_thread(unsigned long clone_flags, unsigned long usp,
 		cregs->ksp = (unsigned long)stack + THREAD_SZ_ALGN + FRAME_SIZE;
 		cregs->kpc = (unsigned long) &child_return;
 
-		/* Setup thread TLS area from the 4th parameter in clone */
+		/* Setup thread TLS area */
 		if (clone_flags & CLONE_SETTLS)
-			cregs->cr27 = cregs->gr[23];
+			cregs->cr27 = tls;
 	}
 
 	return 0;
Signature · 1 line
2.24.1
#18 lore raw

PATCH 5/7 riscv: Implement copy_thread_tls

In reply to Amanieu d'Antras

This is required for clone3 which passes the TLS value through a struct rather than a register.

---
 arch/riscv/Kconfig          | 1 +
 arch/riscv/kernel/process.c | 6 +++---
 2 files changed, 4 insertions(+), 3 deletions(-)
arch/riscv/Kconfig +1 -0
index d8efbaa78d67..d6c3d44f96b5 100644
@@ -64,6 +64,7 @@ config RISCV
 	select SPARSEMEM_STATIC if 32BIT
 	select ARCH_WANT_DEFAULT_TOPDOWN_MMAP_LAYOUT if MMU
 	select HAVE_ARCH_MMAP_RND_BITS if MMU
+	select HAVE_COPY_THREAD_TLS
 
 config ARCH_MMAP_RND_BITS_MIN
 	default 18 if 64BIT
arch/riscv/kernel/process.c +3 -3
index 95a3031e5c7c..817cf7b0974c 100644
@@ -99,8 +99,8 @@ int arch_dup_task_struct(struct task_struct *dst, struct task_struct *src)
 	return 0;
 }
 
-int copy_thread(unsigned long clone_flags, unsigned long usp,
-	unsigned long arg, struct task_struct *p)
+int copy_thread_tls(unsigned long clone_flags, unsigned long usp,
+	unsigned long arg, struct task_struct *p, unsigned long tls)
 {
 	struct pt_regs *childregs = task_pt_regs(p);
 
@@ -121,7 +121,7 @@ int copy_thread(unsigned long clone_flags, unsigned long usp,
 		if (usp) /* User fork */
 			childregs->sp = usp;
 		if (clone_flags & CLONE_SETTLS)
-			childregs->tp = childregs->a5;
+			childregs->tp = tls;
 		childregs->a0 = 0; /* Return value of fork() */
 		p->thread.ra = (unsigned long)ret_from_fork;
 	}
Signature · 1 line
2.24.1
#19 lore raw

PATCH 6/7 xtensa: Implement copy_thread_tls

In reply to Amanieu d'Antras

This is required for clone3 which passes the TLS value through a struct rather than a register.

---
 arch/xtensa/Kconfig          | 1 +
 arch/xtensa/kernel/process.c | 8 ++++----
 2 files changed, 5 insertions(+), 4 deletions(-)
arch/xtensa/Kconfig +1 -0
index 4a3fa295d8fe..296c5324dace 100644
@@ -24,6 +24,7 @@ config XTENSA
 	select HAVE_ARCH_JUMP_LABEL if !XIP_KERNEL
 	select HAVE_ARCH_KASAN if MMU && !XIP_KERNEL
 	select HAVE_ARCH_TRACEHOOK
+	select HAVE_COPY_THREAD_TLS
 	select HAVE_DEBUG_KMEMLEAK
 	select HAVE_DMA_CONTIGUOUS
 	select HAVE_EXIT_THREAD
arch/xtensa/kernel/process.c +4 -4
index 9e1c49134c07..3edecc41ef8c 100644
@@ -202,8 +202,9 @@ int arch_dup_task_struct(struct task_struct *dst, struct task_struct *src)
  * involved.  Much simpler to just not copy those live frames across.
  */
 
-int copy_thread(unsigned long clone_flags, unsigned long usp_thread_fn,
-		unsigned long thread_fn_arg, struct task_struct *p)
+int copy_thread_tls(unsigned long clone_flags, unsigned long usp_thread_fn,
+		unsigned long thread_fn_arg, struct task_struct *p,
+		unsigned long tls)
 {
 	struct pt_regs *childregs = task_pt_regs(p);
 
@@ -266,9 +267,8 @@ int copy_thread(unsigned long clone_flags, unsigned long usp_thread_fn,
 
 		childregs->syscall = regs->syscall;
 
-		/* The thread pointer is passed in the '4th argument' (= a5) */
 		if (clone_flags & CLONE_SETTLS)
-			childregs->threadptr = childregs->areg[5];
+			childregs->threadptr = tls;
 	} else {
 		p->thread.ra = MAKE_RA_FOR_CALL(
 				(unsigned long)ret_from_kernel_thread, 1);
Signature · 1 line
2.24.1
#20 lore raw

PATCH 7/7 clone3: ensure copy_thread_tls is implemented

In reply to Amanieu d'Antras

copy_thread implementations handle CLONE_SETTLS by reading the TLS value from the registers containing the syscall arguments for clone. This doesn't work with clone3 since the TLS value is passed in clone_args instead.

---
 kernel/fork.c | 10 ++++++++++
 1 file changed, 10 insertions(+)
kernel/fork.c +10 -0
index 2508a4f238a3..080809560072 100644
@@ -2578,6 +2578,16 @@ SYSCALL_DEFINE5(clone, unsigned long, clone_flags, unsigned long, newsp,
 #endif
 
 #ifdef __ARCH_WANT_SYS_CLONE3
+
+/*
+ * copy_thread implementations handle CLONE_SETTLS by reading the TLS value from
+ * the registers containing the syscall arguments for clone. This doesn't work
+ * with clone3 since the TLS value is passed in clone_args instead.
+ */
+#ifndef CONFIG_HAVE_COPY_THREAD_TLS
+#error clone3 requires copy_thread_tls support in arch
+#endif
+
 noinline static int copy_clone_args_from_user(struct kernel_clone_args *kargs,
 					      struct clone_args __user *uargs,
 					      size_t usize)
Signature · 1 line
2.24.1
Reviewed-by on v1 7/7
#21 lore raw

Re: [PATCH 7/7] clone3: ensure copy_thread_tls is implemented

Quoted 7 lines · Amanieu d'Antras

copy_thread implementations handle CLONE_SETTLS by reading the TLS value from the registers containing the syscall arguments for clone. This doesn't work with clone3 since the TLS value is passed in clone_args instead.

I'm in favor of this change. But we need to make sure that any arch which now has ARCH_WANTS_SYS_CLONE3 set but doesn't implement copy_thread_tls() is fixed.

Once all architectures have clone3() support - and there are just a few by now (IA64 comes to mind) this means we should also be able to get rid of of copy_thread() completely. That seems desirable to me as it makes the codepaths easier to follow.

#22 lore raw

Re: [PATCH 7/7] clone3: ensure copy_thread_tls is implemented

Quoted 8 lines · Christian Brauner

I'm in favor of this change. But we need to make sure that any arch which now has ARCH_WANTS_SYS_CLONE3 set but doesn't implement copy_thread_tls() is fixed.

Once all architectures have clone3() support - and there are just a few by now (IA64 comes to mind) this means we should also be able to get rid of of copy_thread() completely. That seems desirable to me as it makes the codepaths easier to follow.

I've already implemented copy_thread_tls for all arches that currently have ARCH_WANTS_SYS_CLONE3 in the previous 5 patches. The #error is there so that any future arches that wire up clone3 don't forget to implement copy_thread_tls as well.

#23 lore raw

Re: [PATCH 7/7] clone3: ensure copy_thread_tls is implemented

Quoted 11 lines · Amanieu d'Antras
Quoted 8 lines · Christian Brauner

I'm in favor of this change. But we need to make sure that any arch which now has ARCH_WANTS_SYS_CLONE3 set but doesn't implement copy_thread_tls() is fixed.

Once all architectures have clone3() support - and there are just a few by now (IA64 comes to mind) this means we should also be able to get rid of of copy_thread() completely. That seems desirable to me as it makes the codepaths easier to follow.

Quoted 4 lines

I've already implemented copy_thread_tls for all arches that currently have ARCH_WANTS_SYS_CLONE3 in the previous 5 patches. The #error is there so that any future arches that wire up clone3 don't forget to implement copy_thread_tls as well.

Right, my point is: Once _all_ arches have ARCH_WANT_SYS_CLONE3 they also must implement copy_thread_tls at which point we can remove:
- ARCH_WANT_SYS_CLONE3 ifdefines
- copy_thread()
We can't do this right now because e.g. IA64 does not set ARCH_WANT_SYS_CLONE3 and also does not select HAVE_COPY_THREAD_TLS.

Christian

Acked-by on the v1 series
#24 lore raw
In reply to Amanieu d'Antras
Quoted 11 lines · Amanieu d'Antras

The clone3 syscall is currently broken when used with CLONE_SETTLS on all architectures that don't have an implementation of copy_thread_tls. The old copy_thread function handles CLONE_SETTLS by reading the new TLS value from pt_regs containing the clone syscall parameters. Since clone3 passes the TLS value in clone_args, this results in the TLS register being initialized to a garbage value.

This patch series implements copy_thread_tls on all architectures that currently define __ARCH_WANT_SYS_CLONE3 and adds a compile-time check to ensure that any architecture that enables clone3 in the future also implements copy_thread_tls.

Quoted 5 lines

I have also included a minor fix for the arm64 uapi headers which caused __NR_clone3 to be missing from the exported user headers.

I have only tested this on arm64, but the copy_thread_tls implementations for the various architectures are fairly straightforward.

The series looks straightforward to me but I'd like a few Acks from some of the arch maintainers before taking this.

#25 lore raw

PATCH 1/1 um: Implement copy_thread_tls

In reply to Amanieu d'Antras

This is required for clone3 which passes the TLS value through a struct rather than a register.

---
 arch/um/Kconfig                      | 1 +
 arch/um/include/asm/ptrace-generic.h | 2 +-
 arch/um/kernel/process.c             | 6 +++---
 arch/x86/um/tls_32.c                 | 6 ++----
 arch/x86/um/tls_64.c                 | 7 +++----
 5 files changed, 10 insertions(+), 12 deletions(-)
arch/um/Kconfig +1 -0
index 2a6d04fcb3e9..6f0edd0c0220 100644
@@ -14,6 +14,7 @@ config UML
 	select HAVE_FUTEX_CMPXCHG if FUTEX
 	select HAVE_DEBUG_KMEMLEAK
 	select HAVE_DEBUG_BUGVERBOSE
+	select HAVE_COPY_THREAD_TLS
 	select GENERIC_IRQ_SHOW
 	select GENERIC_CPU_DEVICES
 	select GENERIC_CLOCKEVENTS
arch/um/include/asm/ptrace-generic.h +1 -1
index 81c647ef9c6c..adf91ef553ae 100644
@@ -36,7 +36,7 @@ extern long subarch_ptrace(struct task_struct *child, long request,
 extern unsigned long getreg(struct task_struct *child, int regno);
 extern int putreg(struct task_struct *child, int regno, unsigned long value);
 
-extern int arch_copy_tls(struct task_struct *new);
+extern int arch_set_tls(struct task_struct *new, unsigned long tls);
 extern void clear_flushed_tls(struct task_struct *task);
 extern int syscall_trace_enter(struct pt_regs *regs);
 extern void syscall_trace_leave(struct pt_regs *regs);
arch/um/kernel/process.c +3 -3
index 263a8f069133..17045e7211bf 100644
@@ -153,8 +153,8 @@ void fork_handler(void)
 	userspace(&current->thread.regs.regs, current_thread_info()->aux_fp_regs);
 }
 
-int copy_thread(unsigned long clone_flags, unsigned long sp,
-		unsigned long arg, struct task_struct * p)
+int copy_thread_tls(unsigned long clone_flags, unsigned long sp,
+		unsigned long arg, struct task_struct * p, unsigned long tls)
 {
 	void (*handler)(void);
 	int kthread = current->flags & PF_KTHREAD;
@@ -188,7 +188,7 @@ int copy_thread(unsigned long clone_flags, unsigned long sp,
 		 * Set a new TLS for the child thread?
 		 */
 		if (clone_flags & CLONE_SETTLS)
-			ret = arch_copy_tls(p);
+			ret = arch_set_tls(p, tls);
 	}
 
 	return ret;
arch/x86/um/tls_32.c +2 -4
index 5bd949da7a4a..ac8eee093f9c 100644
@@ -215,14 +215,12 @@ static int set_tls_entry(struct task_struct* task, struct user_desc *info,
 	return 0;
 }
 
-int arch_copy_tls(struct task_struct *new)
+int arch_set_tls(struct task_struct *new, unsigned long tls)
 {
 	struct user_desc info;
 	int idx, ret = -EFAULT;
 
-	if (copy_from_user(&info,
-			   (void __user *) UPT_SI(&new->thread.regs.regs),
-			   sizeof(info)))
+	if (copy_from_user(&info, (void __user *) tls, sizeof(info)))
 		goto out;
 
 	ret = -EINVAL;
arch/x86/um/tls_64.c +3 -4
index 3a621e0d3925..ebd3855d9b13 100644
@@ -6,14 +6,13 @@ void clear_flushed_tls(struct task_struct *task)
 {
 }
 
-int arch_copy_tls(struct task_struct *t)
+int arch_set_tls(struct task_struct *t, unsigned long tls)
 {
 	/*
 	 * If CLONE_SETTLS is set, we need to save the thread id
-	 * (which is argument 5, child_tid, of clone) so it can be set
-	 * during context switches.
+	 * so it can be set during context switches.
 	 */
-	t->thread.arch.fs = t->thread.regs.regs.gp[R8 / sizeof(long)];
+	t->thread.arch.fs = tls;
 
 	return 0;
 }
Signature · 7 lines
2.24.1


_______________________________________________
linux-um mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-um
Acked-by on v1 1/1
#26 lore raw

Re: [PATCH] um: Implement copy_thread_tls

Quoted 6 lines · Amanieu d'Antras

This is required for clone3 which passes the TLS value through a struct rather than a register.

Thanks. I'm picking this up as part of the copy_thread_tls() series. (Leaving the patch in tact so people can Ack right here if they want to.) If I could get an Ack from one of the maintainers that would be great; see
https://lore.kernel.org/lkml/[email protected]
for more context.

Quoted 103 lines
---
 arch/um/Kconfig                      | 1 +
 arch/um/include/asm/ptrace-generic.h | 2 +-
 arch/um/kernel/process.c             | 6 +++---
 arch/x86/um/tls_32.c                 | 6 ++----
 arch/x86/um/tls_64.c                 | 7 +++----
 5 files changed, 10 insertions(+), 12 deletions(-)
arch/um/Kconfig +1 -0
index 2a6d04fcb3e9..6f0edd0c0220 100644
@@ -14,6 +14,7 @@ config UML
 	select HAVE_FUTEX_CMPXCHG if FUTEX
 	select HAVE_DEBUG_KMEMLEAK
 	select HAVE_DEBUG_BUGVERBOSE
+	select HAVE_COPY_THREAD_TLS
 	select GENERIC_IRQ_SHOW
 	select GENERIC_CPU_DEVICES
 	select GENERIC_CLOCKEVENTS
arch/um/include/asm/ptrace-generic.h +1 -1
index 81c647ef9c6c..adf91ef553ae 100644
@@ -36,7 +36,7 @@ extern long subarch_ptrace(struct task_struct *child, long request,
 extern unsigned long getreg(struct task_struct *child, int regno);
 extern int putreg(struct task_struct *child, int regno, unsigned long value);
 
-extern int arch_copy_tls(struct task_struct *new);
+extern int arch_set_tls(struct task_struct *new, unsigned long tls);
 extern void clear_flushed_tls(struct task_struct *task);
 extern int syscall_trace_enter(struct pt_regs *regs);
 extern void syscall_trace_leave(struct pt_regs *regs);
arch/um/kernel/process.c +3 -3
index 263a8f069133..17045e7211bf 100644
@@ -153,8 +153,8 @@ void fork_handler(void)
 	userspace(&current->thread.regs.regs, current_thread_info()->aux_fp_regs);
 }
 
-int copy_thread(unsigned long clone_flags, unsigned long sp,
-		unsigned long arg, struct task_struct * p)
+int copy_thread_tls(unsigned long clone_flags, unsigned long sp,
+		unsigned long arg, struct task_struct * p, unsigned long tls)
 {
 	void (*handler)(void);
 	int kthread = current->flags & PF_KTHREAD;
@@ -188,7 +188,7 @@ int copy_thread(unsigned long clone_flags, unsigned long sp,
 		 * Set a new TLS for the child thread?
 		 */
 		if (clone_flags & CLONE_SETTLS)
-			ret = arch_copy_tls(p);
+			ret = arch_set_tls(p, tls);
 	}
 
 	return ret;
arch/x86/um/tls_32.c +2 -4
index 5bd949da7a4a..ac8eee093f9c 100644
@@ -215,14 +215,12 @@ static int set_tls_entry(struct task_struct* task, struct user_desc *info,
 	return 0;
 }
 
-int arch_copy_tls(struct task_struct *new)
+int arch_set_tls(struct task_struct *new, unsigned long tls)
 {
 	struct user_desc info;
 	int idx, ret = -EFAULT;
 
-	if (copy_from_user(&info,
-			   (void __user *) UPT_SI(&new->thread.regs.regs),
-			   sizeof(info)))
+	if (copy_from_user(&info, (void __user *) tls, sizeof(info)))
 		goto out;
 
 	ret = -EINVAL;
arch/x86/um/tls_64.c +3 -4
index 3a621e0d3925..ebd3855d9b13 100644
@@ -6,14 +6,13 @@ void clear_flushed_tls(struct task_struct *task)
 {
 }
 
-int arch_copy_tls(struct task_struct *t)
+int arch_set_tls(struct task_struct *t, unsigned long tls)
 {
 	/*
 	 * If CLONE_SETTLS is set, we need to save the thread id
-	 * (which is argument 5, child_tid, of clone) so it can be set
-	 * during context switches.
+	 * so it can be set during context switches.
 	 */
-	t->thread.arch.fs = t->thread.regs.regs.gp[R8 / sizeof(long)];
+	t->thread.arch.fs = tls;
 
 	return 0;
 }
Signature · 1 line
2.24.1
#27 lore raw

Re: [PATCH] um: Implement copy_thread_tls

Quoted 8 lines · Christian Brauner
Quoted 6 lines · Amanieu d'Antras

This is required for clone3 which passes the TLS value through a struct rather than a register.

Quoted 8 lines

Thanks. I'm picking this up as part of the copy_thread_tls() series. (Leaving the patch in tact so people can Ack right here if they want to.) If I could get an Ack from one of the maintainers that would be great; see
https://lore.kernel.org/lkml/[email protected]
for more context.

I've this up as part of the series link in above ^^ and moved it into
https://git.kernel.org/pub/scm/linux/kernel/git/brauner/linux.git/log/?h=clone3_tls

If I hear no objections I'll merge into into my fixes tree today or tomorrow.

An Ack from one of the maintainers would still be appreciated.

Thanks!
Christian

#28 lore raw

Re: [PATCH] um: Implement copy_thread_tls

Quoted 19 lines · Christian Brauner
Quoted 8 lines · Christian Brauner
Quoted 6 lines · Amanieu d'Antras

This is required for clone3 which passes the TLS value through a struct rather than a register.

Quoted 8 lines

Thanks. I'm picking this up as part of the copy_thread_tls() series. (Leaving the patch in tact so people can Ack right here if they want to.) If I could get an Ack from one of the maintainers that would be great; see
https://lore.kernel.org/lkml/[email protected]
for more context.

Quoted 7 lines

I've this up as part of the series link in above ^^ and moved it into
https://git.kernel.org/pub/scm/linux/kernel/git/brauner/linux.git/log/?h=clone3_tls

If I hear no objections I'll merge into into my fixes tree today or tomorrow.

An Ack from one of the maintainers would still be appreciated.

For sure too late, but better than nothing? ;-)

Signature · 7 lines
Thanks,
//richard

_______________________________________________
linux-um mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-um
#29 lore raw

Re: [PATCH] um: Implement copy_thread_tls

Quoted 24 lines · Richard Weinberger
Quoted 22 lines · Christian Brauner
Quoted 8 lines · Christian Brauner
Quoted 6 lines · Amanieu d'Antras

This is required for clone3 which passes the TLS value through a struct rather than a register.

Quoted 8 lines

Thanks. I'm picking this up as part of the copy_thread_tls() series. (Leaving the patch in tact so people can Ack right here if they want to.) If I could get an Ack from one of the maintainers that would be great; see
https://lore.kernel.org/lkml/[email protected]
for more context.

I've this up as part of the series link in above ^^ and moved it into
https://git.kernel.org/pub/scm/linux/kernel/git/brauner/linux.git/log/?h=clone3_tls

Quoted 8 lines
Quoted 4 lines

If I hear no objections I'll merge into into my fixes tree today or tomorrow.

An Ack from one of the maintainers would still be appreciated.

For sure too late, but better than nothing? ;-)

Still worth having it. :)

Thanks!
Christian

#30 lore raw
In reply to Amanieu d'Antras
Quoted 11 lines · Amanieu d'Antras

The clone3 syscall is currently broken when used with CLONE_SETTLS on all architectures that don't have an implementation of copy_thread_tls. The old copy_thread function handles CLONE_SETTLS by reading the new TLS value from pt_regs containing the clone syscall parameters. Since clone3 passes the TLS value in clone_args, this results in the TLS register being initialized to a garbage value.

This patch series implements copy_thread_tls on all architectures that currently define __ARCH_WANT_SYS_CLONE3 and adds a compile-time check to ensure that any architecture that enables clone3 in the future also implements copy_thread_tls.

Quoted 5 lines

I have also included a minor fix for the arm64 uapi headers which caused __NR_clone3 to be missing from the exported user headers.

I have only tested this on arm64, but the copy_thread_tls implementations for the various architectures are fairly straightforward.

I've picked up this series and moved it into
https://git.kernel.org/pub/scm/linux/kernel/git/brauner/linux.git/log/?h=clone3_tls

If I hear no objections I'll merge into into my fixes tree today or tomorrow.

Thanks!
Christian