diff mbox series

[v1,2/2] powerpc: add support for Kexec HandOver (KHO)

Message ID 20260928083226.107807-3-sourabhjain@linux.ibm.com (mailing list archive)
State New
Headers show
Series powerpc: enable Kexec HandOver (KHO) | expand

Commit Message

Sourabh Jain Sept. 28, 2026, 8:32 a.m. UTC
Add the architecture bits needed to enable CONFIG_KEXEC_HANDOVER on
powerpc.

Set ARCH_SUPPORTS_KEXEC_HANDOVER for PPC64.

On the boot path, check for KHO information in the device tree before
unflatten_device_tree(). This allows a kernel booted via KHO kexec to
recover the FDT and scratch region left by the previous kernel.

Move early_init_dt_check_kho() out of the device tree implementation
and call it from setup_arch() before the device tree is unflattened.

Note:
Crashkernel reservation impacts KHO scratch reservation. Crashkernel
memory is reserved first using memblock, and this memory is included
when calculating the KHO scratch size. As a result, a large crashkernel
reservation can increase the KHO scratch size and may cause the KHO
scratch reservation to fail.

This issue also exists on other architectures and will be handled
separately.

Cc: Aditya Gupta <adityag@linux.ibm.com>
Cc: Alexander Graf <graf@amazon.com>
Cc: Andrew Morton <akpm@linux-foundation.org>
Cc: Baoquan He <baoquan.he@linux.dev>
Cc: Christophe Leroy (CS GROUP) <chleroy@kernel.org>
Cc: Hari Bathini <hbathini@linux.ibm.com>
Cc: Madhavan Srinivasan <maddy@linux.ibm.com>
Cc: Mahesh Salgaonkar <mahesh@linux.ibm.com>
Cc: Michael Ellerman <mpe@ellerman.id.au>
Cc: Mike Rapoport <rppt@kernel.org>
Cc: Nicholas Piggin <npiggin@gmail.com>
Cc: Pasha Tatashin <pasha.tatashin@soleen.com>
Cc: Pratyush Yadav <pratyush@kernel.org>
Cc: Ritesh Harjani (IBM) <ritesh.list@gmail.com>
Cc: Shivang Upadhyay <shivangu@linux.ibm.com>
Cc: Shrikanth Hegde <sshegde@linux.ibm.com>
Cc: linux-kernel@vger.kernel.org
Cc: linuxppc-dev@lists.ozlabs.org
Signed-off-by: Sourabh Jain <sourabhjain@linux.ibm.com>
---
 arch/powerpc/Kconfig               | 3 +++
 arch/powerpc/kernel/setup-common.c | 2 ++
 drivers/of/fdt.c                   | 2 +-
 include/linux/of_fdt.h             | 1 +
 4 files changed, 7 insertions(+), 1 deletion(-)

Comments

sashiko-bot@kernel.org Sept. 28, 2026, 8:43 a.m. UTC | #1
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [High] boot: late KHO initialization allows memblock to clobber preserved memory
--

commit 2f581dec390379323e7a0b4e4badb03487eea55f
Author: Sourabh Jain <sourabhjain@linux.ibm.com>

powerpc: add support for Kexec HandOver (KHO)

Add architecture bits to enable CONFIG_KEXEC_HANDOVER on powerpc. This
patch checks for KHO information in the device tree before unflattening it,
allowing a kernel booted via KHO kexec to recover the FDT and scratch region.
It also moves early_init_dt_check_kho() to be called from setup_arch().

> diff --git a/arch/powerpc/kernel/setup-common.c b/arch/powerpc/kernel/setup-common.c
> index 4afaba19b5867..53c2f1897bfe1 100644
> --- a/arch/powerpc/kernel/setup-common.c
> +++ b/arch/powerpc/kernel/setup-common.c
> @@ -923,6 +923,8 @@ void __init setup_arch(char **cmdline_p)
>  	/* Set a half-reasonable default so udelay does something sensible */
>  	loops_per_jiffy = 500000000 / HZ;
>  
> +	early_init_dt_check_kho();
> +
>  	/* Unflatten the device-tree passed by prom_init or kexec */
>  	unflatten_device_tree();

[Severity: High]
Does calling early_init_dt_check_kho() in setup_arch() happen too late to
protect against early memblock allocations clobbering preserved KHO memory?

The Kexec HandOver (KHO) mechanism uses a scratch memory region to strictly
confine early boot memory allocations in the successor kernel. This prevents
them from overwriting memory pages preserved by the previous kernel, and is
enforced when kho_populate() invokes memblock_set_kho_scratch_only().

However, setup_arch() is invoked from start_kernel() long after early_setup()
completes. During early_setup(), substantial dynamic memblock allocations occur
before KHO scratch memory confinement is enforced.

For example, move_device_tree() allocates memory for the FDT:

arch/powerpc/kernel/prom.c:move_device_tree() {
	...
	p = memblock_alloc_raw(size, PAGE_SIZE);
	...
}

This is called via early_init_devtree() from early_setup():

arch/powerpc/kernel/setup_64.c:early_setup() {
	...
	early_init_devtree(__va(dt_ptr));

	allocate_paca_ptrs();
	allocate_paca(boot_cpuid);
	... 
	early_init_mmu();
	...
}

Because KHO scratch-only mode has not yet been enforced during these early
allocations, could they use standard memblock policies and overwrite preserved
KHO memory state, corrupting the handover?
Pratyush Yadav Oct. 12, 2026, 2:43 p.m. UTC | #2
On Mon, Sep 28 2026, sashiko-bot@kernel.org wrote:

> Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
> - [High] boot: late KHO initialization allows memblock to clobber preserved memory
> --
>
> commit 2f581dec390379323e7a0b4e4badb03487eea55f
> Author: Sourabh Jain <sourabhjain@linux.ibm.com>
>
> powerpc: add support for Kexec HandOver (KHO)
>
> Add architecture bits to enable CONFIG_KEXEC_HANDOVER on powerpc. This
> patch checks for KHO information in the device tree before unflattening it,
> allowing a kernel booted via KHO kexec to recover the FDT and scratch region.
> It also moves early_init_dt_check_kho() to be called from setup_arch().
>
>> diff --git a/arch/powerpc/kernel/setup-common.c b/arch/powerpc/kernel/setup-common.c
>> index 4afaba19b5867..53c2f1897bfe1 100644
>> --- a/arch/powerpc/kernel/setup-common.c
>> +++ b/arch/powerpc/kernel/setup-common.c
>> @@ -923,6 +923,8 @@ void __init setup_arch(char **cmdline_p)
>>  	/* Set a half-reasonable default so udelay does something sensible */
>>  	loops_per_jiffy = 500000000 / HZ;
>>  
>> +	early_init_dt_check_kho();
>> +
>>  	/* Unflatten the device-tree passed by prom_init or kexec */
>>  	unflatten_device_tree();
>
> [Severity: High]
> Does calling early_init_dt_check_kho() in setup_arch() happen too late to
> protect against early memblock allocations clobbering preserved KHO memory?
>
> The Kexec HandOver (KHO) mechanism uses a scratch memory region to strictly
> confine early boot memory allocations in the successor kernel. This prevents
> them from overwriting memory pages preserved by the previous kernel, and is
> enforced when kho_populate() invokes memblock_set_kho_scratch_only().
>
> However, setup_arch() is invoked from start_kernel() long after early_setup()
> completes. During early_setup(), substantial dynamic memblock allocations occur
> before KHO scratch memory confinement is enforced.
>
> For example, move_device_tree() allocates memory for the FDT:
>
> arch/powerpc/kernel/prom.c:move_device_tree() {
> 	...
> 	p = memblock_alloc_raw(size, PAGE_SIZE);
> 	...
> }
>
> This is called via early_init_devtree() from early_setup():
>
> arch/powerpc/kernel/setup_64.c:early_setup() {
> 	...
> 	early_init_devtree(__va(dt_ptr));
>
> 	allocate_paca_ptrs();
> 	allocate_paca(boot_cpuid);
> 	... 
> 	early_init_mmu();
> 	...
> }
>
> Because KHO scratch-only mode has not yet been enforced during these early
> allocations, could they use standard memblock policies and overwrite preserved
> KHO memory state, corrupting the handover?

Sashiko seems to have a point. early_init_devtree() (called from
early_setup()) already does a lot of initialization that x86's
setup_arch() does, including making memblock allocations, allowing
resize, etc.

So perhaps you need to move this early_init_dt_check_kho() call to
early_setup()?
diff mbox series

Patch

diff --git a/arch/powerpc/Kconfig b/arch/powerpc/Kconfig
index 2580e27e4328..3517d74949d0 100644
--- a/arch/powerpc/Kconfig
+++ b/arch/powerpc/Kconfig
@@ -716,6 +716,9 @@  config ARCH_SELECTS_CRASH_DUMP
 	depends on CRASH_DUMP
 	select RELOCATABLE if PPC64 || 44x || PPC_85xx
 
+config ARCH_SUPPORTS_KEXEC_HANDOVER
+	def_bool PPC64
+
 config ARCH_SUPPORTS_CRASH_HOTPLUG
 	def_bool y
 	depends on PPC64
diff --git a/arch/powerpc/kernel/setup-common.c b/arch/powerpc/kernel/setup-common.c
index 4afaba19b586..53c2f1897bfe 100644
--- a/arch/powerpc/kernel/setup-common.c
+++ b/arch/powerpc/kernel/setup-common.c
@@ -923,6 +923,8 @@  void __init setup_arch(char **cmdline_p)
 	/* Set a half-reasonable default so udelay does something sensible */
 	loops_per_jiffy = 500000000 / HZ;
 
+	early_init_dt_check_kho();
+
 	/* Unflatten the device-tree passed by prom_init or kexec */
 	unflatten_device_tree();
 
diff --git a/drivers/of/fdt.c b/drivers/of/fdt.c
index a64afc3ded3d..69e6ad868fa3 100644
--- a/drivers/of/fdt.c
+++ b/drivers/of/fdt.c
@@ -926,7 +926,7 @@  void __init early_init_dt_check_for_usable_mem_range(void)
 /**
  * early_init_dt_check_kho - Decode info required for kexec handover from DT
  */
-static void __init early_init_dt_check_kho(void)
+void __init early_init_dt_check_kho(void)
 {
 	unsigned long node = chosen_node_offset;
 	u64 fdt_start, fdt_size, scratch_start, scratch_size;
diff --git a/include/linux/of_fdt.h b/include/linux/of_fdt.h
index 51dadbaa3d63..5e9d6af0c7dd 100644
--- a/include/linux/of_fdt.h
+++ b/include/linux/of_fdt.h
@@ -83,6 +83,7 @@  extern int early_init_dt_scan_root(void);
 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 void early_init_dt_check_kho(void);
 
 extern const char *of_flat_dt_get_machine_name(void);
 extern const void *of_flat_dt_match_machine(const void *default_match,