[PATCH] arm64: clean up fixmap initalization

From: Mark Rutland
Date: Tue Feb 20 2024 - 06:09:17 EST


Currently we have redundant initialization of the fixmap, first in
early_fdt_map(), and then again in setup_arch() before we call
early_ioremap_init(). This redundant initialization isn't harmful, as
early_fixmap_init() happens to be idempotent, but it's redundant and
potentially confusing.

We need to call early_fixmap_init() before we map the FDT and before we
call early_ioremap_init(). Ideally we'd place early_fixmap_init() and
early_ioremap_init() in the same caller as early_ioremap_init() is
somewhat coupled with the fixmap code.

Clean this up by moving the calls to early_fixmap_init() and
early_ioremap_init() into a new early_mappings_init() function, and
calling this once in __primary_switched() before we call
early_fdt_map(). This means we initialize the fixmap once, and keep
early_fixmap_init() and early_ioremap_init() together.

This is a cleanup, not a fix, and does not need to be backported to
stable kernels.

Reported-by: Daero Lee <skseofh@xxxxxxxxx>
Signed-off-by: Mark Rutland <mark.rutland@xxxxxxx>
Cc: Catalin Marinas <catalin.marinas@xxxxxxx>
Cc: Itaru Kitayama <itaru.kitayama@xxxxxxxxx>
Cc: Will Deacon <will@xxxxxxxxxx>
---
arch/arm64/include/asm/setup.h | 1 +
arch/arm64/kernel/head.S | 2 ++
arch/arm64/kernel/setup.c | 11 ++++++-----
3 files changed, 9 insertions(+), 5 deletions(-)

diff --git a/arch/arm64/include/asm/setup.h b/arch/arm64/include/asm/setup.h
index 2e4d7da74fb87..c8ba018bcc09f 100644
--- a/arch/arm64/include/asm/setup.h
+++ b/arch/arm64/include/asm/setup.h
@@ -9,6 +9,7 @@

void *get_early_fdt_ptr(void);
void early_fdt_map(u64 dt_phys);
+void early_mappings_init(void);

/*
* These two variables are used in the head.S file.
diff --git a/arch/arm64/kernel/head.S b/arch/arm64/kernel/head.S
index cab7f91949d8f..c60c5454c5704 100644
--- a/arch/arm64/kernel/head.S
+++ b/arch/arm64/kernel/head.S
@@ -510,6 +510,8 @@ SYM_FUNC_START_LOCAL(__primary_switched)
#if defined(CONFIG_KASAN_GENERIC) || defined(CONFIG_KASAN_SW_TAGS)
bl kasan_early_init
#endif
+ bl early_mappings_init
+
mov x0, x21 // pass FDT address in x0
bl early_fdt_map // Try mapping the FDT early
mov x0, x20 // pass the full boot status
diff --git a/arch/arm64/kernel/setup.c b/arch/arm64/kernel/setup.c
index 42c690bb2d608..7a539534ced78 100644
--- a/arch/arm64/kernel/setup.c
+++ b/arch/arm64/kernel/setup.c
@@ -176,8 +176,6 @@ void __init *get_early_fdt_ptr(void)
asmlinkage void __init early_fdt_map(u64 dt_phys)
{
int fdt_size;
-
- early_fixmap_init();
early_fdt_ptr = fixmap_remap_fdt(dt_phys, &fdt_size, PAGE_KERNEL);
}

@@ -290,6 +288,12 @@ u64 cpu_logical_map(unsigned int cpu)
return __cpu_logical_map[cpu];
}

+asmlinkage void __init early_mappings_init(void)
+{
+ early_fixmap_init();
+ early_ioremap_init();
+}
+
void __init __no_sanitize_address setup_arch(char **cmdline_p)
{
setup_initial_init_mm(_stext, _etext, _edata, _end);
@@ -305,9 +309,6 @@ void __init __no_sanitize_address setup_arch(char **cmdline_p)
*/
arm64_use_ng_mappings = kaslr_requires_kpti();

- early_fixmap_init();
- early_ioremap_init();
-
setup_machine_fdt(__fdt_pointer);

/*
--
2.30.2