jump_label.h 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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_branch(&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 'static' branch target using
  15. * jump_label_{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 jump_label_{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 jump_label_key_deferred and
  29. * jump_label_dec_deferred() provide for this.
  30. *
  31. * Lacking toolchain and or architecture support, it falls back to a simple
  32. * conditional branch.
  33. */
  34. #include <linux/types.h>
  35. #include <linux/compiler.h>
  36. #include <linux/workqueue.h>
  37. #if defined(CC_HAVE_ASM_GOTO) && defined(CONFIG_JUMP_LABEL)
  38. struct jump_label_key {
  39. atomic_t enabled;
  40. struct jump_entry *entries;
  41. #ifdef CONFIG_MODULES
  42. struct jump_label_mod *next;
  43. #endif
  44. };
  45. struct jump_label_key_deferred {
  46. struct jump_label_key key;
  47. unsigned long timeout;
  48. struct delayed_work work;
  49. };
  50. # include <asm/jump_label.h>
  51. # define HAVE_JUMP_LABEL
  52. #endif /* CC_HAVE_ASM_GOTO && CONFIG_JUMP_LABEL */
  53. enum jump_label_type {
  54. JUMP_LABEL_DISABLE = 0,
  55. JUMP_LABEL_ENABLE,
  56. };
  57. struct module;
  58. #ifdef HAVE_JUMP_LABEL
  59. #ifdef CONFIG_MODULES
  60. #define JUMP_LABEL_INIT {ATOMIC_INIT(0), NULL, NULL}
  61. #else
  62. #define JUMP_LABEL_INIT {ATOMIC_INIT(0), NULL}
  63. #endif
  64. static __always_inline bool static_branch(struct jump_label_key *key)
  65. {
  66. return arch_static_branch(key);
  67. }
  68. extern struct jump_entry __start___jump_table[];
  69. extern struct jump_entry __stop___jump_table[];
  70. extern void jump_label_init(void);
  71. extern void jump_label_lock(void);
  72. extern void jump_label_unlock(void);
  73. extern void arch_jump_label_transform(struct jump_entry *entry,
  74. enum jump_label_type type);
  75. extern void arch_jump_label_transform_static(struct jump_entry *entry,
  76. enum jump_label_type type);
  77. extern int jump_label_text_reserved(void *start, void *end);
  78. extern void jump_label_inc(struct jump_label_key *key);
  79. extern void jump_label_dec(struct jump_label_key *key);
  80. extern void jump_label_dec_deferred(struct jump_label_key_deferred *key);
  81. extern bool jump_label_enabled(struct jump_label_key *key);
  82. extern void jump_label_apply_nops(struct module *mod);
  83. extern void jump_label_rate_limit(struct jump_label_key_deferred *key,
  84. unsigned long rl);
  85. #else /* !HAVE_JUMP_LABEL */
  86. #include <linux/atomic.h>
  87. #define JUMP_LABEL_INIT {ATOMIC_INIT(0)}
  88. struct jump_label_key {
  89. atomic_t enabled;
  90. };
  91. static __always_inline void jump_label_init(void)
  92. {
  93. }
  94. struct jump_label_key_deferred {
  95. struct jump_label_key key;
  96. };
  97. static __always_inline bool static_branch(struct jump_label_key *key)
  98. {
  99. if (unlikely(atomic_read(&key->enabled)))
  100. return true;
  101. return false;
  102. }
  103. static inline void jump_label_inc(struct jump_label_key *key)
  104. {
  105. atomic_inc(&key->enabled);
  106. }
  107. static inline void jump_label_dec(struct jump_label_key *key)
  108. {
  109. atomic_dec(&key->enabled);
  110. }
  111. static inline void jump_label_dec_deferred(struct jump_label_key_deferred *key)
  112. {
  113. jump_label_dec(&key->key);
  114. }
  115. static inline int jump_label_text_reserved(void *start, void *end)
  116. {
  117. return 0;
  118. }
  119. static inline void jump_label_lock(void) {}
  120. static inline void jump_label_unlock(void) {}
  121. static inline bool jump_label_enabled(struct jump_label_key *key)
  122. {
  123. return !!atomic_read(&key->enabled);
  124. }
  125. static inline int jump_label_apply_nops(struct module *mod)
  126. {
  127. return 0;
  128. }
  129. static inline void jump_label_rate_limit(struct jump_label_key_deferred *key,
  130. unsigned long rl)
  131. {
  132. }
  133. #endif /* HAVE_JUMP_LABEL */
  134. #define jump_label_key_enabled ((struct jump_label_key){ .enabled = ATOMIC_INIT(1), })
  135. #define jump_label_key_disabled ((struct jump_label_key){ .enabled = ATOMIC_INIT(0), })
  136. #endif /* _LINUX_JUMP_LABEL_H */