[PATCH v1 4/7] DCE/DSE: mips: add HAVE_TRIM_UNUSED_SYSCALLS support

From: Zhangjin Wu
Date: Mon Sep 25 2023 - 18:40:31 EST


For HAVE_TRIM_UNUSED_SYSCALLS, the syscall tables are hacked with the
input used syscalls.

Based on the used syscalls information, a new version of tbl file is
generated from the original tbl file and named with a 'used' suffix.

With this new tbl file, both unistd_nr_*.h and syscall_table_*.h files
are updated to only include the used syscalls.

$ grep _Linux_syscalls -ur arch/mips/include/generated/asm/
arch/mips/include/generated/asm/unistd_nr_n64.h:#define __NR_64_Linux_syscalls 165
arch/mips/include/generated/asm/unistd_nr_n32.h:#define __NR_N32_Linux_syscalls 165
arch/mips/include/generated/asm/unistd_nr_o32.h:#define __NR_O32_Linux_syscalls 89

$ grep -vr sys_ni_syscall arch/mips/include/generated/asm/syscall_table_*.h
arch/mips/include/generated/asm/syscall_table_n32.h:__SYSCALL(58, sys_exit)
arch/mips/include/generated/asm/syscall_table_n32.h:__SYSCALL(164, sys_reboot)
arch/mips/include/generated/asm/syscall_table_n64.h:__SYSCALL(58, sys_exit)
arch/mips/include/generated/asm/syscall_table_n64.h:__SYSCALL(164, sys_reboot)
arch/mips/include/generated/asm/syscall_table_o32.h:__SYSCALL(1, sys_exit)
arch/mips/include/generated/asm/syscall_table_o32.h:__SYSCALL(88, sys_reboot)

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

diff --git a/arch/mips/Kconfig b/arch/mips/Kconfig
index bc8421859006..8a6927eff23d 100644
--- a/arch/mips/Kconfig
+++ b/arch/mips/Kconfig
@@ -89,6 +89,7 @@ config MIPS
select HAVE_SPARSE_SYSCALL_NR
select HAVE_STACKPROTECTOR
select HAVE_SYSCALL_TRACEPOINTS
+ select HAVE_TRIM_UNUSED_SYSCALLS if HAVE_LD_DEAD_CODE_DATA_ELIMINATION
select HAVE_VIRT_CPU_ACCOUNTING_GEN if 64BIT || !SMP
select IRQ_FORCED_THREADING
select ISA if EISA
diff --git a/arch/mips/kernel/syscalls/Makefile b/arch/mips/kernel/syscalls/Makefile
index e6b21de65cca..1e292a9f84a0 100644
--- a/arch/mips/kernel/syscalls/Makefile
+++ b/arch/mips/kernel/syscalls/Makefile
@@ -26,10 +26,29 @@ 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
+ifndef CONFIG_TRIM_UNUSED_SYSCALLS
+tbl = $(src)/syscall_%.tbl
+else
+
+include $(srctree)/scripts/Makefile.syscalls
+
+orig_tbl = $(src)/syscall_%.tbl
+ tbl_dir = arch/$(SRCARCH)/include/generated/tbl
+ tbl = $(tbl_dir)/syscall_used_%.tbl
+
+quiet_cmd_used = USED $@
+ cmd_used = sed -E -e "/^[0-9]*[[:space:]]/{/(^($(used_syscalls))[[:space:]]|[[:space:]]($(used_syscalls))[[:space:]]|[[:space:]]($(used_syscalls))$$)/!{s/^/\#/g}}" $< > $@;
+
+$(tbl): $(orig_tbl) $(used_syscalls_deps) FORCE
+ $(Q)mkdir -p $(tbl_dir)
+ $(call cmd,used)
+
+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