~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

TOMOYO Linux Cross Reference
Linux/include/linux/jump_label.h

Version: ~ [ linux-5.15-rc6 ] ~ [ linux-5.14.14 ] ~ [ linux-5.13.19 ] ~ [ linux-5.12.19 ] ~ [ linux-5.11.22 ] ~ [ linux-5.10.75 ] ~ [ linux-5.9.16 ] ~ [ linux-5.8.18 ] ~ [ linux-5.7.19 ] ~ [ linux-5.6.19 ] ~ [ linux-5.5.19 ] ~ [ linux-5.4.155 ] ~ [ linux-5.3.18 ] ~ [ linux-5.2.21 ] ~ [ linux-5.1.21 ] ~ [ linux-5.0.21 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.213 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.252 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.287 ] ~ [ linux-4.8.17 ] ~ [ linux-4.7.10 ] ~ [ linux-4.6.7 ] ~ [ linux-4.5.7 ] ~ [ linux-4.4.289 ] ~ [ linux-4.3.6 ] ~ [ linux-4.2.8 ] ~ [ linux-4.1.52 ] ~ [ linux-4.0.9 ] ~ [ linux-3.18.140 ] ~ [ linux-3.16.85 ] ~ [ linux-3.14.79 ] ~ [ linux-3.12.74 ] ~ [ linux-3.10.108 ] ~ [ linux-2.6.32.71 ] ~ [ linux-2.6.0 ] ~ [ linux-2.4.37.11 ] ~ [ unix-v6-master ] ~ [ ccs-tools-1.8.5 ] ~ [ policy-sample ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

  1 #ifndef _LINUX_JUMP_LABEL_H
  2 #define _LINUX_JUMP_LABEL_H
  3 
  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  *
 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_branch_likely()
 25  * static_branch_unlikely()
 26  *
 27  * Jump labels provide an interface to generate dynamic branches using
 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.
 38  *
 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.
 47  *
 48  * In addition 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'.
 52  *
 53  * Since this relies on modifying code, the branch modifying functions
 54  * must be considered absolute slow paths (machine wide synchronization etc.).
 55  * OTOH, since the affected branches are unconditional, their runtime overhead
 56  * 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  *
 60  * When the control is directly exposed to userspace, it is prudent to delay the
 61  * decrement to avoid high frequency code modifications which can (and do)
 62  * cause significant performance degradation. Struct static_key_deferred and
 63  * static_key_slow_dec_deferred() provide for this.
 64  *
 65  * Lacking toolchain and or architecture support, static keys fall back to a
 66  * simple conditional branch.
 67  *
 68  * Additional babbling in: Documentation/static-keys.txt
 69  */
 70 
 71 #if defined(CC_HAVE_ASM_GOTO) && defined(CONFIG_JUMP_LABEL)
 72 # define HAVE_JUMP_LABEL
 73 #endif
 74 
 75 #ifndef __ASSEMBLY__
 76 
 77 #include <linux/types.h>
 78 #include <linux/compiler.h>
 79 #include <linux/bug.h>
 80 
 81 extern bool static_key_initialized;
 82 
 83 #define STATIC_KEY_CHECK_USE() WARN(!static_key_initialized,                  \
 84                                     "%s used before call to jump_label_init", \
 85                                     __func__)
 86 
 87 #ifdef HAVE_JUMP_LABEL
 88 
 89 struct static_key {
 90         atomic_t enabled;
 91 /* Set lsb bit to 1 if branch is default true, 0 ot */
 92         struct jump_entry *entries;
 93 #ifdef CONFIG_MODULES
 94         struct static_key_mod *next;
 95 #endif
 96 };
 97 
 98 #else
 99 struct static_key {
100         atomic_t enabled;
101 };
102 #endif  /* HAVE_JUMP_LABEL */
103 #endif /* __ASSEMBLY__ */
104 
105 #ifdef HAVE_JUMP_LABEL
106 #include <asm/jump_label.h>
107 #endif
108 
109 #ifndef __ASSEMBLY__
110 
111 enum jump_label_type {
112         JUMP_LABEL_NOP = 0,
113         JUMP_LABEL_JMP,
114 };
115 
116 struct module;
117 
118 #include <linux/atomic.h>
119 
120 #ifdef HAVE_JUMP_LABEL
121 
122 static inline int static_key_count(struct static_key *key)
123 {
124         /*
125          * -1 means the first static_key_slow_inc() is in progress.
126          *  static_key_enabled() must return true, so return 1 here.
127          */
128         int n = atomic_read(&key->enabled);
129         return n >= 0 ? n : 1;
130 }
131 
132 #define JUMP_TYPE_FALSE 0UL
133 #define JUMP_TYPE_TRUE  1UL
134 #define JUMP_TYPE_MASK  1UL
135 
136 static __always_inline bool static_key_false(struct static_key *key)
137 {
138         return arch_static_branch(key, false);
139 }
140 
141 static __always_inline bool static_key_true(struct static_key *key)
142 {
143         return !arch_static_branch(key, true);
144 }
145 
146 extern struct jump_entry __start___jump_table[];
147 extern struct jump_entry __stop___jump_table[];
148 
149 extern void jump_label_init(void);
150 extern void jump_label_lock(void);
151 extern void jump_label_unlock(void);
152 extern void arch_jump_label_transform(struct jump_entry *entry,
153                                       enum jump_label_type type);
154 extern void arch_jump_label_transform_static(struct jump_entry *entry,
155                                              enum jump_label_type type);
156 extern int jump_label_text_reserved(void *start, void *end);
157 extern void static_key_slow_inc(struct static_key *key);
158 extern void static_key_slow_dec(struct static_key *key);
159 extern void jump_label_apply_nops(struct module *mod);
160 
161 #define STATIC_KEY_INIT_TRUE                                    \
162         { .enabled = ATOMIC_INIT(1),                            \
163           .entries = (void *)JUMP_TYPE_TRUE }
164 #define STATIC_KEY_INIT_FALSE                                   \
165         { .enabled = ATOMIC_INIT(0),                            \
166           .entries = (void *)JUMP_TYPE_FALSE }
167 
168 #else  /* !HAVE_JUMP_LABEL */
169 
170 static inline int static_key_count(struct static_key *key)
171 {
172         return atomic_read(&key->enabled);
173 }
174 
175 static __always_inline void jump_label_init(void)
176 {
177         static_key_initialized = true;
178 }
179 
180 static __always_inline bool static_key_false(struct static_key *key)
181 {
182         if (unlikely(static_key_count(key) > 0))
183                 return true;
184         return false;
185 }
186 
187 static __always_inline bool static_key_true(struct static_key *key)
188 {
189         if (likely(static_key_count(key) > 0))
190                 return true;
191         return false;
192 }
193 
194 static inline void static_key_slow_inc(struct static_key *key)
195 {
196         STATIC_KEY_CHECK_USE();
197         atomic_inc(&key->enabled);
198 }
199 
200 static inline void static_key_slow_dec(struct static_key *key)
201 {
202         STATIC_KEY_CHECK_USE();
203         atomic_dec(&key->enabled);
204 }
205 
206 static inline int jump_label_text_reserved(void *start, void *end)
207 {
208         return 0;
209 }
210 
211 static inline void jump_label_lock(void) {}
212 static inline void jump_label_unlock(void) {}
213 
214 static inline int jump_label_apply_nops(struct module *mod)
215 {
216         return 0;
217 }
218 
219 #define STATIC_KEY_INIT_TRUE    { .enabled = ATOMIC_INIT(1) }
220 #define STATIC_KEY_INIT_FALSE   { .enabled = ATOMIC_INIT(0) }
221 
222 #endif  /* HAVE_JUMP_LABEL */
223 
224 #define STATIC_KEY_INIT STATIC_KEY_INIT_FALSE
225 #define jump_label_enabled static_key_enabled
226 
227 static inline void static_key_enable(struct static_key *key)
228 {
229         int count = static_key_count(key);
230 
231         WARN_ON_ONCE(count < 0 || count > 1);
232 
233         if (!count)
234                 static_key_slow_inc(key);
235 }
236 
237 static inline void static_key_disable(struct static_key *key)
238 {
239         int count = static_key_count(key);
240 
241         WARN_ON_ONCE(count < 0 || count > 1);
242 
243         if (count)
244                 static_key_slow_dec(key);
245 }
246 
247 /* -------------------------------------------------------------------------- */
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 
256 struct static_key_true {
257         struct static_key key;
258 };
259 
260 struct 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 DECLARE_STATIC_KEY_TRUE(name)   \
271         extern struct static_key_true name
272 
273 #define DEFINE_STATIC_KEY_FALSE(name)   \
274         struct static_key_false name = STATIC_KEY_FALSE_INIT
275 
276 #define DECLARE_STATIC_KEY_FALSE(name)  \
277         extern struct static_key_false name
278 
279 extern bool ____wrong_branch_error(void);
280 
281 #define static_key_enabled(x)                                                   \
282 ({                                                                              \
283         if (!__builtin_types_compatible_p(typeof(*x), struct static_key) &&     \
284             !__builtin_types_compatible_p(typeof(*x), struct static_key_true) &&\
285             !__builtin_types_compatible_p(typeof(*x), struct static_key_false)) \
286                 ____wrong_branch_error();                                       \
287         static_key_count((struct static_key *)x) > 0;                           \
288 })
289 
290 #ifdef HAVE_JUMP_LABEL
291 
292 /*
293  * Combine the right initial value (type) with the right branch order
294  * to generate the desired result.
295  *
296  *
297  * type\branch| likely (1)            | unlikely (0)
298  * -----------+-----------------------+------------------
299  *            |                       |
300  *  true (1)  |    ...                |    ...
301  *            |    NOP                |    JMP L
302  *            |    <br-stmts>         | 1: ...
303  *            | L: ...                |
304  *            |                       |
305  *            |                       | L: <br-stmts>
306  *            |                       |    jmp 1b
307  *            |                       |
308  * -----------+-----------------------+------------------
309  *            |                       |
310  *  false (0) |    ...                |    ...
311  *            |    JMP L              |    NOP
312  *            |    <br-stmts>         | 1: ...
313  *            | L: ...                |
314  *            |                       |
315  *            |                       | L: <br-stmts>
316  *            |                       |    jmp 1b
317  *            |                       |
318  * -----------+-----------------------+------------------
319  *
320  * The initial value is encoded in the LSB of static_key::entries,
321  * type: 0 = false, 1 = true.
322  *
323  * The branch type is encoded in the LSB of jump_entry::key,
324  * branch: 0 = unlikely, 1 = likely.
325  *
326  * This gives the following logic table:
327  *
328  *      enabled type    branch    instuction
329  * -----------------------------+-----------
330  *      0       0       0       | NOP
331  *      0       0       1       | JMP
332  *      0       1       0       | NOP
333  *      0       1       1       | JMP
334  *
335  *      1       0       0       | JMP
336  *      1       0       1       | NOP
337  *      1       1       0       | JMP
338  *      1       1       1       | NOP
339  *
340  * Which gives the following functions:
341  *
342  *   dynamic: instruction = enabled ^ branch
343  *   static:  instruction = type ^ branch
344  *
345  * See jump_label_type() / jump_label_init_type().
346  */
347 
348 #define static_branch_likely(x)                                                 \
349 ({                                                                              \
350         bool branch;                                                            \
351         if (__builtin_types_compatible_p(typeof(*x), struct static_key_true))   \
352                 branch = !arch_static_branch(&(x)->key, true);                  \
353         else if (__builtin_types_compatible_p(typeof(*x), struct static_key_false)) \
354                 branch = !arch_static_branch_jump(&(x)->key, true);             \
355         else                                                                    \
356                 branch = ____wrong_branch_error();                              \
357         branch;                                                                 \
358 })
359 
360 #define static_branch_unlikely(x)                                               \
361 ({                                                                              \
362         bool branch;                                                            \
363         if (__builtin_types_compatible_p(typeof(*x), struct static_key_true))   \
364                 branch = arch_static_branch_jump(&(x)->key, false);             \
365         else if (__builtin_types_compatible_p(typeof(*x), struct static_key_false)) \
366                 branch = arch_static_branch(&(x)->key, false);                  \
367         else                                                                    \
368                 branch = ____wrong_branch_error();                              \
369         branch;                                                                 \
370 })
371 
372 #else /* !HAVE_JUMP_LABEL */
373 
374 #define static_branch_likely(x)         likely(static_key_enabled(&(x)->key))
375 #define static_branch_unlikely(x)       unlikely(static_key_enabled(&(x)->key))
376 
377 #endif /* HAVE_JUMP_LABEL */
378 
379 /*
380  * Advanced usage; refcount, branch is enabled when: count != 0
381  */
382 
383 #define static_branch_inc(x)            static_key_slow_inc(&(x)->key)
384 #define static_branch_dec(x)            static_key_slow_dec(&(x)->key)
385 
386 /*
387  * Normal usage; boolean enable/disable.
388  */
389 
390 #define static_branch_enable(x)         static_key_enable(&(x)->key)
391 #define static_branch_disable(x)        static_key_disable(&(x)->key)
392 
393 #endif  /* _LINUX_JUMP_LABEL_H */
394 
395 #endif /* __ASSEMBLY__ */
396 

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

kernel.org | git.kernel.org | LWN.net | Project Home | Wiki (Japanese) | Wiki (English) | SVN repository | Mail admin

Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.

osdn.jp