Re: [PATCH v3 13/14] KVM: SVM: Use target APIC ID to complete x2AVIC IRQs when possible

From: Maxim Levitsky
Date: Wed May 04 2022 - 08:34:56 EST


On Wed, 2022-05-04 at 02:31 -0500, Suravee Suthikulpanit wrote:
> For x2AVIC, the index from incomplete IPI #vmexit info is invalid
> for logical cluster mode. Only ICRH/ICRL values can be used
> to determine the IPI destination APIC ID.
>
> Since QEMU defines guest physical APIC ID to be the same as
> vCPU ID, it can be used to quickly identify the target vCPU to deliver IPI,
> and avoid the overhead from searching through all vCPUs to match the target
> vCPU.
>
> Signed-off-by: Suravee Suthikulpanit <suravee.suthikulpanit@xxxxxxx>
> ---
> arch/x86/kvm/svm/avic.c | 21 ++++++++++++++++++++-
> 1 file changed, 20 insertions(+), 1 deletion(-)
>
> diff --git a/arch/x86/kvm/svm/avic.c b/arch/x86/kvm/svm/avic.c
> index 3b6a96043633..a526fbc60bbd 100644
> --- a/arch/x86/kvm/svm/avic.c
> +++ b/arch/x86/kvm/svm/avic.c
> @@ -377,7 +377,26 @@ static int avic_kick_target_vcpus_fast(struct kvm *kvm, struct kvm_lapic *source
> /* For xAPIC logical mode, the index is for logical APIC table. */
> apic_id = avic_logical_id_table[index] & 0x1ff;
> } else {
> - return -EINVAL;
> + /* For x2APIC logical mode, cannot leverage the index.
> + * Instead, calculate physical ID from logical ID in ICRH.
> + */
> + int apic;
> + int first = ffs(icrh & 0xffff);
> + int last = fls(icrh & 0xffff);
> + int cluster = (icrh & 0xffff0000) >> 16;
> +
> + /*
> + * If the x2APIC logical ID sub-field (i.e. icrh[15:0]) contains zero
> + * or more than 1 bits, we cannot match just one vcpu to kick for
> + * fast path.
> + */
> + if (!first || (first != last))
> + return -EINVAL;
> +
> + apic = first - 1;
> + if ((apic < 0) || (apic > 15) || (cluster >= 0xfffff))
> + return -EINVAL;
> + apic_id = (cluster << 4) + apic;
> }
> }
>

Reviewed-by: Maxim Levitsky <mlevitsk@xxxxxxxxxx>

Best regards,
Maxim Levitsky