kdump: reduce vmcore size and capture time
Subsystems (11)
v10 cover kdump: reduce vmcore size and capture time
From: Wandun Chen <[email protected]>
On SoCs that carve out large firmware-owned reserved memory (GPU, camera ISP, ...), 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.
ACPI systems already filter reserved memory out of the vmcore through their existing path: firmware reports those carveouts as unusable in the EFI memory map, they get MEMBLOCK_NOMAP set in memblock.memory (is_usable_memory(), efi-init.c), and the ELF header builder has always skipped nomap regions; only DT-based systems currently fail to filter these regions, which is what this series addresses.
This series introduces a MEMBLOCK_NODUMP flag (suggested by Rob) in memblock to filter reserved memory on DT-based architectures. A new flag is needed because MEMBLOCK_NOMAP can only express regions without a linear mapping, while MEMBLOCK_NODUMP can filter regions that are linearly mapped. Reserved regions are marked MEMBLOCK_NODUMP by default so kdump omits them; reusable CMA regions are different because their pages are handed back to the buddy allocator and may carry crash-relevant data.
Since the reserved memory regions are filtered out, the vmcore is smaller in size and faster to produce.
The series is based on linux-next and is organized as follows:
Patches 1-3: Preparation and bugfixes: switch riscv crash_mem to
memblock, fold the duplicated per-arch memblock walks into the weak defaults, and serialize crash header preparation against memory hotplug.
Patches 4-7: NODUMP infrastructure: switch crash_core to
for_each_mem_region(), introduce the MEMBLOCK_NODUMP flag, add a dumpable flag to struct reserved_mem, and mark /reserved-memory entries with MEMBLOCK_NODUMP.
Patch 8: Skip MEMBLOCK_NODUMP regions when building the vmcore
ELF header.
v9 --> v10:
1. Rebased on the latest linux-next, which includes the reserved-memory rework.
2. Patch 7: fix two issues found by Sashiko [1], by skipping the NODUMP marking entirely when early reservation fail and clamping the marked range to page boundaries.
The changes only tighten the conditions for marking NODUMP and the overall logic is unchanged, so the Acked-by and Reviewed-by tags are kept -- Marek, Rob, please let me know if you disagree and I will drop them.
3. The remaining x86-specific race in patch 3 found by Sashiko [2] (Xen balloon inserting a System RAM resource outside device_hotplug_lock) is pre-existing, and fixing it depends on resolving other pre-existing issues first, so it will be sent separately.
[1] https://sashiko.dev/#/message/[email protected]
[2] https://sashiko.dev/#/message/[email protected]
An Ack for patch 1 from the riscv maintainers is still pending:
I have pinged them, but no response yet. The Ack will be added once received.
v8 --> v9:
1. Patch 7: Avoid a potential boot panic on SH when NODUMP marking requires memblock resizing before it is allowed. SH supports neither CONFIG_KEXEC_FILE nor CONFIG_CRASH_HOTPLUG, so skip marking when both are disabled.
v7: https://lore.kernel.org/lkml/[email protected]/
v6: https://lore.kernel.org/lkml/[email protected]/
v5: https://lore.kernel.org/lkml/[email protected]/
v4: https://lore.kernel.org/lkml/[email protected]/
Wandun Chen (8):
riscv: build crash_mem ranges from memblock instead of resource tree
crash_core: fold duplicated memblock arch hooks into the weak default
crash_core: serialize crash header preparation against hotplug
crash_core: replace for_each_mem_range() with for_each_mem_region()
memblock: introduce MEMBLOCK_NODUMP flag
of: reserved_mem: add dumpable flag to opt-in vmcore
of: reserved_mem: mark /reserved-memory entries with MEMBLOCK_NODUMP
crash_core: skip MEMBLOCK_NODUMP regions when building vmcore ELF
header
arch/arm64/kernel/machine_kexec_file.c | 29 ------------ arch/loongarch/kernel/machine_kexec_file.c | 27 ----------- arch/riscv/Kconfig | 2 +- arch/riscv/kernel/crash.c | 2 +- arch/riscv/kernel/machine_kexec_file.c | 33 -------------- arch/x86/kernel/crash.c | 2 +- drivers/of/fdt.c | 2 + drivers/of/of_private.h | 1 + drivers/of/of_reserved_mem.c | 30 ++++++++++++ include/linux/crash_core.h | 2 + include/linux/memblock.h | 9 ++++ include/linux/of_reserved_mem.h | 1 + kernel/crash_core.c | 53 +++++++++++++++++++++- kernel/dma/contiguous.c | 1 + mm/memblock.c | 16 +++++++ 15 files changed, 116 insertions(+), 94 deletions(-)
Signature · 1 line
2.43.0
PATCH v10 1/8 riscv: build crash_mem ranges from memblock instead of resource tree
From: Wandun Chen <[email protected]>
Replace walk_system_ram_res() with for_each_mem_range(). A later patch introduces MEMBLOCK_NODUMP in memblock, the reserved-memory regions marked with MEMBLOCK_NODUMP can be excluded from the vmcore by walking memblock.
for_each_mem_range() iterates memblock.memory, which is freed after init unless ARCH_KEEP_MEMBLOCK is selected. riscv needs ARCH_KEEP_MEMBLOCK to filter reserved memory from the vmcore, so extend its condition (ACPI || KEXEC) with CRASH_DUMP. arm64 and loongarch already select ARCH_KEEP_MEMBLOCK unconditionally.
--- arch/riscv/Kconfig | 2 +- arch/riscv/kernel/machine_kexec_file.c | 31 ++++++++++---------------- 2 files changed, 13 insertions(+), 20 deletions(-)
arch/riscv/Kconfig +1 -1
@@ -58,7 +58,7 @@ config RISCV select ARCH_HAS_UBSAN select ARCH_HAS_VDSO_ARCH_DATA select ARCH_HAVE_NMI_SAFE_CMPXCHG - select ARCH_KEEP_MEMBLOCK if ACPI || KEXEC + select ARCH_KEEP_MEMBLOCK if ACPI || KEXEC || CRASH_DUMP select ARCH_MHP_MEMMAP_ON_MEMORY_ENABLE if 64BIT && MMU select ARCH_OPTIONAL_KERNEL_RWX if ARCH_HAS_STRICT_KERNEL_RWX select ARCH_OPTIONAL_KERNEL_RWX_DEFAULT
arch/riscv/kernel/machine_kexec_file.c +12 -19
@@ -37,39 +37,32 @@ int arch_kimage_file_post_load_cleanup(struct kimage *image) }#ifdef CONFIG_CRASH_DUMP -static int get_nr_ram_ranges_callback (struct resource *res,void *arg) -{ -unsigned int *nr_ranges = arg; - - (*nr_ranges)++; -return 0 ; -} -unsigned int arch_get_system_nr_ranges (void ) {unsigned int nr_ranges =2 + crashk_cma_cnt;/* For exclusion of crashkernel region */ +phys_addr_t start, end; +u64 i; -walk_system_ram_res (0 ,-1 , &nr_ranges, get_nr_ram_ranges_callback); +for_each_mem_range (i, &start, &end) + nr_ranges++;return nr_ranges; } -static int prepare_elf64_ram_headers_callback (struct resource *res,void *arg) +int arch_crash_populate_cmem (struct crash_mem *cmem) { -struct crash_mem *cmem = arg; +phys_addr_t start, end; +u64 i; - cmem->ranges[cmem->nr_ranges].start = res->start; - cmem->ranges[cmem->nr_ranges].end = res->end; - cmem->nr_ranges++; +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++; + }return 0 ; } -int arch_crash_populate_cmem (struct crash_mem *cmem) -{ -return walk_system_ram_res (0 ,-1 , cmem, prepare_elf64_ram_headers_callback); -} -static char *setup_kdump_cmdline (struct kimage *image,char *cmdline,unsigned long cmdline_len) {
Signature · 1 line
2.43.0
PATCH v10 2/8 crash_core: fold duplicated memblock arch hooks into the weak default
In reply to Wandun ChenFrom: Wandun Chen <[email protected]>
arm64, loongarch and riscv open-code the same memblock walk in arch_get_system_nr_ranges() and arch_crash_populate_cmem(). Move it into the __weak defaults in kernel/crash_core.c and delete the arch copies.
No functional change.
--- arch/arm64/kernel/machine_kexec_file.c | 29 ---------------------- arch/loongarch/kernel/machine_kexec_file.c | 27 -------------------- arch/riscv/kernel/machine_kexec_file.c | 26 ------------------- kernel/crash_core.c | 27 ++++++++++++++++++++ 4 files changed, 27 insertions(+), 82 deletions(-)
arch/arm64/kernel/machine_kexec_file.c +0 -29
@@ -14,7 +14,6 @@#include <linux/kernel.h> #include <linux/kexec.h> #include <linux/libfdt.h> -#include <linux/memblock.h> #include <linux/of.h> #include <linux/of_fdt.h> #include <linux/slab.h>
@@ -39,34 +38,6 @@ int arch_kimage_file_post_load_cleanup(struct kimage *image)return kexec_image_post_load_cleanup_default (image); } -#ifdef CONFIG_CRASH_DUMP -unsigned int arch_get_system_nr_ranges (void ) -{ -unsigned int nr_ranges =2 + crashk_cma_cnt;/* for exclusion of crashkernel region */ -phys_addr_t start, end; -u64 i; - -for_each_mem_range (i, &start, &end) - nr_ranges++; - -return nr_ranges; -} - -int arch_crash_populate_cmem (struct crash_mem *cmem) -{ -phys_addr_t start, end; -u64 i; - -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++; - } - -return 0 ; -} -#endif -/* * Tries to add the initrd and DTB to the image. If it is not possible to find * valid locations, this function will undo changes to the image and return non
arch/loongarch/kernel/machine_kexec_file.c +0 -27
@@ -13,7 +13,6 @@#include <linux/ioport.h> #include <linux/kernel.h> #include <linux/kexec.h> -#include <linux/memblock.h> #include <linux/slab.h> #include <linux/string.h> #include <linux/types.h>
@@ -56,32 +55,6 @@ static void cmdline_add_initrd(struct kimage *image, unsigned long *cmdline_tmpl }#ifdef CONFIG_CRASH_DUMP -unsigned int arch_get_system_nr_ranges (void ) -{ -int nr_ranges =2 ;/* for exclusion of crashkernel region */ -phys_addr_t start, end; -uint64_t i; - -for_each_mem_range (i, &start, &end) - nr_ranges++; - -return nr_ranges; -} - -int arch_crash_populate_cmem (struct crash_mem *cmem) -{ -phys_addr_t start, end; -uint64_t i; - -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++; - } - -return 0 ; -} -/* * Add the "mem=size@start" command line parameter to command line, indicating the * memory region the new kernel can use to boot into.
arch/riscv/kernel/machine_kexec_file.c +0 -26
@@ -37,32 +37,6 @@ int arch_kimage_file_post_load_cleanup(struct kimage *image) }#ifdef CONFIG_CRASH_DUMP -unsigned int arch_get_system_nr_ranges (void ) -{ -unsigned int nr_ranges =2 + crashk_cma_cnt;/* For exclusion of crashkernel region */ -phys_addr_t start, end; -u64 i; - -for_each_mem_range (i, &start, &end) - nr_ranges++; - -return nr_ranges; -} - -int arch_crash_populate_cmem (struct crash_mem *cmem) -{ -phys_addr_t start, end; -u64 i; - -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++; - } - -return 0 ; -} -static char *setup_kdump_cmdline (struct kimage *image,char *cmdline,unsigned long cmdline_len) {
kernel/crash_core.c +27 -0
@@ -281,8 +281,35 @@ static struct crash_mem *alloc_cmem(unsigned int nr_ranges)return cmem; } +#ifdef CONFIG_ARCH_KEEP_MEMBLOCK +unsigned int __weak arch_get_system_nr_ranges (void ) +{ +unsigned int nr_ranges =2 + crashk_cma_cnt;/* crashk_res + crashk_low_res, +CMA splits */ +phys_addr_t start, end; +u64 i; + +for_each_mem_range (i, &start, &end) + nr_ranges++; +return nr_ranges; +} + +int __weak arch_crash_populate_cmem (struct crash_mem *cmem) +{ +phys_addr_t start, end; +u64 i; + +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++; + } +return 0 ; +} +#else unsigned int __weak arch_get_system_nr_ranges (void ) {return 0 ; }int __weak arch_crash_populate_cmem (struct crash_mem *cmem) {return -1 ; } +#endif +int __weak arch_crash_exclude_ranges (struct crash_mem *cmem) {return 0 ; } int __weak arch_crash_exclude_mem_range(struct crash_mem **mem,
Signature · 1 line
2.43.0
PATCH v10 3/8 crash_core: serialize crash header preparation against hotplug
In reply to Wandun ChenFrom: Wandun Chen <[email protected]>
crash_prepare_headers() counts memory ranges before populating the allocated crash_mem array. The weak implementation used by ARM64, RISC-V and LoongArch walks memblock.memory, while x86 performs the same two-pass operation over system RAM resources. Concurrent memory hotplug can change range source between the two walks and make the populate pass overflow cmem->ranges.
Take device_hotplug_lock when preparing crash headers during kexec_file_load(). The x86/riscv memory hotplug path already takes device_hotplug_lock, so call __crash_prepare_headers() directly to avoid recursive locking.
Sashiko reported this issue in [1].
--- arch/riscv/kernel/crash.c | 2 +- arch/x86/kernel/crash.c | 2 +- include/linux/crash_core.h | 2 ++ kernel/crash_core.c | 17 +++++++++++++++-- 4 files changed, 19 insertions(+), 4 deletions(-)
arch/riscv/kernel/crash.c +1 -1
@@ -70,7 +70,7 @@ void arch_crash_handle_hotplug_event(struct kimage *image, void *arg)* Create the new elfcorehdr reflecting the changes to CPU and/or * memory resources. */ -if (crash_prepare_headers (true, &elfbuf, &elfsz,NULL )) { +if (__crash_prepare_headers (true, &elfbuf, &elfsz,NULL )) {pr_err ("unable to create new elfcorehdr" ); goto out; }
arch/x86/kernel/crash.c +1 -1
@@ -465,7 +465,7 @@ void arch_crash_handle_hotplug_event(struct kimage *image, void *arg)* Create the new elfcorehdr reflecting the changes to CPU and/or * memory resources. */ -if (crash_prepare_headers (IS_ENABLED (CONFIG_X86_64 ), &elfbuf, &elfsz,NULL )) { +if (__crash_prepare_headers (IS_ENABLED (CONFIG_X86_64 ), &elfbuf, &elfsz,NULL )) {pr_err ("unable to create new elfcorehdr" ); goto out; }
include/linux/crash_core.h +2 -0
@@ -61,6 +61,8 @@ extern int crash_prepare_elf64_headers(struct crash_mem *mem, int need_kernel_ma void **addr, unsigned long *sz);extern int crash_prepare_headers (int need_kernel_map,void **addr,unsigned long *sz,unsigned long *nr_mem_ranges); +int __crash_prepare_headers (int need_kernel_map,void **addr,unsigned long *sz, +unsigned long *nr_mem_ranges);extern int crash_exclude_core_ranges (struct crash_mem **cmem);struct kimage ;
kernel/crash_core.c +15 -2
@@ -16,6 +16,7 @@#include <linux/mm.h> #include <linux/cpuhotplug.h> #include <linux/memblock.h> +#include <linux/device.h> #include <linux/kmemleak.h> #include <linux/crash_core.h> #include <linux/reboot.h>
@@ -344,8 +345,8 @@ int crash_exclude_core_ranges(struct crash_mem **cmem)return 0 ; } -int crash_prepare_headers (int need_kernel_map,void **addr,unsigned long *sz, -unsigned long *nr_mem_ranges) +int __crash_prepare_headers (int need_kernel_map,void **addr,unsigned long *sz, +unsigned long *nr_mem_ranges) {unsigned int max_nr_ranges;struct crash_mem *cmem;
@@ -382,6 +383,18 @@ int crash_prepare_headers(int need_kernel_map, void **addr, unsigned long *sz,return ret; } +int crash_prepare_headers (int need_kernel_map,void **addr,unsigned long *sz, +unsigned long *nr_mem_ranges) +{ +int ret; + +lock_device_hotplug (); + ret =__crash_prepare_headers (need_kernel_map, addr, sz, nr_mem_ranges); +unlock_device_hotplug (); + +return ret; +} +/** * crash_exclude_mem_range - exclude a mem range for existing ranges * @mem: mem->range contains an array of ranges sorted in ascending order
Signature · 1 line
2.43.0
PATCH v10 4/8 crash_core: replace for_each_mem_range() with for_each_mem_region()
In reply to Wandun ChenFrom: Wandun Chen <[email protected]>
for_each_mem_range() skips MEMBLOCK_NOMAP regions implicitly. Switch the weak defaults to for_each_mem_region(), which exposes struct memblock_region and per-region flags, and filter NOMAP regions explicitly via the new crash_should_skip_region() helper. This prepares for a subsequent patch to extend the skip filter.
No functional change.
--- kernel/crash_core.c | 25 +++++++++++++++++-------- 1 file changed, 17 insertions(+), 8 deletions(-)
kernel/crash_core.c +17 -8
@@ -283,25 +283,34 @@ static struct crash_mem *alloc_cmem(unsigned int nr_ranges) }#ifdef CONFIG_ARCH_KEEP_MEMBLOCK +/* Exclude NOMAP regions from the vmcore. */ +static bool crash_should_skip_region (struct memblock_region *reg) +{ +return memblock_is_nomap (reg); +} +unsigned int __weak arch_get_system_nr_ranges (void ) {unsigned int nr_ranges =2 + crashk_cma_cnt;/* crashk_res + crashk_low_res, +CMA splits */ -phys_addr_t start, end; -u64 i; +struct memblock_region *reg; -for_each_mem_range (i, &start, &end) +for_each_mem_region (reg) { +if (crash_should_skip_region (reg)) +continue ; nr_ranges++; + }return nr_ranges; }int __weak arch_crash_populate_cmem (struct crash_mem *cmem) { -phys_addr_t start, end; -u64 i; +struct memblock_region *reg; -for_each_mem_range (i, &start, &end) { - cmem->ranges[cmem->nr_ranges].start = start; - cmem->ranges[cmem->nr_ranges].end = end -1 ; +for_each_mem_region (reg) { +if (crash_should_skip_region (reg)) +continue ; + cmem->ranges[cmem->nr_ranges].start = reg->base; + cmem->ranges[cmem->nr_ranges].end = reg->base + reg->size -1 ; cmem->nr_ranges++; }return 0 ;
Signature · 1 line
2.43.0
PATCH v10 5/8 memblock: introduce MEMBLOCK_NODUMP flag
In reply to Wandun ChenFrom: Wandun Chen <[email protected]>
Add MEMBLOCK_NODUMP to mark regions that should be excluded from kdump vmcores.
The flag is meant for reserved memory that carries no data useful for crash analysis. Reusable reserved regions such as CMA may hold useful data, so these regions must not be marked MEMBLOCK_NODUMP. Subsequent patches wire this up for /reserved-memory entries.
--- include/linux/memblock.h | 9 +++++++++ mm/memblock.c | 16 ++++++++++++++++ 2 files changed, 25 insertions(+)
include/linux/memblock.h +9 -0
@@ -52,6 +52,8 @@ extern unsigned long long max_possible_pfn;* know is good to use. It is the only memory that allocations may happen from * in this phase. * @MEMBLOCK_RSRV_HUGETLB: memory is reserved for hugetlb pages +* @MEMBLOCK_NODUMP: exclude from kdump vmcore. It carries no data useful +* for crash analysis (e.g. firmware carveouts). */ enum memblock_flags {MEMBLOCK_NONE =0x0 ,/* No special request */
@@ -63,6 +65,7 @@ enum memblock_flags {MEMBLOCK_RSRV_KERN =0x20 ,/* memory reserved for kernel use */ MEMBLOCK_KHO_NOPRSRV =0x40 ,/* memory with no KHO preservations */ MEMBLOCK_RSRV_HUGETLB =0x80 ,/* memory reserved for hugetlb pages */ + MEMBLOCK_NODUMP = 0x100,/* exclude from kdump vmcore */ };/**
@@ -160,6 +163,7 @@ int memblock_reserved_mark_noinit(phys_addr_t base, phys_addr_t size);int memblock_reserved_mark_kern (phys_addr_t base,phys_addr_t size);int memblock_mark_kho_noprsrv (phys_addr_t base,phys_addr_t size);int memblock_clear_kho_noprsrv (phys_addr_t base,phys_addr_t size); +int memblock_mark_nodump (phys_addr_t base,phys_addr_t size);void memblock_free (void *ptr,size_t size);
@@ -305,6 +309,11 @@ static inline bool memblock_is_kho_noprsrv(struct memblock_region *m)return m->flags &MEMBLOCK_KHO_NOPRSRV ; } +static inline bool memblock_is_nodump (struct memblock_region *m) +{ +return m->flags &MEMBLOCK_NODUMP ; +} +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,
mm/memblock.c +16 -0
@@ -1221,6 +1221,21 @@ __init int memblock_clear_kho_noprsrv(phys_addr_t base, phys_addr_t size) MEMBLOCK_KHO_NOPRSRV); } +/** +* memblock_mark_nodump - Mark a memory region with flag MEMBLOCK_NODUMP. +* @base: the base phys addr of the region +* @size: the size of the region +* +* Mark the region as not to be included in crash dumps. +* +* Return: 0 on success, -errno on failure. +*/ +int __init_memblock memblock_mark_nodump(phys_addr_t base, phys_addr_t size) +{ +return memblock_setclr_flag (&memblock.memory, base, size,1 , +MEMBLOCK_NODUMP ); +} +static bool should_skip_region (struct memblock_type *type,struct memblock_region *m,int nid,int flags)
@@ -2864,6 +2879,7 @@ static const char * const flagname[] = { [ilog2(MEMBLOCK_RSRV_KERN)] = "RSV_KERN", [ilog2(MEMBLOCK_KHO_NOPRSRV)] = "KHO_NOPRSRV", [ilog2(MEMBLOCK_RSRV_HUGETLB)] = "RSV_HUGETLB", + [ilog2(MEMBLOCK_NODUMP)] = "NODUMP", };static int memblock_debug_show (struct seq_file *m,void *private)
Signature · 1 line
2.43.0
PATCH v10 6/8 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 to decide whether a reserved area should be included in the kdump vmcore. Non-dumpable regions are later marked MEMBLOCK_NODUMP; the filtering itself happens in memblock, keeping the arch kdump code independent of the DT API.
Most reserved regions are owned by devices and do not contain data useful for kernel crash analysis, so dumpable default to false. Reusable CMA regions are different: their pages 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
@@ -555,6 +555,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 v10 7/8 of: reserved_mem: mark /reserved-memory entries with MEMBLOCK_NODUMP
In reply to Wandun ChenFrom: Wandun Chen <[email protected]>
Mark non-dumpable reserved-memory regions with MEMBLOCK_NODUMP so kdump can omit them from the vmcore.
The marking is guarded by CONFIG_CRASH_DUMP and either CONFIG_KEXEC_FILE or CONFIG_CRASH_HOTPLUG, so kernels that do not build or update crash ELF headers do not pay the cost of splitting memblock.memory entries at NODUMP boundaries.
Clamp the marked range to page boundaries, since a sub-page split would leave fragments that cannot be represented as valid System RAM resources [1].
--- drivers/of/fdt.c | 2 ++ drivers/of/of_private.h | 1 + drivers/of/of_reserved_mem.c | 30 ++++++++++++++++++++++++++++++ 3 files changed, 33 insertions(+)
drivers/of/fdt.c +2 -0
@@ -1283,6 +1283,8 @@ void __init unflatten_device_tree(void) {void *fdt = initial_boot_params; +fdt_mark_reserve_mem_nodump (); +/* Populate an empty root node when bootloader doesn't provide one */ if (!fdt) { fdt = (void *) __dtb_empty_root_begin;
drivers/of/of_private.h +1 -0
@@ -185,6 +185,7 @@ static inline struct device_node *__of_get_dma_parent(const struct device_node *#endif int fdt_scan_reserved_mem (void ); +void __init fdt_mark_reserve_mem_nodump (void );bool of_fdt_device_is_available (const void *blob,unsigned long node);
drivers/of/of_reserved_mem.c +30 -0
@@ -610,6 +610,36 @@ static void __init fdt_init_reserved_mem_node(unsigned long node, const char *un reserved_mem_count++; } +void __init fdt_mark_reserve_mem_nodump (void ) +{ +struct reserved_mem *rmem; +int i; + +if (!IS_ENABLED (CONFIG_CRASH_DUMP )) +return ; + +if (!IS_ENABLED (CONFIG_KEXEC_FILE ) && !IS_ENABLED (CONFIG_CRASH_HOTPLUG )) +return ; + +if (reserved_mem_reserve_failed) +return ; + +for (i =0 ; i < reserved_mem_count; i++) { +phys_addr_t base, end; + + rmem = &reserved_mem[i]; +if (!rmem->size || rmem->dumpable) +continue ; + + base =round_up (rmem->base,PAGE_SIZE ); + end =round_down (rmem->base + rmem->size,PAGE_SIZE ); +if (base >= end) +continue ; + +memblock_mark_nodump (base, end - base); + } +} +struct rmem_assigned_device {struct device *dev;struct reserved_mem *rmem;
Signature · 1 line
2.43.0
PATCH v10 8/8 crash_core: skip MEMBLOCK_NODUMP regions when building vmcore ELF header
In reply to Wandun ChenFrom: Wandun Chen <[email protected]>
Reserve regions (for GPU, DSP, ...) carry no data useful for crash analysis. Exclude MEMBLOCK_NODUMP regions from the vmcore ELF header to reduce vmcore size and capture time. crash_should_skip_region() now also returns true for MEMBLOCK_NODUMP.
--- kernel/crash_core.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
kernel/crash_core.c +2 -2
@@ -283,10 +283,10 @@ static struct crash_mem *alloc_cmem(unsigned int nr_ranges) }#ifdef CONFIG_ARCH_KEEP_MEMBLOCK -/* Exclude NOMAP regions from the vmcore. */ +/* Exclude NOMAP and NODUMP regions from the vmcore. */ static bool crash_should_skip_region (struct memblock_region *reg) { -return memblock_is_nomap (reg); +return memblock_is_nomap (reg) ||memblock_is_nodump (reg); }unsigned int __weak arch_get_system_nr_ranges (void )
Signature · 1 line
2.43.0
Re: [PATCH v10 3/8] crash_core: serialize crash header preparation against hotplug
In reply to Wandun ChenQuoted 74 lines · Wandun Chen
From: Wandun Chen <[email protected]>
crash_prepare_headers()counts memory ranges before populating the allocatedcrash_memarray. The weak implementation used by ARM64, RISC-V and LoongArch walks memblock.memory, while x86 performs the same two-pass operation over system RAM resources. Concurrent memory hotplug can change range source between the two walks and make the populate pass overflowcmem->ranges.Take
device_hotplug_lockwhen preparing crash headers duringkexec_file_load(). The x86/riscv memory hotplug path already takesdevice_hotplug_lock, so call__crash_prepare_headers()directly to avoid recursive locking.Sashiko reported this issue in [1].
Fixes 3751e728cef2 ("arm64: kexec_file: add crash dump support")Fixes 1bcca8620a91 ("LoongArch: Add crash dump support for kexec_file")Fixes 8acea455fafa ("RISC-V: Support for kexec_file on panic")Fixes dd5f726076cc ("kexec: support for kexec on panic using new system call")Signed-off-by Wandun Chen <[email protected]>Acked-by Baoquan He <[email protected]>--- arch/riscv/kernel/crash.c | 2 +- arch/x86/kernel/crash.c | 2 +- include/linux/crash_core.h | 2 ++ kernel/crash_core.c | 17 +++++++++++++++-- 4 files changed, 19 insertions(+), 4 deletions(-)arch/riscv/kernel/crash.c +1
-1@@ -70,7 +70,7 @@ void arch_crash_handle_hotplug_event(struct kimage *image, void *arg)* Create the new elfcorehdr reflecting the changes to CPU and/or * memory resources. */ -if (crash_prepare_headers (true, &elfbuf, &elfsz,NULL )) { +if (__crash_prepare_headers (true, &elfbuf, &elfsz,NULL )) {pr_err ("unable to create new elfcorehdr" ); goto out; }arch/x86/kernel/crash.c +1
-1@@ -465,7 +465,7 @@ void arch_crash_handle_hotplug_event(struct kimage *image, void *arg)* Create the new elfcorehdr reflecting the changes to CPU and/or * memory resources. */ -if (crash_prepare_headers (IS_ENABLED (CONFIG_X86_64 ), &elfbuf, &elfsz,NULL )) { +if (__crash_prepare_headers (IS_ENABLED (CONFIG_X86_64 ), &elfbuf, &elfsz,NULL )) {pr_err ("unable to create new elfcorehdr" ); goto out; }include/linux/crash_core.h +2
-0@@ -61,6 +61,8 @@ extern int crash_prepare_elf64_headers(struct crash_mem *mem, int need_kernel_ma void **addr, unsigned long *sz);extern int crash_prepare_headers (int need_kernel_map,void **addr,unsigned long *sz,unsigned long *nr_mem_ranges); +int __crash_prepare_headers (int need_kernel_map,void **addr,unsigned long *sz, +unsigned long *nr_mem_ranges);extern int crash_exclude_core_ranges (struct crash_mem **cmem);struct kimage ;kernel/crash_core.c +0
-0
kernel/crash_core.c line 22
Quoted 7 lines
kernel/crash_core.c +1
-0@@ -16,6 +16,7 @@#include <linux/mm.h> #include <linux/cpuhotplug.h> #include <linux/memblock.h> +#include <linux/device.h> #include <linux/kmemleak.h> #include <linux/crash_core.h> #include <linux/reboot.h>
The order of the header files is completely messed up now.
Quoted 15 lines
kernel/crash_core.c +2
-2@@ -344,8 +345,8 @@ int crash_exclude_core_ranges(struct crash_mem **cmem)return 0 ; } -int crash_prepare_headers (int need_kernel_map,void **addr,unsigned long *sz, -unsigned long *nr_mem_ranges) +int __crash_prepare_headers (int need_kernel_map,void **addr,unsigned long *sz, +unsigned long *nr_mem_ranges) {unsigned int max_nr_ranges;struct crash_mem *cmem;@@ -382,6 +383,18 @@ int crash_prepare_headers(int need_kernel_map, void **addr, unsigned long *sz,return ret; }
kernel/crash_core.c int crash_prepare_headers(int need_kernel_map, void **addr, unsigned long *sz, · line 395
Quoted 10 lines
kernel/crash_core.c +10
-0+int crash_prepare_headers (int need_kernel_map,void **addr,unsigned long *sz, +unsigned long *nr_mem_ranges) +{ +int ret; + +lock_device_hotplug (); + ret =__crash_prepare_headers (need_kernel_map, addr, sz, nr_mem_ranges); +unlock_device_hotplug (); + +return ret;
otherwise, LGTM
Quoted 5 lines
kernel/crash_core.c +2
-0+} +/** * crash_exclude_mem_range - exclude a mem range for existing ranges * @mem: mem->range contains an array of ranges sorted in ascending order
Re: [PATCH v10 1/8] riscv: build crash_mem ranges from memblock instead of resource tree
In reply to Wandun ChenQuoted 7 lines · Wandun Chen
From: Wandun Chen <[email protected]>
Replace
walk_system_ram_res()withfor_each_mem_range(). A later patch introducesMEMBLOCK_NODUMPin memblock, the reserved-memory regions marked withMEMBLOCK_NODUMPcan be excluded from the vmcore by walking memblock.
Quoted 5 lines
for_each_mem_range()iterates memblock.memory, which is freed after init unlessARCH_KEEP_MEMBLOCKis selected. riscv needsARCH_KEEP_MEMBLOCKto filter reserved memory from the vmcore, so extend its condition (ACPI || KEXEC) withCRASH_DUMP. arm64 and loongarch already selectARCH_KEEP_MEMBLOCKunconditionally.
LGTM
Quoted 78 lines
Signed-off-by Wandun Chen <[email protected]>Tested-by Meijing Zhao <[email protected]>Acked-by Baoquan He <[email protected]>--- arch/riscv/Kconfig | 2 +- arch/riscv/kernel/machine_kexec_file.c | 31 ++++++++++---------------- 2 files changed, 13 insertions(+), 20 deletions(-)arch/riscv/Kconfig +1
-1@@ -58,7 +58,7 @@ config RISCV select ARCH_HAS_UBSAN select ARCH_HAS_VDSO_ARCH_DATA select ARCH_HAVE_NMI_SAFE_CMPXCHG - select ARCH_KEEP_MEMBLOCK if ACPI || KEXEC + select ARCH_KEEP_MEMBLOCK if ACPI || KEXEC || CRASH_DUMP select ARCH_MHP_MEMMAP_ON_MEMORY_ENABLE if 64BIT && MMU select ARCH_OPTIONAL_KERNEL_RWX if ARCH_HAS_STRICT_KERNEL_RWX select ARCH_OPTIONAL_KERNEL_RWX_DEFAULTarch/riscv/kernel/machine_kexec_file.c +12
-19@@ -37,39 +37,32 @@ int arch_kimage_file_post_load_cleanup(struct kimage *image) }#ifdef CONFIG_CRASH_DUMP -static int get_nr_ram_ranges_callback (struct resource *res,void *arg) -{ -unsigned int *nr_ranges = arg; - - (*nr_ranges)++; -return 0 ; -} -unsigned int arch_get_system_nr_ranges (void ) {unsigned int nr_ranges =2 + crashk_cma_cnt;/* For exclusion of crashkernel region */ +phys_addr_t start, end; +u64 i; -walk_system_ram_res (0 ,-1 , &nr_ranges, get_nr_ram_ranges_callback); +for_each_mem_range (i, &start, &end) + nr_ranges++;return nr_ranges; } -static int prepare_elf64_ram_headers_callback (struct resource *res,void *arg) +int arch_crash_populate_cmem (struct crash_mem *cmem) { -struct crash_mem *cmem = arg; +phys_addr_t start, end; +u64 i; - cmem->ranges[cmem->nr_ranges].start = res->start; - cmem->ranges[cmem->nr_ranges].end = res->end; - cmem->nr_ranges++; +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++; + }return 0 ; } -int arch_crash_populate_cmem (struct crash_mem *cmem) -{ -return walk_system_ram_res (0 ,-1 , cmem, prepare_elf64_ram_headers_callback); -} -static char *setup_kdump_cmdline (struct kimage *image,char *cmdline,unsigned long cmdline_len) {
Re: [PATCH v10 2/8] crash_core: fold duplicated memblock arch hooks into the weak default
In reply to Wandun ChenQuoted 5 lines · Wandun Chen
From: Wandun Chen <[email protected]>
arm64, loongarch and riscv open-code the same memblock walk in
arch_get_system_nr_ranges()andarch_crash_populate_cmem(). Move it into the__weakdefaults inkernel/crash_core.cand delete the arch copies.
The code looks good, but shouldn't the commit message also mention x86 and loongarch? Loongarch does not implement crash CMA reservation, so crashk_cma_cnt is 0. x86 has its own arch hook implementation, so the __weak implementation will be overridden by the arch implementation.
otherwise, LGTM
Best regards,
Jinjie
Quoted 182 lines
No functional change.
Signed-off-by Wandun Chen <[email protected]>Tested-by Meijing Zhao <[email protected]>Acked-by Baoquan He <[email protected]>--- arch/arm64/kernel/machine_kexec_file.c | 29 ---------------------- arch/loongarch/kernel/machine_kexec_file.c | 27 -------------------- arch/riscv/kernel/machine_kexec_file.c | 26 ------------------- kernel/crash_core.c | 27 ++++++++++++++++++++ 4 files changed, 27 insertions(+), 82 deletions(-)arch/arm64/kernel/machine_kexec_file.c +0
-29@@ -14,7 +14,6 @@#include <linux/kernel.h> #include <linux/kexec.h> #include <linux/libfdt.h> -#include <linux/memblock.h> #include <linux/of.h> #include <linux/of_fdt.h> #include <linux/slab.h> @@ -39,34 +38,6 @@ int arch_kimage_file_post_load_cleanup(struct kimage *image)return kexec_image_post_load_cleanup_default (image); } -#ifdef CONFIG_CRASH_DUMP -unsigned int arch_get_system_nr_ranges (void ) -{ -unsigned int nr_ranges =2 + crashk_cma_cnt;/* for exclusion of crashkernel region */ -phys_addr_t start, end; -u64 i; - -for_each_mem_range (i, &start, &end) - nr_ranges++; - -return nr_ranges; -} - -int arch_crash_populate_cmem (struct crash_mem *cmem) -{ -phys_addr_t start, end; -u64 i; - -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++; - } - -return 0 ; -} -#endif -/* * Tries to add the initrd and DTB to the image. If it is not possible to find * valid locations, this function will undo changes to the image and return non arch/loongarch/kernel/machine_kexec_file.c +0
-27@@ -13,7 +13,6 @@#include <linux/ioport.h> #include <linux/kernel.h> #include <linux/kexec.h> -#include <linux/memblock.h> #include <linux/slab.h> #include <linux/string.h> #include <linux/types.h> @@ -56,32 +55,6 @@ static void cmdline_add_initrd(struct kimage *image, unsigned long *cmdline_tmpl }#ifdef CONFIG_CRASH_DUMP -unsigned int arch_get_system_nr_ranges (void ) -{ -int nr_ranges =2 ;/* for exclusion of crashkernel region */ -phys_addr_t start, end; -uint64_t i; - -for_each_mem_range (i, &start, &end) - nr_ranges++; - -return nr_ranges; -} - -int arch_crash_populate_cmem (struct crash_mem *cmem) -{ -phys_addr_t start, end; -uint64_t i; - -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++; - } - -return 0 ; -} -/* * Add the "mem=size@start" command line parameter to command line, indicating the * memory region the new kernel can use to boot into. arch/riscv/kernel/machine_kexec_file.c +0
-26@@ -37,32 +37,6 @@ int arch_kimage_file_post_load_cleanup(struct kimage *image) }#ifdef CONFIG_CRASH_DUMP -unsigned int arch_get_system_nr_ranges (void ) -{ -unsigned int nr_ranges =2 + crashk_cma_cnt;/* For exclusion of crashkernel region */ -phys_addr_t start, end; -u64 i; - -for_each_mem_range (i, &start, &end) - nr_ranges++; - -return nr_ranges; -} - -int arch_crash_populate_cmem (struct crash_mem *cmem) -{ -phys_addr_t start, end; -u64 i; - -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++; - } - -return 0 ; -} -static char *setup_kdump_cmdline (struct kimage *image,char *cmdline,unsigned long cmdline_len) {kernel/crash_core.c +27
-0@@ -281,8 +281,35 @@ static struct crash_mem *alloc_cmem(unsigned int nr_ranges)return cmem; } +#ifdef CONFIG_ARCH_KEEP_MEMBLOCK +unsigned int __weak arch_get_system_nr_ranges (void ) +{ +unsigned int nr_ranges =2 + crashk_cma_cnt;/* crashk_res + crashk_low_res, +CMA splits */ +phys_addr_t start, end; +u64 i; + +for_each_mem_range (i, &start, &end) + nr_ranges++; +return nr_ranges; +} + +int __weak arch_crash_populate_cmem (struct crash_mem *cmem) +{ +phys_addr_t start, end; +u64 i; + +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++; + } +return 0 ; +} +#else unsigned int __weak arch_get_system_nr_ranges (void ) {return 0 ; }int __weak arch_crash_populate_cmem (struct crash_mem *cmem) {return -1 ; } +#endif +int __weak arch_crash_exclude_ranges (struct crash_mem *cmem) {return 0 ; } int __weak arch_crash_exclude_mem_range(struct crash_mem **mem,
Hi Wandun,
Could you help review my below kexec rework?
https://lore.kernel.org/all/[email protected]/
Thanks
Jinjie
Quoted 106 lines · Wandun Chen
From: Wandun Chen <[email protected]>
On SoCs that carve out large firmware-owned reserved memory (GPU, camera ISP, ...), 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.
ACPI systems already filter reserved memory out of the vmcore through their existing path: firmware reports those carveouts as unusable in the EFI memory map, they get
MEMBLOCK_NOMAPset in memblock.memory (is_usable_memory(),efi-init.c), and the ELF header builder has always skipped nomap regions; only DT-based systems currently fail to filter these regions, which is what this series addresses.This series introduces a
MEMBLOCK_NODUMPflag (suggested by Rob) in memblock to filter reserved memory on DT-based architectures. A new flag is needed becauseMEMBLOCK_NOMAPcan only express regions without a linear mapping, whileMEMBLOCK_NODUMPcan filter regions that are linearly mapped. Reserved regions are markedMEMBLOCK_NODUMPby default so kdump omits them; reusable CMA regions are different because their pages are handed back to the buddy allocator and may carry crash-relevant data.Since the reserved memory regions are filtered out, the vmcore is smaller in size and faster to produce.
The series is based on linux-next and is organized as follows:
Patches 1-3: Preparation and bugfixes: switch riscv
crash_memto
memblock, fold the duplicated per-arch memblock walks into the weak defaults, and serialize crash header preparation against memory hotplug.
Patches 4-7: NODUMP infrastructure: switchcrash_coreto
for_each_mem_region(), introduce theMEMBLOCK_NODUMPflag, add a dumpable flag to structreserved_mem, and mark /reserved-memory entries withMEMBLOCK_NODUMP.
Patch 8: SkipMEMBLOCK_NODUMPregions when building the vmcore
ELF header.v9 --> v10:
1. Rebased on the latest linux-next, which includes the reserved-memory rework.2. Patch 7: fix two issues found by Sashiko [1], by skipping the NODUMP marking entirely when early reservation fail and clamping the marked range to page boundaries.
The changes only tighten the conditions for marking NODUMP and the overall logic is unchanged, so the Acked-by and Reviewed-by tags are kept -- Marek, Rob, please let me know if you disagree and I will drop them.
3. The remaining x86-specific race in patch 3 found by Sashiko [2] (Xen balloon inserting a System RAM resource outside
device_hotplug_lock) is pre-existing, and fixing it depends on resolving other pre-existing issues first, so it will be sent separately.[1] https://sashiko.dev/#/message/[email protected]
[2] https://sashiko.dev/#/message/[email protected]An Ack for patch 1 from the riscv maintainers is still pending:
I have pinged them, but no response yet. The Ack will be added once received.v8 --> v9:
1. Patch 7: Avoid a potential boot panic on SH when NODUMP marking requires memblock resizing before it is allowed. SH supports neitherCONFIG_KEXEC_FILEnorCONFIG_CRASH_HOTPLUG, so skip marking when both are disabled.v7: https://lore.kernel.org/lkml/[email protected]/
v6: https://lore.kernel.org/lkml/[email protected]/
v5: https://lore.kernel.org/lkml/[email protected]/
v4: https://lore.kernel.org/lkml/[email protected]/Wandun Chen (8):
riscv: buildcrash_memranges from memblock instead of resource tree
crash_core: fold duplicated memblock arch hooks into the weak default
crash_core: serialize crash header preparation against hotplug
crash_core: replacefor_each_mem_range()withfor_each_mem_region()
memblock: introduceMEMBLOCK_NODUMPflag
of:reserved_mem: add dumpable flag to opt-in vmcore
of:reserved_mem: mark /reserved-memory entries withMEMBLOCK_NODUMP
crash_core: skipMEMBLOCK_NODUMPregions when building vmcore ELF
headerarch/arm64/kernel/machine_kexec_file.c | 29 ------------ arch/loongarch/kernel/machine_kexec_file.c | 27 ----------- arch/riscv/Kconfig | 2 +- arch/riscv/kernel/crash.c | 2 +- arch/riscv/kernel/machine_kexec_file.c | 33 -------------- arch/x86/kernel/crash.c | 2 +- drivers/of/fdt.c | 2 + drivers/of/of_private.h | 1 + drivers/of/of_reserved_mem.c | 30 ++++++++++++ include/linux/crash_core.h | 2 + include/linux/memblock.h | 9 ++++ include/linux/of_reserved_mem.h | 1 + kernel/crash_core.c | 53 +++++++++++++++++++++- kernel/dma/contiguous.c | 1 + mm/memblock.c | 16 +++++++ 15 files changed, 116 insertions(+), 94 deletions(-)
Re: [PATCH v10 4/8] crash_core: replace for_each_mem_range() with for_each_mem_region()
In reply to Wandun ChenQuoted 22 lines · Wandun Chen
From: Wandun Chen <[email protected]>
for_each_mem_range()skipsMEMBLOCK_NOMAPregions implicitly. Switch the weak defaults tofor_each_mem_region(), which exposes structmemblock_regionand per-region flags, and filter NOMAP regions explicitly via the newcrash_should_skip_region()helper. This prepares for a subsequent patch to extend the skip filter.No functional change.
Signed-off-by Wandun Chen <[email protected]>Tested-by Meijing Zhao <[email protected]>Acked-by Baoquan He <[email protected]>--- kernel/crash_core.c | 25 +++++++++++++++++-------- 1 file changed, 17 insertions(+), 8 deletions(-)kernel/crash_core.c +0
-0
kernel/crash_core.c static struct crash_mem *alloc_cmem(unsigned int nr_ranges) · line 289
Quoted 7 lines
kernel/crash_core.c +4
-0@@ -283,25 +283,34 @@ static struct crash_mem *alloc_cmem(unsigned int nr_ranges) }#ifdef CONFIG_ARCH_KEEP_MEMBLOCK +/* Exclude NOMAP regions from the vmcore. */ +static bool crash_should_skip_region (struct memblock_region *reg) +{ +return memblock_is_nomap (reg);
LGTM
Quoted 35 lines
kernel/crash_core.c +13
-8+} +unsigned int __weak arch_get_system_nr_ranges (void ) {unsigned int nr_ranges =2 + crashk_cma_cnt;/* crashk_res + crashk_low_res, +CMA splits */ -phys_addr_t start, end; -u64 i; +struct memblock_region *reg; -for_each_mem_range (i, &start, &end) +for_each_mem_region (reg) { +if (crash_should_skip_region (reg)) +continue ; nr_ranges++; + }return nr_ranges; }int __weak arch_crash_populate_cmem (struct crash_mem *cmem) { -phys_addr_t start, end; -u64 i; +struct memblock_region *reg; -for_each_mem_range (i, &start, &end) { - cmem->ranges[cmem->nr_ranges].start = start; - cmem->ranges[cmem->nr_ranges].end = end -1 ; +for_each_mem_region (reg) { +if (crash_should_skip_region (reg)) +continue ; + cmem->ranges[cmem->nr_ranges].start = reg->base; + cmem->ranges[cmem->nr_ranges].end = reg->base + reg->size -1 ; cmem->nr_ranges++; }return 0 ;
[email protected]
AI reviewRe: [PATCH v10 7/8] of: reserved_mem: mark /reserved-memory entries with MEMBLOCK_NODUMP
In reply to Wandun ChenThank you for your contribution! Sashiko AI review found 2 potential issues to consider: - [High] of: reserved_mem: sub-page memblock split in fdt_mark_reserve_mem_nodump() - [Medium] of…
Thank you for your contribution! Sashiko AI review found 2 potential issues to consider:
- [High] of: reserved_mem: sub-page memblock split in fdt_mark_reserve_mem_nodump()
- [Medium] of: reserved_mem: early nodump split in unflatten_device_tree() before kasan
--
commit 47a5a6000f1f57782ed8a71e5ff4a13ee231c6bd
Author: Wandun Chen <[email protected]>
of: reserved_mem: mark /reserved-memory entries with MEMBLOCK_NODUMP
This commit adds fdt_mark_reserve_mem_nodump() and calls it from unflatten_device_tree() to mark page-clamped non-dumpable /reserved-memory regions with MEMBLOCK_NODUMP.
drivers/of/fdt.c void __init unflatten_device_tree(void) · line 1290
Quoted 13 lines
drivers/of/fdt.c +2
-0@@ -1283,6 +1283,8 @@ void __init unflatten_device_tree(void) {void *fdt = initial_boot_params; +fdt_mark_reserve_mem_nodump (); +/* Populate an empty root node when bootloader doesn't provide one */ if (!fdt) { fdt = (void *) __dtb_empty_root_begin;
[Severity: Medium]
Does calling fdt_mark_reserve_mem_nodump() in unflatten_device_tree() split memblock.memory too early for kasan_init() and kvm_hyp_reserve()?
Mapped (!no-map) reserved-memory regions were previously only recorded in memblock.reserved, keeping memblock.memory contiguous. Splitting memblock.memory with MEMBLOCK_NODUMP at PAGE_SIZE boundaries before the rest of setup_arch() causes for_each_mem_range() and for_each_mem_region() to iterate over the split ranges separately.
On arm64 and loongarch, when a mapped non-dumpable region boundary is PAGE_SIZE-aligned but not aligned to PAGE_SIZE << KASAN_SHADOW_SCALE_SHIFT (e.g. 32 KiB or 64 KiB on 4 KiB pages), the two adjacent regions share a KASAN shadow page. Because kasan_pte_populate() checks pte_none() only in its do-while loop condition after the first iteration, the second region allocates a duplicate shadow page, overwrites the PTE, and leaks the first page:
arch/arm64/mm/kasan_init.c:kasan_pte_populate() {
...
do {
phys_addr_t page_phys = early ?
__pa_symbol(kasan_early_shadow_page)
: kasan_alloc_raw_page(node);
if (!early)
memset(__va(page_phys), KASAN_SHADOW_INIT, PAGE_SIZE);
next = addr + PAGE_SIZE;
__set_pte(ptep, pfn_pte(__phys_to_pfn(page_phys), PAGE_KERNEL));
} while (ptep++, addr = next, addr != end && pte_none(__ptep_get(ptep)));
}On riscv, splitting 16 MiB physical ranges before kasan_init() prevents kasan_populate_pmd() from mapping a full 2 MiB shadow PMD, falling back to 4 KiB PTEs. On arm64 with pKVM, splitting memblock.memory before bootmem_init() -> kvm_hyp_reserve() consumes extra slots in the fixed 128-entry hyp_memory[] array and increases per-region hypervisor page-table and vmemmap reservations.
[ ... ]
Quoted 27 lines
drivers/of/of_reserved_mem.c +19
-0@@ -610,6 +610,36 @@ static void __init fdt_init_reserved_mem_node(unsigned long node, const char *un reserved_mem_count++; } +void __init fdt_mark_reserve_mem_nodump (void ) +{ +struct reserved_mem *rmem; +int i; + +if (!IS_ENABLED (CONFIG_CRASH_DUMP )) +return ; + +if (!IS_ENABLED (CONFIG_KEXEC_FILE ) && !IS_ENABLED (CONFIG_CRASH_HOTPLUG )) +return ; + +if (reserved_mem_reserve_failed) +return ; + +for (i =0 ; i < reserved_mem_count; i++) { +phys_addr_t base, end; + + rmem = &reserved_mem[i]; +if (!rmem->size || rmem->dumpable)
drivers/of/of_reserved_mem.c static void __init fdt_init_reserved_mem_node(unsigned long node, const char *un · line 641
Quoted 10 lines
drivers/of/of_reserved_mem.c +10
-0+continue ; + + base =round_up (rmem->base,PAGE_SIZE ); + end =round_down (rmem->base + rmem->size,PAGE_SIZE ); +if (base >= end) +continue ; + +memblock_mark_nodump (base, end - base); + } +}
[Severity: High]
Can clamping each region to PAGE_SIZE here leave sub-page fragments in memblock.memory when a no-map region or an adjacent region is not page-aligned?
During fdt_scan_reserved_mem(), no-map regions are marked with MEMBLOCK_NOMAP in memblock.memory at byte granularity (for example, cont_splash_mem in arch/arm64/boot/dts/exynos/exynos2200-g0s.dts has size 0x9a3f80, and 4 KiB-aligned no-map regions are unaligned under 16 KiB or 64 KiB PAGE_SIZE). Because fdt_mark_reserve_mem_nodump() does not skip MEMBLOCK_NOMAP regions, calling memblock_mark_nodump(base, end - base) on the page-clamped interior range gives [base, end) flags MEMBLOCK_NOMAP | MEMBLOCK_NODUMP while leaving the unaligned head [rmem->base, base) and tail [end, rmem->base + rmem->size) as sub-page regions with only MEMBLOCK_NOMAP.
Similarly, if a mapped non-dumpable region abuts an unaligned no-map region, clamping inward to PAGE_SIZE leaves a sub-page MEMBLOCK_NONE fragment between the two regions; and if two adjacent non-dumpable regions meet at a sub-page boundary, clamping each entry before coalescing leaves their shared page unmarked.
On riscv, init_resources() converts every memblock.memory region (including MEMBLOCK_NOMAP) using PFN_UP for start and PFN_DOWN - 1 for end:
arch/riscv/kernel/setup.c:init_resources() {
...
for_each_mem_region(region) {
res = &mem_res[res_idx--];
non_resv_res++;
if (unlikely(memblock_is_nomap(region))) {
res->name = "Reserved";
res->flags = IORESOURCE_MEM | IORESOURCE_EXCLUSIVE;
} else {
res->name = "System RAM";
res->flags = IORESOURCE_SYSTEM_RAM | IORESOURCE_BUSY;
}
res->start = __pfn_to_phys(memblock_region_memory_base_pfn(region));
res->end = __pfn_to_phys(memblock_region_memory_end_pfn(region)) - 1;
ret = add_resource(&iomem_resource, res);
if (ret < 0)
goto error;
}
...
}Any sub-page fragment produces res->start > res->end, which fails add_resource() and tears down iomem_resource. On arm64 and loongarch, a sub-page MEMBLOCK_NONE fragment likewise produces res->start > res->end in request_standard_resources() / resource_init(), and an unaligned no-map region split by MEMBLOCK_NODUMP is broken into multiple reserved entries in /proc/iomem.
Since crash_should_skip_region() already skips MEMBLOCK_NOMAP regions, skipping no-map entries and coalescing adjacent non-dumpable ranges before clamping to page boundaries would avoid creating sub-page MEMBLOCK_NOMAP splits and unmarked shared pages.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=7
[email protected]
AI reviewSashiko has finished reviewing this 8-patch series:
Sashiko has finished reviewing this 8-patch series:
- Patches 1-6 and 8: no issues found.
- Patch 7 ("of: reserved_mem: mark /reserved-memory entries with..."): 2 potential issues (1 High, 1 Medium)
A detailed review has been sent as a separate reply to patch 7.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]