jump_label.h 12 KB

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