From: Cong Wang <xiyou.wangcong@gmail.com>
To: linux-kernel@vger.kernel.org
Cc: jiri@resnulli.us, stefanha@redhat.com,
multikernel@lists.linux.dev, pasha.tatashin@soleen.com,
Cong Wang <cwang@multikernel.io>,
Andrew Morton <akpm@linux-foundation.org>,
Baoquan He <bhe@redhat.com>, Alexander Graf <graf@amazon.com>,
Mike Rapoport <rppt@kernel.org>,
Changyuan Lyu <changyuanl@google.com>,
kexec@lists.infradead.org, linux-mm@kvack.org
Subject: [RFC Patch v2 12/16] multikernel: Introduce per-instance memory allocation interface
Date: Sat, 18 Oct 2025 23:16:26 -0700 [thread overview]
Message-ID: <20251019061631.2235405-13-xiyou.wangcong@gmail.com> (raw)
In-Reply-To: <20251019061631.2235405-1-xiyou.wangcong@gmail.com>
From: Cong Wang <cwang@multikernel.io>
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.
Signed-off-by: Cong Wang <cwang@multikernel.io>
---
include/linux/multikernel.h | 6 +++
kernel/multikernel/core.c | 98 +++++++++++++++++++++++++++++++++++++
2 files changed, 104 insertions(+)
diff --git a/include/linux/multikernel.h b/include/linux/multikernel.h
index c65d39a66b84..79611923649e 100644
--- a/include/linux/multikernel.h
+++ b/include/linux/multikernel.h
@@ -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
*/
diff --git a/kernel/multikernel/core.c b/kernel/multikernel/core.c
index 52bf8e38206a..ee7a21327ea5 100644
--- a/kernel/multikernel/core.c
+++ b/kernel/multikernel/core.c
@@ -7,7 +7,10 @@
#include <linux/init.h>
#include <linux/cpumask.h>
#include <linux/cpu.h>
+#include <linux/io.h>
+#include <linux/kexec.h>
#include <linux/multikernel.h>
+#include <asm/page.h>
#include "internal.h"
/**
@@ -403,6 +406,101 @@ int mk_instance_reserve_resources(struct mk_instance *instance,
return 0;
}
+/**
+ * Per-instance memory pool management
+ */
+
+/**
+ * mk_instance_alloc() - Allocate memory from instance pool
+ * @instance: Instance to allocate from
+ * @size: Size to allocate
+ * @align: Alignment requirement (must be power of 2)
+ *
+ * Returns virtual address of allocated memory, or NULL on failure.
+ */
+void *mk_instance_alloc(struct mk_instance *instance, size_t size, size_t align)
+{
+ phys_addr_t phys_addr;
+ void *virt_addr;
+
+ if (!instance || !instance->instance_pool) {
+ pr_debug("mk_instance_alloc: instance %p has no pool\n", instance);
+ return NULL;
+ }
+
+ /* Allocate from instance pool with alignment */
+ phys_addr = multikernel_instance_alloc(instance->instance_pool, size, align);
+ if (!phys_addr) {
+ pr_debug("Failed to allocate %zu bytes from instance pool (align=0x%zx)\n", size, align);
+ return NULL;
+ }
+
+ /* Map to virtual address space */
+ virt_addr = memremap(phys_addr, size, MEMREMAP_WB);
+ if (!virt_addr) {
+ pr_err("Failed to map instance memory at 0x%llx\n", (unsigned long long)phys_addr);
+ multikernel_instance_free(instance->instance_pool, phys_addr, size);
+ return NULL;
+ }
+
+ return virt_addr;
+}
+
+/**
+ * mk_instance_free() - Free memory back to instance pool
+ * @instance: Instance to free to
+ * @virt_addr: Virtual address to free
+ * @size: Size to free
+ */
+void mk_instance_free(struct mk_instance *instance, void *virt_addr, size_t size)
+{
+ phys_addr_t phys_addr;
+
+ if (!instance || !instance->instance_pool || !virt_addr)
+ return;
+
+ phys_addr = virt_to_phys(virt_addr);
+ memunmap(virt_addr);
+ multikernel_instance_free(instance->instance_pool, phys_addr, size);
+}
+
+/**
+ * Kimage-based memory pool access functions
+ *
+ * These provide convenient wrappers for accessing instance memory pools
+ * through the kimage structure, commonly used in kexec code paths.
+ */
+
+/**
+ * mk_kimage_alloc() - Allocate memory from kimage's instance pool
+ * @image: kimage with associated mk_instance
+ * @size: Size to allocate
+ * @align: Alignment requirement (must be power of 2)
+ *
+ * Returns virtual address of allocated memory, or NULL on failure.
+ */
+void *mk_kimage_alloc(struct kimage *image, size_t size, size_t align)
+{
+ if (!image || !image->mk_instance)
+ return NULL;
+
+ return mk_instance_alloc(image->mk_instance, size, align);
+}
+
+/**
+ * mk_kimage_free() - Free memory back to kimage's instance pool
+ * @image: kimage with associated mk_instance
+ * @virt_addr: Virtual address to free
+ * @size: Size to free
+ */
+void mk_kimage_free(struct kimage *image, void *virt_addr, size_t size)
+{
+ if (!image || !image->mk_instance)
+ return;
+
+ mk_instance_free(image->mk_instance, virt_addr, size);
+}
+
static int __init multikernel_init(void)
{
int ret;
--
2.34.1
next prev parent reply other threads:[~2025-10-19 6:17 UTC|newest]
Thread overview: 17+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-10-19 6:16 [RFC Patch v2 00/16] kernel: Introduce multikernel architecture support Cong Wang
2025-10-19 6:16 ` [RFC Patch v2 01/16] kexec: Introduce multikernel support via kexec Cong Wang
2025-10-19 6:16 ` [RFC Patch v2 02/16] x86: Introduce SMP INIT trampoline for multikernel CPU bootstrap Cong Wang
2025-10-19 6:16 ` [RFC Patch v2 03/16] multikernel: Introduce basic multikernel subsystem infrastructure Cong Wang
2025-10-19 6:16 ` [RFC Patch v2 04/16] x86: Introduce MULTIKERNEL_VECTOR for inter-kernel communication Cong Wang
2025-10-19 6:16 ` [RFC Patch v2 05/16] x86: Introduce arch_cpu_physical_id() to obtain physical CPU ID Cong Wang
2025-10-19 6:16 ` [RFC Patch v2 06/16] multikernel: Introduce physical memory reservation and allocation Cong Wang
2025-10-19 6:16 ` [RFC Patch v2 07/16] kexec: Implement dynamic kimage tracking Cong Wang
2025-10-19 6:16 ` [RFC Patch v2 08/16] multikernel: Introduce device-tree based kernfs interface Cong Wang
2025-10-19 6:16 ` [RFC Patch v2 09/16] kexec: Integrate multikernel instance management with kexec subsystem Cong Wang
2025-10-19 6:16 ` [RFC Patch v2 10/16] Documentation: Add multikernel usage Cong Wang
2025-10-19 6:16 ` [RFC Patch v2 11/16] kexec: Add /proc/kimage interface for kimage tracking Cong Wang
2025-10-19 6:16 ` Cong Wang [this message]
2025-10-19 6:16 ` [RFC Patch v2 13/16] kernel: Introduce generic multikernel IPI communication framework Cong Wang
2025-10-19 6:16 ` [RFC Patch v2 14/16] multikernel: Add messaging layer for inter-kernel communication Cong Wang
2025-10-19 6:16 ` [RFC Patch v2 15/16] kexec: Integrate multikernel support with kexec_file_load() Cong Wang
2025-10-19 6:16 ` [RFC Patch v2 16/16] multikernel: Integrate Kexec HandOver framework for DTB preservation Cong Wang
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20251019061631.2235405-13-xiyou.wangcong@gmail.com \
--to=xiyou.wangcong@gmail.com \
--cc=akpm@linux-foundation.org \
--cc=bhe@redhat.com \
--cc=changyuanl@google.com \
--cc=cwang@multikernel.io \
--cc=graf@amazon.com \
--cc=jiri@resnulli.us \
--cc=kexec@lists.infradead.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-mm@kvack.org \
--cc=multikernel@lists.linux.dev \
--cc=pasha.tatashin@soleen.com \
--cc=rppt@kernel.org \
--cc=stefanha@redhat.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox