Merge remote-tracking branch 'battery/for-next'
[deliverable/linux.git] / include / linux / jump_label.h
CommitLineData
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>
90eec103 8 * Copyright (C) 2011-2012 Red Hat, Inc., Peter Zijlstra
efb3040d 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);
ef0da55a
CM
24 * DEFINE_STATIC_KEY_ARRAY_TRUE(keys, count);
25 * DEFINE_STATIC_KEY_ARRAY_FALSE(keys, count);
1975dbc2
JC
26 * static_branch_likely()
27 * static_branch_unlikely()
412758cb 28 *
efb3040d 29 * Jump labels provide an interface to generate dynamic branches using
412758cb
JB
30 * self-modifying code. Assuming toolchain and architecture support, if we
31 * define a "key" that is initially false via "DEFINE_STATIC_KEY_FALSE(key)",
32 * an "if (static_branch_unlikely(&key))" statement is an unconditional branch
33 * (which defaults to false - and the true block is placed out of line).
34 * Similarly, we can define an initially true key via
35 * "DEFINE_STATIC_KEY_TRUE(key)", and use it in the same
36 * "if (static_branch_unlikely(&key))", in which case we will generate an
37 * unconditional branch to the out-of-line true branch. Keys that are
38 * initially true or false can be using in both static_branch_unlikely()
39 * and static_branch_likely() statements.
efb3040d 40 *
412758cb
JB
41 * At runtime we can change the branch target by setting the key
42 * to true via a call to static_branch_enable(), or false using
43 * static_branch_disable(). If the direction of the branch is switched by
44 * these calls then we run-time modify the branch target via a
45 * no-op -> jump or jump -> no-op conversion. For example, for an
46 * initially false key that is used in an "if (static_branch_unlikely(&key))"
47 * statement, setting the key to true requires us to patch in a jump
48 * to the out-of-line of true branch.
efb3040d 49 *
1975dbc2 50 * In addition to static_branch_{enable,disable}, we can also reference count
412758cb
JB
51 * the key or branch direction via static_branch_{inc,dec}. Thus,
52 * static_branch_inc() can be thought of as a 'make more true' and
1975dbc2 53 * static_branch_dec() as a 'make more false'.
412758cb
JB
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
82extern bool static_key_initialized;
83
84#define STATIC_KEY_CHECK_USE() WARN(!static_key_initialized, \
85 "%s used before call to jump_label_init", \
86 __func__)
d430d3d7 87
c0ccf6f9 88#ifdef HAVE_JUMP_LABEL
d430d3d7 89
c5905afb 90struct static_key {
d430d3d7 91 atomic_t enabled;
c5905afb 92/* Set lsb bit to 1 if branch is default true, 0 ot */
d430d3d7
JB
93 struct jump_entry *entries;
94#ifdef CONFIG_MODULES
c5905afb 95 struct static_key_mod *next;
d430d3d7
JB
96#endif
97};
98
ea5e9539
MG
99#else
100struct static_key {
101 atomic_t enabled;
102};
c0ccf6f9
AB
103#endif /* HAVE_JUMP_LABEL */
104#endif /* __ASSEMBLY__ */
105
106#ifdef HAVE_JUMP_LABEL
107#include <asm/jump_label.h>
108#endif
109
110#ifndef __ASSEMBLY__
bf5438fc
JB
111
112enum jump_label_type {
76b235c6
PZ
113 JUMP_LABEL_NOP = 0,
114 JUMP_LABEL_JMP,
bf5438fc
JB
115};
116
117struct module;
118
4c5ea0a9
PB
119#ifdef HAVE_JUMP_LABEL
120
a1efb01f
PZ
121#define JUMP_TYPE_FALSE 0UL
122#define JUMP_TYPE_TRUE 1UL
123#define JUMP_TYPE_MASK 1UL
c5905afb
IM
124
125static __always_inline bool static_key_false(struct static_key *key)
126{
11276d53 127 return arch_static_branch(key, false);
c5905afb 128}
d430d3d7 129
c5905afb
IM
130static __always_inline bool static_key_true(struct static_key *key)
131{
11276d53 132 return !arch_static_branch(key, true);
c5905afb
IM
133}
134
bf5438fc
JB
135extern struct jump_entry __start___jump_table[];
136extern struct jump_entry __stop___jump_table[];
137
97ce2c88 138extern void jump_label_init(void);
91bad2f8
JB
139extern void jump_label_lock(void);
140extern void jump_label_unlock(void);
bf5438fc 141extern void arch_jump_label_transform(struct jump_entry *entry,
37348804 142 enum jump_label_type type);
20284aa7
JF
143extern void arch_jump_label_transform_static(struct jump_entry *entry,
144 enum jump_label_type type);
4c3ef6d7 145extern int jump_label_text_reserved(void *start, void *end);
c5905afb
IM
146extern void static_key_slow_inc(struct static_key *key);
147extern void static_key_slow_dec(struct static_key *key);
d430d3d7 148extern void jump_label_apply_nops(struct module *mod);
1f69bf9c
JB
149extern int static_key_count(struct static_key *key);
150extern void static_key_enable(struct static_key *key);
151extern void static_key_disable(struct static_key *key);
c5905afb 152
1f69bf9c
JB
153/*
154 * We should be using ATOMIC_INIT() for initializing .enabled, but
155 * the inclusion of atomic.h is problematic for inclusion of jump_label.h
156 * in 'low-level' headers. Thus, we are initializing .enabled with a
157 * raw value, but have added a BUILD_BUG_ON() to catch any issues in
158 * jump_label_init() see: kernel/jump_label.c.
159 */
11276d53 160#define STATIC_KEY_INIT_TRUE \
1f69bf9c 161 { .enabled = { 1 }, \
11276d53
PZ
162 .entries = (void *)JUMP_TYPE_TRUE }
163#define STATIC_KEY_INIT_FALSE \
1f69bf9c 164 { .enabled = { 0 }, \
11276d53 165 .entries = (void *)JUMP_TYPE_FALSE }
bf5438fc 166
97ce2c88 167#else /* !HAVE_JUMP_LABEL */
bf5438fc 168
1f69bf9c
JB
169#include <linux/atomic.h>
170#include <linux/bug.h>
171
4c5ea0a9
PB
172static inline int static_key_count(struct static_key *key)
173{
174 return atomic_read(&key->enabled);
175}
176
97ce2c88
JF
177static __always_inline void jump_label_init(void)
178{
c4b2c0c5 179 static_key_initialized = true;
97ce2c88
JF
180}
181
c5905afb
IM
182static __always_inline bool static_key_false(struct static_key *key)
183{
ea5e9539 184 if (unlikely(static_key_count(key) > 0))
c5905afb
IM
185 return true;
186 return false;
187}
188
189static __always_inline bool static_key_true(struct static_key *key)
d430d3d7 190{
ea5e9539 191 if (likely(static_key_count(key) > 0))
d430d3d7
JB
192 return true;
193 return false;
194}
bf5438fc 195
c5905afb 196static inline void static_key_slow_inc(struct static_key *key)
d430d3d7 197{
c4b2c0c5 198 STATIC_KEY_CHECK_USE();
d430d3d7
JB
199 atomic_inc(&key->enabled);
200}
bf5438fc 201
c5905afb 202static inline void static_key_slow_dec(struct static_key *key)
bf5438fc 203{
c4b2c0c5 204 STATIC_KEY_CHECK_USE();
d430d3d7 205 atomic_dec(&key->enabled);
bf5438fc
JB
206}
207
4c3ef6d7
JB
208static inline int jump_label_text_reserved(void *start, void *end)
209{
210 return 0;
211}
212
91bad2f8
JB
213static inline void jump_label_lock(void) {}
214static inline void jump_label_unlock(void) {}
215
d430d3d7
JB
216static inline int jump_label_apply_nops(struct module *mod)
217{
218 return 0;
219}
b2029520 220
e33886b3
PZ
221static inline void static_key_enable(struct static_key *key)
222{
223 int count = static_key_count(key);
224
225 WARN_ON_ONCE(count < 0 || count > 1);
226
227 if (!count)
228 static_key_slow_inc(key);
229}
230
231static inline void static_key_disable(struct static_key *key)
232{
233 int count = static_key_count(key);
234
235 WARN_ON_ONCE(count < 0 || count > 1);
236
237 if (count)
238 static_key_slow_dec(key);
239}
240
1f69bf9c
JB
241#define STATIC_KEY_INIT_TRUE { .enabled = ATOMIC_INIT(1) }
242#define STATIC_KEY_INIT_FALSE { .enabled = ATOMIC_INIT(0) }
243
244#endif /* HAVE_JUMP_LABEL */
245
246#define STATIC_KEY_INIT STATIC_KEY_INIT_FALSE
247#define jump_label_enabled static_key_enabled
248
11276d53
PZ
249/* -------------------------------------------------------------------------- */
250
251/*
252 * Two type wrappers around static_key, such that we can use compile time
253 * type differentiation to emit the right code.
254 *
255 * All the below code is macros in order to play type games.
256 */
257
258struct static_key_true {
259 struct static_key key;
260};
261
262struct static_key_false {
263 struct static_key key;
264};
265
266#define STATIC_KEY_TRUE_INIT (struct static_key_true) { .key = STATIC_KEY_INIT_TRUE, }
267#define STATIC_KEY_FALSE_INIT (struct static_key_false){ .key = STATIC_KEY_INIT_FALSE, }
268
269#define DEFINE_STATIC_KEY_TRUE(name) \
270 struct static_key_true name = STATIC_KEY_TRUE_INIT
271
272#define DEFINE_STATIC_KEY_FALSE(name) \
273 struct static_key_false name = STATIC_KEY_FALSE_INIT
274
ef0da55a
CM
275#define DEFINE_STATIC_KEY_ARRAY_TRUE(name, count) \
276 struct static_key_true name[count] = { \
277 [0 ... (count) - 1] = STATIC_KEY_TRUE_INIT, \
278 }
279
280#define DEFINE_STATIC_KEY_ARRAY_FALSE(name, count) \
281 struct static_key_false name[count] = { \
282 [0 ... (count) - 1] = STATIC_KEY_FALSE_INIT, \
283 }
284
fa128fd7
TH
285extern bool ____wrong_branch_error(void);
286
287#define static_key_enabled(x) \
288({ \
289 if (!__builtin_types_compatible_p(typeof(*x), struct static_key) && \
290 !__builtin_types_compatible_p(typeof(*x), struct static_key_true) &&\
291 !__builtin_types_compatible_p(typeof(*x), struct static_key_false)) \
292 ____wrong_branch_error(); \
293 static_key_count((struct static_key *)x) > 0; \
294})
295
11276d53
PZ
296#ifdef HAVE_JUMP_LABEL
297
298/*
299 * Combine the right initial value (type) with the right branch order
300 * to generate the desired result.
301 *
302 *
303 * type\branch| likely (1) | unlikely (0)
304 * -----------+-----------------------+------------------
305 * | |
306 * true (1) | ... | ...
307 * | NOP | JMP L
308 * | <br-stmts> | 1: ...
309 * | L: ... |
310 * | |
311 * | | L: <br-stmts>
312 * | | jmp 1b
313 * | |
314 * -----------+-----------------------+------------------
315 * | |
316 * false (0) | ... | ...
317 * | JMP L | NOP
318 * | <br-stmts> | 1: ...
319 * | L: ... |
320 * | |
321 * | | L: <br-stmts>
322 * | | jmp 1b
323 * | |
324 * -----------+-----------------------+------------------
325 *
326 * The initial value is encoded in the LSB of static_key::entries,
327 * type: 0 = false, 1 = true.
328 *
329 * The branch type is encoded in the LSB of jump_entry::key,
330 * branch: 0 = unlikely, 1 = likely.
331 *
332 * This gives the following logic table:
333 *
334 * enabled type branch instuction
335 * -----------------------------+-----------
336 * 0 0 0 | NOP
337 * 0 0 1 | JMP
338 * 0 1 0 | NOP
339 * 0 1 1 | JMP
340 *
341 * 1 0 0 | JMP
342 * 1 0 1 | NOP
343 * 1 1 0 | JMP
344 * 1 1 1 | NOP
345 *
346 * Which gives the following functions:
347 *
348 * dynamic: instruction = enabled ^ branch
349 * static: instruction = type ^ branch
350 *
351 * See jump_label_type() / jump_label_init_type().
352 */
353
11276d53
PZ
354#define static_branch_likely(x) \
355({ \
356 bool branch; \
357 if (__builtin_types_compatible_p(typeof(*x), struct static_key_true)) \
358 branch = !arch_static_branch(&(x)->key, true); \
359 else if (__builtin_types_compatible_p(typeof(*x), struct static_key_false)) \
360 branch = !arch_static_branch_jump(&(x)->key, true); \
361 else \
362 branch = ____wrong_branch_error(); \
363 branch; \
364})
365
366#define static_branch_unlikely(x) \
367({ \
368 bool branch; \
369 if (__builtin_types_compatible_p(typeof(*x), struct static_key_true)) \
370 branch = arch_static_branch_jump(&(x)->key, false); \
371 else if (__builtin_types_compatible_p(typeof(*x), struct static_key_false)) \
372 branch = arch_static_branch(&(x)->key, false); \
373 else \
374 branch = ____wrong_branch_error(); \
375 branch; \
376})
377
378#else /* !HAVE_JUMP_LABEL */
379
380#define static_branch_likely(x) likely(static_key_enabled(&(x)->key))
381#define static_branch_unlikely(x) unlikely(static_key_enabled(&(x)->key))
382
383#endif /* HAVE_JUMP_LABEL */
384
385/*
386 * Advanced usage; refcount, branch is enabled when: count != 0
387 */
388
389#define static_branch_inc(x) static_key_slow_inc(&(x)->key)
390#define static_branch_dec(x) static_key_slow_dec(&(x)->key)
391
392/*
393 * Normal usage; boolean enable/disable.
394 */
395
396#define static_branch_enable(x) static_key_enable(&(x)->key)
397#define static_branch_disable(x) static_key_disable(&(x)->key)
398
97ce2c88 399#endif /* _LINUX_JUMP_LABEL_H */
c0ccf6f9
AB
400
401#endif /* __ASSEMBLY__ */
This page took 0.534017 seconds and 5 git commands to generate.