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 void jump_label_apply_nops(struct module *mod);
  114. extern void
  115. jump_label_rate_limit(struct static_key_deferred *key, unsigned long rl);
  116. #define STATIC_KEY_INIT_TRUE ((struct static_key) \
  117. { .enabled = ATOMIC_INIT(1), .entries = (void *)1 })
  118. #define STATIC_KEY_INIT_FALSE ((struct static_key) \
  119. { .enabled = ATOMIC_INIT(0), .entries = (void *)0 })
  120. #else /* !HAVE_JUMP_LABEL */
  121. #include <linux/atomic.h>
  122. struct static_key {
  123. atomic_t enabled;
  124. };
  125. static __always_inline void jump_label_init(void)
  126. {
  127. }
  128. struct static_key_deferred {
  129. struct static_key key;
  130. };
  131. static __always_inline bool static_key_false(struct static_key *key)
  132. {
  133. if (unlikely(atomic_read(&key->enabled)) > 0)
  134. return true;
  135. return false;
  136. }
  137. static __always_inline bool static_key_true(struct static_key *key)
  138. {
  139. if (likely(atomic_read(&key->enabled)) > 0)
  140. return true;
  141. return false;
  142. }
  143. /* Deprecated. Please use 'static_key_false() instead. */
  144. static __always_inline bool static_branch(struct static_key *key)
  145. {
  146. if (unlikely(atomic_read(&key->enabled)) > 0)
  147. return true;
  148. return false;
  149. }
  150. static inline void static_key_slow_inc(struct static_key *key)
  151. {
  152. atomic_inc(&key->enabled);
  153. }
  154. static inline void static_key_slow_dec(struct static_key *key)
  155. {
  156. atomic_dec(&key->enabled);
  157. }
  158. static inline void static_key_slow_dec_deferred(struct static_key_deferred *key)
  159. {
  160. static_key_slow_dec(&key->key);
  161. }
  162. static inline int jump_label_text_reserved(void *start, void *end)
  163. {
  164. return 0;
  165. }
  166. static inline void jump_label_lock(void) {}
  167. static inline void jump_label_unlock(void) {}
  168. static inline int jump_label_apply_nops(struct module *mod)
  169. {
  170. return 0;
  171. }
  172. static inline void
  173. jump_label_rate_limit(struct static_key_deferred *key,
  174. unsigned long rl)
  175. {
  176. }
  177. #define STATIC_KEY_INIT_TRUE ((struct static_key) \
  178. { .enabled = ATOMIC_INIT(1) })
  179. #define STATIC_KEY_INIT_FALSE ((struct static_key) \
  180. { .enabled = ATOMIC_INIT(0) })
  181. #endif /* HAVE_JUMP_LABEL */
  182. #define STATIC_KEY_INIT STATIC_KEY_INIT_FALSE
  183. #define jump_label_enabled static_key_enabled
  184. static inline bool static_key_enabled(struct static_key *key)
  185. {
  186. return (atomic_read(&key->enabled) > 0);
  187. }
  188. #endif /* _LINUX_JUMP_LABEL_H */