Thread (32 messages) 32 messages, 4 authors, 2023-02-07

Re: [PATCH 05/22] csky/cpu: Make sure arch_cpu_idle_dead() doesn't return

From: Guo Ren <guoren@kernel.org>
Date: 2023-02-06 03:12:12
Also in: linux-alpha, linux-arm-kernel, linux-mips, linux-sh, lkml, loongarch, sparclinux

On Sat, Feb 4, 2023 at 10:29 AM Josh Poimboeuf [off-list ref] wrote:
On Sat, Feb 04, 2023 at 09:12:31AM +0800, Guo Ren wrote:
quoted
On Sat, Feb 4, 2023 at 6:05 AM Josh Poimboeuf [off-list ref] wrote:
quoted
arch_cpu_idle_dead() doesn't return.  Make that more explicit with a
BUG().

BUG() is preferable to unreachable() because BUG() is a more explicit
failure mode and avoids undefined behavior like falling off the edge of
the function into whatever code happens to be next.

Signed-off-by: Josh Poimboeuf <jpoimboe@kernel.org>
---
 arch/csky/kernel/smp.c | 2 ++
 1 file changed, 2 insertions(+)
diff --git a/arch/csky/kernel/smp.c b/arch/csky/kernel/smp.c
index b45d1073307f..0ec20efaf5fd 100644
--- a/arch/csky/kernel/smp.c
+++ b/arch/csky/kernel/smp.c
@@ -317,5 +317,7 @@ void arch_cpu_idle_dead(void)
                "jmpi   csky_start_secondary"
                :
                : "r" (secondary_stack));
+
+       BUG();
Why not:
diff --git a/kernel/sched/idle.c b/kernel/sched/idle.c
index f26ab2675f7d..1d3bf903add2 100644
--- a/kernel/sched/idle.c
+++ b/kernel/sched/idle.c
@@ -285,6 +285,7 @@ static void do_idle(void)
                        tick_nohz_idle_stop_tick();
                        cpuhp_report_idle_dead();
                        arch_cpu_idle_dead();
+                       BUG();
Without the BUG() in csky arch_cpu_idle_dead(), the compiler will warn
about arch_cpu_idle_dead() returning, because it's marked __noreturn but
doesn't clearly return (as far as the compiler knows).

And we want it marked __noreturn so we'll be more likely to catch such
bugs at build time.

And as a bonus we get better code generation and clearer code semantics
which helps both humans and tooling understand the intent of the code.
Thx for the clarification.

Acked-by: Guo Ren <guoren@kernel.org>
--
Josh


-- 
Best Regards
 Guo Ren
Keyboard shortcuts
hback out one level
jnext message in thread
kprevious message in thread
ldrill in
Escclose help / fold thread tree
?toggle this help