Skip to contentSkip to conversation

uprobes: Change handle_swbp() to expose bp_vaddr to handler_chain()

PATCH In review Oleg Nesterov 4 participants 8 messages Dec 30, 2012 – Jan 8, 2013

Subsystems (4)
lore
Share & tools
lore

Oleg Nesterov

author maintainer
#0 lore raw

cover uprobes: Change handle_swbp() to expose bp_vaddr to handler_chain()

Hello.

Inspired by the recent uretrpobe RFC's. I think it simply makes no sense to call handler/skip/arch_uprobe_pre_xol with regs->ip pointing to the "random" address after the task hits bp.

Ananth, could you please confirm 2/2 can't break powerpc? It "obviously" shouldn't because uprobe_get_swbp_addr() is a "nop" on ppc, but still your ack is welcomed.

Oleg.

author maintainer
#1 lore raw

PATCH 1/2 uprobes/x86: Change __skip_sstep() to actually skip the whole insn

__skip_sstep() doesn't update regs->ip. Currently this is correct but only "by accident" and it doesn't skip the whole insn. Change it to advance ->ip by the length of the detected 0x66*0x90 sequence.

---
 arch/x86/kernel/uprobes.c |    5 ++++-
 1 files changed, 4 insertions(+), 1 deletions(-)
arch/x86/kernel/uprobes.c +4 -1
index c71025b..4e33a35 100644
@@ -680,8 +680,11 @@ static bool __skip_sstep(struct arch_uprobe *auprobe, struct pt_regs *regs)
 		if (auprobe->insn[i] == 0x66)
 			continue;
 
-		if (auprobe->insn[i] == 0x90)
+		if (auprobe->insn[i] == 0x90) {
+			regs->ip = uprobe_get_swbp_addr(regs);
+			regs->ip += i + 1;
 			return true;
+		}
 
 		break;
 	}
Signature · 1 line
1.5.5.1
#2 lore raw

Re: [PATCH 1/2] uprobes/x86: Change __skip_sstep() to actually skip the whole insn

Quoted 14 lines · Oleg Nesterov

__skip_sstep() doesn't update regs->ip. Currently this is correct but only "by accident" and it doesn't skip the whole insn. Change it to advance ->ip by the length of the detected 0x66*0x90 sequence.

---
 arch/x86/kernel/uprobes.c |    5 ++++-
 1 files changed, 4 insertions(+), 1 deletions(-)
arch/x86/kernel/uprobes.c +0 -0
index c71025b..4e33a35 100644

arch/x86/kernel/uprobes.c static bool __skip_sstep(struct arch_uprobe *auprobe, struct pt_regs *regs) · line 687

Quoted 9 lines
arch/x86/kernel/uprobes.c +4 -1
@@ -680,8 +680,11 @@ static bool __skip_sstep(struct arch_uprobe *auprobe, struct pt_regs *regs)
 		if (auprobe->insn[i] == 0x66)
 			continue;
 
-		if (auprobe->insn[i] == 0x90)
+		if (auprobe->insn[i] == 0x90) {
+			regs->ip = uprobe_get_swbp_addr(regs);
+			regs->ip += i + 1;
 			return true;
+		}

This change/routine is not obvious without the patch description, I
would appreciate some comment in the code. :) I believe it could help some random developer in future.
/ I leave it to your judgement though /

Anton.

Acked-by on v1 1/2
#3 lore raw

Re: [PATCH 1/2] uprobes/x86: Change __skip_sstep() to actually skip the whole insn

In reply to Oleg Nesterov
Quoted 5 lines · Oleg Nesterov

__skip_sstep() doesn't update regs->ip. Currently this is correct but only "by accident" and it doesn't skip the whole insn. Change it to advance ->ip by the length of the detected 0x66*0x90 sequence.

Quoted 24 lines
---
 arch/x86/kernel/uprobes.c |    5 ++++-
 1 files changed, 4 insertions(+), 1 deletions(-)
arch/x86/kernel/uprobes.c +4 -1
index c71025b..4e33a35 100644
@@ -680,8 +680,11 @@ static bool __skip_sstep(struct arch_uprobe *auprobe, struct pt_regs *regs)
 		if (auprobe->insn[i] == 0x66)
 			continue;
 
-		if (auprobe->insn[i] == 0x90)
+		if (auprobe->insn[i] == 0x90) {
+			regs->ip = uprobe_get_swbp_addr(regs);
+			regs->ip += i + 1;
 			return true;
+		}
 
 		break;
 	}
Signature · 1 line
1.5.5.1
Acked-by on v1 1/2
#4 lore raw

Re: [PATCH 1/2] uprobes/x86: Change __skip_sstep() to actually skip the whole insn

In reply to Oleg Nesterov
Quoted 5 lines · Oleg Nesterov

__skip_sstep() doesn't update regs->ip. Currently this is correct but only "by accident" and it doesn't skip the whole insn. Change it to advance ->ip by the length of the detected 0x66*0x90 sequence.

Quoted 24 lines
---
 arch/x86/kernel/uprobes.c |    5 ++++-
 1 files changed, 4 insertions(+), 1 deletions(-)
arch/x86/kernel/uprobes.c +4 -1
index c71025b..4e33a35 100644
@@ -680,8 +680,11 @@ static bool __skip_sstep(struct arch_uprobe *auprobe, struct pt_regs *regs)
 		if (auprobe->insn[i] == 0x66)
 			continue;
 
-		if (auprobe->insn[i] == 0x90)
+		if (auprobe->insn[i] == 0x90) {
+			regs->ip = uprobe_get_swbp_addr(regs);
+			regs->ip += i + 1;
 			return true;
+		}
 
 		break;
 	}
Signature · 1 line
1.5.5.1
author maintainer
#5 lore raw

PATCH 2/2 uprobes: Change handle_swbp() to expose bp_vaddr to handler_chain()

In reply to Oleg Nesterov

Change handle_swbp() to set regs->ip = bp_vaddr in advance, this is what consumer->handler() needs but uprobe_get_swbp_addr() is not exported.

This also simplifies the code and makes it more consistent across the supported architectures. handle_swbp() becomes the only caller of uprobe_get_swbp_addr().

---
 arch/x86/kernel/uprobes.c   |    1 -
 kernel/events/uprobes.c     |   15 +++++++--------
 kernel/trace/trace_uprobe.c |    4 ++--
 3 files changed, 9 insertions(+), 11 deletions(-)
arch/x86/kernel/uprobes.c +0 -1
index 4e33a35..0ba4cfb 100644
@@ -681,7 +681,6 @@ static bool __skip_sstep(struct arch_uprobe *auprobe, struct pt_regs *regs)
 			continue;
 
 		if (auprobe->insn[i] == 0x90) {
-			regs->ip = uprobe_get_swbp_addr(regs);
 			regs->ip += i + 1;
 			return true;
 		}
kernel/events/uprobes.c +7 -8
index 59b6e97..f883813 100644
@@ -1504,6 +1504,10 @@ 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
@@ -1511,14 +1515,14 @@ 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 restart;
+		goto out;
 
 	utask = current->utask;
 	if (!utask) {
 		utask = add_utask();
 		/* Cannot allocate; re-execute the instruction. */
 		if (!utask)
-			goto restart;
+			goto out;
 	}
 
 	handler_chain(uprobe, regs);
@@ -1531,12 +1535,7 @@ static void handle_swbp(struct pt_regs *regs)
 		return;
 	}
 
-restart:
-	/*
-	 * cannot singlestep; cannot skip instruction;
-	 * re-execute the instruction.
-	 */
-	instruction_pointer_set(regs, bp_vaddr);
+	/* can_skip_sstep() succeeded, or restart if can't singlestep */
 out:
 	put_uprobe(uprobe);
 }
kernel/trace/trace_uprobe.c +2 -2
index 38eee92..52caead 100644
@@ -488,7 +488,7 @@ static void uprobe_trace_func(struct trace_uprobe *tu, struct pt_regs *regs)
 		return;
 
 	entry = ring_buffer_event_data(event);
-	entry->ip = uprobe_get_swbp_addr(task_pt_regs(current));
+	entry->ip = instruction_pointer(task_pt_regs(current));
 	data = (u8 *)&entry[1];
 	for (i = 0; i < tu->nr_args; i++)
 		call_fetch(&tu->args[i].fetch, regs, data + tu->args[i].offset);
@@ -663,7 +663,7 @@ static void uprobe_perf_func(struct trace_uprobe *tu, struct pt_regs *regs)
 	if (!entry)
 		goto out;
 
-	entry->ip = uprobe_get_swbp_addr(task_pt_regs(current));
+	entry->ip = instruction_pointer(task_pt_regs(current));
 	data = (u8 *)&entry[1];
 	for (i = 0; i < tu->nr_args; i++)
 		call_fetch(&tu->args[i].fetch, regs, data + tu->args[i].offset);
Signature · 1 line
1.5.5.1
Acked-by on v1 2/2
#6 lore raw
Quoted 7 lines · Oleg Nesterov

Change handle_swbp() to set regs->ip = bp_vaddr in advance, this is what consumer->handler() needs but uprobe_get_swbp_addr() is not exported.

This also simplifies the code and makes it more consistent across the supported architectures. handle_swbp() becomes the only caller of uprobe_get_swbp_addr().

The arch agnostic bits look fine to me.

Quoted 1 line
Acked-by on v1 2/2
#7 lore raw
In reply to Oleg Nesterov
Quoted 9 lines · Oleg Nesterov

Change handle_swbp() to set regs->ip = bp_vaddr in advance, this is what consumer->handler() needs but uprobe_get_swbp_addr() is not exported.

This also simplifies the code and makes it more consistent across the supported architectures. handle_swbp() becomes the only caller of uprobe_get_swbp_addr().

Quoted 89 lines
---
 arch/x86/kernel/uprobes.c   |    1 -
 kernel/events/uprobes.c     |   15 +++++++--------
 kernel/trace/trace_uprobe.c |    4 ++--
 3 files changed, 9 insertions(+), 11 deletions(-)
arch/x86/kernel/uprobes.c +0 -1
index 4e33a35..0ba4cfb 100644
@@ -681,7 +681,6 @@ static bool __skip_sstep(struct arch_uprobe *auprobe, struct pt_regs *regs)
 			continue;
 
 		if (auprobe->insn[i] == 0x90) {
-			regs->ip = uprobe_get_swbp_addr(regs);
 			regs->ip += i + 1;
 			return true;
 		}
kernel/events/uprobes.c +7 -8
index 59b6e97..f883813 100644
@@ -1504,6 +1504,10 @@ 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
@@ -1511,14 +1515,14 @@ 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 restart;
+		goto out;
 
 	utask = current->utask;
 	if (!utask) {
 		utask = add_utask();
 		/* Cannot allocate; re-execute the instruction. */
 		if (!utask)
-			goto restart;
+			goto out;
 	}
 
 	handler_chain(uprobe, regs);
@@ -1531,12 +1535,7 @@ static void handle_swbp(struct pt_regs *regs)
 		return;
 	}
 
-restart:
-	/*
-	 * cannot singlestep; cannot skip instruction;
-	 * re-execute the instruction.
-	 */
-	instruction_pointer_set(regs, bp_vaddr);
+	/* can_skip_sstep() succeeded, or restart if can't singlestep */
 out:
 	put_uprobe(uprobe);
 }
kernel/trace/trace_uprobe.c +2 -2
index 38eee92..52caead 100644
@@ -488,7 +488,7 @@ static void uprobe_trace_func(struct trace_uprobe *tu, struct pt_regs *regs)
 		return;
 
 	entry = ring_buffer_event_data(event);
-	entry->ip = uprobe_get_swbp_addr(task_pt_regs(current));
+	entry->ip = instruction_pointer(task_pt_regs(current));
 	data = (u8 *)&entry[1];
 	for (i = 0; i < tu->nr_args; i++)
 		call_fetch(&tu->args[i].fetch, regs, data + tu->args[i].offset);
@@ -663,7 +663,7 @@ static void uprobe_perf_func(struct trace_uprobe *tu, struct pt_regs *regs)
 	if (!entry)
 		goto out;
 
-	entry->ip = uprobe_get_swbp_addr(task_pt_regs(current));
+	entry->ip = instruction_pointer(task_pt_regs(current));
 	data = (u8 *)&entry[1];
 	for (i = 0; i < tu->nr_args; i++)
 		call_fetch(&tu->args[i].fetch, regs, data + tu->args[i].offset);
Signature · 1 line
1.5.5.1