Add static_call
Subsystems (15)
Peter Zijlstra
maintainerPATCH v6 11/17 static_call: Simple self-test
In reply to Peter Zijlstra--- arch/Kconfig | 6 ++++++ kernel/static_call.c | 28 ++++++++++++++++++++++++++++ 2 files changed, 34 insertions(+)
arch/Kconfig +6 -0
@@ -103,6 +103,12 @@ config STATIC_KEYS_SELFTEST help Boot time self-test of the branch patching code. +config STATIC_CALL_SELFTEST + bool "Static call selftest" + depends on HAVE_STATIC_CALL + help + Boot time self-test of the call patching code. + config OPTPROBES def_bool y depends on KPROBES && HAVE_OPTPROBES
kernel/static_call.c +28 -0
@@ -364,3 +364,31 @@ static void __init static_call_init(void#endif }early_initcall (static_call_init); + +#ifdef CONFIG_STATIC_CALL_SELFTEST + +static int func_a (int x) +{ +return x+1 ; +} + +static int func_b (int x) +{ +return x+2 ; +} + +DEFINE_STATIC_CALL (sc_selftest, func_a); + +static int __init test_static_call_init (void ) +{ +WARN_ON (static_call (sc_selftest)(2 ) !=3 ); +static_call_update (sc_selftest, &func_b); +WARN_ON (static_call (sc_selftest)(2 ) !=4 ); +static_call_update (sc_selftest, &func_a); +WARN_ON (static_call (sc_selftest)(2 ) !=3 ); + +return 0 ; +} +early_initcall (test_static_call_init); + +#endif /* CONFIG_STATIC_CALL_SELFTEST */
Peter Zijlstra
maintainerPATCH v6 12/17 x86/alternatives: Teach text_poke_bp() to emulate RET
In reply to Peter ZijlstraFuture patches will need to poke a RET instruction, provide the infrastructure required for this.
--- arch/x86/include/asm/text-patching.h | 19 +++++++++++++++++++ arch/x86/kernel/alternative.c | 5 +++++ 2 files changed, 24 insertions(+)
arch/x86/include/asm/text-patching.h +19 -0
@@ -53,6 +53,9 @@ extern void text_poke_finish(void);#define INT3_INSN_SIZE 1#define INT3_INSN_OPCODE 0xCC +#define RET_INSN_SIZE 1 +#define RET_INSN_OPCODE 0xC3 +#define CALL_INSN_SIZE 5#define CALL_INSN_OPCODE 0xE8
@@ -73,6 +76,7 @@ static __always_inline int text_opcode_sswitch (opcode) {__CASE (INT3 ); +__CASE (RET );__CASE (CALL );__CASE (JMP32 );__CASE (JMP8 );
@@ -141,11 +145,26 @@ void int3_emulate_push(struct pt_regs *r }static __always_inline +unsigned long int3_emulate_pop (struct pt_regs *regs) +{ +unsigned long val = *(unsigned long *)regs->sp; + regs->sp +=sizeof (unsigned long ); +return val; +} + +static __always_inline void int3_emulate_call(struct pt_regs *regs, unsigned long func) {int3_emulate_push (regs, regs->ip -INT3_INSN_SIZE +CALL_INSN_SIZE );int3_emulate_jmp (regs, func); } + +static __always_inline +void int3_emulate_ret(struct pt_regs *regs) +{ +unsigned long ip =int3_emulate_pop (regs); +int3_emulate_jmp (regs, ip); +}#endif /* !CONFIG_UML_X86 */ #endif /* _ASM_X86_TEXT_PATCHING_H */
arch/x86/kernel/alternative.c +5 -0
@@ -1099,6 +1099,10 @@ int noinstr poke_int3_handler(struct pt_*/ goto out_put; +case RET_INSN_OPCODE : +int3_emulate_ret (regs); +break ; +case CALL_INSN_OPCODE :int3_emulate_call (regs, (long )ip + tp->rel32);break ;
@@ -1240,6 +1244,7 @@ static void text_poke_loc_init(struct teswitch (tp->opcode) {case INT3_INSN_OPCODE : +case RET_INSN_OPCODE :break ;case CALL_INSN_OPCODE :
Peter Zijlstra
maintainerPATCH v6 9/17 x86/static_call: Add out-of-line static call implementation
In reply to Peter ZijlstraFrom: Josh Poimboeuf <[email protected]>
Add the x86 out-of-line static call implementation. For each key, a permanent trampoline is created which is the destination for all static calls for the given key. The trampoline has a direct jump which gets patched by static_call_update() when the destination function changes.
--- arch/x86/Kconfig | 1 + arch/x86/include/asm/static_call.h | 23 +++++++++++++++++++++++ arch/x86/kernel/Makefile | 1 + arch/x86/kernel/static_call.c | 31 +++++++++++++++++++++++++++++++ 4 files changed, 56 insertions(+) create mode 100644 arch/x86/include/asm/static_call.h create mode 100644 arch/x86/kernel/static_call.c
arch/x86/Kconfig +1 -0
@@ -211,6 +211,7 @@ config X86 select HAVE_FUNCTION_ARG_ACCESS_API select HAVE_STACKPROTECTOR if CC_HAS_SANE_STACKPROTECTOR select HAVE_STACK_VALIDATION if X86_64 + select HAVE_STATIC_CALL select HAVE_RSEQ select HAVE_SYSCALL_TRACEPOINTS select HAVE_UNSTABLE_SCHED_CLOCK
arch/x86/include/asm/static_call.h new file +23 -0
@@ -0,0 +1,23 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +#ifndef _ASM_STATIC_CALL_H +#define _ASM_STATIC_CALL_H + +#include <asm/text-patching.h> + +/* +* For CONFIG_HAVE_STATIC_CALL, this is a permanent trampoline which +* does a direct jump to the function. The direct jump gets patched by +* static_call_update(). +*/ +#define ARCH_DEFINE_STATIC_CALL_TRAMP (name, func) \ + asm(".pushsection .text, \"ax\" \n" \ + ".align 4 \n" \ + ".globl " STATIC_CALL_TRAMP_STR(name) " \n" \ + STATIC_CALL_TRAMP_STR(name) ": \n" \ + " .byte 0xe9 # jmp.d32 \n" \ + " .long " #func " - (. + 4) \n" \ + ".type " STATIC_CALL_TRAMP_STR(name) ", @function \n" \ + ".size " STATIC_CALL_TRAMP_STR(name) ", . - " STATIC_CALL_TRAMP_STR(name) " \n" \ + ".popsection \n") + +#endif /* _ASM_STATIC_CALL_H */
arch/x86/kernel/Makefile +1 -0
@@ -68,6 +68,7 @@ obj-y += tsc.o tsc_msr.o io_delay.o rt obj-y += pci-iommu_table.o obj-y += resource.o obj-y += irqflags.o +obj-y += static_call.o obj-y += process.o obj-y += fpu/
arch/x86/kernel/static_call.c new file +31 -0
@@ -0,0 +1,31 @@ +// SPDX-License-Identifier: GPL-2.0 +#include <linux/static_call.h> +#include <linux/memory.h> +#include <linux/bug.h> +#include <asm/text-patching.h> + +static void __static_call_transform (void *insn,u8 opcode,void *func) +{ +const void *code =text_gen_insn (opcode, (long )insn, (long )func); + +if (WARN_ONCE (*(u8 *)insn != opcode, +"unexpected static call insn opcode 0x%x at %pS\n" , + opcode, insn)) +return ; + +if (memcmp (insn, code,CALL_INSN_SIZE ) ==0 ) +return ; + +text_poke_bp (insn, code,CALL_INSN_SIZE ,NULL ); +} + +void arch_static_call_transform (void *site,void *tramp,void *func) +{ +mutex_lock (&text_mutex); + +if (tramp) +__static_call_transform (tramp,JMP32_INSN_OPCODE , func); + +mutex_unlock (&text_mutex); +} +EXPORT_SYMBOL_GPL (arch_static_call_transform);
Peter Zijlstra
maintainerPATCH v6 5/17 compiler.h: Make __ADDRESSABLE() symbol truly unique
In reply to Peter ZijlstraFrom: Josh Poimboeuf <[email protected]>
The __ADDRESSABLE() macro uses the __LINE__ macro to create a temporary symbol which has a unique name. However, if the macro is used multiple times from within another macro, the line number will always be the same, resulting in duplicate symbols.
Make the temporary symbols truly unique by using __UNIQUE_ID instead of __LINE__.
--- include/linux/compiler.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
include/linux/compiler.h +1 -1
@@ -299,7 +299,7 @@ unsigned long read_word_at_a_time(const*/ #define __ADDRESSABLE (sym) \ static void * __section(.discard.addressable) __used \ - __PASTE(__addressable_##sym, __LINE__) = (void *)&sym; + __UNIQUE_ID(__PASTE(__addressable_,sym)) = (void *)&sym;/** * offset_to_ptr - convert a relative memory offset to an absolute pointer
Peter Zijlstra
maintainerPATCH v6 4/17 jump_label,module: Fix module lifetime for __jump_label_mod_text_reserved
In reply to Peter ZijlstraNothing ensures the module exists while we're iterating mod->jump_entries in __jump_label_mod_text_reserved(), take a module reference to ensure the module sticks around.
--- kernel/jump_label.c | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-)
kernel/jump_label.c +8 -2
@@ -539,19 +539,25 @@ static void static_key_set_mod(struct ststatic int __jump_label_mod_text_reserved (void *start,void *end) {struct module *mod; +int ret;preempt_disable (); mod =__module_text_address ((unsigned long )start);WARN_ON_ONCE (__module_text_address ((unsigned long )end) != mod); +if (!try_module_get (mod)) + mod =NULL ;preempt_enable ();if (!mod)return 0 ; - -return __jump_label_text_reserved (mod->jump_entries, + ret =__jump_label_text_reserved (mod->jump_entries, mod->jump_entries + mod->num_jump_entries, start, end); + +module_put (mod); + +return ret; }static void __jump_label_mod_update (struct static_key *key)
Peter Zijlstra
maintainerPATCH v6 16/17 tracepoint: Optimize using static_call()
In reply to Peter ZijlstraFrom: "Steven Rostedt (VMware)" <[email protected]>
Currently the tracepoint site will iterate a vector and issue indirect calls to however many handlers are registered (ie. the vector is long).
Using static_call() it is possible to optimize this for the common case of only having a single handler registered. In this case the static_call() can directly call this handler. Otherwise, if the vector is longer than 1, call a function that iterates the whole vector like the current code.
--- include/linux/tracepoint-defs.h | 5 ++ include/linux/tracepoint.h | 86 ++++++++++++++++++++++++++++------------ include/trace/define_trace.h | 14 +++--- kernel/tracepoint.c | 25 +++++++++-- 4 files changed, 94 insertions(+), 36 deletions(-)
include/linux/tracepoint-defs.h +5 -0
@@ -11,6 +11,8 @@#include <linux/atomic.h> #include <linux/static_key.h> +struct static_call_key ; +struct trace_print_flags {unsigned long mask;const char *name;
@@ -30,6 +32,9 @@ struct tracepoint_func {struct tracepoint {const char *name;/* Tracepoint name */ struct static_key key; +struct static_call_key *static_call_key; +void *static_call_tramp; +void *iterator;int (*regfunc)(void );void (*unregfunc)(void );struct tracepoint_func __rcu *funcs;
include/linux/tracepoint.h +61 -25
153 lines not on this page · raw message
include/trace/define_trace.h +7 -7
@@ -25,7 +25,7 @@#undef TRACE_EVENT#define TRACE_EVENT (name, proto, args, tstruct, assign, print) \ - DEFINE_TRACE(name) + DEFINE_TRACE(name, PARAMS(proto), PARAMS(args))#undef TRACE_EVENT_CONDITION#define TRACE_EVENT_CONDITION (name, proto, args, cond, tstruct, assign, print) \
@@ -39,12 +39,12 @@#undef TRACE_EVENT_FN#define TRACE_EVENT_FN (name, proto, args, tstruct, \ assign, print, reg, unreg) \ - DEFINE_TRACE_FN(name, reg, unreg) + DEFINE_TRACE_FN(name, reg, unreg, PARAMS(proto), PARAMS(args))#undef TRACE_EVENT_FN_COND#define TRACE_EVENT_FN_COND (name, proto, args, cond, tstruct, \ assign, print, reg, unreg) \ - DEFINE_TRACE_FN(name, reg, unreg) + DEFINE_TRACE_FN(name, reg, unreg, PARAMS(proto), PARAMS(args))#undef TRACE_EVENT_NOP#define TRACE_EVENT_NOP (name, proto, args, struct, assign, print)
@@ -54,15 +54,15 @@#undef DEFINE_EVENT#define DEFINE_EVENT (template, name, proto, args) \ - DEFINE_TRACE(name) + DEFINE_TRACE(name, PARAMS(proto), PARAMS(args))#undef DEFINE_EVENT_FN#define DEFINE_EVENT_FN (template, name, proto, args, reg, unreg) \ - DEFINE_TRACE_FN(name, reg, unreg) + DEFINE_TRACE_FN(name, reg, unreg, PARAMS(proto), PARAMS(args))#undef DEFINE_EVENT_PRINT#define DEFINE_EVENT_PRINT (template, name, proto, args, print) \ - DEFINE_TRACE(name) + DEFINE_TRACE(name, PARAMS(proto), PARAMS(args))#undef DEFINE_EVENT_CONDITION#define DEFINE_EVENT_CONDITION (template, name, proto, args, cond) \
@@ -70,7 +70,7 @@#undef DECLARE_TRACE#define DECLARE_TRACE (name, proto, args) \ - DEFINE_TRACE(name) + DEFINE_TRACE(name, PARAMS(proto), PARAMS(args))#undef TRACE_INCLUDE#undef __TRACE_INCLUDE
kernel/tracepoint.c +21 -4
@@ -221,6 +221,20 @@ static void *func_remove(struct tracepoireturn old; } +static void tracepoint_update_call (struct tracepoint *tp,struct tracepoint_func *tp_funcs) +{ +void *func = tp->iterator; + +/* Synthetic events do not have static call sites */ +if (!tp->static_call_key) +return ; + +if (!tp_funcs[1 ].func) + func = tp_funcs[0 ].func; + +__static_call_update (tp->static_call_key, tp->static_call_tramp, func); +} +/* * Add the probe function to a tracepoint. */
@@ -251,8 +265,9 @@ static int tracepoint_add_func(struct tr* include/linux/tracepoint.h using rcu_dereference_sched(). */ rcu_assign_pointer (tp->funcs, tp_funcs); -if (!static_key_enabled (&tp->key)) -static_key_slow_inc (&tp->key); +tracepoint_update_call (tp, tp_funcs); +static_key_enable (&tp->key); +release_probes (old);return 0 ; }
@@ -281,9 +296,11 @@ static int tracepoint_remove_func(structif (tp->unregfunc &&static_key_enabled (&tp->key)) tp->unregfunc (); -if (static_key_enabled (&tp->key)) -static_key_slow_dec (&tp->key); +static_key_disable (&tp->key); + }else { +tracepoint_update_call (tp, tp_funcs); } +rcu_assign_pointer (tp->funcs, tp_funcs);release_probes (old);return 0 ;
Peter Zijlstra
maintainerPATCH v6 14/17 static_call: Handle tail-calls
In reply to Peter ZijlstraGCC can turn our static_call(name)(args...) into a tail call, in which case we get a JMP.d32 into the trampoline (which then does a further tail-call).
Teach objtool to recognise and mark these in .static_call_sites and adjust the code patching to deal with this.
--- arch/x86/kernel/static_call.c | 21 ++++++++++++++++++--- include/linux/static_call.h | 4 ++-- include/linux/static_call_types.h | 7 +++++++ kernel/static_call.c | 21 +++++++++++++-------- tools/include/linux/static_call_types.h | 7 +++++++ tools/objtool/check.c | 18 +++++++++++++----- 6 files changed, 60 insertions(+), 18 deletions(-)
arch/x86/kernel/static_call.c +18 -3
@@ -41,15 +41,30 @@ static void __static_call_transform(voidtext_poke_bp (insn, code, size,NULL ); } -void arch_static_call_transform (void *site,void *tramp,void *func) +static inline enum insn_type __sc_insn (bool null,bool tail) +{ +/* +* Encode the following table without branches: +* +* tail null insn +* -----+-------+------ +* 0 | 0 | CALL +* 0 | 1 | NOP +* 1 | 0 | JMP +* 1 | 1 | RET +*/ +return 2 *tail + null; +} + +void arch_static_call_transform (void *site,void *tramp,void *func,bool tail) {mutex_lock (&text_mutex);if (tramp) -__static_call_transform (tramp, func ?JMP :RET , func); +__static_call_transform (tramp,__sc_insn (!func, true), func);if (IS_ENABLED (CONFIG_HAVE_STATIC_CALL_INLINE ) && site) -__static_call_transform (site, func ?CALL :NOP , func); +__static_call_transform (site,__sc_insn (!func, tail), func);mutex_unlock (&text_mutex); }
include/linux/static_call.h +2 -2
@@ -103,7 +103,7 @@/* * Either @site or @tramp can be NULL. */ -extern void arch_static_call_transform (void *site,void *tramp,void *func); +extern void arch_static_call_transform (void *site,void *tramp,void *func,bool tail);#define STATIC_CALL_TRAMP_ADDR (name) &STATIC_CALL_TRAMP(name)
@@ -206,7 +206,7 @@ void __static_call_update(struct static_ {cpus_read_lock ();WRITE_ONCE (key->func, func); -arch_static_call_transform (NULL , tramp, func); +arch_static_call_transform (NULL , tramp, func, false);cpus_read_unlock (); }
include/linux/static_call_types.h +7 -0
@@ -17,6 +17,13 @@#define STATIC_CALL_TRAMP_STR (name) __stringify(STATIC_CALL_TRAMP(name))/* +* Flags in the low bits of static_call_site::key. +*/ +#define STATIC_CALL_SITE_TAIL 1UL/* tail call */ +#define STATIC_CALL_SITE_INIT 2UL/* init section */ +#define STATIC_CALL_SITE_FLAGS 3UL + +/* * The static call site table needs to be created by external tooling (objtool * or a compiler plugin). */
kernel/static_call.c +13 -8
@@ -15,8 +15,6 @@ extern struct static_call_site __start_sstatic bool static_call_initialized; -#define STATIC_CALL_INIT 1UL -/* mutex to protect key modules/sites */ static DEFINE_MUTEX (static_call_mutex);
@@ -39,18 +37,23 @@ static inline void *static_call_addr(strstatic inline struct static_call_key *static_call_key (const struct static_call_site *site) {return (struct static_call_key *) - (((long )site->key + (long )&site->key) & ~STATIC_CALL_INIT ); + (((long )site->key + (long )&site->key) & ~STATIC_CALL_SITE_FLAGS ); }/* These assume the key is word-aligned. */ static inline bool static_call_is_init (struct static_call_site *site) { -return ((long )site->key + (long )&site->key) &STATIC_CALL_INIT ; +return ((long )site->key + (long )&site->key) &STATIC_CALL_SITE_INIT ; +} + +static inline bool static_call_is_tail (struct static_call_site *site) +{ +return ((long )site->key + (long )&site->key) &STATIC_CALL_SITE_TAIL ; }static inline void static_call_set_init (struct static_call_site *site) { - site->key = ((long )static_call_key (site) |STATIC_CALL_INIT ) - + site->key = ((long )static_call_key (site) |STATIC_CALL_SITE_INIT ) - (long )&site->key; }
@@ -104,7 +107,7 @@ void __static_call_update(struct static_ key->func = func; -arch_static_call_transform (NULL , tramp, func); +arch_static_call_transform (NULL , tramp, func, false);/* * If uninitialized, we'll not update the callsites, but they still
@@ -154,7 +157,8 @@ void __static_call_update(struct static_continue ; } -arch_static_call_transform (site_addr,NULL , func); +arch_static_call_transform (site_addr,NULL , func, +static_call_is_tail (site)); } }
@@ -198,7 +202,8 @@ static int __static_call_init(struct mod key->mods = site_mod; } -arch_static_call_transform (site_addr,NULL , key->func); +arch_static_call_transform (site_addr,NULL , key->func, +static_call_is_tail (site)); }return 0 ;
tools/include/linux/static_call_types.h +7 -0
@@ -17,6 +17,13 @@#define STATIC_CALL_TRAMP_STR (name) __stringify(STATIC_CALL_TRAMP(name))/* +* Flags in the low bits of static_call_site::key. +*/ +#define STATIC_CALL_SITE_TAIL 1UL/* tail call */ +#define STATIC_CALL_SITE_INIT 2UL/* init section */ +#define STATIC_CALL_SITE_FLAGS 3UL + +/* * The static call site table needs to be created by external tooling (objtool * or a compiler plugin). */
tools/objtool/check.c +13 -5
@@ -511,7 +511,7 @@ static int create_static_call_sections(s }memset (reloc,0 ,sizeof (*reloc)); reloc->sym = key_sym; - reloc->addend =0 ; + reloc->addend =is_sibling_call (insn) ?STATIC_CALL_SITE_TAIL :0 ; reloc->type =R_X86_64_PC32 ; reloc->offset = idx *sizeof (struct static_call_site ) +4 ; reloc->sec = reloc_sec;
@@ -720,6 +720,10 @@ static int add_jump_destinations(struct }else {/* external sibling call */ insn->call_dest = reloc->sym; +if (insn->call_dest->static_call_tramp) { +list_add_tail (&insn->static_call_node, + &file->static_call_list); + }continue ; }
@@ -771,6 +775,10 @@ static int add_jump_destinations(struct/* internal sibling call */ insn->call_dest = insn->jump_dest->func; +if (insn->call_dest->static_call_tramp) { +list_add_tail (&insn->static_call_node, + &file->static_call_list); + } } } }
@@ -1639,6 +1647,10 @@ static int decode_sections(struct objtooif (ret)return ret; + ret =read_static_call_tramps (file); +if (ret) +return ret; + ret =add_jump_destinations (file);if (ret)return ret;
@@ -1671,10 +1683,6 @@ static int decode_sections(struct objtooif (ret)return ret; - ret =read_static_call_tramps (file); -if (ret) -return ret; -return 0 ; }
Peter Zijlstra
maintainerPATCH v6 6/17 static_call: Add basic static call infrastructure
In reply to Peter ZijlstraFrom: Josh Poimboeuf <[email protected]>
Static calls are a replacement for global function pointers. They use code patching to allow direct calls to be used instead of indirect calls. 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.
The concept and code are an extension of previous work done by Ard Biesheuvel and Steven Rostedt:
https://lkml.kernel.org/r/[email protected]
https://lkml.kernel.org/r/[email protected]
There are two implementations, depending on arch support:
1) out-of-line: patched trampolines (CONFIG_HAVE_STATIC_CALL)
2) basic function pointers
For more details, see the comments in include/linux/static_call.h.
--- arch/Kconfig | 3 include/linux/static_call.h | 156 ++++++++++++++++++++++++++++++++++++++ include/linux/static_call_types.h | 15 +++ 3 files changed, 174 insertions(+) create mode 100644 include/linux/static_call.h create mode 100644 include/linux/static_call_types.h
arch/Kconfig +3 -0
@@ -960,6 +960,9 @@ config HAVE_SPARSE_SYSCALL_NR entries at 4000, 5000 and 6000 locations. This option turns on syscall related optimizations for a given architecture. +config HAVE_STATIC_CALL + bool + source "kernel/gcov/Kconfig" source "scripts/gcc-plugins/Kconfig"
include/linux/static_call.h new file +156 -0
156 lines not on this page · raw message
include/linux/static_call_types.h new file +15 -0
@@ -0,0 +1,15 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +#ifndef _STATIC_CALL_TYPES_H +#define _STATIC_CALL_TYPES_H + +#include <linux/stringify.h> + +#define STATIC_CALL_KEY_PREFIX __SCK__ +#define STATIC_CALL_KEY (name) __PASTE(STATIC_CALL_KEY_PREFIX, name) + +#define STATIC_CALL_TRAMP_PREFIX __SCT__ +#define STATIC_CALL_TRAMP_PREFIX_STR __stringify(STATIC_CALL_TRAMP_PREFIX) +#define STATIC_CALL_TRAMP (name) __PASTE(STATIC_CALL_TRAMP_PREFIX, name) +#define STATIC_CALL_TRAMP_STR (name) __stringify(STATIC_CALL_TRAMP(name)) + +#endif /* _STATIC_CALL_TYPES_H */
Peter Zijlstra
maintainerPATCH v6 15/17 static_call: Allow early init
In reply to Peter ZijlstraIn order to use static_call() to wire up x86_pmu, we need to initialize earlier; copy some of the tricks from jump_label to enable this.
Primarily we overload key->next to store a sites pointer when there are no modules, this avoids having to use kmalloc() to initialize the sites and allows us to run much earlier.
(arguably, this is much much earlier than needed for perf, but it might allow other uses.)
--- arch/x86/kernel/setup.c | 2 + arch/x86/kernel/static_call.c | 8 +++++- include/linux/static_call.h | 15 +++++++++-- kernel/static_call.c | 55 +++++++++++++++++++++++++++++++++++++++--- 4 files changed, 74 insertions(+), 6 deletions(-)
arch/x86/kernel/setup.c +2 -0
@@ -19,6 +19,7 @@#include <linux/hugetlb.h> #include <linux/tboot.h> #include <linux/usb/xhci-dbgp.h> +#include <linux/static_call.h> #include <uapi/linux/mount.h>
@@ -848,6 +849,7 @@ void __init setup_arch(char **cmdline_p)early_cpu_init ();arch_init_ideal_nops ();jump_label_init (); +static_call_init ();early_ioremap_init ();setup_olpc_ofw_pgd ();
arch/x86/kernel/static_call.c +7 -1
@@ -11,7 +11,7 @@ enum insn_type { RET = 3, /* tramp / site cond-tail-call */ }; -static void __static_call_transform (void *insn,enum insn_type type,void *func) +static void __ref __static_call_transform (void *insn,enum insn_type type,void *func) {int size =CALL_INSN_SIZE ;const void *code;
@@ -33,11 +33,17 @@ static void __static_call_transform(void code =text_gen_insn (RET_INSN_OPCODE , insn, func); size =RET_INSN_SIZE ;break ; + +default :/* GCC is a moron -- it figures @code can be uninitialized below */ +BUG (); }if (memcmp (insn, code, size) ==0 )return ; +if (unlikely (system_state ==SYSTEM_BOOTING )) +return text_poke_early (insn, code, size); +text_poke_bp (insn, code, size,NULL ); }
include/linux/static_call.h +13 -2
@@ -99,6 +99,8 @@ extern void arch_static_call_transform(v #ifdef CONFIG_HAVE_STATIC_CALL_INLINE +extern void __init static_call_init(void); + struct static_call_mod { struct static_call_mod *next; struct module *mod; /* for vmlinux, mod == NULL */
@@ -107,7 +109,12 @@ struct static_call_mod {struct static_call_key {void *func; -struct static_call_mod *mods; +union { +/* bit 0: 0 = mods, 1 = sites */ +unsigned long type; +struct static_call_mod *mods; +struct static_call_site *sites; + }; };extern void __static_call_update (struct static_call_key *key,void *tramp,void *func);
@@ -118,7 +125,7 @@ extern int static_call_text_reserved(voiDECLARE_STATIC_CALL (name, _func); \struct static_call_key STATIC_CALL_KEY (name ) = { \ .func = _func, \ - .mods =NULL , \ + .type =1 , \ }; \ARCH_DEFINE_STATIC_CALL_TRAMP (name, _func)
@@ -143,6 +150,8 @@ extern int static_call_text_reserved(voi#elif defined(CONFIG_HAVE_STATIC_CALL ) +static inline void static_call_init (void ) { } +struct static_call_key {void *func; };
@@ -188,6 +197,8 @@ static inline int static_call_text_reser#else /* Generic implementation */ +static inline void static_call_init (void ) { } +struct static_call_key {void *func; };
kernel/static_call.c +52 -3
98 lines not on this page · raw message
Peter Zijlstra
maintainerPATCH v6 1/17 notifier: Fix broken error handling pattern
In reply to Peter ZijlstraThe current notifiers have the following error handling pattern all over the place:
int err, nr; err = __foo_notifier_call_chain(&chain, val_up, v, -1, &nr); if (err & NOTIFIER_STOP_MASK) __foo_notifier_call_chain(&chain, val_down, v, nr-1, NULL)
And aside from the endless repetition thereof, it is broken. Consider blocking notifiers; both calls take and drop the rwsem, this means that the notifier list can change in between the two calls, making @nr meaningless.
Fix this by replacing all the __foo_notifier_call_chain() functions with foo_notifier_call_chain_robust() that embeds the above pattern, but ensures it is inside a single lock region.
Note: I switched atomic_notifier_call_chain_robust() to use
the spinlock, since RCU cannot provide the guarantee required for the recovery.
Note: software_resume() error handling was broken afaict.
--- include/linux/notifier.h | 15 +-- kernel/cpu_pm.c | 48 ++++-------- kernel/notifier.c | 144 ++++++++++++++++++++++--------------- kernel/power/hibernate.c | 26 +++--- kernel/power/main.c | 8 +- kernel/power/power.h | 3 kernel/power/suspend.c | 14 +-- kernel/power/user.c | 14 +-- tools/power/pm-graph/sleepgraph.py | 2 9 files changed, 141 insertions(+), 133 deletions(-)
include/linux/notifier.h +7 -8
@@ -161,20 +161,19 @@ extern int srcu_notifier_chain_unregisteextern int atomic_notifier_call_chain (struct atomic_notifier_head *nh,unsigned long val,void *v); -extern int __atomic_notifier_call_chain (struct atomic_notifier_head *nh, -unsigned long val,void *v,int nr_to_call,int *nr_calls);extern int blocking_notifier_call_chain (struct blocking_notifier_head *nh,unsigned long val,void *v); -extern int __blocking_notifier_call_chain (struct blocking_notifier_head *nh, -unsigned long val,void *v,int nr_to_call,int *nr_calls);extern int raw_notifier_call_chain (struct raw_notifier_head *nh,unsigned long val,void *v); -extern int __raw_notifier_call_chain (struct raw_notifier_head *nh, -unsigned long val,void *v,int nr_to_call,int *nr_calls);extern int srcu_notifier_call_chain (struct srcu_notifier_head *nh,unsigned long val,void *v); -extern int __srcu_notifier_call_chain (struct srcu_notifier_head *nh, -unsigned long val,void *v,int nr_to_call,int *nr_calls); + +extern int atomic_notifier_call_chain_robust (struct atomic_notifier_head *nh, +unsigned long val_up,unsigned long val_down,void *v); +extern int blocking_notifier_call_chain_robust (struct blocking_notifier_head *nh, +unsigned long val_up,unsigned long val_down,void *v); +extern int raw_notifier_call_chain_robust (struct raw_notifier_head *nh, +unsigned long val_up,unsigned long val_down,void *v);#define NOTIFY_DONE 0x0000/* Don't care */ #define NOTIFY_OK 0x0001/* Suits me */
kernel/cpu_pm.c +18 -30
86 lines not on this page · raw message
kernel/notifier.c +88 -56
240 lines not on this page · raw message
kernel/power/hibernate.c +12 -14
@@ -706,8 +706,8 @@ static int load_image_and_restore(void)*/ int hibernate (void ) { -int error, nr_calls =0 ;bool snapshot_test = false; +int error;if (!hibernation_available ()) {pm_pr_dbg ("Hibernation not available.\n" );
@@ -723,11 +723,9 @@ int hibernate(void)pr_info ("hibernation entry\n" );pm_prepare_console (); - error =__pm_notifier_call_chain (PM_HIBERNATION_PREPARE ,-1 , &nr_calls); -if (error) { - nr_calls--; - goto Exit; - } + error =pm_notifier_call_chain_robust (PM_HIBERNATION_PREPARE ,PM_POST_HIBERNATION ); +if (error) + goto Restore;ksys_sync_helper ();
@@ -785,7 +783,8 @@ int hibernate(void)/* Don't bother checking whether freezer_test_done is true */ freezer_test_done = false; Exit: -__pm_notifier_call_chain (PM_POST_HIBERNATION , nr_calls,NULL ); +pm_notifier_call_chain (PM_POST_HIBERNATION ); + Restore:pm_restore_console ();hibernate_release (); Unlock:
@@ -813,7 +812,7 @@ int hibernate(void)*/ static int software_resume (void ) { -int error, nr_calls =0 ; +int error;/* * If the user said "noresume".. bail out early.
@@ -900,11 +899,9 @@ static int software_resume(void)pr_info ("resume from hibernation\n" );pm_prepare_console (); - error =__pm_notifier_call_chain (PM_RESTORE_PREPARE ,-1 , &nr_calls); -if (error) { - nr_calls--; - goto Close_Finish; - } + error =pm_notifier_call_chain_robust (PM_RESTORE_PREPARE ,PM_POST_RESTORE ); +if (error) + goto Restore;pm_pr_dbg ("Preparing processes for hibernation restore.\n" ); error =freeze_processes ();
@@ -920,7 +917,8 @@ static int software_resume(void) error =load_image_and_restore ();thaw_processes (); Finish: -__pm_notifier_call_chain (PM_POST_RESTORE , nr_calls,NULL ); +pm_notifier_call_chain (PM_POST_RESTORE ); + Restore:pm_restore_console ();pr_info ("resume failed (%d)\n" , error);hibernate_release ();
kernel/power/main.c +4 -4
@@ -80,18 +80,18 @@ int unregister_pm_notifier(struct notifi }EXPORT_SYMBOL_GPL (unregister_pm_notifier); -int __pm_notifier_call_chain (unsigned long val,int nr_to_call,int *nr_calls) +int pm_notifier_call_chain_robust (unsigned long val_up,unsigned long val_down) {int ret; - ret =__blocking_notifier_call_chain (&pm_chain_head, val,NULL , - nr_to_call, nr_calls); + ret =blocking_notifier_call_chain_robust (&pm_chain_head, val_up, val_down,NULL );return notifier_to_errno (ret); } +int pm_notifier_call_chain (unsigned long val) { -return __pm_notifier_call_chain (val,-1 ,NULL ); +return blocking_notifier_call_chain (&pm_chain_head, val,NULL ); }/* If set, devices may be suspended and resumed asynchronously. */
kernel/power/power.h +1 -2
@@ -210,8 +210,7 @@ static inline void suspend_test_finish(c#ifdef CONFIG_PM_SLEEP /* kernel/power/main.c */ -extern int __pm_notifier_call_chain (unsigned long val,int nr_to_call, -int *nr_calls); +extern int pm_notifier_call_chain_robust (unsigned long val_up,unsigned long val_down);extern int pm_notifier_call_chain (unsigned long val);#endif
kernel/power/suspend.c +6 -8
@@ -342,18 +342,16 @@ static int suspend_test(int level)*/ static int suspend_prepare (suspend_state_t state) { -int error, nr_calls =0 ; +int error;if (!sleep_state_supported (state))return -EPERM ;pm_prepare_console (); - error =__pm_notifier_call_chain (PM_SUSPEND_PREPARE ,-1 , &nr_calls); -if (error) { - nr_calls--; - goto Finish; - } + error =pm_notifier_call_chain_robust (PM_SUSPEND_PREPARE ,PM_POST_SUSPEND ); +if (error) + goto Restore;trace_suspend_resume (TPS ("freeze_processes" ),0 , true); error =suspend_freeze_processes ();
@@ -363,8 +361,8 @@ static int suspend_prepare(suspend_state suspend_stats.failed_freeze++;dpm_save_failed_step (SUSPEND_FREEZE ); - Finish: -__pm_notifier_call_chain (PM_POST_SUSPEND , nr_calls,NULL ); +pm_notifier_call_chain (PM_POST_SUSPEND ); + Restore:pm_restore_console ();return error; }
kernel/power/user.c +4 -10
@@ -46,7 +46,7 @@ int is_hibernate_resume_dev(const structstatic int snapshot_open (struct inode *inode,struct file *filp) {struct snapshot_data *data; -int error, nr_calls =0 ; +int error;if (!hibernation_available ())return -EPERM ;
@@ -73,9 +73,7 @@ static int snapshot_open(struct inode *i swap_type_of(swsusp_resume_device, 0, NULL) : -1; data->mode =O_RDONLY ; data->free_bitmaps = false; - error =__pm_notifier_call_chain (PM_HIBERNATION_PREPARE ,-1 , &nr_calls); -if (error) -__pm_notifier_call_chain (PM_POST_HIBERNATION , --nr_calls,NULL ); + error =pm_notifier_call_chain_robust (PM_HIBERNATION_PREPARE ,PM_POST_HIBERNATION ); }else {/* * Resuming. We may need to wait for the image device to
@@ -85,15 +83,11 @@ static int snapshot_open(struct inode *i data->swap =-1 ; data->mode =O_WRONLY ; - error =__pm_notifier_call_chain (PM_RESTORE_PREPARE ,-1 , &nr_calls); + error =pm_notifier_call_chain_robust (PM_RESTORE_PREPARE ,PM_POST_RESTORE );if (!error) { error =create_basic_memory_bitmaps (); data->free_bitmaps = !error; - }else - nr_calls--; - -if (error) -__pm_notifier_call_chain (PM_POST_RESTORE , nr_calls,NULL ); + } }if (error)hibernate_release ();
tools/power/pm-graph/sleepgraph.py +1 -1
@@ -171,7 +171,7 @@ import base64 tracefuncs = { 'sys_sync': {}, 'ksys_sync': {}, - '__pm_notifier_call_chain': {}, + 'pm_notifier_call_chain_robust': {}, 'pm_prepare_console': {}, 'pm_notifier_call_chain': {}, 'freeze_processes': {},
Peter Zijlstra
maintainerPATCH v6 10/17 x86/static_call: Add inline static call implementation for x86-64
In reply to Peter ZijlstraFrom: Josh Poimboeuf <[email protected]>
Add the inline static call implementation for x86-64. The generated code is identical to the out-of-line case, except we move the trampoline into it's own section.
Objtool uses the trampoline naming convention to detect all the call sites. It then annotates those call sites in the .static_call_sites section.
During boot (and module init), the call sites are patched to call directly into the destination function. The temporary trampoline is then no longer used.
--- arch/x86/Kconfig | 3 arch/x86/include/asm/static_call.h | 13 +++ arch/x86/kernel/static_call.c | 5 + arch/x86/kernel/vmlinux.lds.S | 1 include/asm-generic/vmlinux.lds.h | 6 + tools/include/linux/static_call_types.h | 28 +++++++ tools/objtool/check.c | 126 ++++++++++++++++++++++++++++++++ tools/objtool/check.h | 1 tools/objtool/elf.c | 8 +- tools/objtool/elf.h | 3 tools/objtool/objtool.h | 1 tools/objtool/orc_gen.c | 4 - tools/objtool/sync-check.sh | 1 13 files changed, 190 insertions(+), 10 deletions(-) create mode 100644 tools/objtool/include/linux/static_call_types.h
arch/x86/Kconfig +2 -1
@@ -214,6 +214,7 @@ config X86 select HAVE_STACKPROTECTOR if CC_HAS_SANE_STACKPROTECTOR select HAVE_STACK_VALIDATION if X86_64 select HAVE_STATIC_CALL + select HAVE_STATIC_CALL_INLINE if HAVE_STACK_VALIDATION select HAVE_RSEQ select HAVE_SYSCALL_TRACEPOINTS select HAVE_UNSTABLE_SCHED_CLOCK
@@ -229,6 +230,7 @@ config X86 select RTC_MC146818_LIB select SPARSE_IRQ select SRCU + select STACK_VALIDATION if HAVE_STACK_VALIDATION && (HAVE_STATIC_CALL_INLINE || RETPOLINE) select SYSCTL_EXCEPTION_TRACE select THREAD_INFO_IN_TASK select USER_STACKTRACE_SUPPORT
@@ -450,7 +452,6 @@ config GOLDFISH config RETPOLINE bool "Avoid speculative indirect branches in kernel" default y - select STACK_VALIDATION if HAVE_STACK_VALIDATION help Compile kernel with the retpoline compiler options to guard against kernel-to-user data leaks by avoiding speculative indirect
arch/x86/include/asm/static_call.h +12 -1
@@ -5,12 +5,23 @@#include <asm/text-patching.h> /* +* For CONFIG_HAVE_STATIC_CALL_INLINE, this is a temporary trampoline which +* uses the current value of the key->func pointer to do an indirect jump to +* the function. This trampoline is only used during boot, before the call +* sites get patched by static_call_update(). The name of this trampoline has +* a magical aspect: objtool uses it to find static call sites so it can create +* the .static_call_sites section. +* * For CONFIG_HAVE_STATIC_CALL, this is a permanent trampoline which * does a direct jump to the function. The direct jump gets patched by * static_call_update(). +* +* Having the trampoline in a special section forces GCC to emit a JMP.d32 when +* it does tail-call optimization on the call; since you cannot compute the +* relative displacement across sections. */ #define ARCH_DEFINE_STATIC_CALL_TRAMP (name, func) \ - asm(".pushsection .text, \"ax\" \n" \ + asm(".pushsection .static_call.text, \"ax\" \n" \ ".align 4 \n" \ ".globl " STATIC_CALL_TRAMP_STR(name) " \n" \ STATIC_CALL_TRAMP_STR(name) ": \n" \
arch/x86/kernel/static_call.c +4 -1
@@ -6,7 +6,7 @@static void __static_call_transform (void *insn,u8 opcode,void *func) { -const void *code =text_gen_insn (opcode, (long )insn, (long )func); +const void *code =text_gen_insn (opcode, insn, func); if (WARN_ONCE(*(u8 *)insn != opcode, "unexpected static call insn opcode 0x%x at %pS\n",
@@ -26,6 +26,9 @@ void arch_static_call_transform(void *siif (tramp)__static_call_transform (tramp,JMP32_INSN_OPCODE , func); +if (IS_ENABLED (CONFIG_HAVE_STATIC_CALL_INLINE ) && site) +__static_call_transform (site,CALL_INSN_OPCODE , func); +mutex_unlock (&text_mutex); }EXPORT_SYMBOL_GPL (arch_static_call_transform);
arch/x86/kernel/vmlinux.lds.S +1 -0
@@ -136,6 +136,7 @@ SECTIONS ENTRY_TEXT ALIGN_ENTRY_TEXT_END SOFTIRQENTRY_TEXT + STATIC_CALL_TEXT *(.fixup) *(.gnu.warning)
include/asm-generic/vmlinux.lds.h +6 -0
@@ -620,6 +620,12 @@ *(.softirqentry.text) \ __softirqentry_text_end = .; +#define STATIC_CALL_TEXT \ + ALIGN_FUNCTION(); \ + __static_call_text_start = .; \ + *(.static_call.text) \ + __static_call_text_end = .; +/* Section used for early init (in .S files) */ #define HEAD_TEXT KEEP(*(.head.text))
tools/include/linux/static_call_types.h new file +28 -0
@@ -0,0 +1,28 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +#ifndef _STATIC_CALL_TYPES_H +#define _STATIC_CALL_TYPES_H + +#include <linux/types.h> +#include <linux/stringify.h> + +#define STATIC_CALL_KEY_PREFIX __SCK__ +#define STATIC_CALL_KEY_PREFIX_STR __stringify(STATIC_CALL_KEY_PREFIX) +#define STATIC_CALL_KEY_PREFIX_LEN (sizeof(STATIC_CALL_KEY_PREFIX_STR) - 1) +#define STATIC_CALL_KEY (name) __PASTE(STATIC_CALL_KEY_PREFIX, name) + +#define STATIC_CALL_TRAMP_PREFIX __SCT__ +#define STATIC_CALL_TRAMP_PREFIX_STR __stringify(STATIC_CALL_TRAMP_PREFIX) +#define STATIC_CALL_TRAMP_PREFIX_LEN (sizeof(STATIC_CALL_TRAMP_PREFIX_STR) - 1) +#define STATIC_CALL_TRAMP (name) __PASTE(STATIC_CALL_TRAMP_PREFIX, name) +#define STATIC_CALL_TRAMP_STR (name) __stringify(STATIC_CALL_TRAMP(name)) + +/* +* The static call site table needs to be created by external tooling (objtool +* or a compiler plugin). +*/ +struct static_call_site { +s32 addr; +s32 key; +}; + +#endif /* _STATIC_CALL_TYPES_H */
tools/objtool/check.c +126 -0
168 lines not on this page · raw message
tools/objtool/check.h +1 -0
@@ -22,6 +22,7 @@ struct insn_state {struct instruction {struct list_head list;struct hlist_node hash; +struct list_head static_call_node;struct section *sec;unsigned long offset;unsigned int len;
tools/objtool/elf.c +4 -4
@@ -652,7 +652,7 @@ struct elf *elf_open_read(const char *na }struct section *elf_create_section (struct elf *elf,const char *name, -size_t entsize,int nr) +unsigned int sh_flags,size_t entsize,int nr) {struct section *sec, *shstrtab;size_t size = entsize * nr;
@@ -712,7 +712,7 @@ struct section *elf_create_section(struc sec->sh.sh_entsize = entsize; sec->sh.sh_type =SHT_PROGBITS ; sec->sh.sh_addralign =1 ; - sec->sh.sh_flags =SHF_ALLOC ; + sec->sh.sh_flags =SHF_ALLOC | sh_flags;/* Add section name to .shstrtab (or .strtab for Clang) */
@@ -767,7 +767,7 @@ static struct section *elf_create_rel_restrcpy (relocname,".rel" );strcat (relocname, base->name); - sec =elf_create_section (elf, relocname,sizeof (GElf_Rel),0 ); + sec =elf_create_section (elf, relocname,0 ,sizeof (GElf_Rel),0 );free (relocname);if (!sec)return NULL ;
@@ -797,7 +797,7 @@ static struct section *elf_create_rela_rstrcpy (relocname,".rela" );strcat (relocname, base->name); - sec =elf_create_section (elf, relocname,sizeof (GElf_Rela),0 ); + sec =elf_create_section (elf, relocname,0 ,sizeof (GElf_Rela),0 );free (relocname);if (!sec)return NULL ;
tools/objtool/elf.h +2 -1
@@ -56,6 +56,7 @@ struct symbol {unsigned int len;struct symbol *pfunc, *cfunc, *alias;bool uaccess_safe; +bool static_call_tramp; };struct reloc {
@@ -120,7 +121,7 @@ static inline u32 reloc_hash(struct relo }struct elf *elf_open_read (const char *name,int flags); -struct section *elf_create_section (struct elf *elf,const char *name,size_t entsize,int nr); +struct section *elf_create_section (struct elf *elf,const char *name,unsigned int sh_flags,size_t entsize,int nr);struct section *elf_create_reloc_section (struct elf *elf,struct section *base,int reltype);void elf_add_reloc (struct elf *elf,struct reloc *reloc); int elf_write_insn(struct elf *elf, struct section *sec,
tools/objtool/objtool.h +1 -0
@@ -16,6 +16,7 @@ struct objtool_file {struct elf *elf;struct list_head insn_list;DECLARE_HASHTABLE (insn_hash,20 ); +struct list_head static_call_list;bool ignore_unreachables, c_file, hints, rodata; };
tools/objtool/orc_gen.c +2 -2
@@ -177,7 +177,7 @@ int create_orc_sections(struct objtool_f/* create .orc_unwind_ip and .rela.orc_unwind_ip sections */ - sec =elf_create_section (file->elf,".orc_unwind_ip" ,sizeof (int ), idx); + sec =elf_create_section (file->elf,".orc_unwind_ip" ,0 ,sizeof (int ), idx);if (!sec)return -1 ;
@@ -186,7 +186,7 @@ int create_orc_sections(struct objtool_freturn -1 ;/* create .orc_unwind section */ - u_sec =elf_create_section (file->elf,".orc_unwind" , + u_sec =elf_create_section (file->elf,".orc_unwind" ,0 ,sizeof (struct orc_entry ), idx);/* populate sections */
tools/objtool/sync-check.sh +1 -0
@@ -7,6 +7,7 @@ arch/x86/include/asm/orc_types.h arch/x86/include/asm/emulate_prefix.h arch/x86/lib/x86-opcode-map.txt arch/x86/tools/gen-insn-attr-x86.awk +include/linux/static_call_types.h ' check_2 () {
Peter Zijlstra
maintainerPATCH v6 7/17 static_call: Add inline static call infrastructure
In reply to Peter ZijlstraFrom: Josh Poimboeuf <[email protected]>
Add infrastructure for an arch-specific CONFIG_HAVE_STATIC_CALL_INLINE option, which is a faster version of CONFIG_HAVE_STATIC_CALL. At runtime, the static call sites are patched directly, rather than using the out-of-line trampolines.
Compared to out-of-line static calls, the performance benefits are more modest, but still measurable. Steven Rostedt did some tracepoint measurements:
https://lkml.kernel.org/r/[email protected]
This code is heavily inspired by the jump label code (aka "static jumps"), as some of the concepts are very similar.
For more details, see the comments in include/linux/static_call.h.
--- arch/Kconfig | 4 include/asm-generic/vmlinux.lds.h | 7 include/linux/module.h | 5 include/linux/static_call.h | 36 ++++ include/linux/static_call_types.h | 13 + kernel/Makefile | 1 kernel/module.c | 5 kernel/static_call.c | 303 ++++++++++++++++++++++++++++++++++++++ 8 files changed, 373 insertions(+), 1 deletion(-) create mode 100644 kernel/static_call.c
arch/Kconfig +4 -0
@@ -982,6 +982,10 @@ config HAVE_SPARSE_SYSCALL_NR config HAVE_STATIC_CALL bool +config HAVE_STATIC_CALL_INLINE + bool + depends on HAVE_STATIC_CALL + source "kernel/gcov/Kconfig" source "scripts/gcc-plugins/Kconfig"
include/asm-generic/vmlinux.lds.h +7 -0
@@ -368,6 +368,12 @@ KEEP(*(__jump_table)) \ __stop___jump_table = .; +#define STATIC_CALL_DATA \ + . = ALIGN(8); \ + __start_static_call_sites = .; \ + KEEP(*(.static_call_sites)) \ + __stop_static_call_sites = .; +/* * Allow architectures to handle ro_after_init data on their * own by defining an empty RO_AFTER_INIT_DATA.
@@ -377,6 +383,7 @@ __start_ro_after_init = .; \ *(.data..ro_after_init) \ JUMP_TABLE_DATA \ + STATIC_CALL_DATA \ __end_ro_after_init = .;#endif
include/linux/module.h +5 -0
@@ -25,6 +25,7 @@#include <linux/error-injection.h> #include <linux/tracepoint-defs.h> #include <linux/srcu.h> +#include <linux/static_call_types.h> #include <linux/percpu.h> #include <asm/module.h>
@@ -497,6 +498,10 @@ struct module {unsigned long *kprobe_blacklist;unsigned int num_kprobe_blacklist;#endif +#ifdef CONFIG_HAVE_STATIC_CALL_INLINE +int num_static_call_sites; +struct static_call_site *static_call_sites; +#endif #ifdef CONFIG_LIVEPATCH bool klp;/* Is this a livepatch module? */
include/linux/static_call.h +35 -1
@@ -95,7 +95,41 @@ extern void arch_static_call_transform(v STATIC_CALL_TRAMP_ADDR(name), func); \ }) -#if defined(CONFIG_HAVE_STATIC_CALL) +#ifdef CONFIG_HAVE_STATIC_CALL_INLINE + +struct static_call_mod { + struct static_call_mod *next; + struct module *mod; /* for vmlinux, mod == NULL */ + struct static_call_site *sites; +}; + +struct static_call_key { + void *func; + struct static_call_mod *mods; +}; + +extern void __static_call_update(struct static_call_key *key, void *tramp, void *func); +extern int static_call_mod_init(struct module *mod); + +#define DEFINE_STATIC_CALL(name, _func) \ + DECLARE_STATIC_CALL(name, _func); \ + struct static_call_key STATIC_CALL_KEY(name) = { \ + .func = _func, \ + .mods = NULL, \ + }; \ + ARCH_DEFINE_STATIC_CALL_TRAMP(name, _func) + +#define static_call(name) __static_call(name) + +#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)) + +#elif defined(CONFIG_HAVE_STATIC_CALL) struct static_call_key { void *func;
include/linux/static_call_types.h +13 -0
@@ -2,14 +2,27 @@#ifndef _STATIC_CALL_TYPES_H#define _STATIC_CALL_TYPES_H +#include <linux/types.h> #include <linux/stringify.h> #define STATIC_CALL_KEY_PREFIX __SCK__ +#define STATIC_CALL_KEY_PREFIX_STR __stringify(STATIC_CALL_KEY_PREFIX) +#define STATIC_CALL_KEY_PREFIX_LEN (sizeof(STATIC_CALL_KEY_PREFIX_STR) - 1)#define STATIC_CALL_KEY (name) __PASTE(STATIC_CALL_KEY_PREFIX, name)#define STATIC_CALL_TRAMP_PREFIX __SCT__#define STATIC_CALL_TRAMP_PREFIX_STR __stringify(STATIC_CALL_TRAMP_PREFIX) +#define STATIC_CALL_TRAMP_PREFIX_LEN (sizeof(STATIC_CALL_TRAMP_PREFIX_STR) - 1)#define STATIC_CALL_TRAMP (name) __PASTE(STATIC_CALL_TRAMP_PREFIX, name)#define STATIC_CALL_TRAMP_STR (name) __stringify(STATIC_CALL_TRAMP(name)) +/* +* The static call site table needs to be created by external tooling (objtool +* or a compiler plugin). +*/ +struct static_call_site { +s32 addr; +s32 key; +}; +#endif /* _STATIC_CALL_TYPES_H */
kernel/Makefile +1 -0
@@ -109,6 +109,7 @@ obj-$(CONFIG_CPU_PM) += cpu_pm.o obj-$(CONFIG_BPF) += bpf/ obj-$(CONFIG_KCSAN) += kcsan/ obj-$(CONFIG_SHADOW_CALL_STACK) += scs.o +obj-$(CONFIG_HAVE_STATIC_CALL_INLINE) += static_call.o obj-$(CONFIG_PERF_EVENTS) += events/
kernel/module.c +5 -0
@@ -3226,6 +3226,11 @@ static int find_module_sections(struct msizeof (unsigned long ), &mod->num_kprobe_blacklist);#endif +#ifdef CONFIG_HAVE_STATIC_CALL_INLINE + mod->static_call_sites =section_objs (info,".static_call_sites" , +sizeof (*mod->static_call_sites), + &mod->num_static_call_sites); +#endif mod->extable =section_objs (info,"__ex_table" ,sizeof (*mod->extable), &mod->num_exentries);
kernel/static_call.c new file +303 -0
303 lines not on this page · raw message
Peter Zijlstra
maintainerPATCH v6 17/17 x86/perf, static_call: Optimize x86_pmu methods
In reply to Peter ZijlstraReplace many of the indirect calls with static_call().
The average PMI time, as measured by perf_sample_event_took()*:
PRE: 3283.03 [ns] POST: 3145.12 [ns]
Which is a ~138 [ns] win per PMI, or a ~4.2% decrease.
[*] on an IVB-EP, using: 'perf record -a -e cycles -- make O=defconfig-build/ -j80'
--- arch/x86/events/core.c | 140 +++++++++++++++++++++++++++++++++++-------------- 1 file changed, 100 insertions(+), 40 deletions(-)
arch/x86/events/core.c +94 -40
@@ -28,6 +28,7 @@#include <linux/bitops.h> #include <linux/device.h> #include <linux/nospec.h> +#include <linux/static_call.h> #include <asm/apic.h> #include <asm/stacktrace.h>
@@ -52,6 +53,34 @@ DEFINE_PER_CPU(struct cpu_hw_events, cpuDEFINE_STATIC_KEY_FALSE (rdpmc_never_available_key);DEFINE_STATIC_KEY_FALSE (rdpmc_always_available_key); +/* +* This here uses DEFINE_STATIC_CALL_NULL() to get a static_call defined +* from just a typename, as opposed to an actual function. +*/ +DEFINE_STATIC_CALL_NULL (x86_pmu_handle_irq, *x86_pmu.handle_irq); +DEFINE_STATIC_CALL_NULL (x86_pmu_disable_all, *x86_pmu.disable_all); +DEFINE_STATIC_CALL_NULL (x86_pmu_enable_all, *x86_pmu.enable_all); +DEFINE_STATIC_CALL_NULL (x86_pmu_enable, *x86_pmu.enable); +DEFINE_STATIC_CALL_NULL (x86_pmu_disable, *x86_pmu.disable); + +DEFINE_STATIC_CALL_NULL (x86_pmu_add, *x86_pmu.add); +DEFINE_STATIC_CALL_NULL (x86_pmu_del, *x86_pmu.del); +DEFINE_STATIC_CALL_NULL (x86_pmu_read, *x86_pmu.read); + +DEFINE_STATIC_CALL_NULL (x86_pmu_schedule_events, *x86_pmu.schedule_events); +DEFINE_STATIC_CALL_NULL (x86_pmu_get_event_constraints, *x86_pmu.get_event_constraints); +DEFINE_STATIC_CALL_NULL (x86_pmu_put_event_constraints, *x86_pmu.put_event_constraints); + +DEFINE_STATIC_CALL_NULL (x86_pmu_start_scheduling, *x86_pmu.start_scheduling); +DEFINE_STATIC_CALL_NULL (x86_pmu_commit_scheduling, *x86_pmu.commit_scheduling); +DEFINE_STATIC_CALL_NULL (x86_pmu_stop_scheduling, *x86_pmu.stop_scheduling); + +DEFINE_STATIC_CALL_NULL (x86_pmu_sched_task, *x86_pmu.sched_task); +DEFINE_STATIC_CALL_NULL (x86_pmu_swap_task_ctx, *x86_pmu.swap_task_ctx); + +DEFINE_STATIC_CALL_NULL (x86_pmu_drain_pebs, *x86_pmu.drain_pebs); +DEFINE_STATIC_CALL_NULL (x86_pmu_pebs_aliases, *x86_pmu.pebs_aliases); +u64 __read_mostly hw_cache_event_ids [PERF_COUNT_HW_CACHE_MAX ] [PERF_COUNT_HW_CACHE_OP_MAX ]
@@ -660,7 +695,7 @@ static void x86_pmu_disable(struct pmu * cpuc->enabled =0 ;barrier (); - x86_pmu.disable_all (); +static_call (x86_pmu_disable_all)(); }void x86_pmu_enable_all (int added)
@@ -907,8 +942,7 @@ int x86_schedule_events(struct cpu_hw_evif (cpuc->txn_flags &PERF_PMU_TXN_ADD ) n0 -= cpuc->n_txn; -if (x86_pmu.start_scheduling) - x86_pmu.start_scheduling (cpuc); +static_call_cond (x86_pmu_start_scheduling)(cpuc);for (i =0 , wmin =X86_PMC_IDX_MAX , wmax =0 ; i < n; i++) { c = cpuc->event_constraint[i];
@@ -925,7 +959,7 @@ int x86_schedule_events(struct cpu_hw_ev* change due to external factors (sibling state, allow_tfa). */ if (!c || (c->flags &PERF_X86_EVENT_DYNAMIC )) { - c = x86_pmu.get_event_constraints (cpuc, i, cpuc->event_list[i]); + c =static_call (x86_pmu_get_event_constraints)(cpuc, i, cpuc->event_list[i]); cpuc->event_constraint[i] = c; }
@@ -1008,8 +1042,7 @@ int x86_schedule_events(struct cpu_hw_evif (!unsched && assign) {for (i =0 ; i < n; i++) { e = cpuc->event_list[i]; -if (x86_pmu.commit_scheduling) - x86_pmu.commit_scheduling (cpuc, i, assign[i]); +static_call_cond (x86_pmu_commit_scheduling)(cpuc, i, assign[i]); } }else {for (i = n0; i < n; i++) {
@@ -1018,15 +1051,13 @@ int x86_schedule_events(struct cpu_hw_ev/* * release events that failed scheduling */ -if (x86_pmu.put_event_constraints) - x86_pmu.put_event_constraints (cpuc, e); +static_call_cond (x86_pmu_put_event_constraints)(cpuc, e); cpuc->event_constraint[i] =NULL ; } } -if (x86_pmu.stop_scheduling) - x86_pmu.stop_scheduling (cpuc); +static_call_cond (x86_pmu_stop_scheduling)(cpuc);return unsched ? -EINVAL :0 ; }
@@ -1217,7 +1248,7 @@ static void x86_pmu_enable(struct pmu *p cpuc->enabled =1 ;barrier (); - x86_pmu.enable_all (added); +static_call (x86_pmu_enable_all)(added); } static DEFINE_PER_CPU(u64 [X86_PMC_IDX_MAX], pmc_prev_left);
@@ -1338,7 +1369,7 @@ static int x86_pmu_add(struct perf_eventif (cpuc->txn_flags &PERF_PMU_TXN_ADD ) goto done_collect; - ret = x86_pmu.schedule_events (cpuc, n, assign); + ret =static_call (x86_pmu_schedule_events)(cpuc, n, assign);if (ret) goto out;/*
@@ -1356,13 +1387,11 @@ static int x86_pmu_add(struct perf_event cpuc->n_added += n - n0; cpuc->n_txn += n - n0; -if (x86_pmu.add) { -/* -* This is before x86_pmu_enable() will call x86_pmu_start(), -* so we enable LBRs before an event needs them etc.. -*/ - x86_pmu.add (event); - } +/* +* This is before x86_pmu_enable() will call x86_pmu_start(), +* so we enable LBRs before an event needs them etc.. +*/ +static_call_cond (x86_pmu_add)(event); ret =0 ; out:
@@ -1390,7 +1419,7 @@ static void x86_pmu_start(struct perf_ev cpuc->events[idx] = event;__set_bit (idx, cpuc->active_mask);__set_bit (idx, cpuc->running); - x86_pmu.enable (event); +static_call (x86_pmu_enable)(event);perf_event_update_userpage (event); }
@@ -1460,7 +1489,7 @@ void x86_pmu_stop(struct perf_event *evestruct hw_perf_event *hwc = &event->hw;if (test_bit (hwc->idx, cpuc->active_mask)) { - x86_pmu.disable (event); +static_call (x86_pmu_disable)(event);__clear_bit (hwc->idx, cpuc->active_mask); cpuc->events[hwc->idx] =NULL ;WARN_ON_ONCE (hwc->state &PERF_HES_STOPPED );
@@ -1510,8 +1539,7 @@ static void x86_pmu_del(struct perf_evenif (i >= cpuc->n_events - cpuc->n_added) --cpuc->n_added; -if (x86_pmu.put_event_constraints) - x86_pmu.put_event_constraints (cpuc, event); +static_call_cond (x86_pmu_put_event_constraints)(cpuc, event);/* Delete the array entry. */ while (++i < cpuc->n_events) {
@@ -1524,13 +1552,12 @@ static void x86_pmu_del(struct perf_evenperf_event_update_userpage (event); do_del: -if (x86_pmu.del) { -/* -* This is after x86_pmu_stop(); so we disable LBRs after any -* event can need them etc.. -*/ - x86_pmu.del (event); - } + +/* +* This is after x86_pmu_stop(); so we disable LBRs after any +* event can need them etc.. +*/ +static_call_cond (x86_pmu_del)(event); }int x86_pmu_handle_irq (struct pt_regs *regs)
@@ -1608,7 +1635,7 @@ perf_event_nmi_handler(unsigned int cmd,return NMI_DONE ; start_clock =sched_clock (); - ret = x86_pmu.handle_irq (regs); + ret =static_call (x86_pmu_handle_irq)(regs); finish_clock =sched_clock ();perf_sample_event_took (finish_clock - start_clock);
@@ -1821,6 +1848,38 @@ ssize_t x86_event_sysfs_show(char *page,static struct attribute_group x86_pmu_attr_group;static struct attribute_group x86_pmu_caps_group; +static void x86_pmu_static_call_update (void ) +{ +static_call_update (x86_pmu_handle_irq, x86_pmu.handle_irq); +static_call_update (x86_pmu_disable_all, x86_pmu.disable_all); +static_call_update (x86_pmu_enable_all, x86_pmu.enable_all); +static_call_update (x86_pmu_enable, x86_pmu.enable); +static_call_update (x86_pmu_disable, x86_pmu.disable); + +static_call_update (x86_pmu_add, x86_pmu.add); +static_call_update (x86_pmu_del, x86_pmu.del); +static_call_update (x86_pmu_read, x86_pmu.read); + +static_call_update (x86_pmu_schedule_events, x86_pmu.schedule_events); +static_call_update (x86_pmu_get_event_constraints, x86_pmu.get_event_constraints); +static_call_update (x86_pmu_put_event_constraints, x86_pmu.put_event_constraints); + +static_call_update (x86_pmu_start_scheduling, x86_pmu.start_scheduling); +static_call_update (x86_pmu_commit_scheduling, x86_pmu.commit_scheduling); +static_call_update (x86_pmu_stop_scheduling, x86_pmu.stop_scheduling); + +static_call_update (x86_pmu_sched_task, x86_pmu.sched_task); +static_call_update (x86_pmu_swap_task_ctx, x86_pmu.swap_task_ctx); + +static_call_update (x86_pmu_drain_pebs, x86_pmu.drain_pebs); +static_call_update (x86_pmu_pebs_aliases, x86_pmu.pebs_aliases); +} + +static void _x86_pmu_read (struct perf_event *event) +{ +x86_perf_event_update (event); +} +static int __init init_hw_perf_events (void ) {struct x86_pmu_quirk *quirk;
@@ -1889,6 +1948,11 @@ static int __init init_hw_perf_events(vopr_info ("... fixed-purpose events: %d\n" , x86_pmu.num_counters_fixed);pr_info ("... event mask: %016Lx\n" , x86_pmu.intel_ctrl); +if (!x86_pmu.read) + x86_pmu.read = _x86_pmu_read; + +x86_pmu_static_call_update (); +/* * Install callbacks. Core will call them for each online * cpu.
@@ -1925,11 +1989,9 @@ static int __init init_hw_perf_events(vo }early_initcall (init_hw_perf_events); -static inline void x86_pmu_read (struct perf_event *event) +static void x86_pmu_read (struct perf_event *event) { -if (x86_pmu.read) -return x86_pmu.read (event); -x86_perf_event_update (event); +static_call (x86_pmu_read)(event); }/*
@@ -2006,7 +2068,7 @@ static int x86_pmu_commit_txn(struct pmuif (!x86_pmu_initialized ())return -EAGAIN ; - ret = x86_pmu.schedule_events (cpuc, n, assign); + ret =static_call (x86_pmu_schedule_events)(cpuc, n, assign);if (ret)return ret;
@@ -2299,15 +2361,13 @@ static const struct attribute_group *x86static void x86_pmu_sched_task (struct perf_event_context *ctx,bool sched_in) { -if (x86_pmu.sched_task) - x86_pmu.sched_task (ctx, sched_in); +static_call_cond (x86_pmu_sched_task)(ctx, sched_in); }static void x86_pmu_swap_task_ctx (struct perf_event_context *prev,struct perf_event_context *next) { -if (x86_pmu.swap_task_ctx) - x86_pmu.swap_task_ctx (prev, next); +static_call_cond (x86_pmu_swap_task_ctx)(prev, next); }void perf_check_microcode (void )
Peter Zijlstra
maintainerPATCH v6 3/17 module: Properly propagate MODULE_STATE_COMING failure
In reply to Peter ZijlstraNow that notifiers got unbroken; use the proper interface to handle notifier errors and propagate them.
There were already MODULE_STATE_COMING notifiers that failed; notably:
- jump_label_module_notifier()
- tracepoint_module_notify()
- bpf_event_notify()
By propagating this error, we fix those users.
--- kernel/module.c | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-)
kernel/module.c +7 -3
@@ -3751,9 +3751,13 @@ static int prepare_coming_module(structif (err)return err; -blocking_notifier_call_chain (&module_notify_list, -MODULE_STATE_COMING , mod); -return 0 ; + err =blocking_notifier_call_chain_robust (&module_notify_list, +MODULE_STATE_COMING ,MODULE_STATE_GOING , mod); + err =notifier_to_errno (err); +if (err) +klp_module_going (mod); + +return err; } static int unknown_module_param_cb(char *param, char *val, const char *modname,
Peter Zijlstra
maintainerPATCH v6 2/17 module: Fix up module_notifier return values
In reply to Peter ZijlstraWhile auditing all module notifiers I noticed a whole bunch of fail wrt the return value. Notifiers have a 'special' return semantics.
As is; NOTIFY_DONE vs NOTIFY_OK is a bit vague; but notifier_from_errno(0) results in NOTIFY_OK and NOTIFY_DONE has a comment that says "Don't care".
Quoted 1 line
From this I've used
NOTIFY_DONEwhen the function completely ignores
the callback and notifier_to_error() isn't used.
--- drivers/oprofile/buffer_sync.c | 4 ++-- kernel/trace/bpf_trace.c | 8 ++++++-- kernel/trace/trace.c | 2 +- kernel/trace/trace_events.c | 2 +- kernel/trace/trace_printk.c | 4 ++-- kernel/tracepoint.c | 2 +- 6 files changed, 13 insertions(+), 9 deletions(-)
drivers/oprofile/buffer_sync.c +2 -2
@@ -116,7 +116,7 @@ module_load_notify(struct notifier_block {#ifdef CONFIG_MODULES if (val !=MODULE_STATE_COMING ) -return 0 ; +return NOTIFY_DONE ;/* FIXME: should we process all CPU buffers ? */ mutex_lock (&buffer_mutex);
@@ -124,7 +124,7 @@ module_load_notify(struct notifier_blockadd_event_entry (MODULE_LOADED_CODE );mutex_unlock (&buffer_mutex);#endif -return 0 ; +return NOTIFY_OK ; }
kernel/trace/bpf_trace.c +6 -2
@@ -1451,10 +1451,11 @@ static int bpf_event_notify(struct notif {struct bpf_trace_module *btm, *tmp;struct module *mod = module; +int ret =0 ;if (mod->num_bpf_raw_events ==0 || (op !=MODULE_STATE_COMING && op !=MODULE_STATE_GOING )) -return 0 ; + goto out;mutex_lock (&bpf_module_mutex);
@@ -1464,6 +1465,8 @@ static int bpf_event_notify(struct notifif (btm) { btm->module = module;list_add (&btm->list, &bpf_trace_modules); + }else { + ret = -ENOMEM ; }break ;case MODULE_STATE_GOING :
@@ -1479,7 +1482,8 @@ static int bpf_event_notify(struct notifmutex_unlock (&bpf_module_mutex); -return 0 ; +out: +return notifier_from_errno (ret); }static struct notifier_block bpf_module_nb = {
kernel/trace/trace.c +1 -1
@@ -8696,7 +8696,7 @@ static int trace_module_notify(struct nobreak ; } -return 0 ; +return NOTIFY_OK ; }static struct notifier_block trace_module_nb = {
kernel/trace/trace_events.c +1 -1
@@ -2442,7 +2442,7 @@ static int trace_module_notify(struct nomutex_unlock (&trace_types_lock);mutex_unlock (&event_mutex); -return 0 ; +return NOTIFY_OK ; }static struct notifier_block trace_module_nb = {
kernel/trace/trace_printk.c +2 -2
@@ -95,7 +95,7 @@ static int module_trace_bprintk_format_nif (val ==MODULE_STATE_COMING )hold_module_trace_bprintk_format (start, end); } -return 0 ; +return NOTIFY_OK ; }/*
@@ -173,7 +173,7 @@ __init static int module_trace_bprintk_format_notify(struct notifier_block *self, unsigned long val, void *data) { - return 0; + return NOTIFY_OK; }static inline const char **find_next_mod_format (int start_index,void *v,const char **fmt,loff_t *pos)
kernel/tracepoint.c +1 -1
@@ -521,7 +521,7 @@ static int tracepoint_module_notify(strucase MODULE_STATE_UNFORMED :break ; } -return ret; +return notifier_from_errno (ret); }static struct notifier_block tracepoint_module_nb = {
Peter Zijlstra
maintainerv6 cover Add static_call
Hi all,
Hopefully for the last time...
static_call(), is the idea of static_branch() applied to indirect function calls. Remove a data load (indirection) by modifying the text.
The inline implementation still relies on objtool to generate the .static_call_sites section, mostly because this is a natural place for x86_64 and works for both GCC and LLVM. Other architectures can pick other means if/when they implement the inline patching. The out-of-line (aka. trampoline) variant doesn't require this.
Patches go on top of: linus + tip/objtool/core
Patches can also be found here:
git://git.kernel.org/pub/scm/linux/kernel/git/peterz/queue.git x86/static_call
Changes since the last time:
- rebased
- fixed ftrace synthetic events (rostedt)
Peter Zijlstra
maintainerPATCH v6 13/17 static_call: Add static_call_cond()
Extend the static_call infrastructure to optimize the following common pattern:
if (func_ptr) func_ptr(args...)
For the trampoline (which is in effect a tail-call), we patch the JMP.d32 into a RET, which then directly consumes the trampoline call.
For the in-line sites we replace the CALL with a NOP5.
NOTE: this is 'obviously' limited to functions with a 'void' return type.
NOTE: DEFINE_STATIC_COND_CALL() only requires a typename, as opposed
to a full function.
--- arch/x86/include/asm/static_call.h | 12 +++-- arch/x86/kernel/static_call.c | 42 +++++++++++++----- include/linux/static_call.h | 86 +++++++++++++++++++++++++++++++++++++ 3 files changed, 127 insertions(+), 13 deletions(-)
arch/x86/include/asm/static_call.h +9 -3
@@ -20,15 +20,21 @@* it does tail-call optimization on the call; since you cannot compute the * relative displacement across sections. */ -#define ARCH_DEFINE_STATIC_CALL_TRAMP (name, func) \ + +#define __ARCH_DEFINE_STATIC_CALL_TRAMP (name, insns) \ asm(".pushsection .static_call.text, \"ax\" \n" \ ".align 4 \n" \ ".globl " STATIC_CALL_TRAMP_STR(name) " \n" \ STATIC_CALL_TRAMP_STR(name) ": \n" \ - " .byte 0xe9 # jmp.d32 \n" \ - " .long " #func " - (. + 4) \n" \ + insns " \n" \ ".type " STATIC_CALL_TRAMP_STR(name) ", @function \n" \ ".size " STATIC_CALL_TRAMP_STR(name) ", . - " STATIC_CALL_TRAMP_STR(name) " \n" \ ".popsection \n") +#define ARCH_DEFINE_STATIC_CALL_TRAMP (name, func) \ + __ARCH_DEFINE_STATIC_CALL_TRAMP(name, ".byte 0xe9; .long " #func " - (. + 4)") + +#define ARCH_DEFINE_STATIC_CALL_NULL_TRAMP (name) \ + __ARCH_DEFINE_STATIC_CALL_TRAMP(name, "ret; nop; nop; nop; nop") +#endif /* _ASM_STATIC_CALL_H */
arch/x86/kernel/static_call.c +32 -10
61 lines not on this page · raw message
include/linux/static_call.h +86 -0
122 lines not on this page · raw message
Peter Zijlstra
maintainerPATCH v6 8/17 static_call: Avoid kprobes on inline static_call()s
In reply to Peter ZijlstraSimilar to how we disallow kprobes on any other dynamic text (ftrace/jump_label) also disallow kprobes on inline static_call()s.
--- arch/x86/kernel/kprobes/opt.c | 4 +- include/linux/static_call.h | 11 +++++++ kernel/kprobes.c | 2 + kernel/static_call.c | 64 ++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 80 insertions(+), 1 deletion(-)
arch/x86/kernel/kprobes/opt.c +3 -1
@@ -17,6 +17,7 @@#include <linux/ftrace.h> #include <linux/frame.h> #include <linux/pgtable.h> +#include <linux/static_call.h> #include <asm/text-patching.h> #include <asm/cacheflush.h>
@@ -209,7 +210,8 @@ static int copy_optimized_instructions(u/* Check whether the address range is reserved */ if (ftrace_text_reserved (src, src + len -1 ) ||alternatives_text_reserved (src, src + len -1 ) || -jump_label_text_reserved (src, src + len -1 )) +jump_label_text_reserved (src, src + len -1 ) || +static_call_text_reserved (src, src + len -1 ))return -EBUSY ;return len;
include/linux/static_call.h +11 -0
@@ -110,6 +110,7 @@ struct static_call_key {extern void __static_call_update (struct static_call_key *key,void *tramp,void *func);extern int static_call_mod_init (struct module *mod); +extern int static_call_text_reserved (void *start,void *end);#define DEFINE_STATIC_CALL (name, _func) \ DECLARE_STATIC_CALL(name, _func); \
@@ -153,6 +154,11 @@ void __static_call_update(struct static_cpus_read_unlock (); } +static inline int static_call_text_reserved (void *start,void *end) +{ +return 0 ; +} +#define EXPORT_STATIC_CALL (name) \ EXPORT_SYMBOL(STATIC_CALL_KEY(name)); \ EXPORT_SYMBOL(STATIC_CALL_TRAMP(name))
@@ -182,6 +188,11 @@ void __static_call_update(struct static_WRITE_ONCE (key->func, func); } +static inline int static_call_text_reserved (void *start,void *end) +{ +return 0 ; +} +#define EXPORT_STATIC_CALL (name) EXPORT_SYMBOL(STATIC_CALL_KEY(name))#define EXPORT_STATIC_CALL_GPL (name) EXPORT_SYMBOL_GPL(STATIC_CALL_KEY(name))
kernel/kprobes.c +2 -0
@@ -35,6 +35,7 @@#include <linux/ftrace.h> #include <linux/cpu.h> #include <linux/jump_label.h> +#include <linux/static_call.h> #include <asm/sections.h> #include <asm/cacheflush.h>
@@ -1591,6 +1592,7 @@ static int check_kprobe_address_safe(strif (!kernel_text_address ((unsigned long ) p->addr) ||within_kprobe_blacklist ((unsigned long ) p->addr) ||jump_label_text_reserved (p->addr, p->addr) || +static_call_text_reserved (p->addr, p->addr) ||find_bug ((unsigned long )p->addr)) { ret = -EINVAL ; goto out;
kernel/static_call.c +64 -0
78 lines not on this page · raw message
Re: [PATCH v6 06/17] static_call: Add basic static call infrastructure
In reply to Peter ZijlstraQuoted 17 lines · On Fri, 10 Jul 2020 15:38:37 +0200 Peter Zijlstra <[email protected]> wrote:
From: Josh Poimboeuf <[email protected]>
Static calls are a replacement for global function pointers. They use code patching to allow direct calls to be used instead of indirect calls. 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.
The concept and code are an extension of previous work done by Ard Biesheuvel and Steven Rostedt:
https://lkml.kernel.org/r/[email protected]
https://lkml.kernel.org/r/[email protected]There are two implementations, depending on arch support:
Quoted 8 lines
1) out-of-line: patched trampolines (
CONFIG_HAVE_STATIC_CALL)
2) basic function pointersFor more details, see the comments in
include/linux/static_call.h.[peterz: simplified interface]Signed-off-by Josh Poimboeuf <[email protected]>Signed-off-by Peter Zijlstra (Intel) <[email protected]>
-- Steve
Re: [PATCH v6 07/17] static_call: Add inline static call infrastructure
In reply to Peter ZijlstraQuoted 13 lines · On Fri, 10 Jul 2020 15:38:38 +0200 Peter Zijlstra <[email protected]> wrote:
From: Josh Poimboeuf <[email protected]>
Add infrastructure for an arch-specific
CONFIG_HAVE_STATIC_CALL_INLINEoption, which is a faster version ofCONFIG_HAVE_STATIC_CALL. At runtime, the static call sites are patched directly, rather than using the out-of-line trampolines.Compared to out-of-line static calls, the performance benefits are more modest, but still measurable. Steven Rostedt did some tracepoint measurements:
Quoted 8 lines
This code is heavily inspired by the jump label code (aka "static jumps"), as some of the concepts are very similar.
For more details, see the comments in
include/linux/static_call.h.[peterz: simplified interface; merged trampolines]Signed-off-by Josh Poimboeuf <[email protected]>Signed-off-by Peter Zijlstra (Intel) <[email protected]>
-- Steve
Quoted 11 lines
--- arch/Kconfig | 4 include/asm-generic/vmlinux.lds.h | 7 include/linux/module.h | 5 include/linux/static_call.h | 36 ++++ include/linux/static_call_types.h | 13 + kernel/Makefile | 1 kernel/module.c | 5 kernel/static_call.c | 303 ++++++++++++++++++++++++++++++++++++++ 8 files changed, 373 insertions(+), 1 deletion(-) create mode 100644 kernel/static_call.c
Steven Rostedt
maintainerRe: [PATCH v6 08/17] static_call: Avoid kprobes on inline static_call()s
In reply to Peter ZijlstraQuoted 4 lines · On Fri, 10 Jul 2020 15:38:39 +0200 Peter Zijlstra <[email protected]> wrote:
Similar to how we disallow kprobes on any other dynamic text (ftrace/jump_label) also disallow kprobes on inline static_call()s.
Signed-off-by Peter Zijlstra (Intel) <[email protected]>
I'd like to have Masami review this patch too.
Quoted 145 lines
--- arch/x86/kernel/kprobes/opt.c | 4 +- include/linux/static_call.h | 11 +++++++ kernel/kprobes.c | 2 + kernel/static_call.c | 64 ++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 80 insertions(+), 1 deletion(-)arch/x86/kernel/kprobes/opt.c +3
-1@@ -17,6 +17,7 @@#include <linux/ftrace.h> #include <linux/frame.h> #include <linux/pgtable.h> +#include <linux/static_call.h> #include <asm/text-patching.h> #include <asm/cacheflush.h> @@ -209,7 +210,8 @@ static int copy_optimized_instructions(u/* Check whether the address range is reserved */ if (ftrace_text_reserved (src, src + len -1 ) ||alternatives_text_reserved (src, src + len -1 ) || -jump_label_text_reserved (src, src + len -1 )) +jump_label_text_reserved (src, src + len -1 ) || +static_call_text_reserved (src, src + len -1 ))return -EBUSY ;return len;include/linux/static_call.h +11
-0@@ -110,6 +110,7 @@ struct static_call_key {extern void __static_call_update (struct static_call_key *key,void *tramp,void *func);extern int static_call_mod_init (struct module *mod); +extern int static_call_text_reserved (void *start,void *end);#define DEFINE_STATIC_CALL (name, _func) \ DECLARE_STATIC_CALL(name, _func); \@@ -153,6 +154,11 @@ void __static_call_update(struct static_cpus_read_unlock (); } +static inline int static_call_text_reserved (void *start,void *end) +{ +return 0 ; +} +#define EXPORT_STATIC_CALL (name) \ EXPORT_SYMBOL(STATIC_CALL_KEY(name)); \ EXPORT_SYMBOL(STATIC_CALL_TRAMP(name))@@ -182,6 +188,11 @@ void __static_call_update(struct static_WRITE_ONCE (key->func, func); } +static inline int static_call_text_reserved (void *start,void *end) +{ +return 0 ; +} +#define EXPORT_STATIC_CALL (name) EXPORT_SYMBOL(STATIC_CALL_KEY(name))#define EXPORT_STATIC_CALL_GPL (name) EXPORT_SYMBOL_GPL(STATIC_CALL_KEY(name))kernel/kprobes.c +2
-0@@ -35,6 +35,7 @@#include <linux/ftrace.h> #include <linux/cpu.h> #include <linux/jump_label.h> +#include <linux/static_call.h> #include <asm/sections.h> #include <asm/cacheflush.h> @@ -1591,6 +1592,7 @@ static int check_kprobe_address_safe(strif (!kernel_text_address ((unsigned long ) p->addr) ||within_kprobe_blacklist ((unsigned long ) p->addr) ||jump_label_text_reserved (p->addr, p->addr) || +static_call_text_reserved (p->addr, p->addr) ||find_bug ((unsigned long )p->addr)) { ret = -EINVAL ; goto out;kernel/static_call.c +51
-0@@ -204,8 +204,58 @@ static int __static_call_init(struct modreturn 0 ; } +static int addr_conflict (struct static_call_site *site,void *start,void *end) +{ +unsigned long addr = (unsigned long )static_call_addr (site); + +if (addr <= (unsigned long )end && + addr +CALL_INSN_SIZE > (unsigned long )start) +return 1 ; + +return 0 ; +} + +static int __static_call_text_reserved (struct static_call_site *iter_start, +struct static_call_site *iter_stop, +void *start,void *end) +{ +struct static_call_site *iter = iter_start; + +while (iter < iter_stop) { +if (addr_conflict (iter, start, end)) +return 1 ; + iter++; + } + +return 0 ; +} +#ifdef CONFIG_MODULES +static int __static_call_mod_text_reserved (void *start,void *end) +{ +struct module *mod; +int ret; + +preempt_disable (); + mod =__module_text_address ((unsigned long )start); +WARN_ON_ONCE (__module_text_address ((unsigned long )end) != mod); +if (!try_module_get (mod)) + mod =NULL ; +preempt_enable (); + +if (!mod) +return 0 ; + + ret =__static_call_text_reserved (mod->static_call_sites, + mod->static_call_sites + mod->num_static_call_sites, + start, end); + +module_put (mod); + +return ret; +} +static int static_call_add_module (struct module *mod) { return __static_call_init(mod, mod->static_call_sites,@@ -275,6 +325,20 @@ static struct notifier_block static_call#endif /* CONFIG_MODULES */ +int static_call_text_reserved (void *start,void *end)
kernel/static_call.c static struct notifier_block static_call · line 338
Quoted 10 lines
kernel/static_call.c +10
-0+{ +int ret =__static_call_text_reserved (__start_static_call_sites, + __stop_static_call_sites, start, end); + +if (ret) +return ret; + +#ifdef CONFIG_MODULES + ret =__static_call_mod_text_reserved (start, end); +#endif
Nit, but why not have a #else /* !CONFIG_MODULE */ above and just:
static inline int __static_call_mod_text_reserve(..)
{
return 0;
}#endif
?
Then you don't need the extra #ifdef above, and just end it with:
return __static_call_mod_next_reserved(start, end);
-- Steve
Quoted 7 lines
kernel/static_call.c +3
-0+return ret; +} +static void __init static_call_init (void ) {int ret;
Steven Rostedt
maintainerRe: [PATCH v6 09/17] x86/static_call: Add out-of-line static call implementation
In reply to Peter ZijlstraQuoted 6 lines · On Fri, 10 Jul 2020 15:38:40 +0200 Peter Zijlstra <[email protected]> wrote:
From: Josh Poimboeuf <[email protected]>
Add the x86 out-of-line static call implementation. For each key, a permanent trampoline is created which is the destination for all static calls for the given key. The trampoline has a direct jump which gets patched by
static_call_update()when the destination function changes.
FYI, I get the following warnings after applying this patch.
/work/git/linux-test.git/arch/x86/kernel/static_call.c: In function ‘__static_call_transform’:
/work/git/linux-test.git/arch/x86/kernel/static_call.c:9:43: warning: passing argument 2 of ‘text_gen_insn’ makes pointer from integer without a cast [-Wint-conversion]
9 | const void *code = text_gen_insn(opcode, (long)insn, (long)func);
| ^~~~~~~~~~
| |
| long int
In file included from /work/git/linux-test.git/arch/x86/include/asm/static_call.h:5,
from /work/git/linux-test.git/include/linux/static_call.h:62,
from /work/git/linux-test.git/arch/x86/kernel/static_call.c:2:
/work/git/linux-test.git/arch/x86/include/asm/text-patching.h:95:44: note: expected ‘const void *’ but argument is of type ‘long int’
95 | void *text_gen_insn(u8 opcode, const void *addr, const void *dest)
| ~~~~~~~~~~~~^~~~
/work/git/linux-test.git/arch/x86/kernel/static_call.c:9:55: warning: passing argument 3 of ‘text_gen_insn’ makes pointer from integer without a cast [-Wint-conversion]
9 | const void *code = text_gen_insn(opcode, (long)insn, (long)func);
| ^~~~~~~~~~
| |
| long int
In file included from /work/git/linux-test.git/arch/x86/include/asm/static_call.h:5,
from /work/git/linux-test.git/include/linux/static_call.h:62,
from /work/git/linux-test.git/arch/x86/kernel/static_call.c:2:/work/git/linux-test.git/arch/x86/include/asm/text-patching.h:95:62: note: expected ‘const void *’ but argument is of type ‘long int’
95 | void *text_gen_insn(u8 opcode, const void *addr, const void *dest)
-- Steve
Steven Rostedt
maintainerRe: [PATCH v6 10/17] x86/static_call: Add inline static call implementation for x86-64
In reply to Peter ZijlstraQuoted 59 lines · On Fri, 10 Jul 2020 15:38:41 +0200 Peter Zijlstra <[email protected]> wrote:
tools/objtool/check.c +46
-0@@ -16,6 +16,7 @@#include <linux/hashtable.h> #include <linux/kernel.h> +#include <linux/static_call_types.h> #define FAKE_JUMP_OFFSET -1@@ -433,6 +434,99 @@ static int add_dead_ends(struct objtool_return 0 ; } +static int create_static_call_sections (struct objtool_file *file) +{ +struct section *sec, *reloc_sec; +struct reloc *reloc; +struct static_call_site *site; +struct instruction *insn; +struct symbol *key_sym; +char *key_name, *tmp; +int idx; + + sec =find_section_by_name (file->elf,".static_call_sites" ); +if (sec) { +INIT_LIST_HEAD (&file->static_call_list); +WARN ("file already has .static_call_sites section, skipping" ); +return 0 ; + } + +if (list_empty (&file->static_call_list)) +return 0 ; + + idx =0 ; +list_for_each_entry (insn, &file->static_call_list, static_call_node) + idx++; + + sec =elf_create_section (file->elf,".static_call_sites" ,SHF_WRITE , +sizeof (struct static_call_site ), idx); +if (!sec) +return -1 ; + + reloc_sec =elf_create_reloc_section (file->elf, sec,SHT_RELA ); +if (!reloc_sec) +return -1 ; + + idx =0 ; +list_for_each_entry (insn, &file->static_call_list, static_call_node) { + + site = (struct static_call_site *)sec->data->d_buf + idx; +memset (site,0 ,sizeof (struct static_call_site )); + +/* populate reloc for 'addr' */ + reloc =malloc (sizeof (*reloc)); +if (!reloc) { +perror ("malloc" ); +return -1 ; + }
tools/objtool/check.c static int add_dead_ends(struct objtool_ · line 491
Quoted 10 lines
tools/objtool/check.c +10
-0+memset (reloc,0 ,sizeof (*reloc)); + reloc->sym = insn->sec->sym; + reloc->addend = insn->offset; + reloc->type =R_X86_64_PC32 ; + reloc->offset = idx *sizeof (struct static_call_site ); + reloc->sec = reloc_sec; +elf_add_reloc (file->elf, reloc); + +/* find key symbol */ + key_name =strdup (insn->call_dest->name);
Should check for failed allocation of strdup().
Quoted 15 lines
tools/objtool/check.c +15
-0+if (strncmp (key_name,STATIC_CALL_TRAMP_PREFIX_STR , +strlen (STATIC_CALL_TRAMP_PREFIX_STR ))) { +WARN ("static_call: trampoline name malformed: %s" , key_name); +return -1 ; + } + tmp = key_name +STATIC_CALL_TRAMP_PREFIX_LEN -STATIC_CALL_KEY_PREFIX_LEN ; +memcpy (tmp,STATIC_CALL_KEY_PREFIX_STR ,STATIC_CALL_KEY_PREFIX_LEN ); + + key_sym =find_symbol_by_name (file->elf, tmp); +if (!key_sym) { +WARN ("static_call: can't find static_call_key symbol: %s" , tmp); +return -1 ; + } +free (key_name); +
tools/objtool/check.c static int add_dead_ends(struct objtool_ · line 516
Quoted 10 lines
tools/objtool/check.c +10
-0+/* populate reloc for 'key' */ + reloc =malloc (sizeof (*reloc)); +if (!reloc) { +perror ("malloc" ); +return -1 ; + } +memset (reloc,0 ,sizeof (*reloc)); + reloc->sym = key_sym; + reloc->addend =0 ; + reloc->type =R_X86_64_PC32 ;
How easy would this be for other architectures to implement this? That is how much of this function is x86 specific?
-- Steve
Quoted 13 lines
tools/objtool/check.c +13
-0+ reloc->offset = idx *sizeof (struct static_call_site ) +4 ; + reloc->sec = reloc_sec; +elf_add_reloc (file->elf, reloc); + + idx++; + } + +if (elf_rebuild_reloc_section (file->elf, reloc_sec)) +return -1 ; + +return 0 ; +} +
Steven Rostedt
maintainerRe: [PATCH v6 11/17] static_call: Simple self-test
In reply to Peter ZijlstraQuoted 42 lines · On Fri, 10 Jul 2020 15:38:42 +0200 Peter Zijlstra <[email protected]> wrote:
Signed-off-by Peter Zijlstra (Intel) <[email protected]>--- arch/Kconfig | 6 ++++++ kernel/static_call.c | 28 ++++++++++++++++++++++++++++ 2 files changed, 34 insertions(+)arch/Kconfig +6
-0@@ -103,6 +103,12 @@ config STATIC_KEYS_SELFTEST help Boot time self-test of the branch patching code. +config STATIC_CALL_SELFTEST + bool "Static call selftest" + depends on HAVE_STATIC_CALL + help + Boot time self-test of the call patching code. + config OPTPROBES def_bool y depends on KPROBES && HAVE_OPTPROBESkernel/static_call.c +15
-0@@ -364,3 +364,31 @@ static void __init static_call_init(void#endif }early_initcall (static_call_init); + +#ifdef CONFIG_STATIC_CALL_SELFTEST + +static int func_a (int x) +{ +return x+1 ; +} + +static int func_b (int x) +{ +return x+2 ; +} + +DEFINE_STATIC_CALL (sc_selftest, func_a); +
kernel/static_call.c static void __init static_call_init(void · line 391
Quoted 10 lines
kernel/static_call.c +10
-0+static int __init test_static_call_init (void ) +{ +WARN_ON (static_call (sc_selftest)(2 ) !=3 ); +static_call_update (sc_selftest, &func_b); +WARN_ON (static_call (sc_selftest)(2 ) !=4 ); +static_call_update (sc_selftest, &func_a); +WARN_ON (static_call (sc_selftest)(2 ) !=3 ); + +return 0 ; +}
I wonder if this would be better if we were testing the same static call each time?
static int __init run_static_call(int val)
{
return static_call(sc_selftest)(val);
}static struct {
int (*func)(int);
int val;
int expect;
} static_call_data [] = {
{ NULL, 2, 3 }
( func_b, 2 , 4},
{ func_a, 2, 3}
} __initdata;static int __init test_static_call_init(void)
{
int i;
for (i = 0; i < ARRAY_SIZE(static_call_data); i++ ) {
if (static_call_data[i].func)
static_call_update(sc_selftest, static_call_data[i].func);
WARN_ON(run_static_call(static_call_data[i].val) != static_call_data[i].expect);
}
return 0;
}-- Steve
Quoted 4 lines
kernel/static_call.c +3
-0+early_initcall (test_static_call_init); + +#endif /* CONFIG_STATIC_CALL_SELFTEST */
Re: [PATCH v6 12/17] x86/alternatives: Teach text_poke_bp() to emulate RET
In reply to Peter ZijlstraQuoted 4 lines · On Fri, 10 Jul 2020 15:38:43 +0200 Peter Zijlstra <[email protected]> wrote:
Future patches will need to poke a RET instruction, provide the infrastructure required for this.
Signed-off-by Peter Zijlstra (Intel) <[email protected]>
Much easier than emulating a CALL :-)
-- Steve
Steven Rostedt
maintainerRe: [PATCH v6 13/17] static_call: Add static_call_cond()
In reply to Peter Zijlstraarch/x86/kernel/static_call.c line 13
Quoted 9 lines · On Fri, 10 Jul 2020 15:38:44 +0200 Peter Zijlstra <[email protected]> wrote:
arch/x86/kernel/static_call.c +3
-4+static void __static_call_transform (void *insn,enum insn_type type,void *func) { -const void *code =text_gen_insn (opcode, insn, func); +int size =CALL_INSN_SIZE ; +const void *code; -if (WARN_ONCE (*(u8 *)insn != opcode, -"unexpected static call insn opcode 0x%x at %pS\n" , - opcode, insn))
I would still feel better if we did some sort of sanity check before just writing to the text. Confirm this is a jmp, call, ret or nop?
-- Steve
Quoted 28 lines
arch/x86/kernel/static_call.c +20
-3-return ; +switch (type) { +case CALL : + code =text_gen_insn (CALL_INSN_OPCODE , insn, func); +break ; + +case NOP : + code = ideal_nops[NOP_ATOMIC5 ]; +break ; + +case JMP : + code =text_gen_insn (JMP32_INSN_OPCODE , insn, func); +break ; + +case RET : + code =text_gen_insn (RET_INSN_OPCODE , insn, func); + size =RET_INSN_SIZE ; +break ; + } -if (memcmp (insn, code,CALL_INSN_SIZE ) ==0 ) +if (memcmp (insn, code, size) ==0 )return ; -text_poke_bp (insn, code,CALL_INSN_SIZE ,NULL ); +text_poke_bp (insn, code, size,NULL ); }
Steven Rostedt
maintainerRe: [PATCH v6 14/17] static_call: Handle tail-calls
In reply to Peter ZijlstraQuoted 7 lines · On Fri, 10 Jul 2020 15:38:45 +0200 Peter Zijlstra <[email protected]> wrote:
GCC can turn our static_call(name)(args...) into a tail call, in which case we get a JMP.d32 into the trampoline (which then does a further tail-call).
Teach objtool to recognise and mark these in .static_call_sites and adjust the code patching to deal with this.
Hmm, were you able to trigger crashes before this patch?
Quoted 210 lines
Signed-off-by Peter Zijlstra (Intel) <[email protected]>--- arch/x86/kernel/static_call.c | 21 ++++++++++++++++++--- include/linux/static_call.h | 4 ++-- include/linux/static_call_types.h | 7 +++++++ kernel/static_call.c | 21 +++++++++++++-------- tools/include/linux/static_call_types.h | 7 +++++++ tools/objtool/check.c | 18 +++++++++++++----- 6 files changed, 60 insertions(+), 18 deletions(-)arch/x86/kernel/static_call.c +18
-3@@ -41,15 +41,30 @@ static void __static_call_transform(voidtext_poke_bp (insn, code, size,NULL ); } -void arch_static_call_transform (void *site,void *tramp,void *func) +static inline enum insn_type __sc_insn (bool null,bool tail) +{ +/* +* Encode the following table without branches: +* +* tail null insn +* -----+-------+------ +* 0 | 0 | CALL +* 0 | 1 | NOP +* 1 | 0 | JMP +* 1 | 1 | RET +*/ +return 2 *tail + null; +} + +void arch_static_call_transform (void *site,void *tramp,void *func,bool tail) {mutex_lock (&text_mutex);if (tramp) -__static_call_transform (tramp, func ?JMP :RET , func); +__static_call_transform (tramp,__sc_insn (!func, true), func);if (IS_ENABLED (CONFIG_HAVE_STATIC_CALL_INLINE ) && site) -__static_call_transform (site, func ?CALL :NOP , func); +__static_call_transform (site,__sc_insn (!func, tail), func);mutex_unlock (&text_mutex); }include/linux/static_call.h +2
-2@@ -103,7 +103,7 @@/* * Either @site or @tramp can be NULL. */ -extern void arch_static_call_transform (void *site,void *tramp,void *func); +extern void arch_static_call_transform (void *site,void *tramp,void *func,bool tail);#define STATIC_CALL_TRAMP_ADDR (name) &STATIC_CALL_TRAMP(name)@@ -206,7 +206,7 @@ void __static_call_update(struct static_ {cpus_read_lock ();WRITE_ONCE (key->func, func); -arch_static_call_transform (NULL , tramp, func); +arch_static_call_transform (NULL , tramp, func, false);cpus_read_unlock (); }include/linux/static_call_types.h +7
-0@@ -17,6 +17,13 @@#define STATIC_CALL_TRAMP_STR (name) __stringify(STATIC_CALL_TRAMP(name))/* +* Flags in the low bits of static_call_site::key. +*/ +#define STATIC_CALL_SITE_TAIL 1UL/* tail call */ +#define STATIC_CALL_SITE_INIT 2UL/* init section */ +#define STATIC_CALL_SITE_FLAGS 3UL + +/* * The static call site table needs to be created by external tooling (objtool * or a compiler plugin). */ kernel/static_call.c +13
-8@@ -15,8 +15,6 @@ extern struct static_call_site __start_sstatic bool static_call_initialized; -#define STATIC_CALL_INIT 1UL -/* mutex to protect key modules/sites */ static DEFINE_MUTEX (static_call_mutex);@@ -39,18 +37,23 @@ static inline void *static_call_addr(strstatic inline struct static_call_key *static_call_key (const struct static_call_site *site) {return (struct static_call_key *) - (((long )site->key + (long )&site->key) & ~STATIC_CALL_INIT ); + (((long )site->key + (long )&site->key) & ~STATIC_CALL_SITE_FLAGS ); }/* These assume the key is word-aligned. */ static inline bool static_call_is_init (struct static_call_site *site) { -return ((long )site->key + (long )&site->key) &STATIC_CALL_INIT ; +return ((long )site->key + (long )&site->key) &STATIC_CALL_SITE_INIT ; +} + +static inline bool static_call_is_tail (struct static_call_site *site) +{ +return ((long )site->key + (long )&site->key) &STATIC_CALL_SITE_TAIL ; }static inline void static_call_set_init (struct static_call_site *site) { - site->key = ((long )static_call_key (site) |STATIC_CALL_INIT ) - + site->key = ((long )static_call_key (site) |STATIC_CALL_SITE_INIT ) - (long )&site->key; }@@ -104,7 +107,7 @@ void __static_call_update(struct static_ key->func = func; -arch_static_call_transform (NULL , tramp, func); +arch_static_call_transform (NULL , tramp, func, false);/* * If uninitialized, we'll not update the callsites, but they still @@ -154,7 +157,8 @@ void __static_call_update(struct static_continue ; } -arch_static_call_transform (site_addr,NULL , func); +arch_static_call_transform (site_addr,NULL , func, +static_call_is_tail (site)); } }@@ -198,7 +202,8 @@ static int __static_call_init(struct mod key->mods = site_mod; } -arch_static_call_transform (site_addr,NULL , key->func); +arch_static_call_transform (site_addr,NULL , key->func, +static_call_is_tail (site)); }return 0 ;tools/include/linux/static_call_types.h +7
-0@@ -17,6 +17,13 @@#define STATIC_CALL_TRAMP_STR (name) __stringify(STATIC_CALL_TRAMP(name))/* +* Flags in the low bits of static_call_site::key. +*/ +#define STATIC_CALL_SITE_TAIL 1UL/* tail call */ +#define STATIC_CALL_SITE_INIT 2UL/* init section */ +#define STATIC_CALL_SITE_FLAGS 3UL + +/* * The static call site table needs to be created by external tooling (objtool * or a compiler plugin). */ tools/objtool/check.c +13
-1@@ -511,7 +511,7 @@ static int create_static_call_sections(s }memset (reloc,0 ,sizeof (*reloc)); reloc->sym = key_sym; - reloc->addend =0 ; + reloc->addend =is_sibling_call (insn) ?STATIC_CALL_SITE_TAIL :0 ; reloc->type =R_X86_64_PC32 ; reloc->offset = idx *sizeof (struct static_call_site ) +4 ; reloc->sec = reloc_sec;@@ -720,6 +720,10 @@ static int add_jump_destinations(struct }else {/* external sibling call */ insn->call_dest = reloc->sym; +if (insn->call_dest->static_call_tramp) { +list_add_tail (&insn->static_call_node, + &file->static_call_list); + }continue ; }@@ -771,6 +775,10 @@ static int add_jump_destinations(struct/* internal sibling call */ insn->call_dest = insn->jump_dest->func; +if (insn->call_dest->static_call_tramp) { +list_add_tail (&insn->static_call_node, + &file->static_call_list); + } } } }@@ -1639,6 +1647,10 @@ static int decode_sections(struct objtooif (ret)return ret; + ret =read_static_call_tramps (file); +if (ret) +return ret; + ret =add_jump_destinations (file);if (ret)return ret;
tools/objtool/check.c static int decode_sections(struct objtoo · line 1676
Quoted 6 lines
tools/objtool/check.c +0
-3@@ -1671,10 +1683,6 @@ static int decode_sections(struct objtooif (ret)return ret; - ret =read_static_call_tramps (file); -if (ret) -return ret;
Hmm, what's the reason for moving this above? Should we have a comment here if there's importance that read_static_call_trampoline() is done earlier?
-- Steve
Quoted 5 lines
tools/objtool/check.c +0
-1-return 0 ; }
Steven Rostedt
maintainerRe: [PATCH v6 15/17] static_call: Allow early init
In reply to Peter ZijlstraQuoted 8 lines · On Fri, 10 Jul 2020 15:38:46 +0200 Peter Zijlstra <[email protected]> wrote:
In order to use
static_call()to wire upx86_pmu, we need to initialize earlier; copy some of the tricks fromjump_labelto enable this.Primarily we overload
key->nextto store a sites pointer when there are no modules, this avoids having to usekmalloc()to initialize the sites and allows us to run much earlier.
I'm confused. What was the need to have key->next store site pointers in order to move it up earlier?
-- Steve
Quoted 215 lines
(arguably, this is much much earlier than needed for perf, but it might allow other uses.)
Signed-off-by Peter Zijlstra (Intel) <[email protected]>--- arch/x86/kernel/setup.c | 2 + arch/x86/kernel/static_call.c | 8 +++++- include/linux/static_call.h | 15 +++++++++-- kernel/static_call.c | 55 +++++++++++++++++++++++++++++++++++++++--- 4 files changed, 74 insertions(+), 6 deletions(-)arch/x86/kernel/setup.c +2
-0@@ -19,6 +19,7 @@#include <linux/hugetlb.h> #include <linux/tboot.h> #include <linux/usb/xhci-dbgp.h> +#include <linux/static_call.h> #include <uapi/linux/mount.h> @@ -848,6 +849,7 @@ void __init setup_arch(char **cmdline_p)early_cpu_init ();arch_init_ideal_nops ();jump_label_init (); +static_call_init ();early_ioremap_init ();setup_olpc_ofw_pgd ();arch/x86/kernel/static_call.c +7
-1@@ -11,7 +11,7 @@ enum insn_type { RET = 3, /* tramp / site cond-tail-call */ }; -static void __static_call_transform (void *insn,enum insn_type type,void *func) +static void __ref __static_call_transform (void *insn,enum insn_type type,void *func) {int size =CALL_INSN_SIZE ;const void *code;@@ -33,11 +33,17 @@ static void __static_call_transform(void code =text_gen_insn (RET_INSN_OPCODE , insn, func); size =RET_INSN_SIZE ;break ; + +default :/* GCC is a moron -- it figures @code can be uninitialized below */ +BUG (); }if (memcmp (insn, code, size) ==0 )return ; +if (unlikely (system_state ==SYSTEM_BOOTING )) +return text_poke_early (insn, code, size); +text_poke_bp (insn, code, size,NULL ); }include/linux/static_call.h +13
-2@@ -99,6 +99,8 @@ extern void arch_static_call_transform(v #ifdef CONFIG_HAVE_STATIC_CALL_INLINE +extern void __init static_call_init(void); + struct static_call_mod { struct static_call_mod *next; struct module *mod; /* for vmlinux, mod == NULL */@@ -107,7 +109,12 @@ struct static_call_mod {struct static_call_key {void *func; -struct static_call_mod *mods; +union { +/* bit 0: 0 = mods, 1 = sites */ +unsigned long type; +struct static_call_mod *mods; +struct static_call_site *sites; + }; };extern void __static_call_update (struct static_call_key *key,void *tramp,void *func);@@ -118,7 +125,7 @@ extern int static_call_text_reserved(voiDECLARE_STATIC_CALL (name, _func); \struct static_call_key STATIC_CALL_KEY (name ) = { \ .func = _func, \ - .mods =NULL , \ + .type =1 , \ }; \ARCH_DEFINE_STATIC_CALL_TRAMP (name, _func)@@ -143,6 +150,8 @@ extern int static_call_text_reserved(voi#elif defined(CONFIG_HAVE_STATIC_CALL ) +static inline void static_call_init (void ) { } +struct static_call_key {void *func; };@@ -188,6 +197,8 @@ static inline int static_call_text_reser#else /* Generic implementation */ +static inline void static_call_init (void ) { } +struct static_call_key {void *func; };kernel/static_call.c +52
-3@@ -94,10 +94,31 @@ static inline void static_call_sort_entr static_call_site_cmp, static_call_site_swap); } +static inline bool static_call_key_has_mods (struct static_call_key *key) +{ +return !(key->type &1 ); +} + +static inline struct static_call_mod *static_call_key_next (struct static_call_key *key) +{ +if (!static_call_key_has_mods (key)) +return NULL ; + +return key->mods; +} + +static inline struct static_call_site *static_call_key_sites (struct static_call_key *key) +{ +if (static_call_key_has_mods (key)) +return NULL ; + +return (struct static_call_site *)(key->type & ~1 ); +} +void __static_call_update (struct static_call_key *key,void *tramp,void *func) {struct static_call_site *site, *stop; -struct static_call_mod *site_mod; +struct static_call_mod *site_mod, first;cpus_read_lock ();static_call_lock ();@@ -116,13 +137,22 @@ void __static_call_update(struct static_if (WARN_ON_ONCE (!static_call_initialized)) goto done; -for (site_mod = key->mods; site_mod; site_mod = site_mod->next) { + first = (struct static_call_mod ){ + .next =static_call_key_next (key), + .mod =NULL , + .sites =static_call_key_sites (key), + }; + +for (site_mod = &first; site_mod; site_mod = site_mod->next) {struct module *mod = site_mod->mod;if (!site_mod->sites) {/* * This can happen if the static call key is defined in * a module which doesn't use it. +* +* It also happens in the has_mods case, where the +* 'first' entry has no sites associated with it. */ continue ; }@@ -192,16 +222,35 @@ static int __static_call_init(struct modif (key != prev_key) { prev_key = key; +if (!mod) { + key->sites = site; + key->type |=1 ; + goto do_transform; + } + site_mod =kzalloc (sizeof (*site_mod),GFP_KERNEL );if (!site_mod)return -ENOMEM ; +if (!static_call_key_has_mods (key)) { + site_mod->mod =NULL ; + site_mod->next =NULL ; + site_mod->sites =static_call_key_sites (key); + + key->mods = site_mod; + + site_mod =kzalloc (sizeof (*site_mod),GFP_KERNEL ); +if (!site_mod) +return -ENOMEM ; + } + site_mod->mod = mod; site_mod->sites = site; site_mod->next = key->mods; key->mods = site_mod; } +do_transform:arch_static_call_transform (site_addr,NULL , key->func,static_call_is_tail (site)); }@@ -344,7 +393,7 @@ int static_call_text_reserved(void *starreturn ret; } -static void __init static_call_init (void ) +void __init static_call_init (void ) {int ret;
Peter Zijlstra
maintainerRe: [PATCH v6 14/17] static_call: Handle tail-calls
In reply to Steven RostedtQuoted 7 lines · Steven Rostedt
Quoted 4 lines · On Fri, 10 Jul 2020 15:38:45 +0200 Peter Zijlstra <[email protected]> wrote:
GCC can turn our static_call(name)(args...) into a tail call, in which case we get a JMP.d32 into the trampoline (which then does a further tail-call).
Quoted 5 lines
Quoted 3 lines
Teach objtool to recognise and mark these in .static_call_sites and adjust the code patching to deal with this.
Hmm, were you able to trigger crashes before this patch?
No, just a bunch of tail-calls that didn't get patched and would still point to the trampoline.
Peter Zijlstra
maintainerRe: [PATCH v6 14/17] static_call: Handle tail-calls
In reply to Steven RostedtQuoted 17 lines · Steven Rostedt
Quoted 15 lines · On Fri, 10 Jul 2020 15:38:45 +0200 Peter Zijlstra <[email protected]> wrote:
tools/objtool/check.c +4
-0@@ -1639,6 +1647,10 @@ static int decode_sections(struct objtooif (ret)return ret; + ret =read_static_call_tramps (file); +if (ret) +return ret; + ret =add_jump_destinations (file);if (ret)return ret;@@ -1671,10 +1683,6 @@ static int decode_sections(struct objtooif (ret)return ret;
Quoted 7 lines
tools/objtool/check.c static int decode_sections(struct objtoo · line 1676
Quoted 3 lines
tools/objtool/check.c +0
-3- ret =read_static_call_tramps (file); -if (ret) -return ret;Hmm, what's the reason for moving this above? Should we have a comment here if there's importance that
read_static_call_trampoline()is done earlier?
I suppose comments is something objtool lacks more of.
The reason is that add_jump_destination() is the thing that does tail-call detection, and if it wants to add static-call sites, it needs to know about the trampolines.
Peter Zijlstra
maintainerRe: [PATCH v6 15/17] static_call: Allow early init
In reply to Steven RostedtQuoted 7 lines · Steven Rostedt
Quoted 4 lines · On Fri, 10 Jul 2020 15:38:46 +0200 Peter Zijlstra <[email protected]> wrote:
In order to use
static_call()to wire upx86_pmu, we need to initialize earlier; copy some of the tricks fromjump_labelto enable this.
Quoted 7 lines
Quoted 4 lines
Primarily we overload
key->nextto store a sites pointer when there are no modules, this avoids having to usekmalloc()to initialize the sites and allows us to run much earlier.I'm confused. What was the need to have
key->nextstore site pointers in order to move it up earlier?
The critical part was to not need an allocation.
Peter Zijlstra
maintainerRe: [PATCH v6 13/17] static_call: Add static_call_cond()
In reply to Steven RostedtQuoted 9 lines · Steven Rostedt
Quoted 6 lines · On Fri, 10 Jul 2020 15:38:44 +0200 Peter Zijlstra <[email protected]> wrote:
arch/x86/kernel/static_call.c +3
-1+static void __static_call_transform (void *insn,enum insn_type type,void *func) { -const void *code =text_gen_insn (opcode, insn, func); +int size =CALL_INSN_SIZE ; +const void *code;
Quoted 6 lines
arch/x86/kernel/static_call.c line 13
Quoted 3 lines
arch/x86/kernel/static_call.c +0
-3-if (WARN_ONCE (*(u8 *)insn != opcode, -"unexpected static call insn opcode 0x%x at %pS\n" , - opcode, insn))I would still feel better if we did some sort of sanity check before just writing to the text. Confirm this is a jmp, call, ret or nop?
I'll see if I can come up with something, but I'm not sure we keep enough state to be able to reconstruct what should be there.
Peter Zijlstra
maintainerRe: [PATCH v6 10/17] x86/static_call: Add inline static call implementation for x86-64
In reply to Steven RostedtQuoted 7 lines · Steven Rostedt
tools/objtool/check.c static int add_dead_ends(struct objtool_ · line 491
Quoted 2 lines · On Fri, 10 Jul 2020 15:38:41 +0200 Peter Zijlstra <[email protected]> wrote:
tools/objtool/check.c +2
-0+/* find key symbol */ + key_name =strdup (insn->call_dest->name);Should check for failed allocation of
strdup().
Rigt, I'll go add the perror and exit.
Quoted 20 lines
Quoted 20 lines
tools/objtool/check.c +20
-0+if (strncmp (key_name,STATIC_CALL_TRAMP_PREFIX_STR , +strlen (STATIC_CALL_TRAMP_PREFIX_STR ))) { +WARN ("static_call: trampoline name malformed: %s" , key_name); +return -1 ; + } + tmp = key_name +STATIC_CALL_TRAMP_PREFIX_LEN -STATIC_CALL_KEY_PREFIX_LEN ; +memcpy (tmp,STATIC_CALL_KEY_PREFIX_STR ,STATIC_CALL_KEY_PREFIX_LEN ); + + key_sym =find_symbol_by_name (file->elf, tmp); +if (!key_sym) { +WARN ("static_call: can't find static_call_key symbol: %s" , tmp); +return -1 ; + } +free (key_name); + +/* populate reloc for 'key' */ + reloc =malloc (sizeof (*reloc)); +if (!reloc) { +perror ("malloc" ); +return -1 ;
Quoted 8 lines
tools/objtool/check.c static int add_dead_ends(struct objtool_ · line 516
Quoted 5 lines
tools/objtool/check.c +5
-0+ } +memset (reloc,0 ,sizeof (*reloc)); + reloc->sym = key_sym; + reloc->addend =0 ; + reloc->type =R_X86_64_PC32 ;How easy would this be for other architectures to implement this? That is how much of this function is x86 specific?
I'm not sure; I think it's fairly generic except for the reloc types. So ARM64 for example might need to use SHT_REL and R_AARCH64_PREL32 instead. But I suppose we'll see that if/when someone tries to make it work.
Peter Zijlstra
maintainerRe: [PATCH v6 09/17] x86/static_call: Add out-of-line static call implementation
In reply to Steven RostedtQuoted 10 lines · Steven Rostedt
Quoted 6 lines · On Fri, 10 Jul 2020 15:38:40 +0200 Peter Zijlstra <[email protected]> wrote:
From: Josh Poimboeuf <[email protected]>
Add the x86 out-of-line static call implementation. For each key, a permanent trampoline is created which is the destination for all static calls for the given key. The trampoline has a direct jump which gets patched by
static_call_update()when the destination function changes.
Quoted 8 lines
FYI, I get the following warnings after applying this patch.
/work/git/linux-test.git/arch/x86/kernel/static_call.c: In function ‘__static_call_transform’:
/work/git/linux-test.git/arch/x86/kernel/static_call.c:9:43: warning: passing argument 2 of ‘text_gen_insn’ makes pointer from integer without a cast [-Wint-conversion]9 | const void *code = text_gen_insn(opcode, (long)insn, (long)func); | ^~~~~~~~~~ | | | long int
Hurmph, shows I haven't build the individual patches in a while I suppose. It was fixed in the next patch.
Fixed it up, thanks!
Peter Zijlstra
maintainerRe: [PATCH v6 11/17] static_call: Simple self-test
In reply to Steven RostedtQuoted 15 lines · Steven Rostedt
kernel/static_call.c static void __init static_call_init(void · line 391
Quoted 10 lines · On Fri, 10 Jul 2020 15:38:42 +0200 Peter Zijlstra <[email protected]> wrote:
kernel/static_call.c +10
-0+static int __init test_static_call_init (void ) +{ +WARN_ON (static_call (sc_selftest)(2 ) !=3 ); +static_call_update (sc_selftest, &func_b); +WARN_ON (static_call (sc_selftest)(2 ) !=4 ); +static_call_update (sc_selftest, &func_a); +WARN_ON (static_call (sc_selftest)(2 ) !=3 ); + +return 0 ; +}I wonder if this would be better if we were testing the same static call each time?
Makes sense, I suppose.
Quoted 4 lines
static int
__initrun_static_call(int val){ return static_call(sc_selftest)(val); }
Don't think we need this, or are you afraid of loop unrolling, in which case you also want a noinline here I suppose.
Quoted 12 lines
static struct { int (*func)(int); int val; int expect; } static_call_data [] = { { NULL, 2, 3 } ( func_b, 2 , 4}, { func_a, 2, 3} } __initdata;static int
__inittest_static_call_init(void)
Quoted 11 lines
{
int i;
for (i = 0; i < ARRAY_SIZE(static_call_data); i++ ) {
if (static_call_data[i].func)
static_call_update(sc_selftest, static_call_data[i].func);
WARN_ON(run_static_call(static_call_data[i].val) != static_call_data[i].expect);
}
return 0;
}Lots of compile errors with that, fixed them all :-)
Peter Zijlstra
maintainerRe: [PATCH v6 08/17] static_call: Avoid kprobes on inline static_call()s
In reply to Steven RostedtQuoted 14 lines · Steven Rostedt
kernel/static_call.c static struct notifier_block static_call · line 338
Quoted 11 lines
kernel/static_call.c +11
-0+int static_call_text_reserved (void *start,void *end) +{ +int ret =__static_call_text_reserved (__start_static_call_sites, + __stop_static_call_sites, start, end); + +if (ret) +return ret; + +#ifdef CONFIG_MODULES + ret =__static_call_mod_text_reserved (start, end); +#endif Nit, but why not have a #else /* !CONFIG_MODULE */ above and just:
Quoted 7 lines
static inline int
__static_call_mod_text_reserve(..){ return 0; }#endif
?
Done.
Peter Zijlstra
maintainerRe: [PATCH v6 13/17] static_call: Add static_call_cond()
In reply to Steven RostedtQuoted 9 lines · Steven Rostedt
Quoted 6 lines · On Fri, 10 Jul 2020 15:38:44 +0200 Peter Zijlstra <[email protected]> wrote:
(quoted diff) +3
-1+static void __static_call_transform(void *insn, enum insn_type type, void *func) { - const void *code = text_gen_insn(opcode, insn, func); + int size = CALL_INSN_SIZE; + const void *code;
Quoted 6 lines
Review
Quoted 3 lines
(quoted diff) +0
-3- if (WARN_ONCE(*(u8 *)insn != opcode, - "unexpected static call insn opcode 0x%x at %pS\n", - opcode, insn))I would still feel better if we did some sort of sanity check before just writing to the text. Confirm this is a jmp, call, ret or nop?
Something like so (on top of the next patch) ?
I'm not convinced it actually helps much, but if it makes you feel better :-)
arch/x86/kernel/static_call.c +23 -2
@@ -56,15 +56,36 @@ static inline enum insn_type __sc_insn(breturn 2 *tail + null; } +static void __static_call_validate (void *insn,bool tail) +{ +u8 opcode = *(u8 *)insn; + +if (tail) { +if (opcode ==JMP32_INSN_OPCODE || + opcode ==RET_INSN_OPCODE ) +return ; + }else { +if (opcode ==CALL_INSN_OPCODE || + !memcmp (insn, ideal_nops[NOP_ATOMIC5 ],5 )) +return ; + } + +WARN_ONCE (1 ,"unexpected static_call insn opcode 0x%x at %pS\n" , opcode, insn); +} +void arch_static_call_transform (void *site,void *tramp,void *func,bool tail) {mutex_lock (&text_mutex); -if (tramp) +if (tramp) { +__static_call_validate (tramp, true);__static_call_transform (tramp,__sc_insn (!func, true), func); + } -if (IS_ENABLED (CONFIG_HAVE_STATIC_CALL_INLINE ) && site) +if (IS_ENABLED (CONFIG_HAVE_STATIC_CALL_INLINE ) && site) { +__static_call_validate (site, tail);__static_call_transform (site,__sc_insn (!func, tail), func); + }mutex_unlock (&text_mutex); }
Steven Rostedt
maintainerRe: [PATCH v6 15/17] static_call: Allow early init
In reply to Peter ZijlstraQuoted 13 lines · On Sat, 11 Jul 2020 07:08:31 +0200 Peter Zijlstra <[email protected]> wrote:
Quoted 12 lines · Steven Rostedt
Quoted 8 lines · On Fri, 10 Jul 2020 15:38:46 +0200 Peter Zijlstra <[email protected]> wrote:
In order to use
static_call()to wire upx86_pmu, we need to initialize earlier; copy some of the tricks fromjump_labelto enable this.Primarily we overload
key->nextto store a sites pointer when there are no modules, this avoids having to usekmalloc()to initialize the sites and allows us to run much earlier.
Quoted 4 lines
Quoted 2 lines
I'm confused. What was the need to have
key->nextstore site pointers in order to move it up earlier?The critical part was to not need an allocation.
Why is an allocation needed? What's different about calling it early that we need an allocation or this trick?
The two paragraphs above seem totally disconnected.
"In order to use static_call() to wire up x86_pmu, we need to initialize earlier; copy some of the tricks from jump_label to enable this."
What tricks were copied?
"Primarily we overload key->next to store a sites pointer when there are no modules, this avoids having to use kmalloc() to initialize the sites and allows us to run much earlier."
Why is kmalloc() (or this trick) needed to initialize the sites?
-- Steve
Steven Rostedt
maintainerRe: [PATCH v6 11/17] static_call: Simple self-test
In reply to Peter ZijlstraQuoted 10 lines · On Sat, 11 Jul 2020 12:27:02 +0200 Peter Zijlstra <[email protected]> wrote:
Quoted 10 lines
static int
__inittest_static_call_init(void){ int i; for (i = 0; i < ARRAY_SIZE(static_call_data); i++ ) { if (static_call_data[i].func) static_call_update(sc_selftest, static_call_data[i].func); WARN_ON(run_static_call(static_call_data[i].val) != static_call_data[i].expect); }
Quoted 4 lines
Quoted 2 lines
return 0; }Lots of compile errors with that, fixed them all :-)
Hey, I wrote that all without compiling it. I would have been surprised if it was clean.
-- Steve
Steven Rostedt
maintainerRe: [PATCH v6 13/17] static_call: Add static_call_cond()
In reply to Peter ZijlstraQuoted 5 lines · On Sat, 11 Jul 2020 12:49:30 +0200 Peter Zijlstra <[email protected]> wrote:
Something like so (on top of the next patch) ?
I'm not convinced it actually helps much, but if it makes you feel better :-)
After you have bricked a bunch of people's NICs, you would be paranoid about this too!
You work for Intel, next time you go to an office, see if you can find my picture on any dartboards in there ;-)
Quoted 28 lines
arch/x86/kernel/static_call.c +17
-0@@ -56,15 +56,36 @@ static inline enum insn_type __sc_insn(breturn 2 *tail + null; } +static void __static_call_validate (void *insn,bool tail) +{ +u8 opcode = *(u8 *)insn; + +if (tail) { +if (opcode ==JMP32_INSN_OPCODE || + opcode ==RET_INSN_OPCODE ) +return ; + }else { +if (opcode ==CALL_INSN_OPCODE || + !memcmp (insn, ideal_nops[NOP_ATOMIC5 ],5 )) +return ; + } + +WARN_ONCE (1 ,"unexpected static_call insn opcode 0x%x at %pS\n" , opcode, insn); +} +void arch_static_call_transform (void *site,void *tramp,void *func,bool tail) {mutex_lock (&text_mutex);
arch/x86/kernel/static_call.c static inline enum insn_type __sc_insn(b · line 86
Quoted 10 lines
arch/x86/kernel/static_call.c +5
-2-if (tramp) +if (tramp) { +__static_call_validate (tramp, true);__static_call_transform (tramp,__sc_insn (!func, true), func); + } -if (IS_ENABLED (CONFIG_HAVE_STATIC_CALL_INLINE ) && site) +if (IS_ENABLED (CONFIG_HAVE_STATIC_CALL_INLINE ) && site) { +__static_call_validate (site, tail);
I'd feel even more better if the validate failed, we just don't do the update.
-- Steve
Quoted 5 lines
arch/x86/kernel/static_call.c +1
-0__static_call_transform (site,__sc_insn (!func, tail), func); + }mutex_unlock (&text_mutex); }
Peter Zijlstra
maintainerRe: [PATCH v6 15/17] static_call: Allow early init
In reply to Steven RostedtQuoted 26 lines · Steven Rostedt
Quoted 13 lines · On Sat, 11 Jul 2020 07:08:31 +0200 Peter Zijlstra <[email protected]> wrote:
Quoted 12 lines · Steven Rostedt
Quoted 8 lines · On Fri, 10 Jul 2020 15:38:46 +0200 Peter Zijlstra <[email protected]> wrote:
In order to use
static_call()to wire upx86_pmu, we need to initialize earlier; copy some of the tricks fromjump_labelto enable this.Primarily we overload
key->nextto store a sites pointer when there are no modules, this avoids having to usekmalloc()to initialize the sites and allows us to run much earlier.Quoted 4 lines
Quoted 2 lines
I'm confused. What was the need to have
key->nextstore site pointers in order to move it up earlier?The critical part was to not need an allocation.
Why is an allocation needed? What's different about calling it early that we need an allocation or this trick?
The two paragraphs above seem totally disconnected.
Quoted 7 lines
"In order to use
static_call()to wire upx86_pmu, we need to initialize earlier; copy some of the tricks fromjump_labelto enable this."What tricks were copied?
"Primarily we overload
key->nextto store a sites pointer when there
^^ this trick...
+ union {
+ /* bit 0: 0 = mods, 1 = sites */
+ unsigned long type;
+ struct static_call_mod *mods;
+ struct static_call_site *sites;
+ };
If that isn't a trick, I don't know ;-)
Quoted 4 lines
are no modules, this avoids having to use
kmalloc()to initialize the sites and allows us to run much earlier."Why is
kmalloc()(or this trick) needed to initialize the sites?
That's just how the code was; it treated vmlinux as the NULL module, and as such needed a static_call_mod allocated to host the static_call_sites pointer.
That is, the static_call_key has a single linked list pointer to static_call_mod, and every module has an entry on that list with a pointer to their sites. Very simple and straight forward.
Except it requires an allocation to set up, which is a pain is you want it initialized very early.
Peter Zijlstra
maintainerRe: [PATCH v6 13/17] static_call: Add static_call_cond()
In reply to Steven RostedtQuoted 5 lines · Steven Rostedt
Quoted 3 lines · On Sat, 11 Jul 2020 12:49:30 +0200 Peter Zijlstra <[email protected]> wrote:
Something like so (on top of the next patch) ?
Quoted 8 lines
Quoted 2 lines
I'm not convinced it actually helps much, but if it makes you feel better :-)
After you have bricked a bunch of people's NICs, you would be paranoid about this too!
You work for Intel, next time you go to an office, see if you can find my picture on any dartboards in there ;-)
I'll tell em it was bad hardware for being able to get bricked like that instead, how's that? ;-)
Quoted 33 lines
Quoted 33 lines
arch/x86/kernel/static_call.c +20
-1@@ -56,15 +56,36 @@ static inline enum insn_type __sc_insn(breturn 2 *tail + null; } +static void __static_call_validate (void *insn,bool tail) +{ +u8 opcode = *(u8 *)insn; + +if (tail) { +if (opcode ==JMP32_INSN_OPCODE || + opcode ==RET_INSN_OPCODE ) +return ; + }else { +if (opcode ==CALL_INSN_OPCODE || + !memcmp (insn, ideal_nops[NOP_ATOMIC5 ],5 )) +return ; + } + +WARN_ONCE (1 ,"unexpected static_call insn opcode 0x%x at %pS\n" , opcode, insn); +} +void arch_static_call_transform (void *site,void *tramp,void *func,bool tail) {mutex_lock (&text_mutex); -if (tramp) +if (tramp) { +__static_call_validate (tramp, true);__static_call_transform (tramp,__sc_insn (!func, true), func); + }
Quoted 6 lines
arch/x86/kernel/static_call.c static inline enum insn_type __sc_insn(b · line 86
Quoted 3 lines
arch/x86/kernel/static_call.c +2
-1-if (IS_ENABLED (CONFIG_HAVE_STATIC_CALL_INLINE ) && site) +if (IS_ENABLED (CONFIG_HAVE_STATIC_CALL_INLINE ) && site) { +__static_call_validate (site, tail);I'd feel even more better if the validate failed, we just don't do the update.
It is either this or BUG()/panic(), you pick ;-)
Steven Rostedt
maintainerRe: [PATCH v6 15/17] static_call: Allow early init
In reply to Peter ZijlstraQuoted 44 lines · On Tue, 14 Jul 2020 11:51:17 +0200 Peter Zijlstra <[email protected]> wrote:
Quoted 26 lines · Steven Rostedt
Quoted 13 lines · On Sat, 11 Jul 2020 07:08:31 +0200 Peter Zijlstra <[email protected]> wrote:
Quoted 12 lines · Steven Rostedt
Quoted 8 lines · On Fri, 10 Jul 2020 15:38:46 +0200 Peter Zijlstra <[email protected]> wrote:
In order to use
static_call()to wire upx86_pmu, we need to initialize earlier; copy some of the tricks fromjump_labelto enable this.Primarily we overload
key->nextto store a sites pointer when there are no modules, this avoids having to usekmalloc()to initialize the sites and allows us to run much earlier.Quoted 4 lines
Quoted 2 lines
I'm confused. What was the need to have
key->nextstore site pointers in order to move it up earlier?The critical part was to not need an allocation.
Why is an allocation needed? What's different about calling it early that we need an allocation or this trick?
The two paragraphs above seem totally disconnected.
Quoted 7 lines
"In order to use
static_call()to wire upx86_pmu, we need to initialize earlier; copy some of the tricks fromjump_labelto enable this."What tricks were copied?
"Primarily we overload
key->nextto store a sites pointer when there^^ this trick...
include/linux/static_call.h +6
-0+union { +/* bit 0: 0 = mods, 1 = sites */ +unsigned long type; +struct static_call_mod *mods; +struct static_call_site *sites; + };
Quoted 1 line
If that isn't a trick, I don't know ;-)
Ah, that makes more sense. The way it was worded didn't quite put 2 and 2 together like that.
Quoted 10 lines
Quoted 5 lines
are no modules, this avoids having to use
kmalloc()to initialize the sites and allows us to run much earlier."Why is
kmalloc()(or this trick) needed to initialize the sites?That's just how the code was; it treated vmlinux as the NULL module, and as such needed a
static_call_modallocated to host thestatic_call_sitespointer.
Quoted 6 lines
That is, the
static_call_keyhas a single linked list pointer tostatic_call_mod, and every module has an entry on that list with a pointer to their sites. Very simple and straight forward.Except it requires an allocation to set up, which is a pain is you want it initialized very early.
OK, I'm still missing something, but having a hard time explaining exactly what that is. ;-)
I guess that is, why did moving the initialization early require an allocation where initializing it later did not? What allocation are we avoiding?
I'm not seeing why this trick is needed when we moved the init early as compared to doing the same thing later on.
Is it this?
kernel/static_call.c static int __static_call_init(struct mod · line 230
Quoted 10 lines
kernel/static_call.c +6
-0@@ -192,16 +222,35 @@ static int __static_call_init(struct modif (key != prev_key) { prev_key = key; +if (!mod) { + key->sites = site; + key->type |=1 ; + goto do_transform; + } +
We want to avoid calling kzalloc() early?
If so, this should have a comment here stating so and why.
kernel/static_call.c static int __static_call_init(struct mod · line 233
Quoted 3 lines
kernel/static_call.c +0
-0site_mod =kzalloc (sizeof (*site_mod),GFP_KERNEL );if (!site_mod)return -ENOMEM ;
-- Steve
Peter Zijlstra
maintainerRe: [PATCH v6 15/17] static_call: Allow early init
Quoted 11 lines · Steven Rostedt
Quoted 10 lines
That's just how the code was; it treated vmlinux as the NULL module, and as such needed a
static_call_modallocated to host thestatic_call_sitespointer.That is, the
static_call_keyhas a single linked list pointer tostatic_call_mod, and every module has an entry on that list with a pointer to their sites. Very simple and straight forward.Except it requires an allocation to set up, which is a pain is you want it initialized very early.
Quoted 6 lines
OK, I'm still missing something, but having a hard time explaining exactly what that is. ;-)
I guess that is, why did moving the initialization early require an allocation where initializing it later did not? What allocation are we avoiding?
The other way around. Before this patch initialization required an allocation, with this patch init no longer requires one.
Quoted 2 lines
I'm not seeing why this trick is needed when we moved the init early as compared to doing the same thing later on.
We use the trick to avoid the alloc, which is what enables early init.
So, before:
- init required alloc
- alloc prohibits early use
after:
- init no longer require alloc
- because horrible pointer reuse
- early use possible
Quoted 13 lines
Review static int __static_call_init(struct mod · line 230
Quoted 10 lines
(quoted diff) +6
-0@@ -192,16 +222,35 @@ static int __static_call_init(struct mod if (key != prev_key) { prev_key = key; + if (!mod) { + key->sites = site; + key->type |= 1; + goto do_transform; + } +We want to avoid calling
kzalloc()early?
Quoted 1 line
If so, this should have a comment here stating so and why.
Fair enough; and I think I even see a further optimiation.
How's this:
---
Subject: static_call: Allow early init
From: Peter Zijlstra <[email protected]>
Date: Fri, 4 Oct 17:21:10 CEST 2019
In order to use static_call() to wire up x86_pmu, we need to initialize earlier; copy some of the tricks from jump_label to enable this.
Primarily we overload key->next to store a sites pointer when there are no modules, this avoids having to use kmalloc() to initialize the sites and allows us to run much earlier.
(arguably, this is much much earlier than needed for perf, but it might allow other uses.)
--- arch/x86/kernel/setup.c | 2 + arch/x86/kernel/static_call.c | 8 ++++- include/linux/static_call.h | 15 ++++++++- kernel/static_call.c | 67 +++++++++++++++++++++++++++++++++++++++--- 4 files changed, 85 insertions(+), 7 deletions(-)
arch/x86/kernel/setup.c +2 -0
@@ -19,6 +19,7 @@#include <linux/hugetlb.h> #include <linux/tboot.h> #include <linux/usb/xhci-dbgp.h> +#include <linux/static_call.h> #include <uapi/linux/mount.h>
@@ -848,6 +849,7 @@ void __init setup_arch(char **cmdline_p)early_cpu_init ();arch_init_ideal_nops ();jump_label_init (); +static_call_init ();early_ioremap_init ();setup_olpc_ofw_pgd ();
arch/x86/kernel/static_call.c +7 -1
@@ -11,7 +11,7 @@ enum insn_type { RET = 3, /* tramp / site cond-tail-call */ }; -static void __static_call_transform (void *insn,enum insn_type type,void *func) +static void __ref __static_call_transform (void *insn,enum insn_type type,void *func) {int size =CALL_INSN_SIZE ;const void *code;
@@ -33,11 +33,17 @@ static void __static_call_transform(void code =text_gen_insn (RET_INSN_OPCODE , insn, func); size =RET_INSN_SIZE ;break ; + +default :/* GCC is a moron -- it figures @code can be uninitialized below */ +BUG (); }if (memcmp (insn, code, size) ==0 )return ; +if (unlikely (system_state ==SYSTEM_BOOTING )) +return text_poke_early (insn, code, size); +text_poke_bp (insn, code, size,NULL ); }
include/linux/static_call.h +13 -2
@@ -136,6 +136,8 @@ extern void arch_static_call_transform(v #ifdef CONFIG_HAVE_STATIC_CALL_INLINE +extern void __init static_call_init(void); + struct static_call_mod { struct static_call_mod *next; struct module *mod; /* for vmlinux, mod == NULL */
@@ -144,7 +146,12 @@ struct static_call_mod {struct static_call_key {void *func; -struct static_call_mod *mods; +union { +/* bit 0: 0 = mods, 1 = sites */ +unsigned long type; +struct static_call_mod *mods; +struct static_call_site *sites; + }; };extern void __static_call_update (struct static_call_key *key,void *tramp,void *func);
@@ -155,7 +162,7 @@ extern int static_call_text_reserved(voiDECLARE_STATIC_CALL (name, _func); \struct static_call_key STATIC_CALL_KEY (name ) = { \ .func = _func, \ - .mods =NULL , \ + .type =1 , \ }; \ARCH_DEFINE_STATIC_CALL_TRAMP (name, _func)
@@ -180,6 +187,8 @@ extern int static_call_text_reserved(voi#elif defined(CONFIG_HAVE_STATIC_CALL ) +static inline void static_call_init (void ) { } +struct static_call_key {void *func; };
@@ -225,6 +234,8 @@ static inline int static_call_text_reser#else /* Generic implementation */ +static inline void static_call_init (void ) { } +struct static_call_key {void *func; };
kernel/static_call.c +63 -4
109 lines not on this page · raw message
Steven Rostedt
maintainerRe: [PATCH v6 15/17] static_call: Allow early init
Quoted 7 lines · On Tue, 14 Jul 2020 17:54:17 +0200 Peter Zijlstra <[email protected]> wrote:
Quoted 3 lines
I guess that is, why did moving the initialization early require an allocation where initializing it later did not? What allocation are we avoiding?
The other way around. Before this patch initialization required an allocation, with this patch init no longer requires one.
Yeah, that's what I figured. Just didn't express myself that way.
Quoted 38 lines
Quoted 2 lines
I'm not seeing why this trick is needed when we moved the init early as compared to doing the same thing later on.
We use the trick to avoid the alloc, which is what enables early init.
So, before:
- init required alloc
- alloc prohibits early useafter:
- init no longer require alloc
- because horrible pointer reuse
- early use possibleQuoted 13 lines
kernel/static_call.c static int __static_call_init(struct mod · line 230
Quoted 10 lines
kernel/static_call.c +6
-0@@ -192,16 +222,35 @@ static int __static_call_init(struct modif (key != prev_key) { prev_key = key; +if (!mod) { + key->sites = site; + key->type |=1 ; + goto do_transform; + } +We want to avoid calling
kzalloc()early?Quoted 1 line
If so, this should have a comment here stating so and why.
Fair enough; and I think I even see a further optimiation.
How's this:
---
Subject:static_call: Allow early init
From: Peter Zijlstra <[email protected]>
Date: Fri, 4 Oct 17:21:10 CEST 2019
Quoted 7 lines
In order to use
static_call()to wire upx86_pmu, we need to initialize earlier; copy some of the tricks fromjump_labelto enable this.Primarily we overload
key->nextto store a sites pointer when there are no modules, this avoids having to usekmalloc()to initialize the sites and allows us to run much earlier.
Can we add a statement that says something like: "Because x86 now calls static_call_init() before the setup of the memory allocator, we must avoid using kmalloc() and friends for core kernel static calls." ?
This was the missing piece for me.
Quoted 171 lines
(arguably, this is much much earlier than needed for perf, but it might allow other uses.)
Signed-off-by Peter Zijlstra (Intel) <[email protected]>--- arch/x86/kernel/setup.c | 2 + arch/x86/kernel/static_call.c | 8 ++++- include/linux/static_call.h | 15 ++++++++- kernel/static_call.c | 67 +++++++++++++++++++++++++++++++++++++++--- 4 files changed, 85 insertions(+), 7 deletions(-)arch/x86/kernel/setup.c +2
-0@@ -19,6 +19,7 @@#include <linux/hugetlb.h> #include <linux/tboot.h> #include <linux/usb/xhci-dbgp.h> +#include <linux/static_call.h> #include <uapi/linux/mount.h> @@ -848,6 +849,7 @@ void __init setup_arch(char **cmdline_p)early_cpu_init ();arch_init_ideal_nops ();jump_label_init (); +static_call_init ();early_ioremap_init ();setup_olpc_ofw_pgd ();arch/x86/kernel/static_call.c +7
-1@@ -11,7 +11,7 @@ enum insn_type { RET = 3, /* tramp / site cond-tail-call */ }; -static void __static_call_transform (void *insn,enum insn_type type,void *func) +static void __ref __static_call_transform (void *insn,enum insn_type type,void *func) {int size =CALL_INSN_SIZE ;const void *code;@@ -33,11 +33,17 @@ static void __static_call_transform(void code =text_gen_insn (RET_INSN_OPCODE , insn, func); size =RET_INSN_SIZE ;break ; + +default :/* GCC is a moron -- it figures @code can be uninitialized below */ +BUG (); }if (memcmp (insn, code, size) ==0 )return ; +if (unlikely (system_state ==SYSTEM_BOOTING )) +return text_poke_early (insn, code, size); +text_poke_bp (insn, code, size,NULL ); }include/linux/static_call.h +13
-2@@ -136,6 +136,8 @@ extern void arch_static_call_transform(v #ifdef CONFIG_HAVE_STATIC_CALL_INLINE +extern void __init static_call_init(void); + struct static_call_mod { struct static_call_mod *next; struct module *mod; /* for vmlinux, mod == NULL */@@ -144,7 +146,12 @@ struct static_call_mod {struct static_call_key {void *func; -struct static_call_mod *mods; +union { +/* bit 0: 0 = mods, 1 = sites */ +unsigned long type; +struct static_call_mod *mods; +struct static_call_site *sites; + }; };extern void __static_call_update (struct static_call_key *key,void *tramp,void *func);@@ -155,7 +162,7 @@ extern int static_call_text_reserved(voiDECLARE_STATIC_CALL (name, _func); \struct static_call_key STATIC_CALL_KEY (name ) = { \ .func = _func, \ - .mods =NULL , \ + .type =1 , \ }; \ARCH_DEFINE_STATIC_CALL_TRAMP (name, _func)@@ -180,6 +187,8 @@ extern int static_call_text_reserved(voi#elif defined(CONFIG_HAVE_STATIC_CALL ) +static inline void static_call_init (void ) { } +struct static_call_key {void *func; };@@ -225,6 +234,8 @@ static inline int static_call_text_reser#else /* Generic implementation */ +static inline void static_call_init (void ) { } +struct static_call_key {void *func; };kernel/static_call.c +32
-2@@ -94,10 +94,31 @@ static inline void static_call_sort_entr static_call_site_cmp, static_call_site_swap); } +static inline bool static_call_key_has_mods (struct static_call_key *key) +{ +return !(key->type &1 ); +} + +static inline struct static_call_mod *static_call_key_next (struct static_call_key *key) +{ +if (!static_call_key_has_mods (key)) +return NULL ; + +return key->mods; +} + +static inline struct static_call_site *static_call_key_sites (struct static_call_key *key) +{ +if (static_call_key_has_mods (key)) +return NULL ; + +return (struct static_call_site *)(key->type & ~1 ); +} +void __static_call_update (struct static_call_key *key,void *tramp,void *func) {struct static_call_site *site, *stop; -struct static_call_mod *site_mod; +struct static_call_mod *site_mod, first;cpus_read_lock ();static_call_lock ();@@ -116,13 +137,22 @@ void __static_call_update(struct static_if (WARN_ON_ONCE (!static_call_initialized)) goto done; -for (site_mod = key->mods; site_mod; site_mod = site_mod->next) { + first = (struct static_call_mod ){ + .next =static_call_key_next (key), + .mod =NULL , + .sites =static_call_key_sites (key), + }; + +for (site_mod = &first; site_mod; site_mod = site_mod->next) {struct module *mod = site_mod->mod;if (!site_mod->sites) {/* * This can happen if the static call key is defined in * a module which doesn't use it. +* +* It also happens in the has_mods case, where the +* 'first' entry has no sites associated with it. */ continue ; }
kernel/static_call.c static int __static_call_init(struct mod · line 225
Quoted 4 lines
kernel/static_call.c +1
-0@@ -192,16 +222,45 @@ static int __static_call_init(struct modif (key != prev_key) { prev_key = key; +/*
Can we add to this comment:
* Some architectures (x86) call this before the memory
* allocator is set up.
kernel/static_call.c static int __static_call_init(struct mod · line 228
Quoted 3 lines
kernel/static_call.c +3
-0+* For vmlinux (!mod) avoid the allocation by storing +* the sites pointer in the key itself. Also see +* __static_call_update()'s @first.
-- Steve
Quoted 48 lines
kernel/static_call.c +27
-2+*/ +if (!mod) { + key->sites = site; + key->type |=1 ; + goto do_transform; + } + site_mod =kzalloc (sizeof (*site_mod),GFP_KERNEL );if (!site_mod)return -ENOMEM ; +/* +* When the key has a direct sites pointer, extract +* that into an explicit struct static_call_mod, so we +* can have a list of modules. +*/ +if (static_call_key_sites (key)) { + site_mod->mod =NULL ; + site_mod->next =NULL ; + site_mod->sites =static_call_key_sites (key); + + key->mods = site_mod; + + site_mod =kzalloc (sizeof (*site_mod),GFP_KERNEL ); +if (!site_mod) +return -ENOMEM ; + } + site_mod->mod = mod; site_mod->sites = site; - site_mod->next = key->mods; + site_mod->next =static_call_key_next (key); key->mods = site_mod; } +do_transform:arch_static_call_transform (site_addr,NULL , key->func,static_call_is_tail (site)); }@@ -348,7 +407,7 @@ int static_call_text_reserved(void *starreturn __static_call_mod_text_reserved (start, end); } -static void __init static_call_init (void ) +void __init static_call_init (void ) {int ret;
Peter Zijlstra
maintainerRe: [PATCH v6 15/17] static_call: Allow early init
Quoted 5 lines · Steven Rostedt
Can we add a statement that says something like: "Because x86 now calls
static_call_init()before the setup of the memory allocator, we must avoid usingkmalloc()and friends for core kernel static calls." ?This was the missing piece for me.
It now reads like this.
---
Subject: static_call: Allow early init
From: Peter Zijlstra <[email protected]>
Date: Fri, 4 Oct 17:21:10 CEST 2019
In order to use static_call() to wire up x86_pmu, we need to initialize earlier, specifically before memory allocation works; copy some of the tricks from jump_label to enable this.
Primarily we overload key->next to store a sites pointer when there are no modules, this avoids having to use kmalloc() to initialize the sites and allows us to run much earlier.
--- arch/x86/kernel/setup.c | 2 + arch/x86/kernel/static_call.c | 8 ++++ include/linux/static_call.h | 15 +++++++-- kernel/static_call.c | 70 +++++++++++++++++++++++++++++++++++++++--- 4 files changed, 88 insertions(+), 7 deletions(-)
arch/x86/kernel/setup.c +2 -0
@@ -19,6 +19,7 @@#include <linux/hugetlb.h> #include <linux/tboot.h> #include <linux/usb/xhci-dbgp.h> +#include <linux/static_call.h> #include <uapi/linux/mount.h>
@@ -848,6 +849,7 @@ void __init setup_arch(char **cmdline_p)early_cpu_init ();arch_init_ideal_nops ();jump_label_init (); +static_call_init ();early_ioremap_init ();setup_olpc_ofw_pgd ();
arch/x86/kernel/static_call.c +7 -1
@@ -11,7 +11,7 @@ enum insn_type { RET = 3, /* tramp / site cond-tail-call */ }; -static void __static_call_transform (void *insn,enum insn_type type,void *func) +static void __ref __static_call_transform (void *insn,enum insn_type type,void *func) {int size =CALL_INSN_SIZE ;const void *code;
@@ -33,11 +33,17 @@ static void __static_call_transform(void code =text_gen_insn (RET_INSN_OPCODE , insn, func); size =RET_INSN_SIZE ;break ; + +default :/* GCC is a moron -- it figures @code can be uninitialized below */ +BUG (); }if (memcmp (insn, code, size) ==0 )return ; +if (unlikely (system_state ==SYSTEM_BOOTING )) +return text_poke_early (insn, code, size); +text_poke_bp (insn, code, size,NULL ); }
include/linux/static_call.h +13 -2
@@ -136,6 +136,8 @@ extern void arch_static_call_transform(v #ifdef CONFIG_HAVE_STATIC_CALL_INLINE +extern void __init static_call_init(void); + struct static_call_mod { struct static_call_mod *next; struct module *mod; /* for vmlinux, mod == NULL */
@@ -144,7 +146,12 @@ struct static_call_mod {struct static_call_key {void *func; -struct static_call_mod *mods; +union { +/* bit 0: 0 = mods, 1 = sites */ +unsigned long type; +struct static_call_mod *mods; +struct static_call_site *sites; + }; };extern void __static_call_update (struct static_call_key *key,void *tramp,void *func);
@@ -155,7 +162,7 @@ extern int static_call_text_reserved(voiDECLARE_STATIC_CALL (name, _func); \struct static_call_key STATIC_CALL_KEY (name ) = { \ .func = _func, \ - .mods =NULL , \ + .type =1 , \ }; \ARCH_DEFINE_STATIC_CALL_TRAMP (name, _func)
@@ -180,6 +187,8 @@ extern int static_call_text_reserved(voi#elif defined(CONFIG_HAVE_STATIC_CALL ) +static inline void static_call_init (void ) { } +struct static_call_key {void *func; };
@@ -225,6 +234,8 @@ static inline int static_call_text_reser#else /* Generic implementation */ +static inline void static_call_init (void ) { } +struct static_call_key {void *func; };
kernel/static_call.c +66 -4
112 lines not on this page · raw message
Re: [PATCH v6 15/17] static_call: Allow early init
Quoted 14 lines · On Tue, 14 Jul 2020 20:31:43 +0200 Peter Zijlstra <[email protected]> wrote:
Quoted 5 lines · Steven Rostedt
Can we add a statement that says something like: "Because x86 now calls
static_call_init()before the setup of the memory allocator, we must avoid usingkmalloc()and friends for core kernel static calls." ?This was the missing piece for me.
It now reads like this.
---
Subject:static_call: Allow early init
From: Peter Zijlstra <[email protected]>
Date: Fri, 4 Oct 17:21:10 CEST 2019
Quoted 7 lines
In order to use
static_call()to wire upx86_pmu, we need to initialize earlier, specifically before memory allocation works; copy some of the tricks fromjump_labelto enable this.Primarily we overload
key->nextto store a sites pointer when there are no modules, this avoids having to usekmalloc()to initialize the sites and allows us to run much earlier.
All the pieces lie in place. Thanks!
-- Steve
Quoted 3 lines
Signed-off-by Peter Zijlstra (Intel) <[email protected]>---