Commit | Line | Data |
---|---|---|
bf5438fc JB |
1 | #ifndef _LINUX_JUMP_LABEL_H |
2 | #define _LINUX_JUMP_LABEL_H | |
3 | ||
efb3040d PZ |
4 | /* |
5 | * Jump label support | |
6 | * | |
7 | * Copyright (C) 2009-2012 Jason Baron <jbaron@redhat.com> | |
8 | * Copyright (C) 2011-2012 Peter Zijlstra <pzijlstr@redhat.com> | |
9 | * | |
412758cb JB |
10 | * DEPRECATED API: |
11 | * | |
12 | * The use of 'struct static_key' directly, is now DEPRECATED. In addition | |
13 | * static_key_{true,false}() is also DEPRECATED. IE DO NOT use the following: | |
14 | * | |
15 | * struct static_key false = STATIC_KEY_INIT_FALSE; | |
16 | * struct static_key true = STATIC_KEY_INIT_TRUE; | |
17 | * static_key_true() | |
18 | * static_key_false() | |
19 | * | |
20 | * The updated API replacements are: | |
21 | * | |
22 | * DEFINE_STATIC_KEY_TRUE(key); | |
23 | * DEFINE_STATIC_KEY_FALSE(key); | |
24 | * static_key_likely() | |
25 | * statick_key_unlikely() | |
26 | * | |
efb3040d | 27 | * Jump labels provide an interface to generate dynamic branches using |
412758cb JB |
28 | * self-modifying code. Assuming toolchain and architecture support, if we |
29 | * define a "key" that is initially false via "DEFINE_STATIC_KEY_FALSE(key)", | |
30 | * an "if (static_branch_unlikely(&key))" statement is an unconditional branch | |
31 | * (which defaults to false - and the true block is placed out of line). | |
32 | * Similarly, we can define an initially true key via | |
33 | * "DEFINE_STATIC_KEY_TRUE(key)", and use it in the same | |
34 | * "if (static_branch_unlikely(&key))", in which case we will generate an | |
35 | * unconditional branch to the out-of-line true branch. Keys that are | |
36 | * initially true or false can be using in both static_branch_unlikely() | |
37 | * and static_branch_likely() statements. | |
efb3040d | 38 | * |
412758cb JB |
39 | * At runtime we can change the branch target by setting the key |
40 | * to true via a call to static_branch_enable(), or false using | |
41 | * static_branch_disable(). If the direction of the branch is switched by | |
42 | * these calls then we run-time modify the branch target via a | |
43 | * no-op -> jump or jump -> no-op conversion. For example, for an | |
44 | * initially false key that is used in an "if (static_branch_unlikely(&key))" | |
45 | * statement, setting the key to true requires us to patch in a jump | |
46 | * to the out-of-line of true branch. | |
efb3040d | 47 | * |
412758cb JB |
48 | * In addtion to static_branch_{enable,disable}, we can also reference count |
49 | * the key or branch direction via static_branch_{inc,dec}. Thus, | |
50 | * static_branch_inc() can be thought of as a 'make more true' and | |
51 | * static_branch_dec() as a 'make more false'. The inc()/dec() | |
52 | * interface is meant to be used exclusively from the inc()/dec() for a given | |
53 | * key. | |
54 | * | |
55 | * Since this relies on modifying code, the branch modifying functions | |
efb3040d | 56 | * must be considered absolute slow paths (machine wide synchronization etc.). |
fd3cbdc0 | 57 | * OTOH, since the affected branches are unconditional, their runtime overhead |
efb3040d PZ |
58 | * will be absolutely minimal, esp. in the default (off) case where the total |
59 | * effect is a single NOP of appropriate size. The on case will patch in a jump | |
60 | * to the out-of-line block. | |
61 | * | |
fd3cbdc0 | 62 | * When the control is directly exposed to userspace, it is prudent to delay the |
efb3040d | 63 | * decrement to avoid high frequency code modifications which can (and do) |
c5905afb IM |
64 | * cause significant performance degradation. Struct static_key_deferred and |
65 | * static_key_slow_dec_deferred() provide for this. | |
efb3040d | 66 | * |
412758cb JB |
67 | * Lacking toolchain and or architecture support, static keys fall back to a |
68 | * simple conditional branch. | |
c5905afb | 69 | * |
412758cb | 70 | * Additional babbling in: Documentation/static-keys.txt |
fd3cbdc0 | 71 | */ |
efb3040d | 72 | |
c0ccf6f9 AB |
73 | #if defined(CC_HAVE_ASM_GOTO) && defined(CONFIG_JUMP_LABEL) |
74 | # define HAVE_JUMP_LABEL | |
75 | #endif | |
76 | ||
77 | #ifndef __ASSEMBLY__ | |
78 | ||
d430d3d7 JB |
79 | #include <linux/types.h> |
80 | #include <linux/compiler.h> | |
c4b2c0c5 HFS |
81 | #include <linux/bug.h> |
82 | ||
83 | extern bool static_key_initialized; | |
84 | ||
85 | #define STATIC_KEY_CHECK_USE() WARN(!static_key_initialized, \ | |
86 | "%s used before call to jump_label_init", \ | |
87 | __func__) | |
d430d3d7 | 88 | |
c0ccf6f9 | 89 | #ifdef HAVE_JUMP_LABEL |
d430d3d7 | 90 | |
c5905afb | 91 | struct static_key { |
d430d3d7 | 92 | atomic_t enabled; |
c5905afb | 93 | /* Set lsb bit to 1 if branch is default true, 0 ot */ |
d430d3d7 JB |
94 | struct jump_entry *entries; |
95 | #ifdef CONFIG_MODULES | |
c5905afb | 96 | struct static_key_mod *next; |
d430d3d7 JB |
97 | #endif |
98 | }; | |
99 | ||
ea5e9539 MG |
100 | #else |
101 | struct static_key { | |
102 | atomic_t enabled; | |
103 | }; | |
c0ccf6f9 AB |
104 | #endif /* HAVE_JUMP_LABEL */ |
105 | #endif /* __ASSEMBLY__ */ | |
106 | ||
107 | #ifdef HAVE_JUMP_LABEL | |
108 | #include <asm/jump_label.h> | |
109 | #endif | |
110 | ||
111 | #ifndef __ASSEMBLY__ | |
bf5438fc JB |
112 | |
113 | enum jump_label_type { | |
76b235c6 PZ |
114 | JUMP_LABEL_NOP = 0, |
115 | JUMP_LABEL_JMP, | |
bf5438fc JB |
116 | }; |
117 | ||
118 | struct module; | |
119 | ||
851cf6e7 | 120 | #include <linux/atomic.h> |
ea5e9539 MG |
121 | |
122 | static inline int static_key_count(struct static_key *key) | |
123 | { | |
124 | return atomic_read(&key->enabled); | |
125 | } | |
126 | ||
bf5438fc JB |
127 | #ifdef HAVE_JUMP_LABEL |
128 | ||
a1efb01f PZ |
129 | #define JUMP_TYPE_FALSE 0UL |
130 | #define JUMP_TYPE_TRUE 1UL | |
131 | #define JUMP_TYPE_MASK 1UL | |
c5905afb IM |
132 | |
133 | static __always_inline bool static_key_false(struct static_key *key) | |
134 | { | |
11276d53 | 135 | return arch_static_branch(key, false); |
c5905afb | 136 | } |
d430d3d7 | 137 | |
c5905afb IM |
138 | static __always_inline bool static_key_true(struct static_key *key) |
139 | { | |
11276d53 | 140 | return !arch_static_branch(key, true); |
c5905afb IM |
141 | } |
142 | ||
bf5438fc JB |
143 | extern struct jump_entry __start___jump_table[]; |
144 | extern struct jump_entry __stop___jump_table[]; | |
145 | ||
97ce2c88 | 146 | extern void jump_label_init(void); |
91bad2f8 JB |
147 | extern void jump_label_lock(void); |
148 | extern void jump_label_unlock(void); | |
bf5438fc | 149 | extern void arch_jump_label_transform(struct jump_entry *entry, |
37348804 | 150 | enum jump_label_type type); |
20284aa7 JF |
151 | extern void arch_jump_label_transform_static(struct jump_entry *entry, |
152 | enum jump_label_type type); | |
4c3ef6d7 | 153 | extern int jump_label_text_reserved(void *start, void *end); |
c5905afb IM |
154 | extern void static_key_slow_inc(struct static_key *key); |
155 | extern void static_key_slow_dec(struct static_key *key); | |
d430d3d7 | 156 | extern void jump_label_apply_nops(struct module *mod); |
c5905afb | 157 | |
11276d53 | 158 | #define STATIC_KEY_INIT_TRUE \ |
f4be8433 | 159 | { .enabled = ATOMIC_INIT(1), \ |
11276d53 PZ |
160 | .entries = (void *)JUMP_TYPE_TRUE } |
161 | #define STATIC_KEY_INIT_FALSE \ | |
f4be8433 | 162 | { .enabled = ATOMIC_INIT(0), \ |
11276d53 | 163 | .entries = (void *)JUMP_TYPE_FALSE } |
bf5438fc | 164 | |
97ce2c88 | 165 | #else /* !HAVE_JUMP_LABEL */ |
bf5438fc | 166 | |
97ce2c88 JF |
167 | static __always_inline void jump_label_init(void) |
168 | { | |
c4b2c0c5 | 169 | static_key_initialized = true; |
97ce2c88 JF |
170 | } |
171 | ||
c5905afb IM |
172 | static __always_inline bool static_key_false(struct static_key *key) |
173 | { | |
ea5e9539 | 174 | if (unlikely(static_key_count(key) > 0)) |
c5905afb IM |
175 | return true; |
176 | return false; | |
177 | } | |
178 | ||
179 | static __always_inline bool static_key_true(struct static_key *key) | |
d430d3d7 | 180 | { |
ea5e9539 | 181 | if (likely(static_key_count(key) > 0)) |
d430d3d7 JB |
182 | return true; |
183 | return false; | |
184 | } | |
bf5438fc | 185 | |
c5905afb | 186 | static inline void static_key_slow_inc(struct static_key *key) |
d430d3d7 | 187 | { |
c4b2c0c5 | 188 | STATIC_KEY_CHECK_USE(); |
d430d3d7 JB |
189 | atomic_inc(&key->enabled); |
190 | } | |
bf5438fc | 191 | |
c5905afb | 192 | static inline void static_key_slow_dec(struct static_key *key) |
bf5438fc | 193 | { |
c4b2c0c5 | 194 | STATIC_KEY_CHECK_USE(); |
d430d3d7 | 195 | atomic_dec(&key->enabled); |
bf5438fc JB |
196 | } |
197 | ||
4c3ef6d7 JB |
198 | static inline int jump_label_text_reserved(void *start, void *end) |
199 | { | |
200 | return 0; | |
201 | } | |
202 | ||
91bad2f8 JB |
203 | static inline void jump_label_lock(void) {} |
204 | static inline void jump_label_unlock(void) {} | |
205 | ||
d430d3d7 JB |
206 | static inline int jump_label_apply_nops(struct module *mod) |
207 | { | |
208 | return 0; | |
209 | } | |
b2029520 | 210 | |
11276d53 PZ |
211 | #define STATIC_KEY_INIT_TRUE { .enabled = ATOMIC_INIT(1) } |
212 | #define STATIC_KEY_INIT_FALSE { .enabled = ATOMIC_INIT(0) } | |
c5905afb | 213 | |
97ce2c88 | 214 | #endif /* HAVE_JUMP_LABEL */ |
d430d3d7 | 215 | |
c5905afb IM |
216 | #define STATIC_KEY_INIT STATIC_KEY_INIT_FALSE |
217 | #define jump_label_enabled static_key_enabled | |
ac99b862 | 218 | |
8eedce99 JB |
219 | static inline bool static_key_enabled(struct static_key *key) |
220 | { | |
ea5e9539 | 221 | return static_key_count(key) > 0; |
8eedce99 JB |
222 | } |
223 | ||
e33886b3 PZ |
224 | static inline void static_key_enable(struct static_key *key) |
225 | { | |
226 | int count = static_key_count(key); | |
227 | ||
228 | WARN_ON_ONCE(count < 0 || count > 1); | |
229 | ||
230 | if (!count) | |
231 | static_key_slow_inc(key); | |
232 | } | |
233 | ||
234 | static inline void static_key_disable(struct static_key *key) | |
235 | { | |
236 | int count = static_key_count(key); | |
237 | ||
238 | WARN_ON_ONCE(count < 0 || count > 1); | |
239 | ||
240 | if (count) | |
241 | static_key_slow_dec(key); | |
242 | } | |
243 | ||
11276d53 PZ |
244 | /* -------------------------------------------------------------------------- */ |
245 | ||
246 | /* | |
247 | * Two type wrappers around static_key, such that we can use compile time | |
248 | * type differentiation to emit the right code. | |
249 | * | |
250 | * All the below code is macros in order to play type games. | |
251 | */ | |
252 | ||
253 | struct static_key_true { | |
254 | struct static_key key; | |
255 | }; | |
256 | ||
257 | struct static_key_false { | |
258 | struct static_key key; | |
259 | }; | |
260 | ||
261 | #define STATIC_KEY_TRUE_INIT (struct static_key_true) { .key = STATIC_KEY_INIT_TRUE, } | |
262 | #define STATIC_KEY_FALSE_INIT (struct static_key_false){ .key = STATIC_KEY_INIT_FALSE, } | |
263 | ||
264 | #define DEFINE_STATIC_KEY_TRUE(name) \ | |
265 | struct static_key_true name = STATIC_KEY_TRUE_INIT | |
266 | ||
267 | #define DEFINE_STATIC_KEY_FALSE(name) \ | |
268 | struct static_key_false name = STATIC_KEY_FALSE_INIT | |
269 | ||
270 | #ifdef HAVE_JUMP_LABEL | |
271 | ||
272 | /* | |
273 | * Combine the right initial value (type) with the right branch order | |
274 | * to generate the desired result. | |
275 | * | |
276 | * | |
277 | * type\branch| likely (1) | unlikely (0) | |
278 | * -----------+-----------------------+------------------ | |
279 | * | | | |
280 | * true (1) | ... | ... | |
281 | * | NOP | JMP L | |
282 | * | <br-stmts> | 1: ... | |
283 | * | L: ... | | |
284 | * | | | |
285 | * | | L: <br-stmts> | |
286 | * | | jmp 1b | |
287 | * | | | |
288 | * -----------+-----------------------+------------------ | |
289 | * | | | |
290 | * false (0) | ... | ... | |
291 | * | JMP L | NOP | |
292 | * | <br-stmts> | 1: ... | |
293 | * | L: ... | | |
294 | * | | | |
295 | * | | L: <br-stmts> | |
296 | * | | jmp 1b | |
297 | * | | | |
298 | * -----------+-----------------------+------------------ | |
299 | * | |
300 | * The initial value is encoded in the LSB of static_key::entries, | |
301 | * type: 0 = false, 1 = true. | |
302 | * | |
303 | * The branch type is encoded in the LSB of jump_entry::key, | |
304 | * branch: 0 = unlikely, 1 = likely. | |
305 | * | |
306 | * This gives the following logic table: | |
307 | * | |
308 | * enabled type branch instuction | |
309 | * -----------------------------+----------- | |
310 | * 0 0 0 | NOP | |
311 | * 0 0 1 | JMP | |
312 | * 0 1 0 | NOP | |
313 | * 0 1 1 | JMP | |
314 | * | |
315 | * 1 0 0 | JMP | |
316 | * 1 0 1 | NOP | |
317 | * 1 1 0 | JMP | |
318 | * 1 1 1 | NOP | |
319 | * | |
320 | * Which gives the following functions: | |
321 | * | |
322 | * dynamic: instruction = enabled ^ branch | |
323 | * static: instruction = type ^ branch | |
324 | * | |
325 | * See jump_label_type() / jump_label_init_type(). | |
326 | */ | |
327 | ||
328 | extern bool ____wrong_branch_error(void); | |
329 | ||
330 | #define static_branch_likely(x) \ | |
331 | ({ \ | |
332 | bool branch; \ | |
333 | if (__builtin_types_compatible_p(typeof(*x), struct static_key_true)) \ | |
334 | branch = !arch_static_branch(&(x)->key, true); \ | |
335 | else if (__builtin_types_compatible_p(typeof(*x), struct static_key_false)) \ | |
336 | branch = !arch_static_branch_jump(&(x)->key, true); \ | |
337 | else \ | |
338 | branch = ____wrong_branch_error(); \ | |
339 | branch; \ | |
340 | }) | |
341 | ||
342 | #define static_branch_unlikely(x) \ | |
343 | ({ \ | |
344 | bool branch; \ | |
345 | if (__builtin_types_compatible_p(typeof(*x), struct static_key_true)) \ | |
346 | branch = arch_static_branch_jump(&(x)->key, false); \ | |
347 | else if (__builtin_types_compatible_p(typeof(*x), struct static_key_false)) \ | |
348 | branch = arch_static_branch(&(x)->key, false); \ | |
349 | else \ | |
350 | branch = ____wrong_branch_error(); \ | |
351 | branch; \ | |
352 | }) | |
353 | ||
354 | #else /* !HAVE_JUMP_LABEL */ | |
355 | ||
356 | #define static_branch_likely(x) likely(static_key_enabled(&(x)->key)) | |
357 | #define static_branch_unlikely(x) unlikely(static_key_enabled(&(x)->key)) | |
358 | ||
359 | #endif /* HAVE_JUMP_LABEL */ | |
360 | ||
361 | /* | |
362 | * Advanced usage; refcount, branch is enabled when: count != 0 | |
363 | */ | |
364 | ||
365 | #define static_branch_inc(x) static_key_slow_inc(&(x)->key) | |
366 | #define static_branch_dec(x) static_key_slow_dec(&(x)->key) | |
367 | ||
368 | /* | |
369 | * Normal usage; boolean enable/disable. | |
370 | */ | |
371 | ||
372 | #define static_branch_enable(x) static_key_enable(&(x)->key) | |
373 | #define static_branch_disable(x) static_key_disable(&(x)->key) | |
374 | ||
97ce2c88 | 375 | #endif /* _LINUX_JUMP_LABEL_H */ |
c0ccf6f9 AB |
376 | |
377 | #endif /* __ASSEMBLY__ */ |