Speed up the symbols' resolution process V3
Subsystems (3)
cover Speed up the symbols' resolution process V3
The intent of this patch is to speed up the symbols resolution process.
This objective is achieved by sorting all ksymtab* and kcrctab* symbols (those which reside both in the kernel and in the modules) and thus use the fast binary search.
To avoid adding lots of code for symbols sorting I rely on the linker which can easily do the job thanks to a little trick. The trick isn't really beautiful to see but permits minimal changes to the code and build process. Indeed the patch is very simple and short.
In the first place I changed the code for place every symbol in a different section (for example: "___ksymtab" sec "__" #sym) at compile time (this the above mentioned trick!). Thus I request to the linker to sort and merge all these sections into the appropriate ones (for example: "__ksymtab") at link time using the linker scripts. Once all symbols are sorted we can use binary search instead of the linear one.
I'm fairly sure that this is a good speed improvement even though I haven't made any comprehensive benchmarking (but follow a simple one). In any case I would be very happy to receive suggestions about how made it. Collaterally, the boot time should be reduced also (proportionally to the number of modules and symbols nvolved at boot stage).
I hope that you find that interesting!
This work was supported by a hardware donation from the CE Linux Forum.
Thanks to Ian Lance Taylor for help about how the linker works.
Changes since V2:
*) Fix a bug in each_symbol() semantics by Anders Kaseorg *) Split the work in three patches as requested by Rusty Russell *) Add a generic binary search implementation made by Tim Abbott *) Remove CONFIG_SYMBOLS_BSEARCH kernel option
Changes since V1:
*) Merge all patches into only one
*) Remove few useless things
*) Introduce CONFIG_SYMBOLS_BSEARCH kernel option
Alessio Igor Bogani (3):
module: Split the find_symbol_in_section function
module: Sort exported symbols
module: Use the binary search for symbols resolution
Tim Abbott (1):
lib: Add generic binary search function to the kernel.
include/asm-generic/vmlinux.lds.h | 20 +++++++------- include/linux/bsearch.h | 9 ++++++ include/linux/module.h | 7 +++-- kernel/module.c | 37 ++++++++++++++++--------- lib/Makefile | 3 +- lib/bsearch.c | 53 +++++++++++++++++++++++++++++++++++++ scripts/module-common.lds | 11 +++++++ 7 files changed, 113 insertions(+), 27 deletions(-) create mode 100644 include/linux/bsearch.h create mode 100644 lib/bsearch.c
PATCH 1/4 module: Split the find_symbol_in_section function
Split the find_symbol_in_section() in two: the first one
(that is compare_symbol_in_section()) for compare values during the search and the second one (that is found_symbol_in_section()) executed when values will be found. Rename each_symbol() in search_symbol() to let its' users notice the change.
This work was supported by a hardware donation from the CE Linux Forum.
--- include/linux/module.h | 3 ++- kernel/module.c | 29 ++++++++++++++++++----------- 2 files changed, 20 insertions(+), 12 deletions(-)
include/linux/module.h +2 -1
@@ -475,7 +475,8 @@ const struct kernel_symbol *find_symbol(const char *name, bool warn);/* Walk the exported symbol table */ -bool each_symbol (bool (*fn)(const struct symsearch *arr,struct module *owner, +bool search_symbol (int (*cmp) (const void *va,const void *vb), +bool (*fn)(const struct symsearch *arr,struct module *owner,unsigned int symnum,void *data),void *data);/* Returns 0 and fills in value, defined and namebuf, or -ERANGE if
kernel/module.c +18 -11
@@ -238,6 +238,7 @@ extern const unsigned long __start___kcrctab_unused_gpl[]; static bool each_symbol_in_section(const struct symsearch *arr, unsigned int arrsize, struct module *owner, + int (*cmp) (const void *va, const void *vb), bool (*fn)(const struct symsearch *syms, struct module *owner, unsigned int symnum, void *data),
@@ -247,15 +248,16 @@ static bool each_symbol_in_section(const struct symsearch *arr,for (j =0 ; j < arrsize; j++) {for (i =0 ; i < arr[j].stop - arr[j].start; i++) -if (fn (&arr[j], owner, i, data)) -return true; +if (cmp (data, &arr[j].start[i]) ==0 ) +return fn (&arr[j], owner, i, data); }return false; }/* Returns true as soon as fn returns true, otherwise false. */ -bool each_symbol (bool (*fn)(const struct symsearch *arr,struct module *owner, +bool search_symbol (int (*cmp) (const void *va,const void *data), +bool (*fn)(const struct symsearch *arr,struct module *owner,unsigned int symnum,void *data),void *data) {struct module *mod;
@@ -278,7 +280,7 @@ bool each_symbol(bool (*fn)(const struct symsearch *arr, struct module *owner,#endif }; -if (each_symbol_in_section (arr,ARRAY_SIZE (arr),NULL , fn, data)) +if (each_symbol_in_section (arr,ARRAY_SIZE (arr),NULL , cmp, fn, data))return true;list_for_each_entry_rcu (mod, &modules, list) {
@@ -304,12 +306,12 @@ bool each_symbol(bool (*fn)(const struct symsearch *arr, struct module *owner,#endif }; -if (each_symbol_in_section (arr,ARRAY_SIZE (arr), mod, fn, data)) +if (each_symbol_in_section (arr,ARRAY_SIZE (arr), mod, cmp, fn, data))return true; }return false; } -EXPORT_SYMBOL_GPL (each_symbol); +EXPORT_SYMBOL_GPL (search_symbol);struct find_symbol_arg {/* Input */
@@ -323,15 +325,20 @@ struct find_symbol_arg {const struct kernel_symbol *sym; }; -static bool find_symbol_in_section (const struct symsearch *syms, +static int compare_symbol_in_section (const void *va,const void *vb) +{ +const struct find_symbol_arg *a; +const struct kernel_symbol *b; + a = va; b = vb; +return strcmp (a->name, b->name); +} + +static bool found_symbol_in_section (const struct symsearch *syms,struct module *owner,unsigned int symnum,void *data) {struct find_symbol_arg *fsa = data; -if (strcmp (syms->start[symnum].name, fsa->name) !=0 ) -return false; -if (!fsa->gplok) {if (syms->licence ==GPL_ONLY )return false;
@@ -379,7 +386,7 @@ const struct kernel_symbol *find_symbol(const char *name, fsa.gplok = gplok; fsa.warn = warn; -if (each_symbol (find_symbol_in_section, &fsa)) { +if (search_symbol (compare_symbol_in_section, found_symbol_in_section, &fsa)) {if (owner) *owner = fsa.owner;if (crc)
Signature · 1 line
1.7.0.4
PATCH 2/4 module: Sort exported symbols
In reply to Alessio Igor BoganiThis patch places every exported symbol in its own section (i.e. "___ksymtab__printk"). Thus the linker will use its SORT() directive to sort and finally merge all symbol in the right and final section (i.e. "__ksymtab").
This work was supported by a hardware donation from the CE Linux Forum.
--- include/asm-generic/vmlinux.lds.h | 20 ++++++++++---------- include/linux/module.h | 4 ++-- scripts/module-common.lds | 11 +++++++++++ 3 files changed, 23 insertions(+), 12 deletions(-)
include/asm-generic/vmlinux.lds.h +10 -10
@@ -274,70 +274,70 @@/* Kernel symbol table: Normal symbols */ \ __ksymtab : AT(ADDR(__ksymtab) - LOAD_OFFSET) { \ VMLINUX_SYMBOL(__start___ksymtab) = .; \ - *(__ksymtab ) \ + *(SORT (___ksymtab__ *)) \ VMLINUX_SYMBOL(__stop___ksymtab) = .; \ } \ \/* Kernel symbol table: GPL-only symbols */ \ __ksymtab_gpl : AT(ADDR(__ksymtab_gpl) - LOAD_OFFSET) { \ VMLINUX_SYMBOL(__start___ksymtab_gpl) = .; \ - *(__ksymtab_gpl ) \ + *(SORT (___ksymtab_gpl__ *)) \ VMLINUX_SYMBOL(__stop___ksymtab_gpl) = .; \ } \ \/* Kernel symbol table: Normal unused symbols */ \ __ksymtab_unused : AT(ADDR(__ksymtab_unused) - LOAD_OFFSET) { \ VMLINUX_SYMBOL(__start___ksymtab_unused) = .; \ - *(__ksymtab_unused ) \ + *(SORT (___ksymtab_unused__ *)) \ VMLINUX_SYMBOL(__stop___ksymtab_unused) = .; \ } \ \/* Kernel symbol table: GPL-only unused symbols */ \ __ksymtab_unused_gpl : AT(ADDR(__ksymtab_unused_gpl) - LOAD_OFFSET) { \ VMLINUX_SYMBOL(__start___ksymtab_unused_gpl) = .; \ - *(__ksymtab_unused_gpl ) \ + *(SORT (___ksymtab_unused_gpl__ *)) \ VMLINUX_SYMBOL(__stop___ksymtab_unused_gpl) = .; \ } \ \/* Kernel symbol table: GPL-future-only symbols */ \ __ksymtab_gpl_future : AT(ADDR(__ksymtab_gpl_future) - LOAD_OFFSET) { \ VMLINUX_SYMBOL(__start___ksymtab_gpl_future) = .; \ - *(__ksymtab_gpl_future ) \ + *(SORT (___ksymtab_gpl_future__ *)) \ VMLINUX_SYMBOL(__stop___ksymtab_gpl_future) = .; \ } \ \/* Kernel symbol table: Normal symbols */ \ __kcrctab : AT(ADDR(__kcrctab) - LOAD_OFFSET) { \ VMLINUX_SYMBOL(__start___kcrctab) = .; \ - *(__kcrctab ) \ + *(SORT (___kcrctab__ *)) \ VMLINUX_SYMBOL(__stop___kcrctab) = .; \ } \ \/* Kernel symbol table: GPL-only symbols */ \ __kcrctab_gpl : AT(ADDR(__kcrctab_gpl) - LOAD_OFFSET) { \ VMLINUX_SYMBOL(__start___kcrctab_gpl) = .; \ - *(__kcrctab_gpl ) \ + *(SORT (___kcrctab_gpl__ *)) \ VMLINUX_SYMBOL(__stop___kcrctab_gpl) = .; \ } \ \/* Kernel symbol table: Normal unused symbols */ \ __kcrctab_unused : AT(ADDR(__kcrctab_unused) - LOAD_OFFSET) { \ VMLINUX_SYMBOL(__start___kcrctab_unused) = .; \ - *(__kcrctab_unused ) \ + *(SORT (___kcrctab_unused__ *)) \ VMLINUX_SYMBOL(__stop___kcrctab_unused) = .; \ } \ \/* Kernel symbol table: GPL-only unused symbols */ \ __kcrctab_unused_gpl : AT(ADDR(__kcrctab_unused_gpl) - LOAD_OFFSET) { \ VMLINUX_SYMBOL(__start___kcrctab_unused_gpl) = .; \ - *(__kcrctab_unused_gpl ) \ + *(SORT (___kcrctab_unused_gpl__ *)) \ VMLINUX_SYMBOL(__stop___kcrctab_unused_gpl) = .; \ } \ \/* Kernel symbol table: GPL-future-only symbols */ \ __kcrctab_gpl_future : AT(ADDR(__kcrctab_gpl_future) - LOAD_OFFSET) { \ VMLINUX_SYMBOL(__start___kcrctab_gpl_future) = .; \ - *(__kcrctab_gpl_future ) \ + *(SORT (___kcrctab_gpl_future__ *)) \ VMLINUX_SYMBOL(__stop___kcrctab_gpl_future) = .; \ } \ \
include/linux/module.h +2 -2
@@ -223,7 +223,7 @@ struct module_use { extern void *__crc_##sym __attribute__((weak)); \ static const unsigned long __kcrctab_##sym \ __used \ - __attribute__((section("__kcrctab" sec), unused)) \ + __attribute__((section("___kcrctab" sec "__"#sym), unused)) \ = (unsigned long) &__crc_##sym;#else #define __CRC_SYMBOL (sym, sec)
@@ -238,7 +238,7 @@ struct module_use { = MODULE_SYMBOL_PREFIX #sym; \ static const struct kernel_symbol __ksymtab_##sym \ __used \ - __attribute__((section("__ksymtab" sec), unused)) \ + __attribute__((section("___ksymtab" sec "__"#sym), unused)) \ = { (unsigned long)&sym, __kstrtab_##sym }#define EXPORT_SYMBOL (sym) \
scripts/module-common.lds +11 -0
@@ -5,4 +5,15 @@ */ SECTIONS { /DISCARD/ : { *(.discard) } + + __ksymtab : { *(SORT(___ksymtab__*)) } + __ksymtab_gpl : { *(SORT(___ksymtab_gpl__*)) } + __ksymtab_unused : { *(SORT(___ksymtab_unused__*)) } + __ksymtab_unused_gpl : { *(SORT(___ksymtab_unused_gpl__*)) } + __ksymtab_gpl_future : { *(SORT(___ksymtab_gpl_future__*)) } + __kcrctab : { *(SORT(___kcrctab__*)) } + __kcrctab_gpl : { *(SORT(___kcrctab_gpl__*)) } + __kcrctab_unused : { *(SORT(___kcrctab_unused__*)) } + __kcrctab_unused_gpl : { *(SORT(___kcrctab_unused_gpl__*)) } + __kcrctab_gpl_future : { *(SORT(___kcrctab_gpl_future__*)) } }
Signature · 1 line
1.7.0.4
PATCH 3/4 lib: Add generic binary search function to the kernel.
In reply to Alessio Igor BoganiFrom: Tim Abbott <[email protected]>
There a large number hand-coded binary searches in the kernel (run "git grep search | grep binary" to find many of them). Since in my experience, hand-coding binary searches can be error-prone, it seems worth cleaning this up by providing a generic binary search function.
This generic binary search implementation comes from Ksplice. It has the same basic API as the C library bsearch() function. Ksplice uses it in half a dozen places with 4 different comparison functions, and I think our code is substantially cleaner because of this.
--- include/linux/bsearch.h | 9 ++++++++ lib/Makefile | 3 +- lib/bsearch.c | 53 +++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 64 insertions(+), 1 deletions(-) create mode 100644 include/linux/bsearch.h create mode 100644 lib/bsearch.c
include/linux/bsearch.h new file +9 -0
@@ -0,0 +1,9 @@ +#ifndef _LINUX_BSEARCH_H +#define _LINUX_BSEARCH_H + +#include <linux/types.h> + +void *bsearch (const void *key,const void *base,size_t num,size_t size, +int (*cmp)(const void *key,const void *elt)); + +#endif /* _LINUX_BSEARCH_H */
lib/Makefile +2 -1
@@ -21,7 +21,8 @@ lib-y += kobject.o kref.o klist.o obj-y += bcd.o div64.o sort.o parser.o halfmd4.o debug_locks.o random32.o \ bust_spinlocks.o hexdump.o kasprintf.o bitmap.o scatterlist.o \ - string_helpers.o gcd.o lcm.o list_sort.o uuid.o flex_array.o + string_helpers.o gcd.o lcm.o list_sort.o uuid.o flex_array.o \ + bsearch.o obj-y += kstrtox.o obj-$(CONFIG_TEST_KSTRTOX) += test-kstrtox.o
lib/bsearch.c new file +53 -0
53 lines not on this page · raw message
Signature · 1 line
1.7.0.4
PATCH 4/4 module: Use the binary search for symbols resolution
In reply to Alessio Igor BoganiTakes advantage of the order and locates symbols using binary search.
This work was supported by a hardware donation from the CE Linux Forum.
--- kernel/module.c | 12 ++++++++---- 1 files changed, 8 insertions(+), 4 deletions(-)
kernel/module.c +8 -4
@@ -57,6 +57,7 @@#include <linux/kmemleak.h> #include <linux/jump_label.h> #include <linux/pfn.h> +#include <linux/bsearch.h> #define CREATE_TRACE_POINTS #include <trace/events/module.h>
@@ -244,12 +245,15 @@ static bool each_symbol_in_section(const struct symsearch *arr, unsigned int symnum, void *data), void *data) { - unsigned int i, j; + unsigned int j; +const struct kernel_symbol *sym, *start; +size_t size =sizeof (struct kernel_symbol );for (j =0 ; j < arrsize; j++) { -for (i =0 ; i < arr[j].stop - arr[j].start; i++) -if (cmp (data, &arr[j].start[i]) ==0 ) -return fn (&arr[j], owner, i, data); + start = arr[j].start; + sym =bsearch (data, start, arr[j].stop - arr[j].start, size, cmp); +if (sym !=NULL ) +return fn (&arr[j], owner, sym - start, data); }return false;
Signature · 1 line
1.7.0.4