Skip to content

Commit 4346db6

Browse files
mmhalsean-jc
authored andcommitted
KVM: x86: Force TLB flush on userspace changes to special registers
Userspace can directly modify the content of vCPU's CR0, CR3, and CR4 via KVM_SYNC_X86_SREGS and KVM_SET_SREGS{,2}. Make sure that KVM flushes guest TLB entries and paging-structure caches if a (partial) guest TLB flush is architecturally required based on the CRn changes. To keep things simple, flush whenever KVM resets the MMU context, i.e. if any bits in CR0, CR3, CR4, or EFER are modified. This is extreme overkill, but stuffing state from userspace is not such a hot path that preserving guest TLB state is a priority. Suggested-by: Paolo Bonzini <[email protected]> Signed-off-by: Michal Luczaj <[email protected]> Link: https://lore.kernel.org/r/[email protected] [sean: call out that the flushing on MMU context resets is for simplicity] Signed-off-by: Sean Christopherson <[email protected]>
1 parent 9dbb029 commit 4346db6

File tree

1 file changed

+6
-2
lines changed

1 file changed

+6
-2
lines changed

arch/x86/kvm/x86.c

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11564,8 +11564,10 @@ static int __set_sregs(struct kvm_vcpu *vcpu, struct kvm_sregs *sregs)
1156411564
if (ret)
1156511565
return ret;
1156611566

11567-
if (mmu_reset_needed)
11567+
if (mmu_reset_needed) {
1156811568
kvm_mmu_reset_context(vcpu);
11569+
kvm_make_request(KVM_REQ_TLB_FLUSH_GUEST, vcpu);
11570+
}
1156911571

1157011572
max_bits = KVM_NR_INTERRUPTS;
1157111573
pending_vec = find_first_bit(
@@ -11606,8 +11608,10 @@ static int __set_sregs2(struct kvm_vcpu *vcpu, struct kvm_sregs2 *sregs2)
1160611608
mmu_reset_needed = 1;
1160711609
vcpu->arch.pdptrs_from_userspace = true;
1160811610
}
11609-
if (mmu_reset_needed)
11611+
if (mmu_reset_needed) {
1161011612
kvm_mmu_reset_context(vcpu);
11613+
kvm_make_request(KVM_REQ_TLB_FLUSH_GUEST, vcpu);
11614+
}
1161111615
return 0;
1161211616
}
1161311617

0 commit comments

Comments
 (0)