Skip to contentSkip to conversation

ARM: kprobes: OPTPROBES and other improvements.

PATCH Superseded Wang Nan 3 participants 18 messages Dec 8, 2014 – Dec 9, 2014

Subsystems (6)
Arm (32-bit) x86 ftrace kprobes +2 more subsystems

v14 of 20 Superseded Latest is v20 →

lore
Share & tools
lore

Wang Nan

author
#0 lore raw

v14 cover ARM: kprobes: OPTPROBES and other improvements.

This is v14 of optprobe related patch series. This series fixes a bug found by Tixy: a race between patch_text() and undef handler.

Previous discussions can be found:

https://lkml.org/lkml/2014/12/4/942
https://lkml.org/lkml/2014/12/4/4
https://lkml.org/lkml/2014/12/1/64
https://lkml.org/lkml/2014/11/22/18
https://lkml.org/lkml/2014/11/21/55
https://lkml.org/lkml/2014/11/18/26
https://lkml.org/lkml/2014/11/19/31
https://lkml.org/lkml/2014/11/18/41
https://lkml.org/lkml/2014/10/25/48
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

Jon Medhurst (1):
ARM: kprobes: Add test cases for stack consuming instructions

Masami Hiramatsu (1):
kprobes: Pass the original kprobe for preparing optimized kprobe

Wang Nan (5):
ARM: probes: move all probe code to dedicate directory
ARM: kprobes: introduces checker
ARM: kprobes: collects stack consumption for store instructions
ARM: kprobes: disallow probing stack consuming instructions
ARM: kprobes: enable OPTPROBES for ARM 32

 arch/arm/Kconfig                                   |   1 +
 arch/arm/Makefile                                  |   1 +
 arch/arm/{kernel => include/asm}/insn.h            |   0
 arch/arm/include/asm/kprobes.h                     |  30 +-
 arch/arm/{kernel => include/asm}/patch.h           |   0
 arch/arm/include/asm/probes.h                      |  13 +
 arch/arm/kernel/Makefile                           |  16 +-
 arch/arm/kernel/entry-armv.S                       |   3 +-
 arch/arm/kernel/ftrace.c                           |   3 +-
 arch/arm/kernel/jump_label.c                       |   5 +-
 arch/arm/kernel/patch.c                            |   3 +-
 arch/arm/probes/Makefile                           |   7 +
 .../{kernel/probes-arm.c => probes/decode-arm.c}   |  12 +-
 .../{kernel/probes-arm.h => probes/decode-arm.h}   |   7 +-
 .../probes-thumb.c => probes/decode-thumb.c}       |  16 +-
 .../probes-thumb.h => probes/decode-thumb.h}       |  10 +-
 arch/arm/{kernel/probes.c => probes/decode.c}      |  74 ++++-
 arch/arm/{kernel/probes.h => probes/decode.h}      |  13 +-
 arch/arm/probes/kprobes/Makefile                   |  12 +
 .../kprobes-arm.c => probes/kprobes/actions-arm.c} |   9 +-
 .../kprobes/actions-common.c}                      |   4 +-
 .../kprobes/actions-thumb.c}                       |  10 +-
 arch/arm/probes/kprobes/checkers-arm.c             |  99 +++++++
 arch/arm/probes/kprobes/checkers-common.c          | 101 +++++++
 arch/arm/probes/kprobes/checkers-thumb.c           | 110 +++++++
 arch/arm/probes/kprobes/checkers.h                 |  54 ++++
 .../{kernel/kprobes.c => probes/kprobes/core.c}    |  23 +-
 .../{kernel/kprobes.h => probes/kprobes/core.h}    |  10 +-
 arch/arm/probes/kprobes/opt-arm.c                  | 322 +++++++++++++++++++++
 .../kprobes/test-arm.c}                            |  31 +-
 .../kprobes-test.c => probes/kprobes/test-core.c}  |   8 +-
 .../kprobes-test.h => probes/kprobes/test-core.h}  |   2 +-
 .../kprobes/test-thumb.c}                          |  16 +-
 arch/arm/probes/uprobes/Makefile                   |   2 +
 .../uprobes-arm.c => probes/uprobes/actions-arm.c} |   6 +-
 .../{kernel/uprobes.c => probes/uprobes/core.c}    |   8 +-
 .../{kernel/uprobes.h => probes/uprobes/core.h}    |   0
 arch/x86/kernel/kprobes/opt.c                      |   3 +-
 include/linux/kprobes.h                            |   3 +-
 kernel/kprobes.c                                   |   4 +-
 samples/kprobes/kprobe_example.c                   |   2 +-
 41 files changed, 961 insertions(+), 92 deletions(-)
 rename arch/arm/{kernel => include/asm}/insn.h (100%)
 rename arch/arm/{kernel => include/asm}/patch.h (100%)
 create mode 100644 arch/arm/probes/Makefile
 rename arch/arm/{kernel/probes-arm.c => probes/decode-arm.c} (99%)
 rename arch/arm/{kernel/probes-arm.h => probes/decode-arm.h} (93%)
 rename arch/arm/{kernel/probes-thumb.c => probes/decode-thumb.c} (98%)
 rename arch/arm/{kernel/probes-thumb.h => probes/decode-thumb.h} (90%)
 rename arch/arm/{kernel/probes.c => probes/decode.c} (85%)
 rename arch/arm/{kernel/probes.h => probes/decode.h} (97%)
 create mode 100644 arch/arm/probes/kprobes/Makefile
 rename arch/arm/{kernel/kprobes-arm.c => probes/kprobes/actions-arm.c} (98%)
 rename arch/arm/{kernel/kprobes-common.c => probes/kprobes/actions-common.c} (98%)
 rename arch/arm/{kernel/kprobes-thumb.c => probes/kprobes/actions-thumb.c} (98%)
 create mode 100644 arch/arm/probes/kprobes/checkers-arm.c
 create mode 100644 arch/arm/probes/kprobes/checkers-common.c
 create mode 100644 arch/arm/probes/kprobes/checkers-thumb.c
 create mode 100644 arch/arm/probes/kprobes/checkers.h
 rename arch/arm/{kernel/kprobes.c => probes/kprobes/core.c} (96%)
 rename arch/arm/{kernel/kprobes.h => probes/kprobes/core.h} (83%)
 create mode 100644 arch/arm/probes/kprobes/opt-arm.c
 rename arch/arm/{kernel/kprobes-test-arm.c => probes/kprobes/test-arm.c} (97%)
 rename arch/arm/{kernel/kprobes-test.c => probes/kprobes/test-core.c} (99%)
 rename arch/arm/{kernel/kprobes-test.h => probes/kprobes/test-core.h} (99%)
 rename arch/arm/{kernel/kprobes-test-thumb.c => probes/kprobes/test-thumb.c} (98%)
 create mode 100644 arch/arm/probes/uprobes/Makefile
 rename arch/arm/{kernel/uprobes-arm.c => probes/uprobes/actions-arm.c} (98%)
 rename arch/arm/{kernel/uprobes.c => probes/uprobes/core.c} (97%)
 rename arch/arm/{kernel/uprobes.h => probes/uprobes/core.h} (100%)
Signature · 1 line
1.8.4
author
#1 lore raw

PATCH v14 1/7 ARM: probes: move all probe code to dedicate directory

In discussion on LKML (https://lkml.org/lkml/2014/11/28/158), Russell King suggests to move all probe related code to arch/arm/probes. This patch does the work. Due to dependency on 'arch/arm/kernel/patch.h', this patch also moves patch.h to 'arch/arm/include/asm/patch.h', and related '#include' directives are also midified to '#include <asm/patch.h>'.

Following is an overview of this patch:

 ./arch/arm/kernel/               ./arch/arm/probes/
 |-- Makefile                     |-- Makefile
 |-- probes-arm.c          ==>    |-- decode-arm.c
 |-- probes-arm.h          ==>    |-- decode-arm.h
 |-- probes-thumb.c        ==>    |-- decode-thumb.c
 |-- probes-thumb.h        ==>    |-- decode-thumb.h
 |-- probes.c              ==>    |-- decode.c
 |-- probes.h              ==>    |-- decode.h
 |                                |-- kprobes
 |                                |   |-- Makefile
 |-- kprobes-arm.c         ==>    |   |-- actions-arm.c
 |-- kprobes-common.c      ==>    |   |-- actions-common.c
 |-- kprobes-thumb.c       ==>    |   |-- actions-thumb.c
 |-- kprobes.c             ==>    |   |-- core.c
 |-- kprobes.h             ==>    |   |-- core.h
 |-- kprobes-test-arm.c    ==>    |   |-- test-arm.c
 |-- kprobes-test.c        ==>    |   |-- test-core.c
 |-- kprobes-test.h        ==>    |   |-- test-core.h
 |-- kprobes-test-thumb.c  ==>    |   `-- test-thumb.c
 |                                `-- uprobes
 |                                    |-- Makefile
 |-- uprobes-arm.c         ==>        |-- actions-arm.c
 |-- uprobes.c             ==>        |-- core.c
 |-- uprobes.h             ==>        `-- core.h
 |
 `-- patch.h               ==>    arch/arm/include/asm/patch.h

---
v1 -> v2:
- Rename source files to describe their functions.
- Add Makefiles in kprobes and uprobes directories.

---
 arch/arm/Makefile                                        |  1 +
 arch/arm/{kernel => include/asm}/patch.h                 |  0
 arch/arm/kernel/Makefile                                 | 16 ++--------------
 arch/arm/kernel/jump_label.c                             |  2 +-
 arch/arm/kernel/patch.c                                  |  3 +--
 arch/arm/probes/Makefile                                 |  7 +++++++
 arch/arm/{kernel/probes-arm.c => probes/decode-arm.c}    |  7 ++++---
 arch/arm/{kernel/probes-arm.h => probes/decode-arm.h}    |  4 +++-
 .../arm/{kernel/probes-thumb.c => probes/decode-thumb.c} |  6 +++---
 .../arm/{kernel/probes-thumb.h => probes/decode-thumb.h} |  4 +++-
 arch/arm/{kernel/probes.c => probes/decode.c}            |  4 ++--
 arch/arm/{kernel/probes.h => probes/decode.h}            |  2 +-
 arch/arm/probes/kprobes/Makefile                         | 11 +++++++++++
 .../kprobes-arm.c => probes/kprobes/actions-arm.c}       |  6 +++---
 .../kprobes-common.c => probes/kprobes/actions-common.c} |  4 ++--
 .../kprobes-thumb.c => probes/kprobes/actions-thumb.c}   |  6 +++---
 arch/arm/{kernel/kprobes.c => probes/kprobes/core.c}     |  8 ++++----
 arch/arm/{kernel/kprobes.h => probes/kprobes/core.h}     |  3 ++-
 .../kprobes-test-arm.c => probes/kprobes/test-arm.c}     |  2 +-
 .../kprobes-test.c => probes/kprobes/test-core.c}        |  8 ++++----
 .../kprobes-test.h => probes/kprobes/test-core.h}        |  2 +-
 .../kprobes-test-thumb.c => probes/kprobes/test-thumb.c} |  4 ++--
 arch/arm/probes/uprobes/Makefile                         |  2 ++
 .../uprobes-arm.c => probes/uprobes/actions-arm.c}       |  6 +++---
 arch/arm/{kernel/uprobes.c => probes/uprobes/core.c}     |  6 +++---
 arch/arm/{kernel/uprobes.h => probes/uprobes/core.h}     |  0
 26 files changed, 69 insertions(+), 55 deletions(-)
 rename arch/arm/{kernel => include/asm}/patch.h (100%)
 create mode 100644 arch/arm/probes/Makefile
 rename arch/arm/{kernel/probes-arm.c => probes/decode-arm.c} (99%)
 rename arch/arm/{kernel/probes-arm.h => probes/decode-arm.h} (97%)
 rename arch/arm/{kernel/probes-thumb.c => probes/decode-thumb.c} (99%)
 rename arch/arm/{kernel/probes-thumb.h => probes/decode-thumb.h} (97%)
 rename arch/arm/{kernel/probes.c => probes/decode.c} (99%)
 rename arch/arm/{kernel/probes.h => probes/decode.h} (99%)
 create mode 100644 arch/arm/probes/kprobes/Makefile
 rename arch/arm/{kernel/kprobes-arm.c => probes/kprobes/actions-arm.c} (99%)
 rename arch/arm/{kernel/kprobes-common.c => probes/kprobes/actions-common.c} (98%)
 rename arch/arm/{kernel/kprobes-thumb.c => probes/kprobes/actions-thumb.c} (99%)
 rename arch/arm/{kernel/kprobes.c => probes/kprobes/core.c} (99%)
 rename arch/arm/{kernel/kprobes.h => probes/kprobes/core.h} (96%)
 rename arch/arm/{kernel/kprobes-test-arm.c => probes/kprobes/test-arm.c} (99%)
 rename arch/arm/{kernel/kprobes-test.c => probes/kprobes/test-core.c} (99%)
 rename arch/arm/{kernel/kprobes-test.h => probes/kprobes/test-core.h} (99%)
 rename arch/arm/{kernel/kprobes-test-thumb.c => probes/kprobes/test-thumb.c} (99%)
 create mode 100644 arch/arm/probes/uprobes/Makefile
 rename arch/arm/{kernel/uprobes-arm.c => probes/uprobes/actions-arm.c} (98%)
 rename arch/arm/{kernel/uprobes.c => probes/uprobes/core.c} (98%)
 rename arch/arm/{kernel/uprobes.h => probes/uprobes/core.h} (100%)
arch/arm/Makefile +1 -0
index 034a949..a57d9bb 100644
@@ -266,6 +266,7 @@ core-$(CONFIG_KVM_ARM_HOST) 	+= arch/arm/kvm/
 
 # If we have a machine-specific directory, then include it in the build.
 core-y				+= arch/arm/kernel/ arch/arm/mm/ arch/arm/common/
+core-y				+= arch/arm/probes/
 core-y				+= arch/arm/net/
 core-y				+= arch/arm/crypto/
 core-y				+= arch/arm/firmware/
arch/arm/include/asm/patch.h renamed from arch/arm/kernel/patch.h +0 -0
similarity index 100%
rename from arch/arm/kernel/patch.h
rename to arch/arm/include/asm/patch.h
arch/arm/kernel/Makefile +2 -14
index 38ddd9f..40d3e00 100644
@@ -51,20 +51,8 @@ obj-$(CONFIG_DYNAMIC_FTRACE)	+= ftrace.o insn.o
 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
-ifdef CONFIG_THUMB2_KERNEL
-obj-$(CONFIG_KPROBES)		+= kprobes-thumb.o probes-thumb.o
-else
-obj-$(CONFIG_KPROBES)		+= kprobes-arm.o probes-arm.o
-endif
-obj-$(CONFIG_ARM_KPROBES_TEST)	+= test-kprobes.o
-test-kprobes-objs		:= kprobes-test.o
-ifdef CONFIG_THUMB2_KERNEL
-test-kprobes-objs		+= kprobes-test-thumb.o
-else
-test-kprobes-objs		+= kprobes-test-arm.o
-endif
+# Main staffs in KPROBES are in arch/arm/probes/ .
+obj-$(CONFIG_KPROBES)		+= patch.o
 obj-$(CONFIG_OABI_COMPAT)	+= sys_oabi-compat.o
 obj-$(CONFIG_ARM_THUMBEE)	+= thumbee.o
 obj-$(CONFIG_KGDB)		+= kgdb.o
arch/arm/kernel/jump_label.c +1 -1
index 4ce4f78..c6c73ed 100644
@@ -1,8 +1,8 @@
 #include <linux/kernel.h>
 #include <linux/jump_label.h>
+#include <asm/patch.h>
 
 #include "insn.h"
-#include "patch.h"
 
 #ifdef HAVE_JUMP_LABEL
 
arch/arm/kernel/patch.c +1 -2
index 07314af..08bcfa1 100644
@@ -5,8 +5,7 @@
 #include <asm/cacheflush.h>
 #include <asm/smp_plat.h>
 #include <asm/opcodes.h>
-
-#include "patch.h"
+#include <asm/patch.h>
 
 struct patch {
 	void *addr;
arch/arm/probes/Makefile new file +7 -0
new file mode 100644
index 0000000..aa1f859
@@ -0,0 +1,7 @@
+obj-$(CONFIG_UPROBES)		+= decode.o decode-arm.o uprobes/
+obj-$(CONFIG_KPROBES)		+= decode.o kprobes/
+ifdef CONFIG_THUMB2_KERNEL
+obj-$(CONFIG_KPROBES)		+= decode-thumb.o
+else
+obj-$(CONFIG_KPROBES)		+= decode-arm.o
+endif
arch/arm/probes/decode-arm.c renamed from arch/arm/kernel/probes-arm.c +4 -3
similarity index 99%
rename from arch/arm/kernel/probes-arm.c
rename to arch/arm/probes/decode-arm.c
index 8eaef81..e39cc75 100644
@@ -1,5 +1,6 @@
 /*
- * arch/arm/kernel/probes-arm.c
+ *
+ * arch/arm/probes/decode-arm.c
  *
  * Some code moved here from arch/arm/kernel/kprobes-arm.c
  *
@@ -20,8 +21,8 @@
 #include <linux/stddef.h>
 #include <linux/ptrace.h>
 
-#include "probes.h"
-#include "probes-arm.h"
+#include "decode.h"
+#include "decode-arm.h"
 
 #define sign_extend(x, signbit) ((x) | (0 - ((x) & (1 << (signbit)))))
 
arch/arm/probes/decode-arm.h renamed from arch/arm/kernel/probes-arm.h +3 -1
similarity index 97%
rename from arch/arm/kernel/probes-arm.h
rename to arch/arm/probes/decode-arm.h
index ace6572..9c56b40 100644
@@ -1,5 +1,5 @@
 /*
- * arch/arm/kernel/probes-arm.h
+ * arch/arm/probes/decode-arm.h
  *
  * Copyright 2013 Linaro Ltd.
  * Written by: David A. Long
@@ -15,6 +15,8 @@
 #ifndef _ARM_KERNEL_PROBES_ARM_H
 #define  _ARM_KERNEL_PROBES_ARM_H
 
+#include "decode.h"
+
 enum probes_arm_action {
 	PROBES_EMULATE_NONE,
 	PROBES_SIMULATE_NOP,
arch/arm/probes/decode-thumb.c renamed from arch/arm/kernel/probes-thumb.c +3 -3
similarity index 99%
rename from arch/arm/kernel/probes-thumb.c
rename to arch/arm/probes/decode-thumb.c
index 4131351..2f0453a 100644
@@ -1,5 +1,5 @@
 /*
- * arch/arm/kernel/probes-thumb.c
+ * arch/arm/probes/decode-thumb.c
  *
  * Copyright (C) 2011 Jon Medhurst <[email protected]>.
  *
@@ -12,8 +12,8 @@
 #include <linux/kernel.h>
 #include <linux/module.h>
 
-#include "probes.h"
-#include "probes-thumb.h"
+#include "decode.h"
+#include "decode-thumb.h"
 
 
 static const union decode_item t32_table_1110_100x_x0xx[] = {
arch/arm/probes/decode-thumb.h renamed from arch/arm/kernel/probes-thumb.h +3 -1
similarity index 97%
rename from arch/arm/kernel/probes-thumb.h
rename to arch/arm/probes/decode-thumb.h
index 7c6f6eb..039013c 100644
@@ -1,5 +1,5 @@
 /*
- * arch/arm/kernel/probes-thumb.h
+ * arch/arm/probes/decode-thumb.h
  *
  * Copyright 2013 Linaro Ltd.
  * Written by: David A. Long
@@ -15,6 +15,8 @@
 #ifndef _ARM_KERNEL_PROBES_THUMB_H
 #define  _ARM_KERNEL_PROBES_THUMB_H
 
+#include "decode.h"
+
 /*
  * True if current instruction is in an IT block.
  */
arch/arm/probes/decode.c renamed from arch/arm/kernel/probes.c +2 -2
similarity index 99%
rename from arch/arm/kernel/probes.c
rename to arch/arm/probes/decode.c
index a8ab540..3b05d57 100644
@@ -1,5 +1,5 @@
 /*
- * arch/arm/kernel/probes.c
+ * arch/arm/probes/decode.c
  *
  * Copyright (C) 2011 Jon Medhurst <[email protected]>.
  *
@@ -17,7 +17,7 @@
 #include <asm/ptrace.h>
 #include <linux/bug.h>
 
-#include "probes.h"
+#include "decode.h"
 
 
 #ifndef find_str_pc_offset
arch/arm/probes/decode.h renamed from arch/arm/kernel/probes.h +1 -1
similarity index 99%
rename from arch/arm/kernel/probes.h
rename to arch/arm/probes/decode.h
index dba9f24..1d0b531 100644
@@ -1,5 +1,5 @@
 /*
- * arch/arm/kernel/probes.h
+ * arch/arm/probes/decode.h
  *
  * Copyright (C) 2011 Jon Medhurst <[email protected]>.
  *
arch/arm/probes/kprobes/Makefile new file +11 -0
new file mode 100644
index 0000000..eb38a42
@@ -0,0 +1,11 @@
+obj-$(CONFIG_KPROBES)		+= core.o actions-common.o
+obj-$(CONFIG_ARM_KPROBES_TEST)	+= test-kprobes.o
+test-kprobes-objs		:= test-core.o
+
+ifdef CONFIG_THUMB2_KERNEL
+obj-$(CONFIG_KPROBES)		+= actions-thumb.o
+test-kprobes-objs		+= test-thumb.o
+else
+obj-$(CONFIG_KPROBES)		+= actions-arm.o
+test-kprobes-objs		+= test-arm.o
+endif
arch/arm/probes/kprobes/actions-arm.c renamed from arch/arm/kernel/kprobes-arm.c +3 -3
similarity index 99%
rename from arch/arm/kernel/kprobes-arm.c
rename to arch/arm/probes/kprobes/actions-arm.c
index ac300c6..8797879 100644
@@ -1,5 +1,5 @@
 /*
- * arch/arm/kernel/kprobes-decode.c
+ * arch/arm/probes/kprobes/actions-arm.c
  *
  * Copyright (C) 2006, 2007 Motorola Inc.
  *
@@ -62,8 +62,8 @@
 #include <linux/kprobes.h>
 #include <linux/ptrace.h>
 
-#include "kprobes.h"
-#include "probes-arm.h"
+#include "../decode-arm.h"
+#include "core.h"
 
 #if  __LINUX_ARM_ARCH__ >= 6
 #define BLX(reg)	"blx	"reg"		\n\t"
arch/arm/probes/kprobes/actions-common.c renamed from arch/arm/kernel/kprobes-common.c +2 -2
similarity index 98%
rename from arch/arm/kernel/kprobes-common.c
rename to arch/arm/probes/kprobes/actions-common.c
index 0bf5d64..bd20a71 100644
@@ -1,5 +1,5 @@
 /*
- * arch/arm/kernel/kprobes-common.c
+ * arch/arm/probes/kprobes/actions-common.c
  *
  * Copyright (C) 2011 Jon Medhurst <[email protected]>.
  *
@@ -15,7 +15,7 @@
 #include <linux/kprobes.h>
 #include <asm/opcodes.h>
 
-#include "kprobes.h"
+#include "core.h"
 
 
 static void __kprobes simulate_ldm1stm1(probes_opcode_t insn,
arch/arm/probes/kprobes/actions-thumb.c renamed from arch/arm/kernel/kprobes-thumb.c +3 -3
similarity index 99%
rename from arch/arm/kernel/kprobes-thumb.c
rename to arch/arm/probes/kprobes/actions-thumb.c
index 9495d7f..6c4e60b 100644
@@ -1,5 +1,5 @@
 /*
- * arch/arm/kernel/kprobes-thumb.c
+ * arch/arm/probes/kprobes/actions-thumb.c
  *
  * Copyright (C) 2011 Jon Medhurst <[email protected]>.
  *
@@ -13,8 +13,8 @@
 #include <linux/ptrace.h>
 #include <linux/kprobes.h>
 
-#include "kprobes.h"
-#include "probes-thumb.h"
+#include "../decode-thumb.h"
+#include "core.h"
 
 /* These emulation encodings are functionally equivalent... */
 #define t32_emulate_rd8rn16rm0ra12_noflags \
arch/arm/probes/kprobes/core.c renamed from arch/arm/kernel/kprobes.c +4 -4
similarity index 99%
rename from arch/arm/kernel/kprobes.c
rename to arch/arm/probes/kprobes/core.c
index 6d64420..701f49d 100644
@@ -30,11 +30,11 @@
 #include <asm/cacheflush.h>
 #include <linux/percpu.h>
 #include <linux/bug.h>
+#include <asm/patch.h>
 
-#include "kprobes.h"
-#include "probes-arm.h"
-#include "probes-thumb.h"
-#include "patch.h"
+#include "../decode-arm.h"
+#include "../decode-thumb.h"
+#include "core.h"
 
 #define MIN_STACK_SIZE(addr) 				\
 	min((unsigned long)MAX_STACK_SIZE,		\
arch/arm/probes/kprobes/core.h renamed from arch/arm/kernel/kprobes.h +2 -1
similarity index 96%
rename from arch/arm/kernel/kprobes.h
rename to arch/arm/probes/kprobes/core.h
index 9a2712e..2e1e5a3 100644
@@ -19,7 +19,8 @@
 #ifndef _ARM_KERNEL_KPROBES_H
 #define _ARM_KERNEL_KPROBES_H
 
-#include "probes.h"
+#include <asm/kprobes.h>
+#include "../decode.h"
 
 /*
  * These undefined instructions must be unique and
arch/arm/probes/kprobes/test-arm.c renamed from arch/arm/kernel/kprobes-test-arm.c +1 -1
similarity index 99%
rename from arch/arm/kernel/kprobes-test-arm.c
rename to arch/arm/probes/kprobes/test-arm.c
index cb14242..d9a1255 100644
@@ -13,7 +13,7 @@
 #include <asm/system_info.h>
 #include <asm/opcodes.h>
 
-#include "kprobes-test.h"
+#include "test-core.h"
 
 
 #define TEST_ISA "32"
arch/arm/probes/kprobes/test-core.c renamed from arch/arm/kernel/kprobes-test.c +4 -4
similarity index 99%
rename from arch/arm/kernel/kprobes-test.c
rename to arch/arm/probes/kprobes/test-core.c
index b206d77..7ab633d 100644
@@ -209,10 +209,10 @@
 #include <linux/bug.h>
 #include <asm/opcodes.h>
 
-#include "kprobes.h"
-#include "probes-arm.h"
-#include "probes-thumb.h"
-#include "kprobes-test.h"
+#include "core.h"
+#include "test-core.h"
+#include "../decode-arm.h"
+#include "../decode-thumb.h"
 
 
 #define BENCHMARKING	1
arch/arm/probes/kprobes/test-core.h renamed from arch/arm/kernel/kprobes-test.h +1 -1
similarity index 99%
rename from arch/arm/kernel/kprobes-test.h
rename to arch/arm/probes/kprobes/test-core.h
index 4430990..9991754 100644
@@ -1,5 +1,5 @@
 /*
- * arch/arm/kernel/kprobes-test.h
+ * arch/arm/probes/kprobes/test-core.h
  *
  * Copyright (C) 2011 Jon Medhurst <[email protected]>.
  *
arch/arm/probes/kprobes/test-thumb.c renamed from arch/arm/kernel/kprobes-test-thumb.c +2 -2
similarity index 99%
rename from arch/arm/kernel/kprobes-test-thumb.c
rename to arch/arm/probes/kprobes/test-thumb.c
index 844dd10..6c6e9a9 100644
@@ -1,5 +1,5 @@
 /*
- * arch/arm/kernel/kprobes-test-thumb.c
+ * arch/arm/probes/kprobes/test-thumb.c
  *
  * Copyright (C) 2011 Jon Medhurst <[email protected]>.
  *
@@ -12,7 +12,7 @@
 #include <linux/module.h>
 #include <asm/opcodes.h>
 
-#include "kprobes-test.h"
+#include "test-core.h"
 
 
 #define TEST_ISA "16"
arch/arm/probes/uprobes/Makefile new file +2 -0
new file mode 100644
index 0000000..6c4c735
@@ -0,0 +1,2 @@
+obj-$(CONFIG_UPROBES)		+= core.o actions-arm.o
+
arch/arm/probes/uprobes/actions-arm.c renamed from arch/arm/kernel/uprobes-arm.c +3 -3
similarity index 98%
rename from arch/arm/kernel/uprobes-arm.c
rename to arch/arm/probes/uprobes/actions-arm.c
index d3b655f..1dd4916 100644
@@ -13,9 +13,9 @@
 #include <linux/uprobes.h>
 #include <linux/module.h>
 
-#include "probes.h"
-#include "probes-arm.h"
-#include "uprobes.h"
+#include "../decode.h"
+#include "../decode-arm.h"
+#include "core.h"
 
 static int uprobes_substitute_pc(unsigned long *pinsn, u32 oregs)
 {
arch/arm/probes/uprobes/core.c renamed from arch/arm/kernel/uprobes.c +3 -3
similarity index 98%
rename from arch/arm/kernel/uprobes.c
rename to arch/arm/probes/uprobes/core.c
index 56adf9c..b2954f6 100644
@@ -17,9 +17,9 @@
 #include <asm/opcodes.h>
 #include <asm/traps.h>
 
-#include "probes.h"
-#include "probes-arm.h"
-#include "uprobes.h"
+#include "../decode.h"
+#include "../decode-arm.h"
+#include "core.h"
 
 #define UPROBE_TRAP_NR	UINT_MAX
 
arch/arm/probes/uprobes/core.h renamed from arch/arm/kernel/uprobes.h +0 -0
similarity index 100%
rename from arch/arm/kernel/uprobes.h
rename to arch/arm/probes/uprobes/core.h
Signature · 1 line
1.8.4
author
#2 lore raw

PATCH v14 2/7 ARM: kprobes: introduces checker

In reply to Wang Nan

This patch introdces 'checker' to decoding phase, and calls checkers when instruction decoding. This allows further decoding for specific instructions. This patch introduces a stub call of checkers in kprobe arch_prepare_kprobe() as an example and for further expansion.

---
v1 -> v2:
- kprobe checker stubs are introduced in this patch.

v2 -> v3:
- Code cleanups following Masami Hiramatsu and Tixy's advises.
- Commit message improvements.

v3 -> v4:
- Move to arch/arm/probes.

---
 arch/arm/probes/decode-arm.c            |  5 +--
 arch/arm/probes/decode-arm.h            |  3 +-
 arch/arm/probes/decode-thumb.c          | 10 +++---
 arch/arm/probes/decode-thumb.h          |  6 ++--
 arch/arm/probes/decode.c                | 60 +++++++++++++++++++++++++++++----
 arch/arm/probes/decode.h                | 11 +++++-
 arch/arm/probes/kprobes/actions-arm.c   |  2 ++
 arch/arm/probes/kprobes/actions-thumb.c |  3 ++
 arch/arm/probes/kprobes/core.c          |  6 +++-
 arch/arm/probes/kprobes/core.h          |  7 ++--
 arch/arm/probes/uprobes/core.c          |  2 +-
 11 files changed, 95 insertions(+), 20 deletions(-)
arch/arm/probes/decode-arm.c +3 -2
index e39cc75..f46d8fc 100644
@@ -726,10 +726,11 @@ 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 union decode_action *actions,
+		       const struct decode_checker *checkers[])
 {
 	asi->insn_singlestep = arm_singlestep;
 	asi->insn_check_cc = probes_condition_checks[insn>>28];
 	return probes_decode_insn(insn, asi, probes_decode_arm_table, false,
-				  emulate, actions);
+				  emulate, actions, checkers);
 }
arch/arm/probes/decode-arm.h +2 -1
index 9c56b40..a7b0398 100644
@@ -70,6 +70,7 @@ 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 union decode_action *actions,
+		const struct decode_checker *checkers[]);
 
 #endif
arch/arm/probes/decode-thumb.c +6 -4
index 2f0453a..985e7dd 100644
@@ -863,20 +863,22 @@ 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 union decode_action *actions,
+			   const struct decode_checker *checkers[])
 {
 	asi->insn_singlestep = thumb16_singlestep;
 	asi->insn_check_cc = thumb_check_cc;
 	return probes_decode_insn(insn, asi, probes_decode_thumb16_table, true,
-				  emulate, actions);
+				  emulate, actions, checkers);
 }
 
 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 union decode_action *actions,
+			   const struct decode_checker *checkers[])
 {
 	asi->insn_singlestep = thumb32_singlestep;
 	asi->insn_check_cc = thumb_check_cc;
 	return probes_decode_insn(insn, asi, probes_decode_thumb32_table, true,
-				  emulate, actions);
+				  emulate, actions, checkers);
 }
arch/arm/probes/decode-thumb.h +4 -2
index 039013c..8457add 100644
@@ -91,9 +91,11 @@ 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 union decode_action *actions,
+		const struct decode_checker *checkers[]);
 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 union decode_action *actions,
+		const struct decode_checker *checkers[]);
 
 #endif
arch/arm/probes/decode.c +54 -6

95 lines not on this page · raw message

arch/arm/probes/decode.h +10 -1
index 1d0b531..f9b08ba 100644
@@ -314,6 +314,14 @@ union decode_action {
 	probes_custom_decode_t	*decoder;
 };
 
+typedef enum probes_insn (probes_check_t)(probes_opcode_t,
+					   struct arch_probes_insn *,
+					   const struct decode_header *);
+
+struct decode_checker {
+	probes_check_t	*checker;
+};
+
 #define DECODE_END			\
 	{.bits = DECODE_TYPE_END}
 
@@ -402,6 +410,7 @@ 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 union decode_action *actions,
+		const struct decode_checker **checkers);
 
 #endif
arch/arm/probes/kprobes/actions-arm.c +2 -0
index 8797879..3c3afa4 100644
@@ -341,3 +341,5 @@ const union decode_action kprobes_arm_actions[NUM_PROBES_ARM_ACTIONS] = {
 	[PROBES_BRANCH] = {.handler = simulate_bbl},
 	[PROBES_LDMSTM] = {.decoder = kprobe_decode_ldmstm}
 };
+
+const struct decode_checker *kprobes_arm_checkers[] = {NULL};
arch/arm/probes/kprobes/actions-thumb.c +3 -0
index 6c4e60b..2796121 100644
@@ -664,3 +664,6 @@ const union decode_action kprobes_t32_actions[NUM_PROBES_T32_ACTIONS] = {
 	[PROBES_T32_MUL_ADD_LONG] = {
 		.handler = t32_emulate_rdlo12rdhi8rn16rm0_noflags},
 };
+
+const struct decode_checker *kprobes_t32_checkers[] = {NULL};
+const struct decode_checker *kprobes_t16_checkers[] = {NULL};
arch/arm/probes/kprobes/core.c +5 -1
index 701f49d..74f3dc3 100644
@@ -61,6 +61,7 @@ int __kprobes arch_prepare_kprobe(struct kprobe *p)
 	kprobe_decode_insn_t *decode_insn;
 	const union decode_action *actions;
 	int is;
+	const struct decode_checker **checkers;
 
 	if (in_exception_text(addr))
 		return -EINVAL;
@@ -74,9 +75,11 @@ int __kprobes arch_prepare_kprobe(struct kprobe *p)
 		insn = __opcode_thumb32_compose(insn, inst2);
 		decode_insn = thumb32_probes_decode_insn;
 		actions = kprobes_t32_actions;
+		checkers = kprobes_t32_checkers;
 	} else {
 		decode_insn = thumb16_probes_decode_insn;
 		actions = kprobes_t16_actions;
+		checkers = kprobes_t16_checkers;
 	}
 #else /* !CONFIG_THUMB2_KERNEL */
 	thumb = false;
@@ -85,12 +88,13 @@ int __kprobes arch_prepare_kprobe(struct kprobe *p)
 	insn = __mem_to_opcode_arm(*p->addr);
 	decode_insn = arm_probes_decode_insn;
 	actions = kprobes_arm_actions;
+	checkers = kprobes_arm_checkers;
 #endif
 
 	p->opcode = insn;
 	p->ainsn.insn = tmp_insn;
 
-	switch ((*decode_insn)(insn, &p->ainsn, true, actions)) {
+	switch ((*decode_insn)(insn, &p->ainsn, true, actions, checkers)) {
 	case INSN_REJECTED:	/* not supported */
 		return -EINVAL;
 
arch/arm/probes/kprobes/core.h +5 -2
index 2e1e5a3..f88c79f 100644
@@ -37,16 +37,19 @@ 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 union decode_action *,
+						const struct decode_checker *[*]);
 
 #ifdef CONFIG_THUMB2_KERNEL
 
 extern const union decode_action kprobes_t32_actions[];
 extern const union decode_action kprobes_t16_actions[];
-
+extern const struct decode_checker *kprobes_t32_checkers[];
+extern const struct decode_checker *kprobes_t16_checkers[];
 #else /* !CONFIG_THUMB2_KERNEL */
 
 extern const union decode_action kprobes_arm_actions[];
+extern const struct decode_checker *kprobes_arm_checkers[];
 
 #endif
 
arch/arm/probes/uprobes/core.c +1 -1
index b2954f6..d1329f1 100644
@@ -88,7 +88,7 @@ int arch_uprobe_analyze_insn(struct arch_uprobe *auprobe, struct mm_struct *mm,
 	auprobe->ixol[1] = __opcode_to_mem_arm(UPROBE_SS_ARM_INSN);
 
 	ret = arm_probes_decode_insn(insn, &auprobe->asi, false,
-				     uprobes_probes_actions);
+				     uprobes_probes_actions, NULL);
 	switch (ret) {
 	case INSN_REJECTED:
 		return -EINVAL;
Signature · 1 line
1.8.4
author
#3 lore raw

PATCH v14 3/7 ARM: kprobes: collects stack consumption for store instructions

In reply to Wang Nan

This patch uses the previously introduced checker functionality on store instructions to record their stack consumption information to arch_probes_insn.

---
v1 -> v2:
- Bugfix and code improvements following Tixy's suggestion. See:

    http://www.spinics.net/lists/arm-kernel/msg372912.html

v2 -> v3:
- Totaly reconstructed following Tixy' instruction. See:
https://lkml.org/lkml/2014/10/27/662 .
Add his SOB.

v3 -> v4:
- Commit message improvements.
- Comments improvements and code cleanup.
- A bug is found and fixed in decode table in arm_check_stack().

v4 -> v5:
- Bugs in thumb2 decoding table is found by Tixy and fixed.

v5 -> v6:
- Move to arch/arm/probes/ .

---
 arch/arm/include/asm/probes.h             |   1 +
 arch/arm/probes/decode.c                  |  10 +++
 arch/arm/probes/kprobes/Makefile          |   6 +-
 arch/arm/probes/kprobes/actions-arm.c     |   3 +-
 arch/arm/probes/kprobes/actions-thumb.c   |   5 +-
 arch/arm/probes/kprobes/checkers-arm.c    |  99 +++++++++++++++++++++++++++
 arch/arm/probes/kprobes/checkers-common.c | 101 +++++++++++++++++++++++++++
 arch/arm/probes/kprobes/checkers-thumb.c  | 110 ++++++++++++++++++++++++++++++
 arch/arm/probes/kprobes/checkers.h        |  54 +++++++++++++++
 9 files changed, 383 insertions(+), 6 deletions(-)
 create mode 100644 arch/arm/probes/kprobes/checkers-arm.c
 create mode 100644 arch/arm/probes/kprobes/checkers-common.c
 create mode 100644 arch/arm/probes/kprobes/checkers-thumb.c
 create mode 100644 arch/arm/probes/kprobes/checkers.h
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/probes/decode.c +10 -0
index c7d4420..f9d7c42 100644
@@ -425,6 +425,16 @@ probes_decode_insn(probes_opcode_t insn, struct arch_probes_insn *asi,
 	 */
 	probes_opcode_t origin_insn = insn;
 
+	/*
+	 * stack_space is initialized to 0 here. Checker functions
+	 * should update is value if they find this is a stack store
+	 * instruction: positive value means bytes of stack usage,
+	 * negitive value means unable to determine stack usage
+	 * statically. For instruction doesn't store to stack, checker
+	 * do nothing with it.
+	 */
+	asi->stack_space = 0;
+
 	if (emulate)
 		insn = prepare_emulated_insn(insn, asi, thumb);
 
arch/arm/probes/kprobes/Makefile +3 -3
index eb38a42..bc8d504 100644
@@ -1,11 +1,11 @@
-obj-$(CONFIG_KPROBES)		+= core.o actions-common.o
+obj-$(CONFIG_KPROBES)		+= core.o actions-common.o checkers-common.o
 obj-$(CONFIG_ARM_KPROBES_TEST)	+= test-kprobes.o
 test-kprobes-objs		:= test-core.o
 
 ifdef CONFIG_THUMB2_KERNEL
-obj-$(CONFIG_KPROBES)		+= actions-thumb.o
+obj-$(CONFIG_KPROBES)		+= actions-thumb.o checkers-thumb.o
 test-kprobes-objs		+= test-thumb.o
 else
-obj-$(CONFIG_KPROBES)		+= actions-arm.o
+obj-$(CONFIG_KPROBES)		+= actions-arm.o checkers-arm.o
 test-kprobes-objs		+= test-arm.o
 endif
arch/arm/probes/kprobes/actions-arm.c +2 -1
index 3c3afa4..1e67839 100644
@@ -64,6 +64,7 @@
 
 #include "../decode-arm.h"
 #include "core.h"
+#include "checkers.h"
 
 #if  __LINUX_ARM_ARCH__ >= 6
 #define BLX(reg)	"blx	"reg"		\n\t"
@@ -342,4 +343,4 @@ const union decode_action kprobes_arm_actions[NUM_PROBES_ARM_ACTIONS] = {
 	[PROBES_LDMSTM] = {.decoder = kprobe_decode_ldmstm}
 };
 
-const struct decode_checker *kprobes_arm_checkers[] = {NULL};
+const struct decode_checker *kprobes_arm_checkers[] = {arm_stack_checker, NULL};
arch/arm/probes/kprobes/actions-thumb.c +3 -2
index 2796121..07cfd9b 100644
@@ -15,6 +15,7 @@
 
 #include "../decode-thumb.h"
 #include "core.h"
+#include "checkers.h"
 
 /* These emulation encodings are functionally equivalent... */
 #define t32_emulate_rd8rn16rm0ra12_noflags \
@@ -665,5 +666,5 @@ const union decode_action kprobes_t32_actions[NUM_PROBES_T32_ACTIONS] = {
 		.handler = t32_emulate_rdlo12rdhi8rn16rm0_noflags},
 };
 
-const struct decode_checker *kprobes_t32_checkers[] = {NULL};
-const struct decode_checker *kprobes_t16_checkers[] = {NULL};
+const struct decode_checker *kprobes_t32_checkers[] = {t32_stack_checker, NULL};
+const struct decode_checker *kprobes_t16_checkers[] = {t16_stack_checker, NULL};
arch/arm/probes/kprobes/checkers-arm.c new file +99 -0

99 lines not on this page · raw message

arch/arm/probes/kprobes/checkers-common.c new file +101 -0

101 lines not on this page · raw message

arch/arm/probes/kprobes/checkers-thumb.c new file +110 -0

110 lines not on this page · raw message

arch/arm/probes/kprobes/checkers.h new file +54 -0

54 lines not on this page · raw message

Signature · 1 line
1.8.4
author
#4 lore raw

PATCH v14 4/7 ARM: kprobes: disallow probing stack consuming instructions

In reply to Wang Nan

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

---
v1 -> v2:
- Use MAX_STACK_SIZE macro instead of hard coded stack size.

v2 -> v3:
- Commit message improvements.

v3 -> v4:
- Commit message improvements.

v4 -> v5:
- Move to arch/arm/probes.

---
 arch/arm/include/asm/kprobes.h     |  1 -
 arch/arm/include/asm/probes.h      | 12 ++++++++++++
 arch/arm/kernel/entry-armv.S       |  3 ++-
 arch/arm/probes/kprobes/core.c     |  9 +++++++++
 arch/arm/probes/kprobes/test-arm.c | 16 ++++++++++------
 5 files changed, 33 insertions(+), 8 deletions(-)
arch/arm/include/asm/kprobes.h +0 -1
index 49fa0df..56f9ac6 100644
@@ -22,7 +22,6 @@
 
 #define __ARCH_WANT_KPROBES_INSN_SLOT
 #define MAX_INSN_SIZE			2
-#define MAX_STACK_SIZE			64	/* 32 would probably be OK */
 
 #define flush_insn_slot(p)		do { } while (0)
 #define kretprobe_blacklist_size	0
arch/arm/include/asm/probes.h +12 -0
index ccf9af3..f0a1ee8 100644
@@ -19,6 +19,8 @@
 #ifndef _ASM_PROBES_H
 #define _ASM_PROBES_H
 
+#ifndef __ASSEMBLY__
+
 typedef u32 probes_opcode_t;
 
 struct arch_probes_insn;
@@ -41,4 +43,14 @@ struct arch_probes_insn {
 	int stack_space;
 };
 
+#endif /* __ASSEMBLY__ */
+
+/*
+ * We assume one instruction can consume at most 64 bytes stack, which is
+ * 'push {r0-r15}'. Instructions consume more or unknown stack space like
+ * 'str r0, [sp, #-80]' and 'str r0, [sp, r1]' should be prohibit to probe.
+ * Both kprobe and jprobe use this macro.
+ */
+#define MAX_STACK_SIZE			64
+
 #endif
arch/arm/kernel/entry-armv.S +2 -1
index 2f5555d..672b219 100644
@@ -31,6 +31,7 @@
 
 #include "entry-header.S"
 #include <asm/entry-macro-multi.S>
+#include <asm/probes.h>
 
 /*
  * Interrupt handling.
@@ -249,7 +250,7 @@ __und_svc:
 	@ If a kprobe is about to simulate a "stmdb sp..." instruction,
 	@ it obviously needs free stack space which then will belong to
 	@ the saved context.
-	svc_entry 64
+	svc_entry MAX_STACK_SIZE
 #else
 	svc_entry
 #endif
arch/arm/probes/kprobes/core.c +9 -0
index 74f3dc3..3a58db4 100644
@@ -115,6 +115,15 @@ int __kprobes arch_prepare_kprobe(struct kprobe *p)
 		break;
 	}
 
+	/*
+	 * Never instrument insn like 'str r0, [sp, +/-r1]'. Also, insn likes
+	 * 'str r0, [sp, #-68]' should also be prohibited.
+	 * See __und_svc.
+	 */
+	if ((p->ainsn.stack_space < 0) ||
+			(p->ainsn.stack_space > MAX_STACK_SIZE))
+		return -EINVAL;
+
 	return 0;
 }
 
arch/arm/probes/kprobes/test-arm.c +10 -6
index d9a1255..fdeb300 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,"")
@@ -638,13 +640,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]")			\
@@ -668,12 +672,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,"]")
 
Signature · 1 line
1.8.4
author
#5 lore raw

PATCH v14 5/7 ARM: kprobes: Add test cases for stack consuming instructions

In reply to Wang Nan

From: Jon Medhurst <[email protected]>

These have extra 'checker' functions associated with them so lets make sure those get covered by testing.

---
v1 -> v2:
- Move to arch/arm/probes/ .

---
 arch/arm/probes/kprobes/test-arm.c   | 17 +++++++++++++++--
 arch/arm/probes/kprobes/test-thumb.c | 12 ++++++++++++
 2 files changed, 27 insertions(+), 2 deletions(-)
arch/arm/probes/kprobes/test-arm.c +15 -2
index fdeb300..9b3b1b4 100644
@@ -12,6 +12,7 @@
 #include <linux/module.h>
 #include <asm/system_info.h>
 #include <asm/opcodes.h>
+#include <asm/probes.h>
 
 #include "test-core.h"
 
@@ -478,6 +479,7 @@ void kprobe_arm_test_cases(void)
 	TEST_RPR(  "strh	r",0, VAL1,", [r",1, 48,", -r",2, 24,"]")
 	TEST_RPR(  "streqh	r",14,VAL2,", [r",11,0, ", r",12, 48,"]")
 	TEST_UNSUPPORTED(  "streqh	r14, [r13, r12]")
+	TEST_UNSUPPORTED(  "streqh	r14, [r12, r13]")
 	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,"")
@@ -502,6 +504,9 @@ void kprobe_arm_test_cases(void)
 	TEST_RP(   "strplh	r",12,VAL2,", [r",11,24,", #-4]!")
 	TEST_RP(   "strh	r",2, VAL1,", [r",3, 24,"], #48")
 	TEST_RP(   "strh	r",10,VAL2,", [r",9, 64,"], #-48")
+	TEST_RP(   "strh	r",3, VAL1,", [r",13,TEST_MEMORY_SIZE,", #-"__stringify(MAX_STACK_SIZE)"]!")
+	TEST_UNSUPPORTED("strh r3, [r13, #-"__stringify(MAX_STACK_SIZE)"-8]!")
+	TEST_RP(   "strh	r",4, VAL1,", [r",14,TEST_MEMORY_SIZE,", #-"__stringify(MAX_STACK_SIZE)"-8]!")
 	TEST_UNSUPPORTED(__inst_arm(0xe1efc3b0) "	@ strh r12, [pc, #48]!")
 	TEST_UNSUPPORTED(__inst_arm(0xe0c9f3b0) "	@ strh pc, [r9], #48")
 
@@ -568,6 +573,7 @@ void kprobe_arm_test_cases(void)
 	TEST_RPR(  "strd	r",0, VAL1,", [r",1, 48,", -r",2,24,"]")
 	TEST_RPR(  "strccd	r",8, VAL2,", [r",11,0, ", r",12,48,"]")
 	TEST_UNSUPPORTED(  "strccd r8, [r13, r12]")
+	TEST_UNSUPPORTED(  "strccd r8, [r12, r13]")
 	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,"")
@@ -591,6 +597,9 @@ void kprobe_arm_test_cases(void)
 	TEST_RP(   "strvcd	r",12,VAL2,", [r",11,24,", #-16]!")
 	TEST_RP(   "strd	r",2, VAL1,", [r",4, 24,"], #48")
 	TEST_RP(   "strd	r",10,VAL2,", [r",9, 64,"], #-48")
+	TEST_RP(   "strd	r",6, VAL1,", [r",13,TEST_MEMORY_SIZE,", #-"__stringify(MAX_STACK_SIZE)"]!")
+	TEST_UNSUPPORTED("strd r6, [r13, #-"__stringify(MAX_STACK_SIZE)"-8]!")
+	TEST_RP(   "strd	r",4, VAL1,", [r",12,TEST_MEMORY_SIZE,", #-"__stringify(MAX_STACK_SIZE)"-8]!")
 	TEST_UNSUPPORTED(__inst_arm(0xe1efc3f0) "	@ strd r12, [pc, #48]!")
 
 	TEST_P(	   "ldrd	r0, [r",0, 24,", #-8]")
@@ -639,16 +648,20 @@ void kprobe_arm_test_cases(void)
 	TEST_RP( "str"byte"	r",12,VAL2,", [r",11,24,", #-4]!")		\
 	TEST_RP( "str"byte"	r",2, VAL1,", [r",3, 24,"], #48")		\
 	TEST_RP( "str"byte"	r",10,VAL2,", [r",9, 64,"], #-48")		\
+	TEST_RP( "str"byte"	r",3, VAL1,", [r",13,TEST_MEMORY_SIZE,", #-"__stringify(MAX_STACK_SIZE)"]!") \
+	TEST_UNSUPPORTED("str"byte" r3, [r13, #-"__stringify(MAX_STACK_SIZE)"-8]!")				\
+	TEST_RP( "str"byte"	r",4, VAL1,", [r",10,TEST_MEMORY_SIZE,", #-"__stringify(MAX_STACK_SIZE)"-8]!") \
 	TEST_RPR("str"byte"	r",0, VAL1,", [r",1, 48,", -r",2, 24,"]")	\
 	TEST_RPR("str"byte"	r",14,VAL2,", [r",11,0, ", r",12, 48,"]")	\
-	TEST_UNSUPPORTED("str"byte" r14, [r13, r12]")	\
+	TEST_UNSUPPORTED("str"byte" r14, [r13, r12]")				\
+	TEST_UNSUPPORTED("str"byte" r14, [r12, r13]")				\
 	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",11,0, ", r",12, 32,", lsr #2]")\
-	TEST_UNSUPPORTED("str"byte"	r14, [r13, r12, 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]")			\
arch/arm/probes/kprobes/test-thumb.c +12 -0
index 6c6e9a9..e8cf193 100644
@@ -11,6 +11,7 @@
 #include <linux/kernel.h>
 #include <linux/module.h>
 #include <asm/opcodes.h>
+#include <asm/probes.h>
 
 #include "test-core.h"
 
@@ -416,6 +417,9 @@ void kprobe_thumb32_test_cases(void)
 	TEST_RR( "strd	r",14,VAL2,", r",12,VAL1,", [sp, #16]!")
 	TEST_RRP("strd	r",1, VAL1,", r",0, VAL2,", [r",7, 24,"], #16")
 	TEST_RR( "strd	r",7, VAL2,", r",8, VAL1,", [sp], #-16")
+	TEST_RRP("strd	r",6, VAL1,", r",7, VAL2,", [r",13, TEST_MEMORY_SIZE,", #-"__stringify(MAX_STACK_SIZE)"]!")
+	TEST_UNSUPPORTED("strd r6, r7, [r13, #-"__stringify(MAX_STACK_SIZE)"-8]!")
+	TEST_RRP("strd	r",4, VAL1,", r",5, VAL2,", [r",14, TEST_MEMORY_SIZE,", #-"__stringify(MAX_STACK_SIZE)"-8]!")
 	TEST_UNSUPPORTED(__inst_thumb32(0xe9efec04) "	@ strd	r14, r12, [pc, #16]!")
 	TEST_UNSUPPORTED(__inst_thumb32(0xe8efec04) "	@ strd	r14, r12, [pc], #16")
 
@@ -821,14 +825,22 @@ CONDITION_INSTRUCTIONS(22,
 	TEST_RP( "str"size"	r",14,VAL2,", [r",1, 256,  ", #-128]!")		\
 	TEST_RPR("str"size".w	r",0, VAL1,", [r",1, 0,", r",2, 4,"]")		\
 	TEST_RPR("str"size"	r",14,VAL2,", [r",10,0,", r",11,4,", lsl #1]")	\
+	TEST_UNSUPPORTED("str"size"	r0, [r13, r1]")				\
 	TEST_R(  "str"size".w	r",7, VAL1,", [sp, #24]")			\
 	TEST_RP( "str"size".w	r",0, VAL2,", [r",0,0, "]")			\
+	TEST_RP( "str"size"	r",6, VAL1,", [r",13, TEST_MEMORY_SIZE,", #-"__stringify(MAX_STACK_SIZE)"]!") \
+	TEST_UNSUPPORTED("str"size"	r6, [r13, #-"__stringify(MAX_STACK_SIZE)"-8]!")			\
+	TEST_RP( "str"size"	r",4, VAL2,", [r",12, TEST_MEMORY_SIZE,", #-"__stringify(MAX_STACK_SIZE)"-8]!") \
 	TEST_UNSUPPORTED("str"size"t	r0, [r1, #4]")
 
 	SINGLE_STORE("b")
 	SINGLE_STORE("h")
 	SINGLE_STORE("")
 
+	TEST_UNSUPPORTED(__inst_thumb32(0xf801000d) "	@ strb	r0, [r1, r13]")
+	TEST_UNSUPPORTED(__inst_thumb32(0xf821000d) "	@ strh	r0, [r1, r13]")
+	TEST_UNSUPPORTED(__inst_thumb32(0xf841000d) "	@ str	r0, [r1, r13]")
+
 	TEST("str	sp, [sp]")
 	TEST_UNSUPPORTED(__inst_thumb32(0xf8cfe000) "	@ str	r14, [pc]")
 	TEST_UNSUPPORTED(__inst_thumb32(0xf8cef000) "	@ str	pc, [r14]")
Signature · 1 line
1.8.4
author
#6 lore raw

PATCH v14 6/7 kprobes: Pass the original kprobe for preparing optimized kprobe

In reply to Wang Nan

From: Masami Hiramatsu <[email protected]>

Pass the original kprobe for preparing an optimized kprobe arch-dep part, since for some architecture (e.g. ARM32) requires the information in original kprobe.
---

---
 arch/x86/kernel/kprobes/opt.c | 3 ++-
 include/linux/kprobes.h       | 3 ++-
 kernel/kprobes.c              | 4 ++--
 3 files changed, 6 insertions(+), 4 deletions(-)
arch/x86/kernel/kprobes/opt.c +2 -1
index f1314d0..7028296 100644
@@ -320,7 +320,8 @@ void arch_remove_optimized_kprobe(struct optimized_kprobe *op)
  * Target instructions MUST be relocatable (checked inside)
  * This is called when new aggr(opt)probe is allocated or reused.
  */
-int arch_prepare_optimized_kprobe(struct optimized_kprobe *op)
+int arch_prepare_optimized_kprobe(struct optimized_kprobe *op,
+				  struct kprobe *__unused)
 {
 	u8 *buf;
 	int ret;
include/linux/kprobes.h +2 -1
index f7296e5..7ab2c93 100644
@@ -308,7 +308,8 @@ struct optimized_kprobe {
 /* Architecture dependent functions for direct jump optimization */
 extern int arch_prepared_optinsn(struct arch_optimized_insn *optinsn);
 extern int arch_check_optimized_kprobe(struct optimized_kprobe *op);
-extern int arch_prepare_optimized_kprobe(struct optimized_kprobe *op);
+extern int arch_prepare_optimized_kprobe(struct optimized_kprobe *op,
+					 struct kprobe *orig);
 extern void arch_remove_optimized_kprobe(struct optimized_kprobe *op);
 extern void arch_optimize_kprobes(struct list_head *oplist);
 extern void arch_unoptimize_kprobes(struct list_head *oplist,
kernel/kprobes.c +2 -2
index 3995f54..9f28aa7 100644
@@ -717,7 +717,7 @@ static void prepare_optimized_kprobe(struct kprobe *p)
 	struct optimized_kprobe *op;
 
 	op = container_of(p, struct optimized_kprobe, kp);
-	arch_prepare_optimized_kprobe(op);
+	arch_prepare_optimized_kprobe(op, p);
 }
 
 /* Allocate new optimized_kprobe and try to prepare optimized instructions */
@@ -731,7 +731,7 @@ static struct kprobe *alloc_aggr_kprobe(struct kprobe *p)
 
 	INIT_LIST_HEAD(&op->list);
 	op->kp.addr = p->addr;
-	arch_prepare_optimized_kprobe(op);
+	arch_prepare_optimized_kprobe(op, p);
 
 	return &op->kp;
 }
Signature · 1 line
1.8.4
author
#7 lore raw

PATCH v14 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.

Cc Jon Medhurst (Tixy) <[email protected]>
Cc Russell King - ARM Linux <[email protected]>
Cc Will Deacon <[email protected]>

---
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.

v6 -> v7:
- Using checker to check stack consumption.

v7 -> v8:
- Small code adjustments.

v8 -> v9:
- Utilize original kprobe passed to arch_prepare_optimized_kprobe() to avoid copy ainsn twice.
- A bug in arch_prepare_optimized_kprobe() is found and fixed.

v9 -> v10:
- Commit message improvements.

v10 -> v11:
- Move to arch/arm/probes/, insn.h is moved to arch/arm/include/asm.
- Code cleanup.
- Bugfix based on Tixy's test result:
- Trampoline deal with ARM -> Thumb transision instructions and AEABI stack alignment requirement correctly.
- Trampoline code buffer should start at 4 byte aligned address. We enforces it in this series by using macro to wrap 'code' var.

v11 -> v12:
- Remove trampoline code stack trick and use r4 to save original stack.
- Remove trampoline code buffer alignment trick.
- Names of files are changed.

v12 -> v13:
- Assume stack always aligned by 4-bytes in any case.
- Comments update.

v13 -> v14:
- Use stop_machine to wrap arch_optimize_kprobes to avoid a racing.

---
 arch/arm/Kconfig                        |   1 +
 arch/arm/{kernel => include/asm}/insn.h |   0
 arch/arm/include/asm/kprobes.h          |  29 +++
 arch/arm/kernel/Makefile                |   2 +-
 arch/arm/kernel/ftrace.c                |   3 +-
 arch/arm/kernel/jump_label.c            |   3 +-
 arch/arm/probes/kprobes/Makefile        |   1 +
 arch/arm/probes/kprobes/opt-arm.c       | 322 ++++++++++++++++++++++++++++++++
 samples/kprobes/kprobe_example.c        |   2 +-
 9 files changed, 357 insertions(+), 6 deletions(-)
 rename arch/arm/{kernel => include/asm}/insn.h (100%)
 create mode 100644 arch/arm/probes/kprobes/opt-arm.c
arch/arm/Kconfig +1 -0
index 89c4b5c..2471240 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/insn.h renamed from arch/arm/kernel/insn.h +0 -0
similarity index 100%
rename from arch/arm/kernel/insn.h
rename to arch/arm/include/asm/insn.h
arch/arm/include/asm/kprobes.h +29 -0
index 56f9ac6..50ff3bc 100644
@@ -50,5 +50,34 @@ 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;
+extern __visible kprobe_opcode_t optprobe_template_sub_sp;
+extern __visible kprobe_opcode_t optprobe_template_add_sp;
+
+#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	DIV_ROUND_UP(RELATIVEJUMP_SIZE, sizeof(kprobe_opcode_t))
+	kprobe_opcode_t copied_insn[MAX_COPIED_INSN];
+	/* detour code buffer */
+	kprobe_opcode_t *insn;
+	/*
+	 * We always copy one instruction on ARM,
+	 * so size will always be 4, and unlike x86, there is no
+	 * need for a size field.
+	 */
+};
 
 #endif /* _ARM_KPROBES_H */
arch/arm/kernel/Makefile +1 -1
index 40d3e00..1d0f4e7 100644
@@ -52,7 +52,7 @@ 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
 # Main staffs in KPROBES are in arch/arm/probes/ .
-obj-$(CONFIG_KPROBES)		+= patch.o
+obj-$(CONFIG_KPROBES)		+= patch.o insn.o
 obj-$(CONFIG_OABI_COMPAT)	+= sys_oabi-compat.o
 obj-$(CONFIG_ARM_THUMBEE)	+= thumbee.o
 obj-$(CONFIG_KGDB)		+= kgdb.o
arch/arm/kernel/ftrace.c +1 -2
index af9a8a9..ec7e332 100644
@@ -19,8 +19,7 @@
 #include <asm/cacheflush.h>
 #include <asm/opcodes.h>
 #include <asm/ftrace.h>
-
-#include "insn.h"
+#include <asm/insn.h>
 
 #ifdef CONFIG_THUMB2_KERNEL
 #define	NOP		0xf85deb04	/* pop.w {lr} */
arch/arm/kernel/jump_label.c +1 -2
index c6c73ed..35a8fbb 100644
@@ -1,8 +1,7 @@
 #include <linux/kernel.h>
 #include <linux/jump_label.h>
 #include <asm/patch.h>
-
-#include "insn.h"
+#include <asm/insn.h>
 
 #ifdef HAVE_JUMP_LABEL
 
arch/arm/probes/kprobes/Makefile +1 -0
index bc8d504..76a36bf 100644
@@ -7,5 +7,6 @@ obj-$(CONFIG_KPROBES)		+= actions-thumb.o checkers-thumb.o
 test-kprobes-objs		+= test-thumb.o
 else
 obj-$(CONFIG_KPROBES)		+= actions-arm.o checkers-arm.o
+obj-$(CONFIG_OPTPROBES)		+= opt-arm.o
 test-kprobes-objs		+= test-arm.o
 endif
arch/arm/probes/kprobes/opt-arm.c new file +322 -0

322 lines not on this page · raw message

samples/kprobes/kprobe_example.c +1 -1
index 623f864..e4e45f1 100644
@@ -94,7 +94,7 @@ static int handler_fault(struct kprobe *p, struct pt_regs *regs, int trapnr)
 static int __init kprobe_init(void)
 {
 	int ret;
-	kp.addr = kallsyms_lookup_name(kp.symbol_name);
+	kp.addr = kallsyms_lookup_name(kp.symbol_name) + 8;
 	kp.symbol_name = NULL;
 	kp.pre_handler = handler_pre;
 	kp.post_handler = NULL;
Signature · 1 line
1.8.4
#8 lore raw

Re: [PATCH v14 7/7] ARM: kprobes: enable OPTPROBES for ARM 32

Quoted 27 lines · 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.

Cc Jon Medhurst (Tixy) <[email protected]>
Cc Russell King - ARM Linux <[email protected]>
Cc Will Deacon <[email protected]>

---

[...]

Quoted 2 lines

v13 -> v14:
- Use stop_machine to wrap arch_optimize_kprobes to avoid a racing.

Think we need to use stop_machine differently, see comments on code below.

Quoted 10 lines
---
 arch/arm/Kconfig                        |   1 +
 arch/arm/{kernel => include/asm}/insn.h |   0
 arch/arm/include/asm/kprobes.h          |  29 +++
 arch/arm/kernel/Makefile                |   2 +-
 arch/arm/kernel/ftrace.c                |   3 +-
 arch/arm/kernel/jump_label.c            |   3 +-
 arch/arm/probes/kprobes/Makefile        |   1 +
 arch/arm/probes/kprobes/opt-arm.c       | 322 ++++++++++++++++++++++++++++++++
 samples/kprobes/kprobe_example.c        |   2 +-

The change kprobe_example.c doesn't apply and I guess wasn't meant to be included in the patch?

[...]

Quoted 23 lines
arch/arm/probes/kprobes/opt-arm.c new file +23 -0
+/*
+ * Similar to __arch_disarm_kprobe, operations which removing
+ * breakpoints must be wrapped by stop_machine to avoid racing.
+ */
+static __kprobes int __arch_optimize_kprobes(void *p)
+{
+	struct list_head *oplist = p;
+	struct optimized_kprobe *op, *tmp;
+
+	list_for_each_entry_safe(op, tmp, oplist, list) {
+		unsigned long insn;
+		WARN_ON(kprobe_disabled(&op->kp));
+
+		/*
+		 * Backup instructions which will be replaced
+		 * by jump address
+		 */
+		memcpy(op->optinsn.copied_insn, op->kp.addr,
+				RELATIVEJUMP_SIZE);
+
+		insn = arm_gen_branch((unsigned long)op->kp.addr,
+				(unsigned long)op->optinsn.insn);
+		BUG_ON(insn == 0);

arch/arm/probes/kprobes/opt-arm.c line 280

Quoted 10 lines
arch/arm/probes/kprobes/opt-arm.c new file +10 -0
+
+		/*
+		 * Make it a conditional branch if replaced insn
+		 * is consitional
+		 */
+		insn = (__mem_to_opcode_arm(
+			  op->optinsn.copied_insn[0]) & 0xf0000000) |
+			(insn & 0x0fffffff);
+
+		patch_text(op->kp.addr, insn);

patch_text() itself may use stop_machine under certain circumstances, and if it were to do so, I believe that would cause the system to lock/panic. So, this should be __patch_text() instead, but we would also need to take care of the cache_ops_need_broadcast() case, where all CPU's need to invalidate their own caches and we can't rely on just one CPU executing the code patching whilst other CPUs spin and wait. Though to make life easier, we could just not optimise kprobes in the legacy cache_ops_need_broadcast() case.

arch/arm/probes/kprobes/opt-arm.c line 290

Quoted 10 lines
arch/arm/probes/kprobes/opt-arm.c new file +10 -0
+
+		list_del_init(&op->list);
+	}
+	return 0;
+}
+
+void arch_optimize_kprobes(struct list_head *oplist)
+{
+	stop_machine(__arch_optimize_kprobes, oplist, cpu_online_mask);
+}

I believe passing cpu_online_mask above will cause __arch_optimize_kprobes to be executed on every CPU, is this safe? If it is, it's a serendipitous optimisation if each CPU can process different probes in the list. If it's not safe, this needs to be NULL instead so only one CPU executes the code.

However, I wonder if optimising all probes under a single stop_machine call is the best thing to do because stop_machine does what it says and prevents everything else in the system from running, including interrupt handlers. Perhaps for system responsiveness this should be a single stop_machine per kprobe? Though of course that compounds the overhead of stop_machine use and puts another delay of one scheduler tick per probe. (stop_machine waits for the next tick to schedule the threads to perform the work which is why the test code takes so long to run).

What do people think?

Signature · 1 line
Tixy
author
#9 lore raw

Re: [PATCH v14 7/7] ARM: kprobes: enable OPTPROBES for ARM 32

Quoted 28 lines · Jon Medhurst (Tixy)
Quoted 21 lines · 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.

Quoted 6 lines
Cc Jon Medhurst (Tixy) <[email protected]>
Cc Russell King - ARM Linux <[email protected]>
Cc Will Deacon <[email protected]>

---

Quoted 6 lines

[...]

Quoted 2 lines

v13 -> v14:
- Use stop_machine to wrap arch_optimize_kprobes to avoid a racing.

Think we need to use stop_machine differently, see comments on code below.

Well, yes, I experienced one deadlock at serval minutes before. I'm not very sure the reason and working on it now. I think it may caused by recursivly stop_machine().

Quoted 12 lines
Quoted 11 lines
---
 arch/arm/Kconfig                        |   1 +
 arch/arm/{kernel => include/asm}/insn.h |   0
 arch/arm/include/asm/kprobes.h          |  29 +++
 arch/arm/kernel/Makefile                |   2 +-
 arch/arm/kernel/ftrace.c                |   3 +-
 arch/arm/kernel/jump_label.c            |   3 +-
 arch/arm/probes/kprobes/Makefile        |   1 +
 arch/arm/probes/kprobes/opt-arm.c       | 322 ++++++++++++++++++++++++++++++++
 samples/kprobes/kprobe_example.c        |   2 +-
Quoted 3 lines

The change kprobe_example.c doesn't apply and I guess wasn't meant to be included in the patch?

Yes. These 2 lines are introduced by mistake.

Quoted 44 lines

[...]

Quoted 23 lines
arch/arm/probes/kprobes/opt-arm.c new file +23 -0
+/*
+ * Similar to __arch_disarm_kprobe, operations which removing
+ * breakpoints must be wrapped by stop_machine to avoid racing.
+ */
+static __kprobes int __arch_optimize_kprobes(void *p)
+{
+	struct list_head *oplist = p;
+	struct optimized_kprobe *op, *tmp;
+
+	list_for_each_entry_safe(op, tmp, oplist, list) {
+		unsigned long insn;
+		WARN_ON(kprobe_disabled(&op->kp));
+
+		/*
+		 * Backup instructions which will be replaced
+		 * by jump address
+		 */
+		memcpy(op->optinsn.copied_insn, op->kp.addr,
+				RELATIVEJUMP_SIZE);
+
+		insn = arm_gen_branch((unsigned long)op->kp.addr,
+				(unsigned long)op->optinsn.insn);
+		BUG_ON(insn == 0);

arch/arm/probes/kprobes/opt-arm.c line 280

Quoted 10 lines
arch/arm/probes/kprobes/opt-arm.c new file +10 -0
+
+		/*
+		 * Make it a conditional branch if replaced insn
+		 * is consitional
+		 */
+		insn = (__mem_to_opcode_arm(
+			  op->optinsn.copied_insn[0]) & 0xf0000000) |
+			(insn & 0x0fffffff);
+
+		patch_text(op->kp.addr, insn);

patch_text() itself may use stop_machine under certain circumstances, and if it were to do so, I believe that would cause the system to lock/panic. So, this should be __patch_text() instead, but we would also need to take care of the cache_ops_need_broadcast() case, where all CPU's need to invalidate their own caches and we can't rely on just one CPU executing the code patching whilst other CPUs spin and wait. Though to make life easier, we could just not optimise kprobes in the legacy cache_ops_need_broadcast() case.

Quoted 17 lines

arch/arm/probes/kprobes/opt-arm.c line 290

Quoted 10 lines
arch/arm/probes/kprobes/opt-arm.c new file +10 -0
+
+		list_del_init(&op->list);
+	}
+	return 0;
+}
+
+void arch_optimize_kprobes(struct list_head *oplist)
+{
+	stop_machine(__arch_optimize_kprobes, oplist, cpu_online_mask);
+}

I believe passing cpu_online_mask above will cause __arch_optimize_kprobes to be executed on every CPU, is this safe? If it is, it's a serendipitous optimisation if each CPU can process different probes in the list. If it's not safe, this needs to be NULL instead so only one CPU executes the code.

This stop_machine() call is copied from arch_disarm_kprobe, I think their senario should be similar.

Quoted 11 lines

However, I wonder if optimising all probes under a single stop_machine call is the best thing to do because stop_machine does what it says and prevents everything else in the system from running, including interrupt handlers. Perhaps for system responsiveness this should be a single stop_machine per kprobe? Though of course that compounds the overhead of stop_machine use and puts another delay of one scheduler tick per probe. (stop_machine waits for the next tick to schedule the threads to perform the work which is why the test code takes so long to run).

What do people think?

#10 lore raw

Re: [PATCH v14 7/7] ARM: kprobes: enable OPTPROBES for ARM 32

Quoted 120 lines · Wang Nan
Quoted 28 lines · Jon Medhurst (Tixy)
Quoted 21 lines · 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.

Quoted 6 lines
Cc Jon Medhurst (Tixy) <[email protected]>
Cc Russell King - ARM Linux <[email protected]>
Cc Will Deacon <[email protected]>

---

Quoted 6 lines

[...]

Quoted 2 lines

v13 -> v14:
- Use stop_machine to wrap arch_optimize_kprobes to avoid a racing.

Think we need to use stop_machine differently, see comments on code below.

Well, yes, I experienced one deadlock at serval minutes before. I'm not very sure the reason and working on it now. I think it may caused by recursivly stop_machine().

Quoted 12 lines
Quoted 11 lines
---
 arch/arm/Kconfig                        |   1 +
 arch/arm/{kernel => include/asm}/insn.h |   0
 arch/arm/include/asm/kprobes.h          |  29 +++
 arch/arm/kernel/Makefile                |   2 +-
 arch/arm/kernel/ftrace.c                |   3 +-
 arch/arm/kernel/jump_label.c            |   3 +-
 arch/arm/probes/kprobes/Makefile        |   1 +
 arch/arm/probes/kprobes/opt-arm.c       | 322 ++++++++++++++++++++++++++++++++
 samples/kprobes/kprobe_example.c        |   2 +-
Quoted 3 lines

The change kprobe_example.c doesn't apply and I guess wasn't meant to be included in the patch?

Yes. These 2 lines are introduced by mistake.

Quoted 44 lines

[...]

Quoted 23 lines
arch/arm/probes/kprobes/opt-arm.c new file +23 -0
+/*
+ * Similar to __arch_disarm_kprobe, operations which removing
+ * breakpoints must be wrapped by stop_machine to avoid racing.
+ */
+static __kprobes int __arch_optimize_kprobes(void *p)
+{
+	struct list_head *oplist = p;
+	struct optimized_kprobe *op, *tmp;
+
+	list_for_each_entry_safe(op, tmp, oplist, list) {
+		unsigned long insn;
+		WARN_ON(kprobe_disabled(&op->kp));
+
+		/*
+		 * Backup instructions which will be replaced
+		 * by jump address
+		 */
+		memcpy(op->optinsn.copied_insn, op->kp.addr,
+				RELATIVEJUMP_SIZE);
+
+		insn = arm_gen_branch((unsigned long)op->kp.addr,
+				(unsigned long)op->optinsn.insn);
+		BUG_ON(insn == 0);

arch/arm/probes/kprobes/opt-arm.c line 280

Quoted 10 lines
arch/arm/probes/kprobes/opt-arm.c new file +10 -0
+
+		/*
+		 * Make it a conditional branch if replaced insn
+		 * is consitional
+		 */
+		insn = (__mem_to_opcode_arm(
+			  op->optinsn.copied_insn[0]) & 0xf0000000) |
+			(insn & 0x0fffffff);
+
+		patch_text(op->kp.addr, insn);

patch_text() itself may use stop_machine under certain circumstances, and if it were to do so, I believe that would cause the system to lock/panic. So, this should be __patch_text() instead, but we would also need to take care of the cache_ops_need_broadcast() case, where all CPU's need to invalidate their own caches and we can't rely on just one CPU executing the code patching whilst other CPUs spin and wait. Though to make life easier, we could just not optimise kprobes in the legacy cache_ops_need_broadcast() case.

Quoted 17 lines

arch/arm/probes/kprobes/opt-arm.c line 290

Quoted 10 lines
arch/arm/probes/kprobes/opt-arm.c new file +10 -0
+
+		list_del_init(&op->list);
+	}
+	return 0;
+}
+
+void arch_optimize_kprobes(struct list_head *oplist)
+{
+	stop_machine(__arch_optimize_kprobes, oplist, cpu_online_mask);
+}

I believe passing cpu_online_mask above will cause __arch_optimize_kprobes to be executed on every CPU, is this safe? If it is, it's a serendipitous optimisation if each CPU can process different probes in the list. If it's not safe, this needs to be NULL instead so only one CPU executes the code.

Quoted 2 lines

This stop_machine() call is copied from arch_disarm_kprobe, I think their senario should be similar.

arch_disarm_kprobe is just executing __patch_text on each cpu, which pokes a word of memory with a new value and flushes caches for it.

arch_optimize_kprobes is calling __arch_optimize_kprobes, which is iterating over a list of probes and removing each one in turn, if this is happening on multiple cpu's simultaneously, it's not clear to me that such an operation is safe. list_del_init calls __list_del which does

	next->prev = prev;
	prev->next = next;

so what happens if another cpu is at the same time updating any of those list entries? Without even fully analysing the code I can see that with the fact that the list handling helpers have no memory barriers, that the above two lines could be seen to execute in the reverse order, e.g.

	prev->next = next;
	next->prev = prev;

so another CPU could find and delete next before this one has finished doing so. Would the list end up in a consistent state where no loops develop and no probes are missed? I don't know the answer and a full analysis would be complicated, but my gut feeling is that if a cpu can observe the links in the list in an inconsistent state then only bad things can result.

Signature · 1 line
Tixy
author
#11 lore raw

Re: [PATCH v14 7/7] ARM: kprobes: enable OPTPROBES for ARM 32

Quoted 143 lines · Jon Medhurst (Tixy)
Quoted 120 lines · Wang Nan
Quoted 28 lines · Jon Medhurst (Tixy)
Quoted 21 lines · 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.

Quoted 6 lines
Cc Jon Medhurst (Tixy) <[email protected]>
Cc Russell King - ARM Linux <[email protected]>
Cc Will Deacon <[email protected]>

---

Quoted 6 lines

[...]

Quoted 2 lines

v13 -> v14:
- Use stop_machine to wrap arch_optimize_kprobes to avoid a racing.

Think we need to use stop_machine differently, see comments on code below.

Well, yes, I experienced one deadlock at serval minutes before. I'm not very sure the reason and working on it now. I think it may caused by recursivly stop_machine().

Quoted 12 lines
Quoted 11 lines
---
 arch/arm/Kconfig                        |   1 +
 arch/arm/{kernel => include/asm}/insn.h |   0
 arch/arm/include/asm/kprobes.h          |  29 +++
 arch/arm/kernel/Makefile                |   2 +-
 arch/arm/kernel/ftrace.c                |   3 +-
 arch/arm/kernel/jump_label.c            |   3 +-
 arch/arm/probes/kprobes/Makefile        |   1 +
 arch/arm/probes/kprobes/opt-arm.c       | 322 ++++++++++++++++++++++++++++++++
 samples/kprobes/kprobe_example.c        |   2 +-
Quoted 3 lines

The change kprobe_example.c doesn't apply and I guess wasn't meant to be included in the patch?

Yes. These 2 lines are introduced by mistake.

Quoted 44 lines

[...]

Quoted 23 lines
arch/arm/probes/kprobes/opt-arm.c new file +23 -0
+/*
+ * Similar to __arch_disarm_kprobe, operations which removing
+ * breakpoints must be wrapped by stop_machine to avoid racing.
+ */
+static __kprobes int __arch_optimize_kprobes(void *p)
+{
+	struct list_head *oplist = p;
+	struct optimized_kprobe *op, *tmp;
+
+	list_for_each_entry_safe(op, tmp, oplist, list) {
+		unsigned long insn;
+		WARN_ON(kprobe_disabled(&op->kp));
+
+		/*
+		 * Backup instructions which will be replaced
+		 * by jump address
+		 */
+		memcpy(op->optinsn.copied_insn, op->kp.addr,
+				RELATIVEJUMP_SIZE);
+
+		insn = arm_gen_branch((unsigned long)op->kp.addr,
+				(unsigned long)op->optinsn.insn);
+		BUG_ON(insn == 0);

arch/arm/probes/kprobes/opt-arm.c line 280

Quoted 10 lines
arch/arm/probes/kprobes/opt-arm.c new file +10 -0
+
+		/*
+		 * Make it a conditional branch if replaced insn
+		 * is consitional
+		 */
+		insn = (__mem_to_opcode_arm(
+			  op->optinsn.copied_insn[0]) & 0xf0000000) |
+			(insn & 0x0fffffff);
+
+		patch_text(op->kp.addr, insn);

patch_text() itself may use stop_machine under certain circumstances, and if it were to do so, I believe that would cause the system to lock/panic. So, this should be __patch_text() instead, but we would also need to take care of the cache_ops_need_broadcast() case, where all CPU's need to invalidate their own caches and we can't rely on just one CPU executing the code patching whilst other CPUs spin and wait. Though to make life easier, we could just not optimise kprobes in the legacy cache_ops_need_broadcast() case.

Quoted 17 lines

arch/arm/probes/kprobes/opt-arm.c line 290

Quoted 10 lines
arch/arm/probes/kprobes/opt-arm.c new file +10 -0
+
+		list_del_init(&op->list);
+	}
+	return 0;
+}
+
+void arch_optimize_kprobes(struct list_head *oplist)
+{
+	stop_machine(__arch_optimize_kprobes, oplist, cpu_online_mask);
+}

I believe passing cpu_online_mask above will cause __arch_optimize_kprobes to be executed on every CPU, is this safe? If it is, it's a serendipitous optimisation if each CPU can process different probes in the list. If it's not safe, this needs to be NULL instead so only one CPU executes the code.

Quoted 2 lines

This stop_machine() call is copied from arch_disarm_kprobe, I think their senario should be similar.

arch_disarm_kprobe is just executing __patch_text on each cpu, which pokes a word of memory with a new value and flushes caches for it.

arch_optimize_kprobes is calling __arch_optimize_kprobes, which is iterating over a list of probes and removing each one in turn, if this is happening on multiple cpu's simultaneously, it's not clear to me that such an operation is safe. list_del_init calls __list_del which does

	next->prev = prev;
	prev->next = next;

so what happens if another cpu is at the same time updating any of those list entries? Without even fully analysing the code I can see that with the fact that the list handling helpers have no memory barriers, that the above two lines could be seen to execute in the reverse order, e.g.

	prev->next = next;
	next->prev = prev;
Quoted 7 lines

so another CPU could find and delete next before this one has finished doing so. Would the list end up in a consistent state where no loops develop and no probes are missed? I don't know the answer and a full analysis would be complicated, but my gut feeling is that if a cpu can observe the links in the list in an inconsistent state then only bad things can result.

I see the problem.

I'm thinking about making core.c and opt-arm.c to share stop_machine() code. stop_machine() is required when removing breakpoint, so I'd like to define a "remove_breakpoint" function in core.c and make opt-arm.c to call it. Do you think it is a good idea?

author
#12 lore raw

Re: [PATCH v14 7/7] ARM: kprobes: enable OPTPROBES for ARM 32

Quoted 4 lines · Wang Nan
Quoted 3 lines · Jon Medhurst (Tixy)
Quoted 2 lines · Wang Nan
Quoted 1 line · Jon Medhurst (Tixy)

On Mon, 2014-12-08 at 14:28 +0800, Wang Nan wrote:

[...]

Quoted 9 lines
Quoted 8 lines

so another CPU could find and delete next before this one has finished doing so. Would the list end up in a consistent state where no loops develop and no probes are missed? I don't know the answer and a full analysis would be complicated, but my gut feeling is that if a cpu can observe the links in the list in an inconsistent state then only bad things can result.

Quoted 8 lines

I see the problem.

I'm thinking about making core.c and opt-arm.c to share stop_machine() code. stop_machine() is required when removing breakpoint, so I'd like to define a "remove_breakpoint" function in core.c and make opt-arm.c to call it. Do you think it is a good idea?

What I mean is something like this:

arch/arm/probes/kprobes/core.c +19 -7
index 3a58db4..efd8ab1 100644
@@ -163,19 +163,31 @@ void __kprobes arch_arm_kprobe(struct kprobe *p)
  * memory. It is also needed to atomically set the two half-words of a 32-bit
  * Thumb breakpoint.
  */
-int __kprobes __arch_disarm_kprobe(void *p)
-{
-	struct kprobe *kp = p;
-	void *addr = (void *)((uintptr_t)kp->addr & ~1);
-
-	__patch_text(addr, kp->opcode);
+struct patch {
+	void *addr;
+	unsigned int insn;
+};
 
+static int __remove_breakpoint(void *data)
+{
+	struct patch *p = data;
+	__patch_text(p->addr, p->insn);
 	return 0;
 }
 
+void __kprobes remove_breakpoint(void *addr, unsigned int insn)
+{
+	struct patch p = {
+		.addr = addr,
+		.insn = insn,
+	};
+	stop_machine(__remove_breakpoint, &p, cpu_online_mask);
+}
+
 void __kprobes arch_disarm_kprobe(struct kprobe *p)
 {
-	stop_machine(__arch_disarm_kprobe, p, cpu_online_mask);
+	remove_breakpoint((void *)((uintptr_t)p->addr & ~1),
+			p->opcode);
 }
 
 void __kprobes arch_remove_kprobe(struct kprobe *p)
arch/arm/probes/kprobes/core.h +2 -0
index f88c79f..7b7c334 100644
@@ -30,6 +30,8 @@
 #define KPROBE_THUMB16_BREAKPOINT_INSTRUCTION	0xde18
 #define KPROBE_THUMB32_BREAKPOINT_INSTRUCTION	0xf7f0a018
 
+extern void remove_breakpoint(void *addr, unsigned int insn);
+
 enum probes_insn __kprobes
 kprobe_decode_ldmstm(kprobe_opcode_t insn, struct arch_probes_insn *asi,
 		const struct decode_header *h);
arch/arm/probes/kprobes/opt-arm.c +10 -15
index afbfeef..a1a1882 100644
@@ -28,8 +28,9 @@
 #include <asm/insn.h>
 /* for patch_text */
 #include <asm/patch.h>
-/* for stop_machine */
-#include <linux/stop_machine.h>
+
+#include "core.h"
+
 /*
  * NOTE: the first sub and add instruction will be modified according
  * to the stack cost of the instruction.
@@ -245,13 +246,8 @@ int arch_prepare_optimized_kprobe(struct optimized_kprobe *op, struct kprobe *or
 	return 0;
 }
 
-/*
- * Similar to __arch_disarm_kprobe, operations which removing
- * breakpoints must be wrapped by stop_machine to avoid racing.
- */
-static __kprobes int __arch_optimize_kprobes(void *p)
+void __kprobes arch_optimize_kprobes(struct list_head *oplist)
 {
-	struct list_head *oplist = p;
 	struct optimized_kprobe *op, *tmp;
 
 	list_for_each_entry_safe(op, tmp, oplist, list) {
@@ -277,16 +273,15 @@ static __kprobes int __arch_optimize_kprobes(void *p)
 			  op->optinsn.copied_insn[0]) & 0xf0000000) |
 			(insn & 0x0fffffff);
 
-		patch_text(op->kp.addr, insn);
+		/*
+		 * Similar to __arch_disarm_kprobe, operations which
+		 * removing breakpoints must be wrapped by stop_machine
+		 * to avoid racing.
+		 */
+		remove_breakpoint(op->kp.addr, insn);
 
 		list_del_init(&op->list);
 	}
-	return 0;
-}
-
-void arch_optimize_kprobes(struct list_head *oplist)
-{
-	stop_machine(__arch_optimize_kprobes, oplist, cpu_online_mask);
 }
 
 void arch_unoptimize_kprobe(struct optimized_kprobe *op)
Quoted 6 lines

--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in the body of a message to majordomo at vger.kernel.org
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/

#13 lore raw

Re: [PATCH v14 7/7] ARM: kprobes: enable OPTPROBES for ARM 32

Quoted 17 lines · Wang Nan
Quoted 4 lines · Wang Nan
Quoted 3 lines · Jon Medhurst (Tixy)
Quoted 2 lines · Wang Nan
Quoted 1 line · Jon Medhurst (Tixy)

On Mon, 2014-12-08 at 14:28 +0800, Wang Nan wrote:

[...]

Quoted 11 lines
Quoted 8 lines

so another CPU could find and delete next before this one has finished doing so. Would the list end up in a consistent state where no loops develop and no probes are missed? I don't know the answer and a full analysis would be complicated, but my gut feeling is that if a cpu can observe the links in the list in an inconsistent state then only bad things can result.

I see the problem.

Quoted 8 lines
Quoted 6 lines

I'm thinking about making core.c and opt-arm.c to share stop_machine() code. stop_machine() is required when removing breakpoint, so I'd like to define a "remove_breakpoint" function in core.c and make opt-arm.c to call it. Do you think it is a good idea?

What I mean is something like this:

Yes, that should work, though as remove_breakpoint is a globally visible symbol, I suggest a less generic name for it, perhaps remove_kprobe_breakpoint ?

Signature · 1 line
Tixy
Quoted 110 lines
arch/arm/probes/kprobes/core.c +19 -7
index 3a58db4..efd8ab1 100644
@@ -163,19 +163,31 @@ void __kprobes arch_arm_kprobe(struct kprobe *p)
  * memory. It is also needed to atomically set the two half-words of a 32-bit
  * Thumb breakpoint.
  */
-int __kprobes __arch_disarm_kprobe(void *p)
-{
-	struct kprobe *kp = p;
-	void *addr = (void *)((uintptr_t)kp->addr & ~1);
-
-	__patch_text(addr, kp->opcode);
+struct patch {
+	void *addr;
+	unsigned int insn;
+};
 
+static int __remove_breakpoint(void *data)
+{
+	struct patch *p = data;
+	__patch_text(p->addr, p->insn);
 	return 0;
 }
 
+void __kprobes remove_breakpoint(void *addr, unsigned int insn)
+{
+	struct patch p = {
+		.addr = addr,
+		.insn = insn,
+	};
+	stop_machine(__remove_breakpoint, &p, cpu_online_mask);
+}
+
 void __kprobes arch_disarm_kprobe(struct kprobe *p)
 {
-	stop_machine(__arch_disarm_kprobe, p, cpu_online_mask);
+	remove_breakpoint((void *)((uintptr_t)p->addr & ~1),
+			p->opcode);
 }
 
 void __kprobes arch_remove_kprobe(struct kprobe *p)
arch/arm/probes/kprobes/core.h +2 -0
index f88c79f..7b7c334 100644
@@ -30,6 +30,8 @@
 #define KPROBE_THUMB16_BREAKPOINT_INSTRUCTION	0xde18
 #define KPROBE_THUMB32_BREAKPOINT_INSTRUCTION	0xf7f0a018
 
+extern void remove_breakpoint(void *addr, unsigned int insn);
+
 enum probes_insn __kprobes
 kprobe_decode_ldmstm(kprobe_opcode_t insn, struct arch_probes_insn *asi,
 		const struct decode_header *h);
arch/arm/probes/kprobes/opt-arm.c +10 -15
index afbfeef..a1a1882 100644
@@ -28,8 +28,9 @@
 #include <asm/insn.h>
 /* for patch_text */
 #include <asm/patch.h>
-/* for stop_machine */
-#include <linux/stop_machine.h>
+
+#include "core.h"
+
 /*
  * NOTE: the first sub and add instruction will be modified according
  * to the stack cost of the instruction.
@@ -245,13 +246,8 @@ int arch_prepare_optimized_kprobe(struct optimized_kprobe *op, struct kprobe *or
 	return 0;
 }
 
-/*
- * Similar to __arch_disarm_kprobe, operations which removing
- * breakpoints must be wrapped by stop_machine to avoid racing.
- */
-static __kprobes int __arch_optimize_kprobes(void *p)
+void __kprobes arch_optimize_kprobes(struct list_head *oplist)
 {
-	struct list_head *oplist = p;
 	struct optimized_kprobe *op, *tmp;
 
 	list_for_each_entry_safe(op, tmp, oplist, list) {
@@ -277,16 +273,15 @@ static __kprobes int __arch_optimize_kprobes(void *p)
 			  op->optinsn.copied_insn[0]) & 0xf0000000) |
 			(insn & 0x0fffffff);
 
-		patch_text(op->kp.addr, insn);
+		/*
+		 * Similar to __arch_disarm_kprobe, operations which
+		 * removing breakpoints must be wrapped by stop_machine
+		 * to avoid racing.
+		 */
+		remove_breakpoint(op->kp.addr, insn);
 
 		list_del_init(&op->list);
 	}
-	return 0;
-}
-
-void arch_optimize_kprobes(struct list_head *oplist)
-{
-	stop_machine(__arch_optimize_kprobes, oplist, cpu_online_mask);
 }
 
 void arch_unoptimize_kprobe(struct optimized_kprobe *op)
author
#14 lore raw

Re: [PATCH v14 7/7] ARM: kprobes: enable OPTPROBES for ARM 32

Quoted 25 lines · Jon Medhurst (Tixy)
Quoted 24 lines · Wang Nan
Quoted 4 lines · Wang Nan
Quoted 3 lines · Jon Medhurst (Tixy)
Quoted 2 lines · Wang Nan
Quoted 1 line · Jon Medhurst (Tixy)

On Mon, 2014-12-08 at 14:28 +0800, Wang Nan wrote:

[...]

Quoted 17 lines
Quoted 8 lines

so another CPU could find and delete next before this one has finished doing so. Would the list end up in a consistent state where no loops develop and no probes are missed? I don't know the answer and a full analysis would be complicated, but my gut feeling is that if a cpu can observe the links in the list in an inconsistent state then only bad things can result.

I see the problem.

I'm thinking about making core.c and opt-arm.c to share stop_machine() code. stop_machine() is required when removing breakpoint, so I'd like to define a "remove_breakpoint" function in core.c and make opt-arm.c to call it. Do you think it is a good idea?

Quoted 6 lines
Quoted 1 line

What I mean is something like this:

Yes, that should work, though as remove_breakpoint is a globally visible symbol, I suggest a less generic name for it, perhaps remove_kprobe_breakpoint ?

I don't think it is globally visible. Only files in arm/probes/kprobes can include "core.h". However, I do agree that remove_breakpoint() is not a good name. In my v15 patch, I'd like to rename it to kprobe_remove_breakpoint(), due to may of names defined in core.h are called kprobes_xxx.

Thank you!

#15 lore raw

Re: Re: [PATCH v14 7/7] ARM: kprobes: enable OPTPROBES for ARM 32

In reply to Jon Medhurst (Tixy)

(2014/12/08 20:50), Jon Medhurst (Tixy) wrote:> arch_optimize_kprobes is calling __arch_optimize_kprobes, which is

Quoted 15 lines

iterating over a list of probes and removing each one in turn, if this is happening on multiple cpu's simultaneously, it's not clear to me that such an operation is safe. list_del_init calls __list_del which does

	next->prev = prev;
	prev->next = next;

so what happens if another cpu is at the same time updating any of those list entries? Without even fully analysing the code I can see that with the fact that the list handling helpers have no memory barriers, that the above two lines could be seen to execute in the reverse order, e.g.

	prev->next = next;
	next->prev = prev;
Quoted 6 lines

so another CPU could find and delete next before this one has finished doing so. Would the list end up in a consistent state where no loops develop and no probes are missed? I don't know the answer and a full analysis would be complicated, but my gut feeling is that if a cpu can observe the links in the list in an inconsistent state then only bad things can result.

Just a comment, arch_optimize_kprobes() are only called under kprobe_mutex held. No concurrent update happens :)

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

Re: Re: [PATCH v14 7/7] ARM: kprobes: enable OPTPROBES for ARM 32

Quoted 23 lines · Masami Hiramatsu

(2014/12/08 20:50), Jon Medhurst (Tixy) wrote:> arch_optimize_kprobes is calling __arch_optimize_kprobes, which is

Quoted 15 lines

iterating over a list of probes and removing each one in turn, if this is happening on multiple cpu's simultaneously, it's not clear to me that such an operation is safe. list_del_init calls __list_del which does

	next->prev = prev;
	prev->next = next;

so what happens if another cpu is at the same time updating any of those list entries? Without even fully analysing the code I can see that with the fact that the list handling helpers have no memory barriers, that the above two lines could be seen to execute in the reverse order, e.g.

	prev->next = next;
	next->prev = prev;
Quoted 6 lines

so another CPU could find and delete next before this one has finished doing so. Would the list end up in a consistent state where no loops develop and no probes are missed? I don't know the answer and a full analysis would be complicated, but my gut feeling is that if a cpu can observe the links in the list in an inconsistent state then only bad things can result.

Quoted 2 lines

Just a comment, arch_optimize_kprobes() are only called under kprobe_mutex held. No concurrent update happens :)

Except in the case of the code I was commenting on which was using stop_machine to make all cpu's simultaneously do the work of arch_optimize_kprobes :-)

Signature · 1 line
Tixy
#17 lore raw

Re: [PATCH v14 7/7] ARM: kprobes: enable OPTPROBES for ARM 32

Quoted 24 lines · Jon Medhurst (Tixy)
Quoted 23 lines · Masami Hiramatsu

(2014/12/08 20:50), Jon Medhurst (Tixy) wrote:> arch_optimize_kprobes is calling __arch_optimize_kprobes, which is

Quoted 21 lines

iterating over a list of probes and removing each one in turn, if this is happening on multiple cpu's simultaneously, it's not clear to me that such an operation is safe. list_del_init calls __list_del which does

	next->prev = prev;
	prev->next = next;

so what happens if another cpu is at the same time updating any of those list entries? Without even fully analysing the code I can see that with the fact that the list handling helpers have no memory barriers, that the above two lines could be seen to execute in the reverse order, e.g.

	prev->next = next;
	next->prev = prev;

so another CPU could find and delete next before this one has finished doing so. Would the list end up in a consistent state where no loops develop and no probes are missed? I don't know the answer and a full analysis would be complicated, but my gut feeling is that if a cpu can observe the links in the list in an inconsistent state then only bad things can result.

Quoted 6 lines
Quoted 2 lines

Just a comment, arch_optimize_kprobes() are only called under kprobe_mutex held. No concurrent update happens :)

Except in the case of the code I was commenting on which was using stop_machine to make all cpu's simultaneously do the work of arch_optimize_kprobes :-)

Ah, right! stop_machine with cpu_online_mask cause that problem.

Thanks,

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