2020-03-12 13:52:54

by Peter Zijlstra

[permalink] [raw]
Subject: [RFC][PATCH 05/16] objtool: Optimize find_symbol_by_index()

The symbol index is object wide, not per section, so it makes no sense
to have the symbol_hash be part of the section object. By moving it to
the elf object we avoid the linear sections iteration.

This reduces the runtime of objtool on vmlinux.o from over 3 hours (I
gave up) to a few minutes. The defconfig vmlinux.o has around 20k
sections.

Signed-off-by: Peter Zijlstra (Intel) <[email protected]>
---
tools/objtool/elf.c | 14 ++++++--------
tools/objtool/elf.h | 3 +--
2 files changed, 7 insertions(+), 10 deletions(-)

--- a/tools/objtool/elf.c
+++ b/tools/objtool/elf.c
@@ -46,13 +46,11 @@ static struct section *find_section_by_i

static struct symbol *find_symbol_by_index(struct elf *elf, unsigned int idx)
{
- struct section *sec;
struct symbol *sym;

- list_for_each_entry(sec, &elf->sections, list)
- hash_for_each_possible(sec->symbol_hash, sym, hash, idx)
- if (sym->idx == idx)
- return sym;
+ hash_for_each_possible(elf->symbol_hash, sym, hash, idx)
+ if (sym->idx == idx)
+ return sym;

return NULL;
}
@@ -156,7 +154,6 @@ static int read_sections(struct elf *elf
INIT_LIST_HEAD(&sec->symbol_list);
INIT_LIST_HEAD(&sec->rela_list);
hash_init(sec->rela_hash);
- hash_init(sec->symbol_hash);

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

@@ -289,7 +286,8 @@ static int read_symbols(struct elf *elf)
}
sym->alias = alias;
list_add(&sym->list, entry);
- hash_add(sym->sec->symbol_hash, &sym->hash, sym->idx);
+
+ hash_add(elf->symbol_hash, &sym->hash, sym->idx);
}

/* Create parent/child links for any cold subfunctions */
@@ -415,6 +413,7 @@ struct elf *elf_read(const char *name, i
}
memset(elf, 0, sizeof(*elf));

+ hash_init(elf->symbol_hash);
INIT_LIST_HEAD(&elf->sections);

elf->fd = open(name, flags);
@@ -476,7 +475,6 @@ struct section *elf_create_section(struc
INIT_LIST_HEAD(&sec->symbol_list);
INIT_LIST_HEAD(&sec->rela_list);
hash_init(sec->rela_hash);
- hash_init(sec->symbol_hash);

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

--- a/tools/objtool/elf.h
+++ b/tools/objtool/elf.h
@@ -27,7 +27,6 @@ struct section {
struct list_head list;
GElf_Shdr sh;
struct list_head symbol_list;
- DECLARE_HASHTABLE(symbol_hash, 8);
struct list_head rela_list;
DECLARE_HASHTABLE(rela_hash, 16);
struct section *base, *rela;
@@ -71,7 +70,7 @@ struct elf {
int fd;
char *name;
struct list_head sections;
- DECLARE_HASHTABLE(rela_hash, 16);
+ DECLARE_HASHTABLE(symbol_hash, 20);
};





2020-03-15 16:10:05

by Josh Poimboeuf

[permalink] [raw]
Subject: Re: [RFC][PATCH 05/16] objtool: Optimize find_symbol_by_index()

On Thu, Mar 12, 2020 at 02:41:12PM +0100, Peter Zijlstra wrote:
> The symbol index is object wide, not per section, so it makes no sense
> to have the symbol_hash be part of the section object. By moving it to
> the elf object we avoid the linear sections iteration.

I remember there was a specific reason for this oddity, but it eludes me
now.

This does make sense, assuming it doesn't break anything.

--
Josh

2020-03-15 16:11:10

by Josh Poimboeuf

[permalink] [raw]
Subject: Re: [RFC][PATCH 05/16] objtool: Optimize find_symbol_by_index()

On Thu, Mar 12, 2020 at 02:41:12PM +0100, Peter Zijlstra wrote:
> @@ -289,7 +286,8 @@ static int read_symbols(struct elf *elf)
> }
> sym->alias = alias;
> list_add(&sym->list, entry);
> - hash_add(sym->sec->symbol_hash, &sym->hash, sym->idx);
> +
> + hash_add(elf->symbol_hash, &sym->hash, sym->idx);

Unnecessary added whitespace.

--
Josh

2020-03-15 16:19:16

by Josh Poimboeuf

[permalink] [raw]
Subject: Re: [RFC][PATCH 05/16] objtool: Optimize find_symbol_by_index()

On Sun, Mar 15, 2020 at 11:09:19AM -0500, Josh Poimboeuf wrote:
> On Thu, Mar 12, 2020 at 02:41:12PM +0100, Peter Zijlstra wrote:
> > The symbol index is object wide, not per section, so it makes no sense
> > to have the symbol_hash be part of the section object. By moving it to
> > the elf object we avoid the linear sections iteration.
>
> I remember there was a specific reason for this oddity, but it eludes me
> now.
>
> This does make sense, assuming it doesn't break anything.

On second thought I guess it was the symbol_list which had this
intentional per-section structure (for a still unremembered reason).

Then the symbol_hash came later, and it just parroted the symbol_list
structure. So yeah, this change should be fine.

--
Josh

2020-03-17 11:55:40

by Miroslav Benes

[permalink] [raw]
Subject: Re: [RFC][PATCH 05/16] objtool: Optimize find_symbol_by_index()

> --- a/tools/objtool/elf.h
> +++ b/tools/objtool/elf.h
> @@ -27,7 +27,6 @@ struct section {
> struct list_head list;
> GElf_Shdr sh;
> struct list_head symbol_list;
> - DECLARE_HASHTABLE(symbol_hash, 8);
> struct list_head rela_list;
> DECLARE_HASHTABLE(rela_hash, 16);
> struct section *base, *rela;
> @@ -71,7 +70,7 @@ struct elf {
> int fd;
> char *name;
> struct list_head sections;
> - DECLARE_HASHTABLE(rela_hash, 16);
> + DECLARE_HASHTABLE(symbol_hash, 20);
> };

Not that it really matters, but what was rela_hash in struct elf for
before this?

Miroslav

2020-03-17 14:09:17

by Peter Zijlstra

[permalink] [raw]
Subject: Re: [RFC][PATCH 05/16] objtool: Optimize find_symbol_by_index()

On Tue, Mar 17, 2020 at 12:55:01PM +0100, Miroslav Benes wrote:
> > --- a/tools/objtool/elf.h
> > +++ b/tools/objtool/elf.h
> > @@ -27,7 +27,6 @@ struct section {
> > struct list_head list;
> > GElf_Shdr sh;
> > struct list_head symbol_list;
> > - DECLARE_HASHTABLE(symbol_hash, 8);
> > struct list_head rela_list;
> > DECLARE_HASHTABLE(rela_hash, 16);
> > struct section *base, *rela;
> > @@ -71,7 +70,7 @@ struct elf {
> > int fd;
> > char *name;
> > struct list_head sections;
> > - DECLARE_HASHTABLE(rela_hash, 16);
> > + DECLARE_HASHTABLE(symbol_hash, 20);
> > };
>
> Not that it really matters, but what was rela_hash in struct elf for
> before this?

Unused afaict.