x86: allow to modify more instructions in one text_poke_bp call
Subsystems (2)
PATCH 3/6 x86: allow to modify more instructions in one text_poke_bp call
When trying to use text_poke_bp in dynamic ftrace, the process was significantly slower than the original code. For example, I tried to modify the ftrace function, enable, and disable the tracer in a cycle. With 9 different trace functions and 500 cycles, I got these times:
original code: with text_poke_bp: real 19m51.667s real 33m29.456s user 0m0.004s user 0m0.008s sys 0m27.124s sys 0m27.620s
It turned out that most of the extra time was spent in the call:
on_each_cpu(do_sync_core, NULL, 1)
It is needed to reset the speculative queue of decoded instructions on each CPU. It is relatively expensive operation. The original code reduced the usage by patching all instructions in parallel.
If we want to share the code and keep the effectivity, we need to extend text_poke_bp and allow to modify more instructions at once as well. It might be useful in more situations, for example, in arch_optimize_kprobes.
Note that the list of modified addresses has to be sorted. Otherwise, the int3 handler would be too slow.
Also note that "addr" and "handler" are arays of pointers (IPs). While "opcode" is a plain array of bytes, new instructions of the same length. text_poke_bp might be used to patch different length of instructions but there will be the same type of intructions for each call. It would be waste of memory if we pass instructions via an array of pointers.
Finally, the list of handlers is optional. ftrace and jump labels pretend that there are nops. Hence. the int3 handler could calculate the next IP address from the lenght of the patched instruction.
--- arch/x86/include/asm/alternative.h | 3 +- arch/x86/kernel/alternative.c | 74 ++++++++++++++++++++++++++++++-------- arch/x86/kernel/jump_label.c | 3 +- arch/x86/kernel/kprobes/opt.c | 8 ++--- 4 files changed, 67 insertions(+), 21 deletions(-)
arch/x86/include/asm/alternative.h +2 -1
@@ -226,6 +226,7 @@ extern void *text_poke_early(void *addr, const void *opcode, size_t len);*/ extern void *text_poke (void *addr,const void *opcode,size_t len);extern int poke_int3_handler (struct pt_regs *regs); -extern void *text_poke_bp (void *addr,const void *opcode,size_t len,void *handler); +extern void *text_poke_bp (void *addr[],const void *opcode,size_t len, +unsigned int count,void *handler[]);#endif /* _ASM_X86_ALTERNATIVE_H */
arch/x86/kernel/alternative.c +60 -14
133 lines not on this page · raw message
arch/x86/kernel/jump_label.c +1 -2
@@ -87,8 +87,7 @@ static void __jump_label_transform(struct jump_entry *entry,if (poker) (*poker)((void *)entry->code, &code,JUMP_LABEL_NOP_SIZE );else -text_poke_bp ((void *)entry->code, &code,JUMP_LABEL_NOP_SIZE , - (void *)entry->code +JUMP_LABEL_NOP_SIZE ); +text_poke_bp ((void *)entry->code, &code,JUMP_LABEL_NOP_SIZE ,1 ,NULL ); } void arch_jump_label_transform(struct jump_entry *entry,
arch/x86/kernel/kprobes/opt.c +4 -4
@@ -390,8 +390,8 @@ void __kprobes arch_optimize_kprobes(struct list_head *oplist) insn_buf[0 ] =RELATIVEJUMP_OPCODE ; *(s32 *)(&insn_buf[1 ]) = rel; -text_poke_bp (op->kp.addr, insn_buf,RELATIVEJUMP_SIZE , - op->optinsn.insn); +text_poke_bp ((void **)&(op->kp.addr), insn_buf,RELATIVEJUMP_SIZE ,1 , + (void **)&(op->optinsn.insn));list_del_init (&op->list); }
@@ -405,8 +405,8 @@ void __kprobes arch_unoptimize_kprobe(struct optimized_kprobe *op)/* Set int3 to first byte for kprobes */ insn_buf[0 ] =BREAKPOINT_INSTRUCTION ;memcpy (insn_buf +1 , op->optinsn.copied_insn,RELATIVE_ADDR_SIZE ); -text_poke_bp (op->kp.addr, insn_buf,RELATIVEJUMP_SIZE , - op->optinsn.insn); +text_poke_bp ((void **)&(op->kp.addr), insn_buf,RELATIVEJUMP_SIZE ,1 , + (void **)&(op->optinsn.insn)); }/*
Signature · 1 line
1.8.4