[RFC PATCH 4/5] RISC-V: Add dead syscalls elimination support
From: Zhangjin Wu
Date: Thu Feb 16 2023 - 19:50:35 EST
By enabling CONFIG_HAVE_LD_DEAD_CODE_DATA_ELIMINATION and setting
CONFIG_SYSCALLS_USED, It is able to remove the left 'dead' syscalls.
For example, if setting CONFIG_SYSCALLS_USED="write exit reboot", a
'used' variant of the *syscall_table.c will be generated.
Here is a test result on qemu with a minimal rv64 config.
| rv64 | config
----------------|-----------------|-------------------
vmlinux | 4893488 | https://pastebin.com/crz82T0s
+ gc-sections | 4376400 (-10.5%)| CONFIG_HAVE_LD_DEAD_CODE_DATA_ELIMINATION=y
+ syscalls_used | 4172112 (-4.67%)| CONFIG_SYSCALLS_USED="pselect6"
+ syscalls_used | 4172848 (-4.65%)| CONFIG_SYSCALLS_USED="write exit reboot"
notes:
- The shrink ratios of the syscalls_used lines are based on the
gc-sections line.
- "write exit reboot" are used by a hello.c to simply print "Hello,
World!", exit and shutdown qemu.
- "pselect6" is used by rcutorture to do a long-time sleep.
Signed-off-by: Zhangjin Wu <falcon@xxxxxxxxxxx>
---
arch/riscv/Kconfig | 1 +
arch/riscv/kernel/Makefile | 5 ++--
arch/riscv/kernel/syscalls/Makefile | 46 +++++++++++++++++++++++++++++
3 files changed, 49 insertions(+), 3 deletions(-)
create mode 100644 arch/riscv/kernel/syscalls/Makefile
diff --git a/arch/riscv/Kconfig b/arch/riscv/Kconfig
index 8a73d7180cb8..f78cc6b2413f 100644
--- a/arch/riscv/Kconfig
+++ b/arch/riscv/Kconfig
@@ -140,6 +140,7 @@ config RISCV
select HAVE_FTRACE_MCOUNT_RECORD if !XIP_KERNEL
select HAVE_FUNCTION_GRAPH_TRACER
select HAVE_FUNCTION_TRACER if !XIP_KERNEL
+ select HAVE_SYSCALLS_USED
config ARCH_MMAP_RND_BITS_MIN
default 18 if 64BIT
diff --git a/arch/riscv/kernel/Makefile b/arch/riscv/kernel/Makefile
index 4cf303a779ab..fd716d5ffda5 100644
--- a/arch/riscv/kernel/Makefile
+++ b/arch/riscv/kernel/Makefile
@@ -8,7 +8,8 @@ CFLAGS_REMOVE_ftrace.o = $(CC_FLAGS_FTRACE)
CFLAGS_REMOVE_patch.o = $(CC_FLAGS_FTRACE)
CFLAGS_REMOVE_sbi.o = $(CC_FLAGS_FTRACE)
endif
-CFLAGS_syscall_table.o += $(call cc-option,-Wno-override-init,)
+
+obj-y += syscalls/
ifdef CONFIG_KEXEC
AFLAGS_kexec_relocate.o := -mcmodel=medany $(call cc-option,-mno-relax)
@@ -42,7 +43,6 @@ obj-y += ptrace.o
obj-y += reset.o
obj-y += setup.o
obj-y += signal.o
-obj-y += syscall_table.o
obj-y += sys_riscv.o
obj-y += time.o
obj-y += traps.o
@@ -86,6 +86,5 @@ obj-$(CONFIG_CRASH_CORE) += crash_core.o
obj-$(CONFIG_JUMP_LABEL) += jump_label.o
obj-$(CONFIG_EFI) += efi.o
-obj-$(CONFIG_COMPAT) += compat_syscall_table.o
obj-$(CONFIG_COMPAT) += compat_signal.o
obj-$(CONFIG_COMPAT) += compat_vdso/
diff --git a/arch/riscv/kernel/syscalls/Makefile b/arch/riscv/kernel/syscalls/Makefile
new file mode 100644
index 000000000000..7bd327331a98
--- /dev/null
+++ b/arch/riscv/kernel/syscalls/Makefile
@@ -0,0 +1,46 @@
+# SPDX-License-Identifier: GPL-2.0-or-later
+#
+# Copyright (C) 2023 Zhangjin Wu <falcon@xxxxxxxxxxx>
+#
+
+CFLAGS_syscall_table_used.o += $(call cc-option,-Wno-override-init,)
+obj-y += syscall_table_used.o
+obj-$(CONFIG_COMPAT) += compat_syscall_table_used.o
+
+ifdef CONFIG_SYSCALLS_USED
+syscalls_used := $(shell echo $(CONFIG_SYSCALLS_USED) | tr -s ' ' | tr ' ' '|')
+endif
+
+ifneq ($(syscalls_used),)
+
+quiet_cmd_calc = CALC $@
+ cmd_calc = sed -n -e '/^\[([0-9 +]*)\] = /{s/.*\[\(.*\)\] = .*/sed \\"s%^\\\\[\1\\\\] = %[$$((\1))] = %g\\" -i /gp}' $@ | xargs -I{} echo {} $@ | sh;
+
+quiet_cmd_used = USED $@
+ cmd_used = sed -E -e '/^\[[0-9]*\] = /{/(^\[($(syscalls_used))\] *=|= *\((sys_)*($(syscalls_used))\),)/!{s%^%// %g}}' -i $@;
+
+$(obj)/syscall_table.i: $(src)/../syscall_table.c $(objtree)/.config FORCE
+ $(call if_changed_dep,cpp_i_c)
+ $(call cmd,calc)
+ $(call cmd,used)
+
+$(obj)/syscall_table_used.o: $(obj)/syscall_table.i FORCE
+ $(call if_changed,cc_o_c)
+
+$(obj)/compat_syscall_table.i: $(src)/../compat_syscall_table.c $(objtree)/.config FORCE
+ $(call if_changed_dep,cpp_i_c)
+ $(call cmd,calc)
+ $(call cmd,used)
+
+$(obj)/compat_syscall_table_used.o: $(obj)/compat_syscall_table.i FORCE
+ $(call if_changed,cc_o_c)
+
+else
+
+$(obj)/syscall_table_used.o: $(src)/../syscall_table.c FORCE
+ $(call if_changed_rule,cc_o_c)
+
+$(obj)/compat_syscall_table_used.o: $(src)/../compat_syscall_table.c FORCE
+ $(call if_changed_rule,cc_o_c)
+
+endif
--
2.25.1