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> |
Peter Zijlstra | 90eec10 | 2015-11-16 11:08:45 +0100 | [diff] [blame] | 8 | * Copyright (C) 2011-2012 Red Hat, Inc., Peter Zijlstra |
Peter Zijlstra | efb3040 | 2012-01-26 13:32:15 +0100 | [diff] [blame] | 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); |
Catalin Marinas | ef0da55 | 2016-09-05 18:25:47 +0100 | [diff] [blame] | 24 | * DEFINE_STATIC_KEY_ARRAY_TRUE(keys, count); |
| 25 | * DEFINE_STATIC_KEY_ARRAY_FALSE(keys, count); |
Jonathan Corbet | 1975dbc | 2015-09-14 17:11:05 -0600 | [diff] [blame] | 26 | * static_branch_likely() |
| 27 | * static_branch_unlikely() |
Jason Baron | 412758c | 2015-07-30 03:59:48 +0000 | [diff] [blame] | 28 | * |
Peter Zijlstra | efb3040 | 2012-01-26 13:32:15 +0100 | [diff] [blame] | 29 | * Jump labels provide an interface to generate dynamic branches using |
Jason Baron | 412758c | 2015-07-30 03:59:48 +0000 | [diff] [blame] | 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. |
Peter Zijlstra | efb3040 | 2012-01-26 13:32:15 +0100 | [diff] [blame] | 40 | * |
Jason Baron | 412758c | 2015-07-30 03:59:48 +0000 | [diff] [blame] | 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. |
Peter Zijlstra | efb3040 | 2012-01-26 13:32:15 +0100 | [diff] [blame] | 49 | * |
Jonathan Corbet | 1975dbc | 2015-09-14 17:11:05 -0600 | [diff] [blame] | 50 | * In addition to static_branch_{enable,disable}, we can also reference count |
Jason Baron | 412758c | 2015-07-30 03:59:48 +0000 | [diff] [blame] | 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 |
Jonathan Corbet | 1975dbc | 2015-09-14 17:11:05 -0600 | [diff] [blame] | 53 | * static_branch_dec() as a 'make more false'. |
Jason Baron | 412758c | 2015-07-30 03:59:48 +0000 | [diff] [blame] | 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 | |
| 82 | extern 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 Baron | d430d3d | 2011-03-16 17:29:47 -0400 | [diff] [blame] | 87 | |
Anton Blanchard | c0ccf6f | 2015-04-09 13:51:31 +1000 | [diff] [blame] | 88 | #ifdef HAVE_JUMP_LABEL |
Jason Baron | d430d3d | 2011-03-16 17:29:47 -0400 | [diff] [blame] | 89 | |
Ingo Molnar | c5905af | 2012-02-24 08:31:31 +0100 | [diff] [blame] | 90 | struct static_key { |
Jason Baron | d430d3d | 2011-03-16 17:29:47 -0400 | [diff] [blame] | 91 | atomic_t enabled; |
Ingo Molnar | c5905af | 2012-02-24 08:31:31 +0100 | [diff] [blame] | 92 | /* Set lsb bit to 1 if branch is default true, 0 ot */ |
Jason Baron | d430d3d | 2011-03-16 17:29:47 -0400 | [diff] [blame] | 93 | struct jump_entry *entries; |
| 94 | #ifdef CONFIG_MODULES |
Ingo Molnar | c5905af | 2012-02-24 08:31:31 +0100 | [diff] [blame] | 95 | struct static_key_mod *next; |
Jason Baron | d430d3d | 2011-03-16 17:29:47 -0400 | [diff] [blame] | 96 | #endif |
| 97 | }; |
| 98 | |
Mel Gorman | ea5e953 | 2014-06-04 16:10:07 -0700 | [diff] [blame] | 99 | #else |
| 100 | struct static_key { |
| 101 | atomic_t enabled; |
| 102 | }; |
Anton Blanchard | c0ccf6f | 2015-04-09 13:51:31 +1000 | [diff] [blame] | 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__ |
Jason Baron | bf5438fc | 2010-09-17 11:09:00 -0400 | [diff] [blame] | 111 | |
| 112 | enum jump_label_type { |
Peter Zijlstra | 76b235c | 2015-07-24 14:45:44 +0200 | [diff] [blame] | 113 | JUMP_LABEL_NOP = 0, |
| 114 | JUMP_LABEL_JMP, |
Jason Baron | bf5438fc | 2010-09-17 11:09:00 -0400 | [diff] [blame] | 115 | }; |
| 116 | |
| 117 | struct module; |
| 118 | |
Paolo Bonzini | 4c5ea0a | 2016-06-21 18:52:17 +0200 | [diff] [blame] | 119 | #ifdef HAVE_JUMP_LABEL |
| 120 | |
Peter Zijlstra | a1efb01 | 2015-07-24 14:55:40 +0200 | [diff] [blame] | 121 | #define JUMP_TYPE_FALSE 0UL |
| 122 | #define JUMP_TYPE_TRUE 1UL |
| 123 | #define JUMP_TYPE_MASK 1UL |
Ingo Molnar | c5905af | 2012-02-24 08:31:31 +0100 | [diff] [blame] | 124 | |
| 125 | static __always_inline bool static_key_false(struct static_key *key) |
| 126 | { |
Peter Zijlstra | 11276d5 | 2015-07-24 15:09:55 +0200 | [diff] [blame] | 127 | return arch_static_branch(key, false); |
Ingo Molnar | c5905af | 2012-02-24 08:31:31 +0100 | [diff] [blame] | 128 | } |
| 129 | |
| 130 | static __always_inline bool static_key_true(struct static_key *key) |
| 131 | { |
Peter Zijlstra | 11276d5 | 2015-07-24 15:09:55 +0200 | [diff] [blame] | 132 | return !arch_static_branch(key, true); |
Ingo Molnar | c5905af | 2012-02-24 08:31:31 +0100 | [diff] [blame] | 133 | } |
| 134 | |
Jason Baron | bf5438fc | 2010-09-17 11:09:00 -0400 | [diff] [blame] | 135 | extern struct jump_entry __start___jump_table[]; |
| 136 | extern struct jump_entry __stop___jump_table[]; |
| 137 | |
Jeremy Fitzhardinge | 97ce2c8 | 2011-10-12 16:17:54 -0700 | [diff] [blame] | 138 | extern void jump_label_init(void); |
Jason Baron | 91bad2f | 2010-10-01 17:23:48 -0400 | [diff] [blame] | 139 | extern void jump_label_lock(void); |
| 140 | extern void jump_label_unlock(void); |
Jason Baron | bf5438fc | 2010-09-17 11:09:00 -0400 | [diff] [blame] | 141 | extern void arch_jump_label_transform(struct jump_entry *entry, |
Jeremy Fitzhardinge | 3734880 | 2011-09-29 11:10:05 -0700 | [diff] [blame] | 142 | enum jump_label_type type); |
Jeremy Fitzhardinge | 20284aa | 2011-10-03 11:01:46 -0700 | [diff] [blame] | 143 | extern void arch_jump_label_transform_static(struct jump_entry *entry, |
| 144 | enum jump_label_type type); |
Jason Baron | 4c3ef6d | 2010-09-17 11:09:08 -0400 | [diff] [blame] | 145 | extern int jump_label_text_reserved(void *start, void *end); |
Ingo Molnar | c5905af | 2012-02-24 08:31:31 +0100 | [diff] [blame] | 146 | extern void static_key_slow_inc(struct static_key *key); |
| 147 | extern void static_key_slow_dec(struct static_key *key); |
Jason Baron | d430d3d | 2011-03-16 17:29:47 -0400 | [diff] [blame] | 148 | extern void jump_label_apply_nops(struct module *mod); |
Jason Baron | 1f69bf9 | 2016-08-03 13:46:36 -0700 | [diff] [blame] | 149 | extern int static_key_count(struct static_key *key); |
| 150 | extern void static_key_enable(struct static_key *key); |
| 151 | extern void static_key_disable(struct static_key *key); |
Ingo Molnar | c5905af | 2012-02-24 08:31:31 +0100 | [diff] [blame] | 152 | |
Jason Baron | 1f69bf9 | 2016-08-03 13:46:36 -0700 | [diff] [blame] | 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 | */ |
Peter Zijlstra | 11276d5 | 2015-07-24 15:09:55 +0200 | [diff] [blame] | 160 | #define STATIC_KEY_INIT_TRUE \ |
Jason Baron | 1f69bf9 | 2016-08-03 13:46:36 -0700 | [diff] [blame] | 161 | { .enabled = { 1 }, \ |
Peter Zijlstra | 11276d5 | 2015-07-24 15:09:55 +0200 | [diff] [blame] | 162 | .entries = (void *)JUMP_TYPE_TRUE } |
| 163 | #define STATIC_KEY_INIT_FALSE \ |
Jason Baron | 1f69bf9 | 2016-08-03 13:46:36 -0700 | [diff] [blame] | 164 | { .enabled = { 0 }, \ |
Peter Zijlstra | 11276d5 | 2015-07-24 15:09:55 +0200 | [diff] [blame] | 165 | .entries = (void *)JUMP_TYPE_FALSE } |
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 | #else /* !HAVE_JUMP_LABEL */ |
Jason Baron | bf5438fc | 2010-09-17 11:09:00 -0400 | [diff] [blame] | 168 | |
Jason Baron | 1f69bf9 | 2016-08-03 13:46:36 -0700 | [diff] [blame] | 169 | #include <linux/atomic.h> |
| 170 | #include <linux/bug.h> |
| 171 | |
Paolo Bonzini | 4c5ea0a | 2016-06-21 18:52:17 +0200 | [diff] [blame] | 172 | static inline int static_key_count(struct static_key *key) |
| 173 | { |
| 174 | return atomic_read(&key->enabled); |
| 175 | } |
| 176 | |
Jeremy Fitzhardinge | 97ce2c8 | 2011-10-12 16:17:54 -0700 | [diff] [blame] | 177 | static __always_inline void jump_label_init(void) |
| 178 | { |
Hannes Frederic Sowa | c4b2c0c | 2013-10-19 21:48:53 +0200 | [diff] [blame] | 179 | static_key_initialized = true; |
Jeremy Fitzhardinge | 97ce2c8 | 2011-10-12 16:17:54 -0700 | [diff] [blame] | 180 | } |
| 181 | |
Ingo Molnar | c5905af | 2012-02-24 08:31:31 +0100 | [diff] [blame] | 182 | static __always_inline bool static_key_false(struct static_key *key) |
Jason Baron | bf5438fc | 2010-09-17 11:09:00 -0400 | [diff] [blame] | 183 | { |
Mel Gorman | ea5e953 | 2014-06-04 16:10:07 -0700 | [diff] [blame] | 184 | if (unlikely(static_key_count(key) > 0)) |
Jason Baron | d430d3d | 2011-03-16 17:29:47 -0400 | [diff] [blame] | 185 | return true; |
| 186 | return false; |
| 187 | } |
| 188 | |
Ingo Molnar | c5905af | 2012-02-24 08:31:31 +0100 | [diff] [blame] | 189 | static __always_inline bool static_key_true(struct static_key *key) |
| 190 | { |
Mel Gorman | ea5e953 | 2014-06-04 16:10:07 -0700 | [diff] [blame] | 191 | if (likely(static_key_count(key) > 0)) |
Ingo Molnar | c5905af | 2012-02-24 08:31:31 +0100 | [diff] [blame] | 192 | return true; |
| 193 | return false; |
| 194 | } |
| 195 | |
Ingo Molnar | c5905af | 2012-02-24 08:31:31 +0100 | [diff] [blame] | 196 | static inline void static_key_slow_inc(struct static_key *key) |
Jason Baron | d430d3d | 2011-03-16 17:29:47 -0400 | [diff] [blame] | 197 | { |
Hannes Frederic Sowa | c4b2c0c | 2013-10-19 21:48:53 +0200 | [diff] [blame] | 198 | STATIC_KEY_CHECK_USE(); |
Jason Baron | d430d3d | 2011-03-16 17:29:47 -0400 | [diff] [blame] | 199 | atomic_inc(&key->enabled); |
| 200 | } |
| 201 | |
Ingo Molnar | c5905af | 2012-02-24 08:31:31 +0100 | [diff] [blame] | 202 | static inline void static_key_slow_dec(struct static_key *key) |
Jason Baron | d430d3d | 2011-03-16 17:29:47 -0400 | [diff] [blame] | 203 | { |
Hannes Frederic Sowa | c4b2c0c | 2013-10-19 21:48:53 +0200 | [diff] [blame] | 204 | STATIC_KEY_CHECK_USE(); |
Jason Baron | d430d3d | 2011-03-16 17:29:47 -0400 | [diff] [blame] | 205 | atomic_dec(&key->enabled); |
Jason Baron | bf5438fc | 2010-09-17 11:09:00 -0400 | [diff] [blame] | 206 | } |
| 207 | |
Jason Baron | 4c3ef6d | 2010-09-17 11:09:08 -0400 | [diff] [blame] | 208 | static inline int jump_label_text_reserved(void *start, void *end) |
| 209 | { |
| 210 | return 0; |
| 211 | } |
| 212 | |
Jason Baron | 91bad2f | 2010-10-01 17:23:48 -0400 | [diff] [blame] | 213 | static inline void jump_label_lock(void) {} |
| 214 | static inline void jump_label_unlock(void) {} |
| 215 | |
Jason Baron | d430d3d | 2011-03-16 17:29:47 -0400 | [diff] [blame] | 216 | static inline int jump_label_apply_nops(struct module *mod) |
| 217 | { |
| 218 | return 0; |
| 219 | } |
Gleb Natapov | b202952 | 2011-11-27 17:59:09 +0200 | [diff] [blame] | 220 | |
Peter Zijlstra | e33886b | 2015-07-24 15:03:40 +0200 | [diff] [blame] | 221 | static 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 | |
| 231 | static 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 | |
Jason Baron | 1f69bf9 | 2016-08-03 13:46:36 -0700 | [diff] [blame] | 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 | |
Peter Zijlstra | 11276d5 | 2015-07-24 15:09:55 +0200 | [diff] [blame] | 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 | |
| 258 | struct static_key_true { |
| 259 | struct static_key key; |
| 260 | }; |
| 261 | |
| 262 | struct 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 | |
Tony Luck | b8fb037 | 2016-09-01 11:39:33 -0700 | [diff] [blame] | 272 | #define DECLARE_STATIC_KEY_TRUE(name) \ |
| 273 | extern struct static_key_true name |
| 274 | |
Peter Zijlstra | 11276d5 | 2015-07-24 15:09:55 +0200 | [diff] [blame] | 275 | #define DEFINE_STATIC_KEY_FALSE(name) \ |
| 276 | struct static_key_false name = STATIC_KEY_FALSE_INIT |
| 277 | |
Tony Luck | b8fb037 | 2016-09-01 11:39:33 -0700 | [diff] [blame] | 278 | #define DECLARE_STATIC_KEY_FALSE(name) \ |
| 279 | extern struct static_key_false name |
| 280 | |
Catalin Marinas | ef0da55 | 2016-09-05 18:25:47 +0100 | [diff] [blame] | 281 | #define DEFINE_STATIC_KEY_ARRAY_TRUE(name, count) \ |
| 282 | struct static_key_true name[count] = { \ |
| 283 | [0 ... (count) - 1] = STATIC_KEY_TRUE_INIT, \ |
| 284 | } |
| 285 | |
| 286 | #define DEFINE_STATIC_KEY_ARRAY_FALSE(name, count) \ |
| 287 | struct static_key_false name[count] = { \ |
| 288 | [0 ... (count) - 1] = STATIC_KEY_FALSE_INIT, \ |
| 289 | } |
| 290 | |
Tejun Heo | fa128fd | 2015-09-18 11:56:28 -0400 | [diff] [blame] | 291 | extern bool ____wrong_branch_error(void); |
| 292 | |
| 293 | #define static_key_enabled(x) \ |
| 294 | ({ \ |
| 295 | if (!__builtin_types_compatible_p(typeof(*x), struct static_key) && \ |
| 296 | !__builtin_types_compatible_p(typeof(*x), struct static_key_true) &&\ |
| 297 | !__builtin_types_compatible_p(typeof(*x), struct static_key_false)) \ |
| 298 | ____wrong_branch_error(); \ |
| 299 | static_key_count((struct static_key *)x) > 0; \ |
| 300 | }) |
| 301 | |
Peter Zijlstra | 11276d5 | 2015-07-24 15:09:55 +0200 | [diff] [blame] | 302 | #ifdef HAVE_JUMP_LABEL |
| 303 | |
| 304 | /* |
| 305 | * Combine the right initial value (type) with the right branch order |
| 306 | * to generate the desired result. |
| 307 | * |
| 308 | * |
| 309 | * type\branch| likely (1) | unlikely (0) |
| 310 | * -----------+-----------------------+------------------ |
| 311 | * | | |
| 312 | * true (1) | ... | ... |
| 313 | * | NOP | JMP L |
| 314 | * | <br-stmts> | 1: ... |
| 315 | * | L: ... | |
| 316 | * | | |
| 317 | * | | L: <br-stmts> |
| 318 | * | | jmp 1b |
| 319 | * | | |
| 320 | * -----------+-----------------------+------------------ |
| 321 | * | | |
| 322 | * false (0) | ... | ... |
| 323 | * | JMP L | NOP |
| 324 | * | <br-stmts> | 1: ... |
| 325 | * | L: ... | |
| 326 | * | | |
| 327 | * | | L: <br-stmts> |
| 328 | * | | jmp 1b |
| 329 | * | | |
| 330 | * -----------+-----------------------+------------------ |
| 331 | * |
| 332 | * The initial value is encoded in the LSB of static_key::entries, |
| 333 | * type: 0 = false, 1 = true. |
| 334 | * |
| 335 | * The branch type is encoded in the LSB of jump_entry::key, |
| 336 | * branch: 0 = unlikely, 1 = likely. |
| 337 | * |
| 338 | * This gives the following logic table: |
| 339 | * |
| 340 | * enabled type branch instuction |
| 341 | * -----------------------------+----------- |
| 342 | * 0 0 0 | NOP |
| 343 | * 0 0 1 | JMP |
| 344 | * 0 1 0 | NOP |
| 345 | * 0 1 1 | JMP |
| 346 | * |
| 347 | * 1 0 0 | JMP |
| 348 | * 1 0 1 | NOP |
| 349 | * 1 1 0 | JMP |
| 350 | * 1 1 1 | NOP |
| 351 | * |
| 352 | * Which gives the following functions: |
| 353 | * |
| 354 | * dynamic: instruction = enabled ^ branch |
| 355 | * static: instruction = type ^ branch |
| 356 | * |
| 357 | * See jump_label_type() / jump_label_init_type(). |
| 358 | */ |
| 359 | |
Peter Zijlstra | 11276d5 | 2015-07-24 15:09:55 +0200 | [diff] [blame] | 360 | #define static_branch_likely(x) \ |
| 361 | ({ \ |
| 362 | bool branch; \ |
| 363 | if (__builtin_types_compatible_p(typeof(*x), struct static_key_true)) \ |
| 364 | branch = !arch_static_branch(&(x)->key, true); \ |
| 365 | else if (__builtin_types_compatible_p(typeof(*x), struct static_key_false)) \ |
| 366 | branch = !arch_static_branch_jump(&(x)->key, true); \ |
| 367 | else \ |
| 368 | branch = ____wrong_branch_error(); \ |
| 369 | branch; \ |
| 370 | }) |
| 371 | |
| 372 | #define static_branch_unlikely(x) \ |
| 373 | ({ \ |
| 374 | bool branch; \ |
| 375 | if (__builtin_types_compatible_p(typeof(*x), struct static_key_true)) \ |
| 376 | branch = arch_static_branch_jump(&(x)->key, false); \ |
| 377 | else if (__builtin_types_compatible_p(typeof(*x), struct static_key_false)) \ |
| 378 | branch = arch_static_branch(&(x)->key, false); \ |
| 379 | else \ |
| 380 | branch = ____wrong_branch_error(); \ |
| 381 | branch; \ |
| 382 | }) |
| 383 | |
| 384 | #else /* !HAVE_JUMP_LABEL */ |
| 385 | |
| 386 | #define static_branch_likely(x) likely(static_key_enabled(&(x)->key)) |
| 387 | #define static_branch_unlikely(x) unlikely(static_key_enabled(&(x)->key)) |
| 388 | |
| 389 | #endif /* HAVE_JUMP_LABEL */ |
| 390 | |
| 391 | /* |
| 392 | * Advanced usage; refcount, branch is enabled when: count != 0 |
| 393 | */ |
| 394 | |
| 395 | #define static_branch_inc(x) static_key_slow_inc(&(x)->key) |
| 396 | #define static_branch_dec(x) static_key_slow_dec(&(x)->key) |
| 397 | |
| 398 | /* |
| 399 | * Normal usage; boolean enable/disable. |
| 400 | */ |
| 401 | |
| 402 | #define static_branch_enable(x) static_key_enable(&(x)->key) |
| 403 | #define static_branch_disable(x) static_key_disable(&(x)->key) |
| 404 | |
Jeremy Fitzhardinge | 97ce2c8 | 2011-10-12 16:17:54 -0700 | [diff] [blame] | 405 | #endif /* _LINUX_JUMP_LABEL_H */ |
Anton Blanchard | c0ccf6f | 2015-04-09 13:51:31 +1000 | [diff] [blame] | 406 | |
| 407 | #endif /* __ASSEMBLY__ */ |