Skip to content

Commit 5b3d610

Browse files
charlie-rivosKAGA-KOKO
authored andcommitted
riscv: entry: Split ret_from_fork() into user and kernel
This function was unified into a single function in commit ab9164d ("riscv: entry: Consolidate ret_from_kernel_thread into ret_from_fork"). However that imposed a performance degradation. Partially reverting this commit to have ret_from_fork() split again, results in a 1% increase on the number of times fork is able to be called per second. Signed-off-by: Charlie Jenkins <charlie@rivosinc.com> Signed-off-by: Thomas Gleixner <tglx@linutronix.de> Acked-by: Alexandre Ghiti <alexghiti@rivosinc.com> Link: https://lore.kernel.org/all/20250320-riscv_optimize_entry-v6-2-63e187e26041@rivosinc.com
1 parent f955aa8 commit 5b3d610

File tree

3 files changed

+23
-10
lines changed

3 files changed

+23
-10
lines changed

arch/riscv/include/asm/asm-prototypes.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,8 @@ DECLARE_DO_ERROR_INFO(do_trap_ecall_s);
5252
DECLARE_DO_ERROR_INFO(do_trap_ecall_m);
5353
DECLARE_DO_ERROR_INFO(do_trap_break);
5454

55-
asmlinkage void ret_from_fork(void *fn_arg, int (*fn)(void *), struct pt_regs *regs);
55+
asmlinkage void ret_from_fork_kernel(void *fn_arg, int (*fn)(void *), struct pt_regs *regs);
56+
asmlinkage void ret_from_fork_user(struct pt_regs *regs);
5657
asmlinkage void handle_bad_stack(struct pt_regs *regs);
5758
asmlinkage void do_page_fault(struct pt_regs *regs);
5859
asmlinkage void do_irq(struct pt_regs *regs);

arch/riscv/kernel/entry.S

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -319,14 +319,21 @@ SYM_CODE_END(handle_kernel_stack_overflow)
319319
ASM_NOKPROBE(handle_kernel_stack_overflow)
320320
#endif
321321

322-
SYM_CODE_START(ret_from_fork_asm)
322+
SYM_CODE_START(ret_from_fork_kernel_asm)
323323
call schedule_tail
324324
move a0, s1 /* fn_arg */
325325
move a1, s0 /* fn */
326326
move a2, sp /* pt_regs */
327-
call ret_from_fork
327+
call ret_from_fork_kernel
328328
j ret_from_exception
329-
SYM_CODE_END(ret_from_fork_asm)
329+
SYM_CODE_END(ret_from_fork_kernel_asm)
330+
331+
SYM_CODE_START(ret_from_fork_user_asm)
332+
call schedule_tail
333+
move a0, sp /* pt_regs */
334+
call ret_from_fork_user
335+
j ret_from_exception
336+
SYM_CODE_END(ret_from_fork_user_asm)
330337

331338
#ifdef CONFIG_IRQ_STACKS
332339
/*

arch/riscv/kernel/process.c

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,8 @@ unsigned long __stack_chk_guard __read_mostly;
3838
EXPORT_SYMBOL(__stack_chk_guard);
3939
#endif
4040

41-
extern asmlinkage void ret_from_fork_asm(void);
41+
extern asmlinkage void ret_from_fork_kernel_asm(void);
42+
extern asmlinkage void ret_from_fork_user_asm(void);
4243

4344
void noinstr arch_cpu_idle(void)
4445
{
@@ -208,14 +209,18 @@ int arch_dup_task_struct(struct task_struct *dst, struct task_struct *src)
208209
return 0;
209210
}
210211

211-
asmlinkage void ret_from_fork(void *fn_arg, int (*fn)(void *), struct pt_regs *regs)
212+
asmlinkage void ret_from_fork_kernel(void *fn_arg, int (*fn)(void *), struct pt_regs *regs)
212213
{
213-
if (unlikely(fn))
214-
fn(fn_arg);
214+
fn(fn_arg);
215215

216216
syscall_exit_to_user_mode(regs);
217217
}
218218

219+
asmlinkage void ret_from_fork_user(struct pt_regs *regs)
220+
{
221+
syscall_exit_to_user_mode(regs);
222+
}
223+
219224
int copy_thread(struct task_struct *p, const struct kernel_clone_args *args)
220225
{
221226
unsigned long clone_flags = args->flags;
@@ -238,6 +243,7 @@ int copy_thread(struct task_struct *p, const struct kernel_clone_args *args)
238243

239244
p->thread.s[0] = (unsigned long)args->fn;
240245
p->thread.s[1] = (unsigned long)args->fn_arg;
246+
p->thread.ra = (unsigned long)ret_from_fork_kernel_asm;
241247
} else {
242248
*childregs = *(current_pt_regs());
243249
/* Turn off status.VS */
@@ -247,12 +253,11 @@ int copy_thread(struct task_struct *p, const struct kernel_clone_args *args)
247253
if (clone_flags & CLONE_SETTLS)
248254
childregs->tp = tls;
249255
childregs->a0 = 0; /* Return value of fork() */
250-
p->thread.s[0] = 0;
256+
p->thread.ra = (unsigned long)ret_from_fork_user_asm;
251257
}
252258
p->thread.riscv_v_flags = 0;
253259
if (has_vector() || has_xtheadvector())
254260
riscv_v_thread_alloc(p);
255-
p->thread.ra = (unsigned long)ret_from_fork_asm;
256261
p->thread.sp = (unsigned long)childregs; /* kernel sp */
257262
return 0;
258263
}

0 commit comments

Comments
 (0)