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

From: Tiezhu Yang
Date: Tue Jun 21 2022 - 06:08:06 EST


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

(1) rt_sigreturn call RESTORE_ALL_AND_RET.
(2) The other syscalls call RESTORE_STATIC_SOME_SP_AND_RET.

This patch only adds the minimal changes as simple as possible
to reduce the code complexity, at the same time, it can reduce
many load instructions.

Here are the test environments:

Hardware: Loongson-LS3A5000-7A1000-1w-A2101
Firmware: UDK2018-LoongArch-A2101-pre-beta8 [1]
System: loongarch64-clfs-system-5.0 [2]

The system passed functional testing used with the following
test case without and with this patch:

git clone https://github.com/hevz/sigaction-test.git
cd sigaction-test
make check

Additionally, use UnixBench syscall to test the performance:

git clone https://github.com/kdlucas/byte-unixbench.git
cd byte-unixbench/UnixBench/
make
pgms/syscall 600

In order to avoid the performance impact, add init=/bin/bash
to the boot cmdline.

Here is the test result, the bigger the better, it shows about
1.2% gain tested with close, getpid and exec [3]:

duration without_this_patch with_this_patch
600 s 626558267 lps 634244079 lps

[1] https://github.com/loongson/Firmware/tree/main/5000Series/PC/A2101
[2] https://github.com/sunhaiyong1978/CLFS-for-LoongArch/releases/tag/5.0
[3] https://github.com/kdlucas/byte-unixbench/blob/master/UnixBench/src/syscall.c

Signed-off-by: Tiezhu Yang <yangtiezhu@xxxxxxxxxxx>
---
arch/loongarch/include/asm/stackframe.h | 5 +++++
arch/loongarch/kernel/entry.S | 15 +++++++++++++++
2 files changed, 20 insertions(+)

diff --git a/arch/loongarch/include/asm/stackframe.h b/arch/loongarch/include/asm/stackframe.h
index 4ca9530..551ab8f 100644
--- a/arch/loongarch/include/asm/stackframe.h
+++ b/arch/loongarch/include/asm/stackframe.h
@@ -216,4 +216,9 @@
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
#endif /* _ASM_STACKFRAME_H */
diff --git a/arch/loongarch/kernel/entry.S b/arch/loongarch/kernel/entry.S
index d5b3dbc..c764c99 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
@@ -62,9 +63,23 @@ 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

+ /*
+ * Syscall number held in a7 which is stored in TI_SYSCALL.
+ * rt_sigreturn call RESTORE_ALL_AND_RET.
+ * The other syscalls call RESTORE_STATIC_SOME_SP_AND_RET.
+ */
+ LONG_L t3, tp, TI_SYSCALL
+ li.w t4, __NR_rt_sigreturn
+ beq t3, t4, 1f
+
+ RESTORE_STATIC_SOME_SP_AND_RET
+1:
RESTORE_ALL_AND_RET
SYM_FUNC_END(handle_syscall)

--
2.1.0