Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

objtool: Optimize find_rela_by_dest_range()

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

+26 -5
+11 -4
tools/objtool/elf.c
··· 215 215 struct rela *find_rela_by_dest_range(struct elf *elf, struct section *sec, 216 216 unsigned long offset, unsigned int len) 217 217 { 218 - struct rela *rela; 218 + struct rela *rela, *r = NULL; 219 219 unsigned long o; 220 220 221 221 if (!sec->rela) ··· 223 223 224 224 sec = sec->rela; 225 225 226 - for (o = offset; o < offset + len; o++) { 226 + for_offset_range(o, offset, offset + len) { 227 227 hash_for_each_possible(elf->rela_hash, rela, hash, 228 228 sec_offset_hash(sec, o)) { 229 - if (rela->sec == sec && rela->offset == o) 230 - return rela; 229 + if (rela->sec != sec) 230 + continue; 231 + 232 + if (rela->offset >= offset && rela->offset < offset + len) { 233 + if (!r || rela->offset < r->offset) 234 + r = rela; 235 + } 231 236 } 237 + if (r) 238 + return r; 232 239 } 233 240 234 241 return NULL;
+15 -1
tools/objtool/elf.h
··· 83 83 DECLARE_HASHTABLE(rela_hash, 20); 84 84 }; 85 85 86 + #define OFFSET_STRIDE_BITS 4 87 + #define OFFSET_STRIDE (1UL << OFFSET_STRIDE_BITS) 88 + #define OFFSET_STRIDE_MASK (~(OFFSET_STRIDE - 1)) 89 + 90 + #define for_offset_range(_offset, _start, _end) \ 91 + for (_offset = ((_start) & OFFSET_STRIDE_MASK); \ 92 + _offset <= ((_end) & OFFSET_STRIDE_MASK); \ 93 + _offset += OFFSET_STRIDE) 94 + 86 95 static inline u32 sec_offset_hash(struct section *sec, unsigned long offset) 87 96 { 88 - u32 ol = offset, oh = offset >> 32, idx = sec->idx; 97 + u32 ol, oh, idx = sec->idx; 98 + 99 + offset &= OFFSET_STRIDE_MASK; 100 + 101 + ol = offset; 102 + oh = offset >> 32; 89 103 90 104 __jhash_mix(ol, oh, idx); 91 105