Re: [PATCH 16/19] KVM: x86: Explicitly track all possibilities for APIC map's logical modes

From: Maxim Levitsky
Date: Wed Aug 31 2022 - 13:54:21 EST


On Wed, 2022-08-31 at 16:56 +0000, Sean Christopherson wrote:
> On Wed, Aug 31, 2022, Maxim Levitsky wrote:
> > > diff --git a/arch/x86/kvm/lapic.c b/arch/x86/kvm/lapic.c
> > > index 8209caffe3ab..3b6ef36b3963 100644
> > > --- a/arch/x86/kvm/lapic.c
> > > +++ b/arch/x86/kvm/lapic.c
> > > @@ -168,7 +168,12 @@ static bool kvm_use_posted_timer_interrupt(struct kvm_vcpu *vcpu)
> > >
> > > static inline bool kvm_apic_map_get_logical_dest(struct kvm_apic_map *map,
> > > u32 dest_id, struct kvm_lapic ***cluster, u16 *mask) {
> > > - switch (map->mode) {
> > > + switch (map->logical_mode) {
> > > + case KVM_APIC_MODE_SW_DISABLED:
> > > + /* Arbitrarily use the flat map so that @cluster isn't NULL. */
> > > + *cluster = map->xapic_flat_map;
> > > + *mask = 0;
> > > + return true;
> > Could you explain why this is needed? I probably missed something.
>
> If all vCPUs leave their APIC software disabled, or leave LDR=0, then the overall
> mode will be KVM_APIC_MODE_SW_DISABLED. In this case, the effective "mask" is '0'
> because there are no targets. And this returns %true because there are no targets,
> i.e. there's no need to go down the slow path after kvm_apic_map_get_dest_lapic().

I guess this case doesn't need optimization (although maybe some OSes do leave all LDRs to 0,
if they don't use logical addressing, don't know)

Anyway thanks, that makes sense.

>
> > > @@ -993,7 +1011,7 @@ static bool kvm_apic_is_broadcast_dest(struct kvm *kvm, struct kvm_lapic **src,
> > > {
> > > if (kvm->arch.x2apic_broadcast_quirk_disabled) {
> > > if ((irq->dest_id == APIC_BROADCAST &&
> > > - map->mode != KVM_APIC_MODE_X2APIC))
> > > + map->logical_mode != KVM_APIC_MODE_X2APIC))
> > > return true;
> > > if (irq->dest_id == X2APIC_BROADCAST)
> > > return true;
> >
> > To be honest I would put that patch first, and then do all the other patches,
> > this way you would avoid all of the hacks they do and removed here.
>
> I did it this way so that I could test this patch for correctness. Without the
> bug fixes in place it's not really possible to verify this patch is 100% correct.
>
> I completely agree that it would be a lot easier to read/understand/review if
> this came first, but I'd rather not sacrifice the ability to easily test this patch.
>

I am not 100% sure about this, but I won't argue about it, let it be.

Best regards,
Maxim Levitsky