jump_label.h 5.2 KB

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