Skip to contentSkip to conversation

ARM: kprobes: enable OPTPROBES for ARM 32.

PATCH Superseded Wang Nan 2 participants 4 messages Oct 25, 2014 – Oct 27, 2014

Subsystems (2)

v7 of 10 Superseded Latest is v10 →

lore
Share & tools
lore

Wang Nan

author
#0 lore raw

v7 cover ARM: kprobes: enable OPTPROBES for ARM 32.

This is the 7th version of ARM KPROBEOPT patch series. This patch series is depend on previous patch series I have posted: 'ARM: kprobes:
introduces instruction checker.'.

Previous discussions can be found:

https://lkml.org/lkml/2014/10/22/254
https://lkml.org/lkml/2014/8/27/255
https://lkml.org/lkml/2014/8/12/12
https://lkml.org/lkml/2014/8/8/992
https://lkml.org/lkml/2014/8/8/5
https://lkml.org/lkml/2014/8/5/63

The main content of this patch series is identical to patch 6/7 and 7/7 in v6. I post a v7 series because Masami Hiramatsu suggests me to make the ARM OPTPROBES and the checker mechanism two sepreted series.

Wang Nan (2):
kprobes: copy ainsn after alloc aggr kprobe
ARM: kprobes: enable OPTPROBES for ARM 32

 arch/arm/Kconfig                  |   1 +
 arch/arm/include/asm/kprobes.h    |  26 ++++
 arch/arm/kernel/Makefile          |   3 +-
 arch/arm/kernel/kprobes-opt-arm.c | 285 ++++++++++++++++++++++++++++++++++++++
 kernel/kprobes.c                  |   6 +
 5 files changed, 320 insertions(+), 1 deletion(-)
 create mode 100644 arch/arm/kernel/kprobes-opt-arm.c
Signature · 1 line
1.8.4
author
#1 lore raw

PATCH v7 1/2 kprobes: copy ainsn after alloc aggr kprobe

Copy old kprobe to newly alloced optimized_kprobe before arch_prepare_optimized_kprobe(). Original kprove can brings more information to optimizer.

v1 -> v2:
- Bugfix: copy p->addr when alloc_aggr_kprobe.

---
 kernel/kprobes.c | 6 ++++++
 1 file changed, 6 insertions(+)
kernel/kprobes.c +6 -0
index 3995f54..e9868ec 100644
@@ -730,7 +730,13 @@ static struct kprobe *alloc_aggr_kprobe(struct kprobe *p)
 		return NULL;
 
 	INIT_LIST_HEAD(&op->list);
+
+	/*
+	 * copy gives arch_prepare_optimized_kprobe
+	 * more information
+	 */
 	op->kp.addr = p->addr;
+	copy_kprobe(p, &op->kp);
 	arch_prepare_optimized_kprobe(op);
 
 	return &op->kp;
Signature · 1 line
1.8.4
#2 lore raw

Re: [PATCH v7 1/2] kprobes: copy ainsn after alloc aggr kprobe

Quoted 3 lines · Wang Nan

Copy old kprobe to newly alloced optimized_kprobe before arch_prepare_optimized_kprobe(). Original kprove can brings more information to optimizer.

Hmm, yes, but this also call copy_kprobe twice in try_to_optimize_kprobe(). If you really need the original kprobe information, I think you'd better change the arch_prepare_optimized_kprobe as below;

/* Pass both of new optprobe and original kprobe */ arch_prepare_optimized_kprobe(op, p);

Thank you,

Quoted 28 lines

v1 -> v2:
- Bugfix: copy p->addr when alloc_aggr_kprobe.

---
 kernel/kprobes.c | 6 ++++++
 1 file changed, 6 insertions(+)
kernel/kprobes.c +6 -0
index 3995f54..e9868ec 100644
@@ -730,7 +730,13 @@ static struct kprobe *alloc_aggr_kprobe(struct kprobe *p)
 		return NULL;
 
 	INIT_LIST_HEAD(&op->list);
+
+	/*
+	 * copy gives arch_prepare_optimized_kprobe
+	 * more information
+	 */
 	op->kp.addr = p->addr;
+	copy_kprobe(p, &op->kp);
 	arch_prepare_optimized_kprobe(op);
 
 	return &op->kp;
Signature · 4 lines
Masami HIRAMATSU
Software Platform Research Dept. Linux Technology Research Center
Hitachi, Ltd., Yokohama Research Laboratory
E-mail: masami.hiramatsu.pt at hitachi.com
author
#3 lore raw

PATCH v7 2/2 ARM: kprobes: enable OPTPROBES for ARM 32

In reply to Wang Nan

This patch introduce kprobeopt for ARM 32.

Limitations:
- Currently only kernel compiled with ARM ISA is supported.

- Offset between probe point and optinsn slot must not larger than 32MiB. Masami Hiramatsu suggests replacing 2 words, it will make things complex. Futher patch can make such optimization.

Kprobe opt on ARM is relatively simpler than kprobe opt on x86 because ARM instruction is always 4 bytes aligned and 4 bytes long. This patch replace probed instruction by a 'b', branch to trampoline code and then calls optimized_callback(). optimized_callback() calls opt_pre_handler() to execute kprobe handler. It also emulate/simulate replaced instruction.

When unregistering kprobe, the deferred manner of unoptimizer may leave branch instruction before optimizer is called. Different from x86_64, which only copy the probed insn after optprobe_template_end and reexecute them, this patch call singlestep to emulate/simulate the insn directly. Futher patch can optimize this behavior.

v1 -> v2:

- Improvement: if replaced instruction is conditional, generate a conditional branch instruction for it;

- Introduces RELATIVEJUMP_OPCODES due to ARM kprobe_opcode_t is 4 bytes;

- Removes size field in struct arch_optimized_insn;

- Use arm_gen_branch() to generate branch instruction;

- Remove all recover logic: ARM doesn't use tail buffer, no need recover replaced instructions like x86;

- Remove incorrect CONFIG_THUMB checking;

- can_optimize() always returns true if address is well aligned;

- Improve optimized_callback: using opt_pre_handler();

- Bugfix: correct range checking code and improve comments;

- Fix commit message.

v2 -> v3:

- Rename RELATIVEJUMP_OPCODES to MAX_COPIED_INSNS;

- Remove unneeded checking:

      arch_check_optimized_kprobe(), can_optimize();

- Add missing flush_icache_range() in arch_prepare_optimized_kprobe();

- Remove unneeded 'return;'.

v3 -> v4:

- Use __mem_to_opcode_arm() to translate copied_insn to ensure it works in big endian kernel;

- Replace 'nop' placeholder in trampoline code template with '.long 0' to avoid confusion: reader may regard 'nop' as an instruction, but it is value in fact.

v4 -> v5:

- Don't optimize stack store operations.

- Introduce prepared field to arch_optimized_insn to indicate whether it is prepared. Similar to size field with x86. See v1 -> v2.

v5 -> v6:

- Dynamically reserve stack according to instruction.

- Rename: kprobes-opt.c -> kprobes-opt-arm.c.

- Set op->optinsn.insn after all works are done.

Cc Jon Medhurst (Tixy) <[email protected]>
Cc Russell King - ARM Linux <[email protected]>
Cc Will Deacon <[email protected]>
---
 arch/arm/Kconfig                  |   1 +
 arch/arm/include/asm/kprobes.h    |  26 ++++
 arch/arm/kernel/Makefile          |   3 +-
 arch/arm/kernel/kprobes-opt-arm.c | 285 ++++++++++++++++++++++++++++++++++++++
 4 files changed, 314 insertions(+), 1 deletion(-)
 create mode 100644 arch/arm/kernel/kprobes-opt-arm.c
arch/arm/Kconfig +1 -0
index 89c4b5c..8281cea 100644
@@ -59,6 +59,7 @@ config ARM
 	select HAVE_MEMBLOCK
 	select HAVE_MOD_ARCH_SPECIFIC if ARM_UNWIND
 	select HAVE_OPROFILE if (HAVE_PERF_EVENTS)
+	select HAVE_OPTPROBES if (!THUMB2_KERNEL)
 	select HAVE_PERF_EVENTS
 	select HAVE_PERF_REGS
 	select HAVE_PERF_USER_STACK_DUMP
arch/arm/include/asm/kprobes.h +26 -0
index 49fa0df..a05297f 100644
@@ -51,5 +51,31 @@ int kprobe_fault_handler(struct pt_regs *regs, unsigned int fsr);
 int kprobe_exceptions_notify(struct notifier_block *self,
 			     unsigned long val, void *data);
 
+/* optinsn template addresses */
+extern __visible kprobe_opcode_t optprobe_template_entry;
+extern __visible kprobe_opcode_t optprobe_template_val;
+extern __visible kprobe_opcode_t optprobe_template_call;
+extern __visible kprobe_opcode_t optprobe_template_end;
+
+#define MAX_OPTIMIZED_LENGTH	(4)
+#define MAX_OPTINSN_SIZE				\
+	(((unsigned long)&optprobe_template_end -	\
+	  (unsigned long)&optprobe_template_entry))
+#define RELATIVEJUMP_SIZE	(4)
+
+struct arch_optimized_insn {
+	/*
+	 * copy of the original instructions.
+	 * Different from x86, ARM kprobe_opcode_t is u32.
+	 */
+#define MAX_COPIED_INSN	((RELATIVEJUMP_SIZE) / sizeof(kprobe_opcode_t))
+	kprobe_opcode_t copied_insn[MAX_COPIED_INSN];
+	/* detour code buffer */
+	kprobe_opcode_t *insn;
+	/*
+	 *  we always copies one instruction on arm32,
+	 *  size always be 4, so no size field.
+	 */
+};
 
 #endif /* _ARM_KPROBES_H */
arch/arm/kernel/Makefile +2 -1
index 38ddd9f..2fcd980 100644
@@ -52,11 +52,12 @@ obj-$(CONFIG_FUNCTION_GRAPH_TRACER)	+= ftrace.o insn.o
 obj-$(CONFIG_JUMP_LABEL)	+= jump_label.o insn.o patch.o
 obj-$(CONFIG_KEXEC)		+= machine_kexec.o relocate_kernel.o
 obj-$(CONFIG_UPROBES)		+= probes.o probes-arm.o uprobes.o uprobes-arm.o
-obj-$(CONFIG_KPROBES)		+= probes.o kprobes.o kprobes-common.o patch.o
+obj-$(CONFIG_KPROBES)		+= probes.o kprobes.o kprobes-common.o patch.o insn.o
 ifdef CONFIG_THUMB2_KERNEL
 obj-$(CONFIG_KPROBES)		+= kprobes-thumb.o probes-thumb.o
 else
 obj-$(CONFIG_KPROBES)		+= kprobes-arm.o probes-arm.o
+obj-$(CONFIG_OPTPROBES)		+= kprobes-opt-arm.o
 endif
 obj-$(CONFIG_ARM_KPROBES_TEST)	+= test-kprobes.o
 test-kprobes-objs		:= kprobes-test.o
arch/arm/kernel/kprobes-opt-arm.c new file +285 -0

285 lines not on this page · raw message

Signature · 1 line
1.8.4