Skip to contentSkip to conversation

crashdump: Kernel handling of CPU and memory hot un/plug

PATCH Superseded Eric DeVolder 1 participant 8 messages Oct 20, 2022

Subsystems (2)

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

cover crashdump: Kernel handling of CPU and memory hot un/plug

When the kdump service is loaded, if a CPU or memory is hot un/plugged, the crash elfcorehdr, which describes the CPUs and memory in the system, must also be updated, else the resulting vmcore is inaccurate (eg. missing either CPU context or memory regions).

The current solution (eg. RHEL /usr/lib/udev/rules.d/98-kexec.rules) utilizes udev to initiate an unload-then-reload of the *entire* kdump image (eg. kernel, initrd, boot_params, puratory and elfcorehdr) by the userspace kexec utility. In a previous kernel patch post I have outlined the significant performance problems related to offloading this activity to userspace (link below, s/vmcoreinfo/elfcorehdr/).

https://lore.kernel.org/lkml/[email protected]/

As such, I've been working to provide the ability for the Linux kernel to directly modify the elfcorehdr in response to hotplug changes.

https://lore.kernel.org/lkml/[email protected]/

The most recent patch series is listed above, and I believe this work to be nearing the finish line. As such, I'd like to start posting the kexec-tools userspace changes for review in order to minimize the time to adoption.

This kexec-tools patch series is for supporting the kexec_load syscall only. The kernel patch series cited above makes the needed changes to support the kexec_file_load syscall.

There are two basic obstacles/requirements for the kexec-tools to overcome in order to support kernel hotplug rewriting of the elfcorehdr.

First, the buffer containing the elfcorehdr must be excluded from the purgatory checksum/digest, which is computed at load time. Otherwise kernel run-time changes to the elfcorehdr, as a result of hot un/plug, would result in the checksum failing (specifically in purgatory at panic kernel boot time), and kdump capture kernel failing to start.

NOTE: The kernel specifically does *NOT* attempt to recompute the checksum/digest as that would ultimately require patching the in- memory purgatory image with the updated checksum. As that purgatory image is already fully linked, it is binary blob containing no ELF information which would allow it to be re-linked or patched. Thus excluding the elfcorehdr from the checksum/digests avoids all these problems.

Second, the size of the elfcorehdr buffer must be large enough to accomodate growth of the number of CPUs and/or memory regions.

To satisfy the first requirement, this patch series introduces the --hotplug option to indicate to kexec-tools that kexec should exclude the elfcorehdr buffer from the purgatory checksum/digest calculation.

To satisfy the second requirement, this patch series introduces the get_elfcorehdrsz() function to determine the size of the elfcorehdr buffer. This routine takes into account the --hotplug and --elfcorehdrsz options in determining the size (see "crashdump:
create get_elfcorehdrsz()" for details).

I am intentionally posting this series before the kernel changes have been merged. I'm hoping to facilitate discussion as to how kexec-tools wants to handle the soon-to-be new kernel feature.

Discussion items:

- It is worth noting, that deploying kexec-tools, with this series included, on kernels that do NOT have the kernel hotplug series cited above, is safe to do. The result of running a kernel without hotplug elfcorehdr support with kexec-tools and the --hotplug option simply removes the elfcorehdr buffer from the digest. This does not prevent kdump from operating; the only risk being a slight chance of corruption of the elfcorehdr, as it now not covered by the checksum. Using the --elfcorehdrsz option on a kernel without hotplug elfcorehdr support simply results in a possibly oversized buffer for the elfcorehdr, there is no harm in that.

- While I currently have the --hotplug as an option, the option could be eliminated (or reversed polarity) it would be safe to *always* omit the elfcorehdr from the checksum/digest for purgatory. If this were the case, then distros would not have to make any changes to kdump scripts to pass the --hotplug option. Then, when their kernel does include the kernel patch series cited above, kdump and hotplug would "just work".

- I'm unsure if these options should be kept as common/global kexec options, or moved to arch options.

- I'm only showing x86 support (and testing) at this time, but it would be straight forward to provide similar support for the other architectures in a future patch revision.

Thanks!
eric

---
v1: 20oct2022
- Initial patch series
---

Eric DeVolder (7):
crashdump: introduce the --hotplug option
crashdump: exclude elfcorehdr segment from digest for hotplug crashdump/x86: identify elfcorehdr segment for hotplug
kexec: create get_nr_cpus()
crashdump: introduce --elfcorehdrsz option
crashdump: create get_elfcorehdrsz()
crashdump/x86: utilize get_elfcorehdrsz()

 include/elf.h                   |  2 ++
 kexec/arch/i386/crashdump-x86.c |  9 ++++++++
 kexec/crashdump-elf.c           |  6 +-----
 kexec/crashdump.c               | 33 +++++++++++++++++++++++++++++
 kexec/crashdump.h               |  5 ++++-
 kexec/kexec-xen.h               |  1 +
 kexec/kexec.c                   | 37 +++++++++++++++++++++++++++++++++
 kexec/kexec.h                   | 13 +++++++++++-
 8 files changed, 99 insertions(+), 7 deletions(-)
Signature · 7 lines
2.31.1


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

PATCH 2/7 crashdump: exclude elfcorehdr segment from digest for hotplug

To allow modification of the elfcorehdr by the kernel, in response to hot un/plug events, the buffer containing the elfcorehdr must be excluded from the purgatory checksum/digest.

To do so, the address of the elfcorehdr is stored in info->elfcorehdr, and the buffer is excluded from the checksum if the --hotplug option is in effect.

---
 kexec/kexec.c | 9 +++++++++
 kexec/kexec.h | 1 +
 2 files changed, 10 insertions(+)
kexec/kexec.c +9 -0
index 1276726..90f2e71 100644
@@ -673,6 +673,15 @@ static void update_purgatory(struct kexec_info *info)
 		if (info->segment[i].mem == (void *)info->rhdr.rel_addr) {
 			continue;
 		}
+
+		/* Do not include the elfcorehdr in the checksum, if hotplug
+		 * support is enabled. The elfcorehdr is modified upon
+		 * hotplug changes, and would fail checksum at crash time.
+		 */
+		if (do_hotplug && (info->segment[i].mem == (void *)info->elfcorehdr)) {
+			continue;
+		}
+
 		sha256_update(&ctx, info->segment[i].buf,
 			      info->segment[i].bufsz);
 		nullsz = info->segment[i].memsz - info->segment[i].bufsz;
kexec/kexec.h +1 -0
index 7f7936d..9a26065 100644
@@ -169,6 +169,7 @@ struct kexec_info {
 	int command_line_len;
 
 	int skip_checks;
+	unsigned long elfcorehdr;
 };
 
 struct arch_map_entry {
Signature · 7 lines
2.31.1


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

PATCH 1/7 crashdump: introduce the --hotplug option

In reply to Eric DeVolder

This change introduces the --hotplug option which is to indicate that kernel changes to the elfcorehdr, in response to hot un/plug events, can occur.

Beginning with Linux kernel v6.x (FIX!), the kernel can modify the elfcorehdr in response to hotplug changes to cpus and memory. As such, the buffer containing the elfcorehdr must be excluded from the purgatory checksum/digest; otherwise kernel changes to the elfcorehdr as a result of hotplug would result in the checksum failing at panic kernel boot time, and kdump fail.

This option excludes the elfcorehdr buffer from the purgatory checksum/digest so that kernel changes to the elfcorehdr can occur.

This option is only meaningful for kexec_load syscall. For the kexec_file_load syscall, this option is a no-op as the kernel handles all the kexec memory segments appropriately.

---
 kexec/kexec.c | 6 ++++++
 kexec/kexec.h | 7 ++++++-
 2 files changed, 12 insertions(+), 1 deletion(-)
kexec/kexec.c +6 -0
index 0e92d96..1276726 100644
@@ -62,6 +62,7 @@ static unsigned long kexec_flags = 0;
 /* Flags for kexec file (fd) based syscall */
 static unsigned long kexec_file_flags = 0;
 int kexec_debug = 0;
+int do_hotplug = 0;
 
 void dbgprint_mem_range(const char *prefix, struct memory_range *mr, int nr_mr)
 {
@@ -1057,6 +1058,8 @@ void usage(void)
 	       " -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"
+	       " --hotplug            Enable kernel modification of the elfcorehdr, in\n"
+	       "                      response to hot un/plug changes to CPUs and memory\n"
 	       "\n"
 	       "Supported kernel file types and options: \n");
 	for (i = 0; i < file_types; i++) {
@@ -1565,6 +1568,9 @@ int main(int argc, char *argv[])
 		case OPT_PRINT_CKR_SIZE:
 			print_crashkernel_region_size();
 			return 0;
+		case OPT_HOTPLUG:
+			do_hotplug = 1;
+			break;
 		default:
 			break;
 		}
kexec/kexec.h +6 -1
index 0d820ad..7f7936d 100644
@@ -231,7 +231,8 @@ extern int file_types;
 #define OPT_PRINT_CKR_SIZE	262
 #define OPT_LOAD_LIVE_UPDATE	263
 #define OPT_EXEC_LIVE_UPDATE	264
-#define OPT_MAX			265
+#define OPT_HOTPLUG		265
+#define OPT_MAX			266
 #define KEXEC_OPTIONS \
 	{ "help",		0, 0, OPT_HELP }, \
 	{ "version",		0, 0, OPT_VERSION }, \
@@ -258,6 +259,8 @@ extern int file_types;
 	{ "debug",		0, 0, OPT_DEBUG }, \
 	{ "status",		0, 0, OPT_STATUS }, \
 	{ "print-ckr-size",     0, 0, OPT_PRINT_CKR_SIZE }, \
+	{ "hotplug",		0, 0, OPT_HOTPLUG }, \
+
 
 #define KEXEC_OPT_STR "h?vdfixyluet:pscaS"
 
@@ -292,6 +295,8 @@ extern void arch_reuse_initrd(void);
 
 extern int ifdown(void);
 
+extern int do_hotplug;
+
 extern char purgatory[];
 extern size_t purgatory_size;
 
Signature · 7 lines
2.31.1


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

PATCH 3/7 crashdump/x86: identify elfcorehdr segment for hotplug

In reply to Eric DeVolder

Identify and record the elfcorehdr buffer so that it can be excluded from the purgatory checksum/digest if --hotplug is in effect.

---
 kexec/arch/i386/crashdump-x86.c | 3 +++
 1 file changed, 3 insertions(+)
kexec/arch/i386/crashdump-x86.c +3 -0
index df1f24c..cb86ca7 100644
@@ -956,6 +956,9 @@ int load_crashdump_segments(struct kexec_info *info, char* mod_cmdline,
 	} else {
 		memsz = bufsz;
 	}
+
+	/* Record the location of the elfcorehdr for hotplug handling */
+	info->elfcorehdr =
 	elfcorehdr = add_buffer(info, tmp, bufsz, memsz, align, min_base,
 							max_addr, -1);
 	dbgprintf("Created elf header segment at 0x%lx\n", elfcorehdr);
Signature · 7 lines
2.31.1


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

PATCH 4/7 kexec: create get_nr_cpus()

In reply to Eric DeVolder

From existing code, create function get_nr_cpus(). This function, will be referenced from multiple locations.

No functional changes.

---
 kexec/crashdump-elf.c |  6 +-----
 kexec/crashdump.h     |  1 -
 kexec/kexec-xen.h     |  1 +
 kexec/kexec.c         | 11 +++++++++++
 kexec/kexec.h         |  2 ++
 5 files changed, 15 insertions(+), 6 deletions(-)
kexec/crashdump-elf.c +1 -5
index b8bb686..5e29f7a 100644
@@ -43,11 +43,7 @@ int FUNC(struct kexec_info *info,
 	int (*get_note_info)(int cpu, uint64_t *addr, uint64_t *len);
 	long int count_cpu;
 
-	if (xen_present())
-		nr_cpus = xen_get_nr_phys_cpus();
-	else
-		nr_cpus = sysconf(_SC_NPROCESSORS_CONF);
-
+	nr_cpus = get_nr_cpus();
 	if (nr_cpus < 0) {
 		return -1;
 	}
kexec/crashdump.h +0 -1
index 18bd691..28d3278 100644
@@ -57,7 +57,6 @@ unsigned long phys_to_virt(struct crash_elf_info *elf_info,
 			   unsigned long long paddr);
 
 unsigned long xen_architecture(struct crash_elf_info *elf_info);
-int xen_get_nr_phys_cpus(void);
 int xen_get_note(int cpu, uint64_t *addr, uint64_t *len);
 int xen_get_crashkernel_region(uint64_t *start, uint64_t *end);
 
kexec/kexec-xen.h +1 -0
index 70fb576..f54a2dd 100644
@@ -83,5 +83,6 @@ extern int __xc_interface_close(xc_interface *xch);
 #endif
 
 int xen_get_kexec_range(int range, uint64_t *start, uint64_t *end);
+int xen_get_nr_phys_cpus(void);
 
 #endif /* KEXEC_XEN_H */
kexec/kexec.c +11 -0
index 90f2e71..d2379cb 100644
@@ -490,6 +490,17 @@ static int add_backup_segments(struct kexec_info *info,
 	return 0;
 }
 
+long int get_nr_cpus(void)
+{
+	long int nr_cpus;
+
+	if (xen_present())
+		nr_cpus = xen_get_nr_phys_cpus();
+	else
+		nr_cpus = sysconf(_SC_NPROCESSORS_CONF);
+	return nr_cpus;
+}
+
 static char *slurp_fd(int fd, const char *filename, off_t size, off_t *nread)
 {
 	char *buf;
kexec/kexec.h +2 -0
index 9a26065..9675e79 100644
@@ -294,6 +294,8 @@ extern unsigned long add_buffer_phys_virt(struct kexec_info *info,
 	int buf_end, int phys);
 extern void arch_reuse_initrd(void);
 
+extern long int get_nr_cpus(void);
+
 extern int ifdown(void);
 
 extern int do_hotplug;
Signature · 7 lines
2.31.1


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

PATCH 5/7 crashdump: introduce --elfcorehdrsz option

In reply to Eric DeVolder

This option provides a means for tuning the size of the elfcorehdr buffer segment.

The motivation for this option is that a maximum-sized elfcorehdr can be several MiB, whereas a typical just-large-enough tends to be several KiB. This order of magnitude difference may be unpalatable for some users, and so this option allows the size to be dialed in.

See patch "crashdump: create get_elfcorehdrsz()" for more info.

---
 kexec/kexec.c | 11 +++++++++++
 kexec/kexec.h |  5 ++++-
 2 files changed, 15 insertions(+), 1 deletion(-)
kexec/kexec.c +11 -0
index d2379cb..5a54c07 100644
@@ -63,6 +63,7 @@ static unsigned long kexec_flags = 0;
 static unsigned long kexec_file_flags = 0;
 int kexec_debug = 0;
 int do_hotplug = 0;
+unsigned long long elfcorehdrsz = 0;
 
 void dbgprint_mem_range(const char *prefix, struct memory_range *mr, int nr_mr)
 {
@@ -1080,6 +1081,7 @@ void usage(void)
 	       "                      0 if not.\n"
 	       " --hotplug            Enable kernel modification of the elfcorehdr, in\n"
 	       "                      response to hot un/plug changes to CPUs and memory\n"
+	       " --elfcorehdrsz       Specify the size of the elfcorehdr buffer segment\n"
 	       "\n"
 	       "Supported kernel file types and options: \n");
 	for (i = 0; i < file_types; i++) {
@@ -1591,6 +1593,15 @@ int main(int argc, char *argv[])
 		case OPT_HOTPLUG:
 			do_hotplug = 1;
 			break;
+		case OPT_ELFCOREHDRSZ:
+			elfcorehdrsz = strtoul(optarg, &endptr, 0);
+			if (*endptr) {
+				fprintf(stderr,
+					"Bad option value in --elfcorehdrsz=%s\n", optarg);
+				usage();
+				return 1;
+			}
+			break;
 		default:
 			break;
 		}
kexec/kexec.h +4 -1
index 9675e79..ae55d2d 100644
@@ -233,7 +233,8 @@ extern int file_types;
 #define OPT_LOAD_LIVE_UPDATE	263
 #define OPT_EXEC_LIVE_UPDATE	264
 #define OPT_HOTPLUG		265
-#define OPT_MAX			266
+#define OPT_ELFCOREHDRSZ	266
+#define OPT_MAX			267
 #define KEXEC_OPTIONS \
 	{ "help",		0, 0, OPT_HELP }, \
 	{ "version",		0, 0, OPT_VERSION }, \
@@ -261,6 +262,7 @@ extern int file_types;
 	{ "status",		0, 0, OPT_STATUS }, \
 	{ "print-ckr-size",     0, 0, OPT_PRINT_CKR_SIZE }, \
 	{ "hotplug",		0, 0, OPT_HOTPLUG }, \
+	{ "elfcorehdrsz",	1, 0, OPT_ELFCOREHDRSZ }, \
 
 
 #define KEXEC_OPT_STR "h?vdfixyluet:pscaS"
@@ -299,6 +301,7 @@ extern long int get_nr_cpus(void);
 extern int ifdown(void);
 
 extern int do_hotplug;
+extern unsigned long long elfcorehdrsz;
 
 extern char purgatory[];
 extern size_t purgatory_size;
Signature · 7 lines
2.31.1


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

PATCH 6/7 crashdump: create get_elfcorehdrsz()

In reply to Eric DeVolder

This routine provides a means to tune the size of the elfcorehdr buffer segment, taking into account the --hotplug and --elfcorehdrsz options. The possibilities are:

                 no --elfcorehdrsz  w/ --elfcorehdrsz
  no --hotplug   just-large-enough  user-specified
  w/ --hotplug   maximum-size       user-specified

Where just-large-enough is the min_size parameter, computed based upon the existing CPUs and memory regions in the system.

Where maximum-size is computed based upon the maximum number of CPUs and memory regions possible in the system.

---
 include/elf.h     |  2 ++
 kexec/crashdump.c | 33 +++++++++++++++++++++++++++++++++
 kexec/crashdump.h |  4 ++++
 3 files changed, 39 insertions(+)
include/elf.h +2 -0
index 1c8d2cc..2c5884d 100644
@@ -163,6 +163,8 @@ typedef struct
 
 #define EI_PAD		9		/* Byte index of padding bytes */
 
+#define PN_XNUM 0xffff		/* Maximum number of headers in e_phnum */
+
 /* Legal values for e_type (object file type).  */
 
 #define ET_NONE		0		/* No file type */
kexec/crashdump.c +33 -0
index 0b363c5..4210c53 100644
@@ -53,6 +53,9 @@
 #undef EHDR
 #undef FUNC
 
+#define MIN(x, y) ((x) < (y) ? (x) : (y))
+#define MAX(x, y) ((x) > (y) ? (x) : (y))
+
 unsigned long crash_architecture(struct crash_elf_info *elf_info)
 {
 	if (xen_present())
@@ -157,3 +160,33 @@ int get_kernel_vmcoreinfo(uint64_t *addr, uint64_t *len)
 {
 	return get_vmcoreinfo("/sys/kernel/vmcoreinfo", addr, len);
 }
+
+unsigned long get_elfcorehdrsz(struct crash_elf_info *elf_info,
+			       unsigned long min_size,
+			       unsigned long nr_memory_ranges)
+{
+	/* Size elfcorehdr appropriately */
+	unsigned long sz = min_size;
+
+	if (elfcorehdrsz) {
+		/* Utilize option value */
+		sz = elfcorehdrsz;
+	} else {
+		/* Compute maximum for hotplug */
+		if (do_hotplug) {
+			long int nr_cpus = get_nr_cpus();
+			sz = nr_cpus + nr_memory_ranges;
+			sz = MIN(sz, PN_XNUM);
+				if (elf_info->class == ELFCLASS32) {
+					sz *= sizeof(Elf32_Phdr);
+					sz += sizeof(Elf32_Ehdr);
+				} else {
+					sz *= sizeof(Elf64_Phdr);
+					sz += sizeof(Elf64_Ehdr);
+				}
+		}
+		/* else default is min_size */
+	}
+	sz = MAX(min_size, sz);
+	return sz;
+}
kexec/crashdump.h +4 -0
index 28d3278..e4cedce 100644
@@ -53,6 +53,10 @@ int crash_create_elf64_headers(struct kexec_info *info,
 
 unsigned long crash_architecture(struct crash_elf_info *elf_info);
 
+unsigned long get_elfcorehdrsz(struct crash_elf_info *elf_info,
+			       unsigned long min_size,
+			       unsigned long nr_memory_ranges);
+
 unsigned long phys_to_virt(struct crash_elf_info *elf_info,
 			   unsigned long long paddr);
 
Signature · 7 lines
2.31.1


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

PATCH 7/7 crashdump/x86: utilize get_elfcorehdrsz()

In reply to Eric DeVolder

Call get_elfcorehdrsz() to obtain the size of the elfcorehdr buffer.

---
 kexec/arch/i386/crashdump-x86.c | 6 ++++++
 1 file changed, 6 insertions(+)
kexec/arch/i386/crashdump-x86.c +6 -0
index cb86ca7..29d4ba5 100644
@@ -957,6 +957,12 @@ int load_crashdump_segments(struct kexec_info *info, char* mod_cmdline,
 		memsz = bufsz;
 	}
 
+	/* Adjust size of buffer according to hotplug and user */
+	sz = get_elfcorehdrsz(&elf_info, bufsz, CRASH_MAX_MEMORY_RANGES);
+	if (sz > memsz)
+		memsz = sz;
+	memsz = _ALIGN(memsz, align);
+
 	/* Record the location of the elfcorehdr for hotplug handling */
 	info->elfcorehdr =
 	elfcorehdr = add_buffer(info, tmp, bufsz, memsz, align, min_base,
Signature · 7 lines
2.31.1


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