Skip to contentSkip to conversation

directed yield for Pause Loop Exiting

PATCH Applied Rik van Riel 5 participants 25 messages Feb 1, 2011 – Mar 4, 2011

Subsystems (5)
lore
Share & tools
lore
#1 lore raw

PATCH 1/7 sched: check the right ->nr_running in yield_task_fair

In reply to Rik van Riel

With CONFIG_FAIR_GROUP_SCHED, each task_group has its own cfs_rq. Yielding to a task from another cfs_rq may be worthwhile, since a process calling yield typically cannot use the CPU right now.

Therefor, we want to check the per-cpu nr_running, not the cgroup local one.

---
 kernel/sched_fair.c |    2 +-
 1 files changed, 1 insertions(+), 1 deletions(-)
kernel/sched_fair.c +1 -1
index c62ebae..7b338ac 100644
@@ -1304,7 +1304,7 @@ static void yield_task_fair(struct rq *rq)
 	/*
 	 * Are we the only task in the tree?
 	 */
-	if (unlikely(cfs_rq->nr_running == 1))
+	if (unlikely(rq->nr_running == 1))
 		return;
 
 	clear_buddies(cfs_rq, se);
#9 lore raw

PATCH 4/7 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.

This also means callers should only call yield_to when they really mean it. Calling it too often can result in the scheduler just ignoring the hint.

include/linux/sched.h +2 -0
index 2c79e92..6c43fc4 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 bool 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 +55 -0

61 lines not on this page · raw message

kernel/sched_fair.c +18 -0
index bd32d2a..728d3e8 100644
@@ -1794,6 +1794,23 @@ static void yield_task_fair(struct rq *rq)
 	set_skip_buddy(se);
 }
 
+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_entity takes care of fairness. */
+	if (preempt)
+		resched_task(rq->curr);
+
+	return true;
+}
+
 #ifdef CONFIG_SMP
 /**************************************************
  * Fair scheduling class load-balancing methods:
@@ -3987,6 +4004,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,
 
#5 lore raw

PATCH 3/7 sched: use a buddy to implement yield_task_fair

In reply to Rik van Riel

Use the buddy mechanism to implement yield_task_fair. This allows us to skip onto the next highest priority se at every level in the CFS tree, unless doing so would introduce gross unfairness in CPU time distribution.

We order the buddy selection in pick_next_entity to check yield first, then last, then next. We need next to be able to override yield, because it is possible for the "next" and "yield" task to be different processen in the same sub-tree of the CFS tree. When they are, we need to go into that sub-tree regardless of the "yield" hint, and pick the correct entity once we get to the right level.

kernel/sched.c +1 -1
index dc91a4d..7ff53e2 100644
@@ -327,7 +327,7 @@ struct cfs_rq {
 	 * 'curr' points to currently running entity on this cfs_rq.
 	 * It is set to NULL otherwise (i.e when none are currently running).
 	 */
-	struct sched_entity *curr, *next, *last;
+	struct sched_entity *curr, *next, *last, *skip;
 
 	unsigned int nr_spread_over;
 
kernel/sched_debug.c +1 -1
index 2e1b0d1..f9a2721 100644
@@ -183,7 +183,7 @@ void print_cfs_rq(struct seq_file *m, int cpu, struct cfs_rq *cfs_rq)
 
 	raw_spin_lock_irqsave(&rq->lock, flags);
 	if (cfs_rq->rb_leftmost)
-		MIN_vruntime = (__pick_next_entity(cfs_rq))->vruntime;
+		MIN_vruntime = (__pick_first_entity(cfs_rq))->vruntime;
 	last = __pick_last_entity(cfs_rq);
 	if (last)
 		max_vruntime = last->vruntime;
kernel/sched_fair.c +87 -51

202 lines not on this page · raw message

kernel/sysctl.c +0 -7
index 5abfa15..6212b4e 100644
@@ -375,13 +375,6 @@ static struct ctl_table kern_table[] = {
 		.mode		= 0644,
 		.proc_handler	= sched_rt_handler,
 	},
-	{
-		.procname	= "sched_compat_yield",
-		.data		= &sysctl_sched_compat_yield,
-		.maxlen		= sizeof(unsigned int),
-		.mode		= 0644,
-		.proc_handler	= proc_dointvec,
-	},
 #ifdef CONFIG_PROVE_LOCKING
 	{
 		.procname	= "prove_locking",
#21 lore raw

PATCH 5/7 export pid symbols needed for kvm_vcpu_on_spin

In reply to Rik van Riel

Export the symbols required for a race-free kvm_vcpu_on_spin.

kernel/fork.c +1 -0
index 3b159c5..adc8f47 100644
@@ -191,6 +191,7 @@ void __put_task_struct(struct task_struct *tsk)
 	if (!profile_handoff_task(tsk))
 		free_task(tsk);
 }
+EXPORT_SYMBOL_GPL(__put_task_struct);
 
 /*
  * macro override instead of weak attribute alias, to workaround
kernel/pid.c +2 -0
index 39b65b6..02f2212 100644
@@ -435,6 +435,7 @@ struct pid *get_task_pid(struct task_struct *task, enum pid_type type)
 	rcu_read_unlock();
 	return pid;
 }
+EXPORT_SYMBOL_GPL(get_task_pid);
 
 struct task_struct *get_pid_task(struct pid *pid, enum pid_type type)
 {
@@ -446,6 +447,7 @@ struct task_struct *get_pid_task(struct pid *pid, enum pid_type type)
 	rcu_read_unlock();
 	return result;
 }
+EXPORT_SYMBOL_GPL(get_pid_task);
 
 struct pid *find_get_pid(pid_t nr)
 {
#23 lore raw

PATCH 7/7 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 get another VCPU in the same KVM guest to run sooner.

This seems to give a 10-15% speedup in certain workloads.

include/linux/kvm_host.h +1 -0
index 9d56ed5..fab2250 100644
@@ -187,6 +187,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 +47 -10

65 lines not on this page · raw message

#22 lore raw

PATCH 6/7 kvm: keep track of which task is running a KVM vcpu

In reply to Rik van Riel

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.

include/linux/kvm_host.h +1 -0
index a055742..9d56ed5 100644
@@ -81,6 +81,7 @@ struct kvm_vcpu {
 #endif
 	int vcpu_id;
 	struct mutex mutex;
+	struct pid *pid;
 	int   cpu;
 	atomic_t guest_mode;
 	struct kvm_run *run;
virt/kvm/kvm_main.c +10 -0
index 5225052..0fa9a48 100644
@@ -117,6 +117,14 @@ void vcpu_load(struct kvm_vcpu *vcpu)
 	int cpu;
 
 	mutex_lock(&vcpu->mutex);
+	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);
+		synchronize_rcu();
+		put_pid(oldpid);
+	}
 	cpu = get_cpu();
 	preempt_notifier_register(&vcpu->preempt_notifier);
 	kvm_arch_vcpu_load(vcpu, cpu);
@@ -185,6 +193,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;
 	init_waitqueue_head(&vcpu->wq);
 
 	page = alloc_page(GFP_KERNEL | __GFP_ZERO);
@@ -208,6 +217,7 @@ EXPORT_SYMBOL_GPL(kvm_vcpu_init);
 
 void kvm_vcpu_uninit(struct kvm_vcpu *vcpu)
 {
+	put_pid(vcpu->pid);
 	kvm_arch_vcpu_uninit(vcpu);
 	free_page((unsigned long)vcpu->run);
 }
#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.

v8:
- some more changes and cleanups suggested by Peter
v7:
- move the vcpu to pid mapping to inside the vcpu->mutex
- rename ->yield to ->skip
- merge patch 5 into patch 4
v6:
- implement yield_task_fair in a way that works with task groups, this allows me to actually get a performance improvement!
- fix another race Avi pointed out, the code should be good now v5:
- fix the race condition Avi pointed out, by tracking vcpu->pid
- also allows us to yield to vcpu tasks that got preempted while in qemu userspace
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)

Benchmark results:

Two 4-CPU KVM guests are pinned to the same 4 physical CPUs.

One guest runs the AMQP performance test, the other guest runs 0, 2 or 4 infinite loops, for CPU overcommit factors of 0, 1.5 and 4.

The AMQP perftest is run 30 times, with message payloads of 8 and 16 bytes.

size8 no overcommit 1.5x overcommit 2x overcommit

no PLE 223801 135137 104951
PLE 224135 141105 118744

size16 no overcommit 1.5x overcommit 2x overcommit

no PLE 222424 126175 105299
PLE 222534 138082 132945

Note: this is with the KVM guests NOT running inside cgroups. There seems to be a CPU load balancing issue with cgroup fair group scheduling, which often results in one guest getting only 80% CPU time and the other guest 320%. That will have to be fixed to get meaningful results with cgroups.

CPU time division between the AMQP guest and the infinite loop guest were not exactly fair, but the guests got close to the same amount of CPU time in each test run.

There is a substantial amount of randomness in CPU time division between guests, but the performance improvement is consistent between multiple runs.

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

PATCH 2/7 sched: limit the scope of clear_buddies

The clear_buddies function does not seem to play well with the concept of hierarchical runqueues. In the following tree, task groups are represented by 'G', tasks by 'T', next by 'n' and last by 'l'.

     (nl)
    /    \

G(nl) G
/ \ \
T(l) T(n) T

This situation can arise when a task is woken up T(n), and the previously running task T(l) is marked last.

When clear_buddies is called from either T(l) or T(n), the next and last buddies of the group G(nl) will be cleared. This is not the desired result, since we would like to be able to find the other type of buddy in many cases.

This especially a worry when implementing yield_task_fair through the buddy system.

The fix is simple: only clear the buddy type that the task itself is indicated to be. As an added bonus, we stop walking up the tree when the buddy has already been cleared or pointed elsewhere.

---
 kernel/sched_fair.c |   30 +++++++++++++++++++++++-------
 1 files changed, 23 insertions(+), 7 deletions(-)
kernel/sched_fair.c +23 -7
index f4ee445..0321473 100644
@@ -784,19 +784,35 @@ enqueue_entity(struct cfs_rq *cfs_rq, struct sched_entity *se, int flags)
 		__enqueue_entity(cfs_rq, se);
 }
 
-static void __clear_buddies(struct cfs_rq *cfs_rq, struct sched_entity *se)
+static void __clear_buddies_last(struct sched_entity *se)
 {
-	if (!se || cfs_rq->last == se)
-		cfs_rq->last = NULL;
+	for_each_sched_entity(se) {
+		struct cfs_rq *cfs_rq = cfs_rq_of(se);
+		if (cfs_rq->last == se)
+			cfs_rq->last = NULL;
+		else
+			break;
+	}
+}
 
-	if (!se || cfs_rq->next == se)
-		cfs_rq->next = NULL;
+static void __clear_buddies_next(struct sched_entity *se)
+{
+	for_each_sched_entity(se) {
+		struct cfs_rq *cfs_rq = cfs_rq_of(se);
+		if (cfs_rq->next == se)
+			cfs_rq->next = NULL;
+		else
+			break;
+	}
 }
 
 static void clear_buddies(struct cfs_rq *cfs_rq, struct sched_entity *se)
 {
-	for_each_sched_entity(se)
-		__clear_buddies(cfs_rq_of(se), se);
+	if (cfs_rq->last == se)
+		__clear_buddies_last(se);
+
+	if (cfs_rq->next == se)
+		__clear_buddies_next(se);
 }
 
 static void
#10 lore raw

Re: [PATCH -v8a 4/7] sched: Add yield_to(task, preempt) functionality

In reply to Rik van Riel
Quoted 33 lines · Rik van Riel
kernel/sched.c +33 -0
+/**
+ * yield_to - yield the current processor to another thread in
+ * your thread group, or accelerate that thread toward the
+ * processor it's on.
+ *
+ * It's the caller's job to ensure that the target task struct
+ * can't go away on us before we can do any checks.
+ *
+ * Returns true if we indeed boosted the target task.
+ */
+bool __sched yield_to(struct task_struct *p, bool preempt)
+{
+	struct task_struct *curr = current;
+	struct rq *rq, *p_rq;
+	unsigned long flags;
+	bool yielded = 0;
+
+	local_irq_save(flags);
+	rq = this_rq();
+
+again:
+	p_rq = task_rq(p);
+	double_rq_lock(rq, p_rq);
+	while (task_rq(p) != p_rq) {
+		double_rq_unlock(rq, p_rq);
+		goto again;
+	}
+
+	if (!curr->sched_class->yield_to_task)
+		goto out;
+
+	if (curr->sched_class != p->sched_class)
+		goto out;

kernel/sched.c void __sched yield(void) · line 5315

Quoted 10 lines
kernel/sched.c +10 -0
+
+	if (task_running(p_rq, p) || p->state)
+		goto out;
+
+	yielded = curr->sched_class->yield_to_task(rq, p, preempt);
+
+	if (yielded) {
+		schedstat_inc(rq, yld_count);
+		current->sched_class->yield_task(rq);
+	}

We can avoid this second indirect function call by

Quoted 16 lines
(quoted diff) +14 -0
+
+out:
+	double_rq_unlock(rq, p_rq);
+	local_irq_restore(flags);
+
+	if (yielded)
+		schedule();
+
+	return yielded;
+}
+EXPORT_SYMBOL_GPL(yield_to);
 
 
+static bool yield_to_task_fair(struct rq *rq, struct task_struct *p, bool preempt)
+{
+	struct sched_entity *se = &p->se;

Review

Quoted 10 lines
(quoted diff) +10 -0
+
+	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_entity takes care of fairness. */
+	if (preempt)
+		resched_task(rq->curr);

calling: yield_task_fair(rq); here.

kernel/sched_fair.c static void yield_task_fair(struct rq *rq) · line 1812

Quoted 2 lines
kernel/sched_fair.c +2 -0
+	return true;
+}

I'll make that change on commit.

#6 lore raw

Re: [PATCH -v8a 3/7] sched: use a buddy to implement yield_task_fair

In reply to Rik van Riel
Quoted 7 lines · Rik van Riel
kernel/sysctl.c +0 -0
@@ -375,13 +375,6 @@ static struct ctl_table kern_table[] = {
                .mode           = 0644,
                .proc_handler   = sched_rt_handler,
        },

kernel/sysctl.c static struct ctl_table kern_table[] = { · line 380

Quoted 10 lines
kernel/sysctl.c +0 -7
-       {
-               .procname       = "sched_compat_yield",
-               .data           = &sysctl_sched_compat_yield,
-               .maxlen         = sizeof(unsigned int),
-               .mode           = 0644,
-               .proc_handler   = proc_dointvec,
-       },
 #ifdef CONFIG_PROVE_LOCKING
        {
                .procname       = "prove_locking",

There's another instance of sysctl_sched_compat_yield in sched.h, I'll remove that and its definition in sched_fair.c too ;-)

#11 lore raw

Re: [PATCH -v8a 4/7] sched: Add yield_to(task, preempt) functionality

In reply to Rik van Riel
Quoted 34 lines · Rik van Riel
(quoted diff) +34 -0
+bool __sched yield_to(struct task_struct *p, bool preempt)
+{
+       struct task_struct *curr = current;
+       struct rq *rq, *p_rq;
+       unsigned long flags;
+       bool yielded = 0;
+
+       local_irq_save(flags);
+       rq = this_rq();
+
+again:
+       p_rq = task_rq(p);
+       double_rq_lock(rq, p_rq);
+       while (task_rq(p) != p_rq) {
+               double_rq_unlock(rq, p_rq);
+               goto again;
+       }
+
+       if (!curr->sched_class->yield_to_task)
+               goto out;
+
+       if (curr->sched_class != p->sched_class)
+               goto out;
+
+       if (task_running(p_rq, p) || p->state)
+               goto out;
+
+       yielded = curr->sched_class->yield_to_task(rq, p, preempt);
+
+       if (yielded) {
+               schedstat_inc(rq, yld_count);
+               current->sched_class->yield_task(rq);
+       }
+

Review

Quoted 10 lines
(quoted diff) +10 -0
+out:
+       double_rq_unlock(rq, p_rq);
+       local_irq_restore(flags);
+
+       if (yielded)
+               schedule();
+
+       return yielded;
+}
+EXPORT_SYMBOL_GPL(yield_to); 

This also wants the below bit to successfully build on SMP=n,

---

kernel/sched.c +33 -0
Index: linux-2.6/kernel/sched.c
===================================================================
@@ -1686,6 +1686,39 @@ static void double_rq_unlock(struct rq *
 		__release(rq2->lock);
 }
 
+#else /* CONFIG_SMP */
+
+/*
+ * double_rq_lock - safely lock two runqueues
+ *
+ * Note this does not disable interrupts like task_rq_lock,
+ * you need to do so manually before calling.
+ */
+static void double_rq_lock(struct rq *rq1, struct rq *rq2)
+	__acquires(rq1->lock)
+	__acquires(rq2->lock)
+{
+	BUG_ON(!irqs_disabled());
+	BUG_ON(rq1 != rq2);
+	raw_spin_lock(&rq1->lock);
+	__acquire(rq2->lock);	/* Fake it out ;) */
+}
+
+/*
+ * double_rq_unlock - safely unlock two runqueues
+ *
+ * Note this does not restore interrupts like task_rq_unlock,
+ * you need to do so manually after calling.
+ */
+static void double_rq_unlock(struct rq *rq1, struct rq *rq2)
+	__releases(rq1->lock)
+	__releases(rq2->lock)
+{
+	BUG_ON(rq1 != rq2);
+	raw_spin_unlock(&rq1->lock);
+	__release(rq2->lock);
+}
+
 #endif
 
 static void calc_load_account_idle(struct rq *this_rq);
#7 lore raw

Re: [PATCH -v8a 3/7] sched: use a buddy to implement yield_task_fair

In reply to Rik van Riel
Quoted 30 lines · Rik van Riel
(quoted diff) +0 -30
-static void yield_task_fair(struct rq *rq)
-{
-       struct task_struct *curr = rq->curr;
-       struct cfs_rq *cfs_rq = task_cfs_rq(curr);
-       struct sched_entity *rightmost, *se = &curr->se;
-
-       /*
-        * Are we the only task in the tree?
-        */
-       if (unlikely(rq->nr_running == 1))
-               return;
-
-       clear_buddies(cfs_rq, se);
-
-       if (likely(!sysctl_sched_compat_yield) && curr->policy != SCHED_BATCH) {
-               update_rq_clock(rq);
-               /*
-                * Update run-time statistics of the 'current'.
-                */
-               update_curr(cfs_rq);
-
-               return;
-       }
-       /*
-        * Find the rightmost entry in the rbtree:
-        */
-       rightmost = __pick_last_entity(cfs_rq);
-       /*
-        * Already in the rightmost position?
-        */

Review

Quoted 10 lines
(quoted diff) +0 -10
-       if (unlikely(!rightmost || entity_before(rightmost, se)))
-               return;
-
-       /*
-        * Minimally necessary key value to be last in the tree:
-        * Upon rescheduling, sched_class::put_prev_task() will place
-        * 'current' within the tree based on its new key value.
-        */
-       se->vruntime = rightmost->vruntime + 1;
-} 

This also wants the below bit in order to build without warnings for SCHED_DEBUG=n:

---

kernel/sched_fair.c +1 -1
Index: linux-2.6/kernel/sched_fair.c
===================================================================
@@ -431,6 +431,7 @@ static struct sched_entity *__pick_next_
 	return rb_entry(next, struct sched_entity, run_node);
 }
 
+#ifdef CONFIG_SCHED_DEBUG
 static struct sched_entity *__pick_last_entity(struct cfs_rq *cfs_rq)
 {
 	struct rb_node *last = rb_last(&cfs_rq->tasks_timeline);
@@ -445,7 +446,6 @@ static struct sched_entity *__pick_last_
  * Scheduling class statistics methods:
  */
 
-#ifdef CONFIG_SCHED_DEBUG
 int sched_proc_update_handler(struct ctl_table *table, int write,
 		void __user *buffer, size_t *lenp,
 		loff_t *ppos)
#2 lore raw

[tip:sched/core] sched: Check the right ->nr_running in yield_task_fair()

In reply to Rik van Riel
Commit-ID:  725e7580aaf98e9f7b22b8ccfc640ad0c09e2b08
Gitweb:     http://git.kernel.org/tip/725e7580aaf98e9f7b22b8ccfc640ad0c09e2b08
Author:     Rik van Riel <[email protected]>
AuthorDate: Tue, 1 Feb 2011 09:47:15 -0500
Committer:  Ingo Molnar <[email protected]>
CommitDate: Thu, 3 Feb 2011 14:20:32 +0100

sched: Check the right ->nr_running in yield_task_fair()

With CONFIG_FAIR_GROUP_SCHED, each task_group has its own cfs_rq. Yielding to a task from another cfs_rq may be worthwhile, since a process calling yield typically cannot use the CPU right now.

Therefor, we want to check the per-cpu nr_running, not the cgroup local one.

LKML-Reference: <[email protected]>

---
 kernel/sched_fair.c |    2 +-
 1 files changed, 1 insertions(+), 1 deletions(-)
kernel/sched_fair.c +1 -1
index 55040f3..4de9905 100644
@@ -1331,7 +1331,7 @@ static void yield_task_fair(struct rq *rq)
 	/*
 	 * Are we the only task in the tree?
 	 */
-	if (unlikely(cfs_rq->nr_running == 1))
+	if (unlikely(rq->nr_running == 1))
 		return;
 
 	clear_buddies(cfs_rq, se);
#4 lore raw

[tip:sched/core] sched: Limit the scope of clear_buddies

In reply to Rik van Riel
Commit-ID:  2c13c919d9e9a3db9896143a501f83dcbbe1ced4
Gitweb:     http://git.kernel.org/tip/2c13c919d9e9a3db9896143a501f83dcbbe1ced4
Author:     Rik van Riel <[email protected]>
AuthorDate: Tue, 1 Feb 2011 09:48:37 -0500
Committer:  Ingo Molnar <[email protected]>
CommitDate: Thu, 3 Feb 2011 14:20:32 +0100

sched: Limit the scope of clear_buddies

The clear_buddies function does not seem to play well with the concept of hierarchical runqueues. In the following tree, task groups are represented by 'G', tasks by 'T', next by 'n' and last by 'l'.

     (nl)
    /    \

G(nl) G
/ \ \
T(l) T(n) T

This situation can arise when a task is woken up T(n), and the previously running task T(l) is marked last.

When clear_buddies is called from either T(l) or T(n), the next and last buddies of the group G(nl) will be cleared. This is not the desired result, since we would like to be able to find the other type of buddy in many cases.

This especially a worry when implementing yield_task_fair through the buddy system.

The fix is simple: only clear the buddy type that the task itself is indicated to be. As an added bonus, we stop walking up the tree when the buddy has already been cleared or pointed elsewhere.

LKML-Reference: <[email protected]>

---
 kernel/sched_fair.c |   30 +++++++++++++++++++++++-------
 1 files changed, 23 insertions(+), 7 deletions(-)
kernel/sched_fair.c +23 -7
index 4de9905..a785e08 100644
@@ -995,19 +995,35 @@ enqueue_entity(struct cfs_rq *cfs_rq, struct sched_entity *se, int flags)
 		list_add_leaf_cfs_rq(cfs_rq);
 }
 
-static void __clear_buddies(struct cfs_rq *cfs_rq, struct sched_entity *se)
+static void __clear_buddies_last(struct sched_entity *se)
 {
-	if (!se || cfs_rq->last == se)
-		cfs_rq->last = NULL;
+	for_each_sched_entity(se) {
+		struct cfs_rq *cfs_rq = cfs_rq_of(se);
+		if (cfs_rq->last == se)
+			cfs_rq->last = NULL;
+		else
+			break;
+	}
+}
 
-	if (!se || cfs_rq->next == se)
-		cfs_rq->next = NULL;
+static void __clear_buddies_next(struct sched_entity *se)
+{
+	for_each_sched_entity(se) {
+		struct cfs_rq *cfs_rq = cfs_rq_of(se);
+		if (cfs_rq->next == se)
+			cfs_rq->next = NULL;
+		else
+			break;
+	}
 }
 
 static void clear_buddies(struct cfs_rq *cfs_rq, struct sched_entity *se)
 {
-	for_each_sched_entity(se)
-		__clear_buddies(cfs_rq_of(se), se);
+	if (cfs_rq->last == se)
+		__clear_buddies_last(se);
+
+	if (cfs_rq->next == se)
+		__clear_buddies_next(se);
 }
 
 static void
#8 lore raw

[tip:sched/core] sched: Use a buddy to implement yield_task_fair()

In reply to Rik van Riel
Commit-ID:  ac53db596cc08ecb8040cfb6f71ae40c6f2041c4
Gitweb:     http://git.kernel.org/tip/ac53db596cc08ecb8040cfb6f71ae40c6f2041c4
Author:     Rik van Riel <[email protected]>
AuthorDate: Tue, 1 Feb 2011 09:51:03 -0500
Committer:  Ingo Molnar <[email protected]>
CommitDate: Thu, 3 Feb 2011 14:20:33 +0100

sched: Use a buddy to implement yield_task_fair()

Use the buddy mechanism to implement yield_task_fair. This allows us to skip onto the next highest priority se at every level in the CFS tree, unless doing so would introduce gross unfairness in CPU time distribution.

We order the buddy selection in pick_next_entity to check yield first, then last, then next. We need next to be able to override yield, because it is possible for the "next" and "yield" task to be different processen in the same sub-tree of the CFS tree. When they are, we need to go into that sub-tree regardless of the "yield" hint, and pick the correct entity once we get to the right level.

LKML-Reference: <[email protected]>

---
 include/linux/sched.h |    2 -
 kernel/sched.c        |    2 +-
 kernel/sched_debug.c  |    2 +-
 kernel/sched_fair.c   |  148 +++++++++++++++++++++++++++++--------------------
 kernel/sysctl.c       |    7 --
 5 files changed, 90 insertions(+), 71 deletions(-)
include/linux/sched.h +0 -2
index 0542774..4e9fad2 100644
@@ -1942,8 +1942,6 @@ int sched_rt_handler(struct ctl_table *table, int write,
 		void __user *buffer, size_t *lenp,
 		loff_t *ppos);
 
-extern unsigned int sysctl_sched_compat_yield;
-
 #ifdef CONFIG_SCHED_AUTOGROUP
 extern unsigned int sysctl_sched_autogroup_enabled;
 
kernel/sched.c +1 -1
index 477e1bc..ae5e1a1 100644
@@ -324,7 +324,7 @@ struct cfs_rq {
 	 * 'curr' points to currently running entity on this cfs_rq.
 	 * It is set to NULL otherwise (i.e when none are currently running).
 	 */
-	struct sched_entity *curr, *next, *last;
+	struct sched_entity *curr, *next, *last, *skip;
 
 	unsigned int nr_spread_over;
 
kernel/sched_debug.c +1 -1
index eb6cb8e..7bacd83 100644
@@ -179,7 +179,7 @@ void print_cfs_rq(struct seq_file *m, int cpu, struct cfs_rq *cfs_rq)
 
 	raw_spin_lock_irqsave(&rq->lock, flags);
 	if (cfs_rq->rb_leftmost)
-		MIN_vruntime = (__pick_next_entity(cfs_rq))->vruntime;
+		MIN_vruntime = (__pick_first_entity(cfs_rq))->vruntime;
 	last = __pick_last_entity(cfs_rq);
 	if (last)
 		max_vruntime = last->vruntime;
kernel/sched_fair.c +88 -60

224 lines not on this page · raw message

kernel/sysctl.c +0 -7
index bc86bb3..cbfda7e 100644
@@ -360,13 +360,6 @@ static struct ctl_table kern_table[] = {
 		.mode		= 0644,
 		.proc_handler	= sched_rt_handler,
 	},
-	{
-		.procname	= "sched_compat_yield",
-		.data		= &sysctl_sched_compat_yield,
-		.maxlen		= sizeof(unsigned int),
-		.mode		= 0644,
-		.proc_handler	= proc_dointvec,
-	},
 #ifdef CONFIG_SCHED_AUTOGROUP
 	{
 		.procname	= "sched_autogroup_enabled",
#12 lore raw

[tip:sched/core] sched: Add yield_to(task, preempt) functionality

In reply to Rik van Riel
Commit-ID:  d95f412200652694e63e64bfd49f0ae274a54479
Gitweb:     http://git.kernel.org/tip/d95f412200652694e63e64bfd49f0ae274a54479
Author:     Mike Galbraith <[email protected]>
AuthorDate: Tue, 1 Feb 2011 09:50:51 -0500
Committer:  Ingo Molnar <[email protected]>
CommitDate: Thu, 3 Feb 2011 14:20:33 +0100

sched: Add yield_to(task, preempt) functionality

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.

This also means callers should only call yield_to when they really mean it. Calling it too often can result in the scheduler just ignoring the hint.

LKML-Reference: <[email protected]>

---
 include/linux/sched.h |    2 +
 kernel/sched.c        |   85 +++++++++++++++++++++++++++++++++++++++++++++++++
 kernel/sched_fair.c   |   20 +++++++++++
 3 files changed, 107 insertions(+), 0 deletions(-)
include/linux/sched.h +2 -0
index 4e9fad2..c88b3bf 100644
@@ -1058,6 +1058,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);
 
@@ -1972,6 +1973,7 @@ static inline int rt_mutex_getprio(struct task_struct *p)
 # define rt_mutex_adjust_pi(p)		do { } while (0)
 #endif
 
+extern bool 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 +85 -0

97 lines not on this page · raw message

kernel/sched_fair.c +20 -0
index c0fbeb9..0270246 100644
@@ -1975,6 +1975,25 @@ static void yield_task_fair(struct rq *rq)
 	set_skip_buddy(se);
 }
 
+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_entity takes care of fairness. */
+	if (preempt)
+		resched_task(rq->curr);
+
+	yield_task_fair(rq);
+
+	return true;
+}
+
 #ifdef CONFIG_SMP
 /**************************************************
  * Fair scheduling class load-balancing methods:
@@ -4243,6 +4262,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,
 
Applied
#24 lore raw
In reply to Rik van Riel
Quoted 79 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.

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.

v8:
- some more changes and cleanups suggested by Peter
v7:
- move the vcpu to pid mapping to inside the vcpu->mutex
- rename ->yield to ->skip
- merge patch 5 into patch 4
v6:
- implement yield_task_fair in a way that works with task groups, this allows me to actually get a performance improvement!
- fix another race Avi pointed out, the code should be good now v5:
- fix the race condition Avi pointed out, by tracking vcpu->pid
- also allows us to yield to vcpu tasks that got preempted while in qemu userspace
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)

Benchmark results:

Two 4-CPU KVM guests are pinned to the same 4 physical CPUs.

One guest runs the AMQP performance test, the other guest runs 0, 2 or 4 infinite loops, for CPU overcommit factors of 0, 1.5 and 4.

The AMQP perftest is run 30 times, with message payloads of 8 and 16 bytes.

size8 no overcommit 1.5x overcommit 2x overcommit

no PLE 223801 135137 104951
PLE 224135 141105 118744

size16 no overcommit 1.5x overcommit 2x overcommit

no PLE 222424 126175 105299
PLE 222534 138082 132945

Note: this is with the KVM guests NOT running inside cgroups. There seems to be a CPU load balancing issue with cgroup fair group scheduling, which often results in one guest getting only 80% CPU time and the other guest 320%. That will have to be fixed to get meaningful results with cgroups.

Quoted 8 lines

CPU time division between the AMQP guest and the infinite loop guest were not exactly fair, but the guests got close to the same amount of CPU time in each test run.

There is a substantial amount of randomness in CPU time division between guests, but the performance improvement is consistent between multiple runs.

I've merged tip's sched/core, which includes yield_to(), and applied the final three patches. Thanks.

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

Re: [tip:sched/core] sched: Add yield_to(task, preempt) functionality

In reply to tip-bot for Mike Galbraith
Quoted 8 lines · tip-bot for Mike Galbraith

Commit-ID:  d95f412200652694e63e64bfd49f0ae274a54479
Gitweb:     http://git.kernel.org/tip/d95f412200652694e63e64bfd49f0ae274a54479
Author:     Mike Galbraith <[email protected]>
AuthorDate: Tue, 1 Feb 2011 09:50:51 -0500
Committer:  Ingo Molnar <[email protected]>
CommitDate: Thu, 3 Feb 2011 14:20:33 +0100

sched: Add yield_to(task, preempt) functionality

I was looking at this patch, while trying to figure out how best to use next buddy to solve another unrelated to this cgroup context switching problem. While going through this change I see something that I don't really understand (inlined below). Not sure whether what I am looking at is a bug or I am missing something very obvious.

Thanks in advance for clarification.

Quoted 7 lines

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.

<snip>

Quoted 68 lines

 static void calc_load_account_idle(struct rq *this_rq);

kernel/sched.c +52 -0
@@ -5448,6 +5481,58 @@ void __sched yield(void)
  }
  EXPORT_SYMBOL(yield);
 
+/**
+ * yield_to - yield the current processor to another thread in
+ * your thread group, or accelerate that thread toward the
+ * processor it's on.
+ *
+ * It's the caller's job to ensure that the target task struct
+ * can't go away on us before we can do any checks.
+ *
+ * Returns true if we indeed boosted the target task.
+ */
+bool __sched yield_to(struct task_struct *p, bool preempt)
+{
+       struct task_struct *curr = current;
+       struct rq *rq, *p_rq;
+       unsigned long flags;
+       bool yielded = 0;
+
+       local_irq_save(flags);
+       rq = this_rq();
+
+again:
+       p_rq = task_rq(p);
+       double_rq_lock(rq, p_rq);
+       while (task_rq(p) != p_rq) {
+               double_rq_unlock(rq, p_rq);
+               goto again;
+       }
+
+       if (!curr->sched_class->yield_to_task)
+               goto out;
+
+       if (curr->sched_class != p->sched_class)
+               goto out;
+
+       if (task_running(p_rq, p) || p->state)
+               goto out;
+
+       yielded = curr->sched_class->yield_to_task(rq, p, preempt);
+       if (yielded)
+               schedstat_inc(rq, yld_count);
+
+out:
+       double_rq_unlock(rq, p_rq);
+       local_irq_restore(flags);
+
+       if (yielded)
+               schedule();
+
+       return yielded;
+}
+EXPORT_SYMBOL_GPL(yield_to);
+
  /*
  * This task is about to go to sleep on IO. Increment rq->nr_iowait so
  * that process accounting knows that this is a task in IO wait state.
kernel/sched_fair.c +0 -0
index c0fbeb9..0270246 100644
@@ -1975,6 +1975,25 @@ static void yield_task_fair(struct rq *rq)
        set_skip_buddy(se);
  }

kernel/sched_fair.c static void yield_task_fair(struct rq *rq) · line 1986

Quoted 10 lines
kernel/sched_fair.c +9 -0
 
+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);

The below comment says about rescheduling p's CPU. But the rq variable we have here is the curr_rq and not p_rq. So, should this be done in yield_to() with p_rq. I did try to see the discussion on other versions of this patch. v3 and before had - "resched_task(task_of(p_cfs_rq->curr));" which seems to be correct...

Also, we already have a test of (task_running(p_rq, p) || p->state) in yield_to. Wondering why we need the additional (!se->on_rq) above?

kernel/sched_fair.c static void yield_task_fair(struct rq *rq) · line 1995

Quoted 9 lines
kernel/sched_fair.c +9 -0
+
+       /* Make p's CPU reschedule; pick_next_entity takes care of fairness. */
+       if (preempt)
+               resched_task(rq->curr);
+
+       yield_task_fair(rq);
+
+       return true;
+}

Thanks,
Venki

Quoted 18 lines
kernel/sched_fair.c +2 -1
+
  #ifdef CONFIG_SMP
  /**************************************************
  * Fair scheduling class load-balancing methods:
@@ -4243,6 +4262,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,
 
--

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

#14 lore raw

Re: [tip:sched/core] sched: Add yield_to(task, preempt) functionality

Quoted 98 lines · Venkatesh Pallipadi
Quoted 8 lines · tip-bot for Mike Galbraith
Commit-ID:  d95f412200652694e63e64bfd49f0ae274a54479
Gitweb:     http://git.kernel.org/tip/d95f412200652694e63e64bfd49f0ae274a54479
Author:     Mike Galbraith<[email protected]>
AuthorDate: Tue, 1 Feb 2011 09:50:51 -0500
Committer:  Ingo Molnar<[email protected]>
CommitDate: Thu, 3 Feb 2011 14:20:33 +0100

sched: Add yield_to(task, preempt) functionality

I was looking at this patch, while trying to figure out how best to use next buddy to solve another unrelated to this cgroup context switching problem. While going through this change I see something that I don't really understand (inlined below). Not sure whether what I am looking at is a bug or I am missing something very obvious.

Thanks in advance for clarification.

Quoted 7 lines

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.

<snip>

Quoted 69 lines
  static void calc_load_account_idle(struct rq *this_rq);
kernel/sched.c +52 -0
@@ -5448,6 +5481,58 @@ void __sched yield(void)
  }
  EXPORT_SYMBOL(yield);
 
+/**
+ * yield_to - yield the current processor to another thread in
+ * your thread group, or accelerate that thread toward the
+ * processor it's on.
+ *
+ * It's the caller's job to ensure that the target task struct
+ * can't go away on us before we can do any checks.
+ *
+ * Returns true if we indeed boosted the target task.
+ */
+bool __sched yield_to(struct task_struct *p, bool preempt)
+{
+       struct task_struct *curr = current;
+       struct rq *rq, *p_rq;
+       unsigned long flags;
+       bool yielded = 0;
+
+       local_irq_save(flags);
+       rq = this_rq();
+
+again:
+       p_rq = task_rq(p);
+       double_rq_lock(rq, p_rq);
+       while (task_rq(p) != p_rq) {
+               double_rq_unlock(rq, p_rq);
+               goto again;
+       }
+
+       if (!curr->sched_class->yield_to_task)
+               goto out;
+
+       if (curr->sched_class != p->sched_class)
+               goto out;
+
+       if (task_running(p_rq, p) || p->state)
+               goto out;
+
+       yielded = curr->sched_class->yield_to_task(rq, p, preempt);
+       if (yielded)
+               schedstat_inc(rq, yld_count);
+
+out:
+       double_rq_unlock(rq, p_rq);
+       local_irq_restore(flags);
+
+       if (yielded)
+               schedule();
+
+       return yielded;
+}
+EXPORT_SYMBOL_GPL(yield_to);
+
  /*
  * This task is about to go to sleep on IO. Increment rq->nr_iowait so
  * that process accounting knows that this is a task in IO wait state.
kernel/sched_fair.c +0 -0
index c0fbeb9..0270246 100644
@@ -1975,6 +1975,25 @@ static void yield_task_fair(struct rq *rq)
        set_skip_buddy(se);
  }
 
Quoted 15 lines

kernel/sched_fair.c static void yield_task_fair(struct rq *rq) · line 1986

Quoted 9 lines
kernel/sched_fair.c +9 -0
+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);

The below comment says about rescheduling p's CPU. But the rq variable we have here is the curr_rq and not p_rq. So, should this be done in yield_to() with p_rq. I did try to see the discussion on other versions of this patch. v3 and before had - "resched_task(task_of(p_cfs_rq->curr));" which seems to be correct...

You are correct. We are calling resched_task on the wrong task, we should call it on p's runqueue's current task...

Quoted 4 lines
Quoted 4 lines
kernel/sched_fair.c +4 -0
+
+       /* Make p's CPU reschedule; pick_next_entity takes care of fairness. */
+       if (preempt)
+               resched_task(rq->curr);
Signature · 1 line
All rights reversed
#15 lore raw

Re: [tip:sched/core] sched: Add yield_to(task, preempt) functionality

Quoted 10 lines · Rik van Riel
Quoted 5 lines · Venkatesh Pallipadi

The below comment says about rescheduling p's CPU. But the rq variable we have here is the curr_rq and not p_rq. So, should this be done in yield_to() with p_rq. I did try to see the discussion on other versions of this patch. v3 and before had - "resched_task(task_of(p_cfs_rq->curr));" which seems to be correct...

You are correct. We are calling resched_task on the wrong task, we should call it on p's runqueue's current task...

Are you going to send a fix?

Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
kernel/sched_fair.c +4 -0
+
+       /* Make p's CPU reschedule; pick_next_entity takes care of fairness. */
+       if (preempt)
+               resched_task(rq->curr);
#16 lore raw

PATCH 1/1 sched: resched proper CPU on yield_to

yield_to_task_fair() has code to resched the CPU of yielding task when the intention is to resched the CPU of the task that is being yielded to.

Change here fixes the problem and also makes the resched conditional on rq != p_rq.

I don't have a setup to test this code path. So, this is only compile tested right now.

---
 kernel/sched.c      |    9 ++++++++-
 kernel/sched_fair.c |    4 ----
 2 files changed, 8 insertions(+), 5 deletions(-)
kernel/sched.c +8 -1
index 5fb5aa2..afdbd09 100644
@@ -5495,8 +5495,15 @@ again:
 		goto out;
 
 	yielded = curr->sched_class->yield_to_task(rq, p, preempt);
-	if (yielded)
+	if (yielded) {
 		schedstat_inc(rq, yld_count);
+		/*
+		 * Make p's CPU reschedule; pick_next_entity takes care of
+		 * fairness.
+		 */
+		if (preempt && rq != p_rq)
+			resched_task(p_rq->curr);
+	}
 
 out:
 	double_rq_unlock(rq, p_rq);
kernel/sched_fair.c +0 -4
index 36e8f02..1b1c780 100644
@@ -2001,10 +2001,6 @@ static bool yield_to_task_fair(struct rq *rq, struct task_struct *p, bool preemp
 	/* Tell the scheduler that we'd really like pse to run next. */
 	set_next_buddy(se);
 
-	/* Make p's CPU reschedule; pick_next_entity takes care of fairness. */
-	if (preempt)
-		resched_task(rq->curr);
-
 	yield_task_fair(rq);
 
 	return true;
Signature · 1 line
1.7.3.1
#17 lore raw

Re: [PATCH] sched: resched proper CPU on yield_to

Quoted 5 lines · Venkatesh Pallipadi

yield_to_task_fair() has code to resched the CPU of yielding task when the intention is to resched the CPU of the task that is being yielded to.

Change here fixes the problem and also makes the resched conditional on rq != p_rq.

That would result in not rescheduling when current and p are on the same runqueue, in effect making yield_to a noop for the easiest case...

When rq != p_rq, we need to ensure both get rescheduled.

We want to have current not run right now (because it is waiting on a resource that's not available), and we do want p to run.

I'm about to fall over, so I'll go to sleep now.

I can send a patch tomorrow morning, unless you beat me to it :)

Signature · 1 line
All rights reversed
#18 lore raw

Re: [PATCH] sched: resched proper CPU on yield_to

Quoted 5 lines · Rik van Riel
Quoted 4 lines · Venkatesh Pallipadi

yield_to_task_fair() has code to resched the CPU of yielding task when the intention is to resched the CPU of the task that is being yielded to.

Quoted 9 lines
Quoted 2 lines

Change here fixes the problem and also makes the resched conditional on rq != p_rq.

That would result in not rescheduling when current and p are on the same runqueue, in effect making yield_to a noop for the easiest case...

When rq != p_rq, we need to ensure both get rescheduled.

Yes. There is a schedule() right after this change which should take care or resched on current CPU. Thats the reason I thought of skipping resched on current CPU. No?

Thanks,
Venki

Quoted 10 lines

We want to have current not run right now (because it is waiting on a resource that's not available), and we do want p to run.

I'm about to fall over, so I'll go to sleep now.

I can send a patch tomorrow morning, unless you beat me to it :)

--
All rights reversed

author Reviewed-by on v1 1/1
#19 lore raw

Re: [PATCH] sched: resched proper CPU on yield_to

In reply to Venkatesh Pallipadi
Quoted 10 lines · Venkatesh Pallipadi

yield_to_task_fair() has code to resched the CPU of yielding task when the intention is to resched the CPU of the task that is being yielded to.

Change here fixes the problem and also makes the resched conditional on rq != p_rq.

I don't have a setup to test this code path. So, this is only compile tested right now.

Never mind what I wrote before. Your patch is correct. (and I am way tired)

Signature · 1 line
All rights reversed
#20 lore raw

[tip:sched/core] sched: Resched proper CPU on yield_to()

In reply to Venkatesh Pallipadi
Commit-ID:  6d1cafd8b56ea726c10a5a104de57cc3ed8fa953
Gitweb:     http://git.kernel.org/tip/6d1cafd8b56ea726c10a5a104de57cc3ed8fa953
Author:     Venkatesh Pallipadi <[email protected]>
AuthorDate: Tue, 1 Mar 2011 16:28:21 -0800
Committer:  Ingo Molnar <[email protected]>
CommitDate: Fri, 4 Mar 2011 11:14:31 +0100

sched: Resched proper CPU on yield_to()

yield_to_task_fair() has code to resched the CPU of yielding task when the intention is to resched the CPU of the task that is being yielded to.

Change here fixes the problem and also makes the resched conditional on rq != p_rq.

LKML-Reference: <[email protected]>

---
 kernel/sched.c      |    9 ++++++++-
 kernel/sched_fair.c |    4 ----
 2 files changed, 8 insertions(+), 5 deletions(-)
kernel/sched.c +8 -1
index f303070..61452e8 100644
@@ -5522,8 +5522,15 @@ again:
 		goto out;
 
 	yielded = curr->sched_class->yield_to_task(rq, p, preempt);
-	if (yielded)
+	if (yielded) {
 		schedstat_inc(rq, yld_count);
+		/*
+		 * Make p's CPU reschedule; pick_next_entity takes care of
+		 * fairness.
+		 */
+		if (preempt && rq != p_rq)
+			resched_task(p_rq->curr);
+	}
 
 out:
 	double_rq_unlock(rq, p_rq);
kernel/sched_fair.c +0 -4
index 1438e13..3f7ec9e 100644
@@ -1987,10 +1987,6 @@ static bool yield_to_task_fair(struct rq *rq, struct task_struct *p, bool preemp
 	/* Tell the scheduler that we'd really like pse to run next. */
 	set_next_buddy(se);
 
-	/* Make p's CPU reschedule; pick_next_entity takes care of fairness. */
-	if (preempt)
-		resched_task(rq->curr);
-
 	yield_task_fair(rq);
 
 	return true;