diff mbox series

[v5,4/4] entry: Inline syscall_exit_to_user_mode()

Message ID 20250305-riscv_optimize_entry-v5-4-6507b5dff3ce@rivosinc.com (mailing list archive)
State New
Headers show
Series entry: Move ret_from_fork() to C and inline syscall_exit_to_user_mode() | expand

Checks

Context Check Description
bjorn/pre-ci_am success Success
bjorn/build-rv32-defconfig success build-rv32-defconfig
bjorn/build-rv64-clang-allmodconfig success build-rv64-clang-allmodconfig
bjorn/build-rv64-gcc-allmodconfig success build-rv64-gcc-allmodconfig
bjorn/build-rv64-nommu-k210-defconfig success build-rv64-nommu-k210-defconfig
bjorn/build-rv64-nommu-k210-virt success build-rv64-nommu-k210-virt
bjorn/checkpatch success checkpatch
bjorn/dtb-warn-rv64 success dtb-warn-rv64
bjorn/header-inline success header-inline
bjorn/kdoc success kdoc
bjorn/module-param success module-param
bjorn/verify-fixes success verify-fixes
bjorn/verify-signedoff success verify-signedoff

Commit Message

Charlie Jenkins March 6, 2025, 12:43 a.m. UTC
Architectures using the generic entry code can be optimized by having
syscall_exit_to_user_mode inlined.

Signed-off-by: Charlie Jenkins <charlie@rivosinc.com>
---
 include/linux/entry-common.h | 43 ++++++++++++++++++++++++++++++++++++--
 kernel/entry/common.c        | 49 +-------------------------------------------
 2 files changed, 42 insertions(+), 50 deletions(-)

Comments

Thomas Gleixner March 8, 2025, 12:37 p.m. UTC | #1
On Wed, Mar 05 2025 at 16:43, Charlie Jenkins wrote:
> Architectures using the generic entry code can be optimized by having
> syscall_exit_to_user_mode inlined.

That's a pretty handwavy claim. What's the actual benefit in numbers?

Thanks,

        tglx
Charlie Jenkins March 9, 2025, 12:08 a.m. UTC | #2
On Sat, Mar 08, 2025 at 01:37:30PM +0100, Thomas Gleixner wrote:
> On Wed, Mar 05 2025 at 16:43, Charlie Jenkins wrote:
> > Architectures using the generic entry code can be optimized by having
> > syscall_exit_to_user_mode inlined.
> 
> That's a pretty handwavy claim. What's the actual benefit in numbers?

I put the numbers in the cover letter! Here is the data I gathered:

Testing was done with the byte-unixbench [1] 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" [2]

- Charlie

> 
> Thanks,
> 
>         tglx
Thomas Gleixner March 9, 2025, 8:23 a.m. UTC | #3
On Sat, Mar 08 2025 at 16:08, Charlie Jenkins wrote:
> On Sat, Mar 08, 2025 at 01:37:30PM +0100, Thomas Gleixner wrote:
>> On Wed, Mar 05 2025 at 16:43, Charlie Jenkins wrote:
>> > Architectures using the generic entry code can be optimized by having
>> > syscall_exit_to_user_mode inlined.
>> 
>> That's a pretty handwavy claim. What's the actual benefit in numbers?
>
> I put the numbers in the cover letter! Here is the data I gathered:

They should be in the change log as the cover letter is not part of what
ends up in git.
Charlie Jenkins March 10, 2025, 4:34 a.m. UTC | #4
On Sun, Mar 09, 2025 at 09:23:20AM +0100, Thomas Gleixner wrote:
> On Sat, Mar 08 2025 at 16:08, Charlie Jenkins wrote:
> > On Sat, Mar 08, 2025 at 01:37:30PM +0100, Thomas Gleixner wrote:
> >> On Wed, Mar 05 2025 at 16:43, Charlie Jenkins wrote:
> >> > Architectures using the generic entry code can be optimized by having
> >> > syscall_exit_to_user_mode inlined.
> >> 
> >> That's a pretty handwavy claim. What's the actual benefit in numbers?
> >
> > I put the numbers in the cover letter! Here is the data I gathered:
> 
> They should be in the change log as the cover letter is not part of what
> ends up in git.

Yes good point. I will add this data here in the next version of the patch.

- Charlie
Alexandre Ghiti March 10, 2025, 9:58 a.m. UTC | #5
Hi Charlie,

On 06/03/2025 01:43, Charlie Jenkins wrote:
> Architectures using the generic entry code can be optimized by having
> syscall_exit_to_user_mode inlined.
>
> Signed-off-by: Charlie Jenkins <charlie@rivosinc.com>
> ---
>   include/linux/entry-common.h | 43 ++++++++++++++++++++++++++++++++++++--
>   kernel/entry/common.c        | 49 +-------------------------------------------
>   2 files changed, 42 insertions(+), 50 deletions(-)
>
> diff --git a/include/linux/entry-common.h b/include/linux/entry-common.h
> index fc61d0205c97084acc89c8e45e088946f5e6d9b2..f94f3fdf15fc0091223cc9f7b823970302e67312 100644
> --- a/include/linux/entry-common.h
> +++ b/include/linux/entry-common.h
> @@ -14,6 +14,7 @@
>   #include <linux/kmsan.h>
>   
>   #include <asm/entry-common.h>
> +#include <asm/syscall.h>
>   
>   /*
>    * 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)
>   	lockdep_hardirqs_on(CALLER_ADDR0);
>   }
>   
> +/**
> + * syscall_exit_work - Handle work before returning to user mode
> + * @regs:	Pointer to current pt_regs
> + * @work:	Current thread syscall work
> + *
> + * Do one-time syscall specific work.
> + */
> +void syscall_exit_work(struct pt_regs *regs, unsigned long work);
> +
>   /**
>    * syscall_exit_to_user_mode_work - Handle work before returning to user mode
>    * @regs:	Pointer to currents pt_regs
> @@ -379,7 +389,30 @@ static __always_inline void exit_to_user_mode(void)
>    * make the final state transitions. Interrupts must stay disabled between
>    * return from this function and the invocation of exit_to_user_mode().
>    */
> -void syscall_exit_to_user_mode_work(struct pt_regs *regs);
> +static __always_inline void syscall_exit_to_user_mode_work(struct pt_regs *regs)
> +{
> +	unsigned long work = READ_ONCE(current_thread_info()->syscall_work);
> +	unsigned long nr = syscall_get_nr(current, regs);
> +
> +	CT_WARN_ON(ct_state() != CT_STATE_KERNEL);
> +
> +	if (IS_ENABLED(CONFIG_PROVE_LOCKING)) {
> +		if (WARN(irqs_disabled(), "syscall %lu left IRQs disabled", nr))
> +			local_irq_enable();
> +	}
> +
> +	rseq_syscall(regs);
> +
> +	/*
> +	 * Do one-time syscall specific work. If these work items are
> +	 * enabled, we want to run them exactly once per syscall exit with
> +	 * interrupts enabled.
> +	 */
> +	if (unlikely(work & SYSCALL_WORK_EXIT))
> +		syscall_exit_work(regs, work);
> +	local_irq_disable_exit_to_user();
> +	exit_to_user_mode_prepare(regs);
> +}
>   
>   /**
>    * 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);
>    * exit_to_user_mode(). This function is preferred unless there is a
>    * compelling architectural reason to use the separate functions.
>    */
> -void syscall_exit_to_user_mode(struct pt_regs *regs);
> +static __always_inline void syscall_exit_to_user_mode(struct pt_regs *regs)
> +{
> +	instrumentation_begin();
> +	syscall_exit_to_user_mode_work(regs);
> +	instrumentation_end();
> +	exit_to_user_mode();
> +}
>   
>   /**
>    * irqentry_enter_from_user_mode - Establish state before invoking the irq handler
> diff --git a/kernel/entry/common.c b/kernel/entry/common.c
> index e33691d5adf7aab4af54cf2bf8e5ef5bd6ad1424..f55e421fb196dd5f9d4e34dd85ae096c774cf879 100644
> --- a/kernel/entry/common.c
> +++ b/kernel/entry/common.c
> @@ -146,7 +146,7 @@ static inline bool report_single_step(unsigned long work)
>   	return work & SYSCALL_WORK_SYSCALL_EXIT_TRAP;
>   }
>   
> -static void syscall_exit_work(struct pt_regs *regs, unsigned long work)
> +void syscall_exit_work(struct pt_regs *regs, unsigned long work)
>   {
>   	bool step;
>   
> @@ -173,53 +173,6 @@ static void syscall_exit_work(struct pt_regs *regs, unsigned long work)
>   		ptrace_report_syscall_exit(regs, step);
>   }
>   
> -/*
> - * Syscall specific exit to user mode preparation. Runs with interrupts
> - * enabled.
> - */
> -static void syscall_exit_to_user_mode_prepare(struct pt_regs *regs)
> -{
> -	unsigned long work = READ_ONCE(current_thread_info()->syscall_work);
> -	unsigned long nr = syscall_get_nr(current, regs);
> -
> -	CT_WARN_ON(ct_state() != CT_STATE_KERNEL);
> -
> -	if (IS_ENABLED(CONFIG_PROVE_LOCKING)) {
> -		if (WARN(irqs_disabled(), "syscall %lu left IRQs disabled", nr))
> -			local_irq_enable();
> -	}
> -
> -	rseq_syscall(regs);
> -
> -	/*
> -	 * Do one-time syscall specific work. If these work items are
> -	 * enabled, we want to run them exactly once per syscall exit with
> -	 * interrupts enabled.
> -	 */
> -	if (unlikely(work & SYSCALL_WORK_EXIT))
> -		syscall_exit_work(regs, work);
> -}
> -
> -static __always_inline void __syscall_exit_to_user_mode_work(struct pt_regs *regs)
> -{
> -	syscall_exit_to_user_mode_prepare(regs);
> -	local_irq_disable_exit_to_user();
> -	exit_to_user_mode_prepare(regs);
> -}
> -
> -void syscall_exit_to_user_mode_work(struct pt_regs *regs)
> -{
> -	__syscall_exit_to_user_mode_work(regs);
> -}
> -
> -__visible noinstr void syscall_exit_to_user_mode(struct pt_regs *regs)
> -{
> -	instrumentation_begin();
> -	__syscall_exit_to_user_mode_work(regs);
> -	instrumentation_end();
> -	exit_to_user_mode();
> -}
> -
>   noinstr void irqentry_enter_from_user_mode(struct pt_regs *regs)
>   {
>   	enter_from_user_mode(regs);
>

Reviewed-by: Alexandre Ghiti <alexghiti@rivosinc.com>

Thanks,

Alex
diff mbox series

Patch

diff --git a/include/linux/entry-common.h b/include/linux/entry-common.h
index fc61d0205c97084acc89c8e45e088946f5e6d9b2..f94f3fdf15fc0091223cc9f7b823970302e67312 100644
--- a/include/linux/entry-common.h
+++ b/include/linux/entry-common.h
@@ -14,6 +14,7 @@ 
 #include <linux/kmsan.h>
 
 #include <asm/entry-common.h>
+#include <asm/syscall.h>
 
 /*
  * 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)
 	lockdep_hardirqs_on(CALLER_ADDR0);
 }
 
+/**
+ * syscall_exit_work - Handle work before returning to user mode
+ * @regs:	Pointer to current pt_regs
+ * @work:	Current thread syscall work
+ *
+ * Do one-time syscall specific work.
+ */
+void syscall_exit_work(struct pt_regs *regs, unsigned long work);
+
 /**
  * syscall_exit_to_user_mode_work - Handle work before returning to user mode
  * @regs:	Pointer to currents pt_regs
@@ -379,7 +389,30 @@  static __always_inline void exit_to_user_mode(void)
  * make the final state transitions. Interrupts must stay disabled between
  * return from this function and the invocation of exit_to_user_mode().
  */
-void syscall_exit_to_user_mode_work(struct pt_regs *regs);
+static __always_inline void syscall_exit_to_user_mode_work(struct pt_regs *regs)
+{
+	unsigned long work = READ_ONCE(current_thread_info()->syscall_work);
+	unsigned long nr = syscall_get_nr(current, regs);
+
+	CT_WARN_ON(ct_state() != CT_STATE_KERNEL);
+
+	if (IS_ENABLED(CONFIG_PROVE_LOCKING)) {
+		if (WARN(irqs_disabled(), "syscall %lu left IRQs disabled", nr))
+			local_irq_enable();
+	}
+
+	rseq_syscall(regs);
+
+	/*
+	 * Do one-time syscall specific work. If these work items are
+	 * enabled, we want to run them exactly once per syscall exit with
+	 * interrupts enabled.
+	 */
+	if (unlikely(work & SYSCALL_WORK_EXIT))
+		syscall_exit_work(regs, work);
+	local_irq_disable_exit_to_user();
+	exit_to_user_mode_prepare(regs);
+}
 
 /**
  * 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);
  * exit_to_user_mode(). This function is preferred unless there is a
  * compelling architectural reason to use the separate functions.
  */
-void syscall_exit_to_user_mode(struct pt_regs *regs);
+static __always_inline void syscall_exit_to_user_mode(struct pt_regs *regs)
+{
+	instrumentation_begin();
+	syscall_exit_to_user_mode_work(regs);
+	instrumentation_end();
+	exit_to_user_mode();
+}
 
 /**
  * irqentry_enter_from_user_mode - Establish state before invoking the irq handler
diff --git a/kernel/entry/common.c b/kernel/entry/common.c
index e33691d5adf7aab4af54cf2bf8e5ef5bd6ad1424..f55e421fb196dd5f9d4e34dd85ae096c774cf879 100644
--- a/kernel/entry/common.c
+++ b/kernel/entry/common.c
@@ -146,7 +146,7 @@  static inline bool report_single_step(unsigned long work)
 	return work & SYSCALL_WORK_SYSCALL_EXIT_TRAP;
 }
 
-static void syscall_exit_work(struct pt_regs *regs, unsigned long work)
+void syscall_exit_work(struct pt_regs *regs, unsigned long work)
 {
 	bool step;
 
@@ -173,53 +173,6 @@  static void syscall_exit_work(struct pt_regs *regs, unsigned long work)
 		ptrace_report_syscall_exit(regs, step);
 }
 
-/*
- * Syscall specific exit to user mode preparation. Runs with interrupts
- * enabled.
- */
-static void syscall_exit_to_user_mode_prepare(struct pt_regs *regs)
-{
-	unsigned long work = READ_ONCE(current_thread_info()->syscall_work);
-	unsigned long nr = syscall_get_nr(current, regs);
-
-	CT_WARN_ON(ct_state() != CT_STATE_KERNEL);
-
-	if (IS_ENABLED(CONFIG_PROVE_LOCKING)) {
-		if (WARN(irqs_disabled(), "syscall %lu left IRQs disabled", nr))
-			local_irq_enable();
-	}
-
-	rseq_syscall(regs);
-
-	/*
-	 * Do one-time syscall specific work. If these work items are
-	 * enabled, we want to run them exactly once per syscall exit with
-	 * interrupts enabled.
-	 */
-	if (unlikely(work & SYSCALL_WORK_EXIT))
-		syscall_exit_work(regs, work);
-}
-
-static __always_inline void __syscall_exit_to_user_mode_work(struct pt_regs *regs)
-{
-	syscall_exit_to_user_mode_prepare(regs);
-	local_irq_disable_exit_to_user();
-	exit_to_user_mode_prepare(regs);
-}
-
-void syscall_exit_to_user_mode_work(struct pt_regs *regs)
-{
-	__syscall_exit_to_user_mode_work(regs);
-}
-
-__visible noinstr void syscall_exit_to_user_mode(struct pt_regs *regs)
-{
-	instrumentation_begin();
-	__syscall_exit_to_user_mode_work(regs);
-	instrumentation_end();
-	exit_to_user_mode();
-}
-
 noinstr void irqentry_enter_from_user_mode(struct pt_regs *regs)
 {
 	enter_from_user_mode(regs);