Re: [PATCH 6/6] svm: Temporary deactivate AVIC during ExtINT handling

From: Suthikulpanit, Suravee
Date: Mon Jun 03 2019 - 15:02:30 EST


Hi Jan,

On 5/8/19 12:37 PM, Jan H. SchÃnherr wrote:
> [CAUTION: External Email]
>
> Hi Suravee.
>
> I wonder, how this interacts with Hyper-V SynIC; see comments below.
>
> On 22/03/2019 12.57, Suthikulpanit, Suravee wrote:
>> AMD AVIC does not support ExtINT. Therefore, AVIC must be temporary
>> deactivated and fall back to using legacy interrupt injection via
>> vINTR and interrupt window.
>>
>> Introduce svm_request_activate/deactivate_avic() helper functions,
>> which handle steps required to activate/deactivate AVIC.
>>
>> Signed-off-by: Suravee Suthikulpanit <suravee.suthikulpanit@xxxxxxx>
>> ---
>> arch/x86/kvm/svm.c | 57 +++++++++++++++++++++++++++++++++++++++++++---
>> 1 file changed, 54 insertions(+), 3 deletions(-)
>>
>> diff --git a/arch/x86/kvm/svm.c b/arch/x86/kvm/svm.c
>> index f41f34f70dde..84116e689d5f 100644
>> --- a/arch/x86/kvm/svm.c
>> +++ b/arch/x86/kvm/svm.c
>> @@ -391,6 +391,8 @@ static u8 rsm_ins_bytes[] = "\x0f\xaa";
>> static void svm_set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0);
>> static void svm_flush_tlb(struct kvm_vcpu *vcpu, bool invalidate_gpa);
>> static void svm_complete_interrupts(struct vcpu_svm *svm);
>> +static void svm_request_activate_avic(struct kvm_vcpu *vcpu);
>> +static bool svm_get_enable_apicv(struct kvm_vcpu *vcpu);
>>
>> static int nested_svm_exit_handled(struct vcpu_svm *svm);
>> static int nested_svm_intercept(struct vcpu_svm *svm);
>> @@ -2109,6 +2111,9 @@ static void avic_set_running(struct kvm_vcpu *vcpu, bool is_run)
>> {
>> struct vcpu_svm *svm = to_svm(vcpu);
>>
>> + if (!kvm_vcpu_apicv_active(vcpu))
>> + return;
>> +
>> svm->avic_is_running = is_run;
>> if (is_run)
>> avic_vcpu_load(vcpu, vcpu->cpu);
>> @@ -2356,6 +2361,10 @@ static void svm_vcpu_blocking(struct kvm_vcpu *vcpu)
>>
>> static void svm_vcpu_unblocking(struct kvm_vcpu *vcpu)
>> {
>> + if (kvm_check_request(KVM_REQ_APICV_ACTIVATE, vcpu))
>> + kvm_vcpu_activate_apicv(vcpu);
>> + if (kvm_check_request(KVM_REQ_APICV_DEACTIVATE, vcpu))
>> + kvm_vcpu_deactivate_apicv(vcpu);
>> avic_set_running(vcpu, true);
>> }
>>
>> @@ -4505,6 +4514,15 @@ static int interrupt_window_interception(struct vcpu_svm *svm)
>> {
>> kvm_make_request(KVM_REQ_EVENT, &svm->vcpu);
>> svm_clear_vintr(svm);
>> +
>> + /*
>> + * For AVIC, the only reason to end up here is ExtINTs.
>> + * In this case AVIC was temporarily disabled for
>> + * requesting the IRQ window and we have to re-enable it.
>> + */
>> + if (svm_get_enable_apicv(&svm->vcpu))
>> + svm_request_activate_avic(&svm->vcpu);
>> +
>
> Are we sure, we're not accidentally re-enabling AVIC, if it was disabled via
> kvm_hv_activate_synic()?

Actually, I missed this case. Now I have a solution that I'll be send out for review in V2.

>> svm->vmcb->control.int_ctl &= ~V_IRQ_MASK;
>> mark_dirty(svm->vmcb, VMCB_INTR);
>> ++svm->vcpu.stat.irq_window_exits;
>> @@ -5206,6 +5224,34 @@ static void svm_hwapic_isr_update(struct kvm_vcpu *vcpu, int max_isr)
>> {
>> }
>>
>> +static bool is_avic_active(struct vcpu_svm *svm)
>> +{
>> + return (svm_get_enable_apicv(&svm->vcpu) &&
>> + svm->vmcb->control.int_ctl & AVIC_ENABLE_MASK);
>> +}
>> +
>> +static void svm_request_activate_avic(struct kvm_vcpu *vcpu)
>> +{
>> + struct vcpu_svm *svm = to_svm(vcpu);
>> +
>> + if (!lapic_in_kernel(vcpu) || is_avic_active(svm))
>> + return;
>> +
>> + avic_setup_access_page(vcpu, false);
>> + kvm_make_apicv_activate_request(vcpu->kvm);
>> +}
>> +
>> +static void svm_request_deactivate_avic(struct kvm_vcpu *vcpu)
>> +{
>> + struct vcpu_svm *svm = to_svm(vcpu);
>> +
>> + if (!lapic_in_kernel(vcpu) || !is_avic_active(svm))
>> + return;
>> +
>> + avic_destroy_access_page(vcpu);
>
> Something like avic_destroy_access_page() is not called, when AVIC is
> disabled via kvm_hv_activate_synic().
>
> Is that an oversight in the other code path, is it not needed here,
> or am I missing something?

This is an oversight. I also have a fix for this in V2.

Thanks,
Suravee