scalable symbol flags with __kflagstab
Subsystems (4)
v5 cover scalable symbol flags with __kflagstab
This patch series implements a mechanism for scalable exported symbol flags using a separate section called __kflagstab. The series introduces __kflagstab support, removes *_gpl sections in favor of a GPL flag, simplifies symbol resolution during module loading.
The __kflagstab contains an 8-bit bitset which can represent up to 8 boolean flags per symbol exported in the __ksymtab. The patch series also uses this bitset to store GPL-only flag values for kernel symbols, thereby eliminating the need for *_gpl sections for representing GPL only symbols.
Petr Pavlu ran a small test to get a better understanding of the different section sizes resulting from this patch series. He used v6.17-rc6 together with the openSUSE x86_64 config [1], which is fairly large. The resulting vmlinux.bin (no debuginfo) had an on-disk size of 58 MiB, and included 5937 + 6589 (GPL-only) exported symbols.
The following table summarizes his measurements and calculations regarding the sizes of all sections related to exported symbols:
| HAVE_ARCH_PREL32_RELOCATIONS | !HAVE_ARCH_PREL32_RELOCATIONS Section | Base [B] | Ext. [B] | Sep. [B] | Base [B] | Ext. [B] | Sep. [B] ---------------------------------------------------------------------------------------- __ksymtab | 71244 | 200416 | 150312 | 142488 | 400832 | 300624 __ksymtab_gpl | 79068 | NA | NA | 158136 | NA | NA __kcrctab | 23748 | 50104 | 50104 | 23748 | 50104 | 50104 __kcrctab_gpl | 26356 | NA | NA | 26356 | NA | NA __ksymtab_strings | 253628 | 253628 | 253628 | 253628 | 253628 | 253628 __kflagstab | NA | NA | 12526 | NA | NA | 12526 ---------------------------------------------------------------------------------------- Total | 454044 | 504148 | 466570 | 604356 | 704564 | 616882
Increase to base [%] | NA | 11.0 | 2.8 | NA | 16.6 | 2.1
The column "HAVE_ARCH_PREL32_RELOCATIONS -> Base" contains themeasured numbers. The rest of the values are calculated. The "Ext." column represents an alternative approach of extending __ksymtab to include a bitset of symbol flags, and the "Sep." column represents the approach of having a separate __kflagstab. With HAVE_ARCH_PREL32_RELOCATIONS, each kernel_symbol is 12 B in size and is extended to 16 B. With !HAVE_ARCH_PREL32_RELOCATIONS, it is 24 B, extended to 32 B. Note that this does not include the metadata needed to relocate __ksymtab*, which is freed after the initial processing.
The base export data in this case totals 0.43 MiB. About 50% is used for storing the names of exported symbols.
Adding __kflagstab as a separate section has a negligible impact, as expected. When extending __ksymtab (kernel_symbol) instead, the worst case with !HAVE_ARCH_PREL32_RELOCATIONS increases the export data size by 16.6%. Note that the larger increase in size for the latter approach is due to 4-byte alignment of kernel_symbol data structure, instead of 1-byte alignment for the flags bitset in __kflagstab in the former approach.
Based on the above, it was concluded that introducing __kflagstab makes senses, as the added complexity is minimal over extending kernel_symbol, and there is overall simplification of symbol finding logic in the module loader.
Thank you Petr Pavlu for doing a section size analysis as well as Sami Tolvanen, Petr Pavlu and Jonathan Corbet for their valuable feedback.
---
Changes from v4:
- squashed patches #4 and #5 to fix a bisecting issue
v4:
https://lore.kernel.org/r/[email protected]
Changes from v3:
- made commit messages more descriptive
v3:
https://lore.kernel.org/[email protected]/
Changes from v2:
- dropped symbol import protection to spin off into its own series
v2:
https://lore.kernel.org/[email protected]/
Changes from v1:
- added a check to ensure __kflagstab is present
- added warnings for the obsolete *_gpl sections
- moved protected symbol check before ref_module() call
- moved protected symbol check failure warning to issue detection point
v1:
https://lore.kernel.org/[email protected]/
---
Siddharth Nayyar (7):
module: define ksym_flags enumeration to represent kernel symbol flags
module: add kflagstab section to vmlinux and modules
module: populate kflagstab in modpost
module: use kflagstab instead of *_gpl sections
module: deprecate usage of *_gpl sections in module loader
module: remove *_gpl sections from vmlinux and modules
documentation: remove references to *_gpl sections
Documentation/kbuild/modules.rst | 11 +++-- include/asm-generic/vmlinux.lds.h | 21 +++----- include/linux/export-internal.h | 28 +++++++---- include/linux/module.h | 4 +- include/linux/module_symbol.h | 5 ++ kernel/module/internal.h | 4 +- kernel/module/main.c | 101 ++++++++++++++++++-------------------- scripts/mod/modpost.c | 16 ++++-- scripts/module.lds.S | 3 +- 9 files changed, 98 insertions(+), 95 deletions(-)
---
base-commit: 0138af2472dfdef0d56fc4697416eaa0ff2589bd
change-id: 20260305-kflagstab-51a08efed244
Best regards,
Signature · 1 line
Siddharth Nayyar <[email protected]>
PATCH v5 1/7 module: define ksym_flags enumeration to represent kernel symbol flags
Symbol flags is an enumeration used to represent flags as a bitset, for example a flag to tell if a symbol is GPL only.
The said bitset is introduced in subsequent patches and will contain values of kernel symbol flags. These bitset will then be used to infer flag values rather than fragmenting ksymtab for separating symbols with different flag values, thereby eliminating the need to fragment the ksymtab.
--- include/linux/module_symbol.h | 5 +++++ 1 file changed, 5 insertions(+)
include/linux/module_symbol.h +5 -0
@@ -2,6 +2,11 @@#ifndef _LINUX_MODULE_SYMBOL_H#define _LINUX_MODULE_SYMBOL_H +/* Kernel symbol flags bitset. */ +enum ksym_flags { +KSYM_FLAG_GPL_ONLY =1 <<0 , +}; +/* This ignores the intensely annoying "mapping symbols" found in ELF files. */ static inline bool is_mapping_symbol (const char *str) {
Signature · 1 line
2.53.0.1018.g2bb0e51243-goog
PATCH v5 2/7 module: add kflagstab section to vmlinux and modules
In reply to Siddharth NayyarThis section will contain read-only data for values of kernel symbol flags in the form of an 8-bit bitsets for each kernel symbol. Each bit in the bitset represents a flag value defined by ksym_flags enumeration.
The kflagstab section introduces a 1-byte overhead for each symbol exported in the ksymtab. Given that typical kernel builds contain roughly a few thousand exported symbols, the resulting memory increase is negligible.
--- include/asm-generic/vmlinux.lds.h | 7 +++++++ scripts/module.lds.S | 1 + 2 files changed, 8 insertions(+)
include/asm-generic/vmlinux.lds.h +7 -0
@@ -536,6 +536,13 @@ __stop___kcrctab_gpl = .; \ } \ \ +/* Kernel symbol flags table */ \ + __kflagstab : AT(ADDR(__kflagstab) - LOAD_OFFSET) { \ + __start___kflagstab = .; \ + KEEP(*(SORT(___kflagstab+*))) \ + __stop___kflagstab = .; \ + } \ + \/* Kernel symbol table: strings */ \ __ksymtab_strings : AT(ADDR(__ksymtab_strings) - LOAD_OFFSET) { \ *(__ksymtab_strings) \
scripts/module.lds.S +1 -0
@@ -23,6 +23,7 @@ SECTIONS { __ksymtab_gpl 0 : ALIGN(8) { *(SORT(___ksymtab_gpl+*)) } __kcrctab 0 : ALIGN(4) { *(SORT(___kcrctab+*)) } __kcrctab_gpl 0 : ALIGN(4) { *(SORT(___kcrctab_gpl+*)) } + __kflagstab 0 : ALIGN(1) { *(SORT(___kflagstab+*)) } .ctors 0 : ALIGN(8) { *(SORT(.ctors.*)) *(.ctors) } .init_array 0 : ALIGN(8) { *(SORT(.init_array.*)) *(.init_array) }
Signature · 1 line
2.53.0.1018.g2bb0e51243-goog
PATCH v5 3/7 module: populate kflagstab in modpost
In reply to Siddharth NayyarThis patch adds the ability to create entries for kernel symbol flag bitsets in kflagstab. Modpost populates only the GPL-only flag for now.
--- include/linux/export-internal.h | 7 +++++++ scripts/mod/modpost.c | 8 ++++++++ 2 files changed, 15 insertions(+)
include/linux/export-internal.h +7 -0
@@ -69,4 +69,11 @@ ".long " #crc "\n" \ ".previous" "\n") +#define SYMBOL_FLAGS (sym, flags) \ + asm(" .section \"___kflagstab+" #sym "\",\"a\"" "\n" \ + "__flags_" #sym ":" "\n" \ + " .byte " #flags "\n" \ + " .previous" "\n" \ + ) +#endif /* __LINUX_EXPORT_INTERNAL_H__ */
scripts/mod/modpost.c +8 -0
@@ -244,6 +244,11 @@ static struct symbol *alloc_symbol(const char *name)return s; } +static uint8_t get_symbol_flags (const struct symbol *sym) +{ +return sym->is_gpl_only ?KSYM_FLAG_GPL_ONLY :0 ; +} +/* For the hash of exported symbols */ static void hash_add_symbol (struct symbol *sym) {
@@ -1874,6 +1879,9 @@ static void add_exported_symbols(struct buffer *buf, struct module *mod)buf_printf (buf,"KSYMTAB_%s(%s, \"%s\", \"%s\");\n" , sym->is_func ?"FUNC" :"DATA" , sym->name, sym->is_gpl_only ?"_gpl" :"" , sym->namespace); + +buf_printf (buf,"SYMBOL_FLAGS(%s, 0x%02x);\n" , + sym->name,get_symbol_flags (sym)); }if (!modversions)
Signature · 1 line
2.53.0.1018.g2bb0e51243-goog
PATCH v5 4/7 module: use kflagstab instead of *_gpl sections
In reply to Siddharth NayyarRead kflagstab section for vmlinux and modules to determine whether kernel symbols are GPL only.
This patch eliminates the need for fragmenting the ksymtab for infering the value of GPL-only symbol flag, henceforth stop using the *_gpl versions of the ksymtab and kcrctab in modpost.
--- include/linux/export-internal.h | 21 ++++++++-------- include/linux/module.h | 1 + kernel/module/internal.h | 1 + kernel/module/main.c | 55 ++++++++++++++++++++++------------------- scripts/mod/modpost.c | 8 +++--- 5 files changed, 46 insertions(+), 40 deletions(-)
include/linux/export-internal.h +11 -10
@@ -37,14 +37,14 @@* 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) \ +#define __KSYMTAB (name, sym, 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" \ + " .section \"___ksymtab+" #name "\", \"a\"" "\n" \ __KSYM_ALIGN "\n" \ "__ksymtab_" #name ":" "\n" \ __KSYM_REF(sym) "\n" \
@@ -59,15 +59,16 @@#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 KSYMTAB_FUNC (name, ns) __KSYMTAB(name, KSYM_FUNC(name), ns) +#define KSYMTAB_DATA (name, ns) __KSYMTAB(name, name, ns) -#define SYMBOL_CRC (sym, crc, sec) \ - asm(".section \"___kcrctab" sec "+" #sym "\",\"a\"" "\n" \ - ".balign 4" "\n" \ - "__crc_" #sym ":" "\n" \ - ".long " #crc "\n" \ - ".previous" "\n") +#define SYMBOL_CRC (sym, crc) \ + asm(" .section \"___kcrctab+" #sym "\",\"a\"" "\n" \ + " .balign 4" "\n" \ + "__crc_" #sym ":" "\n" \ + " .long " #crc "\n" \ + " .previous" "\n" \ + )#define SYMBOL_FLAGS (sym, flags) \ asm(" .section \"___kflagstab+" #sym "\",\"a\"" "\n" \
include/linux/module.h +1 -0
@@ -418,6 +418,7 @@ struct module {/* Exported symbols */ const struct kernel_symbol *syms;const u32 *crcs; +const u8 *flagstab;unsigned int num_syms;#ifdef CONFIG_ARCH_USES_CFI_TRAPS
kernel/module/internal.h +1 -0
@@ -57,6 +57,7 @@ extern const struct kernel_symbol __start___ksymtab_gpl[];extern const struct kernel_symbol __stop___ksymtab_gpl[];extern const u32 __start___kcrctab[];extern const u32 __start___kcrctab_gpl[]; +extern const u8 __start___kflagstab[];#define KMOD_PATH_LEN 256extern char modprobe_path[];
kernel/module/main.c +29 -26
118 lines not on this page · raw message
scripts/mod/modpost.c +4 -4
@@ -1876,9 +1876,9 @@ static void add_exported_symbols(struct buffer *buf, struct module *mod)if (trim_unused_exports && !sym->used)continue ; -buf_printf (buf,"KSYMTAB_%s(%s, \"%s\", \"%s\");\n" , +buf_printf (buf,"KSYMTAB_%s(%s, \"%s\");\n" , sym->is_func ?"FUNC" :"DATA" , sym->name, - sym->is_gpl_only ?"_gpl" :"" , sym->namespace); + sym->namespace);buf_printf (buf,"SYMBOL_FLAGS(%s, 0x%02x);\n" , sym->name,get_symbol_flags (sym));
@@ -1899,8 +1899,8 @@ static void add_exported_symbols(struct buffer *buf, struct module *mod) sym->name, mod->name, mod->is_vmlinux ?"" :".ko" , sym->name); -buf_printf (buf,"SYMBOL_CRC(%s, 0x%08x, \"%s\");\n" , - sym->name, sym->crc, sym->is_gpl_only ?"_gpl" :"" ); +buf_printf (buf,"SYMBOL_CRC(%s, 0x%08x);\n" , + sym->name, sym->crc); } }
Signature · 1 line
2.53.0.1018.g2bb0e51243-goog
PATCH v5 5/7 module: deprecate usage of *_gpl sections in module loader
In reply to Siddharth NayyarThe *_gpl section are not being used populated by modpost anymore. Hence the module loader doesn't need to find and process these sections in modules.
This patch also simplifies symbol finding logic in module loader since *_gpl sections don't have to be searched anymore.
--- include/linux/module.h | 3 --- kernel/module/internal.h | 3 --- kernel/module/main.c | 46 ++++++++++++++++++---------------------------- 3 files changed, 18 insertions(+), 34 deletions(-)
include/linux/module.h +0 -3
@@ -434,9 +434,6 @@ struct module {unsigned int num_kp;/* GPL-only exported symbols. */ -unsigned int num_gpl_syms; -const struct kernel_symbol *gpl_syms; -const u32 *gpl_crcs;bool using_gplonly_symbols;#ifdef CONFIG_MODULE_SIG
kernel/module/internal.h +0 -3
@@ -53,10 +53,7 @@ extern const size_t modinfo_attrs_count;/* Provided by the linker */ extern const struct kernel_symbol __start___ksymtab[];extern const struct kernel_symbol __stop___ksymtab[]; -extern const struct kernel_symbol __start___ksymtab_gpl[]; -extern const struct kernel_symbol __stop___ksymtab_gpl[];extern const u32 __start___kcrctab[]; -extern const u32 __start___kcrctab_gpl[];extern const u8 __start___kflagstab[];#define KMOD_PATH_LEN 256
kernel/module/main.c +18 -28
67 lines not on this page · raw message
Signature · 1 line
2.53.0.1018.g2bb0e51243-goog
PATCH v5 6/7 module: remove *_gpl sections from vmlinux and modules
In reply to Siddharth NayyarThese sections are not used anymore and can be removed from vmlinux and modules during linking.
--- include/asm-generic/vmlinux.lds.h | 18 ++---------------- scripts/module.lds.S | 2 -- 2 files changed, 2 insertions(+), 18 deletions(-)
include/asm-generic/vmlinux.lds.h +2 -16
@@ -508,34 +508,20 @@ \PRINTK_INDEX \ \ -/* Kernel symbol table: Normal symbols */ \ +/* Kernel symbol table */ \ __ksymtab : AT(ADDR(__ksymtab) - LOAD_OFFSET) { \ __start___ksymtab = .; \ KEEP(*(SORT(___ksymtab+*))) \ __stop___ksymtab = .; \ } \ \ -/* Kernel symbol table: GPL-only symbols */ \ - __ksymtab_gpl : AT(ADDR(__ksymtab_gpl) - LOAD_OFFSET) { \ - __start___ksymtab_gpl = .; \ - KEEP(*(SORT(___ksymtab_gpl+*))) \ - __stop___ksymtab_gpl = .; \ - } \ - \ -/* Kernel symbol table: Normal symbols */ \ +/* Kernel symbol CRC table */ \ __kcrctab : AT(ADDR(__kcrctab) - LOAD_OFFSET) { \ __start___kcrctab = .; \ KEEP(*(SORT(___kcrctab+*))) \ __stop___kcrctab = .; \ } \ \ -/* Kernel symbol table: GPL-only symbols */ \ - __kcrctab_gpl : AT(ADDR(__kcrctab_gpl) - LOAD_OFFSET) { \ - __start___kcrctab_gpl = .; \ - KEEP(*(SORT(___kcrctab_gpl+*))) \ - __stop___kcrctab_gpl = .; \ - } \ - \/* Kernel symbol flags table */ \ __kflagstab : AT(ADDR(__kflagstab) - LOAD_OFFSET) { \ __start___kflagstab = .; \
scripts/module.lds.S +0 -2
@@ -20,9 +20,7 @@ SECTIONS { } __ksymtab 0 : ALIGN(8) { *(SORT(___ksymtab+*)) } - __ksymtab_gpl 0 : ALIGN(8) { *(SORT(___ksymtab_gpl+*)) } __kcrctab 0 : ALIGN(4) { *(SORT(___kcrctab+*)) } - __kcrctab_gpl 0 : ALIGN(4) { *(SORT(___kcrctab_gpl+*)) } __kflagstab 0 : ALIGN(1) { *(SORT(___kflagstab+*)) } .ctors 0 : ALIGN(8) { *(SORT(.ctors.*)) *(.ctors) }
Signature · 1 line
2.53.0.1018.g2bb0e51243-goog
PATCH v5 7/7 documentation: remove references to *_gpl sections
In reply to Siddharth Nayyar*_gpl sections are no longer present in the kernel binary.
--- Documentation/kbuild/modules.rst | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-)
Documentation/kbuild/modules.rst +6 -5
@@ -426,11 +426,12 @@ Symbols From the Kernel (vmlinux + modules) Version Information Formats --------------------------- - Exported symbols have information stored in __ksymtab or __ksymtab_gpl - sections. Symbol names and namespaces are stored in __ksymtab_strings, - using a format similar to the string table used for ELF. If - CONFIG_MODVERSIONS is enabled, the CRCs corresponding to exported - symbols will be added to the __kcrctab or __kcrctab_gpl. + Exported symbols have information stored in the __ksymtab and + __kflagstab sections. Symbol names and namespaces are stored in + __ksymtab_strings section, using a format similar to the string + table used for ELF. If CONFIG_MODVERSIONS is enabled, the CRCs + corresponding to exported symbols will be added to the + __kcrctab section. If CONFIG_BASIC_MODVERSIONS is enabled (default with CONFIG_MODVERSIONS), imported symbols will have their symbol name and
Signature · 1 line
2.53.0.1018.g2bb0e51243-goog