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. Signed-off-by: Cong Wang <[email protected]> --- 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
