Skip to content

Commit e6d6c07

Browse files
jpoimboeingomolnar
authored andcommitted
x86/static_call: Add out-of-line static call implementation
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. [peterz: fixed trampoline, rewrote patching code] Signed-off-by: Josh Poimboeuf <jpoimboe@redhat.com> Signed-off-by: Peter Zijlstra (Intel) <peterz@infradead.org> Signed-off-by: Ingo Molnar <mingo@kernel.org> Cc: Linus Torvalds <torvalds@linux-foundation.org> Link: https://lore.kernel.org/r/20200818135804.804315175@infradead.org
1 parent 6333e8f commit e6d6c07

4 files changed

Lines changed: 56 additions & 0 deletions

File tree

arch/x86/Kconfig

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,7 @@ config X86
215215
select HAVE_FUNCTION_ARG_ACCESS_API
216216
select HAVE_STACKPROTECTOR if CC_HAS_SANE_STACKPROTECTOR
217217
select HAVE_STACK_VALIDATION if X86_64
218+
select HAVE_STATIC_CALL
218219
select HAVE_RSEQ
219220
select HAVE_SYSCALL_TRACEPOINTS
220221
select HAVE_UNSTABLE_SCHED_CLOCK

arch/x86/include/asm/static_call.h

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/* SPDX-License-Identifier: GPL-2.0 */
2+
#ifndef _ASM_STATIC_CALL_H
3+
#define _ASM_STATIC_CALL_H
4+
5+
#include <asm/text-patching.h>
6+
7+
/*
8+
* For CONFIG_HAVE_STATIC_CALL, this is a permanent trampoline which
9+
* does a direct jump to the function. The direct jump gets patched by
10+
* static_call_update().
11+
*/
12+
#define ARCH_DEFINE_STATIC_CALL_TRAMP(name, func) \
13+
asm(".pushsection .text, \"ax\" \n" \
14+
".align 4 \n" \
15+
".globl " STATIC_CALL_TRAMP_STR(name) " \n" \
16+
STATIC_CALL_TRAMP_STR(name) ": \n" \
17+
" .byte 0xe9 # jmp.d32 \n" \
18+
" .long " #func " - (. + 4) \n" \
19+
".type " STATIC_CALL_TRAMP_STR(name) ", @function \n" \
20+
".size " STATIC_CALL_TRAMP_STR(name) ", . - " STATIC_CALL_TRAMP_STR(name) " \n" \
21+
".popsection \n")
22+
23+
#endif /* _ASM_STATIC_CALL_H */

arch/x86/kernel/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ obj-y += tsc.o tsc_msr.o io_delay.o rtc.o
6868
obj-y += pci-iommu_table.o
6969
obj-y += resource.o
7070
obj-y += irqflags.o
71+
obj-y += static_call.o
7172

7273
obj-y += process.o
7374
obj-y += fpu/

arch/x86/kernel/static_call.c

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// SPDX-License-Identifier: GPL-2.0
2+
#include <linux/static_call.h>
3+
#include <linux/memory.h>
4+
#include <linux/bug.h>
5+
#include <asm/text-patching.h>
6+
7+
static void __static_call_transform(void *insn, u8 opcode, void *func)
8+
{
9+
const void *code = text_gen_insn(opcode, insn, func);
10+
11+
if (WARN_ONCE(*(u8 *)insn != opcode,
12+
"unexpected static call insn opcode 0x%x at %pS\n",
13+
opcode, insn))
14+
return;
15+
16+
if (memcmp(insn, code, CALL_INSN_SIZE) == 0)
17+
return;
18+
19+
text_poke_bp(insn, code, CALL_INSN_SIZE, NULL);
20+
}
21+
22+
void arch_static_call_transform(void *site, void *tramp, void *func)
23+
{
24+
mutex_lock(&text_mutex);
25+
26+
if (tramp)
27+
__static_call_transform(tramp, JMP32_INSN_OPCODE, func);
28+
29+
mutex_unlock(&text_mutex);
30+
}
31+
EXPORT_SYMBOL_GPL(arch_static_call_transform);

0 commit comments

Comments
 (0)