Skip to contentSkip to conversation

riscv: keep interrupts disabled for BREAKPOINT exception

PATCH In review Jisheng Zhang 6 participants 12 messages Mar 29, 2021 – Apr 12, 2021 RISC-V

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 riscv: keep interrupts disabled for BREAKPOINT exception

From: Jisheng Zhang <[email protected]>

Current riscv's kprobe handlers are run with both preemption and interrupt enabled, this violates kprobe requirements. Fix this issue by keeping interrupts disabled for BREAKPOINT exception.

Fixes c22b0bcb1dd0 ("riscv: Add kprobes supported")
---
 arch/riscv/kernel/entry.S | 2 ++
 1 file changed, 2 insertions(+)
arch/riscv/kernel/entry.S +2 -0
index 744f3209c48d..4114b65698ec 100644
@@ -130,6 +130,8 @@ skip_context_tracking:
 	 */
 	andi t0, s1, SR_PIE
 	beqz t0, 1f
+	li t0, EXC_BREAKPOINT
+	beq s4, t0, 1f
 #ifdef CONFIG_TRACE_IRQFLAGS
 	call trace_hardirqs_on
 #endif
Signature · 1 line
2.31.0
#1 lore raw

Hi Jisheng,

Quoted 5 lines · On Tue, 30 Mar 2021 02:16:24 +0800 Jisheng Zhang <[email protected]>…

From: Jisheng Zhang <[email protected]>

Current riscv's kprobe handlers are run with both preemption and interrupt enabled, this violates kprobe requirements. Fix this issue by keeping interrupts disabled for BREAKPOINT exception.

Not only while the breakpoint exception but also until the end of the single step (maybe you are using __BUG_INSN_32 ??) need to be disable interrupts. Can this do that?

Thank you,

Quoted 24 lines
Fixes c22b0bcb1dd0 ("riscv: Add kprobes supported")
---
 arch/riscv/kernel/entry.S | 2 ++
 1 file changed, 2 insertions(+)
arch/riscv/kernel/entry.S +2 -0
index 744f3209c48d..4114b65698ec 100644
@@ -130,6 +130,8 @@ skip_context_tracking:
 	 */
 	andi t0, s1, SR_PIE
 	beqz t0, 1f
+	li t0, EXC_BREAKPOINT
+	beq s4, t0, 1f
 #ifdef CONFIG_TRACE_IRQFLAGS
 	call trace_hardirqs_on
 #endif
Signature · 1 line
2.31.0
Signature · 6 lines
Masami Hiramatsu <[email protected]>

_______________________________________________
linux-riscv mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-riscv
#2 lore raw
Quoted 1 line · On Tue, 30 Mar 2021 18:33:16 +0900 Masami Hiramatsu <[email protected]> wrote:

Hi Jisheng,

Hi Masami,

Quoted 6 lines
Quoted 2 lines · On Tue, 30 Mar 2021 02:16:24 +0800 Jisheng Zhang <[email protected]>…

From: Jisheng Zhang <[email protected]>

Quoted 8 lines
Quoted 3 lines

Current riscv's kprobe handlers are run with both preemption and interrupt enabled, this violates kprobe requirements. Fix this issue by keeping interrupts disabled for BREAKPOINT exception.

Not only while the breakpoint exception but also until the end of the single step (maybe you are using __BUG_INSN_32 ??) need to be disable interrupts. Can this do that?

interrupt is disabled during "single step" by kprobes_save_local_irqflag() and kprobes_restore_local_irqflag(). The code flow looks like:

do_trap_break() // for bp
kprobe_breakpoint_handler()
setup_singlestep()
kprobes_restore_local_irqflag()

do_trap_break() // for ss
kprobe_single_step_handler()
kprobes_restore_local_irqflag()

Thanks

_______________________________________________
linux-riscv mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-riscv

Reviewed-by
#3 lore raw

Hi,

Quoted 25 lines · On Wed, 31 Mar 2021 22:22:44 +0800 Jisheng Zhang <[email protected]>…
Quoted 1 line · On Tue, 30 Mar 2021 18:33:16 +0900 Masami Hiramatsu <[email protected]> wrote:

Hi Jisheng,

Hi Masami,

Quoted 6 lines
Quoted 2 lines · On Tue, 30 Mar 2021 02:16:24 +0800 Jisheng Zhang <[email protected]>…

From: Jisheng Zhang <[email protected]>

Quoted 8 lines
Quoted 3 lines

Current riscv's kprobe handlers are run with both preemption and interrupt enabled, this violates kprobe requirements. Fix this issue by keeping interrupts disabled for BREAKPOINT exception.

Not only while the breakpoint exception but also until the end of the single step (maybe you are using __BUG_INSN_32 ??) need to be disable interrupts. Can this do that?

interrupt is disabled during "single step" by kprobes_save_local_irqflag() and kprobes_restore_local_irqflag(). The code flow looks like:

Quoted 8 lines

do_trap_break() // for bp
kprobe_breakpoint_handler()
setup_singlestep()
kprobes_restore_local_irqflag()

do_trap_break() // for ss
kprobe_single_step_handler()
kprobes_restore_local_irqflag()

OK, thanks for the confirmation!

Thanks,

Signature · 6 lines
Masami Hiramatsu <[email protected]>

_______________________________________________
linux-riscv mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-riscv
#8 lore raw
In reply to Jisheng Zhang

Hi Jisheng,

Quoted 25 lines · Jisheng Zhang
Quoted 1 line · On Tue, 30 Mar 2021 18:33:16 +0900 Masami Hiramatsu <[email protected]> wrote:

Hi Jisheng,

Hi Masami,

Quoted 6 lines
Quoted 2 lines · On Tue, 30 Mar 2021 02:16:24 +0800 Jisheng Zhang <[email protected]>…

From: Jisheng Zhang <[email protected]>

Quoted 8 lines
Quoted 3 lines

Current riscv's kprobe handlers are run with both preemption and interrupt enabled, this violates kprobe requirements. Fix this issue by keeping interrupts disabled for BREAKPOINT exception.

Not only while the breakpoint exception but also until the end of the single step (maybe you are using __BUG_INSN_32 ??) need to be disable interrupts. Can this do that?

interrupt is disabled during "single step" by kprobes_save_local_irqflag() and kprobes_restore_local_irqflag(). The code flow looks like:

Quoted 8 lines

do_trap_break() // for bp
kprobe_breakpoint_handler()
setup_singlestep()
kprobes_restore_local_irqflag()

do_trap_break() // for ss
kprobe_single_step_handler()
kprobes_restore_local_irqflag()

Recently, kernel hit BUG_ON() on QEMU after I install a probe at "sys_read" via kprobe, accoriding to my debugging and analysis it looks like caused by the "irq disable" operation for single-stepping.

I present a detailed description about this problem in the mail with title "[PATCH] riscv/kprobe: fix kernel panic when invoking sys_read traced by kprobe". Looking forward to some feedback,Thanks.

BR,
Liao Chang

Quoted 10 lines

Thanks

_______________________________________________
linux-riscv mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-riscv
.

_______________________________________________
linux-riscv mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-riscv

#9 lore raw
Quoted 1 line · On Thu, 1 Apr 2021 16:49:47 +0800 "liaochang (A)" <[email protected]> wrote:

Hi Jisheng,

Hi,

Quoted 32 lines · Jisheng Zhang
Quoted 30 lines
Quoted 1 line · On Tue, 30 Mar 2021 18:33:16 +0900 Masami Hiramatsu <[email protected]> wrote:

Hi Jisheng,

Hi Masami,

Quoted 6 lines
Quoted 2 lines · On Tue, 30 Mar 2021 02:16:24 +0800 Jisheng Zhang <[email protected]>…

From: Jisheng Zhang <[email protected]>

Quoted 8 lines
Quoted 3 lines

Current riscv's kprobe handlers are run with both preemption and interrupt enabled, this violates kprobe requirements. Fix this issue by keeping interrupts disabled for BREAKPOINT exception.

Not only while the breakpoint exception but also until the end of the single step (maybe you are using __BUG_INSN_32 ??) need to be disable interrupts. Can this do that?

interrupt is disabled during "single step" by kprobes_save_local_irqflag() and kprobes_restore_local_irqflag(). The code flow looks like:

do_trap_break() // for bp
kprobe_breakpoint_handler()
setup_singlestep()
kprobes_restore_local_irqflag()

Quoted 5 lines
Quoted 3 lines

do_trap_break() // for ss
kprobe_single_step_handler()
kprobes_restore_local_irqflag()

Recently, kernel hit BUG_ON() on QEMU after I install a probe at "sys_read" via kprobe,

TIPS: Each line should not exceed 80 chars

Quoted 5 lines

accoriding to my debugging and analysis it looks like caused by the "irq disable" operation for single-stepping.

I present a detailed description about this problem in the mail with title "[PATCH] riscv/kprobe: fix kernel panic when invoking sys_read traced by kprobe". Looking forward to some feedback,Thanks.

I will comment that patch.

thanks

#4 lore raw
In reply to Masami Hiramatsu
Quoted 17 lines · Masami Hiramatsu
Quoted 17 lines
Quoted 8 lines
Quoted 3 lines

Current riscv's kprobe handlers are run with both preemption and interrupt enabled, this violates kprobe requirements. Fix this issue by keeping interrupts disabled for BREAKPOINT exception.

Not only while the breakpoint exception but also until the end of the single step (maybe you are using __BUG_INSN_32 ??) need to be disable interrupts. Can this do that?

interrupt is disabled during "single step" by kprobes_save_local_irqflag() and kprobes_restore_local_irqflag(). The code flow looks like:

do_trap_break() // for bp
kprobe_breakpoint_handler()
setup_singlestep()
kprobes_restore_local_irqflag()

Quoted 5 lines
Quoted 3 lines

do_trap_break() // for ss
kprobe_single_step_handler()
kprobes_restore_local_irqflag()

OK, thanks for the confirmation!

Is this approach guaranteed to keep interrupt handling latency low enough
for the system not to be negatively affected, e.g. for the purpose of NTP timekeeping?

Maciej

#10 lore raw
In reply to Jisheng Zhang

Hi Jisheng,

Quoted 47 lines · Jisheng Zhang
Quoted 1 line · On Thu, 1 Apr 2021 16:49:47 +0800 "liaochang (A)" <[email protected]> wrote:

Hi Jisheng,

Hi,

Quoted 32 lines · Jisheng Zhang
Quoted 30 lines
Quoted 1 line · On Tue, 30 Mar 2021 18:33:16 +0900 Masami Hiramatsu <[email protected]> wrote:

Hi Jisheng,

Hi Masami,

Quoted 6 lines
Quoted 2 lines · On Tue, 30 Mar 2021 02:16:24 +0800 Jisheng Zhang <[email protected]>…

From: Jisheng Zhang <[email protected]>

Quoted 8 lines
Quoted 3 lines

Current riscv's kprobe handlers are run with both preemption and interrupt enabled, this violates kprobe requirements. Fix this issue by keeping interrupts disabled for BREAKPOINT exception.

Not only while the breakpoint exception but also until the end of the single step (maybe you are using __BUG_INSN_32 ??) need to be disable interrupts. Can this do that?

interrupt is disabled during "single step" by kprobes_save_local_irqflag() and kprobes_restore_local_irqflag(). The code flow looks like:

do_trap_break() // for bp
kprobe_breakpoint_handler()
setup_singlestep()
kprobes_restore_local_irqflag()

Quoted 5 lines
Quoted 3 lines

do_trap_break() // for ss
kprobe_single_step_handler()
kprobes_restore_local_irqflag()

Recently, kernel hit BUG_ON() on QEMU after I install a probe at "sys_read" via kprobe,

TIPS: Each line should not exceed 80 chars

Quoted 7 lines
Quoted 5 lines

accoriding to my debugging and analysis it looks like caused by the "irq disable" operation for single-stepping.

I present a detailed description about this problem in the mail with title "[PATCH] riscv/kprobe: fix kernel panic when invoking sys_read traced by kprobe". Looking forward to some feedback,Thanks.

I will comment that patch.

Thanks for your reminder.

Quoted 5 lines

thanks

.

#5 lore raw
In reply to Maciej W. Rozycki
Quoted 26 lines · On Sat, 3 Apr 2021 20:30:53 +0200 (CEST) "Maciej W. Rozycki"…

CAUTION: Email originated externally, do not click links or open attachments unless you recognize the sender and know the content is safe.

Quoted 21 lines · Masami Hiramatsu
Quoted 20 lines
Quoted 8 lines
Quoted 3 lines

Current riscv's kprobe handlers are run with both preemption and interrupt enabled, this violates kprobe requirements. Fix this issue by keeping interrupts disabled for BREAKPOINT exception.

Not only while the breakpoint exception but also until the end of the single step (maybe you are using __BUG_INSN_32 ??) need to be disable interrupts. Can this do that?

interrupt is disabled during "single step" by kprobes_save_local_irqflag() and kprobes_restore_local_irqflag(). The code flow looks like:

do_trap_break() // for bp
kprobe_breakpoint_handler()
setup_singlestep()
kprobes_restore_local_irqflag()

do_trap_break() // for ss
kprobe_single_step_handler()
kprobes_restore_local_irqflag()

Quoted 5 lines
Quoted 1 line

OK, thanks for the confirmation!

Is this approach guaranteed to keep interrupt handling latency low enough
for the system not to be negatively affected, e.g. for the purpose of NTP timekeeping?

IMHO, interrupt latency can't be ensured if kprobes is triggered.

thanks

#6 lore raw
Quoted 29 lines · On Thu, 8 Apr 2021 19:23:48 +0800 Jisheng Zhang <[email protected]>…
Quoted 26 lines · On Sat, 3 Apr 2021 20:30:53 +0200 (CEST) "Maciej W. Rozycki"…

CAUTION: Email originated externally, do not click links or open attachments unless you recognize the sender and know the content is safe.

Quoted 21 lines · Masami Hiramatsu
Quoted 20 lines
Quoted 8 lines
Quoted 3 lines

Current riscv's kprobe handlers are run with both preemption and interrupt enabled, this violates kprobe requirements. Fix this issue by keeping interrupts disabled for BREAKPOINT exception.

Not only while the breakpoint exception but also until the end of the single step (maybe you are using __BUG_INSN_32 ??) need to be disable interrupts. Can this do that?

interrupt is disabled during "single step" by kprobes_save_local_irqflag() and kprobes_restore_local_irqflag(). The code flow looks like:

do_trap_break() // for bp
kprobe_breakpoint_handler()
setup_singlestep()
kprobes_restore_local_irqflag()

do_trap_break() // for ss
kprobe_single_step_handler()
kprobes_restore_local_irqflag()

Quoted 7 lines
Quoted 5 lines
Quoted 1 line

OK, thanks for the confirmation!

Is this approach guaranteed to keep interrupt handling latency low enough
for the system not to be negatively affected, e.g. for the purpose of NTP timekeeping?

IMHO, interrupt latency can't be ensured if kprobes is triggered.

Indeed. The latency depends on what the kprobe user handler does. Of course it must be as minimal as possible... On x86, it is less than a few microseconds. Thus it may be a jitter on hard realtime system, but not a big issue on usual system like NTP timekeeping.

Thank you,

Signature · 1 line
Masami Hiramatsu <[email protected]>
#7 lore raw
Quoted 10 lines · Masami Hiramatsu
Quoted 5 lines
Quoted 3 lines

Is this approach guaranteed to keep interrupt handling latency low enough
for the system not to be negatively affected, e.g. for the purpose of NTP timekeeping?

IMHO, interrupt latency can't be ensured if kprobes is triggered.

Indeed. The latency depends on what the kprobe user handler does. Of course it must be as minimal as possible... On x86, it is less than a few microseconds. Thus it may be a jitter on hard realtime system, but not a big issue on usual system like NTP timekeeping.

Ack. Assuming that the breakpoint exception will only disable interrupts
if kprobes are in use it seems reasonable to me.

Thanks for double-checking.

Maciej

maintainer
#11 lore raw
In reply to Jisheng Zhang
Quoted 17 lines · On Mon, 29 Mar 2021 11:16:24 PDT (-0700), [email protected] wrote:

From: Jisheng Zhang <[email protected]>

Current riscv's kprobe handlers are run with both preemption and interrupt enabled, this violates kprobe requirements. Fix this issue by keeping interrupts disabled for BREAKPOINT exception.

Fixes c22b0bcb1dd0 ("riscv: Add kprobes supported")
---
 arch/riscv/kernel/entry.S | 2 ++
 1 file changed, 2 insertions(+)
arch/riscv/kernel/entry.S +0 -0
index 744f3209c48d..4114b65698ec 100644

arch/riscv/kernel/entry.S skip_context_tracking: · line 137

Quoted 8 lines
arch/riscv/kernel/entry.S +2 -0
@@ -130,6 +130,8 @@ skip_context_tracking:
 	 */
 	andi t0, s1, SR_PIE
 	beqz t0, 1f
+	li t0, EXC_BREAKPOINT
+	beq s4, t0, 1f
 #ifdef CONFIG_TRACE_IRQFLAGS
 	call trace_hardirqs_on
 #endif

This is on fixes, with a comment as otherwise I'm just going to forget why.