objtool: vmlinux.o and noinstr validation
Subsystems (5)
Peter Zijlstra
maintainercover objtool: vmlinux.o and noinstr validation
Hi all,
These patches extend objtool to be able to run on vmlinux.o and validate Thomas's proposed noinstr annotation:
https://lkml.kernel.org/r/[email protected]
"That's why we want the sections and the annotation. If something calls
out of a noinstr section into a regular text section and the call is not annotated at the call site, then objtool can complain and tell you. What Peter and I came up with looks like this:
noinstr foo()
do_protected(); <- Safe because in the noinstr section instr_begin(); <- Marks the begin of a safe region, ignored by objtool do_stuff(); <- All good instr_end(); <- End of the safe region. objtool starts looking again do_other_stuff(); <- Unsafe because do_other_stuff() is not protected
and:
noinstr do_protected()
bar(); <- objtool will complain here
"
It should be accompanied by something like the below; which you'll find in a series by Thomas.
---
include/asm-generic/sections.h +3 -0
@@ -53,6 +53,9 @@ extern char __ctors_start[], __ctors_end/* Start and end of .opd section - used for function descriptors. */ extern char __start_opd[], __end_opd[]; +/* Start and end of instrumentation protected text section */ +extern char __noinstr_text_start[], __noinstr_text_end[]; + extern __visible const void __nosave_begin, __nosave_end;/* Function descriptor handling (if any). Override in asm/sections.h */
include/asm-generic/vmlinux.lds.h +4 -0
@@ -550,6 +550,10 @@#define TEXT_TEXT \ ALIGN_FUNCTION(); \ *(.text.hot TEXT_MAIN .text.fixup .text.unlikely) \ + ALIGN_FUNCTION(); \ + __noinstr_text_start = .; \ + *(.noinstr.text) \ + __noinstr_text_end = .; \ *(.text..refcount) \ *(.ref.text) \ MEM_KEEP(init.text*) \
include/linux/compiler.h +25 -0
@@ -120,12 +120,37 @@ void ftrace_likely_update(struct ftrace_/* Annotate a C jump table to allow objtool to follow the code flow */ #define __annotate_jump_table __section(.rodata..c_jump_table) +/* Begin/end of an instrumentation safe region */ +#define instr_begin () ({ \ + asm volatile("%c0:\n\t" \ + ".pushsection .discard.instr_begin\n\t" \ + ".long %c0b - .\n\t" \ + ".popsection\n\t" : : "i" (__COUNTER__)); \ +}) + +#define instr_end () ({ \ + asm volatile("%c0:\n\t" \ + ".pushsection .discard.instr_end\n\t" \ + ".long %c0b - .\n\t" \ + ".popsection\n\t" : : "i" (__COUNTER__)); \ +}) +#else #define annotate_reachable ()#define annotate_unreachable ()#define __annotate_jump_table +#define instr_begin () do { } while(0) +#define instr_end () do { } while(0)#endif +#define INSTR (expr) ({ \ + typeof(({ expr; })) __ret; \ + instr_begin(); \ + __ret = ({ expr; }); \ + instr_end(); \ + __ret; \ +}) +#ifndef ASM_UNREACHABLE # define ASM_UNREACHABLE #endif
include/linux/compiler_types.h +5 -0
@@ -118,6 +118,11 @@ struct ftrace_likely_data {#define notrace __attribute__((__no_instrument_function__))#endif +/* Section for code which can't be instrumented at all */ +#define noinstr \ + notrace __attribute((__section__(".noinstr.text"))) + +/* * it doesn't make sense on ARM (currently the only user of __naked) * to trace naked functions because then mcount is called without
scripts/mod/modpost.c +1 -1
@@ -953,7 +953,7 @@ static void check_section(const char *mo#define DATA_SECTIONS ".data", ".data.rel"#define TEXT_SECTIONS ".text", ".text.unlikely", ".sched.text", \ - ".kprobes.text", ".cpuidle.text" + ".kprobes.text", ".cpuidle.text", ".noinstr.text"#define OTHER_TEXT_SECTIONS ".ref.text", ".head.text", ".spinlock.text", \ ".fixup", ".entry.text", ".exception.text", ".text.*", \ ".coldtext"
Peter Zijlstra
maintainerRFC PATCH 1/16 objtool: Introduce validate_return()
Trivial 'cleanup' to save one indentation level and match validate_call().
--- tools/objtool/check.c | 64 ++++++++++++++++++++++++++++---------------------- 1 file changed, 36 insertions(+), 28 deletions(-)
tools/objtool/check.c +36 -28
@@ -1935,6 +1935,41 @@ static int validate_sibling_call(structreturn validate_call (insn, state); } +static int validate_return (struct symbol *func,struct instruction *insn,struct insn_state *state) +{ +if (state->uaccess && !func_uaccess_safe (func)) { +WARN_FUNC ("return with UACCESS enabled" , + insn->sec, insn->offset); +return 1 ; + } + +if (!state->uaccess &&func_uaccess_safe (func)) { +WARN_FUNC ("return with UACCESS disabled from a UACCESS-safe function" , + insn->sec, insn->offset); +return 1 ; + } + +if (state->df) { +WARN_FUNC ("return with DF set" , + insn->sec, insn->offset); +return 1 ; + } + +if (func &&has_modified_stack_frame (state)) { +WARN_FUNC ("return with modified stack frame" , + insn->sec, insn->offset); +return 1 ; + } + +if (state->bp_scratch) { +WARN ("%s uses BP as a scratch register" , + func->name); +return 1 ; + } + +return 0 ; +} +/* * Follow the branch starting at the given instruction, and recursively follow * any other branches (jumps). Meanwhile, track the frame pointer state at
@@ -2050,34 +2085,7 @@ static int validate_branch(struct objtooswitch (insn->type) {case INSN_RETURN : -if (state.uaccess && !func_uaccess_safe (func)) { -WARN_FUNC ("return with UACCESS enabled" , sec, insn->offset); -return 1 ; - } - -if (!state.uaccess &&func_uaccess_safe (func)) { -WARN_FUNC ("return with UACCESS disabled from a UACCESS-safe function" , sec, insn->offset); -return 1 ; - } - -if (state.df) { -WARN_FUNC ("return with DF set" , sec, insn->offset); -return 1 ; - } - -if (func &&has_modified_stack_frame (&state)) { -WARN_FUNC ("return with modified stack frame" , - sec, insn->offset); -return 1 ; - } - -if (state.bp_scratch) { -WARN ("%s uses BP as a scratch register" , - func->name); -return 1 ; - } - -return 0 ; +return validate_return (func, insn, &state);case INSN_CALL :case INSN_CALL_DYNAMIC :
Peter Zijlstra
maintainerRFC PATCH 2/16 objtool: Rename func_for_each_insn()
In reply to Peter ZijlstraThere is func_for_each_insn() and func_for_each_insn_all(), the both iterate the instructions, but the first uses symbol offset/length while the second uses insn->func.
Rename func_for_each_insn() to sym_for_eac_insn() because it iterates on symbol information.
--- tools/objtool/check.c | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-)
tools/objtool/check.c +8 -8
@@ -77,17 +77,17 @@ static struct instruction *next_insn_saminsn ; \ insn = next_insn_same_func(file, insn)) -#define func_for_each_insn (file, func, insn) \ - for (insn = find_insn(file, func->sec, func->offset); \ +#define sym_for_each_insn (file, sym, insn) \ + for (insn = find_insn(file, sym->sec, sym->offset); \ insn && &insn->list != &file->insn_list && \ - insn->sec == func->sec && \ - insn->offset < func->offset + func->len; \ + insn->sec == sym->sec && \ + insn->offset < sym->offset + sym->len; \ insn = list_next_entry(insn, list)) -#define func_for_each_insn_continue_reverse (file, func, insn) \ +#define sym_for_each_insn_continue_reverse (file, sym, insn) \ for (insn = list_prev_entry(insn, list); \ &insn->list != &file->insn_list && \ - insn->sec == func->sec && insn->offset >= func->offset; \ + insn->sec == sym->sec && insn->offset >= sym->offset; \ insn = list_prev_entry(insn, list))#define sec_for_each_insn_from (file, insn) \
@@ -281,7 +281,7 @@ static int decode_instructions(struct obreturn -1 ; } -func_for_each_insn (file, func, insn) +sym_for_each_insn (file, func, insn) insn->func = func; } }
@@ -2024,7 +2024,7 @@ static int validate_branch(struct objtoo i = insn; save_insn =NULL ; -func_for_each_insn_continue_reverse (file, func, i) { +sym_for_each_insn_continue_reverse (file, func, i) {if (i->save) { save_insn = i;break ;
Peter Zijlstra
maintainerRFC PATCH 3/16 objtool: Rename func_for_each_insn_all()
In reply to Peter ZijlstraNow that func_for_each_insn() is available, rename func_for_each_insn_all(). This gets us:
sym_for_each_insn() - iterate on symbol offset/len func_for_each_insn() - iterate on insn->func
--- tools/objtool/check.c | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-)
tools/objtool/check.c +6 -6
@@ -72,7 +72,7 @@ static struct instruction *next_insn_samreturn find_insn (file, func->cfunc->sec, func->cfunc->offset); } -#define func_for_each_insn_all (file, func, insn) \ +#define func_for_each_insn (file, func, insn) \ for (insn = find_insn(file, func->sec, func->offset); \ insn; \ insn = next_insn_same_func(file, insn))
@@ -165,7 +165,7 @@ static bool __dead_end_function(struct oif (!insn->func)return false; -func_for_each_insn_all (file, func, insn) { +func_for_each_insn (file, func, insn) { empty = false;if (insn->type ==INSN_RETURN )
@@ -180,7 +180,7 @@ static bool __dead_end_function(struct o* case, the function's dead-end status depends on whether the target * of the sibling call returns. */ -func_for_each_insn_all (file, func, insn) { +func_for_each_insn (file, func, insn) {if (is_sibling_call (insn)) {struct instruction *dest = insn->jump_dest;
@@ -425,7 +425,7 @@ static void add_ignores(struct objtool_fcontinue ; } -func_for_each_insn_all (file, func, insn) +func_for_each_insn (file, func, insn) insn->ignore = true; } }
@@ -1082,7 +1082,7 @@ static void mark_func_jump_tables(structstruct instruction *insn, *last =NULL ;struct rela *rela; -func_for_each_insn_all (file, func, insn) { +func_for_each_insn (file, func, insn) {if (!last) last = insn;
@@ -1117,7 +1117,7 @@ static int add_func_jump_tables(struct ostruct instruction *insn;int ret; -func_for_each_insn_all (file, func, insn) { +func_for_each_insn (file, func, insn) {if (!insn->jump_table)continue ;
Peter Zijlstra
maintainerRFC PATCH 4/16 objtool: Annotate identity_mapped()
In reply to Peter ZijlstraNormally identity_mapped is not visible to objtool, due to:
arch/x86/kernel/Makefile:OBJECT_FILES_NON_STANDARD_relocate_kernel_$(BITS).o := y
However, when we want to run objtool on vmlinux.o there is no hiding it. Without the annotation we'll get complaints about the:
call 1f
1: popq %rB
construct from the add_call_destinations() pass. Because identity_mapped() is a SYM_CODE_START_LOCAL_NOALIGN() it is STT_NOTYPE and we need sym_for_each_insn() to iterate the actual instructions.
--- arch/x86/kernel/relocate_kernel_64.S | 2 ++ include/linux/frame.h | 16 ++++++++++++++++ tools/objtool/check.c | 4 ++-- 3 files changed, 20 insertions(+), 2 deletions(-)
arch/x86/kernel/relocate_kernel_64.S +2 -0
@@ -5,6 +5,7 @@ */ #include <linux/linkage.h> +#include <linux/frame.h> #include <asm/page_types.h> #include <asm/kexec.h> #include <asm/processor-flags.h>
@@ -210,6 +211,7 @@ SYM_CODE_START_LOCAL_NOALIGN(identity_ma pushq %rax ret SYM_CODE_END(identity_mapped) +STACK_FRAME_NON_STANDARD(identity_mapped) SYM_CODE_START_LOCAL_NOALIGN(virtual_mapped) movq RSP(%r8), %rsp
include/linux/frame.h +16 -0
@@ -3,6 +3,9 @@#define _LINUX_FRAME_H#ifdef CONFIG_STACK_VALIDATION + +#ifndef __ASSEMBLY__ +/* * This macro marks the given function's stack frame as "non-standard", which * tells objtool to ignore the function when doing stack metadata validation.
@@ -15,6 +18,19 @@ static void __used __section(.discard.func_stack_frame_non_standard) \ *__func_stack_frame_non_standard_##func = func +#else /* __ASSEMBLY__ */ + +#define STACK_FRAME_NON_STANDARD (func) \ + .pushsection .discard.func_stack_frame_non_standard, "aw"; \ + .align 8; \ + .type __func_stack_frame_non_standard_##func, @object; \ + .size __func_stack_frame_non_standard_##func, 8; \ +__func_stack_frame_non_standard_##func: \ + .quad func; \ + .popsection + +#endif /* __ASSEMBLY */ +#else /* !CONFIG_STACK_VALIDATION */ #define STACK_FRAME_NON_STANDARD (func)
tools/objtool/check.c +2 -2
@@ -416,7 +416,7 @@ static void add_ignores(struct objtool_fcase STT_SECTION : func =find_symbol_by_offset (rela->sym->sec, rela->addend); -if (!func || func->type !=STT_FUNC ) +if (!func || (func->type !=STT_FUNC && func->type !=STT_NOTYPE ))continue ;break ;
@@ -425,7 +425,7 @@ static void add_ignores(struct objtool_fcontinue ; } -func_for_each_insn (file, func, insn) +sym_for_each_insn (file, func, insn) insn->ignore = true; } }
Peter Zijlstra
maintainerRe: [RFC][PATCH 04/16] objtool: Annotate identity_mapped()
Quoted 12 lines · Peter Zijlstra
tools/objtool/check.c +1
-1@@ -416,7 +416,7 @@ static void add_ignores(struct objtool_fcase STT_SECTION : func =find_symbol_by_offset (rela->sym->sec, rela->addend); -if (!func || func->type !=STT_FUNC ) +if (!func || (func->type !=STT_FUNC && func->type !=STT_NOTYPE ))continue ;break ;
tools/objtool/check.c static void add_ignores(struct objtool_f · line 431
Quoted 8 lines
tools/objtool/check.c +1
-1@@ -425,7 +425,7 @@ static void add_ignores(struct objtool_fcontinue ; } -func_for_each_insn (file, func, insn) +sym_for_each_insn (file, func, insn) insn->ignore = true; } }
This conflicts with:
7acfe5315312 ("objtool: Improve call destination function detection")
which wasn't in the tree we were working against :/
I've resolved it something like so.
---
tools/objtool/check.c +5 -5
@@ -406,7 +406,7 @@ static void add_ignores(struct objtool_f {struct instruction *insn;struct section *sec; -struct symbol *func; +struct symbol *sym;struct rela *rela; sec =find_section_by_name (file->elf,".rela.discard.func_stack_frame_non_standard" );
@@ -416,12 +416,12 @@ static void add_ignores(struct objtool_flist_for_each_entry (rela, &sec->rela_list, list) {switch (rela->sym->type) {case STT_FUNC : - func = rela->sym; + sym = rela->sym;break ;case STT_SECTION : - func =find_func_by_offset (rela->sym->sec, rela->addend); -if (!func) + sym =find_symbol_by_offset (rela->sym->sec, rela->addend); +if (!sym || (sym->type !=STT_FUNC || sym->type !=STT_NOTYPE ))continue ;break ;
@@ -430,7 +430,7 @@ static void add_ignores(struct objtool_fcontinue ; } -sym_for_each_insn (file, func, insn) +sym_for_each_insn (file, sym, insn) insn->ignore = true; } }
Re: [RFC][PATCH 04/16] objtool: Annotate identity_mapped()
Quoted 23 lines · Peter Zijlstra
This conflicts with:
7acfe5315312("objtool: Improve call destination function detection")which wasn't in the tree we were working against :/
I've resolved it something like so.
---
tools/objtool/check.c +1
-1@@ -406,7 +406,7 @@ static void add_ignores(struct objtool_f {struct instruction *insn;struct section *sec; -struct symbol *func; +struct symbol *sym;struct rela *rela; sec =find_section_by_name (file->elf,".rela.discard.func_stack_frame_non_standard" );@@ -416,12 +416,12 @@ static void add_ignores(struct objtool_flist_for_each_entry (rela, &sec->rela_list, list) {switch (rela->sym->type) {
tools/objtool/check.c static void add_ignores(struct objtool_f · line 424
Quoted 10 lines
tools/objtool/check.c +3
-3case STT_FUNC : - func = rela->sym; + sym = rela->sym;break ;case STT_SECTION : - func =find_func_by_offset (rela->sym->sec, rela->addend); -if (!func) + sym =find_symbol_by_offset (rela->sym->sec, rela->addend); +if (!sym || (sym->type !=STT_FUNC || sym->type !=STT_NOTYPE ))
^^ &&
Signature · 1 line
Josh
Re: [RFC][PATCH 04/16] objtool: Annotate identity_mapped()
In reply to Peter ZijlstraQuoted 8 lines · Peter Zijlstra
Normally
identity_mappedis not visible to objtool, due to:arch/x86/kernel/Makefile:OBJECT_FILES_NON_STANDARD_relocate_kernel_$(BITS).o := yHowever, when we want to run objtool on vmlinux.o there is no hiding it. Without the annotation we'll get complaints about the:
call 1f
1: popq %r8
subq $(1b - relocate_kernel), %r8
It looks to me that this code is simply trying to get the virtual address of relocate_kernel using the old 32-bit method of PIC address calculation. On 64-bit can be done with leaq relocate_kernel(%rip), %r8.
--
Brian Gerst
Peter Zijlstra
maintainerRe: [RFC][PATCH 04/16] objtool: Annotate identity_mapped()
Quoted 9 lines · Brian Gerst
Quoted 8 lines · Peter Zijlstra
Normally
identity_mappedis not visible to objtool, due to:arch/x86/kernel/Makefile:OBJECT_FILES_NON_STANDARD_relocate_kernel_$(BITS).o := yHowever, when we want to run objtool on vmlinux.o there is no hiding it. Without the annotation we'll get complaints about the:
Quoted 8 lines
call 1f
1: popq %r8subq $(1b - relocate_kernel), %r8It looks to me that this code is simply trying to get the virtual address of
relocate_kernelusing the old 32-bit method of PIC address calculation. On 64-bit can be done with leaqrelocate_kernel(%rip), %r8.
Indeed. Objtool would be happy with that. And it seems I can still kexec a kernel too.
Thanks!
Peter Zijlstra
maintainerRFC PATCH 5/16 objtool: Optimize find_symbol_by_index()
In reply to Peter ZijlstraThe 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.
--- tools/objtool/elf.c | 14 ++++++-------- tools/objtool/elf.h | 3 +-- 2 files changed, 7 insertions(+), 10 deletions(-)
tools/objtool/elf.c +6 -8
@@ -46,13 +46,11 @@ static struct section *find_section_by_istatic 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 *elfINIT_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(strucINIT_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);
tools/objtool/elf.h +1 -2
@@ -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 ); };
Re: [RFC][PATCH 05/16] objtool: Optimize find_symbol_by_index()
Quoted 3 lines · Peter Zijlstra
The symbol index is object wide, not per section, so it makes no sense to have the
symbol_hashbe 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.
Signature · 1 line
Josh
Re: [RFC][PATCH 05/16] objtool: Optimize find_symbol_by_index()
Quoted 9 lines · Josh Poimboeuf
Quoted 3 lines · Peter Zijlstra
The symbol index is object wide, not per section, so it makes no sense to have the
symbol_hashbe 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.
Signature · 1 line
Josh
Re: [RFC][PATCH 05/16] objtool: Optimize find_symbol_by_index()
In reply to Peter Zijlstratools/objtool/elf.c static int read_symbols(struct elf *elf) · line 290
Quoted 7 lines · Peter Zijlstra
tools/objtool/elf.c +2
-1@@ -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.
Signature · 1 line
Josh
Re: [RFC][PATCH 05/16] objtool: Optimize find_symbol_by_index()
In reply to Peter ZijlstraQuoted 11 lines
tools/objtool/elf.h +0
-1@@ -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;
tools/objtool/elf.h struct elf { · line 74
Quoted 6 lines
tools/objtool/elf.h +1
-1@@ -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
Peter Zijlstra
maintainerRe: [RFC][PATCH 05/16] objtool: Optimize find_symbol_by_index()
Quoted 12 lines · Miroslav Benes
Quoted 12 lines
tools/objtool/elf.h +0
-1@@ -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;
Quoted 8 lines
tools/objtool/elf.h struct elf { · line 74
Quoted 5 lines
tools/objtool/elf.h +1
-1char *name;struct list_head sections; -DECLARE_HASHTABLE (rela_hash,16 ); +DECLARE_HASHTABLE (symbol_hash,20 ); };Not that it really matters, but what was
rela_hashin struct elf for before this?
Unused afaict.
Peter Zijlstra
maintainerRFC PATCH 6/16 objtool: Add a statistics mode
In reply to Peter ZijlstraHave it print a few numbers which can be used to size the hashtables.
--- tools/objtool/builtin-check.c | 3 ++- tools/objtool/builtin.h | 2 +- tools/objtool/check.c | 5 +++++ tools/objtool/elf.c | 18 +++++++++++++++++- 4 files changed, 25 insertions(+), 3 deletions(-)
tools/objtool/builtin-check.c +2 -1
@@ -17,7 +17,7 @@#include "builtin.h" #include "check.h" -bool no_fp, no_unreachable, retpoline, module, backtrace, uaccess; +bool no_fp, no_unreachable, retpoline, module, backtrace, uaccess, stats;static const char *const check_usage[] = {"objtool check [<options>] file.o" ,
@@ -31,6 +31,7 @@ const struct option check_options[] = {OPT_BOOLEAN ('m' ,"module" , &module,"Indicates the object will be part of a kernel module" ),OPT_BOOLEAN ('b' ,"backtrace" , &backtrace,"unwind on error" ),OPT_BOOLEAN ('a' ,"uaccess" , &uaccess,"enable uaccess checking" ), +OPT_BOOLEAN ('s' ,"stats" , &stats,"print statistics" ), OPT_END(), };
tools/objtool/builtin.h +1 -1
@@ -8,7 +8,7 @@#include <subcmd/parse-options.h> extern const struct option check_options[]; -extern bool no_fp, no_unreachable, retpoline, module, backtrace, uaccess; +extern bool no_fp, no_unreachable, retpoline, module, backtrace, uaccess, stats;extern int cmd_check (int argc,const char **argv);extern int cmd_orc (int argc,const char **argv);
tools/objtool/check.c +5 -0
@@ -234,6 +234,7 @@ static int decode_instructions(struct obstruct symbol *func;unsigned long offset;struct instruction *insn; +unsigned long nr_insns =0 ;int ret;for_each_sec (file, sec) {
@@ -268,6 +269,7 @@ static int decode_instructions(struct ob goto err;hash_add (file->insn_hash, &insn->hash, insn->offset); + nr_insns++;list_add_tail (&insn->list, &file->insn_list); }
@@ -286,6 +288,9 @@ static int decode_instructions(struct ob } } +if (stats) +printf ("nr_insns: %lu\n" , nr_insns); +return 0 ; err:
tools/objtool/elf.c +17 -1
@@ -15,6 +15,7 @@#include <string.h> #include <unistd.h> #include <errno.h> +#include "builtin.h" #include "elf.h" #include "warn.h"
@@ -192,6 +193,9 @@ static int read_sections(struct elf *elf sec->len = sec->sh.sh_size; } +if (stats) +printf ("nr_sections: %lu\n" , (unsigned long )sections_nr); +/* sanity check, one more call to elf_nextscn() should return NULL */ if (elf_nextscn (elf->elf, s)) {WARN ("section entry mismatch" );
@@ -290,6 +294,9 @@ static int read_symbols(struct elf *elf)hash_add (elf->symbol_hash, &sym->hash, sym->idx); } +if (stats) +printf ("nr_symbols: %lu\n" , (unsigned long )symbols_nr); +/* Create parent/child links for any cold subfunctions */ list_for_each_entry (sec, &elf->sections, list) {list_for_each_entry (sym, &sec->symbol_list, list) {
@@ -351,6 +358,7 @@ static int read_relas(struct elf *elf)struct rela *rela;int i;unsigned int symndx; +unsigned long nr_rela, max_rela =0 , tot_rela =0 ;list_for_each_entry (sec, &elf->sections, list) {if (sec->sh.sh_type !=SHT_RELA )
@@ -365,6 +373,7 @@ static int read_relas(struct elf *elf) sec->base->rela = sec; + nr_rela =0 ;for (i =0 ; i < sec->sh.sh_size / sec->sh.sh_entsize; i++) { rela =malloc (sizeof (*rela));if (!rela) {
@@ -392,8 +401,15 @@ static int read_relas(struct elf *elf)list_add_tail (&rela->list, &sec->rela_list);hash_add (sec->rela_hash, &rela->hash, rela->offset); - + nr_rela++; } + max_rela =max (max_rela, nr_rela); + tot_rela += nr_rela; + } + +if (stats) { +printf ("max_rela: %lu\n" , max_rela); +printf ("tot_rela: %lu\n" , tot_rela); }return 0 ;
Re: [RFC][PATCH 06/16] objtool: Add a statistics mode
Quoted 11 lines · Peter Zijlstra
tools/objtool/check.c +1
-0@@ -234,6 +234,7 @@ static int decode_instructions(struct obstruct symbol *func;unsigned long offset;struct instruction *insn; +unsigned long nr_insns =0 ;int ret;for_each_sec (file, sec) {
tools/objtool/check.c static int decode_instructions(struct ob · line 273
Quoted 5 lines
tools/objtool/check.c +1
-0@@ -268,6 +269,7 @@ static int decode_instructions(struct ob goto err;hash_add (file->insn_hash, &insn->hash, insn->offset); + nr_insns++;list_add_tail (&insn->list, &file->insn_list);
It's slightly more readable to do the 'nr_insns++' after the list_add_tail().
Signature · 1 line
Josh
Peter Zijlstra
maintainerRFC PATCH 7/16 objtool: Optimize find_section_by_index()
In reply to Peter ZijlstraIn order to avoid a linear search (over 20k entries), add an section_hash to the elf object.
This reduces objtool on vmlinux.o from a few minutes to around 45 seconds.
--- tools/objtool/elf.c | 7 ++++++- tools/objtool/elf.h | 2 ++ 2 files changed, 8 insertions(+), 1 deletion(-)
tools/objtool/elf.c +6 -1
@@ -38,7 +38,7 @@ static struct section *find_section_by_i {struct section *sec; -list_for_each_entry (sec, &elf->sections, list) +hash_for_each_possible (elf->section_hash, sec, hash, idx)if (sec->idx == idx)return sec;
@@ -191,6 +191,8 @@ static int read_sections(struct elf *elf } } sec->len = sec->sh.sh_size; + +hash_add (elf->section_hash, &sec->hash, sec->idx); }if (stats)
@@ -430,6 +432,7 @@ struct elf *elf_read(const char *name, imemset (elf,0 ,sizeof (*elf));hash_init (elf->symbol_hash); +hash_init (elf->section_hash);INIT_LIST_HEAD (&elf->sections); elf->fd =open (name, flags);
@@ -570,6 +573,8 @@ struct section *elf_create_section(struc shstrtab->len +=strlen (name) +1 ; shstrtab->changed = true; +hash_add (elf->section_hash, &sec->hash, sec->idx); +return sec; }
tools/objtool/elf.h +2 -0
@@ -25,6 +25,7 @@struct section {struct list_head list; +struct hlist_node hash;GElf_Shdr sh;struct list_head symbol_list;struct list_head rela_list;
@@ -71,6 +72,7 @@ struct elf {char *name;struct list_head sections;DECLARE_HASHTABLE (symbol_hash,20 ); +DECLARE_HASHTABLE (section_hash,16 ); };
Re: [RFC][PATCH 07/16] objtool: Optimize find_section_by_index()
Quoted 24 lines · Peter Zijlstra
In order to avoid a linear search (over 20k entries), add an
section_hashto the elf object.This reduces objtool on vmlinux.o from a few minutes to around 45 seconds.
Signed-off-by Peter Zijlstra (Intel) <[email protected]>--- tools/objtool/elf.c | 7 ++++++- tools/objtool/elf.h | 2 ++ 2 files changed, 8 insertions(+), 1 deletion(-)tools/objtool/elf.c +1
-1@@ -38,7 +38,7 @@ static struct section *find_section_by_i {struct section *sec; -list_for_each_entry (sec, &elf->sections, list) +hash_for_each_possible (elf->section_hash, sec, hash, idx)if (sec->idx == idx)return sec;
tools/objtool/elf.c static int read_sections(struct elf *elf · line 195
Quoted 5 lines
tools/objtool/elf.c +2
-0@@ -191,6 +191,8 @@ static int read_sections(struct elf *elf } } sec->len = sec->sh.sh_size; + +hash_add (elf->section_hash, &sec->hash, sec->idx);
Can you move the
list_add_tail(&sec->list, &elf->sections);
down here so they're done at the same place?
Quoted 12 lines
tools/objtool/elf.c +1
-0}if (stats)@@ -430,6 +432,7 @@ struct elf *elf_read(const char *name, imemset (elf,0 ,sizeof (*elf));hash_init (elf->symbol_hash); +hash_init (elf->section_hash);INIT_LIST_HEAD (&elf->sections); elf->fd =open (name, flags);
tools/objtool/elf.c struct section *elf_create_section(struc · line 579
Quoted 7 lines
tools/objtool/elf.c +2
-0@@ -570,6 +573,8 @@ struct section *elf_create_section(struc shstrtab->len +=strlen (name) +1 ; shstrtab->changed = true; +hash_add (elf->section_hash, &sec->hash, sec->idx); +return sec; }
Ditto
Signature · 1 line
Josh
Peter Zijlstra
maintainerRFC PATCH 8/16 Optimize find_section_by_name()
In reply to Peter ZijlstraIn order to avoid yet another linear search of (20k) sections, add a name based hash.
This reduces objtool runtime on vmlinux.o by some 10s to around 35s.
--- tools/objtool/elf.c | 9 ++++++++- tools/objtool/elf.h | 3 +++ 2 files changed, 11 insertions(+), 1 deletion(-)
tools/objtool/elf.c +8 -1
@@ -22,11 +22,16 @@#define MAX_NAME_LEN 128 +static inline u32 str_hash (const char *str) +{ +return jhash (str,strlen (str),0 ); +} +struct section *find_section_by_name (struct elf *elf,const char *name) {struct section *sec; -list_for_each_entry (sec, &elf->sections, list) +hash_for_each_possible (elf->section_name_hash, sec, name_hash,str_hash (name))if (!strcmp (sec->name, name))return sec;
@@ -193,6 +198,7 @@ static int read_sections(struct elf *elf sec->len = sec->sh.sh_size;hash_add (elf->section_hash, &sec->hash, sec->idx); +hash_add (elf->section_name_hash, &sec->name_hash,str_hash (sec->name)); }if (stats)
@@ -433,6 +439,7 @@ struct elf *elf_read(const char *name, ihash_init (elf->symbol_hash);hash_init (elf->section_hash); +hash_init (elf->section_name_hash);INIT_LIST_HEAD (&elf->sections); elf->fd =open (name, flags);
tools/objtool/elf.h +3 -0
@@ -10,6 +10,7 @@#include <gelf.h> #include <linux/list.h> #include <linux/hashtable.h> +#include <linux/jhash.h> #ifdef LIBELF_USE_DEPRECATED # define elf_getshdrnum elf_getshnum
@@ -26,6 +27,7 @@struct section {struct list_head list;struct hlist_node hash; +struct hlist_node name_hash;GElf_Shdr sh;struct list_head symbol_list;struct list_head rela_list;
@@ -73,6 +75,7 @@ struct elf {struct list_head sections;DECLARE_HASHTABLE (symbol_hash,20 );DECLARE_HASHTABLE (section_hash,16 ); +DECLARE_HASHTABLE (section_name_hash,16 ); };
Re: [RFC][PATCH 08/16] Optimize find_section_by_name()
Quoted 6 lines · Peter Zijlstra
In order to avoid yet another linear search of (20k) sections, add a name based hash.
This reduces objtool runtime on vmlinux.o by some 10s to around 35s.
Signed-off-by Peter Zijlstra (Intel) <[email protected]>
$SUBJECT needs "objtool: " prefix.
Signature · 1 line
Josh
Re: [RFC][PATCH 08/16] Optimize find_section_by_name()
In reply to Peter Zijlstratools/objtool/elf.c static int read_sections(struct elf *elf · line 202
Quoted 6 lines
tools/objtool/elf.c +1
-0@@ -193,6 +198,7 @@ static int read_sections(struct elf *elf sec->len = sec->sh.sh_size;hash_add (elf->section_hash, &sec->hash, sec->idx); +hash_add (elf->section_name_hash, &sec->name_hash,str_hash (sec->name)); }
Don't you need to the same in elf_create_section()?
Miroslav
Peter Zijlstra
maintainerRe: [RFC][PATCH 08/16] Optimize find_section_by_name()
Quoted 8 lines · Miroslav Benes
tools/objtool/elf.c static int read_sections(struct elf *elf · line 202
Quoted 6 lines
tools/objtool/elf.c +1
-0@@ -193,6 +198,7 @@ static int read_sections(struct elf *elf sec->len = sec->sh.sh_size;hash_add (elf->section_hash, &sec->hash, sec->idx); +hash_add (elf->section_name_hash, &sec->name_hash,str_hash (sec->name)); }Don't you need to the same in
elf_create_section()?
Yes, already fixed. Noticed it yesterday when I was addressing Josh's comments.
Peter Zijlstra
maintainerRFC PATCH 9/16 objtool: Optimize find_symbol_*() and read_symbols()
In reply to Peter ZijlstraAll of:
read_symbols(), find_symbol_by_offset(), find_symbol_containing(), find_containing_func()
do a linear search of the symbols. Add an RB tree to make it go faster.
This about halves objtool runtime on vmlinux.o, from 34s to 18s.
--- tools/objtool/Build | 5 + tools/objtool/elf.c | 186 ++++++++++++++++++++++++++++++++++++---------------- tools/objtool/elf.h | 3 3 files changed, 140 insertions(+), 54 deletions(-)
tools/objtool/Build +5 -0
@@ -11,6 +11,7 @@ objtool-y += objtool.o objtool-y += libstring.o objtool-y += libctype.o objtool-y += str_error_r.o +objtool-y += librbtree.o CFLAGS += -I$(srctree)/tools/lib
@@ -25,3 +26,7 @@ $(OUTPUT)libctype.o: ../lib/ctype.c FORC $(OUTPUT)str_error_r.o: ../lib/str_error_r.c FORCE $(call rule_mkdir) $(call if_changed_dep,cc_o_c) + +$(OUTPUT)librbtree.o: ../lib/rbtree.c FORCE + $(call rule_mkdir) + $(call if_changed_dep,cc_o_c)
tools/objtool/elf.c +132 -54
237 lines not on this page · raw message
tools/objtool/elf.h +3 -0
@@ -10,6 +10,7 @@#include <gelf.h> #include <linux/list.h> #include <linux/hashtable.h> +#include <linux/rbtree.h> #include <linux/jhash.h> #ifdef LIBELF_USE_DEPRECATED
@@ -29,6 +30,7 @@ struct section {struct hlist_node hash;struct hlist_node name_hash;GElf_Shdr sh; +struct rb_root symbol_tree;struct list_head symbol_list;struct list_head rela_list;DECLARE_HASHTABLE (rela_hash,16 );
@@ -43,6 +45,7 @@ struct section {struct symbol {struct list_head list; +struct rb_node node;struct hlist_node hash;GElf_Sym sym;struct section *sec;
Re: [RFC][PATCH 09/16] objtool: Optimize find_symbol_*() and read_symbols()
Quoted 11 lines · Peter Zijlstra
All of:
read_symbols(),find_symbol_by_offset(),find_symbol_containing(),find_containing_func()do a linear search of the symbols. Add an RB tree to make it go faster.
This about halves objtool runtime on vmlinux.o, from 34s to 18s.
Signed-off-by Peter Zijlstra (Intel) <[email protected]>
This one also doesn't apply on latest tip.
I assume we also need to optimize the new find_func_by_offset().
Signature · 1 line
Josh
Re: [RFC][PATCH 09/16] objtool: Optimize find_symbol_*() and read_symbols()
In reply to Peter ZijlstraQuoted 8 lines · Peter Zijlstra
struct symbol *find_symbol_by_offset(struct section *sec, unsigned long offset) {
tools/objtool/elf.c +1
-4-struct symbol *sym; +struct rb_node *node; -list_for_each_entry (sym, &sec->symbol_list, list) -if (sym->type !=STT_SECTION && - sym->offset == offset)
tools/objtool/elf.c static struct symbol *find_symbol_by_ind · line 160
Quoted 10 lines
tools/objtool/elf.c +9
-1-return sym; +rb_for_each (&sec->symbol_tree, node, &offset, symbol_by_offset) { + struct symbol *s = rb_entry(node, struct symbol, node); + +if (s->offset != offset) +continue ; + +if (s->type !=STT_SECTION ) +return s; + }
Can be simplified to:
if (s->offset == offset && s->type != STT_SECTION) return s;
Signature · 1 line
Josh
Peter Zijlstra
maintainerRFC PATCH 10/16 objtool: Resize insn_hash
In reply to Peter ZijlstraPerf shows we're spending a lot of time in find_insn() and the statistics show we have around 3.2 million instruction. Increase the hash table size to reduce the bucket load from around 50 to 3.
This shaves about 2s off of objtool on vmlinux.o runtime, down to 16s.
--- tools/objtool/check.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
tools/objtool/check.h +1 -1
@@ -50,7 +50,7 @@ struct instruction {struct objtool_file {struct elf *elf;struct list_head insn_list; - DECLARE_HASHTABLE(insn_hash, 16); + DECLARE_HASHTABLE(insn_hash, 20);bool ignore_unreachables, c_file, hints, rodata; };
Peter Zijlstra
maintainerRFC PATCH 11/16 objtool: Optimize find_symbol_by_name()
In reply to Peter ZijlstraPerf showed that find_symbol_by_name() takes time; add a symbol name hash.
This shaves another second off of objtool on vmlinux.o runtime, down to 15 seconds.
--- tools/objtool/elf.c | 10 +++++----- tools/objtool/elf.h | 2 ++ 2 files changed, 7 insertions(+), 5 deletions(-)
tools/objtool/elf.c +5 -5
@@ -134,13 +134,11 @@ struct symbol *find_symbol_by_offset(strstruct symbol *find_symbol_by_name (struct elf *elf,const char *name) { -struct section *sec;struct symbol *sym; -list_for_each_entry (sec, &elf->sections, list) -list_for_each_entry (sym, &sec->symbol_list, list) -if (!strcmp (sym->name, name)) -return sym; +hash_for_each_possible (elf->symbol_name_hash, sym, name_hash,str_hash (name)) +if (!strcmp (sym->name, name)) +return sym;return NULL ; }
@@ -365,6 +363,7 @@ static int read_symbols(struct elf *elf)list_add (&sym->list, entry);hash_add (elf->symbol_hash, &sym->hash, sym->idx); +hash_add (elf->symbol_name_hash, &sym->name_hash,str_hash (sym->name)); }if (stats)
@@ -503,6 +502,7 @@ struct elf *elf_read(const char *name, imemset (elf,0 ,sizeof (*elf));hash_init (elf->symbol_hash); +hash_init (elf->symbol_name_hash);hash_init (elf->section_hash);hash_init (elf->section_name_hash);INIT_LIST_HEAD (&elf->sections);
tools/objtool/elf.h +2 -0
@@ -47,6 +47,7 @@ struct symbol {struct list_head list;struct rb_node node;struct hlist_node hash; +struct hlist_node name_hash;GElf_Sym sym;struct section *sec;char *name;
@@ -77,6 +78,7 @@ struct elf {char *name;struct list_head sections;DECLARE_HASHTABLE (symbol_hash,20 ); +DECLARE_HASHTABLE (symbol_name_hash,20 );DECLARE_HASHTABLE (section_hash,16 );DECLARE_HASHTABLE (section_name_hash,16 ); };
Peter Zijlstra
maintainerRFC PATCH 12/16 objtool: Optimize read_sections()
In reply to Peter ZijlstraPerf 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.
--- tools/objtool/elf.c | 18 ++++++++++++------ tools/objtool/elf.h | 18 +++++++++++++++++- tools/objtool/orc_gen.c | 3 ++- 3 files changed, 31 insertions(+), 8 deletions(-)
tools/objtool/elf.c +12 -6
@@ -210,10 +210,15 @@ struct rela *find_rela_by_dest_range(strif (!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, ihash_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);
tools/objtool/elf.h +17 -1
@@ -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);
tools/objtool/orc_gen.c +2 -1
@@ -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 ; }
Re: [RFC][PATCH 12/16] objtool: Optimize read_sections()
Quoted 11 lines · Peter Zijlstra
+++
b/tools/objtool/elf.htools/objtool/elf.h +2
-0@@ -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;
tools/objtool/elf.h struct section { · line 38
Quoted 5 lines
tools/objtool/elf.h +1
-1@@ -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;
Instead of adding 'elf' here I'd rather just add it as an argument to the find_rela_by_dest*() functions.
Signature · 1 line
Josh
Peter Zijlstra
maintainerRFC PATCH 13/16 objtool: Delete cleanup()
In reply to Peter ZijlstraPerf shows we spend a measurable amount of time spend cleaning up right before we exit anyway. Avoid the needsless work and just terminate.
This reduces objtool on vmlinux.o runtime from 5.4s to 4.8s
--- tools/objtool/check.c | 19 ------------------- 1 file changed, 19 deletions(-)
tools/objtool/check.c +0 -19
@@ -2520,23 +2520,6 @@ static int validate_reachable_instructioreturn 0 ; } -static void cleanup (struct objtool_file *file) -{ -struct instruction *insn, *tmpinsn; -struct alternative *alt, *tmpalt; - -list_for_each_entry_safe (insn, tmpinsn, &file->insn_list, list) { -list_for_each_entry_safe (alt, tmpalt, &insn->alts, list) { -list_del (&alt->list); -free (alt); - } -list_del (&insn->list); -hash_del (&insn->hash); -free (insn); - } -elf_close (file->elf); -} -static struct objtool_file file;int check (const char *_objname,bool orc)
@@ -2613,8 +2596,6 @@ int check(const char *_objname, bool orc } out: -cleanup (&file); -/* ignore warnings for now until we get all the code cleaned up */ if (ret || warnings)return 0 ;
Peter Zijlstra
maintainerRFC PATCH 14/16 objtool: Optimize find_rela_by_dest_range()
In reply to Peter ZijlstraPerf 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.
--- tools/objtool/elf.c | 15 +++++++++++---- tools/objtool/elf.h | 11 ++++++++++- 2 files changed, 21 insertions(+), 5 deletions(-)
tools/objtool/elf.c +11 -4
@@ -204,7 +204,7 @@ struct symbol *find_symbol_by_name(strucstruct rela *find_rela_by_dest_range (struct section *sec,unsigned long offset,unsigned int len) { -struct rela *rela; +struct rela *rela, *r =NULL ;unsigned long o;if (!sec->rela)
@@ -212,12 +212,19 @@ struct rela *find_rela_by_dest_range(str sec = sec->rela; -for (o = offset; o < offset + len; o++) { +for (o = offset &RELA_STRIDE_MASK ; o < offset + len; o +=RELA_STRIDE ) {hash_for_each_possible (sec->elf->rela_hash, rela, hash,__rela_hash (o, sec->idx)) { -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 ;
tools/objtool/elf.h +10 -1
@@ -86,9 +86,18 @@ struct elf {DECLARE_HASHTABLE (rela_hash,20 ); }; +#define RELA_STRIDE_BITS 4 +#define RELA_STRIDE (1UL << RELA_STRIDE_BITS) +#define RELA_STRIDE_MASK (~(RELA_STRIDE - 1)) +static inline u32 __rela_hash (unsigned long offset,int idx) { -u32 ol = offset, oh = offset >>32 ; +u32 ol, oh; + + offset &=RELA_STRIDE_MASK ; + + ol = offset; + oh = offset >>32 ;__jhash_mix (ol, oh, idx);
Peter Zijlstra
maintainerRFC PATCH 15/16 objtool: Implement noinstr validation
In reply to Peter ZijlstraValidate that any call out of .noinstr.text is in between instr_begin() and instr_end() annotations.
This annotation is useful to ensure correct behaviour wrt tracing sensitive code like entry/exit and idle code. When we run code in a sensitive context we want a guarantee no unknown code is ran.
Since this validation relies on knowing the section of call destination symbols, we must run it on vmlinux.o instead of on individual object files.
Add the -i "noinstr validation only" option because:
- vmlinux.o isn't 'clean' vs the existing validations
- skipping the other validations (which have already been done earlier in the build) saves around a second of runtime.
--- tools/objtool/builtin-check.c | 4 - tools/objtool/builtin.h | 2 tools/objtool/check.c | 155 ++++++++++++++++++++++++++++++++++++------ tools/objtool/check.h | 3 4 files changed, 140 insertions(+), 24 deletions(-)
tools/objtool/builtin-check.c +3 -1
@@ -17,7 +17,7 @@#include "builtin.h" #include "check.h" -bool no_fp, no_unreachable, retpoline, module, backtrace, uaccess, stats; +bool no_fp, no_unreachable, retpoline, module, backtrace, uaccess, stats, noinstr, vmlinux;static const char *const check_usage[] = {"objtool check [<options>] file.o" ,
@@ -32,6 +32,8 @@ const struct option check_options[] = {OPT_BOOLEAN ('b' ,"backtrace" , &backtrace,"unwind on error" ),OPT_BOOLEAN ('a' ,"uaccess" , &uaccess,"enable uaccess checking" ),OPT_BOOLEAN ('s' ,"stats" , &stats,"print statistics" ), +OPT_BOOLEAN ('i' ,"noinstr" , &noinstr,"noinstr validation only" ), +OPT_BOOLEAN ('l' ,"vmlinux" , &vmlinux,"vmlinux.o validation" ), OPT_END(), };
tools/objtool/builtin.h +1 -1
@@ -8,7 +8,7 @@#include <subcmd/parse-options.h> extern const struct option check_options[]; -extern bool no_fp, no_unreachable, retpoline, module, backtrace, uaccess, stats; +extern bool no_fp, no_unreachable, retpoline, module, backtrace, uaccess, stats, noinstr, vmlinux;extern int cmd_check (int argc,const char **argv);extern int cmd_orc (int argc,const char **argv);
tools/objtool/check.c +133 -22
211 lines not on this page · raw message
tools/objtool/check.h +3 -0
@@ -20,6 +20,8 @@ struct insn_state {unsigned char type;bool bp_scratch;bool drap, end, uaccess, df; +bool noinstr; +s8 instr;unsigned int uaccess_stack;int drap_reg, drap_offset;struct cfi_reg vals[CFI_NUM_REGS ];
@@ -35,6 +37,7 @@ struct instruction {unsigned long immediate;bool alt_group, dead_end, ignore, hint, save, restore, ignore_alts;bool retpoline_safe; +s8 instr;u8 visited;struct symbol *call_dest;struct instruction *jump_dest;
Re: [RFC][PATCH 15/16] objtool: Implement noinstr validation
Quoted 11 lines · Peter Zijlstra
Validate that any call out of .noinstr.text is in between
instr_begin()andinstr_end()annotations.This annotation is useful to ensure correct behaviour wrt tracing sensitive code like entry/exit and idle code. When we run code in a sensitive context we want a guarantee no unknown code is ran.
Since this validation relies on knowing the section of call destination symbols, we must run it on vmlinux.o instead of on individual object files.
Quoted 7 lines
Add the -i "noinstr validation only" option because:
- vmlinux.o isn't 'clean' vs the existing validations
- skipping the other validations (which have already been done earlier in the build) saves around a second of runtime.Signed-off-by Peter Zijlstra (Intel) <[email protected]>
I find the phrase "noinstr" to be WAY too ambiguous. To my brain it clearly stands for "no instructions" and I have to do a double take every time I read it.
And "read_instr_hints" reads as "read_instruction_hints()".
Can we come up with a more readable name? Why not just "notrace"?
The trace begin/end annotations could be
trace_allow_begin()
trace_allow_end()
Also -- what happens when a function belongs in both .notrace.text and in one of the other special-purpose sections like .sched.text, .meminit.text or .entry.text?
Maybe storing pointers to the functions, like NOKPROBE_SYMBOL does, would be better than putting the functions in a separate section.
Quoted 19 lines
--- tools/objtool/builtin-check.c | 4 - tools/objtool/builtin.h | 2 tools/objtool/check.c | 155 ++++++++++++++++++++++++++++++++++++------ tools/objtool/check.h | 3 4 files changed, 140 insertions(+), 24 deletions(-)tools/objtool/builtin-check.c +1
-1@@ -17,7 +17,7 @@#include "builtin.h" #include "check.h" -bool no_fp, no_unreachable, retpoline, module, backtrace, uaccess, stats; +bool no_fp, no_unreachable, retpoline, module, backtrace, uaccess, stats, noinstr, vmlinux;static const char *const check_usage[] = {"objtool check [<options>] file.o" ,
tools/objtool/builtin-check.c const struct option check_options[] = { · line 38
Quoted 7 lines
tools/objtool/builtin-check.c +2
-0@@ -32,6 +32,8 @@ const struct option check_options[] = {OPT_BOOLEAN ('b' ,"backtrace" , &backtrace,"unwind on error" ),OPT_BOOLEAN ('a' ,"uaccess" , &uaccess,"enable uaccess checking" ),OPT_BOOLEAN ('s' ,"stats" , &stats,"print statistics" ), +OPT_BOOLEAN ('i' ,"noinstr" , &noinstr,"noinstr validation only" ), +OPT_BOOLEAN ('l' ,"vmlinux" , &vmlinux,"vmlinux.o validation" ), OPT_END(), };
It seems like there should be an easy way to auto-detect vmlinux.o, without needing a cmdline option.
For example, if the file name is "vmlinux.o" :-)
Also, maybe we can just hard-code the fact that vmlinux.o is always noinstr-only. Over time we'll probably need to move more per-.o functionalities to vmlinux.o and I think we should avoid creating a bunch of cmdline options.
Signature · 1 line
Josh
Peter Zijlstra
maintainerRe: [RFC][PATCH 15/16] objtool: Implement noinstr validation
Quoted 18 lines · Josh Poimboeuf
Quoted 17 lines · Peter Zijlstra
Validate that any call out of .noinstr.text is in between
instr_begin()andinstr_end()annotations.This annotation is useful to ensure correct behaviour wrt tracing sensitive code like entry/exit and idle code. When we run code in a sensitive context we want a guarantee no unknown code is ran.
Since this validation relies on knowing the section of call destination symbols, we must run it on vmlinux.o instead of on individual object files.
Add the -i "noinstr validation only" option because:
- vmlinux.o isn't 'clean' vs the existing validations
- skipping the other validations (which have already been done earlier in the build) saves around a second of runtime.
Quoted 5 lines
Quoted 1 line
Signed-off-by Peter Zijlstra (Intel) <[email protected]>I find the phrase "noinstr" to be WAY too ambiguous. To my brain it clearly stands for "no instructions" and I have to do a double take every time I read it.
Thing is, we use insn for instruction all over both arch/x86/ and objtool.
My personal naming preference didn't make it due to CoC reasons :-/
Quoted 8 lines
And "
read_instr_hints" reads as "read_instruction_hints()".Can we come up with a more readable name? Why not just "notrace"?
The trace begin/end annotations could be
trace_allow_begin()
trace_allow_end()
notrace already exists and we didn't want to confuse things further.
Quoted 3 lines
Also -- what happens when a function belongs in both .notrace.text and in one of the other special-purpose sections like .sched.text, .meminit.text or .entry.text?
Hasn't happened yet, initially we were thinking of using .entry.text for this as a whole, but decided against that due to how .entry.text is special for PTI (although exposing most of this code really wouldn't matter).
The thing with .sched.text is that we really should never call into scheduling from these contexts anyway. We've not ran into meminit yet. (all this finicky entry code is ran with IRQs disabled).
The one that could potentially interfere is .cpuidle.text.
Quoted 2 lines
Maybe storing pointers to the functions, like
NOKPROBE_SYMBOLdoes, would be better than putting the functions in a separate section.
Thing is, I really _hate_ that annotation style.
Quoted 30 lines
Quoted 19 lines
--- tools/objtool/builtin-check.c | 4 - tools/objtool/builtin.h | 2 tools/objtool/check.c | 155 ++++++++++++++++++++++++++++++++++++------ tools/objtool/check.h | 3 4 files changed, 140 insertions(+), 24 deletions(-)tools/objtool/builtin-check.c +1
-1@@ -17,7 +17,7 @@#include "builtin.h" #include "check.h" -bool no_fp, no_unreachable, retpoline, module, backtrace, uaccess, stats; +bool no_fp, no_unreachable, retpoline, module, backtrace, uaccess, stats, noinstr, vmlinux;static const char *const check_usage[] = {"objtool check [<options>] file.o" ,tools/objtool/builtin-check.c const struct option check_options[] = { · line 38
Quoted 7 lines
tools/objtool/builtin-check.c +2
-0@@ -32,6 +32,8 @@ const struct option check_options[] = {OPT_BOOLEAN ('b' ,"backtrace" , &backtrace,"unwind on error" ),OPT_BOOLEAN ('a' ,"uaccess" , &uaccess,"enable uaccess checking" ),OPT_BOOLEAN ('s' ,"stats" , &stats,"print statistics" ), +OPT_BOOLEAN ('i' ,"noinstr" , &noinstr,"noinstr validation only" ), +OPT_BOOLEAN ('l' ,"vmlinux" , &vmlinux,"vmlinux.o validation" ), OPT_END(), };It seems like there should be an easy way to auto-detect vmlinux.o, without needing a cmdline option.
Quoted 1 line
For example, if the file name is "vmlinux.o" :-)
Fair enough I suppose...
Quoted 4 lines
Also, maybe we can just hard-code the fact that vmlinux.o is always noinstr-only. Over time we'll probably need to move more per-.o functionalities to vmlinux.o and I think we should avoid creating a bunch of cmdline options.
but you're ruining things here, see, for a regular x86_64 config, we'd run this with:
objtool check -fail vmlinux.o
And I was hoping to get vmlinux.o objtool clean, surprisingly there really aren't that many complaints. But the -i thing makes it run significantly faster without duplicating all the bits we've already checked.
Anyway, let me address and refresh the series while we bicket about naming later. I'm thikning Thomas would much prefer to get his first round of patches out first.
Re: [RFC][PATCH 15/16] objtool: Implement noinstr validation
Quoted 10 lines · Peter Zijlstra
Quoted 8 lines
And "
read_instr_hints" reads as "read_instruction_hints()".Can we come up with a more readable name? Why not just "notrace"?
The trace begin/end annotations could be
trace_allow_begin()
trace_allow_end()notrace already exists and we didn't want to confuse things further.
Um, why would it confuse things to call a section of notrace code ".notrace.text"???
Quoted 13 lines
Quoted 3 lines
Also -- what happens when a function belongs in both .notrace.text and in one of the other special-purpose sections like .sched.text, .meminit.text or .entry.text?
Hasn't happened yet, initially we were thinking of using .entry.text for this as a whole, but decided against that due to how .entry.text is special for PTI (although exposing most of this code really wouldn't matter).
The thing with .sched.text is that we really should never call into scheduling from these contexts anyway. We've not ran into meminit yet. (all this finicky entry code is ran with IRQs disabled).
Quoted 6 lines
The one that could potentially interfere is .cpuidle.text.
Quoted 2 lines
Maybe storing pointers to the functions, like
NOKPROBE_SYMBOLdoes, would be better than putting the functions in a separate section.Thing is, I really _hate_ that annotation style.
I do too, but I get the feeling this "put everything in its own section" thing is going to bite us.
Quoted 8 lines
Quoted 4 lines
Also, maybe we can just hard-code the fact that vmlinux.o is always noinstr-only. Over time we'll probably need to move more per-.o functionalities to vmlinux.o and I think we should avoid creating a bunch of cmdline options.
but you're ruining things here, see, for a regular
x86_64config, we'd run this with:
Quoted 6 lines
objtool check -fail vmlinux.o
And I was hoping to get vmlinux.o objtool clean, surprisingly there really aren't that many complaints. But the -i thing makes it run significantly faster without duplicating all the bits we've already checked.
My suggestion is that the "-i" option would be hard-coded (for now). So nothing extra would get checked.
Signature · 1 line
Josh
Re: [RFC][PATCH 15/16] objtool: Implement noinstr validation
Quoted 7 lines · Josh Poimboeuf
Quoted 4 lines
And I was hoping to get vmlinux.o objtool clean, surprisingly there really aren't that many complaints. But the -i thing makes it run significantly faster without duplicating all the bits we've already checked.
My suggestion is that the "-i" option would be hard-coded (for now). So nothing extra would get checked.
If that wasn't clear, I mean that for vmlinux.o we'd only do the instr-checking. For individual .o's we'd do everything else.
Signature · 1 line
Josh
Peter Zijlstra
maintainerRe: [RFC][PATCH 15/16] objtool: Implement noinstr validation
Quoted 11 lines · Josh Poimboeuf
Quoted 7 lines · Josh Poimboeuf
Quoted 4 lines
And I was hoping to get vmlinux.o objtool clean, surprisingly there really aren't that many complaints. But the -i thing makes it run significantly faster without duplicating all the bits we've already checked.
My suggestion is that the "-i" option would be hard-coded (for now). So nothing extra would get checked.
If that wasn't clear, I mean that for vmlinux.o we'd only do the instr-checking. For individual .o's we'd do everything else.
That'd get in the way of making vmlinux.o objtool clean though :/
Peter Zijlstra
maintainerRe: [RFC][PATCH 15/16] objtool: Implement noinstr validation
In reply to Josh PoimboeufQuoted 7 lines · Josh Poimboeuf
Quoted 6 lines · Peter Zijlstra
Quoted 6 lines
And "
read_instr_hints" reads as "read_instruction_hints()".Can we come up with a more readable name? Why not just "notrace"?
The trace begin/end annotations could be
Quoted 7 lines
Quoted 4 lines
Quoted 2 lines
trace_allow_begin()
trace_allow_end()notrace already exists and we didn't want to confuse things further.
Um, why would it confuse things to call a section of notrace code ".notrace.text"???
Because it is strictly stronger than the notrace attribute is. And we certainly don't want all that is now marked notrace to end up there.
Our section also very much excludes kprobes and hardware breakpoints for starters.
Re: [RFC][PATCH 15/16] objtool: Implement noinstr validation
Quoted 11 lines · Peter Zijlstra
Quoted 10 lines · Josh Poimboeuf
Quoted 9 lines · Peter Zijlstra
Quoted 8 lines
And "
read_instr_hints" reads as "read_instruction_hints()".Can we come up with a more readable name? Why not just "notrace"?
The trace begin/end annotations could be
trace_allow_begin()
trace_allow_end()
Quoted 7 lines
Quoted 4 lines
Quoted 1 line
notrace already exists and we didn't want to confuse things further.
Um, why would it confuse things to call a section of notrace code ".notrace.text"???
Because it is strictly stronger than the notrace attribute is. And we certainly don't want all that is now marked notrace to end up there.
Ok, I must have misunderstood, I thought it was *all* notrace code going in there.
I still hope we can come up with a better name.
Signature · 1 line
Josh
Peter Zijlstra
maintainerRFC PATCH 16/16 objtool: Optimize !vmlinux.o again
In reply to Peter ZijlstraWhen doing kbuild tests to see if the objtool changes affected those I found that there was a measurable regression:
pre post
real 1m13.594 1m16.488s user 34m58.246s 35m23.947s sys 4m0.393s 4m27.312s
Perf showed that for small files the increased hash-table sizes were a measurable difference. Since we already have -l "vmlinux" to distinguish between the modes, make it also use a smaller portion of the hash-tables.
This flips it into a small win:
real 1m14.143s user 34m49.292s sys 3m44.746s
--- tools/objtool/elf.c | 40 ++++++++++++++++++++++++++++------------ tools/objtool/elf.h | 4 ++-- 2 files changed, 30 insertions(+), 14 deletions(-)
tools/objtool/elf.c +28 -12
@@ -27,6 +27,21 @@ static inline u32 str_hash(const char *sreturn jhash (str,strlen (str),0 ); } +static inline int elf_hash_bits (void ) +{ +return vmlinux ?20 :16 ; +} + +static inline void elf_hash_add (struct hlist_head *table,struct hlist_node *node,u32 key) +{ +hlist_add_head (node, &table[hash_32 (key,elf_hash_bits ())]); +} + +static void elf_hash_init (struct hlist_head *table) +{ +__hash_init (table,1U <<elf_hash_bits ()); +} +static void rb_add (struct rb_root *tree,struct rb_node *node,int (*cmp)(struct rb_node *,const struct rb_node *)) {
@@ -300,8 +315,8 @@ static int read_sections(struct elf *elf } sec->len = sec->sh.sh_size; -hash_add (elf->section_hash, &sec->hash, sec->idx); -hash_add (elf->section_name_hash, &sec->name_hash,str_hash (sec->name)); +elf_hash_add (elf->section_hash, &sec->hash, sec->idx); +elf_hash_add (elf->section_name_hash, &sec->name_hash,str_hash (sec->name)); }if (stats)
@@ -387,8 +402,8 @@ static int read_symbols(struct elf *elf) entry = &sym->sec->symbol_list;list_add (&sym->list, entry); -hash_add (elf->symbol_hash, &sym->hash, sym->idx); -hash_add (elf->symbol_name_hash, &sym->name_hash,str_hash (sym->name)); +elf_hash_add (elf->symbol_hash, &sym->hash, sym->idx); +elf_hash_add (elf->symbol_name_hash, &sym->name_hash,str_hash (sym->name)); }if (stats)
@@ -497,7 +512,7 @@ static int read_relas(struct elf *elf) }list_add_tail (&rela->list, &sec->rela_list); -hash_add (elf->rela_hash, &rela->hash,rela_hash (rela)); +elf_hash_add (elf->rela_hash, &rela->hash,rela_hash (rela)); nr_rela++; } max_rela =max (max_rela, nr_rela);
@@ -524,15 +539,16 @@ struct elf *elf_read(const char *name, iperror ("malloc" );return NULL ; } -memset (elf,0 ,sizeof (*elf)); +memset (elf,0 , offsetof(struct elf , sections)); -hash_init (elf->symbol_hash); -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_hash_init (elf->symbol_hash); +elf_hash_init (elf->symbol_name_hash); +elf_hash_init (elf->section_hash); +elf_hash_init (elf->section_name_hash); +elf_hash_init (elf->rela_hash); + elf->fd =open (name, flags);if (elf->fd ==-1 ) { fprintf(stderr, "objtool: Can't open '%s': %s\n",
@@ -671,7 +687,7 @@ struct section *elf_create_section(struc shstrtab->len +=strlen (name) +1 ; shstrtab->changed = true; -hash_add (elf->section_hash, &sec->hash, sec->idx); +elf_hash_add (elf->section_hash, &sec->hash, sec->idx);return sec; }
tools/objtool/elf.h +2 -2
@@ -81,8 +81,8 @@ struct elf {struct list_head sections;DECLARE_HASHTABLE (symbol_hash,20 );DECLARE_HASHTABLE (symbol_name_hash,20 ); -DECLARE_HASHTABLE (section_hash,16 ); -DECLARE_HASHTABLE (section_name_hash,16 ); +DECLARE_HASHTABLE (section_hash,20 ); +DECLARE_HASHTABLE (section_name_hash,20 );DECLARE_HASHTABLE (rela_hash,20 ); };
Peter Zijlstra
maintainerRFC PATCH v2 16/16 objtool: Optimize !vmlinux.o again
Turns out I lost a refresh on this one.
---
Subject: objtool: Optimize !vmlinux.o again
From: Peter Zijlstra <[email protected]>
Date: Thu Mar 12 14:29:38 CET 2020
When doing kbuild tests to see if the objtool changes affected those I found that there was a measurable regression:
pre post
real 1m13.594 1m16.488s user 34m58.246s 35m23.947s sys 4m0.393s 4m27.312s
Perf showed that for small files the increased hash-table sizes were a measurable difference. Since we already have -l "vmlinux" to distinguish between the modes, make it also use a smaller portion of the hash-tables.
This flips it into a small win:
real 1m14.143s user 34m49.292s sys 3m44.746s
--- tools/objtool/elf.c | 51 ++++++++++++++++++++++++++++++++++----------------- tools/objtool/elf.h | 4 ++-- 2 files changed, 36 insertions(+), 19 deletions(-)
tools/objtool/elf.c +34 -17
@@ -27,6 +27,22 @@ static inline u32 str_hash(const char *sreturn jhash (str,strlen (str),0 ); } +static inline int elf_hash_bits (void ) +{ +return vmlinux ?20 :16 ; +} + +#define elf_hash_add (hashtable, node, key) \ + hlist_add_head(node, &hashtable[hash_min(key, elf_hash_bits())]) + +static void elf_hash_init (struct hlist_head *table) +{ +__hash_init (table,1U <<elf_hash_bits ()); +} + +#define elf_hash_for_each_possible (name, obj, member, key) \ + hlist_for_each_entry(obj, &name[hash_min(key, elf_hash_bits())], member) +static void rb_add (struct rb_root *tree,struct rb_node *node,int (*cmp)(struct rb_node *,const struct rb_node *)) {
@@ -115,7 +131,7 @@ struct section *find_section_by_name(str {struct section *sec; -hash_for_each_possible (elf->section_name_hash, sec, name_hash,str_hash (name)) +elf_hash_for_each_possible (elf->section_name_hash, sec, name_hash,str_hash (name))if (!strcmp (sec->name, name))return sec;
@@ -127,7 +143,7 @@ static struct section *find_section_by_i {struct section *sec; -hash_for_each_possible (elf->section_hash, sec, hash, idx) +elf_hash_for_each_possible (elf->section_hash, sec, hash, idx)if (sec->idx == idx)return sec;
@@ -138,7 +154,7 @@ static struct symbol *find_symbol_by_ind {struct symbol *sym; -hash_for_each_possible (elf->symbol_hash, sym, hash, idx) +elf_hash_for_each_possible (elf->symbol_hash, sym, hash, idx)if (sym->idx == idx)return sym;
@@ -194,7 +210,7 @@ struct symbol *find_symbol_by_name(struc {struct symbol *sym; -hash_for_each_possible (elf->symbol_name_hash, sym, name_hash,str_hash (name)) +elf_hash_for_each_possible (elf->symbol_name_hash, sym, name_hash,str_hash (name))if (!strcmp (sym->name, name))return sym;
@@ -213,7 +229,7 @@ struct rela *find_rela_by_dest_range(str sec = sec->rela;for (o = offset &RELA_STRIDE_MASK ; o < offset + len; o +=RELA_STRIDE ) { -hash_for_each_possible (sec->elf->rela_hash, rela, hash, +elf_hash_for_each_possible (sec->elf->rela_hash, rela, hash,__rela_hash (o, sec->idx)) {if (rela->sec != sec)continue ;
@@ -300,8 +316,8 @@ static int read_sections(struct elf *elf } sec->len = sec->sh.sh_size; -hash_add (elf->section_hash, &sec->hash, sec->idx); -hash_add (elf->section_name_hash, &sec->name_hash,str_hash (sec->name)); +elf_hash_add (elf->section_hash, &sec->hash, sec->idx); +elf_hash_add (elf->section_name_hash, &sec->name_hash,str_hash (sec->name)); }if (stats)
@@ -387,8 +403,8 @@ static int read_symbols(struct elf *elf) entry = &sym->sec->symbol_list;list_add (&sym->list, entry); -hash_add (elf->symbol_hash, &sym->hash, sym->idx); -hash_add (elf->symbol_name_hash, &sym->name_hash,str_hash (sym->name)); +elf_hash_add (elf->symbol_hash, &sym->hash, sym->idx); +elf_hash_add (elf->symbol_name_hash, &sym->name_hash,str_hash (sym->name)); }if (stats)
@@ -497,7 +513,7 @@ static int read_relas(struct elf *elf) }list_add_tail (&rela->list, &sec->rela_list); -hash_add (elf->rela_hash, &rela->hash,rela_hash (rela)); +elf_hash_add (elf->rela_hash, &rela->hash,rela_hash (rela)); nr_rela++; } max_rela =max (max_rela, nr_rela);
@@ -524,15 +540,16 @@ struct elf *elf_read(const char *name, iperror ("malloc" );return NULL ; } -memset (elf,0 ,sizeof (*elf)); +memset (elf,0 , offsetof(struct elf , sections)); -hash_init (elf->symbol_hash); -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_hash_init (elf->symbol_hash); +elf_hash_init (elf->symbol_name_hash); +elf_hash_init (elf->section_hash); +elf_hash_init (elf->section_name_hash); +elf_hash_init (elf->rela_hash); + elf->fd =open (name, flags);if (elf->fd ==-1 ) { fprintf(stderr, "objtool: Can't open '%s': %s\n",
@@ -671,7 +688,7 @@ struct section *elf_create_section(struc shstrtab->len +=strlen (name) +1 ; shstrtab->changed = true; -hash_add (elf->section_hash, &sec->hash, sec->idx); +elf_hash_add (elf->section_hash, &sec->hash, sec->idx);return sec; }
tools/objtool/elf.h +2 -2
@@ -81,8 +81,8 @@ struct elf {struct list_head sections;DECLARE_HASHTABLE (symbol_hash,20 );DECLARE_HASHTABLE (symbol_name_hash,20 ); -DECLARE_HASHTABLE (section_hash,16 ); -DECLARE_HASHTABLE (section_name_hash,16 ); +DECLARE_HASHTABLE (section_hash,20 ); +DECLARE_HASHTABLE (section_name_hash,20 );DECLARE_HASHTABLE (rela_hash,20 ); };
Peter Zijlstra
maintainerQuoted 22 lines · Peter Zijlstra
Hi all,
These patches extend objtool to be able to run on vmlinux.o and validate Thomas's proposed noinstr annotation:
https://lkml.kernel.org/r/[email protected]
"That's why we want the sections and the annotation. If something calls
out of a noinstr section into a regular text section and the call is not annotated at the call site, then objtool can complain and tell you. What Peter and I came up with looks like this:noinstr
foo()do_protected(); <- Safe because in the noinstr section instr_begin(); <- Marks the begin of a safe region, ignored by objtool do_stuff(); <- All good instr_end(); <- End of the safe region. objtool starts looking again do_other_stuff(); <- Unsafe because do_other_stuff() is not protected
Quoted 9 lines
and:
noinstr
do_protected()bar(); <- objtool will complain here"
It should be accompanied by something like the below; which you'll find in a series by Thomas.
So one of the problem i've ran into while playing with this and Thomas' patches is that it is 'difficult' to deal with indirect function calls.
objtool basically gives up instantly.
I know smatch has passes were it looks for function pointer assignments and carries that forward into it's callchain generation. Doing something like that for objtool is going to be 'fun'...
For now I've got limited success dodging a few instances with __always_inline (which then results in the compiler resolving the indirection).
Quoted 8 lines · Peter Zijlstra
So one of the problem i've ran into while playing with this and Thomas' patches is that it is 'difficult' to deal with indirect function calls.
objtool basically gives up instantly.
I know smatch has passes were it looks for function pointer assignments and carries that forward into it's callchain generation. Doing something like that for objtool is going to be 'fun'...
Yeah, it would have to keep track of register and memory values, even across function calls. Which is basically crossing over into emulation territory. Which objtool wasn't really built for.
Signature · 1 line
Josh
Peter Zijlstra
maintainerQuoted 5 lines · Peter Zijlstra
So one of the problem i've ran into while playing with this and Thomas' patches is that it is 'difficult' to deal with indirect function calls.
objtool basically gives up instantly.
Quoted 7 lines
I know smatch has passes were it looks for function pointer assignments and carries that forward into it's callchain generation. Doing something like that for objtool is going to be 'fun'...
For now I've got limited success dodging a few instances with
__always_inline(which then results in the compiler resolving the indirection).
Here's a little something that at least detects 'immediate' function pointers crossing the boundary.
It's slow though; it almost tripples the runtime. But I'm too tired to make it fast, maybe tomorrow.
---
tools/objtool/check.c +35 -2
@@ -247,6 +247,9 @@ static int decode_instructions(struct ob strncmp(sec->name, ".discard.", 9)) sec->text = true; +if (!strcmp (sec->name,".noinstr.text" )) + sec->noinstr = true; +for (offset =0 ; offset < sec->len; offset += insn->len) { insn =malloc (sizeof (*insn));if (!insn) {
@@ -2040,6 +2043,28 @@ static int validate_return(struct symbolreturn 0 ; } +static int validate_rela (struct instruction *insn,struct insn_state *state) +{ +struct section *sec; +struct rela *rela; + +if (!(state->noinstr && state->instr <=0 )) +return 0 ; + + rela =find_rela_by_dest_range (insn->sec, insn->offset, insn->len); +if (!rela || !rela->sym || !rela->sym->sec) +return 0 ; + + sec = rela->sym->sec; +if (sec->text && !sec->noinstr) { +WARN_FUNC ("loading non-noinstr function pointer\n" , + insn->sec, insn->offset); +return 1 ; + } + +return 0 ; +} +/* * Follow the branch starting at the given instruction, and recursively follow * any other branches (jumps). Meanwhile, track the frame pointer state at
@@ -2222,6 +2247,10 @@ static int validate_branch(struct objtooreturn 0 ;case INSN_STACK : + ret =validate_rela (insn, &state); +if (ret) +return ret; +if (update_insn_state (insn, &state))return 1 ;
@@ -2285,6 +2314,10 @@ static int validate_branch(struct objtoobreak ;default : + ret =validate_rela (insn, &state); +if (ret) +return ret; +break ; }
@@ -2442,8 +2475,8 @@ static int validate_sec_functions(struct* not correctly determine insn->call_dest->sec (external symbols do * not have a section). */ -if (vmlinux && !strcmp (sec->name,".noinstr.text" )) - state.noinstr = true; +if (vmlinux) + state.noinstr = sec->noinstr;list_for_each_entry (func, &sec->symbol_list, list) {if (func->type !=STT_FUNC )
tools/objtool/elf.h +1 -1
@@ -43,7 +43,7 @@ struct section {char *name;int idx;unsigned int len; -bool changed, text, rodata; +bool changed, text, rodata, noinstr; };struct symbol {
Quoted 29 lines · On Thu, 12 Mar 2020 17:23:37 +0100 Peter Zijlstra <[email protected]> wrote:
Quoted 28 lines · Peter Zijlstra
Hi all,
These patches extend objtool to be able to run on vmlinux.o and validate Thomas's proposed noinstr annotation:
https://lkml.kernel.org/r/[email protected]
"That's why we want the sections and the annotation. If something calls
out of a noinstr section into a regular text section and the call is not annotated at the call site, then objtool can complain and tell you. What Peter and I came up with looks like this:noinstr
foo()do_protected(); <- Safe because in the noinstr section instr_begin(); <- Marks the begin of a safe region, ignored by objtool do_stuff(); <- All good instr_end(); <- End of the safe region. objtool starts looking again do_other_stuff(); <- Unsafe because do_other_stuff() is not protectedand:
noinstr
do_protected()bar(); <- objtool will complain here"
Quoted 8 lines
Quoted 3 lines
It should be accompanied by something like the below; which you'll find in a series by Thomas.
So one of the problem i've ran into while playing with this and Thomas' patches is that it is 'difficult' to deal with indirect function calls.
objtool basically gives up instantly.
Can we introduce a "safe-call" wrapper function instead of indirect call, and if objtool found an indirect call without safe-call function, it can make it an error?
static int __noinstr safe_indirect_callback(int (*fn)(...), real-args)
{
if (!is_instr_text(fn))
return -ERANGE;
return fn(real-args)
}BTW, out of curiously, if BUG*() or WARN*() cases happens in the noinstr section, do we also need to move them (register dump, stack unwinding, printk, console output, etc.) all into noinstr section?
Thank you,
Signature · 1 line
Masami Hiramatsu <[email protected]>
Quoted 3 lines · Masami Hiramatsu
BTW, out of curiously, if BUG*() or WARN*() cases happens in the noinstr section, do we also need to move them (register dump, stack unwinding, printk, console output, etc.) all into noinstr section?
The current plan is to declare BUG()/WARN() "safe". On x86 it is kinda safe as it uses UD2. That raises an exception which handles the bug/warn after establishing the correct state.
Of course it's debatable, but moving all of this into the noinstr section might be just a too wide scope.
Thanks,
tglx
Quoted 8 lines · On Tue, 17 Mar 2020 10:26:49 +0100 Thomas Gleixner <[email protected]> wrote:
Quoted 3 lines · Masami Hiramatsu
BTW, out of curiously, if BUG*() or WARN*() cases happens in the noinstr section, do we also need to move them (register dump, stack unwinding, printk, console output, etc.) all into noinstr section?
The current plan is to declare BUG()/WARN() "safe". On x86 it is kinda safe as it uses UD2. That raises an exception which handles the bug/warn after establishing the correct state.
OK, so at least the entry of BUG()/WARN() is in noinstr, but the register/stack dump routines are out of noinstr.
Quoted 3 lines
Of course it's debatable, but moving all of this into the noinstr section might be just a too wide scope.
Agreed.
Thank you,
Signature · 1 line
Masami Hiramatsu <[email protected]>
Peter Zijlstra
maintainerQuoted 12 lines · Masami Hiramatsu
Quoted 4 lines · On Thu, 12 Mar 2020 17:23:37 +0100 Peter Zijlstra <[email protected]> wrote:
So one of the problem i've ran into while playing with this and Thomas' patches is that it is 'difficult' to deal with indirect function calls.
objtool basically gives up instantly.
Can we introduce a "safe-call" wrapper function instead of indirect call, and if objtool found an indirect call without safe-call function, it can make it an error?
Quoted 6 lines
static int
__noinstrsafe_indirect_callback(int (*fn)(...), real-args){ if (!is_instr_text(fn)) return -ERANGE; return fn(real-args) }
That is a runtime test and as such susceptible to code coverage issues.
I could probably frob a few cases manually in objtool; so far I've managed to just make them go away.
Quoted 3 lines
BTW, out of curiously, if BUG*() or WARN*() cases happens in the noinstr section, do we also need to move them (register dump, stack unwinding, printk, console output, etc.) all into noinstr section?
Since BUG/WARN should not happen, we've added instr_begin()/instr_end() to their slow path. If those trigger, we've got bigger issues.
kbuild test robot
build bot[if your patch is applied to the wrong git tree, please drop us a note to help improve the system. BTW, we also suggest to use '--base' option to specify the base tree in git format-patch, please…
Hi Peter,
[FYI, it's a private test report for your RFC patch.]
[auto build test WARNING on kbuild/for-next]
[also build test WARNING on tip/x86/core linus/master v5.6-rc5 next-20200312] [if your patch is applied to the wrong git tree, please drop us a note to help improve the system. BTW, we also suggest to use '--base' option to specify the base tree in git format-patch, please see https://stackoverflow.com/a/37406982]
url: https://github.com/0day-ci/linux/commits/Peter-Zijlstra/objtool-vmlinux-o-and-noinstr-validation/20200313-050611 base: https://git.kernel.org/pub/scm/linux/kernel/git/masahiroy/linux-kbuild.git for-next config: m68k-allmodconfig (attached as .config) compiler: m68k-linux-gcc (GCC) 9.2.0
reproduce:
wget https://raw.githubusercontent.com/intel/lkp-tests/master/sbin/make.cross -O ~/bin/make.cross chmod +x ~/bin/make.cross
# save the attached .config to linux build tree
GCC_VERSION=9.2.0 make.cross ARCH=m68k
If you fix the issue, kindly add following tag
All warnings (new ones prefixed by >>):
In file included from drivers/net/dsa/lantiq_gswip.c:47:
>> drivers/net/dsa/lantiq_pce.h:55: warning: "INSTR" redefined
55 | #define INSTR 0
|
In file included from include/linux/err.h:5,
from include/linux/clk.h:12,
from drivers/net/dsa/lantiq_gswip.c:28:
include/linux/compiler.h:146: note: this is the location of the previous definition
146 | #define INSTR(expr) ({ \
| vim +/INSTR +55 drivers/net/dsa/lantiq_pce.h
14fceff4771e51 Hauke Mehrtens 2018-09-09 53
14fceff4771e51 Hauke Mehrtens 2018-09-09 54 /* parser's microcode length type */ 14fceff4771e51 Hauke Mehrtens 2018-09-09 @55 #define INSTR 0 14fceff4771e51 Hauke Mehrtens 2018-09-09 56 #define IPV6 1 14fceff4771e51 Hauke Mehrtens 2018-09-09 57 #define LENACCU 2
14fceff4771e51 Hauke Mehrtens 2018-09-09 58
:::::: The code at line 55 was first introduced by commit
:::::: 14fceff4771e51b23b4485b575cf9e5b3414b89b net: dsa: Add Lantiq / Intel DSA driver for vrx200
:::::: TO: Hauke Mehrtens <[email protected]>
:::::: CC: David S. Miller <[email protected]>
---
0-DAY CI Kernel Test Service, Intel Corporation
https://lists.01.org/hyperkitty/list/kbuild-all(a)lists.01.org
Quoted 22 lines · Peter Zijlstra
Hi all,
These patches extend objtool to be able to run on vmlinux.o and validate Thomas's proposed noinstr annotation:
https://lkml.kernel.org/r/[email protected]
"That's why we want the sections and the annotation. If something calls
out of a noinstr section into a regular text section and the call is not annotated at the call site, then objtool can complain and tell you. What Peter and I came up with looks like this:noinstr
foo()do_protected(); <- Safe because in the noinstr section instr_begin(); <- Marks the begin of a safe region, ignored by objtool do_stuff(); <- All good instr_end(); <- End of the safe region. objtool starts looking again do_other_stuff(); <- Unsafe because do_other_stuff() is not protected
Quoted 8 lines
and:
noinstr
do_protected()bar(); <- objtool will complain here"
It should be accompanied by something like the below; which you'll find in a series by Thomas.
Awesome work!
This is also a big step towards other eventual objtool features, like LTO compatibility.
I did have a few concerns about the overall "noinstr" implementation, mentioned in my reply to patch 15.
Other than that, and the minor comments I sent, the code looks great.
Signature · 1 line
Josh