KVM: track pid for VCPU only on KVM_RUN ioctl
authorChristian Borntraeger <borntraeger@de.ibm.com>
Tue, 5 Aug 2014 14:44:14 +0000 (16:44 +0200)
committerPaolo Bonzini <pbonzini@redhat.com>
Thu, 4 Dec 2014 14:29:13 +0000 (15:29 +0100)
We currently track the pid of the task that runs the VCPU in vcpu_load.
If a yield to that VCPU is triggered while the PID of the wrong thread
is active, the wrong thread might receive a yield, but this will most
likely not help the executing thread at all.  Instead, if we only track
the pid on the KVM_RUN ioctl, there are two possibilities:

1) the thread that did a non-KVM_RUN ioctl is holding a mutex that
the VCPU thread is waiting for.  In this case, the VCPU thread is not
runnable, but we also do not do a wrong yield.

2) the thread that did a non-KVM_RUN ioctl is sleeping, or doing
something that does not block the VCPU thread.  In this case, the
VCPU thread can receive the directed yield correctly.

Signed-off-by: Christian Borntraeger <borntraeger@de.ibm.com>
CC: Rik van Riel <riel@redhat.com>
CC: Raghavendra K T <raghavendra.kt@linux.vnet.ibm.com>
CC: Michael Mueller <mimu@linux.vnet.ibm.com>
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
virt/kvm/kvm_main.c

index 2ffee3018a3d806b3da8861213610902013752ad..c5c186af823bf251245453d124f43a0f5de82ffb 100644 (file)
@@ -124,15 +124,6 @@ int vcpu_load(struct kvm_vcpu *vcpu)
 
        if (mutex_lock_killable(&vcpu->mutex))
                return -EINTR;
-       if (unlikely(vcpu->pid != current->pids[PIDTYPE_PID].pid)) {
-               /* The thread running this VCPU changed. */
-               struct pid *oldpid = vcpu->pid;
-               struct pid *newpid = get_task_pid(current, PIDTYPE_PID);
-               rcu_assign_pointer(vcpu->pid, newpid);
-               if (oldpid)
-                       synchronize_rcu();
-               put_pid(oldpid);
-       }
        cpu = get_cpu();
        preempt_notifier_register(&vcpu->preempt_notifier);
        kvm_arch_vcpu_load(vcpu, cpu);
@@ -2050,6 +2041,15 @@ static long kvm_vcpu_ioctl(struct file *filp,
                r = -EINVAL;
                if (arg)
                        goto out;
+               if (unlikely(vcpu->pid != current->pids[PIDTYPE_PID].pid)) {
+                       /* The thread running this VCPU changed. */
+                       struct pid *oldpid = vcpu->pid;
+                       struct pid *newpid = get_task_pid(current, PIDTYPE_PID);
+                       rcu_assign_pointer(vcpu->pid, newpid);
+                       if (oldpid)
+                               synchronize_rcu();
+                       put_pid(oldpid);
+               }
                r = kvm_arch_vcpu_ioctl_run(vcpu, vcpu->run);
                trace_kvm_userspace_exit(vcpu->run->exit_reason, r);
                break;