Skip to contentSkip to conversation

powerpc: copy_thread cleanups

PATCH Applied Nicholas Piggin 2 participants 10 messages Mar 25, 2023 – Apr 26, 2023 PowerPC

lore
Share & tools
lore

Nicholas Piggin

author reviewer
#0 lore raw

v2 cover powerpc: copy_thread cleanups

This series cleans up copy_thread and hopefully makes the code easier to understand. The big changes in patch 3 and 4 which use the switch stack instead of user int stack for restoring fn and args parameters for the new thread.

I don't think there's any bugs fixed here because in the case of a kernel-user thread, kthread_exec would re-populate the user int frame with regs, so that should be okay.

Since v1, I only rejiged some changes between patches and minor cosmetic improvements but no functional change. Tested with 32e/s 64e/s BE and LE.

Thanks,
Nick

Nicholas Piggin (8):
powerpc: copy_thread remove unused pkey code
powerpc: copy_thread make ret_from_fork register setup consistent
powerpc: use switch frame for ret_from_kernel_thread parameters powerpc/64: ret_from_fork avoid restoring regs twice
powerpc: copy_thread differentiate kthreads and user mode threads
powerpc: differentiate kthread from user kernel thread start
powerpc: copy_thread don't set _TIF_RESTOREALL
powerpc: copy_thread don't set PPR in user interrupt frame regs

 arch/powerpc/include/asm/thread_info.h |   2 +
 arch/powerpc/kernel/entry_32.S         |  23 ++++-
 arch/powerpc/kernel/interrupt_64.S     |  28 +++++-
 arch/powerpc/kernel/process.c          | 124 ++++++++++++++-----------
 4 files changed, 113 insertions(+), 64 deletions(-)
Signature · 1 line
2.37.2
author reviewer
#1 lore raw

PATCH v2 1/8 powerpc: copy_thread remove unused pkey code

The pkey registers (AMR, IAMR) do not get loaded from the switch frame so it is pointless to save anything there. Remove the dead code.

---
 arch/powerpc/kernel/process.c | 12 +-----------
 1 file changed, 1 insertion(+), 11 deletions(-)
arch/powerpc/kernel/process.c +1 -11
index 4b29ac5ddac6..9b4431651ed5 100644
@@ -1813,6 +1813,7 @@ int copy_thread(struct task_struct *p, const struct kernel_clone_args *args)
 	sp -= STACK_SWITCH_FRAME_SIZE;
 	((unsigned long *)sp)[0] = sp + STACK_SWITCH_FRAME_SIZE;
 	kregs = (struct pt_regs *)(sp + STACK_SWITCH_FRAME_REGS);
+	kregs->nip = ppc_function_entry(f);
 	p->thread.ksp = sp;
 
 #ifdef CONFIG_HAVE_HW_BREAKPOINT
@@ -1845,17 +1846,6 @@ int copy_thread(struct task_struct *p, const struct kernel_clone_args *args)
 
 	p->thread.tidr = 0;
 #endif
-	/*
-	 * Run with the current AMR value of the kernel
-	 */
-#ifdef CONFIG_PPC_PKEY
-	if (mmu_has_feature(MMU_FTR_BOOK3S_KUAP))
-		kregs->amr = AMR_KUAP_BLOCKED;
-
-	if (mmu_has_feature(MMU_FTR_BOOK3S_KUEP))
-		kregs->iamr = AMR_KUEP_BLOCKED;
-#endif
-	kregs->nip = ppc_function_entry(f);
 	return 0;
 }
 
Signature · 1 line
2.37.2
author reviewer
#2 lore raw

PATCH v2 2/8 powerpc: copy_thread make ret_from_fork register setup consistent

In reply to Nicholas Piggin

The ret_from_fork code for 64e and 32-bit set r3 for syscall_exit_prepare the same way that 64s does, so there should be no need to special-case them in copy_thread.

---
 arch/powerpc/kernel/entry_32.S | 2 +-
 arch/powerpc/kernel/process.c  | 3 ---
 2 files changed, 1 insertion(+), 4 deletions(-)
arch/powerpc/kernel/entry_32.S +1 -1
index 5604c9a1ac22..755408c63be8 100644
@@ -183,7 +183,7 @@ syscall_exit_finish:
 ret_from_fork:
 	REST_NVGPRS(r1)
 	bl	schedule_tail
-	li	r3,0
+	li	r3,0	/* fork() return value */
 	b	ret_from_syscall
 
 	.globl	ret_from_kernel_thread
arch/powerpc/kernel/process.c +0 -3
index 9b4431651ed5..46e2c3f3a2e9 100644
@@ -1784,9 +1784,6 @@ int copy_thread(struct task_struct *p, const struct kernel_clone_args *args)
 			childregs->gpr[1] = usp;
 		((unsigned long *)sp)[0] = childregs->gpr[1];
 		p->thread.regs = childregs;
-		/* 64s sets this in ret_from_fork */
-		if (!IS_ENABLED(CONFIG_PPC_BOOK3S_64))
-			childregs->gpr[3] = 0;  /* Result from fork() */
 		if (clone_flags & CLONE_SETTLS) {
 			if (!is_32bit_task())
 				childregs->gpr[13] = tls;
Signature · 1 line
2.37.2
author reviewer
#3 lore raw

PATCH v2 3/8 powerpc: use switch frame for ret_from_kernel_thread parameters

In reply to Nicholas Piggin

The kernel thread path in copy_thread creates a user interrupt frame on stack and stores the function and arg parameters there, and ret_from_kernel_thread loads them. This is a slightly confusing way to overload that frame. Non-volatile registers are loaded from the switch frame, so the parameters can be stored there. The user interrupt frame is now only used by user threads when they return to user.

---
 arch/powerpc/kernel/entry_32.S     |  1 -
 arch/powerpc/kernel/interrupt_64.S |  1 -
 arch/powerpc/kernel/process.c      | 13 +++++++++----
 3 files changed, 9 insertions(+), 6 deletions(-)
arch/powerpc/kernel/entry_32.S +0 -1
index 755408c63be8..c3fdb3081d3d 100644
@@ -188,7 +188,6 @@ ret_from_fork:
 
 	.globl	ret_from_kernel_thread
 ret_from_kernel_thread:
-	REST_NVGPRS(r1)
 	bl	schedule_tail
 	mtctr	r14
 	mr	r3,r15
arch/powerpc/kernel/interrupt_64.S +0 -1
index fccc34489add..d60e7e7564df 100644
@@ -741,7 +741,6 @@ _GLOBAL(ret_from_fork)
 
 _GLOBAL(ret_from_kernel_thread)
 	bl	schedule_tail
-	REST_NVGPRS(r1)
 	mtctr	r14
 	mr	r3,r15
 #ifdef CONFIG_PPC64_ELF_ABI_V2
arch/powerpc/kernel/process.c +9 -4
index 46e2c3f3a2e9..c537bebb5dd2 100644
@@ -1765,14 +1765,10 @@ int copy_thread(struct task_struct *p, const struct kernel_clone_args *args)
 		((unsigned long *)sp)[0] = 0;
 		memset(childregs, 0, sizeof(struct pt_regs));
 		childregs->gpr[1] = sp + STACK_USER_INT_FRAME_SIZE;
-		/* function */
-		if (args->fn)
-			childregs->gpr[14] = ppc_function_entry((void *)args->fn);
 #ifdef CONFIG_PPC64
 		clear_tsk_thread_flag(p, TIF_32BIT);
 		childregs->softe = IRQS_ENABLED;
 #endif
-		childregs->gpr[15] = (unsigned long)args->fn_arg;
 		p->thread.regs = NULL;	/* no user register state */
 		ti->flags |= _TIF_RESTOREALL;
 		f = ret_from_kernel_thread;
@@ -1811,6 +1807,15 @@ int copy_thread(struct task_struct *p, const struct kernel_clone_args *args)
 	((unsigned long *)sp)[0] = sp + STACK_SWITCH_FRAME_SIZE;
 	kregs = (struct pt_regs *)(sp + STACK_SWITCH_FRAME_REGS);
 	kregs->nip = ppc_function_entry(f);
+	if (unlikely(args->fn)) {
+		/*
+		 * Put kthread fn, arg parameters in non-volatile GPRs in the
+		 * switch frame so they are loaded by _switch before it returns
+		 * to ret_from_kernel_thread.
+		 */
+		kregs->gpr[14] = ppc_function_entry((void *)args->fn);
+		kregs->gpr[15] = (unsigned long)args->fn_arg;
+	}
 	p->thread.ksp = sp;
 
 #ifdef CONFIG_HAVE_HW_BREAKPOINT
Signature · 1 line
2.37.2
author reviewer
#4 lore raw

PATCH v2 4/8 powerpc/64: ret_from_fork avoid restoring regs twice

In reply to Nicholas Piggin

If the system call return path always restores NVGPRs then there is no need for ret_from_fork to do it. The HANDLER_RESTORE_NVGPRS does the right thing for this.

---
 arch/powerpc/kernel/interrupt_64.S | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
arch/powerpc/kernel/interrupt_64.S +2 -2
index d60e7e7564df..bac1f89501ac 100644
@@ -728,14 +728,14 @@ DEFINE_FIXED_SYMBOL(__end_soft_masked, text)
 #ifdef CONFIG_PPC_BOOK3S
 _GLOBAL(ret_from_fork_scv)
 	bl	schedule_tail
-	REST_NVGPRS(r1)
+	HANDLER_RESTORE_NVGPRS()
 	li	r3,0	/* fork() return value */
 	b	.Lsyscall_vectored_common_exit
 #endif
 
 _GLOBAL(ret_from_fork)
 	bl	schedule_tail
-	REST_NVGPRS(r1)
+	HANDLER_RESTORE_NVGPRS()
 	li	r3,0	/* fork() return value */
 	b	.Lsyscall_exit
 
Signature · 1 line
2.37.2
author reviewer
#5 lore raw

PATCH v2 5/8 powerpc: copy_thread differentiate kthreads and user mode threads

In reply to Nicholas Piggin

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.

---
 arch/powerpc/include/asm/thread_info.h |  2 +
 arch/powerpc/kernel/process.c          | 98 ++++++++++++++++----------
 2 files changed, 64 insertions(+), 36 deletions(-)
arch/powerpc/include/asm/thread_info.h +2 -0
index af58f1ed3952..6c9e70402922 100644
@@ -175,9 +175,11 @@ static inline bool test_thread_local_flags(unsigned int flags)
 #ifdef CONFIG_COMPAT
 #define is_32bit_task()	(test_thread_flag(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
 #define is_32bit_task()	(IS_ENABLED(CONFIG_PPC32))
 #define is_tsk_32bit_task(tsk)	(IS_ENABLED(CONFIG_PPC32))
+#define clear_tsk_compat_task(tsk) do { } while (0)
 #endif
 
 #if defined(CONFIG_PPC64)
arch/powerpc/kernel/process.c +62 -36

125 lines not on this page · raw message

Signature · 1 line
2.37.2
author reviewer
#6 lore raw

PATCH v2 6/8 powerpc: differentiate kthread from user kernel thread start

In reply to Nicholas Piggin

Kernel created user threads start similarly to kernel threads in that they call a kernel function after first returning from _switch, so they share ret_from_kernel_thread for this. Kernel threads never return from that function though, whereas user threads often do (although some don't, e.g., IO threads).

Split these startup functions in two, and catch kernel threads that improperly return from their function. This is intended to make the complicated code a little bit easier to understand.

---
 arch/powerpc/kernel/entry_32.S     | 20 ++++++++++++++++++--
 arch/powerpc/kernel/interrupt_64.S | 18 +++++++++++++++++-
 arch/powerpc/kernel/process.c      |  7 ++++---
 3 files changed, 39 insertions(+), 6 deletions(-)
arch/powerpc/kernel/entry_32.S +18 -2
index c3fdb3081d3d..47f0dd9a45ad 100644
@@ -186,8 +186,8 @@ ret_from_fork:
 	li	r3,0	/* fork() return value */
 	b	ret_from_syscall
 
-	.globl	ret_from_kernel_thread
-ret_from_kernel_thread:
+	.globl	ret_from_kernel_user_thread
+ret_from_kernel_user_thread:
 	bl	schedule_tail
 	mtctr	r14
 	mr	r3,r15
@@ -196,6 +196,22 @@ ret_from_kernel_thread:
 	li	r3,0
 	b	ret_from_syscall
 
+	.globl	start_kernel_thread
+start_kernel_thread:
+	bl	schedule_tail
+	mtctr	r14
+	mr	r3,r15
+	PPC440EP_ERR42
+	bctrl
+	/*
+	 * This must not return. We actually want to BUG here, not WARN,
+	 * because BUG will exit the process which is what the kernel thread
+	 * should have done, which may give some hope of continuing.
+	 */
+100:	trap
+	EMIT_BUG_ENTRY 100b,__FILE__,__LINE__,0
+
+
 /*
  * This routine switches between two different tasks.  The process
  * state of one is saved on its kernel stack.  Then the state
arch/powerpc/kernel/interrupt_64.S +17 -1
index bac1f89501ac..a44c8aab63ec 100644
@@ -739,7 +739,7 @@ _GLOBAL(ret_from_fork)
 	li	r3,0	/* fork() return value */
 	b	.Lsyscall_exit
 
-_GLOBAL(ret_from_kernel_thread)
+_GLOBAL(ret_from_kernel_user_thread)
 	bl	schedule_tail
 	mtctr	r14
 	mr	r3,r15
@@ -749,3 +749,19 @@ _GLOBAL(ret_from_kernel_thread)
 	bctrl
 	li	r3,0
 	b	.Lsyscall_exit
+
+_GLOBAL(start_kernel_thread)
+	bl	schedule_tail
+	mtctr	r14
+	mr	r3,r15
+#ifdef CONFIG_PPC64_ELF_ABI_V2
+	mr	r12,r14
+#endif
+	bctrl
+	/*
+	 * This must not return. We actually want to BUG here, not WARN,
+	 * because BUG will exit the process which is what the kernel thread
+	 * should have done, which may give some hope of continuing.
+	 */
+100:	trap
+	EMIT_BUG_ENTRY 100b,__FILE__,__LINE__,0
arch/powerpc/kernel/process.c +4 -3
index 7b016f958e8b..307639948691 100644
@@ -1741,7 +1741,8 @@ int copy_thread(struct task_struct *p, const struct kernel_clone_args *args)
 	struct pt_regs *kregs; /* Switch frame regs */
 	extern void ret_from_fork(void);
 	extern void ret_from_fork_scv(void);
-	extern void ret_from_kernel_thread(void);
+	extern void ret_from_kernel_user_thread(void);
+	extern void start_kernel_thread(void);
 	void (*f)(void);
 	unsigned long sp = (unsigned long)task_stack_page(p) + THREAD_SIZE;
 	struct thread_info *ti = task_thread_info(p);
@@ -1758,7 +1759,7 @@ int copy_thread(struct task_struct *p, const struct kernel_clone_args *args)
 		sp -= STACK_FRAME_MIN_SIZE;
 		((unsigned long *)sp)[0] = 0;
 
-		f = ret_from_kernel_thread;
+		f = start_kernel_thread;
 		p->thread.regs = NULL;	/* no user register state */
 		clear_tsk_compat_task(p);
 	} else {
@@ -1784,7 +1785,7 @@ int copy_thread(struct task_struct *p, const struct kernel_clone_args *args)
 			childregs->softe = IRQS_ENABLED;
 #endif
 			ti->flags |= _TIF_RESTOREALL;
-			f = ret_from_kernel_thread;
+			f = ret_from_kernel_user_thread;
 		} else {
 			struct pt_regs *regs = current_pt_regs();
 			unsigned long clone_flags = args->flags;
Signature · 1 line
2.37.2
author reviewer
#7 lore raw

PATCH v2 7/8 powerpc: copy_thread don't set _TIF_RESTOREALL

In reply to Nicholas Piggin

In the kernel user thread path, don't set _TIF_RESTOREALL because the thread is required to call kernel_execve() before it returns, which will set _TIF_RESTOREALL if necessary via start_thread().

---
 arch/powerpc/kernel/interrupt_64.S | 5 +++++
 arch/powerpc/kernel/process.c      | 2 --
 2 files changed, 5 insertions(+), 2 deletions(-)
arch/powerpc/kernel/interrupt_64.S +5 -0
index a44c8aab63ec..2a059214c1a9 100644
@@ -748,6 +748,11 @@ _GLOBAL(ret_from_kernel_user_thread)
 #endif
 	bctrl
 	li	r3,0
+	/*
+	 * It does not matter whether this returns via the scv or sc path
+	 * because it returns as execve() and therefore has no calling ABI
+	 * (i.e., it sets registers according to the exec()ed entry point).
+	 */
 	b	.Lsyscall_exit
 
 _GLOBAL(start_kernel_thread)
arch/powerpc/kernel/process.c +0 -2
index 307639948691..4140b25f489f 100644
@@ -1745,7 +1745,6 @@ int copy_thread(struct task_struct *p, const struct kernel_clone_args *args)
 	extern void start_kernel_thread(void);
 	void (*f)(void);
 	unsigned long sp = (unsigned long)task_stack_page(p) + THREAD_SIZE;
-	struct thread_info *ti = task_thread_info(p);
 #ifdef CONFIG_HAVE_HW_BREAKPOINT
 	int i;
 #endif
@@ -1784,7 +1783,6 @@ int copy_thread(struct task_struct *p, const struct kernel_clone_args *args)
 #ifdef CONFIG_PPC64
 			childregs->softe = IRQS_ENABLED;
 #endif
-			ti->flags |= _TIF_RESTOREALL;
 			f = ret_from_kernel_user_thread;
 		} else {
 			struct pt_regs *regs = current_pt_regs();
Signature · 1 line
2.37.2
author reviewer
#8 lore raw

PATCH v2 8/8 powerpc: copy_thread don't set PPR in user interrupt frame regs

In reply to Nicholas Piggin

syscalls do not set the PPR field in their interrupt frame and return from syscall always sets the default PPR for userspace, so setting the value in the ret_from_fork frame is not necessary and mildly inconsistent. Remove it.

---
 arch/powerpc/kernel/process.c | 5 -----
 1 file changed, 5 deletions(-)
arch/powerpc/kernel/process.c +0 -5
index 4140b25f489f..52e18cec865b 100644
@@ -1812,11 +1812,6 @@ int copy_thread(struct task_struct *p, const struct kernel_clone_args *args)
 				f = ret_from_fork;
 		}
 
-#ifdef CONFIG_PPC64
-		if (cpu_has_feature(CPU_FTR_HAS_PPR))
-			childregs->ppr = DEFAULT_PPR;
-#endif
-
 		childregs->msr &= ~(MSR_FP|MSR_VEC|MSR_VSX);
 		p->thread.regs = childregs;
 	}
Signature · 1 line
2.37.2
#9 lore raw
In reply to Nicholas Piggin
Quoted 10 lines · Nicholas Piggin

This series cleans up copy_thread and hopefully makes the code easier to understand. The big changes in patch 3 and 4 which use the switch stack instead of user int stack for restoring fn and args parameters for the new thread.

I don't think there's any bugs fixed here because in the case of a kernel-user thread, kthread_exec would re-populate the user int frame with regs, so that should be okay.

[...]

Applied to powerpc/next.

[1/8] powerpc: copy_thread remove unused pkey code

      https://git.kernel.org/powerpc/c/c013e9f2bbe1d2be5e1c7f4a84216cd10837f20d
[2/8] powerpc: copy_thread make ret_from_fork register setup consistent
      https://git.kernel.org/powerpc/c/959791e45fd2a580403e03611a5aefb9e7abcfc0
[3/8] powerpc: use switch frame for ret_from_kernel_thread parameters
      https://git.kernel.org/powerpc/c/af5ca9d5c8b45244b237d7a5534e1ec2d01cce8e
[4/8] powerpc/64: ret_from_fork avoid restoring regs twice
      https://git.kernel.org/powerpc/c/5088a6246bd3dcfea504376f356683f750136f7f
[5/8] powerpc: copy_thread differentiate kthreads and user mode threads
      https://git.kernel.org/powerpc/c/eed7c420aac7fde5e5915d2747c3ebbbda225835
[6/8] powerpc: differentiate kthread from user kernel thread start
      https://git.kernel.org/powerpc/c/b504b6aade0403eaffa9ce51b8207d710705beaf
[7/8] powerpc: copy_thread don't set _TIF_RESTOREALL
      https://git.kernel.org/powerpc/c/d195ce4695ca1061993424e2d6c8995e5fc81606
[8/8] powerpc: copy_thread don't set PPR in user interrupt frame regs
      https://git.kernel.org/powerpc/c/89fb39134ae3b1e1f207af44a037721d92b32f70

cheers