The exclude of crashk_res, crashk_low_res and crashk_cma memory
are almost identical across different architectures, so handling them
in the crash core would eliminate a lot of duplication, so do
them in the common code.

Signed-off-by: Jinjie Ruan <[email protected]>
---
 arch/arm64/kernel/machine_kexec_file.c     | 12 -------
 arch/loongarch/kernel/machine_kexec_file.c | 12 -------
 arch/powerpc/kexec/ranges.c                | 16 ++-------
 arch/riscv/kernel/machine_kexec_file.c     |  5 +--
 arch/x86/kernel/crash.c                    | 39 ++--------------------
 kernel/crash_core.c                        | 28 ++++++++++++++++
 6 files changed, 34 insertions(+), 78 deletions(-)

diff --git a/arch/arm64/kernel/machine_kexec_file.c 
b/arch/arm64/kernel/machine_kexec_file.c
index 410060ebd86d..ed2c45007158 100644
--- a/arch/arm64/kernel/machine_kexec_file.c
+++ b/arch/arm64/kernel/machine_kexec_file.c
@@ -64,20 +64,8 @@ static int prepare_elf_headers(void **addr, unsigned long 
*sz)
                cmem->nr_ranges++;
        }
 
-       /* 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;
 }
diff --git a/arch/loongarch/kernel/machine_kexec_file.c 
b/arch/loongarch/kernel/machine_kexec_file.c
index fb57026f5f25..26f867e53955 100644
--- a/arch/loongarch/kernel/machine_kexec_file.c
+++ b/arch/loongarch/kernel/machine_kexec_file.c
@@ -80,20 +80,8 @@ static int prepare_elf_headers(void **addr, unsigned long 
*sz)
                cmem->nr_ranges++;
        }
 
-       /* Exclude crashkernel region */
-       ret = crash_exclude_mem_range(cmem, crashk_res.start, crashk_res.end);
-       if (ret < 0)
-               goto out;
-
-       if (crashk_low_res.end) {
-               ret = crash_exclude_mem_range(cmem, crashk_low_res.start, 
crashk_low_res.end);
-               if (ret < 0)
-                       goto out;
-       }
-
        ret = crash_prepare_elf64_headers(cmem, true, addr, sz);
 
-out:
        kfree(cmem);
        return ret;
 }
diff --git a/arch/powerpc/kexec/ranges.c b/arch/powerpc/kexec/ranges.c
index 867135560e5c..3f76dd266b1f 100644
--- a/arch/powerpc/kexec/ranges.c
+++ b/arch/powerpc/kexec/ranges.c
@@ -553,9 +553,7 @@ int get_usable_memory_ranges(struct crash_mem **mem_ranges)
 #endif /* CONFIG_KEXEC_FILE */
 
 #ifdef CONFIG_CRASH_DUMP
-static int crash_exclude_mem_range_guarded(struct crash_mem **mem_ranges,
-                                          unsigned long long mstart,
-                                          unsigned long long mend)
+static int crash_realloc_mem_range_guarded(struct crash_mem **mem_ranges)
 {
        struct crash_mem *tmem = *mem_ranges;
 
@@ -566,7 +564,7 @@ static int crash_exclude_mem_range_guarded(struct crash_mem 
**mem_ranges,
                        return -ENOMEM;
        }
 
-       return crash_exclude_mem_range(tmem, mstart, mend);
+       return 0;
 }
 
 /**
@@ -604,18 +602,10 @@ int get_crash_memory_ranges(struct crash_mem **mem_ranges)
                        sort_memory_ranges(*mem_ranges, true);
        }
 
-       /* Exclude crashkernel region */
-       ret = crash_exclude_mem_range_guarded(mem_ranges, crashk_res.start, 
crashk_res.end);
+       ret = crash_realloc_mem_range_guarded(mem_ranges);
        if (ret)
                goto out;
 
-       for (i = 0; i < crashk_cma_cnt; ++i) {
-               ret = crash_exclude_mem_range_guarded(mem_ranges, 
crashk_cma_ranges[i].start,
-                                             crashk_cma_ranges[i].end);
-               if (ret)
-                       goto out;
-       }
-
        /*
         * FIXME: For now, stay in parity with kexec-tools but if RTAS/OPAL
         *        regions are exported to save their context at the time of
diff --git a/arch/riscv/kernel/machine_kexec_file.c 
b/arch/riscv/kernel/machine_kexec_file.c
index dd9d92a96517..fec3622a13c9 100644
--- a/arch/riscv/kernel/machine_kexec_file.c
+++ b/arch/riscv/kernel/machine_kexec_file.c
@@ -74,10 +74,7 @@ static int prepare_elf_headers(void **addr, unsigned long 
*sz)
        if (ret)
                goto out;
 
-       /* Exclude crashkernel region */
-       ret = crash_exclude_mem_range(cmem, crashk_res.start, crashk_res.end);
-       if (!ret)
-               ret = crash_prepare_elf64_headers(cmem, true, addr, sz);
+       ret = crash_prepare_elf64_headers(cmem, true, addr, sz);
 
 out:
        kfree(cmem);
diff --git a/arch/x86/kernel/crash.c b/arch/x86/kernel/crash.c
index 335fd2ee9766..d8341a48f6b3 100644
--- a/arch/x86/kernel/crash.c
+++ b/arch/x86/kernel/crash.c
@@ -186,41 +186,6 @@ static struct crash_mem *fill_up_crash_elf_data(void)
        return cmem;
 }
 
-/*
- * Look for any unwanted ranges between mstart, mend and remove them. This
- * might lead to split and split ranges are put in cmem->ranges[] array
- */
-static int elf_header_exclude_ranges(struct crash_mem *cmem)
-{
-       int ret = 0;
-       int i;
-
-       /* Exclude the low 1M because it is always reserved */
-       ret = crash_exclude_mem_range(cmem, 0, SZ_1M - 1);
-       if (ret)
-               return ret;
-
-       /* Exclude crashkernel region */
-       ret = crash_exclude_mem_range(cmem, crashk_res.start, crashk_res.end);
-       if (ret)
-               return ret;
-
-       if (crashk_low_res.end)
-               ret = crash_exclude_mem_range(cmem, crashk_low_res.start,
-                                             crashk_low_res.end);
-       if (ret)
-               return ret;
-
-       for (i = 0; i < crashk_cma_cnt; ++i) {
-               ret = crash_exclude_mem_range(cmem, crashk_cma_ranges[i].start,
-                                             crashk_cma_ranges[i].end);
-               if (ret)
-                       return ret;
-       }
-
-       return 0;
-}
-
 static int prepare_elf64_ram_headers_callback(struct resource *res, void *arg)
 {
        struct crash_mem *cmem = arg;
@@ -247,8 +212,8 @@ static int prepare_elf_headers(void **addr, unsigned long 
*sz,
        if (ret)
                goto out;
 
-       /* Exclude unwanted mem ranges */
-       ret = elf_header_exclude_ranges(cmem);
+       /* Exclude the low 1M because it is always reserved */
+       ret = crash_exclude_mem_range(cmem, 0, SZ_1M - 1);
        if (ret)
                goto out;
 
diff --git a/kernel/crash_core.c b/kernel/crash_core.c
index 99dac1aa972a..5c0de111ddc3 100644
--- a/kernel/crash_core.c
+++ b/kernel/crash_core.c
@@ -18,6 +18,7 @@
 #include <linux/memblock.h>
 #include <linux/kmemleak.h>
 #include <linux/crash_core.h>
+#include <linux/crash_reserve.h>
 #include <linux/reboot.h>
 #include <linux/btf.h>
 #include <linux/objtool.h>
@@ -161,8 +162,30 @@ static inline resource_size_t crash_resource_size(const 
struct resource *res)
        return !res->end ? 0 : resource_size(res);
 }
 
+static int crash_exclude_mem_ranges(struct crash_mem *cmem)
+{
+       int ret, i;
+
+       /* Exclude crashkernel region */
+       ret = crash_exclude_mem_range(cmem, crashk_res.start, crashk_res.end);
+       if (ret)
+               return ret;
+
+       if (crashk_low_res.end) {
+               ret = crash_exclude_mem_range(cmem, crashk_low_res.start, 
crashk_low_res.end);
+               if (ret)
+                       return ret;
+       }
 
+       for (i = 0; i < crashk_cma_cnt; ++i) {
+               ret = crash_exclude_mem_range(cmem, crashk_cma_ranges[i].start,
+                                             crashk_cma_ranges[i].end);
+               if (ret)
+                       return ret;
+       }
 
+       return ret;
+}
 
 int crash_prepare_elf64_headers(struct crash_mem *mem, int need_kernel_map,
                          void **addr, unsigned long *sz)
@@ -174,6 +197,11 @@ int crash_prepare_elf64_headers(struct crash_mem *mem, int 
need_kernel_map,
        unsigned int cpu, i;
        unsigned long long notes_addr;
        unsigned long mstart, mend;
+       int ret;
+
+       ret = crash_exclude_mem_ranges(mem);
+       if (ret)
+               return ret;
 
        /* extra phdr for vmcoreinfo ELF note */
        nr_phdr = nr_cpus + 1;
-- 
2.34.1


Reply via email to