jump_label.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389
  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 Red Hat, Inc., Peter Zijlstra
  8. *
  9. * DEPRECATED API:
  10. *
  11. * The use of 'struct static_key' directly, is now DEPRECATED. In addition
  12. * static_key_{true,false}() is also DEPRECATED. IE DO NOT use the following:
  13. *
  14. * struct static_key false = STATIC_KEY_INIT_FALSE;
  15. * struct static_key true = STATIC_KEY_INIT_TRUE;
  16. * static_key_true()
  17. * static_key_false()
  18. *
  19. * The updated API replacements are:
  20. *
  21. * DEFINE_STATIC_KEY_TRUE(key);
  22. * DEFINE_STATIC_KEY_FALSE(key);
  23. * static_branch_likely()
  24. * static_branch_unlikely()
  25. *
  26. * Jump labels provide an interface to generate dynamic branches using
  27. * self-modifying code. Assuming toolchain and architecture support, if we
  28. * define a "key" that is initially false via "DEFINE_STATIC_KEY_FALSE(key)",
  29. * an "if (static_branch_unlikely(&key))" statement is an unconditional branch
  30. * (which defaults to false - and the true block is placed out of line).
  31. * Similarly, we can define an initially true key via
  32. * "DEFINE_STATIC_KEY_TRUE(key)", and use it in the same
  33. * "if (static_branch_unlikely(&key))", in which case we will generate an
  34. * unconditional branch to the out-of-line true branch. Keys that are
  35. * initially true or false can be using in both static_branch_unlikely()
  36. * and static_branch_likely() statements.
  37. *
  38. * At runtime we can change the branch target by setting the key
  39. * to true via a call to static_branch_enable(), or false using
  40. * static_branch_disable(). If the direction of the branch is switched by
  41. * these calls then we run-time modify the branch target via a
  42. * no-op -> jump or jump -> no-op conversion. For example, for an
  43. * initially false key that is used in an "if (static_branch_unlikely(&key))"
  44. * statement, setting the key to true requires us to patch in a jump
  45. * to the out-of-line of true branch.
  46. *
  47. * In addition to static_branch_{enable,disable}, we can also reference count
  48. * the key or branch direction via static_branch_{inc,dec}. Thus,
  49. * static_branch_inc() can be thought of as a 'make more true' and
  50. * static_branch_dec() as a 'make more false'.
  51. *
  52. * Since this relies on modifying code, the branch modifying functions
  53. * must be considered absolute slow paths (machine wide synchronization etc.).
  54. * OTOH, since the affected branches are unconditional, their runtime overhead
  55. * will be absolutely minimal, esp. in the default (off) case where the total
  56. * effect is a single NOP of appropriate size. The on case will patch in a jump
  57. * to the out-of-line block.
  58. *
  59. * When the control is directly exposed to userspace, it is prudent to delay the
  60. * decrement to avoid high frequency code modifications which can (and do)
  61. * cause significant performance degradation. Struct static_key_deferred and
  62. * static_key_slow_dec_deferred() provide for this.
  63. *
  64. * Lacking toolchain and or architecture support, static keys fall back to a
  65. * simple conditional branch.
  66. *
  67. * Additional babbling in: Documentation/static-keys.txt
  68. */
  69. #if defined(CC_HAVE_ASM_GOTO) && defined(CONFIG_JUMP_LABEL)
  70. # define HAVE_JUMP_LABEL
  71. #endif
  72. #ifndef __ASSEMBLY__
  73. #include <linux/types.h>
  74. #include <linux/compiler.h>
  75. #include <linux/bug.h>
  76. extern bool static_key_initialized;
  77. #define STATIC_KEY_CHECK_USE() WARN(!static_key_initialized, \
  78. "%s used before call to jump_label_init", \
  79. __func__)
  80. #ifdef HAVE_JUMP_LABEL
  81. struct static_key {
  82. atomic_t enabled;
  83. /* Set lsb bit to 1 if branch is default true, 0 ot */
  84. struct jump_entry *entries;
  85. #ifdef CONFIG_MODULES
  86. struct static_key_mod *next;
  87. #endif
  88. };
  89. #else
  90. struct static_key {
  91. atomic_t enabled;
  92. };
  93. #endif /* HAVE_JUMP_LABEL */
  94. #endif /* __ASSEMBLY__ */
  95. #ifdef HAVE_JUMP_LABEL
  96. #include <asm/jump_label.h>
  97. #endif
  98. #ifndef __ASSEMBLY__
  99. enum jump_label_type {
  100. JUMP_LABEL_NOP = 0,
  101. JUMP_LABEL_JMP,
  102. };
  103. struct module;
  104. #include <linux/atomic.h>
  105. #ifdef HAVE_JUMP_LABEL
  106. static inline int static_key_count(struct static_key *key)
  107. {
  108. /*
  109. * -1 means the first static_key_slow_inc() is in progress.
  110. * static_key_enabled() must return true, so return 1 here.
  111. */
  112. int n = atomic_read(&key->enabled);
  113. return n >= 0 ? n : 1;
  114. }
  115. #define JUMP_TYPE_FALSE 0UL
  116. #define JUMP_TYPE_TRUE 1UL
  117. #define JUMP_TYPE_MASK 1UL
  118. static __always_inline bool static_key_false(struct static_key *key)
  119. {
  120. return arch_static_branch(key, false);
  121. }
  122. static __always_inline bool static_key_true(struct static_key *key)
  123. {
  124. return !arch_static_branch(key, true);
  125. }
  126. extern struct jump_entry __start___jump_table[];
  127. extern struct jump_entry __stop___jump_table[];
  128. extern void jump_label_init(void);
  129. extern void jump_label_lock(void);
  130. extern void jump_label_unlock(void);
  131. extern void arch_jump_label_transform(struct jump_entry *entry,
  132. enum jump_label_type type);
  133. extern void arch_jump_label_transform_static(struct jump_entry *entry,
  134. enum jump_label_type type);
  135. extern int jump_label_text_reserved(void *start, void *end);
  136. extern void static_key_slow_inc(struct static_key *key);
  137. extern void static_key_slow_dec(struct static_key *key);
  138. extern void jump_label_apply_nops(struct module *mod);
  139. #define STATIC_KEY_INIT_TRUE \
  140. { .enabled = ATOMIC_INIT(1), \
  141. .entries = (void *)JUMP_TYPE_TRUE }
  142. #define STATIC_KEY_INIT_FALSE \
  143. { .enabled = ATOMIC_INIT(0), \
  144. .entries = (void *)JUMP_TYPE_FALSE }
  145. #else /* !HAVE_JUMP_LABEL */
  146. static inline int static_key_count(struct static_key *key)
  147. {
  148. return atomic_read(&key->enabled);
  149. }
  150. static __always_inline void jump_label_init(void)
  151. {
  152. static_key_initialized = true;
  153. }
  154. static __always_inline bool static_key_false(struct static_key *key)
  155. {
  156. if (unlikely(static_key_count(key) > 0))
  157. return true;
  158. return false;
  159. }
  160. static __always_inline bool static_key_true(struct static_key *key)
  161. {
  162. if (likely(static_key_count(key) > 0))
  163. return true;
  164. return false;
  165. }
  166. static inline void static_key_slow_inc(struct static_key *key)
  167. {
  168. STATIC_KEY_CHECK_USE();
  169. atomic_inc(&key->enabled);
  170. }
  171. static inline void static_key_slow_dec(struct static_key *key)
  172. {
  173. STATIC_KEY_CHECK_USE();
  174. atomic_dec(&key->enabled);
  175. }
  176. static inline int jump_label_text_reserved(void *start, void *end)
  177. {
  178. return 0;
  179. }
  180. static inline void jump_label_lock(void) {}
  181. static inline void jump_label_unlock(void) {}
  182. static inline int jump_label_apply_nops(struct module *mod)
  183. {
  184. return 0;
  185. }
  186. #define STATIC_KEY_INIT_TRUE { .enabled = ATOMIC_INIT(1) }
  187. #define STATIC_KEY_INIT_FALSE { .enabled = ATOMIC_INIT(0) }
  188. #endif /* HAVE_JUMP_LABEL */
  189. #define STATIC_KEY_INIT STATIC_KEY_INIT_FALSE
  190. #define jump_label_enabled static_key_enabled
  191. static inline void static_key_enable(struct static_key *key)
  192. {
  193. int count = static_key_count(key);
  194. WARN_ON_ONCE(count < 0 || count > 1);
  195. if (!count)
  196. static_key_slow_inc(key);
  197. }
  198. static inline void static_key_disable(struct static_key *key)
  199. {
  200. int count = static_key_count(key);
  201. WARN_ON_ONCE(count < 0 || count > 1);
  202. if (count)
  203. static_key_slow_dec(key);
  204. }
  205. /* -------------------------------------------------------------------------- */
  206. /*
  207. * Two type wrappers around static_key, such that we can use compile time
  208. * type differentiation to emit the right code.
  209. *
  210. * All the below code is macros in order to play type games.
  211. */
  212. struct static_key_true {
  213. struct static_key key;
  214. };
  215. struct static_key_false {
  216. struct static_key key;
  217. };
  218. #define STATIC_KEY_TRUE_INIT (struct static_key_true) { .key = STATIC_KEY_INIT_TRUE, }
  219. #define STATIC_KEY_FALSE_INIT (struct static_key_false){ .key = STATIC_KEY_INIT_FALSE, }
  220. #define DEFINE_STATIC_KEY_TRUE(name) \
  221. struct static_key_true name = STATIC_KEY_TRUE_INIT
  222. #define DEFINE_STATIC_KEY_FALSE(name) \
  223. struct static_key_false name = STATIC_KEY_FALSE_INIT
  224. extern bool ____wrong_branch_error(void);
  225. #define static_key_enabled(x) \
  226. ({ \
  227. if (!__builtin_types_compatible_p(typeof(*x), struct static_key) && \
  228. !__builtin_types_compatible_p(typeof(*x), struct static_key_true) &&\
  229. !__builtin_types_compatible_p(typeof(*x), struct static_key_false)) \
  230. ____wrong_branch_error(); \
  231. static_key_count((struct static_key *)x) > 0; \
  232. })
  233. #ifdef HAVE_JUMP_LABEL
  234. /*
  235. * Combine the right initial value (type) with the right branch order
  236. * to generate the desired result.
  237. *
  238. *
  239. * type\branch| likely (1) | unlikely (0)
  240. * -----------+-----------------------+------------------
  241. * | |
  242. * true (1) | ... | ...
  243. * | NOP | JMP L
  244. * | <br-stmts> | 1: ...
  245. * | L: ... |
  246. * | |
  247. * | | L: <br-stmts>
  248. * | | jmp 1b
  249. * | |
  250. * -----------+-----------------------+------------------
  251. * | |
  252. * false (0) | ... | ...
  253. * | JMP L | NOP
  254. * | <br-stmts> | 1: ...
  255. * | L: ... |
  256. * | |
  257. * | | L: <br-stmts>
  258. * | | jmp 1b
  259. * | |
  260. * -----------+-----------------------+------------------
  261. *
  262. * The initial value is encoded in the LSB of static_key::entries,
  263. * type: 0 = false, 1 = true.
  264. *
  265. * The branch type is encoded in the LSB of jump_entry::key,
  266. * branch: 0 = unlikely, 1 = likely.
  267. *
  268. * This gives the following logic table:
  269. *
  270. * enabled type branch instuction
  271. * -----------------------------+-----------
  272. * 0 0 0 | NOP
  273. * 0 0 1 | JMP
  274. * 0 1 0 | NOP
  275. * 0 1 1 | JMP
  276. *
  277. * 1 0 0 | JMP
  278. * 1 0 1 | NOP
  279. * 1 1 0 | JMP
  280. * 1 1 1 | NOP
  281. *
  282. * Which gives the following functions:
  283. *
  284. * dynamic: instruction = enabled ^ branch
  285. * static: instruction = type ^ branch
  286. *
  287. * See jump_label_type() / jump_label_init_type().
  288. */
  289. #define static_branch_likely(x) \
  290. ({ \
  291. bool branch; \
  292. if (__builtin_types_compatible_p(typeof(*x), struct static_key_true)) \
  293. branch = !arch_static_branch(&(x)->key, true); \
  294. else if (__builtin_types_compatible_p(typeof(*x), struct static_key_false)) \
  295. branch = !arch_static_branch_jump(&(x)->key, true); \
  296. else \
  297. branch = ____wrong_branch_error(); \
  298. branch; \
  299. })
  300. #define static_branch_unlikely(x) \
  301. ({ \
  302. bool branch; \
  303. if (__builtin_types_compatible_p(typeof(*x), struct static_key_true)) \
  304. branch = arch_static_branch_jump(&(x)->key, false); \
  305. else if (__builtin_types_compatible_p(typeof(*x), struct static_key_false)) \
  306. branch = arch_static_branch(&(x)->key, false); \
  307. else \
  308. branch = ____wrong_branch_error(); \
  309. branch; \
  310. })
  311. #else /* !HAVE_JUMP_LABEL */
  312. #define static_branch_likely(x) likely(static_key_enabled(&(x)->key))
  313. #define static_branch_unlikely(x) unlikely(static_key_enabled(&(x)->key))
  314. #endif /* HAVE_JUMP_LABEL */
  315. /*
  316. * Advanced usage; refcount, branch is enabled when: count != 0
  317. */
  318. #define static_branch_inc(x) static_key_slow_inc(&(x)->key)
  319. #define static_branch_dec(x) static_key_slow_dec(&(x)->key)
  320. /*
  321. * Normal usage; boolean enable/disable.
  322. */
  323. #define static_branch_enable(x) static_key_enable(&(x)->key)
  324. #define static_branch_disable(x) static_key_disable(&(x)->key)
  325. #endif /* _LINUX_JUMP_LABEL_H */
  326. #endif /* __ASSEMBLY__ */