Skip to contentSkip to conversation

ARM: kprobes: enable OPTPROBES for ARM 32.

PATCH Superseded Wang Nan 3 participants 13 messages Oct 22, 2014 – Oct 28, 2014

Subsystems (2)

v6 of 10 Superseded Latest is v10 →

lore
Share & tools
lore

Wang Nan

author
#0 lore raw

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

Previous 5 version of ARM OPTPROBES patches are unable to deal with stack storing instructions correctly. V5 patches disallow optimizing every protential stack store instructions based on pessimistic assumption. Which, as Tixy comments, 'excludes the main use of kprobes'. (https://lkml.org/lkml/2014/8/29/117 )

The main obstacle which prevents us from computing stack requirement is the missing of per-instruction decoder in probes_decode_insn() and its friends. Only part of instructions have their decoders (and not in each case).

In this patch series, I propose 'checker', which allows us define functions for each type of instruction, extract more information. Stack consumption computing is an example. Checker can be further employed to determine whether one instruction is possible to execute directy in optimized kprobe. I'd like to expand current checker framework by chaining checkers together. After that, I believe most of ARM instructions can be executed directly like x86, kprobe performace can be improved.

The first 3 patches introduces checker. After that, patch 4/7 checks stack requirement for probed instructions. Patches 5/7 - 7/7 are similar to patch v5, except:

1. As Tixy proposed, unoptimized probes are also suffer from stack problem (https://lkml.org/lkml/2014/9/1/548 ). Commit d30a0c8b saves 64 bytes for them, but for instruction use register addressing (like 'str r0, [sp, r1]'), 64 bytes are unsafe. Patch 5/7 prohibit such probing according to stack information collected by checker.

2. In patch 7/7, stack protection code now is generated according to the instruction be optimized.

3. In patch 7/7, kprobes-opt.c is renamed to kprobes-opt-arm.c due to it only deal with ARM case.

4. Bug in v5 is fixed.

Wang Nan (7):
ARM: kprobes: replace 'union decode_action' to 'struct decode_action'
ARM: kprobes: seprates load and store actions
ARM: kprobes: introduces checker
ARM: kprobes: collects stack consumption for store instructions
ARM: kprobes: disallow probing stack consuming instructions
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/include/asm/probes.h        |   1 +
 arch/arm/kernel/Makefile             |   3 +-
 arch/arm/kernel/kprobes-arm.c        |  12 +-
 arch/arm/kernel/kprobes-opt-arm.c    | 285 +++++++++++++++++++++++++++++++++++
 arch/arm/kernel/kprobes-test-arm.c   |  17 ++-
 arch/arm/kernel/kprobes-test-thumb.c |  13 ++
 arch/arm/kernel/kprobes-thumb.c      |  24 +--
 arch/arm/kernel/kprobes.c            |  10 +-
 arch/arm/kernel/kprobes.h            |   8 +-
 arch/arm/kernel/probes-arm.c         |  32 +++-
 arch/arm/kernel/probes-arm.h         |  15 +-
 arch/arm/kernel/probes-thumb.c       | 152 ++++++++++++++++---
 arch/arm/kernel/probes-thumb.h       |  31 +++-
 arch/arm/kernel/probes.c             |  76 +++++++++-
 arch/arm/kernel/probes.h             |  27 +++-
 arch/arm/kernel/uprobes-arm.c        |  12 +-
 arch/arm/kernel/uprobes.h            |   2 +-
 kernel/kprobes.c                     |   6 +
 20 files changed, 679 insertions(+), 74 deletions(-)
 create mode 100644 arch/arm/kernel/kprobes-opt-arm.c
Signature · 1 line
1.8.4
author
#1 lore raw

PATCH v6 1/7 ARM: kprobes: replace 'union decode_action' to 'struct decode_action'

This patch is generated simply using:

 $ sed -i "s/union decode_action/struct decode_action/g" `grep decode_action * -rl`

Which allows futher expansion to decode_action.

---
 arch/arm/kernel/kprobes-arm.c   | 2 +-
 arch/arm/kernel/kprobes-thumb.c | 4 ++--
 arch/arm/kernel/kprobes.c       | 2 +-
 arch/arm/kernel/kprobes.h       | 8 ++++----
 arch/arm/kernel/probes-arm.c    | 2 +-
 arch/arm/kernel/probes-arm.h    | 2 +-
 arch/arm/kernel/probes-thumb.c  | 4 ++--
 arch/arm/kernel/probes-thumb.h  | 4 ++--
 arch/arm/kernel/probes.c        | 2 +-
 arch/arm/kernel/probes.h        | 4 ++--
 arch/arm/kernel/uprobes-arm.c   | 2 +-
 arch/arm/kernel/uprobes.h       | 2 +-
 12 files changed, 19 insertions(+), 19 deletions(-)
arch/arm/kernel/kprobes-arm.c +1 -1
index ac300c6..6df9f1f 100644
@@ -301,7 +301,7 @@ emulate_rdlo12rdhi16rn0rm8_rwflags_nopc(probes_opcode_t insn,
 	regs->ARM_cpsr = (regs->ARM_cpsr & ~APSR_MASK) | (cpsr & APSR_MASK);
 }
 
-const union decode_action kprobes_arm_actions[NUM_PROBES_ARM_ACTIONS] = {
+const struct decode_action kprobes_arm_actions[NUM_PROBES_ARM_ACTIONS] = {
 	[PROBES_EMULATE_NONE] = {.handler = probes_emulate_none},
 	[PROBES_SIMULATE_NOP] = {.handler = probes_simulate_nop},
 	[PROBES_PRELOAD_IMM] = {.handler = probes_simulate_nop},
arch/arm/kernel/kprobes-thumb.c +2 -2
index 9495d7f..0672457 100644
@@ -611,7 +611,7 @@ t16_decode_pop(probes_opcode_t insn, struct arch_probes_insn *asi,
 	return INSN_GOOD;
 }
 
-const union decode_action kprobes_t16_actions[NUM_PROBES_T16_ACTIONS] = {
+const struct decode_action kprobes_t16_actions[NUM_PROBES_T16_ACTIONS] = {
 	[PROBES_T16_ADD_SP] = {.handler = t16_simulate_add_sp_imm},
 	[PROBES_T16_CBZ] = {.handler = t16_simulate_cbz},
 	[PROBES_T16_SIGN_EXTEND] = {.handler = t16_emulate_loregs_rwflags},
@@ -634,7 +634,7 @@ const union decode_action kprobes_t16_actions[NUM_PROBES_T16_ACTIONS] = {
 	[PROBES_T16_BRANCH] = {.handler = t16_simulate_branch},
 };
 
-const union decode_action kprobes_t32_actions[NUM_PROBES_T32_ACTIONS] = {
+const struct decode_action kprobes_t32_actions[NUM_PROBES_T32_ACTIONS] = {
 	[PROBES_T32_LDMSTM] = {.decoder = t32_decode_ldmstm},
 	[PROBES_T32_LDRDSTRD] = {.handler = t32_emulate_ldrdstrd},
 	[PROBES_T32_TABLE_BRANCH] = {.handler = t32_simulate_table_branch},
arch/arm/kernel/kprobes.c +1 -1
index 6d64420..028159c 100644
@@ -59,7 +59,7 @@ int __kprobes arch_prepare_kprobe(struct kprobe *p)
 	unsigned long addr = (unsigned long)p->addr;
 	bool thumb;
 	kprobe_decode_insn_t *decode_insn;
-	const union decode_action *actions;
+	const struct decode_action *actions;
 	int is;
 
 	if (in_exception_text(addr))
arch/arm/kernel/kprobes.h +4 -4
index 9a2712e..34db1eb 100644
@@ -36,16 +36,16 @@ kprobe_decode_ldmstm(kprobe_opcode_t insn, struct arch_probes_insn *asi,
 typedef enum probes_insn (kprobe_decode_insn_t)(probes_opcode_t,
 						struct arch_probes_insn *,
 						bool,
-						const union decode_action *);
+						const struct decode_action *);
 
 #ifdef CONFIG_THUMB2_KERNEL
 
-extern const union decode_action kprobes_t32_actions[];
-extern const union decode_action kprobes_t16_actions[];
+extern const struct decode_action kprobes_t32_actions[];
+extern const struct decode_action kprobes_t16_actions[];
 
 #else /* !CONFIG_THUMB2_KERNEL */
 
-extern const union decode_action kprobes_arm_actions[];
+extern const struct decode_action kprobes_arm_actions[];
 
 #endif
 
arch/arm/kernel/probes-arm.c +1 -1
index 8eaef81..3d00aa7 100644
@@ -725,7 +725,7 @@ static void __kprobes arm_singlestep(probes_opcode_t insn,
  */
 enum probes_insn __kprobes
 arm_probes_decode_insn(probes_opcode_t insn, struct arch_probes_insn *asi,
-		       bool emulate, const union decode_action *actions)
+		       bool emulate, const struct decode_action *actions)
 {
 	asi->insn_singlestep = arm_singlestep;
 	asi->insn_check_cc = probes_condition_checks[insn>>28];
arch/arm/kernel/probes-arm.h +1 -1
index ace6572..6ecc25a 100644
@@ -68,6 +68,6 @@ extern const union decode_item probes_decode_arm_table[];
 
 enum probes_insn arm_probes_decode_insn(probes_opcode_t,
 		struct arch_probes_insn *, bool emulate,
-		const union decode_action *actions);
+		const struct decode_action *actions);
 
 #endif
arch/arm/kernel/probes-thumb.c +2 -2
index 4131351..72aa217 100644
@@ -863,7 +863,7 @@ static void __kprobes thumb32_singlestep(probes_opcode_t opcode,
 
 enum probes_insn __kprobes
 thumb16_probes_decode_insn(probes_opcode_t insn, struct arch_probes_insn *asi,
-			   bool emulate, const union decode_action *actions)
+			   bool emulate, const struct decode_action *actions)
 {
 	asi->insn_singlestep = thumb16_singlestep;
 	asi->insn_check_cc = thumb_check_cc;
@@ -873,7 +873,7 @@ thumb16_probes_decode_insn(probes_opcode_t insn, struct arch_probes_insn *asi,
 
 enum probes_insn __kprobes
 thumb32_probes_decode_insn(probes_opcode_t insn, struct arch_probes_insn *asi,
-			   bool emulate, const union decode_action *actions)
+			   bool emulate, const struct decode_action *actions)
 {
 	asi->insn_singlestep = thumb32_singlestep;
 	asi->insn_check_cc = thumb_check_cc;
arch/arm/kernel/probes-thumb.h +2 -2
index 7c6f6eb..2658d95 100644
@@ -89,9 +89,9 @@ extern const union decode_item probes_decode_thumb16_table[];
 
 enum probes_insn __kprobes
 thumb16_probes_decode_insn(probes_opcode_t insn, struct arch_probes_insn *asi,
-		bool emulate, const union decode_action *actions);
+		bool emulate, const struct decode_action *actions);
 enum probes_insn __kprobes
 thumb32_probes_decode_insn(probes_opcode_t insn, struct arch_probes_insn *asi,
-		bool emulate, const union decode_action *actions);
+		bool emulate, const struct decode_action *actions);
 
 #endif
arch/arm/kernel/probes.c +1 -1
index a8ab540..ec030b8 100644
@@ -388,7 +388,7 @@ static const int decode_struct_sizes[NUM_DECODE_TYPES] = {
 int __kprobes
 probes_decode_insn(probes_opcode_t insn, struct arch_probes_insn *asi,
 		   const union decode_item *table, bool thumb,
-		   bool emulate, const union decode_action *actions)
+		   bool emulate, const struct decode_action *actions)
 {
 	const struct decode_header *h = (struct decode_header *)table;
 	const struct decode_header *next;
arch/arm/kernel/probes.h +2 -2
index dba9f24..739c2a2 100644
@@ -309,7 +309,7 @@ typedef enum probes_insn (probes_custom_decode_t)(probes_opcode_t,
 						  struct arch_probes_insn *,
 						  const struct decode_header *);
 
-union decode_action {
+struct decode_action {
 	probes_insn_handler_t	*handler;
 	probes_custom_decode_t	*decoder;
 };
@@ -402,6 +402,6 @@ probes_insn_handler_t probes_emulate_none;
 int __kprobes
 probes_decode_insn(probes_opcode_t insn, struct arch_probes_insn *asi,
 		const union decode_item *table, bool thumb, bool emulate,
-		const union decode_action *actions);
+		const struct decode_action *actions);
 
 #endif
arch/arm/kernel/uprobes-arm.c +1 -1
index d3b655f..0c0f299 100644
@@ -194,7 +194,7 @@ uprobe_decode_ldmstm(probes_opcode_t insn,
 	return INSN_GOOD;
 }
 
-const union decode_action uprobes_probes_actions[] = {
+const struct decode_action uprobes_probes_actions[] = {
 	[PROBES_EMULATE_NONE] = {.handler = probes_simulate_nop},
 	[PROBES_SIMULATE_NOP] = {.handler = probes_simulate_nop},
 	[PROBES_PRELOAD_IMM] = {.handler = probes_simulate_nop},
arch/arm/kernel/uprobes.h +1 -1
index 1d0c12d..57b4373 100644
@@ -30,6 +30,6 @@ enum probes_insn
 decode_pc_ro(probes_opcode_t insn, struct arch_probes_insn *asi,
 	     const struct decode_header *d);
 
-extern const union decode_action uprobes_probes_actions[];
+extern const struct decode_action uprobes_probes_actions[];
 
 #endif
Signature · 1 line
1.8.4
author
#2 lore raw

PATCH v6 2/7 ARM: kprobes: seprates load and store actions

In reply to Wang Nan

This patch seprates actions for load and store. Following patches will check store instructions for more informations.

Coverage test complains register test coverage missing after this sepration. This patch introduces one testcase for it.

---
 arch/arm/kernel/kprobes-arm.c        |  6 ++-
 arch/arm/kernel/kprobes-test-arm.c   |  1 +
 arch/arm/kernel/kprobes-test-thumb.c | 13 ++++++
 arch/arm/kernel/kprobes-thumb.c      | 18 ++++++---
 arch/arm/kernel/probes-arm.c         | 11 +++--
 arch/arm/kernel/probes-arm.h         |  6 ++-
 arch/arm/kernel/probes-thumb.c       | 78 +++++++++++++++++++++++++++---------
 arch/arm/kernel/probes-thumb.h       | 18 ++++++---
 arch/arm/kernel/uprobes-arm.c        |  6 ++-
 9 files changed, 116 insertions(+), 41 deletions(-)
arch/arm/kernel/kprobes-arm.c +4 -2
index 6df9f1f..1094ff1 100644
@@ -315,7 +315,8 @@ const struct decode_action kprobes_arm_actions[NUM_PROBES_ARM_ACTIONS] = {
 	[PROBES_MUL1] = {.handler = emulate_rdlo12rdhi16rn0rm8_rwflags_nopc},
 	[PROBES_MUL2] = {.handler = emulate_rd16rn12rm0rs8_rwflags_nopc},
 	[PROBES_SWP] = {.handler = emulate_rd12rn16rm0_rwflags_nopc},
-	[PROBES_LDRSTRD] = {.handler = emulate_ldrdstrd},
+	[PROBES_LDRD] = {.handler = emulate_ldrdstrd},
+	[PROBES_STRD] = {.handler = emulate_ldrdstrd},
 	[PROBES_LOAD_EXTRA] = {.handler = emulate_ldr},
 	[PROBES_LOAD] = {.handler = emulate_ldr},
 	[PROBES_STORE_EXTRA] = {.handler = emulate_str},
@@ -339,5 +340,6 @@ const struct decode_action kprobes_arm_actions[NUM_PROBES_ARM_ACTIONS] = {
 	[PROBES_MUL_ADD] = {.handler = emulate_rd16rn12rm0rs8_rwflags_nopc},
 	[PROBES_BITFIELD] = {.handler = emulate_rd12rm0_noflags_nopc},
 	[PROBES_BRANCH] = {.handler = simulate_bbl},
-	[PROBES_LDMSTM] = {.decoder = kprobe_decode_ldmstm}
+	[PROBES_LDM] = {.decoder = kprobe_decode_ldmstm},
+	[PROBES_STM] = {.decoder = kprobe_decode_ldmstm},
 };
arch/arm/kernel/kprobes-test-arm.c +1 -0
index cb14242..264c064 100644
@@ -571,6 +571,7 @@ void kprobe_arm_test_cases(void)
 	TEST_RPR(  "strd	r",2, VAL1,", [r",5, 24,"], r",4,48,"")
 	TEST_RPR(  "strd	r",10,VAL2,", [r",9, 48,"], -r",7,24,"")
 	TEST_UNSUPPORTED(__inst_arm(0xe1afc0fa) "	@ strd r12, [pc, r10]!")
+	TEST_UNSUPPORTED(__inst_arm(0xe1aac0ff) "	@ strd r12, [r10, pc]!")
 
 	TEST_PR(   "ldrd	r0, [r",0, 48,", -r",2,24,"]")
 	TEST_PR(   "ldrmid	r8, [r",13,0, ", r",12,48,"]")
arch/arm/kernel/kprobes-test-thumb.c +13 -0
index 844dd10..ed863c4 100644
@@ -410,6 +410,13 @@ void kprobe_thumb32_test_cases(void)
 	TEST_UNSUPPORTED(__inst_thumb32(0xe9d47d00) "	@ ldrd	r7, sp, [r4]")
 	TEST_UNSUPPORTED(__inst_thumb32(0xe9d47f00) "	@ ldrd	r7, pc, [r4]")
 
+	TEST_UNSUPPORTED(__inst_thumb32(0xe9efec04) "	@ strd	r14, r12, [pc, #16]!")
+	TEST_UNSUPPORTED(__inst_thumb32(0xe8efec04) "	@ strd	r14, r12, [pc], #16")
+	TEST_UNSUPPORTED(__inst_thumb32(0xe9c4d800) "	@ strd	sp, r8, [r4]")
+	TEST_UNSUPPORTED(__inst_thumb32(0xe9c4f800) "	@ strd	pc, r8, [r4]")
+	TEST_UNSUPPORTED(__inst_thumb32(0xe9c47d00) "	@ strd	r7, sp, [r4]")
+	TEST_UNSUPPORTED(__inst_thumb32(0xe9c47f00) "	@ strd	r7, pc, [r4]")
+
 	TEST_RRP("strd	r",0, VAL1,", r",1, VAL2,", [r",1, 24,", #-16]")
 	TEST_RR( "strd	r",12,VAL2,", r",14,VAL1,", [sp, #16]")
 	TEST_RRP("strd	r",1, VAL1,", r",0, VAL2,", [r",7, 24,", #-16]!")
@@ -832,6 +839,12 @@ CONDITION_INSTRUCTIONS(22,
 	TEST("str	sp, [sp]")
 	TEST_UNSUPPORTED(__inst_thumb32(0xf8cfe000) "	@ str	r14, [pc]")
 	TEST_UNSUPPORTED(__inst_thumb32(0xf8cef000) "	@ str	pc, [r14]")
+	TEST_UNSUPPORTED(__inst_thumb32(0xf841100f) "	@ str	r1, [r1, pc]")
+	TEST_UNSUPPORTED(__inst_thumb32(0xf841100d) "	@ str	r1, [r1, sp]")
+	TEST_UNSUPPORTED(__inst_thumb32(0xf8a1d000) "	@ strh	sp, [r1]")
+	TEST_UNSUPPORTED(__inst_thumb32(0xf821d002) "	@ strh	sp, [r1, r2]")
+	TEST_UNSUPPORTED(__inst_thumb32(0xf822100f) "	@ strh	r1, [r2, pc]")
+	TEST_UNSUPPORTED(__inst_thumb32(0xf822100d) "	@ strh	r1, [r2, sp]")
 
 	TEST_GROUP("Advanced SIMD element or structure load/store instructions")
 
arch/arm/kernel/kprobes-thumb.c +12 -6
index 0672457..c6426b6 100644
@@ -626,17 +626,22 @@ const struct decode_action kprobes_t16_actions[NUM_PROBES_T16_ACTIONS] = {
 	[PROBES_T16_LDR_LIT] = {.handler = t16_simulate_ldr_literal},
 	[PROBES_T16_BLX] = {.handler = t16_simulate_bxblx},
 	[PROBES_T16_HIREGOPS] = {.decoder = t16_decode_hiregs},
-	[PROBES_T16_LDRHSTRH] = {.handler = t16_emulate_loregs_rwflags},
-	[PROBES_T16_LDRSTR] = {.handler = t16_simulate_ldrstr_sp_relative},
+	[PROBES_T16_LDRH] = {.handler = t16_emulate_loregs_rwflags},
+	[PROBES_T16_STRH] = {.handler = t16_emulate_loregs_rwflags},
+	[PROBES_T16_LDR] = {.handler = t16_simulate_ldrstr_sp_relative},
+	[PROBES_T16_STR] = {.handler = t16_simulate_ldrstr_sp_relative},
 	[PROBES_T16_ADR] = {.handler = t16_simulate_reladr},
-	[PROBES_T16_LDMSTM] = {.handler = t16_emulate_loregs_rwflags},
+	[PROBES_T16_LDM] = {.handler = t16_emulate_loregs_rwflags},
+	[PROBES_T16_STM] = {.handler = t16_emulate_loregs_rwflags},
 	[PROBES_T16_BRANCH_COND] = {.decoder = t16_decode_cond_branch},
 	[PROBES_T16_BRANCH] = {.handler = t16_simulate_branch},
 };
 
 const struct decode_action kprobes_t32_actions[NUM_PROBES_T32_ACTIONS] = {
-	[PROBES_T32_LDMSTM] = {.decoder = t32_decode_ldmstm},
-	[PROBES_T32_LDRDSTRD] = {.handler = t32_emulate_ldrdstrd},
+	[PROBES_T32_LDM] = {.decoder = t32_decode_ldmstm},
+	[PROBES_T32_STM] = {.decoder = t32_decode_ldmstm},
+	[PROBES_T32_LDRD] = {.handler = t32_emulate_ldrdstrd},
+	[PROBES_T32_STRD] = {.handler = t32_emulate_ldrdstrd},
 	[PROBES_T32_TABLE_BRANCH] = {.handler = t32_simulate_table_branch},
 	[PROBES_T32_TST] = {.handler = t32_emulate_rd8rn16rm0_rwflags},
 	[PROBES_T32_MOV] = {.handler = t32_emulate_rd8rn16rm0_rwflags},
@@ -655,7 +660,8 @@ const struct decode_action kprobes_t32_actions[NUM_PROBES_T32_ACTIONS] = {
 	[PROBES_T32_BRANCH] = {.handler = t32_simulate_branch},
 	[PROBES_T32_PLDI] = {.handler = probes_simulate_nop},
 	[PROBES_T32_LDR_LIT] = {.handler = t32_simulate_ldr_literal},
-	[PROBES_T32_LDRSTR] = {.handler = t32_emulate_ldrstr},
+	[PROBES_T32_LDR] = {.handler = t32_emulate_ldrstr},
+	[PROBES_T32_STR] = {.handler = t32_emulate_ldrstr},
 	[PROBES_T32_SIGN_EXTEND] = {.handler = t32_emulate_rd8rn16rm0_rwflags},
 	[PROBES_T32_MEDIA] = {.handler = t32_emulate_rd8rn16rm0_rwflags},
 	[PROBES_T32_REVERSE] = {.handler = t32_emulate_rd8rn16_noflags},
arch/arm/kernel/probes-arm.c +8 -3
index 3d00aa7..148153e 100644
@@ -270,13 +270,17 @@ static const union decode_item arm_cccc_000x_____1xx1_table[] = {
 	DECODE_REJECT	(0x0e10e0d0, 0x0000e0d0),
 
 	/* LDRD (register)	cccc 000x x0x0 xxxx xxxx xxxx 1101 xxxx */
+	DECODE_EMULATEX	(0x0e5000f0, 0x000000d0, PROBES_LDRD,
+						 REGS(NOPCWB, NOPCX, 0, 0, NOPC)),
 	/* STRD (register)	cccc 000x x0x0 xxxx xxxx xxxx 1111 xxxx */
-	DECODE_EMULATEX	(0x0e5000d0, 0x000000d0, PROBES_LDRSTRD,
+	DECODE_EMULATEX	(0x0e5000f0, 0x000000f0, PROBES_STRD,
 						 REGS(NOPCWB, NOPCX, 0, 0, NOPC)),
 
 	/* LDRD (immediate)	cccc 000x x1x0 xxxx xxxx xxxx 1101 xxxx */
+	DECODE_EMULATEX	(0x0e5000f0, 0x004000d0, PROBES_LDRD,
+						 REGS(NOPCWB, NOPCX, 0, 0, 0)),
 	/* STRD (immediate)	cccc 000x x1x0 xxxx xxxx xxxx 1111 xxxx */
-	DECODE_EMULATEX	(0x0e5000d0, 0x004000d0, PROBES_LDRSTRD,
+	DECODE_EMULATEX	(0x0e5000f0, 0x004000f0, PROBES_STRD,
 						 REGS(NOPCWB, NOPCX, 0, 0, 0)),
 
 	/* STRH (register)	cccc 000x x0x0 xxxx xxxx xxxx 1011 xxxx */
@@ -601,8 +605,9 @@ static const union decode_item arm_cccc_100x_table[] = {
 	/* Block data transfer instructions				*/
 
 	/* LDM			cccc 100x x0x1 xxxx xxxx xxxx xxxx xxxx */
+	DECODE_CUSTOM	(0x0e500000, 0x08100000, PROBES_LDM),
 	/* STM			cccc 100x x0x0 xxxx xxxx xxxx xxxx xxxx */
-	DECODE_CUSTOM	(0x0e400000, 0x08000000, PROBES_LDMSTM),
+	DECODE_CUSTOM	(0x0e500000, 0x08000000, PROBES_STM),
 
 	/* STM (user registers)	cccc 100x x1x0 xxxx xxxx xxxx xxxx xxxx */
 	/* LDM (user registers)	cccc 100x x1x1 xxxx 0xxx xxxx xxxx xxxx */
arch/arm/kernel/probes-arm.h +4 -2
index 6ecc25a..18ffc9a 100644
@@ -28,7 +28,8 @@ enum probes_arm_action {
 	PROBES_MUL1,
 	PROBES_MUL2,
 	PROBES_SWP,
-	PROBES_LDRSTRD,
+	PROBES_LDRD,
+	PROBES_STRD,
 	PROBES_LOAD,
 	PROBES_STORE,
 	PROBES_LOAD_EXTRA,
@@ -49,7 +50,8 @@ enum probes_arm_action {
 	PROBES_MUL_ADD,
 	PROBES_BITFIELD,
 	PROBES_BRANCH,
-	PROBES_LDMSTM,
+	PROBES_LDM,
+	PROBES_STM,
 	NUM_PROBES_ARM_ACTIONS
 };
 
arch/arm/kernel/probes-thumb.c +58 -20

153 lines not on this page · raw message

arch/arm/kernel/probes-thumb.h +12 -6
index 2658d95..a9c65c9 100644
@@ -30,8 +30,10 @@
 enum probes_t32_action {
 	PROBES_T32_EMULATE_NONE,
 	PROBES_T32_SIMULATE_NOP,
-	PROBES_T32_LDMSTM,
-	PROBES_T32_LDRDSTRD,
+	PROBES_T32_LDM,
+	PROBES_T32_STM,
+	PROBES_T32_LDRD,
+	PROBES_T32_STRD,
 	PROBES_T32_TABLE_BRANCH,
 	PROBES_T32_TST,
 	PROBES_T32_CMP,
@@ -50,7 +52,8 @@ enum probes_t32_action {
 	PROBES_T32_BRANCH,
 	PROBES_T32_PLDI,
 	PROBES_T32_LDR_LIT,
-	PROBES_T32_LDRSTR,
+	PROBES_T32_LDR,
+	PROBES_T32_STR,
 	PROBES_T32_SIGN_EXTEND,
 	PROBES_T32_MEDIA,
 	PROBES_T32_REVERSE,
@@ -75,10 +78,13 @@ enum probes_t16_action {
 	PROBES_T16_BLX,
 	PROBES_T16_HIREGOPS,
 	PROBES_T16_LDR_LIT,
-	PROBES_T16_LDRHSTRH,
-	PROBES_T16_LDRSTR,
+	PROBES_T16_LDRH,
+	PROBES_T16_STRH,
+	PROBES_T16_LDR,
+	PROBES_T16_STR,
 	PROBES_T16_ADR,
-	PROBES_T16_LDMSTM,
+	PROBES_T16_LDM,
+	PROBES_T16_STM,
 	PROBES_T16_BRANCH_COND,
 	PROBES_T16_BRANCH,
 	NUM_PROBES_T16_ACTIONS
arch/arm/kernel/uprobes-arm.c +4 -2
index 0c0f299..0a8caa3 100644
@@ -207,7 +207,8 @@ const struct decode_action uprobes_probes_actions[] = {
 	[PROBES_MUL1] = {.handler = probes_simulate_nop},
 	[PROBES_MUL2] = {.handler = probes_simulate_nop},
 	[PROBES_SWP] = {.handler = probes_simulate_nop},
-	[PROBES_LDRSTRD] = {.decoder = decode_pc_ro},
+	[PROBES_LDRD] = {.decoder = decode_pc_ro},
+	[PROBES_STRD] = {.decoder = decode_pc_ro},
 	[PROBES_LOAD_EXTRA] = {.decoder = decode_pc_ro},
 	[PROBES_LOAD] = {.decoder = decode_ldr},
 	[PROBES_STORE_EXTRA] = {.decoder = decode_pc_ro},
@@ -230,5 +231,6 @@ const struct decode_action uprobes_probes_actions[] = {
 	[PROBES_MUL_ADD] = {.handler = probes_simulate_nop},
 	[PROBES_BITFIELD] = {.handler = probes_simulate_nop},
 	[PROBES_BRANCH] = {.handler = simulate_bbl},
-	[PROBES_LDMSTM] = {.decoder = uprobe_decode_ldmstm}
+	[PROBES_LDM] = {.decoder = uprobe_decode_ldmstm},
+	[PROBES_STM] = {.decoder = uprobe_decode_ldmstm}
 };
Signature · 1 line
1.8.4
author
#3 lore raw

PATCH v6 3/7 ARM: kprobes: introduces checker

In reply to Wang Nan

This patch introdces a 'checker' field to decode_action, and calls checkers when instruction decoding. This allows further analysis for specific instructions.

---
 arch/arm/kernel/probes.c | 10 ++++++++++
 arch/arm/kernel/probes.h | 10 ++++++++--
 2 files changed, 18 insertions(+), 2 deletions(-)
arch/arm/kernel/probes.c +10 -0
index ec030b8..6164b4d 100644
@@ -393,6 +393,7 @@ probes_decode_insn(probes_opcode_t insn, struct arch_probes_insn *asi,
 	const struct decode_header *h = (struct decode_header *)table;
 	const struct decode_header *next;
 	bool matched = false;
+	probes_opcode_t origin_insn = insn;
 
 	if (emulate)
 		insn = prepare_emulated_insn(insn, asi, thumb);
@@ -423,17 +424,26 @@ probes_decode_insn(probes_opcode_t insn, struct arch_probes_insn *asi,
 
 		case DECODE_TYPE_CUSTOM: {
 			struct decode_custom *d = (struct decode_custom *)h;
+			probes_check_t *checker = actions[d->decoder.action].checker;
+			if (checker && (checker(origin_insn, asi, h) == INSN_REJECTED))
+				return INSN_REJECTED;
 			return actions[d->decoder.action].decoder(insn, asi, h);
 		}
 
 		case DECODE_TYPE_SIMULATE: {
 			struct decode_simulate *d = (struct decode_simulate *)h;
+			probes_check_t *checker = actions[d->handler.action].checker;
+			if (checker && (checker(origin_insn, asi, h) == INSN_REJECTED))
+				return INSN_REJECTED;
 			asi->insn_handler = actions[d->handler.action].handler;
 			return INSN_GOOD_NO_SLOT;
 		}
 
 		case DECODE_TYPE_EMULATE: {
 			struct decode_emulate *d = (struct decode_emulate *)h;
+			probes_check_t *checker = actions[d->handler.action].checker;
+			if (checker && (checker(origin_insn, asi, h) == INSN_REJECTED))
+				return INSN_REJECTED;
 
 			if (!emulate)
 				return actions[d->handler.action].decoder(insn,
arch/arm/kernel/probes.h +8 -2
index 739c2a2..c56dd3d 100644
@@ -309,9 +309,15 @@ typedef enum probes_insn (probes_custom_decode_t)(probes_opcode_t,
 						  struct arch_probes_insn *,
 						  const struct decode_header *);
 
+typedef enum probes_insn (probes_check_t)(probes_opcode_t,
+					   struct arch_probes_insn *,
+					   const struct decode_header *);
 struct decode_action {
-	probes_insn_handler_t	*handler;
-	probes_custom_decode_t	*decoder;
+	probes_check_t *checker;
+	union {
+		probes_insn_handler_t	*handler;
+		probes_custom_decode_t	*decoder;
+	};
 };
 
 #define DECODE_END			\
Signature · 1 line
1.8.4
author
#4 lore raw

PATCH v6 4/7 ARM: kprobes: collects stack consumption for store instructions

In reply to Wang Nan

This patch use previous introduced checker on store instructions, record stack consumption informations to arch_probes_insn. With such information, kprobe opt can decide how much stack needs to be protected.

---
 arch/arm/include/asm/probes.h   |  1 +
 arch/arm/kernel/kprobes-arm.c   |  8 ++---
 arch/arm/kernel/kprobes-thumb.c |  8 ++---
 arch/arm/kernel/probes-arm.c    | 19 +++++++++++
 arch/arm/kernel/probes-arm.h    |  7 +++++
 arch/arm/kernel/probes-thumb.c  | 70 +++++++++++++++++++++++++++++++++++++++++
 arch/arm/kernel/probes-thumb.h  |  9 ++++++
 arch/arm/kernel/probes.c        | 64 +++++++++++++++++++++++++++++++++++++
 arch/arm/kernel/probes.h        | 13 ++++++++
 arch/arm/kernel/uprobes-arm.c   |  8 ++---
 10 files changed, 195 insertions(+), 12 deletions(-)
arch/arm/include/asm/probes.h +1 -0
index 806cfe6..ccf9af3 100644
@@ -38,6 +38,7 @@ struct arch_probes_insn {
 	probes_check_cc			*insn_check_cc;
 	probes_insn_singlestep_t	*insn_singlestep;
 	probes_insn_fn_t		*insn_fn;
+	int stack_space;
 };
 
 #endif
arch/arm/kernel/kprobes-arm.c +4 -4
index 1094ff1..bb1bec3 100644
@@ -316,11 +316,11 @@ const struct decode_action kprobes_arm_actions[NUM_PROBES_ARM_ACTIONS] = {
 	[PROBES_MUL2] = {.handler = emulate_rd16rn12rm0rs8_rwflags_nopc},
 	[PROBES_SWP] = {.handler = emulate_rd12rn16rm0_rwflags_nopc},
 	[PROBES_LDRD] = {.handler = emulate_ldrdstrd},
-	[PROBES_STRD] = {.handler = emulate_ldrdstrd},
+	[PROBES_STRD] = {.checker = probes_check_arm_store_extra, .handler = emulate_ldrdstrd},
 	[PROBES_LOAD_EXTRA] = {.handler = emulate_ldr},
 	[PROBES_LOAD] = {.handler = emulate_ldr},
-	[PROBES_STORE_EXTRA] = {.handler = emulate_str},
-	[PROBES_STORE] = {.handler = emulate_str},
+	[PROBES_STORE_EXTRA] = {.checker = probes_check_arm_store_extra, .handler = emulate_str},
+	[PROBES_STORE] = {.checker = probes_check_arm_store, .handler = emulate_str},
 	[PROBES_MOV_IP_SP] = {.handler = simulate_mov_ipsp},
 	[PROBES_DATA_PROCESSING_REG] = {
 		.handler = emulate_rd12rn16rm0rs8_rwflags},
@@ -341,5 +341,5 @@ const struct decode_action kprobes_arm_actions[NUM_PROBES_ARM_ACTIONS] = {
 	[PROBES_BITFIELD] = {.handler = emulate_rd12rm0_noflags_nopc},
 	[PROBES_BRANCH] = {.handler = simulate_bbl},
 	[PROBES_LDM] = {.decoder = kprobe_decode_ldmstm},
-	[PROBES_STM] = {.decoder = kprobe_decode_ldmstm},
+	[PROBES_STM] = {.checker = probes_check_stm, .decoder = kprobe_decode_ldmstm},
 };
arch/arm/kernel/kprobes-thumb.c +4 -4
index c6426b6..5da4231 100644
@@ -615,7 +615,7 @@ const struct decode_action kprobes_t16_actions[NUM_PROBES_T16_ACTIONS] = {
 	[PROBES_T16_ADD_SP] = {.handler = t16_simulate_add_sp_imm},
 	[PROBES_T16_CBZ] = {.handler = t16_simulate_cbz},
 	[PROBES_T16_SIGN_EXTEND] = {.handler = t16_emulate_loregs_rwflags},
-	[PROBES_T16_PUSH] = {.decoder = t16_decode_push},
+	[PROBES_T16_PUSH] = {.checker = t16_check_push, .decoder = t16_decode_push},
 	[PROBES_T16_POP] = {.decoder = t16_decode_pop},
 	[PROBES_T16_SEV] = {.handler = probes_emulate_none},
 	[PROBES_T16_WFE] = {.handler = probes_simulate_nop},
@@ -639,9 +639,9 @@ const struct decode_action kprobes_t16_actions[NUM_PROBES_T16_ACTIONS] = {
 
 const struct decode_action kprobes_t32_actions[NUM_PROBES_T32_ACTIONS] = {
 	[PROBES_T32_LDM] = {.decoder = t32_decode_ldmstm},
-	[PROBES_T32_STM] = {.decoder = t32_decode_ldmstm},
+	[PROBES_T32_STM] = {.checker = probes_check_stm, .decoder = t32_decode_ldmstm},
 	[PROBES_T32_LDRD] = {.handler = t32_emulate_ldrdstrd},
-	[PROBES_T32_STRD] = {.handler = t32_emulate_ldrdstrd},
+	[PROBES_T32_STRD] = {.checker = t32_check_strd, .handler = t32_emulate_ldrdstrd},
 	[PROBES_T32_TABLE_BRANCH] = {.handler = t32_simulate_table_branch},
 	[PROBES_T32_TST] = {.handler = t32_emulate_rd8rn16rm0_rwflags},
 	[PROBES_T32_MOV] = {.handler = t32_emulate_rd8rn16rm0_rwflags},
@@ -661,7 +661,7 @@ const struct decode_action kprobes_t32_actions[NUM_PROBES_T32_ACTIONS] = {
 	[PROBES_T32_PLDI] = {.handler = probes_simulate_nop},
 	[PROBES_T32_LDR_LIT] = {.handler = t32_simulate_ldr_literal},
 	[PROBES_T32_LDR] = {.handler = t32_emulate_ldrstr},
-	[PROBES_T32_STR] = {.handler = t32_emulate_ldrstr},
+	[PROBES_T32_STR] = {.checker = t32_check_str, .handler = t32_emulate_ldrstr},
 	[PROBES_T32_SIGN_EXTEND] = {.handler = t32_emulate_rd8rn16rm0_rwflags},
 	[PROBES_T32_MEDIA] = {.handler = t32_emulate_rd8rn16rm0_rwflags},
 	[PROBES_T32_REVERSE] = {.handler = t32_emulate_rd8rn16_noflags},
arch/arm/kernel/probes-arm.c +19 -0
index 148153e..90d2f29 100644
@@ -109,6 +109,25 @@ void __kprobes simulate_mov_ipsp(probes_opcode_t insn,
 	regs->uregs[12] = regs->uregs[13];
 }
 
+enum probes_insn __kprobes probes_check_arm_store(probes_opcode_t insn,
+		       struct arch_probes_insn *asi,
+		       const struct decode_header *h)
+{
+	int imm = insn & 0xfff;
+	check_insn_stack_regs(insn, asi, h, imm);
+	return INSN_GOOD;
+}
+
+enum probes_insn __kprobes probes_check_arm_store_extra(probes_opcode_t insn,
+		       struct arch_probes_insn *asi,
+		       const struct decode_header *h)
+{
+	int imm = ((insn & 0xf00) >> 4) + (insn & 0xf);
+	check_insn_stack_regs(insn, asi, h, imm);
+	return INSN_GOOD;
+}
+
+
 /*
  * For the instruction masking and comparisons in all the "space_*"
  * functions below, Do _not_ rearrange the order of tests unless
arch/arm/kernel/probes-arm.h +7 -0
index 18ffc9a..d0ad9a4 100644
@@ -66,6 +66,13 @@ void __kprobes simulate_mrs(probes_opcode_t opcode,
 void __kprobes simulate_mov_ipsp(probes_opcode_t opcode,
 	struct arch_probes_insn *asi, struct pt_regs *regs);
 
+enum probes_insn __kprobes probes_check_arm_store(probes_opcode_t,
+	struct arch_probes_insn *,
+	const struct decode_header *);
+enum probes_insn __kprobes probes_check_arm_store_extra(probes_opcode_t,
+	struct arch_probes_insn *,
+	const struct decode_header *);
+
 extern const union decode_item probes_decode_arm_table[];
 
 enum probes_insn arm_probes_decode_insn(probes_opcode_t,
arch/arm/kernel/probes-thumb.c +70 -0

76 lines not on this page · raw message

arch/arm/kernel/probes-thumb.h +9 -0
index a9c65c9..f908b12 100644
@@ -100,4 +100,13 @@ enum probes_insn __kprobes
 thumb32_probes_decode_insn(probes_opcode_t insn, struct arch_probes_insn *asi,
 		bool emulate, const struct decode_action *actions);
 
+enum probes_insn __kprobes t32_check_strd(probes_opcode_t insn,
+		struct arch_probes_insn *asi,
+		const struct decode_header *h);
+enum probes_insn __kprobes t32_check_str(probes_opcode_t insn,
+		struct arch_probes_insn *asi,
+		const struct decode_header *h);
+enum probes_insn __kprobes t16_check_push(probes_opcode_t insn,
+		struct arch_probes_insn *asi,
+		const struct decode_header *h);
 #endif
arch/arm/kernel/probes.c +64 -0

79 lines not on this page · raw message

arch/arm/kernel/probes.h +13 -0
index c56dd3d..4c0a04a 100644
@@ -410,4 +410,17 @@ probes_decode_insn(probes_opcode_t insn, struct arch_probes_insn *asi,
 		const union decode_item *table, bool thumb, bool emulate,
 		const struct decode_action *actions);
 
+enum probes_insn __kprobes probes_check_stm(probes_opcode_t,
+	struct arch_probes_insn *,
+	const struct decode_header *);
+
+enum {
+	NOT_STACK_STORE,
+	STACK_REG,
+	STACK_IMM,
+};
+int __kprobes check_insn_stack_regs(probes_opcode_t insn,
+		struct arch_probes_insn *asi,
+		const struct decode_header *h,
+		int imm);
 #endif
arch/arm/kernel/uprobes-arm.c +4 -4
index 0a8caa3..1da524d 100644
@@ -208,11 +208,11 @@ const struct decode_action uprobes_probes_actions[] = {
 	[PROBES_MUL2] = {.handler = probes_simulate_nop},
 	[PROBES_SWP] = {.handler = probes_simulate_nop},
 	[PROBES_LDRD] = {.decoder = decode_pc_ro},
-	[PROBES_STRD] = {.decoder = decode_pc_ro},
+	[PROBES_STRD] = {.checker = probes_check_arm_store_extra, .decoder = decode_pc_ro},
 	[PROBES_LOAD_EXTRA] = {.decoder = decode_pc_ro},
 	[PROBES_LOAD] = {.decoder = decode_ldr},
-	[PROBES_STORE_EXTRA] = {.decoder = decode_pc_ro},
-	[PROBES_STORE] = {.decoder = decode_pc_ro},
+	[PROBES_STORE_EXTRA] = {.checker = probes_check_arm_store_extra, .decoder = decode_pc_ro},
+	[PROBES_STORE] = {.checker = probes_check_arm_store, .decoder = decode_pc_ro},
 	[PROBES_MOV_IP_SP] = {.handler = simulate_mov_ipsp},
 	[PROBES_DATA_PROCESSING_REG] = {
 		.decoder = decode_rd12rn16rm0rs8_rwflags},
@@ -232,5 +232,5 @@ const struct decode_action uprobes_probes_actions[] = {
 	[PROBES_BITFIELD] = {.handler = probes_simulate_nop},
 	[PROBES_BRANCH] = {.handler = simulate_bbl},
 	[PROBES_LDM] = {.decoder = uprobe_decode_ldmstm},
-	[PROBES_STM] = {.decoder = uprobe_decode_ldmstm}
+	[PROBES_STM] = {.checker = probes_check_stm, .decoder = uprobe_decode_ldmstm}
 };
Signature · 1 line
1.8.4
author
#5 lore raw

PATCH v6 5/7 ARM: kprobes: disallow probing stack consuming instructions

In reply to Wang Nan

This patch prohibit probing instructions for which the stack requirement are unable to be determined statically. Some test cases are found not work again after the modification, this patch also removes them.

---
 arch/arm/kernel/kprobes-test-arm.c | 16 ++++++++++------
 arch/arm/kernel/kprobes.c          |  8 ++++++++
 2 files changed, 18 insertions(+), 6 deletions(-)
arch/arm/kernel/kprobes-test-arm.c +10 -6
index 264c064..59f9b25 100644
@@ -476,7 +476,8 @@ void kprobe_arm_test_cases(void)
 	TEST_GROUP("Extra load/store instructions")
 
 	TEST_RPR(  "strh	r",0, VAL1,", [r",1, 48,", -r",2, 24,"]")
-	TEST_RPR(  "streqh	r",14,VAL2,", [r",13,0, ", r",12, 48,"]")
+	TEST_RPR(  "streqh	r",14,VAL2,", [r",11,0, ", r",12, 48,"]")
+	TEST_UNSUPPORTED(  "streqh	r14, [r13, r12]")
 	TEST_RPR(  "strh	r",1, VAL1,", [r",2, 24,", r",3,  48,"]!")
 	TEST_RPR(  "strneh	r",12,VAL2,", [r",11,48,", -r",10,24,"]!")
 	TEST_RPR(  "strh	r",2, VAL1,", [r",3, 24,"], r",4, 48,"")
@@ -565,7 +566,8 @@ void kprobe_arm_test_cases(void)
 
 #if __LINUX_ARM_ARCH__ >= 5
 	TEST_RPR(  "strd	r",0, VAL1,", [r",1, 48,", -r",2,24,"]")
-	TEST_RPR(  "strccd	r",8, VAL2,", [r",13,0, ", r",12,48,"]")
+	TEST_RPR(  "strccd	r",8, VAL2,", [r",11,0, ", r",12,48,"]")
+	TEST_UNSUPPORTED(  "strccd r8, [r13, r12]")
 	TEST_RPR(  "strd	r",4, VAL1,", [r",2, 24,", r",3, 48,"]!")
 	TEST_RPR(  "strcsd	r",12,VAL2,", [r",11,48,", -r",10,24,"]!")
 	TEST_RPR(  "strd	r",2, VAL1,", [r",5, 24,"], r",4,48,"")
@@ -639,13 +641,15 @@ void kprobe_arm_test_cases(void)
 	TEST_RP( "str"byte"	r",2, VAL1,", [r",3, 24,"], #48")		\
 	TEST_RP( "str"byte"	r",10,VAL2,", [r",9, 64,"], #-48")		\
 	TEST_RPR("str"byte"	r",0, VAL1,", [r",1, 48,", -r",2, 24,"]")	\
-	TEST_RPR("str"byte"	r",14,VAL2,", [r",13,0, ", r",12, 48,"]")	\
+	TEST_RPR("str"byte"	r",14,VAL2,", [r",11,0, ", r",12, 48,"]")	\
+	TEST_UNSUPPORTED("str"byte" r14, [r13, r12]")	\
 	TEST_RPR("str"byte"	r",1, VAL1,", [r",2, 24,", r",3,  48,"]!")	\
 	TEST_RPR("str"byte"	r",12,VAL2,", [r",11,48,", -r",10,24,"]!")	\
 	TEST_RPR("str"byte"	r",2, VAL1,", [r",3, 24,"], r",4, 48,"")	\
 	TEST_RPR("str"byte"	r",10,VAL2,", [r",9, 48,"], -r",11,24,"")	\
 	TEST_RPR("str"byte"	r",0, VAL1,", [r",1, 24,", r",2,  32,", asl #1]")\
-	TEST_RPR("str"byte"	r",14,VAL2,", [r",13,0, ", r",12, 32,", lsr #2]")\
+	TEST_RPR("str"byte"	r",14,VAL2,", [r",11,0, ", r",12, 32,", lsr #2]")\
+	TEST_UNSUPPORTED("str"byte"	r14, [r13, r12, lsr #2]")\
 	TEST_RPR("str"byte"	r",1, VAL1,", [r",2, 24,", r",3,  32,", asr #3]!")\
 	TEST_RPR("str"byte"	r",12,VAL2,", [r",11,24,", r",10, 4,", ror #31]!")\
 	TEST_P(  "ldr"byte"	r0, [r",0,  24,", #-2]")			\
@@ -669,12 +673,12 @@ void kprobe_arm_test_cases(void)
 
 	LOAD_STORE("")
 	TEST_P(   "str	pc, [r",0,0,", #15*4]")
-	TEST_R(   "str	pc, [sp, r",2,15*4,"]")
+	TEST_UNSUPPORTED(   "str	pc, [sp, r2]")
 	TEST_BF(  "ldr	pc, [sp, #15*4]")
 	TEST_BF_R("ldr	pc, [sp, r",2,15*4,"]")
 
 	TEST_P(   "str	sp, [r",0,0,", #13*4]")
-	TEST_R(   "str	sp, [sp, r",2,13*4,"]")
+	TEST_UNSUPPORTED(   "str	sp, [sp, r2]")
 	TEST_BF(  "ldr	sp, [sp, #13*4]")
 	TEST_BF_R("ldr	sp, [sp, r",2,13*4,"]")
 
arch/arm/kernel/kprobes.c +8 -0
index 028159c..afbb3e5 100644
@@ -111,6 +111,14 @@ int __kprobes arch_prepare_kprobe(struct kprobe *p)
 		break;
 	}
 
+	/*
+	 * Unable to instrument insn like 'str r0, [sp, +/-r1]'.
+	 * __und_svc protects 64 bytes stack, so instrumenting insn
+	 * likes 'str r0, [sp, #-68]' should be prohibited.
+	 */
+	if ((p->ainsn.stack_space < 0) || (p->ainsn.stack_space > 64))
+		return -EINVAL;
+
 	return 0;
 }
 
Signature · 1 line
1.8.4
author
#6 lore raw

PATCH v6 6/7 kprobes: copy ainsn after alloc aggr kprobe

In reply to Wang Nan

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
author
#7 lore raw

PATCH v6 7/7 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
#8 lore raw
In reply to Wang Nan
Quoted 24 lines · Wang Nan

Previous 5 version of ARM OPTPROBES patches are unable to deal with stack storing instructions correctly. V5 patches disallow optimizing every protential stack store instructions based on pessimistic assumption. Which, as Tixy comments, 'excludes the main use of kprobes'. (https://lkml.org/lkml/2014/8/29/117 )

The main obstacle which prevents us from computing stack requirement is the missing of per-instruction decoder in probes_decode_insn() and its friends. Only part of instructions have their decoders (and not in each case).

In this patch series, I propose 'checker', which allows us define functions for each type of instruction, extract more information. Stack consumption computing is an example. Checker can be further employed to determine whether one instruction is possible to execute directy in optimized kprobe. I'd like to expand current checker framework by chaining checkers together. After that, I believe most of ARM instructions can be executed directly like x86, kprobe performace can be improved.

The first 3 patches introduces checker. After that, patch 4/7 checks stack requirement for probed instructions. Patches 5/7 - 7/7 are similar to patch v5, except:

Quoted 5 lines

1. As Tixy proposed, unoptimized probes are also suffer from stack problem (https://lkml.org/lkml/2014/9/1/548 ). Commit d30a0c8b saves 64 bytes for them, but for instruction use register addressing (like 'str r0, [sp, r1]'), 64 bytes are unsafe. Patch 5/7 prohibit such probing according to stack information collected by checker.

By the way, this sounds like a bugfix rather than an improvement. Is it possible to separate 1/7-5/7 as a bugfix series? I think those should go to 3.18.

Thank you,

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
#9 lore raw
Quoted 31 lines · Masami Hiramatsu
Quoted 24 lines · Wang Nan

Previous 5 version of ARM OPTPROBES patches are unable to deal with stack storing instructions correctly. V5 patches disallow optimizing every protential stack store instructions based on pessimistic assumption. Which, as Tixy comments, 'excludes the main use of kprobes'. (https://lkml.org/lkml/2014/8/29/117 )

The main obstacle which prevents us from computing stack requirement is the missing of per-instruction decoder in probes_decode_insn() and its friends. Only part of instructions have their decoders (and not in each case).

In this patch series, I propose 'checker', which allows us define functions for each type of instruction, extract more information. Stack consumption computing is an example. Checker can be further employed to determine whether one instruction is possible to execute directy in optimized kprobe. I'd like to expand current checker framework by chaining checkers together. After that, I believe most of ARM instructions can be executed directly like x86, kprobe performace can be improved.

The first 3 patches introduces checker. After that, patch 4/7 checks stack requirement for probed instructions. Patches 5/7 - 7/7 are similar to patch v5, except:

Quoted 5 lines

1. As Tixy proposed, unoptimized probes are also suffer from stack problem (https://lkml.org/lkml/2014/9/1/548 ). Commit d30a0c8b saves 64 bytes for them, but for instruction use register addressing (like 'str r0, [sp, r1]'), 64 bytes are unsafe. Patch 5/7 prohibit such probing according to stack information collected by checker.

Quoted 3 lines

By the way, this sounds like a bugfix rather than an improvement. Is it possible to separate 1/7-5/7 as a bugfix series? I think those should go to 3.18.

I believe that problem has existed since kprobes was first implemented on ARM 7 years ago, and the problematic instruction type doesn't appear to get generated by GCC so, in my opinion, I don't think there is any particular urgency to fix this as a bug in the current and, by implication, stable kernels.

Signature · 1 line
Tixy
author
#10 lore raw
Quoted 36 lines · Jon Medhurst (Tixy)
Quoted 31 lines · Masami Hiramatsu
Quoted 24 lines · Wang Nan

Previous 5 version of ARM OPTPROBES patches are unable to deal with stack storing instructions correctly. V5 patches disallow optimizing every protential stack store instructions based on pessimistic assumption. Which, as Tixy comments, 'excludes the main use of kprobes'. (https://lkml.org/lkml/2014/8/29/117 )

The main obstacle which prevents us from computing stack requirement is the missing of per-instruction decoder in probes_decode_insn() and its friends. Only part of instructions have their decoders (and not in each case).

In this patch series, I propose 'checker', which allows us define functions for each type of instruction, extract more information. Stack consumption computing is an example. Checker can be further employed to determine whether one instruction is possible to execute directy in optimized kprobe. I'd like to expand current checker framework by chaining checkers together. After that, I believe most of ARM instructions can be executed directly like x86, kprobe performace can be improved.

The first 3 patches introduces checker. After that, patch 4/7 checks stack requirement for probed instructions. Patches 5/7 - 7/7 are similar to patch v5, except:

Quoted 5 lines

1. As Tixy proposed, unoptimized probes are also suffer from stack problem (https://lkml.org/lkml/2014/9/1/548 ). Commit d30a0c8b saves 64 bytes for them, but for instruction use register addressing (like 'str r0, [sp, r1]'), 64 bytes are unsafe. Patch 5/7 prohibit such probing according to stack information collected by checker.

Quoted 3 lines

By the way, this sounds like a bugfix rather than an improvement. Is it possible to separate 1/7-5/7 as a bugfix series? I think those should go to 3.18.

Quoted 6 lines

I believe that problem has existed since kprobes was first implemented on ARM 7 years ago, and the problematic instruction type doesn't appear to get generated by GCC so, in my opinion, I don't think there is any particular urgency to fix this as a bug in the current and, by implication, stable kernels.

Anyway, I have done the separation. Please refer to:

http://lists.infradead.org/pipermail/linux-arm-kernel/2014-October/297031.html
http://lists.infradead.org/pipermail/linux-arm-kernel/2014-October/297036.html

There is a big change in checker code in the first thread. Please help me review whether checker is acceptable. The next thing I'll do is to create another checker table to check whether the probed insn can be directly executed as in x86_64.

Thanks.

#11 lore raw

On Sat, 2014-10-25 at 17:49 +0800, Wang Nan wrote:
[...]

Quoted 7 lines

Anyway, I have done the separation. Please refer to:

http://lists.infradead.org/pipermail/linux-arm-kernel/2014-October/297031.html
http://lists.infradead.org/pipermail/linux-arm-kernel/2014-October/297036.html

There is a big change in checker code in the first thread. Please help me review whether checker is acceptable.

I had started reviewing the previous version, I'll switch to the new one.

I do prefer the new checker code better, and the only obvious alternative I can think off would be to break down the decoding tables into a lot more special cases of instruction forms, which I isn't as scalable, so I don't like that idea.

However, I wonder if there is scope for the checker functions to recursively call probes_decode_insn rather than decoding instructions in C code. I don't know if that would be clearer and/or smaller or not.

Below is something I've just thrown together to get a feel of how it could look. The decode table could possibly incorporate patterns to cover instructions types that you split up in PATCH 1, e.g. so we might not need separate PROBES_STORE and PROBES_STORE_EXTRA (depends if that ends up making things simpler or not)...

int stack_use_none(probes_opcode_t insn,
struct arch_probes_insn *asi, const struct decode_header *h)

{
	asi->stack_space = 0;
	return INSN_GOOD_NO_SLOT;
}

int stack_use_unknown(probes_opcode_t insn,
struct arch_probes_insn *asi, const struct decode_header *h)

{
	asi->stack_space = -1;
	return INSN_GOOD_NO_SLOT;
}

int stack_use_imm_x0x(probes_opcode_t insn,
struct arch_probes_insn *asi, const struct decode_header *h)

{
	asi->stack_space = ((insn & 0xf00) >> 4) + (insn & 0xf);
	return INSN_GOOD_NO_SLOT;
}

int stack_use_imm_xxx(probes_opcode_t insn,
struct arch_probes_insn *asi, const struct decode_header *h)

{
	asi->stack_space = insn & 0xfff;
	return INSN_GOOD_NO_SLOT;
}

enum {
STACK_USE_NONE,
STACK_USE_UNKNOWN,
STACK_USE_FIXED_X0X,
STACK_USE_FIXED_XXX,
NUM_STACK_USE_TYPES
};

static const union decode_action chk_stack_actions[] = {
	[STACK_USE_NONE] = {.handler = stack_use_none},
	[STACK_USE_UNKNOWN] = {.handler = stack_use_unknown},
	[STACK_USE_FIXED_X0X] = {.handler = stack_use_imm_x0x}
	[STACK_USE_FIXED_XXX] = {.handler = stack_use_imm_xxx}
}

enum probes_insn __kprobes chk_stack_use_arm(probes_opcode_t insn,
struct arch_probes_insn *asi, const struct decode_header *h)

{
	static const union decode_item table[] = {
		/* Register indexed addressing modes with SP as index register (!)*/
		DECODE_OR	(0x0040000d, 0x0000000d),
		/* Register indexed addressing modes with SP as base register */
		DECODE_CUSTOM	(0x004f0000, 0x000d0000, STACK_USE_UNKOWN,
							 REGS(0, 0, 0, 0, 0)),

		/* STR{,B} with immediate pre-indexed addressing mode with SP base address */
		DECODE_CUSTOM	(0x05cf0000, 0x054d0000, STACK_USE_FIXED_XXX,
							 REGS(0, 0, 0, 0, 0)),

		/* STR{H,D} with immediate pre-indexed addressing mode with SP base address */
		DECODE_CUSTOM	(0x05cf0000, 0x014d0000, STACK_USE_FIXED_X0X,
							 REGS(0, 0, 0, 0, 0)),

		... other rules here, possibly including ...
		... REGS values like 'NOSP' to reject certain forms ...

		/* Catch all remaining cases */
		DECODE_CUSTOM	(0, 0, STACK_USE_NONE, REGS(0, 0, 0, 0, 0))
	}

	return probes_decode_insn(insn, asi, table, false, false, chk_stack_actions, NULL);
}

And in the dubious case that anyone wants to copy any of the above, it's

Signature · 1 line
Tixy
#12 lore raw

On Mon, 2014-10-27 at 17:17 +0000, Jon Medhurst (Tixy) wrote:
[...]

Quoted 3 lines

The decode table could possibly incorporate patterns to cover instructions types that you split up in PATCH 1, e.g. so we might not need separate PROBES_STORE and PROBES_STORE_EXTRA (

Sorry, I got that a bit wrong, the first patch only splits loads and stores and doesn't create create any new 'extra' instruction types. However, my comment could still apply to that split between between loads and stores; for many of them, the difference is just a single bit that is possibly cheap or free to test in the checkers.

The reason I am thinking along these lines is that each additional enum value in the instruction types adds an entry into every action and checker table, as well as expanding the decoding tables to detect them. So I just want to make sure that we think these additions result in a net benefit in code size and complexity.

Signature · 1 line
Tixy