Skip to content

Commit 6d35786

Browse files
committed
Merge tag 'for-linus' of git://git.kernel.org/pub/scm/virt/kvm/kvm
Pull kvm fixes from Paolo Bonzini: "Three bug fixes for x86: - Check that nEPT/nNPT is enabled in slow flush hypercalls. If it is not, the hypercalls can be processed as usual even while running a nested guest - Fix shadow paging use-after-free due to page tables changing outside execution of the guest. A bug that is 16 years old and stems from an imprecision in the very first KVM series - Scan IRR whenever PID.ON is true, even if PIR is empty, which avoids a somewhat rare WARN" * tag 'for-linus' of git://git.kernel.org/pub/scm/virt/kvm/kvm: KVM: x86: Fix shadow paging use-after-free due to unexpected GFN KVM: x86: Fix misleading variable names and add more comments for PIR=>IRR flow KVM: x86: Do IRR scan in __kvm_apic_update_irr even if PIR is empty KVM: x86: check for nEPT/nNPT in slow flush hypercalls
2 parents 7fd2df2 + 0cb2af2 commit 6d35786

4 files changed

Lines changed: 60 additions & 41 deletions

File tree

arch/x86/kvm/hyperv.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2040,7 +2040,7 @@ static u64 kvm_hv_flush_tlb(struct kvm_vcpu *vcpu, struct kvm_hv_hcall *hc)
20402040
* flush). Translate the address here so the memory can be uniformly
20412041
* read with kvm_read_guest().
20422042
*/
2043-
if (!hc->fast && is_guest_mode(vcpu)) {
2043+
if (!hc->fast && mmu_is_nested(vcpu)) {
20442044
hc->ingpa = translate_nested_gpa(vcpu, hc->ingpa, 0, NULL);
20452045
if (unlikely(hc->ingpa == INVALID_GPA))
20462046
return HV_STATUS_INVALID_HYPERCALL_INPUT;

arch/x86/kvm/lapic.c

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -667,13 +667,15 @@ bool __kvm_apic_update_irr(unsigned long *pir, void *regs, int *max_irr)
667667
u32 *__pir = (void *)pir_vals;
668668
u32 i, vec;
669669
u32 irr_val, prev_irr_val;
670-
int max_updated_irr;
670+
int max_new_irr;
671671

672-
max_updated_irr = -1;
673-
*max_irr = -1;
674-
675-
if (!pi_harvest_pir(pir, pir_vals))
672+
if (!pi_harvest_pir(pir, pir_vals)) {
673+
*max_irr = apic_find_highest_vector(regs + APIC_IRR);
676674
return false;
675+
}
676+
677+
max_new_irr = -1;
678+
*max_irr = -1;
677679

678680
for (i = vec = 0; i <= 7; i++, vec += 32) {
679681
u32 *p_irr = (u32 *)(regs + APIC_IRR + i * 0x10);
@@ -688,25 +690,25 @@ bool __kvm_apic_update_irr(unsigned long *pir, void *regs, int *max_irr)
688690
!try_cmpxchg(p_irr, &prev_irr_val, irr_val));
689691

690692
if (prev_irr_val != irr_val)
691-
max_updated_irr = __fls(irr_val ^ prev_irr_val) + vec;
693+
max_new_irr = __fls(irr_val ^ prev_irr_val) + vec;
692694
}
693695
if (irr_val)
694696
*max_irr = __fls(irr_val) + vec;
695697
}
696698

697-
return ((max_updated_irr != -1) &&
698-
(max_updated_irr == *max_irr));
699+
return max_new_irr != -1 && max_new_irr == *max_irr;
699700
}
700701
EXPORT_SYMBOL_FOR_KVM_INTERNAL(__kvm_apic_update_irr);
701702

702703
bool kvm_apic_update_irr(struct kvm_vcpu *vcpu, unsigned long *pir, int *max_irr)
703704
{
704705
struct kvm_lapic *apic = vcpu->arch.apic;
705-
bool irr_updated = __kvm_apic_update_irr(pir, apic->regs, max_irr);
706+
bool max_irr_is_from_pir;
706707

707-
if (unlikely(!apic->apicv_active && irr_updated))
708+
max_irr_is_from_pir = __kvm_apic_update_irr(pir, apic->regs, max_irr);
709+
if (unlikely(!apic->apicv_active && max_irr_is_from_pir))
708710
apic->irr_pending = true;
709-
return irr_updated;
711+
return max_irr_is_from_pir;
710712
}
711713
EXPORT_SYMBOL_FOR_KVM_INTERNAL(kvm_apic_update_irr);
712714

arch/x86/kvm/mmu/mmu.c

Lines changed: 14 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,8 @@ static struct kmem_cache *pte_list_desc_cache;
182182
struct kmem_cache *mmu_page_header_cache;
183183

184184
static void mmu_spte_set(u64 *sptep, u64 spte);
185+
static int mmu_page_zap_pte(struct kvm *kvm, struct kvm_mmu_page *sp,
186+
u64 *spte, struct list_head *invalid_list);
185187

186188
struct kvm_mmu_role_regs {
187189
const unsigned long cr0;
@@ -1287,19 +1289,6 @@ static void drop_spte(struct kvm *kvm, u64 *sptep)
12871289
rmap_remove(kvm, sptep);
12881290
}
12891291

1290-
static void drop_large_spte(struct kvm *kvm, u64 *sptep, bool flush)
1291-
{
1292-
struct kvm_mmu_page *sp;
1293-
1294-
sp = sptep_to_sp(sptep);
1295-
WARN_ON_ONCE(sp->role.level == PG_LEVEL_4K);
1296-
1297-
drop_spte(kvm, sptep);
1298-
1299-
if (flush)
1300-
kvm_flush_remote_tlbs_sptep(kvm, sptep);
1301-
}
1302-
13031292
/*
13041293
* Write-protect on the specified @sptep, @pt_protect indicates whether
13051294
* spte write-protection is caused by protecting shadow page table.
@@ -2466,7 +2455,8 @@ static struct kvm_mmu_page *kvm_mmu_get_child_sp(struct kvm_vcpu *vcpu,
24662455
{
24672456
union kvm_mmu_page_role role;
24682457

2469-
if (is_shadow_present_pte(*sptep) && !is_large_pte(*sptep))
2458+
if (is_shadow_present_pte(*sptep) && !is_large_pte(*sptep) &&
2459+
spte_to_child_sp(*sptep) && spte_to_child_sp(*sptep)->gfn == gfn)
24702460
return ERR_PTR(-EEXIST);
24712461

24722462
role = kvm_mmu_child_role(sptep, direct, access);
@@ -2544,13 +2534,16 @@ static void __link_shadow_page(struct kvm *kvm,
25442534

25452535
BUILD_BUG_ON(VMX_EPT_WRITABLE_MASK != PT_WRITABLE_MASK);
25462536

2547-
/*
2548-
* If an SPTE is present already, it must be a leaf and therefore
2549-
* a large one. Drop it, and flush the TLB if needed, before
2550-
* installing sp.
2551-
*/
2552-
if (is_shadow_present_pte(*sptep))
2553-
drop_large_spte(kvm, sptep, flush);
2537+
if (is_shadow_present_pte(*sptep)) {
2538+
struct kvm_mmu_page *parent_sp;
2539+
LIST_HEAD(invalid_list);
2540+
2541+
parent_sp = sptep_to_sp(sptep);
2542+
WARN_ON_ONCE(parent_sp->role.level == PG_LEVEL_4K);
2543+
2544+
mmu_page_zap_pte(kvm, parent_sp, sptep, &invalid_list);
2545+
kvm_mmu_remote_flush_or_zap(kvm, &invalid_list, true);
2546+
}
25542547

25552548
spte = make_nonleaf_spte(sp->spt, sp_ad_disabled(sp));
25562549

arch/x86/kvm/vmx/vmx.c

Lines changed: 32 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7029,8 +7029,8 @@ static void vmx_set_rvi(int vector)
70297029
int vmx_sync_pir_to_irr(struct kvm_vcpu *vcpu)
70307030
{
70317031
struct vcpu_vt *vt = to_vt(vcpu);
7032+
bool max_irr_is_from_pir;
70327033
int max_irr;
7033-
bool got_posted_interrupt;
70347034

70357035
if (KVM_BUG_ON(!enable_apicv, vcpu->kvm))
70367036
return -EIO;
@@ -7042,17 +7042,22 @@ int vmx_sync_pir_to_irr(struct kvm_vcpu *vcpu)
70427042
* But on x86 this is just a compiler barrier anyway.
70437043
*/
70447044
smp_mb__after_atomic();
7045-
got_posted_interrupt =
7046-
kvm_apic_update_irr(vcpu, vt->pi_desc.pir, &max_irr);
7045+
max_irr_is_from_pir = kvm_apic_update_irr(vcpu, vt->pi_desc.pir,
7046+
&max_irr);
70477047
} else {
70487048
max_irr = kvm_lapic_find_highest_irr(vcpu);
7049-
got_posted_interrupt = false;
7049+
max_irr_is_from_pir = false;
70507050
}
70517051

70527052
/*
7053-
* Newly recognized interrupts are injected via either virtual interrupt
7054-
* delivery (RVI) or KVM_REQ_EVENT. Virtual interrupt delivery is
7055-
* disabled in two cases:
7053+
* If APICv is enabled and L2 is not active, then update the Requesting
7054+
* Virtual Interrupt (RVI) portion of vmcs01.GUEST_INTR_STATUS with the
7055+
* highest priority IRR to deliver the IRQ via Virtual Interrupt
7056+
* Delivery. Note, this is required even if the highest priority IRQ
7057+
* was already pending in the IRR, as RVI isn't updated in lockstep with
7058+
* the IRR (unlike apic->irr_pending).
7059+
*
7060+
* For the cases where Virtual Interrupt Delivery can't be used:
70567061
*
70577062
* 1) If L2 is running and the vCPU has a new pending interrupt. If L1
70587063
* wants to exit on interrupts, KVM_REQ_EVENT is needed to synthesize a
@@ -7063,10 +7068,29 @@ int vmx_sync_pir_to_irr(struct kvm_vcpu *vcpu)
70637068
* 2) If APICv is disabled for this vCPU, assigned devices may still
70647069
* attempt to post interrupts. The posted interrupt vector will cause
70657070
* a VM-Exit and the subsequent entry will call sync_pir_to_irr.
7071+
*
7072+
* In both cases, set KVM_REQ_EVENT if and only if the highest priority
7073+
* pending IRQ came from the PIR, as setting KVM_REQ_EVENT if any IRQ
7074+
* is pending may put the vCPU into an infinite loop, e.g. if the IRQ
7075+
* is blocked, then it will stay pending until an IRQ window is opened.
7076+
*
7077+
* Note! It's possible that one or more IRQs were moved from the PIR
7078+
* to the IRR _without_ max_irr_is_from_pir being true! I.e. if there
7079+
* was a higher priority IRQ already pending in the IRR. Not setting
7080+
* KVM_REQ_EVENT in this case is intentional and safe. If APICv is
7081+
* inactive, or L2 is running with exit-on-interrupt off (in vmcs12),
7082+
* i.e. without nested virtual interrupt delivery, then there's no need
7083+
* to request an IRQ window as the lower priority IRQ only needs to be
7084+
* delivered when the higher priority IRQ is dismissed from the ISR,
7085+
* i.e. on the next EOI, and EOIs are always intercepted if APICv is
7086+
* disabled or if L2 is running without nested VID. If L2 is running
7087+
* exit-on-interrupt on (in vmcs12), then the higher priority IRQ will
7088+
* trigger a nested VM-Exit, at which point KVM will re-evaluate L1's
7089+
* pending IRQs.
70667090
*/
70677091
if (!is_guest_mode(vcpu) && kvm_vcpu_apicv_active(vcpu))
70687092
vmx_set_rvi(max_irr);
7069-
else if (got_posted_interrupt)
7093+
else if (max_irr_is_from_pir)
70707094
kvm_make_request(KVM_REQ_EVENT, vcpu);
70717095

70727096
return max_irr;

0 commit comments

Comments
 (0)