[RFC PATCH 2/5] MIPS: Add dead syscalls elimination support

From: Zhangjin Wu
Date: Thu Feb 16 2023 - 19:53:05 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 *.tbl will be generated, accordingly, the kernel
api unistd_nr_*.h and syscall_table_*.h will be generated from the
'used' *tbl variant. the user api version of unistd_*.h is reserved
as-is.

Here is a test result on qemu with a minimal malta config.

| mipsel malta | config
----------------|-----------------|-------------------
vmlinux | 5041628 | https://pastebin.com/0bE2ibLD
+ gc-sections | 4474060 (-11.2%)| CONFIG_HAVE_LD_DEAD_CODE_DATA_ELIMINATION=y
+ syscalls_used | 4265280 (-4.67%)| CONFIG_SYSCALLS_USED="_newselect"
+ syscalls_used | 4274364 (-4.46%)| 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.

- "_newselect" is used by rcutorture to do a long-time sleep.

Signed-off-by: Zhangjin Wu <falcon@xxxxxxxxxxx>
---
arch/mips/Kconfig | 1 +
arch/mips/kernel/syscalls/Makefile | 24 ++++++++++++++++++++++--
2 files changed, 23 insertions(+), 2 deletions(-)

diff --git a/arch/mips/Kconfig b/arch/mips/Kconfig
index 15cb692b0a09..868d9a871b3e 100644
--- a/arch/mips/Kconfig
+++ b/arch/mips/Kconfig
@@ -102,6 +102,7 @@ config MIPS
select TRACE_IRQFLAGS_SUPPORT
select ARCH_HAS_ELFCORE_COMPAT
select HAVE_ARCH_KCSAN if 64BIT
+ select HAVE_SYSCALLS_USED

config MIPS_FIXUP_BIGPHYS_ADDR
bool
diff --git a/arch/mips/kernel/syscalls/Makefile b/arch/mips/kernel/syscalls/Makefile
index e6b21de65cca..8ffba5301cf0 100644
--- a/arch/mips/kernel/syscalls/Makefile
+++ b/arch/mips/kernel/syscalls/Makefile
@@ -26,10 +26,30 @@ sysnr_pfx_unistd_nr_n32 := N32
sysnr_pfx_unistd_nr_n64 := 64
sysnr_pfx_unistd_nr_o32 := O32

-$(kapi)/unistd_nr_%.h: $(src)/syscall_%.tbl $(sysnr) FORCE
+ifdef CONFIG_SYSCALLS_USED
+syscalls_used := $(shell echo $(CONFIG_SYSCALLS_USED) | tr -s ' ' | tr ' ' '|')
+endif
+
+ifneq ($(syscalls_used),)
+utbl := arch/$(SRCARCH)/include/generated/tbl
+_tbl := $(src)/syscall_%.tbl
+ tbl := $(utbl)/syscall_used_%.tbl
+
+$(shell mkdir -p $(utbl))
+
+quiet_cmd_used = USED $@
+ cmd_used = sed -E -e "/^[0-9]*[[:space:]]/{/(^($(syscalls_used))[[:space:]]|[[:space:]]($(syscalls_used))[[:space:]]|[[:space:]]($(syscalls_used))$$)/!{s/^/\#/g}}" $< > $@;
+
+$(tbl): $(_tbl) $(objtree)/.config
+ $(call cmd,used)
+else
+tbl := $(src)/syscall_%.tbl
+endif
+
+$(kapi)/unistd_nr_%.h: $(tbl) $(sysnr) FORCE
$(call if_changed,sysnr)

-$(kapi)/syscall_table_%.h: $(src)/syscall_%.tbl $(systbl) FORCE
+$(kapi)/syscall_table_%.h: $(tbl) $(systbl) FORCE
$(call if_changed,systbl)

uapisyshdr-y += unistd_n32.h \
--
2.25.1