Jason Baron | bf5438fc | 2010-09-17 11:09:00 -0400 | [diff] [blame] | 1 | #ifndef _LINUX_JUMP_LABEL_H |
| 2 | #define _LINUX_JUMP_LABEL_H |
| 3 | |
Peter Zijlstra | efb3040 | 2012-01-26 13:32:15 +0100 | [diff] [blame] | 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 | * |
Jason Baron | 412758c | 2015-07-30 03:59:48 +0000 | [diff] [blame] | 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 | * |
Peter Zijlstra | efb3040 | 2012-01-26 13:32:15 +0100 | [diff] [blame] | 27 | * Jump labels provide an interface to generate dynamic branches using |
Jason Baron | 412758c | 2015-07-30 03:59:48 +0000 | [diff] [blame] | 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. |
Peter Zijlstra | efb3040 | 2012-01-26 13:32:15 +0100 | [diff] [blame] | 38 | * |
Jason Baron | 412758c | 2015-07-30 03:59:48 +0000 | [diff] [blame] | 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. |
Peter Zijlstra | efb3040 | 2012-01-26 13:32:15 +0100 | [diff] [blame] | 47 | * |
Jason Baron | 412758c | 2015-07-30 03:59:48 +0000 | [diff] [blame] | 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 |
Peter Zijlstra | efb3040 | 2012-01-26 13:32:15 +0100 | [diff] [blame] | 56 | * must be considered absolute slow paths (machine wide synchronization etc.). |
Ingo Molnar | fd3cbdc | 2014-08-10 08:53:39 +0200 | [diff] [blame] | 57 | * OTOH, since the affected branches are unconditional, their runtime overhead |
Peter Zijlstra | efb3040 | 2012-01-26 13:32:15 +0100 | [diff] [blame] | 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 | * |
Ingo Molnar | fd3cbdc | 2014-08-10 08:53:39 +0200 | [diff] [blame] | 62 | * When the control is directly exposed to userspace, it is prudent to delay the |
Peter Zijlstra | efb3040 | 2012-01-26 13:32:15 +0100 | [diff] [blame] | 63 | * decrement to avoid high frequency code modifications which can (and do) |
Ingo Molnar | c5905af | 2012-02-24 08:31:31 +0100 | [diff] [blame] | 64 | * cause significant performance degradation. Struct static_key_deferred and |
| 65 | * static_key_slow_dec_deferred() provide for this. |
Peter Zijlstra | efb3040 | 2012-01-26 13:32:15 +0100 | [diff] [blame] | 66 | * |
Jason Baron | 412758c | 2015-07-30 03:59:48 +0000 | [diff] [blame] | 67 | * Lacking toolchain and or architecture support, static keys fall back to a |
| 68 | * simple conditional branch. |
Ingo Molnar | c5905af | 2012-02-24 08:31:31 +0100 | [diff] [blame] | 69 | * |
Jason Baron | 412758c | 2015-07-30 03:59:48 +0000 | [diff] [blame] | 70 | * Additional babbling in: Documentation/static-keys.txt |
Ingo Molnar | fd3cbdc | 2014-08-10 08:53:39 +0200 | [diff] [blame] | 71 | */ |
Peter Zijlstra | efb3040 | 2012-01-26 13:32:15 +0100 | [diff] [blame] | 72 | |
Anton Blanchard | c0ccf6f | 2015-04-09 13:51:31 +1000 | [diff] [blame] | 73 | #if defined(CC_HAVE_ASM_GOTO) && defined(CONFIG_JUMP_LABEL) |
| 74 | # define HAVE_JUMP_LABEL |
| 75 | #endif |
| 76 | |
| 77 | #ifndef __ASSEMBLY__ |
| 78 | |
Jason Baron | d430d3d | 2011-03-16 17:29:47 -0400 | [diff] [blame] | 79 | #include <linux/types.h> |
| 80 | #include <linux/compiler.h> |
Hannes Frederic Sowa | c4b2c0c | 2013-10-19 21:48:53 +0200 | [diff] [blame] | 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__) |
Jason Baron | d430d3d | 2011-03-16 17:29:47 -0400 | [diff] [blame] | 88 | |
Anton Blanchard | c0ccf6f | 2015-04-09 13:51:31 +1000 | [diff] [blame] | 89 | #ifdef HAVE_JUMP_LABEL |
Jason Baron | d430d3d | 2011-03-16 17:29:47 -0400 | [diff] [blame] | 90 | |
Ingo Molnar | c5905af | 2012-02-24 08:31:31 +0100 | [diff] [blame] | 91 | struct static_key { |
Jason Baron | d430d3d | 2011-03-16 17:29:47 -0400 | [diff] [blame] | 92 | atomic_t enabled; |
Ingo Molnar | c5905af | 2012-02-24 08:31:31 +0100 | [diff] [blame] | 93 | /* Set lsb bit to 1 if branch is default true, 0 ot */ |
Jason Baron | d430d3d | 2011-03-16 17:29:47 -0400 | [diff] [blame] | 94 | struct jump_entry *entries; |
| 95 | #ifdef CONFIG_MODULES |
Ingo Molnar | c5905af | 2012-02-24 08:31:31 +0100 | [diff] [blame] | 96 | struct static_key_mod *next; |
Jason Baron | d430d3d | 2011-03-16 17:29:47 -0400 | [diff] [blame] | 97 | #endif |
| 98 | }; |
| 99 | |
Mel Gorman | ea5e953 | 2014-06-04 16:10:07 -0700 | [diff] [blame] | 100 | #else |
| 101 | struct static_key { |
| 102 | atomic_t enabled; |
| 103 | }; |
Anton Blanchard | c0ccf6f | 2015-04-09 13:51:31 +1000 | [diff] [blame] | 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__ |
Jason Baron | bf5438fc | 2010-09-17 11:09:00 -0400 | [diff] [blame] | 112 | |
| 113 | enum jump_label_type { |
Peter Zijlstra | 76b235c | 2015-07-24 14:45:44 +0200 | [diff] [blame] | 114 | JUMP_LABEL_NOP = 0, |
| 115 | JUMP_LABEL_JMP, |
Jason Baron | bf5438fc | 2010-09-17 11:09:00 -0400 | [diff] [blame] | 116 | }; |
| 117 | |
| 118 | struct module; |
| 119 | |
Andrew Jones | 851cf6e | 2013-08-09 19:51:57 +0530 | [diff] [blame] | 120 | #include <linux/atomic.h> |
Mel Gorman | ea5e953 | 2014-06-04 16:10:07 -0700 | [diff] [blame] | 121 | |
| 122 | static inline int static_key_count(struct static_key *key) |
| 123 | { |
| 124 | return atomic_read(&key->enabled); |
| 125 | } |
| 126 | |
Jason Baron | bf5438fc | 2010-09-17 11:09:00 -0400 | [diff] [blame] | 127 | #ifdef HAVE_JUMP_LABEL |
| 128 | |
Peter Zijlstra | a1efb01 | 2015-07-24 14:55:40 +0200 | [diff] [blame] | 129 | #define JUMP_TYPE_FALSE 0UL |
| 130 | #define JUMP_TYPE_TRUE 1UL |
| 131 | #define JUMP_TYPE_MASK 1UL |
Ingo Molnar | c5905af | 2012-02-24 08:31:31 +0100 | [diff] [blame] | 132 | |
| 133 | static __always_inline bool static_key_false(struct static_key *key) |
| 134 | { |
Peter Zijlstra | 11276d5 | 2015-07-24 15:09:55 +0200 | [diff] [blame] | 135 | return arch_static_branch(key, false); |
Ingo Molnar | c5905af | 2012-02-24 08:31:31 +0100 | [diff] [blame] | 136 | } |
| 137 | |
| 138 | static __always_inline bool static_key_true(struct static_key *key) |
| 139 | { |
Peter Zijlstra | 11276d5 | 2015-07-24 15:09:55 +0200 | [diff] [blame] | 140 | return !arch_static_branch(key, true); |
Ingo Molnar | c5905af | 2012-02-24 08:31:31 +0100 | [diff] [blame] | 141 | } |
| 142 | |
Jason Baron | bf5438fc | 2010-09-17 11:09:00 -0400 | [diff] [blame] | 143 | extern struct jump_entry __start___jump_table[]; |
| 144 | extern struct jump_entry __stop___jump_table[]; |
| 145 | |
Jeremy Fitzhardinge | 97ce2c8 | 2011-10-12 16:17:54 -0700 | [diff] [blame] | 146 | extern void jump_label_init(void); |
Jason Baron | 91bad2f | 2010-10-01 17:23:48 -0400 | [diff] [blame] | 147 | extern void jump_label_lock(void); |
| 148 | extern void jump_label_unlock(void); |
Jason Baron | bf5438fc | 2010-09-17 11:09:00 -0400 | [diff] [blame] | 149 | extern void arch_jump_label_transform(struct jump_entry *entry, |
Jeremy Fitzhardinge | 3734880 | 2011-09-29 11:10:05 -0700 | [diff] [blame] | 150 | enum jump_label_type type); |
Jeremy Fitzhardinge | 20284aa | 2011-10-03 11:01:46 -0700 | [diff] [blame] | 151 | extern void arch_jump_label_transform_static(struct jump_entry *entry, |
| 152 | enum jump_label_type type); |
Jason Baron | 4c3ef6d | 2010-09-17 11:09:08 -0400 | [diff] [blame] | 153 | extern int jump_label_text_reserved(void *start, void *end); |
Ingo Molnar | c5905af | 2012-02-24 08:31:31 +0100 | [diff] [blame] | 154 | extern void static_key_slow_inc(struct static_key *key); |
| 155 | extern void static_key_slow_dec(struct static_key *key); |
Jason Baron | d430d3d | 2011-03-16 17:29:47 -0400 | [diff] [blame] | 156 | extern void jump_label_apply_nops(struct module *mod); |
Ingo Molnar | c5905af | 2012-02-24 08:31:31 +0100 | [diff] [blame] | 157 | |
Peter Zijlstra | 11276d5 | 2015-07-24 15:09:55 +0200 | [diff] [blame] | 158 | #define STATIC_KEY_INIT_TRUE \ |
Jiang Liu | f4be843 | 2014-01-07 22:17:14 +0800 | [diff] [blame] | 159 | { .enabled = ATOMIC_INIT(1), \ |
Peter Zijlstra | 11276d5 | 2015-07-24 15:09:55 +0200 | [diff] [blame] | 160 | .entries = (void *)JUMP_TYPE_TRUE } |
| 161 | #define STATIC_KEY_INIT_FALSE \ |
Jiang Liu | f4be843 | 2014-01-07 22:17:14 +0800 | [diff] [blame] | 162 | { .enabled = ATOMIC_INIT(0), \ |
Peter Zijlstra | 11276d5 | 2015-07-24 15:09:55 +0200 | [diff] [blame] | 163 | .entries = (void *)JUMP_TYPE_FALSE } |
Jason Baron | bf5438fc | 2010-09-17 11:09:00 -0400 | [diff] [blame] | 164 | |
Jeremy Fitzhardinge | 97ce2c8 | 2011-10-12 16:17:54 -0700 | [diff] [blame] | 165 | #else /* !HAVE_JUMP_LABEL */ |
Jason Baron | bf5438fc | 2010-09-17 11:09:00 -0400 | [diff] [blame] | 166 | |
Jeremy Fitzhardinge | 97ce2c8 | 2011-10-12 16:17:54 -0700 | [diff] [blame] | 167 | static __always_inline void jump_label_init(void) |
| 168 | { |
Hannes Frederic Sowa | c4b2c0c | 2013-10-19 21:48:53 +0200 | [diff] [blame] | 169 | static_key_initialized = true; |
Jeremy Fitzhardinge | 97ce2c8 | 2011-10-12 16:17:54 -0700 | [diff] [blame] | 170 | } |
| 171 | |
Ingo Molnar | c5905af | 2012-02-24 08:31:31 +0100 | [diff] [blame] | 172 | static __always_inline bool static_key_false(struct static_key *key) |
Jason Baron | bf5438fc | 2010-09-17 11:09:00 -0400 | [diff] [blame] | 173 | { |
Mel Gorman | ea5e953 | 2014-06-04 16:10:07 -0700 | [diff] [blame] | 174 | if (unlikely(static_key_count(key) > 0)) |
Jason Baron | d430d3d | 2011-03-16 17:29:47 -0400 | [diff] [blame] | 175 | return true; |
| 176 | return false; |
| 177 | } |
| 178 | |
Ingo Molnar | c5905af | 2012-02-24 08:31:31 +0100 | [diff] [blame] | 179 | static __always_inline bool static_key_true(struct static_key *key) |
| 180 | { |
Mel Gorman | ea5e953 | 2014-06-04 16:10:07 -0700 | [diff] [blame] | 181 | if (likely(static_key_count(key) > 0)) |
Ingo Molnar | c5905af | 2012-02-24 08:31:31 +0100 | [diff] [blame] | 182 | return true; |
| 183 | return false; |
| 184 | } |
| 185 | |
Ingo Molnar | c5905af | 2012-02-24 08:31:31 +0100 | [diff] [blame] | 186 | static inline void static_key_slow_inc(struct static_key *key) |
Jason Baron | d430d3d | 2011-03-16 17:29:47 -0400 | [diff] [blame] | 187 | { |
Hannes Frederic Sowa | c4b2c0c | 2013-10-19 21:48:53 +0200 | [diff] [blame] | 188 | STATIC_KEY_CHECK_USE(); |
Jason Baron | d430d3d | 2011-03-16 17:29:47 -0400 | [diff] [blame] | 189 | atomic_inc(&key->enabled); |
| 190 | } |
| 191 | |
Ingo Molnar | c5905af | 2012-02-24 08:31:31 +0100 | [diff] [blame] | 192 | static inline void static_key_slow_dec(struct static_key *key) |
Jason Baron | d430d3d | 2011-03-16 17:29:47 -0400 | [diff] [blame] | 193 | { |
Hannes Frederic Sowa | c4b2c0c | 2013-10-19 21:48:53 +0200 | [diff] [blame] | 194 | STATIC_KEY_CHECK_USE(); |
Jason Baron | d430d3d | 2011-03-16 17:29:47 -0400 | [diff] [blame] | 195 | atomic_dec(&key->enabled); |
Jason Baron | bf5438fc | 2010-09-17 11:09:00 -0400 | [diff] [blame] | 196 | } |
| 197 | |
Jason Baron | 4c3ef6d | 2010-09-17 11:09:08 -0400 | [diff] [blame] | 198 | static inline int jump_label_text_reserved(void *start, void *end) |
| 199 | { |
| 200 | return 0; |
| 201 | } |
| 202 | |
Jason Baron | 91bad2f | 2010-10-01 17:23:48 -0400 | [diff] [blame] | 203 | static inline void jump_label_lock(void) {} |
| 204 | static inline void jump_label_unlock(void) {} |
| 205 | |
Jason Baron | d430d3d | 2011-03-16 17:29:47 -0400 | [diff] [blame] | 206 | static inline int jump_label_apply_nops(struct module *mod) |
| 207 | { |
| 208 | return 0; |
| 209 | } |
Gleb Natapov | b202952 | 2011-11-27 17:59:09 +0200 | [diff] [blame] | 210 | |
Peter Zijlstra | 11276d5 | 2015-07-24 15:09:55 +0200 | [diff] [blame] | 211 | #define STATIC_KEY_INIT_TRUE { .enabled = ATOMIC_INIT(1) } |
| 212 | #define STATIC_KEY_INIT_FALSE { .enabled = ATOMIC_INIT(0) } |
Ingo Molnar | c5905af | 2012-02-24 08:31:31 +0100 | [diff] [blame] | 213 | |
Jeremy Fitzhardinge | 97ce2c8 | 2011-10-12 16:17:54 -0700 | [diff] [blame] | 214 | #endif /* HAVE_JUMP_LABEL */ |
Jason Baron | d430d3d | 2011-03-16 17:29:47 -0400 | [diff] [blame] | 215 | |
Ingo Molnar | c5905af | 2012-02-24 08:31:31 +0100 | [diff] [blame] | 216 | #define STATIC_KEY_INIT STATIC_KEY_INIT_FALSE |
| 217 | #define jump_label_enabled static_key_enabled |
Peter Zijlstra | ac99b86 | 2011-07-06 14:20:14 +0200 | [diff] [blame] | 218 | |
Peter Zijlstra | e33886b | 2015-07-24 15:03:40 +0200 | [diff] [blame] | 219 | static 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 | |
| 229 | static 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 | |
Peter Zijlstra | 11276d5 | 2015-07-24 15:09:55 +0200 | [diff] [blame] | 239 | /* -------------------------------------------------------------------------- */ |
| 240 | |
| 241 | /* |
| 242 | * Two type wrappers around static_key, such that we can use compile time |
| 243 | * type differentiation to emit the right code. |
| 244 | * |
| 245 | * All the below code is macros in order to play type games. |
| 246 | */ |
| 247 | |
| 248 | struct static_key_true { |
| 249 | struct static_key key; |
| 250 | }; |
| 251 | |
| 252 | struct static_key_false { |
| 253 | struct static_key key; |
| 254 | }; |
| 255 | |
| 256 | #define STATIC_KEY_TRUE_INIT (struct static_key_true) { .key = STATIC_KEY_INIT_TRUE, } |
| 257 | #define STATIC_KEY_FALSE_INIT (struct static_key_false){ .key = STATIC_KEY_INIT_FALSE, } |
| 258 | |
| 259 | #define DEFINE_STATIC_KEY_TRUE(name) \ |
| 260 | struct static_key_true name = STATIC_KEY_TRUE_INIT |
| 261 | |
| 262 | #define DEFINE_STATIC_KEY_FALSE(name) \ |
| 263 | struct static_key_false name = STATIC_KEY_FALSE_INIT |
| 264 | |
Tejun Heo | fa128fd | 2015-09-18 11:56:28 -0400 | [diff] [blame^] | 265 | extern bool ____wrong_branch_error(void); |
| 266 | |
| 267 | #define static_key_enabled(x) \ |
| 268 | ({ \ |
| 269 | if (!__builtin_types_compatible_p(typeof(*x), struct static_key) && \ |
| 270 | !__builtin_types_compatible_p(typeof(*x), struct static_key_true) &&\ |
| 271 | !__builtin_types_compatible_p(typeof(*x), struct static_key_false)) \ |
| 272 | ____wrong_branch_error(); \ |
| 273 | static_key_count((struct static_key *)x) > 0; \ |
| 274 | }) |
| 275 | |
Peter Zijlstra | 11276d5 | 2015-07-24 15:09:55 +0200 | [diff] [blame] | 276 | #ifdef HAVE_JUMP_LABEL |
| 277 | |
| 278 | /* |
| 279 | * Combine the right initial value (type) with the right branch order |
| 280 | * to generate the desired result. |
| 281 | * |
| 282 | * |
| 283 | * type\branch| likely (1) | unlikely (0) |
| 284 | * -----------+-----------------------+------------------ |
| 285 | * | | |
| 286 | * true (1) | ... | ... |
| 287 | * | NOP | JMP L |
| 288 | * | <br-stmts> | 1: ... |
| 289 | * | L: ... | |
| 290 | * | | |
| 291 | * | | L: <br-stmts> |
| 292 | * | | jmp 1b |
| 293 | * | | |
| 294 | * -----------+-----------------------+------------------ |
| 295 | * | | |
| 296 | * false (0) | ... | ... |
| 297 | * | JMP L | NOP |
| 298 | * | <br-stmts> | 1: ... |
| 299 | * | L: ... | |
| 300 | * | | |
| 301 | * | | L: <br-stmts> |
| 302 | * | | jmp 1b |
| 303 | * | | |
| 304 | * -----------+-----------------------+------------------ |
| 305 | * |
| 306 | * The initial value is encoded in the LSB of static_key::entries, |
| 307 | * type: 0 = false, 1 = true. |
| 308 | * |
| 309 | * The branch type is encoded in the LSB of jump_entry::key, |
| 310 | * branch: 0 = unlikely, 1 = likely. |
| 311 | * |
| 312 | * This gives the following logic table: |
| 313 | * |
| 314 | * enabled type branch instuction |
| 315 | * -----------------------------+----------- |
| 316 | * 0 0 0 | NOP |
| 317 | * 0 0 1 | JMP |
| 318 | * 0 1 0 | NOP |
| 319 | * 0 1 1 | JMP |
| 320 | * |
| 321 | * 1 0 0 | JMP |
| 322 | * 1 0 1 | NOP |
| 323 | * 1 1 0 | JMP |
| 324 | * 1 1 1 | NOP |
| 325 | * |
| 326 | * Which gives the following functions: |
| 327 | * |
| 328 | * dynamic: instruction = enabled ^ branch |
| 329 | * static: instruction = type ^ branch |
| 330 | * |
| 331 | * See jump_label_type() / jump_label_init_type(). |
| 332 | */ |
| 333 | |
Peter Zijlstra | 11276d5 | 2015-07-24 15:09:55 +0200 | [diff] [blame] | 334 | #define static_branch_likely(x) \ |
| 335 | ({ \ |
| 336 | bool branch; \ |
| 337 | if (__builtin_types_compatible_p(typeof(*x), struct static_key_true)) \ |
| 338 | branch = !arch_static_branch(&(x)->key, true); \ |
| 339 | else if (__builtin_types_compatible_p(typeof(*x), struct static_key_false)) \ |
| 340 | branch = !arch_static_branch_jump(&(x)->key, true); \ |
| 341 | else \ |
| 342 | branch = ____wrong_branch_error(); \ |
| 343 | branch; \ |
| 344 | }) |
| 345 | |
| 346 | #define static_branch_unlikely(x) \ |
| 347 | ({ \ |
| 348 | bool branch; \ |
| 349 | if (__builtin_types_compatible_p(typeof(*x), struct static_key_true)) \ |
| 350 | branch = arch_static_branch_jump(&(x)->key, false); \ |
| 351 | else if (__builtin_types_compatible_p(typeof(*x), struct static_key_false)) \ |
| 352 | branch = arch_static_branch(&(x)->key, false); \ |
| 353 | else \ |
| 354 | branch = ____wrong_branch_error(); \ |
| 355 | branch; \ |
| 356 | }) |
| 357 | |
| 358 | #else /* !HAVE_JUMP_LABEL */ |
| 359 | |
| 360 | #define static_branch_likely(x) likely(static_key_enabled(&(x)->key)) |
| 361 | #define static_branch_unlikely(x) unlikely(static_key_enabled(&(x)->key)) |
| 362 | |
| 363 | #endif /* HAVE_JUMP_LABEL */ |
| 364 | |
| 365 | /* |
| 366 | * Advanced usage; refcount, branch is enabled when: count != 0 |
| 367 | */ |
| 368 | |
| 369 | #define static_branch_inc(x) static_key_slow_inc(&(x)->key) |
| 370 | #define static_branch_dec(x) static_key_slow_dec(&(x)->key) |
| 371 | |
| 372 | /* |
| 373 | * Normal usage; boolean enable/disable. |
| 374 | */ |
| 375 | |
| 376 | #define static_branch_enable(x) static_key_enable(&(x)->key) |
| 377 | #define static_branch_disable(x) static_key_disable(&(x)->key) |
| 378 | |
Jeremy Fitzhardinge | 97ce2c8 | 2011-10-12 16:17:54 -0700 | [diff] [blame] | 379 | #endif /* _LINUX_JUMP_LABEL_H */ |
Anton Blanchard | c0ccf6f | 2015-04-09 13:51:31 +1000 | [diff] [blame] | 380 | |
| 381 | #endif /* __ASSEMBLY__ */ |