Skip to content

Commit 8b5fa6b

Browse files
author
Peter Zijlstra
committed
objtool: Optimize read_sections()
Perf showed that __hash_init() is a significant portion of read_sections(), so instead of doing a per section rela_hash, use an elf-wide rela_hash. Statistics show us there are about 1.1 million relas, so size it accordingly. This reduces the objtool on vmlinux.o runtime to a third, from 15 to 5 seconds. Signed-off-by: Peter Zijlstra (Intel) <[email protected]> Reviewed-by: Miroslav Benes <[email protected]> Acked-by: Josh Poimboeuf <[email protected]> Link: https://lkml.kernel.org/r/[email protected]
1 parent cdb3d05 commit 8b5fa6b

File tree

5 files changed

+47
-29
lines changed

5 files changed

+47
-29
lines changed

tools/objtool/check.c

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -569,8 +569,8 @@ static int add_jump_destinations(struct objtool_file *file)
569569
if (insn->ignore || insn->offset == FAKE_JUMP_OFFSET)
570570
continue;
571571

572-
rela = find_rela_by_dest_range(insn->sec, insn->offset,
573-
insn->len);
572+
rela = find_rela_by_dest_range(file->elf, insn->sec,
573+
insn->offset, insn->len);
574574
if (!rela) {
575575
dest_sec = insn->sec;
576576
dest_off = insn->offset + insn->len + insn->immediate;
@@ -666,8 +666,8 @@ static int add_call_destinations(struct objtool_file *file)
666666
if (insn->type != INSN_CALL)
667667
continue;
668668

669-
rela = find_rela_by_dest_range(insn->sec, insn->offset,
670-
insn->len);
669+
rela = find_rela_by_dest_range(file->elf, insn->sec,
670+
insn->offset, insn->len);
671671
if (!rela) {
672672
dest_off = insn->offset + insn->len + insn->immediate;
673673
insn->call_dest = find_func_by_offset(insn->sec, dest_off);
@@ -796,7 +796,7 @@ static int handle_group_alt(struct objtool_file *file,
796796
*/
797797
if ((insn->offset != special_alt->new_off ||
798798
(insn->type != INSN_CALL && !is_static_jump(insn))) &&
799-
find_rela_by_dest_range(insn->sec, insn->offset, insn->len)) {
799+
find_rela_by_dest_range(file->elf, insn->sec, insn->offset, insn->len)) {
800800

801801
WARN_FUNC("unsupported relocation in alternatives section",
802802
insn->sec, insn->offset);
@@ -1066,8 +1066,8 @@ static struct rela *find_jump_table(struct objtool_file *file,
10661066
break;
10671067

10681068
/* look for a relocation which references .rodata */
1069-
text_rela = find_rela_by_dest_range(insn->sec, insn->offset,
1070-
insn->len);
1069+
text_rela = find_rela_by_dest_range(file->elf, insn->sec,
1070+
insn->offset, insn->len);
10711071
if (!text_rela || text_rela->sym->type != STT_SECTION ||
10721072
!text_rela->sym->sec->rodata)
10731073
continue;
@@ -1096,7 +1096,7 @@ static struct rela *find_jump_table(struct objtool_file *file,
10961096
* should reference text in the same function as the original
10971097
* instruction.
10981098
*/
1099-
table_rela = find_rela_by_dest(table_sec, table_offset);
1099+
table_rela = find_rela_by_dest(file->elf, table_sec, table_offset);
11001100
if (!table_rela)
11011101
continue;
11021102
dest_insn = find_insn(file, table_rela->sym->sec, table_rela->addend);
@@ -1232,7 +1232,7 @@ static int read_unwind_hints(struct objtool_file *file)
12321232
for (i = 0; i < sec->len / sizeof(struct unwind_hint); i++) {
12331233
hint = (struct unwind_hint *)sec->data->d_buf + i;
12341234

1235-
rela = find_rela_by_dest(sec, i * sizeof(*hint));
1235+
rela = find_rela_by_dest(file->elf, sec, i * sizeof(*hint));
12361236
if (!rela) {
12371237
WARN("can't find rela for unwind_hints[%d]", i);
12381238
return -1;

tools/objtool/elf.c

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -212,26 +212,31 @@ struct symbol *find_symbol_by_name(struct elf *elf, const char *name)
212212
return NULL;
213213
}
214214

215-
struct rela *find_rela_by_dest_range(struct section *sec, unsigned long offset,
216-
unsigned int len)
215+
struct rela *find_rela_by_dest_range(struct elf *elf, struct section *sec,
216+
unsigned long offset, unsigned int len)
217217
{
218218
struct rela *rela;
219219
unsigned long o;
220220

221221
if (!sec->rela)
222222
return NULL;
223223

224-
for (o = offset; o < offset + len; o++)
225-
hash_for_each_possible(sec->rela->rela_hash, rela, hash, o)
226-
if (rela->offset == o)
224+
sec = sec->rela;
225+
226+
for (o = offset; o < offset + len; o++) {
227+
hash_for_each_possible(elf->rela_hash, rela, hash,
228+
sec_offset_hash(sec, o)) {
229+
if (rela->sec == sec && rela->offset == o)
227230
return rela;
231+
}
232+
}
228233

229234
return NULL;
230235
}
231236

232-
struct rela *find_rela_by_dest(struct section *sec, unsigned long offset)
237+
struct rela *find_rela_by_dest(struct elf *elf, struct section *sec, unsigned long offset)
233238
{
234-
return find_rela_by_dest_range(sec, offset, 1);
239+
return find_rela_by_dest_range(elf, sec, offset, 1);
235240
}
236241

237242
static int read_sections(struct elf *elf)
@@ -261,7 +266,6 @@ static int read_sections(struct elf *elf)
261266

262267
INIT_LIST_HEAD(&sec->symbol_list);
263268
INIT_LIST_HEAD(&sec->rela_list);
264-
hash_init(sec->rela_hash);
265269

266270
s = elf_getscn(elf->elf, i);
267271
if (!s) {
@@ -493,7 +497,7 @@ static int read_relas(struct elf *elf)
493497
}
494498

495499
list_add_tail(&rela->list, &sec->rela_list);
496-
hash_add(sec->rela_hash, &rela->hash, rela->offset);
500+
hash_add(elf->rela_hash, &rela->hash, rela_hash(rela));
497501
nr_rela++;
498502
}
499503
max_rela = max(max_rela, nr_rela);
@@ -526,6 +530,7 @@ struct elf *elf_read(const char *name, int flags)
526530
hash_init(elf->symbol_name_hash);
527531
hash_init(elf->section_hash);
528532
hash_init(elf->section_name_hash);
533+
hash_init(elf->rela_hash);
529534
INIT_LIST_HEAD(&elf->sections);
530535

531536
elf->fd = open(name, flags);
@@ -586,7 +591,6 @@ struct section *elf_create_section(struct elf *elf, const char *name,
586591

587592
INIT_LIST_HEAD(&sec->symbol_list);
588593
INIT_LIST_HEAD(&sec->rela_list);
589-
hash_init(sec->rela_hash);
590594

591595
s = elf_newscn(elf->elf);
592596
if (!s) {

tools/objtool/elf.h

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@ struct section {
3333
struct rb_root symbol_tree;
3434
struct list_head symbol_list;
3535
struct list_head rela_list;
36-
DECLARE_HASHTABLE(rela_hash, 16);
3736
struct section *base, *rela;
3837
struct symbol *sym;
3938
Elf_Data *data;
@@ -81,18 +80,32 @@ struct elf {
8180
DECLARE_HASHTABLE(symbol_name_hash, 20);
8281
DECLARE_HASHTABLE(section_hash, 16);
8382
DECLARE_HASHTABLE(section_name_hash, 16);
83+
DECLARE_HASHTABLE(rela_hash, 20);
8484
};
8585

86+
static inline u32 sec_offset_hash(struct section *sec, unsigned long offset)
87+
{
88+
u32 ol = offset, oh = offset >> 32, idx = sec->idx;
89+
90+
__jhash_mix(ol, oh, idx);
91+
92+
return ol;
93+
}
94+
95+
static inline u32 rela_hash(struct rela *rela)
96+
{
97+
return sec_offset_hash(rela->sec, rela->offset);
98+
}
8699

87100
struct elf *elf_read(const char *name, int flags);
88101
struct section *find_section_by_name(struct elf *elf, const char *name);
89102
struct symbol *find_func_by_offset(struct section *sec, unsigned long offset);
90103
struct symbol *find_symbol_by_offset(struct section *sec, unsigned long offset);
91104
struct symbol *find_symbol_by_name(struct elf *elf, const char *name);
92105
struct symbol *find_symbol_containing(struct section *sec, unsigned long offset);
93-
struct rela *find_rela_by_dest(struct section *sec, unsigned long offset);
94-
struct rela *find_rela_by_dest_range(struct section *sec, unsigned long offset,
95-
unsigned int len);
106+
struct rela *find_rela_by_dest(struct elf *elf, struct section *sec, unsigned long offset);
107+
struct rela *find_rela_by_dest_range(struct elf *elf, struct section *sec,
108+
unsigned long offset, unsigned int len);
96109
struct symbol *find_func_containing(struct section *sec, unsigned long offset);
97110
struct section *elf_create_section(struct elf *elf, const char *name, size_t
98111
entsize, int nr);

tools/objtool/orc_gen.c

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ int create_orc(struct objtool_file *file)
8181
return 0;
8282
}
8383

84-
static int create_orc_entry(struct section *u_sec, struct section *ip_relasec,
84+
static int create_orc_entry(struct elf *elf, struct section *u_sec, struct section *ip_relasec,
8585
unsigned int idx, struct section *insn_sec,
8686
unsigned long insn_off, struct orc_entry *o)
8787
{
@@ -109,9 +109,10 @@ static int create_orc_entry(struct section *u_sec, struct section *ip_relasec,
109109
rela->addend = insn_off;
110110
rela->type = R_X86_64_PC32;
111111
rela->offset = idx * sizeof(int);
112+
rela->sec = ip_relasec;
112113

113114
list_add_tail(&rela->list, &ip_relasec->rela_list);
114-
hash_add(ip_relasec->rela_hash, &rela->hash, rela->offset);
115+
hash_add(elf->rela_hash, &rela->hash, rela_hash(rela));
115116

116117
return 0;
117118
}
@@ -182,7 +183,7 @@ int create_orc_sections(struct objtool_file *file)
182183
if (!prev_insn || memcmp(&insn->orc, &prev_insn->orc,
183184
sizeof(struct orc_entry))) {
184185

185-
if (create_orc_entry(u_sec, ip_relasec, idx,
186+
if (create_orc_entry(file->elf, u_sec, ip_relasec, idx,
186187
insn->sec, insn->offset,
187188
&insn->orc))
188189
return -1;
@@ -194,7 +195,7 @@ int create_orc_sections(struct objtool_file *file)
194195

195196
/* section terminator */
196197
if (prev_insn) {
197-
if (create_orc_entry(u_sec, ip_relasec, idx,
198+
if (create_orc_entry(file->elf, u_sec, ip_relasec, idx,
198199
prev_insn->sec,
199200
prev_insn->offset + prev_insn->len,
200201
&empty))

tools/objtool/special.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ static int get_alt_entry(struct elf *elf, struct special_entry *entry,
118118
}
119119
}
120120

121-
orig_rela = find_rela_by_dest(sec, offset + entry->orig);
121+
orig_rela = find_rela_by_dest(elf, sec, offset + entry->orig);
122122
if (!orig_rela) {
123123
WARN_FUNC("can't find orig rela", sec, offset + entry->orig);
124124
return -1;
@@ -133,7 +133,7 @@ static int get_alt_entry(struct elf *elf, struct special_entry *entry,
133133
alt->orig_off = orig_rela->addend;
134134

135135
if (!entry->group || alt->new_len) {
136-
new_rela = find_rela_by_dest(sec, offset + entry->new);
136+
new_rela = find_rela_by_dest(elf, sec, offset + entry->new);
137137
if (!new_rela) {
138138
WARN_FUNC("can't find new rela",
139139
sec, offset + entry->new);

0 commit comments

Comments
 (0)