]> Git Repo - J-linux.git/commitdiff
KVM: Mark a vCPU as preempted/ready iff it's scheduled out while running
authorDavid Matlack <[email protected]>
Fri, 3 May 2024 18:17:34 +0000 (11:17 -0700)
committerSean Christopherson <[email protected]>
Tue, 18 Jun 2024 16:22:42 +0000 (09:22 -0700)
Mark a vCPU as preempted/ready if-and-only-if it's scheduled out while
running. i.e. Do not mark a vCPU preempted/ready if it's scheduled out
during a non-KVM_RUN ioctl() or when userspace is doing KVM_RUN with
immediate_exit.

Commit 54aa83c90198 ("KVM: x86: do not set st->preempted when going back
to user space") stopped marking a vCPU as preempted when returning to
userspace, but if userspace then invokes a KVM vCPU ioctl() that gets
preempted, the vCPU will be marked preempted/ready. This is arguably
incorrect behavior since the vCPU was not actually preempted while the
guest was running, it was preempted while doing something on behalf of
userspace.

Marking a vCPU preempted iff its running also avoids KVM dirtying guest
memory after userspace has paused vCPUs, e.g. for live migration, which
allows userspace to collect the final dirty bitmap before or in parallel
with saving vCPU state,  without having to worry about saving vCPU state
triggering writes to guest memory.

Suggested-by: Sean Christopherson <[email protected]>
Signed-off-by: David Matlack <[email protected]>
Link: https://lore.kernel.org/r/[email protected]
[sean: massage changelog]
Signed-off-by: Sean Christopherson <[email protected]>
virt/kvm/kvm_main.c

index 2fed9a9b8bd688367123c393dc4ea068e67f4afc..5a2643c5a8ec831ec0c353b4b7fdb5580601edf9 100644 (file)
@@ -6317,7 +6317,7 @@ static void kvm_sched_out(struct preempt_notifier *pn,
 
        WRITE_ONCE(vcpu->scheduled_out, true);
 
-       if (current->on_rq) {
+       if (current->on_rq && vcpu->wants_to_run) {
                WRITE_ONCE(vcpu->preempted, true);
                WRITE_ONCE(vcpu->ready, true);
        }
This page took 0.053191 seconds and 4 git commands to generate.