Skip to content

Commit 34f7c96

Browse files
Peter ZijlstraIngo Molnar
authored andcommitted
objtool: Optimize !vmlinux.o again
When doing kbuild tests to see if the objtool changes affected those I found that there was a measurable regression: pre post real 1m13.594 1m16.488s user 34m58.246s 35m23.947s sys 4m0.393s 4m27.312s Perf showed that for small files the increased hash-table sizes were a measurable difference. Since we already have -l "vmlinux" to distinguish between the modes, make it also use a smaller portion of the hash-tables. This flips it into a small win: real 1m14.143s user 34m49.292s sys 3m44.746s Signed-off-by: Peter Zijlstra (Intel) <[email protected]> Reviewed-by: Miroslav Benes <[email protected]> Reviewed-by: Alexandre Chartre <[email protected]> Acked-by: Josh Poimboeuf <[email protected]> Link: https://lkml.kernel.org/r/[email protected] Signed-off-by: Ingo Molnar <[email protected]>
1 parent c4a3393 commit 34f7c96

File tree

3 files changed

+52
-26
lines changed

3 files changed

+52
-26
lines changed

tools/objtool/elf.c

Lines changed: 43 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,22 @@ static inline u32 str_hash(const char *str)
2727
return jhash(str, strlen(str), 0);
2828
}
2929

30+
static inline int elf_hash_bits(void)
31+
{
32+
return vmlinux ? ELF_HASH_BITS : 16;
33+
}
34+
35+
#define elf_hash_add(hashtable, node, key) \
36+
hlist_add_head(node, &hashtable[hash_min(key, elf_hash_bits())])
37+
38+
static void elf_hash_init(struct hlist_head *table)
39+
{
40+
__hash_init(table, 1U << elf_hash_bits());
41+
}
42+
43+
#define elf_hash_for_each_possible(name, obj, member, key) \
44+
hlist_for_each_entry(obj, &name[hash_min(key, elf_hash_bits())], member)
45+
3046
static void rb_add(struct rb_root *tree, struct rb_node *node,
3147
int (*cmp)(struct rb_node *, const struct rb_node *))
3248
{
@@ -115,7 +131,7 @@ struct section *find_section_by_name(struct elf *elf, const char *name)
115131
{
116132
struct section *sec;
117133

118-
hash_for_each_possible(elf->section_name_hash, sec, name_hash, str_hash(name))
134+
elf_hash_for_each_possible(elf->section_name_hash, sec, name_hash, str_hash(name))
119135
if (!strcmp(sec->name, name))
120136
return sec;
121137

@@ -127,7 +143,7 @@ static struct section *find_section_by_index(struct elf *elf,
127143
{
128144
struct section *sec;
129145

130-
hash_for_each_possible(elf->section_hash, sec, hash, idx)
146+
elf_hash_for_each_possible(elf->section_hash, sec, hash, idx)
131147
if (sec->idx == idx)
132148
return sec;
133149

@@ -138,7 +154,7 @@ static struct symbol *find_symbol_by_index(struct elf *elf, unsigned int idx)
138154
{
139155
struct symbol *sym;
140156

141-
hash_for_each_possible(elf->symbol_hash, sym, hash, idx)
157+
elf_hash_for_each_possible(elf->symbol_hash, sym, hash, idx)
142158
if (sym->idx == idx)
143159
return sym;
144160

@@ -205,7 +221,7 @@ struct symbol *find_symbol_by_name(struct elf *elf, const char *name)
205221
{
206222
struct symbol *sym;
207223

208-
hash_for_each_possible(elf->symbol_name_hash, sym, name_hash, str_hash(name))
224+
elf_hash_for_each_possible(elf->symbol_name_hash, sym, name_hash, str_hash(name))
209225
if (!strcmp(sym->name, name))
210226
return sym;
211227

@@ -224,7 +240,7 @@ struct rela *find_rela_by_dest_range(struct elf *elf, struct section *sec,
224240
sec = sec->rela;
225241

226242
for_offset_range(o, offset, offset + len) {
227-
hash_for_each_possible(elf->rela_hash, rela, hash,
243+
elf_hash_for_each_possible(elf->rela_hash, rela, hash,
228244
sec_offset_hash(sec, o)) {
229245
if (rela->sec != sec)
230246
continue;
@@ -309,8 +325,8 @@ static int read_sections(struct elf *elf)
309325
sec->len = sec->sh.sh_size;
310326

311327
list_add_tail(&sec->list, &elf->sections);
312-
hash_add(elf->section_hash, &sec->hash, sec->idx);
313-
hash_add(elf->section_name_hash, &sec->name_hash, str_hash(sec->name));
328+
elf_hash_add(elf->section_hash, &sec->hash, sec->idx);
329+
elf_hash_add(elf->section_name_hash, &sec->name_hash, str_hash(sec->name));
314330
}
315331

316332
if (stats)
@@ -394,8 +410,8 @@ static int read_symbols(struct elf *elf)
394410
else
395411
entry = &sym->sec->symbol_list;
396412
list_add(&sym->list, entry);
397-
hash_add(elf->symbol_hash, &sym->hash, sym->idx);
398-
hash_add(elf->symbol_name_hash, &sym->name_hash, str_hash(sym->name));
413+
elf_hash_add(elf->symbol_hash, &sym->hash, sym->idx);
414+
elf_hash_add(elf->symbol_name_hash, &sym->name_hash, str_hash(sym->name));
399415
}
400416

401417
if (stats)
@@ -456,6 +472,14 @@ static int read_symbols(struct elf *elf)
456472
return -1;
457473
}
458474

475+
void elf_add_rela(struct elf *elf, struct rela *rela)
476+
{
477+
struct section *sec = rela->sec;
478+
479+
list_add_tail(&rela->list, &sec->rela_list);
480+
elf_hash_add(elf->rela_hash, &rela->hash, rela_hash(rela));
481+
}
482+
459483
static int read_relas(struct elf *elf)
460484
{
461485
struct section *sec;
@@ -503,8 +527,7 @@ static int read_relas(struct elf *elf)
503527
return -1;
504528
}
505529

506-
list_add_tail(&rela->list, &sec->rela_list);
507-
hash_add(elf->rela_hash, &rela->hash, rela_hash(rela));
530+
elf_add_rela(elf, rela);
508531
nr_rela++;
509532
}
510533
max_rela = max(max_rela, nr_rela);
@@ -531,15 +554,16 @@ struct elf *elf_read(const char *name, int flags)
531554
perror("malloc");
532555
return NULL;
533556
}
534-
memset(elf, 0, sizeof(*elf));
557+
memset(elf, 0, offsetof(struct elf, sections));
535558

536-
hash_init(elf->symbol_hash);
537-
hash_init(elf->symbol_name_hash);
538-
hash_init(elf->section_hash);
539-
hash_init(elf->section_name_hash);
540-
hash_init(elf->rela_hash);
541559
INIT_LIST_HEAD(&elf->sections);
542560

561+
elf_hash_init(elf->symbol_hash);
562+
elf_hash_init(elf->symbol_name_hash);
563+
elf_hash_init(elf->section_hash);
564+
elf_hash_init(elf->section_name_hash);
565+
elf_hash_init(elf->rela_hash);
566+
543567
elf->fd = open(name, flags);
544568
if (elf->fd == -1) {
545569
fprintf(stderr, "objtool: Can't open '%s': %s\n",
@@ -676,8 +700,8 @@ struct section *elf_create_section(struct elf *elf, const char *name,
676700
shstrtab->changed = true;
677701

678702
list_add_tail(&sec->list, &elf->sections);
679-
hash_add(elf->section_hash, &sec->hash, sec->idx);
680-
hash_add(elf->section_name_hash, &sec->name_hash, str_hash(sec->name));
703+
elf_hash_add(elf->section_hash, &sec->hash, sec->idx);
704+
elf_hash_add(elf->section_name_hash, &sec->name_hash, str_hash(sec->name));
681705

682706
return sec;
683707
}

tools/objtool/elf.h

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -70,17 +70,19 @@ struct rela {
7070
bool jump_table_start;
7171
};
7272

73+
#define ELF_HASH_BITS 20
74+
7375
struct elf {
7476
Elf *elf;
7577
GElf_Ehdr ehdr;
7678
int fd;
7779
char *name;
7880
struct list_head sections;
79-
DECLARE_HASHTABLE(symbol_hash, 20);
80-
DECLARE_HASHTABLE(symbol_name_hash, 20);
81-
DECLARE_HASHTABLE(section_hash, 16);
82-
DECLARE_HASHTABLE(section_name_hash, 16);
83-
DECLARE_HASHTABLE(rela_hash, 20);
81+
DECLARE_HASHTABLE(symbol_hash, ELF_HASH_BITS);
82+
DECLARE_HASHTABLE(symbol_name_hash, ELF_HASH_BITS);
83+
DECLARE_HASHTABLE(section_hash, ELF_HASH_BITS);
84+
DECLARE_HASHTABLE(section_name_hash, ELF_HASH_BITS);
85+
DECLARE_HASHTABLE(rela_hash, ELF_HASH_BITS);
8486
};
8587

8688
#define OFFSET_STRIDE_BITS 4
@@ -127,6 +129,7 @@ struct section *elf_create_rela_section(struct elf *elf, struct section *base);
127129
int elf_rebuild_rela_section(struct section *sec);
128130
int elf_write(struct elf *elf);
129131
void elf_close(struct elf *elf);
132+
void elf_add_rela(struct elf *elf, struct rela *rela);
130133

131134
#define for_each_sec(file, sec) \
132135
list_for_each_entry(sec, &file->elf->sections, list)

tools/objtool/orc_gen.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -130,8 +130,7 @@ static int create_orc_entry(struct elf *elf, struct section *u_sec, struct secti
130130
rela->offset = idx * sizeof(int);
131131
rela->sec = ip_relasec;
132132

133-
list_add_tail(&rela->list, &ip_relasec->rela_list);
134-
hash_add(elf->rela_hash, &rela->hash, rela_hash(rela));
133+
elf_add_rela(elf, rela);
135134

136135
return 0;
137136
}

0 commit comments

Comments
 (0)