Re: [PATCH v3 08/14] KVM: SVM: Update AVIC settings when changing APIC mode

From: Suravee Suthikulpanit
Date: Wed May 04 2022 - 21:38:27 EST


Maxim,

On 5/4/22 7:19 PM, Maxim Levitsky wrote:
On Wed, 2022-05-04 at 02:31 -0500, Suravee Suthikulpanit wrote:
Update and refresh AVIC settings when guest APIC mode is updated
(e.g. changing between disabled, xAPIC, or x2APIC).

Signed-off-by: Suravee Suthikulpanit<suravee.suthikulpanit@xxxxxxx>
---
arch/x86/kvm/svm/avic.c | 16 ++++++++++++++++
arch/x86/kvm/svm/svm.c | 1 +
2 files changed, 17 insertions(+)

diff --git a/arch/x86/kvm/svm/avic.c b/arch/x86/kvm/svm/avic.c
index 3ebeea19b487..d185dd8ddf17 100644
--- a/arch/x86/kvm/svm/avic.c
+++ b/arch/x86/kvm/svm/avic.c
@@ -691,6 +691,22 @@ void avic_apicv_post_state_restore(struct kvm_vcpu *vcpu)
avic_handle_ldr_update(vcpu);
}
+void avic_set_virtual_apic_mode(struct kvm_vcpu *vcpu)
+{
+ struct vcpu_svm *svm = to_svm(vcpu);
+
+ if (!lapic_in_kernel(vcpu) || (avic_mode == AVIC_MODE_NONE))
+ return;
+
+ if (kvm_get_apic_mode(vcpu) == LAPIC_MODE_INVALID) {
+ WARN_ONCE(true, "Invalid local APIC state (vcpu_id=%d)", vcpu->vcpu_id);
+ return;
+ }
+
+ kvm_vcpu_update_apicv(&svm->vcpu);
Why to have this call? I think that all that is needed is only to call the
avic_refresh_apicv_exec_ctrl.

When APIC mode is updated on each vCPU, we need to check and update
vcpu->arch.apicv_active accordingly, which happens in the kvm_vcpu_update_apicv()

One test case that would fail w/o the kvm_vcpu_update_apicv() is when
we boot a Linux guest w/ guest kernel option _nox2apic_, which Linux forces APIC
mode of vCPUs with APIC ID 255 and higher to disable. W/o this line of code, the VM
would not boot w/ more than 255 vCPUs.

Regards,
Suravee