x86/alternatives: Do NOPs optimization on a temporary buffer
Borislav Petkov
maintainerv2 cover x86/alternatives: Do NOPs optimization on a temporary buffer
From: "Borislav Petkov (AMD)" <[email protected]>
Hi,
here's v2 of the set to do NOPs optimizations on a temporary buffer after the last-minute conflict and resulting breakage before the last merge window which made me retract those.
This v2 has been extensively tested on 6.9-rc1.
Thx.
Changelog:
=========
v0:
here's a small set which sprang out from my reacting to the fact that NOPs optimization in the alternatives code needs to happen on a temporary buffer like the other alternative operations - not in-place and cause all kinds of fun.
The result is this, which makes the alternatives code simpler and it is a net win, size-wise:
1 file changed, 50 insertions(+), 72 deletions(-)
Constructive feedback is always welcome!
Borislav Petkov (AMD) (4):
x86/alternatives: Use a temporary buffer when optimizing NOPs x86/alternatives: Get rid of __optimize_nops() x86/alternatives: Optimize optimize_nops() x86/alternatives: Sort local vars in apply_alternatives()
arch/x86/include/asm/text-patching.h | 2 +- arch/x86/kernel/alternative.c | 133 ++++++++++++--------------- arch/x86/kernel/callthunks.c | 9 +- 3 files changed, 62 insertions(+), 82 deletions(-)
Signature · 1 line
2.43.0
Borislav Petkov
maintainerPATCH v2 1/4 x86/alternatives: Use a temporary buffer when optimizing NOPs
From: "Borislav Petkov (AMD)" <[email protected]>
Instead of optimizing NOPs in-place, use a temporary buffer like the usual alternatives patching flow does. This obviates the need to grab locks when patching, see
6778977590da ("x86/alternatives: Disable interrupts and sync when optimizing NOPs in place")
While at it, add nomenclature definitions clarifying and simplifying the naming of function-local variables in the alternatives code.
--- arch/x86/include/asm/text-patching.h | 2 +- arch/x86/kernel/alternative.c | 84 +++++++++++++++------------- arch/x86/kernel/callthunks.c | 9 +-- 3 files changed, 49 insertions(+), 46 deletions(-)
arch/x86/include/asm/text-patching.h +1 -1
@@ -15,7 +15,7 @@extern void text_poke_early (void *addr,const void *opcode,size_t len); -extern void apply_relocation (u8 *buf,size_t len,u8 *dest,u8 *src,size_t src_len); +extern void apply_relocation (u8 *buf,const u8 *const instr,size_t instrlen,u8 *repl,size_t repl_len);/* * Clear and restore the kernel write-protection flag on the local CPU.
arch/x86/kernel/alternative.c +45 -39
208 lines not on this page · raw message
arch/x86/kernel/callthunks.c +3 -6
@@ -185,8 +185,7 @@ static void *patch_dest(void *dest, bool direct)u8 *pad = dest - tsize;memcpy (insn_buff, skl_call_thunk_template, tsize); -apply_relocation (insn_buff, tsize, pad, - skl_call_thunk_template, tsize); +apply_relocation (insn_buff, pad, tsize, skl_call_thunk_template, tsize);/* Already patched? */ if (!bcmp (pad, insn_buff, tsize))
@@ -308,8 +307,7 @@ static bool is_callthunk(void *addr) pad = (void *)(dest - tmpl_size);memcpy (insn_buff, skl_call_thunk_template, tmpl_size); -apply_relocation (insn_buff, tmpl_size, pad, - skl_call_thunk_template, tmpl_size); +apply_relocation (insn_buff, pad, tmpl_size, skl_call_thunk_template, tmpl_size);return !bcmp (pad, insn_buff, tmpl_size); }
@@ -327,8 +325,7 @@ int x86_call_depth_emit_accounting(u8 **pprog, void *func)return 0 ;memcpy (insn_buff, skl_call_thunk_template, tmpl_size); -apply_relocation (insn_buff, tmpl_size, *pprog, - skl_call_thunk_template, tmpl_size); +apply_relocation (insn_buff, *pprog, tmpl_size, skl_call_thunk_template, tmpl_size);memcpy (*pprog, insn_buff, tmpl_size); *pprog += tmpl_size;
Signature · 1 line
2.43.0
Borislav Petkov
maintainerPATCH v2 2/4 x86/alternatives: Get rid of __optimize_nops()
In reply to Borislav PetkovFrom: "Borislav Petkov (AMD)" <[email protected]>
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 patching.
A lot simpler code.
--- arch/x86/kernel/alternative.c | 59 ++++++++++------------------------- 1 file changed, 16 insertions(+), 43 deletions(-)
arch/x86/kernel/alternative.c +16 -43
@@ -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 -__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) +static void 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); + } } }
@@ -339,10 +311,8 @@ bool need_reloc(unsigned long offset, u8 *src, size_t src_len)return (target < src || target > src + src_len); } -void apply_relocation (u8 *buf,const u8 *const instr,size_t instrlen,u8 *repl,size_t repl_len) +static void __apply_relocation (u8 *buf,const u8 *const instr,size_t instrlen,u8 *repl,size_t repl_len) { -int prev, target =0 ; -for (int next, i =0 ; i < instrlen; i = next) {struct insn insn;
@@ -351,9 +321,6 @@ void apply_relocation(u8 *buf, const u8 * const instr, size_t instrlen, u8 *repl next = i + insn.length; -if (__optimize_nops (instr, buf, instrlen, &insn, &next, &prev, &target)) -continue ; -switch (insn.opcode.bytes[0 ]) {case 0x0f : if (insn.opcode.bytes[1] < 0x80 ||
@@ -398,6 +365,12 @@ void apply_relocation(u8 *buf, const u8 * const instr, size_t instrlen, u8 *repl } } +void apply_relocation (u8 *buf,const u8 *const instr,size_t instrlen,u8 *repl,size_t repl_len) +{ +__apply_relocation (buf, instr, instrlen, repl, repl_len); +optimize_nops (instr, buf, repl_len); +} +/* Low-level backend functions usable from alternative code replacements. */ DEFINE_ASM_FUNC(nop_func, "", .entry.text);EXPORT_SYMBOL_GPL (nop_func);
Signature · 1 line
2.43.0
Borislav Petkov
maintainerPATCH v2 3/4 x86/alternatives: Optimize optimize_nops()
In reply to Borislav PetkovFrom: "Borislav Petkov (AMD)" <[email protected]>
Return early if NOPs have already been optimized.
--- arch/x86/kernel/alternative.c | 4 ++++ 1 file changed, 4 insertions(+)
arch/x86/kernel/alternative.c +4 -0
@@ -233,6 +233,10 @@ static void noinline optimize_nops(const u8 * const instr, u8 *buf, size_t len)if (insn_is_nop (&insn)) {int nop = i; +/* Has the NOP already been optimized? */ +if (i + insn.length == len) +return ; + next =skip_nops (buf, next, len);add_nop (buf + nop, next - nop);
Signature · 1 line
2.43.0
Borislav Petkov
maintainerPATCH v2 4/4 x86/alternatives: Sort local vars in apply_alternatives()
In reply to Borislav PetkovFrom: "Borislav Petkov (AMD)" <[email protected]>
In a reverse x-mas tree.
No functional changes.
--- arch/x86/kernel/alternative.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
arch/x86/kernel/alternative.c +2 -2
@@ -445,9 +445,9 @@ static int alt_replace_call(u8 *instr, u8 *insn_buff, struct alt_instr *a) void __init_or_module noinline apply_alternatives(struct alt_instr *start,struct alt_instr *end) { -struct alt_instr *a; -u8 *instr, *replacement;u8 insn_buff[MAX_PATCH_LEN ]; +u8 *instr, *replacement; +struct alt_instr *a;DPRINTK (ALT ,"alt table %px, -> %px" , start, end);
Signature · 1 line
2.43.0