Skip to content

Commit d2408e0

Browse files
committed
x86/alternative: Optimize returns patching
Instead of decoding each instruction in the return sites range only to realize that that return site is a jump to the default return thunk which is needed - X86_FEATURE_RETHUNK is enabled - lift that check before the loop and get rid of that loop overhead. Add comments about what gets patched, while at it. Signed-off-by: Borislav Petkov (AMD) <bp@alien8.de> Acked-by: Peter Zijlstra (Intel) <peterz@infradead.org> Link: https://lore.kernel.org/r/20230512120952.7924-1-bp@alien8.de
1 parent b6c881b commit d2408e0

1 file changed

Lines changed: 10 additions & 3 deletions

File tree

arch/x86/kernel/alternative.c

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -693,13 +693,12 @@ static int patch_return(void *addr, struct insn *insn, u8 *bytes)
693693
{
694694
int i = 0;
695695

696+
/* Patch the custom return thunks... */
696697
if (cpu_feature_enabled(X86_FEATURE_RETHUNK)) {
697-
if (x86_return_thunk == __x86_return_thunk)
698-
return -1;
699-
700698
i = JMP32_INSN_SIZE;
701699
__text_gen_insn(bytes, JMP32_INSN_OPCODE, addr, x86_return_thunk, i);
702700
} else {
701+
/* ... or patch them out if not needed. */
703702
bytes[i++] = RET_INSN_OPCODE;
704703
}
705704

@@ -712,6 +711,14 @@ void __init_or_module noinline apply_returns(s32 *start, s32 *end)
712711
{
713712
s32 *s;
714713

714+
/*
715+
* Do not patch out the default return thunks if those needed are the
716+
* ones generated by the compiler.
717+
*/
718+
if (cpu_feature_enabled(X86_FEATURE_RETHUNK) &&
719+
(x86_return_thunk == __x86_return_thunk))
720+
return;
721+
715722
for (s = start; s < end; s++) {
716723
void *dest = NULL, *addr = (void *)s + *s;
717724
struct insn insn;

0 commit comments

Comments
 (0)