Skip to contentSkip to conversation

uprobes: Add uprobes support for ARM

PATCH Superseded David Long 5 participants 61 messages Oct 15, 2013 – Nov 15, 2013

Subsystems (7)
Arm (32-bit) x86 PowerPC perf events +3 more subsystems

v2 of 7 Superseded Latest is v7 →

lore
Share & tools
lore

David Long

author
#0 lore raw

v2 cover uprobes: Add uprobes support for ARM

From: "David A. Long" <[email protected]>

This patch series adds basic uprobes support to ARM. It is based on patches developed earlier by Rabin Vincent. That approach of adding hooks into the kprobes instruction parsing code was not well received. This approach separates the ARM instruction parsing code in kprobes out into a separate set of functions which can be used by both kprobes and uprobes. Both kprobes and uprobes then provide their own semantic action tables to process the results of the parsing.

The following changes have been made for v2:

1) The last dependencies of uprobes on kprobes have been removed and the two features may now be independently configured.
2) The larger changes have been separated into a few stages.
3) Use of "void *" has been replaced with an appropriate pointer type in various places.
4) The arm kprobes-test module has been changed to work better with a thumb-mode kernel.

These patches are based on v3.12-rc5.

David A. Long (11):
uprobes: move function declarations out of arch
uprobes: allow ignoring of probe hits
uprobes: allow arch access to xol slot
ARM: move shared uprobe/kprobe definitions into new include file
ARM: move generic thumb instruction parsing code to new files for use
by other features
ARM: use a function table for determining instruction interpreter
actions
ARM: Disable jprobe selftests in thumb kernels
kprobes: Remove uneeded kernel dependency on struct arch_specific_insn
ARM: Finish renaming ARM kprobes APIs for sharing with uprobes
ARM: add uprobes support
ARM: Remove uprobes dependency on kprobes

Rabin Vincent (2):
uprobes: allow arch-specific initialization
uprobes: add arch write opcode hook

 arch/arm/Kconfig                   |    3 +
 arch/arm/include/asm/kprobes.h     |   15 +-
 arch/arm/include/asm/probes.h      |   25 +
 arch/arm/include/asm/ptrace.h      |    6 +
 arch/arm/include/asm/thread_info.h |    5 +-
 arch/arm/include/asm/uprobes.h     |   34 ++
 arch/arm/kernel/Makefile           |    7 +-
 arch/arm/kernel/kprobes-arm.c      |  813 +++----------------------
 arch/arm/kernel/kprobes-common.c   |  469 +--------------
 arch/arm/kernel/kprobes-test.c     |   13 +-
 arch/arm/kernel/kprobes-thumb.c    | 1156 ++++++------------------------------
 arch/arm/kernel/kprobes.c          |   25 +-
 arch/arm/kernel/kprobes.h          |  397 +------------
 arch/arm/kernel/probes-arm.c       |  742 +++++++++++++++++++++++
 arch/arm/kernel/probes-arm.h       |   72 +++
 arch/arm/kernel/probes-thumb.c     |  877 +++++++++++++++++++++++++++
 arch/arm/kernel/probes-thumb.h     |   95 +++
 arch/arm/kernel/probes.c           |  459 ++++++++++++++
 arch/arm/kernel/probes.h           |  395 ++++++++++++
 arch/arm/kernel/signal.c           |    4 +
 arch/arm/kernel/uprobes-arm.c      |  220 +++++++
 arch/arm/kernel/uprobes.c          |  201 +++++++
 arch/arm/kernel/uprobes.h          |   25 +
 arch/powerpc/include/asm/uprobes.h |    1 -
 arch/x86/include/asm/uprobes.h     |    7 -
 include/linux/kprobes.h            |    7 +-
 include/linux/uprobes.h            |   17 +
 kernel/events/uprobes.c            |   58 +-
 28 files changed, 3573 insertions(+), 2575 deletions(-)
 create mode 100644 arch/arm/include/asm/probes.h
 create mode 100644 arch/arm/include/asm/uprobes.h
 create mode 100644 arch/arm/kernel/probes-arm.c
 create mode 100644 arch/arm/kernel/probes-arm.h
 create mode 100644 arch/arm/kernel/probes-thumb.c
 create mode 100644 arch/arm/kernel/probes-thumb.h
 create mode 100644 arch/arm/kernel/probes.c
 create mode 100644 arch/arm/kernel/probes.h
 create mode 100644 arch/arm/kernel/uprobes-arm.c
 create mode 100644 arch/arm/kernel/uprobes.c
 create mode 100644 arch/arm/kernel/uprobes.h
Signature · 1 line
1.8.1.2
author
#1 lore raw

PATCH v2 1/13 uprobes: move function declarations out of arch

From: "David A. Long" <[email protected]>

Move the function declarations from the arch header to the common header, since only the function bodies are architecture-specific. These changes are from Vincent Rabin's uprobes patch.

---
 arch/x86/include/asm/uprobes.h | 7 -------
 include/linux/uprobes.h        | 8 ++++++++
 2 files changed, 8 insertions(+), 7 deletions(-)
arch/x86/include/asm/uprobes.h +0 -7
index 6e51979..b20b4d6 100644
@@ -49,11 +49,4 @@ struct arch_uprobe_task {
 	unsigned int			saved_tf;
 };
 
-extern int  arch_uprobe_analyze_insn(struct arch_uprobe *aup, struct mm_struct *mm, unsigned long addr);
-extern int  arch_uprobe_pre_xol(struct arch_uprobe *aup, struct pt_regs *regs);
-extern int  arch_uprobe_post_xol(struct arch_uprobe *aup, struct pt_regs *regs);
-extern bool arch_uprobe_xol_was_trapped(struct task_struct *tsk);
-extern int  arch_uprobe_exception_notify(struct notifier_block *self, unsigned long val, void *data);
-extern void arch_uprobe_abort_xol(struct arch_uprobe *aup, struct pt_regs *regs);
-extern unsigned long arch_uretprobe_hijack_return_addr(unsigned long trampoline_vaddr, struct pt_regs *regs);
 #endif	/* _ASM_UPROBES_H */
include/linux/uprobes.h +8 -0
index 06f28be..9fd60c5 100644
@@ -30,6 +30,7 @@
 struct vm_area_struct;
 struct mm_struct;
 struct inode;
+struct notifier_block;
 
 #ifdef CONFIG_ARCH_SUPPORTS_UPROBES
 # include <asm/uprobes.h>
@@ -125,6 +126,13 @@ extern void uprobe_notify_resume(struct pt_regs *regs);
 extern bool uprobe_deny_signal(void);
 extern bool __weak arch_uprobe_skip_sstep(struct arch_uprobe *aup, struct pt_regs *regs);
 extern void uprobe_clear_state(struct mm_struct *mm);
+extern int  arch_uprobe_analyze_insn(struct arch_uprobe *aup, struct mm_struct *mm, unsigned long addr);
+extern int  arch_uprobe_pre_xol(struct arch_uprobe *aup, struct pt_regs *regs);
+extern int  arch_uprobe_post_xol(struct arch_uprobe *aup, struct pt_regs *regs);
+extern bool arch_uprobe_xol_was_trapped(struct task_struct *tsk);
+extern int  arch_uprobe_exception_notify(struct notifier_block *self, unsigned long val, void *data);
+extern void arch_uprobe_abort_xol(struct arch_uprobe *aup, struct pt_regs *regs);
+extern unsigned long arch_uretprobe_hijack_return_addr(unsigned long trampoline_vaddr, struct pt_regs *regs);
 #else /* !CONFIG_UPROBES */
 struct uprobes_state {
 };
Signature · 1 line
1.8.1.2
maintainer
#2 lore raw

Re: [PATCH v2 01/13] uprobes: move function declarations out of arch

Quoted 4 lines · David Long

Move the function declarations from the arch header to the common header, since only the function bodies are architecture-specific. These changes are from Vincent Rabin's uprobes patch.

Hmm. I didn't look at this patch because I thought it is obviously fine, but...

Quoted 3 lines
 arch/x86/include/asm/uprobes.h | 7 -------
 include/linux/uprobes.h        | 8 ++++++++
 2 files changed, 8 insertions(+), 7 deletions(-)

it should also update arch/powerpc/include/asm/uprobes.h ?

And 12/13 should not touch arch/powerpc/ or include/linux/uprobes.h.

Oleg.

maintainer
#3 lore raw

Re: [PATCH v2 01/13] uprobes: move function declarations out of arch

Quoted 10 lines · Oleg Nesterov
Quoted 4 lines · David Long

Move the function declarations from the arch header to the common header, since only the function bodies are architecture-specific. These changes are from Vincent Rabin's uprobes patch.

Hmm. I didn't look at this patch because I thought it is obviously fine, but...

Quoted 7 lines
Quoted 3 lines
 arch/x86/include/asm/uprobes.h | 7 -------
 include/linux/uprobes.h        | 8 ++++++++
 2 files changed, 8 insertions(+), 7 deletions(-)

it should also update arch/powerpc/include/asm/uprobes.h ?

And 12/13 should not touch arch/powerpc/ or include/linux/uprobes.h.

And I think we can merge this change separately to reduce the number of non-arm changes in the next version.

Please see the patch I am going to add to my tree below.

Oleg.
---

Subject: [PATCH] uprobes: move function declarations out of arch
From: David A. Long <[email protected]>
Date: Tue, 15 Oct 2013 17:04:16 -0400

Move the function declarations from the arch headers to the common header, since only the function bodies are architecture-specific. These changes are from Vincent Rabin's uprobes patch.

[ oleg: update arch/powerpc/include/asm/uprobes.h ]

---
 arch/powerpc/include/asm/uprobes.h |    7 -------
 arch/x86/include/asm/uprobes.h     |    7 -------
 include/linux/uprobes.h            |    8 ++++++++
 3 files changed, 8 insertions(+), 14 deletions(-)
arch/powerpc/include/asm/uprobes.h +0 -7
index 541fd6f..75c6ecd 100644
@@ -46,11 +46,4 @@ struct arch_uprobe_task {
 	unsigned long	saved_trap_nr;
 };
 
-extern int  arch_uprobe_analyze_insn(struct arch_uprobe *aup, struct mm_struct *mm, unsigned long addr);
-extern int  arch_uprobe_pre_xol(struct arch_uprobe *aup, struct pt_regs *regs);
-extern int  arch_uprobe_post_xol(struct arch_uprobe *aup, struct pt_regs *regs);
-extern bool arch_uprobe_xol_was_trapped(struct task_struct *tsk);
-extern int  arch_uprobe_exception_notify(struct notifier_block *self, unsigned long val, void *data);
-extern void arch_uprobe_abort_xol(struct arch_uprobe *aup, struct pt_regs *regs);
-extern unsigned long arch_uretprobe_hijack_return_addr(unsigned long trampoline_vaddr, struct pt_regs *regs);
 #endif	/* _ASM_UPROBES_H */
arch/x86/include/asm/uprobes.h +0 -7
index 2a24180..3087ea9 100644
@@ -52,11 +52,4 @@ struct arch_uprobe_task {
 	unsigned int			saved_tf;
 };
 
-extern int  arch_uprobe_analyze_insn(struct arch_uprobe *aup, struct mm_struct *mm, unsigned long addr);
-extern int  arch_uprobe_pre_xol(struct arch_uprobe *aup, struct pt_regs *regs);
-extern int  arch_uprobe_post_xol(struct arch_uprobe *aup, struct pt_regs *regs);
-extern bool arch_uprobe_xol_was_trapped(struct task_struct *tsk);
-extern int  arch_uprobe_exception_notify(struct notifier_block *self, unsigned long val, void *data);
-extern void arch_uprobe_abort_xol(struct arch_uprobe *aup, struct pt_regs *regs);
-extern unsigned long arch_uretprobe_hijack_return_addr(unsigned long trampoline_vaddr, struct pt_regs *regs);
 #endif	/* _ASM_UPROBES_H */
include/linux/uprobes.h +8 -0
index 06f28be..9fd60c5 100644
@@ -30,6 +30,7 @@
 struct vm_area_struct;
 struct mm_struct;
 struct inode;
+struct notifier_block;
 
 #ifdef CONFIG_ARCH_SUPPORTS_UPROBES
 # include <asm/uprobes.h>
@@ -125,6 +126,13 @@ extern void uprobe_notify_resume(struct pt_regs *regs);
 extern bool uprobe_deny_signal(void);
 extern bool __weak arch_uprobe_skip_sstep(struct arch_uprobe *aup, struct pt_regs *regs);
 extern void uprobe_clear_state(struct mm_struct *mm);
+extern int  arch_uprobe_analyze_insn(struct arch_uprobe *aup, struct mm_struct *mm, unsigned long addr);
+extern int  arch_uprobe_pre_xol(struct arch_uprobe *aup, struct pt_regs *regs);
+extern int  arch_uprobe_post_xol(struct arch_uprobe *aup, struct pt_regs *regs);
+extern bool arch_uprobe_xol_was_trapped(struct task_struct *tsk);
+extern int  arch_uprobe_exception_notify(struct notifier_block *self, unsigned long val, void *data);
+extern void arch_uprobe_abort_xol(struct arch_uprobe *aup, struct pt_regs *regs);
+extern unsigned long arch_uretprobe_hijack_return_addr(unsigned long trampoline_vaddr, struct pt_regs *regs);
 #else /* !CONFIG_UPROBES */
 struct uprobes_state {
 };
Signature · 1 line
1.5.5.1
maintainer
#4 lore raw

PATCH 1/1 uprobes: Export write_opcode() as uprobe_write_opcode()

Quoted 3 lines · Oleg Nesterov

And I think we can merge this change separately to reduce the number of non-arm changes in the next version.

And if you think this can help, I can push another change right now, see below.

This way v3 will only need a single change outside of arch/arm, arch_uprobe_ignore(), and this change should be trivial.

Oleg.
---

Subject: [PATCH] uprobes: Export write_opcode() as uprobe_write_opcode()
From: Oleg Nesterov <[email protected]>
Date: Tue, 5 Nov 2013 19:50:39 +0100

set_swbp() and set_orig_insn() are __weak, but this is pointless because write_opcode() is static.

Export write_opcode() as uprobe_write_opcode() for the upcoming arm port, this way it can actually override set_swbp() and use __opcode_to_mem_arm(bpinsn) instead if UPROBE_SWBP_INSN.

---
 include/linux/uprobes.h |    1 +
 kernel/events/uprobes.c |   14 +++++++-------
 2 files changed, 8 insertions(+), 7 deletions(-)
include/linux/uprobes.h +1 -0
index 9fd60c5..6d62577 100644
@@ -109,6 +109,7 @@ extern int __weak set_swbp(struct arch_uprobe *aup, struct mm_struct *mm, unsign
 extern int __weak set_orig_insn(struct arch_uprobe *aup, struct mm_struct *mm, unsigned long vaddr);
 extern bool __weak is_swbp_insn(uprobe_opcode_t *insn);
 extern bool __weak is_trap_insn(uprobe_opcode_t *insn);
+extern int uprobe_write_opcode(struct mm_struct *mm, unsigned long vaddr, uprobe_opcode_t);
 extern int uprobe_register(struct inode *inode, loff_t offset, struct uprobe_consumer *uc);
 extern int uprobe_apply(struct inode *inode, loff_t offset, struct uprobe_consumer *uc, bool);
 extern void uprobe_unregister(struct inode *inode, loff_t offset, struct uprobe_consumer *uc);
kernel/events/uprobes.c +7 -7
index 6aef5ad..9081d15 100644
@@ -244,12 +244,12 @@ static int verify_opcode(struct page *page, unsigned long vaddr, uprobe_opcode_t
  * the architecture. If an arch has variable length instruction and the
  * breakpoint instruction is not of the smallest length instruction
  * supported by that architecture then we need to modify is_trap_at_addr and
- * write_opcode accordingly. This would never be a problem for archs that
- * have fixed length instructions.
+ * uprobe_write_opcode accordingly. This would never be a problem for archs
+ * that have fixed length instructions.
  */
 
 /*
- * write_opcode - write the opcode at a given virtual address.
+ * uprobe_write_opcode - write the opcode at a given virtual address.
  * @mm: the probed process address space.
  * @vaddr: the virtual address to store the opcode.
  * @opcode: opcode to be written at @vaddr.
@@ -260,7 +260,7 @@ static int verify_opcode(struct page *page, unsigned long vaddr, uprobe_opcode_t
  * For mm @mm, write the opcode at @vaddr.
  * Return 0 (success) or a negative errno.
  */
-static int write_opcode(struct mm_struct *mm, unsigned long vaddr,
+int uprobe_write_opcode(struct mm_struct *mm, unsigned long vaddr,
 			uprobe_opcode_t opcode)
 {
 	struct page *old_page, *new_page;
@@ -314,7 +314,7 @@ put_old:
  */
 int __weak set_swbp(struct arch_uprobe *auprobe, struct mm_struct *mm, unsigned long vaddr)
 {
-	return write_opcode(mm, vaddr, UPROBE_SWBP_INSN);
+	return uprobe_write_opcode(mm, vaddr, UPROBE_SWBP_INSN);
 }
 
 /**
@@ -329,7 +329,7 @@ int __weak set_swbp(struct arch_uprobe *auprobe, struct mm_struct *mm, unsigned
 int __weak
 set_orig_insn(struct arch_uprobe *auprobe, struct mm_struct *mm, unsigned long vaddr)
 {
-	return write_opcode(mm, vaddr, *(uprobe_opcode_t *)auprobe->insn);
+	return uprobe_write_opcode(mm, vaddr, *(uprobe_opcode_t *)auprobe->insn);
 }
 
 static int match_uprobe(struct uprobe *l, struct uprobe *r)
@@ -576,7 +576,7 @@ static int prepare_uprobe(struct uprobe *uprobe, struct file *file,
 	if (ret)
 		goto out;
 
-	/* write_opcode() assumes we don't cross page boundary */
+	/* uprobe_write_opcode() assumes we don't cross page boundary */
 	BUG_ON((uprobe->offset & ~PAGE_MASK) +
 			UPROBE_SWBP_INSN_SIZE > PAGE_SIZE);
 
Signature · 1 line
1.5.5.1
author
#5 lore raw

Re: [PATCH] uprobes: Export write_opcode() as uprobe_write_opcode()

Quoted 5 lines · Oleg Nesterov
Quoted 3 lines · Oleg Nesterov

And I think we can merge this change separately to reduce the number of non-arm changes in the next version.

Quoted 8 lines

And if you think this can help, I can push another change right now, see below.

This way v3 will only need a single change outside of arch/arm, arch_uprobe_ignore(), and this change should be trivial.

Oleg.

Yes, I agree it could only help if you merge that now instead of waiting for my patchset. I have built and tested your patch in place of mine (on ARM) and it looks good.

-dl

author
#6 lore raw

Re: [PATCH v2 01/13] uprobes: move function declarations out of arch

In reply to Oleg Nesterov
Quoted 95 lines · Oleg Nesterov
Quoted 10 lines · Oleg Nesterov
Quoted 4 lines · David Long

Move the function declarations from the arch header to the common header, since only the function bodies are architecture-specific. These changes are from Vincent Rabin's uprobes patch.

Hmm. I didn't look at this patch because I thought it is obviously fine, but...

Quoted 7 lines
Quoted 3 lines
  arch/x86/include/asm/uprobes.h | 7 -------
  include/linux/uprobes.h        | 8 ++++++++
  2 files changed, 8 insertions(+), 7 deletions(-)

it should also update arch/powerpc/include/asm/uprobes.h ?

And 12/13 should not touch arch/powerpc/ or include/linux/uprobes.h.

And I think we can merge this change separately to reduce the number of non-arm changes in the next version.

Please see the patch I am going to add to my tree below.

Oleg.
---

Subject: [PATCH] uprobes: move function declarations out of arch
From: David A. Long <[email protected]>
Date: Tue, 15 Oct 2013 17:04:16 -0400

Move the function declarations from the arch headers to the common header, since only the function bodies are architecture-specific. These changes are from Vincent Rabin's uprobes patch.

[ oleg: update arch/powerpc/include/asm/uprobes.h ]

---
  arch/powerpc/include/asm/uprobes.h |    7 -------
  arch/x86/include/asm/uprobes.h     |    7 -------
  include/linux/uprobes.h            |    8 ++++++++
  3 files changed, 8 insertions(+), 14 deletions(-)
arch/powerpc/include/asm/uprobes.h +0 -7
index 541fd6f..75c6ecd 100644
@@ -46,11 +46,4 @@ struct arch_uprobe_task {
  	unsigned long	saved_trap_nr;
  };
 
-extern int  arch_uprobe_analyze_insn(struct arch_uprobe *aup, struct mm_struct *mm, unsigned long addr);
-extern int  arch_uprobe_pre_xol(struct arch_uprobe *aup, struct pt_regs *regs);
-extern int  arch_uprobe_post_xol(struct arch_uprobe *aup, struct pt_regs *regs);
-extern bool arch_uprobe_xol_was_trapped(struct task_struct *tsk);
-extern int  arch_uprobe_exception_notify(struct notifier_block *self, unsigned long val, void *data);
-extern void arch_uprobe_abort_xol(struct arch_uprobe *aup, struct pt_regs *regs);
-extern unsigned long arch_uretprobe_hijack_return_addr(unsigned long trampoline_vaddr, struct pt_regs *regs);
  #endif	/* _ASM_UPROBES_H */
arch/x86/include/asm/uprobes.h +0 -7
index 2a24180..3087ea9 100644
@@ -52,11 +52,4 @@ struct arch_uprobe_task {
  	unsigned int			saved_tf;
  };
 
-extern int  arch_uprobe_analyze_insn(struct arch_uprobe *aup, struct mm_struct *mm, unsigned long addr);
-extern int  arch_uprobe_pre_xol(struct arch_uprobe *aup, struct pt_regs *regs);
-extern int  arch_uprobe_post_xol(struct arch_uprobe *aup, struct pt_regs *regs);
-extern bool arch_uprobe_xol_was_trapped(struct task_struct *tsk);
-extern int  arch_uprobe_exception_notify(struct notifier_block *self, unsigned long val, void *data);
-extern void arch_uprobe_abort_xol(struct arch_uprobe *aup, struct pt_regs *regs);
-extern unsigned long arch_uretprobe_hijack_return_addr(unsigned long trampoline_vaddr, struct pt_regs *regs);
  #endif	/* _ASM_UPROBES_H */
include/linux/uprobes.h +1 -0
index 06f28be..9fd60c5 100644
@@ -30,6 +30,7 @@
  struct vm_area_struct;
  struct mm_struct;
  struct inode;
+struct notifier_block;
 
  #ifdef CONFIG_ARCH_SUPPORTS_UPROBES
  # include <asm/uprobes.h>
@@ -125,6 +126,13 @@ extern void uprobe_notify_resume(struct pt_regs *regs);
  extern bool uprobe_deny_signal(void);
  extern bool __weak arch_uprobe_skip_sstep(struct arch_uprobe *aup, struct pt_regs *regs);
  extern void uprobe_clear_state(struct mm_struct *mm);

include/linux/uprobes.h extern void uprobe_notify_resume(struct pt_regs *regs); · line 138

Quoted 10 lines
include/linux/uprobes.h +7 -0
+extern int  arch_uprobe_analyze_insn(struct arch_uprobe *aup, struct mm_struct *mm, unsigned long addr);
+extern int  arch_uprobe_pre_xol(struct arch_uprobe *aup, struct pt_regs *regs);
+extern int  arch_uprobe_post_xol(struct arch_uprobe *aup, struct pt_regs *regs);
+extern bool arch_uprobe_xol_was_trapped(struct task_struct *tsk);
+extern int  arch_uprobe_exception_notify(struct notifier_block *self, unsigned long val, void *data);
+extern void arch_uprobe_abort_xol(struct arch_uprobe *aup, struct pt_regs *regs);
+extern unsigned long arch_uretprobe_hijack_return_addr(unsigned long trampoline_vaddr, struct pt_regs *regs);
  #else /* !CONFIG_UPROBES */
  struct uprobes_state {
  };

Looks good to me. ARM builds/runs fine with your patch. I will remove my (updated) version from my patch set.

-dl

author
#7 lore raw

PATCH v2 2/13 uprobes: allow ignoring of probe hits

In reply to David Long

From: "David A. Long" <[email protected]>

Allow arches to decided to ignore a probe hit. ARM will use this to only call handlers if the conditions to execute a conditionally executed instruction are satisfied.

Upleveled for v3.12-rc4.

---
 include/linux/uprobes.h |  1 +
 kernel/events/uprobes.c | 17 ++++++++++++-----
 2 files changed, 13 insertions(+), 5 deletions(-)
include/linux/uprobes.h +1 -0
index 9fd60c5..80116c9 100644
@@ -133,6 +133,7 @@ extern bool arch_uprobe_xol_was_trapped(struct task_struct *tsk);
 extern int  arch_uprobe_exception_notify(struct notifier_block *self, unsigned long val, void *data);
 extern void arch_uprobe_abort_xol(struct arch_uprobe *aup, struct pt_regs *regs);
 extern unsigned long arch_uretprobe_hijack_return_addr(unsigned long trampoline_vaddr, struct pt_regs *regs);
+extern bool __weak arch_uprobe_ignore(struct arch_uprobe *aup, struct pt_regs *regs);
 #else /* !CONFIG_UPROBES */
 struct uprobes_state {
 };
kernel/events/uprobes.c +12 -5
index ad8e1bd..3955172 100644
@@ -1732,9 +1732,6 @@ static void handle_swbp(struct pt_regs *regs)
 		return;
 	}
 
-	/* change it in advance for ->handler() and restart */
-	instruction_pointer_set(regs, bp_vaddr);
-
 	/*
 	 * TODO: move copy_insn/etc into _register and remove this hack.
 	 * After we hit the bp, _unregister + _register can install the
@@ -1742,16 +1739,26 @@ static void handle_swbp(struct pt_regs *regs)
 	 */
 	smp_rmb(); /* pairs with wmb() in install_breakpoint() */
 	if (unlikely(!test_bit(UPROBE_COPY_INSN, &uprobe->flags)))
-		goto out;
+		goto restart;
 
 	handler_chain(uprobe, regs);
+
+	if (arch_uprobe_ignore(&uprobe->arch, regs))
+		goto out;
+
 	if (can_skip_sstep(uprobe, regs))
 		goto out;
 
 	if (!pre_ssout(uprobe, regs, bp_vaddr))
 		return;
 
-	/* can_skip_sstep() succeeded, or restart if can't singlestep */
+restart:
+	/*
+	 * cannot singlestep; cannot skip instruction;
+	 * re-execute the instruction.
+	 */
+	instruction_pointer_set(regs, bp_vaddr);
+
 out:
 	put_uprobe(uprobe);
 }
Signature · 1 line
1.8.1.2
maintainer
#8 lore raw

Re: [PATCH v2 02/13] uprobes: allow ignoring of probe hits

kernel/events/uprobes.c static void handle_swbp(struct pt_regs *regs) · line 1737

Quoted 8 lines · David Long
kernel/events/uprobes.c +0 -3
@@ -1732,9 +1732,6 @@ static void handle_swbp(struct pt_regs *regs)
 		return;
 	}
 
-	/* change it in advance for ->handler() and restart */
-	instruction_pointer_set(regs, bp_vaddr);
-

Well, this looks obviously wrong. This SET_IP() has the comment ;)

Note also that with this breaks __skip_sstep() on x86.

Oleg.

author
#9 lore raw

Re: [PATCH v2 02/13] uprobes: allow ignoring of probe hits

Quoted 12 lines · Oleg Nesterov

kernel/events/uprobes.c static void handle_swbp(struct pt_regs *regs) · line 1737

Quoted 8 lines · David Long
kernel/events/uprobes.c +0 -3
@@ -1732,9 +1732,6 @@ static void handle_swbp(struct pt_regs *regs)
  		return;
  	}
 
-	/* change it in advance for ->handler() and restart */
-	instruction_pointer_set(regs, bp_vaddr);
-

Well, this looks obviously wrong. This SET_IP() has the comment ;)

Quoted 4 lines

Note also that with this breaks __skip_sstep() on x86.

Oleg.

Yes, and there's a missing weak stub function in there too. It was a surprise to me that declaring an external as weak means that it quietly ignores the fact there is no definition for it at link time, and makes it zero. I think there may be some similar land mines elsewhere in the kernel, unrelated to these changes or uprobes in general.

I have an updated version to go out with the v3 patches. It is working with v3.12-rc6 on x86 and ARM, to the extent I'm able to test it.

-dl

#10 lore raw

Re: [PATCH v2 02/13] uprobes: allow ignoring of probe hits

Sorry for top-posting/formatting,

Do you mean arch_uprobe_skip_sstep() ?

Yes, this __weak is wrong, already fixed in my tree. See
http://marc.info/?l=linux-mips&m=138132052022388&w=2

----- Original Message -----
From: "David Long" <[email protected]>
To: "Oleg Nesterov" <[email protected]>
Cc: linux-arm-kernel at lists.infradead.org, "Rabin Vincent" <[email protected]>, "Jon Medhurst (Tixy)" <[email protected]>, "Srikar Dronamraju" <[email protected]>, "Ingo Molnar" <[email protected]>, linux-kernel at vger.kernel.org
Sent: Tuesday, 22 October, 2013 5:45:47 AM
Subject: Re: [PATCH v2 02/13] uprobes: allow ignoring of probe hits

Quoted 12 lines · Oleg Nesterov

kernel/events/uprobes.c static void handle_swbp(struct pt_regs *regs) · line 1737

Quoted 8 lines · David Long
kernel/events/uprobes.c +0 -3
@@ -1732,9 +1732,6 @@ static void handle_swbp(struct pt_regs *regs)
  		return;
  	}
 
-	/* change it in advance for ->handler() and restart */
-	instruction_pointer_set(regs, bp_vaddr);
-

Well, this looks obviously wrong. This SET_IP() has the comment ;)

Quoted 4 lines

Note also that with this breaks __skip_sstep() on x86.

Oleg.

Yes, and there's a missing weak stub function in there too. It was a surprise to me that declaring an external as weak means that it quietly ignores the fact there is no definition for it at link time, and makes it zero. I think there may be some similar land mines elsewhere in the kernel, unrelated to these changes or uprobes in general.

I have an updated version to go out with the v3 patches. It is working with v3.12-rc6 on x86 and ARM, to the extent I'm able to test it.

-dl

author
#11 lore raw

Re: [PATCH v2 02/13] uprobes: allow ignoring of probe hits

Quoted 7 lines · Oleg Nesterov

Sorry for top-posting/formatting,

Do you mean arch_uprobe_skip_sstep() ?

Yes, this __weak is wrong, already fixed in my tree. See
http://marc.info/?l=linux-mips&m=138132052022388&w=2

That was one I was looking at, but it seemed to me a general opportunity for runtime failures in the kernel that could have been detected at link time. But that's a topic for discussion elsewhere I should think.

-dl

maintainer
#12 lore raw

Re: [PATCH v2 02/13] uprobes: allow ignoring of probe hits

In reply to Oleg Nesterov

David, sorry for delay.

Quoted 7 lines · Oleg Nesterov
Quoted 5 lines · David Long
(quoted diff) +0 -0
@@ -1732,9 +1732,6 @@ static void handle_swbp(struct pt_regs *regs)
 		return;
 	}
 
Quoted 7 lines

kernel/events/uprobes.c static void handle_swbp(struct pt_regs *regs) · line 1737

Quoted 3 lines
kernel/events/uprobes.c +0 -3
-	/* change it in advance for ->handler() and restart */
-	instruction_pointer_set(regs, bp_vaddr);
-

Well, this looks obviously wrong. This SET_IP() has the comment ;)

Note also that with this breaks __skip_sstep() on x86.

Hmm. Thinking more, it seems that this patch has another problem.

IIUC, the whole point of arch_uprobe_ignore() is to avoid handler_chain() if the condition was not satisfied, so you need to call it before handler_chain() ?

Otherwise this logic should go into can_skip_sstep() and we simply do not need the new hook, just we need to tweak the (ugly) UPROBE_SKIP_SSTEP logic.

Oleg.

author
#13 lore raw

Re: [PATCH v2 02/13] uprobes: allow ignoring of probe hits

Sorry for the delay, all this week it's me that's traveling.

Quoted 16 lines · Oleg Nesterov

David, sorry for delay.

Quoted 13 lines · Oleg Nesterov

kernel/events/uprobes.c static void handle_swbp(struct pt_regs *regs) · line 1737

Quoted 8 lines · David Long
kernel/events/uprobes.c +0 -3
@@ -1732,9 +1732,6 @@ static void handle_swbp(struct pt_regs *regs)
  		return;
  	}
 
-	/* change it in advance for ->handler() and restart */
-	instruction_pointer_set(regs, bp_vaddr);
-

Well, this looks obviously wrong. This SET_IP() has the comment ;)

Quoted 7 lines
Quoted 1 line

Note also that with this breaks __skip_sstep() on x86.

Hmm. Thinking more, it seems that this patch has another problem.

IIUC, the whole point of arch_uprobe_ignore() is to avoid handler_chain() if the condition was not satisfied, so you need to call it before handler_chain() ?

Yes, you're right. I can see now that is a bad merge of an earlier version of the patch. I have just tested the fix. Thanks for finding this.

Quoted 7 lines

Otherwise this logic should go into can_skip_sstep() and we simply do not need the new hook, just we need to tweak the (ugly) UPROBE_SKIP_SSTEP logic.

Oleg.

-dl

author
#14 lore raw

PATCH v2 3/13 uprobes: allow arch access to xol slot

In reply to David Long

From: "David A. Long" <[email protected]>

Allow arches to customize how the instruction is filled into the xol slot. ARM will use this to insert an undefined instruction after the real instruction in order to simulate a single step of the instruction without hardware support.

---
 include/linux/uprobes.h |  1 +
 kernel/events/uprobes.c | 10 +++++++++-
 2 files changed, 10 insertions(+), 1 deletion(-)
include/linux/uprobes.h +1 -0
index 80116c9..2556ab6 100644
@@ -134,6 +134,7 @@ extern int  arch_uprobe_exception_notify(struct notifier_block *self, unsigned l
 extern void arch_uprobe_abort_xol(struct arch_uprobe *aup, struct pt_regs *regs);
 extern unsigned long arch_uretprobe_hijack_return_addr(unsigned long trampoline_vaddr, struct pt_regs *regs);
 extern bool __weak arch_uprobe_ignore(struct arch_uprobe *aup, struct pt_regs *regs);
+extern void __weak arch_uprobe_xol_copy(struct arch_uprobe *auprobe, void *vaddr);
 #else /* !CONFIG_UPROBES */
 struct uprobes_state {
 };
kernel/events/uprobes.c +9 -1
index 3955172..22d0121 100644
@@ -1238,6 +1238,11 @@ static unsigned long xol_take_insn_slot(struct xol_area *area)
 	return slot_addr;
 }
 
+void __weak arch_uprobe_xol_copy(struct arch_uprobe *auprobe, void *vaddr)
+{
+	memcpy(vaddr, auprobe->insn, MAX_UINSN_BYTES);
+}
+
 /*
  * xol_get_insn_slot - allocate a slot for xol.
  * Returns the allocated slot address or 0.
@@ -1246,6 +1251,7 @@ static unsigned long xol_get_insn_slot(struct uprobe *uprobe)
 {
 	struct xol_area *area;
 	unsigned long xol_vaddr;
+	void *kaddr;
 
 	area = get_xol_area();
 	if (!area)
@@ -1256,7 +1262,9 @@ static unsigned long xol_get_insn_slot(struct uprobe *uprobe)
 		return 0;
 
 	/* Initialize the slot */
-	copy_to_page(area->page, xol_vaddr, uprobe->arch.insn, MAX_UINSN_BYTES);
+	kaddr = kmap_atomic(area->page);
+	arch_uprobe_xol_copy(&uprobe->arch, kaddr + (xol_vaddr & ~PAGE_MASK));
+	kunmap_atomic(kaddr);
 	/*
 	 * We probably need flush_icache_user_range() but it needs vma.
 	 * This should work on supported architectures too.
Signature · 1 line
1.8.1.2
maintainer
#15 lore raw

Re: [PATCH v2 03/13] uprobes: allow arch access to xol slot

Quoted 5 lines · David Long

Allow arches to customize how the instruction is filled into the xol slot. ARM will use this to insert an undefined instruction after the real instruction in order to simulate a single step of the instruction without hardware support.

OK, but

Quoted 17 lines
kernel/events/uprobes.c +6 -0
+void __weak arch_uprobe_xol_copy(struct arch_uprobe *auprobe, void *vaddr)
+{
+	memcpy(vaddr, auprobe->insn, MAX_UINSN_BYTES);
+}
+
 /*
  * xol_get_insn_slot - allocate a slot for xol.
  * Returns the allocated slot address or 0.
@@ -1246,6 +1251,7 @@ static unsigned long xol_get_insn_slot(struct uprobe *uprobe)
 {
 	struct xol_area *area;
 	unsigned long xol_vaddr;
+	void *kaddr;
 
 	area = get_xol_area();
 	if (!area)

kernel/events/uprobes.c static unsigned long xol_get_insn_slot(struct uprobe *uprobe) · line 1267

Quoted 7 lines
kernel/events/uprobes.c +3 -1
@@ -1256,7 +1262,9 @@ static unsigned long xol_get_insn_slot(struct uprobe *uprobe)
 		return 0;
 
 	/* Initialize the slot */
-	copy_to_page(area->page, xol_vaddr, uprobe->arch.insn, MAX_UINSN_BYTES);
+	kaddr = kmap_atomic(area->page);
+	arch_uprobe_xol_copy(&uprobe->arch, kaddr + (xol_vaddr & ~PAGE_MASK));
+	kunmap_atomic(kaddr);

This looks a bit strange and defeats the purpose of generic helper...

How about

	void __weak arch_uprobe_xol_copy(...)
	{
		copy_to_page(...);
	}

then just

- copy_to_page(...);
+ arch_uprobe_xol_copy(...);

?

Or, I am just curious, can't we have an empty "__weak arch_uprobe_xol_copy" if we call it right after copy_to_page() ?

Oleg.

author
#16 lore raw

Re: [PATCH v2 03/13] uprobes: allow arch access to xol slot

Quoted 43 lines · Oleg Nesterov
Quoted 5 lines · David Long

Allow arches to customize how the instruction is filled into the xol slot. ARM will use this to insert an undefined instruction after the real instruction in order to simulate a single step of the instruction without hardware support.

OK, but

Quoted 17 lines
kernel/events/uprobes.c +6 -0
+void __weak arch_uprobe_xol_copy(struct arch_uprobe *auprobe, void *vaddr)
+{
+	memcpy(vaddr, auprobe->insn, MAX_UINSN_BYTES);
+}
+
  /*
   * xol_get_insn_slot - allocate a slot for xol.
   * Returns the allocated slot address or 0.
@@ -1246,6 +1251,7 @@ static unsigned long xol_get_insn_slot(struct uprobe *uprobe)
  {
  	struct xol_area *area;
  	unsigned long xol_vaddr;
+	void *kaddr;
 
  	area = get_xol_area();
  	if (!area)

kernel/events/uprobes.c static unsigned long xol_get_insn_slot(struct uprobe *uprobe) · line 1267

Quoted 7 lines
kernel/events/uprobes.c +3 -1
@@ -1256,7 +1262,9 @@ static unsigned long xol_get_insn_slot(struct uprobe *uprobe)
  		return 0;
 
  	/* Initialize the slot */
-	copy_to_page(area->page, xol_vaddr, uprobe->arch.insn, MAX_UINSN_BYTES);
+	kaddr = kmap_atomic(area->page);
+	arch_uprobe_xol_copy(&uprobe->arch, kaddr + (xol_vaddr & ~PAGE_MASK));
+	kunmap_atomic(kaddr);

This looks a bit strange and defeats the purpose of generic helper...

How about

	void __weak arch_uprobe_xol_copy(...)
	{
		copy_to_page(...);
	}
Quoted 7 lines

then just

- copy_to_page(...);
+ arch_uprobe_xol_copy(...);

?

I was trying to avoid duplicating the VM calls in the architecture-specific implementations, but maybe that is the cleaner way to do it after all. I've made changes as suggested above.

Quoted 3 lines

Or, I am just curious, can't we have an empty "__weak arch_uprobe_xol_copy" if we call it right after copy_to_page() ?

Then there would potentially be effectively two copy calls. That doesn't feel at all the right thing to do.

-dl

maintainer
#17 lore raw

Re: [PATCH v2 03/13] uprobes: allow arch access to xol slot

Quoted 12 lines · David Long
Quoted 10 lines · Oleg Nesterov

How about

	void __weak arch_uprobe_xol_copy(...)
	{
		copy_to_page(...);
	}

then just

Quoted 8 lines
Quoted 5 lines

- copy_to_page(...);
+ arch_uprobe_xol_copy(...);

?

I was trying to avoid duplicating the VM calls in the architecture-specific implementations,

Yes, I understand... But this uglifies the generic code.

Quoted 2 lines

but maybe that is the cleaner way
to do it after all. I've made changes as suggested above.

Thanks. and this uglifies arm code I guess ;)

David. Perhaps we can avoid the new hook altogether? What if we do the simple change below (it ignores powerpc) ?

Then arm can add "unsigned long ixol[2]" into its arch_uprobe, and arch_uprobe_analyze_insn() can initialize this member correctly.

What do you think?

Oleg.

arch/x86/include/asm/uprobes.h +4 -1
@@ -35,7 +35,10 @@ typedef u8 uprobe_opcode_t;
 
 struct arch_uprobe {
 	u16				fixups;
-	u8				insn[MAX_UINSN_BYTES];
+	union {
+		u8			insn[MAX_UINSN_BYTES];
+		u8			ixol[MAX_UINSN_BYTES];
+	}
 #ifdef CONFIG_X86_64
 	unsigned long			rip_rela_target_address;
 #endif
kernel/events/uprobes.c +2 -1
@@ -1264,7 +1264,8 @@ static unsigned long xol_get_insn_slot(s
 		return 0;
 
 	/* Initialize the slot */
-	copy_to_page(area->page, xol_vaddr, uprobe->arch.insn, MAX_UINSN_BYTES);
+	copy_to_page(area->page, xol_vaddr,
+			uprobe->arch.ixol, sizeof(uprobe->arch.ixol));
 	/*
 	 * We probably need flush_icache_user_range() but it needs vma.
 	 * This should work on supported architectures too.
maintainer
#18 lore raw

PATCH 1/1 uprobes: introduce arch_uprobe->ixol

Quoted 8 lines · Oleg Nesterov

David. Perhaps we can avoid the new hook altogether? What if we do the simple change below (it ignores powerpc) ?

Then arm can add "unsigned long ixol[2]" into its arch_uprobe, and arch_uprobe_analyze_insn() can initialize this member correctly.

What do you think?

Seriouly, how about the patch below?

In fact, given that you are going to reimplement set_swbp/orig_insn, the new member is not strictly needed (afaics). But it looks more clear this way, and we need s/MAX_UINSN_BYTES/sizeof()/ anyway.

Oleg.
---

Subject: [PATCH] uprobes: introduce arch_uprobe->ixol

Currently xol_get_insn_slot() assumes that we should simply copy arch_uprobe->insn[] which is (ignoring arch_uprobe_analyze_insn) just the copy of the original insn.

This is not true for arm which needs to create another insn to execute it out-of-line.

So this patch simply adds the new member, ->ixol into the union. This doesn't make any difference for x86 and powerpc, but arm can divorce insn/ixol and initialize the correct xol insn in arch_uprobe_analyze_insn().

---
 arch/powerpc/include/asm/uprobes.h |    1 +
 arch/x86/include/asm/uprobes.h     |    5 ++++-
 kernel/events/uprobes.c            |    3 ++-
 3 files changed, 7 insertions(+), 2 deletions(-)
arch/powerpc/include/asm/uprobes.h +1 -0
index 2301602..541fd6f 100644
@@ -37,6 +37,7 @@ typedef ppc_opcode_t uprobe_opcode_t;
 struct arch_uprobe {
 	union {
 		u8	insn[MAX_UINSN_BYTES];
+		u8	ixol[MAX_UINSN_BYTES];
 		u32	ainsn;
 	};
 };
arch/x86/include/asm/uprobes.h +4 -1
index 6e51979..2a24180 100644
@@ -35,7 +35,10 @@ typedef u8 uprobe_opcode_t;
 
 struct arch_uprobe {
 	u16				fixups;
-	u8				insn[MAX_UINSN_BYTES];
+	union {
+		u8			insn[MAX_UINSN_BYTES];
+		u8			ixol[MAX_UINSN_BYTES];
+	};
 #ifdef CONFIG_X86_64
 	unsigned long			rip_rela_target_address;
 #endif
kernel/events/uprobes.c +2 -1
index ad8e1bd..6aef5ad 100644
@@ -1256,7 +1256,8 @@ static unsigned long xol_get_insn_slot(struct uprobe *uprobe)
 		return 0;
 
 	/* Initialize the slot */
-	copy_to_page(area->page, xol_vaddr, uprobe->arch.insn, MAX_UINSN_BYTES);
+	copy_to_page(area->page, xol_vaddr,
+			uprobe->arch.ixol, sizeof(uprobe->arch.ixol));
 	/*
 	 * We probably need flush_icache_user_range() but it needs vma.
 	 * This should work on supported architectures too.
Signature · 1 line
1.5.5.1
author
#19 lore raw

Re: [PATCH] uprobes: introduce arch_uprobe->ixol

Quoted 10 lines · Oleg Nesterov
Quoted 8 lines · Oleg Nesterov

David. Perhaps we can avoid the new hook altogether? What if we do the simple change below (it ignores powerpc) ?

Then arm can add "unsigned long ixol[2]" into its arch_uprobe, and arch_uprobe_analyze_insn() can initialize this member correctly.

What do you think?

Quoted 8 lines

Seriouly, how about the patch below?

In fact, given that you are going to reimplement set_swbp/orig_insn, the new member is not strictly needed (afaics). But it looks more clear this way, and we need s/MAX_UINSN_BYTES/sizeof()/ anyway.

Oleg.

Sorry for the delay, have not quite been successful in getting this to work yet. Hopefully will have better results shortly.

-dl

author
#20 lore raw

Re: [PATCH] uprobes: introduce arch_uprobe->ixol

In reply to Oleg Nesterov
Quoted 10 lines · Oleg Nesterov
Quoted 8 lines · Oleg Nesterov

David. Perhaps we can avoid the new hook altogether? What if we do the simple change below (it ignores powerpc) ?

Then arm can add "unsigned long ixol[2]" into its arch_uprobe, and arch_uprobe_analyze_insn() can initialize this member correctly.

What do you think?

Quoted 8 lines

Seriouly, how about the patch below?

In fact, given that you are going to reimplement set_swbp/orig_insn, the new member is not strictly needed (afaics). But it looks more clear this way, and we need s/MAX_UINSN_BYTES/sizeof()/ anyway.

Oleg.
---

I agree that this is cleaner than another weak callout. I have it working for ARM now.

Thanks,
-dl

maintainer
#21 lore raw

Re: [PATCH] uprobes: introduce arch_uprobe->ixol

Quoted 10 lines · David Long
Quoted 8 lines · Oleg Nesterov
Quoted 1 line · Oleg Nesterov

Seriouly, how about the patch below?

In fact, given that you are going to reimplement set_swbp/orig_insn, the new member is not strictly needed (afaics). But it looks more clear this way, and we need s/MAX_UINSN_BYTES/sizeof()/ anyway.

Quoted 5 lines
Quoted 2 lines

Oleg.
---

I agree that this is cleaner than another weak callout. I have it working for ARM now.

OK, I am going to add this patch to my tree, thanks. Can I add your acked-by ?

Oleg.

author
#22 lore raw

Re: [PATCH] uprobes: introduce arch_uprobe->ixol

Quoted 14 lines · Oleg Nesterov
Quoted 13 lines · David Long
Quoted 10 lines · Oleg Nesterov
Quoted 1 line · Oleg Nesterov

Seriouly, how about the patch below?

In fact, given that you are going to reimplement set_swbp/orig_insn, the new member is not strictly needed (afaics). But it looks more clear this way, and we need s/MAX_UINSN_BYTES/sizeof()/ anyway.

Oleg.
---

Quoted 8 lines
Quoted 2 lines

I agree that this is cleaner than another weak callout. I have it working for ARM now.

OK, I am going to add this patch to my tree, thanks. Can I add your acked-by ?

Oleg.

yes.

-dl

author
#23 lore raw

PATCH v2 4/13 uprobes: allow arch-specific initialization

In reply to David Long

From: Rabin Vincent <[email protected]>

Add a weak function for any architecture-specific initialization. ARM will use this to register the handlers for the undefined instructions it uses to implement uprobes.

---
 include/linux/uprobes.h |  1 +
 kernel/events/uprobes.c | 10 ++++++++++
 2 files changed, 11 insertions(+)
include/linux/uprobes.h +1 -0
index 2556ab6..a28dcbe 100644
@@ -135,6 +135,7 @@ extern void arch_uprobe_abort_xol(struct arch_uprobe *aup, struct pt_regs *regs)
 extern unsigned long arch_uretprobe_hijack_return_addr(unsigned long trampoline_vaddr, struct pt_regs *regs);
 extern bool __weak arch_uprobe_ignore(struct arch_uprobe *aup, struct pt_regs *regs);
 extern void __weak arch_uprobe_xol_copy(struct arch_uprobe *auprobe, void *vaddr);
+extern int __weak arch_uprobes_init(void);
 #else /* !CONFIG_UPROBES */
 struct uprobes_state {
 };
kernel/events/uprobes.c +10 -0
index 22d0121..9734a5f 100644
@@ -1860,8 +1860,14 @@ static struct notifier_block uprobe_exception_nb = {
 	.priority		= INT_MAX-1,	/* notified after kprobes, kgdb */
 };
 
+int __weak __init arch_uprobes_init(void)
+{
+	return 0;
+}
+
 static int __init init_uprobes(void)
 {
+	int ret;
 	int i;
 
 	for (i = 0; i < UPROBES_HASH_SZ; i++)
@@ -1870,6 +1876,10 @@ static int __init init_uprobes(void)
 	if (percpu_init_rwsem(&dup_mmap_sem))
 		return -ENOMEM;
 
+	ret = arch_uprobes_init();
+	if (ret)
+		return ret;
+
 	return register_die_notifier(&uprobe_exception_nb);
 }
 module_init(init_uprobes);
Signature · 1 line
1.8.1.2
maintainer
#24 lore raw

Re: [PATCH v2 04/13] uprobes: allow arch-specific initialization

Quoted 4 lines · David Long

Add a weak function for any architecture-specific initialization. ARM will use this to register the handlers for the undefined instructions it uses to implement uprobes.

Could you explain why ARM can't simply do the necessary initialization in arch/arm/kernel/uprobes-arm.c ?

Quoted 12 lines
kernel/events/uprobes.c +6 -0
+int __weak __init arch_uprobes_init(void)
+{
+	return 0;
+}
+
 static int __init init_uprobes(void)
 {
+	int ret;
 	int i;
 
 	for (i = 0; i < UPROBES_HASH_SZ; i++)

kernel/events/uprobes.c static int __init init_uprobes(void) · line 1885

Quoted 10 lines
kernel/events/uprobes.c +4 -0
@@ -1870,6 +1876,10 @@ static int __init init_uprobes(void)
 	if (percpu_init_rwsem(&dup_mmap_sem))
 		return -ENOMEM;
 
+	ret = arch_uprobes_init();
+	if (ret)
+		return ret;
+
 	return register_die_notifier(&uprobe_exception_nb);
 }
 module_init(init_uprobes);

IOW, why do we need to call arch_uprobes_init() from init_uprobes().

Oleg.

author
#25 lore raw

Re: [PATCH v2 04/13] uprobes: allow arch-specific initialization

Quoted 35 lines · Oleg Nesterov
Quoted 4 lines · David Long

Add a weak function for any architecture-specific initialization. ARM will use this to register the handlers for the undefined instructions it uses to implement uprobes.

Could you explain why ARM can't simply do the necessary initialization in arch/arm/kernel/uprobes-arm.c ?

Quoted 12 lines
kernel/events/uprobes.c +6 -0
+int __weak __init arch_uprobes_init(void)
+{
+	return 0;
+}
+
  static int __init init_uprobes(void)
  {
+	int ret;
  	int i;
 
  	for (i = 0; i < UPROBES_HASH_SZ; i++)

kernel/events/uprobes.c static int __init init_uprobes(void) · line 1885

Quoted 10 lines
kernel/events/uprobes.c +4 -0
@@ -1870,6 +1876,10 @@ static int __init init_uprobes(void)
  	if (percpu_init_rwsem(&dup_mmap_sem))
  		return -ENOMEM;
 
+	ret = arch_uprobes_init();
+	if (ret)
+		return ret;
+
  	return register_die_notifier(&uprobe_exception_nb);
  }
  module_init(init_uprobes);

IOW, why do we need to call arch_uprobes_init() from init_uprobes().

Quoted 2 lines

Oleg

I don't know how you would do the initialization without invoking it through the module_init function, which I think you can only have one of. Could you explain in more detail what you had in mind?

-dl

maintainer
#26 lore raw

Re: [PATCH v2 04/13] uprobes: allow arch-specific initialization

Quoted 37 lines · David Long
Quoted 35 lines · Oleg Nesterov
Quoted 4 lines · David Long

Add a weak function for any architecture-specific initialization. ARM will use this to register the handlers for the undefined instructions it uses to implement uprobes.

Could you explain why ARM can't simply do the necessary initialization in arch/arm/kernel/uprobes-arm.c ?

Quoted 12 lines
kernel/events/uprobes.c +6 -0
+int __weak __init arch_uprobes_init(void)
+{
+	return 0;
+}
+
  static int __init init_uprobes(void)
  {
+	int ret;
  	int i;
 
  	for (i = 0; i < UPROBES_HASH_SZ; i++)

kernel/events/uprobes.c static int __init init_uprobes(void) · line 1885

Quoted 10 lines
kernel/events/uprobes.c +4 -0
@@ -1870,6 +1876,10 @@ static int __init init_uprobes(void)
  	if (percpu_init_rwsem(&dup_mmap_sem))
  		return -ENOMEM;
 
+	ret = arch_uprobes_init();
+	if (ret)
+		return ret;
+
  	return register_die_notifier(&uprobe_exception_nb);
  }
  module_init(init_uprobes);

IOW, why do we need to call arch_uprobes_init() from init_uprobes().

Quoted 6 lines
Quoted 2 lines

Oleg

I don't know how you would do the initialization without invoking it through the module_init function, which I think you can only have one of. Could you explain in more detail what you had in mind?

I simply do not understand why uprobes.c uses module_init/module_exit, it can't be compiled as a module.

I think that module_exit/exit_uprobes should be killed, and module_init() should be turned into __initcall(). uprobes-arm.c can have another one.

Oleg.

author
#27 lore raw

Re: [PATCH v2 04/13] uprobes: allow arch-specific initialization

Quoted 3 lines · Oleg Nesterov

On 10/22, David Long wrote:
I simply do not understand why uprobes.c uses module_init/module_exit, it can't be compiled as a module.

I guess that makes sense, assuming it can never be made a module. I saw you recent commit for this.

Quoted 3 lines

I think that module_exit/exit_uprobes should be killed, and module_init() should be turned into __initcall(). uprobes-arm.c can have another one.

I will see if I can make this work. Right now the arch-specific initialization call is done in the middle of the generic initialization code, but I don't know that it *has* to be that way. I have some concern too about getting the order right, since these are built from different makefiles.

-dl

maintainer
#28 lore raw

Re: [PATCH v2 04/13] uprobes: allow arch-specific initialization

Quoted 5 lines · David Long
Quoted 3 lines · Oleg Nesterov

On 10/22, David Long wrote:
I simply do not understand why uprobes.c uses module_init/module_exit, it can't be compiled as a module.

Quoted 8 lines

I guess that makes sense, assuming it can never be made a module. I saw you recent commit for this.

Quoted 3 lines

I think that module_exit/exit_uprobes should be killed, and module_init() should be turned into __initcall(). uprobes-arm.c can have another one.

I will see if I can make this work.

If this can't work, then we need the new hook (this patch). But in this case please update the changelog to explain the reason.

Quoted 5 lines

Right now the arch-specific
initialization call is done in the middle of the generic initialization code, but I don't know that it *has* to be that way. I have some concern too about getting the order right, since these are built from different makefiles.

Not sure I understand... But grep shows a lot of core_initcall()'s in arch/arm/ which do register_undef_hook(). And I guess you can use any initcall level.

Oleg.

author
#29 lore raw

Re: [PATCH v2 04/13] uprobes: allow arch-specific initialization

Quoted 24 lines · Oleg Nesterov
Quoted 5 lines · David Long
Quoted 3 lines · Oleg Nesterov

On 10/22, David Long wrote:
I simply do not understand why uprobes.c uses module_init/module_exit, it can't be compiled as a module.

Quoted 8 lines

I guess that makes sense, assuming it can never be made a module. I saw you recent commit for this.

Quoted 3 lines

I think that module_exit/exit_uprobes should be killed, and module_init() should be turned into __initcall(). uprobes-arm.c can have another one.

I will see if I can make this work.

If this can't work, then we need the new hook (this patch). But in this case please update the changelog to explain the reason.

Quoted 5 lines

Right now the arch-specific
initialization call is done in the middle of the generic initialization code, but I don't know that it *has* to be that way. I have some concern too about getting the order right, since these are built from different makefiles.

Quoted 3 lines

Not sure I understand... But grep shows a lot of core_initcall()'s in arch/arm/ which do register_undef_hook(). And I guess you can use any initcall level.

Just to close on this, I implemented your suggested __initcall change and it tested out fine.

-dl

author
#30 lore raw

PATCH v2 5/13 uprobes: add arch write opcode hook

In reply to David Long

From: Rabin Vincent <[email protected]>

Allow arches to write the opcode with a custom function. ARM needs to customize the swbp instruction depending on the condition code of the instruction it replaces.

---
 include/linux/uprobes.h |  3 +++
 kernel/events/uprobes.c | 21 ++++++++++++++++-----
 2 files changed, 19 insertions(+), 5 deletions(-)
include/linux/uprobes.h +3 -0
index a28dcbe..34375da 100644
@@ -136,6 +136,9 @@ extern unsigned long arch_uretprobe_hijack_return_addr(unsigned long trampoline_
 extern bool __weak arch_uprobe_ignore(struct arch_uprobe *aup, struct pt_regs *regs);
 extern void __weak arch_uprobe_xol_copy(struct arch_uprobe *auprobe, void *vaddr);
 extern int __weak arch_uprobes_init(void);
+extern void __weak arch_uprobe_write_opcode(struct arch_uprobe *auprobe,
+					    void *vaddr,
+					    uprobe_opcode_t opcode);
 #else /* !CONFIG_UPROBES */
 struct uprobes_state {
 };
kernel/events/uprobes.c +16 -5
index 9734a5f..cb01d82 100644
@@ -248,6 +248,12 @@ static int verify_opcode(struct page *page, unsigned long vaddr, uprobe_opcode_t
  * have fixed length instructions.
  */
 
+void __weak arch_uprobe_write_opcode(struct arch_uprobe *auprobe, void *vaddr,
+				     uprobe_opcode_t opcode)
+{
+	memcpy(vaddr, &opcode, UPROBE_SWBP_INSN_SIZE);
+}
+
 /*
  * write_opcode - write the opcode at a given virtual address.
  * @mm: the probed process address space.
@@ -260,11 +266,12 @@ static int verify_opcode(struct page *page, unsigned long vaddr, uprobe_opcode_t
  * For mm @mm, write the opcode at @vaddr.
  * Return 0 (success) or a negative errno.
  */
-static int write_opcode(struct mm_struct *mm, unsigned long vaddr,
-			uprobe_opcode_t opcode)
+static int write_opcode(struct arch_uprobe *auprobe, struct mm_struct *mm,
+			unsigned long vaddr, uprobe_opcode_t opcode)
 {
 	struct page *old_page, *new_page;
 	struct vm_area_struct *vma;
+	void *vaddr_new;
 	int ret;
 
 retry:
@@ -285,7 +292,10 @@ retry:
 	__SetPageUptodate(new_page);
 
 	copy_highpage(new_page, old_page);
-	copy_to_page(new_page, vaddr, &opcode, UPROBE_SWBP_INSN_SIZE);
+	vaddr_new = kmap_atomic(new_page);
+	arch_uprobe_write_opcode(auprobe, vaddr_new + (vaddr & ~PAGE_MASK),
+				 opcode);
+	kunmap_atomic(vaddr_new);
 
 	ret = anon_vma_prepare(vma);
 	if (ret)
@@ -314,7 +324,7 @@ put_old:
  */
 int __weak set_swbp(struct arch_uprobe *auprobe, struct mm_struct *mm, unsigned long vaddr)
 {
-	return write_opcode(mm, vaddr, UPROBE_SWBP_INSN);
+	return write_opcode(auprobe, mm, vaddr, UPROBE_SWBP_INSN);
 }
 
 /**
@@ -329,7 +339,8 @@ int __weak set_swbp(struct arch_uprobe *auprobe, struct mm_struct *mm, unsigned
 int __weak
 set_orig_insn(struct arch_uprobe *auprobe, struct mm_struct *mm, unsigned long vaddr)
 {
-	return write_opcode(mm, vaddr, *(uprobe_opcode_t *)auprobe->insn);
+	return write_opcode(auprobe, mm, vaddr,
+			    *(uprobe_opcode_t *)auprobe->insn);
 }
 
 static int match_uprobe(struct uprobe *l, struct uprobe *r)
Signature · 1 line
1.8.1.2
maintainer
#31 lore raw

Re: [PATCH v2 05/13] uprobes: add arch write opcode hook

Quoted 4 lines · David Long

Allow arches to write the opcode with a custom function. ARM needs to customize the swbp instruction depending on the condition code of the instruction it replaces.

Well, we already have "__weak set_swbp(auprobe, ...)", can't arm use it?

If not,

kernel/events/uprobes.c static int verify_opcode(struct page *page, unsigned long vaddr, uprobe_opcode_t

Quoted 11 lines
kernel/events/uprobes.c +9 -1
+void __weak arch_uprobe_write_opcode(struct arch_uprobe *auprobe, void *vaddr,
+				     uprobe_opcode_t opcode)
+{
+	memcpy(vaddr, &opcode, UPROBE_SWBP_INSN_SIZE);
+}
...
-	copy_to_page(new_page, vaddr, &opcode, UPROBE_SWBP_INSN_SIZE);
+	vaddr_new = kmap_atomic(new_page);
+	arch_uprobe_write_opcode(auprobe, vaddr_new + (vaddr & ~PAGE_MASK),
+				 opcode);
+	kunmap_atomic(vaddr_new);

Again, if you need to add the new __weak helper, I think it should simply do copy_to_page().

Oleg.

author
#32 lore raw

Re: [PATCH v2 05/13] uprobes: add arch write opcode hook

Quoted 25 lines · Oleg Nesterov
Quoted 4 lines · David Long

Allow arches to write the opcode with a custom function. ARM needs to customize the swbp instruction depending on the condition code of the instruction it replaces.

Well, we already have "__weak set_swbp(auprobe, ...)", can't arm use it?

If not,

kernel/events/uprobes.c static int verify_opcode(struct page *page, unsigned long vaddr, uprobe_opcode_t

Quoted 11 lines
kernel/events/uprobes.c +9 -1
+void __weak arch_uprobe_write_opcode(struct arch_uprobe *auprobe, void *vaddr,
+				     uprobe_opcode_t opcode)
+{
+	memcpy(vaddr, &opcode, UPROBE_SWBP_INSN_SIZE);
+}
...
-	copy_to_page(new_page, vaddr, &opcode, UPROBE_SWBP_INSN_SIZE);
+	vaddr_new = kmap_atomic(new_page);
+	arch_uprobe_write_opcode(auprobe, vaddr_new + (vaddr & ~PAGE_MASK),
+				 opcode);
+	kunmap_atomic(vaddr_new);

Again, if you need to add the new __weak helper, I think it should simply do copy_to_page().

Quoted 2 lines

Oleg.

Unfortunately, providing an alternative set_swbp() would mean duplicating a moderate chunk of code from kernel/uprobes.c. Adding arch_uprobe_write_opcode() feels a little more like the right thing to do to me. I notice there don't seem to be any alternative set_swbp functions in the (rc6) kernel tree. I will adjust the arch_uprobe_write_opcode imlementation as suggested.

-dl

maintainer
#33 lore raw

Re: [PATCH v2 05/13] uprobes: add arch write opcode hook

Quoted 27 lines · David Long
Quoted 25 lines · Oleg Nesterov
Quoted 4 lines · David Long

Allow arches to write the opcode with a custom function. ARM needs to customize the swbp instruction depending on the condition code of the instruction it replaces.

Well, we already have "__weak set_swbp(auprobe, ...)", can't arm use it?

If not,

Review

Quoted 11 lines
(quoted diff) +9 -1
+void __weak arch_uprobe_write_opcode(struct arch_uprobe *auprobe, void *vaddr,
+				     uprobe_opcode_t opcode)
+{
+	memcpy(vaddr, &opcode, UPROBE_SWBP_INSN_SIZE);
+}
...
-	copy_to_page(new_page, vaddr, &opcode, UPROBE_SWBP_INSN_SIZE);
+	vaddr_new = kmap_atomic(new_page);
+	arch_uprobe_write_opcode(auprobe, vaddr_new + (vaddr & ~PAGE_MASK),
+				 opcode);
+	kunmap_atomic(vaddr_new);

Again, if you need to add the new __weak helper, I think it should simply do copy_to_page().

Quoted 5 lines
Quoted 2 lines

Oleg.

Unfortunately, providing an alternative set_swbp() would mean duplicating a moderate chunk of code from kernel/uprobes.c.

Yes, yes, sorry for confusion. What I actually tried to suggest is something like the trivial patch below.

Then arm can do:

	uprobe_opcode_t arch_uprobe_swbp_insn(struct arch_uprobe *auprobe)
	{
		return __opcode_to_mem_arm(auprobe->bpinsn);
	}

No?

Quoted 2 lines

I notice there don't seem to be any alternative set_swbp functions in the (rc6) kernel tree

Yes... I think we should simply make it "static". And set_orig_insn() too.

Oleg.

kernel/events/uprobes.c +6 -1
@@ -304,6 +304,11 @@ put_old:
 	return ret;
 }
 
+uprobe_opcode_t __weak arch_uprobe_swbp_insn(struct arch_uprobe *auprobe)
+{
+	return UPROBE_SWBP_INSN;
+}
+
 /**
  * set_swbp - store breakpoint at a given address.
  * @auprobe: arch specific probepoint information.
@@ -315,7 +320,7 @@ put_old:
  */
 int __weak set_swbp(struct arch_uprobe *auprobe, struct mm_struct *mm, unsigned long vaddr)
 {
-	return write_opcode(mm, vaddr, UPROBE_SWBP_INSN);
+	return write_opcode(mm, vaddr, arch_uprobe_swbp_insn(auprobe));
 }
 
 /**
maintainer
#34 lore raw

Re: [PATCH v2 05/13] uprobes: add arch write opcode hook

Quoted 11 lines · Oleg Nesterov

Yes, yes, sorry for confusion. What I actually tried to suggest is something like the trivial patch below.

Then arm can do:

	uprobe_opcode_t arch_uprobe_swbp_insn(struct arch_uprobe *auprobe)
	{
		return __opcode_to_mem_arm(auprobe->bpinsn);
	}
Quoted 7 lines

No?

Quoted 2 lines

I notice there don't seem to be any alternative set_swbp functions in the (rc6) kernel tree

Yes... I think we should simply make it "static". And set_orig_insn() too.

Or. arm can actually reimplement set_swbp(). This doesn't mean the duplication of write_opcode() code, we can simply export this helper.

Either way is imho better than this patch. Unless I missed something.

Oleg.

author
#35 lore raw

Re: [PATCH v2 05/13] uprobes: add arch write opcode hook

Quoted 14 lines · Oleg Nesterov
Quoted 13 lines · Oleg Nesterov

Yes, yes, sorry for confusion. What I actually tried to suggest is something like the trivial patch below.

Then arm can do:

	uprobe_opcode_t arch_uprobe_swbp_insn(struct arch_uprobe *auprobe)
	{
		return __opcode_to_mem_arm(auprobe->bpinsn);
	}

No?

Quoted 9 lines
Quoted 5 lines
Quoted 2 lines

I notice there don't seem to be any alternative set_swbp functions in the (rc6) kernel tree

Yes... I think we should simply make it "static". And set_orig_insn() too.

Or. arm can actually reimplement set_swbp(). This doesn't mean the duplication of write_opcode() code, we can simply export this helper.

That actually looks to me like the cleanest approach. I have changed the static write_opcode() to a global uprobe_write_opcode(), and now call it from an arm set_swbp().

Please do *not* make set_swbp() (and set_orig_insn()) static's. It looks like we now have a use for at least one of them.

Thanks,
-dl

maintainer
#36 lore raw

Re: [PATCH v2 05/13] uprobes: add arch write opcode hook

Quoted 10 lines · David Long
Quoted 4 lines · Oleg Nesterov

Or. arm can actually reimplement set_swbp(). This doesn't mean the duplication of write_opcode() code, we can simply export this helper.

That actually looks to me like the cleanest approach. I have changed the static write_opcode() to a global uprobe_write_opcode(), and now call it from an arm set_swbp().

OK, great.

Quoted 2 lines

Please do *not* make set_swbp() (and set_orig_insn()) static's. It looks like we now have a use for at least one of them.

I am not sure... it still seems to me it makes sense to cleanup this interface...

But. I am not going to do this until we merge your changes. We will see then.

Oleg.

author
#37 lore raw

PATCH v2 6/13 ARM: move shared uprobe/kprobe definitions into new include file

In reply to David Long

From: "David A. Long" <[email protected]>

Separate the kprobe-only definitions from the definitions needed by both kprobes and uprobes.

---
 arch/arm/include/asm/kprobes.h | 15 +--------------
 arch/arm/include/asm/probes.h  | 18 ++++++++++++++++++
 2 files changed, 19 insertions(+), 14 deletions(-)
 create mode 100644 arch/arm/include/asm/probes.h
arch/arm/include/asm/kprobes.h +1 -14
index f82ec22..30fc11b 100644
@@ -28,21 +28,8 @@
 #define kretprobe_blacklist_size	0
 
 typedef u32 kprobe_opcode_t;
-
 struct kprobe;
-typedef void (kprobe_insn_handler_t)(struct kprobe *, struct pt_regs *);
-typedef unsigned long (kprobe_check_cc)(unsigned long);
-typedef void (kprobe_insn_singlestep_t)(struct kprobe *, struct pt_regs *);
-typedef void (kprobe_insn_fn_t)(void);
-
-/* Architecture specific copy of original instruction. */
-struct arch_specific_insn {
-	kprobe_opcode_t			*insn;
-	kprobe_insn_handler_t		*insn_handler;
-	kprobe_check_cc			*insn_check_cc;
-	kprobe_insn_singlestep_t	*insn_singlestep;
-	kprobe_insn_fn_t		*insn_fn;
-};
+#include <asm/probes.h>
 
 struct prev_kprobe {
 	struct kprobe *kp;
arch/arm/include/asm/probes.h new file +18 -0
new file mode 100644
index 0000000..21da148
@@ -0,0 +1,18 @@
+#ifndef _ASM_PROBES_H
+#define _ASM_PROBES_H
+
+typedef void (kprobe_insn_handler_t)(struct kprobe *, struct pt_regs *);
+typedef unsigned long (kprobe_check_cc)(unsigned long);
+typedef void (kprobe_insn_singlestep_t)(struct kprobe *, struct pt_regs *);
+typedef void (kprobe_insn_fn_t)(void);
+
+/* Architecture specific copy of original instruction. */
+struct arch_specific_insn {
+	kprobe_opcode_t			*insn;
+	kprobe_insn_handler_t		*insn_handler;
+	kprobe_check_cc			*insn_check_cc;
+	kprobe_insn_singlestep_t	*insn_singlestep;
+	kprobe_insn_fn_t		*insn_fn;
+};
+
+#endif
Signature · 1 line
1.8.1.2
author
#38 lore raw

PATCH v2 7/13 ARM: move generic thumb instruction parsing code to new files for use by other features

In reply to David Long

From: "David A. Long" <[email protected]>

Move the thumb version of the kprobes instruction parsing code into more generic files from where it can be used by uprobes and possibly other subsystems. The symbol names will be made more generic in a subsequent part of this patchset.

---
 arch/arm/include/asm/probes.h    |   2 +
 arch/arm/kernel/Makefile         |   4 +-
 arch/arm/kernel/kprobes-arm.c    | 723 +-------------------------------------
 arch/arm/kernel/kprobes-common.c | 422 +---------------------
 arch/arm/kernel/kprobes-test.c   |   1 +
 arch/arm/kernel/kprobes-thumb.c  |   1 +
 arch/arm/kernel/kprobes.c        |   1 +
 arch/arm/kernel/kprobes.h        | 372 --------------------
 arch/arm/kernel/probes-arm.c     | 730 +++++++++++++++++++++++++++++++++++++++
 arch/arm/kernel/probes-arm.h     |  38 ++
 arch/arm/kernel/probes.c         | 441 +++++++++++++++++++++++
 arch/arm/kernel/probes.h         | 395 +++++++++++++++++++++
 12 files changed, 1622 insertions(+), 1508 deletions(-)
 create mode 100644 arch/arm/kernel/probes-arm.c
 create mode 100644 arch/arm/kernel/probes-arm.h
 create mode 100644 arch/arm/kernel/probes.c
 create mode 100644 arch/arm/kernel/probes.h
arch/arm/include/asm/probes.h +2 -0
index 21da148..edf5abe 100644
@@ -1,6 +1,8 @@
 #ifndef _ASM_PROBES_H
 #define _ASM_PROBES_H
 
+struct kprobe;
+
 typedef void (kprobe_insn_handler_t)(struct kprobe *, struct pt_regs *);
 typedef unsigned long (kprobe_check_cc)(unsigned long);
 typedef void (kprobe_insn_singlestep_t)(struct kprobe *, struct pt_regs *);
arch/arm/kernel/Makefile +2 -2
index 5140df5f..cdfcb6a 100644
@@ -49,11 +49,11 @@ 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_KPROBES)		+= kprobes.o kprobes-common.o patch.o
+obj-$(CONFIG_KPROBES)		+= probes.o kprobes.o kprobes-common.o patch.o
 ifdef CONFIG_THUMB2_KERNEL
 obj-$(CONFIG_KPROBES)		+= kprobes-thumb.o
 else
-obj-$(CONFIG_KPROBES)		+= kprobes-arm.o
+obj-$(CONFIG_KPROBES)		+= kprobes-arm.o probes-arm.o
 endif
 obj-$(CONFIG_ARM_KPROBES_TEST)	+= test-kprobes.o
 test-kprobes-objs		:= kprobes-test.o
arch/arm/kernel/kprobes-arm.c +10 -713

781 lines not on this page · raw message

arch/arm/kernel/kprobes-common.c +1 -421

433 lines not on this page · raw message

arch/arm/kernel/kprobes-test.c +1 -0
index 0cd63d0..44a8a13 100644
@@ -205,6 +205,7 @@
 #include <asm/opcodes.h>
 
 #include "kprobes.h"
+#include "probes.h"
 #include "kprobes-test.h"
 
 
arch/arm/kernel/kprobes-thumb.c +1 -0
index 6123daf..496281f9 100644
@@ -13,6 +13,7 @@
 #include <linux/module.h>
 
 #include "kprobes.h"
+#include "probes.h"
 
 
 /*
arch/arm/kernel/kprobes.c +1 -0
index 170e9f3..e4bae21 100644
@@ -29,6 +29,7 @@
 #include <asm/cacheflush.h>
 
 #include "kprobes.h"
+#include "probes.h"
 #include "patch.h"
 
 #define MIN_STACK_SIZE(addr) 				\
arch/arm/kernel/kprobes.h +0 -372

377 lines not on this page · raw message

arch/arm/kernel/probes-arm.c new file +730 -0

730 lines not on this page · raw message

arch/arm/kernel/probes-arm.h new file +38 -0
new file mode 100644
index 0000000..8608472
@@ -0,0 +1,38 @@
+/*
+ * arch/arm/kernel/probes-arm.h
+ *
+ * Copyright 2013 Linaro Ltd.
+ * Written by: David A. Long
+ *
+ * The code contained herein is licensed under the GNU General Public
+ * License. You may obtain a copy of the GNU General Public License
+ * Version 2 or later at the following locations:
+ *
+ * http://www.opensource.org/licenses/gpl-license.html
+ * http://www.gnu.org/copyleft/gpl.html
+ */
+
+#ifndef _ARM_KERNEL_PROBES_ARM_H
+#define  _ARM_KERNEL_PROBES_ARM_H
+
+void __kprobes simulate_bbl(struct kprobe *p, struct pt_regs *regs);
+void __kprobes simulate_blx1(struct kprobe *p, struct pt_regs *regs);
+void __kprobes simulate_blx2bx(struct kprobe *p, struct pt_regs *regs);
+void __kprobes simulate_mrs(struct kprobe *p, struct pt_regs *regs);
+void __kprobes simulate_mov_ipsp(struct kprobe *p, struct pt_regs *regs);
+
+void __kprobes emulate_ldrdstrd(struct kprobe *p, struct pt_regs *regs);
+void __kprobes emulate_ldr(struct kprobe *p, struct pt_regs *regs);
+void __kprobes emulate_str(struct kprobe *p, struct pt_regs *regs);
+void __kprobes emulate_rd12rn16rm0rs8_rwflags(struct kprobe *p,
+	struct pt_regs *regs);
+void __kprobes emulate_rd12rn16rm0_rwflags_nopc(struct kprobe *p,
+	struct pt_regs *regs);
+void __kprobes emulate_rd16rn12rm0rs8_rwflags_nopc(struct kprobe *p,
+	struct pt_regs *regs);
+void __kprobes emulate_rd12rm0_noflags_nopc(struct kprobe *p,
+	struct pt_regs *regs);
+void __kprobes emulate_rdlo12rdhi16rn0rm8_rwflags_nopc(struct kprobe *p,
+	struct pt_regs *regs);
+
+#endif
arch/arm/kernel/probes.c new file +441 -0

441 lines not on this page · raw message

arch/arm/kernel/probes.h new file +395 -0

158 lines not on this page · raw message

The rest of this diff is not shown. View the raw message.

Signature · 1 line
1.8.1.2

Part of the diff is not shown here. View the raw message.

#39 lore raw

Re: [PATCH v2 07/13] ARM: move generic thumb instruction parsing code to new files for use by other features

Quoted 3 lines · David Long

From: "David A. Long" <[email protected]>

Move the thumb version of the kprobes instruction parsing code into more generic

This patch is moving the 'ARM' instructions, not 'thumb' instructions, so both description and title needs fixing for that.

Actually, after looking at patch 08/13, that one seems to erroneously include the thumb reorg, I'm guessing that was meant to be in this patch instead, or in a standalone patch?

Quoted 14 lines

files from where it can be used by uprobes and possibly other subsystems. The symbol names will be made more generic in a subsequent part of this patchset.

---
 arch/arm/include/asm/probes.h    |   2 +
 arch/arm/kernel/Makefile         |   4 +-
 arch/arm/kernel/kprobes-arm.c    | 723 +-------------------------------------
 arch/arm/kernel/kprobes-common.c | 422 +---------------------
 arch/arm/kernel/kprobes-test.c   |   1 +
 arch/arm/kernel/kprobes-thumb.c  |   1 +
 arch/arm/kernel/kprobes.c        |   1 +
 arch/arm/kernel/kprobes.h        | 372 --------------------
 arch/arm/kernel/probes-arm.c     | 730 +++++++++++++++++++++++++++++++++++++++
Quoted 8 lines
 arch/arm/kernel/probes-arm.h     |  38 ++
 arch/arm/kernel/probes.c         | 441 +++++++++++++++++++++++
 arch/arm/kernel/probes.h         | 395 +++++++++++++++++++++
 12 files changed, 1622 insertions(+), 1508 deletions(-)
 create mode 100644 arch/arm/kernel/probes-arm.c
 create mode 100644 arch/arm/kernel/probes-arm.h
 create mode 100644 arch/arm/kernel/probes.c
 create mode 100644 arch/arm/kernel/probes.h

A git tip: if you use the -C argument with git format-patch (and send-email I assume) then it will detect files which are copies of others and produce a much smaller patch, as well as a more informative file summary. I believe that this is OK for submissions posted to mailing lists, I know that I've seen people regularly reminded to use -M (which detects file renames).

[...]

arch/arm/kernel/kprobes-arm.c line 66

Quoted 11 lines
arch/arm/kernel/kprobes-arm.c +1 -4
@@ -63,10 +63,8 @@
 #include <linux/module.h>
 
 #include "kprobes.h"
-
-#define sign_extend(x, signbit) ((x) | (0 - ((x) & (1 << (signbit)))))
-
-#define branch_displacement(insn) sign_extend(((insn) & 0xffffff) << 2, 25)
+#include "probes.h"

Rather than include "probes.h" after "kprobes.h" here, (and in the other 7 places in this patch, and other patches) I think it would make more sense to have kprobes.h include probes.h as kprobes code is always going to depend on the new generic probe functions.

I've trimmed the rest of this large patch as I have no other comments except the above.

Signature · 1 line
Tixy
author
#40 lore raw

Re: [PATCH v2 07/13] ARM: move generic thumb instruction parsing code to new files for use by other features

Quoted 7 lines · Jon Medhurst (Tixy)
Quoted 3 lines · David Long

From: "David A. Long" <[email protected]>

Move the thumb version of the kprobes instruction parsing code into more generic

This patch is moving the 'ARM' instructions, not 'thumb' instructions, so both description and title needs fixing for that.

Indeed. I've fixed it for the next version.

Quoted 3 lines

Actually, after looking at patch 08/13, that one seems to erroneously include the thumb reorg, I'm guessing that was meant to be in this patch instead, or in a standalone patch?

I've now broken the thumb reorg out into its own patch with appropriate description.

Quoted 23 lines
Quoted 18 lines

files from where it can be used by uprobes and possibly other subsystems. The symbol names will be made more generic in a subsequent part of this patchset.

---
  arch/arm/include/asm/probes.h    |   2 +
  arch/arm/kernel/Makefile         |   4 +-
  arch/arm/kernel/kprobes-arm.c    | 723 +-------------------------------------
  arch/arm/kernel/kprobes-common.c | 422 +---------------------
  arch/arm/kernel/kprobes-test.c   |   1 +
  arch/arm/kernel/kprobes-thumb.c  |   1 +
  arch/arm/kernel/kprobes.c        |   1 +
  arch/arm/kernel/kprobes.h        | 372 --------------------
  arch/arm/kernel/probes-arm.c     | 730 +++++++++++++++++++++++++++++++++++++++
  arch/arm/kernel/probes-arm.h     |  38 ++
  arch/arm/kernel/probes.c         | 441 +++++++++++++++++++++++
  arch/arm/kernel/probes.h         | 395 +++++++++++++++++++++
  12 files changed, 1622 insertions(+), 1508 deletions(-)
Quoted 4 lines

create mode 100644 arch/arm/kernel/probes-arm.c
create mode 100644 arch/arm/kernel/probes-arm.h
create mode 100644 arch/arm/kernel/probes.c
create mode 100644 arch/arm/kernel/probes.h

Quoted 8 lines

A git tip: if you use the -C argument with git format-patch (and send-email I assume) then it will detect files which are copies of others and produce a much smaller patch, as well as a more informative file summary. I believe that this is OK for submissions posted to mailing lists, I know that I've seen people regularly reminded to use -M (which detects file renames).

[...]

I will give this a try with the next round.

Quoted 6 lines
Quoted 6 lines
arch/arm/kernel/kprobes-arm.c +0 -0
@@ -63,10 +63,8 @@
  #include <linux/module.h>
 
Quoted 11 lines

arch/arm/kernel/kprobes-arm.c line 66

Quoted 6 lines
arch/arm/kernel/kprobes-arm.c +1 -4
  #include "kprobes.h"
-
-#define sign_extend(x, signbit) ((x) | (0 - ((x) & (1 << (signbit)))))
-
-#define branch_displacement(insn) sign_extend(((insn) & 0xffffff) << 2, 25)
+#include "probes.h"

Rather than include "probes.h" after "kprobes.h" here, (and in the other 7 places in this patch, and other patches) I think it would make more sense to have kprobes.h include probes.h as kprobes code is always going to depend on the new generic probe functions.

Suggested change applied for next version.

-dl

author
#41 lore raw

PATCH v2 8/13 ARM: use a function table for determining instruction interpreter actions

In reply to David Long

From: "David A. Long" <[email protected]>

When interpreting instructions use a client-supplied array of functions to take action. This array can then be provided by krpobes, uprobes, or possibly some other client subsystem. The interpreter assigns a group number to each recognized instruction and invokes the corresponding action function.

---
 arch/arm/kernel/Makefile         |    2 +-
 arch/arm/kernel/kprobes-arm.c    |   41 ++
 arch/arm/kernel/kprobes-common.c |    3 +-
 arch/arm/kernel/kprobes-thumb.c  | 1016 ++++----------------------------------
 arch/arm/kernel/kprobes.c        |   10 +-
 arch/arm/kernel/kprobes.h        |   14 +-
 arch/arm/kernel/probes-arm.c     |  114 ++---
 arch/arm/kernel/probes-arm.h     |   37 ++
 arch/arm/kernel/probes-thumb.c   |  875 ++++++++++++++++++++++++++++++++
 arch/arm/kernel/probes-thumb.h   |  136 +++++
 arch/arm/kernel/probes.c         |    9 +-
 arch/arm/kernel/probes.h         |   15 +-
 12 files changed, 1288 insertions(+), 984 deletions(-)
 create mode 100644 arch/arm/kernel/probes-thumb.c
 create mode 100644 arch/arm/kernel/probes-thumb.h
arch/arm/kernel/Makefile +1 -1
index cdfcb6a..2fe6139 100644
@@ -51,7 +51,7 @@ obj-$(CONFIG_JUMP_LABEL)	+= jump_label.o insn.o patch.o
 obj-$(CONFIG_KEXEC)		+= machine_kexec.o relocate_kernel.o
 obj-$(CONFIG_KPROBES)		+= probes.o kprobes.o kprobes-common.o patch.o
 ifdef CONFIG_THUMB2_KERNEL
-obj-$(CONFIG_KPROBES)		+= kprobes-thumb.o
+obj-$(CONFIG_KPROBES)		+= kprobes-thumb.o probes-thumb.o
 else
 obj-$(CONFIG_KPROBES)		+= kprobes-arm.o probes-arm.o
 endif
arch/arm/kernel/kprobes-arm.c +41 -0

44 lines not on this page · raw message

arch/arm/kernel/kprobes-common.c +2 -1
index 28c41b6..cea610a 100644
@@ -114,7 +114,8 @@ emulate_ldm_r3_15(struct kprobe *p, struct pt_regs *regs)
 }
 
 enum kprobe_insn __kprobes
-kprobe_decode_ldmstm(kprobe_opcode_t insn, struct arch_specific_insn *asi)
+kprobe_decode_ldmstm(kprobe_opcode_t insn, struct arch_specific_insn *asi,
+		const struct decode_header *h)
 {
 	kprobe_insn_handler_t *handler = 0;
 	unsigned reglist = insn & 0xffff;
arch/arm/kernel/kprobes-thumb.c +105 -911

1,233 lines not on this page · raw message

arch/arm/kernel/kprobes.c +8 -2
index e4bae21..c72d552 100644
@@ -55,6 +55,7 @@ int __kprobes arch_prepare_kprobe(struct kprobe *p)
 	unsigned long addr = (unsigned long)p->addr;
 	bool thumb;
 	kprobe_decode_insn_t *decode_insn;
+	const union decode_item *actions;
 	int is;
 
 	if (in_exception_text(addr))
@@ -68,20 +69,25 @@ int __kprobes arch_prepare_kprobe(struct kprobe *p)
 		insn <<= 16;
 		insn |= ((u16 *)addr)[1];
 		decode_insn = thumb32_kprobe_decode_insn;
-	} else
+		actions = kprobes_t32_actions;
+	} else {
 		decode_insn = thumb16_kprobe_decode_insn;
+		actions = kprobes_t16_actions;
+	}
 #else /* !CONFIG_THUMB2_KERNEL */
 	thumb = false;
 	if (addr & 0x3)
 		return -EINVAL;
 	insn = *p->addr;
 	decode_insn = arm_kprobe_decode_insn;
+	actions = kprobes_arm_actions;
 #endif
 
 	p->opcode = insn;
 	p->ainsn.insn = tmp_insn;
 
-	switch ((*decode_insn)(insn, &p->ainsn)) {
+	switch ((*decode_insn)
+		(insn, &p->ainsn, (struct decode_header *) actions)) {
 	case INSN_REJECTED:	/* not supported */
 		return -EINVAL;
 
arch/arm/kernel/kprobes.h +10 -4
index 7315830..77e4374 100644
@@ -27,6 +27,7 @@
 #define KPROBE_THUMB16_BREAKPOINT_INSTRUCTION	0xde18
 #define KPROBE_THUMB32_BREAKPOINT_INSTRUCTION	0xf7f0a018
 
+struct decode_header;
 
 enum kprobe_insn {
 	INSN_REJECTED,
@@ -35,19 +36,24 @@ enum kprobe_insn {
 };
 
 typedef enum kprobe_insn (kprobe_decode_insn_t)(kprobe_opcode_t,
-						struct arch_specific_insn *);
+		struct arch_specific_insn *,
+		const struct decode_header *actions);
 
 #ifdef CONFIG_THUMB2_KERNEL
 
 enum kprobe_insn thumb16_kprobe_decode_insn(kprobe_opcode_t,
-						struct arch_specific_insn *);
+						struct arch_specific_insn *,
+						const struct decode_header *);
 enum kprobe_insn thumb32_kprobe_decode_insn(kprobe_opcode_t,
-						struct arch_specific_insn *);
+						struct arch_specific_insn *,
+						const struct decode_header *);
 
 #else /* !CONFIG_THUMB2_KERNEL */
 
 enum kprobe_insn arm_kprobe_decode_insn(kprobe_opcode_t,
-					struct arch_specific_insn *);
+					struct arch_specific_insn *,
+					const struct decode_header *);
+
 #endif
 
 void __init arm_kprobe_decode_init(void);
arch/arm/kernel/probes-arm.c +58 -56

388 lines not on this page · raw message

arch/arm/kernel/probes-arm.h +37 -0
index 8608472..f8e0f7a 100644
@@ -15,6 +15,43 @@
 #ifndef _ARM_KERNEL_PROBES_ARM_H
 #define  _ARM_KERNEL_PROBES_ARM_H
 
+enum probes_arm_action {
+	PROBES_EMULATE_NONE,
+	PROBES_SIMULATE_NOP,
+	PROBES_PRELOAD_IMM,
+	PROBES_PRELOAD_REG,
+	PROBES_BRANCH_IMM,
+	PROBES_BRANCH_REG,
+	PROBES_MRS,
+	PROBES_CLZ,
+	PROBES_SATURATING_ARITHMETIC,
+	PROBES_MUL1,
+	PROBES_MUL2,
+	PROBES_SWP,
+	PROBES_LDRSTRD,
+	PROBES_LOAD,
+	PROBES_STORE,
+	PROBES_LOAD_EXTRA,
+	PROBES_STORE_EXTRA,
+	PROBES_MOV_IP_SP,
+	PROBES_DATA_PROCESSING_REG,
+	PROBES_DATA_PROCESSING_IMM,
+	PROBES_MOV_HALFWORD,
+	PROBES_SEV,
+	PROBES_WFE,
+	PROBES_SATURATE,
+	PROBES_REV,
+	PROBES_MMI,
+	PROBES_PACK,
+	PROBES_EXTEND,
+	PROBES_EXTEND_ADD,
+	PROBES_MUL_ADD_LONG,
+	PROBES_MUL_ADD,
+	PROBES_BITFIELD,
+	PROBES_BRANCH,
+	PROBES_LDMSTM
+};
+
 void __kprobes simulate_bbl(struct kprobe *p, struct pt_regs *regs);
 void __kprobes simulate_blx1(struct kprobe *p, struct pt_regs *regs);
 void __kprobes simulate_blx2bx(struct kprobe *p, struct pt_regs *regs);
arch/arm/kernel/probes-thumb.c new file +875 -0

875 lines not on this page · raw message

arch/arm/kernel/probes-thumb.h new file +136 -0

136 lines not on this page · raw message

arch/arm/kernel/probes.c +5 -4
index 9b30486..cdfb3c5 100644
@@ -379,7 +379,8 @@ static const int decode_struct_sizes[NUM_DECODE_TYPES] = {
  */
 int __kprobes
 kprobe_decode_insn(kprobe_opcode_t insn, struct arch_specific_insn *asi,
-				const union decode_item *table, bool thumb)
+				const union decode_item *table, bool thumb,
+				const union decode_item *actions)
 {
 	const struct decode_header *h = (struct decode_header *)table;
 	const struct decode_header *next;
@@ -413,18 +414,18 @@ kprobe_decode_insn(kprobe_opcode_t insn, struct arch_specific_insn *asi,
 
 		case DECODE_TYPE_CUSTOM: {
 			struct decode_custom *d = (struct decode_custom *)h;
-			return (*d->decoder.decoder)(insn, asi);
+			return actions[d->decoder.bits].decoder(insn, asi, h);
 		}
 
 		case DECODE_TYPE_SIMULATE: {
 			struct decode_simulate *d = (struct decode_simulate *)h;
-			asi->insn_handler = d->handler.handler;
+			asi->insn_handler = actions[d->handler.bits].handler;
 			return INSN_GOOD_NO_SLOT;
 		}
 
 		case DECODE_TYPE_EMULATE: {
 			struct decode_emulate *d = (struct decode_emulate *)h;
-			asi->insn_handler = d->handler.handler;
+			asi->insn_handler = actions[d->handler.bits].handler;
 			set_emulated_insn(insn, asi, thumb);
 			return INSN_GOOD;
 		}
arch/arm/kernel/probes.h +10 -5
index d14d224..ad87d68 100644
@@ -131,7 +131,8 @@ void __kprobes kprobe_simulate_nop(struct kprobe *p, struct pt_regs *regs);
 void __kprobes kprobe_emulate_none(struct kprobe *p, struct pt_regs *regs);
 
 enum kprobe_insn __kprobes
-kprobe_decode_ldmstm(kprobe_opcode_t insn, struct arch_specific_insn *asi);
+kprobe_decode_ldmstm(kprobe_opcode_t insn, struct arch_specific_insn *asi,
+		     const struct decode_header *h);
 
 /*
  * Test if load/store instructions writeback the address register.
@@ -334,7 +335,7 @@ struct decode_custom {
 
 #define DECODE_CUSTOM(_mask, _value, _decoder)			\
 	DECODE_HEADER(DECODE_TYPE_CUSTOM, _mask, _value, 0),	\
-	{.decoder = (_decoder)}
+	{.bits = (_decoder)}
 
 
 struct decode_simulate {
@@ -344,7 +345,7 @@ struct decode_simulate {
 
 #define DECODE_SIMULATEX(_mask, _value, _handler, _regs)		\
 	DECODE_HEADER(DECODE_TYPE_SIMULATE, _mask, _value, _regs),	\
-	{.handler = (_handler)}
+	{.bits = (_handler)}
 
 #define DECODE_SIMULATE(_mask, _value, _handler)	\
 	DECODE_SIMULATEX(_mask, _value, _handler, 0)
@@ -357,7 +358,7 @@ struct decode_emulate {
 
 #define DECODE_EMULATEX(_mask, _value, _handler, _regs)			\
 	DECODE_HEADER(DECODE_TYPE_EMULATE, _mask, _value, _regs),	\
-	{.handler = (_handler)}
+	{.bits = (_handler)}
 
 #define DECODE_EMULATE(_mask, _value, _handler)		\
 	DECODE_EMULATEX(_mask, _value, _handler, 0)
@@ -382,14 +383,18 @@ struct decode_reject {
 #ifdef CONFIG_THUMB2_KERNEL
 extern const union decode_item kprobe_decode_thumb16_table[];
 extern const union decode_item kprobe_decode_thumb32_table[];
+extern const union decode_item kprobes_t32_actions[];
+extern const union decode_item kprobes_t16_actions[];
 #else
 extern const union decode_item kprobe_decode_arm_table[];
+extern const union decode_item kprobes_arm_actions[];
 #endif
 
 extern kprobe_check_cc * const kprobe_condition_checks[16];
 
 
 int kprobe_decode_insn(kprobe_opcode_t insn, struct arch_specific_insn *asi,
-			const union decode_item *table, bool thumb16);
+			const union decode_item *table, bool thumb16,
+			const union decode_item *actions);
 
 #endif
Signature · 1 line
1.8.1.2
#42 lore raw

Re: [PATCH v2 08/13] ARM: use a function table for determining instruction interpreter actions

Quoted 4 lines · David Long

From: "David A. Long" <[email protected]>

When interpreting instructions use a client-supplied array of functions to take action. This array can then be provided by krpobes, uprobes, or

'to take action' sounds a bit weird to me, how about 'to specify actions'?

Quoted 5 lines

possibly some other client subsystem. The interpreter assigns a group number to each recognized instruction and invokes the corresponding action function.

Quoted 16 lines
---
 arch/arm/kernel/Makefile         |    2 +-
 arch/arm/kernel/kprobes-arm.c    |   41 ++
 arch/arm/kernel/kprobes-common.c |    3 +-
 arch/arm/kernel/kprobes-thumb.c  | 1016 ++++----------------------------------
 arch/arm/kernel/kprobes.c        |   10 +-
 arch/arm/kernel/kprobes.h        |   14 +-
 arch/arm/kernel/probes-arm.c     |  114 ++---
 arch/arm/kernel/probes-arm.h     |   37 ++
 arch/arm/kernel/probes-thumb.c   |  875 ++++++++++++++++++++++++++++++++
 arch/arm/kernel/probes-thumb.h   |  136 +++++
 arch/arm/kernel/probes.c         |    9 +-
 arch/arm/kernel/probes.h         |   15 +-
 12 files changed, 1288 insertions(+), 984 deletions(-)
 create mode 100644 arch/arm/kernel/probes-thumb.c
 create mode 100644 arch/arm/kernel/probes-thumb.h

As I mentioned in comments to patch 7, this patch seems to accidentally include the thumb code reorg of moving code from kprobes-thumb.c to probes-thumb.c

I've no other comments on the patch so trimmed the rest...

Signature · 1 line
Tixy
author
#43 lore raw

Re: [PATCH v2 08/13] ARM: use a function table for determining instruction interpreter actions

Quoted 8 lines · Jon Medhurst (Tixy)
Quoted 4 lines · David Long

From: "David A. Long" <[email protected]>

When interpreting instructions use a client-supplied array of functions to take action. This array can then be provided by krpobes, uprobes, or

'to take action' sounds a bit weird to me, how about 'to specify actions'?

I've changed it to:

ARM: use a function table for determining instruction interpreter actions

Make the instruction interpreter call back to semantic action functions through a function pointer array provided by the invoker. The interpreter decodes the instructions into groups and uses the group number to index into the supplied array. kprobes and uprobes code will each supply their own array of functions.

Quoted 22 lines
Quoted 19 lines

possibly some other client subsystem. The interpreter assigns a group number to each recognized instruction and invokes the corresponding action function.

---
  arch/arm/kernel/Makefile         |    2 +-
  arch/arm/kernel/kprobes-arm.c    |   41 ++
  arch/arm/kernel/kprobes-common.c |    3 +-
  arch/arm/kernel/kprobes-thumb.c  | 1016 ++++----------------------------------
  arch/arm/kernel/kprobes.c        |   10 +-
  arch/arm/kernel/kprobes.h        |   14 +-
  arch/arm/kernel/probes-arm.c     |  114 ++---
  arch/arm/kernel/probes-arm.h     |   37 ++
  arch/arm/kernel/probes-thumb.c   |  875 ++++++++++++++++++++++++++++++++
  arch/arm/kernel/probes-thumb.h   |  136 +++++
  arch/arm/kernel/probes.c         |    9 +-
  arch/arm/kernel/probes.h         |   15 +-
  12 files changed, 1288 insertions(+), 984 deletions(-)
Quoted 2 lines

create mode 100644 arch/arm/kernel/probes-thumb.c create mode 100644 arch/arm/kernel/probes-thumb.h

Quoted 3 lines

As I mentioned in comments to patch 7, this patch seems to accidentally include the thumb code reorg of moving code from kprobes-thumb.c to probes-thumb.c

Fixed for next version (as described in previous email).

-dl

author
#44 lore raw

PATCH v2 9/13 ARM: Disable jprobe selftests in thumb kernels

In reply to David Long

From: "David A. Long" <[email protected]>

jprobe kernel selftests are not supported for thumb kernels. Conditionally disable them in the kernel kprobes-test module.

---
 arch/arm/kernel/kprobes-test.c | 4 ++++
 1 file changed, 4 insertions(+)
arch/arm/kernel/kprobes-test.c +4 -0
index 44a8a13..2144df1 100644
@@ -358,6 +358,7 @@ static int test_kprobe(long (*func)(long, long))
 	return 0;
 }
 
+#ifndef CONFIG_THUMB2_KERNEL
 static void __kprobes jprobe_func(long r0, long r1)
 {
 	jprobe_func_called = test_func_instance;
@@ -401,6 +402,7 @@ static int test_jprobe(long (*func)(long, long))
 
 	return 0;
 }
+#endif
 
 static int __kprobes
 kretprobe_handler(struct kretprobe_instance *ri, struct pt_regs *regs)
@@ -456,10 +458,12 @@ static int run_api_tests(long (*func)(long, long))
 	if (ret < 0)
 		return ret;
 
+#ifndef CONFIG_THUMB2_KERNEL
 	pr_info("    jprobe\n");
 	ret = test_jprobe(func);
 	if (ret < 0)
 		return ret;
+#endif
 
 	pr_info("    kretprobe\n");
 	ret = test_kretprobe(func);
Signature · 1 line
1.8.1.2
#45 lore raw

Re: [PATCH v2 09/13] ARM: Disable jprobe selftests in thumb kernels

Quoted 4 lines · David Long

From: "David A. Long" <[email protected]>

jprobe kernel selftests are not supported for thumb kernels. Conditionally disable them in the kernel kprobes-test module.

I don't think it's fair to say they aren't supported, it's just that the implementation of jprobes and/or symbol lookup has bugs on Thumb kernels which the test code is finding, see
http://lists.infradead.org/pipermail/linux-arm-kernel/2011-August/063026.html

Note, the current code works OK if the function being probed is in a loadable module (which is why I didn't spot the problem when doing the original Thumb kprobes work).

Now I admit that having the tests always bombing out because of this hinders testing of kprobes, but simply disabling the test is just burying this long standing problem even more. So what do people think about something like the change below, to let other tests get run but make the overall test still fail...?

arch/arm/kernel/kprobes-test.c +10 -0
@@ -221,6 +221,7 @@ static int pre_handler_called;
 static int post_handler_called;
 static int jprobe_func_called;
 static int kretprobe_handler_called;
+static int tests_failed;
 
 #define FUNC_ARG1 0x12345678
 #define FUNC_ARG2 0xabcdef
@@ -457,6 +458,13 @@ static int run_api_tests(long (*func)(long, long))
 
 	pr_info("    jprobe\n");
 	ret = test_jprobe(func);
+#if defined(CONFIG_THUMB2_KERNEL) && !defined(MODULE)
+	if (ret == -EINVAL) {
+		pr_err("FAIL: Known longtime bug with jprobe on Thumb kernels");
+		tests_failed = ret;
+		ret = 0;
+	}
+#endif
 	if (ret < 0)
 		return ret;
 
@@ -1667,6 +1675,8 @@ static int __init run_all_tests(void)
 
 out:
 	if (ret == 0)
+		ret = tests_failed;
+	if (ret == 0)
 		pr_info("Finished kprobe tests OK\n");
 	else
 		pr_err("kprobe tests failed\n");
Signature · 1 line
Tixy
author
#46 lore raw

Re: [PATCH v2 09/13] ARM: Disable jprobe selftests in thumb kernels

Quoted 50 lines · Jon Medhurst (Tixy)
Quoted 4 lines · David Long

From: "David A. Long" <[email protected]>

jprobe kernel selftests are not supported for thumb kernels. Conditionally disable them in the kernel kprobes-test module.

I don't think it's fair to say they aren't supported, it's just that the implementation of jprobes and/or symbol lookup has bugs on Thumb kernels which the test code is finding, see
http://lists.infradead.org/pipermail/linux-arm-kernel/2011-August/063026.html

Note, the current code works OK if the function being probed is in a loadable module (which is why I didn't spot the problem when doing the original Thumb kprobes work).

Now I admit that having the tests always bombing out because of this hinders testing of kprobes, but simply disabling the test is just burying this long standing problem even more. So what do people think about something like the change below, to let other tests get run but make the overall test still fail...?

arch/arm/kernel/kprobes-test.c +8 -0
@@ -221,6 +221,7 @@ static int pre_handler_called;
  static int post_handler_called;
  static int jprobe_func_called;
  static int kretprobe_handler_called;
+static int tests_failed;
 
  #define FUNC_ARG1 0x12345678
  #define FUNC_ARG2 0xabcdef
@@ -457,6 +458,13 @@ static int run_api_tests(long (*func)(long, long))
 
  	pr_info("    jprobe\n");
  	ret = test_jprobe(func);
+#if defined(CONFIG_THUMB2_KERNEL) && !defined(MODULE)
+	if (ret == -EINVAL) {
+		pr_err("FAIL: Known longtime bug with jprobe on Thumb kernels");
+		tests_failed = ret;
+		ret = 0;
+	}
+#endif
  	if (ret < 0)
  		return ret;
 

arch/arm/kernel/kprobes-test.c static int __init run_all_tests(void) · line 1682

Quoted 8 lines
arch/arm/kernel/kprobes-test.c +2 -0
@@ -1667,6 +1675,8 @@ static int __init run_all_tests(void)
 
  out:
  	if (ret == 0)
+		ret = tests_failed;
+	if (ret == 0)
  		pr_info("Finished kprobe tests OK\n");
  	else
  		pr_err("kprobe tests failed\n");

Thanks for clarifying the problem Tixy. I agree we should try and allow the tests to run for these more typical use cases where they do actually work. I have tested your patch and I will use it in place of mine unless there are strong objections.

-dl

author
#47 lore raw

PATCH v2 10/13 kprobes: Remove uneeded kernel dependency on struct arch_specific_insn

In reply to David Long

From: "David A. Long" <[email protected]>

Instead of depending on include/asm/kprobes.h to provide a dummy definition for struct arch_specific_insn, do so in include/linux/kprobes.h.

---
 include/linux/kprobes.h | 7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)
include/linux/kprobes.h +4 -3
index 925eaf2..4b5a74d 100644
@@ -52,9 +52,6 @@
 
 #else /* CONFIG_KPROBES */
 typedef int kprobe_opcode_t;
-struct arch_specific_insn {
-	int dummy;
-};
 #endif /* CONFIG_KPROBES */
 
 struct kprobe;
@@ -110,7 +107,11 @@ struct kprobe {
 	kprobe_opcode_t opcode;
 
 	/* copy of the original instruction */
+#ifdef CONFIG_KPROBES
 	struct arch_specific_insn ainsn;
+#else
+	int ainsn;
+#endif
 
 	/*
 	 * Indicates various status flags.
Signature · 1 line
1.8.1.2
#48 lore raw

Re: [PATCH v2 10/13] kprobes: Remove uneeded kernel dependency on struct arch_specific_insn

Quoted 4 lines · David Long

From: "David A. Long" <[email protected]>

Instead of depending on include/asm/kprobes.h to provide a dummy definition for struct arch_specific_insn, do so in include/linux/kprobes.h.

That change description doesn't quite seem to quite make sense to me.

Anyway, what we're trying to do with this patch is to allow us to use arch_specific_insn for purposes additional to implementing kprobes. This patch enables that but I'm wary that the kprobes code assumes that ainsn is a struct arch_specific_insn, e.g. in linux/kernel/kprobes.c we have:

	memcpy(&p->ainsn, &ap->ainsn, sizeof(struct arch_specific_insn));

Now, that code isn't compiled when kprobes isn't configured, but it seams to me to be safer if that was also changed to

	memcpy(&p->ainsn, &ap->ainsn, sizeof(p->ainsn));

However, I also wonder if we should instead leave arch_specific_insn as a kprobes specific structure and on ARM define it in terms of a new more generic 'struct probe_insn'? The drawback with that is that we'd probably end up with a struct just containing a single member which seems a bit redundant:

struct arch_specific_insn {
	struct probe_insn pinsn;
};

Thought's anyone?

Quoted 32 lines
---
 include/linux/kprobes.h | 7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)
include/linux/kprobes.h +4 -3
index 925eaf2..4b5a74d 100644
@@ -52,9 +52,6 @@
 
 #else /* CONFIG_KPROBES */
 typedef int kprobe_opcode_t;
-struct arch_specific_insn {
-	int dummy;
-};
 #endif /* CONFIG_KPROBES */
 
 struct kprobe;
@@ -110,7 +107,11 @@ struct kprobe {
 	kprobe_opcode_t opcode;
 
 	/* copy of the original instruction */
+#ifdef CONFIG_KPROBES
 	struct arch_specific_insn ainsn;
+#else
+	int ainsn;
+#endif
 
 	/*
 	 * Indicates various status flags.
Signature · 1 line
Tixy
#49 lore raw

Re: Re: [PATCH v2 10/13] kprobes: Remove uneeded kernel dependency on struct arch_specific_insn

Quoted 13 lines · Jon Medhurst (Tixy)
Quoted 4 lines · David Long

From: "David A. Long" <[email protected]>

Instead of depending on include/asm/kprobes.h to provide a dummy definition for struct arch_specific_insn, do so in include/linux/kprobes.h.

That change description doesn't quite seem to quite make sense to me.

Anyway, what we're trying to do with this patch is to allow us to use arch_specific_insn for purposes additional to implementing kprobes. This patch enables that but I'm wary that the kprobes code assumes that ainsn is a struct arch_specific_insn, e.g. in linux/kernel/kprobes.c we have:

Quoted 6 lines
	memcpy(&p->ainsn, &ap->ainsn, sizeof(struct arch_specific_insn));

Now, that code isn't compiled when kprobes isn't configured, but it seams to me to be safer if that was also changed to

	memcpy(&p->ainsn, &ap->ainsn, sizeof(p->ainsn));

This kind of cleanup looks good for me, but I don't agree to change the type of the member (removing is OK) by Kconfig. If you want to change the framework of kprobes and uprobes itself (unification), I'm appreciate to discuss with you and uprobes people, because it will involve all arch dependent code change, *NOT ONLY* the ARM issue.

Quoted 9 lines

However, I also wonder if we should instead leave arch_specific_insn as a kprobes specific structure and on ARM define it in terms of a new more generic 'struct probe_insn'? The drawback with that is that we'd probably end up with a struct just containing a single member which seems a bit redundant:

struct arch_specific_insn {
	struct probe_insn pinsn;
};

I also disagree it. If you have a plan to integrate uprobes and kprobes arch specific code, please share it with us. I'm happy to work with you. There are already many maintainers on each feature who is responsible for it (even it is a piece of code), and scripts/get_maintainers.pl gives you who are.

Srikar, Oleg, I think it's a good time to merge such arch_specific mechanism of uprobes and kprobes. Would you think we can do similar thing on x86 too?

Thank you,

Signature · 4 lines
Masami HIRAMATSU
IT Management Research Dept. Linux Technology Center
Hitachi, Ltd., Yokohama Research Laboratory
E-mail: masami.hiramatsu.pt at hitachi.com
#50 lore raw

Re: Re: [PATCH v2 10/13] kprobes: Remove uneeded kernel dependency on struct arch_specific_insn

Quoted 16 lines · Masami Hiramatsu
Quoted 15 lines · Jon Medhurst (Tixy)
Quoted 4 lines · David Long

From: "David A. Long" <[email protected]>

Instead of depending on include/asm/kprobes.h to provide a dummy definition for struct arch_specific_insn, do so in include/linux/kprobes.h.

That change description doesn't quite seem to quite make sense to me.

Anyway, what we're trying to do with this patch is to allow us to use arch_specific_insn for purposes additional to implementing kprobes. This patch enables that but I'm wary that the kprobes code assumes that ainsn is a struct arch_specific_insn, e.g. in linux/kernel/kprobes.c we have:

	memcpy(&p->ainsn, &ap->ainsn, sizeof(struct arch_specific_insn));
Quoted 7 lines
Quoted 4 lines

Now, that code isn't compiled when kprobes isn't configured, but it seams to me to be safer if that was also changed to

	memcpy(&p->ainsn, &ap->ainsn, sizeof(p->ainsn));

This kind of cleanup looks good for me, but I don't agree to change the type of the member (removing is OK) by Kconfig.

Wouldn't that still require an #ifdef CONFIG_KPROBES around ainsn? Admittedly a less ugly one than one to change its type to an int.

Quoted 4 lines

If you want to
change the framework of kprobes and uprobes itself (unification), I'm appreciate to discuss with you and uprobes people, because it will involve all arch dependent code change, *NOT ONLY* the ARM issue.

Well, I don't think the goal wasn't unification as such. For kprobes on ARM we have to decode and simulate pretty much the entire instruction set(s) and the attempt to implement uprobes on ARM have tried to make use of as much of that as possible. The tricky bit has been as to where to try and draw the level of abstraction, and it seems this may well be leaking out of the arch specific arena.

Bit of background, Dave Long has been working on ARM uprobes based on Rabin Vincent's earlier work, and I, as author of a large part of the current ARM kprobes code, have been reviewing (not very satisfactorily I admit) the bits that impact that. One of my motivations has been to push the kprobes instruction decoding to be more generic, rather than special casing things to cope with uprobes. This is because I'm aware of the reoccurring theme on the ARM lists that it would be good to not have all the different methods of instruction decoding, for probes, debug and simulation, etc. (I'm sceptical that a one-size-fits-all is possible, but consolidation where practical is good).

Quoted 7 lines
Quoted 7 lines

However, I also wonder if we should instead leave arch_specific_insn as a kprobes specific structure and on ARM define it in terms of a new more generic 'struct probe_insn'? The drawback with that is that we'd probably end up with a struct just containing a single member which seems a bit redundant:

Quoted 6 lines
Quoted 3 lines
struct arch_specific_insn {
	struct probe_insn pinsn;
};

I also disagree it. If you have a plan to integrate uprobes and kprobes arch specific code, please share it with us.

There's not really a 'plan', just an attempt to reuse the instruction decoding code used by kprobes in the implementation of uprobes, i.e. the patch series [1] which this mail thread is in reply to.

[1] http://thread.gmane.org/gmane.linux.kernel/1579985

Quoted 8 lines

I'm happy to work with you.
There are already many maintainers on each feature who is responsible for it (even it is a piece of code), and scripts/get_maintainers.pl gives you who are.

Srikar, Oleg, I think it's a good time to merge such arch_specific mechanism of uprobes and kprobes. Would you think we can do similar thing on x86 too?

Signature · 1 line
Tixy
author
#51 lore raw

Re: [PATCH v2 10/13] kprobes: Remove uneeded kernel dependency on struct arch_specific_insn

Quoted 15 lines · Jon Medhurst (Tixy)
Quoted 14 lines · Masami Hiramatsu
Quoted 13 lines · Jon Medhurst (Tixy)
Quoted 4 lines · David Long

From: "David A. Long" <[email protected]>

Instead of depending on include/asm/kprobes.h to provide a dummy definition for struct arch_specific_insn, do so in include/linux/kprobes.h.

That change description doesn't quite seem to quite make sense to me.

Anyway, what we're trying to do with this patch is to allow us to use arch_specific_insn for purposes additional to implementing kprobes. This patch enables that but I'm wary that the kprobes code assumes that ainsn is a struct arch_specific_insn, e.g. in linux/kernel/kprobes.c we have:

Quoted 7 lines
Quoted 7 lines
Quoted 6 lines
	memcpy(&p->ainsn, &ap->ainsn, sizeof(struct arch_specific_insn));

Now, that code isn't compiled when kprobes isn't configured, but it seams to me to be safer if that was also changed to

	memcpy(&p->ainsn, &ap->ainsn, sizeof(p->ainsn));

That does look like an important improvement.

Quoted 6 lines
Quoted 2 lines

This kind of cleanup looks good for me, but I don't agree to change the type of the member (removing is OK) by Kconfig.

Wouldn't that still require an #ifdef CONFIG_KPROBES around ainsn? Admittedly a less ugly one than one to change its type to an int.

It is also possible to make the include of asm/kprobes.h unconditional, although that might only cause the #ifdef to appear in more than one include file.

Quoted 34 lines
Quoted 4 lines

If you want to
change the framework of kprobes and uprobes itself (unification), I'm appreciate to discuss with you and uprobes people, because it will involve all arch dependent code change, *NOT ONLY* the ARM issue.

Well, I don't think the goal wasn't unification as such. For kprobes on ARM we have to decode and simulate pretty much the entire instruction set(s) and the attempt to implement uprobes on ARM have tried to make use of as much of that as possible. The tricky bit has been as to where to try and draw the level of abstraction, and it seems this may well be leaking out of the arch specific arena.

Bit of background, Dave Long has been working on ARM uprobes based on Rabin Vincent's earlier work, and I, as author of a large part of the current ARM kprobes code, have been reviewing (not very satisfactorily I admit) the bits that impact that. One of my motivations has been to push the kprobes instruction decoding to be more generic, rather than special casing things to cope with uprobes. This is because I'm aware of the reoccurring theme on the ARM lists that it would be good to not have all the different methods of instruction decoding, for probes, debug and simulation, etc. (I'm sceptical that a one-size-fits-all is possible, but consolidation where practical is good).

Quoted 11 lines
Quoted 10 lines

However, I also wonder if we should instead leave arch_specific_insn as a kprobes specific structure and on ARM define it in terms of a new more generic 'struct probe_insn'? The drawback with that is that we'd probably end up with a struct just containing a single member which seems a bit redundant:

struct arch_specific_insn {
	struct probe_insn pinsn;
};
Quoted 14 lines
Quoted 2 lines

I also disagree it. If you have a plan to integrate uprobes and kprobes arch specific code, please share it with us.

There's not really a 'plan', just an attempt to reuse the instruction decoding code used by kprobes in the implementation of uprobes, i.e. the patch series [1] which this mail thread is in reply to.

[1] http://thread.gmane.org/gmane.linux.kernel/1579985

Quoted 5 lines

I'm happy to work with you.
There are already many maintainers on each feature who is responsible for it (even it is a piece of code), and scripts/get_maintainers.pl gives you who are.

Sorry, that would indeed have told me that you were responsible for include/linux/kprobes.h.

Quoted 4 lines
Quoted 3 lines

Srikar, Oleg, I think it's a good time to merge such arch_specific mechanism of uprobes and kprobes. Would you think we can do similar thing on x86 too?

I welcome input from people who have experience in this area. I have no desire to complicate efforts that might be attempted on other architectures, but the existing code was unique to ARM and the goal here was just to share it on ARM in implementing the currently non-existant ARM uprobes feature. It seems to me the leakage of these changes into generic kprobes code is exceedingly small, and unlikely to be a hinderance to any future work (if any is even needed or planned) supporting uprobes or kprobes on other architectures. In the meantime we have ARM users who have been asking for uprobes support for a while and this is, IMHO, a fairly clean approach to providing it.

-dl

#52 lore raw

Re: Re: [PATCH v2 10/13] kprobes: Remove uneeded kernel dependency on struct arch_specific_insn

Quoted 18 lines · David Long
Quoted 17 lines · Jon Medhurst (Tixy)
Quoted 16 lines · Masami Hiramatsu
Quoted 15 lines · Jon Medhurst (Tixy)
Quoted 4 lines · David Long

From: "David A. Long" <[email protected]>

Instead of depending on include/asm/kprobes.h to provide a dummy definition for struct arch_specific_insn, do so in include/linux/kprobes.h.

That change description doesn't quite seem to quite make sense to me.

Anyway, what we're trying to do with this patch is to allow us to use arch_specific_insn for purposes additional to implementing kprobes. This patch enables that but I'm wary that the kprobes code assumes that ainsn is a struct arch_specific_insn, e.g. in linux/kernel/kprobes.c we have:

	memcpy(&p->ainsn, &ap->ainsn, sizeof(struct arch_specific_insn));
Quoted 7 lines
Quoted 5 lines
Quoted 5 lines
Quoted 4 lines

Now, that code isn't compiled when kprobes isn't configured, but it seams to me to be safer if that was also changed to

	memcpy(&p->ainsn, &ap->ainsn, sizeof(p->ainsn));

That does look like an important improvement.

Agreed.

Quoted 10 lines
Quoted 6 lines
Quoted 2 lines

This kind of cleanup looks good for me, but I don't agree to change the type of the member (removing is OK) by Kconfig.

Wouldn't that still require an #ifdef CONFIG_KPROBES around ainsn? Admittedly a less ugly one than one to change its type to an int.

It is also possible to make the include of asm/kprobes.h unconditional, although that might only cause the #ifdef to appear in more than one include file.

Good point!

Quoted 4 lines
Quoted 3 lines
Quoted 2 lines

Srikar, Oleg, I think it's a good time to merge such arch_specific mechanism of uprobes and kprobes. Would you think we can do similar thing on x86 too?

Quoted 10 lines

I welcome input from people who have experience in this area. I have no desire to complicate efforts that might be attempted on other architectures, but the existing code was unique to ARM and the goal here was just to share it on ARM in implementing the currently non-existant ARM uprobes feature. It seems to me the leakage of these changes into generic kprobes code is exceedingly small, and unlikely to be a hinderance to any future work (if any is even needed or planned) supporting uprobes or kprobes on other architectures. In the meantime we have ARM users who have been asking for uprobes support for a while and this is, IMHO, a fairly clean approach to providing it.

I see. I'd just like to suggest you that your improvement on ONE arch can also be useful idea for the other archs. In that case, there would be better, more efficient way to do that.
Since we are on the same (or, next) track, we can learn many things each other. :)

Thank you,

Signature · 4 lines
Masami HIRAMATSU
IT Management Research Dept. Linux Technology Center
Hitachi, Ltd., Yokohama Research Laboratory
E-mail: masami.hiramatsu.pt at hitachi.com
author
#53 lore raw

Re: [PATCH v2 10/13] kprobes: Remove uneeded kernel dependency on struct arch_specific_insn

Quoted 7 lines · Masami Hiramatsu

I see. I'd just like to suggest you that your improvement on ONE arch can also be useful idea for the other archs. In that case, there would be better, more efficient way to do that.
Since we are on the same (or, next) track, we can learn many things each other. :)

Thank you,

I'd like to suggest there is still no reason not to take the current work (with suggested changes), which interested parties can then further adapt for supporting other/multiple architectures. There is virtually no new architecture-specific code that would have to be redone later. Additionally, it's not clear to me how useful instruction interpretation is to any other architecture. We are doing this only because 32-bit ARM does not support single-stepping.

-dl

#54 lore raw

Re: Re: Re: [PATCH v2 10/13] kprobes: Remove uneeded kernel dependency on struct arch_specific_insn

In reply to Jon Medhurst (Tixy)
Quoted 20 lines · Jon Medhurst (Tixy)
Quoted 19 lines · Masami Hiramatsu
Quoted 18 lines · Jon Medhurst (Tixy)
Quoted 4 lines · David Long

From: "David A. Long" <[email protected]>

Instead of depending on include/asm/kprobes.h to provide a dummy definition for struct arch_specific_insn, do so in include/linux/kprobes.h.

That change description doesn't quite seem to quite make sense to me.

Anyway, what we're trying to do with this patch is to allow us to use arch_specific_insn for purposes additional to implementing kprobes. This patch enables that but I'm wary that the kprobes code assumes that ainsn is a struct arch_specific_insn, e.g. in linux/kernel/kprobes.c we have:

	memcpy(&p->ainsn, &ap->ainsn, sizeof(struct arch_specific_insn));

Now, that code isn't compiled when kprobes isn't configured, but it seams to me to be safer if that was also changed to

Quoted 7 lines
Quoted 4 lines
Quoted 1 line
	memcpy(&p->ainsn, &ap->ainsn, sizeof(p->ainsn));

This kind of cleanup looks good for me, but I don't agree to change the type of the member (removing is OK) by Kconfig.

Wouldn't that still require an #ifdef CONFIG_KPROBES around ainsn? Admittedly a less ugly one than one to change its type to an int.

Yeah, that's the point.

Quoted 6 lines
Quoted 5 lines

If you want to
change the framework of kprobes and uprobes itself (unification), I'm appreciate to discuss with you and uprobes people, because it will involve all arch dependent code change, *NOT ONLY* the ARM issue.

Quoted 6 lines

Well, I don't think the goal wasn't unification as such. For kprobes on ARM we have to decode and simulate pretty much the entire instruction set(s) and the attempt to implement uprobes on ARM have tried to make use of as much of that as possible. The tricky bit has been as to where to try and draw the level of abstraction, and it seems this may well be leaking out of the arch specific arena.

I see, I've heard that from Sandeepa who are working on arm64 kprobes. His patch series now has generic interface of decoder/simulator.

Quoted 10 lines

Bit of background, Dave Long has been working on ARM uprobes based on Rabin Vincent's earlier work, and I, as author of a large part of the current ARM kprobes code, have been reviewing (not very satisfactorily I admit) the bits that impact that. One of my motivations has been to push the kprobes instruction decoding to be more generic, rather than special casing things to cope with uprobes. This is because I'm aware of the reoccurring theme on the ARM lists that it would be good to not have all the different methods of instruction decoding, for probes, debug and simulation, etc. (I'm sceptical that a one-size-fits-all is possible, but consolidation where practical is good).

Same as x86, we still have different code base of kprobes and uprobes Fortunately, x86 instruction decoder is separated, but single-stepping and other parts are not well shared.

Quoted 10 lines
Quoted 10 lines
Quoted 9 lines

However, I also wonder if we should instead leave arch_specific_insn as a kprobes specific structure and on ARM define it in terms of a new more generic 'struct probe_insn'? The drawback with that is that we'd probably end up with a struct just containing a single member which seems a bit redundant:

struct arch_specific_insn {
	struct probe_insn pinsn;
};
Quoted 8 lines
Quoted 2 lines

I also disagree it. If you have a plan to integrate uprobes and kprobes arch specific code, please share it with us.

There's not really a 'plan', just an attempt to reuse the instruction decoding code used by kprobes in the implementation of uprobes, i.e. the patch series [1] which this mail thread is in reply to.

[1] http://thread.gmane.org/gmane.linux.kernel/1579985

OK, and I think similar method we can use on x86 too. :) In that case, we may be able to simplify the arch_specific_insn.

Thank you,

Signature · 4 lines
Masami HIRAMATSU
IT Management Research Dept. Linux Technology Center
Hitachi, Ltd., Yokohama Research Laboratory
E-mail: masami.hiramatsu.pt at hitachi.com
#55 lore raw

Re: [PATCH v2 10/13] kprobes: Remove uneeded kernel dependency on struct arch_specific_insn

In reply to David Long
Quoted 4 lines · David Long

From: "David A. Long" <[email protected]>

Instead of depending on include/asm/kprobes.h to provide a dummy definition for struct arch_specific_insn, do so in include/linux/kprobes.h.

Nak, why do we need ugly #ifdef again there? And also, please cc to the original maintainers.

Thank you,

Quoted 33 lines
---
 include/linux/kprobes.h | 7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)
include/linux/kprobes.h +4 -3
index 925eaf2..4b5a74d 100644
@@ -52,9 +52,6 @@
 
 #else /* CONFIG_KPROBES */
 typedef int kprobe_opcode_t;
-struct arch_specific_insn {
-	int dummy;
-};
 #endif /* CONFIG_KPROBES */
 
 struct kprobe;
@@ -110,7 +107,11 @@ struct kprobe {
 	kprobe_opcode_t opcode;
 
 	/* copy of the original instruction */
+#ifdef CONFIG_KPROBES
 	struct arch_specific_insn ainsn;
+#else
+	int ainsn;
+#endif
 
 	/*
 	 * Indicates various status flags.
Signature · 4 lines
Masami HIRAMATSU
IT Management Research Dept. Linux Technology Center
Hitachi, Ltd., Yokohama Research Laboratory
E-mail: masami.hiramatsu.pt at hitachi.com
author
#56 lore raw

PATCH v2 11/13 ARM: Finish renaming ARM kprobes APIs for sharing with uprobes

In reply to David Long

From: "David A. Long" <[email protected]>

Use the prefix "probes_" for APIs and definitions to be shared between kprobes and uprobes. Pass additional information into the lower-level decoding functions to avoid having to duplicate architecture-specfific data structures. Make susbsystem-specific APIs static (non-global) again. Make a couple of utility functions (probes_simulate_nop and probes_emulate_none) sharable and in a common file. Keep the current "action" tables specific to kprobes, as this is where uprobes functionality will be connected up to the instruction interpreter.

---
 arch/arm/include/asm/kprobes.h   |   4 +-
 arch/arm/include/asm/probes.h    |  25 ++--
 arch/arm/kernel/kprobes-arm.c    |  99 ++++++++------
 arch/arm/kernel/kprobes-common.c |  48 ++++---
 arch/arm/kernel/kprobes-test.c   |  10 +-
 arch/arm/kernel/kprobes-thumb.c  | 289 +++++++++++++++++++++------------------
 arch/arm/kernel/kprobes.c        |  20 +--
 arch/arm/kernel/kprobes.h        |  31 ++---
 arch/arm/kernel/probes-arm.c     |  58 ++++----
 arch/arm/kernel/probes-arm.h     |  33 ++---
 arch/arm/kernel/probes-thumb.c   |  44 +++---
 arch/arm/kernel/probes-thumb.h   |  57 ++------
 arch/arm/kernel/probes.c         |  65 +++++----
 arch/arm/kernel/probes.h         |  49 +++----
 14 files changed, 430 insertions(+), 402 deletions(-)
arch/arm/include/asm/kprobes.h +2 -2
index 30fc11b..73669b8 100644
@@ -19,6 +19,7 @@
 #include <linux/types.h>
 #include <linux/ptrace.h>
 #include <linux/percpu.h>
+#include <asm/probes.h>
 
 #define __ARCH_WANT_KPROBES_INSN_SLOT
 #define MAX_INSN_SIZE			2
@@ -28,9 +29,8 @@
 #define kretprobe_blacklist_size	0
 
 typedef u32 kprobe_opcode_t;
-struct kprobe;
-#include <asm/probes.h>
 
+struct kprobe;
 struct prev_kprobe {
 	struct kprobe *kp;
 	unsigned int status;
arch/arm/include/asm/probes.h +15 -10
index edf5abe..00a93a2 100644
@@ -1,20 +1,25 @@
 #ifndef _ASM_PROBES_H
 #define _ASM_PROBES_H
 
-struct kprobe;
+typedef u32 probes_opcode_t;
 
-typedef void (kprobe_insn_handler_t)(struct kprobe *, struct pt_regs *);
-typedef unsigned long (kprobe_check_cc)(unsigned long);
-typedef void (kprobe_insn_singlestep_t)(struct kprobe *, struct pt_regs *);
-typedef void (kprobe_insn_fn_t)(void);
+struct arch_specific_insn;
+typedef void (probes_insn_handler_t)(probes_opcode_t, probes_opcode_t *,
+				     struct arch_specific_insn *,
+				     struct pt_regs *);
+typedef unsigned long (probes_check_cc)(unsigned long);
+typedef void (probes_insn_singlestep_t)(probes_opcode_t, probes_opcode_t *,
+					struct arch_specific_insn *,
+					struct pt_regs *);
+typedef void (probes_insn_fn_t)(void);
 
 /* Architecture specific copy of original instruction. */
 struct arch_specific_insn {
-	kprobe_opcode_t			*insn;
-	kprobe_insn_handler_t		*insn_handler;
-	kprobe_check_cc			*insn_check_cc;
-	kprobe_insn_singlestep_t	*insn_singlestep;
-	kprobe_insn_fn_t		*insn_fn;
+	probes_opcode_t			*insn;
+	probes_insn_handler_t		*insn_handler;
+	probes_check_cc			*insn_check_cc;
+	probes_insn_singlestep_t	*insn_singlestep;
+	probes_insn_fn_t		*insn_fn;
 };
 
 #endif
arch/arm/kernel/kprobes-arm.c +54 -45

223 lines not on this page · raw message

arch/arm/kernel/kprobes-common.c +30 -18

99 lines not on this page · raw message

arch/arm/kernel/kprobes-test.c +6 -4
index 2144df1..710bae8 100644
@@ -204,8 +204,10 @@
 
 #include <asm/opcodes.h>
 
-#include "kprobes.h"
 #include "probes.h"
+#include "probes-arm.h"
+#include "probes-thumb.h"
+#include "kprobes.h"
 #include "kprobes-test.h"
 
 
@@ -1613,7 +1615,7 @@ static int __init run_all_tests(void)
 		goto out;
 
 	pr_info("ARM instruction simulation\n");
-	ret = run_test_cases(kprobe_arm_test_cases, kprobe_decode_arm_table);
+	ret = run_test_cases(kprobe_arm_test_cases, probes_decode_arm_table);
 	if (ret)
 		goto out;
 
@@ -1636,13 +1638,13 @@ static int __init run_all_tests(void)
 
 	pr_info("16-bit Thumb instruction simulation\n");
 	ret = run_test_cases(kprobe_thumb16_test_cases,
-				kprobe_decode_thumb16_table);
+				probes_decode_thumb16_table);
 	if (ret)
 		goto out;
 
 	pr_info("32-bit Thumb instruction simulation\n");
 	ret = run_test_cases(kprobe_thumb32_test_cases,
-				kprobe_decode_thumb32_table);
+				probes_decode_thumb32_table);
 	if (ret)
 		goto out;
 #endif
arch/arm/kernel/kprobes-thumb.c +159 -130

626 lines not on this page · raw message

arch/arm/kernel/kprobes.c +11 -9
index c72d552..30c74c0 100644
@@ -28,8 +28,10 @@
 #include <asm/traps.h>
 #include <asm/cacheflush.h>
 
-#include "kprobes.h"
 #include "probes.h"
+#include "probes-arm.h"
+#include "probes-thumb.h"
+#include "kprobes.h"
 #include "patch.h"
 
 #define MIN_STACK_SIZE(addr) 				\
@@ -68,10 +70,10 @@ int __kprobes arch_prepare_kprobe(struct kprobe *p)
 	if (is_wide_instruction(insn)) {
 		insn <<= 16;
 		insn |= ((u16 *)addr)[1];
-		decode_insn = thumb32_kprobe_decode_insn;
+		decode_insn = thumb32_probes_decode_insn;
 		actions = kprobes_t32_actions;
 	} else {
-		decode_insn = thumb16_kprobe_decode_insn;
+		decode_insn = thumb16_probes_decode_insn;
 		actions = kprobes_t16_actions;
 	}
 #else /* !CONFIG_THUMB2_KERNEL */
@@ -79,15 +81,15 @@ int __kprobes arch_prepare_kprobe(struct kprobe *p)
 	if (addr & 0x3)
 		return -EINVAL;
 	insn = *p->addr;
-	decode_insn = arm_kprobe_decode_insn;
+	decode_insn = arm_probes_decode_insn;
 	actions = kprobes_arm_actions;
 #endif
 
 	p->opcode = insn;
 	p->ainsn.insn = tmp_insn;
 
-	switch ((*decode_insn)
-		(insn, &p->ainsn, (struct decode_header *) actions)) {
+	switch ((*decode_insn)(insn, &p->ainsn,
+			       false, actions)) {
 	case INSN_REJECTED:	/* not supported */
 		return -EINVAL;
 
@@ -99,7 +101,7 @@ int __kprobes arch_prepare_kprobe(struct kprobe *p)
 			p->ainsn.insn[is] = tmp_insn[is];
 		flush_insns(p->ainsn.insn,
 				sizeof(p->ainsn.insn[0]) * MAX_INSN_SIZE);
-		p->ainsn.insn_fn = (kprobe_insn_fn_t *)
+		p->ainsn.insn_fn = (probes_insn_fn_t *)
 					((uintptr_t)p->ainsn.insn | thumb);
 		break;
 
@@ -204,7 +206,7 @@ singlestep_skip(struct kprobe *p, struct pt_regs *regs)
 static inline void __kprobes
 singlestep(struct kprobe *p, struct pt_regs *regs, struct kprobe_ctlblk *kcb)
 {
-	p->ainsn.insn_singlestep(p, regs);
+	p->ainsn.insn_singlestep(p->opcode, p->addr, &p->ainsn, regs);
 }
 
 /*
@@ -614,7 +616,7 @@ static struct undef_hook kprobes_arm_break_hook = {
 
 int __init arch_init_kprobes()
 {
-	arm_kprobe_decode_init();
+	arm_probes_decode_init();
 #ifdef CONFIG_THUMB2_KERNEL
 	register_undef_hook(&kprobes_thumb16_break_hook);
 	register_undef_hook(&kprobes_thumb32_break_hook);
arch/arm/kernel/kprobes.h +10 -21
index 77e4374..c13f3b1 100644
@@ -27,36 +27,25 @@
 #define KPROBE_THUMB16_BREAKPOINT_INSTRUCTION	0xde18
 #define KPROBE_THUMB32_BREAKPOINT_INSTRUCTION	0xf7f0a018
 
-struct decode_header;
+enum probes_insn __kprobes
+kprobe_decode_ldmstm(kprobe_opcode_t insn, struct arch_specific_insn *asi,
+		const struct decode_header *h);
 
-enum kprobe_insn {
-	INSN_REJECTED,
-	INSN_GOOD,
-	INSN_GOOD_NO_SLOT
-};
-
-typedef enum kprobe_insn (kprobe_decode_insn_t)(kprobe_opcode_t,
-		struct arch_specific_insn *,
-		const struct decode_header *actions);
+typedef enum probes_insn (kprobe_decode_insn_t)(kprobe_opcode_t,
+			struct arch_specific_insn *,
+			bool,
+			const union decode_item *);
 
 #ifdef CONFIG_THUMB2_KERNEL
 
-enum kprobe_insn thumb16_kprobe_decode_insn(kprobe_opcode_t,
-						struct arch_specific_insn *,
-						const struct decode_header *);
-enum kprobe_insn thumb32_kprobe_decode_insn(kprobe_opcode_t,
-						struct arch_specific_insn *,
-						const struct decode_header *);
+extern const union decode_item kprobes_t32_actions[];
+extern const union decode_item kprobes_t16_actions[];
 
 #else /* !CONFIG_THUMB2_KERNEL */
 
-enum kprobe_insn arm_kprobe_decode_insn(kprobe_opcode_t,
-					struct arch_specific_insn *,
-					const struct decode_header *);
+extern const union decode_item kprobes_arm_actions[];
 
 #endif
 
-void __init arm_kprobe_decode_init(void);
-
 
 #endif /* _ARM_KERNEL_KPROBES_H */
arch/arm/kernel/probes-arm.c +34 -24

124 lines not on this page · raw message

arch/arm/kernel/probes-arm.h +15 -18
index f8e0f7a..55fe7da 100644
@@ -52,24 +52,21 @@ enum probes_arm_action {
 	PROBES_LDMSTM
 };
 
-void __kprobes simulate_bbl(struct kprobe *p, struct pt_regs *regs);
-void __kprobes simulate_blx1(struct kprobe *p, struct pt_regs *regs);
-void __kprobes simulate_blx2bx(struct kprobe *p, struct pt_regs *regs);
-void __kprobes simulate_mrs(struct kprobe *p, struct pt_regs *regs);
-void __kprobes simulate_mov_ipsp(struct kprobe *p, struct pt_regs *regs);
+void __kprobes simulate_bbl(probes_opcode_t opcode, probes_opcode_t *addr,
+	struct arch_specific_insn *asi, struct pt_regs *regs);
+void __kprobes simulate_blx1(probes_opcode_t opcode, probes_opcode_t *addr,
+	struct arch_specific_insn *asi, struct pt_regs *regs);
+void __kprobes simulate_blx2bx(probes_opcode_t opcode, probes_opcode_t *addr,
+	struct arch_specific_insn *asi, struct pt_regs *regs);
+void __kprobes simulate_mrs(probes_opcode_t opcode, probes_opcode_t *addr,
+	struct arch_specific_insn *asi, struct pt_regs *regs);
+void __kprobes simulate_mov_ipsp(probes_opcode_t opcode, probes_opcode_t *addr,
+	struct arch_specific_insn *asi, struct pt_regs *regs);
 
-void __kprobes emulate_ldrdstrd(struct kprobe *p, struct pt_regs *regs);
-void __kprobes emulate_ldr(struct kprobe *p, struct pt_regs *regs);
-void __kprobes emulate_str(struct kprobe *p, struct pt_regs *regs);
-void __kprobes emulate_rd12rn16rm0rs8_rwflags(struct kprobe *p,
-	struct pt_regs *regs);
-void __kprobes emulate_rd12rn16rm0_rwflags_nopc(struct kprobe *p,
-	struct pt_regs *regs);
-void __kprobes emulate_rd16rn12rm0rs8_rwflags_nopc(struct kprobe *p,
-	struct pt_regs *regs);
-void __kprobes emulate_rd12rm0_noflags_nopc(struct kprobe *p,
-	struct pt_regs *regs);
-void __kprobes emulate_rdlo12rdhi16rn0rm8_rwflags_nopc(struct kprobe *p,
-	struct pt_regs *regs);
+extern const union decode_item probes_decode_arm_table[];
+
+enum probes_insn arm_probes_decode_insn(probes_opcode_t,
+		struct arch_specific_insn *, bool usermode,
+		const union decode_item *actions);
 
 #endif
arch/arm/kernel/probes-thumb.c +23 -21

99 lines not on this page · raw message

arch/arm/kernel/probes-thumb.h +8 -49

63 lines not on this page · raw message

arch/arm/kernel/probes.c +41 -24

157 lines not on this page · raw message

arch/arm/kernel/probes.h +22 -27

83 lines not on this page · raw message

Signature · 1 line
1.8.1.2
#57 lore raw

Re: [PATCH v2 11/13] ARM: Finish renaming ARM kprobes APIs for sharing with uprobes

Quoted 9 lines · David Long

From: "David A. Long" <[email protected]>

Use the prefix "probes_" for APIs and definitions to be shared between kprobes and uprobes. Pass additional information into the lower-level decoding functions to avoid having to duplicate architecture-specfific data structures. Make susbsystem-specific APIs static (non-global) again. Make a couple of utility functions (probes_simulate_nop and probes_emulate_none) sharable and in a common file. Keep the current "action" tables specific to kprobes, as this is where uprobes functionality will be connected up to the instruction interpreter.

That's a rather long list of things this patch is reorganising and makes it very difficult to review. For those class of changes which are independent of each other, can you do them as separate patches?

Particularly, the change from using the function arguments

struct kprobe *p'

to
probes_opcode_t opcode, probes_opcode_t *addr, struct arch_specific_insn *asi

should be in it's own separate patch as that is spread out across multiple files and functions.

I have a few general comments on this patch...

1. I don't think we actually need to pass 'probes_opcode_t *addr' to the simulation functions, this will be the same as regs->ARM_pc (except that for thumb code 'addr' has bit0 set, which we don't actually care about). So drop the new 'addr' argument, and we can also delete the definition of thumb_probe_pc() and replace its use with 'regs->ARM_pc + 4'

2. Now we pass 'probes_opcode_t opcode' to simulation functions a lot of them end up doing the redundant...

kprobe_opcode_t insn = opcode

I would suggest deleting all of these assignments and either rename all use of 'insn' to 'opcode' or just changing the function argument name to 'insn'.

3. I've a comment about the change to probes_decode_insn, I've snipped the rest of the patch...

[...]

arch/arm/kernel/probes.c static const int decode_struct_sizes[NUM_DECODE_TYPES] = { · line 394

Quoted 7 lines

int __kprobes

arch/arm/kernel/probes.c +2 -2
-kprobe_decode_insn(kprobe_opcode_t insn, struct arch_specific_insn *asi,
+probes_decode_insn(probes_opcode_t insn, struct arch_specific_insn *asi,
 				const union decode_item *table, bool thumb,
-				const union decode_item *actions)
+				bool usermode, const union decode_item *actions)
 {

The new argument 'usermode' is named for the use you are using it for, i.e. uprobes, I think the code is more intuitive if it was called something like 'no_emulate', because what the argument does is force all emulate operations to become 'custom'.

Perhaps to avoid double negatives like !no_emulate, the logic of the argument could be inverted and it called 'use_emulation' or something.

Quoted 43 lines
arch/arm/kernel/probes.c +12 -5
-	const struct decode_header *h = (struct decode_header *)table;
-	const struct decode_header *next;
+	struct decode_header *h = (struct decode_header *)table;
+	struct decode_header *next;
 	bool matched = false;
 
-	insn = prepare_emulated_insn(insn, asi, thumb);
+	if (!usermode)
+		insn = prepare_emulated_insn(insn, asi, thumb);
 
 	for (;; h = next) {
 		enum decode_type type = h->type_regs.bits & DECODE_TYPE_MASK;
@@ -401,7 +412,7 @@ kprobe_decode_insn(kprobe_opcode_t insn, struct arch_specific_insn *asi,
 		if (!matched && (insn & h->mask.bits) != h->value.bits)
 			continue;
 
-		if (!decode_regs(&insn, regs))
+		if (!decode_regs(&insn, regs, !usermode))
 			return INSN_REJECTED;
 
 		switch (type) {
@@ -414,7 +425,8 @@ kprobe_decode_insn(kprobe_opcode_t insn, struct arch_specific_insn *asi,
 
 		case DECODE_TYPE_CUSTOM: {
 			struct decode_custom *d = (struct decode_custom *)h;
-			return actions[d->decoder.bits].decoder(insn, asi, h);
+			return actions[d->decoder.bits].decoder(insn,
+					asi, h);
 		}
 
 		case DECODE_TYPE_SIMULATE: {
@@ -425,6 +437,11 @@ kprobe_decode_insn(kprobe_opcode_t insn, struct arch_specific_insn *asi,
 
 		case DECODE_TYPE_EMULATE: {
 			struct decode_emulate *d = (struct decode_emulate *)h;
+
+			if (usermode)
+				return actions[d->handler.bits].decoder(insn,
+								asi, h);
+
 			asi->insn_handler = actions[d->handler.bits].handler;
 			set_emulated_insn(insn, asi, thumb);
 			return INSN_GOOD;

[...]

Signature · 1 line
Tixy
author
#58 lore raw

Re: [PATCH v2 11/13] ARM: Finish renaming ARM kprobes APIs for sharing with uprobes

Quoted 17 lines · Jon Medhurst (Tixy)
Quoted 9 lines · David Long

From: "David A. Long" <[email protected]>

Use the prefix "probes_" for APIs and definitions to be shared between kprobes and uprobes. Pass additional information into the lower-level decoding functions to avoid having to duplicate architecture-specfific data structures. Make susbsystem-specific APIs static (non-global) again. Make a couple of utility functions (probes_simulate_nop and probes_emulate_none) sharable and in a common file. Keep the current "action" tables specific to kprobes, as this is where uprobes functionality will be connected up to the instruction interpreter.

That's a rather long list of things this patch is reorganising and makes it very difficult to review. For those class of changes which are independent of each other, can you do them as separate patches?

Particularly, the change from using the function arguments

Quoted 7 lines

struct kprobe *p

to
probes_opcode_t opcode, probes_opcode_t *addr, struct arch_specific_insn *asi

should be in it's own separate patch as that is spread out across multiple files and functions.

OK, I will break this down further into separate patches.

Quoted 7 lines

I have a few general comments on this patch...

1. I don't think we actually need to pass 'probes_opcode_t *addr' to the simulation functions, this will be the same as regs->ARM_pc (except that for thumb code 'addr' has bit0 set, which we don't actually care about). So drop the new 'addr' argument, and we can also delete the definition of thumb_probe_pc() and replace its use with 'regs->ARM_pc + 4'

I had some trouble getting that to work before, but I'll try again.

Quoted 8 lines

2. Now we pass 'probes_opcode_t opcode' to simulation functions a lot of them end up doing the redundant...

kprobe_opcode_t insn = opcode

I would suggest deleting all of these assignments and either rename all use of 'insn' to 'opcode' or just changing the function argument name to 'insn'.

OK, I'll have a go at that.

Quoted 17 lines

3. I've a comment about the change to probes_decode_insn, I've snipped the rest of the patch...

[...]

arch/arm/kernel/probes.c static const int decode_struct_sizes[NUM_DECODE_TYPES] = { · line 394

Quoted 7 lines

int __kprobes

arch/arm/kernel/probes.c +2 -2
-kprobe_decode_insn(kprobe_opcode_t insn, struct arch_specific_insn *asi,
+probes_decode_insn(probes_opcode_t insn, struct arch_specific_insn *asi,
  				const union decode_item *table, bool thumb,
-				const union decode_item *actions)
+				bool usermode, const union decode_item *actions)
  {

The new argument 'usermode' is named for the use you are using it for, i.e. uprobes, I think the code is more intuitive if it was called something like 'no_emulate', because what the argument does is force all emulate operations to become 'custom'.

Quoted 2 lines

Perhaps to avoid double negatives like !no_emulate, the logic of the argument could be inverted and it called 'use_emulation' or something.

Or maybe just "emulate".

Quoted 46 lines
Quoted 43 lines
arch/arm/kernel/probes.c +12 -5
-	const struct decode_header *h = (struct decode_header *)table;
-	const struct decode_header *next;
+	struct decode_header *h = (struct decode_header *)table;
+	struct decode_header *next;
  	bool matched = false;
 
-	insn = prepare_emulated_insn(insn, asi, thumb);
+	if (!usermode)
+		insn = prepare_emulated_insn(insn, asi, thumb);
 
  	for (;; h = next) {
  		enum decode_type type = h->type_regs.bits & DECODE_TYPE_MASK;
@@ -401,7 +412,7 @@ kprobe_decode_insn(kprobe_opcode_t insn, struct arch_specific_insn *asi,
  		if (!matched && (insn & h->mask.bits) != h->value.bits)
  			continue;
 
-		if (!decode_regs(&insn, regs))
+		if (!decode_regs(&insn, regs, !usermode))
  			return INSN_REJECTED;
 
  		switch (type) {
@@ -414,7 +425,8 @@ kprobe_decode_insn(kprobe_opcode_t insn, struct arch_specific_insn *asi,
 
  		case DECODE_TYPE_CUSTOM: {
  			struct decode_custom *d = (struct decode_custom *)h;
-			return actions[d->decoder.bits].decoder(insn, asi, h);
+			return actions[d->decoder.bits].decoder(insn,
+					asi, h);
  		}
 
  		case DECODE_TYPE_SIMULATE: {
@@ -425,6 +437,11 @@ kprobe_decode_insn(kprobe_opcode_t insn, struct arch_specific_insn *asi,
 
  		case DECODE_TYPE_EMULATE: {
  			struct decode_emulate *d = (struct decode_emulate *)h;
+
+			if (usermode)
+				return actions[d->handler.bits].decoder(insn,
+								asi, h);
+
  			asi->insn_handler = actions[d->handler.bits].handler;
  			set_emulated_insn(insn, asi, thumb);
  			return INSN_GOOD;

[...]

author
#59 lore raw

PATCH v2 12/13 ARM: add uprobes support

In reply to David Long

From: "David A. Long" <[email protected]>

Using Rabin Vincent's ARM uprobes patches as a base, enable uprobes support on ARM.

Caveats:

- Thumb is not supported
- XOL abort/trap handling is not implemented

---
 arch/arm/Kconfig                   |   4 +
 arch/arm/include/asm/ptrace.h      |   6 +
 arch/arm/include/asm/thread_info.h |   5 +-
 arch/arm/include/asm/uprobes.h     |  34 ++++++
 arch/arm/kernel/Makefile           |   1 +
 arch/arm/kernel/kprobes-common.c   |   2 +-
 arch/arm/kernel/kprobes.h          |   2 +-
 arch/arm/kernel/signal.c           |   4 +
 arch/arm/kernel/uprobes-arm.c      | 220 +++++++++++++++++++++++++++++++++++++
 arch/arm/kernel/uprobes.c          | 201 +++++++++++++++++++++++++++++++++
 arch/arm/kernel/uprobes.h          |  25 +++++
 arch/powerpc/include/asm/uprobes.h |   1 -
 include/linux/uprobes.h            |   3 +
 13 files changed, 504 insertions(+), 4 deletions(-)
 create mode 100644 arch/arm/include/asm/uprobes.h
 create mode 100644 arch/arm/kernel/uprobes-arm.c
 create mode 100644 arch/arm/kernel/uprobes.c
 create mode 100644 arch/arm/kernel/uprobes.h
arch/arm/Kconfig +4 -0
index 1ad6fb6..5b2cc2b 100644
@@ -198,6 +198,10 @@ config ZONE_DMA
 config NEED_DMA_MAP_STATE
        def_bool y
 
+config ARCH_SUPPORTS_UPROBES
+	depends on KPROBES
+	def_bool y
+
 config ARCH_HAS_DMA_SET_COHERENT_MASK
 	bool
 
arch/arm/include/asm/ptrace.h +6 -0
index 04c99f3..ee688b0a 100644
@@ -80,6 +80,12 @@ static inline long regs_return_value(struct pt_regs *regs)
 
 #define instruction_pointer(regs)	(regs)->ARM_pc
 
+static inline void instruction_pointer_set(struct pt_regs *regs,
+					   unsigned long val)
+{
+	instruction_pointer(regs) = val;
+}
+
 #ifdef CONFIG_SMP
 extern unsigned long profile_pc(struct pt_regs *regs);
 #else
arch/arm/include/asm/thread_info.h +4 -1
index df5e13d..2aa12e8 100644
@@ -159,6 +159,7 @@ extern int vfp_restore_user_hwstate(struct user_vfp __user *,
 #define TIF_SIGPENDING		0
 #define TIF_NEED_RESCHED	1
 #define TIF_NOTIFY_RESUME	2	/* callback before returning to user */
+#define TIF_UPROBE		7
 #define TIF_SYSCALL_TRACE	8
 #define TIF_SYSCALL_AUDIT	9
 #define TIF_SYSCALL_TRACEPOINT	10
@@ -171,6 +172,7 @@ extern int vfp_restore_user_hwstate(struct user_vfp __user *,
 #define _TIF_SIGPENDING		(1 << TIF_SIGPENDING)
 #define _TIF_NEED_RESCHED	(1 << TIF_NEED_RESCHED)
 #define _TIF_NOTIFY_RESUME	(1 << TIF_NOTIFY_RESUME)
+#define _TIF_UPROBE		(1 << TIF_UPROBE)
 #define _TIF_SYSCALL_TRACE	(1 << TIF_SYSCALL_TRACE)
 #define _TIF_SYSCALL_AUDIT	(1 << TIF_SYSCALL_AUDIT)
 #define _TIF_SYSCALL_TRACEPOINT	(1 << TIF_SYSCALL_TRACEPOINT)
@@ -184,7 +186,8 @@ extern int vfp_restore_user_hwstate(struct user_vfp __user *,
 /*
  * Change these and you break ASM code in entry-common.S
  */
-#define _TIF_WORK_MASK		(_TIF_NEED_RESCHED | _TIF_SIGPENDING | _TIF_NOTIFY_RESUME)
+#define _TIF_WORK_MASK		(_TIF_NEED_RESCHED | _TIF_SIGPENDING | \
+				 _TIF_NOTIFY_RESUME | _TIF_UPROBE)
 
 #endif /* __KERNEL__ */
 #endif /* __ASM_ARM_THREAD_INFO_H */
arch/arm/include/asm/uprobes.h new file +34 -0
new file mode 100644
index 0000000..fa4b81e
@@ -0,0 +1,34 @@
+#ifndef _ASM_UPROBES_H
+#define _ASM_UPROBES_H
+
+#include <asm/probes.h>
+
+typedef u32 uprobe_opcode_t;
+
+#define MAX_UINSN_BYTES		4
+#define UPROBE_XOL_SLOT_BYTES	64
+
+#define UPROBE_SWBP_INSN	0x07f001f9
+#define UPROBE_SS_INSN		0x07f001fa
+#define UPROBE_SWBP_INSN_SIZE	4
+
+struct arch_uprobe_task {
+	u32 backup;
+};
+
+struct arch_uprobe {
+	u8 insn[MAX_UINSN_BYTES];
+	uprobe_opcode_t modinsn;
+	uprobe_opcode_t bpinsn;
+	bool simulate;
+	u32 pcreg;
+	void (*prehandler)(struct arch_uprobe *auprobe,
+			   struct arch_uprobe_task *autask,
+			   struct pt_regs *regs);
+	void (*posthandler)(struct arch_uprobe *auprobe,
+			    struct arch_uprobe_task *autask,
+			    struct pt_regs *regs);
+	struct arch_specific_insn asi;
+};
+
+#endif
arch/arm/kernel/Makefile +1 -0
index 2fe6139..f84eb26 100644
@@ -49,6 +49,7 @@ 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
arch/arm/kernel/kprobes-common.c +1 -1
index 1093f5a..948e025 100644
@@ -127,7 +127,7 @@ emulate_ldm_r3_15(probes_opcode_t opcode, probes_opcode_t *addr,
 
 enum probes_insn __kprobes
 kprobe_decode_ldmstm(kprobe_opcode_t insn, struct arch_specific_insn *asi,
-		const struct decode_header *h)
+		struct decode_header *h)
 {
 	probes_insn_handler_t *handler = 0;
 	unsigned reglist = insn & 0xffff;
arch/arm/kernel/kprobes.h +1 -1
index c13f3b1..0c7a071 100644
@@ -29,7 +29,7 @@
 
 enum probes_insn __kprobes
 kprobe_decode_ldmstm(kprobe_opcode_t insn, struct arch_specific_insn *asi,
-		const struct decode_header *h);
+		struct decode_header *h);
 
 typedef enum probes_insn (kprobe_decode_insn_t)(kprobe_opcode_t,
 			struct arch_specific_insn *,
arch/arm/kernel/signal.c +4 -0
index ab33042..5bc71c6 100644
@@ -13,6 +13,7 @@
 #include <linux/personality.h>
 #include <linux/uaccess.h>
 #include <linux/tracehook.h>
+#include <linux/uprobes.h>
 
 #include <asm/elf.h>
 #include <asm/cacheflush.h>
@@ -606,6 +607,9 @@ do_work_pending(struct pt_regs *regs, unsigned int thread_flags, int syscall)
 					return restart;
 				}
 				syscall = 0;
+			} else if (thread_flags & _TIF_UPROBE) {
+				clear_thread_flag(TIF_UPROBE);
+				uprobe_notify_resume(regs);
 			} else {
 				clear_thread_flag(TIF_NOTIFY_RESUME);
 				tracehook_notify_resume(regs);
arch/arm/kernel/uprobes-arm.c new file +220 -0

220 lines not on this page · raw message

arch/arm/kernel/uprobes.c new file +201 -0

201 lines not on this page · raw message

arch/arm/kernel/uprobes.h new file +25 -0
new file mode 100644
index 0000000..2850e793
@@ -0,0 +1,25 @@
+#ifndef __ARM_KERNEL_UPROBES_H
+#define __ARM_KERNEL_UPROBES_H
+
+enum probes_insn uprobe_decode_ldmstm(probes_opcode_t insn,
+				      struct arch_specific_insn *asi,
+				      struct decode_header *d);
+
+enum probes_insn decode_ldr(probes_opcode_t insn,
+			    struct arch_specific_insn *asi,
+			    struct decode_header *d);
+
+enum probes_insn
+decode_rd12rn16rm0rs8_rwflags(probes_opcode_t insn,
+			      struct arch_specific_insn *asi, struct decode_header *d);
+
+enum probes_insn
+decode_wb_pc(probes_opcode_t insn, struct arch_specific_insn *asi,
+	     struct decode_header *d, bool alu);
+
+enum probes_insn
+decode_pc_ro(probes_opcode_t insn, struct arch_specific_insn *asi, struct decode_header *d);
+
+extern const union decode_item uprobes_probes_actions[];
+
+#endif
arch/powerpc/include/asm/uprobes.h +0 -1
index 2301602..b532060 100644
@@ -51,5 +51,4 @@ extern int  arch_uprobe_post_xol(struct arch_uprobe *aup, struct pt_regs *regs);
 extern bool arch_uprobe_xol_was_trapped(struct task_struct *tsk);
 extern int  arch_uprobe_exception_notify(struct notifier_block *self, unsigned long val, void *data);
 extern void arch_uprobe_abort_xol(struct arch_uprobe *aup, struct pt_regs *regs);
-extern unsigned long arch_uretprobe_hijack_return_addr(unsigned long trampoline_vaddr, struct pt_regs *regs);
 #endif	/* _ASM_UPROBES_H */
include/linux/uprobes.h +3 -0
index 34375da..2d30820 100644
@@ -139,6 +139,9 @@ extern int __weak arch_uprobes_init(void);
 extern void __weak arch_uprobe_write_opcode(struct arch_uprobe *auprobe,
 					    void *vaddr,
 					    uprobe_opcode_t opcode);
+extern
+unsigned long arch_uretprobe_hijack_return_addr(unsigned long trampoline_vaddr,
+						       struct pt_regs *regs);
 #else /* !CONFIG_UPROBES */
 struct uprobes_state {
 };
Signature · 1 line
1.8.1.2
author
#60 lore raw

PATCH v2 13/13 ARM: Remove uprobes dependency on kprobes

In reply to David Long

From: "David A. Long" <[email protected]>

Now that arm uprobes support has been made separate from the arm kprobes code the Kconfig can be changed to reflect that.

---
 arch/arm/Kconfig | 1 -
 1 file changed, 1 deletion(-)
arch/arm/Kconfig +0 -1
index 5b2cc2b..5f4b5d1 100644
@@ -199,7 +199,6 @@ config NEED_DMA_MAP_STATE
        def_bool y
 
 config ARCH_SUPPORTS_UPROBES
-	depends on KPROBES
 	def_bool y
 
 config ARCH_HAS_DMA_SET_COHERENT_MASK
Signature · 1 line
1.8.1.2