kdump: reduce vmcore size and capture time
Subsystems (8)
v4 cover kdump: reduce vmcore size and capture time
From: Wandun Chen <[email protected]>
On SoCs that carve out large firmware-owned reserved memory (GPU firmware, DSP, modem, camera ISP, NPU, ...), kdump currently dumps those carveouts as part of system RAM even though their contents are firmware state that is not useful for kernel crash analysis.
This series introduces an opt-in 'dumpable' flag [1] on struct reserved_mem and uses it to filter the elfcorehdr PT_LOAD ranges on DT-based architectures (arm64, riscv, loongarch). By default reserved regions are treated as non-dumpable; CMA regions are explicitly opted in because their pages are returned to the buddy allocator and may carry key crash-analysis data.
Since the reserved memory regions are filtered out, the vmcore is smaller in size and faster to produce. The reserved memory has already been filtered out in x86; the optimizations in this series apply to systems that use DTS such as arm64/riscv/loongarch.
The series is organized as follows:
Patches 1-4: Small prepare changes and cleanups.
Patches 5: Add dumpable flag to opt-in vmcore.
Patches 6: Append /memreserve/ entries into reserved_mem[]. Patch 7: Add generic kdump helpers.
Patches 8-10: Wire the helpers into arm64, riscv and loongarch kdump
elfcorehdr preparation.
v3 --> v4:
1. Rebase this series on v7.2-rc1.
2. Add two cleanup patches (patch 02/03).
3. Simplify patch 03 to avoid checking whether initial_boot_params is NULL multiple times, suggested by Rob.
v2 --> v3:
1. Fix out-of-bounds issue if device tree lacks /reserved-memory node.[2]
2. Fix UAF issue when alloc_reserved_mem_array() fails.
3. Add some prepare patches.
v1 --> v2:
1. v1 added an opt-out DT property ('linux,no-dump'). Per Rob's feedback [1], v2 drop that property and exclude reserve memory by default.
2. Split some prepared patches from the original patches.
3. Address coding-style comments on patch 5 from Rob.
[1] https://lore.kernel.org/lkml/[email protected]/
[2] https://sashiko.dev/#/patchset/20260520091844.592753-1-chenwandun%40lixiang.com?part=4
Wandun Chen (10):
kexec/crash: provide crash_exclude_mem_range() stub when
CONFIG_CRASH_DUMP=n
of: reserved_mem: dedup and relocate reserved-memory messages
of: reserved_mem: skip late scan when no regions are reserved
of: reserved_mem: split alloc_reserved_mem_array() from
fdt_scan_reserved_mem_late()
of: reserved_mem: add dumpable flag to opt-in vmcore
of: reserved_mem: save /memreserve/ entries into the reserved_mem
array
of: reserved_mem: add kdump helpers to exclude non-dumpable regions
arm64: kdump: exclude non-dumpable reserved memory regions from vmcore
riscv: kdump: exclude non-dumpable reserved memory regions from vmcore
loongarch: kdump: exclude non-dumpable reserved memory regions from
vmcore
arch/arm64/kernel/machine_kexec_file.c | 6 ++ arch/loongarch/kernel/machine_kexec_file.c | 6 ++ arch/riscv/kernel/machine_kexec_file.c | 4 + drivers/of/fdt.c | 11 ++- drivers/of/of_private.h | 3 + drivers/of/of_reserved_mem.c | 108 ++++++++++++++++++--- include/linux/crash_core.h | 6 ++ include/linux/of_reserved_mem.h | 15 +++ kernel/dma/contiguous.c | 1 + 9 files changed, 142 insertions(+), 18 deletions(-)
Signature · 1 line
2.43.0
PATCH v4 1/10 kexec/crash: provide crash_exclude_mem_range() stub when CONFIG_CRASH_DUMP=n
From: Wandun Chen <[email protected]>
Prepare for an upcoming change that excludes non-dumpable reserved regions from the kdump vmcore and will call crash_exclude_mem_range() from generic, non-arch code.
No functional change.
--- include/linux/crash_core.h | 6 ++++++ 1 file changed, 6 insertions(+)
include/linux/crash_core.h +6 -0
@@ -87,6 +87,12 @@ static inline int kexec_should_crash(struct task_struct *p) { return 0; }static inline int kexec_crash_loaded (void ) {return 0 ; }static inline void crash_save_cpu (struct pt_regs *regs,int cpu) {};static inline int kimage_crash_copy_vmcoreinfo (struct kimage *image) {return 0 ; }; +static inline int crash_exclude_mem_range (struct crash_mem *mem, +unsigned long long mstart, +unsigned long long mend) +{ +return 0 ; +}#endif /* CONFIG_CRASH_DUMP*/ #ifdef CONFIG_CRASH_DM_CRYPT
Signature · 1 line
2.43.0
Re: [PATCH v4 01/10] kexec/crash: provide crash_exclude_mem_range() stub when CONFIG_CRASH_DUMP=n
Quoted 9 lines · Wandun Chen
From: Wandun Chen <[email protected]>
Prepare for an upcoming change that excludes non-dumpable reserved regions from the kdump vmcore and will call
crash_exclude_mem_range()from generic, non-arch code.No functional change.
Signed-off-by Wandun Chen <[email protected]>
[...]
Signature · 2 lines
Regards, Pratyush Yadav
PATCH v4 2/10 of: reserved_mem: dedup and relocate reserved-memory messages
In reply to Wandun ChenFrom: Wandun Chen <[email protected]>
In both fdt_scan_reserved_mem and fdt_scan_reserved_mem_late, there are identical check-related print messages. Consolidate these messages by moving them all into the fdt_scan_reserved_mem function.
No functional change.
--- drivers/of/of_reserved_mem.c | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-)
drivers/of/of_reserved_mem.c +3 -6
@@ -270,19 +270,15 @@ void __init fdt_scan_reserved_mem_late(void)return ; node =fdt_path_offset (fdt,"/reserved-memory" ); -if (node <0 ) { -pr_info ("Reserved memory: No reserved-memory node in the DT\n" ); +if (node <0 )return ; - }/* Attempt dynamic allocation of a new reserved_mem array */ if (alloc_reserved_mem_array ())return ; -if (__reserved_mem_check_root (node)) { -pr_err ("Reserved memory: unsupported node format, ignoring\n" ); +if (__reserved_mem_check_root (node))return ; - }fdt_for_each_subnode (child, fdt, node) {const __be32 *prop;
@@ -337,6 +333,7 @@ int __init fdt_scan_reserved_mem(void) node =fdt_path_offset (fdt,"/reserved-memory" );if (node <0 ) { +pr_info ("Reserved memory: No reserved-memory node in the DT\n" ); total_reserved_mem_cnt =0 ;return -ENODEV ; }
Signature · 1 line
2.43.0
PATCH v4 3/10 of: reserved_mem: skip late scan when no regions are reserved
In reply to Wandun ChenFrom: Wandun Chen <[email protected]>
When total_reserved_mem_cnt is 0, there is no /reserved-memory node so fdt_scan_reserved_mem_late() have nothing to do, so return -ENODEV directly from alloc_reserved_mem_array() in this case.
No functional change.
--- drivers/of/of_reserved_mem.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/of/of_reserved_mem.c +1 -1
@@ -76,7 +76,7 @@ static int __init alloc_reserved_mem_array(void)int ret;if (!total_reserved_mem_cnt) -return 0 ; +return -ENODEV ; alloc_size =array_size (total_reserved_mem_cnt,sizeof (*new_array));if (alloc_size ==SIZE_MAX ) {
Signature · 1 line
2.43.0
[email protected]
AI reviewRe: [PATCH v4 03/10] of: reserved_mem: skip late scan when no regions are reserved
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
Pre-existing issues:
- [High] The dynamic_nodes array in fdt_scan_reserved_mem() has a fixed size of MAX_RESERVED_REGIONS (64) but lacks bounds checking, leading to a potential stack buffer overflow. --
commit e62a55f3fff119a2e88f5aefe6dbf1cf5ef32c31
Author: Wandun Chen <[email protected]>
of: reserved_mem: skip late scan when no regions are reserved
This commit optimizes reserved memory initialization by skipping the late scan when no reserved regions exist. It updates alloc_reserved_mem_array() to return -ENODEV directly if total_reserved_mem_cnt is 0.
drivers/of/of_reserved_mem.c static int __init alloc_reserved_mem_array(void) · line 81
Quoted 12 lines
drivers/of/of_reserved_mem.c +1
-1@@ -76,7 +76,7 @@ static int __init alloc_reserved_mem_array(void)int ret;if (!total_reserved_mem_cnt) -return 0 ; +return -ENODEV ; alloc_size =array_size (total_reserved_mem_cnt,sizeof (*new_array));
[Severity: High]
This isn't a bug introduced by this patch, but while reviewing the reserved memory changes, I noticed a potential issue in the nearby fdt_scan_reserved_mem() function.
Does this code overflow dynamic_nodes[] if the device tree contains more than MAX_RESERVED_REGIONS dynamic regions?
drivers/of/of_reserved_mem.c:fdt_scan_reserved_mem() {
int dynamic_nodes_cnt = 0;
int dynamic_nodes[MAX_RESERVED_REGIONS];
...
if (err == -ENOENT && of_get_flat_dt_prop(child, "size", NULL)) {
dynamic_nodes[dynamic_nodes_cnt] = child;
dynamic_nodes_cnt++;
}...
}
If a malformed Flattened Device Tree is provided with more than 64 dynamically allocated reserved memory regions, it looks like dynamic_nodes_cnt could increment past the bounds of the array, leading to stack corruption.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=3
PATCH v4 4/10 of: reserved_mem: split alloc_reserved_mem_array() from fdt_scan_reserved_mem_late()
In reply to Wandun ChenFrom: Wandun Chen <[email protected]>
Prepare for storing /memreserve/ entries in the reserved_mem array. alloc_reserved_mem_array was skipped if the device tree lacks a /reserved-memory node, pointer 'reserved_mem' continues to reference the reserved_mem_array which lives in __initdata, storing /memreserve/ entries into reserved_mem_array would result in metadata loss, and an out-of-bounds memory access will occur if the device tree contains more than MAX_RESERVED_REGIONS /memreserve/ entries.
So split alloc_reserved_mem_array() from fdt_scan_reserved_mem_late(), and call alloc_reserved_mem_array() whether or not there is a /reserved-memory node.
No functional change.
The actual /memreserve/ population is added in a follow-up patch.
--- drivers/of/fdt.c | 7 +++++-- drivers/of/of_private.h | 1 + drivers/of/of_reserved_mem.c | 11 ++--------- 3 files changed, 8 insertions(+), 11 deletions(-)
drivers/of/fdt.c +5 -2
@@ -1282,8 +1282,11 @@ void __init unflatten_device_tree(void) {void *fdt = initial_boot_params; -/* Save the statically-placed regions in the reserved_mem array */ -fdt_scan_reserved_mem_late (); +/* Attempt dynamic allocation of a new reserved_mem array */ +if (!alloc_reserved_mem_array ()) { +/* Save the statically-placed regions in the reserved_mem array */ +fdt_scan_reserved_mem_late (); + }/* Populate an empty root node when bootloader doesn't provide one */ if (!fdt) {
drivers/of/of_private.h +1 -0
@@ -187,6 +187,7 @@ static inline struct device_node *__of_get_dma_parent(const struct device_node *int fdt_scan_reserved_mem (void );void __init fdt_scan_reserved_mem_late (void ); +int __init alloc_reserved_mem_array (void );bool of_fdt_device_is_available (const void *blob,unsigned long node);
drivers/of/of_reserved_mem.c +2 -9
@@ -69,13 +69,13 @@ static int __init early_init_dt_alloc_reserved_memory_arch(phys_addr_t size,* the initial static array is copied over to this new array and * the new array is used from this point on. */ -static int __init alloc_reserved_mem_array (void ) +int __init alloc_reserved_mem_array (void ) {struct reserved_mem *new_array;size_t alloc_size, copy_size, memset_size;int ret; -if (!total_reserved_mem_cnt) +if (!initial_boot_params || !total_reserved_mem_cnt)return -ENODEV ; alloc_size =array_size (total_reserved_mem_cnt,sizeof (*new_array));
@@ -266,17 +266,10 @@ void __init fdt_scan_reserved_mem_late(void)phys_addr_t base, size;int node, child; -if (!fdt) -return ; - node =fdt_path_offset (fdt,"/reserved-memory" );if (node <0 )return ; -/* Attempt dynamic allocation of a new reserved_mem array */ -if (alloc_reserved_mem_array ()) -return ; -if (__reserved_mem_check_root (node))return ;
Signature · 1 line
2.43.0
PATCH v4 5/10 of: reserved_mem: add dumpable flag to opt-in vmcore
In reply to Wandun ChenFrom: Wandun Chen <[email protected]>
Add a 'dumpable' flag to struct reserved_mem so the kernel can decide whether a reserved area should be included in the kdump vmcore. Most reserved regions are owned by devices and do not contain data useful for kernel crash analysis, so excluding them by default is the right behaviour.
Reusable CMA regions are different: pages in a CMA region are handed back to the buddy allocator and may contain key data for crash analysis, so set dumpable to true in rmem_cma_setup().
--- include/linux/of_reserved_mem.h | 1 + kernel/dma/contiguous.c | 1 + 2 files changed, 2 insertions(+)
include/linux/of_reserved_mem.h +1 -0
@@ -15,6 +15,7 @@ struct reserved_mem {phys_addr_t base;phys_addr_t size;void *priv; +bool dumpable; };struct reserved_mem_ops {
kernel/dma/contiguous.c +1 -0
@@ -565,6 +565,7 @@ static int __init rmem_cma_setup(unsigned long node, struct reserved_mem *rmem) dma_contiguous_default_area = cma; rmem->priv = cma; + rmem->dumpable = true;pr_info ("Reserved memory: created CMA memory pool at %pa, size %ld MiB\n" , &rmem->base, (unsigned long )rmem->size /SZ_1M );
Signature · 1 line
2.43.0
PATCH v4 6/10 of: reserved_mem: save /memreserve/ entries into the reserved_mem array
In reply to Wandun ChenFrom: Wandun Chen <[email protected]>
/memreserve/ is used by firmware or bootloaders, such regions hold no useful data for crash analysis, they should be excluded from the kdump vmcore, so save /memreserve/ entries into the reserved_mem array for later exclusion.
If a /memreserve/ entry overlaps any dumpable reserved region, mark the whole memreserve entry dumpable as well. This may keep slightly more memory in vmcore than strictly necessary, but avoids splitting entries and never drops data that may be useful for crash analysis.
--- drivers/of/fdt.c | 4 +++ drivers/of/of_private.h | 2 ++ drivers/of/of_reserved_mem.c | 52 ++++++++++++++++++++++++++++++++++++ 3 files changed, 58 insertions(+)
drivers/of/fdt.c +4 -0
@@ -497,6 +497,7 @@ void __init early_init_fdt_scan_reserved_mem(void)int n;int res;u64 base, size; +int nr_memreserve =0 ;if (!initial_boot_params)return ;
@@ -514,7 +515,9 @@ void __init early_init_fdt_scan_reserved_mem(void)if (!size)break ;memblock_reserve (base, size); + nr_memreserve++; } +fdt_reserved_mem_account_memreserve (nr_memreserve); }/**
@@ -1286,6 +1289,7 @@ void __init unflatten_device_tree(void)if (!alloc_reserved_mem_array ()) {/* Save the statically-placed regions in the reserved_mem array */ fdt_scan_reserved_mem_late (); +fdt_reserved_mem_save_memreserve_entries (); }/* Populate an empty root node when bootloader doesn't provide one */
drivers/of/of_private.h +2 -0
@@ -188,6 +188,8 @@ static inline struct device_node *__of_get_dma_parent(const struct device_node *int fdt_scan_reserved_mem (void );void __init fdt_scan_reserved_mem_late (void );int __init alloc_reserved_mem_array (void ); +void __init fdt_reserved_mem_account_memreserve (int n); +void __init fdt_reserved_mem_save_memreserve_entries (void );bool of_fdt_device_is_available (const void *blob,unsigned long node);
drivers/of/of_reserved_mem.c +52 -0
64 lines not on this page · raw message
Signature · 1 line
2.43.0
PATCH v4 7/10 of: reserved_mem: add kdump helpers to exclude non-dumpable regions
In reply to Wandun ChenFrom: Wandun Chen <[email protected]>
Add two helpers to exclude non-dumpable regions for arch-specific code.
- of_reserved_mem_kdump_nr_ranges() returns the count of regions that are not dumpable. Each excluded region may split an existing crash_mem range into two, so callers use this to calculate crash_mem allocation size.
- of_reserved_mem_kdump_exclude() walks reserved_mem[] and calls crash_exclude_mem_range() for every non-dumpable region.
--- drivers/of/of_reserved_mem.c | 34 +++++++++++++++++++++++++++++++++ include/linux/of_reserved_mem.h | 14 ++++++++++++++ 2 files changed, 48 insertions(+)
drivers/of/of_reserved_mem.c +34 -0
@@ -24,6 +24,7 @@#include <linux/slab.h> #include <linux/memblock.h> #include <linux/kmemleak.h> +#include <linux/crash_core.h> #include "of_private.h"
@@ -856,6 +857,39 @@ struct reserved_mem *of_reserved_mem_lookup(struct device_node *np) }EXPORT_SYMBOL_GPL (of_reserved_mem_lookup); +/* +* Count non-dumpable reserved regions. Excluding each one may split a +* crash_mem range in two, callers use this to size the allocation. +*/ +unsigned int of_reserved_mem_kdump_nr_ranges (void ) +{ +unsigned int i, n =0 ; + +for (i =0 ; i < reserved_mem_count; i++) +if (reserved_mem[i].size && !reserved_mem[i].dumpable) + n++; +return n; +} + +/* Exclude non-dumpable reserved regions from @cmem. */ +int of_reserved_mem_kdump_exclude (struct crash_mem *cmem) +{ +unsigned int i; +int ret; + +for (i =0 ; i < reserved_mem_count; i++) { +struct reserved_mem *r = &reserved_mem[i]; + +if (!r->size || r->dumpable) +continue ; + ret =crash_exclude_mem_range (cmem, r->base, + r->base + r->size -1 ); +if (ret) +return ret; + } +return 0 ; +} +/** * of_reserved_mem_region_to_resource() - Get a reserved memory region as a resource * @np: node containing 'memory-region' property
include/linux/of_reserved_mem.h +14 -0
@@ -8,6 +8,7 @@struct of_phandle_args ;struct reserved_mem_ops ;struct resource ; +struct crash_mem ;struct reserved_mem {const char *name;
@@ -48,6 +49,9 @@ int of_reserved_mem_region_to_resource_byname(const struct device_node *np, const char *name, struct resource *res);int of_reserved_mem_region_count (const struct device_node *np); +unsigned int of_reserved_mem_kdump_nr_ranges (void ); +int of_reserved_mem_kdump_exclude (struct crash_mem *cmem); +#else #define RESERVEDMEM_OF_DECLARE (name, compat, ops) \
@@ -92,6 +96,16 @@ static inline int of_reserved_mem_region_count(const struct device_node *np) {return 0 ; } + +static inline unsigned int of_reserved_mem_kdump_nr_ranges (void ) +{ +return 0 ; +} + +static inline int of_reserved_mem_kdump_exclude (struct crash_mem *cmem) +{ +return 0 ; +}#endif /**
Signature · 1 line
2.43.0
Re: [PATCH v4 07/10] of: reserved_mem: add kdump helpers to exclude non-dumpable regions
Quoted 10 lines · Wandun Chen
From: Wandun Chen <[email protected]>
Add two helpers to exclude non-dumpable regions for arch-specific code.
-
of_reserved_mem_kdump_nr_ranges()returns the count of regions that are not dumpable. Each excluded region may split an existingcrash_memrange into two, so callers use this to calculatecrash_memallocation size.
Quoted 5 lines
-
of_reserved_mem_kdump_exclude()walks reserved_mem[] and callscrash_exclude_mem_range()for every non-dumpable region.Signed-off-by Wandun Chen <[email protected]>Tested-by Meijing Zhao <[email protected]>
[...]
Signature · 2 lines
Regards, Pratyush Yadav
PATCH v4 8/10 arm64: kdump: exclude non-dumpable reserved memory regions from vmcore
In reply to Wandun ChenFrom: Wandun Chen <[email protected]>
Reserved memory regions are excluded from vmcore by default unless marked dumpable. Honor the dumpable flag to filter out device firmware regions (e.g., GPU, DSP, modem) reserved via device tree, since they typically contain data not useful for kernel crash analysis and can significantly increase vmcore size.
Use of_reserved_mem_kdump_exclude() to perform the exclusion, and pre-size the crash_mem array via of_reserved_mem_kdump_nr_ranges().
--- arch/arm64/kernel/machine_kexec_file.c | 6 ++++++ 1 file changed, 6 insertions(+)
arch/arm64/kernel/machine_kexec_file.c +6 -0
@@ -17,6 +17,7 @@#include <linux/memblock.h> #include <linux/of.h> #include <linux/of_fdt.h> +#include <linux/of_reserved_mem.h> #include <linux/slab.h> #include <linux/string.h> #include <linux/types.h>
@@ -51,6 +52,7 @@ static int prepare_elf_headers(void **addr, unsigned long *sz) nr_ranges =2 ;/* for exclusion of crashkernel region */ for_each_mem_range (i, &start, &end) nr_ranges++; + nr_ranges +=of_reserved_mem_kdump_nr_ranges (); cmem =kmalloc_flex (*cmem, ranges, nr_ranges);if (!cmem)
@@ -75,6 +77,10 @@ static int prepare_elf_headers(void **addr, unsigned long *sz) goto out; } + ret =of_reserved_mem_kdump_exclude (cmem); +if (ret) + goto out; + ret =crash_prepare_elf64_headers (cmem, true, addr, sz); out:
Signature · 1 line
2.43.0
[email protected]
AI reviewRe: [PATCH v4 08/10] arm64: kdump: exclude non-dumpable reserved memory regions from vmcore
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
Pre-existing issues:
- [High] Preexisting TOCTOU race in prepare_elf_headers() during memblock iteration can cause an out-of-bounds array write due to concurrent memory hotplug. --
commit 66936af0a692eab34029fe4f8b5ede43875e861f
Author: Wandun Chen <[email protected]> Date: Tue Jun 30 07:47:12 2026 +0000
arm64: kdump: exclude non-dumpable reserved memory regions from vmcore
This commit excludes reserved memory regions from the vmcore unless they are explicitly marked as dumpable. It uses device tree reservations to filter out device firmware regions, reducing the size of the vmcore. The array size is adjusted to accommodate these exclusions.
Quoted 4 lines
arch/arm64/kernel/machine_kexec_file.c +0
-0
[ ... ]
arch/arm64/kernel/machine_kexec_file.c static int prepare_elf_headers(void **addr, unsigned long *sz) · line 58
Quoted 8 lines
arch/arm64/kernel/machine_kexec_file.c +1
-0@@ -51,6 +52,7 @@ static int prepare_elf_headers(void **addr, unsigned long *sz) nr_ranges =2 ;/* for exclusion of crashkernel region */ for_each_mem_range (i, &start, &end) nr_ranges++; + nr_ranges +=of_reserved_mem_kdump_nr_ranges (); cmem =kmalloc_flex (*cmem, ranges, nr_ranges);if (!cmem)
[Severity: High]
This isn't a bug introduced by this patch, but could this allocation lead to an out-of-bounds write due to a time-of-check to time-of-use race?
In prepare_elf_headers(), the code counts the memory ranges with a first for_each_mem_range() loop, allocates cmem based on that count, and then populates cmem->ranges in a second for_each_mem_range() loop further down.
Because sys_kexec_file_load() does not hold the memory hotplug lock, a concurrent memory hotplug operation could increase the number of ranges between the two loops.
Since the second loop does not verify if cmem->nr_ranges exceeds cmem->max_nr_ranges, could it write past the allocated array bounds?
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=8
Re: [PATCH v4 08/10] arm64: kdump: exclude non-dumpable reserved memory regions from vmcore
In reply to Wandun ChenQuoted 8 lines · Wandun Chen
From: Wandun Chen <[email protected]>
Reserved memory regions are excluded from vmcore by default unless marked dumpable. Honor the dumpable flag to filter out device firmware regions (e.g., GPU, DSP, modem) reserved via device tree, since they typically contain data not useful for kernel crash analysis and can significantly increase vmcore size.
Quoted 6 lines
Use
of_reserved_mem_kdump_exclude()to perform the exclusion, and pre-size thecrash_memarray viaof_reserved_mem_kdump_nr_ranges().Signed-off-by Wandun Chen <[email protected]>Tested-by Meijing Zhao <[email protected]>Acked-by Will Deacon <[email protected]>
[...]
Signature · 2 lines
Regards, Pratyush Yadav
PATCH v4 9/10 riscv: kdump: exclude non-dumpable reserved memory regions from vmcore
In reply to Wandun ChenFrom: Wandun Chen <[email protected]>
Apply the same non-dumpable reserved memory filtering to RISC-V kdump as was done for arm64. Use of_reserved_mem_kdump_exclude() to drop flagged regions from the elfcorehdr PT_LOAD segments, and of_reserved_mem_kdump_nr_ranges() to pre-size the crash_mem array.
--- arch/riscv/kernel/machine_kexec_file.c | 4 ++++ 1 file changed, 4 insertions(+)
arch/riscv/kernel/machine_kexec_file.c +4 -0
@@ -10,6 +10,7 @@#include <linux/elf.h> #include <linux/slab.h> #include <linux/of.h> +#include <linux/of_reserved_mem.h> #include <linux/libfdt.h> #include <linux/types.h> #include <linux/memblock.h>
@@ -64,6 +65,7 @@ static int prepare_elf_headers(void **addr, unsigned long *sz) nr_ranges =1 ;/* For exclusion of crashkernel region */ walk_system_ram_res (0 ,-1 , &nr_ranges, get_nr_ram_ranges_callback); + nr_ranges +=of_reserved_mem_kdump_nr_ranges (); cmem =kmalloc_flex (*cmem, ranges, nr_ranges);if (!cmem)
@@ -77,6 +79,8 @@ static int prepare_elf_headers(void **addr, unsigned long *sz)/* Exclude crashkernel region */ ret =crash_exclude_mem_range (cmem, crashk_res.start, crashk_res.end); +if (!ret) + ret =of_reserved_mem_kdump_exclude (cmem);if (!ret) ret =crash_prepare_elf64_headers (cmem, true, addr, sz);
Signature · 1 line
2.43.0
[email protected]
AI reviewRe: [PATCH v4 09/10] riscv: kdump: exclude non-dumpable reserved memory regions from vmcore
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
Pre-existing issues:
- [High] Concurrent memory hotplug between two walk_system_ram_res() calls creates a race condition leading to an out-of-bounds array write in prepare_elf64_ram_headers_callback(). --
commit a4a73b4654fd97d90e65198367a18f9dc30631b2
Author: Wandun Chen <[email protected]>
riscv: kdump: exclude non-dumpable reserved memory regions from vmcore
This commit applies non-dumpable reserved memory filtering to RISC-V kdump, similar to arm64. It excludes flagged regions from the elfcorehdr PT_LOAD segments and pre-sizes the crash_mem array accordingly.
Quoted 4 lines
arch/riscv/kernel/machine_kexec_file.c +0
-0
[ ... ]
arch/riscv/kernel/machine_kexec_file.c static int prepare_elf_headers(void **addr, unsigned long *sz) · line 71
Quoted 8 lines
arch/riscv/kernel/machine_kexec_file.c +1
-0@@ -64,6 +65,7 @@ static int prepare_elf_headers(void **addr, unsigned long *sz) nr_ranges =1 ;/* For exclusion of crashkernel region */ walk_system_ram_res (0 ,-1 , &nr_ranges, get_nr_ram_ranges_callback); + nr_ranges +=of_reserved_mem_kdump_nr_ranges (); cmem =kmalloc_flex (*cmem, ranges, nr_ranges);if (!cmem)
[Severity: High]
This is a pre-existing issue, but is it possible for a memory hotplug event to occur between counting the ranges here and when the array is actually populated?
In prepare_elf_headers(), the code counts the memory ranges to allocate the cmem array, but a concurrent memory hot-add could occur before the subsequent walk_system_ram_res() call populates it:
arch/riscv/kernel/machine_kexec_file.c:prepare_elf_headers() {
...
walk_system_ram_res(0, -1, &nr_ranges, get_nr_ram_ranges_callback);
...
ret = walk_system_ram_res(0, -1, cmem, prepare_elf64_ram_headers_callback);
...
}If the number of ranges increases, prepare_elf64_ram_headers_callback() will write past the end of the flexible array since it doesn't verify that cmem->nr_ranges is less than cmem->max_nr_ranges:
arch/riscv/kernel/machine_kexec_file.c:prepare_elf64_ram_headers_callback() {
...
cmem->ranges[cmem->nr_ranges].start = res->start;
cmem->ranges[cmem->nr_ranges].end = res->end;
cmem->nr_ranges++;
...
}Could this missing bounds check lead to an out-of-bounds heap write?
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=9
Re: [PATCH v4 09/10] riscv: kdump: exclude non-dumpable reserved memory regions from vmcore
In reply to Wandun ChenQuoted 33 lines · Wandun Chen
From: Wandun Chen <[email protected]>
Apply the same non-dumpable reserved memory filtering to RISC-V kdump as was done for arm64. Use
of_reserved_mem_kdump_exclude()to drop flagged regions from the elfcorehdrPT_LOADsegments, andof_reserved_mem_kdump_nr_ranges()to pre-size thecrash_memarray.Signed-off-by Wandun Chen <[email protected]>--- arch/riscv/kernel/machine_kexec_file.c | 4 ++++ 1 file changed, 4 insertions(+)arch/riscv/kernel/machine_kexec_file.c +2
-0@@ -10,6 +10,7 @@#include <linux/elf.h> #include <linux/slab.h> #include <linux/of.h> +#include <linux/of_reserved_mem.h> #include <linux/libfdt.h> #include <linux/types.h> #include <linux/memblock.h> @@ -64,6 +65,7 @@ static int prepare_elf_headers(void **addr, unsigned long *sz) nr_ranges =1 ;/* For exclusion of crashkernel region */ walk_system_ram_res (0 ,-1 , &nr_ranges, get_nr_ram_ranges_callback); + nr_ranges +=of_reserved_mem_kdump_nr_ranges (); cmem =kmalloc_flex (*cmem, ranges, nr_ranges);if (!cmem)
arch/riscv/kernel/machine_kexec_file.c static int prepare_elf_headers(void **addr, unsigned long *sz) · line 85
Quoted 7 lines
arch/riscv/kernel/machine_kexec_file.c +2
-0@@ -77,6 +79,8 @@ static int prepare_elf_headers(void **addr, unsigned long *sz)/* Exclude crashkernel region */ ret =crash_exclude_mem_range (cmem, crashk_res.start, crashk_res.end); +if (!ret) + ret =of_reserved_mem_kdump_exclude (cmem);if (!ret) ret =crash_prepare_elf64_headers (cmem, true, addr, sz);
Nit: can you do the usual pattern of if (err) goto err; instead?
So this would look like:
/* Exclude crashkernel region */
ret = crash_exclude_mem_range(cmem, crashk_res.start, crashk_res.end);
if (ret)
goto out;
ret = of_reserved_mem_kdump_exclude(cmem);
if (ret)
goto out;
ret = crash_prepare_elf64_headers(cmem, true, addr, sz);
out:
...With this,
Signature · 2 lines
Regards, Pratyush Yadav
Re: [PATCH v4 09/10] riscv: kdump: exclude non-dumpable reserved memory regions from vmcore
Quoted 36 lines · Pratyush Yadav
Quoted 34 lines · Wandun Chen
From: Wandun Chen <[email protected]>
Apply the same non-dumpable reserved memory filtering to RISC-V kdump as was done for arm64. Use
of_reserved_mem_kdump_exclude()to drop flagged regions from the elfcorehdrPT_LOADsegments, andof_reserved_mem_kdump_nr_ranges()to pre-size thecrash_memarray.Signed-off-by Wandun Chen <[email protected]>--- arch/riscv/kernel/machine_kexec_file.c | 4 ++++ 1 file changed, 4 insertions(+)arch/riscv/kernel/machine_kexec_file.c +2
-0@@ -10,6 +10,7 @@#include <linux/elf.h> #include <linux/slab.h> #include <linux/of.h> +#include <linux/of_reserved_mem.h> #include <linux/libfdt.h> #include <linux/types.h> #include <linux/memblock.h> @@ -64,6 +65,7 @@ static int prepare_elf_headers(void **addr, unsigned long *sz) nr_ranges =1 ;/* For exclusion of crashkernel region */ walk_system_ram_res (0 ,-1 , &nr_ranges, get_nr_ram_ranges_callback); + nr_ranges +=of_reserved_mem_kdump_nr_ranges (); cmem =kmalloc_flex (*cmem, ranges, nr_ranges);if (!cmem)@@ -77,6 +79,8 @@ static int prepare_elf_headers(void **addr, unsigned long *sz)
Quoted 9 lines
arch/riscv/kernel/machine_kexec_file.c static int prepare_elf_headers(void **addr, unsigned long *sz) · line 85
Quoted 6 lines
arch/riscv/kernel/machine_kexec_file.c +2
-0/* Exclude crashkernel region */ ret =crash_exclude_mem_range (cmem, crashk_res.start, crashk_res.end); +if (!ret) + ret =of_reserved_mem_kdump_exclude (cmem);if (!ret) ret =crash_prepare_elf64_headers (cmem, true, addr, sz);Nit: can you do the usual pattern of if (err) goto err; instead?
Sure, will fix in the next version.
Best regards,
Wandun
Quoted 19 lines
So this would look like:
/* Exclude crashkernel region */ ret = crash_exclude_mem_range(cmem, crashk_res.start, crashk_res.end); if (ret) goto out; ret = of_reserved_mem_kdump_exclude(cmem); if (ret) goto out; ret = crash_prepare_elf64_headers(cmem, true, addr, sz); out: ...With this,
Acked-by Pratyush Yadav <[email protected]>
PATCH v4 10/10 loongarch: kdump: exclude non-dumpable reserved memory regions from vmcore
In reply to Wandun ChenFrom: Wandun Chen <[email protected]>
Apply the same non-dumpable reserved memory filtering to LoongArch kdump as was done for arm64. Use of_reserved_mem_kdump_exclude() to drop flagged regions from the elfcorehdr PT_LOAD segments, and of_reserved_mem_kdump_nr_ranges() to pre-size the crash_mem array.
--- arch/loongarch/kernel/machine_kexec_file.c | 6 ++++++ 1 file changed, 6 insertions(+)
arch/loongarch/kernel/machine_kexec_file.c +6 -0
@@ -14,6 +14,7 @@#include <linux/kernel.h> #include <linux/kexec.h> #include <linux/memblock.h> +#include <linux/of_reserved_mem.h> #include <linux/slab.h> #include <linux/string.h> #include <linux/types.h>
@@ -67,6 +68,7 @@ static int prepare_elf_headers(void **addr, unsigned long *sz) nr_ranges =2 ;/* for exclusion of crashkernel region */ for_each_mem_range (i, &start, &end) nr_ranges++; + nr_ranges +=of_reserved_mem_kdump_nr_ranges (); cmem =kmalloc_flex (*cmem, ranges, nr_ranges);if (!cmem)
@@ -91,6 +93,10 @@ static int prepare_elf_headers(void **addr, unsigned long *sz) goto out; } + ret =of_reserved_mem_kdump_exclude (cmem); +if (ret) + goto out; + ret =crash_prepare_elf64_headers (cmem, true, addr, sz); out:
Signature · 1 line
2.43.0
[email protected]
AI reviewRe: [PATCH v4 10/10] loongarch: kdump: exclude non-dumpable reserved memory regions from vmcore
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
Pre-existing issues:
- [High] Concurrent memory hotplug during kexec_file_load() can cause an out-of-bounds write in the cmem->ranges array due to a TOCTOU between two lockless passes of for_each_mem_range(). --
commit d8948c2d581027666a36bbb47058ae244b39b8ae
Author: Wandun Chen <[email protected]>
loongarch: kdump: exclude non-dumpable reserved memory regions from vmcore
This commit applies the arm64 non-dumpable reserved memory filtering to LoongArch kdump. It uses of_reserved_mem_kdump_nr_ranges() to size the crash_mem array and of_reserved_mem_kdump_exclude() to drop regions.
Quoted 4 lines
arch/loongarch/kernel/machine_kexec_file.c +0
-0
[ ... ]
arch/loongarch/kernel/machine_kexec_file.c static int prepare_elf_headers(void **addr, unsigned long *sz) · line 74
Quoted 8 lines
arch/loongarch/kernel/machine_kexec_file.c +1
-0@@ -67,6 +68,7 @@ static int prepare_elf_headers(void **addr, unsigned long *sz) nr_ranges =2 ;/* for exclusion of crashkernel region */ for_each_mem_range (i, &start, &end) nr_ranges++; + nr_ranges +=of_reserved_mem_kdump_nr_ranges (); cmem =kmalloc_flex (*cmem, ranges, nr_ranges);if (!cmem)
[Severity: High]
This is a pre-existing issue, but can concurrent memory hotplug during kexec_file_load() cause an out-of-bounds write in the cmem->ranges array?
If the number of memory regions increases between the first lockless pass of for_each_mem_range() shown above, and the second lockless pass that populates the array later in prepare_elf_headers():
for_each_mem_range(i, &start, &end) {
cmem->ranges[cmem->nr_ranges].start = start;
cmem->ranges[cmem->nr_ranges].end = end - 1;
cmem->nr_ranges++;
}could the second pass write past the allocated cmem->ranges array?
Since kexec_file_load() takes kexec_mutex while memory hotplug uses mem_hotplug_lock, they can run concurrently. A concurrent addition via memblock_add_node() could result in writing beyond the bounds of the array. Should there be bounds checking when adding ranges to the array in the second pass?
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=10
Re: [PATCH v4 10/10] loongarch: kdump: exclude non-dumpable reserved memory regions from vmcore
In reply to Wandun ChenQuoted 8 lines · Wandun Chen
From: Wandun Chen <[email protected]>
Apply the same non-dumpable reserved memory filtering to LoongArch kdump as was done for arm64. Use
of_reserved_mem_kdump_exclude()to drop flagged regions from the elfcorehdrPT_LOADsegments, andof_reserved_mem_kdump_nr_ranges()to pre-size thecrash_memarray.Signed-off-by Wandun Chen <[email protected]>
[...]
Signature · 2 lines
Regards, Pratyush Yadav
Quoted 19 lines · Wandun Chen
From: Wandun Chen <[email protected]>
On SoCs that carve out large firmware-owned reserved memory (GPU firmware, DSP, modem, camera ISP, NPU, ...), kdump currently dumps those carveouts as part of system RAM even though their contents are firmware state that is not useful for kernel crash analysis.
This series introduces an opt-in 'dumpable' flag [1] on struct
reserved_memand uses it to filter the elfcorehdrPT_LOADranges on DT-based architectures (arm64, riscv, loongarch). By default reserved regions are treated as non-dumpable; CMA regions are explicitly opted in because their pages are returned to the buddy allocator and may carry key crash-analysis data.Since the reserved memory regions are filtered out, the vmcore is smaller in size and faster to produce. The reserved memory has already been filtered out in x86; the optimizations in this series apply to systems that use DTS such as arm64/riscv/loongarch.
Quoted 7 lines
The series is organized as follows:
Patches 1-4: Small prepare changes and cleanups.
Patches 5: Add dumpable flag to opt-in vmcore.
Patches 6: Append /memreserve/ entries into reserved_mem[]. Patch 7: Add generic kdump helpers.
Patches 8-10: Wire the helpers into arm64, riscv and loongarch kdump
elfcorehdr preparation.
Hi Rob,
Gentle ping on this series. Patches 01, 05 and 07-10 have been acked (thanks Pratyush, Will and Marek), and the comments there have been addressed in my local tree.
Patch 02/03/04/06 are the core of: reserved_mem changes, they are still pending review on the devicetree side.
Could you take a look when you have a chance? Happy to respin and address any feedback.
Best regards,
Wandun
Quoted 50 lines
v3 --> v4:
1. Rebase this series on v7.2-rc1.
2. Add two cleanup patches (patch 02/03).
3. Simplify patch 03 to avoid checking whetherinitial_boot_paramsis NULL multiple times, suggested by Rob.v2 --> v3:
1. Fix out-of-bounds issue if device tree lacks /reserved-memory node.[2]
2. Fix UAF issue whenalloc_reserved_mem_array()fails.
3. Add some prepare patches.v1 --> v2:
1. v1 added an opt-out DT property ('linux,no-dump'). Per Rob's feedback [1], v2 drop that property and exclude reserve memory by default.
2. Split some prepared patches from the original patches.
3. Address coding-style comments on patch 5 from Rob.[1] https://lore.kernel.org/lkml/[email protected]/
[2] https://sashiko.dev/#/patchset/20260520091844.592753-1-chenwandun%40lixiang.com?part=4Wandun Chen (10):
kexec/crash: providecrash_exclude_mem_range()stub when
CONFIG_CRASH_DUMP=n
of:reserved_mem: dedup and relocate reserved-memory messages
of:reserved_mem: skip late scan when no regions are reserved
of:reserved_mem: splitalloc_reserved_mem_array()from
fdt_scan_reserved_mem_late()
of:reserved_mem: add dumpable flag to opt-in vmcore
of:reserved_mem: save /memreserve/ entries into thereserved_mem
array
of:reserved_mem: add kdump helpers to exclude non-dumpable regions
arm64: kdump: exclude non-dumpable reserved memory regions from vmcore
riscv: kdump: exclude non-dumpable reserved memory regions from vmcore
loongarch: kdump: exclude non-dumpable reserved memory regions from
vmcorearch/arm64/kernel/machine_kexec_file.c | 6 ++ arch/loongarch/kernel/machine_kexec_file.c | 6 ++ arch/riscv/kernel/machine_kexec_file.c | 4 + drivers/of/fdt.c | 11 ++- drivers/of/of_private.h | 3 + drivers/of/of_reserved_mem.c | 108 ++++++++++++++++++--- include/linux/crash_core.h | 6 ++ include/linux/of_reserved_mem.h | 15 +++ kernel/dma/contiguous.c | 1 + 9 files changed, 142 insertions(+), 18 deletions(-)
Rob Herring
maintainerQuoted 6 lines · Wandun Chen
From: Wandun Chen <[email protected]>
On SoCs that carve out large firmware-owned reserved memory (GPU firmware, DSP, modem, camera ISP, NPU, ...), kdump currently dumps those carveouts as part of system RAM even though their contents are firmware state that is not useful for kernel crash analysis.
What about reserved regions on ACPI based systems?
Quoted 6 lines
This series introduces an opt-in 'dumpable' flag [1] on struct
reserved_memand uses it to filter the elfcorehdrPT_LOADranges on DT-based architectures (arm64, riscv, loongarch). By default reserved regions are treated as non-dumpable; CMA regions are explicitly opted in because their pages are returned to the buddy allocator and may carry key crash-analysis data.
I never like seeing the same change being made to each architecture. That's generally a sign of restructuring needed. loongarch and arm64 prepare_elf_headers() look about the same. riscv version uses walk_system_ram_res() for some reason.
Perhaps the dumpable flag belongs in memblock instead? Then at least we wouldn't need more DT APIs exposed to the arch code, and the code stays independent of the firmware API.
I'd really like Marek's review on the reserved memory code changes.
Rob
Quoted 9 lines · Rob Herring
Quoted 6 lines · Wandun Chen
From: Wandun Chen <[email protected]>
On SoCs that carve out large firmware-owned reserved memory (GPU firmware, DSP, modem, camera ISP, NPU, ...), kdump currently dumps those carveouts as part of system RAM even though their contents are firmware state that is not useful for kernel crash analysis.
What about reserved regions on ACPI based systems?
ACPI based systems already filter out these reserved regions from vmcore. In the EFI memory map, firmware carveouts are reported with the type EFI_RESERVED_TYPE. This type of memory gets the nomap flag set in memblock.memory (is_usable_memory() returns false in reserve_regions() and mark these regions with nomap flag), during prepare_elf_headers(), for_each_mem_range() filters out these nomap regions. Therefore, the memory reserved on ACPI based systems will not be dumped into the vmcore.
Quoted 8 lines
Quoted 7 lines
This series introduces an opt-in 'dumpable' flag [1] on struct
reserved_memand uses it to filter the elfcorehdrPT_LOADranges on DT-based architectures (arm64, riscv, loongarch). By default reserved regions are treated as non-dumpable; CMA regions are explicitly opted in because their pages are returned to the buddy allocator and may carry key crash-analysis data.
Quoted 8 lines
I never like seeing the same change being made to each architecture. That's generally a sign of restructuring needed. loongarch and arm64
prepare_elf_headers()look about the same. riscv version useswalk_system_ram_res()for some reason.Perhaps the dumpable flag belongs in memblock instead? Then at least we wouldn't need more DT APIs exposed to the arch code, and the code stays independent of the firmware API.
Agreed on both points. Adding the flag to memblock can indeed eliminate the duplicated code in prepare_elf_headers, and avoid expose APIs to the arch code.
I will add a dumpability marker to memblock for future extensibility and to avoid duplicating the same code across different architectures.
I'm inclined to add a nodump flag, similar to nomap, by default all memory is dumpable, and only the small fraction of memory that carries this flag would be excluded from the vmcore, What do you think about this approach?
walk_system_ram_res() in riscv could be reworked into the same form as in the arm64/loongarch architectures, will do in next version.
Quoted 2 lines
I'd really like Marek's review on the reserved memory code changes.
Marek, could you take a look when you have a chance about the reserved memory code changes, I'm happy to respin and address any feedback.
Quoted 2 lines
Rob
Best regards,
Wandun