Skip to contentSkip to conversation

directed yield for Pause Loop Exiting

RFC PATCH In review Rik van Riel 2 participants 9 messages Jan 13, 2011 – Jan 14, 2011

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

cover directed yield for Pause Loop Exiting

When running SMP virtual machines, it is possible for one VCPU to be spinning on a spinlock, while the VCPU that holds the spinlock is not currently running, because the host scheduler preempted it to run something else.

Both Intel and AMD CPUs have a feature that detects when a virtual CPU is spinning on a lock and will trap to the host.

The current KVM code sleeps for a bit whenever that happens, which results in eg. a 64 VCPU Windows guest taking forever and a bit to boot up. This is because the VCPU holding the lock is actually running and not sleeping, so the pause is counter-productive.

In other workloads a pause can also be counter-productive, with spinlock detection resulting in one guest giving up its CPU time to the others. Instead of spinning, it ends up simply not running much at all.

This patch series aims to fix that, by having a VCPU that spins give the remainder of its timeslice to another VCPU in the same guest before yielding the CPU - one that is runnable but got preempted, hopefully the lock holder.

v4:
- change to newer version of Mike Galbraith's yield_to implementation
- chainsaw out some code from Mike that looked like a great idea, but turned out to give weird interactions in practice
v3:
- more cleanups
- change to Mike Galbraith's yield_to implementation
- yield to spinning VCPUs, this seems to work better in some situations and has little downside potential
v2:
- make lots of cleanups and improvements suggested
- do not implement timeslice scheduling or fairness stuff yet, since it is not entirely clear how to do that right (suggestions welcome)

Signature · 1 line
All rights reversed.
#1 lore raw

RFC PATCH 1/3 kvm: keep track of which task is running a KVM vcpu

Keep track of which task is running a KVM vcpu. This helps us figure out later what task to wake up if we want to boost a vcpu that got preempted.

Unfortunately there are no guarantees that the same task always keeps the same vcpu, so we can only track the task across a single "run" of the vcpu.

---
- move vcpu->task manipulation as suggested by Chris Wright

 include/linux/kvm_host.h |    1 +
 virt/kvm/kvm_main.c      |    2 ++
 2 files changed, 3 insertions(+), 0 deletions(-)
include/linux/kvm_host.h +1 -0
index a055742..180085b 100644
@@ -81,6 +81,7 @@ struct kvm_vcpu {
 #endif
 	int vcpu_id;
 	struct mutex mutex;
+	struct task_struct *task;
 	int   cpu;
 	atomic_t guest_mode;
 	struct kvm_run *run;
virt/kvm/kvm_main.c +2 -0
index 5225052..c95bad1 100644
@@ -2248,6 +2248,7 @@ static void kvm_sched_in(struct preempt_notifier *pn, int cpu)
 {
 	struct kvm_vcpu *vcpu = preempt_notifier_to_vcpu(pn);
 
+	vcpu->task = NULL;
 	kvm_arch_vcpu_load(vcpu, cpu);
 }
 
@@ -2256,6 +2257,7 @@ static void kvm_sched_out(struct preempt_notifier *pn,
 {
 	struct kvm_vcpu *vcpu = preempt_notifier_to_vcpu(pn);
 
+	vcpu->task = current;
 	kvm_arch_vcpu_put(vcpu);
 }
 
#2 lore raw

RFC PATCH 2/3 sched: Add yield_to(task, preempt) functionality.

In reply to Rik van Riel

From: Mike Galbraith <[email protected]>

Currently only implemented for fair class tasks.

Add a yield_to_task method() to the fair scheduling class. allowing the caller of yield_to() to accelerate another thread in it's thread group, task group.

Implemented via a scheduler hint, using cfs_rq->next to encourage the target being selected. We can rely on pick_next_entity to keep things fair, so noone can accelerate a thread that has already used its fair share of CPU time.

include/linux/sched.h +2 -0
index 2c79e92..4492d84 100644
@@ -1047,6 +1047,7 @@ struct sched_class {
 	void (*enqueue_task) (struct rq *rq, struct task_struct *p, int flags);
 	void (*dequeue_task) (struct rq *rq, struct task_struct *p, int flags);
 	void (*yield_task) (struct rq *rq);
+	bool (*yield_to_task) (struct rq *rq, struct task_struct *p, bool preempt);
 
 	void (*check_preempt_curr) (struct rq *rq, struct task_struct *p, int flags);
 
@@ -1943,6 +1944,7 @@ static inline int rt_mutex_getprio(struct task_struct *p)
 # define rt_mutex_adjust_pi(p)		do { } while (0)
 #endif
 
+extern void yield_to(struct task_struct *p, bool preempt);
 extern void set_user_nice(struct task_struct *p, long nice);
 extern int task_prio(const struct task_struct *p);
 extern int task_nice(const struct task_struct *p);
kernel/sched.c +56 -0

62 lines not on this page · raw message

kernel/sched_fair.c +18 -0
index 00ebd76..5006f35 100644
@@ -1742,6 +1742,23 @@ static void put_prev_task_fair(struct rq *rq, struct task_struct *prev)
 	}
 }
 
+static bool yield_to_task_fair(struct rq *rq, struct task_struct *p, bool preempt)
+{
+	struct sched_entity *se = &p->se;
+
+	if (!se->on_rq)
+		return false;
+
+	/* Tell the scheduler that we'd really like pse to run next. */
+	set_next_buddy(se);
+
+	/* Make p's CPU reschedule; pick_next_entry takes care of fairness. */
+	if (preempt)
+		resched_task(rq->curr);
+
+	return true;
+}
+
 #ifdef CONFIG_SMP
 /**************************************************
  * Fair scheduling class load-balancing methods:
@@ -3935,6 +3952,7 @@ static const struct sched_class fair_sched_class = {
 	.enqueue_task		= enqueue_task_fair,
 	.dequeue_task		= dequeue_task_fair,
 	.yield_task		= yield_task_fair,
+	.yield_to_task		= yield_to_task_fair,
 
 	.check_preempt_curr	= check_preempt_wakeup,
 
#3 lore raw

RFC PATCH 3/3 kvm: use yield_to instead of sleep in kvm_vcpu_on_spin

In reply to Rik van Riel

Instead of sleeping in kvm_vcpu_on_spin, which can cause gigantic slowdowns of certain workloads, we instead use yield_to to hand the rest of our timeslice to another vcpu in the same KVM guest.

include/linux/kvm_host.h +1 -0
index c011ba3..ad3cb4a 100644
@@ -185,6 +185,7 @@ struct kvm {
 #endif
 	struct kvm_vcpu *vcpus[KVM_MAX_VCPUS];
 	atomic_t online_vcpus;
+	int last_boosted_vcpu;
 	struct list_head vm_list;
 	struct mutex lock;
 	struct kvm_io_bus *buses[KVM_NR_BUSES];
virt/kvm/kvm_main.c +36 -10

54 lines not on this page · raw message

#4 lore raw

Re: [RFC -v4 PATCH 3/3] kvm: use yield_to instead of sleep in kvm_vcpu_on_spin

Quoted 10 lines · Rik van Riel

Instead of sleeping in kvm_vcpu_on_spin, which can cause gigantic slowdowns of certain workloads, we instead use yield_to to hand the rest of our timeslice to another vcpu in the same KVM guest.

virt/kvm/kvm_main.c +4 -0
+	for (pass = 0; pass<  2&&  !yielded; pass++) {
+		kvm_for_each_vcpu(i, vcpu, kvm) {
+			struct task_struct *task = vcpu->task;
+			if (!pass&&  i<  last_boosted_vcpu) {

virt/kvm/kvm_main.c void kvm_resched(struct kvm_vcpu *vcpu) · line 1910

Quoted 10 lines
virt/kvm/kvm_main.c +10 -0
+				i = last_boosted_vcpu;
+				continue;
+			} else if (pass&&  i>  last_boosted_vcpu)
+				break;
+			if (vcpu == me)
+				continue;
+			if (!task)
+				continue;
+			if (waitqueue_active(&vcpu->wq))
+				continue;

Suppose the vcpu exits at this point, and its task terminates.

Review

Quoted 2 lines
(quoted diff) +2 -0
+			if (task->flags&  PF_VCPU)
+				continue;

Here you dereference freed memory.

virt/kvm/kvm_main.c void kvm_resched(struct kvm_vcpu *vcpu) · line 1915

Quoted 3 lines
virt/kvm/kvm_main.c +3 -0
+			kvm->last_boosted_vcpu = i;
+			yielded = 1;
+			yield_to(task, 1);

And here you do unimaginable things to that freed memory.

I think the first patch needs some reference counting... I'd move it to the outermost KVM_RUN loop to reduce the performance impact.

Quoted 5 lines
virt/kvm/kvm_main.c +3 -0
+			break;
+		}
+	}
  }
  EXPORT_SYMBOL_GPL(kvm_vcpu_on_spin);
Signature · 1 line
error compiling committee.c: too many arguments to function
#5 lore raw

Re: [RFC -v4 PATCH 3/3] kvm: use yield_to instead of sleep in kvm_vcpu_on_spin

Quoted 15 lines · Avi Kivity
Quoted 6 lines

+ for (pass = 0; pass< 2&& !yielded; pass++) {
+ kvm_for_each_vcpu(i, vcpu, kvm) {
+ struct task_struct *task = vcpu->task;
+ if (!pass&& i< last_boosted_vcpu) {
+ i = last_boosted_vcpu;
+ continue;

Quoted 8 lines

+ } else if (pass&& i> last_boosted_vcpu)
+ break;
+ if (vcpu == me)
+ continue;
+ if (!task)
+ continue;
+ if (waitqueue_active(&vcpu->wq))
+ continue;

Quoted 1 line

Suppose the vcpu exits at this point, and its task terminates.

Arghh, good point.

Quoted 2 lines

I think the first patch needs some reference counting... I'd move it to the outermost KVM_RUN loop to reduce the performance impact.

I don't see how refcounting from that other thread could possibly help, and I now see that the task_struct_cachep does not have SLAB_DESTROY_BY_LRU, either :(

What do you have in mind here that would both work and be acceptable to you as KVM maintainer?

Signature · 1 line
All rights reversed
#6 lore raw

Re: [RFC -v4 PATCH 3/3] kvm: use yield_to instead of sleep in kvm_vcpu_on_spin

Quoted 11 lines · Rik van Riel
Quoted 3 lines

I think the first patch needs some reference counting... I'd move it to the outermost KVM_RUN loop to reduce the performance impact.

I don't see how refcounting from that other thread could possibly help, and I now see that the task_struct_cachep does not have SLAB_DESTROY_BY_LRU, either :(

What do you have in mind here that would both work and be acceptable to you as KVM maintainer?

I think a 'struct pid' fits the bill here.

Signature · 1 line
error compiling committee.c: too many arguments to function
#7 lore raw

Re: [RFC -v4 PATCH 3/3] kvm: use yield_to instead of sleep in kvm_vcpu_on_spin

Quoted 9 lines · Avi Kivity
Quoted 8 lines · Rik van Riel
Quoted 3 lines

I think the first patch needs some reference counting... I'd move it to the outermost KVM_RUN loop to reduce the performance impact.

I don't see how refcounting from that other thread could possibly help, and I now see that the task_struct_cachep does not have SLAB_DESTROY_BY_LRU, either :(

Quoted 5 lines
Quoted 3 lines

What do you have in mind here that would both work and be acceptable to you as KVM maintainer?

I think a 'struct pid' fits the bill here.

Indeed it does. New patches on the way later, after I have
tested them some more.

Signature · 1 line
All rights reversed
#8 lore raw
In reply to Rik van Riel
Quoted 18 lines · Rik van Riel

When running SMP virtual machines, it is possible for one VCPU to be spinning on a spinlock, while the VCPU that holds the spinlock is not currently running, because the host scheduler preempted it to run something else.

Both Intel and AMD CPUs have a feature that detects when a virtual CPU is spinning on a lock and will trap to the host.

The current KVM code sleeps for a bit whenever that happens, which results in eg. a 64 VCPU Windows guest taking forever and a bit to boot up. This is because the VCPU holding the lock is actually running and not sleeping, so the pause is counter-productive.

In other workloads a pause can also be counter-productive, with spinlock detection resulting in one guest giving up its CPU time to the others. Instead of spinning, it ends up simply not running much at all.

Quoted 4 lines

This patch series aims to fix that, by having a VCPU that spins give the remainder of its timeslice to another VCPU in the same guest before yielding the CPU - one that is runnable but got preempted, hopefully the lock holder.

Can you share some benchmark results?

I'm mostly interested in moderately sized guests (4-8 vcpus) under conditions of no overcommit, and high overcommit (2x).

For no overcommit, I'd like to see comparisons against mainline with PLE disabled, to be sure there aren't significant regressions. For overcommit, comparisons against the no overcommit case. Comparisons against mainline, with or without PLE disabled, are uninteresting since we know it sucks both ways.

Signature · 1 line
error compiling committee.c: too many arguments to function