linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [RFC PATCH v3 0/3] LoongArch: Modify handle_syscall
@ 2022-06-15 10:29 Tiezhu Yang
  2022-06-15 10:29 ` [RFC PATCH v3 1/3] LoongArch: Add TI_SYSCALL in output_thread_info_defines() Tiezhu Yang
                   ` (2 more replies)
  0 siblings, 3 replies; 6+ messages in thread
From: Tiezhu Yang @ 2022-06-15 10:29 UTC (permalink / raw)
  To: Huacai Chen, WANG Xuerui
  Cc: Xuefeng Li, Jianmin Lv, Jun Yi, Rui Wang, linux-kernel

Tiezhu Yang (3):
  LoongArch: Add TI_SYSCALL in output_thread_info_defines()
  LoongArch: Only clone and clone3 need to call SAVE_STATIC
  LoongArch: No need to call RESTORE_ALL_AND_RET for all syscalls

 arch/loongarch/include/asm/stackframe.h | 17 ++++++++++++++++
 arch/loongarch/kernel/asm-offsets.c     |  1 +
 arch/loongarch/kernel/entry.S           | 35 ++++++++++++++++++++++++++++++++-
 3 files changed, 52 insertions(+), 1 deletion(-)

-- 
2.1.0


^ permalink raw reply	[flat|nested] 6+ messages in thread

* [RFC PATCH v3 1/3] LoongArch: Add TI_SYSCALL in output_thread_info_defines()
  2022-06-15 10:29 [RFC PATCH v3 0/3] LoongArch: Modify handle_syscall Tiezhu Yang
@ 2022-06-15 10:29 ` Tiezhu Yang
  2022-06-15 10:29 ` [RFC PATCH v3 2/3] LoongArch: Only clone and clone3 need to call SAVE_STATIC Tiezhu Yang
  2022-06-15 10:29 ` [RFC PATCH v3 3/3] LoongArch: No need to call RESTORE_ALL_AND_RET for all syscalls Tiezhu Yang
  2 siblings, 0 replies; 6+ messages in thread
From: Tiezhu Yang @ 2022-06-15 10:29 UTC (permalink / raw)
  To: Huacai Chen, WANG Xuerui
  Cc: Xuefeng Li, Jianmin Lv, Jun Yi, Rui Wang, linux-kernel

The initial idea was to store the syscall number in PT_R11,
and then we can get the syscall number from PT_R11 to check
before SAVE and RESTORE in handle_syscall, but PT_R11 may
be overwritten by the signal handler and the syscall number
is lost.

Add TI_SYSCALL in output_thread_info_defines(), then we can
store the syscall number in TI_SYSCALL. This is preparation
for later patch.

Signed-off-by: Tiezhu Yang <yangtiezhu@loongson.cn>
---
 arch/loongarch/kernel/asm-offsets.c | 1 +
 1 file changed, 1 insertion(+)

diff --git a/arch/loongarch/kernel/asm-offsets.c b/arch/loongarch/kernel/asm-offsets.c
index bfb65eb..4757ebe 100644
--- a/arch/loongarch/kernel/asm-offsets.c
+++ b/arch/loongarch/kernel/asm-offsets.c
@@ -81,6 +81,7 @@ void output_thread_info_defines(void)
 	OFFSET(TI_CPU, thread_info, cpu);
 	OFFSET(TI_PRE_COUNT, thread_info, preempt_count);
 	OFFSET(TI_REGS, thread_info, regs);
+	OFFSET(TI_SYSCALL, thread_info, syscall);
 	DEFINE(_THREAD_SIZE, THREAD_SIZE);
 	DEFINE(_THREAD_MASK, THREAD_MASK);
 	DEFINE(_IRQ_STACK_SIZE, IRQ_STACK_SIZE);
-- 
2.1.0


^ permalink raw reply related	[flat|nested] 6+ messages in thread

* [RFC PATCH v3 2/3] LoongArch: Only clone and clone3 need to call SAVE_STATIC
  2022-06-15 10:29 [RFC PATCH v3 0/3] LoongArch: Modify handle_syscall Tiezhu Yang
  2022-06-15 10:29 ` [RFC PATCH v3 1/3] LoongArch: Add TI_SYSCALL in output_thread_info_defines() Tiezhu Yang
@ 2022-06-15 10:29 ` Tiezhu Yang
  2022-06-15 10:29 ` [RFC PATCH v3 3/3] LoongArch: No need to call RESTORE_ALL_AND_RET for all syscalls Tiezhu Yang
  2 siblings, 0 replies; 6+ messages in thread
From: Tiezhu Yang @ 2022-06-15 10:29 UTC (permalink / raw)
  To: Huacai Chen, WANG Xuerui
  Cc: Xuefeng Li, Jianmin Lv, Jun Yi, Rui Wang, linux-kernel

In handle_syscall, it is unnecessary to call SAVE_STATIC for all syscalls,
only clone and clone3 need to do this operation, so it is better to check
the syscall number before call SAVE_STATIC.

With this patch, it can reduce many store instructions.

Signed-off-by: Tiezhu Yang <yangtiezhu@loongson.cn>
---
 arch/loongarch/kernel/entry.S | 10 ++++++++++
 1 file changed, 10 insertions(+)

diff --git a/arch/loongarch/kernel/entry.S b/arch/loongarch/kernel/entry.S
index d5b3dbc..a0f7fcc 100644
--- a/arch/loongarch/kernel/entry.S
+++ b/arch/loongarch/kernel/entry.S
@@ -14,6 +14,7 @@
 #include <asm/regdef.h>
 #include <asm/stackframe.h>
 #include <asm/thread_info.h>
+#include <asm/unistd.h>
 
 	.text
 	.cfi_sections	.debug_frame
@@ -56,8 +57,17 @@ SYM_FUNC_START(handle_syscall)
 	cfi_st	u0, PT_R21
 	cfi_st	fp, PT_R22
 
+	/* Syscall number held in a7, only clone and clone3 need to call SAVE_STATIC. */
+	li.w	t3, __NR_clone
+	beq	a7, t3, 1f
+	li.w	t3, __NR_clone3
+	beq	a7, t3, 1f
+	b	2f
+
+1:
 	SAVE_STATIC
 
+2:
 	move	u0, t0
 	li.d	tp, ~_THREAD_MASK
 	and	tp, tp, sp
-- 
2.1.0


^ permalink raw reply related	[flat|nested] 6+ messages in thread

* [RFC PATCH v3 3/3] LoongArch: No need to call RESTORE_ALL_AND_RET for all syscalls
  2022-06-15 10:29 [RFC PATCH v3 0/3] LoongArch: Modify handle_syscall Tiezhu Yang
  2022-06-15 10:29 ` [RFC PATCH v3 1/3] LoongArch: Add TI_SYSCALL in output_thread_info_defines() Tiezhu Yang
  2022-06-15 10:29 ` [RFC PATCH v3 2/3] LoongArch: Only clone and clone3 need to call SAVE_STATIC Tiezhu Yang
@ 2022-06-15 10:29 ` Tiezhu Yang
  2022-06-16 12:52   ` Huacai Chen
  2 siblings, 1 reply; 6+ messages in thread
From: Tiezhu Yang @ 2022-06-15 10:29 UTC (permalink / raw)
  To: Huacai Chen, WANG Xuerui
  Cc: Xuefeng Li, Jianmin Lv, Jun Yi, Rui Wang, linux-kernel

In handle_syscall, it is unnecessary to call RESTORE_ALL_AND_RET
for all syscalls.

(1) clone and clone3 call RESTORE_STATIC_SOME_SP_AND_RET.
(2) rt_sigreturn and rt_sigsuspend call RESTORE_TEMP_SOME_SP_AND_RET.
(3) The other syscalls call RESTORE_SOME_SP_AND_RET.

With this patch, it can reduce many load instructions.

Signed-off-by: Tiezhu Yang <yangtiezhu@loongson.cn>
---
 arch/loongarch/include/asm/stackframe.h | 17 +++++++++++++++++
 arch/loongarch/kernel/entry.S           | 25 ++++++++++++++++++++++++-
 2 files changed, 41 insertions(+), 1 deletion(-)

diff --git a/arch/loongarch/include/asm/stackframe.h b/arch/loongarch/include/asm/stackframe.h
index 4ca9530..52649a5f 100644
--- a/arch/loongarch/include/asm/stackframe.h
+++ b/arch/loongarch/include/asm/stackframe.h
@@ -216,4 +216,21 @@
 	RESTORE_SP_AND_RET \docfi
 	.endm
 
+	.macro	RESTORE_SOME_SP_AND_RET docfi=0
+	RESTORE_SOME \docfi
+	RESTORE_SP_AND_RET \docfi
+	.endm
+
+	.macro	RESTORE_STATIC_SOME_SP_AND_RET docfi=0
+	RESTORE_STATIC \docfi
+	RESTORE_SOME \docfi
+	RESTORE_SP_AND_RET \docfi
+	.endm
+
+	.macro	RESTORE_TEMP_SOME_SP_AND_RET docfi=0
+	RESTORE_TEMP \docfi
+	RESTORE_SOME \docfi
+	RESTORE_SP_AND_RET \docfi
+	.endm
+
 #endif /* _ASM_STACKFRAME_H */
diff --git a/arch/loongarch/kernel/entry.S b/arch/loongarch/kernel/entry.S
index a0f7fcc..e27e38b 100644
--- a/arch/loongarch/kernel/entry.S
+++ b/arch/loongarch/kernel/entry.S
@@ -72,10 +72,33 @@ SYM_FUNC_START(handle_syscall)
 	li.d	tp, ~_THREAD_MASK
 	and	tp, tp, sp
 
+	/* Syscall number held in a7, we can store it in TI_SYSCALL. */
+        LONG_S  a7, tp, TI_SYSCALL
+
 	move	a0, sp
 	bl	do_syscall
 
-	RESTORE_ALL_AND_RET
+	/*
+	 * Syscall number held in a7 which is stored in TI_SYSCALL.
+	 * clone and clone3 call RESTORE_STATIC_SOME_SP_AND_RET.
+	 * rt_sigreturn and rt_sigsuspend call RESTORE_TEMP_SOME_SP_AND_RET.
+	 * The other syscalls call RESTORE_SOME_SP_AND_RET.
+	 */
+	LONG_L	t3, tp, TI_SYSCALL
+	li.w	t4, __NR_clone
+	beq	t3, t4, 3f
+	li.w	t4, __NR_clone3
+	beq	t3, t4, 3f
+	li.w	t4, __NR_rt_sigreturn
+	beq	t3, t4, 4f
+	li.w	t4, __NR_rt_sigsuspend
+	beq	t3, t4, 4f
+
+	RESTORE_SOME_SP_AND_RET
+3:
+	RESTORE_STATIC_SOME_SP_AND_RET
+4:
+	RESTORE_TEMP_SOME_SP_AND_RET
 SYM_FUNC_END(handle_syscall)
 
 SYM_CODE_START(ret_from_fork)
-- 
2.1.0


^ permalink raw reply related	[flat|nested] 6+ messages in thread

* Re: [RFC PATCH v3 3/3] LoongArch: No need to call RESTORE_ALL_AND_RET for all syscalls
  2022-06-15 10:29 ` [RFC PATCH v3 3/3] LoongArch: No need to call RESTORE_ALL_AND_RET for all syscalls Tiezhu Yang
@ 2022-06-16 12:52   ` Huacai Chen
  2022-06-16 13:36     ` Tiezhu Yang
  0 siblings, 1 reply; 6+ messages in thread
From: Huacai Chen @ 2022-06-16 12:52 UTC (permalink / raw)
  To: Tiezhu Yang; +Cc: WANG Xuerui, Xuefeng Li, Jianmin Lv, Jun Yi, Rui Wang, LKML

Hi, Tiezhu,

On Wed, Jun 15, 2022 at 6:29 PM Tiezhu Yang <yangtiezhu@loongson.cn> wrote:
>
> In handle_syscall, it is unnecessary to call RESTORE_ALL_AND_RET
> for all syscalls.
>
> (1) clone and clone3 call RESTORE_STATIC_SOME_SP_AND_RET.
> (2) rt_sigreturn and rt_sigsuspend call RESTORE_TEMP_SOME_SP_AND_RET.
I doubt that you have not tested, it is obvious that rt_sigreturn need
RESTORE_ALL.

Huacai

> (3) The other syscalls call RESTORE_SOME_SP_AND_RET.
>
> With this patch, it can reduce many load instructions.
>
> Signed-off-by: Tiezhu Yang <yangtiezhu@loongson.cn>
> ---
>  arch/loongarch/include/asm/stackframe.h | 17 +++++++++++++++++
>  arch/loongarch/kernel/entry.S           | 25 ++++++++++++++++++++++++-
>  2 files changed, 41 insertions(+), 1 deletion(-)
>
> diff --git a/arch/loongarch/include/asm/stackframe.h b/arch/loongarch/include/asm/stackframe.h
> index 4ca9530..52649a5f 100644
> --- a/arch/loongarch/include/asm/stackframe.h
> +++ b/arch/loongarch/include/asm/stackframe.h
> @@ -216,4 +216,21 @@
>         RESTORE_SP_AND_RET \docfi
>         .endm
>
> +       .macro  RESTORE_SOME_SP_AND_RET docfi=0
> +       RESTORE_SOME \docfi
> +       RESTORE_SP_AND_RET \docfi
> +       .endm
> +
> +       .macro  RESTORE_STATIC_SOME_SP_AND_RET docfi=0
> +       RESTORE_STATIC \docfi
> +       RESTORE_SOME \docfi
> +       RESTORE_SP_AND_RET \docfi
> +       .endm
> +
> +       .macro  RESTORE_TEMP_SOME_SP_AND_RET docfi=0
> +       RESTORE_TEMP \docfi
> +       RESTORE_SOME \docfi
> +       RESTORE_SP_AND_RET \docfi
> +       .endm
> +
>  #endif /* _ASM_STACKFRAME_H */
> diff --git a/arch/loongarch/kernel/entry.S b/arch/loongarch/kernel/entry.S
> index a0f7fcc..e27e38b 100644
> --- a/arch/loongarch/kernel/entry.S
> +++ b/arch/loongarch/kernel/entry.S
> @@ -72,10 +72,33 @@ SYM_FUNC_START(handle_syscall)
>         li.d    tp, ~_THREAD_MASK
>         and     tp, tp, sp
>
> +       /* Syscall number held in a7, we can store it in TI_SYSCALL. */
> +        LONG_S  a7, tp, TI_SYSCALL
> +
>         move    a0, sp
>         bl      do_syscall
>
> -       RESTORE_ALL_AND_RET
> +       /*
> +        * Syscall number held in a7 which is stored in TI_SYSCALL.
> +        * clone and clone3 call RESTORE_STATIC_SOME_SP_AND_RET.
> +        * rt_sigreturn and rt_sigsuspend call RESTORE_TEMP_SOME_SP_AND_RET.
> +        * The other syscalls call RESTORE_SOME_SP_AND_RET.
> +        */
> +       LONG_L  t3, tp, TI_SYSCALL
> +       li.w    t4, __NR_clone
> +       beq     t3, t4, 3f
> +       li.w    t4, __NR_clone3
> +       beq     t3, t4, 3f
> +       li.w    t4, __NR_rt_sigreturn
> +       beq     t3, t4, 4f
> +       li.w    t4, __NR_rt_sigsuspend
> +       beq     t3, t4, 4f
> +
> +       RESTORE_SOME_SP_AND_RET
> +3:
> +       RESTORE_STATIC_SOME_SP_AND_RET
> +4:
> +       RESTORE_TEMP_SOME_SP_AND_RET
>  SYM_FUNC_END(handle_syscall)
>
>  SYM_CODE_START(ret_from_fork)
> --
> 2.1.0
>

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [RFC PATCH v3 3/3] LoongArch: No need to call RESTORE_ALL_AND_RET for all syscalls
  2022-06-16 12:52   ` Huacai Chen
@ 2022-06-16 13:36     ` Tiezhu Yang
  0 siblings, 0 replies; 6+ messages in thread
From: Tiezhu Yang @ 2022-06-16 13:36 UTC (permalink / raw)
  To: Huacai Chen; +Cc: WANG Xuerui, Xuefeng Li, Jianmin Lv, Jun Yi, Rui Wang, LKML



On 06/16/2022 08:52 PM, Huacai Chen wrote:
> Hi, Tiezhu,
>
> On Wed, Jun 15, 2022 at 6:29 PM Tiezhu Yang <yangtiezhu@loongson.cn> wrote:
>>
>> In handle_syscall, it is unnecessary to call RESTORE_ALL_AND_RET
>> for all syscalls.
>>
>> (1) clone and clone3 call RESTORE_STATIC_SOME_SP_AND_RET.
>> (2) rt_sigreturn and rt_sigsuspend call RESTORE_TEMP_SOME_SP_AND_RET.
> I doubt that you have not tested, it is obvious that rt_sigreturn need
> RESTORE_ALL.

Yes, your guess is right. :)

I have no workable machine to update the upstream kernel now, only 
compile test.

I will try my best to find a workable machine and then test it,
there is a good test case written by Rui Wang [1].

This patch is RFC, any comments will be much appreciated, thank you.

[1] https://github.com/hevz/sigaction-test

Thanks,
Tiezhu


^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2022-06-16 13:36 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-06-15 10:29 [RFC PATCH v3 0/3] LoongArch: Modify handle_syscall Tiezhu Yang
2022-06-15 10:29 ` [RFC PATCH v3 1/3] LoongArch: Add TI_SYSCALL in output_thread_info_defines() Tiezhu Yang
2022-06-15 10:29 ` [RFC PATCH v3 2/3] LoongArch: Only clone and clone3 need to call SAVE_STATIC Tiezhu Yang
2022-06-15 10:29 ` [RFC PATCH v3 3/3] LoongArch: No need to call RESTORE_ALL_AND_RET for all syscalls Tiezhu Yang
2022-06-16 12:52   ` Huacai Chen
2022-06-16 13:36     ` Tiezhu Yang

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).