Skip to contentSkip to conversation

KVM: SVM: Fix missing LAPIC TPR sync into VMCB::V_TPR with AVIC on

PATCH Superseded Maciej S. Szmigiero 4 participants 12 messages Aug 19, 2025 – Aug 22, 2025

Subsystems (4)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

cover KVM: SVM: Fix missing LAPIC TPR sync into VMCB::V_TPR with AVIC on

From: "Maciej S. Szmigiero" <[email protected]>

When AVIC is enabled the normal pre-VMRUN LAPIC TPR to VMCB::V_TPR sync in sync_lapic_to_cr8() is inhibited so any changed TPR in the LAPIC state would *not* get copied into the V_TPR field of VMCB.

AVIC does sync between these two fields, however it does so only on explicit guest writes to one of these fields, not on a bare VMRUN.

This is especially true when it is the userspace setting LAPIC state via KVM_SET_LAPIC ioctl() since userspace does not have access to the guest VMCB.

Practice shows that it is the V_TPR that is actually used by the AVIC to decide whether to issue pending interrupts to the CPU (not TPR in TASKPRI), so any leftover value in V_TPR will cause serious interrupt delivery issues in the guest when AVIC is enabled.

Fix this issue by explicitly copying LAPIC TPR to VMCB::V_TPR in avic_apicv_post_state_restore(), which gets called from KVM_SET_LAPIC and similar code paths when AVIC is enabled.

Add also a relevant set of tests to xapic_state_test so hopefully we'll be protected against getting such regressions in the future.

Yes, this breaks real guests when AVIC is enabled. Specifically, the one OS that sometimes needs different handling and its name begins with letter 'W'.

KVM: SVM: Sync TPR from LAPIC into VMCB::V_TPR when setting LAPIC regs
KVM: selftests: Test TPR / CR8 sync and interrupt masking

 arch/x86/kvm/svm/avic.c                       |  23 ++
 .../testing/selftests/kvm/include/x86/apic.h  |   5 +
 .../selftests/kvm/x86/xapic_state_test.c      | 265 +++++++++++++++++-
 3 files changed, 290 insertions(+), 3 deletions(-)
#1 lore raw

PATCH 1/2 KVM: SVM: Sync TPR from LAPIC into VMCB::V_TPR when setting LAPIC regs

From: "Maciej S. Szmigiero" <[email protected]>

When AVIC is enabled the normal pre-VMRUN sync in sync_lapic_to_cr8() is inhibited so any changed TPR in the LAPIC state would not get copied into the V_TPR field of VMCB.

AVIC does sync between these two fields, however it does so only on explicit guest writes to one of these fields, not on a bare VMRUN.

This is especially true when it is the userspace setting LAPIC state via KVM_SET_LAPIC ioctl() since userspace does not have access to the guest VMCB.

Practice shows that it is the V_TPR that is actually used by the AVIC to decide whether to issue pending interrupts to the CPU (not TPR in TASKPRI), so any leftover value in V_TPR will cause serious interrupt delivery issues in the guest when AVIC is enabled.

Fix this issue by explicitly copying LAPIC TPR to VMCB::V_TPR in avic_apicv_post_state_restore(), which gets called from KVM_SET_LAPIC and similar code paths when AVIC is enabled.

Fixes 3bbf3565f48c ("svm: Do not intercept CR8 when enable AVIC")
---
 arch/x86/kvm/svm/avic.c | 23 +++++++++++++++++++++++
 1 file changed, 23 insertions(+)
arch/x86/kvm/svm/avic.c +23 -0
index a34c5c3b164e..877bc3db2c6e 100644
@@ -725,8 +725,31 @@ int avic_init_vcpu(struct vcpu_svm *svm)
 
 void avic_apicv_post_state_restore(struct kvm_vcpu *vcpu)
 {
+	struct vcpu_svm *svm = to_svm(vcpu);
+	u64 cr8;
+
 	avic_handle_dfr_update(vcpu);
 	avic_handle_ldr_update(vcpu);
+
+	/* Running nested should have inhibited AVIC. */
+	if (WARN_ON_ONCE(nested_svm_virtualize_tpr(vcpu)))
+		return;
+
+	/*
+	 * Sync TPR from LAPIC TASKPRI into V_TPR field of the VMCB.
+	 *
+	 * When AVIC is enabled the normal pre-VMRUN sync in sync_lapic_to_cr8()
+	 * is inhibited so any set TPR LAPIC state would not get reflected
+	 * in V_TPR.
+	 *
+	 * Practice shows that it is the V_TPR that is actually used by the
+	 * AVIC to decide whether to issue pending interrupts to the CPU, not
+	 * TPR in TASKPRI.
+	 */
+	cr8 = kvm_get_cr8(vcpu);
+	svm->vmcb->control.int_ctl &= ~V_TPR_MASK;
+	svm->vmcb->control.int_ctl |= cr8 & V_TPR_MASK;
+	WARN_ON_ONCE(!vmcb_is_dirty(svm->vmcb, VMCB_INTR));
 }
 
 static void svm_ir_list_del(struct kvm_kernel_irqfd *irqfd)
#2 lore raw

Re: [PATCH 1/2] KVM: SVM: Sync TPR from LAPIC into VMCB::V_TPR when setting LAPIC regs

Quoted 44 lines · Maciej S. Szmigiero

From: "Maciej S. Szmigiero" <[email protected]>

When AVIC is enabled the normal pre-VMRUN sync in sync_lapic_to_cr8() is inhibited so any changed TPR in the LAPIC state would not get copied into the V_TPR field of VMCB.

AVIC does sync between these two fields, however it does so only on explicit guest writes to one of these fields, not on a bare VMRUN.

This is especially true when it is the userspace setting LAPIC state via KVM_SET_LAPIC ioctl() since userspace does not have access to the guest VMCB.

Practice shows that it is the V_TPR that is actually used by the AVIC to decide whether to issue pending interrupts to the CPU (not TPR in TASKPRI), so any leftover value in V_TPR will cause serious interrupt delivery issues in the guest when AVIC is enabled.

Fix this issue by explicitly copying LAPIC TPR to VMCB::V_TPR in avic_apicv_post_state_restore(), which gets called from KVM_SET_LAPIC and similar code paths when AVIC is enabled.

Fixes 3bbf3565f48c ("svm: Do not intercept CR8 when enable AVIC")
---
 arch/x86/kvm/svm/avic.c | 23 +++++++++++++++++++++++
 1 file changed, 23 insertions(+)
arch/x86/kvm/svm/avic.c +6 -0
index a34c5c3b164e..877bc3db2c6e 100644
@@ -725,8 +725,31 @@ int avic_init_vcpu(struct vcpu_svm *svm)
 
 void avic_apicv_post_state_restore(struct kvm_vcpu *vcpu)
 {
+	struct vcpu_svm *svm = to_svm(vcpu);
+	u64 cr8;
+
 	avic_handle_dfr_update(vcpu);
 	avic_handle_ldr_update(vcpu);
+
+	/* Running nested should have inhibited AVIC. */
+	if (WARN_ON_ONCE(nested_svm_virtualize_tpr(vcpu)))

arch/x86/kvm/svm/avic.c int avic_init_vcpu(struct vcpu_svm *svm) · line 745

Quoted 10 lines
arch/x86/kvm/svm/avic.c +8 -0
+		return;
 
 
+
+	/*
+	 * Sync TPR from LAPIC TASKPRI into V_TPR field of the VMCB.
+	 *
+	 * When AVIC is enabled the normal pre-VMRUN sync in sync_lapic_to_cr8()
+	 * is inhibited so any set TPR LAPIC state would not get reflected
+	 * in V_TPR.

Hmm, I think that code is straight up wrong. There's no justification, just a claim:

commit 3bbf3565f48ce3999b5a12cde946f81bd4475312
Author: Suravee Suthikulpanit <[email protected]>
AuthorDate: Wed May 4 14:09:51 2016 -0500
Commit: Paolo Bonzini <[email protected]>
CommitDate: Wed May 18 18:04:31 2016 +0200

svm: Do not intercept CR8 when enable AVIC

When enable AVIC:
* Do not intercept CR8 since this should be handled by AVIC HW.
* Also, we don't need to sync cr8/V_TPR and APIC backing page. <======

[Rename svm_in_nested_interrupt_shadow to svm_nested_virtualize_tpr. - Paolo]

That claim assumes APIC[TPR] will _never_ be modified by anything other than hardware. That's obviously false for state restore from userspace, and it's also technically false at steady state, e.g. if KVM managed to trigger emulation of a store to the APIC page, then KVM would bypass the automatic harware sync.

There's also the comically ancient KVM_SET_VAPIC_ADDR, which AFAICT appears to be largely dead code with respect to vTPR (nothing sets KVM_APIC_CHECK_VAPIC except for the initial ioctl), but could again set APIC[TPR] without updating V_TPR.

So, rather than manually do the update during state restore, my vote is to restore the sync logic. And if we want to optimize that code (seems unnecessary), then we should hook all TPR writes.

arch/x86/kvm/svm/svm.c +1 -2
index d9931c6c4bc6..1bfebe40854f 100644
@@ -4046,8 +4046,7 @@ static inline void sync_lapic_to_cr8(struct kvm_vcpu *vcpu)
        struct vcpu_svm *svm = to_svm(vcpu);
        u64 cr8;
 
-       if (nested_svm_virtualize_tpr(vcpu) ||
-           kvm_vcpu_apicv_active(vcpu))
+       if (nested_svm_virtualize_tpr(vcpu))
                return;
 
        cr8 = kvm_get_cr8(vcpu);

arch/x86/kvm/svm/avic.c int avic_init_vcpu(struct vcpu_svm *svm) · line 749

Quoted 4 lines
arch/x86/kvm/svm/avic.c +4 -0
+	 *
+	 * Practice shows that it is the V_TPR that is actually used by the
+	 * AVIC to decide whether to issue pending interrupts to the CPU, not
+	 * TPR in TASKPRI.

FWIW, the APM kinda sorta alludes to this:

Enabling AVIC also affects CR8 behavior independent of V_INTR_MASKING enable (bit 24): writes to CR8 affect the V_TPR and update the backing page and reads from CR8 return V_TPR.

Quoted 10 lines
arch/x86/kvm/svm/avic.c +5 -0
+	 */
+	cr8 = kvm_get_cr8(vcpu);
+	svm->vmcb->control.int_ctl &= ~V_TPR_MASK;
+	svm->vmcb->control.int_ctl |= cr8 & V_TPR_MASK;
+	WARN_ON_ONCE(!vmcb_is_dirty(svm->vmcb, VMCB_INTR));
 

}

static void svm_ir_list_del(struct kvm_kernel_irqfd *irqfd)

#3 lore raw

Re: [PATCH 1/2] KVM: SVM: Sync TPR from LAPIC into VMCB::V_TPR when setting LAPIC regs

Quoted 71 lines · Sean Christopherson
Quoted 44 lines · Maciej S. Szmigiero

From: "Maciej S. Szmigiero" <[email protected]>

When AVIC is enabled the normal pre-VMRUN sync in sync_lapic_to_cr8() is inhibited so any changed TPR in the LAPIC state would not get copied into the V_TPR field of VMCB.

AVIC does sync between these two fields, however it does so only on explicit guest writes to one of these fields, not on a bare VMRUN.

This is especially true when it is the userspace setting LAPIC state via KVM_SET_LAPIC ioctl() since userspace does not have access to the guest VMCB.

Practice shows that it is the V_TPR that is actually used by the AVIC to decide whether to issue pending interrupts to the CPU (not TPR in TASKPRI), so any leftover value in V_TPR will cause serious interrupt delivery issues in the guest when AVIC is enabled.

Fix this issue by explicitly copying LAPIC TPR to VMCB::V_TPR in avic_apicv_post_state_restore(), which gets called from KVM_SET_LAPIC and similar code paths when AVIC is enabled.

Fixes 3bbf3565f48c ("svm: Do not intercept CR8 when enable AVIC")
---
 arch/x86/kvm/svm/avic.c | 23 +++++++++++++++++++++++
 1 file changed, 23 insertions(+)
arch/x86/kvm/svm/avic.c +6 -0
index a34c5c3b164e..877bc3db2c6e 100644
@@ -725,8 +725,31 @@ int avic_init_vcpu(struct vcpu_svm *svm)
 
 void avic_apicv_post_state_restore(struct kvm_vcpu *vcpu)
 {
+	struct vcpu_svm *svm = to_svm(vcpu);
+	u64 cr8;
+
 	avic_handle_dfr_update(vcpu);
 	avic_handle_ldr_update(vcpu);
+
+	/* Running nested should have inhibited AVIC. */
+	if (WARN_ON_ONCE(nested_svm_virtualize_tpr(vcpu)))

arch/x86/kvm/svm/avic.c int avic_init_vcpu(struct vcpu_svm *svm) · line 745

Quoted 10 lines
arch/x86/kvm/svm/avic.c +8 -0
+		return;
 
 
+
+	/*
+	 * Sync TPR from LAPIC TASKPRI into V_TPR field of the VMCB.
+	 *
+	 * When AVIC is enabled the normal pre-VMRUN sync in sync_lapic_to_cr8()
+	 * is inhibited so any set TPR LAPIC state would not get reflected
+	 * in V_TPR.

Hmm, I think that code is straight up wrong. There's no justification, just a claim:

commit 3bbf3565f48ce3999b5a12cde946f81bd4475312
Author: Suravee Suthikulpanit <[email protected]>
AuthorDate: Wed May 4 14:09:51 2016 -0500
Commit: Paolo Bonzini <[email protected]>
CommitDate: Wed May 18 18:04:31 2016 +0200

svm: Do not intercept CR8 when enable AVIC

When enable AVIC:
* Do not intercept CR8 since this should be handled by AVIC HW.
* Also, we don't need to sync cr8/V_TPR and APIC backing page. <======

Quoted 6 lines
[Rename svm_in_nested_interrupt_shadow to svm_nested_virtualize_tpr. - Paolo]

That claim assumes APIC[TPR] will _never_ be modified by anything other than hardware.

It also isn't clear to me why only sync_lapic_to_cr8() was gated when AVIC was enabled, while sync_cr8_to_lapic() continues to copy V_TRP to the backing page. If AVIC is enabled, then the AVIC hardware updates both the backing page and V_TPR on a guest write to TPR.

Quoted 3 lines

That's obviously false for state restore from userspace, and it's also technically false at steady state, e.g. if KVM managed to trigger emulation of a store to the APIC page, then KVM would bypass the automatic harware sync.

Do you mean emulation due to AVIC being inhibited? I initially thought this could be a problem, but in this scenario, AVIC would be disabled on the next VMRUN, so we will end up sync'ing TPR from the lapic to V_TPR.

Quoted 9 lines

There's also the comically ancient KVM_SET_VAPIC_ADDR, which AFAICT appears to be largely dead code with respect to vTPR (nothing sets KVM_APIC_CHECK_VAPIC except for the initial ioctl), but could again set APIC[TPR] without updating V_TPR.

So, rather than manually do the update during state restore, my vote is to restore the sync logic. And if we want to optimize that code (seems unnecessary), then we should hook all TPR writes.

I guess you mean apic_set_tpr()? We will need to hook into that in addition to updating avic_apicv_post_state_restore() since KVM_SET_LAPIC just does a memcpy of the register state.

Quoted 6 lines
arch/x86/kvm/svm/svm.c +0 -0
index d9931c6c4bc6..1bfebe40854f 100644

arch/x86/kvm/svm/svm.c static inline void sync_lapic_to_cr8(struct kvm_vcpu *vcpu) · line 4052

Quoted 9 lines
arch/x86/kvm/svm/svm.c +1 -2
@@ -4046,8 +4046,7 @@ static inline void sync_lapic_to_cr8(struct kvm_vcpu *vcpu)
        struct vcpu_svm *svm = to_svm(vcpu);
        u64 cr8;
 
-       if (nested_svm_virtualize_tpr(vcpu) ||
-           kvm_vcpu_apicv_active(vcpu))
+       if (nested_svm_virtualize_tpr(vcpu))
                return;
 
        cr8 = kvm_get_cr8(vcpu);

I agree that this is a simpler fix, so would be good to do for backport ease.

The code in sync_lapic_to_cr8 ends up being a function call to kvm_get_cr8() and ~6 instructions, which isn't that much. But if we can gate sync'ing V_TPR to the backing page in sync_cr8_to_lapic() as well, then it might be good to do so.

- Naveen

#4 lore raw

Re: [PATCH 1/2] KVM: SVM: Sync TPR from LAPIC into VMCB::V_TPR when setting LAPIC regs

Quoted 36 lines · Naveen N Rao
Quoted 23 lines · Sean Christopherson

Review

Quoted 6 lines · Maciej S. Szmigiero
(quoted diff) +6 -0
+	/*
+	 * Sync TPR from LAPIC TASKPRI into V_TPR field of the VMCB.
+	 *
+	 * When AVIC is enabled the normal pre-VMRUN sync in sync_lapic_to_cr8()
+	 * is inhibited so any set TPR LAPIC state would not get reflected
+	 * in V_TPR.

Hmm, I think that code is straight up wrong. There's no justification, just a claim:

commit 3bbf3565f48ce3999b5a12cde946f81bd4475312
Author: Suravee Suthikulpanit <[email protected]>
AuthorDate: Wed May 4 14:09:51 2016 -0500
Commit: Paolo Bonzini <[email protected]>
CommitDate: Wed May 18 18:04:31 2016 +0200

svm: Do not intercept CR8 when enable AVIC

When enable AVIC:
* Do not intercept CR8 since this should be handled by AVIC HW.
* Also, we don't need to sync cr8/V_TPR and APIC backing page. <======

Quoted 6 lines
[Rename svm_in_nested_interrupt_shadow to svm_nested_virtualize_tpr. - Paolo]

That claim assumes APIC[TPR] will _never_ be modified by anything other than hardware.

It also isn't clear to me why only sync_lapic_to_cr8() was gated when AVIC was enabled, while sync_cr8_to_lapic() continues to copy V_TRP to the backing page. If AVIC is enabled, then the AVIC hardware updates both the backing page and V_TPR on a guest write to TPR.

Quoted 7 lines
Quoted 3 lines

That's obviously false for state restore from userspace, and it's also technically false at steady state, e.g. if KVM managed to trigger emulation of a store to the APIC page, then KVM would bypass the automatic harware sync.

Do you mean emulation due to AVIC being inhibited? I initially thought this could be a problem, but in this scenario, AVIC would be disabled on the next VMRUN, so we will end up sync'ing TPR from the lapic to V_TPR.

No, if emulation is triggered when AVIC isn't inhibited. E.g. a contrived but entirely possible situation would be if MOVBE isn't supported in hardware, KVM is emulating MOVBE for emulation, and the guest sets the TPR via MOVBE. The MOVBE #UDs, KVM emulates in response to the #UD, and Bob's your uncle.

There are other scenarios where KVM would emulate, though they're even more contrived.

Quoted 10 lines
Quoted 8 lines

There's also the comically ancient KVM_SET_VAPIC_ADDR, which AFAICT appears to be largely dead code with respect to vTPR (nothing sets KVM_APIC_CHECK_VAPIC except for the initial ioctl), but could again set APIC[TPR] without updating V_TPR.

So, rather than manually do the update during state restore, my vote is to restore the sync logic. And if we want to optimize that code (seems unnecessary), then we should hook all TPR writes.

I guess you mean apic_set_tpr()?

Yep.

Quoted 3 lines

We will need to hook into that in addition to updating avic_apicv_post_state_restore() since KVM_SET_LAPIC just does a memcpy of the register state.

Yeah, or explicitly call the hook, e.g. like kvm_apic_set_state() does for hwapic_isr_update(). But I don't think we should add a hook unless someone proves that unconditionally synchronizing before VMRUN affects performance.

Quoted 26 lines
Quoted 5 lines
arch/x86/kvm/svm/svm.c +0 -0
index d9931c6c4bc6..1bfebe40854f 100644

arch/x86/kvm/svm/svm.c static inline void sync_lapic_to_cr8(struct kvm_vcpu *vcpu) · line 4052

Quoted 9 lines
arch/x86/kvm/svm/svm.c +1 -2
@@ -4046,8 +4046,7 @@ static inline void sync_lapic_to_cr8(struct kvm_vcpu *vcpu)
        struct vcpu_svm *svm = to_svm(vcpu);
        u64 cr8;
 
-       if (nested_svm_virtualize_tpr(vcpu) ||
-           kvm_vcpu_apicv_active(vcpu))
+       if (nested_svm_virtualize_tpr(vcpu))
                return;
 
        cr8 = kvm_get_cr8(vcpu);

I agree that this is a simpler fix, so would be good to do for backport ease.

The code in sync_lapic_to_cr8 ends up being a function call to kvm_get_cr8() and ~6 instructions, which isn't that much. But if we can gate sync'ing V_TPR to the backing page in sync_cr8_to_lapic() as well, then it might be good to do so.

- Naveen

#5 lore raw

Re: [PATCH 1/2] KVM: SVM: Sync TPR from LAPIC into VMCB::V_TPR when setting LAPIC regs

In reply to Sean Christopherson
Quoted 96 lines · Sean Christopherson
Quoted 44 lines · Maciej S. Szmigiero

From: "Maciej S. Szmigiero" <[email protected]>

When AVIC is enabled the normal pre-VMRUN sync in sync_lapic_to_cr8() is inhibited so any changed TPR in the LAPIC state would not get copied into the V_TPR field of VMCB.

AVIC does sync between these two fields, however it does so only on explicit guest writes to one of these fields, not on a bare VMRUN.

This is especially true when it is the userspace setting LAPIC state via KVM_SET_LAPIC ioctl() since userspace does not have access to the guest VMCB.

Practice shows that it is the V_TPR that is actually used by the AVIC to decide whether to issue pending interrupts to the CPU (not TPR in TASKPRI), so any leftover value in V_TPR will cause serious interrupt delivery issues in the guest when AVIC is enabled.

Fix this issue by explicitly copying LAPIC TPR to VMCB::V_TPR in avic_apicv_post_state_restore(), which gets called from KVM_SET_LAPIC and similar code paths when AVIC is enabled.

Fixes 3bbf3565f48c ("svm: Do not intercept CR8 when enable AVIC")
---
  arch/x86/kvm/svm/avic.c | 23 +++++++++++++++++++++++
  1 file changed, 23 insertions(+)
arch/x86/kvm/svm/avic.c +6 -0
index a34c5c3b164e..877bc3db2c6e 100644
@@ -725,8 +725,31 @@ int avic_init_vcpu(struct vcpu_svm *svm)
 
  void avic_apicv_post_state_restore(struct kvm_vcpu *vcpu)
  {
+	struct vcpu_svm *svm = to_svm(vcpu);
+	u64 cr8;
+
  	avic_handle_dfr_update(vcpu);
  	avic_handle_ldr_update(vcpu);
+
+	/* Running nested should have inhibited AVIC. */
+	if (WARN_ON_ONCE(nested_svm_virtualize_tpr(vcpu)))

arch/x86/kvm/svm/avic.c int avic_init_vcpu(struct vcpu_svm *svm) · line 745

Quoted 10 lines
arch/x86/kvm/svm/avic.c +8 -0
+		return;
 
 
+
+	/*
+	 * Sync TPR from LAPIC TASKPRI into V_TPR field of the VMCB.
+	 *
+	 * When AVIC is enabled the normal pre-VMRUN sync in sync_lapic_to_cr8()
+	 * is inhibited so any set TPR LAPIC state would not get reflected
+	 * in V_TPR.

Hmm, I think that code is straight up wrong. There's no justification, just a claim:

commit 3bbf3565f48ce3999b5a12cde946f81bd4475312
Author: Suravee Suthikulpanit <[email protected]>
AuthorDate: Wed May 4 14:09:51 2016 -0500
Commit: Paolo Bonzini <[email protected]>
CommitDate: Wed May 18 18:04:31 2016 +0200

svm: Do not intercept CR8 when enable AVIC

When enable AVIC:
* Do not intercept CR8 since this should be handled by AVIC HW.
* Also, we don't need to sync cr8/V_TPR and APIC backing page. <======

[Rename svm_in_nested_interrupt_shadow to svm_nested_virtualize_tpr. - Paolo]

That claim assumes APIC[TPR] will _never_ be modified by anything other than hardware. That's obviously false for state restore from userspace, and it's also technically false at steady state, e.g. if KVM managed to trigger emulation of a store to the APIC page, then KVM would bypass the automatic harware sync.

There's also the comically ancient KVM_SET_VAPIC_ADDR, which AFAICT appears to be largely dead code with respect to vTPR (nothing sets KVM_APIC_CHECK_VAPIC except for the initial ioctl), but could again set APIC[TPR] without updating V_TPR.

So, rather than manually do the update during state restore, my vote is to restore the sync logic. And if we want to optimize that code (seems unnecessary), then we should hook all TPR writes.

arch/x86/kvm/svm/svm.c +0 -0
index d9931c6c4bc6..1bfebe40854f 100644

arch/x86/kvm/svm/svm.c static inline void sync_lapic_to_cr8(struct kvm_vcpu *vcpu) · line 4052

Quoted 9 lines
arch/x86/kvm/svm/svm.c +1 -2
@@ -4046,8 +4046,7 @@ static inline void sync_lapic_to_cr8(struct kvm_vcpu *vcpu)
         struct vcpu_svm *svm = to_svm(vcpu);
         u64 cr8;
 
-       if (nested_svm_virtualize_tpr(vcpu) ||
-           kvm_vcpu_apicv_active(vcpu))
+       if (nested_svm_virtualize_tpr(vcpu))
                 return;
 
         cr8 = kvm_get_cr8(vcpu);

So you want to just do an unconditional LAPIC -> V_TPR sync at each VMRUN and not try to patch every code flow where these possibly could get de-synced to do such sync only on demand, correct?

By the way, the original Suravee's submission for the aforementioned patch did *not* inhibit that sync when AVIC is on [1].

Something similar to this sync inhibit only showed in v4 [2], probably upon Radim's comment on v3 [3] that:

Quoted 1 line

I think we can exit early with svm_vcpu_avic_enabled().

But the initial sync inhibit condition in v4 was essentially nested_svm_virtualize_tpr() && svm_vcpu_avic_enabled(), which suggests there was some confusion what was exactly meant by the reviewer comment.

The final sync inhibit condition only showed in v5 [4]. No further discussion happened on that point.

Thanks,
Maciej

[1]: https://lore.kernel.org/kvm/[email protected]/
[2]: https://lore.kernel.org/kvm/[email protected]/
[3]: https://lore.kernel.org/kvm/[email protected]/
[4]: https://lore.kernel.org/kvm/[email protected]/

#6 lore raw

Re: [PATCH 1/2] KVM: SVM: Sync TPR from LAPIC into VMCB::V_TPR when setting LAPIC regs

Quoted 50 lines · Maciej S. Szmigiero
Quoted 49 lines · Sean Christopherson

Review

Quoted 6 lines · Maciej S. Szmigiero
(quoted diff) +6 -0
+	/*
+	 * Sync TPR from LAPIC TASKPRI into V_TPR field of the VMCB.
+	 *
+	 * When AVIC is enabled the normal pre-VMRUN sync in sync_lapic_to_cr8()
+	 * is inhibited so any set TPR LAPIC state would not get reflected
+	 * in V_TPR.

Hmm, I think that code is straight up wrong. There's no justification, just a claim:

commit 3bbf3565f48ce3999b5a12cde946f81bd4475312
Author: Suravee Suthikulpanit <[email protected]>
AuthorDate: Wed May 4 14:09:51 2016 -0500
Commit: Paolo Bonzini <[email protected]>
CommitDate: Wed May 18 18:04:31 2016 +0200

svm: Do not intercept CR8 when enable AVIC
When enable AVIC:
* Do not intercept CR8 since this should be handled by AVIC HW.
* Also, we don't need to sync cr8/V_TPR and APIC backing page. <======

[Rename svm_in_nested_interrupt_shadow to svm_nested_virtualize_tpr. - Paolo]

That claim assumes APIC[TPR] will _never_ be modified by anything other than hardware. That's obviously false for state restore from userspace, and it's also technically false at steady state, e.g. if KVM managed to trigger emulation of a store to the APIC page, then KVM would bypass the automatic harware sync.

There's also the comically ancient KVM_SET_VAPIC_ADDR, which AFAICT appears to be largely dead code with respect to vTPR (nothing sets KVM_APIC_CHECK_VAPIC except for the initial ioctl), but could again set APIC[TPR] without updating V_TPR.

So, rather than manually do the update during state restore, my vote is to restore the sync logic. And if we want to optimize that code (seems unnecessary), then we should hook all TPR writes.

arch/x86/kvm/svm/svm.c +1 -2
index d9931c6c4bc6..1bfebe40854f 100644
@@ -4046,8 +4046,7 @@ static inline void sync_lapic_to_cr8(struct kvm_vcpu *vcpu)
         struct vcpu_svm *svm = to_svm(vcpu);
         u64 cr8;
-       if (nested_svm_virtualize_tpr(vcpu) ||
-           kvm_vcpu_apicv_active(vcpu))
+       if (nested_svm_virtualize_tpr(vcpu))
Quoted 8 lines

arch/x86/kvm/svm/svm.c static inline void sync_lapic_to_cr8(struct kvm_vcpu *vcpu)

Quoted 4 lines
arch/x86/kvm/svm/svm.c +0 -0
                 return;
         cr8 = kvm_get_cr8(vcpu);
 
 

So you want to just do an unconditional LAPIC -> V_TPR sync at each VMRUN and not try to patch every code flow where these possibly could get de-synced to do such sync only on demand, correct?

Yep. For a fix, I definitely want to go with the bare minimum. If we want to optimize the sync, that can be done on top, and it can be done irrespective of AVIC. E.g. for guests that don't modify TPR, the sync is almost pure overhead too.

Quoted 11 lines

By the way, the original Suravee's submission for the aforementioned patch did *not* inhibit that sync when AVIC is on [1].

Something similar to this sync inhibit only showed in v4 [2], probably upon Radim's comment on v3 [3] that:

Quoted 1 line

I think we can exit early with svm_vcpu_avic_enabled().

But the initial sync inhibit condition in v4 was essentially nested_svm_virtualize_tpr() && svm_vcpu_avic_enabled(), which suggests there was some confusion what was exactly meant by the reviewer comment.

Hmm, I suspect that was just a goof. My guess is that Radim and Suravee simply forgot to consider TPR writes that aren't handled by hardware.

Quoted 11 lines

The final sync inhibit condition only showed in v5 [4]. No further discussion happened on that point.

Thanks,
Maciej

[1]: https://lore.kernel.org/kvm/[email protected]/
[2]: https://lore.kernel.org/kvm/[email protected]/
[3]: https://lore.kernel.org/kvm/[email protected]/
[4]: https://lore.kernel.org/kvm/[email protected]/

#7 lore raw

PATCH 2/2 KVM: selftests: Test TPR / CR8 sync and interrupt masking

In reply to Maciej S. Szmigiero

From: "Maciej S. Szmigiero" <[email protected]>

Add a few extra TPR / CR8 tests to x86's xapic_state_test to see if:
* TPR is 0 on reset,
* TPR, PPR and CR8 are equal inside the guest,
* TPR and CR8 read equal by the host after a VMExit
* TPR borderline values set by the host correctly mask interrupts in the guest.

These hopefully will catch the most obvious cases of improper TPR sync or interrupt masking.

Do these tests both in x2APIC and xAPIC modes.
The x2APIC mode uses SELF_IPI register to trigger interrupts to give it a bit of exercise too.

---
 .../testing/selftests/kvm/include/x86/apic.h  |   5 +
 .../selftests/kvm/x86/xapic_state_test.c      | 265 +++++++++++++++++-
 2 files changed, 267 insertions(+), 3 deletions(-)
tools/testing/selftests/kvm/include/x86/apic.h +5 -0
index 80fe9f69b38d..67285e533e14 100644
@@ -27,7 +27,11 @@
 #define	APIC_LVR	0x30
 #define		GET_APIC_ID_FIELD(x)	(((x) >> 24) & 0xFF)
 #define	APIC_TASKPRI	0x80
+#define		APIC_TASKPRI_TP_SHIFT	4
+#define		APIC_TASKPRI_TP_MASK	GENMASK(7, 4)
 #define	APIC_PROCPRI	0xA0
+#define		APIC_PROCPRI_PP_SHIFT	4
+#define		APIC_PROCPRI_PP_MASK	GENMASK(7, 4)
 #define	APIC_EOI	0xB0
 #define	APIC_SPIV	0xF0
 #define		APIC_SPIV_FOCUS_DISABLED	(1 << 9)
@@ -67,6 +71,7 @@
 #define	APIC_TMICT	0x380
 #define	APIC_TMCCT	0x390
 #define	APIC_TDCR	0x3E0
+#define	APIC_SELF_IPI	0x3F0
 
 void apic_disable(void);
 void xapic_enable(void);
tools/testing/selftests/kvm/x86/xapic_state_test.c +262 -3

305 lines not on this page · raw message

#8 lore raw
In reply to Maciej S. Szmigiero
Quoted 6 lines · Maciej S. Szmigiero

From: "Maciej S. Szmigiero" <[email protected]>

When AVIC is enabled the normal pre-VMRUN LAPIC TPR to VMCB::V_TPR sync in sync_lapic_to_cr8() is inhibited so any changed TPR in the LAPIC state would *not* get copied into the V_TPR field of VMCB.

Quoted 6 lines

AVIC does sync between these two fields, however it does so only on explicit guest writes to one of these fields, not on a bare VMRUN.

This is especially true when it is the userspace setting LAPIC state via KVM_SET_LAPIC ioctl() since userspace does not have access to the guest VMCB.

Dumb question: why is the VMM updating TPR? Is this related to live migration or such?

I think I do see the problem described here, but when AVIC is temporarily inhibited. So, trying to understand if there are other flows involving the VMM where TPR could be updated outside of the guest.

Quoted 6 lines

Practice shows that it is the V_TPR that is actually used by the AVIC to decide whether to issue pending interrupts to the CPU (not TPR in TASKPRI), so any leftover value in V_TPR will cause serious interrupt delivery issues in the guest when AVIC is enabled.

Quoted 6 lines

Fix this issue by explicitly copying LAPIC TPR to VMCB::V_TPR in avic_apicv_post_state_restore(), which gets called from KVM_SET_LAPIC and similar code paths when AVIC is enabled.

Add also a relevant set of tests to xapic_state_test so hopefully we'll be protected against getting such regressions in the future.

Do the new tests reproduce this issue?

Quoted 5 lines

Yes, this breaks real guests when AVIC is enabled. Specifically, the one OS that sometimes needs different handling and its name begins with letter 'W'.

Indeed, Linux does not use TPR AFAIK.

- Naveen

#9 lore raw
Quoted 10 lines · Naveen N Rao
Quoted 9 lines · Maciej S. Szmigiero

From: "Maciej S. Szmigiero" <[email protected]>

When AVIC is enabled the normal pre-VMRUN LAPIC TPR to VMCB::V_TPR sync in sync_lapic_to_cr8() is inhibited so any changed TPR in the LAPIC state would *not* get copied into the V_TPR field of VMCB.

AVIC does sync between these two fields, however it does so only on explicit guest writes to one of these fields, not on a bare VMRUN.

Quoted 6 lines
Quoted 3 lines

This is especially true when it is the userspace setting LAPIC state via KVM_SET_LAPIC ioctl() since userspace does not have access to the guest VMCB.

Dumb question: why is the VMM updating TPR? Is this related to live migration or such?

In this case, VMM is resetting LAPIC state on machine reset.

Quoted 10 lines

I think I do see the problem described here, but when AVIC is temporarily inhibited. So, trying to understand if there are other flows involving the VMM where TPR could be updated outside of the guest.

Quoted 6 lines

Practice shows that it is the V_TPR that is actually used by the AVIC to decide whether to issue pending interrupts to the CPU (not TPR in TASKPRI), so any leftover value in V_TPR will cause serious interrupt delivery issues in the guest when AVIC is enabled.

Quoted 8 lines
Quoted 6 lines

Fix this issue by explicitly copying LAPIC TPR to VMCB::V_TPR in avic_apicv_post_state_restore(), which gets called from KVM_SET_LAPIC and similar code paths when AVIC is enabled.

Add also a relevant set of tests to xapic_state_test so hopefully we'll be protected against getting such regressions in the future.

Do the new tests reproduce this issue?

Yes, and also check quite a bit more of TPR-related functionality.

Quoted 11 lines
Quoted 5 lines

Yes, this breaks real guests when AVIC is enabled. Specifically, the one OS that sometimes needs different handling and its name begins with letter 'W'.

Indeed, Linux does not use TPR AFAIK.

- Naveen

Thanks,
Maciej

#10 lore raw
Quoted 34 lines · Maciej S. Szmigiero
Quoted 10 lines · Naveen N Rao
Quoted 9 lines · Maciej S. Szmigiero

From: "Maciej S. Szmigiero" <[email protected]>

When AVIC is enabled the normal pre-VMRUN LAPIC TPR to VMCB::V_TPR sync in sync_lapic_to_cr8() is inhibited so any changed TPR in the LAPIC state would *not* get copied into the V_TPR field of VMCB.

AVIC does sync between these two fields, however it does so only on explicit guest writes to one of these fields, not on a bare VMRUN.

Quoted 6 lines
Quoted 3 lines

This is especially true when it is the userspace setting LAPIC state via KVM_SET_LAPIC ioctl() since userspace does not have access to the guest VMCB.

Dumb question: why is the VMM updating TPR? Is this related to live migration or such?

In this case, VMM is resetting LAPIC state on machine reset.

Quoted 14 lines

I think I do see the problem described here, but when AVIC is temporarily inhibited. So, trying to understand if there are other flows involving the VMM where TPR could be updated outside of the guest.

Quoted 10 lines

Practice shows that it is the V_TPR that is actually used by the AVIC to decide whether to issue pending interrupts to the CPU (not TPR in TASKPRI), so any leftover value in V_TPR will cause serious interrupt delivery issues in the guest when AVIC is enabled.

Fix this issue by explicitly copying LAPIC TPR to VMCB::V_TPR in avic_apicv_post_state_restore(), which gets called from KVM_SET_LAPIC and similar code paths when AVIC is enabled.

Quoted 16 lines
Quoted 4 lines
Quoted 2 lines

Add also a relevant set of tests to xapic_state_test so hopefully we'll be protected against getting such regressions in the future.

Do the new tests reproduce this issue?

Yes, and also check quite a bit more of TPR-related functionality.

Quoted 9 lines
Quoted 5 lines

Yes, this breaks real guests when AVIC is enabled. Specifically, the one OS that sometimes needs different handling and its name begins with letter 'W'.

Indeed, Linux does not use TPR AFAIK.

I believe it does, during the local APIC initialization. When Maciej determined the root cause of this issue, I was wondering why we have not seen it earlier in Linux. I found that Linux takes a defensive approach and drains all pending interrupts during lapic initialization. Essentially, for each CPU, Linux will:
- temporarily disable the Local APIC (via Spurious Int Vector Reg)
- set the TPR to accept all "regular" interrupts i.e. tpr=0x10
- drain all pending interrupts in ISR and/or IRR
- attempt the above draining step a max of 512 times
- then re-enable APIC and continue initialization

The relevant code is in setup_local_APIC()
https://elixir.bootlin.com/linux/v6.16/source/arch/x86/kernel/apic/apic.c#L1533-L1545

So without Maciej's proposed change, other OSs that are not as resilient could also be affected by this issue.

Alejandro

Quoted 7 lines
Quoted 2 lines

- Naveen

Thanks,
Maciej

#11 lore raw
Quoted 9 lines · Alejandro Jimenez
Quoted 6 lines · Maciej S. Szmigiero
Quoted 5 lines · Naveen N Rao
Quoted 3 lines

Yes, this breaks real guests when AVIC is enabled. Specifically, the one OS that sometimes needs different handling and its name begins with letter 'W'.

Indeed, Linux does not use TPR AFAIK.

I believe it does,

Heh, yes, Linux technically "uses" the TPR in that it does a one-time write to it. But what Naveen really meant is that Linux doesn't actively use TPR to manage what IRQs are masked/allowed, whereas Windows heavily uses TPR to do exactly that. Specifically, what matters is that Linux doesn't use TPR to _mask_ IRQs, and so clobbering it to '0' on migration is largely benign.

Quoted 27 lines

during the local APIC initialization. When Maciej
determined the root cause of this issue, I was wondering why we have not seen it earlier in Linux. I found that Linux takes a defensive approach and drains all pending interrupts during lapic initialization. Essentially, for each CPU, Linux will:
- temporarily disable the Local APIC (via Spurious Int Vector Reg)
- set the TPR to accept all "regular" interrupts i.e. tpr=0x10
- drain all pending interrupts in ISR and/or IRR
- attempt the above draining step a max of 512 times
- then re-enable APIC and continue initialization

The relevant code is in setup_local_APIC()
https://elixir.bootlin.com/linux/v6.16/source/arch/x86/kernel/apic/apic.c#L1533-L1545

So without Maciej's proposed change, other OSs that are not as resilient could also be affected by this issue.

Alejandro

Quoted 7 lines
Quoted 2 lines

- Naveen

Thanks,
Maciej