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

Message ID 3042b32a13aecbf8f3c8bedee34a4ca8c7d7cb2e.1695679700.git.falcon@tinylab.org
State New
Headers
Series DCE/DSE: Add Dead Syscalls Elimination support, part1 |

Commit Message

Zhangjin Wu Sept. 25, 2023, 10:40 p.m. UTC
  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@tinylab.org>
---
 arch/mips/Kconfig                  |  1 +
 arch/mips/kernel/syscalls/Makefile | 23 +++++++++++++++++++++--
 2 files changed, 22 insertions(+), 2 deletions(-)
  

Comments

Arnd Bergmann Sept. 26, 2023, 6:07 a.m. UTC | #1
On Tue, Sep 26, 2023, at 00:40, Zhangjin Wu wrote:
> 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)

My feeling is that instead of postprocessing the generated files,
it would be much better to make the elimination part of the
existing infrastructure that generates the files from syscall.tbl,
and finally change the include/asm-generic/unistd.h to the
same format, as we had planned for a long time.

I should be able to help out with that part.

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

> On Tue, Sep 26, 2023, at 00:40, Zhangjin Wu wrote:
> > 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)
> 
> My feeling is that instead of postprocessing the generated files,
> it would be much better to make the elimination part of the
> existing infrastructure that generates the files from syscall.tbl,
> and finally change the include/asm-generic/unistd.h to the
> same format, as we had planned for a long time.
>

Agree very much, then we can simply touch the common files, no need to
touch the arch specific files.

> I should be able to help out with that part.
>

Thanks, is it enough to touch these ones?

    $ ls scripts/syscall*
    scripts/syscallhdr.sh  scripts/syscallnr.sh  scripts/syscalltbl.sh

One question here is that is it possible or required to share the used syscalls
selection code among them?

Another question require your help is the compat part, the compat stuff makes
things harder (include the Kconfig symbol interface definition and select
logic), perhaps we can simply limit our first DSE version under !COMPAT?

Best regards,
Zhangjin

>       Arnd
  

Patch

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			\