* [PATCH v6 00/14] kexec: introduce Kexec HandOver (KHO)
@ 2025-04-11 5:37 Changyuan Lyu
2025-04-11 5:37 ` [PATCH v6 01/14] memblock: add MEMBLOCK_RSRV_KERN flag Changyuan Lyu
` (14 more replies)
0 siblings, 15 replies; 26+ messages in thread
From: Changyuan Lyu @ 2025-04-11 5:37 UTC (permalink / raw)
To: linux-kernel
Cc: akpm, anthony.yznaga, arnd, ashish.kalra, benh, bp,
catalin.marinas, corbet, dave.hansen, devicetree, dwmw2, ebiederm,
graf, hpa, jgowans, kexec, krzk, linux-arm-kernel, linux-doc,
linux-mm, luto, mark.rutland, mingo, pasha.tatashin, pbonzini,
peterz, ptyadav, robh, rostedt, rppt, saravanak, skinsburskii,
tglx, thomas.lendacky, will, x86, Changyuan Lyu
Hi,
This is the version 6 of Alexander Graf and Mike Rapoport's
"kexec: introduce Kexec HandOver (KHO)" series
(https://lore.kernel.org/all/20250320015551.2157511-1-changyuanl@google.com/).
The patches are also available in git:
https://github.com/googleprodkernel/linux-liveupdate/tree/kho/v6.
v5 -> v6:
- Rebase the patchset on v6.15-rc1.
- Revert hashtable-based API introduced in V5.
- In kho_parse_scratch_size(), replace simple_strtoul() with kstrtouint().
- Make KHO focus on memory preservation. Subsystem's metadata is not
saved into KHO root tree, instead Subsystems allocate and create their
own FDT and use KHO to preserve the FDT's underlying folios, suggested
by [6].
- Subsystem's own FDT is presented at debugfs kho/(in|out)/sub_fdts/$name.
- Remove `fdt_max`, limit KHO root FDT to 1 page [6].
- Move linked pages of bitmaps of preserved memory from
/preserved-memory.metadata to preserved-memory-map of KHO root FDT
as suggested in [1].
- Add struct kho_serialization to hold the root FDT and struct
kho_mem_track, and pass it through notifiers, as suggested in [2].
- Update the KHO example of memblock. memblock now prepares its own FDT
early. In the notifier callback, it only preserves the memory and saves
its own FDT blob address to KHO root tree.
- Add the doc of KHO property in node 'chosen' to
github.com/devicetree-org/dt-schema [3] as requested in [4].
- Add back YAML files to describe KHO and memblock FDT bindings as requested
in [2]
- Remove kho_restore_phys().
- Move documentations to Documentation/admin-guide/kho.rst and
Documentation/core-api/kho as requested in [5].
- Split KHO from kexec in MAINTAINERS.
- kho_restore_folio() refuses to create folios larger than
MAX_PAGE_ORDER [7].
- Fix the bug on arm64 reported in [8].
- Fix the bug in kho_preserve_phys() reported in [9].
- Check KHO root node "compatible" in kho_populate().
Since the discussion of the data format for serializing preserved memory [10] is
still going on, this version goes with the original xarray+bitmap approach
and save the optimization for future versions/patches.
[1] https://lore.kernel.org/all/20250212152336.GA3848889@nvidia.com/
[2] https://lore.kernel.org/all/20250321134629.GA252045@nvidia.com/
[3] https://github.com/devicetree-org/dt-schema/pull/158
[4] https://lore.kernel.org/all/55a5e3f3-1b3f-469b-bde0-69abfff826e4@kernel.org/
[5] https://lore.kernel.org/all/87wmcj69sg.fsf@trenco.lwn.net/
[6] https://lore.kernel.org/all/Z+GIRecXeYXiPrYv@nvidia.com/
[7] https://lore.kernel.org/all/mafs05xjmqsqc.fsf@amazon.de/
[8] https://lore.kernel.org/all/20250411034748.1781232-1-changyuanl@google.com/
[9] https://lore.kernel.org/all/20250411040207.1785245-1-changyuanl@google.com/
[10] https://lore.kernel.org/all/20250320015551.2157511-10-changyuanl@google.com/
Best,
Changyuan
= Original cover letter =
Kexec today considers itself purely a boot loader: When we enter the new
kernel, any state the previous kernel left behind is irrelevant and the
new kernel reinitializes the system.
However, there are use cases where this mode of operation is not what we
actually want. In virtualization hosts for example, we want to use kexec
to update the host kernel while virtual machine memory stays untouched.
When we add device assignment to the mix, we also need to ensure that
IOMMU and VFIO states are untouched. If we add PCIe peer to peer DMA, we
need to do the same for the PCI subsystem. If we want to kexec while an
SEV-SNP enabled virtual machine is running, we need to preserve the VM
context pages and physical memory. See "pkernfs: Persisting guest memory
and kernel/device state safely across kexec" Linux Plumbers
Conference 2023 presentation for details:
https://lpc.events/event/17/contributions/1485/
To start us on the journey to support all the use cases above, this patch
implements basic infrastructure to allow hand over of kernel state across
kexec (Kexec HandOver, aka KHO). As a really simple example target, we use
memblock's reserve_mem.
With this patch set applied, memory that was reserved using "reserve_mem"
command line options remains intact after kexec and it is guaranteed to
reside at the same physical address.
== Alternatives ==
There are alternative approaches to (parts of) the problems above:
* Memory Pools [1] - preallocated persistent memory region + allocator
* PRMEM [2] - resizable persistent memory regions with fixed metadata
pointer on the kernel command line + allocator
* Pkernfs [3] - preallocated file system for in-kernel data with fixed
address location on the kernel command line
* PKRAM [4] - handover of user space pages using a fixed metadata page
specified via command line
All of the approaches above fundamentally have the same problem: They
require the administrator to explicitly carve out a physical memory
location because they have no mechanism outside of the kernel command
line to pass data (including memory reservations) between kexec'ing
kernels.
KHO provides that base foundation. We will determine later whether we
still need any of the approaches above for fast bulk memory handover of for
example IOMMU page tables. But IMHO they would all be users of KHO, with
KHO providing the foundational primitive to pass metadata and bulk memory
reservations as well as provide easy versioning for data.
== Overview ==
We introduce a metadata file that the kernels pass between each other. How
they pass it is architecture specific. The file's format is a Flattened
Device Tree (fdt) which has a generator and parser already included in
Linux. KHO is enabled in the kernel command line by `kho=on`. When the root
user enables KHO through /sys/kernel/debug/kho/out/finalize, the kernel
invokes callbacks to every KHO users to register preserved memory regions,
which contain drivers' states.
When the actual kexec happens, the fdt is part of the image
set that we boot into. In addition, we keep "scratch regions" available
for kexec: physically contiguous memory regions that are guaranteed to
not have any memory that KHO would preserve. The new kernel bootstraps
itself using the scratch regions and sets all handed over memory as in use.
When drivers initialize that support KHO, they introspect the fdt, restore
preserved memory regions, and retrieve their states stored in the preserved
memory.
== Limitations ==
Currently KHO is only implemented for file based kexec. The kernel
interfaces in the patch set are already in place to support user space
kexec as well, but it is still not implemented it yet inside kexec tools.
== How to Use ==
To use the code, please boot the kernel with the "kho=on" command line
parameter. KHO will automatically create scratch regions. If you want to set
the scratch size explicitly you can use "kho_scratch=" command line parameter.
For instance, "kho_scratch=16M,512M,256M" will reserve a 16 MiB low
memory scratch area, a 512 MiB global scratch region, and 256 MiB
per NUMA node scratch regions on boot.
Make sure to have a reserved memory range requested with reserv_mem
command line option, for example, "reserve_mem=64m:4k:n1".
Then before you invoke file based "kexec -l", finalize
KHO FDT:
# echo 1 > /sys/kernel/debug/kho/out/finalize
You can preview the generated FDT using `dtc`,
# dtc /sys/kernel/debug/kho/out/fdt
# dtc /sys/kernel/debug/kho/out/sub_fdts/memblock
`dtc` is available on ubuntu by `sudo apt-get install device-tree-compiler`.
Now kexec into the new kernel,
# kexec -l Image --initrd=initrd -s
# kexec -e
(The order of KHO finalization and "kexec -l" does not matter.)
The new kernel will boot up and contain the previous kernel's reserve_mem
contents at the same physical address as the first kernel.
You can also review the FDT passed from the old kernel,
# dtc /sys/kernel/debug/kho/in/fdt
# dtc /sys/kernel/debug/kho/in/sub_fdts/memblock
== Changelog ==
v4 -> v5:
- New: Preserve folios and address ranges in bitmaps [1]. Removed the
`mem` property.
- New: Hash table based API for manipulating the KHO state tree.
- Change the concept of "active phase" to "finalization phase". KHO
users can add/remove data into/from KHO DT anytime before the
finalization phase.
- Decouple kexec_file_load and KHO FDT creation. kexec_file_load can be
done before KHO FDT is created.
- Update the example usecase (reserve_mem) using the new KHO API,
replace underscores with dashes in reserve-mem fdt generation.
- Drop the YAMLs for now and add a brief description of KHO FDT before
KHO schema is stable.
- Move all sysfs interfaces to debugfs.
- Fixed the memblock test reported in [2].
- Incorporate fix for kho_locate_mem_hole() with !CONFIG_KEXEC_HANDOVER
[3] into "kexec: Add KHO support to kexec file loads".
[1] https://lore.kernel.org/all/20250212152336.GA3848889@nvidia.com/
[2] https://lore.kernel.org/all/20250217040448.56xejbvsr2a73h4c@master/
[3] https://lore.kernel.org/all/20250214125402.90709-1-sourabhjain@linux.ibm.com/
v3 -> v4:
- Major rework of scrach management. Rather than force scratch memory
allocations only very early in boot now we rely on scratch for all
memblock allocations.
- Use simple example usecase (reserv_mem instead of ftrace)
- merge all KHO functionality into a single kernel/kexec_handover.c file
- rename CONFIG_KEXEC_KHO to CONFIG_KEXEC_HANDOVER
v2 -> v3:
- Fix make dt_binding_check
- Add descriptions for each object
- s/trace_flags/trace-flags/
- s/global_trace/global-trace/
- Make all additionalProperties false
- Change subject to reflect subsysten (dt-bindings)
- Fix indentation
- Remove superfluous examples
- Convert to 64bit syntax
- Move to kho directory
- s/"global_trace"/"global-trace"/
- s/"global_trace"/"global-trace"/
- s/"trace_flags"/"trace-flags"/
- Fix wording
- Add Documentation to MAINTAINERS file
- Remove kho reference on read error
- Move handover_dt unmap up
- s/reserve_scratch_mem/mark_phys_as_cma/
- Remove ifdeffery
- Remove superfluous comment
v1 -> v2:
- Removed: tracing: Introduce names for ring buffers
- Removed: tracing: Introduce names for events
- New: kexec: Add config option for KHO
- New: kexec: Add documentation for KHO
- New: tracing: Initialize fields before registering
- New: devicetree: Add bindings for ftrace KHO
- test bot warning fixes
- Change kconfig option to ARCH_SUPPORTS_KEXEC_KHO
- s/kho_reserve_mem/kho_reserve_previous_mem/g
- s/kho_reserve/kho_reserve_scratch/g
- Remove / reduce ifdefs
- Select crc32
- Leave anything that requires a name in trace.c to keep buffers
unnamed entities
- Put events as array into a property, use fingerprint instead of
names to identify them
- Reduce footprint without CONFIG_FTRACE_KHO
- s/kho_reserve_mem/kho_reserve_previous_mem/g
- make kho_get_fdt() const
- Add stubs for return_mem and claim_mem
- make kho_get_fdt() const
- Get events as array from a property, use fingerprint instead of
names to identify events
- Change kconfig option to ARCH_SUPPORTS_KEXEC_KHO
- s/kho_reserve_mem/kho_reserve_previous_mem/g
- s/kho_reserve/kho_reserve_scratch/g
- Leave the node generation code that needs to know the name in
trace.c so that ring buffers can stay anonymous
- s/kho_reserve/kho_reserve_scratch/g
- Move kho enums out of ifdef
- Move from names to fdt offsets. That way, trace.c can find the trace
array offset and then the ring buffer code only needs to read out
its per-CPU data. That way it can stay oblivient to its name.
- Make kho_get_fdt() const
Alexander Graf (9):
memblock: Add support for scratch memory
kexec: add Kexec HandOver (KHO) generation helpers
kexec: add KHO parsing support
kexec: add KHO support to kexec file loads
kexec: add config option for KHO
arm64: add KHO support
x86: add KHO support
memblock: add KHO support for reserve_mem
Documentation: add documentation for KHO
Mike Rapoport (Microsoft) (5):
memblock: add MEMBLOCK_RSRV_KERN flag
memblock: introduce memmap_init_kho_scratch()
kexec: enable KHO support for memory preservation
x86/setup: use memblock_reserve_kern for memory used by kernel
Documentation: KHO: Add memblock bindings
.../admin-guide/kernel-parameters.txt | 25 +
Documentation/admin-guide/mm/index.rst | 1 +
Documentation/admin-guide/mm/kho.rst | 120 ++
Documentation/core-api/index.rst | 1 +
Documentation/core-api/kho/bindings/kho.yaml | 43 +
.../kho/bindings/memblock/memblock.yaml | 39 +
.../kho/bindings/memblock/reserve-mem.yaml | 40 +
.../core-api/kho/bindings/sub-fdt.yaml | 27 +
Documentation/core-api/kho/concepts.rst | 74 +
Documentation/core-api/kho/fdt.rst | 80 ++
Documentation/core-api/kho/index.rst | 13 +
MAINTAINERS | 12 +
arch/arm64/Kconfig | 3 +
arch/x86/Kconfig | 3 +
arch/x86/boot/compressed/kaslr.c | 52 +-
arch/x86/include/asm/setup.h | 4 +
arch/x86/include/uapi/asm/setup_data.h | 13 +-
arch/x86/kernel/e820.c | 18 +
arch/x86/kernel/kexec-bzimage64.c | 36 +
arch/x86/kernel/setup.c | 41 +-
arch/x86/realmode/init.c | 2 +
drivers/of/fdt.c | 34 +
drivers/of/kexec.c | 42 +
include/linux/kexec.h | 7 +
include/linux/kexec_handover.h | 111 ++
include/linux/memblock.h | 41 +-
kernel/Kconfig.kexec | 14 +
kernel/Makefile | 1 +
kernel/kexec_file.c | 13 +
kernel/kexec_handover.c | 1253 +++++++++++++++++
kernel/kexec_internal.h | 16 +
mm/Kconfig | 4 +
mm/internal.h | 2 +
mm/memblock.c | 329 ++++-
mm/mm_init.c | 19 +-
tools/testing/memblock/tests/alloc_api.c | 22 +-
.../memblock/tests/alloc_helpers_api.c | 4 +-
tools/testing/memblock/tests/alloc_nid_api.c | 20 +-
38 files changed, 2534 insertions(+), 45 deletions(-)
create mode 100644 Documentation/admin-guide/mm/kho.rst
create mode 100644 Documentation/core-api/kho/bindings/kho.yaml
create mode 100644 Documentation/core-api/kho/bindings/memblock/memblock.yaml
create mode 100644 Documentation/core-api/kho/bindings/memblock/reserve-mem.yaml
create mode 100644 Documentation/core-api/kho/bindings/sub-fdt.yaml
create mode 100644 Documentation/core-api/kho/concepts.rst
create mode 100644 Documentation/core-api/kho/fdt.rst
create mode 100644 Documentation/core-api/kho/index.rst
create mode 100644 include/linux/kexec_handover.h
create mode 100644 kernel/kexec_handover.c
base-commit: 0af2f6be1b4281385b618cb86ad946eded089ac8
--
2.49.0.604.gff1f9ca942-goog
^ permalink raw reply [flat|nested] 26+ messages in thread
* [PATCH v6 01/14] memblock: add MEMBLOCK_RSRV_KERN flag
2025-04-11 5:37 [PATCH v6 00/14] kexec: introduce Kexec HandOver (KHO) Changyuan Lyu
@ 2025-04-11 5:37 ` Changyuan Lyu
2025-04-11 5:37 ` [PATCH v6 02/14] memblock: Add support for scratch memory Changyuan Lyu
` (13 subsequent siblings)
14 siblings, 0 replies; 26+ messages in thread
From: Changyuan Lyu @ 2025-04-11 5:37 UTC (permalink / raw)
To: linux-kernel
Cc: akpm, anthony.yznaga, arnd, ashish.kalra, benh, bp,
catalin.marinas, corbet, dave.hansen, devicetree, dwmw2, ebiederm,
graf, hpa, jgowans, kexec, krzk, linux-arm-kernel, linux-doc,
linux-mm, luto, mark.rutland, mingo, pasha.tatashin, pbonzini,
peterz, ptyadav, robh, rostedt, rppt, saravanak, skinsburskii,
tglx, thomas.lendacky, will, x86, Changyuan Lyu
From: "Mike Rapoport (Microsoft)" <rppt@kernel.org>
to denote areas that were reserved for kernel use either directly with
memblock_reserve_kern() or via memblock allocations.
Signed-off-by: Mike Rapoport (Microsoft) <rppt@kernel.org>
Co-developed-by: Changyuan Lyu <changyuanl@google.com>
Signed-off-by: Changyuan Lyu <changyuanl@google.com>
---
include/linux/memblock.h | 19 ++++++++-
mm/memblock.c | 40 +++++++++++++++----
tools/testing/memblock/tests/alloc_api.c | 22 +++++-----
.../memblock/tests/alloc_helpers_api.c | 4 +-
tools/testing/memblock/tests/alloc_nid_api.c | 20 +++++-----
5 files changed, 73 insertions(+), 32 deletions(-)
diff --git a/include/linux/memblock.h b/include/linux/memblock.h
index ef5a1ecc6e595..6c00fbc085132 100644
--- a/include/linux/memblock.h
+++ b/include/linux/memblock.h
@@ -42,6 +42,9 @@ extern unsigned long long max_possible_pfn;
* kernel resource tree.
* @MEMBLOCK_RSRV_NOINIT: memory region for which struct pages are
* not initialized (only for reserved regions).
+ * @MEMBLOCK_RSRV_KERN: memory region that is reserved for kernel use,
+ * either explictitly with memblock_reserve_kern() or via memblock
+ * allocation APIs. All memblock allocations set this flag.
*/
enum memblock_flags {
MEMBLOCK_NONE = 0x0, /* No special request */
@@ -50,6 +53,7 @@ enum memblock_flags {
MEMBLOCK_NOMAP = 0x4, /* don't add to kernel direct mapping */
MEMBLOCK_DRIVER_MANAGED = 0x8, /* always detected via a driver */
MEMBLOCK_RSRV_NOINIT = 0x10, /* don't initialize struct pages */
+ MEMBLOCK_RSRV_KERN = 0x20, /* memory reserved for kernel use */
};
/**
@@ -116,7 +120,19 @@ int memblock_add_node(phys_addr_t base, phys_addr_t size, int nid,
int memblock_add(phys_addr_t base, phys_addr_t size);
int memblock_remove(phys_addr_t base, phys_addr_t size);
int memblock_phys_free(phys_addr_t base, phys_addr_t size);
-int memblock_reserve(phys_addr_t base, phys_addr_t size);
+int __memblock_reserve(phys_addr_t base, phys_addr_t size, int nid,
+ enum memblock_flags flags);
+
+static __always_inline int memblock_reserve(phys_addr_t base, phys_addr_t size)
+{
+ return __memblock_reserve(base, size, NUMA_NO_NODE, 0);
+}
+
+static __always_inline int memblock_reserve_kern(phys_addr_t base, phys_addr_t size)
+{
+ return __memblock_reserve(base, size, NUMA_NO_NODE, MEMBLOCK_RSRV_KERN);
+}
+
#ifdef CONFIG_HAVE_MEMBLOCK_PHYS_MAP
int memblock_physmem_add(phys_addr_t base, phys_addr_t size);
#endif
@@ -476,6 +492,7 @@ static inline __init_memblock bool memblock_bottom_up(void)
phys_addr_t memblock_phys_mem_size(void);
phys_addr_t memblock_reserved_size(void);
+phys_addr_t memblock_reserved_kern_size(phys_addr_t limit, int nid);
unsigned long memblock_estimated_nr_free_pages(void);
phys_addr_t memblock_start_of_DRAM(void);
phys_addr_t memblock_end_of_DRAM(void);
diff --git a/mm/memblock.c b/mm/memblock.c
index 0a53db4d9f7be..2dc95ecdee5ce 100644
--- a/mm/memblock.c
+++ b/mm/memblock.c
@@ -492,7 +492,7 @@ static int __init_memblock memblock_double_array(struct memblock_type *type,
* needn't do it
*/
if (!use_slab)
- BUG_ON(memblock_reserve(addr, new_alloc_size));
+ BUG_ON(memblock_reserve_kern(addr, new_alloc_size));
/* Update slab flag */
*in_slab = use_slab;
@@ -642,7 +642,7 @@ static int __init_memblock memblock_add_range(struct memblock_type *type,
#ifdef CONFIG_NUMA
WARN_ON(nid != memblock_get_region_node(rgn));
#endif
- WARN_ON(flags != rgn->flags);
+ WARN_ON(flags != MEMBLOCK_NONE && flags != rgn->flags);
nr_new++;
if (insert) {
if (start_rgn == -1)
@@ -902,14 +902,15 @@ int __init_memblock memblock_phys_free(phys_addr_t base, phys_addr_t size)
return memblock_remove_range(&memblock.reserved, base, size);
}
-int __init_memblock memblock_reserve(phys_addr_t base, phys_addr_t size)
+int __init_memblock __memblock_reserve(phys_addr_t base, phys_addr_t size,
+ int nid, enum memblock_flags flags)
{
phys_addr_t end = base + size - 1;
- memblock_dbg("%s: [%pa-%pa] %pS\n", __func__,
- &base, &end, (void *)_RET_IP_);
+ memblock_dbg("%s: [%pa-%pa] nid=%d flags=%x %pS\n", __func__,
+ &base, &end, nid, flags, (void *)_RET_IP_);
- return memblock_add_range(&memblock.reserved, base, size, MAX_NUMNODES, 0);
+ return memblock_add_range(&memblock.reserved, base, size, nid, flags);
}
#ifdef CONFIG_HAVE_MEMBLOCK_PHYS_MAP
@@ -1460,14 +1461,14 @@ phys_addr_t __init memblock_alloc_range_nid(phys_addr_t size,
again:
found = memblock_find_in_range_node(size, align, start, end, nid,
flags);
- if (found && !memblock_reserve(found, size))
+ if (found && !__memblock_reserve(found, size, nid, MEMBLOCK_RSRV_KERN))
goto done;
if (numa_valid_node(nid) && !exact_nid) {
found = memblock_find_in_range_node(size, align, start,
end, NUMA_NO_NODE,
flags);
- if (found && !memblock_reserve(found, size))
+ if (found && !memblock_reserve_kern(found, size))
goto done;
}
@@ -1752,6 +1753,28 @@ phys_addr_t __init_memblock memblock_reserved_size(void)
return memblock.reserved.total_size;
}
+phys_addr_t __init_memblock memblock_reserved_kern_size(phys_addr_t limit, int nid)
+{
+ struct memblock_region *r;
+ phys_addr_t total = 0;
+
+ for_each_reserved_mem_region(r) {
+ phys_addr_t size = r->size;
+
+ if (r->base > limit)
+ break;
+
+ if (r->base + r->size > limit)
+ size = limit - r->base;
+
+ if (nid == memblock_get_region_node(r) || !numa_valid_node(nid))
+ if (r->flags & MEMBLOCK_RSRV_KERN)
+ total += size;
+ }
+
+ return total;
+}
+
/**
* memblock_estimated_nr_free_pages - return estimated number of free pages
* from memblock point of view
@@ -2441,6 +2464,7 @@ static const char * const flagname[] = {
[ilog2(MEMBLOCK_NOMAP)] = "NOMAP",
[ilog2(MEMBLOCK_DRIVER_MANAGED)] = "DRV_MNG",
[ilog2(MEMBLOCK_RSRV_NOINIT)] = "RSV_NIT",
+ [ilog2(MEMBLOCK_RSRV_KERN)] = "RSV_KERN",
};
static int memblock_debug_show(struct seq_file *m, void *private)
diff --git a/tools/testing/memblock/tests/alloc_api.c b/tools/testing/memblock/tests/alloc_api.c
index 68f1a75cd72c4..c55f67dd367d0 100644
--- a/tools/testing/memblock/tests/alloc_api.c
+++ b/tools/testing/memblock/tests/alloc_api.c
@@ -134,7 +134,7 @@ static int alloc_top_down_before_check(void)
PREFIX_PUSH();
setup_memblock();
- memblock_reserve(memblock_end_of_DRAM() - total_size, r1_size);
+ memblock_reserve_kern(memblock_end_of_DRAM() - total_size, r1_size);
allocated_ptr = run_memblock_alloc(r2_size, SMP_CACHE_BYTES);
@@ -182,7 +182,7 @@ static int alloc_top_down_after_check(void)
total_size = r1.size + r2_size;
- memblock_reserve(r1.base, r1.size);
+ memblock_reserve_kern(r1.base, r1.size);
allocated_ptr = run_memblock_alloc(r2_size, SMP_CACHE_BYTES);
@@ -231,8 +231,8 @@ static int alloc_top_down_second_fit_check(void)
total_size = r1.size + r2.size + r3_size;
- memblock_reserve(r1.base, r1.size);
- memblock_reserve(r2.base, r2.size);
+ memblock_reserve_kern(r1.base, r1.size);
+ memblock_reserve_kern(r2.base, r2.size);
allocated_ptr = run_memblock_alloc(r3_size, SMP_CACHE_BYTES);
@@ -285,8 +285,8 @@ static int alloc_in_between_generic_check(void)
total_size = r1.size + r2.size + r3_size;
- memblock_reserve(r1.base, r1.size);
- memblock_reserve(r2.base, r2.size);
+ memblock_reserve_kern(r1.base, r1.size);
+ memblock_reserve_kern(r2.base, r2.size);
allocated_ptr = run_memblock_alloc(r3_size, SMP_CACHE_BYTES);
@@ -422,7 +422,7 @@ static int alloc_limited_space_generic_check(void)
setup_memblock();
/* Simulate almost-full memory */
- memblock_reserve(memblock_start_of_DRAM(), reserved_size);
+ memblock_reserve_kern(memblock_start_of_DRAM(), reserved_size);
allocated_ptr = run_memblock_alloc(available_size, SMP_CACHE_BYTES);
@@ -608,7 +608,7 @@ static int alloc_bottom_up_before_check(void)
PREFIX_PUSH();
setup_memblock();
- memblock_reserve(memblock_start_of_DRAM() + r1_size, r2_size);
+ memblock_reserve_kern(memblock_start_of_DRAM() + r1_size, r2_size);
allocated_ptr = run_memblock_alloc(r1_size, SMP_CACHE_BYTES);
@@ -655,7 +655,7 @@ static int alloc_bottom_up_after_check(void)
total_size = r1.size + r2_size;
- memblock_reserve(r1.base, r1.size);
+ memblock_reserve_kern(r1.base, r1.size);
allocated_ptr = run_memblock_alloc(r2_size, SMP_CACHE_BYTES);
@@ -705,8 +705,8 @@ static int alloc_bottom_up_second_fit_check(void)
total_size = r1.size + r2.size + r3_size;
- memblock_reserve(r1.base, r1.size);
- memblock_reserve(r2.base, r2.size);
+ memblock_reserve_kern(r1.base, r1.size);
+ memblock_reserve_kern(r2.base, r2.size);
allocated_ptr = run_memblock_alloc(r3_size, SMP_CACHE_BYTES);
diff --git a/tools/testing/memblock/tests/alloc_helpers_api.c b/tools/testing/memblock/tests/alloc_helpers_api.c
index 3ef9486da8a09..e5362cfd2ff30 100644
--- a/tools/testing/memblock/tests/alloc_helpers_api.c
+++ b/tools/testing/memblock/tests/alloc_helpers_api.c
@@ -163,7 +163,7 @@ static int alloc_from_top_down_no_space_above_check(void)
min_addr = memblock_end_of_DRAM() - SMP_CACHE_BYTES * 2;
/* No space above this address */
- memblock_reserve(min_addr, r2_size);
+ memblock_reserve_kern(min_addr, r2_size);
allocated_ptr = memblock_alloc_from(r1_size, SMP_CACHE_BYTES, min_addr);
@@ -199,7 +199,7 @@ static int alloc_from_top_down_min_addr_cap_check(void)
start_addr = (phys_addr_t)memblock_start_of_DRAM();
min_addr = start_addr - SMP_CACHE_BYTES * 3;
- memblock_reserve(start_addr + r1_size, MEM_SIZE - r1_size);
+ memblock_reserve_kern(start_addr + r1_size, MEM_SIZE - r1_size);
allocated_ptr = memblock_alloc_from(r1_size, SMP_CACHE_BYTES, min_addr);
diff --git a/tools/testing/memblock/tests/alloc_nid_api.c b/tools/testing/memblock/tests/alloc_nid_api.c
index 49bb416d34ffc..562e4701b0e02 100644
--- a/tools/testing/memblock/tests/alloc_nid_api.c
+++ b/tools/testing/memblock/tests/alloc_nid_api.c
@@ -324,7 +324,7 @@ static int alloc_nid_min_reserved_generic_check(void)
min_addr = max_addr - r2_size;
reserved_base = min_addr - r1_size;
- memblock_reserve(reserved_base, r1_size);
+ memblock_reserve_kern(reserved_base, r1_size);
allocated_ptr = run_memblock_alloc_nid(r2_size, SMP_CACHE_BYTES,
min_addr, max_addr,
@@ -374,7 +374,7 @@ static int alloc_nid_max_reserved_generic_check(void)
max_addr = memblock_end_of_DRAM() - r1_size;
min_addr = max_addr - r2_size;
- memblock_reserve(max_addr, r1_size);
+ memblock_reserve_kern(max_addr, r1_size);
allocated_ptr = run_memblock_alloc_nid(r2_size, SMP_CACHE_BYTES,
min_addr, max_addr,
@@ -436,8 +436,8 @@ static int alloc_nid_top_down_reserved_with_space_check(void)
min_addr = r2.base + r2.size;
max_addr = r1.base;
- memblock_reserve(r1.base, r1.size);
- memblock_reserve(r2.base, r2.size);
+ memblock_reserve_kern(r1.base, r1.size);
+ memblock_reserve_kern(r2.base, r2.size);
allocated_ptr = run_memblock_alloc_nid(r3_size, SMP_CACHE_BYTES,
min_addr, max_addr,
@@ -499,8 +499,8 @@ static int alloc_nid_reserved_full_merge_generic_check(void)
min_addr = r2.base + r2.size;
max_addr = r1.base;
- memblock_reserve(r1.base, r1.size);
- memblock_reserve(r2.base, r2.size);
+ memblock_reserve_kern(r1.base, r1.size);
+ memblock_reserve_kern(r2.base, r2.size);
allocated_ptr = run_memblock_alloc_nid(r3_size, SMP_CACHE_BYTES,
min_addr, max_addr,
@@ -563,8 +563,8 @@ static int alloc_nid_top_down_reserved_no_space_check(void)
min_addr = r2.base + r2.size;
max_addr = r1.base;
- memblock_reserve(r1.base, r1.size);
- memblock_reserve(r2.base, r2.size);
+ memblock_reserve_kern(r1.base, r1.size);
+ memblock_reserve_kern(r2.base, r2.size);
allocated_ptr = run_memblock_alloc_nid(r3_size, SMP_CACHE_BYTES,
min_addr, max_addr,
@@ -909,8 +909,8 @@ static int alloc_nid_bottom_up_reserved_with_space_check(void)
min_addr = r2.base + r2.size;
max_addr = r1.base;
- memblock_reserve(r1.base, r1.size);
- memblock_reserve(r2.base, r2.size);
+ memblock_reserve_kern(r1.base, r1.size);
+ memblock_reserve_kern(r2.base, r2.size);
allocated_ptr = run_memblock_alloc_nid(r3_size, SMP_CACHE_BYTES,
min_addr, max_addr,
--
2.49.0.604.gff1f9ca942-goog
^ permalink raw reply related [flat|nested] 26+ messages in thread
* [PATCH v6 02/14] memblock: Add support for scratch memory
2025-04-11 5:37 [PATCH v6 00/14] kexec: introduce Kexec HandOver (KHO) Changyuan Lyu
2025-04-11 5:37 ` [PATCH v6 01/14] memblock: add MEMBLOCK_RSRV_KERN flag Changyuan Lyu
@ 2025-04-11 5:37 ` Changyuan Lyu
2025-04-11 5:37 ` [PATCH v6 03/14] memblock: introduce memmap_init_kho_scratch() Changyuan Lyu
` (12 subsequent siblings)
14 siblings, 0 replies; 26+ messages in thread
From: Changyuan Lyu @ 2025-04-11 5:37 UTC (permalink / raw)
To: linux-kernel
Cc: akpm, anthony.yznaga, arnd, ashish.kalra, benh, bp,
catalin.marinas, corbet, dave.hansen, devicetree, dwmw2, ebiederm,
graf, hpa, jgowans, kexec, krzk, linux-arm-kernel, linux-doc,
linux-mm, luto, mark.rutland, mingo, pasha.tatashin, pbonzini,
peterz, ptyadav, robh, rostedt, rppt, saravanak, skinsburskii,
tglx, thomas.lendacky, will, x86, Changyuan Lyu
From: Alexander Graf <graf@amazon.com>
With KHO (Kexec HandOver), we need a way to ensure that the new kernel
does not allocate memory on top of any memory regions that the previous
kernel was handing over. But to know where those are, we need to include
them in the memblock.reserved array which may not be big enough to hold
all ranges that need to be persisted across kexec. To resize the array,
we need to allocate memory. That brings us into a catch 22 situation.
The solution to that is limit memblock allocations to the scratch regions:
safe regions to operate in the case when there is memory that should remain
intact across kexec.
KHO provides several "scratch regions" as part of its metadata. These
scratch regions are contiguous memory blocks that known not to contain any
memory that should be persisted across kexec. These regions should be large
enough to accommodate all memblock allocations done by the kexeced kernel.
We introduce a new memblock_set_scratch_only() function that allows KHO to
indicate that any memblock allocation must happen from the scratch regions.
Later, we may want to perform another KHO kexec. For that, we reuse the
same scratch regions. To ensure that no eventually handed over data gets
allocated inside a scratch region, we flip the semantics of the scratch
region with memblock_clear_scratch_only(): After that call, no allocations
may happen from scratch memblock regions. We will lift that restriction
in the next patch.
Signed-off-by: Alexander Graf <graf@amazon.com>
Co-developed-by: Mike Rapoport (Microsoft) <rppt@kernel.org>
Signed-off-by: Mike Rapoport (Microsoft) <rppt@kernel.org>
Signed-off-by: Changyuan Lyu <changyuanl@google.com>
---
include/linux/memblock.h | 20 +++++++++++++
mm/Kconfig | 4 +++
mm/memblock.c | 61 ++++++++++++++++++++++++++++++++++++++++
3 files changed, 85 insertions(+)
diff --git a/include/linux/memblock.h b/include/linux/memblock.h
index 6c00fbc085132..993937a6b9620 100644
--- a/include/linux/memblock.h
+++ b/include/linux/memblock.h
@@ -45,6 +45,11 @@ extern unsigned long long max_possible_pfn;
* @MEMBLOCK_RSRV_KERN: memory region that is reserved for kernel use,
* either explictitly with memblock_reserve_kern() or via memblock
* allocation APIs. All memblock allocations set this flag.
+ * @MEMBLOCK_KHO_SCRATCH: memory region that kexec can pass to the next
+ * kernel in handover mode. During early boot, we do not know about all
+ * memory reservations yet, so we get scratch memory from the previous
+ * kernel that we know is good to use. It is the only memory that
+ * allocations may happen from in this phase.
*/
enum memblock_flags {
MEMBLOCK_NONE = 0x0, /* No special request */
@@ -54,6 +59,7 @@ enum memblock_flags {
MEMBLOCK_DRIVER_MANAGED = 0x8, /* always detected via a driver */
MEMBLOCK_RSRV_NOINIT = 0x10, /* don't initialize struct pages */
MEMBLOCK_RSRV_KERN = 0x20, /* memory reserved for kernel use */
+ MEMBLOCK_KHO_SCRATCH = 0x40, /* scratch memory for kexec handover */
};
/**
@@ -148,6 +154,8 @@ int memblock_mark_mirror(phys_addr_t base, phys_addr_t size);
int memblock_mark_nomap(phys_addr_t base, phys_addr_t size);
int memblock_clear_nomap(phys_addr_t base, phys_addr_t size);
int memblock_reserved_mark_noinit(phys_addr_t base, phys_addr_t size);
+int memblock_mark_kho_scratch(phys_addr_t base, phys_addr_t size);
+int memblock_clear_kho_scratch(phys_addr_t base, phys_addr_t size);
void memblock_free(void *ptr, size_t size);
void reset_all_zones_managed_pages(void);
@@ -291,6 +299,11 @@ static inline bool memblock_is_driver_managed(struct memblock_region *m)
return m->flags & MEMBLOCK_DRIVER_MANAGED;
}
+static inline bool memblock_is_kho_scratch(struct memblock_region *m)
+{
+ return m->flags & MEMBLOCK_KHO_SCRATCH;
+}
+
int memblock_search_pfn_nid(unsigned long pfn, unsigned long *start_pfn,
unsigned long *end_pfn);
void __next_mem_pfn_range(int *idx, int nid, unsigned long *out_start_pfn,
@@ -619,5 +632,12 @@ static inline void early_memtest(phys_addr_t start, phys_addr_t end) { }
static inline void memtest_report_meminfo(struct seq_file *m) { }
#endif
+#ifdef CONFIG_MEMBLOCK_KHO_SCRATCH
+void memblock_set_kho_scratch_only(void);
+void memblock_clear_kho_scratch_only(void);
+#else
+static inline void memblock_set_kho_scratch_only(void) { }
+static inline void memblock_clear_kho_scratch_only(void) { }
+#endif
#endif /* _LINUX_MEMBLOCK_H */
diff --git a/mm/Kconfig b/mm/Kconfig
index e113f713b4938..60ea9eba48140 100644
--- a/mm/Kconfig
+++ b/mm/Kconfig
@@ -469,6 +469,10 @@ config HAVE_GUP_FAST
depends on MMU
bool
+# Enable memblock support for scratch memory which is needed for kexec handover
+config MEMBLOCK_KHO_SCRATCH
+ bool
+
# Don't discard allocated memory used to track "memory" and "reserved" memblocks
# after early boot, so it can still be used to test for validity of memory.
# Also, memblocks are updated with memory hot(un)plug.
diff --git a/mm/memblock.c b/mm/memblock.c
index 2dc95ecdee5ce..3a213e2a485bc 100644
--- a/mm/memblock.c
+++ b/mm/memblock.c
@@ -107,6 +107,13 @@ unsigned long min_low_pfn;
unsigned long max_pfn;
unsigned long long max_possible_pfn;
+#ifdef CONFIG_MEMBLOCK_KHO_SCRATCH
+/* When set to true, only allocate from MEMBLOCK_KHO_SCRATCH ranges */
+static bool kho_scratch_only;
+#else
+#define kho_scratch_only false
+#endif
+
static struct memblock_region memblock_memory_init_regions[INIT_MEMBLOCK_MEMORY_REGIONS] __initdata_memblock;
static struct memblock_region memblock_reserved_init_regions[INIT_MEMBLOCK_RESERVED_REGIONS] __initdata_memblock;
#ifdef CONFIG_HAVE_MEMBLOCK_PHYS_MAP
@@ -166,6 +173,10 @@ bool __init_memblock memblock_has_mirror(void)
static enum memblock_flags __init_memblock choose_memblock_flags(void)
{
+ /* skip non-scratch memory for kho early boot allocations */
+ if (kho_scratch_only)
+ return MEMBLOCK_KHO_SCRATCH;
+
return system_has_some_mirror ? MEMBLOCK_MIRROR : MEMBLOCK_NONE;
}
@@ -925,6 +936,18 @@ int __init_memblock memblock_physmem_add(phys_addr_t base, phys_addr_t size)
}
#endif
+#ifdef CONFIG_MEMBLOCK_KHO_SCRATCH
+__init_memblock void memblock_set_kho_scratch_only(void)
+{
+ kho_scratch_only = true;
+}
+
+__init_memblock void memblock_clear_kho_scratch_only(void)
+{
+ kho_scratch_only = false;
+}
+#endif
+
/**
* memblock_setclr_flag - set or clear flag for a memory region
* @type: memblock type to set/clear flag for
@@ -1050,6 +1073,36 @@ int __init_memblock memblock_reserved_mark_noinit(phys_addr_t base, phys_addr_t
MEMBLOCK_RSRV_NOINIT);
}
+/**
+ * memblock_mark_kho_scratch - Mark a memory region as MEMBLOCK_KHO_SCRATCH.
+ * @base: the base phys addr of the region
+ * @size: the size of the region
+ *
+ * Only memory regions marked with %MEMBLOCK_KHO_SCRATCH will be considered
+ * for allocations during early boot with kexec handover.
+ *
+ * Return: 0 on success, -errno on failure.
+ */
+int __init_memblock memblock_mark_kho_scratch(phys_addr_t base, phys_addr_t size)
+{
+ return memblock_setclr_flag(&memblock.memory, base, size, 1,
+ MEMBLOCK_KHO_SCRATCH);
+}
+
+/**
+ * memblock_clear_kho_scratch - Clear MEMBLOCK_KHO_SCRATCH flag for a
+ * specified region.
+ * @base: the base phys addr of the region
+ * @size: the size of the region
+ *
+ * Return: 0 on success, -errno on failure.
+ */
+int __init_memblock memblock_clear_kho_scratch(phys_addr_t base, phys_addr_t size)
+{
+ return memblock_setclr_flag(&memblock.memory, base, size, 0,
+ MEMBLOCK_KHO_SCRATCH);
+}
+
static bool should_skip_region(struct memblock_type *type,
struct memblock_region *m,
int nid, int flags)
@@ -1081,6 +1134,13 @@ static bool should_skip_region(struct memblock_type *type,
if (!(flags & MEMBLOCK_DRIVER_MANAGED) && memblock_is_driver_managed(m))
return true;
+ /*
+ * In early alloc during kexec handover, we can only consider
+ * MEMBLOCK_KHO_SCRATCH regions for the allocations
+ */
+ if ((flags & MEMBLOCK_KHO_SCRATCH) && !memblock_is_kho_scratch(m))
+ return true;
+
return false;
}
@@ -2465,6 +2525,7 @@ static const char * const flagname[] = {
[ilog2(MEMBLOCK_DRIVER_MANAGED)] = "DRV_MNG",
[ilog2(MEMBLOCK_RSRV_NOINIT)] = "RSV_NIT",
[ilog2(MEMBLOCK_RSRV_KERN)] = "RSV_KERN",
+ [ilog2(MEMBLOCK_KHO_SCRATCH)] = "KHO_SCRATCH",
};
static int memblock_debug_show(struct seq_file *m, void *private)
--
2.49.0.604.gff1f9ca942-goog
^ permalink raw reply related [flat|nested] 26+ messages in thread
* [PATCH v6 03/14] memblock: introduce memmap_init_kho_scratch()
2025-04-11 5:37 [PATCH v6 00/14] kexec: introduce Kexec HandOver (KHO) Changyuan Lyu
2025-04-11 5:37 ` [PATCH v6 01/14] memblock: add MEMBLOCK_RSRV_KERN flag Changyuan Lyu
2025-04-11 5:37 ` [PATCH v6 02/14] memblock: Add support for scratch memory Changyuan Lyu
@ 2025-04-11 5:37 ` Changyuan Lyu
2025-04-11 5:37 ` [PATCH v6 04/14] kexec: add Kexec HandOver (KHO) generation helpers Changyuan Lyu
` (11 subsequent siblings)
14 siblings, 0 replies; 26+ messages in thread
From: Changyuan Lyu @ 2025-04-11 5:37 UTC (permalink / raw)
To: linux-kernel
Cc: akpm, anthony.yznaga, arnd, ashish.kalra, benh, bp,
catalin.marinas, corbet, dave.hansen, devicetree, dwmw2, ebiederm,
graf, hpa, jgowans, kexec, krzk, linux-arm-kernel, linux-doc,
linux-mm, luto, mark.rutland, mingo, pasha.tatashin, pbonzini,
peterz, ptyadav, robh, rostedt, rppt, saravanak, skinsburskii,
tglx, thomas.lendacky, will, x86, Changyuan Lyu
From: "Mike Rapoport (Microsoft)" <rppt@kernel.org>
With deferred initialization of struct page it will be necessary to
initialize memory map for KHO scratch regions early.
Add memmap_init_kho_scratch() method that will allow such initialization
in upcoming patches.
Signed-off-by: Mike Rapoport (Microsoft) <rppt@kernel.org>
Signed-off-by: Changyuan Lyu <changyuanl@google.com>
---
include/linux/memblock.h | 2 ++
mm/internal.h | 2 ++
mm/memblock.c | 22 ++++++++++++++++++++++
mm/mm_init.c | 11 ++++++++---
4 files changed, 34 insertions(+), 3 deletions(-)
diff --git a/include/linux/memblock.h b/include/linux/memblock.h
index 993937a6b9620..bb19a25342246 100644
--- a/include/linux/memblock.h
+++ b/include/linux/memblock.h
@@ -635,9 +635,11 @@ static inline void memtest_report_meminfo(struct seq_file *m) { }
#ifdef CONFIG_MEMBLOCK_KHO_SCRATCH
void memblock_set_kho_scratch_only(void);
void memblock_clear_kho_scratch_only(void);
+void memmap_init_kho_scratch_pages(void);
#else
static inline void memblock_set_kho_scratch_only(void) { }
static inline void memblock_clear_kho_scratch_only(void) { }
+static inline void memmap_init_kho_scratch_pages(void) {}
#endif
#endif /* _LINUX_MEMBLOCK_H */
diff --git a/mm/internal.h b/mm/internal.h
index 50c2f590b2d04..a47e5539321b4 100644
--- a/mm/internal.h
+++ b/mm/internal.h
@@ -1121,6 +1121,8 @@ DECLARE_STATIC_KEY_TRUE(deferred_pages);
bool __init deferred_grow_zone(struct zone *zone, unsigned int order);
#endif /* CONFIG_DEFERRED_STRUCT_PAGE_INIT */
+void init_deferred_page(unsigned long pfn, int nid);
+
enum mminit_level {
MMINIT_WARNING,
MMINIT_VERIFY,
diff --git a/mm/memblock.c b/mm/memblock.c
index 3a213e2a485bc..c2633003ed8ea 100644
--- a/mm/memblock.c
+++ b/mm/memblock.c
@@ -946,6 +946,28 @@ __init_memblock void memblock_clear_kho_scratch_only(void)
{
kho_scratch_only = false;
}
+
+void __init_memblock memmap_init_kho_scratch_pages(void)
+{
+ phys_addr_t start, end;
+ unsigned long pfn;
+ int nid;
+ u64 i;
+
+ if (!IS_ENABLED(CONFIG_DEFERRED_STRUCT_PAGE_INIT))
+ return;
+
+ /*
+ * Initialize struct pages for free scratch memory.
+ * The struct pages for reserved scratch memory will be set up in
+ * reserve_bootmem_region()
+ */
+ __for_each_mem_range(i, &memblock.memory, NULL, NUMA_NO_NODE,
+ MEMBLOCK_KHO_SCRATCH, &start, &end, &nid) {
+ for (pfn = PFN_UP(start); pfn < PFN_DOWN(end); pfn++)
+ init_deferred_page(pfn, nid);
+ }
+}
#endif
/**
diff --git a/mm/mm_init.c b/mm/mm_init.c
index 84f14fa12d0dd..1451cb250fd3f 100644
--- a/mm/mm_init.c
+++ b/mm/mm_init.c
@@ -743,7 +743,7 @@ defer_init(int nid, unsigned long pfn, unsigned long end_pfn)
return false;
}
-static void __meminit init_deferred_page(unsigned long pfn, int nid)
+static void __meminit __init_deferred_page(unsigned long pfn, int nid)
{
if (early_page_initialised(pfn, nid))
return;
@@ -763,11 +763,16 @@ static inline bool defer_init(int nid, unsigned long pfn, unsigned long end_pfn)
return false;
}
-static inline void init_deferred_page(unsigned long pfn, int nid)
+static inline void __init_deferred_page(unsigned long pfn, int nid)
{
}
#endif /* CONFIG_DEFERRED_STRUCT_PAGE_INIT */
+void __meminit init_deferred_page(unsigned long pfn, int nid)
+{
+ __init_deferred_page(pfn, nid);
+}
+
/*
* Initialised pages do not have PageReserved set. This function is
* called for each range allocated by the bootmem allocator and
@@ -784,7 +789,7 @@ void __meminit reserve_bootmem_region(phys_addr_t start,
if (pfn_valid(start_pfn)) {
struct page *page = pfn_to_page(start_pfn);
- init_deferred_page(start_pfn, nid);
+ __init_deferred_page(start_pfn, nid);
/*
* no need for atomic set_bit because the struct
--
2.49.0.604.gff1f9ca942-goog
^ permalink raw reply related [flat|nested] 26+ messages in thread
* [PATCH v6 04/14] kexec: add Kexec HandOver (KHO) generation helpers
2025-04-11 5:37 [PATCH v6 00/14] kexec: introduce Kexec HandOver (KHO) Changyuan Lyu
` (2 preceding siblings ...)
2025-04-11 5:37 ` [PATCH v6 03/14] memblock: introduce memmap_init_kho_scratch() Changyuan Lyu
@ 2025-04-11 5:37 ` Changyuan Lyu
2025-04-11 5:37 ` [PATCH v6 05/14] kexec: add KHO parsing support Changyuan Lyu
` (10 subsequent siblings)
14 siblings, 0 replies; 26+ messages in thread
From: Changyuan Lyu @ 2025-04-11 5:37 UTC (permalink / raw)
To: linux-kernel
Cc: akpm, anthony.yznaga, arnd, ashish.kalra, benh, bp,
catalin.marinas, corbet, dave.hansen, devicetree, dwmw2, ebiederm,
graf, hpa, jgowans, kexec, krzk, linux-arm-kernel, linux-doc,
linux-mm, luto, mark.rutland, mingo, pasha.tatashin, pbonzini,
peterz, ptyadav, robh, rostedt, rppt, saravanak, skinsburskii,
tglx, thomas.lendacky, will, x86, Changyuan Lyu
From: Alexander Graf <graf@amazon.com>
Add the infrastructure to generate Kexec HandOver metadata. Kexec
HandOver is a mechanism that allows Linux to preserve state - arbitrary
properties as well as memory locations - across kexec.
It does so using 2 concepts:
1) KHO FDT - Every KHO kexec carries a KHO specific flattened device tree
blob that describes preserved memory regions. Device drivers can
register to KHO to serialize and preserve their states before kexec.
2) Scratch Regions - CMA regions that we allocate in the first kernel.
CMA gives us the guarantee that no handover pages land in those
regions, because handover pages must be at a static physical memory
location. We use these regions as the place to load future kexec
images so that they won't collide with any handover data.
Signed-off-by: Alexander Graf <graf@amazon.com>
Co-developed-by: Pratyush Yadav <ptyadav@amazon.de>
Signed-off-by: Pratyush Yadav <ptyadav@amazon.de>
Co-developed-by: Mike Rapoport (Microsoft) <rppt@kernel.org>
Signed-off-by: Mike Rapoport (Microsoft) <rppt@kernel.org>
Co-developed-by: Changyuan Lyu <changyuanl@google.com>
Signed-off-by: Changyuan Lyu <changyuanl@google.com>
---
MAINTAINERS | 9 +
include/linux/kexec_handover.h | 58 ++++
kernel/Makefile | 1 +
kernel/kexec_handover.c | 557 +++++++++++++++++++++++++++++++++
mm/mm_init.c | 8 +
5 files changed, 633 insertions(+)
create mode 100644 include/linux/kexec_handover.h
create mode 100644 kernel/kexec_handover.c
diff --git a/MAINTAINERS b/MAINTAINERS
index 96b8270495018..5f33e18385cbc 100644
--- a/MAINTAINERS
+++ b/MAINTAINERS
@@ -13117,6 +13117,15 @@ F: include/linux/kexec.h
F: include/uapi/linux/kexec.h
F: kernel/kexec*
+KEXEC HANDOVER (KHO)
+M: Alexander Graf <graf@amazon.com>
+M: Mike Rapoport <rppt@kernel.org>
+M: Changyuan Lyu <changyuanl@google.com>
+L: kexec@lists.infradead.org
+S: Maintained
+F: include/linux/kexec_handover.h
+F: kernel/kexec_handover.c
+
KEYS-ENCRYPTED
M: Mimi Zohar <zohar@linux.ibm.com>
L: linux-integrity@vger.kernel.org
diff --git a/include/linux/kexec_handover.h b/include/linux/kexec_handover.h
new file mode 100644
index 0000000000000..593ea842a5b61
--- /dev/null
+++ b/include/linux/kexec_handover.h
@@ -0,0 +1,58 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+#ifndef LINUX_KEXEC_HANDOVER_H
+#define LINUX_KEXEC_HANDOVER_H
+
+#include <linux/types.h>
+
+struct kho_scratch {
+ phys_addr_t addr;
+ phys_addr_t size;
+};
+
+/* KHO Notifier index */
+enum kho_event {
+ KEXEC_KHO_FINALIZE = 0,
+ KEXEC_KHO_ABORT = 1,
+};
+
+struct notifier_block;
+
+struct kho_serialization;
+
+#ifdef CONFIG_KEXEC_HANDOVER
+bool kho_is_enabled(void);
+
+int kho_add_subtree(struct kho_serialization *ser, const char *name, void *fdt);
+
+int register_kho_notifier(struct notifier_block *nb);
+int unregister_kho_notifier(struct notifier_block *nb);
+
+void kho_memory_init(void);
+#else
+static inline bool kho_is_enabled(void)
+{
+ return false;
+}
+
+static inline int kho_add_subtree(struct kho_serialization *ser,
+ const char *name, void *fdt)
+{
+ return -EOPNOTSUPP;
+}
+
+static inline int register_kho_notifier(struct notifier_block *nb)
+{
+ return -EOPNOTSUPP;
+}
+
+static inline int unregister_kho_notifier(struct notifier_block *nb)
+{
+ return -EOPNOTSUPP;
+}
+
+static inline void kho_memory_init(void)
+{
+}
+#endif /* CONFIG_KEXEC_HANDOVER */
+
+#endif /* LINUX_KEXEC_HANDOVER_H */
diff --git a/kernel/Makefile b/kernel/Makefile
index 434929de17ef2..97c09847db42f 100644
--- a/kernel/Makefile
+++ b/kernel/Makefile
@@ -80,6 +80,7 @@ obj-$(CONFIG_CRASH_DUMP) += crash_core.o
obj-$(CONFIG_KEXEC) += kexec.o
obj-$(CONFIG_KEXEC_FILE) += kexec_file.o
obj-$(CONFIG_KEXEC_ELF) += kexec_elf.o
+obj-$(CONFIG_KEXEC_HANDOVER) += kexec_handover.o
obj-$(CONFIG_BACKTRACE_SELF_TEST) += backtracetest.o
obj-$(CONFIG_COMPAT) += compat.o
obj-$(CONFIG_CGROUPS) += cgroup/
diff --git a/kernel/kexec_handover.c b/kernel/kexec_handover.c
new file mode 100644
index 0000000000000..e541d3d5003d1
--- /dev/null
+++ b/kernel/kexec_handover.c
@@ -0,0 +1,557 @@
+// SPDX-License-Identifier: GPL-2.0-only
+/*
+ * kexec_handover.c - kexec handover metadata processing
+ * Copyright (C) 2023 Alexander Graf <graf@amazon.com>
+ * Copyright (C) 2025 Microsoft Corporation, Mike Rapoport <rppt@kernel.org>
+ * Copyright (C) 2025 Google LLC, Changyuan Lyu <changyuanl@google.com>
+ */
+
+#define pr_fmt(fmt) "KHO: " fmt
+
+#include <linux/cma.h>
+#include <linux/debugfs.h>
+#include <linux/kexec.h>
+#include <linux/kexec_handover.h>
+#include <linux/libfdt.h>
+#include <linux/list.h>
+#include <linux/memblock.h>
+#include <linux/notifier.h>
+#include <linux/page-isolation.h>
+/*
+ * KHO is tightly coupled with mm init and needs access to some of mm
+ * internal APIs.
+ */
+#include "../mm/internal.h"
+
+#define KHO_FDT_COMPATIBLE "kho-v1"
+#define PROP_PRESERVED_MEMORY_MAP "preserved-memory-map"
+#define PROP_SUB_FDT "fdt"
+
+static bool kho_enable __ro_after_init;
+
+bool kho_is_enabled(void)
+{
+ return kho_enable;
+}
+EXPORT_SYMBOL_GPL(kho_is_enabled);
+
+static int __init kho_parse_enable(char *p)
+{
+ return kstrtobool(p, &kho_enable);
+}
+early_param("kho", kho_parse_enable);
+
+struct kho_serialization {
+ struct page *fdt;
+ struct list_head fdt_list;
+ struct dentry *sub_fdt_dir;
+};
+
+/*
+ * With KHO enabled, memory can become fragmented because KHO regions may
+ * be anywhere in physical address space. The scratch regions give us a
+ * safe zones that we will never see KHO allocations from. This is where we
+ * can later safely load our new kexec images into and then use the scratch
+ * area for early allocations that happen before page allocator is
+ * initialized.
+ */
+static struct kho_scratch *kho_scratch;
+static unsigned int kho_scratch_cnt;
+
+/*
+ * The scratch areas are scaled by default as percent of memory allocated from
+ * memblock. A user can override the scale with command line parameter:
+ *
+ * kho_scratch=N%
+ *
+ * It is also possible to explicitly define size for a lowmem, a global and
+ * per-node scratch areas:
+ *
+ * kho_scratch=l[KMG],n[KMG],m[KMG]
+ *
+ * The explicit size definition takes precedence over scale definition.
+ */
+static unsigned int scratch_scale __initdata = 200;
+static phys_addr_t scratch_size_global __initdata;
+static phys_addr_t scratch_size_pernode __initdata;
+static phys_addr_t scratch_size_lowmem __initdata;
+
+static int __init kho_parse_scratch_size(char *p)
+{
+ size_t len;
+ unsigned long sizes[3];
+ int i;
+
+ if (!p)
+ return -EINVAL;
+
+ len = strlen(p);
+ if (!len)
+ return -EINVAL;
+
+ /* parse nn% */
+ if (p[len - 1] == '%') {
+ /* unsigned int max is 4,294,967,295, 10 chars */
+ char s_scale[11] = {};
+ int ret = 0;
+
+ if (len > ARRAY_SIZE(s_scale))
+ return -EINVAL;
+
+ memcpy(s_scale, p, len - 1);
+ ret = kstrtouint(s_scale, 10, &scratch_scale);
+ if (!ret)
+ pr_notice("scratch scale is %d%%\n", scratch_scale);
+ return ret;
+ }
+
+ /* parse ll[KMG],mm[KMG],nn[KMG] */
+ for (i = 0; i < ARRAY_SIZE(sizes); i++) {
+ char *endp = p;
+
+ if (i > 0) {
+ if (*p != ',')
+ return -EINVAL;
+ p += 1;
+ }
+
+ sizes[i] = memparse(p, &endp);
+ if (!sizes[i] || endp == p)
+ return -EINVAL;
+ p = endp;
+ }
+
+ scratch_size_lowmem = sizes[0];
+ scratch_size_global = sizes[1];
+ scratch_size_pernode = sizes[2];
+ scratch_scale = 0;
+
+ pr_notice("scratch areas: lowmem: %lluMiB global: %lluMiB pernode: %lldMiB\n",
+ (u64)(scratch_size_lowmem >> 20),
+ (u64)(scratch_size_global >> 20),
+ (u64)(scratch_size_pernode >> 20));
+
+ return 0;
+}
+early_param("kho_scratch", kho_parse_scratch_size);
+
+static void __init scratch_size_update(void)
+{
+ phys_addr_t size;
+
+ if (!scratch_scale)
+ return;
+
+ size = memblock_reserved_kern_size(ARCH_LOW_ADDRESS_LIMIT,
+ NUMA_NO_NODE);
+ size = size * scratch_scale / 100;
+ scratch_size_lowmem = round_up(size, CMA_MIN_ALIGNMENT_BYTES);
+
+ size = memblock_reserved_kern_size(MEMBLOCK_ALLOC_ANYWHERE,
+ NUMA_NO_NODE);
+ size = size * scratch_scale / 100 - scratch_size_lowmem;
+ scratch_size_global = round_up(size, CMA_MIN_ALIGNMENT_BYTES);
+}
+
+static phys_addr_t __init scratch_size_node(int nid)
+{
+ phys_addr_t size;
+
+ if (scratch_scale) {
+ size = memblock_reserved_kern_size(MEMBLOCK_ALLOC_ANYWHERE,
+ nid);
+ size = size * scratch_scale / 100;
+ } else {
+ size = scratch_size_pernode;
+ }
+
+ return round_up(size, CMA_MIN_ALIGNMENT_BYTES);
+}
+
+/**
+ * kho_reserve_scratch - Reserve a contiguous chunk of memory for kexec
+ *
+ * With KHO we can preserve arbitrary pages in the system. To ensure we still
+ * have a large contiguous region of memory when we search the physical address
+ * space for target memory, let's make sure we always have a large CMA region
+ * active. This CMA region will only be used for movable pages which are not a
+ * problem for us during KHO because we can just move them somewhere else.
+ */
+static void __init kho_reserve_scratch(void)
+{
+ phys_addr_t addr, size;
+ int nid, i = 0;
+
+ if (!kho_enable)
+ return;
+
+ scratch_size_update();
+
+ /* FIXME: deal with node hot-plug/remove */
+ kho_scratch_cnt = num_online_nodes() + 2;
+ size = kho_scratch_cnt * sizeof(*kho_scratch);
+ kho_scratch = memblock_alloc(size, PAGE_SIZE);
+ if (!kho_scratch)
+ goto err_disable_kho;
+
+ /*
+ * reserve scratch area in low memory for lowmem allocations in the
+ * next kernel
+ */
+ size = scratch_size_lowmem;
+ addr = memblock_phys_alloc_range(size, CMA_MIN_ALIGNMENT_BYTES, 0,
+ ARCH_LOW_ADDRESS_LIMIT);
+ if (!addr)
+ goto err_free_scratch_desc;
+
+ kho_scratch[i].addr = addr;
+ kho_scratch[i].size = size;
+ i++;
+
+ /* reserve large contiguous area for allocations without nid */
+ size = scratch_size_global;
+ addr = memblock_phys_alloc(size, CMA_MIN_ALIGNMENT_BYTES);
+ if (!addr)
+ goto err_free_scratch_areas;
+
+ kho_scratch[i].addr = addr;
+ kho_scratch[i].size = size;
+ i++;
+
+ for_each_online_node(nid) {
+ size = scratch_size_node(nid);
+ addr = memblock_alloc_range_nid(size, CMA_MIN_ALIGNMENT_BYTES,
+ 0, MEMBLOCK_ALLOC_ACCESSIBLE,
+ nid, true);
+ if (!addr)
+ goto err_free_scratch_areas;
+
+ kho_scratch[i].addr = addr;
+ kho_scratch[i].size = size;
+ i++;
+ }
+
+ return;
+
+err_free_scratch_areas:
+ for (i--; i >= 0; i--)
+ memblock_phys_free(kho_scratch[i].addr, kho_scratch[i].size);
+err_free_scratch_desc:
+ memblock_free(kho_scratch, kho_scratch_cnt * sizeof(*kho_scratch));
+err_disable_kho:
+ kho_enable = false;
+}
+
+struct fdt_debugfs {
+ struct list_head list;
+ struct debugfs_blob_wrapper wrapper;
+ struct dentry *file;
+};
+
+static int kho_debugfs_fdt_add(struct list_head *list, struct dentry *dir,
+ const char *name, const void *fdt)
+{
+ struct fdt_debugfs *f;
+ struct dentry *file;
+
+ f = kmalloc(sizeof(*f), GFP_KERNEL);
+ if (!f)
+ return -ENOMEM;
+
+ f->wrapper.data = (void *)fdt;
+ f->wrapper.size = fdt_totalsize(fdt);
+
+ file = debugfs_create_blob(name, 0400, dir, &f->wrapper);
+ if (IS_ERR(file)) {
+ kfree(f);
+ return PTR_ERR(file);
+ }
+
+ f->file = file;
+ list_add(&f->list, list);
+
+ return 0;
+}
+
+/**
+ * kho_add_subtree - record the physical address of a sub FDT in KHO root tree.
+ * @ser: serialization control object passed by KHO notifiers.
+ * @name: name of the sub tree.
+ * @fdt: the sub tree blob.
+ *
+ * Creates a new child node named @name in KHO root FDT and records
+ * the physical address of @fdt. The pages of @fdt must also be preserved
+ * by KHO for the new kernel to retrieve it after kexec.
+ *
+ * A debugfs blob entry is also created at
+ * ``/sys/kernel/debug/kho/out/sub_fdts/@name``.
+ *
+ * Return: 0 on success, error code on failure
+ */
+int kho_add_subtree(struct kho_serialization *ser, const char *name, void *fdt)
+{
+ int err = 0;
+ u64 phys = (u64)virt_to_phys(fdt);
+ void *root = page_to_virt(ser->fdt);
+
+ err |= fdt_begin_node(root, name);
+ err |= fdt_property(root, PROP_SUB_FDT, &phys, sizeof(phys));
+ err |= fdt_end_node(root);
+
+ if (err)
+ return err;
+
+ return kho_debugfs_fdt_add(&ser->fdt_list, ser->sub_fdt_dir, name, fdt);
+}
+EXPORT_SYMBOL_GPL(kho_add_subtree);
+
+struct kho_out {
+ struct blocking_notifier_head chain_head;
+
+ struct dentry *dir;
+
+ struct mutex lock; /* protects KHO FDT finalization */
+
+ struct kho_serialization ser;
+ bool finalized;
+};
+
+static struct kho_out kho_out = {
+ .chain_head = BLOCKING_NOTIFIER_INIT(kho_out.chain_head),
+ .lock = __MUTEX_INITIALIZER(kho_out.lock),
+ .ser = {
+ .fdt_list = LIST_HEAD_INIT(kho_out.ser.fdt_list),
+ },
+ .finalized = false,
+};
+
+int register_kho_notifier(struct notifier_block *nb)
+{
+ return blocking_notifier_chain_register(&kho_out.chain_head, nb);
+}
+EXPORT_SYMBOL_GPL(register_kho_notifier);
+
+int unregister_kho_notifier(struct notifier_block *nb)
+{
+ return blocking_notifier_chain_unregister(&kho_out.chain_head, nb);
+}
+EXPORT_SYMBOL_GPL(unregister_kho_notifier);
+
+/* Handling for debug/kho/out */
+
+static struct dentry *debugfs_root;
+
+static int kho_out_update_debugfs_fdt(void)
+{
+ int err = 0;
+ struct fdt_debugfs *ff, *tmp;
+
+ if (kho_out.finalized) {
+ err = kho_debugfs_fdt_add(&kho_out.ser.fdt_list, kho_out.dir,
+ "fdt", page_to_virt(kho_out.ser.fdt));
+ } else {
+ list_for_each_entry_safe(ff, tmp, &kho_out.ser.fdt_list, list) {
+ debugfs_remove(ff->file);
+ list_del(&ff->list);
+ kfree(ff);
+ }
+ }
+
+ return err;
+}
+
+static int kho_abort(void)
+{
+ int err;
+
+ err = blocking_notifier_call_chain(&kho_out.chain_head, KEXEC_KHO_ABORT,
+ NULL);
+ err = notifier_to_errno(err);
+
+ if (err)
+ pr_err("Failed to abort KHO finalization: %d\n", err);
+
+ return err;
+}
+
+static int kho_finalize(void)
+{
+ int err = 0;
+ void *fdt = page_to_virt(kho_out.ser.fdt);
+
+ err |= fdt_create(fdt, PAGE_SIZE);
+ err |= fdt_finish_reservemap(fdt);
+ err |= fdt_begin_node(fdt, "");
+ err |= fdt_property_string(fdt, "compatible", KHO_FDT_COMPATIBLE);
+ if (err)
+ goto abort;
+
+ err = blocking_notifier_call_chain(&kho_out.chain_head,
+ KEXEC_KHO_FINALIZE, &kho_out.ser);
+ err = notifier_to_errno(err);
+ if (err)
+ goto abort;
+
+ err |= fdt_end_node(fdt);
+ err |= fdt_finish(fdt);
+
+abort:
+ if (err) {
+ pr_err("Failed to convert KHO state tree: %d\n", err);
+ kho_abort();
+ }
+
+ return err;
+}
+
+static int kho_out_finalize_get(void *data, u64 *val)
+{
+ mutex_lock(&kho_out.lock);
+ *val = kho_out.finalized;
+ mutex_unlock(&kho_out.lock);
+
+ return 0;
+}
+
+static int kho_out_finalize_set(void *data, u64 _val)
+{
+ int ret = 0;
+ bool val = !!_val;
+
+ mutex_lock(&kho_out.lock);
+
+ if (val == kho_out.finalized) {
+ if (kho_out.finalized)
+ ret = -EEXIST;
+ else
+ ret = -ENOENT;
+ goto unlock;
+ }
+
+ if (val)
+ ret = kho_finalize();
+ else
+ ret = kho_abort();
+
+ if (ret)
+ goto unlock;
+
+ kho_out.finalized = val;
+ ret = kho_out_update_debugfs_fdt();
+
+unlock:
+ mutex_unlock(&kho_out.lock);
+ return ret;
+}
+
+DEFINE_DEBUGFS_ATTRIBUTE(fops_kho_out_finalize, kho_out_finalize_get,
+ kho_out_finalize_set, "%llu\n");
+
+static int scratch_phys_show(struct seq_file *m, void *v)
+{
+ for (int i = 0; i < kho_scratch_cnt; i++)
+ seq_printf(m, "0x%llx\n", kho_scratch[i].addr);
+
+ return 0;
+}
+DEFINE_SHOW_ATTRIBUTE(scratch_phys);
+
+static int scratch_len_show(struct seq_file *m, void *v)
+{
+ for (int i = 0; i < kho_scratch_cnt; i++)
+ seq_printf(m, "0x%llx\n", kho_scratch[i].size);
+
+ return 0;
+}
+DEFINE_SHOW_ATTRIBUTE(scratch_len);
+
+static __init int kho_out_debugfs_init(void)
+{
+ struct dentry *dir, *f, *sub_fdt_dir;
+
+ dir = debugfs_create_dir("out", debugfs_root);
+ if (IS_ERR(dir))
+ return -ENOMEM;
+
+ sub_fdt_dir = debugfs_create_dir("sub_fdts", dir);
+ if (IS_ERR(sub_fdt_dir))
+ goto err_rmdir;
+
+ f = debugfs_create_file("scratch_phys", 0400, dir, NULL,
+ &scratch_phys_fops);
+ if (IS_ERR(f))
+ goto err_rmdir;
+
+ f = debugfs_create_file("scratch_len", 0400, dir, NULL,
+ &scratch_len_fops);
+ if (IS_ERR(f))
+ goto err_rmdir;
+
+ f = debugfs_create_file("finalize", 0600, dir, NULL,
+ &fops_kho_out_finalize);
+ if (IS_ERR(f))
+ goto err_rmdir;
+
+ kho_out.dir = dir;
+ kho_out.ser.sub_fdt_dir = sub_fdt_dir;
+ return 0;
+
+err_rmdir:
+ debugfs_remove_recursive(dir);
+ return -ENOENT;
+}
+
+static __init int kho_init(void)
+{
+ int err = 0;
+
+ if (!kho_enable)
+ return 0;
+
+ kho_out.ser.fdt = alloc_page(GFP_KERNEL);
+ if (!kho_out.ser.fdt) {
+ err = -ENOMEM;
+ goto err_free_scratch;
+ }
+
+ debugfs_root = debugfs_create_dir("kho", NULL);
+ if (IS_ERR(debugfs_root)) {
+ err = -ENOENT;
+ goto err_free_fdt;
+ }
+
+ err = kho_out_debugfs_init();
+ if (err)
+ goto err_free_fdt;
+
+ for (int i = 0; i < kho_scratch_cnt; i++) {
+ unsigned long base_pfn = PHYS_PFN(kho_scratch[i].addr);
+ unsigned long count = kho_scratch[i].size >> PAGE_SHIFT;
+ unsigned long pfn;
+
+ for (pfn = base_pfn; pfn < base_pfn + count;
+ pfn += pageblock_nr_pages)
+ init_cma_reserved_pageblock(pfn_to_page(pfn));
+ }
+
+ return 0;
+
+err_free_fdt:
+ put_page(kho_out.ser.fdt);
+ kho_out.ser.fdt = NULL;
+err_free_scratch:
+ for (int i = 0; i < kho_scratch_cnt; i++) {
+ void *start = __va(kho_scratch[i].addr);
+ void *end = start + kho_scratch[i].size;
+
+ free_reserved_area(start, end, -1, "");
+ }
+ kho_enable = false;
+ return err;
+}
+late_initcall(kho_init);
+
+void __init kho_memory_init(void)
+{
+ kho_reserve_scratch();
+}
diff --git a/mm/mm_init.c b/mm/mm_init.c
index 1451cb250fd3f..9ed4a0dda6e39 100644
--- a/mm/mm_init.c
+++ b/mm/mm_init.c
@@ -30,6 +30,7 @@
#include <linux/crash_dump.h>
#include <linux/execmem.h>
#include <linux/vmstat.h>
+#include <linux/kexec_handover.h>
#include <linux/hugetlb.h>
#include "internal.h"
#include "slab.h"
@@ -2770,6 +2771,13 @@ void __init mm_core_init(void)
report_meminit();
kmsan_init_shadow();
stack_depot_early_init();
+
+ /*
+ * KHO memory setup must happen while memblock is still active, but
+ * as close as possible to buddy initialization
+ */
+ kho_memory_init();
+
memblock_free_all();
mem_init();
kmem_cache_init();
--
2.49.0.604.gff1f9ca942-goog
^ permalink raw reply related [flat|nested] 26+ messages in thread
* [PATCH v6 05/14] kexec: add KHO parsing support
2025-04-11 5:37 [PATCH v6 00/14] kexec: introduce Kexec HandOver (KHO) Changyuan Lyu
` (3 preceding siblings ...)
2025-04-11 5:37 ` [PATCH v6 04/14] kexec: add Kexec HandOver (KHO) generation helpers Changyuan Lyu
@ 2025-04-11 5:37 ` Changyuan Lyu
2025-04-11 5:37 ` [PATCH v6 06/14] kexec: enable KHO support for memory preservation Changyuan Lyu
` (9 subsequent siblings)
14 siblings, 0 replies; 26+ messages in thread
From: Changyuan Lyu @ 2025-04-11 5:37 UTC (permalink / raw)
To: linux-kernel
Cc: akpm, anthony.yznaga, arnd, ashish.kalra, benh, bp,
catalin.marinas, corbet, dave.hansen, devicetree, dwmw2, ebiederm,
graf, hpa, jgowans, kexec, krzk, linux-arm-kernel, linux-doc,
linux-mm, luto, mark.rutland, mingo, pasha.tatashin, pbonzini,
peterz, ptyadav, robh, rostedt, rppt, saravanak, skinsburskii,
tglx, thomas.lendacky, will, x86, Changyuan Lyu
From: Alexander Graf <graf@amazon.com>
When we have a KHO kexec, we get an FDT blob and scratch region to
populate the state of the system. Provide helper functions that allow
architecture code to easily handle memory reservations based on them and
give device drivers visibility into the KHO FDT and memory reservations
so they can recover their own state.
Signed-off-by: Alexander Graf <graf@amazon.com>
Co-developed-by: Mike Rapoport (Microsoft) <rppt@kernel.org>
Signed-off-by: Mike Rapoport (Microsoft) <rppt@kernel.org>
Co-developed-by: Changyuan Lyu <changyuanl@google.com>
Signed-off-by: Changyuan Lyu <changyuanl@google.com>
---
include/linux/kexec_handover.h | 14 ++
kernel/kexec_handover.c | 230 ++++++++++++++++++++++++++++++++-
mm/memblock.c | 1 +
3 files changed, 244 insertions(+), 1 deletion(-)
diff --git a/include/linux/kexec_handover.h b/include/linux/kexec_handover.h
index 593ea842a5b61..2b77d4b9fbc3c 100644
--- a/include/linux/kexec_handover.h
+++ b/include/linux/kexec_handover.h
@@ -23,11 +23,15 @@ struct kho_serialization;
bool kho_is_enabled(void);
int kho_add_subtree(struct kho_serialization *ser, const char *name, void *fdt);
+int kho_retrieve_subtree(const char *name, phys_addr_t *phys);
int register_kho_notifier(struct notifier_block *nb);
int unregister_kho_notifier(struct notifier_block *nb);
void kho_memory_init(void);
+
+void kho_populate(phys_addr_t fdt_phys, u64 fdt_len, phys_addr_t scratch_phys,
+ u64 scratch_len);
#else
static inline bool kho_is_enabled(void)
{
@@ -40,6 +44,11 @@ static inline int kho_add_subtree(struct kho_serialization *ser,
return -EOPNOTSUPP;
}
+static inline int kho_retrieve_subtree(const char *name, phys_addr_t *phys)
+{
+ return -EOPNOTSUPP;
+}
+
static inline int register_kho_notifier(struct notifier_block *nb)
{
return -EOPNOTSUPP;
@@ -53,6 +62,11 @@ static inline int unregister_kho_notifier(struct notifier_block *nb)
static inline void kho_memory_init(void)
{
}
+
+static inline void kho_populate(phys_addr_t fdt_phys, u64 fdt_len,
+ phys_addr_t scratch_phys, u64 scratch_len)
+{
+}
#endif /* CONFIG_KEXEC_HANDOVER */
#endif /* LINUX_KEXEC_HANDOVER_H */
diff --git a/kernel/kexec_handover.c b/kernel/kexec_handover.c
index e541d3d5003d1..a1e1cd0330143 100644
--- a/kernel/kexec_handover.c
+++ b/kernel/kexec_handover.c
@@ -501,9 +501,112 @@ static __init int kho_out_debugfs_init(void)
return -ENOENT;
}
+struct kho_in {
+ struct dentry *dir;
+ phys_addr_t fdt_phys;
+ phys_addr_t scratch_phys;
+ struct list_head fdt_list;
+};
+
+static struct kho_in kho_in = {
+ .fdt_list = LIST_HEAD_INIT(kho_in.fdt_list),
+};
+
+static const void *kho_get_fdt(void)
+{
+ return kho_in.fdt_phys ? phys_to_virt(kho_in.fdt_phys) : NULL;
+}
+
+/**
+ * kho_retrieve_subtree - retrieve a preserved sub FDT by its name.
+ * @name: the name of the sub FDT passed to kho_add_subtree().
+ * @phys: if found, the physical address of the sub FDT is stored in @phys.
+ *
+ * Retrieve a preserved sub FDT named @name and store its physical
+ * address in @phys.
+ *
+ * Return: 0 on success, error code on failure
+ */
+int kho_retrieve_subtree(const char *name, phys_addr_t *phys)
+{
+ const void *fdt = kho_get_fdt();
+ const u64 *val;
+ int offset, len;
+
+ if (!fdt)
+ return -ENOENT;
+
+ if (!phys)
+ return -EINVAL;
+
+ offset = fdt_subnode_offset(fdt, 0, name);
+ if (offset < 0)
+ return -ENOENT;
+
+ val = fdt_getprop(fdt, offset, PROP_SUB_FDT, &len);
+ if (!val || len != sizeof(*val))
+ return -EINVAL;
+
+ *phys = (phys_addr_t)*val;
+
+ return 0;
+}
+EXPORT_SYMBOL_GPL(kho_retrieve_subtree);
+
+/* Handling for debugfs/kho/in */
+
+static __init int kho_in_debugfs_init(const void *fdt)
+{
+ struct dentry *sub_fdt_dir;
+ int err, child;
+
+ kho_in.dir = debugfs_create_dir("in", debugfs_root);
+ if (IS_ERR(kho_in.dir))
+ return PTR_ERR(kho_in.dir);
+
+ sub_fdt_dir = debugfs_create_dir("sub_fdts", kho_in.dir);
+ if (IS_ERR(sub_fdt_dir)) {
+ err = PTR_ERR(sub_fdt_dir);
+ goto err_rmdir;
+ }
+
+ err = kho_debugfs_fdt_add(&kho_in.fdt_list, kho_in.dir, "fdt", fdt);
+ if (err)
+ goto err_rmdir;
+
+ fdt_for_each_subnode(child, fdt, 0) {
+ int len = 0;
+ const char *name = fdt_get_name(fdt, child, NULL);
+ const u64 *fdt_phys;
+
+ fdt_phys = fdt_getprop(fdt, child, "fdt", &len);
+ if (!fdt_phys)
+ continue;
+ if (len != sizeof(*fdt_phys)) {
+ pr_warn("node `%s`'s prop `fdt` has invalid length: %d\n",
+ name, len);
+ continue;
+ }
+ err = kho_debugfs_fdt_add(&kho_in.fdt_list, sub_fdt_dir, name,
+ phys_to_virt(*fdt_phys));
+ if (err) {
+ pr_warn("failed to add fdt `%s` to debugfs: %d\n", name,
+ err);
+ continue;
+ }
+ }
+
+ return 0;
+
+err_rmdir:
+ debugfs_remove_recursive(kho_in.dir);
+ return err;
+}
+
static __init int kho_init(void)
{
int err = 0;
+ const void *fdt = kho_get_fdt();
if (!kho_enable)
return 0;
@@ -524,6 +627,20 @@ static __init int kho_init(void)
if (err)
goto err_free_fdt;
+ if (fdt) {
+ err = kho_in_debugfs_init(fdt);
+ /*
+ * Failure to create /sys/kernel/debug/kho/in does not prevent
+ * reviving state from KHO and setting up KHO for the next
+ * kexec.
+ */
+ if (err)
+ pr_err("failed exposing handover FDT in debugfs: %d\n",
+ err);
+
+ return 0;
+ }
+
for (int i = 0; i < kho_scratch_cnt; i++) {
unsigned long base_pfn = PHYS_PFN(kho_scratch[i].addr);
unsigned long count = kho_scratch[i].size >> PAGE_SHIFT;
@@ -551,7 +668,118 @@ static __init int kho_init(void)
}
late_initcall(kho_init);
+static void __init kho_release_scratch(void)
+{
+ phys_addr_t start, end;
+ u64 i;
+
+ memmap_init_kho_scratch_pages();
+
+ /*
+ * Mark scratch mem as CMA before we return it. That way we
+ * ensure that no kernel allocations happen on it. That means
+ * we can reuse it as scratch memory again later.
+ */
+ __for_each_mem_range(i, &memblock.memory, NULL, NUMA_NO_NODE,
+ MEMBLOCK_KHO_SCRATCH, &start, &end, NULL) {
+ ulong start_pfn = pageblock_start_pfn(PFN_DOWN(start));
+ ulong end_pfn = pageblock_align(PFN_UP(end));
+ ulong pfn;
+
+ for (pfn = start_pfn; pfn < end_pfn; pfn += pageblock_nr_pages)
+ set_pageblock_migratetype(pfn_to_page(pfn),
+ MIGRATE_CMA);
+ }
+}
+
void __init kho_memory_init(void)
{
- kho_reserve_scratch();
+ if (kho_in.scratch_phys) {
+ kho_scratch = phys_to_virt(kho_in.scratch_phys);
+ kho_release_scratch();
+ } else {
+ kho_reserve_scratch();
+ }
+}
+
+void __init kho_populate(phys_addr_t fdt_phys, u64 fdt_len,
+ phys_addr_t scratch_phys, u64 scratch_len)
+{
+ void *fdt = NULL;
+ struct kho_scratch *scratch = NULL;
+ int err = 0;
+ unsigned int scratch_cnt = scratch_len / sizeof(*kho_scratch);
+
+ /* Validate the input FDT */
+ fdt = early_memremap(fdt_phys, fdt_len);
+ if (!fdt) {
+ pr_warn("setup: failed to memremap FDT (0x%llx)\n", fdt_phys);
+ err = -EFAULT;
+ goto out;
+ }
+ err = fdt_check_header(fdt);
+ if (err) {
+ pr_warn("setup: handover FDT (0x%llx) is invalid: %d\n",
+ fdt_phys, err);
+ err = -EINVAL;
+ goto out;
+ }
+ err = fdt_node_check_compatible(fdt, 0, KHO_FDT_COMPATIBLE);
+ if (err) {
+ pr_warn("setup: handover FDT (0x%llx) is incompatible with '%s': %d\n",
+ fdt_phys, KHO_FDT_COMPATIBLE, err);
+ err = -EINVAL;
+ goto out;
+ }
+
+ scratch = early_memremap(scratch_phys, scratch_len);
+ if (!scratch) {
+ pr_warn("setup: failed to memremap scratch (phys=0x%llx, len=%lld)\n",
+ scratch_phys, scratch_len);
+ err = -EFAULT;
+ goto out;
+ }
+
+ /*
+ * We pass a safe contiguous blocks of memory to use for early boot
+ * purporses from the previous kernel so that we can resize the
+ * memblock array as needed.
+ */
+ for (int i = 0; i < scratch_cnt; i++) {
+ struct kho_scratch *area = &scratch[i];
+ u64 size = area->size;
+
+ memblock_add(area->addr, size);
+ err = memblock_mark_kho_scratch(area->addr, size);
+ if (WARN_ON(err)) {
+ pr_warn("failed to mark the scratch region 0x%pa+0x%pa: %d",
+ &area->addr, &size, err);
+ goto out;
+ }
+ pr_debug("Marked 0x%pa+0x%pa as scratch", &area->addr, &size);
+ }
+
+ memblock_reserve(scratch_phys, scratch_len);
+
+ /*
+ * Now that we have a viable region of scratch memory, let's tell
+ * the memblocks allocator to only use that for any allocations.
+ * That way we ensure that nothing scribbles over in use data while
+ * we initialize the page tables which we will need to ingest all
+ * memory reservations from the previous kernel.
+ */
+ memblock_set_kho_scratch_only();
+
+ kho_in.fdt_phys = fdt_phys;
+ kho_in.scratch_phys = scratch_phys;
+ kho_scratch_cnt = scratch_cnt;
+ pr_info("found kexec handover data. Will skip init for some devices\n");
+
+out:
+ if (fdt)
+ early_memunmap(fdt, fdt_len);
+ if (scratch)
+ early_memunmap(scratch, scratch_len);
+ if (err)
+ pr_warn("disabling KHO revival: %d\n", err);
}
diff --git a/mm/memblock.c b/mm/memblock.c
index c2633003ed8ea..456689cb73e20 100644
--- a/mm/memblock.c
+++ b/mm/memblock.c
@@ -2377,6 +2377,7 @@ void __init memblock_free_all(void)
free_unused_memmap();
reset_all_zones_managed_pages();
+ memblock_clear_kho_scratch_only();
pages = free_low_memory_core_early();
totalram_pages_add(pages);
}
--
2.49.0.604.gff1f9ca942-goog
^ permalink raw reply related [flat|nested] 26+ messages in thread
* [PATCH v6 06/14] kexec: enable KHO support for memory preservation
2025-04-11 5:37 [PATCH v6 00/14] kexec: introduce Kexec HandOver (KHO) Changyuan Lyu
` (4 preceding siblings ...)
2025-04-11 5:37 ` [PATCH v6 05/14] kexec: add KHO parsing support Changyuan Lyu
@ 2025-04-11 5:37 ` Changyuan Lyu
2025-04-11 5:37 ` [PATCH v6 07/14] kexec: add KHO support to kexec file loads Changyuan Lyu
` (8 subsequent siblings)
14 siblings, 0 replies; 26+ messages in thread
From: Changyuan Lyu @ 2025-04-11 5:37 UTC (permalink / raw)
To: linux-kernel
Cc: akpm, anthony.yznaga, arnd, ashish.kalra, benh, bp,
catalin.marinas, corbet, dave.hansen, devicetree, dwmw2, ebiederm,
graf, hpa, jgowans, kexec, krzk, linux-arm-kernel, linux-doc,
linux-mm, luto, mark.rutland, mingo, pasha.tatashin, pbonzini,
peterz, ptyadav, robh, rostedt, rppt, saravanak, skinsburskii,
tglx, thomas.lendacky, will, x86, Jason Gunthorpe, Changyuan Lyu
From: "Mike Rapoport (Microsoft)" <rppt@kernel.org>
Introduce APIs allowing KHO users to preserve memory across kexec and
get access to that memory after boot of the kexeced kernel
kho_preserve_folio() - record a folio to be preserved over kexec
kho_restore_folio() - recreates the folio from the preserved memory
kho_preserve_phys() - record physically contiguous range to be
preserved over kexec.
The memory preservations are tracked by two levels of xarrays to manage
chunks of per-order 512 byte bitmaps. For instance if PAGE_SIZE = 4096,
the entire 1G order of a 1TB x86 system would fit inside a single 512
byte bitmap. For order 0 allocations each bitmap will cover 16M of
address space. Thus, for 16G of memory at most 512K of bitmap memory
will be needed for order 0.
At serialization time all bitmaps are recorded in a linked list of pages
for the next kernel to process and the physical address of the list is
recorded in KHO FDT.
The next kernel then processes that list, reserves the memory ranges and
later, when a user requests a folio or a physical range, KHO restores
corresponding memory map entries.
Suggested-by: Jason Gunthorpe <jgg@nvidia.com>
Signed-off-by: Mike Rapoport (Microsoft) <rppt@kernel.org>
Co-developed-by: Changyuan Lyu <changyuanl@google.com>
Signed-off-by: Changyuan Lyu <changyuanl@google.com>
---
include/linux/kexec_handover.h | 39 ++++
kernel/kexec_handover.c | 401 +++++++++++++++++++++++++++++++++
2 files changed, 440 insertions(+)
diff --git a/include/linux/kexec_handover.h b/include/linux/kexec_handover.h
index 2b77d4b9fbc3c..d96ba4fb27704 100644
--- a/include/linux/kexec_handover.h
+++ b/include/linux/kexec_handover.h
@@ -15,13 +15,35 @@ enum kho_event {
KEXEC_KHO_ABORT = 1,
};
+struct folio;
struct notifier_block;
+#define DECLARE_KHOSER_PTR(name, type) \
+ union { \
+ phys_addr_t phys; \
+ type ptr; \
+ } name
+#define KHOSER_STORE_PTR(dest, val) \
+ ({ \
+ typeof(val) v = val; \
+ typecheck(typeof((dest).ptr), v); \
+ (dest).phys = virt_to_phys(v); \
+ })
+#define KHOSER_LOAD_PTR(src) \
+ ({ \
+ typeof(src) s = src; \
+ (typeof((s).ptr))((s).phys ? phys_to_virt((s).phys) : NULL); \
+ })
+
struct kho_serialization;
#ifdef CONFIG_KEXEC_HANDOVER
bool kho_is_enabled(void);
+int kho_preserve_folio(struct kho_serialization *ser, struct folio *folio);
+int kho_preserve_phys(struct kho_serialization *ser, phys_addr_t phys,
+ size_t size);
+struct folio *kho_restore_folio(phys_addr_t phys);
int kho_add_subtree(struct kho_serialization *ser, const char *name, void *fdt);
int kho_retrieve_subtree(const char *name, phys_addr_t *phys);
@@ -38,6 +60,23 @@ static inline bool kho_is_enabled(void)
return false;
}
+static inline int kho_preserve_folio(struct kho_serialization *ser,
+ struct folio *folio)
+{
+ return -EOPNOTSUPP;
+}
+
+static inline int kho_preserve_phys(struct kho_serialization *ser,
+ phys_addr_t phys, size_t size)
+{
+ return -EOPNOTSUPP;
+}
+
+static inline struct folio *kho_restore_folio(phys_addr_t phys)
+{
+ return NULL;
+}
+
static inline int kho_add_subtree(struct kho_serialization *ser,
const char *name, void *fdt)
{
diff --git a/kernel/kexec_handover.c b/kernel/kexec_handover.c
index a1e1cd0330143..a594da2a22912 100644
--- a/kernel/kexec_handover.c
+++ b/kernel/kexec_handover.c
@@ -9,6 +9,7 @@
#define pr_fmt(fmt) "KHO: " fmt
#include <linux/cma.h>
+#include <linux/count_zeros.h>
#include <linux/debugfs.h>
#include <linux/kexec.h>
#include <linux/kexec_handover.h>
@@ -41,12 +42,369 @@ static int __init kho_parse_enable(char *p)
}
early_param("kho", kho_parse_enable);
+/*
+ * Keep track of memory that is to be preserved across KHO.
+ *
+ * The serializing side uses two levels of xarrays to manage chunks of per-order
+ * 512 byte bitmaps. For instance if PAGE_SIZE = 4096, the entire 1G order of a
+ * 1TB system would fit inside a single 512 byte bitmap. For order 0 allocations
+ * each bitmap will cover 16M of address space. Thus, for 16G of memory at most
+ * 512K of bitmap memory will be needed for order 0.
+ *
+ * This approach is fully incremental, as the serialization progresses folios
+ * can continue be aggregated to the tracker. The final step, immediately prior
+ * to kexec would serialize the xarray information into a linked list for the
+ * successor kernel to parse.
+ */
+
+#define PRESERVE_BITS (512 * 8)
+
+struct kho_mem_phys_bits {
+ DECLARE_BITMAP(preserve, PRESERVE_BITS);
+};
+
+struct kho_mem_phys {
+ /*
+ * Points to kho_mem_phys_bits, a sparse bitmap array. Each bit is sized
+ * to order.
+ */
+ struct xarray phys_bits;
+};
+
+struct kho_mem_track {
+ /* Points to kho_mem_phys, each order gets its own bitmap tree */
+ struct xarray orders;
+};
+
+struct khoser_mem_chunk;
+
struct kho_serialization {
struct page *fdt;
struct list_head fdt_list;
struct dentry *sub_fdt_dir;
+ struct kho_mem_track track;
+ /* First chunk of serialized preserved memory map */
+ struct khoser_mem_chunk *preserved_mem_map;
+};
+
+static void *xa_load_or_alloc(struct xarray *xa, unsigned long index, size_t sz)
+{
+ void *elm, *res;
+
+ elm = xa_load(xa, index);
+ if (elm)
+ return elm;
+
+ elm = kzalloc(sz, GFP_KERNEL);
+ if (!elm)
+ return ERR_PTR(-ENOMEM);
+
+ res = xa_cmpxchg(xa, index, NULL, elm, GFP_KERNEL);
+ if (xa_is_err(res))
+ res = ERR_PTR(xa_err(res));
+
+ if (res) {
+ kfree(elm);
+ return res;
+ }
+
+ return elm;
+}
+
+static void __kho_unpreserve(struct kho_mem_track *track, unsigned long pfn,
+ unsigned long end_pfn)
+{
+ struct kho_mem_phys_bits *bits;
+ struct kho_mem_phys *physxa;
+
+ while (pfn < end_pfn) {
+ const unsigned int order =
+ min(count_trailing_zeros(pfn), ilog2(end_pfn - pfn));
+ const unsigned long pfn_high = pfn >> order;
+
+ physxa = xa_load(&track->orders, order);
+ if (!physxa)
+ continue;
+
+ bits = xa_load(&physxa->phys_bits, pfn_high / PRESERVE_BITS);
+ if (!bits)
+ continue;
+
+ clear_bit(pfn_high % PRESERVE_BITS, bits->preserve);
+
+ pfn += 1 << order;
+ }
+}
+
+static int __kho_preserve_order(struct kho_mem_track *track, unsigned long pfn,
+ unsigned int order)
+{
+ struct kho_mem_phys_bits *bits;
+ struct kho_mem_phys *physxa;
+ const unsigned long pfn_high = pfn >> order;
+
+ might_sleep();
+
+ physxa = xa_load_or_alloc(&track->orders, order, sizeof(*physxa));
+ if (IS_ERR(physxa))
+ return PTR_ERR(physxa);
+
+ bits = xa_load_or_alloc(&physxa->phys_bits, pfn_high / PRESERVE_BITS,
+ sizeof(*bits));
+ if (IS_ERR(bits))
+ return PTR_ERR(bits);
+
+ set_bit(pfn_high % PRESERVE_BITS, bits->preserve);
+
+ return 0;
+}
+
+/**
+ * kho_preserve_folio - preserve a folio across kexec.
+ * @ser: serialization control object passed by KHO notifiers.
+ * @folio: folio to preserve.
+ *
+ * Instructs KHO to preserve the whole folio across kexec. The order
+ * will be preserved as well.
+ *
+ * Return: 0 on success, error code on failure
+ */
+int kho_preserve_folio(struct kho_serialization *ser, struct folio *folio)
+{
+ const unsigned long pfn = folio_pfn(folio);
+ const unsigned int order = folio_order(folio);
+
+ return __kho_preserve_order(&ser->track, pfn, order);
+}
+EXPORT_SYMBOL_GPL(kho_preserve_folio);
+
+/**
+ * kho_preserve_phys - preserve a physically contiguous range across kexec.
+ * @ser: serialization control object passed by KHO notifiers.
+ * @phys: physical address of the range.
+ * @size: size of the range.
+ *
+ * Instructs KHO to preserve the memory range from @phys to @phys + @size
+ * across kexec.
+ *
+ * Return: 0 on success, error code on failure
+ */
+int kho_preserve_phys(struct kho_serialization *ser, phys_addr_t phys,
+ size_t size)
+{
+ unsigned long pfn = PHYS_PFN(phys);
+ unsigned long failed_pfn = 0;
+ const unsigned long start_pfn = pfn;
+ const unsigned long end_pfn = PHYS_PFN(phys + size);
+ int err = 0;
+
+ if (!PAGE_ALIGNED(phys) || !PAGE_ALIGNED(size))
+ return -EINVAL;
+
+ while (pfn < end_pfn) {
+ const unsigned int order =
+ min(count_trailing_zeros(pfn), ilog2(end_pfn - pfn));
+
+ err = __kho_preserve_order(&ser->track, pfn, order);
+ if (err) {
+ failed_pfn = pfn;
+ break;
+ }
+
+ pfn += 1 << order;
+ }
+
+ if (err)
+ __kho_unpreserve(&ser->track, start_pfn, failed_pfn);
+
+ return err;
+}
+EXPORT_SYMBOL_GPL(kho_preserve_phys);
+
+/* almost as free_reserved_page(), just don't free the page */
+static void kho_restore_page(struct page *page)
+{
+ ClearPageReserved(page);
+ init_page_count(page);
+ adjust_managed_page_count(page, 1);
+}
+
+/**
+ * kho_restore_folio - recreates the folio from the preserved memory.
+ * @phys: physical address of the folio.
+ *
+ * Return: pointer to the struct folio on success, NULL on failure.
+ */
+struct folio *kho_restore_folio(phys_addr_t phys)
+{
+ struct page *page = pfn_to_online_page(PHYS_PFN(phys));
+ unsigned long order;
+
+ if (!page)
+ return NULL;
+
+ order = page->private;
+ if (order) {
+ if (order > MAX_PAGE_ORDER)
+ return NULL;
+
+ prep_compound_page(page, order);
+ } else {
+ kho_restore_page(page);
+ }
+
+ return page_folio(page);
+}
+EXPORT_SYMBOL_GPL(kho_restore_folio);
+
+/* Serialize and deserialize struct kho_mem_phys across kexec
+ *
+ * Record all the bitmaps in a linked list of pages for the next kernel to
+ * process. Each chunk holds bitmaps of the same order and each block of bitmaps
+ * starts at a given physical address. This allows the bitmaps to be sparse. The
+ * xarray is used to store them in a tree while building up the data structure,
+ * but the KHO successor kernel only needs to process them once in order.
+ *
+ * All of this memory is normal kmalloc() memory and is not marked for
+ * preservation. The successor kernel will remain isolated to the scratch space
+ * until it completes processing this list. Once processed all the memory
+ * storing these ranges will be marked as free.
+ */
+
+struct khoser_mem_bitmap_ptr {
+ phys_addr_t phys_start;
+ DECLARE_KHOSER_PTR(bitmap, struct kho_mem_phys_bits *);
+};
+
+struct khoser_mem_chunk_hdr {
+ DECLARE_KHOSER_PTR(next, struct khoser_mem_chunk *);
+ unsigned int order;
+ unsigned int num_elms;
};
+#define KHOSER_BITMAP_SIZE \
+ ((PAGE_SIZE - sizeof(struct khoser_mem_chunk_hdr)) / \
+ sizeof(struct khoser_mem_bitmap_ptr))
+
+struct khoser_mem_chunk {
+ struct khoser_mem_chunk_hdr hdr;
+ struct khoser_mem_bitmap_ptr bitmaps[KHOSER_BITMAP_SIZE];
+};
+
+static_assert(sizeof(struct khoser_mem_chunk) == PAGE_SIZE);
+
+static struct khoser_mem_chunk *new_chunk(struct khoser_mem_chunk *cur_chunk,
+ unsigned long order)
+{
+ struct khoser_mem_chunk *chunk;
+
+ chunk = kzalloc(PAGE_SIZE, GFP_KERNEL);
+ if (!chunk)
+ return NULL;
+ chunk->hdr.order = order;
+ if (cur_chunk)
+ KHOSER_STORE_PTR(cur_chunk->hdr.next, chunk);
+ return chunk;
+}
+
+static void kho_mem_ser_free(struct khoser_mem_chunk *first_chunk)
+{
+ struct khoser_mem_chunk *chunk = first_chunk;
+
+ while (chunk) {
+ struct khoser_mem_chunk *tmp = chunk;
+
+ chunk = KHOSER_LOAD_PTR(chunk->hdr.next);
+ kfree(tmp);
+ }
+}
+
+static int kho_mem_serialize(struct kho_serialization *ser)
+{
+ struct khoser_mem_chunk *first_chunk = NULL;
+ struct khoser_mem_chunk *chunk = NULL;
+ struct kho_mem_phys *physxa;
+ unsigned long order;
+
+ xa_for_each(&ser->track.orders, order, physxa) {
+ struct kho_mem_phys_bits *bits;
+ unsigned long phys;
+
+ chunk = new_chunk(chunk, order);
+ if (!chunk)
+ goto err_free;
+
+ if (!first_chunk)
+ first_chunk = chunk;
+
+ xa_for_each(&physxa->phys_bits, phys, bits) {
+ struct khoser_mem_bitmap_ptr *elm;
+
+ if (chunk->hdr.num_elms == ARRAY_SIZE(chunk->bitmaps)) {
+ chunk = new_chunk(chunk, order);
+ if (!chunk)
+ goto err_free;
+ }
+
+ elm = &chunk->bitmaps[chunk->hdr.num_elms];
+ chunk->hdr.num_elms++;
+ elm->phys_start = (phys * PRESERVE_BITS)
+ << (order + PAGE_SHIFT);
+ KHOSER_STORE_PTR(elm->bitmap, bits);
+ }
+ }
+
+ ser->preserved_mem_map = first_chunk;
+
+ return 0;
+
+err_free:
+ kho_mem_ser_free(first_chunk);
+ return -ENOMEM;
+}
+
+static void deserialize_bitmap(unsigned int order,
+ struct khoser_mem_bitmap_ptr *elm)
+{
+ struct kho_mem_phys_bits *bitmap = KHOSER_LOAD_PTR(elm->bitmap);
+ unsigned long bit;
+
+ for_each_set_bit(bit, bitmap->preserve, PRESERVE_BITS) {
+ int sz = 1 << (order + PAGE_SHIFT);
+ phys_addr_t phys =
+ elm->phys_start + (bit << (order + PAGE_SHIFT));
+ struct page *page = phys_to_page(phys);
+
+ memblock_reserve(phys, sz);
+ memblock_reserved_mark_noinit(phys, sz);
+ page->private = order;
+ }
+}
+
+static void __init kho_mem_deserialize(const void *fdt)
+{
+ struct khoser_mem_chunk *chunk;
+ const phys_addr_t *mem;
+ int len;
+
+ mem = fdt_getprop(fdt, 0, PROP_PRESERVED_MEMORY_MAP, &len);
+
+ if (!mem || len != sizeof(*mem)) {
+ pr_err("failed to get preserved memory bitmaps\n");
+ return;
+ }
+
+ chunk = *mem ? phys_to_virt(*mem) : NULL;
+ while (chunk) {
+ unsigned int i;
+
+ for (i = 0; i != chunk->hdr.num_elms; i++)
+ deserialize_bitmap(chunk->hdr.order,
+ &chunk->bitmaps[i]);
+ chunk = KHOSER_LOAD_PTR(chunk->hdr.next);
+ }
+}
+
/*
* With KHO enabled, memory can become fragmented because KHO regions may
* be anywhere in physical address space. The scratch regions give us a
@@ -321,6 +679,9 @@ static struct kho_out kho_out = {
.lock = __MUTEX_INITIALIZER(kho_out.lock),
.ser = {
.fdt_list = LIST_HEAD_INIT(kho_out.ser.fdt_list),
+ .track = {
+ .orders = XARRAY_INIT(kho_out.ser.track.orders, 0),
+ },
},
.finalized = false,
};
@@ -363,6 +724,25 @@ static int kho_out_update_debugfs_fdt(void)
static int kho_abort(void)
{
int err;
+ unsigned long order;
+ struct kho_mem_phys *physxa;
+
+ xa_for_each(&kho_out.ser.track.orders, order, physxa) {
+ struct kho_mem_phys_bits *bits;
+ unsigned long phys;
+
+ xa_for_each(&physxa->phys_bits, phys, bits)
+ kfree(bits);
+
+ xa_destroy(&physxa->phys_bits);
+ kfree(physxa);
+ }
+ xa_destroy(&kho_out.ser.track.orders);
+
+ if (kho_out.ser.preserved_mem_map) {
+ kho_mem_ser_free(kho_out.ser.preserved_mem_map);
+ kho_out.ser.preserved_mem_map = NULL;
+ }
err = blocking_notifier_call_chain(&kho_out.chain_head, KEXEC_KHO_ABORT,
NULL);
@@ -377,12 +757,25 @@ static int kho_abort(void)
static int kho_finalize(void)
{
int err = 0;
+ u64 *preserved_mem_map;
void *fdt = page_to_virt(kho_out.ser.fdt);
err |= fdt_create(fdt, PAGE_SIZE);
err |= fdt_finish_reservemap(fdt);
err |= fdt_begin_node(fdt, "");
err |= fdt_property_string(fdt, "compatible", KHO_FDT_COMPATIBLE);
+ /**
+ * Reserve the preserved-memory-map property in the root FDT, so
+ * that all property definitions will precede subnodes created by
+ * KHO callers.
+ */
+ err |= fdt_property_placeholder(fdt, PROP_PRESERVED_MEMORY_MAP,
+ sizeof(*preserved_mem_map),
+ (void **)&preserved_mem_map);
+ if (err)
+ goto abort;
+
+ err = kho_preserve_folio(&kho_out.ser, page_folio(kho_out.ser.fdt));
if (err)
goto abort;
@@ -392,6 +785,12 @@ static int kho_finalize(void)
if (err)
goto abort;
+ err = kho_mem_serialize(&kho_out.ser);
+ if (err)
+ goto abort;
+
+ *preserved_mem_map = (u64)virt_to_phys(kho_out.ser.preserved_mem_map);
+
err |= fdt_end_node(fdt);
err |= fdt_finish(fdt);
@@ -697,6 +1096,8 @@ void __init kho_memory_init(void)
if (kho_in.scratch_phys) {
kho_scratch = phys_to_virt(kho_in.scratch_phys);
kho_release_scratch();
+
+ kho_mem_deserialize(kho_get_fdt());
} else {
kho_reserve_scratch();
}
--
2.49.0.604.gff1f9ca942-goog
^ permalink raw reply related [flat|nested] 26+ messages in thread
* [PATCH v6 07/14] kexec: add KHO support to kexec file loads
2025-04-11 5:37 [PATCH v6 00/14] kexec: introduce Kexec HandOver (KHO) Changyuan Lyu
` (5 preceding siblings ...)
2025-04-11 5:37 ` [PATCH v6 06/14] kexec: enable KHO support for memory preservation Changyuan Lyu
@ 2025-04-11 5:37 ` Changyuan Lyu
2025-04-11 5:37 ` [PATCH v6 08/14] kexec: add config option for KHO Changyuan Lyu
` (7 subsequent siblings)
14 siblings, 0 replies; 26+ messages in thread
From: Changyuan Lyu @ 2025-04-11 5:37 UTC (permalink / raw)
To: linux-kernel
Cc: akpm, anthony.yznaga, arnd, ashish.kalra, benh, bp,
catalin.marinas, corbet, dave.hansen, devicetree, dwmw2, ebiederm,
graf, hpa, jgowans, kexec, krzk, linux-arm-kernel, linux-doc,
linux-mm, luto, mark.rutland, mingo, pasha.tatashin, pbonzini,
peterz, ptyadav, robh, rostedt, rppt, saravanak, skinsburskii,
tglx, thomas.lendacky, will, x86, Changyuan Lyu
From: Alexander Graf <graf@amazon.com>
Kexec has 2 modes: A user space driven mode and a kernel driven mode.
For the kernel driven mode, kernel code determines the physical
addresses of all target buffers that the payload gets copied into.
With KHO, we can only safely copy payloads into the "scratch area".
Teach the kexec file loader about it, so it only allocates for that
area. In addition, enlighten it with support to ask the KHO subsystem
for its respective payloads to copy into target memory. Also teach the
KHO subsystem how to fill the images for file loads.
Signed-off-by: Alexander Graf <graf@amazon.com>
Co-developed-by: Mike Rapoport (Microsoft) <rppt@kernel.org>
Signed-off-by: Mike Rapoport (Microsoft) <rppt@kernel.org>
Co-developed-by: Changyuan Lyu <changyuanl@google.com>
Signed-off-by: Changyuan Lyu <changyuanl@google.com>
---
include/linux/kexec.h | 7 +++++
kernel/kexec_file.c | 13 ++++++++
kernel/kexec_handover.c | 67 +++++++++++++++++++++++++++++++++++++++++
kernel/kexec_internal.h | 16 ++++++++++
4 files changed, 103 insertions(+)
diff --git a/include/linux/kexec.h b/include/linux/kexec.h
index c8971861521a5..d3cad108b8076 100644
--- a/include/linux/kexec.h
+++ b/include/linux/kexec.h
@@ -371,6 +371,13 @@ struct kimage {
size_t ima_buffer_size;
#endif
+#ifdef CONFIG_KEXEC_HANDOVER
+ struct {
+ struct kexec_segment *scratch;
+ phys_addr_t fdt;
+ } kho;
+#endif
+
/* Core ELF header buffer */
void *elf_headers;
unsigned long elf_headers_sz;
diff --git a/kernel/kexec_file.c b/kernel/kexec_file.c
index fba686487e3b5..77758c5331229 100644
--- a/kernel/kexec_file.c
+++ b/kernel/kexec_file.c
@@ -253,6 +253,11 @@ kimage_file_prepare_segments(struct kimage *image, int kernel_fd, int initrd_fd,
/* IMA needs to pass the measurement list to the next kernel. */
ima_add_kexec_buffer(image);
+ /* If KHO is active, add its images to the list */
+ ret = kho_fill_kimage(image);
+ if (ret)
+ goto out;
+
/* Call image load handler */
ldata = kexec_image_load_default(image);
@@ -648,6 +653,14 @@ int kexec_locate_mem_hole(struct kexec_buf *kbuf)
if (kbuf->mem != KEXEC_BUF_MEM_UNKNOWN)
return 0;
+ /*
+ * If KHO is active, only use KHO scratch memory. All other memory
+ * could potentially be handed over.
+ */
+ ret = kho_locate_mem_hole(kbuf, locate_mem_hole_callback);
+ if (ret <= 0)
+ return ret;
+
if (!IS_ENABLED(CONFIG_ARCH_KEEP_MEMBLOCK))
ret = kexec_walk_resources(kbuf, locate_mem_hole_callback);
else
diff --git a/kernel/kexec_handover.c b/kernel/kexec_handover.c
index a594da2a22912..4bbc6f5ef7230 100644
--- a/kernel/kexec_handover.c
+++ b/kernel/kexec_handover.c
@@ -23,6 +23,7 @@
* internal APIs.
*/
#include "../mm/internal.h"
+#include "kexec_internal.h"
#define KHO_FDT_COMPATIBLE "kho-v1"
#define PROP_PRESERVED_MEMORY_MAP "preserved-memory-map"
@@ -1184,3 +1185,69 @@ void __init kho_populate(phys_addr_t fdt_phys, u64 fdt_len,
if (err)
pr_warn("disabling KHO revival: %d\n", err);
}
+
+/* Helper functions for kexec_file_load */
+
+int kho_fill_kimage(struct kimage *image)
+{
+ ssize_t scratch_size;
+ int err = 0;
+ struct kexec_buf scratch;
+
+ if (!kho_enable)
+ return 0;
+
+ image->kho.fdt = page_to_phys(kho_out.ser.fdt);
+
+ scratch_size = sizeof(*kho_scratch) * kho_scratch_cnt;
+ scratch = (struct kexec_buf){
+ .image = image,
+ .buffer = kho_scratch,
+ .bufsz = scratch_size,
+ .mem = KEXEC_BUF_MEM_UNKNOWN,
+ .memsz = scratch_size,
+ .buf_align = SZ_64K, /* Makes it easier to map */
+ .buf_max = ULONG_MAX,
+ .top_down = true,
+ };
+ err = kexec_add_buffer(&scratch);
+ if (err)
+ return err;
+ image->kho.scratch = &image->segment[image->nr_segments - 1];
+
+ return 0;
+}
+
+static int kho_walk_scratch(struct kexec_buf *kbuf,
+ int (*func)(struct resource *, void *))
+{
+ int ret = 0;
+ int i;
+
+ for (i = 0; i < kho_scratch_cnt; i++) {
+ struct resource res = {
+ .start = kho_scratch[i].addr,
+ .end = kho_scratch[i].addr + kho_scratch[i].size - 1,
+ };
+
+ /* Try to fit the kimage into our KHO scratch region */
+ ret = func(&res, kbuf);
+ if (ret)
+ break;
+ }
+
+ return ret;
+}
+
+int kho_locate_mem_hole(struct kexec_buf *kbuf,
+ int (*func)(struct resource *, void *))
+{
+ int ret;
+
+ if (!kho_enable || kbuf->image->type == KEXEC_TYPE_CRASH)
+ return 1;
+
+ ret = kho_walk_scratch(kbuf, func);
+
+ return ret == 1 ? 0 : -EADDRNOTAVAIL;
+}
diff --git a/kernel/kexec_internal.h b/kernel/kexec_internal.h
index d35d9792402d1..30a733a55a67b 100644
--- a/kernel/kexec_internal.h
+++ b/kernel/kexec_internal.h
@@ -39,4 +39,20 @@ extern size_t kexec_purgatory_size;
#else /* CONFIG_KEXEC_FILE */
static inline void kimage_file_post_load_cleanup(struct kimage *image) { }
#endif /* CONFIG_KEXEC_FILE */
+
+struct kexec_buf;
+
+#ifdef CONFIG_KEXEC_HANDOVER
+int kho_locate_mem_hole(struct kexec_buf *kbuf,
+ int (*func)(struct resource *, void *));
+int kho_fill_kimage(struct kimage *image);
+#else
+static inline int kho_locate_mem_hole(struct kexec_buf *kbuf,
+ int (*func)(struct resource *, void *))
+{
+ return 1;
+}
+
+static inline int kho_fill_kimage(struct kimage *image) { return 0; }
+#endif /* CONFIG_KEXEC_HANDOVER */
#endif /* LINUX_KEXEC_INTERNAL_H */
--
2.49.0.604.gff1f9ca942-goog
^ permalink raw reply related [flat|nested] 26+ messages in thread
* [PATCH v6 08/14] kexec: add config option for KHO
2025-04-11 5:37 [PATCH v6 00/14] kexec: introduce Kexec HandOver (KHO) Changyuan Lyu
` (6 preceding siblings ...)
2025-04-11 5:37 ` [PATCH v6 07/14] kexec: add KHO support to kexec file loads Changyuan Lyu
@ 2025-04-11 5:37 ` Changyuan Lyu
2025-04-11 5:37 ` [PATCH v6 09/14] arm64: add KHO support Changyuan Lyu
` (6 subsequent siblings)
14 siblings, 0 replies; 26+ messages in thread
From: Changyuan Lyu @ 2025-04-11 5:37 UTC (permalink / raw)
To: linux-kernel
Cc: akpm, anthony.yznaga, arnd, ashish.kalra, benh, bp,
catalin.marinas, corbet, dave.hansen, devicetree, dwmw2, ebiederm,
graf, hpa, jgowans, kexec, krzk, linux-arm-kernel, linux-doc,
linux-mm, luto, mark.rutland, mingo, pasha.tatashin, pbonzini,
peterz, ptyadav, robh, rostedt, rppt, saravanak, skinsburskii,
tglx, thomas.lendacky, will, x86, Changyuan Lyu
From: Alexander Graf <graf@amazon.com>
We have all generic code in place now to support Kexec with KHO. This
patch adds a config option that depends on architecture support to
enable KHO support.
Signed-off-by: Alexander Graf <graf@amazon.com>
Co-developed-by: Mike Rapoport (Microsoft) <rppt@kernel.org>
Signed-off-by: Mike Rapoport (Microsoft) <rppt@kernel.org>
Signed-off-by: Changyuan Lyu <changyuanl@google.com>
---
kernel/Kconfig.kexec | 14 ++++++++++++++
1 file changed, 14 insertions(+)
diff --git a/kernel/Kconfig.kexec b/kernel/Kconfig.kexec
index 4d111f8719516..4fa212909d699 100644
--- a/kernel/Kconfig.kexec
+++ b/kernel/Kconfig.kexec
@@ -95,6 +95,20 @@ config KEXEC_JUMP
Jump between original kernel and kexeced kernel and invoke
code in physical address mode via KEXEC
+config KEXEC_HANDOVER
+ bool "kexec handover"
+ depends on ARCH_SUPPORTS_KEXEC_HANDOVER && ARCH_SUPPORTS_KEXEC_FILE
+ select MEMBLOCK_KHO_SCRATCH
+ select KEXEC_FILE
+ select DEBUG_FS
+ select LIBFDT
+ select CMA
+ help
+ Allow kexec to hand over state across kernels by generating and
+ passing additional metadata to the target kernel. This is useful
+ to keep data or state alive across the kexec. For this to work,
+ both source and target kernels need to have this option enabled.
+
config CRASH_DUMP
bool "kernel crash dumps"
default ARCH_DEFAULT_CRASH_DUMP
--
2.49.0.604.gff1f9ca942-goog
^ permalink raw reply related [flat|nested] 26+ messages in thread
* [PATCH v6 09/14] arm64: add KHO support
2025-04-11 5:37 [PATCH v6 00/14] kexec: introduce Kexec HandOver (KHO) Changyuan Lyu
` (7 preceding siblings ...)
2025-04-11 5:37 ` [PATCH v6 08/14] kexec: add config option for KHO Changyuan Lyu
@ 2025-04-11 5:37 ` Changyuan Lyu
2025-04-11 5:37 ` [PATCH v6 10/14] x86/setup: use memblock_reserve_kern for memory used by kernel Changyuan Lyu
` (5 subsequent siblings)
14 siblings, 0 replies; 26+ messages in thread
From: Changyuan Lyu @ 2025-04-11 5:37 UTC (permalink / raw)
To: linux-kernel
Cc: akpm, anthony.yznaga, arnd, ashish.kalra, benh, bp,
catalin.marinas, corbet, dave.hansen, devicetree, dwmw2, ebiederm,
graf, hpa, jgowans, kexec, krzk, linux-arm-kernel, linux-doc,
linux-mm, luto, mark.rutland, mingo, pasha.tatashin, pbonzini,
peterz, ptyadav, robh, rostedt, rppt, saravanak, skinsburskii,
tglx, thomas.lendacky, will, x86, Changyuan Lyu
From: Alexander Graf <graf@amazon.com>
We now have all bits in place to support KHO kexecs. Add awareness of
KHO in the kexec file as well as boot path for arm64 and adds the
respective kconfig option to the architecture so that it can use KHO
successfully.
Changes to the "chosen" node have been sent to
https://github.com/devicetree-org/dt-schema/pull/158.
Signed-off-by: Alexander Graf <graf@amazon.com>
Co-developed-by: Mike Rapoport (Microsoft) <rppt@kernel.org>
Signed-off-by: Mike Rapoport (Microsoft) <rppt@kernel.org>
Co-developed-by: Changyuan Lyu <changyuanl@google.com>
Signed-off-by: Changyuan Lyu <changyuanl@google.com>
---
arch/arm64/Kconfig | 3 +++
drivers/of/fdt.c | 34 ++++++++++++++++++++++++++++++++++
drivers/of/kexec.c | 42 ++++++++++++++++++++++++++++++++++++++++++
3 files changed, 79 insertions(+)
diff --git a/arch/arm64/Kconfig b/arch/arm64/Kconfig
index a182295e6f08b..34c79f4fee3f9 100644
--- a/arch/arm64/Kconfig
+++ b/arch/arm64/Kconfig
@@ -1602,6 +1602,9 @@ config ARCH_SUPPORTS_KEXEC_IMAGE_VERIFY_SIG
config ARCH_DEFAULT_KEXEC_IMAGE_VERIFY_SIG
def_bool y
+config ARCH_SUPPORTS_KEXEC_HANDOVER
+ def_bool y
+
config ARCH_SUPPORTS_CRASH_DUMP
def_bool y
diff --git a/drivers/of/fdt.c b/drivers/of/fdt.c
index aedd0e2dcd890..0edd639898a63 100644
--- a/drivers/of/fdt.c
+++ b/drivers/of/fdt.c
@@ -25,6 +25,7 @@
#include <linux/serial_core.h>
#include <linux/sysfs.h>
#include <linux/random.h>
+#include <linux/kexec_handover.h>
#include <asm/setup.h> /* for COMMAND_LINE_SIZE */
#include <asm/page.h>
@@ -875,6 +876,36 @@ void __init early_init_dt_check_for_usable_mem_range(void)
memblock_add(rgn[i].base, rgn[i].size);
}
+/**
+ * early_init_dt_check_kho - Decode info required for kexec handover from DT
+ */
+static void __init early_init_dt_check_kho(void)
+{
+ unsigned long node = chosen_node_offset;
+ u64 fdt_start, fdt_size, scratch_start, scratch_size;
+ const __be32 *p;
+ int l;
+
+ if (!IS_ENABLED(CONFIG_KEXEC_HANDOVER) || (long)node < 0)
+ return;
+
+ p = of_get_flat_dt_prop(node, "linux,kho-fdt", &l);
+ if (l != (dt_root_addr_cells + dt_root_size_cells) * sizeof(__be32))
+ return;
+
+ fdt_start = dt_mem_next_cell(dt_root_addr_cells, &p);
+ fdt_size = dt_mem_next_cell(dt_root_addr_cells, &p);
+
+ p = of_get_flat_dt_prop(node, "linux,kho-scratch", &l);
+ if (l != (dt_root_addr_cells + dt_root_size_cells) * sizeof(__be32))
+ return;
+
+ scratch_start = dt_mem_next_cell(dt_root_addr_cells, &p);
+ scratch_size = dt_mem_next_cell(dt_root_addr_cells, &p);
+
+ kho_populate(fdt_start, fdt_size, scratch_start, scratch_size);
+}
+
#ifdef CONFIG_SERIAL_EARLYCON
int __init early_init_dt_scan_chosen_stdout(void)
@@ -1169,6 +1200,9 @@ void __init early_init_dt_scan_nodes(void)
/* Handle linux,usable-memory-range property */
early_init_dt_check_for_usable_mem_range();
+
+ /* Handle kexec handover */
+ early_init_dt_check_kho();
}
bool __init early_init_dt_scan(void *dt_virt, phys_addr_t dt_phys)
diff --git a/drivers/of/kexec.c b/drivers/of/kexec.c
index 5b924597a4deb..1ee2d31816aeb 100644
--- a/drivers/of/kexec.c
+++ b/drivers/of/kexec.c
@@ -264,6 +264,43 @@ static inline int setup_ima_buffer(const struct kimage *image, void *fdt,
}
#endif /* CONFIG_IMA_KEXEC */
+static int kho_add_chosen(const struct kimage *image, void *fdt, int chosen_node)
+{
+ int ret = 0;
+#ifdef CONFIG_KEXEC_HANDOVER
+ phys_addr_t fdt_mem = 0;
+ phys_addr_t fdt_len = 0;
+ phys_addr_t scratch_mem = 0;
+ phys_addr_t scratch_len = 0;
+
+ ret = fdt_delprop(fdt, chosen_node, "linux,kho-fdt");
+ if (ret && ret != -FDT_ERR_NOTFOUND)
+ return ret;
+ ret = fdt_delprop(fdt, chosen_node, "linux,kho-scratch");
+ if (ret && ret != -FDT_ERR_NOTFOUND)
+ return ret;
+
+ if (!image->kho.fdt || !image->kho.scratch)
+ return 0;
+
+ fdt_mem = image->kho.fdt;
+ fdt_len = PAGE_SIZE;
+ scratch_mem = image->kho.scratch->mem;
+ scratch_len = image->kho.scratch->bufsz;
+
+ pr_debug("Adding kho metadata to DT");
+
+ ret = fdt_appendprop_addrrange(fdt, 0, chosen_node, "linux,kho-fdt",
+ fdt_mem, fdt_len);
+ if (ret)
+ return ret;
+ ret = fdt_appendprop_addrrange(fdt, 0, chosen_node, "linux,kho-scratch",
+ scratch_mem, scratch_len);
+
+#endif /* CONFIG_KEXEC_HANDOVER */
+ return ret;
+}
+
/*
* of_kexec_alloc_and_setup_fdt - Alloc and setup a new Flattened Device Tree
*
@@ -414,6 +451,11 @@ void *of_kexec_alloc_and_setup_fdt(const struct kimage *image,
#endif
}
+ /* Add kho metadata if this is a KHO image */
+ ret = kho_add_chosen(image, fdt, chosen_node);
+ if (ret)
+ goto out;
+
/* add bootargs */
if (cmdline) {
ret = fdt_setprop_string(fdt, chosen_node, "bootargs", cmdline);
--
2.49.0.604.gff1f9ca942-goog
^ permalink raw reply related [flat|nested] 26+ messages in thread
* [PATCH v6 10/14] x86/setup: use memblock_reserve_kern for memory used by kernel
2025-04-11 5:37 [PATCH v6 00/14] kexec: introduce Kexec HandOver (KHO) Changyuan Lyu
` (8 preceding siblings ...)
2025-04-11 5:37 ` [PATCH v6 09/14] arm64: add KHO support Changyuan Lyu
@ 2025-04-11 5:37 ` Changyuan Lyu
2025-04-28 22:15 ` Dave Hansen
2025-04-11 5:37 ` [PATCH v6 11/14] x86: add KHO support Changyuan Lyu
` (4 subsequent siblings)
14 siblings, 1 reply; 26+ messages in thread
From: Changyuan Lyu @ 2025-04-11 5:37 UTC (permalink / raw)
To: linux-kernel
Cc: akpm, anthony.yznaga, arnd, ashish.kalra, benh, bp,
catalin.marinas, corbet, dave.hansen, devicetree, dwmw2, ebiederm,
graf, hpa, jgowans, kexec, krzk, linux-arm-kernel, linux-doc,
linux-mm, luto, mark.rutland, mingo, pasha.tatashin, pbonzini,
peterz, ptyadav, robh, rostedt, rppt, saravanak, skinsburskii,
tglx, thomas.lendacky, will, x86, Changyuan Lyu
From: "Mike Rapoport (Microsoft)" <rppt@kernel.org>
memblock_reserve() does not distinguish memory used by firmware from memory
used by kernel.
The distinction is nice to have for accounting of early memory allocations
and reservations, but it is essential for kexec handover (kho) to know how
much memory kernel consumes during boot.
Use memblock_reserve_kern() to reserve kernel memory, such as kernel image,
initrd and setup data.
Signed-off-by: Mike Rapoport (Microsoft) <rppt@kernel.org>
Signed-off-by: Changyuan Lyu <changyuanl@google.com>
---
arch/x86/kernel/setup.c | 16 ++++++++--------
1 file changed, 8 insertions(+), 8 deletions(-)
diff --git a/arch/x86/kernel/setup.c b/arch/x86/kernel/setup.c
index 9d2a13b37833c..766176c4f5ee8 100644
--- a/arch/x86/kernel/setup.c
+++ b/arch/x86/kernel/setup.c
@@ -286,8 +286,8 @@ static void __init cleanup_highmap(void)
static void __init reserve_brk(void)
{
if (_brk_end > _brk_start)
- memblock_reserve(__pa_symbol(_brk_start),
- _brk_end - _brk_start);
+ memblock_reserve_kern(__pa_symbol(_brk_start),
+ _brk_end - _brk_start);
/* Mark brk area as locked down and no longer taking any
new allocations */
@@ -360,7 +360,7 @@ static void __init early_reserve_initrd(void)
!ramdisk_image || !ramdisk_size)
return; /* No initrd provided by bootloader */
- memblock_reserve(ramdisk_image, ramdisk_end - ramdisk_image);
+ memblock_reserve_kern(ramdisk_image, ramdisk_end - ramdisk_image);
}
static void __init reserve_initrd(void)
@@ -413,7 +413,7 @@ static void __init add_early_ima_buffer(u64 phys_addr)
}
if (data->size) {
- memblock_reserve(data->addr, data->size);
+ memblock_reserve_kern(data->addr, data->size);
ima_kexec_buffer_phys = data->addr;
ima_kexec_buffer_size = data->size;
}
@@ -553,7 +553,7 @@ static void __init memblock_x86_reserve_range_setup_data(void)
len = sizeof(*data);
pa_next = data->next;
- memblock_reserve(pa_data, sizeof(*data) + data->len);
+ memblock_reserve_kern(pa_data, sizeof(*data) + data->len);
if (data->type == SETUP_INDIRECT) {
len += data->len;
@@ -567,7 +567,7 @@ static void __init memblock_x86_reserve_range_setup_data(void)
indirect = (struct setup_indirect *)data->data;
if (indirect->type != SETUP_INDIRECT)
- memblock_reserve(indirect->addr, indirect->len);
+ memblock_reserve_kern(indirect->addr, indirect->len);
}
pa_data = pa_next;
@@ -770,8 +770,8 @@ static void __init early_reserve_memory(void)
* __end_of_kernel_reserve symbol must be explicitly reserved with a
* separate memblock_reserve() or they will be discarded.
*/
- memblock_reserve(__pa_symbol(_text),
- (unsigned long)__end_of_kernel_reserve - (unsigned long)_text);
+ memblock_reserve_kern(__pa_symbol(_text),
+ (unsigned long)__end_of_kernel_reserve - (unsigned long)_text);
/*
* The first 4Kb of memory is a BIOS owned area, but generally it is
--
2.49.0.604.gff1f9ca942-goog
^ permalink raw reply related [flat|nested] 26+ messages in thread
* [PATCH v6 11/14] x86: add KHO support
2025-04-11 5:37 [PATCH v6 00/14] kexec: introduce Kexec HandOver (KHO) Changyuan Lyu
` (9 preceding siblings ...)
2025-04-11 5:37 ` [PATCH v6 10/14] x86/setup: use memblock_reserve_kern for memory used by kernel Changyuan Lyu
@ 2025-04-11 5:37 ` Changyuan Lyu
2025-04-28 22:05 ` Dave Hansen
2025-04-11 5:37 ` [PATCH v6 12/14] memblock: add KHO support for reserve_mem Changyuan Lyu
` (3 subsequent siblings)
14 siblings, 1 reply; 26+ messages in thread
From: Changyuan Lyu @ 2025-04-11 5:37 UTC (permalink / raw)
To: linux-kernel
Cc: akpm, anthony.yznaga, arnd, ashish.kalra, benh, bp,
catalin.marinas, corbet, dave.hansen, devicetree, dwmw2, ebiederm,
graf, hpa, jgowans, kexec, krzk, linux-arm-kernel, linux-doc,
linux-mm, luto, mark.rutland, mingo, pasha.tatashin, pbonzini,
peterz, ptyadav, robh, rostedt, rppt, saravanak, skinsburskii,
tglx, thomas.lendacky, will, x86, Changyuan Lyu
From: Alexander Graf <graf@amazon.com>
We now have all bits in place to support KHO kexecs. This patch adds
awareness of KHO in the kexec file as well as boot path for x86 and
adds the respective kconfig option to the architecture so that it can
use KHO successfully.
In addition, it enlightens it decompression code with KHO so that its
KASLR location finder only considers memory regions that are not already
occupied by KHO memory.
Signed-off-by: Alexander Graf <graf@amazon.com>
Co-developed-by: Mike Rapoport (Microsoft) <rppt@kernel.org>
Signed-off-by: Mike Rapoport (Microsoft) <rppt@kernel.org>
Co-developed-by: Changyuan Lyu <changyuanl@google.com>
Signed-off-by: Changyuan Lyu <changyuanl@google.com>
---
arch/x86/Kconfig | 3 ++
arch/x86/boot/compressed/kaslr.c | 52 +++++++++++++++++++++++++-
arch/x86/include/asm/setup.h | 4 ++
arch/x86/include/uapi/asm/setup_data.h | 13 ++++++-
arch/x86/kernel/e820.c | 18 +++++++++
arch/x86/kernel/kexec-bzimage64.c | 36 ++++++++++++++++++
arch/x86/kernel/setup.c | 25 +++++++++++++
arch/x86/realmode/init.c | 2 +
8 files changed, 151 insertions(+), 2 deletions(-)
diff --git a/arch/x86/Kconfig b/arch/x86/Kconfig
index 4b9f378e05f6b..ae82a42ff70e7 100644
--- a/arch/x86/Kconfig
+++ b/arch/x86/Kconfig
@@ -2029,6 +2029,9 @@ config ARCH_SUPPORTS_KEXEC_BZIMAGE_VERIFY_SIG
config ARCH_SUPPORTS_KEXEC_JUMP
def_bool y
+config ARCH_SUPPORTS_KEXEC_HANDOVER
+ def_bool y
+
config ARCH_SUPPORTS_CRASH_DUMP
def_bool X86_64 || (X86_32 && HIGHMEM)
diff --git a/arch/x86/boot/compressed/kaslr.c b/arch/x86/boot/compressed/kaslr.c
index f03d59ea6e40f..ff11688810162 100644
--- a/arch/x86/boot/compressed/kaslr.c
+++ b/arch/x86/boot/compressed/kaslr.c
@@ -760,6 +760,55 @@ static void process_e820_entries(unsigned long minimum,
}
}
+/*
+ * If KHO is active, only process its scratch areas to ensure we are not
+ * stepping onto preserved memory.
+ */
+#ifdef CONFIG_KEXEC_HANDOVER
+static bool process_kho_entries(unsigned long minimum, unsigned long image_size)
+{
+ struct kho_scratch *kho_scratch;
+ struct setup_data *ptr;
+ int i, nr_areas = 0;
+
+ ptr = (struct setup_data *)(unsigned long)boot_params_ptr->hdr.setup_data;
+ while (ptr) {
+ if (ptr->type == SETUP_KEXEC_KHO) {
+ struct kho_data *kho = (struct kho_data *)ptr->data;
+
+ kho_scratch = (void *)kho->scratch_addr;
+ nr_areas = kho->scratch_size / sizeof(*kho_scratch);
+
+ break;
+ }
+
+ ptr = (struct setup_data *)(unsigned long)ptr->next;
+ }
+
+ if (!nr_areas)
+ return false;
+
+ for (i = 0; i < nr_areas; i++) {
+ struct kho_scratch *area = &kho_scratch[i];
+ struct mem_vector region = {
+ .start = area->addr,
+ .size = area->size,
+ };
+
+ if (process_mem_region(®ion, minimum, image_size))
+ break;
+ }
+
+ return true;
+}
+#else
+static inline bool process_kho_entries(unsigned long minimum,
+ unsigned long image_size)
+{
+ return false;
+}
+#endif
+
static unsigned long find_random_phys_addr(unsigned long minimum,
unsigned long image_size)
{
@@ -775,7 +824,8 @@ static unsigned long find_random_phys_addr(unsigned long minimum,
return 0;
}
- if (!process_efi_entries(minimum, image_size))
+ if (!process_kho_entries(minimum, image_size) &&
+ !process_efi_entries(minimum, image_size))
process_e820_entries(minimum, image_size);
phys_addr = slots_fetch_random();
diff --git a/arch/x86/include/asm/setup.h b/arch/x86/include/asm/setup.h
index ad9212df0ec0c..906ab5e6d25fe 100644
--- a/arch/x86/include/asm/setup.h
+++ b/arch/x86/include/asm/setup.h
@@ -67,6 +67,10 @@ extern void x86_ce4100_early_setup(void);
static inline void x86_ce4100_early_setup(void) { }
#endif
+#ifdef CONFIG_KEXEC_HANDOVER
+#include <linux/kexec_handover.h>
+#endif
+
#ifndef _SETUP
#include <asm/espfix.h>
diff --git a/arch/x86/include/uapi/asm/setup_data.h b/arch/x86/include/uapi/asm/setup_data.h
index 50c45ead4e7c9..2671c4e1b3a0b 100644
--- a/arch/x86/include/uapi/asm/setup_data.h
+++ b/arch/x86/include/uapi/asm/setup_data.h
@@ -13,7 +13,8 @@
#define SETUP_CC_BLOB 7
#define SETUP_IMA 8
#define SETUP_RNG_SEED 9
-#define SETUP_ENUM_MAX SETUP_RNG_SEED
+#define SETUP_KEXEC_KHO 10
+#define SETUP_ENUM_MAX SETUP_KEXEC_KHO
#define SETUP_INDIRECT (1<<31)
#define SETUP_TYPE_MAX (SETUP_ENUM_MAX | SETUP_INDIRECT)
@@ -78,6 +79,16 @@ struct ima_setup_data {
__u64 size;
} __attribute__((packed));
+/*
+ * Locations of kexec handover metadata
+ */
+struct kho_data {
+ __u64 fdt_addr;
+ __u64 fdt_size;
+ __u64 scratch_addr;
+ __u64 scratch_size;
+} __attribute__((packed));
+
#endif /* __ASSEMBLER__ */
#endif /* _UAPI_ASM_X86_SETUP_DATA_H */
diff --git a/arch/x86/kernel/e820.c b/arch/x86/kernel/e820.c
index 57120f0749cc3..c314212a5ecd5 100644
--- a/arch/x86/kernel/e820.c
+++ b/arch/x86/kernel/e820.c
@@ -1300,6 +1300,24 @@ void __init e820__memblock_setup(void)
memblock_add(entry->addr, entry->size);
}
+ /*
+ * At this point with KHO we only allocate from scratch memory.
+ * At the same time, we configure memblock to only allow
+ * allocations from memory below ISA_END_ADDRESS which is not
+ * a natural scratch region, because Linux ignores memory below
+ * ISA_END_ADDRESS at runtime. Beside very few (if any) early
+ * allocations, we must allocate real-mode trapoline below
+ * ISA_END_ADDRESS.
+ *
+ * To make sure that we can actually perform allocations during
+ * this phase, let's mark memory below ISA_END_ADDRESS as scratch
+ * so we can allocate from there in a scratch-only world.
+ *
+ * After real mode trampoline is allocated, we clear scratch
+ * marking from the memory below ISA_END_ADDRESS
+ */
+ memblock_mark_kho_scratch(0, ISA_END_ADDRESS);
+
/* Throw away partial pages: */
memblock_trim_memory(PAGE_SIZE);
diff --git a/arch/x86/kernel/kexec-bzimage64.c b/arch/x86/kernel/kexec-bzimage64.c
index 68530fad05f74..518635cc0876c 100644
--- a/arch/x86/kernel/kexec-bzimage64.c
+++ b/arch/x86/kernel/kexec-bzimage64.c
@@ -233,6 +233,31 @@ setup_ima_state(const struct kimage *image, struct boot_params *params,
#endif /* CONFIG_IMA_KEXEC */
}
+static void setup_kho(const struct kimage *image, struct boot_params *params,
+ unsigned long params_load_addr,
+ unsigned int setup_data_offset)
+{
+#ifdef CONFIG_KEXEC_HANDOVER
+ struct setup_data *sd = (void *)params + setup_data_offset;
+ struct kho_data *kho = (void *)sd + sizeof(*sd);
+
+ sd->type = SETUP_KEXEC_KHO;
+ sd->len = sizeof(struct kho_data);
+
+ /* Only add if we have all KHO images in place */
+ if (!image->kho.fdt || !image->kho.scratch)
+ return;
+
+ /* Add setup data */
+ kho->fdt_addr = image->kho.fdt;
+ kho->fdt_size = PAGE_SIZE;
+ kho->scratch_addr = image->kho.scratch->mem;
+ kho->scratch_size = image->kho.scratch->bufsz;
+ sd->next = params->hdr.setup_data;
+ params->hdr.setup_data = params_load_addr + setup_data_offset;
+#endif /* CONFIG_KEXEC_HANDOVER */
+}
+
static int
setup_boot_parameters(struct kimage *image, struct boot_params *params,
unsigned long params_load_addr,
@@ -312,6 +337,13 @@ setup_boot_parameters(struct kimage *image, struct boot_params *params,
sizeof(struct ima_setup_data);
}
+ if (IS_ENABLED(CONFIG_KEXEC_HANDOVER)) {
+ /* Setup space to store preservation metadata */
+ setup_kho(image, params, params_load_addr, setup_data_offset);
+ setup_data_offset += sizeof(struct setup_data) +
+ sizeof(struct kho_data);
+ }
+
/* Setup RNG seed */
setup_rng_seed(params, params_load_addr, setup_data_offset);
@@ -479,6 +511,10 @@ static void *bzImage64_load(struct kimage *image, char *kernel,
kbuf.bufsz += sizeof(struct setup_data) +
sizeof(struct ima_setup_data);
+ if (IS_ENABLED(CONFIG_KEXEC_HANDOVER))
+ kbuf.bufsz += sizeof(struct setup_data) +
+ sizeof(struct kho_data);
+
params = kzalloc(kbuf.bufsz, GFP_KERNEL);
if (!params)
return ERR_PTR(-ENOMEM);
diff --git a/arch/x86/kernel/setup.c b/arch/x86/kernel/setup.c
index 766176c4f5ee8..496dae89cf95d 100644
--- a/arch/x86/kernel/setup.c
+++ b/arch/x86/kernel/setup.c
@@ -451,6 +451,28 @@ int __init ima_get_kexec_buffer(void **addr, size_t *size)
}
#endif
+static void __init add_kho(u64 phys_addr, u32 data_len)
+{
+#ifdef CONFIG_KEXEC_HANDOVER
+ struct kho_data *kho;
+ u64 addr = phys_addr + sizeof(struct setup_data);
+ u64 size = data_len - sizeof(struct setup_data);
+
+ kho = early_memremap(addr, size);
+ if (!kho) {
+ pr_warn("setup: failed to memremap kho data (0x%llx, 0x%llx)\n",
+ addr, size);
+ return;
+ }
+
+ kho_populate(kho->fdt_addr, kho->fdt_size, kho->scratch_addr, kho->scratch_size);
+
+ early_memunmap(kho, size);
+#else
+ pr_warn("Passed KHO data, but CONFIG_KEXEC_HANDOVER not set. Ignoring.\n");
+#endif
+}
+
static void __init parse_setup_data(void)
{
struct setup_data *data;
@@ -479,6 +501,9 @@ static void __init parse_setup_data(void)
case SETUP_IMA:
add_early_ima_buffer(pa_data);
break;
+ case SETUP_KEXEC_KHO:
+ add_kho(pa_data, data_len);
+ break;
case SETUP_RNG_SEED:
data = early_memremap(pa_data, data_len);
add_bootloader_randomness(data->data, data->len);
diff --git a/arch/x86/realmode/init.c b/arch/x86/realmode/init.c
index f9bc444a3064d..9b9f4534086d2 100644
--- a/arch/x86/realmode/init.c
+++ b/arch/x86/realmode/init.c
@@ -65,6 +65,8 @@ void __init reserve_real_mode(void)
* setup_arch().
*/
memblock_reserve(0, SZ_1M);
+
+ memblock_clear_kho_scratch(0, SZ_1M);
}
static void __init sme_sev_setup_real_mode(struct trampoline_header *th)
--
2.49.0.604.gff1f9ca942-goog
^ permalink raw reply related [flat|nested] 26+ messages in thread
* [PATCH v6 12/14] memblock: add KHO support for reserve_mem
2025-04-11 5:37 [PATCH v6 00/14] kexec: introduce Kexec HandOver (KHO) Changyuan Lyu
` (10 preceding siblings ...)
2025-04-11 5:37 ` [PATCH v6 11/14] x86: add KHO support Changyuan Lyu
@ 2025-04-11 5:37 ` Changyuan Lyu
2025-04-22 13:31 ` Mike Rapoport
2025-04-11 5:37 ` [PATCH v6 13/14] Documentation: add documentation for KHO Changyuan Lyu
` (2 subsequent siblings)
14 siblings, 1 reply; 26+ messages in thread
From: Changyuan Lyu @ 2025-04-11 5:37 UTC (permalink / raw)
To: linux-kernel
Cc: akpm, anthony.yznaga, arnd, ashish.kalra, benh, bp,
catalin.marinas, corbet, dave.hansen, devicetree, dwmw2, ebiederm,
graf, hpa, jgowans, kexec, krzk, linux-arm-kernel, linux-doc,
linux-mm, luto, mark.rutland, mingo, pasha.tatashin, pbonzini,
peterz, ptyadav, robh, rostedt, rppt, saravanak, skinsburskii,
tglx, thomas.lendacky, will, x86, Changyuan Lyu
From: Alexander Graf <graf@amazon.com>
Linux has recently gained support for "reserve_mem": A mechanism to
allocate a region of memory early enough in boot that we can cross our
fingers and hope it stays at the same location during most boots, so we
can store for example ftrace buffers into it.
Thanks to KASLR, we can never be really sure that "reserve_mem"
allocations are static across kexec. Let's teach it KHO awareness so
that it serializes its reservations on kexec exit and deserializes them
again on boot, preserving the exact same mapping across kexec.
This is an example user for KHO in the KHO patch set to ensure we have
at least one (not very controversial) user in the tree before extending
KHO's use to more subsystems.
Signed-off-by: Alexander Graf <graf@amazon.com>
Co-developed-by: Mike Rapoport (Microsoft) <rppt@kernel.org>
Signed-off-by: Mike Rapoport (Microsoft) <rppt@kernel.org>
Co-developed-by: Changyuan Lyu <changyuanl@google.com>
Signed-off-by: Changyuan Lyu <changyuanl@google.com>
---
mm/memblock.c | 205 ++++++++++++++++++++++++++++++++++++++++++++++++++
1 file changed, 205 insertions(+)
diff --git a/mm/memblock.c b/mm/memblock.c
index 456689cb73e20..3571a859f2fe1 100644
--- a/mm/memblock.c
+++ b/mm/memblock.c
@@ -18,6 +18,11 @@
#include <linux/memblock.h>
#include <linux/mutex.h>
+#ifdef CONFIG_KEXEC_HANDOVER
+#include <linux/libfdt.h>
+#include <linux/kexec_handover.h>
+#endif /* CONFIG_KEXEC_HANDOVER */
+
#include <asm/sections.h>
#include <linux/io.h>
@@ -2475,6 +2480,201 @@ int reserve_mem_release_by_name(const char *name)
return 1;
}
+#ifdef CONFIG_KEXEC_HANDOVER
+#define MEMBLOCK_KHO_FDT "memblock"
+#define MEMBLOCK_KHO_NODE_COMPATIBLE "memblock-v1"
+#define RESERVE_MEM_KHO_NODE_COMPATIBLE "reserve-mem-v1"
+static struct page *kho_fdt;
+
+static int reserve_mem_kho_finalize(struct kho_serialization *ser)
+{
+ int err = 0, i;
+
+ if (!reserved_mem_count)
+ return NOTIFY_DONE;
+
+ if (IS_ERR(kho_fdt)) {
+ err = PTR_ERR(kho_fdt);
+ pr_err("memblock FDT was not prepared successfully: %d\n", err);
+ return notifier_from_errno(err);
+ }
+
+ for (i = 0; i < reserved_mem_count; i++) {
+ struct reserve_mem_table *map = &reserved_mem_table[i];
+
+ err |= kho_preserve_phys(ser, map->start, map->size);
+ }
+
+ err |= kho_preserve_folio(ser, page_folio(kho_fdt));
+ err |= kho_add_subtree(ser, MEMBLOCK_KHO_FDT, page_to_virt(kho_fdt));
+
+ return notifier_from_errno(err);
+}
+
+static int reserve_mem_kho_notifier(struct notifier_block *self,
+ unsigned long cmd, void *v)
+{
+ switch (cmd) {
+ case KEXEC_KHO_FINALIZE:
+ return reserve_mem_kho_finalize((struct kho_serialization *)v);
+ case KEXEC_KHO_ABORT:
+ return NOTIFY_DONE;
+ default:
+ return NOTIFY_BAD;
+ }
+}
+
+static struct notifier_block reserve_mem_kho_nb = {
+ .notifier_call = reserve_mem_kho_notifier,
+};
+
+static void __init prepare_kho_fdt(void)
+{
+ int err = 0, i;
+ void *fdt;
+
+ if (!reserved_mem_count)
+ return;
+
+ kho_fdt = alloc_page(GFP_KERNEL);
+ if (!kho_fdt) {
+ kho_fdt = ERR_PTR(-ENOMEM);
+ return;
+ }
+
+ fdt = page_to_virt(kho_fdt);
+
+ err |= fdt_create(fdt, PAGE_SIZE);
+ err |= fdt_finish_reservemap(fdt);
+
+ err |= fdt_begin_node(fdt, "");
+ err |= fdt_property_string(fdt, "compatible", MEMBLOCK_KHO_NODE_COMPATIBLE);
+ for (i = 0; i < reserved_mem_count; i++) {
+ struct reserve_mem_table *map = &reserved_mem_table[i];
+
+ err |= fdt_begin_node(fdt, map->name);
+ err |= fdt_property_string(fdt, "compatible", RESERVE_MEM_KHO_NODE_COMPATIBLE);
+ err |= fdt_property(fdt, "start", &map->start, sizeof(map->start));
+ err |= fdt_property(fdt, "size", &map->size, sizeof(map->size));
+ err |= fdt_end_node(fdt);
+ }
+ err |= fdt_end_node(fdt);
+
+ err |= fdt_finish(fdt);
+
+ if (err) {
+ pr_err("failed to prepare memblock FDT for KHO: %d\n", err);
+ put_page(kho_fdt);
+ kho_fdt = ERR_PTR(-EINVAL);
+ }
+}
+
+static int __init reserve_mem_init(void)
+{
+ if (!kho_is_enabled())
+ return 0;
+
+ prepare_kho_fdt();
+
+ return register_kho_notifier(&reserve_mem_kho_nb);
+}
+late_initcall(reserve_mem_init);
+
+static void *kho_fdt_in __initdata;
+
+static void *__init reserve_mem_kho_retrieve_fdt(void)
+{
+ phys_addr_t fdt_phys;
+ struct folio *fdt_folio;
+ void *fdt;
+ int err;
+
+ err = kho_retrieve_subtree(MEMBLOCK_KHO_FDT, &fdt_phys);
+ if (err) {
+ if (err != -ENOENT)
+ pr_warn("failed to retrieve FDT '%s' from KHO: %d\n",
+ MEMBLOCK_KHO_FDT, err);
+ return ERR_PTR(err);
+ }
+
+ fdt_folio = kho_restore_folio(fdt_phys);
+ if (!fdt_folio) {
+ pr_warn("failed to restore memblock KHO FDT (0x%llx)\n", fdt_phys);
+ return ERR_PTR(-EFAULT);
+ }
+
+ fdt = page_to_virt(folio_page(fdt_folio, 0));
+
+ err = fdt_node_check_compatible(fdt, 0, MEMBLOCK_KHO_NODE_COMPATIBLE);
+ if (err) {
+ pr_warn("FDT '%s' is incompatible with '%s': %d\n",
+ MEMBLOCK_KHO_FDT, MEMBLOCK_KHO_NODE_COMPATIBLE, err);
+ return ERR_PTR(-EINVAL);
+ }
+
+ return fdt;
+}
+
+static bool __init reserve_mem_kho_revive(const char *name, phys_addr_t size,
+ phys_addr_t align)
+{
+ int err, len_start, len_size, offset;
+ const phys_addr_t *p_start, *p_size;
+ const void *fdt;
+
+ if (!kho_fdt_in)
+ kho_fdt_in = reserve_mem_kho_retrieve_fdt();
+
+ if (IS_ERR(kho_fdt_in))
+ return false;
+
+ fdt = kho_fdt_in;
+
+ offset = fdt_subnode_offset(fdt, 0, name);
+ if (offset < 0) {
+ pr_warn("FDT '%s' has no child '%s': %d\n",
+ MEMBLOCK_KHO_FDT, name, offset);
+ return false;
+ }
+ err = fdt_node_check_compatible(fdt, offset, RESERVE_MEM_KHO_NODE_COMPATIBLE);
+ if (err) {
+ pr_warn("Node '%s' is incompatible with '%s': %d\n",
+ name, RESERVE_MEM_KHO_NODE_COMPATIBLE, err);
+ return false;
+ }
+
+ p_start = fdt_getprop(fdt, offset, "start", &len_start);
+ p_size = fdt_getprop(fdt, offset, "size", &len_size);
+ if (!p_start || len_start != sizeof(*p_start) || !p_size ||
+ len_size != sizeof(*p_size)) {
+ return false;
+ }
+
+ if (*p_start & (align - 1)) {
+ pr_warn("KHO reserve-mem '%s' has wrong alignment (0x%lx, 0x%lx)\n",
+ name, (long)align, (long)*p_start);
+ return false;
+ }
+
+ if (*p_size != size) {
+ pr_warn("KHO reserve-mem '%s' has wrong size (0x%lx != 0x%lx)\n",
+ name, (long)*p_size, (long)size);
+ return false;
+ }
+
+ reserved_mem_add(*p_start, size, name);
+ pr_info("Revived memory reservation '%s' from KHO\n", name);
+
+ return true;
+}
+#else
+static bool __init reserve_mem_kho_revive(const char *name, phys_addr_t size,
+ phys_addr_t align)
+{
+ return false;
+}
+#endif /* CONFIG_KEXEC_HANDOVER */
+
/*
* Parse reserve_mem=nn:align:name
*/
@@ -2530,6 +2730,11 @@ static int __init reserve_mem(char *p)
if (reserve_mem_find_by_name(name, &start, &tmp))
return -EBUSY;
+ /* Pick previous allocations up from KHO if available */
+ if (reserve_mem_kho_revive(name, size, align))
+ return 1;
+
+ /* TODO: Allocation must be outside of scratch region */
start = memblock_phys_alloc(size, align);
if (!start)
return -ENOMEM;
--
2.49.0.604.gff1f9ca942-goog
^ permalink raw reply related [flat|nested] 26+ messages in thread
* [PATCH v6 13/14] Documentation: add documentation for KHO
2025-04-11 5:37 [PATCH v6 00/14] kexec: introduce Kexec HandOver (KHO) Changyuan Lyu
` (11 preceding siblings ...)
2025-04-11 5:37 ` [PATCH v6 12/14] memblock: add KHO support for reserve_mem Changyuan Lyu
@ 2025-04-11 5:37 ` Changyuan Lyu
2025-04-11 5:37 ` [PATCH v6 14/14] Documentation: KHO: Add memblock bindings Changyuan Lyu
2025-04-28 22:19 ` [PATCH v6 00/14] kexec: introduce Kexec HandOver (KHO) Dave Hansen
14 siblings, 0 replies; 26+ messages in thread
From: Changyuan Lyu @ 2025-04-11 5:37 UTC (permalink / raw)
To: linux-kernel
Cc: akpm, anthony.yznaga, arnd, ashish.kalra, benh, bp,
catalin.marinas, corbet, dave.hansen, devicetree, dwmw2, ebiederm,
graf, hpa, jgowans, kexec, krzk, linux-arm-kernel, linux-doc,
linux-mm, luto, mark.rutland, mingo, pasha.tatashin, pbonzini,
peterz, ptyadav, robh, rostedt, rppt, saravanak, skinsburskii,
tglx, thomas.lendacky, will, x86, Changyuan Lyu
From: Alexander Graf <graf@amazon.com>
With KHO in place, let's add documentation that describes what it is and
how to use it.
Signed-off-by: Alexander Graf <graf@amazon.com>
Co-developed-by: Mike Rapoport (Microsoft) <rppt@kernel.org>
Signed-off-by: Mike Rapoport (Microsoft) <rppt@kernel.org>
Co-developed-by: Changyuan Lyu <changyuanl@google.com>
Signed-off-by: Changyuan Lyu <changyuanl@google.com>
---
.../admin-guide/kernel-parameters.txt | 25 ++++
Documentation/admin-guide/mm/index.rst | 1 +
Documentation/admin-guide/mm/kho.rst | 120 ++++++++++++++++++
Documentation/core-api/index.rst | 1 +
Documentation/core-api/kho/bindings/kho.yaml | 43 +++++++
.../core-api/kho/bindings/sub-fdt.yaml | 27 ++++
Documentation/core-api/kho/concepts.rst | 74 +++++++++++
Documentation/core-api/kho/fdt.rst | 80 ++++++++++++
Documentation/core-api/kho/index.rst | 13 ++
MAINTAINERS | 2 +
10 files changed, 386 insertions(+)
create mode 100644 Documentation/admin-guide/mm/kho.rst
create mode 100644 Documentation/core-api/kho/bindings/kho.yaml
create mode 100644 Documentation/core-api/kho/bindings/sub-fdt.yaml
create mode 100644 Documentation/core-api/kho/concepts.rst
create mode 100644 Documentation/core-api/kho/fdt.rst
create mode 100644 Documentation/core-api/kho/index.rst
diff --git a/Documentation/admin-guide/kernel-parameters.txt b/Documentation/admin-guide/kernel-parameters.txt
index 76e538c77e316..b7868ad3179d7 100644
--- a/Documentation/admin-guide/kernel-parameters.txt
+++ b/Documentation/admin-guide/kernel-parameters.txt
@@ -2728,6 +2728,31 @@
kgdbwait [KGDB,EARLY] Stop kernel execution and enter the
kernel debugger at the earliest opportunity.
+ kho= [KEXEC,EARLY]
+ Format: { "0" | "1" | "off" | "on" | "y" | "n" }
+ Enables or disables Kexec HandOver.
+ "0" | "off" | "n" - kexec handover is disabled
+ "1" | "on" | "y" - kexec handover is enabled
+
+ kho_scratch= [KEXEC,EARLY]
+ Format: ll[KMG],mm[KMG],nn[KMG] | nn%
+ Defines the size of the KHO scratch region. The KHO
+ scratch regions are physically contiguous memory
+ ranges that can only be used for non-kernel
+ allocations. That way, even when memory is heavily
+ fragmented with handed over memory, the kexeced
+ kernel will always have enough contiguous ranges to
+ bootstrap itself.
+
+ It is possible to specify the exact amount of
+ memory in the form of "ll[KMG],mm[KMG],nn[KMG]"
+ where the first parameter defines the size of a low
+ memory scratch area, the second parameter defines
+ the size of a global scratch area and the third
+ parameter defines the size of additional per-node
+ scratch areas. The form "nn%" defines scale factor
+ (in percents) of memory that was used during boot.
+
kmac= [MIPS] Korina ethernet MAC address.
Configure the RouterBoard 532 series on-chip
Ethernet adapter MAC address.
diff --git a/Documentation/admin-guide/mm/index.rst b/Documentation/admin-guide/mm/index.rst
index 8b35795b664be..2d2f6c222308f 100644
--- a/Documentation/admin-guide/mm/index.rst
+++ b/Documentation/admin-guide/mm/index.rst
@@ -42,3 +42,4 @@ the Linux memory management.
transhuge
userfaultfd
zswap
+ kho
diff --git a/Documentation/admin-guide/mm/kho.rst b/Documentation/admin-guide/mm/kho.rst
new file mode 100644
index 0000000000000..c64aa7aadb300
--- /dev/null
+++ b/Documentation/admin-guide/mm/kho.rst
@@ -0,0 +1,120 @@
+.. SPDX-License-Identifier: GPL-2.0-or-later
+
+====================
+Kexec Handover Usage
+====================
+
+Kexec HandOver (KHO) is a mechanism that allows Linux to preserve memory
+regions, which could contain serialized system states, across kexec.
+
+This document expects that you are familiar with the base KHO
+:ref:`concepts <concepts>`. If you have not read
+them yet, please do so now.
+
+Prerequisites
+=============
+
+KHO is available when the ``CONFIG_KEXEC_HANDOVER`` config option is set to y
+at compile time. Every KHO producer may have its own config option that you
+need to enable if you would like to preserve their respective state across
+kexec.
+
+To use KHO, please boot the kernel with the ``kho=on`` command line
+parameter. You may use ``kho_scratch`` parameter to define size of the
+scratch regions. For example ``kho_scratch=16M,512M,256M`` will reserve a
+16 MiB low memory scratch area, a 512 MiB global scratch region, and 256 MiB
+per NUMA node scratch regions on boot.
+
+Perform a KHO kexec
+===================
+
+First, before you perform a KHO kexec, you need to move the system into
+the :ref:`KHO finalization phase <finalization_phase>` ::
+
+ $ echo 1 > /sys/kernel/debug/kho/out/finalize
+
+After this command, the KHO FDT is available in
+``/sys/kernel/debug/kho/out/fdt``. Other subsystems may also register
+their own preserved sub FDTs under
+``/sys/kernel/debug/kho/out/sub_fdts/``.
+
+Next, load the target payload and kexec into it. It is important that you
+use the ``-s`` parameter to use the in-kernel kexec file loader, as user
+space kexec tooling currently has no support for KHO with the user space
+based file loader ::
+
+ # kexec -l Image --initrd=initrd -s
+ # kexec -e
+
+The new kernel will boot up and contain some of the previous kernel's state.
+
+For example, if you used ``reserve_mem`` command line parameter to create
+an early memory reservation, the new kernel will have that memory at the
+same physical address as the old kernel.
+
+Abort a KHO exec
+================
+
+You can move the system out of KHO finalization phase again by calling ::
+
+ $ echo 0 > /sys/kernel/debug/kho/out/active
+
+After this command, the KHO FDT is no longer available in
+``/sys/kernel/debug/kho/out/fdt``.
+
+debugfs Interfaces
+==================
+
+Currently KHO creates the following debugfs interfaces. Notice that these
+interfaces may change in the future. They will be moved to sysfs once KHO is
+stabilized.
+
+``/sys/kernel/debug/kho/out/finalize``
+ Kexec HandOver (KHO) allows Linux to transition the state of
+ compatible drivers into the next kexec'ed kernel. To do so,
+ device drivers will instruct KHO to preserve memory regions,
+ which could contain serialized kernel state.
+ While the state is serialized, they are unable to perform
+ any modifications to state that was serialized, such as
+ handed over memory allocations.
+
+ When this file contains "1", the system is in the transition
+ state. When contains "0", it is not. To switch between the
+ two states, echo the respective number into this file.
+
+``/sys/kernel/debug/kho/out/fdt``
+ When KHO state tree is finalized, the kernel exposes the
+ flattened device tree blob that carries its current KHO
+ state in this file. Kexec user space tooling can use this
+ as input file for the KHO payload image.
+
+``/sys/kernel/debug/kho/out/scratch_len``
+ To support continuous KHO kexecs, we need to reserve
+ physically contiguous memory regions that will always stay
+ available for future kexec allocations. This file describes
+ the length of these memory regions. Kexec user space tooling
+ can use this to determine where it should place its payload
+ images.
+
+``/sys/kernel/debug/kho/out/scratch_phys``
+ To support continuous KHO kexecs, we need to reserve
+ physically contiguous memory regions that will always stay
+ available for future kexec allocations. This file describes
+ the physical location of these memory regions. Kexec user space
+ tooling can use this to determine where it should place its
+ payload images.
+
+``/sys/kernel/debug/kho/out/sub_fdts/``
+ In the KHO finalization phase, KHO producers register their own
+ FDT blob under this directory.
+
+``/sys/kernel/debug/kho/in/fdt``
+ When the kernel was booted with Kexec HandOver (KHO),
+ the state tree that carries metadata about the previous
+ kernel's state is in this file in the format of flattened
+ device tree. This file may disappear when all consumers of
+ it finished to interpret their metadata.
+
+``/sys/kernel/debug/kho/in/sub_fdts/``
+ Similar to ``kho/out/sub_fdts/``, but contains sub FDT blobs
+ of KHO producers passed from the old kernel.
diff --git a/Documentation/core-api/index.rst b/Documentation/core-api/index.rst
index e9789bd381d80..7a4ca18ca6e2d 100644
--- a/Documentation/core-api/index.rst
+++ b/Documentation/core-api/index.rst
@@ -115,6 +115,7 @@ more memory-management documentation in Documentation/mm/index.rst.
pin_user_pages
boot-time-mm
gfp_mask-from-fs-io
+ kho/index
Interfaces for kernel debugging
===============================
diff --git a/Documentation/core-api/kho/bindings/kho.yaml b/Documentation/core-api/kho/bindings/kho.yaml
new file mode 100644
index 0000000000000..11e8ab7b219d9
--- /dev/null
+++ b/Documentation/core-api/kho/bindings/kho.yaml
@@ -0,0 +1,43 @@
+# SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)
+%YAML 1.2
+---
+title: Kexec HandOver (KHO) root tree
+
+maintainers:
+ - Mike Rapoport <rppt@kernel.org>
+ - Changyuan Lyu <changyuanl@google.com>
+
+description: |
+ System memory preserved by KHO across kexec.
+
+properties:
+ compatible:
+ enum:
+ - kho-v1
+
+ preserved-memory-map:
+ description: |
+ physical address (u64) of an in-memory structure describing all preserved
+ folios and memory ranges.
+
+patternProperties:
+ "$[0-9a-f_]+^":
+ $ref: sub-fdt.yaml#
+ description: physical address of a KHO user's own FDT.
+
+required:
+ - compatible
+ - preserved-memory-map
+
+additionalProperties: false
+
+examples:
+ - |
+ kho {
+ compatible = "kho-v1";
+ preserved-memory-map = <0xf0be16 0x1000000>;
+
+ memblock {
+ fdt = <0x80cc16 0x1000000>;
+ };
+ };
diff --git a/Documentation/core-api/kho/bindings/sub-fdt.yaml b/Documentation/core-api/kho/bindings/sub-fdt.yaml
new file mode 100644
index 0000000000000..b9a3d2d248501
--- /dev/null
+++ b/Documentation/core-api/kho/bindings/sub-fdt.yaml
@@ -0,0 +1,27 @@
+# SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)
+%YAML 1.2
+---
+title: KHO users' FDT address
+
+maintainers:
+ - Mike Rapoport <rppt@kernel.org>
+ - Changyuan Lyu <changyuanl@google.com>
+
+description: |
+ Physical address of an FDT blob registered by a KHO user.
+
+properties:
+ fdt:
+ description: |
+ physical address (u64) of an FDT blob.
+
+required:
+ - fdt
+
+additionalProperties: false
+
+examples:
+ - |
+ memblock {
+ fdt = <0x80cc16 0x1000000>;
+ };
diff --git a/Documentation/core-api/kho/concepts.rst b/Documentation/core-api/kho/concepts.rst
new file mode 100644
index 0000000000000..f1826ac10da75
--- /dev/null
+++ b/Documentation/core-api/kho/concepts.rst
@@ -0,0 +1,74 @@
+.. SPDX-License-Identifier: GPL-2.0-or-later
+.. _concepts:
+
+=======================
+Kexec Handover Concepts
+=======================
+
+Kexec HandOver (KHO) is a mechanism that allows Linux to preserve memory
+regions, which could contain serialized system states, across kexec.
+
+It introduces multiple concepts:
+
+KHO FDT
+=======
+
+Every KHO kexec carries a KHO specific flattened device tree (FDT) blob
+that describes preserved memory regions. These regions contain either
+serialized subsystem states, or in-memory data that shall not be touched
+across kexec. After KHO, subsystems can retrieve and restore preserved
+memory regions from KHO FDT.
+
+KHO only uses the FDT container format and libfdt library, but does not
+adhere to the same property semantics that normal device trees do: Properties
+are passed in native endianness and standardized properties like ``regs`` and
+``ranges`` do not exist, hence there are no ``#...-cells`` properties.
+
+KHO is still under development. The FDT schema is unstable and would change
+in the future.
+
+Scratch Regions
+===============
+
+To boot into kexec, we need to have a physically contiguous memory range that
+contains no handed over memory. Kexec then places the target kernel and initrd
+into that region. The new kernel exclusively uses this region for memory
+allocations before during boot up to the initialization of the page allocator.
+
+We guarantee that we always have such regions through the scratch regions: On
+first boot KHO allocates several physically contiguous memory regions. Since
+after kexec these regions will be used by early memory allocations, there is a
+scratch region per NUMA node plus a scratch region to satisfy allocations
+requests that do not require particular NUMA node assignment.
+By default, size of the scratch region is calculated based on amount of memory
+allocated during boot. The ``kho_scratch`` kernel command line option may be
+used to explicitly define size of the scratch regions.
+The scratch regions are declared as CMA when page allocator is initialized so
+that their memory can be used during system lifetime. CMA gives us the
+guarantee that no handover pages land in that region, because handover pages
+must be at a static physical memory location and CMA enforces that only
+movable pages can be located inside.
+
+After KHO kexec, we ignore the ``kho_scratch`` kernel command line option and
+instead reuse the exact same region that was originally allocated. This allows
+us to recursively execute any amount of KHO kexecs. Because we used this region
+for boot memory allocations and as target memory for kexec blobs, some parts
+of that memory region may be reserved. These reservations are irrelevant for
+the next KHO, because kexec can overwrite even the original kernel.
+
+.. _finalization_phase:
+
+KHO finalization phase
+======================
+
+To enable user space based kexec file loader, the kernel needs to be able to
+provide the FDT that describes the current kernel's state before
+performing the actual kexec. The process of generating that FDT is
+called serialization. When the FDT is generated, some properties
+of the system may become immutable because they are already written down
+in the FDT. That state is called the KHO finalization phase.
+
+Public API
+==========
+.. kernel-doc:: kernel/kexec_handover.c
+ :export:
diff --git a/Documentation/core-api/kho/fdt.rst b/Documentation/core-api/kho/fdt.rst
new file mode 100644
index 0000000000000..4a5d53c670d4b
--- /dev/null
+++ b/Documentation/core-api/kho/fdt.rst
@@ -0,0 +1,80 @@
+.. SPDX-License-Identifier: GPL-2.0-or-later
+
+=======
+KHO FDT
+=======
+
+KHO uses the flattened device tree (FDT) container format and libfdt
+library to create and parse the data that is passed between the
+kernels. The properties in KHO FDT are stored in native format.
+It includes the physical address of an in-memory structure describing
+all preserved memory regions, as well as physical addresses of KHO users'
+own FDTs. Interpreting those sub FDTs is the responsibility of KHO users.
+
+KHO nodes and properties
+========================
+
+Property ``preserved-memory-map``
+---------------------------------
+
+KHO saves a special property named ``preserved-memory-map`` under the root node.
+This node contains the physical address of an in-memory structure for KHO to
+preserve memory regions across kexec.
+
+Property ``compatible``
+-----------------------
+
+The ``compatible`` property determines compatibility between the kernel
+that created the KHO FDT and the kernel that attempts to load it.
+If the kernel that loads the KHO FDT is not compatible with it, the entire
+KHO process will be bypassed.
+
+Property ``fdt``
+----------------
+
+Generally, A KHO user serialize its state into its own FDT and instructs
+KHO to preserve the underlying memory, such that after kexec, the new kernel
+can recover its state from the preserved FDT.
+
+A KHO user thus can create a node in KHO root tree and save the physical address
+of its own FDT in that node's property ``fdt`` .
+
+Examples
+========
+
+The following example demonstrates KHO FDT that preserves two memory
+regions created with ``reserve_mem`` kernel command line parameter::
+
+ /dts-v1/;
+
+ / {
+ compatible = "kho-v1";
+
+ preserved-memory-map = <0x40be16 0x1000000>;
+
+ memblock {
+ fdt = <0x1517 0x1000000>;
+ };
+ };
+
+where the ``memblock`` node contains an FDT that is requested by the
+subsystem memblock for preservation. The FDT contains the following
+serialized data::
+
+ /dts-v1/;
+
+ / {
+ compatible = "memblock-v1";
+
+ n1 {
+ compatible = "reserve-mem-v1";
+ start = <0xc06b 0x4000000>;
+ size = <0x04 0x00>;
+ };
+
+ n2 {
+ compatible = "reserve-mem-v1";
+ start = <0xc067 0x4000000>;
+ size = <0x04 0x00>;
+ };
+ };
diff --git a/Documentation/core-api/kho/index.rst b/Documentation/core-api/kho/index.rst
new file mode 100644
index 0000000000000..0c63b0c5c1436
--- /dev/null
+++ b/Documentation/core-api/kho/index.rst
@@ -0,0 +1,13 @@
+.. SPDX-License-Identifier: GPL-2.0-or-later
+
+========================
+Kexec Handover Subsystem
+========================
+
+.. toctree::
+ :maxdepth: 1
+
+ concepts
+ fdt
+
+.. only:: subproject and html
diff --git a/MAINTAINERS b/MAINTAINERS
index 5f33e18385cbc..b3be800e5ea46 100644
--- a/MAINTAINERS
+++ b/MAINTAINERS
@@ -13123,6 +13123,8 @@ M: Mike Rapoport <rppt@kernel.org>
M: Changyuan Lyu <changyuanl@google.com>
L: kexec@lists.infradead.org
S: Maintained
+F: Documentation/admin-guide/mm/kho.rst
+F: Documentation/core-api/kho/*
F: include/linux/kexec_handover.h
F: kernel/kexec_handover.c
--
2.49.0.604.gff1f9ca942-goog
^ permalink raw reply related [flat|nested] 26+ messages in thread
* [PATCH v6 14/14] Documentation: KHO: Add memblock bindings
2025-04-11 5:37 [PATCH v6 00/14] kexec: introduce Kexec HandOver (KHO) Changyuan Lyu
` (12 preceding siblings ...)
2025-04-11 5:37 ` [PATCH v6 13/14] Documentation: add documentation for KHO Changyuan Lyu
@ 2025-04-11 5:37 ` Changyuan Lyu
2025-04-28 22:19 ` [PATCH v6 00/14] kexec: introduce Kexec HandOver (KHO) Dave Hansen
14 siblings, 0 replies; 26+ messages in thread
From: Changyuan Lyu @ 2025-04-11 5:37 UTC (permalink / raw)
To: linux-kernel
Cc: akpm, anthony.yznaga, arnd, ashish.kalra, benh, bp,
catalin.marinas, corbet, dave.hansen, devicetree, dwmw2, ebiederm,
graf, hpa, jgowans, kexec, krzk, linux-arm-kernel, linux-doc,
linux-mm, luto, mark.rutland, mingo, pasha.tatashin, pbonzini,
peterz, ptyadav, robh, rostedt, rppt, saravanak, skinsburskii,
tglx, thomas.lendacky, will, x86, Changyuan Lyu
From: "Mike Rapoport (Microsoft)" <rppt@kernel.org>
We introduced KHO into Linux: A framework that allows Linux to pass
metadata and memory across kexec from Linux to Linux. KHO reuses fdt
as file format and shares a lot of the same properties of firmware-to-
Linux boot formats: It needs a stable, documented ABI that allows for
forward and backward compatibility as well as versioning.
As first user of KHO, we introduced memblock which can now preserve
memory ranges reserved with reserve_mem command line options contents
across kexec, so you can use the post-kexec kernel to read traces from
the pre-kexec kernel.
This patch adds memblock schemas similar to "device" device tree ones to
a new kho bindings directory. This allows us to force contributors to
document the data that moves across KHO kexecs and catch breaking change
during review.
Co-developed-by: Alexander Graf <graf@amazon.com>
Signed-off-by: Alexander Graf <graf@amazon.com>
Signed-off-by: Mike Rapoport (Microsoft) <rppt@kernel.org>
Signed-off-by: Changyuan Lyu <changyuanl@google.com>
---
.../kho/bindings/memblock/memblock.yaml | 39 ++++++++++++++++++
.../kho/bindings/memblock/reserve-mem.yaml | 40 +++++++++++++++++++
MAINTAINERS | 1 +
3 files changed, 80 insertions(+)
create mode 100644 Documentation/core-api/kho/bindings/memblock/memblock.yaml
create mode 100644 Documentation/core-api/kho/bindings/memblock/reserve-mem.yaml
diff --git a/Documentation/core-api/kho/bindings/memblock/memblock.yaml b/Documentation/core-api/kho/bindings/memblock/memblock.yaml
new file mode 100644
index 0000000000000..d388c28eb91d1
--- /dev/null
+++ b/Documentation/core-api/kho/bindings/memblock/memblock.yaml
@@ -0,0 +1,39 @@
+# SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)
+%YAML 1.2
+---
+title: Memblock reserved memory
+
+maintainers:
+ - Mike Rapoport <rppt@kernel.org>
+
+description: |
+ Memblock can serialize its current memory reservations created with
+ reserve_mem command line option across kexec through KHO.
+ The post-KHO kernel can then consume these reservations and they are
+ guaranteed to have the same physical address.
+
+properties:
+ compatible:
+ enum:
+ - reserve-mem-v1
+
+patternProperties:
+ "$[0-9a-f_]+^":
+ $ref: reserve-mem.yaml#
+ description: reserved memory regions
+
+required:
+ - compatible
+
+additionalProperties: false
+
+examples:
+ - |
+ memblock {
+ compatible = "memblock-v1";
+ n1 {
+ compatible = "reserve-mem-v1";
+ start = <0xc06b 0x4000000>;
+ size = <0x04 0x00>;
+ };
+ };
diff --git a/Documentation/core-api/kho/bindings/memblock/reserve-mem.yaml b/Documentation/core-api/kho/bindings/memblock/reserve-mem.yaml
new file mode 100644
index 0000000000000..10282d3d1bcdc
--- /dev/null
+++ b/Documentation/core-api/kho/bindings/memblock/reserve-mem.yaml
@@ -0,0 +1,40 @@
+# SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)
+%YAML 1.2
+---
+title: Memblock reserved memory regions
+
+maintainers:
+ - Mike Rapoport <rppt@kernel.org>
+
+description: |
+ Memblock can serialize its current memory reservations created with
+ reserve_mem command line option across kexec through KHO.
+ This object describes each such region.
+
+properties:
+ compatible:
+ enum:
+ - reserve-mem-v1
+
+ start:
+ description: |
+ physical address (u64) of the reserved memory region.
+
+ size:
+ description: |
+ size (u64) of the reserved memory region.
+
+required:
+ - compatible
+ - start
+ - size
+
+additionalProperties: false
+
+examples:
+ - |
+ n1 {
+ compatible = "reserve-mem-v1";
+ start = <0xc06b 0x4000000>;
+ size = <0x04 0x00>;
+ };
diff --git a/MAINTAINERS b/MAINTAINERS
index b3be800e5ea46..60e4093042e1b 100644
--- a/MAINTAINERS
+++ b/MAINTAINERS
@@ -15425,6 +15425,7 @@ M: Mike Rapoport <rppt@kernel.org>
L: linux-mm@kvack.org
S: Maintained
F: Documentation/core-api/boot-time-mm.rst
+F: Documentation/core-api/kho/bindings/memblock/*
F: include/linux/memblock.h
F: mm/memblock.c
F: mm/mm_init.c
--
2.49.0.604.gff1f9ca942-goog
^ permalink raw reply related [flat|nested] 26+ messages in thread
* Re: [PATCH v6 12/14] memblock: add KHO support for reserve_mem
2025-04-11 5:37 ` [PATCH v6 12/14] memblock: add KHO support for reserve_mem Changyuan Lyu
@ 2025-04-22 13:31 ` Mike Rapoport
2025-04-24 8:32 ` Changyuan Lyu
0 siblings, 1 reply; 26+ messages in thread
From: Mike Rapoport @ 2025-04-22 13:31 UTC (permalink / raw)
To: Changyuan Lyu
Cc: linux-kernel, akpm, anthony.yznaga, arnd, ashish.kalra, benh, bp,
catalin.marinas, corbet, dave.hansen, devicetree, dwmw2, ebiederm,
graf, hpa, jgowans, kexec, krzk, linux-arm-kernel, linux-doc,
linux-mm, luto, mark.rutland, mingo, pasha.tatashin, pbonzini,
peterz, ptyadav, robh, rostedt, saravanak, skinsburskii, tglx,
thomas.lendacky, will, x86
On Thu, Apr 10, 2025 at 10:37:43PM -0700, Changyuan Lyu wrote:
> From: Alexander Graf <graf@amazon.com>
>
> Linux has recently gained support for "reserve_mem": A mechanism to
> allocate a region of memory early enough in boot that we can cross our
> fingers and hope it stays at the same location during most boots, so we
> can store for example ftrace buffers into it.
>
> Thanks to KASLR, we can never be really sure that "reserve_mem"
> allocations are static across kexec. Let's teach it KHO awareness so
> that it serializes its reservations on kexec exit and deserializes them
> again on boot, preserving the exact same mapping across kexec.
>
> This is an example user for KHO in the KHO patch set to ensure we have
> at least one (not very controversial) user in the tree before extending
> KHO's use to more subsystems.
>
> Signed-off-by: Alexander Graf <graf@amazon.com>
> Co-developed-by: Mike Rapoport (Microsoft) <rppt@kernel.org>
> Signed-off-by: Mike Rapoport (Microsoft) <rppt@kernel.org>
> Co-developed-by: Changyuan Lyu <changyuanl@google.com>
> Signed-off-by: Changyuan Lyu <changyuanl@google.com>
> ---
> mm/memblock.c | 205 ++++++++++++++++++++++++++++++++++++++++++++++++++
> 1 file changed, 205 insertions(+)
>
> diff --git a/mm/memblock.c b/mm/memblock.c
> index 456689cb73e20..3571a859f2fe1 100644
> --- a/mm/memblock.c
> +++ b/mm/memblock.c
> @@ -18,6 +18,11 @@
> #include <linux/memblock.h>
> #include <linux/mutex.h>
>
> +#ifdef CONFIG_KEXEC_HANDOVER
> +#include <linux/libfdt.h>
> +#include <linux/kexec_handover.h>
> +#endif /* CONFIG_KEXEC_HANDOVER */
> +
> #include <asm/sections.h>
> #include <linux/io.h>
>
> @@ -2475,6 +2480,201 @@ int reserve_mem_release_by_name(const char *name)
> return 1;
> }
>
> +#ifdef CONFIG_KEXEC_HANDOVER
> +#define MEMBLOCK_KHO_FDT "memblock"
> +#define MEMBLOCK_KHO_NODE_COMPATIBLE "memblock-v1"
> +#define RESERVE_MEM_KHO_NODE_COMPATIBLE "reserve-mem-v1"
> +static struct page *kho_fdt;
> +
> +static int reserve_mem_kho_finalize(struct kho_serialization *ser)
> +{
> + int err = 0, i;
> +
> + if (!reserved_mem_count)
> + return NOTIFY_DONE;
> +
> + if (IS_ERR(kho_fdt)) {
> + err = PTR_ERR(kho_fdt);
> + pr_err("memblock FDT was not prepared successfully: %d\n", err);
> + return notifier_from_errno(err);
> + }
> +
> + for (i = 0; i < reserved_mem_count; i++) {
> + struct reserve_mem_table *map = &reserved_mem_table[i];
> +
> + err |= kho_preserve_phys(ser, map->start, map->size);
> + }
> +
> + err |= kho_preserve_folio(ser, page_folio(kho_fdt));
> + err |= kho_add_subtree(ser, MEMBLOCK_KHO_FDT, page_to_virt(kho_fdt));
> +
> + return notifier_from_errno(err);
> +}
> +
> +static int reserve_mem_kho_notifier(struct notifier_block *self,
> + unsigned long cmd, void *v)
> +{
> + switch (cmd) {
> + case KEXEC_KHO_FINALIZE:
> + return reserve_mem_kho_finalize((struct kho_serialization *)v);
> + case KEXEC_KHO_ABORT:
> + return NOTIFY_DONE;
> + default:
> + return NOTIFY_BAD;
> + }
> +}
> +
> +static struct notifier_block reserve_mem_kho_nb = {
> + .notifier_call = reserve_mem_kho_notifier,
> +};
> +
> +static void __init prepare_kho_fdt(void)
> +{
> + int err = 0, i;
> + void *fdt;
> +
> + if (!reserved_mem_count)
> + return;
It's better to have this check in reserve_mem_init() before registering kho
notifier.
> +
> + kho_fdt = alloc_page(GFP_KERNEL);
> + if (!kho_fdt) {
> + kho_fdt = ERR_PTR(-ENOMEM);
Do we really care about having errno in kho_fdt? I think NULL would work
just fine.
> + return;
And actually, it makes sense to me to return -ENOMEM here and let
reserve_mem_init() bail out before registering notifier if fdt preparation
failed.
That will save the checks in reserve_mem_kho_finalize() because it would be
called only if we have reserve_mem areas and fdt is ready.
> + }
> +
> + fdt = page_to_virt(kho_fdt);
> +
> + err |= fdt_create(fdt, PAGE_SIZE);
> + err |= fdt_finish_reservemap(fdt);
> +
> + err |= fdt_begin_node(fdt, "");
> + err |= fdt_property_string(fdt, "compatible", MEMBLOCK_KHO_NODE_COMPATIBLE);
> + for (i = 0; i < reserved_mem_count; i++) {
> + struct reserve_mem_table *map = &reserved_mem_table[i];
> +
> + err |= fdt_begin_node(fdt, map->name);
> + err |= fdt_property_string(fdt, "compatible", RESERVE_MEM_KHO_NODE_COMPATIBLE);
> + err |= fdt_property(fdt, "start", &map->start, sizeof(map->start));
> + err |= fdt_property(fdt, "size", &map->size, sizeof(map->size));
> + err |= fdt_end_node(fdt);
> + }
> + err |= fdt_end_node(fdt);
> +
> + err |= fdt_finish(fdt);
> +
> + if (err) {
> + pr_err("failed to prepare memblock FDT for KHO: %d\n", err);
> + put_page(kho_fdt);
> + kho_fdt = ERR_PTR(-EINVAL);
> + }
> +}
> +
> +static int __init reserve_mem_init(void)
> +{
> + if (!kho_is_enabled())
> + return 0;
> +
> + prepare_kho_fdt();
> +
> + return register_kho_notifier(&reserve_mem_kho_nb);
> +}
> +late_initcall(reserve_mem_init);
> +
> +static void *kho_fdt_in __initdata;
> +
> +static void *__init reserve_mem_kho_retrieve_fdt(void)
> +{
> + phys_addr_t fdt_phys;
> + struct folio *fdt_folio;
> + void *fdt;
> + int err;
> +
> + err = kho_retrieve_subtree(MEMBLOCK_KHO_FDT, &fdt_phys);
> + if (err) {
> + if (err != -ENOENT)
> + pr_warn("failed to retrieve FDT '%s' from KHO: %d\n",
> + MEMBLOCK_KHO_FDT, err);
> + return ERR_PTR(err);
Wouldn't just 'return NULL' work here?
> + }
> +
> + fdt_folio = kho_restore_folio(fdt_phys);
> + if (!fdt_folio) {
> + pr_warn("failed to restore memblock KHO FDT (0x%llx)\n", fdt_phys);
> + return ERR_PTR(-EFAULT);
> + }
> +
> + fdt = page_to_virt(folio_page(fdt_folio, 0));
fdt = folio_address(folio);
> +
> + err = fdt_node_check_compatible(fdt, 0, MEMBLOCK_KHO_NODE_COMPATIBLE);
> + if (err) {
> + pr_warn("FDT '%s' is incompatible with '%s': %d\n",
> + MEMBLOCK_KHO_FDT, MEMBLOCK_KHO_NODE_COMPATIBLE, err);
> + return ERR_PTR(-EINVAL);
> + }
> +
> + return fdt;
> +}
> +
> +static bool __init reserve_mem_kho_revive(const char *name, phys_addr_t size,
> + phys_addr_t align)
> +{
> + int err, len_start, len_size, offset;
> + const phys_addr_t *p_start, *p_size;
> + const void *fdt;
> +
> + if (!kho_fdt_in)
> + kho_fdt_in = reserve_mem_kho_retrieve_fdt();
I'd invert this and move to reserve_mem_kho_retrieve_fdt(), so there it
would be
if (kho_fdt_in)
return kho_fdt_in;
/* actually retrieve the fdt */
kho_fdt_in = fdt;
return fdt;
and here
fdt = reserve_mem_kho_retrieve_fdt();
if (!fdt)
return false;
> +
> + if (IS_ERR(kho_fdt_in))
> + return false;
> +
> + fdt = kho_fdt_in;
> +
> + offset = fdt_subnode_offset(fdt, 0, name);
> + if (offset < 0) {
> + pr_warn("FDT '%s' has no child '%s': %d\n",
> + MEMBLOCK_KHO_FDT, name, offset);
> + return false;
> + }
> + err = fdt_node_check_compatible(fdt, offset, RESERVE_MEM_KHO_NODE_COMPATIBLE);
> + if (err) {
> + pr_warn("Node '%s' is incompatible with '%s': %d\n",
> + name, RESERVE_MEM_KHO_NODE_COMPATIBLE, err);
> + return false;
> + }
> +
> + p_start = fdt_getprop(fdt, offset, "start", &len_start);
> + p_size = fdt_getprop(fdt, offset, "size", &len_size);
> + if (!p_start || len_start != sizeof(*p_start) || !p_size ||
> + len_size != sizeof(*p_size)) {
> + return false;
> + }
> +
> + if (*p_start & (align - 1)) {
> + pr_warn("KHO reserve-mem '%s' has wrong alignment (0x%lx, 0x%lx)\n",
> + name, (long)align, (long)*p_start);
> + return false;
> + }
> +
> + if (*p_size != size) {
> + pr_warn("KHO reserve-mem '%s' has wrong size (0x%lx != 0x%lx)\n",
> + name, (long)*p_size, (long)size);
> + return false;
> + }
> +
> + reserved_mem_add(*p_start, size, name);
> + pr_info("Revived memory reservation '%s' from KHO\n", name);
> +
> + return true;
> +}
> +#else
> +static bool __init reserve_mem_kho_revive(const char *name, phys_addr_t size,
> + phys_addr_t align)
> +{
> + return false;
> +}
> +#endif /* CONFIG_KEXEC_HANDOVER */
> +
> /*
> * Parse reserve_mem=nn:align:name
> */
> @@ -2530,6 +2730,11 @@ static int __init reserve_mem(char *p)
> if (reserve_mem_find_by_name(name, &start, &tmp))
> return -EBUSY;
>
> + /* Pick previous allocations up from KHO if available */
> + if (reserve_mem_kho_revive(name, size, align))
> + return 1;
> +
> + /* TODO: Allocation must be outside of scratch region */
> start = memblock_phys_alloc(size, align);
> if (!start)
> return -ENOMEM;
> --
> 2.49.0.604.gff1f9ca942-goog
>
--
Sincerely yours,
Mike.
^ permalink raw reply [flat|nested] 26+ messages in thread
* Re: [PATCH v6 12/14] memblock: add KHO support for reserve_mem
2025-04-22 13:31 ` Mike Rapoport
@ 2025-04-24 8:32 ` Changyuan Lyu
0 siblings, 0 replies; 26+ messages in thread
From: Changyuan Lyu @ 2025-04-24 8:32 UTC (permalink / raw)
To: rppt, akpm
Cc: anthony.yznaga, arnd, ashish.kalra, benh, bp, catalin.marinas,
changyuanl, corbet, dave.hansen, devicetree, dwmw2, ebiederm,
graf, hpa, jgowans, kexec, krzk, linux-arm-kernel, linux-doc,
linux-kernel, linux-mm, luto, mark.rutland, mingo, pasha.tatashin,
pbonzini, peterz, ptyadav, robh, rostedt, saravanak, skinsburskii,
tglx, thomas.lendacky, will, x86
On Tue, Apr 22, 2025 at 16:31:19 +0300, Mike Rapoport <rppt@kernel.org> wrote:
> On Thu, Apr 10, 2025 at 10:37:43PM -0700, Changyuan Lyu wrote:
> > [...]
> > +static struct notifier_block reserve_mem_kho_nb = {
> > + .notifier_call = reserve_mem_kho_notifier,
> > +};
> > +
> > +static void __init prepare_kho_fdt(void)
> > +{
> > + int err = 0, i;
> > + void *fdt;
> > +
> > + if (!reserved_mem_count)
> > + return;
>
> It's better to have this check in reserve_mem_init() before registering kho
> notifier.
Sounds good!
> > +
> > + kho_fdt = alloc_page(GFP_KERNEL);
> > + if (!kho_fdt) {
> > + kho_fdt = ERR_PTR(-ENOMEM);
>
> Do we really care about having errno in kho_fdt? I think NULL would work
> just fine.
I was originally using ERR_PTR(-ENOMEM) and NULL to differentiate the following
2 cases:
1. prepare_kho_fdt() failed,
2. reserved_mem_count == 0, so no memblock FDT was created.
Based on the suggestion above, since now we only register the notifier when
reserved_mem_count == 0, case 2 shall never happen. So NULL is enough.
> > + return;
>
> And actually, it makes sense to me to return -ENOMEM here and let
> reserve_mem_init() bail out before registering notifier if fdt preparation
> failed.
>
> That will save the checks in reserve_mem_kho_finalize() because it would be
> called only if we have reserve_mem areas and fdt is ready.
>
Sounds good!
> > + }
> > +
> > + fdt = page_to_virt(kho_fdt);
> > +
> > + err |= fdt_create(fdt, PAGE_SIZE);
> > + err |= fdt_finish_reservemap(fdt);
> > +
> > + err |= fdt_begin_node(fdt, "");
> > + err |= fdt_property_string(fdt, "compatible", MEMBLOCK_KHO_NODE_COMPATIBLE);
> > + for (i = 0; i < reserved_mem_count; i++) {
> > + struct reserve_mem_table *map = &reserved_mem_table[i];
> > +
> > + err |= fdt_begin_node(fdt, map->name);
> > + err |= fdt_property_string(fdt, "compatible", RESERVE_MEM_KHO_NODE_COMPATIBLE);
> > + err |= fdt_property(fdt, "start", &map->start, sizeof(map->start));
> > + err |= fdt_property(fdt, "size", &map->size, sizeof(map->size));
> > + err |= fdt_end_node(fdt);
> > + }
> > + err |= fdt_end_node(fdt);
> > +
> > + err |= fdt_finish(fdt);
> > +
> > + if (err) {
> > + pr_err("failed to prepare memblock FDT for KHO: %d\n", err);
> > + put_page(kho_fdt);
> > + kho_fdt = ERR_PTR(-EINVAL);
> > + }
> > +}
> > +
> > +static int __init reserve_mem_init(void)
> > +{
> > + if (!kho_is_enabled())
> > + return 0;
> > +
> > + prepare_kho_fdt();
> > +
> > + return register_kho_notifier(&reserve_mem_kho_nb);
> > +}
> > +late_initcall(reserve_mem_init);
> > +
> > +static void *kho_fdt_in __initdata;
> > +
> > +static void *__init reserve_mem_kho_retrieve_fdt(void)
> > +{
> > + phys_addr_t fdt_phys;
> > + struct folio *fdt_folio;
> > + void *fdt;
> > + int err;
> > +
> > + err = kho_retrieve_subtree(MEMBLOCK_KHO_FDT, &fdt_phys);
> > + if (err) {
> > + if (err != -ENOENT)
> > + pr_warn("failed to retrieve FDT '%s' from KHO: %d\n",
> > + MEMBLOCK_KHO_FDT, err);
> > + return ERR_PTR(err);
>
> Wouldn't just 'return NULL' work here?
If we have multiple `reserve_mem` in the kernel command line,
reserve_mem_kho_revive() will also be called multiple times. However
reserve_mem_kho_retrieve_fdt() should only be called once.
Here I am returning the ERR_PTR(err) such that if the first
reserve_mem_kho_retrieve_fdt() failed, subsequent reserve_mem_kho_revive()
can tell that reserve_mem_kho_retrieve_fdt() has failed so no need to try it
again. If we return NULL here, subsequent reserve_mem_kho_revive() would find
kho_fdt_in == NULL, and it could not tell whether it was due to previously
failed reserve_mem_kho_retrieve_fdt(), or it is the first
reserve_mem_kho_revive().
> > + }
> > +
> > + fdt_folio = kho_restore_folio(fdt_phys);
> > + if (!fdt_folio) {
> > + pr_warn("failed to restore memblock KHO FDT (0x%llx)\n", fdt_phys);
> > + return ERR_PTR(-EFAULT);
> > + }
> > +
> > + fdt = page_to_virt(folio_page(fdt_folio, 0));
>
> fdt = folio_address(folio);
Fixed.
> > +
> > + err = fdt_node_check_compatible(fdt, 0, MEMBLOCK_KHO_NODE_COMPATIBLE);
> > + if (err) {
> > + pr_warn("FDT '%s' is incompatible with '%s': %d\n",
> > + MEMBLOCK_KHO_FDT, MEMBLOCK_KHO_NODE_COMPATIBLE, err);
> > + return ERR_PTR(-EINVAL);
> > + }
> > +
> > + return fdt;
> > +}
> > +
> > +static bool __init reserve_mem_kho_revive(const char *name, phys_addr_t size,
> > + phys_addr_t align)
> > +{
> > + int err, len_start, len_size, offset;
> > + const phys_addr_t *p_start, *p_size;
> > + const void *fdt;
> > +
> > + if (!kho_fdt_in)
> > + kho_fdt_in = reserve_mem_kho_retrieve_fdt();
>
> I'd invert this and move to reserve_mem_kho_retrieve_fdt(), so there it
> would be
>
> if (kho_fdt_in)
> return kho_fdt_in;
>
> /* actually retrieve the fdt */
> kho_fdt_in = fdt;
>
> return fdt;
>
> and here
>
> fdt = reserve_mem_kho_retrieve_fdt();
> if (!fdt)
> return false;
Ah Ok, this is more elegant!
> > +
> > + if (IS_ERR(kho_fdt_in))
> > + return false;
> > +
> > + fdt = kho_fdt_in;
> > +
> >[...]
> > --
> > 2.49.0.604.gff1f9ca942-goog
> >
>
> --
> Sincerely yours,
> Mike.
Best,
Changyuan
---- 8< ----
From 7ad4379062aa9709d3702bfc53d237d0c1a4e326 Mon Sep 17 00:00:00 2001
From: Changyuan Lyu <changyuanl@google.com>
Date: Thu, 24 Apr 2025 01:10:24 -0700
Subject: [PATCH] fixup! memblock: add KHO support for reserve_mem
This patch includes the suggested changes from
https://lore.kernel.org/lkml/aAeaJ2iqkrv_ffhT@kernel.org/ and can be
squashed with "memblock: add KHO support for reserve_mem".
Fixes: 2e257a656639 ("memblock: add KHO support for reserve_mem")
Suggested-by: Mike Rapoport (Microsoft) <rppt@kernel.org>
Signed-off-by: Changyuan Lyu <changyuanl@google.com>
---
mm/memblock.c | 69 +++++++++++++++++++++++++--------------------------
1 file changed, 34 insertions(+), 35 deletions(-)
diff --git a/mm/memblock.c b/mm/memblock.c
index 3571a859f2fe1..d38a72f07ea0c 100644
--- a/mm/memblock.c
+++ b/mm/memblock.c
@@ -2490,15 +2490,6 @@ static int reserve_mem_kho_finalize(struct kho_serialization *ser)
{
int err = 0, i;
- if (!reserved_mem_count)
- return NOTIFY_DONE;
-
- if (IS_ERR(kho_fdt)) {
- err = PTR_ERR(kho_fdt);
- pr_err("memblock FDT was not prepared successfully: %d\n", err);
- return notifier_from_errno(err);
- }
-
for (i = 0; i < reserved_mem_count; i++) {
struct reserve_mem_table *map = &reserved_mem_table[i];
@@ -2528,19 +2519,14 @@ static struct notifier_block reserve_mem_kho_nb = {
.notifier_call = reserve_mem_kho_notifier,
};
-static void __init prepare_kho_fdt(void)
+static int __init prepare_kho_fdt(void)
{
int err = 0, i;
void *fdt;
- if (!reserved_mem_count)
- return;
-
kho_fdt = alloc_page(GFP_KERNEL);
- if (!kho_fdt) {
- kho_fdt = ERR_PTR(-ENOMEM);
- return;
- }
+ if (!kho_fdt)
+ return -ENOMEM;
fdt = page_to_virt(kho_fdt);
@@ -2565,18 +2551,30 @@ static void __init prepare_kho_fdt(void)
if (err) {
pr_err("failed to prepare memblock FDT for KHO: %d\n", err);
put_page(kho_fdt);
- kho_fdt = ERR_PTR(-EINVAL);
+ kho_fdt = NULL;
}
+
+ return err;
}
static int __init reserve_mem_init(void)
{
- if (!kho_is_enabled())
+ int err;
+
+ if (!kho_is_enabled() || !reserved_mem_count)
return 0;
- prepare_kho_fdt();
+ err = prepare_kho_fdt();
+ if (err)
+ return err;
+
+ err = register_kho_notifier(&reserve_mem_kho_nb);
+ if (err) {
+ put_page(kho_fdt);
+ kho_fdt = NULL;
+ }
- return register_kho_notifier(&reserve_mem_kho_nb);
+ return err;
}
late_initcall(reserve_mem_init);
@@ -2586,33 +2584,38 @@ static void *__init reserve_mem_kho_retrieve_fdt(void)
{
phys_addr_t fdt_phys;
struct folio *fdt_folio;
- void *fdt;
int err;
+ if (kho_fdt_in)
+ return kho_fdt_in;
+
err = kho_retrieve_subtree(MEMBLOCK_KHO_FDT, &fdt_phys);
if (err) {
if (err != -ENOENT)
pr_warn("failed to retrieve FDT '%s' from KHO: %d\n",
MEMBLOCK_KHO_FDT, err);
- return ERR_PTR(err);
+ goto out;
}
fdt_folio = kho_restore_folio(fdt_phys);
if (!fdt_folio) {
pr_warn("failed to restore memblock KHO FDT (0x%llx)\n", fdt_phys);
- return ERR_PTR(-EFAULT);
+ err = -EFAULT;
+ goto out;
}
- fdt = page_to_virt(folio_page(fdt_folio, 0));
+ kho_fdt_in = folio_address(fdt_folio);
- err = fdt_node_check_compatible(fdt, 0, MEMBLOCK_KHO_NODE_COMPATIBLE);
+ err = fdt_node_check_compatible(kho_fdt_in, 0, MEMBLOCK_KHO_NODE_COMPATIBLE);
if (err) {
pr_warn("FDT '%s' is incompatible with '%s': %d\n",
MEMBLOCK_KHO_FDT, MEMBLOCK_KHO_NODE_COMPATIBLE, err);
- return ERR_PTR(-EINVAL);
+ err = -EFAULT;
}
-
- return fdt;
+out:
+ if (err)
+ kho_fdt_in = ERR_PTR(err);
+ return kho_fdt_in;
}
static bool __init reserve_mem_kho_revive(const char *name, phys_addr_t size,
@@ -2622,14 +2625,10 @@ static bool __init reserve_mem_kho_revive(const char *name, phys_addr_t size,
const phys_addr_t *p_start, *p_size;
const void *fdt;
- if (!kho_fdt_in)
- kho_fdt_in = reserve_mem_kho_retrieve_fdt();
-
- if (IS_ERR(kho_fdt_in))
+ fdt = reserve_mem_kho_retrieve_fdt();
+ if (IS_ERR(fdt))
return false;
- fdt = kho_fdt_in;
-
offset = fdt_subnode_offset(fdt, 0, name);
if (offset < 0) {
pr_warn("FDT '%s' has no child '%s': %d\n",
--
2.49.0.805.g082f7c87e0-goog
^ permalink raw reply related [flat|nested] 26+ messages in thread
* Re: [PATCH v6 11/14] x86: add KHO support
2025-04-11 5:37 ` [PATCH v6 11/14] x86: add KHO support Changyuan Lyu
@ 2025-04-28 22:05 ` Dave Hansen
2025-04-29 8:06 ` Mike Rapoport
2025-04-29 15:53 ` Mike Rapoport
0 siblings, 2 replies; 26+ messages in thread
From: Dave Hansen @ 2025-04-28 22:05 UTC (permalink / raw)
To: Changyuan Lyu, linux-kernel
Cc: akpm, anthony.yznaga, arnd, ashish.kalra, benh, bp,
catalin.marinas, corbet, dave.hansen, devicetree, dwmw2, ebiederm,
graf, hpa, jgowans, kexec, krzk, linux-arm-kernel, linux-doc,
linux-mm, luto, mark.rutland, mingo, pasha.tatashin, pbonzini,
peterz, ptyadav, robh, rostedt, rppt, saravanak, skinsburskii,
tglx, thomas.lendacky, will, x86
On 4/10/25 22:37, Changyuan Lyu wrote:
> From: Alexander Graf <graf@amazon.com>
>
> We now have all bits in place to support KHO kexecs. This patch adds
Please use imperative voice and also avoid the "this patch" stuff.
...
> +/*
> + * If KHO is active, only process its scratch areas to ensure we are not
> + * stepping onto preserved memory.
> + */
Same thing on the imperative voice here.
I'm also not fully understanding the comment. Do these "scratch" regions
basically represent all the memory that's not being handed over? It's
not obvious.
> +#ifdef CONFIG_KEXEC_HANDOVER
> +static bool process_kho_entries(unsigned long minimum, unsigned long image_size)
> +{
> + struct kho_scratch *kho_scratch;
> + struct setup_data *ptr;
> + int i, nr_areas = 0;
Do these really need actual #ifdefs or will a nice IS_ENABLED() check
work instead?
> + ptr = (struct setup_data *)(unsigned long)boot_params_ptr->hdr.setup_data;
What's with the double cast?
> + while (ptr) {
> + if (ptr->type == SETUP_KEXEC_KHO) {
> + struct kho_data *kho = (struct kho_data *)ptr->data;
> +
> + kho_scratch = (void *)kho->scratch_addr;
> + nr_areas = kho->scratch_size / sizeof(*kho_scratch);
> +
> + break;
> + }
> +
> + ptr = (struct setup_data *)(unsigned long)ptr->next;
> + }
Wow, there are a solid number of these loops, each with similarly fun
casting.
> + if (!nr_areas)
> + return false;
> +
> + for (i = 0; i < nr_areas; i++) {
> + struct kho_scratch *area = &kho_scratch[i];
> + struct mem_vector region = {
> + .start = area->addr,
> + .size = area->size,
> + };
> +
> + if (process_mem_region(®ion, minimum, image_size))
> + break;
> + }
> +
> + return true;
> +}
> +#else
> +static inline bool process_kho_entries(unsigned long minimum,
> + unsigned long image_size)
> +{
> + return false;
> +}
> +#endif
> +
> static unsigned long find_random_phys_addr(unsigned long minimum,
> unsigned long image_size)
> {
> @@ -775,7 +824,8 @@ static unsigned long find_random_phys_addr(unsigned long minimum,
> return 0;
> }
>
> - if (!process_efi_entries(minimum, image_size))
> + if (!process_kho_entries(minimum, image_size) &&
> + !process_efi_entries(minimum, image_size))
> process_e820_entries(minimum, image_size);
Oh, so KHO really does replace the other memory maps here. That seems
important to call out.
> phys_addr = slots_fetch_random();
> diff --git a/arch/x86/include/asm/setup.h b/arch/x86/include/asm/setup.h
> index ad9212df0ec0c..906ab5e6d25fe 100644
> --- a/arch/x86/include/asm/setup.h
> +++ b/arch/x86/include/asm/setup.h
> @@ -67,6 +67,10 @@ extern void x86_ce4100_early_setup(void);
> static inline void x86_ce4100_early_setup(void) { }
> #endif
>
> +#ifdef CONFIG_KEXEC_HANDOVER
> +#include <linux/kexec_handover.h>
> +#endif
Please do this #ifdef'ing inside the kexec_handover.h header if at all
possible.
> diff --git a/arch/x86/include/uapi/asm/setup_data.h b/arch/x86/include/uapi/asm/setup_data.h
> index 50c45ead4e7c9..2671c4e1b3a0b 100644
> --- a/arch/x86/include/uapi/asm/setup_data.h
> +++ b/arch/x86/include/uapi/asm/setup_data.h
> @@ -13,7 +13,8 @@
> #define SETUP_CC_BLOB 7
> #define SETUP_IMA 8
> #define SETUP_RNG_SEED 9
> -#define SETUP_ENUM_MAX SETUP_RNG_SEED
> +#define SETUP_KEXEC_KHO 10
> +#define SETUP_ENUM_MAX SETUP_KEXEC_KHO
>
> #define SETUP_INDIRECT (1<<31)
> #define SETUP_TYPE_MAX (SETUP_ENUM_MAX | SETUP_INDIRECT)
> @@ -78,6 +79,16 @@ struct ima_setup_data {
> __u64 size;
> } __attribute__((packed));
>
> +/*
> + * Locations of kexec handover metadata
> + */
> +struct kho_data {
> + __u64 fdt_addr;
> + __u64 fdt_size;
> + __u64 scratch_addr;
> + __u64 scratch_size;
> +} __attribute__((packed));
> +
> #endif /* __ASSEMBLER__ */
>
> #endif /* _UAPI_ASM_X86_SETUP_DATA_H */
> diff --git a/arch/x86/kernel/e820.c b/arch/x86/kernel/e820.c
> index 57120f0749cc3..c314212a5ecd5 100644
> --- a/arch/x86/kernel/e820.c
> +++ b/arch/x86/kernel/e820.c
> @@ -1300,6 +1300,24 @@ void __init e820__memblock_setup(void)
> memblock_add(entry->addr, entry->size);
> }
>
> + /*
> + * At this point with KHO we only allocate from scratch memory.
> + * At the same time, we configure memblock to only allow
> + * allocations from memory below ISA_END_ADDRESS which is not
> + * a natural scratch region, because Linux ignores memory below
> + * ISA_END_ADDRESS at runtime. Beside very few (if any) early
> + * allocations, we must allocate real-mode trapoline below
trampoline ^
> + * ISA_END_ADDRESS.
> + *
> + * To make sure that we can actually perform allocations during
> + * this phase, let's mark memory below ISA_END_ADDRESS as scratch
> + * so we can allocate from there in a scratch-only world.
> + *
> + * After real mode trampoline is allocated, we clear scratch
> + * marking from the memory below ISA_END_ADDRESS
> + */
> + memblock_mark_kho_scratch(0, ISA_END_ADDRESS);
This isn't making a whole ton of sense to me.
Is this *only* to facilitate possible users that need <ISA_END_ADDRESS
allocations? If so, please say that.
I _think_ this is trying to say that KHO kernels are special and are
trying to only allocate from scratch areas. But <ISA_END_ADDRESS
allocations are both necessary and not marked by KHO _as_ a scratch area
which causes a problem.
Also, it's a bit goofy that this code does the "mark" with
ISA_END_ADDRESS and the clear with SZ_1M:
memblock_clear_kho_scratch(0, SZ_1M);
That seems unnecessarily oblique.
> /* Throw away partial pages: */
> memblock_trim_memory(PAGE_SIZE);
>
> diff --git a/arch/x86/kernel/kexec-bzimage64.c b/arch/x86/kernel/kexec-bzimage64.c
> index 68530fad05f74..518635cc0876c 100644
> --- a/arch/x86/kernel/kexec-bzimage64.c
> +++ b/arch/x86/kernel/kexec-bzimage64.c
> @@ -233,6 +233,31 @@ setup_ima_state(const struct kimage *image, struct boot_params *params,
> #endif /* CONFIG_IMA_KEXEC */
> }
>
> +static void setup_kho(const struct kimage *image, struct boot_params *params,
> + unsigned long params_load_addr,
> + unsigned int setup_data_offset)
> +{
> +#ifdef CONFIG_KEXEC_HANDOVER
Can this #ifdef be replaced with IS_ENABLED()?
> + struct setup_data *sd = (void *)params + setup_data_offset;
> + struct kho_data *kho = (void *)sd + sizeof(*sd);
> +
> + sd->type = SETUP_KEXEC_KHO;
> + sd->len = sizeof(struct kho_data);
> +
> + /* Only add if we have all KHO images in place */
> + if (!image->kho.fdt || !image->kho.scratch)
> + return;
> +
> + /* Add setup data */
> + kho->fdt_addr = image->kho.fdt;
> + kho->fdt_size = PAGE_SIZE;
> + kho->scratch_addr = image->kho.scratch->mem;
> + kho->scratch_size = image->kho.scratch->bufsz;
> + sd->next = params->hdr.setup_data;
> + params->hdr.setup_data = params_load_addr + setup_data_offset;
> +#endif /* CONFIG_KEXEC_HANDOVER */
> +}
> +
> static int
> setup_boot_parameters(struct kimage *image, struct boot_params *params,
> unsigned long params_load_addr,
> @@ -312,6 +337,13 @@ setup_boot_parameters(struct kimage *image, struct boot_params *params,
> sizeof(struct ima_setup_data);
> }
>
> + if (IS_ENABLED(CONFIG_KEXEC_HANDOVER)) {
> + /* Setup space to store preservation metadata */
> + setup_kho(image, params, params_load_addr, setup_data_offset);
> + setup_data_offset += sizeof(struct setup_data) +
> + sizeof(struct kho_data);
> + }
This is a bit weird that it needs this IS_ENABLED() check and the
earlier #ifdef. But I guess it's following the IMA_KEXEC code's pattern
at least.
> /* Setup RNG seed */
> setup_rng_seed(params, params_load_addr, setup_data_offset);
>
> @@ -479,6 +511,10 @@ static void *bzImage64_load(struct kimage *image, char *kernel,
> kbuf.bufsz += sizeof(struct setup_data) +
> sizeof(struct ima_setup_data);
>
> + if (IS_ENABLED(CONFIG_KEXEC_HANDOVER))
> + kbuf.bufsz += sizeof(struct setup_data) +
> + sizeof(struct kho_data);
> +
> params = kzalloc(kbuf.bufsz, GFP_KERNEL);
> if (!params)
> return ERR_PTR(-ENOMEM);
> diff --git a/arch/x86/kernel/setup.c b/arch/x86/kernel/setup.c
> index 766176c4f5ee8..496dae89cf95d 100644
> --- a/arch/x86/kernel/setup.c
> +++ b/arch/x86/kernel/setup.c
> @@ -451,6 +451,28 @@ int __init ima_get_kexec_buffer(void **addr, size_t *size)
> }
> #endif
>
> +static void __init add_kho(u64 phys_addr, u32 data_len)
> +{
> +#ifdef CONFIG_KEXEC_HANDOVER
> + struct kho_data *kho;
> + u64 addr = phys_addr + sizeof(struct setup_data);
> + u64 size = data_len - sizeof(struct setup_data);
> +
> + kho = early_memremap(addr, size);
> + if (!kho) {
> + pr_warn("setup: failed to memremap kho data (0x%llx, 0x%llx)\n",
> + addr, size);
> + return;
> + }
> +
> + kho_populate(kho->fdt_addr, kho->fdt_size, kho->scratch_addr, kho->scratch_size);
> +
> + early_memunmap(kho, size);
> +#else
> + pr_warn("Passed KHO data, but CONFIG_KEXEC_HANDOVER not set. Ignoring.\n");
> +#endif
> +}
Please axe the #ifdef in the .c file if at all possible, just like the
others.
> static void __init parse_setup_data(void)
> {
> struct setup_data *data;
> @@ -479,6 +501,9 @@ static void __init parse_setup_data(void)
> case SETUP_IMA:
> add_early_ima_buffer(pa_data);
> break;
> + case SETUP_KEXEC_KHO:
> + add_kho(pa_data, data_len);
> + break;
> case SETUP_RNG_SEED:
> data = early_memremap(pa_data, data_len);
> add_bootloader_randomness(data->data, data->len);
> diff --git a/arch/x86/realmode/init.c b/arch/x86/realmode/init.c
> index f9bc444a3064d..9b9f4534086d2 100644
> --- a/arch/x86/realmode/init.c
> +++ b/arch/x86/realmode/init.c
> @@ -65,6 +65,8 @@ void __init reserve_real_mode(void)
> * setup_arch().
> */
> memblock_reserve(0, SZ_1M);
> +
> + memblock_clear_kho_scratch(0, SZ_1M);
Eek. Needs a comment, badly.
Overall this doesn't look bad. It might be useful to break it up into a
couple of patches, but it's not horrific as-is.
^ permalink raw reply [flat|nested] 26+ messages in thread
* Re: [PATCH v6 10/14] x86/setup: use memblock_reserve_kern for memory used by kernel
2025-04-11 5:37 ` [PATCH v6 10/14] x86/setup: use memblock_reserve_kern for memory used by kernel Changyuan Lyu
@ 2025-04-28 22:15 ` Dave Hansen
0 siblings, 0 replies; 26+ messages in thread
From: Dave Hansen @ 2025-04-28 22:15 UTC (permalink / raw)
To: Changyuan Lyu, linux-kernel
Cc: akpm, anthony.yznaga, arnd, ashish.kalra, benh, bp,
catalin.marinas, corbet, dave.hansen, devicetree, dwmw2, ebiederm,
graf, hpa, jgowans, kexec, krzk, linux-arm-kernel, linux-doc,
linux-mm, luto, mark.rutland, mingo, pasha.tatashin, pbonzini,
peterz, ptyadav, robh, rostedt, rppt, saravanak, skinsburskii,
tglx, thomas.lendacky, will, x86
On 4/10/25 22:37, Changyuan Lyu wrote:
> From: "Mike Rapoport (Microsoft)" <rppt@kernel.org>
>
> memblock_reserve() does not distinguish memory used by firmware from memory
> used by kernel.
>
> The distinction is nice to have for accounting of early memory allocations
> and reservations, but it is essential for kexec handover (kho) to know how
> much memory kernel consumes during boot.
>
> Use memblock_reserve_kern() to reserve kernel memory, such as kernel image,
> initrd and setup data.
Acked-by: Dave Hansen <dave.hansen@linux.intel.com>
^ permalink raw reply [flat|nested] 26+ messages in thread
* Re: [PATCH v6 00/14] kexec: introduce Kexec HandOver (KHO)
2025-04-11 5:37 [PATCH v6 00/14] kexec: introduce Kexec HandOver (KHO) Changyuan Lyu
` (13 preceding siblings ...)
2025-04-11 5:37 ` [PATCH v6 14/14] Documentation: KHO: Add memblock bindings Changyuan Lyu
@ 2025-04-28 22:19 ` Dave Hansen
14 siblings, 0 replies; 26+ messages in thread
From: Dave Hansen @ 2025-04-28 22:19 UTC (permalink / raw)
To: Changyuan Lyu, linux-kernel
Cc: akpm, anthony.yznaga, arnd, ashish.kalra, benh, bp,
catalin.marinas, corbet, dave.hansen, devicetree, dwmw2, ebiederm,
graf, hpa, jgowans, kexec, krzk, linux-arm-kernel, linux-doc,
linux-mm, luto, mark.rutland, mingo, pasha.tatashin, pbonzini,
peterz, ptyadav, robh, rostedt, rppt, saravanak, skinsburskii,
tglx, thomas.lendacky, will, x86
On 4/10/25 22:37, Changyuan Lyu wrote:
> When the actual kexec happens, the fdt is part of the image
> set that we boot into. In addition, we keep "scratch regions" available
> for kexec: physically contiguous memory regions that are guaranteed to
> not have any memory that KHO would preserve. The new kernel bootstraps
> itself using the scratch regions and sets all handed over memory as in use.
> When drivers initialize that support KHO, they introspect the fdt, restore
> preserved memory regions, and retrieve their states stored in the preserved
> memory.
It would be nice to have some bit of this blurb in the per-arch
changelogs. It's not clear what the scratch regions are and how they are
being used in the early boot code. Even a pointer over to the
Documentation/ would be appreciated.
^ permalink raw reply [flat|nested] 26+ messages in thread
* Re: [PATCH v6 11/14] x86: add KHO support
2025-04-28 22:05 ` Dave Hansen
@ 2025-04-29 8:06 ` Mike Rapoport
2025-04-29 16:06 ` Dave Hansen
2025-04-29 15:53 ` Mike Rapoport
1 sibling, 1 reply; 26+ messages in thread
From: Mike Rapoport @ 2025-04-29 8:06 UTC (permalink / raw)
To: Dave Hansen
Cc: Changyuan Lyu, linux-kernel, akpm, anthony.yznaga, arnd,
ashish.kalra, benh, bp, catalin.marinas, corbet, dave.hansen,
devicetree, dwmw2, ebiederm, graf, hpa, jgowans, kexec, krzk,
linux-arm-kernel, linux-doc, linux-mm, luto, mark.rutland, mingo,
pasha.tatashin, pbonzini, peterz, ptyadav, robh, rostedt,
saravanak, skinsburskii, tglx, thomas.lendacky, will, x86
On Mon, Apr 28, 2025 at 03:05:55PM -0700, Dave Hansen wrote:
> On 4/10/25 22:37, Changyuan Lyu wrote:
> > From: Alexander Graf <graf@amazon.com>
> >
> > +/*
> > + * If KHO is active, only process its scratch areas to ensure we are not
> > + * stepping onto preserved memory.
> > + */
>
> Same thing on the imperative voice here.
>
> I'm also not fully understanding the comment. Do these "scratch" regions
> basically represent all the memory that's not being handed over? It's
> not obvious.
Scratch memory represents areas created at the boot of the first kernel and
it's known that scratch areas won't contain any memory that's being handed
over.
> > diff --git a/arch/x86/kernel/e820.c b/arch/x86/kernel/e820.c
> > index 57120f0749cc3..c314212a5ecd5 100644
> > --- a/arch/x86/kernel/e820.c
> > +++ b/arch/x86/kernel/e820.c
> > @@ -1300,6 +1300,24 @@ void __init e820__memblock_setup(void)
> > memblock_add(entry->addr, entry->size);
> > }
> >
> > + /*
> > + * At this point with KHO we only allocate from scratch memory.
> > + * At the same time, we configure memblock to only allow
> > + * allocations from memory below ISA_END_ADDRESS which is not
> > + * a natural scratch region, because Linux ignores memory below
> > + * ISA_END_ADDRESS at runtime. Beside very few (if any) early
> > + * allocations, we must allocate real-mode trapoline below
>
> trampoline ^
>
> > + * ISA_END_ADDRESS.
> > + *
> > + * To make sure that we can actually perform allocations during
> > + * this phase, let's mark memory below ISA_END_ADDRESS as scratch
> > + * so we can allocate from there in a scratch-only world.
> > + *
> > + * After real mode trampoline is allocated, we clear scratch
> > + * marking from the memory below ISA_END_ADDRESS
> > + */
> > + memblock_mark_kho_scratch(0, ISA_END_ADDRESS);
>
> This isn't making a whole ton of sense to me.
>
> Is this *only* to facilitate possible users that need <ISA_END_ADDRESS
> allocations? If so, please say that.
>
> I _think_ this is trying to say that KHO kernels are special and are
> trying to only allocate from scratch areas. But <ISA_END_ADDRESS
> allocations are both necessary and not marked by KHO _as_ a scratch area
> which causes a problem.
Yes :)
--
Sincerely yours,
Mike.
^ permalink raw reply [flat|nested] 26+ messages in thread
* Re: [PATCH v6 11/14] x86: add KHO support
2025-04-28 22:05 ` Dave Hansen
2025-04-29 8:06 ` Mike Rapoport
@ 2025-04-29 15:53 ` Mike Rapoport
2025-04-29 16:05 ` Dave Hansen
1 sibling, 1 reply; 26+ messages in thread
From: Mike Rapoport @ 2025-04-29 15:53 UTC (permalink / raw)
To: Dave Hansen
Cc: Changyuan Lyu, linux-kernel, akpm, anthony.yznaga, arnd,
ashish.kalra, benh, bp, catalin.marinas, corbet, dave.hansen,
devicetree, dwmw2, ebiederm, graf, hpa, jgowans, kexec, krzk,
linux-arm-kernel, linux-doc, linux-mm, luto, mark.rutland, mingo,
pasha.tatashin, pbonzini, peterz, ptyadav, robh, rostedt,
saravanak, skinsburskii, tglx, thomas.lendacky, will, x86
On Mon, Apr 28, 2025 at 03:05:55PM -0700, Dave Hansen wrote:
> On 4/10/25 22:37, Changyuan Lyu wrote:
> > From: Alexander Graf <graf@amazon.com>
> >
> > +#ifdef CONFIG_KEXEC_HANDOVER
> > +static bool process_kho_entries(unsigned long minimum, unsigned long image_size)
> > +{
> > + struct kho_scratch *kho_scratch;
> > + struct setup_data *ptr;
> > + int i, nr_areas = 0;
>
> Do these really need actual #ifdefs or will a nice IS_ENABLED() check
> work instead?
>
> > + ptr = (struct setup_data *)(unsigned long)boot_params_ptr->hdr.setup_data;
>
> What's with the double cast?
The double cast is required for this to be compiled on 32 bits (just like
in mem_avoid_overlap). The setup_data is all u64 and to cast it to a
pointer on 32 bit it has to go via unsigned long.
If we keep actual #ifdef we can drop the double cast because KHO depends on
CONFIG_KEXEC_FILE which is only enabled for 64 bits.
> > diff --git a/arch/x86/kernel/kexec-bzimage64.c b/arch/x86/kernel/kexec-bzimage64.c
> > index 68530fad05f74..518635cc0876c 100644
> > --- a/arch/x86/kernel/kexec-bzimage64.c
> > +++ b/arch/x86/kernel/kexec-bzimage64.c
> > @@ -233,6 +233,31 @@ setup_ima_state(const struct kimage *image, struct boot_params *params,
> > #endif /* CONFIG_IMA_KEXEC */
> > }
> >
> > +static void setup_kho(const struct kimage *image, struct boot_params *params,
> > + unsigned long params_load_addr,
> > + unsigned int setup_data_offset)
> > +{
> > +#ifdef CONFIG_KEXEC_HANDOVER
>
> Can this #ifdef be replaced with IS_ENABLED()?
The KHO structures in kexec image are under #ifdef, so it won't compile
with IS_ENABLED().
> > @@ -312,6 +337,13 @@ setup_boot_parameters(struct kimage *image, struct boot_params *params,
> > sizeof(struct ima_setup_data);
> > }
> >
> > + if (IS_ENABLED(CONFIG_KEXEC_HANDOVER)) {
> > + /* Setup space to store preservation metadata */
> > + setup_kho(image, params, params_load_addr, setup_data_offset);
> > + setup_data_offset += sizeof(struct setup_data) +
> > + sizeof(struct kho_data);
> > + }
>
> This is a bit weird that it needs this IS_ENABLED() check and the
> earlier #ifdef. But I guess it's following the IMA_KEXEC code's pattern
> at least.
And with other #ifdefs as well :)
if (IS_ENABLED()) can be dropped here, but having it makes things more
explicit IMHO.
> > diff --git a/arch/x86/kernel/setup.c b/arch/x86/kernel/setup.c
> > index 766176c4f5ee8..496dae89cf95d 100644
> > --- a/arch/x86/kernel/setup.c
> > +++ b/arch/x86/kernel/setup.c
> > @@ -451,6 +451,28 @@ int __init ima_get_kexec_buffer(void **addr, size_t *size)
> > }
> > #endif
> >
> > +static void __init add_kho(u64 phys_addr, u32 data_len)
> > +{
> > +#ifdef CONFIG_KEXEC_HANDOVER
> > + struct kho_data *kho;
> > + u64 addr = phys_addr + sizeof(struct setup_data);
> > + u64 size = data_len - sizeof(struct setup_data);
> > +
> > + kho = early_memremap(addr, size);
> > + if (!kho) {
> > + pr_warn("setup: failed to memremap kho data (0x%llx, 0x%llx)\n",
> > + addr, size);
> > + return;
> > + }
> > +
> > + kho_populate(kho->fdt_addr, kho->fdt_size, kho->scratch_addr, kho->scratch_size);
> > +
> > + early_memunmap(kho, size);
> > +#else
> > + pr_warn("Passed KHO data, but CONFIG_KEXEC_HANDOVER not set. Ignoring.\n");
> > +#endif
> > +}
>
> Please axe the #ifdef in the .c file if at all possible, just like the
> others.
This one follows IMA, but it's easy to make it IS_ENABLED(). It's really up
to x86 folks preference.
--
Sincerely yours,
Mike.
^ permalink raw reply [flat|nested] 26+ messages in thread
* Re: [PATCH v6 11/14] x86: add KHO support
2025-04-29 15:53 ` Mike Rapoport
@ 2025-04-29 16:05 ` Dave Hansen
2025-04-29 16:34 ` Mike Rapoport
0 siblings, 1 reply; 26+ messages in thread
From: Dave Hansen @ 2025-04-29 16:05 UTC (permalink / raw)
To: Mike Rapoport
Cc: Changyuan Lyu, linux-kernel, akpm, anthony.yznaga, arnd,
ashish.kalra, benh, bp, catalin.marinas, corbet, dave.hansen,
devicetree, dwmw2, ebiederm, graf, hpa, jgowans, kexec, krzk,
linux-arm-kernel, linux-doc, linux-mm, luto, mark.rutland, mingo,
pasha.tatashin, pbonzini, peterz, ptyadav, robh, rostedt,
saravanak, skinsburskii, tglx, thomas.lendacky, will, x86
On 4/29/25 08:53, Mike Rapoport wrote:
> On Mon, Apr 28, 2025 at 03:05:55PM -0700, Dave Hansen wrote:
>> On 4/10/25 22:37, Changyuan Lyu wrote:
>>> From: Alexander Graf <graf@amazon.com>
>>>
>>> +#ifdef CONFIG_KEXEC_HANDOVER
>>> +static bool process_kho_entries(unsigned long minimum, unsigned long image_size)
>>> +{
>>> + struct kho_scratch *kho_scratch;
>>> + struct setup_data *ptr;
>>> + int i, nr_areas = 0;
>>
>> Do these really need actual #ifdefs or will a nice IS_ENABLED() check
>> work instead?
>>
>>> + ptr = (struct setup_data *)(unsigned long)boot_params_ptr->hdr.setup_data;
>>
>> What's with the double cast?
>
> The double cast is required for this to be compiled on 32 bits (just like
> in mem_avoid_overlap). The setup_data is all u64 and to cast it to a
> pointer on 32 bit it has to go via unsigned long.
Let's just make KHO depend on 64BIT, at least on x86.
>>> diff --git a/arch/x86/kernel/kexec-bzimage64.c b/arch/x86/kernel/kexec-bzimage64.c
>>> index 68530fad05f74..518635cc0876c 100644
>>> --- a/arch/x86/kernel/kexec-bzimage64.c
>>> +++ b/arch/x86/kernel/kexec-bzimage64.c
>>> @@ -233,6 +233,31 @@ setup_ima_state(const struct kimage *image, struct boot_params *params,
>>> #endif /* CONFIG_IMA_KEXEC */
>>> }
>>>
>>> +static void setup_kho(const struct kimage *image, struct boot_params *params,
>>> + unsigned long params_load_addr,
>>> + unsigned int setup_data_offset)
>>> +{
>>> +#ifdef CONFIG_KEXEC_HANDOVER
>>
>> Can this #ifdef be replaced with IS_ENABLED()?
>
> The KHO structures in kexec image are under #ifdef, so it won't compile
> with IS_ENABLED().
They shouldn't be. Define them unconditionally, please.
...
>> Please axe the #ifdef in the .c file if at all possible, just like the
>> others.
>
> This one follows IMA, but it's easy to make it IS_ENABLED(). It's really up
> to x86 folks preference.
Last I checked, I'm listed under the big M: for "X86 ARCHITECTURE". ;)
^ permalink raw reply [flat|nested] 26+ messages in thread
* Re: [PATCH v6 11/14] x86: add KHO support
2025-04-29 8:06 ` Mike Rapoport
@ 2025-04-29 16:06 ` Dave Hansen
2025-04-29 16:32 ` Mike Rapoport
0 siblings, 1 reply; 26+ messages in thread
From: Dave Hansen @ 2025-04-29 16:06 UTC (permalink / raw)
To: Mike Rapoport
Cc: Changyuan Lyu, linux-kernel, akpm, anthony.yznaga, arnd,
ashish.kalra, benh, bp, catalin.marinas, corbet, dave.hansen,
devicetree, dwmw2, ebiederm, graf, hpa, jgowans, kexec, krzk,
linux-arm-kernel, linux-doc, linux-mm, luto, mark.rutland, mingo,
pasha.tatashin, pbonzini, peterz, ptyadav, robh, rostedt,
saravanak, skinsburskii, tglx, thomas.lendacky, will, x86
On 4/29/25 01:06, Mike Rapoport wrote:
> On Mon, Apr 28, 2025 at 03:05:55PM -0700, Dave Hansen wrote:
>> On 4/10/25 22:37, Changyuan Lyu wrote:
>>> From: Alexander Graf <graf@amazon.com>
>>>
>>> +/*
>>> + * If KHO is active, only process its scratch areas to ensure we are not
>>> + * stepping onto preserved memory.
>>> + */
>>
>> Same thing on the imperative voice here.
>>
>> I'm also not fully understanding the comment. Do these "scratch" regions
>> basically represent all the memory that's not being handed over? It's
>> not obvious.
>
> Scratch memory represents areas created at the boot of the first kernel and
> it's known that scratch areas won't contain any memory that's being handed
> over.
>
>>> diff --git a/arch/x86/kernel/e820.c b/arch/x86/kernel/e820.c
>>> index 57120f0749cc3..c314212a5ecd5 100644
>>> --- a/arch/x86/kernel/e820.c
>>> +++ b/arch/x86/kernel/e820.c
>>> @@ -1300,6 +1300,24 @@ void __init e820__memblock_setup(void)
>>> memblock_add(entry->addr, entry->size);
>>> }
>>>
>>> + /*
>>> + * At this point with KHO we only allocate from scratch memory.
>>> + * At the same time, we configure memblock to only allow
>>> + * allocations from memory below ISA_END_ADDRESS which is not
>>> + * a natural scratch region, because Linux ignores memory below
>>> + * ISA_END_ADDRESS at runtime. Beside very few (if any) early
>>> + * allocations, we must allocate real-mode trapoline below
>>
>> trampoline ^
>>
>>> + * ISA_END_ADDRESS.
>>> + *
>>> + * To make sure that we can actually perform allocations during
>>> + * this phase, let's mark memory below ISA_END_ADDRESS as scratch
>>> + * so we can allocate from there in a scratch-only world.
>>> + *
>>> + * After real mode trampoline is allocated, we clear scratch
>>> + * marking from the memory below ISA_END_ADDRESS
>>> + */
>>> + memblock_mark_kho_scratch(0, ISA_END_ADDRESS);
>>
>> This isn't making a whole ton of sense to me.
>>
>> Is this *only* to facilitate possible users that need <ISA_END_ADDRESS
>> allocations? If so, please say that.
>>
>> I _think_ this is trying to say that KHO kernels are special and are
>> trying to only allocate from scratch areas. But <ISA_END_ADDRESS
>> allocations are both necessary and not marked by KHO _as_ a scratch area
>> which causes a problem.
>
> Yes :)
So, on both of these, could the submitters please add or revise the
comments to make it more clear?
^ permalink raw reply [flat|nested] 26+ messages in thread
* Re: [PATCH v6 11/14] x86: add KHO support
2025-04-29 16:06 ` Dave Hansen
@ 2025-04-29 16:32 ` Mike Rapoport
0 siblings, 0 replies; 26+ messages in thread
From: Mike Rapoport @ 2025-04-29 16:32 UTC (permalink / raw)
To: Dave Hansen
Cc: Changyuan Lyu, linux-kernel, akpm, anthony.yznaga, arnd,
ashish.kalra, benh, bp, catalin.marinas, corbet, dave.hansen,
devicetree, dwmw2, ebiederm, graf, hpa, jgowans, kexec, krzk,
linux-arm-kernel, linux-doc, linux-mm, luto, mark.rutland, mingo,
pasha.tatashin, pbonzini, peterz, ptyadav, robh, rostedt,
saravanak, skinsburskii, tglx, thomas.lendacky, will, x86
On Tue, Apr 29, 2025 at 09:06:19AM -0700, Dave Hansen wrote:
> On 4/29/25 01:06, Mike Rapoport wrote:
> > On Mon, Apr 28, 2025 at 03:05:55PM -0700, Dave Hansen wrote:
> >> On 4/10/25 22:37, Changyuan Lyu wrote:
> >>> From: Alexander Graf <graf@amazon.com>
> >>>
> >>> @@ -1300,6 +1300,24 @@ void __init e820__memblock_setup(void)
> >>> memblock_add(entry->addr, entry->size);
> >>> }
> >>>
> >>> + /*
> >>> + * At this point with KHO we only allocate from scratch memory.
> >>> + * At the same time, we configure memblock to only allow
> >>> + * allocations from memory below ISA_END_ADDRESS which is not
> >>> + * a natural scratch region, because Linux ignores memory below
> >>> + * ISA_END_ADDRESS at runtime. Beside very few (if any) early
> >>> + * allocations, we must allocate real-mode trapoline below
> >>
> >> trampoline ^
> >>
> >>> + * ISA_END_ADDRESS.
> >>> + *
> >>> + * To make sure that we can actually perform allocations during
> >>> + * this phase, let's mark memory below ISA_END_ADDRESS as scratch
> >>> + * so we can allocate from there in a scratch-only world.
> >>> + *
> >>> + * After real mode trampoline is allocated, we clear scratch
> >>> + * marking from the memory below ISA_END_ADDRESS
> >>> + */
> >>> + memblock_mark_kho_scratch(0, ISA_END_ADDRESS);
> >>
> >> This isn't making a whole ton of sense to me.
> >>
> >> Is this *only* to facilitate possible users that need <ISA_END_ADDRESS
> >> allocations? If so, please say that.
> >>
> >> I _think_ this is trying to say that KHO kernels are special and are
> >> trying to only allocate from scratch areas. But <ISA_END_ADDRESS
> >> allocations are both necessary and not marked by KHO _as_ a scratch area
> >> which causes a problem.
> >
> > Yes :)
>
> So, on both of these, could the submitters please add or revise the
> comments to make it more clear?
Is this one clearer?
/*
* At this point memblock is only allowed to allocate from memory
* below 1M (aka ISA_END_ADDRESS) up until direct map is completely set
* up in init_mem_mapping().
*
* KHO kernels are special and use only scratch memory for memblock
* allocations, but memory below 1M is ignored by kernel after early
* boot and cannot be naturally marked as scratch.
*
* To allow allocation of the real-mode trampoline and a few (if any)
* other very early allocations from below 1M forcibly mark the memory
* below 1M as scratch.
*
* After real mode trampoline is allocated, we clear that scratch
* marking.
*/
memblock_mark_kho_scratch(0, SZ_1M);
--
Sincerely yours,
Mike.
^ permalink raw reply [flat|nested] 26+ messages in thread
* Re: [PATCH v6 11/14] x86: add KHO support
2025-04-29 16:05 ` Dave Hansen
@ 2025-04-29 16:34 ` Mike Rapoport
0 siblings, 0 replies; 26+ messages in thread
From: Mike Rapoport @ 2025-04-29 16:34 UTC (permalink / raw)
To: Dave Hansen
Cc: Changyuan Lyu, linux-kernel, akpm, anthony.yznaga, arnd,
ashish.kalra, benh, bp, catalin.marinas, corbet, dave.hansen,
devicetree, dwmw2, ebiederm, graf, hpa, jgowans, kexec, krzk,
linux-arm-kernel, linux-doc, linux-mm, luto, mark.rutland, mingo,
pasha.tatashin, pbonzini, peterz, ptyadav, robh, rostedt,
saravanak, skinsburskii, tglx, thomas.lendacky, will, x86
On Tue, Apr 29, 2025 at 09:05:02AM -0700, Dave Hansen wrote:
> On 4/29/25 08:53, Mike Rapoport wrote:
> > On Mon, Apr 28, 2025 at 03:05:55PM -0700, Dave Hansen wrote:
> >> On 4/10/25 22:37, Changyuan Lyu wrote:
> >>> From: Alexander Graf <graf@amazon.com>
> >>>
> >>> +#ifdef CONFIG_KEXEC_HANDOVER
> >>> +static bool process_kho_entries(unsigned long minimum, unsigned long image_size)
> >>> +{
> >>> + struct kho_scratch *kho_scratch;
> >>> + struct setup_data *ptr;
> >>> + int i, nr_areas = 0;
> >>
> >> Do these really need actual #ifdefs or will a nice IS_ENABLED() check
> >> work instead?
> >>
> >>> + ptr = (struct setup_data *)(unsigned long)boot_params_ptr->hdr.setup_data;
> >>
> >> What's with the double cast?
> >
> > The double cast is required for this to be compiled on 32 bits (just like
> > in mem_avoid_overlap). The setup_data is all u64 and to cast it to a
> > pointer on 32 bit it has to go via unsigned long.
>
> Let's just make KHO depend on 64BIT, at least on x86.
Ok, so we are keeping #ifdef and dropping double cast here.
> >>> diff --git a/arch/x86/kernel/kexec-bzimage64.c b/arch/x86/kernel/kexec-bzimage64.c
> >>> index 68530fad05f74..518635cc0876c 100644
> >>> --- a/arch/x86/kernel/kexec-bzimage64.c
> >>> +++ b/arch/x86/kernel/kexec-bzimage64.c
> >>> @@ -233,6 +233,31 @@ setup_ima_state(const struct kimage *image, struct boot_params *params,
> >>> #endif /* CONFIG_IMA_KEXEC */
> >>> }
> >>>
> >>> +static void setup_kho(const struct kimage *image, struct boot_params *params,
> >>> + unsigned long params_load_addr,
> >>> + unsigned int setup_data_offset)
> >>> +{
> >>> +#ifdef CONFIG_KEXEC_HANDOVER
> >>
> >> Can this #ifdef be replaced with IS_ENABLED()?
> >
> > The KHO structures in kexec image are under #ifdef, so it won't compile
> > with IS_ENABLED().
>
> They shouldn't be. Define them unconditionally, please.
>
> ...
> >> Please axe the #ifdef in the .c file if at all possible, just like the
> >> others.
> >
> > This one follows IMA, but it's easy to make it IS_ENABLED(). It's really up
> > to x86 folks preference.
>
> Last I checked, I'm listed under the big M: for "X86 ARCHITECTURE". ;)
I remember :)
--
Sincerely yours,
Mike.
^ permalink raw reply [flat|nested] 26+ messages in thread
end of thread, other threads:[~2025-04-29 17:46 UTC | newest]
Thread overview: 26+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-04-11 5:37 [PATCH v6 00/14] kexec: introduce Kexec HandOver (KHO) Changyuan Lyu
2025-04-11 5:37 ` [PATCH v6 01/14] memblock: add MEMBLOCK_RSRV_KERN flag Changyuan Lyu
2025-04-11 5:37 ` [PATCH v6 02/14] memblock: Add support for scratch memory Changyuan Lyu
2025-04-11 5:37 ` [PATCH v6 03/14] memblock: introduce memmap_init_kho_scratch() Changyuan Lyu
2025-04-11 5:37 ` [PATCH v6 04/14] kexec: add Kexec HandOver (KHO) generation helpers Changyuan Lyu
2025-04-11 5:37 ` [PATCH v6 05/14] kexec: add KHO parsing support Changyuan Lyu
2025-04-11 5:37 ` [PATCH v6 06/14] kexec: enable KHO support for memory preservation Changyuan Lyu
2025-04-11 5:37 ` [PATCH v6 07/14] kexec: add KHO support to kexec file loads Changyuan Lyu
2025-04-11 5:37 ` [PATCH v6 08/14] kexec: add config option for KHO Changyuan Lyu
2025-04-11 5:37 ` [PATCH v6 09/14] arm64: add KHO support Changyuan Lyu
2025-04-11 5:37 ` [PATCH v6 10/14] x86/setup: use memblock_reserve_kern for memory used by kernel Changyuan Lyu
2025-04-28 22:15 ` Dave Hansen
2025-04-11 5:37 ` [PATCH v6 11/14] x86: add KHO support Changyuan Lyu
2025-04-28 22:05 ` Dave Hansen
2025-04-29 8:06 ` Mike Rapoport
2025-04-29 16:06 ` Dave Hansen
2025-04-29 16:32 ` Mike Rapoport
2025-04-29 15:53 ` Mike Rapoport
2025-04-29 16:05 ` Dave Hansen
2025-04-29 16:34 ` Mike Rapoport
2025-04-11 5:37 ` [PATCH v6 12/14] memblock: add KHO support for reserve_mem Changyuan Lyu
2025-04-22 13:31 ` Mike Rapoport
2025-04-24 8:32 ` Changyuan Lyu
2025-04-11 5:37 ` [PATCH v6 13/14] Documentation: add documentation for KHO Changyuan Lyu
2025-04-11 5:37 ` [PATCH v6 14/14] Documentation: KHO: Add memblock bindings Changyuan Lyu
2025-04-28 22:19 ` [PATCH v6 00/14] kexec: introduce Kexec HandOver (KHO) Dave Hansen
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).