Skip to contentSkip to conversation

assign each vcpu an owning thread and improve yielding

RFC PATCH In review David Hildenbrand 4 participants 20 messages Nov 25, 2014 – Dec 3, 2014 KVM

lore
Share & tools
lore
#0 lore raw

cover assign each vcpu an owning thread and improve yielding

This series improves yielding on architectures that cannot disable preemption while entering the guest and makes the creating thread of a VCPU the owning thread and therefore the yield target when yielding to that VCPU.

We should focus on the case creating thread == executing thread and therefore remove the complicated handling of PIDs involving synchronize_rcus.

This way we can speed up the creation of VCPUs and directly yield to the executing vcpu threads.

Please note that - in theory - all VCPU ioctls should be triggered from the same VCPU thread, so changing threads is not a scenario we should optimize.

David Hildenbrand (2):
KVM: don't check for PF_VCPU when yielding
KVM: thread creating a vcpu is the owner of that vcpu

 include/linux/kvm_host.h |  1 +
 virt/kvm/kvm_main.c      | 22 ++--------------------
 2 files changed, 3 insertions(+), 20 deletions(-)
Signature · 1 line
1.8.5.5
#1 lore raw

RFC PATCH 1/2 KVM: don't check for PF_VCPU when yielding

As some architectures (e.g. s390) can't disable preemption while entering/leaving the guest, they won't receive the yield in all situations.

kvm_enter_guest() has to be called with preemption_disabled and will set PF_VCPU. After that point e.g. s390 reenables preemption and starts to execute the guest. The thread might therefore be scheduled out between kvm_enter_guest() and kvm_exit_guest(), resulting in PF_VCPU being set but not being run.

Please note that preemption has to stay enabled in order to correctly process page faults on s390.

Current code takes PF_VCPU as a hint that the VCPU thread is running and therefore needs no yield. yield_to() checks whether the target thread is running, so let's use the inbuilt functionality to make it independent of PF_VCPU and preemption.

---
 virt/kvm/kvm_main.c | 4 ----
 1 file changed, 4 deletions(-)
virt/kvm/kvm_main.c +0 -4
index 5b45330..184f52e 100644
@@ -1782,10 +1782,6 @@ int kvm_vcpu_yield_to(struct kvm_vcpu *target)
 	rcu_read_unlock();
 	if (!task)
 		return ret;
-	if (task->flags & PF_VCPU) {
-		put_task_struct(task);
-		return ret;
-	}
 	ret = yield_to(task, 1);
 	put_task_struct(task);
 
Signature · 1 line
1.8.5.5
#12 lore raw

RFC PATCH 2/2 KVM: thread creating a vcpu is the owner of that vcpu

In reply to David Hildenbrand

Currently, we allow changing the PID of a VCPU. This PID is used to identify the thread to yield to if we want to yield to this specific VCPU.

In practice (e.g. QEMU), the thread creating and executing the VCPU remains always the same. Temporarily exchanging the PID (e.g. because an ioctl is triggered from a wrong thread) doesn't really make sense.

The PID is exchanged and a synchronize_rcu() is called. When the executing thread tries to run the VCPU again, another synchronize_rcu() happens.

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. The executing thread won't have a higher priority after the wrong thread has finished with the ioctl. The wrong thread will even receive yields afterwards that were targeted to the executing vcpu, until the executing VCPU has replaced the PID on the next ioctl - doesn't feel correct to me.

This patch makes the creating thread the owning thread, and therefore the only valid yield candidate (especially because VCPU ioctls are - in theory - only valid when triggered from the owning thread - old user space versions may not stick to this rule). This should also speed up the initial start of all VCPUs, when the PID is assigned for the first time.

Should be backwards compatible - if there is any old user space version out there that doesn't stick to the creating == executing thread rule, yields will not work as intended.

---
 include/linux/kvm_host.h |  1 +
 virt/kvm/kvm_main.c      | 18 ++----------------
 2 files changed, 3 insertions(+), 16 deletions(-)
include/linux/kvm_host.h +1 -0
index aa56894..f1fe655 100644
@@ -245,6 +245,7 @@ struct kvm_vcpu {
 	int fpu_active;
 	int guest_fpu_loaded, guest_xcr0_loaded;
 	wait_queue_head_t wq;
+	/* the pid owning this vcpu - target for vcpu yields */
 	struct pid *pid;
 	int sigset_active;
 	sigset_t sigset;
virt/kvm/kvm_main.c +2 -16
index 184f52e..4ba7810 100644
@@ -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);
@@ -220,7 +211,7 @@ int kvm_vcpu_init(struct kvm_vcpu *vcpu, struct kvm *kvm, unsigned id)
 	vcpu->cpu = -1;
 	vcpu->kvm = kvm;
 	vcpu->vcpu_id = id;
-	vcpu->pid = NULL;
+	vcpu->pid = get_task_pid(current, PIDTYPE_PID);
 	init_waitqueue_head(&vcpu->wq);
 	kvm_async_pf_vcpu_init(vcpu);
 
@@ -1771,15 +1762,10 @@ EXPORT_SYMBOL_GPL(kvm_vcpu_kick);
 
 int kvm_vcpu_yield_to(struct kvm_vcpu *target)
 {
-	struct pid *pid;
 	struct task_struct *task = NULL;
 	int ret = 0;
 
-	rcu_read_lock();
-	pid = rcu_dereference(target->pid);
-	if (pid)
-		task = get_pid_task(pid, PIDTYPE_PID);
-	rcu_read_unlock();
+	task = get_pid_task(target->pid, PIDTYPE_PID);
 	if (!task)
 		return ret;
 	ret = yield_to(task, 1);
Signature · 1 line
1.8.5.5
#2 lore raw

Re: [PATCH RFC 1/2] KVM: don't check for PF_VCPU when yielding

In reply to David Hildenbrand

Am 25.11.2014 um 17:04 schrieb David Hildenbrand:

Quoted 8 lines

As some architectures (e.g. s390) can't disable preemption while entering/leaving the guest, they won't receive the yield in all situations.

kvm_enter_guest() has to be called with preemption_disabled and will set PF_VCPU. After that point e.g. s390 reenables preemption and starts to execute the guest. The thread might therefore be scheduled out between kvm_enter_guest() and kvm_exit_guest(), resulting in PF_VCPU being set but not being run.

Quoted 7 lines

Please note that preemption has to stay enabled in order to correctly process page faults on s390.

Current code takes PF_VCPU as a hint that the VCPU thread is running and therefore needs no yield. yield_to() checks whether the target thread is running, so let's use the inbuilt functionality to make it independent of PF_VCPU and preemption.

This change is a trade-off.
PRO: This patch would improve the case of preemption on s390. This is probably a corner case as most distros have preemption off anyway.
CON: The downside is that kvm_vcpu_yield_to is called also from kvm_vcpu_on_spin. Here we want to avoid the scheduler overhead for a wrong decision.

So I think this patch is probably not what we want in most cases.

Quoted 21 lines
---
 virt/kvm/kvm_main.c | 4 ----
 1 file changed, 4 deletions(-)
virt/kvm/kvm_main.c +0 -4
index 5b45330..184f52e 100644
@@ -1782,10 +1782,6 @@ int kvm_vcpu_yield_to(struct kvm_vcpu *target)
 	rcu_read_unlock();
 	if (!task)
 		return ret;
-	if (task->flags & PF_VCPU) {
-		put_task_struct(task);
-		return ret;
-	}
 	ret = yield_to(task, 1);
 	put_task_struct(task);
 
Acked-by on v1 2/2
#13 lore raw

Re: [PATCH RFC 2/2] KVM: thread creating a vcpu is the owner of that vcpu

In reply to David Hildenbrand

Am 25.11.2014 um 17:04 schrieb David Hildenbrand:

Quoted 25 lines

Currently, we allow changing the PID of a VCPU. This PID is used to identify the thread to yield to if we want to yield to this specific VCPU.

In practice (e.g. QEMU), the thread creating and executing the VCPU remains always the same. Temporarily exchanging the PID (e.g. because an ioctl is triggered from a wrong thread) doesn't really make sense.

The PID is exchanged and a synchronize_rcu() is called. When the executing thread tries to run the VCPU again, another synchronize_rcu() happens.

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. The executing thread won't have a higher priority after the wrong thread has finished with the ioctl. The wrong thread will even receive yields afterwards that were targeted to the executing vcpu, until the executing VCPU has replaced the PID on the next ioctl - doesn't feel correct to me.

This patch makes the creating thread the owning thread, and therefore the only valid yield candidate (especially because VCPU ioctls are - in theory - only valid when triggered from the owning thread - old user space versions may not stick to this rule). This should also speed up the initial start of all VCPUs, when the PID is assigned for the first time.

Quoted 5 lines

Should be backwards compatible - if there is any old user space version out there that doesn't stick to the creating == executing thread rule, yields will not work as intended.

This change actually makes perfect sense to me:
- The runtime change logic was problematic, (e.g. see commit 7103f60de8 "KVM: avoid unnecessary synchronize_rc" and the qemu fixes for s390 to bring all vCPU ioctls in the right thread).
- It makes vcpu_load cheaper
- It emphasizes what in api.txt: " Only run vcpu ioctls from the same thread that was used to create the
vcpu."

Quoted 64 lines
---
 include/linux/kvm_host.h |  1 +
 virt/kvm/kvm_main.c      | 18 ++----------------
 2 files changed, 3 insertions(+), 16 deletions(-)
include/linux/kvm_host.h +1 -0
index aa56894..f1fe655 100644
@@ -245,6 +245,7 @@ struct kvm_vcpu {
 	int fpu_active;
 	int guest_fpu_loaded, guest_xcr0_loaded;
 	wait_queue_head_t wq;
+	/* the pid owning this vcpu - target for vcpu yields */
 	struct pid *pid;
 	int sigset_active;
 	sigset_t sigset;
virt/kvm/kvm_main.c +2 -16
index 184f52e..4ba7810 100644
@@ -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);
@@ -220,7 +211,7 @@ int kvm_vcpu_init(struct kvm_vcpu *vcpu, struct kvm *kvm, unsigned id)
 	vcpu->cpu = -1;
 	vcpu->kvm = kvm;
 	vcpu->vcpu_id = id;
-	vcpu->pid = NULL;
+	vcpu->pid = get_task_pid(current, PIDTYPE_PID);
 	init_waitqueue_head(&vcpu->wq);
 	kvm_async_pf_vcpu_init(vcpu);
 
@@ -1771,15 +1762,10 @@ EXPORT_SYMBOL_GPL(kvm_vcpu_kick);
 
 int kvm_vcpu_yield_to(struct kvm_vcpu *target)
 {
-	struct pid *pid;
 	struct task_struct *task = NULL;
 	int ret = 0;
 
-	rcu_read_lock();
-	pid = rcu_dereference(target->pid);
-	if (pid)
-		task = get_pid_task(pid, PIDTYPE_PID);
-	rcu_read_unlock();
+	task = get_pid_task(target->pid, PIDTYPE_PID);
 	if (!task)
 		return ret;
 	ret = yield_to(task, 1);
#3 lore raw

Re: [PATCH RFC 1/2] KVM: don't check for PF_VCPU when yielding

In reply to Christian Borntraeger
Quoted 3 lines

This change is a trade-off.
PRO: This patch would improve the case of preemption on s390. This is probably a corner case as most distros have preemption off anyway.
CON: The downside is that kvm_vcpu_yield_to is called also from kvm_vcpu_on_spin. Here we want to avoid the scheduler overhead for a wrong decision.

Won't most of that part be covered by:

	if (!ACCESS_ONCE(vcpu->preempted))

vcpu->preempted is only set when scheduled out involuntarily. It is cleared when scheduled in. s390 sets it manually, to speed up waking up a vcpu.

So when our task is scheduled in (an PF_VCPU is set), this check will already avoid scheduler overhead in kvm_vcpu_on_spin() or am I missing something?

#4 lore raw

Re: [PATCH RFC 1/2] KVM: don't check for PF_VCPU when yielding

Am 26.11.2014 um 10:23 schrieb David Hildenbrand:

Quoted 6 lines
Quoted 3 lines

This change is a trade-off.
PRO: This patch would improve the case of preemption on s390. This is probably a corner case as most distros have preemption off anyway.
CON: The downside is that kvm_vcpu_yield_to is called also from kvm_vcpu_on_spin. Here we want to avoid the scheduler overhead for a wrong decision.

Won't most of that part be covered by:

	if (!ACCESS_ONCE(vcpu->preempted))

Hmm, right. Checking vcpu->preempted and PF_VCPU might boil down to the same. Would be good if to have to performance regression test, though.

Quoted 7 lines

vcpu->preempted is only set when scheduled out involuntarily. It is cleared when scheduled in. s390 sets it manually, to speed up waking up a vcpu.

So when our task is scheduled in (an PF_VCPU is set), this check will already avoid scheduler overhead in kvm_vcpu_on_spin() or am I missing something?

CC Raghavendra KT. Could be rerun your kernbench/sysbench/ebizzy setup on x86 to see if the patch in this thread causes any regression? If think your commit 7bc7ae25b143"kvm: Iterate over only vcpus that are preempted" might have really made the PF_VCPU check unnecessary

CC Michael Mueller, do we still have our yield performance setup handy to check if this patch causes any regression?

Christian

#5 lore raw

Re: [PATCH RFC 1/2] KVM: don't check for PF_VCPU when yielding

Was able to test the patch, here is the result: I have not tested with bigger VMs though. Results make it difficult to talk about any side effect of
patch if any.

System 16 core 32cpu (+ht) sandybridge
with 4 guests of 16vcpu each

+-----------+-----------+-----------+------------+-----------+
kernbench (time taken lower is better)

+-----------+-----------+-----------+------------+-----------+
     base       %stdev      patched      %stdev    %improvement
+-----------+-----------+-----------+------------+-----------+
1x   53.1421     2.3086        54.6671     2.9673      -2.86966
2x   89.6858     6.4540        94.0626     6.8317      -4.88015
+-----------+-----------+-----------+------------+-----------+
+-----------+-----------+-----------+------------+-----------+
             ebizzy  (recors/sec higher is better)
+-----------+-----------+-----------+------------+-----------+
     base        %stdev          patched      %stdev    %improvement
+-----------+-----------+-----------+------------+-----------+
1x 14523.2500     8.4388    14928.8750     3.0478       2.79294
2x  3338.8750     1.4592     3270.8750     2.3980      -2.03661
+-----------+-----------+-----------+------------+-----------+
+-----------+-----------+-----------+------------+-----------+
             dbench  (Throughput higher is better)
+-----------+-----------+-----------+------------+-----------+
     base       %stdev           patched      %stdev    %improvement
+-----------+-----------+-----------+------------+-----------+
1x  6386.4737     1.0487    6703.9113     1.2298       4.97047
2x  2571.4712     1.3733    2571.8175     1.6919       0.01347
+-----------+-----------+-----------+------------+-----------+

Raghu

Quoted 26 lines · Christian Borntraeger

Am 26.11.2014 um 10:23 schrieb David Hildenbrand:

Quoted 6 lines
Quoted 3 lines

This change is a trade-off.
PRO: This patch would improve the case of preemption on s390. This is probably a corner case as most distros have preemption off anyway.
CON: The downside is that kvm_vcpu_yield_to is called also from kvm_vcpu_on_spin. Here we want to avoid the scheduler overhead for a wrong decision.

Won't most of that part be covered by:

      if (!ACCESS_ONCE(vcpu->preempted))

Hmm, right. Checking vcpu->preempted and PF_VCPU might boil down to the same. Would be good if to have to performance regression test, though.

Quoted 7 lines

vcpu->preempted is only set when scheduled out involuntarily. It is cleared when scheduled in. s390 sets it manually, to speed up waking up a vcpu.

So when our task is scheduled in (an PF_VCPU is set), this check will already avoid scheduler overhead in kvm_vcpu_on_spin() or am I missing something?

CC Raghavendra KT. Could be rerun your kernbench/sysbench/ebizzy setup on x86 to see if the patch in this thread causes any regression? If think your commit 7bc7ae25b143"kvm: Iterate over only vcpus that are preempted" might have really made the PF_VCPU check unnecessary

CC Michael Mueller, do we still have our yield performance setup handy to check if this patch causes any regression?

Christian

#6 lore raw

Re: [PATCH RFC 1/2] KVM: don't check for PF_VCPU when yielding

Am 28.11.2014 um 11:08 schrieb Raghavendra KT:

Quoted 4 lines

Was able to test the patch, here is the result: I have not tested with bigger VMs though. Results make it difficult to talk about any side effect of
patch if any.

Thanks a log.

If our assumption is correct, then this patch should have no side effect on x86. Do you have any confidence guess if the numbers below mean: no-change vs. regression vs improvement?

Christian

Quoted 65 lines

System 16 core 32cpu (+ht) sandybridge
with 4 guests of 16vcpu each

+-----------+-----------+-----------+------------+-----------+
kernbench (time taken lower is better)

+-----------+-----------+-----------+------------+-----------+
     base       %stdev      patched      %stdev    %improvement
+-----------+-----------+-----------+------------+-----------+
1x   53.1421     2.3086        54.6671     2.9673      -2.86966
2x   89.6858     6.4540        94.0626     6.8317      -4.88015
+-----------+-----------+-----------+------------+-----------+
+-----------+-----------+-----------+------------+-----------+
             ebizzy  (recors/sec higher is better)
+-----------+-----------+-----------+------------+-----------+
     base        %stdev          patched      %stdev    %improvement
+-----------+-----------+-----------+------------+-----------+
1x 14523.2500     8.4388    14928.8750     3.0478       2.79294
2x  3338.8750     1.4592     3270.8750     2.3980      -2.03661
+-----------+-----------+-----------+------------+-----------+
+-----------+-----------+-----------+------------+-----------+
             dbench  (Throughput higher is better)
+-----------+-----------+-----------+------------+-----------+
     base       %stdev           patched      %stdev    %improvement
+-----------+-----------+-----------+------------+-----------+
1x  6386.4737     1.0487    6703.9113     1.2298       4.97047
2x  2571.4712     1.3733    2571.8175     1.6919       0.01347
+-----------+-----------+-----------+------------+-----------+

Raghu

Quoted 19 lines · Christian Borntraeger

Am 26.11.2014 um 10:23 schrieb David Hildenbrand:

Quoted 6 lines
Quoted 3 lines

This change is a trade-off.
PRO: This patch would improve the case of preemption on s390. This is probably a corner case as most distros have preemption off anyway.
CON: The downside is that kvm_vcpu_yield_to is called also from kvm_vcpu_on_spin. Here we want to avoid the scheduler overhead for a wrong decision.

Won't most of that part be covered by:

      if (!ACCESS_ONCE(vcpu->preempted))

Hmm, right. Checking vcpu->preempted and PF_VCPU might boil down to the same. Would be good if to have to performance regression test, though.

Quoted 7 lines

vcpu->preempted is only set when scheduled out involuntarily. It is cleared when scheduled in. s390 sets it manually, to speed up waking up a vcpu.

So when our task is scheduled in (an PF_VCPU is set), this check will already avoid scheduler overhead in kvm_vcpu_on_spin() or am I missing something?

Quoted 7 lines

CC Raghavendra KT. Could be rerun your kernbench/sysbench/ebizzy setup on x86 to see if the patch in this thread causes any regression? If think your commit 7bc7ae25b143"kvm: Iterate over only vcpus that are preempted" might have really made the PF_VCPU check unnecessary

CC Michael Mueller, do we still have our yield performance setup handy to check if this patch causes any regression?

Christian

--
To unsubscribe from this list: send the line "unsubscribe kvm" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html

#7 lore raw

Re: [PATCH RFC 1/2] KVM: don't check for PF_VCPU when yielding

Quoted 10 lines · Christian Borntraeger

Am 28.11.2014 um 11:08 schrieb Raghavendra KT:

Quoted 4 lines

Was able to test the patch, here is the result: I have not tested with bigger VMs though. Results make it difficult to talk about any side effect of
patch if any.

Thanks a log.

If our assumption is correct, then this patch should have no side effect on x86. Do you have any confidence guess if the numbers below mean: no-change vs. regression vs improvement?

I am seeing very small improvement in <= 1x commit cases and for >1x overcommit, a very slight regression. But considering the test environment noises, I do not see much effect from the patch.

But I admit, I have not explored deeply about,
1. assumption of preempted approximately equals PF_VCPU case logic,
2. whether it helps for any future usages of yield_to against current sole usage of virtualization.

#8 lore raw

Re: [PATCH RFC 1/2] KVM: don't check for PF_VCPU when yielding

Quoted 12 lines · Christian Borntraeger
Quoted 10 lines

Am 28.11.2014 um 11:08 schrieb Raghavendra KT:

Quoted 4 lines

Was able to test the patch, here is the result: I have not tested with bigger VMs though. Results make it difficult to talk about any side effect of
patch if any.

Thanks a log.

If our assumption is correct, then this patch should have no side effect on x86. Do you have any confidence guess if the numbers below mean: no-change vs. regression vs improvement?

Quoted 7 lines

I am seeing very small improvement in <= 1x commit cases and for >1x overcommit, a very slight regression. But considering the test environment noises, I do not see much effect from the patch.

But I admit, I have not explored deeply about,
1. assumption of preempted approximately equals PF_VCPU case logic,

PF_VCPU is only a hint whether the target vcpu is executing the guest. If preemption is off or !s390, PF_VCPU means that the target vcpu is running and can't be preempted.

Although for preemption on and s390, this statement is false. Therefore this check is not always right.

Quoted 5 lines

2. whether it helps for any future usages of yield_to against current sole usage of virtualization.

Thanks for your test!

#15 lore raw
In reply to David Hildenbrand
Quoted 14 lines

This series improves yielding on architectures that cannot disable preemption while entering the guest and makes the creating thread of a VCPU the owning thread and therefore the yield target when yielding to that VCPU.

We should focus on the case creating thread == executing thread and therefore remove the complicated handling of PIDs involving synchronize_rcus.

This way we can speed up the creation of VCPUs and directly yield to the executing vcpu threads.

Please note that - in theory - all VCPU ioctls should be triggered from the same VCPU thread, so changing threads is not a scenario we should optimize.

Quoted 8 lines

David Hildenbrand (2):
KVM: don't check for PF_VCPU when yielding
KVM: thread creating a vcpu is the owner of that vcpu

 include/linux/kvm_host.h |  1 +
 virt/kvm/kvm_main.c      | 22 ++--------------------
 2 files changed, 3 insertions(+), 20 deletions(-)

Hi Paolo,

would be good if you could have a look at these patches.

Thanks!

maintainer
#9 lore raw

Re: [PATCH RFC 1/2] KVM: don't check for PF_VCPU when yielding

In reply to Raghavendra K T
Quoted 4 lines · Raghavendra K T

I am seeing very small improvement in <= 1x commit cases and for >1x overcommit, a very slight regression. But considering the test environment noises, I do not see much effect from the patch.

I think these results are the only one that could be statisically significant:

                   base     %stdev      patched     %stdev    %improvement
kernbench 1x    53.1421     2.3086      54.6671     2.9673      -2.86966
dbench    1x  6386.4737     1.0487    6703.9113     1.2298       4.97047

and, of course :) one of them says things get worse and the other says things get better.

Paolo

Quoted 7 lines

But I admit, I have not explored deeply about,
1. assumption of preempted approximately equals PF_VCPU case logic,
2. whether it helps for any future usages of yield_to against current sole usage of virtualization.

maintainer
#14 lore raw

Re: [PATCH RFC 2/2] KVM: thread creating a vcpu is the owner of that vcpu

In reply to David Hildenbrand

virt/kvm/kvm_main.c int vcpu_load(struct kvm_vcpu *vcpu) · line 135

Quoted 13 lines · David Hildenbrand
virt/kvm/kvm_main.c +0 -9
@@ -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);
-	}

I think it would make more sense to do this only for the KVM_RUN ioctl.

Paolo

maintainer
#16 lore raw
In reply to David Hildenbrand
Quoted 18 lines · David Hildenbrand
Quoted 18 lines

This series improves yielding on architectures that cannot disable preemption while entering the guest and makes the creating thread of a VCPU the owning thread and therefore the yield target when yielding to that VCPU.

We should focus on the case creating thread == executing thread and therefore remove the complicated handling of PIDs involving synchronize_rcus.

This way we can speed up the creation of VCPUs and directly yield to the executing vcpu threads.

Please note that - in theory - all VCPU ioctls should be triggered from the same VCPU thread, so changing threads is not a scenario we should optimize.

David Hildenbrand (2):
KVM: don't check for PF_VCPU when yielding
KVM: thread creating a vcpu is the owner of that vcpu

Quoted 8 lines
Quoted 4 lines
 include/linux/kvm_host.h |  1 +
 virt/kvm/kvm_main.c      | 22 ++--------------------
 2 files changed, 3 insertions(+), 20 deletions(-)

Hi Paolo,

would be good if you could have a look at these patches.

Sure.

I think patch 1 is fine and I am applying it. For patch 2, what about moving the ->pid assignment in the KVM_RUN case of kvm_vcpu_ioctl?

Paolo

#17 lore raw
Quoted 26 lines · David Hildenbrand
Quoted 23 lines
Quoted 22 lines

This series improves yielding on architectures that cannot disable preemption while entering the guest and makes the creating thread of a VCPU the owning thread and therefore the yield target when yielding to that VCPU.

We should focus on the case creating thread == executing thread and therefore remove the complicated handling of PIDs involving synchronize_rcus.

This way we can speed up the creation of VCPUs and directly yield to the executing vcpu threads.

Please note that - in theory - all VCPU ioctls should be triggered from the same VCPU thread, so changing threads is not a scenario we should optimize.

David Hildenbrand (2):
KVM: don't check for PF_VCPU when yielding
KVM: thread creating a vcpu is the owner of that vcpu

 include/linux/kvm_host.h |  1 +
 virt/kvm/kvm_main.c      | 22 ++--------------------
 2 files changed, 3 insertions(+), 20 deletions(-)
Quoted 8 lines
Quoted 3 lines

Hi Paolo,

would be good if you could have a look at these patches.

Sure.

I think patch 1 is fine and I am applying it. For patch 2, what about moving the ->pid assignment in the KVM_RUN case of kvm_vcpu_ioctl?

Thanks Paolo!

Well, do we have any known user that relies on this thread-switching in case of KVM_RUN? If yes, I am totally with you.

If not I'd prefer to get this code completely out, as it contains some unnecessary complexity. And maintaining such code that already had a couple of bugs in it without any benefit doesn't make much sense. What do you think?

David

Quoted 3 lines

Paolo

#18 lore raw
In reply to Paolo Bonzini

Am 03.12.2014 um 13:54 schrieb Paolo Bonzini:

Quoted 26 lines · David Hildenbrand
Quoted 23 lines
Quoted 22 lines

This series improves yielding on architectures that cannot disable preemption while entering the guest and makes the creating thread of a VCPU the owning thread and therefore the yield target when yielding to that VCPU.

We should focus on the case creating thread == executing thread and therefore remove the complicated handling of PIDs involving synchronize_rcus.

This way we can speed up the creation of VCPUs and directly yield to the executing vcpu threads.

Please note that - in theory - all VCPU ioctls should be triggered from the same VCPU thread, so changing threads is not a scenario we should optimize.

David Hildenbrand (2):
KVM: don't check for PF_VCPU when yielding
KVM: thread creating a vcpu is the owner of that vcpu

 include/linux/kvm_host.h |  1 +
 virt/kvm/kvm_main.c      | 22 ++--------------------
 2 files changed, 3 insertions(+), 20 deletions(-)
Quoted 8 lines
Quoted 3 lines

Hi Paolo,

would be good if you could have a look at these patches.

Sure.

I think patch 1 is fine and I am applying it. For patch 2, what about moving the ->pid assignment in the KVM_RUN case of kvm_vcpu_ioctl?

That was my initial patch for the rcu specific latencies (do you remember?) But IMHO patch 2 is actually the proper thing to do, no?

Christian

maintainer
#10 lore raw

Re: [PATCH RFC 1/2] KVM: don't check for PF_VCPU when yielding

In reply to David Hildenbrand
Quoted 27 lines · David Hildenbrand

As some architectures (e.g. s390) can't disable preemption while entering/leaving the guest, they won't receive the yield in all situations.

kvm_enter_guest() has to be called with preemption_disabled and will set PF_VCPU. After that point e.g. s390 reenables preemption and starts to execute the guest. The thread might therefore be scheduled out between kvm_enter_guest() and kvm_exit_guest(), resulting in PF_VCPU being set but not being run.

Please note that preemption has to stay enabled in order to correctly process page faults on s390.

Current code takes PF_VCPU as a hint that the VCPU thread is running and therefore needs no yield. yield_to() checks whether the target thread is running, so let's use the inbuilt functionality to make it independent of PF_VCPU and preemption.

---
 virt/kvm/kvm_main.c | 4 ----
 1 file changed, 4 deletions(-)
virt/kvm/kvm_main.c +0 -0
index 5b45330..184f52e 100644

virt/kvm/kvm_main.c int kvm_vcpu_yield_to(struct kvm_vcpu *target) · line 1787

Quoted 10 lines
virt/kvm/kvm_main.c +0 -4
@@ -1782,10 +1782,6 @@ int kvm_vcpu_yield_to(struct kvm_vcpu *target)
 	rcu_read_unlock();
 	if (!task)
 		return ret;
-	if (task->flags & PF_VCPU) {
-		put_task_struct(task);
-		return ret;
-	}
 	ret = yield_to(task, 1);
 	put_task_struct(task);
 

Applied with a rewritten commit message:

KVM: don't check for PF_VCPU when yielding

kvm_enter_guest() has to be called with preemption disabled and will set PF_VCPU. Current code takes PF_VCPU as a hint that the VCPU thread is running and therefore needs no yield.

However, the check on PF_VCPU is wrong on s390, where preemption has to stay enabled on s390 in order to correctly process page faults. Thus, s390 reenables preemption and starts to execute the guest. The thread might be scheduled out between kvm_enter_guest() and kvm_exit_guest(), resulting in PF_VCPU being set but not being run. When this happens, the opportunity for directed yield is missed.

However, this check is done already in kvm_vcpu_on_spin before calling kvm_vcpu_yield_loop:

        if (!ACCESS_ONCE(vcpu->preempted))
                continue;

so the check on PF_VCPU is superfluous in general, and this patch removes it.

#11 lore raw

Re: [PATCH RFC 1/2] KVM: don't check for PF_VCPU when yielding

Quoted 19 lines

Applied with a rewritten commit message:

KVM: don't check for PF_VCPU when yielding

kvm_enter_guest() has to be called with preemption disabled and will set PF_VCPU. Current code takes PF_VCPU as a hint that the VCPU thread is running and therefore needs no yield.

However, the check on PF_VCPU is wrong on s390, where preemption has to stay enabled on s390 in order to correctly process page faults. Thus, s390 reenables preemption and starts to execute the guest. The thread might be scheduled out between kvm_enter_guest() and kvm_exit_guest(), resulting in PF_VCPU being set but not being run. When this happens, the opportunity for directed yield is missed.

However, this check is done already in kvm_vcpu_on_spin before calling kvm_vcpu_yield_loop:

Quoted 9 lines
        if (!ACCESS_ONCE(vcpu->preempted))
                continue;

so the check on PF_VCPU is superfluous in general, and this patch removes it.

Perfect, thanks!

David

maintainer
#19 lore raw
In reply to Christian Borntraeger
Quoted 31 lines · Christian Borntraeger

Am 03.12.2014 um 13:54 schrieb Paolo Bonzini:

Quoted 30 lines · David Hildenbrand
Quoted 26 lines
Quoted 14 lines

This series improves yielding on architectures that cannot disable preemption while entering the guest and makes the creating thread of a VCPU the owning thread and therefore the yield target when yielding to that VCPU.

We should focus on the case creating thread == executing thread and therefore remove the complicated handling of PIDs involving synchronize_rcus.

This way we can speed up the creation of VCPUs and directly yield to the executing vcpu threads.

Please note that - in theory - all VCPU ioctls should be triggered from the same VCPU thread, so changing threads is not a scenario we should optimize.

Quoted 8 lines

David Hildenbrand (2):
KVM: don't check for PF_VCPU when yielding
KVM: thread creating a vcpu is the owner of that vcpu

 include/linux/kvm_host.h |  1 +
 virt/kvm/kvm_main.c      | 22 ++--------------------
 2 files changed, 3 insertions(+), 20 deletions(-)

Hi Paolo,

would be good if you could have a look at these patches.

Quoted 7 lines
Quoted 4 lines

Sure.

I think patch 1 is fine and I am applying it. For patch 2, what about moving the ->pid assignment in the KVM_RUN case of kvm_vcpu_ioctl?

That was my initial patch for the rcu specific latencies (do you remember?) But IMHO patch 2 is actually the proper thing to do, no?

Was it? :) Found it:
http://permalink.gmane.org/gmane.comp.emulators.kvm.devel/125694

I don't know... I think it's feasible to have a userspace that creates all VCPUs in the main thread, and then runs them from multiple threads.
Sure, those people would not have read the docs carefully, but it has
worked until now.

Wrongly-directed yields are a much better reason to apply your patch than QEMU slowness, and I've done that now.

Paolo