Skip to content

Commit

Permalink
objtool: Optimize find_rela_by_dest_range()
Browse files Browse the repository at this point in the history
Perf shows there is significant time in find_rela_by_dest(); this is
because we have to iterate the address space per byte, looking for
relocation entries.

Optimize this by reducing the address space granularity.

This reduces objtool on vmlinux.o runtime from 4.8 to 4.4 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.861321325@infradead.org
  • Loading branch information
Peter Zijlstra committed Mar 25, 2020
1 parent 8887a86 commit 74b873e
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 5 deletions.
15 changes: 11 additions & 4 deletions tools/objtool/elf.c
Original file line number Diff line number Diff line change
Expand Up @@ -215,20 +215,27 @@ struct symbol *find_symbol_by_name(struct elf *elf, const char *name)
struct rela *find_rela_by_dest_range(struct elf *elf, struct section *sec,
unsigned long offset, unsigned int len)
{
struct rela *rela;
struct rela *rela, *r = NULL;
unsigned long o;

if (!sec->rela)
return NULL;

sec = sec->rela;

for (o = offset; o < offset + len; o++) {
for_offset_range(o, offset, offset + len) {
hash_for_each_possible(elf->rela_hash, rela, hash,
sec_offset_hash(sec, o)) {
if (rela->sec == sec && rela->offset == o)
return rela;
if (rela->sec != sec)
continue;

if (rela->offset >= offset && rela->offset < offset + len) {
if (!r || rela->offset < r->offset)
r = rela;
}
}
if (r)
return r;
}

return NULL;
Expand Down
16 changes: 15 additions & 1 deletion tools/objtool/elf.h
Original file line number Diff line number Diff line change
Expand Up @@ -83,9 +83,23 @@ struct elf {
DECLARE_HASHTABLE(rela_hash, 20);
};

#define OFFSET_STRIDE_BITS 4
#define OFFSET_STRIDE (1UL << OFFSET_STRIDE_BITS)
#define OFFSET_STRIDE_MASK (~(OFFSET_STRIDE - 1))

#define for_offset_range(_offset, _start, _end) \
for (_offset = ((_start) & OFFSET_STRIDE_MASK); \
_offset <= ((_end) & OFFSET_STRIDE_MASK); \
_offset += OFFSET_STRIDE)

static inline u32 sec_offset_hash(struct section *sec, unsigned long offset)
{
u32 ol = offset, oh = offset >> 32, idx = sec->idx;
u32 ol, oh, idx = sec->idx;

offset &= OFFSET_STRIDE_MASK;

ol = offset;
oh = offset >> 32;

__jhash_mix(ol, oh, idx);

Expand Down

0 comments on commit 74b873e

Please sign in to comment.