Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

KVM: x86: Ensure PV TLB flush tracepoint reflects KVM behavior

In record_steal_time(), st->preempted is read twice, and
trace_kvm_pv_tlb_flush() might output result inconsistent if
kvm_vcpu_flush_tlb_guest() see a different st->preempted later.

It is a very trivial problem and hardly has actual harm and can be
avoided by reseting and reading st->preempted in atomic way via xchg().

Signed-off-by: Lai Jiangshan <laijs@linux.alibaba.com>

Message-Id: <20210531174628.10265-1-jiangshanlai@gmail.com>
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>

authored by

Lai Jiangshan and committed by
Paolo Bonzini
af3511ff b1bd5cba

+4 -2
+4 -2
arch/x86/kvm/x86.c
··· 3101 3101 * expensive IPIs. 3102 3102 */ 3103 3103 if (guest_pv_has(vcpu, KVM_FEATURE_PV_TLB_FLUSH)) { 3104 + u8 st_preempted = xchg(&st->preempted, 0); 3105 + 3104 3106 trace_kvm_pv_tlb_flush(vcpu->vcpu_id, 3105 - st->preempted & KVM_VCPU_FLUSH_TLB); 3106 - if (xchg(&st->preempted, 0) & KVM_VCPU_FLUSH_TLB) 3107 + st_preempted & KVM_VCPU_FLUSH_TLB); 3108 + if (st_preempted & KVM_VCPU_FLUSH_TLB) 3107 3109 kvm_vcpu_flush_tlb_guest(vcpu); 3108 3110 } else { 3109 3111 st->preempted = 0;