kvm: x86: Don't dirty guest memory on every vcpu_put()
PATCH 1/1 kvm: x86: Don't dirty guest memory on every vcpu_put()
Beginning with commit 0b9f6c4615c99 ("x86/kvm: Support the vCPU preemption check"), the KVM_VCPU_PREEMPTED flag is set in the guest copy of the kvm_steal_time struct on every call to vcpu_put(). As a result, guest memory is dirtied on every call to vcpu_put(), even when the VM is quiescent.
To avoid dirtying guest memory unnecessarily, don't bother setting the flag in the guest copy of the struct if it is already set in the kernel copy of the struct.
If a different vCPU thread clears the guest copy of the flag, it will no longer get reset on the next call to vcpu_put, but it's not clear that resetting the flag in this case was intentional to begin with.
--- arch/x86/kvm/x86.c | 3 +++ 1 file changed, 3 insertions(+)
arch/x86/kvm/x86.c +3 -0
@@ -3504,6 +3504,9 @@ static void kvm_steal_time_set_preempted(struct kvm_vcpu *vcpu)if (!(vcpu->arch.st.msr_val &KVM_MSR_ENABLED ))return ; +if (vcpu->arch.st.steal.preempted &KVM_VCPU_PREEMPTED ) +return ; + vcpu->arch.st.steal.preempted =KVM_VCPU_PREEMPTED ; kvm_write_guest_offset_cached(vcpu->kvm, &vcpu->arch.st.stime,
Signature · 1 line
2.25.0.rc1.283.g88dfdc4193-goog
Quoted 10 lines · Jim Mattson
Beginning with commit
0b9f6c4615c99("x86/kvm: Support the vCPU preemption check"), theKVM_VCPU_PREEMPTEDflag is set in the guest copy of thekvm_steal_timestruct on every call tovcpu_put(). As a result, guest memory is dirtied on every call tovcpu_put(), even when the VM is quiescent.
Quoted 3 lines
To avoid dirtying guest memory unnecessarily, don't bother setting the flag in the guest copy of the struct if it is already set in the kernel copy of the struct.
I suggest adding this comment to code as-well.
Quoted 4 lines
If a different vCPU thread clears the guest copy of the flag, it will no longer get reset on the next call to
vcpu_put, but it's not clear that resetting the flag in this case was intentional to begin with.
I agree… I find it hard to believe that guest vCPU is allowed to clear the flag and expect host to set it again on the next vcpu_put() call. Doesn’t really make sense.
Quoted 5 lines
Signed-off-by Jim Mattson <[email protected]>Tested-by Kevin Mcgaire <[email protected]>Reviewed-by Ben Gardon <[email protected]>Reviewed-by Oliver Upton <[email protected]>
Good catch.
-Liran
Quoted 22 lines
---
arch/x86/kvm/x86.c| 3 +++1 file changed, 3 insertions(+)arch/x86/kvm/x86.c +3
-0@@ -3504,6 +3504,9 @@ static void kvm_steal_time_set_preempted(struct kvm_vcpu *vcpu)if (!(vcpu->arch.st.msr_val &KVM_MSR_ENABLED ))return ; +if (vcpu->arch.st.steal.preempted &KVM_VCPU_PREEMPTED ) +return ; + vcpu->arch.st.steal.preempted =KVM_VCPU_PREEMPTED ; kvm_write_guest_offset_cached(vcpu->kvm, &vcpu->arch.st.stime,Signature · 1 line
2.25.0.rc1.283.g88dfdc4193-goog
Quoted 11 lines · Liran Alon
Quoted 11 lines · Jim Mattson
Beginning with commit
0b9f6c4615c99("x86/kvm: Support the vCPU preemption check"), theKVM_VCPU_PREEMPTEDflag is set in the guest copy of thekvm_steal_timestruct on every call tovcpu_put(). As a result, guest memory is dirtied on every call tovcpu_put(), even when the VM is quiescent.
Quoted 5 lines
Quoted 3 lines
To avoid dirtying guest memory unnecessarily, don't bother setting the flag in the guest copy of the struct if it is already set in the kernel copy of the struct.
I suggest adding this comment to code as-well.
Ping. I don't know if a v2 of this change with the comment in code is needed for acceptance, but I don't want this to fall through the cracks and get lost.
Quoted 43 lines
Quoted 5 lines
If a different vCPU thread clears the guest copy of the flag, it will no longer get reset on the next call to
vcpu_put, but it's not clear that resetting the flag in this case was intentional to begin with.I agree… I find it hard to believe that guest vCPU is allowed to clear the flag and expect host to set it again on the next
vcpu_put()call. Doesn’t really make sense.Quoted 5 lines
Signed-off-by Jim Mattson <[email protected]>Tested-by Kevin Mcgaire <[email protected]>Reviewed-by Ben Gardon <[email protected]>Reviewed-by Oliver Upton <[email protected]>Good catch.
Reviewed-by Liran Alon <[email protected]>-Liran
Quoted 22 lines
---
arch/x86/kvm/x86.c| 3 +++1 file changed, 3 insertions(+)arch/x86/kvm/x86.c +3
-1@@ -3504,6 +3504,9 @@ static void kvm_steal_time_set_preempted(struct kvm_vcpu *vcpu)if (!(vcpu->arch.st.msr_val &KVM_MSR_ENABLED ))return ; +if (vcpu->arch.st.steal.preempted &KVM_VCPU_PREEMPTED ) +return ; + vcpu->arch.st.steal.preempted =KVM_VCPU_PREEMPTED ; kvm_write_guest_offset_cached(vcpu->kvm, &vcpu->arch.st.stime, --2.25.0.rc1.283.g88dfdc4193-goog
Quoted 16 lines · Ben Gardon
Quoted 15 lines · Liran Alon
Quoted 14 lines · Jim Mattson
Beginning with commit
0b9f6c4615c99("x86/kvm: Support the vCPU preemption check"), theKVM_VCPU_PREEMPTEDflag is set in the guest copy of thekvm_steal_timestruct on every call tovcpu_put(). As a result, guest memory is dirtied on every call tovcpu_put(), even when the VM is quiescent.To avoid dirtying guest memory unnecessarily, don't bother setting the flag in the guest copy of the struct if it is already set in the kernel copy of the struct.
Quoted 5 lines
Quoted 1 line
I suggest adding this comment to code as-well.
Ping. I don't know if a v2 of this change with the comment in code is needed for acceptance, but I don't want this to fall through the cracks and get lost.
A version of this was committed a while ago. The CVE number makes me think it went stealthily...
commit 8c6de56a42e0c657955e12b882a81ef07d1d073e
Author: Boris Ostrovsky <[email protected]> Date: Wed Oct 30 19:01:31 2019 +0000
x86/kvm: Be careful not to clear KVM_VCPU_FLUSH_TLB bit
kvm_steal_time_set_preempted() may accidentally clear KVM_VCPU_FLUSH_TLB bit if it is called more than once while VCPU is preempted.
This is part of CVE-2019-3016.
(This bug was also independently discovered by Jim Mattson <[email protected]>)
arch/x86/kvm/x86.c +3 -0
@@ -3504,6 +3504,9 @@ static void kvm_steal_time_set_preempted(struct kvm_vcpu *vcpu)if (!(vcpu->arch.st.msr_val &KVM_MSR_ENABLED ))return ; +if (vcpu->arch.st.steal.preempted) +return ; + vcpu->arch.st.steal.preempted =KVM_VCPU_PREEMPTED ; kvm_write_guest_offset_cached(vcpu->kvm, &vcpu->arch.st.stime,
Quoted 41 lines
Quoted 41 lines
Quoted 3 lines
If a different vCPU thread clears the guest copy of the flag, it will no longer get reset on the next call to
vcpu_put, but it's not clear that resetting the flag in this case was intentional to begin with.I agree… I find it hard to believe that guest vCPU is allowed to clear the flag and expect host to set it again on the next
vcpu_put()call. Doesn’t really make sense.Quoted 5 lines
Signed-off-by Jim Mattson <[email protected]>Tested-by Kevin Mcgaire <[email protected]>Reviewed-by Ben Gardon <[email protected]>Reviewed-by Oliver Upton <[email protected]>Good catch.
Reviewed-by Liran Alon <[email protected]>-Liran
Quoted 22 lines
---
arch/x86/kvm/x86.c| 3 +++1 file changed, 3 insertions(+)arch/x86/kvm/x86.c +3
-1@@ -3504,6 +3504,9 @@ static void kvm_steal_time_set_preempted(struct kvm_vcpu *vcpu)if (!(vcpu->arch.st.msr_val &KVM_MSR_ENABLED ))return ; +if (vcpu->arch.st.steal.preempted &KVM_VCPU_PREEMPTED ) +return ; + vcpu->arch.st.steal.preempted =KVM_VCPU_PREEMPTED ; kvm_write_guest_offset_cached(vcpu->kvm, &vcpu->arch.st.stime, --2.25.0.rc1.283.g88dfdc4193-goog
Quoted 18 lines · Sean Christopherson
Quoted 16 lines · Ben Gardon
Quoted 15 lines · Liran Alon
Quoted 14 lines · Jim Mattson
Beginning with commit
0b9f6c4615c99("x86/kvm: Support the vCPU preemption check"), theKVM_VCPU_PREEMPTEDflag is set in the guest copy of thekvm_steal_timestruct on every call tovcpu_put(). As a result, guest memory is dirtied on every call tovcpu_put(), even when the VM is quiescent.To avoid dirtying guest memory unnecessarily, don't bother setting the flag in the guest copy of the struct if it is already set in the kernel copy of the struct.
Quoted 8 lines
Quoted 5 lines
Quoted 1 line
I suggest adding this comment to code as-well.
Ping. I don't know if a v2 of this change with the comment in code is needed for acceptance, but I don't want this to fall through the cracks and get lost.
A version of this was committed a while ago. The CVE number makes me think it went stealthily...
That's great to know. Thanks for digging that up Sean.
Quoted 77 lines
commit
8c6de56a42e0c657955e12b882a81ef07d1d073eAuthor: Boris Ostrovsky <[email protected]> Date: Wed Oct 30 19:01:31 2019 +0000x86/kvm: Be careful not to clear
KVM_VCPU_FLUSH_TLBbit
kvm_steal_time_set_preempted()may accidentally clearKVM_VCPU_FLUSH_TLBbit if it is called more than once while VCPU is preempted.This is part of CVE-2019-3016.
(This bug was also independently discovered by Jim Mattson <[email protected]>)
Signed-off-by Boris Ostrovsky <[email protected]>Reviewed-by Joao Martins <[email protected]>Signed-off-by Paolo Bonzini <[email protected]>arch/x86/kvm/x86.c +3
-0@@ -3504,6 +3504,9 @@ static void kvm_steal_time_set_preempted(struct kvm_vcpu *vcpu)if (!(vcpu->arch.st.msr_val &KVM_MSR_ENABLED ))return ; +if (vcpu->arch.st.steal.preempted) +return ; + vcpu->arch.st.steal.preempted =KVM_VCPU_PREEMPTED ; kvm_write_guest_offset_cached(vcpu->kvm, &vcpu->arch.st.stime,Quoted 43 lines
Quoted 43 lines
Quoted 5 lines
If a different vCPU thread clears the guest copy of the flag, it will no longer get reset on the next call to
vcpu_put, but it's not clear that resetting the flag in this case was intentional to begin with.I agree… I find it hard to believe that guest vCPU is allowed to clear the flag and expect host to set it again on the next
vcpu_put()call. Doesn’t really make sense.Quoted 5 lines
Signed-off-by Jim Mattson <[email protected]>Tested-by Kevin Mcgaire <[email protected]>Reviewed-by Ben Gardon <[email protected]>Reviewed-by Oliver Upton <[email protected]>Good catch.
Reviewed-by Liran Alon <[email protected]>-Liran
Quoted 22 lines
---
arch/x86/kvm/x86.c| 3 +++1 file changed, 3 insertions(+)arch/x86/kvm/x86.c +3
-1@@ -3504,6 +3504,9 @@ static void kvm_steal_time_set_preempted(struct kvm_vcpu *vcpu)if (!(vcpu->arch.st.msr_val &KVM_MSR_ENABLED ))return ; +if (vcpu->arch.st.steal.preempted &KVM_VCPU_PREEMPTED ) +return ; + vcpu->arch.st.steal.preempted =KVM_VCPU_PREEMPTED ; kvm_write_guest_offset_cached(vcpu->kvm, &vcpu->arch.st.stime, --2.25.0.rc1.283.g88dfdc4193-goog
Paolo Bonzini
maintainerQuoted 4 lines · Ben Gardon
Quoted 2 lines
A version of this was committed a while ago. The CVE number makes me think it went stealthily...
That's great to know. Thanks for digging that up Sean.
I usually try to send CVE/embargoed patches after the fact to the list, using with an [FYI PATCH] prefix.
Paolo
Paolo Bonzini
maintainerQuoted 4 lines · Ben Gardon
Quoted 2 lines
A version of this was committed a while ago. The CVE number makes me think it went stealthily...
That's great to know. Thanks for digging that up Sean.
I usually try to send CVE/embargoed patches after the fact to the list, using an [FYI PATCH] prefix.
Paolo