Re: [PATCH 14/19] KVM: x86: Honor architectural behavior for aliased 8-bit APIC IDs

From: Sean Christopherson
Date: Wed Aug 31 2022 - 12:41:55 EST


On Wed, Aug 31, 2022, Maxim Levitsky wrote:
> On Wed, 2022-08-31 at 00:35 +0000, Sean Christopherson wrote:
> > - if (!apic_x2apic_mode(apic) && !new->phys_map[xapic_id])
> > - new->phys_map[xapic_id] = apic;
> > + if (kvm->arch.x2apic_format) {
> > + /* See also kvm_apic_match_physical_addr(). */
> > + if ((apic_x2apic_mode(apic) || x2apic_id > 0xff) &&
> > + x2apic_id <= new->max_apic_id)
> > + new->phys_map[x2apic_id] = apic;
> > +
> > + if (!apic_x2apic_mode(apic) && !new->phys_map[xapic_id])
> > + new->phys_map[xapic_id] = apic;
> > + } else {
> > + /*
> > + * Disable the optimized map if the physical APIC ID is
> > + * already mapped, i.e. is aliased to multiple vCPUs.
> > + * The optimized map requires a strict 1:1 mapping
> > + * between IDs and vCPUs.
> > + */
> > + if (apic_x2apic_mode(apic))
> > + physical_id = x2apic_id;
> > + else
> > + physical_id = xapic_id;
> > +
> > + if (new->phys_map[physical_id]) {
> > + kvfree(new);
> > + new = NULL;
> > + goto out;
> Why not to use the same KVM_APIC_MODE_XAPIC_FLAT | KVM_APIC_MODE_XAPIC_CLUSTER
> hack here?

The map's "mode" only covers logical mode (the cleanup patch renames "mode" to
"logical_mode" to make this more clear). There is no equivalent for dealing with
the physical IDs. Alternatively, a flag to say "physical map is disabled" could
be added, but KVM already has to cleanly handle a NULL map and in all likelihood
the logical map is also going to be disabled anyways.

Not to mention that APIC performance is unlikely to be a priority for any guest
that triggers this code :-)