mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Borislav Petkov <bp@alien8.de>
To: X86 ML <x86@kernel.org>
Cc: Paul Gortmaker <paul.gortmaker@windriver.com>,
	LKML <linux-kernel@vger.kernel.org>
Subject: [PATCH 2/4] x86/alternatives: Get rid of __optimize_nops()
Date: Tue, 30 Jan 2024 11:59:39 +0100	[thread overview]
Message-ID: <20240130105941.19707-3-bp@alien8.de> (raw)
In-Reply-To: <20240130105941.19707-1-bp@alien8.de>

From: "Borislav Petkov (AMD)" <bp@alien8.de>

There's no need to carve out bits of the NOP optimization functionality
and look at JMP opcodes - simply do one more NOPs optimization pass
at the end of the patching.

A lot simpler code.

Signed-off-by: Borislav Petkov (AMD) <bp@alien8.de>
---
 arch/x86/kernel/alternative.c | 52 +++++++----------------------------
 1 file changed, 10 insertions(+), 42 deletions(-)

diff --git a/arch/x86/kernel/alternative.c b/arch/x86/kernel/alternative.c
index d633eb59f2b6..2dd1c7fe0949 100644
--- a/arch/x86/kernel/alternative.c
+++ b/arch/x86/kernel/alternative.c
@@ -216,47 +216,12 @@ static int skip_nops(u8 *buf, int offset, int len)
 	return offset;
 }
 
-/*
- * Optimize a sequence of NOPs, possibly preceded by an unconditional jump
- * to the end of the NOP sequence into a single NOP.
- */
-static bool __init_or_module
-__optimize_nops(const u8 * const instr, u8 *buf, size_t len, struct insn *insn, int *next, int *prev, int *target)
-{
-	int i = *next - insn->length;
-
-	switch (insn->opcode.bytes[0]) {
-	case JMP8_INSN_OPCODE:
-	case JMP32_INSN_OPCODE:
-		*prev = i;
-		*target = *next + insn->immediate.value;
-		return false;
-	}
-
-	if (insn_is_nop(insn)) {
-		int nop = i;
-
-		*next = skip_nops(buf, *next, len);
-		if (*target && *next == *target)
-			nop = *prev;
-
-		add_nop(buf + nop, *next - nop);
-		DUMP_BYTES(ALT, buf, len, "%px: [%d:%d) optimized NOPs: ", instr, nop, *next);
-		return true;
-	}
-
-	*target = 0;
-	return false;
-}
-
 /*
  * "noinline" to cause control flow change and thus invalidate I$ and
  * cause refetch after modification.
  */
 static void __init_or_module noinline optimize_nops(const u8 * const instr, u8 *buf, size_t len)
 {
-	int prev, target = 0;
-
 	for (int next, i = 0; i < len; i = next) {
 		struct insn insn;
 
@@ -265,7 +230,14 @@ static void __init_or_module noinline optimize_nops(const u8 * const instr, u8 *
 
 		next = i + insn.length;
 
-		__optimize_nops(instr, buf, len, &insn, &next, &prev, &target);
+		if (insn_is_nop(&insn)) {
+			int nop = i;
+
+			next = skip_nops(buf, next, len);
+
+			add_nop(buf + nop, next - nop);
+			DUMP_BYTES(ALT, buf, len, "%px: [%d:%d) optimized NOPs: ", instr, nop, next);
+		}
 	}
 }
 
@@ -342,8 +314,6 @@ bool need_reloc(unsigned long offset, u8 *src, size_t src_len)
 static void __init_or_module noinline
 apply_relocation(const u8 * const instr, u8 *buf, size_t len, u8 *src, size_t src_len)
 {
-	int prev, target = 0;
-
 	for (int next, i = 0; i < len; i = next) {
 		struct insn insn;
 
@@ -352,9 +322,6 @@ apply_relocation(const u8 * const instr, u8 *buf, size_t len, u8 *src, size_t sr
 
 		next = i + insn.length;
 
-		if (__optimize_nops(instr, buf, len, &insn, &next, &prev, &target))
-			continue;
-
 		switch (insn.opcode.bytes[0]) {
 		case 0x0f:
 			if (insn.opcode.bytes[1] < 0x80 ||
@@ -533,7 +500,8 @@ void __init_or_module noinline apply_alternatives(struct alt_instr *start,
 		for (; insn_buff_sz < a->instrlen; insn_buff_sz++)
 			insn_buff[insn_buff_sz] = 0x90;
 
-		apply_relocation(instr, insn_buff, a->instrlen, replacement, a->replacementlen);
+		apply_relocation(instr, insn_buff, a->instrlen, replacement, insn_buff_sz);
+		optimize_nops(instr, insn_buff, insn_buff_sz);
 
 		DUMP_BYTES(ALT, instr, a->instrlen, "%px:   old_insn: ", instr);
 		DUMP_BYTES(ALT, replacement, a->replacementlen, "%px:   rpl_insn: ", replacement);
-- 
2.43.0


  parent reply	other threads:[~2024-01-30 11:00 UTC|newest]

Thread overview: 15+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-01-30 10:59 [PATCH 0/4] x86/alternatives: Do NOPs optimization on a temporary buffer Borislav Petkov
2024-01-30 10:59 ` [PATCH 1/4] x86/alternatives: Use a temporary buffer when optimizing NOPs Borislav Petkov
2024-02-13 15:36   ` [tip: x86/alternatives] " tip-bot2 for Borislav Petkov (AMD)
2024-04-09 17:11   ` tip-bot2 for Borislav Petkov (AMD)
2024-01-30 10:59 ` Borislav Petkov [this message]
2024-02-13 15:35   ` [tip: x86/alternatives] x86/alternatives: Get rid of __optimize_nops() tip-bot2 for Borislav Petkov (AMD)
2024-04-09 17:11   ` tip-bot2 for Borislav Petkov (AMD)
2024-01-30 10:59 ` [PATCH 3/4] x86/alternatives: Optimize optimize_nops() Borislav Petkov
2024-02-13 15:35   ` [tip: x86/alternatives] " tip-bot2 for Borislav Petkov (AMD)
2024-04-09 17:11   ` tip-bot2 for Borislav Petkov (AMD)
2024-01-30 10:59 ` [PATCH 4/4] x86/alternatives: Sort local vars in apply_alternatives() Borislav Petkov
2024-02-13 15:35   ` [tip: x86/alternatives] " tip-bot2 for Borislav Petkov (AMD)
2024-04-09 17:11   ` tip-bot2 for Borislav Petkov (AMD)
2024-01-31 16:17 ` [PATCH 0/4] x86/alternatives: Do NOPs optimization on a temporary buffer Paul Gortmaker
2024-01-31 16:25   ` Borislav Petkov

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=20240130105941.19707-3-bp@alien8.de \
    --to=bp@alien8.de \
    --cc=linux-kernel@vger.kernel.org \
    --cc=paul.gortmaker@windriver.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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

Powered by JetHome