2022-06-15 06:45:47

by Tiezhu Yang

[permalink] [raw]
Subject: [RFC PATCH v2 0/2] LoongArch: Modify handle_syscall

Tiezhu Yang (2):
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/entry.S | 38 ++++++++++++++++++++++++++++++++-
2 files changed, 54 insertions(+), 1 deletion(-)

--
2.1.0


2022-06-15 06:45:52

by Tiezhu Yang

[permalink] [raw]
Subject: [RFC PATCH v2 1/2] LoongArch: Only clone and clone3 need to call SAVE_STATIC

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 <[email protected]>
---
arch/loongarch/kernel/entry.S | 14 ++++++++++++++
1 file changed, 14 insertions(+)

diff --git a/arch/loongarch/kernel/entry.S b/arch/loongarch/kernel/entry.S
index d5b3dbc..53ce2cb 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,21 @@ SYM_FUNC_START(handle_syscall)
cfi_st u0, PT_R21
cfi_st fp, PT_R22

+ /*
+ * Syscall number held in a7 which is stored in PT_R11.
+ * Only if syscall number is __NR_clone and __NR_clone3, call SAVE_STATIC.
+ */
+ cfi_ld t3, PT_R11
+ li.w t4, __NR_clone
+ beq t3, t4, 1f
+ li.w t4, __NR_clone3
+ beq t3, t4, 1f
+ b 2f
+
+1:
SAVE_STATIC

+2:
move u0, t0
li.d tp, ~_THREAD_MASK
and tp, tp, sp
--
2.1.0

2022-06-15 06:46:25

by Tiezhu Yang

[permalink] [raw]
Subject: [RFC PATCH v2 2/2] LoongArch: No need to call RESTORE_ALL_AND_RET for all syscalls

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

(1) If syscall number is __NR_clone and __NR_clone3,
call RESTORE_STATIC_SOME_SP_AND_RET.
(2) If syscall number is __NR_rt_sigreturn and __NR_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 <[email protected]>
---
arch/loongarch/include/asm/stackframe.h | 17 +++++++++++++++++
arch/loongarch/kernel/entry.S | 24 +++++++++++++++++++++++-
2 files changed, 40 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 53ce2cb..58fe507 100644
--- a/arch/loongarch/kernel/entry.S
+++ b/arch/loongarch/kernel/entry.S
@@ -79,7 +79,29 @@ SYM_FUNC_START(handle_syscall)
move a0, sp
bl do_syscall

- RESTORE_ALL_AND_RET
+ /*
+ * Syscall number held in a7 which is stored in PT_R11.
+ * If syscall number is __NR_clone and __NR_clone3,
+ * call RESTORE_STATIC_SOME_SP_AND_RET.
+ * If syscall number is __NR_rt_sigreturn and __NR_rt_sigsuspend,
+ * call RESTORE_TEMP_SOME_SP_AND_RET.
+ * The other syscalls call RESTORE_SOME_SP_AND_RET.
+ */
+ cfi_ld t3, PT_R11
+ 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

2022-06-15 07:36:59

by hev

[permalink] [raw]
Subject: Re: [RFC PATCH v2 2/2] LoongArch: No need to call RESTORE_ALL_AND_RET for all syscalls

Hello,

On Wed, Jun 15, 2022 at 2:38 PM Tiezhu Yang <[email protected]> wrote:
>
> In handle_syscall, it is unnecessary to call RESTORE_ALL_AND_RET
> for all syscalls.
>
> (1) If syscall number is __NR_clone and __NR_clone3,
> call RESTORE_STATIC_SOME_SP_AND_RET.
> (2) If syscall number is __NR_rt_sigreturn and __NR_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 <[email protected]>
> ---
> arch/loongarch/include/asm/stackframe.h | 17 +++++++++++++++++
> arch/loongarch/kernel/entry.S | 24 +++++++++++++++++++++++-
> 2 files changed, 40 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 53ce2cb..58fe507 100644
> --- a/arch/loongarch/kernel/entry.S
> +++ b/arch/loongarch/kernel/entry.S
> @@ -79,7 +79,29 @@ SYM_FUNC_START(handle_syscall)
> move a0, sp
> bl do_syscall
>
> - RESTORE_ALL_AND_RET
> + /*
> + * Syscall number held in a7 which is stored in PT_R11.
> + * If syscall number is __NR_clone and __NR_clone3,
> + * call RESTORE_STATIC_SOME_SP_AND_RET.
> + * If syscall number is __NR_rt_sigreturn and __NR_rt_sigsuspend,
> + * call RESTORE_TEMP_SOME_SP_AND_RET.
> + * The other syscalls call RESTORE_SOME_SP_AND_RET.
> + */
> + cfi_ld t3, PT_R11

I think PT_R11 may be overwritten by the signal handler and the
syscall number is now lost.

> + 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
>

Best regards,
hev

2022-06-15 08:30:16

by Tiezhu Yang

[permalink] [raw]
Subject: Re: [RFC PATCH v2 2/2] LoongArch: No need to call RESTORE_ALL_AND_RET for all syscalls



On 06/15/2022 02:57 PM, hev wrote:
> Hello,
>
> On Wed, Jun 15, 2022 at 2:38 PM Tiezhu Yang <[email protected]> wrote:
>>
>> In handle_syscall, it is unnecessary to call RESTORE_ALL_AND_RET
>> for all syscalls.
>>
>> (1) If syscall number is __NR_clone and __NR_clone3,
>> call RESTORE_STATIC_SOME_SP_AND_RET.
>> (2) If syscall number is __NR_rt_sigreturn and __NR_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 <[email protected]>
>> ---
>> arch/loongarch/include/asm/stackframe.h | 17 +++++++++++++++++
>> arch/loongarch/kernel/entry.S | 24 +++++++++++++++++++++++-
>> 2 files changed, 40 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 53ce2cb..58fe507 100644
>> --- a/arch/loongarch/kernel/entry.S
>> +++ b/arch/loongarch/kernel/entry.S
>> @@ -79,7 +79,29 @@ SYM_FUNC_START(handle_syscall)
>> move a0, sp
>> bl do_syscall
>>
>> - RESTORE_ALL_AND_RET
>> + /*
>> + * Syscall number held in a7 which is stored in PT_R11.
>> + * If syscall number is __NR_clone and __NR_clone3,
>> + * call RESTORE_STATIC_SOME_SP_AND_RET.
>> + * If syscall number is __NR_rt_sigreturn and __NR_rt_sigsuspend,
>> + * call RESTORE_TEMP_SOME_SP_AND_RET.
>> + * The other syscalls call RESTORE_SOME_SP_AND_RET.
>> + */
>> + cfi_ld t3, PT_R11
>
> I think PT_R11 may be overwritten by the signal handler and the
> syscall number is now lost.
>

Thank you. Let me use TI_SYSCALL. I will send RFC v3 patch later.

arch/loongarch/include/asm/thread_info.h
struct thread_info {
struct task_struct *task; /* main task structure */
unsigned long flags; /* low level flags */
unsigned long tp_value; /* thread pointer */
__u32 cpu; /* current CPU */
int preempt_count; /* 0 => preemptible, <0 => BUG */
struct pt_regs *regs;
unsigned long syscall; /* syscall number */
unsigned long syscall_work; /* SYSCALL_WORK_ flags */
};

>> + 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
>>
>
> Best regards,
> hev
>