x86/crash: Include Elf64_Ehdr and RAM ranges in elfcorehdr size
Subsystems (3)
PATCH 1/1 x86/crash: Include Elf64_Ehdr and RAM ranges in elfcorehdr size
arch_crash_get_elfcorehdr_size() computes the size exported via /sys/kernel/kexec/crash_elfcorehdr_size for kexec_load() with crash hotplug support. Unlike crash_load_segments(), it omits both sizeof(Elf64_Ehdr) and the existing System RAM ranges when CONFIG_MEMORY_HOTPLUG is disabled.
With --hotplug, kexec-tools overrides the elfcorehdr segment memsz with this sysfs value and truncates bufsz if bufsz > memsz, so the loaded elfcorehdr can be silently truncated on CONFIG_MEMORY_HOTPLUG=n systems, and subsequent hotplug updates fail. For example, on a default CONFIG_NR_CPUS=64 kernel with CONFIG_MEMORY_HOTPLUG=n, 64 possible CPUs, and 10 System RAM ranges, crash_elfcorehdr_size reports 3696 bytes (memsz 4096) and CPU offline fails with:
crash hp: update elfcorehdr elfsz 8192 > memsz 4096
Add arch_get_system_nr_ranges() when !IS_ENABLED(CONFIG_MEMORY_HOTPLUG) and include sizeof(Elf64_Ehdr) so arch_crash_get_elfcorehdr_size() matches crash_load_segments(). Tested with kexec-tools 2.0.32 in QEMU under the configuration above: crash_elfcorehdr_size reports 4488 bytes (memsz 8192) and CPU offline updates elfcorehdr cleanly.
Gemini assisted with code analysis and the QEMU reproducer. All changes and test results were reviewed by the author.
--- arch/x86/kernel/crash.c | 3 +++ 1 file changed, 3 insertions(+)
arch/x86/kernel/crash.c +3 -0
@@ -433,7 +433,10 @@ unsigned int arch_crash_get_elfcorehdr_size(void) sz =2 +CONFIG_NR_CPUS ;if (IS_ENABLED (CONFIG_MEMORY_HOTPLUG )) sz +=CONFIG_CRASH_MAX_MEMORY_RANGES ; +else + sz +=arch_get_system_nr_ranges (); sz *=sizeof (Elf64_Phdr); + sz +=sizeof (Elf64_Ehdr);return sz; }
base-commit: fe6ff8d5d675c01793b9f051d16055dd4aa75244
Signature · 1 line
2.56.1.911.g883d8bcb20-goog
[email protected]
AI reviewSashiko has reviewed this patch and found no issues.
Quoted 22 lines
arch_crash_get_elfcorehdr_size()computes the size exported via/sys/kernel/kexec/crash_elfcorehdr_sizeforkexec_load()with crash hotplug support. Unlikecrash_load_segments(), it omits bothsizeof(Elf64_Ehdr)and the existing System RAM ranges whenCONFIG_MEMORY_HOTPLUGis disabled.With --hotplug, kexec-tools overrides the elfcorehdr segment memsz with this sysfs value and truncates bufsz if bufsz > memsz, so the loaded elfcorehdr can be silently truncated on
CONFIG_MEMORY_HOTPLUG=nsystems, and subsequent hotplug updates fail. For example, on a defaultCONFIG_NR_CPUS=64kernel withCONFIG_MEMORY_HOTPLUG=n, 64 possible CPUs, and 10 System RAM ranges,crash_elfcorehdr_sizereports 3696 bytes (memsz 4096) and CPU offline fails with:crash hp: update elfcorehdr elfsz 8192 > memsz 4096
Add
arch_get_system_nr_ranges()when !IS_ENABLED(CONFIG_MEMORY_HOTPLUG) and includesizeof(Elf64_Ehdr)soarch_crash_get_elfcorehdr_size()matchescrash_load_segments(). Tested with kexec-tools 2.0.32 in QEMU under the configuration above:crash_elfcorehdr_sizereports 4488 bytes (memsz 8192) and CPU offline updates elfcorehdr cleanly.
Quoted 5 lines
Gemini assisted with code analysis and the QEMU reproducer. All changes and test results were reviewed by the author.
Fixes a72bbec70da2 ("crash: hotplug support for kexec_load()")Signed-off-by Suseendran Bhaskaran <[email protected]>
Sashiko has reviewed this patch and found no issues.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
Quoted 36 lines · Suseendran Bhaskaran
arch_crash_get_elfcorehdr_size()computes the size exported via/sys/kernel/kexec/crash_elfcorehdr_sizeforkexec_load()with crash hotplug support. Unlikecrash_load_segments(), it omits bothsizeof(Elf64_Ehdr)and the existing System RAM ranges whenCONFIG_MEMORY_HOTPLUGis disabled.With --hotplug, kexec-tools overrides the elfcorehdr segment memsz with this sysfs value and truncates bufsz if bufsz > memsz, so the loaded elfcorehdr can be silently truncated on
CONFIG_MEMORY_HOTPLUG=nsystems, and subsequent hotplug updates fail. For example, on a defaultCONFIG_NR_CPUS=64kernel withCONFIG_MEMORY_HOTPLUG=n, 64 possible CPUs, and 10 System RAM ranges,crash_elfcorehdr_sizereports 3696 bytes (memsz 4096) and CPU offline fails with:crash hp: update elfcorehdr elfsz 8192 > memsz 4096
Add
arch_get_system_nr_ranges()when !IS_ENABLED(CONFIG_MEMORY_HOTPLUG) and includesizeof(Elf64_Ehdr)soarch_crash_get_elfcorehdr_size()matchescrash_load_segments(). Tested with kexec-tools 2.0.32 in QEMU under the configuration above:crash_elfcorehdr_sizereports 4488 bytes (memsz 8192) and CPU offline updates elfcorehdr cleanly.Gemini assisted with code analysis and the QEMU reproducer. All changes and test results were reviewed by the author.
Fixes a72bbec70da2 ("crash: hotplug support for kexec_load()")Signed-off-by Suseendran Bhaskaran <[email protected]>--- arch/x86/kernel/crash.c | 3 +++ 1 file changed, 3 insertions(+)arch/x86/kernel/crash.c +0
-0
arch/x86/kernel/crash.c unsigned int arch_crash_get_elfcorehdr_size(void) · line 439
Quoted 7 lines
arch/x86/kernel/crash.c +3
-0@@ -433,7 +433,10 @@ unsigned int arch_crash_get_elfcorehdr_size(void) sz =2 +CONFIG_NR_CPUS ;if (IS_ENABLED (CONFIG_MEMORY_HOTPLUG )) sz +=CONFIG_CRASH_MAX_MEMORY_RANGES ; +else + sz +=arch_get_system_nr_ranges (); sz *=sizeof (Elf64_Phdr); + sz +=sizeof (Elf64_Ehdr);
This is consistent with the calculation of the number of PT_LOAD phdr sections in crash_prepare_elf64_headers().
182 >-------/* extra phdr for vmcoreinfo ELF note */
183 >-------nr_phdr = nr_cpus + 1;
184 >-------nr_phdr += mem->nr_ranges;
185
186 >-------/*
187 >------- * kexec-tools creates an extra PT_LOAD phdr for kernel text mapping
188 >------- * area (for example, ffffffff80000000 - ffffffffa0000000 on x86_64).
189 >------- * I think this is required by tools like gdb. So same physical 190 >------- * memory will be mapped in two ELF headers. One will contain kernel
191 >------- * text virtual addresses and other will have __va(physical) addresses.
192 >------- */
193
194 >-------nr_phdr++;
195 >-------elf_sz = sizeof(Elf64_Ehdr) + nr_phdr * sizeof(Elf64_Phdr); 196 >-------elf_sz = ALIGN(elf_sz, ELF_CORE_HEADER_ALIGN);
When I implemented crash hotplug on arm64, sashiko also pointed out similar issues.
Link:
https://sashiko.dev/#/patchset/20260918100442.3841135-1-ruanjinjie%40huawei.com
LGTM
Quoted 5 lines
arch/x86/kernel/crash.c +0
-0return sz; }base-commit:
fe6ff8d5d675c01793b9f051d16055dd4aa75244