Commit 9754bf04 authored by Peter Zijlstra's avatar Peter Zijlstra Committed by sanglipeng1
Browse files

x86/returnthunk: Allow different return thunks

stable inclusion
from stable-v5.10.211
commit e8e9d1f6cf0218847a1bf3dc4aef8d8ed34443ae
category: bugfix
bugzilla: https://gitee.com/openeuler/kernel/issues/IAF2J4

Reference: https://git.kernel.org/pub/scm/linux/kernel/git/stable/linux.git/commit/?id=e8e9d1f6cf0218847a1bf3dc4aef8d8ed34443ae



--------------------------------

Upstream commit: 770ae1b7

In preparation for call depth tracking on Intel SKL CPUs, make it possible
to patch in a SKL specific return thunk.

Signed-off-by: default avatarPeter Zijlstra (Intel) <peterz@infradead.org>
Signed-off-by: default avatarThomas Gleixner <tglx@linutronix.de>
Signed-off-by: default avatarPeter Zijlstra (Intel) <peterz@infradead.org>
Link: https://lore.kernel.org/r/20220915111147.680469665@infradead.org


Signed-off-by: default avatarBorislav Petkov (AMD) <bp@alien8.de>
Signed-off-by: default avatarGreg Kroah-Hartman <gregkh@linuxfoundation.org>
Signed-off-by: default avatarsanglipeng1 <sanglipeng1@jd.com>
parent 6fc078d3
Loading
Loading
Loading
Loading
+6 −0
Original line number Diff line number Diff line
@@ -207,6 +207,12 @@ extern void srso_alias_untrain_ret(void);
extern void entry_untrain_ret(void);
extern void entry_ibpb(void);

#ifdef CONFIG_CALL_THUNKS
extern void (*x86_return_thunk)(void);
#else
#define x86_return_thunk	(&__x86_return_thunk)
#endif

#ifdef CONFIG_RETPOLINE

typedef u8 retpoline_thunk_t[RETPOLINE_THUNK_SIZE];
+13 −4
Original line number Diff line number Diff line
@@ -520,6 +520,11 @@ void __init_or_module noinline apply_retpolines(s32 *start, s32 *end)
}

#ifdef CONFIG_RETHUNK

#ifdef CONFIG_CALL_THUNKS
void (*x86_return_thunk)(void) __ro_after_init = &__x86_return_thunk;
#endif

/*
 * Rewrite the compiler generated return thunk tail-calls.
 *
@@ -535,14 +540,18 @@ static int patch_return(void *addr, struct insn *insn, u8 *bytes)
{
	int i = 0;

	if (cpu_feature_enabled(X86_FEATURE_RETHUNK))
	if (cpu_feature_enabled(X86_FEATURE_RETHUNK)) {
		if (x86_return_thunk == __x86_return_thunk)
			return -1;

		i = JMP32_INSN_SIZE;
		__text_gen_insn(bytes, JMP32_INSN_OPCODE, addr, x86_return_thunk, i);
	} else {
		bytes[i++] = RET_INSN_OPCODE;
	}

	for (; i < insn->length;)
		bytes[i++] = INT3_INSN_OPCODE;

	return i;
}

+2 −0
Original line number Diff line number Diff line
@@ -61,7 +61,9 @@ EXPORT_SYMBOL_GPL(x86_pred_cmd);

static DEFINE_MUTEX(spec_ctrl_mutex);

#ifdef CONFIG_CALL_THUNKS
void (*x86_return_thunk)(void) __ro_after_init = &__x86_return_thunk;
#endif

/* Update SPEC_CTRL MSR and its cached copy unconditionally */
static void update_spec_ctrl(u64 val)
+1 −1
Original line number Diff line number Diff line
@@ -368,7 +368,7 @@ create_trampoline(struct ftrace_ops *ops, unsigned int *tramp_size)

	ip = trampoline + size;
	if (cpu_feature_enabled(X86_FEATURE_RETHUNK))
		__text_gen_insn(ip, JMP32_INSN_OPCODE, ip, &__x86_return_thunk, JMP32_INSN_SIZE);
		__text_gen_insn(ip, JMP32_INSN_OPCODE, ip, x86_return_thunk, JMP32_INSN_SIZE);
	else
		memcpy(ip, retq, sizeof(retq));

+1 −1
Original line number Diff line number Diff line
@@ -52,7 +52,7 @@ static void __ref __static_call_transform(void *insn, enum insn_type type,

	case RET:
		if (cpu_feature_enabled(X86_FEATURE_RETHUNK))
			code = text_gen_insn(JMP32_INSN_OPCODE, insn, &__x86_return_thunk);
			code = text_gen_insn(JMP32_INSN_OPCODE, insn, x86_return_thunk);
		else
			code = &retinsn;
		break;
Loading