Re: [PATCH v3 2/4] x86/alternative: Support relocations in alternatives

From: Borislav Petkov
Date: Fri Feb 17 2023 - 17:21:40 EST


On Wed, Feb 08, 2023 at 06:10:52PM +0100, Peter Zijlstra wrote:
> + if (insn.opcode.bytes[0] == JMP32_INSN_OPCODE) {
> + s32 imm = insn.immediate.value;
> + imm += src - dest;
> + imm += JMP32_INSN_SIZE - JMP8_INSN_SIZE;
> + if ((imm >> 31) == (imm >> 7)) {
> + buf[i+0] = JMP8_INSN_OPCODE;
> + buf[i+1] = (s8)imm;
> + for (int j = 2; j < insn.length; j++)
> + buf[i+j] = INT3_INSN_OPCODE;

Let's get rid of that third nested loop:

diff --git a/arch/x86/kernel/alternative.c b/arch/x86/kernel/alternative.c
index e14bc15bf646..28eb1d0bc4a0 100644
--- a/arch/x86/kernel/alternative.c
+++ b/arch/x86/kernel/alternative.c
@@ -308,8 +308,8 @@ apply_relocation(u8 *buf, size_t len, u8 *dest, u8 *src, size_t src_len)
if ((imm >> 31) == (imm >> 7)) {
buf[i+0] = JMP8_INSN_OPCODE;
buf[i+1] = (s8)imm;
- for (int j = 2; j < insn.length; j++)
- buf[i+j] = INT3_INSN_OPCODE;
+
+ memset(&buf[i+2], INT3_INSN_OPCODE, insn.length - 2);
}
}
break;

--
Regards/Gruss,
Boris.

https://people.kernel.org/tglx/notes-about-netiquette