Skip to contentSkip to conversation

kprobes,x86: disable irq durinr optimized callback

PATCH Superseded Jiri Olsa 4 participants 10 messages Apr 26, 2011 – May 11, 2011 x86

lore
Share & tools
lore

Jiri Olsa

author
#0 lore raw

PATCH 1/1 kprobes,x86: disable irq durinr optimized callback

hi,

attached patch is disabling irqs during optimized callback, so we dont miss any in-irq kprobes as missed.

Also I think there's small window where current_kprobe variable could be touched in non-safe way, but I was not able to hit any issue.

I'm not sure wether this is a bug or if it was intentional to have irqs enabled during the pre_handler callback.

wbr,
jirka

---
Disabling irqs during optimized callback, so we dont miss any in-irq kprobes as missed.

Interrupts are also disabled during non-optimized kprobes callbacks.

---
 arch/x86/kernel/kprobes.c |    3 +++
 1 files changed, 3 insertions(+), 0 deletions(-)
arch/x86/kernel/kprobes.c +3 -0
index c969fd9..917cb31 100644
@@ -1183,11 +1183,13 @@ static void __kprobes optimized_callback(struct optimized_kprobe *op,
 					 struct pt_regs *regs)
 {
 	struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
+	unsigned long flags;
 
 	/* This is possible if op is under delayed unoptimizing */
 	if (kprobe_disabled(&op->kp))
 		return;
 
+	local_irq_save(flags);
 	preempt_disable();
 	if (kprobe_running()) {
 		kprobes_inc_nmissed_count(&op->kp);
@@ -1208,6 +1210,7 @@ static void __kprobes optimized_callback(struct optimized_kprobe *op,
 		__this_cpu_write(current_kprobe, NULL);
 	}
 	preempt_enable_no_resched();
+	local_irq_restore(flags);
 }
 
 static int __kprobes copy_optimized_instructions(u8 *dest, u8 *src)
Signature · 1 line
1.7.1
#1 lore raw
Quoted 11 lines · Jiri Olsa

hi,

attached patch is disabling irqs during optimized callback, so we dont miss any in-irq kprobes as missed.

Also I think there's small window where current_kprobe variable could be touched in non-safe way, but I was not able to hit any issue.

I'm not sure wether this is a bug or if it was intentional to have irqs enabled during the pre_handler callback.

That's not very convincing. Did you see if we actually did miss events. If that's the case then it is a bug. The conversion to optimizing should not cause events to be missed.

Quoted 21 lines

wbr,
jirka

---
Disabling irqs during optimized callback, so we dont miss any in-irq kprobes as missed.

Interrupts are also disabled during non-optimized kprobes callbacks.

---
 arch/x86/kernel/kprobes.c |    3 +++
 1 files changed, 3 insertions(+), 0 deletions(-)
arch/x86/kernel/kprobes.c +0 -0
index c969fd9..917cb31 100644
@@ -1183,11 +1183,13 @@ static void __kprobes optimized_callback(struct optimized_kprobe *op,
 					 struct pt_regs *regs)

arch/x86/kernel/kprobes.c static void __kprobes optimized_callback(struct optimized_kprobe *op, · line 1193

Quoted 10 lines
arch/x86/kernel/kprobes.c +2 -0
 {
 	struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
+	unsigned long flags;
 
 	/* This is possible if op is under delayed unoptimizing */
 	if (kprobe_disabled(&op->kp))
 		return;
 
+	local_irq_save(flags);
 	preempt_disable();

No reason to disable preemption if you disabled interrupts.

arch/x86/kernel/kprobes.c static void __kprobes optimized_callback(struct optimized_kprobe *op, · line 1212

Quoted 6 lines
arch/x86/kernel/kprobes.c +0 -0
 	if (kprobe_running()) {
 		kprobes_inc_nmissed_count(&op->kp);
@@ -1208,6 +1210,7 @@ static void __kprobes optimized_callback(struct optimized_kprobe *op,
 		__this_cpu_write(current_kprobe, NULL);
 	}
 	preempt_enable_no_resched();

Remove the preempt_enable_no_resched() as well.

BTW, what's up with all these preempt_enable_no_resched()'s laying around in the kprobe code? Looks to me that this can cause lots of missing wakeups (preemption leaks). Which would make this horrible for real-time.

-- Steve

Quoted 4 lines
arch/x86/kernel/kprobes.c +1 -0
+	local_irq_restore(flags);
 }
 
 static int __kprobes copy_optimized_instructions(u8 *dest, u8 *src)
author
#2 lore raw
Quoted 10 lines · Steven Rostedt
Quoted 9 lines · Jiri Olsa

hi,

attached patch is disabling irqs during optimized callback, so we dont miss any in-irq kprobes as missed.

Also I think there's small window where current_kprobe variable could be touched in non-safe way, but I was not able to hit any issue.

Quoted 6 lines
Quoted 2 lines

I'm not sure wether this is a bug or if it was intentional to have irqs enabled during the pre_handler callback.

That's not very convincing. Did you see if we actually did miss events. If that's the case then it is a bug. The conversion to optimizing should not cause events to be missed.

yep, running following:

# cd /debug/tracing/
# echo "p mutex_unlock" >> kprobe_events
# echo "p _raw_spin_lock" >> kprobe_events
# echo "p smp_apic_timer_interrupt" >> ./kprobe_events
# echo 1 > events/enable

makes the optimized kprobes to be missed. They are not missed in same testcase for non-optimized kprobes. I should have mentioned that, sry ;)

Quoted 27 lines
Quoted 27 lines

wbr,
jirka

---
Disabling irqs during optimized callback, so we dont miss any in-irq kprobes as missed.

Interrupts are also disabled during non-optimized kprobes callbacks.

---
 arch/x86/kernel/kprobes.c |    3 +++
 1 files changed, 3 insertions(+), 0 deletions(-)
arch/x86/kernel/kprobes.c +1 -0
index c969fd9..917cb31 100644
@@ -1183,11 +1183,13 @@ static void __kprobes optimized_callback(struct optimized_kprobe *op,
 					 struct pt_regs *regs)
 {
 	struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
+	unsigned long flags;
 
Quoted 8 lines

arch/x86/kernel/kprobes.c static void __kprobes optimized_callback(struct optimized_kprobe *op, · line 1193

Quoted 6 lines
arch/x86/kernel/kprobes.c +1 -0
 	/* This is possible if op is under delayed unoptimizing */
 	if (kprobe_disabled(&op->kp))
 		return;
 
+	local_irq_save(flags);
 	preempt_disable();

No reason to disable preemption if you disabled interrupts.

ops, missed that.. attaching new patch

thanks,
jirka

---
Disabling irqs during optimized callback, so we dont miss any in-irq kprobes as missed.

running following:

# cd /debug/tracing/
# echo "p mutex_unlock" >> kprobe_events
# echo "p _raw_spin_lock" >> kprobe_events
# echo "p smp_apic_timer_interrupt" >> ./kprobe_events
# echo 1 > events/enable

makes the optimized kprobes to be missed. None is missed if the kprobe optimatization is disabled.

---
 arch/x86/kernel/kprobes.c |    5 +++--
 1 files changed, 3 insertions(+), 2 deletions(-)
arch/x86/kernel/kprobes.c +3 -2
index c969fd9..f1a6244 100644
@@ -1183,12 +1183,13 @@ static void __kprobes optimized_callback(struct optimized_kprobe *op,
 					 struct pt_regs *regs)
 {
 	struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
+	unsigned long flags;
 
 	/* This is possible if op is under delayed unoptimizing */
 	if (kprobe_disabled(&op->kp))
 		return;
 
-	preempt_disable();
+	local_irq_save(flags);
 	if (kprobe_running()) {
 		kprobes_inc_nmissed_count(&op->kp);
 	} else {
@@ -1207,7 +1208,7 @@ static void __kprobes optimized_callback(struct optimized_kprobe *op,
 		opt_pre_handler(&op->kp, regs);
 		__this_cpu_write(current_kprobe, NULL);
 	}
-	preempt_enable_no_resched();
+	local_irq_restore(flags);
 }
 
 static int __kprobes copy_optimized_instructions(u8 *dest, u8 *src)
Signature · 1 line
1.7.1
#3 lore raw
Quoted 26 lines · Jiri Olsa
Quoted 10 lines · Steven Rostedt
Quoted 9 lines · Jiri Olsa

hi,

attached patch is disabling irqs during optimized callback, so we dont miss any in-irq kprobes as missed.

Also I think there's small window where current_kprobe variable could be touched in non-safe way, but I was not able to hit any issue.

Quoted 6 lines
Quoted 2 lines

I'm not sure wether this is a bug or if it was intentional to have irqs enabled during the pre_handler callback.

That's not very convincing. Did you see if we actually did miss events. If that's the case then it is a bug. The conversion to optimizing should not cause events to be missed.

yep, running following:

# cd /debug/tracing/
# echo "p mutex_unlock" >> kprobe_events
# echo "p _raw_spin_lock" >> kprobe_events
# echo "p smp_apic_timer_interrupt" >> ./kprobe_events
# echo 1 > events/enable
Quoted 3 lines

makes the optimized kprobes to be missed. They are not missed in same testcase for non-optimized kprobes. I should have mentioned that, sry ;)

Good catch! that's right! kprobes' int3 automatically disables irq, but optimized path doesn't. And that causes unexpected event loss.

Quoted 28 lines
Quoted 28 lines
Quoted 28 lines

wbr,
jirka

---
Disabling irqs during optimized callback, so we dont miss any in-irq kprobes as missed.

Interrupts are also disabled during non-optimized kprobes callbacks.

---
 arch/x86/kernel/kprobes.c |    3 +++
 1 files changed, 3 insertions(+), 0 deletions(-)
arch/x86/kernel/kprobes.c +1 -0
index c969fd9..917cb31 100644
@@ -1183,11 +1183,13 @@ static void __kprobes optimized_callback(struct optimized_kprobe *op,
 					 struct pt_regs *regs)
 {
 	struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
+	unsigned long flags;
 
Quoted 8 lines
Quoted 8 lines

arch/x86/kernel/kprobes.c static void __kprobes optimized_callback(struct optimized_kprobe *op,

Quoted 6 lines
arch/x86/kernel/kprobes.c +1 -0
 	/* This is possible if op is under delayed unoptimizing */
 	if (kprobe_disabled(&op->kp))
 		return;
 
+	local_irq_save(flags);
 	preempt_disable();

No reason to disable preemption if you disabled interrupts.

Right,

Quoted 15 lines

ops, missed that.. attaching new patch

---
Disabling irqs during optimized callback, so we dont miss any in-irq kprobes as missed.

running following:

# cd /debug/tracing/
# echo "p mutex_unlock" >> kprobe_events
# echo "p _raw_spin_lock" >> kprobe_events
# echo "p smp_apic_timer_interrupt" >> ./kprobe_events
# echo 1 > events/enable
Quoted 4 lines

makes the optimized kprobes to be missed. None is missed if the kprobe optimatization is disabled.

Ingo, could you pull this as a bugfix?

Thank you!

Quoted 32 lines
---
 arch/x86/kernel/kprobes.c |    5 +++--
 1 files changed, 3 insertions(+), 2 deletions(-)
arch/x86/kernel/kprobes.c +3 -2
index c969fd9..f1a6244 100644
@@ -1183,12 +1183,13 @@ static void __kprobes optimized_callback(struct optimized_kprobe *op,
 					 struct pt_regs *regs)
 {
 	struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
+	unsigned long flags;
 
 	/* This is possible if op is under delayed unoptimizing */
 	if (kprobe_disabled(&op->kp))
 		return;
 
-	preempt_disable();
+	local_irq_save(flags);
 	if (kprobe_running()) {
 		kprobes_inc_nmissed_count(&op->kp);
 	} else {
@@ -1207,7 +1208,7 @@ static void __kprobes optimized_callback(struct optimized_kprobe *op,
 		opt_pre_handler(&op->kp, regs);
 		__this_cpu_write(current_kprobe, NULL);
 	}
-	preempt_enable_no_resched();
+	local_irq_restore(flags);
 }
 
 static int __kprobes copy_optimized_instructions(u8 *dest, u8 *src)
Signature · 4 lines
Masami HIRAMATSU
Software Platform Research Dept. Linux Technology Center
Hitachi, Ltd., Yokohama Research Laboratory
E-mail: [email protected]
author
#4 lore raw
Quoted 3 lines · Masami Hiramatsu
Quoted 2 lines · Jiri Olsa
Quoted 1 line · Steven Rostedt

On Tue, Apr 26, 2011 at 03:01:31PM +0200, Jiri Olsa wrote:

SNIP

Quoted 11 lines
Quoted 6 lines
Quoted 1 line
Quoted 1 line

hi,

makes the optimized kprobes to be missed. None is missed if the kprobe optimatization is disabled.

Ingo, could you pull this as a bugfix?

hi,
could this be pulled in?

thanks,
jirka

Quoted 43 lines

Thank you!

Quoted 32 lines
---
 arch/x86/kernel/kprobes.c |    5 +++--
 1 files changed, 3 insertions(+), 2 deletions(-)
arch/x86/kernel/kprobes.c +3 -2
index c969fd9..f1a6244 100644
@@ -1183,12 +1183,13 @@ static void __kprobes optimized_callback(struct optimized_kprobe *op,
 					 struct pt_regs *regs)
 {
 	struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
+	unsigned long flags;
 
 	/* This is possible if op is under delayed unoptimizing */
 	if (kprobe_disabled(&op->kp))
 		return;
 
-	preempt_disable();
+	local_irq_save(flags);
 	if (kprobe_running()) {
 		kprobes_inc_nmissed_count(&op->kp);
 	} else {
@@ -1207,7 +1208,7 @@ static void __kprobes optimized_callback(struct optimized_kprobe *op,
 		opt_pre_handler(&op->kp, regs);
 		__this_cpu_write(current_kprobe, NULL);
 	}
-	preempt_enable_no_resched();
+	local_irq_restore(flags);
 }
 
 static int __kprobes copy_optimized_instructions(u8 *dest, u8 *src)
Signature · 4 lines
Masami HIRAMATSU
Software Platform Research Dept. Linux Technology Center
Hitachi, Ltd., Yokohama Research Laboratory
E-mail: [email protected]
#5 lore raw
In reply to Jiri Olsa

arch/x86/kernel/kprobes.c static void __kprobes optimized_callback(struct optimized_kprobe *op, · line 1213

Quoted 9 lines · Jiri Olsa
arch/x86/kernel/kprobes.c +2 -0
+	local_irq_save(flags);
 	preempt_disable();
 	if (kprobe_running()) {
 		kprobes_inc_nmissed_count(&op->kp);
@@ -1208,6 +1210,7 @@ static void __kprobes optimized_callback(struct optimized_kprobe *op,
 		__this_cpu_write(current_kprobe, NULL);
 	}
 	preempt_enable_no_resched();
+	local_irq_restore(flags);

irq-disable is synonymous to preempt disable so the preempt_disable()/enable() pair looks like superfluous overhead.

Thanks,

Ingo

author
#6 lore raw
Quoted 15 lines · Ingo Molnar

arch/x86/kernel/kprobes.c static void __kprobes optimized_callback(struct optimized_kprobe *op, · line 1213

Quoted 9 lines · Jiri Olsa
arch/x86/kernel/kprobes.c +2 -0
+	local_irq_save(flags);
 	preempt_disable();
 	if (kprobe_running()) {
 		kprobes_inc_nmissed_count(&op->kp);
@@ -1208,6 +1210,7 @@ static void __kprobes optimized_callback(struct optimized_kprobe *op,
 		__this_cpu_write(current_kprobe, NULL);
 	}
 	preempt_enable_no_resched();
+	local_irq_restore(flags);

irq-disable is synonymous to preempt disable so the preempt_disable()/enable() pair looks like superfluous overhead.

yes, there's correct patch already in the list here:
http://marc.info/?l=linux-kernel&m=130382756829695&w=2

thanks,
jirka

Quoted 4 lines

Thanks,

Ingo

#7 lore raw
Quoted 17 lines · Jiri Olsa
Quoted 15 lines · Ingo Molnar

arch/x86/kernel/kprobes.c static void __kprobes optimized_callback(struct optimized_kprobe *op, · line 1213

Quoted 9 lines · Jiri Olsa
arch/x86/kernel/kprobes.c +2 -0
+	local_irq_save(flags);
 	preempt_disable();
 	if (kprobe_running()) {
 		kprobes_inc_nmissed_count(&op->kp);
@@ -1208,6 +1210,7 @@ static void __kprobes optimized_callback(struct optimized_kprobe *op,
 		__this_cpu_write(current_kprobe, NULL);
 	}
 	preempt_enable_no_resched();
+	local_irq_restore(flags);

irq-disable is synonymous to preempt disable so the preempt_disable()/enable() pair looks like superfluous overhead.

Quoted 2 lines

yes, there's correct patch already in the list here:
http://marc.info/?l=linux-kernel&m=130382756829695&w=2

It helps to change the subject line when you think another patch should be considered, to something like:

[PATCH, v2] kprobes, x86: Disable irq during optimized callback

(also note the other changes i made to the title, 3 altogether.)

Thanks,

Ingo

author
#8 lore raw

PATCH v2 1/1 kprobes, x86: Disable irq during optimized callback

Quoted 23 lines · Ingo Molnar
Quoted 17 lines · Jiri Olsa
Quoted 15 lines · Ingo Molnar

Review static void __kprobes optimized_callback(struct optimized_kprobe *op, · line 1213

Quoted 9 lines · Jiri Olsa
(quoted diff) +2 -0
+	local_irq_save(flags);
 	preempt_disable();
 	if (kprobe_running()) {
 		kprobes_inc_nmissed_count(&op->kp);
@@ -1208,6 +1210,7 @@ static void __kprobes optimized_callback(struct optimized_kprobe *op,
 		__this_cpu_write(current_kprobe, NULL);
 	}
 	preempt_enable_no_resched();
+	local_irq_restore(flags);

irq-disable is synonymous to preempt disable so the preempt_disable()/enable() pair looks like superfluous overhead.

Quoted 2 lines

yes, there's correct patch already in the list here:
http://marc.info/?l=linux-kernel&m=130382756829695&w=2

Quoted 6 lines

It helps to change the subject line when you think another patch should be considered, to something like:

[PATCH, v2] kprobes, x86: Disable irq during optimized callback

(also note the other changes i made to the title, 3 altogether.)

sorry, here it is ;) thanks

jirka

---
Disabling irqs during optimized callback, so we dont miss any in-irq kprobes as missed.

running following:

# cd /debug/tracing/
# echo "p mutex_unlock" >> kprobe_events
# echo "p _raw_spin_lock" >> kprobe_events
# echo "p smp_apic_timer_interrupt" >> ./kprobe_events
# echo 1 > events/enable

makes the optimized kprobes to be missed. None is missed if the kprobe optimatization is disabled.

---
 arch/x86/kernel/kprobes.c |    5 +++--
 1 files changed, 3 insertions(+), 2 deletions(-)
arch/x86/kernel/kprobes.c +3 -2
index c969fd9..f1a6244 100644
@@ -1183,12 +1183,13 @@ static void __kprobes optimized_callback(struct optimized_kprobe *op,
 					 struct pt_regs *regs)
 {
 	struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
+	unsigned long flags;
 
 	/* This is possible if op is under delayed unoptimizing */
 	if (kprobe_disabled(&op->kp))
 		return;
 
-	preempt_disable();
+	local_irq_save(flags);
 	if (kprobe_running()) {
 		kprobes_inc_nmissed_count(&op->kp);
 	} else {
@@ -1207,7 +1208,7 @@ static void __kprobes optimized_callback(struct optimized_kprobe *op,
 		opt_pre_handler(&op->kp, regs);
 		__this_cpu_write(current_kprobe, NULL);
 	}
-	preempt_enable_no_resched();
+	local_irq_restore(flags);
 }
 
 static int __kprobes copy_optimized_instructions(u8 *dest, u8 *src)
Signature · 1 line
1.7.1
#9 lore raw

[tip:perf/urgent] kprobes, x86: Disable irqs during optimized callback

Commit-ID:  9bbeacf52f66d165739a4bbe9c018d17493a74b5
Gitweb:     http://git.kernel.org/tip/9bbeacf52f66d165739a4bbe9c018d17493a74b5
Author:     Jiri Olsa <[email protected]>
AuthorDate: Wed, 11 May 2011 13:06:13 +0200
Committer:  Ingo Molnar <[email protected]>
CommitDate: Wed, 11 May 2011 13:21:23 +0200

kprobes, x86: Disable irqs during optimized callback

Disable irqs during optimized callback, so we dont miss any in-irq kprobes.

The following commands:

 # cd /debug/tracing/
 # echo "p mutex_unlock" >> kprobe_events
 # echo "p _raw_spin_lock" >> kprobe_events
 # echo "p smp_apic_timer_interrupt" >> ./kprobe_events
 # echo 1 > events/enable

Cause the optimized kprobes to be missed. None is missed with the fix applied.

---
 arch/x86/kernel/kprobes.c |    5 +++--
 1 files changed, 3 insertions(+), 2 deletions(-)
arch/x86/kernel/kprobes.c +3 -2
index c969fd9..f1a6244 100644
@@ -1183,12 +1183,13 @@ static void __kprobes optimized_callback(struct optimized_kprobe *op,
 					 struct pt_regs *regs)
 {
 	struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
+	unsigned long flags;
 
 	/* This is possible if op is under delayed unoptimizing */
 	if (kprobe_disabled(&op->kp))
 		return;
 
-	preempt_disable();
+	local_irq_save(flags);
 	if (kprobe_running()) {
 		kprobes_inc_nmissed_count(&op->kp);
 	} else {
@@ -1207,7 +1208,7 @@ static void __kprobes optimized_callback(struct optimized_kprobe *op,
 		opt_pre_handler(&op->kp, regs);
 		__this_cpu_write(current_kprobe, NULL);
 	}
-	preempt_enable_no_resched();
+	local_irq_restore(flags);
 }
 
 static int __kprobes copy_optimized_instructions(u8 *dest, u8 *src)