[PATCH RFC 04/28] ARM: process: Rewrite ret_from_fork i C

From: Linus Walleij
Date: Thu Oct 10 2024 - 07:34:30 EST


Move the direct assembly call to schedule_tail() and the
call starting a kernel thread into a C version of ret_from_fork()
in process.c and rename the old ret_from_fork to ret_from_fork_asm
following the pattern in x86.

Leave a comment on what we will do when we get to switching to
generic entry with this as a base.

Signed-off-by: Linus Walleij <linus.walleij@xxxxxxxxxx>
---
arch/arm/include/asm/switch_to.h | 4 ++++
arch/arm/kernel/entry-common.S | 22 +++++++++++++++-------
arch/arm/kernel/process.c | 24 ++++++++++++++++++++++--
3 files changed, 41 insertions(+), 9 deletions(-)

diff --git a/arch/arm/include/asm/switch_to.h b/arch/arm/include/asm/switch_to.h
index 9372348516ce..18d084aff5a4 100644
--- a/arch/arm/include/asm/switch_to.h
+++ b/arch/arm/include/asm/switch_to.h
@@ -17,6 +17,10 @@
#define __complete_pending_tlbi()
#endif

+asmlinkage void ret_from_fork_asm(void);
+__visible void ret_from_fork(struct task_struct *prev, struct pt_regs *regs,
+ int (*fn)(void *), void *fn_arg);
+
/*
* switch_to(prev, next) should switch from task `prev' to `next'
* `prev' will never be the same as `next'. schedule() itself
diff --git a/arch/arm/kernel/entry-common.S b/arch/arm/kernel/entry-common.S
index 2f93c2f6d5b8..df6961a1006b 100644
--- a/arch/arm/kernel/entry-common.S
+++ b/arch/arm/kernel/entry-common.S
@@ -5,6 +5,7 @@
* Copyright (C) 2000 Russell King
*/

+#include <linux/cfi_types.h>
#include <asm/assembler.h>
#include <asm/unistd.h>
#include <asm/ftrace.h>
@@ -128,15 +129,22 @@ ENDPROC(ret_to_user)
/*
* This is how we return from a fork.
*/
-ENTRY(ret_from_fork)
- bl schedule_tail
- cmp r5, #0
- movne r0, r4
- badrne lr, 1f
- retne r5
+SYM_TYPED_FUNC_START(ret_from_fork_asm)
+ /*
+ * ret_from_fork() expects:
+ * r0: prev
+ * r1: regs
+ * r2: fn() for kernel process or NULL
+ * r3: fn_arg for kernel process or N/A
+ */
+ mov r1, sp
+ mov r2, r5
+ mov r3, r4
+ bl ret_from_fork
+ mov r0, #0
1: get_thread_info tsk
b ret_to_user
-ENDPROC(ret_from_fork)
+SYM_FUNC_END(ret_from_fork_asm)

/*=============================================================================
* SWI handler
diff --git a/arch/arm/kernel/process.c b/arch/arm/kernel/process.c
index e16ed102960c..81c1c3f98834 100644
--- a/arch/arm/kernel/process.c
+++ b/arch/arm/kernel/process.c
@@ -29,6 +29,7 @@
#include <asm/processor.h>
#include <asm/thread_notify.h>
#include <asm/stacktrace.h>
+#include <asm/switch_to.h>
#include <asm/system_misc.h>
#include <asm/mach/time.h>
#include <asm/tls.h>
@@ -230,7 +231,26 @@ void flush_thread(void)
thread_notify(THREAD_NOTIFY_FLUSH, thread);
}

-asmlinkage void ret_from_fork(void) __asm__("ret_from_fork");
+/* The assembly stub sets up the parameters and calls ret_from_fork() */
+asmlinkage void ret_from_fork_asm(void) __asm__("ret_from_fork_asm");
+
+__visible void ret_from_fork(struct task_struct *prev, struct pt_regs *regs,
+ int (*fn)(void *), void *fn_arg)
+{
+ schedule_tail(prev);
+
+ /* Is this a kernel thread? */
+ if (unlikely(fn)) {
+ fn(fn_arg);
+ /*
+ * A kernel thread is allowed to return here after successfully
+ * calling kernel_execve(). Exit to userspace to complete the
+ * execve() syscall.
+ */
+ }
+
+ /* syscall_exit_to_user_mode(regs); here once we switch to generic entry */
+}

int copy_thread(struct task_struct *p, const struct kernel_clone_args *args)
{
@@ -263,7 +283,7 @@ int copy_thread(struct task_struct *p, const struct kernel_clone_args *args)
thread->cpu_context.r5 = (unsigned long)args->fn;
childregs->ARM_cpsr = SVC_MODE;
}
- thread->cpu_context.pc = (unsigned long)ret_from_fork;
+ thread->cpu_context.pc = (unsigned long)ret_from_fork_asm;
thread->cpu_context.sp = (unsigned long)childregs;

clear_ptrace_hw_breakpoint(p);

--
2.46.2