Commit eed7c420 authored by Nicholas Piggin's avatar Nicholas Piggin Committed by Michael Ellerman

powerpc: copy_thread differentiate kthreads and user mode threads

When copy_thread is given a kernel function to run in arg->fn, this
does not necessarily mean it is a kernel thread. User threads can be
created this way (e.g., kernel_init, see also x86's copy_thread()).
These threads run a kernel function which may call kernel_execve()
and return, which returns like a userspace exec(2) syscall.

Kernel threads are to  be differentiated with PF_KTHREAD, will always
have arg->fn set, and should never return from that function, instead
calling kthread_exit() to exit.

Create separate paths for the kthread and user kernel thread creation
logic. The kthread path will never exit and does not require a user
interrupt frame, so it gets a minimal stack frame.
Signed-off-by: default avatarNicholas Piggin <npiggin@gmail.com>
Signed-off-by: default avatarMichael Ellerman <mpe@ellerman.id.au>
Link: https://msgid.link/20230325122904.2375060-6-npiggin@gmail.com
parent 5088a624
...@@ -176,9 +176,11 @@ static inline bool test_thread_local_flags(unsigned int flags) ...@@ -176,9 +176,11 @@ static inline bool test_thread_local_flags(unsigned int flags)
#ifdef CONFIG_COMPAT #ifdef CONFIG_COMPAT
#define is_32bit_task() (test_thread_flag(TIF_32BIT)) #define is_32bit_task() (test_thread_flag(TIF_32BIT))
#define is_tsk_32bit_task(tsk) (test_tsk_thread_flag(tsk, TIF_32BIT)) #define is_tsk_32bit_task(tsk) (test_tsk_thread_flag(tsk, TIF_32BIT))
#define clear_tsk_compat_task(tsk) (clear_tsk_thread_flag(p, TIF_32BIT))
#else #else
#define is_32bit_task() (IS_ENABLED(CONFIG_PPC32)) #define is_32bit_task() (IS_ENABLED(CONFIG_PPC32))
#define is_tsk_32bit_task(tsk) (IS_ENABLED(CONFIG_PPC32)) #define is_tsk_32bit_task(tsk) (IS_ENABLED(CONFIG_PPC32))
#define clear_tsk_compat_task(tsk) do { } while (0)
#endif #endif
#if defined(CONFIG_PPC64) #if defined(CONFIG_PPC64)
......
...@@ -1738,10 +1738,7 @@ static void setup_ksp_vsid(struct task_struct *p, unsigned long sp) ...@@ -1738,10 +1738,7 @@ static void setup_ksp_vsid(struct task_struct *p, unsigned long sp)
*/ */
int copy_thread(struct task_struct *p, const struct kernel_clone_args *args) int copy_thread(struct task_struct *p, const struct kernel_clone_args *args)
{ {
unsigned long clone_flags = args->flags; struct pt_regs *kregs; /* Switch frame regs */
unsigned long usp = args->stack;
unsigned long tls = args->tls;
struct pt_regs *childregs, *kregs;
extern void ret_from_fork(void); extern void ret_from_fork(void);
extern void ret_from_fork_scv(void); extern void ret_from_fork_scv(void);
extern void ret_from_kernel_thread(void); extern void ret_from_kernel_thread(void);
...@@ -1754,45 +1751,76 @@ int copy_thread(struct task_struct *p, const struct kernel_clone_args *args) ...@@ -1754,45 +1751,76 @@ int copy_thread(struct task_struct *p, const struct kernel_clone_args *args)
klp_init_thread_info(p); klp_init_thread_info(p);
/* Create initial stack frame. */ if (unlikely(p->flags & PF_KTHREAD)) {
sp -= STACK_USER_INT_FRAME_SIZE;
*(unsigned long *)(sp + STACK_INT_FRAME_MARKER) = STACK_FRAME_REGS_MARKER;
/* Copy registers */
childregs = (struct pt_regs *)(sp + STACK_INT_FRAME_REGS);
if (unlikely(args->fn)) {
/* kernel thread */ /* kernel thread */
/* Create initial minimum stack frame. */
sp -= STACK_FRAME_MIN_SIZE;
((unsigned long *)sp)[0] = 0; ((unsigned long *)sp)[0] = 0;
memset(childregs, 0, sizeof(struct pt_regs));
childregs->gpr[1] = sp + STACK_USER_INT_FRAME_SIZE;
#ifdef CONFIG_PPC64
clear_tsk_thread_flag(p, TIF_32BIT);
childregs->softe = IRQS_ENABLED;
#endif
p->thread.regs = NULL; /* no user register state */
ti->flags |= _TIF_RESTOREALL;
f = ret_from_kernel_thread; f = ret_from_kernel_thread;
p->thread.regs = NULL; /* no user register state */
clear_tsk_compat_task(p);
} else { } else {
/* user thread */ /* user thread */
struct pt_regs *regs = current_pt_regs(); struct pt_regs *childregs;
*childregs = *regs;
if (usp) /* Create initial user return stack frame. */
childregs->gpr[1] = usp; sp -= STACK_USER_INT_FRAME_SIZE;
((unsigned long *)sp)[0] = childregs->gpr[1]; *(unsigned long *)(sp + STACK_INT_FRAME_MARKER) = STACK_FRAME_REGS_MARKER;
p->thread.regs = childregs;
if (clone_flags & CLONE_SETTLS) { childregs = (struct pt_regs *)(sp + STACK_INT_FRAME_REGS);
if (!is_32bit_task())
childregs->gpr[13] = tls; if (unlikely(args->fn)) {
/*
* A user space thread, but it first runs a kernel
* thread, and then returns as though it had called
* execve rather than fork, so user regs will be
* filled in (e.g., by kernel_execve()).
*/
((unsigned long *)sp)[0] = 0;
memset(childregs, 0, sizeof(struct pt_regs));
#ifdef CONFIG_PPC64
childregs->softe = IRQS_ENABLED;
#endif
ti->flags |= _TIF_RESTOREALL;
f = ret_from_kernel_thread;
} else {
struct pt_regs *regs = current_pt_regs();
unsigned long clone_flags = args->flags;
unsigned long usp = args->stack;
/* Copy registers */
*childregs = *regs;
if (usp)
childregs->gpr[1] = usp;
((unsigned long *)sp)[0] = childregs->gpr[1];
#ifdef CONFIG_PPC_IRQ_SOFT_MASK_DEBUG
WARN_ON_ONCE(childregs->softe != IRQS_ENABLED);
#endif
if (clone_flags & CLONE_SETTLS) {
unsigned long tls = args->tls;
if (!is_32bit_task())
childregs->gpr[13] = tls;
else
childregs->gpr[2] = tls;
}
if (trap_is_scv(regs))
f = ret_from_fork_scv;
else else
childregs->gpr[2] = tls; f = ret_from_fork;
} }
if (trap_is_scv(regs)) #ifdef CONFIG_PPC64
f = ret_from_fork_scv; if (cpu_has_feature(CPU_FTR_HAS_PPR))
else childregs->ppr = DEFAULT_PPR;
f = ret_from_fork; #endif
childregs->msr &= ~(MSR_FP|MSR_VEC|MSR_VSX);
p->thread.regs = childregs;
} }
childregs->msr &= ~(MSR_FP|MSR_VEC|MSR_VSX);
/* /*
* The way this works is that at some point in the future * The way this works is that at some point in the future
...@@ -1843,8 +1871,6 @@ int copy_thread(struct task_struct *p, const struct kernel_clone_args *args) ...@@ -1843,8 +1871,6 @@ int copy_thread(struct task_struct *p, const struct kernel_clone_args *args)
p->thread.dscr_inherit = current->thread.dscr_inherit; p->thread.dscr_inherit = current->thread.dscr_inherit;
p->thread.dscr = mfspr(SPRN_DSCR); p->thread.dscr = mfspr(SPRN_DSCR);
} }
if (cpu_has_feature(CPU_FTR_HAS_PPR))
childregs->ppr = DEFAULT_PPR;
p->thread.tidr = 0; p->thread.tidr = 0;
#endif #endif
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment