jump_label.h 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  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 an 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, jump labels fall 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.
  45. */
  46. #include <linux/types.h>
  47. #include <linux/compiler.h>
  48. #include <linux/bug.h>
  49. extern bool static_key_initialized;
  50. #define STATIC_KEY_CHECK_USE() WARN(!static_key_initialized, \
  51. "%s used before call to jump_label_init", \
  52. __func__)
  53. #if defined(CC_HAVE_ASM_GOTO) && defined(CONFIG_JUMP_LABEL)
  54. struct static_key {
  55. atomic_t enabled;
  56. /* Set lsb bit to 1 if branch is default true, 0 ot */
  57. struct jump_entry *entries;
  58. #ifdef CONFIG_MODULES
  59. struct static_key_mod *next;
  60. #endif
  61. };
  62. # include <asm/jump_label.h>
  63. # define HAVE_JUMP_LABEL
  64. #else
  65. struct static_key {
  66. atomic_t enabled;
  67. };
  68. #endif /* CC_HAVE_ASM_GOTO && CONFIG_JUMP_LABEL */
  69. enum jump_label_type {
  70. JUMP_LABEL_DISABLE = 0,
  71. JUMP_LABEL_ENABLE,
  72. };
  73. struct module;
  74. #include <linux/atomic.h>
  75. static inline int static_key_count(struct static_key *key)
  76. {
  77. return atomic_read(&key->enabled);
  78. }
  79. #ifdef HAVE_JUMP_LABEL
  80. #define JUMP_LABEL_TYPE_FALSE_BRANCH 0UL
  81. #define JUMP_LABEL_TYPE_TRUE_BRANCH 1UL
  82. #define JUMP_LABEL_TYPE_MASK 1UL
  83. static
  84. inline struct jump_entry *jump_label_get_entries(struct static_key *key)
  85. {
  86. return (struct jump_entry *)((unsigned long)key->entries
  87. & ~JUMP_LABEL_TYPE_MASK);
  88. }
  89. static inline bool jump_label_get_branch_default(struct static_key *key)
  90. {
  91. if (((unsigned long)key->entries & JUMP_LABEL_TYPE_MASK) ==
  92. JUMP_LABEL_TYPE_TRUE_BRANCH)
  93. return true;
  94. return false;
  95. }
  96. static __always_inline bool static_key_false(struct static_key *key)
  97. {
  98. return arch_static_branch(key);
  99. }
  100. static __always_inline bool static_key_true(struct static_key *key)
  101. {
  102. return !static_key_false(key);
  103. }
  104. extern struct jump_entry __start___jump_table[];
  105. extern struct jump_entry __stop___jump_table[];
  106. extern void jump_label_init(void);
  107. extern void jump_label_lock(void);
  108. extern void jump_label_unlock(void);
  109. extern void arch_jump_label_transform(struct jump_entry *entry,
  110. enum jump_label_type type);
  111. extern void arch_jump_label_transform_static(struct jump_entry *entry,
  112. enum jump_label_type type);
  113. extern int jump_label_text_reserved(void *start, void *end);
  114. extern void static_key_slow_inc(struct static_key *key);
  115. extern void static_key_slow_dec(struct static_key *key);
  116. extern void jump_label_apply_nops(struct module *mod);
  117. #define STATIC_KEY_INIT_TRUE ((struct static_key) \
  118. { .enabled = ATOMIC_INIT(1), \
  119. .entries = (void *)JUMP_LABEL_TYPE_TRUE_BRANCH })
  120. #define STATIC_KEY_INIT_FALSE ((struct static_key) \
  121. { .enabled = ATOMIC_INIT(0), \
  122. .entries = (void *)JUMP_LABEL_TYPE_FALSE_BRANCH })
  123. #else /* !HAVE_JUMP_LABEL */
  124. static __always_inline void jump_label_init(void)
  125. {
  126. static_key_initialized = true;
  127. }
  128. static __always_inline bool static_key_false(struct static_key *key)
  129. {
  130. if (unlikely(static_key_count(key) > 0))
  131. return true;
  132. return false;
  133. }
  134. static __always_inline bool static_key_true(struct static_key *key)
  135. {
  136. if (likely(static_key_count(key) > 0))
  137. return true;
  138. return false;
  139. }
  140. static inline void static_key_slow_inc(struct static_key *key)
  141. {
  142. STATIC_KEY_CHECK_USE();
  143. atomic_inc(&key->enabled);
  144. }
  145. static inline void static_key_slow_dec(struct static_key *key)
  146. {
  147. STATIC_KEY_CHECK_USE();
  148. atomic_dec(&key->enabled);
  149. }
  150. static inline int jump_label_text_reserved(void *start, void *end)
  151. {
  152. return 0;
  153. }
  154. static inline void jump_label_lock(void) {}
  155. static inline void jump_label_unlock(void) {}
  156. static inline int jump_label_apply_nops(struct module *mod)
  157. {
  158. return 0;
  159. }
  160. #define STATIC_KEY_INIT_TRUE ((struct static_key) \
  161. { .enabled = ATOMIC_INIT(1) })
  162. #define STATIC_KEY_INIT_FALSE ((struct static_key) \
  163. { .enabled = ATOMIC_INIT(0) })
  164. #endif /* HAVE_JUMP_LABEL */
  165. #define STATIC_KEY_INIT STATIC_KEY_INIT_FALSE
  166. #define jump_label_enabled static_key_enabled
  167. static inline bool static_key_enabled(struct static_key *key)
  168. {
  169. return static_key_count(key) > 0;
  170. }
  171. #endif /* _LINUX_JUMP_LABEL_H */