jump_label.h 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. #ifndef _LINUX_JUMP_LABEL_H
  2. #define _LINUX_JUMP_LABEL_H
  3. /*
  4. * Jump label support
  5. *
  6. * Copyright (C) 2009-2012 Jason Baron <jbaron@redhat.com>
  7. * Copyright (C) 2011-2012 Peter Zijlstra <pzijlstr@redhat.com>
  8. *
  9. * Jump labels provide an interface to generate dynamic branches using
  10. * self-modifying code. Assuming toolchain and architecture support the result
  11. * of a "if (static_key_false(&key))" statement is a unconditional branch (which
  12. * defaults to false - and the true block is placed out of line).
  13. *
  14. * However at runtime we can change the branch target using
  15. * static_key_slow_{inc,dec}(). These function as a 'reference' count on the key
  16. * object and for as long as there are references all branches referring to
  17. * that particular key will point to the (out of line) true block.
  18. *
  19. * Since this relies on modifying code the static_key_slow_{inc,dec}() functions
  20. * must be considered absolute slow paths (machine wide synchronization etc.).
  21. * OTOH, since the affected branches are unconditional their runtime overhead
  22. * will be absolutely minimal, esp. in the default (off) case where the total
  23. * effect is a single NOP of appropriate size. The on case will patch in a jump
  24. * to the out-of-line block.
  25. *
  26. * When the control is directly exposed to userspace it is prudent to delay the
  27. * decrement to avoid high frequency code modifications which can (and do)
  28. * cause significant performance degradation. Struct static_key_deferred and
  29. * static_key_slow_dec_deferred() provide for this.
  30. *
  31. * Lacking toolchain and or architecture support, it falls back to a simple
  32. * conditional branch.
  33. *
  34. * struct static_key my_key = STATIC_KEY_INIT_TRUE;
  35. *
  36. * if (static_key_true(&my_key)) {
  37. * }
  38. *
  39. * will result in the true case being in-line and starts the key with a single
  40. * reference. Mixing static_key_true() and static_key_false() on the same key is not
  41. * allowed.
  42. *
  43. * Not initializing the key (static data is initialized to 0s anyway) is the
  44. * same as using STATIC_KEY_INIT_FALSE and static_key_false() is
  45. * equivalent with static_branch().
  46. *
  47. */
  48. #include <linux/types.h>
  49. #include <linux/compiler.h>
  50. #include <linux/workqueue.h>
  51. #if defined(CC_HAVE_ASM_GOTO) && defined(CONFIG_JUMP_LABEL)
  52. struct static_key {
  53. atomic_t enabled;
  54. /* Set lsb bit to 1 if branch is default true, 0 ot */
  55. struct jump_entry *entries;
  56. #ifdef CONFIG_MODULES
  57. struct static_key_mod *next;
  58. #endif
  59. };
  60. struct static_key_deferred {
  61. struct static_key key;
  62. unsigned long timeout;
  63. struct delayed_work work;
  64. };
  65. # include <asm/jump_label.h>
  66. # define HAVE_JUMP_LABEL
  67. #endif /* CC_HAVE_ASM_GOTO && CONFIG_JUMP_LABEL */
  68. enum jump_label_type {
  69. JUMP_LABEL_DISABLE = 0,
  70. JUMP_LABEL_ENABLE,
  71. };
  72. struct module;
  73. #ifdef HAVE_JUMP_LABEL
  74. #define JUMP_LABEL_TRUE_BRANCH 1UL
  75. static
  76. inline struct jump_entry *jump_label_get_entries(struct static_key *key)
  77. {
  78. return (struct jump_entry *)((unsigned long)key->entries
  79. & ~JUMP_LABEL_TRUE_BRANCH);
  80. }
  81. static inline bool jump_label_get_branch_default(struct static_key *key)
  82. {
  83. if ((unsigned long)key->entries & JUMP_LABEL_TRUE_BRANCH)
  84. return true;
  85. return false;
  86. }
  87. static __always_inline bool static_key_false(struct static_key *key)
  88. {
  89. return arch_static_branch(key);
  90. }
  91. static __always_inline bool static_key_true(struct static_key *key)
  92. {
  93. return !static_key_false(key);
  94. }
  95. /* Deprecated. Please use 'static_key_false() instead. */
  96. static __always_inline bool static_branch(struct static_key *key)
  97. {
  98. return arch_static_branch(key);
  99. }
  100. extern struct jump_entry __start___jump_table[];
  101. extern struct jump_entry __stop___jump_table[];
  102. extern void jump_label_init(void);
  103. extern void jump_label_lock(void);
  104. extern void jump_label_unlock(void);
  105. extern void arch_jump_label_transform(struct jump_entry *entry,
  106. enum jump_label_type type);
  107. extern void arch_jump_label_transform_static(struct jump_entry *entry,
  108. enum jump_label_type type);
  109. extern int jump_label_text_reserved(void *start, void *end);
  110. extern void static_key_slow_inc(struct static_key *key);
  111. extern void static_key_slow_dec(struct static_key *key);
  112. extern void static_key_slow_dec_deferred(struct static_key_deferred *key);
  113. extern bool static_key_enabled(struct static_key *key);
  114. extern void jump_label_apply_nops(struct module *mod);
  115. extern void
  116. jump_label_rate_limit(struct static_key_deferred *key, unsigned long rl);
  117. #define STATIC_KEY_INIT_TRUE ((struct static_key) \
  118. { .enabled = ATOMIC_INIT(1), .entries = (void *)1 })
  119. #define STATIC_KEY_INIT_FALSE ((struct static_key) \
  120. { .enabled = ATOMIC_INIT(0), .entries = (void *)0 })
  121. #else /* !HAVE_JUMP_LABEL */
  122. #include <linux/atomic.h>
  123. struct static_key {
  124. atomic_t enabled;
  125. };
  126. static __always_inline void jump_label_init(void)
  127. {
  128. }
  129. struct static_key_deferred {
  130. struct static_key key;
  131. };
  132. static __always_inline bool static_key_false(struct static_key *key)
  133. {
  134. if (unlikely(atomic_read(&key->enabled)) > 0)
  135. return true;
  136. return false;
  137. }
  138. static __always_inline bool static_key_true(struct static_key *key)
  139. {
  140. if (likely(atomic_read(&key->enabled)) > 0)
  141. return true;
  142. return false;
  143. }
  144. /* Deprecated. Please use 'static_key_false() instead. */
  145. static __always_inline bool static_branch(struct static_key *key)
  146. {
  147. if (unlikely(atomic_read(&key->enabled)) > 0)
  148. return true;
  149. return false;
  150. }
  151. static inline void static_key_slow_inc(struct static_key *key)
  152. {
  153. atomic_inc(&key->enabled);
  154. }
  155. static inline void static_key_slow_dec(struct static_key *key)
  156. {
  157. atomic_dec(&key->enabled);
  158. }
  159. static inline void static_key_slow_dec_deferred(struct static_key_deferred *key)
  160. {
  161. static_key_slow_dec(&key->key);
  162. }
  163. static inline int jump_label_text_reserved(void *start, void *end)
  164. {
  165. return 0;
  166. }
  167. static inline void jump_label_lock(void) {}
  168. static inline void jump_label_unlock(void) {}
  169. static inline bool static_key_enabled(struct static_key *key)
  170. {
  171. return (atomic_read(&key->enabled) > 0);
  172. }
  173. static inline int jump_label_apply_nops(struct module *mod)
  174. {
  175. return 0;
  176. }
  177. static inline void
  178. jump_label_rate_limit(struct static_key_deferred *key,
  179. unsigned long rl)
  180. {
  181. }
  182. #define STATIC_KEY_INIT_TRUE ((struct static_key) \
  183. { .enabled = ATOMIC_INIT(1) })
  184. #define STATIC_KEY_INIT_FALSE ((struct static_key) \
  185. { .enabled = ATOMIC_INIT(0) })
  186. #endif /* HAVE_JUMP_LABEL */
  187. #define STATIC_KEY_INIT STATIC_KEY_INIT_FALSE
  188. #define jump_label_enabled static_key_enabled
  189. #endif /* _LINUX_JUMP_LABEL_H */