xref: /openbmc/linux/include/linux/static_call.h (revision 7825451f)
1115284d8SJosh Poimboeuf /* SPDX-License-Identifier: GPL-2.0 */
2115284d8SJosh Poimboeuf #ifndef _LINUX_STATIC_CALL_H
3115284d8SJosh Poimboeuf #define _LINUX_STATIC_CALL_H
4115284d8SJosh Poimboeuf 
5115284d8SJosh Poimboeuf /*
6115284d8SJosh Poimboeuf  * Static call support
7115284d8SJosh Poimboeuf  *
8115284d8SJosh Poimboeuf  * Static calls use code patching to hard-code function pointers into direct
9115284d8SJosh Poimboeuf  * branch instructions. They give the flexibility of function pointers, but
10115284d8SJosh Poimboeuf  * with improved performance. This is especially important for cases where
11115284d8SJosh Poimboeuf  * retpolines would otherwise be used, as retpolines can significantly impact
12115284d8SJosh Poimboeuf  * performance.
13115284d8SJosh Poimboeuf  *
14115284d8SJosh Poimboeuf  *
15115284d8SJosh Poimboeuf  * API overview:
16115284d8SJosh Poimboeuf  *
17115284d8SJosh Poimboeuf  *   DECLARE_STATIC_CALL(name, func);
18115284d8SJosh Poimboeuf  *   DEFINE_STATIC_CALL(name, func);
19452cddbfSPeter Zijlstra  *   DEFINE_STATIC_CALL_NULL(name, typename);
209ae6ab27SPeter Zijlstra  *   DEFINE_STATIC_CALL_RET0(name, typename);
219ae6ab27SPeter Zijlstra  *
229ae6ab27SPeter Zijlstra  *   __static_call_return0;
239ae6ab27SPeter Zijlstra  *
24115284d8SJosh Poimboeuf  *   static_call(name)(args...);
25452cddbfSPeter Zijlstra  *   static_call_cond(name)(args...);
26115284d8SJosh Poimboeuf  *   static_call_update(name, func);
276ea312d9SJuergen Gross  *   static_call_query(name);
28115284d8SJosh Poimboeuf  *
299ae6ab27SPeter Zijlstra  *   EXPORT_STATIC_CALL{,_TRAMP}{,_GPL}()
309ae6ab27SPeter Zijlstra  *
31115284d8SJosh Poimboeuf  * Usage example:
32115284d8SJosh Poimboeuf  *
33115284d8SJosh Poimboeuf  *   # Start with the following functions (with identical prototypes):
34115284d8SJosh Poimboeuf  *   int func_a(int arg1, int arg2);
35115284d8SJosh Poimboeuf  *   int func_b(int arg1, int arg2);
36115284d8SJosh Poimboeuf  *
37115284d8SJosh Poimboeuf  *   # Define a 'my_name' reference, associated with func_a() by default
38115284d8SJosh Poimboeuf  *   DEFINE_STATIC_CALL(my_name, func_a);
39115284d8SJosh Poimboeuf  *
40115284d8SJosh Poimboeuf  *   # Call func_a()
41115284d8SJosh Poimboeuf  *   static_call(my_name)(arg1, arg2);
42115284d8SJosh Poimboeuf  *
43115284d8SJosh Poimboeuf  *   # Update 'my_name' to point to func_b()
44115284d8SJosh Poimboeuf  *   static_call_update(my_name, &func_b);
45115284d8SJosh Poimboeuf  *
46115284d8SJosh Poimboeuf  *   # Call func_b()
47115284d8SJosh Poimboeuf  *   static_call(my_name)(arg1, arg2);
48115284d8SJosh Poimboeuf  *
49115284d8SJosh Poimboeuf  *
50115284d8SJosh Poimboeuf  * Implementation details:
51115284d8SJosh Poimboeuf  *
52115284d8SJosh Poimboeuf  *   This requires some arch-specific code (CONFIG_HAVE_STATIC_CALL).
53115284d8SJosh Poimboeuf  *   Otherwise basic indirect calls are used (with function pointers).
54115284d8SJosh Poimboeuf  *
55115284d8SJosh Poimboeuf  *   Each static_call() site calls into a trampoline associated with the name.
56115284d8SJosh Poimboeuf  *   The trampoline has a direct branch to the default function.  Updates to a
57115284d8SJosh Poimboeuf  *   name will modify the trampoline's branch destination.
58115284d8SJosh Poimboeuf  *
59115284d8SJosh Poimboeuf  *   If the arch has CONFIG_HAVE_STATIC_CALL_INLINE, then the call sites
60115284d8SJosh Poimboeuf  *   themselves will be patched at runtime to call the functions directly,
61115284d8SJosh Poimboeuf  *   rather than calling through the trampoline.  This requires objtool or a
62115284d8SJosh Poimboeuf  *   compiler plugin to detect all the static_call() sites and annotate them
63115284d8SJosh Poimboeuf  *   in the .static_call_sites section.
64452cddbfSPeter Zijlstra  *
65452cddbfSPeter Zijlstra  *
66452cddbfSPeter Zijlstra  * Notes on NULL function pointers:
67452cddbfSPeter Zijlstra  *
68452cddbfSPeter Zijlstra  *   Static_call()s support NULL functions, with many of the caveats that
69452cddbfSPeter Zijlstra  *   regular function pointers have.
70452cddbfSPeter Zijlstra  *
71452cddbfSPeter Zijlstra  *   Clearly calling a NULL function pointer is 'BAD', so too for
72452cddbfSPeter Zijlstra  *   static_call()s (although when HAVE_STATIC_CALL it might not be immediately
73452cddbfSPeter Zijlstra  *   fatal). A NULL static_call can be the result of:
74452cddbfSPeter Zijlstra  *
75452cddbfSPeter Zijlstra  *     DECLARE_STATIC_CALL_NULL(my_static_call, void (*)(int));
76452cddbfSPeter Zijlstra  *
77452cddbfSPeter Zijlstra  *   which is equivalent to declaring a NULL function pointer with just a
78452cddbfSPeter Zijlstra  *   typename:
79452cddbfSPeter Zijlstra  *
80452cddbfSPeter Zijlstra  *     void (*my_func_ptr)(int arg1) = NULL;
81452cddbfSPeter Zijlstra  *
82452cddbfSPeter Zijlstra  *   or using static_call_update() with a NULL function. In both cases the
83452cddbfSPeter Zijlstra  *   HAVE_STATIC_CALL implementation will patch the trampoline with a RET
84452cddbfSPeter Zijlstra  *   instruction, instead of an immediate tail-call JMP. HAVE_STATIC_CALL_INLINE
85452cddbfSPeter Zijlstra  *   architectures can patch the trampoline call to a NOP.
86452cddbfSPeter Zijlstra  *
87452cddbfSPeter Zijlstra  *   In all cases, any argument evaluation is unconditional. Unlike a regular
88452cddbfSPeter Zijlstra  *   conditional function pointer call:
89452cddbfSPeter Zijlstra  *
90452cddbfSPeter Zijlstra  *     if (my_func_ptr)
91452cddbfSPeter Zijlstra  *         my_func_ptr(arg1)
92452cddbfSPeter Zijlstra  *
93452cddbfSPeter Zijlstra  *   where the argument evaludation also depends on the pointer value.
94452cddbfSPeter Zijlstra  *
95452cddbfSPeter Zijlstra  *   When calling a static_call that can be NULL, use:
96452cddbfSPeter Zijlstra  *
97452cddbfSPeter Zijlstra  *     static_call_cond(name)(arg1);
98452cddbfSPeter Zijlstra  *
99452cddbfSPeter Zijlstra  *   which will include the required value tests to avoid NULL-pointer
100452cddbfSPeter Zijlstra  *   dereferences.
1016ea312d9SJuergen Gross  *
1026ea312d9SJuergen Gross  *   To query which function is currently set to be called, use:
1036ea312d9SJuergen Gross  *
1046ea312d9SJuergen Gross  *   func = static_call_query(name);
1059ae6ab27SPeter Zijlstra  *
1069ae6ab27SPeter Zijlstra  *
1079ae6ab27SPeter Zijlstra  * DEFINE_STATIC_CALL_RET0 / __static_call_return0:
1089ae6ab27SPeter Zijlstra  *
1099ae6ab27SPeter Zijlstra  *   Just like how DEFINE_STATIC_CALL_NULL() / static_call_cond() optimize the
1109ae6ab27SPeter Zijlstra  *   conditional void function call, DEFINE_STATIC_CALL_RET0 /
1119ae6ab27SPeter Zijlstra  *   __static_call_return0 optimize the do nothing return 0 function.
1129ae6ab27SPeter Zijlstra  *
1139ae6ab27SPeter Zijlstra  *   This feature is strictly UB per the C standard (since it casts a function
1149ae6ab27SPeter Zijlstra  *   pointer to a different signature) and relies on the architecture ABI to
1159ae6ab27SPeter Zijlstra  *   make things work. In particular it relies on Caller Stack-cleanup and the
1169ae6ab27SPeter Zijlstra  *   whole return register being clobbered for short return values. All normal
1179ae6ab27SPeter Zijlstra  *   CDECL style ABIs conform.
1189ae6ab27SPeter Zijlstra  *
1199ae6ab27SPeter Zijlstra  *   In particular the x86_64 implementation replaces the 5 byte CALL
1209ae6ab27SPeter Zijlstra  *   instruction at the callsite with a 5 byte clear of the RAX register,
1219ae6ab27SPeter Zijlstra  *   completely eliding any function call overhead.
1229ae6ab27SPeter Zijlstra  *
1239ae6ab27SPeter Zijlstra  *   Notably argument setup is unconditional.
1249ae6ab27SPeter Zijlstra  *
1259ae6ab27SPeter Zijlstra  *
1269ae6ab27SPeter Zijlstra  * EXPORT_STATIC_CALL() vs EXPORT_STATIC_CALL_TRAMP():
1279ae6ab27SPeter Zijlstra  *
1289ae6ab27SPeter Zijlstra  *   The difference is that the _TRAMP variant tries to only export the
1299ae6ab27SPeter Zijlstra  *   trampoline with the result that a module can use static_call{,_cond}() but
1309ae6ab27SPeter Zijlstra  *   not static_call_update().
1319ae6ab27SPeter Zijlstra  *
132115284d8SJosh Poimboeuf  */
133115284d8SJosh Poimboeuf 
134115284d8SJosh Poimboeuf #include <linux/types.h>
135115284d8SJosh Poimboeuf #include <linux/cpu.h>
136115284d8SJosh Poimboeuf #include <linux/static_call_types.h>
137115284d8SJosh Poimboeuf 
138115284d8SJosh Poimboeuf #ifdef CONFIG_HAVE_STATIC_CALL
139115284d8SJosh Poimboeuf #include <asm/static_call.h>
140115284d8SJosh Poimboeuf 
141115284d8SJosh Poimboeuf /*
142115284d8SJosh Poimboeuf  * Either @site or @tramp can be NULL.
143115284d8SJosh Poimboeuf  */
1445b06fd3bSPeter Zijlstra extern void arch_static_call_transform(void *site, void *tramp, void *func, bool tail);
145115284d8SJosh Poimboeuf 
146115284d8SJosh Poimboeuf #define STATIC_CALL_TRAMP_ADDR(name) &STATIC_CALL_TRAMP(name)
147115284d8SJosh Poimboeuf 
148115284d8SJosh Poimboeuf #else
149115284d8SJosh Poimboeuf #define STATIC_CALL_TRAMP_ADDR(name) NULL
150115284d8SJosh Poimboeuf #endif
151115284d8SJosh Poimboeuf 
152115284d8SJosh Poimboeuf #define static_call_update(name, func)					\
153115284d8SJosh Poimboeuf ({									\
1549432bbd9SPeter Zijlstra 	typeof(&STATIC_CALL_TRAMP(name)) __F = (func);			\
155115284d8SJosh Poimboeuf 	__static_call_update(&STATIC_CALL_KEY(name),			\
1569432bbd9SPeter Zijlstra 			     STATIC_CALL_TRAMP_ADDR(name), __F);	\
157115284d8SJosh Poimboeuf })
158115284d8SJosh Poimboeuf 
1596ea312d9SJuergen Gross #define static_call_query(name) (READ_ONCE(STATIC_CALL_KEY(name).func))
1606ea312d9SJuergen Gross 
1619183c3f9SJosh Poimboeuf #ifdef CONFIG_HAVE_STATIC_CALL_INLINE
1629183c3f9SJosh Poimboeuf 
16369e0ad37SNathan Chancellor extern int __init static_call_init(void);
164a945c834SPeter Zijlstra 
165*7825451fSPeter Zijlstra extern void static_call_force_reinit(void);
166*7825451fSPeter Zijlstra 
1679183c3f9SJosh Poimboeuf struct static_call_mod {
1689183c3f9SJosh Poimboeuf 	struct static_call_mod *next;
1699183c3f9SJosh Poimboeuf 	struct module *mod; /* for vmlinux, mod == NULL */
1709183c3f9SJosh Poimboeuf 	struct static_call_site *sites;
1719183c3f9SJosh Poimboeuf };
1729183c3f9SJosh Poimboeuf 
17373f44fe1SJosh Poimboeuf /* For finding the key associated with a trampoline */
17473f44fe1SJosh Poimboeuf struct static_call_tramp_key {
17573f44fe1SJosh Poimboeuf 	s32 tramp;
17673f44fe1SJosh Poimboeuf 	s32 key;
17773f44fe1SJosh Poimboeuf };
17873f44fe1SJosh Poimboeuf 
1799183c3f9SJosh Poimboeuf extern void __static_call_update(struct static_call_key *key, void *tramp, void *func);
1809183c3f9SJosh Poimboeuf extern int static_call_mod_init(struct module *mod);
1816333e8f7SPeter Zijlstra extern int static_call_text_reserved(void *start, void *end);
1829183c3f9SJosh Poimboeuf 
1833f2a8fc4SPeter Zijlstra extern long __static_call_return0(void);
1843f2a8fc4SPeter Zijlstra 
185df21c0d7SChristophe Leroy #define DEFINE_STATIC_CALL(name, _func)					\
1869183c3f9SJosh Poimboeuf 	DECLARE_STATIC_CALL(name, _func);				\
1879183c3f9SJosh Poimboeuf 	struct static_call_key STATIC_CALL_KEY(name) = {		\
188df21c0d7SChristophe Leroy 		.func = _func,						\
189a945c834SPeter Zijlstra 		.type = 1,						\
1909183c3f9SJosh Poimboeuf 	};								\
191df21c0d7SChristophe Leroy 	ARCH_DEFINE_STATIC_CALL_TRAMP(name, _func)
1929183c3f9SJosh Poimboeuf 
193452cddbfSPeter Zijlstra #define DEFINE_STATIC_CALL_NULL(name, _func)				\
194452cddbfSPeter Zijlstra 	DECLARE_STATIC_CALL(name, _func);				\
195452cddbfSPeter Zijlstra 	struct static_call_key STATIC_CALL_KEY(name) = {		\
196452cddbfSPeter Zijlstra 		.func = NULL,						\
197452cddbfSPeter Zijlstra 		.type = 1,						\
198452cddbfSPeter Zijlstra 	};								\
199452cddbfSPeter Zijlstra 	ARCH_DEFINE_STATIC_CALL_NULL_TRAMP(name)
200452cddbfSPeter Zijlstra 
2015517d500SChristophe Leroy #define DEFINE_STATIC_CALL_RET0(name, _func)				\
2025517d500SChristophe Leroy 	DECLARE_STATIC_CALL(name, _func);				\
2035517d500SChristophe Leroy 	struct static_call_key STATIC_CALL_KEY(name) = {		\
2045517d500SChristophe Leroy 		.func = __static_call_return0,				\
2055517d500SChristophe Leroy 		.type = 1,						\
2065517d500SChristophe Leroy 	};								\
2075517d500SChristophe Leroy 	ARCH_DEFINE_STATIC_CALL_RET0_TRAMP(name)
2085517d500SChristophe Leroy 
209452cddbfSPeter Zijlstra #define static_call_cond(name)	(void)__static_call(name)
2109183c3f9SJosh Poimboeuf 
2119183c3f9SJosh Poimboeuf #define EXPORT_STATIC_CALL(name)					\
2129183c3f9SJosh Poimboeuf 	EXPORT_SYMBOL(STATIC_CALL_KEY(name));				\
2139183c3f9SJosh Poimboeuf 	EXPORT_SYMBOL(STATIC_CALL_TRAMP(name))
2149183c3f9SJosh Poimboeuf #define EXPORT_STATIC_CALL_GPL(name)					\
2159183c3f9SJosh Poimboeuf 	EXPORT_SYMBOL_GPL(STATIC_CALL_KEY(name));			\
2169183c3f9SJosh Poimboeuf 	EXPORT_SYMBOL_GPL(STATIC_CALL_TRAMP(name))
2179183c3f9SJosh Poimboeuf 
21873f44fe1SJosh Poimboeuf /* Leave the key unexported, so modules can't change static call targets: */
21973f44fe1SJosh Poimboeuf #define EXPORT_STATIC_CALL_TRAMP(name)					\
22073f44fe1SJosh Poimboeuf 	EXPORT_SYMBOL(STATIC_CALL_TRAMP(name));				\
22173f44fe1SJosh Poimboeuf 	ARCH_ADD_TRAMP_KEY(name)
22273f44fe1SJosh Poimboeuf #define EXPORT_STATIC_CALL_TRAMP_GPL(name)				\
22373f44fe1SJosh Poimboeuf 	EXPORT_SYMBOL_GPL(STATIC_CALL_TRAMP(name));			\
22473f44fe1SJosh Poimboeuf 	ARCH_ADD_TRAMP_KEY(name)
22573f44fe1SJosh Poimboeuf 
2269183c3f9SJosh Poimboeuf #elif defined(CONFIG_HAVE_STATIC_CALL)
227115284d8SJosh Poimboeuf 
static_call_init(void)22869e0ad37SNathan Chancellor static inline int static_call_init(void) { return 0; }
229a945c834SPeter Zijlstra 
230df21c0d7SChristophe Leroy #define DEFINE_STATIC_CALL(name, _func)					\
231115284d8SJosh Poimboeuf 	DECLARE_STATIC_CALL(name, _func);				\
232115284d8SJosh Poimboeuf 	struct static_call_key STATIC_CALL_KEY(name) = {		\
233df21c0d7SChristophe Leroy 		.func = _func,						\
234115284d8SJosh Poimboeuf 	};								\
235df21c0d7SChristophe Leroy 	ARCH_DEFINE_STATIC_CALL_TRAMP(name, _func)
236115284d8SJosh Poimboeuf 
237452cddbfSPeter Zijlstra #define DEFINE_STATIC_CALL_NULL(name, _func)				\
238452cddbfSPeter Zijlstra 	DECLARE_STATIC_CALL(name, _func);				\
239452cddbfSPeter Zijlstra 	struct static_call_key STATIC_CALL_KEY(name) = {		\
240452cddbfSPeter Zijlstra 		.func = NULL,						\
241452cddbfSPeter Zijlstra 	};								\
242452cddbfSPeter Zijlstra 	ARCH_DEFINE_STATIC_CALL_NULL_TRAMP(name)
243452cddbfSPeter Zijlstra 
2445517d500SChristophe Leroy #define DEFINE_STATIC_CALL_RET0(name, _func)				\
2455517d500SChristophe Leroy 	DECLARE_STATIC_CALL(name, _func);				\
2465517d500SChristophe Leroy 	struct static_call_key STATIC_CALL_KEY(name) = {		\
2475517d500SChristophe Leroy 		.func = __static_call_return0,				\
2485517d500SChristophe Leroy 	};								\
2495517d500SChristophe Leroy 	ARCH_DEFINE_STATIC_CALL_RET0_TRAMP(name)
2506ea312d9SJuergen Gross 
251452cddbfSPeter Zijlstra #define static_call_cond(name)	(void)__static_call(name)
252115284d8SJosh Poimboeuf 
253115284d8SJosh Poimboeuf static inline
__static_call_update(struct static_call_key * key,void * tramp,void * func)254115284d8SJosh Poimboeuf void __static_call_update(struct static_call_key *key, void *tramp, void *func)
255115284d8SJosh Poimboeuf {
256115284d8SJosh Poimboeuf 	cpus_read_lock();
257115284d8SJosh Poimboeuf 	WRITE_ONCE(key->func, func);
2585b06fd3bSPeter Zijlstra 	arch_static_call_transform(NULL, tramp, func, false);
259115284d8SJosh Poimboeuf 	cpus_read_unlock();
260115284d8SJosh Poimboeuf }
261115284d8SJosh Poimboeuf 
static_call_text_reserved(void * start,void * end)2626333e8f7SPeter Zijlstra static inline int static_call_text_reserved(void *start, void *end)
2636333e8f7SPeter Zijlstra {
2646333e8f7SPeter Zijlstra 	return 0;
2656333e8f7SPeter Zijlstra }
2666333e8f7SPeter Zijlstra 
2678fd4dddaSChristophe Leroy extern long __static_call_return0(void);
2683f2a8fc4SPeter Zijlstra 
269115284d8SJosh Poimboeuf #define EXPORT_STATIC_CALL(name)					\
270115284d8SJosh Poimboeuf 	EXPORT_SYMBOL(STATIC_CALL_KEY(name));				\
271115284d8SJosh Poimboeuf 	EXPORT_SYMBOL(STATIC_CALL_TRAMP(name))
272115284d8SJosh Poimboeuf #define EXPORT_STATIC_CALL_GPL(name)					\
273115284d8SJosh Poimboeuf 	EXPORT_SYMBOL_GPL(STATIC_CALL_KEY(name));			\
274115284d8SJosh Poimboeuf 	EXPORT_SYMBOL_GPL(STATIC_CALL_TRAMP(name))
275115284d8SJosh Poimboeuf 
27673f44fe1SJosh Poimboeuf /* Leave the key unexported, so modules can't change static call targets: */
27773f44fe1SJosh Poimboeuf #define EXPORT_STATIC_CALL_TRAMP(name)					\
27873f44fe1SJosh Poimboeuf 	EXPORT_SYMBOL(STATIC_CALL_TRAMP(name))
27973f44fe1SJosh Poimboeuf #define EXPORT_STATIC_CALL_TRAMP_GPL(name)				\
28073f44fe1SJosh Poimboeuf 	EXPORT_SYMBOL_GPL(STATIC_CALL_TRAMP(name))
28173f44fe1SJosh Poimboeuf 
282115284d8SJosh Poimboeuf #else /* Generic implementation */
283115284d8SJosh Poimboeuf 
static_call_init(void)28469e0ad37SNathan Chancellor static inline int static_call_init(void) { return 0; }
285a945c834SPeter Zijlstra 
__static_call_return0(void)2863f2a8fc4SPeter Zijlstra static inline long __static_call_return0(void)
2873f2a8fc4SPeter Zijlstra {
2883f2a8fc4SPeter Zijlstra 	return 0;
2893f2a8fc4SPeter Zijlstra }
2903f2a8fc4SPeter Zijlstra 
29129fd0194SFrederic Weisbecker #define __DEFINE_STATIC_CALL(name, _func, _func_init)			\
292115284d8SJosh Poimboeuf 	DECLARE_STATIC_CALL(name, _func);				\
293115284d8SJosh Poimboeuf 	struct static_call_key STATIC_CALL_KEY(name) = {		\
29429fd0194SFrederic Weisbecker 		.func = _func_init,					\
295115284d8SJosh Poimboeuf 	}
296115284d8SJosh Poimboeuf 
297df21c0d7SChristophe Leroy #define DEFINE_STATIC_CALL(name, _func)					\
298df21c0d7SChristophe Leroy 	__DEFINE_STATIC_CALL(name, _func, _func)
299df21c0d7SChristophe Leroy 
300452cddbfSPeter Zijlstra #define DEFINE_STATIC_CALL_NULL(name, _func)				\
301df21c0d7SChristophe Leroy 	__DEFINE_STATIC_CALL(name, _func, NULL)
302452cddbfSPeter Zijlstra 
3035517d500SChristophe Leroy #define DEFINE_STATIC_CALL_RET0(name, _func)				\
3045517d500SChristophe Leroy 	__DEFINE_STATIC_CALL(name, _func, __static_call_return0)
3055517d500SChristophe Leroy 
__static_call_nop(void)306452cddbfSPeter Zijlstra static inline void __static_call_nop(void) { }
307452cddbfSPeter Zijlstra 
308452cddbfSPeter Zijlstra /*
309452cddbfSPeter Zijlstra  * This horrific hack takes care of two things:
310452cddbfSPeter Zijlstra  *
311452cddbfSPeter Zijlstra  *  - it ensures the compiler will only load the function pointer ONCE,
312452cddbfSPeter Zijlstra  *    which avoids a reload race.
313452cddbfSPeter Zijlstra  *
314452cddbfSPeter Zijlstra  *  - it ensures the argument evaluation is unconditional, similar
315452cddbfSPeter Zijlstra  *    to the HAVE_STATIC_CALL variant.
316452cddbfSPeter Zijlstra  *
317452cddbfSPeter Zijlstra  * Sadly current GCC/Clang (10 for both) do not optimize this properly
318452cddbfSPeter Zijlstra  * and will emit an indirect call for the NULL case :-(
319452cddbfSPeter Zijlstra  */
320452cddbfSPeter Zijlstra #define __static_call_cond(name)					\
321452cddbfSPeter Zijlstra ({									\
322452cddbfSPeter Zijlstra 	void *func = READ_ONCE(STATIC_CALL_KEY(name).func);		\
323452cddbfSPeter Zijlstra 	if (!func)							\
324452cddbfSPeter Zijlstra 		func = &__static_call_nop;				\
325452cddbfSPeter Zijlstra 	(typeof(STATIC_CALL_TRAMP(name))*)func;				\
326452cddbfSPeter Zijlstra })
327452cddbfSPeter Zijlstra 
328452cddbfSPeter Zijlstra #define static_call_cond(name)	(void)__static_call_cond(name)
329452cddbfSPeter Zijlstra 
330115284d8SJosh Poimboeuf static inline
__static_call_update(struct static_call_key * key,void * tramp,void * func)331115284d8SJosh Poimboeuf void __static_call_update(struct static_call_key *key, void *tramp, void *func)
332115284d8SJosh Poimboeuf {
333115284d8SJosh Poimboeuf 	WRITE_ONCE(key->func, func);
334115284d8SJosh Poimboeuf }
335115284d8SJosh Poimboeuf 
static_call_text_reserved(void * start,void * end)3366333e8f7SPeter Zijlstra static inline int static_call_text_reserved(void *start, void *end)
3376333e8f7SPeter Zijlstra {
3386333e8f7SPeter Zijlstra 	return 0;
3396333e8f7SPeter Zijlstra }
3406333e8f7SPeter Zijlstra 
341115284d8SJosh Poimboeuf #define EXPORT_STATIC_CALL(name)	EXPORT_SYMBOL(STATIC_CALL_KEY(name))
342115284d8SJosh Poimboeuf #define EXPORT_STATIC_CALL_GPL(name)	EXPORT_SYMBOL_GPL(STATIC_CALL_KEY(name))
343115284d8SJosh Poimboeuf 
344115284d8SJosh Poimboeuf #endif /* CONFIG_HAVE_STATIC_CALL */
345115284d8SJosh Poimboeuf 
346115284d8SJosh Poimboeuf #endif /* _LINUX_STATIC_CALL_H */
347