Skip to contentSkip to conversation

kernel: Introduce multikernel architecture support

RFC PATCH New Cong Wang 2 participants 20 messages Oct 19, 2025

Subsystems (8)
lore
Share & tools
lore

Cong Wang

author
#0 lore raw

v2 cover kernel: Introduce multikernel architecture support

This patch series introduces multikernel architecture support, enabling multiple independent kernel instances to coexist and communicate on a single physical machine. Each kernel instance can run on dedicated CPU cores while sharing the underlying hardware resources.

The multikernel architecture provides several key benefits:
- Better resource utilization than traditional VM (KVM, Xen etc.)
- Better performance than containers without noisy neighbor issues
- Improved fault isolation between different kernels
- Potential zero-down kernel update with KHO (Kernel HandOver)

Architecture Overview:
The implementation leverages kexec infrastructure to load and manage multiple kernel images, with each kernel instance assigned to specific CPU cores. Inter-kernel communication is facilitated through a dedicated IPI framework that allows kernels to coordinate and share information when necessary.

Key Components:
1. Enhanced kexec subsystem with dynamic kimage tracking
2. Generic physical memory allocation for multikernel and kernel instances
3. Multikernel-specific kernfs for managing kernel instances
4. Device-tree based mechanism for static resource allocation
5. Device-tree overlay based mechanism for dynamic resource allation
6. KHO based mechanism for device-tree sharing between kernels
7. Generic IPI communication framework for inter-kernel messaging
8. Proc interface (/proc/kimage) for monitoring loaded kernel images

The implementation maintains full backward compatibility with existing kexec functionality while adding the new multikernel capabilities.

For the complete roadmap, please see:
https://docs.google.com/document/d/1yneO6O6C_z0Lh3A2QyT8XsH7ZrQ7-naGQT-rpdjWa_g/

For the Proof-of-Concept demo (prior to device-tree), please see:
https://www.youtube.com/watch?v=bmC4JRTSDKE

For user-space components, especially device-tree specification, please check kerf project: https://github.com/multikernel/kerf

IMPORTANT NOTES:

1) This is a Request for Comments (RFC) submission. While the core architecture is functional, there are numerous implementation details that need improvement. The primary goal is to gather feedback on the high-level design and overall approach rather than focus on specific coding details at this stage.

2) This patch series represents only the foundational framework for multikernel support. It establishes the basic infrastructure and communication mechanisms. We welcome the community to build upon this foundation and develop their own solutions based on this framework.

3) Testing has been limited to the author's development machine using hard-coded boot parameters and specific hardware configurations. Community testing across different hardware platforms, configurations, and use cases would be greatly appreciated to identify potential issues and improve robustness. Obviously, don't use this code beyond testing.

Changes since RFC V1:
- Switched from kexec_load() to kexec_file_load()
- Introduced generic multikernel physical memory allocation and per-instance virtual memory allocation
- Introduced kernfs interface for managing kernel instances
- Introduced device-tree and KHO framework for resource management and sharing
- Introduced messaging over IPI framework, preparing for dynamic resource allocation and zero-downtime upgrade
- Grouped multikernel code in the kernel/multikernel/ directory
- Reorganized and rebased all the patches

---

Cong Wang (16):
kexec: Introduce multikernel support via kexec
x86: Introduce SMP INIT trampoline for multikernel CPU bootstrap
multikernel: Introduce basic multikernel subsystem infrastructure
x86: Introduce MULTIKERNEL_VECTOR for inter-kernel communication
x86: Introduce arch_cpu_physical_id() to obtain physical CPU ID
multikernel: Introduce physical memory reservation and allocation
kexec: Implement dynamic kimage tracking
multikernel: Introduce device-tree based kernfs interface
kexec: Integrate multikernel instance management with kexec subsystem
Documentation: Add multikernel usage
kexec: Add /proc/kimage interface for kimage tracking
multikernel: Introduce per-instance memory allocation interface
kernel: Introduce generic multikernel IPI communication framework
multikernel: Add messaging layer for inter-kernel communication
kexec: Integrate multikernel support with kexec_file_load()
multikernel: Integrate Kexec HandOver framework for DTB preservation

 Documentation/multikernel/usage.rst | 215 ++++++++
 arch/powerpc/kexec/crash.c          |   8 +-
 arch/x86/include/asm/idtentry.h     |   3 +
 arch/x86/include/asm/irq_vectors.h  |   1 +
 arch/x86/include/asm/smp.h          |   7 +
 arch/x86/kernel/Makefile            |   1 +
 arch/x86/kernel/crash.c             |   4 +-
 arch/x86/kernel/head64.c            |   5 +
 arch/x86/kernel/idt.c               |   3 +
 arch/x86/kernel/kexec-bzimage64.c   |  16 +-
 arch/x86/kernel/setup.c             |  11 +-
 arch/x86/kernel/smp.c               |  17 +
 arch/x86/kernel/smpboot.c           | 159 ++++++
 arch/x86/kernel/trampoline_64_bsp.S | 288 +++++++++++
 arch/x86/kernel/vmlinux.lds.S       |   6 +
 drivers/of/kexec.c                  |  20 +-
 include/linux/kexec.h               |  30 +-
 include/linux/kexec_handover.h      |  40 ++
 include/linux/multikernel.h         | 659 ++++++++++++++++++++++++
 include/uapi/linux/kexec.h          |   5 +
 include/uapi/linux/reboot.h         |   2 +-
 kernel/Kconfig.kexec                |   2 +
 kernel/Makefile                     |   1 +
 kernel/kexec.c                      |  60 ++-
 kernel/kexec_core.c                 | 384 ++++++++++++++
 kernel/kexec_file.c                 | 158 +++++-
 kernel/kexec_handover.c             | 197 ++++++-
 kernel/multikernel/Kconfig          |  26 +
 kernel/multikernel/Makefile         |   9 +
 kernel/multikernel/core.c           | 532 +++++++++++++++++++
 kernel/multikernel/dts.c            | 466 +++++++++++++++++
 kernel/multikernel/internal.h       |   4 +
 kernel/multikernel/ipi.c            | 471 +++++++++++++++++
 kernel/multikernel/kernfs.c         | 772 ++++++++++++++++++++++++++++
 kernel/multikernel/kho.c            | 356 +++++++++++++
 kernel/multikernel/mem.c            | 376 ++++++++++++++
 kernel/multikernel/messaging.c      | 278 ++++++++++
 kernel/reboot.c                     |  10 +
 38 files changed, 5572 insertions(+), 30 deletions(-)
 create mode 100644 Documentation/multikernel/usage.rst
 create mode 100644 arch/x86/kernel/trampoline_64_bsp.S
 create mode 100644 include/linux/multikernel.h
 create mode 100644 kernel/multikernel/Kconfig
 create mode 100644 kernel/multikernel/Makefile
 create mode 100644 kernel/multikernel/core.c
 create mode 100644 kernel/multikernel/dts.c
 create mode 100644 kernel/multikernel/internal.h
 create mode 100644 kernel/multikernel/ipi.c
 create mode 100644 kernel/multikernel/kernfs.c
 create mode 100644 kernel/multikernel/kho.c
 create mode 100644 kernel/multikernel/mem.c
 create mode 100644 kernel/multikernel/messaging.c
Signature · 1 line
2.34.1
author
#1 lore raw

RFC PATCH v2 1/16 kexec: Introduce multikernel support via kexec

From: Cong Wang <[email protected]>

This patch extends the kexec subsystem to support multikernel functionality, allowing different kernel instances to be loaded and executed on specific CPUs. The implementation introduces:

- New KEXEC_TYPE_MULTIKERNEL type and KEXEC_MULTIKERNEL flag

- multikernel_kick_ap() function for CPU-specific kernel booting

- LINUX_REBOOT_CMD_MULTIKERNEL reboot command with CPU parameter

- Specialized segment loading for multikernel images using memremap

- Integration with existing kexec infrastructure while bypassing standard machine_kexec_prepare() for avoiding resets

The multikernel_kexec() function validates CPU availability and uses the existing kexec image start address to boot the target CPU with a different kernel instance. This enables heterogeneous computing scenarios where different CPUs can run specialized kernel variants.

---
 arch/x86/include/asm/smp.h  |   1 +
 arch/x86/kernel/smpboot.c   | 104 ++++++++++++++++++++++++++++++++++
 include/linux/kexec.h       |   6 +-
 include/uapi/linux/kexec.h  |   1 +
 include/uapi/linux/reboot.h |   2 +-
 kernel/kexec_core.c         | 109 ++++++++++++++++++++++++++++++++++++
 kernel/reboot.c             |  10 ++++
 7 files changed, 230 insertions(+), 3 deletions(-)
arch/x86/include/asm/smp.h +1 -0
index 22bfebe6776d..1a59fd0de759 100644
@@ -107,6 +107,7 @@ void native_smp_prepare_cpus(unsigned int max_cpus);
 void native_smp_cpus_done(unsigned int max_cpus);
 int common_cpu_up(unsigned int cpunum, struct task_struct *tidle);
 int native_kick_ap(unsigned int cpu, struct task_struct *tidle);
+int multikernel_kick_ap(unsigned int cpu, unsigned long kernel_start_address);
 int native_cpu_disable(void);
 void __noreturn hlt_play_dead(void);
 void native_play_dead(void);
arch/x86/kernel/smpboot.c +104 -0

116 lines not on this page · raw message

include/linux/kexec.h +4 -2
index ff7e231b0485..edf64bc98ed5 100644
@@ -358,9 +358,10 @@ struct kimage {
 	unsigned long control_page;
 
 	/* Flags to indicate special processing */
-	unsigned int type : 1;
+	unsigned int type : 2;
 #define KEXEC_TYPE_DEFAULT 0
 #define KEXEC_TYPE_CRASH   1
+#define KEXEC_TYPE_MULTIKERNEL 2
 	unsigned int preserve_context : 1;
 	/* If set, we are using file mode kexec syscall */
 	unsigned int file_mode:1;
@@ -437,6 +438,7 @@ extern void machine_kexec(struct kimage *image);
 extern int machine_kexec_prepare(struct kimage *image);
 extern void machine_kexec_cleanup(struct kimage *image);
 extern int kernel_kexec(void);
+extern int multikernel_kexec(int cpu);
 extern struct page *kimage_alloc_control_pages(struct kimage *image,
 						unsigned int order);
 
@@ -458,7 +460,7 @@ bool kexec_load_permitted(int kexec_image_type);
 #define KEXEC_FLAGS    (KEXEC_ON_CRASH | KEXEC_UPDATE_ELFCOREHDR | KEXEC_CRASH_HOTPLUG_SUPPORT)
 #else
 #define KEXEC_FLAGS    (KEXEC_ON_CRASH | KEXEC_PRESERVE_CONTEXT | KEXEC_UPDATE_ELFCOREHDR | \
-			KEXEC_CRASH_HOTPLUG_SUPPORT)
+			KEXEC_CRASH_HOTPLUG_SUPPORT | KEXEC_MULTIKERNEL)
 #endif
 
 /* List of defined/legal kexec file flags */
include/uapi/linux/kexec.h +1 -0
index 55749cb0b81d..346e0ff4e663 100644
@@ -14,6 +14,7 @@
 #define KEXEC_PRESERVE_CONTEXT	0x00000002
 #define KEXEC_UPDATE_ELFCOREHDR	0x00000004
 #define KEXEC_CRASH_HOTPLUG_SUPPORT 0x00000008
+#define KEXEC_MULTIKERNEL	0x00000010
 #define KEXEC_ARCH_MASK		0xffff0000
 
 /*
include/uapi/linux/reboot.h +1 -1
index 58e64398efc5..aac2f2f94a98 100644
@@ -34,7 +34,7 @@
 #define	LINUX_REBOOT_CMD_RESTART2	0xA1B2C3D4
 #define	LINUX_REBOOT_CMD_SW_SUSPEND	0xD000FCE2
 #define	LINUX_REBOOT_CMD_KEXEC		0x45584543
-
+#define	LINUX_REBOOT_CMD_MULTIKERNEL	0x4D4B4C49
 
 
 #endif /* _UAPI_LINUX_REBOOT_H */
kernel/kexec_core.c +109 -0

130 lines not on this page · raw message

kernel/reboot.c +10 -0
index ec087827c85c..f3ac703c4695 100644
@@ -717,6 +717,10 @@ EXPORT_SYMBOL_GPL(kernel_power_off);
 
 DEFINE_MUTEX(system_transition_mutex);
 
+struct multikernel_boot_args {
+	int cpu;
+};
+
 /*
  * Reboot system call: for obvious reasons only root may call it,
  * and even root needs to set up some magic numbers in the registers
@@ -729,6 +733,7 @@ SYSCALL_DEFINE4(reboot, int, magic1, int, magic2, unsigned int, cmd,
 		void __user *, arg)
 {
 	struct pid_namespace *pid_ns = task_active_pid_ns(current);
+	struct multikernel_boot_args boot_args;
 	char buffer[256];
 	int ret = 0;
 
@@ -799,6 +804,11 @@ SYSCALL_DEFINE4(reboot, int, magic1, int, magic2, unsigned int, cmd,
 	case LINUX_REBOOT_CMD_KEXEC:
 		ret = kernel_kexec();
 		break;
+	case LINUX_REBOOT_CMD_MULTIKERNEL:
+		if (copy_from_user(&boot_args, arg, sizeof(boot_args)))
+			return -EFAULT;
+		ret = multikernel_kexec(boot_args.cpu);
+		break;
 #endif
 
 #ifdef CONFIG_HIBERNATION
Signature · 1 line
2.34.1
#2 lore raw

Re: [RFC Patch v2 01/16] kexec: Introduce multikernel support via kexec

kernel test robot noticed the following build errors:

Hi Cong,

[This is a private test report for your RFC patch.] kernel test robot noticed the following build errors:

[auto build test ERROR on tip/x86/core]
[also build test ERROR on powerpc/next powerpc/fixes robh/for-next linus/master v6.18-rc1 next-20251017] [If your patch is applied to the wrong git tree, kindly drop us a note.
And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch#_base_tree_information]

url:    https://github.com/intel-lab-lkp/linux/commits/Cong-Wang/kexec-Introduce-multikernel-support-via-kexec/20251019-141940
base:   tip/x86/core
patch link:    https://lore.kernel.org/r/20251019061631.2235405-2-xiyou.wangcong%40gmail.com
patch subject: [RFC Patch v2 01/16] kexec: Introduce multikernel support via kexec
config: x86_64-buildonly-randconfig-002-20251019 (https://download.01.org/0day-ci/archive/20251019/[email protected]/config)
compiler: gcc-13 (Debian 13.3.0-16) 13.3.0
reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20251019/[email protected]/reproduce)

If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/

All errors (new ones prefixed by >>):

kernel/kexec_core.c: In function 'multikernel_kexec':

>> kernel/kexec_core.c:1334:14: error: implicit declaration of function 'multikernel_kick_ap'; did you mean 'multikernel_kexec'? [-Werror=implicit-function-declaration]
    1334 |         rc = multikernel_kick_ap(cpu, kexec_image->start);
         |              ^~~~~~~~~~~~~~~~~~~
         |              multikernel_kexec

cc1: some warnings being treated as errors

vim +1334 kernel/kexec_core.c

1314
1315 int multikernel_kexec(int cpu)
1316 {
1317 int rc;
1318
1319 pr_info("multikernel kexec: cpu %d\n", cpu);
1320
1321 if (cpu_online(cpu)) {
1322 pr_err("The CPU is currently running with this kernel instance.");
1323 return -EBUSY;
1324 }
1325
1326 if (!kexec_trylock())
1327 return -EBUSY;
1328 if (!kexec_image) {
1329 rc = -EINVAL;
1330 goto unlock;
1331 }
1332
1333 cpus_read_lock();

> 1334		rc = multikernel_kick_ap(cpu, kexec_image->start);
Signature · 2 lines
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki
author
#3 lore raw

RFC PATCH v2 2/16 x86: Introduce SMP INIT trampoline for multikernel CPU bootstrap

In reply to Cong Wang

From: Cong Wang <[email protected]>

This patch introduces a dedicated trampoline mechanism for booting secondary CPUs with different kernel instances in multikernel mode. The implementation provides:

- New trampoline_64_bsp.S assembly code for real-mode to long-mode transition when launching kernels on secondary CPUs
- Trampoline memory allocation and setup in low memory (<1MB) for real-mode execution compatibility
- Page table construction for identity mapping during CPU bootstrap
- Integration with existing multikernel kexec infrastructure

The trampoline handles the complete CPU initialization sequence from 16-bit real mode through 32-bit protected mode to 64-bit long mode, setting up appropriate GDT, page tables, and control registers before jumping to the target kernel entry point without resetting the whole system or the running kernel.

Note: This implementation uses legacy assembly-based trampoline code and should be migrated to C-based x86 trampoline in future updates.

---
 arch/x86/kernel/Makefile            |   1 +
 arch/x86/kernel/head64.c            |   5 +
 arch/x86/kernel/setup.c             |   3 +
 arch/x86/kernel/smpboot.c           |  85 ++++++--
 arch/x86/kernel/trampoline_64_bsp.S | 288 ++++++++++++++++++++++++++++
 arch/x86/kernel/vmlinux.lds.S       |   6 +
 6 files changed, 373 insertions(+), 15 deletions(-)
 create mode 100644 arch/x86/kernel/trampoline_64_bsp.S
arch/x86/kernel/Makefile +1 -0
index bc184dd38d99..49ff8272c888 100644
@@ -50,6 +50,7 @@ CFLAGS_irq.o := -I $(src)/../include/asm/trace
 
 obj-y			+= head_$(BITS).o
 obj-y			+= head$(BITS).o
+obj-y			+= trampoline_64_bsp.o
 obj-y			+= ebda.o
 obj-y			+= platform-quirks.o
 obj-y			+= process_$(BITS).o signal.o signal_$(BITS).o
arch/x86/kernel/head64.c +5 -0
index fd28b53dbac5..60005472facb 100644
@@ -219,6 +219,9 @@ static void __init copy_bootdata(char *real_mode_data)
 	sme_unmap_bootdata(real_mode_data);
 }
 
+unsigned long orig_boot_params;
+EXPORT_SYMBOL(orig_boot_params);
+
 asmlinkage __visible void __init __noreturn x86_64_start_kernel(char * real_mode_data)
 {
 	/*
@@ -288,6 +291,8 @@ asmlinkage __visible void __init __noreturn x86_64_start_kernel(char * real_mode
 	/* set init_top_pgt kernel high mapping*/
 	init_top_pgt[511] = early_top_pgt[511];
 
+	orig_boot_params = (unsigned long) real_mode_data;
+
 	x86_64_start_reservations(real_mode_data);
 }
 
arch/x86/kernel/setup.c +3 -0
index 1b2edd07a3e1..8342c4e46bad 100644
@@ -877,6 +877,8 @@ static void __init x86_report_nx(void)
  * Note: On x86_64, fixmaps are ready for use even before this is called.
  */
 
+extern void __init setup_trampolines_bsp(void);
+
 void __init setup_arch(char **cmdline_p)
 {
 #ifdef CONFIG_X86_32
@@ -1103,6 +1105,7 @@ void __init setup_arch(char **cmdline_p)
 			(max_pfn_mapped<<PAGE_SHIFT) - 1);
 #endif
 
+	setup_trampolines_bsp();
 	/*
 	 * Find free memory for the real mode trampoline and place it there. If
 	 * there is not enough free memory under 1M, on EFI-enabled systems
arch/x86/kernel/smpboot.c +70 -15

109 lines not on this page · raw message

arch/x86/kernel/trampoline_64_bsp.S new file +288 -0

288 lines not on this page · raw message

arch/x86/kernel/vmlinux.lds.S +6 -0
index d7af4a64c211..38e16ed2b3fb 100644
@@ -228,6 +228,12 @@ SECTIONS
 
 	INIT_DATA_SECTION(16)
 
+         .x86_trampoline_bsp : AT(ADDR(.x86_trampoline_bsp) - LOAD_OFFSET) {
+                x86_trampoline_bsp_start = .;
+                *(.x86_trampoline_bsp)
+                x86_trampoline_bsp_end = .;
+        }
+
 	.x86_cpu_dev.init : AT(ADDR(.x86_cpu_dev.init) - LOAD_OFFSET) {
 		__x86_cpu_dev_start = .;
 		*(.x86_cpu_dev.init)
Signature · 1 line
2.34.1
author
#4 lore raw

RFC PATCH v2 3/16 multikernel: Introduce basic multikernel subsystem infrastructure

In reply to Cong Wang

From: Cong Wang <[email protected]>

This commit introduces:

* Configuration infrastructure (kernel/multikernel/Kconfig) that adds CONFIG_MULTIKERNEL option depending on KEXEC_CORE, it will provide kernfs interface for multikernel instance management, device tree based resource management, physical memory pool allocation, and kexec integration.

* Core initialization module (kernel/multikernel/core.c) that provides basic subsystem initialization using subsys_initcall() to ensure multikernel support is initialized after core kernel subsystems.

This foundational commit establishes the basic framework that subsequent patches will build upon to implement the full multikernel functionality.

---
 kernel/Kconfig.kexec        |  2 ++
 kernel/Makefile             |  1 +
 kernel/multikernel/Kconfig  | 20 ++++++++++++++++++++
 kernel/multikernel/Makefile |  6 ++++++
 kernel/multikernel/core.c   | 17 +++++++++++++++++
 5 files changed, 46 insertions(+)
 create mode 100644 kernel/multikernel/Kconfig
 create mode 100644 kernel/multikernel/Makefile
 create mode 100644 kernel/multikernel/core.c
kernel/Kconfig.kexec +2 -0
index 422270d64820..e0fbd7e9af43 100644
@@ -194,4 +194,6 @@ config CRASH_MAX_MEMORY_RANGES
 	  the computation behind the value provided through the
 	  /sys/kernel/crash_elfcorehdr_size attribute.
 
+source "kernel/multikernel/Kconfig"
+
 endmenu
kernel/Makefile +1 -0
index df3dd8291bb6..017ed567f86a 100644
@@ -56,6 +56,7 @@ obj-y += dma/
 obj-y += entry/
 obj-y += unwind/
 obj-$(CONFIG_MODULES) += module/
+obj-$(CONFIG_MULTIKERNEL) += multikernel/
 
 obj-$(CONFIG_KCMP) += kcmp.o
 obj-$(CONFIG_FREEZER) += freezer.o
kernel/multikernel/Kconfig new file +20 -0
new file mode 100644
index 000000000000..0e61fd2e505a
@@ -0,0 +1,20 @@
+# SPDX-License-Identifier: GPL-2.0-only
+#
+# Multikernel configuration
+#
+
+config MULTIKERNEL
+	bool "Multikernel support"
+	depends on KEXEC_CORE
+	help
+	  Enable multikernel support, which allows running multiple kernel
+	  instances simultaneously with resource isolation and inter-kernel
+	  communication capabilities.
+
+	  This feature provides:
+	  - Sysfs interface for multikernel instance management
+	  - Device tree based resource specification
+	  - Memory pool management for kernel instances
+	  - Integration with kexec for kernel loading
+
+	  If unsure, say N.
kernel/multikernel/Makefile new file +6 -0
new file mode 100644
index 000000000000..950bace927a0
@@ -0,0 +1,6 @@
+# SPDX-License-Identifier: GPL-2.0-only
+#
+# Makefile for multikernel support
+#
+
+obj-y += core.o
kernel/multikernel/core.c new file +17 -0
new file mode 100644
index 000000000000..218424d59cc3
@@ -0,0 +1,17 @@
+// SPDX-License-Identifier: GPL-2.0-only
+/*
+ * Copyright (C) 2025 Multikernel Technologies, Inc. All rights reserved
+ */
+#include <linux/module.h>
+#include <linux/kernel.h>
+#include <linux/init.h>
+#include <linux/multikernel.h>
+
+static int __init multikernel_init(void)
+{
+	pr_info("Multikernel support initialized\n");
+	return 0;
+}
+
+/* Initialize multikernel after core kernel subsystems are ready */
+subsys_initcall(multikernel_init);
Signature · 1 line
2.34.1
author
#5 lore raw

RFC PATCH v2 4/16 x86: Introduce MULTIKERNEL_VECTOR for inter-kernel communication

In reply to Cong Wang

From: Cong Wang <[email protected]>

This patch adds a dedicated IPI vector (0xea) for multikernel communication, enabling different kernel instances running on separate CPUs to send interrupts to each other.

The implementation includes:

- MULTIKERNEL_VECTOR definition at interrupt vector 0xea
- IDT entry declaration and registration for sysvec_multikernel
- Interrupt handler sysvec_multikernel() with proper APIC EOI and IRQ statistics tracking
- Placeholder generic_multikernel_interrupt() function for extensible multikernel interrupt handling

This vector provides the foundational interrupt mechanism required for implementing inter-kernel communication protocols in multikernel environments, where heterogeneous kernel instances coordinate while maintaining CPU-level isolation.

---
 arch/x86/include/asm/idtentry.h    | 3 +++
 arch/x86/include/asm/irq_vectors.h | 1 +
 arch/x86/kernel/idt.c              | 3 +++
 arch/x86/kernel/smp.c              | 8 ++++++++
 4 files changed, 15 insertions(+)
arch/x86/include/asm/idtentry.h +3 -0
index abd637e54e94..d2c3f1ca481a 100644
@@ -703,6 +703,9 @@ DECLARE_IDTENTRY(RESCHEDULE_VECTOR,			sysvec_reschedule_ipi);
 DECLARE_IDTENTRY_SYSVEC(REBOOT_VECTOR,			sysvec_reboot);
 DECLARE_IDTENTRY_SYSVEC(CALL_FUNCTION_SINGLE_VECTOR,	sysvec_call_function_single);
 DECLARE_IDTENTRY_SYSVEC(CALL_FUNCTION_VECTOR,		sysvec_call_function);
+# ifdef CONFIG_MULTIKERNEL
+DECLARE_IDTENTRY_SYSVEC(MULTIKERNEL_VECTOR,			sysvec_multikernel);
+# endif
 #else
 # define fred_sysvec_reschedule_ipi			NULL
 # define fred_sysvec_reboot				NULL
arch/x86/include/asm/irq_vectors.h +1 -0
index 47051871b436..478e2e2d188a 100644
@@ -102,6 +102,7 @@
  * the host kernel.
  */
 #define POSTED_MSI_NOTIFICATION_VECTOR	0xeb
+#define MULTIKERNEL_VECTOR		0xea
 
 #define NR_VECTORS			 256
 
arch/x86/kernel/idt.c +3 -0
index f445bec516a0..5e6d03bb18b5 100644
@@ -135,6 +135,9 @@ static const __initconst struct idt_data apic_idts[] = {
 	INTG(RESCHEDULE_VECTOR,			asm_sysvec_reschedule_ipi),
 	INTG(CALL_FUNCTION_VECTOR,		asm_sysvec_call_function),
 	INTG(CALL_FUNCTION_SINGLE_VECTOR,	asm_sysvec_call_function_single),
+#ifdef CONFIG_MULTIKERNEL
+	INTG(MULTIKERNEL_VECTOR,		asm_sysvec_multikernel),
+#endif
 	INTG(REBOOT_VECTOR,			asm_sysvec_reboot),
 #endif
 
arch/x86/kernel/smp.c +8 -0
index b014e6d229f9..59658fcd9037 100644
@@ -272,6 +272,14 @@ DEFINE_IDTENTRY_SYSVEC(sysvec_call_function_single)
 	trace_call_function_single_exit(CALL_FUNCTION_SINGLE_VECTOR);
 }
 
+#ifdef CONFIG_MULTIKERNEL
+DEFINE_IDTENTRY_SYSVEC(sysvec_multikernel)
+{
+	apic_eoi();
+	inc_irq_stat(irq_call_count);
+}
+#endif /* CONFIG_MULTIKERNEL */
+
 static int __init nonmi_ipi_setup(char *str)
 {
 	smp_no_nmi_ipi = true;
Signature · 1 line
2.34.1
author
#6 lore raw

RFC PATCH v2 5/16 x86: Introduce arch_cpu_physical_id() to obtain physical CPU ID

In reply to Cong Wang

From: Cong Wang <[email protected]>

The tranditional smp_processor_id() is a software-defined CPU ID which is only unique within the same kernel. With Multikernel architecture, we run multiple Linux kernels on different CPU's, hence the host kernel needs a globally unique CPU ID to manage the CPU's. The physical CPU ID is perfect for this case.

This API will be used to globally distinguish CPU's among different multikernels.

---
 arch/x86/include/asm/smp.h | 6 ++++++
 arch/x86/kernel/smp.c      | 6 ++++++
 2 files changed, 12 insertions(+)
arch/x86/include/asm/smp.h +6 -0
index 1a59fd0de759..378be65ceafa 100644
@@ -40,6 +40,7 @@ struct smp_ops {
 
 	void (*send_call_func_ipi)(const struct cpumask *mask);
 	void (*send_call_func_single_ipi)(int cpu);
+	int (*cpu_physical_id)(int cpu);
 };
 
 /* Globals due to paravirt */
@@ -100,6 +101,11 @@ static inline void arch_send_call_function_ipi_mask(const struct cpumask *mask)
 	smp_ops.send_call_func_ipi(mask);
 }
 
+static inline int arch_cpu_physical_id(int cpu)
+{
+	return smp_ops.cpu_physical_id(cpu);
+}
+
 void cpu_disable_common(void);
 void native_smp_prepare_boot_cpu(void);
 void smp_prepare_cpus_common(void);
arch/x86/kernel/smp.c +6 -0
index 59658fcd9037..e2eba09da7fc 100644
@@ -288,6 +288,11 @@ static int __init nonmi_ipi_setup(char *str)
 
 __setup("nonmi_ipi", nonmi_ipi_setup);
 
+static int native_cpu_physical_id(int cpu)
+{
+	return cpu_physical_id(cpu);
+}
+
 struct smp_ops smp_ops = {
 	.smp_prepare_boot_cpu	= native_smp_prepare_boot_cpu,
 	.smp_prepare_cpus	= native_smp_prepare_cpus,
@@ -305,6 +310,7 @@ struct smp_ops smp_ops = {
 
 	.send_call_func_ipi	= native_send_call_func_ipi,
 	.send_call_func_single_ipi = native_send_call_func_single_ipi,
+	.cpu_physical_id	= native_cpu_physical_id,
 };
 EXPORT_SYMBOL_GPL(smp_ops);
 
Signature · 1 line
2.34.1
author
#7 lore raw

RFC PATCH v2 6/16 multikernel: Introduce physical memory reservation and allocation

In reply to Cong Wang

From: Cong Wang <[email protected]>

This commit introduces:

* Memory pool reservation via mkkernel_pool= kernel parameter that supports both fixed address reservation (mkkernel_pool=size@addr) and dynamic allocation (mkkernel_pool=size). Uses memblock reservation during early boot to ensure memory availability.

* Global memory pool management using gen_pool for runtime allocation and deallocation of physical memory chunks from the reserved pool. Provides thread-safe operations with mutex protection.

* Per-instance memory pool management that allows creating dedicated memory pools for individual kernel instances. Each instance pool is carved out from the main multikernel pool and provides fine-grained allocation capabilities for IPI data, buffers, and other per-instance resources.

* Integration with /proc/iomem resource hierarchy to provide visibility into multikernel memory usage and prevent conflicts with other kernel subsystems.

This memory management system uses a two-tier approach: a main pool reserved during boot handles large allocations and spawning operations, while per-instance pools provide efficient small allocation services for runtime inter-kernel communication and instance-specific data structures such as kernel image and initramfs.

---
 include/linux/multikernel.h |  22 +++
 kernel/multikernel/Makefile |   2 +-
 kernel/multikernel/mem.c    | 376 ++++++++++++++++++++++++++++++++++++
 3 files changed, 399 insertions(+), 1 deletion(-)
 create mode 100644 include/linux/multikernel.h
 create mode 100644 kernel/multikernel/mem.c
include/linux/multikernel.h new file +22 -0
new file mode 100644
index 000000000000..51c989139a75
@@ -0,0 +1,22 @@
+// SPDX-License-Identifier: GPL-2.0-only
+/*
+ * Copyright (C) 2025 Multikernel Technologies, Inc. All rights reserved
+ */
+#ifndef _LINUX_MULTIKERNEL_H
+#define _LINUX_MULTIKERNEL_H
+
+struct resource;
+
+extern phys_addr_t multikernel_alloc(size_t size);
+extern void multikernel_free(phys_addr_t addr, size_t size);
+extern struct resource *multikernel_get_pool_resource(void);
+extern bool multikernel_pool_available(void);
+
+/* Per-instance memory pool management */
+extern void *multikernel_create_instance_pool(int instance_id, size_t pool_size, int min_alloc_order);
+extern void multikernel_destroy_instance_pool(void *pool_handle);
+extern phys_addr_t multikernel_instance_alloc(void *pool_handle, size_t size, size_t align);
+extern void multikernel_instance_free(void *pool_handle, phys_addr_t addr, size_t size);
+extern size_t multikernel_instance_pool_avail(void *pool_handle);
+
+#endif /* _LINUX_MULTIKERNEL_H */
kernel/multikernel/Makefile +1 -1
index 950bace927a0..0dad7f2267f9 100644
@@ -3,4 +3,4 @@
 # Makefile for multikernel support
 #
 
-obj-y += core.o
+obj-y += core.o mem.o
kernel/multikernel/mem.c new file +376 -0

376 lines not on this page · raw message

Signature · 1 line
2.34.1
author
#8 lore raw

RFC PATCH v2 7/16 kexec: Implement dynamic kimage tracking

In reply to Cong Wang

From: Cong Wang <[email protected]>

Replace static kexec_image and kexec_crash_image globals with a dynamic linked list infrastructure to support multiple kernel images. This change enables multikernel functionality while maintaining backward compatibility.

Key changes:
- Add list_head member to kimage structure for chaining
- Implement thread-safe linked list management with global mutex
- Update kexec load/unload logic to use list-based APIs for multikernel
- Add helper functions for finding and managing multiple kimages
- Preserve existing kexec_image/kexec_crash_image pointers for compatibility
- Update architecture-specific crash handling to use new APIs

The multikernel case now properly uses list-based management instead of overwriting compatibility pointers, allowing multiple multikernel images to coexist in the system.

---
 arch/powerpc/kexec/crash.c |  8 ++--
 arch/x86/kernel/crash.c    |  4 +-
 include/linux/kexec.h      | 13 ++++++
 kernel/kexec.c             | 60 ++++++++++++++++++++++++++--
 kernel/kexec_core.c        | 81 ++++++++++++++++++++++++++++++++++++++
 kernel/kexec_file.c        | 33 ++++++++++++++--
 6 files changed, 187 insertions(+), 12 deletions(-)
arch/powerpc/kexec/crash.c +4 -4
index a325c1c02f96..af190fad4f22 100644
@@ -477,13 +477,13 @@ static void update_crash_elfcorehdr(struct kimage *image, struct memory_notify *
 	ptr = __va(mem);
 	if (ptr) {
 		/* Temporarily invalidate the crash image while it is replaced */
-		xchg(&kexec_crash_image, NULL);
+		kimage_update_compat_pointers(NULL, KEXEC_TYPE_CRASH);
 
 		/* Replace the old elfcorehdr with newly prepared elfcorehdr */
 		memcpy((void *)ptr, elfbuf, elfsz);
 
 		/* The crash image is now valid once again */
-		xchg(&kexec_crash_image, image);
+		kimage_update_compat_pointers(image, KEXEC_TYPE_CRASH);
 	}
 out:
 	kvfree(cmem);
@@ -537,14 +537,14 @@ static void update_crash_fdt(struct kimage *image)
 	fdt = __va((void *)image->segment[fdt_index].mem);
 
 	/* Temporarily invalidate the crash image while it is replaced */
-	xchg(&kexec_crash_image, NULL);
+	kimage_update_compat_pointers(NULL, KEXEC_TYPE_CRASH);
 
 	/* update FDT to reflect changes in CPU resources */
 	if (update_cpus_node(fdt))
 		pr_err("Failed to update crash FDT");
 
 	/* The crash image is now valid once again */
-	xchg(&kexec_crash_image, image);
+	kimage_update_compat_pointers(image, KEXEC_TYPE_CRASH);
 }
 
 int arch_crash_hotplug_support(struct kimage *image, unsigned long kexec_flags)
arch/x86/kernel/crash.c +2 -2
index 335fd2ee9766..a81f91d4352d 100644
@@ -559,9 +559,9 @@ void arch_crash_handle_hotplug_event(struct kimage *image, void *arg)
 	 * Temporarily invalidate the crash image while the
 	 * elfcorehdr is updated.
 	 */
-	xchg(&kexec_crash_image, NULL);
+	kimage_update_compat_pointers(NULL, KEXEC_TYPE_CRASH);
 	memcpy_flushcache(old_elfcorehdr, elfbuf, elfsz);
-	xchg(&kexec_crash_image, image);
+	kimage_update_compat_pointers(image, KEXEC_TYPE_CRASH);
 	kunmap_local(old_elfcorehdr);
 	pr_debug("updated elfcorehdr\n");
 
include/linux/kexec.h +13 -0
index edf64bc98ed5..69877db5360b 100644
@@ -431,6 +431,9 @@ struct kimage {
 	/* dm crypt keys buffer */
 	unsigned long dm_crypt_keys_addr;
 	unsigned long dm_crypt_keys_sz;
+
+	/* For multikernel support: linked list node */
+	struct list_head list;
 };
 
 /* kexec interface functions */
@@ -534,6 +537,16 @@ extern bool kexec_file_dbg_print;
 
 extern void *kimage_map_segment(struct kimage *image, unsigned long addr, unsigned long size);
 extern void kimage_unmap_segment(void *buffer);
+
+/* Multikernel support functions */
+extern struct kimage *kimage_find_by_type(int type);
+extern void kimage_add_to_list(struct kimage *image);
+extern void kimage_remove_from_list(struct kimage *image);
+extern void kimage_update_compat_pointers(struct kimage *new_image, int type);
+extern int kimage_get_all_by_type(int type, struct kimage **images, int max_count);
+extern void kimage_list_lock(void);
+extern void kimage_list_unlock(void);
+extern void kimage_list_multikernel_images(void);
 #else /* !CONFIG_KEXEC_CORE */
 struct pt_regs;
 struct task_struct;
kernel/kexec.c +57 -3

78 lines not on this page · raw message

kernel/kexec_core.c +81 -0

111 lines not on this page · raw message

kernel/kexec_file.c +30 -3
index eb62a9794242..2d9d5626c8da 100644
@@ -400,8 +400,13 @@ SYSCALL_DEFINE5(kexec_file_load, int, kernel_fd, int, initrd_fd,
 	 * same memory where old crash kernel might be loaded. Free any
 	 * current crash dump kernel before we corrupt it.
 	 */
-	if (flags & KEXEC_FILE_ON_CRASH)
-		kimage_free(xchg(&kexec_crash_image, NULL));
+	if (flags & KEXEC_FILE_ON_CRASH) {
+		struct kimage *old_crash_image = xchg(&kexec_crash_image, NULL);
+		if (old_crash_image) {
+			kimage_remove_from_list(old_crash_image);
+			kimage_free(old_crash_image);
+		}
+	}
 
 	ret = kimage_file_alloc_init(&image, kernel_fd, initrd_fd, cmdline_ptr,
 				     cmdline_len, flags);
@@ -457,7 +462,29 @@ SYSCALL_DEFINE5(kexec_file_load, int, kernel_fd, int, initrd_fd,
 	 */
 	kimage_file_post_load_cleanup(image);
 exchange:
-	image = xchg(dest_image, image);
+	if (image_type == KEXEC_TYPE_CRASH) {
+		struct kimage *old_image = xchg(&kexec_crash_image, image);
+		if (old_image) {
+			kimage_remove_from_list(old_image);
+			kimage_free(old_image);
+		}
+		if (image) {
+			kimage_add_to_list(image);
+			kimage_update_compat_pointers(image, KEXEC_TYPE_CRASH);
+		}
+		image = NULL; /* Don't free the new image */
+	} else {
+		struct kimage *old_image = xchg(&kexec_image, image);
+		if (old_image) {
+			kimage_remove_from_list(old_image);
+			kimage_free(old_image);
+		}
+		if (image) {
+			kimage_add_to_list(image);
+			kimage_update_compat_pointers(image, KEXEC_TYPE_DEFAULT);
+		}
+		image = NULL; /* Don't free the new image */
+	}
 out:
 #ifdef CONFIG_CRASH_DUMP
 	if ((flags & KEXEC_FILE_ON_CRASH) && kexec_crash_image)
Signature · 1 line
2.34.1
author
#9 lore raw

RFC PATCH v2 8/16 multikernel: Introduce device-tree based kernfs interface

In reply to Cong Wang

From: Cong Wang <[email protected]>

This commit introduces:

* Device tree parsing infrastructure (kernel/multikernel/dts.c) that supports parsing multikernel-specific device tree blobs with resource specifications including memory size requirements and CPU assignments. Provides validation, configuration management, and resource availability checking with extensible property parsing for future enhancements.

* Kernfs-based filesystem interface (kernel/multikernel/kernfs.c) that creates a dedicated multikernel filesystem mountable at /sys/fs/multikernel/ with full instance lifecycle management through device tree uploads rather than manual directory creation.

* Instance management infrastructure with comprehensive state tracking (empty, ready, loading, active, failed), reference counting, resource reservation, and integration with the kexec multikernel system for seamless kernel loading operations.

Key features of the interface:

- Device tree upload via /device_tree file that automatically parses multikernel DTBs and creates instance directories with proper resource allocation and CPU assignment including CPU hotplug operations.

- Per-instance attribute files (id, status, device_tree_source) that provide read-only access to instance information and configuration with human-readable device tree source reconstruction.

- Resource management integration that reserves memory from multikernel pools, creates proper resource hierarchy entries, and handles CPU offline operations for dedicated CPU assignment.

- State synchronization with kexec operations through mk_instance_find(), mk_instance_set_kexec_active(), and mk_instance_set_kexec_loading() functions for proper lifecycle tracking.

The device tree format supports /instances/<name> nodes with resources subnodes containing memory-bytes and cpus properties, providing a structured approach to multikernel configuration with proper validation and resource conflict detection.

See also the user-space kerf project for more details about device-tree
specification: https://github.com/multikernel/kerf

---
 include/linux/multikernel.h   | 328 +++++++++++++++
 kernel/multikernel/Kconfig    |   1 +
 kernel/multikernel/Makefile   |   5 +-
 kernel/multikernel/core.c     | 404 ++++++++++++++++++
 kernel/multikernel/dts.c      | 466 ++++++++++++++++++++
 kernel/multikernel/internal.h |   4 +
 kernel/multikernel/kernfs.c   | 772 ++++++++++++++++++++++++++++++++++
 7 files changed, 1979 insertions(+), 1 deletion(-)
 create mode 100644 kernel/multikernel/dts.c
 create mode 100644 kernel/multikernel/internal.h
 create mode 100644 kernel/multikernel/kernfs.c
include/linux/multikernel.h +328 -0
index 51c989139a75..75cbb316d565 100644
@@ -5,6 +5,15 @@
 #ifndef _LINUX_MULTIKERNEL_H
 #define _LINUX_MULTIKERNEL_H
 
+#include <linux/types.h>
+#include <linux/irq_work.h>
+#include <linux/kobject.h>
+#include <linux/kernfs.h>
+#include <linux/ioport.h>
+#include <linux/of.h>
+#include <linux/cpumask.h>
+#include <linux/genalloc.h>
+
 struct resource;
 
 extern phys_addr_t multikernel_alloc(size_t size);
@@ -19,4 +28,323 @@ extern phys_addr_t multikernel_instance_alloc(void *pool_handle, size_t size, si
 extern void multikernel_instance_free(void *pool_handle, phys_addr_t addr, size_t size);
 extern size_t multikernel_instance_pool_avail(void *pool_handle);
 
+/**
+ * Multikernel Instance States
+ */
+enum mk_instance_state {
+	MK_STATE_EMPTY = 0,     /* Instance directory exists but no DTB */
+	MK_STATE_READY,         /* DTB loaded, resources reserved */
+	MK_STATE_LOADING,       /* Kernel being loaded */
+	MK_STATE_ACTIVE,        /* Kernel running */
+	MK_STATE_FAILED,        /* Error occurred */
+};
+
+/**
+ * Memory region wrapper
+ *
+ * This wraps a struct resource with gen_pool_chunk for memory pool management.
+ * Used by instance management to track both resource hierarchy and pool chunks.
+ */
+struct mk_memory_region {
+	struct resource res;            /* The actual resource */
+	struct gen_pool_chunk *chunk;   /* Associated gen_pool chunk */
+	struct list_head list;          /* List entry for management */
+};
+
+/**
+ * Complete multikernel device tree configuration
+ *
+ * This structure handles memory size requirements and CPU assignment
+ * parsed from device tree blobs.
+ */
+struct mk_dt_config {
+	/* Version for compatibility checking */
+	u32 version;
+
+	/* Memory requirements */
+	size_t memory_size;              /* Total memory size required */
+
+	/* CPU resources */
+	cpumask_var_t cpus;              /* Assigned CPU mask */
+	bool cpus_valid;                 /* Whether CPU assignment is valid */
+
+	/* Extensibility: Reserved fields for future use */
+	u32 reserved[12];                /* Increased due to removed fields */
+
+	/* Raw device tree data */
+	void *dtb_data;
+	size_t dtb_size;
+};
+
+/**
+ * Multikernel Instance Structure
+ *
+ * Each instance represents a potential or active multikernel with
+ * its own resource allocation and state management.
+ */
+struct mk_instance {
+	int id;                         /* Kernel-assigned instance ID */
+	char *name;                     /* User-provided instance name */
+	enum mk_instance_state state;   /* Current state */
+
+	/* Resource management - list of reserved memory regions */
+	struct list_head memory_regions;  /* List of struct mk_memory_region */
+	int region_count;                  /* Number of memory regions */
+	/* Memory pool for this instance */
+	void *instance_pool;            /* Handle for instance-specific memory pool */
+	size_t pool_size;               /* Size of the instance pool */
+
+	/* CPU resources */
+	cpumask_var_t cpus;              /* Assigned CPU mask */
+	bool cpus_valid;                 /* Whether CPU assignment is valid */
+
+	/* Device tree information */
+	void *dtb_data;                 /* Device tree blob data */
+	size_t dtb_size;                /* Size of DTB */
+
+	/* Sysfs representation */
+	struct kernfs_node *kn;            /* Kernfs node for this instance */
+
+	/* List management */
+	struct list_head list;          /* Link to global instance list */
+
+	/* Reference counting */
+	struct kref refcount;           /* Reference count for cleanup */
+};
+
+/**
+ * Device Tree Parsing Functions
+ */
+
+/**
+ * mk_dt_parse() - Parse multikernel device tree blob
+ * @dtb_data: Device tree blob data
+ * @dtb_size: Size of DTB data
+ * @config: Output configuration structure
+ *
+ * Parses a device tree blob and extracts multikernel-specific
+ * memory region properties. Supports multiple memory regions
+ * specified as:
+ * - linux,multikernel-memory = <start1 size1 start2 size2 ...>;
+ *
+ * Each memory region becomes a struct resource that will be
+ * inserted as a child of the main multikernel_res.
+ *
+ * Returns 0 on success, negative error code on failure.
+ */
+int mk_dt_parse(const void *dtb_data, size_t dtb_size,
+		struct mk_dt_config *config);
+
+/**
+ * mk_dt_validate() - Validate parsed device tree configuration
+ * @config: Configuration to validate
+ *
+ * Validates that the parsed memory regions are reasonable and
+ * compatible with the current system state. Checks for:
+ * - Region alignment
+ * - Availability in multikernel pool
+ * - No overlaps between regions
+ *
+ * Returns 0 if valid, negative error code on validation failure.
+ */
+int mk_dt_validate(const struct mk_dt_config *config);
+
+/**
+ * mk_dt_config_init() - Initialize a device tree configuration
+ * @config: Configuration structure to initialize
+ *
+ * Initializes all fields to safe defaults.
+ */
+void mk_dt_config_init(struct mk_dt_config *config);
+
+/**
+ * mk_dt_config_free() - Free device tree configuration resources
+ * @config: Configuration to free
+ *
+ * Frees any dynamically allocated resources in the configuration.
+ */
+void mk_dt_config_free(struct mk_dt_config *config);
+
+/**
+ * mk_dt_resources_available() - Check if memory and CPU resources are available
+ * @config: Configuration with resources to check
+ *
+ * Checks if the specified memory size is available in the
+ * multikernel memory pool and all CPUs are possible on the system.
+ *
+ * Returns true if all resources are available, false otherwise.
+ */
+bool mk_dt_resources_available(const struct mk_dt_config *config);
+
+/**
+ * mk_dt_get_property_size() - Get size of a specific property
+ * @dtb_data: Device tree blob
+ * @dtb_size: Size of DTB
+ * @property: Property name (e.g., "linux,multikernel-memory")
+ *
+ * Helper function to determine the size of a property before parsing.
+ * Useful for validation and memory allocation.
+ *
+ * Returns property size in bytes, or -ENOENT if not found.
+ */
+int mk_dt_get_property_size(const void *dtb_data, size_t dtb_size,
+			    const char *property);
+
+/**
+ * mk_dt_print_config() - Print configuration for debugging
+ * @config: Configuration to print
+ *
+ * Prints the parsed configuration in a human-readable format
+ * for debugging purposes.
+ */
+void mk_dt_print_config(const struct mk_dt_config *config);
+
+/**
+ * Sysfs Interface Functions
+ */
+
+/**
+ * mk_kernfs_init() - Initialize multikernel kernfs interface
+ *
+ * Creates /sys/kernel/multikernel/ directory and sets up
+ * the kernfs infrastructure for multikernel instances.
+ *
+ * Returns 0 on success, negative error code on failure.
+ */
+int mk_kernfs_init(void);
+
+/**
+ * mk_kernfs_cleanup() - Cleanup multikernel kernfs interface
+ *
+ * Removes all kernfs entries and cleans up resources.
+ */
+void mk_kernfs_cleanup(void);
+
+/**
+ * mk_instance_find_by_name() - Find an existing instance by name
+ * @name: Instance name to find
+ *
+ * Returns pointer to mk_instance if found, NULL otherwise.
+ * Caller must hold appropriate locks.
+ */
+struct mk_instance *mk_instance_find_by_name(const char *name);
+
+/**
+ * mk_instance_get() - Increment reference count
+ * @instance: Instance to reference
+ *
+ * Returns the instance pointer for convenience.
+ */
+struct mk_instance *mk_instance_get(struct mk_instance *instance);
+
+/**
+ * mk_instance_put() - Decrement reference count
+ * @instance: Instance to dereference
+ *
+ * May free the instance if reference count reaches zero.
+ */
+void mk_instance_put(struct mk_instance *instance);
+
+/**
+ * mk_instance_set_state() - Update instance state
+ * @instance: Instance to update
+ * @state: New state
+ *
+ * Updates the instance state and notifies sysfs.
+ */
+void mk_instance_set_state(struct mk_instance *instance,
+			   enum mk_instance_state state);
+
+/**
+ * mk_instance_reserve_resources() - Reserve CPU and memory resources for instance
+ * @instance: Instance to reserve resources for
+ * @config: Device tree configuration with memory size and CPU assignment
+ *
+ * Allocates the specified memory size from the multikernel pool, creates
+ * memory regions, and copies CPU assignment.
+ *
+ * Returns 0 on success, negative error code on failure.
+ */
+int mk_instance_reserve_resources(struct mk_instance *instance,
+				  const struct mk_dt_config *config);
+
+/**
+ * mk_instance_free_memory() - Free all reserved memory regions
+ * @instance: Instance to free memory for
+ *
+ * Returns all reserved memory regions back to the multikernel pool
+ * and removes them from the resource hierarchy.
+ */
+void mk_instance_free_memory(struct mk_instance *instance);
+
+/**
+ * String conversion helpers
+ */
+const char *mk_state_to_string(enum mk_instance_state state);
+enum mk_instance_state mk_string_to_state(const char *str);
+
+/**
+ * Kexec Integration Functions
+ *
+ * These functions bridge the gap between the sysfs instance management
+ * and the kexec multikernel system.
+ */
+
+/**
+ * mk_instance_find() - Find instance by a multikernel instance ID
+ * @mk_id: Multikernel instance ID
+ *
+ * Returns pointer to mk_instance if found, NULL otherwise.
+ */
+struct mk_instance *mk_instance_find(int mk_id);
+
+/**
+ * mk_instance_set_kexec_active() - Mark instance as active for kexec
+ * @mk_id: Multikernel ID from kexec system
+ *
+ * Returns 0 on success, negative error code on failure.
+ */
+int mk_instance_set_kexec_active(int mk_id);
+
+/**
+ * mk_instance_set_kexec_loading() - Mark instance as loading for kexec
+ * @mk_id: Multikernel ID from kexec system
+ *
+ * Returns 0 on success, negative error code on failure.
+ */
+int mk_instance_set_kexec_loading(int mk_id);
+
+/**
+ * Version and Compatibility
+ */
+#define MK_DT_CONFIG_VERSION_1  1
+#define MK_DT_CONFIG_CURRENT    MK_DT_CONFIG_VERSION_1
+#define MK_FDT_COMPATIBLE "multikernel-v1"
+
+/**
+ * Property Names
+ */
+#define MK_DT_RESOURCE_MEMORY   "memory-bytes"
+#define MK_DT_RESOURCE_CPUS     "cpus"
+
+static const char * const mk_resource_properties[] = {
+	MK_DT_RESOURCE_MEMORY,
+	MK_DT_RESOURCE_CPUS,
+	NULL  /* Sentinel */
+};
+
+static inline bool mk_is_resource_property(const char *prop_name)
+{
+	const char * const *prop;
+
+	if (!prop_name)
+		return false;
+
+	for (prop = mk_resource_properties; *prop; prop++) {
+		if (strcmp(prop_name, *prop) == 0)
+			return true;
+	}
+	return false;
+}
+
 #endif /* _LINUX_MULTIKERNEL_H */
kernel/multikernel/Kconfig +1 -0
index 0e61fd2e505a..a9582a4d0c54 100644
@@ -6,6 +6,7 @@
 config MULTIKERNEL
 	bool "Multikernel support"
 	depends on KEXEC_CORE
+	select LIBFDT
 	help
 	  Enable multikernel support, which allows running multiple kernel
 	  instances simultaneously with resource isolation and inter-kernel
kernel/multikernel/Makefile +4 -1
index 0dad7f2267f9..d004c577f13d 100644
@@ -3,4 +3,7 @@
 # Makefile for multikernel support
 #
 
-obj-y += core.o mem.o
+obj-y += core.o mem.o kernfs.o dts.o
+
+# Add libfdt include path for device tree parsing
+CFLAGS_dts.o = -I $(srctree)/scripts/dtc/libfdt
kernel/multikernel/core.c +404 -0

414 lines not on this page · raw message

kernel/multikernel/dts.c new file +466 -0

466 lines not on this page · raw message

kernel/multikernel/internal.h new file +4 -0
new file mode 100644
index 000000000000..54253d29b2ce
@@ -0,0 +1,4 @@
+extern struct resource multikernel_res;
+extern struct mutex mk_instance_mutex;
+extern struct idr mk_instance_idr;
+extern struct list_head mk_instance_list;
kernel/multikernel/kernfs.c new file +772 -0

772 lines not on this page · raw message

Signature · 1 line
2.34.1
author
#10 lore raw

RFC PATCH v2 9/16 kexec: Integrate multikernel instance management with kexec subsystem

In reply to Cong Wang

From: Cong Wang <[email protected]>

Establish bidirectional integration between the kexec subsystem and multikernel instance management, enabling proper lifecycle tracking and resource coordination for multikernel operations.

This commit introduces:

* Enhanced kimage structure with multikernel-specific fields including mk_id for unique multikernel identification and mk_instance pointer for cross-referencing with the multikernel instance management system, enabling proper state synchronization.

* UAPI extensions in include/uapi/linux/kexec.h that define multikernel ID encoding within kexec flags using KEXEC_MK_ID_MASK and KEXEC_MK_ID_SHIFT, providing up to 2047 unique multikernel instances with proper bit field management macros.

* Multikernel image lookup infrastructure through kimage_find_by_id() that leverages the mk_instance system for efficient image retrieval by multikernel ID, replacing CPU-based lookup with proper instance management.

* Refactored multikernel_kexec() interface from CPU-based to ID-based operation (multikernel_kexec_by_id()) that uses instance CPU assignments from device tree configuration rather than manual CPU specification, improving safety and consistency.

* Proper resource lifecycle management in kimage_free() that clears cross-references, updates instance states, and handles reference counting when multikernel images are freed, preventing resource leaks and dangling pointers.

* Updated reboot syscall interface that accepts multikernel instance IDs instead of CPU numbers in LINUX_REBOOT_CMD_MULTIKERNEL, providing a more intuitive and safer user interface.

---
 include/linux/kexec.h       | 10 ++++-
 include/linux/multikernel.h |  3 ++
 include/uapi/linux/kexec.h  |  4 ++
 kernel/kexec_core.c         | 75 +++++++++++++++++++++++++++++++++----
 kernel/reboot.c             |  4 +-
 5 files changed, 85 insertions(+), 11 deletions(-)
include/linux/kexec.h +9 -1
index 69877db5360b..5e9e9ad1dfeb 100644
@@ -132,6 +132,7 @@ struct purgatory_info {
 };
 
 struct kimage;
+struct mk_instance;
 
 typedef int (kexec_probe_t)(const char *kernel_buf, unsigned long kernel_size);
 typedef void *(kexec_load_t)(struct kimage *image, char *kernel_buf,
@@ -434,6 +435,12 @@ struct kimage {
 
 	/* For multikernel support: linked list node */
 	struct list_head list;
+
+	/* Multikernel unique ID (0 = current kernel, >0 = multikernel images) */
+	int mk_id;
+
+	/* Multikernel instance cross-reference */
+	struct mk_instance *mk_instance;
 };
 
 /* kexec interface functions */
@@ -441,7 +448,8 @@ extern void machine_kexec(struct kimage *image);
 extern int machine_kexec_prepare(struct kimage *image);
 extern void machine_kexec_cleanup(struct kimage *image);
 extern int kernel_kexec(void);
-extern int multikernel_kexec(int cpu);
+extern int multikernel_kexec_by_id(int mk_id);
+extern struct kimage *kimage_find_by_id(int mk_id);
 extern struct page *kimage_alloc_control_pages(struct kimage *image,
 						unsigned int order);
 
include/linux/multikernel.h +3 -0
index 75cbb316d565..c65d39a66b84 100644
@@ -102,6 +102,9 @@ struct mk_instance {
 	void *dtb_data;                 /* Device tree blob data */
 	size_t dtb_size;                /* Size of DTB */
 
+	/* Kexec integration */
+	struct kimage *kimage;          /* Associated kimage object */
+
 	/* Sysfs representation */
 	struct kernfs_node *kn;            /* Kernfs node for this instance */
 
include/uapi/linux/kexec.h +4 -0
index 346e0ff4e663..82a562ae6ac1 100644
@@ -15,6 +15,10 @@
 #define KEXEC_UPDATE_ELFCOREHDR	0x00000004
 #define KEXEC_CRASH_HOTPLUG_SUPPORT 0x00000008
 #define KEXEC_MULTIKERNEL	0x00000010
+#define KEXEC_MK_ID_MASK	0x0000ffe0
+#define KEXEC_MK_ID_SHIFT	5
+#define KEXEC_MK_ID(id)		(((id) << KEXEC_MK_ID_SHIFT) & KEXEC_MK_ID_MASK)
+#define KEXEC_GET_MK_ID(flags)	(((flags) & KEXEC_MK_ID_MASK) >> KEXEC_MK_ID_SHIFT)
 #define KEXEC_ARCH_MASK		0xffff0000
 
 /*
kernel/kexec_core.c +67 -8

111 lines not on this page · raw message

kernel/reboot.c +2 -2
index f3ac703c4695..bff6d3603a17 100644
@@ -718,7 +718,7 @@ EXPORT_SYMBOL_GPL(kernel_power_off);
 DEFINE_MUTEX(system_transition_mutex);
 
 struct multikernel_boot_args {
-	int cpu;
+	int mk_id;
 };
 
 /*
@@ -807,7 +807,7 @@ SYSCALL_DEFINE4(reboot, int, magic1, int, magic2, unsigned int, cmd,
 	case LINUX_REBOOT_CMD_MULTIKERNEL:
 		if (copy_from_user(&boot_args, arg, sizeof(boot_args)))
 			return -EFAULT;
-		ret = multikernel_kexec(boot_args.cpu);
+		ret = multikernel_kexec_by_id(boot_args.mk_id);
 		break;
 #endif
 
Signature · 1 line
2.34.1
author
#11 lore raw

RFC PATCH v2 10/16 Documentation: Add multikernel usage

In reply to Cong Wang

From: Cong Wang <[email protected]>

---
 Documentation/multikernel/usage.rst | 215 ++++++++++++++++++++++++++++
 1 file changed, 215 insertions(+)
 create mode 100644 Documentation/multikernel/usage.rst
Documentation/multikernel/usage.rst new file +215 -0
new file mode 100644
index 000000000000..a2ec8d56ca1d
@@ -0,0 +1,215 @@
+===================================
+Multikernel Kernfs Interface Usage
+===================================
+
+Overview
+========
+
+The multikernel kernfs interface provides a clean, user-friendly way to manage multikernel instances through the filesystem. The interface is located at ``/sys/fs/multikernel/`` and supports automatic instance creation from multikernel device trees.
+
+Architecture
+============
+
+::
+
+    /sys/fs/multikernel/
+    ├── device_tree         # Root-level DTB upload (write-only)
+    └── instances/          # Instance directory
+        ├── web-server/     # Instance created from DTB
+        │   ├── id          # Instance ID (read-only)
+        │   ├── status      # Instance status (read-only)
+        │   └── device_tree_source  # Instance DTB in DTS format (read-only)
+        ├── database/       # Another instance
+        │   ├── id
+        │   ├── status
+        │   └── device_tree_source
+        └── ...
+
+Workflow
+========
+
+Phase 1: Instance Creation (Automatic from DTB)
+------------------------------------------------
+
+1. **Create Multikernel Device Tree**
+
+   Create a device tree with multiple instances:
+
+   .. code-block:: dts
+
+      /dts-v1/;
+      / {
+          compatible = "multikernel-v1";
+
+          instances {
+              web-server {
+                  id = <1>;
+                  resources {
+                      cpus = <1>;
+                      memory-bytes = <0x20000000>;   // 512MB
+                  };
+              };
+
+              database {
+                  id = <2>;
+                  resources {
+                      cpus = <2 3>;
+                      memory-bytes = <0x40000000>;   // 1GB
+                  };
+              };
+          };
+      };
+
+2. **Upload Multikernel DTB**
+
+   .. code-block:: bash
+
+      # Compile device tree to binary format
+      dtc -O dtb -o multikernel.dtb multikernel.dts
+
+      # Upload DTB to create instances automatically
+      cat multikernel.dtb > /sys/fs/multikernel/device_tree
+
+   This automatically:
+
+   - Validates DTB format and multikernel-v1 compatibility
+   - Parses each instance in the ``/instances`` node
+   - Creates instance directories under ``instances/``
+   - Reserves memory and CPU resources for each instance
+   - Updates each instance status to "ready"
+
+3. **Check Created Instances**
+
+   .. code-block:: bash
+
+      # List created instances
+      ls /sys/fs/multikernel/instances/
+      # Output: database  web-server
+
+      # Check instance details
+      cat /sys/fs/multikernel/instances/web-server/id
+      # Output: 1
+
+      cat /sys/fs/multikernel/instances/web-server/status
+      # Output: ready
+
+      # View instance device tree
+      cat /sys/fs/multikernel/instances/web-server/device_tree_source
+      # Output: DTS format showing the instance configuration
+
+Phase 2: Kernel Loading (Kexec Integration)
+--------------------------------------------
+
+1. **Load Kernel Image**
+
+   .. code-block:: bash
+
+      # Load kernel for instance ID 1 (web-server)
+      kexec_file_load(..., KEXEC_MULTIKERNEL | KEXEC_MK_ID(1))
+
+   This:
+
+   - Finds pre-reserved resources for instance ID 1
+   - Creates kimage using pre-allocated memory and CPU resources
+   - Updates status to "loading" → "active"
+   - Preserves instance DTB for KHO (Kexec HandOver) restoration
+
+2. **Instance DTB Preservation**
+
+   The multikernel system automatically preserves each instance's device tree during kexec for restoration in the spawn kernel. The spawn kernel will:
+
+   - Detect multikernel KHO data during early boot
+   - Restore the instance's DTB and recreate the instance structure
+   - Re-reserve the same memory and CPU resources
+
+Device Tree Format
+==================
+
+Multikernel DTB Structure
+--------------------------
+
+The multikernel device tree uses the ``/instances`` structure with ``multikernel-v1`` compatibility:
+
+.. code-block:: dts
+
+    /dts-v1/;
+    / {
+        compatible = "multikernel-v1";
+
+        instances {
+            web-server {
+                id = <1>;
+                resources {
+                    cpus = <1>;                      // CPU ID 1
+                    memory-bytes = <0x20000000>;     // 512MB
+                };
+            };
+
+            database {
+                id = <2>;
+                resources {
+                    cpus = <2 3>;                    // CPU IDs 2 and 3
+                    memory-bytes = <0x40000000>;     // 1GB
+                };
+            };
+
+            load-balancer {
+                id = <3>;
+                resources {
+                    cpus = <0>;                      // CPU ID 0
+                    memory-bytes = <0x10000000>;     // 256MB
+                };
+            };
+        };
+    };
+
+Per-Instance DTB Format
+-----------------------
+
+When viewing an instance's ``device_tree_source``, it appears in per-instance format:
+
+.. code-block:: dts
+
+    /dts-v1/;
+
+    /web-server {
+        compatible = "multikernel-v1";
+        id = <1>;
+        resources {
+            cpus = <1>;
+            memory-bytes = <0x20000000>; // 512 MB
+        };
+    };
+
+Resource Properties
+-------------------
+
+- **cpus**: Array of CPU IDs to assign to this instance
+- **memory-bytes**: Memory size in bytes (must be page-aligned)
+- **id**: Unique instance identifier used for kexec operations
+
+The system validates that:
+
+- CPU IDs are valid and available
+- Memory requests don't exceed available multikernel pool
+- Instance IDs are unique
+- All values are properly aligned
+
+Instance States
+===============
+
+- **empty**: Instance created but no resources allocated yet
+- **ready**: DTB processed, resources reserved, ready for kexec
+- **loading**: Kernel being loaded via kexec
+- **active**: Kernel running in this instance
+- **failed**: Error occurred during any phase
+
+Interface Restrictions
+======================
+
+The new kernfs interface has the following restrictions:
+
+- **No manual instance creation**: Use ``mkdir`` under ``instances/`` is disabled
+- **No direct DTB upload to instances**: Instances don't have writable ``device_tree`` files
+- **Centralized DTB management**: All instances must be created via the root ``device_tree`` file
+- **Read-only instance files**: All instance attributes are read-only for consistency
Signature · 1 line
2.34.1
author
#12 lore raw

RFC PATCH v2 11/16 kexec: Add /proc/kimage interface for kimage tracking

In reply to Cong Wang

From: Cong Wang <[email protected]>

Add a dedicated /proc/kimage file to provide read-only access to all loaded kernel images in the system, for both regular kexec kernel images and multikernel images.

The interface displays kernel images in a tabular format showing:
- Type: kexec type (default, crash, multikernel)
- Start Address: entry point in hexadecimal format
- Segments: number of memory segments

This interface is particularly useful for inspecting kimages, debugging kexec, and verifying that kernel images are loaded correctly.

---
 kernel/kexec_core.c | 63 +++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 63 insertions(+)
kernel/kexec_core.c +63 -0
index ed5c97b4531e..7db755e64dd6 100644
@@ -13,6 +13,8 @@
 #include <linux/slab.h>
 #include <linux/fs.h>
 #include <linux/kexec.h>
+#include <linux/proc_fs.h>
+#include <linux/seq_file.h>
 #include <linux/mutex.h>
 #include <linux/list.h>
 #include <linux/highmem.h>
@@ -1214,6 +1216,52 @@ struct kimage *kexec_image;
 struct kimage *kexec_crash_image;
 static int kexec_load_disabled;
 
+/*
+ * Proc interface for /proc/kimage
+ */
+static int kimage_proc_show(struct seq_file *m, void *v)
+{
+	struct kimage *image;
+	const char *type_names[] = {
+		[KEXEC_TYPE_DEFAULT] = "default",
+		[KEXEC_TYPE_CRASH] = "crash",
+		[KEXEC_TYPE_MULTIKERNEL] = "multikernel"
+	};
+
+	seq_printf(m, "MK_ID  Type        Start Address   Segments\n");
+	seq_printf(m, "-----  ----------  --------------  --------\n");
+
+	kimage_list_lock();
+	if (list_empty(&kexec_image_list)) {
+		seq_printf(m, "No kimages loaded\n");
+	} else {
+		list_for_each_entry(image, &kexec_image_list, list) {
+			const char *type_name = "unknown";
+
+			if (image->type < ARRAY_SIZE(type_names) && type_names[image->type])
+				type_name = type_names[image->type];
+
+			seq_printf(m, "%5d  %-10s  0x%012lx  %8lu\n",
+				   image->mk_id, type_name, image->start, image->nr_segments);
+		}
+	}
+	kimage_list_unlock();
+
+	return 0;
+}
+
+static int kimage_proc_open(struct inode *inode, struct file *file)
+{
+	return single_open(file, kimage_proc_show, NULL);
+}
+
+static const struct proc_ops kimage_proc_ops = {
+	.proc_open	= kimage_proc_open,
+	.proc_read	= seq_read,
+	.proc_lseek	= seq_lseek,
+	.proc_release	= single_release,
+};
+
 #ifdef CONFIG_SYSCTL
 static int kexec_limit_handler(const struct ctl_table *table, int write,
 			       void *buffer, size_t *lenp, loff_t *ppos)
@@ -1285,6 +1333,21 @@ static int __init kexec_core_sysctl_init(void)
 late_initcall(kexec_core_sysctl_init);
 #endif
 
+static int __init kimage_proc_init(void)
+{
+	struct proc_dir_entry *entry;
+
+	entry = proc_create("kimage", 0444, NULL, &kimage_proc_ops);
+	if (!entry) {
+		pr_err("Failed to create /proc/kimage\n");
+		return -ENOMEM;
+	}
+
+	pr_debug("Created /proc/kimage interface\n");
+	return 0;
+}
+late_initcall(kimage_proc_init);
+
 bool kexec_load_permitted(int kexec_image_type)
 {
 	struct kexec_load_limit *limit;
Signature · 1 line
2.34.1
author
#13 lore raw

RFC PATCH v2 12/16 multikernel: Introduce per-instance memory allocation interface

In reply to Cong Wang

From: Cong Wang <[email protected]>

Add virtual memory allocation interface for multikernel instances, providing convenient high-level functions for memory management within instance-specific memory pools with automatic virtual address mapping.

This commit introduces:

* Instance-based memory allocation functions (mk_instance_alloc() and mk_instance_free()) that provide virtual memory allocation from instance-specific memory pools with configurable alignment support and automatic physical-to-virtual address mapping using memremap().

* Kimage-based memory allocation wrappers (mk_kimage_alloc() and mk_kimage_free()) that provide convenient access to instance memory pools through kimage structures, commonly used in kexec code paths for multikernel operations.

* Automatic memory mapping infrastructure that uses memremap() with MEMREMAP_WB caching policy to provide write-back cached virtual addresses for allocated physical memory from instance pools.

* Proper error handling and cleanup with automatic rollback of physical allocations when virtual mapping fails, preventing memory leaks in error conditions.

The allocation functions complement the existing physical memory pool management by providing a higher-level interface suitable for kernel code that requires virtual addresses, such as buffer management, data structure allocation, and inter-kernel communication buffers.

This interface enables multikernel subsystems to allocate kernel images, initramfs etc., maintaining proper resource isolation between multikernel instances.

---
 include/linux/multikernel.h |  6 +++
 kernel/multikernel/core.c   | 98 +++++++++++++++++++++++++++++++++++++
 2 files changed, 104 insertions(+)
include/linux/multikernel.h +6 -0
index c65d39a66b84..79611923649e 100644
@@ -280,6 +280,12 @@ int mk_instance_reserve_resources(struct mk_instance *instance,
  */
 void mk_instance_free_memory(struct mk_instance *instance);
 
+void *mk_instance_alloc(struct mk_instance *instance, size_t size, size_t align);
+void mk_instance_free(struct mk_instance *instance, void *virt_addr, size_t size);
+
+void *mk_kimage_alloc(struct kimage *image, size_t size, size_t align);
+void mk_kimage_free(struct kimage *image, void *virt_addr, size_t size);
+
 /**
  * String conversion helpers
  */
kernel/multikernel/core.c +98 -0

111 lines not on this page · raw message

Signature · 1 line
2.34.1
author
#14 lore raw

RFC PATCH v2 13/16 kernel: Introduce generic multikernel IPI communication framework

In reply to Cong Wang

From: Cong Wang <[email protected]>

This patch implements a comprehensive IPI-based communication system for multikernel environments, enabling data exchange between different kernel instances running on separate CPUs.

Key features include:

- Generic IPI handler registration and callback mechanism allowing modules to register for multikernel communication events
- Shared memory infrastructure on top of the general per-instance memory allocation infrastructure
- Per-instance data buffers in shared memory for efficient IPI payload transfer up to 256 bytes per message
- IRQ work integration for safe callback execution in interrupt context
- PFN-based flexible shared memory APIs for page-level data sharing
- Resource tracking integration for /proc/iomem visibility

It provides the key API multikernel_send_ipi_data() for sending typed data to target kernel instance and multikernel_register_handler() for registering IPI handler. Shared memory is established on top of the per-instance memory allocation infra.

This infrastructure enables multikernel instances to coordinate and share data while maintaining isolation on their respective CPU cores.

(Note, as a proof-of-concept, we have only implemented the x86 part.)

---
 arch/x86/kernel/smp.c       |   3 +
 include/linux/multikernel.h |  66 +++++
 kernel/multikernel/Makefile |   2 +-
 kernel/multikernel/ipi.c    | 471 ++++++++++++++++++++++++++++++++++++
 4 files changed, 541 insertions(+), 1 deletion(-)
 create mode 100644 kernel/multikernel/ipi.c
arch/x86/kernel/smp.c +3 -0
index e2eba09da7fc..2be7c1a777ef 100644
@@ -273,10 +273,13 @@ DEFINE_IDTENTRY_SYSVEC(sysvec_call_function_single)
 }
 
 #ifdef CONFIG_MULTIKERNEL
+void generic_multikernel_interrupt(void);
+
 DEFINE_IDTENTRY_SYSVEC(sysvec_multikernel)
 {
 	apic_eoi();
 	inc_irq_stat(irq_call_count);
+	generic_multikernel_interrupt();
 }
 #endif /* CONFIG_MULTIKERNEL */
 
include/linux/multikernel.h +66 -0

72 lines not on this page · raw message

kernel/multikernel/Makefile +1 -1
index d004c577f13d..b539acc656c6 100644
@@ -3,7 +3,7 @@
 # Makefile for multikernel support
 #
 
-obj-y += core.o mem.o kernfs.o dts.o
+obj-y += core.o mem.o kernfs.o dts.o ipi.o
 
 # Add libfdt include path for device tree parsing
 CFLAGS_dts.o = -I $(srctree)/scripts/dtc/libfdt
kernel/multikernel/ipi.c new file +471 -0

471 lines not on this page · raw message

Signature · 1 line
2.34.1
author
#15 lore raw

RFC PATCH v2 14/16 multikernel: Add messaging layer for inter-kernel communication

In reply to Cong Wang

From: Cong Wang <[email protected]>

Introduce a structured messaging system built on top of the existing multikernel IPI infrastructure to enable reliable communication between kernel instances running on different CPUs.

The messaging layer provides:

* Simple message format with type/subtype hierarchy for extensibility
* Support for I/O interrupt forwarding between kernel instances
* Resource management messages for CPU and memory hotplug operations
* Type-safe payload structures with validation
* Handler registration system for message processing
* Convenient inline functions for common operations

Message types include:
- MK_MSG_IO: I/O interrupt forwarding and load balancing
- MK_MSG_RESOURCE: CPU/memory add/remove operations
- MK_MSG_SYSTEM: System-level coordination messages
- MK_MSG_USER: User-defined message types

The implementation leverages the reliable nature of intra-machine IPIs, maintaining simplicity and performance. Messages are limited to the existing 256-byte IPI buffer size, with larger data transfers handled via the existing PFN-based shared memory mechanism.

This messaging foundation enables sophisticated multikernel coordination scenarios including dynamic resource allocation, interrupt load balancing, and system-wide state management.

---
 include/linux/multikernel.h    | 200 ++++++++++++++++++++++++
 kernel/multikernel/Makefile    |   2 +-
 kernel/multikernel/core.c      |   7 +
 kernel/multikernel/messaging.c | 278 +++++++++++++++++++++++++++++++++
 4 files changed, 486 insertions(+), 1 deletion(-)
 create mode 100644 kernel/multikernel/messaging.c
include/linux/multikernel.h +200 -0
index ee96bd2332b6..3bc07361145b 100644
@@ -80,6 +80,206 @@ void *mk_receive_map_page(struct mk_ipi_data *data);
 
 #define mk_receive_unmap_page(p) memunmap(p)
 
+/*
+ * Multikernel Messaging System
+ */
+
+/**
+ * Message type definitions - organized by category
+ */
+
+/* Top-level message categories */
+#define MK_MSG_IO           0x1000
+#define MK_MSG_RESOURCE     0x2000
+#define MK_MSG_SYSTEM       0x3000
+#define MK_MSG_USER         0x4000
+
+/* I/O interrupt forwarding subtypes */
+#define MK_IO_IRQ_FORWARD   (MK_MSG_IO + 1)
+#define MK_IO_IRQ_BALANCE   (MK_MSG_IO + 2)
+#define MK_IO_IRQ_MASK      (MK_MSG_IO + 3)
+#define MK_IO_IRQ_UNMASK    (MK_MSG_IO + 4)
+
+/* Resource management subtypes */
+#define MK_RES_CPU_ADD      (MK_MSG_RESOURCE + 1)
+#define MK_RES_CPU_REMOVE   (MK_MSG_RESOURCE + 2)
+#define MK_RES_MEM_ADD      (MK_MSG_RESOURCE + 3)
+#define MK_RES_MEM_REMOVE   (MK_MSG_RESOURCE + 4)
+#define MK_RES_QUERY        (MK_MSG_RESOURCE + 5)
+
+/* System management subtypes */
+#define MK_SYS_HEARTBEAT    (MK_MSG_SYSTEM + 1)
+#define MK_SYS_SHUTDOWN     (MK_MSG_SYSTEM + 2)
+
+/**
+ * Core message structure
+ */
+struct mk_message {
+	u32 msg_type;           /* Message type identifier */
+	u32 msg_subtype;        /* Subtype for specific operations */
+	u64 msg_id;             /* Optional message ID for correlation */
+	u32 payload_len;        /* Length of payload data */
+	u8 payload[];           /* Variable payload (up to remaining IPI buffer) */
+};
+
+/**
+ * Payload structures for specific message types
+ */
+
+/* I/O interrupt forwarding */
+struct mk_io_irq_payload {
+	u32 irq_number;         /* Hardware IRQ number */
+	u32 vector;             /* Interrupt vector */
+	u32 device_id;          /* Device identifier (optional) */
+	u32 flags;              /* Control flags (priority, etc.) */
+};
+
+/* IRQ control flags */
+#define MK_IRQ_HIGH_PRIORITY    0x01
+#define MK_IRQ_LOW_LATENCY      0x02
+#define MK_IRQ_EDGE_TRIGGERED   0x04
+#define MK_IRQ_LEVEL_TRIGGERED  0x08
+
+/* CPU resource operations */
+struct mk_cpu_resource_payload {
+	u32 cpu_id;             /* Physical CPU ID */
+	u32 numa_node;          /* NUMA node (optional) */
+	u32 flags;              /* CPU capabilities/attributes */
+};
+
+/* CPU capability flags */
+#define MK_CPU_HAS_AVX512       0x01
+#define MK_CPU_HAS_TSX          0x02
+#define MK_CPU_HYPERTHREAD      0x04
+
+/* Memory resource operations */
+struct mk_mem_resource_payload {
+	u64 start_pfn;          /* Starting page frame number */
+	u64 nr_pages;           /* Number of pages */
+	u32 numa_node;          /* NUMA node */
+	u32 mem_type;           /* Memory type (normal/DMA/etc.) */
+};
+
+/* Memory types */
+#define MK_MEM_NORMAL           0x01
+#define MK_MEM_DMA              0x02
+#define MK_MEM_DMA32            0x04
+#define MK_MEM_HIGHMEM          0x08
+
+/**
+ * Message handler callback type
+ */
+typedef void (*mk_msg_handler_t)(u32 msg_type, u32 subtype,
+				 void *payload, u32 payload_len, void *ctx);
+
+/**
+ * Message API functions
+ */
+
+/**
+ * mk_send_message - Send a message to another CPU
+ * @instance_id: Target multikernel instance ID
+ * @msg_type: Message type identifier
+ * @subtype: Message subtype
+ * @payload: Pointer to payload data (can be NULL)
+ * @payload_len: Length of payload data
+ *
+ * Returns 0 on success, negative error code on failure
+ */
+int mk_send_message(int instance_id, u32 msg_type, u32 subtype,
+		    void *payload, u32 payload_len);
+
+/**
+ * mk_register_msg_handler - Register handler for specific message type
+ * @msg_type: Message type to handle
+ * @handler: Handler function
+ * @ctx: Context pointer passed to handler
+ *
+ * Returns 0 on success, negative error code on failure
+ */
+int mk_register_msg_handler(u32 msg_type, mk_msg_handler_t handler, void *ctx);
+
+/**
+ * mk_unregister_msg_handler - Unregister message handler
+ * @msg_type: Message type to unregister
+ * @handler: Handler function to remove
+ *
+ * Returns 0 on success, negative error code on failure
+ */
+int mk_unregister_msg_handler(u32 msg_type, mk_msg_handler_t handler);
+
+/**
+ * Convenience functions for common message types
+ */
+
+/* I/O interrupt forwarding */
+static inline int mk_send_irq_forward(int instance_id, u32 irq_number,
+				      u32 vector, u32 device_id, u32 flags)
+{
+	struct mk_io_irq_payload payload = {
+		.irq_number = irq_number,
+		.vector = vector,
+		.device_id = device_id,
+		.flags = flags
+	};
+	return mk_send_message(instance_id, MK_MSG_IO, MK_IO_IRQ_FORWARD,
+			       &payload, sizeof(payload));
+}
+
+/* CPU resource management */
+static inline int mk_send_cpu_add(int instance_id, u32 cpu_id,
+				  u32 numa_node, u32 flags)
+{
+	struct mk_cpu_resource_payload payload = {
+		.cpu_id = cpu_id,
+		.numa_node = numa_node,
+		.flags = flags
+	};
+	return mk_send_message(instance_id, MK_MSG_RESOURCE, MK_RES_CPU_ADD,
+			       &payload, sizeof(payload));
+}
+
+static inline int mk_send_cpu_remove(int instance_id, u32 cpu_id)
+{
+	struct mk_cpu_resource_payload payload = {
+		.cpu_id = cpu_id,
+		.numa_node = 0,
+		.flags = 0
+	};
+	return mk_send_message(instance_id, MK_MSG_RESOURCE, MK_RES_CPU_REMOVE,
+			       &payload, sizeof(payload));
+}
+
+/* Memory resource management */
+static inline int mk_send_mem_add(int instance_id, u64 start_pfn, u64 nr_pages,
+				  u32 numa_node, u32 mem_type)
+{
+	struct mk_mem_resource_payload payload = {
+		.start_pfn = start_pfn,
+		.nr_pages = nr_pages,
+		.numa_node = numa_node,
+		.mem_type = mem_type
+	};
+	return mk_send_message(instance_id, MK_MSG_RESOURCE, MK_RES_MEM_ADD,
+			       &payload, sizeof(payload));
+}
+
+static inline int mk_send_mem_remove(int instance_id, u64 start_pfn, u64 nr_pages)
+{
+	struct mk_mem_resource_payload payload = {
+		.start_pfn = start_pfn,
+		.nr_pages = nr_pages,
+		.numa_node = 0,
+		.mem_type = 0
+	};
+	return mk_send_message(instance_id, MK_MSG_RESOURCE, MK_RES_MEM_REMOVE,
+			       &payload, sizeof(payload));
+}
+
+/* Messaging system functions */
+int __init mk_messaging_init(void);
+void mk_messaging_cleanup(void);
+
 struct resource;
 
 extern phys_addr_t multikernel_alloc(size_t size);
kernel/multikernel/Makefile +1 -1
index b539acc656c6..f133e1eaf534 100644
@@ -3,7 +3,7 @@
 # Makefile for multikernel support
 #
 
-obj-y += core.o mem.o kernfs.o dts.o ipi.o
+obj-y += core.o mem.o kernfs.o dts.o ipi.o messaging.o
 
 # Add libfdt include path for device tree parsing
 CFLAGS_dts.o = -I $(srctree)/scripts/dtc/libfdt
kernel/multikernel/core.c +7 -0
index ee7a21327ea5..37dbf0cf4be6 100644
@@ -505,9 +505,16 @@ static int __init multikernel_init(void)
 {
 	int ret;
 
+	ret = mk_messaging_init();
+	if (ret < 0) {
+		pr_err("Failed to initialize multikernel messaging: %d\n", ret);
+		return ret;
+	}
+
 	ret = mk_kernfs_init();
 	if (ret < 0) {
 		pr_err("Failed to initialize multikernel sysfs interface: %d\n", ret);
+		mk_messaging_cleanup();
 		return ret;
 	}
 
kernel/multikernel/messaging.c new file +278 -0

278 lines not on this page · raw message

Signature · 1 line
2.34.1
author
#16 lore raw

RFC PATCH v2 15/16 kexec: Integrate multikernel support with kexec_file_load()

In reply to Cong Wang

From: Cong Wang <[email protected]>

This commit introduces:

* Multikernel flag support in kexec_file_load by adding KEXEC_MULTIKERNEL to KEXEC_FILE_FLAGS, enabling user-space to specify multikernel operations through the file-based kexec interface with proper flag validation and ID extraction.

* Instance-based memory allocation for multikernel images through kexec_alloc_multikernel() that allocates kernel segments from instance-specific memory pools rather than system memory, ensuring compliance with device tree resource specifications.

* Multikernel control page allocation via kimage_alloc_multikernel_control_pages() that provides page-aligned control structures from instance pools with proper alignment validation and conflict detection against existing segments.

* Enhanced kimage_file_alloc_init() with multikernel instance association that extracts multikernel IDs from kexec flags, validates instance availability, establishes bidirectional cross-references, and updates instance states to LOADING during the load process.

* Integrated memory hole location in kexec_locate_mem_hole() that prioritizes multikernel instance pool allocation over system memory allocation, ensuring multikernel segments respect reserved memory boundaries and resource isolation.

The integration maintains compatibility with existing kexec_file_load() use cases, such as crash kernel, while extending it for multikernel case. Standard kexec operations continue to use system memory allocation, while multikernel operations automatically use instance-specific pools when the KEXEC_MULTIKERNEL flag is specified.

This enables user-space tools to load multikernel images using the more secure and flexible kexec_file_load interface rather than the legacy kexec_load syscall, providing better integration with modern security frameworks and signed kernel verification.

---
 include/linux/kexec.h |   3 +-
 kernel/kexec_core.c   |  61 ++++++++++++++++++++++
 kernel/kexec_file.c   | 116 +++++++++++++++++++++++++++++++++++++++++-
 3 files changed, 177 insertions(+), 3 deletions(-)
include/linux/kexec.h +2 -1
index 5e9e9ad1dfeb..b907b7a92fd2 100644
@@ -477,7 +477,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_CMA | KEXEC_FILE_FORCE_DTB)
+				 KEXEC_FILE_NO_CMA | KEXEC_FILE_FORCE_DTB | \
+				 KEXEC_MULTIKERNEL)
 
 /* flag to track if kexec reboot is in progress */
 extern bool kexec_in_progress;
kernel/kexec_core.c +61 -0

73 lines not on this page · raw message

kernel/kexec_file.c +114 -2

148 lines not on this page · raw message

Signature · 1 line
2.34.1
author
#17 lore raw

RFC PATCH v2 16/16 multikernel: Integrate Kexec HandOver framework for DTB preservation

In reply to Cong Wang

From: Cong Wang <[email protected]>

This commit introduces:

* Multikernel-specific KHO handling in arch/x86 that differentiates between regular KHO operations (requiring both FDT and scratch areas) and multikernel operations (requiring only FDT) with conditional setup data generation and proper boot parameter configuration.

* Enhanced KHO framework with multikernel-aware notifier chain (mk_kexec_register_notifier/mk_kexec_unregister_notifier) that provides dedicated callbacks for multikernel DTB preservation without the overhead of full KHO serialization infrastructure.

* Multikernel KHO finalization through mk_kexec_finalize() that creates minimal FDT structures with multikernel-v1 compatibility and calls specialized notifiers to preserve target instance DTBs in shared memory for seamless handover to spawned kernels.

* DTB restoration infrastructure (mk_kho_restore_dtbs()) that enables spawned kernels to automatically recover their device tree configurations from KHO shared memory during early boot, recreating instance structures with proper resource allocation.

* Integration with existing kexec_file_load workflow through automatic FDT page allocation for multikernel images and proper cleanup handling when images are freed, ensuring resource lifecycle management across kexec boundaries.

The KHO integration maintains full compatibility with existing KHO code path while extending it for the multikernel use case. Regular kexec operations continue to use standard KHO scratch area and serialization, while multikernel case uses the streamlined DTB-only protocol.

This enables seamless multikernel instance migration across kernel boundaries, allowing spawned kernels to automatically inherit their device tree configurations and resource allocations without manual intervention or complex boot parameter manipulation.

---
 arch/x86/kernel/kexec-bzimage64.c |  16 +-
 arch/x86/kernel/setup.c           |   8 +-
 drivers/of/kexec.c                |  20 +-
 include/linux/kexec_handover.h    |  40 ++++
 include/linux/multikernel.h       |  34 +++
 kernel/kexec_core.c               |  11 +
 kernel/kexec_file.c               |   9 +
 kernel/kexec_handover.c           | 197 ++++++++++++++++-
 kernel/multikernel/Kconfig        |   5 +
 kernel/multikernel/Makefile       |   2 +-
 kernel/multikernel/core.c         |   6 +
 kernel/multikernel/kho.c          | 356 ++++++++++++++++++++++++++++++
 12 files changed, 691 insertions(+), 13 deletions(-)
 create mode 100644 kernel/multikernel/kho.c
arch/x86/kernel/kexec-bzimage64.c +12 -4
index c3244ac680d1..8ed2b49bf086 100644
@@ -276,15 +276,23 @@ static void setup_kho(const struct kimage *image, struct boot_params *params,
 	sd->type = SETUP_KEXEC_KHO;
 	sd->len = sizeof(struct kho_data);
 
-	/* Only add if we have all KHO images in place */
-	if (!image->kho.fdt || !image->kho.scratch)
+	if (image->type == KEXEC_TYPE_MULTIKERNEL) {
+		if (!image->kho.fdt)
+			return;
+	} else if (!image->kho.fdt || !image->kho.scratch)
 		return;
 
 	/* Add setup data */
 	kho->fdt_addr = image->kho.fdt;
 	kho->fdt_size = PAGE_SIZE;
-	kho->scratch_addr = image->kho.scratch->mem;
-	kho->scratch_size = image->kho.scratch->bufsz;
+	if (image->type == KEXEC_TYPE_MULTIKERNEL) {
+		kho->scratch_addr = 0;
+		kho->scratch_size = 0;
+	} else {
+		kho->scratch_addr = image->kho.scratch->mem;
+		kho->scratch_size = image->kho.scratch->bufsz;
+	}
+
 	sd->next = params->hdr.setup_data;
 	params->hdr.setup_data = params_load_addr + setup_data_offset;
 }
arch/x86/kernel/setup.c +7 -1
index 8342c4e46bad..40e3cb19e06b 100644
@@ -467,7 +467,13 @@ static void __init add_kho(u64 phys_addr, u32 data_len)
 		return;
 	}
 
-	kho_populate(kho->fdt_addr, kho->fdt_size, kho->scratch_addr, kho->scratch_size);
+	if (kho->scratch_addr == 0 && kho->scratch_size == 0) {
+		pr_info("setup: detected multikernel KHO data\n");
+		mk_kho_populate(kho->fdt_addr, kho->fdt_size);
+	} else {
+		pr_info("setup: detected regular KHO data\n");
+		kho_populate(kho->fdt_addr, kho->fdt_size, kho->scratch_addr, kho->scratch_size);
+	}
 
 	early_memunmap(kho, size);
 }
drivers/of/kexec.c +15 -5
index 1ee2d31816ae..a27971be555c 100644
@@ -280,13 +280,21 @@ static int kho_add_chosen(const struct kimage *image, void *fdt, int chosen_node
 	if (ret && ret != -FDT_ERR_NOTFOUND)
 		return ret;
 
-	if (!image->kho.fdt || !image->kho.scratch)
+	if (image->type == KEXEC_TYPE_MULTIKERNEL) {
+		if (!image->kho.fdt)
+			return 0;
+	} else if (!image->kho.fdt || !image->kho.scratch)
 		return 0;
 
 	fdt_mem = image->kho.fdt;
 	fdt_len = PAGE_SIZE;
-	scratch_mem = image->kho.scratch->mem;
-	scratch_len = image->kho.scratch->bufsz;
+	if (image->type == KEXEC_TYPE_MULTIKERNEL) {
+		scratch_mem = 0;
+		scratch_len = 0;
+	} else {
+		scratch_mem = image->kho.scratch->mem;
+		scratch_len = image->kho.scratch->bufsz;
+	}
 
 	pr_debug("Adding kho metadata to DT");
 
@@ -294,8 +302,10 @@ static int kho_add_chosen(const struct kimage *image, void *fdt, int chosen_node
 				       fdt_mem, fdt_len);
 	if (ret)
 		return ret;
-	ret = fdt_appendprop_addrrange(fdt, 0, chosen_node, "linux,kho-scratch",
-				       scratch_mem, scratch_len);
+
+	if (scratch_mem && scratch_len)
+		ret = fdt_appendprop_addrrange(fdt, 0, chosen_node, "linux,kho-scratch",
+					       scratch_mem, scratch_len);
 
 #endif /* CONFIG_KEXEC_HANDOVER */
 	return ret;
include/linux/kexec_handover.h +40 -0
index 25042c1d8d54..480c4510a9ed 100644
@@ -64,9 +64,20 @@ int register_kho_notifier(struct notifier_block *nb);
 int unregister_kho_notifier(struct notifier_block *nb);
 
 void kho_memory_init(void);
+int kho_get_target_mk_id(struct kho_serialization *ser);
+struct kimage;
 
 void kho_populate(phys_addr_t fdt_phys, u64 fdt_len, phys_addr_t scratch_phys,
 		  u64 scratch_len);
+void mk_kho_populate(phys_addr_t fdt_phys, u64 fdt_len);
+
+/* Multikernel kexec notifier functions */
+int mk_kexec_register_notifier(struct notifier_block *nb);
+int mk_kexec_unregister_notifier(struct notifier_block *nb);
+int mk_kexec_finalize(struct kimage *target_image);
+
+/* KHO FDT access */
+phys_addr_t kho_get_fdt_phys(void);
 #else
 static inline bool kho_is_enabled(void)
 {
@@ -121,6 +132,11 @@ static inline int kho_retrieve_subtree(const char *name, phys_addr_t *phys)
 	return -EOPNOTSUPP;
 }
 
+static inline int kho_get_target_mk_id(struct kho_serialization *ser)
+{
+	return 0;
+}
+
 static inline int register_kho_notifier(struct notifier_block *nb)
 {
 	return -EOPNOTSUPP;
@@ -139,6 +155,30 @@ static inline void kho_populate(phys_addr_t fdt_phys, u64 fdt_len,
 				phys_addr_t scratch_phys, u64 scratch_len)
 {
 }
+
+static inline void mk_kho_populate(phys_addr_t fdt_phys, u64 fdt_len)
+{
+}
+
+static inline int mk_kexec_register_notifier(struct notifier_block *nb)
+{
+	return -EOPNOTSUPP;
+}
+
+static inline int mk_kexec_unregister_notifier(struct notifier_block *nb)
+{
+	return -EOPNOTSUPP;
+}
+
+static inline int mk_kexec_finalize(struct kimage *target_image)
+{
+	return -EOPNOTSUPP;
+}
+
+static inline phys_addr_t kho_get_fdt_phys(void)
+{
+	return 0;
+}
 #endif /* CONFIG_KEXEC_HANDOVER */
 
 #endif /* LINUX_KEXEC_HANDOVER_H */
include/linux/multikernel.h +34 -0
index 3bc07361145b..c463940eb27c 100644
@@ -622,4 +622,38 @@ static inline bool mk_is_resource_property(const char *prop_name)
 	return false;
 }
 
+/**
+ * KHO (Kexec HandOver) Integration Functions
+ *
+ * These functions provide KHO support for preserving and restoring
+ * multikernel instance device trees across kexec boundaries.
+ */
+
+/**
+ * mk_kho_restore_dtbs() - Restore DTBs from KHO shared memory
+ *
+ * Called during multikernel initialization to restore DTBs that were
+ * preserved by the previous kernel via KHO.
+ *
+ * Returns: 0 on success, negative error code on failure
+ */
+int __init mk_kho_restore_dtbs(void);
+
+/**
+ * mk_kho_init() - Initialize KHO support for multikernel
+ *
+ * Registers the KHO notifier and attempts to restore DTBs from
+ * a previous KHO boot.
+ *
+ * Returns: 0 on success, negative error code on failure
+ */
+int __init mk_kho_init(void);
+
+/**
+ * mk_kho_cleanup() - Cleanup KHO support for multikernel
+ *
+ * Unregisters the KHO notifier.
+ */
+void mk_kho_cleanup(void);
+
 #endif /* _LINUX_MULTIKERNEL_H */
kernel/kexec_core.c +11 -0
index 61ad01acd034..c5ed3fd90ede 100644
@@ -43,6 +43,7 @@
 #include <linux/objtool.h>
 #include <linux/kmsg_dump.h>
 #include <linux/multikernel.h>
+#include <linux/kexec_handover.h>
 #include <linux/dma-map-ops.h>
 #include <linux/memblock.h>
 
@@ -673,6 +674,10 @@ void kimage_free(struct kimage *image)
 		image->mk_instance = NULL;
 		pr_info("Freed multikernel ID %d\n", image->mk_id);
 	}
+	if (image->kho.fdt && image->type == KEXEC_TYPE_MULTIKERNEL) {
+		put_page(phys_to_page(image->kho.fdt));
+		image->kho.fdt = 0;
+	}
 
 #ifdef CONFIG_CRASH_DUMP
 	if (image->vmcoreinfo_data_copy) {
@@ -1591,6 +1596,12 @@ int multikernel_kexec_by_id(int mk_id)
 		goto unlock;
 	}
 
+	rc = mk_kexec_finalize(mk_image);
+	if (rc)
+		pr_warn("KHO finalization failed: %d\n", rc);
+	else
+		pr_info("KHO finalized for multikernel instance\n");
+
 	pr_info("Using multikernel image with ID %d (entry point: 0x%lx) on CPU %d\n",
 		mk_image->mk_id, mk_image->start, cpu);
 
kernel/kexec_file.c +9 -0
index f9979c1d9f9e..44b42ae34ef2 100644
@@ -329,6 +329,7 @@ kimage_file_alloc_init(struct kimage **rimage, int kernel_fd,
 	if (multikernel_load) {
 		struct mk_instance *instance;
 		int mk_id = KEXEC_GET_MK_ID(flags);
+		struct page *fdt_page;
 
 		/* Set multikernel image type for proper memory allocation */
 		image->type = KEXEC_TYPE_MULTIKERNEL;
@@ -367,6 +368,14 @@ kimage_file_alloc_init(struct kimage **rimage, int kernel_fd,
 		mk_instance_set_state(instance, MK_STATE_LOADING);
 
 		pr_info("Associated kimage with multikernel instance %d\n", mk_id);
+
+		fdt_page = alloc_page(GFP_KERNEL);
+		if (!fdt_page) {
+			pr_err("Failed to allocate FDT page for multikernel kimage\n");
+			ret = -ENOMEM;
+			goto out_free_image;
+		}
+		image->kho.fdt = page_to_phys(fdt_page);
 	}
 
 	ret = kimage_file_prepare_segments(image, kernel_fd, initrd_fd,
kernel/kexec_handover.c +195 -2

244 lines not on this page · raw message

kernel/multikernel/Kconfig +5 -0
index a9582a4d0c54..1b0191beffa7 100644
@@ -6,6 +6,7 @@
 config MULTIKERNEL
 	bool "Multikernel support"
 	depends on KEXEC_CORE
+	depends on KEXEC_HANDOVER
 	select LIBFDT
 	help
 	  Enable multikernel support, which allows running multiple kernel
@@ -17,5 +18,9 @@ config MULTIKERNEL
 	  - Device tree based resource specification
 	  - Memory pool management for kernel instances
 	  - Integration with kexec for kernel loading
+	  - KHO (Kexec HandOver) support for seamless DTB transfer
+
+	  Requires KEXEC_HANDOVER for preserving device trees and instance
+	  state across kernel boundaries.
 
 	  If unsure, say N.
kernel/multikernel/Makefile +1 -1
index f133e1eaf534..3d81a3233bba 100644
@@ -3,7 +3,7 @@
 # Makefile for multikernel support
 #
 
-obj-y += core.o mem.o kernfs.o dts.o ipi.o messaging.o
+obj-y += core.o mem.o kernfs.o dts.o ipi.o messaging.o kho.o
 
 # Add libfdt include path for device tree parsing
 CFLAGS_dts.o = -I $(srctree)/scripts/dtc/libfdt
kernel/multikernel/core.c +6 -0
index 37dbf0cf4be6..95d27b3b15cc 100644
@@ -518,6 +518,12 @@ static int __init multikernel_init(void)
 		return ret;
 	}
 
+	ret = mk_kho_init();
+	if (ret < 0) {
+		pr_warn("Failed to initialize KHO support: %d\n", ret);
+		/* Continue without KHO support - this is not fatal */
+	}
+
 	pr_info("Multikernel support initialized\n");
 	return 0;
 }
kernel/multikernel/kho.c new file +356 -0

356 lines not on this page · raw message

Signature · 1 line
2.34.1
#18 lore raw

Re: [RFC Patch v2 16/16] multikernel: Integrate Kexec HandOver framework for DTB preservation

kernel test robot noticed the following build warnings:

Hi Cong,

[This is a private test report for your RFC patch.] kernel test robot noticed the following build warnings:

[auto build test WARNING on tip/x86/core]
[also build test WARNING on powerpc/fixes robh/for-next linus/master v6.18-rc1] [cannot apply to powerpc/next next-20251017]
[If your patch is applied to the wrong git tree, kindly drop us a note. And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch#_base_tree_information]

url:    https://github.com/intel-lab-lkp/linux/commits/Cong-Wang/kexec-Introduce-multikernel-support-via-kexec/20251019-141940
base:   tip/x86/core
patch link:    https://lore.kernel.org/r/20251019061631.2235405-17-xiyou.wangcong%40gmail.com
patch subject: [RFC Patch v2 16/16] multikernel: Integrate Kexec HandOver framework for DTB preservation
config: i386-buildonly-randconfig-004-20251019 (https://download.01.org/0day-ci/archive/20251019/[email protected]/config)
compiler: clang version 20.1.8 (https://github.com/llvm/llvm-project 87f0227cb60147a26a1eeb4fb06e3b505e9c7261)
reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20251019/[email protected]/reproduce)

If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/

All warnings (new ones prefixed by >>):

In file included from arch/x86/pci/common.c:21:
In file included from arch/x86/include/asm/setup.h:72:

>> include/linux/kexec_handover.h:173:44: warning: declaration of 'struct kimage' will not be visible outside of this function [-Wvisibility]

173 | static inline int mk_kexec_finalize(struct kimage *target_image)
| ^
1 warning generated.

vim +173 include/linux/kexec_handover.h

172

 > 173	static inline int mk_kexec_finalize(struct kimage *target_image)

174 {
175 return -EOPNOTSUPP;
176 }
177

Signature · 2 lines
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki
#19 lore raw

Re: [RFC Patch v2 16/16] multikernel: Integrate Kexec HandOver framework for DTB preservation

In reply to Cong Wang
kernel test robot noticed the following build errors:

Hi Cong,

[This is a private test report for your RFC patch.] kernel test robot noticed the following build errors:

[auto build test ERROR on tip/x86/core]
[also build test ERROR on powerpc/fixes robh/for-next linus/master v6.18-rc1] [cannot apply to powerpc/next next-20251017]
[If your patch is applied to the wrong git tree, kindly drop us a note. And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch#_base_tree_information]

url:    https://github.com/intel-lab-lkp/linux/commits/Cong-Wang/kexec-Introduce-multikernel-support-via-kexec/20251019-141940
base:   tip/x86/core
patch link:    https://lore.kernel.org/r/20251019061631.2235405-17-xiyou.wangcong%40gmail.com
patch subject: [RFC Patch v2 16/16] multikernel: Integrate Kexec HandOver framework for DTB preservation
config: i386-buildonly-randconfig-002-20251019 (https://download.01.org/0day-ci/archive/20251019/[email protected]/config)
compiler: gcc-14 (Debian 14.2.0-19) 14.2.0
reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20251019/[email protected]/reproduce)

If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/

All error/warnings (new ones prefixed by >>):

In file included from arch/x86/include/asm/setup.h:72,

                    from arch/x86/kernel/ebda.c:6:
>> include/linux/kexec_handover.h:173:44: warning: 'struct kimage' declared inside parameter list will not be visible outside of this definition or declaration

173 | static inline int mk_kexec_finalize(struct kimage *target_image)
| ^~~~~~
--
ld: arch/x86/kernel/trampoline_64_bsp.o: in function `startup_32_vector_bsp':

>> (.x86_trampoline_bsp+0x1ec): undefined reference to `__KERNEL32_CS'

vim +173 include/linux/kexec_handover.h

172

 > 173	static inline int mk_kexec_finalize(struct kimage *target_image)

174 {
175 return -EOPNOTSUPP;
176 }
177

Signature · 2 lines
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki