jump_label.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547
  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 <[email protected]>
  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/staging/static-keys.rst
  71. */
  72. #ifndef __ASSEMBLY__
  73. #include <linux/types.h>
  74. #include <linux/compiler.h>
  75. extern bool static_key_initialized;
  76. #define STATIC_KEY_CHECK_USE(key) WARN(!static_key_initialized, \
  77. "%s(): static key '%pS' used before call to jump_label_init()", \
  78. __func__, (key))
  79. struct static_key {
  80. atomic_t enabled;
  81. #ifdef CONFIG_JUMP_LABEL
  82. /*
  83. * Note:
  84. * To make anonymous unions work with old compilers, the static
  85. * initialization of them requires brackets. This creates a dependency
  86. * on the order of the struct with the initializers. If any fields
  87. * are added, STATIC_KEY_INIT_TRUE and STATIC_KEY_INIT_FALSE may need
  88. * to be modified.
  89. *
  90. * bit 0 => 1 if key is initially true
  91. * 0 if initially false
  92. * bit 1 => 1 if points to struct static_key_mod
  93. * 0 if points to struct jump_entry
  94. */
  95. union {
  96. unsigned long type;
  97. struct jump_entry *entries;
  98. struct static_key_mod *next;
  99. };
  100. #endif /* CONFIG_JUMP_LABEL */
  101. };
  102. #endif /* __ASSEMBLY__ */
  103. #if defined(CONFIG_JUMP_LABEL) && !defined(BUILD_FIPS140_KO)
  104. #include <asm/jump_label.h>
  105. #ifndef __ASSEMBLY__
  106. #ifdef CONFIG_HAVE_ARCH_JUMP_LABEL_RELATIVE
  107. struct jump_entry {
  108. s32 code;
  109. s32 target;
  110. long key; // key may be far away from the core kernel under KASLR
  111. };
  112. static inline unsigned long jump_entry_code(const struct jump_entry *entry)
  113. {
  114. return (unsigned long)&entry->code + entry->code;
  115. }
  116. static inline unsigned long jump_entry_target(const struct jump_entry *entry)
  117. {
  118. return (unsigned long)&entry->target + entry->target;
  119. }
  120. static inline struct static_key *jump_entry_key(const struct jump_entry *entry)
  121. {
  122. long offset = entry->key & ~3L;
  123. return (struct static_key *)((unsigned long)&entry->key + offset);
  124. }
  125. #else
  126. static inline unsigned long jump_entry_code(const struct jump_entry *entry)
  127. {
  128. return entry->code;
  129. }
  130. static inline unsigned long jump_entry_target(const struct jump_entry *entry)
  131. {
  132. return entry->target;
  133. }
  134. static inline struct static_key *jump_entry_key(const struct jump_entry *entry)
  135. {
  136. return (struct static_key *)((unsigned long)entry->key & ~3UL);
  137. }
  138. #endif
  139. static inline bool jump_entry_is_branch(const struct jump_entry *entry)
  140. {
  141. return (unsigned long)entry->key & 1UL;
  142. }
  143. static inline bool jump_entry_is_init(const struct jump_entry *entry)
  144. {
  145. return (unsigned long)entry->key & 2UL;
  146. }
  147. static inline void jump_entry_set_init(struct jump_entry *entry, bool set)
  148. {
  149. if (set)
  150. entry->key |= 2;
  151. else
  152. entry->key &= ~2;
  153. }
  154. static inline int jump_entry_size(struct jump_entry *entry)
  155. {
  156. #ifdef JUMP_LABEL_NOP_SIZE
  157. return JUMP_LABEL_NOP_SIZE;
  158. #else
  159. return arch_jump_entry_size(entry);
  160. #endif
  161. }
  162. #endif
  163. #endif
  164. #ifndef __ASSEMBLY__
  165. enum jump_label_type {
  166. JUMP_LABEL_NOP = 0,
  167. JUMP_LABEL_JMP,
  168. };
  169. struct module;
  170. #ifdef BUILD_FIPS140_KO
  171. #include <linux/atomic.h>
  172. static inline int static_key_count(struct static_key *key)
  173. {
  174. return arch_atomic_read(&key->enabled);
  175. }
  176. static __always_inline bool static_key_false(struct static_key *key)
  177. {
  178. if (unlikely(static_key_count(key) > 0))
  179. return true;
  180. return false;
  181. }
  182. static __always_inline bool static_key_true(struct static_key *key)
  183. {
  184. if (likely(static_key_count(key) > 0))
  185. return true;
  186. return false;
  187. }
  188. #elif defined(CONFIG_JUMP_LABEL)
  189. #define JUMP_TYPE_FALSE 0UL
  190. #define JUMP_TYPE_TRUE 1UL
  191. #define JUMP_TYPE_LINKED 2UL
  192. #define JUMP_TYPE_MASK 3UL
  193. static __always_inline bool static_key_false(struct static_key *key)
  194. {
  195. return arch_static_branch(key, false);
  196. }
  197. static __always_inline bool static_key_true(struct static_key *key)
  198. {
  199. return !arch_static_branch(key, true);
  200. }
  201. extern struct jump_entry __start___jump_table[];
  202. extern struct jump_entry __stop___jump_table[];
  203. extern void jump_label_init(void);
  204. extern void jump_label_lock(void);
  205. extern void jump_label_unlock(void);
  206. extern void arch_jump_label_transform(struct jump_entry *entry,
  207. enum jump_label_type type);
  208. extern bool arch_jump_label_transform_queue(struct jump_entry *entry,
  209. enum jump_label_type type);
  210. extern void arch_jump_label_transform_apply(void);
  211. extern int jump_label_text_reserved(void *start, void *end);
  212. extern void static_key_slow_inc(struct static_key *key);
  213. extern void static_key_slow_dec(struct static_key *key);
  214. extern void static_key_slow_inc_cpuslocked(struct static_key *key);
  215. extern void static_key_slow_dec_cpuslocked(struct static_key *key);
  216. extern int static_key_count(struct static_key *key);
  217. extern void static_key_enable(struct static_key *key);
  218. extern void static_key_disable(struct static_key *key);
  219. extern void static_key_enable_cpuslocked(struct static_key *key);
  220. extern void static_key_disable_cpuslocked(struct static_key *key);
  221. extern enum jump_label_type jump_label_init_type(struct jump_entry *entry);
  222. /*
  223. * We should be using ATOMIC_INIT() for initializing .enabled, but
  224. * the inclusion of atomic.h is problematic for inclusion of jump_label.h
  225. * in 'low-level' headers. Thus, we are initializing .enabled with a
  226. * raw value, but have added a BUILD_BUG_ON() to catch any issues in
  227. * jump_label_init() see: kernel/jump_label.c.
  228. */
  229. #define STATIC_KEY_INIT_TRUE \
  230. { .enabled = { 1 }, \
  231. { .type = JUMP_TYPE_TRUE } }
  232. #define STATIC_KEY_INIT_FALSE \
  233. { .enabled = { 0 }, \
  234. { .type = JUMP_TYPE_FALSE } }
  235. #else /* !CONFIG_JUMP_LABEL */
  236. #include <linux/atomic.h>
  237. #include <linux/bug.h>
  238. static __always_inline int static_key_count(struct static_key *key)
  239. {
  240. return arch_atomic_read(&key->enabled);
  241. }
  242. static __always_inline void jump_label_init(void)
  243. {
  244. static_key_initialized = true;
  245. }
  246. static __always_inline bool static_key_false(struct static_key *key)
  247. {
  248. if (unlikely_notrace(static_key_count(key) > 0))
  249. return true;
  250. return false;
  251. }
  252. static __always_inline bool static_key_true(struct static_key *key)
  253. {
  254. if (likely_notrace(static_key_count(key) > 0))
  255. return true;
  256. return false;
  257. }
  258. static inline void static_key_slow_inc(struct static_key *key)
  259. {
  260. STATIC_KEY_CHECK_USE(key);
  261. atomic_inc(&key->enabled);
  262. }
  263. static inline void static_key_slow_dec(struct static_key *key)
  264. {
  265. STATIC_KEY_CHECK_USE(key);
  266. atomic_dec(&key->enabled);
  267. }
  268. #define static_key_slow_inc_cpuslocked(key) static_key_slow_inc(key)
  269. #define static_key_slow_dec_cpuslocked(key) static_key_slow_dec(key)
  270. static inline int jump_label_text_reserved(void *start, void *end)
  271. {
  272. return 0;
  273. }
  274. static inline void jump_label_lock(void) {}
  275. static inline void jump_label_unlock(void) {}
  276. static inline void static_key_enable(struct static_key *key)
  277. {
  278. STATIC_KEY_CHECK_USE(key);
  279. if (atomic_read(&key->enabled) != 0) {
  280. WARN_ON_ONCE(atomic_read(&key->enabled) != 1);
  281. return;
  282. }
  283. atomic_set(&key->enabled, 1);
  284. }
  285. static inline void static_key_disable(struct static_key *key)
  286. {
  287. STATIC_KEY_CHECK_USE(key);
  288. if (atomic_read(&key->enabled) != 1) {
  289. WARN_ON_ONCE(atomic_read(&key->enabled) != 0);
  290. return;
  291. }
  292. atomic_set(&key->enabled, 0);
  293. }
  294. #define static_key_enable_cpuslocked(k) static_key_enable((k))
  295. #define static_key_disable_cpuslocked(k) static_key_disable((k))
  296. #define STATIC_KEY_INIT_TRUE { .enabled = ATOMIC_INIT(1) }
  297. #define STATIC_KEY_INIT_FALSE { .enabled = ATOMIC_INIT(0) }
  298. #endif /* CONFIG_JUMP_LABEL */
  299. #define STATIC_KEY_INIT STATIC_KEY_INIT_FALSE
  300. #define jump_label_enabled static_key_enabled
  301. /* -------------------------------------------------------------------------- */
  302. /*
  303. * Two type wrappers around static_key, such that we can use compile time
  304. * type differentiation to emit the right code.
  305. *
  306. * All the below code is macros in order to play type games.
  307. */
  308. struct static_key_true {
  309. struct static_key key;
  310. };
  311. struct static_key_false {
  312. struct static_key key;
  313. };
  314. #define STATIC_KEY_TRUE_INIT (struct static_key_true) { .key = STATIC_KEY_INIT_TRUE, }
  315. #define STATIC_KEY_FALSE_INIT (struct static_key_false){ .key = STATIC_KEY_INIT_FALSE, }
  316. #define DEFINE_STATIC_KEY_TRUE(name) \
  317. struct static_key_true name = STATIC_KEY_TRUE_INIT
  318. #define DEFINE_STATIC_KEY_TRUE_RO(name) \
  319. struct static_key_true name __ro_after_init = STATIC_KEY_TRUE_INIT
  320. #define DECLARE_STATIC_KEY_TRUE(name) \
  321. extern struct static_key_true name
  322. #define DEFINE_STATIC_KEY_FALSE(name) \
  323. struct static_key_false name = STATIC_KEY_FALSE_INIT
  324. #define DEFINE_STATIC_KEY_FALSE_RO(name) \
  325. struct static_key_false name __ro_after_init = STATIC_KEY_FALSE_INIT
  326. #define DECLARE_STATIC_KEY_FALSE(name) \
  327. extern struct static_key_false name
  328. #define DEFINE_STATIC_KEY_ARRAY_TRUE(name, count) \
  329. struct static_key_true name[count] = { \
  330. [0 ... (count) - 1] = STATIC_KEY_TRUE_INIT, \
  331. }
  332. #define DEFINE_STATIC_KEY_ARRAY_FALSE(name, count) \
  333. struct static_key_false name[count] = { \
  334. [0 ... (count) - 1] = STATIC_KEY_FALSE_INIT, \
  335. }
  336. #define _DEFINE_STATIC_KEY_1(name) DEFINE_STATIC_KEY_TRUE(name)
  337. #define _DEFINE_STATIC_KEY_0(name) DEFINE_STATIC_KEY_FALSE(name)
  338. #define DEFINE_STATIC_KEY_MAYBE(cfg, name) \
  339. __PASTE(_DEFINE_STATIC_KEY_, IS_ENABLED(cfg))(name)
  340. #define _DEFINE_STATIC_KEY_RO_1(name) DEFINE_STATIC_KEY_TRUE_RO(name)
  341. #define _DEFINE_STATIC_KEY_RO_0(name) DEFINE_STATIC_KEY_FALSE_RO(name)
  342. #define DEFINE_STATIC_KEY_MAYBE_RO(cfg, name) \
  343. __PASTE(_DEFINE_STATIC_KEY_RO_, IS_ENABLED(cfg))(name)
  344. #define _DECLARE_STATIC_KEY_1(name) DECLARE_STATIC_KEY_TRUE(name)
  345. #define _DECLARE_STATIC_KEY_0(name) DECLARE_STATIC_KEY_FALSE(name)
  346. #define DECLARE_STATIC_KEY_MAYBE(cfg, name) \
  347. __PASTE(_DECLARE_STATIC_KEY_, IS_ENABLED(cfg))(name)
  348. extern bool ____wrong_branch_error(void);
  349. #define static_key_enabled(x) \
  350. ({ \
  351. if (!__builtin_types_compatible_p(typeof(*x), struct static_key) && \
  352. !__builtin_types_compatible_p(typeof(*x), struct static_key_true) &&\
  353. !__builtin_types_compatible_p(typeof(*x), struct static_key_false)) \
  354. ____wrong_branch_error(); \
  355. static_key_count((struct static_key *)x) > 0; \
  356. })
  357. #if defined(CONFIG_JUMP_LABEL) && !defined(BUILD_FIPS140_KO)
  358. /*
  359. * Combine the right initial value (type) with the right branch order
  360. * to generate the desired result.
  361. *
  362. *
  363. * type\branch| likely (1) | unlikely (0)
  364. * -----------+-----------------------+------------------
  365. * | |
  366. * true (1) | ... | ...
  367. * | NOP | JMP L
  368. * | <br-stmts> | 1: ...
  369. * | L: ... |
  370. * | |
  371. * | | L: <br-stmts>
  372. * | | jmp 1b
  373. * | |
  374. * -----------+-----------------------+------------------
  375. * | |
  376. * false (0) | ... | ...
  377. * | JMP L | NOP
  378. * | <br-stmts> | 1: ...
  379. * | L: ... |
  380. * | |
  381. * | | L: <br-stmts>
  382. * | | jmp 1b
  383. * | |
  384. * -----------+-----------------------+------------------
  385. *
  386. * The initial value is encoded in the LSB of static_key::entries,
  387. * type: 0 = false, 1 = true.
  388. *
  389. * The branch type is encoded in the LSB of jump_entry::key,
  390. * branch: 0 = unlikely, 1 = likely.
  391. *
  392. * This gives the following logic table:
  393. *
  394. * enabled type branch instuction
  395. * -----------------------------+-----------
  396. * 0 0 0 | NOP
  397. * 0 0 1 | JMP
  398. * 0 1 0 | NOP
  399. * 0 1 1 | JMP
  400. *
  401. * 1 0 0 | JMP
  402. * 1 0 1 | NOP
  403. * 1 1 0 | JMP
  404. * 1 1 1 | NOP
  405. *
  406. * Which gives the following functions:
  407. *
  408. * dynamic: instruction = enabled ^ branch
  409. * static: instruction = type ^ branch
  410. *
  411. * See jump_label_type() / jump_label_init_type().
  412. */
  413. #define static_branch_likely(x) \
  414. ({ \
  415. bool branch; \
  416. if (__builtin_types_compatible_p(typeof(*x), struct static_key_true)) \
  417. branch = !arch_static_branch(&(x)->key, true); \
  418. else if (__builtin_types_compatible_p(typeof(*x), struct static_key_false)) \
  419. branch = !arch_static_branch_jump(&(x)->key, true); \
  420. else \
  421. branch = ____wrong_branch_error(); \
  422. likely_notrace(branch); \
  423. })
  424. #define static_branch_unlikely(x) \
  425. ({ \
  426. bool branch; \
  427. if (__builtin_types_compatible_p(typeof(*x), struct static_key_true)) \
  428. branch = arch_static_branch_jump(&(x)->key, false); \
  429. else if (__builtin_types_compatible_p(typeof(*x), struct static_key_false)) \
  430. branch = arch_static_branch(&(x)->key, false); \
  431. else \
  432. branch = ____wrong_branch_error(); \
  433. unlikely_notrace(branch); \
  434. })
  435. #else /* !CONFIG_JUMP_LABEL */
  436. #define static_branch_likely(x) likely_notrace(static_key_enabled(&(x)->key))
  437. #define static_branch_unlikely(x) unlikely_notrace(static_key_enabled(&(x)->key))
  438. #endif /* CONFIG_JUMP_LABEL */
  439. #define static_branch_maybe(config, x) \
  440. (IS_ENABLED(config) ? static_branch_likely(x) \
  441. : static_branch_unlikely(x))
  442. /*
  443. * Advanced usage; refcount, branch is enabled when: count != 0
  444. */
  445. #define static_branch_inc(x) static_key_slow_inc(&(x)->key)
  446. #define static_branch_dec(x) static_key_slow_dec(&(x)->key)
  447. #define static_branch_inc_cpuslocked(x) static_key_slow_inc_cpuslocked(&(x)->key)
  448. #define static_branch_dec_cpuslocked(x) static_key_slow_dec_cpuslocked(&(x)->key)
  449. /*
  450. * Normal usage; boolean enable/disable.
  451. */
  452. #define static_branch_enable(x) static_key_enable(&(x)->key)
  453. #define static_branch_disable(x) static_key_disable(&(x)->key)
  454. #define static_branch_enable_cpuslocked(x) static_key_enable_cpuslocked(&(x)->key)
  455. #define static_branch_disable_cpuslocked(x) static_key_disable_cpuslocked(&(x)->key)
  456. #endif /* __ASSEMBLY__ */
  457. #endif /* _LINUX_JUMP_LABEL_H */