Skip to content

Commit 505aef8

Browse files
Xiao Guangrongavikivity
authored andcommitted
KVM: MMU: cleanup FNAME(invlpg)
Directly Use mmu_page_zap_pte to zap spte in FNAME(invlpg), also remove the same code between FNAME(invlpg) and FNAME(sync_page) Signed-off-by: Xiao Guangrong <[email protected]> Signed-off-by: Avi Kivity <[email protected]>
1 parent d01f8d5 commit 505aef8

File tree

2 files changed

+27
-33
lines changed

2 files changed

+27
-33
lines changed

arch/x86/kvm/mmu.c

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1809,25 +1809,29 @@ static void validate_direct_spte(struct kvm_vcpu *vcpu, u64 *sptep,
18091809
}
18101810
}
18111811

1812-
static void mmu_page_zap_pte(struct kvm *kvm, struct kvm_mmu_page *sp,
1812+
static bool mmu_page_zap_pte(struct kvm *kvm, struct kvm_mmu_page *sp,
18131813
u64 *spte)
18141814
{
18151815
u64 pte;
18161816
struct kvm_mmu_page *child;
18171817

18181818
pte = *spte;
18191819
if (is_shadow_present_pte(pte)) {
1820-
if (is_last_spte(pte, sp->role.level))
1820+
if (is_last_spte(pte, sp->role.level)) {
18211821
drop_spte(kvm, spte);
1822-
else {
1822+
if (is_large_pte(pte))
1823+
--kvm->stat.lpages;
1824+
} else {
18231825
child = page_header(pte & PT64_BASE_ADDR_MASK);
18241826
drop_parent_pte(child, spte);
18251827
}
1826-
} else if (is_mmio_spte(pte))
1828+
return true;
1829+
}
1830+
1831+
if (is_mmio_spte(pte))
18271832
mmu_spte_clear_no_track(spte);
18281833

1829-
if (is_large_pte(pte))
1830-
--kvm->stat.lpages;
1834+
return false;
18311835
}
18321836

18331837
static void kvm_mmu_page_unlink_children(struct kvm *kvm,

arch/x86/kvm/paging_tmpl.h

Lines changed: 17 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -656,14 +656,25 @@ static int FNAME(page_fault)(struct kvm_vcpu *vcpu, gva_t addr, u32 error_code,
656656
return 0;
657657
}
658658

659+
static gpa_t FNAME(get_level1_sp_gpa)(struct kvm_mmu_page *sp)
660+
{
661+
int offset = 0;
662+
663+
WARN_ON(sp->role.level != 1);
664+
665+
if (PTTYPE == 32)
666+
offset = sp->role.quadrant << PT64_LEVEL_BITS;
667+
668+
return gfn_to_gpa(sp->gfn) + offset * sizeof(pt_element_t);
669+
}
670+
659671
static void FNAME(invlpg)(struct kvm_vcpu *vcpu, gva_t gva)
660672
{
661673
struct kvm_shadow_walk_iterator iterator;
662674
struct kvm_mmu_page *sp;
663675
gpa_t pte_gpa = -1;
664676
int level;
665677
u64 *sptep;
666-
int need_flush = 0;
667678

668679
vcpu_clear_mmio_info(vcpu, gva);
669680

@@ -675,36 +686,20 @@ static void FNAME(invlpg)(struct kvm_vcpu *vcpu, gva_t gva)
675686

676687
sp = page_header(__pa(sptep));
677688
if (is_last_spte(*sptep, level)) {
678-
int offset, shift;
679-
680689
if (!sp->unsync)
681690
break;
682691

683-
shift = PAGE_SHIFT -
684-
(PT_LEVEL_BITS - PT64_LEVEL_BITS) * level;
685-
offset = sp->role.quadrant << shift;
686-
687-
pte_gpa = (sp->gfn << PAGE_SHIFT) + offset;
692+
pte_gpa = FNAME(get_level1_sp_gpa)(sp);
688693
pte_gpa += (sptep - sp->spt) * sizeof(pt_element_t);
689694

690-
if (is_shadow_present_pte(*sptep)) {
691-
if (is_large_pte(*sptep))
692-
--vcpu->kvm->stat.lpages;
693-
drop_spte(vcpu->kvm, sptep);
694-
need_flush = 1;
695-
} else if (is_mmio_spte(*sptep))
696-
mmu_spte_clear_no_track(sptep);
697-
698-
break;
695+
if (mmu_page_zap_pte(vcpu->kvm, sp, sptep))
696+
kvm_flush_remote_tlbs(vcpu->kvm);
699697
}
700698

701699
if (!is_shadow_present_pte(*sptep) || !sp->unsync_children)
702700
break;
703701
}
704702

705-
if (need_flush)
706-
kvm_flush_remote_tlbs(vcpu->kvm);
707-
708703
atomic_inc(&vcpu->kvm->arch.invlpg_counter);
709704

710705
spin_unlock(&vcpu->kvm->mmu_lock);
@@ -769,19 +764,14 @@ static gpa_t FNAME(gva_to_gpa_nested)(struct kvm_vcpu *vcpu, gva_t vaddr,
769764
*/
770765
static int FNAME(sync_page)(struct kvm_vcpu *vcpu, struct kvm_mmu_page *sp)
771766
{
772-
int i, offset, nr_present;
767+
int i, nr_present = 0;
773768
bool host_writable;
774769
gpa_t first_pte_gpa;
775770

776-
offset = nr_present = 0;
777-
778771
/* direct kvm_mmu_page can not be unsync. */
779772
BUG_ON(sp->role.direct);
780773

781-
if (PTTYPE == 32)
782-
offset = sp->role.quadrant << PT64_LEVEL_BITS;
783-
784-
first_pte_gpa = gfn_to_gpa(sp->gfn) + offset * sizeof(pt_element_t);
774+
first_pte_gpa = FNAME(get_level1_sp_gpa)(sp);
785775

786776
for (i = 0; i < PT64_ENT_PER_PAGE; i++) {
787777
unsigned pte_access;

0 commit comments

Comments
 (0)