[PATCH RFC 1/2] KVM: x86: Update stealtime before clearing preempted state
From: Dongli Zhang
Date: Sun Aug 23 2026 - 21:38:33 EST
The guest Linux scheduler may rely on KVM stealtime to determine whether
elapsed time should be deducted from task runtime.
However, when vCPU A reads vCPU B's stealtime for scheduler accounting,
the value may not be up to date. KVM updates stealtime only when the vCPU
is about to enter the guest.
Update stealtime before clearing the preempted state, so a remote vCPU that
observes vcpu_is_preempted() as false also observes the new stealtime.
Otherwise, reading a vCPU's stealtime from another vCPU is not reliable.
The remote vCPU should wait until vcpu_is_preempted() returns false for the
target vCPU.
Signed-off-by: Dongli Zhang <dongli.zhang@xxxxxxxxxx>
---
arch/x86/kvm/x86.c | 58 +++++++++++++++++++++++++---------------------
1 file changed, 32 insertions(+), 26 deletions(-)
diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c
index 69469bbdc84a..525a1448195e 100644
--- a/arch/x86/kvm/x86.c
+++ b/arch/x86/kvm/x86.c
@@ -3751,6 +3751,35 @@ static void record_steal_time(struct kvm_vcpu *vcpu)
}
st = (struct kvm_steal_time __user *)ghc->hva;
+
+ if (!user_access_begin(st, sizeof(*st)))
+ return;
+
+ unsafe_get_user(version, &st->version, out);
+ if (version & 1)
+ version += 1; /* first time write, random junk */
+
+ version += 1;
+ unsafe_put_user(version, &st->version, out);
+
+ /* Pairs with the guest side virt_rmb() in kvm_steal_clock(). */
+ smp_wmb();
+
+ unsafe_get_user(steal, &st->steal, out);
+ steal += current->sched_info.run_delay -
+ vcpu->arch.st.last_steal;
+ vcpu->arch.st.last_steal = current->sched_info.run_delay;
+ unsafe_put_user(steal, &st->steal, out);
+
+ version += 1;
+ unsafe_put_user(version, &st->version, out);
+
+ /*
+ * Publish the stealtime before making the vCPU look runnable to
+ * the guest.
+ */
+ smp_wmb();
+
/*
* Doing a TLB flush here, on the guest's behalf, can avoid
* expensive IPIs.
@@ -3759,9 +3788,6 @@ static void record_steal_time(struct kvm_vcpu *vcpu)
u8 st_preempted = 0;
int err = -EFAULT;
- if (!user_access_begin(st, sizeof(*st)))
- return;
-
asm volatile("1: xchgb %0, %2\n"
"xor %1, %1\n"
"2:\n"
@@ -3781,37 +3807,17 @@ static void record_steal_time(struct kvm_vcpu *vcpu)
if (st_preempted & KVM_VCPU_FLUSH_TLB)
kvm_vcpu_flush_tlb_guest(vcpu);
- if (!user_access_begin(st, sizeof(*st)))
- goto dirty;
} else {
- if (!user_access_begin(st, sizeof(*st)))
- return;
-
unsafe_put_user(0, &st->preempted, out);
vcpu->arch.st.preempted = 0;
+ user_access_end();
}
- unsafe_get_user(version, &st->version, out);
- if (version & 1)
- version += 1; /* first time write, random junk */
-
- version += 1;
- unsafe_put_user(version, &st->version, out);
-
- smp_wmb();
-
- unsafe_get_user(steal, &st->steal, out);
- steal += current->sched_info.run_delay -
- vcpu->arch.st.last_steal;
- vcpu->arch.st.last_steal = current->sched_info.run_delay;
- unsafe_put_user(steal, &st->steal, out);
-
- version += 1;
- unsafe_put_user(version, &st->version, out);
+ mark_page_dirty_in_slot(vcpu->kvm, ghc->memslot, gpa_to_gfn(ghc->gpa));
+ return;
out:
user_access_end();
- dirty:
mark_page_dirty_in_slot(vcpu->kvm, ghc->memslot, gpa_to_gfn(ghc->gpa));
}
--
2.43.5