[PATCH 2/2] objtool: Optimize/fix retpoline alternative generation

From: Peter Zijlstra
Date: Thu Oct 07 2021 - 17:27:25 EST


When re-running objtool it will generate alterantives for all
retpoline hunks, even if they are already present.

Discard the retpoline alternatives later so we can mark the
instructions as already having alternatives and subsequently skip
generating them. Use ->ignore_alts for this.

Signed-off-by: Peter Zijlstra (Intel) <peterz@xxxxxxxxxxxxx>
---
tools/objtool/arch/x86/decode.c | 3 +++
tools/objtool/check.c | 8 ++++++++
tools/objtool/special.c | 8 --------
3 files changed, 11 insertions(+), 8 deletions(-)

--- a/tools/objtool/arch/x86/decode.c
+++ b/tools/objtool/arch/x86/decode.c
@@ -806,6 +806,9 @@ int arch_rewrite_retpolines(struct objto
if (!strcmp(insn->sec->name, ".text.__x86.indirect_thunk"))
continue;

+ if (insn->ignore_alts)
+ continue;
+
reloc = insn->reloc;

sprintf(name, "__x86_indirect_alt_%s_%s",
--- a/tools/objtool/check.c
+++ b/tools/objtool/check.c
@@ -1468,6 +1468,14 @@ static int add_special_section_alts(stru
ret = -1;
goto out;
}
+ /*
+ * Skip (but mark) the retpoline alternatives so that we
+ * don't generate them again.
+ */
+ if (new_insn->func && arch_is_retpoline(new_insn->func)) {
+ orig_insn->ignore_alts = true;
+ continue;
+ }
}

if (special_alt->group) {
--- a/tools/objtool/special.c
+++ b/tools/objtool/special.c
@@ -109,14 +109,6 @@ static int get_alt_entry(struct elf *elf
return -1;
}

- /*
- * Skip retpoline .altinstr_replacement... we already rewrite the
- * instructions for retpolines anyway, see arch_is_retpoline()
- * usage in add_{call,jump}_destinations().
- */
- if (arch_is_retpoline(new_reloc->sym))
- return 1;
-
reloc_to_sec_off(new_reloc, &alt->new_sec, &alt->new_off);

/* _ASM_EXTABLE_EX hack */