Unify <linux/export.h> and <asm/export.h>, remove EXPORT_DATA_SYMBOL()
Subsystems (4)
cover Unify <linux/export.h> and <asm/export.h>, remove EXPORT_DATA_SYMBOL()
This patch set refactors EXPORT_SYMBOL, <linux/export.h> and <asm/export.h>.
You can still put EXPORT_SYMBOL() in *.S file, very close to the definition, but you do not need to care about whether it is a function or a data. Remove EXPORT_DATA_SYMBOL().
Masahiro Yamada (7):
modpost: fix section mismatch check for exported init/exit sections
modpost: put get_secindex() call inside sec_name()
kbuild: generate struct kernel_symbol by modpost ia64,export.h: replace EXPORT_DATA_SYMBOL* with EXPORT_SYMBOL*
checkpatch: warn if <asm/export.h> is included
modpost: merge sym_update_namespace() into sym_add_exported()
modpost: use null string instead of NULL pointer for default namespace
arch/ia64/include/asm/Kbuild | 1 + arch/ia64/include/asm/export.h | 3 - arch/ia64/kernel/head.S | 2 +- arch/ia64/kernel/ivt.S | 2 +- include/asm-generic/export.h | 83 +------------------ include/linux/export-internal.h | 44 ++++++++++ include/linux/export.h | 97 ++++++++-------------- kernel/module/internal.h | 12 +++ kernel/module/main.c | 1 - scripts/Makefile.build | 8 +- scripts/check-local-export | 4 +- scripts/checkpatch.pl | 7 ++ scripts/mod/modpost.c | 139 +++++++++++++++++--------------- scripts/mod/modpost.h | 1 + 14 files changed, 182 insertions(+), 222 deletions(-) delete mode 100644 arch/ia64/include/asm/export.h
Signature · 1 line
2.32.0
PATCH 1/7 modpost: fix section mismatch check for exported init/exit sections
Since commit f02e8a6596b7 ("module: Sort exported symbols"), EXPORT_SYMBOL* is placed in the individual section ___ksymtab(_gpl)+<sym> (3 leading underscores instead of 2).
Since then, modpost cannot detect the bad combination of EXPORT_SYMBOL and __init/__exit.
Fix the .fromsec field.
---
scripts/mod/modpost.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
scripts/mod/modpost.c +1 -1
@@ -980,7 +980,7 @@ static const struct sectioncheck sectioncheck[] = { },/* Do not export init/exit functions or data */ { - .fromsec = { "__ksymtab*", NULL }, + .fromsec = { "___ksymtab*", NULL }, .bad_tosec = { INIT_SECTIONS, EXIT_SECTIONS, NULL }, .mismatch = EXPORT_TO_INIT_EXIT, .symbol_white_list = { DEFAULT_SYMBOL_WHITE_LIST, NULL },
Signature · 1 line
2.32.0
PATCH 2/7 modpost: put get_secindex() call inside sec_name()
In reply to Masahiro YamadaThere are 5 callsites of sec_name(). In all the places, sec_name() is used together with get_secindex().
So, it is simpler to merge two function calls
sec_name(elf, get_secindex(elf, sym))
into one call:
sec_name_of_symbol(elf, sym)
While I was here, I also inserted this array range check:
if (secindex >= info->num_sections)
return "";This will make the code robust against info->sechdrs[] overrun.
sym->st_shndx is 2 bytes (for both 32 and 64 bit systems), and the range 0xff00..0xffff is reserved for special sections.
For example, a symbol specifies an absolute value, sym->st_shndx==0xfff1. get_secindex() remaps it to 0xfffffff1.
There is no corresponding section header for such special sections.
The existing code does not hit this issue, but it is better to check the array range.
---
scripts/mod/modpost.c | 23 +++++++++++++++++------ 1 file changed, 17 insertions(+), 6 deletions(-)
scripts/mod/modpost.c +17 -6
@@ -339,8 +339,19 @@ static const char *sech_name(const struct elf_info *info, Elf_Shdr *sechdr) sechdr->sh_name); } -static const char *sec_name (const struct elf_info *info,int secindex) +static const char *sec_name_of_symbol (const struct elf_info *info, +const Elf_Sym *sym) { +unsigned int secindex =get_secindex (info, sym); + +/* +* If sym->st_shndx is within the special section range, get_secindex() +* will remapit to a big number. +* Bail out here, otherwise info->sechdrs[secindex] would overrun. +*/ +if (secindex >= info->num_sections) +return "" ; +return sech_name (info, &info->sechdrs[secindex]); }
@@ -649,7 +660,7 @@ static void handle_symbol(struct module *mod, struct elf_info *info,const char *name, *secname; name = symname +strlen ("__ksymtab_" ); - secname =sec_name (info,get_secindex (info, sym)); + secname =sec_name_of_symbol (info, sym);if (strstarts (secname,"___ksymtab_gpl+" ))sym_add_exported (name, mod, true);
@@ -1217,7 +1228,7 @@ static Elf_Sym *find_elf_symbol2(struct elf_info *elf, Elf_Addr addr,if (is_shndx_special (sym->st_shndx))continue ; - symsec =sec_name (elf,get_secindex (elf, sym)); + symsec =sec_name_of_symbol (elf, sym);if (strcmp (symsec, sec) !=0 )continue ;if (!is_valid_name (elf, sym))
@@ -1457,7 +1468,7 @@ static void default_mismatch_handler(const char *modname, struct elf_info *elf,if (strstarts (fromsym,"reference___initcall" ))return ; - tosec =sec_name (elf,get_secindex (elf, sym)); + tosec =sec_name_of_symbol (elf, sym); to =find_elf_symbol (elf, r->r_addend, sym); tosym =sym_name (elf, to);
@@ -1559,7 +1570,7 @@ static void extable_mismatch_handler(const char* modname, struct elf_info *elf, Elf_Rela* r, Elf_Sym* sym, const char *fromsec) { -const char * tosec =sec_name (elf,get_secindex (elf, sym)); +const char *tosec =sec_name_of_symbol (elf, sym); sec_mismatch_count++;
@@ -1593,7 +1604,7 @@ static void extable_mismatch_handler(const char* modname, struct elf_info *elf,static void check_section_mismatch (const char *modname,struct elf_info *elf,Elf_Rela *r,Elf_Sym *sym,const char *fromsec) { -const char *tosec =sec_name (elf,get_secindex (elf, sym)); +const char *tosec =sec_name_of_symbol (elf, sym);const struct sectioncheck *mismatch =section_mismatch (fromsec, tosec);if (mismatch) {
Signature · 1 line
2.32.0
Re: [PATCH 2/7] modpost: put get_secindex() call inside sec_name()
Quoted 25 lines · Masahiro Yamada
There are 5 callsites of
sec_name(). In all the places,sec_name()is used together withget_secindex().So, it is simpler to merge two function calls
sec_name(elf, get_secindex(elf, sym))into one call:
sec_name_of_symbol(elf, sym)While I was here, I also inserted this array range check:
if (secindex >= info->num_sections) return "";This will make the code robust against info->sechdrs[] overrun.
sym->st_shndxis 2 bytes (for both 32 and 64 bit systems), and the range 0xff00..0xffff is reserved for special sections.For example, a symbol specifies an absolute value, sym->st_shndx==0xfff1.
get_secindex()remaps it to0xfffffff1.
Quoted 6 lines
There is no corresponding section header for such special sections.
The existing code does not hit this issue, but it is better to check the array range.
Signed-off-by Masahiro Yamada <[email protected]>
Thanks for the patch!
Quoted 78 lines
---
scripts/mod/modpost.c | 23 +++++++++++++++++------ 1 file changed, 17 insertions(+), 6 deletions(-)scripts/mod/modpost.c +17
-7@@ -339,8 +339,19 @@ static const char *sech_name(const struct elf_info *info, Elf_Shdr *sechdr) sechdr->sh_name); } -static const char *sec_name (const struct elf_info *info,int secindex) +static const char *sec_name_of_symbol (const struct elf_info *info, +const Elf_Sym *sym) { +unsigned int secindex =get_secindex (info, sym); + +/* +* If sym->st_shndx is within the special section range, get_secindex() +* will remapit to a big number. +* Bail out here, otherwise info->sechdrs[secindex] would overrun. +*/ +if (secindex >= info->num_sections) +return "" ; +return sech_name (info, &info->sechdrs[secindex]); }@@ -649,7 +660,7 @@ static void handle_symbol(struct module *mod, struct elf_info *info,const char *name, *secname; name = symname +strlen ("__ksymtab_" ); - secname =sec_name (info,get_secindex (info, sym)); + secname =sec_name_of_symbol (info, sym);if (strstarts (secname,"___ksymtab_gpl+" ))sym_add_exported (name, mod, true);@@ -1217,7 +1228,7 @@ static Elf_Sym *find_elf_symbol2(struct elf_info *elf, Elf_Addr addr,if (is_shndx_special (sym->st_shndx))continue ; - symsec =sec_name (elf,get_secindex (elf, sym)); + symsec =sec_name_of_symbol (elf, sym);if (strcmp (symsec, sec) !=0 )continue ;if (!is_valid_name (elf, sym))@@ -1457,7 +1468,7 @@ static void default_mismatch_handler(const char *modname, struct elf_info *elf,if (strstarts (fromsym,"reference___initcall" ))return ; - tosec =sec_name (elf,get_secindex (elf, sym)); + tosec =sec_name_of_symbol (elf, sym); to =find_elf_symbol (elf, r->r_addend, sym); tosym =sym_name (elf, to);@@ -1559,7 +1570,7 @@ static void extable_mismatch_handler(const char* modname, struct elf_info *elf, Elf_Rela* r, Elf_Sym* sym, const char *fromsec) { -const char * tosec =sec_name (elf,get_secindex (elf, sym)); +const char *tosec =sec_name_of_symbol (elf, sym); sec_mismatch_count++;@@ -1593,7 +1604,7 @@ static void extable_mismatch_handler(const char* modname, struct elf_info *elf,static void check_section_mismatch (const char *modname,struct elf_info *elf,Elf_Rela *r,Elf_Sym *sym,const char *fromsec) { -const char *tosec =sec_name (elf,get_secindex (elf, sym)); +const char *tosec =sec_name_of_symbol (elf, sym);const struct sectioncheck *mismatch =section_mismatch (fromsec, tosec);if (mismatch) { --2.32.0
Signature · 2 lines
Thanks, ~Nick Desaulniers
PATCH 3/7 kbuild: generate struct kernel_symbol by modpost
In reply to Masahiro YamadaCommit 7b4537199a4a ("kbuild: link symbol CRCs at final link, removing CONFIG_MODULE_REL_CRCS") made the module versioning implementation arch-agnostic; now all the version CRCs are generated by modpost as C code whether the EXPORT_SYMBOL() is placed in *.c or *.S.
Doing similar for the entire data structure of EXPORT_SYMBOL() makes further cleanups possible.
This commit splits EXPORT_SYMBOL() compilation 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 expanded 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"
.previousThey 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 of EXPORT_SYMBOL() from the .discard.export_symbol section, then generates C code:
KSYMTAB_ENTRY(foo, "", "");
KSYMTAB_ENTRY(bar, "_gpl", "BAR_NAMESPACE");KSYMTAB_ENTRY() 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 assembly code, EXPORT_SYMBOL() was placed in a separate file. arch/arm/kernel/armksyms.c is one example written in the old fashion.
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.
For example, commit e007c53397ac ("ia64: move exports to definitions") moved EXPORT_SYMBOL(ia64_ivt) to a .S file, but it resulted in EXPORT_DATA_SYMBOL().
In the new approach, struct kernel_symbol is populated in C. You can always use EXPORT_SYMBOL().
EXPORT_DATA_SYMBOL() is now deprecated.
[2] merge <linux/export.h> and <asm-generic/export.h>
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 part to C. EXPORT_SYMBOL_NS() is now supported for .S files.
<asm/export.h> and <asm-generic/export.h> will remain as a wrapper of <linux/export.h> for a while.
They will be removed after #include <asm/export.h> directives are all replaced.
[3] Implement CONFIG_TRIM_UNUSED_KSYMS in one-pass (planned)
CONFIG_TRIM_UNUSED_KSYMS works by recursively traversing the directory tree. If an EXPORT_SYMBOL turns out to be unused by anyone, the source file is recompiled, making the EXPORT_SYMBOL() a no-op.
Linus did not like this slowness.
See:
- 5cf0fd591f2e ("Kbuild: disable TRIM_UNUSED_KSYMS option")
- a555bdd0c58c ("Kbuild: enable TRIM_UNUSED_KSYMS again, with some guarding")
We can do this better now; modpost can selectively emit KSYMTAB_ENTRY's that are really referenced by modules.
Nicolas explained why the trimming was implemented with recursion:
https://lore.kernel.org/all/[email protected]/
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 making the directory traverse one-pass is presumably the right way to go.
---
arch/ia64/include/asm/Kbuild | 1 + arch/ia64/include/asm/export.h | 3 - include/asm-generic/export.h | 84 ++-------------------------- include/linux/export-internal.h | 44 +++++++++++++++ include/linux/export.h | 97 +++++++++++---------------------- kernel/module/internal.h | 12 ++++ kernel/module/main.c | 1 - scripts/Makefile.build | 8 +-- scripts/check-local-export | 4 +- scripts/mod/modpost.c | 85 +++++++++++++++++------------ scripts/mod/modpost.h | 1 + 11 files changed, 151 insertions(+), 189 deletions(-) delete mode 100644 arch/ia64/include/asm/export.h
arch/ia64/include/asm/Kbuild +1 -0
@@ -1,5 +1,6 @@ # SPDX-License-Identifier: GPL-2.0 generated-y += syscall_table.h +generic-y += export.h generic-y += kvm_para.h generic-y += mcs_spinlock.h generic-y += vtime.h
arch/ia64/include/asm/export.h deleted +0 -3
@@ -1,3 +0,0 @@ -/* EXPORT_DATA_SYMBOL != EXPORT_SYMBOL here */ -#define KSYM_FUNC (name) @fptr(name) -#include <asm-generic/export.h>
include/asm-generic/export.h +5 -79
91 lines not on this page · raw message
include/linux/export-internal.h +44 -0
48 lines not on this page · raw message
include/linux/export.h +32 -65
130 lines not on this page · raw message
kernel/module/internal.h +12 -0
@@ -36,6 +36,18 @@# define strict_align (X ) (X)#endif +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 +}; +extern struct mutex module_mutex;extern struct list_head modules;
kernel/module/main.c +0 -1
@@ -6,7 +6,6 @@#define INCLUDE_VERMAGIC -#include <linux/export.h> #include <linux/extable.h> #include <linux/moduleloader.h> #include <linux/module_signature.h>
scripts/Makefile.build +3 -5
@@ -164,7 +164,7 @@ quiet_cmd_cc_o_c = CC $(quiet_modtag) $@ ifdef CONFIG_MODVERSIONS # When module versioning is enabled the following steps are executed: # o compile a <file>.o from <file>.c -# o if <file>.o doesn't contain a __ksymtab version, i.e. does +# o if <file>.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. gen_symversions = \ - 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=.symtypes)) \ >> $(dot-target).cmd; \ fi
@@ -288,9 +288,7 @@ $(obj)/%.lst: $(src)/%.c FORCE cmd_gensymtypes_S = \ { echo "\#include <linux/kernel.h>" ; \ echo "\#include <asm/asm-prototypes.h>" ; \ - $(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) quiet_cmd_cc_symtypes_S = SYM $(quiet_modtag) $@
scripts/check-local-export +2 -2
@@ -34,8 +34,8 @@ do symbol_types[${name}]=${type} # append the exported symbol to the array - if [[ ${name} = __ksymtab_* ]]; then - export_symbols+=(${name#__ksymtab_}) + if [[ ${name} = __export_symbol*.* ]]; then + export_symbols+=(${name#*.}) fi # If there is no symbol in the object, ${NM} (both GNU nm and llvm-nm)
scripts/mod/modpost.c +51 -34
137 lines not on this page · raw message
scripts/mod/modpost.h +1 -0
@@ -139,6 +139,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;
Signature · 1 line
2.32.0
Re: [PATCH 3/7] kbuild: generate struct kernel_symbol by modpost
Quoted 41 lines · Masahiro Yamada
Commit
7b4537199a4a("kbuild: link symbol CRCs at final link, removingCONFIG_MODULE_REL_CRCS") made the module versioning implementation arch-agnostic; now all the version CRCs are generated by modpost as C code whether theEXPORT_SYMBOL()is placed in *.c or *.S.Doing similar for the entire data structure of
EXPORT_SYMBOL()makes further cleanups possible.This commit splits
EXPORT_SYMBOL()compilation 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 expanded 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" .previousThey 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 of
EXPORT_SYMBOL()from the .discard.export_symbol section, then generates C code:KSYMTAB_ENTRY(foo, "", ""); KSYMTAB_ENTRY(bar, "_gpl", "BAR_NAMESPACE");
Quoted 7 lines
KSYMTAB_ENTRY()is expanded to structkernel_symbolthat 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()
Sorry, please let me take back this.
I completely missed how linkage for ia64 works. I used inline assembly in C, so it would break ia64.
Please ignore this patch.
Signature · 2 lines
Best Regards Masahiro Yamada
PATCH 4/7 ia64,export.h: replace EXPORT_DATA_SYMBOL* with EXPORT_SYMBOL*
In reply to Masahiro YamadaWith the previous refactoring, you no longer need to know whether the exported symbol is a function or a data.
Replace two instances in ia64, then remove EXPORT_DATA_SYMBOL*.
---
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(-)
arch/ia64/kernel/head.S +1 -1
@@ -170,7 +170,7 @@ RestRR: \ __PAGE_ALIGNED_DATA .global empty_zero_page -EXPORT_DATA_SYMBOL_GPL(empty_zero_page) +EXPORT_SYMBOL_GPL(empty_zero_page) empty_zero_page: .skip PAGE_SIZE
arch/ia64/kernel/ivt.S +1 -1
@@ -87,7 +87,7 @@ .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)
include/asm-generic/export.h +0 -3
@@ -8,7 +8,4 @@*/ #include <linux/export.h> -#define EXPORT_DATA_SYMBOL (name) EXPORT_SYMBOL(name) -#define EXPORT_DATA_SYMBOL_GPL (name) EXPORT_SYMBOL_GPL(name) -#endif
Signature · 1 line
2.32.0
Re: [PATCH 4/7] ia64,export.h: replace EXPORT_DATA_SYMBOL* with EXPORT_SYMBOL*
Quoted 7 lines · Masahiro Yamada
With the previous refactoring, you no longer need to know whether the exported symbol is a function or a data.
Replace two instances in ia64, then remove EXPORT_DATA_SYMBOL*.
Signed-off-by Masahiro Yamada <[email protected]>
Please ignore this patch.
The previous refactoring is not working correctly.
Signature · 2 lines
Best Regards Masahiro Yamada
PATCH 5/7 checkpatch: warn if <asm/export.h> is included
In reply to Masahiro YamadaWith the previous refactoring,
- <asm/export.h> is a wrapper of <asm-generic/export.h>
- <asm-generic/export.h> is a wrapper of <linux/export.h>
My hope is to replace
#include <asm/export.h> --> #include <linux/export.h>
for all *.S files.
For now, adding a warning in the checkpatch.
---
scripts/checkpatch.pl | 7 +++++++ 1 file changed, 7 insertions(+)
scripts/checkpatch.pl +7 -0
@@ -3753,6 +3753,13 @@ sub process { "Avoid using '.L' prefixed local symbol names for denoting a range of code via 'SYM_*_START/END' annotations; see Documentation/asm-annotations.rst\n" . $herecurr); } +# warn if <asm/export.h> is included. +# <asm/export.h> is a wrapper of <linux/export.h>. Please include <linux/export.h> directly. + if ($tree && $rawline =~ m{^.\s*\#\s*include\s*\<asm\/export\.h\>}) { + WARN("INCLUDE_LINUX_EXPORT", + "Please include <linux/export.h> instead of <asm/export.h>\n" . $herecurr); + } + # check we are in a valid source file C or perl if not then ignore this hunk next if ($realfile !~ /\.(h|c|pl|dtsi|dts)$/);
Signature · 1 line
2.32.0
Joe Perches
maintainerRe: [PATCH 5/7] checkpatch: warn if <asm/export.h> is included
scripts/checkpatch.pl
Quoted 20 lines · Masahiro Yamada
With the previous refactoring,
- <
asm/export.h> is a wrapper of <asm-generic/export.h>
- <asm-generic/export.h> is a wrapper of <linux/export.h>My hope is to replace
#include <asm/export.h> --> #include <linux/export.h>for all *.S files.
For now, adding a warning in the checkpatch.
Signed-off-by Masahiro Yamada <[email protected]>---
scripts/checkpatch.pl | 7 +++++++ 1 file changed, 7 insertions(+)scripts/checkpatch.pl +0
-0
[]
scripts/checkpatch.pl sub process { · line 3761
Quoted 10 lines
scripts/checkpatch.pl +6
-0@@ -3753,6 +3753,13 @@ sub process { "Avoid using '.L' prefixed local symbol names for denoting a range of code via 'SYM_*_START/END' annotations; see Documentation/asm-annotations.rst\n" . $herecurr); } +# warn if <asm/export.h> is included. +# <asm/export.h> is a wrapper of <linux/export.h>. Please include <linux/export.h> directly. + if ($tree && $rawline =~ m{^.\s*\#\s*include\s*\<asm\/export\.h\>}) { + WARN("INCLUDE_LINUX_EXPORT", + "Please include <linux/export.h> instead of <asm/export.h>\n" . $herecurr); + }
This warns on patch context lines.
That's not something checkpatch generally does.
Likely this should use /^\+ rather than /^.
And it's nice to have --fix capability
if (WARN("etc...") &&
$fix) {
$fixed[$fixlinenr] =~ s/\s*#\s*include\s*\<asm\/export\.h\>/#include <linux/export.h>/;
}cheers, Joe
Re: [PATCH 5/7] checkpatch: warn if <asm/export.h> is included
Quoted 39 lines · Joe Perches
scripts/checkpatch.pl
Quoted 20 lines · Masahiro Yamada
With the previous refactoring,
- <
asm/export.h> is a wrapper of <asm-generic/export.h>
- <asm-generic/export.h> is a wrapper of <linux/export.h>My hope is to replace
#include <asm/export.h> --> #include <linux/export.h>for all *.S files.
For now, adding a warning in the checkpatch.
Signed-off-by Masahiro Yamada <[email protected]>---
scripts/checkpatch.pl | 7 +++++++ 1 file changed, 7 insertions(+)scripts/checkpatch.pl +0
-0[]
scripts/checkpatch.pl sub process { · line 3761
Quoted 10 lines
scripts/checkpatch.pl +6
-0@@ -3753,6 +3753,13 @@ sub process { "Avoid using '.L' prefixed local symbol names for denoting a range of code via 'SYM_*_START/END' annotations; see Documentation/asm-annotations.rst\n" . $herecurr); } +# warn if <asm/export.h> is included. +# <asm/export.h> is a wrapper of <linux/export.h>. Please include <linux/export.h> directly. + if ($tree && $rawline =~ m{^.\s*\#\s*include\s*\<asm\/export\.h\>}) { + WARN("INCLUDE_LINUX_EXPORT", + "Please include <linux/export.h> instead of <asm/export.h>\n" . $herecurr); + }This warns on patch context lines.
That's not something checkpatch generally does.Likely this should use /^\+ rather than /^.
Quoted 8 lines
And it's nice to have --fix capability
if (WARN("etc...") && $fix) { $fixed[$fixlinenr] =~ s/\s*#\s*include\s*\<asm\/export\.h\>/#include <linux/export.h>/; }cheers, Joe
I retract this patch series because I realized it was breaking ia64. Please ignore this patch.
Anyway, your expert feedback about checkpatch.pl
was very appreciated. Thank you.
Signature · 2 lines
Best Regards Masahiro Yamada
PATCH 6/7 modpost: merge sym_update_namespace() into sym_add_exported()
In reply to Masahiro YamadaPass a set of the name, license, and namespace to sym_add_exported().
sym_update_namespace() is unneeded.
---
scripts/mod/modpost.c | 41 +++++++++-------------------------------- 1 file changed, 9 insertions(+), 32 deletions(-)
scripts/mod/modpost.c +9 -32
@@ -357,26 +357,8 @@ static const char *sec_name_of_symbol(const struct elf_info *info,#define strstarts (str, prefix) (strncmp(str, prefix, strlen(prefix)) == 0) -static void sym_update_namespace (const char *symname,const char *namespace) -{ -struct symbol *s =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 = namespace[0 ] ?NOFAIL (strdup (namespace)) :NULL ; -} -static struct symbol *sym_add_exported (const char *name,struct module *mod, -bool gpl_only) +bool gpl_only,const char *namespace) {struct symbol *s =find_symbol (name);
@@ -389,6 +371,7 @@ static struct symbol *sym_add_exported(const char *name, struct module *mod, s =alloc_symbol (name); s->module = mod; s->is_gpl_only = gpl_only; + s->namespace = namespace[0 ] ?NOFAIL (strdup (namespace)) :NULL ;list_add_tail (&s->list, &mod->exported_symbols);hash_add_symbol (s);
@@ -658,17 +641,12 @@ static void handle_symbol(struct module *mod, struct elf_info *info,break ;default :if (sym->st_shndx == info->export_symbol_sec) { -const char *name; - -if (strstarts (symname,"__export_symbol_gpl." )) { - name = 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 = 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));break ; }
@@ -2470,9 +2448,8 @@ static void read_dump(const char *fname) mod =new_module (modname,strlen (modname)); mod->from_dump = true; } - s =sym_add_exported (symname, mod, gpl_only); + s =sym_add_exported (symname, mod, gpl_only, namespace);sym_set_crc (s, crc); -sym_update_namespace (symname, namespace); }free (buf);return ;
Signature · 1 line
2.32.0
Re: [PATCH 6/7] modpost: merge sym_update_namespace() into sym_add_exported()
Quoted 6 lines · Masahiro Yamada
Pass a set of the name, license, and namespace to
sym_add_exported().
sym_update_namespace()is unneeded.Signed-off-by Masahiro Yamada <[email protected]>
Thanks for the patch!
Quoted 83 lines
---
scripts/mod/modpost.c | 41 +++++++++-------------------------------- 1 file changed, 9 insertions(+), 32 deletions(-)scripts/mod/modpost.c +9
-33@@ -357,26 +357,8 @@ static const char *sec_name_of_symbol(const struct elf_info *info,#define strstarts (str, prefix) (strncmp(str, prefix, strlen(prefix)) == 0) -static void sym_update_namespace (const char *symname,const char *namespace) -{ -struct symbol *s =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 = namespace[0 ] ?NOFAIL (strdup (namespace)) :NULL ; -} -static struct symbol *sym_add_exported (const char *name,struct module *mod, -bool gpl_only) +bool gpl_only,const char *namespace) {struct symbol *s =find_symbol (name);@@ -389,6 +371,7 @@ static struct symbol *sym_add_exported(const char *name, struct module *mod, s =alloc_symbol (name); s->module = mod; s->is_gpl_only = gpl_only; + s->namespace = namespace[0 ] ?NOFAIL (strdup (namespace)) :NULL ;list_add_tail (&s->list, &mod->exported_symbols);hash_add_symbol (s);@@ -658,17 +641,12 @@ static void handle_symbol(struct module *mod, struct elf_info *info,break ;default :if (sym->st_shndx == info->export_symbol_sec) { -const char *name; - -if (strstarts (symname,"__export_symbol_gpl." )) { - name = 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 = 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));break ; }@@ -2470,9 +2448,8 @@ static void read_dump(const char *fname) mod =new_module (modname,strlen (modname)); mod->from_dump = true; } - s =sym_add_exported (symname, mod, gpl_only); + s =sym_add_exported (symname, mod, gpl_only, namespace);sym_set_crc (s, crc); -sym_update_namespace (symname, namespace); }free (buf);return ; --2.32.0
Signature · 2 lines
Thanks, ~Nick Desaulniers
PATCH 7/7 modpost: use null string instead of NULL pointer for default namespace
In reply to Masahiro YamadaThe default namespace is the null string, "".
When set, the null string "" is converted to NULL:
s->namespace = 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.
---
scripts/mod/modpost.c | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-)
scripts/mod/modpost.c +11 -5
@@ -296,6 +296,13 @@ static bool contains_namespace(struct list_head *head, const char *namespace) {struct namespace_list *list; +/* +* 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;
@@ -371,7 +378,7 @@ static struct symbol *sym_add_exported(const char *name, struct module *mod, s =alloc_symbol (name); s->module = mod; s->is_gpl_only = gpl_only; - s->namespace = namespace[0 ] ?NOFAIL (strdup (namespace)) :NULL ; + s->namespace =NOFAIL (strdup (namespace));list_add_tail (&s->list, &mod->exported_symbols);hash_add_symbol (s);
@@ -2117,8 +2124,7 @@ static void check_exports(struct module *mod)else basename = mod->name; -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 it.\n" , basename, exp->name, exp->namespace);
@@ -2201,7 +2207,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_ENTRY(%s, \"%s\", \"%s\");\n" , sym->name, sym->is_gpl_only ?"_gpl" :"" , - sym->namespace ?:"" ); + sym->namespace);if (!modversions)return ;
@@ -2471,7 +2477,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);
Signature · 1 line
2.32.0
Re: [PATCH 7/7] modpost: use null string instead of NULL pointer for default namespace
Quoted 13 lines · Masahiro Yamada
The default namespace is the null string, "".
When set, the null string "" is converted to NULL:
s->namespace = 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.
Quoted 7 lines
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 <[email protected]>
Sorry for the late review. If this is still useful:
Feel free to ping me via mail if I'm falling behind. Otherwise you should join us on IRC. (#clangbuiltlinux on libera)
Quoted 63 lines
---
scripts/mod/modpost.c | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-)scripts/mod/modpost.c +11
-6@@ -296,6 +296,13 @@ static bool contains_namespace(struct list_head *head, const char *namespace) {struct namespace_list *list; +/* +* 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;@@ -371,7 +378,7 @@ static struct symbol *sym_add_exported(const char *name, struct module *mod, s =alloc_symbol (name); s->module = mod; s->is_gpl_only = gpl_only; - s->namespace = namespace[0 ] ?NOFAIL (strdup (namespace)) :NULL ; + s->namespace =NOFAIL (strdup (namespace));list_add_tail (&s->list, &mod->exported_symbols);hash_add_symbol (s);@@ -2117,8 +2124,7 @@ static void check_exports(struct module *mod)else basename = mod->name; -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 it.\n" , basename, exp->name, exp->namespace);@@ -2201,7 +2207,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_ENTRY(%s, \"%s\", \"%s\");\n" , sym->name, sym->is_gpl_only ?"_gpl" :"" , - sym->namespace ?:"" ); + sym->namespace);if (!modversions)return ;@@ -2471,7 +2477,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); --2.32.0
Signature · 2 lines
Thanks, ~Nick Desaulniers
Quoted 8 lines · Masahiro Yamada
This patch set refactors
EXPORT_SYMBOL, <linux/export.h> and <asm/export.h>.You can still put
EXPORT_SYMBOL()in *.S file, very close to the definition, but you do not need to care about whether it is a function or a data. RemoveEXPORT_DATA_SYMBOL().
Sorry, please ignore this patch set.
With further testing for ia64,
I realized it was not working.
Quoted 30 lines
Masahiro Yamada (7):
modpost: fix section mismatch check for exported init/exit sections
modpost: putget_secindex()call insidesec_name()
kbuild: generate structkernel_symbolby modpost ia64,export.h: replace EXPORT_DATA_SYMBOL* with EXPORT_SYMBOL*
checkpatch: warn if <asm/export.h> is included
modpost: mergesym_update_namespace()intosym_add_exported()
modpost: use null string instead of NULL pointer for default namespacearch/ia64/include/asm/Kbuild | 1 + arch/ia64/include/asm/export.h | 3 - arch/ia64/kernel/head.S | 2 +- arch/ia64/kernel/ivt.S | 2 +- include/asm-generic/export.h | 83 +------------------ include/linux/export-internal.h | 44 ++++++++++ include/linux/export.h | 97 ++++++++-------------- kernel/module/internal.h | 12 +++ kernel/module/main.c | 1 - scripts/Makefile.build | 8 +- scripts/check-local-export | 4 +- scripts/checkpatch.pl | 7 ++ scripts/mod/modpost.c | 139 +++++++++++++++++--------------- scripts/mod/modpost.h | 1 + 14 files changed, 182 insertions(+), 222 deletions(-) delete mode 100644 arch/ia64/include/asm/export.h--
2.32.0
Signature · 2 lines
Best Regards Masahiro Yamada