Skip to content

Commit e43b8bb

Browse files
charlie-rivosKAGA-KOKO
authored andcommitted
entry: Inline syscall_exit_to_user_mode()
Similar to commit 221a164 ("entry: Move syscall_enter_from_user_mode() to header file"), move syscall_exit_to_user_mode() to the header file as well. Testing was done with the byte-unixbench syscall benchmark (which calls getpid) and QEMU. On riscv I measured a 7.09246% improvement, on x86 a 2.98843% improvement, on loongarch a 6.07954% improvement, and on s390 a 11.1328% improvement. The Intel bot also reported "kernel test robot noticed a 1.9% improvement of stress-ng.seek.ops_per_sec". Signed-off-by: Charlie Jenkins <charlie@rivosinc.com> Signed-off-by: Thomas Gleixner <tglx@linutronix.de> Reviewed-by: Alexandre Ghiti <alexghiti@rivosinc.com> Link: https://lore.kernel.org/all/20250320-riscv_optimize_entry-v6-4-63e187e26041@rivosinc.com Link: https://lore.kernel.org/linux-riscv/202502051555.85ae6844-lkp@intel.com/
1 parent 7ace160 commit e43b8bb

File tree

2 files changed

+42
-50
lines changed

2 files changed

+42
-50
lines changed

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)