jump_label.h 14 KB

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