From: Chenyi Qiang <chenyi.qiang@intel.com>
To: Paolo Bonzini <pbonzini@redhat.com>,
Sean Christopherson <seanjc@google.com>,
Vitaly Kuznetsov <vkuznets@redhat.com>,
Wanpeng Li <wanpengli@tencent.com>,
Jim Mattson <jmattson@google.com>, Joerg Roedel <joro@8bytes.org>,
Xiaoyao Li <xiaoyao.li@intel.com>
Cc: Chenyi Qiang <chenyi.qiang@intel.com>,
kvm@vger.kernel.org, linux-kernel@vger.kernel.org
Subject: [PATCH v5 2/7] KVM: VMX: Add proper cache tracking for PKRS
Date: Wed, 11 Aug 2021 18:11:21 +0800 [thread overview]
Message-ID: <20210811101126.8973-3-chenyi.qiang@intel.com> (raw)
In-Reply-To: <20210811101126.8973-1-chenyi.qiang@intel.com>
Add PKRS caching into the standard register caching mechanism in order
to take advantage of the availability checks provided by regs_avail.
This is because vcpu->arch.pkrs will be rarely acceesed by KVM, only in
the case of host userspace MSR reads and GVA->GPA translation in
following patches. It is unnecessary to keep it up-to-date at all times.
Signed-off-by: Chenyi Qiang <chenyi.qiang@intel.com>
---
arch/x86/include/asm/kvm_host.h | 2 ++
arch/x86/kvm/kvm_cache_regs.h | 7 +++++++
arch/x86/kvm/vmx/vmx.c | 4 ++++
arch/x86/kvm/vmx/vmx.h | 3 ++-
4 files changed, 15 insertions(+), 1 deletion(-)
diff --git a/arch/x86/include/asm/kvm_host.h b/arch/x86/include/asm/kvm_host.h
index 974cbfb1eefe..c2bcb88781b3 100644
--- a/arch/x86/include/asm/kvm_host.h
+++ b/arch/x86/include/asm/kvm_host.h
@@ -173,6 +173,7 @@ enum kvm_reg {
VCPU_EXREG_SEGMENTS,
VCPU_EXREG_EXIT_INFO_1,
VCPU_EXREG_EXIT_INFO_2,
+ VCPU_EXREG_PKRS,
};
enum {
@@ -620,6 +621,7 @@ struct kvm_vcpu_arch {
unsigned long cr8;
u32 host_pkru;
u32 pkru;
+ u32 pkrs;
u32 hflags;
u64 efer;
u64 apic_base;
diff --git a/arch/x86/kvm/kvm_cache_regs.h b/arch/x86/kvm/kvm_cache_regs.h
index 90e1ffdc05b7..da014b1be874 100644
--- a/arch/x86/kvm/kvm_cache_regs.h
+++ b/arch/x86/kvm/kvm_cache_regs.h
@@ -171,6 +171,13 @@ static inline u64 kvm_read_edx_eax(struct kvm_vcpu *vcpu)
| ((u64)(kvm_rdx_read(vcpu) & -1u) << 32);
}
+static inline ulong kvm_read_pkrs(struct kvm_vcpu *vcpu)
+{
+ if (!kvm_register_is_available(vcpu, VCPU_EXREG_PKRS))
+ static_call(kvm_x86_cache_reg)(vcpu, VCPU_EXREG_PKRS);
+ return vcpu->arch.pkrs;
+}
+
static inline void enter_guest_mode(struct kvm_vcpu *vcpu)
{
vcpu->arch.hflags |= HF_GUEST_MASK;
diff --git a/arch/x86/kvm/vmx/vmx.c b/arch/x86/kvm/vmx/vmx.c
index 927a552393b9..bf911029aa35 100644
--- a/arch/x86/kvm/vmx/vmx.c
+++ b/arch/x86/kvm/vmx/vmx.c
@@ -2273,6 +2273,10 @@ static void vmx_cache_reg(struct kvm_vcpu *vcpu, enum kvm_reg reg)
vcpu->arch.cr4 &= ~guest_owned_bits;
vcpu->arch.cr4 |= vmcs_readl(GUEST_CR4) & guest_owned_bits;
break;
+ case VCPU_EXREG_PKRS:
+ if (kvm_cpu_cap_has(X86_FEATURE_PKS))
+ vcpu->arch.pkrs = vmcs_read64(GUEST_IA32_PKRS);
+ break;
default:
WARN_ON_ONCE(1);
break;
diff --git a/arch/x86/kvm/vmx/vmx.h b/arch/x86/kvm/vmx/vmx.h
index db88ed4f2121..18039eb6efb0 100644
--- a/arch/x86/kvm/vmx/vmx.h
+++ b/arch/x86/kvm/vmx/vmx.h
@@ -447,7 +447,8 @@ static inline void vmx_register_cache_reset(struct kvm_vcpu *vcpu)
| (1 << VCPU_EXREG_CR3)
| (1 << VCPU_EXREG_CR4)
| (1 << VCPU_EXREG_EXIT_INFO_1)
- | (1 << VCPU_EXREG_EXIT_INFO_2));
+ | (1 << VCPU_EXREG_EXIT_INFO_2)
+ | (1 << VCPU_EXREG_PKRS));
vcpu->arch.regs_dirty = 0;
}
--
2.17.1
next prev parent reply other threads:[~2021-08-11 10:07 UTC|newest]
Thread overview: 22+ messages / expand[flat|nested] mbox.gz Atom feed top
2021-08-11 10:11 [PATCH v5 0/7] KVM: PKS Virtualization support Chenyi Qiang
2021-08-11 10:11 ` [PATCH v5 1/7] KVM: VMX: Introduce PKS VMCS fields Chenyi Qiang
2021-08-11 10:11 ` Chenyi Qiang [this message]
2021-11-08 17:13 ` [PATCH v5 2/7] KVM: VMX: Add proper cache tracking for PKRS Sean Christopherson
2021-11-08 18:07 ` Sean Christopherson
2021-08-11 10:11 ` [PATCH v5 3/7] KVM: X86: Expose IA32_PKRS MSR Chenyi Qiang
2021-11-08 17:44 ` Sean Christopherson
2021-11-09 5:54 ` Chenyi Qiang
2021-11-09 15:30 ` Sean Christopherson
2021-11-10 0:56 ` Chenyi Qiang
2021-11-08 20:18 ` Sean Christopherson
2021-08-11 10:11 ` [PATCH v5 4/7] KVM: MMU: Rename the pkru to pkr Chenyi Qiang
2021-08-11 10:11 ` [PATCH v5 5/7] KVM: MMU: Add support for PKS emulation Chenyi Qiang
2021-11-08 19:46 ` Sean Christopherson
2021-11-09 6:42 ` Chenyi Qiang
2021-11-08 19:52 ` Sean Christopherson
2021-08-11 10:11 ` [PATCH v5 6/7] KVM: VMX: Expose PKS to guest Chenyi Qiang
2021-11-08 21:31 ` Sean Christopherson
2021-08-11 10:11 ` [PATCH v5 7/7] KVM: VMX: Enable PKS for nested VM Chenyi Qiang
2021-08-26 2:04 ` [PATCH v5 0/7] KVM: PKS Virtualization support Chenyi Qiang
2021-10-25 15:12 ` Paolo Bonzini
2021-10-26 3:14 ` Chenyi Qiang
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20210811101126.8973-3-chenyi.qiang@intel.com \
--to=chenyi.qiang@intel.com \
--cc=jmattson@google.com \
--cc=joro@8bytes.org \
--cc=kvm@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=pbonzini@redhat.com \
--cc=seanjc@google.com \
--cc=vkuznets@redhat.com \
--cc=wanpengli@tencent.com \
--cc=xiaoyao.li@intel.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox