Skip to contentSkip to conversation

KVM: x86: move saving of arch.host_pkru to kvm_arch_vcpu_load in 5.4.y

PATCH New Jon Kohler 1 participant 1 message May 6, 2021

Subsystems (4)
lore
Share & tools
lore

Jon Kohler

author
#0 lore raw

PATCH 1/1 KVM: x86: move saving of arch.host_pkru to kvm_arch_vcpu_load in 5.4.y

commit 37486135d3a7b03acc7755b63627a130437f066a upstream.

In 5.4.y only, vcpu->arch.host_pkru is being set on every run thru of vcpu_enter_guest, when it really only needs to be set on load. As a result, we're doing a rdpkru on supported CPUs on every iteration of vcpu_enter_guest even though the value never changes.

Mainline and 5.10.y already has host_pkru being initialized in kvm_arch_vcpu_load. This change is 5.4.y specific and moves host_pkru save to kvm_arch_vcpu_load.

Fixes 99e392a4979b ("KVM: x86: Fix pkru save/restore when guest CR4.PKE=0, move it to x86.c")
Cc Babu Moger <[email protected]>
---
 arch/x86/kvm/x86.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
arch/x86/kvm/x86.c +3 -3
index 153659e8f403..1f7521752a94 100644
@@ -3507,6 +3507,9 @@ void kvm_arch_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
 
 	kvm_x86_ops->vcpu_load(vcpu, cpu);
 
+	/* Save host pkru register if supported */
+	vcpu->arch.host_pkru = read_pkru();
+
 	/* Apply any externally detected TSC adjustments (due to suspend) */
 	if (unlikely(vcpu->arch.tsc_offset_adjustment)) {
 		adjust_tsc_offset_host(vcpu, vcpu->arch.tsc_offset_adjustment);
@@ -8253,9 +8256,6 @@ static int vcpu_enter_guest(struct kvm_vcpu *vcpu)
 	trace_kvm_entry(vcpu->vcpu_id);
 	guest_enter_irqoff();
 
-	/* Save host pkru register if supported */
-	vcpu->arch.host_pkru = read_pkru();
-
 	fpregs_assert_state_consistent();
 	if (test_thread_flag(TIF_NEED_FPU_LOAD))
 		switch_fpu_return();
Signature · 1 line
2.30.1 (Apple Git-130)