blob: 661af564fae8b2662f4fdd8f17557ebc8a443208 [file] [log] [blame]
Jason Baronbf5438fc2010-09-17 11:09:00 -04001#ifndef _LINUX_JUMP_LABEL_H
2#define _LINUX_JUMP_LABEL_H
3
Peter Zijlstraefb30402012-01-26 13:32:15 +01004/*
5 * Jump label support
6 *
7 * Copyright (C) 2009-2012 Jason Baron <jbaron@redhat.com>
Peter Zijlstra90eec102015-11-16 11:08:45 +01008 * Copyright (C) 2011-2012 Red Hat, Inc., Peter Zijlstra
Peter Zijlstraefb30402012-01-26 13:32:15 +01009 *
Jason Baron412758c2015-07-30 03:59:48 +000010 * 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);
Jonathan Corbet1975dbc2015-09-14 17:11:05 -060024 * static_branch_likely()
25 * static_branch_unlikely()
Jason Baron412758c2015-07-30 03:59:48 +000026 *
Peter Zijlstraefb30402012-01-26 13:32:15 +010027 * Jump labels provide an interface to generate dynamic branches using
Jason Baron412758c2015-07-30 03:59:48 +000028 * 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.
Peter Zijlstraefb30402012-01-26 13:32:15 +010038 *
Jason Baron412758c2015-07-30 03:59:48 +000039 * 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.
Peter Zijlstraefb30402012-01-26 13:32:15 +010047 *
Jonathan Corbet1975dbc2015-09-14 17:11:05 -060048 * In addition to static_branch_{enable,disable}, we can also reference count
Jason Baron412758c2015-07-30 03:59:48 +000049 * 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
Jonathan Corbet1975dbc2015-09-14 17:11:05 -060051 * static_branch_dec() as a 'make more false'.
Jason Baron412758c2015-07-30 03:59:48 +000052 *
53 * Since this relies on modifying code, the branch modifying functions
Peter Zijlstraefb30402012-01-26 13:32:15 +010054 * must be considered absolute slow paths (machine wide synchronization etc.).
Ingo Molnarfd3cbdc2014-08-10 08:53:39 +020055 * OTOH, since the affected branches are unconditional, their runtime overhead
Peter Zijlstraefb30402012-01-26 13:32:15 +010056 * will be absolutely minimal, esp. in the default (off) case where the total
57 * effect is a single NOP of appropriate size. The on case will patch in a jump
58 * to the out-of-line block.
59 *
Ingo Molnarfd3cbdc2014-08-10 08:53:39 +020060 * When the control is directly exposed to userspace, it is prudent to delay the
Peter Zijlstraefb30402012-01-26 13:32:15 +010061 * decrement to avoid high frequency code modifications which can (and do)
Ingo Molnarc5905af2012-02-24 08:31:31 +010062 * cause significant performance degradation. Struct static_key_deferred and
63 * static_key_slow_dec_deferred() provide for this.
Peter Zijlstraefb30402012-01-26 13:32:15 +010064 *
Jason Baron412758c2015-07-30 03:59:48 +000065 * Lacking toolchain and or architecture support, static keys fall back to a
66 * simple conditional branch.
Ingo Molnarc5905af2012-02-24 08:31:31 +010067 *
Jason Baron412758c2015-07-30 03:59:48 +000068 * Additional babbling in: Documentation/static-keys.txt
Ingo Molnarfd3cbdc2014-08-10 08:53:39 +020069 */
Peter Zijlstraefb30402012-01-26 13:32:15 +010070
Anton Blanchardc0ccf6f2015-04-09 13:51:31 +100071#if defined(CC_HAVE_ASM_GOTO) && defined(CONFIG_JUMP_LABEL)
72# define HAVE_JUMP_LABEL
73#endif
74
75#ifndef __ASSEMBLY__
76
Jason Barond430d3d2011-03-16 17:29:47 -040077#include <linux/types.h>
78#include <linux/compiler.h>
Hannes Frederic Sowac4b2c0c2013-10-19 21:48:53 +020079
80extern bool static_key_initialized;
81
82#define STATIC_KEY_CHECK_USE() WARN(!static_key_initialized, \
83 "%s used before call to jump_label_init", \
84 __func__)
Jason Barond430d3d2011-03-16 17:29:47 -040085
Anton Blanchardc0ccf6f2015-04-09 13:51:31 +100086#ifdef HAVE_JUMP_LABEL
Jason Barond430d3d2011-03-16 17:29:47 -040087
Ingo Molnarc5905af2012-02-24 08:31:31 +010088struct static_key {
Jason Barond430d3d2011-03-16 17:29:47 -040089 atomic_t enabled;
Ingo Molnarc5905af2012-02-24 08:31:31 +010090/* Set lsb bit to 1 if branch is default true, 0 ot */
Jason Barond430d3d2011-03-16 17:29:47 -040091 struct jump_entry *entries;
92#ifdef CONFIG_MODULES
Ingo Molnarc5905af2012-02-24 08:31:31 +010093 struct static_key_mod *next;
Jason Barond430d3d2011-03-16 17:29:47 -040094#endif
95};
96
Mel Gormanea5e9532014-06-04 16:10:07 -070097#else
98struct static_key {
99 atomic_t enabled;
100};
Anton Blanchardc0ccf6f2015-04-09 13:51:31 +1000101#endif /* HAVE_JUMP_LABEL */
102#endif /* __ASSEMBLY__ */
103
104#ifdef HAVE_JUMP_LABEL
105#include <asm/jump_label.h>
106#endif
107
108#ifndef __ASSEMBLY__
Jason Baronbf5438fc2010-09-17 11:09:00 -0400109
110enum jump_label_type {
Peter Zijlstra76b235c2015-07-24 14:45:44 +0200111 JUMP_LABEL_NOP = 0,
112 JUMP_LABEL_JMP,
Jason Baronbf5438fc2010-09-17 11:09:00 -0400113};
114
115struct module;
116
Paolo Bonzini4c5ea0a2016-06-21 18:52:17 +0200117#ifdef HAVE_JUMP_LABEL
118
Peter Zijlstraa1efb012015-07-24 14:55:40 +0200119#define JUMP_TYPE_FALSE 0UL
120#define JUMP_TYPE_TRUE 1UL
121#define JUMP_TYPE_MASK 1UL
Ingo Molnarc5905af2012-02-24 08:31:31 +0100122
123static __always_inline bool static_key_false(struct static_key *key)
124{
Peter Zijlstra11276d52015-07-24 15:09:55 +0200125 return arch_static_branch(key, false);
Ingo Molnarc5905af2012-02-24 08:31:31 +0100126}
127
128static __always_inline bool static_key_true(struct static_key *key)
129{
Peter Zijlstra11276d52015-07-24 15:09:55 +0200130 return !arch_static_branch(key, true);
Ingo Molnarc5905af2012-02-24 08:31:31 +0100131}
132
Jason Baronbf5438fc2010-09-17 11:09:00 -0400133extern struct jump_entry __start___jump_table[];
134extern struct jump_entry __stop___jump_table[];
135
Jeremy Fitzhardinge97ce2c82011-10-12 16:17:54 -0700136extern void jump_label_init(void);
Jason Baron91bad2f2010-10-01 17:23:48 -0400137extern void jump_label_lock(void);
138extern void jump_label_unlock(void);
Jason Baronbf5438fc2010-09-17 11:09:00 -0400139extern void arch_jump_label_transform(struct jump_entry *entry,
Jeremy Fitzhardinge37348802011-09-29 11:10:05 -0700140 enum jump_label_type type);
Jeremy Fitzhardinge20284aa2011-10-03 11:01:46 -0700141extern void arch_jump_label_transform_static(struct jump_entry *entry,
142 enum jump_label_type type);
Jason Baron4c3ef6d2010-09-17 11:09:08 -0400143extern int jump_label_text_reserved(void *start, void *end);
Ingo Molnarc5905af2012-02-24 08:31:31 +0100144extern void static_key_slow_inc(struct static_key *key);
145extern void static_key_slow_dec(struct static_key *key);
Jason Barond430d3d2011-03-16 17:29:47 -0400146extern void jump_label_apply_nops(struct module *mod);
Jason Baron1f69bf92016-08-03 13:46:36 -0700147extern int static_key_count(struct static_key *key);
148extern void static_key_enable(struct static_key *key);
149extern void static_key_disable(struct static_key *key);
Ingo Molnarc5905af2012-02-24 08:31:31 +0100150
Jason Baron1f69bf92016-08-03 13:46:36 -0700151/*
152 * We should be using ATOMIC_INIT() for initializing .enabled, but
153 * the inclusion of atomic.h is problematic for inclusion of jump_label.h
154 * in 'low-level' headers. Thus, we are initializing .enabled with a
155 * raw value, but have added a BUILD_BUG_ON() to catch any issues in
156 * jump_label_init() see: kernel/jump_label.c.
157 */
Peter Zijlstra11276d52015-07-24 15:09:55 +0200158#define STATIC_KEY_INIT_TRUE \
Jason Baron1f69bf92016-08-03 13:46:36 -0700159 { .enabled = { 1 }, \
Peter Zijlstra11276d52015-07-24 15:09:55 +0200160 .entries = (void *)JUMP_TYPE_TRUE }
161#define STATIC_KEY_INIT_FALSE \
Jason Baron1f69bf92016-08-03 13:46:36 -0700162 { .enabled = { 0 }, \
Peter Zijlstra11276d52015-07-24 15:09:55 +0200163 .entries = (void *)JUMP_TYPE_FALSE }
Jason Baronbf5438fc2010-09-17 11:09:00 -0400164
Jeremy Fitzhardinge97ce2c82011-10-12 16:17:54 -0700165#else /* !HAVE_JUMP_LABEL */
Jason Baronbf5438fc2010-09-17 11:09:00 -0400166
Jason Baron1f69bf92016-08-03 13:46:36 -0700167#include <linux/atomic.h>
168#include <linux/bug.h>
169
Paolo Bonzini4c5ea0a2016-06-21 18:52:17 +0200170static inline int static_key_count(struct static_key *key)
171{
172 return atomic_read(&key->enabled);
173}
174
Jeremy Fitzhardinge97ce2c82011-10-12 16:17:54 -0700175static __always_inline void jump_label_init(void)
176{
Hannes Frederic Sowac4b2c0c2013-10-19 21:48:53 +0200177 static_key_initialized = true;
Jeremy Fitzhardinge97ce2c82011-10-12 16:17:54 -0700178}
179
Ingo Molnarc5905af2012-02-24 08:31:31 +0100180static __always_inline bool static_key_false(struct static_key *key)
Jason Baronbf5438fc2010-09-17 11:09:00 -0400181{
Mel Gormanea5e9532014-06-04 16:10:07 -0700182 if (unlikely(static_key_count(key) > 0))
Jason Barond430d3d2011-03-16 17:29:47 -0400183 return true;
184 return false;
185}
186
Ingo Molnarc5905af2012-02-24 08:31:31 +0100187static __always_inline bool static_key_true(struct static_key *key)
188{
Mel Gormanea5e9532014-06-04 16:10:07 -0700189 if (likely(static_key_count(key) > 0))
Ingo Molnarc5905af2012-02-24 08:31:31 +0100190 return true;
191 return false;
192}
193
Ingo Molnarc5905af2012-02-24 08:31:31 +0100194static inline void static_key_slow_inc(struct static_key *key)
Jason Barond430d3d2011-03-16 17:29:47 -0400195{
Hannes Frederic Sowac4b2c0c2013-10-19 21:48:53 +0200196 STATIC_KEY_CHECK_USE();
Jason Barond430d3d2011-03-16 17:29:47 -0400197 atomic_inc(&key->enabled);
198}
199
Ingo Molnarc5905af2012-02-24 08:31:31 +0100200static inline void static_key_slow_dec(struct static_key *key)
Jason Barond430d3d2011-03-16 17:29:47 -0400201{
Hannes Frederic Sowac4b2c0c2013-10-19 21:48:53 +0200202 STATIC_KEY_CHECK_USE();
Jason Barond430d3d2011-03-16 17:29:47 -0400203 atomic_dec(&key->enabled);
Jason Baronbf5438fc2010-09-17 11:09:00 -0400204}
205
Jason Baron4c3ef6d2010-09-17 11:09:08 -0400206static inline int jump_label_text_reserved(void *start, void *end)
207{
208 return 0;
209}
210
Jason Baron91bad2f2010-10-01 17:23:48 -0400211static inline void jump_label_lock(void) {}
212static inline void jump_label_unlock(void) {}
213
Jason Barond430d3d2011-03-16 17:29:47 -0400214static inline int jump_label_apply_nops(struct module *mod)
215{
216 return 0;
217}
Gleb Natapovb2029522011-11-27 17:59:09 +0200218
Peter Zijlstrae33886b2015-07-24 15:03:40 +0200219static inline void static_key_enable(struct static_key *key)
220{
221 int count = static_key_count(key);
222
223 WARN_ON_ONCE(count < 0 || count > 1);
224
225 if (!count)
226 static_key_slow_inc(key);
227}
228
229static inline void static_key_disable(struct static_key *key)
230{
231 int count = static_key_count(key);
232
233 WARN_ON_ONCE(count < 0 || count > 1);
234
235 if (count)
236 static_key_slow_dec(key);
237}
238
Jason Baron1f69bf92016-08-03 13:46:36 -0700239#define STATIC_KEY_INIT_TRUE { .enabled = ATOMIC_INIT(1) }
240#define STATIC_KEY_INIT_FALSE { .enabled = ATOMIC_INIT(0) }
241
242#endif /* HAVE_JUMP_LABEL */
243
244#define STATIC_KEY_INIT STATIC_KEY_INIT_FALSE
245#define jump_label_enabled static_key_enabled
246
Peter Zijlstra11276d52015-07-24 15:09:55 +0200247/* -------------------------------------------------------------------------- */
248
249/*
250 * Two type wrappers around static_key, such that we can use compile time
251 * type differentiation to emit the right code.
252 *
253 * All the below code is macros in order to play type games.
254 */
255
256struct static_key_true {
257 struct static_key key;
258};
259
260struct static_key_false {
261 struct static_key key;
262};
263
264#define STATIC_KEY_TRUE_INIT (struct static_key_true) { .key = STATIC_KEY_INIT_TRUE, }
265#define STATIC_KEY_FALSE_INIT (struct static_key_false){ .key = STATIC_KEY_INIT_FALSE, }
266
267#define DEFINE_STATIC_KEY_TRUE(name) \
268 struct static_key_true name = STATIC_KEY_TRUE_INIT
269
270#define DEFINE_STATIC_KEY_FALSE(name) \
271 struct static_key_false name = STATIC_KEY_FALSE_INIT
272
Tejun Heofa128fd2015-09-18 11:56:28 -0400273extern bool ____wrong_branch_error(void);
274
275#define static_key_enabled(x) \
276({ \
277 if (!__builtin_types_compatible_p(typeof(*x), struct static_key) && \
278 !__builtin_types_compatible_p(typeof(*x), struct static_key_true) &&\
279 !__builtin_types_compatible_p(typeof(*x), struct static_key_false)) \
280 ____wrong_branch_error(); \
281 static_key_count((struct static_key *)x) > 0; \
282})
283
Peter Zijlstra11276d52015-07-24 15:09:55 +0200284#ifdef HAVE_JUMP_LABEL
285
286/*
287 * Combine the right initial value (type) with the right branch order
288 * to generate the desired result.
289 *
290 *
291 * type\branch| likely (1) | unlikely (0)
292 * -----------+-----------------------+------------------
293 * | |
294 * true (1) | ... | ...
295 * | NOP | JMP L
296 * | <br-stmts> | 1: ...
297 * | L: ... |
298 * | |
299 * | | L: <br-stmts>
300 * | | jmp 1b
301 * | |
302 * -----------+-----------------------+------------------
303 * | |
304 * false (0) | ... | ...
305 * | JMP L | NOP
306 * | <br-stmts> | 1: ...
307 * | L: ... |
308 * | |
309 * | | L: <br-stmts>
310 * | | jmp 1b
311 * | |
312 * -----------+-----------------------+------------------
313 *
314 * The initial value is encoded in the LSB of static_key::entries,
315 * type: 0 = false, 1 = true.
316 *
317 * The branch type is encoded in the LSB of jump_entry::key,
318 * branch: 0 = unlikely, 1 = likely.
319 *
320 * This gives the following logic table:
321 *
322 * enabled type branch instuction
323 * -----------------------------+-----------
324 * 0 0 0 | NOP
325 * 0 0 1 | JMP
326 * 0 1 0 | NOP
327 * 0 1 1 | JMP
328 *
329 * 1 0 0 | JMP
330 * 1 0 1 | NOP
331 * 1 1 0 | JMP
332 * 1 1 1 | NOP
333 *
334 * Which gives the following functions:
335 *
336 * dynamic: instruction = enabled ^ branch
337 * static: instruction = type ^ branch
338 *
339 * See jump_label_type() / jump_label_init_type().
340 */
341
Peter Zijlstra11276d52015-07-24 15:09:55 +0200342#define static_branch_likely(x) \
343({ \
344 bool branch; \
345 if (__builtin_types_compatible_p(typeof(*x), struct static_key_true)) \
346 branch = !arch_static_branch(&(x)->key, true); \
347 else if (__builtin_types_compatible_p(typeof(*x), struct static_key_false)) \
348 branch = !arch_static_branch_jump(&(x)->key, true); \
349 else \
350 branch = ____wrong_branch_error(); \
351 branch; \
352})
353
354#define static_branch_unlikely(x) \
355({ \
356 bool branch; \
357 if (__builtin_types_compatible_p(typeof(*x), struct static_key_true)) \
358 branch = arch_static_branch_jump(&(x)->key, false); \
359 else if (__builtin_types_compatible_p(typeof(*x), struct static_key_false)) \
360 branch = arch_static_branch(&(x)->key, false); \
361 else \
362 branch = ____wrong_branch_error(); \
363 branch; \
364})
365
366#else /* !HAVE_JUMP_LABEL */
367
368#define static_branch_likely(x) likely(static_key_enabled(&(x)->key))
369#define static_branch_unlikely(x) unlikely(static_key_enabled(&(x)->key))
370
371#endif /* HAVE_JUMP_LABEL */
372
373/*
374 * Advanced usage; refcount, branch is enabled when: count != 0
375 */
376
377#define static_branch_inc(x) static_key_slow_inc(&(x)->key)
378#define static_branch_dec(x) static_key_slow_dec(&(x)->key)
379
380/*
381 * Normal usage; boolean enable/disable.
382 */
383
384#define static_branch_enable(x) static_key_enable(&(x)->key)
385#define static_branch_disable(x) static_key_disable(&(x)->key)
386
Jeremy Fitzhardinge97ce2c82011-10-12 16:17:54 -0700387#endif /* _LINUX_JUMP_LABEL_H */
Anton Blanchardc0ccf6f2015-04-09 13:51:31 +1000388
389#endif /* __ASSEMBLY__ */