Skip to content

Commit 61f9447

Browse files
sean-jcbonzini
authored andcommitted
KVM: x86/mmu: Set disallowed_nx_huge_page in TDP MMU before setting SPTE
Set nx_huge_page_disallowed in TDP MMU shadow pages before making the SP visible to other readers, i.e. before setting its SPTE. This will allow KVM to query the flag when determining if a shadow page can be replaced by a NX huge page without violating the rules of the mitigation. Note, the shadow/legacy MMU holds mmu_lock for write, so it's impossible for another CPU to see a shadow page without an up-to-date nx_huge_page_disallowed, i.e. only the TDP MMU needs the complicated dance. Signed-off-by: Sean Christopherson <seanjc@google.com> Reviewed-by: David Matlack <dmatlack@google.com> Reviewed-by: Yan Zhao <yan.y.zhao@intel.com> Message-Id: <20221019165618.927057-5-seanjc@google.com> Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
1 parent b5b0977 commit 61f9447

File tree

3 files changed

+39
-25
lines changed

3 files changed

+39
-25
lines changed

arch/x86/kvm/mmu/mmu.c

+19-9
Original file line numberDiff line numberDiff line change
@@ -803,11 +803,8 @@ static void account_shadowed(struct kvm *kvm, struct kvm_mmu_page *sp)
803803
kvm_flush_remote_tlbs_with_address(kvm, gfn, 1);
804804
}
805805

806-
void account_nx_huge_page(struct kvm *kvm, struct kvm_mmu_page *sp,
807-
bool nx_huge_page_possible)
806+
void track_possible_nx_huge_page(struct kvm *kvm, struct kvm_mmu_page *sp)
808807
{
809-
sp->nx_huge_page_disallowed = true;
810-
811808
/*
812809
* If it's possible to replace the shadow page with an NX huge page,
813810
* i.e. if the shadow page is the only thing currently preventing KVM
@@ -816,15 +813,23 @@ void account_nx_huge_page(struct kvm *kvm, struct kvm_mmu_page *sp,
816813
* on the list if KVM is reusing an existing shadow page, i.e. if KVM
817814
* links a shadow page at multiple points.
818815
*/
819-
if (!nx_huge_page_possible ||
820-
!list_empty(&sp->possible_nx_huge_page_link))
816+
if (!list_empty(&sp->possible_nx_huge_page_link))
821817
return;
822818

823819
++kvm->stat.nx_lpage_splits;
824820
list_add_tail(&sp->possible_nx_huge_page_link,
825821
&kvm->arch.possible_nx_huge_pages);
826822
}
827823

824+
static void account_nx_huge_page(struct kvm *kvm, struct kvm_mmu_page *sp,
825+
bool nx_huge_page_possible)
826+
{
827+
sp->nx_huge_page_disallowed = true;
828+
829+
if (nx_huge_page_possible)
830+
track_possible_nx_huge_page(kvm, sp);
831+
}
832+
828833
static void unaccount_shadowed(struct kvm *kvm, struct kvm_mmu_page *sp)
829834
{
830835
struct kvm_memslots *slots;
@@ -842,17 +847,22 @@ static void unaccount_shadowed(struct kvm *kvm, struct kvm_mmu_page *sp)
842847
kvm_mmu_gfn_allow_lpage(slot, gfn);
843848
}
844849

845-
void unaccount_nx_huge_page(struct kvm *kvm, struct kvm_mmu_page *sp)
850+
void untrack_possible_nx_huge_page(struct kvm *kvm, struct kvm_mmu_page *sp)
846851
{
847-
sp->nx_huge_page_disallowed = false;
848-
849852
if (list_empty(&sp->possible_nx_huge_page_link))
850853
return;
851854

852855
--kvm->stat.nx_lpage_splits;
853856
list_del_init(&sp->possible_nx_huge_page_link);
854857
}
855858

859+
static void unaccount_nx_huge_page(struct kvm *kvm, struct kvm_mmu_page *sp)
860+
{
861+
sp->nx_huge_page_disallowed = false;
862+
863+
untrack_possible_nx_huge_page(kvm, sp);
864+
}
865+
856866
static struct kvm_memory_slot *
857867
gfn_to_memslot_dirty_bitmap(struct kvm_vcpu *vcpu, gfn_t gfn,
858868
bool no_dirty_log)

arch/x86/kvm/mmu/mmu_internal.h

+2-3
Original file line numberDiff line numberDiff line change
@@ -328,8 +328,7 @@ void disallowed_hugepage_adjust(struct kvm_page_fault *fault, u64 spte, int cur_
328328

329329
void *mmu_memory_cache_alloc(struct kvm_mmu_memory_cache *mc);
330330

331-
void account_nx_huge_page(struct kvm *kvm, struct kvm_mmu_page *sp,
332-
bool nx_huge_page_possible);
333-
void unaccount_nx_huge_page(struct kvm *kvm, struct kvm_mmu_page *sp);
331+
void track_possible_nx_huge_page(struct kvm *kvm, struct kvm_mmu_page *sp);
332+
void untrack_possible_nx_huge_page(struct kvm *kvm, struct kvm_mmu_page *sp);
334333

335334
#endif /* __KVM_X86_MMU_INTERNAL_H */

arch/x86/kvm/mmu/tdp_mmu.c

+18-13
Original file line numberDiff line numberDiff line change
@@ -403,8 +403,11 @@ static void tdp_mmu_unlink_sp(struct kvm *kvm, struct kvm_mmu_page *sp,
403403
lockdep_assert_held_write(&kvm->mmu_lock);
404404

405405
list_del(&sp->link);
406-
if (sp->nx_huge_page_disallowed)
407-
unaccount_nx_huge_page(kvm, sp);
406+
407+
if (sp->nx_huge_page_disallowed) {
408+
sp->nx_huge_page_disallowed = false;
409+
untrack_possible_nx_huge_page(kvm, sp);
410+
}
408411

409412
if (shared)
410413
spin_unlock(&kvm->arch.tdp_mmu_pages_lock);
@@ -1118,16 +1121,13 @@ static int tdp_mmu_map_handle_target_level(struct kvm_vcpu *vcpu,
11181121
* @kvm: kvm instance
11191122
* @iter: a tdp_iter instance currently on the SPTE that should be set
11201123
* @sp: The new TDP page table to install.
1121-
* @account_nx: True if this page table is being installed to split a
1122-
* non-executable huge page.
11231124
* @shared: This operation is running under the MMU lock in read mode.
11241125
*
11251126
* Returns: 0 if the new page table was installed. Non-0 if the page table
11261127
* could not be installed (e.g. the atomic compare-exchange failed).
11271128
*/
11281129
static int tdp_mmu_link_sp(struct kvm *kvm, struct tdp_iter *iter,
1129-
struct kvm_mmu_page *sp, bool account_nx,
1130-
bool shared)
1130+
struct kvm_mmu_page *sp, bool shared)
11311131
{
11321132
u64 spte = make_nonleaf_spte(sp->spt, !kvm_ad_enabled());
11331133
int ret = 0;
@@ -1142,8 +1142,6 @@ static int tdp_mmu_link_sp(struct kvm *kvm, struct tdp_iter *iter,
11421142

11431143
spin_lock(&kvm->arch.tdp_mmu_pages_lock);
11441144
list_add(&sp->link, &kvm->arch.tdp_mmu_pages);
1145-
if (account_nx)
1146-
account_nx_huge_page(kvm, sp, true);
11471145
spin_unlock(&kvm->arch.tdp_mmu_pages_lock);
11481146
tdp_account_mmu_page(kvm, sp);
11491147

@@ -1157,6 +1155,7 @@ static int tdp_mmu_link_sp(struct kvm *kvm, struct tdp_iter *iter,
11571155
int kvm_tdp_mmu_map(struct kvm_vcpu *vcpu, struct kvm_page_fault *fault)
11581156
{
11591157
struct kvm_mmu *mmu = vcpu->arch.mmu;
1158+
struct kvm *kvm = vcpu->kvm;
11601159
struct tdp_iter iter;
11611160
struct kvm_mmu_page *sp;
11621161
int ret;
@@ -1193,9 +1192,6 @@ int kvm_tdp_mmu_map(struct kvm_vcpu *vcpu, struct kvm_page_fault *fault)
11931192
}
11941193

11951194
if (!is_shadow_present_pte(iter.old_spte)) {
1196-
bool account_nx = fault->huge_page_disallowed &&
1197-
fault->req_level >= iter.level;
1198-
11991195
/*
12001196
* If SPTE has been frozen by another thread, just
12011197
* give up and retry, avoiding unnecessary page table
@@ -1207,10 +1203,19 @@ int kvm_tdp_mmu_map(struct kvm_vcpu *vcpu, struct kvm_page_fault *fault)
12071203
sp = tdp_mmu_alloc_sp(vcpu);
12081204
tdp_mmu_init_child_sp(sp, &iter);
12091205

1210-
if (tdp_mmu_link_sp(vcpu->kvm, &iter, sp, account_nx, true)) {
1206+
sp->nx_huge_page_disallowed = fault->huge_page_disallowed;
1207+
1208+
if (tdp_mmu_link_sp(kvm, &iter, sp, true)) {
12111209
tdp_mmu_free_sp(sp);
12121210
break;
12131211
}
1212+
1213+
if (fault->huge_page_disallowed &&
1214+
fault->req_level >= iter.level) {
1215+
spin_lock(&kvm->arch.tdp_mmu_pages_lock);
1216+
track_possible_nx_huge_page(kvm, sp);
1217+
spin_unlock(&kvm->arch.tdp_mmu_pages_lock);
1218+
}
12141219
}
12151220
}
12161221

@@ -1498,7 +1503,7 @@ static int tdp_mmu_split_huge_page(struct kvm *kvm, struct tdp_iter *iter,
14981503
* correctness standpoint since the translation will be the same either
14991504
* way.
15001505
*/
1501-
ret = tdp_mmu_link_sp(kvm, iter, sp, false, shared);
1506+
ret = tdp_mmu_link_sp(kvm, iter, sp, shared);
15021507
if (ret)
15031508
goto out;
15041509

0 commit comments

Comments
 (0)