diff mbox series

[v1,7/7] DCE/DSE: riscv: trim syscall tables

Message ID aad452c57bce2ab7983e723d78bd2cc7b6f533c1.1695679700.git.falcon@tinylab.org (mailing list archive)
State Changes Requested
Headers show
Series DCE/DSE: Add Dead Syscalls Elimination support, part1 | expand

Checks

Context Check Description
conchuod/cover_letter success Series has a cover letter
conchuod/tree_selection success Guessed tree name to be for-next at HEAD 0bb80ecc33a8
conchuod/fixes_present success Fixes tag not required for -next series
conchuod/maintainers_pattern success MAINTAINERS pattern errors before the patch: 5 and now 5
conchuod/verify_signedoff success Signed-off-by tag matches author and committer
conchuod/kdoc success Errors and warnings before: 0 this patch: 0
conchuod/build_rv64_clang_allmodconfig success Errors and warnings before: 2132 this patch: 2132
conchuod/module_param success Was 0 now: 0
conchuod/build_rv64_gcc_allmodconfig success Errors and warnings before: 1388 this patch: 1388
conchuod/build_rv32_defconfig success Build OK
conchuod/dtb_warn_rv64 success Errors and warnings before: 29 this patch: 29
conchuod/header_inline success No static functions without inline keyword in header files
conchuod/checkpatch success total: 0 errors, 0 warnings, 0 checks, 87 lines checked
conchuod/build_rv64_nommu_k210_defconfig success Build OK
conchuod/verify_fixes success No Fixes tag
conchuod/build_rv64_nommu_virt_defconfig success Build OK

Commit Message

Zhangjin Wu Sept. 25, 2023, 10:43 p.m. UTC
When the maximum nr of the used syscalls is smaller than __NR_syscalls
(original syscalls total). It is able to update __NR_syscalls to
(maximum nr + 1) and further trim the '>= (maximum nr + 1)' part of the
syscall tables:

For example:

    sys_call_table [143] = {
	[0 ... 143 - 1] = sys_ni_syscall,
        [64] = sys_write,
        [93] = sys_exit,
        [142] = sys_reboot,
    }

The >= 143 part of the syscall tables can be trimmed.

At the same time, the syscall >= 143 from user space must be ignored
from do_trap_ecall_u() of traps.c.

Signed-off-by: Zhangjin Wu <falcon@tinylab.org>
---
 arch/riscv/include/asm/unistd.h               |  2 ++
 arch/riscv/kernel/Makefile                    |  2 ++
 arch/riscv/kernel/syscalls/Makefile           | 22 +++++++++++++++++++
 .../kernel/syscalls/compat_syscall_table.c    |  4 ++--
 arch/riscv/kernel/syscalls/syscall_table.c    |  4 ++--
 5 files changed, 30 insertions(+), 4 deletions(-)

Comments

Arnd Bergmann Sept. 26, 2023, 6:01 a.m. UTC | #1
On Tue, Sep 26, 2023, at 00:43, Zhangjin Wu wrote:
> When the maximum nr of the used syscalls is smaller than __NR_syscalls
> (original syscalls total). It is able to update __NR_syscalls to
> (maximum nr + 1) and further trim the '>= (maximum nr + 1)' part of the
> syscall tables:
>
> For example:
>
>     sys_call_table [143] = {
> 	[0 ... 143 - 1] = sys_ni_syscall,
>         [64] = sys_write,
>         [93] = sys_exit,
>         [142] = sys_reboot,
>     }
>
> The >= 143 part of the syscall tables can be trimmed.
>
> At the same time, the syscall >= 143 from user space must be ignored
> from do_trap_ecall_u() of traps.c.
>
> Signed-off-by: Zhangjin Wu <falcon@tinylab.org>
> ---
>  arch/riscv/include/asm/unistd.h               |  2 ++
>  arch/riscv/kernel/Makefile                    |  2 ++
>  arch/riscv/kernel/syscalls/Makefile           | 22 +++++++++++++++++++
>  .../kernel/syscalls/compat_syscall_table.c    |  4 ++--
>  arch/riscv/kernel/syscalls/syscall_table.c    |  4 ++--
>  5 files changed, 30 insertions(+), 4 deletions(-)

This bit feels like you are overoptimizing for a corner case:
there is not much to be gained in terms of memory savings, but
you add complexity in an area that I feel should be made common
between architectures.

I hope to get back to working on consolidating both the
syscall.tbl input files and the build infrastructure for them
across architectures, and you make that harder here, so I'd
prefer you to drop this part, at least until the code is
shared across all architectures.

    Arnd
Zhangjin Wu Oct. 7, 2023, 1:35 p.m. UTC | #2
Hi, Arnd

> On Tue, Sep 26, 2023, at 00:43, Zhangjin Wu wrote:
> > When the maximum nr of the used syscalls is smaller than __NR_syscalls
> > (original syscalls total). It is able to update __NR_syscalls to
> > (maximum nr + 1) and further trim the '>= (maximum nr + 1)' part of the
> > syscall tables:
> >
> > For example:
> >
> >     sys_call_table [143] = {
> > 	[0 ... 143 - 1] = sys_ni_syscall,
> >         [64] = sys_write,
> >         [93] = sys_exit,
> >         [142] = sys_reboot,
> >     }
> >
> > The >= 143 part of the syscall tables can be trimmed.
> >
> > At the same time, the syscall >= 143 from user space must be ignored
> > from do_trap_ecall_u() of traps.c.
> >
> > Signed-off-by: Zhangjin Wu <falcon@tinylab.org>
> > ---
> >  arch/riscv/include/asm/unistd.h               |  2 ++
> >  arch/riscv/kernel/Makefile                    |  2 ++
> >  arch/riscv/kernel/syscalls/Makefile           | 22 +++++++++++++++++++
> >  .../kernel/syscalls/compat_syscall_table.c    |  4 ++--
> >  arch/riscv/kernel/syscalls/syscall_table.c    |  4 ++--
> >  5 files changed, 30 insertions(+), 4 deletions(-)
> 
> This bit feels like you are overoptimizing for a corner case:
> there is not much to be gained in terms of memory savings, but
> you add complexity in an area that I feel should be made common
> between architectures.
> 
> I hope to get back to working on consolidating both the
> syscall.tbl input files and the build infrastructure for them
> across architectures, and you make that harder here, so I'd
> prefer you to drop this part, at least until the code is
> shared across all architectures.
>

Agree, let's drop it.

Thanks,
Zhangjin

>     Arnd
diff mbox series

Patch

diff --git a/arch/riscv/include/asm/unistd.h b/arch/riscv/include/asm/unistd.h
index 221630bdbd07..4d8e41f446ff 100644
--- a/arch/riscv/include/asm/unistd.h
+++ b/arch/riscv/include/asm/unistd.h
@@ -23,4 +23,6 @@ 
 
 #include <uapi/asm/unistd.h>
 
+#ifndef NR_syscalls
 #define NR_syscalls (__NR_syscalls)
+#endif
diff --git a/arch/riscv/kernel/Makefile b/arch/riscv/kernel/Makefile
index 40aebbf06880..e75424c10729 100644
--- a/arch/riscv/kernel/Makefile
+++ b/arch/riscv/kernel/Makefile
@@ -49,7 +49,9 @@  obj-y	+= signal.o
 obj-y	+= syscalls/
 obj-y	+= sys_riscv.o
 obj-y	+= time.o
+ifneq ($(CONFIG_TRIM_UNUSED_SYSCALLS),y)
 obj-y	+= traps.o
+endif
 obj-y	+= riscv_ksyms.o
 obj-y	+= stacktrace.o
 obj-y	+= cacheinfo.o
diff --git a/arch/riscv/kernel/syscalls/Makefile b/arch/riscv/kernel/syscalls/Makefile
index 3b5969aaa9e8..f1a0597c8b24 100644
--- a/arch/riscv/kernel/syscalls/Makefile
+++ b/arch/riscv/kernel/syscalls/Makefile
@@ -14,9 +14,18 @@  else # CONFIG_TRIM_UNUSED_SYSCALLS
 
 include $(srctree)/scripts/Makefile.syscalls
 
+# calculate syscalls total from $(obj)/syscall_table_used.i
+ifneq ($(used_syscalls),)
+  NR_syscalls := $$(($$(sed -E -n -e '/^\[([0-9]+|\([0-9]+ \+ [0-9]+\))\] = /{s/^\[(.*)\].*/\1/gp}' $(obj)/syscall_table_used.i | bc | sort -g | tail -1 | grep '[0-9]' || echo -1) + 1))
+else
+  NR_syscalls := 0
+endif
+
+CFLAGS_traps_used.o                += -DNR_syscalls=$(NR_syscalls)
 CFLAGS_syscall_table_used.o        += $(call cc-option,-Wno-override-init,)
 CFLAGS_compat_syscall_table_used.o += $(call cc-option,-Wno-override-init,)
 
+obj-y                += traps_used.o
 obj-y                += syscall_table_used.o
 obj-$(CONFIG_COMPAT) += compat_syscall_table_used.o
 
@@ -24,15 +33,26 @@  obj-$(CONFIG_COMPAT) += compat_syscall_table_used.o
 quiet_cmd_used = USED    $@
       cmd_used = sed -E -e '/^\[([0-9]+|\([0-9]+ \+ [0-9]+\))\] = /{/= *__riscv_(__sys_|sys_|compat_)*($(used_syscalls)),/!{s%^%/* %g;s%$$% */%g}}' -i $@;
 
+# update the syscalls total
+quiet_cmd_snr = SNR     $@
+      cmd_snr = snr=$(NR_syscalls); if [ $$snr -ne 0 ]; then \
+		sed -i -e "s/sys_call_table\[.*\] =/sys_call_table[($$snr)] =/g;s/\[0 ... (.*) - 1\] = __riscv_sys_ni_syscall/[0 ... ($$snr) - 1] = __riscv_sys_ni_syscall/g" $@; \
+		fi;
+
+$(obj)/traps_used.c: $(src)/../traps.c $(obj)/syscall_table_used.i FORCE
+	$(Q)cp $< $@
+
 $(obj)/syscall_table_used.c: $(src)/syscall_table.c
 	$(Q)cp $< $@
 
 $(obj)/syscall_table_used.i: $(src)/syscall_table_used.c $(used_syscalls_deps) FORCE
 	$(call if_changed_dep,cpp_i_c)
 	$(call cmd,used)
+	$(call cmd,snr)
 
 $(obj)/syscall_table_used.o: $(obj)/syscall_table_used.i FORCE
 	$(call if_changed,cc_o_c)
+	$(call cmd,force_checksrc)
 
 $(obj)/compat_syscall_table_used.c: $(src)/compat_syscall_table.c
 	$(Q)cp $< $@
@@ -40,8 +60,10 @@  $(obj)/compat_syscall_table_used.c: $(src)/compat_syscall_table.c
 $(obj)/compat_syscall_table_used.i: $(src)/compat_syscall_table_used.c $(used_syscalls_deps) FORCE
 	$(call if_changed_dep,cpp_i_c)
 	$(call cmd,used)
+	$(call cmd,snr)
 
 $(obj)/compat_syscall_table_used.o: $(obj)/compat_syscall_table_used.i FORCE
 	$(call if_changed,cc_o_c)
+	$(call cmd,force_checksrc)
 
 endif # CONFIG_TRIM_UNUSED_SYSCALLS
diff --git a/arch/riscv/kernel/syscalls/compat_syscall_table.c b/arch/riscv/kernel/syscalls/compat_syscall_table.c
index ad7f2d712f5f..4756b6858eac 100644
--- a/arch/riscv/kernel/syscalls/compat_syscall_table.c
+++ b/arch/riscv/kernel/syscalls/compat_syscall_table.c
@@ -17,7 +17,7 @@ 
 
 asmlinkage long compat_sys_rt_sigreturn(void);
 
-void * const compat_sys_call_table[__NR_syscalls] = {
-	[0 ... __NR_syscalls - 1] = __riscv_sys_ni_syscall,
+void * const compat_sys_call_table[NR_syscalls] = {
+	[0 ... NR_syscalls - 1] = __riscv_sys_ni_syscall,
 #include <asm/unistd.h>
 };
diff --git a/arch/riscv/kernel/syscalls/syscall_table.c b/arch/riscv/kernel/syscalls/syscall_table.c
index dda913764903..d2b3233ae5d4 100644
--- a/arch/riscv/kernel/syscalls/syscall_table.c
+++ b/arch/riscv/kernel/syscalls/syscall_table.c
@@ -16,7 +16,7 @@ 
 #undef __SYSCALL
 #define __SYSCALL(nr, call)	[nr] = __riscv_##call,
 
-void * const sys_call_table[__NR_syscalls] = {
-	[0 ... __NR_syscalls - 1] = __riscv_sys_ni_syscall,
+void * const sys_call_table[NR_syscalls] = {
+	[0 ... NR_syscalls - 1] = __riscv_sys_ni_syscall,
 #include <asm/unistd.h>
 };