Received: by 2002:a05:7412:2a8c:b0:e2:908c:2ebd with SMTP id u12csp1519345rdh; Mon, 25 Sep 2023 15:46:59 -0700 (PDT) X-Google-Smtp-Source: AGHT+IF4JWPjVj1z7VFdFENxUN8GpG4RO7hR7JwDR/1vTiUvaawSeCz+gS3T3IVu4S430ZCBb4Sy X-Received: by 2002:a17:903:2452:b0:1c6:2780:3adc with SMTP id l18-20020a170903245200b001c627803adcmr1862825pls.24.1695682018984; Mon, 25 Sep 2023 15:46:58 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1695682018; cv=none; d=google.com; s=arc-20160816; b=a+jYeInhft2OZBGBYWOVysGp+rj2JnmYzDeDICHtOAQBxuWqMOlakh8+vNMSMhehxu /CpVOyhAJGruqAVPCHaEfCZUuC5YwnHVTsYSmKqn3ckwvZSjwcxgXAMRNCo8+cUQhOtR klmMy9HjzSUgA0+chUBVrfc3+pErWByvDzQACpzc8CPVaRps9WgvyerkOrMPHAzPuwKy kWD+0FFYZKi5QrbxJj7sZI5osaB+l63EXt66HBD1K6Vmx7+8aHOtmBmKReu9osuDAHNb OHvN5PoeG6RPZig7ESytmKA9KL+TsUHFbBATjQoaY2KRp6yMW1xCKqPzsBJN10v9t8uy LvkA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:feedback-id:content-transfer-encoding :mime-version:references:in-reply-to:message-id:date:subject:cc:to :from; bh=NTYHdo553y/1oC4xe1CfFQWSPnzrdW/ohVSonOrb7Z8=; fh=Hu5Z85mCjwbIpQ6O7J4KtGQomc/bitxjLDlxDRGGJ5w=; b=t7c7Bi70+s4sJsv8eh7o5x3gAvWgWnT7S8nFrUydY3b7pkT4au0gkHeubV3Oc8kIeA mek2uHXUIb7V8ZWmu/XNvywuhbin9et1UODDMHu95ojKMeHisKswFdJwrgjyQWdsAiS4 pzos0rCgqhyc3F/WXdzNvlLFtXLMk/IPOr2aiT3QZT8sZSbSSn5UupZ9z9mNrz2y7p0a X725N8xOORTk9/ur4zGhOJZNTs47wTZ8b15HTa1ME4ZZBplUpta/AEY5hLQLzqqMGk2X tua1ktWI8rjVE8jMI0CxekLgulUukUwRu4Un1Pr+qrD4QTa6D7Y4B7lZZ1DDOmNsvXTF xDhw== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.35 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from groat.vger.email (groat.vger.email. [23.128.96.35]) by mx.google.com with ESMTPS id q15-20020a17090311cf00b001c5cf1c30d8si11715453plh.367.2023.09.25.15.46.56 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 25 Sep 2023 15:46:58 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.35 as permitted sender) client-ip=23.128.96.35; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.35 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by groat.vger.email (Postfix) with ESMTP id 9DA8B807830C; Mon, 25 Sep 2023 15:43:51 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at groat.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232611AbjIYWnw (ORCPT + 99 others); Mon, 25 Sep 2023 18:43:52 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:60680 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229481AbjIYWnv (ORCPT ); Mon, 25 Sep 2023 18:43:51 -0400 Received: from bg4.exmail.qq.com (bg4.exmail.qq.com [43.154.54.12]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 28C05109; Mon, 25 Sep 2023 15:43:43 -0700 (PDT) X-QQ-mid: bizesmtp81t1695681815tls4ddg5 Received: from linux-lab-host.localdomain ( [116.30.124.152]) by bizesmtp.qq.com (ESMTP) with id ; Tue, 26 Sep 2023 06:43:33 +0800 (CST) X-QQ-SSF: 01200000002000E0Y000B00A0000000 X-QQ-FEAT: aBJFcW+uBGYffXzygR0k5C/WLWWZcLSCd2TbMPG0O7Tlzn1m+5D3uZe31rmrB aujFQULia5dBlILNDRUyEjx57EpMsxaL6Hz0UfpOimP01EPYwODfLNDt/08+rViZ0kDvJPS c/l6PK5eg6fwnuRPIEYFxcxK83ddGI9AWtGlJq/Ee72+sNa4MrAQ3EN8Qe34Np4jqLArABG JS2WsLg0nT+US7V5b/8yFqpM/D3X+G29I8CLiztdDhT1Y7JbtyMwz8ftGBL0fuh92PguYE4 IKeAjeoyg3J6mOYoYZE003kMKygyo6u/CV03ypKigE7GsLxTYVFe5MpxCrKElLULeYxae/d HtHN7uvHgeUIKpoUbjQ3Dfq7OGBezfWvX3+if5KLWzLVYk17ZVHC2GUJSVYzHQJ1NQ/1p5y X-QQ-GoodBg: 0 X-BIZMAIL-ID: 4775298546958751858 From: Zhangjin Wu To: linux-kernel@vger.kernel.org, linux-mips@vger.kernel.org, linux-riscv@lists.infradead.org, Arnd Bergmann Cc: falcon@tinylab.org, palmer@rivosinc.com, paul.walmsley@sifive.com, paulburton@kernel.org, paulmck@kernel.org, tsbogend@alpha.franken.de, w@1wt.eu, =?UTF-8?q?Thomas=20Wei=C3=9Fschuh?= , Tim Bird Subject: [PATCH v1 7/7] DCE/DSE: riscv: trim syscall tables Date: Tue, 26 Sep 2023 06:43:32 +0800 Message-Id: X-Mailer: git-send-email 2.25.1 In-Reply-To: References: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-QQ-SENDSIZE: 520 Feedback-ID: bizesmtp:tinylab.org:qybglogicsvrgz:qybglogicsvrgz5a-1 X-Spam-Status: No, score=2.8 required=5.0 tests=HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,RCVD_IN_SBL_CSS,SPF_HELO_NONE,SPF_PASS autolearn=no autolearn_force=no version=3.4.6 X-Spam-Level: ** X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on groat.vger.email Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (groat.vger.email [0.0.0.0]); Mon, 25 Sep 2023 15:43:52 -0700 (PDT) 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 --- 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(-) 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 +#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 }; 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 }; -- 2.25.1