[PATCH] config-tools: HV and VM memory address conflict


Li, Ziheng
 

From 20bb6c70bd830056406817b8fc7412fd8abbdd4d Mon Sep 17 00:00:00 2001
From: zihengL1 <ziheng.li@...>
Date: Thu, 21 Jul 2022 17:48:04 +0800
Subject: [PATCH] config-tools: HV and VM memory address conflict

Fixed the problem that acrn can still build normally
when the memory addresses of HV and VM conflict,
which causes the hypervisor to hang.

Now, if there is a memory addressed conflict problem,
an error will be reported.

Tracked-On: #7913
Signed-off-by: Ziheng Li <ziheng.li@...>
---
.../static_allocators/memory_allocator.py | 28 ++++++++++++-------
1 file changed, 18 insertions(+), 10 deletions(-)

diff --git a/misc/config_tools/static_allocators/memory_allocator.py b/misc/config_tools/static_allocators/memory_allocator.py
index eb00f2749..822974e6e 100644
--- a/misc/config_tools/static_allocators/memory_allocator.py
+++ b/misc/config_tools/static_allocators/memory_allocator.py
@@ -11,13 +11,19 @@ import lib.error
sys.path.append(os.path.join(os.path.dirname(os.path.abspath(__file__)), '..', 'library'))
import common, math, logging

-def import_memory_info(board_etree):
+def import_memory_info(board_etree, allocation_etree):
ram_range = {}
start = board_etree.xpath("/acrn-config/memory/range/@start")
size = board_etree.xpath("/acrn-config/memory/range/@size")
+
+ hv_start_addr = int(common.get_node("/acrn-config/hv/MEMORY/HV_RAM_START/text()", allocation_etree), 16)
+ hv_ram_size = int(common.get_node("/acrn-config/hv/MEMORY/HV_RAM_SIZE/text()", allocation_etree), 16)
for i in range(len(start)):
start_hex = int(start[i], 16)
size_hex = int(size[i], 10)
+ if start_hex < hv_start_addr and start_hex + size_hex > hv_start_addr + hv_ram_size:
+ ram_range[start_hex] = hv_start_addr - start_hex
+ ram_range[hv_start_addr + hv_ram_size] = start_hex + size_hex - hv_start_addr - hv_ram_size
ram_range[start_hex] = size_hex

return ram_range
@@ -79,20 +85,22 @@ def alloc_hpa_region(ram_range_info, mem_info_list, vm_node_index_list):
mem_key = sorted(ram_range_info)
for mem_start in mem_key:
mem_size = ram_range_info[mem_start]
+ mem_end = mem_start + mem_size
for hpa_start in hpa_key:
hpa_size = mem_info_list[vm_index][hpa_start]
+ hpa_end = hpa_start + hpa_size
if hpa_start != 0:
- if mem_start < hpa_start and mem_start + mem_size > hpa_start + hpa_size:
+ if mem_start < hpa_start and mem_end > hpa_end:
ram_range_info[mem_start] = hpa_start - mem_start
- ram_range_info[hpa_start + hpa_size] = mem_start + mem_size - hpa_start - hpa_size
- elif mem_start == hpa_start and mem_start + mem_size > hpa_start + hpa_size:
+ ram_range_info[hpa_end] = mem_end - hpa_end
+ elif mem_start == hpa_start and mem_start + mem_size > hpa_end:
del ram_range_info[mem_start]
- ram_range_info[hpa_start + hpa_size] = mem_start + mem_size - hpa_start - hpa_size
- elif mem_start < hpa_start and mem_start + mem_size == hpa_start + hpa_size:
+ ram_range_info[hpa_end] = mem_end - hpa_end
+ elif mem_start < hpa_start and mem_end == hpa_end:
ram_range_info[mem_start] = hpa_start - mem_start
- elif mem_start == hpa_start and mem_start + mem_size == hpa_start + hpa_size:
+ elif mem_start == hpa_start and mem_end == hpa_end:
del ram_range_info[mem_start]
- elif mem_start > hpa_start or mem_start + mem_size < hpa_start + hpa_size:
+ elif mem_start > hpa_start or mem_end < hpa_end:
raise lib.error.ResourceError(f"Start address of HPA is out of available memory range: vm id: {vm_index}, hpa_start: {hpa_start}.")
elif mem_size < hpa_size:
raise lib.error.ResourceError(f"Size of HPA is out of available memory range: vm id: {vm_index}, hpa_size: {hpa_size}.")
@@ -149,14 +157,14 @@ def write_hpa_info(allocation_etree, mem_info_list, vm_node_index_list):
region_index = region_index + 1

def alloc_vm_memory(board_etree, scenario_etree, allocation_etree):
- ram_range_info = import_memory_info(board_etree)
+ ram_range_info = import_memory_info(board_etree, allocation_etree)
ram_range_info, mem_info_list, vm_node_index_list = alloc_memory(scenario_etree, ram_range_info)
write_hpa_info(allocation_etree, mem_info_list, vm_node_index_list)

def allocate_hugepages(board_etree, scenario_etree, allocation_etree):
hugepages_1gb = 0
hugepages_2mb = 0
- ram_range_info = import_memory_info(board_etree)
+ ram_range_info = import_memory_info(board_etree, allocation_etree)
total_hugepages = int(sum(ram_range_info[i] for i in ram_range_info if i >= 0x100000000)*0.98/(1024*1024*1024) \
- sum(int(i) for i in scenario_etree.xpath("//vm[load_order = 'PRE_LAUNCHED_VM']/memory/hpa_region/size_hpa/text()"))/1024 \
- 5 - 300/1024 * len(scenario_etree.xpath("//virtio_devices/gpu")))
--
2.35.1.windows.2

Join {acrn-dev@lists.projectacrn.org to automatically receive all group messages.