Skip to content

Commit 3e46bb4

Browse files
ahunter6Peter Zijlstra
authored andcommitted
perf/x86: Add perf text poke events for kprobes
Add perf text poke events for kprobes. That includes: - the replaced instruction(s) which are executed out-of-line i.e. arch_copy_kprobe() and arch_remove_kprobe() - the INT3 that activates the kprobe i.e. arch_arm_kprobe() and arch_disarm_kprobe() - optimised kprobe function i.e. arch_prepare_optimized_kprobe() and __arch_remove_optimized_kprobe() - optimised kprobe i.e. arch_optimize_kprobes() and arch_unoptimize_kprobe() Resulting in 8 possible text_poke events: 0: NULL -> probe.ainsn.insn (if ainsn.boostable && !kp.post_handler) arch_copy_kprobe() 1: old0 -> INT3 arch_arm_kprobe() // boosted kprobe active 2: NULL -> optprobe_trampoline arch_prepare_optimized_kprobe() 3: INT3,old1,old2,old3,old4 -> JMP32 arch_optimize_kprobes() // optprobe active 4: JMP32 -> INT3,old1,old2,old3,old4 // optprobe disabled and kprobe active (this sometimes goes back to 3) arch_unoptimize_kprobe() 5: optprobe_trampoline -> NULL arch_remove_optimized_kprobe() // boosted kprobe active 6: INT3 -> old0 arch_disarm_kprobe() 7: probe.ainsn.insn -> NULL (if ainsn.boostable && !kp.post_handler) arch_remove_kprobe() Signed-off-by: Adrian Hunter <adrian.hunter@intel.com> Signed-off-by: Peter Zijlstra (Intel) <peterz@infradead.org> Acked-by: Peter Zijlstra (Intel) <peterz@infradead.org> Acked-by: Masami Hiramatsu <mhiramat@kernel.org> Link: https://lkml.kernel.org/r/20200512121922.8997-6-adrian.hunter@intel.com
1 parent 69e4908 commit 3e46bb4

3 files changed

Lines changed: 49 additions & 6 deletions

File tree

‎arch/x86/include/asm/kprobes.h‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,8 @@ struct arch_specific_insn {
6666
*/
6767
bool boostable;
6868
bool if_modifier;
69+
/* Number of bytes of text poked */
70+
int tp_len;
6971
};
7072

7173
struct arch_optimized_insn {

‎arch/x86/kernel/kprobes/core.c‎

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
#include <linux/hardirq.h>
3434
#include <linux/preempt.h>
3535
#include <linux/sched/debug.h>
36+
#include <linux/perf_event.h>
3637
#include <linux/extable.h>
3738
#include <linux/kdebug.h>
3839
#include <linux/kallsyms.h>
@@ -471,6 +472,9 @@ static int arch_copy_kprobe(struct kprobe *p)
471472
/* Also, displacement change doesn't affect the first byte */
472473
p->opcode = buf[0];
473474

475+
p->ainsn.tp_len = len;
476+
perf_event_text_poke(p->ainsn.insn, NULL, 0, buf, len);
477+
474478
/* OK, write back the instruction(s) into ROX insn buffer */
475479
text_poke(p->ainsn.insn, buf, len);
476480

@@ -502,19 +506,28 @@ int arch_prepare_kprobe(struct kprobe *p)
502506

503507
void arch_arm_kprobe(struct kprobe *p)
504508
{
505-
text_poke(p->addr, ((unsigned char []){INT3_INSN_OPCODE}), 1);
509+
u8 int3 = INT3_INSN_OPCODE;
510+
511+
text_poke(p->addr, &int3, 1);
506512
text_poke_sync();
513+
perf_event_text_poke(p->addr, &p->opcode, 1, &int3, 1);
507514
}
508515

509516
void arch_disarm_kprobe(struct kprobe *p)
510517
{
518+
u8 int3 = INT3_INSN_OPCODE;
519+
520+
perf_event_text_poke(p->addr, &int3, 1, &p->opcode, 1);
511521
text_poke(p->addr, &p->opcode, 1);
512522
text_poke_sync();
513523
}
514524

515525
void arch_remove_kprobe(struct kprobe *p)
516526
{
517527
if (p->ainsn.insn) {
528+
/* Record the perf event before freeing the slot */
529+
perf_event_text_poke(p->ainsn.insn, p->ainsn.insn,
530+
p->ainsn.tp_len, NULL, 0);
518531
free_insn_slot(p->ainsn.insn, p->ainsn.boostable);
519532
p->ainsn.insn = NULL;
520533
}

‎arch/x86/kernel/kprobes/opt.c‎

Lines changed: 33 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
* Copyright (C) Hitachi Ltd., 2012
77
*/
88
#include <linux/kprobes.h>
9+
#include <linux/perf_event.h>
910
#include <linux/ptrace.h>
1011
#include <linux/string.h>
1112
#include <linux/slab.h>
@@ -352,8 +353,15 @@ int arch_within_optimized_kprobe(struct optimized_kprobe *op,
352353
static
353354
void __arch_remove_optimized_kprobe(struct optimized_kprobe *op, int dirty)
354355
{
355-
if (op->optinsn.insn) {
356-
free_optinsn_slot(op->optinsn.insn, dirty);
356+
u8 *slot = op->optinsn.insn;
357+
if (slot) {
358+
int len = TMPL_END_IDX + op->optinsn.size + JMP32_INSN_SIZE;
359+
360+
/* Record the perf event before freeing the slot */
361+
if (dirty)
362+
perf_event_text_poke(slot, slot, len, NULL, 0);
363+
364+
free_optinsn_slot(slot, dirty);
357365
op->optinsn.insn = NULL;
358366
op->optinsn.size = 0;
359367
}
@@ -424,8 +432,15 @@ int arch_prepare_optimized_kprobe(struct optimized_kprobe *op,
424432
(u8 *)op->kp.addr + op->optinsn.size);
425433
len += JMP32_INSN_SIZE;
426434

435+
/*
436+
* Note len = TMPL_END_IDX + op->optinsn.size + JMP32_INSN_SIZE is also
437+
* used in __arch_remove_optimized_kprobe().
438+
*/
439+
427440
/* We have to use text_poke() for instruction buffer because it is RO */
441+
perf_event_text_poke(slot, NULL, 0, buf, len);
428442
text_poke(slot, buf, len);
443+
429444
ret = 0;
430445
out:
431446
kfree(buf);
@@ -477,10 +492,23 @@ void arch_optimize_kprobes(struct list_head *oplist)
477492
*/
478493
void arch_unoptimize_kprobe(struct optimized_kprobe *op)
479494
{
480-
arch_arm_kprobe(&op->kp);
481-
text_poke(op->kp.addr + INT3_INSN_SIZE,
482-
op->optinsn.copied_insn, DISP32_SIZE);
495+
u8 new[JMP32_INSN_SIZE] = { INT3_INSN_OPCODE, };
496+
u8 old[JMP32_INSN_SIZE];
497+
u8 *addr = op->kp.addr;
498+
499+
memcpy(old, op->kp.addr, JMP32_INSN_SIZE);
500+
memcpy(new + INT3_INSN_SIZE,
501+
op->optinsn.copied_insn,
502+
JMP32_INSN_SIZE - INT3_INSN_SIZE);
503+
504+
text_poke(addr, new, INT3_INSN_SIZE);
483505
text_poke_sync();
506+
text_poke(addr + INT3_INSN_SIZE,
507+
new + INT3_INSN_SIZE,
508+
JMP32_INSN_SIZE - INT3_INSN_SIZE);
509+
text_poke_sync();
510+
511+
perf_event_text_poke(op->kp.addr, old, JMP32_INSN_SIZE, new, JMP32_INSN_SIZE);
484512
}
485513

486514
/*

0 commit comments

Comments
 (0)