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) <peterz@infradead.org>
Reviewed-by: Miroslav Benes <mbenes@suse.cz>
Acked-by: Josh Poimboeuf <jpoimboe@redhat.com>
Link: https://lkml.kernel.org/r/20200324160924.739153726@infradead.org
This commit is contained in:
Peter Zijlstra 2020-03-12 11:23:36 +01:00
parent cdb3d057a1
commit 8b5fa6bc32
5 changed files with 47 additions and 29 deletions

View File

@ -569,8 +569,8 @@ static int add_jump_destinations(struct objtool_file *file)
if (insn->ignore || insn->offset == FAKE_JUMP_OFFSET) if (insn->ignore || insn->offset == FAKE_JUMP_OFFSET)
continue; continue;
rela = find_rela_by_dest_range(insn->sec, insn->offset, rela = find_rela_by_dest_range(file->elf, insn->sec,
insn->len); insn->offset, insn->len);
if (!rela) { if (!rela) {
dest_sec = insn->sec; dest_sec = insn->sec;
dest_off = insn->offset + insn->len + insn->immediate; dest_off = insn->offset + insn->len + insn->immediate;
@ -666,8 +666,8 @@ static int add_call_destinations(struct objtool_file *file)
if (insn->type != INSN_CALL) if (insn->type != INSN_CALL)
continue; continue;
rela = find_rela_by_dest_range(insn->sec, insn->offset, rela = find_rela_by_dest_range(file->elf, insn->sec,
insn->len); insn->offset, insn->len);
if (!rela) { if (!rela) {
dest_off = insn->offset + insn->len + insn->immediate; dest_off = insn->offset + insn->len + insn->immediate;
insn->call_dest = find_func_by_offset(insn->sec, dest_off); insn->call_dest = find_func_by_offset(insn->sec, dest_off);
@ -796,7 +796,7 @@ static int handle_group_alt(struct objtool_file *file,
*/ */
if ((insn->offset != special_alt->new_off || if ((insn->offset != special_alt->new_off ||
(insn->type != INSN_CALL && !is_static_jump(insn))) && (insn->type != INSN_CALL && !is_static_jump(insn))) &&
find_rela_by_dest_range(insn->sec, insn->offset, insn->len)) { find_rela_by_dest_range(file->elf, insn->sec, insn->offset, insn->len)) {
WARN_FUNC("unsupported relocation in alternatives section", WARN_FUNC("unsupported relocation in alternatives section",
insn->sec, insn->offset); insn->sec, insn->offset);
@ -1066,8 +1066,8 @@ static struct rela *find_jump_table(struct objtool_file *file,
break; break;
/* look for a relocation which references .rodata */ /* look for a relocation which references .rodata */
text_rela = find_rela_by_dest_range(insn->sec, insn->offset, text_rela = find_rela_by_dest_range(file->elf, insn->sec,
insn->len); insn->offset, insn->len);
if (!text_rela || text_rela->sym->type != STT_SECTION || if (!text_rela || text_rela->sym->type != STT_SECTION ||
!text_rela->sym->sec->rodata) !text_rela->sym->sec->rodata)
continue; continue;
@ -1096,7 +1096,7 @@ static struct rela *find_jump_table(struct objtool_file *file,
* should reference text in the same function as the original * should reference text in the same function as the original
* instruction. * instruction.
*/ */
table_rela = find_rela_by_dest(table_sec, table_offset); table_rela = find_rela_by_dest(file->elf, table_sec, table_offset);
if (!table_rela) if (!table_rela)
continue; continue;
dest_insn = find_insn(file, table_rela->sym->sec, table_rela->addend); 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)
for (i = 0; i < sec->len / sizeof(struct unwind_hint); i++) { for (i = 0; i < sec->len / sizeof(struct unwind_hint); i++) {
hint = (struct unwind_hint *)sec->data->d_buf + i; hint = (struct unwind_hint *)sec->data->d_buf + i;
rela = find_rela_by_dest(sec, i * sizeof(*hint)); rela = find_rela_by_dest(file->elf, sec, i * sizeof(*hint));
if (!rela) { if (!rela) {
WARN("can't find rela for unwind_hints[%d]", i); WARN("can't find rela for unwind_hints[%d]", i);
return -1; return -1;

View File

@ -212,8 +212,8 @@ struct symbol *find_symbol_by_name(struct elf *elf, const char *name)
return NULL; return NULL;
} }
struct rela *find_rela_by_dest_range(struct section *sec, unsigned long offset, struct rela *find_rela_by_dest_range(struct elf *elf, struct section *sec,
unsigned int len) unsigned long offset, unsigned int len)
{ {
struct rela *rela; struct rela *rela;
unsigned long o; unsigned long o;
@ -221,17 +221,22 @@ struct rela *find_rela_by_dest_range(struct section *sec, unsigned long offset,
if (!sec->rela) if (!sec->rela)
return NULL; return NULL;
for (o = offset; o < offset + len; o++) sec = sec->rela;
hash_for_each_possible(sec->rela->rela_hash, rela, hash, o)
if (rela->offset == o) for (o = offset; o < offset + len; o++) {
hash_for_each_possible(elf->rela_hash, rela, hash,
sec_offset_hash(sec, o)) {
if (rela->sec == sec && rela->offset == o)
return rela; return rela;
}
}
return NULL; return NULL;
} }
struct rela *find_rela_by_dest(struct section *sec, unsigned long offset) struct rela *find_rela_by_dest(struct elf *elf, struct section *sec, unsigned long offset)
{ {
return find_rela_by_dest_range(sec, offset, 1); return find_rela_by_dest_range(elf, sec, offset, 1);
} }
static int read_sections(struct elf *elf) static int read_sections(struct elf *elf)
@ -261,7 +266,6 @@ static int read_sections(struct elf *elf)
INIT_LIST_HEAD(&sec->symbol_list); INIT_LIST_HEAD(&sec->symbol_list);
INIT_LIST_HEAD(&sec->rela_list); INIT_LIST_HEAD(&sec->rela_list);
hash_init(sec->rela_hash);
s = elf_getscn(elf->elf, i); s = elf_getscn(elf->elf, i);
if (!s) { if (!s) {
@ -493,7 +497,7 @@ static int read_relas(struct elf *elf)
} }
list_add_tail(&rela->list, &sec->rela_list); list_add_tail(&rela->list, &sec->rela_list);
hash_add(sec->rela_hash, &rela->hash, rela->offset); hash_add(elf->rela_hash, &rela->hash, rela_hash(rela));
nr_rela++; nr_rela++;
} }
max_rela = max(max_rela, nr_rela); max_rela = max(max_rela, nr_rela);
@ -526,6 +530,7 @@ struct elf *elf_read(const char *name, int flags)
hash_init(elf->symbol_name_hash); hash_init(elf->symbol_name_hash);
hash_init(elf->section_hash); hash_init(elf->section_hash);
hash_init(elf->section_name_hash); hash_init(elf->section_name_hash);
hash_init(elf->rela_hash);
INIT_LIST_HEAD(&elf->sections); INIT_LIST_HEAD(&elf->sections);
elf->fd = open(name, flags); elf->fd = open(name, flags);
@ -586,7 +591,6 @@ struct section *elf_create_section(struct elf *elf, const char *name,
INIT_LIST_HEAD(&sec->symbol_list); INIT_LIST_HEAD(&sec->symbol_list);
INIT_LIST_HEAD(&sec->rela_list); INIT_LIST_HEAD(&sec->rela_list);
hash_init(sec->rela_hash);
s = elf_newscn(elf->elf); s = elf_newscn(elf->elf);
if (!s) { if (!s) {

View File

@ -33,7 +33,6 @@ struct section {
struct rb_root symbol_tree; struct rb_root symbol_tree;
struct list_head symbol_list; struct list_head symbol_list;
struct list_head rela_list; struct list_head rela_list;
DECLARE_HASHTABLE(rela_hash, 16);
struct section *base, *rela; struct section *base, *rela;
struct symbol *sym; struct symbol *sym;
Elf_Data *data; Elf_Data *data;
@ -81,8 +80,22 @@ struct elf {
DECLARE_HASHTABLE(symbol_name_hash, 20); DECLARE_HASHTABLE(symbol_name_hash, 20);
DECLARE_HASHTABLE(section_hash, 16); DECLARE_HASHTABLE(section_hash, 16);
DECLARE_HASHTABLE(section_name_hash, 16); DECLARE_HASHTABLE(section_name_hash, 16);
DECLARE_HASHTABLE(rela_hash, 20);
}; };
static inline u32 sec_offset_hash(struct section *sec, unsigned long offset)
{
u32 ol = offset, oh = offset >> 32, idx = sec->idx;
__jhash_mix(ol, oh, idx);
return ol;
}
static inline u32 rela_hash(struct rela *rela)
{
return sec_offset_hash(rela->sec, rela->offset);
}
struct elf *elf_read(const char *name, int flags); struct elf *elf_read(const char *name, int flags);
struct section *find_section_by_name(struct elf *elf, const char *name); struct section *find_section_by_name(struct elf *elf, const char *name);
@ -90,9 +103,9 @@ struct symbol *find_func_by_offset(struct section *sec, unsigned long offset);
struct symbol *find_symbol_by_offset(struct section *sec, unsigned long offset); struct symbol *find_symbol_by_offset(struct section *sec, unsigned long offset);
struct symbol *find_symbol_by_name(struct elf *elf, const char *name); struct symbol *find_symbol_by_name(struct elf *elf, const char *name);
struct symbol *find_symbol_containing(struct section *sec, unsigned long offset); struct symbol *find_symbol_containing(struct section *sec, unsigned long offset);
struct rela *find_rela_by_dest(struct section *sec, unsigned long offset); struct rela *find_rela_by_dest(struct elf *elf, struct section *sec, unsigned long offset);
struct rela *find_rela_by_dest_range(struct section *sec, unsigned long offset, struct rela *find_rela_by_dest_range(struct elf *elf, struct section *sec,
unsigned int len); unsigned long offset, unsigned int len);
struct symbol *find_func_containing(struct section *sec, unsigned long offset); struct symbol *find_func_containing(struct section *sec, unsigned long offset);
struct section *elf_create_section(struct elf *elf, const char *name, size_t struct section *elf_create_section(struct elf *elf, const char *name, size_t
entsize, int nr); entsize, int nr);

View File

@ -81,7 +81,7 @@ int create_orc(struct objtool_file *file)
return 0; return 0;
} }
static int create_orc_entry(struct section *u_sec, struct section *ip_relasec, static int create_orc_entry(struct elf *elf, struct section *u_sec, struct section *ip_relasec,
unsigned int idx, struct section *insn_sec, unsigned int idx, struct section *insn_sec,
unsigned long insn_off, struct orc_entry *o) unsigned long insn_off, struct orc_entry *o)
{ {
@ -109,9 +109,10 @@ static int create_orc_entry(struct section *u_sec, struct section *ip_relasec,
rela->addend = insn_off; rela->addend = insn_off;
rela->type = R_X86_64_PC32; rela->type = R_X86_64_PC32;
rela->offset = idx * sizeof(int); rela->offset = idx * sizeof(int);
rela->sec = ip_relasec;
list_add_tail(&rela->list, &ip_relasec->rela_list); list_add_tail(&rela->list, &ip_relasec->rela_list);
hash_add(ip_relasec->rela_hash, &rela->hash, rela->offset); hash_add(elf->rela_hash, &rela->hash, rela_hash(rela));
return 0; return 0;
} }
@ -182,7 +183,7 @@ int create_orc_sections(struct objtool_file *file)
if (!prev_insn || memcmp(&insn->orc, &prev_insn->orc, if (!prev_insn || memcmp(&insn->orc, &prev_insn->orc,
sizeof(struct orc_entry))) { sizeof(struct orc_entry))) {
if (create_orc_entry(u_sec, ip_relasec, idx, if (create_orc_entry(file->elf, u_sec, ip_relasec, idx,
insn->sec, insn->offset, insn->sec, insn->offset,
&insn->orc)) &insn->orc))
return -1; return -1;
@ -194,7 +195,7 @@ int create_orc_sections(struct objtool_file *file)
/* section terminator */ /* section terminator */
if (prev_insn) { if (prev_insn) {
if (create_orc_entry(u_sec, ip_relasec, idx, if (create_orc_entry(file->elf, u_sec, ip_relasec, idx,
prev_insn->sec, prev_insn->sec,
prev_insn->offset + prev_insn->len, prev_insn->offset + prev_insn->len,
&empty)) &empty))

View File

@ -118,7 +118,7 @@ static int get_alt_entry(struct elf *elf, struct special_entry *entry,
} }
} }
orig_rela = find_rela_by_dest(sec, offset + entry->orig); orig_rela = find_rela_by_dest(elf, sec, offset + entry->orig);
if (!orig_rela) { if (!orig_rela) {
WARN_FUNC("can't find orig rela", sec, offset + entry->orig); WARN_FUNC("can't find orig rela", sec, offset + entry->orig);
return -1; return -1;
@ -133,7 +133,7 @@ static int get_alt_entry(struct elf *elf, struct special_entry *entry,
alt->orig_off = orig_rela->addend; alt->orig_off = orig_rela->addend;
if (!entry->group || alt->new_len) { if (!entry->group || alt->new_len) {
new_rela = find_rela_by_dest(sec, offset + entry->new); new_rela = find_rela_by_dest(elf, sec, offset + entry->new);
if (!new_rela) { if (!new_rela) {
WARN_FUNC("can't find new rela", WARN_FUNC("can't find new rela",
sec, offset + entry->new); sec, offset + entry->new);