[RFC][PATCH 12/16] objtool: Optimize read_sections()

Peter Zijlstra
2020-03-12T13:52:14Z

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>
---
tools/objtool/elf.c | 18 ++++++++++++------
tools/objtool/elf.h | 18 +++++++++++++++++-
tools/objtool/orc_gen.c | 3 ++-
3 files changed, 31 insertions(+), 8 deletions(-)

--- a/tools/objtool/elf.c
+++ b/tools/objtool/elf.c
@@ -210,10 +210,15 @@ struct rela *find_rela_by_dest_range(str
if (!sec->rela)
return NULL;

- for (o = offset; o < offset + len; o++)
- hash_for_each_possible(sec->rela->rela_hash, rela, hash, o)
- if (rela->offset == o)
+ sec = sec->rela;
+
+ for (o = offset; o < offset + len; o++) {
+ hash_for_each_possible(sec->elf->rela_hash, rela, hash,
+ __rela_hash(o, sec->idx)) {
+ if (rela->sec == sec && rela->offset == o)
return rela;
+ }
+ }

return NULL;
}
@@ -248,9 +253,9 @@ static int read_sections(struct elf *elf
}
memset(sec, 0, sizeof(*sec));

+ sec->elf = elf;
INIT_LIST_HEAD(&sec->symbol_list);
INIT_LIST_HEAD(&sec->rela_list);
- hash_init(sec->rela_hash);

list_add_tail(&sec->list, &elf->sections);

@@ -485,7 +490,7 @@ static int read_relas(struct elf *elf)
}

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++;
}
max_rela = max(max_rela, nr_rela);
@@ -518,6 +523,7 @@ struct elf *elf_read(const char *name, i
hash_init(elf->symbol_name_hash);
hash_init(elf->section_hash);
hash_init(elf->section_name_hash);
+ hash_init(elf->rela_hash);
INIT_LIST_HEAD(&elf->sections);

elf->fd = open(name, flags);
@@ -576,9 +582,9 @@ struct section *elf_create_section(struc
}
memset(sec, 0, sizeof(*sec));

+ sec->elf = elf;
INIT_LIST_HEAD(&sec->symbol_list);
INIT_LIST_HEAD(&sec->rela_list);
- hash_init(sec->rela_hash);

list_add_tail(&sec->list, &elf->sections);

--- a/tools/objtool/elf.h
+++ b/tools/objtool/elf.h
@@ -25,6 +25,8 @@
#define ELF_C_READ_MMAP ELF_C_READ
#endif

+struct elf;
+
struct section {
struct list_head list;
struct hlist_node hash;
@@ -33,7 +35,7 @@ struct section {
struct rb_root symbol_tree;
struct list_head symbol_list;
struct list_head rela_list;
- DECLARE_HASHTABLE(rela_hash, 16);
+ struct elf *elf;
struct section *base, *rela;
struct symbol *sym;
Elf_Data *data;
@@ -81,8 +83,22 @@ struct elf {
DECLARE_HASHTABLE(symbol_name_hash, 20);
DECLARE_HASHTABLE(section_hash, 16);
DECLARE_HASHTABLE(section_name_hash, 16);
+ DECLARE_HASHTABLE(rela_hash, 20);
};

+static inline u32 __rela_hash(unsigned long offset, int idx)
+{
+ u32 ol = offset, oh = offset >> 32;
+
+ __jhash_mix(ol, oh, idx);
+
+ return ol;
+}
+
+static inline u32 rela_hash(struct rela *rela)
+{
+ return __rela_hash(rela->offset, rela->sec->idx);
+}

struct elf *elf_read(const char *name, int flags);
struct section *find_section_by_name(struct elf *elf, const char *name);
--- a/tools/objtool/orc_gen.c
+++ b/tools/objtool/orc_gen.c
@@ -109,9 +109,10 @@ static int create_orc_entry(struct secti
rela->addend = insn_off;
rela->type = R_X86_64_PC32;
rela->offset = idx * sizeof(int);
+ rela->sec = ip_relasec;

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

return 0;
}