[patch 00/17] init, treewide, x86: Cleanup check_bugs() and start sanitizing the x86 boot process

Thomas Gleixner
2023-06-13T23:39:29Z

Hi!

My team and myself are working on sanitizing the x86 boot process,
especially the complete horror show of CPUID evaluation, which is
constructed with hay-wire circuits, duct tape and superglue.

A related goal is to move the initialization of infrastructure which is not
required during early boot out into a later phase of the boot process.
Early boot is fragile and convoluted enough already, so anything which can
move into a later phase is a win.

X86 FPU initialization is one of the obvious parts which has zero
justification to be done early. The only requirement is that it happens
before alternative patching. Doing it early also requires custom command
line parsing which can be obviously avoided when the initialization happens
later.

Alternative patching happens from check_bugs() which is invoked late in
start_kernel(). Moving FPU initialization into that is too late because
check_bugs() is invoked after fork_init(), but fork_init() requires that
the FPU is initialized on X86 as on X86 the size of task_struct depends on
the FPU register buffer size.

In order to avoid another magic function we set out to move check_bugs()
earlier and inspected all incarnations whether there is any reason to do
that so late. It turned out there is none (famous last words), but it also
revealed that check_bugs() is a gross misnomer.

check_bugs() has become a dump ground for finalizing the CPU initialization
before running the rest of the init code.

Most implementations are empty, a few do actual bug checks, some do
alternative patching and one cobbles a CPU advertisment string together...

As a consequence we decided to rename it to arch_cpu_finalize_init(). The
purely mechanical 's/check_bugs/arch_cpu_finalize_init/' would have been
trivial, but having stared at the actual implementations of check_bugs()
had triggered the scavenger reflex already. So I got the mop out and
cleaned it up completely.

The resulting series consists therefore of three parts:

1) Patches 1-11

Rename and mop up check_bugs() which removes a solid amount of
redundant historical copy & pasta crud:

39 files changed, 161 insertions(+), 321 deletions(-)

11 out of the 39 changed files are removed completely.

2) Patches 12-13

Move the invocation of arch_cpu_finalize_init() earlier in
start_kernel() and move the x86'ism mem_encrypt_init() into the x86
space.

3) Patches 14-17

Implement the late FPU initialization for X86 on top. Removal of the
custom early command line parsing is subject to separate x86 specific
changes. This part #3 is to illustrate the use case for #2.

The series applies on Linus tree and is also available from git:

git://git.kernel.org/pub/scm/linux/kernel/git/tglx/devel.git init

Thanks,

tglx
---
a/arch/alpha/include/asm/bugs.h | 20 ---------
a/arch/ia64/include/asm/bugs.h | 20 ---------
a/arch/loongarch/include/asm/bugs.h | 15 ------
a/arch/m68k/include/asm/bugs.h | 21 ---------
a/arch/parisc/include/asm/bugs.h | 20 ---------
a/arch/powerpc/include/asm/bugs.h | 15 ------
a/arch/sh/include/asm/bugs.h | 74 ---------------------------------
a/arch/sparc/include/asm/bugs.h | 18 --------
a/arch/um/include/asm/bugs.h | 7 ---
a/arch/xtensa/include/asm/bugs.h | 18 --------
a/include/asm-generic/bugs.h | 11 -----
arch/Kconfig | 3 +
arch/arm/Kconfig | 1
arch/arm/include/asm/bugs.h | 4 -
arch/arm/kernel/bugs.c | 3 -
arch/ia64/Kconfig | 1
arch/ia64/kernel/setup.c | 3 -
arch/loongarch/Kconfig | 1
arch/loongarch/kernel/setup.c | 4 -
arch/m68k/Kconfig | 1
arch/m68k/kernel/setup_mm.c | 3 -
arch/mips/Kconfig | 1
arch/mips/include/asm/bugs.h | 17 -------
arch/mips/kernel/setup.c | 13 +++++
arch/sh/Kconfig | 1
arch/sh/include/asm/processor.h | 2
arch/sh/kernel/idle.c | 1
arch/sh/kernel/setup.c | 55 +++++++++++++++++++++++++
arch/sparc/Kconfig | 1
arch/sparc/kernel/setup_32.c | 7 +++
arch/um/Kconfig | 1
arch/um/kernel/um_arch.c | 3 -
arch/x86/Kconfig | 1
arch/x86/include/asm/bugs.h | 2
arch/x86/include/asm/fpu/api.h | 2
arch/x86/include/asm/mem_encrypt.h | 7 +--
arch/x86/include/asm/sigframe.h | 2
arch/x86/kernel/cpu/bugs.c | 51 -----------------------
arch/x86/kernel/cpu/common.c | 79 ++++++++++++++++++++++++++++++++----
arch/x86/kernel/cpu/cpu.h | 1
arch/x86/kernel/fpu/init.c | 8 +--
arch/x86/kernel/signal.c | 4 +
include/linux/cpu.h | 6 ++
init/main.c | 16 -------
44 files changed, 192 insertions(+), 352 deletions(-)