Skip to contentSkip to conversation

KVM: x86/mmu: Precisely invalidate MMU root_role during CPUID update

PATCH In review Sean Christopherson 1 participant 2 messages Apr 8, 2024 – Apr 9, 2024

Subsystems (3)
lore
Share & tools
lore

Sean Christopherson

author maintainer
#0 lore raw

PATCH 1/1 KVM: x86/mmu: Precisely invalidate MMU root_role during CPUID update

Set kvm_mmu_page_role.invalid to mark the various MMU root_roles invalid during CPUID update in order to force a refresh, instead of zeroing out the entire role. This fixes a bug where kvm_mmu_free_roots() incorrectly thinks a root is indirect, i.e. not a TDP MMU, due to "direct" being zeroed, which in turn causes KVM to take mmu_lock for write instead of read.

Note, paving over the entire role was largely unintentional, commit 7a458f0e1ba1 ("KVM: x86/mmu: remove extended bits from mmu_role, rename field") simply missed that "invalid" could be set.

Fixes 576a15de8d29 ("KVM: x86/mmu: Free TDP MMU roots while holding mmy_lock for read")
Reported-by [email protected]
Cc Phi Nguyen <[email protected]>
---
 arch/x86/kvm/mmu/mmu.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
arch/x86/kvm/mmu/mmu.c +3 -3
index 992e651540e8..1eadefdb4bf2 100644
@@ -5576,9 +5576,9 @@ void kvm_mmu_after_set_cpuid(struct kvm_vcpu *vcpu)
 	 * that problem is swept under the rug; KVM's CPUID API is horrific and
 	 * it's all but impossible to solve it without introducing a new API.
 	 */
-	vcpu->arch.root_mmu.root_role.word = 0;
-	vcpu->arch.guest_mmu.root_role.word = 0;
-	vcpu->arch.nested_mmu.root_role.word = 0;
+	vcpu->arch.root_mmu.root_role.invalid = 1;
+	vcpu->arch.guest_mmu.root_role.invalid = 1;
+	vcpu->arch.nested_mmu.root_role.invalid = 1;
 	vcpu->arch.root_mmu.cpu_role.ext.valid = 0;
 	vcpu->arch.guest_mmu.cpu_role.ext.valid = 0;
 	vcpu->arch.nested_mmu.cpu_role.ext.valid = 0;

base-commit: 8cb4a9a82b21623dbb4b3051dd30d98356cf95bc

Signature · 1 line
2.44.0.478.gd926399ef9-goog
author maintainer
#1 lore raw
Quoted 8 lines · Sean Christopherson

Set kvm_mmu_page_role.invalid to mark the various MMU root_roles invalid during CPUID update in order to force a refresh, instead of zeroing out the entire role. This fixes a bug where kvm_mmu_free_roots() incorrectly thinks a root is indirect, i.e. not a TDP MMU, due to "direct" being zeroed, which in turn causes KVM to take mmu_lock for write instead of read.

[...]

Applied super quickly to kvm-x86 fixes, so that it gets time in -next before I pass it on to Paolo. This is the last commit in "fixes", so it's easy to drop and/or modify if there's an issue.

[1/1] KVM: x86/mmu: Precisely invalidate MMU root_role during CPUID update

      https://github.com/kvm-x86/linux/commit/4fa96da3e6fe

--
https://github.com/kvm-x86/linux/tree/next