OPTPROBES for powerpc
v4 cover OPTPROBES for powerpc
This is the patchset of the kprobes jump optimization (a.k.a OPTPROBES)for powerpc. Kprobe being an inevitable tool for kernel developers, enhancing the performance of kprobe has got much importance.
Currently kprobes inserts a trap instruction to probe a running kernel. Jump optimization allows kprobes to replace the trap with a branch, reducing the probe overhead drastically.
In this series, conditional branch instructions are not considered for optimization as there isn't a foolproof mechanism to ensure the conditional branch targets lie in the range of addresses (2^^24) that is a necessary condition for optimization.
The kprobe placed on the kretprobe_trampoline during boot time, is also optimized in this series. Patch 2/2 furnishes this.
Note: This patch set depends on the patch series which I send earlier.
Links are here : https://patchwork.ozlabs.org/patch/725563/ https://patchwork.ozlabs.org/patch/725562/ https://patchwork.ozlabs.org/patch/725564/
The helper functions in these patches are invoked in patch 1/2.
Performance:
============
An optimized kprobe in powerpc is up to 4 times faster than a trap based kprobe.
Example:
Placed a probe at an instruction in _do_fork().
*Time Diff here is, difference in time before hitting the probe and after the probed instruction. mftb() is employed in kernel/fork.c for this purpose.
# echo 0 > /proc/sys/debug/kprobes-optimization Kprobes globally unoptimized
[ 172.252347] Time diff = 0xc4c [ 172.257389] Time diff = 0x86e [ 172.262035] Time diff = 0xb8f [ 172.266479] Time diff = 0x5ec [ 172.270641] Time diff = 0xd4f [ 172.273224] Time diff = 0x52b [ 172.277328] Time diff = 0x793 [ 172.280520] Time diff = 0x286 [ 172.284125] Time diff = 0x592 [ 172.287319] Time diff = 0x593 [ 172.292319] Time diff = 0xa30 [ 172.294909] Time diff = 0x2e1 [ 172.297806] Time diff = 0x6a3 [ 172.300718] Time diff = 0x5aa [ 172.304675] Time diff = 0xa6e [ 172.307668] Time diff = 0x322 [ 172.310875] Time diff = 0x844 [ 172.313710] Time diff = 0x2db [ 172.317361] Time diff = 0x831 [ 172.320066] Time diff = 0x327
# echo 1 > /proc/sys/debug/kprobes-optimization Kprobes globally optimized
[ 207.070301] Time diff = 0x1dd [ 207.073401] Time diff = 0x118 [ 207.075724] Time diff = 0x100 [ 207.078643] Time diff = 0x242 [ 207.080938] Time diff = 0x129 [ 207.084103] Time diff = 0x32f [ 207.087022] Time diff = 0x194 [ 207.090139] Time diff = 0x13d [ 207.092436] Time diff = 0x195 [ 207.095031] Time diff = 0x103 [ 207.097481] Time diff = 0x15a [ 207.100414] Time diff = 0x11f [ 207.102831] Time diff = 0x161 [ 207.105713] Time diff = 0x242 [ 207.108271] Time diff = 0x2d7 [ 207.111741] Time diff = 0x104 [ 207.114389] Time diff = 0xf1 [ 207.118002] Time diff = 0x2f1 [ 207.120930] Time diff = 0x179 [ 207.124259] Time diff = 0x10f
Implementation:
===================
The trap instruction is replaced by a branch to a detour buffer. To address the limitation of branch instruction in POWER architecture, detour buffer slot is allocated from a reserved area. This will ensure that the branch is within +/- 32 MB range. The existing generic approach for kprobes instruction cache uses module_alloc() to allocate memory area for instruction slots. This will always be beyond +/- 32MB range.
The detour buffer contains a call to optimized_callback() which in turn call the pre_handler(). Once the pre-handler is run, the original instruction is emulated from the detour buffer itself. Also the detour buffer is equipped with a branch back to the normal work flow after the probed instruction is emulated. This branch instruction is set up during the preparation of the detour buffer itself. The address of the instruction to which we need to jump back is determined through analyse_instr(), which is invoked during the sanity checks for optprobes. A dummy pt_regs along with probed instruction is used for this purpose.
Kprobe placed in conditional branch instructions are not optimized, as we can't predict the nip prior with dummy pt_regs and can not ensure that the return branch from detour buffer falls in the range of address (i.e 32MB).
Before preparing optimization, Kprobes inserts original(breakpoint instruction) kprobe on the specified address. So, even if the kprobe is not possible to be optimized, it just uses a normal kprobe.
Limitations:
==============
- Number of probes which can be optimized is limited by the size of the area reserved.
- Currently instructions which can be emulated using analyse_instr() are the only candidates for optimization.
- Conditional branch instructions are not optimized.
- Probes on kernel module region are not considered for optimization now.
Changes from v3:
- The optprobe specific patches are moved to a separate series.
- Comments by Michael Ellerman are addressed.
- Performance results in the cover letter are updated with the latest patch set.
Changes from v2:
- Comments by Masami are addressed.
- Description in the cover letter is modified a bit.
Changes from v1:
- Merged the three patches in V1 into a single patch.
- Comments by Masami are addressed.
- Some helper functions are implemented in separate patches.
- Optimization for kprobe placed on the kretprobe_trampoline during boot time is implemented.
Kindly let me know your suggestions and comments.
Thanks,
-Anju
Anju T Sudhakar (2):
arch/powerpc: Implement Optprobes arch/powerpc: Optimize kprobe in kretprobe_trampoline
arch/powerpc/Kconfig | 1 + arch/powerpc/include/asm/code-patching.h | 1 + arch/powerpc/include/asm/kprobes.h | 24 ++- arch/powerpc/kernel/Makefile | 1 + arch/powerpc/kernel/kprobes.c | 8 + arch/powerpc/kernel/optprobes.c | 347 +++++++++++++++++++++++++++++++ arch/powerpc/kernel/optprobes_head.S | 135 ++++++++++++ arch/powerpc/lib/code-patching.c | 21 ++ 8 files changed, 537 insertions(+), 1 deletion(-) create mode 100644 arch/powerpc/kernel/optprobes.c create mode 100644 arch/powerpc/kernel/optprobes_head.S
Signature · 1 line
2.7.4
PATCH v4 1/2 arch/powerpc: Implement Optprobes
Current infrastructure of kprobe uses the unconditional trap instruction to probe a running kernel. Optprobe allows kprobe to replace the trap with a branch instruction to a detour buffer. Detour buffer contains instructions to create an in memory pt_regs. Detour buffer also has a call to optimized_callback() which in turn call the pre_handler().
After the execution of the pre-handler, a call is made for instruction emulation. The NIP is determined in advanced through dummy instruction emulation and a branch instruction is created to the NIP at the end of the trampoline.
To address the limitation of branch instruction in POWER architecture, detour buffer slot is allocated from a reserved area. For the time being, 64KB is reserved in memory for this purpose.
Instructions which can be emulated using analyse_instr() are the candidates for optimization. Before optimization ensure that the address range between the detour buffer allocated and the instruction being probed is within +/- 32MB.
--- arch/powerpc/Kconfig | 1 + arch/powerpc/include/asm/code-patching.h | 1 + arch/powerpc/include/asm/kprobes.h | 24 ++- arch/powerpc/kernel/Makefile | 1 + arch/powerpc/kernel/optprobes.c | 348 +++++++++++++++++++++++++++++++ arch/powerpc/kernel/optprobes_head.S | 135 ++++++++++++ arch/powerpc/lib/code-patching.c | 21 ++ 7 files changed, 530 insertions(+), 1 deletion(-) create mode 100644 arch/powerpc/kernel/optprobes.c create mode 100644 arch/powerpc/kernel/optprobes_head.S
arch/powerpc/Kconfig +1 -0
@@ -99,6 +99,7 @@ config PPC select HAVE_IOREMAP_PROT select HAVE_EFFICIENT_UNALIGNED_ACCESS if !(CPU_LITTLE_ENDIAN && POWER7_CPU) select HAVE_KPROBES + select HAVE_OPTPROBES if PPC64 select HAVE_ARCH_KGDB select HAVE_KRETPROBES select HAVE_ARCH_TRACEHOOK
arch/powerpc/include/asm/code-patching.h +1 -0
@@ -35,6 +35,7 @@ int instr_is_branch_to_addr(const unsigned int *instr, unsigned long addr);unsigned long branch_target (const unsigned int *instr);unsigned int translate_branch (const unsigned int *dest,const unsigned int *src); +extern bool is_conditional_branch (unsigned int instr);#ifdef CONFIG_PPC_BOOK3E_64 void __patch_exception (int exc,unsigned long addr);#define patch_exception (exc, name) do { \
arch/powerpc/include/asm/kprobes.h +23 -1
@@ -40,7 +40,23 @@ struct pt_regs;struct kprobe ;typedef ppc_opcode_t kprobe_opcode_t ; -#define MAX_INSN_SIZE 1 + +extern kprobe_opcode_t optinsn_slot; + +/* Optinsn template address */ +extern kprobe_opcode_t optprobe_template_entry[]; +extern kprobe_opcode_t optprobe_template_op_address[]; +extern kprobe_opcode_t optprobe_template_call_handler[]; +extern kprobe_opcode_t optprobe_template_insn[]; +extern kprobe_opcode_t optprobe_template_call_emulate[]; +extern kprobe_opcode_t optprobe_template_ret[]; +extern kprobe_opcode_t optprobe_template_end[]; + +/* Fixed instruction size for powerpc */ +#define MAX_INSN_SIZE 1 +#define MAX_OPTIMIZED_LENGTH sizeof(kprobe_opcode_t)/* 4 bytes */ +#define MAX_OPTINSN_SIZE (optprobe_template_end - optprobe_template_entry) +#define RELATIVEJUMP_SIZE sizeof(kprobe_opcode_t)/* 4 bytes */ #ifdef PPC64_ELF_ABI_v2/* PPC64 ABIv2 needs local entry point */
@@ -126,6 +142,12 @@ struct kprobe_ctlblk {struct prev_kprobe prev_kprobe; }; +struct arch_optimized_insn { +kprobe_opcode_t copied_insn[1 ]; +/* detour buffer */ +kprobe_opcode_t *insn; +}; +extern int kprobe_exceptions_notify (struct notifier_block *self,unsigned long val,void *data);extern int kprobe_fault_handler (struct pt_regs *regs,int trapnr);
arch/powerpc/kernel/Makefile +1 -0
@@ -100,6 +100,7 @@ obj-$(CONFIG_KGDB) += kgdb.o obj-$(CONFIG_BOOTX_TEXT) += btext.o obj-$(CONFIG_SMP) += smp.o obj-$(CONFIG_KPROBES) += kprobes.o +obj-$(CONFIG_OPTPROBES) += optprobes.o optprobes_head.o obj-$(CONFIG_UPROBES) += uprobes.o obj-$(CONFIG_PPC_UDBG_16550) += legacy_serial.o udbg_16550.o obj-$(CONFIG_STACKTRACE) += stacktrace.o
arch/powerpc/kernel/optprobes.c new file +348 -0
348 lines not on this page · raw message
arch/powerpc/kernel/optprobes_head.S new file +135 -0
135 lines not on this page · raw message
arch/powerpc/lib/code-patching.c +21 -0
@@ -54,6 +54,27 @@ bool is_offset_in_branch_range(long offset)return (offset >=-0x2000000 && offset <=0x1fffffc && !(offset &0x3 )); } +/* +* Helper to check if a given instruction is a conditional branch +* Derived from the conditional checks in analyse_instr() +*/ +bool __kprobes is_conditional_branch (unsigned int instr) +{ +unsigned int opcode = instr >>26 ; + +if (opcode ==16 )/* bc, bca, bcl, bcla */ +return true; +if (opcode ==19 ) { +switch ((instr >>1 ) &0x3ff ) { +case 16 :/* bclr, bclrl */ +case 528 :/* bcctr, bcctrl */ +case 560 :/* bctar, bctarl */ +return true; + } + } +return false; +} +unsigned int create_branch (const unsigned int *addr,unsigned long target,int flags) {
Signature · 1 line
2.7.4
PATCH v4 2/2 arch/powerpc: Optimize kprobe in kretprobe_trampoline
In reply to Anju T SudhakarKprobe placed on the kretprobe_trampoline during boot time can be optimized, since the instruction at probe point is a 'nop'.
--- arch/powerpc/kernel/kprobes.c | 8 ++++++++ arch/powerpc/kernel/optprobes.c | 7 +++---- 2 files changed, 11 insertions(+), 4 deletions(-)
arch/powerpc/kernel/kprobes.c +8 -0
@@ -282,6 +282,7 @@ asm(".global kretprobe_trampoline\n"".type kretprobe_trampoline, @function\n" "kretprobe_trampoline:\n" "nop\n" +"blr\n" ".size kretprobe_trampoline, .-kretprobe_trampoline\n");/*
@@ -334,6 +335,13 @@ static int __kprobes trampoline_probe_handler(struct kprobe *p,kretprobe_assert (ri, orig_ret_address, trampoline_address); regs->nip = orig_ret_address; +/* +* Make LR point to the orig_ret_address. +* When the 'nop' inside the kretprobe_trampoline +* is optimized, we can do a 'blr' after executing the +* detour buffer code. +*/ + regs->link = orig_ret_address;reset_current_kprobe ();kretprobe_hash_unlock (current, &flags);
arch/powerpc/kernel/optprobes.c +3 -4
@@ -72,12 +72,11 @@ static unsigned long can_optimize(struct kprobe *p)/* * kprobe placed for kretprobe during boot time -* is not optimizing now. -* -* TODO: Optimize kprobe in kretprobe_trampoline +* has a 'nop' instruction, which can be emulated. +* So further checks can be skipped. */ if (p->addr == (kprobe_opcode_t *)&kretprobe_trampoline) -return 0 ; +return (unsigned long )p->addr +sizeof (kprobe_opcode_t);/* * We only support optimizing kernel addresses, but not
Signature · 1 line
2.7.4
Re: [v4,1/2] arch/powerpc: Implement Optprobes
In reply to Anju T SudhakarQuoted 14 lines · Anju T
Current infrastructure of kprobe uses the unconditional trap instruction to probe a running kernel. Optprobe allows kprobe to replace the trap with a branch instruction to a detour buffer. Detour buffer contains instructions to create an in memory
pt_regs. Detour buffer also has a call tooptimized_callback()which in turn call thepre_handler().
After the execution of the pre-handler, a call is made for instruction emulation. The NIP is determined in advanced through dummy instruction emulation and a branch instruction is created to the NIP at the end of the trampoline.To address the limitation of branch instruction in POWER architecture, detour buffer slot is allocated from a reserved area. For the time being, 64KB is reserved in memory for this purpose.
Quoted 8 lines
Instructions which can be emulated using
analyse_instr()are the candidates for optimization. Before optimization ensure that the address range between the detour buffer allocated and the instruction being probed is within +/- 32MB.Signed-off-by Anju T Sudhakar <[email protected]>Signed-off-by Naveen N. Rao <[email protected]>Acked-by Masami Hiramatsu <[email protected]>
Series applied to powerpc next, thanks.
https://git.kernel.org/powerpc/c/51c9c0843993528bffc920c54c2121
cheers
Re: [v4,1/2] arch/powerpc: Implement Optprobes
Thank You Michael. :)
Quoted 29 lines · Michael Ellerman
Quoted 14 lines · Anju T
Current infrastructure of kprobe uses the unconditional trap instruction to probe a running kernel. Optprobe allows kprobe to replace the trap with a branch instruction to a detour buffer. Detour buffer contains instructions to create an in memory
pt_regs. Detour buffer also has a call tooptimized_callback()which in turn call thepre_handler().
After the execution of the pre-handler, a call is made for instruction emulation. The NIP is determined in advanced through dummy instruction emulation and a branch instruction is created to the NIP at the end of the trampoline.To address the limitation of branch instruction in POWER architecture, detour buffer slot is allocated from a reserved area. For the time being, 64KB is reserved in memory for this purpose.
Quoted 8 lines
Instructions which can be emulated using
analyse_instr()are the candidates for optimization. Before optimization ensure that the address range between the detour buffer allocated and the instruction being probed is within +/- 32MB.Signed-off-by Anju T Sudhakar <[email protected]>Signed-off-by Naveen N. Rao <[email protected]>Acked-by Masami Hiramatsu <[email protected]>Series applied to powerpc next, thanks.
https://git.kernel.org/powerpc/c/51c9c0843993528bffc920c54c2121
cheers