Skip to contentSkip to conversation

x86/kexec: Carry forward the boot DTB on kexec

PATCH Applied Brian Mak 4 participants 15 messages Aug 5, 2025 – Sep 9, 2025

Subsystems (4)
lore
Share & tools
lore

Brian Mak

author
#0 lore raw

v2 cover x86/kexec: Carry forward the boot DTB on kexec

Hi all,

Here is v2 with the comments from v1 addressed. I also added a patch to the series to add KEXEC_FILE_NO_CMA as a legal flag. I noticed that this was missing when I went to add my KEXEC_FILE_FORCE_DTB flag.

Thanks,
Brian

Changes in v2:
- Added a patch to add KEXEC_FILE_NO_CMA as a legal flag
- Added a KEXEC_FILE_FORCE_DTB flag to enable carrying over the current boot's DTB on x86.
- Modified the commit message to include more reasoning for the change.
- Changed a pr_info print to a pr_debug print.

Brian Mak (2):
kexec: Add KEXEC_FILE_NO_CMA as a legal flag
x86/kexec: Carry forward the boot DTB on kexec

 arch/x86/kernel/kexec-bzimage64.c | 47 +++++++++++++++++++++++++++++--
 include/linux/kexec.h             |  6 +++-
 include/uapi/linux/kexec.h        |  4 +++
 kernel/kexec_file.c               |  1 +
 4 files changed, 54 insertions(+), 4 deletions(-)

base-commit: 35a813e010b99894bb4706c56c16a580bf7959c2

Signature · 1 line
2.25.1
author
#1 lore raw

PATCH v2 1/2 kexec: Add KEXEC_FILE_NO_CMA as a legal flag

Commit 07d24902977e ("kexec: enable CMA based contiguous allocation") introduces logic to use CMA-based allocation in kexec by default. As part of the changes, it introduces a kexec_file_load flag to disable the use of CMA allocations from userspace. However, this flag is broken since it is missing from the list of legal flags for kexec_file_load. kexec_file_load returns EINVAL when attempting to use the flag.

Fix this by adding the KEXEC_FILE_NO_CMA flag to the list of legal flags for kexec_file_load.

Fixes 07d24902977e ("kexec: enable CMA based contiguous allocation")
---
 include/linux/kexec.h | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
include/linux/kexec.h +2 -1
index 1b10a5d84b68..39fe3e6cd282 100644
@@ -460,7 +460,8 @@ bool kexec_load_permitted(int kexec_image_type);
 
 /* List of defined/legal kexec file flags */
 #define KEXEC_FILE_FLAGS	(KEXEC_FILE_UNLOAD | KEXEC_FILE_ON_CRASH | \
-				 KEXEC_FILE_NO_INITRAMFS | KEXEC_FILE_DEBUG)
+				 KEXEC_FILE_NO_INITRAMFS | KEXEC_FILE_DEBUG | \
+				 KEXEC_FILE_NO_CMA)
 
 /* flag to track if kexec reboot is in progress */
 extern bool kexec_in_progress;
Signature · 1 line
2.25.1
maintainer
#2 lore raw

Re: [PATCH v2 1/2] kexec: Add KEXEC_FILE_NO_CMA as a legal flag

Quoted 11 lines · On Tue, 5 Aug 2025 14:15:26 -0700 Brian Mak <[email protected]> wrote:

Commit 07d24902977e ("kexec: enable CMA based contiguous allocation") introduces logic to use CMA-based allocation in kexec by default. As part of the changes, it introduces a kexec_file_load flag to disable the use of CMA allocations from userspace. However, this flag is broken since it is missing from the list of legal flags for kexec_file_load. kexec_file_load returns EINVAL when attempting to use the flag.

Fix this by adding the KEXEC_FILE_NO_CMA flag to the list of legal flags for kexec_file_load.

Fixes 07d24902977e ("kexec: enable CMA based contiguous allocation")

A description of the userspace-visible runtime effects of this bug would be very helpful, please. A lot more than "is broken"!

Also, could we please have some reviewer input on this change?

Thanks.

Quoted 15 lines
include/linux/kexec.h +2 -1
@@ -460,7 +460,8 @@ bool kexec_load_permitted(int kexec_image_type);
 
 /* List of defined/legal kexec file flags */
 #define KEXEC_FILE_FLAGS	(KEXEC_FILE_UNLOAD | KEXEC_FILE_ON_CRASH | \
-				 KEXEC_FILE_NO_INITRAMFS | KEXEC_FILE_DEBUG)
+				 KEXEC_FILE_NO_INITRAMFS | KEXEC_FILE_DEBUG | \
+				 KEXEC_FILE_NO_CMA)
 
 /* flag to track if kexec reboot is in progress */
 extern bool kexec_in_progress;
Signature · 1 line
2.25.1
#3 lore raw

Re: [PATCH v2 1/2] kexec: Add KEXEC_FILE_NO_CMA as a legal flag

Quoted 12 lines · Andrew Morton
Quoted 10 lines · On Tue, 5 Aug 2025 14:15:26 -0700 Brian Mak <[email protected]> wrote:

Commit 07d24902977e ("kexec: enable CMA based contiguous allocation") introduces logic to use CMA-based allocation in kexec by default. As part of the changes, it introduces a kexec_file_load flag to disable the use of CMA allocations from userspace. However, this flag is broken since it is missing from the list of legal flags for kexec_file_load. kexec_file_load returns EINVAL when attempting to use the flag.

Fix this by adding the KEXEC_FILE_NO_CMA flag to the list of legal flags for kexec_file_load.

Quoted 6 lines
Quoted 1 line
Fixes 07d24902977e ("kexec: enable CMA based contiguous allocation")

A description of the userspace-visible runtime effects of this bug would be very helpful, please. A lot more than "is broken"!

Also, could we please have some reviewer input on this change?

I didn't receive this patchset, and kexec mailing list is not in CC. I don't know what happened.

Quoted 18 lines
Quoted 17 lines
include/linux/kexec.h +2 -1
@@ -460,7 +460,8 @@ bool kexec_load_permitted(int kexec_image_type);
 
 /* List of defined/legal kexec file flags */
 #define KEXEC_FILE_FLAGS	(KEXEC_FILE_UNLOAD | KEXEC_FILE_ON_CRASH | \
-				 KEXEC_FILE_NO_INITRAMFS | KEXEC_FILE_DEBUG)
+				 KEXEC_FILE_NO_INITRAMFS | KEXEC_FILE_DEBUG | \
+				 KEXEC_FILE_NO_CMA)
 
 /* flag to track if kexec reboot is in progress */
 extern bool kexec_in_progress;
Signature · 1 line
2.25.1
maintainer
#4 lore raw

Re: [PATCH v2 1/2] kexec: Add KEXEC_FILE_NO_CMA as a legal flag

Quoted 15 lines · On Thu, 21 Aug 2025 16:33:26 +0800 Baoquan He <[email protected]> wrote:
Quoted 14 lines · Andrew Morton
Quoted 11 lines · On Tue, 5 Aug 2025 14:15:26 -0700 Brian Mak <[email protected]> wrote:

Commit 07d24902977e ("kexec: enable CMA based contiguous allocation") introduces logic to use CMA-based allocation in kexec by default. As part of the changes, it introduces a kexec_file_load flag to disable the use of CMA allocations from userspace. However, this flag is broken since it is missing from the list of legal flags for kexec_file_load. kexec_file_load returns EINVAL when attempting to use the flag.

Fix this by adding the KEXEC_FILE_NO_CMA flag to the list of legal flags for kexec_file_load.

Fixes 07d24902977e ("kexec: enable CMA based contiguous allocation")
Quoted 7 lines
Quoted 4 lines

A description of the userspace-visible runtime effects of this bug would be very helpful, please. A lot more than "is broken"!

Also, could we please have some reviewer input on this change?

I didn't receive this patchset, and kexec mailing list is not in CC. I don't know what happened.

Well that's annoying. kexec@ and linux-kernel were on the cc line.

Here's what's in mm.git's mm-hotfixes branch:

From: Brian Mak <[email protected]>
Subject: kexec: add KEXEC_FILE_NO_CMA as a legal flag
Date: Tue, 5 Aug 2025 14:15:26 -0700

Commit 07d24902977e ("kexec: enable CMA based contiguous allocation") introduces logic to use CMA-based allocation in kexec by default. As part of the changes, it introduces a kexec_file_load flag to disable the use of CMA allocations from userspace. However, this flag is broken since it is missing from the list of legal flags for kexec_file_load. kexec_file_load returns EINVAL when attempting to use the flag.

Fix this by adding the KEXEC_FILE_NO_CMA flag to the list of legal flags for kexec_file_load.

Fixes 07d24902977e ("kexec: enable CMA based contiguous allocation")
Cc Alexander Graf <[email protected]>
Cc Baoquan He <[email protected]>
Cc Borislav Betkov <[email protected]>
Cc Dave Young <[email protected]>
Cc "H. Peter Anvin" <[email protected]>
Cc Ingo Molnar <[email protected]>
Cc Rob Herring <[email protected]>
Cc Saravana Kannan <[email protected]>
Cc Thomas Gleinxer <[email protected]>

---

 include/linux/kexec.h |    3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
include/linux/kexec.h renamed from include/linux/kexec.h~kexec-add-kexec_file_no_cma-as-a-legal-flag +2 -1
@@ -460,7 +460,8 @@ bool kexec_load_permitted(int kexec_imag
 
 /* List of defined/legal kexec file flags */
 #define KEXEC_FILE_FLAGS	(KEXEC_FILE_UNLOAD | KEXEC_FILE_ON_CRASH | \
-				 KEXEC_FILE_NO_INITRAMFS | KEXEC_FILE_DEBUG)
+				 KEXEC_FILE_NO_INITRAMFS | KEXEC_FILE_DEBUG | \
+				 KEXEC_FILE_NO_CMA)
 
 /* flag to track if kexec reboot is in progress */
 extern bool kexec_in_progress;

_

and the second patch I placed in mm-unstable:

From: Brian Mak <[email protected]>
Subject: x86/kexec: carry forward the boot DTB on kexec
Date: Tue, 5 Aug 2025 14:15:27 -0700

Currently, the kexec_file_load syscall on x86 does not support passing a device tree blob to the new kernel. Some embedded x86 systems use device trees. On these systems, failing to pass a device tree to the new kernel causes a boot failure.

To add support for this, we copy the behavior of ARM64 and PowerPC and copy the current boot's device tree blob for use in the new kernel. We do this on x86 by passing the device tree blob as a setup_data entry in accordance with the x86 boot protocol.

This behavior is gated behind the KEXEC_FILE_FORCE_DTB flag.

Cc Alexander Graf <[email protected]>
Cc Baoquan He <[email protected]>
Cc Borislav Betkov <[email protected]>
Cc Dave Young <[email protected]>
Cc "H. Peter Anvin" <[email protected]>
Cc Ingo Molnar <[email protected]>
Cc Rob Herring <[email protected]>
Cc Saravana Kannan <[email protected]>
Cc Thomas Gleinxer <[email protected]>

---

 arch/x86/kernel/kexec-bzimage64.c |   47 ++++++++++++++++++++++++++--
 include/linux/kexec.h             |    5 ++
 include/uapi/linux/kexec.h        |    4 ++
 kernel/kexec_file.c               |    1 
 4 files changed, 53 insertions(+), 4 deletions(-)
arch/x86/kernel/kexec-bzimage64.c renamed from arch/x86/kernel/kexec-bzimage64.c~x86-kexec-carry-forward-the-boot-dtb-on-kexec +44 -3

89 lines not on this page · raw message

include/linux/kexec.h renamed from include/linux/kexec.h~x86-kexec-carry-forward-the-boot-dtb-on-kexec +4 -1
@@ -395,6 +395,9 @@ struct kimage {
 
 	/* Information for loading purgatory */
 	struct purgatory_info purgatory_info;
+
+	/* Force carrying over the DTB from the current boot */
+	bool force_dtb;
 #endif
 
 #ifdef CONFIG_CRASH_HOTPLUG
@@ -461,7 +464,7 @@ bool kexec_load_permitted(int kexec_imag
 /* List of defined/legal kexec file flags */
 #define KEXEC_FILE_FLAGS	(KEXEC_FILE_UNLOAD | KEXEC_FILE_ON_CRASH | \
 				 KEXEC_FILE_NO_INITRAMFS | KEXEC_FILE_DEBUG | \
-				 KEXEC_FILE_NO_CMA)
+				 KEXEC_FILE_NO_CMA | KEXEC_FILE_FORCE_DTB)
 
 /* flag to track if kexec reboot is in progress */
 extern bool kexec_in_progress;
include/uapi/linux/kexec.h renamed from include/uapi/linux/kexec.h~x86-kexec-carry-forward-the-boot-dtb-on-kexec +4 -0
@@ -22,12 +22,16 @@
  * KEXEC_FILE_ON_CRASH : Load/unload operation belongs to kdump image.
  * KEXEC_FILE_NO_INITRAMFS : No initramfs is being loaded. Ignore the initrd
  *                           fd field.
+ * KEXEC_FILE_FORCE_DTB : Force carrying over the current boot's DTB to the new
+ *                        kernel on x86. This is already the default behavior on
+ *                        some other architectures, like ARM64 and PowerPC.
  */
 #define KEXEC_FILE_UNLOAD	0x00000001
 #define KEXEC_FILE_ON_CRASH	0x00000002
 #define KEXEC_FILE_NO_INITRAMFS	0x00000004
 #define KEXEC_FILE_DEBUG	0x00000008
 #define KEXEC_FILE_NO_CMA	0x00000010
+#define KEXEC_FILE_FORCE_DTB	0x00000020
 
 /* These values match the ELF architecture values.
  * Unless there is a good reason that should continue to be the case.
kernel/kexec_file.c renamed from kernel/kexec_file.c~x86-kexec-carry-forward-the-boot-dtb-on-kexec +1 -0
@@ -255,6 +255,7 @@ kimage_file_prepare_segments(struct kima
 	}
 
 	image->no_cma = !!(flags & KEXEC_FILE_NO_CMA);
+	image->force_dtb = flags & KEXEC_FILE_FORCE_DTB;
 
 	if (cmdline_len) {
 		image->cmdline_buf = memdup_user(cmdline_ptr, cmdline_len);

_

Acked-by
#5 lore raw

Re: [PATCH v2 1/2] kexec: Add KEXEC_FILE_NO_CMA as a legal flag

Quoted 4 lines · Andrew Morton
Quoted 2 lines · On Thu, 21 Aug 2025 16:33:26 +0800 Baoquan He <[email protected]> wrote:
Quoted 1 line · Andrew Morton

On Tue, 5 Aug 2025 14:15:26 -0700 Brian Mak <[email protected]> wrote:

......snip.....

Quoted 8 lines

---

 include/linux/kexec.h |    3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

include/linux/kexec.h bool kexec_load_permitted(int kexec_imag · line 467

Quoted 9 lines
include/linux/kexec.h renamed from include/linux/kexec.h~kexec-add-kexec_file_no_cma-as-a-legal-flag +2 -1
@@ -460,7 +460,8 @@ bool kexec_load_permitted(int kexec_imag
 
 /* List of defined/legal kexec file flags */
 #define KEXEC_FILE_FLAGS	(KEXEC_FILE_UNLOAD | KEXEC_FILE_ON_CRASH | \
-				 KEXEC_FILE_NO_INITRAMFS | KEXEC_FILE_DEBUG)
+				 KEXEC_FILE_NO_INITRAMFS | KEXEC_FILE_DEBUG | \
+				 KEXEC_FILE_NO_CMA)
 
 /* flag to track if kexec reboot is in progress */
 extern bool kexec_in_progress;

Yeah, this is a good catch and great fix. Without this fix, kexec_file_load syscall will failed and return '-EINVAL' when KEXEC_FILE_NO_CMA is specified just as below code shows. So, for this patch,

And, by the way, has the user space kexec-tools got the change merged to allow KEXEC_FILE_NO_CMA specified?

And, Alexander, I am wondering why this is not captured when you test specifying KEXEC_FILE_NO_CMA case. Or you just skip the no_cma case testing?

===================================================================
SYSCALL_DEFINE5(kexec_file_load, int, kernel_fd, int, initrd_fd,
unsigned long, cmdline_len, const char __user *, cmdline_ptr, unsigned long, flags)

{               
        int image_type = (flags & KEXEC_FILE_ON_CRASH) ?
                         KEXEC_TYPE_CRASH : KEXEC_TYPE_DEFAULT;
        struct kimage **dest_image, *image;
        int ret = 0, i;

        /* We only trust the superuser with rebooting the system. */
        if (!kexec_load_permitted(image_type))
                return -EPERM;

        /* Make sure we have a legal set of flags */
        if (flags != (flags & KEXEC_FILE_FLAGS))
                return -EINVAL;
         ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
	......
}

=====================================================

Quoted 113 lines

_

and the second patch I placed in mm-unstable:

From: Brian Mak <[email protected]>
Subject: x86/kexec: carry forward the boot DTB on kexec
Date: Tue, 5 Aug 2025 14:15:27 -0700

Currently, the kexec_file_load syscall on x86 does not support passing a device tree blob to the new kernel. Some embedded x86 systems use device trees. On these systems, failing to pass a device tree to the new kernel causes a boot failure.

To add support for this, we copy the behavior of ARM64 and PowerPC and copy the current boot's device tree blob for use in the new kernel. We do this on x86 by passing the device tree blob as a setup_data entry in accordance with the x86 boot protocol.

This behavior is gated behind the KEXEC_FILE_FORCE_DTB flag.

Cc Alexander Graf <[email protected]>
Cc Baoquan He <[email protected]>
Cc Borislav Betkov <[email protected]>
Cc Dave Young <[email protected]>
Cc "H. Peter Anvin" <[email protected]>
Cc Ingo Molnar <[email protected]>
Cc Rob Herring <[email protected]>
Cc Saravana Kannan <[email protected]>
Cc Thomas Gleinxer <[email protected]>

---

 arch/x86/kernel/kexec-bzimage64.c |   47 ++++++++++++++++++++++++++--
 include/linux/kexec.h             |    5 ++
 include/uapi/linux/kexec.h        |    4 ++
 kernel/kexec_file.c               |    1 
 4 files changed, 53 insertions(+), 4 deletions(-)
arch/x86/kernel/kexec-bzimage64.c renamed from arch/x86/kernel/kexec-bzimage64.c~x86-kexec-carry-forward-the-boot-dtb-on-kexec +41 -0
@@ -16,6 +16,8 @@
 #include <linux/kexec.h>
 #include <linux/kernel.h>
 #include <linux/mm.h>
+#include <linux/libfdt.h>
+#include <linux/of_fdt.h>
 #include <linux/efi.h>
 #include <linux/random.h>
 
@@ -212,6 +214,28 @@ setup_efi_state(struct boot_params *para
 }
 #endif /* CONFIG_EFI */
 
+#ifdef CONFIG_OF_FLATTREE
+static void setup_dtb(struct boot_params *params,
+		      unsigned long params_load_addr,
+		      unsigned int dtb_setup_data_offset)
+{
+	struct setup_data *sd = (void *)params + dtb_setup_data_offset;
+	unsigned long setup_data_phys, dtb_len;
+
+	dtb_len = fdt_totalsize(initial_boot_params);
+	sd->type = SETUP_DTB;
+	sd->len = dtb_len;
+
+	/* Carry over current boot DTB with setup_data */
+	memcpy(sd->data, initial_boot_params, dtb_len);
+
+	/* Add setup data */
+	setup_data_phys = params_load_addr + dtb_setup_data_offset;
+	sd->next = params->hdr.setup_data;
+	params->hdr.setup_data = setup_data_phys;
+}
+#endif /* CONFIG_OF_FLATTREE */
+
 static void
 setup_ima_state(const struct kimage *image, struct boot_params *params,
 		unsigned long params_load_addr,
@@ -336,6 +360,17 @@ setup_boot_parameters(struct kimage *ima
 			sizeof(struct efi_setup_data);
 #endif
 
+#ifdef CONFIG_OF_FLATTREE
+	if (image->force_dtb && initial_boot_params) {
+		setup_dtb(params, params_load_addr, setup_data_offset);
+		setup_data_offset += sizeof(struct setup_data) +
+				     fdt_totalsize(initial_boot_params);
+	} else {
+		pr_debug("Not carrying over DTB, force_dtb = %d\n",
+			 image->force_dtb);
+	}
+#endif
+
 	if (IS_ENABLED(CONFIG_IMA_KEXEC)) {
 		/* Setup IMA log buffer state */
 		setup_ima_state(image, params, params_load_addr,
@@ -529,6 +564,12 @@ static void *bzImage64_load(struct kimag
 				sizeof(struct setup_data) +
 				RNG_SEED_LENGTH;
 
+#ifdef CONFIG_OF_FLATTREE
+	if (image->force_dtb && initial_boot_params)
+		kbuf.bufsz += sizeof(struct setup_data) +
+			      fdt_totalsize(initial_boot_params);
+#endif
+
 	if (IS_ENABLED(CONFIG_IMA_KEXEC))
 		kbuf.bufsz += sizeof(struct setup_data) +
 			      sizeof(struct ima_setup_data);

arch/x86/kernel/kexec-bzimage64.c static void *bzImage64_load(struct kimag · line 581

Quoted 5 lines
arch/x86/kernel/kexec-bzimage64.c renamed from arch/x86/kernel/kexec-bzimage64.c~x86-kexec-carry-forward-the-boot-dtb-on-kexec +1 -1
@@ -537,7 +578,7 @@ static void *bzImage64_load(struct kimag
 		kbuf.bufsz += sizeof(struct setup_data) +
 			      sizeof(struct kho_data);
 
-	params = kzalloc(kbuf.bufsz, GFP_KERNEL);
+	params = kvzalloc(kbuf.bufsz, GFP_KERNEL);

Wondering how big the dtb blob is, can you explain a little bit about the kvzalloc usage here?

Except of this, I have no other concern about this patch.

And what's your plan about the user space kexec-tool change?

Quoted 73 lines
arch/x86/kernel/kexec-bzimage64.c renamed from arch/x86/kernel/kexec-bzimage64.c~x86-kexec-carry-forward-the-boot-dtb-on-kexec +2 -2
 	if (!params)
 		return ERR_PTR(-ENOMEM);
 	efi_map_offset = params_cmdline_sz;
@@ -647,7 +688,7 @@ static void *bzImage64_load(struct kimag
 	return ldata;
 
 out_free_params:
-	kfree(params);
+	kvfree(params);
 	return ERR_PTR(ret);
 }
 
@@ -659,7 +700,7 @@ static int bzImage64_cleanup(void *loade
 	if (!ldata)
 		return 0;
 
-	kfree(ldata->bootparams_buf);
+	kvfree(ldata->bootparams_buf);
 	ldata->bootparams_buf = NULL;
 
 	return 0;
include/linux/kexec.h renamed from include/linux/kexec.h~x86-kexec-carry-forward-the-boot-dtb-on-kexec +4 -1
@@ -395,6 +395,9 @@ struct kimage {
 
 	/* Information for loading purgatory */
 	struct purgatory_info purgatory_info;
+
+	/* Force carrying over the DTB from the current boot */
+	bool force_dtb;
 #endif
 
 #ifdef CONFIG_CRASH_HOTPLUG
@@ -461,7 +464,7 @@ bool kexec_load_permitted(int kexec_imag
 /* List of defined/legal kexec file flags */
 #define KEXEC_FILE_FLAGS	(KEXEC_FILE_UNLOAD | KEXEC_FILE_ON_CRASH | \
 				 KEXEC_FILE_NO_INITRAMFS | KEXEC_FILE_DEBUG | \
-				 KEXEC_FILE_NO_CMA)
+				 KEXEC_FILE_NO_CMA | KEXEC_FILE_FORCE_DTB)
 
 /* flag to track if kexec reboot is in progress */
 extern bool kexec_in_progress;
include/uapi/linux/kexec.h renamed from include/uapi/linux/kexec.h~x86-kexec-carry-forward-the-boot-dtb-on-kexec +4 -0
@@ -22,12 +22,16 @@
  * KEXEC_FILE_ON_CRASH : Load/unload operation belongs to kdump image.
  * KEXEC_FILE_NO_INITRAMFS : No initramfs is being loaded. Ignore the initrd
  *                           fd field.
+ * KEXEC_FILE_FORCE_DTB : Force carrying over the current boot's DTB to the new
+ *                        kernel on x86. This is already the default behavior on
+ *                        some other architectures, like ARM64 and PowerPC.
  */
 #define KEXEC_FILE_UNLOAD	0x00000001
 #define KEXEC_FILE_ON_CRASH	0x00000002
 #define KEXEC_FILE_NO_INITRAMFS	0x00000004
 #define KEXEC_FILE_DEBUG	0x00000008
 #define KEXEC_FILE_NO_CMA	0x00000010
+#define KEXEC_FILE_FORCE_DTB	0x00000020
 
 /* These values match the ELF architecture values.
  * Unless there is a good reason that should continue to be the case.
kernel/kexec_file.c renamed from kernel/kexec_file.c~x86-kexec-carry-forward-the-boot-dtb-on-kexec +1 -0
@@ -255,6 +255,7 @@ kimage_file_prepare_segments(struct kima
 	}
 
 	image->no_cma = !!(flags & KEXEC_FILE_NO_CMA);
+	image->force_dtb = flags & KEXEC_FILE_FORCE_DTB;
 
 	if (cmdline_len) {
 		image->cmdline_buf = memdup_user(cmdline_ptr, cmdline_len);

_

author
#6 lore raw

Re: [PATCH v2 1/2] kexec: Add KEXEC_FILE_NO_CMA as a legal flag

Quoted 6 lines · Baoquan He

Yeah, this is a good catch and great fix. Without this fix, kexec_file_load syscall will failed and return '-EINVAL' when KEXEC_FILE_NO_CMA is specified just as below code shows. So, for this patch,

Hi Baoquan,

Thanks for the ACK!

Quoted 2 lines

And, by the way, has the user space kexec-tools got the change merged to allow KEXEC_FILE_NO_CMA specified?

I don't see any recent commits to kexec-tools to support KEXEC_FILE_NO_CMA.

Quoted 117 lines
Quoted 108 lines

From: Brian Mak <[email protected]>
Subject: x86/kexec: carry forward the boot DTB on kexec
Date: Tue, 5 Aug 2025 14:15:27 -0700

Currently, the kexec_file_load syscall on x86 does not support passing a device tree blob to the new kernel. Some embedded x86 systems use device trees. On these systems, failing to pass a device tree to the new kernel causes a boot failure.

To add support for this, we copy the behavior of ARM64 and PowerPC and copy the current boot's device tree blob for use in the new kernel. We do this on x86 by passing the device tree blob as a setup_data entry in accordance with the x86 boot protocol.

This behavior is gated behind the KEXEC_FILE_FORCE_DTB flag.

---

arch/x86/kernel/kexec-bzimage64.c |   47 ++++++++++++++++++++++++++--
include/linux/kexec.h             |    5 ++
include/uapi/linux/kexec.h        |    4 ++
kernel/kexec_file.c               |    1
4 files changed, 53 insertions(+), 4 deletions(-)
arch/x86/kernel/kexec-bzimage64.c renamed from arch/x86/kernel/kexec-bzimage64.c~x86-kexec-carry-forward-the-boot-dtb-on-kexec +24 -0
@@ -16,6 +16,8 @@
 #include <linux/kexec.h>
 #include <linux/kernel.h>
 #include <linux/mm.h>
+#include <linux/libfdt.h>
+#include <linux/of_fdt.h>
 #include <linux/efi.h>
 #include <linux/random.h>
 
@@ -212,6 +214,28 @@ setup_efi_state(struct boot_params *para
 }
 #endif /* CONFIG_EFI */
 
+#ifdef CONFIG_OF_FLATTREE
+static void setup_dtb(struct boot_params *params,
+                   unsigned long params_load_addr,
+                   unsigned int dtb_setup_data_offset)
+{
+     struct setup_data *sd = (void *)params + dtb_setup_data_offset;
+     unsigned long setup_data_phys, dtb_len;
+
+     dtb_len = fdt_totalsize(initial_boot_params);
+     sd->type = SETUP_DTB;
+     sd->len = dtb_len;
+
+     /* Carry over current boot DTB with setup_data */
+     memcpy(sd->data, initial_boot_params, dtb_len);
+
+     /* Add setup data */
+     setup_data_phys = params_load_addr + dtb_setup_data_offset;
+     sd->next = params->hdr.setup_data;
+     params->hdr.setup_data = setup_data_phys;
+}
+#endif /* CONFIG_OF_FLATTREE */
+

static void
setup_ima_state(const struct kimage *image, struct boot_params *params,
unsigned long params_load_addr,

arch/x86/kernel/kexec-bzimage64.c renamed from arch/x86/kernel/kexec-bzimage64.c~x86-kexec-carry-forward-the-boot-dtb-on-kexec +17 -0
@@ -336,6 +360,17 @@ setup_boot_parameters(struct kimage *ima
                     sizeof(struct efi_setup_data);
 #endif
 
+#ifdef CONFIG_OF_FLATTREE
+     if (image->force_dtb && initial_boot_params) {
+             setup_dtb(params, params_load_addr, setup_data_offset);
+             setup_data_offset += sizeof(struct setup_data) +
+                                  fdt_totalsize(initial_boot_params);
+     } else {
+             pr_debug("Not carrying over DTB, force_dtb = %d\n",
+                      image->force_dtb);
+     }
+#endif
+
     if (IS_ENABLED(CONFIG_IMA_KEXEC)) {
             /* Setup IMA log buffer state */
             setup_ima_state(image, params, params_load_addr,
@@ -529,6 +564,12 @@ static void *bzImage64_load(struct kimag
                             sizeof(struct setup_data) +
                             RNG_SEED_LENGTH;
 
+#ifdef CONFIG_OF_FLATTREE
+     if (image->force_dtb && initial_boot_params)
+             kbuf.bufsz += sizeof(struct setup_data) +
+                           fdt_totalsize(initial_boot_params);
+#endif
+
     if (IS_ENABLED(CONFIG_IMA_KEXEC))
             kbuf.bufsz += sizeof(struct setup_data) +
                           sizeof(struct ima_setup_data);

arch/x86/kernel/kexec-bzimage64.c static void *bzImage64_load(struct kimag · line 581

Quoted 5 lines
arch/x86/kernel/kexec-bzimage64.c renamed from arch/x86/kernel/kexec-bzimage64.c~x86-kexec-carry-forward-the-boot-dtb-on-kexec +1 -1
@@ -537,7 +578,7 @@ static void *bzImage64_load(struct kimag
             kbuf.bufsz += sizeof(struct setup_data) +
                           sizeof(struct kho_data);
 
-     params = kzalloc(kbuf.bufsz, GFP_KERNEL);
+     params = kvzalloc(kbuf.bufsz, GFP_KERNEL);

Wondering how big the dtb blob is, can you explain a little bit about the kvzalloc usage here?

Quoted 3 lines

Except of this, I have no other concern about this patch.

And what's your plan about the user space kexec-tool change?

When I tested this earlier on x86, the DTB was allowed to be up to just under 64 pages large before the DTB failed to load. This is because it has to fit into an early_memremap() mapping (relevant code snippet at the bottom). Since the allocation can be many pages, I changed the kzalloc to a kvzalloc.

For the kexec-tools change, I have a draft change that I've already shared on this thread for testing purposes. I believe you said you were going to test it, but I haven't heard anything back from that yet. I'll raise that change for review properly once this kernel commit is in mainline.

---------

void __init x86_flattree_get_config(void)
{

#ifdef CONFIG_OF_EARLY_FLATTREE
	u32 size, map_len;
	void *dt;

	if (initial_dtb) {
		map_len = max(PAGE_SIZE - (initial_dtb & ~PAGE_MASK), (u64)128);

		dt = early_memremap(initial_dtb, map_len);
		size = fdt_totalsize(dt);
		if (map_len < size) {
			early_memunmap(dt, map_len);
			dt = early_memremap(initial_dtb, size);
			map_len = size;
		}

		early_init_dt_verify(dt, __pa(dt));
	}

	unflatten_and_copy_device_tree();

	if (initial_dtb)
		early_memunmap(dt, map_len);
#endif
	if (acpi_disabled && of_have_populated_dt())
		x86_init.mpparse.parse_smp_cfg = x86_dtb_parse_smp_config;
}

---------

Thanks,
Brian

author
#7 lore raw

Re: [PATCH v2 1/2] kexec: Add KEXEC_FILE_NO_CMA as a legal flag

Quoted 183 lines · Brian Mak
Quoted 6 lines · Baoquan He

Yeah, this is a good catch and great fix. Without this fix, kexec_file_load syscall will failed and return '-EINVAL' when KEXEC_FILE_NO_CMA is specified just as below code shows. So, for this patch,

Hi Baoquan,

Thanks for the ACK!

Quoted 2 lines

And, by the way, has the user space kexec-tools got the change merged to allow KEXEC_FILE_NO_CMA specified?

I don't see any recent commits to kexec-tools to support KEXEC_FILE_NO_CMA.

Quoted 117 lines
Quoted 108 lines

From: Brian Mak <[email protected]>
Subject: x86/kexec: carry forward the boot DTB on kexec
Date: Tue, 5 Aug 2025 14:15:27 -0700

Currently, the kexec_file_load syscall on x86 does not support passing a device tree blob to the new kernel. Some embedded x86 systems use device trees. On these systems, failing to pass a device tree to the new kernel causes a boot failure.

To add support for this, we copy the behavior of ARM64 and PowerPC and copy the current boot's device tree blob for use in the new kernel. We do this on x86 by passing the device tree blob as a setup_data entry in accordance with the x86 boot protocol.

This behavior is gated behind the KEXEC_FILE_FORCE_DTB flag.

---

arch/x86/kernel/kexec-bzimage64.c |   47 ++++++++++++++++++++++++++--
include/linux/kexec.h             |    5 ++
include/uapi/linux/kexec.h        |    4 ++
kernel/kexec_file.c               |    1
4 files changed, 53 insertions(+), 4 deletions(-)
arch/x86/kernel/kexec-bzimage64.c renamed from arch/x86/kernel/kexec-bzimage64.c~x86-kexec-carry-forward-the-boot-dtb-on-kexec +24 -0
@@ -16,6 +16,8 @@
 #include <linux/kexec.h>
 #include <linux/kernel.h>
 #include <linux/mm.h>
+#include <linux/libfdt.h>
+#include <linux/of_fdt.h>
 #include <linux/efi.h>
 #include <linux/random.h>
 
@@ -212,6 +214,28 @@ setup_efi_state(struct boot_params *para
 }
 #endif /* CONFIG_EFI */
 
+#ifdef CONFIG_OF_FLATTREE
+static void setup_dtb(struct boot_params *params,
+                   unsigned long params_load_addr,
+                   unsigned int dtb_setup_data_offset)
+{
+     struct setup_data *sd = (void *)params + dtb_setup_data_offset;
+     unsigned long setup_data_phys, dtb_len;
+
+     dtb_len = fdt_totalsize(initial_boot_params);
+     sd->type = SETUP_DTB;
+     sd->len = dtb_len;
+
+     /* Carry over current boot DTB with setup_data */
+     memcpy(sd->data, initial_boot_params, dtb_len);
+
+     /* Add setup data */
+     setup_data_phys = params_load_addr + dtb_setup_data_offset;
+     sd->next = params->hdr.setup_data;
+     params->hdr.setup_data = setup_data_phys;
+}
+#endif /* CONFIG_OF_FLATTREE */
+

static void
setup_ima_state(const struct kimage *image, struct boot_params *params,
unsigned long params_load_addr,

arch/x86/kernel/kexec-bzimage64.c renamed from arch/x86/kernel/kexec-bzimage64.c~x86-kexec-carry-forward-the-boot-dtb-on-kexec +17 -0
@@ -336,6 +360,17 @@ setup_boot_parameters(struct kimage *ima
                    sizeof(struct efi_setup_data);
 #endif
 
+#ifdef CONFIG_OF_FLATTREE
+     if (image->force_dtb && initial_boot_params) {
+             setup_dtb(params, params_load_addr, setup_data_offset);
+             setup_data_offset += sizeof(struct setup_data) +
+                                  fdt_totalsize(initial_boot_params);
+     } else {
+             pr_debug("Not carrying over DTB, force_dtb = %d\n",
+                      image->force_dtb);
+     }
+#endif
+
    if (IS_ENABLED(CONFIG_IMA_KEXEC)) {
            /* Setup IMA log buffer state */
            setup_ima_state(image, params, params_load_addr,
@@ -529,6 +564,12 @@ static void *bzImage64_load(struct kimag
                            sizeof(struct setup_data) +
                            RNG_SEED_LENGTH;
 
+#ifdef CONFIG_OF_FLATTREE
+     if (image->force_dtb && initial_boot_params)
+             kbuf.bufsz += sizeof(struct setup_data) +
+                           fdt_totalsize(initial_boot_params);
+#endif
+
    if (IS_ENABLED(CONFIG_IMA_KEXEC))
            kbuf.bufsz += sizeof(struct setup_data) +
                          sizeof(struct ima_setup_data);

arch/x86/kernel/kexec-bzimage64.c static void *bzImage64_load(struct kimag · line 581

Quoted 5 lines
arch/x86/kernel/kexec-bzimage64.c renamed from arch/x86/kernel/kexec-bzimage64.c~x86-kexec-carry-forward-the-boot-dtb-on-kexec +1 -1
@@ -537,7 +578,7 @@ static void *bzImage64_load(struct kimag
            kbuf.bufsz += sizeof(struct setup_data) +
                          sizeof(struct kho_data);
 
-     params = kzalloc(kbuf.bufsz, GFP_KERNEL);
+     params = kvzalloc(kbuf.bufsz, GFP_KERNEL);

Wondering how big the dtb blob is, can you explain a little bit about the kvzalloc usage here?

Quoted 3 lines

Except of this, I have no other concern about this patch.

And what's your plan about the user space kexec-tool change?

When I tested this earlier on x86, the DTB was allowed to be up to just under 64 pages large before the DTB failed to load. This is because it has to fit into an early_memremap() mapping (relevant code snippet at the bottom). Since the allocation can be many pages, I changed the kzalloc to a kvzalloc.

For the kexec-tools change, I have a draft change that I've already shared on this thread for testing purposes. I believe you said you were going to test it, but I haven't heard anything back from that yet. I'll raise that change for review properly once this kernel commit is in mainline.

---------

void __init x86_flattree_get_config(void)
{
#ifdef CONFIG_OF_EARLY_FLATTREE
u32 size, map_len;
void *dt;

if (initial_dtb) {
map_len = max(PAGE_SIZE - (initial_dtb & ~PAGE_MASK), (u64)128);

dt = early_memremap(initial_dtb, map_len);
size = fdt_totalsize(dt);
if (map_len < size) {
early_memunmap(dt, map_len);
dt = early_memremap(initial_dtb, size);
map_len = size;
}

early_init_dt_verify(dt, __pa(dt));
}

unflatten_and_copy_device_tree();

if (initial_dtb)
early_memunmap(dt, map_len);
#endif
if (acpi_disabled && of_have_populated_dt()) x86_init.mpparse.parse_smp_cfg = x86_dtb_parse_smp_config; }

Quoted 4 lines

---------

Thanks,
Brian

Hi Baoquan,

Just wanted to check back on this. Did you have any further concerns? If not, would you be able to provide an ACK?

Thanks,
Brian

Acked-by
#8 lore raw

Re: [PATCH v2 1/2] kexec: Add KEXEC_FILE_NO_CMA as a legal flag

Quoted 187 lines · Brian Mak
Quoted 185 lines · Brian Mak
Quoted 6 lines · Baoquan He

Yeah, this is a good catch and great fix. Without this fix, kexec_file_load syscall will failed and return '-EINVAL' when KEXEC_FILE_NO_CMA is specified just as below code shows. So, for this patch,

Hi Baoquan,

Thanks for the ACK!

Quoted 2 lines

And, by the way, has the user space kexec-tools got the change merged to allow KEXEC_FILE_NO_CMA specified?

I don't see any recent commits to kexec-tools to support KEXEC_FILE_NO_CMA.

Quoted 117 lines
Quoted 108 lines

From: Brian Mak <[email protected]>
Subject: x86/kexec: carry forward the boot DTB on kexec
Date: Tue, 5 Aug 2025 14:15:27 -0700

Currently, the kexec_file_load syscall on x86 does not support passing a device tree blob to the new kernel. Some embedded x86 systems use device trees. On these systems, failing to pass a device tree to the new kernel causes a boot failure.

To add support for this, we copy the behavior of ARM64 and PowerPC and copy the current boot's device tree blob for use in the new kernel. We do this on x86 by passing the device tree blob as a setup_data entry in accordance with the x86 boot protocol.

This behavior is gated behind the KEXEC_FILE_FORCE_DTB flag.

---

arch/x86/kernel/kexec-bzimage64.c |   47 ++++++++++++++++++++++++++--
include/linux/kexec.h             |    5 ++
include/uapi/linux/kexec.h        |    4 ++
kernel/kexec_file.c               |    1
4 files changed, 53 insertions(+), 4 deletions(-)
arch/x86/kernel/kexec-bzimage64.c renamed from arch/x86/kernel/kexec-bzimage64.c~x86-kexec-carry-forward-the-boot-dtb-on-kexec +24 -0
@@ -16,6 +16,8 @@
 #include <linux/kexec.h>
 #include <linux/kernel.h>
 #include <linux/mm.h>
+#include <linux/libfdt.h>
+#include <linux/of_fdt.h>
 #include <linux/efi.h>
 #include <linux/random.h>
 
@@ -212,6 +214,28 @@ setup_efi_state(struct boot_params *para
 }
 #endif /* CONFIG_EFI */
 
+#ifdef CONFIG_OF_FLATTREE
+static void setup_dtb(struct boot_params *params,
+                   unsigned long params_load_addr,
+                   unsigned int dtb_setup_data_offset)
+{
+     struct setup_data *sd = (void *)params + dtb_setup_data_offset;
+     unsigned long setup_data_phys, dtb_len;
+
+     dtb_len = fdt_totalsize(initial_boot_params);
+     sd->type = SETUP_DTB;
+     sd->len = dtb_len;
+
+     /* Carry over current boot DTB with setup_data */
+     memcpy(sd->data, initial_boot_params, dtb_len);
+
+     /* Add setup data */
+     setup_data_phys = params_load_addr + dtb_setup_data_offset;
+     sd->next = params->hdr.setup_data;
+     params->hdr.setup_data = setup_data_phys;
+}
+#endif /* CONFIG_OF_FLATTREE */
+

static void
setup_ima_state(const struct kimage *image, struct boot_params *params,
unsigned long params_load_addr,

arch/x86/kernel/kexec-bzimage64.c renamed from arch/x86/kernel/kexec-bzimage64.c~x86-kexec-carry-forward-the-boot-dtb-on-kexec +17 -0
@@ -336,6 +360,17 @@ setup_boot_parameters(struct kimage *ima
                    sizeof(struct efi_setup_data);
 #endif
 
+#ifdef CONFIG_OF_FLATTREE
+     if (image->force_dtb && initial_boot_params) {
+             setup_dtb(params, params_load_addr, setup_data_offset);
+             setup_data_offset += sizeof(struct setup_data) +
+                                  fdt_totalsize(initial_boot_params);
+     } else {
+             pr_debug("Not carrying over DTB, force_dtb = %d\n",
+                      image->force_dtb);
+     }
+#endif
+
    if (IS_ENABLED(CONFIG_IMA_KEXEC)) {
            /* Setup IMA log buffer state */
            setup_ima_state(image, params, params_load_addr,
@@ -529,6 +564,12 @@ static void *bzImage64_load(struct kimag
                            sizeof(struct setup_data) +
                            RNG_SEED_LENGTH;
 
+#ifdef CONFIG_OF_FLATTREE
+     if (image->force_dtb && initial_boot_params)
+             kbuf.bufsz += sizeof(struct setup_data) +
+                           fdt_totalsize(initial_boot_params);
+#endif
+
    if (IS_ENABLED(CONFIG_IMA_KEXEC))
            kbuf.bufsz += sizeof(struct setup_data) +
                          sizeof(struct ima_setup_data);

arch/x86/kernel/kexec-bzimage64.c static void *bzImage64_load(struct kimag · line 581

Quoted 5 lines
arch/x86/kernel/kexec-bzimage64.c renamed from arch/x86/kernel/kexec-bzimage64.c~x86-kexec-carry-forward-the-boot-dtb-on-kexec +1 -1
@@ -537,7 +578,7 @@ static void *bzImage64_load(struct kimag
            kbuf.bufsz += sizeof(struct setup_data) +
                          sizeof(struct kho_data);
 
-     params = kzalloc(kbuf.bufsz, GFP_KERNEL);
+     params = kvzalloc(kbuf.bufsz, GFP_KERNEL);

Wondering how big the dtb blob is, can you explain a little bit about the kvzalloc usage here?

Quoted 3 lines

Except of this, I have no other concern about this patch.

And what's your plan about the user space kexec-tool change?

When I tested this earlier on x86, the DTB was allowed to be up to just under 64 pages large before the DTB failed to load. This is because it has to fit into an early_memremap() mapping (relevant code snippet at the bottom). Since the allocation can be many pages, I changed the kzalloc to a kvzalloc.

For the kexec-tools change, I have a draft change that I've already shared on this thread for testing purposes. I believe you said you were going to test it, but I haven't heard anything back from that yet. I'll raise that change for review properly once this kernel commit is in mainline.

---------

void __init x86_flattree_get_config(void)
{
#ifdef CONFIG_OF_EARLY_FLATTREE
u32 size, map_len;
void *dt;

if (initial_dtb) {
map_len = max(PAGE_SIZE - (initial_dtb & ~PAGE_MASK), (u64)128);

dt = early_memremap(initial_dtb, map_len);
size = fdt_totalsize(dt);
if (map_len < size) {
early_memunmap(dt, map_len);
dt = early_memremap(initial_dtb, size);
map_len = size;
}

early_init_dt_verify(dt, __pa(dt));
}

unflatten_and_copy_device_tree();

if (initial_dtb)
early_memunmap(dt, map_len);
#endif
if (acpi_disabled && of_have_populated_dt()) x86_init.mpparse.parse_smp_cfg = x86_dtb_parse_smp_config; }

---------

Quoted 7 lines
Quoted 2 lines

Thanks,
Brian

Hi Baoquan,

Just wanted to check back on this. Did you have any further concerns? If not, would you be able to provide an ACK?

I thought I have acked it, seems I only ack-ed the 1st one, then ack the 2nd patch:

author
#9 lore raw

Re: [PATCH v2 1/2] kexec: Add KEXEC_FILE_NO_CMA as a legal flag

In reply to Andrew Morton
Quoted 9 lines · Andrew Morton
Quoted 7 lines · On Tue, 5 Aug 2025 14:15:26 -0700 Brian Mak <[email protected]> wrote:

Commit 07d24902977e ("kexec: enable CMA based contiguous allocation") introduces logic to use CMA-based allocation in kexec by default. As part of the changes, it introduces a kexec_file_load flag to disable the use of CMA allocations from userspace. However, this flag is broken since it is missing from the list of legal flags for kexec_file_load. kexec_file_load returns EINVAL when attempting to use the flag.

Quoted 7 lines
Quoted 4 lines

Fix this by adding the KEXEC_FILE_NO_CMA flag to the list of legal flags for kexec_file_load.

Fixes 07d24902977e ("kexec: enable CMA based contiguous allocation")

A description of the userspace-visible runtime effects of this bug would be very helpful, please. A lot more than "is broken"!

Hi Andrew,

Thanks for your comment. The userspace-visible runtime effect of this bug is that kexec_file_load returns EINVAL when attempting to use the KEXEC_FILE_NO_CMA flag. This is stated in the sentence following the "this flag is broken" statement.

Is there something else that you wanted me to add here?

Thanks,
Brian

author
#10 lore raw

PATCH v2 2/2 x86/kexec: Carry forward the boot DTB on kexec

In reply to Brian Mak

Currently, the kexec_file_load syscall on x86 does not support passing a device tree blob to the new kernel. Some embedded x86 systems use device trees. On these systems, failing to pass a device tree to the new kernel causes a boot failure.

To add support for this, we copy the behavior of ARM64 and PowerPC and copy the current boot's device tree blob for use in the new kernel. We do this on x86 by passing the device tree blob as a setup_data entry in accordance with the x86 boot protocol.

This behavior is gated behind the KEXEC_FILE_FORCE_DTB flag.

---
 arch/x86/kernel/kexec-bzimage64.c | 47 +++++++++++++++++++++++++++++--
 include/linux/kexec.h             |  5 +++-
 include/uapi/linux/kexec.h        |  4 +++
 kernel/kexec_file.c               |  1 +
 4 files changed, 53 insertions(+), 4 deletions(-)
arch/x86/kernel/kexec-bzimage64.c +44 -3
index 24a41f0e0cf1..c3244ac680d1 100644
@@ -16,6 +16,8 @@
 #include <linux/kexec.h>
 #include <linux/kernel.h>
 #include <linux/mm.h>
+#include <linux/libfdt.h>
+#include <linux/of_fdt.h>
 #include <linux/efi.h>
 #include <linux/random.h>
 
@@ -212,6 +214,28 @@ setup_efi_state(struct boot_params *params, unsigned long params_load_addr,
 }
 #endif /* CONFIG_EFI */
 
+#ifdef CONFIG_OF_FLATTREE
+static void setup_dtb(struct boot_params *params,
+		      unsigned long params_load_addr,
+		      unsigned int dtb_setup_data_offset)
+{
+	struct setup_data *sd = (void *)params + dtb_setup_data_offset;
+	unsigned long setup_data_phys, dtb_len;
+
+	dtb_len = fdt_totalsize(initial_boot_params);
+	sd->type = SETUP_DTB;
+	sd->len = dtb_len;
+
+	/* Carry over current boot DTB with setup_data */
+	memcpy(sd->data, initial_boot_params, dtb_len);
+
+	/* Add setup data */
+	setup_data_phys = params_load_addr + dtb_setup_data_offset;
+	sd->next = params->hdr.setup_data;
+	params->hdr.setup_data = setup_data_phys;
+}
+#endif /* CONFIG_OF_FLATTREE */
+
 static void
 setup_ima_state(const struct kimage *image, struct boot_params *params,
 		unsigned long params_load_addr,
@@ -336,6 +360,17 @@ setup_boot_parameters(struct kimage *image, struct boot_params *params,
 			sizeof(struct efi_setup_data);
 #endif
 
+#ifdef CONFIG_OF_FLATTREE
+	if (image->force_dtb && initial_boot_params) {
+		setup_dtb(params, params_load_addr, setup_data_offset);
+		setup_data_offset += sizeof(struct setup_data) +
+				     fdt_totalsize(initial_boot_params);
+	} else {
+		pr_debug("Not carrying over DTB, force_dtb = %d\n",
+			 image->force_dtb);
+	}
+#endif
+
 	if (IS_ENABLED(CONFIG_IMA_KEXEC)) {
 		/* Setup IMA log buffer state */
 		setup_ima_state(image, params, params_load_addr,
@@ -529,6 +564,12 @@ static void *bzImage64_load(struct kimage *image, char *kernel,
 				sizeof(struct setup_data) +
 				RNG_SEED_LENGTH;
 
+#ifdef CONFIG_OF_FLATTREE
+	if (image->force_dtb && initial_boot_params)
+		kbuf.bufsz += sizeof(struct setup_data) +
+			      fdt_totalsize(initial_boot_params);
+#endif
+
 	if (IS_ENABLED(CONFIG_IMA_KEXEC))
 		kbuf.bufsz += sizeof(struct setup_data) +
 			      sizeof(struct ima_setup_data);
@@ -537,7 +578,7 @@ static void *bzImage64_load(struct kimage *image, char *kernel,
 		kbuf.bufsz += sizeof(struct setup_data) +
 			      sizeof(struct kho_data);
 
-	params = kzalloc(kbuf.bufsz, GFP_KERNEL);
+	params = kvzalloc(kbuf.bufsz, GFP_KERNEL);
 	if (!params)
 		return ERR_PTR(-ENOMEM);
 	efi_map_offset = params_cmdline_sz;
@@ -647,7 +688,7 @@ static void *bzImage64_load(struct kimage *image, char *kernel,
 	return ldata;
 
 out_free_params:
-	kfree(params);
+	kvfree(params);
 	return ERR_PTR(ret);
 }
 
@@ -659,7 +700,7 @@ static int bzImage64_cleanup(void *loader_data)
 	if (!ldata)
 		return 0;
 
-	kfree(ldata->bootparams_buf);
+	kvfree(ldata->bootparams_buf);
 	ldata->bootparams_buf = NULL;
 
 	return 0;
include/linux/kexec.h +4 -1
index 39fe3e6cd282..ff7e231b0485 100644
@@ -395,6 +395,9 @@ struct kimage {
 
 	/* Information for loading purgatory */
 	struct purgatory_info purgatory_info;
+
+	/* Force carrying over the DTB from the current boot */
+	bool force_dtb;
 #endif
 
 #ifdef CONFIG_CRASH_HOTPLUG
@@ -461,7 +464,7 @@ bool kexec_load_permitted(int kexec_image_type);
 /* List of defined/legal kexec file flags */
 #define KEXEC_FILE_FLAGS	(KEXEC_FILE_UNLOAD | KEXEC_FILE_ON_CRASH | \
 				 KEXEC_FILE_NO_INITRAMFS | KEXEC_FILE_DEBUG | \
-				 KEXEC_FILE_NO_CMA)
+				 KEXEC_FILE_NO_CMA | KEXEC_FILE_FORCE_DTB)
 
 /* flag to track if kexec reboot is in progress */
 extern bool kexec_in_progress;
include/uapi/linux/kexec.h +4 -0
index 8958ebfcff94..55749cb0b81d 100644
@@ -22,12 +22,16 @@
  * KEXEC_FILE_ON_CRASH : Load/unload operation belongs to kdump image.
  * KEXEC_FILE_NO_INITRAMFS : No initramfs is being loaded. Ignore the initrd
  *                           fd field.
+ * KEXEC_FILE_FORCE_DTB : Force carrying over the current boot's DTB to the new
+ *                        kernel on x86. This is already the default behavior on
+ *                        some other architectures, like ARM64 and PowerPC.
  */
 #define KEXEC_FILE_UNLOAD	0x00000001
 #define KEXEC_FILE_ON_CRASH	0x00000002
 #define KEXEC_FILE_NO_INITRAMFS	0x00000004
 #define KEXEC_FILE_DEBUG	0x00000008
 #define KEXEC_FILE_NO_CMA	0x00000010
+#define KEXEC_FILE_FORCE_DTB	0x00000020
 
 /* These values match the ELF architecture values.
  * Unless there is a good reason that should continue to be the case.
kernel/kexec_file.c +1 -0
index 91d46502a817..eb62a9794242 100644
@@ -255,6 +255,7 @@ kimage_file_prepare_segments(struct kimage *image, int kernel_fd, int initrd_fd,
 	}
 
 	image->no_cma = !!(flags & KEXEC_FILE_NO_CMA);
+	image->force_dtb = flags & KEXEC_FILE_FORCE_DTB;
 
 	if (cmdline_len) {
 		image->cmdline_buf = memdup_user(cmdline_ptr, cmdline_len);
Signature · 1 line
2.25.1
author
#11 lore raw
In reply to Brian Mak
Quoted 20 lines · Brian Mak

Hi all,

Here is v2 with the comments from v1 addressed. I also added a patch to the series to add KEXEC_FILE_NO_CMA as a legal flag. I noticed that this was missing when I went to add my KEXEC_FILE_FORCE_DTB flag.

Thanks,
Brian

Changes in v2:
- Added a patch to add KEXEC_FILE_NO_CMA as a legal flag
- Added a KEXEC_FILE_FORCE_DTB flag to enable carrying over the current boot's DTB on x86.
- Modified the commit message to include more reasoning for the change.
- Changed a pr_info print to a pr_debug print.

Brian Mak (2):
kexec: Add KEXEC_FILE_NO_CMA as a legal flag
x86/kexec: Carry forward the boot DTB on kexec

Quoted 10 lines
arch/x86/kernel/kexec-bzimage64.c | 47 +++++++++++++++++++++++++++++--
include/linux/kexec.h             |  6 +++-
include/uapi/linux/kexec.h        |  4 +++
kernel/kexec_file.c               |  1 +
4 files changed, 54 insertions(+), 4 deletions(-)

base-commit: 35a813e010b99894bb4706c56c16a580bf7959c2

Signature · 1 line
2.25.1

I see Andrew has sent these patches to linux-next. Thanks for that, Andrew!

Dave and Baoquan, have you two had a chance to peek at the updated patch set yet?

Thanks,
Brian

#12 lore raw
Quoted 34 lines · Brian Mak
Quoted 27 lines · Brian Mak

Hi all,

Here is v2 with the comments from v1 addressed. I also added a patch to the series to add KEXEC_FILE_NO_CMA as a legal flag. I noticed that this was missing when I went to add my KEXEC_FILE_FORCE_DTB flag.

Thanks,
Brian

Changes in v2:
- Added a patch to add KEXEC_FILE_NO_CMA as a legal flag
- Added a KEXEC_FILE_FORCE_DTB flag to enable carrying over the current boot's DTB on x86.
- Modified the commit message to include more reasoning for the change.
- Changed a pr_info print to a pr_debug print.

Brian Mak (2):
kexec: Add KEXEC_FILE_NO_CMA as a legal flag
x86/kexec: Carry forward the boot DTB on kexec

arch/x86/kernel/kexec-bzimage64.c | 47 +++++++++++++++++++++++++++++--
include/linux/kexec.h             |  6 +++-
include/uapi/linux/kexec.h        |  4 +++
kernel/kexec_file.c               |  1 +
4 files changed, 54 insertions(+), 4 deletions(-)
Quoted 3 lines

base-commit: 35a813e010b99894bb4706c56c16a580bf7959c2 --
2.25.1

Quoted 5 lines

I see Andrew has sent these patches to linux-next. Thanks for that, Andrew!

Dave and Baoquan, have you two had a chance to peek at the updated patch set yet?

Hi, The #ifdef CONFIG_* which can be replaced by #if defined(), but I do not have a strong opinion. other than that do you have kexec-tools patch ready? It would be better to provide a link so that people can try and test it.

Thanks
Dave

author
#13 lore raw
Quoted 4 lines · Dave Young

Hi, The #ifdef CONFIG_* which can be replaced by #if defined(), but I do not have a strong opinion. other than that do you have kexec-tools patch ready? It would be better to provide a link so that people can try and test it.

Hi Dave,

Thanks for looking at this! The #ifdef CONFIG_* is used, as it matches the style of other ifdefs in this file. I'd like to keep it as-is for consistency.

I do have a prototype kexec-tools patch ready for testing. The changes can be found at this repo:

https://github.com/makb-juniper/kexec-tools.git

I've also placed the diff below.

Thanks,
Brian

kexec/kexec-syscall.h +2 -0
index 9b17578..8419b23 100644
@@ -124,6 +124,8 @@ static inline long kexec_file_load(int kernel_fd, int initrd_fd,
 #define KEXEC_FILE_ON_CRASH	0x00000002
 #define KEXEC_FILE_NO_INITRAMFS	0x00000004
 #define KEXEC_FILE_DEBUG	0x00000008
+#define KEXEC_FILE_NO_CMA	0x00000010
+#define KEXEC_FILE_FORCE_DTB	0x00000020
 
 /* These values match the ELF architecture values. 
  * Unless there is a good reason that should continue to be the case.
kexec/kexec.c +4 -0
index 6bf12d7..0e13b6b 100644
@@ -1102,6 +1102,7 @@ void usage(void)
 	       " --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"
+	       " --force-dtb          Carry over the current boot's device tree blob (x86 only).\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"
@@ -1640,6 +1641,9 @@ int main(int argc, char *argv[])
 		case OPT_HOTPLUG:
 			do_hotplug = 1;
 			break;
+		case OPT_FORCE_DTB:
+			kexec_file_flags |= KEXEC_FILE_FORCE_DTB;
+			break;
 		default:
 			break;
 		}
kexec/kexec.h +3 -1
index a2e19c4..2235aa2 100644
@@ -235,7 +235,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_FORCE_DTB		266
+#define OPT_MAX		267
 #define KEXEC_OPTIONS \
 	{ "help",		0, 0, OPT_HELP }, \
 	{ "version",		0, 0, OPT_VERSION }, \
@@ -263,6 +264,7 @@ extern int file_types;
 	{ "status",		0, 0, OPT_STATUS }, \
 	{ "print-ckr-size",     0, 0, OPT_PRINT_CKR_SIZE }, \
 	{ "hotplug",		    0, 0, OPT_HOTPLUG }, \
+	{ "force-dtb",          0, 0, OPT_FORCE_DTB }, \
 
 #define KEXEC_OPT_STR "h?vdfixyluet:pscaS"
 

base-commit: daa29443819d3045338792b5ba950ed90e79d7a5

Signature · 1 line
2.25.1
#14 lore raw
Quoted 13 lines · Brian Mak
Quoted 4 lines · Dave Young

Hi, The #ifdef CONFIG_* which can be replaced by #if defined(), but I do not have a strong opinion. other than that do you have kexec-tools patch ready? It would be better to provide a link so that people can try and test it.

Hi Dave,

Thanks for looking at this! The #ifdef CONFIG_* is used, as it matches the style of other ifdefs in this file. I'd like to keep it as-is for consistency.

Quoted 6 lines

I do have a prototype kexec-tools patch ready for testing. The changes can be found at this repo:

https://github.com/makb-juniper/kexec-tools.git

I've also placed the diff below.

Thanks, I will apply your patch and take a test.

Usually, Andrew will merge kexec/kdump patch if the patch is OK to him. I will add comment if I have concern. Otherwise, I don't want Andrew to bother to add my ACK.

Quoted 63 lines
kexec/kexec-syscall.h +2 -0
index 9b17578..8419b23 100644
@@ -124,6 +124,8 @@ static inline long kexec_file_load(int kernel_fd, int initrd_fd,
 #define KEXEC_FILE_ON_CRASH	0x00000002
 #define KEXEC_FILE_NO_INITRAMFS	0x00000004
 #define KEXEC_FILE_DEBUG	0x00000008
+#define KEXEC_FILE_NO_CMA	0x00000010
+#define KEXEC_FILE_FORCE_DTB	0x00000020
 
 /* These values match the ELF architecture values. 
  * Unless there is a good reason that should continue to be the case.
kexec/kexec.c +4 -0
index 6bf12d7..0e13b6b 100644
@@ -1102,6 +1102,7 @@ void usage(void)
 	       " --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"
+	       " --force-dtb          Carry over the current boot's device tree blob (x86 only).\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"
@@ -1640,6 +1641,9 @@ int main(int argc, char *argv[])
 		case OPT_HOTPLUG:
 			do_hotplug = 1;
 			break;
+		case OPT_FORCE_DTB:
+			kexec_file_flags |= KEXEC_FILE_FORCE_DTB;
+			break;
 		default:
 			break;
 		}
kexec/kexec.h +3 -1
index a2e19c4..2235aa2 100644
@@ -235,7 +235,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_FORCE_DTB		266
+#define OPT_MAX		267
 #define KEXEC_OPTIONS \
 	{ "help",		0, 0, OPT_HELP }, \
 	{ "version",		0, 0, OPT_VERSION }, \
@@ -263,6 +264,7 @@ extern int file_types;
 	{ "status",		0, 0, OPT_STATUS }, \
 	{ "print-ckr-size",     0, 0, OPT_PRINT_CKR_SIZE }, \
 	{ "hotplug",		    0, 0, OPT_HOTPLUG }, \
+	{ "force-dtb",          0, 0, OPT_FORCE_DTB }, \
 
 #define KEXEC_OPT_STR "h?vdfixyluet:pscaS"
 

base-commit: daa29443819d3045338792b5ba950ed90e79d7a5

Signature · 1 line
2.25.1