Skip to content

Commit 7e00c01

Browse files
bp3tk0vgregkh
authored andcommitted
x86/alternative: Optimize returns patching
commit d2408e043e7296017420aa5929b3bba4d5e61013 upstream. 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) <[email protected]> Acked-by: Peter Zijlstra (Intel) <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Pawan Gupta <[email protected]> Signed-off-by: Greg Kroah-Hartman <[email protected]>
1 parent c2bece0 commit 7e00c01

File tree

1 file changed

+10
-3
lines changed

1 file changed

+10
-3
lines changed

arch/x86/kernel/alternative.c

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

781+
/* Patch the custom return thunks... */
781782
if (cpu_wants_rethunk_at(addr)) {
782-
if (x86_return_thunk == __x86_return_thunk)
783-
return -1;
784-
785783
i = JMP32_INSN_SIZE;
786784
__text_gen_insn(bytes, JMP32_INSN_OPCODE, addr, x86_return_thunk, i);
787785
} else {
786+
/* ... or patch them out if not needed. */
788787
bytes[i++] = RET_INSN_OPCODE;
789788
}
790789

@@ -797,6 +796,14 @@ void __init_or_module noinline apply_returns(s32 *start, s32 *end)
797796
{
798797
s32 *s;
799798

799+
/*
800+
* Do not patch out the default return thunks if those needed are the
801+
* ones generated by the compiler.
802+
*/
803+
if (cpu_feature_enabled(X86_FEATURE_RETHUNK) &&
804+
(x86_return_thunk == __x86_return_thunk))
805+
return;
806+
800807
for (s = start; s < end; s++) {
801808
void *dest = NULL, *addr = (void *)s + *s;
802809
struct insn insn;

0 commit comments

Comments
 (0)