Skip to content

Commit 23c1ad5

Browse files
Peter ZijlstraIngo Molnar
authored andcommitted
x86/alternatives: Optimize optimize_nops()
Currently, optimize_nops() scans to see if the alternative starts with NOPs. However, the emit pattern is: 141: \oldinstr 142: .skip (len-(142b-141b)), 0x90 That is, when 'oldinstr' is short, the tail is padded with NOPs. This case never gets optimized. Rewrite optimize_nops() to replace any trailing string of NOPs inside the alternative to larger NOPs. Also run it irrespective of patching, replacing NOPs in both the original and replaced code. A direct consequence is that 'padlen' becomes superfluous, so remove it. [ bp: - Adjust commit message - remove a stale comment about needing to pad - add a comment in optimize_nops() - exit early if the NOP verif. loop catches a mismatch - function should not not add NOPs in that case - fix the "optimized NOPs" offsets output ] Signed-off-by: Peter Zijlstra (Intel) <[email protected]> Signed-off-by: Borislav Petkov <[email protected]> Signed-off-by: Ingo Molnar <[email protected]> Link: https://lkml.kernel.org/r/[email protected]
1 parent b1f480b commit 23c1ad5

File tree

3 files changed

+37
-31
lines changed

3 files changed

+37
-31
lines changed

arch/x86/include/asm/alternative.h

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,6 @@ struct alt_instr {
6565
u16 cpuid; /* cpuid bit set for replacement */
6666
u8 instrlen; /* length of original instruction */
6767
u8 replacementlen; /* length of new instruction */
68-
u8 padlen; /* length of build-time padding */
6968
} __packed;
7069

7170
/*
@@ -104,7 +103,6 @@ static inline int alternatives_text_reserved(void *start, void *end)
104103

105104
#define alt_end_marker "663"
106105
#define alt_slen "662b-661b"
107-
#define alt_pad_len alt_end_marker"b-662b"
108106
#define alt_total_slen alt_end_marker"b-661b"
109107
#define alt_rlen(num) e_replacement(num)"f-"b_replacement(num)"f"
110108

@@ -151,8 +149,7 @@ static inline int alternatives_text_reserved(void *start, void *end)
151149
" .long " b_replacement(num)"f - .\n" /* new instruction */ \
152150
" .word " __stringify(feature) "\n" /* feature bit */ \
153151
" .byte " alt_total_slen "\n" /* source len */ \
154-
" .byte " alt_rlen(num) "\n" /* replacement len */ \
155-
" .byte " alt_pad_len "\n" /* pad len */
152+
" .byte " alt_rlen(num) "\n" /* replacement len */
156153

157154
#define ALTINSTR_REPLACEMENT(newinstr, num) /* replacement */ \
158155
"# ALT: replacement " #num "\n" \
@@ -224,9 +221,6 @@ static inline int alternatives_text_reserved(void *start, void *end)
224221
* Peculiarities:
225222
* No memory clobber here.
226223
* Argument numbers start with 1.
227-
* Best is to use constraints that are fixed size (like (%1) ... "r")
228-
* If you use variable sized constraints like "m" or "g" in the
229-
* replacement make sure to pad to the worst case length.
230224
* Leaving an unused argument 0 to keep API compatibility.
231225
*/
232226
#define alternative_input(oldinstr, newinstr, feature, input...) \
@@ -315,13 +309,12 @@ static inline int alternatives_text_reserved(void *start, void *end)
315309
* enough information for the alternatives patching code to patch an
316310
* instruction. See apply_alternatives().
317311
*/
318-
.macro altinstruction_entry orig alt feature orig_len alt_len pad_len
312+
.macro altinstruction_entry orig alt feature orig_len alt_len
319313
.long \orig - .
320314
.long \alt - .
321315
.word \feature
322316
.byte \orig_len
323317
.byte \alt_len
324-
.byte \pad_len
325318
.endm
326319

327320
/*
@@ -338,7 +331,7 @@ static inline int alternatives_text_reserved(void *start, void *end)
338331
142:
339332

340333
.pushsection .altinstructions,"a"
341-
altinstruction_entry 140b,143f,\feature,142b-140b,144f-143f,142b-141b
334+
altinstruction_entry 140b,143f,\feature,142b-140b,144f-143f
342335
.popsection
343336

344337
.pushsection .altinstr_replacement,"ax"
@@ -375,8 +368,8 @@ static inline int alternatives_text_reserved(void *start, void *end)
375368
142:
376369

377370
.pushsection .altinstructions,"a"
378-
altinstruction_entry 140b,143f,\feature1,142b-140b,144f-143f,142b-141b
379-
altinstruction_entry 140b,144f,\feature2,142b-140b,145f-144f,142b-141b
371+
altinstruction_entry 140b,143f,\feature1,142b-140b,144f-143f
372+
altinstruction_entry 140b,144f,\feature2,142b-140b,145f-144f
380373
.popsection
381374

382375
.pushsection .altinstr_replacement,"ax"

arch/x86/kernel/alternative.c

Lines changed: 31 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -189,19 +189,35 @@ recompute_jump(struct alt_instr *a, u8 *orig_insn, u8 *repl_insn, u8 *insn_buff)
189189
static void __init_or_module noinline optimize_nops(struct alt_instr *a, u8 *instr)
190190
{
191191
unsigned long flags;
192-
int i;
192+
struct insn insn;
193+
int nop, i = 0;
194+
195+
/*
196+
* Jump over the non-NOP insns, the remaining bytes must be single-byte
197+
* NOPs, optimize them.
198+
*/
199+
for (;;) {
200+
if (insn_decode_kernel(&insn, &instr[i]))
201+
return;
202+
203+
if (insn.length == 1 && insn.opcode.bytes[0] == 0x90)
204+
break;
205+
206+
if ((i += insn.length) >= a->instrlen)
207+
return;
208+
}
193209

194-
for (i = 0; i < a->padlen; i++) {
195-
if (instr[i] != 0x90)
210+
for (nop = i; i < a->instrlen; i++) {
211+
if (WARN_ONCE(instr[i] != 0x90, "Not a NOP at 0x%px\n", &instr[i]))
196212
return;
197213
}
198214

199215
local_irq_save(flags);
200-
add_nops(instr + (a->instrlen - a->padlen), a->padlen);
216+
add_nops(instr + nop, i - nop);
201217
local_irq_restore(flags);
202218

203219
DUMP_BYTES(instr, a->instrlen, "%px: [%d:%d) optimized NOPs: ",
204-
instr, a->instrlen - a->padlen, a->padlen);
220+
instr, nop, a->instrlen);
205221
}
206222

207223
/*
@@ -247,19 +263,15 @@ void __init_or_module noinline apply_alternatives(struct alt_instr *start,
247263
* - feature not present but ALTINSTR_FLAG_INV is set to mean,
248264
* patch if feature is *NOT* present.
249265
*/
250-
if (!boot_cpu_has(feature) == !(a->cpuid & ALTINSTR_FLAG_INV)) {
251-
if (a->padlen > 1)
252-
optimize_nops(a, instr);
253-
254-
continue;
255-
}
266+
if (!boot_cpu_has(feature) == !(a->cpuid & ALTINSTR_FLAG_INV))
267+
goto next;
256268

257-
DPRINTK("feat: %s%d*32+%d, old: (%pS (%px) len: %d), repl: (%px, len: %d), pad: %d",
269+
DPRINTK("feat: %s%d*32+%d, old: (%pS (%px) len: %d), repl: (%px, len: %d)",
258270
(a->cpuid & ALTINSTR_FLAG_INV) ? "!" : "",
259271
feature >> 5,
260272
feature & 0x1f,
261273
instr, instr, a->instrlen,
262-
replacement, a->replacementlen, a->padlen);
274+
replacement, a->replacementlen);
263275

264276
DUMP_BYTES(instr, a->instrlen, "%px: old_insn: ", instr);
265277
DUMP_BYTES(replacement, a->replacementlen, "%px: rpl_insn: ", replacement);
@@ -283,14 +295,15 @@ void __init_or_module noinline apply_alternatives(struct alt_instr *start,
283295
if (a->replacementlen && is_jmp(replacement[0]))
284296
recompute_jump(a, instr, replacement, insn_buff);
285297

286-
if (a->instrlen > a->replacementlen) {
287-
add_nops(insn_buff + a->replacementlen,
288-
a->instrlen - a->replacementlen);
289-
insn_buff_sz += a->instrlen - a->replacementlen;
290-
}
298+
for (; insn_buff_sz < a->instrlen; insn_buff_sz++)
299+
insn_buff[insn_buff_sz] = 0x90;
300+
291301
DUMP_BYTES(insn_buff, insn_buff_sz, "%px: final_insn: ", instr);
292302

293303
text_poke_early(instr, insn_buff, insn_buff_sz);
304+
305+
next:
306+
optimize_nops(a, instr);
294307
}
295308
}
296309

tools/objtool/arch/x86/include/arch/special.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
#define JUMP_ORIG_OFFSET 0
1111
#define JUMP_NEW_OFFSET 4
1212

13-
#define ALT_ENTRY_SIZE 13
13+
#define ALT_ENTRY_SIZE 12
1414
#define ALT_ORIG_OFFSET 0
1515
#define ALT_NEW_OFFSET 4
1616
#define ALT_FEATURE_OFFSET 8

0 commit comments

Comments
 (0)