All of lore.kernel.org
 help / color / mirror / Atom feed
From: Peter Zijlstra <peterz@infradead.org>
To: x86@kernel.org, jpoimboe@redhat.com, andrew.cooper3@citrix.com
Cc: linux-kernel@vger.kernel.org, peterz@infradead.org,
	alexei.starovoitov@gmail.com, ndesaulniers@google.com,
	bpf@vger.kernel.org
Subject: [PATCH v3 10/16] x86/alternative: Implement .retpoline_sites support
Date: Tue, 26 Oct 2021 14:01:42 +0200	[thread overview]
Message-ID: <20211026120310.232495794@infradead.org> (raw)
In-Reply-To: 20211026120132.613201817@infradead.org

Rewrite retpoline thunk call sites to be indirect calls for
spectre_v2=off. This ensures spectre_v2=off is as near to a
RETPOLINE=n build as possible.

This is the replacement for objtool writing alternative entries to
ensure the same and achieves feature-parity with the previous
approach.

One noteworthy feature is that it relies on the thunks to be in
machine order to compute the register index.

Specifically, this does not yet address the Jcc __x86_indirect_thunk_*
calls generated by clang, a future patch will add this.

Signed-off-by: Peter Zijlstra (Intel) <peterz@infradead.org>
---
 arch/um/kernel/um_arch.c           |    4 +
 arch/x86/include/asm/alternative.h |    1 
 arch/x86/kernel/alternative.c      |  141 +++++++++++++++++++++++++++++++++++--
 arch/x86/kernel/module.c           |    9 ++
 4 files changed, 150 insertions(+), 5 deletions(-)

--- a/arch/um/kernel/um_arch.c
+++ b/arch/um/kernel/um_arch.c
@@ -421,6 +421,10 @@ void __init check_bugs(void)
 	os_check_bugs();
 }
 
+void apply_retpolines(s32 *start, s32 *end)
+{
+}
+
 void apply_alternatives(struct alt_instr *start, struct alt_instr *end)
 {
 }
--- a/arch/x86/include/asm/alternative.h
+++ b/arch/x86/include/asm/alternative.h
@@ -75,6 +75,7 @@ extern int alternatives_patched;
 
 extern void alternative_instructions(void);
 extern void apply_alternatives(struct alt_instr *start, struct alt_instr *end);
+extern void apply_retpolines(s32 *start, s32 *end);
 
 struct module;
 
--- a/arch/x86/kernel/alternative.c
+++ b/arch/x86/kernel/alternative.c
@@ -29,6 +29,7 @@
 #include <asm/io.h>
 #include <asm/fixmap.h>
 #include <asm/paravirt.h>
+#include <asm/asm-prototypes.h>
 
 int __read_mostly alternatives_patched;
 
@@ -113,6 +114,7 @@ static void __init_or_module add_nops(vo
 	}
 }
 
+extern s32 __retpoline_sites[], __retpoline_sites_end[];
 extern struct alt_instr __alt_instructions[], __alt_instructions_end[];
 extern s32 __smp_locks[], __smp_locks_end[];
 void text_poke_early(void *addr, const void *opcode, size_t len);
@@ -221,7 +223,7 @@ static __always_inline int optimize_nops
  * "noinline" to cause control flow change and thus invalidate I$ and
  * cause refetch after modification.
  */
-static void __init_or_module noinline optimize_nops(struct alt_instr *a, u8 *instr)
+static void __init_or_module noinline optimize_nops(u8 *instr, size_t len)
 {
 	struct insn insn;
 	int i = 0;
@@ -239,11 +241,11 @@ static void __init_or_module noinline op
 		 * optimized.
 		 */
 		if (insn.length == 1 && insn.opcode.bytes[0] == 0x90)
-			i += optimize_nops_range(instr, a->instrlen, i);
+			i += optimize_nops_range(instr, len, i);
 		else
 			i += insn.length;
 
-		if (i >= a->instrlen)
+		if (i >= len)
 			return;
 	}
 }
@@ -331,10 +333,135 @@ void __init_or_module noinline apply_alt
 		text_poke_early(instr, insn_buff, insn_buff_sz);
 
 next:
-		optimize_nops(a, instr);
+		optimize_nops(instr, a->instrlen);
 	}
 }
 
+#if defined(CONFIG_RETPOLINE) && defined(CONFIG_STACK_VALIDATION)
+
+/*
+ * CALL/JMP *%\reg
+ */
+static int emit_indirect(int op, int reg, u8 *bytes)
+{
+	int i = 0;
+	u8 modrm;
+
+	switch (op) {
+	case CALL_INSN_OPCODE:
+		modrm = 0x10; /* Reg = 2; CALL r/m */
+		break;
+
+	case JMP32_INSN_OPCODE:
+		modrm = 0x20; /* Reg = 4; JMP r/m */
+		break;
+
+	default:
+		WARN_ON_ONCE(1);
+		return -1;
+	}
+
+	if (reg >= 8) {
+		bytes[i++] = 0x41; /* REX.B prefix */
+		reg -= 8;
+	}
+
+	modrm |= 0xc0; /* Mod = 3 */
+	modrm += reg;
+
+	bytes[i++] = 0xff; /* opcode */
+	bytes[i++] = modrm;
+
+	return i;
+}
+
+/*
+ * Rewrite the compiler generated retpoline thunk calls.
+ *
+ * For spectre_v2=off (!X86_FEATURE_RETPOLINE), rewrite them into immediate
+ * indirect instructions, avoiding the extra indirection.
+ *
+ * For example, convert:
+ *
+ *   CALL __x86_indirect_thunk_\reg
+ *
+ * into:
+ *
+ *   CALL *%\reg
+ *
+ */
+static int patch_retpoline(void *addr, struct insn *insn, u8 *bytes)
+{
+	retpoline_thunk_t *target;
+	int reg, i = 0;
+
+	target = addr + insn->length + insn->immediate.value;
+	reg = target - __x86_indirect_thunk_array;
+
+	if (WARN_ON_ONCE(reg & ~0xf))
+		return -1;
+
+	/* If anyone ever does: CALL/JMP *%rsp, we're in deep trouble. */
+	BUG_ON(reg == 4);
+
+	if (cpu_feature_enabled(X86_FEATURE_RETPOLINE))
+		return -1;
+
+	i = emit_indirect(insn->opcode.bytes[0], reg, bytes);
+	if (i < 0)
+		return i;
+
+	for (; i < insn->length;)
+		bytes[i++] = BYTES_NOP1;
+
+	return i;
+}
+
+/*
+ * Generated by 'objtool --retpoline'.
+ */
+void __init_or_module noinline apply_retpolines(s32 *start, s32 *end)
+{
+	s32 *s;
+
+	for (s = start; s < end; s++) {
+		void *addr = (void *)s + *s;
+		struct insn insn;
+		int len, ret;
+		u8 bytes[16];
+		u8 op1, op2;
+
+		ret = insn_decode_kernel(&insn, addr);
+		if (WARN_ON_ONCE(ret < 0))
+			continue;
+
+		op1 = insn.opcode.bytes[0];
+		op2 = insn.opcode.bytes[1];
+
+		switch (op1) {
+		case CALL_INSN_OPCODE:
+		case JMP32_INSN_OPCODE:
+			break;
+
+		default:
+			WARN_ON_ONCE(1);
+			continue;
+		}
+
+		len = patch_retpoline(addr, &insn, bytes);
+		if (len == insn.length) {
+			optimize_nops(bytes, len);
+			text_poke_early(addr, bytes, len);
+		}
+	}
+}
+
+#else /* !RETPOLINES || !CONFIG_STACK_VALIDATION */
+
+void __init_or_module noinline apply_retpolines(s32 *start, s32 *end) { }
+
+#endif /* CONFIG_RETPOLINE && CONFIG_STACK_VALIDATION */
+
 #ifdef CONFIG_SMP
 static void alternatives_smp_lock(const s32 *start, const s32 *end,
 				  u8 *text, u8 *text_end)
@@ -643,6 +770,12 @@ void __init alternative_instructions(voi
 	apply_paravirt(__parainstructions, __parainstructions_end);
 
 	/*
+	 * Rewrite the retpolines, must be done before alternatives since
+	 * those can rewrite the retpoline thunks.
+	 */
+	apply_retpolines(__retpoline_sites, __retpoline_sites_end);
+
+	/*
 	 * Then patch alternatives, such that those paravirt calls that are in
 	 * alternatives can be overwritten by their immediate fragments.
 	 */
--- a/arch/x86/kernel/module.c
+++ b/arch/x86/kernel/module.c
@@ -251,7 +251,8 @@ int module_finalize(const Elf_Ehdr *hdr,
 		    struct module *me)
 {
 	const Elf_Shdr *s, *text = NULL, *alt = NULL, *locks = NULL,
-		*para = NULL, *orc = NULL, *orc_ip = NULL;
+		*para = NULL, *orc = NULL, *orc_ip = NULL,
+		*retpolines = NULL;
 	char *secstrings = (void *)hdr + sechdrs[hdr->e_shstrndx].sh_offset;
 
 	for (s = sechdrs; s < sechdrs + hdr->e_shnum; s++) {
@@ -267,8 +268,14 @@ int module_finalize(const Elf_Ehdr *hdr,
 			orc = s;
 		if (!strcmp(".orc_unwind_ip", secstrings + s->sh_name))
 			orc_ip = s;
+		if (!strcmp(".retpoline_sites", secstrings + s->sh_name))
+			retpolines = s;
 	}
 
+	if (retpolines) {
+		void *rseg = (void *)retpolines->sh_addr;
+		apply_retpolines(rseg, rseg + retpolines->sh_size);
+	}
 	if (alt) {
 		/* patch .altinstructions */
 		void *aseg = (void *)alt->sh_addr;



  parent reply	other threads:[~2021-10-26 12:05 UTC|newest]

Thread overview: 48+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-10-26 12:01 [PATCH v3 00/16] x86: Rewrite the retpoline rewrite logic Peter Zijlstra
2021-10-26 12:01 ` [PATCH v3 01/16] objtool: Classify symbols Peter Zijlstra
2021-10-28  3:58   ` Josh Poimboeuf
2021-10-29  8:03   ` [tip: objtool/core] " tip-bot2 for Peter Zijlstra
2021-10-26 12:01 ` [PATCH v3 02/16] objtool: Explicitly avoid self modifying code in .altinstr_replacement Peter Zijlstra
2021-10-28  4:01   ` Josh Poimboeuf
2021-10-29  8:03   ` [tip: objtool/core] " tip-bot2 for Peter Zijlstra
2021-10-26 12:01 ` [PATCH v3 03/16] objtool: Shrink struct instruction Peter Zijlstra
2021-10-29  8:03   ` [tip: objtool/core] " tip-bot2 for Peter Zijlstra
2021-10-26 12:01 ` [PATCH v3 04/16] objtool,x86: Replace alternatives with .retpoline_sites Peter Zijlstra
2021-10-29  8:03   ` [tip: objtool/core] " tip-bot2 for Peter Zijlstra
2021-10-26 12:01 ` [PATCH v3 05/16] x86/retpoline: Remove unused replacement symbols Peter Zijlstra
2021-10-29  8:03   ` [tip: objtool/core] " tip-bot2 for Peter Zijlstra
2021-10-26 12:01 ` [PATCH v3 06/16] x86/asm: Fix register order Peter Zijlstra
2021-10-29  8:03   ` [tip: objtool/core] " tip-bot2 for Peter Zijlstra
2021-10-26 12:01 ` [PATCH v3 07/16] x86/asm: Fixup odd GEN-for-each-reg.h usage Peter Zijlstra
2021-10-29  8:03   ` [tip: objtool/core] " tip-bot2 for Peter Zijlstra
2021-10-26 12:01 ` [PATCH v3 08/16] x86/retpoline: Move the retpoline thunk declarations to nospec-branch.h Peter Zijlstra
2021-10-29  8:03   ` [tip: objtool/core] " tip-bot2 for Peter Zijlstra
2021-10-26 12:01 ` [PATCH v3 09/16] x86/retpoline: Create a retpoline thunk array Peter Zijlstra
2021-10-29  8:03   ` [tip: objtool/core] " tip-bot2 for Peter Zijlstra
2021-10-26 12:01 ` Peter Zijlstra [this message]
2021-10-27 17:38   ` [PATCH v3 10/16] x86/alternative: Implement .retpoline_sites support Borislav Petkov
2021-10-27 19:10     ` Peter Zijlstra
2021-10-29  8:02   ` [tip: objtool/core] " tip-bot2 for Peter Zijlstra
2021-10-26 12:01 ` [PATCH v3 11/16] x86/alternative: Handle Jcc __x86_indirect_thunk_\reg Peter Zijlstra
2021-10-28  9:29   ` Borislav Petkov
2021-10-29  8:02   ` [tip: objtool/core] " tip-bot2 for Peter Zijlstra
2021-10-26 12:01 ` [PATCH v3 12/16] x86/alternative: Try inline spectre_v2=retpoline,amd Peter Zijlstra
2021-10-29  8:02   ` [tip: objtool/core] " tip-bot2 for Peter Zijlstra
2021-10-26 12:01 ` [PATCH v3 13/16] x86/alternative: Add debug prints to apply_retpolines() Peter Zijlstra
2021-10-29  8:02   ` [tip: objtool/core] " tip-bot2 for Peter Zijlstra
2021-10-26 12:01 ` [PATCH v3 14/16] x86,bugs: Unconditionally allow spectre_v2=retpoline,amd Peter Zijlstra
2021-10-29  8:02   ` [tip: objtool/core] " tip-bot2 for Peter Zijlstra
2021-10-26 12:01 ` [PATCH v3 15/16] bpf, x86: Simplify computing label offsets Peter Zijlstra
2021-10-29  8:02   ` [tip: objtool/core] bpf,x86: " tip-bot2 for Peter Zijlstra
2021-10-26 12:01 ` [PATCH v3 16/16] bpf,x86: Respect X86_FEATURE_RETPOLINE* Peter Zijlstra
2021-10-29  8:02   ` [tip: objtool/core] " tip-bot2 for Peter Zijlstra
2021-10-26 18:26 ` [PATCH v3 00/16] x86: Rewrite the retpoline rewrite logic Alexei Starovoitov
2021-10-26 18:45   ` Peter Zijlstra
2021-10-26 20:00     ` Alexei Starovoitov
2021-10-26 21:05       ` Peter Zijlstra
2021-10-26 21:05         ` Alexei Starovoitov
2021-10-27  9:00           ` Peter Zijlstra
2021-10-27 17:32             ` Alexei Starovoitov
2021-10-28  5:17 ` Josh Poimboeuf
2021-10-28 17:43 ` Borislav Petkov
2021-10-29 12:43 ` Miroslav Benes

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20211026120310.232495794@infradead.org \
    --to=peterz@infradead.org \
    --cc=alexei.starovoitov@gmail.com \
    --cc=andrew.cooper3@citrix.com \
    --cc=bpf@vger.kernel.org \
    --cc=jpoimboe@redhat.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=ndesaulniers@google.com \
    --cc=x86@kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.