KVM: x86: pass arguments on the page fault path via struct kvm_page_fault
Paolo Bonzini
maintainerv3 cover KVM: x86: pass arguments on the page fault path via struct kvm_page_fault
The current kvm page fault handlers passes around many arguments to the functions. To simplify those arguments and local variables, introduce a data structure, struct kvm_page_fault, to hold those arguments and variables. struct kvm_page_fault is allocated on stack on the caller of kvm fault handler, kvm_mmu_do_page_fault(), and passed around.
Later in the series, my patches are interleaved with David's work to add the memory slot to the struct and avoid repeated lookups. Along the way you will find some cleanups of functions with a ludicrous number of arguments, so that they use struct kvm_page_fault as much as possible or at least receive related information from a single argument. make_spte in particular goes from 11 to 10 arguments (yeah I know) despite gaining two for kvm_mmu_page and kvm_memory_slot.
This can be sometimes a bit debatable (for example struct kvm_mmu_page is used a little more on the TDP MMU paths), but overall I think the result is an improvement. For example the SET_SPTE_* constants go away, and they absolutely didn't belong in the TDP MMU. But if you disagree with some of the changes, please speak up loudly!
Testing: survives kvm-unit-tests on Intel with all of ept=0, ept=1 tdp_mmu=0, ept=1. Will do more before committing to it in kvm/next of course.
Paolo
David Matlack (5):
KVM: x86/mmu: Fold rmap_recycle into rmap_add
KVM: x86/mmu: Pass the memslot around via struct kvm_page_fault
KVM: x86/mmu: Avoid memslot lookup in page_fault_handle_page_track
KVM: x86/mmu: Avoid memslot lookup in rmap_add
KVM: x86/mmu: Avoid memslot lookup in make_spte and
mmu_try_to_unsync_pages
Paolo Bonzini (25):
KVM: MMU: pass unadulterated gpa to direct_page_fault
KVM: MMU: Introduce struct kvm_page_fault
KVM: MMU: change mmu->page_fault() arguments to kvm_page_fault
KVM: MMU: change direct_page_fault() arguments to kvm_page_fault
KVM: MMU: change page_fault_handle_page_track() arguments to
kvm_page_fault
KVM: MMU: change kvm_faultin_pfn() arguments to kvm_page_fault
KVM: MMU: change handle_abnormal_pfn() arguments to kvm_page_fault
KVM: MMU: change __direct_map() arguments to kvm_page_fault
KVM: MMU: change FNAME(fetch)() arguments to kvm_page_fault
KVM: MMU: change kvm_tdp_mmu_map() arguments to kvm_page_fault
KVM: MMU: change tdp_mmu_map_handle_target_level() arguments to
kvm_page_fault
KVM: MMU: change fast_page_fault() arguments to kvm_page_fault
KVM: MMU: change kvm_mmu_hugepage_adjust() arguments to kvm_page_fault
KVM: MMU: change disallowed_hugepage_adjust() arguments to
kvm_page_fault
KVM: MMU: change tracepoints arguments to kvm_page_fault
KVM: MMU: mark page dirty in make_spte
KVM: MMU: unify tdp_mmu_map_set_spte_atomic and
tdp_mmu_set_spte_atomic_no_dirty_log
KVM: MMU: inline set_spte in mmu_set_spte
KVM: MMU: inline set_spte in FNAME(sync_page)
KVM: MMU: clean up make_spte return value
KVM: MMU: remove unnecessary argument to mmu_set_spte
KVM: MMU: set ad_disabled in TDP MMU role
KVM: MMU: pass kvm_mmu_page struct to make_spte
KVM: MMU: pass struct kvm_page_fault to mmu_set_spte
KVM: MMU: make spte an in-out argument in make_spte
Sean Christopherson (1):
KVM: x86/mmu: Verify shadow walk doesn't terminate early in page
faults
arch/x86/include/asm/kvm_host.h | 4 +- arch/x86/include/asm/kvm_page_track.h | 4 +- arch/x86/kvm/mmu.h | 84 +++++- arch/x86/kvm/mmu/mmu.c | 408 +++++++++++--------------- arch/x86/kvm/mmu/mmu_internal.h | 22 +- arch/x86/kvm/mmu/mmutrace.h | 18 +- arch/x86/kvm/mmu/page_track.c | 6 +- arch/x86/kvm/mmu/paging_tmpl.h | 137 +++++---- arch/x86/kvm/mmu/spte.c | 29 +- arch/x86/kvm/mmu/spte.h | 14 +- arch/x86/kvm/mmu/tdp_mmu.c | 123 +++----- arch/x86/kvm/mmu/tdp_mmu.h | 4 +- 12 files changed, 390 insertions(+), 463 deletions(-)
Signature · 1 line
2.27.0
Paolo Bonzini
maintainerPATCH v3 1/31 KVM: MMU: pass unadulterated gpa to direct_page_fault
Do not bother removing the low bits of the gpa. This masking dates back to the very first commit of KVM but it is unnecessary, as exemplified by the other call in kvm_tdp_page_fault.
--- arch/x86/kvm/mmu/mmu.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
arch/x86/kvm/mmu/mmu.c +1 -1
@@ -4018,7 +4018,7 @@ static int nonpaging_page_fault(struct kvm_vcpu *vcpu, gpa_t gpa,pgprintk ("%s: gva %lx error %x\n" , __func__, gpa, error_code);/* This path builds a PAE pagetable, we can map 2mb pages at maximum. */ -return direct_page_fault (vcpu, gpa &PAGE_MASK , error_code, prefault, +return direct_page_fault (vcpu, gpa, error_code, prefault,PG_LEVEL_2M , false); }
Signature · 1 line
2.27.0
Paolo Bonzini
maintainerPATCH v3 2/31 KVM: MMU: Introduce struct kvm_page_fault
In reply to Paolo BonziniCreate a single structure for arguments that are passed from kvm_mmu_do_page_fault to the page fault handlers. Later the structure will grow to include various output parameters that are passed back to the next steps in the page fault handling.
--- arch/x86/kvm/mmu.h | 34 +++++++++++++++++++++++++++++++--- 1 file changed, 31 insertions(+), 3 deletions(-)
arch/x86/kvm/mmu.h +31 -3
@@ -114,17 +114,45 @@ static inline void kvm_mmu_load_pgd(struct kvm_vcpu *vcpu) vcpu->arch.mmu->shadow_root_level); } +struct kvm_page_fault { +/* arguments to kvm_mmu_do_page_fault. */ +const gpa_t addr; +const u32 error_code; +const bool prefault; + +/* Derived from error_code. */ +const bool exec; +const bool write; +const bool present; +const bool rsvd; +const bool user; + +/* Derived from mmu. */ +const bool is_tdp; +}; +int kvm_tdp_page_fault (struct kvm_vcpu *vcpu,gpa_t gpa,u32 error_code,bool prefault);static inline int kvm_mmu_do_page_fault (struct kvm_vcpu *vcpu,gpa_t cr2_or_gpa,u32 err,bool prefault) { +struct kvm_page_fault fault = { + .addr = cr2_or_gpa, + .error_code = err, + .exec = err &PFERR_FETCH_MASK , + .write = err &PFERR_WRITE_MASK , + .present = err &PFERR_PRESENT_MASK , + .rsvd = err &PFERR_RSVD_MASK , + .user = err &PFERR_USER_MASK , + .prefault = prefault, + .is_tdp =likely (vcpu->arch.mmu->page_fault == kvm_tdp_page_fault), + };#ifdef CONFIG_RETPOLINE -if (likely (vcpu->arch.mmu->page_fault == kvm_tdp_page_fault)) -return kvm_tdp_page_fault (vcpu, cr2_or_gpa, err, prefault); +if (fault.is_tdp) +return kvm_tdp_page_fault (vcpu, fault.addr, fault.error_code, fault.prefault);#endif -return vcpu->arch.mmu->page_fault (vcpu, cr2_or_gpa, err, prefault); +return vcpu->arch.mmu->page_fault (vcpu, fault.addr, fault.error_code, fault.prefault); }/*
Signature · 1 line
2.27.0
Re: [PATCH v3 02/31] KVM: MMU: Introduce struct kvm_page_fault
Quoted 17 lines · Paolo Bonzini
Create a single structure for arguments that are passed from
kvm_mmu_do_page_faultto the page fault handlers. Later the structure will grow to include various output parameters that are passed back to the next steps in the page fault handling.Suggested-by Isaku Yamahata <[email protected]>Signed-off-by Paolo Bonzini <[email protected]>--- arch/x86/kvm/mmu.h | 34 +++++++++++++++++++++++++++++++--- 1 file changed, 31 insertions(+), 3 deletions(-)arch/x86/kvm/mmu.h +0
-0
arch/x86/kvm/mmu.h static inline void kvm_mmu_load_pgd(struct kvm_vcpu *vcpu) · line 121
Quoted 8 lines
arch/x86/kvm/mmu.h +5
-0@@ -114,17 +114,45 @@ static inline void kvm_mmu_load_pgd(struct kvm_vcpu *vcpu) vcpu->arch.mmu->shadow_root_level); } +struct kvm_page_fault { +/* arguments to kvm_mmu_do_page_fault. */ +const gpa_t addr; +const u32 error_code; +const bool prefault;
This is somewhat tangential to your change but... I notice KVM uses "prefetch" and "prefault" interchangably. If we changed prefault to prefetch here and in kvm_mmu_do_page_fault then that would make the naming consistent throughout KVM ("prefetch" for everything).
Quoted 44 lines
arch/x86/kvm/mmu.h +26
-3+ +/* Derived from error_code. */ +const bool exec; +const bool write; +const bool present; +const bool rsvd; +const bool user; + +/* Derived from mmu. */ +const bool is_tdp; +}; +int kvm_tdp_page_fault (struct kvm_vcpu *vcpu,gpa_t gpa,u32 error_code,bool prefault);static inline int kvm_mmu_do_page_fault (struct kvm_vcpu *vcpu,gpa_t cr2_or_gpa,u32 err,bool prefault) { +struct kvm_page_fault fault = { + .addr = cr2_or_gpa, + .error_code = err, + .exec = err &PFERR_FETCH_MASK , + .write = err &PFERR_WRITE_MASK , + .present = err &PFERR_PRESENT_MASK , + .rsvd = err &PFERR_RSVD_MASK , + .user = err &PFERR_USER_MASK , + .prefault = prefault, + .is_tdp =likely (vcpu->arch.mmu->page_fault == kvm_tdp_page_fault), + };#ifdef CONFIG_RETPOLINE -if (likely (vcpu->arch.mmu->page_fault == kvm_tdp_page_fault)) -return kvm_tdp_page_fault (vcpu, cr2_or_gpa, err, prefault); +if (fault.is_tdp) +return kvm_tdp_page_fault (vcpu, fault.addr, fault.error_code, fault.prefault);#endif -return vcpu->arch.mmu->page_fault (vcpu, cr2_or_gpa, err, prefault); +return vcpu->arch.mmu->page_fault (vcpu, fault.addr, fault.error_code, fault.prefault); }/* Signature · 1 line
2.27.0
Paolo Bonzini
maintainerRe: [PATCH v3 02/31] KVM: MMU: Introduce struct kvm_page_fault
Quoted 10 lines · David Matlack
arch/x86/kvm/mmu.h static inline void kvm_mmu_load_pgd(struct kvm_vcpu *vcpu) · line 121
Quoted 5 lines
arch/x86/kvm/mmu.h +5
-0+struct kvm_page_fault { +/* arguments to kvm_mmu_do_page_fault. */ +const gpa_t addr; +const u32 error_code; +const bool prefault;This is somewhat tangential to your change but... I notice KVM uses "prefetch" and "prefault" interchangably. If we changed prefault to prefetch here and in
kvm_mmu_do_page_faultthen that would make the naming consistent throughout KVM ("prefetch" for everything).
Sounds good.
Paolo
Paolo Bonzini
maintainerPATCH v3 3/31 KVM: MMU: change mmu->page_fault() arguments to kvm_page_fault
In reply to Paolo BonziniPass struct kvm_page_fault to mmu->page_fault() instead of extracting the arguments from the struct. FNAME(page_fault) can use the precomputed bools from the error code.
--- arch/x86/include/asm/kvm_host.h | 4 ++-- arch/x86/kvm/mmu.h | 7 +++---- arch/x86/kvm/mmu/mmu.c | 15 ++++++++------- arch/x86/kvm/mmu/paging_tmpl.h | 22 +++++++++++----------- 4 files changed, 24 insertions(+), 24 deletions(-)
arch/x86/include/asm/kvm_host.h +2 -2
@@ -407,6 +407,7 @@ struct kvm_mmu_root_info {#define KVM_HAVE_MMU_RWLOCK struct kvm_mmu_page ; +struct kvm_page_fault ;/* * x86 supports 4 paging modes (5-level 64-bit, 4-level 64-bit, 3-level 32-bit,
@@ -416,8 +417,7 @@ struct kvm_mmu_page;struct kvm_mmu {unsigned long (*get_guest_pgd)(struct kvm_vcpu *vcpu);u64 (*get_pdptr)(struct kvm_vcpu *vcpu,int index); -int (*page_fault)(struct kvm_vcpu *vcpu,gpa_t cr2_or_gpa,u32 err, -bool prefault); +int (*page_fault)(struct kvm_vcpu *vcpu,struct kvm_page_fault *fault);void (*inject_page_fault)(struct kvm_vcpu *vcpu,struct x86_exception *fault); gpa_t (*gva_to_gpa)(struct kvm_vcpu *vcpu, gpa_t gva_or_gpa,
arch/x86/kvm/mmu.h +3 -4
@@ -131,8 +131,7 @@ struct kvm_page_fault {const bool is_tdp; }; -int kvm_tdp_page_fault (struct kvm_vcpu *vcpu,gpa_t gpa,u32 error_code, -bool prefault); +int kvm_tdp_page_fault (struct kvm_vcpu *vcpu,struct kvm_page_fault *fault);static inline int kvm_mmu_do_page_fault (struct kvm_vcpu *vcpu,gpa_t cr2_or_gpa,u32 err,bool prefault)
@@ -150,9 +149,9 @@ static inline int kvm_mmu_do_page_fault(struct kvm_vcpu *vcpu, gpa_t cr2_or_gpa, };#ifdef CONFIG_RETPOLINE if (fault.is_tdp) -return kvm_tdp_page_fault (vcpu, fault.addr, fault.error_code, fault.prefault); +return kvm_tdp_page_fault (vcpu, &fault);#endif -return vcpu->arch.mmu->page_fault (vcpu, fault.addr, fault.error_code, fault.prefault); +return vcpu->arch.mmu->page_fault (vcpu, &fault); }/*
arch/x86/kvm/mmu/mmu.c +8 -7
@@ -4012,13 +4012,14 @@ static int direct_page_fault(struct kvm_vcpu *vcpu, gpa_t gpa, u32 error_code,return r; } -static int nonpaging_page_fault (struct kvm_vcpu *vcpu,gpa_t gpa, -u32 error_code,bool prefault) +static int nonpaging_page_fault (struct kvm_vcpu *vcpu, +struct kvm_page_fault *fault) {pgprintk ("%s: gva %lx error %x\n" , __func__, gpa, error_code);/* This path builds a PAE pagetable, we can map 2mb pages at maximum. */ -return direct_page_fault (vcpu, gpa, error_code, prefault, +return direct_page_fault (vcpu, fault->addr, + fault->error_code, fault->prefault,PG_LEVEL_2M , false); }
@@ -4055,10 +4056,10 @@ int kvm_handle_page_fault(struct kvm_vcpu *vcpu, u64 error_code, }EXPORT_SYMBOL_GPL (kvm_handle_page_fault); -int kvm_tdp_page_fault (struct kvm_vcpu *vcpu,gpa_t gpa,u32 error_code, -bool prefault) +int kvm_tdp_page_fault (struct kvm_vcpu *vcpu,struct kvm_page_fault *fault) {int max_level; +gpa_t gpa = fault->addr; for (max_level = KVM_MAX_HUGEPAGE_LEVEL; max_level > PG_LEVEL_4K;
@@ -4070,8 +4071,8 @@ int kvm_tdp_page_fault(struct kvm_vcpu *vcpu, gpa_t gpa, u32 error_code,break ; } -return direct_page_fault (vcpu, gpa, error_code, prefault, - max_level, true); +return direct_page_fault (vcpu, gpa, fault->error_code, + fault->prefault, max_level, true); }static void nonpaging_init_context (struct kvm_mmu *context)
arch/x86/kvm/mmu/paging_tmpl.h +11 -11
@@ -833,11 +833,10 @@ FNAME(is_self_change_mapping)(struct kvm_vcpu *vcpu,* Returns: 1 if we need to emulate the instruction, 0 otherwise, or * a negative value on error. */ -static int FNAME (page_fault )(struct kvm_vcpu *vcpu,gpa_t addr,u32 error_code, -bool prefault) +static int FNAME (page_fault )(struct kvm_vcpu *vcpu,struct kvm_page_fault *fault) { -bool write_fault = error_code &PFERR_WRITE_MASK ; -bool user_fault = error_code &PFERR_USER_MASK ; +gpa_t addr = fault->addr; +u32 error_code = fault->error_code;struct guest_walker walker;int r;kvm_pfn_t pfn;
@@ -847,6 +846,7 @@ static int FNAME(page_fault)(struct kvm_vcpu *vcpu, gpa_t addr, u32 error_code,int max_level;pgprintk ("%s: addr %lx err %x\n" , __func__, addr, error_code); +WARN_ON_ONCE (fault->is_tdp);/* * If PFEC.RSVD is set, this is a shadow page fault.
@@ -864,7 +864,7 @@ static int FNAME(page_fault)(struct kvm_vcpu *vcpu, gpa_t addr, u32 error_code,*/ if (!r) {pgprintk ("%s: guest page fault\n" , __func__); -if (!prefault) +if (!fault->prefault)kvm_inject_emulated_page_fault (vcpu, &walker.fault);return RET_PF_RETRY ;
@@ -882,7 +882,7 @@ static int FNAME(page_fault)(struct kvm_vcpu *vcpu, gpa_t addr, u32 error_code, vcpu->arch.write_fault_to_shadow_pgtable = false; is_self_change_mapping =FNAME (is_self_change_mapping)(vcpu, - &walker, user_fault, &vcpu->arch.write_fault_to_shadow_pgtable); + &walker, fault->user, &vcpu->arch.write_fault_to_shadow_pgtable);if (is_self_change_mapping) max_level =PG_LEVEL_4K ;
@@ -892,8 +892,8 @@ static int FNAME(page_fault)(struct kvm_vcpu *vcpu, gpa_t addr, u32 error_code, mmu_seq = vcpu->kvm->mmu_notifier_seq;smp_rmb (); -if (kvm_faultin_pfn (vcpu, prefault, walker.gfn, addr, &pfn, &hva, - write_fault, &map_writable, &r)) +if (kvm_faultin_pfn (vcpu, fault->prefault, walker.gfn, addr, &pfn, &hva, + fault->write, &map_writable, &r))return r;if (handle_abnormal_pfn (vcpu, addr, walker.gfn, pfn, walker.pte_access, &r))
@@ -903,8 +903,8 @@ static int FNAME(page_fault)(struct kvm_vcpu *vcpu, gpa_t addr, u32 error_code,* Do not change pte_access if the pfn is a mmio page, otherwise * we will cache the incorrect access into mmio spte. */ -if (write_fault && !(walker.pte_access &ACC_WRITE_MASK ) && - !is_cr0_wp (vcpu->arch.mmu) && !user_fault && !is_noslot_pfn (pfn)) { +if (fault->write && !(walker.pte_access &ACC_WRITE_MASK ) && + !is_cr0_wp (vcpu->arch.mmu) && !fault->user && !is_noslot_pfn (pfn)) { walker.pte_access |=ACC_WRITE_MASK ; walker.pte_access &= ~ACC_USER_MASK ;
@@ -928,7 +928,7 @@ static int FNAME(page_fault)(struct kvm_vcpu *vcpu, gpa_t addr, u32 error_code,if (r) goto out_unlock; r =FNAME (fetch)(vcpu, addr, &walker, error_code, max_level, pfn, - map_writable, prefault); + map_writable, fault->prefault);kvm_mmu_audit (vcpu,AUDIT_POST_PAGE_FAULT ); out_unlock:
Signature · 1 line
2.27.0
Paolo Bonzini
maintainerPATCH v3 4/31 KVM: MMU: change direct_page_fault() arguments to kvm_page_fault
In reply to Paolo BonziniAdd fields to struct kvm_page_fault corresponding to the arguments of direct_page_fault(). The fields are initialized in the callers, and direct_page_fault() receives a struct kvm_page_fault instead of having to extract the arguments out of it.
Also adjust FNAME(page_fault) to store the max_level in struct kvm_page_fault, to keep it similar to the direct map path.
--- arch/x86/kvm/mmu.h | 5 ++++ arch/x86/kvm/mmu/mmu.c | 43 +++++++++++++++------------------- arch/x86/kvm/mmu/paging_tmpl.h | 7 +++--- 3 files changed, 27 insertions(+), 28 deletions(-)
arch/x86/kvm/mmu.h +5 -0
@@ -129,6 +129,9 @@ struct kvm_page_fault {/* Derived from mmu. */ const bool is_tdp; + +/* Input to FNAME(fetch), __direct_map and kvm_tdp_mmu_map. */ +u8 max_level; };int kvm_tdp_page_fault (struct kvm_vcpu *vcpu,struct kvm_page_fault *fault);
@@ -146,6 +149,8 @@ static inline int kvm_mmu_do_page_fault(struct kvm_vcpu *vcpu, gpa_t cr2_or_gpa, .user = err & PFERR_USER_MASK, .prefault = prefault, .is_tdp = likely(vcpu->arch.mmu->page_fault == kvm_tdp_page_fault), + + .max_level = KVM_MAX_HUGEPAGE_LEVEL, };#ifdef CONFIG_RETPOLINE if (fault.is_tdp)
arch/x86/kvm/mmu/mmu.c +19 -24
85 lines not on this page · raw message
arch/x86/kvm/mmu/paging_tmpl.h +3 -4
@@ -843,7 +843,6 @@ static int FNAME(page_fault)(struct kvm_vcpu *vcpu, struct kvm_page_fault *faulthva_t hva;unsigned long mmu_seq;bool map_writable, is_self_change_mapping; -int max_level;pgprintk ("%s: addr %lx err %x\n" , __func__, addr, error_code);WARN_ON_ONCE (fault->is_tdp);
@@ -885,9 +884,9 @@ static int FNAME(page_fault)(struct kvm_vcpu *vcpu, struct kvm_page_fault *fault &walker, fault->user, &vcpu->arch.write_fault_to_shadow_pgtable);if (is_self_change_mapping) - max_level =PG_LEVEL_4K ; + fault->max_level =PG_LEVEL_4K ;else - max_level = walker.level; + fault->max_level = walker.level; mmu_seq = vcpu->kvm->mmu_notifier_seq;smp_rmb ();
@@ -927,7 +926,7 @@ static int FNAME(page_fault)(struct kvm_vcpu *vcpu, struct kvm_page_fault *fault r =make_mmu_pages_available (vcpu);if (r) goto out_unlock; - r =FNAME (fetch)(vcpu, addr, &walker, error_code, max_level, pfn, + r =FNAME (fetch)(vcpu, addr, &walker, error_code, fault->max_level, pfn, map_writable, fault->prefault);kvm_mmu_audit (vcpu,AUDIT_POST_PAGE_FAULT );
Signature · 1 line
2.27.0
Paolo Bonzini
maintainerPATCH v3 5/31 KVM: MMU: change page_fault_handle_page_track() arguments to kvm_page_fault
In reply to Paolo BonziniAdd fields to struct kvm_page_fault corresponding to the arguments of page_fault_handle_page_track(). The fields are initialized in the callers, and page_fault_handle_page_track() receives a struct kvm_page_fault instead of having to extract the arguments out of it.
--- arch/x86/kvm/mmu.h | 3 +++ arch/x86/kvm/mmu/mmu.c | 18 +++++++++--------- arch/x86/kvm/mmu/paging_tmpl.h | 7 ++++--- 3 files changed, 16 insertions(+), 12 deletions(-)
arch/x86/kvm/mmu.h +3 -0
@@ -132,6 +132,9 @@ struct kvm_page_fault {/* Input to FNAME(fetch), __direct_map and kvm_tdp_mmu_map. */ u8 max_level; + +/* Shifted addr, or result of guest page table walk if addr is a gva. */ +gfn_t gfn; };int kvm_tdp_page_fault (struct kvm_vcpu *vcpu,struct kvm_page_fault *fault);
arch/x86/kvm/mmu/mmu.c +9 -9
@@ -3846,20 +3846,19 @@ static int handle_mmio_page_fault(struct kvm_vcpu *vcpu, u64 addr, bool direct) }static bool page_fault_handle_page_track (struct kvm_vcpu *vcpu, -u32 error_code,gfn_t gfn) +struct kvm_page_fault *fault) { -if (unlikely (error_code &PFERR_RSVD_MASK )) +if (unlikely (fault->rsvd))return false; -if (!(error_code &PFERR_PRESENT_MASK ) || - !(error_code &PFERR_WRITE_MASK )) +if (!fault->present || !fault->write)return false;/* * guest is writing the page which is write tracked which can * not be fixed by page fault handler. */ -if (kvm_page_track_is_active (vcpu, gfn,KVM_PAGE_TRACK_WRITE )) +if (kvm_page_track_is_active (vcpu, fault->gfn,KVM_PAGE_TRACK_WRITE ))return true;return false;
@@ -3956,13 +3955,13 @@ static int direct_page_fault(struct kvm_vcpu *vcpu, struct kvm_page_fault *faultbool is_tdp_mmu_fault =is_tdp_mmu (vcpu->arch.mmu);bool map_writable; -gfn_t gfn = gpa >>PAGE_SHIFT ;unsigned long mmu_seq;kvm_pfn_t pfn;hva_t hva;int r; -if (page_fault_handle_page_track (vcpu, error_code, gfn)) + fault->gfn = gpa >>PAGE_SHIFT ; +if (page_fault_handle_page_track (vcpu, fault))return RET_PF_EMULATE ; r =fast_page_fault (vcpu, gpa, error_code);
@@ -3976,11 +3975,12 @@ static int direct_page_fault(struct kvm_vcpu *vcpu, struct kvm_page_fault *fault mmu_seq = vcpu->kvm->mmu_notifier_seq;smp_rmb (); -if (kvm_faultin_pfn (vcpu, fault->prefault, gfn, gpa, &pfn, &hva, +if (kvm_faultin_pfn (vcpu, fault->prefault, fault->gfn, gpa, &pfn, &hva, fault->write, &map_writable, &r))return r; -if (handle_abnormal_pfn (vcpu, fault->is_tdp ?0 : gpa, gfn, pfn,ACC_ALL , &r)) +if (handle_abnormal_pfn (vcpu, fault->is_tdp ?0 : gpa, + fault->gfn, pfn,ACC_ALL , &r))return r; r =RET_PF_RETRY ;
arch/x86/kvm/mmu/paging_tmpl.h +4 -3
@@ -869,7 +869,8 @@ static int FNAME(page_fault)(struct kvm_vcpu *vcpu, struct kvm_page_fault *faultreturn RET_PF_RETRY ; } -if (page_fault_handle_page_track (vcpu, error_code, walker.gfn)) { + fault->gfn = walker.gfn; +if (page_fault_handle_page_track (vcpu, fault)) {shadow_page_table_clear_flood (vcpu, addr);return RET_PF_EMULATE ; }
@@ -891,11 +892,11 @@ static int FNAME(page_fault)(struct kvm_vcpu *vcpu, struct kvm_page_fault *fault mmu_seq = vcpu->kvm->mmu_notifier_seq;smp_rmb (); -if (kvm_faultin_pfn (vcpu, fault->prefault, walker.gfn, addr, &pfn, &hva, +if (kvm_faultin_pfn (vcpu, fault->prefault, fault->gfn, addr, &pfn, &hva, fault->write, &map_writable, &r))return r; -if (handle_abnormal_pfn (vcpu, addr, walker.gfn, pfn, walker.pte_access, &r)) +if (handle_abnormal_pfn (vcpu, addr, fault->gfn, pfn, walker.pte_access, &r))return r;/*
Signature · 1 line
2.27.0
Paolo Bonzini
maintainerPATCH v3 6/31 KVM: MMU: change kvm_faultin_pfn() arguments to kvm_page_fault
In reply to Paolo BonziniAdd fields to struct kvm_page_fault corresponding to outputs of kvm_faultin_pfn(). For now they have to be extracted again from struct kvm_page_fault in the subsequent steps, but this is temporary until other functions in the chain are switched over as well.
--- arch/x86/kvm/mmu.h | 5 ++++ arch/x86/kvm/mmu/mmu.c | 50 ++++++++++++++++------------------ arch/x86/kvm/mmu/paging_tmpl.h | 19 ++++++------- 3 files changed, 36 insertions(+), 38 deletions(-)
arch/x86/kvm/mmu.h +5 -0
@@ -135,6 +135,11 @@ struct kvm_page_fault {/* Shifted addr, or result of guest page table walk if addr is a gva. */ gfn_t gfn; + +/* Outputs of kvm_faultin_pfn. */ +kvm_pfn_t pfn; +hva_t hva; +bool map_writable; };int kvm_tdp_page_fault (struct kvm_vcpu *vcpu,struct kvm_page_fault *fault);
arch/x86/kvm/mmu/mmu.c +23 -27
113 lines not on this page · raw message
arch/x86/kvm/mmu/paging_tmpl.h +8 -11
@@ -839,10 +839,8 @@ static int FNAME(page_fault)(struct kvm_vcpu *vcpu, struct kvm_page_fault *faultu32 error_code = fault->error_code;struct guest_walker walker;int r; -kvm_pfn_t pfn; -hva_t hva;unsigned long mmu_seq; -bool map_writable, is_self_change_mapping; +bool is_self_change_mapping;pgprintk ("%s: addr %lx err %x\n" , __func__, addr, error_code);WARN_ON_ONCE (fault->is_tdp);
@@ -892,11 +890,10 @@ static int FNAME(page_fault)(struct kvm_vcpu *vcpu, struct kvm_page_fault *fault mmu_seq = vcpu->kvm->mmu_notifier_seq;smp_rmb (); -if (kvm_faultin_pfn (vcpu, fault->prefault, fault->gfn, addr, &pfn, &hva, - fault->write, &map_writable, &r)) +if (kvm_faultin_pfn (vcpu, fault, &r))return r; -if (handle_abnormal_pfn (vcpu, addr, fault->gfn, pfn, walker.pte_access, &r)) +if (handle_abnormal_pfn (vcpu, addr, fault->gfn, fault->pfn, walker.pte_access, &r))return r;/*
@@ -904,7 +901,7 @@ static int FNAME(page_fault)(struct kvm_vcpu *vcpu, struct kvm_page_fault *fault* we will cache the incorrect access into mmio spte. */ if (fault->write && !(walker.pte_access &ACC_WRITE_MASK ) && - !is_cr0_wp (vcpu->arch.mmu) && !fault->user && !is_noslot_pfn (pfn)) { + !is_cr0_wp (vcpu->arch.mmu) && !fault->user && !is_noslot_pfn (fault->pfn)) { walker.pte_access |=ACC_WRITE_MASK ; walker.pte_access &= ~ACC_USER_MASK ;
@@ -920,20 +917,20 @@ static int FNAME(page_fault)(struct kvm_vcpu *vcpu, struct kvm_page_fault *fault r =RET_PF_RETRY ;write_lock (&vcpu->kvm->mmu_lock); -if (!is_noslot_pfn (pfn) &&mmu_notifier_retry_hva (vcpu->kvm, mmu_seq, hva)) +if (!is_noslot_pfn (fault->pfn) &&mmu_notifier_retry_hva (vcpu->kvm, mmu_seq, fault->hva)) goto out_unlock;kvm_mmu_audit (vcpu,AUDIT_PRE_PAGE_FAULT ); r =make_mmu_pages_available (vcpu);if (r) goto out_unlock; - r =FNAME (fetch)(vcpu, addr, &walker, error_code, fault->max_level, pfn, - map_writable, fault->prefault); + r =FNAME (fetch)(vcpu, addr, &walker, error_code, fault->max_level, fault->pfn, + fault->map_writable, fault->prefault);kvm_mmu_audit (vcpu,AUDIT_POST_PAGE_FAULT ); out_unlock:write_unlock (&vcpu->kvm->mmu_lock); -kvm_release_pfn_clean (pfn); +kvm_release_pfn_clean (fault->pfn);return r; }
Signature · 1 line
2.27.0
Paolo Bonzini
maintainerPATCH v3 7/31 KVM: MMU: change handle_abnormal_pfn() arguments to kvm_page_fault
In reply to Paolo BonziniPass struct kvm_page_fault to handle_abnormal_pfn() instead of extracting the arguments from the struct.
--- arch/x86/kvm/mmu/mmu.c | 18 +++++++++--------- arch/x86/kvm/mmu/paging_tmpl.h | 2 +- 2 files changed, 10 insertions(+), 10 deletions(-)
arch/x86/kvm/mmu/mmu.c +9 -9
@@ -3060,18 +3060,19 @@ static int kvm_handle_bad_page(struct kvm_vcpu *vcpu, gfn_t gfn, kvm_pfn_t pfn)return -EFAULT ; } -static bool handle_abnormal_pfn (struct kvm_vcpu *vcpu,gva_t gva,gfn_t gfn, -kvm_pfn_t pfn,unsigned int access, -int *ret_val) +static bool handle_abnormal_pfn (struct kvm_vcpu *vcpu,struct kvm_page_fault *fault, +unsigned int access,int *ret_val) {/* The pfn is invalid, report the error! */ -if (unlikely (is_error_pfn (pfn))) { - *ret_val =kvm_handle_bad_page (vcpu, gfn, pfn); +if (unlikely (is_error_pfn (fault->pfn))) { + *ret_val =kvm_handle_bad_page (vcpu, fault->gfn, fault->pfn);return true; } -if (unlikely (is_noslot_pfn (pfn))) { -vcpu_cache_mmio_info (vcpu, gva, gfn, +if (unlikely (is_noslot_pfn (fault->pfn))) { +gva_t gva = fault->is_tdp ?0 : fault->addr; + +vcpu_cache_mmio_info (vcpu, gva, fault->gfn, access & shadow_mmio_access_mask);/* * If MMIO caching is disabled, emulate immediately without
@@ -3975,8 +3976,7 @@ static int direct_page_fault(struct kvm_vcpu *vcpu, struct kvm_page_fault *faultif (kvm_faultin_pfn (vcpu, fault, &r))return r; -if (handle_abnormal_pfn (vcpu, fault->is_tdp ?0 : gpa, - fault->gfn, fault->pfn,ACC_ALL , &r)) +if (handle_abnormal_pfn (vcpu, fault,ACC_ALL , &r))return r; r =RET_PF_RETRY ;
arch/x86/kvm/mmu/paging_tmpl.h +1 -1
@@ -893,7 +893,7 @@ static int FNAME(page_fault)(struct kvm_vcpu *vcpu, struct kvm_page_fault *faultif (kvm_faultin_pfn (vcpu, fault, &r))return r; -if (handle_abnormal_pfn (vcpu, addr, fault->gfn, fault->pfn, walker.pte_access, &r)) +if (handle_abnormal_pfn (vcpu, fault, walker.pte_access, &r))return r;/*
Signature · 1 line
2.27.0
Paolo Bonzini
maintainerPATCH v3 8/31 KVM: MMU: change __direct_map() arguments to kvm_page_fault
In reply to Paolo BonziniPass struct kvm_page_fault to __direct_map() instead of extracting the arguments from the struct.
--- arch/x86/kvm/mmu/mmu.c | 32 +++++++++++++------------------- 1 file changed, 13 insertions(+), 19 deletions(-)
arch/x86/kvm/mmu/mmu.c +13 -19
@@ -2982,34 +2982,29 @@ void disallowed_hugepage_adjust(u64 spte, gfn_t gfn, int cur_level, } } -static int __direct_map (struct kvm_vcpu *vcpu,gpa_t gpa,u32 error_code, -int map_writable,int max_level,kvm_pfn_t pfn, -bool prefault,bool is_tdp) +static int __direct_map (struct kvm_vcpu *vcpu,struct kvm_page_fault *fault) {bool nx_huge_page_workaround_enabled =is_nx_huge_page_enabled (); -bool write = error_code &PFERR_WRITE_MASK ; -bool exec = error_code &PFERR_FETCH_MASK ; -bool huge_page_disallowed = exec && nx_huge_page_workaround_enabled; +bool huge_page_disallowed = fault->exec && nx_huge_page_workaround_enabled;struct kvm_shadow_walk_iterator it;struct kvm_mmu_page *sp;int level, req_level, ret; -gfn_t gfn = gpa >>PAGE_SHIFT ; -gfn_t base_gfn = gfn; +gfn_t base_gfn = fault->gfn; - level =kvm_mmu_hugepage_adjust (vcpu, gfn, max_level, &pfn, + level =kvm_mmu_hugepage_adjust (vcpu, fault->gfn, fault->max_level, &fault->pfn, huge_page_disallowed, &req_level); -trace_kvm_mmu_spte_requested (gpa, level, pfn); -for_each_shadow_entry (vcpu, gpa, it) { +trace_kvm_mmu_spte_requested (fault->addr, level, fault->pfn); +for_each_shadow_entry (vcpu, fault->addr, it) {/* * We cannot overwrite existing page tables with an NX * large page, as the leaf could be executable. */ if (nx_huge_page_workaround_enabled) -disallowed_hugepage_adjust (*it.sptep, gfn, it.level, - &pfn, &level); +disallowed_hugepage_adjust (*it.sptep, fault->gfn, it.level, + &fault->pfn, &level); - base_gfn = gfn & ~(KVM_PAGES_PER_HPAGE (it.level) -1 ); + base_gfn = fault->gfn & ~(KVM_PAGES_PER_HPAGE (it.level) -1 );if (it.level == level)break ;
@@ -3021,14 +3016,14 @@ static int __direct_map(struct kvm_vcpu *vcpu, gpa_t gpa, u32 error_code, it.level - 1, true, ACC_ALL);link_shadow_page (vcpu, it.sptep, sp); -if (is_tdp && huge_page_disallowed && +if (fault->is_tdp && huge_page_disallowed && req_level >= it.level)account_huge_nx_page (vcpu->kvm, sp); } ret =mmu_set_spte (vcpu, it.sptep,ACC_ALL , - write, level, base_gfn, pfn, prefault, - map_writable); + fault->write, level, base_gfn, fault->pfn, + fault->prefault, fault->map_writable);if (ret ==RET_PF_SPURIOUS )return ret;
@@ -3996,8 +3991,7 @@ static int direct_page_fault(struct kvm_vcpu *vcpu, struct kvm_page_fault *fault r =kvm_tdp_mmu_map (vcpu, gpa, error_code, fault->map_writable, fault->max_level, fault->pfn, fault->prefault); else - r =__direct_map (vcpu, gpa, error_code, fault->map_writable, fault->max_level, - fault->pfn, fault->prefault, fault->is_tdp); + r =__direct_map (vcpu, fault); out_unlock:if (is_tdp_mmu_fault)
Signature · 1 line
2.27.0
Paolo Bonzini
maintainerPATCH v3 9/31 KVM: MMU: change FNAME(fetch)() arguments to kvm_page_fault
In reply to Paolo BonziniPass struct kvm_page_fault to FNAME(fetch)() instead of extracting the arguments from the struct.
--- arch/x86/kvm/mmu/paging_tmpl.h | 52 ++++++++++++++-------------------- 1 file changed, 22 insertions(+), 30 deletions(-)
arch/x86/kvm/mmu/paging_tmpl.h +22 -30
@@ -655,21 +655,18 @@ static void FNAME(pte_prefetch)(struct kvm_vcpu *vcpu, struct guest_walker *gw,* If the guest tries to write a write-protected page, we need to * emulate this operation, return 1 to indicate this case. */ -static int FNAME (fetch )(struct kvm_vcpu *vcpu,gpa_t addr, -struct guest_walker *gw,u32 error_code, -int max_level,kvm_pfn_t pfn,bool map_writable, -bool prefault) +static int FNAME (fetch )(struct kvm_vcpu *vcpu,struct kvm_page_fault *fault, +struct guest_walker *gw) {bool nx_huge_page_workaround_enabled =is_nx_huge_page_enabled (); -bool write_fault = error_code &PFERR_WRITE_MASK ; -bool exec = error_code &PFERR_FETCH_MASK ; -bool huge_page_disallowed = exec && nx_huge_page_workaround_enabled; +bool huge_page_disallowed = fault->exec && nx_huge_page_workaround_enabled;struct kvm_mmu_page *sp =NULL ;struct kvm_shadow_walk_iterator it;unsigned int direct_access, access;int top_level, level, req_level, ret; -gfn_t base_gfn = gw->gfn; +gfn_t base_gfn = fault->gfn; +WARN_ON_ONCE (gw->gfn != base_gfn); direct_access = gw->pte_access; top_level = vcpu->arch.mmu->root_level;
@@ -687,7 +684,7 @@ static int FNAME(fetch)(struct kvm_vcpu *vcpu, gpa_t addr,if (WARN_ON (!VALID_PAGE (vcpu->arch.mmu->root_hpa))) goto out_gpte_changed; -for (shadow_walk_init (&it, vcpu, addr); +for (shadow_walk_init (&it, vcpu, fault->addr);shadow_walk_okay (&it) && it.level > gw->level;shadow_walk_next (&it)) {gfn_t table_gfn;
@@ -699,7 +696,7 @@ static int FNAME(fetch)(struct kvm_vcpu *vcpu, gpa_t addr,if (!is_shadow_present_pte (*it.sptep)) { table_gfn = gw->table_gfn[it.level -2 ]; access = gw->pt_access[it.level -2 ]; - sp =kvm_mmu_get_page (vcpu, table_gfn, addr, + sp =kvm_mmu_get_page (vcpu, table_gfn, fault->addr, it.level-1 , false, access);/* * We must synchronize the pagetable before linking it
@@ -733,10 +730,10 @@ static int FNAME(fetch)(struct kvm_vcpu *vcpu, gpa_t addr,link_shadow_page (vcpu, it.sptep, sp); } - level =kvm_mmu_hugepage_adjust (vcpu, gw->gfn, max_level, &pfn, + level =kvm_mmu_hugepage_adjust (vcpu, gw->gfn, fault->max_level, &fault->pfn, huge_page_disallowed, &req_level); -trace_kvm_mmu_spte_requested (addr, gw->level, pfn); +trace_kvm_mmu_spte_requested (fault->addr, gw->level, fault->pfn);for (;shadow_walk_okay (&it);shadow_walk_next (&it)) {clear_sp_write_flooding_count (it.sptep);
@@ -746,10 +743,10 @@ static int FNAME(fetch)(struct kvm_vcpu *vcpu, gpa_t addr,* large page, as the leaf could be executable. */ if (nx_huge_page_workaround_enabled) -disallowed_hugepage_adjust (*it.sptep, gw->gfn, it.level, - &pfn, &level); +disallowed_hugepage_adjust (*it.sptep, fault->gfn, it.level, + &fault->pfn, &level); - base_gfn = gw->gfn & ~(KVM_PAGES_PER_HPAGE (it.level) -1 ); + base_gfn = fault->gfn & ~(KVM_PAGES_PER_HPAGE (it.level) -1 );if (it.level == level)break ;
@@ -758,7 +755,7 @@ static int FNAME(fetch)(struct kvm_vcpu *vcpu, gpa_t addr,drop_large_spte (vcpu, it.sptep);if (!is_shadow_present_pte (*it.sptep)) { - sp =kvm_mmu_get_page (vcpu, base_gfn, addr, + sp =kvm_mmu_get_page (vcpu, base_gfn, fault->addr, it.level -1 , true, direct_access);link_shadow_page (vcpu, it.sptep, sp);if (huge_page_disallowed && req_level >= it.level)
@@ -766,8 +763,9 @@ static int FNAME(fetch)(struct kvm_vcpu *vcpu, gpa_t addr, } } - ret =mmu_set_spte (vcpu, it.sptep, gw->pte_access, write_fault, - it.level, base_gfn, pfn, prefault, map_writable); + ret =mmu_set_spte (vcpu, it.sptep, gw->pte_access, fault->write, + it.level, base_gfn, fault->pfn, fault->prefault, + fault->map_writable);if (ret ==RET_PF_SPURIOUS )return ret;
@@ -835,26 +833,21 @@ FNAME(is_self_change_mapping)(struct kvm_vcpu *vcpu,*/ static int FNAME (page_fault )(struct kvm_vcpu *vcpu,struct kvm_page_fault *fault) { -gpa_t addr = fault->addr; -u32 error_code = fault->error_code;struct guest_walker walker;int r;unsigned long mmu_seq;bool is_self_change_mapping; -pgprintk ("%s: addr %lx err %x\n" , __func__, addr, error_code); +pgprintk ("%s: addr %lx err %x\n" , __func__, fault->addr, fault->error_code);WARN_ON_ONCE (fault->is_tdp);/* +* Look up the guest pte for the faulting address. * If PFEC.RSVD is set, this is a shadow page fault. * The bit needs to be cleared before walking guest page tables. */ - error_code &= ~PFERR_RSVD_MASK ; - -/* -* Look up the guest pte for the faulting address. -*/ - r =FNAME (walk_addr)(&walker, vcpu, addr, error_code); + r =FNAME (walk_addr)(&walker, vcpu, fault->addr, + fault->error_code & ~PFERR_RSVD_MASK );/* * The page is not mapped by the guest. Let the guest handle it.
@@ -869,7 +862,7 @@ static int FNAME(page_fault)(struct kvm_vcpu *vcpu, struct kvm_page_fault *fault fault->gfn = walker.gfn;if (page_fault_handle_page_track (vcpu, fault)) { -shadow_page_table_clear_flood (vcpu, addr); +shadow_page_table_clear_flood (vcpu, fault->addr);return RET_PF_EMULATE ; }
@@ -924,8 +917,7 @@ static int FNAME(page_fault)(struct kvm_vcpu *vcpu, struct kvm_page_fault *fault r =make_mmu_pages_available (vcpu);if (r) goto out_unlock; - r =FNAME (fetch)(vcpu, addr, &walker, error_code, fault->max_level, fault->pfn, - fault->map_writable, fault->prefault); + r =FNAME (fetch)(vcpu, fault, &walker);kvm_mmu_audit (vcpu,AUDIT_POST_PAGE_FAULT ); out_unlock:
Signature · 1 line
2.27.0
Paolo Bonzini
maintainerPATCH v3 10/31 KVM: MMU: change kvm_tdp_mmu_map() arguments to kvm_page_fault
In reply to Paolo BonziniPass struct kvm_page_fault to kvm_tdp_mmu_map() instead of extracting the arguments from the struct.
--- arch/x86/kvm/mmu/mmu.c | 3 +-- arch/x86/kvm/mmu/tdp_mmu.c | 23 +++++++++-------------- arch/x86/kvm/mmu/tdp_mmu.h | 4 +--- 3 files changed, 11 insertions(+), 19 deletions(-)
arch/x86/kvm/mmu/mmu.c +1 -2
@@ -3988,8 +3988,7 @@ static int direct_page_fault(struct kvm_vcpu *vcpu, struct kvm_page_fault *fault goto out_unlock;if (is_tdp_mmu_fault) - r =kvm_tdp_mmu_map (vcpu, gpa, error_code, fault->map_writable, fault->max_level, - fault->pfn, fault->prefault); + r =kvm_tdp_mmu_map (vcpu, fault);else r =__direct_map (vcpu, fault);
arch/x86/kvm/mmu/tdp_mmu.c +9 -14
@@ -985,35 +985,30 @@ static int tdp_mmu_map_handle_target_level(struct kvm_vcpu *vcpu, int write,* Handle a TDP page fault (NPT/EPT violation/misconfiguration) by installing * page tables and SPTEs to translate the faulting guest physical address. */ -int kvm_tdp_mmu_map (struct kvm_vcpu *vcpu,gpa_t gpa,u32 error_code, -int map_writable,int max_level,kvm_pfn_t pfn, -bool prefault) +int kvm_tdp_mmu_map (struct kvm_vcpu *vcpu,struct kvm_page_fault *fault) {bool nx_huge_page_workaround_enabled =is_nx_huge_page_enabled (); -bool write = error_code &PFERR_WRITE_MASK ; -bool exec = error_code &PFERR_FETCH_MASK ; -bool huge_page_disallowed = exec && nx_huge_page_workaround_enabled; +bool huge_page_disallowed = fault->exec && nx_huge_page_workaround_enabled;struct kvm_mmu *mmu = vcpu->arch.mmu;struct tdp_iter iter;struct kvm_mmu_page *sp;u64 *child_pt;u64 new_spte;int ret; -gfn_t gfn = gpa >>PAGE_SHIFT ;int level;int req_level; - level =kvm_mmu_hugepage_adjust (vcpu, gfn, max_level, &pfn, + level =kvm_mmu_hugepage_adjust (vcpu, fault->gfn, fault->max_level, &fault->pfn, huge_page_disallowed, &req_level); -trace_kvm_mmu_spte_requested (gpa, level, pfn); +trace_kvm_mmu_spte_requested (fault->addr, level, fault->pfn);rcu_read_lock (); -tdp_mmu_for_each_pte (iter, mmu, gfn, gfn +1 ) { +tdp_mmu_for_each_pte (iter, mmu, fault->gfn, fault->gfn +1 ) {if (nx_huge_page_workaround_enabled) -disallowed_hugepage_adjust (iter.old_spte, gfn, - iter.level, &pfn, &level); +disallowed_hugepage_adjust (iter.old_spte, fault->gfn, + iter.level, &fault->pfn, &level);if (iter.level == level)break ;
@@ -1069,8 +1064,8 @@ int kvm_tdp_mmu_map(struct kvm_vcpu *vcpu, gpa_t gpa, u32 error_code,return RET_PF_RETRY ; } - ret =tdp_mmu_map_handle_target_level (vcpu, write, map_writable, &iter, - pfn, prefault); + ret =tdp_mmu_map_handle_target_level (vcpu, fault->write, fault->map_writable, &iter, + fault->pfn, fault->prefault);rcu_read_unlock ();return ret;
arch/x86/kvm/mmu/tdp_mmu.h +1 -3
@@ -48,9 +48,7 @@ void kvm_tdp_mmu_zap_all(struct kvm *kvm);void kvm_tdp_mmu_invalidate_all_roots (struct kvm *kvm);void kvm_tdp_mmu_zap_invalidated_roots (struct kvm *kvm); -int kvm_tdp_mmu_map (struct kvm_vcpu *vcpu,gpa_t gpa,u32 error_code, -int map_writable,int max_level,kvm_pfn_t pfn, -bool prefault); +int kvm_tdp_mmu_map (struct kvm_vcpu *vcpu,struct kvm_page_fault *fault);bool kvm_tdp_mmu_unmap_gfn_range (struct kvm *kvm,struct kvm_gfn_range *range,bool flush);
Signature · 1 line
2.27.0
Paolo Bonzini
maintainerPATCH v3 11/31 KVM: MMU: change tdp_mmu_map_handle_target_level() arguments to kvm_page_fault
In reply to Paolo BonziniPass struct kvm_page_fault to tdp_mmu_map_handle_target_level() instead of extracting the arguments from the struct.
--- arch/x86/kvm/mmu/tdp_mmu.c | 18 ++++++++---------- 1 file changed, 8 insertions(+), 10 deletions(-)
arch/x86/kvm/mmu/tdp_mmu.c +8 -10
@@ -929,21 +929,20 @@ void kvm_tdp_mmu_invalidate_all_roots(struct kvm *kvm)* Installs a last-level SPTE to handle a TDP page fault. * (NPT/EPT violation/misconfiguration) */ -static int tdp_mmu_map_handle_target_level (struct kvm_vcpu *vcpu,int write, -int map_writable, -struct tdp_iter *iter, -kvm_pfn_t pfn,bool prefault) +static int tdp_mmu_map_handle_target_level (struct kvm_vcpu *vcpu, +struct kvm_page_fault *fault, +struct tdp_iter *iter) {u64 new_spte;int ret =RET_PF_FIXED ;int make_spte_ret =0 ; -if (unlikely (is_noslot_pfn (pfn))) +if (unlikely (is_noslot_pfn (fault->pfn))) new_spte =make_mmio_spte (vcpu, iter->gfn,ACC_ALL );else make_spte_ret =make_spte (vcpu,ACC_ALL , iter->level, iter->gfn, - pfn, iter->old_spte, prefault, true, - map_writable, !shadow_accessed_mask, + fault->pfn, iter->old_spte, fault->prefault, true, + fault->map_writable, !shadow_accessed_mask, &new_spte);if (new_spte == iter->old_spte)
@@ -957,7 +956,7 @@ static int tdp_mmu_map_handle_target_level(struct kvm_vcpu *vcpu, int write,* the vCPU would have the same fault again. */ if (make_spte_ret &SET_SPTE_WRITE_PROTECTED_PT ) { -if (write) +if (fault->write) ret =RET_PF_EMULATE ; }
@@ -1064,8 +1063,7 @@ int kvm_tdp_mmu_map(struct kvm_vcpu *vcpu, struct kvm_page_fault *fault)return RET_PF_RETRY ; } - ret =tdp_mmu_map_handle_target_level (vcpu, fault->write, fault->map_writable, &iter, - fault->pfn, fault->prefault); + ret =tdp_mmu_map_handle_target_level (vcpu, fault, &iter);rcu_read_unlock ();return ret;
Signature · 1 line
2.27.0
Paolo Bonzini
maintainerPATCH v3 12/31 KVM: MMU: change fast_page_fault() arguments to kvm_page_fault
In reply to Paolo BonziniPass struct kvm_page_fault to fast_page_fault() instead of extracting the arguments from the struct.
--- arch/x86/kvm/mmu/mmu.c | 39 +++++++++++++++++---------------------- 1 file changed, 17 insertions(+), 22 deletions(-)
arch/x86/kvm/mmu/mmu.c +17 -22
@@ -3083,18 +3083,17 @@ static bool handle_abnormal_pfn(struct kvm_vcpu *vcpu, struct kvm_page_fault *fareturn false; } -static bool page_fault_can_be_fast (u32 error_code) +static bool page_fault_can_be_fast (struct kvm_page_fault *fault) {/* * Do not fix the mmio spte with invalid generation number which * need to be updated by slow page fault path. */ -if (unlikely (error_code &PFERR_RSVD_MASK )) +if (fault->rsvd)return false;/* See if the page fault is due to an NX violation */ -if (unlikely (((error_code & (PFERR_FETCH_MASK |PFERR_PRESENT_MASK )) - == (PFERR_FETCH_MASK |PFERR_PRESENT_MASK )))) +if (unlikely (fault->exec && fault->present))return false;/*
@@ -3111,9 +3110,7 @@ static bool page_fault_can_be_fast(u32 error_code)* accesses to a present page. */ -return shadow_acc_track_mask !=0 || - ((error_code & (PFERR_WRITE_MASK |PFERR_PRESENT_MASK )) - == (PFERR_WRITE_MASK |PFERR_PRESENT_MASK )); +return shadow_acc_track_mask !=0 || (fault->write && fault->present); }/*
@@ -3155,12 +3152,12 @@ fast_pf_fix_direct_spte(struct kvm_vcpu *vcpu, struct kvm_mmu_page *sp,return true; } -static bool is_access_allowed (u32 fault_err_code,u64 spte) +static bool is_access_allowed (struct kvm_page_fault *fault,u64 spte) { -if (fault_err_code &PFERR_FETCH_MASK ) +if (fault->exec)return is_executable_pte (spte); -if (fault_err_code &PFERR_WRITE_MASK ) +if (fault->write)return is_writable_pte (spte);/* Fault was on Read access */
@@ -3193,7 +3190,7 @@ static u64 *fast_pf_get_last_sptep(struct kvm_vcpu *vcpu, gpa_t gpa, u64 *spte)/* * Returns one of RET_PF_INVALID, RET_PF_FIXED or RET_PF_SPURIOUS. */ -static int fast_page_fault (struct kvm_vcpu *vcpu,gpa_t gpa,u32 error_code) +static int fast_page_fault (struct kvm_vcpu *vcpu,struct kvm_page_fault *fault) {struct kvm_mmu_page *sp;int ret =RET_PF_INVALID ;
@@ -3201,7 +3198,7 @@ static int fast_page_fault(struct kvm_vcpu *vcpu, gpa_t gpa, u32 error_code)u64 *sptep =NULL ;uint retry_count =0 ; -if (!page_fault_can_be_fast (error_code)) +if (!page_fault_can_be_fast (fault))return ret;walk_shadow_page_lockless_begin (vcpu);
@@ -3210,9 +3207,9 @@ static int fast_page_fault(struct kvm_vcpu *vcpu, gpa_t gpa, u32 error_code)u64 new_spte;if (is_tdp_mmu (vcpu->arch.mmu)) - sptep =kvm_tdp_mmu_fast_pf_get_last_sptep (vcpu, gpa, &spte); + sptep =kvm_tdp_mmu_fast_pf_get_last_sptep (vcpu, fault->addr, &spte);else - sptep =fast_pf_get_last_sptep (vcpu, gpa, &spte); + sptep =fast_pf_get_last_sptep (vcpu, fault->addr, &spte);if (!is_shadow_present_pte (spte))break ;
@@ -3231,7 +3228,7 @@ static int fast_page_fault(struct kvm_vcpu *vcpu, gpa_t gpa, u32 error_code)* Need not check the access of upper level table entries since * they are always ACC_ALL. */ -if (is_access_allowed (error_code, spte)) { +if (is_access_allowed (fault, spte)) { ret =RET_PF_SPURIOUS ;break ; }
@@ -3246,7 +3243,7 @@ static int fast_page_fault(struct kvm_vcpu *vcpu, gpa_t gpa, u32 error_code)* be removed in the fast path only if the SPTE was * write-protected for dirty-logging or access tracking. */ -if ((error_code &PFERR_WRITE_MASK ) && +if (fault->write &&spte_can_locklessly_be_made_writable (spte)) { new_spte |=PT_WRITABLE_MASK ;
@@ -3267,7 +3264,7 @@ static int fast_page_fault(struct kvm_vcpu *vcpu, gpa_t gpa, u32 error_code)/* Verify that the fault can be handled in the fast path */ if (new_spte == spte || - !is_access_allowed (error_code, new_spte)) + !is_access_allowed (fault, new_spte))break ;/*
@@ -3288,7 +3285,7 @@ static int fast_page_fault(struct kvm_vcpu *vcpu, gpa_t gpa, u32 error_code) }while (true); -trace_fast_page_fault (vcpu, gpa, error_code, sptep, spte, ret); +trace_fast_page_fault (vcpu, fault->addr, fault->error_code, sptep, spte, ret);walk_shadow_page_lockless_end (vcpu);return ret;
@@ -3946,18 +3943,16 @@ static bool kvm_faultin_pfn(struct kvm_vcpu *vcpu, struct kvm_page_fault *fault,static int direct_page_fault (struct kvm_vcpu *vcpu,struct kvm_page_fault *fault) { -gpa_t gpa = fault->addr; -u32 error_code = fault->error_code;bool is_tdp_mmu_fault =is_tdp_mmu (vcpu->arch.mmu);unsigned long mmu_seq;int r; - fault->gfn = gpa >>PAGE_SHIFT ; + fault->gfn = fault->addr >>PAGE_SHIFT ;if (page_fault_handle_page_track (vcpu, fault))return RET_PF_EMULATE ; - r =fast_page_fault (vcpu, gpa, error_code); + r =fast_page_fault (vcpu, fault);if (r !=RET_PF_INVALID )return r;
Signature · 1 line
2.27.0
Paolo Bonzini
maintainerPATCH v3 13/31 KVM: MMU: change kvm_mmu_hugepage_adjust() arguments to kvm_page_fault
In reply to Paolo BonziniPass struct kvm_page_fault to kvm_mmu_hugepage_adjust() instead of extracting the arguments from the struct; the results are also stored in the struct, so the callers are adjusted consequently.
--- arch/x86/kvm/mmu.h | 35 +++++++++++++++++-- arch/x86/kvm/mmu/mmu.c | 60 +++++++++++++++------------------ arch/x86/kvm/mmu/mmu_internal.h | 12 ++----- arch/x86/kvm/mmu/paging_tmpl.h | 16 ++++----- arch/x86/kvm/mmu/tdp_mmu.c | 21 +++++------- 5 files changed, 77 insertions(+), 67 deletions(-)
arch/x86/kvm/mmu.h +33 -2
@@ -127,12 +127,34 @@ struct kvm_page_fault {const bool rsvd;const bool user; -/* Derived from mmu. */ +/* Derived from mmu and global state. */ const bool is_tdp; +const bool nx_huge_page_workaround_enabled; -/* Input to FNAME(fetch), __direct_map and kvm_tdp_mmu_map. */ +/* +* Whether a >4KB mapping can be created or is forbidden due to NX +* hugepages. +*/ +bool huge_page_disallowed; + +/* +* Maximum page size that can be created for this fault; input to +* FNAME(fetch), __direct_map and kvm_tdp_mmu_map. +*/ u8 max_level; +/* +* Page size that can be created based on the max_level and the +* page size used by the host mapping. +*/ +u8 req_level; + +/* +* Page size that will be created based on the req_level and +* huge_page_disallowed. +*/ +u8 goal_level; +/* Shifted addr, or result of guest page table walk if addr is a gva. */ gfn_t gfn;
@@ -144,6 +166,12 @@ struct kvm_page_fault {int kvm_tdp_page_fault (struct kvm_vcpu *vcpu,struct kvm_page_fault *fault); +extern int nx_huge_pages; +static inline bool is_nx_huge_page_enabled (void ) +{ +return READ_ONCE (nx_huge_pages); +} +static inline int kvm_mmu_do_page_fault (struct kvm_vcpu *vcpu,gpa_t cr2_or_gpa,u32 err,bool prefault) {
@@ -157,8 +185,11 @@ static inline int kvm_mmu_do_page_fault(struct kvm_vcpu *vcpu, gpa_t cr2_or_gpa, .user = err & PFERR_USER_MASK, .prefault = prefault, .is_tdp = likely(vcpu->arch.mmu->page_fault == kvm_tdp_page_fault), + .nx_huge_page_workaround_enabled = is_nx_huge_page_enabled(), .max_level = KVM_MAX_HUGEPAGE_LEVEL, + .req_level = PG_LEVEL_4K, + .goal_level = PG_LEVEL_4K, };#ifdef CONFIG_RETPOLINE if (fault.is_tdp)
arch/x86/kvm/mmu/mmu.c +27 -33
116 lines not on this page · raw message
arch/x86/kvm/mmu/mmu_internal.h +2 -10
@@ -118,12 +118,6 @@ static inline bool kvm_vcpu_ad_need_write_protect(struct kvm_vcpu *vcpu) kvm_x86_ops.cpu_dirty_log_size; } -extern int nx_huge_pages; -static inline bool is_nx_huge_page_enabled (void ) -{ -return READ_ONCE (nx_huge_pages); -} -int mmu_try_to_unsync_pages (struct kvm_vcpu *vcpu,gfn_t gfn,bool can_unsync,bool speculative);
@@ -164,11 +158,9 @@ enum {int kvm_mmu_max_mapping_level (struct kvm *kvm,const struct kvm_memory_slot *slot,gfn_t gfn,kvm_pfn_t pfn,int max_level); -int kvm_mmu_hugepage_adjust (struct kvm_vcpu *vcpu,gfn_t gfn, -int max_level,kvm_pfn_t *pfnp, -bool huge_page_disallowed,int *req_level); +void kvm_mmu_hugepage_adjust (struct kvm_vcpu *vcpu,struct kvm_page_fault *fault);void disallowed_hugepage_adjust (u64 spte,gfn_t gfn,int cur_level, -kvm_pfn_t *pfnp,int *goal_levelp); +kvm_pfn_t *pfnp,u8 *goal_levelp);void *mmu_memory_cache_alloc (struct kvm_mmu_memory_cache *mc);
arch/x86/kvm/mmu/paging_tmpl.h +7 -9
@@ -658,12 +658,10 @@ static void FNAME(pte_prefetch)(struct kvm_vcpu *vcpu, struct guest_walker *gw,static int FNAME (fetch )(struct kvm_vcpu *vcpu,struct kvm_page_fault *fault,struct guest_walker *gw) { -bool nx_huge_page_workaround_enabled =is_nx_huge_page_enabled (); -bool huge_page_disallowed = fault->exec && nx_huge_page_workaround_enabled;struct kvm_mmu_page *sp =NULL ;struct kvm_shadow_walk_iterator it;unsigned int direct_access, access; -int top_level, level, req_level, ret; +int top_level, ret;gfn_t base_gfn = fault->gfn;WARN_ON_ONCE (gw->gfn != base_gfn);
@@ -730,8 +728,7 @@ static int FNAME(fetch)(struct kvm_vcpu *vcpu, struct kvm_page_fault *fault,link_shadow_page (vcpu, it.sptep, sp); } - level =kvm_mmu_hugepage_adjust (vcpu, gw->gfn, fault->max_level, &fault->pfn, - huge_page_disallowed, &req_level); +kvm_mmu_hugepage_adjust (vcpu, fault);trace_kvm_mmu_spte_requested (fault->addr, gw->level, fault->pfn);
@@ -742,12 +739,12 @@ static int FNAME(fetch)(struct kvm_vcpu *vcpu, struct kvm_page_fault *fault,* We cannot overwrite existing page tables with an NX * large page, as the leaf could be executable. */ -if (nx_huge_page_workaround_enabled) +if (fault->nx_huge_page_workaround_enabled)disallowed_hugepage_adjust (*it.sptep, fault->gfn, it.level, - &fault->pfn, &level); + &fault->pfn, &fault->goal_level); base_gfn = fault->gfn & ~(KVM_PAGES_PER_HPAGE (it.level) -1 ); -if (it.level == level) +if (it.level == fault->goal_level)break ;validate_direct_spte (vcpu, it.sptep, direct_access);
@@ -758,7 +755,8 @@ static int FNAME(fetch)(struct kvm_vcpu *vcpu, struct kvm_page_fault *fault, sp =kvm_mmu_get_page (vcpu, base_gfn, fault->addr, it.level -1 , true, direct_access);link_shadow_page (vcpu, it.sptep, sp); -if (huge_page_disallowed && req_level >= it.level) +if (fault->huge_page_disallowed && + fault->req_level >= it.level)account_huge_nx_page (vcpu->kvm, sp); } }
arch/x86/kvm/mmu/tdp_mmu.c +8 -13
@@ -986,30 +986,25 @@ static int tdp_mmu_map_handle_target_level(struct kvm_vcpu *vcpu,*/ int kvm_tdp_mmu_map (struct kvm_vcpu *vcpu,struct kvm_page_fault *fault) { -bool nx_huge_page_workaround_enabled =is_nx_huge_page_enabled (); -bool huge_page_disallowed = fault->exec && nx_huge_page_workaround_enabled;struct kvm_mmu *mmu = vcpu->arch.mmu;struct tdp_iter iter;struct kvm_mmu_page *sp;u64 *child_pt;u64 new_spte;int ret; -int level; -int req_level; - level =kvm_mmu_hugepage_adjust (vcpu, fault->gfn, fault->max_level, &fault->pfn, - huge_page_disallowed, &req_level); +kvm_mmu_hugepage_adjust (vcpu, fault); -trace_kvm_mmu_spte_requested (fault->addr, level, fault->pfn); +trace_kvm_mmu_spte_requested (fault->addr, fault->goal_level, fault->pfn);rcu_read_lock ();tdp_mmu_for_each_pte (iter, mmu, fault->gfn, fault->gfn +1 ) { -if (nx_huge_page_workaround_enabled) +if (fault->nx_huge_page_workaround_enabled)disallowed_hugepage_adjust (iter.old_spte, fault->gfn, - iter.level, &fault->pfn, &level); + iter.level, &fault->pfn, &fault->goal_level); -if (iter.level == level) +if (iter.level == fault->goal_level)break ;/*
@@ -1047,8 +1042,8 @@ int kvm_tdp_mmu_map(struct kvm_vcpu *vcpu, struct kvm_page_fault *fault)if (tdp_mmu_set_spte_atomic_no_dirty_log (vcpu->kvm, &iter, new_spte)) {tdp_mmu_link_page (vcpu->kvm, sp, - huge_page_disallowed && - req_level >= iter.level); + fault->huge_page_disallowed && + fault->req_level >= iter.level);trace_kvm_mmu_get_page (sp, true); }else {
@@ -1058,7 +1053,7 @@ int kvm_tdp_mmu_map(struct kvm_vcpu *vcpu, struct kvm_page_fault *fault) } } -if (iter.level != level) { +if (iter.level != fault->goal_level) {rcu_read_unlock ();return RET_PF_RETRY ; }
Signature · 1 line
2.27.0
Paolo Bonzini
maintainerPATCH v3 14/31 KVM: MMU: change disallowed_hugepage_adjust() arguments to kvm_page_fault
In reply to Paolo BonziniPass struct kvm_page_fault to disallowed_hugepage_adjust() instead of extracting the arguments from the struct. Tweak a bit the conditions to avoid long lines.
--- arch/x86/kvm/mmu/mmu.c | 19 ++++++++----------- arch/x86/kvm/mmu/mmu_internal.h | 3 +-- arch/x86/kvm/mmu/paging_tmpl.h | 3 +-- arch/x86/kvm/mmu/tdp_mmu.c | 3 +-- 4 files changed, 11 insertions(+), 17 deletions(-)
arch/x86/kvm/mmu/mmu.c +8 -11
@@ -2957,12 +2957,10 @@ void kvm_mmu_hugepage_adjust(struct kvm_vcpu *vcpu, struct kvm_page_fault *fault fault->pfn &= ~mask; } -void disallowed_hugepage_adjust (u64 spte,gfn_t gfn,int cur_level, -kvm_pfn_t *pfnp,u8 *goal_levelp) +void disallowed_hugepage_adjust (struct kvm_page_fault *fault,u64 spte,int cur_level) { -int level = *goal_levelp; - -if (cur_level == level && level >PG_LEVEL_4K && +if (cur_level >PG_LEVEL_4K && + cur_level == fault->goal_level &&is_shadow_present_pte (spte) && !is_large_pte (spte)) {/*
@@ -2972,10 +2970,10 @@ void disallowed_hugepage_adjust(u64 spte, gfn_t gfn, int cur_level,* patching back for them into pfn the next 9 bits of * the address. */ -u64 page_mask =KVM_PAGES_PER_HPAGE (level) - -KVM_PAGES_PER_HPAGE (level -1 ); - *pfnp |= gfn & page_mask; - (*goal_levelp)--; +u64 page_mask =KVM_PAGES_PER_HPAGE (cur_level) - +KVM_PAGES_PER_HPAGE (cur_level -1 ); + fault->pfn |= fault->gfn & page_mask; + fault->goal_level--; } }
@@ -2995,8 +2993,7 @@ static int __direct_map(struct kvm_vcpu *vcpu, struct kvm_page_fault *fault)* large page, as the leaf could be executable. */ if (fault->nx_huge_page_workaround_enabled) -disallowed_hugepage_adjust (*it.sptep, fault->gfn, it.level, - &fault->pfn, &fault->goal_level); +disallowed_hugepage_adjust (fault, *it.sptep, it.level); base_gfn = fault->gfn & ~(KVM_PAGES_PER_HPAGE (it.level) -1 );if (it.level == fault->goal_level)
arch/x86/kvm/mmu/mmu_internal.h +1 -2
@@ -159,8 +159,7 @@ int kvm_mmu_max_mapping_level(struct kvm *kvm, const struct kvm_memory_slot *slot, gfn_t gfn, kvm_pfn_t pfn, int max_level);void kvm_mmu_hugepage_adjust (struct kvm_vcpu *vcpu,struct kvm_page_fault *fault); -void disallowed_hugepage_adjust (u64 spte,gfn_t gfn,int cur_level, -kvm_pfn_t *pfnp,u8 *goal_levelp); +void disallowed_hugepage_adjust (struct kvm_page_fault *fault,u64 spte,int cur_level);void *mmu_memory_cache_alloc (struct kvm_mmu_memory_cache *mc);
arch/x86/kvm/mmu/paging_tmpl.h +1 -2
@@ -740,8 +740,7 @@ static int FNAME(fetch)(struct kvm_vcpu *vcpu, struct kvm_page_fault *fault,* large page, as the leaf could be executable. */ if (fault->nx_huge_page_workaround_enabled) -disallowed_hugepage_adjust (*it.sptep, fault->gfn, it.level, - &fault->pfn, &fault->goal_level); +disallowed_hugepage_adjust (fault, *it.sptep, it.level); base_gfn = fault->gfn & ~(KVM_PAGES_PER_HPAGE (it.level) -1 );if (it.level == fault->goal_level)
arch/x86/kvm/mmu/tdp_mmu.c +1 -2
@@ -1001,8 +1001,7 @@ int kvm_tdp_mmu_map(struct kvm_vcpu *vcpu, struct kvm_page_fault *fault)tdp_mmu_for_each_pte (iter, mmu, fault->gfn, fault->gfn +1 ) {if (fault->nx_huge_page_workaround_enabled) -disallowed_hugepage_adjust (iter.old_spte, fault->gfn, - iter.level, &fault->pfn, &fault->goal_level); +disallowed_hugepage_adjust (fault, iter.old_spte, iter.level);if (iter.level == fault->goal_level)break ;
Signature · 1 line
2.27.0
Paolo Bonzini
maintainerPATCH v3 15/31 KVM: MMU: change tracepoints arguments to kvm_page_fault
In reply to Paolo BonziniPass struct kvm_page_fault to tracepoints instead of extracting the arguments from the struct. This also lets the kvm_mmu_spte_requested tracepoint pick the gfn directly from fault->gfn, instead of using the address.
--- arch/x86/kvm/mmu/mmu.c | 4 ++-- arch/x86/kvm/mmu/mmutrace.h | 18 +++++++++--------- arch/x86/kvm/mmu/paging_tmpl.h | 2 +- arch/x86/kvm/mmu/tdp_mmu.c | 2 +- 4 files changed, 13 insertions(+), 13 deletions(-)
arch/x86/kvm/mmu/mmu.c +2 -2
@@ -2986,7 +2986,7 @@ static int __direct_map(struct kvm_vcpu *vcpu, struct kvm_page_fault *fault)kvm_mmu_hugepage_adjust (vcpu, fault); -trace_kvm_mmu_spte_requested (fault->addr, fault->goal_level, fault->pfn); +trace_kvm_mmu_spte_requested (fault);for_each_shadow_entry (vcpu, fault->addr, it) {/* * We cannot overwrite existing page tables with an NX
@@ -3276,7 +3276,7 @@ static int fast_page_fault(struct kvm_vcpu *vcpu, struct kvm_page_fault *fault) }while (true); -trace_fast_page_fault (vcpu, fault->addr, fault->error_code, sptep, spte, ret); +trace_fast_page_fault (vcpu, fault, sptep, spte, ret);walk_shadow_page_lockless_end (vcpu);return ret;
arch/x86/kvm/mmu/mmutrace.h +9 -9
@@ -252,9 +252,9 @@ TRACE_EVENT( TRACE_EVENT( fast_page_fault, - TP_PROTO(struct kvm_vcpu *vcpu, gpa_t cr2_or_gpa, u32 error_code, + TP_PROTO(struct kvm_vcpu *vcpu, struct kvm_page_fault *fault, u64 *sptep, u64 old_spte, int ret), - TP_ARGS(vcpu, cr2_or_gpa, error_code, sptep, old_spte, ret), + TP_ARGS(vcpu, fault, sptep, old_spte, ret), TP_STRUCT__entry( __field(int, vcpu_id)
@@ -268,8 +268,8 @@ TRACE_EVENT(TP_fast_assign ( __entry->vcpu_id = vcpu->vcpu_id; - __entry->cr2_or_gpa = cr2_or_gpa; - __entry->error_code = error_code; + __entry->cr2_or_gpa = fault->addr; + __entry->error_code = fault->error_code; __entry->sptep = sptep; __entry->old_spte = old_spte; __entry->new_spte = *sptep;
@@ -367,8 +367,8 @@ TRACE_EVENT( TRACE_EVENT( kvm_mmu_spte_requested, - TP_PROTO(gpa_t addr, int level, kvm_pfn_t pfn), - TP_ARGS(addr, level, pfn), + TP_PROTO(struct kvm_page_fault *fault), + TP_ARGS(fault), TP_STRUCT__entry( __field(u64, gfn)
@@ -377,9 +377,9 @@ TRACE_EVENT( ), TP_fast_assign( - __entry->gfn = addr >> PAGE_SHIFT; - __entry->pfn = pfn | (__entry->gfn & (KVM_PAGES_PER_HPAGE (level) -1 )); - __entry->level = level; + __entry->gfn = fault->gfn; + __entry->pfn = fault->pfn | (fault->gfn & (KVM_PAGES_PER_HPAGE (fault->goal_level) -1 )); + __entry->level = fault->goal_level; ), TP_printk("gfn %llx pfn %llx level %d",
arch/x86/kvm/mmu/paging_tmpl.h +1 -1
@@ -730,7 +730,7 @@ static int FNAME(fetch)(struct kvm_vcpu *vcpu, struct kvm_page_fault *fault,kvm_mmu_hugepage_adjust (vcpu, fault); -trace_kvm_mmu_spte_requested (fault->addr, gw->level, fault->pfn); +trace_kvm_mmu_spte_requested (fault);for (;shadow_walk_okay (&it);shadow_walk_next (&it)) {clear_sp_write_flooding_count (it.sptep);
arch/x86/kvm/mmu/tdp_mmu.c +1 -1
@@ -995,7 +995,7 @@ int kvm_tdp_mmu_map(struct kvm_vcpu *vcpu, struct kvm_page_fault *fault)kvm_mmu_hugepage_adjust (vcpu, fault); -trace_kvm_mmu_spte_requested (fault->addr, fault->goal_level, fault->pfn); +trace_kvm_mmu_spte_requested (fault);rcu_read_lock ();
Signature · 1 line
2.27.0
Paolo Bonzini
maintainerPATCH v3 16/31 KVM: x86/mmu: Verify shadow walk doesn't terminate early in page faults
In reply to Paolo BonziniFrom: Sean Christopherson <[email protected]>
WARN and bail if the shadow walk for faulting in a SPTE terminates early, i.e. doesn't reach the expected level because the walk encountered a terminal SPTE. The shadow walks for page faults are subtle in that they install non-leaf SPTEs (zapping leaf SPTEs if necessary!) in the loop body, and consume the newly created non-leaf SPTE in the loop control, e.g. __shadow_walk_next(). In other words, the walks guarantee that the walk will stop if and only if the target level is reached by installing non-leaf SPTEs to guarantee the walk remains valid.
Opportunistically use fault->goal-level instead of it.level in FNAME(fetch) to further clarify that KVM always installs the leaf SPTE at the target level.
Message-Id: <[email protected]>
--- arch/x86/kvm/mmu/mmu.c | 3 +++ arch/x86/kvm/mmu/paging_tmpl.h | 7 +++++-- 2 files changed, 8 insertions(+), 2 deletions(-)
arch/x86/kvm/mmu/mmu.c +3 -0
@@ -3012,6 +3012,9 @@ static int __direct_map(struct kvm_vcpu *vcpu, struct kvm_page_fault *fault)account_huge_nx_page (vcpu->kvm, sp); } +if (WARN_ON_ONCE (it.level != fault->goal_level)) +return -EFAULT ; + ret =mmu_set_spte (vcpu, it.sptep,ACC_ALL , fault->write, fault->goal_level, base_gfn, fault->pfn, fault->prefault, fault->map_writable);
arch/x86/kvm/mmu/paging_tmpl.h +5 -2
@@ -760,9 +760,12 @@ static int FNAME(fetch)(struct kvm_vcpu *vcpu, struct kvm_page_fault *fault, } } +if (WARN_ON_ONCE (it.level != fault->goal_level)) +return -EFAULT ; + ret =mmu_set_spte (vcpu, it.sptep, gw->pte_access, fault->write, - it.level, base_gfn, fault->pfn, fault->prefault, - fault->map_writable); + fault->goal_level, base_gfn, fault->pfn, + fault->prefault, fault->map_writable);if (ret ==RET_PF_SPURIOUS )return ret;
Signature · 1 line
2.27.0
Paolo Bonzini
maintainerPATCH v3 17/31 KVM: x86/mmu: Fold rmap_recycle into rmap_add
In reply to Paolo BonziniFrom: David Matlack <[email protected]>
Consolidate rmap_recycle and rmap_add into a single function since they are only ever called together (and only from one place). This has a nice side effect of eliminating an extra kvm_vcpu_gfn_to_memslot(). In addition it makes mmu_set_spte(), which is a very long function, a little shorter.
No functional change intended.
Message-Id: <[email protected]>
--- arch/x86/kvm/mmu/mmu.c | 40 ++++++++++++++-------------------------- 1 file changed, 14 insertions(+), 26 deletions(-)
arch/x86/kvm/mmu/mmu.c +14 -26
@@ -1071,20 +1071,6 @@ static bool rmap_can_add(struct kvm_vcpu *vcpu)return kvm_mmu_memory_cache_nr_free_objects (mc); } -static int rmap_add (struct kvm_vcpu *vcpu,u64 *spte,gfn_t gfn) -{ -struct kvm_memory_slot *slot; -struct kvm_mmu_page *sp; -struct kvm_rmap_head *rmap_head; - - sp =sptep_to_sp (spte); -kvm_mmu_page_set_gfn (sp, spte - sp->spt, gfn); - slot =kvm_vcpu_gfn_to_memslot (vcpu, gfn); - rmap_head =gfn_to_rmap (gfn, sp->role.level, slot); -return pte_list_add (vcpu, spte, rmap_head); -} - -static void rmap_remove (struct kvm *kvm,u64 *spte) {struct kvm_memslots *slots;
@@ -1097,9 +1083,9 @@ static void rmap_remove(struct kvm *kvm, u64 *spte) gfn =kvm_mmu_page_get_gfn (sp, spte - sp->spt);/* -* Unlike rmap_add and rmap_recycle, rmap_remove does not run in the -* context of a vCPU so have to determine which memslots to use based -* on context information in sp->role. +* Unlike rmap_add, rmap_remove does not run in the context of a vCPU +* so we have to determine which memslots to use based on context +* information in sp->role. */ slots =kvm_memslots_for_spte_role (kvm, sp->role);
@@ -1639,19 +1625,24 @@ static bool kvm_test_age_rmapp(struct kvm *kvm, struct kvm_rmap_head *rmap_head,#define RMAP_RECYCLE_THRESHOLD 1000 -static void rmap_recycle (struct kvm_vcpu *vcpu,u64 *spte,gfn_t gfn) +static void rmap_add (struct kvm_vcpu *vcpu,u64 *spte,gfn_t gfn) {struct kvm_memory_slot *slot; -struct kvm_rmap_head *rmap_head;struct kvm_mmu_page *sp; +struct kvm_rmap_head *rmap_head; +int rmap_count; sp =sptep_to_sp (spte); +kvm_mmu_page_set_gfn (sp, spte - sp->spt, gfn); slot =kvm_vcpu_gfn_to_memslot (vcpu, gfn); rmap_head =gfn_to_rmap (gfn, sp->role.level, slot); + rmap_count =pte_list_add (vcpu, spte, rmap_head); -kvm_unmap_rmapp (vcpu->kvm, rmap_head,NULL , gfn, sp->role.level,__pte (0 )); -kvm_flush_remote_tlbs_with_address (vcpu->kvm, sp->gfn, -KVM_PAGES_PER_HPAGE (sp->role.level)); +if (rmap_count >RMAP_RECYCLE_THRESHOLD ) { +kvm_unmap_rmapp (vcpu->kvm, rmap_head,NULL , gfn, sp->role.level,__pte (0 )); +kvm_flush_remote_tlbs_with_address ( + vcpu->kvm, sp->gfn,KVM_PAGES_PER_HPAGE (sp->role.level)); + } }bool kvm_age_gfn (struct kvm *kvm,struct kvm_gfn_range *range)
@@ -2713,7 +2704,6 @@ static int mmu_set_spte(struct kvm_vcpu *vcpu, u64 *sptep, bool host_writable) {int was_rmapped =0 ; -int rmap_count;int set_spte_ret;int ret =RET_PF_FIXED ;bool flush = false;
@@ -2772,9 +2762,7 @@ static int mmu_set_spte(struct kvm_vcpu *vcpu, u64 *sptep,if (!was_rmapped) {kvm_update_page_stats (vcpu->kvm, level,1 ); - rmap_count =rmap_add (vcpu, sptep, gfn); -if (rmap_count >RMAP_RECYCLE_THRESHOLD ) -rmap_recycle (vcpu, sptep, gfn); +rmap_add (vcpu, sptep, gfn); }return ret;
Signature · 1 line
2.27.0
Paolo Bonzini
maintainerPATCH v3 18/31 KVM: MMU: mark page dirty in make_spte
In reply to Paolo BonziniThis simplifies set_spte, which we want to remove, and unifies code between the shadow MMU and the TDP MMU. The warning will be added back later to make_spte as well.
There is a small disadvantage in the TDP MMU; it may unnecessarily mark a page as dirty twice if two vCPUs end up mapping the same page twice. However, this is a very small cost for a case that is already rare.
--- arch/x86/kvm/mmu/mmu.c | 3 --- arch/x86/kvm/mmu/spte.c | 3 +++ arch/x86/kvm/mmu/tdp_mmu.c | 21 +-------------------- 3 files changed, 4 insertions(+), 23 deletions(-)
arch/x86/kvm/mmu/mmu.c +0 -3
@@ -2688,9 +2688,6 @@ static int set_spte(struct kvm_vcpu *vcpu, u64 *sptep, ret =make_spte (vcpu, pte_access, level, gfn, pfn, *sptep, speculative, can_unsync, host_writable,sp_ad_disabled (sp), &spte); -if (spte &PT_WRITABLE_MASK ) -kvm_vcpu_mark_page_dirty (vcpu, gfn); -if (*sptep == spte) ret |=SET_SPTE_SPURIOUS ;else if (mmu_spte_update (sptep, spte))
arch/x86/kvm/mmu/spte.c +3 -0
@@ -179,6 +179,9 @@ int make_spte(struct kvm_vcpu *vcpu, unsigned int pte_access, int level,"spte = 0x%llx, level = %d, rsvd bits = 0x%llx" , spte, level, get_rsvd_bits(&vcpu->arch.mmu->shadow_zero_check, spte, level)); +if (spte &PT_WRITABLE_MASK ) +kvm_vcpu_mark_page_dirty (vcpu, gfn); + *new_spte = spte;return ret; }
arch/x86/kvm/mmu/tdp_mmu.c +1 -20
@@ -542,26 +542,7 @@ static inline bool tdp_mmu_map_set_spte_atomic(struct kvm_vcpu *vcpu, struct tdp_iter *iter, u64 new_spte) { -struct kvm *kvm = vcpu->kvm; - -if (!tdp_mmu_set_spte_atomic_no_dirty_log (kvm, iter, new_spte)) -return false; - -/* -* Use kvm_vcpu_gfn_to_memslot() instead of going through -* handle_changed_spte_dirty_log() to leverage vcpu->last_used_slot. -*/ -if (is_writable_pte (new_spte)) { -struct kvm_memory_slot *slot =kvm_vcpu_gfn_to_memslot (vcpu, iter->gfn); - -if (slot &&kvm_slot_dirty_track_enabled (slot)) { -/* Enforced by kvm_mmu_hugepage_adjust. */ -WARN_ON_ONCE (iter->level >PG_LEVEL_4K ); -mark_page_dirty_in_slot (kvm, slot, iter->gfn); - } - } - -return true; +return tdp_mmu_set_spte_atomic_no_dirty_log (vcpu->kvm, iter, new_spte); } static inline bool tdp_mmu_zap_spte_atomic(struct kvm *kvm,
Signature · 1 line
2.27.0
Paolo Bonzini
maintainerPATCH v3 19/31 KVM: MMU: unify tdp_mmu_map_set_spte_atomic and tdp_mmu_set_spte_atomic_no_dirty_log
In reply to Paolo Bonzinitdp_mmu_map_set_spte_atomic is not taking care of dirty logging anymore, the only difference that remains is that it takes a vCPU instead of the struct kvm. Merge the two functions.
--- arch/x86/kvm/mmu/tdp_mmu.c | 40 ++++++++++---------------------------- 1 file changed, 10 insertions(+), 30 deletions(-)
arch/x86/kvm/mmu/tdp_mmu.c +10 -30
@@ -489,8 +489,8 @@ static void handle_changed_spte(struct kvm *kvm, int as_id, gfn_t gfn, }/* -* tdp_mmu_set_spte_atomic_no_dirty_log - Set a TDP MMU SPTE atomically -* and handle the associated bookkeeping, but do not mark the page dirty +* tdp_mmu_set_spte_atomic - Set a TDP MMU SPTE atomically +* and handle the associated bookkeeping. Do not mark the page dirty * in KVM's dirty bitmaps. * * @kvm: kvm instance
@@ -499,9 +499,9 @@ static void handle_changed_spte(struct kvm *kvm, int as_id, gfn_t gfn,* Returns: true if the SPTE was set, false if it was not. If false is returned, * this function will have no side-effects. */ -static inline bool tdp_mmu_set_spte_atomic_no_dirty_log (struct kvm *kvm, -struct tdp_iter *iter, -u64 new_spte) +static inline bool tdp_mmu_set_spte_atomic (struct kvm *kvm, +struct tdp_iter *iter, +u64 new_spte) {lockdep_assert_held_read (&kvm->mmu_lock);
@@ -527,24 +527,6 @@ static inline bool tdp_mmu_set_spte_atomic_no_dirty_log(struct kvm *kvm,return true; } -/* -* tdp_mmu_map_set_spte_atomic - Set a leaf TDP MMU SPTE atomically to resolve a -* TDP page fault. -* -* @vcpu: The vcpu instance that took the TDP page fault. -* @iter: a tdp_iter instance currently on the SPTE that should be set -* @new_spte: The value the SPTE should be set to -* -* Returns: true if the SPTE was set, false if it was not. If false is returned, -* this function will have no side-effects. -*/ -static inline bool tdp_mmu_map_set_spte_atomic (struct kvm_vcpu *vcpu, -struct tdp_iter *iter, -u64 new_spte) -{ -return tdp_mmu_set_spte_atomic_no_dirty_log (vcpu->kvm, iter, new_spte); -} -static inline bool tdp_mmu_zap_spte_atomic (struct kvm *kvm,struct tdp_iter *iter) {
@@ -554,7 +536,7 @@ static inline bool tdp_mmu_zap_spte_atomic(struct kvm *kvm,* immediately installing a present entry in its place * before the TLBs are flushed. */ -if (!tdp_mmu_set_spte_atomic_no_dirty_log (kvm, iter,REMOVED_SPTE )) +if (!tdp_mmu_set_spte_atomic (kvm, iter,REMOVED_SPTE ))return false; kvm_flush_remote_tlbs_with_address(kvm, iter->gfn,
@@ -928,7 +910,7 @@ static int tdp_mmu_map_handle_target_level(struct kvm_vcpu *vcpu,if (new_spte == iter->old_spte) ret =RET_PF_SPURIOUS ; -else if (!tdp_mmu_map_set_spte_atomic (vcpu, iter, new_spte)) +else if (!tdp_mmu_set_spte_atomic (vcpu->kvm, iter, new_spte))return RET_PF_RETRY ;/*
@@ -1020,7 +1002,7 @@ int kvm_tdp_mmu_map(struct kvm_vcpu *vcpu, struct kvm_page_fault *fault) new_spte =make_nonleaf_spte (child_pt, !shadow_accessed_mask); -if (tdp_mmu_set_spte_atomic_no_dirty_log (vcpu->kvm, &iter, new_spte)) { +if (tdp_mmu_set_spte_atomic (vcpu->kvm, &iter, new_spte)) {tdp_mmu_link_page (vcpu->kvm, sp, fault->huge_page_disallowed && fault->req_level >= iter.level);
@@ -1208,8 +1190,7 @@ static bool wrprot_gfn_range(struct kvm *kvm, struct kvm_mmu_page *root, new_spte = iter.old_spte & ~PT_WRITABLE_MASK ; -if (!tdp_mmu_set_spte_atomic_no_dirty_log (kvm, &iter, - new_spte)) { +if (!tdp_mmu_set_spte_atomic (kvm, &iter, new_spte)) {/* * The iter must explicitly re-read the SPTE because * the atomic cmpxchg failed.
@@ -1277,8 +1258,7 @@ static bool clear_dirty_gfn_range(struct kvm *kvm, struct kvm_mmu_page *root,continue ; } -if (!tdp_mmu_set_spte_atomic_no_dirty_log (kvm, &iter, - new_spte)) { +if (!tdp_mmu_set_spte_atomic (kvm, &iter, new_spte)) {/* * The iter must explicitly re-read the SPTE because * the atomic cmpxchg failed.
Signature · 1 line
2.27.0
Paolo Bonzini
maintainerPATCH v3 20/31 KVM: x86/mmu: Pass the memslot around via struct kvm_page_fault
In reply to Paolo BonziniFrom: David Matlack <[email protected]>
The memslot for the faulting gfn is used throughout the page fault handling code, so capture it in kvm_page_fault as soon as we know the gfn and use it in the page fault handling code that has direct access to the kvm_page_fault struct. Replace various tests using is_noslot_pfn with more direct tests on fault->slot being NULL.
This, in combination with the subsequent patch, improves "Populate memory time" in dirty_log_perf_test by 5% when using the legacy MMU. There is no discerable improvement to the performance of the TDP MMU.
No functional change intended.
Message-Id: <[email protected]>
--- arch/x86/kvm/mmu.h | 3 +++ arch/x86/kvm/mmu/mmu.c | 32 ++++++++++++-------------------- arch/x86/kvm/mmu/paging_tmpl.h | 6 ++++-- arch/x86/kvm/mmu/tdp_mmu.c | 2 +- 4 files changed, 20 insertions(+), 23 deletions(-)
arch/x86/kvm/mmu.h +3 -0
@@ -158,6 +158,9 @@ struct kvm_page_fault {/* Shifted addr, or result of guest page table walk if addr is a gva. */ gfn_t gfn; +/* The memslot containing gfn. May be NULL. */ +struct kvm_memory_slot *slot; +/* Outputs of kvm_faultin_pfn. */ kvm_pfn_t pfn;hva_t hva;
arch/x86/kvm/mmu/mmu.c +12 -20
@@ -2907,7 +2907,7 @@ int kvm_mmu_max_mapping_level(struct kvm *kvm,void kvm_mmu_hugepage_adjust (struct kvm_vcpu *vcpu,struct kvm_page_fault *fault) { -struct kvm_memory_slot *slot; +struct kvm_memory_slot *slot = fault->slot;kvm_pfn_t mask; fault->huge_page_disallowed = fault->exec && fault->nx_huge_page_workaround_enabled;
@@ -2918,8 +2918,7 @@ void kvm_mmu_hugepage_adjust(struct kvm_vcpu *vcpu, struct kvm_page_fault *faultif (is_error_noslot_pfn (fault->pfn) ||kvm_is_reserved_pfn (fault->pfn))return ; - slot =gfn_to_memslot_dirty_bitmap (vcpu, fault->gfn, true); -if (!slot) +if (kvm_slot_dirty_track_enabled (slot))return ;/*
@@ -3043,7 +3042,7 @@ static bool handle_abnormal_pfn(struct kvm_vcpu *vcpu, struct kvm_page_fault *fareturn true; } -if (unlikely (is_noslot_pfn (fault->pfn))) { +if (unlikely (!fault->slot)) {gva_t gva = fault->is_tdp ?0 : fault->addr; vcpu_cache_mmio_info(vcpu, gva, fault->gfn,
@@ -3097,13 +3096,9 @@ static bool page_fault_can_be_fast(struct kvm_page_fault *fault)* someone else modified the SPTE from its original value. */ static bool -fast_pf_fix_direct_spte (struct kvm_vcpu *vcpu,struct kvm_mmu_page *sp, +fast_pf_fix_direct_spte (struct kvm_vcpu *vcpu,struct kvm_page_fault *fault,u64 *sptep,u64 old_spte,u64 new_spte) { -gfn_t gfn; - -WARN_ON (!sp->role.direct); -/* * Theoretically we could also set dirty bit (and flush TLB) here in * order to eliminate unnecessary PML logging. See comments in
@@ -3119,14 +3114,8 @@ fast_pf_fix_direct_spte(struct kvm_vcpu *vcpu, struct kvm_mmu_page *sp,if (cmpxchg64 (sptep, old_spte, new_spte) != old_spte)return false; -if (is_writable_pte (new_spte) && !is_writable_pte (old_spte)) { -/* -* The gfn of direct spte is stable since it is -* calculated by sp->gfn. -*/ - gfn =kvm_mmu_page_get_gfn (sp, sptep - sp->spt); -kvm_vcpu_mark_page_dirty (vcpu, gfn); - } +if (is_writable_pte (new_spte) && !is_writable_pte (old_spte)) +mark_page_dirty_in_slot (vcpu->kvm, fault->slot, fault->gfn);return true; }
@@ -3251,7 +3240,7 @@ static int fast_page_fault(struct kvm_vcpu *vcpu, struct kvm_page_fault *fault)* since the gfn is not stable for indirect shadow page. See * Documentation/virt/kvm/locking.rst to get more detail. */ -if (fast_pf_fix_direct_spte (vcpu, sp, sptep, spte, new_spte)) { +if (fast_pf_fix_direct_spte (vcpu, fault, sptep, spte, new_spte)) { ret =RET_PF_FIXED ;break ; }
@@ -3863,7 +3852,7 @@ static bool kvm_arch_setup_async_pf(struct kvm_vcpu *vcpu, gpa_t cr2_or_gpa,static bool kvm_faultin_pfn (struct kvm_vcpu *vcpu,struct kvm_page_fault *fault,int *r) { -struct kvm_memory_slot *slot =kvm_vcpu_gfn_to_memslot (vcpu, fault->gfn); +struct kvm_memory_slot *slot = fault->slot;bool async;/*
@@ -3877,6 +3866,7 @@ static bool kvm_faultin_pfn(struct kvm_vcpu *vcpu, struct kvm_page_fault *fault,if (!kvm_is_visible_memslot (slot)) {/* Don't expose private memslots to L2. */ if (is_guest_mode (vcpu)) { + fault->slot =NULL ; fault->pfn =KVM_PFN_NOSLOT ; fault->map_writable = false;return false;
@@ -3928,6 +3918,8 @@ static int direct_page_fault(struct kvm_vcpu *vcpu, struct kvm_page_fault *faultint r; fault->gfn = fault->addr >>PAGE_SHIFT ; + fault->slot =kvm_vcpu_gfn_to_memslot (vcpu, fault->gfn); +if (page_fault_handle_page_track (vcpu, fault))return RET_PF_EMULATE ;
@@ -3955,7 +3947,7 @@ static int direct_page_fault(struct kvm_vcpu *vcpu, struct kvm_page_fault *faultelse write_lock (&vcpu->kvm->mmu_lock); -if (!is_noslot_pfn (fault->pfn) &&mmu_notifier_retry_hva (vcpu->kvm, mmu_seq, fault->hva)) +if (fault->slot &&mmu_notifier_retry_hva (vcpu->kvm, mmu_seq, fault->hva)) goto out_unlock; r =make_mmu_pages_available (vcpu);if (r)
arch/x86/kvm/mmu/paging_tmpl.h +4 -2
@@ -861,6 +861,8 @@ static int FNAME(page_fault)(struct kvm_vcpu *vcpu, struct kvm_page_fault *fault } fault->gfn = walker.gfn; + fault->slot =kvm_vcpu_gfn_to_memslot (vcpu, fault->gfn); +if (page_fault_handle_page_track (vcpu, fault)) {shadow_page_table_clear_flood (vcpu, fault->addr);return RET_PF_EMULATE ;
@@ -894,7 +896,7 @@ static int FNAME(page_fault)(struct kvm_vcpu *vcpu, struct kvm_page_fault *fault* we will cache the incorrect access into mmio spte. */ if (fault->write && !(walker.pte_access &ACC_WRITE_MASK ) && - !is_cr0_wp (vcpu->arch.mmu) && !fault->user && !is_noslot_pfn (fault->pfn)) { + !is_cr0_wp (vcpu->arch.mmu) && !fault->user && fault->slot) { walker.pte_access |=ACC_WRITE_MASK ; walker.pte_access &= ~ACC_USER_MASK ;
@@ -910,7 +912,7 @@ static int FNAME(page_fault)(struct kvm_vcpu *vcpu, struct kvm_page_fault *fault r =RET_PF_RETRY ;write_lock (&vcpu->kvm->mmu_lock); -if (!is_noslot_pfn (fault->pfn) &&mmu_notifier_retry_hva (vcpu->kvm, mmu_seq, fault->hva)) +if (fault->slot &&mmu_notifier_retry_hva (vcpu->kvm, mmu_seq, fault->hva)) goto out_unlock;kvm_mmu_audit (vcpu,AUDIT_PRE_PAGE_FAULT );
arch/x86/kvm/mmu/tdp_mmu.c +1 -1
@@ -900,7 +900,7 @@ static int tdp_mmu_map_handle_target_level(struct kvm_vcpu *vcpu,int ret =RET_PF_FIXED ;int make_spte_ret =0 ; -if (unlikely (is_noslot_pfn (fault->pfn))) +if (unlikely (!fault->slot)) new_spte =make_mmio_spte (vcpu, iter->gfn,ACC_ALL );else make_spte_ret = make_spte(vcpu, ACC_ALL, iter->level, iter->gfn,
Signature · 1 line
2.27.0
Paolo Bonzini
maintainerPATCH v3 21/31 KVM: x86/mmu: Avoid memslot lookup in page_fault_handle_page_track
In reply to Paolo BonziniFrom: David Matlack <[email protected]>
Now that kvm_page_fault has a pointer to the memslot it can be passed down to the page tracking code to avoid a redundant slot lookup.
No functional change intended.
Message-Id: <[email protected]>
--- arch/x86/include/asm/kvm_page_track.h | 2 ++ arch/x86/kvm/mmu/mmu.c | 2 +- arch/x86/kvm/mmu/page_track.c | 20 +++++++++++++------- 3 files changed, 16 insertions(+), 8 deletions(-)
arch/x86/include/asm/kvm_page_track.h +2 -0
@@ -61,6 +61,8 @@ void kvm_slot_page_track_remove_page(struct kvm *kvm, enum kvm_page_track_mode mode);bool kvm_page_track_is_active (struct kvm_vcpu *vcpu,gfn_t gfn,enum kvm_page_track_mode mode); +bool kvm_slot_page_track_is_active (struct kvm_memory_slot *slot,gfn_t gfn, +enum kvm_page_track_mode mode); void kvm_page_track_register_notifier(struct kvm *kvm,
arch/x86/kvm/mmu/mmu.c +1 -1
@@ -3819,7 +3819,7 @@ static bool page_fault_handle_page_track(struct kvm_vcpu *vcpu,* guest is writing the page which is write tracked which can * not be fixed by page fault handler. */ -if (kvm_page_track_is_active (vcpu, fault->gfn,KVM_PAGE_TRACK_WRITE )) +if (kvm_slot_page_track_is_active (fault->slot, fault->gfn,KVM_PAGE_TRACK_WRITE ))return true;return false;
arch/x86/kvm/mmu/page_track.c +13 -7
@@ -136,19 +136,14 @@ void kvm_slot_page_track_remove_page(struct kvm *kvm, }EXPORT_SYMBOL_GPL (kvm_slot_page_track_remove_page); -/* -* check if the corresponding access on the specified guest page is tracked. -*/ -bool kvm_page_track_is_active (struct kvm_vcpu *vcpu,gfn_t gfn, -enum kvm_page_track_mode mode) +bool kvm_slot_page_track_is_active (struct kvm_memory_slot *slot,gfn_t gfn, +enum kvm_page_track_mode mode) { -struct kvm_memory_slot *slot;int index;if (WARN_ON (!page_track_mode_is_valid (mode)))return false; - slot =kvm_vcpu_gfn_to_memslot (vcpu, gfn);if (!slot)return false;
@@ -156,6 +151,17 @@ bool kvm_page_track_is_active(struct kvm_vcpu *vcpu, gfn_t gfn,return !!READ_ONCE (slot->arch.gfn_track[mode][index]); } +/* +* check if the corresponding access on the specified guest page is tracked. +*/ +bool kvm_page_track_is_active (struct kvm_vcpu *vcpu,gfn_t gfn, +enum kvm_page_track_mode mode) +{ +struct kvm_memory_slot *slot =kvm_vcpu_gfn_to_memslot (vcpu, gfn); + +return kvm_slot_page_track_is_active (slot, gfn, mode); +} +void kvm_page_track_cleanup (struct kvm *kvm) {struct kvm_page_track_notifier_head *head;
Signature · 1 line
2.27.0
Paolo Bonzini
maintainerPATCH v3 22/31 KVM: MMU: inline set_spte in mmu_set_spte
In reply to Paolo BonziniSince the two callers of set_spte do different things with the results, inlining it actually makes the code simpler to reason about. For example, mmu_set_spte looks quite like tdp_mmu_map_handle_target_level, but the similarity is hidden by set_spte.
--- arch/x86/kvm/mmu/mmu.c | 31 ++++++++++++++++--------------- 1 file changed, 16 insertions(+), 15 deletions(-)
arch/x86/kvm/mmu/mmu.c +16 -15
@@ -2700,10 +2700,12 @@ static int mmu_set_spte(struct kvm_vcpu *vcpu, u64 *sptep, gfn_t gfn, kvm_pfn_t pfn, bool speculative, bool host_writable) { + struct kvm_mmu_page *sp = sptep_to_sp(sptep);int was_rmapped =0 ; -int set_spte_ret;int ret =RET_PF_FIXED ;bool flush = false; +int make_spte_ret; +u64 spte;pgprintk ("%s: spte %llx write_fault %d gfn %llx\n" , __func__, *sptep, write_fault, gfn);
@@ -2734,30 +2736,29 @@ static int mmu_set_spte(struct kvm_vcpu *vcpu, u64 *sptep, was_rmapped =1 ; } - set_spte_ret =set_spte (vcpu, sptep, pte_access, level, gfn, pfn, - speculative, true, host_writable); -if (set_spte_ret &SET_SPTE_WRITE_PROTECTED_PT ) { + make_spte_ret =make_spte (vcpu, pte_access, level, gfn, pfn, *sptep, speculative, + true, host_writable,sp_ad_disabled (sp), &spte); + +if (*sptep == spte) { + ret =RET_PF_SPURIOUS ; + }else { +trace_kvm_mmu_set_spte (level, gfn, sptep); + flush |=mmu_spte_update (sptep, spte); + } + +if (make_spte_ret &SET_SPTE_WRITE_PROTECTED_PT ) {if (write_fault) ret =RET_PF_EMULATE ; } -if (set_spte_ret &SET_SPTE_NEED_REMOTE_TLB_FLUSH || flush) +if (flush)kvm_flush_remote_tlbs_with_address (vcpu->kvm, gfn,KVM_PAGES_PER_HPAGE (level)); -/* -* The fault is fully spurious if and only if the new SPTE and old SPTE -* are identical, and emulation is not required. -*/ -if ((set_spte_ret &SET_SPTE_SPURIOUS ) && ret ==RET_PF_FIXED ) { -WARN_ON_ONCE (!was_rmapped); -return RET_PF_SPURIOUS ; - } -pgprintk ("%s: setting spte %llx\n" , __func__, *sptep); -trace_kvm_mmu_set_spte (level, gfn, sptep);if (!was_rmapped) { +WARN_ON_ONCE (ret ==RET_PF_SPURIOUS );kvm_update_page_stats (vcpu->kvm, level,1 );rmap_add (vcpu, sptep, gfn); }
Signature · 1 line
2.27.0
Paolo Bonzini
maintainerPATCH v3 23/31 KVM: MMU: inline set_spte in FNAME(sync_page)
In reply to Paolo BonziniSince the two callers of set_spte do different things with the results, inlining it actually makes the code simpler to reason about. For example, FNAME(sync_page) already has a struct kvm_mmu_page *, but set_spte had to fish it back out of sptep's private page data.
--- arch/x86/kvm/mmu/mmu.c | 21 --------------------- arch/x86/kvm/mmu/paging_tmpl.h | 21 ++++++++++++--------- 2 files changed, 12 insertions(+), 30 deletions(-)
arch/x86/kvm/mmu/mmu.c +0 -21
@@ -2674,27 +2674,6 @@ int mmu_try_to_unsync_pages(struct kvm_vcpu *vcpu, gfn_t gfn, bool can_unsync,return 0 ; } -static int set_spte (struct kvm_vcpu *vcpu,u64 *sptep, -unsigned int pte_access,int level, -gfn_t gfn,kvm_pfn_t pfn,bool speculative, -bool can_unsync,bool host_writable) -{ -u64 spte; -struct kvm_mmu_page *sp; -int ret; - - sp =sptep_to_sp (sptep); - - ret =make_spte (vcpu, pte_access, level, gfn, pfn, *sptep, speculative, - can_unsync, host_writable,sp_ad_disabled (sp), &spte); - -if (*sptep == spte) - ret |=SET_SPTE_SPURIOUS ; -else if (mmu_spte_update (sptep, spte)) - ret |=SET_SPTE_NEED_REMOTE_TLB_FLUSH ; -return ret; -} - static int mmu_set_spte(struct kvm_vcpu *vcpu, u64 *sptep, unsigned int pte_access, bool write_fault, int level, gfn_t gfn, kvm_pfn_t pfn, bool speculative,
arch/x86/kvm/mmu/paging_tmpl.h +12 -9
@@ -1061,7 +1061,7 @@ static int FNAME(sync_page)(struct kvm_vcpu *vcpu, struct kvm_mmu_page *sp)int i;bool host_writable;gpa_t first_pte_gpa; -int set_spte_ret =0 ; +bool flush = false;/* * Ignore various flags when verifying that it's safe to sync a shadow
@@ -1091,6 +1091,7 @@ static int FNAME(sync_page)(struct kvm_vcpu *vcpu, struct kvm_mmu_page *sp) first_pte_gpa =FNAME (get_level1_sp_gpa)(sp);for (i =0 ; i <PT64_ENT_PER_PAGE ; i++) { +u64 *sptep, spte;unsigned pte_access;pt_element_t gpte;gpa_t pte_gpa;
@@ -1106,7 +1107,7 @@ static int FNAME(sync_page)(struct kvm_vcpu *vcpu, struct kvm_mmu_page *sp)return -1 ;if (FNAME (prefetch_invalid_gpte)(vcpu, sp, &sp->spt[i], gpte)) { - set_spte_ret |=SET_SPTE_NEED_REMOTE_TLB_FLUSH ; + flush = true;continue ; }
@@ -1120,19 +1121,21 @@ static int FNAME(sync_page)(struct kvm_vcpu *vcpu, struct kvm_mmu_page *sp)if (gfn != sp->gfns[i]) {drop_spte (vcpu->kvm, &sp->spt[i]); - set_spte_ret |=SET_SPTE_NEED_REMOTE_TLB_FLUSH ; + flush = true;continue ; } - host_writable = sp->spt[i] & shadow_host_writable_mask; + sptep = &sp->spt[i]; + spte = *sptep; + host_writable = spte & shadow_host_writable_mask; +make_spte (vcpu, pte_access,PG_LEVEL_4K , gfn, +spte_to_pfn (spte), spte, true, false, + host_writable,sp_ad_disabled (sp), &spte); - set_spte_ret |=set_spte (vcpu, &sp->spt[i], - pte_access,PG_LEVEL_4K , - gfn,spte_to_pfn (sp->spt[i]), - true, false, host_writable); + flush |=mmu_spte_update (sptep, spte); } -return set_spte_ret &SET_SPTE_NEED_REMOTE_TLB_FLUSH ; +return flush; }#undef pt_element_t
Signature · 1 line
2.27.0
Paolo Bonzini
maintainerPATCH v3 24/31 KVM: MMU: clean up make_spte return value
In reply to Paolo BonziniNow that make_spte is called directly by the shadow MMU (rather than wrapped by set_spte), it only has to return one boolean value.
--- arch/x86/kvm/mmu/mmu.c | 8 ++++---- arch/x86/kvm/mmu/mmu_internal.h | 5 ----- arch/x86/kvm/mmu/spte.c | 8 ++++---- arch/x86/kvm/mmu/spte.h | 7 +------ arch/x86/kvm/mmu/tdp_mmu.c | 6 +++--- 5 files changed, 12 insertions(+), 22 deletions(-)
arch/x86/kvm/mmu/mmu.c +4 -4
@@ -2683,7 +2683,7 @@ static int mmu_set_spte(struct kvm_vcpu *vcpu, u64 *sptep,int was_rmapped =0 ;int ret =RET_PF_FIXED ;bool flush = false; -int make_spte_ret; +bool wrprot;u64 spte; pgprintk("%s: spte %llx write_fault %d gfn %llx\n", __func__,
@@ -2715,8 +2715,8 @@ static int mmu_set_spte(struct kvm_vcpu *vcpu, u64 *sptep, was_rmapped =1 ; } - make_spte_ret =make_spte (vcpu, pte_access, level, gfn, pfn, *sptep, speculative, - true, host_writable,sp_ad_disabled (sp), &spte); + wrprot =make_spte (vcpu, pte_access, level, gfn, pfn, *sptep, speculative, + true, host_writable,sp_ad_disabled (sp), &spte);if (*sptep == spte) { ret =RET_PF_SPURIOUS ;
@@ -2725,7 +2725,7 @@ static int mmu_set_spte(struct kvm_vcpu *vcpu, u64 *sptep, flush |=mmu_spte_update (sptep, spte); } -if (make_spte_ret &SET_SPTE_WRITE_PROTECTED_PT ) { +if (wrprot) {if (write_fault) ret =RET_PF_EMULATE ; }
arch/x86/kvm/mmu/mmu_internal.h +0 -5
@@ -150,11 +150,6 @@ enum { RET_PF_SPURIOUS, }; -/* Bits which may be returned by set_spte() */ -#define SET_SPTE_WRITE_PROTECTED_PT BIT(0) -#define SET_SPTE_NEED_REMOTE_TLB_FLUSH BIT(1) -#define SET_SPTE_SPURIOUS BIT(2) -int kvm_mmu_max_mapping_level (struct kvm *kvm,const struct kvm_memory_slot *slot,gfn_t gfn,kvm_pfn_t pfn,int max_level);
arch/x86/kvm/mmu/spte.c +4 -4
@@ -89,13 +89,13 @@ static bool kvm_is_mmio_pfn(kvm_pfn_t pfn) E820_TYPE_RAM); } -int make_spte (struct kvm_vcpu *vcpu,unsigned int pte_access,int level, +bool make_spte (struct kvm_vcpu *vcpu,unsigned int pte_access,int level,gfn_t gfn,kvm_pfn_t pfn,u64 old_spte,bool speculative,bool can_unsync,bool host_writable,bool ad_disabled,u64 *new_spte) {u64 spte =SPTE_MMU_PRESENT_MASK ; -int ret =0 ; +bool wrprot = false;if (ad_disabled) spte |=SPTE_TDP_AD_DISABLED_MASK ;
@@ -162,7 +162,7 @@ int make_spte(struct kvm_vcpu *vcpu, unsigned int pte_access, int level,if (mmu_try_to_unsync_pages (vcpu, gfn, can_unsync, speculative)) {pgprintk ("%s: found shadow page for %llx, marking ro\n" , __func__, gfn); - ret |=SET_SPTE_WRITE_PROTECTED_PT ; + wrprot = true; pte_access &= ~ACC_WRITE_MASK ; spte &= ~(PT_WRITABLE_MASK | shadow_mmu_writable_mask); }
@@ -183,7 +183,7 @@ int make_spte(struct kvm_vcpu *vcpu, unsigned int pte_access, int level,kvm_vcpu_mark_page_dirty (vcpu, gfn); *new_spte = spte; -return ret; +return wrprot; }u64 make_nonleaf_spte (u64 *child_pt,bool ad_disabled)
arch/x86/kvm/mmu/spte.h +1 -6
@@ -334,12 +334,7 @@ static inline u64 get_mmio_spte_generation(u64 spte)return gen; } -/* Bits which may be returned by set_spte() */ -#define SET_SPTE_WRITE_PROTECTED_PT BIT(0) -#define SET_SPTE_NEED_REMOTE_TLB_FLUSH BIT(1) -#define SET_SPTE_SPURIOUS BIT(2) - -int make_spte (struct kvm_vcpu *vcpu,unsigned int pte_access,int level, +bool make_spte (struct kvm_vcpu *vcpu,unsigned int pte_access,int level,gfn_t gfn,kvm_pfn_t pfn,u64 old_spte,bool speculative,bool can_unsync,bool host_writable,bool ad_disabled,u64 *new_spte);
arch/x86/kvm/mmu/tdp_mmu.c +3 -3
@@ -898,12 +898,12 @@ static int tdp_mmu_map_handle_target_level(struct kvm_vcpu *vcpu, {u64 new_spte;int ret =RET_PF_FIXED ; -int make_spte_ret =0 ; +bool wrprot = false;if (unlikely (!fault->slot)) new_spte =make_mmio_spte (vcpu, iter->gfn,ACC_ALL );else - make_spte_ret =make_spte (vcpu,ACC_ALL , iter->level, iter->gfn, + wrprot =make_spte (vcpu,ACC_ALL , iter->level, iter->gfn, fault->pfn, iter->old_spte, fault->prefault, true, fault->map_writable, !shadow_accessed_mask, &new_spte);
@@ -918,7 +918,7 @@ static int tdp_mmu_map_handle_target_level(struct kvm_vcpu *vcpu,* protected, emulation is needed. If the emulation was skipped, * the vCPU would have the same fault again. */ -if (make_spte_ret &SET_SPTE_WRITE_PROTECTED_PT ) { +if (wrprot) {if (fault->write) ret =RET_PF_EMULATE ; }
Signature · 1 line
2.27.0
Paolo Bonzini
maintainerPATCH v3 25/31 KVM: MMU: remove unnecessary argument to mmu_set_spte
In reply to Paolo BonziniThe level of the new SPTE can be found in the kvm_mmu_page struct; there is no need to pass it down.
--- arch/x86/kvm/mmu/mmu.c | 7 ++++--- arch/x86/kvm/mmu/paging_tmpl.h | 6 +++--- 2 files changed, 7 insertions(+), 6 deletions(-)
arch/x86/kvm/mmu/mmu.c +4 -3
@@ -2675,11 +2675,12 @@ int mmu_try_to_unsync_pages(struct kvm_vcpu *vcpu, gfn_t gfn, bool can_unsync, }static int mmu_set_spte (struct kvm_vcpu *vcpu,u64 *sptep, -unsigned int pte_access,bool write_fault,int level, +unsigned int pte_access,bool write_fault,gfn_t gfn,kvm_pfn_t pfn,bool speculative,bool host_writable) {struct kvm_mmu_page *sp =sptep_to_sp (sptep); +int level = sp->role.level;int was_rmapped =0 ;int ret =RET_PF_FIXED ;bool flush = false;
@@ -2777,7 +2778,7 @@ static int direct_pte_prefetch_many(struct kvm_vcpu *vcpu,return -1 ;for (i =0 ; i < ret; i++, gfn++, start++) { -mmu_set_spte (vcpu, start, access, false, sp->role.level, gfn, +mmu_set_spte (vcpu, start, access, false, gfn,page_to_pfn (pages[i]), true, true);put_page (pages[i]); }
@@ -2980,7 +2981,7 @@ static int __direct_map(struct kvm_vcpu *vcpu, struct kvm_page_fault *fault)return -EFAULT ; ret =mmu_set_spte (vcpu, it.sptep,ACC_ALL , - fault->write, fault->goal_level, base_gfn, fault->pfn, + fault->write, base_gfn, fault->pfn, fault->prefault, fault->map_writable);if (ret ==RET_PF_SPURIOUS )return ret;
arch/x86/kvm/mmu/paging_tmpl.h +3 -3
@@ -582,7 +582,7 @@ FNAME(prefetch_gpte)(struct kvm_vcpu *vcpu, struct kvm_mmu_page *sp,* we call mmu_set_spte() with host_writable = true because * pte_prefetch_gfn_to_pfn always gets a writable pfn. */ -mmu_set_spte (vcpu, spte, pte_access, false,PG_LEVEL_4K , gfn, pfn, +mmu_set_spte (vcpu, spte, pte_access, false, gfn, pfn, true, true);kvm_release_pfn_clean (pfn);
@@ -764,8 +764,8 @@ static int FNAME(fetch)(struct kvm_vcpu *vcpu, struct kvm_page_fault *fault,return -EFAULT ; ret =mmu_set_spte (vcpu, it.sptep, gw->pte_access, fault->write, - fault->goal_level, base_gfn, fault->pfn, - fault->prefault, fault->map_writable); + base_gfn, fault->pfn, fault->prefault, + fault->map_writable);if (ret ==RET_PF_SPURIOUS )return ret;
Signature · 1 line
2.27.0
Paolo Bonzini
maintainerPATCH v3 26/31 KVM: MMU: set ad_disabled in TDP MMU role
In reply to Paolo BonziniPrepare for removing the ad_disabled argument of make_spte; instead it can be found in the role of a struct kvm_mmu_page. First of all, the TDP MMU must set the role accurately.
--- arch/x86/kvm/mmu/tdp_mmu.c | 1 + 1 file changed, 1 insertion(+)
arch/x86/kvm/mmu/tdp_mmu.c +1 -0
@@ -167,6 +167,7 @@ static union kvm_mmu_page_role page_role_for_level(struct kvm_vcpu *vcpu, role.direct = true; role.gpte_is_8_bytes = true; role.access =ACC_ALL ; + role.ad_disabled = !shadow_accessed_mask;return role; }
Signature · 1 line
2.27.0
Paolo Bonzini
maintainerPATCH v3 27/31 KVM: MMU: pass kvm_mmu_page struct to make_spte
In reply to Paolo BonziniThe level and A/D bit support of the new SPTE can be found in the role, which is stored in the kvm_mmu_page struct. This merges two arguments into one.
For the TDP MMU, the kvm_mmu_page was not used (kvm_tdp_mmu_map does not use it if the SPTE is already present) so we fetch it just before calling make_spte.
--- arch/x86/kvm/mmu/mmu.c | 4 ++-- arch/x86/kvm/mmu/paging_tmpl.h | 4 ++-- arch/x86/kvm/mmu/spte.c | 11 ++++++----- arch/x86/kvm/mmu/spte.h | 8 ++++---- arch/x86/kvm/mmu/tdp_mmu.c | 7 ++++--- 5 files changed, 18 insertions(+), 16 deletions(-)
arch/x86/kvm/mmu/mmu.c +2 -2
@@ -2716,8 +2716,8 @@ static int mmu_set_spte(struct kvm_vcpu *vcpu, u64 *sptep, was_rmapped =1 ; } - wrprot =make_spte (vcpu, pte_access, level, gfn, pfn, *sptep, speculative, - true, host_writable,sp_ad_disabled (sp), &spte); + wrprot =make_spte (vcpu, sp, pte_access, gfn, pfn, *sptep, speculative, + true, host_writable, &spte);if (*sptep == spte) { ret =RET_PF_SPURIOUS ;
arch/x86/kvm/mmu/paging_tmpl.h +2 -2
@@ -1128,9 +1128,9 @@ static int FNAME(sync_page)(struct kvm_vcpu *vcpu, struct kvm_mmu_page *sp) sptep = &sp->spt[i]; spte = *sptep; host_writable = spte & shadow_host_writable_mask; -make_spte (vcpu, pte_access,PG_LEVEL_4K , gfn, +make_spte (vcpu, sp, pte_access, gfn,spte_to_pfn (spte), spte, true, false, - host_writable,sp_ad_disabled (sp), &spte); + host_writable, &spte); flush |=mmu_spte_update (sptep, spte); }
arch/x86/kvm/mmu/spte.c +6 -5
@@ -89,15 +89,16 @@ static bool kvm_is_mmio_pfn(kvm_pfn_t pfn) E820_TYPE_RAM); } -bool make_spte (struct kvm_vcpu *vcpu,unsigned int pte_access,int level, -gfn_t gfn,kvm_pfn_t pfn,u64 old_spte,bool speculative, -bool can_unsync,bool host_writable,bool ad_disabled, -u64 *new_spte) +bool make_spte (struct kvm_vcpu *vcpu,struct kvm_mmu_page *sp, +unsigned int pte_access,gfn_t gfn,kvm_pfn_t pfn, +u64 old_spte,bool speculative,bool can_unsync, +bool host_writable,u64 *new_spte) { +int level = sp->role.level;u64 spte =SPTE_MMU_PRESENT_MASK ;bool wrprot = false; -if (ad_disabled) +if (sp->role.ad_disabled) spte |=SPTE_TDP_AD_DISABLED_MASK ;else if (kvm_vcpu_ad_need_write_protect (vcpu)) spte |=SPTE_TDP_AD_WRPROT_ONLY_MASK ;
arch/x86/kvm/mmu/spte.h +4 -4
@@ -334,10 +334,10 @@ static inline u64 get_mmio_spte_generation(u64 spte)return gen; } -bool make_spte (struct kvm_vcpu *vcpu,unsigned int pte_access,int level, -gfn_t gfn,kvm_pfn_t pfn,u64 old_spte,bool speculative, -bool can_unsync,bool host_writable,bool ad_disabled, -u64 *new_spte); +bool make_spte (struct kvm_vcpu *vcpu,struct kvm_mmu_page *sp, +unsigned int pte_access,gfn_t gfn,kvm_pfn_t pfn, +u64 old_spte,bool speculative,bool can_unsync, +bool host_writable,u64 *new_spte);u64 make_nonleaf_spte (u64 *child_pt,bool ad_disabled);u64 make_mmio_spte (struct kvm_vcpu *vcpu,u64 gfn,unsigned int access);u64 mark_spte_for_access_track (u64 spte);
arch/x86/kvm/mmu/tdp_mmu.c +4 -3
@@ -897,17 +897,18 @@ static int tdp_mmu_map_handle_target_level(struct kvm_vcpu *vcpu,struct kvm_page_fault *fault, struct tdp_iter *iter) { + struct kvm_mmu_page *sp = sptep_to_sp(iter->sptep);u64 new_spte;int ret =RET_PF_FIXED ;bool wrprot = false; +WARN_ON (sp->role.level != fault->goal_level);if (unlikely (!fault->slot)) new_spte =make_mmio_spte (vcpu, iter->gfn,ACC_ALL );else - wrprot =make_spte (vcpu,ACC_ALL , iter->level, iter->gfn, + wrprot =make_spte (vcpu, sp,ACC_ALL , iter->gfn, fault->pfn, iter->old_spte, fault->prefault, true, - fault->map_writable, !shadow_accessed_mask, - &new_spte); + fault->map_writable, &new_spte);if (new_spte == iter->old_spte) ret =RET_PF_SPURIOUS ;
Signature · 1 line
2.27.0
Paolo Bonzini
maintainerPATCH v3 28/31 KVM: MMU: pass struct kvm_page_fault to mmu_set_spte
In reply to Paolo Bonzinimmu_set_spte is called for either PTE prefetching or page faults. The three boolean arguments write_fault, speculative and host_writable are always respectively false/true/true for prefetching and coming from a struct kvm_page_fault for page faults.
Let mmu_set_spte distinguish these two situation by accepting a possibly NULL struct kvm_page_fault argument.
--- arch/x86/kvm/mmu/mmu.c | 17 ++++++++++------- arch/x86/kvm/mmu/paging_tmpl.h | 13 +++---------- 2 files changed, 13 insertions(+), 17 deletions(-)
arch/x86/kvm/mmu/mmu.c +10 -7
@@ -2675,9 +2675,8 @@ int mmu_try_to_unsync_pages(struct kvm_vcpu *vcpu, gfn_t gfn, bool can_unsync, }static int mmu_set_spte (struct kvm_vcpu *vcpu,u64 *sptep, -unsigned int pte_access,bool write_fault, -gfn_t gfn,kvm_pfn_t pfn,bool speculative, -bool host_writable) +unsigned int pte_access,gfn_t gfn, +kvm_pfn_t pfn,struct kvm_page_fault *fault) {struct kvm_mmu_page *sp =sptep_to_sp (sptep);int level = sp->role.level;
@@ -2687,6 +2686,11 @@ static int mmu_set_spte(struct kvm_vcpu *vcpu, u64 *sptep,bool wrprot;u64 spte; +/* Prefetching always gets a writable pfn. */ +bool host_writable = !fault || fault->map_writable; +bool speculative = !fault || fault->prefault; +bool write_fault = fault && fault->write; +pgprintk ("%s: spte %llx write_fault %d gfn %llx\n" , __func__, *sptep, write_fault, gfn);
@@ -2778,8 +2782,8 @@ static int direct_pte_prefetch_many(struct kvm_vcpu *vcpu,return -1 ;for (i =0 ; i < ret; i++, gfn++, start++) { -mmu_set_spte (vcpu, start, access, false, gfn, -page_to_pfn (pages[i]), true, true); +mmu_set_spte (vcpu, start, access, gfn, +page_to_pfn (pages[i]),NULL );put_page (pages[i]); }
@@ -2981,8 +2985,7 @@ static int __direct_map(struct kvm_vcpu *vcpu, struct kvm_page_fault *fault)return -EFAULT ; ret =mmu_set_spte (vcpu, it.sptep,ACC_ALL , - fault->write, base_gfn, fault->pfn, - fault->prefault, fault->map_writable); + base_gfn, fault->pfn, fault);if (ret ==RET_PF_SPURIOUS )return ret;
arch/x86/kvm/mmu/paging_tmpl.h +3 -10
@@ -578,13 +578,7 @@ FNAME(prefetch_gpte)(struct kvm_vcpu *vcpu, struct kvm_mmu_page *sp,if (is_error_pfn (pfn))return false; -/* -* we call mmu_set_spte() with host_writable = true because -* pte_prefetch_gfn_to_pfn always gets a writable pfn. -*/ -mmu_set_spte (vcpu, spte, pte_access, false, gfn, pfn, - true, true); - +mmu_set_spte (vcpu, spte, pte_access, gfn, pfn,NULL );kvm_release_pfn_clean (pfn);return true; }
@@ -763,9 +757,8 @@ static int FNAME(fetch)(struct kvm_vcpu *vcpu, struct kvm_page_fault *fault,if (WARN_ON_ONCE (it.level != fault->goal_level))return -EFAULT ; - ret =mmu_set_spte (vcpu, it.sptep, gw->pte_access, fault->write, - base_gfn, fault->pfn, fault->prefault, - fault->map_writable); + ret =mmu_set_spte (vcpu, it.sptep, gw->pte_access, + base_gfn, fault->pfn, fault);if (ret ==RET_PF_SPURIOUS )return ret;
Signature · 1 line
2.27.0
Paolo Bonzini
maintainerPATCH v3 29/31 KVM: x86/mmu: Avoid memslot lookup in rmap_add
In reply to Paolo BonziniFrom: David Matlack <[email protected]>
Avoid the memslot lookup in rmap_add, by passing it down from the fault handling code to mmu_set_spte and then to rmap_add.
No functional change intended.
Message-Id: <[email protected]>
--- arch/x86/kvm/mmu/mmu.c | 27 +++++++-------------------- arch/x86/kvm/mmu/paging_tmpl.h | 12 +++++++++--- 2 files changed, 16 insertions(+), 23 deletions(-)
arch/x86/kvm/mmu/mmu.c +7 -20
@@ -1625,16 +1625,15 @@ static bool kvm_test_age_rmapp(struct kvm *kvm, struct kvm_rmap_head *rmap_head,#define RMAP_RECYCLE_THRESHOLD 1000 -static void rmap_add (struct kvm_vcpu *vcpu,u64 *spte,gfn_t gfn) +static void rmap_add (struct kvm_vcpu *vcpu,struct kvm_memory_slot *slot, +u64 *spte,gfn_t gfn) { -struct kvm_memory_slot *slot;struct kvm_mmu_page *sp;struct kvm_rmap_head *rmap_head;int rmap_count; sp =sptep_to_sp (spte);kvm_mmu_page_set_gfn (sp, spte - sp->spt, gfn); - slot =kvm_vcpu_gfn_to_memslot (vcpu, gfn); rmap_head =gfn_to_rmap (gfn, sp->role.level, slot); rmap_count =pte_list_add (vcpu, spte, rmap_head);
@@ -2674,8 +2673,8 @@ int mmu_try_to_unsync_pages(struct kvm_vcpu *vcpu, gfn_t gfn, bool can_unsync,return 0 ; } -static int mmu_set_spte (struct kvm_vcpu *vcpu,u64 *sptep, -unsigned int pte_access,gfn_t gfn, +static int mmu_set_spte (struct kvm_vcpu *vcpu,struct kvm_memory_slot *slot, +u64 *sptep,unsigned int pte_access,gfn_t gfn,kvm_pfn_t pfn,struct kvm_page_fault *fault) {struct kvm_mmu_page *sp =sptep_to_sp (sptep);
@@ -2744,24 +2743,12 @@ static int mmu_set_spte(struct kvm_vcpu *vcpu, u64 *sptep,if (!was_rmapped) {WARN_ON_ONCE (ret ==RET_PF_SPURIOUS );kvm_update_page_stats (vcpu->kvm, level,1 ); -rmap_add (vcpu, sptep, gfn); +rmap_add (vcpu, slot, sptep, gfn); }return ret; } -static kvm_pfn_t pte_prefetch_gfn_to_pfn (struct kvm_vcpu *vcpu,gfn_t gfn, -bool no_dirty_log) -{ -struct kvm_memory_slot *slot; - - slot =gfn_to_memslot_dirty_bitmap (vcpu, gfn, no_dirty_log); -if (!slot) -return KVM_PFN_ERR_FAULT ; - -return gfn_to_pfn_memslot_atomic (slot, gfn); -} -static int direct_pte_prefetch_many (struct kvm_vcpu *vcpu,struct kvm_mmu_page *sp,u64 *start,u64 *end)
@@ -2782,7 +2769,7 @@ static int direct_pte_prefetch_many(struct kvm_vcpu *vcpu,return -1 ;for (i =0 ; i < ret; i++, gfn++, start++) { -mmu_set_spte (vcpu, start, access, gfn, +mmu_set_spte (vcpu, slot, start, access, gfn,page_to_pfn (pages[i]),NULL );put_page (pages[i]); }
@@ -2984,7 +2971,7 @@ static int __direct_map(struct kvm_vcpu *vcpu, struct kvm_page_fault *fault)if (WARN_ON_ONCE (it.level != fault->goal_level))return -EFAULT ; - ret =mmu_set_spte (vcpu, it.sptep,ACC_ALL , + ret =mmu_set_spte (vcpu, fault->slot, it.sptep,ACC_ALL , base_gfn, fault->pfn, fault);if (ret ==RET_PF_SPURIOUS )return ret;
arch/x86/kvm/mmu/paging_tmpl.h +9 -3
@@ -561,6 +561,7 @@ static boolFNAME (prefetch_gpte)(struct kvm_vcpu *vcpu,struct kvm_mmu_page *sp,u64 *spte,pt_element_t gpte,bool no_dirty_log) { +struct kvm_memory_slot *slot;unsigned pte_access;gfn_t gfn;kvm_pfn_t pfn;
@@ -573,12 +574,17 @@ FNAME(prefetch_gpte)(struct kvm_vcpu *vcpu, struct kvm_mmu_page *sp, gfn =gpte_to_gfn (gpte); pte_access = sp->role.access &FNAME (gpte_access)(gpte);FNAME (protect_clean_gpte)(vcpu->arch.mmu, &pte_access, gpte); - pfn =pte_prefetch_gfn_to_pfn (vcpu, gfn, + + slot =gfn_to_memslot_dirty_bitmap (vcpu, gfn, no_dirty_log && (pte_access &ACC_WRITE_MASK )); +if (!slot) +return false; + + pfn =gfn_to_pfn_memslot_atomic (slot, gfn);if (is_error_pfn (pfn))return false; -mmu_set_spte (vcpu, spte, pte_access, gfn, pfn,NULL ); +mmu_set_spte (vcpu, slot, spte, pte_access, gfn, pfn,NULL );kvm_release_pfn_clean (pfn);return true; }
@@ -757,7 +763,7 @@ static int FNAME(fetch)(struct kvm_vcpu *vcpu, struct kvm_page_fault *fault,if (WARN_ON_ONCE (it.level != fault->goal_level))return -EFAULT ; - ret =mmu_set_spte (vcpu, it.sptep, gw->pte_access, + ret =mmu_set_spte (vcpu, fault->slot, it.sptep, gw->pte_access, base_gfn, fault->pfn, fault);if (ret ==RET_PF_SPURIOUS )return ret;
Signature · 1 line
2.27.0
Paolo Bonzini
maintainerPATCH v3 30/31 KVM: x86/mmu: Avoid memslot lookup in make_spte and mmu_try_to_unsync_pages
In reply to Paolo BonziniFrom: David Matlack <[email protected]>
mmu_try_to_unsync_pages checks if page tracking is active for the given gfn, which requires knowing the memslot. We can pass down the memslot via make_spte to avoid this lookup.
The memslot is also handy for make_spte's marking of the gfn as dirty:
we can test whether dirty page tracking is enabled, and if so ensure that pages are mapped as writable with 4K granularity. Apart from the warning, no functional change is intended.
Message-Id: <[email protected]>
--- arch/x86/include/asm/kvm_page_track.h | 2 -- arch/x86/kvm/mmu/mmu.c | 8 ++++---- arch/x86/kvm/mmu/mmu_internal.h | 4 ++-- arch/x86/kvm/mmu/page_track.c | 14 +++----------- arch/x86/kvm/mmu/paging_tmpl.h | 4 +++- arch/x86/kvm/mmu/spte.c | 10 +++++++--- arch/x86/kvm/mmu/spte.h | 1 + arch/x86/kvm/mmu/tdp_mmu.c | 2 +- 8 files changed, 21 insertions(+), 24 deletions(-)
arch/x86/include/asm/kvm_page_track.h +0 -2
@@ -59,8 +59,6 @@ void kvm_slot_page_track_add_page(struct kvm *kvm,void kvm_slot_page_track_remove_page (struct kvm *kvm,struct kvm_memory_slot *slot,gfn_t gfn,enum kvm_page_track_mode mode); -bool kvm_page_track_is_active (struct kvm_vcpu *vcpu,gfn_t gfn, -enum kvm_page_track_mode mode);bool kvm_slot_page_track_is_active (struct kvm_memory_slot *slot,gfn_t gfn,enum kvm_page_track_mode mode);
arch/x86/kvm/mmu/mmu.c +4 -4
@@ -2572,8 +2572,8 @@ static void kvm_unsync_page(struct kvm_vcpu *vcpu, struct kvm_mmu_page *sp)* were marked unsync (or if there is no shadow page), -EPERM if the SPTE must * be write-protected. */ -int mmu_try_to_unsync_pages (struct kvm_vcpu *vcpu,gfn_t gfn,bool can_unsync, -bool speculative) +int mmu_try_to_unsync_pages (struct kvm_vcpu *vcpu,struct kvm_memory_slot *slot, +gfn_t gfn,bool can_unsync,bool speculative) {struct kvm_mmu_page *sp;bool locked = false;
@@ -2583,7 +2583,7 @@ int mmu_try_to_unsync_pages(struct kvm_vcpu *vcpu, gfn_t gfn, bool can_unsync,* track machinery is used to write-protect upper-level shadow pages, * i.e. this guards the role.level == 4K assertion below! */ -if (kvm_page_track_is_active (vcpu, gfn,KVM_PAGE_TRACK_WRITE )) +if (kvm_slot_page_track_is_active (slot, gfn,KVM_PAGE_TRACK_WRITE ))return -EPERM ;/*
@@ -2719,7 +2719,7 @@ static int mmu_set_spte(struct kvm_vcpu *vcpu, struct kvm_memory_slot *slot, was_rmapped =1 ; } - wrprot =make_spte (vcpu, sp, pte_access, gfn, pfn, *sptep, speculative, + wrprot =make_spte (vcpu, sp, slot, pte_access, gfn, pfn, *sptep, speculative, true, host_writable, &spte);if (*sptep == spte) {
arch/x86/kvm/mmu/mmu_internal.h +2 -2
@@ -118,8 +118,8 @@ static inline bool kvm_vcpu_ad_need_write_protect(struct kvm_vcpu *vcpu) kvm_x86_ops.cpu_dirty_log_size; } -int mmu_try_to_unsync_pages (struct kvm_vcpu *vcpu,gfn_t gfn,bool can_unsync, -bool speculative); +int mmu_try_to_unsync_pages (struct kvm_vcpu *vcpu,struct kvm_memory_slot *slot, +gfn_t gfn,bool can_unsync,bool speculative);void kvm_mmu_gfn_disallow_lpage (const struct kvm_memory_slot *slot,gfn_t gfn);void kvm_mmu_gfn_allow_lpage (const struct kvm_memory_slot *slot,gfn_t gfn);
arch/x86/kvm/mmu/page_track.c +3 -11
@@ -136,6 +136,9 @@ void kvm_slot_page_track_remove_page(struct kvm *kvm, }EXPORT_SYMBOL_GPL (kvm_slot_page_track_remove_page); +/* +* check if the corresponding access on the specified guest page is tracked. +*/ bool kvm_slot_page_track_is_active (struct kvm_memory_slot *slot,gfn_t gfn,enum kvm_page_track_mode mode) {
@@ -151,17 +154,6 @@ bool kvm_slot_page_track_is_active(struct kvm_memory_slot *slot, gfn_t gfn,return !!READ_ONCE (slot->arch.gfn_track[mode][index]); } -/* -* check if the corresponding access on the specified guest page is tracked. -*/ -bool kvm_page_track_is_active (struct kvm_vcpu *vcpu,gfn_t gfn, -enum kvm_page_track_mode mode) -{ -struct kvm_memory_slot *slot =kvm_vcpu_gfn_to_memslot (vcpu, gfn); - -return kvm_slot_page_track_is_active (slot, gfn, mode); -} -void kvm_page_track_cleanup (struct kvm *kvm) {struct kvm_page_track_notifier_head *head;
arch/x86/kvm/mmu/paging_tmpl.h +3 -1
@@ -1091,6 +1091,7 @@ static int FNAME(sync_page)(struct kvm_vcpu *vcpu, struct kvm_mmu_page *sp)for (i =0 ; i <PT64_ENT_PER_PAGE ; i++) {u64 *sptep, spte; +struct kvm_memory_slot *slot;unsigned pte_access;pt_element_t gpte;gpa_t pte_gpa;
@@ -1127,7 +1128,8 @@ static int FNAME(sync_page)(struct kvm_vcpu *vcpu, struct kvm_mmu_page *sp) sptep = &sp->spt[i]; spte = *sptep; host_writable = spte & shadow_host_writable_mask; -make_spte (vcpu, sp, pte_access, gfn, + slot =kvm_vcpu_gfn_to_memslot (vcpu, gfn); +make_spte (vcpu, sp, slot, pte_access, gfn,spte_to_pfn (spte), spte, true, false, host_writable, &spte);
arch/x86/kvm/mmu/spte.c +7 -3
@@ -90,6 +90,7 @@ static bool kvm_is_mmio_pfn(kvm_pfn_t pfn) }bool make_spte (struct kvm_vcpu *vcpu,struct kvm_mmu_page *sp, +struct kvm_memory_slot *slot,unsigned int pte_access,gfn_t gfn,kvm_pfn_t pfn,u64 old_spte,bool speculative,bool can_unsync,bool host_writable,u64 *new_spte)
@@ -160,7 +161,7 @@ bool make_spte(struct kvm_vcpu *vcpu, struct kvm_mmu_page *sp,* e.g. it's write-tracked (upper-level SPs) or has one or more * shadow pages and unsync'ing pages is not allowed. */ -if (mmu_try_to_unsync_pages (vcpu, gfn, can_unsync, speculative)) { +if (mmu_try_to_unsync_pages (vcpu, slot, gfn, can_unsync, speculative)) {pgprintk ("%s: found shadow page for %llx, marking ro\n" , __func__, gfn); wrprot = true;
@@ -180,8 +181,11 @@ bool make_spte(struct kvm_vcpu *vcpu, struct kvm_mmu_page *sp,"spte = 0x%llx, level = %d, rsvd bits = 0x%llx" , spte, level, get_rsvd_bits(&vcpu->arch.mmu->shadow_zero_check, spte, level)); -if (spte &PT_WRITABLE_MASK ) -kvm_vcpu_mark_page_dirty (vcpu, gfn); +if ((spte &PT_WRITABLE_MASK ) &&kvm_slot_dirty_track_enabled (slot)) { +/* Enforced by kvm_mmu_hugepage_adjust. */ +WARN_ON (level >PG_LEVEL_4K ); +mark_page_dirty_in_slot (vcpu->kvm, slot, gfn); + } *new_spte = spte;return wrprot;
arch/x86/kvm/mmu/spte.h +1 -0
@@ -335,6 +335,7 @@ static inline u64 get_mmio_spte_generation(u64 spte) }bool make_spte (struct kvm_vcpu *vcpu,struct kvm_mmu_page *sp, +struct kvm_memory_slot *slot,unsigned int pte_access,gfn_t gfn,kvm_pfn_t pfn,u64 old_spte,bool speculative,bool can_unsync,bool host_writable,u64 *new_spte);
arch/x86/kvm/mmu/tdp_mmu.c +1 -1
@@ -906,7 +906,7 @@ static int tdp_mmu_map_handle_target_level(struct kvm_vcpu *vcpu,if (unlikely (!fault->slot)) new_spte =make_mmio_spte (vcpu, iter->gfn,ACC_ALL );else - wrprot =make_spte (vcpu, sp,ACC_ALL , iter->gfn, + wrprot =make_spte (vcpu, sp, fault->slot,ACC_ALL , iter->gfn, fault->pfn, iter->old_spte, fault->prefault, true, fault->map_writable, &new_spte);
Signature · 1 line
2.27.0
Paolo Bonzini
maintainerPATCH v3 31/31 KVM: MMU: make spte an in-out argument in make_spte
In reply to Paolo BonziniPass the old SPTE in the same variable that receives the new SPTE. This reduces the number of arguments from 11 to 10.
--- arch/x86/kvm/mmu/mmu.c | 18 ++++++++---------- arch/x86/kvm/mmu/paging_tmpl.h | 2 +- arch/x86/kvm/mmu/spte.c | 7 ++++--- arch/x86/kvm/mmu/spte.h | 2 +- arch/x86/kvm/mmu/tdp_mmu.c | 9 +++++---- 5 files changed, 19 insertions(+), 19 deletions(-)
arch/x86/kvm/mmu/mmu.c +8 -10
@@ -2682,8 +2682,8 @@ static int mmu_set_spte(struct kvm_vcpu *vcpu, struct kvm_memory_slot *slot,int was_rmapped =0 ;int ret =RET_PF_FIXED ;bool flush = false; +u64 spte = *sptep;bool wrprot; -u64 spte;/* Prefetching always gets a writable pfn. */ bool host_writable = !fault || fault->map_writable;
@@ -2691,35 +2691,33 @@ static int mmu_set_spte(struct kvm_vcpu *vcpu, struct kvm_memory_slot *slot,bool write_fault = fault && fault->write;pgprintk ("%s: spte %llx write_fault %d gfn %llx\n" , __func__, - *sptep, write_fault, gfn); + spte, write_fault, gfn);if (unlikely (is_noslot_pfn (pfn))) {mark_mmio_spte (vcpu, sptep, gfn, pte_access);return RET_PF_EMULATE ; } -if (is_shadow_present_pte (*sptep)) { +if (is_shadow_present_pte (spte)) {/* * If we overwrite a PTE page pointer with a 2MB PMD, unlink * the parent of the now unreachable PTE. */ -if (level >PG_LEVEL_4K && !is_large_pte (*sptep)) { +if (level >PG_LEVEL_4K && !is_large_pte (spte)) {struct kvm_mmu_page *child; -u64 pte = *sptep; - - child =to_shadow_page (pte &PT64_BASE_ADDR_MASK ); + child =to_shadow_page (spte &PT64_BASE_ADDR_MASK );drop_parent_pte (child, sptep); flush = true; - }else if (pfn !=spte_to_pfn (*sptep)) { + }else if (pfn !=spte_to_pfn (spte)) {pgprintk ("hfn old %llx new %llx\n" , -spte_to_pfn (*sptep), pfn); +spte_to_pfn (spte), pfn);drop_spte (vcpu->kvm, sptep); flush = true; }else was_rmapped =1 ; } - wrprot =make_spte (vcpu, sp, slot, pte_access, gfn, pfn, *sptep, speculative, + wrprot =make_spte (vcpu, sp, slot, pte_access, gfn, pfn, speculative, true, host_writable, &spte);if (*sptep == spte) {
arch/x86/kvm/mmu/paging_tmpl.h +1 -1
@@ -1130,7 +1130,7 @@ static int FNAME(sync_page)(struct kvm_vcpu *vcpu, struct kvm_mmu_page *sp) host_writable = spte & shadow_host_writable_mask; slot =kvm_vcpu_gfn_to_memslot (vcpu, gfn);make_spte (vcpu, sp, slot, pte_access, gfn, -spte_to_pfn (spte), spte, true, false, +spte_to_pfn (spte), true, false, host_writable, &spte); flush |=mmu_spte_update (sptep, spte);
arch/x86/kvm/mmu/spte.c +4 -3
@@ -92,10 +92,11 @@ static bool kvm_is_mmio_pfn(kvm_pfn_t pfn)bool make_spte (struct kvm_vcpu *vcpu,struct kvm_mmu_page *sp,struct kvm_memory_slot *slot,unsigned int pte_access,gfn_t gfn,kvm_pfn_t pfn, -u64 old_spte,bool speculative,bool can_unsync, -bool host_writable,u64 *new_spte) +bool speculative,bool can_unsync, +bool host_writable,u64 *sptep) {int level = sp->role.level; +u64 old_spte = *sptep;u64 spte =SPTE_MMU_PRESENT_MASK ;bool wrprot = false;
@@ -187,7 +188,7 @@ bool make_spte(struct kvm_vcpu *vcpu, struct kvm_mmu_page *sp,mark_page_dirty_in_slot (vcpu->kvm, slot, gfn); } - *new_spte = spte; + *sptep = spte;return wrprot; }
arch/x86/kvm/mmu/spte.h +1 -1
@@ -337,7 +337,7 @@ static inline u64 get_mmio_spte_generation(u64 spte)bool make_spte (struct kvm_vcpu *vcpu,struct kvm_mmu_page *sp,struct kvm_memory_slot *slot,unsigned int pte_access,gfn_t gfn,kvm_pfn_t pfn, -u64 old_spte,bool speculative,bool can_unsync, +bool speculative,bool can_unsync,bool host_writable,u64 *new_spte);u64 make_nonleaf_spte (u64 *child_pt,bool ad_disabled);u64 make_mmio_spte (struct kvm_vcpu *vcpu,u64 gfn,unsigned int access);
arch/x86/kvm/mmu/tdp_mmu.c +5 -4
@@ -903,13 +903,14 @@ static int tdp_mmu_map_handle_target_level(struct kvm_vcpu *vcpu,bool wrprot = false;WARN_ON (sp->role.level != fault->goal_level); -if (unlikely (!fault->slot)) +if (unlikely (!fault->slot)) { new_spte =make_mmio_spte (vcpu, iter->gfn,ACC_ALL ); -else + }else { + new_spte = iter->old_spte; wrprot =make_spte (vcpu, sp, fault->slot,ACC_ALL , iter->gfn, - fault->pfn, iter->old_spte, fault->prefault, true, + fault->pfn, fault->prefault, true, fault->map_writable, &new_spte); - + }if (new_spte == iter->old_spte) ret =RET_PF_SPURIOUS ;else if (!tdp_mmu_set_spte_atomic (vcpu->kvm, iter, new_spte))
Signature · 1 line
2.27.0
Re: [PATCH v3 31/31] KVM: MMU: make spte an in-out argument in make_spte
Quoted 87 lines · Paolo Bonzini
Pass the old SPTE in the same variable that receives the new SPTE. This reduces the number of arguments from 11 to 10.
Signed-off-by Paolo Bonzini <[email protected]>--- arch/x86/kvm/mmu/mmu.c | 18 ++++++++---------- arch/x86/kvm/mmu/paging_tmpl.h | 2 +- arch/x86/kvm/mmu/spte.c | 7 ++++--- arch/x86/kvm/mmu/spte.h | 2 +- arch/x86/kvm/mmu/tdp_mmu.c | 9 +++++---- 5 files changed, 19 insertions(+), 19 deletions(-)arch/x86/kvm/mmu/mmu.c +8
-10@@ -2682,8 +2682,8 @@ static int mmu_set_spte(struct kvm_vcpu *vcpu, struct kvm_memory_slot *slot,int was_rmapped =0 ;int ret =RET_PF_FIXED ;bool flush = false; +u64 spte = *sptep;bool wrprot; -u64 spte;/* Prefetching always gets a writable pfn. */ bool host_writable = !fault || fault->map_writable;@@ -2691,35 +2691,33 @@ static int mmu_set_spte(struct kvm_vcpu *vcpu, struct kvm_memory_slot *slot,bool write_fault = fault && fault->write;pgprintk ("%s: spte %llx write_fault %d gfn %llx\n" , __func__, - *sptep, write_fault, gfn); + spte, write_fault, gfn);if (unlikely (is_noslot_pfn (pfn))) {mark_mmio_spte (vcpu, sptep, gfn, pte_access);return RET_PF_EMULATE ; } -if (is_shadow_present_pte (*sptep)) { +if (is_shadow_present_pte (spte)) {/* * If we overwrite a PTE page pointer with a 2MB PMD, unlink * the parent of the now unreachable PTE. */ -if (level >PG_LEVEL_4K && !is_large_pte (*sptep)) { +if (level >PG_LEVEL_4K && !is_large_pte (spte)) {struct kvm_mmu_page *child; -u64 pte = *sptep; - - child =to_shadow_page (pte &PT64_BASE_ADDR_MASK ); + child =to_shadow_page (spte &PT64_BASE_ADDR_MASK );drop_parent_pte (child, sptep); flush = true; - }else if (pfn !=spte_to_pfn (*sptep)) { + }else if (pfn !=spte_to_pfn (spte)) {pgprintk ("hfn old %llx new %llx\n" , -spte_to_pfn (*sptep), pfn); +spte_to_pfn (spte), pfn);drop_spte (vcpu->kvm, sptep); flush = true; }else was_rmapped =1 ; } - wrprot =make_spte (vcpu, sp, slot, pte_access, gfn, pfn, *sptep, speculative, + wrprot =make_spte (vcpu, sp, slot, pte_access, gfn, pfn, speculative, true, host_writable, &spte);if (*sptep == spte) {arch/x86/kvm/mmu/paging_tmpl.h +1
-1@@ -1130,7 +1130,7 @@ static int FNAME(sync_page)(struct kvm_vcpu *vcpu, struct kvm_mmu_page *sp) host_writable = spte & shadow_host_writable_mask; slot =kvm_vcpu_gfn_to_memslot (vcpu, gfn);make_spte (vcpu, sp, slot, pte_access, gfn, -spte_to_pfn (spte), spte, true, false, +spte_to_pfn (spte), true, false, host_writable, &spte); flush |=mmu_spte_update (sptep, spte);arch/x86/kvm/mmu/spte.c +0
-0
arch/x86/kvm/mmu/spte.c static bool kvm_is_mmio_pfn(kvm_pfn_t pfn) · line 96
Quoted 7 lines
arch/x86/kvm/mmu/spte.c +2
-2@@ -92,10 +92,11 @@ static bool kvm_is_mmio_pfn(kvm_pfn_t pfn)bool make_spte (struct kvm_vcpu *vcpu,struct kvm_mmu_page *sp,struct kvm_memory_slot *slot,unsigned int pte_access,gfn_t gfn,kvm_pfn_t pfn, -u64 old_spte,bool speculative,bool can_unsync, -bool host_writable,u64 *new_spte) +bool speculative,bool can_unsync, +bool host_writable,u64 *sptep)
I'd prefer a different name since sptep has specific meaning throughout the mmu code. (It's the address of the spte in the page table.)
Case in point, I was going to suggest we can get rid of struct kvm_mmu_page since it can be derived from the sptep and then realized how wrong that was :).
Instead of receiving the new spte as a parameter what do you think about changing make_spte to return the new spte? I think that would make the code more readable (but won't reduce the number of arguments because you'd have to add wrprot).
Quoted 54 lines
arch/x86/kvm/mmu/spte.c +2
-1{int level = sp->role.level; +u64 old_spte = *sptep;u64 spte =SPTE_MMU_PRESENT_MASK ;bool wrprot = false;@@ -187,7 +188,7 @@ bool make_spte(struct kvm_vcpu *vcpu, struct kvm_mmu_page *sp,mark_page_dirty_in_slot (vcpu->kvm, slot, gfn); } - *new_spte = spte; + *sptep = spte;return wrprot; }arch/x86/kvm/mmu/spte.h +1
-1@@ -337,7 +337,7 @@ static inline u64 get_mmio_spte_generation(u64 spte)bool make_spte (struct kvm_vcpu *vcpu,struct kvm_mmu_page *sp,struct kvm_memory_slot *slot,unsigned int pte_access,gfn_t gfn,kvm_pfn_t pfn, -u64 old_spte,bool speculative,bool can_unsync, +bool speculative,bool can_unsync,bool host_writable,u64 *new_spte);u64 make_nonleaf_spte (u64 *child_pt,bool ad_disabled);u64 make_mmio_spte (struct kvm_vcpu *vcpu,u64 gfn,unsigned int access);arch/x86/kvm/mmu/tdp_mmu.c +5
-4@@ -903,13 +903,14 @@ static int tdp_mmu_map_handle_target_level(struct kvm_vcpu *vcpu,bool wrprot = false;WARN_ON (sp->role.level != fault->goal_level); -if (unlikely (!fault->slot)) +if (unlikely (!fault->slot)) { new_spte =make_mmio_spte (vcpu, iter->gfn,ACC_ALL ); -else + }else { + new_spte = iter->old_spte; wrprot =make_spte (vcpu, sp, fault->slot,ACC_ALL , iter->gfn, - fault->pfn, iter->old_spte, fault->prefault, true, + fault->pfn, fault->prefault, true, fault->map_writable, &new_spte); - + }if (new_spte == iter->old_spte) ret =RET_PF_SPURIOUS ;else if (!tdp_mmu_set_spte_atomic (vcpu->kvm, iter, new_spte))Signature · 1 line
2.27.0
Paolo Bonzini
maintainerRe: [PATCH v3 31/31] KVM: MMU: make spte an in-out argument in make_spte
Quoted 11 lines · David Matlack
arch/x86/kvm/mmu/spte.c static bool kvm_is_mmio_pfn(kvm_pfn_t pfn) · line 96
Quoted 7 lines
bool make_spte(struct
kvm_vcpu*vcpu, structkvm_mmu_page*sp,
structkvm_memory_slot*slot,
unsigned intpte_access,gfn_tgfn,kvm_pfn_tpfn,arch/x86/kvm/mmu/spte.c +2
-2- u64 old_spte, bool speculative, bool can_unsync, - bool host_writable, u64 *new_spte) + bool speculative, bool can_unsync, + bool host_writable, u64 *sptep)I'd prefer a different name since
sptephas specific meaning throughout the mmu code. (It's the address of the spte in the page table.)
Quoted 9 lines
Case in point, I was going to suggest we can get rid of struct
kvm_mmu_pagesince it can be derived from the sptep and then realized how wrong that was:).Instead of receiving the new spte as a parameter what do you think about changing
make_spteto return the new spte? I think that would make the code more readable (but won't reduce the number of arguments because you'd have to add wrprot).
You have a point. I've dropped this patch for now.
Paolo
Quoted 14 lines · Paolo Bonzini
The current kvm page fault handlers passes around many arguments to the functions. To simplify those arguments and local variables, introduce a data structure, struct
kvm_page_fault, to hold those arguments and variables. structkvm_page_faultis allocated on stack on the caller of kvm fault handler,kvm_mmu_do_page_fault(), and passed around.Later in the series, my patches are interleaved with David's work to add the memory slot to the struct and avoid repeated lookups. Along the way you will find some cleanups of functions with a ludicrous number of arguments, so that they use struct
kvm_page_faultas much as possible or at least receive related information from a single argument.make_sptein particular goes from 11 to 10 arguments (yeah I know) despite gaining two forkvm_mmu_pageandkvm_memory_slot.
Quoted 5 lines
This can be sometimes a bit debatable (for example struct
kvm_mmu_pageis used a little more on the TDP MMU paths), but overall I think the result is an improvement. For example the SET_SPTE_* constants go away, and they absolutely didn't belong in the TDP MMU. But if you disagree with some of the changes, please speak up loudly!
Thanks for getting this cleaned up and sent out. The series looks good overall. I had 2 small comments but otherwise:
Quoted 67 lines
Testing: survives kvm-unit-tests on Intel with all of ept=0, ept=1 tdp_mmu=0, ept=1. Will do more before committing to it in kvm/next of course.
Paolo
David Matlack (5):
KVM: x86/mmu: Foldrmap_recycleintormap_add
KVM: x86/mmu: Pass the memslot around via structkvm_page_fault
KVM: x86/mmu: Avoid memslot lookup inpage_fault_handle_page_track
KVM: x86/mmu: Avoid memslot lookup inrmap_add
KVM: x86/mmu: Avoid memslot lookup inmake_spteand
mmu_try_to_unsync_pagesPaolo Bonzini (25):
KVM: MMU: pass unadulterated gpa todirect_page_fault
KVM: MMU: Introduce structkvm_page_fault
KVM: MMU: changemmu->page_fault()arguments tokvm_page_fault
KVM: MMU: changedirect_page_fault()arguments tokvm_page_fault
KVM: MMU: changepage_fault_handle_page_track()arguments to
kvm_page_fault
KVM: MMU: changekvm_faultin_pfn()arguments tokvm_page_fault
KVM: MMU: changehandle_abnormal_pfn()arguments tokvm_page_fault
KVM: MMU: change__direct_map()arguments tokvm_page_fault
KVM: MMU: change FNAME(fetch)() arguments tokvm_page_fault
KVM: MMU: changekvm_tdp_mmu_map()arguments tokvm_page_fault
KVM: MMU: changetdp_mmu_map_handle_target_level()arguments to
kvm_page_fault
KVM: MMU: changefast_page_fault()arguments tokvm_page_fault
KVM: MMU: changekvm_mmu_hugepage_adjust()arguments tokvm_page_fault
KVM: MMU: changedisallowed_hugepage_adjust()arguments to
kvm_page_fault
KVM: MMU: change tracepoints arguments tokvm_page_fault
KVM: MMU: mark page dirty inmake_spte
KVM: MMU: unifytdp_mmu_map_set_spte_atomicand
tdp_mmu_set_spte_atomic_no_dirty_log
KVM: MMU: inlineset_spteinmmu_set_spte
KVM: MMU: inlineset_spteinFNAME(sync_page)
KVM: MMU: clean upmake_sptereturn value
KVM: MMU: remove unnecessary argument tommu_set_spte
KVM: MMU: setad_disabledin TDP MMU role
KVM: MMU: passkvm_mmu_pagestruct tomake_spte
KVM: MMU: pass structkvm_page_faulttommu_set_spte
KVM: MMU: make spte an in-out argument inmake_spteSean Christopherson (1):
KVM: x86/mmu: Verify shadow walk doesn't terminate early in page
faultsarch/x86/include/asm/kvm_host.h | 4 +- arch/x86/include/asm/kvm_page_track.h | 4 +- arch/x86/kvm/mmu.h | 84 +++++- arch/x86/kvm/mmu/mmu.c | 408 +++++++++++--------------- arch/x86/kvm/mmu/mmu_internal.h | 22 +- arch/x86/kvm/mmu/mmutrace.h | 18 +- arch/x86/kvm/mmu/page_track.c | 6 +- arch/x86/kvm/mmu/paging_tmpl.h | 137 +++++---- arch/x86/kvm/mmu/spte.c | 29 +- arch/x86/kvm/mmu/spte.h | 14 +- arch/x86/kvm/mmu/tdp_mmu.c | 123 +++----- arch/x86/kvm/mmu/tdp_mmu.h | 4 +- 12 files changed, 390 insertions(+), 463 deletions(-)Signature · 1 line
2.27.0