On 6/8/2026 3:34 PM, Jinjie Ruan wrote:
> Use the newly introduced crash_prepare_headers() function to replace
> the existing prepare_elf_headers(), allocate cmem and exclude crash kernel
> memory in the crash core, which reduce code duplication.
> 
> Only the following two architecture functions need to be implemented:
> - arch_get_system_nr_ranges(). Call get_nr_ram_ranges_callback()
>   to pre-counts the max number of memory ranges.
> 
> - arch_crash_populate_cmem(). Use prepare_elf64_ram_headers_callback()
>   to collects the memory ranges and fills them into cmem.


The below AI reported TOCTOU bug is an existing artifact and completely
unrelated to the core CMA implementation introduced here. Therefore, we
will skip fixing it in this series to avoid unnecessary noise.

Link:
https://lore.kernel.org/all/[email protected]/

> 
> Cc: Paul Walmsley <[email protected]>
> Cc: Palmer Dabbelt <[email protected]>
> Cc: Albert Ou <[email protected]>
> Cc: Alexandre Ghiti <[email protected]>
> Cc: Guo Ren <[email protected]>
> Reviewed-by: Sourabh Jain <[email protected]>
> Acked-by: Baoquan He <[email protected]>
> Acked-by: Mike Rapoport (Microsoft) <[email protected]>
> Signed-off-by: Jinjie Ruan <[email protected]>
> ---
>  arch/riscv/kernel/machine_kexec_file.c | 47 +++++++-------------------
>  1 file changed, 12 insertions(+), 35 deletions(-)
> 
> diff --git a/arch/riscv/kernel/machine_kexec_file.c 
> b/arch/riscv/kernel/machine_kexec_file.c
> index 3f7766057cac..439cbc50dfa6 100644
> --- a/arch/riscv/kernel/machine_kexec_file.c
> +++ b/arch/riscv/kernel/machine_kexec_file.c
> @@ -44,6 +44,15 @@ static int get_nr_ram_ranges_callback(struct resource 
> *res, void *arg)
>       return 0;
>  }
>  
> +unsigned int arch_get_system_nr_ranges(void)
> +{
> +     unsigned int nr_ranges = 2; /* For exclusion of crashkernel region */
> +
> +     walk_system_ram_res(0, -1, &nr_ranges, get_nr_ram_ranges_callback);
> +
> +     return nr_ranges;
> +}
> +
>  static int prepare_elf64_ram_headers_callback(struct resource *res, void 
> *arg)
>  {
>       struct crash_mem *cmem = arg;
> @@ -55,41 +64,9 @@ static int prepare_elf64_ram_headers_callback(struct 
> resource *res, void *arg)
>       return 0;
>  }
>  
> -static int prepare_elf_headers(void **addr, unsigned long *sz)
> +int arch_crash_populate_cmem(struct crash_mem *cmem)
>  {
> -     struct crash_mem *cmem;
> -     unsigned int nr_ranges;
> -     int ret;
> -
> -     nr_ranges = 2; /* For exclusion of crashkernel region */
> -     walk_system_ram_res(0, -1, &nr_ranges, get_nr_ram_ranges_callback);
> -
> -     cmem = kmalloc_flex(*cmem, ranges, nr_ranges);
> -     if (!cmem)
> -             return -ENOMEM;
> -
> -     cmem->max_nr_ranges = nr_ranges;
> -     cmem->nr_ranges = 0;
> -     ret = walk_system_ram_res(0, -1, cmem, 
> prepare_elf64_ram_headers_callback);
> -     if (ret)
> -             goto out;
> -
> -     /* Exclude crashkernel region */
> -     ret = crash_exclude_mem_range(cmem, crashk_res.start, crashk_res.end);
> -     if (ret)
> -             goto out;
> -
> -     if (crashk_low_res.end) {
> -             ret = crash_exclude_mem_range(cmem, crashk_low_res.start, 
> crashk_low_res.end);
> -             if (ret)
> -                     goto out;
> -     }
> -
> -     ret = crash_prepare_elf64_headers(cmem, true, addr, sz);
> -
> -out:
> -     kfree(cmem);
> -     return ret;
> +     return walk_system_ram_res(0, -1, cmem, 
> prepare_elf64_ram_headers_callback);
>  }
>  
>  static char *setup_kdump_cmdline(struct kimage *image, char *cmdline,
> @@ -281,7 +258,7 @@ int load_extra_segments(struct kimage *image, unsigned 
> long kernel_start,
>       if (image->type == KEXEC_TYPE_CRASH) {
>               void *headers;
>               unsigned long headers_sz;
> -             ret = prepare_elf_headers(&headers, &headers_sz);
> +             ret = crash_prepare_headers(true, &headers, &headers_sz, NULL);
>               if (ret) {
>                       pr_err("Preparing elf core header failed\n");
>                       goto out;


Reply via email to