From nobody Fri Sep 12 22:42:03 2025 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id DAA45C07E9D for ; Mon, 26 Sep 2022 20:38:02 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230244AbiIZUiB (ORCPT ); Mon, 26 Sep 2022 16:38:01 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:35836 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230057AbiIZUhx (ORCPT ); Mon, 26 Sep 2022 16:37:53 -0400 Received: from conuserg-10.nifty.com (conuserg-10.nifty.com [210.131.2.77]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 904909E882; Mon, 26 Sep 2022 13:37:51 -0700 (PDT) Received: from zoe.. (133-32-182-133.west.xps.vectant.ne.jp [133.32.182.133]) (authenticated) by conuserg-10.nifty.com with ESMTP id 28QKaWu1018737; Tue, 27 Sep 2022 05:36:34 +0900 DKIM-Filter: OpenDKIM Filter v2.10.3 conuserg-10.nifty.com 28QKaWu1018737 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=nifty.com; s=dec2015msa; t=1664224594; bh=yCb6Dol7u5zeqemMeSYfgpDFkRAmK+1W2KSFszpgWOc=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=Ag6L+QrdRfmi4HvEHJPPp0QbeYgv65pZBGhwWayIPsH6kzuQiXpdg/BbAPvheYQRF IWR8buPedfzstaSN1PWiHBH2O3hEwmITIIJ4Xd3AnWmYWUpKFEGCe2/GeTinBPy6+o FEOrGIJSjX7GluaBfnNxurL140wBKdeQkdAO4HGYvtkRkSUMV1ln0XlRSHJk6Cif3Y zZB85xG6gfEijLacEdBztyqsc8SkrrCCCZN8XX1QaTy2v/akdodlzyW69koJ/BRETX hKR1SoXxI2h2vA+bof4SNcU4quXAgfFpTqg09GnFbT326fc/7KTvGfXy4R+6Gnpcd3 JMpCMjw7rmHFQ== X-Nifty-SrcIP: [133.32.182.133] From: Masahiro Yamada To: linux-kbuild@vger.kernel.org Cc: linux-arch@vger.kernel.org, linux-kernel@vger.kernel.org, linux-ia64@vger.kernel.org, Al Viro , Ard Biesheuvel , Nicolas Pitre , Masahiro Yamada , Arnd Bergmann , Luis Chamberlain , Michal Marek , Nick Desaulniers , linux-modules@vger.kernel.org Subject: [PATCH v2 1/7] kbuild: generate KSYMTAB entries by modpost Date: Tue, 27 Sep 2022 05:36:19 +0900 Message-Id: <20220926203625.1117261-2-masahiroy@kernel.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20220926203625.1117261-1-masahiroy@kernel.org> References: <20220926203625.1117261-1-masahiroy@kernel.org> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" Commit 7b4537199a4a ("kbuild: link symbol CRCs at final link, removing CONFIG_MODULE_REL_CRCS") made modpost output CRCs in the same way whether the EXPORT_SYMBOL() is placed in *.c or *.S. This commit applies a similar approach to the entire data structure of EXPORT_SYMBOL() for further cleanups. The EXPORT_SYMBOL() compilation is split into two stages. When a source file is compiled, EXPORT_SYMBOL() is converted into a dummy symbol in the .discard.export_symbol section. For example, EXPORT_SYMBOL(foo); EXPORT_SYMBOL_NS_GPL(bar, BAR_NAMESPACE); will be encoded into the following assembly code: .section .discard.export_symbol __export_symbol.foo: .asciz "" .previous .section .discard.export_symbol __export_symbol_gpl.bar: .asciz "BAR_NAMESPACE" .previous They are just markers to tell modpost the name, license, and namespace of the symbols. They will be dropped from the final vmlinux and modules because the section name starts with ".discard.". Then, modpost extracts all the information about EXPORT_SYMBOL() from the .discard.export_symbol section, then generates C code: KSYMTAB_FUNC(foo, "", ""); KSYMTAB_FUNC(bar, "_gpl", "BAR_NAMESPACE"); KSYMTAB_FUNC() (or KSYMTAB_DATA() if it is data) is expanded to struct kernel_symbol that will be linked to the vmlinux or a module. With this change, EXPORT_SYMBOL() works in the same way for *.c and *.S files, providing the following benefits. [1] Deprecate EXPORT_DATA_SYMBOL() In the old days, EXPORT_SYMBOL() was only available in C files. To export a symbol in *.S, EXPORT_SYMBOL() was placed in a separate *.c file. arch/arm/kernel/armksyms.c is one example written in the classic manner. Commit 22823ab419d8 ("EXPORT_SYMBOL() for asm") removed this limitation. Since then, EXPORT_SYMBOL() can be placed close to the symbol definition in *.S files. It was a nice improvement. However, as that commit mentioned, you need to use EXPORT_DATA_SYMBOL() for data objects on some architectures. In the new approach, modpost checks symbol's type (STT_FUNC or not), and outputs KSYMTAB_FUNC() or KSYMTAB_DATA() accordingly. There are only two users of EXPORT_DATA_SYMBOL: EXPORT_DATA_SYMBOL_GPL(empty_zero_page) (arch/ia64/kernel/head.S) EXPORT_DATA_SYMBOL(ia64_ivt) (arch/ia64/kernel/ivt.S) They are transformed as follows and output into .vmlinux.export.c KSYMTAB_DATA(empty_zero_page, "_gpl", ""); KSYMTAB_DATA(ia64_ivt, "", ""); The other EXPORT_SYMBOL users in ia64 assembly are output as KSYMTAB_FUNC(). EXPORT_DATA_SYMBOL() is now deprecated. [2] merge and There are two similar header implementations: include/linux/export.h for .c files include/asm-generic/export.h for .S files Ideally, the functionality should be consistent between them, but they tend to diverge. Commit 8651ec01daed ("module: add support for symbol namespaces.") did not support the namespace for *.S files. This commit shifts the essential implementation part to C, which supports EXPORT_SYMBOL_NS() for *.S files. and will remain as a wrapper of for a while. They will be removed after #include directives are all replaced with #include . [3] Implement CONFIG_TRIM_UNUSED_KSYMS in one-pass algorithm When CONFIG_TRIM_UNUSED_KSYMS is enabled, Kbuild recursively traverses the directory tree to determine which EXPORT_SYMBOL to trim. If an EXPORT_SYMBOL turns out to be unused by anyone, Kbuild begins the second traverse, where some source files are recompiled with their EXPORT_SYMBOL() tuned into a no-op. We can do this better now; modpost can selectively emit KSYMTAB entries that are really referenced by modules. Signed-off-by: Masahiro Yamada --- Changes in v2: - Use KSYMTAB_FUNC and KSYMTAB_DATA for functions and data, respectively This distinction is needed for ia64. arch/ia64/include/asm/Kbuild | 1 + arch/ia64/include/asm/export.h | 3 - include/asm-generic/export.h | 84 ++-------------------------- include/linux/export-internal.h | 67 ++++++++++++++++++++++- include/linux/export.h | 97 +++++++++++---------------------- kernel/module/internal.h | 1 + kernel/module/main.c | 1 - scripts/Makefile.build | 8 +-- scripts/check-local-export | 4 +- scripts/mod/modpost.c | 81 +++++++++++++++++---------- scripts/mod/modpost.h | 1 + 11 files changed, 164 insertions(+), 184 deletions(-) delete mode 100644 arch/ia64/include/asm/export.h diff --git a/arch/ia64/include/asm/Kbuild b/arch/ia64/include/asm/Kbuild index f994c1daf9d4..fc998339c405 100644 --- a/arch/ia64/include/asm/Kbuild +++ b/arch/ia64/include/asm/Kbuild @@ -1,5 +1,6 @@ # SPDX-License-Identifier: GPL-2.0 generated-y +=3D syscall_table.h +generic-y +=3D export.h generic-y +=3D kvm_para.h generic-y +=3D mcs_spinlock.h generic-y +=3D vtime.h diff --git a/arch/ia64/include/asm/export.h b/arch/ia64/include/asm/export.h deleted file mode 100644 index ad18c6583252..000000000000 --- a/arch/ia64/include/asm/export.h +++ /dev/null @@ -1,3 +0,0 @@ -/* EXPORT_DATA_SYMBOL !=3D EXPORT_SYMBOL here */ -#define KSYM_FUNC(name) @fptr(name) -#include diff --git a/include/asm-generic/export.h b/include/asm-generic/export.h index 5e4b1f2369d2..0ae9f38a904c 100644 --- a/include/asm-generic/export.h +++ b/include/asm-generic/export.h @@ -3,86 +3,12 @@ #define __ASM_GENERIC_EXPORT_H =20 /* - * This comment block is used by fixdep. Please do not remove. - * - * When CONFIG_MODVERSIONS is changed from n to y, all source files having - * EXPORT_SYMBOL variants must be re-compiled because genksyms is run as a - * side effect of the *.o build rule. + * and are deprecated. + * Please include directly. */ +#include =20 -#ifndef KSYM_FUNC -#define KSYM_FUNC(x) x -#endif -#ifdef CONFIG_HAVE_ARCH_PREL32_RELOCATIONS -#define KSYM_ALIGN 4 -#elif defined(CONFIG_64BIT) -#define KSYM_ALIGN 8 -#else -#define KSYM_ALIGN 4 -#endif - -.macro __put, val, name -#ifdef CONFIG_HAVE_ARCH_PREL32_RELOCATIONS - .long \val - ., \name - ., 0 -#elif defined(CONFIG_64BIT) - .quad \val, \name, 0 -#else - .long \val, \name, 0 -#endif -.endm - -/* - * note on .section use: we specify progbits since usage of the "M" (SHF_M= ERGE) - * section flag requires it. Use '%progbits' instead of '@progbits' since = the - * former apparently works on all arches according to the binutils source. - */ - -.macro ___EXPORT_SYMBOL name,val,sec -#if defined(CONFIG_MODULES) && !defined(__DISABLE_EXPORTS) - .section ___ksymtab\sec+\name,"a" - .balign KSYM_ALIGN -__ksymtab_\name: - __put \val, __kstrtab_\name - .previous - .section __ksymtab_strings,"aMS",%progbits,1 -__kstrtab_\name: - .asciz "\name" - .previous -#endif -.endm - -#if defined(CONFIG_TRIM_UNUSED_KSYMS) - -#include -#include - -.macro __ksym_marker sym - .section ".discard.ksym","a" -__ksym_marker_\sym: - .previous -.endm - -#define __EXPORT_SYMBOL(sym, val, sec) \ - __ksym_marker sym; \ - __cond_export_sym(sym, val, sec, __is_defined(__KSYM_##sym)) -#define __cond_export_sym(sym, val, sec, conf) \ - ___cond_export_sym(sym, val, sec, conf) -#define ___cond_export_sym(sym, val, sec, enabled) \ - __cond_export_sym_##enabled(sym, val, sec) -#define __cond_export_sym_1(sym, val, sec) ___EXPORT_SYMBOL sym, val, sec -#define __cond_export_sym_0(sym, val, sec) /* nothing */ - -#else -#define __EXPORT_SYMBOL(sym, val, sec) ___EXPORT_SYMBOL sym, val, sec -#endif - -#define EXPORT_SYMBOL(name) \ - __EXPORT_SYMBOL(name, KSYM_FUNC(name),) -#define EXPORT_SYMBOL_GPL(name) \ - __EXPORT_SYMBOL(name, KSYM_FUNC(name), _gpl) -#define EXPORT_DATA_SYMBOL(name) \ - __EXPORT_SYMBOL(name, name,) -#define EXPORT_DATA_SYMBOL_GPL(name) \ - __EXPORT_SYMBOL(name, name,_gpl) +#define EXPORT_DATA_SYMBOL(name) EXPORT_SYMBOL(name) +#define EXPORT_DATA_SYMBOL_GPL(name) EXPORT_SYMBOL_GPL(name) =20 #endif diff --git a/include/linux/export-internal.h b/include/linux/export-interna= l.h index fe7e6ba918f1..8d921cde1b33 100644 --- a/include/linux/export-internal.h +++ b/include/linux/export-internal.h @@ -1,6 +1,6 @@ /* SPDX-License-Identifier: GPL-2.0-only */ /* - * Please do not include this explicitly. + * Please do not include this except from kernel/module/. * This is used by C files generated by modpost. */ =20 @@ -10,6 +10,71 @@ #include #include =20 +/* + * This struct is used in kernel/module/, but placed in this header + * to force rebuilding vmlinux when this header is updated. + */ +struct kernel_symbol { +#ifdef CONFIG_HAVE_ARCH_PREL32_RELOCATIONS + int value_offset; + int name_offset; + int namespace_offset; +#else + unsigned long value; + const char *name; + const char *namespace; +#endif +}; + +#if defined(CONFIG_HAVE_ARCH_PREL32_RELOCATIONS) +/* + * relative reference: this reduces the size by half on 64-bit architectur= es, + * and eliminates the need for absolute relocations that require runtime + * processing on relocatable kernels. + */ +#define __KSYM_REF(sym) ".long " #sym "- ." +#elif defined(CONFIG_64BIT) +#define __KSYM_REF(sym) ".quad " #sym +#else +#define __KSYM_REF(sym) ".long " #sym +#endif + +/* + * For every exported symbol, do the following: + * + * - Put the name of the symbol and namespace (empty string "" for none) in + * __ksymtab_strings. + * - Place a struct kernel_symbol entry in the __ksymtab section. + * + * Note on .section use: we specify progbits since usage of the "M" (SHF_M= ERGE) + * section flag requires it. Use '%progbits' instead of '@progbits' since = the + * former apparently works on all arches according to the binutils source. + */ +#define __KSYMTAB(name, sym, sec, ns) \ + asm(" .section \"__ksymtab_strings\",\"aMS\",%progbits,1" "\n" \ + "__kstrtab_" #name ":" "\n" \ + " .asciz \"" #name "\"" "\n" \ + "__kstrtabns_" #name ":" "\n" \ + " .asciz \"" ns "\"" "\n" \ + " .previous" "\n" \ + " .section \"___ksymtab" sec "+" #name "\", \"a\"" "\n" \ + " .balign 4" "\n" \ + "__ksymtab_" #name ":" "\n" \ + __KSYM_REF(sym) "\n" \ + __KSYM_REF(__kstrtab_ ##name) "\n" \ + __KSYM_REF(__kstrtabns_ ##name) "\n" \ + " .previous" "\n" \ + ) + +#ifdef CONFIG_IA64 +#define KSYM_FUNC(name) @fptr(name) +#else +#define KSYM_FUNC(name) name +#endif + +#define KSYMTAB_FUNC(name, sec, ns) __KSYMTAB(name, KSYM_FUNC(name), sec, = ns) +#define KSYMTAB_DATA(name, sec, ns) __KSYMTAB(name, name, sec, ns) + #define SYMBOL_CRC(sym, crc, sec) \ asm(".section \"___kcrctab" sec "+" #sym "\",\"a\"" "\n" \ "__crc_" #sym ":" "\n" \ diff --git a/include/linux/export.h b/include/linux/export.h index 3f31ced0d977..0c71577cf8bb 100644 --- a/include/linux/export.h +++ b/include/linux/export.h @@ -2,6 +2,7 @@ #ifndef _LINUX_EXPORT_H #define _LINUX_EXPORT_H =20 +#include #include =20 /* @@ -28,72 +29,28 @@ extern struct module __this_module; #else #define THIS_MODULE ((struct module *)0) #endif - -#ifdef CONFIG_HAVE_ARCH_PREL32_RELOCATIONS -#include -/* - * Emit the ksymtab entry as a pair of relative references: this reduces - * the size by half on 64-bit architectures, and eliminates the need for - * absolute relocations that require runtime processing on relocatable - * kernels. - */ -#define __KSYMTAB_ENTRY(sym, sec) \ - __ADDRESSABLE(sym) \ - asm(" .section \"___ksymtab" sec "+" #sym "\", \"a\" \n" \ - " .balign 4 \n" \ - "__ksymtab_" #sym ": \n" \ - " .long " #sym "- . \n" \ - " .long __kstrtab_" #sym "- . \n" \ - " .long __kstrtabns_" #sym "- . \n" \ - " .previous \n") - -struct kernel_symbol { - int value_offset; - int name_offset; - int namespace_offset; -}; -#else -#define __KSYMTAB_ENTRY(sym, sec) \ - static const struct kernel_symbol __ksymtab_##sym \ - __attribute__((section("___ksymtab" sec "+" #sym), used)) \ - __aligned(sizeof(void *)) \ - =3D { (unsigned long)&sym, __kstrtab_##sym, __kstrtabns_##sym } - -struct kernel_symbol { - unsigned long value; - const char *name; - const char *namespace; -}; -#endif +#endif /* __ASSEMBLY__ */ =20 #ifdef __GENKSYMS__ =20 #define ___EXPORT_SYMBOL(sym, sec, ns) __GENKSYMS_EXPORT_SYMBOL(sym) =20 +#elif defined(__ASSEMBLY__) + +#define ___EXPORT_SYMBOL(sym, sec, ns) \ + .section .discard.export_symbol ; \ + __export_symbol##sec##.sym: ; \ + .asciz ns ; \ + .previous + #else =20 -/* - * For every exported symbol, do the following: - * - * - Put the name of the symbol and namespace (empty string "" for none) in - * __ksymtab_strings. - * - Place a struct kernel_symbol entry in the __ksymtab section. - * - * note on .section use: we specify progbits since usage of the "M" (SHF_M= ERGE) - * section flag requires it. Use '%progbits' instead of '@progbits' since = the - * former apparently works on all arches according to the binutils source. - */ -#define ___EXPORT_SYMBOL(sym, sec, ns) \ - extern typeof(sym) sym; \ - extern const char __kstrtab_##sym[]; \ - extern const char __kstrtabns_##sym[]; \ - asm(" .section \"__ksymtab_strings\",\"aMS\",%progbits,1 \n" \ - "__kstrtab_" #sym ": \n" \ - " .asciz \"" #sym "\" \n" \ - "__kstrtabns_" #sym ": \n" \ - " .asciz \"" ns "\" \n" \ - " .previous \n"); \ - __KSYMTAB_ENTRY(sym, sec) +#define ___EXPORT_SYMBOL(sym, sec, ns) \ + __ADDRESSABLE(sym) \ + asm(".section .discard.export_symbol \n" \ + "__export_symbol" #sec "." #sym ": \n" \ + ".asciz " "\"" ns "\"" " \n" \ + ".previous \n") =20 #endif =20 @@ -117,9 +74,21 @@ struct kernel_symbol { * from the $(NM) output (see scripts/gen_ksymdeps.sh). These symbols are * discarded in the final link stage. */ + +#ifdef __ASSEMBLY__ + +#define __ksym_marker(sym) \ + .section ".discard.ksym","a" ; \ +__ksym_marker_##sym: ; \ + .previous + +#else + #define __ksym_marker(sym) \ static int __ksym_marker_##sym[0] __section(".discard.ksym") __used =20 +#endif + #define __EXPORT_SYMBOL(sym, sec, ns) \ __ksym_marker(sym); \ __cond_export_sym(sym, sec, ns, __is_defined(__KSYM_##sym)) @@ -147,11 +116,9 @@ struct kernel_symbol { #define _EXPORT_SYMBOL(sym, sec) __EXPORT_SYMBOL(sym, sec, "") #endif =20 -#define EXPORT_SYMBOL(sym) _EXPORT_SYMBOL(sym, "") -#define EXPORT_SYMBOL_GPL(sym) _EXPORT_SYMBOL(sym, "_gpl") -#define EXPORT_SYMBOL_NS(sym, ns) __EXPORT_SYMBOL(sym, "", __stringify(ns)) -#define EXPORT_SYMBOL_NS_GPL(sym, ns) __EXPORT_SYMBOL(sym, "_gpl", __strin= gify(ns)) - -#endif /* !__ASSEMBLY__ */ +#define EXPORT_SYMBOL(sym) _EXPORT_SYMBOL(sym,) +#define EXPORT_SYMBOL_GPL(sym) _EXPORT_SYMBOL(sym,_gpl) +#define EXPORT_SYMBOL_NS(sym, ns) __EXPORT_SYMBOL(sym,, __stringify(ns)) +#define EXPORT_SYMBOL_NS_GPL(sym, ns) __EXPORT_SYMBOL(sym,_gpl, __stringif= y(ns)) =20 #endif /* _LINUX_EXPORT_H */ diff --git a/kernel/module/internal.h b/kernel/module/internal.h index 680d980a4fb2..fc2bf6c6ba05 100644 --- a/kernel/module/internal.h +++ b/kernel/module/internal.h @@ -7,6 +7,7 @@ =20 #include #include +#include #include #include #include diff --git a/kernel/module/main.c b/kernel/module/main.c index a4e4d84b6f4e..776aad90b3c6 100644 --- a/kernel/module/main.c +++ b/kernel/module/main.c @@ -6,7 +6,6 @@ =20 #define INCLUDE_VERMAGIC =20 -#include #include #include #include diff --git a/scripts/Makefile.build b/scripts/Makefile.build index 91d2e5461a3e..4c1d0bd1bc03 100644 --- a/scripts/Makefile.build +++ b/scripts/Makefile.build @@ -164,7 +164,7 @@ quiet_cmd_cc_o_c =3D CC $(quiet_modtag) $@ ifdef CONFIG_MODVERSIONS # When module versioning is enabled the following steps are executed: # o compile a .o from .c -# o if .o doesn't contain a __ksymtab version, i.e. does +# o if .o doesn't contain a __export_symbol*, i.e. does # not export symbols, it's done. # o otherwise, we calculate symbol versions using the good old # genksyms on the preprocessed source and dump them into the .cmd file. @@ -172,7 +172,7 @@ ifdef CONFIG_MODVERSIONS # be compiled and linked to the kernel and/or modules. =20 gen_symversions =3D \ - if $(NM) $@ 2>/dev/null | grep -q __ksymtab; then \ + if $(NM) $@ 2>/dev/null | grep -q '__export_symbol.*\.'; then \ $(call cmd_gensymtypes_$(1),$(KBUILD_SYMTYPES),$(@:.o=3D.symtypes)) \ >> $(dot-target).cmd; \ fi @@ -288,9 +288,7 @@ $(obj)/%.lst: $(src)/%.c FORCE cmd_gensymtypes_S =3D = \ { echo "\#include " ; = \ echo "\#include " ; = \ - $(CPP) $(a_flags) $< | = \ - grep "\<___EXPORT_SYMBOL\>" | = \ - sed 's/.*___EXPORT_SYMBOL[[:space:]]*\([a-zA-Z0-9_]*\)[[:space:]]*,.*= /EXPORT_SYMBOL(\1);/' ; } | \ + $(NM) $@ | sed -n 's/.*__export_symbol.*\.\(.*\)/EXPORT_SYMBOL(\1);/p= ' ; } | \ $(CPP) -D__GENKSYMS__ $(c_flags) -xc - | $(genksyms) =20 quiet_cmd_cc_symtypes_S =3D SYM $(quiet_modtag) $@ diff --git a/scripts/check-local-export b/scripts/check-local-export index 0c049ff44aca..e07534880fbf 100755 --- a/scripts/check-local-export +++ b/scripts/check-local-export @@ -45,9 +45,9 @@ BEGIN { { symbol_types[$3]=3D$2 } =20 # append the exported symbol to the array -($3 ~ /^__ksymtab_/) { +($3 ~ /^__export_symbol.*\..*/) { export_symbols[i] =3D $3 - sub(/^__ksymtab_/, "", export_symbols[i]) + sub(/^__export_symbol.*\./, "", export_symbols[i]) i++ } =20 diff --git a/scripts/mod/modpost.c b/scripts/mod/modpost.c index 2c80da0220c3..13fff6e92aef 100644 --- a/scripts/mod/modpost.c +++ b/scripts/mod/modpost.c @@ -214,6 +214,7 @@ struct symbol { unsigned int crc; bool crc_valid; bool weak; + bool is_func; bool is_gpl_only; /* exported by EXPORT_SYMBOL_GPL */ char name[]; }; @@ -531,6 +532,8 @@ static int parse_elf(struct elf_info *info, const char = *filename) fatal("%s has NOBITS .modinfo\n", filename); info->modinfo =3D (void *)hdr + sechdrs[i].sh_offset; info->modinfo_len =3D sechdrs[i].sh_size; + } else if (!strcmp(secname, ".discard.export_symbol")) { + info->export_symbol_sec =3D i; } =20 if (sechdrs[i].sh_type =3D=3D SHT_SYMTAB) { @@ -653,17 +656,20 @@ static void handle_symbol(struct module *mod, struct = elf_info *info, ELF_ST_BIND(sym->st_info) =3D=3D STB_WEAK); break; default: - /* All exported symbols */ - if (strstarts(symname, "__ksymtab_")) { - const char *name, *secname; + if (sym->st_shndx =3D=3D info->export_symbol_sec) { + const char *name; =20 - name =3D symname + strlen("__ksymtab_"); - secname =3D sec_name(info, get_secindex(info, sym)); - - if (strstarts(secname, "___ksymtab_gpl+")) + if (strstarts(symname, "__export_symbol_gpl.")) { + name =3D symname + strlen("__export_symbol_gpl."); sym_add_exported(name, mod, true); - else if (strstarts(secname, "___ksymtab+")) + sym_update_namespace(name, sym_get_data(info, sym)); + } else if (strstarts(symname, "__export_symbol.")) { + name =3D symname + strlen("__export_symbol."); sym_add_exported(name, mod, false); + sym_update_namespace(name, sym_get_data(info, sym)); + } + + break; } if (strcmp(symname, "init_module") =3D=3D 0) mod->has_init =3D true; @@ -865,7 +871,6 @@ enum mismatch { XXXEXIT_TO_SOME_EXIT, ANY_INIT_TO_ANY_EXIT, ANY_EXIT_TO_ANY_INIT, - EXPORT_TO_INIT_EXIT, EXTABLE_TO_NON_TEXT, }; =20 @@ -960,12 +965,6 @@ static const struct sectioncheck sectioncheck[] =3D { .bad_tosec =3D { INIT_SECTIONS, NULL }, .mismatch =3D ANY_INIT_TO_ANY_EXIT, }, -/* Do not export init/exit functions or data */ -{ - .fromsec =3D { "___ksymtab*", NULL }, - .bad_tosec =3D { INIT_SECTIONS, EXIT_SECTIONS, NULL }, - .mismatch =3D EXPORT_TO_INIT_EXIT, -}, { .fromsec =3D { "__ex_table", NULL }, /* If you're adding any new black-listed sections in here, consider @@ -1256,10 +1255,6 @@ static void report_sec_mismatch(const char *modname, warn("%s: section mismatch in reference: %s (section: %s) -> %s (section= : %s)\n", modname, fromsym, fromsec, tosym, tosec); break; - case EXPORT_TO_INIT_EXIT: - warn("%s: EXPORT_SYMBOL used for init/exit symbol: %s (section: %s)\n", - modname, tosym, tosec); - break; case EXTABLE_TO_NON_TEXT: fatal("There's a special handler for this mismatch type, we should never= get here.\n"); break; @@ -1634,6 +1629,36 @@ static void section_rel(const char *modname, struct = elf_info *elf, } } =20 +static void check_export_symbols(struct module *mod, struct elf_info *elf) +{ + Elf_Sym *sym; + + for (sym =3D elf->symtab_start; sym < elf->symtab_stop; sym++) { + const char *symname =3D sym_name(elf, sym); + const char *secname; + struct symbol *s; + + s =3D sym_find_with_module(symname, mod); + if (!s) + continue; /* This is not an exported symbol */ + + /* + * We need to be aware whether we are exporting a function or + * a data on some architectures. + */ + s->is_func =3D (ELF_ST_TYPE(sym->st_info) =3D=3D STT_FUNC); + + secname =3D sec_name(elf, get_secindex(elf, sym)); + + if (match(secname, PATTERNS(INIT_SECTIONS))) + error("%s: EXPORT_SYMBOL used for init symbol. Remove __init or EXPORT_= SYMBOL.\n", + symname); + else if (match(secname, PATTERNS(EXIT_SECTIONS))) + error("%s: EXPORT_SYMBOL used for exit symbol. Remove __exit or EXPORT_= SYMBOL.\n", + symname); + } +} + /** * A module includes a number of sections that are discarded * either when loaded or when used as built-in. @@ -1820,16 +1845,8 @@ static void read_symbols(const char *modname) handle_moddevtable(mod, &info, sym, symname); } =20 - for (sym =3D info.symtab_start; sym < info.symtab_stop; sym++) { - symname =3D remove_dot(info.strtab + sym->st_name); - - /* Apply symbol namespaces from __kstrtabns_ entries. */ - if (strstarts(symname, "__kstrtabns_")) - sym_update_namespace(symname + strlen("__kstrtabns_"), - sym_get_data(&info, sym)); - } - check_sec_ref(modname, &info); + check_export_symbols(mod, &info); =20 if (!mod->is_vmlinux) { version =3D get_modinfo(&info, "version"); @@ -2015,6 +2032,14 @@ static void add_exported_symbols(struct buffer *buf,= struct module *mod) { struct symbol *sym; =20 + /* generate struct for exported symbols */ + buf_printf(buf, "\n"); + list_for_each_entry(sym, &mod->exported_symbols, list) + buf_printf(buf, "KSYMTAB_%s(%s, \"%s\", \"%s\");\n", + sym->is_func ? "FUNC" : "DATA", sym->name, + sym->is_gpl_only ? "_gpl" : "", + sym->namespace ?: ""); + if (!modversions) return; =20 diff --git a/scripts/mod/modpost.h b/scripts/mod/modpost.h index 1178f40a73f3..6f7a5c2d37b9 100644 --- a/scripts/mod/modpost.h +++ b/scripts/mod/modpost.h @@ -137,6 +137,7 @@ struct elf_info { Elf_Shdr *sechdrs; Elf_Sym *symtab_start; Elf_Sym *symtab_stop; + unsigned int export_symbol_sec; /* .discard.export_symbol section */ char *strtab; char *modinfo; unsigned int modinfo_len; --=20 2.34.1 From nobody Fri Sep 12 22:42:03 2025 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 4CBFEC07E9D for ; Mon, 26 Sep 2022 20:37:30 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229788AbiIZUh2 (ORCPT ); Mon, 26 Sep 2022 16:37:28 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:35284 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229871AbiIZUhV (ORCPT ); Mon, 26 Sep 2022 16:37:21 -0400 Received: from conuserg-10.nifty.com (conuserg-10.nifty.com [210.131.2.77]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 86FD3A1D1D; Mon, 26 Sep 2022 13:37:18 -0700 (PDT) Received: from zoe.. (133-32-182-133.west.xps.vectant.ne.jp [133.32.182.133]) (authenticated) by conuserg-10.nifty.com with ESMTP id 28QKaWu2018737; Tue, 27 Sep 2022 05:36:35 +0900 DKIM-Filter: OpenDKIM Filter v2.10.3 conuserg-10.nifty.com 28QKaWu2018737 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=nifty.com; s=dec2015msa; t=1664224596; bh=mbThpvrninpaCK/CTWbzGhqDF8eGttC+bocdQ0Ww/r4=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=lFnesgtsmjAQwbTE5KKkdDfIOBZ8mHhG6E3Quzirelhg8syO6B1g9hYQ5Ch8Vw6Qg /Kv2nfpcst5RadJWrNBxiSiuNvzV9+ZZPdAfjjt26bJv+88ZSCtImDSxXLNDKgwRMp uunhaM7AnHC6F4B7iDsBIGM/jZZo0U8FNx8vjV7OKgryDKf5IUa9r+w7fKmWcwEPdy PKOJEaQwPnfaZpcrvgyV9gKgc4cXvikLiMBH/p9Jg0GrwToMh9vJb3sMTEJdMqAjqk hmPySWCTWV7OyNImIwroDMC6UAEoaTYnCppiR3DM7PxjPiMwTP8xKrhU4BLsgkCbW/ 3mT0of71F6kEw== X-Nifty-SrcIP: [133.32.182.133] From: Masahiro Yamada To: linux-kbuild@vger.kernel.org Cc: linux-arch@vger.kernel.org, linux-kernel@vger.kernel.org, linux-ia64@vger.kernel.org, Al Viro , Ard Biesheuvel , Nicolas Pitre , Masahiro Yamada , Arnd Bergmann Subject: [PATCH v2 2/7] ia64,export.h: replace EXPORT_DATA_SYMBOL* with EXPORT_SYMBOL* Date: Tue, 27 Sep 2022 05:36:20 +0900 Message-Id: <20220926203625.1117261-3-masahiroy@kernel.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20220926203625.1117261-1-masahiroy@kernel.org> References: <20220926203625.1117261-1-masahiroy@kernel.org> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" With the previous refactoring, you can always use EXPORT_SYMBOL*. Replace two instances in ia64, then remove EXPORT_DATA_SYMBOL*. Signed-off-by: Masahiro Yamada --- (no changes since v1) arch/ia64/kernel/head.S | 2 +- arch/ia64/kernel/ivt.S | 2 +- include/asm-generic/export.h | 3 --- 3 files changed, 2 insertions(+), 5 deletions(-) diff --git a/arch/ia64/kernel/head.S b/arch/ia64/kernel/head.S index f22469f1c1fc..c096500590e9 100644 --- a/arch/ia64/kernel/head.S +++ b/arch/ia64/kernel/head.S @@ -170,7 +170,7 @@ RestRR: \ __PAGE_ALIGNED_DATA =20 .global empty_zero_page -EXPORT_DATA_SYMBOL_GPL(empty_zero_page) +EXPORT_SYMBOL_GPL(empty_zero_page) empty_zero_page: .skip PAGE_SIZE =20 diff --git a/arch/ia64/kernel/ivt.S b/arch/ia64/kernel/ivt.S index d6d4229b28db..7a418e324d30 100644 --- a/arch/ia64/kernel/ivt.S +++ b/arch/ia64/kernel/ivt.S @@ -87,7 +87,7 @@ =20 .align 32768 // align on 32KB boundary .global ia64_ivt - EXPORT_DATA_SYMBOL(ia64_ivt) + EXPORT_SYMBOL(ia64_ivt) ia64_ivt: //////////////////////////////////////////////////////////////////////////= /////////////// // 0x0000 Entry 0 (size 64 bundles) VHPT Translation (8,20,47) diff --git a/include/asm-generic/export.h b/include/asm-generic/export.h index 0ae9f38a904c..570cd4da7210 100644 --- a/include/asm-generic/export.h +++ b/include/asm-generic/export.h @@ -8,7 +8,4 @@ */ #include =20 -#define EXPORT_DATA_SYMBOL(name) EXPORT_SYMBOL(name) -#define EXPORT_DATA_SYMBOL_GPL(name) EXPORT_SYMBOL_GPL(name) - #endif --=20 2.34.1 From nobody Fri Sep 12 22:42:03 2025 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 24624C07E9D for ; Mon, 26 Sep 2022 20:37:59 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230128AbiIZUh5 (ORCPT ); Mon, 26 Sep 2022 16:37:57 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:35792 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229913AbiIZUht (ORCPT ); Mon, 26 Sep 2022 16:37:49 -0400 Received: from conuserg-10.nifty.com (conuserg-10.nifty.com [210.131.2.77]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id BB1838F96B; Mon, 26 Sep 2022 13:37:46 -0700 (PDT) Received: from zoe.. (133-32-182-133.west.xps.vectant.ne.jp [133.32.182.133]) (authenticated) by conuserg-10.nifty.com with ESMTP id 28QKaWu3018737; Tue, 27 Sep 2022 05:36:36 +0900 DKIM-Filter: OpenDKIM Filter v2.10.3 conuserg-10.nifty.com 28QKaWu3018737 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=nifty.com; s=dec2015msa; t=1664224597; bh=YuiSaa5TunTalSUSMGyvPMuKWQEyEfJw4GcU2AlJPTE=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=nplNeLG6r/TpbfWLXiMv77IUCbyTNDsDqyirwRmTRUCHR7Glfkfy2jbS96tJNbFrz ncZbBEK73Svvmeb0S5afAvUgE4SSLoMbroA7o8t3LjfIrWWk3iRxBLpeuZ09ab2I0D udqVkrXtyIbPQgb9rIFELmOleazKYRE8+oNB4MlyYJD+/J2rhC2qy0dFn8kAuwaS6A nR4GW0LmJIVb/wVPcFRJ9dtQb/RPxAaucfLfEo6YJLrLrtWBibP09Er0eqPcjqX3g1 4pLjqj3bgECyOo7RDK9vfStjDSzOK22hQvetXBO+g/CI+fT/NZer07cfubsvhlDZWr VjkbNkxELX+3g== X-Nifty-SrcIP: [133.32.182.133] From: Masahiro Yamada To: linux-kbuild@vger.kernel.org Cc: linux-arch@vger.kernel.org, linux-kernel@vger.kernel.org, linux-ia64@vger.kernel.org, Al Viro , Ard Biesheuvel , Nicolas Pitre , Masahiro Yamada , Nick Desaulniers , Michal Marek Subject: [PATCH v2 3/7] modpost: merge sym_update_namespace() into sym_add_exported() Date: Tue, 27 Sep 2022 05:36:21 +0900 Message-Id: <20220926203625.1117261-4-masahiroy@kernel.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20220926203625.1117261-1-masahiroy@kernel.org> References: <20220926203625.1117261-1-masahiroy@kernel.org> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" Pass a set of the name, license, and namespace to sym_add_exported(). sym_update_namespace() is unneeded. Signed-off-by: Masahiro Yamada Reviewed-by: Nick Desaulniers --- (no changes since v1) scripts/mod/modpost.c | 41 +++++++++-------------------------------- 1 file changed, 9 insertions(+), 32 deletions(-) diff --git a/scripts/mod/modpost.c b/scripts/mod/modpost.c index 13fff6e92aef..0bb5bbd176b4 100644 --- a/scripts/mod/modpost.c +++ b/scripts/mod/modpost.c @@ -352,26 +352,8 @@ static const char *sec_name(const struct elf_info *inf= o, unsigned int secindex) =20 #define strstarts(str, prefix) (strncmp(str, prefix, strlen(prefix)) =3D= =3D 0) =20 -static void sym_update_namespace(const char *symname, const char *namespac= e) -{ - struct symbol *s =3D find_symbol(symname); - - /* - * That symbol should have been created earlier and thus this is - * actually an assertion. - */ - if (!s) { - error("Could not update namespace(%s) for symbol %s\n", - namespace, symname); - return; - } - - free(s->namespace); - s->namespace =3D namespace[0] ? NOFAIL(strdup(namespace)) : NULL; -} - static struct symbol *sym_add_exported(const char *name, struct module *mo= d, - bool gpl_only) + bool gpl_only, const char *namespace) { struct symbol *s =3D find_symbol(name); =20 @@ -384,6 +366,7 @@ static struct symbol *sym_add_exported(const char *name= , struct module *mod, s =3D alloc_symbol(name); s->module =3D mod; s->is_gpl_only =3D gpl_only; + s->namespace =3D namespace[0] ? NOFAIL(strdup(namespace)) : NULL; list_add_tail(&s->list, &mod->exported_symbols); hash_add_symbol(s); =20 @@ -657,17 +640,12 @@ static void handle_symbol(struct module *mod, struct = elf_info *info, break; default: if (sym->st_shndx =3D=3D info->export_symbol_sec) { - const char *name; - - if (strstarts(symname, "__export_symbol_gpl.")) { - name =3D symname + strlen("__export_symbol_gpl."); - sym_add_exported(name, mod, true); - sym_update_namespace(name, sym_get_data(info, sym)); - } else if (strstarts(symname, "__export_symbol.")) { - name =3D symname + strlen("__export_symbol."); - sym_add_exported(name, mod, false); - sym_update_namespace(name, sym_get_data(info, sym)); - } + if (strstarts(symname, "__export_symbol_gpl.")) + sym_add_exported(symname + strlen("__export_symbol_gpl."), + mod, true, sym_get_data(info, sym)); + else if (strstarts(symname, "__export_symbol.")) + sym_add_exported(symname + strlen("__export_symbol."), + mod, false, sym_get_data(info, sym)); =20 break; } @@ -2283,9 +2261,8 @@ static void read_dump(const char *fname) mod =3D new_module(modname, strlen(modname)); mod->from_dump =3D true; } - s =3D sym_add_exported(symname, mod, gpl_only); + s =3D sym_add_exported(symname, mod, gpl_only, namespace); sym_set_crc(s, crc); - sym_update_namespace(symname, namespace); } free(buf); return; --=20 2.34.1 From nobody Fri Sep 12 22:42:03 2025 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id CDB3AC07E9D for ; Mon, 26 Sep 2022 20:41:00 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230496AbiIZUk6 (ORCPT ); Mon, 26 Sep 2022 16:40:58 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:37870 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230429AbiIZUkc (ORCPT ); Mon, 26 Sep 2022 16:40:32 -0400 Received: from condef-05.nifty.com (condef-05.nifty.com [202.248.20.70]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 44BC07C755; Mon, 26 Sep 2022 13:40:15 -0700 (PDT) Received: from conuserg-10.nifty.com ([10.126.8.73])by condef-05.nifty.com with ESMTP id 28QKbjxE029749; Tue, 27 Sep 2022 05:37:45 +0900 Received: from zoe.. (133-32-182-133.west.xps.vectant.ne.jp [133.32.182.133]) (authenticated) by conuserg-10.nifty.com with ESMTP id 28QKaWu4018737; Tue, 27 Sep 2022 05:36:37 +0900 DKIM-Filter: OpenDKIM Filter v2.10.3 conuserg-10.nifty.com 28QKaWu4018737 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=nifty.com; s=dec2015msa; t=1664224598; bh=OfuGM282m19iNX0i9PBWYpUdIc6yc2RZLrtabmbI8uk=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=A48lnOjslGssnRTkBNZAS/jOg1yyoExtR8J6J98kYhDc/3Hxt3agsC0V2RpL8EOKE nK0g02ZftZq4njEU1acRjmpXyUTYzrofbyDc9eOj+A6Br5clcX+idZJhoFei2Gw/TF AG/tEzLm61kJSuxUgI9HZrftvcy7m473r7LGgO6phczEG5yV1wRvf70J3rlOIDRCES j5R47wRdK7tT7seyr/qX7qM38GymLrZw7ChG8rkexHsBnYNjGDUx7AqJtAVMmyIp6h 3qAXbSklItGOecxTqfKF+jGLR6EkOTT5OWBUQdRIeoN8tq7dASssxcbRDLIg1lzZb7 J7eE7tbZtZ9ow== X-Nifty-SrcIP: [133.32.182.133] From: Masahiro Yamada To: linux-kbuild@vger.kernel.org Cc: linux-arch@vger.kernel.org, linux-kernel@vger.kernel.org, linux-ia64@vger.kernel.org, Al Viro , Ard Biesheuvel , Nicolas Pitre , Masahiro Yamada , Nick Desaulniers , Michal Marek Subject: [PATCH v2 4/7] modpost: use null string instead of NULL pointer for default namespace Date: Tue, 27 Sep 2022 05:36:22 +0900 Message-Id: <20220926203625.1117261-5-masahiroy@kernel.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20220926203625.1117261-1-masahiroy@kernel.org> References: <20220926203625.1117261-1-masahiroy@kernel.org> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" The default namespace is the null string, "". When set, the null string "" is converted to NULL: s->namespace =3D namespace[0] ? NOFAIL(strdup(namespace)) : NULL; When printed, the NULL pointer is get back to the null string: sym->namespace ?: "" This saves 1 byte memory allocated for "", but loses the readability. In kernel-space, we strive to save memory, but modpost is a userspace tool used to build the kernel. On modern systems, such small piece of memory is not a big deal. Handle the namespace string as is. Signed-off-by: Masahiro Yamada Reviewed-by: Nick Desaulniers --- (no changes since v1) scripts/mod/modpost.c | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/scripts/mod/modpost.c b/scripts/mod/modpost.c index 0bb5bbd176b4..29f30558a398 100644 --- a/scripts/mod/modpost.c +++ b/scripts/mod/modpost.c @@ -297,6 +297,13 @@ static bool contains_namespace(struct list_head *head,= const char *namespace) { struct namespace_list *list; =20 + /* + * The default namespace is null string "", which is always implicitly + * contained. + */ + if (!namespace[0]) + return true; + list_for_each_entry(list, head, list) { if (!strcmp(list->namespace, namespace)) return true; @@ -366,7 +373,7 @@ static struct symbol *sym_add_exported(const char *name= , struct module *mod, s =3D alloc_symbol(name); s->module =3D mod; s->is_gpl_only =3D gpl_only; - s->namespace =3D namespace[0] ? NOFAIL(strdup(namespace)) : NULL; + s->namespace =3D NOFAIL(strdup(namespace)); list_add_tail(&s->list, &mod->exported_symbols); hash_add_symbol(s); =20 @@ -1928,8 +1935,7 @@ static void check_exports(struct module *mod) else basename =3D mod->name; =20 - if (exp->namespace && - !contains_namespace(&mod->imported_namespaces, exp->namespace)) { + if (!contains_namespace(&mod->imported_namespaces, exp->namespace)) { modpost_log(allow_missing_ns_imports ? LOG_WARN : LOG_ERROR, "module %s uses symbol %s from namespace %s, but does not import i= t.\n", basename, exp->name, exp->namespace); @@ -2015,8 +2021,7 @@ static void add_exported_symbols(struct buffer *buf, = struct module *mod) list_for_each_entry(sym, &mod->exported_symbols, list) buf_printf(buf, "KSYMTAB_%s(%s, \"%s\", \"%s\");\n", sym->is_func ? "FUNC" : "DATA", sym->name, - sym->is_gpl_only ? "_gpl" : "", - sym->namespace ?: ""); + sym->is_gpl_only ? "_gpl" : "", sym->namespace); =20 if (!modversions) return; @@ -2284,7 +2289,7 @@ static void write_dump(const char *fname) buf_printf(&buf, "0x%08x\t%s\t%s\tEXPORT_SYMBOL%s\t%s\n", sym->crc, sym->name, mod->name, sym->is_gpl_only ? "_GPL" : "", - sym->namespace ?: ""); + sym->namespace); } } write_buf(&buf, fname); --=20 2.34.1 From nobody Fri Sep 12 22:42:03 2025 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 259CCC07E9D for ; Mon, 26 Sep 2022 20:42:36 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231254AbiIZUmd (ORCPT ); Mon, 26 Sep 2022 16:42:33 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:47784 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230517AbiIZUmS (ORCPT ); Mon, 26 Sep 2022 16:42:18 -0400 Received: from condef-02.nifty.com (condef-02.nifty.com [202.248.20.67]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 1EBF3ABD4A; Mon, 26 Sep 2022 13:42:06 -0700 (PDT) Received: from conuserg-10.nifty.com ([10.126.8.73])by condef-02.nifty.com with ESMTP id 28QKblcg003725; Tue, 27 Sep 2022 05:37:47 +0900 Received: from zoe.. (133-32-182-133.west.xps.vectant.ne.jp [133.32.182.133]) (authenticated) by conuserg-10.nifty.com with ESMTP id 28QKaWu5018737; Tue, 27 Sep 2022 05:36:39 +0900 DKIM-Filter: OpenDKIM Filter v2.10.3 conuserg-10.nifty.com 28QKaWu5018737 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=nifty.com; s=dec2015msa; t=1664224599; bh=pj6Bhh62wjJj180r/iVpfwJ9jYP/0YH+qO8nbtvL0F4=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=BCUzrt/fwqCoSL8FG+jV1kx/mVFMpWkKEC2viT5kZTVOD6PAV1I15YPYypC6R3FGM sve3sF+F8hMIsThRuPKct3CGE6JTvz2yRIjsGC78E/+Sgjqa0SDRK37MQHsU4JTZji pwg6sKCLcav29sbWnbnkMV1Ifh5DN/5ZGcxhwE1cvBvkPfRHw5IHoYsNrvj9jTIV7K kqSL0aFzJ9fdA5msHs6shjCLUXi6YTD43rsT3tk3TciX+WJRnutQyRWoeSxDuNNYAt iUII3+92LGNrDHXaa17VEaALlLDD78+6MgUAqCaJSgJyppW5HXys5kSkGtwXuMUAm+ C1Y0Xh4nFg4Rw== X-Nifty-SrcIP: [133.32.182.133] From: Masahiro Yamada To: linux-kbuild@vger.kernel.org Cc: linux-arch@vger.kernel.org, linux-kernel@vger.kernel.org, linux-ia64@vger.kernel.org, Al Viro , Ard Biesheuvel , Nicolas Pitre , Masahiro Yamada , Michal Marek , Nick Desaulniers Subject: [PATCH v2 5/7] modpost: squash report_sec_mismatch() and remove enum mismatch Date: Tue, 27 Sep 2022 05:36:23 +0900 Message-Id: <20220926203625.1117261-6-masahiroy@kernel.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20220926203625.1117261-1-masahiroy@kernel.org> References: <20220926203625.1117261-1-masahiroy@kernel.org> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" Now report_sec_mismatch() prints the same warning message for all possible cases. (fatal() is just a sanity check for unreachable code.) Squash it into default_mismatch_handler(). enum mismatch is no longer used. Remove it as well. Signed-off-by: Masahiro Yamada --- Changes in v2: - New patch scripts/mod/modpost.c | 61 +++---------------------------------------- 1 file changed, 4 insertions(+), 57 deletions(-) diff --git a/scripts/mod/modpost.c b/scripts/mod/modpost.c index 29f30558a398..90733664a602 100644 --- a/scripts/mod/modpost.c +++ b/scripts/mod/modpost.c @@ -847,18 +847,6 @@ static const char *const linker_symbols[] =3D { "__init_begin", "_sinittext", "_einittext", NULL }; static const char *const optim_symbols[] =3D { "*.constprop.*", NULL }; =20 -enum mismatch { - TEXT_TO_ANY_INIT, - DATA_TO_ANY_INIT, - TEXT_TO_ANY_EXIT, - DATA_TO_ANY_EXIT, - XXXINIT_TO_SOME_INIT, - XXXEXIT_TO_SOME_EXIT, - ANY_INIT_TO_ANY_EXIT, - ANY_EXIT_TO_ANY_INIT, - EXTABLE_TO_NON_TEXT, -}; - /** * Describe how to match sections on different criteria: * @@ -880,7 +868,6 @@ struct sectioncheck { const char *fromsec[20]; const char *bad_tosec[20]; const char *good_tosec[20]; - enum mismatch mismatch; void (*handler)(const char *modname, struct elf_info *elf, const struct sectioncheck* const mismatch, Elf_Rela *r, Elf_Sym *sym, const char *fromsec); @@ -899,56 +886,46 @@ static const struct sectioncheck sectioncheck[] =3D { { .fromsec =3D { TEXT_SECTIONS, NULL }, .bad_tosec =3D { ALL_INIT_SECTIONS, NULL }, - .mismatch =3D TEXT_TO_ANY_INIT, }, { .fromsec =3D { DATA_SECTIONS, NULL }, .bad_tosec =3D { ALL_XXXINIT_SECTIONS, NULL }, - .mismatch =3D DATA_TO_ANY_INIT, }, { .fromsec =3D { DATA_SECTIONS, NULL }, .bad_tosec =3D { INIT_SECTIONS, NULL }, - .mismatch =3D DATA_TO_ANY_INIT, }, { .fromsec =3D { TEXT_SECTIONS, NULL }, .bad_tosec =3D { ALL_EXIT_SECTIONS, NULL }, - .mismatch =3D TEXT_TO_ANY_EXIT, }, { .fromsec =3D { DATA_SECTIONS, NULL }, .bad_tosec =3D { ALL_EXIT_SECTIONS, NULL }, - .mismatch =3D DATA_TO_ANY_EXIT, }, /* Do not reference init code/data from meminit code/data */ { .fromsec =3D { ALL_XXXINIT_SECTIONS, NULL }, .bad_tosec =3D { INIT_SECTIONS, NULL }, - .mismatch =3D XXXINIT_TO_SOME_INIT, }, /* Do not reference exit code/data from memexit code/data */ { .fromsec =3D { ALL_XXXEXIT_SECTIONS, NULL }, .bad_tosec =3D { EXIT_SECTIONS, NULL }, - .mismatch =3D XXXEXIT_TO_SOME_EXIT, }, /* Do not use exit code/data from init code */ { .fromsec =3D { ALL_INIT_SECTIONS, NULL }, .bad_tosec =3D { ALL_EXIT_SECTIONS, NULL }, - .mismatch =3D ANY_INIT_TO_ANY_EXIT, }, /* Do not use init code/data from exit code */ { .fromsec =3D { ALL_EXIT_SECTIONS, NULL }, .bad_tosec =3D { ALL_INIT_SECTIONS, NULL }, - .mismatch =3D ANY_EXIT_TO_ANY_INIT, }, { .fromsec =3D { ALL_PCI_INIT_SECTIONS, NULL }, .bad_tosec =3D { INIT_SECTIONS, NULL }, - .mismatch =3D ANY_INIT_TO_ANY_EXIT, }, { .fromsec =3D { "__ex_table", NULL }, @@ -957,7 +934,6 @@ static const struct sectioncheck sectioncheck[] =3D { */ .bad_tosec =3D { ".altinstr_replacement", NULL }, .good_tosec =3D {ALL_TEXT_SECTIONS , NULL}, - .mismatch =3D EXTABLE_TO_NON_TEXT, .handler =3D extable_mismatch_handler, } }; @@ -1215,37 +1191,6 @@ static inline void get_pretty_name(int is_func, cons= t char** name, const char** } } =20 -/* - * Print a warning about a section mismatch. - * Try to find symbols near it so user can find it. - * Check whitelist before warning - it may be a false positive. - */ -static void report_sec_mismatch(const char *modname, - const struct sectioncheck *mismatch, - const char *fromsec, - const char *fromsym, - const char *tosec, const char *tosym) -{ - sec_mismatch_count++; - - switch (mismatch->mismatch) { - case TEXT_TO_ANY_INIT: - case DATA_TO_ANY_INIT: - case TEXT_TO_ANY_EXIT: - case DATA_TO_ANY_EXIT: - case XXXINIT_TO_SOME_INIT: - case XXXEXIT_TO_SOME_EXIT: - case ANY_INIT_TO_ANY_EXIT: - case ANY_EXIT_TO_ANY_INIT: - warn("%s: section mismatch in reference: %s (section: %s) -> %s (section= : %s)\n", - modname, fromsym, fromsec, tosym, tosec); - break; - case EXTABLE_TO_NON_TEXT: - fatal("There's a special handler for this mismatch type, we should never= get here.\n"); - break; - } -} - static void default_mismatch_handler(const char *modname, struct elf_info = *elf, const struct sectioncheck* const mismatch, Elf_Rela *r, Elf_Sym *sym, const char *fromsec) @@ -1266,8 +1211,10 @@ static void default_mismatch_handler(const char *mod= name, struct elf_info *elf, /* check whitelist - we may ignore it */ if (secref_whitelist(mismatch, fromsec, fromsym, tosec, tosym)) { - report_sec_mismatch(modname, mismatch, - fromsec, fromsym, tosec, tosym); + sec_mismatch_count++; + + warn("%s: section mismatch in reference: %s (section: %s) -> %s (section= : %s)\n", + modname, fromsym, fromsec, tosym, tosec); } } =20 --=20 2.34.1 From nobody Fri Sep 12 22:42:03 2025 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 0A34FC32771 for ; Mon, 26 Sep 2022 20:40:37 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230513AbiIZUke (ORCPT ); Mon, 26 Sep 2022 16:40:34 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:42756 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230476AbiIZUkY (ORCPT ); Mon, 26 Sep 2022 16:40:24 -0400 X-Greylist: delayed 78 seconds by postgrey-1.37 at lindbergh.monkeyblade.net; Mon, 26 Sep 2022 13:40:15 PDT Received: from condef-09.nifty.com (condef-09.nifty.com [202.248.20.74]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 86378A6ADE; Mon, 26 Sep 2022 13:40:14 -0700 (PDT) Received: from conuserg-10.nifty.com ([10.126.8.73])by condef-09.nifty.com with ESMTP id 28QKbcA2013809; Tue, 27 Sep 2022 05:37:57 +0900 Received: from zoe.. (133-32-182-133.west.xps.vectant.ne.jp [133.32.182.133]) (authenticated) by conuserg-10.nifty.com with ESMTP id 28QKaWu6018737; Tue, 27 Sep 2022 05:36:40 +0900 DKIM-Filter: OpenDKIM Filter v2.10.3 conuserg-10.nifty.com 28QKaWu6018737 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=nifty.com; s=dec2015msa; t=1664224601; bh=P5dF9TpsmnzehTX7jhWq4e/43M6Ionp+mydBWcrgrQM=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=MGuiHbfsWYLsm4kJ6WX18v6LajU3d2arhUIUArFNS+lG2D3X5Lwkvf9ZmRkfJRTyk Wr9Ur4Cuj15xKeLftoWhZ2uBF3bytuqxEjZQrKyzw7P+ktD6lERV37rsaizqmL9A9G SPRqtwDeKsp7jkAGekcz9TqNfD1+0oy9i1kJmMAOcmsDDnK7hRI2KjkJS/Lyo6RRv+ k5e2GKc6uymSR+3HDpWxWIPgI+9ZGTndGu+hCAs/Wl2iJi99ggy8GkrueS3EECcaBl lTDevZ+lHoZOTt36A5O6mAmB9meATYB2r7O9nbW308977LJswYKK0cjzNHxSSC4zf/ TyXeUy2oNBTAw== X-Nifty-SrcIP: [133.32.182.133] From: Masahiro Yamada To: linux-kbuild@vger.kernel.org Cc: linux-arch@vger.kernel.org, linux-kernel@vger.kernel.org, linux-ia64@vger.kernel.org, Al Viro , Ard Biesheuvel , Nicolas Pitre , Masahiro Yamada , Michal Marek , Nathan Chancellor , Nick Desaulniers , Tom Rix , llvm@lists.linux.dev Subject: [PATCH v2 6/7] kbuild: implement CONFIG_TRIM_UNUSED_KSYMS without recursion Date: Tue, 27 Sep 2022 05:36:24 +0900 Message-Id: <20220926203625.1117261-7-masahiroy@kernel.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20220926203625.1117261-1-masahiroy@kernel.org> References: <20220926203625.1117261-1-masahiroy@kernel.org> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" When CONFIG_TRIM_UNUSED_KSYMS is enabled, Kbuild recursively traverses the directory tree to determine which EXPORT_SYMBOL to trim. If an EXPORT_SYMBOL turns out to be unused by anyone, Kbuild begins the second traverse, where some source files are recompiled with their EXPORT_SYMBOL() tuned into a no-op. Linus stated a negative opinion about this slowness in commits: - 5cf0fd591f2e ("Kbuild: disable TRIM_UNUSED_KSYMS option") - a555bdd0c58c ("Kbuild: enable TRIM_UNUSED_KSYMS again, with some guardin= g") We can do this better now. The final data structures of EXPORT_SYMBOL are generated by the modpost stage, so modpost can selectively emit KSYMTAB entries that are really referenced by modules. modpost needs to know all defined symbols in all modules (that is, EXPORT_SYMBOL's that have users). Commit 8da79d44fd2e ("kbuild: unify two modpost invocations") is a ground-work to do this in one-pass. Nicolas explained why the trimming was implemented with recursion: https://lore.kernel.org/all/2o2rpn97-79nq-p7s2-nq5-8p83391473r@syhkavp.ar= g/ Actually, we never achieved that level of optimization where the chain reaction of trimming comes into play because: - CONFIG_LTO_CLANG cannot remove any unused symbols - CONFIG_LD_DEAD_CODE_DATA_ELIMINATION is enabled only for vmlinux, but not modules If deeper trimming is required, we need to revisit this, but that is unlikely to happen. Signed-off-by: Masahiro Yamada --- Changes in v2: - New patch .gitignore | 1 - Makefile | 24 ++++-------- include/linux/export.h | 41 --------------------- scripts/Makefile.build | 7 ---- scripts/Makefile.modpost | 8 +++- scripts/adjust_autoksyms.sh | 73 ------------------------------------- scripts/basic/fixdep.c | 3 +- scripts/gen_autoksyms.sh | 62 ------------------------------- scripts/gen_ksymdeps.sh | 30 --------------- scripts/link-vmlinux.sh | 5 ++- scripts/mod/modpost.c | 54 ++++++++++++++++++++++++--- scripts/remove-stale-files | 2 + 12 files changed, 70 insertions(+), 240 deletions(-) delete mode 100755 scripts/adjust_autoksyms.sh delete mode 100755 scripts/gen_autoksyms.sh delete mode 100755 scripts/gen_ksymdeps.sh diff --git a/.gitignore b/.gitignore index 265959544978..a9b44cd36066 100644 --- a/.gitignore +++ b/.gitignore @@ -103,7 +103,6 @@ modules.order # /include/config/ /include/generated/ -/include/ksym/ /arch/*/include/generated/ =20 # stgit generated dirs diff --git a/Makefile b/Makefile index 2b4980490ecb..36ae888c37b3 100644 --- a/Makefile +++ b/Makefile @@ -1120,28 +1120,13 @@ endif export KBUILD_VMLINUX_LIBS export KBUILD_LDS :=3D arch/$(SRCARCH)/kernel/vmlinux.lds =20 -# Recurse until adjust_autoksyms.sh is satisfied -PHONY +=3D autoksyms_recursive ifdef CONFIG_TRIM_UNUSED_KSYMS # For the kernel to actually contain only the needed exported symbols, # we have to build modules as well to determine what those symbols are. # (this can be evaluated only once include/config/auto.conf has been inclu= ded) KBUILD_MODULES :=3D 1 - -autoksyms_recursive: $(build-dir) modules.order - $(Q)$(CONFIG_SHELL) $(srctree)/scripts/adjust_autoksyms.sh \ - "$(MAKE) -f $(srctree)/Makefile autoksyms_recursive" endif =20 -autoksyms_h :=3D $(if $(CONFIG_TRIM_UNUSED_KSYMS), include/generated/autok= syms.h) - -quiet_cmd_autoksyms_h =3D GEN $@ - cmd_autoksyms_h =3D mkdir -p $(dir $@); \ - $(CONFIG_SHELL) $(srctree)/scripts/gen_autoksyms.sh $@ - -$(autoksyms_h): - $(call cmd,autoksyms_h) - # '$(AR) mPi' needs 'T' to workaround the bug of llvm-ar <=3D 14 quiet_cmd_ar_vmlinux.a =3D AR $@ cmd_ar_vmlinux.a =3D \ @@ -1153,7 +1138,7 @@ targets +=3D vmlinux.a vmlinux.a: $(KBUILD_VMLINUX_OBJS) scripts/head-object-list.txt FORCE $(call if_changed,ar_vmlinux.a) =20 -vmlinux.o: autoksyms_recursive vmlinux.a $(KBUILD_VMLINUX_LIBS) FORCE +vmlinux.o: vmlinux.a $(KBUILD_VMLINUX_LIBS) FORCE $(Q)$(MAKE) -f $(srctree)/scripts/Makefile.vmlinux_o =20 ARCH_POSTLINK :=3D $(wildcard $(srctree)/arch/$(SRCARCH)/Makefile.postlink) @@ -1168,6 +1153,11 @@ vmlinux: scripts/link-vmlinux.sh vmlinux.o $(KBUILD_= LDS) modpost FORCE =20 targets +=3D vmlinux =20 +# When .vmlinux.export.c is updated, vmlinux must be relinked. +# (REVISIT for refactoring) +.vmlinux.export.c: modpost + @: + # The actual objects are generated when descending, # make sure no implicit rule kicks in $(sort $(KBUILD_LDS) $(KBUILD_VMLINUX_OBJS) $(KBUILD_VMLINUX_LIBS)): . ; @@ -1195,7 +1185,7 @@ scripts: scripts_basic scripts_dtc PHONY +=3D prepare archprepare =20 archprepare: outputmakefile archheaders archscripts scripts include/config= /kernel.release \ - asm-generic $(version_h) $(autoksyms_h) include/generated/utsrelease.h \ + asm-generic $(version_h) include/generated/utsrelease.h \ include/generated/compile.h include/generated/autoconf.h remove-stale-fil= es =20 prepare0: archprepare diff --git a/include/linux/export.h b/include/linux/export.h index 0c71577cf8bb..d9fb1ebb535c 100644 --- a/include/linux/export.h +++ b/include/linux/export.h @@ -63,47 +63,6 @@ extern struct module __this_module; */ #define __EXPORT_SYMBOL(sym, sec, ns) =20 -#elif defined(CONFIG_TRIM_UNUSED_KSYMS) - -#include - -/* - * For fine grained build dependencies, we want to tell the build system - * about each possible exported symbol even if they're not actually export= ed. - * We use a symbol pattern __ksym_marker_ that the build system fi= lters - * from the $(NM) output (see scripts/gen_ksymdeps.sh). These symbols are - * discarded in the final link stage. - */ - -#ifdef __ASSEMBLY__ - -#define __ksym_marker(sym) \ - .section ".discard.ksym","a" ; \ -__ksym_marker_##sym: ; \ - .previous - -#else - -#define __ksym_marker(sym) \ - static int __ksym_marker_##sym[0] __section(".discard.ksym") __used - -#endif - -#define __EXPORT_SYMBOL(sym, sec, ns) \ - __ksym_marker(sym); \ - __cond_export_sym(sym, sec, ns, __is_defined(__KSYM_##sym)) -#define __cond_export_sym(sym, sec, ns, conf) \ - ___cond_export_sym(sym, sec, ns, conf) -#define ___cond_export_sym(sym, sec, ns, enabled) \ - __cond_export_sym_##enabled(sym, sec, ns) -#define __cond_export_sym_1(sym, sec, ns) ___EXPORT_SYMBOL(sym, sec, ns) - -#ifdef __GENKSYMS__ -#define __cond_export_sym_0(sym, sec, ns) __GENKSYMS_EXPORT_SYMBOL(sym) -#else -#define __cond_export_sym_0(sym, sec, ns) /* nothing */ -#endif - #else =20 #define __EXPORT_SYMBOL(sym, sec, ns) ___EXPORT_SYMBOL(sym, sec, ns) diff --git a/scripts/Makefile.build b/scripts/Makefile.build index 4c1d0bd1bc03..64f652b03d29 100644 --- a/scripts/Makefile.build +++ b/scripts/Makefile.build @@ -218,16 +218,10 @@ is-standard-object =3D $(if $(filter-out y%, $(OBJECT= _FILES_NON_STANDARD_$(basetar =20 $(obj)/%.o: objtool-enabled =3D $(if $(is-standard-object),$(if $(delay-ob= jtool),$(is-single-obj-m),y)) =20 -ifdef CONFIG_TRIM_UNUSED_KSYMS -cmd_gen_ksymdeps =3D \ - $(CONFIG_SHELL) $(srctree)/scripts/gen_ksymdeps.sh $@ >> $(dot-target).cmd -endif - cmd_check_local_export =3D $(srctree)/scripts/check-local-export $@ =20 define rule_cc_o_c $(call cmd_and_fixdep,cc_o_c) - $(call cmd,gen_ksymdeps) $(call cmd,check_local_export) $(call cmd,checksrc) $(call cmd,checkdoc) @@ -238,7 +232,6 @@ endef =20 define rule_as_o_S $(call cmd_and_fixdep,as_o_S) - $(call cmd,gen_ksymdeps) $(call cmd,check_local_export) $(call cmd,gen_objtooldep) $(call cmd,gen_symversions_S) diff --git a/scripts/Makefile.modpost b/scripts/Makefile.modpost index 7740ce3b29e8..555b9e741a01 100644 --- a/scripts/Makefile.modpost +++ b/scripts/Makefile.modpost @@ -86,6 +86,12 @@ output-symdump :=3D $(if $(vmlinux.o-if-present), Module= .symvers, modules-only.sym missing-input :=3D $(filter-out $(vmlinux.o-if-present),vmlinux.o) endif =20 +ifdef CONFIG_TRIM_UNUSED_KSYMS +ksym-wl :=3D $(CONFIG_UNUSED_KSYMS_WHITELIST) +ksym-wl :=3D $(if $(filter-out /%, $(ksym-wl)),$(srctree)/)$(ksym-wl) +modpost-args +=3D -t $(addprefix -W, $(ksym-wl)) +endif + else =20 # set src + obj - they may be used in the modules's Makefile @@ -122,7 +128,7 @@ quiet_cmd_modpost =3D MODPOST $@ sed 's/ko$$/o/' $(or $(modorder-if-needed), /dev/null) | $(MODPOST) $(mod= post-args) $(vmlinux.o-if-present) -T - =20 targets +=3D $(output-symdump) -$(output-symdump): $(modorder-if-needed) $(vmlinux.o-if-present) $(moudle.= symvers-if-present) $(MODPOST) FORCE +$(output-symdump): $(modorder-if-needed) $(vmlinux.o-if-present) $(moudle.= symvers-if-present) $(MODPOST) $(ksym-wl) FORCE $(call if_changed,modpost) =20 __modpost: $(output-symdump) diff --git a/scripts/adjust_autoksyms.sh b/scripts/adjust_autoksyms.sh deleted file mode 100755 index f1b5ac818411..000000000000 --- a/scripts/adjust_autoksyms.sh +++ /dev/null @@ -1,73 +0,0 @@ -#!/bin/sh -# SPDX-License-Identifier: GPL-2.0-only - -# Script to update include/generated/autoksyms.h and dependency files -# -# Copyright: (C) 2016 Linaro Limited -# Created by: Nicolas Pitre, January 2016 -# - -# Update the include/generated/autoksyms.h file. -# -# For each symbol being added or removed, the corresponding dependency -# file's timestamp is updated to force a rebuild of the affected source -# file. All arguments passed to this script are assumed to be a command -# to be exec'd to trigger a rebuild of those files. - -set -e - -cur_ksyms_file=3D"include/generated/autoksyms.h" -new_ksyms_file=3D"include/generated/autoksyms.h.tmpnew" - -info() { - if [ "$quiet" !=3D "silent_" ]; then - printf " %-7s %s\n" "$1" "$2" - fi -} - -info "CHK" "$cur_ksyms_file" - -# Use "make V=3D1" to debug this script. -case "$KBUILD_VERBOSE" in -*1*) - set -x - ;; -esac - -# Generate a new symbol list file -$CONFIG_SHELL $srctree/scripts/gen_autoksyms.sh --modorder "$new_ksyms_fil= e" - -# Extract changes between old and new list and touch corresponding -# dependency files. -changed=3D$( -count=3D0 -sort "$cur_ksyms_file" "$new_ksyms_file" | uniq -u | -sed -n 's/^#define __KSYM_\(.*\) 1/\1/p' | -while read sympath; do - if [ -z "$sympath" ]; then continue; fi - depfile=3D"include/ksym/${sympath}" - mkdir -p "$(dirname "$depfile")" - touch "$depfile" - # Filesystems with coarse time precision may create timestamps - # equal to the one from a file that was very recently built and that - # needs to be rebuild. Let's guard against that by making sure our - # dep files are always newer than the first file we created here. - while [ ! "$depfile" -nt "$new_ksyms_file" ]; do - touch "$depfile" - done - echo $((count +=3D 1)) -done | tail -1 ) -changed=3D${changed:-0} - -if [ $changed -gt 0 ]; then - # Replace the old list with tne new one - old=3D$(grep -c "^#define __KSYM_" "$cur_ksyms_file" || true) - new=3D$(grep -c "^#define __KSYM_" "$new_ksyms_file" || true) - info "KSYMS" "symbols: before=3D$old, after=3D$new, changed=3D$changed" - info "UPD" "$cur_ksyms_file" - mv -f "$new_ksyms_file" "$cur_ksyms_file" - # Then trigger a rebuild of affected source files - exec $@ -else - rm -f "$new_ksyms_file" -fi diff --git a/scripts/basic/fixdep.c b/scripts/basic/fixdep.c index 2328f9a641da..e7db12140d87 100644 --- a/scripts/basic/fixdep.c +++ b/scripts/basic/fixdep.c @@ -243,8 +243,7 @@ static void *read_file(const char *filename) /* Ignore certain dependencies */ static int is_ignored_file(const char *s, int len) { - return str_ends_with(s, len, "include/generated/autoconf.h") || - str_ends_with(s, len, "include/generated/autoksyms.h"); + return str_ends_with(s, len, "include/generated/autoconf.h"); } =20 /* diff --git a/scripts/gen_autoksyms.sh b/scripts/gen_autoksyms.sh deleted file mode 100755 index 653fadbad302..000000000000 --- a/scripts/gen_autoksyms.sh +++ /dev/null @@ -1,62 +0,0 @@ -#!/bin/sh -# SPDX-License-Identifier: GPL-2.0-only - -# Create an autoksyms.h header file from the list of all module's needed s= ymbols -# as recorded in *.usyms files and the user-provided symbol whitelist. - -set -e - -# Use "make V=3D1" to debug this script. -case "$KBUILD_VERBOSE" in -*1*) - set -x - ;; -esac - -read_modorder=3D - -if [ "$1" =3D --modorder ]; then - shift - read_modorder=3D1 -fi - -output_file=3D"$1" - -needed_symbols=3D - -# Special case for modversions (see modpost.c) -if grep -q "^CONFIG_MODVERSIONS=3Dy$" include/config/auto.conf; then - needed_symbols=3D"$needed_symbols module_layout" -fi - -ksym_wl=3D$(sed -n 's/^CONFIG_UNUSED_KSYMS_WHITELIST=3D\(.*\)$/\1/p' inclu= de/config/auto.conf) -if [ -n "$ksym_wl" ]; then - [ "${ksym_wl}" !=3D "${ksym_wl#/}" ] || ksym_wl=3D"$abs_srctree/$ksym_wl" - if [ ! -f "$ksym_wl" ] || [ ! -r "$ksym_wl" ]; then - echo "ERROR: '$ksym_wl' whitelist file not found" >&2 - exit 1 - fi -fi - -# Generate a new ksym list file with symbols needed by the current -# set of modules. -cat > "$output_file" << EOT -/* - * Automatically generated file; DO NOT EDIT. - */ - -EOT - -{ - [ -n "${read_modorder}" ] && sed 's/ko$/usyms/' modules.order | xargs cat - echo "$needed_symbols" - [ -n "$ksym_wl" ] && cat "$ksym_wl" -} | sed -e 's/ /\n/g' | sed -n -e '/^$/!p' | -# Remove the dot prefix for ppc64; symbol names with a dot (.) hold entry -# point addresses. -sed -e 's/^\.//' | -sort -u | -# Ignore __this_module. It's not an exported symbol, and will be resolved -# when the final .ko's are linked. -grep -v '^__this_module$' | -sed -e 's/\(.*\)/#define __KSYM_\1 1/' >> "$output_file" diff --git a/scripts/gen_ksymdeps.sh b/scripts/gen_ksymdeps.sh deleted file mode 100755 index 8ee533f33659..000000000000 --- a/scripts/gen_ksymdeps.sh +++ /dev/null @@ -1,30 +0,0 @@ -#!/bin/sh -# SPDX-License-Identifier: GPL-2.0 - -set -e - -# List of exported symbols -# -# If the object has no symbol, $NM warns 'no symbols'. -# Suppress the stderr. -# TODO: -# Use -q instead of 2>/dev/null when we upgrade the minimum version of -# binutils to 2.37, llvm to 13.0.0. -ksyms=3D$($NM $1 2>/dev/null | sed -n 's/.*__ksym_marker_\(.*\)/\1/p') - -if [ -z "$ksyms" ]; then - exit 0 -fi - -echo -echo "ksymdeps_$1 :=3D \\" - -for s in $ksyms -do - printf ' $(wildcard include/ksym/%s) \\\n' "$s" -done - -echo -echo "$1: \$(ksymdeps_$1)" -echo -echo "\$(ksymdeps_$1):" diff --git a/scripts/link-vmlinux.sh b/scripts/link-vmlinux.sh index 2782c5d1518b..9c3140d33ccd 100755 --- a/scripts/link-vmlinux.sh +++ b/scripts/link-vmlinux.sh @@ -305,4 +305,7 @@ if is_enabled CONFIG_KALLSYMS; then fi =20 # For fixdep -echo "vmlinux: $0" > .vmlinux.d +echo "vmlinux: $0 \\" > .vmlinux.d +if is_enabled CONFIG_MODULES; then + echo .vmlinux.export.c >> .vmlinux.d +fi diff --git a/scripts/mod/modpost.c b/scripts/mod/modpost.c index 90733664a602..1e9eba0a6cec 100644 --- a/scripts/mod/modpost.c +++ b/scripts/mod/modpost.c @@ -34,6 +34,9 @@ static bool warn_unresolved; =20 static int sec_mismatch_count; static bool sec_mismatch_warn_only =3D true; +/* Trim EXPORT_SYMBOLs that are unused by in-tree modules */ +static bool trim_unused_exports; + /* ignore missing files */ static bool ignore_missing_files; /* If set to 1, only warn (instead of error) about missing ns imports */ @@ -216,6 +219,7 @@ struct symbol { bool weak; bool is_func; bool is_gpl_only; /* exported by EXPORT_SYMBOL_GPL */ + bool used; /* there exists a user of this symbol */ char name[]; }; =20 @@ -1872,6 +1876,7 @@ static void check_exports(struct module *mod) continue; } =20 + exp->used =3D true; s->module =3D exp->module; s->crc_valid =3D exp->crc_valid; s->crc =3D exp->crc; @@ -1895,6 +1900,23 @@ static void check_exports(struct module *mod) } } =20 +static void handle_white_list_exports(const char *white_list) +{ + char *buf, *p, *name; + + buf =3D read_text_file(white_list); + p =3D buf; + + while ((name =3D strsep(&p, "\n"))) { + struct symbol *sym =3D find_symbol(name); + + if (sym) + sym->used =3D true; + } + + free(buf); +} + static void check_modname_len(struct module *mod) { const char *mod_name; @@ -1965,10 +1987,14 @@ static void add_exported_symbols(struct buffer *buf= , struct module *mod) =20 /* generate struct for exported symbols */ buf_printf(buf, "\n"); - list_for_each_entry(sym, &mod->exported_symbols, list) + list_for_each_entry(sym, &mod->exported_symbols, list) { + if (trim_unused_exports && !sym->used) + continue; + buf_printf(buf, "KSYMTAB_%s(%s, \"%s\", \"%s\");\n", sym->is_func ? "FUNC" : "DATA", sym->name, sym->is_gpl_only ? "_gpl" : "", sym->namespace); + } =20 if (!modversions) return; @@ -1976,6 +2002,9 @@ static void add_exported_symbols(struct buffer *buf, = struct module *mod) /* record CRCs for exported symbols */ buf_printf(buf, "\n"); list_for_each_entry(sym, &mod->exported_symbols, list) { + if (trim_unused_exports && !sym->used) + continue; + if (!sym->crc_valid) warn("EXPORT symbol \"%s\" [%s%s] version generation failed, symbol wil= l not be versioned.\n" "Is \"%s\" prototyped in ?\n", @@ -2139,9 +2168,6 @@ static void write_mod_c_file(struct module *mod) char fname[PATH_MAX]; int ret; =20 - check_modname_len(mod); - check_exports(mod); - add_header(&buf, mod); add_exported_symbols(&buf, mod); add_versions(&buf, mod); @@ -2275,12 +2301,13 @@ int main(int argc, char **argv) { struct module *mod; char *missing_namespace_deps =3D NULL; + char *unused_exports_white_list =3D NULL; char *dump_write =3D NULL, *files_source =3D NULL; int opt; LIST_HEAD(dump_lists); struct dump_list *dl, *dl2; =20 - while ((opt =3D getopt(argc, argv, "ei:mnT:o:awENd:")) !=3D -1) { + while ((opt =3D getopt(argc, argv, "ei:mntT:tW:o:awENd:")) !=3D -1) { switch (opt) { case 'e': external_module =3D true; @@ -2305,6 +2332,12 @@ int main(int argc, char **argv) case 'T': files_source =3D optarg; break; + case 't': + trim_unused_exports =3D true; + break; + case 'W': + unused_exports_white_list =3D optarg; + break; case 'w': warn_unresolved =3D true; break; @@ -2334,6 +2367,17 @@ int main(int argc, char **argv) if (files_source) read_symbols_from_files(files_source); =20 + list_for_each_entry(mod, &modules, list) { + if (mod->from_dump || mod->is_vmlinux) + continue; + + check_modname_len(mod); + check_exports(mod); + } + + if (unused_exports_white_list) + handle_white_list_exports(unused_exports_white_list); + list_for_each_entry(mod, &modules, list) { if (mod->from_dump) continue; diff --git a/scripts/remove-stale-files b/scripts/remove-stale-files index ccadfa3afb2b..d2009eda270a 100755 --- a/scripts/remove-stale-files +++ b/scripts/remove-stale-files @@ -47,3 +47,5 @@ rm -f arch/riscv/purgatory/kexec-purgatory.c rm -f scripts/extract-cert =20 rm -f arch/x86/purgatory/kexec-purgatory.c + +rm -rf include/ksym --=20 2.34.1 From nobody Fri Sep 12 22:42:03 2025 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 32B9AC07E9D for ; Mon, 26 Sep 2022 20:37:55 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229973AbiIZUhx (ORCPT ); Mon, 26 Sep 2022 16:37:53 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:35762 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229871AbiIZUhs (ORCPT ); Mon, 26 Sep 2022 16:37:48 -0400 Received: from conuserg-10.nifty.com (conuserg-10.nifty.com [210.131.2.77]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 27B179E6A9; Mon, 26 Sep 2022 13:37:45 -0700 (PDT) Received: from zoe.. (133-32-182-133.west.xps.vectant.ne.jp [133.32.182.133]) (authenticated) by conuserg-10.nifty.com with ESMTP id 28QKaWu7018737; Tue, 27 Sep 2022 05:36:41 +0900 DKIM-Filter: OpenDKIM Filter v2.10.3 conuserg-10.nifty.com 28QKaWu7018737 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=nifty.com; s=dec2015msa; t=1664224602; bh=0kYHR0v0Xht9beu6edv2Fg++Fw26ZI8vbsd8r5R7tlM=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=JtuTJAYw4LhkBbOZKNuuCK0OsB2aQnt6l5TygroYmQVy79p0dKrKueoWQbcMJnZLv D0MnmqdTtTXyP/wGjcVfZGS9hC2h8mZIIykMi9u1GG5Xjrnhj522nScOt2GxPSIm/V Qh0R94FQpXzF3U9pyRn7bnU1Lq9i/noTvRST5kglQuNR+XLEIxF3RK+80n1AnzI71N K79gElcPnjMxl5q6nTNzzCcT+w0YdjtgqVo2PaTGPkpx/js8blvbcJ0pw8M2aKxb/X AhNPPTd/c4LAthtYl6BfU5c1I3xQ1lR8J3jPY72A7kjQsJfAL/IObEkRR/4KnhTS/l OH++ITvdYFP9Q== X-Nifty-SrcIP: [133.32.182.133] From: Masahiro Yamada To: linux-kbuild@vger.kernel.org Cc: linux-arch@vger.kernel.org, linux-kernel@vger.kernel.org, linux-ia64@vger.kernel.org, Al Viro , Ard Biesheuvel , Nicolas Pitre , Masahiro Yamada , Michal Marek , Nick Desaulniers Subject: [PATCH v2 7/7] kbuild: move modules.builtin(.modinfo) rules to Makefile.vmlinux_o Date: Tue, 27 Sep 2022 05:36:25 +0900 Message-Id: <20220926203625.1117261-8-masahiroy@kernel.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20220926203625.1117261-1-masahiroy@kernel.org> References: <20220926203625.1117261-1-masahiroy@kernel.org> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" Do not build modules.builtin(.modinfo) as a side-effect of vmlinux. There are no good reason to rebuild them just because vmlinux's prerequistes (vmlinux.lds, .vmlinux.export.c, etc.) have been updated. Signed-off-by: Masahiro Yamada --- Changes in v2: - New patch scripts/Makefile.vmlinux_o | 26 +++++++++++++++++++++++++- scripts/link-vmlinux.sh | 7 ------- 2 files changed, 25 insertions(+), 8 deletions(-) diff --git a/scripts/Makefile.vmlinux_o b/scripts/Makefile.vmlinux_o index 68c22879bade..5e8f75d336af 100644 --- a/scripts/Makefile.vmlinux_o +++ b/scripts/Makefile.vmlinux_o @@ -1,7 +1,7 @@ # SPDX-License-Identifier: GPL-2.0-only =20 PHONY :=3D __default -__default: vmlinux.o +__default: vmlinux.o modules.builtin =20 include include/config/auto.conf include $(srctree)/scripts/Kbuild.include @@ -62,6 +62,30 @@ vmlinux.o: $(initcalls-lds) vmlinux.a $(KBUILD_VMLINUX_L= IBS) FORCE =20 targets +=3D vmlinux.o =20 +# module.builtin.modinfo +# ------------------------------------------------------------------------= --- + +OBJCOPYFLAGS_modules.builtin.modinfo :=3D -j .modinfo -O binary + +targets +=3D modules.builtin.modinfo +modules.builtin.modinfo: vmlinux.o FORCE + $(call if_changed,objcopy) + +# module.builtin +# ------------------------------------------------------------------------= --- + +# The second line aids cases where multiple modules share the same object. + +quiet_cmd_modules_builtin =3D GEN $@ + cmd_modules_builtin =3D \ + tr '\0' '\n' < $< | \ + sed -n 's/^[[:alnum:]:_]*\.file=3D//p' | \ + tr ' ' '\n' | uniq | sed -e 's:^:kernel/:' -e 's/$$/.ko/' > $@ + +targets +=3D modules.builtin +modules.builtin: modules.builtin.modinfo FORCE + $(call if_changed,modules_builtin) + # Add FORCE to the prequisites of a target to force it to be always rebuil= t. # ------------------------------------------------------------------------= --- =20 diff --git a/scripts/link-vmlinux.sh b/scripts/link-vmlinux.sh index 9c3140d33ccd..5e7a12284b39 100755 --- a/scripts/link-vmlinux.sh +++ b/scripts/link-vmlinux.sh @@ -214,13 +214,6 @@ if [ "$1" =3D "clean" ]; then exit 0 fi =20 -info MODINFO modules.builtin.modinfo -${OBJCOPY} -j .modinfo -O binary vmlinux.o modules.builtin.modinfo -info GEN modules.builtin -# The second line aids cases where multiple modules share the same object. -tr '\0' '\n' < modules.builtin.modinfo | sed -n 's/^[[:alnum:]:_]*\.file= =3D//p' | - tr ' ' '\n' | uniq | sed -e 's:^:kernel/:' -e 's/$/.ko/' > modules.builtin - if is_enabled CONFIG_MODULES; then ${MAKE} -f "${srctree}/scripts/Makefile.vmlinux" .vmlinux.export.o fi --=20 2.34.1