[RFC PATCH 2/2] hv: refine the sworld memory allocate


chenli.wei
 

From: Chenli Wei <chenli.wei@...>

The current code uses a predefined sworld memory array to reserve memory
for trusty VMs, and assume all post launched VMs are trusty VM which is
not correct.

This patch statically reserved memory just for trusty VMs and save 16M
memory for every non trusty VM.

Signed-off-by: Chenli Wei <chenli.wei@...>
---
hypervisor/arch/x86/guest/vm.c | 21 ++++++++++++++++++---
1 file changed, 18 insertions(+), 3 deletions(-)

diff --git a/hypervisor/arch/x86/guest/vm.c b/hypervisor/arch/x86/guest/vm.c
index bdd31d3fa..9593815a4 100644
--- a/hypervisor/arch/x86/guest/vm.c
+++ b/hypervisor/arch/x86/guest/vm.c
@@ -48,7 +48,7 @@
/* Local variables */

/* pre-assumption: TRUSTY_RAM_SIZE is 2M aligned */
-static struct page post_user_vm_sworld_memory[MAX_POST_VM_NUM][TRUSTY_RAM_SIZE >> PAGE_SHIFT] __aligned(MEM_2M);
+static struct page post_user_vm_sworld_memory[MAX_TRUSTY_VM_NUM][TRUSTY_RAM_SIZE >> PAGE_SHIFT] __aligned(MEM_2M);

static struct acrn_vm vm_array[CONFIG_MAX_VM_NUM] __aligned(PAGE_SIZE);

@@ -635,6 +635,22 @@ void prepare_vm_identical_memmap(struct acrn_vm *vm, uint16_t e820_entry_type, u
}
}

+int16_t get_sworld_page_idx(uint16_t vm_id)
+{
+ int16_t i;
+ int16_t page_idx = 0;
+ struct acrn_vm_config *vm_config;
+
+ for (i = 0; i < vm_id; i++) {
+ vm_config = get_vm_config(vm_id);
+ if ((vm_config->guest_flags & GUEST_FLAG_SECURE_WORLD_ENABLED) != 0U) {
+ page_idx += 1;
+ }
+ }
+
+ return page_idx;
+}
+
/**
* @pre vm_id < CONFIG_MAX_VM_NUM && vm_config != NULL && rtn_vm != NULL
* @pre vm->state == VM_POWERED_OFF
@@ -667,8 +683,7 @@ int32_t create_vm(uint16_t vm_id, uint64_t pcpu_bitmap, struct acrn_vm_config *v
vm->sworld_control.flag.supported = 1U;
}
if (vm->sworld_control.flag.supported != 0UL) {
- uint16_t service_vm_id = (get_service_vm())->vm_id;
- uint16_t page_idx = vmid_2_rel_vmid(service_vm_id, vm_id) - 1U;
+ uint16_t page_idx = get_sworld_page_idx(vm_id);

ept_add_mr(vm, (uint64_t *)vm->arch_vm.nworld_eptp,
hva2hpa(post_user_vm_sworld_memory[page_idx]),
--
2.25.1


Eddie Dong
 

-----Original Message-----
From: acrn-dev@... <acrn-dev@...> On
Behalf Of chenli.wei
Sent: Monday, September 26, 2022 10:31 AM
To: Wang, Yu1 <yu1.wang@...>; Mao, Junjie <junjie.mao@...>;
acrn-dev@...
Cc: Wei, Chenli <chenli.wei@...>
Subject: [acrn-dev] [RFC PATCH 2/2] hv: refine the sworld memory allocate

From: Chenli Wei <chenli.wei@...>

The current code uses a predefined sworld memory array to reserve memory
for trusty VMs, and assume all post launched VMs are trusty VM which is not
correct.

This patch statically reserved memory just for trusty VMs and save 16M
memory for every non trusty VM.

Signed-off-by: Chenli Wei <chenli.wei@...>
---
hypervisor/arch/x86/guest/vm.c | 21 ++++++++++++++++++---
1 file changed, 18 insertions(+), 3 deletions(-)

diff --git a/hypervisor/arch/x86/guest/vm.c
b/hypervisor/arch/x86/guest/vm.c index bdd31d3fa..9593815a4 100644
--- a/hypervisor/arch/x86/guest/vm.c
+++ b/hypervisor/arch/x86/guest/vm.c
@@ -48,7 +48,7 @@
/* Local variables */

/* pre-assumption: TRUSTY_RAM_SIZE is 2M aligned */ -static struct page
post_user_vm_sworld_memory[MAX_POST_VM_NUM][TRUSTY_RAM_SIZE >>
PAGE_SHIFT] __aligned(MEM_2M);
+static struct page
+post_user_vm_sworld_memory[MAX_TRUSTY_VM_NUM][TRUSTY_RAM_SIZ
E >>
+PAGE_SHIFT] __aligned(MEM_2M);

static struct acrn_vm vm_array[CONFIG_MAX_VM_NUM]
__aligned(PAGE_SIZE);

@@ -635,6 +635,22 @@ void prepare_vm_identical_memmap(struct
acrn_vm *vm, uint16_t e820_entry_type, u
}
}

+int16_t get_sworld_page_idx(uint16_t vm_id) {
This is to get relative index of VM with secure world, right?
If yes, this name is bad.

+ int16_t i;
+ int16_t page_idx = 0;
+ struct acrn_vm_config *vm_config;
+
+ for (i = 0; i < vm_id; i++) {
+ vm_config = get_vm_config(vm_id);
+ if ((vm_config->guest_flags &
GUEST_FLAG_SECURE_WORLD_ENABLED) != 0U) {
+ page_idx += 1;
+ }
+ }
+
Need to make sure it is smaller than MAX_TRUSTY_VM_NUM.


+ return page_idx;
+}
+
/**
* @pre vm_id < CONFIG_MAX_VM_NUM && vm_config != NULL &&
rtn_vm != NULL
* @pre vm->state == VM_POWERED_OFF
@@ -667,8 +683,7 @@ int32_t create_vm(uint16_t vm_id, uint64_t
pcpu_bitmap, struct acrn_vm_config *v
vm->sworld_control.flag.supported = 1U;
}
if (vm->sworld_control.flag.supported != 0UL) {
- uint16_t service_vm_id = (get_service_vm())->vm_id;
- uint16_t page_idx = vmid_2_rel_vmid(service_vm_id,
vm_id) - 1U;
+ uint16_t page_idx = get_sworld_page_idx(vm_id);

ept_add_mr(vm, (uint64_t *)vm-
arch_vm.nworld_eptp,
hva2hpa(post_user_vm_sworld_memory[page_idx]),
--
2.25.1





chenli.wei
 

On 9/27/2022 12:22 AM, Eddie Dong wrote:

-----Original Message-----
From: acrn-dev@... <acrn-dev@...> On
Behalf Of chenli.wei
Sent: Monday, September 26, 2022 10:31 AM
To: Wang, Yu1 <yu1.wang@...>; Mao, Junjie <junjie.mao@...>;
acrn-dev@...
Cc: Wei, Chenli <chenli.wei@...>
Subject: [acrn-dev] [RFC PATCH 2/2] hv: refine the sworld memory allocate

From: Chenli Wei <chenli.wei@...>

The current code uses a predefined sworld memory array to reserve memory
for trusty VMs, and assume all post launched VMs are trusty VM which is not
correct.

This patch statically reserved memory just for trusty VMs and save 16M
memory for every non trusty VM.

Signed-off-by: Chenli Wei <chenli.wei@...>
---
hypervisor/arch/x86/guest/vm.c | 21 ++++++++++++++++++---
1 file changed, 18 insertions(+), 3 deletions(-)

diff --git a/hypervisor/arch/x86/guest/vm.c
b/hypervisor/arch/x86/guest/vm.c index bdd31d3fa..9593815a4 100644
--- a/hypervisor/arch/x86/guest/vm.c
+++ b/hypervisor/arch/x86/guest/vm.c
@@ -48,7 +48,7 @@
/* Local variables */

/* pre-assumption: TRUSTY_RAM_SIZE is 2M aligned */ -static struct page
post_user_vm_sworld_memory[MAX_POST_VM_NUM][TRUSTY_RAM_SIZE >>
PAGE_SHIFT] __aligned(MEM_2M);
+static struct page
+post_user_vm_sworld_memory[MAX_TRUSTY_VM_NUM][TRUSTY_RAM_SIZ
E >>
+PAGE_SHIFT] __aligned(MEM_2M);

static struct acrn_vm vm_array[CONFIG_MAX_VM_NUM]
__aligned(PAGE_SIZE);

@@ -635,6 +635,22 @@ void prepare_vm_identical_memmap(struct
acrn_vm *vm, uint16_t e820_entry_type, u
}
}

+int16_t get_sworld_page_idx(uint16_t vm_id) {
This is to get relative index of VM with secure world, right?
If yes, this name is bad.
Yes, this function will return the index of the sworld memory array:

"static struct page post_user_vm_sworld_memory[MAX_TRUSTY_VM_NUM][TRUSTY_RAM_SIZE >> PAGE_SHIFT] __aligned(MEM_2M);"

Is better to rename it to "get_sworld_memory_index" ?


+ int16_t i;
+ int16_t page_idx = 0;
+ struct acrn_vm_config *vm_config;
+
+ for (i = 0; i < vm_id; i++) {
+ vm_config = get_vm_config(vm_id);
+ if ((vm_config->guest_flags &
GUEST_FLAG_SECURE_WORLD_ENABLED) != 0U) {
+ page_idx += 1;
+ }
+ }
+
Need to make sure it is smaller than MAX_TRUSTY_VM_NUM.
OK, I will add some check logic.

BTW, the "MAX_TRUSTY_VM_NUM" is not a user config parameter, the offline tool defined it by count the trusty VM number of the scenario.



+ return page_idx;
+}
+
/**
* @pre vm_id < CONFIG_MAX_VM_NUM && vm_config != NULL &&
rtn_vm != NULL
* @pre vm->state == VM_POWERED_OFF
@@ -667,8 +683,7 @@ int32_t create_vm(uint16_t vm_id, uint64_t
pcpu_bitmap, struct acrn_vm_config *v
vm->sworld_control.flag.supported = 1U;
}
if (vm->sworld_control.flag.supported != 0UL) {
- uint16_t service_vm_id = (get_service_vm())->vm_id;
- uint16_t page_idx = vmid_2_rel_vmid(service_vm_id,
vm_id) - 1U;
+ uint16_t page_idx = get_sworld_page_idx(vm_id);

ept_add_mr(vm, (uint64_t *)vm-
arch_vm.nworld_eptp,
hva2hpa(post_user_vm_sworld_memory[page_idx]),
--
2.25.1







Eddie Dong
 

-----Original Message-----
From: acrn-dev@... <acrn-dev@...> On
Behalf Of chenli.wei
Sent: Tuesday, September 27, 2022 3:15 AM
To: acrn-dev@...; Wang, Yu1 <yu1.wang@...>; Mao,
Junjie <junjie.mao@...>
Cc: Wei, Chenli <chenli.wei@...>
Subject: Re: [acrn-dev] [RFC PATCH 2/2] hv: refine the sworld memory allocate


On 9/27/2022 12:22 AM, Eddie Dong wrote:

-----Original Message-----
From: acrn-dev@... <acrn-dev@...>
On Behalf Of chenli.wei
Sent: Monday, September 26, 2022 10:31 AM
To: Wang, Yu1 <yu1.wang@...>; Mao, Junjie
<junjie.mao@...>; acrn-dev@...
Cc: Wei, Chenli <chenli.wei@...>
Subject: [acrn-dev] [RFC PATCH 2/2] hv: refine the sworld memory
allocate

From: Chenli Wei <chenli.wei@...>

The current code uses a predefined sworld memory array to reserve
memory for trusty VMs, and assume all post launched VMs are trusty VM
which is not correct.

This patch statically reserved memory just for trusty VMs and save
16M memory for every non trusty VM.

Signed-off-by: Chenli Wei <chenli.wei@...>
---
hypervisor/arch/x86/guest/vm.c | 21 ++++++++++++++++++---
1 file changed, 18 insertions(+), 3 deletions(-)

diff --git a/hypervisor/arch/x86/guest/vm.c
b/hypervisor/arch/x86/guest/vm.c index bdd31d3fa..9593815a4 100644
--- a/hypervisor/arch/x86/guest/vm.c
+++ b/hypervisor/arch/x86/guest/vm.c
@@ -48,7 +48,7 @@
/* Local variables */

/* pre-assumption: TRUSTY_RAM_SIZE is 2M aligned */ -static struct
page
post_user_vm_sworld_memory[MAX_POST_VM_NUM][TRUSTY_RAM_SIZE >>
PAGE_SHIFT] __aligned(MEM_2M);
+static struct page
+post_user_vm_sworld_memory[MAX_TRUSTY_VM_NUM][TRUSTY_RAM_SIZ
E >>
+PAGE_SHIFT] __aligned(MEM_2M);

static struct acrn_vm vm_array[CONFIG_MAX_VM_NUM]
__aligned(PAGE_SIZE);

@@ -635,6 +635,22 @@ void prepare_vm_identical_memmap(struct
acrn_vm *vm, uint16_t e820_entry_type, u
}
}

+int16_t get_sworld_page_idx(uint16_t vm_id) {
This is to get relative index of VM with secure world, right?
If yes, this name is bad.
Yes, this function will return the index of the sworld memory array:

"static struct page
post_user_vm_sworld_memory[MAX_TRUSTY_VM_NUM][TRUSTY_RAM_SIZE
>> PAGE_SHIFT] __aligned(MEM_2M);"

Is better to rename it to "get_sworld_memory_index" ?
Get_svm_index? Or get_sword_vm_index?



+ int16_t i;
+ int16_t page_idx = 0;
+ struct acrn_vm_config *vm_config;
+
+ for (i = 0; i < vm_id; i++) {
+ vm_config = get_vm_config(vm_id);
+ if ((vm_config->guest_flags &
GUEST_FLAG_SECURE_WORLD_ENABLED) != 0U) {
+ page_idx += 1;
+ }
+ }
+
Need to make sure it is smaller than MAX_TRUSTY_VM_NUM.
OK, I will add some check logic.

BTW, the "MAX_TRUSTY_VM_NUM" is not a user config parameter, the
offline tool defined it by count the trusty VM number of the scenario.
That is fine.




+ return page_idx;
+}
+
/**
* @pre vm_id < CONFIG_MAX_VM_NUM && vm_config != NULL &&
rtn_vm !=
NULL
* @pre vm->state == VM_POWERED_OFF @@ -667,8 +683,7 @@
int32_t
create_vm(uint16_t vm_id, uint64_t pcpu_bitmap, struct acrn_vm_config
*v
vm->sworld_control.flag.supported = 1U;
}
if (vm->sworld_control.flag.supported != 0UL) {
- uint16_t service_vm_id = (get_service_vm())->vm_id;
- uint16_t page_idx = vmid_2_rel_vmid(service_vm_id,
vm_id) - 1U;
+ uint16_t page_idx = get_sworld_page_idx(vm_id);

ept_add_mr(vm, (uint64_t *)vm-
arch_vm.nworld_eptp,
hva2hpa(post_user_vm_sworld_memory[page_idx]),
--
2.25.1










chenli.wei
 

On 9/28/2022 4:26 AM, Eddie Dong wrote:

-----Original Message-----
From: acrn-dev@... <acrn-dev@...> On
Behalf Of chenli.wei
Sent: Tuesday, September 27, 2022 3:15 AM
To: acrn-dev@...; Wang, Yu1 <yu1.wang@...>; Mao,
Junjie <junjie.mao@...>
Cc: Wei, Chenli <chenli.wei@...>
Subject: Re: [acrn-dev] [RFC PATCH 2/2] hv: refine the sworld memory allocate


On 9/27/2022 12:22 AM, Eddie Dong wrote:
-----Original Message-----
From: acrn-dev@... <acrn-dev@...>
On Behalf Of chenli.wei
Sent: Monday, September 26, 2022 10:31 AM
To: Wang, Yu1 <yu1.wang@...>; Mao, Junjie
<junjie.mao@...>; acrn-dev@...
Cc: Wei, Chenli <chenli.wei@...>
Subject: [acrn-dev] [RFC PATCH 2/2] hv: refine the sworld memory
allocate

From: Chenli Wei <chenli.wei@...>

The current code uses a predefined sworld memory array to reserve
memory for trusty VMs, and assume all post launched VMs are trusty VM
which is not correct.

This patch statically reserved memory just for trusty VMs and save
16M memory for every non trusty VM.

Signed-off-by: Chenli Wei <chenli.wei@...>
---
hypervisor/arch/x86/guest/vm.c | 21 ++++++++++++++++++---
1 file changed, 18 insertions(+), 3 deletions(-)

diff --git a/hypervisor/arch/x86/guest/vm.c
b/hypervisor/arch/x86/guest/vm.c index bdd31d3fa..9593815a4 100644
--- a/hypervisor/arch/x86/guest/vm.c
+++ b/hypervisor/arch/x86/guest/vm.c
@@ -48,7 +48,7 @@
/* Local variables */

/* pre-assumption: TRUSTY_RAM_SIZE is 2M aligned */ -static struct
page
post_user_vm_sworld_memory[MAX_POST_VM_NUM][TRUSTY_RAM_SIZE >>
PAGE_SHIFT] __aligned(MEM_2M);
+static struct page
+post_user_vm_sworld_memory[MAX_TRUSTY_VM_NUM][TRUSTY_RAM_SIZ
E >>
+PAGE_SHIFT] __aligned(MEM_2M);

static struct acrn_vm vm_array[CONFIG_MAX_VM_NUM]
__aligned(PAGE_SIZE);

@@ -635,6 +635,22 @@ void prepare_vm_identical_memmap(struct
acrn_vm *vm, uint16_t e820_entry_type, u
}
}

+int16_t get_sworld_page_idx(uint16_t vm_id) {
This is to get relative index of VM with secure world, right?
If yes, this name is bad.
Yes, this function will return the index of the sworld memory array:

"static struct page
post_user_vm_sworld_memory[MAX_TRUSTY_VM_NUM][TRUSTY_RAM_SIZE
>> PAGE_SHIFT] __aligned(MEM_2M);"

Is better to rename it to "get_sworld_memory_index" ?
Get_svm_index? Or get_sword_vm_index?
OK, I will rename it to "get_sword_vm_index".
+ int16_t i;
+ int16_t page_idx = 0;
+ struct acrn_vm_config *vm_config;
+
+ for (i = 0; i < vm_id; i++) {
+ vm_config = get_vm_config(vm_id);
+ if ((vm_config->guest_flags &
GUEST_FLAG_SECURE_WORLD_ENABLED) != 0U) {
+ page_idx += 1;
+ }
+ }
+
Need to make sure it is smaller than MAX_TRUSTY_VM_NUM.
OK, I will add some check logic.

BTW, the "MAX_TRUSTY_VM_NUM" is not a user config parameter, the
offline tool defined it by count the trusty VM number of the scenario.
That is fine.


+ return page_idx;
+}
+
/**
* @pre vm_id < CONFIG_MAX_VM_NUM && vm_config != NULL &&
rtn_vm !=
NULL
* @pre vm->state == VM_POWERED_OFF @@ -667,8 +683,7 @@
int32_t
create_vm(uint16_t vm_id, uint64_t pcpu_bitmap, struct acrn_vm_config
*v
vm->sworld_control.flag.supported = 1U;
}
if (vm->sworld_control.flag.supported != 0UL) {
- uint16_t service_vm_id = (get_service_vm())->vm_id;
- uint16_t page_idx = vmid_2_rel_vmid(service_vm_id,
vm_id) - 1U;
+ uint16_t page_idx = get_sworld_page_idx(vm_id);

ept_add_mr(vm, (uint64_t *)vm-
arch_vm.nworld_eptp,
hva2hpa(post_user_vm_sworld_memory[page_idx]),
--
2.25.1