kexec: introduce Kexec HandOver (KHO)
Subsystems (12)
Mike Rapoport
maintainerv4 cover kexec: introduce Kexec HandOver (KHO)
From: "Mike Rapoport (Microsoft)" <[email protected]>
Hi,
This a next version of Alex's "kexec: Allow preservation of ftrace buffers" series (https://lore.kernel.org/all/[email protected]),
just to make things simpler instead of ftrace we decided to preserve "reserve_mem" regions.
The patches are also available in git:
https://git.kernel.org/rppt/h/kho/v4
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. When the root user enables KHO through /sys/kernel/kho/active, the kernel invokes callbacks to every driver that supports KHO to serialize its state. When the actual kexec happens, the fdt is part of the image set that we boot into. In addition, we keep a "scratch regions" available for kexec: A physically contiguous memory regions that is 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 and recover their state from it. This includes memory reservations, where the driver can either discard or claim reservations.
== 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=512M,256M" will create a global scratch area of 512Mib and per-node scrath areas of 256Mib.
Make sure to to have a reserved memory range requested with reserv_mem command line option. Then before you invoke file based "kexec -l", activate KHO:
# echo 1 > /sys/kernel/kho/active # kexec -l Image --initrd=initrd -s # kexec -e
The new kernel will boot up and contain the previous kernel's reserve_mem contents at the same physical address as the first kernel.
== Changelog ==
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
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
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
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
kexec: Add documentation for KHO
arm64: Add KHO support
x86: Add KHO support
memblock: Add KHO support for reserve_mem
Mike Rapoport (Microsoft) (5):
mm/mm_init: rename init_reserved_page to init_deferred_page
memblock: add MEMBLOCK_RSRV_KERN flag
memblock: introduce memmap_init_kho_scratch() x86/setup: use memblock_reserve_kern for memory used by kernel
Documentation: KHO: Add memblock bindings
Documentation/ABI/testing/sysfs-firmware-kho | 9 + Documentation/ABI/testing/sysfs-kernel-kho | 53 ++ .../admin-guide/kernel-parameters.txt | 24 + .../kho/bindings/memblock/reserve_mem.yaml | 41 + .../bindings/memblock/reserve_mem_map.yaml | 42 + Documentation/kho/concepts.rst | 80 ++ Documentation/kho/index.rst | 19 + Documentation/kho/usage.rst | 60 ++ Documentation/subsystem-apis.rst | 1 + MAINTAINERS | 3 + 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 | 39 +- arch/x86/realmode/init.c | 2 + drivers/of/fdt.c | 36 + drivers/of/kexec.c | 42 + include/linux/cma.h | 2 + include/linux/kexec.h | 37 + include/linux/kexec_handover.h | 10 + include/linux/memblock.h | 38 +- kernel/Kconfig.kexec | 13 + kernel/Makefile | 1 + kernel/kexec_file.c | 19 + kernel/kexec_handover.c | 808 ++++++++++++++++++ kernel/kexec_internal.h | 16 + mm/Kconfig | 4 + mm/internal.h | 5 +- mm/memblock.c | 247 +++++- mm/mm_init.c | 19 +- 34 files changed, 1775 insertions(+), 24 deletions(-) create mode 100644 Documentation/ABI/testing/sysfs-firmware-kho create mode 100644 Documentation/ABI/testing/sysfs-kernel-kho create mode 100644 Documentation/kho/bindings/memblock/reserve_mem.yaml create mode 100644 Documentation/kho/bindings/memblock/reserve_mem_map.yaml create mode 100644 Documentation/kho/concepts.rst create mode 100644 Documentation/kho/index.rst create mode 100644 Documentation/kho/usage.rst create mode 100644 include/linux/kexec_handover.h create mode 100644 kernel/kexec_handover.c
base-commit: 2014c95afecee3e76ca4a56956a936e23283f05b
Signature · 1 line
2.47.2
Mike Rapoport
maintainerPATCH v4 1/14 mm/mm_init: rename init_reserved_page to init_deferred_page
From: "Mike Rapoport (Microsoft)" <[email protected]>
When CONFIG_DEFERRED_STRUCT_PAGE_INIT is enabled, init_reserved_page() function performs initialization of a struct page that would have been deferred normally.
Rename it to init_deferred_page() to better reflect what the function does.
--- mm/mm_init.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-)
mm/mm_init.c +3 -3
@@ -705,7 +705,7 @@ defer_init(int nid, unsigned long pfn, unsigned long end_pfn)return false; } -static void __meminit init_reserved_page (unsigned long pfn,int nid) +static void __meminit init_deferred_page (unsigned long pfn,int nid) {pg_data_t *pgdat;int zid;
@@ -739,7 +739,7 @@ static inline bool defer_init(int nid, unsigned long pfn, unsigned long end_pfn) return false; } -static inline void init_reserved_page (unsigned long pfn,int nid) +static inline void init_deferred_page (unsigned long pfn,int nid) { }#endif /* CONFIG_DEFERRED_STRUCT_PAGE_INIT */
@@ -760,7 +760,7 @@ void __meminit reserve_bootmem_region(phys_addr_t start,if (pfn_valid (start_pfn)) {struct page *page =pfn_to_page (start_pfn); -init_reserved_page (start_pfn, nid); +init_deferred_page (start_pfn, nid);/* * no need for atomic set_bit because the struct
Signature · 1 line
2.47.2
Re: [PATCH v4 01/14] mm/mm_init: rename init_reserved_page to init_deferred_page
Quoted 7 lines · Mike Rapoport
From: "Mike Rapoport (Microsoft)" <[email protected]>
When
CONFIG_DEFERRED_STRUCT_PAGE_INITis enabled,init_reserved_page()function performs initialization of a struct page that would have been deferred normally.Rename it to
init_deferred_page()to better reflect what the function does.
Would it be confused with deferred_init_pages()?
And it still calls __init_reserved_page_zone(), even we __SetPageReserved() after it. Current logic looks not clear.
Signature · 2 lines
Wei Yang Help you, Help me
Mike Rapoport
maintainerRe: [PATCH v4 01/14] mm/mm_init: rename init_reserved_page to init_deferred_page
Hi,
Quoted 10 lines · Wei Yang
Quoted 7 lines · Mike Rapoport
From: "Mike Rapoport (Microsoft)" <[email protected]>
When
CONFIG_DEFERRED_STRUCT_PAGE_INITis enabled,init_reserved_page()function performs initialization of a struct page that would have been deferred normally.Rename it to
init_deferred_page()to better reflect what the function does.Would it be confused with
deferred_init_pages()?
Why? It initializes a single page, deferred_init_pages() initializes many.
Quoted 2 lines
And it still calls
__init_reserved_page_zone(), even we__SetPageReserved()after it. Current logic looks not clear.
There's no __init_reserved_page_zone(). Currently init_reserved_page() detects the zone of the page and calls __init_single_page(), so essentially it initializes one struct page.
And we __SetPageReserved() in reserve_bootmem_region() after call to init_reseved_page() because pages there are indeed reserved.
Quoted 3 lines
Signature · 2 lines
Wei Yang Help you, Help me
Signature · 2 lines
Sincerely yours, Mike.
Re: [PATCH v4 01/14] mm/mm_init: rename init_reserved_page to init_deferred_page
Quoted 10 lines · Mike Rapoport
Hi,
Quoted 7 lines · Wei Yang
Quoted 6 lines · Mike Rapoport
From: "Mike Rapoport (Microsoft)" <[email protected]>
When
CONFIG_DEFERRED_STRUCT_PAGE_INITis enabled,init_reserved_page()function performs initialization of a struct page that would have been deferred normally.
Quoted 6 lines
Quoted 3 lines
Quoted 1 line
Rename it to
init_deferred_page()to better reflect what the function does.Would it be confused with
deferred_init_pages()?Why? It initializes a single page,
deferred_init_pages()initializes many.
See below.
Quoted 10 lines
Quoted 2 lines
And it still calls
__init_reserved_page_zone(), even we__SetPageReserved()after it. Current logic looks not clear.There's no
__init_reserved_page_zone(). Currentlyinit_reserved_page()detects the zone of the page and calls__init_single_page(), so essentially it initializes one struct page.And we
__SetPageReserved()inreserve_bootmem_region()after call toinit_reseved_page()because pages there are indeed reserved.
Hmm... I am not sure we are looking at the same code. I take a look at current mm-unstable, this patch set is not included. So I am looking at previous version with this last commit:
8bf30f9d23eb 2025-02-06 Documentation: KHO: add memblock bindings
Here is what I see for init_deferred_page()'s definition:
init_deferred_page()
__init_deferred_page()
__init_reserved_page_zone() <--- I do see this function, it is removed?
__init_single_page()
What I want to say is __init_deferred_page() calls __init_reserved_page_zone(). This sounds imply a deferred page is always reserved page. But we know it is not. deferred_init_pages() initialize the pages are not reserved one. Or we want to have this context in __init_deferred_page()?
Quoted 3 lines
Signature · 2 lines
Sincerely yours, Mike.
Signature · 2 lines
Wei Yang Help you, Help me
Mike Rapoport
maintainerRe: [PATCH v4 01/14] mm/mm_init: rename init_reserved_page to init_deferred_page
Quoted 27 lines · Wei Yang
Quoted 10 lines · Mike Rapoport
Hi,
Quoted 7 lines · Wei Yang
Quoted 6 lines · Mike Rapoport
From: "Mike Rapoport (Microsoft)" <[email protected]>
When
CONFIG_DEFERRED_STRUCT_PAGE_INITis enabled,init_reserved_page()function performs initialization of a struct page that would have been deferred normally.Quoted 6 lines
Quoted 3 lines
Quoted 1 line
Rename it to
init_deferred_page()to better reflect what the function does.Would it be confused with
deferred_init_pages()?Why? It initializes a single page,
deferred_init_pages()initializes many.See below.
Quoted 7 lines
Quoted 2 lines
And it still calls
__init_reserved_page_zone(), even we__SetPageReserved()after it. Current logic looks not clear.There's no
__init_reserved_page_zone(). Currentlyinit_reserved_page()detects the zone of the page and calls__init_single_page(), so essentially it initializes one struct page.
Quoted 6 lines
Quoted 3 lines
And we
__SetPageReserved()inreserve_bootmem_region()after call toinit_reseved_page()because pages there are indeed reserved.Hmm... I am not sure we are looking at the same code. I take a look at current mm-unstable, this patch set is not included.
I was looking at Linus tree, it was not there yet :)
Quoted 11 lines
So I am looking at previous version with this last commit:
8bf30f9d23eb2025-02-06 Documentation: KHO: add memblock bindingsHere is what I see for init_deferred_page()'s definition:
init_deferred_page()
__init_deferred_page()
__init_reserved_page_zone()<--- I do see this function, it is removed?
__init_single_page()
Quoted 5 lines
What I want to say is
__init_deferred_page()calls__init_reserved_page_zone(). This sounds imply a deferred page is always reserved page. But we know it is not.deferred_init_pages()initialize the pages are not reserved one. Or we want to have this context in__init_deferred_page()?
If the commit that introduced __init_reserved_page_zone goes in before KHO, I'll just rename both functions, there is nothing about reserved pages there.
Quoted 7 lines
Quoted 3 lines
Signature · 2 lines
Sincerely yours, Mike.Signature · 2 lines
Wei Yang Help you, Help me
Signature · 2 lines
Sincerely yours, Mike.
Mike Rapoport
maintainerRe: [PATCH v4 01/14] mm/mm_init: rename init_reserved_page to init_deferred_page
In reply to Wei YangQuoted 27 lines · Wei Yang
Quoted 10 lines · Mike Rapoport
Hi,
Quoted 7 lines · Wei Yang
Quoted 6 lines · Mike Rapoport
From: "Mike Rapoport (Microsoft)" <[email protected]>
When
CONFIG_DEFERRED_STRUCT_PAGE_INITis enabled,init_reserved_page()function performs initialization of a struct page that would have been deferred normally.Quoted 6 lines
Quoted 3 lines
Quoted 1 line
Rename it to
init_deferred_page()to better reflect what the function does.Would it be confused with
deferred_init_pages()?Why? It initializes a single page,
deferred_init_pages()initializes many.See below.
Quoted 7 lines
Quoted 2 lines
And it still calls
__init_reserved_page_zone(), even we__SetPageReserved()after it. Current logic looks not clear.There's no
__init_reserved_page_zone(). Currentlyinit_reserved_page()detects the zone of the page and calls__init_single_page(), so essentially it initializes one struct page.
Quoted 5 lines
Quoted 3 lines
And we
__SetPageReserved()inreserve_bootmem_region()after call toinit_reseved_page()because pages there are indeed reserved.Hmm... I am not sure we are looking at the same code.
By "currently" I meant the Linus tree.
Quoted 13 lines
I take a look at current
mm-unstable, this patch set is not included. So I am looking at previous version with this last commit:
8bf30f9d23eb2025-02-06 Documentation: KHO: add memblock bindingsHere is what I see for init_deferred_page()'s definition:
init_deferred_page()
__init_deferred_page()
__init_reserved_page_zone()<--- I do see this function, it is removed?
__init_single_page()
Quoted 5 lines
What I want to say is
__init_deferred_page()calls__init_reserved_page_zone(). This sounds imply a deferred page is always reserved page. But we know it is not.deferred_init_pages()initialize the pages are not reserved one. Or we want to have this context in__init_deferred_page()?
In mm-unstable the code is slightly reorganized, but in the end it still initializes a deferred page. It's just only called for reserved page, but the initialization itself does not presume that the page is reserved.
So both functions are misnamed here, __init_reserved_page_zone() and init_reserved_page().
Quoted 3 lines
Signature · 2 lines
Wei Yang Help you, Help me
Signature · 2 lines
Sincerely yours, Mike.
Mike Rapoport
maintainerPATCH v4 2/14 memblock: add MEMBLOCK_RSRV_KERN flag
In reply to Mike RapoportFrom: "Mike Rapoport (Microsoft)" <[email protected]>
to denote areas that were reserved for kernel use either directly with memblock_reserve_kern() or via memblock allocations.
--- include/linux/memblock.h | 16 +++++++++++++++- mm/memblock.c | 32 ++++++++++++++++++++++++-------- 2 files changed, 39 insertions(+), 9 deletions(-)
include/linux/memblock.h +15 -1
@@ -50,6 +50,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 +117,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
@@ -477,6 +490,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 (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 );
mm/memblock.c +24 -8
@@ -491,7 +491,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;
@@ -641,7 +641,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 )
@@ -901,14 +901,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
@@ -1459,14 +1460,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; }
@@ -1751,6 +1752,20 @@ phys_addr_t __init_memblock memblock_reserved_size(void)return memblock.reserved.total_size; } +phys_addr_t __init_memblock memblock_reserved_kern_size(int nid) +{ +struct memblock_region *r; +phys_addr_t total =0 ; + +for_each_reserved_mem_region (r) { +if (nid ==memblock_get_region_node (r) || !numa_valid_node (nid)) +if (r->flags &MEMBLOCK_RSRV_KERN ) + total += r->size; + } + +return total; +} +/** * memblock_estimated_nr_free_pages - return estimated number of free pages * from memblock point of view
@@ -2397,6 +2412,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)
Signature · 1 line
2.47.2
Re: [PATCH v4 02/14] memblock: add MEMBLOCK_RSRV_KERN flag
Quoted 16 lines · Mike Rapoport
From: "Mike Rapoport (Microsoft)" <[email protected]>
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) <[email protected]>---
include/linux/memblock.h| 16 +++++++++++++++-mm/memblock.c| 32 ++++++++++++++++++++++++--------2 files changed, 39 insertions(+), 9 deletions(-)include/linux/memblock.h +0
-0
include/linux/memblock.h enum memblock_flags { · line 53
Quoted 4 lines
include/linux/memblock.h +1
-0@@ -50,6 +50,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 */
Above memblock_flags, there are comments on explaining those flags.
Seems we miss it for MEMBLOCK_RSRV_KERN.
Quoted 4 lines
include/linux/memblock.h +0
-0};/**
include/linux/memblock.h int memblock_add_node(phys_addr_t base, phys_addr_t size, int nid, · line 125
Quoted 10 lines
include/linux/memblock.h +6
-1@@ -116,7 +117,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 );
^ MEMBLOCK_NONE ?
Quoted 64 lines
include/linux/memblock.h +8
-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 @@ -477,6 +490,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 (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 );mm/memblock.c +7
-6@@ -491,7 +491,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;@@ -641,7 +641,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 )@@ -901,14 +901,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 @@ -1459,14 +1460,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);
mm/memblock.c phys_addr_t __init memblock_alloc_range_nid(phys_addr_t size, · line 1463
Quoted 2 lines
mm/memblock.c +1
-1-if (found && !memblock_reserve (found, size)) +if (found && !__memblock_reserve (found, size, nid,MEMBLOCK_RSRV_KERN ))
Maybe we could use memblock_reserve_kern() directly. If my understanding is correct, the reserved region's nid is not used.
BTW, one question here. How we handle concurrent memblock allocation? If two threads find the same available range and do the reservation, it seems to be a problem to me. Or I missed something?
Quoted 43 lines
goto done; if (numa_valid_node(nid) && !exact_nid) { found = memblock_find_in_range_node(size, align, start, end, NUMA_NO_NODE, flags);mm/memblock.c +1
-1-if (found && !memblock_reserve (found, size)) +if (found && !memblock_reserve_kern (found, size))goto done; }mm/memblock.c +15
-0@@ -1751,6 +1752,20 @@ phys_addr_t __init_memblock memblock_reserved_size(void)return memblock.reserved.total_size; } +phys_addr_t __init_memblock memblock_reserved_kern_size(int nid) +{ +struct memblock_region *r; +phys_addr_t total =0 ; + +for_each_reserved_mem_region (r) { +if (nid ==memblock_get_region_node (r) || !numa_valid_node (nid)) +if (r->flags &MEMBLOCK_RSRV_KERN ) + total += r->size; + } + +return total; +} +/** * memblock_estimated_nr_free_pages - return estimated number of free pages * from memblock point of view @@ -2397,6 +2412,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)Signature · 1 line
2.47.2
Signature · 2 lines
Wei Yang Help you, Help me
Mike Rapoport
maintainerRe: [PATCH v4 02/14] memblock: add MEMBLOCK_RSRV_KERN flag
Hi,
Quoted 24 lines · Wei Yang
Quoted 16 lines · Mike Rapoport
From: "Mike Rapoport (Microsoft)" <[email protected]>
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) <[email protected]>---
include/linux/memblock.h| 16 +++++++++++++++-mm/memblock.c| 32 ++++++++++++++++++++++++--------2 files changed, 39 insertions(+), 9 deletions(-)include/linux/memblock.h +0
-0include/linux/memblock.h enum memblock_flags { · line 53
Quoted 4 lines
include/linux/memblock.h +1
-0@@ -50,6 +50,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 */Above
memblock_flags, there are comments on explaining those flags.
Quoted 1 line
Seems we miss it for
MEMBLOCK_RSRV_KERN.
Right, thanks!
Quoted 11 lines
include/linux/memblock.h
Quoted 8 lines
include/linux/memblock.h +0
-0#ifdef CONFIG_HAVE_MEMBLOCK_PHYS_MAP @@ -1459,14 +1460,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);include/linux/memblock.h +1
-1-if (found && !memblock_reserve (found, size)) +if (found && !__memblock_reserve (found, size, nid,MEMBLOCK_RSRV_KERN ))Maybe we could use
memblock_reserve_kern()directly. If my understanding is correct, the reserved region's nid is not used.
We use nid of reserved regions in reserve_bootmem_region() (commit 61167ad5fecd ("mm: pass nid to reserve_bootmem_region()")) but KHO needs to know the distribution of reserved memory among the nodes before memmap_init_reserved_pages().
Quoted 3 lines
BTW, one question here. How we handle concurrent memblock allocation? If two threads find the same available range and do the reservation, it seems to be a problem to me. Or I missed something?
memblock allocations end before smp_init(), there is no possible concurrency.
Quoted 3 lines
Signature · 2 lines
Wei Yang Help you, Help me
Signature · 2 lines
Sincerely yours, Mike.
Re: [PATCH v4 02/14] memblock: add MEMBLOCK_RSRV_KERN flag
Quoted 43 lines · Mike Rapoport
Hi,
Quoted 24 lines · Wei Yang
Quoted 16 lines · Mike Rapoport
From: "Mike Rapoport (Microsoft)" <[email protected]>
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) <[email protected]>---
include/linux/memblock.h| 16 +++++++++++++++-mm/memblock.c| 32 ++++++++++++++++++++++++--------2 files changed, 39 insertions(+), 9 deletions(-)include/linux/memblock.h +0
-0include/linux/memblock.h enum memblock_flags { · line 53
Quoted 4 lines
include/linux/memblock.h +1
-0@@ -50,6 +50,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 */Above
memblock_flags, there are comments on explaining those flags.Quoted 1 line
Seems we miss it for
MEMBLOCK_RSRV_KERN.Right, thanks!
Quoted 11 lines
include/linux/memblock.h
Quoted 8 lines
include/linux/memblock.h +0
-0#ifdef CONFIG_HAVE_MEMBLOCK_PHYS_MAP @@ -1459,14 +1460,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);include/linux/memblock.h +1
-1-if (found && !memblock_reserve (found, size)) +if (found && !__memblock_reserve (found, size, nid,MEMBLOCK_RSRV_KERN ))Maybe we could use
memblock_reserve_kern()directly. If my understanding is correct, the reserved region's nid is not used.
Quoted 5 lines
We use nid of reserved regions in
reserve_bootmem_region()(commit61167ad5fecd("mm: pass nid toreserve_bootmem_region()")) but KHO needs to know the distribution of reserved memory among the nodes beforememmap_init_reserved_pages().
I took another look into this commit. There maybe a very corner case in which will leave a reserved region with no nid set.
memmap_init_reserved_pages()
for_each_mem_region() {
...
memblock_set_node(start, end, &memblock.reserved, nid);
}We leverage the iteration here to set nid to all regions in memblock.reserved. But memblock_set_node() may call memblock_double_array() to expand the array, which may get a range before current start. So we would miss to set the correct nid to the new reserved region.
I have tried to create a case in memblock test. This would happen when there are 126 memblock.reserved regions. And the last region is across the last two node.
One way to fix this is compare type->max in memblock_set_node(). Then check this return value in memmap_init_reserved_pages(). If we found the size changes, repeat the iteration.
But this is a very trivial one, not sure it worth fix.
Signature · 2 lines
Wei Yang Help you, Help me
Re: [PATCH v4 02/14] memblock: add MEMBLOCK_RSRV_KERN flag
Quoted 68 lines · Wei Yang
Quoted 43 lines · Mike Rapoport
Hi,
Quoted 24 lines · Wei Yang
Quoted 16 lines · Mike Rapoport
From: "Mike Rapoport (Microsoft)" <[email protected]>
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) <[email protected]>---
include/linux/memblock.h| 16 +++++++++++++++-mm/memblock.c| 32 ++++++++++++++++++++++++--------2 files changed, 39 insertions(+), 9 deletions(-)include/linux/memblock.h +0
-0include/linux/memblock.h enum memblock_flags { · line 53
Quoted 4 lines
include/linux/memblock.h +1
-0@@ -50,6 +50,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 */Above
memblock_flags, there are comments on explaining those flags.Quoted 1 line
Seems we miss it for
MEMBLOCK_RSRV_KERN.Right, thanks!
Quoted 11 lines
include/linux/memblock.h
Quoted 8 lines
include/linux/memblock.h +0
-0#ifdef CONFIG_HAVE_MEMBLOCK_PHYS_MAP @@ -1459,14 +1460,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);include/linux/memblock.h +1
-1-if (found && !memblock_reserve (found, size)) +if (found && !__memblock_reserve (found, size, nid,MEMBLOCK_RSRV_KERN ))Maybe we could use
memblock_reserve_kern()directly. If my understanding is correct, the reserved region's nid is not used.Quoted 5 lines
We use nid of reserved regions in
reserve_bootmem_region()(commit61167ad5fecd("mm: pass nid toreserve_bootmem_region()")) but KHO needs to know the distribution of reserved memory among the nodes beforememmap_init_reserved_pages().I took another look into this commit. There maybe a very corner case in which will leave a reserved region with no nid set.
memmap_init_reserved_pages()for_each_mem_region() { ... memblock_set_node(start, end, &memblock.reserved, nid);}
We leverage the iteration here to set nid to all regions in memblock.reserved. But
memblock_set_node()may callmemblock_double_array()to expand the array, which may get a range before current start. So we would miss to set the correct nid to the new reserved region.I have tried to create a case in memblock test. This would happen when there are 126 memblock.reserved regions. And the last region is across the last two node.
Quoted 6 lines
One way to fix this is compare
type->maxinmemblock_set_node(). Then check this return value inmemmap_init_reserved_pages(). If we found the size changes, repeat the iteration.But this is a very trivial one, not sure it worth fix.
Hi, Mike
I have done a user space test which shows we may have a chance to leave a region with non-nid set.
Not sure you are ok with my approach of fixing.
Signature · 2 lines
Wei Yang Help you, Help me
Mike Rapoport
maintainerRe: [PATCH v4 02/14] memblock: add MEMBLOCK_RSRV_KERN flag
Hi Wei,
Quoted 76 lines · Wei Yang
Quoted 68 lines · Wei Yang
Quoted 43 lines · Mike Rapoport
Hi,
Quoted 24 lines · Wei Yang
Quoted 16 lines · Mike Rapoport
From: "Mike Rapoport (Microsoft)" <[email protected]>
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) <[email protected]>---
include/linux/memblock.h| 16 +++++++++++++++-mm/memblock.c| 32 ++++++++++++++++++++++++--------2 files changed, 39 insertions(+), 9 deletions(-)include/linux/memblock.h +0
-0include/linux/memblock.h enum memblock_flags { · line 53
Quoted 4 lines
include/linux/memblock.h +1
-0@@ -50,6 +50,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 */Above
memblock_flags, there are comments on explaining those flags.Quoted 1 line
Seems we miss it for
MEMBLOCK_RSRV_KERN.Right, thanks!
Quoted 11 lines
include/linux/memblock.h
Quoted 8 lines
include/linux/memblock.h +0
-0#ifdef CONFIG_HAVE_MEMBLOCK_PHYS_MAP @@ -1459,14 +1460,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);include/linux/memblock.h +1
-1-if (found && !memblock_reserve (found, size)) +if (found && !__memblock_reserve (found, size, nid,MEMBLOCK_RSRV_KERN ))Maybe we could use
memblock_reserve_kern()directly. If my understanding is correct, the reserved region's nid is not used.Quoted 5 lines
We use nid of reserved regions in
reserve_bootmem_region()(commit61167ad5fecd("mm: pass nid toreserve_bootmem_region()")) but KHO needs to know the distribution of reserved memory among the nodes beforememmap_init_reserved_pages().I took another look into this commit. There maybe a very corner case in which will leave a reserved region with no nid set.
memmap_init_reserved_pages()for_each_mem_region() { ... memblock_set_node(start, end, &memblock.reserved, nid);}
We leverage the iteration here to set nid to all regions in memblock.reserved. But
memblock_set_node()may callmemblock_double_array()to expand the array, which may get a range before current start. So we would miss to set the correct nid to the new reserved region.I have tried to create a case in memblock test. This would happen when there are 126 memblock.reserved regions. And the last region is across the last two node.
Quoted 6 lines
One way to fix this is compare
type->maxinmemblock_set_node(). Then check this return value inmemmap_init_reserved_pages(). If we found the size changes, repeat the iteration.But this is a very trivial one, not sure it worth fix.
Quoted 6 lines
Hi, Mike
I have done a user space test which shows we may have a chance to leave a region with non-nid set.
Not sure you are ok with my approach of fixing.
Wouldn't it be better to check for a change in reserved.max in memmap_init_reserved_pages()?
Quoted 3 lines
Signature · 2 lines
Wei Yang Help you, Help me
Signature · 2 lines
Sincerely yours, Mike.
Re: [PATCH v4 02/14] memblock: add MEMBLOCK_RSRV_KERN flag
Quoted 81 lines · Mike Rapoport
Hi Wei,
Quoted 78 lines · Wei Yang
Quoted 68 lines · Wei Yang
Quoted 43 lines · Mike Rapoport
Hi,
Quoted 24 lines · Wei Yang
Quoted 16 lines · Mike Rapoport
From: "Mike Rapoport (Microsoft)" <[email protected]>
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) <[email protected]>---
include/linux/memblock.h| 16 +++++++++++++++-mm/memblock.c| 32 ++++++++++++++++++++++++--------2 files changed, 39 insertions(+), 9 deletions(-)include/linux/memblock.h +0
-0include/linux/memblock.h enum memblock_flags { · line 53
Quoted 4 lines
include/linux/memblock.h +1
-0@@ -50,6 +50,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 */Above
memblock_flags, there are comments on explaining those flags.Quoted 1 line
Seems we miss it for
MEMBLOCK_RSRV_KERN.Right, thanks!
Quoted 11 lines
include/linux/memblock.h
Quoted 8 lines
include/linux/memblock.h +0
-0#ifdef CONFIG_HAVE_MEMBLOCK_PHYS_MAP @@ -1459,14 +1460,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);include/linux/memblock.h +1
-1-if (found && !memblock_reserve (found, size)) +if (found && !__memblock_reserve (found, size, nid,MEMBLOCK_RSRV_KERN ))Maybe we could use
memblock_reserve_kern()directly. If my understanding is correct, the reserved region's nid is not used.Quoted 5 lines
We use nid of reserved regions in
reserve_bootmem_region()(commit61167ad5fecd("mm: pass nid toreserve_bootmem_region()")) but KHO needs to know the distribution of reserved memory among the nodes beforememmap_init_reserved_pages().I took another look into this commit. There maybe a very corner case in which will leave a reserved region with no nid set.
memmap_init_reserved_pages()for_each_mem_region() { ... memblock_set_node(start, end, &memblock.reserved, nid);}
We leverage the iteration here to set nid to all regions in memblock.reserved. But
memblock_set_node()may callmemblock_double_array()to expand the array, which may get a range before current start. So we would miss to set the correct nid to the new reserved region.I have tried to create a case in memblock test. This would happen when there are 126 memblock.reserved regions. And the last region is across the last two node.
Quoted 6 lines
One way to fix this is compare
type->maxinmemblock_set_node(). Then check this return value inmemmap_init_reserved_pages(). If we found the size changes, repeat the iteration.But this is a very trivial one, not sure it worth fix.
Hi, Mike
Quoted 8 lines
Quoted 4 lines
I have done a user space test which shows we may have a chance to leave a region with non-nid set.
Not sure you are ok with my approach of fixing.
Wouldn't it be better to check for a change in reserved.max in
memmap_init_reserved_pages()?
Sounds better.
Previously I thought we need to hide detail from user, but actually it is already in memblock.c :-)
If you agree, I would like to prepare a fix.
Quoted 7 lines
Quoted 3 lines
Signature · 2 lines
Wei Yang Help you, Help meSignature · 2 lines
Sincerely yours, Mike.
Signature · 2 lines
Wei Yang Help you, Help me
Mike Rapoport
maintainerRe: [PATCH v4 02/14] memblock: add MEMBLOCK_RSRV_KERN flag
Quoted 39 lines · Wei Yang
Quoted 29 lines · Mike Rapoport
Quoted 29 lines
Quoted 20 lines
Quoted 1 line
I took another look into this commit. There maybe a very corner case in which will leave a reserved region with no nid set.
memmap_init_reserved_pages()for_each_mem_region() { ... memblock_set_node(start, end, &memblock.reserved, nid);}
We leverage the iteration here to set nid to all regions in memblock.reserved. But
memblock_set_node()may callmemblock_double_array()to expand the array, which may get a range before current start. So we would miss to set the correct nid to the new reserved region.I have tried to create a case in memblock test. This would happen when there are 126 memblock.reserved regions. And the last region is across the last two node.
Quoted 6 lines
One way to fix this is compare
type->maxinmemblock_set_node(). Then check this return value inmemmap_init_reserved_pages(). If we found the size changes, repeat the iteration.But this is a very trivial one, not sure it worth fix.
Hi, Mike
Quoted 8 lines
Quoted 4 lines
I have done a user space test which shows we may have a chance to leave a region with non-nid set.
Not sure you are ok with my approach of fixing.
Wouldn't it be better to check for a change in reserved.max in
memmap_init_reserved_pages()?
Quoted 6 lines
Sounds better.
Previously I thought we need to hide detail from user, but actually it is already in
memblock.c:-)If you agree, I would like to prepare a fix.
Sure :)
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Signature · 2 lines
Wei Yang Help you, Help me
Signature · 2 lines
Sincerely yours, Mike.
Re: [PATCH v4 02/14] memblock: add MEMBLOCK_RSRV_KERN flag
In reply to Mike RapoportQuoted 48 lines · Mike Rapoport
Hi,
Quoted 24 lines · Wei Yang
Quoted 16 lines · Mike Rapoport
From: "Mike Rapoport (Microsoft)" <[email protected]>
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) <[email protected]>---
include/linux/memblock.h| 16 +++++++++++++++-mm/memblock.c| 32 ++++++++++++++++++++++++--------2 files changed, 39 insertions(+), 9 deletions(-)include/linux/memblock.h +0
-0include/linux/memblock.h enum memblock_flags { · line 53
Quoted 4 lines
include/linux/memblock.h +1
-0@@ -50,6 +50,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 */Above
memblock_flags, there are comments on explaining those flags.Quoted 1 line
Seems we miss it for
MEMBLOCK_RSRV_KERN.Right, thanks!
Quoted 11 lines
include/linux/memblock.h
Quoted 8 lines
include/linux/memblock.h +0
-0#ifdef CONFIG_HAVE_MEMBLOCK_PHYS_MAP @@ -1459,14 +1460,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);include/linux/memblock.h +1
-1-if (found && !memblock_reserve (found, size)) +if (found && !__memblock_reserve (found, size, nid,MEMBLOCK_RSRV_KERN ))Maybe we could use
memblock_reserve_kern()directly. If my understanding is correct, the reserved region's nid is not used.We use nid of reserved regions in
reserve_bootmem_region()(commit61167ad5fecd("mm: pass nid toreserve_bootmem_region()")) but KHO needs to know the distribution of reserved memory among the nodes beforememmap_init_reserved_pages().
Quoted 6 lines
Quoted 3 lines
BTW, one question here. How we handle concurrent memblock allocation? If two threads find the same available range and do the reservation, it seems to be a problem to me. Or I missed something?
memblock allocations end before
smp_init(), there is no possible concurrency.
Thanks, I still have one question here.
Below is a simplified call flow.
mm_core_init()
mem_init()
memblock_free_all()
free_low_memory_core_early()
memmap_init_reserved_pages()
memblock_set_node(..., memblock.reserved, ) --- (1)
__free_memory_core()
kmem_cache_init()
slab_state = UP; --- (2)And memblock_allloc_range_nid() is not supposed to be called after slab_is_available(). Even someone do dose it, it will get memory from slab instead of reserve region in memblock.
From the above call flow and background, there are three cases when memblock_alloc_range_nid() would be called:
* If it is called before (1), memblock.reserved's nid would be adjusted correctly.
* If it is called after (2), we don't touch memblock.reserved.
* If it happens between (1) and (2), it looks would break the consistency of nid information in memblock.reserved. Because when we use memblock_reserve_kern(), NUMA_NO_NODE would be stored in region.
So my question is if the third case happens, would it introduce a bug? If it won't happen, seems we don't need to specify the nid here?
Signature · 2 lines
Wei Yang Help you, Help me
Mike Rapoport
maintainerRe: [PATCH v4 02/14] memblock: add MEMBLOCK_RSRV_KERN flag
Quoted 77 lines · Wei Yang
Quoted 48 lines · Mike Rapoport
Hi,
Quoted 24 lines · Wei Yang
Quoted 16 lines · Mike Rapoport
From: "Mike Rapoport (Microsoft)" <[email protected]>
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) <[email protected]>---
include/linux/memblock.h| 16 +++++++++++++++-mm/memblock.c| 32 ++++++++++++++++++++++++--------2 files changed, 39 insertions(+), 9 deletions(-)include/linux/memblock.h +0
-0include/linux/memblock.h enum memblock_flags { · line 53
Quoted 4 lines
include/linux/memblock.h +1
-0@@ -50,6 +50,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 */Above
memblock_flags, there are comments on explaining those flags.Quoted 1 line
Seems we miss it for
MEMBLOCK_RSRV_KERN.Right, thanks!
Quoted 11 lines
include/linux/memblock.h
Quoted 8 lines
include/linux/memblock.h +0
-0#ifdef CONFIG_HAVE_MEMBLOCK_PHYS_MAP @@ -1459,14 +1460,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);include/linux/memblock.h +1
-1-if (found && !memblock_reserve (found, size)) +if (found && !__memblock_reserve (found, size, nid,MEMBLOCK_RSRV_KERN ))Maybe we could use
memblock_reserve_kern()directly. If my understanding is correct, the reserved region's nid is not used.We use nid of reserved regions in
reserve_bootmem_region()(commit61167ad5fecd("mm: pass nid toreserve_bootmem_region()")) but KHO needs to know the distribution of reserved memory among the nodes beforememmap_init_reserved_pages().Quoted 6 lines
Quoted 3 lines
BTW, one question here. How we handle concurrent memblock allocation? If two threads find the same available range and do the reservation, it seems to be a problem to me. Or I missed something?
memblock allocations end before
smp_init(), there is no possible concurrency.Thanks, I still have one question here.
Below is a simplified call flow.
mm_core_init() mem_init() memblock_free_all() free_low_memory_core_early() memmap_init_reserved_pages() memblock_set_node(..., memblock.reserved, ) --- (1) __free_memory_core() kmem_cache_init() slab_state = UP; --- (2)And
memblock_allloc_range_nid()is not supposed to be called afterslab_is_available(). Even someone do dose it, it will get memory from slab instead of reserve region in memblock.From the above call flow and background, there are three cases when
memblock_alloc_range_nid()would be called:
Quoted 8 lines
* If it is called before (1), memblock.reserved's nid would be adjusted correctly.
* If it is called after (2), we don't touch memblock.reserved.
* If it happens between (1) and (2), it looks would break the consistency of nid information in memblock.reserved. Because when we usememblock_reserve_kern(),NUMA_NO_NODEwould be stored in region.So my question is if the third case happens, would it introduce a bug? If it won't happen, seems we don't need to specify the nid here?
We don't really care about proper assignment of nodes between (1) and (2) from one side and the third case does not happen on the other side. Nothing should call membloc_alloc() after memblock_free_all().
But it's easy to make the window between (1) and (2) disappear by replacing checks for slab_is_available() in memblock with a variable local to memblock.
Quoted 3 lines
Signature · 2 lines
Wei Yang Help you, Help me
Signature · 2 lines
Sincerely yours, Mike.
Re: [PATCH v4 02/14] memblock: add MEMBLOCK_RSRV_KERN flag
Quoted 84 lines · Mike Rapoport
Quoted 83 lines · Wei Yang
Quoted 48 lines · Mike Rapoport
Hi,
Quoted 24 lines · Wei Yang
Quoted 16 lines · Mike Rapoport
From: "Mike Rapoport (Microsoft)" <[email protected]>
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) <[email protected]>---
include/linux/memblock.h| 16 +++++++++++++++-mm/memblock.c| 32 ++++++++++++++++++++++++--------2 files changed, 39 insertions(+), 9 deletions(-)include/linux/memblock.h +0
-0include/linux/memblock.h enum memblock_flags { · line 53
Quoted 4 lines
include/linux/memblock.h +1
-0@@ -50,6 +50,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 */Above
memblock_flags, there are comments on explaining those flags.Quoted 1 line
Seems we miss it for
MEMBLOCK_RSRV_KERN.Right, thanks!
Quoted 11 lines
include/linux/memblock.h
Quoted 8 lines
include/linux/memblock.h +0
-0#ifdef CONFIG_HAVE_MEMBLOCK_PHYS_MAP @@ -1459,14 +1460,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);include/linux/memblock.h +1
-1-if (found && !memblock_reserve (found, size)) +if (found && !__memblock_reserve (found, size, nid,MEMBLOCK_RSRV_KERN ))Maybe we could use
memblock_reserve_kern()directly. If my understanding is correct, the reserved region's nid is not used.We use nid of reserved regions in
reserve_bootmem_region()(commit61167ad5fecd("mm: pass nid toreserve_bootmem_region()")) but KHO needs to know the distribution of reserved memory among the nodes beforememmap_init_reserved_pages().Quoted 6 lines
Quoted 3 lines
BTW, one question here. How we handle concurrent memblock allocation? If two threads find the same available range and do the reservation, it seems to be a problem to me. Or I missed something?
memblock allocations end before
smp_init(), there is no possible concurrency.Thanks, I still have one question here.
Below is a simplified call flow.
mm_core_init() mem_init() memblock_free_all() free_low_memory_core_early() memmap_init_reserved_pages() memblock_set_node(..., memblock.reserved, ) --- (1) __free_memory_core() kmem_cache_init() slab_state = UP; --- (2)And
memblock_allloc_range_nid()is not supposed to be called afterslab_is_available(). Even someone do dose it, it will get memory from slab instead of reserve region in memblock.From the above call flow and background, there are three cases when
memblock_alloc_range_nid()would be called:* If it is called before (1), memblock.reserved's nid would be adjusted correctly.
* If it is called after (2), we don't touch memblock.reserved.
* If it happens between (1) and (2), it looks would break the consistency of nid information in memblock.reserved. Because when we usememblock_reserve_kern(),NUMA_NO_NODEwould be stored in region.
Quoted 7 lines
Quoted 2 lines
So my question is if the third case happens, would it introduce a bug? If it won't happen, seems we don't need to specify the nid here?
We don't really care about proper assignment of nodes between (1) and (2) from one side and the third case does not happen on the other side. Nothing should call
membloc_alloc()aftermemblock_free_all().
My point is if no one would call memblock_alloc() after memblock_free_all(), which set nid in memblock.reserved properly, it seems not necessary to do __memblock_reserve() with exact nid during memblock_alloc()?
As you did __memblock_reserve(found, size, nid, MEMBLOCK_RSRV_KERN) in this patch.
Quoted 11 lines
But it's easy to make the window between (1) and (2) disappear by replacing checks for
slab_is_available()in memblock with a variable local to memblock.Quoted 3 lines
Signature · 2 lines
Wei Yang Help you, Help meSignature · 2 lines
Sincerely yours, Mike.
Signature · 2 lines
Wei Yang Help you, Help me
Re: [PATCH v4 02/14] memblock: add MEMBLOCK_RSRV_KERN flag
Quoted 93 lines · Wei Yang
Quoted 84 lines · Mike Rapoport
Quoted 83 lines · Wei Yang
Quoted 48 lines · Mike Rapoport
Hi,
Quoted 24 lines · Wei Yang
Quoted 16 lines · Mike Rapoport
From: "Mike Rapoport (Microsoft)" <[email protected]>
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) <[email protected]>---
include/linux/memblock.h| 16 +++++++++++++++-mm/memblock.c| 32 ++++++++++++++++++++++++--------2 files changed, 39 insertions(+), 9 deletions(-)include/linux/memblock.h +0
-0include/linux/memblock.h enum memblock_flags { · line 53
Quoted 4 lines
include/linux/memblock.h +1
-0@@ -50,6 +50,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 */Above
memblock_flags, there are comments on explaining those flags.Quoted 1 line
Seems we miss it for
MEMBLOCK_RSRV_KERN.Right, thanks!
Quoted 11 lines
include/linux/memblock.h
Quoted 8 lines
include/linux/memblock.h +0
-0#ifdef CONFIG_HAVE_MEMBLOCK_PHYS_MAP @@ -1459,14 +1460,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);include/linux/memblock.h +1
-1-if (found && !memblock_reserve (found, size)) +if (found && !__memblock_reserve (found, size, nid,MEMBLOCK_RSRV_KERN ))Maybe we could use
memblock_reserve_kern()directly. If my understanding is correct, the reserved region's nid is not used.We use nid of reserved regions in
reserve_bootmem_region()(commit61167ad5fecd("mm: pass nid toreserve_bootmem_region()")) but KHO needs to know the distribution of reserved memory among the nodes beforememmap_init_reserved_pages().Quoted 6 lines
Quoted 3 lines
BTW, one question here. How we handle concurrent memblock allocation? If two threads find the same available range and do the reservation, it seems to be a problem to me. Or I missed something?
memblock allocations end before
smp_init(), there is no possible concurrency.Thanks, I still have one question here.
Below is a simplified call flow.
mm_core_init() mem_init() memblock_free_all() free_low_memory_core_early() memmap_init_reserved_pages() memblock_set_node(..., memblock.reserved, ) --- (1) __free_memory_core() kmem_cache_init() slab_state = UP; --- (2)And
memblock_allloc_range_nid()is not supposed to be called afterslab_is_available(). Even someone do dose it, it will get memory from slab instead of reserve region in memblock.From the above call flow and background, there are three cases when
memblock_alloc_range_nid()would be called:* If it is called before (1), memblock.reserved's nid would be adjusted correctly.
* If it is called after (2), we don't touch memblock.reserved.
* If it happens between (1) and (2), it looks would break the consistency of nid information in memblock.reserved. Because when we usememblock_reserve_kern(),NUMA_NO_NODEwould be stored in region.Quoted 7 lines
Quoted 2 lines
So my question is if the third case happens, would it introduce a bug? If it won't happen, seems we don't need to specify the nid here?
We don't really care about proper assignment of nodes between (1) and (2) from one side and the third case does not happen on the other side. Nothing should call
membloc_alloc()aftermemblock_free_all().
Quoted 7 lines
My point is if no one would call
memblock_alloc()aftermemblock_free_all(), which set nid in memblock.reserved properly, it seems not necessary to do__memblock_reserve()with exact nid duringmemblock_alloc()?As you did
__memblock_reserve(found, size, nid, MEMBLOCK_RSRV_KERN)in this patch.
Hi, Mike
Do you think my understanding is reasonable?
Signature · 2 lines
Wei Yang Help you, Help me
Mike Rapoport
maintainerRe: [PATCH v4 02/14] memblock: add MEMBLOCK_RSRV_KERN flag
Hi Wei,
Quoted 23 lines · Wei Yang
Quoted 22 lines · Wei Yang
Quoted 10 lines
Quoted 10 lines
From the above call flow and background, there are three cases when
memblock_alloc_range_nid()would be called:* If it is called before (1), memblock.reserved's nid would be adjusted correctly.
* If it is called after (2), we don't touch memblock.reserved.
* If it happens between (1) and (2), it looks would break the consistency of nid information in memblock.reserved. Because when we usememblock_reserve_kern(),NUMA_NO_NODEwould be stored in region.Quoted 7 lines
Quoted 2 lines
So my question is if the third case happens, would it introduce a bug? If it won't happen, seems we don't need to specify the nid here?
We don't really care about proper assignment of nodes between (1) and (2) from one side and the third case does not happen on the other side. Nothing should call
membloc_alloc()aftermemblock_free_all().My point is if no one would call
memblock_alloc()aftermemblock_free_all(), which set nid in memblock.reserved properly, it seems not necessary to do__memblock_reserve()with exact nid duringmemblock_alloc()?
Quoted 7 lines
Quoted 3 lines
As you did
__memblock_reserve(found, size, nid, MEMBLOCK_RSRV_KERN)in this patch.Hi, Mike
Do you think my understanding is reasonable?
Without KHO it is indeed not strictly necessary to set nid during memblock_alloc(). But since we anyway have nid parameter in memblock_alloc_range_nid() and it anyway propagates to memblock_add_range(), I think it's easier and cleaner to pass nid to __memblock_reserve() there.
And for KHO estimation of scratch size it is important to have nid assigned to the reserved areas before memblock_free_all(), at least for the allocations that request particular nid explicitly.
Quoted 3 lines
Signature · 2 lines
Wei Yang Help you, Help me
Signature · 2 lines
Sincerely yours, Mike.
Re: [PATCH v4 02/14] memblock: add MEMBLOCK_RSRV_KERN flag
Quoted 34 lines · Mike Rapoport
Hi Wei,
Quoted 23 lines · Wei Yang
Quoted 22 lines · Wei Yang
Quoted 10 lines
Quoted 10 lines
From the above call flow and background, there are three cases when
memblock_alloc_range_nid()would be called:* If it is called before (1), memblock.reserved's nid would be adjusted correctly.
* If it is called after (2), we don't touch memblock.reserved.
* If it happens between (1) and (2), it looks would break the consistency of nid information in memblock.reserved. Because when we usememblock_reserve_kern(),NUMA_NO_NODEwould be stored in region.Quoted 7 lines
Quoted 2 lines
So my question is if the third case happens, would it introduce a bug? If it won't happen, seems we don't need to specify the nid here?
We don't really care about proper assignment of nodes between (1) and (2) from one side and the third case does not happen on the other side. Nothing should call
membloc_alloc()aftermemblock_free_all().My point is if no one would call
memblock_alloc()aftermemblock_free_all(), which set nid in memblock.reserved properly, it seems not necessary to do__memblock_reserve()with exact nid duringmemblock_alloc()?Quoted 7 lines
Quoted 3 lines
As you did
__memblock_reserve(found, size, nid, MEMBLOCK_RSRV_KERN)in this patch.Hi, Mike
Do you think my understanding is reasonable?
Quoted 8 lines
Without KHO it is indeed not strictly necessary to set nid during
memblock_alloc(). But since we anyway have nid parameter inmemblock_alloc_range_nid()and it anyway propagates tomemblock_add_range(), I think it's easier and cleaner to pass nid to__memblock_reserve()there.And for KHO estimation of scratch size it is important to have nid assigned to the reserved areas before
memblock_free_all(), at least for the allocations that request particular nid explicitly.
Thanks, I see your point.
Quoted 8 lines
Quoted 4 lines
Signature · 3 lines
-- Wei Yang Help you, Help meSignature · 2 lines
Sincerely yours, Mike.
Signature · 2 lines
Wei Yang Help you, Help me
Re: [PATCH v4 02/14] memblock: add MEMBLOCK_RSRV_KERN flag
In reply to Mike RapoportHi Mike,
Quoted 17 lines · Mike Rapoport
From: "Mike Rapoport (Microsoft)" <[email protected]>
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) <[email protected]>--- include/linux/memblock.h | 16 +++++++++++++++- mm/memblock.c | 32 ++++++++++++++++++++++++-------- 2 files changed, 39 insertions(+), 9 deletions(-)include/linux/memblock.h +0
-0
include/linux/memblock.h int memblock_add_node(phys_addr_t base, phys_addr_t size, int nid, · line 125
Quoted 10 lines
include/linux/memblock.h +6
-1@@ -116,7 +117,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 );
Without this patch memblock_reserve eventually calls memblock_add_range with MAX_NUMNODES, but with this patch, memblock_reserve calls memblock_add_range with NUMA_NO_NODE. Is it intended or an accidental typo? Thanks!
Quoted 4 lines
......
include/linux/memblock.h +1
-1-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,
include/linux/memblock.h
Quoted 10 lines
include/linux/memblock.h +3
-3+ 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 );
Originally memblock_reserve calls memblock_add_range with MAX_NUMNODES, See my comments above.
Best,
Changyuan
Mike Rapoport
maintainerRe: [PATCH v4 02/14] memblock: add MEMBLOCK_RSRV_KERN flag
Hi Changyuan,
Quoted 27 lines · Changyuan Lyu
Hi Mike,
Quoted 24 lines · Mike Rapoport
From: "Mike Rapoport (Microsoft)" <[email protected]>
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) <[email protected]>--- include/linux/memblock.h | 16 +++++++++++++++- mm/memblock.c | 32 ++++++++++++++++++++++++-------- 2 files changed, 39 insertions(+), 9 deletions(-)include/linux/memblock.h +3
-1@@ -116,7 +117,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); +
Quoted 8 lines
include/linux/memblock.h int memblock_add_node(phys_addr_t base, phys_addr_t size, int nid, · line 125
Quoted 3 lines
include/linux/memblock.h +3
-0+static __always_inline int memblock_reserve(phys_addr_t base, phys_addr_t size) +{ +return __memblock_reserve (base, size,NUMA_NO_NODE ,0 );Without this patch
memblock_reserveeventually callsmemblock_add_rangewithMAX_NUMNODES, but with this patch,memblock_reservecallsmemblock_add_rangewithNUMA_NO_NODE. Is it intended or an accidental typo? Thanks!
We were mixing NUMA_NO_NODE and MAX_NUMNODES for memory with undefined node id for a while, with MAX_NUMNODES being older and NUMA_NO_NODE newer define for the same thing.
To make sure both are treated correctly in memblock we use numa_valid_node() to check if a range has node id set.
Quoted 2 lines
Best,
Changyuan
Signature · 2 lines
Sincerely yours, Mike.
Mike Rapoport
maintainerPATCH v4 3/14 memblock: Add support for scratch memory
In reply to Mike RapoportFrom: Alexander Graf <[email protected]>
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.
--- include/linux/memblock.h | 20 +++++++++++++ mm/Kconfig | 4 +++ mm/memblock.c | 61 ++++++++++++++++++++++++++++++++++++++++ 3 files changed, 85 insertions(+)
include/linux/memblock.h +20 -0
@@ -42,6 +42,11 @@ 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_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 */
@@ -51,6 +56,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 */ };/**
@@ -145,6 +151,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_all (void );void memblock_free (void *ptr,size_t size);
@@ -289,6 +297,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,
@@ -617,5 +630,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 */
mm/Kconfig +4 -0
@@ -506,6 +506,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.
mm/memblock.c +61 -0
97 lines not on this page · raw message
Signature · 1 line
2.47.2
Re: [PATCH v4 03/14] memblock: Add support for scratch memory
Quoted 13 lines · Mike Rapoport
From: Alexander Graf <[email protected]>
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.
Quoted 6 lines
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
memblock_set_kho_scratch_only?
Quoted 6 lines
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
memblock_clear_kho_scratch_only?
Quoted 3 lines
may happen from scratch memblock regions. We will lift that restriction in the next patch.
Signature · 2 lines
Wei Yang Help you, Help me
Mike Rapoport
maintainerRe: [PATCH v4 03/14] memblock: Add support for scratch memory
Quoted 23 lines · Wei Yang
Quoted 13 lines · Mike Rapoport
From: Alexander Graf <[email protected]>
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.Quoted 6 lines
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
memblock_set_kho_scratch_only?
Quoted 8 lines
Quoted 6 lines
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
memblock_clear_kho_scratch_only?
Right, I missed those in the commit message.
Quoted 7 lines
Quoted 3 lines
may happen from scratch memblock regions. We will lift that restriction in the next patch.
Signature · 2 lines
Wei Yang Help you, Help me
Signature · 2 lines
Sincerely yours, Mike.
Mike Rapoport
maintainerPATCH v4 4/14 memblock: introduce memmap_init_kho_scratch()
In reply to Mike RapoportFrom: "Mike Rapoport (Microsoft)" <[email protected]>
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.
--- 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(-)
include/linux/memblock.h +2 -0
@@ -633,9 +633,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 */
mm/internal.h +2 -0
@@ -1053,6 +1053,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 ,
mm/memblock.c +22 -0
@@ -945,6 +945,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 /**
mm/mm_init.c +8 -3
@@ -705,7 +705,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) {pg_data_t *pgdat;int zid;
@@ -739,11 +739,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
@@ -760,7 +765,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
Signature · 1 line
2.47.2
Re: [PATCH v4 04/14] memblock: introduce memmap_init_kho_scratch()
Quoted 6 lines · Mike Rapoport
From: "Mike Rapoport (Microsoft)" <[email protected]>
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
memmap_init_kho_scratch_pages ?
Quoted 2 lines
in upcoming patches.
Signature · 2 lines
Wei Yang Help you, Help me
Mike Rapoport
maintainerPATCH v4 5/14 kexec: Add Kexec HandOver (KHO) generation helpers
In reply to Mike RapoportFrom: Alexander Graf <[email protected]>
This patch adds the core 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) Device Tree - Every KHO kexec carries a KHO specific flattened device tree blob that describes the state of the system. Device drivers can register to KHO to serialize their state 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.
--- Documentation/ABI/testing/sysfs-kernel-kho | 53 +++ .../admin-guide/kernel-parameters.txt | 24 + MAINTAINERS | 1 + include/linux/cma.h | 2 + include/linux/kexec.h | 18 + include/linux/kexec_handover.h | 10 + kernel/Makefile | 1 + kernel/kexec_handover.c | 450 ++++++++++++++++++ mm/internal.h | 3 - mm/mm_init.c | 8 + 10 files changed, 567 insertions(+), 3 deletions(-) create mode 100644 Documentation/ABI/testing/sysfs-kernel-kho create mode 100644 include/linux/kexec_handover.h create mode 100644 kernel/kexec_handover.c
Documentation/ABI/testing/sysfs-kernel-kho new file +53 -0
@@ -0,0 +1,53 @@ +What: /sys/kernel/kho/active +Date: December 2023 +Contact: Alexander Graf <[email protected]> +Description: + Kexec HandOver (KHO) allows Linux to transition the state of + compatible drivers into the next kexec'ed kernel. To do so, + device drivers will serialize their current state into a DT. + 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. + +What: /sys/kernel/kho/dt_max +Date: December 2023 +Contact: Alexander Graf <[email protected]> +Description: + KHO needs to allocate a buffer for the DT that gets + generated before it knows the final size. By default, it + will allocate 10 MiB for it. You can write to this file + to modify the size of that allocation. + +What: /sys/kernel/kho/dt +Date: December 2023 +Contact: Alexander Graf <[email protected]> +Description: + When KHO is active, the kernel exposes the generated DT that + carries its current KHO state in this file. Kexec user space + tooling can use this as input file for the KHO payload image. + +What: /sys/kernel/kho/scratch_len +Date: December 2023 +Contact: Alexander Graf <[email protected]> +Description: + 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. + +What: /sys/kernel/kho/scratch_phys +Date: December 2023 +Contact: Alexander Graf <[email protected]> +Description: + 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.
Documentation/admin-guide/kernel-parameters.txt +24 -0
@@ -2698,6 +2698,30 @@ 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: nn[KMG],mm[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 "nn[KMG],mm[KMG]" where the + first parameter defines the size of a global + scratch area and the second 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.
MAINTAINERS +1 -0
@@ -12826,6 +12826,7 @@ M: Eric Biederman <[email protected]> L: [email protected] S: Maintained W: http://kernel.org/pub/linux/utils/kernel/kexec/ +F: Documentation/ABI/testing/sysfs-kernel-kho F: include/linux/kexec.h F: include/uapi/linux/kexec.h F: kernel/kexec*
include/linux/cma.h +2 -0
@@ -56,6 +56,8 @@ extern void cma_reserve_pages_on_error(struct cma *cma);#ifdef CONFIG_CMA struct folio *cma_alloc_folio (struct cma *cma,int order,gfp_t gfp);bool cma_free_folio (struct cma *cma,const struct folio *folio); +/* Free whole pageblock and set its migration type to MIGRATE_CMA. */ +void init_cma_reserved_pageblock (struct page *page);#else static inline struct folio *cma_alloc_folio (struct cma *cma,int order,gfp_t gfp) {
include/linux/kexec.h +18 -0
@@ -483,6 +483,24 @@ void set_kexec_sig_enforced(void);static inline void set_kexec_sig_enforced (void ) {}#endif +/* KHO Notifier index */ +enum kho_event { +KEXEC_KHO_DUMP =0 , +KEXEC_KHO_ABORT =1 , +}; + +struct notifier_block ; + +#ifdef CONFIG_KEXEC_HANDOVER +int register_kho_notifier (struct notifier_block *nb); +int unregister_kho_notifier (struct notifier_block *nb); +void kho_memory_init (void ); +#else +static inline int register_kho_notifier (struct notifier_block *nb) {return 0 ; } +static inline int unregister_kho_notifier (struct notifier_block *nb) {return 0 ; } +static inline void kho_memory_init (void ) {} +#endif /* CONFIG_KEXEC_HANDOVER */ +#endif /* !defined(__ASSEBMLY__) */ #endif /* LINUX_KEXEC_H */
include/linux/kexec_handover.h new file +10 -0
@@ -0,0 +1,10 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +#ifndef LINUX_KEXEC_HANDOVER_H +#define LINUX_KEXEC_HANDOVER_H + +struct kho_mem { +phys_addr_t addr; +phys_addr_t size; +}; + +#endif /* LINUX_KEXEC_HANDOVER_H */
kernel/Makefile +1 -0
@@ -75,6 +75,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/
kernel/kexec_handover.c new file +450 -0
450 lines not on this page · raw message
mm/internal.h +0 -3
@@ -841,9 +841,6 @@ int isolate_migratepages_range(struct compact_control *cc, unsigned long low_pfn, unsigned long end_pfn); -/* Free whole pageblock and set its migration type to MIGRATE_CMA. */ -void init_cma_reserved_pageblock (struct page *page); -#endif /* CONFIG_COMPACTION || CONFIG_CMA */ int find_suitable_fallback(struct free_area *area, unsigned int order,
mm/mm_init.c +8 -0
@@ -30,6 +30,7 @@#include <linux/crash_dump.h> #include <linux/execmem.h> #include <linux/vmstat.h> +#include <linux/kexec.h> #include "internal.h" #include "slab.h" #include "shuffle.h"
@@ -2661,6 +2662,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 (); +mem_init ();kmem_cache_init ();/*
Signature · 1 line
2.47.2
Re: [PATCH v4 05/14] kexec: Add Kexec HandOver (KHO) generation helpers
Quoted 10 lines · Mike Rapoport
Documentation/ABI/testing/sysfs-kernel-kho new file +4
-0@@ -0,0 +1,53 @@ +What: /sys/kernel/kho/active +Date: December 2023 +Contact: Alexander Graf <[email protected]> +Description:
Documentation/ABI/testing/sysfs-kernel-kho line 14
Quoted 10 lines
Documentation/ABI/testing/sysfs-kernel-kho new file +10
-0+ Kexec HandOver (KHO) allows Linux to transition the state of + compatible drivers into the next kexec'ed kernel. To do so, + device drivers will serialize their current state into a DT. + 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.
I don't think this is a great interface for the actual state machine..
Documentation/ABI/testing/sysfs-kernel-kho line 23
Quoted 8 lines
Documentation/ABI/testing/sysfs-kernel-kho new file +8
-0+What: /sys/kernel/kho/dt_max +Date: December 2023 +Contact: Alexander Graf <[email protected]> +Description: + KHO needs to allocate a buffer for the DT that gets + generated before it knows the final size. By default, it + will allocate 10 MiB for it. You can write to this file + to modify the size of that allocation.
Seems gross, why can't it use a non-contiguous page list to generate the FDT? :\
See below for a suggestion..
Quoted 19 lines
Documentation/ABI/testing/sysfs-kernel-kho new file +19
-0+static int kho_serialize(void) +{ + void *fdt = NULL; + int err = -ENOMEM; + + fdt = kvmalloc(kho_out.dt_max, GFP_KERNEL); + if (!fdt) + goto out; + + if (fdt_create(fdt, kho_out.dt_max)) { + err = -EINVAL; + goto out; + } + + err = fdt_finish_reservemap(fdt); + if (err) + goto out; + + err = fdt_begin_node(fdt, "");
Documentation/ABI/testing/sysfs-kernel-kho line 51
Quoted 10 lines
Documentation/ABI/testing/sysfs-kernel-kho new file +10
-0+ if (err) + goto out; + + err = fdt_property_string(fdt, "compatible", "kho-v1"); + if (err) + goto out; + + /* Loop through all kho dump functions */ + err = blocking_notifier_call_chain(&kho_out.chain_head, KEXEC_KHO_DUMP, fdt); + err = notifier_to_errno(err);
I don't see this really working long term. I think we'd like each component to be able to serialize at its own pace under userspace control.
This design requires that the whole thing be wrapped in a notifier callback just so we can make use of the fdt APIs.
It seems like a poor fit me.
IMHO if you want to keep using FDT I suggest that each serializing component (ie driver, ftrace whatever) allocate its own FDT fragment from scratch and the main KHO one just link to the memories that holds those fragements.
Ie the driver experience would be more like
kho = kho_start_storage("my_compatible_string,v1", some_kind_of_instance_key);
fdt...(kho->fdt..)
kho_finish_storage(kho);
Where this ends up creating a stand alone FDT fragment:
/dts-v1/;
/ {
compatible = "linux-kho,my_compatible_string,v1"; instance = some_kind_of_instance_key; key-value-1 = <..>;
key-value-1 = <..>;
};
And then kho_finish_storage() would remember the phys/length until the kexec fdt is produced as the very last step.
This way we could do things like fdbox an iommufd and create the above FDT fragment completely seperately from any notifier chain and, crucially, disconnected from the fdt_create() for the kexec payload.
Further, if you split things like this (it will waste some small amount of memory) you can probably get to a point where no single FDT is more than 4k. That looks like it would simplify/robustify alot of stuff?
Jason
Pasha Tatashin
maintainerRe: [PATCH v4 05/14] kexec: Add Kexec HandOver (KHO) generation helpers
Quoted 16 lines · Jason Gunthorpe
Quoted 14 lines · Mike Rapoport
Documentation/ABI/testing/sysfs-kernel-kho new file +8
-0@@ -0,0 +1,53 @@ +What: /sys/kernel/kho/active +Date: December 2023 +Contact: Alexander Graf <[email protected]> +Description: + Kexec HandOver (KHO) allows Linux to transition the state of + compatible drivers into the next kexec'ed kernel. To do so, + device drivers will serialize their current state into a DT. + While the state is serialized, they are unable to perform
Quoted 8 lines
Documentation/ABI/testing/sysfs-kernel-kho line 14
Quoted 6 lines
Documentation/ABI/testing/sysfs-kernel-kho new file +6
-0+ 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.I don't think this is a great interface for the actual state machine..
In our next proposal we are going to remove this "activate" phase.
Quoted 12 lines
Documentation/ABI/testing/sysfs-kernel-kho line 23
Quoted 9 lines
Documentation/ABI/testing/sysfs-kernel-kho new file +8
-0+What: /sys/kernel/kho/dt_max +Date: December 2023 +Contact: Alexander Graf <[email protected]> +Description: + KHO needs to allocate a buffer for the DT that gets + generated before it knows the final size. By default, it + will allocate 10 MiB for it. You can write to this file + to modify the size of that allocation.Seems gross, why can't it use a non-contiguous page list to generate the FDT? :\
We will consider some of these ideas in the future version. I like the idea of using preserved memory to carry sparse KHO tree: i.e FDT over sparse memory, maybe use the anchor page to describe how it should be vmapped into a virtually contiguous tree in the next kernel?
Quoted 79 lines
See below for a suggestion..
Quoted 19 lines
Documentation/ABI/testing/sysfs-kernel-kho new file +19
-0+static int kho_serialize(void) +{ + void *fdt = NULL; + int err = -ENOMEM; + + fdt = kvmalloc(kho_out.dt_max, GFP_KERNEL); + if (!fdt) + goto out; + + if (fdt_create(fdt, kho_out.dt_max)) { + err = -EINVAL; + goto out; + } + + err = fdt_finish_reservemap(fdt); + if (err) + goto out; + + err = fdt_begin_node(fdt, "");Documentation/ABI/testing/sysfs-kernel-kho line 51
Quoted 10 lines
Documentation/ABI/testing/sysfs-kernel-kho new file +10
-0+ if (err) + goto out; + + err = fdt_property_string(fdt, "compatible", "kho-v1"); + if (err) + goto out; + + /* Loop through all kho dump functions */ + err = blocking_notifier_call_chain(&kho_out.chain_head, KEXEC_KHO_DUMP, fdt); + err = notifier_to_errno(err);I don't see this really working long term. I think we'd like each component to be able to serialize at its own pace under userspace control.
This design requires that the whole thing be wrapped in a notifier callback just so we can make use of the fdt APIs.
It seems like a poor fit me.
IMHO if you want to keep using FDT I suggest that each serializing component (ie driver, ftrace whatever) allocate its own FDT fragment from scratch and the main KHO one just link to the memories that holds those fragements.
Ie the driver experience would be more like
kho =
kho_start_storage("my_compatible_string,v1", some_kind_of_instance_key);fdt...(kho->fdt..)
kho_finish_storage(kho);Where this ends up creating a stand alone FDT fragment:
/dts-v1/;
/ {
compatible = "linux-kho,my_compatible_string,v1"; instance =some_kind_of_instance_key; key-value-1 = <..>;
key-value-1 = <..>;
};And then
kho_finish_storage()would remember the phys/length until the kexec fdt is produced as the very last step.This way we could do things like fdbox an iommufd and create the above FDT fragment completely seperately from any notifier chain and, crucially, disconnected from the
fdt_create()for the kexec payload.Further, if you split things like this (it will waste some small amount of memory) you can probably get to a point where no single FDT is more than 4k. That looks like it would simplify/robustify alot of stuff?
Jason
Re: [PATCH v4 05/14] kexec: Add Kexec HandOver (KHO) generation helpers
Quoted 13 lines · Pasha Tatashin
Quoted 12 lines
Documentation/ABI/testing/sysfs-kernel-kho line 23
Quoted 9 lines
Documentation/ABI/testing/sysfs-kernel-kho new file +8
-0+What: /sys/kernel/kho/dt_max +Date: December 2023 +Contact: Alexander Graf <[email protected]> +Description: + KHO needs to allocate a buffer for the DT that gets + generated before it knows the final size. By default, it + will allocate 10 MiB for it. You can write to this file + to modify the size of that allocation.Seems gross, why can't it use a non-contiguous page list to generate the FDT? :\
Quoted 4 lines
We will consider some of these ideas in the future version. I like the idea of using preserved memory to carry sparse KHO tree: i.e FDT over sparse memory, maybe use the anchor page to describe how it should be vmapped into a virtually contiguous tree in the next kernel?
Yeah, but this is now permanent uAPI that has to be kept forever. I think you should not add this when there are enough ideas on how to completely avoid it.
Jason
Pasha Tatashin
maintainerRe: [PATCH v4 05/14] kexec: Add Kexec HandOver (KHO) generation helpers
Quoted 15 lines · Jason Gunthorpe
Quoted 13 lines · Pasha Tatashin
Quoted 12 lines
Documentation/ABI/testing/sysfs-kernel-kho line 23
Quoted 9 lines
Documentation/ABI/testing/sysfs-kernel-kho new file +8
-0+What: /sys/kernel/kho/dt_max +Date: December 2023 +Contact: Alexander Graf <[email protected]> +Description: + KHO needs to allocate a buffer for the DT that gets + generated before it knows the final size. By default, it + will allocate 10 MiB for it. You can write to this file + to modify the size of that allocation.Seems gross, why can't it use a non-contiguous page list to generate the FDT? :\
Quoted 6 lines
Quoted 4 lines
We will consider some of these ideas in the future version. I like the idea of using preserved memory to carry sparse KHO tree: i.e FDT over sparse memory, maybe use the anchor page to describe how it should be vmapped into a virtually contiguous tree in the next kernel?
Yeah, but this is now permanent uAPI that has to be kept forever. I
Agree, what I meant in the future patch version is before it gets merged. I should have been more clear.
Quoted 2 lines
think you should not add this when there are enough ideas on how to completely avoid it.
Thinking about it some more, I'm actually leaning towards keeping things as they are, instead of going with a sparse FDT. With a sparse KHO-tree, we'd be kinda trying to fix something that should be handled higher up. All userspace preservable memory (like emulated pmem with devdax/fsdax and also pstore for logging) can already survive cold reboots with modified firmware Google and Microsoft do this. Similarly, the firmware could give the kernel the KHO-tree (generated by firmware or from the previous kernel) to keep stuff like telemetry, oops messages, time stamps etc. KHO should not be considered explicitly as a mechanism to carry device serialization data, the KHO should be a standard and simple way to pass kernel data between reboots. The more complex state can be built on top of it, for example guestmemfs, could preserve terabytes of data and have only one node in the KHO tree.
Quoted 2 lines
Jason
Re: [PATCH v4 05/14] kexec: Add Kexec HandOver (KHO) generation helpers
Quoted 5 lines · Pasha Tatashin
Quoted 2 lines
think you should not add this when there are enough ideas on how to completely avoid it.
Thinking about it some more, I'm actually leaning towards keeping things as they are, instead of going with a sparse FDT.
What is a sparse FDT? My suggestion each driver make its own FDT?
The reason for this was sequencing because we need a more more flexable way to manage all this serialization than just a notifier chain. The existing FDT construction process is too restrictive to accommodate this, IMHO.
That it also resolves the weird dt_max stuff above is a nice side effect.
Quoted 5 lines
With a sparse KHO-tree, we'd be kinda trying to fix something that should be handled higher up. All userspace preservable memory (like emulated pmem with devdax/fsdax and also pstore for logging) can already survive cold reboots with modified firmware Google and Microsoft do this.
I was hoping the VM memory wouldn't be in DAX. If you want some DAX stuff to interact with FW, OK, but I think the design here should be driving toward preserving a memfd/guestmemfd/hugetlbfs FDs directly and eliminate the DAX backed VMs. We won't get to CC guestmemfd with DAX.
fdbox of a guestmemfd, for instance.
To do that you need to preserve folios as the basic primitive.
Quoted 3 lines
Similarly, the firmware could give the kernel the KHO-tree (generated by firmware or from the previous kernel) to keep stuff like telemetry, oops messages, time stamps etc.
This feels like a mistake to comingle things like this. KHO is complex enough, it should stay focused on its thing..
Jason
Re: [PATCH v4 05/14] kexec: Add Kexec HandOver (KHO) generation helpers
Quoted 1 line · Jason Gunthorpe
To do that you need to preserve folios as the basic primitive.
I made a small sketch of what I suggest.
I imagine the FDT schema for this would look something like this:
/dts-v1/;
/ {
compatible = "linux-kho,v1";
phys-addr-size = 64;
void-p-size = 64;
preserved-folio-map = <phys_addr>;
// The per "driver" storage
instance@1 {..};
instance@2 {..};
};
I think this is alot better than what is in this series. It uses much less memory when there are alot of allocation, it supports any order folios, it is efficient for 1G guestmemfd folios, and it only needs a few bytes in the FDT. It could preserve and restore the high order folio struct page folding (HVO).
The use cases I'm imagining for drivers would be pushing gigabytes of memory into this preservation mechanism. It needs to be scalable!
This also illustrates my point that I don't think FDT is a good representation to use exclusively. This in-memory structure is much better and faster than trying to represent the same information embedded directly into the FDT. I imagine this to be the general pattern that drivers will want to use. A few bytes in the FDT pointing at a scalable in-memory structure for the bulk of the data.
/*
* Keep track of folio memory that is to be preserved across KHO.
*
* This is designed with the idea that the system will have alot of memory, eg
* 1TB, and the majority of it will be ~1G folios assigned to a hugetlb/etc
* being used to back guest memory. This would leave a smaller amount of memory,
* eg 16G, reserved for the hypervisor to use. The pages to preserve across KHO
* would be randomly distributed over the hypervisor memory. The hypervisor
* memory is not required to be contiguous.
*
* 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.
*
* The serializing side uses two levels of xarrays to manage chunks of per-order
* 512 byte bitmaps. For instance 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 hypervisor memory at most 512K
* of bitmap memory will be needed for order 0.
*/
struct kho_mem_track
{
/* Points to kho_mem_phys, each order gets its own bitmap tree */
struct xarray orders;
};struct kho_mem_phys
{
/*
* Points to kho_mem_phys_bits, a sparse bitmap array. Each bit is sized
* to order.
*/
struct xarray phys_bits;
};#define PRESERVE_BITS (512 * 8)
struct kho_mem_phys_bits
{
DECLARE_BITMAP(preserve, PRESERVE_BITS)
};
static void *xa_load_or_alloc(struct xarray *xa, unsigned long index, size_t elmsz)
{
void *elm;
void *res;
elm = xa_load(xa, index);
if (elm)
return elm;
elm = kzalloc(elmsz, GFP_KERNEL);
if (!elm)
return ERR_PTR(-ENOMEM);
res = xa_cmpxchg(xa, elmsz, NULL, elm, GFP_KERNEL);
if (xa_is_err(res)) {
kfree(elm);
return ERR_PTR(xa_err(res));
};
if (res != NULL) {
kfree(elm);
return res;
}
return elm;
}/*
* Record that the entire folio under virt is preserved across KHO. virt must
* have come from alloc_pages/folio_alloc or similar and point to the first page
* of the folio. The order will be preserved as well.
*/
int kho_preserve_folio(struct kho_mem_track *tracker, void *virt)
{
struct folio *folio = virt_to_folio(virt);
unsigned int order = folio_order(folio);
phys_addr_t phys = virt_to_phys(virt);
struct kho_mem_phys_bits *bits;
struct kho_mem_phys *physxa;
might_sleep();
physxa = xa_load_or_alloc(&tracker->orders, order, sizeof(*physxa));
if (IS_ERR(physxa))
return PTR_ERR(physxa);
phys >>= PAGE_SHIFT + order;
static_assert(sizeof(phys_addr_t) <= sizeof(unsigned long));
bits = xa_load_or_alloc(&physxa->phys_bits, phys / PRESERVE_BITS,
sizeof(*bits));
set_bit(phys % PRESERVE_BITS, bits->preserve);
return 0;
}#define KHOSER_PTR(type) union {phys_addr_t phys; type ptr;}
#define KHOSER_STORE_PTR(dest, val) \
({ \
(dest).phys = virt_to_phys(val); \
typecheck(typeof((dest).ptr), val); \
})#define KHOSER_LOAD_PTR(src) ((typeof((src).ptr))(phys_to_virt((src).phys)))
struct khoser_mem_bitmap_ptr {
phys_addr_t phys_start;
KHOSER_PTR(struct kho_mem_phys_bits *) bitmap;
};struct khoser_mem_chunk {
unsigned int order;
unsigned int num_elms;
KHOSER_PTR(struct khoser_mem_chunk *) next;
struct khoser_mem_bitmap_ptr
bitmaps[(PAGE_SIZE - 16) / sizeof(struct khoser_mem_bitmap_ptr)];
};static_assert(sizeof(struct khoser_mem_chunk) == PAGE_SIZE);
static int new_chunk(struct khoser_mem_chunk **cur_chunk)
{
struct khoser_mem_chunk *chunk;
chunk = kzalloc(sizeof(*chunk), GFP_KERNEL);
if (!chunk)
return -ENOMEM;
if (*cur_chunk)
KHOSER_STORE_PTR((*cur_chunk)->next, chunk);
*cur_chunk = chunk;
return 0;
}/*
* 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.
*/
int kho_serialize(struct kho_mem_track *tracker, phys_addr_t *fdt_value)
{
struct khoser_mem_chunk *first_chunk = NULL;
struct khoser_mem_chunk *chunk = NULL;
struct kho_mem_phys *physxa;
unsigned long order;
int ret;
xa_for_each(&tracker->orders, order, physxa) {
struct kho_mem_phys_bits *bits;
unsigned long phys;
ret = new_chunk(&chunk);
if (ret)
goto err_free;
if (!first_chunk)
first_chunk = chunk;
chunk->order = order;
xa_for_each(&physxa->phys_bits, phys, bits) {
struct khoser_mem_bitmap_ptr *elm;
if (chunk->num_elms == ARRAY_SIZE(chunk->bitmaps)) {
ret = new_chunk(&chunk);
if (ret)
goto err_free;
}
elm = &chunk->bitmaps[chunk->num_elms];
chunk->num_elms++;
elm->phys_start = phys << (order + PAGE_SIZE);
KHOSER_STORE_PTR(elm->bitmap, bits);
}
}
*fdt_value = virt_to_phys(first_chunk);
return 0;
err_free:
chunk = first_chunk;
while (chunk) {
struct khoser_mem_chunk *tmp = chunk;
chunk = KHOSER_LOAD_PTR(chunk->next);
kfree(tmp);
}
return ret;
}static void preserve_bitmap(unsigned int order,
struct khoser_mem_bitmap_ptr *elm)
{
struct kho_mem_phys_bits *bitmap = KHOSER_LOAD_PTR(elm->bitmap);
unsigned int bit;
for_each_set_bit(bit, bitmap->preserve, PRESERVE_BITS) {
phys_addr_t phys =
elm->phys_start + (bit << (order +
PAGE_SHIFT));
// Do the struct page stuff..
}
}void kho_deserialize(phys_addr_t fdt_value)
{
struct khoser_mem_chunk *chunk = phys_to_virt(fdt_value);
while (chunk) {
unsigned int i;
for (i = 0; i != chunk->num_elms; i++)
preserve_bitmap(chunk->order, chunk->bitmaps[i]);
chunk = KHOSER_LOAD_PTR(chunk->next);
}
}Mike Rapoport
maintainerRe: [PATCH v4 05/14] kexec: Add Kexec HandOver (KHO) generation helpers
Hi Jason,
Quoted 29 lines · Jason Gunthorpe
Quoted 1 line · Jason Gunthorpe
To do that you need to preserve folios as the basic primitive.
I made a small sketch of what I suggest.
I imagine the FDT schema for this would look something like this:
/dts-v1/;
/ {
compatible = "linux-kho,v1";
phys-addr-size = 64;
void-p-size = 64;
preserved-folio-map = <phys_addr>;// The per "driver" storage
instance@1 {..};
instance@2 {..};
};I think this is alot better than what is in this series. It uses much less memory when there are alot of allocation, it supports any order folios, it is efficient for 1G guestmemfd folios, and it only needs a few bytes in the FDT. It could preserve and restore the high order folio struct page folding (HVO).
The use cases I'm imagining for drivers would be pushing gigabytes of memory into this preservation mechanism. It needs to be scalable!
Quoted 6 lines
This also illustrates my point that I don't think FDT is a good representation to use exclusively. This in-memory structure is much better and faster than trying to represent the same information embedded directly into the FDT. I imagine this to be the general pattern that drivers will want to use. A few bytes in the FDT pointing at a scalable in-memory structure for the bulk of the data.
As I've mentioned off-list earlier, KHO in its current form is the lowest level of abstraction for state preservation and it is by no means is intended to provide complex drivers with all the tools necessary.
It's sole purpose is to allow preserving simple properties and ensure that memory ranges KHO clients need to preserve won't be overwritten.
What you propose is a great optimization for memory preservation mechanism, and additional and very useful abstraction layer on top of "basic KHO"!
But I think it will be easier to start with something *very simple* and probably suboptimal and then extend it rather than to try to build complex comprehensive solution from day one.
Signature · 2 lines
Sincerely yours, Mike.
Re: [PATCH v4 05/14] kexec: Add Kexec HandOver (KHO) generation helpers
Quoted 3 lines · Mike Rapoport
As I've mentioned off-list earlier, KHO in its current form is the lowest level of abstraction for state preservation and it is by no means is intended to provide complex drivers with all the tools necessary.
My point, is I think it is the wrong level of abstraction and the wrong FDT schema. It does not and cannot solve the problems we know we will have, so why invest anything into that schema?
I think the scratch system is great, and an amazing improvement over past version. Upgrade the memory preservation to match and it will be really good.
Quoted 2 lines
What you propose is a great optimization for memory preservation mechanism, and additional and very useful abstraction layer on top of "basic KHO"!
I do not see this as a layer on top, I see it as fundamentally replacing the memory preservation mechanism with something more scalable.
Quoted 3 lines
But I think it will be easier to start with something *very simple* and probably suboptimal and then extend it rather than to try to build complex comprehensive solution from day one.
But why? Just do it right from the start? I spent like a hour sketching that, the existing preservation code is also very simple, why not just fix it right now?
Jason
Mike Rapoport
maintainerRe: [PATCH v4 05/14] kexec: Add Kexec HandOver (KHO) generation helpers
Quoted 9 lines · Jason Gunthorpe
Quoted 3 lines · Mike Rapoport
As I've mentioned off-list earlier, KHO in its current form is the lowest level of abstraction for state preservation and it is by no means is intended to provide complex drivers with all the tools necessary.
My point, is I think it is the wrong level of abstraction and the wrong FDT schema. It does not and cannot solve the problems we know we will have, so why invest anything into that schema?
Preserving a lot of random pages spread all over the place will be a problem no matter what. With kho_preserve_folio() the users will still need to save physical address of that folio somewhere, be it FDT or some binary structure that FDT will point to. So either instead of "mem" properties we'll have "addresses" property or a pointer to yet another page that should be preserved and, by the way, "mem" may come handy in this case :)
I don't see how the "mem" property contradicts future extensions and for simple use cases it is already enough. The simple reserve_mem use case in this patchset indeed does not represent the complexity of a driver, but it's still useful, at least for the ftrace folks. And reserve_mem is just fine with "mem" property.
Quoted 10 lines
I think the scratch system is great, and an amazing improvement over past version. Upgrade the memory preservation to match and it will be really good.
Quoted 2 lines
What you propose is a great optimization for memory preservation mechanism, and additional and very useful abstraction layer on top of "basic KHO"!
I do not see this as a layer on top, I see it as fundamentally replacing the memory preservation mechanism with something more scalable.
There are two parts to the memory preservation: making sure the preserved pages don't make it to the free lists and than restoring struct page/folio/memdesc so that the pages will look the same way as when they were allocated.
For the first part we must memblock_reserve(addr, size) for every preserved range before memblock releases memory to the buddy.
I did an experiment and preserved 1GiB of random order-0 pages and measured time required to reserve everything in memblock. kho_deserialize() you suggested slightly outperformed kho_init_reserved_pages() that parsed a single "mem" property containing an array of <addr, size> pairs. For more random distribution of orders and more deep FDT the difference or course would be higher, but still both options sucked relatively to a maple tree serialized similarly to your tracker xarray.
For the restoration of the struct folio for multiorder folios the tracker xarray is a really great fit, but again, it does not contradict having "mem" properties. And the restoration of struct folio does not have to happen very early, so we'd probably want to run it in parallel, somewhat like deferred initialization of struct page.
Quoted 7 lines
Quoted 3 lines
But I think it will be easier to start with something *very simple* and probably suboptimal and then extend it rather than to try to build complex comprehensive solution from day one.
But why? Just do it right from the start? I spent like a hour sketching that, the existing preservation code is also very simple, why not just fix it right now?
As I see it, we can have both. "mem" property for simple use cases, or as a partial solution for complex use cases and tracker you proposed for preserving the order of the folios.
And as another optimization we may want a maple tree for coalescing as much as possible to reduce amount of memblock_reserve() calls.
Quoted 1 line
Jason
Signature · 2 lines
Sincerely yours, Mike.
Re: [PATCH v4 05/14] kexec: Add Kexec HandOver (KHO) generation helpers
Quoted 7 lines · Mike Rapoport
Quoted 6 lines · Jason Gunthorpe
Quoted 3 lines · Mike Rapoport
As I've mentioned off-list earlier, KHO in its current form is the lowest level of abstraction for state preservation and it is by no means is intended to provide complex drivers with all the tools necessary.
Quoted 7 lines
Quoted 3 lines
My point, is I think it is the wrong level of abstraction and the wrong FDT schema. It does not and cannot solve the problems we know we will have, so why invest anything into that schema?
Preserving a lot of random pages spread all over the place will be a problem no matter what. With
kho_preserve_folio()the users will still need to save physical address of that folio somewhere,
Yes of course. However the schema of each node now gets a choice for how it does that. ie the iommu is probably going to just store the top pointer of a page table and rely on the internal table pointers to store the physical addresses.
My point is that fdt mem should not be *mandatory* in the schema because it is inherently unscalable and not what we want.
Quoted 3 lines
structure that FDT will point to. So either instead of "mem" properties we'll have "addresses" property or a pointer to yet another page that should be preserved and, by the way, "mem" may come handy in this case :)
I think the preservation of the memory should be completely independent of the FDT scheme of the nodes. If ftrace wants a "mem" then sure, but the core preservation code should not parse it.
Nodes should be free to select whatever serialization scheme they want.
Memory preservation should be a seperate self-contained node with its own schema version. They should not be mixed together.
There should be single API toward the drivers, there should not get "automatic" preservation because they put magic stuff in the FDT.
Quoted 2 lines
I don't see how the "mem" property contradicts future extensions and for simple use cases it is already enough.
You'd just have to throw out all this code parsing mem to build the memblock.
It also makes the weird preallocation of the FDT and it's related sysfs probably unnecessary as it seems largely driven by this unbounded mem attribute problem.
Quoted 5 lines
I did an experiment and preserved 1GiB of random order-0 pages and measured time required to reserve everything in memblock.
kho_deserialize()you suggested slightly outperformedkho_init_reserved_pages()that parsed a single "mem" property containing an array of <addr, size> pairs.
It has to be considered end-to-end, there is more cost to build up the FDT array, and copying it around as well. Your 16Gib of random order 0 pages is 64MB of FDT space to represent as 16 byte addr/len pairs. That's alot of memory to be allocating, zeroing and copying around three (or four/five?) times.
So if the bitmap parsing is already slightly faster I expect the whole end-to-end solution will be notably faster.
Quoted 4 lines
For more random distribution of orders and more deep FDT the difference or course would be higher, but still both options sucked relatively to a maple tree serialized similarly to your tracker xarray.
I didn't like a maple tree like thing because the worst case memory requirements become much higher - and it is more expensive to build it on the serializing side (you have to run maple tree algorithms per-4k, and then copy it out of the maple tree to a representation). Maybe it is better, but I'd defer to real data on real systems before deciding.
With the numbers I was working with there are 512k of bitmaps worst case for 16G of memory. If you imagine encoding ranges in, say, 8 bytes per range (52 bits of phys_addr_t, 12 bits of length) then you get about 65k of ranges in the same 512k. That is only enough to store a random distribution of 256MB of 4k pages.
Still, I'd like the see the memory preservation have its own independent scheme, so if there is a better approach it can be upgraded as self-contained project. It should have no effect on the schema of the other nodes, or API toward the drivers.
Quoted 7 lines
Quoted 3 lines
But why? Just do it right from the start? I spent like a hour sketching that, the existing preservation code is also very simple, why not just fix it right now?
As I see it, we can have both. "mem" property for simple use cases, or as a partial solution for complex use cases and tracker you proposed for preserving the order of the folios.
I don't think that is a good idea. Two ways it is unnecessarily complicated. memory preservation should be integral to the system and be done in one way that works well for all cases. We definately don't want two APIs toward drivers for this.
If we have the bitmap then all drivers should be updated to use it. The core code parsing of the mem schema should be removed.
Quoted 2 lines
And as another optimization we may want a maple tree for coalescing as much as possible to reduce amount of
memblock_reserve()calls.
Is the bitmap scanning really such a high cost? It can be coalescing the set bitmap ranges with ffs/ffz if you want to run a memblock_reserve() sort of thing.
However, I was not imagining using something as inefficient as memblock_reserve() in the long run. It doesn't make sense to take a bitmap and then convert it into ranges, parse the ranges to build up the free list, then throw away the ranges.
Instead the bitmaps should be consulted as the free list is being built up immediately after allocating the struct pages. No ranges ever.
I didn't try to show this because it is definately complicated, but the serialize side has eveything indexed in xarrays so it can generate a linear sorted list of 'de-serializing' instructions that are slices of bitmaps of different orders. The code that builds the free list would simply walk that linear list of instructions and not add memory with set bits to the free list. Simple O(1) de-serialzing approach with some cost on the serializing side
I think going through memblock_reserve() is a good starting point, but there is certainly alot of room for improving away from using ranges.
Jason
Re: [PATCH v4 05/14] kexec: Add Kexec HandOver (KHO) generation helpers
In reply to Mike RapoportQuoted 36 lines · Mike Rapoport
From: Alexander Graf <[email protected]>
This patch adds the core 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) Device Tree - Every KHO kexec carries a KHO specific flattened device tree blob that describes the state of the system. Device drivers can register to KHO to serialize their state 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 <[email protected]>Co-developed-by Mike Rapoport (Microsoft) <[email protected]>Signed-off-by Mike Rapoport (Microsoft) <[email protected]>--- Documentation/ABI/testing/sysfs-kernel-kho | 53 +++ .../admin-guide/kernel-parameters.txt | 24 + MAINTAINERS | 1 + include/linux/cma.h | 2 + include/linux/kexec.h | 18 + include/linux/kexec_handover.h | 10 + kernel/Makefile | 1 + kernel/kexec_handover.c | 450 ++++++++++++++++++ mm/internal.h | 3 - mm/mm_init.c | 8 + 10 files changed, 567 insertions(+), 3 deletions(-) create mode 100644 Documentation/ABI/testing/sysfs-kernel-kho create mode 100644 include/linux/kexec_handover.h create mode 100644 kernel/kexec_handover.c
<snip>
include/linux/kexec_handover.h line 3
Quoted 6 lines
include/linux/kexec_handover.h new file +3
-0@@ -0,0 +1,10 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +#ifndef LINUX_KEXEC_HANDOVER_H +#define LINUX_KEXEC_HANDOVER_H
#include <linux/types.h>
Quoted 7 lines
include/linux/kexec_handover.h new file +7
-0+ +struct kho_mem { +phys_addr_t addr; +phys_addr_t size; +}; + +#endif /* LINUX_KEXEC_HANDOVER_H */
<snip>
include/linux/kexec_handover.h
Quoted 2 lines
include/linux/kexec_handover.h new file +2
-0+static ssize_t dt_read(struct file *file, struct kobject *kobj, + struct bin_attribute *attr, char *buf,
Please make the bin_attribute argument const. Currently both work, but the non-const variant will go away.
This way I can test my stuff on linux-next.
include/linux/kexec_handover.h
Quoted 10 lines
include/linux/kexec_handover.h new file +10
-0+ loff_t pos, size_t count) +{ +mutex_lock (&kho_out.lock); +memcpy (buf, attr->private + pos, count); +mutex_unlock (&kho_out.lock); + +return count; +} + +struct bin_attribute bin_attr_dt_kern =__BIN_ATTR (dt,0400 , dt_read,NULL ,0 );
The new __BIN_ATTR_ADMIN_RO() could make this slightly shorter.
<snip>
Mike Rapoport
maintainerPATCH v4 6/14 kexec: Add KHO parsing support
In reply to Mike RapoportFrom: Alexander Graf <[email protected]>
When we have a KHO kexec, we get a device tree 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 DT and memory reservations so they can recover their own state.
--- Documentation/ABI/testing/sysfs-firmware-kho | 9 + MAINTAINERS | 1 + include/linux/kexec.h | 12 + kernel/kexec_handover.c | 268 ++++++++++++++++++- mm/memblock.c | 1 + 5 files changed, 290 insertions(+), 1 deletion(-) create mode 100644 Documentation/ABI/testing/sysfs-firmware-kho
Documentation/ABI/testing/sysfs-firmware-kho new file +9 -0
@@ -0,0 +1,9 @@ +What: /sys/firmware/kho/dt +Date: December 2023 +Contact: Alexander Graf <[email protected]> +Description: + When the kernel was booted with Kexec HandOver (KHO), + the device tree that carries metadata about the previous + kernel's state is in this file. This file may disappear + when all consumers of it finished to interpret their + metadata.
MAINTAINERS +1 -0
@@ -12826,6 +12826,7 @@ M: Eric Biederman <[email protected]> L: [email protected] S: Maintained W: http://kernel.org/pub/linux/utils/kernel/kexec/ +F: Documentation/ABI/testing/sysfs-firmware-kho F: Documentation/ABI/testing/sysfs-kernel-kho F: include/linux/kexec.h F: include/uapi/linux/kexec.h
include/linux/kexec.h +12 -0
@@ -490,12 +490,24 @@ enum kho_event { };struct notifier_block ; +struct kho_mem ;#ifdef CONFIG_KEXEC_HANDOVER +void kho_populate (phys_addr_t dt_phys,phys_addr_t scratch_phys, +u64 scratch_len); +const void *kho_get_fdt (void ); +void kho_return_mem (const struct kho_mem *mem); +void *kho_claim_mem (const struct kho_mem *mem);int register_kho_notifier (struct notifier_block *nb);int unregister_kho_notifier (struct notifier_block *nb);void kho_memory_init (void );#else +static inline void kho_populate (phys_addr_t dt_phys,phys_addr_t scratch_phys, +u64 scratch_len) {} +static inline void *kho_get_fdt (void ) {return NULL ; } +static inline void kho_return_mem (const struct kho_mem *mem) { } +static inline void *kho_claim_mem (const struct kho_mem *mem) {return NULL ; } +static inline int register_kho_notifier (struct notifier_block *nb) {return 0 ; }static inline int unregister_kho_notifier (struct notifier_block *nb) {return 0 ; }static inline void kho_memory_init (void ) {}
kernel/kexec_handover.c +267 -1
304 lines not on this page · raw message
mm/memblock.c +1 -0
@@ -2366,6 +2366,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); }
Signature · 1 line
2.47.2
Re: [PATCH v4 06/14] kexec: Add KHO parsing support
Quoted 19 lines · Mike Rapoport
kernel/kexec_handover.c +19
-0+/** +* kho_claim_mem - Notify the kernel that a handed over memory range is now +* in use +* @mem: memory range that was preserved during kexec handover +* +* A kernel subsystem preserved that range during handover and it is going +* to reuse this range after kexec. The pages in the range are treated as +* allocated, but not %PG_reserved. +* +* Return: virtual address of the preserved memory range +*/ +void *kho_claim_mem (const struct kho_mem *mem) +{ +unsigned long start_pfn, end_pfn, pfn; +void *va =__va (mem->addr); + + start_pfn =PFN_DOWN (mem->addr); + end_pfn =PFN_UP (mem->addr + mem->size); +
kernel/kexec_handover.c int unregister_kho_notifier(struct notifier_block *nb) · line 156
Quoted 10 lines
kernel/kexec_handover.c +10
-0+for (pfn = start_pfn; pfn < end_pfn; pfn++) { +int err =kho_claim_pfn (pfn); + +if (err) +return NULL ; + } + +return va; +} +EXPORT_SYMBOL_GPL (kho_claim_mem);
I think this is not the sort of interface toward drivers we should be going for, I think we should be round tripping folios at their allocated order and when restored the folio should freed with folio_put(), just like in the normal way. Here you are breaking down high order folios and undoing the GFP_COMP, it is not desirable for drivers..
Eventually with some kind of support for conserving the memdesc struct/page metadata if a driver is using it.
Following that basic primitive you'd want to have the same idea to preserve kmalloc() memory.
And like I said elsewhere, the drivers should be working on naked phys_addr_t's stored in their own structs in their own way, not special kho_mem things.. It won't scale like this if the driver needs to pass thousands of pages.
Also, how is the driver supposed to figure out what the structure is inside the kho_mem anyhow? I would expect the FDT key/value store to have a key/phys_addr_t structure outlining the various driver data structures.
IMHO this links to my frist comment on how the FDT represents the preserved memory, it seems thsat FDT format cannot effectively preserve folios..
Jason
Re: [PATCH v4 06/14] kexec: Add KHO parsing support
In reply to Mike RapoportHi Mike,
On Thu, Feb 06 2025, Mike Rapoport wrote:
[...]
Quoted 19 lines
(quoted diff) +15
-0@@ -444,7 +576,141 @@ static void kho_reserve_scratch(void) kho_enable = false; } +/* + * Scan the DT for any memory ranges and make sure they are reserved in + * memblock, otherwise they will end up in a weird state on free lists. + */ +static void kho_init_reserved_pages(void) +{ + const void *fdt = kho_get_fdt(); + int offset = 0, depth = 0, initial_depth = 0, len; + + if (!fdt) + return; + + /* Go through the mem list and add 1 for each reference */ + for (offset = 0; + offset >= 0 && depth >= initial_depth;
Review static void kho_reserve_scratch(void) · line 603
Quoted 10 lines
(quoted diff) +10
-0+ offset = fdt_next_node(fdt, offset, &depth)) { + const struct kho_mem *mems; + u32 i; + + mems = fdt_getprop(fdt, offset, "mem", &len); + if (!mems || len & (sizeof(*mems) - 1)) + continue; + + for (i = 0; i < len; i += sizeof(*mems)) { + const struct kho_mem *mem = &mems[i];
i goes from 0 to len in steps of 16, but you use it to dereference an array of type struct kho_mem. So you end up only looking at only one of every 16 mems and do an out of bounds access. I found this when testing the memfd patches and any time the file was more than 1 page, it started to crash randomly.
Below patch should fix that:
---- 8< ----
kernel/kexec_handover.c +4 -2
@@ -685,13 +685,15 @@ static void kho_init_reserved_pages(void) offset >=0 && depth >= initial_depth; offset = fdt_next_node(fdt, offset, &depth)) {const struct kho_mem *mems; -u32 i; +u32 i, nr_mems; mems =fdt_getprop (fdt, offset,"mem" , &len);if (!mems || len & (sizeof (*mems) -1 ))continue ; -for (i =0 ; i < len; i +=sizeof (*mems)) { + nr_mems = len /sizeof (*mems); + +for (i =0 ; i < nr_mems; i++) {const struct kho_mem *mem = &mems[i];memblock_reserve (mem->addr, mem->size);
---- >8 ----
[...]
Signature · 2 lines
Regards, Pratyush Yadav
Mike Rapoport
maintainerRe: [PATCH v4 06/14] kexec: Add KHO parsing support
Hi Pratyush,
Quoted 31 lines · Pratyush Yadav
Hi Mike,
On Thu, Feb 06 2025, Mike Rapoport wrote:
[...]Quoted 27 lines
(quoted diff) +23
-0@@ -444,7 +576,141 @@ static void kho_reserve_scratch(void) kho_enable = false; } +/* + * Scan the DT for any memory ranges and make sure they are reserved in + * memblock, otherwise they will end up in a weird state on free lists. + */ +static void kho_init_reserved_pages(void) +{ + const void *fdt = kho_get_fdt(); + int offset = 0, depth = 0, initial_depth = 0, len; + + if (!fdt) + return; + + /* Go through the mem list and add 1 for each reference */ + for (offset = 0; + offset >= 0 && depth >= initial_depth; + offset = fdt_next_node(fdt, offset, &depth)) { + const struct kho_mem *mems; + u32 i; + + mems = fdt_getprop(fdt, offset, "mem", &len); + if (!mems || len & (sizeof(*mems) - 1)) + continue; +
Quoted 8 lines
Review static void kho_reserve_scratch(void) · line 603
Quoted 2 lines
(quoted diff) +2
-0+ for (i = 0; i < len; i += sizeof(*mems)) { + const struct kho_mem *mem = &mems[i];i goes from 0 to len in steps of 16, but you use it to dereference an array of type struct
kho_mem. So you end up only looking at only one of every 16 mems and do an out of bounds access. I found this when testing the memfd patches and any time the file was more than 1 page, it started to crash randomly.
Thanks! Changyuan already pointed that out privately.
But I'm going to adopt the memory reservation scheme Jason proposed so this code is going to go away anyway :)
Quoted 31 lines
Below patch should fix that:
---- 8< ----
kernel/kexec_handover.c +4
-3@@ -685,13 +685,15 @@ static void kho_init_reserved_pages(void) offset >=0 && depth >= initial_depth; offset = fdt_next_node(fdt, offset, &depth)) {const struct kho_mem *mems; -u32 i; +u32 i, nr_mems; mems =fdt_getprop (fdt, offset,"mem" , &len);if (!mems || len & (sizeof (*mems) -1 ))continue ; -for (i =0 ; i < len; i +=sizeof (*mems)) { + nr_mems = len /sizeof (*mems); + +for (i =0 ; i < nr_mems; i++) {const struct kho_mem *mem = &mems[i];memblock_reserve (mem->addr, mem->size); ---- >8 ----Signature · 3 lines
-- Regards, Pratyush Yadav
Signature · 2 lines
Sincerely yours, Mike.
Mike Rapoport
maintainerPATCH v4 7/14 kexec: Add KHO support to kexec file loads
In reply to Mike RapoportFrom: Alexander Graf <[email protected]>
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.
--- include/linux/kexec.h | 7 ++++ kernel/kexec_file.c | 19 +++++++++ kernel/kexec_handover.c | 92 +++++++++++++++++++++++++++++++++++++++++ kernel/kexec_internal.h | 16 +++++++ 4 files changed, 134 insertions(+)
include/linux/kexec.h +7 -0
@@ -364,6 +364,13 @@ struct kimage {size_t ima_buffer_size;#endif +#ifdef CONFIG_KEXEC_HANDOVER +struct { +struct kexec_buf dt; +struct kexec_buf scratch; + } kho; +#endif +/* Core ELF header buffer */ void *elf_headers;unsigned long elf_headers_sz;
kernel/kexec_file.c +19 -0
@@ -113,6 +113,12 @@ void kimage_file_post_load_cleanup(struct kimage *image) image->ima_buffer =NULL ;#endif /* CONFIG_IMA_KEXEC */ +#ifdef CONFIG_KEXEC_HANDOVER +kvfree (image->kho.dt.buffer); + image->kho.dt = (struct kexec_buf ) {}; + image->kho.scratch = (struct kexec_buf ) {}; +#endif +/* See if architecture has anything to cleanup post load */ arch_kimage_file_post_load_cleanup (image);
@@ -253,6 +259,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);
@@ -636,6 +647,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
kernel/kexec_handover.c +92 -0
104 lines not on this page · raw message
kernel/kexec_internal.h +16 -0
@@ -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 0 ; +} + +static inline int kho_fill_kimage (struct kimage *image) {return 0 ; } +#endif #endif /* LINUX_KEXEC_INTERNAL_H */
Signature · 1 line
2.47.2
Mike Rapoport
maintainerPATCH v4 8/14 kexec: Add config option for KHO
In reply to Mike RapoportFrom: Alexander Graf <[email protected]>
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.
--- kernel/Kconfig.kexec | 13 +++++++++++++ 1 file changed, 13 insertions(+)
kernel/Kconfig.kexec +13 -0
@@ -95,6 +95,19 @@ 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 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
Signature · 1 line
2.47.2
Mike Rapoport
maintainerPATCH v4 9/14 kexec: Add documentation for KHO
In reply to Mike RapoportFrom: Alexander Graf <[email protected]>
With KHO in place, let's add documentation that describes what it is and how to use it.
--- Documentation/kho/concepts.rst | 80 ++++++++++++++++++++++++++++++++ Documentation/kho/index.rst | 19 ++++++++ Documentation/kho/usage.rst | 60 ++++++++++++++++++++++++ Documentation/subsystem-apis.rst | 1 + MAINTAINERS | 1 + 5 files changed, 161 insertions(+) create mode 100644 Documentation/kho/concepts.rst create mode 100644 Documentation/kho/index.rst create mode 100644 Documentation/kho/usage.rst
Documentation/kho/concepts.rst new file +80 -0
@@ -0,0 +1,80 @@ +.. SPDX-License-Identifier: GPL-2.0-or-later + +======================= +Kexec Handover Concepts +======================= + +Kexec HandOver (KHO) is a mechanism that allows Linux to preserve state - +arbitrary properties as well as memory locations - across kexec. + +It introduces multiple concepts: + +KHO Device Tree +--------------- + +Every KHO kexec carries a KHO specific flattened device tree blob that +describes the state of the system. Device drivers can register to KHO to +serialize their state before kexec. After KHO, device drivers can read +the device tree and extract previous state. + +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 introduces a new concept to its device tree: ``mem`` properties. A +``mem`` property can be inside any subnode in the device tree. When present, +it contains an array of physical memory ranges that the new kernel must mark +as reserved on boot. It is recommended, but not required, to make these ranges +as physically contiguous as possible to reduce the number of array elements :: + + struct kho_mem { + __u64 addr; + __u64 len; + }; + +After boot, drivers can call the kho subsystem to transfer ownership of memory +that was reserved via a ``mem`` property to themselves to continue using memory +from the previous execution. + +The KHO device tree follows the in-Linux schema requirements. Any element in +the device tree is documented via device tree schema yamls that explain what +data gets transferred. + +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 particilar 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 irrenevant for +the next KHO, because kexec can overwrite even the original kernel. + +KHO active phase +---------------- + +To enable user space based kexec file loader, the kernel needs to be able to +provide the device tree that describes the previous kernel's state before +performing the actual kexec. The process of generating that device tree is +called serialization. When the device tree is generated, some properties +of the system may become immutable because they are already written down +in the device tree. That state is called the KHO active phase.
Documentation/kho/index.rst new file +19 -0
@@ -0,0 +1,19 @@ +.. SPDX-License-Identifier: GPL-2.0-or-later + +======================== +Kexec Handover Subsystem +======================== + +.. toctree:: + :maxdepth: 1 + + concepts + usage + +.. only:: subproject and html + + + Indices + ======= + + * :ref:`genindex`
Documentation/kho/usage.rst new file +60 -0
60 lines not on this page · raw message
Documentation/subsystem-apis.rst +1 -0
@@ -90,3 +90,4 @@ Other subsystems peci/index wmi/index tee/index + kho/index
MAINTAINERS +1 -0
@@ -12828,6 +12828,7 @@ S: Maintained W: http://kernel.org/pub/linux/utils/kernel/kexec/ F: Documentation/ABI/testing/sysfs-firmware-kho F: Documentation/ABI/testing/sysfs-kernel-kho +F: Documentation/kho/ F: include/linux/kexec.h F: include/uapi/linux/kexec.h F: kernel/kexec*
Signature · 1 line
2.47.2
Re: [PATCH v4 09/14] kexec: Add documentation for KHO
Quoted 2 lines · Mike Rapoport
+KHO introduces a new concept to its device tree: ``mem`` properties. A +``mem`` property can be inside any subnode in the device tree.
I do not think this is a good idea.
It should be core infrastructure, totally unrelated to any per-device fdt nodes, to carry the memory map.
IOW a full DT that looks something more like:
/dts-v1/;
/ {
compatible = "linux-kho,v1";
allocated-memory {
<>};
ftracebuffer {
compatible = "linux-kho,ftracem,v1"; ftrace-buffer-phys = <..>; ftrace-buffer-len = <..>; ..etc..
};
};
Where allocated_memory will remove all memory from the buddy allocator very early on in an efficient way. that process should not be walking the fdt to find mem nodes.
Documentation/kho/concepts.rst line 38
Quoted 3 lines
Documentation/kho/concepts.rst new file +3
-0+After boot, drivers can call the kho subsystem to transfer ownership of memory +that was reserved via a ``mem`` property to themselves to continue using memory +from the previous execution.
And this transfer should be done by phys that the node itself describes.
Ie if ftrace has a single high order folio to store it's ftrace buffer then I would expect code like:
allocate ftrace:
buffer = folio_alloc(..);
activate callback:
kho_preserve_folio(buffer) fdt...("ftrace-buffer-phys", virt_to_phys(buffer))
restore callback:
buffer_phys = fdt..("ftrace-buffer-phys")
buffer = kho_restore_folio(buffer_phys)
[..]
destroy ftrace:
folio_put(buffer);
And kho will take care to restore the struct folio, put back the order, etc, etc.
Similar for slab.
I think this sort of memory-based operation should be the very basic core building primitive here.
So the allocated-memory node should preserve information about KHO'd folios, their order and so on.
It doesn't matter what part of the FDT owns those folios, all the core kernel should do is keep track of them and at some point check that all preserved folios have been claimed.
Documentation/kho/concepts.rst line 56
Quoted 5 lines
Documentation/kho/concepts.rst new file +5
-0+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 particilar NUMA node assignment.
This plan sounds great, way better than the pmem approaches/etc.
Documentation/kho/concepts.rst line 80
Quoted 6 lines
Documentation/kho/concepts.rst new file +6
-0+To enable user space based kexec file loader, the kernel needs to be able to +provide the device tree that describes the previous kernel's state before +performing the actual kexec. The process of generating that device tree is +called serialization. When the device tree is generated, some properties +of the system may become immutable because they are already written down +in the device tree. That state is called the KHO active phase.
This should have a whole state diagram as we've talked a few times. There is alot more to worry about here than just 'activate'.
Jason
Mike Rapoport
maintainerPATCH v4 10/14 arm64: Add KHO support
In reply to Mike RapoportFrom: Alexander Graf <[email protected]>
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 arm64 and adds the respective kconfig option to the architecture so that it can use KHO successfully.
--- arch/arm64/Kconfig | 3 +++ drivers/of/fdt.c | 36 ++++++++++++++++++++++++++++++++++++ drivers/of/kexec.c | 42 ++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 81 insertions(+)
arch/arm64/Kconfig +3 -0
@@ -1590,6 +1590,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
drivers/of/fdt.c +36 -0
@@ -875,6 +875,39 @@ 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 kho_start, 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-dt" , &l); +if (l != (dt_root_addr_cells + dt_root_size_cells) *sizeof (__be32)) +return ; + + kho_start =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); + + p =of_get_flat_dt_prop (node,"linux,kho-mem" , &l); +if (l != (dt_root_addr_cells + dt_root_size_cells) *sizeof (__be32)) +return ; + +kho_populate (kho_start, scratch_start, scratch_size); +} +#ifdef CONFIG_SERIAL_EARLYCON int __init early_init_dt_scan_chosen_stdout (void )
@@ -1169,6 +1202,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)
drivers/of/kexec.c +42 -0
54 lines not on this page · raw message
Signature · 1 line
2.47.2
Re: [PATCH v4 10/14] arm64: Add KHO support
Quoted 3 lines · Mike Rapoport
From: Alexander Graf <[email protected]>
We now have all bits in place to support KHO kexecs. This patch adds
Please do not use "This commit/patch/change", but imperative mood. See longer explanation here:
https://elixir.bootlin.com/linux/v5.17.1/source/Documentation/process/submitting-patches.rst#L95
Quoted 8 lines
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.
Signed-off-by Alexander Graf <[email protected]>Co-developed-by Mike Rapoport (Microsoft) <[email protected]>Signed-off-by Mike Rapoport (Microsoft) <[email protected]>---
...
Quoted 5 lines
drivers/of/kexec.c +5
-0+#ifdef CONFIG_KEXEC_HANDOVER + dt = image->kho.dt.buffer; + dt_mem = image->kho.dt.mem; + dt_len = image->kho.dt.bufsz; +
drivers/of/kexec.c static inline int setup_ima_buffer(const struct kimage *image, void *fdt, · line 290
Quoted 10 lines
drivers/of/kexec.c +10
-0+ scratch_mem = image->kho.scratch.mem; + scratch_len = image->kho.scratch.bufsz; +#endif + +if (!dt) + goto out; + +pr_debug ("Adding kho metadata to DT" ); + + ret = fdt_appendprop_addrrange(fdt, 0, chosen_node, "linux,kho-dt",
Where is the ABI documentation for this?
drivers/of/kexec.c static inline int setup_ima_buffer(const struct kimage *image, void *fdt, · line 295
Quoted 5 lines
drivers/of/kexec.c +5
-0+ dt_mem, dt_len); +if (ret) + goto out; + + ret = fdt_appendprop_addrrange(fdt, 0, chosen_node, "linux,kho-scratch",
Same question.
drivers/of/kexec.c static inline int setup_ima_buffer(const struct kimage *image, void *fdt, · line 299
Quoted 4 lines
drivers/of/kexec.c +4
-0+ scratch_mem, scratch_len); +if (ret) + goto out; +
Best regards,
Krzysztof
Mike Rapoport
maintainerPATCH v4 11/14 x86/setup: use memblock_reserve_kern for memory used by kernel
In reply to Mike RapoportFrom: "Mike Rapoport (Microsoft)" <[email protected]>
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.
--- arch/x86/kernel/setup.c | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-)
arch/x86/kernel/setup.c +7 -7
@@ -220,8 +220,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 */
@@ -294,7 +294,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 )
@@ -347,7 +347,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; }
@@ -447,7 +447,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;
@@ -461,7 +461,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;
@@ -649,7 +649,7 @@ 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), +memblock_reserve_kern (__pa_symbol (_text), (unsigned long )__end_of_kernel_reserve - (unsigned long )_text);/*
Signature · 1 line
2.47.2
Mike Rapoport
maintainerPATCH v4 12/14 x86: Add KHO support
In reply to Mike RapoportFrom: Alexander Graf <[email protected]>
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.
--- 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(-)
arch/x86/Kconfig +3 -0
@@ -2090,6 +2090,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)
arch/x86/boot/compressed/kaslr.c +51 -1
64 lines not on this page · raw message
arch/x86/include/asm/setup.h +4 -0
@@ -66,6 +66,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>
arch/x86/include/uapi/asm/setup_data.h +12 -1
@@ -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 dt_addr; +__u64 dt_size; +__u64 scratch_addr; +__u64 scratch_size; +} __attribute__((packed)); +#endif /* __ASSEMBLY__ */ #endif /* _UAPI_ASM_X86_SETUP_DATA_H */
arch/x86/kernel/e820.c +18 -0
@@ -1329,6 +1329,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 );
arch/x86/kernel/kexec-bzimage64.c +36 -0
@@ -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.dt.buffer || !image->kho.scratch.buffer) +return ; + +/* Add setup data */ + kho->dt_addr = image->kho.dt.mem; + kho->dt_size = image->kho.dt.bufsz; + 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 );
arch/x86/kernel/setup.c +25 -0
@@ -385,6 +385,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->dt_addr, 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;
@@ -413,6 +435,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);
arch/x86/realmode/init.c +2 -0
@@ -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)
Signature · 1 line
2.47.2
Re: [PATCH v4 12/14] x86: Add KHO support
Quoted 1 line · Mike Rapoport
From: Alexander Graf <[email protected]>
[...]
Quoted 16 lines
arch/x86/kernel/e820.c +8
-0@@ -1329,6 +1329,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.
arch/x86/kernel/e820.c void __init e820__memblock_setup(void) · line 1349
Quoted 10 lines
arch/x86/kernel/e820.c +10
-0+* +* 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 ); +
At the beginning of e820__memblock_setup() we call memblock_allow_resize(), which means during adding memory region it could double the array. And the memory used here is from some region just added.
But with KHO, I am afraid it would fail?
Quoted 3 lines
arch/x86/kernel/e820.c +0
-0/* Throw away partial pages: */ memblock_trim_memory (PAGE_SIZE );
Signature · 2 lines
Wei Yang Help you, Help me
Mike Rapoport
maintainerRe: [PATCH v4 12/14] x86: Add KHO support
Quoted 25 lines · Wei Yang
Quoted 1 line · Mike Rapoport
From: Alexander Graf <[email protected]>
[...]
Quoted 22 lines
arch/x86/kernel/e820.c +14
-0@@ -1329,6 +1329,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
Quoted 8 lines
arch/x86/kernel/e820.c void __init e820__memblock_setup(void) · line 1349
Quoted 4 lines
arch/x86/kernel/e820.c +4
-0+* marking from the memory below ISA_END_ADDRESS +*/ +memblock_mark_kho_scratch (0 ,ISA_END_ADDRESS ); +At the beginning of
e820__memblock_setup()we callmemblock_allow_resize(), which means during adding memory region it could double the array. And the memory used here is from some region just added.
There are large KHO scratch areas that will be used for most allocations. Marking the memory below ISA_END_ADDRESS as KHO scratch is required to satisfy allocations that explicitly limit the allocation to ISA_END_ADDRESS, e.g the real time trampoline.
Quoted 9 lines
But with KHO, I am afraid it would fail?
Quoted 3 lines
arch/x86/kernel/e820.c +0
-0/* Throw away partial pages: */ memblock_trim_memory (PAGE_SIZE );Signature · 2 lines
Wei Yang Help you, Help me
Signature · 2 lines
Sincerely yours, Mike.
Re: [PATCH v4 12/14] x86: Add KHO support
Quoted 35 lines · Mike Rapoport
Quoted 25 lines · Wei Yang
Quoted 1 line · Mike Rapoport
From: Alexander Graf <[email protected]>
[...]
Quoted 22 lines
arch/x86/kernel/e820.c +14
-0@@ -1329,6 +1329,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 Quoted 8 lines
arch/x86/kernel/e820.c void __init e820__memblock_setup(void) · line 1349
Quoted 4 lines
arch/x86/kernel/e820.c +4
-0+* marking from the memory below ISA_END_ADDRESS +*/ +memblock_mark_kho_scratch (0 ,ISA_END_ADDRESS ); +At the beginning of
e820__memblock_setup()we callmemblock_allow_resize(), which means during adding memory region it could double the array. And the memory used here is from some region just added.
Quoted 5 lines
There are large KHO scratch areas that will be used for most allocations. Marking the memory below
ISA_END_ADDRESSas KHO scratch is required to satisfy allocations that explicitly limit the allocation toISA_END_ADDRESS, e.g the real time trampoline.
Thanks, I see you point. We would add memory region during kho_populate() and mark it scratch.
Quoted 13 lines
Quoted 9 lines
But with KHO, I am afraid it would fail?
Quoted 3 lines
arch/x86/kernel/e820.c +0
-0/* Throw away partial pages: */ memblock_trim_memory (PAGE_SIZE );Signature · 2 lines
Wei Yang Help you, Help meSignature · 2 lines
Sincerely yours, Mike.
Signature · 2 lines
Wei Yang Help you, Help me
Mike Rapoport
maintainerPATCH v4 13/14 memblock: Add KHO support for reserve_mem
In reply to Mike RapoportFrom: Alexander Graf <[email protected]>
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.
--- mm/memblock.c | 131 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 131 insertions(+)
mm/memblock.c +131 -0
@@ -16,6 +16,9 @@#include <linux/kmemleak.h> #include <linux/seq_file.h> #include <linux/memblock.h> +#include <linux/kexec_handover.h> +#include <linux/kexec.h> +#include <linux/libfdt.h> #include <asm/sections.h> #include <linux/io.h>
@@ -2423,6 +2426,70 @@ int reserve_mem_find_by_name(const char *name, phys_addr_t *start, phys_addr_t * }EXPORT_SYMBOL_GPL (reserve_mem_find_by_name); +static bool __init reserve_mem_kho_revive (const char *name,phys_addr_t size, +phys_addr_t align) +{ +const void *fdt =kho_get_fdt (); +const char *path ="/reserve_mem" ; +int node, child, err; + +if (!IS_ENABLED (CONFIG_KEXEC_HANDOVER )) +return false; + +if (!fdt) +return false; + + node =fdt_path_offset (fdt,"/reserve_mem" ); +if (node <0 ) +return false; + + err =fdt_node_check_compatible (fdt, node,"reserve_mem-v1" ); +if (err) { +pr_warn ("Node '%s' has unknown compatible" , path); +return false; + } + +fdt_for_each_subnode (child, fdt, node) { +const struct kho_mem *mem; +const char *child_name; +int len; + +/* Search for old kernel's reserved_mem with the same name */ + child_name =fdt_get_name (fdt, child,NULL ); +if (strcmp (name, child_name)) +continue ; + + err =fdt_node_check_compatible (fdt, child,"reserve_mem_map-v1" ); +if (err) { +pr_warn ("Node '%s/%s' has unknown compatible" , path, name); +continue ; + } + + mem =fdt_getprop (fdt, child,"mem" , &len); +if (!mem || len !=sizeof (*mem)) +continue ; + +if (mem->addr & (align -1 )) { +pr_warn ("KHO reserved_mem '%s' has wrong alignment (0x%lx, 0x%lx)" , + name, (long )align, (long )mem->addr); +continue ; + } + +if (mem->size != size) { +pr_warn ("KHO reserved_mem '%s' has wrong size (0x%lx != 0x%lx)" , + name, (long )mem->size, (long )size); +continue ; + } + +reserved_mem_add (mem->addr, mem->size, name); +pr_info ("Revived memory reservation '%s' from KHO" , name); + +return true; + } + +return false; +} +/* * Parse reserve_mem=nn:align:name */
@@ -2478,6 +2545,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 ;
@@ -2488,6 +2560,65 @@ static int __init reserve_mem(char *p) }__setup ("reserve_mem=" , reserve_mem); +static int reserve_mem_kho_write_map (void *fdt,struct reserve_mem_table *map) +{ +int err =0 ; +const char compatible[] ="reserve_mem_map-v1" ; +struct kho_mem mem = { + .addr = map->start, + .size = map->size, + }; + + err |=fdt_begin_node (fdt, map->name); + err |=fdt_property (fdt,"compatible" , compatible,sizeof (compatible)); + err |=fdt_property (fdt,"mem" , &mem,sizeof (mem)); + err |=fdt_end_node (fdt); + +return err; +} + +static int reserve_mem_kho_notifier (struct notifier_block *self, +unsigned long cmd,void *v) +{ +const char compatible[] ="reserve_mem-v1" ; +void *fdt = v; +int err =0 ; +int i; + +switch (cmd) { +case KEXEC_KHO_ABORT : +return NOTIFY_DONE ; +case KEXEC_KHO_DUMP : +/* Handled below */ +break ; +default : +return NOTIFY_BAD ; + } + +if (!reserved_mem_count) +return NOTIFY_DONE ; + + err |=fdt_begin_node (fdt,"reserve_mem" ); + err |=fdt_property (fdt,"compatible" , compatible,sizeof (compatible)); +for (i =0 ; i < reserved_mem_count; i++) + err |=reserve_mem_kho_write_map (fdt, &reserved_mem_table[i]); + err |=fdt_end_node (fdt); + +return err ?NOTIFY_BAD :NOTIFY_DONE ; +} + +static struct notifier_block reserve_mem_kho_nb = { + .notifier_call = reserve_mem_kho_notifier, +}; + +static int __init reserve_mem_init (void ) +{ +register_kho_notifier (&reserve_mem_kho_nb); + +return 0 ; +} +core_initcall (reserve_mem_init); +#if defined(CONFIG_DEBUG_FS ) && defined(CONFIG_ARCH_KEEP_MEMBLOCK )static const char *const flagname[] = { [ilog2(MEMBLOCK_HOTPLUG)] = "HOTPLUG",
Signature · 1 line
2.47.2
Re: [PATCH v4 13/14] memblock: Add KHO support for reserve_mem
Quoted 66 lines · Mike Rapoport
From: Alexander Graf <[email protected]>
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 <[email protected]>Co-developed-by Mike Rapoport (Microsoft) <[email protected]>Signed-off-by Mike Rapoport (Microsoft) <[email protected]>--- mm/memblock.c | 131 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 131 insertions(+)mm/memblock.c +27
-0@@ -16,6 +16,9 @@#include <linux/kmemleak.h> #include <linux/seq_file.h> #include <linux/memblock.h> +#include <linux/kexec_handover.h> +#include <linux/kexec.h> +#include <linux/libfdt.h> #include <asm/sections.h> #include <linux/io.h> @@ -2423,6 +2426,70 @@ int reserve_mem_find_by_name(const char *name, phys_addr_t *start, phys_addr_t * }EXPORT_SYMBOL_GPL (reserve_mem_find_by_name); +static bool __init reserve_mem_kho_revive (const char *name,phys_addr_t size, +phys_addr_t align) +{ +const void *fdt =kho_get_fdt (); +const char *path ="/reserve_mem" ; +int node, child, err; + +if (!IS_ENABLED (CONFIG_KEXEC_HANDOVER )) +return false; + +if (!fdt) +return false; + + node =fdt_path_offset (fdt,"/reserve_mem" ); +if (node <0 ) +return false; + + err =fdt_node_check_compatible (fdt, node,"reserve_mem-v1" ); +if (err) { +pr_warn ("Node '%s' has unknown compatible" , path); +return false; + } + +fdt_for_each_subnode (child, fdt, node) {
mm/memblock.c int reserve_mem_find_by_name(const char *name, phys_addr_t *start, phys_addr_t * · line 2462
Quoted 10 lines
mm/memblock.c +10
-0+const struct kho_mem *mem; +const char *child_name; +int len; + +/* Search for old kernel's reserved_mem with the same name */ + child_name =fdt_get_name (fdt, child,NULL ); +if (strcmp (name, child_name)) +continue ; + + err =fdt_node_check_compatible (fdt, child,"reserve_mem_map-v1" );
It really seems you all are trying to have things both ways. It's not Devicetree, just the FDT file format, but then here you use "compatible" which *is* Devicetree. At best, it's all just confusing for folks. At worst, you're just picking and choosing what you want to use.
I'm not saying don't use "compatible" just for the sake of looking less like DT, but perhaps your versioning should be done differently. You are reading the 'mem' property straight into a struct. Maybe the struct should have a version. Or the size of the struct is the version much like the userspace ABI is handled for structs.
mm/memblock.c int reserve_mem_find_by_name(const char *name, phys_addr_t *start, phys_addr_t * · line 2472
Quoted 10 lines
mm/memblock.c +10
-0+if (err) { +pr_warn ("Node '%s/%s' has unknown compatible" , path, name); +continue ; + } + + mem =fdt_getprop (fdt, child,"mem" , &len); +if (!mem || len !=sizeof (*mem)) +continue ; + +if (mem->addr & (align -1 )) {
It's stated somewhere in this that the FDT data is LE, but here you are assuming the FDT is the same endianness as the CPU not that it's LE. Arm64 can do BE. PowerPC does both. I'm not sure if kexec from one endianness to another is possible. I would guess in theory it is and in practice it's broken already (because kexec is always an afterthought). Either you need to guarantee that native endianness will never be an issue for any arch or you need to make the endianness fixed.
Rob
Mike Rapoport
maintainerRe: [PATCH v4 13/14] memblock: Add KHO support for reserve_mem
Quoted 84 lines · Rob Herring
Quoted 66 lines · Mike Rapoport
From: Alexander Graf <[email protected]>
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 <[email protected]>Co-developed-by Mike Rapoport (Microsoft) <[email protected]>Signed-off-by Mike Rapoport (Microsoft) <[email protected]>--- mm/memblock.c | 131 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 131 insertions(+)mm/memblock.c +27
-0@@ -16,6 +16,9 @@#include <linux/kmemleak.h> #include <linux/seq_file.h> #include <linux/memblock.h> +#include <linux/kexec_handover.h> +#include <linux/kexec.h> +#include <linux/libfdt.h> #include <asm/sections.h> #include <linux/io.h> @@ -2423,6 +2426,70 @@ int reserve_mem_find_by_name(const char *name, phys_addr_t *start, phys_addr_t * }EXPORT_SYMBOL_GPL (reserve_mem_find_by_name); +static bool __init reserve_mem_kho_revive (const char *name,phys_addr_t size, +phys_addr_t align) +{ +const void *fdt =kho_get_fdt (); +const char *path ="/reserve_mem" ; +int node, child, err; + +if (!IS_ENABLED (CONFIG_KEXEC_HANDOVER )) +return false; + +if (!fdt) +return false; + + node =fdt_path_offset (fdt,"/reserve_mem" ); +if (node <0 ) +return false; + + err =fdt_node_check_compatible (fdt, node,"reserve_mem-v1" ); +if (err) { +pr_warn ("Node '%s' has unknown compatible" , path); +return false; + } + +fdt_for_each_subnode (child, fdt, node) {mm/memblock.c int reserve_mem_find_by_name(const char *name, phys_addr_t *start, phys_addr_t * · line 2462
Quoted 10 lines
mm/memblock.c +10
-0+const struct kho_mem *mem; +const char *child_name; +int len; + +/* Search for old kernel's reserved_mem with the same name */ + child_name =fdt_get_name (fdt, child,NULL ); +if (strcmp (name, child_name)) +continue ; + + err =fdt_node_check_compatible (fdt, child,"reserve_mem_map-v1" );It really seems you all are trying to have things both ways. It's not Devicetree, just the FDT file format, but then here you use "compatible" which *is* Devicetree. At best, it's all just confusing for folks. At worst, you're just picking and choosing what you want to use.
Quoted 5 lines
I'm not saying don't use "compatible" just for the sake of looking less like DT, but perhaps your versioning should be done differently. You are reading the 'mem' property straight into a struct. Maybe the struct should have a version. Or the size of the struct is the version much like the userspace ABI is handled for structs.
The idea is to have high level compatibility notion for node level and up rather than verify that for each and every struct like uABI does. For that "compatible" seems just a perfect fit.
Quoted 11 lines
mm/memblock.c int reserve_mem_find_by_name(const char *name, phys_addr_t *start, phys_addr_t * · line 2472
Quoted 10 lines
mm/memblock.c +10
-0+if (err) { +pr_warn ("Node '%s/%s' has unknown compatible" , path, name); +continue ; + } + + mem =fdt_getprop (fdt, child,"mem" , &len); +if (!mem || len !=sizeof (*mem)) +continue ; + +if (mem->addr & (align -1 )) {
Quoted 8 lines
It's stated somewhere in this that the FDT data is LE, but here you are assuming the FDT is the same endianness as the CPU not that it's LE. Arm64 can do BE. PowerPC does both. I'm not sure if kexec from one endianness to another is possible. I would guess in theory it is and in practice it's broken already (because kexec is always an afterthought). Either you need to guarantee that native endianness will never be an issue for any arch or you need to make the endianness fixed.
I believe Alex mentioned little endian in the sense of native endianness for practical purposes :)
Since arm64 does seem to support kexec from one endianness to another in certain circumstances, but I believe that we can limit KHO only to work when both kernels have the same endianness.
Quoted 1 line
Rob
Signature · 2 lines
Sincerely yours, Mike.
Re: [PATCH v4 13/14] memblock: Add KHO support for reserve_mem
In reply to Mike RapoportQuoted 28 lines · Mike Rapoport
From: Alexander Graf <[email protected]>
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 <[email protected]>Co-developed-by Mike Rapoport (Microsoft) <[email protected]>Signed-off-by Mike Rapoport (Microsoft) <[email protected]>---
mm/memblock.c| 131 ++++++++++++++++++++++++++++++++++++++++++++++++++1 file changed, 131 insertions(+)mm/memblock.c +0
-0
mm/memblock.c line 19
Quoted 4 lines
mm/memblock.c +1
-0@@ -16,6 +16,9 @@#include <linux/kmemleak.h> #include <linux/seq_file.h> #include <linux/memblock.h> +#include <linux/kexec_handover.h>
Looks this one breaks the memblock test in tools/testing/memblock.
memblock.c:19:10: fatal error: linux/kexec_handover.h: No such file or directory
19 | #include <linux/kexec_handover.h>
| ^~~~~~~~~~~~~~~~~~~~~~~~
Quoted 3 lines
mm/memblock.c +2
-0+#include <linux/kexec.h> +#include <linux/libfdt.h>
Signature · 2 lines
Wei Yang Help you, Help me
Mike Rapoport
maintainerRe: [PATCH v4 13/14] memblock: Add KHO support for reserve_mem
Quoted 36 lines · Wei Yang
Quoted 28 lines · Mike Rapoport
From: Alexander Graf <[email protected]>
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 <[email protected]>Co-developed-by Mike Rapoport (Microsoft) <[email protected]>Signed-off-by Mike Rapoport (Microsoft) <[email protected]>---
mm/memblock.c| 131 ++++++++++++++++++++++++++++++++++++++++++++++++++1 file changed, 131 insertions(+)mm/memblock.c +0
-0mm/memblock.c line 19
Quoted 4 lines
mm/memblock.c +1
-0@@ -16,6 +16,9 @@#include <linux/kmemleak.h> #include <linux/seq_file.h> #include <linux/memblock.h> +#include <linux/kexec_handover.h> Looks this one breaks the memblock test in
tools/testing/memblock.
Quoted 3 lines
memblock.c:19:10: fatal error:
linux/kexec_handover.h: No such file or directory
19 | #include <linux/kexec_handover.h>| ^~~~~~~~~~~~~~~~~~~~~~~~
Thanks, will fix.
Quoted 7 lines
Quoted 3 lines
mm/memblock.c +2
-0+#include <linux/kexec.h> +#include <linux/libfdt.h> Signature · 2 lines
Wei Yang Help you, Help me
Signature · 2 lines
Sincerely yours, Mike.
Mike Rapoport
maintainerPATCH v4 14/14 Documentation: KHO: Add memblock bindings
In reply to Mike RapoportFrom: "Mike Rapoport (Microsoft)" <[email protected]>
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.
--- .../kho/bindings/memblock/reserve_mem.yaml | 41 ++++++++++++++++++ .../bindings/memblock/reserve_mem_map.yaml | 42 +++++++++++++++++++ 2 files changed, 83 insertions(+) create mode 100644 Documentation/kho/bindings/memblock/reserve_mem.yaml create mode 100644 Documentation/kho/bindings/memblock/reserve_mem_map.yaml
Documentation/kho/bindings/memblock/reserve_mem.yaml new file +41 -0
@@ -0,0 +1,41 @@ +# SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause) +%YAML 1.2 +--- +$id: http://devicetree.org/schemas/memblock/reserve_mem.yaml# +$schema: http://devicetree.org/meta-schemas/core.yaml# + +title: Memblock reserved memory + +maintainers: + - Mike Rapoport <[email protected]> + +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_map.yaml# + description: reserved memory regions + +required: + - compatible + +additionalProperties: false + +examples: + - | + reserve_mem { + compatible = "reserve_mem-v1"; + r1 { + compatible = "reserve_mem_map-v1"; + mem = <0xc07c 0x2000000 0x01 0x00>; + }; + };
Documentation/kho/bindings/memblock/reserve_mem_map.yaml new file +42 -0
42 lines not on this page · raw message
Signature · 1 line
2.47.2
Re: [PATCH v4 14/14] Documentation: KHO: Add memblock bindings
Quoted 7 lines · Mike Rapoport
From: "Mike Rapoport (Microsoft)" <[email protected]>
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.
Please use subject prefixes matching the subsystem. You can get them for example with git log --oneline -- DIRECTORY_OR_FILE on the directory your patch is touching. For bindings, the preferred subjects are explained here:
https://www.kernel.org/doc/html/latest/devicetree/bindings/submitting-patches.html#i-for-patch-submitters
Quoted 38 lines
As first user of KHO, we introduced memblock which can now preserve memory ranges reserved with
reserve_memcommand 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 <[email protected]>Signed-off-by Alexander Graf <[email protected]>Signed-off-by Mike Rapoport (Microsoft) <[email protected]>--- .../kho/bindings/memblock/reserve_mem.yaml | 41 ++++++++++++++++++ .../bindings/memblock/reserve_mem_map.yaml | 42 +++++++++++++++++++ 2 files changed, 83 insertions(+) create mode 100644 Documentation/kho/bindings/memblock/reserve_mem.yaml create mode 100644 Documentation/kho/bindings/memblock/reserve_mem_map.yamlDocumentation/kho/bindings/memblock/reserve_mem.yaml new file +11
-0@@ -0,0 +1,41 @@ +# SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause) +%YAML 1.2 +--- +$id: http://devicetree.org/schemas/memblock/reserve_mem.yaml# +$schema: http://devicetree.org/meta-schemas/core.yaml# + +title: Memblock reserved memory + +maintainers: + - Mike Rapoport <[email protected]> +
Documentation/kho/bindings/memblock/reserve_mem.yaml line 21
Quoted 10 lines
Documentation/kho/bindings/memblock/reserve_mem.yaml new file +10
-0+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
NAK, underscores are not allowed. Please follow carefully DTS coding style.
Documentation/kho/bindings/memblock/reserve_mem.yaml line 24
Quoted 3 lines
Documentation/kho/bindings/memblock/reserve_mem.yaml new file +3
-0+ +patternProperties: + "$[0-9a-f_]+^":
No underscores.
Documentation/kho/bindings/memblock/reserve_mem.yaml line 35
Quoted 11 lines
Documentation/kho/bindings/memblock/reserve_mem.yaml new file +11
-0+ $ref: reserve_mem_map.yaml# + description: reserved memory regions + +required: + - compatible + +additionalProperties: false + +examples: + - | + reserve_mem {
Again, do not introduce own coding style.
I don't understand why do you need this in the first place. There is already reserved-memory block.
Quoted 22 lines
Documentation/kho/bindings/memblock/reserve_mem.yaml new file +6
-0+ compatible = "reserve_mem-v1"; + r1 { + compatible = "reserve_mem_map-v1"; + mem = <0xc07c 0x2000000 0x01 0x00>; + }; + };Documentation/kho/bindings/memblock/reserve_mem_map.yaml new file +10
-0@@ -0,0 +1,42 @@ +# SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause) +%YAML 1.2 +--- +$id: http://devicetree.org/schemas/memblock/reserve_mem_map.yaml# +$schema: http://devicetree.org/meta-schemas/core.yaml# + +title: Memblock reserved memory regions + +maintainers: + - Mike Rapoport <[email protected]>
Documentation/kho/bindings/memblock/reserve_mem_map.yaml line 20
Quoted 10 lines
Documentation/kho/bindings/memblock/reserve_mem_map.yaml new file +10
-0+ +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_map-v1
Explain why you cannot use existing reserved memory bindings.
Best regards,
Krzysztof
Mike Rapoport
maintainerRe: [PATCH v4 14/14] Documentation: KHO: Add memblock bindings
Quoted 9 lines · Krzysztof Kozlowski
Quoted 7 lines · Mike Rapoport
From: "Mike Rapoport (Microsoft)" <[email protected]>
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.
Quoted 5 lines
Please use subject prefixes matching the subsystem. You can get them for example with
git log --oneline -- DIRECTORY_OR_FILEon the directory your patch is touching. For bindings, the preferred subjects are explained here:
https://www.kernel.org/doc/html/latest/devicetree/bindings/submitting-patches.html#i-for-patch-submitters
These are not devicetree binding for communicating data from firmware to the kernel. These bindings are specific to KHO which is perfectly reflected by the subject.
Just a brief reminder from v2 discussion:
(https://lore.kernel.org/linux-mm/[email protected]/)
"For quick reference: KHO is a new mechanism this patch set introduces which allows Linux to pass arbitrary memory and metadata between kernels on kexec. I'm reusing FDTs to implement the hand over protocol, as Linux-to-Linux boot communication holds very similar properties to firmware-to-Linux boot communication. So this binding is not about hardware; it's about preserving Linux subsystem state across kexec.
For more details, please refer to the KHO documentation which is part of patch 7 of this patch set:
https://lore.kernel.org/lkml/[email protected]/"
and
"This is our own data structure for KHO that just happens to again contain a DT structure. The reason is simple: I want a unified, versioned, introspectable data format that is cross platform so you don't need to touch every architecture specific boot passing logic every time you want to add a tiny piece of data."
Quoted 49 lines
Quoted 36 lines
As first user of KHO, we introduced memblock which can now preserve memory ranges reserved with
reserve_memcommand 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 <[email protected]>Signed-off-by Alexander Graf <[email protected]>Signed-off-by Mike Rapoport (Microsoft) <[email protected]>--- .../kho/bindings/memblock/reserve_mem.yaml | 41 ++++++++++++++++++ .../bindings/memblock/reserve_mem_map.yaml | 42 +++++++++++++++++++ 2 files changed, 83 insertions(+) create mode 100644 Documentation/kho/bindings/memblock/reserve_mem.yaml create mode 100644 Documentation/kho/bindings/memblock/reserve_mem_map.yamlDocumentation/kho/bindings/memblock/reserve_mem.yaml new file +10
-0@@ -0,0 +1,41 @@ +# SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause) +%YAML 1.2 +--- +$id: http://devicetree.org/schemas/memblock/reserve_mem.yaml# +$schema: http://devicetree.org/meta-schemas/core.yaml# + +title: Memblock reserved memory + +maintainers: + - Mike Rapoport <[email protected]>Documentation/kho/bindings/memblock/reserve_mem.yaml line 20
Quoted 10 lines
Documentation/kho/bindings/memblock/reserve_mem.yaml new file +10
-0+ +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. + +examples: + - | + reserve_mem {Again, do not introduce own coding style.
Quoted 2 lines
I don't understand why do you need this in the first place. There is already reserved-memory block.
Because these regions are not "... designed for the special usage by various device drivers" and should not be exclude by the operating system from normal usage.
Quoted 2 lines
Best regards,
Krzysztof
Signature · 2 lines
Sincerely yours, Mike.
Re: [PATCH v4 14/14] Documentation: KHO: Add memblock bindings
Quoted 16 lines · Mike Rapoport
Quoted 9 lines · Krzysztof Kozlowski
Quoted 7 lines · Mike Rapoport
From: "Mike Rapoport (Microsoft)" <[email protected]>
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.
Quoted 5 lines
Please use subject prefixes matching the subsystem. You can get them for example with
git log --oneline -- DIRECTORY_OR_FILEon the directory your patch is touching. For bindings, the preferred subjects are explained here:
https://www.kernel.org/doc/html/latest/devicetree/bindings/submitting-patches.html#i-for-patch-submitters
Quoted 3 lines
These are not devicetree binding for communicating data from firmware to the kernel. These bindings are specific to KHO which is perfectly reflected by the subject.
No, it is not. None of the bindings use above subject prefix.
Quoted 10 lines
Just a brief reminder from v2 discussion:
(https://lore.kernel.org/linux-mm/[email protected]/)"For quick reference: KHO is a new mechanism this patch set introduces which allows Linux to pass arbitrary memory and metadata between kernels on kexec. I'm reusing FDTs to implement the hand over protocol, as Linux-to-Linux boot communication holds very similar properties to firmware-to-Linux boot communication. So this binding is not about hardware; it's about preserving Linux subsystem state across kexec.
does not matter. You added file to ABI documentation so you must follow that ABI documentation rules. One rule is proper subject prefix.
Quoted 4 lines
For more details, please refer to the KHO documentation which is part of patch 7 of this patch set:
https://lore.kernel.org/lkml/[email protected]/"
I fail to see how this is related to the incorrect subject prefix as I pointed.
Quoted 58 lines
and
"This is our own data structure for KHO that just happens to again contain a DT structure. The reason is simple: I want a unified, versioned, introspectable data format that is cross platform so you don't need to touch every architecture specific boot passing logic every time you want to add a tiny piece of data."
Quoted 49 lines
Quoted 36 lines
As first user of KHO, we introduced memblock which can now preserve memory ranges reserved with
reserve_memcommand 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 <[email protected]>Signed-off-by Alexander Graf <[email protected]>Signed-off-by Mike Rapoport (Microsoft) <[email protected]>--- .../kho/bindings/memblock/reserve_mem.yaml | 41 ++++++++++++++++++ .../bindings/memblock/reserve_mem_map.yaml | 42 +++++++++++++++++++ 2 files changed, 83 insertions(+) create mode 100644 Documentation/kho/bindings/memblock/reserve_mem.yaml create mode 100644 Documentation/kho/bindings/memblock/reserve_mem_map.yamlDocumentation/kho/bindings/memblock/reserve_mem.yaml new file +10
-0@@ -0,0 +1,41 @@ +# SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause) +%YAML 1.2 +--- +$id: http://devicetree.org/schemas/memblock/reserve_mem.yaml# +$schema: http://devicetree.org/meta-schemas/core.yaml# + +title: Memblock reserved memory + +maintainers: + - Mike Rapoport <[email protected]>Documentation/kho/bindings/memblock/reserve_mem.yaml line 20
Quoted 10 lines
Documentation/kho/bindings/memblock/reserve_mem.yaml new file +10
-0+ +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. + +examples: + - | + reserve_mem {Again, do not introduce own coding style.
Quoted 5 lines
Quoted 2 lines
I don't understand why do you need this in the first place. There is already reserved-memory block.
Because these regions are not "... designed for the special usage by various device drivers"
So you use now very similar name, but different with few letters just to note that you do not fit into existing formats. If this does not fit existing usage, then use different name.
Quoted 2 lines
and should not be exclude by the operating system
from normal usage.
Then it does not look like a reserved memory
Quoted 4 lines
Quoted 3 lines
Best regards,
Krzysztof
Best regards,
Krzysztof
Mike Rapoport
maintainerRe: [PATCH v4 14/14] Documentation: KHO: Add memblock bindings
Quoted 34 lines · Krzysztof Kozlowski
Quoted 16 lines · Mike Rapoport
Quoted 9 lines · Krzysztof Kozlowski
Quoted 7 lines · Mike Rapoport
From: "Mike Rapoport (Microsoft)" <[email protected]>
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.
Quoted 5 lines
Please use subject prefixes matching the subsystem. You can get them for example with
git log --oneline -- DIRECTORY_OR_FILEon the directory your patch is touching. For bindings, the preferred subjects are explained here:
https://www.kernel.org/doc/html/latest/devicetree/bindings/submitting-patches.html#i-for-patch-submittersQuoted 3 lines
These are not devicetree binding for communicating data from firmware to the kernel. These bindings are specific to KHO which is perfectly reflected by the subject.
No, it is not. None of the bindings use above subject prefix.
Quoted 10 lines
Just a brief reminder from v2 discussion:
(https://lore.kernel.org/linux-mm/[email protected]/)"For quick reference: KHO is a new mechanism this patch set introduces which allows Linux to pass arbitrary memory and metadata between kernels on kexec. I'm reusing FDTs to implement the hand over protocol, as Linux-to-Linux boot communication holds very similar properties to firmware-to-Linux boot communication. So this binding is not about hardware; it's about preserving Linux subsystem state across kexec.
Quoted 2 lines
does not matter. You added file to ABI documentation so you must follow that ABI documentation rules. One rule is proper subject prefix.
No, it does not. It's a different ABI.
FDT is a _data structure_ that provides cross platform unified, versioned, introspectable data format.
Documentation/devicetree/bindings standardizes it's use for describing hardware, but KHO uses FDT _data structure_ to describe state of the kernel components that will be reused by the kexec'ed kernel.
KHO is a different namespace from Open Firmware Device Tree, with different requirements and different stakeholders. Putting descriptions of KHO data formats in Documentation/kho rather than in Documentation/devicetree/bindings was not done to evade review of Open Firmware Device Tree maintainers, but rather to emphasize that KHO FDT _is not_ Open Firmware Device Tree.
Quoted 2 lines
Best regards,
Krzysztof
Signature · 2 lines
Sincerely yours, Mike.
Re: [PATCH v4 14/14] Documentation: KHO: Add memblock bindings
Quoted 47 lines · Mike Rapoport
Quoted 34 lines · Krzysztof Kozlowski
Quoted 16 lines · Mike Rapoport
Quoted 9 lines · Krzysztof Kozlowski
Quoted 7 lines · Mike Rapoport
From: "Mike Rapoport (Microsoft)" <[email protected]>
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.
Quoted 5 lines
Please use subject prefixes matching the subsystem. You can get them for example with
git log --oneline -- DIRECTORY_OR_FILEon the directory your patch is touching. For bindings, the preferred subjects are explained here:
https://www.kernel.org/doc/html/latest/devicetree/bindings/submitting-patches.html#i-for-patch-submittersQuoted 3 lines
These are not devicetree binding for communicating data from firmware to the kernel. These bindings are specific to KHO which is perfectly reflected by the subject.
No, it is not. None of the bindings use above subject prefix.
Quoted 10 lines
Just a brief reminder from v2 discussion:
(https://lore.kernel.org/linux-mm/[email protected]/)"For quick reference: KHO is a new mechanism this patch set introduces which allows Linux to pass arbitrary memory and metadata between kernels on kexec. I'm reusing FDTs to implement the hand over protocol, as Linux-to-Linux boot communication holds very similar properties to firmware-to-Linux boot communication. So this binding is not about hardware; it's about preserving Linux subsystem state across kexec.
Quoted 2 lines
does not matter. You added file to ABI documentation so you must follow that ABI documentation rules. One rule is proper subject prefix.
No, it does not. It's a different ABI.
FDT is a
_datastructure_that provides cross platform unified, versioned, introspectable data format.
Documentation/devicetree/bindingsstandardizes it's use for describing hardware, but KHO uses FDT_datastructure_to describe state of the kernel components that will be reused by the kexec'ed kernel.
Quoted 6 lines
KHO is a different namespace from Open Firmware Device Tree, with different requirements and different stakeholders. Putting descriptions of KHO data formats in
Documentation/khorather than inDocumentation/devicetree/bindingswas not done to evade review of Open Firmware Device Tree maintainers, but rather to emphasize that KHO FDT_isnot_Open Firmware Device Tree.
Ah, neat, that would almost solve the problem but you wrote:
+$id: http://devicetree.org/schemas/memblock/reserve_mem.yaml#
+$schema: http://devicetree.org/meta-schemas/core.yaml#
so no, this does not work like that. You use devicetree here namespace and ignore its rules.
You cannot pretend this is not devicetree if you put it into devicetree schemas.
Best regards,
Krzysztof
Re: [PATCH v4 14/14] Documentation: KHO: Add memblock bindings
Quoted 38 lines · Krzysztof Kozlowski
Quoted 30 lines
Quoted 18 lines
Quoted 4 lines
These are not devicetree binding for communicating data from firmware to the kernel. These bindings are specific to KHO which is perfectly reflected by the subject.
No, it is not. None of the bindings use above subject prefix.
Quoted 10 lines
Just a brief reminder from v2 discussion:
(https://lore.kernel.org/linux-mm/[email protected]/)"For quick reference: KHO is a new mechanism this patch set introduces which allows Linux to pass arbitrary memory and metadata between kernels on kexec. I'm reusing FDTs to implement the hand over protocol, as Linux-to-Linux boot communication holds very similar properties to firmware-to-Linux boot communication. So this binding is not about hardware; it's about preserving Linux subsystem state across kexec.
Quoted 2 lines
does not matter. You added file to ABI documentation so you must follow that ABI documentation rules. One rule is proper subject prefix.
No, it does not. It's a different ABI.
FDT is a
_datastructure_that provides cross platform unified, versioned, introspectable data format.
Documentation/devicetree/bindingsstandardizes it's use for describing hardware, but KHO uses FDT_datastructure_to describe state of the kernel components that will be reused by the kexec'ed kernel.Quoted 6 lines
KHO is a different namespace from Open Firmware Device Tree, with different requirements and different stakeholders. Putting descriptions of KHO data formats in
Documentation/khorather than inDocumentation/devicetree/bindingswas not done to evade review of Open Firmware Device Tree maintainers, but rather to emphasize that KHO FDT_isnot_Open Firmware Device Tree.
Quoted 7 lines
Ah, neat, that would almost solve the problem but you wrote:
+$id: http://devicetree.org/schemas/memblock/reserve_mem.yaml#
+$schema: http://devicetree.org/meta-schemas/core.yaml#so no, this does not work like that. You use devicetree here namespace and ignore its rules.
... and that obviously is barely parseable, so maybe one more try:
"You use here devicetree namespace but ignore its rules."
Quoted 3 lines
You cannot pretend this is not devicetree if you put it into devicetree schemas.
Best regards,
Krzysztof
Re: [PATCH v4 14/14] Documentation: KHO: Add memblock bindings
Quoted 10 lines · Krzysztof Kozlowski
Quoted 7 lines
Ah, neat, that would almost solve the problem but you wrote:
+$id: http://devicetree.org/schemas/memblock/reserve_mem.yaml#
+$schema: http://devicetree.org/meta-schemas/core.yaml#so no, this does not work like that. You use devicetree here namespace and ignore its rules.
... and that obviously is barely parseable, so maybe one more try:
"You use here devicetree namespace but ignore its rules."
It makes sense to me, there should be zero cross-over of the two specs, KHO should be completely self defined and stand alone.
There is some documentation missing, I think. This yaml describes one node type, but the entire overall structure of the fdt does not seem to have documentation?
Jason
Re: [PATCH v4 14/14] Documentation: KHO: Add memblock bindings
Quoted 12 lines · Jason Gunthorpe
Quoted 10 lines · Krzysztof Kozlowski
Quoted 7 lines
Ah, neat, that would almost solve the problem but you wrote:
+$id: http://devicetree.org/schemas/memblock/reserve_mem.yaml#
+$schema: http://devicetree.org/meta-schemas/core.yaml#so no, this does not work like that. You use devicetree here namespace and ignore its rules.
... and that obviously is barely parseable, so maybe one more try:
"You use here devicetree namespace but ignore its rules."
Quoted 6 lines
It makes sense to me, there should be zero cross-over of the two specs, KHO should be completely self defined and stand alone.
There is some documentation missing, I think. This yaml describes one node type, but the entire overall structure of the fdt does not seem to have documentation?
A lot of ABI is missing there and undocumented like: node name (which for every standard DT would be a NAK), few properties. This binding describes only subset while skipping all the rest and effectively introducing implied/undocumented ABI.
Best regards,
Krzysztof
Re: [PATCH v4 14/14] Documentation: KHO: Add memblock bindings
Quoted 16 lines · Krzysztof Kozlowski
Quoted 15 lines · Jason Gunthorpe
Quoted 10 lines · Krzysztof Kozlowski
Quoted 7 lines
Ah, neat, that would almost solve the problem but you wrote:
+$id: http://devicetree.org/schemas/memblock/reserve_mem.yaml#
+$schema: http://devicetree.org/meta-schemas/core.yaml#so no, this does not work like that. You use devicetree here namespace and ignore its rules.
... and that obviously is barely parseable, so maybe one more try:
"You use here devicetree namespace but ignore its rules."It makes sense to me, there should be zero cross-over of the two specs, KHO should be completely self defined and stand alone.
Quoted 8 lines
Quoted 3 lines
There is some documentation missing, I think. This yaml describes one node type, but the entire overall structure of the fdt does not seem to have documentation?
A lot of ABI is missing there and undocumented like: node name (which for every standard DT would be a NAK), few properties. This binding describes only subset while skipping all the rest and effectively introducing implied/undocumented ABI.
I agree, I think it can be easily adressed - the docs should have a sample of the overal DT from the root node and yaml for each of the blocks, laying out the purpose very much like the open dt spec..
Jason
Mike Rapoport
maintainerRe: [PATCH v4 14/14] Documentation: KHO: Add memblock bindings
Quoted 21 lines · Jason Gunthorpe
Quoted 20 lines · Krzysztof Kozlowski
Quoted 18 lines · Jason Gunthorpe
Quoted 10 lines · Krzysztof Kozlowski
Quoted 7 lines
Ah, neat, that would almost solve the problem but you wrote:
+$id: http://devicetree.org/schemas/memblock/reserve_mem.yaml#
+$schema: http://devicetree.org/meta-schemas/core.yaml#so no, this does not work like that. You use devicetree here namespace and ignore its rules.
... and that obviously is barely parseable, so maybe one more try:
"You use here devicetree namespace but ignore its rules."It makes sense to me, there should be zero cross-over of the two specs, KHO should be completely self defined and stand alone.
There is some documentation missing, I think. This yaml describes one node type, but the entire overall structure of the fdt does not seem to have documentation?
Quoted 8 lines
Quoted 4 lines
A lot of ABI is missing there and undocumented like: node name (which for every standard DT would be a NAK), few properties. This binding describes only subset while skipping all the rest and effectively introducing implied/undocumented ABI.
I agree, I think it can be easily adressed - the docs should have a sample of the overal DT from the root node and yaml for each of the blocks, laying out the purpose very much like the open dt spec..
I'll update the docs with more details about overall structure and will make it clear that it's a different namespace.
Quoted 1 line
Jason
Signature · 2 lines
Sincerely yours, Mike.
Andrew Morton
maintainerQuoted 5 lines · On Thu, 6 Feb 2025 15:27:40 +0200 Mike Rapoport <[email protected]> wrote:
This a next version of Alex's "kexec: Allow preservation of ftrace buffers" series (https://lore.kernel.org/all/[email protected]),
just to make things simpler instead of ftrace we decided to preserve "reserve_mem" regions.
Quoted 7 lines
The patches are also available in git:
https://git.kernel.org/rppt/h/kho/v4Kexec 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.
I tossed this into mm.git for some testing and exposure.
What merge path are you anticipating?
Review activity seems pretty thin thus far?
Pasha Tatashin
maintainerQuoted 16 lines · Andrew Morton
Quoted 5 lines · On Thu, 6 Feb 2025 15:27:40 +0200 Mike Rapoport <[email protected]> wrote:
This a next version of Alex's "kexec: Allow preservation of ftrace buffers" series (https://lore.kernel.org/all/[email protected]),
just to make things simpler instead of ftrace we decided to preserve "reserve_mem" regions.Quoted 7 lines
The patches are also available in git:
https://git.kernel.org/rppt/h/kho/v4Kexec 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.
Quoted 5 lines
I tossed this into mm.git for some testing and exposure.
What merge path are you anticipating?
Review activity seems pretty thin thus far?
KHO is going to be discussed at the upcoming lsfmm, we are also planning to send v5 of this patch series (discussed with Mike Rapoport) in a couple of weeks. It will include enhancements needed for the hypervisor live update scenario:
1. Allow nodes to be added to the KHO tree at any time
2. Remove "activate" (I will also send a live update framework that provides the activate functionality).
3. Allow serialization during shutdown.
4. Decouple KHO from kexec_file_load(), as kexec_file_load() should not be used during live update blackout time.
5. Enable multithreaded serialization by using hash-table as an intermediate step before conversion to FDT.
Pasha
Quoted 19 lines · Pasha Tatashin
Quoted 18 lines · Andrew Morton
Quoted 5 lines · On Thu, 6 Feb 2025 15:27:40 +0200 Mike Rapoport <[email protected]> wrote:
This a next version of Alex's "kexec: Allow preservation of ftrace buffers" series (https://lore.kernel.org/all/[email protected]),
just to make things simpler instead of ftrace we decided to preserve "reserve_mem" regions.Quoted 7 lines
The patches are also available in git:
https://git.kernel.org/rppt/h/kho/v4Kexec 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.
I tossed this into mm.git for some testing and exposure.
Quoted 8 lines
Quoted 3 lines
What merge path are you anticipating?
Review activity seems pretty thin thus far?
KHO is going to be discussed at the upcoming lsfmm, we are also planning to send v5 of this patch series (discussed with Mike Rapoport) in a couple of weeks. It will include enhancements needed for the hypervisor live update scenario:
So is this V4 still a RFC if v5 will be sent by plan? Should we hold the reviewing until v5? Or this series is a infrustructure building, v5 will add more details as you listed as below. I am a little confused.
Quoted 9 lines
1. Allow nodes to be added to the KHO tree at any time
2. Remove "activate" (I will also send a live update framework that provides the activate functionality).
3. Allow serialization during shutdown.
4. Decouple KHO fromkexec_file_load(), askexec_file_load()should not be used during live update blackout time.
5. Enable multithreaded serialization by using hash-table as an intermediate step before conversion to FDT.
Mike Rapoport
maintainerHi Baoquan,
Quoted 24 lines · Baoquan He
Quoted 23 lines · Pasha Tatashin
Quoted 21 lines · Andrew Morton
Quoted 5 lines · On Thu, 6 Feb 2025 15:27:40 +0200 Mike Rapoport <[email protected]> wrote:
This a next version of Alex's "kexec: Allow preservation of ftrace buffers" series (https://lore.kernel.org/all/[email protected]),
just to make things simpler instead of ftrace we decided to preserve "reserve_mem" regions.Quoted 7 lines
The patches are also available in git:
https://git.kernel.org/rppt/h/kho/v4Kexec 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.
I tossed this into mm.git for some testing and exposure.
What merge path are you anticipating?
Review activity seems pretty thin thus far?
Quoted 8 lines
Quoted 4 lines
KHO is going to be discussed at the upcoming lsfmm, we are also planning to send v5 of this patch series (discussed with Mike Rapoport) in a couple of weeks. It will include enhancements needed for the hypervisor live update scenario:
So is this V4 still a RFC if v5 will be sent by plan? Should we hold the reviewing until v5? Or this series is a infrustructure building, v5 will add more details as you listed as below. I am a little confused.
v4 adds the very basic support for kexec handover in the simplest form we could think of. There were discussions on Linux MM Alignment and Hypervisor live update meetings and there people agreed about MVP for KHO that v4 essentially implements.
v5 will add more details on top of v4 and I'm not sure there's a consensus about some of them among the people involved in KHO.
Quoted 9 lines
Quoted 8 lines
1. Allow nodes to be added to the KHO tree at any time
2. Remove "activate" (I will also send a live update framework that provides the activate functionality).
3. Allow serialization during shutdown.
4. Decouple KHO fromkexec_file_load(), askexec_file_load()should not be used during live update blackout time.
5. Enable multithreaded serialization by using hash-table as an intermediate step before conversion to FDT.
Signature · 2 lines
Sincerely yours, Mike.
Quoted 36 lines · Mike Rapoport
Hi Baoquan,
Quoted 24 lines · Baoquan He
Quoted 23 lines · Pasha Tatashin
Quoted 21 lines · Andrew Morton
Quoted 5 lines · On Thu, 6 Feb 2025 15:27:40 +0200 Mike Rapoport <[email protected]> wrote:
This a next version of Alex's "kexec: Allow preservation of ftrace buffers" series (https://lore.kernel.org/all/[email protected]),
just to make things simpler instead of ftrace we decided to preserve "reserve_mem" regions.Quoted 7 lines
The patches are also available in git:
https://git.kernel.org/rppt/h/kho/v4Kexec 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.
I tossed this into mm.git for some testing and exposure.
What merge path are you anticipating?
Review activity seems pretty thin thus far?
Quoted 8 lines
Quoted 4 lines
KHO is going to be discussed at the upcoming lsfmm, we are also planning to send v5 of this patch series (discussed with Mike Rapoport) in a couple of weeks. It will include enhancements needed for the hypervisor live update scenario:
So is this V4 still a RFC if v5 will be sent by plan? Should we hold the reviewing until v5? Or this series is a infrustructure building, v5 will add more details as you listed as below. I am a little confused.
Quoted 7 lines
v4 adds the very basic support for kexec handover in the simplest form we could think of. There were discussions on Linux MM Alignment and Hypervisor live update meetings and there people agreed about MVP for KHO that v4 essentially implements.
v5 will add more details on top of v4 and I'm not sure there's a consensus about some of them among the people involved in KHO.
Thanks for the information.
Then I will apply v4 and learn the infrastructure and mechanism firstly.
While what sounds more meaningful to me is v4 can be reviewed, then updated and merged. Then another patchset can be posted to add details, if you have reached the consensus on the infrastructure part. With that, posting and reviewing will be much easier. Unless you guys are still discussing the infrastructure part.
Quoted 15 lines
Quoted 10 lines
Quoted 9 lines
1. Allow nodes to be added to the KHO tree at any time
2. Remove "activate" (I will also send a live update framework that provides the activate functionality).
3. Allow serialization during shutdown.
4. Decouple KHO fromkexec_file_load(), askexec_file_load()should not be used during live update blackout time.
5. Enable multithreaded serialization by using hash-table as an intermediate step before conversion to FDT.Signature · 2 lines
Sincerely yours, Mike.
Pasha Tatashin
maintainerQuoted 25 lines · Baoquan He
Quoted 23 lines · Pasha Tatashin
Quoted 21 lines · Andrew Morton
Quoted 5 lines · On Thu, 6 Feb 2025 15:27:40 +0200 Mike Rapoport <[email protected]> wrote:
This a next version of Alex's "kexec: Allow preservation of ftrace buffers" series (https://lore.kernel.org/all/[email protected]),
just to make things simpler instead of ftrace we decided to preserve "reserve_mem" regions.Quoted 7 lines
The patches are also available in git:
https://git.kernel.org/rppt/h/kho/v4Kexec 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.
I tossed this into mm.git for some testing and exposure.
What merge path are you anticipating?
Review activity seems pretty thin thus far?
Quoted 8 lines
Quoted 4 lines
KHO is going to be discussed at the upcoming lsfmm, we are also planning to send v5 of this patch series (discussed with Mike Rapoport) in a couple of weeks. It will include enhancements needed for the hypervisor live update scenario:
So is this V4 still a RFC if v5 will be sent by plan? Should we hold the reviewing until v5? Or this series is a infrustructure building, v5 will add more details as you listed as below. I am a little confused.
We will modify the existing patches and send as v5 because some interfaces are going to be changed*.
Otherwise, v5 will make KHO a lot more flexible as it will allow to use the tree all the time while the system is running instead of only once during the activation phase.
* Changing interfaces is optional, but decision whether to change will be discussed at Hypervisor Live Update on Feb 10th:
https://lore.kernel.org/all/[email protected]/
Quoted 11 lines
Quoted 10 lines
1. Allow nodes to be added to the KHO tree at any time
2. Remove "activate" (I will also send a live update framework that provides the activate functionality).
3. Allow serialization during shutdown.
4. Decouple KHO fromkexec_file_load(), askexec_file_load()should not be used during live update blackout time.
5. Enable multithreaded serialization by using hash-table as an intermediate step before conversion to FDT.
Quoted 38 lines · Pasha Tatashin
Quoted 25 lines · Baoquan He
Quoted 23 lines · Pasha Tatashin
Quoted 21 lines · Andrew Morton
Quoted 5 lines · On Thu, 6 Feb 2025 15:27:40 +0200 Mike Rapoport <[email protected]> wrote:
This a next version of Alex's "kexec: Allow preservation of ftrace buffers" series (https://lore.kernel.org/all/[email protected]),
just to make things simpler instead of ftrace we decided to preserve "reserve_mem" regions.Quoted 7 lines
The patches are also available in git:
https://git.kernel.org/rppt/h/kho/v4Kexec 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.
I tossed this into mm.git for some testing and exposure.
What merge path are you anticipating?
Review activity seems pretty thin thus far?
Quoted 8 lines
Quoted 4 lines
KHO is going to be discussed at the upcoming lsfmm, we are also planning to send v5 of this patch series (discussed with Mike Rapoport) in a couple of weeks. It will include enhancements needed for the hypervisor live update scenario:
So is this V4 still a RFC if v5 will be sent by plan? Should we hold the reviewing until v5? Or this series is a infrustructure building, v5 will add more details as you listed as below. I am a little confused.
We will modify the existing patches and send as v5 because some interfaces are going to be changed*.
Quoted 7 lines
Otherwise, v5 will make KHO a lot more flexible as it will allow to use the tree all the time while the system is running instead of only once during the activation phase.
* Changing interfaces is optional, but decision whether to change will be discussed at Hypervisor Live Update on Feb 10th:
https://lore.kernel.org/all/[email protected]/
Ah, this is what I would like to know about the difference between v4 and v5. Thanks for the information, and looking forward to seeing the v5 update.
Quoted 13 lines
Quoted 12 lines
Quoted 11 lines
1. Allow nodes to be added to the KHO tree at any time
2. Remove "activate" (I will also send a live update framework that provides the activate functionality).
3. Allow serialization during shutdown.
4. Decouple KHO fromkexec_file_load(), askexec_file_load()should not be used during live update blackout time.
5. Enable multithreaded serialization by using hash-table as an intermediate step before conversion to FDT.
Mike Rapoport
maintainerQuoted 11 lines · Andrew Morton
Quoted 9 lines · On Thu, 6 Feb 2025 15:27:40 +0200 Mike Rapoport <[email protected]> wrote:
This a next version of Alex's "kexec: Allow preservation of ftrace buffers" series (https://lore.kernel.org/all/[email protected]),
just to make things simpler instead of ftrace we decided to preserve "reserve_mem" regions.The patches are also available in git:
https://git.kernel.org/rppt/h/kho/v4
Quoted 7 lines
Quoted 3 lines
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.
I tossed this into mm.git for some testing and exposure.
What merge path are you anticipating?
I think your tree is the most appropriate, but let's wait for Acks from x86 and arm64 people ;-)
Quoted 1 line
Review activity seems pretty thin thus far?
Yeah :(
Maybe with Pasha's version on top of that we'll have more people reviewing.
And here is another fixup for a sparse error kbuild reported:
From e1e34b96b96b89a01ee31be223c8dfc2ce1c4cbe Mon Sep 17 00:00:00 2001
From: "Mike Rapoport (Microsoft)" <[email protected]>
Date: Fri, 7 Feb 2025 09:55:03 +0200
Subject: [PATCH] kho: make bin_attr_dt_kern static
--- kernel/kexec_handover.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
kernel/kexec_handover.c +1 -1
@@ -258,7 +258,7 @@ static ssize_t dt_read(struct file *file, struct kobject *kobj,return count; } -struct bin_attribute bin_attr_dt_kern =__BIN_ATTR (dt,0400 , dt_read,NULL ,0 ); +static struct bin_attribute bin_attr_dt_kern =__BIN_ATTR (dt,0400 , dt_read,NULL ,0 );static int kho_expose_dt (void *fdt) {
Signature · 6 lines
2.47.2 -- Sincerely yours, Mike.
Quoted 15 lines · Andrew Morton
Quoted 5 lines · On Thu, 6 Feb 2025 15:27:40 +0200 Mike Rapoport <[email protected]> wrote:
This a next version of Alex's "kexec: Allow preservation of ftrace buffers" series (https://lore.kernel.org/all/[email protected]),
just to make things simpler instead of ftrace we decided to preserve "reserve_mem" regions.Quoted 7 lines
The patches are also available in git:
https://git.kernel.org/rppt/h/kho/v4Kexec 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.
Quoted 5 lines
I tossed this into mm.git for some testing and exposure.
What merge path are you anticipating?
Review activity seems pretty thin thus far?
At least for DT ABI because:
1. For some reason this escaped Patchwork. Maybe was blocked by spam filters, maybe Cc list is too big. No clue.
2. In the same time fallback to Patchwork was avoided by:
Cc-ing wrong address and not using expected (see git log) subject prefixes.
Best regards,
Krzysztof
Andrew Morton
maintainerMy x86_64 allmodconfig sayeth:
WARNING: modpost: vmlinux: section mismatch in reference: kho_reserve_scratch+0xca (section: .text) -> memblock_alloc_try_nid (section: .init.text)
WARNING: modpost: vmlinux: section mismatch in reference: kho_reserve_scratch+0xf5 (section: .text) -> scratch_scale (section: .init.data)
WARNING: modpost: vmlinux: section mismatch in reference: kho_reserve_scratch+0x100 (section: .text) -> scratch_size_global (section: .init.data)
WARNING: modpost: vmlinux: section mismatch in reference: kho_reserve_scratch+0x11d (section: .text) -> scratch_size_global (section: .init.data)
WARNING: modpost: vmlinux: section mismatch in reference: kho_reserve_scratch+0x129 (section: .text) -> scratch_size_pernode (section: .init.data)
WARNING: modpost: vmlinux: section mismatch in reference: kho_reserve_scratch+0x14e (section: .text) -> memblock_phys_alloc_range (section: .init.text)
WARNING: modpost: vmlinux: section mismatch in reference: kho_reserve_scratch+0x261 (section: .text) -> scratch_size_pernode (section: .init.data)
WARNING: modpost: vmlinux: section mismatch in reference: kho_reserve_scratch+0x26d (section: .text) -> scratch_size_pernode (section: .init.data)
WARNING: modpost: vmlinux: section mismatch in reference: kho_reserve_scratch+0x29b (section: .text) -> memblock_alloc_range_nid (section: .init.text)
WARNING: modpost: vmlinux: section mismatch in reference: kho_reserve_scratch+0x334 (section: .text) -> scratch_scale (section: .init.data)
WARNING: modpost: vmlinux: section mismatch in reference: kho_reserve_scratch+0x33f (section: .text) -> scratch_size_global (section: .init.data)
WARNING: modpost: vmlinux: section mismatch in reference: kho_reserve_scratch+0x363 (section: .text) -> scratch_scale (section: .init.data)
WARNING: modpost: vmlinux: section mismatch in reference: kho_reserve_scratch+0x371 (section: .text) -> scratch_size_global (section: .init.data)
WARNING: modpost: vmlinux: section mismatch in reference: kho_reserve_scratch+0x3a1 (section: .text) -> scratch_scale (section: .init.data)
WARNING: modpost: vmlinux: section mismatch in reference: kho_reserve_scratch+0x3af (section: .text) -> scratch_size_global (section: .init.data)
Mike Rapoport
maintainerQuoted 4 lines · Andrew Morton
My
x86_64allmodconfig sayeth:WARNING: modpost: vmlinux: section mismatch in reference:
kho_reserve_scratch+0xca(section: .text) ->memblock_alloc_try_nid(section: .init.text)
WARNING: modpost: vmlinux: section mismatch in reference:kho_reserve_scratch+0xf5(section: .text) ->scratch_scale(section: .init.data)
This should fix it:
From 176767698d4ac5b7cddffe16677b60cb18dce786 Mon Sep 17 00:00:00 2001
From: "Mike Rapoport (Microsoft)" <[email protected]>
Date: Fri, 7 Feb 2025 09:57:09 +0200
Subject: [PATCH] kho: make kho_reserve_scratch and kho_init_reserved_pages
__init
--- kernel/kexec_handover.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
kernel/kexec_handover.c +2 -2
@@ -620,7 +620,7 @@ static phys_addr_t __init scratch_size(int nid)* 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 kho_reserve_scratch (void ) +static void __init kho_reserve_scratch (void ) {phys_addr_t addr, size;int nid, i =1 ;
@@ -672,7 +672,7 @@ static void kho_reserve_scratch(void)* Scan the DT for any memory ranges and make sure they are reserved in * memblock, otherwise they will end up in a weird state on free lists. */ -static void kho_init_reserved_pages (void ) +static void __init kho_init_reserved_pages (void ) {const void *fdt =kho_get_fdt ();int offset =0 , depth =0 , initial_depth =0 , len;
Signature · 6 lines
2.47.2 -- Sincerely yours, Mike.
Hi Mike,
Quoted 31 lines · Mike Rapoport
From: "Mike Rapoport (Microsoft)" <[email protected]>
Hi,
This a next version of Alex's "kexec: Allow preservation of ftrace buffers" series (https://lore.kernel.org/all/[email protected]),
just to make things simpler instead of ftrace we decided to preserve "reserve_mem" regions.The patches are also available in git:
https://git.kernel.org/rppt/h/kho/v4Kexec 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:
Quoted 7 lines
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.
Nice work!
One concern there is that using memblock to reserve memory as crashkernel= is not flexible. I worked on kdump years ago and one of the biggest pains of kdump is how much memory should be reserved with crashkernel=. And it is still a pain today.
If we reserve more, that would mean more waste for the 1st kernel. If we reserve less, that would induce more OOM for the 2nd kernel.
I'd suggest considering using CMA, where the "reserved" memory can be still reusable for other purposes, just that pages can be migrated out of this reserved region on demand, that is, when loading a kexec kernel. Of course, we need to make sure they are not reused by what you want to preserve here, e.g., IOMMU. So you might need additional work to make it work, but still I believe this is the right direction.
Just my two cents.
Thanks!
Pasha Tatashin
maintainerQuoted 53 lines · Cong Wang
Hi Mike,
Quoted 31 lines · Mike Rapoport
From: "Mike Rapoport (Microsoft)" <[email protected]>
Hi,
This a next version of Alex's "kexec: Allow preservation of ftrace buffers" series (https://lore.kernel.org/all/[email protected]),
just to make things simpler instead of ftrace we decided to preserve "reserve_mem" regions.The patches are also available in git:
https://git.kernel.org/rppt/h/kho/v4Kexec 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:
Quoted 7 lines
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.Nice work!
One concern there is that using memblock to reserve memory as crashkernel= is not flexible. I worked on kdump years ago and one of the biggest pains of kdump is how much memory should be reserved with crashkernel=. And it is still a pain today.
If we reserve more, that would mean more waste for the 1st kernel. If we reserve less, that would induce more OOM for the 2nd kernel.
Quoted 6 lines
I'd suggest considering using CMA, where the "reserved" memory can be still reusable for other purposes, just that pages can be migrated out of this reserved region on demand, that is, when loading a kexec kernel. Of course, we need to make sure they are not reused by what you want to preserve here, e.g., IOMMU. So you might need additional work to make it work, but still I believe this is the right direction.
This is exactly what scratch memory is used for. Unlike crashkernel=, the entire scratch area is available to user applications as CMA, as we know that no kernel-reserved memory will come from that area. This doesn't work for crashkernel=, because in some cases, the user pages might also need to be preserved in the crash dump. However, if user pages are going to be discarded from the crash dump (as is done 99% of the time), then it is better to also make it use CMA or ZONE_MOVABLE and use only the memory occupied by the crash kernel and do not waste any memory at all. We have an internal patch at Google that does this, and I think it would be a good improvement for the upstream kernel to carry as well.
Pasha
Quoted 4 lines
Just my two cents.
Thanks!
Quoted 62 lines · Pasha Tatashin
Quoted 53 lines · Cong Wang
Hi Mike,
Quoted 31 lines · Mike Rapoport
From: "Mike Rapoport (Microsoft)" <[email protected]>
Hi,
This a next version of Alex's "kexec: Allow preservation of ftrace buffers" series (https://lore.kernel.org/all/[email protected]),
just to make things simpler instead of ftrace we decided to preserve "reserve_mem" regions.The patches are also available in git:
https://git.kernel.org/rppt/h/kho/v4Kexec 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:
Quoted 7 lines
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.Nice work!
One concern there is that using memblock to reserve memory as crashkernel= is not flexible. I worked on kdump years ago and one of the biggest pains of kdump is how much memory should be reserved with crashkernel=. And it is still a pain today.
If we reserve more, that would mean more waste for the 1st kernel. If we reserve less, that would induce more OOM for the 2nd kernel.
Quoted 6 lines
I'd suggest considering using CMA, where the "reserved" memory can be still reusable for other purposes, just that pages can be migrated out of this reserved region on demand, that is, when loading a kexec kernel. Of course, we need to make sure they are not reused by what you want to preserve here, e.g., IOMMU. So you might need additional work to make it work, but still I believe this is the right direction.
Quoted 11 lines
This is exactly what scratch memory is used for. Unlike crashkernel=, the entire scratch area is available to user applications as CMA, as we know that no kernel-reserved memory will come from that area. This doesn't work for crashkernel=, because in some cases, the user pages might also need to be preserved in the crash dump. However, if user pages are going to be discarded from the crash dump (as is done 99% of the time), then it is better to also make it use CMA or
ZONE_MOVABLEand use only the memory occupied by the crash kernel and do not waste any memory at all. We have an internal patch at Google that does this, and I think it would be a good improvement for the upstream kernel to carry as well.
Good to know CMA is already used, I could not tell from the cover letter.
The case that user-space pages need to be preserved is for scenarios like RDMA which pins user-space pages for DMA transfer. Since the goal here is also to preserve hardware states like RDMA's I guess the same concern remains.
Thanks!
Hi Mike,
Quoted 5 lines · Mike Rapoport
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. When the root user enables KHO through
/sys/kernel/kho/active, the kernel invokes callbacks to every driver that supports KHO to serialize
Quoted 8 lines
its state. When the actual kexec happens, the fdt is part of the image set that we boot into. In addition, we keep a "scratch regions" available for kexec: A physically contiguous memory regions that is 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 and recover their state from it. This includes memory reservations, where the driver can either discard or claim reservations.
I have gone through your entire patchset, if you could provide an example of a specific driver that supports KHO it would help a lot for people to understand and more importantly help driver developers to adopt. Even with a simulated driver, e.g. netdevsim, it would be greatly helpful.
Thanks.
Quoted 79 lines · Mike Rapoport
From: "Mike Rapoport (Microsoft)" <[email protected]>
Hi,
This a next version of Alex's "kexec: Allow preservation of ftrace buffers" series (https://lore.kernel.org/all/[email protected]),
just to make things simpler instead of ftrace we decided to preserve "reserve_mem" regions.The patches are also available in git:
https://git.kernel.org/rppt/h/kho/v4Kexec 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 lineAll 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. When the root user enables KHO through
/sys/kernel/kho/active, the kernel invokes callbacks to every driver that supports KHO to serialize its state. When the actual kexec happens, the fdt is part of the image set that we boot into. In addition, we keep a "scratch regions" available for kexec: A physically contiguous memory regions that is 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 and recover their state from it. This includes memory reservations, where the driver can either discard or claim reservations.
Quoted 6 lines
== 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.
What architecture exactly does this KHO work fine? Device Tree
should be ok on arm*, x86 and power*, but how about s390?
Thanks
Dae
Mike Rapoport
maintainerQuoted 10 lines · RuiRui Yang
Quoted 6 lines · Mike Rapoport
== 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.
What architecture exactly does this KHO work fine? Device Tree
should be ok on arm*, x86 and power*, but how about s390?
KHO does not use device tree as the boot protocol, it uses FDT as a data structure and adds architecture specific bits to the boot structures to point to that data, very similar to how IMA_KEXEC works.
Currently KHO is implemented on arm64 and x86, but there is no fundamental reason why it wouldn't work on any architecture that supports kexec.
Quoted 3 lines
Thanks
Dae
Signature · 2 lines
Sincerely yours, Mike.
Quoted 13 lines · Mike Rapoport
Quoted 10 lines · RuiRui Yang
Quoted 6 lines · Mike Rapoport
== 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.
What architecture exactly does this KHO work fine? Device Tree
should be ok on arm*, x86 and power*, but how about s390?
Quoted 6 lines
KHO does not use device tree as the boot protocol, it uses FDT as a data structure and adds architecture specific bits to the boot structures to point to that data, very similar to how
IMA_KEXECworks.Currently KHO is implemented on arm64 and x86, but there is no fundamental reason why it wouldn't work on any architecture that supports kexec.
Well, the problem is whether there is a way to add dtb in the early boot path, for X86 it is added via setup_data, if there is no such way I'm not sure if it is doable especially for passing some info for early boot use. Then the KHO will be only for limited use cases.
Quoted 9 lines
Quoted 4 lines
Thanks
Dae--
Sincerely yours,
Mike.
Alexander Graf
reviewerQuoted 15 lines · Dave Young
Quoted 14 lines · Mike Rapoport
Quoted 9 lines · RuiRui Yang
Quoted 6 lines · Mike Rapoport
== 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.
What architecture exactly does this KHO work fine? Device Tree
should be ok on arm*, x86 and power*, but how about s390?KHO does not use device tree as the boot protocol, it uses FDT as a data structure and adds architecture specific bits to the boot structures to point to that data, very similar to how
IMA_KEXECworks.
Quoted 6 lines
Quoted 2 lines
Currently KHO is implemented on arm64 and x86, but there is no fundamental reason why it wouldn't work on any architecture that supports kexec.
Well, the problem is whether there is a way to add dtb in the early boot path, for X86 it is added via
setup_data, if there is no such way I'm not sure if it is doable especially for passing some info for early boot use. Then the KHO will be only for limited use cases.
Every architecture has a platform specific way of passing data into the kernel so it can find its command line and initrd. S390x for example has struct parmarea. To enable s390x, you would remove some of its padding and replace it with a KHO base addr + size, so that the new kernel can find the KHO state tree.
Alex
Quoted 26 lines · Alexander Graf
Quoted 15 lines · Dave Young
Quoted 14 lines · Mike Rapoport
Quoted 9 lines · RuiRui Yang
Quoted 6 lines · Mike Rapoport
== 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.
What architecture exactly does this KHO work fine? Device Tree
should be ok on arm*, x86 and power*, but how about s390?KHO does not use device tree as the boot protocol, it uses FDT as a data structure and adds architecture specific bits to the boot structures to point to that data, very similar to how
IMA_KEXECworks.Quoted 6 lines
Quoted 2 lines
Currently KHO is implemented on arm64 and x86, but there is no fundamental reason why it wouldn't work on any architecture that supports kexec.
Well, the problem is whether there is a way to add dtb in the early boot path, for X86 it is added via
setup_data, if there is no such way I'm not sure if it is doable especially for passing some info for early boot use. Then the KHO will be only for limited use cases.
Quoted 5 lines
Every architecture has a platform specific way of passing data into the kernel so it can find its command line and initrd. S390x for example has struct parmarea. To enable s390x, you would remove some of its padding and replace it with a KHO base addr + size, so that the new kernel can find the KHO state tree.
Ok, thanks for the info, I cced s390 people maybe they can provide inputs.
Other than the arch concern, I'm not so excited about the KHO
because for kexec reboot there is a fundamental problem which makes us (Red Hat kexec/kdump team) can not full support it in RHEL distribution, that is the stability due to drivers usually do not
implement the device shutdown method or not well tested. From time
to time we see weird bugs, could be malfunctioned devices or memory
corruption caused by ongoing DMA etc. Also no way for the time being
to make some graphic/drm drivers work ok after a kexec reboot, it might happen to work by luck but also not stable.
So I personally think that improving the above concern is more important than introducing more features to utilize kexec reboot.
Quoted 5 lines
Alex
Alexander Gordeev
maintainerQuoted 15 lines · Dave Young
Quoted 14 lines · Alexander Graf
Quoted 12 lines
Quoted 8 lines
Quoted 2 lines
What architecture exactly does this KHO work fine? Device Tree
should be ok on arm*, x86 and power*, but how about s390?KHO does not use device tree as the boot protocol, it uses FDT as a data structure and adds architecture specific bits to the boot structures to point to that data, very similar to how
IMA_KEXECworks.Currently KHO is implemented on arm64 and x86, but there is no fundamental reason why it wouldn't work on any architecture that supports kexec.
Well, the problem is whether there is a way to add dtb in the early boot path, for X86 it is added via
setup_data, if there is no such way I'm not sure if it is doable especially for passing some info for early boot use. Then the KHO will be only for limited use cases.
Quoted 7 lines
Quoted 5 lines
Every architecture has a platform specific way of passing data into the kernel so it can find its command line and initrd. S390x for example has struct parmarea. To enable s390x, you would remove some of its padding and replace it with a KHO base addr + size, so that the new kernel can find the KHO state tree.
Ok, thanks for the info, I cced s390 people maybe they can provide inputs.
If I understand correctly, the parmarea would be used for passing the FDT address - which appears to be fine. However, s390 does not implement early_memremap()/early_memunmap(), which KHO needs.
Thanks, Dave!
Mike Rapoport
maintainerQuoted 22 lines · Alexander Gordeev
Quoted 21 lines · Dave Young
Quoted 19 lines · Alexander Graf
Quoted 6 lines
Quoted 6 lines
Quoted 2 lines
What architecture exactly does this KHO work fine? Device Tree
should be ok on arm*, x86 and power*, but how about s390?KHO does not use device tree as the boot protocol, it uses FDT as a data structure and adds architecture specific bits to the boot structures to point to that data, very similar to how
IMA_KEXECworks.Quoted 6 lines
Quoted 2 lines
Currently KHO is implemented on arm64 and x86, but there is no fundamental reason why it wouldn't work on any architecture that supports kexec.
Well, the problem is whether there is a way to add dtb in the early boot path, for X86 it is added via
setup_data, if there is no such way I'm not sure if it is doable especially for passing some info for early boot use. Then the KHO will be only for limited use cases.Every architecture has a platform specific way of passing data into the kernel so it can find its command line and initrd. S390x for example has struct parmarea. To enable s390x, you would remove some of its padding and replace it with a KHO base addr + size, so that the new kernel can find the KHO state tree.
Quoted 5 lines
Quoted 1 line
Ok, thanks for the info, I cced s390 people maybe they can provide inputs.
If I understand correctly, the parmarea would be used for passing the FDT address - which appears to be fine. However, s390 does not implement early_memremap()/early_memunmap(), which KHO needs.
KHO uses early_memremap()/early_memunmap() because it parses FDT before phys_to_virt() is available on arm64 and x86. AFAIU on s390 phys_to_virt() can be used at setup_arch() time, so it shouldn't be a problem to add appropriate wrappers.
Signature · 2 lines
Sincerely yours, Mike.
Pratyush Yadav
maintainerHi Mike,
Quoted 8 lines · Mike Rapoport
From: "Mike Rapoport (Microsoft)" <[email protected]>
Hi,
This a next version of Alex's "kexec: Allow preservation of ftrace buffers" series (https://lore.kernel.org/all/[email protected]),
just to make things simpler instead of ftrace we decided to preserve "reserve_mem" regions.
[...]
I applied the patches on top of v6.14-rc1 and tried them out on an x86 qemu machine . When I do a plain KHO activate and kexec, I get the below errors on boot. This causes networking to fail on the VM. The errors are consistent and happen every kexec-reboot, though fairly late in boot after systemd tries to bring up network. The same setup has worked fine with Alex's v3 of KHO patches.
Do you see anything obvious that might cause this? I can try to debug this tomorrow, but if it rings any loud bells it would be nice to know.
[ 1.665225] ------------[ cut here ]------------
[ 1.665606] e1000 0000:00:03.0: DMA addr 0x0000000107978040+1522 overflow (mask ffffffff, bus limit 0).
[ 1.666364] WARNING: CPU: 6 PID: 2033 at kernel/dma/direct.h:103 dma_direct_map_page+0x271/0x280
[ 1.667074] Modules linked in:
[ 1.667335] CPU: 6 UID: 980 PID: 2033 Comm: systemd-network Not tainted 6.14.0-rc1+ #70
[ 1.668004] Hardware name: QEMU Standard PC (i440FX + PIIX, 1996), BIOS Arch Linux 1.16.3-1-1 04/01/2014
[ 1.668760] RIP: 0010:dma_direct_map_page+0x271/0x280
[ 1.669166] Code: 04 48 8b 5d 00 48 89 ef e8 7c 5f 7a 00 4d 89 e9 4d 89 e0 48 89 da 48 89 e1 41 56 48 89 c6 48 c7 c7 58 a9 4f 82 e8 3f 09 f4 ff <0f> 0b 58 eb 88 e8 05 b7 b9 00 0f 1f 44 00 00 90 90 90 90 90 90 90
[ 1.670624] RSP: 0018:ffffc90002baf628 EFLAGS: 00010282
[ 1.671035] RAX: 0000000000000000 RBX: ffff888100dd2e00 RCX: 0000000000000027
[ 1.671607] RDX: ffff88842d920a88 RSI: 0000000000000001 RDI: ffff88842d920a80
[ 1.672177] RBP: ffff8881015530c0 R08: 0000000000000000 R09: ffffc90002baf4b0
[ 1.672757] R10: ffffffff82deeec8 R11: 0000000000000003 R12: 00000000000005f2
[ 1.673320] R13: 00000000ffffffff R14: 0000000000000000 R15: 0000000000000040
[ 1.673882] FS: 00007f15c3edb880(0000) GS:ffff88842d900000(0000) knlGS:0000000000000000
[ 1.674516] CS: 0010 DS: 0000 ES: 0000 CR0: 0000000080050033
[ 1.674977] CR2: 00007f15c430cb50 CR3: 0000000102d28000 CR4: 00000000000006f0
[ 1.675554] DR0: 0000000000000000 DR1: 0000000000000000 DR2: 0000000000000000
[ 1.676117] DR3: 0000000000000000 DR6: 00000000fffe0ff0 DR7: 0000000000000400
[ 1.676698] Call Trace:
[ 1.676914] <TASK>
[ 1.677097] ? show_trace_log_lvl+0x1a7/0x2f0
[ 1.677458] ? show_trace_log_lvl+0x1a7/0x2f0
[ 1.677811] ? dma_map_page_attrs+0x6e/0x1f0
[ 1.678158] ? dma_direct_map_page+0x271/0x280
[ 1.678510] ? __warn.cold+0x93/0xf1
[ 1.678802] ? dma_direct_map_page+0x271/0x280
[ 1.679158] ? report_bug+0xff/0x140
[ 1.679461] ? handle_bug+0x53/0x90
[ 1.679768] ? exc_invalid_op+0x17/0x70
[ 1.680077] ? asm_exc_invalid_op+0x1a/0x20
[ 1.680428] ? dma_direct_map_page+0x271/0x280
[ 1.680789] ? dma_direct_map_page+0x271/0x280
[ 1.681149] dma_map_page_attrs+0x6e/0x1f0
[ 1.681483] e1000_alloc_rx_buffers+0x140/0x340
[ 1.681852] e1000_configure+0xf9/0x110
[ 1.682163] e1000_open+0xc5/0x200
[ 1.682441] __dev_open+0xff/0x1d0
[ 1.682728] __dev_change_flags+0x1f8/0x240
[ 1.683066] ? __nla_put+0x10/0x30
[ 1.683345] dev_change_flags+0x26/0x70
[ 1.683668] do_setlink.isra.0+0x2ca/0xbe0
[ 1.684002] ? cred_has_capability.isra.0+0x6a/0x110
[ 1.684411] ? blk_mq_get_tags+0x33/0x70
[ 1.684731] ? virtqueue_add_split+0xa4/0x6b0
[ 1.685086] ? security_capable+0x70/0xc0
[ 1.685409] rtnl_setlink+0x184/0x220
[ 1.685712] ? cred_has_capability.isra.0+0x6a/0x110
[ 1.686110] ? xa_load+0x7a/0xb0
[ 1.686374] ? __pfx_rtnl_setlink+0x10/0x10
[ 1.686710] rtnetlink_rcv_msg+0x354/0x3f0
[ 1.687039] ? folio_wait_bit_common+0x28b/0x300
[ 1.687407] ? ___pte_offset_map+0x1b/0x140
[ 1.687757] ? __pfx_rtnetlink_rcv_msg+0x10/0x10
[ 1.688123] netlink_rcv_skb+0x53/0x100
[ 1.688451] netlink_unicast+0x245/0x390
[ 1.688772] netlink_sendmsg+0x21b/0x470
[ 1.689085] ? __check_object_size.part.0+0x39/0xd0
[ 1.689481] __sys_sendto+0x1d4/0x1e0
[ 1.689788] __x64_sys_sendto+0x24/0x30
[ 1.690108] do_syscall_64+0x4b/0x110
[ 1.690414] entry_SYSCALL_64_after_hwframe+0x76/0x7e
[ 1.690820] RIP: 0033:0x7f15c40fc897
[ 1.691114] Code: c7 c0 ff ff ff ff eb be 66 2e 0f 1f 84 00 00 00 00 00 90 f3 0f 1e fa 80 3d d5 37 0d 00 00 41 89 ca 74 10 b8 2c 00 00 00 0f 05 <48> 3d 00 f0 ff ff 77 69 c3 55 48 89 e5 53 48 83 ec 38 44 89 4d d0
[ 1.692589] RSP: 002b:00007ffc3c3080b8 EFLAGS: 00000202 ORIG_RAX: 000000000000002c
[ 1.693191] RAX: ffffffffffffffda RBX: 000055eda6a76a70 RCX: 00007f15c40fc897
[ 1.693752] RDX: 0000000000000020 RSI: 000055eda6a93260 RDI: 0000000000000003
[ 1.694308] RBP: 00007ffc3c308150 R08: 00007ffc3c3080c0 R09: 0000000000000080
[ 1.694867] R10: 0000000000000000 R11: 0000000000000202 R12: 000055eda6a93bc0
[ 1.695423] R13: 000055eda6a8c748 R14: 0000000000000000 R15: 000055eda6a8c700
[ 1.695998] </TASK>
[ 1.696183] ---[ end trace 0000000000000000 ]---
[ 1.707952] e1000: ens3 NIC Link is Up 1000 Mbps Full Duplex, Flow Control: RX
[...]
[ 3.071312] e1000 0000:00:03.0: TX DMA map failed
[ 3.263302] e1000 0000:00:03.0: TX DMA map failed
[ 3.388180] e1000 0000:00:03.0: TX DMA map failed
[many more times]Qemu version:
QEMU emulator version 9.2.0
Copyright (c) 2003-2024 Fabrice Bellard and the QEMU Project developers
Command to run qemu:
qemu-system-x86_64 \
-m 16G \
-smp 10 \
-kernel arch/x86/boot/bzImage \
-append "console=ttyS0 root=/dev/vda rw earlyprintk=serial net.ifnames=0 kho=1 kho_scratch=200M,200M nokaslr" \
-drive file=/local/home/ptyadav/qemu/drive.ext4,format=raw,if=virtio \
-nic user,hostfwd=tcp::10022-:22 \
-gdb tcp::1234 \
-nographicSteps used to kexec:
$ echo 1 > /sys/kernel/kho/active
$ kexec -l bzImage -s --initrd /boot/initramfs-linux.img --reuse-cmdlineDT after KHO activate:
/dts-v1/;
/ {
compatible = "kho-v1";
};Signature · 2 lines
Regards, Pratyush Yadav
Mike Rapoport
maintainerHi Pratyush,
Quoted 21 lines · Pratyush Yadav
Hi Mike,
Quoted 8 lines · Mike Rapoport
From: "Mike Rapoport (Microsoft)" <[email protected]>
Hi,
This a next version of Alex's "kexec: Allow preservation of ftrace buffers" series (https://lore.kernel.org/all/[email protected]),
just to make things simpler instead of ftrace we decided to preserve "reserve_mem" regions.[...]
I applied the patches on top of v6.14-rc1 and tried them out on an x86 qemu machine . When I do a plain KHO activate and kexec, I get the below errors on boot. This causes networking to fail on the VM. The errors are consistent and happen every kexec-reboot, though fairly late in boot after systemd tries to bring up network. The same setup has worked fine with Alex's v3 of KHO patches.
Quoted 2 lines
Do you see anything obvious that might cause this? I can try to debug this tomorrow, but if it rings any loud bells it would be nice to know.
Thanks for the report!
It didn't ring any bells, but after I've found the issue and a fast-and-dirty fix.
The scratch areas are allocated from high addresses and there is no scratch memory to satisfy memblock_alloc_low() in swiotb, so second kernel produces a couple of
software IO TLB: swiotlb_memblock_alloc: Failed to allocate 67108864 bytes for tlb structure
and without those buffers e1000 can't dma :(
A quick fix would be to add another scratch area in the lower memory (below). I'll work on a better fix.
kernel/kexec_handover.c +11 -2
@@ -623,13 +623,13 @@ static phys_addr_t __init scratch_size(int nid)static void kho_reserve_scratch (void ) {phys_addr_t addr, size; -int nid, i =1 ; +int nid, i =2 ;if (!kho_enable)return ;/* FIXME: deal with node hot-plug/remove */ - kho_scratch_cnt =num_online_nodes () +1 ; + kho_scratch_cnt =num_online_nodes () +2 ; size = kho_scratch_cnt *sizeof (*kho_scratch); kho_scratch =memblock_alloc (size,PAGE_SIZE );if (!kho_scratch)
@@ -644,6 +644,15 @@ static void kho_reserve_scratch(void) kho_scratch[0 ].addr = addr; kho_scratch[0 ].size = size; + addr =memblock_phys_alloc_range (size,CMA_MIN_ALIGNMENT_BYTES , +MEMBLOCK_LOW_LIMIT , +ARCH_LOW_ADDRESS_LIMIT ); +if (!addr) + goto err_free_scratch_areas; + + kho_scratch[1 ].addr = addr; + kho_scratch[1 ].size = size; +for_each_online_node (nid) { size =scratch_size (nid); addr = memblock_alloc_range_nid(size, CMA_MIN_ALIGNMENT_BYTES,
Quoted 4 lines
Signature · 2 lines
Regards, Pratyush Yadav
Signature · 2 lines
Sincerely yours, Mike.
Pratyush Yadav
maintainerQuoted 31 lines · Mike Rapoport
Hi Pratyush,
Quoted 21 lines · Pratyush Yadav
Hi Mike,
Quoted 8 lines · Mike Rapoport
From: "Mike Rapoport (Microsoft)" <[email protected]>
Hi,
This a next version of Alex's "kexec: Allow preservation of ftrace buffers" series (https://lore.kernel.org/all/[email protected]),
just to make things simpler instead of ftrace we decided to preserve "reserve_mem" regions.[...]
I applied the patches on top of v6.14-rc1 and tried them out on an x86 qemu machine . When I do a plain KHO activate and kexec, I get the below errors on boot. This causes networking to fail on the VM. The errors are consistent and happen every kexec-reboot, though fairly late in boot after systemd tries to bring up network. The same setup has worked fine with Alex's v3 of KHO patches.
Quoted 2 lines
Do you see anything obvious that might cause this? I can try to debug this tomorrow, but if it rings any loud bells it would be nice to know.
Thanks for the report!
It didn't ring any bells, but after I've found the issue and a fast-and-dirty fix.
Quoted 5 lines
The scratch areas are allocated from high addresses and there is no scratch memory to satisfy
memblock_alloc_low()in swiotb, so second kernel produces a couple ofsoftware IO TLB:
swiotlb_memblock_alloc: Failed to allocate 67108864 bytes for tlb structure
I also did some digging today and ended up finding the same thing out but it seems you got there before me :-)
Quoted 5 lines
and without those buffers e1000 can't dma :(
A quick fix would be to add another scratch area in the lower memory (below). I'll work on a better fix.
I have already written a less-quick fix (patch pasted below) so I suppose we can use that to review the idea instead. It adds a dedicated scratch area for lowmem, similar to your patch, and adds some tracking to calculate the size.
I am not sure if the size estimation is completely right though, since it is possible that allocations that don't _need_ to be in lowmem end up being there, causing the scratch area to be too big (or perhaps even causing allocation failures if the scale is big enough). Maybe we would be better off tracking lowmem allocation requests separately?
----- 8< -----
From d60aeb2c4a1c0eea05e1a13b48b268d6192a615e Mon Sep 17 00:00:00 2001
From: Pratyush Yadav <[email protected]>
Date: Fri, 28 Feb 2025 22:36:06 +0000
Subject: [PATCH] KHO: always have a lowmem scratch region
During initialization, some callers need to allocate low memory from memblock. One such caller is swiotlb_memblock_alloc() on x86. The global and per-node scratch regions are allocated without any constraints on the range. This can lead to having no scratch region in lowmem. If that happens, the lowmem allocations can fail, leading to failures during boot or later down the line.
Always ensure there is some scratch memory available in low memory by having a separate scratch area for it, along with the global and per-node ones, and allow specifying its size via the command line.
To more accurately guess suitable scratch sizes, add memblock_reserved_kern_lowmem_size() and memblock_reserved_kern_highmem_size() which calculate how much memory was allocated in low and high memory, along with some helper functions to calculate scratch sizes.
--- .../admin-guide/kernel-parameters.txt | 9 +- Documentation/kho/usage.rst | 10 +-- include/linux/memblock.h | 2 + kernel/kexec_handover.c | 83 ++++++++++++++----- mm/memblock.c | 28 +++++++ 5 files changed, 103 insertions(+), 29 deletions(-)
Documentation/admin-guide/kernel-parameters.txt +5 -4
@@ -2705,7 +2705,7 @@ "1" | "on" | "y" - kexec handover is enabled kho_scratch= [KEXEC,EARLY] - Format: nn[KMG],mm[KMG] | nn% + Format: ll[KMG],nn[KMG],mm[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
@@ -2715,9 +2715,10 @@ bootstrap itself. It is possible to specify the exact amount of - memory in the form of "nn[KMG],mm[KMG]" where the - first parameter defines the size of a global - scratch area and the second parameter defines the + memory in the form of "ll[KMG],nn[KMG],mm[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.
Documentation/kho/usage.rst +5 -5
@@ -19,11 +19,11 @@ 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=512M,512M`` will reserve a 512 -MiB for a global scratch region and 512 MiB per NUMA node scratch regions -on boot. +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=128M,512M,512M`` will reserve a 128 MiB low memory scratch +region, a 512 MiB global scratch region and 512 MiB per NUMA node scratch +regions on boot. Perform a KHO kexec -------------------
include/linux/memblock.h +2 -0
@@ -504,6 +504,8 @@ 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 (int nid); +phys_addr_t memblock_reserved_kern_lowmem_size (void ); +phys_addr_t memblock_reserved_kern_highmem_size (void );unsigned long memblock_estimated_nr_free_pages (void );phys_addr_t memblock_start_of_DRAM (void );phys_addr_t memblock_end_of_DRAM (void );
kernel/kexec_handover.c +63 -20
145 lines not on this page · raw message
mm/memblock.c +28 -0
@@ -1837,6 +1837,34 @@ phys_addr_t __init_memblock memblock_reserved_size(void)return memblock.reserved.total_size; } +phys_addr_t __init_memblock memblock_reserved_kern_lowmem_size(void) +{ +struct memblock_region *r; +phys_addr_t total =0 ; + +for_each_reserved_mem_region (r) { +if ((r->flags &MEMBLOCK_RSRV_KERN ) && + (r->base + r->size <=ARCH_LOW_ADDRESS_LIMIT )) + total += r->size; + } + +return total; +} + +phys_addr_t __init_memblock memblock_reserved_kern_highmem_size(void) +{ +struct memblock_region *r; +phys_addr_t total =0 ; + +for_each_reserved_mem_region (r) { +if ((r->flags &MEMBLOCK_RSRV_KERN ) && + (r->base + r->size >ARCH_LOW_ADDRESS_LIMIT )) + total += r->size; + } + +return total; +} + phys_addr_t __init_memblock memblock_reserved_kern_size(int nid) {struct memblock_region *r;
Signature · 2 lines
Regards, Pratyush Yadav
Mike Rapoport
maintainerQuoted 48 lines · Pratyush Yadav
Quoted 27 lines · Mike Rapoport
Quoted 19 lines · Pratyush Yadav
Quoted 8 lines · Mike Rapoport
From: "Mike Rapoport (Microsoft)" <[email protected]>
Hi,
This a next version of Alex's "kexec: Allow preservation of ftrace buffers" series (https://lore.kernel.org/all/[email protected]),
just to make things simpler instead of ftrace we decided to preserve "reserve_mem" regions.[...]
I applied the patches on top of v6.14-rc1 and tried them out on an x86 qemu machine . When I do a plain KHO activate and kexec, I get the below errors on boot. This causes networking to fail on the VM. The errors are consistent and happen every kexec-reboot, though fairly late in boot after systemd tries to bring up network. The same setup has worked fine with Alex's v3 of KHO patches.
Quoted 2 lines
Do you see anything obvious that might cause this? I can try to debug this tomorrow, but if it rings any loud bells it would be nice to know.
Thanks for the report!
It didn't ring any bells, but after I've found the issue and a fast-and-dirty fix.Quoted 5 lines
The scratch areas are allocated from high addresses and there is no scratch memory to satisfy
memblock_alloc_low()in swiotb, so second kernel produces a couple ofsoftware IO TLB:
swiotlb_memblock_alloc: Failed to allocate 67108864 bytes for tlb structureI also did some digging today and ended up finding the same thing out but it seems you got there before me :-)
Quoted 5 lines
and without those buffers e1000 can't dma :(
A quick fix would be to add another scratch area in the lower memory (below). I'll work on a better fix.
I have already written a less-quick fix (patch pasted below) so I suppose we can use that to review the idea instead. It adds a dedicated scratch area for lowmem, similar to your patch, and adds some tracking to calculate the size.
Quoted 5 lines
I am not sure if the size estimation is completely right though, since it is possible that allocations that don't _need_ to be in lowmem end up being there, causing the scratch area to be too big (or perhaps even causing allocation failures if the scale is big enough). Maybe we would be better off tracking lowmem allocation requests separately?
Normally memblock allocates top-down, so if there is enough memory allocations that do not limit memory ranges won't end up in lowmem. And on systems with smaller memory (e.g 4G and less) we'd anyway have scratch areas in lowmem.
Yet, there is an issue with swiotlb_init() and estimation of the size for lowmem scratch area we need to address. swiotlb_init() is called from mem_init() and the memory it allocates won't be accounted by the time of kho_memory_init().
I've been meaning to pull out common parts of mem_init() from arch-specific code to mm_init for a while now, maybe the time has come :)
Quoted 289 lines
----- 8< -----
Fromd60aeb2c4a1c0eea05e1a13b48b268d6192a615eMon Sep 17 00:00:00 2001
From: Pratyush Yadav <[email protected]>
Date: Fri, 28 Feb 2025 22:36:06 +0000
Subject: [PATCH] KHO: always have a lowmem scratch regionDuring initialization, some callers need to allocate low memory from memblock. One such caller is
swiotlb_memblock_alloc()on x86. The global and per-node scratch regions are allocated without any constraints on the range. This can lead to having no scratch region in lowmem. If that happens, the lowmem allocations can fail, leading to failures during boot or later down the line.Always ensure there is some scratch memory available in low memory by having a separate scratch area for it, along with the global and per-node ones, and allow specifying its size via the command line.
To more accurately guess suitable scratch sizes, add
memblock_reserved_kern_lowmem_size()andmemblock_reserved_kern_highmem_size()which calculate how much memory was allocated in low and high memory, along with some helper functions to calculate scratch sizes.Signed-off-by Pratyush Yadav <[email protected]>--- .../admin-guide/kernel-parameters.txt | 9 +- Documentation/kho/usage.rst | 10 +-- include/linux/memblock.h | 2 + kernel/kexec_handover.c | 83 ++++++++++++++----- mm/memblock.c | 28 +++++++ 5 files changed, 103 insertions(+), 29 deletions(-)Documentation/admin-guide/kernel-parameters.txt +5
-4@@ -2705,7 +2705,7 @@ "1" | "on" | "y" - kexec handover is enabled kho_scratch= [KEXEC,EARLY] - Format: nn[KMG],mm[KMG] | nn% + Format: ll[KMG],nn[KMG],mm[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@@ -2715,9 +2715,10 @@ bootstrap itself. It is possible to specify the exact amount of - memory in the form of "nn[KMG],mm[KMG]" where the - first parameter defines the size of a global - scratch area and the second parameter defines the + memory in the form of "ll[KMG],nn[KMG],mm[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.Documentation/kho/usage.rst +5
-5@@ -19,11 +19,11 @@ 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=512M,512M`` will reserve a 512 -MiB for a global scratch region and 512 MiB per NUMA node scratch regions -on boot. +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=128M,512M,512M`` will reserve a 128 MiB low memory scratch +region, a 512 MiB global scratch region and 512 MiB per NUMA node scratch +regions on boot. Perform a KHO kexec -------------------include/linux/memblock.h +2
-0@@ -504,6 +504,8 @@ 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 (int nid); +phys_addr_t memblock_reserved_kern_lowmem_size (void ); +phys_addr_t memblock_reserved_kern_highmem_size (void );unsigned long memblock_estimated_nr_free_pages (void );phys_addr_t memblock_start_of_DRAM (void );phys_addr_t memblock_end_of_DRAM (void );kernel/kexec_handover.c +63
-20@@ -547,20 +547,21 @@ late_initcall(kho_init);* * kho_scratch=N% * -* It is also possible to explicitly define size for a global and per-node -* scratch areas: +* It is also possible to explicitly define size for a lowmem, a global and +* per-node scratch areas: * -* kho_scratch=n[KMG],m[KMG] +* 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) { -unsigned long size, size_pernode; +unsigned long size, size_pernode, size_global;char *endptr, *oldp = p;if (!p)@@ -578,15 +579,25 @@ static int __init kho_parse_scratch_size(char *p)if (*p !=',' )return -EINVAL ; + oldp = p; + size_global =memparse (p +1 , &p); +if (!size_global || p == oldp) +return -EINVAL ; + +if (*p !=',' ) +return -EINVAL ; + size_pernode =memparse (p +1 , &p);if (!size_pernode)return -EINVAL ; - scratch_size_global = size; + scratch_size_lowmem = size; + scratch_size_global = size_global; scratch_size_pernode = size_pernode; scratch_scale =0 ; -pr_notice ("scratch areas: global: %lluMB pernode: %lldMB\n" , +pr_notice ("scratch areas: lowmem: %lluMB global: %lluMB pernode: %lldMB\n" , + (u64)(scratch_size_lowmem >>20 ), (u64)(scratch_size_global >>20 ), (u64)(scratch_size_pernode >>20 )); }@@ -595,18 +606,38 @@ static int __init kho_parse_scratch_size(char *p) }early_param ("kho_scratch" , kho_parse_scratch_size); -static phys_addr_t __init scratch_size (int nid) +static phys_addr_t __init scratch_size_low (void ) +{ +phys_addr_t size; + +if (scratch_scale) + size =memblock_reserved_kern_lowmem_size () * scratch_scale /100 ; +else + size = scratch_size_lowmem; + +return round_up (size,CMA_MIN_ALIGNMENT_BYTES ); +} + +static phys_addr_t __init scratch_size_high (void ) +{ +phys_addr_t size; + +if (scratch_scale) + size =memblock_reserved_kern_highmem_size () * scratch_scale /100 ; +else + size = scratch_size_global; + +return round_up (size,CMA_MIN_ALIGNMENT_BYTES ); +} + +static phys_addr_t __init scratch_size_node (int nid) {phys_addr_t size; -if (scratch_scale) { +if (scratch_scale) size =memblock_reserved_kern_size (nid) * scratch_scale /100 ; - }else { -if (numa_valid_node (nid)) - size = scratch_size_pernode; -else - size = scratch_size_global; - } +else + size = scratch_size_pernode;return round_up (size,CMA_MIN_ALIGNMENT_BYTES ); }@@ -623,29 +654,41 @@ static phys_addr_t __init scratch_size(int nid)static void kho_reserve_scratch (void ) {phys_addr_t addr, size; -int nid, i =1 ; +int nid, i =0 ;if (!kho_enable)return ;/* FIXME: deal with node hot-plug/remove */ - kho_scratch_cnt =num_online_nodes () +1 ; + 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 large contiguous area for allocations without nid */ - size =scratch_size (NUMA_NO_NODE ); - addr =memblock_phys_alloc (size,CMA_MIN_ALIGNMENT_BYTES ); +/* reserve area for lowmem allocations. */ + size =scratch_size_low (); + addr =memblock_phys_alloc_range (size,CMA_MIN_ALIGNMENT_BYTES ,0 , +ARCH_LOW_ADDRESS_LIMIT );if (!addr) goto err_free_scratch_desc; kho_scratch[0 ].addr = addr; kho_scratch[0 ].size = size; + i++; + +/* reserve large contiguous area for allocations without nid */ + size =scratch_size_high (); + addr =memblock_phys_alloc (size,CMA_MIN_ALIGNMENT_BYTES ); +if (!addr) + goto err_free_scratch_areas; + + kho_scratch[1 ].addr = addr; + kho_scratch[1 ].size = size; + i++;for_each_online_node (nid) { - size =scratch_size (nid); + size =scratch_size_node (nid); addr =memblock_alloc_range_nid (size,CMA_MIN_ALIGNMENT_BYTES ,0 ,MEMBLOCK_ALLOC_ACCESSIBLE , nid, true);mm/memblock.c +28
-0@@ -1837,6 +1837,34 @@ phys_addr_t __init_memblock memblock_reserved_size(void)return memblock.reserved.total_size; } +phys_addr_t __init_memblock memblock_reserved_kern_lowmem_size(void) +{ +struct memblock_region *r; +phys_addr_t total =0 ; + +for_each_reserved_mem_region (r) { +if ((r->flags &MEMBLOCK_RSRV_KERN ) && + (r->base + r->size <=ARCH_LOW_ADDRESS_LIMIT )) + total += r->size; + } + +return total; +} + +phys_addr_t __init_memblock memblock_reserved_kern_highmem_size(void) +{ +struct memblock_region *r; +phys_addr_t total =0 ; + +for_each_reserved_mem_region (r) { +if ((r->flags &MEMBLOCK_RSRV_KERN ) && + (r->base + r->size >ARCH_LOW_ADDRESS_LIMIT )) + total += r->size; + } + +return total; +} + phys_addr_t __init_memblock memblock_reserved_kern_size(int nid) {struct memblock_region *r;Signature · 2 lines
Regards, Pratyush Yadav
Signature · 2 lines
Sincerely yours, Mike.