of/fdt: add dt_phys arg to early_init_dt_scan and early_init_dt_verify
Subsystems (18)
PATCH v2 1/1 of/fdt: add dt_phys arg to early_init_dt_scan and early_init_dt_verify
__pa() is only intended to be used for linear map addresses and using
it for initial_boot_params which is in fixmap for arm64 will give an incorrect value. Hence save the physical address when it is known at boot time when calling early_init_dt_scan for arm64 and use it at kexec time instead of converting the virtual address using __pa().
---
v1 -> 2:
- pass dt_phys in early_init_dt_scan instead of creating anorther arch->dt function (Rob Herring)
--- arch/arc/kernel/devtree.c | 2 +- arch/arm/kernel/devtree.c | 2 +- arch/arm64/kernel/setup.c | 6 +++++- arch/csky/kernel/setup.c | 4 ++-- arch/loongarch/kernel/setup.c | 2 +- arch/microblaze/kernel/prom.c | 2 +- arch/mips/kernel/prom.c | 2 +- arch/mips/kernel/relocate.c | 2 +- arch/nios2/kernel/prom.c | 4 ++-- arch/openrisc/kernel/prom.c | 2 +- arch/powerpc/kernel/dt_cpu_ftrs.c | 2 +- arch/powerpc/kernel/prom.c | 2 +- arch/powerpc/platforms/pseries/plpks.c | 2 +- arch/riscv/kernel/setup.c | 2 +- arch/sh/kernel/setup.c | 2 +- arch/um/kernel/dtb.c | 2 +- arch/x86/kernel/devicetree.c | 2 +- arch/xtensa/kernel/setup.c | 2 +- drivers/of/fdt.c | 14 ++++++++------ drivers/of/kexec.c | 2 +- include/linux/of_fdt.h | 5 +++-- 21 files changed, 36 insertions(+), 29 deletions(-)
arch/arc/kernel/devtree.c +1 -1
@@ -62,7 +62,7 @@ const struct machine_desc * __init setup_machine_fdt(void *dt)const struct machine_desc *mdesc;unsigned long dt_root; -if (!early_init_dt_scan (dt)) +if (!early_init_dt_scan (dt,__pa (dt)))return NULL ; mdesc =of_flat_dt_match_machine (NULL , arch_get_next_mach);
arch/arm/kernel/devtree.c +1 -1
@@ -200,7 +200,7 @@ const struct machine_desc * __init setup_machine_fdt(void *dt_virt) mdesc_best = &__mach_desc_GENERIC_DT; -if (!dt_virt || !early_init_dt_verify (dt_virt)) +if (!dt_virt || !early_init_dt_verify (dt_virt,__pa (dt_virt)))return NULL ; mdesc =of_flat_dt_match_machine (mdesc_best, arch_get_next_mach);
arch/arm64/kernel/setup.c +5 -1
@@ -175,7 +175,11 @@ static void __init setup_machine_fdt(phys_addr_t dt_phys)if (dt_virt)memblock_reserve (dt_phys, size); -if (!dt_virt || !early_init_dt_scan (dt_virt)) { +/* +* dt_virt is a fixmap address, hence __pa(dt_virt) can't be used. +* Pass dt_phys directly. +*/ +if (!dt_virt || !early_init_dt_scan (dt_virt, dt_phys)) {pr_crit ("\n" "Error: invalid device tree blob at physical address %pa (virtual address 0x%px)\n" "The dtb must be 8-byte aligned and must not exceed 2 MB in size\n"
arch/csky/kernel/setup.c +2 -2
@@ -112,9 +112,9 @@ asmlinkage __visible void __init csky_start(unsigned int unused,pre_trap_init ();if (dtb_start ==NULL ) -early_init_dt_scan (__dtb_start); +early_init_dt_scan (__dtb_start,__pa (dtb_start));else -early_init_dt_scan (dtb_start); +early_init_dt_scan (dtb_start,__pa (dtb_start));start_kernel ();
arch/loongarch/kernel/setup.c +1 -1
@@ -290,7 +290,7 @@ static void __init fdt_setup(void)if (!fdt_pointer ||fdt_check_header (fdt_pointer))return ; -early_init_dt_scan (fdt_pointer); +early_init_dt_scan (fdt_pointer,__pa (fdt_pointer));early_init_fdt_reserve_self (); max_low_pfn =PFN_PHYS (memblock_end_of_DRAM ());
arch/microblaze/kernel/prom.c +1 -1
@@ -18,7 +18,7 @@ void __init early_init_devtree(void *params) {pr_debug (" -> early_init_devtree(%p)\n" , params); -early_init_dt_scan (params); +early_init_dt_scan (params,__pa (params));if (!strlen (boot_command_line))strscpy (boot_command_line, cmd_line,COMMAND_LINE_SIZE );
arch/mips/kernel/prom.c +1 -1
@@ -41,7 +41,7 @@ char *mips_get_machine_name(void)void __init __dt_setup_arch (void *bph) { -if (!early_init_dt_scan (bph)) +if (!early_init_dt_scan (bph,__pa (bph)))return ;mips_set_machine_name (of_flat_dt_get_machine_name ());
arch/mips/kernel/relocate.c +1 -1
@@ -337,7 +337,7 @@ void *__init relocate_kernel(void)#if defined(CONFIG_USE_OF )/* Deal with the device tree */ fdt =plat_get_fdt (); -early_init_dt_scan (fdt); +early_init_dt_scan (fdt,__pa (fdt));if (boot_command_line[0 ]) {/* Boot command line was passed in device tree */ strscpy (arcs_cmdline, boot_command_line,COMMAND_LINE_SIZE );
arch/nios2/kernel/prom.c +2 -2
@@ -27,7 +27,7 @@ void __init early_init_devtree(void *params)if (be32_to_cpup ((__be32 *)CONFIG_NIOS2_DTB_PHYS_ADDR ) ==OF_DT_HEADER ) { params = (void *)CONFIG_NIOS2_DTB_PHYS_ADDR ; -early_init_dt_scan (params); +early_init_dt_scan (params,__pa (params));return ; }#endif
@@ -37,5 +37,5 @@ void __init early_init_devtree(void *params) params = (void *)__dtb_start;#endif -early_init_dt_scan (params); +early_init_dt_scan (params,__pa (params)); }
arch/openrisc/kernel/prom.c +1 -1
@@ -22,6 +22,6 @@void __init early_init_devtree (void *params) { -early_init_dt_scan (params); +early_init_dt_scan (params,__pa (params));memblock_allow_resize (); }
arch/powerpc/kernel/dt_cpu_ftrs.c +1 -1
@@ -867,7 +867,7 @@ bool __init dt_cpu_ftrs_init(void *fdt) using_dt_cpu_ftrs = false;/* Setup and verify the FDT, if it fails we just bail */ -if (!early_init_dt_verify (fdt)) +if (!early_init_dt_verify (fdt,__pa (fdt)))return false;if (!of_scan_flat_dt (fdt_find_cpu_features,NULL ))
arch/powerpc/kernel/prom.c +1 -1
@@ -791,7 +791,7 @@ void __init early_init_devtree(void *params)DBG (" -> early_init_devtree(%px)\n" , params);/* Too early to BUG_ON(), do it by hand */ -if (!early_init_dt_verify (params)) +if (!early_init_dt_verify (params,__pa (params)))panic ("BUG: Failed verifying flat device tree, bad version?" );of_scan_flat_dt (early_init_dt_scan_model,NULL );
arch/powerpc/platforms/pseries/plpks.c +1 -1
@@ -683,7 +683,7 @@ void __init plpks_early_init_devtree(void) out:fdt_nop_property (fdt, chosen_node,"ibm,plpks-pw" );// Since we've cleared the password, we must update the FDT checksum -early_init_dt_verify (fdt); +early_init_dt_verify (fdt,__pa (fdt)); }static __init int pseries_plpks_init (void )
arch/riscv/kernel/setup.c +1 -1
@@ -227,7 +227,7 @@ static void __init init_resources(void)static void __init parse_dtb (void ) {/* Early scan of device tree from init memory */ -if (early_init_dt_scan (dtb_early_va)) { +if (early_init_dt_scan (dtb_early_va,__pa (dtb_early_va))) {const char *name =of_flat_dt_get_machine_name ();if (name) {
arch/sh/kernel/setup.c +1 -1
@@ -255,7 +255,7 @@ void __ref sh_fdt_init(phys_addr_t dt_phys) dt_virt =phys_to_virt (dt_phys);#endif -if (!dt_virt || !early_init_dt_scan (dt_virt)) { +if (!dt_virt || !early_init_dt_scan (dt_virt,__pa (dt_virt))) {pr_crit ("Error: invalid device tree blob" " at physical address %p\n" , (void *)dt_phys);
arch/um/kernel/dtb.c +1 -1
@@ -17,7 +17,7 @@ void uml_dtb_init(void) area =uml_load_file (dtb, &size);if (area) { -if (!early_init_dt_scan (area)) { +if (!early_init_dt_scan (area,__pa (area))) {pr_err ("invalid DTB %s\n" , dtb);memblock_free (area, size);return ;
arch/x86/kernel/devicetree.c +1 -1
@@ -305,7 +305,7 @@ void __init x86_flattree_get_config(void) map_len = size; } -early_init_dt_verify (dt); +early_init_dt_verify (dt,__pa (dt)); }unflatten_and_copy_device_tree ();
arch/xtensa/kernel/setup.c +1 -1
@@ -216,7 +216,7 @@ static int __init xtensa_dt_io_area(unsigned long node, const char *uname,void __init early_init_devtree (void *params) { -early_init_dt_scan (params); +early_init_dt_scan (params,__pa (params));of_scan_flat_dt (xtensa_dt_io_area,NULL );if (!command_line[0 ])
drivers/of/fdt.c +8 -6
@@ -457,6 +457,7 @@ int __initdata dt_root_addr_cells;int __initdata dt_root_size_cells;void *initial_boot_params__ro_after_init ; +phys_addr_t initial_boot_params_pa__ro_after_init ;#ifdef CONFIG_OF_EARLY_FLATTREE
@@ -1136,17 +1137,18 @@ static void * __init early_init_dt_alloc_memory_arch(u64 size, u64 align)return ptr; } -bool __init early_init_dt_verify (void *params) +bool __init early_init_dt_verify (void *dt_virt,phys_addr_t dt_phys) { -if (!params) +if (!dt_virt)return false;/* check device tree validity */ -if (fdt_check_header (params)) +if (fdt_check_header (dt_virt))return false;/* Setup flat device-tree pointer */ - initial_boot_params = params; + initial_boot_params = dt_virt; + initial_boot_params_pa = dt_phys; of_fdt_crc32 =crc32_be (~0 , initial_boot_params,fdt_totalsize (initial_boot_params));
@@ -1173,11 +1175,11 @@ void __init early_init_dt_scan_nodes(void)early_init_dt_check_for_usable_mem_range (); } -bool __init early_init_dt_scan (void *params) +bool __init early_init_dt_scan (void *dt_virt,phys_addr_t dt_phys) {bool status; - status =early_init_dt_verify (params); + status =early_init_dt_verify (dt_virt, dt_phys);if (!status)return false;
drivers/of/kexec.c +1 -1
@@ -301,7 +301,7 @@ void *of_kexec_alloc_and_setup_fdt(const struct kimage *image, }/* Remove memory reservation for the current device tree. */ - ret =fdt_find_and_del_mem_rsv (fdt,__pa (initial_boot_params), + ret =fdt_find_and_del_mem_rsv (fdt, initial_boot_params_pa,fdt_totalsize (initial_boot_params));if (ret == -EINVAL ) {pr_err ("Error removing memory reservation.\n" );
include/linux/of_fdt.h +3 -2
@@ -31,6 +31,7 @@ extern void *of_fdt_unflatten_tree(const unsigned long *blob,extern int __initdata dt_root_addr_cells;extern int __initdata dt_root_size_cells;extern void *initial_boot_params; +extern phys_addr_t initial_boot_params_pa;extern char __dtb_start[];extern char __dtb_end[];
@@ -70,8 +71,8 @@ extern u64 dt_mem_next_cell(int s, const __be32 **cellp);/* Early flat tree scan hooks */ extern int early_init_dt_scan_root (void ); -extern bool early_init_dt_scan (void *params); -extern bool early_init_dt_verify (void *params); +extern bool early_init_dt_scan (void *dt_virt,phys_addr_t dt_phys); +extern bool early_init_dt_verify (void *dt_virt,phys_addr_t dt_phys);extern void early_init_dt_scan_nodes (void );extern const char *of_flat_dt_get_machine_name (void );
Signature · 1 line
2.43.5
Rob Herring
maintainerQuoted 11 lines · Usama Arif
__pa()is only intended to be used for linear map addresses and using
it forinitial_boot_paramswhich is in fixmap for arm64 will give an incorrect value. Hence save the physical address when it is known at boot time when callingearly_init_dt_scanfor arm64 and use it at kexec time instead of converting the virtual address using__pa().Reported-by Breno Leitao <[email protected]>Suggested-by Mark Rutland <[email protected]>Signed-off-by Usama Arif <[email protected]>Fixes ac10be5cdbfa ("arm64: Use common of_kexec_alloc_and_setup_fdt()")
This looks fine, but what is the symptom without this compared to before the above change? The original code in the referenced commit above didn't remove the reservation at all. Unless the current code does something worse, this is new functionality more than a fix (for stable).
Rob
Quoted 13 lines · Rob Herring
Quoted 11 lines · Usama Arif
__pa()is only intended to be used for linear map addresses and using
it forinitial_boot_paramswhich is in fixmap for arm64 will give an incorrect value. Hence save the physical address when it is known at boot time when callingearly_init_dt_scanfor arm64 and use it at kexec time instead of converting the virtual address using__pa().Reported-by Breno Leitao <[email protected]>Suggested-by Mark Rutland <[email protected]>Signed-off-by Usama Arif <[email protected]>Fixes ac10be5cdbfa ("arm64: Use common of_kexec_alloc_and_setup_fdt()")
Quoted 7 lines
This looks fine, but what is the symptom without this compared to before the above change? The original code in the referenced commit above didn't remove the reservation at all. Unless the current code does something worse, this is new functionality more than a fix (for stable).
Rob
After the series in [1] was merged, we always get a warning when kexecing a debug kernel, which was reported by Breno in [2].
The issue is using __pa for a fixmap address in arm64 as described in [2], which could result in removing a memory reservation for a completely unrelated area.
That was introduced by the patch just before
"arm64: Use common of_kexec_alloc_and_setup_fdt" [3], but arm64 switched to using the common kexec fdt function in that commit. This commit is trying to fix removing and corrupting any random memory reservation (and get rid of the warning) that was introduced by [1], not adding a new functionality.
[1] https://lore.kernel.org/all/[email protected]/
[2] https://lore.kernel.org/all/[email protected]/
[3] https://lore.kernel.org/all/[email protected]/
Thanks,
Usama
Will Deacon
maintainerQuoted 69 lines · Usama Arif
__pa()is only intended to be used for linear map addresses and using
it forinitial_boot_paramswhich is in fixmap for arm64 will give an incorrect value. Hence save the physical address when it is known at boot time when callingearly_init_dt_scanfor arm64 and use it at kexec time instead of converting the virtual address using__pa().Reported-by Breno Leitao <[email protected]>Suggested-by Mark Rutland <[email protected]>Signed-off-by Usama Arif <[email protected]>Fixes ac10be5cdbfa ("arm64: Use common of_kexec_alloc_and_setup_fdt()")---
v1 -> 2:
- passdt_physinearly_init_dt_scaninstead of creating anortherarch->dtfunction (Rob Herring)--- arch/arc/kernel/devtree.c | 2 +- arch/arm/kernel/devtree.c | 2 +- arch/arm64/kernel/setup.c | 6 +++++- arch/csky/kernel/setup.c | 4 ++-- arch/loongarch/kernel/setup.c | 2 +- arch/microblaze/kernel/prom.c | 2 +- arch/mips/kernel/prom.c | 2 +- arch/mips/kernel/relocate.c | 2 +- arch/nios2/kernel/prom.c | 4 ++-- arch/openrisc/kernel/prom.c | 2 +- arch/powerpc/kernel/dt_cpu_ftrs.c | 2 +- arch/powerpc/kernel/prom.c | 2 +- arch/powerpc/platforms/pseries/plpks.c | 2 +- arch/riscv/kernel/setup.c | 2 +- arch/sh/kernel/setup.c | 2 +- arch/um/kernel/dtb.c | 2 +- arch/x86/kernel/devicetree.c | 2 +- arch/xtensa/kernel/setup.c | 2 +- drivers/of/fdt.c | 14 ++++++++------ drivers/of/kexec.c | 2 +- include/linux/of_fdt.h | 5 +++-- 21 files changed, 36 insertions(+), 29 deletions(-)arch/arc/kernel/devtree.c +1
-1@@ -62,7 +62,7 @@ const struct machine_desc * __init setup_machine_fdt(void *dt)const struct machine_desc *mdesc;unsigned long dt_root; -if (!early_init_dt_scan (dt)) +if (!early_init_dt_scan (dt,__pa (dt)))return NULL ; mdesc =of_flat_dt_match_machine (NULL , arch_get_next_mach);arch/arm/kernel/devtree.c +1
-1@@ -200,7 +200,7 @@ const struct machine_desc * __init setup_machine_fdt(void *dt_virt) mdesc_best = &__mach_desc_GENERIC_DT; -if (!dt_virt || !early_init_dt_verify (dt_virt)) +if (!dt_virt || !early_init_dt_verify (dt_virt,__pa (dt_virt)))return NULL ; mdesc =of_flat_dt_match_machine (mdesc_best, arch_get_next_mach);arch/arm64/kernel/setup.c +0
-0
arch/arm64/kernel/setup.c static void __init setup_machine_fdt(phys_addr_t dt_phys) · line 182
Quoted 9 lines
arch/arm64/kernel/setup.c +5
-1@@ -175,7 +175,11 @@ static void __init setup_machine_fdt(phys_addr_t dt_phys)if (dt_virt)memblock_reserve (dt_phys, size); -if (!dt_virt || !early_init_dt_scan (dt_virt)) { +/* +* dt_virt is a fixmap address, hence __pa(dt_virt) can't be used. +* Pass dt_phys directly. +*/ +if (!dt_virt || !early_init_dt_scan (dt_virt, dt_phys)) {
nit: It looks like early_init_dt_verify() will now return false if !dt_virt, so we can drop the additional check here.
Will
Rob Herring
maintainerQuoted 74 lines · Will Deacon
Quoted 72 lines · Usama Arif
__pa()is only intended to be used for linear map addresses and using
it forinitial_boot_paramswhich is in fixmap for arm64 will give an incorrect value. Hence save the physical address when it is known at boot time when callingearly_init_dt_scanfor arm64 and use it at kexec time instead of converting the virtual address using__pa().Reported-by Breno Leitao <[email protected]>Suggested-by Mark Rutland <[email protected]>Signed-off-by Usama Arif <[email protected]>Fixes ac10be5cdbfa ("arm64: Use common of_kexec_alloc_and_setup_fdt()")---
v1 -> 2:
- passdt_physinearly_init_dt_scaninstead of creating anortherarch->dtfunction (Rob Herring)--- arch/arc/kernel/devtree.c | 2 +- arch/arm/kernel/devtree.c | 2 +- arch/arm64/kernel/setup.c | 6 +++++- arch/csky/kernel/setup.c | 4 ++-- arch/loongarch/kernel/setup.c | 2 +- arch/microblaze/kernel/prom.c | 2 +- arch/mips/kernel/prom.c | 2 +- arch/mips/kernel/relocate.c | 2 +- arch/nios2/kernel/prom.c | 4 ++-- arch/openrisc/kernel/prom.c | 2 +- arch/powerpc/kernel/dt_cpu_ftrs.c | 2 +- arch/powerpc/kernel/prom.c | 2 +- arch/powerpc/platforms/pseries/plpks.c | 2 +- arch/riscv/kernel/setup.c | 2 +- arch/sh/kernel/setup.c | 2 +- arch/um/kernel/dtb.c | 2 +- arch/x86/kernel/devicetree.c | 2 +- arch/xtensa/kernel/setup.c | 2 +- drivers/of/fdt.c | 14 ++++++++------ drivers/of/kexec.c | 2 +- include/linux/of_fdt.h | 5 +++-- 21 files changed, 36 insertions(+), 29 deletions(-)arch/arc/kernel/devtree.c +1
-1@@ -62,7 +62,7 @@ const struct machine_desc * __init setup_machine_fdt(void *dt)const struct machine_desc *mdesc;unsigned long dt_root; -if (!early_init_dt_scan (dt)) +if (!early_init_dt_scan (dt,__pa (dt)))return NULL ; mdesc =of_flat_dt_match_machine (NULL , arch_get_next_mach);arch/arm/kernel/devtree.c +1
-1@@ -200,7 +200,7 @@ const struct machine_desc * __init setup_machine_fdt(void *dt_virt) mdesc_best = &__mach_desc_GENERIC_DT; -if (!dt_virt || !early_init_dt_verify (dt_virt)) +if (!dt_virt || !early_init_dt_verify (dt_virt,__pa (dt_virt)))return NULL ; mdesc =of_flat_dt_match_machine (mdesc_best, arch_get_next_mach);arch/arm64/kernel/setup.c +0
-0@@ -175,7 +175,11 @@ static void __init setup_machine_fdt(phys_addr_t dt_phys)if (dt_virt)memblock_reserve (dt_phys, size);
Quoted 9 lines
arch/arm64/kernel/setup.c static void __init setup_machine_fdt(phys_addr_t dt_phys) · line 182
Quoted 6 lines
arch/arm64/kernel/setup.c +5
-1-if (!dt_virt || !early_init_dt_scan (dt_virt)) { +/* +* dt_virt is a fixmap address, hence __pa(dt_virt) can't be used. +* Pass dt_phys directly. +*/ +if (!dt_virt || !early_init_dt_scan (dt_virt, dt_phys)) {nit: It looks like
early_init_dt_verify()will now return false if !dt_virt, so we can drop the additional check here.
That was true before, but I'll fix this up when applying.
Rob
This patch was applied to riscv/linux.git (fixes) by Rob Herring (Arm) <[email protected]>:
Hello:
This patch was applied to riscv/linux.git (fixes)
by Rob Herring (Arm) <[email protected]>:
Quoted 6 lines · On Wed, 23 Oct 2024 18:14:26 +0100 you wrote:
__pa()is only intended to be used for linear map addresses and using it forinitial_boot_paramswhich is in fixmap for arm64 will give an incorrect value. Hence save the physical address when it is known at boot time when callingearly_init_dt_scanfor arm64 and use it at kexec time instead of converting the virtual address using__pa().
Quoted 6 lines
Reported-by Breno Leitao <[email protected]>Suggested-by Mark Rutland <[email protected]>Signed-off-by Usama Arif <[email protected]>Fixes ac10be5cdbfa ("arm64: Use common of_kexec_alloc_and_setup_fdt()")[...]
Here is the summary with links:
- [v2] of/fdt: add dt_phys arg to early_init_dt_scan and early_init_dt_verify
https://git.kernel.org/riscv/c/b2473a359763
You are awesome, thank you!
Signature · 2 lines
Deet-doot-dot, I am a bot. https://korg.docs.kernel.org/patchwork/pwbot.html