Skip to contentSkip to conversation

riscv: correct the do_trap_break()

PATCH In review Vincent Chen 4 participants 20 messages Sep 23, 2019 – Oct 9, 2019 RISC-V

lore
Share & tools
lore
#0 lore raw

cover riscv: correct the do_trap_break()

The following three situations may occur in the current implementation of do_trap_break().
1. When the CONFIG_GENERIC_BUG is disabled, if a kernel thread is trapped by BUG(), the whole system will be in the loop that infinitely handles the break exception instead of entering the die function.
2. When the kernel runs code on behalf of a user thread, and the kernel executes a WARN() or WARN_ON(), the user thread will be sent a bogus SIGTRAP.
3. Handling the unexpected ebreak instructions is to send a SIGTRAP to the trapped thread. However, if a kernel executes an unexpected ebreak, it may cause the kernel thread to be stuck in the ebreak instruction.

This patch set will solve the above problems by adjusting the implementations of the do_trap_break().

Vincent Chen (4):
riscv: avoid kernel hangs when trapped in BUG()
rsicv: avoid sending a SIGTRAP to a user thread trapped in WARN()
riscv: Correct the handling of unexpected ebreak in do_trap_break()
riscv: remove the switch statement in do_trap_break()

 arch/riscv/kernel/traps.c | 18 +++++++++---------
 1 file changed, 9 insertions(+), 9 deletions(-)
Signature · 1 line
2.7.4
#1 lore raw

PATCH 1/4 riscv: avoid kernel hangs when trapped in BUG()

When the CONFIG_GENERIC_BUG is disabled by disabling CONFIG_BUG, if a kernel thread is trapped by BUG(), the whole system will be in the loop that infinitely handles the ebreak exception instead of entering the die function. To fix this problem, the do_trap_break() will always call the die() to deal with the break exception as the type of break is BUG_TRAP_TYPE_BUG.

---
 arch/riscv/kernel/traps.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
arch/riscv/kernel/traps.c +3 -3
index 424eb72d56b1..055a937aca70 100644
@@ -124,23 +124,23 @@ static inline unsigned long get_break_insn_length(unsigned long pc)
 
 asmlinkage void do_trap_break(struct pt_regs *regs)
 {
-#ifdef CONFIG_GENERIC_BUG
 	if (!user_mode(regs)) {
 		enum bug_trap_type type;
 
 		type = report_bug(regs->sepc, regs);
 		switch (type) {
+#ifdef CONFIG_GENERIC_BUG
 		case BUG_TRAP_TYPE_NONE:
 			break;
 		case BUG_TRAP_TYPE_WARN:
 			regs->sepc += get_break_insn_length(regs->sepc);
 			break;
 		case BUG_TRAP_TYPE_BUG:
+#endif /* CONFIG_GENERIC_BUG */
+		default:
 			die(regs, "Kernel BUG");
 		}
 	}
-#endif /* CONFIG_GENERIC_BUG */
-
 	force_sig_fault(SIGTRAP, TRAP_BRKPT, (void __user *)(regs->sepc));
 }
 
Signature · 7 lines
2.7.4


_______________________________________________
linux-riscv mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-riscv
Reviewed-by on v1 1/4
#2 lore raw

Re: [PATCH 1/4] riscv: avoid kernel hangs when trapped in BUG()

Quoted 8 lines · Vincent Chen

When the CONFIG_GENERIC_BUG is disabled by disabling CONFIG_BUG, if a kernel thread is trapped by BUG(), the whole system will be in the loop that infinitely handles the ebreak exception instead of entering the die function. To fix this problem, the do_trap_break() will always call the die() to deal with the break exception as the type of break is BUG_TRAP_TYPE_BUG.

Looks good,

#3 lore raw

Re: [PATCH 1/4] riscv: avoid kernel hangs when trapped in BUG()

In reply to Vincent Chen
Quoted 8 lines · Vincent Chen

When the CONFIG_GENERIC_BUG is disabled by disabling CONFIG_BUG, if a kernel thread is trapped by BUG(), the whole system will be in the loop that infinitely handles the ebreak exception instead of entering the die function. To fix this problem, the do_trap_break() will always call the die() to deal with the break exception as the type of break is BUG_TRAP_TYPE_BUG.

Thanks, queued for v5.4-rc.

- Paul

#4 lore raw

PATCH 2/4 rsicv: avoid sending a SIGTRAP to a user thread trapped in WARN()

In reply to Vincent Chen

On RISC-V, when the kernel runs code on behalf of a user thread, and the kernel executes a WARN() or WARN_ON(), the user thread will be sent a bogus SIGTRAP. Fix the RISC-V kernel code to not send a SIGTRAP when a WARN()/WARN_ON() is executed.

---
 arch/riscv/kernel/traps.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
arch/riscv/kernel/traps.c +1 -1
index 055a937aca70..82f42a55451e 100644
@@ -134,7 +134,7 @@ asmlinkage void do_trap_break(struct pt_regs *regs)
 			break;
 		case BUG_TRAP_TYPE_WARN:
 			regs->sepc += get_break_insn_length(regs->sepc);
-			break;
+			return;
 		case BUG_TRAP_TYPE_BUG:
 #endif /* CONFIG_GENERIC_BUG */
 		default:
Signature · 1 line
2.7.4
Reviewed-by on v1 2/4
#5 lore raw

Re: [PATCH 2/4] rsicv: avoid sending a SIGTRAP to a user thread trapped in WARN()

Quoted 6 lines · Vincent Chen

On RISC-V, when the kernel runs code on behalf of a user thread, and the kernel executes a WARN() or WARN_ON(), the user thread will be sent a bogus SIGTRAP. Fix the RISC-V kernel code to not send a SIGTRAP when a WARN()/WARN_ON() is executed.

Looks good,

#7 lore raw

Re: [PATCH 2/4] rsicv: avoid sending a SIGTRAP to a user thread trapped in WARN()

Quoted 2 lines · Christoph Hellwig

Oh and s/rsicv/riscv/ in the subject, please.

Oh! Thank you for finding this typo.
I will correct it.

#8 lore raw

Re: [PATCH 2/4] rsicv: avoid sending a SIGTRAP to a user thread trapped in WARN()

In reply to Vincent Chen
Quoted 6 lines · Vincent Chen

On RISC-V, when the kernel runs code on behalf of a user thread, and the kernel executes a WARN() or WARN_ON(), the user thread will be sent a bogus SIGTRAP. Fix the RISC-V kernel code to not send a SIGTRAP when a WARN()/WARN_ON() is executed.

Thanks, queued for v5.4-rc.

- Paul

#9 lore raw

PATCH 3/4 riscv: Correct the handling of unexpected ebreak in do_trap_break()

In reply to Vincent Chen

For the kernel space, all ebreak instructions are determined at compile time because the kernel space debugging module is currently unsupported. Hence, it should be treated as a bug if an ebreak instruction which does not belong to BUG_TRAP_TYPE_WARN or BUG_TRAP_TYPE_BUG is executed in kernel space. For the userspace, debugging module or user problem may intentionally insert an ebreak instruction to trigger a SIGTRAP signal. To approach the above two situations, the do_trap_break() will direct the BUG_TRAP_TYPE_NONE ebreak exception issued in kernel space to die() and will send a SIGTRAP to the trapped process only when the ebreak is in userspace.

---
 arch/riscv/kernel/traps.c | 7 +++----
 1 file changed, 3 insertions(+), 4 deletions(-)
arch/riscv/kernel/traps.c +3 -4
index 82f42a55451e..dd13bc90aeb6 100644
@@ -130,8 +130,6 @@ asmlinkage void do_trap_break(struct pt_regs *regs)
 		type = report_bug(regs->sepc, regs);
 		switch (type) {
 #ifdef CONFIG_GENERIC_BUG
-		case BUG_TRAP_TYPE_NONE:
-			break;
 		case BUG_TRAP_TYPE_WARN:
 			regs->sepc += get_break_insn_length(regs->sepc);
 			return;
@@ -140,8 +138,9 @@ asmlinkage void do_trap_break(struct pt_regs *regs)
 		default:
 			die(regs, "Kernel BUG");
 		}
-	}
-	force_sig_fault(SIGTRAP, TRAP_BRKPT, (void __user *)(regs->sepc));
+	} else
+		force_sig_fault(SIGTRAP, TRAP_BRKPT,
+				(void __user *)(regs->sepc));
 }
 
 #ifdef CONFIG_GENERIC_BUG
Signature · 7 lines
2.7.4


_______________________________________________
linux-riscv mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-riscv
Reviewed-by Christoph Hellwig on v1 3/4
#11 lore raw

Re: [PATCH 3/4] riscv: Correct the handling of unexpected ebreak in do_trap_break()

In reply to Vincent Chen
Quoted 11 lines · Vincent Chen

For the kernel space, all ebreak instructions are determined at compile time because the kernel space debugging module is currently unsupported. Hence, it should be treated as a bug if an ebreak instruction which does not belong to BUG_TRAP_TYPE_WARN or BUG_TRAP_TYPE_BUG is executed in kernel space. For the userspace, debugging module or user problem may intentionally insert an ebreak instruction to trigger a SIGTRAP signal. To approach the above two situations, the do_trap_break() will direct the BUG_TRAP_TYPE_NONE ebreak exception issued in kernel space to die() and will send a SIGTRAP to the trapped process only when the ebreak is in userspace.

Quoted 1 line

Thanks, queued the following for v5.4-rc.

- Paul

From: Vincent Chen <[email protected]>
Date: Mon, 23 Sep 2019 08:45:16 +0800
Subject: [PATCH] riscv: Correct the handling of unexpected ebreak in
do_trap_break()

For the kernel space, all ebreak instructions are determined at compile time because the kernel space debugging module is currently unsupported. Hence, it should be treated as a bug if an ebreak instruction which does not belong to BUG_TRAP_TYPE_WARN or BUG_TRAP_TYPE_BUG is executed in kernel space. For the userspace, debugging module or user problem may intentionally insert an ebreak instruction to trigger a SIGTRAP signal. To approach the above two situations, the do_trap_break() will direct the BUG_TRAP_TYPE_NONE ebreak exception issued in kernel space to die() and will send a SIGTRAP to the trapped process only when the ebreak is in userspace.

---
 arch/riscv/kernel/traps.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
arch/riscv/kernel/traps.c +3 -3
index 82f42a55451e..93742df9067f 100644
@@ -130,8 +130,6 @@ asmlinkage void do_trap_break(struct pt_regs *regs)
 		type = report_bug(regs->sepc, regs);
 		switch (type) {
 #ifdef CONFIG_GENERIC_BUG
-		case BUG_TRAP_TYPE_NONE:
-			break;
 		case BUG_TRAP_TYPE_WARN:
 			regs->sepc += get_break_insn_length(regs->sepc);
 			return;
@@ -140,8 +138,10 @@ asmlinkage void do_trap_break(struct pt_regs *regs)
 		default:
 			die(regs, "Kernel BUG");
 		}
+	} else {
+		force_sig_fault(SIGTRAP, TRAP_BRKPT,
+				(void __user *)(regs->sepc));
 	}
-	force_sig_fault(SIGTRAP, TRAP_BRKPT, (void __user *)(regs->sepc));
 }
 
 #ifdef CONFIG_GENERIC_BUG
Signature · 1 line
2.23.0
#12 lore raw

PATCH 4/4 riscv: remove the switch statement in do_trap_break()

In reply to Vincent Chen

To make the code more straightforward, replacing the switch statement with if statement.

---
 arch/riscv/kernel/traps.c | 23 ++++++++++++-----------
 1 file changed, 12 insertions(+), 11 deletions(-)
arch/riscv/kernel/traps.c +12 -11
index dd13bc90aeb6..1ac75f7d0bff 100644
@@ -124,23 +124,24 @@ static inline unsigned long get_break_insn_length(unsigned long pc)
 
 asmlinkage void do_trap_break(struct pt_regs *regs)
 {
-	if (!user_mode(regs)) {
+	if (user_mode(regs)) {
+		force_sig_fault(SIGTRAP, TRAP_BRKPT,
+				(void __user *)(regs->sepc));
+		return;
+	}
+#ifdef CONFIG_GENERIC_BUG
+	{
 		enum bug_trap_type type;
 
 		type = report_bug(regs->sepc, regs);
-		switch (type) {
-#ifdef CONFIG_GENERIC_BUG
-		case BUG_TRAP_TYPE_WARN:
+		if (type == BUG_TRAP_TYPE_WARN) {
 			regs->sepc += get_break_insn_length(regs->sepc);
 			return;
-		case BUG_TRAP_TYPE_BUG:
-#endif /* CONFIG_GENERIC_BUG */
-		default:
-			die(regs, "Kernel BUG");
 		}
-	} else
-		force_sig_fault(SIGTRAP, TRAP_BRKPT,
-				(void __user *)(regs->sepc));
+	}
+#endif /* CONFIG_GENERIC_BUG */
+
+	die(regs, "Kernel BUG");
 }
 
 #ifdef CONFIG_GENERIC_BUG
Signature · 7 lines
2.7.4


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

Re: [PATCH 4/4] riscv: remove the switch statement in do_trap_break()

Quoted 24 lines · Vincent Chen

To make the code more straightforward, replacing the switch statement with if statement.

---
 arch/riscv/kernel/traps.c | 23 ++++++++++++-----------
 1 file changed, 12 insertions(+), 11 deletions(-)
arch/riscv/kernel/traps.c +7 -0
index dd13bc90aeb6..1ac75f7d0bff 100644
@@ -124,23 +124,24 @@ static inline unsigned long get_break_insn_length(unsigned long pc)
 
 asmlinkage void do_trap_break(struct pt_regs *regs)
 {
+	if (user_mode(regs)) {
+		force_sig_fault(SIGTRAP, TRAP_BRKPT,
+				(void __user *)(regs->sepc));
+		return;
+	}
+#ifdef CONFIG_GENERIC_BUG
+	{

arch/riscv/kernel/traps.c static inline unsigned long get_break_insn_length(unsigned long pc) · line 143

Quoted 10 lines
arch/riscv/kernel/traps.c +4 -0
 		enum bug_trap_type type;
 
 		type = report_bug(regs->sepc, regs);
+		if (type == BUG_TRAP_TYPE_WARN) {
 			regs->sepc += get_break_insn_length(regs->sepc);
 			return;
 		}
+#endif /* CONFIG_GENERIC_BUG */
+
+	die(regs, "Kernel BUG");

I like where this is going, but I think this can be improved further given that fact that report_bug has a nice stub for the !CONFIG_GENERIC_BUG case.

How about:

	if (user_mode(regs))
		force_sig_fault(SIGTRAP, TRAP_BRKPT, (void __user *)regs->sepc);
	else if (report_bug(regs->sepc, regs) == BUG_TRAP_TYPE_WARN)
		regs->sepc += get_break_insn_length(regs->sepc);
	else
		die(regs, "Kernel BUG");

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

#14 lore raw

Re: [PATCH 4/4] riscv: remove the switch statement in do_trap_break()

Vincent,

Quoted 6 lines · Christoph Hellwig
Quoted 5 lines · Vincent Chen

To make the code more straightforward, replacing the switch statement with if statement.

...

Quoted 4 lines

I like where this is going, but I think this can be improved further given that fact that report_bug has a nice stub for the !CONFIG_GENERIC_BUG case.

Quoted 9 lines

How about:

	if (user_mode(regs))
		force_sig_fault(SIGTRAP, TRAP_BRKPT, (void __user *)regs->sepc);
	else if (report_bug(regs->sepc, regs) == BUG_TRAP_TYPE_WARN)
		regs->sepc += get_break_insn_length(regs->sepc);
	else
		die(regs, "Kernel BUG");

Christoph's suggestion looks good to me. What do you think about this modification to your patch?

- Paul

From: Vincent Chen <[email protected]>
Date: Mon, 23 Sep 2019 08:45:17 +0800
Subject: [PATCH] riscv: remove the switch statement in do_trap_break()

To make the code more straightforward, replace the switch statement with an if statement.

[[email protected]: removed CONFIG_GENERIC_BUG tests per
Christoph's suggestion; cleaned up patch description]

---
 arch/riscv/kernel/traps.c | 21 +++++----------------
 1 file changed, 5 insertions(+), 16 deletions(-)
arch/riscv/kernel/traps.c +5 -16
index 93742df9067f..45b82be00714 100644
@@ -124,24 +124,13 @@ static inline unsigned long get_break_insn_length(unsigned long pc)
 
 asmlinkage void do_trap_break(struct pt_regs *regs)
 {
-	if (!user_mode(regs)) {
-		enum bug_trap_type type;
-
-		type = report_bug(regs->sepc, regs);
-		switch (type) {
-#ifdef CONFIG_GENERIC_BUG
-		case BUG_TRAP_TYPE_WARN:
-			regs->sepc += get_break_insn_length(regs->sepc);
-			return;
-		case BUG_TRAP_TYPE_BUG:
-#endif /* CONFIG_GENERIC_BUG */
-		default:
-			die(regs, "Kernel BUG");
-		}
-	} else {
+	if (user_mode(regs))
 		force_sig_fault(SIGTRAP, TRAP_BRKPT,
 				(void __user *)(regs->sepc));
-	}
+	else if (report_bug(regs->sepc, regs) == BUG_TRAP_TYPE_WARN)
+		regs->sepc += get_break_insn_length(regs->sepc);
+	else
+		die(regs, "Kernel BUG");
 }
 
 #ifdef CONFIG_GENERIC_BUG
Signature · 1 line
2.23.0
#15 lore raw

Re: [PATCH 4/4] riscv: remove the switch statement in do_trap_break()

arch/riscv/kernel/traps.c static inline unsigned long get_break_insn_length(unsigned long pc) · line 129

Quoted 2 lines · Paul Walmsley
arch/riscv/kernel/traps.c +0 -0
 		force_sig_fault(SIGTRAP, TRAP_BRKPT,
 				(void __user *)(regs->sepc));

No nee for the extra braces, which also means it all fits onto a single line. You could have just copied what I pasted..

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

#16 lore raw

Re: [PATCH 4/4] riscv: remove the switch statement in do_trap_break()

Quoted 6 lines · Christoph Hellwig

arch/riscv/kernel/traps.c static inline unsigned long get_break_insn_length(unsigned long pc) · line 129

Quoted 2 lines · Paul Walmsley
arch/riscv/kernel/traps.c +0 -0
 		force_sig_fault(SIGTRAP, TRAP_BRKPT,
 				(void __user *)(regs->sepc));

No nee for the extra braces, which also means it all fits onto a single line.

Good point, will drop the extra parens and remove the braces.

- Paul

#17 lore raw

Re: [PATCH 4/4] riscv: remove the switch statement in do_trap_break()

Sorry, I missed the comment. Christoph's suggestion is also good to me. I will modify it as you suggested.
Thanks

Quoted 14 lines · Paul Walmsley
Quoted 6 lines · Christoph Hellwig
Quoted 2 lines · Paul Walmsley
            force_sig_fault(SIGTRAP, TRAP_BRKPT,
                            (void __user *)(regs->sepc));

No nee for the extra braces, which also means it all fits onto a single line.

Good point, will drop the extra parens and remove the braces.

- Paul

#18 lore raw

Re: [PATCH 4/4] riscv: remove the switch statement in do_trap_break()

Quoted 2 lines · Vincent Chen

Sorry, I missed the comment. Christoph's suggestion is also good to me. I will modify it as you suggested.

Thanks - no need to resend, I'll queue the modified patch up here.

- Paul

#19 lore raw

Re: [PATCH 4/4] riscv: remove the switch statement in do_trap_break()

In reply to Christoph Hellwig
Quoted 6 lines · Christoph Hellwig

arch/riscv/kernel/traps.c static inline unsigned long get_break_insn_length(unsigned long pc) · line 129

Quoted 2 lines · Paul Walmsley
arch/riscv/kernel/traps.c +0 -0
 		force_sig_fault(SIGTRAP, TRAP_BRKPT,
 				(void __user *)(regs->sepc));

No nee for the extra braces, which also means it all fits onto a single line. You could have just copied what I pasted..

It turns out that the rewrite breaks allnoconfig:

https://lists.01.org/hyperkitty/list/[email protected]/thread/VDCU2WOB6KQISREO4V5DTXEI2M7VOV55/

Am just going to pick up Vincent's original patch. Then we can do any subsequent cleanup in a separate patch.

- Paul