Skip to content

Commit 4074532

Browse files
Jack Brennenmasahir0y
authored andcommitted
modpost: Optimize symbol search from linear to binary search
Modify modpost to use binary search for converting addresses back into symbol references. Previously it used linear search. This change saves a few seconds of wall time for defconfig builds, but can save several minutes on allyesconfigs. Before: $ make LLVM=1 -j128 allyesconfig vmlinux -s KCFLAGS="-Wno-error" $ time scripts/mod/modpost -M -m -a -N -o vmlinux.symvers vmlinux.o 198.38user 1.27system 3:19.71elapsed After: $ make LLVM=1 -j128 allyesconfig vmlinux -s KCFLAGS="-Wno-error" $ time scripts/mod/modpost -M -m -a -N -o vmlinux.symvers vmlinux.o 11.91user 0.85system 0:12.78elapsed Signed-off-by: Jack Brennen <jbrennen@google.com> Tested-by: Nick Desaulniers <ndesaulniers@google.com> Signed-off-by: Masahiro Yamada <masahiroy@kernel.org>
1 parent 8a749fd commit 4074532

4 files changed

Lines changed: 232 additions & 66 deletions

File tree

scripts/mod/Makefile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ CFLAGS_REMOVE_empty.o += $(CC_FLAGS_LTO)
55
hostprogs-always-y += modpost mk_elfconfig
66
always-y += empty.o
77

8-
modpost-objs := modpost.o file2alias.o sumversion.o
8+
modpost-objs := modpost.o file2alias.o sumversion.o symsearch.o
99

1010
devicetable-offsets-file := devicetable-offsets.h
1111

@@ -16,7 +16,7 @@ targets += $(devicetable-offsets-file) devicetable-offsets.s
1616

1717
# dependencies on generated files need to be listed explicitly
1818

19-
$(obj)/modpost.o $(obj)/file2alias.o $(obj)/sumversion.o: $(obj)/elfconfig.h
19+
$(obj)/modpost.o $(obj)/file2alias.o $(obj)/sumversion.o $(obj)/symsearch.o: $(obj)/elfconfig.h
2020
$(obj)/file2alias.o: $(obj)/$(devicetable-offsets-file)
2121

2222
quiet_cmd_elfconfig = MKELF $@

scripts/mod/modpost.c

Lines changed: 6 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
#include <errno.h>
2323
#include "modpost.h"
2424
#include "../../include/linux/license.h"
25-
#include "../../include/linux/module_symbol.h"
2625

2726
static bool module_enabled;
2827
/* Are we using CONFIG_MODVERSIONS? */
@@ -577,11 +576,14 @@ static int parse_elf(struct elf_info *info, const char *filename)
577576
*p = TO_NATIVE(*p);
578577
}
579578

579+
symsearch_init(info);
580+
580581
return 1;
581582
}
582583

583584
static void parse_elf_finish(struct elf_info *info)
584585
{
586+
symsearch_finish(info);
585587
release_file(info->hdr, info->size);
586588
}
587589

@@ -1050,71 +1052,10 @@ static int secref_whitelist(const char *fromsec, const char *fromsym,
10501052
return 1;
10511053
}
10521054

1053-
/*
1054-
* If there's no name there, ignore it; likewise, ignore it if it's
1055-
* one of the magic symbols emitted used by current tools.
1056-
*
1057-
* Otherwise if find_symbols_between() returns those symbols, they'll
1058-
* fail the whitelist tests and cause lots of false alarms ... fixable
1059-
* only by merging __exit and __init sections into __text, bloating
1060-
* the kernel (which is especially evil on embedded platforms).
1061-
*/
1062-
static inline int is_valid_name(struct elf_info *elf, Elf_Sym *sym)
1063-
{
1064-
const char *name = elf->strtab + sym->st_name;
1065-
1066-
if (!name || !strlen(name))
1067-
return 0;
1068-
return !is_mapping_symbol(name);
1069-
}
1070-
1071-
/* Look up the nearest symbol based on the section and the address */
1072-
static Elf_Sym *find_nearest_sym(struct elf_info *elf, Elf_Addr addr,
1073-
unsigned int secndx, bool allow_negative,
1074-
Elf_Addr min_distance)
1075-
{
1076-
Elf_Sym *sym;
1077-
Elf_Sym *near = NULL;
1078-
Elf_Addr sym_addr, distance;
1079-
bool is_arm = (elf->hdr->e_machine == EM_ARM);
1080-
1081-
for (sym = elf->symtab_start; sym < elf->symtab_stop; sym++) {
1082-
if (get_secindex(elf, sym) != secndx)
1083-
continue;
1084-
if (!is_valid_name(elf, sym))
1085-
continue;
1086-
1087-
sym_addr = sym->st_value;
1088-
1089-
/*
1090-
* For ARM Thumb instruction, the bit 0 of st_value is set
1091-
* if the symbol is STT_FUNC type. Mask it to get the address.
1092-
*/
1093-
if (is_arm && ELF_ST_TYPE(sym->st_info) == STT_FUNC)
1094-
sym_addr &= ~1;
1095-
1096-
if (addr >= sym_addr)
1097-
distance = addr - sym_addr;
1098-
else if (allow_negative)
1099-
distance = sym_addr - addr;
1100-
else
1101-
continue;
1102-
1103-
if (distance <= min_distance) {
1104-
min_distance = distance;
1105-
near = sym;
1106-
}
1107-
1108-
if (min_distance == 0)
1109-
break;
1110-
}
1111-
return near;
1112-
}
1113-
11141055
static Elf_Sym *find_fromsym(struct elf_info *elf, Elf_Addr addr,
11151056
unsigned int secndx)
11161057
{
1117-
return find_nearest_sym(elf, addr, secndx, false, ~0);
1058+
return symsearch_find_nearest(elf, addr, secndx, false, ~0);
11181059
}
11191060

11201061
static Elf_Sym *find_tosym(struct elf_info *elf, Elf_Addr addr, Elf_Sym *sym)
@@ -1127,7 +1068,8 @@ static Elf_Sym *find_tosym(struct elf_info *elf, Elf_Addr addr, Elf_Sym *sym)
11271068
* Strive to find a better symbol name, but the resulting name may not
11281069
* match the symbol referenced in the original code.
11291070
*/
1130-
return find_nearest_sym(elf, addr, get_secindex(elf, sym), true, 20);
1071+
return symsearch_find_nearest(elf, addr, get_secindex(elf, sym),
1072+
true, 20);
11311073
}
11321074

11331075
static bool is_executable_section(struct elf_info *elf, unsigned int secndx)

scripts/mod/modpost.h

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#include <fcntl.h>
1111
#include <unistd.h>
1212
#include <elf.h>
13+
#include "../../include/linux/module_symbol.h"
1314

1415
#include "list.h"
1516
#include "elfconfig.h"
@@ -128,6 +129,8 @@ struct elf_info {
128129
* take shndx from symtab_shndx_start[N] instead */
129130
Elf32_Word *symtab_shndx_start;
130131
Elf32_Word *symtab_shndx_stop;
132+
133+
struct symsearch *symsearch;
131134
};
132135

133136
/* Accessor for sym->st_shndx, hides ugliness of "64k sections" */
@@ -154,6 +157,28 @@ static inline unsigned int get_secindex(const struct elf_info *info,
154157
return index;
155158
}
156159

160+
/*
161+
* If there's no name there, ignore it; likewise, ignore it if it's
162+
* one of the magic symbols emitted used by current tools.
163+
*
164+
* Internal symbols created by tools should be ignored by modpost.
165+
*/
166+
static inline int is_valid_name(struct elf_info *elf, Elf_Sym *sym)
167+
{
168+
const char *name = elf->strtab + sym->st_name;
169+
170+
if (!name || !strlen(name))
171+
return 0;
172+
return !is_mapping_symbol(name);
173+
}
174+
175+
/* symsearch.c */
176+
void symsearch_init(struct elf_info *elf);
177+
void symsearch_finish(struct elf_info *elf);
178+
Elf_Sym *symsearch_find_nearest(struct elf_info *elf, Elf_Addr addr,
179+
unsigned int secndx, bool allow_negative,
180+
Elf_Addr min_distance);
181+
157182
/* file2alias.c */
158183
void handle_moddevtable(struct module *mod, struct elf_info *info,
159184
Elf_Sym *sym, const char *symname);

scripts/mod/symsearch.c

Lines changed: 199 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,199 @@
1+
// SPDX-License-Identifier: GPL-2.0
2+
3+
/*
4+
* Helper functions for finding the symbol in an ELF which is "nearest"
5+
* to a given address.
6+
*/
7+
8+
#include "modpost.h"
9+
10+
struct syminfo {
11+
unsigned int symbol_index;
12+
unsigned int section_index;
13+
Elf_Addr addr;
14+
};
15+
16+
/*
17+
* Container used to hold an entire binary search table.
18+
* Entries in table are ascending, sorted first by section_index,
19+
* then by addr, and last by symbol_index. The sorting by
20+
* symbol_index is used to ensure predictable behavior when
21+
* multiple symbols are present with the same address; all
22+
* symbols past the first are effectively ignored, by eliding
23+
* them in symsearch_fixup().
24+
*/
25+
struct symsearch {
26+
unsigned int table_size;
27+
struct syminfo table[];
28+
};
29+
30+
static int syminfo_compare(const void *s1, const void *s2)
31+
{
32+
const struct syminfo *sym1 = s1;
33+
const struct syminfo *sym2 = s2;
34+
35+
if (sym1->section_index > sym2->section_index)
36+
return 1;
37+
if (sym1->section_index < sym2->section_index)
38+
return -1;
39+
if (sym1->addr > sym2->addr)
40+
return 1;
41+
if (sym1->addr < sym2->addr)
42+
return -1;
43+
if (sym1->symbol_index > sym2->symbol_index)
44+
return 1;
45+
if (sym1->symbol_index < sym2->symbol_index)
46+
return -1;
47+
return 0;
48+
}
49+
50+
static unsigned int symbol_count(struct elf_info *elf)
51+
{
52+
unsigned int result = 0;
53+
54+
for (Elf_Sym *sym = elf->symtab_start; sym < elf->symtab_stop; sym++) {
55+
if (is_valid_name(elf, sym))
56+
result++;
57+
}
58+
return result;
59+
}
60+
61+
/*
62+
* Populate the search array that we just allocated.
63+
* Be slightly paranoid here. The ELF file is mmap'd and could
64+
* conceivably change between symbol_count() and symsearch_populate().
65+
* If we notice any difference, bail out rather than potentially
66+
* propagating errors or crashing.
67+
*/
68+
static void symsearch_populate(struct elf_info *elf,
69+
struct syminfo *table,
70+
unsigned int table_size)
71+
{
72+
bool is_arm = (elf->hdr->e_machine == EM_ARM);
73+
74+
for (Elf_Sym *sym = elf->symtab_start; sym < elf->symtab_stop; sym++) {
75+
if (is_valid_name(elf, sym)) {
76+
if (table_size-- == 0)
77+
fatal("%s: size mismatch\n", __func__);
78+
table->symbol_index = sym - elf->symtab_start;
79+
table->section_index = get_secindex(elf, sym);
80+
table->addr = sym->st_value;
81+
82+
/*
83+
* For ARM Thumb instruction, the bit 0 of st_value is
84+
* set if the symbol is STT_FUNC type. Mask it to get
85+
* the address.
86+
*/
87+
if (is_arm && ELF_ST_TYPE(sym->st_info) == STT_FUNC)
88+
table->addr &= ~1;
89+
90+
table++;
91+
}
92+
}
93+
94+
if (table_size != 0)
95+
fatal("%s: size mismatch\n", __func__);
96+
}
97+
98+
/*
99+
* Do any fixups on the table after sorting.
100+
* For now, this just finds adjacent entries which have
101+
* the same section_index and addr, and it propagates
102+
* the first symbol_index over the subsequent entries,
103+
* so that only one symbol_index is seen for any given
104+
* section_index and addr. This ensures that whether
105+
* we're looking at an address from "above" or "below"
106+
* that we see the same symbol_index.
107+
* This does leave some duplicate entries in the table;
108+
* in practice, these are a small fraction of the
109+
* total number of entries, and they are harmless to
110+
* the binary search algorithm other than a few occasional
111+
* unnecessary comparisons.
112+
*/
113+
static void symsearch_fixup(struct syminfo *table, unsigned int table_size)
114+
{
115+
/* Don't look at index 0, it will never change. */
116+
for (unsigned int i = 1; i < table_size; i++) {
117+
if (table[i].addr == table[i - 1].addr &&
118+
table[i].section_index == table[i - 1].section_index) {
119+
table[i].symbol_index = table[i - 1].symbol_index;
120+
}
121+
}
122+
}
123+
124+
void symsearch_init(struct elf_info *elf)
125+
{
126+
unsigned int table_size = symbol_count(elf);
127+
128+
elf->symsearch = NOFAIL(malloc(sizeof(struct symsearch) +
129+
sizeof(struct syminfo) * table_size));
130+
elf->symsearch->table_size = table_size;
131+
132+
symsearch_populate(elf, elf->symsearch->table, table_size);
133+
qsort(elf->symsearch->table, table_size,
134+
sizeof(struct syminfo), syminfo_compare);
135+
136+
symsearch_fixup(elf->symsearch->table, table_size);
137+
}
138+
139+
void symsearch_finish(struct elf_info *elf)
140+
{
141+
free(elf->symsearch);
142+
elf->symsearch = NULL;
143+
}
144+
145+
/*
146+
* Find the syminfo which is in secndx and "nearest" to addr.
147+
* allow_negative: allow returning a symbol whose address is > addr.
148+
* min_distance: ignore symbols which are further away than this.
149+
*
150+
* Returns a pointer into the symbol table for success.
151+
* Returns NULL if no legal symbol is found within the requested range.
152+
*/
153+
Elf_Sym *symsearch_find_nearest(struct elf_info *elf, Elf_Addr addr,
154+
unsigned int secndx, bool allow_negative,
155+
Elf_Addr min_distance)
156+
{
157+
unsigned int hi = elf->symsearch->table_size;
158+
unsigned int lo = 0;
159+
struct syminfo *table = elf->symsearch->table;
160+
struct syminfo target;
161+
162+
target.addr = addr;
163+
target.section_index = secndx;
164+
target.symbol_index = ~0; /* compares greater than any actual index */
165+
while (hi > lo) {
166+
unsigned int mid = lo + (hi - lo) / 2; /* Avoids overflow */
167+
168+
if (syminfo_compare(&table[mid], &target) > 0)
169+
hi = mid;
170+
else
171+
lo = mid + 1;
172+
}
173+
174+
/*
175+
* table[hi], if it exists, is the first entry in the array which
176+
* lies beyond target. table[hi - 1], if it exists, is the last
177+
* entry in the array which comes before target, including the
178+
* case where it perfectly matches the section and the address.
179+
*
180+
* Note -- if the address we're looking up falls perfectly
181+
* in the middle of two symbols, this is written to always
182+
* prefer the symbol with the lower address.
183+
*/
184+
Elf_Sym *result = NULL;
185+
186+
if (allow_negative &&
187+
hi < elf->symsearch->table_size &&
188+
table[hi].section_index == secndx &&
189+
table[hi].addr - addr <= min_distance) {
190+
min_distance = table[hi].addr - addr;
191+
result = &elf->symtab_start[table[hi].symbol_index];
192+
}
193+
if (hi > 0 &&
194+
table[hi - 1].section_index == secndx &&
195+
addr - table[hi - 1].addr <= min_distance) {
196+
result = &elf->symtab_start[table[hi - 1].symbol_index];
197+
}
198+
return result;
199+
}

0 commit comments

Comments
 (0)