Kernel KVM virtualization development
 help / color / mirror / Atom feed
From: Dongli Zhang <dongli.zhang@oracle.com>
To: kvm@vger.kernel.org, kvmarm@lists.linux.dev,
	loongarch@lists.linux.dev, kvm-riscv@lists.infradead.org,
	linux-kselftest@vger.kernel.org
Cc: maz@kernel.org, oupton@kernel.org, fuad.tabba@linux.dev,
	joey.gouly@arm.com, seiden@linux.ibm.com, suzuki.poulose@arm.com,
	yuzenghui@huawei.com, zhaotianrui@loongson.cn,
	maobibo@loongson.cn, chenhuacai@kernel.org, anup@brainfault.org,
	atish.patra@linux.dev, seanjc@google.com, pbonzini@redhat.com,
	shuah@kernel.org, dwmw2@infradead.org, joe.jin@oracle.com
Subject: [PATCH v2 2/4] KVM: Reset last_steal on vCPU pid change
Date: Fri,  4 Sep 2026 17:55:24 +0000	[thread overview]
Message-ID: <20260904175550.430266-3-dongli.zhang@oracle.com> (raw)
In-Reply-To: <20260904175550.430266-1-dongli.zhang@oracle.com>

KVM does not support vCPU hotplug. When a vCPU is removed, its
corresponding data structures are not freed by KVM. Instead, QEMU destroys
only the userspace state and the vCPU thread, while the KVM vCPU fd remains
open and parked in QEMU.

As a result, vcpu->last_steal is not reset.

If the same vCPU is later re-created by QEMU, last_steal retains its old
value, while current->sched_info.run_delay starts from zero since a new
vCPU thread is created. This causes
current->sched_info.run_delay - vcpu->last_steal to produce a large, bogus
value.

Fix this by resetting vcpu->last_steal to current->sched_info.run_delay
when KVM vCPU PID is changed.

Suggested-by: Sean Christopherson <seanjc@google.com>
Signed-off-by: Dongli Zhang <dongli.zhang@oracle.com>
---
v1->v2:
  - Move from kvm_arch_vcpu_run_pid_change() to its caller.

 virt/kvm/kvm_main.c | 4 ++++
 1 file changed, 4 insertions(+)

diff --git a/virt/kvm/kvm_main.c b/virt/kvm/kvm_main.c
index 65eb26a0520d..784f0d945b45 100644
--- a/virt/kvm/kvm_main.c
+++ b/virt/kvm/kvm_main.c
@@ -4478,6 +4478,10 @@ static long kvm_vcpu_ioctl(struct file *filp,
 			if (r)
 				break;
 
+#ifdef CONFIG_HAVE_KVM_PV_STEAL_TIME
+			vcpu->last_steal = current->sched_info.run_delay;
+#endif
+
 			newpid = get_task_pid(current, PIDTYPE_PID);
 			write_lock(&vcpu->pid_lock);
 			vcpu->pid = newpid;
-- 
2.43.7


  parent reply	other threads:[~2026-09-04 17:56 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-09-04 17:55 [PATCH v2 0/4] KVM: Reset steal time accounting on vCPU pid change Dongli Zhang
2026-09-04 17:55 ` [PATCH v2 1/4] KVM: Move last_steal to common struct kvm_vcpu Dongli Zhang
2026-09-04 18:07   ` sashiko-bot
2026-09-04 20:22     ` Dongli Zhang
2026-09-04 17:55 ` Dongli Zhang [this message]
2026-09-04 17:55 ` [PATCH v2 3/4] KVM: selftests: Test steal time across vCPU pid changes on x86 Dongli Zhang
2026-09-04 18:11   ` sashiko-bot
2026-09-04 20:27     ` Dongli Zhang
2026-09-04 17:55 ` [PATCH v2 4/4] KVM: selftests: Add arm64 coverage for steal time pid changes 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=20260904175550.430266-3-dongli.zhang@oracle.com \
    --to=dongli.zhang@oracle.com \
    --cc=anup@brainfault.org \
    --cc=atish.patra@linux.dev \
    --cc=chenhuacai@kernel.org \
    --cc=dwmw2@infradead.org \
    --cc=fuad.tabba@linux.dev \
    --cc=joe.jin@oracle.com \
    --cc=joey.gouly@arm.com \
    --cc=kvm-riscv@lists.infradead.org \
    --cc=kvm@vger.kernel.org \
    --cc=kvmarm@lists.linux.dev \
    --cc=linux-kselftest@vger.kernel.org \
    --cc=loongarch@lists.linux.dev \
    --cc=maobibo@loongson.cn \
    --cc=maz@kernel.org \
    --cc=oupton@kernel.org \
    --cc=pbonzini@redhat.com \
    --cc=seanjc@google.com \
    --cc=seiden@linux.ibm.com \
    --cc=shuah@kernel.org \
    --cc=suzuki.poulose@arm.com \
    --cc=yuzenghui@huawei.com \
    --cc=zhaotianrui@loongson.cn \
    /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