Skip to content

Commit b8b354d

Browse files
committed
KVM: x86: Allow APICv APIC ID inhibit to be cleared
jira LE-1907 Rebuild_History Non-Buildable kernel-5.14.0-284.30.1.el9_2 commit-author Greg Edwards <gedwards@ddn.com> commit d471bd8 Legacy kernels prior to commit 4399c03 ("x86/apic: Remove verify_local_APIC()") write the APIC ID of the boot CPU twice to verify a functioning local APIC. This results in APIC acceleration inhibited on these kernels for reason APICV_INHIBIT_REASON_APIC_ID_MODIFIED. Allow the APICV_INHIBIT_REASON_APIC_ID_MODIFIED inhibit reason to be cleared if/when all APICs in xAPIC mode set their APIC ID back to the expected vcpu_id value. Fold the functionality previously in kvm_lapic_xapic_id_updated() into kvm_recalculate_apic_map(), as this allows examining all APICs in one pass. Fixes: 3743c2f ("KVM: x86: inhibit APICv/AVIC on changes to APIC ID or APIC base") Signed-off-by: Greg Edwards <gedwards@ddn.com> Link: https://lore.kernel.org/r/20221117183247.94314-1-gedwards@ddn.com Signed-off-by: Sean Christopherson <seanjc@google.com> Message-Id: <20230106011306.85230-33-seanjc@google.com> Signed-off-by: Paolo Bonzini <pbonzini@redhat.com> (cherry picked from commit d471bd8) Signed-off-by: Jonathan Maple <jmaple@ciq.com>
1 parent e3e64fd commit b8b354d

File tree

1 file changed

+15
-26
lines changed

1 file changed

+15
-26
lines changed

arch/x86/kvm/lapic.c

Lines changed: 15 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,7 @@ void kvm_recalculate_apic_map(struct kvm *kvm)
237237
struct kvm_vcpu *vcpu;
238238
unsigned long i;
239239
u32 max_id = 255; /* enough space for any xAPIC ID */
240+
bool xapic_id_mismatch = false;
240241

241242
/* Read kvm->arch.apic_map_dirty before kvm->arch.apic_map. */
242243
if (atomic_read_acquire(&kvm->arch.apic_map_dirty) == CLEAN)
@@ -286,6 +287,15 @@ void kvm_recalculate_apic_map(struct kvm *kvm)
286287
xapic_id = kvm_xapic_id(apic);
287288
x2apic_id = kvm_x2apic_id(apic);
288289

290+
/*
291+
* Deliberately truncate the vCPU ID when detecting a mismatched
292+
* APIC ID to avoid false positives if the vCPU ID, i.e. x2APIC
293+
* ID, is a 32-bit value. Any unwanted aliasing due to
294+
* truncation results will be detected below.
295+
*/
296+
if (!apic_x2apic_mode(apic) && xapic_id != (u8)vcpu->vcpu_id)
297+
xapic_id_mismatch = true;
298+
289299
/*
290300
* Apply KVM's hotplug hack if userspace has enable 32-bit APIC
291301
* IDs. Allow sending events to vCPUs by their x2APIC ID even
@@ -402,6 +412,11 @@ void kvm_recalculate_apic_map(struct kvm *kvm)
402412
else
403413
kvm_clear_apicv_inhibit(kvm, APICV_INHIBIT_REASON_LOGICAL_ID_ALIASED);
404414

415+
if (xapic_id_mismatch)
416+
kvm_set_apicv_inhibit(kvm, APICV_INHIBIT_REASON_APIC_ID_MODIFIED);
417+
else
418+
kvm_clear_apicv_inhibit(kvm, APICV_INHIBIT_REASON_APIC_ID_MODIFIED);
419+
405420
old = rcu_dereference_protected(kvm->arch.apic_map,
406421
lockdep_is_held(&kvm->arch.apic_map_lock));
407422
rcu_assign_pointer(kvm->arch.apic_map, new);
@@ -2162,28 +2177,6 @@ static void apic_manage_nmi_watchdog(struct kvm_lapic *apic, u32 lvt0_val)
21622177
}
21632178
}
21642179

2165-
static void kvm_lapic_xapic_id_updated(struct kvm_lapic *apic)
2166-
{
2167-
struct kvm *kvm = apic->vcpu->kvm;
2168-
2169-
if (!kvm_apic_hw_enabled(apic))
2170-
return;
2171-
2172-
if (KVM_BUG_ON(apic_x2apic_mode(apic), kvm))
2173-
return;
2174-
2175-
/*
2176-
* Deliberately truncate the vCPU ID when detecting a modified APIC ID
2177-
* to avoid false positives if the vCPU ID, i.e. x2APIC ID, is a 32-bit
2178-
* value. If the wrap/truncation results in unwanted aliasing, APICv
2179-
* will be inhibited as part of updating KVM's optimized APIC maps.
2180-
*/
2181-
if (kvm_xapic_id(apic) == (u8)apic->vcpu->vcpu_id)
2182-
return;
2183-
2184-
kvm_set_apicv_inhibit(apic->vcpu->kvm, APICV_INHIBIT_REASON_APIC_ID_MODIFIED);
2185-
}
2186-
21872180
static int get_lvt_index(u32 reg)
21882181
{
21892182
if (reg == APIC_LVTCMCI)
@@ -2204,7 +2197,6 @@ static int kvm_lapic_reg_write(struct kvm_lapic *apic, u32 reg, u32 val)
22042197
case APIC_ID: /* Local APIC ID */
22052198
if (!apic_x2apic_mode(apic)) {
22062199
kvm_apic_set_xapic_id(apic, val >> 24);
2207-
kvm_lapic_xapic_id_updated(apic);
22082200
} else {
22092201
ret = 1;
22102202
}
@@ -2926,9 +2918,6 @@ int kvm_apic_set_state(struct kvm_vcpu *vcpu, struct kvm_lapic_state *s)
29262918
}
29272919
memcpy(vcpu->arch.apic->regs, s->regs, sizeof(*s));
29282920

2929-
if (!apic_x2apic_mode(apic))
2930-
kvm_lapic_xapic_id_updated(apic);
2931-
29322921
atomic_set_release(&apic->vcpu->kvm->arch.apic_map_dirty, DIRTY);
29332922
kvm_recalculate_apic_map(vcpu->kvm);
29342923
kvm_apic_set_version(vcpu);

0 commit comments

Comments
 (0)