Skip to content

Commit b89c07d

Browse files
committed
Merge tags 'objtool-urgent-2021-06-28' and 'objtool-core-2021-06-28' of git://git.kernel.org/pub/scm/linux/kernel/git/tip/tip
Pull objtool fix and updates from Ingo Molnar: "An ELF format fix for a section flags mismatch bug that breaks kernel tooling such as kpatch-build. The biggest change in this cycle is the new code to handle and rewrite variable sized jump labels - which results in slightly tighter code generation in hot paths, through the use of short(er) NOPs. Also a number of cleanups and fixes, and a change to the generic include/linux/compiler.h to handle a s390 GCC quirk" * tag 'objtool-urgent-2021-06-28' of git://git.kernel.org/pub/scm/linux/kernel/git/tip/tip: objtool: Don't make .altinstructions writable * tag 'objtool-core-2021-06-28' of git://git.kernel.org/pub/scm/linux/kernel/git/tip/tip: objtool: Improve reloc hash size guestimate instrumentation.h: Avoid using inline asm operand modifiers compiler.h: Avoid using inline asm operand modifiers kbuild: Fix objtool dependency for 'OBJECT_FILES_NON_STANDARD_<obj> := n' objtool: Reflow handle_jump_alt() jump_label/x86: Remove unused JUMP_LABEL_NOP_SIZE jump_label, x86: Allow short NOPs objtool: Provide stats for jump_labels objtool: Rewrite jump_label instructions objtool: Decode jump_entry::key addend jump_label, x86: Emit short JMP jump_label: Free jump_entry::key bit1 for build use jump_label, x86: Add variable length patching support jump_label, x86: Introduce jump_entry_size() jump_label, x86: Improve error when we fail expected text jump_label, x86: Factor out the __jump_table generation jump_label, x86: Strip ASM jump_label support x86, objtool: Dont exclude arch/x86/realmode/ objtool: Rewrite hashtable sizing
3 parents 6796355 + e31694e + d33b903 commit b89c07d

16 files changed

Lines changed: 268 additions & 155 deletions

File tree

arch/x86/include/asm/jump_label.h

Lines changed: 30 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@
44

55
#define HAVE_JUMP_LABEL_BATCH
66

7-
#define JUMP_LABEL_NOP_SIZE 5
8-
97
#include <asm/asm.h>
108
#include <asm/nops.h>
119

@@ -14,74 +12,57 @@
1412
#include <linux/stringify.h>
1513
#include <linux/types.h>
1614

15+
#define JUMP_TABLE_ENTRY \
16+
".pushsection __jump_table, \"aw\" \n\t" \
17+
_ASM_ALIGN "\n\t" \
18+
".long 1b - . \n\t" \
19+
".long %l[l_yes] - . \n\t" \
20+
_ASM_PTR "%c0 + %c1 - .\n\t" \
21+
".popsection \n\t"
22+
23+
#ifdef CONFIG_STACK_VALIDATION
24+
25+
static __always_inline bool arch_static_branch(struct static_key *key, bool branch)
26+
{
27+
asm_volatile_goto("1:"
28+
"jmp %l[l_yes] # objtool NOPs this \n\t"
29+
JUMP_TABLE_ENTRY
30+
: : "i" (key), "i" (2 | branch) : : l_yes);
31+
32+
return false;
33+
l_yes:
34+
return true;
35+
}
36+
37+
#else
38+
1739
static __always_inline bool arch_static_branch(struct static_key * const key, const bool branch)
1840
{
1941
asm_volatile_goto("1:"
2042
".byte " __stringify(BYTES_NOP5) "\n\t"
21-
".pushsection __jump_table, \"aw\" \n\t"
22-
_ASM_ALIGN "\n\t"
23-
".long 1b - ., %l[l_yes] - . \n\t"
24-
_ASM_PTR "%c0 + %c1 - .\n\t"
25-
".popsection \n\t"
43+
JUMP_TABLE_ENTRY
2644
: : "i" (key), "i" (branch) : : l_yes);
2745

2846
return false;
2947
l_yes:
3048
return true;
3149
}
3250

51+
#endif /* STACK_VALIDATION */
52+
3353
static __always_inline bool arch_static_branch_jump(struct static_key * const key, const bool branch)
3454
{
3555
asm_volatile_goto("1:"
36-
".byte 0xe9\n\t .long %l[l_yes] - 2f\n\t"
37-
"2:\n\t"
38-
".pushsection __jump_table, \"aw\" \n\t"
39-
_ASM_ALIGN "\n\t"
40-
".long 1b - ., %l[l_yes] - . \n\t"
41-
_ASM_PTR "%c0 + %c1 - .\n\t"
42-
".popsection \n\t"
56+
"jmp %l[l_yes]\n\t"
57+
JUMP_TABLE_ENTRY
4358
: : "i" (key), "i" (branch) : : l_yes);
4459

4560
return false;
4661
l_yes:
4762
return true;
4863
}
4964

50-
#else /* __ASSEMBLY__ */
51-
52-
.macro STATIC_JUMP_IF_TRUE target, key, def
53-
.Lstatic_jump_\@:
54-
.if \def
55-
/* Equivalent to "jmp.d32 \target" */
56-
.byte 0xe9
57-
.long \target - .Lstatic_jump_after_\@
58-
.Lstatic_jump_after_\@:
59-
.else
60-
.byte BYTES_NOP5
61-
.endif
62-
.pushsection __jump_table, "aw"
63-
_ASM_ALIGN
64-
.long .Lstatic_jump_\@ - ., \target - .
65-
_ASM_PTR \key - .
66-
.popsection
67-
.endm
68-
69-
.macro STATIC_JUMP_IF_FALSE target, key, def
70-
.Lstatic_jump_\@:
71-
.if \def
72-
.byte BYTES_NOP5
73-
.else
74-
/* Equivalent to "jmp.d32 \target" */
75-
.byte 0xe9
76-
.long \target - .Lstatic_jump_after_\@
77-
.Lstatic_jump_after_\@:
78-
.endif
79-
.pushsection __jump_table, "aw"
80-
_ASM_ALIGN
81-
.long .Lstatic_jump_\@ - ., \target - .
82-
_ASM_PTR \key + 1 - .
83-
.popsection
84-
.endm
65+
extern int arch_jump_entry_size(struct jump_entry *entry);
8566

8667
#endif /* __ASSEMBLY__ */
8768

arch/x86/kernel/jump_label.c

Lines changed: 52 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -15,50 +15,75 @@
1515
#include <asm/kprobes.h>
1616
#include <asm/alternative.h>
1717
#include <asm/text-patching.h>
18+
#include <asm/insn.h>
1819

19-
static void bug_at(const void *ip, int line)
20+
int arch_jump_entry_size(struct jump_entry *entry)
2021
{
21-
/*
22-
* The location is not an op that we were expecting.
23-
* Something went wrong. Crash the box, as something could be
24-
* corrupting the kernel.
25-
*/
26-
pr_crit("jump_label: Fatal kernel bug, unexpected op at %pS [%p] (%5ph) %d\n", ip, ip, ip, line);
27-
BUG();
22+
struct insn insn = {};
23+
24+
insn_decode_kernel(&insn, (void *)jump_entry_code(entry));
25+
BUG_ON(insn.length != 2 && insn.length != 5);
26+
27+
return insn.length;
2828
}
2929

30-
static const void *
31-
__jump_label_set_jump_code(struct jump_entry *entry, enum jump_label_type type)
30+
struct jump_label_patch {
31+
const void *code;
32+
int size;
33+
};
34+
35+
static struct jump_label_patch
36+
__jump_label_patch(struct jump_entry *entry, enum jump_label_type type)
3237
{
33-
const void *expect, *code;
38+
const void *expect, *code, *nop;
3439
const void *addr, *dest;
35-
int line;
40+
int size;
3641

3742
addr = (void *)jump_entry_code(entry);
3843
dest = (void *)jump_entry_target(entry);
3944

40-
code = text_gen_insn(JMP32_INSN_OPCODE, addr, dest);
45+
size = arch_jump_entry_size(entry);
46+
switch (size) {
47+
case JMP8_INSN_SIZE:
48+
code = text_gen_insn(JMP8_INSN_OPCODE, addr, dest);
49+
nop = x86_nops[size];
50+
break;
4151

42-
if (type == JUMP_LABEL_JMP) {
43-
expect = x86_nops[5]; line = __LINE__;
44-
} else {
45-
expect = code; line = __LINE__;
52+
case JMP32_INSN_SIZE:
53+
code = text_gen_insn(JMP32_INSN_OPCODE, addr, dest);
54+
nop = x86_nops[size];
55+
break;
56+
57+
default: BUG();
4658
}
4759

48-
if (memcmp(addr, expect, JUMP_LABEL_NOP_SIZE))
49-
bug_at(addr, line);
60+
if (type == JUMP_LABEL_JMP)
61+
expect = nop;
62+
else
63+
expect = code;
64+
65+
if (memcmp(addr, expect, size)) {
66+
/*
67+
* The location is not an op that we were expecting.
68+
* Something went wrong. Crash the box, as something could be
69+
* corrupting the kernel.
70+
*/
71+
pr_crit("jump_label: Fatal kernel bug, unexpected op at %pS [%p] (%5ph != %5ph)) size:%d type:%d\n",
72+
addr, addr, addr, expect, size, type);
73+
BUG();
74+
}
5075

5176
if (type == JUMP_LABEL_NOP)
52-
code = x86_nops[5];
77+
code = nop;
5378

54-
return code;
79+
return (struct jump_label_patch){.code = code, .size = size};
5580
}
5681

5782
static inline void __jump_label_transform(struct jump_entry *entry,
5883
enum jump_label_type type,
5984
int init)
6085
{
61-
const void *opcode = __jump_label_set_jump_code(entry, type);
86+
const struct jump_label_patch jlp = __jump_label_patch(entry, type);
6287

6388
/*
6489
* As long as only a single processor is running and the code is still
@@ -72,12 +97,11 @@ static inline void __jump_label_transform(struct jump_entry *entry,
7297
* always nop being the 'currently valid' instruction
7398
*/
7499
if (init || system_state == SYSTEM_BOOTING) {
75-
text_poke_early((void *)jump_entry_code(entry), opcode,
76-
JUMP_LABEL_NOP_SIZE);
100+
text_poke_early((void *)jump_entry_code(entry), jlp.code, jlp.size);
77101
return;
78102
}
79103

80-
text_poke_bp((void *)jump_entry_code(entry), opcode, JUMP_LABEL_NOP_SIZE, NULL);
104+
text_poke_bp((void *)jump_entry_code(entry), jlp.code, jlp.size, NULL);
81105
}
82106

83107
static void __ref jump_label_transform(struct jump_entry *entry,
@@ -98,7 +122,7 @@ void arch_jump_label_transform(struct jump_entry *entry,
98122
bool arch_jump_label_transform_queue(struct jump_entry *entry,
99123
enum jump_label_type type)
100124
{
101-
const void *opcode;
125+
struct jump_label_patch jlp;
102126

103127
if (system_state == SYSTEM_BOOTING) {
104128
/*
@@ -109,9 +133,8 @@ bool arch_jump_label_transform_queue(struct jump_entry *entry,
109133
}
110134

111135
mutex_lock(&text_mutex);
112-
opcode = __jump_label_set_jump_code(entry, type);
113-
text_poke_queue((void *)jump_entry_code(entry),
114-
opcode, JUMP_LABEL_NOP_SIZE, NULL);
136+
jlp = __jump_label_patch(entry, type);
137+
text_poke_queue((void *)jump_entry_code(entry), jlp.code, jlp.size, NULL);
115138
mutex_unlock(&text_mutex);
116139
return true;
117140
}

arch/x86/realmode/Makefile

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
# Sanitizer runtimes are unavailable and cannot be linked here.
1111
KASAN_SANITIZE := n
1212
KCSAN_SANITIZE := n
13-
OBJECT_FILES_NON_STANDARD := y
1413

1514
subdir- := rm
1615

include/linux/compiler.h

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -115,18 +115,24 @@ void ftrace_likely_update(struct ftrace_likely_data *f, int val,
115115
* The __COUNTER__ based labels are a hack to make each instance of the macros
116116
* unique, to convince GCC not to merge duplicate inline asm statements.
117117
*/
118-
#define annotate_reachable() ({ \
119-
asm volatile("%c0:\n\t" \
118+
#define __stringify_label(n) #n
119+
120+
#define __annotate_reachable(c) ({ \
121+
asm volatile(__stringify_label(c) ":\n\t" \
120122
".pushsection .discard.reachable\n\t" \
121-
".long %c0b - .\n\t" \
122-
".popsection\n\t" : : "i" (__COUNTER__)); \
123+
".long " __stringify_label(c) "b - .\n\t" \
124+
".popsection\n\t"); \
123125
})
124-
#define annotate_unreachable() ({ \
125-
asm volatile("%c0:\n\t" \
126+
#define annotate_reachable() __annotate_reachable(__COUNTER__)
127+
128+
#define __annotate_unreachable(c) ({ \
129+
asm volatile(__stringify_label(c) ":\n\t" \
126130
".pushsection .discard.unreachable\n\t" \
127-
".long %c0b - .\n\t" \
128-
".popsection\n\t" : : "i" (__COUNTER__)); \
131+
".long " __stringify_label(c) "b - .\n\t" \
132+
".popsection\n\t"); \
129133
})
134+
#define annotate_unreachable() __annotate_unreachable(__COUNTER__)
135+
130136
#define ASM_UNREACHABLE \
131137
"999:\n\t" \
132138
".pushsection .discard.unreachable\n\t" \

include/linux/instrumentation.h

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,16 @@
44

55
#if defined(CONFIG_DEBUG_ENTRY) && defined(CONFIG_STACK_VALIDATION)
66

7+
#include <linux/stringify.h>
8+
79
/* Begin/end of an instrumentation safe region */
8-
#define instrumentation_begin() ({ \
9-
asm volatile("%c0: nop\n\t" \
10+
#define __instrumentation_begin(c) ({ \
11+
asm volatile(__stringify(c) ": nop\n\t" \
1012
".pushsection .discard.instr_begin\n\t" \
11-
".long %c0b - .\n\t" \
12-
".popsection\n\t" : : "i" (__COUNTER__)); \
13+
".long " __stringify(c) "b - .\n\t" \
14+
".popsection\n\t"); \
1315
})
16+
#define instrumentation_begin() __instrumentation_begin(__COUNTER__)
1417

1518
/*
1619
* Because instrumentation_{begin,end}() can nest, objtool validation considers
@@ -43,12 +46,13 @@
4346
* To avoid this, have _end() be a NOP instruction, this ensures it will be
4447
* part of the condition block and does not escape.
4548
*/
46-
#define instrumentation_end() ({ \
47-
asm volatile("%c0: nop\n\t" \
49+
#define __instrumentation_end(c) ({ \
50+
asm volatile(__stringify(c) ": nop\n\t" \
4851
".pushsection .discard.instr_end\n\t" \
49-
".long %c0b - .\n\t" \
50-
".popsection\n\t" : : "i" (__COUNTER__)); \
52+
".long " __stringify(c) "b - .\n\t" \
53+
".popsection\n\t"); \
5154
})
55+
#define instrumentation_end() __instrumentation_end(__COUNTER__)
5256
#else
5357
# define instrumentation_begin() do { } while(0)
5458
# define instrumentation_end() do { } while(0)

include/linux/jump_label.h

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -171,9 +171,21 @@ static inline bool jump_entry_is_init(const struct jump_entry *entry)
171171
return (unsigned long)entry->key & 2UL;
172172
}
173173

174-
static inline void jump_entry_set_init(struct jump_entry *entry)
174+
static inline void jump_entry_set_init(struct jump_entry *entry, bool set)
175175
{
176-
entry->key |= 2;
176+
if (set)
177+
entry->key |= 2;
178+
else
179+
entry->key &= ~2;
180+
}
181+
182+
static inline int jump_entry_size(struct jump_entry *entry)
183+
{
184+
#ifdef JUMP_LABEL_NOP_SIZE
185+
return JUMP_LABEL_NOP_SIZE;
186+
#else
187+
return arch_jump_entry_size(entry);
188+
#endif
177189
}
178190

179191
#endif

kernel/jump_label.c

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -309,7 +309,7 @@ EXPORT_SYMBOL_GPL(jump_label_rate_limit);
309309
static int addr_conflict(struct jump_entry *entry, void *start, void *end)
310310
{
311311
if (jump_entry_code(entry) <= (unsigned long)end &&
312-
jump_entry_code(entry) + JUMP_LABEL_NOP_SIZE > (unsigned long)start)
312+
jump_entry_code(entry) + jump_entry_size(entry) > (unsigned long)start)
313313
return 1;
314314

315315
return 0;
@@ -483,13 +483,14 @@ void __init jump_label_init(void)
483483

484484
for (iter = iter_start; iter < iter_stop; iter++) {
485485
struct static_key *iterk;
486+
bool in_init;
486487

487488
/* rewrite NOPs */
488489
if (jump_label_type(iter) == JUMP_LABEL_NOP)
489490
arch_jump_label_transform_static(iter, JUMP_LABEL_NOP);
490491

491-
if (init_section_contains((void *)jump_entry_code(iter), 1))
492-
jump_entry_set_init(iter);
492+
in_init = init_section_contains((void *)jump_entry_code(iter), 1);
493+
jump_entry_set_init(iter, in_init);
493494

494495
iterk = jump_entry_key(iter);
495496
if (iterk == key)
@@ -634,9 +635,10 @@ static int jump_label_add_module(struct module *mod)
634635

635636
for (iter = iter_start; iter < iter_stop; iter++) {
636637
struct static_key *iterk;
638+
bool in_init;
637639

638-
if (within_module_init(jump_entry_code(iter), mod))
639-
jump_entry_set_init(iter);
640+
in_init = within_module_init(jump_entry_code(iter), mod);
641+
jump_entry_set_init(iter, in_init);
640642

641643
iterk = jump_entry_key(iter);
642644
if (iterk == key)

0 commit comments

Comments
 (0)