This is a note to let you know that I've just added the patch titled objtool: Introduce CFI hash to the 5.10-stable tree which can be found at: http://www.kernel.org/git/?p=linux/kernel/git/stable/stable-queue.git;a=summary The filename of the patch is: objtool-introduce-cfi-hash.patch and it can be found in the queue-5.10 subdirectory. If you, or anyone else, feels it should not be added to the stable tree, please let <stable@xxxxxxxxxxxxxxx> know about it. >From foo@baz Tue Jul 12 05:07:35 PM CEST 2022 From: Peter Zijlstra <peterz@xxxxxxxxxxxxx> Date: Thu, 24 Jun 2021 11:41:01 +0200 Subject: objtool: Introduce CFI hash From: Peter Zijlstra <peterz@xxxxxxxxxxxxx> commit 8b946cc38e063f0f7bb67789478c38f6d7d457c9 upstream. Andi reported that objtool on vmlinux.o consumes more memory than his system has, leading to horrific performance. This is in part because we keep a struct instruction for every instruction in the file in-memory. Shrink struct instruction by removing the CFI state (which includes full register state) from it and demand allocating it. Given most instructions don't actually change CFI state, there's lots of repetition there, so add a hash table to find previous CFI instances. Reduces memory consumption (and runtime) for processing an x86_64-allyesconfig: pre: 4:40.84 real, 143.99 user, 44.18 sys, 30624988 mem post: 2:14.61 real, 108.58 user, 25.04 sys, 16396184 mem Suggested-by: Andi Kleen <andi@xxxxxxxxxxxxxx> Signed-off-by: Peter Zijlstra (Intel) <peterz@xxxxxxxxxxxxx> Link: https://lore.kernel.org/r/20210624095147.756759107@xxxxxxxxxxxxx Signed-off-by: Thadeu Lima de Souza Cascardo <cascardo@xxxxxxxxxxxxx> [bwh: Backported to 5.10: - Don't use bswap_if_needed() since we don't have any of the other fixes for mixed-endian cross-compilation - Since we don't have "objtool: Rewrite hashtable sizing", make cfi_hash_alloc() set the number of bits similarly to elf_hash_bits() - objtool doesn't have any mcount handling - Adjust context] Signed-off-by: Ben Hutchings <ben@xxxxxxxxxxxxxxx> Signed-off-by: Greg Kroah-Hartman <gregkh@xxxxxxxxxxxxxxxxxxx> --- tools/objtool/arch.h | 2 tools/objtool/arch/x86/decode.c | 20 ++--- tools/objtool/cfi.h | 2 tools/objtool/check.c | 154 +++++++++++++++++++++++++++++++++++----- tools/objtool/check.h | 2 tools/objtool/orc_gen.c | 15 ++- 6 files changed, 160 insertions(+), 35 deletions(-) --- a/tools/objtool/arch.h +++ b/tools/objtool/arch.h @@ -84,7 +84,7 @@ unsigned long arch_dest_reloc_offset(int const char *arch_nop_insn(int len); -int arch_decode_hint_reg(struct instruction *insn, u8 sp_reg); +int arch_decode_hint_reg(u8 sp_reg, int *base); bool arch_is_retpoline(struct symbol *sym); --- a/tools/objtool/arch/x86/decode.c +++ b/tools/objtool/arch/x86/decode.c @@ -706,34 +706,32 @@ int arch_rewrite_retpolines(struct objto return 0; } -int arch_decode_hint_reg(struct instruction *insn, u8 sp_reg) +int arch_decode_hint_reg(u8 sp_reg, int *base) { - struct cfi_reg *cfa = &insn->cfi.cfa; - switch (sp_reg) { case ORC_REG_UNDEFINED: - cfa->base = CFI_UNDEFINED; + *base = CFI_UNDEFINED; break; case ORC_REG_SP: - cfa->base = CFI_SP; + *base = CFI_SP; break; case ORC_REG_BP: - cfa->base = CFI_BP; + *base = CFI_BP; break; case ORC_REG_SP_INDIRECT: - cfa->base = CFI_SP_INDIRECT; + *base = CFI_SP_INDIRECT; break; case ORC_REG_R10: - cfa->base = CFI_R10; + *base = CFI_R10; break; case ORC_REG_R13: - cfa->base = CFI_R13; + *base = CFI_R13; break; case ORC_REG_DI: - cfa->base = CFI_DI; + *base = CFI_DI; break; case ORC_REG_DX: - cfa->base = CFI_DX; + *base = CFI_DX; break; default: return -1; --- a/tools/objtool/cfi.h +++ b/tools/objtool/cfi.h @@ -7,6 +7,7 @@ #define _OBJTOOL_CFI_H #include "cfi_regs.h" +#include <linux/list.h> #define CFI_UNDEFINED -1 #define CFI_CFA -2 @@ -24,6 +25,7 @@ struct cfi_init_state { }; struct cfi_state { + struct hlist_node hash; /* must be first, cficmp() */ struct cfi_reg regs[CFI_NUM_REGS]; struct cfi_reg vals[CFI_NUM_REGS]; struct cfi_reg cfa; --- a/tools/objtool/check.c +++ b/tools/objtool/check.c @@ -5,6 +5,7 @@ #include <string.h> #include <stdlib.h> +#include <sys/mman.h> #include "builtin.h" #include "cfi.h" @@ -25,7 +26,11 @@ struct alternative { bool skip_orig; }; -struct cfi_init_state initial_func_cfi; +static unsigned long nr_cfi, nr_cfi_reused, nr_cfi_cache; + +static struct cfi_init_state initial_func_cfi; +static struct cfi_state init_cfi; +static struct cfi_state func_cfi; struct instruction *find_insn(struct objtool_file *file, struct section *sec, unsigned long offset) @@ -265,6 +270,78 @@ static void init_insn_state(struct insn_ state->noinstr = sec->noinstr; } +static struct cfi_state *cfi_alloc(void) +{ + struct cfi_state *cfi = calloc(sizeof(struct cfi_state), 1); + if (!cfi) { + WARN("calloc failed"); + exit(1); + } + nr_cfi++; + return cfi; +} + +static int cfi_bits; +static struct hlist_head *cfi_hash; + +static inline bool cficmp(struct cfi_state *cfi1, struct cfi_state *cfi2) +{ + return memcmp((void *)cfi1 + sizeof(cfi1->hash), + (void *)cfi2 + sizeof(cfi2->hash), + sizeof(struct cfi_state) - sizeof(struct hlist_node)); +} + +static inline u32 cfi_key(struct cfi_state *cfi) +{ + return jhash((void *)cfi + sizeof(cfi->hash), + sizeof(*cfi) - sizeof(cfi->hash), 0); +} + +static struct cfi_state *cfi_hash_find_or_add(struct cfi_state *cfi) +{ + struct hlist_head *head = &cfi_hash[hash_min(cfi_key(cfi), cfi_bits)]; + struct cfi_state *obj; + + hlist_for_each_entry(obj, head, hash) { + if (!cficmp(cfi, obj)) { + nr_cfi_cache++; + return obj; + } + } + + obj = cfi_alloc(); + *obj = *cfi; + hlist_add_head(&obj->hash, head); + + return obj; +} + +static void cfi_hash_add(struct cfi_state *cfi) +{ + struct hlist_head *head = &cfi_hash[hash_min(cfi_key(cfi), cfi_bits)]; + + hlist_add_head(&cfi->hash, head); +} + +static void *cfi_hash_alloc(void) +{ + cfi_bits = vmlinux ? ELF_HASH_BITS - 3 : 13; + cfi_hash = mmap(NULL, sizeof(struct hlist_head) << cfi_bits, + PROT_READ|PROT_WRITE, + MAP_PRIVATE|MAP_ANON, -1, 0); + if (cfi_hash == (void *)-1L) { + WARN("mmap fail cfi_hash"); + cfi_hash = NULL; + } else if (stats) { + printf("cfi_bits: %d\n", cfi_bits); + } + + return cfi_hash; +} + +static unsigned long nr_insns; +static unsigned long nr_insns_visited; + /* * Call the arch-specific instruction decoder for all the instructions and add * them to the global instruction list. @@ -275,7 +352,6 @@ static int decode_instructions(struct ob struct symbol *func; unsigned long offset; struct instruction *insn; - unsigned long nr_insns = 0; int ret; for_each_sec(file, sec) { @@ -301,7 +377,6 @@ static int decode_instructions(struct ob memset(insn, 0, sizeof(*insn)); INIT_LIST_HEAD(&insn->alts); INIT_LIST_HEAD(&insn->stack_ops); - init_cfi_state(&insn->cfi); insn->sec = sec; insn->offset = offset; @@ -1077,7 +1152,6 @@ static int handle_group_alt(struct objto memset(nop, 0, sizeof(*nop)); INIT_LIST_HEAD(&nop->alts); INIT_LIST_HEAD(&nop->stack_ops); - init_cfi_state(&nop->cfi); nop->sec = special_alt->new_sec; nop->offset = special_alt->new_off + special_alt->new_len; @@ -1454,10 +1528,11 @@ static void set_func_state(struct cfi_st static int read_unwind_hints(struct objtool_file *file) { + struct cfi_state cfi = init_cfi; struct section *sec, *relocsec; - struct reloc *reloc; struct unwind_hint *hint; struct instruction *insn; + struct reloc *reloc; int i; sec = find_section_by_name(file->elf, ".discard.unwind_hints"); @@ -1495,19 +1570,24 @@ static int read_unwind_hints(struct objt insn->hint = true; if (hint->type == UNWIND_HINT_TYPE_FUNC) { - set_func_state(&insn->cfi); + insn->cfi = &func_cfi; continue; } - if (arch_decode_hint_reg(insn, hint->sp_reg)) { + if (insn->cfi) + cfi = *(insn->cfi); + + if (arch_decode_hint_reg(hint->sp_reg, &cfi.cfa.base)) { WARN_FUNC("unsupported unwind_hint sp base reg %d", insn->sec, insn->offset, hint->sp_reg); return -1; } - insn->cfi.cfa.offset = hint->sp_offset; - insn->cfi.type = hint->type; - insn->cfi.end = hint->end; + cfi.cfa.offset = hint->sp_offset; + cfi.type = hint->type; + cfi.end = hint->end; + + insn->cfi = cfi_hash_find_or_add(&cfi); } return 0; @@ -2283,13 +2363,18 @@ static int propagate_alt_cfi(struct objt if (!insn->alt_group) return 0; + if (!insn->cfi) { + WARN("CFI missing"); + return -1; + } + alt_cfi = insn->alt_group->cfi; group_off = insn->offset - insn->alt_group->first_insn->offset; if (!alt_cfi[group_off]) { - alt_cfi[group_off] = &insn->cfi; + alt_cfi[group_off] = insn->cfi; } else { - if (memcmp(alt_cfi[group_off], &insn->cfi, sizeof(struct cfi_state))) { + if (cficmp(alt_cfi[group_off], insn->cfi)) { WARN_FUNC("stack layout conflict in alternatives", insn->sec, insn->offset); return -1; @@ -2335,9 +2420,14 @@ static int handle_insn_ops(struct instru static bool insn_cfi_match(struct instruction *insn, struct cfi_state *cfi2) { - struct cfi_state *cfi1 = &insn->cfi; + struct cfi_state *cfi1 = insn->cfi; int i; + if (!cfi1) { + WARN("CFI missing"); + return false; + } + if (memcmp(&cfi1->cfa, &cfi2->cfa, sizeof(cfi1->cfa))) { WARN_FUNC("stack state mismatch: cfa1=%d%+d cfa2=%d%+d", @@ -2522,7 +2612,7 @@ static int validate_branch(struct objtoo struct instruction *insn, struct insn_state state) { struct alternative *alt; - struct instruction *next_insn; + struct instruction *next_insn, *prev_insn = NULL; struct section *sec; u8 visited; int ret; @@ -2551,15 +2641,25 @@ static int validate_branch(struct objtoo if (insn->visited & visited) return 0; + } else { + nr_insns_visited++; } if (state.noinstr) state.instr += insn->instr; - if (insn->hint) - state.cfi = insn->cfi; - else - insn->cfi = state.cfi; + if (insn->hint) { + state.cfi = *insn->cfi; + } else { + /* XXX track if we actually changed state.cfi */ + + if (prev_insn && !cficmp(prev_insn->cfi, &state.cfi)) { + insn->cfi = prev_insn->cfi; + nr_cfi_reused++; + } else { + insn->cfi = cfi_hash_find_or_add(&state.cfi); + } + } insn->visited |= visited; @@ -2709,6 +2809,7 @@ static int validate_branch(struct objtoo return 1; } + prev_insn = insn; insn = next_insn; } @@ -2964,10 +3065,20 @@ int check(struct objtool_file *file) int ret, warnings = 0; arch_initial_func_cfi_state(&initial_func_cfi); + init_cfi_state(&init_cfi); + init_cfi_state(&func_cfi); + set_func_state(&func_cfi); + + if (!cfi_hash_alloc()) + goto out; + + cfi_hash_add(&init_cfi); + cfi_hash_add(&func_cfi); ret = decode_sections(file); if (ret < 0) goto out; + warnings += ret; if (list_empty(&file->insn_list)) @@ -3011,6 +3122,13 @@ int check(struct objtool_file *file) goto out; warnings += ret; + if (stats) { + printf("nr_insns_visited: %ld\n", nr_insns_visited); + printf("nr_cfi: %ld\n", nr_cfi); + printf("nr_cfi_reused: %ld\n", nr_cfi_reused); + printf("nr_cfi_cache: %ld\n", nr_cfi_cache); + } + out: /* * For now, don't fail the kernel build on fatal warnings. These --- a/tools/objtool/check.h +++ b/tools/objtool/check.h @@ -59,7 +59,7 @@ struct instruction { struct list_head alts; struct symbol *func; struct list_head stack_ops; - struct cfi_state cfi; + struct cfi_state *cfi; }; static inline bool is_static_jump(struct instruction *insn) --- a/tools/objtool/orc_gen.c +++ b/tools/objtool/orc_gen.c @@ -12,13 +12,19 @@ #include "check.h" #include "warn.h" -static int init_orc_entry(struct orc_entry *orc, struct cfi_state *cfi) +static int init_orc_entry(struct orc_entry *orc, struct cfi_state *cfi, + struct instruction *insn) { - struct instruction *insn = container_of(cfi, struct instruction, cfi); struct cfi_reg *bp = &cfi->regs[CFI_BP]; memset(orc, 0, sizeof(*orc)); + if (!cfi) { + orc->end = 0; + orc->sp_reg = ORC_REG_UNDEFINED; + return 0; + } + orc->end = cfi->end; if (cfi->cfa.base == CFI_UNDEFINED) { @@ -159,7 +165,7 @@ int orc_create(struct objtool_file *file int i; if (!alt_group) { - if (init_orc_entry(&orc, &insn->cfi)) + if (init_orc_entry(&orc, insn->cfi, insn)) return -1; if (!memcmp(&prev_orc, &orc, sizeof(orc))) continue; @@ -183,7 +189,8 @@ int orc_create(struct objtool_file *file struct cfi_state *cfi = alt_group->cfi[i]; if (!cfi) continue; - if (init_orc_entry(&orc, cfi)) + /* errors are reported on the original insn */ + if (init_orc_entry(&orc, cfi, insn)) return -1; if (!memcmp(&prev_orc, &orc, sizeof(orc))) continue; Patches currently in stable-queue which might be from peterz@xxxxxxxxxxxxx are queue-5.10/objtool-cache-instruction-relocs.patch queue-5.10/x86-sev-avoid-using-__x86_return_thunk.patch queue-5.10/objtool-add-elf_create_undef_symbol.patch queue-5.10/x86-ftrace-use-alternative-ret-encoding.patch queue-5.10/objtool-re-add-unwind_hint_-save_restore.patch queue-5.10/x86-bugs-add-retbleed-ibpb.patch queue-5.10/x86-bugs-enable-stibp-for-jmp2ret.patch queue-5.10/x86-retpoline-cleanup-some-ifdefery.patch queue-5.10/objtool-handle-__sanitize_cov-tail-calls.patch queue-5.10/x86-prepare-asm-files-for-straight-line-speculation.patch queue-5.10/kvm-vmx-flatten-__vmx_vcpu_run.patch queue-5.10/x86-kvm-vmx-make-noinstr-clean.patch queue-5.10/objtool-x86-replace-alternatives-with-.retpoline_sites.patch queue-5.10/objtool-skip-magical-retpoline-.altinstr_replacement.patch queue-5.10/x86-retbleed-add-fine-grained-kconfig-knobs.patch queue-5.10/x86-cpu-amd-add-spectral-chicken.patch queue-5.10/objtool-add-straight-line-speculation-validation.patch queue-5.10/kvm-vmx-fix-ibrs-handling-after-vmexit.patch queue-5.10/kvm-vmx-prevent-guest-rsb-poisoning-attacks-with-eibrs.patch queue-5.10/x86-vsyscall_emu-64-don-t-use-ret-in-vsyscall-emulation.patch queue-5.10/tools-arch-update-arch-x86-lib-mem-cpy-set-_64.s-copies-used-in-perf-bench-mem-memcpy.patch queue-5.10/x86-add-straight-line-speculation-mitigation.patch queue-5.10/x86-add-magic-amd-return-thunk.patch queue-5.10/x86-bugs-keep-a-per-cpu-ia32_spec_ctrl-value.patch queue-5.10/x86-alternatives-optimize-optimize_nops.patch queue-5.10/x86-objtool-create-.return_sites.patch queue-5.10/crypto-x86-poly1305-fixup-sls.patch queue-5.10/x86-alternative-handle-jcc-__x86_indirect_thunk_-reg.patch queue-5.10/x86-kvm-fix-setcc-emulation-for-return-thunks.patch queue-5.10/objtool-fix-objtool-regression-on-x32-systems.patch queue-5.10/x86-alternative-relax-text_poke_bp-constraint.patch queue-5.10/x86-retpoline-swizzle-retpoline-thunk.patch queue-5.10/objtool-rework-the-elf_rebuild_reloc_section-logic.patch queue-5.10/x86-speculation-fix-firmware-entry-spec_ctrl-handling.patch queue-5.10/x86-retpoline-remove-unused-replacement-symbols.patch queue-5.10/objtool-fix-symbol-creation.patch queue-5.10/x86-speculation-add-spectre_v2-ibrs-option-to-support-kernel-ibrs.patch queue-5.10/bpf-x86-respect-x86_feature_retpoline.patch queue-5.10/objtool-fix-type-of-reloc-addend.patch queue-5.10/objtool-x86-rewrite-retpoline-thunk-calls.patch queue-5.10/x86-undo-return-thunk-damage.patch queue-5.10/x86-prepare-inline-asm-for-straight-line-speculation.patch queue-5.10/x86-alternative-support-alternative_ternary.patch queue-5.10/kvm-emulate-fix-setcc-emulation-function-offsets-with-sls.patch queue-5.10/objtool-handle-per-arch-retpoline-naming.patch queue-5.10/x86-retpoline-create-a-retpoline-thunk-array.patch queue-5.10/x86-retpoline-simplify-retpolines.patch queue-5.10/x86-asm-fix-register-order.patch queue-5.10/x86-speculation-fill-rsb-on-vmexit-for-ibrs.patch queue-5.10/objtool-add-entry-unret-validation.patch queue-5.10/objtool-keep-track-of-retpoline-call-sites.patch queue-5.10/kvm-vmx-convert-launched-argument-to-flags.patch queue-5.10/objtool-add-elf_create_reloc-helper.patch queue-5.10/objtool-make-.altinstructions-section-entry-size-consistent.patch queue-5.10/x86-bpf-use-alternative-ret-encoding.patch queue-5.10/x86-common-stamp-out-the-stepping-madness.patch queue-5.10/x86-bugs-split-spectre_v2_select_mitigation-and-spectre_v2_user_select_mitigation.patch queue-5.10/x86-bugs-report-intel-retbleed-vulnerability.patch queue-5.10/bpf-x86-simplify-computing-label-offsets.patch queue-5.10/x86-cpufeatures-move-retpoline-flags-to-word-11.patch queue-5.10/x86-speculation-fix-spec_ctrl-write-on-smt-state-change.patch queue-5.10/x86-retpoline-use-mfunction-return.patch queue-5.10/x86-xen-rename-sys-entry-points.patch queue-5.10/objtool-only-rewrite-unconditional-retpoline-thunk-calls.patch queue-5.10/x86-bugs-optimize-spec_ctrl-msr-writes.patch queue-5.10/x86-alternative-optimize-single-byte-nops-at-an-arbitrary-position.patch queue-5.10/objtool-fix-code-relocs-vs-weak-symbols.patch queue-5.10/x86-bugs-report-amd-retbleed-vulnerability.patch queue-5.10/x86-static_call-use-alternative-ret-encoding.patch queue-5.10/x86-speculation-fix-rsb-filling-with-config_retpoline-n.patch queue-5.10/x86-asm-fixup-odd-gen-for-each-reg.h-usage.patch queue-5.10/x86-alternative-add-debug-prints-to-apply_retpolines.patch queue-5.10/objtool-extract-elf_symbol_add.patch queue-5.10/x86-use-return-thunk-in-asm-code.patch queue-5.10/objtool-remove-reloc-symbol-type-checks-in-get_alt_entry.patch queue-5.10/objtool-classify-symbols.patch queue-5.10/intel_idle-disable-ibrs-during-long-idle.patch queue-5.10/objtool-correctly-handle-retpoline-thunk-calls.patch queue-5.10/objtool-fix-.symtab_shndx-handling-for-elf_create_undef_symbol.patch queue-5.10/x86-retpoline-move-the-retpoline-thunk-declarations-to-nospec-branch.h.patch queue-5.10/objtool-support-asm-jump-tables.patch queue-5.10/x86-alternative-implement-.retpoline_sites-support.patch queue-5.10/objtool-x86-ignore-__x86_indirect_alt_-symbols.patch queue-5.10/objtool-fix-sls-validation-for-kcov-tail-call-replacement.patch queue-5.10/x86-alternative-try-inline-spectre_v2-retpoline-amd.patch queue-5.10/x86-entry-remove-skip_r11rcx.patch queue-5.10/objtool-explicitly-avoid-self-modifying-code-in-.altinstr_replacement.patch queue-5.10/x86-speculation-use-cached-host-spec_ctrl-value-for-guest-entry-exit.patch queue-5.10/x86-bugs-add-amd-retbleed-boot-parameter.patch queue-5.10/objtool-create-reloc-sections-implicitly.patch queue-5.10/x86-entry-add-kernel-ibrs-implementation.patch queue-5.10/objtool-treat-.text.__x86.-as-noinstr.patch queue-5.10/x86-lib-atomic64_386_32-rename-things.patch queue-5.10/objtool-introduce-cfi-hash.patch queue-5.10/objtool-default-ignore-int3-for-unreachable.patch queue-5.10/objtool-extract-elf_strtab_concat.patch queue-5.10/objtool-teach-get_alt_entry-about-more-relocation-types.patch queue-5.10/objtool-update-retpoline-validation.patch