2020-08-18 13:57:41 +00:00
|
|
|
/* SPDX-License-Identifier: GPL-2.0 */
|
|
|
|
#ifndef _LINUX_STATIC_CALL_H
|
|
|
|
#define _LINUX_STATIC_CALL_H
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Static call support
|
|
|
|
*
|
|
|
|
* Static calls use code patching to hard-code function pointers into direct
|
|
|
|
* branch instructions. They give the flexibility of function pointers, but
|
|
|
|
* with improved performance. This is especially important for cases where
|
|
|
|
* retpolines would otherwise be used, as retpolines can significantly impact
|
|
|
|
* performance.
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* API overview:
|
|
|
|
*
|
|
|
|
* DECLARE_STATIC_CALL(name, func);
|
|
|
|
* DEFINE_STATIC_CALL(name, func);
|
2020-08-18 13:57:48 +00:00
|
|
|
* DEFINE_STATIC_CALL_NULL(name, typename);
|
2021-08-05 15:49:54 +00:00
|
|
|
* DEFINE_STATIC_CALL_RET0(name, typename);
|
|
|
|
*
|
|
|
|
* __static_call_return0;
|
|
|
|
*
|
2020-08-18 13:57:41 +00:00
|
|
|
* static_call(name)(args...);
|
2020-08-18 13:57:48 +00:00
|
|
|
* static_call_cond(name)(args...);
|
2020-08-18 13:57:41 +00:00
|
|
|
* static_call_update(name, func);
|
2021-03-11 14:23:08 +00:00
|
|
|
* static_call_query(name);
|
2020-08-18 13:57:41 +00:00
|
|
|
*
|
2021-08-05 15:49:54 +00:00
|
|
|
* EXPORT_STATIC_CALL{,_TRAMP}{,_GPL}()
|
|
|
|
*
|
2020-08-18 13:57:41 +00:00
|
|
|
* Usage example:
|
|
|
|
*
|
|
|
|
* # Start with the following functions (with identical prototypes):
|
|
|
|
* int func_a(int arg1, int arg2);
|
|
|
|
* int func_b(int arg1, int arg2);
|
|
|
|
*
|
|
|
|
* # Define a 'my_name' reference, associated with func_a() by default
|
|
|
|
* DEFINE_STATIC_CALL(my_name, func_a);
|
|
|
|
*
|
|
|
|
* # Call func_a()
|
|
|
|
* static_call(my_name)(arg1, arg2);
|
|
|
|
*
|
|
|
|
* # Update 'my_name' to point to func_b()
|
|
|
|
* static_call_update(my_name, &func_b);
|
|
|
|
*
|
|
|
|
* # Call func_b()
|
|
|
|
* static_call(my_name)(arg1, arg2);
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* Implementation details:
|
|
|
|
*
|
|
|
|
* This requires some arch-specific code (CONFIG_HAVE_STATIC_CALL).
|
|
|
|
* Otherwise basic indirect calls are used (with function pointers).
|
|
|
|
*
|
|
|
|
* Each static_call() site calls into a trampoline associated with the name.
|
|
|
|
* The trampoline has a direct branch to the default function. Updates to a
|
|
|
|
* name will modify the trampoline's branch destination.
|
|
|
|
*
|
|
|
|
* If the arch has CONFIG_HAVE_STATIC_CALL_INLINE, then the call sites
|
|
|
|
* themselves will be patched at runtime to call the functions directly,
|
|
|
|
* rather than calling through the trampoline. This requires objtool or a
|
|
|
|
* compiler plugin to detect all the static_call() sites and annotate them
|
|
|
|
* in the .static_call_sites section.
|
2020-08-18 13:57:48 +00:00
|
|
|
*
|
|
|
|
*
|
|
|
|
* Notes on NULL function pointers:
|
|
|
|
*
|
|
|
|
* Static_call()s support NULL functions, with many of the caveats that
|
|
|
|
* regular function pointers have.
|
|
|
|
*
|
|
|
|
* Clearly calling a NULL function pointer is 'BAD', so too for
|
|
|
|
* static_call()s (although when HAVE_STATIC_CALL it might not be immediately
|
|
|
|
* fatal). A NULL static_call can be the result of:
|
|
|
|
*
|
|
|
|
* DECLARE_STATIC_CALL_NULL(my_static_call, void (*)(int));
|
|
|
|
*
|
|
|
|
* which is equivalent to declaring a NULL function pointer with just a
|
|
|
|
* typename:
|
|
|
|
*
|
|
|
|
* void (*my_func_ptr)(int arg1) = NULL;
|
|
|
|
*
|
|
|
|
* or using static_call_update() with a NULL function. In both cases the
|
|
|
|
* HAVE_STATIC_CALL implementation will patch the trampoline with a RET
|
|
|
|
* instruction, instead of an immediate tail-call JMP. HAVE_STATIC_CALL_INLINE
|
|
|
|
* architectures can patch the trampoline call to a NOP.
|
|
|
|
*
|
|
|
|
* In all cases, any argument evaluation is unconditional. Unlike a regular
|
|
|
|
* conditional function pointer call:
|
|
|
|
*
|
|
|
|
* if (my_func_ptr)
|
|
|
|
* my_func_ptr(arg1)
|
|
|
|
*
|
|
|
|
* where the argument evaludation also depends on the pointer value.
|
|
|
|
*
|
|
|
|
* When calling a static_call that can be NULL, use:
|
|
|
|
*
|
|
|
|
* static_call_cond(name)(arg1);
|
|
|
|
*
|
|
|
|
* which will include the required value tests to avoid NULL-pointer
|
|
|
|
* dereferences.
|
2021-03-11 14:23:08 +00:00
|
|
|
*
|
|
|
|
* To query which function is currently set to be called, use:
|
|
|
|
*
|
|
|
|
* func = static_call_query(name);
|
2021-08-05 15:49:54 +00:00
|
|
|
*
|
|
|
|
*
|
|
|
|
* DEFINE_STATIC_CALL_RET0 / __static_call_return0:
|
|
|
|
*
|
|
|
|
* Just like how DEFINE_STATIC_CALL_NULL() / static_call_cond() optimize the
|
|
|
|
* conditional void function call, DEFINE_STATIC_CALL_RET0 /
|
|
|
|
* __static_call_return0 optimize the do nothing return 0 function.
|
|
|
|
*
|
|
|
|
* This feature is strictly UB per the C standard (since it casts a function
|
|
|
|
* pointer to a different signature) and relies on the architecture ABI to
|
|
|
|
* make things work. In particular it relies on Caller Stack-cleanup and the
|
|
|
|
* whole return register being clobbered for short return values. All normal
|
|
|
|
* CDECL style ABIs conform.
|
|
|
|
*
|
|
|
|
* In particular the x86_64 implementation replaces the 5 byte CALL
|
|
|
|
* instruction at the callsite with a 5 byte clear of the RAX register,
|
|
|
|
* completely eliding any function call overhead.
|
|
|
|
*
|
|
|
|
* Notably argument setup is unconditional.
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* EXPORT_STATIC_CALL() vs EXPORT_STATIC_CALL_TRAMP():
|
|
|
|
*
|
|
|
|
* The difference is that the _TRAMP variant tries to only export the
|
|
|
|
* trampoline with the result that a module can use static_call{,_cond}() but
|
|
|
|
* not static_call_update().
|
|
|
|
*
|
2020-08-18 13:57:41 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <linux/types.h>
|
|
|
|
#include <linux/cpu.h>
|
|
|
|
#include <linux/static_call_types.h>
|
|
|
|
|
|
|
|
#ifdef CONFIG_HAVE_STATIC_CALL
|
|
|
|
#include <asm/static_call.h>
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Either @site or @tramp can be NULL.
|
|
|
|
*/
|
2020-08-18 13:57:49 +00:00
|
|
|
extern void arch_static_call_transform(void *site, void *tramp, void *func, bool tail);
|
2020-08-18 13:57:41 +00:00
|
|
|
|
|
|
|
#define STATIC_CALL_TRAMP_ADDR(name) &STATIC_CALL_TRAMP(name)
|
|
|
|
|
|
|
|
#else
|
|
|
|
#define STATIC_CALL_TRAMP_ADDR(name) NULL
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#define static_call_update(name, func) \
|
|
|
|
({ \
|
2021-03-23 15:49:03 +00:00
|
|
|
typeof(&STATIC_CALL_TRAMP(name)) __F = (func); \
|
2020-08-18 13:57:41 +00:00
|
|
|
__static_call_update(&STATIC_CALL_KEY(name), \
|
2021-03-23 15:49:03 +00:00
|
|
|
STATIC_CALL_TRAMP_ADDR(name), __F); \
|
2020-08-18 13:57:41 +00:00
|
|
|
})
|
|
|
|
|
2021-03-11 14:23:08 +00:00
|
|
|
#define static_call_query(name) (READ_ONCE(STATIC_CALL_KEY(name).func))
|
|
|
|
|
2020-08-18 13:57:42 +00:00
|
|
|
#ifdef CONFIG_HAVE_STATIC_CALL_INLINE
|
|
|
|
|
2020-09-28 23:09:39 +00:00
|
|
|
extern int __init static_call_init(void);
|
2020-08-18 13:57:51 +00:00
|
|
|
|
2020-08-18 13:57:42 +00:00
|
|
|
struct static_call_mod {
|
|
|
|
struct static_call_mod *next;
|
|
|
|
struct module *mod; /* for vmlinux, mod == NULL */
|
|
|
|
struct static_call_site *sites;
|
|
|
|
};
|
|
|
|
|
2021-01-27 23:18:37 +00:00
|
|
|
/* For finding the key associated with a trampoline */
|
|
|
|
struct static_call_tramp_key {
|
|
|
|
s32 tramp;
|
|
|
|
s32 key;
|
|
|
|
};
|
|
|
|
|
2020-08-18 13:57:42 +00:00
|
|
|
extern void __static_call_update(struct static_call_key *key, void *tramp, void *func);
|
|
|
|
extern int static_call_mod_init(struct module *mod);
|
2020-08-18 13:57:43 +00:00
|
|
|
extern int static_call_text_reserved(void *start, void *end);
|
2020-08-18 13:57:42 +00:00
|
|
|
|
2021-01-18 14:12:16 +00:00
|
|
|
extern long __static_call_return0(void);
|
|
|
|
|
2021-01-18 14:12:17 +00:00
|
|
|
#define __DEFINE_STATIC_CALL(name, _func, _func_init) \
|
2020-08-18 13:57:42 +00:00
|
|
|
DECLARE_STATIC_CALL(name, _func); \
|
|
|
|
struct static_call_key STATIC_CALL_KEY(name) = { \
|
2021-01-18 14:12:17 +00:00
|
|
|
.func = _func_init, \
|
2020-08-18 13:57:51 +00:00
|
|
|
.type = 1, \
|
2020-08-18 13:57:42 +00:00
|
|
|
}; \
|
2021-01-18 14:12:17 +00:00
|
|
|
ARCH_DEFINE_STATIC_CALL_TRAMP(name, _func_init)
|
2020-08-18 13:57:42 +00:00
|
|
|
|
2020-08-18 13:57:48 +00:00
|
|
|
#define DEFINE_STATIC_CALL_NULL(name, _func) \
|
|
|
|
DECLARE_STATIC_CALL(name, _func); \
|
|
|
|
struct static_call_key STATIC_CALL_KEY(name) = { \
|
|
|
|
.func = NULL, \
|
|
|
|
.type = 1, \
|
|
|
|
}; \
|
|
|
|
ARCH_DEFINE_STATIC_CALL_NULL_TRAMP(name)
|
|
|
|
|
|
|
|
#define static_call_cond(name) (void)__static_call(name)
|
2020-08-18 13:57:42 +00:00
|
|
|
|
|
|
|
#define EXPORT_STATIC_CALL(name) \
|
|
|
|
EXPORT_SYMBOL(STATIC_CALL_KEY(name)); \
|
|
|
|
EXPORT_SYMBOL(STATIC_CALL_TRAMP(name))
|
|
|
|
#define EXPORT_STATIC_CALL_GPL(name) \
|
|
|
|
EXPORT_SYMBOL_GPL(STATIC_CALL_KEY(name)); \
|
|
|
|
EXPORT_SYMBOL_GPL(STATIC_CALL_TRAMP(name))
|
|
|
|
|
2021-01-27 23:18:37 +00:00
|
|
|
/* Leave the key unexported, so modules can't change static call targets: */
|
|
|
|
#define EXPORT_STATIC_CALL_TRAMP(name) \
|
|
|
|
EXPORT_SYMBOL(STATIC_CALL_TRAMP(name)); \
|
|
|
|
ARCH_ADD_TRAMP_KEY(name)
|
|
|
|
#define EXPORT_STATIC_CALL_TRAMP_GPL(name) \
|
|
|
|
EXPORT_SYMBOL_GPL(STATIC_CALL_TRAMP(name)); \
|
|
|
|
ARCH_ADD_TRAMP_KEY(name)
|
|
|
|
|
2020-08-18 13:57:42 +00:00
|
|
|
#elif defined(CONFIG_HAVE_STATIC_CALL)
|
2020-08-18 13:57:41 +00:00
|
|
|
|
2020-09-28 23:09:39 +00:00
|
|
|
static inline int static_call_init(void) { return 0; }
|
2020-08-18 13:57:51 +00:00
|
|
|
|
2021-01-18 14:12:17 +00:00
|
|
|
#define __DEFINE_STATIC_CALL(name, _func, _func_init) \
|
2020-08-18 13:57:41 +00:00
|
|
|
DECLARE_STATIC_CALL(name, _func); \
|
|
|
|
struct static_call_key STATIC_CALL_KEY(name) = { \
|
2021-01-18 14:12:17 +00:00
|
|
|
.func = _func_init, \
|
2020-08-18 13:57:41 +00:00
|
|
|
}; \
|
2021-01-18 14:12:17 +00:00
|
|
|
ARCH_DEFINE_STATIC_CALL_TRAMP(name, _func_init)
|
2020-08-18 13:57:41 +00:00
|
|
|
|
2020-08-18 13:57:48 +00:00
|
|
|
#define DEFINE_STATIC_CALL_NULL(name, _func) \
|
|
|
|
DECLARE_STATIC_CALL(name, _func); \
|
|
|
|
struct static_call_key STATIC_CALL_KEY(name) = { \
|
|
|
|
.func = NULL, \
|
|
|
|
}; \
|
|
|
|
ARCH_DEFINE_STATIC_CALL_NULL_TRAMP(name)
|
|
|
|
|
2021-03-11 14:23:08 +00:00
|
|
|
|
2020-08-18 13:57:48 +00:00
|
|
|
#define static_call_cond(name) (void)__static_call(name)
|
2020-08-18 13:57:41 +00:00
|
|
|
|
|
|
|
static inline
|
|
|
|
void __static_call_update(struct static_call_key *key, void *tramp, void *func)
|
|
|
|
{
|
|
|
|
cpus_read_lock();
|
|
|
|
WRITE_ONCE(key->func, func);
|
2020-08-18 13:57:49 +00:00
|
|
|
arch_static_call_transform(NULL, tramp, func, false);
|
2020-08-18 13:57:41 +00:00
|
|
|
cpus_read_unlock();
|
|
|
|
}
|
|
|
|
|
2020-08-18 13:57:43 +00:00
|
|
|
static inline int static_call_text_reserved(void *start, void *end)
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2021-01-18 14:12:16 +00:00
|
|
|
static inline long __static_call_return0(void)
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2020-08-18 13:57:41 +00:00
|
|
|
#define EXPORT_STATIC_CALL(name) \
|
|
|
|
EXPORT_SYMBOL(STATIC_CALL_KEY(name)); \
|
|
|
|
EXPORT_SYMBOL(STATIC_CALL_TRAMP(name))
|
|
|
|
#define EXPORT_STATIC_CALL_GPL(name) \
|
|
|
|
EXPORT_SYMBOL_GPL(STATIC_CALL_KEY(name)); \
|
|
|
|
EXPORT_SYMBOL_GPL(STATIC_CALL_TRAMP(name))
|
|
|
|
|
2021-01-27 23:18:37 +00:00
|
|
|
/* Leave the key unexported, so modules can't change static call targets: */
|
|
|
|
#define EXPORT_STATIC_CALL_TRAMP(name) \
|
|
|
|
EXPORT_SYMBOL(STATIC_CALL_TRAMP(name))
|
|
|
|
#define EXPORT_STATIC_CALL_TRAMP_GPL(name) \
|
|
|
|
EXPORT_SYMBOL_GPL(STATIC_CALL_TRAMP(name))
|
|
|
|
|
2020-08-18 13:57:41 +00:00
|
|
|
#else /* Generic implementation */
|
|
|
|
|
2020-09-28 23:09:39 +00:00
|
|
|
static inline int static_call_init(void) { return 0; }
|
2020-08-18 13:57:51 +00:00
|
|
|
|
2021-01-18 14:12:16 +00:00
|
|
|
static inline long __static_call_return0(void)
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2021-01-18 14:12:17 +00:00
|
|
|
#define __DEFINE_STATIC_CALL(name, _func, _func_init) \
|
2020-08-18 13:57:41 +00:00
|
|
|
DECLARE_STATIC_CALL(name, _func); \
|
|
|
|
struct static_call_key STATIC_CALL_KEY(name) = { \
|
2021-01-18 14:12:17 +00:00
|
|
|
.func = _func_init, \
|
2020-08-18 13:57:41 +00:00
|
|
|
}
|
|
|
|
|
2020-08-18 13:57:48 +00:00
|
|
|
#define DEFINE_STATIC_CALL_NULL(name, _func) \
|
|
|
|
DECLARE_STATIC_CALL(name, _func); \
|
|
|
|
struct static_call_key STATIC_CALL_KEY(name) = { \
|
|
|
|
.func = NULL, \
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline void __static_call_nop(void) { }
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This horrific hack takes care of two things:
|
|
|
|
*
|
|
|
|
* - it ensures the compiler will only load the function pointer ONCE,
|
|
|
|
* which avoids a reload race.
|
|
|
|
*
|
|
|
|
* - it ensures the argument evaluation is unconditional, similar
|
|
|
|
* to the HAVE_STATIC_CALL variant.
|
|
|
|
*
|
|
|
|
* Sadly current GCC/Clang (10 for both) do not optimize this properly
|
|
|
|
* and will emit an indirect call for the NULL case :-(
|
|
|
|
*/
|
|
|
|
#define __static_call_cond(name) \
|
|
|
|
({ \
|
|
|
|
void *func = READ_ONCE(STATIC_CALL_KEY(name).func); \
|
|
|
|
if (!func) \
|
|
|
|
func = &__static_call_nop; \
|
|
|
|
(typeof(STATIC_CALL_TRAMP(name))*)func; \
|
|
|
|
})
|
|
|
|
|
|
|
|
#define static_call_cond(name) (void)__static_call_cond(name)
|
|
|
|
|
2020-08-18 13:57:41 +00:00
|
|
|
static inline
|
|
|
|
void __static_call_update(struct static_call_key *key, void *tramp, void *func)
|
|
|
|
{
|
|
|
|
WRITE_ONCE(key->func, func);
|
|
|
|
}
|
|
|
|
|
2020-08-18 13:57:43 +00:00
|
|
|
static inline int static_call_text_reserved(void *start, void *end)
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2020-08-18 13:57:41 +00:00
|
|
|
#define EXPORT_STATIC_CALL(name) EXPORT_SYMBOL(STATIC_CALL_KEY(name))
|
|
|
|
#define EXPORT_STATIC_CALL_GPL(name) EXPORT_SYMBOL_GPL(STATIC_CALL_KEY(name))
|
|
|
|
|
|
|
|
#endif /* CONFIG_HAVE_STATIC_CALL */
|
|
|
|
|
2021-01-18 14:12:17 +00:00
|
|
|
#define DEFINE_STATIC_CALL(name, _func) \
|
|
|
|
__DEFINE_STATIC_CALL(name, _func, _func)
|
|
|
|
|
|
|
|
#define DEFINE_STATIC_CALL_RET0(name, _func) \
|
|
|
|
__DEFINE_STATIC_CALL(name, _func, __static_call_return0)
|
|
|
|
|
2020-08-18 13:57:41 +00:00
|
|
|
#endif /* _LINUX_STATIC_CALL_H */
|