static_call.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. #ifndef _LINUX_STATIC_CALL_H
  3. #define _LINUX_STATIC_CALL_H
  4. /*
  5. * Static call support
  6. *
  7. * Static calls use code patching to hard-code function pointers into direct
  8. * branch instructions. They give the flexibility of function pointers, but
  9. * with improved performance. This is especially important for cases where
  10. * retpolines would otherwise be used, as retpolines can significantly impact
  11. * performance.
  12. *
  13. *
  14. * API overview:
  15. *
  16. * DECLARE_STATIC_CALL(name, func);
  17. * DEFINE_STATIC_CALL(name, func);
  18. * DEFINE_STATIC_CALL_NULL(name, typename);
  19. * DEFINE_STATIC_CALL_RET0(name, typename);
  20. *
  21. * __static_call_return0;
  22. *
  23. * static_call(name)(args...);
  24. * static_call_cond(name)(args...);
  25. * static_call_update(name, func);
  26. * static_call_query(name);
  27. *
  28. * EXPORT_STATIC_CALL{,_TRAMP}{,_GPL}()
  29. *
  30. * Usage example:
  31. *
  32. * # Start with the following functions (with identical prototypes):
  33. * int func_a(int arg1, int arg2);
  34. * int func_b(int arg1, int arg2);
  35. *
  36. * # Define a 'my_name' reference, associated with func_a() by default
  37. * DEFINE_STATIC_CALL(my_name, func_a);
  38. *
  39. * # Call func_a()
  40. * static_call(my_name)(arg1, arg2);
  41. *
  42. * # Update 'my_name' to point to func_b()
  43. * static_call_update(my_name, &func_b);
  44. *
  45. * # Call func_b()
  46. * static_call(my_name)(arg1, arg2);
  47. *
  48. *
  49. * Implementation details:
  50. *
  51. * This requires some arch-specific code (CONFIG_HAVE_STATIC_CALL).
  52. * Otherwise basic indirect calls are used (with function pointers).
  53. *
  54. * Each static_call() site calls into a trampoline associated with the name.
  55. * The trampoline has a direct branch to the default function. Updates to a
  56. * name will modify the trampoline's branch destination.
  57. *
  58. * If the arch has CONFIG_HAVE_STATIC_CALL_INLINE, then the call sites
  59. * themselves will be patched at runtime to call the functions directly,
  60. * rather than calling through the trampoline. This requires objtool or a
  61. * compiler plugin to detect all the static_call() sites and annotate them
  62. * in the .static_call_sites section.
  63. *
  64. *
  65. * Notes on NULL function pointers:
  66. *
  67. * Static_call()s support NULL functions, with many of the caveats that
  68. * regular function pointers have.
  69. *
  70. * Clearly calling a NULL function pointer is 'BAD', so too for
  71. * static_call()s (although when HAVE_STATIC_CALL it might not be immediately
  72. * fatal). A NULL static_call can be the result of:
  73. *
  74. * DECLARE_STATIC_CALL_NULL(my_static_call, void (*)(int));
  75. *
  76. * which is equivalent to declaring a NULL function pointer with just a
  77. * typename:
  78. *
  79. * void (*my_func_ptr)(int arg1) = NULL;
  80. *
  81. * or using static_call_update() with a NULL function. In both cases the
  82. * HAVE_STATIC_CALL implementation will patch the trampoline with a RET
  83. * instruction, instead of an immediate tail-call JMP. HAVE_STATIC_CALL_INLINE
  84. * architectures can patch the trampoline call to a NOP.
  85. *
  86. * In all cases, any argument evaluation is unconditional. Unlike a regular
  87. * conditional function pointer call:
  88. *
  89. * if (my_func_ptr)
  90. * my_func_ptr(arg1)
  91. *
  92. * where the argument evaludation also depends on the pointer value.
  93. *
  94. * When calling a static_call that can be NULL, use:
  95. *
  96. * static_call_cond(name)(arg1);
  97. *
  98. * which will include the required value tests to avoid NULL-pointer
  99. * dereferences.
  100. *
  101. * To query which function is currently set to be called, use:
  102. *
  103. * func = static_call_query(name);
  104. *
  105. *
  106. * DEFINE_STATIC_CALL_RET0 / __static_call_return0:
  107. *
  108. * Just like how DEFINE_STATIC_CALL_NULL() / static_call_cond() optimize the
  109. * conditional void function call, DEFINE_STATIC_CALL_RET0 /
  110. * __static_call_return0 optimize the do nothing return 0 function.
  111. *
  112. * This feature is strictly UB per the C standard (since it casts a function
  113. * pointer to a different signature) and relies on the architecture ABI to
  114. * make things work. In particular it relies on Caller Stack-cleanup and the
  115. * whole return register being clobbered for short return values. All normal
  116. * CDECL style ABIs conform.
  117. *
  118. * In particular the x86_64 implementation replaces the 5 byte CALL
  119. * instruction at the callsite with a 5 byte clear of the RAX register,
  120. * completely eliding any function call overhead.
  121. *
  122. * Notably argument setup is unconditional.
  123. *
  124. *
  125. * EXPORT_STATIC_CALL() vs EXPORT_STATIC_CALL_TRAMP():
  126. *
  127. * The difference is that the _TRAMP variant tries to only export the
  128. * trampoline with the result that a module can use static_call{,_cond}() but
  129. * not static_call_update().
  130. *
  131. */
  132. #include <linux/types.h>
  133. #include <linux/cpu.h>
  134. #include <linux/static_call_types.h>
  135. #ifdef CONFIG_HAVE_STATIC_CALL
  136. #include <asm/static_call.h>
  137. /*
  138. * Either @site or @tramp can be NULL.
  139. */
  140. extern void arch_static_call_transform(void *site, void *tramp, void *func, bool tail);
  141. #define STATIC_CALL_TRAMP_ADDR(name) &STATIC_CALL_TRAMP(name)
  142. #else
  143. #define STATIC_CALL_TRAMP_ADDR(name) NULL
  144. #endif
  145. #define static_call_update(name, func) \
  146. ({ \
  147. typeof(&STATIC_CALL_TRAMP(name)) __F = (func); \
  148. __static_call_update(&STATIC_CALL_KEY(name), \
  149. STATIC_CALL_TRAMP_ADDR(name), __F); \
  150. })
  151. #define static_call_query(name) (READ_ONCE(STATIC_CALL_KEY(name).func))
  152. #ifdef CONFIG_HAVE_STATIC_CALL_INLINE
  153. extern int __init static_call_init(void);
  154. struct static_call_mod {
  155. struct static_call_mod *next;
  156. struct module *mod; /* for vmlinux, mod == NULL */
  157. struct static_call_site *sites;
  158. };
  159. /* For finding the key associated with a trampoline */
  160. struct static_call_tramp_key {
  161. s32 tramp;
  162. s32 key;
  163. };
  164. extern void __static_call_update(struct static_call_key *key, void *tramp, void *func);
  165. extern int static_call_mod_init(struct module *mod);
  166. extern int static_call_text_reserved(void *start, void *end);
  167. extern long __static_call_return0(void);
  168. #define DEFINE_STATIC_CALL(name, _func) \
  169. DECLARE_STATIC_CALL(name, _func); \
  170. struct static_call_key STATIC_CALL_KEY(name) = { \
  171. .func = _func, \
  172. .type = 1, \
  173. }; \
  174. ARCH_DEFINE_STATIC_CALL_TRAMP(name, _func)
  175. #define DEFINE_STATIC_CALL_NULL(name, _func) \
  176. DECLARE_STATIC_CALL(name, _func); \
  177. struct static_call_key STATIC_CALL_KEY(name) = { \
  178. .func = NULL, \
  179. .type = 1, \
  180. }; \
  181. ARCH_DEFINE_STATIC_CALL_NULL_TRAMP(name)
  182. #define DEFINE_STATIC_CALL_RET0(name, _func) \
  183. DECLARE_STATIC_CALL(name, _func); \
  184. struct static_call_key STATIC_CALL_KEY(name) = { \
  185. .func = __static_call_return0, \
  186. .type = 1, \
  187. }; \
  188. ARCH_DEFINE_STATIC_CALL_RET0_TRAMP(name)
  189. #define static_call_cond(name) (void)__static_call(name)
  190. #define EXPORT_STATIC_CALL(name) \
  191. EXPORT_SYMBOL(STATIC_CALL_KEY(name)); \
  192. EXPORT_SYMBOL(STATIC_CALL_TRAMP(name))
  193. #define EXPORT_STATIC_CALL_GPL(name) \
  194. EXPORT_SYMBOL_GPL(STATIC_CALL_KEY(name)); \
  195. EXPORT_SYMBOL_GPL(STATIC_CALL_TRAMP(name))
  196. /* Leave the key unexported, so modules can't change static call targets: */
  197. #define EXPORT_STATIC_CALL_TRAMP(name) \
  198. EXPORT_SYMBOL(STATIC_CALL_TRAMP(name)); \
  199. ARCH_ADD_TRAMP_KEY(name)
  200. #define EXPORT_STATIC_CALL_TRAMP_GPL(name) \
  201. EXPORT_SYMBOL_GPL(STATIC_CALL_TRAMP(name)); \
  202. ARCH_ADD_TRAMP_KEY(name)
  203. #elif defined(CONFIG_HAVE_STATIC_CALL)
  204. static inline int static_call_init(void) { return 0; }
  205. #define DEFINE_STATIC_CALL(name, _func) \
  206. DECLARE_STATIC_CALL(name, _func); \
  207. struct static_call_key STATIC_CALL_KEY(name) = { \
  208. .func = _func, \
  209. }; \
  210. ARCH_DEFINE_STATIC_CALL_TRAMP(name, _func)
  211. #define DEFINE_STATIC_CALL_NULL(name, _func) \
  212. DECLARE_STATIC_CALL(name, _func); \
  213. struct static_call_key STATIC_CALL_KEY(name) = { \
  214. .func = NULL, \
  215. }; \
  216. ARCH_DEFINE_STATIC_CALL_NULL_TRAMP(name)
  217. #define DEFINE_STATIC_CALL_RET0(name, _func) \
  218. DECLARE_STATIC_CALL(name, _func); \
  219. struct static_call_key STATIC_CALL_KEY(name) = { \
  220. .func = __static_call_return0, \
  221. }; \
  222. ARCH_DEFINE_STATIC_CALL_RET0_TRAMP(name)
  223. #define static_call_cond(name) (void)__static_call(name)
  224. static inline
  225. void __static_call_update(struct static_call_key *key, void *tramp, void *func)
  226. {
  227. cpus_read_lock();
  228. WRITE_ONCE(key->func, func);
  229. arch_static_call_transform(NULL, tramp, func, false);
  230. cpus_read_unlock();
  231. }
  232. static inline int static_call_text_reserved(void *start, void *end)
  233. {
  234. return 0;
  235. }
  236. extern long __static_call_return0(void);
  237. #define EXPORT_STATIC_CALL(name) \
  238. EXPORT_SYMBOL(STATIC_CALL_KEY(name)); \
  239. EXPORT_SYMBOL(STATIC_CALL_TRAMP(name))
  240. #define EXPORT_STATIC_CALL_GPL(name) \
  241. EXPORT_SYMBOL_GPL(STATIC_CALL_KEY(name)); \
  242. EXPORT_SYMBOL_GPL(STATIC_CALL_TRAMP(name))
  243. /* Leave the key unexported, so modules can't change static call targets: */
  244. #define EXPORT_STATIC_CALL_TRAMP(name) \
  245. EXPORT_SYMBOL(STATIC_CALL_TRAMP(name))
  246. #define EXPORT_STATIC_CALL_TRAMP_GPL(name) \
  247. EXPORT_SYMBOL_GPL(STATIC_CALL_TRAMP(name))
  248. #else /* Generic implementation */
  249. static inline int static_call_init(void) { return 0; }
  250. static inline long __static_call_return0(void)
  251. {
  252. return 0;
  253. }
  254. #define __DEFINE_STATIC_CALL(name, _func, _func_init) \
  255. DECLARE_STATIC_CALL(name, _func); \
  256. struct static_call_key STATIC_CALL_KEY(name) = { \
  257. .func = _func_init, \
  258. }
  259. #define DEFINE_STATIC_CALL(name, _func) \
  260. __DEFINE_STATIC_CALL(name, _func, _func)
  261. #define DEFINE_STATIC_CALL_NULL(name, _func) \
  262. __DEFINE_STATIC_CALL(name, _func, NULL)
  263. #define DEFINE_STATIC_CALL_RET0(name, _func) \
  264. __DEFINE_STATIC_CALL(name, _func, __static_call_return0)
  265. static inline void __static_call_nop(void) { }
  266. /*
  267. * This horrific hack takes care of two things:
  268. *
  269. * - it ensures the compiler will only load the function pointer ONCE,
  270. * which avoids a reload race.
  271. *
  272. * - it ensures the argument evaluation is unconditional, similar
  273. * to the HAVE_STATIC_CALL variant.
  274. *
  275. * Sadly current GCC/Clang (10 for both) do not optimize this properly
  276. * and will emit an indirect call for the NULL case :-(
  277. */
  278. #define __static_call_cond(name) \
  279. ({ \
  280. void *func = READ_ONCE(STATIC_CALL_KEY(name).func); \
  281. if (!func) \
  282. func = &__static_call_nop; \
  283. (typeof(STATIC_CALL_TRAMP(name))*)func; \
  284. })
  285. #define static_call_cond(name) (void)__static_call_cond(name)
  286. static inline
  287. void __static_call_update(struct static_call_key *key, void *tramp, void *func)
  288. {
  289. WRITE_ONCE(key->func, func);
  290. }
  291. static inline int static_call_text_reserved(void *start, void *end)
  292. {
  293. return 0;
  294. }
  295. #define EXPORT_STATIC_CALL(name) EXPORT_SYMBOL(STATIC_CALL_KEY(name))
  296. #define EXPORT_STATIC_CALL_GPL(name) EXPORT_SYMBOL_GPL(STATIC_CALL_KEY(name))
  297. #endif /* CONFIG_HAVE_STATIC_CALL */
  298. #endif /* _LINUX_STATIC_CALL_H */