* [PATCH v3 00/17] crash: Rework and add arm64 crash hotplug support
@ 2026-08-26 9:25 Jinjie Ruan
2026-08-26 9:25 ` [PATCH v3 01/17] kexec: Record allocated CMA pages to fix release size mismatch Jinjie Ruan
` (16 more replies)
0 siblings, 17 replies; 19+ messages in thread
From: Jinjie Ruan @ 2026-08-26 9:25 UTC (permalink / raw)
To: catalin.marinas, will, mark.rutland, chenhuacai, kernel, maddy,
mpe, npiggin, chleroy, tglx, mingo, bp, dave.hansen, hpa, akpm,
baoquan.he, rppt, pasha.tatashin, pratyush, ruirui.yang, kees,
thuth, gshan, jic23, james.morse, ardb, leitao, yeoreum.yun,
sourabhjain, coxu, tangyouling, hbathini, adityag, ionut.nechita,
liaoyuanhong, seanjc, fuqiang.wang, makb, piliu, ebiggers,
jbouron, mclapinski, me, graf, bgwin, robh, takahiro.akashi,
palmer, x86, linux-arm-kernel, linux-kernel, loongarch,
linuxppc-dev, kexec, linux-fsdevel, linux-mm
Cc: ruanjinjie
When CPU or memory hotplug events occur, the elfcorehdr in the kdump
image becomes stale, potentially leading to incomplete crash dumps.
Currently, userspace udev rules reload the entire kdump image upon such
events, which is inefficient and leaves kdump inactive for a long time.
Commit 247262756121 ("crash: add generic infrastructure for crash hotplug
support") introduced a kernel mechanism to update only the elfcorehdr.
This patch set implements crash hotplug support for arm64.
As Baoquan and Catalin suggested, it also addresses and fixes several
pre-existing code issues found by Sashiko AI [1][2].
The major improvements and fixes included in this series are:
- Fix several memory leaks for arm64, and similar issues on LoongArch.
- Fix TOCTOU race in crash memory range collection
- Simplify x86 related code.
- Simplify arm64 load_other_segments().
- Implement infrastructure for arm64 crash memory hotplug support.
This patch set is rebased on x86 fix [3].
TESTING
=======
Only kexec_file_load path has been tested; kexec_load is expected to
work via KEXEC_CRASH_HOTPLUG_SUPPORT flag but not yet verified.
Tested on an arm64 guest using KVM-QEMU[4] with the following
configuration:
-M virt,acpi=on,highmem=on
-m 4G,slots=256,maxmem=16G
-smp cpus=4,maxcpus=8,cores=4,threads=2,sockets=1
1. Memory Hot-Add Test
[Step 1] Load kexec first:
./kexec --kexec-file-syscall ...
[Step 2] Hotplug and online 128M memory and trigger crash:
(qemu) object_add memory-backend-ram,id=mem1,size=128M
(qemu) device_add pc-dimm,id=dimm3,memdev=mem1,addr=0x160000000
echo 1 > /sys/devices/system/memory/memory44/online
echo c > /proc/sysrq-trigger
[Step 3] Verify vmcore layout in the secondary kernel:
readelf -l /proc/vmcore
The newly added 128M memory segment (0x160000000) is successfully
recognized and populated as a LOAD segment:
LOAD 0x... 0x0000000160000000 0x08000000 0x08000000 RWE 0x0
2. Memory Hot-Remove Test
[Step 1] Add memory device and online 128M memory first:
(qemu) device_add pc-dimm,id=dimm3,memdev=mem1,addr=0x160000000
echo 1 > /sys/devices/system/memory/memory44/online
[Step 2] Load kexec, offline memory, and crash:
./kexec --kexec-file-syscall ...
echo 0 > /sys/devices/system/memory/memory44/online
echo c > /proc/sysrq-trigger
[Step 3] Verify vmcore layout:
readelf -l /proc/vmcore
Result: The 0x160000000 segment is cleanly excluded from the vmcore
program headers, and the dump completes without any hang.
3. CPU Hot-Add Test
[Step 1] Load kexec first:
./kexec --kexec-file-syscall ...
[Step 2] hotplug and online one CPU, then crash:
(qemu) device_add driver=host-arm-cpu,core-id=2,thread-id=0,id=cpu4
echo 1 > /sys/devices/system/cpu/cpu4/online
echo c > /proc/sysrq-trigger
[Step 3] Verify notes count:
readelf -n /proc/vmcore | grep -w CORE | wc -l
5
Result: Crash hotplug responds correctly; the newly plugged CPU4 is
tracked, and 5 NT_PRSTATUS notes are generated.
4. CPU Hot-Remove Test
[Step 1] Add CPU device and online it first:
(qemu) device_add driver=host-arm-cpu,core-id=2,thread-id=0,id=cpu4
echo 1 > /sys/devices/system/cpu/cpu4/online
[Step 2] Load kexec, remove CPU, and crash:
./kexec --kexec-file-syscall ...
(qemu) device_del cpu4
echo c > /proc/sysrq-trigger
[Step 3] Verify notes count:
readelf -n /proc/vmcore | grep -w CORE | wc -l
4
Result: Crash hotplug automatically updates the headers upon CPU
eviction; only 4 online CPUs are registered in the vmcore.
[1]: https://lore.kernel.org/all/20260601094805.2928614-1-ruanjinjie@huawei.com/
[2]: https://sashiko.dev/#/patchset/20260729031235.2840255-1-ruanjinjie%40huawei.com
[3]: https://lore.kernel.org/all/20260826073527.21487-2-ionut.nechita@windriver.com/
[4]: https://github.com/salil-mehta/qemu.git virt-cpuhp-armv8/rfc-v2
Changes in v3:
- Handle "KEXEC_CRASH_HP_REMOVE_MEMORY" action.
- Fix several pre-existing code issues reported by Sashiko AI review [3].
- Introduce crash_extra_elfcorehdr_size() and elf64_phdr_size() helper.
- Rework related crash and arch code.
- Add test method.
- v2: https://lore.kernel.org/all/20260729031235.2840255-1-ruanjinjie@huawei.com/
Changes in v2:
- Split out Powerpc bugfix patch as Mike suggested.
- Use phys_to_virt() instead of __va() in update_crash_elfcorehdr().
- Convert pnum_hdr_sz() to a function.
- Only assign elfcorehdr_index after kexec_add_buffer succeeds, considering
crash_handle_hotplug_event() already performs validity check on
elfcorehdr_index:
- We can safely remove the check for CPU hotplug
in arch_crash_handle_hotplug_event().
- The elfcorehdr_index's segment mem will be valid in
update_crash_elfcorehdr(), so we can safely remove the NULL check.
- Simplify the commit message.
- v1: https://lore.kernel.org/all/20260723131242.1537633-1-ruanjinjie@huawei.com/#t
Jinjie Ruan (17):
kexec: Record allocated CMA pages to fix release size mismatch
kexec: Extract kexec_free_segment_cma() from kimage_free_cma()
arm64: kexec_file: Fix CMA page leaks in segment placement retry loops
arm64: kexec_file: Fix elf_headers memory leak in retry loop
LoongArch: kexec: Fix CMA page leaks in segment placement retry loops
LoongArch: kexec_file: Fix elf_headers memory leak in retry loop
crash_dump: Fix potential double-free of keys_header
crash: Extract crash_get_memory_ranges() helper
crash: Fix TOCTOU race in crash memory range collection
elf: Introduce elf64_phdr_size() helper
crash: Introduce crash_extra_elfcorehdr_size() helper
x86/crash: Use num_possible_cpus() for elfcorehdr size
crash: Improve elfcorehdr segment identification
x86/crash: Simplify crash_load_segments() using
crash_extra_elfcorehdr_size()
crash: Simplify CRASH_MAX_MEMORY_RANGES handling
arm64: kexec_file: Simplify load_other_segments()
arm64: crash: Add crash hotplug support
arch/arm64/Kconfig | 3 +
arch/arm64/include/asm/kexec.h | 11 ++
arch/arm64/kernel/Makefile | 2 +-
arch/arm64/kernel/crash.c | 165 +++++++++++++++++++++
arch/arm64/kernel/kexec_image.c | 1 +
arch/arm64/kernel/machine_kexec_file.c | 57 ++-----
arch/loongarch/kernel/kexec_efi.c | 1 +
arch/loongarch/kernel/machine_kexec_file.c | 10 +-
arch/powerpc/kexec/crash.c | 6 +-
arch/powerpc/kexec/file_load_64.c | 19 +--
arch/powerpc/platforms/powernv/opal-core.c | 3 +-
arch/x86/kernel/crash.c | 46 ++----
fs/proc/vmcore.c | 6 +-
include/linux/crash_core.h | 27 ++++
include/linux/elf.h | 4 +
include/linux/kexec.h | 3 +
kernel/Kconfig.kexec | 3 +-
kernel/crash_core.c | 57 ++++++-
kernel/crash_dump_dm_crypt.c | 15 +-
kernel/kexec_core.c | 26 ++--
kernel/kexec_file.c | 16 +-
21 files changed, 352 insertions(+), 129 deletions(-)
create mode 100644 arch/arm64/kernel/crash.c
--
2.34.1
^ permalink raw reply [flat|nested] 19+ messages in thread
* [PATCH v3 01/17] kexec: Record allocated CMA pages to fix release size mismatch
2026-08-26 9:25 [PATCH v3 00/17] crash: Rework and add arm64 crash hotplug support Jinjie Ruan
@ 2026-08-26 9:25 ` Jinjie Ruan
2026-08-26 9:25 ` [PATCH v3 02/17] kexec: Extract kexec_free_segment_cma() from kimage_free_cma() Jinjie Ruan
` (15 subsequent siblings)
16 siblings, 0 replies; 19+ messages in thread
From: Jinjie Ruan @ 2026-08-26 9:25 UTC (permalink / raw)
To: catalin.marinas, will, mark.rutland, chenhuacai, kernel, maddy,
mpe, npiggin, chleroy, tglx, mingo, bp, dave.hansen, hpa, akpm,
baoquan.he, rppt, pasha.tatashin, pratyush, ruirui.yang, kees,
thuth, gshan, jic23, james.morse, ardb, leitao, yeoreum.yun,
sourabhjain, coxu, tangyouling, hbathini, adityag, ionut.nechita,
liaoyuanhong, seanjc, fuqiang.wang, makb, piliu, ebiggers,
jbouron, mclapinski, me, graf, bgwin, robh, takahiro.akashi,
palmer, x86, linux-arm-kernel, linux-kernel, loongarch,
linuxppc-dev, kexec, linux-fsdevel, linux-mm
Cc: ruanjinjie
The CMA pages allocated for a kexec segment are released using the
segment's memsz to calculate the number of pages. However, some
architecture loaders modify the segment's memsz after allocation
(e.g. arm64 subtracts text_offset), causing the release function to
free fewer pages than were originally allocated, leaking the remaining
CMA pages.
Add a per-segment `segment_cma_pages` array to store the number of
pages actually allocated from CMA. Populate it during
kexec_add_buffer() using the aligned memsz, and use it in
kimage_free_cma() to accurately release all allocated pages.
This avoids relying on the potentially modified segment->memsz and
prevents silent CMA memory leaks.
Cc: Andrew Morton <akpm@linux-foundation.org>
Cc: Baoquan He <baoquan.he@linux.dev>
Cc: Mike Rapoport <rppt@kernel.org>
Cc: Pasha Tatashin <pasha.tatashin@soleen.com>
Cc: Pratyush Yadav <pratyush@kernel.org>
Cc: Brian Mak <makb@juniper.net>
Cc: Pingfan Liu <piliu@redhat.com>
Cc: Sourabh Jain <sourabhjain@linux.ibm.com>
Cc: Justinien Bouron <jbouron@amazon.com>
Cc: Li Chen <me@linux.beauty>
Cc: stable@vger.kernel.org
Link: https://sashiko.dev/#/patchset/20260729031235.2840255-1-ruanjinjie%40huawei.com
Fixes: 07d24902977e ("kexec: enable CMA based contiguous allocation")
Signed-off-by: Jinjie Ruan <ruanjinjie@huawei.com>
---
include/linux/kexec.h | 1 +
kernel/kexec_core.c | 3 ++-
kernel/kexec_file.c | 16 ++++++++++++----
3 files changed, 15 insertions(+), 5 deletions(-)
diff --git a/include/linux/kexec.h b/include/linux/kexec.h
index 0af8ae4fdd08..83c296c0eb6c 100644
--- a/include/linux/kexec.h
+++ b/include/linux/kexec.h
@@ -349,6 +349,7 @@ struct kimage {
unsigned long nr_segments;
struct kexec_segment segment[KEXEC_SEGMENT_MAX];
struct page *segment_cma[KEXEC_SEGMENT_MAX];
+ unsigned int segment_cma_pages[KEXEC_SEGMENT_MAX];
struct list_head control_pages;
struct list_head dest_pages;
diff --git a/kernel/kexec_core.c b/kernel/kexec_core.c
index dc770b9a6d05..611b15bb1369 100644
--- a/kernel/kexec_core.c
+++ b/kernel/kexec_core.c
@@ -560,7 +560,7 @@ static void kimage_free_cma(struct kimage *image)
for (i = 0; i < image->nr_segments; i++) {
struct page *cma = image->segment_cma[i];
- u32 nr_pages = image->segment[i].memsz >> PAGE_SHIFT;
+ unsigned int nr_pages = image->segment_cma_pages[i];
if (!cma)
continue;
@@ -568,6 +568,7 @@ static void kimage_free_cma(struct kimage *image)
arch_kexec_pre_free_pages(page_address(cma), nr_pages);
dma_release_from_contiguous(NULL, cma, nr_pages);
image->segment_cma[i] = NULL;
+ image->segment_cma_pages[i] = 0;
}
}
diff --git a/kernel/kexec_file.c b/kernel/kexec_file.c
index 59fb9d71e9d8..bfae3fee7f2f 100644
--- a/kernel/kexec_file.c
+++ b/kernel/kexec_file.c
@@ -670,7 +670,7 @@ static int kexec_walk_resources(struct kexec_buf *kbuf,
static int kexec_alloc_contig(struct kexec_buf *kbuf)
{
- size_t nr_pages = kbuf->memsz >> PAGE_SHIFT;
+ size_t nr_pages = PFN_DOWN(kbuf->memsz);
unsigned long mem;
struct page *p;
@@ -756,6 +756,8 @@ int kexec_locate_mem_hole(struct kexec_buf *kbuf)
*/
int kexec_add_buffer(struct kexec_buf *kbuf)
{
+ unsigned long nr_segments = kbuf->image->nr_segments;
+ size_t nr_pages;
struct kexec_segment *ksegment;
int ret;
@@ -763,7 +765,7 @@ int kexec_add_buffer(struct kexec_buf *kbuf)
if (!kbuf->image->file_mode)
return -EINVAL;
- if (kbuf->image->nr_segments >= KEXEC_SEGMENT_MAX)
+ if (nr_segments >= KEXEC_SEGMENT_MAX)
return -EINVAL;
/*
@@ -789,12 +791,18 @@ int kexec_add_buffer(struct kexec_buf *kbuf)
return ret;
/* Found a suitable memory range */
- ksegment = &kbuf->image->segment[kbuf->image->nr_segments];
+ ksegment = &kbuf->image->segment[nr_segments];
ksegment->kbuf = kbuf->buffer;
ksegment->bufsz = kbuf->bufsz;
ksegment->mem = kbuf->mem;
ksegment->memsz = kbuf->memsz;
- kbuf->image->segment_cma[kbuf->image->nr_segments] = kbuf->cma;
+ kbuf->image->segment_cma[nr_segments] = kbuf->cma;
+ if (kbuf->cma) {
+ nr_pages = (unsigned int)(PFN_DOWN(kbuf->memsz));
+ kbuf->image->segment_cma_pages[nr_segments] = nr_pages;
+ } else {
+ kbuf->image->segment_cma_pages[nr_segments] = 0;
+ }
kbuf->image->nr_segments++;
return 0;
}
--
2.34.1
^ permalink raw reply [flat|nested] 19+ messages in thread
* [PATCH v3 02/17] kexec: Extract kexec_free_segment_cma() from kimage_free_cma()
2026-08-26 9:25 [PATCH v3 00/17] crash: Rework and add arm64 crash hotplug support Jinjie Ruan
2026-08-26 9:25 ` [PATCH v3 01/17] kexec: Record allocated CMA pages to fix release size mismatch Jinjie Ruan
@ 2026-08-26 9:25 ` Jinjie Ruan
2026-08-26 9:25 ` [PATCH v3 03/17] arm64: kexec_file: Fix CMA page leaks in segment placement retry loops Jinjie Ruan
` (14 subsequent siblings)
16 siblings, 0 replies; 19+ messages in thread
From: Jinjie Ruan @ 2026-08-26 9:25 UTC (permalink / raw)
To: catalin.marinas, will, mark.rutland, chenhuacai, kernel, maddy,
mpe, npiggin, chleroy, tglx, mingo, bp, dave.hansen, hpa, akpm,
baoquan.he, rppt, pasha.tatashin, pratyush, ruirui.yang, kees,
thuth, gshan, jic23, james.morse, ardb, leitao, yeoreum.yun,
sourabhjain, coxu, tangyouling, hbathini, adityag, ionut.nechita,
liaoyuanhong, seanjc, fuqiang.wang, makb, piliu, ebiggers,
jbouron, mclapinski, me, graf, bgwin, robh, takahiro.akashi,
palmer, x86, linux-arm-kernel, linux-kernel, loongarch,
linuxppc-dev, kexec, linux-fsdevel, linux-mm
Cc: ruanjinjie
kimage_free_cma() relies on image->nr_segments to iterate over segments.
When an architecture loader (e.g., arm64) truncates nr_segments on a
mid-way failure, CMA pages allocated beyond the new boundary become
unreachable, causing silent memory leaks.
Extract the per-segment freeing logic into the exported helper
kexec_free_segment_cma(), so that architecture loaders can release
individual segments before nr_segments is truncated. Refactor
kimage_free_cma() to loop over the new helper, preserving existing
behavior.
Cc: Andrew Morton <akpm@linux-foundation.org>
Cc: Baoquan He <baoquan.he@linux.dev>
Cc: Mike Rapoport <rppt@kernel.org>
Cc: Pasha Tatashin <pasha.tatashin@soleen.com>
Cc: Pratyush Yadav <pratyush@kernel.org>
Signed-off-by: Jinjie Ruan <ruanjinjie@huawei.com>
---
include/linux/kexec.h | 2 ++
kernel/kexec_core.c | 27 +++++++++++++++------------
2 files changed, 17 insertions(+), 12 deletions(-)
diff --git a/include/linux/kexec.h b/include/linux/kexec.h
index 83c296c0eb6c..d7c83938dfd3 100644
--- a/include/linux/kexec.h
+++ b/include/linux/kexec.h
@@ -533,6 +533,7 @@ extern bool kexec_file_dbg_print;
extern void *kimage_map_segment(struct kimage *image, int idx);
extern void kimage_unmap_segment(void *buffer);
+extern void kexec_free_segment_cma(struct kimage *image, unsigned long idx);
#else /* !CONFIG_KEXEC_CORE */
struct pt_regs;
struct task_struct;
@@ -544,6 +545,7 @@ static inline int kexec_crash_loaded(void) { return 0; }
static inline void *kimage_map_segment(struct kimage *image, int idx)
{ return NULL; }
static inline void kimage_unmap_segment(void *buffer) { }
+static inline void kexec_free_segment_cma(struct kimage *image, unsigned long idx) { }
#define kexec_in_progress false
#endif /* CONFIG_KEXEC_CORE */
diff --git a/kernel/kexec_core.c b/kernel/kexec_core.c
index 611b15bb1369..ee66e8ffd358 100644
--- a/kernel/kexec_core.c
+++ b/kernel/kexec_core.c
@@ -554,23 +554,26 @@ static void kimage_free_entry(kimage_entry_t entry)
kimage_free_pages(page);
}
-static void kimage_free_cma(struct kimage *image)
+void kexec_free_segment_cma(struct kimage *image, unsigned long idx)
{
- unsigned long i;
+ unsigned int nr_pages = image->segment_cma_pages[idx];
+ struct page *cma = image->segment_cma[idx];
- for (i = 0; i < image->nr_segments; i++) {
- struct page *cma = image->segment_cma[i];
- unsigned int nr_pages = image->segment_cma_pages[i];
+ if (!cma)
+ return;
- if (!cma)
- continue;
+ arch_kexec_pre_free_pages(page_address(cma), nr_pages);
+ dma_release_from_contiguous(NULL, cma, nr_pages);
+ image->segment_cma[idx] = NULL;
+ image->segment_cma_pages[idx] = 0;
+}
- arch_kexec_pre_free_pages(page_address(cma), nr_pages);
- dma_release_from_contiguous(NULL, cma, nr_pages);
- image->segment_cma[i] = NULL;
- image->segment_cma_pages[i] = 0;
- }
+static void kimage_free_cma(struct kimage *image)
+{
+ unsigned long i;
+ for (i = 0; i < image->nr_segments; i++)
+ kexec_free_segment_cma(image, i);
}
void kimage_free(struct kimage *image)
--
2.34.1
^ permalink raw reply [flat|nested] 19+ messages in thread
* [PATCH v3 03/17] arm64: kexec_file: Fix CMA page leaks in segment placement retry loops
2026-08-26 9:25 [PATCH v3 00/17] crash: Rework and add arm64 crash hotplug support Jinjie Ruan
2026-08-26 9:25 ` [PATCH v3 01/17] kexec: Record allocated CMA pages to fix release size mismatch Jinjie Ruan
2026-08-26 9:25 ` [PATCH v3 02/17] kexec: Extract kexec_free_segment_cma() from kimage_free_cma() Jinjie Ruan
@ 2026-08-26 9:25 ` Jinjie Ruan
2026-08-26 9:25 ` [PATCH v3 04/17] arm64: kexec_file: Fix elf_headers memory leak in retry loop Jinjie Ruan
` (13 subsequent siblings)
16 siblings, 0 replies; 19+ messages in thread
From: Jinjie Ruan @ 2026-08-26 9:25 UTC (permalink / raw)
To: catalin.marinas, will, mark.rutland, chenhuacai, kernel, maddy,
mpe, npiggin, chleroy, tglx, mingo, bp, dave.hansen, hpa, akpm,
baoquan.he, rppt, pasha.tatashin, pratyush, ruirui.yang, kees,
thuth, gshan, jic23, james.morse, ardb, leitao, yeoreum.yun,
sourabhjain, coxu, tangyouling, hbathini, adityag, ionut.nechita,
liaoyuanhong, seanjc, fuqiang.wang, makb, piliu, ebiggers,
jbouron, mclapinski, me, graf, bgwin, robh, takahiro.akashi,
palmer, x86, linux-arm-kernel, linux-kernel, loongarch,
linuxppc-dev, kexec, linux-fsdevel, linux-mm
Cc: ruanjinjie
During kexec image placement retry loops, any midway failure causes
the loader to truncate `image->nr_segments` back to its initial state
to purge the failed segments.
However, this truncation introduces a memory leak. The CMA pages
allocated via kexec_add_buffer() during the failed attempt are tracked
in the `image->segment_cma` array. Because the subsequent cleanup paths
only iterate up to the truncated `nr_segments` boundary, these allocated
CMA pages outside the new boundary are permanently leaked.
Fix this by explicitly releasing the associated CMA buffers in
the failure paths before `image->nr_segments` is reduced.
Cc: Catalin Marinas <catalin.marinas@arm.com>
Cc: Will Deacon <will@kernel.org>
Cc: Breno Leitao <leitao@debian.org>
Cc: Pratyush Yadav <pratyush@kernel.org>
Cc: Andrew Morton <akpm@linux-foundation.org>
Cc: Yeoreum Yun <yeoreum.yun@arm.com>
Cc: Baoquan He <bhe@redhat.com>
Cc: stable@vger.kernel.org
Fixes: 07d24902977e4 ("kexec: enable CMA based contiguous allocation")
Signed-off-by: Jinjie Ruan <ruanjinjie@huawei.com>
---
arch/arm64/kernel/kexec_image.c | 1 +
arch/arm64/kernel/machine_kexec_file.c | 5 ++++-
2 files changed, 5 insertions(+), 1 deletion(-)
diff --git a/arch/arm64/kernel/kexec_image.c b/arch/arm64/kernel/kexec_image.c
index b70f4df15a1a..ffcb7f9075e6 100644
--- a/arch/arm64/kernel/kexec_image.c
+++ b/arch/arm64/kernel/kexec_image.c
@@ -107,6 +107,7 @@ static void *image_load(struct kimage *image,
* We couldn't find space for the other segments; erase the
* kernel segment and try the next available hole.
*/
+ kexec_free_segment_cma(image, kernel_segment_number);
image->nr_segments -= 1;
kbuf.buf_min = kernel_segment->mem + kernel_segment->memsz;
kbuf.mem = KEXEC_BUF_MEM_UNKNOWN;
diff --git a/arch/arm64/kernel/machine_kexec_file.c b/arch/arm64/kernel/machine_kexec_file.c
index 854d872dfd0f..e48f29167b38 100644
--- a/arch/arm64/kernel/machine_kexec_file.c
+++ b/arch/arm64/kernel/machine_kexec_file.c
@@ -179,7 +179,10 @@ int load_other_segments(struct kimage *image,
return 0;
out_err:
- image->nr_segments = orig_segments;
+ while (image->nr_segments > orig_segments) {
+ kexec_free_segment_cma(image, image->nr_segments - 1);
+ image->nr_segments--;
+ }
kvfree(dtb);
return ret;
}
--
2.34.1
^ permalink raw reply [flat|nested] 19+ messages in thread
* [PATCH v3 04/17] arm64: kexec_file: Fix elf_headers memory leak in retry loop
2026-08-26 9:25 [PATCH v3 00/17] crash: Rework and add arm64 crash hotplug support Jinjie Ruan
` (2 preceding siblings ...)
2026-08-26 9:25 ` [PATCH v3 03/17] arm64: kexec_file: Fix CMA page leaks in segment placement retry loops Jinjie Ruan
@ 2026-08-26 9:25 ` Jinjie Ruan
2026-08-26 9:25 ` [PATCH v3 05/17] LoongArch: kexec: Fix CMA page leaks in segment placement retry loops Jinjie Ruan
` (12 subsequent siblings)
16 siblings, 0 replies; 19+ messages in thread
From: Jinjie Ruan @ 2026-08-26 9:25 UTC (permalink / raw)
To: catalin.marinas, will, mark.rutland, chenhuacai, kernel, maddy,
mpe, npiggin, chleroy, tglx, mingo, bp, dave.hansen, hpa, akpm,
baoquan.he, rppt, pasha.tatashin, pratyush, ruirui.yang, kees,
thuth, gshan, jic23, james.morse, ardb, leitao, yeoreum.yun,
sourabhjain, coxu, tangyouling, hbathini, adityag, ionut.nechita,
liaoyuanhong, seanjc, fuqiang.wang, makb, piliu, ebiggers,
jbouron, mclapinski, me, graf, bgwin, robh, takahiro.akashi,
palmer, x86, linux-arm-kernel, linux-kernel, loongarch,
linuxppc-dev, kexec, linux-fsdevel, linux-mm
Cc: ruanjinjie
If load_other_segments() fails after image->elf_headers is assigned,
the memory lifecycle is safely managed by the global kimage object
and will be freed in arch_kimage_file_post_load_cleanup().
However, during a retry loop in image_load(), a subsequent iteration
will allocate a new buffer and overwrite image->elf_headers. This
permanently leaks the stale memory from the previous iteration before
the global cleanup can track it.
Fix this by explicitly freeing the stale `image->elf_headers` buffer
before assigning the newly allocated headers.
Cc: Catalin Marinas <catalin.marinas@arm.com>
Cc: Will Deacon <will@kernel.org>
Cc: Thomas Huth <thuth@redhat.com>
Cc: Breno Leitao <leitao@debian.org>
Cc: Andrew Morton <akpm@linux-foundation.org>
Cc: Yeoreum Yun <yeoreum.yun@arm.com>
Cc: Baoquan He <bhe@redhat.com>
Cc: stable@vger.kernel.org
Fixes: 108aa503657e ("arm64: kexec_file: try more regions if loading segments fails")
Signed-off-by: Jinjie Ruan <ruanjinjie@huawei.com>
---
arch/arm64/kernel/machine_kexec_file.c | 4 ++++
1 file changed, 4 insertions(+)
diff --git a/arch/arm64/kernel/machine_kexec_file.c b/arch/arm64/kernel/machine_kexec_file.c
index e48f29167b38..2f750e5f4fcc 100644
--- a/arch/arm64/kernel/machine_kexec_file.c
+++ b/arch/arm64/kernel/machine_kexec_file.c
@@ -112,6 +112,10 @@ int load_other_segments(struct kimage *image,
vfree(headers);
goto out_err;
}
+
+ if (unlikely(image->elf_headers))
+ vfree(image->elf_headers);
+
image->elf_headers = headers;
image->elf_load_addr = kbuf.mem;
image->elf_headers_sz = headers_sz;
--
2.34.1
^ permalink raw reply [flat|nested] 19+ messages in thread
* [PATCH v3 05/17] LoongArch: kexec: Fix CMA page leaks in segment placement retry loops
2026-08-26 9:25 [PATCH v3 00/17] crash: Rework and add arm64 crash hotplug support Jinjie Ruan
` (3 preceding siblings ...)
2026-08-26 9:25 ` [PATCH v3 04/17] arm64: kexec_file: Fix elf_headers memory leak in retry loop Jinjie Ruan
@ 2026-08-26 9:25 ` Jinjie Ruan
2026-08-26 9:25 ` [PATCH v3 06/17] LoongArch: kexec_file: Fix elf_headers memory leak in retry loop Jinjie Ruan
` (11 subsequent siblings)
16 siblings, 0 replies; 19+ messages in thread
From: Jinjie Ruan @ 2026-08-26 9:25 UTC (permalink / raw)
To: catalin.marinas, will, mark.rutland, chenhuacai, kernel, maddy,
mpe, npiggin, chleroy, tglx, mingo, bp, dave.hansen, hpa, akpm,
baoquan.he, rppt, pasha.tatashin, pratyush, ruirui.yang, kees,
thuth, gshan, jic23, james.morse, ardb, leitao, yeoreum.yun,
sourabhjain, coxu, tangyouling, hbathini, adityag, ionut.nechita,
liaoyuanhong, seanjc, fuqiang.wang, makb, piliu, ebiggers,
jbouron, mclapinski, me, graf, bgwin, robh, takahiro.akashi,
palmer, x86, linux-arm-kernel, linux-kernel, loongarch,
linuxppc-dev, kexec, linux-fsdevel, linux-mm
Cc: ruanjinjie
During kexec image placement retry loops, any midway failure causes
the loader to truncate `image->nr_segments` back to its initial state
to purge the failed segments.
However, this truncation introduces a memory leak. The CMA pages
allocated via kexec_add_buffer() during the failed attempt are tracked
in the `image->segment_cma` array. Because the subsequent cleanup paths
only iterate up to the truncated `nr_segments` boundary, these allocated
CMA pages outside the new boundary are permanently leaked.
Fix this by explicitly releasing the associated CMA buffers in
the failure paths before `image->nr_segments` is reduced.
Cc: Huacai Chen <chenhuacai@kernel.org>
Cc: WANG Xuerui <kernel@xen0n.name>
Cc: Youling Tang <tangyouling@kylinos.cn>
Cc: "Mike Rapoport (Microsoft)" <rppt@kernel.org>
Cc: Sourabh Jain <sourabhjain@linux.ibm.com>
Cc: Kees Cook <kees@kernel.org>
Cc: stable@vger.kernel.org
Link: https://sashiko.dev/#/patchset/20260729031235.2840255-1-ruanjinjie%40huawei.com
Fixes: 55d990f0084c ("LoongArch: Add EFI binary support for kexec_file")
Signed-off-by: Jinjie Ruan <ruanjinjie@huawei.com>
---
arch/loongarch/kernel/kexec_efi.c | 1 +
arch/loongarch/kernel/machine_kexec_file.c | 6 +++++-
2 files changed, 6 insertions(+), 1 deletion(-)
diff --git a/arch/loongarch/kernel/kexec_efi.c b/arch/loongarch/kernel/kexec_efi.c
index 5ee78ebb1546..15fd797ff3de 100644
--- a/arch/loongarch/kernel/kexec_efi.c
+++ b/arch/loongarch/kernel/kexec_efi.c
@@ -86,6 +86,7 @@ static void *efi_kexec_load(struct kimage *image,
* We couldn't find space for the other segments; erase the
* kernel segment and try the next available hole.
*/
+ kexec_free_segment_cma(image, kernel_segment_number);
image->nr_segments -= 1;
kbuf.buf_min = kernel_segment->mem + kernel_segment->memsz;
kbuf.mem = KEXEC_BUF_MEM_UNKNOWN;
diff --git a/arch/loongarch/kernel/machine_kexec_file.c b/arch/loongarch/kernel/machine_kexec_file.c
index 5412aa9f3568..62a5be102065 100644
--- a/arch/loongarch/kernel/machine_kexec_file.c
+++ b/arch/loongarch/kernel/machine_kexec_file.c
@@ -217,7 +217,11 @@ int load_other_segments(struct kimage *image,
return 0;
out_err:
- image->nr_segments = orig_segments;
+ while (image->nr_segments > orig_segments) {
+ kexec_free_segment_cma(image, image->nr_segments - 1);
+ image->nr_segments--;
+ }
+
kfree(modified_cmdline);
return ret;
}
--
2.34.1
^ permalink raw reply [flat|nested] 19+ messages in thread
* [PATCH v3 06/17] LoongArch: kexec_file: Fix elf_headers memory leak in retry loop
2026-08-26 9:25 [PATCH v3 00/17] crash: Rework and add arm64 crash hotplug support Jinjie Ruan
` (4 preceding siblings ...)
2026-08-26 9:25 ` [PATCH v3 05/17] LoongArch: kexec: Fix CMA page leaks in segment placement retry loops Jinjie Ruan
@ 2026-08-26 9:25 ` Jinjie Ruan
2026-08-26 9:25 ` [PATCH v3 07/17] crash_dump: Fix potential double-free of keys_header Jinjie Ruan
` (10 subsequent siblings)
16 siblings, 0 replies; 19+ messages in thread
From: Jinjie Ruan @ 2026-08-26 9:25 UTC (permalink / raw)
To: catalin.marinas, will, mark.rutland, chenhuacai, kernel, maddy,
mpe, npiggin, chleroy, tglx, mingo, bp, dave.hansen, hpa, akpm,
baoquan.he, rppt, pasha.tatashin, pratyush, ruirui.yang, kees,
thuth, gshan, jic23, james.morse, ardb, leitao, yeoreum.yun,
sourabhjain, coxu, tangyouling, hbathini, adityag, ionut.nechita,
liaoyuanhong, seanjc, fuqiang.wang, makb, piliu, ebiggers,
jbouron, mclapinski, me, graf, bgwin, robh, takahiro.akashi,
palmer, x86, linux-arm-kernel, linux-kernel, loongarch,
linuxppc-dev, kexec, linux-fsdevel, linux-mm
Cc: ruanjinjie
If load_other_segments() fails after image->elf_headers is assigned,
the memory lifecycle is safely managed by the global kimage object
and will be freed in arch_kimage_file_post_load_cleanup().
However, during a retry loop in efi_kexec_load(), a subsequent iteration
will allocate a new buffer and overwrite image->elf_headers. This
permanently leaks the stale memory from the previous iteration before
the global cleanup can track it.
Fix this by explicitly freeing the stale `image->elf_headers` buffer
before assigning the newly allocated headers.
Cc: Huacai Chen <chenhuacai@kernel.org>
Cc: WANG Xuerui <kernel@xen0n.name>
Cc: Youling Tang <tangyouling@kylinos.cn>
Cc: "Mike Rapoport (Microsoft)" <rppt@kernel.org>
Cc: Sourabh Jain <sourabhjain@linux.ibm.com>
Cc: Kees Cook <kees@kernel.org>
Cc: stable@vger.kernel.org
Link: https://sashiko.dev/#/patchset/20260729031235.2840255-1-ruanjinjie%40huawei.com
Fixes: 55d990f0084c ("LoongArch: Add EFI binary support for kexec_file")
Signed-off-by: Jinjie Ruan <ruanjinjie@huawei.com>
---
arch/loongarch/kernel/machine_kexec_file.c | 4 ++++
1 file changed, 4 insertions(+)
diff --git a/arch/loongarch/kernel/machine_kexec_file.c b/arch/loongarch/kernel/machine_kexec_file.c
index 62a5be102065..3beb6977ecc6 100644
--- a/arch/loongarch/kernel/machine_kexec_file.c
+++ b/arch/loongarch/kernel/machine_kexec_file.c
@@ -166,6 +166,10 @@ int load_other_segments(struct kimage *image,
vfree(headers);
goto out_err;
}
+
+ if (unlikely(image->elf_headers))
+ vfree(image->elf_headers);
+
image->elf_headers = headers;
image->elf_load_addr = kbuf.mem;
image->elf_headers_sz = headers_sz;
--
2.34.1
^ permalink raw reply [flat|nested] 19+ messages in thread
* [PATCH v3 07/17] crash_dump: Fix potential double-free of keys_header
2026-08-26 9:25 [PATCH v3 00/17] crash: Rework and add arm64 crash hotplug support Jinjie Ruan
` (5 preceding siblings ...)
2026-08-26 9:25 ` [PATCH v3 06/17] LoongArch: kexec_file: Fix elf_headers memory leak in retry loop Jinjie Ruan
@ 2026-08-26 9:25 ` Jinjie Ruan
2026-08-30 5:59 ` Sourabh Jain
2026-08-26 9:25 ` [PATCH v3 08/17] crash: Extract crash_get_memory_ranges() helper Jinjie Ruan
` (9 subsequent siblings)
16 siblings, 1 reply; 19+ messages in thread
From: Jinjie Ruan @ 2026-08-26 9:25 UTC (permalink / raw)
To: catalin.marinas, will, mark.rutland, chenhuacai, kernel, maddy,
mpe, npiggin, chleroy, tglx, mingo, bp, dave.hansen, hpa, akpm,
baoquan.he, rppt, pasha.tatashin, pratyush, ruirui.yang, kees,
thuth, gshan, jic23, james.morse, ardb, leitao, yeoreum.yun,
sourabhjain, coxu, tangyouling, hbathini, adityag, ionut.nechita,
liaoyuanhong, seanjc, fuqiang.wang, makb, piliu, ebiggers,
jbouron, mclapinski, me, graf, bgwin, robh, takahiro.akashi,
palmer, x86, linux-arm-kernel, linux-kernel, loongarch,
linuxppc-dev, kexec, linux-fsdevel, linux-mm
Cc: ruanjinjie
`keys_header` was freed in `build_keys_header()` without being reset
to NULL, and the error path in `crash_load_dm_crypt_keys()` freed it
unconditionally even when reused, leading to double-free or
use-after-free.
Add `free_keys_header()` to centralize freeing and NULL-setting.
Use it in `build_keys_header()` and only free in the error path when
the header was newly built (`!is_dm_key_reused`).
Cc: Andrew Morton <akpm@linux-foundation.org>
Cc: Baoquan He <bhe@redhat.com>
Cc: Mike Rapoport <rppt@kernel.org>
Cc: Pasha Tatashin <pasha.tatashin@soleen.com>
Cc: Pratyush Yadav <pratyush@kernel.org>
Cc: Dave Young <ruirui.yang@linux.dev>
Cc: stable@vger.kernel.org
Fixes: e3a84be1ec2f ("arm64,ppc64le/kdump: pass dm-crypt keys to kdump kernel")
Signed-off-by: Jinjie Ruan <ruanjinjie@huawei.com>
---
kernel/crash_dump_dm_crypt.c | 15 +++++++++++----
1 file changed, 11 insertions(+), 4 deletions(-)
diff --git a/kernel/crash_dump_dm_crypt.c b/kernel/crash_dump_dm_crypt.c
index c685497cd470..ed0960ff0987 100644
--- a/kernel/crash_dump_dm_crypt.c
+++ b/kernel/crash_dump_dm_crypt.c
@@ -363,15 +363,21 @@ static struct configfs_subsystem config_keys_subsys = {
},
};
+static void free_keys_header(void)
+{
+ if (keys_header) {
+ kvfree(keys_header);
+ keys_header = NULL;
+ }
+}
+
static int build_keys_header(void)
{
struct config_item *item = NULL;
struct config_key *key;
int i, r;
- if (keys_header != NULL)
- kvfree(keys_header);
-
+ free_keys_header();
keys_header = kzalloc(get_keys_header_size(key_count), GFP_KERNEL);
if (!keys_header)
return -ENOMEM;
@@ -441,7 +447,8 @@ int crash_load_dm_crypt_keys(struct kimage *image)
r = kexec_add_buffer(&kbuf);
if (r) {
pr_err("Failed to call kexec_add_buffer, ret=%d\n", r);
- kvfree((void *)kbuf.buffer);
+ if (!is_dm_key_reused)
+ free_keys_header();
return r;
}
image->dm_crypt_keys_addr = kbuf.mem;
--
2.34.1
^ permalink raw reply [flat|nested] 19+ messages in thread
* [PATCH v3 08/17] crash: Extract crash_get_memory_ranges() helper
2026-08-26 9:25 [PATCH v3 00/17] crash: Rework and add arm64 crash hotplug support Jinjie Ruan
` (6 preceding siblings ...)
2026-08-26 9:25 ` [PATCH v3 07/17] crash_dump: Fix potential double-free of keys_header Jinjie Ruan
@ 2026-08-26 9:25 ` Jinjie Ruan
2026-08-26 9:25 ` [PATCH v3 09/17] crash: Fix TOCTOU race in crash memory range collection Jinjie Ruan
` (8 subsequent siblings)
16 siblings, 0 replies; 19+ messages in thread
From: Jinjie Ruan @ 2026-08-26 9:25 UTC (permalink / raw)
To: catalin.marinas, will, mark.rutland, chenhuacai, kernel, maddy,
mpe, npiggin, chleroy, tglx, mingo, bp, dave.hansen, hpa, akpm,
baoquan.he, rppt, pasha.tatashin, pratyush, ruirui.yang, kees,
thuth, gshan, jic23, james.morse, ardb, leitao, yeoreum.yun,
sourabhjain, coxu, tangyouling, hbathini, adityag, ionut.nechita,
liaoyuanhong, seanjc, fuqiang.wang, makb, piliu, ebiggers,
jbouron, mclapinski, me, graf, bgwin, robh, takahiro.akashi,
palmer, x86, linux-arm-kernel, linux-kernel, loongarch,
linuxppc-dev, kexec, linux-fsdevel, linux-mm
Cc: ruanjinjie
Factor out the crash memory range collection logic from
crash_prepare_headers() into a separate function. This allows
the memory hotplug path to obtain and modify the range list
(e.g. remove offlined memory) before generating the elfcorehdr.
Cc: Andrew Morton <akpm@linux-foundation.org>
Cc: Baoquan He <baoquan.he@linux.dev>
Cc: Mike Rapoport <rppt@kernel.org>
Cc: Pasha Tatashin <pasha.tatashin@soleen.com>
Cc: Pratyush Yadav <pratyush@kernel.org>
Cc: Dave Young <ruirui.yang@linux.dev>
Signed-off-by: Jinjie Ruan <ruanjinjie@huawei.com>
---
include/linux/crash_core.h | 1 +
kernel/crash_core.c | 22 +++++++++++++++++++---
2 files changed, 20 insertions(+), 3 deletions(-)
diff --git a/include/linux/crash_core.h b/include/linux/crash_core.h
index bc087124cd78..619af312bd9a 100644
--- a/include/linux/crash_core.h
+++ b/include/linux/crash_core.h
@@ -62,6 +62,7 @@ extern int crash_prepare_elf64_headers(struct crash_mem *mem, int need_kernel_ma
extern 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);
+extern int crash_get_memory_ranges(struct crash_mem **mem_ranges);
struct kimage;
struct kexec_segment;
diff --git a/kernel/crash_core.c b/kernel/crash_core.c
index d0bd2d0cf899..991d1599cf9a 100644
--- a/kernel/crash_core.c
+++ b/kernel/crash_core.c
@@ -317,8 +317,7 @@ 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_get_memory_ranges(struct crash_mem **mem_ranges)
{
unsigned int max_nr_ranges;
struct crash_mem *cmem;
@@ -344,13 +343,30 @@ int crash_prepare_headers(int need_kernel_map, void **addr, unsigned long *sz,
if (ret)
goto out;
+ *mem_ranges = cmem;
+ return 0;
+
+out:
+ kvfree(cmem);
+ return ret;
+}
+
+int crash_prepare_headers(int need_kernel_map, void **addr, unsigned long *sz,
+ unsigned long *nr_mem_ranges)
+{
+ struct crash_mem *cmem = NULL;
+ int ret;
+
+ ret = crash_get_memory_ranges(&cmem);
+ if (ret)
+ return ret;
+
/* Return the computed number of memory ranges, for hotplug usage */
if (nr_mem_ranges)
*nr_mem_ranges = cmem->nr_ranges;
ret = crash_prepare_elf64_headers(cmem, need_kernel_map, addr, sz);
-out:
kvfree(cmem);
return ret;
}
--
2.34.1
^ permalink raw reply [flat|nested] 19+ messages in thread
* [PATCH v3 09/17] crash: Fix TOCTOU race in crash memory range collection
2026-08-26 9:25 [PATCH v3 00/17] crash: Rework and add arm64 crash hotplug support Jinjie Ruan
` (7 preceding siblings ...)
2026-08-26 9:25 ` [PATCH v3 08/17] crash: Extract crash_get_memory_ranges() helper Jinjie Ruan
@ 2026-08-26 9:25 ` Jinjie Ruan
2026-08-26 9:25 ` [PATCH v3 10/17] elf: Introduce elf64_phdr_size() helper Jinjie Ruan
` (7 subsequent siblings)
16 siblings, 0 replies; 19+ messages in thread
From: Jinjie Ruan @ 2026-08-26 9:25 UTC (permalink / raw)
To: catalin.marinas, will, mark.rutland, chenhuacai, kernel, maddy,
mpe, npiggin, chleroy, tglx, mingo, bp, dave.hansen, hpa, akpm,
baoquan.he, rppt, pasha.tatashin, pratyush, ruirui.yang, kees,
thuth, gshan, jic23, james.morse, ardb, leitao, yeoreum.yun,
sourabhjain, coxu, tangyouling, hbathini, adityag, ionut.nechita,
liaoyuanhong, seanjc, fuqiang.wang, makb, piliu, ebiggers,
jbouron, mclapinski, me, graf, bgwin, robh, takahiro.akashi,
palmer, x86, linux-arm-kernel, linux-kernel, loongarch,
linuxppc-dev, kexec, linux-fsdevel, linux-mm
Cc: ruanjinjie
The crash kernel ELF core header construction counts system memory
ranges via `arch_get_system_nr_ranges()`, allocates the crash_mem
buffer, and then populates it via `arch_crash_populate_cmem()`.
This sequence has a time-of-check-to-time-of-use (TOCTOU) race with
memory hotplug: a concurrent hotplug event between the count
and populate steps can increase the number of ranges beyond the allocated
capacity, causing an out-of-bounds write. If the event triggers
memblock_double_array(), the memblock array can be freed and reallocated
during iteration, leading to a use-after-free.
Protect the entire range collection with device_hotplug_lock. Since
the hotplug notification path already holds that lock, add a lockless
helper, crash_get_memory_ranges_nolock(), for use there. The regular
crash_get_memory_ranges() acquires the lock and calls the helper.
Cc: stable@vger.kernel.org
Cc: Andrew Morton <akpm@linux-foundation.org>
Cc: Baoquan He <baoquan.he@linux.dev>
Cc: Mike Rapoport <rppt@kernel.org>
Cc: Pasha Tatashin <pasha.tatashin@soleen.com>
Cc: Pratyush Yadav <pratyush@kernel.org>
Cc: Dave Young <ruirui.yang@linux.dev>
Cc: AKASHI Takahiro <takahiro.akashi@linaro.org>
Cc: Will Deacon <will@kernel.org>
Cc: James Morse <james.morse@arm.com>
Cc: Palmer Dabbelt <palmer@rivosinc.com>
Cc: Youling Tang <tangyouling@kylinos.cn>
Cc: Huacai Chen <chenhuacai@kernel.org>
Fixes: 8d5f894a3108 ("x86: kexec_file: lift CRASH_MAX_RANGES limit on crash_mem buffer")
Fixes: 3751e728cef2 ("arm64: kexec_file: add crash dump support")
Fixes: 8acea455fafa ("RISC-V: Support for kexec_file on panic")
Fixes: 1bcca8620a91 ("LoongArch: Add crash dump support for kexec_file")
Link: https://sashiko.dev/#/patchset/20260729031235.2840255-1-ruanjinjie%40huawei.com
Signed-off-by: Jinjie Ruan <ruanjinjie@huawei.com>
---
arch/x86/kernel/crash.c | 9 ++++++++-
include/linux/crash_core.h | 2 +-
kernel/crash_core.c | 28 +++++++++++++++++++++++++++-
3 files changed, 36 insertions(+), 3 deletions(-)
diff --git a/arch/x86/kernel/crash.c b/arch/x86/kernel/crash.c
index e6f23933a6df..8f8c0e592849 100644
--- a/arch/x86/kernel/crash.c
+++ b/arch/x86/kernel/crash.c
@@ -448,6 +448,7 @@ unsigned int arch_crash_get_elfcorehdr_size(void)
void arch_crash_handle_hotplug_event(struct kimage *image, void *arg)
{
void *elfbuf = NULL, *old_elfcorehdr;
+ struct crash_mem *cmem = NULL;
unsigned long mem, memsz;
unsigned long elfsz = 0;
@@ -461,11 +462,16 @@ void arch_crash_handle_hotplug_event(struct kimage *image, void *arg)
(image->hp_action == KEXEC_CRASH_HP_REMOVE_CPU)))
return;
+ if (crash_get_memory_ranges_nolock(&cmem)) {
+ pr_err("Failed to get crash mem range\n");
+ goto out;
+ }
+
/*
* 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_elf64_headers(cmem, IS_ENABLED(CONFIG_X86_64), &elfbuf, &elfsz)) {
pr_err("unable to create new elfcorehdr");
goto out;
}
@@ -502,6 +508,7 @@ void arch_crash_handle_hotplug_event(struct kimage *image, void *arg)
pr_debug("updated elfcorehdr\n");
out:
+ kvfree(cmem);
vfree(elfbuf);
}
#endif
diff --git a/include/linux/crash_core.h b/include/linux/crash_core.h
index 619af312bd9a..6789ff0af39e 100644
--- a/include/linux/crash_core.h
+++ b/include/linux/crash_core.h
@@ -62,7 +62,7 @@ extern int crash_prepare_elf64_headers(struct crash_mem *mem, int need_kernel_ma
extern 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);
-extern int crash_get_memory_ranges(struct crash_mem **mem_ranges);
+extern int crash_get_memory_ranges_nolock(struct crash_mem **mem_ranges);
struct kimage;
struct kexec_segment;
diff --git a/kernel/crash_core.c b/kernel/crash_core.c
index 991d1599cf9a..05a2a8be083d 100644
--- a/kernel/crash_core.c
+++ b/kernel/crash_core.c
@@ -7,6 +7,7 @@
#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
#include <linux/buildid.h>
+#include <linux/device.h>
#include <linux/init.h>
#include <linux/utsname.h>
#include <linux/vmalloc.h>
@@ -317,7 +318,21 @@ int crash_exclude_core_ranges(struct crash_mem **cmem)
return 0;
}
-int crash_get_memory_ranges(struct crash_mem **mem_ranges)
+/**
+ * crash_get_memory_ranges_nolock - Collect crash kernel memory ranges
+ * @mem_ranges: Output parameter for the allocated crash_mem structure
+ *
+ * Gathers the system memory ranges to be included in the crash kernel's
+ * ELF core header, excluding the crashkernel reserved region and other
+ * architecture-specific areas.
+ *
+ * Context: Caller must hold device_hotplug_lock.
+ *
+ * Return: 0 on success, in which case *@mem_ranges points to a newly
+ * allocated struct crash_mem that the caller must free with kvfree().
+ * Returns a negative error code on failure.
+ */
+int crash_get_memory_ranges_nolock(struct crash_mem **mem_ranges)
{
unsigned int max_nr_ranges;
struct crash_mem *cmem;
@@ -351,6 +366,17 @@ int crash_get_memory_ranges(struct crash_mem **mem_ranges)
return ret;
}
+static int crash_get_memory_ranges(struct crash_mem **mem_ranges)
+{
+ int ret;
+
+ lock_device_hotplug();
+ ret = crash_get_memory_ranges_nolock(mem_ranges);
+ unlock_device_hotplug();
+
+ return ret;
+}
+
int crash_prepare_headers(int need_kernel_map, void **addr, unsigned long *sz,
unsigned long *nr_mem_ranges)
{
--
2.34.1
^ permalink raw reply [flat|nested] 19+ messages in thread
* [PATCH v3 10/17] elf: Introduce elf64_phdr_size() helper
2026-08-26 9:25 [PATCH v3 00/17] crash: Rework and add arm64 crash hotplug support Jinjie Ruan
` (8 preceding siblings ...)
2026-08-26 9:25 ` [PATCH v3 09/17] crash: Fix TOCTOU race in crash memory range collection Jinjie Ruan
@ 2026-08-26 9:25 ` Jinjie Ruan
2026-08-26 9:25 ` [PATCH v3 11/17] crash: Introduce crash_extra_elfcorehdr_size() helper Jinjie Ruan
` (6 subsequent siblings)
16 siblings, 0 replies; 19+ messages in thread
From: Jinjie Ruan @ 2026-08-26 9:25 UTC (permalink / raw)
To: catalin.marinas, will, mark.rutland, chenhuacai, kernel, maddy,
mpe, npiggin, chleroy, tglx, mingo, bp, dave.hansen, hpa, akpm,
baoquan.he, rppt, pasha.tatashin, pratyush, ruirui.yang, kees,
thuth, gshan, jic23, james.morse, ardb, leitao, yeoreum.yun,
sourabhjain, coxu, tangyouling, hbathini, adityag, ionut.nechita,
liaoyuanhong, seanjc, fuqiang.wang, makb, piliu, ebiggers,
jbouron, mclapinski, me, graf, bgwin, robh, takahiro.akashi,
palmer, x86, linux-arm-kernel, linux-kernel, loongarch,
linuxppc-dev, kexec, linux-fsdevel, linux-mm
Cc: ruanjinjie
Add a common helper to compute the total size of an ELF64 header
(Ehdr + program headers) from the number of program headers.
Replace open-coded calculations in powerpc, x86, vmcore,
and crash_core.
On ppc64, struct elfhdr maps to elf64_hdr, so the powerpc change
is a pure cleanup.
No functional change intended.
Cc: Madhavan Srinivasan <maddy@linux.ibm.com>
Cc: Michael Ellerman <mpe@ellerman.id.au>
Cc: Nicholas Piggin <npiggin@gmail.com>
Cc: "Christophe Leroy (CS GROUP)" <chleroy@kernel.org>
Cc: Thomas Gleixner <tglx@kernel.org>
Cc: Ingo Molnar <mingo@redhat.com>
Cc: Borislav Petkov <bp@alien8.de>
Cc: Dave Hansen <dave.hansen@linux.intel.com>
Cc: "H. Peter Anvin" <hpa@zytor.com>
Cc: Andrew Morton <akpm@linux-foundation.org>
Cc: Baoquan He <baoquan.he@linux.dev>
Cc: Mike Rapoport <rppt@kernel.org>
Cc: Pasha Tatashin <pasha.tatashin@soleen.com>
Cc: Pratyush Yadav <pratyush@kernel.org>
Cc: Dave Young <ruirui.yang@linux.dev>
Cc: Kees Cook <kees@kernel.org>
Cc: Sourabh Jain <sourabhjain@linux.ibm.com>
Signed-off-by: Jinjie Ruan <ruanjinjie@huawei.com>
---
arch/powerpc/kexec/crash.c | 2 +-
arch/powerpc/platforms/powernv/opal-core.c | 3 +--
arch/x86/kernel/crash.c | 3 +--
fs/proc/vmcore.c | 6 ++----
include/linux/elf.h | 4 ++++
kernel/crash_core.c | 2 +-
6 files changed, 10 insertions(+), 10 deletions(-)
diff --git a/arch/powerpc/kexec/crash.c b/arch/powerpc/kexec/crash.c
index 775895f31037..fc0105c7af4c 100644
--- a/arch/powerpc/kexec/crash.c
+++ b/arch/powerpc/kexec/crash.c
@@ -478,7 +478,7 @@ unsigned int arch_crash_get_elfcorehdr_size(void)
if (IS_ENABLED(CONFIG_MEMORY_HOTPLUG))
phdr_cnt += CONFIG_CRASH_MAX_MEMORY_RANGES;
- return sizeof(struct elfhdr) + (phdr_cnt * sizeof(Elf64_Phdr));
+ return elf64_phdr_size(phdr_cnt);
}
/**
diff --git a/arch/powerpc/platforms/powernv/opal-core.c b/arch/powerpc/platforms/powernv/opal-core.c
index 32662d30d70f..fc0aad61504b 100644
--- a/arch/powerpc/platforms/powernv/opal-core.c
+++ b/arch/powerpc/platforms/powernv/opal-core.c
@@ -309,8 +309,7 @@ static int __init create_opalcore(void)
char *bufp;
/* Get size of header & CPU notes for OPAL core */
- hdr_size = (sizeof(Elf64_Ehdr) +
- ((oc_conf->ptload_cnt + 1) * sizeof(Elf64_Phdr)));
+ hdr_size = elf64_phdr_size(oc_conf->ptload_cnt + 1);
cpu_notes_size = ((oc_conf->num_cpus * (CRASH_CORE_NOTE_HEAD_BYTES +
CRASH_CORE_NOTE_NAME_BYTES +
CRASH_CORE_NOTE_DESC_BYTES)) +
diff --git a/arch/x86/kernel/crash.c b/arch/x86/kernel/crash.c
index 8f8c0e592849..a3bf786286d4 100644
--- a/arch/x86/kernel/crash.c
+++ b/arch/x86/kernel/crash.c
@@ -374,8 +374,7 @@ int crash_load_segments(struct kimage *image)
pnum += 2 + CONFIG_NR_CPUS;
if (pnum < (unsigned long)PN_XNUM) {
- kbuf.memsz = pnum * sizeof(Elf64_Phdr);
- kbuf.memsz += sizeof(Elf64_Ehdr);
+ kbuf.memsz = elf64_phdr_size(pnum);
image->elfcorehdr_index = image->nr_segments;
diff --git a/fs/proc/vmcore.c b/fs/proc/vmcore.c
index 44d15436439f..ff324969d798 100644
--- a/fs/proc/vmcore.c
+++ b/fs/proc/vmcore.c
@@ -1238,8 +1238,7 @@ static int __init parse_crash_elf64_headers(void)
}
/* Read in all elf headers. */
- elfcorebuf_sz_orig = sizeof(Elf64_Ehdr) +
- ehdr.e_phnum * sizeof(Elf64_Phdr);
+ elfcorebuf_sz_orig = elf64_phdr_size(ehdr.e_phnum);
elfcorebuf_sz = elfcorebuf_sz_orig;
elfcorebuf = (void *)__get_free_pages(GFP_KERNEL | __GFP_ZERO,
get_order(elfcorebuf_sz_orig));
@@ -1605,8 +1604,7 @@ static int vmcore_add_device_ram_elf64(struct list_head *list, size_t count)
}
/* elfcorebuf_sz must always cover full pages. */
- new_size = sizeof(Elf64_Ehdr) +
- (ehdr->e_phnum + count) * sizeof(Elf64_Phdr);
+ new_size = elf64_phdr_size(ehdr->e_phnum + count);
new_size = roundup(new_size, PAGE_SIZE);
/*
diff --git a/include/linux/elf.h b/include/linux/elf.h
index 5c402788da19..400f58a13d92 100644
--- a/include/linux/elf.h
+++ b/include/linux/elf.h
@@ -109,4 +109,8 @@ static inline int arch_elf_adjust_prot(int prot,
}
#endif
+static inline unsigned long elf64_phdr_size(unsigned long phdr_cnt)
+{
+ return phdr_cnt * sizeof(Elf64_Phdr) + sizeof(Elf64_Ehdr);
+}
#endif /* _LINUX_ELF_H */
diff --git a/kernel/crash_core.c b/kernel/crash_core.c
index 05a2a8be083d..bd3f82b62751 100644
--- a/kernel/crash_core.c
+++ b/kernel/crash_core.c
@@ -193,7 +193,7 @@ int crash_prepare_elf64_headers(struct crash_mem *mem, int need_kernel_map,
*/
nr_phdr++;
- elf_sz = sizeof(Elf64_Ehdr) + nr_phdr * sizeof(Elf64_Phdr);
+ elf_sz = elf64_phdr_size(nr_phdr);
elf_sz = ALIGN(elf_sz, ELF_CORE_HEADER_ALIGN);
buf = vzalloc(elf_sz);
--
2.34.1
^ permalink raw reply [flat|nested] 19+ messages in thread
* [PATCH v3 11/17] crash: Introduce crash_extra_elfcorehdr_size() helper
2026-08-26 9:25 [PATCH v3 00/17] crash: Rework and add arm64 crash hotplug support Jinjie Ruan
` (9 preceding siblings ...)
2026-08-26 9:25 ` [PATCH v3 10/17] elf: Introduce elf64_phdr_size() helper Jinjie Ruan
@ 2026-08-26 9:25 ` Jinjie Ruan
2026-08-26 9:25 ` [PATCH v3 12/17] x86/crash: Use num_possible_cpus() for elfcorehdr size Jinjie Ruan
` (5 subsequent siblings)
16 siblings, 0 replies; 19+ messages in thread
From: Jinjie Ruan @ 2026-08-26 9:25 UTC (permalink / raw)
To: catalin.marinas, will, mark.rutland, chenhuacai, kernel, maddy,
mpe, npiggin, chleroy, tglx, mingo, bp, dave.hansen, hpa, akpm,
baoquan.he, rppt, pasha.tatashin, pratyush, ruirui.yang, kees,
thuth, gshan, jic23, james.morse, ardb, leitao, yeoreum.yun,
sourabhjain, coxu, tangyouling, hbathini, adityag, ionut.nechita,
liaoyuanhong, seanjc, fuqiang.wang, makb, piliu, ebiggers,
jbouron, mclapinski, me, graf, bgwin, robh, takahiro.akashi,
palmer, x86, linux-arm-kernel, linux-kernel, loongarch,
linuxppc-dev, kexec, linux-fsdevel, linux-mm
Cc: ruanjinjie
Extract the elfcorehdr extra space calculation from powerpc into a
generic helper crash_extra_elfcorehdr_size() for use by other
architectures.
The helper includes compile-time and runtime checks, it also checks
whether the total number of program headers (including fixed headers for
kernel_map, VMCOREINFO, and CPU notes) exceeds PN_XNUM, and provides a
stub when crash/memory hotplug is disabled.
Cc: Madhavan Srinivasan <maddy@linux.ibm.com>
Cc: Michael Ellerman <mpe@ellerman.id.au>
Cc: Nicholas Piggin <npiggin@gmail.com>
Cc: "Christophe Leroy (CS GROUP)" <chleroy@kernel.org>
Cc: Andrew Morton <akpm@linux-foundation.org>
Cc: Baoquan He <baoquan.he@linux.dev>
Cc: Mike Rapoport <rppt@kernel.org>
Cc: Pasha Tatashin <pasha.tatashin@soleen.com>
Cc: Pratyush Yadav <pratyush@kernel.org>
Cc: Dave Young <ruirui.yang@linux.dev>
Cc: Sourabh Jain <sourabhjain@linux.ibm.com>
Signed-off-by: Jinjie Ruan <ruanjinjie@huawei.com>
---
arch/powerpc/kexec/file_load_64.c | 19 +------------------
include/linux/crash_core.h | 26 ++++++++++++++++++++++++++
2 files changed, 27 insertions(+), 18 deletions(-)
diff --git a/arch/powerpc/kexec/file_load_64.c b/arch/powerpc/kexec/file_load_64.c
index 6075b1c88511..2b0325e4a628 100644
--- a/arch/powerpc/kexec/file_load_64.c
+++ b/arch/powerpc/kexec/file_load_64.c
@@ -374,23 +374,6 @@ static int load_backup_segment(struct kimage *image, struct kexec_buf *kbuf)
return 0;
}
-static unsigned int kdump_extra_elfcorehdr_size(struct crash_mem *cmem)
-{
-#if defined(CONFIG_CRASH_HOTPLUG) && defined(CONFIG_MEMORY_HOTPLUG)
- unsigned int extra_sz = 0;
-
- if (CONFIG_CRASH_MAX_MEMORY_RANGES > (unsigned int)PN_XNUM)
- pr_warn("Number of Phdrs %u exceeds max\n", CONFIG_CRASH_MAX_MEMORY_RANGES);
- else if (cmem->nr_ranges >= CONFIG_CRASH_MAX_MEMORY_RANGES)
- pr_warn("Configured crash mem ranges may not be enough\n");
- else
- extra_sz = (CONFIG_CRASH_MAX_MEMORY_RANGES - cmem->nr_ranges) * sizeof(Elf64_Phdr);
-
- return extra_sz;
-#endif
- return 0;
-}
-
/**
* load_elfcorehdr_segment - Setup crash memory ranges and initialize elfcorehdr
* segment needed to load kdump kernel.
@@ -428,7 +411,7 @@ static int load_elfcorehdr_segment(struct kimage *image, struct kexec_buf *kbuf)
* Account for extra space required to accommodate additional memory
* ranges in elfcorehdr due to memory hotplug events.
*/
- kbuf->memsz = headers_sz + kdump_extra_elfcorehdr_size(cmem);
+ kbuf->memsz = headers_sz + crash_extra_elfcorehdr_size(cmem->nr_ranges);
kbuf->top_down = false;
ret = kexec_add_buffer(kbuf);
diff --git a/include/linux/crash_core.h b/include/linux/crash_core.h
index 6789ff0af39e..d5981008a812 100644
--- a/include/linux/crash_core.h
+++ b/include/linux/crash_core.h
@@ -106,4 +106,30 @@ ssize_t dm_crypt_keys_read(char *buf, size_t count, u64 *ppos);
static inline int crash_load_dm_crypt_keys(struct kimage *image) {return 0; }
#endif
+#if defined(CONFIG_CRASH_HOTPLUG) && defined(CONFIG_MEMORY_HOTPLUG)
+static inline unsigned int crash_extra_elfcorehdr_size(unsigned int nr_mem_ranges)
+{
+ unsigned int total_phdrs = 2 + num_possible_cpus() + CONFIG_CRASH_MAX_MEMORY_RANGES;
+
+ BUILD_BUG_ON(CONFIG_CRASH_MAX_MEMORY_RANGES > (unsigned int)PN_XNUM);
+
+ if (nr_mem_ranges >= CONFIG_CRASH_MAX_MEMORY_RANGES) {
+ pr_warn_once("Configured crash mem ranges may not be enough\n");
+ return 0;
+ }
+
+ if (total_phdrs >= (unsigned int)PN_XNUM) {
+ pr_warn_once("number of Phdrs %u exceeds max\n", total_phdrs);
+ return 0;
+ }
+
+ return (CONFIG_CRASH_MAX_MEMORY_RANGES - nr_mem_ranges) * sizeof(Elf64_Phdr);
+}
+#else
+static inline unsigned int crash_extra_elfcorehdr_size(unsigned int nr_mem_ranges)
+{
+ return 0;
+}
+#endif
+
#endif /* LINUX_CRASH_CORE_H */
--
2.34.1
^ permalink raw reply [flat|nested] 19+ messages in thread
* [PATCH v3 12/17] x86/crash: Use num_possible_cpus() for elfcorehdr size
2026-08-26 9:25 [PATCH v3 00/17] crash: Rework and add arm64 crash hotplug support Jinjie Ruan
` (10 preceding siblings ...)
2026-08-26 9:25 ` [PATCH v3 11/17] crash: Introduce crash_extra_elfcorehdr_size() helper Jinjie Ruan
@ 2026-08-26 9:25 ` Jinjie Ruan
2026-08-26 9:25 ` [PATCH v3 13/17] crash: Improve elfcorehdr segment identification Jinjie Ruan
` (4 subsequent siblings)
16 siblings, 0 replies; 19+ messages in thread
From: Jinjie Ruan @ 2026-08-26 9:25 UTC (permalink / raw)
To: catalin.marinas, will, mark.rutland, chenhuacai, kernel, maddy,
mpe, npiggin, chleroy, tglx, mingo, bp, dave.hansen, hpa, akpm,
baoquan.he, rppt, pasha.tatashin, pratyush, ruirui.yang, kees,
thuth, gshan, jic23, james.morse, ardb, leitao, yeoreum.yun,
sourabhjain, coxu, tangyouling, hbathini, adityag, ionut.nechita,
liaoyuanhong, seanjc, fuqiang.wang, makb, piliu, ebiggers,
jbouron, mclapinski, me, graf, bgwin, robh, takahiro.akashi,
palmer, x86, linux-arm-kernel, linux-kernel, loongarch,
linuxppc-dev, kexec, linux-fsdevel, linux-mm
Cc: ruanjinjie
Use num_possible_cpus() instead of CONFIG_NR_CPUS to calculate the
elfcorehdr buffer size, matching what crash_prepare_elf64_headers()
already uses for the actual header content.
This avoids over-allocation when the system has fewer possible CPUs
than the compile-time limit, and aligns x86 with PowerPC.
Signed-off-by: Jinjie Ruan <ruanjinjie@huawei.com>
---
arch/x86/kernel/crash.c | 6 +++---
1 file changed, 3 insertions(+), 3 deletions(-)
diff --git a/arch/x86/kernel/crash.c b/arch/x86/kernel/crash.c
index a3bf786286d4..afe2aea19f47 100644
--- a/arch/x86/kernel/crash.c
+++ b/arch/x86/kernel/crash.c
@@ -369,9 +369,9 @@ int crash_load_segments(struct kimage *image)
* maximum CPUs and maximum memory ranges.
*/
if (IS_ENABLED(CONFIG_MEMORY_HOTPLUG))
- pnum = 2 + CONFIG_NR_CPUS + CONFIG_CRASH_MAX_MEMORY_RANGES;
+ pnum = 2 + num_possible_cpus() + CONFIG_CRASH_MAX_MEMORY_RANGES;
else
- pnum += 2 + CONFIG_NR_CPUS;
+ pnum += 2 + num_possible_cpus();
if (pnum < (unsigned long)PN_XNUM) {
kbuf.memsz = elf64_phdr_size(pnum);
@@ -429,7 +429,7 @@ unsigned int arch_crash_get_elfcorehdr_size(void)
unsigned int sz;
/* kernel_map, VMCOREINFO and maximum CPUs */
- sz = 2 + CONFIG_NR_CPUS;
+ sz = 2 + num_possible_cpus();
if (IS_ENABLED(CONFIG_MEMORY_HOTPLUG))
sz += CONFIG_CRASH_MAX_MEMORY_RANGES;
sz *= sizeof(Elf64_Phdr);
--
2.34.1
^ permalink raw reply [flat|nested] 19+ messages in thread
* [PATCH v3 13/17] crash: Improve elfcorehdr segment identification
2026-08-26 9:25 [PATCH v3 00/17] crash: Rework and add arm64 crash hotplug support Jinjie Ruan
` (11 preceding siblings ...)
2026-08-26 9:25 ` [PATCH v3 12/17] x86/crash: Use num_possible_cpus() for elfcorehdr size Jinjie Ruan
@ 2026-08-26 9:25 ` Jinjie Ruan
2026-08-26 9:25 ` [PATCH v3 14/17] x86/crash: Simplify crash_load_segments() using crash_extra_elfcorehdr_size() Jinjie Ruan
` (3 subsequent siblings)
16 siblings, 0 replies; 19+ messages in thread
From: Jinjie Ruan @ 2026-08-26 9:25 UTC (permalink / raw)
To: catalin.marinas, will, mark.rutland, chenhuacai, kernel, maddy,
mpe, npiggin, chleroy, tglx, mingo, bp, dave.hansen, hpa, akpm,
baoquan.he, rppt, pasha.tatashin, pratyush, ruirui.yang, kees,
thuth, gshan, jic23, james.morse, ardb, leitao, yeoreum.yun,
sourabhjain, coxu, tangyouling, hbathini, adityag, ionut.nechita,
liaoyuanhong, seanjc, fuqiang.wang, makb, piliu, ebiggers,
jbouron, mclapinski, me, graf, bgwin, robh, takahiro.akashi,
palmer, x86, linux-arm-kernel, linux-kernel, loongarch,
linuxppc-dev, kexec, linux-fsdevel, linux-mm
Cc: ruanjinjie
Only checking ELFMAG is insufficient, as other segments like the
kernel image or an ELF initrd may also start with \x7fELF, leading
to misidentification. This is especially problematic when elfcorehdr
is not the last segment, such as:
kexec -d --t bzImage -p bzImage --initrd=/bin/true
Add an "e_type == ET_CORE" check to ensure the correct segment is
identified, and break early after finding it.
Signed-off-by: Jinjie Ruan <ruanjinjie@huawei.com>
---
kernel/crash_core.c | 7 ++++++-
1 file changed, 6 insertions(+), 1 deletion(-)
diff --git a/kernel/crash_core.c b/kernel/crash_core.c
index bd3f82b62751..0730c4ea8054 100644
--- a/kernel/crash_core.c
+++ b/kernel/crash_core.c
@@ -737,9 +737,14 @@ static void crash_handle_hotplug_event(unsigned int hp_action, unsigned int cpu,
mem = image->segment[n].mem;
ptr = kmap_local_page(pfn_to_page(mem >> PAGE_SHIFT));
if (ptr) {
+ Elf64_Ehdr *ehdr = (Elf64_Ehdr *)ptr;
+
/* The segment containing elfcorehdr */
- if (memcmp(ptr, ELFMAG, SELFMAG) == 0)
+ if (memcmp(ptr, ELFMAG, SELFMAG) == 0 && ehdr->e_type == ET_CORE) {
image->elfcorehdr_index = (int)n;
+ kunmap_local(ptr);
+ break;
+ }
kunmap_local(ptr);
}
}
--
2.34.1
^ permalink raw reply [flat|nested] 19+ messages in thread
* [PATCH v3 14/17] x86/crash: Simplify crash_load_segments() using crash_extra_elfcorehdr_size()
2026-08-26 9:25 [PATCH v3 00/17] crash: Rework and add arm64 crash hotplug support Jinjie Ruan
` (12 preceding siblings ...)
2026-08-26 9:25 ` [PATCH v3 13/17] crash: Improve elfcorehdr segment identification Jinjie Ruan
@ 2026-08-26 9:25 ` Jinjie Ruan
2026-08-26 9:25 ` [PATCH v3 15/17] crash: Simplify CRASH_MAX_MEMORY_RANGES handling Jinjie Ruan
` (2 subsequent siblings)
16 siblings, 0 replies; 19+ messages in thread
From: Jinjie Ruan @ 2026-08-26 9:25 UTC (permalink / raw)
To: catalin.marinas, will, mark.rutland, chenhuacai, kernel, maddy,
mpe, npiggin, chleroy, tglx, mingo, bp, dave.hansen, hpa, akpm,
baoquan.he, rppt, pasha.tatashin, pratyush, ruirui.yang, kees,
thuth, gshan, jic23, james.morse, ardb, leitao, yeoreum.yun,
sourabhjain, coxu, tangyouling, hbathini, adityag, ionut.nechita,
liaoyuanhong, seanjc, fuqiang.wang, makb, piliu, ebiggers,
jbouron, mclapinski, me, graf, bgwin, robh, takahiro.akashi,
palmer, x86, linux-arm-kernel, linux-kernel, loongarch,
linuxppc-dev, kexec, linux-fsdevel, linux-mm
Cc: ruanjinjie
Replace the open-coded elfcorehdr reservation logic in
crash_load_segments() with the generic crash_extra_elfcorehdr_size()
helper. The helper centralizes all PN_XNUM and memory range checks,
reducing code duplication.
The elfcorehdr_index is handled separately in crash_handle_hotplug_event()
during the first hotplug event, so it does not need to be set in
crash_load_segments().
Cc: Thomas Gleixner <tglx@kernel.org>
Cc: Ingo Molnar <mingo@redhat.com>
Cc: Borislav Petkov <bp@alien8.de>
Cc: Dave Hansen <dave.hansen@linux.intel.com>
Cc: "H. Peter Anvin" <hpa@zytor.com>
Cc: Andrew Morton <akpm@linux-foundation.org>
Cc: Baoquan He <baoquan.he@linux.dev>
Cc: "Mike Rapoport (Microsoft)" <rppt@kernel.org>
Signed-off-by: Jinjie Ruan <ruanjinjie@huawei.com>
---
arch/x86/kernel/crash.c | 32 +++++---------------------------
1 file changed, 5 insertions(+), 27 deletions(-)
diff --git a/arch/x86/kernel/crash.c b/arch/x86/kernel/crash.c
index afe2aea19f47..99eb8583d932 100644
--- a/arch/x86/kernel/crash.c
+++ b/arch/x86/kernel/crash.c
@@ -348,42 +348,20 @@ int crash_setup_memmap_entries(struct kimage *image, struct boot_params *params)
int crash_load_segments(struct kimage *image)
{
- int ret;
- unsigned long pnum = 0;
struct kexec_buf kbuf = { .image = image, .buf_min = 0,
.buf_max = ULONG_MAX, .top_down = false };
+ unsigned long nr_ranges = 0;
+ int ret;
/* Prepare elf headers and add a segment */
ret = crash_prepare_headers(IS_ENABLED(CONFIG_X86_64), &kbuf.buffer,
- &kbuf.bufsz, &pnum);
+ &kbuf.bufsz, &nr_ranges);
if (ret)
return ret;
image->elf_headers = kbuf.buffer;
- image->elf_headers_sz = kbuf.bufsz;
- kbuf.memsz = kbuf.bufsz;
-
-#ifdef CONFIG_CRASH_HOTPLUG
- /*
- * The elfcorehdr segment size accounts for VMCOREINFO, kernel_map,
- * maximum CPUs and maximum memory ranges.
- */
- if (IS_ENABLED(CONFIG_MEMORY_HOTPLUG))
- pnum = 2 + num_possible_cpus() + CONFIG_CRASH_MAX_MEMORY_RANGES;
- else
- pnum += 2 + num_possible_cpus();
-
- if (pnum < (unsigned long)PN_XNUM) {
- kbuf.memsz = elf64_phdr_size(pnum);
-
- image->elfcorehdr_index = image->nr_segments;
-
- /* Mark as usable to crash kernel, else crash kernel fails on boot */
- image->elf_headers_sz = kbuf.memsz;
- } else {
- pr_err("number of Phdrs %lu exceeds max\n", pnum);
- }
-#endif
+ kbuf.memsz = kbuf.bufsz + crash_extra_elfcorehdr_size(nr_ranges);
+ image->elf_headers_sz = kbuf.memsz;
kbuf.buf_align = ELF_CORE_HEADER_ALIGN;
kbuf.mem = KEXEC_BUF_MEM_UNKNOWN;
--
2.34.1
^ permalink raw reply [flat|nested] 19+ messages in thread
* [PATCH v3 15/17] crash: Simplify CRASH_MAX_MEMORY_RANGES handling
2026-08-26 9:25 [PATCH v3 00/17] crash: Rework and add arm64 crash hotplug support Jinjie Ruan
` (13 preceding siblings ...)
2026-08-26 9:25 ` [PATCH v3 14/17] x86/crash: Simplify crash_load_segments() using crash_extra_elfcorehdr_size() Jinjie Ruan
@ 2026-08-26 9:25 ` Jinjie Ruan
2026-08-26 9:25 ` [PATCH v3 16/17] arm64: kexec_file: Simplify load_other_segments() Jinjie Ruan
2026-08-26 9:25 ` [PATCH v3 17/17] arm64: crash: Add crash hotplug support Jinjie Ruan
16 siblings, 0 replies; 19+ messages in thread
From: Jinjie Ruan @ 2026-08-26 9:25 UTC (permalink / raw)
To: catalin.marinas, will, mark.rutland, chenhuacai, kernel, maddy,
mpe, npiggin, chleroy, tglx, mingo, bp, dave.hansen, hpa, akpm,
baoquan.he, rppt, pasha.tatashin, pratyush, ruirui.yang, kees,
thuth, gshan, jic23, james.morse, ardb, leitao, yeoreum.yun,
sourabhjain, coxu, tangyouling, hbathini, adityag, ionut.nechita,
liaoyuanhong, seanjc, fuqiang.wang, makb, piliu, ebiggers,
jbouron, mclapinski, me, graf, bgwin, robh, takahiro.akashi,
palmer, x86, linux-arm-kernel, linux-kernel, loongarch,
linuxppc-dev, kexec, linux-fsdevel, linux-mm
Cc: ruanjinjie
Set CRASH_MAX_MEMORY_RANGES to 0 when MEMORY_HOTPLUG is disabled, then
remove the now unnecessary IS_ENABLED(CONFIG_MEMORY_HOTPLUG) checks in
powerpc and x86 arch_crash_get_elfcorehdr_size().
No functional change intended.
Signed-off-by: Jinjie Ruan <ruanjinjie@huawei.com>
---
arch/powerpc/kexec/crash.c | 4 +---
arch/x86/kernel/crash.c | 4 +---
kernel/Kconfig.kexec | 3 ++-
3 files changed, 4 insertions(+), 7 deletions(-)
diff --git a/arch/powerpc/kexec/crash.c b/arch/powerpc/kexec/crash.c
index fc0105c7af4c..c47e3f867ea1 100644
--- a/arch/powerpc/kexec/crash.c
+++ b/arch/powerpc/kexec/crash.c
@@ -474,9 +474,7 @@ unsigned int arch_crash_get_elfcorehdr_size(void)
unsigned long phdr_cnt;
/* A program header for possible CPUs + vmcoreinfo */
- phdr_cnt = num_possible_cpus() + 1;
- if (IS_ENABLED(CONFIG_MEMORY_HOTPLUG))
- phdr_cnt += CONFIG_CRASH_MAX_MEMORY_RANGES;
+ phdr_cnt = num_possible_cpus() + 1 + CONFIG_CRASH_MAX_MEMORY_RANGES;
return elf64_phdr_size(phdr_cnt);
}
diff --git a/arch/x86/kernel/crash.c b/arch/x86/kernel/crash.c
index 99eb8583d932..116410ac90fb 100644
--- a/arch/x86/kernel/crash.c
+++ b/arch/x86/kernel/crash.c
@@ -407,9 +407,7 @@ unsigned int arch_crash_get_elfcorehdr_size(void)
unsigned int sz;
/* kernel_map, VMCOREINFO and maximum CPUs */
- sz = 2 + num_possible_cpus();
- if (IS_ENABLED(CONFIG_MEMORY_HOTPLUG))
- sz += CONFIG_CRASH_MAX_MEMORY_RANGES;
+ sz = 2 + num_possible_cpus() + CONFIG_CRASH_MAX_MEMORY_RANGES;
sz *= sizeof(Elf64_Phdr);
return sz;
}
diff --git a/kernel/Kconfig.kexec b/kernel/Kconfig.kexec
index a97ed9605602..e6251dfe451a 100644
--- a/kernel/Kconfig.kexec
+++ b/kernel/Kconfig.kexec
@@ -160,7 +160,8 @@ config CRASH_HOTPLUG
config CRASH_MAX_MEMORY_RANGES
int "Specify the maximum number of memory regions for the elfcorehdr"
- default 8192
+ default 8192 if MEMORY_HOTPLUG
+ default 0 if !MEMORY_HOTPLUG
depends on CRASH_HOTPLUG
help
For the kexec_file_load() syscall path, specify the maximum number of
--
2.34.1
^ permalink raw reply [flat|nested] 19+ messages in thread
* [PATCH v3 16/17] arm64: kexec_file: Simplify load_other_segments()
2026-08-26 9:25 [PATCH v3 00/17] crash: Rework and add arm64 crash hotplug support Jinjie Ruan
` (14 preceding siblings ...)
2026-08-26 9:25 ` [PATCH v3 15/17] crash: Simplify CRASH_MAX_MEMORY_RANGES handling Jinjie Ruan
@ 2026-08-26 9:25 ` Jinjie Ruan
2026-08-26 9:25 ` [PATCH v3 17/17] arm64: crash: Add crash hotplug support Jinjie Ruan
16 siblings, 0 replies; 19+ messages in thread
From: Jinjie Ruan @ 2026-08-26 9:25 UTC (permalink / raw)
To: catalin.marinas, will, mark.rutland, chenhuacai, kernel, maddy,
mpe, npiggin, chleroy, tglx, mingo, bp, dave.hansen, hpa, akpm,
baoquan.he, rppt, pasha.tatashin, pratyush, ruirui.yang, kees,
thuth, gshan, jic23, james.morse, ardb, leitao, yeoreum.yun,
sourabhjain, coxu, tangyouling, hbathini, adityag, ionut.nechita,
liaoyuanhong, seanjc, fuqiang.wang, makb, piliu, ebiggers,
jbouron, mclapinski, me, graf, bgwin, robh, takahiro.akashi,
palmer, x86, linux-arm-kernel, linux-kernel, loongarch,
linuxppc-dev, kexec, linux-fsdevel, linux-mm
Cc: ruanjinjie
Use `kbuf` fields directly in crash_prepare_headers() to eliminate
the local variables "headers" and "headers_sz"..
Advance the assignment to image->elf_headers before
calling kexec_add_buffer(). If kexec_add_buffer() fails, the explicit
vfree() in the error path can be removed, as the global infrastructure
in arch_kimage_file_post_load_cleanup() will handle the cleanup.
Cc: Catalin Marinas <catalin.marinas@arm.com>
Cc: Will Deacon <will@kernel.org>
Cc: Baoquan He <bhe@redhat.com>
Cc: Breno Leitao <leitao@debian.org>
Signed-off-by: Jinjie Ruan <ruanjinjie@huawei.com>
---
arch/arm64/kernel/machine_kexec_file.c | 24 +++++++++---------------
1 file changed, 9 insertions(+), 15 deletions(-)
diff --git a/arch/arm64/kernel/machine_kexec_file.c b/arch/arm64/kernel/machine_kexec_file.c
index 2f750e5f4fcc..3d907f8ee594 100644
--- a/arch/arm64/kernel/machine_kexec_file.c
+++ b/arch/arm64/kernel/machine_kexec_file.c
@@ -89,36 +89,30 @@ int load_other_segments(struct kimage *image,
kbuf.buf_min = kernel_load_addr + kernel_size;
#ifdef CONFIG_CRASH_DUMP
- /* load elf core header */
- void *headers;
- unsigned long headers_sz;
if (image->type == KEXEC_TYPE_CRASH) {
- ret = crash_prepare_headers(true, &headers, &headers_sz, NULL);
+ ret = crash_prepare_headers(true, &kbuf.buffer, &kbuf.bufsz, NULL);
if (ret) {
pr_err("Preparing elf core header failed\n");
goto out_err;
}
- kbuf.buffer = headers;
- kbuf.bufsz = headers_sz;
+ if (unlikely(image->elf_headers))
+ vfree(image->elf_headers);
+
+ image->elf_headers = kbuf.buffer;
+ image->elf_headers_sz = kbuf.bufsz;
+
kbuf.mem = KEXEC_BUF_MEM_UNKNOWN;
- kbuf.memsz = headers_sz;
+ kbuf.memsz = kbuf.bufsz;
kbuf.buf_align = SZ_64K; /* largest supported page size */
kbuf.buf_max = ULONG_MAX;
kbuf.top_down = true;
ret = kexec_add_buffer(&kbuf);
- if (ret) {
- vfree(headers);
+ if (ret)
goto out_err;
- }
-
- if (unlikely(image->elf_headers))
- vfree(image->elf_headers);
- image->elf_headers = headers;
image->elf_load_addr = kbuf.mem;
- image->elf_headers_sz = headers_sz;
kexec_dprintk("Loaded elf core header at 0x%lx bufsz=0x%lx memsz=0x%lx\n",
image->elf_load_addr, kbuf.bufsz, kbuf.memsz);
--
2.34.1
^ permalink raw reply [flat|nested] 19+ messages in thread
* [PATCH v3 17/17] arm64: crash: Add crash hotplug support
2026-08-26 9:25 [PATCH v3 00/17] crash: Rework and add arm64 crash hotplug support Jinjie Ruan
` (15 preceding siblings ...)
2026-08-26 9:25 ` [PATCH v3 16/17] arm64: kexec_file: Simplify load_other_segments() Jinjie Ruan
@ 2026-08-26 9:25 ` Jinjie Ruan
16 siblings, 0 replies; 19+ messages in thread
From: Jinjie Ruan @ 2026-08-26 9:25 UTC (permalink / raw)
To: catalin.marinas, will, mark.rutland, chenhuacai, kernel, maddy,
mpe, npiggin, chleroy, tglx, mingo, bp, dave.hansen, hpa, akpm,
baoquan.he, rppt, pasha.tatashin, pratyush, ruirui.yang, kees,
thuth, gshan, jic23, james.morse, ardb, leitao, yeoreum.yun,
sourabhjain, coxu, tangyouling, hbathini, adityag, ionut.nechita,
liaoyuanhong, seanjc, fuqiang.wang, makb, piliu, ebiggers,
jbouron, mclapinski, me, graf, bgwin, robh, takahiro.akashi,
palmer, x86, linux-arm-kernel, linux-kernel, loongarch,
linuxppc-dev, kexec, linux-fsdevel, linux-mm
Cc: ruanjinjie
When CPU or memory hotplug events occur, the elfcorehdr in the kdump
image becomes stale, potentially leading to incomplete crash dumps.
Currently, userspace udev rules reload the entire kdump image upon such
events, which is inefficient and leaves kdump inactive for a long time.
Commit 247262756121 ("crash: add generic infrastructure for crash hotplug
support") introduced a kernel mechanism to update only the elfcorehdr.
This patch enables that support for arm64.
On arm64, only memory hotplug events require elfcorehdr updates:
- Physical CPU hotplug is not supported.
- For ACPI based vCPU hotplug [1], the elfcorehdr is built using
for_each_possible_cpu(), so no update is needed.
The patch:
- Adds CONFIG_ARCH_SUPPORTS_CRASH_HOTPLUG (default y).
- Implements following arch functions to handle memory hotplug:
1. arch_crash_hotplug_support()
2. arch_crash_get_elfcorehdr_size()
3. arch_crash_handle_hotplug_event()
- Moves arch_get_system_nr_ranges() and arch_crash_populate_cmem()
from machine_kexec_file.c to crash.c for crash hotplug reuse.
Follows the approach of x86 commit ea53ad9cf73b ("x86/crash: add x86 crash
hotplug support") and powerpc commit b741092d5976 ("powerpc/crash: add
crash CPU hotplug support").
Cc: Catalin Marinas <catalin.marinas@arm.com>
Cc: Will Deacon <will@kernel.org>
Cc: Baoquan He <bhe@redhat.com>
Cc: "Mike Rapoport (Microsoft)" <rppt@kernel.org>
Cc: Andrew Morton <akpm@linux-foundation.org>
Cc: Breno Leitao <leitao@debian.org>
Cc: Sourabh Jain <sourabhjain@linux.ibm.com>
Cc: Mark Rutland <mark.rutland@arm.com>
Cc: Ard Biesheuvel <ardb@kernel.org>
Cc: Thomas Huth <thuth@redhat.com>
[1]: https://lore.kernel.org/all/20240529133446.28446-1-Jonathan.Cameron@huawei.com/
Signed-off-by: Jinjie Ruan <ruanjinjie@huawei.com>
---
arch/arm64/Kconfig | 3 +
arch/arm64/include/asm/kexec.h | 11 ++
arch/arm64/kernel/Makefile | 2 +-
arch/arm64/kernel/crash.c | 165 +++++++++++++++++++++++++
arch/arm64/kernel/machine_kexec_file.c | 36 +-----
5 files changed, 184 insertions(+), 33 deletions(-)
create mode 100644 arch/arm64/kernel/crash.c
diff --git a/arch/arm64/Kconfig b/arch/arm64/Kconfig
index b5a51b0ef944..96de2660b1cb 100644
--- a/arch/arm64/Kconfig
+++ b/arch/arm64/Kconfig
@@ -1720,6 +1720,9 @@ config ARCH_DEFAULT_CRASH_DUMP
config ARCH_HAS_GENERIC_CRASHKERNEL_RESERVATION
def_bool CRASH_RESERVE
+config ARCH_SUPPORTS_CRASH_HOTPLUG
+ def_bool y
+
config TRANS_TABLE
def_bool y
depends on HIBERNATION || KEXEC_CORE
diff --git a/arch/arm64/include/asm/kexec.h b/arch/arm64/include/asm/kexec.h
index 892e5bebda95..f165c094b32e 100644
--- a/arch/arm64/include/asm/kexec.h
+++ b/arch/arm64/include/asm/kexec.h
@@ -130,6 +130,17 @@ extern int load_other_segments(struct kimage *image,
char *cmdline);
#endif
+#ifdef CONFIG_CRASH_HOTPLUG
+void arch_crash_handle_hotplug_event(struct kimage *image, void *arg);
+#define arch_crash_handle_hotplug_event arch_crash_handle_hotplug_event
+
+int arch_crash_hotplug_support(struct kimage *image, unsigned long kexec_flags);
+#define arch_crash_hotplug_support arch_crash_hotplug_support
+
+unsigned int arch_crash_get_elfcorehdr_size(void);
+#define crash_get_elfcorehdr_size arch_crash_get_elfcorehdr_size
+#endif
+
#endif /* __ASSEMBLER__ */
#endif
diff --git a/arch/arm64/kernel/Makefile b/arch/arm64/kernel/Makefile
index d2690c3ec528..9bbac452994c 100644
--- a/arch/arm64/kernel/Makefile
+++ b/arch/arm64/kernel/Makefile
@@ -64,7 +64,7 @@ obj-$(CONFIG_KEXEC_CORE) += machine_kexec.o relocate_kernel.o \
obj-$(CONFIG_KEXEC_FILE) += machine_kexec_file.o kexec_image.o
obj-$(CONFIG_ARM64_RELOC_TEST) += arm64-reloc-test.o
arm64-reloc-test-y := reloc_test_core.o reloc_test_syms.o
-obj-$(CONFIG_CRASH_DUMP) += crash_dump.o
+obj-$(CONFIG_CRASH_DUMP) += crash_dump.o crash.o
obj-$(CONFIG_VMCORE_INFO) += vmcore_info.o
obj-$(CONFIG_ARM_SDE_INTERFACE) += sdei.o
obj-$(CONFIG_ARM64_PTR_AUTH) += pointer_auth.o
diff --git a/arch/arm64/kernel/crash.c b/arch/arm64/kernel/crash.c
new file mode 100644
index 000000000000..d97e9cea2fc9
--- /dev/null
+++ b/arch/arm64/kernel/crash.c
@@ -0,0 +1,165 @@
+// SPDX-License-Identifier: GPL-2.0-only
+/*
+ * Architecture specific functions for kexec based crash dumps.
+ */
+
+#define pr_fmt(fmt) "crash hp: " fmt
+
+#include <linux/cacheflush.h>
+#include <linux/elf.h>
+#include <linux/kexec.h>
+#include <linux/memblock.h>
+#include <linux/memory.h>
+#include <linux/vmalloc.h>
+
+#include <asm/kexec.h>
+
+#if defined(CONFIG_KEXEC_FILE) || defined(CONFIG_CRASH_HOTPLUG)
+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
+
+#ifdef CONFIG_CRASH_HOTPLUG
+int arch_crash_hotplug_support(struct kimage *image, unsigned long kexec_flags)
+{
+#ifdef CONFIG_KEXEC_FILE
+ if (image->file_mode)
+ return 1;
+#endif
+ /*
+ * For kexec_load syscall, crash hotplug support requires
+ * KEXEC_CRASH_HOTPLUG_SUPPORT flag to be passed by userspace.
+ */
+ return kexec_flags & KEXEC_CRASH_HOTPLUG_SUPPORT;
+}
+
+unsigned int arch_crash_get_elfcorehdr_size(void)
+{
+ unsigned long phdr_cnt;
+
+ /* A program header for possible CPUs, vmcoreinfo and kernel_map */
+ phdr_cnt = 2 + num_possible_cpus() + CONFIG_CRASH_MAX_MEMORY_RANGES;
+
+ return elf64_phdr_size(phdr_cnt);
+}
+
+/**
+ * update_crash_elfcorehdr() - Recreate the elfcorehdr and replace it with old
+ * elfcorehdr in the kexec segment array.
+ * @image: the active struct kimage
+ * @mn: struct memory_notify data handler
+ */
+static void update_crash_elfcorehdr(struct kimage *image, struct memory_notify *mn)
+{
+ void *elfbuf = NULL, *old_elfcorehdr;
+ unsigned long mem, memsz, elfsz = 0;
+ struct crash_mem *cmem = NULL;
+ u64 start, end;
+ int ret;
+
+ ret = crash_get_memory_ranges_nolock(&cmem);
+ if (ret) {
+ pr_err("Failed to get crash memory ranges.\n");
+ goto out;
+ }
+
+ /*
+ * The hot unplugged memory is part of crash memory ranges,
+ * remove it here.
+ */
+ if (image->hp_action == KEXEC_CRASH_HP_REMOVE_MEMORY) {
+ start = PFN_PHYS(mn->start_pfn);
+ end = start + PFN_PHYS(mn->nr_pages) - 1;
+
+ ret = crash_exclude_mem_range(cmem, start, end);
+ if (ret) {
+ pr_err("Failed to remove hot-unplugged memory from crash memory ranges.\n");
+ goto out;
+ }
+ }
+
+ /*
+ * Create the new elfcorehdr reflecting the changes to CPU and/or
+ * memory resources.
+ */
+ ret = crash_prepare_elf64_headers(cmem, true, &elfbuf, &elfsz);
+ if (ret) {
+ pr_err("Failed to create new elfcorehdr");
+ goto out;
+ }
+
+ /*
+ * Obtain address and size of the elfcorehdr segment, and
+ * check it against the new elfcorehdr buffer.
+ */
+ mem = image->segment[image->elfcorehdr_index].mem;
+ memsz = image->segment[image->elfcorehdr_index].memsz;
+ if (elfsz > memsz) {
+ pr_err("update elfcorehdr elfsz %lu > memsz %lu",
+ elfsz, memsz);
+ goto out;
+ }
+
+ /* Copy new elfcorehdr over the old elfcorehdr at destination. */
+ old_elfcorehdr = phys_to_virt(mem);
+
+ /*
+ * Temporarily invalidate the crash image while the
+ * elfcorehdr is updated.
+ */
+ xchg(&kexec_crash_image, NULL);
+ memcpy(old_elfcorehdr, elfbuf, elfsz);
+ dcache_clean_inval_poc((unsigned long)old_elfcorehdr,
+ (unsigned long)(old_elfcorehdr + elfsz));
+ xchg(&kexec_crash_image, image);
+ pr_debug("updated elfcorehdr\n");
+
+out:
+ kvfree(cmem);
+ vfree(elfbuf);
+}
+
+/**
+ * arch_crash_handle_hotplug_event() - Handle hotplug elfcorehdr changes
+ * @image: a pointer to kexec_crash_image
+ * @arg: struct memory_notify handler for memory hotplug case and
+ * NULL for CPU hotplug case.
+ *
+ * Update the kdump image based on the type of hotplug event:
+ * - CPU add and remove: No action is needed.
+ * - Memory add/remove: Update the elfcorehdr to reflect the current memory layout.
+ *
+ * Prepare the new elfcorehdr and replace the existing elfcorehdr.
+ */
+void arch_crash_handle_hotplug_event(struct kimage *image, void *arg)
+{
+ if (image->hp_action == KEXEC_CRASH_HP_ADD_CPU ||
+ image->hp_action == KEXEC_CRASH_HP_REMOVE_CPU)
+ return;
+
+ update_crash_elfcorehdr(image, (struct memory_notify *)arg);
+}
+#endif /* CONFIG_CRASH_HOTPLUG */
diff --git a/arch/arm64/kernel/machine_kexec_file.c b/arch/arm64/kernel/machine_kexec_file.c
index 3d907f8ee594..acba49b04ae1 100644
--- a/arch/arm64/kernel/machine_kexec_file.c
+++ b/arch/arm64/kernel/machine_kexec_file.c
@@ -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
@@ -89,8 +60,9 @@ int load_other_segments(struct kimage *image,
kbuf.buf_min = kernel_load_addr + kernel_size;
#ifdef CONFIG_CRASH_DUMP
+ unsigned long nr_ranges = 0;
if (image->type == KEXEC_TYPE_CRASH) {
- ret = crash_prepare_headers(true, &kbuf.buffer, &kbuf.bufsz, NULL);
+ ret = crash_prepare_headers(true, &kbuf.buffer, &kbuf.bufsz, &nr_ranges);
if (ret) {
pr_err("Preparing elf core header failed\n");
goto out_err;
@@ -100,10 +72,10 @@ int load_other_segments(struct kimage *image,
vfree(image->elf_headers);
image->elf_headers = kbuf.buffer;
- image->elf_headers_sz = kbuf.bufsz;
kbuf.mem = KEXEC_BUF_MEM_UNKNOWN;
- kbuf.memsz = kbuf.bufsz;
+ kbuf.memsz = kbuf.bufsz + crash_extra_elfcorehdr_size(nr_ranges);
+ image->elf_headers_sz = kbuf.memsz;
kbuf.buf_align = SZ_64K; /* largest supported page size */
kbuf.buf_max = ULONG_MAX;
kbuf.top_down = true;
--
2.34.1
^ permalink raw reply [flat|nested] 19+ messages in thread
* Re: [PATCH v3 07/17] crash_dump: Fix potential double-free of keys_header
2026-08-26 9:25 ` [PATCH v3 07/17] crash_dump: Fix potential double-free of keys_header Jinjie Ruan
@ 2026-08-30 5:59 ` Sourabh Jain
0 siblings, 0 replies; 19+ messages in thread
From: Sourabh Jain @ 2026-08-30 5:59 UTC (permalink / raw)
To: Jinjie Ruan, catalin.marinas, will, mark.rutland, chenhuacai,
kernel, maddy, mpe, npiggin, chleroy, tglx, mingo, bp,
dave.hansen, hpa, akpm, baoquan.he, rppt, pasha.tatashin,
pratyush, ruirui.yang, kees, thuth, gshan, jic23, james.morse,
ardb, leitao, yeoreum.yun, coxu, tangyouling, hbathini, adityag,
ionut.nechita, liaoyuanhong, seanjc, fuqiang.wang, makb, piliu,
ebiggers, jbouron, mclapinski, me, graf, bgwin, robh,
takahiro.akashi, palmer, x86, linux-arm-kernel, linux-kernel,
loongarch, linuxppc-dev, kexec, linux-fsdevel, linux-mm,
Coiby Xu
Hello Jinjie,
Coiby is handling this issue in the below patch series:
https://lore.kernel.org/all/20260828084900.1496839-2-coiby.xu@gmail.com/
Since the above patch series is all about crash_load_dm_crypt_keys,
could you
please consider dropping this patch from your series and reviewing his patch
instead?
Thanks,
Sourabh Jain
On 26/08/26 14:55, Jinjie Ruan wrote:
> `keys_header` was freed in `build_keys_header()` without being reset
> to NULL, and the error path in `crash_load_dm_crypt_keys()` freed it
> unconditionally even when reused, leading to double-free or
> use-after-free.
>
> Add `free_keys_header()` to centralize freeing and NULL-setting.
> Use it in `build_keys_header()` and only free in the error path when
> the header was newly built (`!is_dm_key_reused`).
>
> Cc: Andrew Morton <akpm@linux-foundation.org>
> Cc: Baoquan He <bhe@redhat.com>
> Cc: Mike Rapoport <rppt@kernel.org>
> Cc: Pasha Tatashin <pasha.tatashin@soleen.com>
> Cc: Pratyush Yadav <pratyush@kernel.org>
> Cc: Dave Young <ruirui.yang@linux.dev>
> Cc: stable@vger.kernel.org
> Fixes: e3a84be1ec2f ("arm64,ppc64le/kdump: pass dm-crypt keys to kdump kernel")
> Signed-off-by: Jinjie Ruan <ruanjinjie@huawei.com>
> ---
> kernel/crash_dump_dm_crypt.c | 15 +++++++++++----
> 1 file changed, 11 insertions(+), 4 deletions(-)
>
> diff --git a/kernel/crash_dump_dm_crypt.c b/kernel/crash_dump_dm_crypt.c
> index c685497cd470..ed0960ff0987 100644
> --- a/kernel/crash_dump_dm_crypt.c
> +++ b/kernel/crash_dump_dm_crypt.c
> @@ -363,15 +363,21 @@ static struct configfs_subsystem config_keys_subsys = {
> },
> };
>
> +static void free_keys_header(void)
> +{
> + if (keys_header) {
> + kvfree(keys_header);
> + keys_header = NULL;
> + }
> +}
> +
> static int build_keys_header(void)
> {
> struct config_item *item = NULL;
> struct config_key *key;
> int i, r;
>
> - if (keys_header != NULL)
> - kvfree(keys_header);
> -
> + free_keys_header();
> keys_header = kzalloc(get_keys_header_size(key_count), GFP_KERNEL);
> if (!keys_header)
> return -ENOMEM;
> @@ -441,7 +447,8 @@ int crash_load_dm_crypt_keys(struct kimage *image)
> r = kexec_add_buffer(&kbuf);
> if (r) {
> pr_err("Failed to call kexec_add_buffer, ret=%d\n", r);
> - kvfree((void *)kbuf.buffer);
> + if (!is_dm_key_reused)
> + free_keys_header();
> return r;
> }
> image->dm_crypt_keys_addr = kbuf.mem;
^ permalink raw reply [flat|nested] 19+ messages in thread
end of thread, other threads:[~2026-08-30 6:01 UTC | newest]
Thread overview: 19+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-08-26 9:25 [PATCH v3 00/17] crash: Rework and add arm64 crash hotplug support Jinjie Ruan
2026-08-26 9:25 ` [PATCH v3 01/17] kexec: Record allocated CMA pages to fix release size mismatch Jinjie Ruan
2026-08-26 9:25 ` [PATCH v3 02/17] kexec: Extract kexec_free_segment_cma() from kimage_free_cma() Jinjie Ruan
2026-08-26 9:25 ` [PATCH v3 03/17] arm64: kexec_file: Fix CMA page leaks in segment placement retry loops Jinjie Ruan
2026-08-26 9:25 ` [PATCH v3 04/17] arm64: kexec_file: Fix elf_headers memory leak in retry loop Jinjie Ruan
2026-08-26 9:25 ` [PATCH v3 05/17] LoongArch: kexec: Fix CMA page leaks in segment placement retry loops Jinjie Ruan
2026-08-26 9:25 ` [PATCH v3 06/17] LoongArch: kexec_file: Fix elf_headers memory leak in retry loop Jinjie Ruan
2026-08-26 9:25 ` [PATCH v3 07/17] crash_dump: Fix potential double-free of keys_header Jinjie Ruan
2026-08-30 5:59 ` Sourabh Jain
2026-08-26 9:25 ` [PATCH v3 08/17] crash: Extract crash_get_memory_ranges() helper Jinjie Ruan
2026-08-26 9:25 ` [PATCH v3 09/17] crash: Fix TOCTOU race in crash memory range collection Jinjie Ruan
2026-08-26 9:25 ` [PATCH v3 10/17] elf: Introduce elf64_phdr_size() helper Jinjie Ruan
2026-08-26 9:25 ` [PATCH v3 11/17] crash: Introduce crash_extra_elfcorehdr_size() helper Jinjie Ruan
2026-08-26 9:25 ` [PATCH v3 12/17] x86/crash: Use num_possible_cpus() for elfcorehdr size Jinjie Ruan
2026-08-26 9:25 ` [PATCH v3 13/17] crash: Improve elfcorehdr segment identification Jinjie Ruan
2026-08-26 9:25 ` [PATCH v3 14/17] x86/crash: Simplify crash_load_segments() using crash_extra_elfcorehdr_size() Jinjie Ruan
2026-08-26 9:25 ` [PATCH v3 15/17] crash: Simplify CRASH_MAX_MEMORY_RANGES handling Jinjie Ruan
2026-08-26 9:25 ` [PATCH v3 16/17] arm64: kexec_file: Simplify load_other_segments() Jinjie Ruan
2026-08-26 9:25 ` [PATCH v3 17/17] arm64: crash: Add crash hotplug support Jinjie Ruan
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®