Skip to contentSkip to conversation

kexec: Rework segment loading and fix CMA retries

PATCH New Jinjie Ruan 2 participants 13 messages Nov 27

Subsystems (9)
x86 PowerPC RISC-V arm64 +5 more subsystems
lore
Share & tools
lore
#0 lore raw

v9 cover kexec: Rework segment loading and fix CMA retries

ARM64, LoongArch, RISC-V, x86 and Powerpc build the elf core header and place it together with the initrd and the DTB with almost identical code.

This series reworks that path into common helpers that the architectures reuse, and fixes the CMA page and elf core header leaks in the arm64 and LoongArch placement retry loops.

The patch organization is as follows:

- Patch 1 moves the release of the elf core header into the generic post-load cleanup, so that the architectures no longer free it themselves.

- Patch 2 adds the common helpers for reuse.

- Patches 3-7 convert arm64, LoongArch, RISC-V, X86 and Powerpc to reuse the common helpers

- Patches 8-11 fix the CMA pages leaked by the retry loops and the loop that makes no progress

Tested with kexec_file_load() on arm64, LoongArch, RISC-V, X86 and Powerpc under QEMU, including the CMA retry paths, loading a crash kernel and booting the second kernel, with the agent help.

This series is also available at:
https://github.com/ruanjinjie-eng/linux/tree/kexec_rework_cma_bugfx_v9

Changes in v9:
- Put kexec_add_fdt() in drivers/of/kexec.c. (Mike)
- Keep the elf core header reservation of crash hotplug in x86. (Mike)
- Rename kexec_add_fdt() to of_kexec_add_fdt().
- Drop the memsz comparison in kexec_add_elf_core_header so the architectures that reserve room for crash hotplug now record the size themselves, which fix the Sashiko issue related to crash hotplug [1].
- Update the commit message.
- https://lore.kernel.org/all/[email protected]/

[1]: https://sashiko.dev/#/patchset/20261112133418.4044428-1-ruanjinjie%40huawei.com

Changes in v8:
- Rework the common code as Mike suggested: the core provides building blocks and the architectures uses them (Mike)
- Build the elf core header once and only place it on the retries (Mike)
- Keep the segment order and the arch placement policy unchanged (Mike)
- Convert x86 and powerpc as well
- Move the CMA fixes to the end of the series (Mike)
- Link to v7: https://lore.kernel.org/all/[email protected]/

Previous Versions are as below:
- v6: https://lore.kernel.org/all/[email protected]/
- v5: https://lore.kernel.org/all/[email protected]/
- v4: https://lore.kernel.org/all/[email protected]/
- v3: https://lore.kernel.org/all/[email protected]/
- v2: https://lore.kernel.org/all/[email protected]/
- v1: https://lore.kernel.org/all/[email protected]/#t

Jinjie Ruan (11):
kexec: Free the elf core header in kimage_file_post_load_cleanup()
kexec: Add helpers to load the elf core header, initrd and FDT
arm64: kexec_file: Load the other segments with common helpers
LoongArch: kexec_file: Load the other segments with common helpers
riscv: kexec_file: Load the extra segments with common helpers
x86: kexec_file: Use the common segment helpers
powerpc: kexec_file: Use the common segment helpers
kexec: Extract kexec_free_segment_cma() from kimage_free_cma()
kexec: Add kexec_rollback_segments() helper
arm64: kexec_file: Fix CMA segment placement retries
LoongArch: kexec_file: Fix CMA segment placement retries

 arch/arm64/include/asm/kexec.h             |  4 +-
 arch/arm64/kernel/kexec_image.c            | 15 ++--
 arch/arm64/kernel/machine_kexec_file.c     | 87 +++++-----------------
 arch/loongarch/include/asm/kexec.h         |  3 +-
 arch/loongarch/kernel/kexec_efi.c          | 15 ++--
 arch/loongarch/kernel/kexec_elf.c          |  3 +-
 arch/loongarch/kernel/machine_kexec_file.c | 77 ++++++-------------
 arch/powerpc/kexec/elf_64.c                |  9 +--
 arch/powerpc/kexec/file_load_64.c          | 18 +----
 arch/riscv/include/asm/kexec.h             |  4 +-
 arch/riscv/kernel/kexec_elf.c              |  3 +-
 arch/riscv/kernel/kexec_image.c            |  3 +-
 arch/riscv/kernel/machine_kexec_file.c     | 83 ++++++---------------
 arch/x86/kernel/crash.c                    | 34 ++++-----
 arch/x86/kernel/kexec-bzimage64.c          |  8 +-
 arch/x86/kernel/machine_kexec_64.c         |  4 -
 drivers/of/kexec.c                         | 35 +++++++++
 include/linux/kexec.h                      |  9 +++
 include/linux/of.h                         |  4 +
 kernel/kexec_core.c                        | 25 ++++---
 kernel/kexec_file.c                        | 81 ++++++++++++++++++++
 21 files changed, 251 insertions(+), 273 deletions(-)
Signature · 1 line
2.34.1
#1 lore raw

PATCH v9 1/11 kexec: Free the elf core header in kimage_file_post_load_cleanup()

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]>
---
 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(-)
arch/arm64/kernel/machine_kexec_file.c +0 -4
index 14f453dd0946..097ee397e949 100644
@@ -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);
 }
 
arch/loongarch/kernel/machine_kexec_file.c +0 -4
index 0fe60907b97f..f16d9ab14726 100644
@@ -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);
 }
 
arch/powerpc/kexec/file_load_64.c +0 -4
index bd80c5fb1b1f..0cf03cee35a7 100644
@@ -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;
 
arch/riscv/kernel/machine_kexec_file.c +0 -4
index 26cd2a8bd0cd..1d67fb42617a 100644
@@ -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);
 }
 
arch/x86/kernel/machine_kexec_64.c +0 -4
index c3f4a389992d..0a66cc268ee7 100644
@@ -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 */
kernel/kexec_file.c +9 -0
index b668ce8d68ee..1f128c6e6fa3 100644
@@ -102,6 +102,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
@@ -120,6 +127,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;
 
Signature · 1 line
2.34.1
#2 lore raw

PATCH v9 2/11 kexec: Add helpers to load the elf core header, initrd and FDT

In reply to Jinjie Ruan

arm64, LoongArch and RISC-V load the kdump kernel's elf core header and initrd by hand, and arm64 and RISC-V also build the device tree for the kexec kernel and place it as a segment. Add helpers for them so that every architecture keeps its own placement policy, which the caller puts into the kexec_buf:

- kexec_prepare_elf_core_header() builds the header once per image and returns the number of memory ranges, which x86 needs to size its crash hotplug reservation.

- kexec_add_elf_core_header() places the prepared header as a segment. The caller provides the kexec_buf with its placement policy and the size to reserve: x86 and powerpc put their crash hotplug reservation there, the other architectures the header size.

- kexec_add_initrd() places the initrd from the image.

- of_kexec_add_fdt() builds the device tree for the kexec kernel, packs it and places it.

Cc Andrew Morton <[email protected]>
Cc Baoquan He <[email protected]>
Cc Mike Rapoport <[email protected]>
---
 drivers/of/kexec.c    | 35 +++++++++++++++++++++++++
 include/linux/kexec.h |  6 +++++
 include/linux/of.h    |  4 +++
 kernel/kexec_file.c   | 60 +++++++++++++++++++++++++++++++++++++++++++
 4 files changed, 105 insertions(+)
drivers/of/kexec.c +35 -0
index bb4e0d9bfd38..a6f07c33fc40 100644
@@ -533,3 +533,38 @@ void *of_kexec_alloc_and_setup_fdt(const struct kimage *image,
 
 	return fdt;
 }
+
+int of_kexec_add_fdt(struct kimage *image, struct kexec_buf *kbuf,
+		     unsigned long initrd_load_addr, const char *cmdline,
+		     void **fdt)
+{
+	void *new_fdt;
+	int ret;
+
+	new_fdt = of_kexec_alloc_and_setup_fdt(image, initrd_load_addr,
+					       image->initrd_buf_len,
+					       cmdline, 0);
+	if (!new_fdt) {
+		pr_err("Preparing for new dtb failed\n");
+		return -EINVAL;
+	}
+
+	fdt_pack(new_fdt);
+
+	kbuf->buffer = new_fdt;
+	kbuf->bufsz = kbuf->memsz = fdt_totalsize(new_fdt);
+	kbuf->mem = KEXEC_BUF_MEM_UNKNOWN;
+
+	ret = kexec_add_buffer(kbuf);
+	if (ret) {
+		kvfree(new_fdt);
+		return ret;
+	}
+
+	*fdt = new_fdt;
+
+	kexec_dprintk("Loaded dtb at 0x%lx bufsz=0x%lx memsz=0x%lx\n",
+		      kbuf->mem, kbuf->bufsz, kbuf->memsz);
+
+	return 0;
+}
include/linux/kexec.h +6 -0
index bac11e940e74..491046a90ffa 100644
@@ -258,6 +258,12 @@ int kexec_kernel_verify_pe_sig(const char *kernel, unsigned long kernel_len);
 
 extern int kexec_add_buffer(struct kexec_buf *kbuf);
 int kexec_locate_mem_hole(struct kexec_buf *kbuf);
+int kexec_add_initrd(struct kimage *image, struct kexec_buf *kbuf);
+#ifdef CONFIG_CRASH_DUMP
+int kexec_prepare_elf_core_header(struct kimage *image, bool need_kernel_map,
+				  unsigned long *nr_ranges);
+int kexec_add_elf_core_header(struct kimage *image, struct kexec_buf *kbuf);
+#endif
 
 #ifndef arch_kexec_apply_relocations_add
 /*
include/linux/of.h +4 -0
index 44d83133a65a..5934097e7a21 100644
@@ -480,10 +480,14 @@ int of_map_msi_id(const struct device_node *np, u32 id,
 phys_addr_t of_dma_get_max_cpu_address(struct device_node *np);
 
 struct kimage;
+struct kexec_buf;
 void *of_kexec_alloc_and_setup_fdt(const struct kimage *image,
 				   unsigned long initrd_load_addr,
 				   unsigned long initrd_len,
 				   const char *cmdline, size_t extra_fdt_size);
+int of_kexec_add_fdt(struct kimage *image, struct kexec_buf *kbuf,
+		     unsigned long initrd_load_addr, const char *cmdline,
+		     void **fdt);
 #else /* CONFIG_OF */
 
 static inline void of_core_init(void)
kernel/kexec_file.c +60 -0

66 lines not on this page · raw message

Signature · 1 line
2.34.1
#3 lore raw

PATCH v9 3/11 arm64: kexec_file: Load the other segments with common helpers

In reply to Jinjie Ruan

Use the common segment helpers to load the elf core header, the initrd and the DTB instead of open coding them. The initrd and the command line come from the image.

Cc Catalin Marinas <[email protected]>
Cc Will Deacon <[email protected]>
Cc Andrew Morton <[email protected]>
Cc Baoquan He <[email protected]>
Cc Mike Rapoport <[email protected]>
---
 arch/arm64/include/asm/kexec.h         |  4 +-
 arch/arm64/kernel/kexec_image.c        |  3 +-
 arch/arm64/kernel/machine_kexec_file.c | 81 ++++++--------------------
 3 files changed, 20 insertions(+), 68 deletions(-)
arch/arm64/include/asm/kexec.h +1 -3
index 0be042ec22c1..f844fb5fe3ef 100644
@@ -127,9 +127,7 @@ int arch_kimage_file_post_load_cleanup(struct kimage *image);
 #define arch_kimage_file_post_load_cleanup arch_kimage_file_post_load_cleanup
 
 extern int load_other_segments(struct kimage *image,
-		unsigned long kernel_load_addr, unsigned long kernel_size,
-		char *initrd, unsigned long initrd_len,
-		char *cmdline);
+		unsigned long kernel_load_addr, unsigned long kernel_size);
 #endif
 
 #endif /* __ASSEMBLER__ */
arch/arm64/kernel/kexec_image.c +1 -2
index f4ad22447259..a2a7c283b331 100644
@@ -104,8 +104,7 @@ static void *image_load(struct kimage *image,
 		/* Try to load additional data */
 		kernel_segment = &image->segment[kernel_segment_number];
 		ret = load_other_segments(image, kernel_segment->mem,
-					  kernel_segment->memsz, initrd,
-					  initrd_len, cmdline);
+					  kernel_segment->memsz);
 		if (!ret)
 			break;
 
arch/arm64/kernel/machine_kexec_file.c +18 -63

128 lines not on this page · raw message

Signature · 1 line
2.34.1
#4 lore raw

PATCH v9 4/11 LoongArch: kexec_file: Load the other segments with common helpers

In reply to Jinjie Ruan

Use the common segment helpers to load the elf core header and the initrd instead of open coding them. The initrd and the command line come from the image.

Cc Huacai Chen <[email protected]>
Cc Andrew Morton <[email protected]>
Cc Baoquan He <[email protected]>
Cc Mike Rapoport <[email protected]>
---
 arch/loongarch/include/asm/kexec.h         |  3 +-
 arch/loongarch/kernel/kexec_efi.c          |  3 +-
 arch/loongarch/kernel/kexec_elf.c          |  3 +-
 arch/loongarch/kernel/machine_kexec_file.c | 71 +++++++---------------
 4 files changed, 25 insertions(+), 55 deletions(-)
arch/loongarch/include/asm/kexec.h +1 -2
index 6be136e9f0a0..b000e97c95f6 100644
@@ -51,8 +51,7 @@ int arch_kimage_file_post_load_cleanup(struct kimage *image);
 #define arch_kimage_file_post_load_cleanup arch_kimage_file_post_load_cleanup
 
 extern int load_other_segments(struct kimage *image,
-		unsigned long kernel_load_addr, unsigned long kernel_size,
-		char *initrd, unsigned long initrd_len, char *cmdline, unsigned long cmdline_len);
+		unsigned long kernel_load_addr, unsigned long kernel_size);
 #endif
 
 typedef void (*do_kexec_t)(unsigned long efi_boot,
arch/loongarch/kernel/kexec_efi.c +1 -2
index 28a1d0420ba3..4824c47907ac 100644
@@ -77,8 +77,7 @@ static void *efi_kexec_load(struct kimage *image,
 		/* Try to load additional data */
 		kernel_segment = &image->segment[kernel_segment_number];
 		ret = load_other_segments(image, kernel_segment->mem,
-					  kernel_segment->memsz, initrd,
-					  initrd_len, cmdline, cmdline_len);
+					  kernel_segment->memsz);
 		if (!ret)
 			break;
 
arch/loongarch/kernel/kexec_elf.c +1 -2
index 1b6b64744c7f..6fda49de330f 100644
@@ -83,8 +83,7 @@ static void *elf_kexec_load(struct kimage *image,
 
 	/* Load additional data */
 	kernel_segment = &image->segment[kernel_segment_number];
-	ret = load_other_segments(image, kernel_segment->mem, kernel_segment->memsz,
-				  initrd, initrd_len, cmdline, cmdline_len);
+	ret = load_other_segments(image, kernel_segment->mem, kernel_segment->memsz);
 	if (ret < 0)
 		goto out;
 
arch/loongarch/kernel/machine_kexec_file.c +22 -49

120 lines not on this page · raw message

Signature · 1 line
2.34.1
#5 lore raw

PATCH v9 5/11 riscv: kexec_file: Load the extra segments with common helpers

In reply to Jinjie Ruan

Use the common segment helpers to load the elf core header, the initrd and the DTB instead of open coding them. The initrd and the command line come from the image.

Cc Palmer Dabbelt <[email protected]>
Cc Alexandre Ghiti <[email protected]>
Cc Andrew Morton <[email protected]>
Cc Baoquan He <[email protected]>
Cc Mike Rapoport <[email protected]>
---
 arch/riscv/include/asm/kexec.h         |  4 +-
 arch/riscv/kernel/kexec_elf.c          |  3 +-
 arch/riscv/kernel/kexec_image.c        |  3 +-
 arch/riscv/kernel/machine_kexec_file.c | 79 +++++++-------------------
 4 files changed, 24 insertions(+), 65 deletions(-)
arch/riscv/include/asm/kexec.h +1 -3
index e218835eb10c..34da4d0e2cdc 100644
@@ -70,9 +70,7 @@ int arch_kimage_file_post_load_cleanup(struct kimage *image);
 #define arch_kimage_file_post_load_cleanup arch_kimage_file_post_load_cleanup
 
 int load_extra_segments(struct kimage *image, unsigned long kernel_start,
-			unsigned long kernel_len, char *initrd,
-			unsigned long initrd_len, char *cmdline,
-			unsigned long cmdline_len);
+			unsigned long kernel_len);
 #endif
 
 #ifdef CONFIG_CRASH_HOTPLUG
arch/riscv/kernel/kexec_elf.c +1 -2
index ca5abbad59e5..2071a22b846c 100644
@@ -126,8 +126,7 @@ static void *elf_kexec_load(struct kimage *image, char *kernel_buf,
 	if (ret)
 		goto out;
 
-	ret = load_extra_segments(image, image->start, kernel_len,
-				  initrd, initrd_len, cmdline, cmdline_len);
+	ret = load_extra_segments(image, image->start, kernel_len);
 out:
 	kexec_free_elf_info(&elf_info);
 	return ret ? ERR_PTR(ret) : NULL;
arch/riscv/kernel/kexec_image.c +1 -2
index 963a25f5b55b..010a84085cf8 100644
@@ -83,8 +83,7 @@ static void *image_load(struct kimage *image,
 	pr_info("Loaded kernel at 0x%lx bufsz=0x%lx memsz=0x%lx\n",
 		kbuf.mem, kbuf.bufsz, kbuf.memsz);
 
-	ret = load_extra_segments(image, kbuf.mem, kbuf.memsz,
-				  initrd, initrd_len, cmdline, cmdline_len);
+	ret = load_extra_segments(image, kbuf.mem, kbuf.memsz);
 
 out:
 	return ret ? ERR_PTR(ret) : NULL;
arch/riscv/kernel/machine_kexec_file.c +21 -58

120 lines not on this page · raw message

Signature · 1 line
2.34.1
#6 lore raw

PATCH v9 6/11 x86: kexec_file: Use the common segment helpers

In reply to Jinjie Ruan

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]>
---
 arch/x86/kernel/crash.c           | 34 ++++++++++++++-----------------
 arch/x86/kernel/kexec-bzimage64.c |  8 +-------
 2 files changed, 16 insertions(+), 26 deletions(-)
arch/x86/kernel/crash.c +15 -19
index e6f23933a6df..94c0dde6070a 100644
@@ -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 */
 
arch/x86/kernel/kexec-bzimage64.c +1 -7
index 3aa77006adab..c8e285c9a4c6 100644
@@ -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);
 	}
 
Signature · 1 line
2.34.1
#7 lore raw

PATCH v9 7/11 powerpc: kexec_file: Use the common segment helpers

In reply to Jinjie Ruan

load_elfcorehdr_segment() builds the elf core header from the powerpc crash memory ranges and places it as a segment. The header needs powerpc specific post processing, so keep building it here, but use kexec_add_elf_core_header() to place it.

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

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]>
---
 arch/powerpc/kexec/elf_64.c       |  9 ++-------
 arch/powerpc/kexec/file_load_64.c | 14 ++++----------
 2 files changed, 6 insertions(+), 17 deletions(-)
arch/powerpc/kexec/elf_64.c +2 -7
index d9a466cd602e..1ef444494641 100644
@@ -96,18 +96,13 @@ static void *elf64_load(struct kimage *image, char *kernel_buf,
 
 	kexec_dprintk("Command line: %s", cmdline ? cmdline : "");
 
-	if (initrd != NULL) {
-		kbuf.buffer = initrd;
-		kbuf.bufsz = kbuf.memsz = initrd_len;
+	if (initrd) {
 		kbuf.buf_align = PAGE_SIZE;
 		kbuf.top_down = false;
-		kbuf.mem = KEXEC_BUF_MEM_UNKNOWN;
-		ret = kexec_add_buffer(&kbuf);
+		ret = kexec_add_initrd(image, &kbuf);
 		if (ret)
 			goto out;
 		initrd_load_addr = kbuf.mem;
-
-		kexec_dprintk("Loaded initrd at 0x%lx\n", initrd_load_addr);
 	}
 
 	ret = get_reserved_memory_ranges(&rmem);
arch/powerpc/kexec/file_load_64.c +4 -10
index 0cf03cee35a7..77652db7d1ca 100644
@@ -416,9 +416,8 @@ static int load_elfcorehdr_segment(struct kimage *image, struct kexec_buf *kbuf)
 	/* Fix the offset for backup region in the ELF header */
 	sync_backup_region_phdr(image, headers, false);
 
-	kbuf->buffer = headers;
-	kbuf->mem = KEXEC_BUF_MEM_UNKNOWN;
-	kbuf->bufsz = headers_sz;
+	image->elf_headers = headers;
+	image->elf_headers_sz = headers_sz;
 
 	/*
 	 * Account for extra space required to accommodate additional memory
@@ -427,13 +426,9 @@ static int load_elfcorehdr_segment(struct kimage *image, struct kexec_buf *kbuf)
 	kbuf->memsz = headers_sz + kdump_extra_elfcorehdr_size(cmem);
 	kbuf->top_down = false;
 
-	ret = kexec_add_buffer(kbuf);
-	if (ret) {
-		vfree(headers);
+	ret = kexec_add_elf_core_header(image, kbuf);
+	if (ret)
 		goto out;
-	}
-
-	image->elf_load_addr = kbuf->mem;
 
 	/*
 	 * If CONFIG_CRASH_HOTPLUG is enabled, the elfcorehdr kexec segment
@@ -442,7 +437,6 @@ static int load_elfcorehdr_segment(struct kimage *image, struct kexec_buf *kbuf)
 	 * memory in the FDT prepared for kdump.
 	 */
 	image->elf_headers_sz = kbuf->memsz;
-	image->elf_headers = headers;
 out:
 	kfree(cmem);
 	return ret;
Signature · 1 line
2.34.1
#8 lore raw

PATCH v9 8/11 kexec: Extract kexec_free_segment_cma() from kimage_free_cma()

In reply to Jinjie Ruan

Arch loaders truncate nr_segments when a placement fails, so the CMA pages of the dropped segments become unreachable. Export a per-segment helper so they can be released first.

No functional change intended.

Cc Andrew Morton <[email protected]>
Cc Baoquan He <[email protected]>
Cc Mike Rapoport <[email protected]>
Cc Pasha Tatashin <[email protected]>
Cc Pratyush Yadav <[email protected]>
Cc Breno Leitao <[email protected]>
---
 include/linux/kexec.h |  2 ++
 kernel/kexec_core.c   | 25 +++++++++++++------------
 2 files changed, 15 insertions(+), 12 deletions(-)
include/linux/kexec.h +2 -0
index 491046a90ffa..3c030e478adf 100644
@@ -524,6 +524,7 @@ extern bool kexec_file_dbg_print;
 
 extern void *kimage_map_segment(struct kimage *image, int idx);
 extern void kimage_unmap_segment(void *buffer);
+void kexec_free_segment_cma(struct kimage *image, unsigned long idx);
 #else /* !CONFIG_KEXEC_CORE */
 struct pt_regs;
 struct task_struct;
@@ -535,6 +536,7 @@ static inline int kexec_crash_loaded(void) { return 0; }
 static inline void *kimage_map_segment(struct kimage *image, int idx)
 { return NULL; }
 static inline void kimage_unmap_segment(void *buffer) { }
+static inline void kexec_free_segment_cma(struct kimage *image, unsigned long idx) { }
 #define kexec_in_progress false
 static inline bool kexec_is_in_progress(void) { return false; }
 #endif /* CONFIG_KEXEC_CORE */
kernel/kexec_core.c +13 -12
index f4a31565c447..3ae09baee9c7 100644
@@ -571,22 +571,23 @@ static void kimage_free_entry(kimage_entry_t entry)
 	kimage_free_pages(page);
 }
 
-static void kimage_free_cma(struct kimage *image)
+void kexec_free_segment_cma(struct kimage *image, unsigned long idx)
 {
-	unsigned long i;
-
-	for (i = 0; i < image->nr_segments; i++) {
-		struct page *cma = image->segment_cma[i];
-		u32 nr_pages = image->segment[i].memsz >> PAGE_SHIFT;
+	u32 nr_pages = image->segment[idx].memsz >> PAGE_SHIFT;
+	struct page *cma = image->segment_cma[idx];
 
-		if (!cma)
-			continue;
+	if (!cma)
+		return;
 
-		arch_kexec_pre_free_pages(page_address(cma), nr_pages);
-		dma_release_from_contiguous(NULL, cma, nr_pages);
-		image->segment_cma[i] = NULL;
-	}
+	arch_kexec_pre_free_pages(page_address(cma), nr_pages);
+	dma_release_from_contiguous(NULL, cma, nr_pages);
+	image->segment_cma[idx] = NULL;
+}
 
+static void kimage_free_cma(struct kimage *image)
+{
+	for (unsigned long i = 0; i < image->nr_segments; i++)
+		kexec_free_segment_cma(image, i);
 }
 
 void kimage_free(struct kimage *image)
Signature · 1 line
2.34.1
#9 lore raw

PATCH v9 9/11 kexec: Add kexec_rollback_segments() helper

In reply to Jinjie Ruan

A load attempt that cannot place all of its segments drops the segments it added, but the CMA pages backing them become unreachable.

Add a helper that releases those pages and truncates nr_segments, so the loaders do not have to open code it.

Cc Andrew Morton <[email protected]>
Cc Baoquan He <[email protected]>
Cc Mike Rapoport <[email protected]>
---
 include/linux/kexec.h |  1 +
 kernel/kexec_file.c   | 12 ++++++++++++
 2 files changed, 13 insertions(+)
include/linux/kexec.h +1 -0
index 3c030e478adf..bfcc0a362fc9 100644
@@ -259,6 +259,7 @@ int kexec_kernel_verify_pe_sig(const char *kernel, unsigned long kernel_len);
 extern int kexec_add_buffer(struct kexec_buf *kbuf);
 int kexec_locate_mem_hole(struct kexec_buf *kbuf);
 int kexec_add_initrd(struct kimage *image, struct kexec_buf *kbuf);
+void kexec_rollback_segments(struct kimage *image, unsigned long nr_segments);
 #ifdef CONFIG_CRASH_DUMP
 int kexec_prepare_elf_core_header(struct kimage *image, bool need_kernel_map,
 				  unsigned long *nr_ranges);
kernel/kexec_file.c +12 -0
index 66f389346c2b..d9ae97564a4a 100644
@@ -1375,3 +1375,15 @@ int kexec_purgatory_get_set_symbol(struct kimage *image, const char *name,
 	return 0;
 }
 #endif /* CONFIG_ARCH_SUPPORTS_KEXEC_PURGATORY */
+
+/*
+ * Undo a partial load attempt: release the CMA pages backing
+ * the segments above @nr_segments and drop them from the image.
+ */
+void kexec_rollback_segments(struct kimage *image, unsigned long nr_segments)
+{
+	while (image->nr_segments > nr_segments) {
+		kexec_free_segment_cma(image, image->nr_segments - 1);
+		image->nr_segments--;
+	}
+}
Signature · 1 line
2.34.1
#10 lore raw

PATCH v9 10/11 arm64: kexec_file: Fix CMA segment placement retries

In reply to Jinjie Ruan

The file-based kexec system call places segments with kexec_add_buffer(), which takes them from CMA when kexec_alloc_contig() succeeds, to avoid copying the payload at kexec time.

When load_other_segments() cannot place the initrd or the DTB, the segments of the attempt are dropped but their CMA pages are leaked. Release them with kexec_rollback_segments() in the retry loop and in the error path of load_other_segments().

The loop makes progress by advancing buf_min past a segment placed by the walk. kexec_alloc_contig() ignores buf_min, so once the pages of a CMA segment are released, the next attempt can get the same region again and the retry makes no progress. Disable CMA for the retry.

A CMA segment can be anywhere in RAM, so moving buf_min to its end can raise the walk's lower bound close to the top of memory and leave the retry with too little space for the initrd and DTB, failing with -EADDRNOTAVAIL. Only advance buf_min for segments found by the walk.

Tested with a non-crash kexec_file_load() on QEMU virt, 1 GiB RAM and cma=64M, which places CMA at the top of RAM.

Before the fix:
The retry leaks the CMA pages and kexec_file_load() returns -EADDRNOTAVAIL even though RAM below CMA is free.

After the fix:
The kernel and initrd are placed below CMA and the second kernel boots.

Fixes 07d24902977e ("kexec: enable CMA based contiguous allocation")
Cc Catalin Marinas <[email protected]>
Cc Will Deacon <[email protected]>
Cc Breno Leitao <[email protected]>
Cc Pratyush Yadav <[email protected]>
Cc Andrew Morton <[email protected]>
Cc Yeoreum Yun <[email protected]>
Cc Baoquan He <[email protected]>
---
 arch/arm64/kernel/kexec_image.c        | 12 ++++++++----
 arch/arm64/kernel/machine_kexec_file.c |  2 +-
 2 files changed, 9 insertions(+), 5 deletions(-)
arch/arm64/kernel/kexec_image.c +8 -4
index a2a7c283b331..1cbb3c73c6e9 100644
@@ -109,11 +109,15 @@ static void *image_load(struct kimage *image,
 			break;
 
 		/*
-		 * We couldn't find space for the other segments; erase the
-		 * kernel segment and try the next available hole.
+		 * kexec_alloc_contig() ignores buf_min and always returns
+		 * CMA memory, so disable it and keep buf_min.
 		 */
-		image->nr_segments -= 1;
-		kbuf.buf_min = kernel_segment->mem + kernel_segment->memsz;
+		if (image->segment_cma[kernel_segment_number])
+			image->no_cma = true;
+		else
+			kbuf.buf_min = kernel_segment->mem + kernel_segment->memsz;
+
+		kexec_rollback_segments(image, kernel_segment_number);
 		kbuf.mem = KEXEC_BUF_MEM_UNKNOWN;
 	}
 
arch/arm64/kernel/machine_kexec_file.c +1 -1
index dff2cd613f87..169d991488ff 100644
@@ -132,7 +132,7 @@ int load_other_segments(struct kimage *image, unsigned long kernel_load_addr,
 	return 0;
 
 out_err:
-	image->nr_segments = orig_segments;
+	kexec_rollback_segments(image, orig_segments);
 	kvfree(dtb);
 	return ret;
 }
Signature · 1 line
2.34.1
#11 lore raw

PATCH v9 11/11 LoongArch: kexec_file: Fix CMA segment placement retries

In reply to Jinjie Ruan

The file-based kexec system call places segments with kexec_add_buffer(), which takes them from CMA when kexec_alloc_contig() succeeds, to avoid copying the payload at kexec time.

When load_other_segments() cannot place the initrd, the segments of the attempt are dropped but their CMA pages are leaked. Release them with kexec_rollback_segments() in the retry loop and in the error path of load_other_segments().

The loop makes progress by advancing buf_min past a segment placed by the walk. kexec_alloc_contig() ignores buf_min, so once the pages of a CMA segment are released, the next attempt can get the same region again and the retry makes no progress. Disable CMA for the retry.

A CMA segment can be anywhere in RAM, so moving buf_min to its end can raise the walk's lower bound close to the top of memory and leave the retry with too little space for the initrd, failing with -EADDRNOTAVAIL. Only advance buf_min for segments found by the walk.

Tested with a non-crash kexec_file_load() on QEMU virt, 2 GiB RAM and cma=64M@0xe8000000, which leaves only 64 MiB above the CMA area.

Before the fix:
The retry leaks the CMA pages and kexec_file_load() returns -EADDRNOTAVAIL even though RAM below CMA is free.

After the fix:
The kernel and initrd are placed below CMA and the second kernel boots.

Fixes 55d990f0084c ("LoongArch: Add EFI binary support for kexec_file")
Cc Huacai Chen <[email protected]>
Cc WANG Xuerui <[email protected]>
Cc Youling Tang <[email protected]>
Cc Mike Rapoport (Microsoft) <[email protected]>
Cc Sourabh Jain <[email protected]>
Cc Kees Cook <[email protected]>
---
 arch/loongarch/kernel/kexec_efi.c          | 12 ++++++++----
 arch/loongarch/kernel/machine_kexec_file.c |  2 +-
 2 files changed, 9 insertions(+), 5 deletions(-)
arch/loongarch/kernel/kexec_efi.c +8 -4
index 4824c47907ac..1fbd0ee1deb6 100644
@@ -82,11 +82,15 @@ static void *efi_kexec_load(struct kimage *image,
 			break;
 
 		/*
-		 * We couldn't find space for the other segments; erase the
-		 * kernel segment and try the next available hole.
+		 * kexec_alloc_contig() ignores buf_min and always returns
+		 * CMA memory, so disable it and keep buf_min.
 		 */
-		image->nr_segments -= 1;
-		kbuf.buf_min = kernel_segment->mem + kernel_segment->memsz;
+		if (image->segment_cma[kernel_segment_number])
+			image->no_cma = true;
+		else
+			kbuf.buf_min = kernel_segment->mem + kernel_segment->memsz;
+
+		kexec_rollback_segments(image, kernel_segment_number);
 		kbuf.mem = KEXEC_BUF_MEM_UNKNOWN;
 	}
 
arch/loongarch/kernel/machine_kexec_file.c +1 -1
index 00940f20b45e..adf7416ff49f 100644
@@ -190,7 +190,7 @@ int load_other_segments(struct kimage *image,
 	return 0;
 
 out_err:
-	image->nr_segments = orig_segments;
+	kexec_rollback_segments(image, orig_segments);
 	kfree(modified_cmdline);
 	return ret;
 }
Signature · 1 line
2.34.1