[PATCH] sched: Work around undefined behavior in sched class checking

From: Andi Kleen
Date: Tue May 04 2021 - 23:39:58 EST


From: Andi Kleen <andi@xxxxxxxxxxxxxx>

The scheduler initialization code checks that the scheduling
classes are consecutive in memory by comparing the end
addresses with the next address.

Technically in ISO C comparing symbol addresseses outside different objects
is undefined. With LTO gcc 10 tries to exploits this and creates an
unconditional BUG_ON in the scheduler initialization, resulting
in a boot hang.

Use RELOC_HIDE to make this work. This hides the symbols from gcc,
so the optimizer won't make these assumption. I also split
the BUG_ONs in multiple.

Signed-off-by: Andi Kleen <andi@xxxxxxxxxxxxxx>
---
kernel/sched/core.c | 18 +++++++++++++-----
1 file changed, 13 insertions(+), 5 deletions(-)

diff --git a/kernel/sched/core.c b/kernel/sched/core.c
index 98191218d891..272a654f5293 100644
--- a/kernel/sched/core.c
+++ b/kernel/sched/core.c
@@ -8086,12 +8086,20 @@ void __init sched_init(void)
unsigned long ptr = 0;
int i;

- /* Make sure the linker didn't screw up */
- BUG_ON(&idle_sched_class + 1 != &fair_sched_class ||
- &fair_sched_class + 1 != &rt_sched_class ||
- &rt_sched_class + 1 != &dl_sched_class);
+ /*
+ * Make sure the linker didn't screw up.
+ * We have to use RELOC_HIDE to prevent gcc from optimizing
+ * them to true because they're technically undefined in ISO-C.
+ */
+ BUG_ON(RELOC_HIDE(&idle_sched_class, 0) + 1 !=
+ RELOC_HIDE(&fair_sched_class, 0));
+ BUG_ON(RELOC_HIDE(&fair_sched_class, 0) + 1 !=
+ RELOC_HIDE(&rt_sched_class, 0));
+ BUG_ON(RELOC_HIDE(&rt_sched_class, 0) + 1 !=
+ RELOC_HIDE(&dl_sched_class, 0));
#ifdef CONFIG_SMP
- BUG_ON(&dl_sched_class + 1 != &stop_sched_class);
+ BUG_ON(RELOC_HIDE(&dl_sched_class, 0) + 1 !=
+ RELOC_HIDE(&stop_sched_class, 0));
#endif

wait_bit_init();
--
2.25.4