linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] KVM: X86: always reset st->preempted in record_steal_time()
@ 2021-05-27 17:33 Lai Jiangshan
  2021-05-28  6:12 ` Wanpeng Li
  0 siblings, 1 reply; 4+ messages in thread
From: Lai Jiangshan @ 2021-05-27 17:33 UTC (permalink / raw)
  To: linux-kernel
  Cc: Lai Jiangshan, Oliver Upton, Paolo Bonzini, Sean Christopherson,
	Vitaly Kuznetsov, Wanpeng Li, Jim Mattson, Joerg Roedel,
	Thomas Gleixner, Ingo Molnar, Borislav Petkov, x86,
	H. Peter Anvin, Peter Shier, kvm

From: Lai Jiangshan <laijs@linux.alibaba.com>

st->preempted needs to be reset in record_steal_time() to clear the
KVM_VCPU_PREEMPTED bit.

But the commit 66570e966dd9 ("kvm: x86: only provide PV features if
enabled in guest's CPUID") made it cleared conditionally and
KVM_VCPU_PREEMPTED might not be cleared when entering into the guest.

Also make st->preempted be only read once, so that trace_kvm_pv_tlb_flush()
and kvm_vcpu_flush_tlb_guest() is consistent with same value of st->preempted.

Cc: Oliver Upton <oupton@google.com>
Fixes: 66570e966dd9 ("kvm: x86: only provide PV features if enabled in guest's CPUID")
Signed-off-by: Lai Jiangshan <laijs@linux.alibaba.com>
---
 arch/x86/kvm/x86.c | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)

diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c
index bbc4e04e67ad..b8a7259ebd14 100644
--- a/arch/x86/kvm/x86.c
+++ b/arch/x86/kvm/x86.c
@@ -3101,10 +3101,14 @@ static void record_steal_time(struct kvm_vcpu *vcpu)
 	 * expensive IPIs.
 	 */
 	if (guest_pv_has(vcpu, KVM_FEATURE_PV_TLB_FLUSH)) {
+		u8 st_preempted = xchg(&st->preempted, 0);
+
 		trace_kvm_pv_tlb_flush(vcpu->vcpu_id,
-				       st->preempted & KVM_VCPU_FLUSH_TLB);
-		if (xchg(&st->preempted, 0) & KVM_VCPU_FLUSH_TLB)
+				       st_preempted & KVM_VCPU_FLUSH_TLB);
+		if (st_preempted & KVM_VCPU_FLUSH_TLB)
 			kvm_vcpu_flush_tlb_guest(vcpu);
+	} else {
+		st->preempted = 0;
 	}
 
 	vcpu->arch.st.preempted = 0;
-- 
2.19.1.6.gb485710b


^ permalink raw reply related	[flat|nested] 4+ messages in thread

* Re: [PATCH] KVM: X86: always reset st->preempted in record_steal_time()
  2021-05-27 17:33 [PATCH] KVM: X86: always reset st->preempted in record_steal_time() Lai Jiangshan
@ 2021-05-28  6:12 ` Wanpeng Li
  2021-05-31 17:46   ` [PATCH] KVM: X86: reset and read st->preempted in atomic way Lai Jiangshan
  0 siblings, 1 reply; 4+ messages in thread
From: Wanpeng Li @ 2021-05-28  6:12 UTC (permalink / raw)
  To: Lai Jiangshan
  Cc: LKML, Lai Jiangshan, Oliver Upton, Paolo Bonzini,
	Sean Christopherson, Vitaly Kuznetsov, Wanpeng Li, Jim Mattson,
	Joerg Roedel, Thomas Gleixner, Ingo Molnar, Borislav Petkov,
	the arch/x86 maintainers, H. Peter Anvin, Peter Shier, kvm

On Fri, 28 May 2021 at 14:11, Lai Jiangshan <jiangshanlai@gmail.com> wrote:
>
> From: Lai Jiangshan <laijs@linux.alibaba.com>
>
> st->preempted needs to be reset in record_steal_time() to clear the
> KVM_VCPU_PREEMPTED bit.
>
> But the commit 66570e966dd9 ("kvm: x86: only provide PV features if
> enabled in guest's CPUID") made it cleared conditionally and
> KVM_VCPU_PREEMPTED might not be cleared when entering into the guest.
>
> Also make st->preempted be only read once, so that trace_kvm_pv_tlb_flush()
> and kvm_vcpu_flush_tlb_guest() is consistent with same value of st->preempted.
>
> Cc: Oliver Upton <oupton@google.com>
> Fixes: 66570e966dd9 ("kvm: x86: only provide PV features if enabled in guest's CPUID")

It has already been fixed by commit 1eff0ada88b48 (KVM: X86: Fix vCPU
preempted state from guest's point of view) in kvm/master.

    Wanpeng

^ permalink raw reply	[flat|nested] 4+ messages in thread

* [PATCH] KVM: X86: reset and read st->preempted in atomic way
  2021-05-28  6:12 ` Wanpeng Li
@ 2021-05-31 17:46   ` Lai Jiangshan
  2021-06-08  0:33     ` Sean Christopherson
  0 siblings, 1 reply; 4+ messages in thread
From: Lai Jiangshan @ 2021-05-31 17:46 UTC (permalink / raw)
  To: linux-kernel
  Cc: Lai Jiangshan, Paolo Bonzini, Sean Christopherson,
	Vitaly Kuznetsov, Wanpeng Li, Jim Mattson, Joerg Roedel,
	Thomas Gleixner, Ingo Molnar, Borislav Petkov, x86,
	H. Peter Anvin, kvm

From: Lai Jiangshan <laijs@linux.alibaba.com>

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>

diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c
index 0087d3532c98..fba39fe162da 100644
--- a/arch/x86/kvm/x86.c
+++ b/arch/x86/kvm/x86.c
@@ -3117,9 +3117,11 @@ static void record_steal_time(struct kvm_vcpu *vcpu)
 	 * expensive IPIs.
 	 */
 	if (guest_pv_has(vcpu, KVM_FEATURE_PV_TLB_FLUSH)) {
+		u8 st_preempted = xchg(&st->preempted, 0);
+
 		trace_kvm_pv_tlb_flush(vcpu->vcpu_id,
-				       st->preempted & KVM_VCPU_FLUSH_TLB);
-		if (xchg(&st->preempted, 0) & KVM_VCPU_FLUSH_TLB)
+				       st_preempted & KVM_VCPU_FLUSH_TLB);
+		if (st_preempted & KVM_VCPU_FLUSH_TLB)
 			kvm_vcpu_flush_tlb_guest(vcpu);
 	} else {
 		st->preempted = 0;
-- 
2.19.1.6.gb485710b


^ permalink raw reply related	[flat|nested] 4+ messages in thread

* Re: [PATCH] KVM: X86: reset and read st->preempted in atomic way
  2021-05-31 17:46   ` [PATCH] KVM: X86: reset and read st->preempted in atomic way Lai Jiangshan
@ 2021-06-08  0:33     ` Sean Christopherson
  0 siblings, 0 replies; 4+ messages in thread
From: Sean Christopherson @ 2021-06-08  0:33 UTC (permalink / raw)
  To: Lai Jiangshan
  Cc: linux-kernel, Lai Jiangshan, Paolo Bonzini, Vitaly Kuznetsov,
	Wanpeng Li, Jim Mattson, Joerg Roedel, Thomas Gleixner,
	Ingo Molnar, Borislav Petkov, x86, H. Peter Anvin, kvm

Nit: the shortlog is somewhat inaccurate now, maybe just:

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

or something along those lines.  Not sure what the best wording is :-/

On Tue, Jun 01, 2021, Lai Jiangshan wrote:
> From: Lai Jiangshan <laijs@linux.alibaba.com>
> 
> 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>

I saw this quirk too, but couldn't quite bring myself to care enought to test a
patch :-)

Reviewed-by: Sean Christopherson <seanjc@google.com> 

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2021-06-08  0:35 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-05-27 17:33 [PATCH] KVM: X86: always reset st->preempted in record_steal_time() Lai Jiangshan
2021-05-28  6:12 ` Wanpeng Li
2021-05-31 17:46   ` [PATCH] KVM: X86: reset and read st->preempted in atomic way Lai Jiangshan
2021-06-08  0:33     ` Sean Christopherson

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).