Skip to contentSkip to conversation

kexec_load: Use new kexec flag for hotplug support

PATCH Superseded Sourabh Jain 2 participants 7 messages Jul 2, 2024 – Jul 3, 2024

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

PATCH v3 1/3 kexec_load: Use new kexec flag for hotplug support

Kernel commit 79365026f869 (crash: add a new kexec flag for hotplug support) has introduced a new kexec flag to generalize hotplug support. The newly introduced kexec flags for hotplug allow architectures to exclude all the required kexec segments from SHA calculation so that the kernel can update them on hotplug events. This was not possible earlier with the KEXEC_UPDATE_ELFCOREHDR kexec flags since it was added only for the elfcorehdr segment.

To enable architectures to control the list of kexec segments to exclude when hotplug support is enabled, add a new architecture-specific function named arch_do_exclude_segment. During the SHA calculation, this function gets called to let the architecture decide whether a specific kexec segment should be considered for SHA calculation or not.

Given that the KEXEC_UPDATE_ELFCOREHDR is no longer required and was colliding with the KEXEC_LIVE_UPDATE update flag, it is removed.

Cc Aditya Gupta <[email protected]>
Cc Baoquan He <[email protected]>
Cc Coiby Xu <[email protected]>
Cc Mahesh Salgaonkar <[email protected]>
Cc Simon Horman <[email protected]>

---

* No changes in v2.

---
 kexec/arch/arm/kexec-arm.c             |  5 +++++
 kexec/arch/arm64/kexec-arm64.c         |  4 ++++
 kexec/arch/cris/kexec-cris.c           |  4 ++++
 kexec/arch/hppa/kexec-hppa.c           |  5 +++++
 kexec/arch/i386/kexec-x86.c            |  8 ++++++++
 kexec/arch/ia64/kexec-ia64.c           |  4 ++++
 kexec/arch/loongarch/kexec-loongarch.c |  5 +++++
 kexec/arch/m68k/kexec-m68k.c           |  5 +++++
 kexec/arch/mips/kexec-mips.c           |  4 ++++
 kexec/arch/ppc/kexec-ppc.c             |  4 ++++
 kexec/arch/ppc64/kexec-ppc64.c         |  5 +++++
 kexec/arch/s390/kexec-s390.c           |  5 +++++
 kexec/arch/sh/kexec-sh.c               |  5 +++++
 kexec/arch/x86_64/kexec-x86_64.c       |  5 +++++
 kexec/kexec-syscall.h                  |  2 +-
 kexec/kexec.c                          | 14 ++++++++------
 kexec/kexec.h                          |  2 ++
 17 files changed, 79 insertions(+), 7 deletions(-)
kexec/arch/arm/kexec-arm.c +5 -0
index 49f35b1..34531f9 100644
@@ -148,3 +148,8 @@ int have_sysfs_fdt(void)
 {
 	return !access(SYSFS_FDT, F_OK);
 }
+
+int arch_do_exclude_segment(struct kexec_segment *UNUSED(seg_ptr), struct kexec_info *UNUSED(info))
+{
+	return 0;
+}
kexec/arch/arm64/kexec-arm64.c +4 -0
index 4a67b0d..9d052b0 100644
@@ -1363,3 +1363,7 @@ void arch_reuse_initrd(void)
 void arch_update_purgatory(struct kexec_info *UNUSED(info))
 {
 }
+int arch_do_exclude_segment(struct kexec_segment *UNUSED(seg_ptr), struct kexec_info *UNUSED(info))
+{
+	return 0;
+}
kexec/arch/cris/kexec-cris.c +4 -0
index 3b69709..7f09121 100644
@@ -109,3 +109,7 @@ unsigned long add_buffer(struct kexec_info *info, const void *buf,
                                     buf_min, buf_max, buf_end, 1);
 }
 
+int arch_do_exclude_segment(struct kexec_segment *UNUSED(seg_ptr), struct kexec_info *UNUSED(info))
+{
+	return 0;
+}
kexec/arch/hppa/kexec-hppa.c +5 -0
index 77c9739..a64dc3d 100644
@@ -146,3 +146,8 @@ unsigned long virt_to_phys(unsigned long addr)
 {
 	return addr - phys_offset;
 }
+
+int arch_do_exclude_segment(struct kexec_segment *UNUSED(seg_ptr), struct kexec_info *UNUSED(info))
+{
+	return 0;
+}
kexec/arch/i386/kexec-x86.c +8 -0
index 444cb69..b4947a0 100644
@@ -208,3 +208,11 @@ void arch_update_purgatory(struct kexec_info *info)
 	elf_rel_set_symbol(&info->rhdr, "panic_kernel",
 		&panic_kernel, sizeof(panic_kernel));
 }
+
+int arch_do_exclude_segment(struct kexec_segment *seg_ptr, struct kexec_info *info)
+{
+	if (info->elfcorehdr == (unsigned long) seg_ptr->mem)
+		return 1;
+
+	return 0;
+}
kexec/arch/ia64/kexec-ia64.c +4 -0
index 418d997..8d9c1f3 100644
@@ -245,3 +245,7 @@ void arch_update_purgatory(struct kexec_info *UNUSED(info))
 {
 }
 
+int arch_do_exclude_segment(struct kexec_segment *UNUSED(seg_ptr), struct kexec_info *UNUSED(info))
+{
+	return 0;
+}
kexec/arch/loongarch/kexec-loongarch.c +5 -0
index ac75030..ee7b9f1 100644
@@ -381,3 +381,8 @@ unsigned long add_buffer(struct kexec_info *info, const void *buf,
 	return add_buffer_phys_virt(info, buf, bufsz, memsz, buf_align,
 				    buf_min, buf_max, buf_end, 1);
 }
+
+int arch_do_exclude_segment(struct kexec_segment *UNUSED(seg_ptr), struct kexec_info *UNUSED(info))
+{
+	return 0;
+}
kexec/arch/m68k/kexec-m68k.c +5 -0
index cb54927..0c7dbaf 100644
@@ -108,3 +108,8 @@ void add_segment(struct kexec_info *info, const void *buf, size_t bufsz,
 {
 	add_segment_phys_virt(info, buf, bufsz, base, memsz, 1);
 }
+
+int arch_do_exclude_segment(struct kexec_segment *UNUSED(seg_ptr), struct kexec_info *UNUSED(info))
+{
+	return 0;
+}
kexec/arch/mips/kexec-mips.c +4 -0
index d8cbea8..94224ee 100644
@@ -189,3 +189,7 @@ unsigned long add_buffer(struct kexec_info *info, const void *buf,
 				    buf_min, buf_max, buf_end, 1);
 }
 
+int arch_do_exclude_segment(const void *UNUSED(seg_ptr), struct kexec_info *UNUSED(info))
+{
+	return 0;
+}
kexec/arch/ppc/kexec-ppc.c +4 -0
index 03bec36..c8af870 100644
@@ -966,3 +966,7 @@ void arch_update_purgatory(struct kexec_info *UNUSED(info))
 {
 }
 
+int arch_do_exclude_segment(struct kexec_segment *UNUSED(seg_ptr), struct kexec_info *UNUSED(info))
+{
+	return 0;
+}
kexec/arch/ppc64/kexec-ppc64.c +5 -0
index bd5274c..fb27b6b 100644
@@ -967,3 +967,8 @@ int arch_compat_trampoline(struct kexec_info *UNUSED(info))
 void arch_update_purgatory(struct kexec_info *UNUSED(info))
 {
 }
+
+int arch_do_exclude_segment(struct kexec_segment *UNUSED(seg_ptr), struct kexec_info *UNUSED(info))
+{
+	return 0;
+}
kexec/arch/s390/kexec-s390.c +5 -0
index 33ba6b9..0561ee7 100644
@@ -267,3 +267,8 @@ int get_crash_kernel_load_range(uint64_t *start, uint64_t *end)
 {
 	return parse_iomem_single("Crash kernel\n", start, end);
 }
+
+int arch_do_exclude_segment(struct kexec_segment *UNUSED(seg_ptr), struct kexec_info *UNUSED(info))
+{
+	return 0;
+}
kexec/arch/sh/kexec-sh.c +5 -0
index ce341c8..f84c40c 100644
@@ -257,3 +257,8 @@ unsigned long add_buffer(struct kexec_info *info, const void *buf,
 	return add_buffer_phys_virt(info, buf, bufsz, memsz, buf_align,
 				    buf_min, buf_max, buf_end, 1);
 }
+
+int arch_do_exclude_segment(struct kexec_segment *UNUSED(seg_ptr), struct kexec_info *UNUSED(info))
+{
+	return 0;
+}
kexec/arch/x86_64/kexec-x86_64.c +5 -0
index ffd84f0..42af90a 100644
@@ -188,3 +188,8 @@ void arch_update_purgatory(struct kexec_info *info)
 	elf_rel_set_symbol(&info->rhdr, "panic_kernel",
 				&panic_kernel, sizeof(panic_kernel));
 }
+
+int arch_do_exclude_segment(struct kexec_segment *UNUSED(seg_ptr), struct kexec_info *UNUSED(info))
+{
+	return 0;
+}
kexec/kexec-syscall.h +1 -1
index 73e5254..4675c46 100644
@@ -112,7 +112,7 @@ static inline long kexec_file_load(int kernel_fd, int initrd_fd,
 
 #define KEXEC_ON_CRASH		0x00000001
 #define KEXEC_PRESERVE_CONTEXT	0x00000002
-#define KEXEC_UPDATE_ELFCOREHDR	0x00000004
+#define KEXEC_CRASH_HOTPLUG_SUPPORT	0x00000008
 #define KEXEC_ARCH_MASK		0xffff0000
 
 /* Flags for kexec file based system call */
kexec/kexec.c +8 -6
index 222f79e..034cea6 100644
@@ -701,10 +701,13 @@ static void update_purgatory(struct kexec_info *info)
 			continue;
 		}
 
-		/* Don't include elfcorehdr in the checksum, if hotplug
-		 * support enabled.
+		/*
+		 * Let architecture decide which segments to exclude from checksum
+		 * if hotplug support is enabled.
 		 */
-		if (do_hotplug && (info->segment[i].mem == (void *)info->elfcorehdr)) {
+		if (do_hotplug && arch_do_exclude_segment(&info->segment[i], info)) {
+			dbgprintf("Skipping segment mem: 0x%lx from SHA calculation\n",
+				  (unsigned long)info->segment[i].mem);
 			continue;
 		}
 
@@ -1651,7 +1654,6 @@ int main(int argc, char *argv[])
 		die("--load-live-update can only be used with xen\n");
 	}
 
-	/* NOTE: Xen KEXEC_LIVE_UPDATE and KEXEC_UPDATE_ELFCOREHDR collide */
 	if (do_hotplug) {
 		const char *ces = "/sys/kernel/crash_elfcorehdr_size";
 		char *buf, *endptr = NULL;
@@ -1665,8 +1667,8 @@ int main(int argc, char *argv[])
 		if (!elfcorehdrsz || (endptr && *endptr != '\0'))
 			die("Path %s does not exist, the kernel needs CONFIG_CRASH_HOTPLUG\n", ces);
 		dbgprintf("ELFCOREHDR_SIZE %lu\n", elfcorehdrsz);
-		/* Indicate to the kernel it is ok to modify the elfcorehdr */
-		kexec_flags |= KEXEC_UPDATE_ELFCOREHDR;
+		/* Indicate to the kernel it is ok to modify the relevant kexec segments */
+		kexec_flags |= KEXEC_CRASH_HOTPLUG_SUPPORT;
 	}
 
 	fileind = optind;
kexec/kexec.h +2 -0
index 1004aff..2d758c9 100644
@@ -307,6 +307,8 @@ extern int do_hotplug;
 #define BOOTLOADER_VERSION PACKAGE_VERSION
 
 void arch_usage(void);
+/* Return non-zero if segment needs to be excluded from SHA calculation, else 0. */
+int arch_do_exclude_segment(struct kexec_segment *seg_ptr, struct kexec_info *info);
 int arch_process_options(int argc, char **argv);
 int arch_compat_trampoline(struct kexec_info *info);
 void arch_update_purgatory(struct kexec_info *info);
Signature · 7 lines
2.45.1


_______________________________________________
kexec mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/kexec
#1 lore raw

PATCH v3 2/3 powerpc/kexec_load: add hotplug support

Kernel commits b741092d5976 ("powerpc/crash: add crash CPU hotplug support") and 849599b702ef ("powerpc/crash: add crash memory hotplug support") added crash CPU/Memory hotplug support on PowerPC. This patch extends that support for the kexec_load syscall.

During CPU/Memory hotplug events on PowerPC, two kexec segments, elfcorehdr, and FDT, get updated by the kernel. To ensure the kernel can safely update these two kexec segments for the kdump image loaded using the kexec_load system call, the following changes are made:

1. Extra size is allocated for both elfcorehdr and FDT to accommodate additional resources in the future. For the elfcorehdr, the size hint is taken from /sys/kernel/crash_elfcorehdr_size sysfs, while for FDT, extra size is allocated to hold possible CPU nodes.

2. Both elfcorehdr and FDT are skipped from SHA calculation.

Cc Aditya Gupta <[email protected]>
Cc Baoquan He <[email protected]>
Cc Coiby Xu <[email protected]>
Cc Mahesh Salgaonkar <[email protected]>
Cc Simon Horman <[email protected]>

---

Changelog:

From v2 -> v3:
- Added a Acked-by, no functional changes.

From v1 -> v2:
- Find CPUs in the system using the /sys/devices/system/cpu/present sysfs instead of traversing all nodes under /proc/device-tree/cpus.

- Added a new function to find present CPUs in the system.

- Removed unnecessary NULL check on seg_ptr from arch_do_exclude_segment().

---
 kexec/arch/ppc64/crashdump-ppc64.c  |  16 +-
 kexec/arch/ppc64/fdt.c              | 236 +++++++++++++++++++++++++++-
 kexec/arch/ppc64/include/arch/fdt.h |   2 +-
 kexec/arch/ppc64/kexec-elf-ppc64.c  |   2 +-
 kexec/arch/ppc64/kexec-ppc64.c      |   9 +-
 5 files changed, 258 insertions(+), 7 deletions(-)
kexec/arch/ppc64/crashdump-ppc64.c +13 -3
index 6d47898..98d439a 100644
@@ -476,7 +476,7 @@ int load_crashdump_segments(struct kexec_info *info, char* mod_cmdline,
 				uint64_t max_addr, unsigned long min_base)
 {
 	void *tmp;
-	unsigned long sz;
+	unsigned long sz, memsz;
 	uint64_t elfcorehdr;
 	int nr_ranges, align = 1024, i;
 	unsigned long long end;
@@ -531,8 +531,18 @@ int load_crashdump_segments(struct kexec_info *info, char* mod_cmdline,
 		}
 	}
 
-	elfcorehdr = add_buffer(info, tmp, sz, sz, align, min_base,
-				max_addr, 1);
+	memsz = sz;
+	/* To support --hotplug, replace the calculated memsz with the value
+	 * from /sys/kernel/crash_elfcorehdr_size and align it correctly.
+	 */
+	if (do_hotplug) {
+		if (elfcorehdrsz > sz)
+			memsz = _ALIGN(elfcorehdrsz, align);
+	}
+
+	/* Record the location of the elfcorehdr for hotplug handling */
+	info->elfcorehdr = elfcorehdr = add_buffer(info, tmp, sz, memsz, align,
+						   min_base, max_addr, 1);
 	reserve(elfcorehdr, sz);
 	/* modify and store the cmdline in a global array. This is later
 	 * read by flatten_device_tree and modified if required
kexec/arch/ppc64/fdt.c +235 -1

258 lines not on this page · raw message

kexec/arch/ppc64/include/arch/fdt.h +1 -1
index b19f185..5f340b0 100644
@@ -3,6 +3,6 @@
 
 #include <sys/types.h>
 
-int fixup_dt(char **fdt, off_t *size);
+int fixup_dt(char **fdt, off_t *size, unsigned long kexec_flags);
 
 #endif
kexec/arch/ppc64/kexec-elf-ppc64.c +1 -1
index bdcfd20..858c994 100644
@@ -345,7 +345,7 @@ int elf_ppc64_load(int argc, char **argv, const char *buf, off_t len,
 		create_flatten_tree(&seg_buf, &seg_size, cmdline);
 	}
 
-	result = fixup_dt(&seg_buf, &seg_size);
+	result = fixup_dt(&seg_buf, &seg_size, info->kexec_flags);
 	if (result < 0)
 		return result;
 
kexec/arch/ppc64/kexec-ppc64.c +8 -1
index fb27b6b..13c3ce3 100644
@@ -24,6 +24,7 @@
 #include <errno.h>
 #include <stdint.h>
 #include <string.h>
+#include <libfdt.h>
 #include <sys/stat.h>
 #include <sys/types.h>
 #include <dirent.h>
@@ -968,7 +969,13 @@ void arch_update_purgatory(struct kexec_info *UNUSED(info))
 {
 }
 
-int arch_do_exclude_segment(struct kexec_segment *UNUSED(seg_ptr), struct kexec_info *UNUSED(info))
+int arch_do_exclude_segment(struct kexec_segment *seg_ptr, struct kexec_info *info)
 {
+	if (info->elfcorehdr == (unsigned long) seg_ptr->mem)
+		return 1;
+
+	if (seg_ptr->buf && fdt_magic(seg_ptr->buf) == FDT_MAGIC)
+		return 1;
+
 	return 0;
 }
Signature · 7 lines
2.45.1


_______________________________________________
kexec mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/kexec
#2 lore raw

PATCH v3 3/3 doc/hotplug: update man and --help

In reply to Sourabh Jain

Update the man page and --help option to make the description of the --hotplug option easier to understand.

Cc Aditya Gupta <[email protected]>
Cc Baoquan He <[email protected]>
Cc Coiby Xu <[email protected]>
Cc Mahesh Salgaonkar <[email protected]>
Cc Simon Horman <[email protected]>

---
Changelog:

From v2 -> v3
- Updated --hotplug option description

---
 kexec/kexec.8 | 8 ++++----
 kexec/kexec.c | 4 +++-
 2 files changed, 7 insertions(+), 5 deletions(-)
kexec/kexec.8 +4 -4
index 9e995fe..793e876 100644
@@ -140,10 +140,10 @@ Open a help file for
 .BR kexec .
 .TP
 .B \-\-hotplug
-Setup for kernel modification of the elfcorehdr. This option performs
-the steps needed to support kernel updates to the elfcorehdr in the
-presence of hot un/plug and/or on/offline events. This option only
-useful for KEXEC_LOAD syscall.
+Setup kexec segments such that kernel can safely update them on CPU/Memory
+hot add/remove events. If this option is enabled, kernel does in-kernel
+update of kexec segments on CPU/Memory hot add/remove events, thus avoiding
+the need to reload kdump kernel.
 .TP
 .B \-i\ (\-\-no-checks)
 Fast reboot, no memory integrity checks.
kexec/kexec.c +3 -1
index 034cea6..9b7c34c 100644
@@ -1093,7 +1093,9 @@ void usage(void)
 	       "                      back to the compatibility syscall when file based\n"
 	       "                      syscall is not supported or the kernel did not\n"
 	       "                      understand the image (default)\n"
-	       " --hotplug            Setup for kernel modification of elfcorehdr.\n"
+	       " --hotplug            Do in-kernel update of kexec segments on CPU/Memory\n"
+	       "                      hot add/remove events, avoiding the need to reload\n"
+	       "                      kdump kernel on online/offline events.\n"
 	       " -d, --debug          Enable debugging to help spot a failure.\n"
 	       " -S, --status         Return 1 if the type (by default crash) is loaded,\n"
 	       "                      0 if not.\n"
Signature · 7 lines
2.45.1


_______________________________________________
kexec mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/kexec
Acked-by on v3 3/3
#3 lore raw

Re: [PATCH v3 3/3] doc/hotplug: update man and --help

Quoted 13 lines · Sourabh Jain

Update the man page and --help option to make the description of the --hotplug option easier to understand.

Cc Aditya Gupta <[email protected]>
Cc Baoquan He <[email protected]>
Cc Coiby Xu <[email protected]>
Cc Mahesh Salgaonkar <[email protected]>
Cc Simon Horman <[email protected]>

---
Changelog:

Quoted 7 lines

From v2 -> v3
- Updated --hotplug option description

---
 kexec/kexec.8 | 8 ++++----
 kexec/kexec.c | 4 +++-
 2 files changed, 7 insertions(+), 5 deletions(-)
Quoted 38 lines
kexec/kexec.8 +4 -4
index 9e995fe..793e876 100644
@@ -140,10 +140,10 @@ Open a help file for
 .BR kexec .
 .TP
 .B \-\-hotplug
-Setup for kernel modification of the elfcorehdr. This option performs
-the steps needed to support kernel updates to the elfcorehdr in the
-presence of hot un/plug and/or on/offline events. This option only
-useful for KEXEC_LOAD syscall.
+Setup kexec segments such that kernel can safely update them on CPU/Memory
+hot add/remove events. If this option is enabled, kernel does in-kernel
+update of kexec segments on CPU/Memory hot add/remove events, thus avoiding
+the need to reload kdump kernel.
 .TP
 .B \-i\ (\-\-no-checks)
 Fast reboot, no memory integrity checks.
kexec/kexec.c +3 -1
index 034cea6..9b7c34c 100644
@@ -1093,7 +1093,9 @@ void usage(void)
 	       "                      back to the compatibility syscall when file based\n"
 	       "                      syscall is not supported or the kernel did not\n"
 	       "                      understand the image (default)\n"
-	       " --hotplug            Setup for kernel modification of elfcorehdr.\n"
+	       " --hotplug            Do in-kernel update of kexec segments on CPU/Memory\n"
+	       "                      hot add/remove events, avoiding the need to reload\n"
+	       "                      kdump kernel on online/offline events.\n"
 	       " -d, --debug          Enable debugging to help spot a failure.\n"
 	       " -S, --status         Return 1 if the type (by default crash) is loaded,\n"
 	       "                      0 if not.\n"
Signature · 1 line
2.45.1

_______________________________________________
kexec mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/kexec

#4 lore raw

Re: [PATCH v3 3/3] doc/hotplug: update man and --help

Quoted 14 lines · Baoquan He
Quoted 13 lines · Sourabh Jain

Update the man page and --help option to make the description of the --hotplug option easier to understand.

Cc Aditya Gupta <[email protected]>
Cc Baoquan He <[email protected]>
Cc Coiby Xu <[email protected]>
Cc Mahesh Salgaonkar <[email protected]>
Cc Simon Horman <[email protected]>

---
Changelog:

Quoted 8 lines
Quoted 7 lines

From v2 -> v3
- Updated --hotplug option description

---
  kexec/kexec.8 | 8 ++++----
  kexec/kexec.c | 4 +++-
  2 files changed, 7 insertions(+), 5 deletions(-)

Thanks for Ack!

- Sourabh Jain

_______________________________________________
kexec mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/kexec

#5 lore raw
In reply to Sourabh Jain

On 07/02/24 at 10:00am, Sourabh Jain wrote:
......

Quoted 6 lines
kexec/arch/i386/kexec-x86.c +0 -0
index 444cb69..b4947a0 100644
@@ -208,3 +208,11 @@ void arch_update_purgatory(struct kexec_info *info)
 	elf_rel_set_symbol(&info->rhdr, "panic_kernel",

kexec/arch/i386/kexec-x86.c void arch_update_purgatory(struct kexec_info *info) · line 218

Quoted 10 lines
kexec/arch/i386/kexec-x86.c +8 -0
 		&panic_kernel, sizeof(panic_kernel));
 }
+
+int arch_do_exclude_segment(struct kexec_segment *seg_ptr, struct kexec_info *info)
+{
+	if (info->elfcorehdr == (unsigned long) seg_ptr->mem)
+		return 1;
+
+	return 0;
+}

I know the similar question has been asked in earlier version, I may not get it. still raise concern here to ask why x86_64 returns 0 directly, while i386 will have the different cases.

kexec/arch/ia64/kexec-ia64.c void arch_update_purgatory(struct kexec_info *UNUSED(info)) · line 248

Quoted 9 lines
kexec/arch/ia64/kexec-ia64.c +1 -0
index 418d997..8d9c1f3 100644
@@ -245,3 +245,7 @@ void arch_update_purgatory(struct kexec_info *UNUSED(info))
 {
 }
 
+int arch_do_exclude_segment(struct kexec_segment *UNUSED(seg_ptr), struct kexec_info *UNUSED(info))
                                                            ~~~~~~~

A tiny nit about the parameter naming, since the existing is taking 'struct kexec_segment *segment', can we also take the same way? Not strong opinion.

=====
kexec-tools/kexec/kexec.c:
static int valid_memory_segment(struct kexec_info *info,
struct kexec_segment *segment)

{
        unsigned long sstart, send;
        sstart = (unsigned long)segment->mem;
        send   = sstart + segment->memsz - 1;

        return valid_memory_range(info, sstart, send);
}
Quoted 110 lines
kexec/arch/ia64/kexec-ia64.c +3 -0
+{
+	return 0;
+}
kexec/arch/loongarch/kexec-loongarch.c +5 -0
index ac75030..ee7b9f1 100644
@@ -381,3 +381,8 @@ unsigned long add_buffer(struct kexec_info *info, const void *buf,
 	return add_buffer_phys_virt(info, buf, bufsz, memsz, buf_align,
 				    buf_min, buf_max, buf_end, 1);
 }
+
+int arch_do_exclude_segment(struct kexec_segment *UNUSED(seg_ptr), struct kexec_info *UNUSED(info))
+{
+	return 0;
+}
kexec/arch/m68k/kexec-m68k.c +5 -0
index cb54927..0c7dbaf 100644
@@ -108,3 +108,8 @@ void add_segment(struct kexec_info *info, const void *buf, size_t bufsz,
 {
 	add_segment_phys_virt(info, buf, bufsz, base, memsz, 1);
 }
+
+int arch_do_exclude_segment(struct kexec_segment *UNUSED(seg_ptr), struct kexec_info *UNUSED(info))
+{
+	return 0;
+}
kexec/arch/mips/kexec-mips.c +4 -0
index d8cbea8..94224ee 100644
@@ -189,3 +189,7 @@ unsigned long add_buffer(struct kexec_info *info, const void *buf,
 				    buf_min, buf_max, buf_end, 1);
 }
 
+int arch_do_exclude_segment(const void *UNUSED(seg_ptr), struct kexec_info *UNUSED(info))
+{
+	return 0;
+}
kexec/arch/ppc/kexec-ppc.c +4 -0
index 03bec36..c8af870 100644
@@ -966,3 +966,7 @@ void arch_update_purgatory(struct kexec_info *UNUSED(info))
 {
 }
 
+int arch_do_exclude_segment(struct kexec_segment *UNUSED(seg_ptr), struct kexec_info *UNUSED(info))
+{
+	return 0;
+}
kexec/arch/ppc64/kexec-ppc64.c +5 -0
index bd5274c..fb27b6b 100644
@@ -967,3 +967,8 @@ int arch_compat_trampoline(struct kexec_info *UNUSED(info))
 void arch_update_purgatory(struct kexec_info *UNUSED(info))
 {
 }
+
+int arch_do_exclude_segment(struct kexec_segment *UNUSED(seg_ptr), struct kexec_info *UNUSED(info))
+{
+	return 0;
+}
kexec/arch/s390/kexec-s390.c +5 -0
index 33ba6b9..0561ee7 100644
@@ -267,3 +267,8 @@ int get_crash_kernel_load_range(uint64_t *start, uint64_t *end)
 {
 	return parse_iomem_single("Crash kernel\n", start, end);
 }
+
+int arch_do_exclude_segment(struct kexec_segment *UNUSED(seg_ptr), struct kexec_info *UNUSED(info))
+{
+	return 0;
+}
kexec/arch/sh/kexec-sh.c +5 -0
index ce341c8..f84c40c 100644
@@ -257,3 +257,8 @@ unsigned long add_buffer(struct kexec_info *info, const void *buf,
 	return add_buffer_phys_virt(info, buf, bufsz, memsz, buf_align,
 				    buf_min, buf_max, buf_end, 1);
 }
+
+int arch_do_exclude_segment(struct kexec_segment *UNUSED(seg_ptr), struct kexec_info *UNUSED(info))
+{
+	return 0;
+}
kexec/arch/x86_64/kexec-x86_64.c +5 -0
index ffd84f0..42af90a 100644
@@ -188,3 +188,8 @@ void arch_update_purgatory(struct kexec_info *info)
 	elf_rel_set_symbol(&info->rhdr, "panic_kernel",
 				&panic_kernel, sizeof(panic_kernel));
 }
+
+int arch_do_exclude_segment(struct kexec_segment *UNUSED(seg_ptr), struct kexec_info *UNUSED(info))
+{
+	return 0;
+}
kexec/kexec-syscall.h +0 -0
index 73e5254..4675c46 100644

kexec/kexec-syscall.h static inline long kexec_file_load(int kernel_fd, int initrd_fd, · line 116

Quoted 6 lines
kexec/kexec-syscall.h +1 -1
@@ -112,7 +112,7 @@ static inline long kexec_file_load(int kernel_fd, int initrd_fd,
 
 #define KEXEC_ON_CRASH		0x00000001
 #define KEXEC_PRESERVE_CONTEXT	0x00000002
-#define KEXEC_UPDATE_ELFCOREHDR	0x00000004
+#define KEXEC_CRASH_HOTPLUG_SUPPORT	0x00000008
 #define KEXEC_ARCH_MASK		0xffff0000

Yeah, removing KEXEC_UPDATE_ELFCOREHDR sounds good to avoid collision with xen code.

Quoted 58 lines
kexec/kexec-syscall.h +0 -0
 /* Flags for kexec file based system call */
kexec/kexec.c +8 -6
index 222f79e..034cea6 100644
@@ -701,10 +701,13 @@ static void update_purgatory(struct kexec_info *info)
 			continue;
 		}
 
-		/* Don't include elfcorehdr in the checksum, if hotplug
-		 * support enabled.
+		/*
+		 * Let architecture decide which segments to exclude from checksum
+		 * if hotplug support is enabled.
 		 */
-		if (do_hotplug && (info->segment[i].mem == (void *)info->elfcorehdr)) {
+		if (do_hotplug && arch_do_exclude_segment(&info->segment[i], info)) {
+			dbgprintf("Skipping segment mem: 0x%lx from SHA calculation\n",
+				  (unsigned long)info->segment[i].mem);
 			continue;
 		}
 
@@ -1651,7 +1654,6 @@ int main(int argc, char *argv[])
 		die("--load-live-update can only be used with xen\n");
 	}
 
-	/* NOTE: Xen KEXEC_LIVE_UPDATE and KEXEC_UPDATE_ELFCOREHDR collide */
 	if (do_hotplug) {
 		const char *ces = "/sys/kernel/crash_elfcorehdr_size";
 		char *buf, *endptr = NULL;
@@ -1665,8 +1667,8 @@ int main(int argc, char *argv[])
 		if (!elfcorehdrsz || (endptr && *endptr != '\0'))
 			die("Path %s does not exist, the kernel needs CONFIG_CRASH_HOTPLUG\n", ces);
 		dbgprintf("ELFCOREHDR_SIZE %lu\n", elfcorehdrsz);
-		/* Indicate to the kernel it is ok to modify the elfcorehdr */
-		kexec_flags |= KEXEC_UPDATE_ELFCOREHDR;
+		/* Indicate to the kernel it is ok to modify the relevant kexec segments */
+		kexec_flags |= KEXEC_CRASH_HOTPLUG_SUPPORT;
 	}
 
 	fileind = optind;
kexec/kexec.h +2 -0
index 1004aff..2d758c9 100644
@@ -307,6 +307,8 @@ extern int do_hotplug;
 #define BOOTLOADER_VERSION PACKAGE_VERSION
 
 void arch_usage(void);
+/* Return non-zero if segment needs to be excluded from SHA calculation, else 0. */
+int arch_do_exclude_segment(struct kexec_segment *seg_ptr, struct kexec_info *info);
 int arch_process_options(int argc, char **argv);
 int arch_compat_trampoline(struct kexec_info *info);
 void arch_update_purgatory(struct kexec_info *info);
Signature · 1 line
2.45.1

_______________________________________________
kexec mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/kexec

#6 lore raw

Hello Baoquan,

Quoted 13 lines · Baoquan He

On 07/02/24 at 10:00am, Sourabh Jain wrote:
......

Quoted 11 lines
kexec/arch/i386/kexec-x86.c +3 -0
index 444cb69..b4947a0 100644
@@ -208,3 +208,11 @@ void arch_update_purgatory(struct kexec_info *info)
  	elf_rel_set_symbol(&info->rhdr, "panic_kernel",
  		&panic_kernel, sizeof(panic_kernel));
  }
+
+int arch_do_exclude_segment(struct kexec_segment *seg_ptr, struct kexec_info *info)
+{
Quoted 8 lines

kexec/arch/i386/kexec-x86.c void arch_update_purgatory(struct kexec_info *info) · line 218

Quoted 5 lines
kexec/arch/i386/kexec-x86.c +5 -0
+	if (info->elfcorehdr == (unsigned long) seg_ptr->mem)
+		return 1;
+
+	return 0;
+}

I know the similar question has been asked in earlier version, I may not get it. still raise concern here to ask why x86_64 returns 0 directly, while i386 will have the different cases.

Currently, info->elfcorehdr is only getting initialized in load_crashdump_segments() in kexec/arch/i386/crashdump-x86.c. This led me think that elfcorehdr is only skipped for i386 and NOT x86_64.

However, info->elfcorehdr is actually getting initialized for x86_64 too. Because load_crashdump_segments() defined under kexec/arch/i386/crashdump-x86.c is called for both i386 and x86_64.

You and Hari are both right; the arch_do_exclude_segment() implementation should be the same for both i386 and x86_64.

I will update the patch.

Quoted 19 lines

kexec/arch/ia64/kexec-ia64.c void arch_update_purgatory(struct kexec_info *UNUSED(info)) · line 248

Quoted 10 lines
kexec/arch/ia64/kexec-ia64.c +1 -0
index 418d997..8d9c1f3 100644
@@ -245,3 +245,7 @@ void arch_update_purgatory(struct kexec_info *UNUSED(info))
  {
  }
 
+int arch_do_exclude_segment(struct kexec_segment *UNUSED(seg_ptr), struct kexec_info *UNUSED(info))
                                                             ~~~~~~~

A tiny nit about the parameter naming, since the existing is taking 'struct kexec_segment *segment', can we also take the same way? Not strong opinion.

=====
kexec-tools/kexec/kexec.c:
static int valid_memory_segment(struct kexec_info *info,
struct kexec_segment *segment)

Quoted 7 lines
{
         unsigned long sstart, send;
         sstart = (unsigned long)segment->mem;
         send   = sstart + segment->memsz - 1;

         return valid_memory_range(info, sstart, send);
}

Sure, I will rename the parameter and reorder them just like in the above function.

Quoted 112 lines
Quoted 112 lines
kexec/arch/ia64/kexec-ia64.c +3 -0
+{
+	return 0;
+}
kexec/arch/loongarch/kexec-loongarch.c +5 -0
index ac75030..ee7b9f1 100644
@@ -381,3 +381,8 @@ unsigned long add_buffer(struct kexec_info *info, const void *buf,
  	return add_buffer_phys_virt(info, buf, bufsz, memsz, buf_align,
  				    buf_min, buf_max, buf_end, 1);
  }
+
+int arch_do_exclude_segment(struct kexec_segment *UNUSED(seg_ptr), struct kexec_info *UNUSED(info))
+{
+	return 0;
+}
kexec/arch/m68k/kexec-m68k.c +5 -0
index cb54927..0c7dbaf 100644
@@ -108,3 +108,8 @@ void add_segment(struct kexec_info *info, const void *buf, size_t bufsz,
  {
  	add_segment_phys_virt(info, buf, bufsz, base, memsz, 1);
  }
+
+int arch_do_exclude_segment(struct kexec_segment *UNUSED(seg_ptr), struct kexec_info *UNUSED(info))
+{
+	return 0;
+}
kexec/arch/mips/kexec-mips.c +4 -0
index d8cbea8..94224ee 100644
@@ -189,3 +189,7 @@ unsigned long add_buffer(struct kexec_info *info, const void *buf,
  				    buf_min, buf_max, buf_end, 1);
  }
 
+int arch_do_exclude_segment(const void *UNUSED(seg_ptr), struct kexec_info *UNUSED(info))
+{
+	return 0;
+}
kexec/arch/ppc/kexec-ppc.c +4 -0
index 03bec36..c8af870 100644
@@ -966,3 +966,7 @@ void arch_update_purgatory(struct kexec_info *UNUSED(info))
  {
  }
 
+int arch_do_exclude_segment(struct kexec_segment *UNUSED(seg_ptr), struct kexec_info *UNUSED(info))
+{
+	return 0;
+}
kexec/arch/ppc64/kexec-ppc64.c +5 -0
index bd5274c..fb27b6b 100644
@@ -967,3 +967,8 @@ int arch_compat_trampoline(struct kexec_info *UNUSED(info))
  void arch_update_purgatory(struct kexec_info *UNUSED(info))
  {
  }
+
+int arch_do_exclude_segment(struct kexec_segment *UNUSED(seg_ptr), struct kexec_info *UNUSED(info))
+{
+	return 0;
+}
kexec/arch/s390/kexec-s390.c +5 -0
index 33ba6b9..0561ee7 100644
@@ -267,3 +267,8 @@ int get_crash_kernel_load_range(uint64_t *start, uint64_t *end)
  {
  	return parse_iomem_single("Crash kernel\n", start, end);
  }
+
+int arch_do_exclude_segment(struct kexec_segment *UNUSED(seg_ptr), struct kexec_info *UNUSED(info))
+{
+	return 0;
+}
kexec/arch/sh/kexec-sh.c +5 -0
index ce341c8..f84c40c 100644
@@ -257,3 +257,8 @@ unsigned long add_buffer(struct kexec_info *info, const void *buf,
  	return add_buffer_phys_virt(info, buf, bufsz, memsz, buf_align,
  				    buf_min, buf_max, buf_end, 1);
  }
+
+int arch_do_exclude_segment(struct kexec_segment *UNUSED(seg_ptr), struct kexec_info *UNUSED(info))
+{
+	return 0;
+}
kexec/arch/x86_64/kexec-x86_64.c +5 -0
index ffd84f0..42af90a 100644
@@ -188,3 +188,8 @@ void arch_update_purgatory(struct kexec_info *info)
  	elf_rel_set_symbol(&info->rhdr, "panic_kernel",
  				&panic_kernel, sizeof(panic_kernel));
  }
+
+int arch_do_exclude_segment(struct kexec_segment *UNUSED(seg_ptr), struct kexec_info *UNUSED(info))
+{
+	return 0;
+}
kexec/kexec-syscall.h +0 -0
index 73e5254..4675c46 100644
@@ -112,7 +112,7 @@ static inline long kexec_file_load(int kernel_fd, int initrd_fd,
 
Quoted 7 lines

kexec/kexec-syscall.h static inline long kexec_file_load(int kernel_fd, int initrd_fd, · line 116

Quoted 5 lines
kexec/kexec-syscall.h +1 -1
  #define KEXEC_ON_CRASH		0x00000001
  #define KEXEC_PRESERVE_CONTEXT	0x00000002
-#define KEXEC_UPDATE_ELFCOREHDR	0x00000004
+#define KEXEC_CRASH_HOTPLUG_SUPPORT	0x00000008
  #define KEXEC_ARCH_MASK		0xffff0000

Yeah, removing KEXEC_UPDATE_ELFCOREHDR sounds good to avoid collision with xen code.

Thanks for the review.

I will make the necessary changes as mentioned above and send v4.

Thanks,
Sourabh Jain

_______________________________________________
kexec mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/kexec