The elf core header buffer is generic state, but arm64, LoongArch,
RISC-V, x86 and powerpc each free it in their post-load cleanup.  Move
it into the generic cleanup, which also gives the loader a helper to
release it on error.

Cc: Catalin Marinas <[email protected]>
Cc: Will Deacon <[email protected]>
Cc: Huacai Chen <[email protected]>
Cc: Palmer Dabbelt <[email protected]>
Cc: Alexandre Ghiti <[email protected]>
Cc: Thomas Gleixner <[email protected]>
Cc: Ingo Molnar <[email protected]>
Cc: Borislav Petkov <[email protected]>
Cc: Dave Hansen <[email protected]>
Cc: Michael Ellerman <[email protected]>
Cc: Nicholas Piggin <[email protected]>
Cc: Christophe Leroy <[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/arm64/kernel/machine_kexec_file.c     | 4 ----
 arch/loongarch/kernel/machine_kexec_file.c | 4 ----
 arch/powerpc/kexec/file_load_64.c          | 4 ----
 arch/riscv/kernel/machine_kexec_file.c     | 4 ----
 arch/x86/kernel/machine_kexec_64.c         | 4 ----
 kernel/kexec_file.c                        | 9 +++++++++
 6 files changed, 9 insertions(+), 20 deletions(-)

diff --git a/arch/arm64/kernel/machine_kexec_file.c 
b/arch/arm64/kernel/machine_kexec_file.c
index d3d191a5ac89..e7f280915e8d 100644
--- a/arch/arm64/kernel/machine_kexec_file.c
+++ b/arch/arm64/kernel/machine_kexec_file.c
@@ -32,10 +32,6 @@ int arch_kimage_file_post_load_cleanup(struct kimage *image)
        kvfree(image->arch.dtb);
        image->arch.dtb = NULL;
 
-       vfree(image->elf_headers);
-       image->elf_headers = NULL;
-       image->elf_headers_sz = 0;
-
        return kexec_image_post_load_cleanup_default(image);
 }
 
diff --git a/arch/loongarch/kernel/machine_kexec_file.c 
b/arch/loongarch/kernel/machine_kexec_file.c
index 6ea60e0faa2c..c0a799f0d240 100644
--- a/arch/loongarch/kernel/machine_kexec_file.c
+++ b/arch/loongarch/kernel/machine_kexec_file.c
@@ -28,10 +28,6 @@ const struct kexec_file_ops * const kexec_file_loaders[] = {
 
 int arch_kimage_file_post_load_cleanup(struct kimage *image)
 {
-       vfree(image->elf_headers);
-       image->elf_headers = NULL;
-       image->elf_headers_sz = 0;
-
        return kexec_image_post_load_cleanup_default(image);
 }
 
diff --git a/arch/powerpc/kexec/file_load_64.c 
b/arch/powerpc/kexec/file_load_64.c
index bd80c5fb1b1f..0cf03cee35a7 100644
--- a/arch/powerpc/kexec/file_load_64.c
+++ b/arch/powerpc/kexec/file_load_64.c
@@ -841,10 +841,6 @@ int arch_kimage_file_post_load_cleanup(struct kimage 
*image)
        vfree(image->arch.backup_buf);
        image->arch.backup_buf = NULL;
 
-       vfree(image->elf_headers);
-       image->elf_headers = NULL;
-       image->elf_headers_sz = 0;
-
        kvfree(image->arch.fdt);
        image->arch.fdt = NULL;
 
diff --git a/arch/riscv/kernel/machine_kexec_file.c 
b/arch/riscv/kernel/machine_kexec_file.c
index ca921b063920..d191351c6055 100644
--- a/arch/riscv/kernel/machine_kexec_file.c
+++ b/arch/riscv/kernel/machine_kexec_file.c
@@ -29,10 +29,6 @@ int arch_kimage_file_post_load_cleanup(struct kimage *image)
        kvfree(image->arch.fdt);
        image->arch.fdt = NULL;
 
-       vfree(image->elf_headers);
-       image->elf_headers = NULL;
-       image->elf_headers_sz = 0;
-
        return kexec_image_post_load_cleanup_default(image);
 }
 
diff --git a/arch/x86/kernel/machine_kexec_64.c 
b/arch/x86/kernel/machine_kexec_64.c
index c3f4a389992d..0a66cc268ee7 100644
--- a/arch/x86/kernel/machine_kexec_64.c
+++ b/arch/x86/kernel/machine_kexec_64.c
@@ -600,10 +600,6 @@ int arch_kexec_apply_relocations_add(struct purgatory_info 
*pi,
 
 int arch_kimage_file_post_load_cleanup(struct kimage *image)
 {
-       vfree(image->elf_headers);
-       image->elf_headers = NULL;
-       image->elf_headers_sz = 0;
-
        return kexec_image_post_load_cleanup_default(image);
 }
 #endif /* CONFIG_KEXEC_FILE */
diff --git a/kernel/kexec_file.c b/kernel/kexec_file.c
index 59fb9d71e9d8..62d4cba278a5 100644
--- a/kernel/kexec_file.c
+++ b/kernel/kexec_file.c
@@ -100,6 +100,13 @@ int kexec_image_post_load_cleanup_default(struct kimage 
*image)
        return image->fops->cleanup(image->image_loader_data);
 }
 
+static void kexec_free_elf_headers(struct kimage *image)
+{
+       vfree(image->elf_headers);
+       image->elf_headers = NULL;
+       image->elf_headers_sz = 0;
+}
+
 /*
  * Free up memory used by kernel, initrd, and command line. This is temporary
  * memory allocation which is not needed any more after these buffers have
@@ -118,6 +125,8 @@ void kimage_file_post_load_cleanup(struct kimage *image)
        kfree(image->cmdline_buf);
        image->cmdline_buf = NULL;
 
+       kexec_free_elf_headers(image);
+
        vfree(pi->purgatory_buf);
        pi->purgatory_buf = NULL;
 
-- 
2.34.1


Reply via email to