Re: [PATCH v2 14/18] KVM: x86/mmu: avoid indirect call for get_cr3

From: Paolo Bonzini
Date: Sat Feb 19 2022 - 05:03:57 EST


On 2/18/22 21:30, Sean Christopherson wrote:
On Thu, Feb 17, 2022, Paolo Bonzini wrote:
Most of the time, calls to get_guest_pgd result in calling
kvm_read_cr3 (the exception is only nested TDP). Hardcode
the default instead of using the get_cr3 function, avoiding
a retpoline if they are enabled.

Signed-off-by: Paolo Bonzini <pbonzini@xxxxxxxxxx>
---
arch/x86/kvm/mmu.h | 13 +++++++++++++
arch/x86/kvm/mmu/mmu.c | 15 +++++----------
arch/x86/kvm/mmu/paging_tmpl.h | 2 +-
arch/x86/kvm/x86.c | 2 +-
4 files changed, 20 insertions(+), 12 deletions(-)

diff --git a/arch/x86/kvm/mmu.h b/arch/x86/kvm/mmu.h
index 1d0c1904d69a..1808d6814ddb 100644
--- a/arch/x86/kvm/mmu.h
+++ b/arch/x86/kvm/mmu.h
@@ -116,6 +116,19 @@ static inline void kvm_mmu_load_pgd(struct kvm_vcpu *vcpu)
vcpu->arch.mmu->shadow_root_level);
}
+static inline gpa_t __kvm_mmu_get_guest_pgd(struct kvm_vcpu *vcpu, struct kvm_mmu *mmu)
+{

I'd prefer to do what we do for page faults. That approach avoids the need for a
comment to document NULL and avoids a conditional when RETPOLINE is not enabled.

Might be worth renaming get_cr3 => get_guest_cr3 though.

I did it this way to avoid a slightly gratuitous extern function just because kvm_mmu_get_guest_pgd and kvm_read_cr3 are both inline. But at least it's not an export since there are no callers in vmx/svm, so it's okay to do it as you suggested.

#ifdef CONFIG_RETPOLINE
if (mmu->get_guest_pgd = get_guest_cr3)
return kvm_read_cr3(vcpu);
#endif
return mmu->get_guest_pgd(vcpu);


+ if (!mmu->get_guest_pgd)
+ return kvm_read_cr3(vcpu);
+ else
+ return mmu->get_guest_pgd(vcpu);
+}
+
+static inline gpa_t kvm_mmu_get_guest_pgd(struct kvm_vcpu *vcpu)
+{
+ return __kvm_mmu_get_guest_pgd(vcpu, vcpu->arch.mmu);

I'd much prefer we don't provide an @vcpu-only variant and force the caller to
provide the mmu.

No problem.

Paolo