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.

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 | 51 ++++++--------------------
 1 file changed, 12 insertions(+), 39 deletions(-)

diff --git a/arch/riscv/kernel/machine_kexec_file.c 
b/arch/riscv/kernel/machine_kexec_file.c
index a88362dfbd89..d2bb61a8b6b6 100644
--- a/arch/riscv/kernel/machine_kexec_file.c
+++ b/arch/riscv/kernel/machine_kexec_file.c
@@ -8,7 +8,6 @@
  */
 #include <linux/kexec.h>
 #include <linux/elf.h>
-#include <linux/memory_hotplug.h>
 #include <linux/slab.h>
 #include <linux/of.h>
 #include <linux/libfdt.h>
@@ -45,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;
@@ -59,41 +67,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,
@@ -285,15 +261,12 @@ int load_extra_segments(struct kimage *image, unsigned 
long kernel_start,
        if (image->type == KEXEC_TYPE_CRASH) {
                void *headers;
                unsigned long headers_sz;
-               get_online_mems();
-               ret = prepare_elf_headers(&headers, &headers_sz);
+               ret = crash_prepare_headers_locked(true, &headers, &headers_sz, 
NULL);
                if (ret) {
-                       put_online_mems();
                        pr_err("Preparing elf core header failed\n");
                        goto out;
                }
 
-               put_online_mems();
                kbuf.buffer = headers;
                kbuf.bufsz = headers_sz;
                kbuf.mem = KEXEC_BUF_MEM_UNKNOWN;
-- 
2.34.1


Reply via email to