crash_load_segments() open codes the elf core header loading: it builds
the header, accounts for crash hotplug and places the segment.

Use kexec_prepare_elf_core_header() and kexec_add_elf_core_header()
instead.  The crash hotplug reserved size is still computed and recorded
here, as the VMCOREINFO, kernel map, CPU and memory range accounting is
x86 specific.

bzImage64_load() also loads the initrd by hand, use kexec_add_initrd()
for it.

Cc: Thomas Gleixner <[email protected]>
Cc: Ingo Molnar <[email protected]>
Cc: Borislav Petkov <[email protected]>
Cc: Dave Hansen <[email protected]>
Cc: H. Peter Anvin <[email protected]>
Cc: Andrew Morton <[email protected]>
Cc: Baoquan He <[email protected]>
Cc: Mike Rapoport <[email protected]>
Signed-off-by: Jinjie Ruan <[email protected]>
---
 arch/x86/kernel/crash.c           | 34 ++++++++++++++-----------------
 arch/x86/kernel/kexec-bzimage64.c |  8 +-------
 2 files changed, 16 insertions(+), 26 deletions(-)

diff --git a/arch/x86/kernel/crash.c b/arch/x86/kernel/crash.c
index e6f23933a6df..94c0dde6070a 100644
--- a/arch/x86/kernel/crash.c
+++ b/arch/x86/kernel/crash.c
@@ -348,20 +348,20 @@ int crash_setup_memmap_entries(struct kimage *image, 
struct boot_params *params)
 
 int crash_load_segments(struct kimage *image)
 {
-       int ret;
-       unsigned long pnum = 0;
        struct kexec_buf kbuf = { .image = image, .buf_min = 0,
                                  .buf_max = ULONG_MAX, .top_down = false };
+       unsigned long hotplug_sz;
+       unsigned long pnum = 0;
+       int ret;
 
        /* Prepare elf headers and add a segment */
-       ret = crash_prepare_headers(IS_ENABLED(CONFIG_X86_64), &kbuf.buffer,
-                                   &kbuf.bufsz, &pnum);
+       ret = kexec_prepare_elf_core_header(image, IS_ENABLED(CONFIG_X86_64),
+                                           &pnum);
        if (ret)
                return ret;
 
-       image->elf_headers      = kbuf.buffer;
-       image->elf_headers_sz   = kbuf.bufsz;
-       kbuf.memsz              = kbuf.bufsz;
+       hotplug_sz = image->elf_headers_sz;
+       kbuf.memsz = hotplug_sz;
 
 #ifdef CONFIG_CRASH_HOTPLUG
        /*
@@ -374,28 +374,24 @@ int crash_load_segments(struct kimage *image)
                pnum += 2 + CONFIG_NR_CPUS;
 
        if (pnum < (unsigned long)PN_XNUM) {
-               kbuf.memsz = pnum * sizeof(Elf64_Phdr);
-               kbuf.memsz += sizeof(Elf64_Ehdr);
-
+               hotplug_sz = pnum * sizeof(Elf64_Phdr) + sizeof(Elf64_Ehdr);
+               kbuf.memsz = hotplug_sz;
                image->elfcorehdr_index = image->nr_segments;
-
-               /* Mark as usable to crash kernel, else crash kernel fails on 
boot */
-               image->elf_headers_sz = kbuf.memsz;
        } else {
                pr_err("number of Phdrs %lu exceeds max\n", pnum);
        }
 #endif
 
        kbuf.buf_align = ELF_CORE_HEADER_ALIGN;
-       kbuf.mem = KEXEC_BUF_MEM_UNKNOWN;
-       ret = kexec_add_buffer(&kbuf);
+
+       ret = kexec_add_elf_core_header(image, &kbuf);
        if (ret)
                return ret;
-       image->elf_load_addr = kbuf.mem;
-       kexec_dprintk("Loaded ELF headers at 0x%lx bufsz=0x%lx memsz=0x%lx\n",
-                     image->elf_load_addr, kbuf.bufsz, kbuf.memsz);
 
-       return ret;
+       /* Mark as usable to crash kernel, else crash kernel fails on boot. */
+       image->elf_headers_sz = hotplug_sz;
+
+       return 0;
 }
 #endif /* CONFIG_KEXEC_FILE */
 
diff --git a/arch/x86/kernel/kexec-bzimage64.c 
b/arch/x86/kernel/kexec-bzimage64.c
index 3aa77006adab..c8e285c9a4c6 100644
--- a/arch/x86/kernel/kexec-bzimage64.c
+++ b/arch/x86/kernel/kexec-bzimage64.c
@@ -626,19 +626,13 @@ static void *bzImage64_load(struct kimage *image, char 
*kernel,
 
        /* Load initrd high */
        if (initrd) {
-               kbuf.buffer = initrd;
-               kbuf.bufsz = kbuf.memsz = initrd_len;
                kbuf.buf_align = PAGE_SIZE;
                kbuf.buf_min = MIN_INITRD_LOAD_ADDR;
-               kbuf.mem = KEXEC_BUF_MEM_UNKNOWN;
-               ret = kexec_add_buffer(&kbuf);
+               ret = kexec_add_initrd(image, &kbuf);
                if (ret)
                        goto out_free_params;
                initrd_load_addr = kbuf.mem;
 
-               kexec_dprintk("Loaded initrd at 0x%lx bufsz=0x%lx 
memsz=0x%lx\n",
-                             initrd_load_addr, initrd_len, initrd_len);
-
                setup_initrd(params, initrd_load_addr, initrd_len);
        }
 
-- 
2.34.1


Reply via email to