Skip to content

Commit c006d5d

Browse files
committed
Merge commit 'core-entry-2025-05-25' into loongarch-next
LoongArch architecture changes for 6.16 modify some same files with the core-entry changes, so merge them to create a base to resolve conflicts.
2 parents 0ff41df + 8278fd6 commit c006d5d

File tree

8 files changed

+119
-79
lines changed

8 files changed

+119
-79
lines changed

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,11 @@ __int128_t __ashlti3(__int128_t a, int b);
1212
__int128_t __ashrti3(__int128_t a, int b);
1313
__int128_t __lshrti3(__int128_t a, int b);
1414
#endif
15+
16+
asmlinkage void noinstr __no_stack_protector ret_from_fork(struct task_struct *prev,
17+
struct pt_regs *regs);
18+
19+
asmlinkage void noinstr __no_stack_protector ret_from_kernel_thread(struct task_struct *prev,
20+
struct pt_regs *regs,
21+
int (*fn)(void *),
22+
void *fn_arg);

arch/loongarch/kernel/entry.S

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -77,24 +77,22 @@ SYM_CODE_START(handle_syscall)
7777
SYM_CODE_END(handle_syscall)
7878
_ASM_NOKPROBE(handle_syscall)
7979

80-
SYM_CODE_START(ret_from_fork)
80+
SYM_CODE_START(ret_from_fork_asm)
8181
UNWIND_HINT_REGS
82-
bl schedule_tail # a0 = struct task_struct *prev
83-
move a0, sp
84-
bl syscall_exit_to_user_mode
82+
move a1, sp
83+
bl ret_from_fork
8584
RESTORE_STATIC
8685
RESTORE_SOME
8786
RESTORE_SP_AND_RET
88-
SYM_CODE_END(ret_from_fork)
87+
SYM_CODE_END(ret_from_fork_asm)
8988

90-
SYM_CODE_START(ret_from_kernel_thread)
89+
SYM_CODE_START(ret_from_kernel_thread_asm)
9190
UNWIND_HINT_REGS
92-
bl schedule_tail # a0 = struct task_struct *prev
93-
move a0, s1
94-
jirl ra, s0, 0
95-
move a0, sp
96-
bl syscall_exit_to_user_mode
91+
move a1, sp
92+
move a2, s0
93+
move a3, s1
94+
bl ret_from_kernel_thread
9795
RESTORE_STATIC
9896
RESTORE_SOME
9997
RESTORE_SP_AND_RET
100-
SYM_CODE_END(ret_from_kernel_thread)
98+
SYM_CODE_END(ret_from_kernel_thread_asm)

arch/loongarch/kernel/process.c

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#include <linux/cpu.h>
1414
#include <linux/init.h>
1515
#include <linux/kernel.h>
16+
#include <linux/entry-common.h>
1617
#include <linux/errno.h>
1718
#include <linux/sched.h>
1819
#include <linux/sched/debug.h>
@@ -34,6 +35,7 @@
3435
#include <linux/nmi.h>
3536

3637
#include <asm/asm.h>
38+
#include <asm/asm-prototypes.h>
3739
#include <asm/bootinfo.h>
3840
#include <asm/cpu.h>
3941
#include <asm/elf.h>
@@ -47,6 +49,7 @@
4749
#include <asm/pgtable.h>
4850
#include <asm/processor.h>
4951
#include <asm/reg.h>
52+
#include <asm/switch_to.h>
5053
#include <asm/unwind.h>
5154
#include <asm/vdso.h>
5255

@@ -63,8 +66,9 @@ EXPORT_SYMBOL(__stack_chk_guard);
6366
unsigned long boot_option_idle_override = IDLE_NO_OVERRIDE;
6467
EXPORT_SYMBOL(boot_option_idle_override);
6568

66-
asmlinkage void ret_from_fork(void);
67-
asmlinkage void ret_from_kernel_thread(void);
69+
asmlinkage void restore_and_ret(void);
70+
asmlinkage void ret_from_fork_asm(void);
71+
asmlinkage void ret_from_kernel_thread_asm(void);
6872

6973
void start_thread(struct pt_regs *regs, unsigned long pc, unsigned long sp)
7074
{
@@ -138,6 +142,23 @@ int arch_dup_task_struct(struct task_struct *dst, struct task_struct *src)
138142
return 0;
139143
}
140144

145+
asmlinkage void noinstr __no_stack_protector ret_from_fork(struct task_struct *prev,
146+
struct pt_regs *regs)
147+
{
148+
schedule_tail(prev);
149+
syscall_exit_to_user_mode(regs);
150+
}
151+
152+
asmlinkage void noinstr __no_stack_protector ret_from_kernel_thread(struct task_struct *prev,
153+
struct pt_regs *regs,
154+
int (*fn)(void *),
155+
void *fn_arg)
156+
{
157+
schedule_tail(prev);
158+
fn(fn_arg);
159+
syscall_exit_to_user_mode(regs);
160+
}
161+
141162
/*
142163
* Copy architecture-specific thread state
143164
*/
@@ -165,8 +186,8 @@ int copy_thread(struct task_struct *p, const struct kernel_clone_args *args)
165186
p->thread.reg03 = childksp;
166187
p->thread.reg23 = (unsigned long)args->fn;
167188
p->thread.reg24 = (unsigned long)args->fn_arg;
168-
p->thread.reg01 = (unsigned long)ret_from_kernel_thread;
169-
p->thread.sched_ra = (unsigned long)ret_from_kernel_thread;
189+
p->thread.reg01 = (unsigned long)ret_from_kernel_thread_asm;
190+
p->thread.sched_ra = (unsigned long)ret_from_kernel_thread_asm;
170191
memset(childregs, 0, sizeof(struct pt_regs));
171192
childregs->csr_euen = p->thread.csr_euen;
172193
childregs->csr_crmd = p->thread.csr_crmd;
@@ -182,8 +203,8 @@ int copy_thread(struct task_struct *p, const struct kernel_clone_args *args)
182203
childregs->regs[3] = usp;
183204

184205
p->thread.reg03 = (unsigned long) childregs;
185-
p->thread.reg01 = (unsigned long) ret_from_fork;
186-
p->thread.sched_ra = (unsigned long) ret_from_fork;
206+
p->thread.reg01 = (unsigned long) ret_from_fork_asm;
207+
p->thread.sched_ra = (unsigned long) ret_from_fork_asm;
187208

188209
/*
189210
* New tasks lose permission to use the fpu. This accelerates context

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +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_kernel(void *fn_arg, int (*fn)(void *), struct pt_regs *regs);
56+
asmlinkage void ret_from_fork_user(struct pt_regs *regs);
5557
asmlinkage void handle_bad_stack(struct pt_regs *regs);
5658
asmlinkage void do_page_fault(struct pt_regs *regs);
5759
asmlinkage void do_irq(struct pt_regs *regs);

arch/riscv/kernel/entry.S

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -319,17 +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)
322+
SYM_CODE_START(ret_from_fork_kernel_asm)
323+
call schedule_tail
324+
move a0, s1 /* fn_arg */
325+
move a1, s0 /* fn */
326+
move a2, sp /* pt_regs */
327+
call ret_from_fork_kernel
328+
j ret_from_exception
329+
SYM_CODE_END(ret_from_fork_kernel_asm)
330+
331+
SYM_CODE_START(ret_from_fork_user_asm)
323332
call schedule_tail
324-
beqz s0, 1f /* not from kernel thread */
325-
/* Call fn(arg) */
326-
move a0, s1
327-
jalr s0
328-
1:
329333
move a0, sp /* pt_regs */
330-
call syscall_exit_to_user_mode
334+
call ret_from_fork_user
331335
j ret_from_exception
332-
SYM_CODE_END(ret_from_fork)
336+
SYM_CODE_END(ret_from_fork_user_asm)
333337

334338
#ifdef CONFIG_IRQ_STACKS
335339
/*

arch/riscv/kernel/process.c

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,9 @@
1717
#include <linux/ptrace.h>
1818
#include <linux/uaccess.h>
1919
#include <linux/personality.h>
20+
#include <linux/entry-common.h>
2021

22+
#include <asm/asm-prototypes.h>
2123
#include <asm/unistd.h>
2224
#include <asm/processor.h>
2325
#include <asm/csr.h>
@@ -36,7 +38,8 @@ unsigned long __stack_chk_guard __read_mostly;
3638
EXPORT_SYMBOL(__stack_chk_guard);
3739
#endif
3840

39-
extern asmlinkage void ret_from_fork(void);
41+
extern asmlinkage void ret_from_fork_kernel_asm(void);
42+
extern asmlinkage void ret_from_fork_user_asm(void);
4043

4144
void noinstr arch_cpu_idle(void)
4245
{
@@ -206,6 +209,18 @@ int arch_dup_task_struct(struct task_struct *dst, struct task_struct *src)
206209
return 0;
207210
}
208211

212+
asmlinkage void ret_from_fork_kernel(void *fn_arg, int (*fn)(void *), struct pt_regs *regs)
213+
{
214+
fn(fn_arg);
215+
216+
syscall_exit_to_user_mode(regs);
217+
}
218+
219+
asmlinkage void ret_from_fork_user(struct pt_regs *regs)
220+
{
221+
syscall_exit_to_user_mode(regs);
222+
}
223+
209224
int copy_thread(struct task_struct *p, const struct kernel_clone_args *args)
210225
{
211226
unsigned long clone_flags = args->flags;
@@ -228,6 +243,7 @@ int copy_thread(struct task_struct *p, const struct kernel_clone_args *args)
228243

229244
p->thread.s[0] = (unsigned long)args->fn;
230245
p->thread.s[1] = (unsigned long)args->fn_arg;
246+
p->thread.ra = (unsigned long)ret_from_fork_kernel_asm;
231247
} else {
232248
*childregs = *(current_pt_regs());
233249
/* Turn off status.VS */
@@ -237,12 +253,11 @@ int copy_thread(struct task_struct *p, const struct kernel_clone_args *args)
237253
if (clone_flags & CLONE_SETTLS)
238254
childregs->tp = tls;
239255
childregs->a0 = 0; /* Return value of fork() */
240-
p->thread.s[0] = 0;
256+
p->thread.ra = (unsigned long)ret_from_fork_user_asm;
241257
}
242258
p->thread.riscv_v_flags = 0;
243259
if (has_vector() || has_xtheadvector())
244260
riscv_v_thread_alloc(p);
245-
p->thread.ra = (unsigned long)ret_from_fork;
246261
p->thread.sp = (unsigned long)childregs; /* kernel sp */
247262
return 0;
248263
}

include/linux/entry-common.h

Lines changed: 41 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
#include <linux/kmsan.h>
1515

1616
#include <asm/entry-common.h>
17+
#include <asm/syscall.h>
1718

1819
/*
1920
* Define dummy _TIF work flags if not defined by the architecture or for
@@ -366,6 +367,15 @@ static __always_inline void exit_to_user_mode(void)
366367
lockdep_hardirqs_on(CALLER_ADDR0);
367368
}
368369

370+
/**
371+
* syscall_exit_work - Handle work before returning to user mode
372+
* @regs: Pointer to current pt_regs
373+
* @work: Current thread syscall work
374+
*
375+
* Do one-time syscall specific work.
376+
*/
377+
void syscall_exit_work(struct pt_regs *regs, unsigned long work);
378+
369379
/**
370380
* syscall_exit_to_user_mode_work - Handle work before returning to user mode
371381
* @regs: Pointer to currents pt_regs
@@ -379,7 +389,30 @@ static __always_inline void exit_to_user_mode(void)
379389
* make the final state transitions. Interrupts must stay disabled between
380390
* return from this function and the invocation of exit_to_user_mode().
381391
*/
382-
void syscall_exit_to_user_mode_work(struct pt_regs *regs);
392+
static __always_inline void syscall_exit_to_user_mode_work(struct pt_regs *regs)
393+
{
394+
unsigned long work = READ_ONCE(current_thread_info()->syscall_work);
395+
unsigned long nr = syscall_get_nr(current, regs);
396+
397+
CT_WARN_ON(ct_state() != CT_STATE_KERNEL);
398+
399+
if (IS_ENABLED(CONFIG_PROVE_LOCKING)) {
400+
if (WARN(irqs_disabled(), "syscall %lu left IRQs disabled", nr))
401+
local_irq_enable();
402+
}
403+
404+
rseq_syscall(regs);
405+
406+
/*
407+
* Do one-time syscall specific work. If these work items are
408+
* enabled, we want to run them exactly once per syscall exit with
409+
* interrupts enabled.
410+
*/
411+
if (unlikely(work & SYSCALL_WORK_EXIT))
412+
syscall_exit_work(regs, work);
413+
local_irq_disable_exit_to_user();
414+
exit_to_user_mode_prepare(regs);
415+
}
383416

384417
/**
385418
* syscall_exit_to_user_mode - Handle work before returning to user mode
@@ -410,7 +443,13 @@ void syscall_exit_to_user_mode_work(struct pt_regs *regs);
410443
* exit_to_user_mode(). This function is preferred unless there is a
411444
* compelling architectural reason to use the separate functions.
412445
*/
413-
void syscall_exit_to_user_mode(struct pt_regs *regs);
446+
static __always_inline void syscall_exit_to_user_mode(struct pt_regs *regs)
447+
{
448+
instrumentation_begin();
449+
syscall_exit_to_user_mode_work(regs);
450+
instrumentation_end();
451+
exit_to_user_mode();
452+
}
414453

415454
/**
416455
* irqentry_enter_from_user_mode - Establish state before invoking the irq handler

kernel/entry/common.c

Lines changed: 1 addition & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ static inline bool report_single_step(unsigned long work)
146146
return work & SYSCALL_WORK_SYSCALL_EXIT_TRAP;
147147
}
148148

149-
static void syscall_exit_work(struct pt_regs *regs, unsigned long work)
149+
void syscall_exit_work(struct pt_regs *regs, unsigned long work)
150150
{
151151
bool step;
152152

@@ -173,53 +173,6 @@ static void syscall_exit_work(struct pt_regs *regs, unsigned long work)
173173
ptrace_report_syscall_exit(regs, step);
174174
}
175175

176-
/*
177-
* Syscall specific exit to user mode preparation. Runs with interrupts
178-
* enabled.
179-
*/
180-
static void syscall_exit_to_user_mode_prepare(struct pt_regs *regs)
181-
{
182-
unsigned long work = READ_ONCE(current_thread_info()->syscall_work);
183-
unsigned long nr = syscall_get_nr(current, regs);
184-
185-
CT_WARN_ON(ct_state() != CT_STATE_KERNEL);
186-
187-
if (IS_ENABLED(CONFIG_PROVE_LOCKING)) {
188-
if (WARN(irqs_disabled(), "syscall %lu left IRQs disabled", nr))
189-
local_irq_enable();
190-
}
191-
192-
rseq_syscall(regs);
193-
194-
/*
195-
* Do one-time syscall specific work. If these work items are
196-
* enabled, we want to run them exactly once per syscall exit with
197-
* interrupts enabled.
198-
*/
199-
if (unlikely(work & SYSCALL_WORK_EXIT))
200-
syscall_exit_work(regs, work);
201-
}
202-
203-
static __always_inline void __syscall_exit_to_user_mode_work(struct pt_regs *regs)
204-
{
205-
syscall_exit_to_user_mode_prepare(regs);
206-
local_irq_disable_exit_to_user();
207-
exit_to_user_mode_prepare(regs);
208-
}
209-
210-
void syscall_exit_to_user_mode_work(struct pt_regs *regs)
211-
{
212-
__syscall_exit_to_user_mode_work(regs);
213-
}
214-
215-
__visible noinstr void syscall_exit_to_user_mode(struct pt_regs *regs)
216-
{
217-
instrumentation_begin();
218-
__syscall_exit_to_user_mode_work(regs);
219-
instrumentation_end();
220-
exit_to_user_mode();
221-
}
222-
223176
noinstr void irqentry_enter_from_user_mode(struct pt_regs *regs)
224177
{
225178
enter_from_user_mode(regs);

0 commit comments

Comments
 (0)