Skip to contentSkip to conversation

kexec_file_load implementation for PowerPC

PATCH In review Thiago Jung Bauermann 3 participants 20 messages Nov 10, 2016 – Nov 23, 2016

Subsystems (8)
lore
Share & tools
lore
#0 lore raw

v10 cover kexec_file_load implementation for PowerPC

Hello,

[ Andrew, you might want to wait until the kexec maintainers say whether
they agree with patch 4 before picking up this version. ]

v10 addresses two requests from Michael Ellerman: build the purgatory as a Position Independent Executable binary to reduce the number of relocation types that we have to deal with, and remove code that is not strictly necessary from the purgatory.

The first objective is made possible by patch 4, which is new in this version and adds the support in generic code that arch-specific code needs if it wants to use a PIE purgatory. Now it's not necessary anymore to refactor code in module_64.c to reuse its implementation of the relocations.

I also added a script which checks purgatory.ro and warns if it has a relocation type that is not supported.

The second objective is met by removing code for printing messages to the console. Everything else that is in the purgatory is to implement the checksum verification of the kexec image.

The gory details are in the detailed changelog at the end of this email.

Original cover letter:

This patch series implements the kexec_file_load system call on PowerPC.

This system call moves the reading of the kernel, initrd and the device tree from the userspace kexec tool to the kernel. This is needed if you want to do one or both of the following:

1. only allow loading of signed kernels.
2. "measure" (i.e., record the hashes of) the kernel, initrd, kernel command line and other boot inputs for the Integrity Measurement Architecture subsystem.

The above are the functions kexec already has built into kexec_file_load. Yesterday I posted a set of patches which allows a third feature:

3. have IMA pass-on its event log (where integrity measurements are registered) accross kexec to the second kernel, so that the event history is preserved.

Because OpenPower uses an intermediary Linux instance as a boot loader (skiroot), feature 1 is needed to implement secure boot for the platform, while features 2 and 3 are needed to implement trusted boot.

This patch series starts by removing an x86 assumption from kexec_file:
kexec_add_buffer uses iomem to find reserved memory ranges, but PowerPC uses the memblock subsystem. A hook is added so that each arch can specify how memory ranges can be found.

Also, the memory-walking logic in kexec_add_buffer is useful in this implementation to find a free area for the purgatory's stack, so the next patch moves that logic to kexec_locate_mem_hole.

The kexec_file_load system call needs to apply relocations to the purgatory but adding code for that would duplicate functionality with the module loading mechanism, which also needs to apply relocations to the kernel modules. Therefore, this patch series factors out the module relocation code so that it can be shared.

One thing that is still missing is crashkernel support, which I intend to submit shortly. For now, arch_kexec_kernel_image_probe rejects crash kernels.

This code is based on kexec-tools, but with many modifications to adapt it to the kernel environment and facilities. Except the purgatory, which only has minimal changes.

Changes for v10:
- Patch "kexec_file: Add support for purgatory built as PIE."
- New patch.
- Patch "powerpc: Factor out relocation code in module_64.c"
- Dropped patch from series.
- Patch "powerpc: Implement kexec_file_load."
- Made CONFIG_KEXEC_FILE select HAVE_KEXEC_FILE_PIE_PURGATORY.
- Removed elf_my_r2 and elf_toc_section functions, they aren't necessary anymore.
- Adapted arch_kexec_apply_relocations_add to the absolute addresses found in PIE binaries. Apply the relocations in this function instead of calling elf64_apply_relocate_add_item (which doesn't exist anymore).
- No need to change module_64.c anymore.
- Implemented R_PPC64_RELATIVE relocation.
- Patch "powerpc: Add functions to read ELF files of any endianness."
- Introduce <asm/elf_util.h> and elf_util.c in this patch.
- Patch "powerpc: Add support for loading ELF kernels with kexec_file_load."
- Define PURGATORY_ELF_TYPE macro in <asm/kexec.h>.
- Removed debug argument from the setup_purgatory function.
- Don't call find_debug_console in elf64_load anymore.
- Removed function find_debug_console.
- Find the TOC address by looking for the .got section in setup_purgatory.
- Don't set the "debug" purgatory symbol (which doesn't exist anymore) in setup_purgatory.
- Patch "powerpc: Add purgatory for kexec_file_load implementation."
- Don't change boot/string.S.
- Build purgatory as a PIE binary and suppress generation of the INTERP segment, which is not meaningful in this case.
- Add check-purgatory-relocs.sh and call it after building purgatory.ro.
- Removed console-ppc64.c, hvCall.S, ppc64_asm.h and printf.c.
- Moved inclusion of <linux/compiler.h> from purgatory.h to

    purgatory-ppc64.c.

- Removed "debug" global variable from purgatory-ppc64.c.
- Removed inclusion of "../boot/string.h" from purgatory.c.
- Removed printf calls from purgatory.c.
- Included <linux/types.h>, added prototype for memcmp and removed prototypes for putchar, sprintf and printf in purgatory.h.
- Added string.c with code copied from lib/string.c.
- Removed inclusion of "ppc64_asm.h" and use of DOTSYM macro from v2wrap.S.

Changes for v9:
- Rebased on top of v4.9-rc1
- Patch "powerpc: Change places using CONFIG_KEXEC to use CONFIG_KEXEC_CORE instead."
- Fixed conflict with patch renaming CONFIG_WORD_SIZE to BITS.
- Patch "powerpc: Factor out relocation code from module_64.c to elf_util_64.c."
- Retitled to "powerpc: Factor out relocation code in module_64.c"
- Fixed conflict with patch renaming CONFIG_WORD_SIZE to BITS.
- Put relocation code in function elf64_apply_relocate_add_item in module_64.c itself instead of moving it to elf_util_64.c.
- Only factored out the switch statement to apply the relocations instead of the whole logic to iterate through all of the relocations.
- There's no elf_util_64.c anymore.
- Doesn't change <asm/module.h> anymore.
- Doesn't change arch/powerpc/kernel/Makefile anymore.
- Moved creation of <asm/elf_util.h> to patch "Implement kexec_file_load."
- Patch "powerpc: Generalize elf64_apply_relocate_add."
- Dropped patch from series.
- Moved changes adding address variable to patch "powerpc: Implement kexec_file_load."
- Patch "powerpc: Adapt elf64_apply_relocate_add for kexec_file_load."
- Dropped patch from series.
- Moved new relocs needed by the purgatory to patch "powerpc: Implement kexec_file_load."
- Patch "powerpc: Implement kexec_file_load."
- Fixed conflict with patch renaming CONFIG_WORD_SIZE to BITS.
- Put code in a new file machine_kexec_file_64.c instead of adding it to

    machine_kexec_64.c.

- arch_kexec_apply_relocations_add now has the for loop which was previously factored out in elf64_apply_relocate_add.
- Moved definition of setup_purgatory to "powerpc: Add support for loading ELF kernels with kexec_file_load.".
- Introduce <asm/elf_util.h> and elf_util.c in this patch.
- Only build elf_util.c if CONFIG_KEXEC_FILE=y.
- Renamed my_r2 to elf_my_r2 and changed it to receive a pointer to struct elf_shdr instead of struct elf_info.
- Added toc_section function.
- Removed elf_init_elf_info function.
- Patch "powerpc: Add functions to read ELF files of any endianness."
- Fixed conflict with patch renaming CONFIG_WORD_SIZE to BITS.
- Introduce struct elf_info in this patch.
- Removed stubs_section and toc_section from struct elf_info.
- Doesn't change arch/powerpc/kernel/Makefile anymore.
- Patch "powerpc: Add code to work with device trees in kexec_file_load."
- Squashed into "powerpc: Add support for loading ELF kernels with kexec_file_load.". The code is unchanged.
- Patch "powerpc: Add support for loading ELF kernels with kexec_file_load."
- Fixed conflict with patch renaming CONFIG_WORD_SIZE to BITS.
- Fixed uninitialized variable warning when building with GCC 4.6.3.
- Don't create <asm/kexec_elf_64.h> anymore, put declaration of kexec_el_64_ops in <asm/kexec.h> instead.
- Removed unused extern declaration of kexec_purgatory_size.
- Made elf64_load static.
- Factor out delete_fdt_mem_rsv from setup_new_fdt. This change was in the patch series for preserving IMA measurements accross kexec.
- Patch "powerpc: Add purgatory for kexec_file_load implementation."
- Fixed conflict with patch renaming CONFIG_WORD_SIZE to BITS.
- Changed purgatory Makefile to use the kernel KBUILD_CFLAGS, filtering out the flags for ftrace. (Michael Ellerman)
- Added extern declaration of the debug global variable to purgatory.h and included it in console-ppc64.c to fix checkpatch.pl warning.
- Added __section attribute to global variables in purgatory-ppc64.c and don't initialize them to zero, to fix checkpatch.pl warning.
- Fixed type of debug global variable to int instead of unsigned int.
- Included <linux/compiler.h> in purgatory.h and use __printf macro to fix checkpatch.pl warning.
- Removed crashdump-ppc64.h, crashdump_backup.c and purgatory-ppc64.h.

Changes for v8:
- Rebased on top of v4.8-rc5.
- Patch "powerpc: Add purgatory for kexec_file_load implementation."
- Fix cross build from ppc64 LE to BE and vice-versa.
- Don't build the purgatory during archprepare.

Changes for v7:
- Rebased on top of v4.8-rc4.
- Patch "powerpc: Change places using CONFIG_KEXEC to use CONFIG_KEXEC_CORE instead."
- New patch. Fixes build when CONFIG_KEXEC=n and CONFIG_KEXEC_FILE=y.
- Patch "powerpc: Adapt elf64_apply_relocate_add for kexec_file_load."
- Fixed checkpatch warning "else is not generally useful after a break or return".
- Fixed checkpatch warnings about line length. (Andrew Morton)
- Patch "powerpc: Add code to work with device trees in kexec_file_load."
- Remove space before tabs in doc comment for setup_new_fdt. (Andrew Morton)
- Fixed checkpatch warnings about line length.
- Patch "powerpc: Add support for loading ELF kernels with kexec_file_load."
- Removed duplicate #include <linux/kexec.h>.

Changes for v6:
- Based directly on top of v4.8-rc1.
- Patch "powerpc: Adapt elf64_apply_relocate_add for kexec_file_load."
- Allow undefined symbols if they are relocations for the TOC in the big endian ABI.
- Fixed build error in this patch by adding the ehdr member to elf_info here instead of in the next patch.
- Initialize elf_info.ehdr in module_64.c:module_frob_arch_sections.
- Patch "powerpc: Add code to work with device trees in kexec_file_load."
- Changed find_debug_console to look for /chosen instead of receiving its offset as an argument.
- setup_new_fdt: no need to find /chosen again after deleting the memory reservation for initrd.
- Patch "powerpc: Add support for loading ELF kernels with kexec_file_load."
- Don't pass the offset to /chosen to find_debug_console.
- Patch "powerpc: Allow userspace to set device tree properties in kexec_file_load"
- Dropped patch.
- Patch "powerpc: Add purgatory for kexec_file_load implementation."
- Make boot/string.S use the DOTSYM macro so that it can be
used by the ppc64 big endian purgatory.
- Use -mcall-aixdesc to compile the purgatory on big endian ppc64.

Thiago Jung Bauermann (10):
kexec_file: Allow arch-specific memory walking for kexec_add_buffer
kexec_file: Change kexec_add_buffer to take kexec_buf as argument.
kexec_file: Factor out kexec_locate_mem_hole from kexec_add_buffer.
kexec_file: Add support for purgatory built as PIE.
powerpc: Change places using CONFIG_KEXEC to use CONFIG_KEXEC_CORE
instead.
powerpc: Implement kexec_file_load.
powerpc: Add functions to read ELF files of any endianness.
powerpc: Add support for loading ELF kernels with kexec_file_load.
powerpc: Add purgatory for kexec_file_load implementation.
powerpc: Enable CONFIG_KEXEC_FILE in powerpc server defconfigs.

 arch/Kconfig                                   |  11 +
 arch/powerpc/Kconfig                           |  16 +-
 arch/powerpc/Makefile                          |   1 +
 arch/powerpc/configs/powernv_defconfig         |   2 +
 arch/powerpc/configs/ppc64_defconfig           |   2 +
 arch/powerpc/configs/pseries_defconfig         |   2 +
 arch/powerpc/include/asm/debug.h               |   2 +-
 arch/powerpc/include/asm/elf_util.h            |  43 ++
 arch/powerpc/include/asm/kexec.h               |  18 +-
 arch/powerpc/include/asm/machdep.h             |   4 +-
 arch/powerpc/include/asm/smp.h                 |   2 +-
 arch/powerpc/include/asm/systbl.h              |   1 +
 arch/powerpc/include/asm/unistd.h              |   2 +-
 arch/powerpc/include/uapi/asm/unistd.h         |   1 +
 arch/powerpc/kernel/Makefile                   |   6 +-
 arch/powerpc/kernel/elf_util.c                 | 437 +++++++++++++++++++
 arch/powerpc/kernel/head_64.S                  |   2 +-
 arch/powerpc/kernel/kexec_elf_64.c             | 279 ++++++++++++
 arch/powerpc/kernel/machine_kexec_file_64.c    | 578 +++++++++++++++++++++++++
 arch/powerpc/kernel/misc_32.S                  |   2 +-
 arch/powerpc/kernel/misc_64.S                  |   6 +-
 arch/powerpc/kernel/prom.c                     |   2 +-
 arch/powerpc/kernel/setup_64.c                 |   4 +-
 arch/powerpc/kernel/smp.c                      |   6 +-
 arch/powerpc/kernel/traps.c                    |   2 +-
 arch/powerpc/platforms/85xx/corenet_generic.c  |   2 +-
 arch/powerpc/platforms/85xx/smp.c              |   8 +-
 arch/powerpc/platforms/cell/spu_base.c         |   2 +-
 arch/powerpc/platforms/powernv/setup.c         |   6 +-
 arch/powerpc/platforms/ps3/setup.c             |   4 +-
 arch/powerpc/platforms/pseries/Makefile        |   2 +-
 arch/powerpc/platforms/pseries/setup.c         |   4 +-
 arch/powerpc/purgatory/.gitignore              |   2 +
 arch/powerpc/purgatory/Makefile                |  40 ++
 arch/powerpc/purgatory/crtsavres.S             |   5 +
 arch/powerpc/purgatory/kexec-sha256.h          |  11 +
 arch/powerpc/purgatory/purgatory-ppc64.c       |  36 ++
 arch/powerpc/purgatory/purgatory.c             |  48 ++
 arch/powerpc/purgatory/purgatory.h             |  10 +
 arch/powerpc/purgatory/sha256.c                |   6 +
 arch/powerpc/purgatory/sha256.h                |   1 +
 arch/powerpc/purgatory/string.c                |  60 +++
 arch/powerpc/purgatory/v2wrap.S                | 132 ++++++
 arch/powerpc/scripts/check-purgatory-relocs.sh |  47 ++
 arch/x86/kernel/crash.c                        |  37 +-
 arch/x86/kernel/kexec-bzimage64.c              |  48 +-
 include/linux/kexec.h                          |  40 +-
 kernel/kexec_file.c                            | 413 +++++++++++++-----
 kernel/kexec_internal.h                        |  16 -
 49 files changed, 2197 insertions(+), 214 deletions(-)
 create mode 100644 arch/powerpc/include/asm/elf_util.h
 create mode 100644 arch/powerpc/kernel/elf_util.c
 create mode 100644 arch/powerpc/kernel/kexec_elf_64.c
 create mode 100644 arch/powerpc/kernel/machine_kexec_file_64.c
 create mode 100644 arch/powerpc/purgatory/.gitignore
 create mode 100644 arch/powerpc/purgatory/Makefile
 create mode 100644 arch/powerpc/purgatory/crtsavres.S
 create mode 100644 arch/powerpc/purgatory/kexec-sha256.h
 create mode 100644 arch/powerpc/purgatory/purgatory-ppc64.c
 create mode 100644 arch/powerpc/purgatory/purgatory.c
 create mode 100644 arch/powerpc/purgatory/purgatory.h
 create mode 100644 arch/powerpc/purgatory/sha256.c
 create mode 100644 arch/powerpc/purgatory/sha256.h
 create mode 100644 arch/powerpc/purgatory/string.c
 create mode 100644 arch/powerpc/purgatory/v2wrap.S
 create mode 100755 arch/powerpc/scripts/check-purgatory-relocs.sh
Signature · 1 line
2.7.4
#1 lore raw

PATCH v10 1/10 kexec_file: Allow arch-specific memory walking for kexec_add_buffer

Allow architectures to specify a different memory walking function for kexec_add_buffer. x86 uses iomem to track reserved memory ranges, but PowerPC uses the memblock subsystem.

---
 include/linux/kexec.h   | 29 ++++++++++++++++++++++++++++-
 kernel/kexec_file.c     | 30 ++++++++++++++++++++++--------
 kernel/kexec_internal.h | 16 ----------------
 3 files changed, 50 insertions(+), 25 deletions(-)
include/linux/kexec.h +28 -1
index 406c33dcae13..5e320ddaaa82 100644
@@ -148,7 +148,34 @@ struct kexec_file_ops {
 	kexec_verify_sig_t *verify_sig;
 #endif
 };
-#endif
+
+/**
+ * struct kexec_buf - parameters for finding a place for a buffer in memory
+ * @image:	kexec image in which memory to search.
+ * @buffer:	Contents which will be copied to the allocated memory.
+ * @bufsz:	Size of @buffer.
+ * @mem:	On return will have address of the buffer in memory.
+ * @memsz:	Size for the buffer in memory.
+ * @buf_align:	Minimum alignment needed.
+ * @buf_min:	The buffer can't be placed below this address.
+ * @buf_max:	The buffer can't be placed above this address.
+ * @top_down:	Allocate from top of memory.
+ */
+struct kexec_buf {
+	struct kimage *image;
+	char *buffer;
+	unsigned long bufsz;
+	unsigned long mem;
+	unsigned long memsz;
+	unsigned long buf_align;
+	unsigned long buf_min;
+	unsigned long buf_max;
+	bool top_down;
+};
+
+int __weak arch_kexec_walk_mem(struct kexec_buf *kbuf,
+			       int (*func)(u64, u64, void *));
+#endif /* CONFIG_KEXEC_FILE */
 
 struct kimage {
 	kimage_entry_t head;
kernel/kexec_file.c +22 -8
index 037c321c5618..f865674bff51 100644
@@ -428,6 +428,27 @@ static int locate_mem_hole_callback(u64 start, u64 end, void *arg)
 	return locate_mem_hole_bottom_up(start, end, kbuf);
 }
 
+/**
+ * arch_kexec_walk_mem - call func(data) on free memory regions
+ * @kbuf:	Context info for the search. Also passed to @func.
+ * @func:	Function to call for each memory region.
+ *
+ * Return: The memory walk will stop when func returns a non-zero value
+ * and that value will be returned. If all free regions are visited without
+ * func returning non-zero, then zero will be returned.
+ */
+int __weak arch_kexec_walk_mem(struct kexec_buf *kbuf,
+			       int (*func)(u64, u64, void *))
+{
+	if (kbuf->image->type == KEXEC_TYPE_CRASH)
+		return walk_iomem_res_desc(crashk_res.desc,
+					   IORESOURCE_SYSTEM_RAM | IORESOURCE_BUSY,
+					   crashk_res.start, crashk_res.end,
+					   kbuf, func);
+	else
+		return walk_system_ram_res(0, ULONG_MAX, kbuf, func);
+}
+
 /*
  * Helper function for placing a buffer in a kexec segment. This assumes
  * that kexec_mutex is held.
@@ -474,14 +495,7 @@ int kexec_add_buffer(struct kimage *image, char *buffer, unsigned long bufsz,
 	kbuf->top_down = top_down;
 
 	/* Walk the RAM ranges and allocate a suitable range for the buffer */
-	if (image->type == KEXEC_TYPE_CRASH)
-		ret = walk_iomem_res_desc(crashk_res.desc,
-				IORESOURCE_SYSTEM_RAM | IORESOURCE_BUSY,
-				crashk_res.start, crashk_res.end, kbuf,
-				locate_mem_hole_callback);
-	else
-		ret = walk_system_ram_res(0, -1, kbuf,
-					  locate_mem_hole_callback);
+	ret = arch_kexec_walk_mem(kbuf, locate_mem_hole_callback);
 	if (ret != 1) {
 		/* A suitable memory range could not be found for buffer */
 		return -EADDRNOTAVAIL;
kernel/kexec_internal.h +0 -16
index 0a52315d9c62..4cef7e4706b0 100644
@@ -20,22 +20,6 @@ struct kexec_sha_region {
 	unsigned long len;
 };
 
-/*
- * Keeps track of buffer parameters as provided by caller for requesting
- * memory placement of buffer.
- */
-struct kexec_buf {
-	struct kimage *image;
-	char *buffer;
-	unsigned long bufsz;
-	unsigned long mem;
-	unsigned long memsz;
-	unsigned long buf_align;
-	unsigned long buf_min;
-	unsigned long buf_max;
-	bool top_down;		/* allocate from top of memory hole */
-};
-
 void kimage_file_post_load_cleanup(struct kimage *image);
 #else /* CONFIG_KEXEC_FILE */
 static inline void kimage_file_post_load_cleanup(struct kimage *image) { }
Signature · 1 line
2.7.4
#2 lore raw

PATCH v10 2/10 kexec_file: Change kexec_add_buffer to take kexec_buf as argument.

In reply to Thiago Jung Bauermann

This is done to simplify the kexec_add_buffer argument list. Adapt all callers to set up a kexec_buf to pass to kexec_add_buffer.

In addition, change the type of kexec_buf.buffer from char * to void *. There is no particular reason for it to be a char *, and the change allows us to get rid of 3 existing casts to char * in the code.

---
 arch/x86/kernel/crash.c           | 37 ++++++++--------
 arch/x86/kernel/kexec-bzimage64.c | 48 +++++++++++----------
 include/linux/kexec.h             |  8 +---
 kernel/kexec_file.c               | 88 ++++++++++++++++++---------------------
 4 files changed, 87 insertions(+), 94 deletions(-)
arch/x86/kernel/crash.c +19 -18
index 650830e39e3a..3741461c63a0 100644
@@ -631,9 +631,9 @@ static int determine_backup_region(u64 start, u64 end, void *arg)
 
 int crash_load_segments(struct kimage *image)
 {
-	unsigned long src_start, src_sz, elf_sz;
-	void *elf_addr;
 	int ret;
+	struct kexec_buf kbuf = { .image = image, .buf_min = 0,
+				  .buf_max = ULONG_MAX, .top_down = false };
 
 	/*
 	 * Determine and load a segment for backup area. First 640K RAM
@@ -647,43 +647,44 @@ int crash_load_segments(struct kimage *image)
 	if (ret < 0)
 		return ret;
 
-	src_start = image->arch.backup_src_start;
-	src_sz = image->arch.backup_src_sz;
-
 	/* Add backup segment. */
-	if (src_sz) {
+	if (image->arch.backup_src_sz) {
+		kbuf.buffer = &crash_zero_bytes;
+		kbuf.bufsz = sizeof(crash_zero_bytes);
+		kbuf.memsz = image->arch.backup_src_sz;
+		kbuf.buf_align = PAGE_SIZE;
 		/*
 		 * Ideally there is no source for backup segment. This is
 		 * copied in purgatory after crash. Just add a zero filled
 		 * segment for now to make sure checksum logic works fine.
 		 */
-		ret = kexec_add_buffer(image, (char *)&crash_zero_bytes,
-				       sizeof(crash_zero_bytes), src_sz,
-				       PAGE_SIZE, 0, -1, 0,
-				       &image->arch.backup_load_addr);
+		ret = kexec_add_buffer(&kbuf);
 		if (ret)
 			return ret;
+		image->arch.backup_load_addr = kbuf.mem;
 		pr_debug("Loaded backup region at 0x%lx backup_start=0x%lx memsz=0x%lx\n",
-			 image->arch.backup_load_addr, src_start, src_sz);
+			 image->arch.backup_load_addr,
+			 image->arch.backup_src_start, kbuf.memsz);
 	}
 
 	/* Prepare elf headers and add a segment */
-	ret = prepare_elf_headers(image, &elf_addr, &elf_sz);
+	ret = prepare_elf_headers(image, &kbuf.buffer, &kbuf.bufsz);
 	if (ret)
 		return ret;
 
-	image->arch.elf_headers = elf_addr;
-	image->arch.elf_headers_sz = elf_sz;
+	image->arch.elf_headers = kbuf.buffer;
+	image->arch.elf_headers_sz = kbuf.bufsz;
 
-	ret = kexec_add_buffer(image, (char *)elf_addr, elf_sz, elf_sz,
-			ELF_CORE_HEADER_ALIGN, 0, -1, 0,
-			&image->arch.elf_load_addr);
+	kbuf.memsz = kbuf.bufsz;
+	kbuf.buf_align = ELF_CORE_HEADER_ALIGN;
+	ret = kexec_add_buffer(&kbuf);
 	if (ret) {
 		vfree((void *)image->arch.elf_headers);
 		return ret;
 	}
+	image->arch.elf_load_addr = kbuf.mem;
 	pr_debug("Loaded ELF headers at 0x%lx bufsz=0x%lx memsz=0x%lx\n",
-		 image->arch.elf_load_addr, elf_sz, elf_sz);
+		 image->arch.elf_load_addr, kbuf.bufsz, kbuf.bufsz);
 
 	return ret;
 }
arch/x86/kernel/kexec-bzimage64.c +26 -22

91 lines not on this page · raw message

include/linux/kexec.h +2 -6
index 5e320ddaaa82..437ef1b47428 100644
@@ -163,7 +163,7 @@ struct kexec_file_ops {
  */
 struct kexec_buf {
 	struct kimage *image;
-	char *buffer;
+	void *buffer;
 	unsigned long bufsz;
 	unsigned long mem;
 	unsigned long memsz;
@@ -175,6 +175,7 @@ struct kexec_buf {
 
 int __weak arch_kexec_walk_mem(struct kexec_buf *kbuf,
 			       int (*func)(u64, u64, void *));
+extern int kexec_add_buffer(struct kexec_buf *kbuf);
 #endif /* CONFIG_KEXEC_FILE */
 
 struct kimage {
@@ -239,11 +240,6 @@ extern asmlinkage long sys_kexec_load(unsigned long entry,
 					struct kexec_segment __user *segments,
 					unsigned long flags);
 extern int kernel_kexec(void);
-extern int kexec_add_buffer(struct kimage *image, char *buffer,
-			    unsigned long bufsz, unsigned long memsz,
-			    unsigned long buf_align, unsigned long buf_min,
-			    unsigned long buf_max, bool top_down,
-			    unsigned long *load_addr);
 extern struct page *kimage_alloc_control_pages(struct kimage *image,
 						unsigned int order);
 extern int kexec_load_purgatory(struct kimage *image, unsigned long min,
kernel/kexec_file.c +40 -48

175 lines not on this page · raw message

Signature · 1 line
2.7.4
#3 lore raw

PATCH v10 3/10 kexec_file: Factor out kexec_locate_mem_hole from kexec_add_buffer.

In reply to Thiago Jung Bauermann

kexec_locate_mem_hole will be used by the PowerPC kexec_file_load implementation to find free memory for the purgatory stack.

---
 include/linux/kexec.h |  1 +
 kernel/kexec_file.c   | 25 ++++++++++++++++++++-----
 2 files changed, 21 insertions(+), 5 deletions(-)
include/linux/kexec.h +1 -0
index 437ef1b47428..a33f63351f86 100644
@@ -176,6 +176,7 @@ struct kexec_buf {
 int __weak arch_kexec_walk_mem(struct kexec_buf *kbuf,
 			       int (*func)(u64, u64, void *));
 extern int kexec_add_buffer(struct kexec_buf *kbuf);
+int kexec_locate_mem_hole(struct kexec_buf *kbuf);
 #endif /* CONFIG_KEXEC_FILE */
 
 struct kimage {
kernel/kexec_file.c +20 -5
index efd2c094af7e..0c2df7f73792 100644
@@ -450,6 +450,23 @@ int __weak arch_kexec_walk_mem(struct kexec_buf *kbuf,
 }
 
 /**
+ * kexec_locate_mem_hole - find free memory for the purgatory or the next kernel
+ * @kbuf:	Parameters for the memory search.
+ *
+ * On success, kbuf->mem will have the start address of the memory region found.
+ *
+ * Return: 0 on success, negative errno on error.
+ */
+int kexec_locate_mem_hole(struct kexec_buf *kbuf)
+{
+	int ret;
+
+	ret = arch_kexec_walk_mem(kbuf, locate_mem_hole_callback);
+
+	return ret == 1 ? 0 : -EADDRNOTAVAIL;
+}
+
+/**
  * kexec_add_buffer - place a buffer in a kexec segment
  * @kbuf:	Buffer contents and memory parameters.
  *
@@ -489,11 +506,9 @@ int kexec_add_buffer(struct kexec_buf *kbuf)
 	kbuf->buf_align = max(kbuf->buf_align, PAGE_SIZE);
 
 	/* Walk the RAM ranges and allocate a suitable range for the buffer */
-	ret = arch_kexec_walk_mem(kbuf, locate_mem_hole_callback);
-	if (ret != 1) {
-		/* A suitable memory range could not be found for buffer */
-		return -EADDRNOTAVAIL;
-	}
+	ret = kexec_locate_mem_hole(kbuf);
+	if (ret)
+		return ret;
 
 	/* Found a suitable memory range */
 	ksegment = &kbuf->image->segment[kbuf->image->nr_segments];
Signature · 1 line
2.7.4
#4 lore raw

PATCH v10 4/10 kexec_file: Add support for purgatory built as PIE.

In reply to Thiago Jung Bauermann

powerpc's purgatory.ro has 12 relocation types when built as a relocatable object. To implement support for them requires arch_kexec_apply_relocations_add to duplicate a lot of code with module_64.c:apply_relocate_add.

When built as a Position Independent Executable there are only 4 relocation types in purgatory.ro, so it becomes practical for the powerpc implementation of kexec_file to have its own relocation implementation.

Also, the purgatory is an executable and not an intermediary output from the compiler so it makes sense conceptually that it is easier to build it as a PIE than as a partially linked object.

Apart from the greatly reduced number of relocations, there are two differences between a relocatable object and a PIE:

1. __kexec_load_purgatory needs to use the program headers rather than the section headers to figure out how to load the binary.
2. Symbol values are absolute addresses instead of relative to the start of the section.

This patch adds the support needed in generic code for the differences above and allows powerpc to load and relocate a position independent purgatory.

---
 arch/Kconfig          |  11 ++
 include/linux/kexec.h |   4 +
 kernel/kexec_file.c   | 314 ++++++++++++++++++++++++++++++++++++++------------
 3 files changed, 253 insertions(+), 76 deletions(-)
arch/Kconfig +11 -0
index 659bdd079277..f4498530a618 100644
@@ -5,6 +5,17 @@
 config KEXEC_CORE
 	bool
 
+config HAVE_KEXEC_FILE_PIE_PURGATORY
+	bool
+	help
+	  By default, the purgatory binary is built as a relocatable
+	  object, but on some architectures it might be an advantage
+	  to build it as a Position Independent Executable to reduce
+	  the types of relocation that have to be dealt with.
+
+	  If an architecture builds a PIE purgatory it should select
+	  this symbol.
+
 config OPROFILE
 	tristate "OProfile system profiling"
 	depends on PROFILING
include/linux/kexec.h +4 -0
index a33f63351f86..5c356e387240 100644
@@ -112,6 +112,10 @@ struct compat_kexec_segment {
 #endif
 
 #ifdef CONFIG_KEXEC_FILE
+#ifndef PURGATORY_ELF_TYPE
+#define PURGATORY_ELF_TYPE ET_REL
+#endif
+
 struct purgatory_info {
 	/* Pointer to elf header of read only purgatory */
 	Elf_Ehdr *ehdr;
kernel/kexec_file.c +238 -76

409 lines not on this page · raw message

Signature · 1 line
2.7.4
#14 lore raw

PATCH v10 5/10 powerpc: Change places using CONFIG_KEXEC to use CONFIG_KEXEC_CORE instead.

In reply to Thiago Jung Bauermann

Commit 2965faa5e03d ("kexec: split kexec_load syscall from kexec core code") introduced CONFIG_KEXEC_CORE so that CONFIG_KEXEC means whether the kexec_load system call should be compiled-in and CONFIG_KEXEC_FILE means whether the kexec_file_load system call should be compiled-in. These options can be set independently from each other.

Since until now powerpc only supported kexec_load, CONFIG_KEXEC and CONFIG_KEXEC_CORE were synonyms. That is not the case anymore, so we need to make a distinction. Almost all places where CONFIG_KEXEC was being used should be using CONFIG_KEXEC_CORE instead, since kexec_file_load also needs that code compiled in.

---
 arch/powerpc/Kconfig                          | 2 +-
 arch/powerpc/include/asm/debug.h              | 2 +-
 arch/powerpc/include/asm/kexec.h              | 6 +++---
 arch/powerpc/include/asm/machdep.h            | 4 ++--
 arch/powerpc/include/asm/smp.h                | 2 +-
 arch/powerpc/kernel/Makefile                  | 4 ++--
 arch/powerpc/kernel/head_64.S                 | 2 +-
 arch/powerpc/kernel/misc_32.S                 | 2 +-
 arch/powerpc/kernel/misc_64.S                 | 6 +++---
 arch/powerpc/kernel/prom.c                    | 2 +-
 arch/powerpc/kernel/setup_64.c                | 4 ++--
 arch/powerpc/kernel/smp.c                     | 6 +++---
 arch/powerpc/kernel/traps.c                   | 2 +-
 arch/powerpc/platforms/85xx/corenet_generic.c | 2 +-
 arch/powerpc/platforms/85xx/smp.c             | 8 ++++----
 arch/powerpc/platforms/cell/spu_base.c        | 2 +-
 arch/powerpc/platforms/powernv/setup.c        | 6 +++---
 arch/powerpc/platforms/ps3/setup.c            | 4 ++--
 arch/powerpc/platforms/pseries/Makefile       | 2 +-
 arch/powerpc/platforms/pseries/setup.c        | 4 ++--
 20 files changed, 36 insertions(+), 36 deletions(-)
arch/powerpc/Kconfig +1 -1
index 65fba4c34cd7..6cb59c6e5ba4 100644
@@ -489,7 +489,7 @@ config CRASH_DUMP
 
 config FA_DUMP
 	bool "Firmware-assisted dump"
-	depends on PPC64 && PPC_RTAS && CRASH_DUMP && KEXEC
+	depends on PPC64 && PPC_RTAS && CRASH_DUMP && KEXEC_CORE
 	help
 	  A robust mechanism to get reliable kernel crash dump with
 	  assistance from firmware. This approach does not use kexec,
arch/powerpc/include/asm/debug.h +1 -1
index a954e4975049..86308f177f2d 100644
@@ -10,7 +10,7 @@ struct pt_regs;
 
 extern struct dentry *powerpc_debugfs_root;
 
-#if defined(CONFIG_DEBUGGER) || defined(CONFIG_KEXEC)
+#if defined(CONFIG_DEBUGGER) || defined(CONFIG_KEXEC_CORE)
 
 extern int (*__debugger)(struct pt_regs *regs);
 extern int (*__debugger_ipi)(struct pt_regs *regs);
arch/powerpc/include/asm/kexec.h +3 -3
index a46f5f45570c..eca2f975bf44 100644
@@ -53,7 +53,7 @@
 
 typedef void (*crash_shutdown_t)(void);
 
-#ifdef CONFIG_KEXEC
+#ifdef CONFIG_KEXEC_CORE
 
 /*
  * This function is responsible for capturing register states if coming
@@ -91,7 +91,7 @@ static inline bool kdump_in_progress(void)
 	return crashing_cpu >= 0;
 }
 
-#else /* !CONFIG_KEXEC */
+#else /* !CONFIG_KEXEC_CORE */
 static inline void crash_kexec_secondary(struct pt_regs *regs) { }
 
 static inline int overlaps_crashkernel(unsigned long start, unsigned long size)
@@ -116,7 +116,7 @@ static inline bool kdump_in_progress(void)
 	return false;
 }
 
-#endif /* CONFIG_KEXEC */
+#endif /* CONFIG_KEXEC_CORE */
 #endif /* ! __ASSEMBLY__ */
 #endif /* __KERNEL__ */
 #endif /* _ASM_POWERPC_KEXEC_H */
arch/powerpc/include/asm/machdep.h +2 -2
index e02cbc6a6c70..5011b69107a7 100644
@@ -183,7 +183,7 @@ struct machdep_calls {
 	 */
 	void (*machine_shutdown)(void);
 
-#ifdef CONFIG_KEXEC
+#ifdef CONFIG_KEXEC_CORE
 	void (*kexec_cpu_down)(int crash_shutdown, int secondary);
 
 	/* Called to do what every setup is needed on image and the
@@ -198,7 +198,7 @@ struct machdep_calls {
 	 * no return.
 	 */
 	void (*machine_kexec)(struct kimage *image);
-#endif /* CONFIG_KEXEC */
+#endif /* CONFIG_KEXEC_CORE */
 
 #ifdef CONFIG_SUSPEND
 	/* These are called to disable and enable, respectively, IRQs when
arch/powerpc/include/asm/smp.h +1 -1
index 0d02c11dc331..32db16d2e7ad 100644
@@ -176,7 +176,7 @@ static inline void set_hard_smp_processor_id(int cpu, int phys)
 #endif /* !CONFIG_SMP */
 #endif /* !CONFIG_PPC64 */
 
-#if defined(CONFIG_PPC64) && (defined(CONFIG_SMP) || defined(CONFIG_KEXEC))
+#if defined(CONFIG_PPC64) && (defined(CONFIG_SMP) || defined(CONFIG_KEXEC_CORE))
 extern void smp_release_cpus(void);
 #else
 static inline void smp_release_cpus(void) { };
arch/powerpc/kernel/Makefile +2 -2
index 1925341dbb9c..22534a56c914 100644
@@ -107,7 +107,7 @@ pci64-$(CONFIG_PPC64)		+= pci_dn.o pci-hotplug.o isa-bridge.o
 obj-$(CONFIG_PCI)		+= pci_$(BITS).o $(pci64-y) \
 				   pci-common.o pci_of_scan.o
 obj-$(CONFIG_PCI_MSI)		+= msi.o
-obj-$(CONFIG_KEXEC)		+= machine_kexec.o crash.o \
+obj-$(CONFIG_KEXEC_CORE)	+= machine_kexec.o crash.o \
 				   machine_kexec_$(BITS).o
 obj-$(CONFIG_AUDIT)		+= audit.o
 obj64-$(CONFIG_AUDIT)		+= compat_audit.o
@@ -128,7 +128,7 @@ obj64-$(CONFIG_PPC_TRANSACTIONAL_MEM)	+= tm.o
 obj-$(CONFIG_PPC64)		+= $(obj64-y)
 obj-$(CONFIG_PPC32)		+= $(obj32-y)
 
-ifneq ($(CONFIG_XMON)$(CONFIG_KEXEC),)
+ifneq ($(CONFIG_XMON)$(CONFIG_KEXEC_CORE),)
 obj-y				+= ppc_save_regs.o
 endif
 
arch/powerpc/kernel/head_64.S +1 -1
index 04c546e20cc0..d3d482b628a6 100644
@@ -153,7 +153,7 @@ __secondary_hold:
 	cmpdi	0,r12,0
 	beq	100b
 
-#if defined(CONFIG_SMP) || defined(CONFIG_KEXEC)
+#if defined(CONFIG_SMP) || defined(CONFIG_KEXEC_CORE)
 #ifdef CONFIG_PPC_BOOK3E
 	tovirt(r12,r12)
 #endif
arch/powerpc/kernel/misc_32.S +1 -1
index 93cf7a5846a6..1863324c6a3c 100644
@@ -614,7 +614,7 @@ _GLOBAL(start_secondary_resume)
 _GLOBAL(__main)
 	blr
 
-#ifdef CONFIG_KEXEC
+#ifdef CONFIG_KEXEC_CORE
 	/*
 	 * Must be relocatable PIC code callable as a C function.
 	 */
arch/powerpc/kernel/misc_64.S +3 -3
index 4f178671f230..32be2a844947 100644
@@ -478,7 +478,7 @@ _GLOBAL(kexec_wait)
 	addi	r5,r5,kexec_flag-1b
 
 99:	HMT_LOW
-#ifdef CONFIG_KEXEC		/* use no memory without kexec */
+#ifdef CONFIG_KEXEC_CORE	/* use no memory without kexec */
 	lwz	r4,0(r5)
 	cmpwi	0,r4,0
 	beq	99b
@@ -503,7 +503,7 @@ kexec_flag:
 	.long	0
 
 
-#ifdef CONFIG_KEXEC
+#ifdef CONFIG_KEXEC_CORE
 #ifdef CONFIG_PPC_BOOK3E
 /*
  * BOOK3E has no real MMU mode, so we have to setup the initial TLB
@@ -716,4 +716,4 @@ _GLOBAL(kexec_sequence)
 	mtlr	4
 	li	r5,0
 	blr	/* image->start(physid, image->start, 0); */
-#endif /* CONFIG_KEXEC */
+#endif /* CONFIG_KEXEC_CORE */
arch/powerpc/kernel/prom.c +1 -1
index b0245bed6f54..fa7aea479fba 100644
@@ -427,7 +427,7 @@ static int __init early_init_dt_scan_chosen_ppc(unsigned long node,
 		tce_alloc_end = *lprop;
 #endif
 
-#ifdef CONFIG_KEXEC
+#ifdef CONFIG_KEXEC_CORE
 	lprop = of_get_flat_dt_prop(node, "linux,crashkernel-base", NULL);
 	if (lprop)
 		crashk_res.start = *lprop;
arch/powerpc/kernel/setup_64.c +2 -2
index 7ac8e6eaab5b..c3e129080c31 100644
@@ -346,7 +346,7 @@ void early_setup_secondary(void)
 
 #endif /* CONFIG_SMP */
 
-#if defined(CONFIG_SMP) || defined(CONFIG_KEXEC)
+#if defined(CONFIG_SMP) || defined(CONFIG_KEXEC_CORE)
 static bool use_spinloop(void)
 {
 	if (!IS_ENABLED(CONFIG_PPC_BOOK3E))
@@ -391,7 +391,7 @@ void smp_release_cpus(void)
 
 	DBG(" <- smp_release_cpus()\n");
 }
-#endif /* CONFIG_SMP || CONFIG_KEXEC */
+#endif /* CONFIG_SMP || CONFIG_KEXEC_CORE */
 
 /*
  * Initialize some remaining members of the ppc64_caches and systemcfg
arch/powerpc/kernel/smp.c +3 -3
index 9c6f3fd58059..893bd7f79be6 100644
@@ -193,7 +193,7 @@ int smp_request_message_ipi(int virq, int msg)
 	if (msg < 0 || msg > PPC_MSG_DEBUGGER_BREAK) {
 		return -EINVAL;
 	}
-#if !defined(CONFIG_DEBUGGER) && !defined(CONFIG_KEXEC)
+#if !defined(CONFIG_DEBUGGER) && !defined(CONFIG_KEXEC_CORE)
 	if (msg == PPC_MSG_DEBUGGER_BREAK) {
 		return 1;
 	}
@@ -325,7 +325,7 @@ void tick_broadcast(const struct cpumask *mask)
 }
 #endif
 
-#if defined(CONFIG_DEBUGGER) || defined(CONFIG_KEXEC)
+#if defined(CONFIG_DEBUGGER) || defined(CONFIG_KEXEC_CORE)
 void smp_send_debugger_break(void)
 {
 	int cpu;
@@ -340,7 +340,7 @@ void smp_send_debugger_break(void)
 }
 #endif
 
-#ifdef CONFIG_KEXEC
+#ifdef CONFIG_KEXEC_CORE
 void crash_send_ipi(void (*crash_ipi_callback)(struct pt_regs *))
 {
 	crash_ipi_function_ptr = crash_ipi_callback;
arch/powerpc/kernel/traps.c +1 -1
index 023a462725b5..d26605d950fe 100644
@@ -65,7 +65,7 @@
 #include <asm/hmi.h>
 #include <sysdev/fsl_pci.h>
 
-#if defined(CONFIG_DEBUGGER) || defined(CONFIG_KEXEC)
+#if defined(CONFIG_DEBUGGER) || defined(CONFIG_KEXEC_CORE)
 int (*__debugger)(struct pt_regs *regs) __read_mostly;
 int (*__debugger_ipi)(struct pt_regs *regs) __read_mostly;
 int (*__debugger_bpt)(struct pt_regs *regs) __read_mostly;
arch/powerpc/platforms/85xx/corenet_generic.c +1 -1
index 1179115a4b5c..3803b0addf65 100644
@@ -220,7 +220,7 @@ define_machine(corenet_generic) {
  *
  * Likewise, problems have been seen with kexec when coreint is enabled.
  */
-#if defined(CONFIG_HOTPLUG_CPU) || defined(CONFIG_KEXEC)
+#if defined(CONFIG_HOTPLUG_CPU) || defined(CONFIG_KEXEC_CORE)
 	.get_irq		= mpic_get_irq,
 #else
 	.get_irq		= mpic_get_coreint_irq,
arch/powerpc/platforms/85xx/smp.c +4 -4
index fe9f19e5e935..a83a6d26090d 100644
@@ -349,13 +349,13 @@ struct smp_ops_t smp_85xx_ops = {
 	.cpu_disable	= generic_cpu_disable,
 	.cpu_die	= generic_cpu_die,
 #endif
-#if defined(CONFIG_KEXEC) && !defined(CONFIG_PPC64)
+#if defined(CONFIG_KEXEC_CORE) && !defined(CONFIG_PPC64)
 	.give_timebase	= smp_generic_give_timebase,
 	.take_timebase	= smp_generic_take_timebase,
 #endif
 };
 
-#ifdef CONFIG_KEXEC
+#ifdef CONFIG_KEXEC_CORE
 #ifdef CONFIG_PPC32
 atomic_t kexec_down_cpus = ATOMIC_INIT(0);
 
@@ -458,7 +458,7 @@ static void mpc85xx_smp_machine_kexec(struct kimage *image)
 
 	default_machine_kexec(image);
 }
-#endif /* CONFIG_KEXEC */
+#endif /* CONFIG_KEXEC_CORE */
 
 static void smp_85xx_basic_setup(int cpu_nr)
 {
@@ -512,7 +512,7 @@ void __init mpc85xx_smp_init(void)
 #endif
 	smp_ops = &smp_85xx_ops;
 
-#ifdef CONFIG_KEXEC
+#ifdef CONFIG_KEXEC_CORE
 	ppc_md.kexec_cpu_down = mpc85xx_smp_kexec_cpu_down;
 	ppc_md.machine_kexec = mpc85xx_smp_machine_kexec;
 #endif
arch/powerpc/platforms/cell/spu_base.c +1 -1
index e84d8fbc2e21..96c2b8a40630 100644
@@ -676,7 +676,7 @@ static ssize_t spu_stat_show(struct device *dev,
 
 static DEVICE_ATTR(stat, 0444, spu_stat_show, NULL);
 
-#ifdef CONFIG_KEXEC
+#ifdef CONFIG_KEXEC_CORE
 
 struct crash_spu_info {
 	struct spu *spu;
arch/powerpc/platforms/powernv/setup.c +3 -3
index efe8b6bb168b..d50c7d99baaf 100644
@@ -174,7 +174,7 @@ static void pnv_shutdown(void)
 	opal_shutdown();
 }
 
-#ifdef CONFIG_KEXEC
+#ifdef CONFIG_KEXEC_CORE
 static void pnv_kexec_wait_secondaries_down(void)
 {
 	int my_cpu, i, notified = -1;
@@ -245,7 +245,7 @@ static void pnv_kexec_cpu_down(int crash_shutdown, int secondary)
 		opal_reinit_cpus(OPAL_REINIT_CPUS_HILE_BE);
 	}
 }
-#endif /* CONFIG_KEXEC */
+#endif /* CONFIG_KEXEC_CORE */
 
 #ifdef CONFIG_MEMORY_HOTPLUG_SPARSE
 static unsigned long pnv_memory_block_size(void)
@@ -311,7 +311,7 @@ define_machine(powernv) {
 	.machine_shutdown	= pnv_shutdown,
 	.power_save             = NULL,
 	.calibrate_decr		= generic_calibrate_decr,
-#ifdef CONFIG_KEXEC
+#ifdef CONFIG_KEXEC_CORE
 	.kexec_cpu_down		= pnv_kexec_cpu_down,
 #endif
 #ifdef CONFIG_MEMORY_HOTPLUG_SPARSE
arch/powerpc/platforms/ps3/setup.c +2 -2
index 3a487e7f4a5e..6244bc849469 100644
@@ -250,7 +250,7 @@ static int __init ps3_probe(void)
 	return 1;
 }
 
-#if defined(CONFIG_KEXEC)
+#if defined(CONFIG_KEXEC_CORE)
 static void ps3_kexec_cpu_down(int crash_shutdown, int secondary)
 {
 	int cpu = smp_processor_id();
@@ -276,7 +276,7 @@ define_machine(ps3) {
 	.progress			= ps3_progress,
 	.restart			= ps3_restart,
 	.halt				= ps3_halt,
-#if defined(CONFIG_KEXEC)
+#if defined(CONFIG_KEXEC_CORE)
 	.kexec_cpu_down			= ps3_kexec_cpu_down,
 #endif
 };
arch/powerpc/platforms/pseries/Makefile +1 -1
index fedc2ccf029d..dd9d9c2ba71b 100644
@@ -8,7 +8,7 @@ obj-y			:= lpar.o hvCall.o nvram.o reconfig.o \
 			   pci.o pci_dlpar.o eeh_pseries.o msi.o
 obj-$(CONFIG_SMP)	+= smp.o
 obj-$(CONFIG_SCANLOG)	+= scanlog.o
-obj-$(CONFIG_KEXEC)	+= kexec.o
+obj-$(CONFIG_KEXEC_CORE)	+= kexec.o
 obj-$(CONFIG_PSERIES_ENERGY)	+= pseries_energy.o
 
 obj-$(CONFIG_HOTPLUG_CPU)	+= hotplug-cpu.o
arch/powerpc/platforms/pseries/setup.c +2 -2
index 97aa3f332f24..7736352f7279 100644
@@ -367,7 +367,7 @@ void pseries_disable_reloc_on_exc(void)
 }
 EXPORT_SYMBOL(pseries_disable_reloc_on_exc);
 
-#ifdef CONFIG_KEXEC
+#ifdef CONFIG_KEXEC_CORE
 static void pSeries_machine_kexec(struct kimage *image)
 {
 	if (firmware_has_feature(FW_FEATURE_SET_MODE))
@@ -725,7 +725,7 @@ define_machine(pseries) {
 	.progress		= rtas_progress,
 	.system_reset_exception = pSeries_system_reset_exception,
 	.machine_check_exception = pSeries_machine_check_exception,
-#ifdef CONFIG_KEXEC
+#ifdef CONFIG_KEXEC_CORE
 	.machine_kexec          = pSeries_machine_kexec,
 	.kexec_cpu_down         = pseries_kexec_cpu_down,
 #endif
Signature · 1 line
2.7.4
#15 lore raw

PATCH v10 6/10 powerpc: Implement kexec_file_load.

In reply to Thiago Jung Bauermann

Add arch-specific functions needed by the generic kexec_file code.

---
 arch/powerpc/Kconfig                        |  14 ++
 arch/powerpc/include/asm/systbl.h           |   1 +
 arch/powerpc/include/asm/unistd.h           |   2 +-
 arch/powerpc/include/uapi/asm/unistd.h      |   1 +
 arch/powerpc/kernel/Makefile                |   1 +
 arch/powerpc/kernel/machine_kexec_file_64.c | 301 ++++++++++++++++++++++++++++
 6 files changed, 319 insertions(+), 1 deletion(-)
arch/powerpc/Kconfig +14 -0
index 6cb59c6e5ba4..a5a7bcf30c05 100644
@@ -455,6 +455,20 @@ config KEXEC
 	  interface is strongly in flux, so no good recommendation can be
 	  made.
 
+config KEXEC_FILE
+	bool "kexec file based system call"
+	select KEXEC_CORE
+	select HAVE_KEXEC_FILE_PIE_PURGATORY
+	select BUILD_BIN2C
+	depends on PPC64
+	depends on CRYPTO=y
+	depends on CRYPTO_SHA256=y
+	help
+	  This is a new version of the kexec system call. This call is
+	  file based and takes in file descriptors as system call arguments
+	  for kernel and initramfs as opposed to a list of segments as is the
+	  case for the older kexec call.
+
 config RELOCATABLE
 	bool "Build a relocatable kernel"
 	depends on (PPC64 && !COMPILE_TEST) || (FLATMEM && (44x || FSL_BOOKE))
arch/powerpc/include/asm/systbl.h +1 -0
index 2fc5d4db503c..4b369d83fe9c 100644
@@ -386,3 +386,4 @@ SYSCALL(mlock2)
 SYSCALL(copy_file_range)
 COMPAT_SYS_SPU(preadv2)
 COMPAT_SYS_SPU(pwritev2)
+SYSCALL(kexec_file_load)
arch/powerpc/include/asm/unistd.h +1 -1
index cf12c580f6b2..a01e97d3f305 100644
@@ -12,7 +12,7 @@
 #include <uapi/asm/unistd.h>
 
 
-#define NR_syscalls		382
+#define NR_syscalls		383
 
 #define __NR__exit __NR_exit
 
arch/powerpc/include/uapi/asm/unistd.h +1 -0
index e9f5f41aa55a..2f26335a3c42 100644
@@ -392,5 +392,6 @@
 #define __NR_copy_file_range	379
 #define __NR_preadv2		380
 #define __NR_pwritev2		381
+#define __NR_kexec_file_load	382
 
 #endif /* _UAPI_ASM_POWERPC_UNISTD_H_ */
arch/powerpc/kernel/Makefile +1 -0
index 22534a56c914..6de731d90bff 100644
@@ -109,6 +109,7 @@ obj-$(CONFIG_PCI)		+= pci_$(BITS).o $(pci64-y) \
 obj-$(CONFIG_PCI_MSI)		+= msi.o
 obj-$(CONFIG_KEXEC_CORE)	+= machine_kexec.o crash.o \
 				   machine_kexec_$(BITS).o
+obj-$(CONFIG_KEXEC_FILE)	+= machine_kexec_file_$(BITS).o
 obj-$(CONFIG_AUDIT)		+= audit.o
 obj64-$(CONFIG_AUDIT)		+= compat_audit.o
 
arch/powerpc/kernel/machine_kexec_file_64.c new file +301 -0

301 lines not on this page · raw message

Signature · 1 line
2.7.4
#16 lore raw

PATCH v10 7/10 powerpc: Add functions to read ELF files of any endianness.

In reply to Thiago Jung Bauermann

A little endian kernel might need to kexec a big endian kernel (the opposite is less likely but could happen as well), so we can't just cast the buffer with the binary to ELF structs and use them as is done elsewhere.

This patch adds functions which do byte-swapping as necessary when populating the ELF structs. These functions will be used in the next patch in the series.

---
 arch/powerpc/include/asm/elf_util.h |  43 ++++
 arch/powerpc/kernel/Makefile        |   2 +-
 arch/powerpc/kernel/elf_util.c      | 437 ++++++++++++++++++++++++++++++++++++
 3 files changed, 481 insertions(+), 1 deletion(-)
arch/powerpc/include/asm/elf_util.h new file +43 -0
new file mode 100644
index 000000000000..944b3a2d8b73
@@ -0,0 +1,43 @@
+/*
+ * Utility functions to work with ELF files.
+ *
+ * Copyright (C) 2016, IBM Corporation
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2, or (at your option)
+ * any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ */
+
+#ifndef _ASM_POWERPC_ELF_UTIL_H
+#define _ASM_POWERPC_ELF_UTIL_H
+
+#include <linux/elf.h>
+
+struct elf_info {
+	/*
+	 * Where the ELF binary contents are kept.
+	 * Memory managed by the user of the struct.
+	 */
+	const char *buffer;
+
+	const struct elfhdr *ehdr;
+	const struct elf_phdr *proghdrs;
+	struct elf_shdr *sechdrs;
+};
+
+static inline bool elf_is_elf_file(const struct elfhdr *ehdr)
+{
+	return memcmp(ehdr->e_ident, ELFMAG, SELFMAG) == 0;
+}
+
+int elf_read_from_buffer(const char *buf, size_t len, struct elfhdr *ehdr,
+			 struct elf_info *elf_info);
+void elf_free_info(struct elf_info *elf_info);
+
+#endif /* _ASM_POWERPC_ELF_UTIL_H */
arch/powerpc/kernel/Makefile +1 -1
index 6de731d90bff..de14b7eb11bb 100644
@@ -109,7 +109,7 @@ obj-$(CONFIG_PCI)		+= pci_$(BITS).o $(pci64-y) \
 obj-$(CONFIG_PCI_MSI)		+= msi.o
 obj-$(CONFIG_KEXEC_CORE)	+= machine_kexec.o crash.o \
 				   machine_kexec_$(BITS).o
-obj-$(CONFIG_KEXEC_FILE)	+= machine_kexec_file_$(BITS).o
+obj-$(CONFIG_KEXEC_FILE)	+= machine_kexec_file_$(BITS).o elf_util.o
 obj-$(CONFIG_AUDIT)		+= audit.o
 obj64-$(CONFIG_AUDIT)		+= compat_audit.o
 
arch/powerpc/kernel/elf_util.c new file +437 -0

437 lines not on this page · raw message

Signature · 1 line
2.7.4
#17 lore raw

PATCH v10 8/10 powerpc: Add support for loading ELF kernels with kexec_file_load.

In reply to Thiago Jung Bauermann

This uses all the infrastructure built up by the previous patches in the series to load an ELF vmlinux file and an initrd. It uses the flattened device tree at initial_boot_params as a base and adjusts memory reservations and its /chosen node for the next kernel.

---
 arch/powerpc/include/asm/kexec.h            |  12 ++
 arch/powerpc/kernel/Makefile                |   3 +-
 arch/powerpc/kernel/kexec_elf_64.c          | 279 +++++++++++++++++++++++++++
 arch/powerpc/kernel/machine_kexec_file_64.c | 281 +++++++++++++++++++++++++++-
 4 files changed, 572 insertions(+), 3 deletions(-)
arch/powerpc/include/asm/kexec.h +12 -0
index eca2f975bf44..6b8cbcf42466 100644
@@ -91,6 +91,18 @@ static inline bool kdump_in_progress(void)
 	return crashing_cpu >= 0;
 }
 
+#ifdef CONFIG_KEXEC_FILE
+#define PURGATORY_ELF_TYPE ET_EXEC
+
+extern struct kexec_file_ops kexec_elf64_ops;
+
+int setup_purgatory(struct kimage *image, const void *slave_code,
+		    const void *fdt, unsigned long kernel_load_addr,
+		    unsigned long fdt_load_addr, unsigned long stack_top);
+int setup_new_fdt(void *fdt, unsigned long initrd_load_addr,
+		  unsigned long initrd_len, const char *cmdline);
+#endif /* CONFIG_KEXEC_FILE */
+
 #else /* !CONFIG_KEXEC_CORE */
 static inline void crash_kexec_secondary(struct pt_regs *regs) { }
 
arch/powerpc/kernel/Makefile +2 -1
index de14b7eb11bb..424b13b1b2b0 100644
@@ -109,7 +109,8 @@ obj-$(CONFIG_PCI)		+= pci_$(BITS).o $(pci64-y) \
 obj-$(CONFIG_PCI_MSI)		+= msi.o
 obj-$(CONFIG_KEXEC_CORE)	+= machine_kexec.o crash.o \
 				   machine_kexec_$(BITS).o
-obj-$(CONFIG_KEXEC_FILE)	+= machine_kexec_file_$(BITS).o elf_util.o
+obj-$(CONFIG_KEXEC_FILE)	+= machine_kexec_file_$(BITS).o elf_util.o \
+				   kexec_elf_$(BITS).o
 obj-$(CONFIG_AUDIT)		+= audit.o
 obj64-$(CONFIG_AUDIT)		+= compat_audit.o
 
arch/powerpc/kernel/kexec_elf_64.c new file +279 -0

279 lines not on this page · raw message

arch/powerpc/kernel/machine_kexec_file_64.c +279 -2

304 lines not on this page · raw message

Signature · 1 line
2.7.4
#18 lore raw

PATCH v10 9/10 powerpc: Add purgatory for kexec_file_load implementation.

In reply to Thiago Jung Bauermann

This purgatory implementation comes from kexec-tools and was trimmed down a bit.

It uses the memset, memcpy and memcmp implementations from lib/string.c. It's not straightforward to #include "lib/string.c" so we simply copy those functions.

The changes made to the purgatory code relative to the version in kexec-tools were:

The support for printing messages to the console was removed.

Also, since we don't support loading a crashdump kernel via kexec_file_load yet, the code related to that functionality has been removed for now.

The sha256_regions global variable was renamed to sha_regions to match what kexec_file_load expects, and to use the sha256.c file from x86's purgatory (this avoids adding yet another SHA-256 implementation).

The global variables in purgatory.c and purgatory-ppc64.c now use a __section attribute to put them in the .data section instead of being initialized to zero. It doesn't matter what their initial value is, because they will be set by the kernel when preparing the kexec image.

Finally, some checkpatch.pl warnings were fixed.

---
 arch/powerpc/Makefile                          |   1 +
 arch/powerpc/purgatory/.gitignore              |   2 +
 arch/powerpc/purgatory/Makefile                |  40 ++++++++
 arch/powerpc/purgatory/crtsavres.S             |   5 +
 arch/powerpc/purgatory/kexec-sha256.h          |  11 +++
 arch/powerpc/purgatory/purgatory-ppc64.c       |  36 +++++++
 arch/powerpc/purgatory/purgatory.c             |  48 +++++++++
 arch/powerpc/purgatory/purgatory.h             |  10 ++
 arch/powerpc/purgatory/sha256.c                |   6 ++
 arch/powerpc/purgatory/sha256.h                |   1 +
 arch/powerpc/purgatory/string.c                |  60 +++++++++++
 arch/powerpc/purgatory/v2wrap.S                | 132 +++++++++++++++++++++++++
 arch/powerpc/scripts/check-purgatory-relocs.sh |  47 +++++++++
 13 files changed, 399 insertions(+)
arch/powerpc/Makefile +1 -0
index 617dece67924..5e7dcdaf93f5 100644
@@ -249,6 +249,7 @@ core-y				+= arch/powerpc/kernel/ \
 core-$(CONFIG_XMON)		+= arch/powerpc/xmon/
 core-$(CONFIG_KVM) 		+= arch/powerpc/kvm/
 core-$(CONFIG_PERF_EVENTS)	+= arch/powerpc/perf/
+core-$(CONFIG_KEXEC_FILE)	+= arch/powerpc/purgatory/
 
 drivers-$(CONFIG_OPROFILE)	+= arch/powerpc/oprofile/
 
arch/powerpc/purgatory/.gitignore new file +2 -0
new file mode 100644
index 000000000000..e9e66f178a6d
@@ -0,0 +1,2 @@
+kexec-purgatory.c
+purgatory.ro
arch/powerpc/purgatory/Makefile new file +40 -0
new file mode 100644
index 000000000000..2dfb53ac9944
@@ -0,0 +1,40 @@
+OBJECT_FILES_NON_STANDARD := y
+
+purgatory-y := purgatory.o string.o v2wrap.o purgatory-ppc64.o crtsavres.o \
+		sha256.o
+
+targets += $(purgatory-y)
+PURGATORY_OBJS = $(addprefix $(obj)/,$(purgatory-y))
+
+LDFLAGS_purgatory.ro := -pie --no-dynamic-linker -e purgatory_start \
+			--no-undefined -nostartfiles -nostdlib -nodefaultlibs
+targets += purgatory.ro
+
+KBUILD_CFLAGS := $(filter-out $(CC_FLAGS_FTRACE), $(KBUILD_CFLAGS))
+
+KBUILD_CFLAGS += -fno-zero-initialized-in-bss -fno-builtin -ffreestanding \
+		 -fno-stack-protector -fno-exceptions -fpie
+KBUILD_AFLAGS += -fno-exceptions -msoft-float -fpie
+
+$(obj)/purgatory.ro: $(PURGATORY_OBJS) FORCE
+		$(call if_changed,ld)
+
+targets += kexec-purgatory.c
+
+quiet_cmd_relocs_check = CALL    $<
+      cmd_relocs_check = $(CONFIG_SHELL) $< "$(OBJDUMP)" "$(obj)/purgatory.ro"
+
+PHONY += relocs_check
+relocs_check: arch/powerpc/scripts/check-purgatory-relocs.sh $(obj)/purgatory.ro
+	$(call cmd,relocs_check)
+
+CMD_BIN2C = $(objtree)/scripts/basic/bin2c
+quiet_cmd_bin2c = BIN2C   $@
+      cmd_bin2c = $(CMD_BIN2C) kexec_purgatory < $< > $@
+
+$(obj)/kexec-purgatory.c: $(obj)/purgatory.ro relocs_check FORCE
+	$(call if_changed,bin2c)
+	@:
+
+
+obj-$(CONFIG_KEXEC_FILE)	+= kexec-purgatory.o
arch/powerpc/purgatory/crtsavres.S new file +5 -0
new file mode 100644
index 000000000000..5d17e1c0d575
@@ -0,0 +1,5 @@
+#ifndef CONFIG_CC_OPTIMIZE_FOR_SIZE
+#define CONFIG_CC_OPTIMIZE_FOR_SIZE 1
+#endif
+
+#include "../lib/crtsavres.S"
arch/powerpc/purgatory/kexec-sha256.h new file +11 -0
new file mode 100644
index 000000000000..4418ed02c052
@@ -0,0 +1,11 @@
+#ifndef KEXEC_SHA256_H
+#define KEXEC_SHA256_H
+
+struct kexec_sha_region {
+	unsigned long start;
+	unsigned long len;
+};
+
+#define SHA256_REGIONS 16
+
+#endif /* KEXEC_SHA256_H */
arch/powerpc/purgatory/purgatory-ppc64.c new file +36 -0
new file mode 100644
index 000000000000..05f3216c3ded
@@ -0,0 +1,36 @@
+/*
+ * kexec: Linux boots Linux
+ *
+ * Created by: Mohan Kumar M ([email protected])
+ *
+ * Copyright (C) IBM Corporation, 2005. All rights reserved
+ *
+ * Code taken from kexec-tools.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation (version 2 of the License).
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ */
+
+#include <linux/compiler.h>
+#include "purgatory.h"
+
+unsigned long stack __section(".data");
+unsigned long dt_offset __section(".data");
+unsigned long my_toc __section(".data");
+unsigned long kernel __section(".data");
+unsigned long opal_base __section(".data");
+unsigned long opal_entry __section(".data");
+
+void setup_arch(void)
+{
+}
+
+void post_verification_setup_arch(void)
+{
+}
arch/powerpc/purgatory/purgatory.c new file +48 -0

48 lines not on this page · raw message

arch/powerpc/purgatory/purgatory.h new file +10 -0
new file mode 100644
index 000000000000..5a31628b9b99
@@ -0,0 +1,10 @@
+#ifndef PURGATORY_H
+#define PURGATORY_H
+
+#include <linux/types.h>
+
+int memcmp(const void *cs, const void *ct, size_t count);
+void setup_arch(void);
+void post_verification_setup_arch(void);
+
+#endif /* PURGATORY_H */
arch/powerpc/purgatory/sha256.c new file +6 -0
new file mode 100644
index 000000000000..6abee1877d56
@@ -0,0 +1,6 @@
+#include "../boot/string.h"
+
+/* Avoid including x86's boot/string.h in sha256.c. */
+#define BOOT_STRING_H
+
+#include "../../x86/purgatory/sha256.c"
arch/powerpc/purgatory/sha256.h new file +1 -0
new file mode 100644
index 000000000000..72818f3a207e
@@ -0,0 +1 @@
+#include "../../x86/purgatory/sha256.h"
arch/powerpc/purgatory/string.c new file +60 -0

60 lines not on this page · raw message

arch/powerpc/purgatory/v2wrap.S new file +132 -0

132 lines not on this page · raw message

arch/powerpc/scripts/check-purgatory-relocs.sh new file +47 -0

47 lines not on this page · raw message

Signature · 1 line
2.7.4
#19 lore raw

PATCH v10 10/10 powerpc: Enable CONFIG_KEXEC_FILE in powerpc server defconfigs.

In reply to Thiago Jung Bauermann

Enable CONFIG_KEXEC_FILE in powernv_defconfig, ppc64_defconfig and pseries_defconfig.

It depends on CONFIG_CRYPTO_SHA256=y, so add that as well.

---
 arch/powerpc/configs/powernv_defconfig | 2 ++
 arch/powerpc/configs/ppc64_defconfig   | 2 ++
 arch/powerpc/configs/pseries_defconfig | 2 ++
 3 files changed, 6 insertions(+)
arch/powerpc/configs/powernv_defconfig +2 -0
index d98b6eb3254f..5a190aa5534b 100644
@@ -49,6 +49,7 @@ CONFIG_BINFMT_MISC=m
 CONFIG_PPC_TRANSACTIONAL_MEM=y
 CONFIG_HOTPLUG_CPU=y
 CONFIG_KEXEC=y
+CONFIG_KEXEC_FILE=y
 CONFIG_IRQ_ALL_CPUS=y
 CONFIG_NUMA=y
 CONFIG_MEMORY_HOTPLUG=y
@@ -301,6 +302,7 @@ CONFIG_CRYPTO_CCM=m
 CONFIG_CRYPTO_PCBC=m
 CONFIG_CRYPTO_HMAC=y
 CONFIG_CRYPTO_MICHAEL_MIC=m
+CONFIG_CRYPTO_SHA256=y
 CONFIG_CRYPTO_TGR192=m
 CONFIG_CRYPTO_WP512=m
 CONFIG_CRYPTO_ANUBIS=m
arch/powerpc/configs/ppc64_defconfig +2 -0
index 58a98d40086f..0059d2088b9c 100644
@@ -46,6 +46,7 @@ CONFIG_HZ_100=y
 CONFIG_BINFMT_MISC=m
 CONFIG_PPC_TRANSACTIONAL_MEM=y
 CONFIG_KEXEC=y
+CONFIG_KEXEC_FILE=y
 CONFIG_CRASH_DUMP=y
 CONFIG_IRQ_ALL_CPUS=y
 CONFIG_MEMORY_HOTREMOVE=y
@@ -336,6 +337,7 @@ CONFIG_CRYPTO_TEST=m
 CONFIG_CRYPTO_PCBC=m
 CONFIG_CRYPTO_HMAC=y
 CONFIG_CRYPTO_MICHAEL_MIC=m
+CONFIG_CRYPTO_SHA256=y
 CONFIG_CRYPTO_TGR192=m
 CONFIG_CRYPTO_WP512=m
 CONFIG_CRYPTO_ANUBIS=m
arch/powerpc/configs/pseries_defconfig +2 -0
index 8a3bc016b732..f022f657a984 100644
@@ -52,6 +52,7 @@ CONFIG_HZ_100=y
 CONFIG_BINFMT_MISC=m
 CONFIG_PPC_TRANSACTIONAL_MEM=y
 CONFIG_KEXEC=y
+CONFIG_KEXEC_FILE=y
 CONFIG_IRQ_ALL_CPUS=y
 CONFIG_MEMORY_HOTPLUG=y
 CONFIG_MEMORY_HOTREMOVE=y
@@ -303,6 +304,7 @@ CONFIG_CRYPTO_TEST=m
 CONFIG_CRYPTO_PCBC=m
 CONFIG_CRYPTO_HMAC=y
 CONFIG_CRYPTO_MICHAEL_MIC=m
+CONFIG_CRYPTO_SHA256=y
 CONFIG_CRYPTO_TGR192=m
 CONFIG_CRYPTO_WP512=m
 CONFIG_CRYPTO_ANUBIS=m
Signature · 1 line
2.7.4
#5 lore raw

Re: [PATCH v10 04/10] kexec_file: Add support for purgatory built as PIE.

In reply to Thiago Jung Bauermann
Quoted 16 lines · Thiago Jung Bauermann

powerpc's purgatory.ro has 12 relocation types when built as a relocatable object. To implement support for them requires arch_kexec_apply_relocations_add to duplicate a lot of code with module_64.c:apply_relocate_add.

When built as a Position Independent Executable there are only 4 relocation types in purgatory.ro, so it becomes practical for the powerpc implementation of kexec_file to have its own relocation implementation.

Also, the purgatory is an executable and not an intermediary output from the compiler so it makes sense conceptually that it is easier to build it as a PIE than as a partially linked object.

Apart from the greatly reduced number of relocations, there are two differences between a relocatable object and a PIE:

Quoted 9 lines

1. __kexec_load_purgatory needs to use the program headers rather than the section headers to figure out how to load the binary.
2. Symbol values are absolute addresses instead of relative to the start of the section.

This patch adds the support needed in generic code for the differences above and allows powerpc to load and relocate a position independent purgatory.

[snip]

The kexec-tools machine_apply_elf_rel is pretty simple for ppc64, it is not that complex. So could you look into simplify your kexec_file implementation?

kernel/kexec_file.c kexec_apply_relocations only do limited things and some of the logic is in arch/x86, so move general code out of arch code, then I guess the arch code will be simpler and then we probably do not need this PIE stuff anymore.

BTW, __kexec_really_load_purgatory looks worse than ___kexec_load_purgatory ;)

Thanks
Dave

#6 lore raw

Re: [PATCH v10 04/10] kexec_file: Add support for purgatory built as PIE.

Hello Dave,

Thanks for your review.

Am Sonntag, 20. November 2016, 10:45:46 BRST schrieb Dave Young:

Quoted 29 lines · Thiago Jung Bauermann
Quoted 20 lines

powerpc's purgatory.ro has 12 relocation types when built as a relocatable object. To implement support for them requires arch_kexec_apply_relocations_add to duplicate a lot of code with module_64.c:apply_relocate_add.

When built as a Position Independent Executable there are only 4 relocation types in purgatory.ro, so it becomes practical for the powerpc implementation of kexec_file to have its own relocation implementation.

Also, the purgatory is an executable and not an intermediary output from the compiler so it makes sense conceptually that it is easier to build it as a PIE than as a partially linked object.

Apart from the greatly reduced number of relocations, there are two differences between a relocatable object and a PIE:

1. __kexec_load_purgatory needs to use the program headers rather than the

section headers to figure out how to load the binary.

Quoted 7 lines

2. Symbol values are absolute addresses instead of relative to the

start of the section.

This patch adds the support needed in generic code for the differences above and allows powerpc to load and relocate a position independent purgatory.

Quoted 5 lines

[snip]

The kexec-tools machine_apply_elf_rel is pretty simple for ppc64, it is not that complex. So could you look into simplify your kexec_file implementation?

I can try, but there is one fundamental issue here: powerpc position-dependent code relies more on relocations than x86 position-dependent code does, so there's a limit to how simple it can be made without switching to position- independent code. And it will always be more involved than it is on x86.

BTW, building x86's purgatory as PIE results in it not having any relocation at all, so it's an advantage even in that architecture. Unfortunately, the machine locks up during reboot and I didn't have time to try to figure out what's going on.

Quoted 3 lines

kernel/kexec_file.c kexec_apply_relocations only do limited things and some of the logic is in arch/x86, so move general code out of arch code, then I guess the arch code will be simpler

I agree that is a good idea. Is the patch below what you had in mind?

Quoted 1 line

and then we probably do not need this PIE stuff anymore.

If you are ok with the patch below I can post a new version of the series based on it and we can see if Michael Ellerman thinks it is enough.

Quoted 2 lines

BTW, __kexec_really_load_purgatory looks worse than ___kexec_load_purgatory ;)

Really? I find the special handling of bss makes the section-based loader a bit more confusing.

Signature · 20 lines
Thiago Jung Bauermann
IBM Linux Technology Center


Subject: [PATCH] kexec_file: Move generic relocation code from arch/x86 to
 kernel/kexec_file.c

The check for undefined symbols stays in arch-specific code because
powerpc needs to allow TOC symbols to be processed even though they're
undefined.

There is no functional change.

Suggested-by: Dave Young <[email protected]>
Signed-off-by: Thiago Jung Bauermann <[email protected]>
---
 arch/x86/kernel/machine_kexec_64.c | 160 +++++++------------------------------
 include/linux/kexec.h              |   9 ++-
 kernel/kexec_file.c                | 120 +++++++++++++++++++++++++++-
 3 files changed, 154 insertions(+), 135 deletions(-)
arch/x86/kernel/machine_kexec_64.c +31 -129
index 8c1f218926d7..f4860c408ece 100644
@@ -401,143 +401,45 @@ int arch_kexec_kernel_verify_sig(struct kimage *image, void *kernel,
 }
 #endif
 
-/*
- * Apply purgatory relocations.
- *
- * ehdr: Pointer to elf headers
- * sechdrs: Pointer to section headers.
- * relsec: section index of SHT_RELA section.
- *
- * TODO: Some of the code belongs to generic code. Move that in kexec.c.
- */
-int arch_kexec_apply_relocations_add(const Elf64_Ehdr *ehdr,
-				     Elf64_Shdr *sechdrs, unsigned int relsec)
+int arch_kexec_apply_relocation_add(const Elf_Ehdr *ehdr, Elf_Shdr *sechdrs,
+				    unsigned int reltype, Elf_Sym *sym,
+				    const char *name, unsigned long *location,
+				    unsigned long address, unsigned long value)
 {
-	unsigned int i;
-	Elf64_Rela *rel;
-	Elf64_Sym *sym;
-	void *location;
-	Elf64_Shdr *section, *symtabsec;
-	unsigned long address, sec_base, value;
-	const char *strtab, *name, *shstrtab;
-
-	/*
-	 * ->sh_offset has been modified to keep the pointer to section
-	 * contents in memory
-	 */
-	rel = (void *)sechdrs[relsec].sh_offset;
-
-	/* Section to which relocations apply */
-	section = &sechdrs[sechdrs[relsec].sh_info];
-
-	pr_debug("Applying relocate section %u to %u\n", relsec,
-		 sechdrs[relsec].sh_info);
-
-	/* Associated symbol table */
-	symtabsec = &sechdrs[sechdrs[relsec].sh_link];
-
-	/* String table */
-	if (symtabsec->sh_link >= ehdr->e_shnum) {
-		/* Invalid strtab section number */
-		pr_err("Invalid string table section index %d\n",
-		       symtabsec->sh_link);
+	if (sym->st_shndx == SHN_UNDEF) {
+		pr_err("Undefined symbol: %s\n", name);
 		return -ENOEXEC;
 	}
 
-	strtab = (char *)sechdrs[symtabsec->sh_link].sh_offset;
-
-	/* section header string table */
-	shstrtab = (char *)sechdrs[ehdr->e_shstrndx].sh_offset;
-
-	for (i = 0; i < sechdrs[relsec].sh_size / sizeof(*rel); i++) {
-
-		/*
-		 * rel[i].r_offset contains byte offset from beginning
-		 * of section to the storage unit affected.
-		 *
-		 * This is location to update (->sh_offset). This is temporary
-		 * buffer where section is currently loaded. This will finally
-		 * be loaded to a different address later, pointed to by
-		 * ->sh_addr. kexec takes care of moving it
-		 *  (kexec_load_segment()).
-		 */
-		location = (void *)(section->sh_offset + rel[i].r_offset);
-
-		/* Final address of the location */
-		address = section->sh_addr + rel[i].r_offset;
-
-		/*
-		 * rel[i].r_info contains information about symbol table index
-		 * w.r.t which relocation must be made and type of relocation
-		 * to apply. ELF64_R_SYM() and ELF64_R_TYPE() macros get
-		 * these respectively.
-		 */
-		sym = (Elf64_Sym *)symtabsec->sh_offset +
-				ELF64_R_SYM(rel[i].r_info);
-
-		if (sym->st_name)
-			name = strtab + sym->st_name;
-		else
-			name = shstrtab + sechdrs[sym->st_shndx].sh_name;
-
-		pr_debug("Symbol: %s info: %02x shndx: %02x value=%llx size: %llx\n",
-			 name, sym->st_info, sym->st_shndx, sym->st_value,
-			 sym->st_size);
-
-		if (sym->st_shndx == SHN_UNDEF) {
-			pr_err("Undefined symbol: %s\n", name);
-			return -ENOEXEC;
-		}
-
-		if (sym->st_shndx == SHN_COMMON) {
-			pr_err("symbol '%s' in common section\n", name);
-			return -ENOEXEC;
-		}
-
-		if (sym->st_shndx == SHN_ABS)
-			sec_base = 0;
-		else if (sym->st_shndx >= ehdr->e_shnum) {
-			pr_err("Invalid section %d for symbol %s\n",
-			       sym->st_shndx, name);
-			return -ENOEXEC;
-		} else
-			sec_base = sechdrs[sym->st_shndx].sh_addr;
-
-		value = sym->st_value;
-		value += sec_base;
-		value += rel[i].r_addend;
-
-		switch (ELF64_R_TYPE(rel[i].r_info)) {
-		case R_X86_64_NONE:
-			break;
-		case R_X86_64_64:
-			*(u64 *)location = value;
-			break;
-		case R_X86_64_32:
-			*(u32 *)location = value;
-			if (value != *(u32 *)location)
-				goto overflow;
-			break;
-		case R_X86_64_32S:
-			*(s32 *)location = value;
-			if ((s64)value != *(s32 *)location)
-				goto overflow;
-			break;
-		case R_X86_64_PC32:
-			value -= (u64)address;
-			*(u32 *)location = value;
-			break;
-		default:
-			pr_err("Unknown rela relocation: %llu\n",
-			       ELF64_R_TYPE(rel[i].r_info));
-			return -ENOEXEC;
-		}
+	switch (reltype) {
+	case R_X86_64_NONE:
+		break;
+	case R_X86_64_64:
+		*(u64 *)location = value;
+		break;
+	case R_X86_64_32:
+		*(u32 *)location = value;
+		if (value != *(u32 *)location)
+			goto overflow;
+		break;
+	case R_X86_64_32S:
+		*(s32 *)location = value;
+		if ((s64)value != *(s32 *)location)
+			goto overflow;
+		break;
+	case R_X86_64_PC32:
+		value -= (u64)address;
+		*(u32 *)location = value;
+		break;
+	default:
+		pr_err("Unknown rela relocation: %u\n", reltype);
+		return -ENOEXEC;
 	}
+
 	return 0;
 
 overflow:
-	pr_err("Overflow in relocation type %d value 0x%lx\n",
-	       (int)ELF64_R_TYPE(rel[i].r_info), value);
+	pr_err("Overflow in relocation type %u value 0x%lx\n", reltype, value);
 	return -ENOEXEC;
 }
 #endif /* CONFIG_KEXEC_FILE */
include/linux/kexec.h +7 -2
index 406c33dcae13..e171a083540d 100644
@@ -320,8 +320,13 @@ void * __weak arch_kexec_kernel_image_load(struct kimage *image);
 int __weak arch_kimage_file_post_load_cleanup(struct kimage *image);
 int __weak arch_kexec_kernel_verify_sig(struct kimage *image, void *buf,
 					unsigned long buf_len);
-int __weak arch_kexec_apply_relocations_add(const Elf_Ehdr *ehdr,
-					Elf_Shdr *sechdrs, unsigned int relsec);
+int __weak arch_kexec_apply_relocation_add(const Elf_Ehdr *ehdr,
+					   Elf_Shdr *sechdrs,
+					   unsigned int reltype, Elf_Sym *sym,
+					   const char *name,
+					   unsigned long *location,
+					   unsigned long address,
+					   unsigned long value);
 int __weak arch_kexec_apply_relocations(const Elf_Ehdr *ehdr, Elf_Shdr *sechdrs,
 					unsigned int relsec);
 void arch_kexec_protect_crashkres(void);
kernel/kexec_file.c +116 -4

138 lines not on this page · raw message

Signature · 1 line
2.7.4
#7 lore raw

Re: [PATCH v10 04/10] kexec_file: Add support for purgatory built as PIE.

Quoted 54 lines · Thiago Jung Bauermann

Hello Dave,

Thanks for your review.

Am Sonntag, 20. November 2016, 10:45:46 BRST schrieb Dave Young:

Quoted 29 lines · Thiago Jung Bauermann
Quoted 20 lines

powerpc's purgatory.ro has 12 relocation types when built as a relocatable object. To implement support for them requires arch_kexec_apply_relocations_add to duplicate a lot of code with module_64.c:apply_relocate_add.

When built as a Position Independent Executable there are only 4 relocation types in purgatory.ro, so it becomes practical for the powerpc implementation of kexec_file to have its own relocation implementation.

Also, the purgatory is an executable and not an intermediary output from the compiler so it makes sense conceptually that it is easier to build it as a PIE than as a partially linked object.

Apart from the greatly reduced number of relocations, there are two differences between a relocatable object and a PIE:

1. __kexec_load_purgatory needs to use the program headers rather than the

section headers to figure out how to load the binary.

Quoted 7 lines

2. Symbol values are absolute addresses instead of relative to the

start of the section.

This patch adds the support needed in generic code for the differences above and allows powerpc to load and relocate a position independent purgatory.

Quoted 5 lines

[snip]

The kexec-tools machine_apply_elf_rel is pretty simple for ppc64, it is not that complex. So could you look into simplify your kexec_file implementation?

I can try, but there is one fundamental issue here: powerpc position-dependent code relies more on relocations than x86 position-dependent code does, so there's a limit to how simple it can be made without switching to position- independent code. And it will always be more involved than it is on x86.

BTW, building x86's purgatory as PIE results in it not having any relocation at all, so it's an advantage even in that architecture. Unfortunately, the machine locks up during reboot and I didn't have time to try to figure out what's going on.

Quoted 3 lines

kernel/kexec_file.c kexec_apply_relocations only do limited things and some of the logic is in arch/x86, so move general code out of arch code, then I guess the arch code will be simpler

Quoted 7 lines

I agree that is a good idea. Is the patch below what you had in mind?

Quoted 1 line

and then we probably do not need this PIE stuff anymore.

If you are ok with the patch below I can post a new version of the series based on it and we can see if Michael Ellerman thinks it is enough.

Will review it and do a test. Thanks. I believe this will benefit for other arches if they want a kexec_file in the future.

Quoted 5 lines
Quoted 2 lines

BTW, __kexec_really_load_purgatory looks worse than ___kexec_load_purgatory ;)

Really? I find the special handling of bss makes the section-based loader a bit more confusing.

I'm not sure I understand above about "*bss*", personally I like ___kexec_load_purgatory more. But anyway if we move arch code as general code then it will be not necessary anymore..

Quoted 371 lines
Signature · 21 lines
-- 
Thiago Jung Bauermann
IBM Linux Technology Center


Subject: [PATCH] kexec_file: Move generic relocation code from arch/x86 to
 kernel/kexec_file.c

The check for undefined symbols stays in arch-specific code because
powerpc needs to allow TOC symbols to be processed even though they're
undefined.

There is no functional change.

Suggested-by: Dave Young <[email protected]>
Signed-off-by: Thiago Jung Bauermann <[email protected]>
---
 arch/x86/kernel/machine_kexec_64.c | 160 +++++++------------------------------
 include/linux/kexec.h              |   9 ++-
 kernel/kexec_file.c                | 120 +++++++++++++++++++++++++++-
 3 files changed, 154 insertions(+), 135 deletions(-)
arch/x86/kernel/machine_kexec_64.c +31 -129
index 8c1f218926d7..f4860c408ece 100644
@@ -401,143 +401,45 @@ int arch_kexec_kernel_verify_sig(struct kimage *image, void *kernel,
 }
 #endif
 
-/*
- * Apply purgatory relocations.
- *
- * ehdr: Pointer to elf headers
- * sechdrs: Pointer to section headers.
- * relsec: section index of SHT_RELA section.
- *
- * TODO: Some of the code belongs to generic code. Move that in kexec.c.
- */
-int arch_kexec_apply_relocations_add(const Elf64_Ehdr *ehdr,
-				     Elf64_Shdr *sechdrs, unsigned int relsec)
+int arch_kexec_apply_relocation_add(const Elf_Ehdr *ehdr, Elf_Shdr *sechdrs,
+				    unsigned int reltype, Elf_Sym *sym,
+				    const char *name, unsigned long *location,
+				    unsigned long address, unsigned long value)
 {
-	unsigned int i;
-	Elf64_Rela *rel;
-	Elf64_Sym *sym;
-	void *location;
-	Elf64_Shdr *section, *symtabsec;
-	unsigned long address, sec_base, value;
-	const char *strtab, *name, *shstrtab;
-
-	/*
-	 * ->sh_offset has been modified to keep the pointer to section
-	 * contents in memory
-	 */
-	rel = (void *)sechdrs[relsec].sh_offset;
-
-	/* Section to which relocations apply */
-	section = &sechdrs[sechdrs[relsec].sh_info];
-
-	pr_debug("Applying relocate section %u to %u\n", relsec,
-		 sechdrs[relsec].sh_info);
-
-	/* Associated symbol table */
-	symtabsec = &sechdrs[sechdrs[relsec].sh_link];
-
-	/* String table */
-	if (symtabsec->sh_link >= ehdr->e_shnum) {
-		/* Invalid strtab section number */
-		pr_err("Invalid string table section index %d\n",
-		       symtabsec->sh_link);
+	if (sym->st_shndx == SHN_UNDEF) {
+		pr_err("Undefined symbol: %s\n", name);
 		return -ENOEXEC;
 	}
 
-	strtab = (char *)sechdrs[symtabsec->sh_link].sh_offset;
-
-	/* section header string table */
-	shstrtab = (char *)sechdrs[ehdr->e_shstrndx].sh_offset;
-
-	for (i = 0; i < sechdrs[relsec].sh_size / sizeof(*rel); i++) {
-
-		/*
-		 * rel[i].r_offset contains byte offset from beginning
-		 * of section to the storage unit affected.
-		 *
-		 * This is location to update (->sh_offset). This is temporary
-		 * buffer where section is currently loaded. This will finally
-		 * be loaded to a different address later, pointed to by
-		 * ->sh_addr. kexec takes care of moving it
-		 *  (kexec_load_segment()).
-		 */
-		location = (void *)(section->sh_offset + rel[i].r_offset);
-
-		/* Final address of the location */
-		address = section->sh_addr + rel[i].r_offset;
-
-		/*
-		 * rel[i].r_info contains information about symbol table index
-		 * w.r.t which relocation must be made and type of relocation
-		 * to apply. ELF64_R_SYM() and ELF64_R_TYPE() macros get
-		 * these respectively.
-		 */
-		sym = (Elf64_Sym *)symtabsec->sh_offset +
-				ELF64_R_SYM(rel[i].r_info);
-
-		if (sym->st_name)
-			name = strtab + sym->st_name;
-		else
-			name = shstrtab + sechdrs[sym->st_shndx].sh_name;
-
-		pr_debug("Symbol: %s info: %02x shndx: %02x value=%llx size: %llx\n",
-			 name, sym->st_info, sym->st_shndx, sym->st_value,
-			 sym->st_size);
-
-		if (sym->st_shndx == SHN_UNDEF) {
-			pr_err("Undefined symbol: %s\n", name);
-			return -ENOEXEC;
-		}
-
-		if (sym->st_shndx == SHN_COMMON) {
-			pr_err("symbol '%s' in common section\n", name);
-			return -ENOEXEC;
-		}
-
-		if (sym->st_shndx == SHN_ABS)
-			sec_base = 0;
-		else if (sym->st_shndx >= ehdr->e_shnum) {
-			pr_err("Invalid section %d for symbol %s\n",
-			       sym->st_shndx, name);
-			return -ENOEXEC;
-		} else
-			sec_base = sechdrs[sym->st_shndx].sh_addr;
-
-		value = sym->st_value;
-		value += sec_base;
-		value += rel[i].r_addend;
-
-		switch (ELF64_R_TYPE(rel[i].r_info)) {
-		case R_X86_64_NONE:
-			break;
-		case R_X86_64_64:
-			*(u64 *)location = value;
-			break;
-		case R_X86_64_32:
-			*(u32 *)location = value;
-			if (value != *(u32 *)location)
-				goto overflow;
-			break;
-		case R_X86_64_32S:
-			*(s32 *)location = value;
-			if ((s64)value != *(s32 *)location)
-				goto overflow;
-			break;
-		case R_X86_64_PC32:
-			value -= (u64)address;
-			*(u32 *)location = value;
-			break;
-		default:
-			pr_err("Unknown rela relocation: %llu\n",
-			       ELF64_R_TYPE(rel[i].r_info));
-			return -ENOEXEC;
-		}
+	switch (reltype) {
+	case R_X86_64_NONE:
+		break;
+	case R_X86_64_64:
+		*(u64 *)location = value;
+		break;
+	case R_X86_64_32:
+		*(u32 *)location = value;
+		if (value != *(u32 *)location)
+			goto overflow;
+		break;
+	case R_X86_64_32S:
+		*(s32 *)location = value;
+		if ((s64)value != *(s32 *)location)
+			goto overflow;
+		break;
+	case R_X86_64_PC32:
+		value -= (u64)address;
+		*(u32 *)location = value;
+		break;
+	default:
+		pr_err("Unknown rela relocation: %u\n", reltype);
+		return -ENOEXEC;
 	}
+
 	return 0;
 
 overflow:
-	pr_err("Overflow in relocation type %d value 0x%lx\n",
-	       (int)ELF64_R_TYPE(rel[i].r_info), value);
+	pr_err("Overflow in relocation type %u value 0x%lx\n", reltype, value);
 	return -ENOEXEC;
 }
 #endif /* CONFIG_KEXEC_FILE */
include/linux/kexec.h +7 -2
index 406c33dcae13..e171a083540d 100644
@@ -320,8 +320,13 @@ void * __weak arch_kexec_kernel_image_load(struct kimage *image);
 int __weak arch_kimage_file_post_load_cleanup(struct kimage *image);
 int __weak arch_kexec_kernel_verify_sig(struct kimage *image, void *buf,
 					unsigned long buf_len);
-int __weak arch_kexec_apply_relocations_add(const Elf_Ehdr *ehdr,
-					Elf_Shdr *sechdrs, unsigned int relsec);
+int __weak arch_kexec_apply_relocation_add(const Elf_Ehdr *ehdr,
+					   Elf_Shdr *sechdrs,
+					   unsigned int reltype, Elf_Sym *sym,
+					   const char *name,
+					   unsigned long *location,
+					   unsigned long address,
+					   unsigned long value);
 int __weak arch_kexec_apply_relocations(const Elf_Ehdr *ehdr, Elf_Shdr *sechdrs,
 					unsigned int relsec);
 void arch_kexec_protect_crashkres(void);
kernel/kexec_file.c +116 -4
index 037c321c5618..1517f977cc25 100644
@@ -61,8 +61,10 @@ int __weak arch_kexec_kernel_verify_sig(struct kimage *image, void *buf,
 
 /* Apply relocations of type RELA */
 int __weak
-arch_kexec_apply_relocations_add(const Elf_Ehdr *ehdr, Elf_Shdr *sechdrs,
-				 unsigned int relsec)
+arch_kexec_apply_relocation_add(const Elf_Ehdr *ehdr, Elf_Shdr *sechdrs,
+				unsigned int reltype, Elf_Sym *sym,
+				const char *name, unsigned long *location,
+				unsigned long address, unsigned long value)
 {
 	pr_err("RELA relocation unsupported.\n");
 	return -ENOEXEC;
@@ -793,6 +795,117 @@ static int __kexec_load_purgatory(struct kimage *image, unsigned long min,
 	return ret;
 }
 
+/**
+ * kexec_apply_relocations_add - apply purgatory relocations
+ * @ehdr:	Pointer to elf headers
+ * @sechdrs:	Pointer to section headers.
+ * @relsec:	Section index of SHT_RELA section.
+ */
+static int kexec_apply_relocations_add(const Elf64_Ehdr *ehdr,
+				       Elf64_Shdr *sechdrs, unsigned int relsec)
+{
+	int ret;
+	unsigned int i;
+	Elf64_Rela *rel;
+	Elf64_Sym *sym;
+	void *location;
+	Elf64_Shdr *section, *symtabsec;
+	unsigned long address, sec_base, value;
+	const char *strtab, *name, *shstrtab;
+
+	/*
+	 * ->sh_offset has been modified to keep the pointer to section
+	 * contents in memory
+	 */
+	rel = (void *)sechdrs[relsec].sh_offset;
+
+	/* Section to which relocations apply */
+	section = &sechdrs[sechdrs[relsec].sh_info];
+
+	pr_debug("Applying relocate section %u to %u\n", relsec,
+		 sechdrs[relsec].sh_info);
+
+	/* Associated symbol table */
+	symtabsec = &sechdrs[sechdrs[relsec].sh_link];
+
+	/* String table */
+	if (symtabsec->sh_link >= ehdr->e_shnum) {
+		/* Invalid strtab section number */
+		pr_err("Invalid string table section index %d\n",
+		       symtabsec->sh_link);
+		return -ENOEXEC;
+	}
+
+	/* String table for the associated symbol table. */
+	strtab = (char *)sechdrs[symtabsec->sh_link].sh_offset;
+
+	/* section header string table */
+	shstrtab = (char *)sechdrs[ehdr->e_shstrndx].sh_offset;
+
+	for (i = 0; i < sechdrs[relsec].sh_size / sizeof(*rel); i++) {
+
+		/*
+		 * rel[i].r_offset contains byte offset from beginning
+		 * of section to the storage unit affected.
+		 *
+		 * This is location to update (->sh_offset). This is temporary
+		 * buffer where section is currently loaded. This will finally
+		 * be loaded to a different address later, pointed to by
+		 * ->sh_addr. kexec takes care of moving it
+		 *  (kexec_load_segment()).
+		 */
+		location = (void *)(section->sh_offset + rel[i].r_offset);
+
+		/* Final address of the location */
+		address = section->sh_addr + rel[i].r_offset;
+
+		/*
+		 * rel[i].r_info contains information about symbol table index
+		 * w.r.t which relocation must be made and type of relocation
+		 * to apply. ELF64_R_SYM() and ELF64_R_TYPE() macros get
+		 * these respectively.
+		 */
+		sym = (Elf64_Sym *)symtabsec->sh_offset +
+				ELF64_R_SYM(rel[i].r_info);
+
+		if (sym->st_name)
+			name = strtab + sym->st_name;
+		else
+			name = shstrtab + sechdrs[sym->st_shndx].sh_name;
+
+		pr_debug("Symbol: %s info: %02x shndx: %02x value=%llx size: %llx\n",
+			 name, sym->st_info, sym->st_shndx, sym->st_value,
+			 sym->st_size);
+
+		if (sym->st_shndx == SHN_COMMON) {
+			pr_err("symbol '%s' in common section\n", name);
+			return -ENOEXEC;
+		}
+
+		if (sym->st_shndx == SHN_ABS)
+			sec_base = 0;
+		else if (sym->st_shndx >= ehdr->e_shnum) {
+			pr_err("Invalid section %d for symbol %s\n",
+			       sym->st_shndx, name);
+			return -ENOEXEC;
+		} else
+			sec_base = sechdrs[sym->st_shndx].sh_addr;
+
+		value = sym->st_value;
+		value += sec_base;
+		value += rel[i].r_addend;
+
+		ret = arch_kexec_apply_relocation_add(ehdr, sechdrs,
+						      ELF64_R_TYPE(rel[i].r_info),
+						      sym, name, location,
+						      address, value);
+		if (ret)
+			return ret;
+	}
+
+	return 0;
+}
+
 static int kexec_apply_relocations(struct kimage *image)
 {
 	int i, ret;
@@ -836,8 +949,7 @@ static int kexec_apply_relocations(struct kimage *image)
 		 * relocations of type SHT_RELA/SHT_REL.
 		 */
 		if (sechdrs[i].sh_type == SHT_RELA)
-			ret = arch_kexec_apply_relocations_add(pi->ehdr,
-							       sechdrs, i);
+			ret = kexec_apply_relocations_add(pi->ehdr, sechdrs, i);
 		else if (sechdrs[i].sh_type == SHT_REL)
 			ret = arch_kexec_apply_relocations(pi->ehdr,
 							   sechdrs, i);
Signature · 1 line
2.7.4
#8 lore raw

Re: [PATCH v10 04/10] kexec_file: Add support for purgatory built as PIE.

In reply to Thiago Jung Bauermann
Quoted 32 lines · Thiago Jung Bauermann

Am Sonntag, 20. November 2016, 10:45:46 BRST schrieb Dave Young:

Quoted 31 lines · Thiago Jung Bauermann
Quoted 27 lines

powerpc's purgatory.ro has 12 relocation types when built as a relocatable object. To implement support for them requires arch_kexec_apply_relocations_add to duplicate a lot of code with module_64.c:apply_relocate_add.

When built as a Position Independent Executable there are only 4 relocation types in purgatory.ro, so it becomes practical for the powerpc implementation of kexec_file to have its own relocation implementation.

Also, the purgatory is an executable and not an intermediary output from the compiler so it makes sense conceptually that it is easier to build it as a PIE than as a partially linked object.

Apart from the greatly reduced number of relocations, there are two differences between a relocatable object and a PIE:

1. __kexec_load_purgatory needs to use the program headers rather than the

section headers to figure out how to load the binary.

2. Symbol values are absolute addresses instead of relative to the

start of the section.

This patch adds the support needed in generic code for the differences above and allows powerpc to load and relocate a position independent purgatory.

[snip]

Quoted 8 lines
Quoted 3 lines

The kexec-tools machine_apply_elf_rel is pretty simple for ppc64, it is not that complex. So could you look into simplify your kexec_file implementation?

I can try, but there is one fundamental issue here: powerpc position-dependent code relies more on relocations than x86 position-dependent code does, so there's a limit to how simple it can be made without switching to position- independent code. And it will always be more involved than it is on x86.

I think we need to go back to the drawing board on this one.

My hope was that building purgatory as PIE would reduce the amount of complexity, but instead it's just added more. Sorry for sending you in that direction.

In general I dislike the level of complexity of the kexec-tools purgatory, and in particular I'm not comfortable with things like:

arch/powerpc/purgatory/sha256.c new file +6 -0
new file mode 100644
index 000000000000..6abee1877d56
@@ -0,0 +1,6 @@
+#include "../boot/string.h"
+
+/* Avoid including x86's boot/string.h in sha256.c. */
+#define BOOT_STRING_H
+
+#include "../../x86/purgatory/sha256.c"

I think the best way to get this over the line would be to take the kexec-lite purgatory implementation and use that to begin with. I know it doesn't have all the features of the kexec-tools version, but it should work, and we can look at adding the extra features later.

I'll try and get that working tonight.

cheers

#9 lore raw

Re: [PATCH v10 04/10] kexec_file: Add support for purgatory built as PIE.

Hi Michael

Quoted 59 lines · Michael Ellerman
Quoted 32 lines · Thiago Jung Bauermann

Am Sonntag, 20. November 2016, 10:45:46 BRST schrieb Dave Young:

Quoted 31 lines · Thiago Jung Bauermann
Quoted 27 lines

powerpc's purgatory.ro has 12 relocation types when built as a relocatable object. To implement support for them requires arch_kexec_apply_relocations_add to duplicate a lot of code with module_64.c:apply_relocate_add.

When built as a Position Independent Executable there are only 4 relocation types in purgatory.ro, so it becomes practical for the powerpc implementation of kexec_file to have its own relocation implementation.

Also, the purgatory is an executable and not an intermediary output from the compiler so it makes sense conceptually that it is easier to build it as a PIE than as a partially linked object.

Apart from the greatly reduced number of relocations, there are two differences between a relocatable object and a PIE:

1. __kexec_load_purgatory needs to use the program headers rather than the

section headers to figure out how to load the binary.

2. Symbol values are absolute addresses instead of relative to the

start of the section.

This patch adds the support needed in generic code for the differences above and allows powerpc to load and relocate a position independent purgatory.

[snip]

Quoted 8 lines
Quoted 3 lines

The kexec-tools machine_apply_elf_rel is pretty simple for ppc64, it is not that complex. So could you look into simplify your kexec_file implementation?

I can try, but there is one fundamental issue here: powerpc position-dependent code relies more on relocations than x86 position-dependent code does, so there's a limit to how simple it can be made without switching to position- independent code. And it will always be more involved than it is on x86.

I think we need to go back to the drawing board on this one.

My hope was that building purgatory as PIE would reduce the amount of complexity, but instead it's just added more. Sorry for sending you in that direction.

In general I dislike the level of complexity of the kexec-tools purgatory, and in particular I'm not comfortable with things like:

arch/powerpc/purgatory/sha256.c new file +0 -0
new file mode 100644
index 000000000000..6abee1877d56

arch/powerpc/purgatory/sha256.c line 6

Quoted 6 lines
arch/powerpc/purgatory/sha256.c new file +6 -0
@@ -0,0 +1,6 @@
+#include "../boot/string.h"
+
+/* Avoid including x86's boot/string.h in sha256.c. */
+#define BOOT_STRING_H
+
+#include "../../x86/purgatory/sha256.c"

Agreed, include x86 code in powerpc looks bad

Quoted 5 lines

I think the best way to get this over the line would be to take the kexec-lite purgatory implementation and use that to begin with. I know it doesn't have all the features of the kexec-tools version, but it should work, and we can look at adding the extra features later.

Instead of adding other implementation, moving the purgatory sha256 code out of x86 sounds better so that we can reuse them cleanly..

Quoted 4 lines

I'll try and get that working tonight.

cheers

Thanks
Dave

#10 lore raw

Re: [PATCH v10 04/10] kexec_file: Add support for purgatory built as PIE.

In reply to Michael Ellerman

Am Dienstag, 22. November 2016, 17:01:10 BRST schrieb Michael Ellerman:

Quoted 45 lines
Quoted 39 lines · Thiago Jung Bauermann

Am Sonntag, 20. November 2016, 10:45:46 BRST schrieb Dave Young:

Quoted 32 lines · Thiago Jung Bauermann
Quoted 23 lines

powerpc's purgatory.ro has 12 relocation types when built as a relocatable object. To implement support for them requires arch_kexec_apply_relocations_add to duplicate a lot of code with module_64.c:apply_relocate_add.

When built as a Position Independent Executable there are only 4 relocation types in purgatory.ro, so it becomes practical for the powerpc
implementation of kexec_file to have its own relocation implementation.

Also, the purgatory is an executable and not an intermediary output from
the compiler so it makes sense conceptually that it is easier to build it as a PIE than as a partially linked object.

Apart from the greatly reduced number of relocations, there are two differences between a relocatable object and a PIE:

1. __kexec_load_purgatory needs to use the program headers rather than the

section headers to figure out how to load the binary.

Quoted 7 lines

2. Symbol values are absolute addresses instead of relative to the

start of the section.

This patch adds the support needed in generic code for the differences above and allows powerpc to load and relocate a position independent purgatory.

Quoted 5 lines

[snip]

The kexec-tools machine_apply_elf_rel is pretty simple for ppc64, it is not that complex. So could you look into simplify your kexec_file implementation?

Quoted 5 lines

I can try, but there is one fundamental issue here: powerpc position-dependent code relies more on relocations than x86 position-dependent code does, so there's a limit to how simple it can be made without switching to position- independent code. And it will always be more involved than it is on x86.

Quoted 5 lines

I think we need to go back to the drawing board on this one.

My hope was that building purgatory as PIE would reduce the amount of complexity, but instead it's just added more. Sorry for sending you in that direction.

It added complexity because in my series powerpc was using a PIE purgatory but x86 kept using a partially-linked object (because of the problem I mentioned I had when trying out a PIE x86 purgatory), so generic code needed two purgatory loaders.

I'll see if I can make the PIE x86 purgatory to work so that generic code can have only one loader implementation. Then it will indeed be simpler.

Am Dienstag, 22. November 2016, 14:16:22 BRST schrieb Dave Young:

Quoted 21 lines

Hi Michael

Quoted 9 lines · Michael Ellerman

In general I dislike the level of complexity of the kexec-tools purgatory, and in particular I'm not comfortable with things like:

(quoted diff) +0 -0

b/arch/powerpc/purgatory/sha256.c new file mode 100644 index 000000000000..6abee1877d56

arch/powerpc/purgatory/sha256.c line 6

Quoted 6 lines
arch/powerpc/purgatory/sha256.c new file +6 -0
@@ -0,0 +1,6 @@
+#include "../boot/string.h"
+
+/* Avoid including x86's boot/string.h in sha256.c. */
+#define BOOT_STRING_H
+
+#include "../../x86/purgatory/sha256.c"

Agreed, include x86 code in powerpc looks bad

Quoted 7 lines
Quoted 4 lines

I think the best way to get this over the line would be to take the kexec-lite purgatory implementation and use that to begin with. I know it doesn't have all the features of the kexec-tools version, but it should work, and we can look at adding the extra features later.

Instead of adding other implementation, moving the purgatory sha256 code out of x86 sounds better so that we can reuse them cleanly..

Do you have a suggestion of where that code can live so that it can be shared between purgatories for different arches?

Do we need a purgatory with generic and arch-specific code like in kexec- tools?

Signature · 2 lines
Thiago Jung Bauermann
IBM Linux Technology Center
#11 lore raw

Re: [PATCH v10 04/10] kexec_file: Add support for purgatory built as PIE.

Quoted 52 lines · Thiago Jung Bauermann

Am Dienstag, 22. November 2016, 17:01:10 BRST schrieb Michael Ellerman:

Quoted 45 lines
Quoted 39 lines · Thiago Jung Bauermann

Am Sonntag, 20. November 2016, 10:45:46 BRST schrieb Dave Young:

Quoted 32 lines · Thiago Jung Bauermann
Quoted 23 lines

powerpc's purgatory.ro has 12 relocation types when built as a relocatable object. To implement support for them requires arch_kexec_apply_relocations_add to duplicate a lot of code with module_64.c:apply_relocate_add.

When built as a Position Independent Executable there are only 4 relocation types in purgatory.ro, so it becomes practical for the powerpc
implementation of kexec_file to have its own relocation implementation.

Also, the purgatory is an executable and not an intermediary output from
the compiler so it makes sense conceptually that it is easier to build it as a PIE than as a partially linked object.

Apart from the greatly reduced number of relocations, there are two differences between a relocatable object and a PIE:

1. __kexec_load_purgatory needs to use the program headers rather than the

section headers to figure out how to load the binary.

Quoted 7 lines

2. Symbol values are absolute addresses instead of relative to the

start of the section.

This patch adds the support needed in generic code for the differences above and allows powerpc to load and relocate a position independent purgatory.

Quoted 5 lines

[snip]

The kexec-tools machine_apply_elf_rel is pretty simple for ppc64, it is not that complex. So could you look into simplify your kexec_file implementation?

Quoted 5 lines

I can try, but there is one fundamental issue here: powerpc position-dependent code relies more on relocations than x86 position-dependent code does, so there's a limit to how simple it can be made without switching to position- independent code. And it will always be more involved than it is on x86.

Quoted 5 lines

I think we need to go back to the drawing board on this one.

My hope was that building purgatory as PIE would reduce the amount of complexity, but instead it's just added more. Sorry for sending you in that direction.

Quoted 7 lines

It added complexity because in my series powerpc was using a PIE purgatory but x86 kept using a partially-linked object (because of the problem I mentioned I had when trying out a PIE x86 purgatory), so generic code needed two purgatory loaders.

I'll see if I can make the PIE x86 purgatory to work so that generic code can have only one loader implementation. Then it will indeed be simpler.

Do we really need the PIE purgatory, after moving generic code out of x86, there will be no much benefit, no? Anyway, the first step should be making the purgatory code more generic so that it can be easier for other arches to support kexec_file in the future.

Quoted 29 lines

Am Dienstag, 22. November 2016, 14:16:22 BRST schrieb Dave Young:

Quoted 26 lines

Hi Michael

Quoted 9 lines · Michael Ellerman

In general I dislike the level of complexity of the kexec-tools purgatory, and in particular I'm not comfortable with things like:

(quoted diff) +0 -0

b/arch/powerpc/purgatory/sha256.c new file mode 100644 index 000000000000..6abee1877d56

arch/powerpc/purgatory/sha256.c line 6

Quoted 6 lines
arch/powerpc/purgatory/sha256.c new file +6 -0
@@ -0,0 +1,6 @@
+#include "../boot/string.h"
+
+/* Avoid including x86's boot/string.h in sha256.c. */
+#define BOOT_STRING_H
+
+#include "../../x86/purgatory/sha256.c"

Agreed, include x86 code in powerpc looks bad

Quoted 4 lines

I think the best way to get this over the line would be to take the kexec-lite purgatory implementation and use that to begin with. I know it doesn't have all the features of the kexec-tools version, but it should work, and we can look at adding the extra features later.

Quoted 5 lines
Quoted 2 lines

Instead of adding other implementation, moving the purgatory sha256 code out of x86 sounds better so that we can reuse them cleanly..

Do you have a suggestion of where that code can live so that it can be shared between purgatories for different arches?

Maybe it is better to stay in lib/purgatory/

Quoted 3 lines

Do we need a purgatory with generic and arch-specific code like in kexec- tools?

Yes, if we have more arches to add kexec_file, this should be necessary..

Quoted 5 lines
Signature · 3 lines
-- 
Thiago Jung Bauermann
IBM Linux Technology Center

Thanks
Dave

#12 lore raw

Re: [PATCH v10 04/10] kexec_file: Add support for purgatory built as PIE.

Am Mittwoch, 23. November 2016, 09:32:58 BRST schrieb Dave Young:

Quoted 67 lines · Thiago Jung Bauermann
Quoted 66 lines

Am Dienstag, 22. November 2016, 17:01:10 BRST schrieb Michael Ellerman:

Quoted 54 lines
Quoted 45 lines · Thiago Jung Bauermann

Am Sonntag, 20. November 2016, 10:45:46 BRST schrieb Dave Young:

Quoted 37 lines · Thiago Jung Bauermann
Quoted 28 lines

powerpc's purgatory.ro has 12 relocation types when built as a relocatable object. To implement support for them requires arch_kexec_apply_relocations_add to duplicate a lot of code with module_64.c:apply_relocate_add.

When built as a Position Independent Executable there are only 4 relocation types in purgatory.ro, so it becomes practical for the powerpc
implementation of kexec_file to have its own relocation implementation.

Also, the purgatory is an executable and not an intermediary output from
the compiler so it makes sense conceptually that it is easier to build
it as a PIE than as a partially linked object.

Apart from the greatly reduced number of relocations, there are two differences between a relocatable object and a PIE:

1. __kexec_load_purgatory needs to use the program headers rather than
the

section headers to figure out how to load the binary.

2. Symbol values are absolute addresses instead of relative to the

Quoted 7 lines

start of the section.

This patch adds the support needed in generic code for the differences
above and allows powerpc to load and relocate a position independent
purgatory.

Quoted 6 lines

[snip]

The kexec-tools machine_apply_elf_rel is pretty simple for ppc64, it is
not that complex. So could you look into simplify your kexec_file implementation?

Quoted 7 lines

I can try, but there is one fundamental issue here: powerpc position-dependent code relies more on relocations than x86 position-dependent code does, so there's a limit to how simple it can be
made without switching to position- independent code. And it will always
be more involved than it is on x86.

Quoted 5 lines

I think we need to go back to the drawing board on this one.

My hope was that building purgatory as PIE would reduce the amount of complexity, but instead it's just added more. Sorry for sending you in that direction.

It added complexity because in my series powerpc was using a PIE purgatory but x86 kept using a partially-linked object (because of the problem I mentioned I had when trying out a PIE x86 purgatory), so generic code needed two purgatory loaders.

Quoted 4 lines
Quoted 2 lines

I'll see if I can make the PIE x86 purgatory to work so that generic code can have only one loader implementation. Then it will indeed be simpler.

Do we really need the PIE purgatory, after moving generic code out of x86, there will be no much benefit, no?

It still makes a big difference on powerpc, even after moving out the generic code. I just got the PIE purgatory working on x86 and it also simplifies the code there, so it's a win for both architectures.

I'll clean up the code and post tomorrow so that you can see what you think.

Quoted 3 lines

Anyway, the first step should be
making the purgatory code more generic so that it can be easier for other arches to support kexec_file in the future.

I'll try putting sha256.c in lib/purgatory/ as you suggested.

Signature · 2 lines
Thiago Jung Bauermann
IBM Linux Technology Center
#13 lore raw

Re: [PATCH v10 04/10] kexec_file: Add support for purgatory built as PIE.

In reply to Thiago Jung Bauermann
Quoted 424 lines · Thiago Jung Bauermann

Hello Dave,

Thanks for your review.

Am Sonntag, 20. November 2016, 10:45:46 BRST schrieb Dave Young:

Quoted 29 lines · Thiago Jung Bauermann
Quoted 20 lines

powerpc's purgatory.ro has 12 relocation types when built as a relocatable object. To implement support for them requires arch_kexec_apply_relocations_add to duplicate a lot of code with module_64.c:apply_relocate_add.

When built as a Position Independent Executable there are only 4 relocation types in purgatory.ro, so it becomes practical for the powerpc implementation of kexec_file to have its own relocation implementation.

Also, the purgatory is an executable and not an intermediary output from the compiler so it makes sense conceptually that it is easier to build it as a PIE than as a partially linked object.

Apart from the greatly reduced number of relocations, there are two differences between a relocatable object and a PIE:

1. __kexec_load_purgatory needs to use the program headers rather than the

section headers to figure out how to load the binary.

Quoted 7 lines

2. Symbol values are absolute addresses instead of relative to the

start of the section.

This patch adds the support needed in generic code for the differences above and allows powerpc to load and relocate a position independent purgatory.

Quoted 5 lines

[snip]

The kexec-tools machine_apply_elf_rel is pretty simple for ppc64, it is not that complex. So could you look into simplify your kexec_file implementation?

I can try, but there is one fundamental issue here: powerpc position-dependent code relies more on relocations than x86 position-dependent code does, so there's a limit to how simple it can be made without switching to position- independent code. And it will always be more involved than it is on x86.

BTW, building x86's purgatory as PIE results in it not having any relocation at all, so it's an advantage even in that architecture. Unfortunately, the machine locks up during reboot and I didn't have time to try to figure out what's going on.

Quoted 3 lines

kernel/kexec_file.c kexec_apply_relocations only do limited things and some of the logic is in arch/x86, so move general code out of arch code, then I guess the arch code will be simpler

I agree that is a good idea. Is the patch below what you had in mind?

Quoted 1 line

and then we probably do not need this PIE stuff anymore.

If you are ok with the patch below I can post a new version of the series based on it and we can see if Michael Ellerman thinks it is enough.

Quoted 2 lines

BTW, __kexec_really_load_purgatory looks worse than ___kexec_load_purgatory ;)

Really? I find the special handling of bss makes the section-based loader a bit more confusing.

Signature · 20 lines
Thiago Jung Bauermann
IBM Linux Technology Center


Subject: [PATCH] kexec_file: Move generic relocation code from arch/x86 to
 kernel/kexec_file.c

The check for undefined symbols stays in arch-specific code because
powerpc needs to allow TOC symbols to be processed even though they're
undefined.

There is no functional change.

Suggested-by: Dave Young <[email protected]>
Signed-off-by: Thiago Jung Bauermann <[email protected]>
---
 arch/x86/kernel/machine_kexec_64.c | 160 +++++++------------------------------
 include/linux/kexec.h              |   9 ++-
 kernel/kexec_file.c                | 120 +++++++++++++++++++++++++++-
 3 files changed, 154 insertions(+), 135 deletions(-)
arch/x86/kernel/machine_kexec_64.c +31 -129
index 8c1f218926d7..f4860c408ece 100644
@@ -401,143 +401,45 @@ int arch_kexec_kernel_verify_sig(struct kimage *image, void *kernel,
 }
 #endif
 
-/*
- * Apply purgatory relocations.
- *
- * ehdr: Pointer to elf headers
- * sechdrs: Pointer to section headers.
- * relsec: section index of SHT_RELA section.
- *
- * TODO: Some of the code belongs to generic code. Move that in kexec.c.
- */
-int arch_kexec_apply_relocations_add(const Elf64_Ehdr *ehdr,
-				     Elf64_Shdr *sechdrs, unsigned int relsec)
+int arch_kexec_apply_relocation_add(const Elf_Ehdr *ehdr, Elf_Shdr *sechdrs,
+				    unsigned int reltype, Elf_Sym *sym,
+				    const char *name, unsigned long *location,
+				    unsigned long address, unsigned long value)
 {
-	unsigned int i;
-	Elf64_Rela *rel;
-	Elf64_Sym *sym;
-	void *location;
-	Elf64_Shdr *section, *symtabsec;
-	unsigned long address, sec_base, value;
-	const char *strtab, *name, *shstrtab;
-
-	/*
-	 * ->sh_offset has been modified to keep the pointer to section
-	 * contents in memory
-	 */
-	rel = (void *)sechdrs[relsec].sh_offset;
-
-	/* Section to which relocations apply */
-	section = &sechdrs[sechdrs[relsec].sh_info];
-
-	pr_debug("Applying relocate section %u to %u\n", relsec,
-		 sechdrs[relsec].sh_info);
-
-	/* Associated symbol table */
-	symtabsec = &sechdrs[sechdrs[relsec].sh_link];
-
-	/* String table */
-	if (symtabsec->sh_link >= ehdr->e_shnum) {
-		/* Invalid strtab section number */
-		pr_err("Invalid string table section index %d\n",
-		       symtabsec->sh_link);
+	if (sym->st_shndx == SHN_UNDEF) {
+		pr_err("Undefined symbol: %s\n", name);
 		return -ENOEXEC;
 	}
 
-	strtab = (char *)sechdrs[symtabsec->sh_link].sh_offset;
-
-	/* section header string table */
-	shstrtab = (char *)sechdrs[ehdr->e_shstrndx].sh_offset;
-
-	for (i = 0; i < sechdrs[relsec].sh_size / sizeof(*rel); i++) {
-
-		/*
-		 * rel[i].r_offset contains byte offset from beginning
-		 * of section to the storage unit affected.
-		 *
-		 * This is location to update (->sh_offset). This is temporary
-		 * buffer where section is currently loaded. This will finally
-		 * be loaded to a different address later, pointed to by
-		 * ->sh_addr. kexec takes care of moving it
-		 *  (kexec_load_segment()).
-		 */
-		location = (void *)(section->sh_offset + rel[i].r_offset);
-
-		/* Final address of the location */
-		address = section->sh_addr + rel[i].r_offset;
-
-		/*
-		 * rel[i].r_info contains information about symbol table index
-		 * w.r.t which relocation must be made and type of relocation
-		 * to apply. ELF64_R_SYM() and ELF64_R_TYPE() macros get
-		 * these respectively.
-		 */
-		sym = (Elf64_Sym *)symtabsec->sh_offset +
-				ELF64_R_SYM(rel[i].r_info);
-
-		if (sym->st_name)
-			name = strtab + sym->st_name;
-		else
-			name = shstrtab + sechdrs[sym->st_shndx].sh_name;
-
-		pr_debug("Symbol: %s info: %02x shndx: %02x value=%llx size: %llx\n",
-			 name, sym->st_info, sym->st_shndx, sym->st_value,
-			 sym->st_size);
-
-		if (sym->st_shndx == SHN_UNDEF) {
-			pr_err("Undefined symbol: %s\n", name);
-			return -ENOEXEC;
-		}
-
-		if (sym->st_shndx == SHN_COMMON) {
-			pr_err("symbol '%s' in common section\n", name);
-			return -ENOEXEC;
-		}
-
-		if (sym->st_shndx == SHN_ABS)
-			sec_base = 0;
-		else if (sym->st_shndx >= ehdr->e_shnum) {
-			pr_err("Invalid section %d for symbol %s\n",
-			       sym->st_shndx, name);
-			return -ENOEXEC;
-		} else
-			sec_base = sechdrs[sym->st_shndx].sh_addr;
-
-		value = sym->st_value;
-		value += sec_base;
-		value += rel[i].r_addend;
-
-		switch (ELF64_R_TYPE(rel[i].r_info)) {
-		case R_X86_64_NONE:
-			break;
-		case R_X86_64_64:
-			*(u64 *)location = value;
-			break;
-		case R_X86_64_32:
-			*(u32 *)location = value;
-			if (value != *(u32 *)location)
-				goto overflow;
-			break;
-		case R_X86_64_32S:
-			*(s32 *)location = value;
-			if ((s64)value != *(s32 *)location)
-				goto overflow;
-			break;
-		case R_X86_64_PC32:
-			value -= (u64)address;
-			*(u32 *)location = value;
-			break;
-		default:
-			pr_err("Unknown rela relocation: %llu\n",
-			       ELF64_R_TYPE(rel[i].r_info));
-			return -ENOEXEC;
-		}
+	switch (reltype) {
+	case R_X86_64_NONE:
+		break;
+	case R_X86_64_64:
+		*(u64 *)location = value;
+		break;
+	case R_X86_64_32:
+		*(u32 *)location = value;
+		if (value != *(u32 *)location)
+			goto overflow;
+		break;
+	case R_X86_64_32S:
+		*(s32 *)location = value;
+		if ((s64)value != *(s32 *)location)
+			goto overflow;
+		break;
+	case R_X86_64_PC32:
+		value -= (u64)address;
+		*(u32 *)location = value;
+		break;
+	default:
+		pr_err("Unknown rela relocation: %u\n", reltype);
+		return -ENOEXEC;
 	}
+
 	return 0;
 
 overflow:
-	pr_err("Overflow in relocation type %d value 0x%lx\n",
-	       (int)ELF64_R_TYPE(rel[i].r_info), value);
+	pr_err("Overflow in relocation type %u value 0x%lx\n", reltype, value);
 	return -ENOEXEC;
 }
 #endif /* CONFIG_KEXEC_FILE */
include/linux/kexec.h +7 -2
index 406c33dcae13..e171a083540d 100644
@@ -320,8 +320,13 @@ void * __weak arch_kexec_kernel_image_load(struct kimage *image);
 int __weak arch_kimage_file_post_load_cleanup(struct kimage *image);
 int __weak arch_kexec_kernel_verify_sig(struct kimage *image, void *buf,
 					unsigned long buf_len);
-int __weak arch_kexec_apply_relocations_add(const Elf_Ehdr *ehdr,
-					Elf_Shdr *sechdrs, unsigned int relsec);
+int __weak arch_kexec_apply_relocation_add(const Elf_Ehdr *ehdr,
+					   Elf_Shdr *sechdrs,
+					   unsigned int reltype, Elf_Sym *sym,
+					   const char *name,
+					   unsigned long *location,
+					   unsigned long address,
+					   unsigned long value);
 int __weak arch_kexec_apply_relocations(const Elf_Ehdr *ehdr, Elf_Shdr *sechdrs,
 					unsigned int relsec);
 void arch_kexec_protect_crashkres(void);
kernel/kexec_file.c +115 -2
index 037c321c5618..1517f977cc25 100644
@@ -61,8 +61,10 @@ int __weak arch_kexec_kernel_verify_sig(struct kimage *image, void *buf,
 
 /* Apply relocations of type RELA */
 int __weak
-arch_kexec_apply_relocations_add(const Elf_Ehdr *ehdr, Elf_Shdr *sechdrs,
-				 unsigned int relsec)
+arch_kexec_apply_relocation_add(const Elf_Ehdr *ehdr, Elf_Shdr *sechdrs,
+				unsigned int reltype, Elf_Sym *sym,
+				const char *name, unsigned long *location,
+				unsigned long address, unsigned long value)
 {
 	pr_err("RELA relocation unsupported.\n");
 	return -ENOEXEC;
@@ -793,6 +795,117 @@ static int __kexec_load_purgatory(struct kimage *image, unsigned long min,
 	return ret;
 }
 
+/**
+ * kexec_apply_relocations_add - apply purgatory relocations
+ * @ehdr:	Pointer to elf headers
+ * @sechdrs:	Pointer to section headers.
+ * @relsec:	Section index of SHT_RELA section.
+ */
+static int kexec_apply_relocations_add(const Elf64_Ehdr *ehdr,
+				       Elf64_Shdr *sechdrs, unsigned int relsec)
+{
+	int ret;
+	unsigned int i;
+	Elf64_Rela *rel;
+	Elf64_Sym *sym;
+	void *location;
+	Elf64_Shdr *section, *symtabsec;
+	unsigned long address, sec_base, value;
+	const char *strtab, *name, *shstrtab;
+
+	/*
+	 * ->sh_offset has been modified to keep the pointer to section
+	 * contents in memory
+	 */
+	rel = (void *)sechdrs[relsec].sh_offset;
+
+	/* Section to which relocations apply */
+	section = &sechdrs[sechdrs[relsec].sh_info];
+
+	pr_debug("Applying relocate section %u to %u\n", relsec,
+		 sechdrs[relsec].sh_info);
+
+	/* Associated symbol table */
+	symtabsec = &sechdrs[sechdrs[relsec].sh_link];
+
+	/* String table */
+	if (symtabsec->sh_link >= ehdr->e_shnum) {
+		/* Invalid strtab section number */
+		pr_err("Invalid string table section index %d\n",
+		       symtabsec->sh_link);
+		return -ENOEXEC;
+	}
+
+	/* String table for the associated symbol table. */
+	strtab = (char *)sechdrs[symtabsec->sh_link].sh_offset;
+
+	/* section header string table */
+	shstrtab = (char *)sechdrs[ehdr->e_shstrndx].sh_offset;
+
+	for (i = 0; i < sechdrs[relsec].sh_size / sizeof(*rel); i++) {
+
+		/*
+		 * rel[i].r_offset contains byte offset from beginning
+		 * of section to the storage unit affected.
+		 *
+		 * This is location to update (->sh_offset). This is temporary
+		 * buffer where section is currently loaded. This will finally
+		 * be loaded to a different address later, pointed to by
+		 * ->sh_addr. kexec takes care of moving it
+		 *  (kexec_load_segment()).
+		 */
+		location = (void *)(section->sh_offset + rel[i].r_offset);
+
+		/* Final address of the location */
+		address = section->sh_addr + rel[i].r_offset;
+
+		/*
+		 * rel[i].r_info contains information about symbol table index
+		 * w.r.t which relocation must be made and type of relocation
+		 * to apply. ELF64_R_SYM() and ELF64_R_TYPE() macros get
+		 * these respectively.
+		 */
+		sym = (Elf64_Sym *)symtabsec->sh_offset +
+				ELF64_R_SYM(rel[i].r_info);
+
+		if (sym->st_name)
+			name = strtab + sym->st_name;
+		else
+			name = shstrtab + sechdrs[sym->st_shndx].sh_name;
+
+		pr_debug("Symbol: %s info: %02x shndx: %02x value=%llx size: %llx\n",
+			 name, sym->st_info, sym->st_shndx, sym->st_value,
+			 sym->st_size);
+
+		if (sym->st_shndx == SHN_COMMON) {
+			pr_err("symbol '%s' in common section\n", name);
+			return -ENOEXEC;
+		}
+
+		if (sym->st_shndx == SHN_ABS)
+			sec_base = 0;
+		else if (sym->st_shndx >= ehdr->e_shnum) {
+			pr_err("Invalid section %d for symbol %s\n",
+			       sym->st_shndx, name);
+			return -ENOEXEC;
+		} else
+			sec_base = sechdrs[sym->st_shndx].sh_addr;
+
+		value = sym->st_value;
+		value += sec_base;
+		value += rel[i].r_addend;
+
+		ret = arch_kexec_apply_relocation_add(ehdr, sechdrs,
+						      ELF64_R_TYPE(rel[i].r_info),
+						      sym, name, location,
+						      address, value);
+		if (ret)
+			return ret;
+	}
+
+	return 0;
+}
+
 static int kexec_apply_relocations(struct kimage *image)
 {
 	int i, ret;

kernel/kexec_file.c static int kexec_apply_relocations(struct kimage *image) · line 955

Quoted 9 lines
kernel/kexec_file.c +1 -2
@@ -836,8 +949,7 @@ static int kexec_apply_relocations(struct kimage *image)
 		 * relocations of type SHT_RELA/SHT_REL.
 		 */
 		if (sechdrs[i].sh_type == SHT_RELA)
-			ret = arch_kexec_apply_relocations_add(pi->ehdr,
-							       sechdrs, i);
+			ret = kexec_apply_relocations_add(pi->ehdr, sechdrs, i);
 		else if (sechdrs[i].sh_type == SHT_REL)
 			ret = arch_kexec_apply_relocations(pi->ehdr,
 							   sechdrs, i);

Hi,

As we are here, two weak functions looks not necessary, let arch code to determin if SHT_REL can be handled is reasonable though I'm not sure why there is not such things in kexec-tools.

Could you just use arch_kexec_apply_relocations for these two types instead of adding another?

Thanks
Dave