riscv: correct the do_trap_break()
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
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
@@ -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
Re: [PATCH 1/4] riscv: avoid kernel hangs when trapped in BUG()
Quoted 8 lines · Vincent Chen
When the
CONFIG_GENERIC_BUGis disabled by disablingCONFIG_BUG, if a kernel thread is trapped byBUG(), the whole system will be in the loop that infinitely handles the ebreak exception instead of entering the die function. To fix this problem, thedo_trap_break()will always call thedie()to deal with the break exception as the type of break isBUG_TRAP_TYPE_BUG.Signed-off-by Vincent Chen <[email protected]>
Looks good,
Re: [PATCH 1/4] riscv: avoid kernel hangs when trapped in BUG()
In reply to Vincent ChenQuoted 8 lines · Vincent Chen
When the
CONFIG_GENERIC_BUGis disabled by disablingCONFIG_BUG, if a kernel thread is trapped byBUG(), the whole system will be in the loop that infinitely handles the ebreak exception instead of entering the die function. To fix this problem, thedo_trap_break()will always call thedie()to deal with the break exception as the type of break isBUG_TRAP_TYPE_BUG.Signed-off-by Vincent Chen <[email protected]>
Thanks, queued for v5.4-rc.
- Paul
PATCH 2/4 rsicv: avoid sending a SIGTRAP to a user thread trapped in WARN()
In reply to Vincent ChenOn 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
@@ -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
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()orWARN_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.Signed-off-by Vincent Chen <[email protected]>
Looks good,
Re: [PATCH 2/4] rsicv: avoid sending a SIGTRAP to a user thread trapped in WARN()
In reply to Vincent ChenOh and s/rsicv/riscv/ in the subject, please.
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.
Re: [PATCH 2/4] rsicv: avoid sending a SIGTRAP to a user thread trapped in WARN()
In reply to Vincent ChenQuoted 6 lines · Vincent Chen
On RISC-V, when the kernel runs code on behalf of a user thread, and the kernel executes a
WARN()orWARN_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.Signed-off-by Vincent Chen <[email protected]>
Thanks, queued for v5.4-rc.
- Paul
PATCH 3/4 riscv: Correct the handling of unexpected ebreak in do_trap_break()
In reply to Vincent ChenFor 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
@@ -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
Re: [PATCH 3/4] riscv: Correct the handling of unexpected ebreak in do_trap_break()
Looks ok:
_______________________________________________
linux-riscv mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-riscv
Re: [PATCH 3/4] riscv: Correct the handling of unexpected ebreak in do_trap_break()
In reply to Vincent ChenQuoted 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_WARNorBUG_TRAP_TYPE_BUGis 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, thedo_trap_break()will direct theBUG_TRAP_TYPE_NONEebreak exception issued in kernel space todie()and will send a SIGTRAP to the trapped process only when the ebreak is in userspace.
Quoted 1 line
Signed-off-by Vincent Chen <[email protected]>
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
@@ -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
PATCH 4/4 riscv: remove the switch statement in do_trap_break()
In reply to Vincent ChenTo 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
@@ -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
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.
Suggested-by Paul Walmsley <[email protected]>Signed-off-by Vincent Chen <[email protected]>--- arch/riscv/kernel/traps.c | 23 ++++++++++++----------- 1 file changed, 12 insertions(+), 11 deletions(-)arch/riscv/kernel/traps.c +7
-0@@ -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
-0enum 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
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.
Suggested-by Paul Walmsley <[email protected]>Signed-off-by Vincent Chen <[email protected]>
...
Quoted 4 lines
I like where this is going, but I think this can be improved further given that fact that
report_bughas 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
@@ -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
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
-0force_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
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
-0force_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
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
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
Re: [PATCH 4/4] riscv: remove the switch statement in do_trap_break()
In reply to Christoph HellwigQuoted 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
-0force_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