xref: /openbmc/linux/include/linux/static_call.h (revision 16f6ccde74a6f8538c62f127f17207c75f4dba7a)
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 
163*e68cbbefSJuergen Gross extern int static_call_initialized;
164*e68cbbefSJuergen Gross 
16569e0ad37SNathan Chancellor extern int __init static_call_init(void);
166a945c834SPeter Zijlstra 
1677825451fSPeter Zijlstra extern void static_call_force_reinit(void);
1687825451fSPeter Zijlstra 
1699183c3f9SJosh Poimboeuf struct static_call_mod {
1709183c3f9SJosh Poimboeuf 	struct static_call_mod *next;
1719183c3f9SJosh Poimboeuf 	struct module *mod; /* for vmlinux, mod == NULL */
1729183c3f9SJosh Poimboeuf 	struct static_call_site *sites;
1739183c3f9SJosh Poimboeuf };
1749183c3f9SJosh Poimboeuf 
17573f44fe1SJosh Poimboeuf /* For finding the key associated with a trampoline */
17673f44fe1SJosh Poimboeuf struct static_call_tramp_key {
17773f44fe1SJosh Poimboeuf 	s32 tramp;
17873f44fe1SJosh Poimboeuf 	s32 key;
17973f44fe1SJosh Poimboeuf };
18073f44fe1SJosh Poimboeuf 
1819183c3f9SJosh Poimboeuf extern void __static_call_update(struct static_call_key *key, void *tramp, void *func);
1829183c3f9SJosh Poimboeuf extern int static_call_mod_init(struct module *mod);
1836333e8f7SPeter Zijlstra extern int static_call_text_reserved(void *start, void *end);
1849183c3f9SJosh Poimboeuf 
1853f2a8fc4SPeter Zijlstra extern long __static_call_return0(void);
1863f2a8fc4SPeter Zijlstra 
187df21c0d7SChristophe Leroy #define DEFINE_STATIC_CALL(name, _func)					\
1889183c3f9SJosh Poimboeuf 	DECLARE_STATIC_CALL(name, _func);				\
1899183c3f9SJosh Poimboeuf 	struct static_call_key STATIC_CALL_KEY(name) = {		\
190df21c0d7SChristophe Leroy 		.func = _func,						\
191a945c834SPeter Zijlstra 		.type = 1,						\
1929183c3f9SJosh Poimboeuf 	};								\
193df21c0d7SChristophe Leroy 	ARCH_DEFINE_STATIC_CALL_TRAMP(name, _func)
1949183c3f9SJosh Poimboeuf 
195452cddbfSPeter Zijlstra #define DEFINE_STATIC_CALL_NULL(name, _func)				\
196452cddbfSPeter Zijlstra 	DECLARE_STATIC_CALL(name, _func);				\
197452cddbfSPeter Zijlstra 	struct static_call_key STATIC_CALL_KEY(name) = {		\
198452cddbfSPeter Zijlstra 		.func = NULL,						\
199452cddbfSPeter Zijlstra 		.type = 1,						\
200452cddbfSPeter Zijlstra 	};								\
201452cddbfSPeter Zijlstra 	ARCH_DEFINE_STATIC_CALL_NULL_TRAMP(name)
202452cddbfSPeter Zijlstra 
2035517d500SChristophe Leroy #define DEFINE_STATIC_CALL_RET0(name, _func)				\
2045517d500SChristophe Leroy 	DECLARE_STATIC_CALL(name, _func);				\
2055517d500SChristophe Leroy 	struct static_call_key STATIC_CALL_KEY(name) = {		\
2065517d500SChristophe Leroy 		.func = __static_call_return0,				\
2075517d500SChristophe Leroy 		.type = 1,						\
2085517d500SChristophe Leroy 	};								\
2095517d500SChristophe Leroy 	ARCH_DEFINE_STATIC_CALL_RET0_TRAMP(name)
2105517d500SChristophe Leroy 
211452cddbfSPeter Zijlstra #define static_call_cond(name)	(void)__static_call(name)
2129183c3f9SJosh Poimboeuf 
2139183c3f9SJosh Poimboeuf #define EXPORT_STATIC_CALL(name)					\
2149183c3f9SJosh Poimboeuf 	EXPORT_SYMBOL(STATIC_CALL_KEY(name));				\
2159183c3f9SJosh Poimboeuf 	EXPORT_SYMBOL(STATIC_CALL_TRAMP(name))
2169183c3f9SJosh Poimboeuf #define EXPORT_STATIC_CALL_GPL(name)					\
2179183c3f9SJosh Poimboeuf 	EXPORT_SYMBOL_GPL(STATIC_CALL_KEY(name));			\
2189183c3f9SJosh Poimboeuf 	EXPORT_SYMBOL_GPL(STATIC_CALL_TRAMP(name))
2199183c3f9SJosh Poimboeuf 
22073f44fe1SJosh Poimboeuf /* Leave the key unexported, so modules can't change static call targets: */
22173f44fe1SJosh Poimboeuf #define EXPORT_STATIC_CALL_TRAMP(name)					\
22273f44fe1SJosh Poimboeuf 	EXPORT_SYMBOL(STATIC_CALL_TRAMP(name));				\
22373f44fe1SJosh Poimboeuf 	ARCH_ADD_TRAMP_KEY(name)
22473f44fe1SJosh Poimboeuf #define EXPORT_STATIC_CALL_TRAMP_GPL(name)				\
22573f44fe1SJosh Poimboeuf 	EXPORT_SYMBOL_GPL(STATIC_CALL_TRAMP(name));			\
22673f44fe1SJosh Poimboeuf 	ARCH_ADD_TRAMP_KEY(name)
22773f44fe1SJosh Poimboeuf 
2289183c3f9SJosh Poimboeuf #elif defined(CONFIG_HAVE_STATIC_CALL)
229115284d8SJosh Poimboeuf 
230*e68cbbefSJuergen Gross #define static_call_initialized 0
231*e68cbbefSJuergen Gross 
static_call_init(void)23269e0ad37SNathan Chancellor static inline int static_call_init(void) { return 0; }
233a945c834SPeter Zijlstra 
234df21c0d7SChristophe Leroy #define DEFINE_STATIC_CALL(name, _func)					\
235115284d8SJosh Poimboeuf 	DECLARE_STATIC_CALL(name, _func);				\
236115284d8SJosh Poimboeuf 	struct static_call_key STATIC_CALL_KEY(name) = {		\
237df21c0d7SChristophe Leroy 		.func = _func,						\
238115284d8SJosh Poimboeuf 	};								\
239df21c0d7SChristophe Leroy 	ARCH_DEFINE_STATIC_CALL_TRAMP(name, _func)
240115284d8SJosh Poimboeuf 
241452cddbfSPeter Zijlstra #define DEFINE_STATIC_CALL_NULL(name, _func)				\
242452cddbfSPeter Zijlstra 	DECLARE_STATIC_CALL(name, _func);				\
243452cddbfSPeter Zijlstra 	struct static_call_key STATIC_CALL_KEY(name) = {		\
244452cddbfSPeter Zijlstra 		.func = NULL,						\
245452cddbfSPeter Zijlstra 	};								\
246452cddbfSPeter Zijlstra 	ARCH_DEFINE_STATIC_CALL_NULL_TRAMP(name)
247452cddbfSPeter Zijlstra 
2485517d500SChristophe Leroy #define DEFINE_STATIC_CALL_RET0(name, _func)				\
2495517d500SChristophe Leroy 	DECLARE_STATIC_CALL(name, _func);				\
2505517d500SChristophe Leroy 	struct static_call_key STATIC_CALL_KEY(name) = {		\
2515517d500SChristophe Leroy 		.func = __static_call_return0,				\
2525517d500SChristophe Leroy 	};								\
2535517d500SChristophe Leroy 	ARCH_DEFINE_STATIC_CALL_RET0_TRAMP(name)
2546ea312d9SJuergen Gross 
255452cddbfSPeter Zijlstra #define static_call_cond(name)	(void)__static_call(name)
256115284d8SJosh Poimboeuf 
257115284d8SJosh Poimboeuf static inline
__static_call_update(struct static_call_key * key,void * tramp,void * func)258115284d8SJosh Poimboeuf void __static_call_update(struct static_call_key *key, void *tramp, void *func)
259115284d8SJosh Poimboeuf {
260115284d8SJosh Poimboeuf 	cpus_read_lock();
261115284d8SJosh Poimboeuf 	WRITE_ONCE(key->func, func);
2625b06fd3bSPeter Zijlstra 	arch_static_call_transform(NULL, tramp, func, false);
263115284d8SJosh Poimboeuf 	cpus_read_unlock();
264115284d8SJosh Poimboeuf }
265115284d8SJosh Poimboeuf 
static_call_text_reserved(void * start,void * end)2666333e8f7SPeter Zijlstra static inline int static_call_text_reserved(void *start, void *end)
2676333e8f7SPeter Zijlstra {
2686333e8f7SPeter Zijlstra 	return 0;
2696333e8f7SPeter Zijlstra }
2706333e8f7SPeter Zijlstra 
2718fd4dddaSChristophe Leroy extern long __static_call_return0(void);
2723f2a8fc4SPeter Zijlstra 
273115284d8SJosh Poimboeuf #define EXPORT_STATIC_CALL(name)					\
274115284d8SJosh Poimboeuf 	EXPORT_SYMBOL(STATIC_CALL_KEY(name));				\
275115284d8SJosh Poimboeuf 	EXPORT_SYMBOL(STATIC_CALL_TRAMP(name))
276115284d8SJosh Poimboeuf #define EXPORT_STATIC_CALL_GPL(name)					\
277115284d8SJosh Poimboeuf 	EXPORT_SYMBOL_GPL(STATIC_CALL_KEY(name));			\
278115284d8SJosh Poimboeuf 	EXPORT_SYMBOL_GPL(STATIC_CALL_TRAMP(name))
279115284d8SJosh Poimboeuf 
28073f44fe1SJosh Poimboeuf /* Leave the key unexported, so modules can't change static call targets: */
28173f44fe1SJosh Poimboeuf #define EXPORT_STATIC_CALL_TRAMP(name)					\
28273f44fe1SJosh Poimboeuf 	EXPORT_SYMBOL(STATIC_CALL_TRAMP(name))
28373f44fe1SJosh Poimboeuf #define EXPORT_STATIC_CALL_TRAMP_GPL(name)				\
28473f44fe1SJosh Poimboeuf 	EXPORT_SYMBOL_GPL(STATIC_CALL_TRAMP(name))
28573f44fe1SJosh Poimboeuf 
286115284d8SJosh Poimboeuf #else /* Generic implementation */
287115284d8SJosh Poimboeuf 
288*e68cbbefSJuergen Gross #define static_call_initialized 0
289*e68cbbefSJuergen Gross 
static_call_init(void)29069e0ad37SNathan Chancellor static inline int static_call_init(void) { return 0; }
291a945c834SPeter Zijlstra 
__static_call_return0(void)2923f2a8fc4SPeter Zijlstra static inline long __static_call_return0(void)
2933f2a8fc4SPeter Zijlstra {
2943f2a8fc4SPeter Zijlstra 	return 0;
2953f2a8fc4SPeter Zijlstra }
2963f2a8fc4SPeter Zijlstra 
29729fd0194SFrederic Weisbecker #define __DEFINE_STATIC_CALL(name, _func, _func_init)			\
298115284d8SJosh Poimboeuf 	DECLARE_STATIC_CALL(name, _func);				\
299115284d8SJosh Poimboeuf 	struct static_call_key STATIC_CALL_KEY(name) = {		\
30029fd0194SFrederic Weisbecker 		.func = _func_init,					\
301115284d8SJosh Poimboeuf 	}
302115284d8SJosh Poimboeuf 
303df21c0d7SChristophe Leroy #define DEFINE_STATIC_CALL(name, _func)					\
304df21c0d7SChristophe Leroy 	__DEFINE_STATIC_CALL(name, _func, _func)
305df21c0d7SChristophe Leroy 
306452cddbfSPeter Zijlstra #define DEFINE_STATIC_CALL_NULL(name, _func)				\
307df21c0d7SChristophe Leroy 	__DEFINE_STATIC_CALL(name, _func, NULL)
308452cddbfSPeter Zijlstra 
3095517d500SChristophe Leroy #define DEFINE_STATIC_CALL_RET0(name, _func)				\
3105517d500SChristophe Leroy 	__DEFINE_STATIC_CALL(name, _func, __static_call_return0)
3115517d500SChristophe Leroy 
__static_call_nop(void)312452cddbfSPeter Zijlstra static inline void __static_call_nop(void) { }
313452cddbfSPeter Zijlstra 
314452cddbfSPeter Zijlstra /*
315452cddbfSPeter Zijlstra  * This horrific hack takes care of two things:
316452cddbfSPeter Zijlstra  *
317452cddbfSPeter Zijlstra  *  - it ensures the compiler will only load the function pointer ONCE,
318452cddbfSPeter Zijlstra  *    which avoids a reload race.
319452cddbfSPeter Zijlstra  *
320452cddbfSPeter Zijlstra  *  - it ensures the argument evaluation is unconditional, similar
321452cddbfSPeter Zijlstra  *    to the HAVE_STATIC_CALL variant.
322452cddbfSPeter Zijlstra  *
323452cddbfSPeter Zijlstra  * Sadly current GCC/Clang (10 for both) do not optimize this properly
324452cddbfSPeter Zijlstra  * and will emit an indirect call for the NULL case :-(
325452cddbfSPeter Zijlstra  */
326452cddbfSPeter Zijlstra #define __static_call_cond(name)					\
327452cddbfSPeter Zijlstra ({									\
328452cddbfSPeter Zijlstra 	void *func = READ_ONCE(STATIC_CALL_KEY(name).func);		\
329452cddbfSPeter Zijlstra 	if (!func)							\
330452cddbfSPeter Zijlstra 		func = &__static_call_nop;				\
331452cddbfSPeter Zijlstra 	(typeof(STATIC_CALL_TRAMP(name))*)func;				\
332452cddbfSPeter Zijlstra })
333452cddbfSPeter Zijlstra 
334452cddbfSPeter Zijlstra #define static_call_cond(name)	(void)__static_call_cond(name)
335452cddbfSPeter Zijlstra 
336115284d8SJosh Poimboeuf static inline
__static_call_update(struct static_call_key * key,void * tramp,void * func)337115284d8SJosh Poimboeuf void __static_call_update(struct static_call_key *key, void *tramp, void *func)
338115284d8SJosh Poimboeuf {
339115284d8SJosh Poimboeuf 	WRITE_ONCE(key->func, func);
340115284d8SJosh Poimboeuf }
341115284d8SJosh Poimboeuf 
static_call_text_reserved(void * start,void * end)3426333e8f7SPeter Zijlstra static inline int static_call_text_reserved(void *start, void *end)
3436333e8f7SPeter Zijlstra {
3446333e8f7SPeter Zijlstra 	return 0;
3456333e8f7SPeter Zijlstra }
3466333e8f7SPeter Zijlstra 
347115284d8SJosh Poimboeuf #define EXPORT_STATIC_CALL(name)	EXPORT_SYMBOL(STATIC_CALL_KEY(name))
348115284d8SJosh Poimboeuf #define EXPORT_STATIC_CALL_GPL(name)	EXPORT_SYMBOL_GPL(STATIC_CALL_KEY(name))
349115284d8SJosh Poimboeuf 
350115284d8SJosh Poimboeuf #endif /* CONFIG_HAVE_STATIC_CALL */
351115284d8SJosh Poimboeuf 
352115284d8SJosh Poimboeuf #endif /* _LINUX_STATIC_CALL_H */
353