Skip to content

Commit 0805eb9

Browse files
ZideChen0wenlingz
authored andcommitted
hv: dynamically configure CPU affinity through hypercall
- add a new member cpu_affinity to struct acrn_create_vm, so that acrn-dm is able to assign CPU affinity through HC_CREATE_VM hypercall. - if vm_create.cpu_affinity is zero, hypervisor launches the VM with the statically configured CPU affinity. Tracked-On: #4616 Signed-off-by: Zide Chen <zide.chen@intel.com> Acked-by: Eddie Dong <eddie.dong@intel.com>
1 parent 4675394 commit 0805eb9

File tree

2 files changed

+34
-13
lines changed

2 files changed

+34
-13
lines changed

hypervisor/common/hypercall.c

Lines changed: 26 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -166,21 +166,35 @@ int32_t hcall_create_vm(struct acrn_vm *vm, uint64_t param)
166166
/* Filter out the bits should not set by DM and then assign it to guest_flags */
167167
vm_config->guest_flags |= (cv.vm_flag & DM_OWNED_GUEST_FLAG_MASK);
168168

169-
/* GUEST_FLAG_RT must be set if we have GUEST_FLAG_LAPIC_PASSTHROUGH set in guest_flags */
170-
if (((vm_config->guest_flags & GUEST_FLAG_LAPIC_PASSTHROUGH) != 0U)
171-
&& ((vm_config->guest_flags & GUEST_FLAG_RT) == 0U)) {
172-
pr_err("Wrong guest flags 0x%lx\n", vm_config->guest_flags);
169+
/* post-launched VM is allowed to choose pCPUs from vm_config->cpu_affinity_bitmap only */
170+
if ((cv.cpu_affinity & ~(vm_config->cpu_affinity_bitmap)) != 0UL) {
171+
pr_err("%s: Post-launch VM can't share PCPU with Pre-launch VM!", __func__);
173172
} else {
174-
if (create_vm(vm_id, vm_config, &target_vm) != 0) {
175-
dev_dbg(DBG_LEVEL_HYCALL, "HCALL: Create VM failed");
176-
cv.vmid = ACRN_INVALID_VMID;
177-
} else {
178-
/* return a relative vm_id from SOS view */
179-
cv.vmid = vmid_2_rel_vmid(vm->vm_id, vm_id);
180-
cv.vcpu_num = vm_config->vcpu_num;
173+
/* DM could overwrite the statically configured PCPU bitmap */
174+
if (bitmap_weight(cv.cpu_affinity) != 0U) {
175+
vm_config->vcpu_num = bitmap_weight(cv.cpu_affinity);
176+
vm_config->cpu_affinity_bitmap = cv.cpu_affinity;
181177
}
182178

183-
ret = copy_to_gpa(vm, &cv, param, sizeof(cv));
179+
/*
180+
* GUEST_FLAG_RT must be set if we have GUEST_FLAG_LAPIC_PASSTHROUGH
181+
* set in guest_flags
182+
*/
183+
if (((vm_config->guest_flags & GUEST_FLAG_LAPIC_PASSTHROUGH) != 0UL)
184+
&& ((vm_config->guest_flags & GUEST_FLAG_RT) == 0UL)) {
185+
pr_err("Wrong guest flags 0x%lx\n", vm_config->guest_flags);
186+
} else {
187+
if (create_vm(vm_id, vm_config, &target_vm) != 0) {
188+
dev_dbg(DBG_LEVEL_HYCALL, "HCALL: Create VM failed");
189+
cv.vmid = ACRN_INVALID_VMID;
190+
} else {
191+
/* return a relative vm_id from SOS view */
192+
cv.vmid = vmid_2_rel_vmid(vm->vm_id, vm_id);
193+
cv.vcpu_num = vm_config->vcpu_num;
194+
}
195+
196+
ret = copy_to_gpa(vm, &cv, param, sizeof(cv));
197+
}
184198
}
185199
}
186200
}

hypervisor/include/public/acrn_common.h

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -360,8 +360,15 @@ struct acrn_create_vm {
360360

361361
uint64_t req_buf;
362362

363+
/**
364+
* The least significant set bit is the PCPU # the VCPU 0 maps to;
365+
* second set least significant bit is the PCPU # the VCPU 1 maps to;
366+
* and so on...
367+
*/
368+
uint64_t cpu_affinity;
369+
363370
/** Reserved for future use*/
364-
uint8_t reserved2[16];
371+
uint8_t reserved2[8];
365372
} __aligned(8);
366373

367374
/**

0 commit comments

Comments
 (0)