x86/kexec: fix potential cmem->ranges out of memory
Subsystems (5)
PATCH v6 1/2 x86/kexec: fix potential cmem->ranges out of memory
From: fuqiang wang <[email protected]>
In memmap_exclude_ranges(), elfheader will be excluded from crashk_res. In the current x86 architecture code, the elfheader is always allocated at crashk_res.start. It seems that there won't be a new split range. But it depends on the allocation position of elfheader in crashk_res. To avoid potential out of memory in future, add a extra slot. Otherwise loading the kdump kernel will fail because crash_exclude_mem_range will return -ENOMEM. random kexec_buf for passing dm crypt keys may cause a range split too,
add another extra slot here.
The similar issue also exists in fill_up_crash_elf_data(). The range to be excluded is [0, 1M], start (0) is special and will not appear in the middle of existing cmem->ranges[]. But in cast the low 1M could be changed in the future, add a extra slot too.
Previously discussed link:
[1] https://lore.kernel.org/kexec/ZXk2oBf%2FT1Ul6o0c@MiWiFi-R3L-srv/
[2] https://lore.kernel.org/kexec/[email protected]/
[3] https://lore.kernel.org/kexec/ZYQ6O%2F57sHAPxTHm@MiWiFi-R3L-srv/
---v5->v6:
- rebase onto v6.17-rc4
v4->v5 (Baoquan):
- This is on top of Coiby's LUKS patchset in branch mm-nonmm-unstable of akpm/mm.git. I did some adaption based on Coiby's patches.
- [PATCH v9 0/8] Support kdump with LUKS encryption by reusing LUKS volume keys
arch/x86/kernel/crash.c | 23 +++++++++++++++++++---- 1 file changed, 19 insertions(+), 4 deletions(-)
arch/x86/kernel/crash.c +19 -4
@@ -165,8 +165,18 @@ static struct crash_mem *fill_up_crash_elf_data(void)/* * Exclusion of crash region, crashk_low_res and/or crashk_cma_ranges * may cause range splits. So add extra slots here. +* +* Exclusion of low 1M may not cause another range split, because the +* range of exclude is [0, 1M] and the condition for splitting a new +* region is that the start, end parameters are both in a certain +* existing region in cmem and cannot be equal to existing region's +* start or end. Obviously, the start of [0, 1M] cannot meet this +* condition. +* +* But in order to lest the low 1M could be changed in the future, +* (e.g. [start, 1M]), add a extra slot. */ - nr_ranges +=2 + crashk_cma_cnt; + nr_ranges +=3 + crashk_cma_cnt; cmem =vzalloc (struct_size (cmem, ranges, nr_ranges));if (!cmem)return NULL ;
@@ -323,10 +333,15 @@ int crash_setup_memmap_entries(struct kimage *image, struct boot_params *params)struct crash_mem *cmem;/* -* Using random kexec_buf for passing dm crypt keys may cause a range -* split. So use two slots here. +* In the current x86 architecture code, the elfheader is always +* allocated at crashk_res.start. But it depends on the allocation +* position of elfheader in crashk_res. To avoid potential out of +* bounds in future, add an extra slot. +* +* And using random kexec_buf for passing dm crypt keys may cause a +* range split too, add another extra slot here. */ - nr_ranges =2 ; + nr_ranges =3 ; cmem =vzalloc (struct_size (cmem, ranges, nr_ranges));if (!cmem)return -ENOMEM ;
base-commit: b320789d6883cc00ac78ce83bccbfe7ed58afcf0
Signature · 1 line
2.51.0
PATCH v6 2/2 crash: Add KUnit tests for crash_exclude_mem_range
crash_exclude_mem_range seems to be a simple function but there have been multiple attempts to fix it,
- commit a2e9a95d2190 ("kexec: Improve & fix crash_exclude_mem_range() to handle overlapping ranges")
- commit 6dff31597264 ("crash_core: fix and simplify the logic of crash_exclude_mem_range()")
So add a set of unit tests to verify the correctness of current implementation. Shall we change the function in the future, the unit tests can also help prevent any regression. For example, we may make the function smarter by allocating extra crash_mem range on demand thus there is no need for the caller to foresee any memory range split or address -ENOMEM failure.
The testing strategy is to verify the correctness of base case. The base case is there is one to-be-excluded range A and one existing range B. Then we can exhaust all possibilities of the position of A regarding B. For example, here are two combinations,
Case: A is completely inside B (causes split)
Original: [----B----]
Exclude: {--A--}
Result: [B1] .. [B2]
Case: A overlaps B's left part
Original: [----B----]
Exclude: {---A---}
Result: [..B..]In theory we can prove the correctness by induction,
- Base case: crash_exclude_mem_range is correct in the case where n=1 (n is the number of existing ranges).
- Inductive step: If crash_exclude_mem_range is correct for n=k existing ranges, then the it's also correct for n=k+1 ranges.
But for the sake of simplicity, simply use unit tests to cover the base case together with two regression tests.
Note most of exclude_single_range_test code is generated by Google Gemini with some small tweaks. The function specification, function body and the exhausting test strategy are presented as prompts.
--- kernel/Kconfig.kexec | 11 ++ kernel/Makefile | 1 + kernel/crash_core.c | 14 ++ kernel/crash_core_test.c | 343 +++++++++++++++++++++++++++++++++++++++ 4 files changed, 369 insertions(+) create mode 100644 kernel/crash_core_test.c
kernel/Kconfig.kexec +11 -0
@@ -148,6 +148,17 @@ config CRASH_DM_CRYPT_CONFIGS CRASH_DM_CRYPT cannot directly select CONFIGFS_FS, because that is required to be built-in. +config CRASH_DUMP_KUNIT_TEST + tristate "Unit Tests for kernel crash dumps" if !KUNIT_ALL_TESTS + depends on CRASH_DUMP && KUNIT + default KUNIT_ALL_TESTS + help + This option builds KUnit unit tests for kernel crash dumps. The unit + tests will be used to verify the correctness of covered functions and + also prevent any regression. + + If unsure, say N. + config CRASH_HOTPLUG bool "Update the crash elfcorehdr on system configuration changes" default y
kernel/Makefile +1 -0
@@ -78,6 +78,7 @@ obj-$(CONFIG_CRASH_RESERVE) += crash_reserve.o obj-$(CONFIG_KEXEC_CORE) += kexec_core.o obj-$(CONFIG_CRASH_DUMP) += crash_core.o obj-$(CONFIG_CRASH_DM_CRYPT) += crash_dump_dm_crypt.o +obj-$(CONFIG_CRASH_DUMP_KUNIT_TEST) += crash_core_test.o obj-$(CONFIG_KEXEC) += kexec.o obj-$(CONFIG_KEXEC_FILE) += kexec_file.o obj-$(CONFIG_KEXEC_ELF) += kexec_elf.o
kernel/crash_core.c +14 -0
@@ -274,6 +274,20 @@ int crash_prepare_elf64_headers(struct crash_mem *mem, int need_kernel_map,return 0 ; } +/** +* crash_exclude_mem_range - exclude a mem range for existing ranges +* @mem: mem->range contains an array of ranges sorted in ascending order +* @mstart: the start of to-be-excluded range +* @mend: the start of to-be-excluded range +* +* If you are unsure if a range split will happen, to avoid function call +* failure because of -ENOMEM, always make sure +* mem->max_nr_ranges == mem->nr_ranges + 1 +* before calling the function each time. +* +* returns 0 if a memory range is excluded successfully +* return -ENOMEM if mem->ranges doesn't have space to hold split ranges +*/ int crash_exclude_mem_range (struct crash_mem *mem,unsigned long long mstart,unsigned long long mend) {
kernel/crash_core_test.c new file +343 -0
343 lines not on this page · raw message
Signature · 1 line
2.51.0
kernel test robot
build botRe: [PATCH v6 2/2] crash: Add KUnit tests for crash_exclude_mem_range
kernel test robot noticed the following build errors:
Hi Coiby,
kernel test robot noticed the following build errors:
[auto build test ERROR on b320789d6883cc00ac78ce83bccbfe7ed58afcf0]
url: https://github.com/intel-lab-lkp/linux/commits/Coiby-Xu/crash-Add-KUnit-tests-for-crash_exclude_mem_range/20250904-174105 base: b320789d6883cc00ac78ce83bccbfe7ed58afcf0 patch link: https://lore.kernel.org/r/20250904093855.1180154-2-coxu%40redhat.com patch subject: [PATCH v6 2/2] crash: Add KUnit tests for crash_exclude_mem_range config: x86_64-randconfig-073-20250908 (https://download.01.org/0day-ci/archive/20250909/[email protected]/config) compiler: clang version 20.1.8 (https://github.com/llvm/llvm-project 87f0227cb60147a26a1eeb4fb06e3b505e9c7261) reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20250909/[email protected]/reproduce)
If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/
All errors (new ones prefixed by >>, old ones prefixed by <<):
>> ERROR: modpost: "crash_exclude_mem_range" [kernel/crash_core_test.ko] undefined!
Signature · 2 lines
0-DAY CI Kernel Test Service https://github.com/intel/lkp-tests/wiki
Re: [PATCH v6 2/2] crash: Add KUnit tests for crash_exclude_mem_range
Quoted 1 line · kernel test robot
Hi Coiby,
Hi,
Quoted 13 lines
kernel test robot noticed the following build errors:
[auto build test ERROR on
b320789d6883cc00ac78ce83bccbfe7ed58afcf0]url: https://github.com/intel-lab-lkp/linux/commits/Coiby-Xu/crash-Add-KUnit-tests-for-crash_exclude_mem_range/20250904-174105 base: b320789d6883cc00ac78ce83bccbfe7ed58afcf0 patch link: https://lore.kernel.org/r/20250904093855.1180154-2-coxu%40redhat.com patch subject: [PATCH v6 2/2] crash: Add KUnit tests for crash_exclude_mem_range config: x86_64-randconfig-073-20250908 (https://download.01.org/0day-ci/archive/20250909/[email protected]/config) compiler: clang version 20.1.8 (https://github.com/llvm/llvm-project 87f0227cb60147a26a1eeb4fb06e3b505e9c7261) reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20250909/[email protected]/reproduce)
Quoted 8 lines
If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/All errors (new ones prefixed by >>, old ones prefixed by <<):
Quoted 1 line
Quoted 1 line
ERROR: modpost: "
crash_exclude_mem_range" [kernel/crash_core_test.ko] undefined!
Thanks for reporting this problem. This issue has already been fixed by Andrew [1][2] earlier.
This issue happens because the KUnit tests can be built and run as a kernel module.
[1] https://lore.kernel.org/mm-commits/[email protected]/
[2] https://lore.kernel.org/mm-commits/[email protected]/
Quoted 5 lines
Signature · 3 lines
-- 0-DAY CI Kernel Test Service https://github.com/intel/lkp-tests/wiki
Signature · 2 lines
Best regards, Coiby