blob: cd5861651b17eeb28e66ecb82f5b4ba99ce3cf39 [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);
Catalin Marinasef0da552016-09-05 18:25:47 +010024 * DEFINE_STATIC_KEY_ARRAY_TRUE(keys, count);
25 * DEFINE_STATIC_KEY_ARRAY_FALSE(keys, count);
Jonathan Corbet1975dbc2015-09-14 17:11:05 -060026 * static_branch_likely()
27 * static_branch_unlikely()
Jason Baron412758c2015-07-30 03:59:48 +000028 *
Peter Zijlstraefb30402012-01-26 13:32:15 +010029 * Jump labels provide an interface to generate dynamic branches using
Jason Baron412758c2015-07-30 03:59:48 +000030 * 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.
Peter Zijlstraefb30402012-01-26 13:32:15 +010040 *
Jason Baron412758c2015-07-30 03:59:48 +000041 * 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.
Peter Zijlstraefb30402012-01-26 13:32:15 +010049 *
Jonathan Corbet1975dbc2015-09-14 17:11:05 -060050 * In addition to static_branch_{enable,disable}, we can also reference count
Jason Baron412758c2015-07-30 03:59:48 +000051 * 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
Jonathan Corbet1975dbc2015-09-14 17:11:05 -060053 * static_branch_dec() as a 'make more false'.
Jason Baron412758c2015-07-30 03:59:48 +000054 *
55 * Since this relies on modifying code, the branch modifying functions
Peter Zijlstraefb30402012-01-26 13:32:15 +010056 * must be considered absolute slow paths (machine wide synchronization etc.).
Ingo Molnarfd3cbdc2014-08-10 08:53:39 +020057 * OTOH, since the affected branches are unconditional, their runtime overhead
Peter Zijlstraefb30402012-01-26 13:32:15 +010058 * 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 *
Ingo Molnarfd3cbdc2014-08-10 08:53:39 +020062 * When the control is directly exposed to userspace, it is prudent to delay the
Peter Zijlstraefb30402012-01-26 13:32:15 +010063 * decrement to avoid high frequency code modifications which can (and do)
Ingo Molnarc5905af2012-02-24 08:31:31 +010064 * cause significant performance degradation. Struct static_key_deferred and
65 * static_key_slow_dec_deferred() provide for this.
Peter Zijlstraefb30402012-01-26 13:32:15 +010066 *
Jason Baron412758c2015-07-30 03:59:48 +000067 * Lacking toolchain and or architecture support, static keys fall back to a
68 * simple conditional branch.
Ingo Molnarc5905af2012-02-24 08:31:31 +010069 *
Jason Baron412758c2015-07-30 03:59:48 +000070 * Additional babbling in: Documentation/static-keys.txt
Ingo Molnarfd3cbdc2014-08-10 08:53:39 +020071 */
Peter Zijlstraefb30402012-01-26 13:32:15 +010072
Anton Blanchardc0ccf6f2015-04-09 13:51:31 +100073#if defined(CC_HAVE_ASM_GOTO) && defined(CONFIG_JUMP_LABEL)
74# define HAVE_JUMP_LABEL
75#endif
76
77#ifndef __ASSEMBLY__
78
Jason Barond430d3d2011-03-16 17:29:47 -040079#include <linux/types.h>
80#include <linux/compiler.h>
Hannes Frederic Sowac4b2c0c2013-10-19 21:48:53 +020081
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__)
Jason Barond430d3d2011-03-16 17:29:47 -040087
Anton Blanchardc0ccf6f2015-04-09 13:51:31 +100088#ifdef HAVE_JUMP_LABEL
Jason Barond430d3d2011-03-16 17:29:47 -040089
Ingo Molnarc5905af2012-02-24 08:31:31 +010090struct static_key {
Jason Barond430d3d2011-03-16 17:29:47 -040091 atomic_t enabled;
Jason Baron3821fd32017-02-03 15:42:24 -050092/*
Steven Rostedt (VMware)b17ef2e2017-03-02 17:28:45 -050093 * 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 *
Jason Baron3821fd32017-02-03 15:42:24 -0500100 * 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 };
Jason Barond430d3d2011-03-16 17:29:47 -0400110};
111
Mel Gormanea5e9532014-06-04 16:10:07 -0700112#else
113struct static_key {
114 atomic_t enabled;
115};
Anton Blanchardc0ccf6f2015-04-09 13:51:31 +1000116#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__
Jason Baronbf5438fc2010-09-17 11:09:00 -0400124
125enum jump_label_type {
Peter Zijlstra76b235c2015-07-24 14:45:44 +0200126 JUMP_LABEL_NOP = 0,
127 JUMP_LABEL_JMP,
Jason Baronbf5438fc2010-09-17 11:09:00 -0400128};
129
130struct module;
131
Paolo Bonzini4c5ea0a2016-06-21 18:52:17 +0200132#ifdef HAVE_JUMP_LABEL
133
Jason Baron3821fd32017-02-03 15:42:24 -0500134#define JUMP_TYPE_FALSE 0UL
135#define JUMP_TYPE_TRUE 1UL
136#define JUMP_TYPE_LINKED 2UL
137#define JUMP_TYPE_MASK 3UL
Ingo Molnarc5905af2012-02-24 08:31:31 +0100138
139static __always_inline bool static_key_false(struct static_key *key)
140{
Peter Zijlstra11276d52015-07-24 15:09:55 +0200141 return arch_static_branch(key, false);
Ingo Molnarc5905af2012-02-24 08:31:31 +0100142}
143
144static __always_inline bool static_key_true(struct static_key *key)
145{
Peter Zijlstra11276d52015-07-24 15:09:55 +0200146 return !arch_static_branch(key, true);
Ingo Molnarc5905af2012-02-24 08:31:31 +0100147}
148
Jason Baronbf5438fc2010-09-17 11:09:00 -0400149extern struct jump_entry __start___jump_table[];
150extern struct jump_entry __stop___jump_table[];
151
Jeremy Fitzhardinge97ce2c82011-10-12 16:17:54 -0700152extern void jump_label_init(void);
Jason Baron91bad2f2010-10-01 17:23:48 -0400153extern void jump_label_lock(void);
154extern void jump_label_unlock(void);
Jason Baronbf5438fc2010-09-17 11:09:00 -0400155extern void arch_jump_label_transform(struct jump_entry *entry,
Jeremy Fitzhardinge37348802011-09-29 11:10:05 -0700156 enum jump_label_type type);
Jeremy Fitzhardinge20284aa2011-10-03 11:01:46 -0700157extern void arch_jump_label_transform_static(struct jump_entry *entry,
158 enum jump_label_type type);
Jason Baron4c3ef6d2010-09-17 11:09:08 -0400159extern int jump_label_text_reserved(void *start, void *end);
Ingo Molnarc5905af2012-02-24 08:31:31 +0100160extern void static_key_slow_inc(struct static_key *key);
161extern void static_key_slow_dec(struct static_key *key);
Jason Barond430d3d2011-03-16 17:29:47 -0400162extern void jump_label_apply_nops(struct module *mod);
Jason Baron1f69bf92016-08-03 13:46:36 -0700163extern 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);
Marc Zyngier5a405272017-08-01 09:02:56 +0100166extern void static_key_enable_cpuslocked(struct static_key *key);
167extern void static_key_disable_cpuslocked(struct static_key *key);
Ingo Molnarc5905af2012-02-24 08:31:31 +0100168
Jason Baron1f69bf92016-08-03 13:46:36 -0700169/*
170 * We should be using ATOMIC_INIT() for initializing .enabled, but
171 * the inclusion of atomic.h is problematic for inclusion of jump_label.h
172 * in 'low-level' headers. Thus, we are initializing .enabled with a
173 * raw value, but have added a BUILD_BUG_ON() to catch any issues in
174 * jump_label_init() see: kernel/jump_label.c.
175 */
Peter Zijlstra11276d52015-07-24 15:09:55 +0200176#define STATIC_KEY_INIT_TRUE \
Jason Baron1f69bf92016-08-03 13:46:36 -0700177 { .enabled = { 1 }, \
Boris Ostrovskycd8d8602017-02-28 11:32:22 -0500178 { .entries = (void *)JUMP_TYPE_TRUE } }
Peter Zijlstra11276d52015-07-24 15:09:55 +0200179#define STATIC_KEY_INIT_FALSE \
Jason Baron1f69bf92016-08-03 13:46:36 -0700180 { .enabled = { 0 }, \
Boris Ostrovskycd8d8602017-02-28 11:32:22 -0500181 { .entries = (void *)JUMP_TYPE_FALSE } }
Jason Baronbf5438fc2010-09-17 11:09:00 -0400182
Jeremy Fitzhardinge97ce2c82011-10-12 16:17:54 -0700183#else /* !HAVE_JUMP_LABEL */
Jason Baronbf5438fc2010-09-17 11:09:00 -0400184
Jason Baron1f69bf92016-08-03 13:46:36 -0700185#include <linux/atomic.h>
186#include <linux/bug.h>
187
Paolo Bonzini4c5ea0a2016-06-21 18:52:17 +0200188static inline int static_key_count(struct static_key *key)
189{
190 return atomic_read(&key->enabled);
191}
192
Jeremy Fitzhardinge97ce2c82011-10-12 16:17:54 -0700193static __always_inline void jump_label_init(void)
194{
Hannes Frederic Sowac4b2c0c2013-10-19 21:48:53 +0200195 static_key_initialized = true;
Jeremy Fitzhardinge97ce2c82011-10-12 16:17:54 -0700196}
197
Ingo Molnarc5905af2012-02-24 08:31:31 +0100198static __always_inline bool static_key_false(struct static_key *key)
Jason Baronbf5438fc2010-09-17 11:09:00 -0400199{
Mel Gormanea5e9532014-06-04 16:10:07 -0700200 if (unlikely(static_key_count(key) > 0))
Jason Barond430d3d2011-03-16 17:29:47 -0400201 return true;
202 return false;
203}
204
Ingo Molnarc5905af2012-02-24 08:31:31 +0100205static __always_inline bool static_key_true(struct static_key *key)
206{
Mel Gormanea5e9532014-06-04 16:10:07 -0700207 if (likely(static_key_count(key) > 0))
Ingo Molnarc5905af2012-02-24 08:31:31 +0100208 return true;
209 return false;
210}
211
Ingo Molnarc5905af2012-02-24 08:31:31 +0100212static inline void static_key_slow_inc(struct static_key *key)
Jason Barond430d3d2011-03-16 17:29:47 -0400213{
Hannes Frederic Sowac4b2c0c2013-10-19 21:48:53 +0200214 STATIC_KEY_CHECK_USE();
Jason Barond430d3d2011-03-16 17:29:47 -0400215 atomic_inc(&key->enabled);
216}
217
Ingo Molnarc5905af2012-02-24 08:31:31 +0100218static inline void static_key_slow_dec(struct static_key *key)
Jason Barond430d3d2011-03-16 17:29:47 -0400219{
Hannes Frederic Sowac4b2c0c2013-10-19 21:48:53 +0200220 STATIC_KEY_CHECK_USE();
Jason Barond430d3d2011-03-16 17:29:47 -0400221 atomic_dec(&key->enabled);
Jason Baronbf5438fc2010-09-17 11:09:00 -0400222}
223
Jason Baron4c3ef6d2010-09-17 11:09:08 -0400224static inline int jump_label_text_reserved(void *start, void *end)
225{
226 return 0;
227}
228
Jason Baron91bad2f2010-10-01 17:23:48 -0400229static inline void jump_label_lock(void) {}
230static inline void jump_label_unlock(void) {}
231
Jason Barond430d3d2011-03-16 17:29:47 -0400232static inline int jump_label_apply_nops(struct module *mod)
233{
234 return 0;
235}
Gleb Natapovb2029522011-11-27 17:59:09 +0200236
Peter Zijlstrae33886b2015-07-24 15:03:40 +0200237static inline void static_key_enable(struct static_key *key)
238{
Paolo Bonzini1dbb6702017-08-01 17:24:04 +0200239 STATIC_KEY_CHECK_USE();
Peter Zijlstrae33886b2015-07-24 15:03:40 +0200240
Paolo Bonzini1dbb6702017-08-01 17:24:04 +0200241 if (atomic_read(&key->enabled) != 0) {
242 WARN_ON_ONCE(atomic_read(&key->enabled) != 1);
243 return;
244 }
245 atomic_set(&key->enabled, 1);
Peter Zijlstrae33886b2015-07-24 15:03:40 +0200246}
247
248static inline void static_key_disable(struct static_key *key)
249{
Paolo Bonzini1dbb6702017-08-01 17:24:04 +0200250 STATIC_KEY_CHECK_USE();
Peter Zijlstrae33886b2015-07-24 15:03:40 +0200251
Paolo Bonzini1dbb6702017-08-01 17:24:04 +0200252 if (atomic_read(&key->enabled) != 1) {
253 WARN_ON_ONCE(atomic_read(&key->enabled) != 0);
254 return;
255 }
256 atomic_set(&key->enabled, 0);
Peter Zijlstrae33886b2015-07-24 15:03:40 +0200257}
258
Marc Zyngier5a405272017-08-01 09:02:56 +0100259#define static_key_enable_cpuslocked(k) static_key_enable((k))
260#define static_key_disable_cpuslocked(k) static_key_disable((k))
261
Jason Baron1f69bf92016-08-03 13:46:36 -0700262#define STATIC_KEY_INIT_TRUE { .enabled = ATOMIC_INIT(1) }
263#define STATIC_KEY_INIT_FALSE { .enabled = ATOMIC_INIT(0) }
264
265#endif /* HAVE_JUMP_LABEL */
266
267#define STATIC_KEY_INIT STATIC_KEY_INIT_FALSE
268#define jump_label_enabled static_key_enabled
269
Peter Zijlstra11276d52015-07-24 15:09:55 +0200270/* -------------------------------------------------------------------------- */
271
272/*
273 * Two type wrappers around static_key, such that we can use compile time
274 * type differentiation to emit the right code.
275 *
276 * All the below code is macros in order to play type games.
277 */
278
279struct static_key_true {
280 struct static_key key;
281};
282
283struct static_key_false {
284 struct static_key key;
285};
286
287#define STATIC_KEY_TRUE_INIT (struct static_key_true) { .key = STATIC_KEY_INIT_TRUE, }
288#define STATIC_KEY_FALSE_INIT (struct static_key_false){ .key = STATIC_KEY_INIT_FALSE, }
289
290#define DEFINE_STATIC_KEY_TRUE(name) \
291 struct static_key_true name = STATIC_KEY_TRUE_INIT
292
Tony Luckb8fb0372016-09-01 11:39:33 -0700293#define DECLARE_STATIC_KEY_TRUE(name) \
294 extern struct static_key_true name
295
Peter Zijlstra11276d52015-07-24 15:09:55 +0200296#define DEFINE_STATIC_KEY_FALSE(name) \
297 struct static_key_false name = STATIC_KEY_FALSE_INIT
298
Tony Luckb8fb0372016-09-01 11:39:33 -0700299#define DECLARE_STATIC_KEY_FALSE(name) \
300 extern struct static_key_false name
301
Catalin Marinasef0da552016-09-05 18:25:47 +0100302#define DEFINE_STATIC_KEY_ARRAY_TRUE(name, count) \
303 struct static_key_true name[count] = { \
304 [0 ... (count) - 1] = STATIC_KEY_TRUE_INIT, \
305 }
306
307#define DEFINE_STATIC_KEY_ARRAY_FALSE(name, count) \
308 struct static_key_false name[count] = { \
309 [0 ... (count) - 1] = STATIC_KEY_FALSE_INIT, \
310 }
311
Tejun Heofa128fd2015-09-18 11:56:28 -0400312extern bool ____wrong_branch_error(void);
313
314#define static_key_enabled(x) \
315({ \
316 if (!__builtin_types_compatible_p(typeof(*x), struct static_key) && \
317 !__builtin_types_compatible_p(typeof(*x), struct static_key_true) &&\
318 !__builtin_types_compatible_p(typeof(*x), struct static_key_false)) \
319 ____wrong_branch_error(); \
320 static_key_count((struct static_key *)x) > 0; \
321})
322
Peter Zijlstra11276d52015-07-24 15:09:55 +0200323#ifdef HAVE_JUMP_LABEL
324
325/*
326 * Combine the right initial value (type) with the right branch order
327 * to generate the desired result.
328 *
329 *
330 * type\branch| likely (1) | unlikely (0)
331 * -----------+-----------------------+------------------
332 * | |
333 * true (1) | ... | ...
334 * | NOP | JMP L
335 * | <br-stmts> | 1: ...
336 * | L: ... |
337 * | |
338 * | | L: <br-stmts>
339 * | | jmp 1b
340 * | |
341 * -----------+-----------------------+------------------
342 * | |
343 * false (0) | ... | ...
344 * | JMP L | NOP
345 * | <br-stmts> | 1: ...
346 * | L: ... |
347 * | |
348 * | | L: <br-stmts>
349 * | | jmp 1b
350 * | |
351 * -----------+-----------------------+------------------
352 *
353 * The initial value is encoded in the LSB of static_key::entries,
354 * type: 0 = false, 1 = true.
355 *
356 * The branch type is encoded in the LSB of jump_entry::key,
357 * branch: 0 = unlikely, 1 = likely.
358 *
359 * This gives the following logic table:
360 *
361 * enabled type branch instuction
362 * -----------------------------+-----------
363 * 0 0 0 | NOP
364 * 0 0 1 | JMP
365 * 0 1 0 | NOP
366 * 0 1 1 | JMP
367 *
368 * 1 0 0 | JMP
369 * 1 0 1 | NOP
370 * 1 1 0 | JMP
371 * 1 1 1 | NOP
372 *
373 * Which gives the following functions:
374 *
375 * dynamic: instruction = enabled ^ branch
376 * static: instruction = type ^ branch
377 *
378 * See jump_label_type() / jump_label_init_type().
379 */
380
Peter Zijlstra11276d52015-07-24 15:09:55 +0200381#define static_branch_likely(x) \
382({ \
383 bool branch; \
384 if (__builtin_types_compatible_p(typeof(*x), struct static_key_true)) \
385 branch = !arch_static_branch(&(x)->key, true); \
386 else if (__builtin_types_compatible_p(typeof(*x), struct static_key_false)) \
387 branch = !arch_static_branch_jump(&(x)->key, true); \
388 else \
389 branch = ____wrong_branch_error(); \
390 branch; \
391})
392
393#define static_branch_unlikely(x) \
394({ \
395 bool branch; \
396 if (__builtin_types_compatible_p(typeof(*x), struct static_key_true)) \
397 branch = arch_static_branch_jump(&(x)->key, false); \
398 else if (__builtin_types_compatible_p(typeof(*x), struct static_key_false)) \
399 branch = arch_static_branch(&(x)->key, false); \
400 else \
401 branch = ____wrong_branch_error(); \
402 branch; \
403})
404
405#else /* !HAVE_JUMP_LABEL */
406
407#define static_branch_likely(x) likely(static_key_enabled(&(x)->key))
408#define static_branch_unlikely(x) unlikely(static_key_enabled(&(x)->key))
409
410#endif /* HAVE_JUMP_LABEL */
411
412/*
413 * Advanced usage; refcount, branch is enabled when: count != 0
414 */
415
416#define static_branch_inc(x) static_key_slow_inc(&(x)->key)
417#define static_branch_dec(x) static_key_slow_dec(&(x)->key)
418
419/*
420 * Normal usage; boolean enable/disable.
421 */
422
Marc Zyngier5a405272017-08-01 09:02:56 +0100423#define static_branch_enable(x) static_key_enable(&(x)->key)
424#define static_branch_disable(x) static_key_disable(&(x)->key)
425#define static_branch_enable_cpuslocked(x) static_key_enable_cpuslocked(&(x)->key)
426#define static_branch_disable_cpuslocked(x) static_key_disable_cpuslocked(&(x)->key)
Peter Zijlstra11276d52015-07-24 15:09:55 +0200427
Anton Blanchardc0ccf6f2015-04-09 13:51:31 +1000428#endif /* __ASSEMBLY__ */
Luis R. Rodriguez85b36c92017-01-18 09:38:04 -0800429
430#endif /* _LINUX_JUMP_LABEL_H */