]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - include/linux/jump_label.h
UBUNTU: SAUCE: LSM stacking: procfs: add smack subdir to attrs
[mirror_ubuntu-artful-kernel.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;
3821fd35 92/*
b17ef2ed
SRV
93 * Note:
94 * To make anonymous unions work with old compilers, the static
95 * initialization of them requires brackets. This creates a dependency
96 * on the order of the struct with the initializers. If any fields
97 * are added, STATIC_KEY_INIT_TRUE and STATIC_KEY_INIT_FALSE may need
98 * to be modified.
99 *
3821fd35
JB
100 * bit 0 => 1 if key is initially true
101 * 0 if initially false
102 * bit 1 => 1 if points to struct static_key_mod
103 * 0 if points to struct jump_entry
104 */
105 union {
106 unsigned long type;
107 struct jump_entry *entries;
108 struct static_key_mod *next;
109 };
d430d3d7
JB
110};
111
ea5e9539
MG
112#else
113struct static_key {
114 atomic_t enabled;
115};
c0ccf6f9
AB
116#endif /* HAVE_JUMP_LABEL */
117#endif /* __ASSEMBLY__ */
118
119#ifdef HAVE_JUMP_LABEL
120#include <asm/jump_label.h>
121#endif
122
123#ifndef __ASSEMBLY__
bf5438fc
JB
124
125enum jump_label_type {
76b235c6
PZ
126 JUMP_LABEL_NOP = 0,
127 JUMP_LABEL_JMP,
bf5438fc
JB
128};
129
130struct module;
131
4c5ea0a9
PB
132#ifdef HAVE_JUMP_LABEL
133
3821fd35
JB
134#define JUMP_TYPE_FALSE 0UL
135#define JUMP_TYPE_TRUE 1UL
136#define JUMP_TYPE_LINKED 2UL
137#define JUMP_TYPE_MASK 3UL
c5905afb
IM
138
139static __always_inline bool static_key_false(struct static_key *key)
140{
11276d53 141 return arch_static_branch(key, false);
c5905afb 142}
d430d3d7 143
c5905afb
IM
144static __always_inline bool static_key_true(struct static_key *key)
145{
11276d53 146 return !arch_static_branch(key, true);
c5905afb
IM
147}
148
bf5438fc
JB
149extern struct jump_entry __start___jump_table[];
150extern struct jump_entry __stop___jump_table[];
151
97ce2c88 152extern void jump_label_init(void);
91bad2f8
JB
153extern void jump_label_lock(void);
154extern void jump_label_unlock(void);
bf5438fc 155extern void arch_jump_label_transform(struct jump_entry *entry,
37348804 156 enum jump_label_type type);
20284aa7
JF
157extern void arch_jump_label_transform_static(struct jump_entry *entry,
158 enum jump_label_type type);
4c3ef6d7 159extern int jump_label_text_reserved(void *start, void *end);
c5905afb
IM
160extern void static_key_slow_inc(struct static_key *key);
161extern void static_key_slow_dec(struct static_key *key);
d430d3d7 162extern void jump_label_apply_nops(struct module *mod);
1f69bf9c
JB
163extern int static_key_count(struct static_key *key);
164extern void static_key_enable(struct static_key *key);
165extern void static_key_disable(struct static_key *key);
c5905afb 166
1f69bf9c
JB
167/*
168 * We should be using ATOMIC_INIT() for initializing .enabled, but
169 * the inclusion of atomic.h is problematic for inclusion of jump_label.h
170 * in 'low-level' headers. Thus, we are initializing .enabled with a
171 * raw value, but have added a BUILD_BUG_ON() to catch any issues in
172 * jump_label_init() see: kernel/jump_label.c.
173 */
11276d53 174#define STATIC_KEY_INIT_TRUE \
1f69bf9c 175 { .enabled = { 1 }, \
cd8d860d 176 { .entries = (void *)JUMP_TYPE_TRUE } }
11276d53 177#define STATIC_KEY_INIT_FALSE \
1f69bf9c 178 { .enabled = { 0 }, \
cd8d860d 179 { .entries = (void *)JUMP_TYPE_FALSE } }
bf5438fc 180
97ce2c88 181#else /* !HAVE_JUMP_LABEL */
bf5438fc 182
1f69bf9c
JB
183#include <linux/atomic.h>
184#include <linux/bug.h>
185
4c5ea0a9
PB
186static inline int static_key_count(struct static_key *key)
187{
188 return atomic_read(&key->enabled);
189}
190
97ce2c88
JF
191static __always_inline void jump_label_init(void)
192{
c4b2c0c5 193 static_key_initialized = true;
97ce2c88
JF
194}
195
c5905afb
IM
196static __always_inline bool static_key_false(struct static_key *key)
197{
ea5e9539 198 if (unlikely(static_key_count(key) > 0))
c5905afb
IM
199 return true;
200 return false;
201}
202
203static __always_inline bool static_key_true(struct static_key *key)
d430d3d7 204{
ea5e9539 205 if (likely(static_key_count(key) > 0))
d430d3d7
JB
206 return true;
207 return false;
208}
bf5438fc 209
c5905afb 210static inline void static_key_slow_inc(struct static_key *key)
d430d3d7 211{
c4b2c0c5 212 STATIC_KEY_CHECK_USE();
d430d3d7
JB
213 atomic_inc(&key->enabled);
214}
bf5438fc 215
c5905afb 216static inline void static_key_slow_dec(struct static_key *key)
bf5438fc 217{
c4b2c0c5 218 STATIC_KEY_CHECK_USE();
d430d3d7 219 atomic_dec(&key->enabled);
bf5438fc
JB
220}
221
4c3ef6d7
JB
222static inline int jump_label_text_reserved(void *start, void *end)
223{
224 return 0;
225}
226
91bad2f8
JB
227static inline void jump_label_lock(void) {}
228static inline void jump_label_unlock(void) {}
229
d430d3d7
JB
230static inline int jump_label_apply_nops(struct module *mod)
231{
232 return 0;
233}
b2029520 234
e33886b3
PZ
235static inline void static_key_enable(struct static_key *key)
236{
237 int count = static_key_count(key);
238
239 WARN_ON_ONCE(count < 0 || count > 1);
240
241 if (!count)
242 static_key_slow_inc(key);
243}
244
245static inline void static_key_disable(struct static_key *key)
246{
247 int count = static_key_count(key);
248
249 WARN_ON_ONCE(count < 0 || count > 1);
250
251 if (count)
252 static_key_slow_dec(key);
253}
254
1f69bf9c
JB
255#define STATIC_KEY_INIT_TRUE { .enabled = ATOMIC_INIT(1) }
256#define STATIC_KEY_INIT_FALSE { .enabled = ATOMIC_INIT(0) }
257
258#endif /* HAVE_JUMP_LABEL */
259
260#define STATIC_KEY_INIT STATIC_KEY_INIT_FALSE
261#define jump_label_enabled static_key_enabled
262
11276d53
PZ
263/* -------------------------------------------------------------------------- */
264
265/*
266 * Two type wrappers around static_key, such that we can use compile time
267 * type differentiation to emit the right code.
268 *
269 * All the below code is macros in order to play type games.
270 */
271
272struct static_key_true {
273 struct static_key key;
274};
275
276struct static_key_false {
277 struct static_key key;
278};
279
280#define STATIC_KEY_TRUE_INIT (struct static_key_true) { .key = STATIC_KEY_INIT_TRUE, }
281#define STATIC_KEY_FALSE_INIT (struct static_key_false){ .key = STATIC_KEY_INIT_FALSE, }
282
283#define DEFINE_STATIC_KEY_TRUE(name) \
284 struct static_key_true name = STATIC_KEY_TRUE_INIT
285
b8fb0378
TL
286#define DECLARE_STATIC_KEY_TRUE(name) \
287 extern struct static_key_true name
288
11276d53
PZ
289#define DEFINE_STATIC_KEY_FALSE(name) \
290 struct static_key_false name = STATIC_KEY_FALSE_INIT
291
b8fb0378
TL
292#define DECLARE_STATIC_KEY_FALSE(name) \
293 extern struct static_key_false name
294
ef0da55a
CM
295#define DEFINE_STATIC_KEY_ARRAY_TRUE(name, count) \
296 struct static_key_true name[count] = { \
297 [0 ... (count) - 1] = STATIC_KEY_TRUE_INIT, \
298 }
299
300#define DEFINE_STATIC_KEY_ARRAY_FALSE(name, count) \
301 struct static_key_false name[count] = { \
302 [0 ... (count) - 1] = STATIC_KEY_FALSE_INIT, \
303 }
304
fa128fd7
TH
305extern bool ____wrong_branch_error(void);
306
307#define static_key_enabled(x) \
308({ \
309 if (!__builtin_types_compatible_p(typeof(*x), struct static_key) && \
310 !__builtin_types_compatible_p(typeof(*x), struct static_key_true) &&\
311 !__builtin_types_compatible_p(typeof(*x), struct static_key_false)) \
312 ____wrong_branch_error(); \
313 static_key_count((struct static_key *)x) > 0; \
314})
315
11276d53
PZ
316#ifdef HAVE_JUMP_LABEL
317
318/*
319 * Combine the right initial value (type) with the right branch order
320 * to generate the desired result.
321 *
322 *
323 * type\branch| likely (1) | unlikely (0)
324 * -----------+-----------------------+------------------
325 * | |
326 * true (1) | ... | ...
327 * | NOP | JMP L
328 * | <br-stmts> | 1: ...
329 * | L: ... |
330 * | |
331 * | | L: <br-stmts>
332 * | | jmp 1b
333 * | |
334 * -----------+-----------------------+------------------
335 * | |
336 * false (0) | ... | ...
337 * | JMP L | NOP
338 * | <br-stmts> | 1: ...
339 * | L: ... |
340 * | |
341 * | | L: <br-stmts>
342 * | | jmp 1b
343 * | |
344 * -----------+-----------------------+------------------
345 *
346 * The initial value is encoded in the LSB of static_key::entries,
347 * type: 0 = false, 1 = true.
348 *
349 * The branch type is encoded in the LSB of jump_entry::key,
350 * branch: 0 = unlikely, 1 = likely.
351 *
352 * This gives the following logic table:
353 *
354 * enabled type branch instuction
355 * -----------------------------+-----------
356 * 0 0 0 | NOP
357 * 0 0 1 | JMP
358 * 0 1 0 | NOP
359 * 0 1 1 | JMP
360 *
361 * 1 0 0 | JMP
362 * 1 0 1 | NOP
363 * 1 1 0 | JMP
364 * 1 1 1 | NOP
365 *
366 * Which gives the following functions:
367 *
368 * dynamic: instruction = enabled ^ branch
369 * static: instruction = type ^ branch
370 *
371 * See jump_label_type() / jump_label_init_type().
372 */
373
11276d53
PZ
374#define static_branch_likely(x) \
375({ \
376 bool branch; \
377 if (__builtin_types_compatible_p(typeof(*x), struct static_key_true)) \
378 branch = !arch_static_branch(&(x)->key, true); \
379 else if (__builtin_types_compatible_p(typeof(*x), struct static_key_false)) \
380 branch = !arch_static_branch_jump(&(x)->key, true); \
381 else \
382 branch = ____wrong_branch_error(); \
383 branch; \
384})
385
386#define static_branch_unlikely(x) \
387({ \
388 bool branch; \
389 if (__builtin_types_compatible_p(typeof(*x), struct static_key_true)) \
390 branch = arch_static_branch_jump(&(x)->key, false); \
391 else if (__builtin_types_compatible_p(typeof(*x), struct static_key_false)) \
392 branch = arch_static_branch(&(x)->key, false); \
393 else \
394 branch = ____wrong_branch_error(); \
395 branch; \
396})
397
398#else /* !HAVE_JUMP_LABEL */
399
400#define static_branch_likely(x) likely(static_key_enabled(&(x)->key))
401#define static_branch_unlikely(x) unlikely(static_key_enabled(&(x)->key))
402
403#endif /* HAVE_JUMP_LABEL */
404
405/*
406 * Advanced usage; refcount, branch is enabled when: count != 0
407 */
408
409#define static_branch_inc(x) static_key_slow_inc(&(x)->key)
410#define static_branch_dec(x) static_key_slow_dec(&(x)->key)
411
412/*
413 * Normal usage; boolean enable/disable.
414 */
415
416#define static_branch_enable(x) static_key_enable(&(x)->key)
417#define static_branch_disable(x) static_key_disable(&(x)->key)
418
c0ccf6f9 419#endif /* __ASSEMBLY__ */
85b36c93
LR
420
421#endif /* _LINUX_JUMP_LABEL_H */