From: Dongli Zhang <dongli.zhang@oracle.com>
To: linux-kernel@vger.kernel.org, kvm@vger.kernel.org
Cc: seanjc@google.com, pbonzini@redhat.com, peterz@infradead.org,
juri.lelli@redhat.com, vincent.guittot@linaro.org,
dietmar.eggemann@arm.com, rostedt@goodmis.org,
bsegall@google.com, mgorman@suse.de, vschneid@redhat.com,
kprateek.nayak@amd.com, dwmw2@infradead.org, joe.jin@oracle.com
Subject: [PATCH RFC 1/2] KVM: x86: Update stealtime before clearing preempted state
Date: Sun, 23 Aug 2026 18:26:25 -0700 [thread overview]
Message-ID: <20260824012716.753022-2-dongli.zhang@oracle.com> (raw)
In-Reply-To: <20260824012716.753022-1-dongli.zhang@oracle.com>
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@oracle.com>
---
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
next prev parent reply other threads:[~2026-08-24 1:37 UTC|newest]
Thread overview: 5+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-08-24 1:26 [PATCH RFC 0/2] Fix KVM guest scheduling accounting issue related to stealtime Dongli Zhang
2026-08-24 1:26 ` Dongli Zhang [this message]
2026-08-24 1:26 ` [PATCH RFC 2/2] sched/core: Defer preempted remote vCPU task clock updates Dongli Zhang
2026-09-21 15:59 ` Sean Christopherson
2026-09-23 17:25 ` Dongli Zhang
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20260824012716.753022-2-dongli.zhang@oracle.com \
--to=dongli.zhang@oracle.com \
--cc=bsegall@google.com \
--cc=dietmar.eggemann@arm.com \
--cc=dwmw2@infradead.org \
--cc=joe.jin@oracle.com \
--cc=juri.lelli@redhat.com \
--cc=kprateek.nayak@amd.com \
--cc=kvm@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=mgorman@suse.de \
--cc=pbonzini@redhat.com \
--cc=peterz@infradead.org \
--cc=rostedt@goodmis.org \
--cc=seanjc@google.com \
--cc=vincent.guittot@linaro.org \
--cc=vschneid@redhat.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®