Skip to contentSkip to conversation

sched: Flatten the pick

RFC PATCH Superseded Peter Zijlstra 2 participants 12 messages Mar 17 – Mar 18

Subsystems (3)

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore

Peter Zijlstra

author maintainer
#0 lore raw

cover sched: Flatten the pick

Hi!

So cgroup scheduling has always been a pain in the arse. The problems start with weight distribution and end with hierachical picks and it all sucks.

The problems with weight distribution are related to that infernal global fraction:

             tg->w * grq_i->w
   ge_i->w = ----------------
             \Sum_j grq_j->w

which we've approximated reasonably well by now. However, the immediate consequence of this fraction is that the total group weight (tg->w) gets fragmented across all your CPUs. And at 64 CPUs that means your per-cpu cgroup weight ends up being a nice 19 task worth. And more CPUs more tiny. Combine with the fact that 256 CPU systems are relatively common these days, this becomes painful.

The common 'solution' is to inflate the group weight by 'nr_cpus'; the immediate problem with that is that when all load of a group gets concentrated on a single CPU, the per-cpu cgroup weight becomes insanely large, easily exceeding nice -20.

Additionally there are numerical limits on the max weight you can have before the math starts suffering overflows. As such there is a definite limit on the total group weight. Which has annoyed people ;-)

The first few patches add a knob /debug/sched/cgroup_mode and a few different options on how to deal with this. My favourite is 'concur', but obviously that is also the most expensive one :-/ It adds a tg->tasks counter which makes the update_tg_load_avg() thing more expensive.

I have some ideas but I figured I ought to share these things before sinking more time into it.

On to the hierarchical pick; this has been causing trouble for a very long time. So once again an attempt at flatting it. The basic idea is to keep the full hierarchical load tracking as-is, but keep all the runnable entities in a single level. The immediate concequence of all this is ofcourse that we need to constantly re-compute the effective weight of each entity as things progress.

Reweight is done on:
- enqueue
- pick -- or rather set_next_entity(.first=true)
- tick

So while the {en,de}queue operations are still O(depth) due to the full accounting mess, the pick is now a single level. Removing the intermediate levels that obscure runnability etc.

Anyway, these patches stopped crashing a few days ago and I figured it ought to be good enough to post.

Can also be had:

git://git.kernel.org/pub/scm/linux/kernel/git/peterz/queue.git sched/flat

---
 include/linux/cpuset.h |    6 +
 include/linux/sched.h  |    1 +
 kernel/cgroup/cpuset.c |   15 +
 kernel/sched/core.c    |   27 +-
 kernel/sched/debug.c   |  188 ++++++---
 kernel/sched/fair.c    | 1032 ++++++++++++++++++++++--------------------------
 kernel/sched/sched.h   |   20 +-
 7 files changed, 641 insertions(+), 648 deletions(-)
author maintainer
#1 lore raw

RFC PATCH 1/8 sched/debug: Collapse subsequent CONFIG_SCHED_CLASS_EXT sections

---
 kernel/sched/debug.c |   92 ++++++++++++++++++++++++---------------------------
 1 file changed, 44 insertions(+), 48 deletions(-)
kernel/sched/debug.c +44 -48
@@ -445,6 +445,8 @@ static const struct file_operations fair
 	.release	= single_release,
 };
 
+static struct dentry *debugfs_sched;
+
 #ifdef CONFIG_SCHED_CLASS_EXT
 static ssize_t
 sched_ext_server_runtime_write(struct file *filp, const char __user *ubuf,
@@ -477,75 +479,92 @@ static const struct file_operations ext_
 	.llseek		= seq_lseek,
 	.release	= single_release,
 };
-#endif /* CONFIG_SCHED_CLASS_EXT */
 
 static ssize_t
-sched_fair_server_period_write(struct file *filp, const char __user *ubuf,
-			       size_t cnt, loff_t *ppos)
+sched_ext_server_period_write(struct file *filp, const char __user *ubuf,
+			      size_t cnt, loff_t *ppos)
 {
 	long cpu = (long) ((struct seq_file *) filp->private_data)->private;
 	struct rq *rq = cpu_rq(cpu);
 
 	return sched_server_write_common(filp, ubuf, cnt, ppos, DL_PERIOD,
-					&rq->fair_server);
+					&rq->ext_server);
 }
 
-static int sched_fair_server_period_show(struct seq_file *m, void *v)
+static int sched_ext_server_period_show(struct seq_file *m, void *v)
 {
 	unsigned long cpu = (unsigned long) m->private;
 	struct rq *rq = cpu_rq(cpu);
 
-	return sched_server_show_common(m, v, DL_PERIOD, &rq->fair_server);
+	return sched_server_show_common(m, v, DL_PERIOD, &rq->ext_server);
 }
 
-static int sched_fair_server_period_open(struct inode *inode, struct file *filp)
+static int sched_ext_server_period_open(struct inode *inode, struct file *filp)
 {
-	return single_open(filp, sched_fair_server_period_show, inode->i_private);
+	return single_open(filp, sched_ext_server_period_show, inode->i_private);
 }
 
-static const struct file_operations fair_server_period_fops = {
-	.open		= sched_fair_server_period_open,
-	.write		= sched_fair_server_period_write,
+static const struct file_operations ext_server_period_fops = {
+	.open		= sched_ext_server_period_open,
+	.write		= sched_ext_server_period_write,
 	.read		= seq_read,
 	.llseek		= seq_lseek,
 	.release	= single_release,
 };
 
-#ifdef CONFIG_SCHED_CLASS_EXT
+static void debugfs_ext_server_init(void)
+{
+	struct dentry *d_ext;
+	unsigned long cpu;
+
+	d_ext = debugfs_create_dir("ext_server", debugfs_sched);
+	if (!d_ext)
+		return;
+
+	for_each_possible_cpu(cpu) {
+		struct dentry *d_cpu;
+		char buf[32];
+
+		snprintf(buf, sizeof(buf), "cpu%lu", cpu);
+		d_cpu = debugfs_create_dir(buf, d_ext);
+
+		debugfs_create_file("runtime", 0644, d_cpu, (void *) cpu, &ext_server_runtime_fops);
+		debugfs_create_file("period", 0644, d_cpu, (void *) cpu, &ext_server_period_fops);
+	}
+}
+#endif /* CONFIG_SCHED_CLASS_EXT */
+
 static ssize_t
-sched_ext_server_period_write(struct file *filp, const char __user *ubuf,
-			      size_t cnt, loff_t *ppos)
+sched_fair_server_period_write(struct file *filp, const char __user *ubuf,
+			       size_t cnt, loff_t *ppos)
 {
 	long cpu = (long) ((struct seq_file *) filp->private_data)->private;
 	struct rq *rq = cpu_rq(cpu);
 
 	return sched_server_write_common(filp, ubuf, cnt, ppos, DL_PERIOD,
-					&rq->ext_server);
+					&rq->fair_server);
 }
 
-static int sched_ext_server_period_show(struct seq_file *m, void *v)
+static int sched_fair_server_period_show(struct seq_file *m, void *v)
 {
 	unsigned long cpu = (unsigned long) m->private;
 	struct rq *rq = cpu_rq(cpu);
 
-	return sched_server_show_common(m, v, DL_PERIOD, &rq->ext_server);
+	return sched_server_show_common(m, v, DL_PERIOD, &rq->fair_server);
 }
 
-static int sched_ext_server_period_open(struct inode *inode, struct file *filp)
+static int sched_fair_server_period_open(struct inode *inode, struct file *filp)
 {
-	return single_open(filp, sched_ext_server_period_show, inode->i_private);
+	return single_open(filp, sched_fair_server_period_show, inode->i_private);
 }
 
-static const struct file_operations ext_server_period_fops = {
-	.open		= sched_ext_server_period_open,
-	.write		= sched_ext_server_period_write,
+static const struct file_operations fair_server_period_fops = {
+	.open		= sched_fair_server_period_open,
+	.write		= sched_fair_server_period_write,
 	.read		= seq_read,
 	.llseek		= seq_lseek,
 	.release	= single_release,
 };
-#endif /* CONFIG_SCHED_CLASS_EXT */
-
-static struct dentry *debugfs_sched;
 
 static void debugfs_fair_server_init(void)
 {
@@ -568,29 +587,6 @@ static void debugfs_fair_server_init(voi
 	}
 }
 
-#ifdef CONFIG_SCHED_CLASS_EXT
-static void debugfs_ext_server_init(void)
-{
-	struct dentry *d_ext;
-	unsigned long cpu;
-
-	d_ext = debugfs_create_dir("ext_server", debugfs_sched);
-	if (!d_ext)
-		return;
-
-	for_each_possible_cpu(cpu) {
-		struct dentry *d_cpu;
-		char buf[32];
-
-		snprintf(buf, sizeof(buf), "cpu%lu", cpu);
-		d_cpu = debugfs_create_dir(buf, d_ext);
-
-		debugfs_create_file("runtime", 0644, d_cpu, (void *) cpu, &ext_server_runtime_fops);
-		debugfs_create_file("period", 0644, d_cpu, (void *) cpu, &ext_server_period_fops);
-	}
-}
-#endif /* CONFIG_SCHED_CLASS_EXT */
-
 static __init int sched_init_debug(void)
 {
 	struct dentry __maybe_unused *numa;
author maintainer
#2 lore raw

RFC PATCH 2/8 sched/fair: Add cgroup_mode switch

In reply to Peter Zijlstra

Since calc_group_shares() has issues with 'many' CPUs, specifically the computed shares value gets to be roughly 1/nr_cpus, prepare to add a few alternative methods.

---
 kernel/sched/debug.c |   72 +++++++++++++++++++++++++++++++++++++++++++++++++++
 kernel/sched/sched.h |    1 
 2 files changed, 73 insertions(+)
kernel/sched/debug.c +72 -0
@@ -587,6 +587,74 @@ static void debugfs_fair_server_init(voi
 	}
 }
 
+#ifdef CONFIG_FAIR_GROUP_SCHED
+int cgroup_mode = 0;
+
+static const char *cgroup_mode_str[] = {
+	"smp",
+};
+
+static int sched_cgroup_mode(const char *str)
+{
+	for (int i = 0; i < ARRAY_SIZE(cgroup_mode_str); i++) {
+		if (!strcmp(str, cgroup_mode_str[i]))
+			return i;
+	}
+	return -EINVAL;
+}
+
+static ssize_t sched_cgroup_write(struct file *filp, const char __user *ubuf,
+				   size_t cnt, loff_t *ppos)
+{
+	char buf[16];
+	int mode;
+
+	if (cnt > 15)
+		cnt = 15;
+
+	if (copy_from_user(&buf, ubuf, cnt))
+		return -EFAULT;
+
+	buf[cnt] = 0;
+	mode = sched_cgroup_mode(strstrip(buf));
+	if (mode < 0)
+		return mode;
+
+	cgroup_mode = mode;
+
+	*ppos += cnt;
+	return cnt;
+}
+
+static int sched_cgroup_show(struct seq_file *m, void *v)
+{
+	for (int i = 0; i < ARRAY_SIZE(cgroup_mode_str); i++) {
+		if (cgroup_mode == i)
+			seq_puts(m, "(");
+		seq_puts(m, cgroup_mode_str[i]);
+		if (cgroup_mode == i)
+			seq_puts(m, ")");
+
+		seq_puts(m, " ");
+	}
+	seq_puts(m, "\n");
+	return 0;
+}
+
+static int sched_cgroup_open(struct inode *inode, struct file *filp)
+{
+	return single_open(filp, sched_cgroup_show, NULL);
+}
+
+static const struct file_operations sched_cgroup_fops = {
+	.open		= sched_cgroup_open,
+	.write		= sched_cgroup_write,
+	.read		= seq_read,
+	.llseek		= seq_lseek,
+	.release	= single_release,
+};
+#endif
+
 static __init int sched_init_debug(void)
 {
 	struct dentry __maybe_unused *numa;
@@ -624,6 +692,10 @@ static __init int sched_init_debug(void)
 
 	debugfs_create_file("debug", 0444, debugfs_sched, NULL, &sched_debug_fops);
 
+#ifdef CONFIG_FAIR_GROUP_SCHED
+	debugfs_create_file("cgroup_mode", 0444, debugfs_sched, NULL, &sched_cgroup_fops);
+#endif
+
 	debugfs_fair_server_init();
 #ifdef CONFIG_SCHED_CLASS_EXT
 	debugfs_ext_server_init();
kernel/sched/sched.h +1 -0
@@ -565,6 +565,7 @@ static inline struct task_group *css_tg(
 extern int tg_nop(struct task_group *tg, void *data);
 
 #ifdef CONFIG_FAIR_GROUP_SCHED
+extern int cgroup_mode;
 extern void free_fair_sched_group(struct task_group *tg);
 extern int alloc_fair_sched_group(struct task_group *tg, struct task_group *parent);
 extern void online_fair_sched_group(struct task_group *tg);
author maintainer
#3 lore raw

RFC PATCH 3/8 sched/fair: Add cgroup_mode: UP

In reply to Peter Zijlstra

Instead of calculating the proportional fraction of tg->shares for each CPU, just give each CPU the full measure, ignoring these pesky SMP problems.

---
 kernel/sched/debug.c |    3 ++-
 kernel/sched/fair.c  |   19 ++++++++++++++++++-
 2 files changed, 20 insertions(+), 2 deletions(-)
kernel/sched/debug.c +2 -1
@@ -588,9 +588,10 @@ static void debugfs_fair_server_init(voi
 }
 
 #ifdef CONFIG_FAIR_GROUP_SCHED
-int cgroup_mode = 0;
+int cgroup_mode = 1;
 
 static const char *cgroup_mode_str[] = {
+	"up",
 	"smp",
 };
 
kernel/sched/fair.c +18 -1
@@ -4152,7 +4152,7 @@ static inline int throttled_hierarchy(st
  *
  * hence icky!
  */
-static long calc_group_shares(struct cfs_rq *cfs_rq)
+static long calc_smp_shares(struct cfs_rq *cfs_rq)
 {
 	long tg_weight, tg_shares, load, shares;
 	struct task_group *tg = cfs_rq->tg;
@@ -4187,6 +4187,23 @@ static long calc_group_shares(struct cfs
 }
 
 /*
+ * Ignore this pesky SMP stuff, use (4).
+ */
+static long calc_up_shares(struct cfs_rq *cfs_rq)
+{
+	struct task_group *tg = cfs_rq->tg;
+	return READ_ONCE(tg->shares);
+}
+
+static long calc_group_shares(struct cfs_rq *cfs_rq)
+{
+	if (cgroup_mode == 0)
+		return calc_up_shares(cfs_rq);
+
+	return calc_smp_shares(cfs_rq);
+}
+
+/*
  * Recomputes the group entity based on the current state of its group
  * runqueue.
  */
author maintainer
#4 lore raw

RFC PATCH 4/8 sched/fair: Add cgroup_mode: MAX

In reply to Peter Zijlstra

In order to avoid the CPU shares becoming tiny '1 / nr_cpus', assume each cgroup is maximally concurrent and distrubute 'nr_cpus * tg->shares', such that each CPU ends up with a 'tg->shares' sized fraction (on average).

There is the corner case, when a cgroup is minimally loaded, eg a single spinner, therefore limit the CPU shares to that of a nice -20 task to avoid getting too much load.

It was previously suggested to allow raising cpu.weight to '100 * nr_cpus' to combat this same problem, but the problem there is the above corner case, allowing multiple cgroups with such immense weight to the runqueue has significant problems.

It would drown the kthreads, but it also risks overflowing the load values.

---
 include/linux/cpuset.h |    6 +++++
 kernel/cgroup/cpuset.c |   15 ++++++++++++++
 kernel/sched/debug.c   |    1 
 kernel/sched/fair.c    |   50 ++++++++++++++++++++++++++++++++++++++++++++-----
 4 files changed, 67 insertions(+), 5 deletions(-)
include/linux/cpuset.h +6 -0
@@ -80,6 +80,7 @@ extern void lockdep_assert_cpuset_lock_h
 extern void cpuset_cpus_allowed_locked(struct task_struct *p, struct cpumask *mask);
 extern void cpuset_cpus_allowed(struct task_struct *p, struct cpumask *mask);
 extern bool cpuset_cpus_allowed_fallback(struct task_struct *p);
+extern int cpuset_num_cpus(struct cgroup *cgroup);
 extern nodemask_t cpuset_mems_allowed(struct task_struct *p);
 #define cpuset_current_mems_allowed (current->mems_allowed)
 void cpuset_init_current_mems_allowed(void);
@@ -216,6 +217,11 @@ static inline bool cpuset_cpus_allowed_f
 	return false;
 }
 
+static inline int cpuset_num_cpus(struct cgroup *cgroup)
+{
+	return num_online_cpus();
+}
+
 static inline nodemask_t cpuset_mems_allowed(struct task_struct *p)
 {
 	return node_possible_map;
kernel/cgroup/cpuset.c +15 -0
@@ -4097,6 +4097,21 @@ bool cpuset_cpus_allowed_fallback(struct
 	return changed;
 }
 
+int cpuset_num_cpus(struct cgroup *cgrp)
+{
+	int nr = num_online_cpus();
+	struct cpuset *cs;
+
+	if (is_in_v2_mode()) {
+		guard(rcu)();
+		cs = css_cs(cgroup_e_css(cgrp, &cpuset_cgrp_subsys));
+		if (cs)
+			nr = cpumask_weight(cs->effective_cpus);
+	}
+
+	return nr;
+}
+
 void __init cpuset_init_current_mems_allowed(void)
 {
 	nodes_setall(current->mems_allowed);
kernel/sched/debug.c +1 -0
@@ -593,6 +593,7 @@ int cgroup_mode = 1;
 static const char *cgroup_mode_str[] = {
 	"up",
 	"smp",
+	"max",
 };
 
 static int sched_cgroup_mode(const char *str)
kernel/sched/fair.c +45 -5

70 lines not on this page · raw message

author maintainer
#5 lore raw

RFC PATCH 5/8 sched/fair: Add cgroup_mode: CONCUR

In reply to Peter Zijlstra

A variation of MAX; where instead of assuming maximal concurrent, this scales with 'min(nr_tasks, nr_cpus)'. This handles the low concurrency cases more gracefully, with the exception of CPU affnity.

Note: the tracking of tg->tasks is somewhat expensive :-/

---
 kernel/sched/debug.c |    1 +
 kernel/sched/fair.c  |   38 +++++++++++++++++++++++++++++++++++---
 kernel/sched/sched.h |    3 +++
 3 files changed, 39 insertions(+), 3 deletions(-)
kernel/sched/debug.c +1 -0
@@ -593,6 +593,7 @@ int cgroup_mode = 1;
 static const char *cgroup_mode_str[] = {
 	"up",
 	"smp",
+	"concur",
 	"max",
 };
 
kernel/sched/fair.c +35 -3
@@ -4210,6 +4210,30 @@ static long calc_max_shares(struct cfs_r
 	return __calc_smp_shares(cfs_rq, tg_shares * nr, max_shares);
 }
 
+static inline int tg_tasks(struct task_group *tg)
+{
+	return max(1, atomic_long_read(&tg->tasks));
+}
+
+/*
+ * Func: min(fraction(num * tg->shares), nice -20); where
+ *       num = min(nr_tasks, nr_cpus)
+ *
+ * Similar to max, except scale with min(nr_tasks, nr_cpus), which gives
+ * a far more natural distrubution. Can still create edge case using CPU
+ * affinity.
+ */
+static long calc_concur_shares(struct cfs_rq *cfs_rq)
+{
+	struct task_group *tg = cfs_rq->tg;
+	int nr_cpus = tg_cpus(tg);
+	int nr_tasks = tg_tasks(tg);
+	int nr = min(nr_tasks, nr_cpus);
+	long tg_shares = READ_ONCE(tg->shares);
+	long max_shares = scale_load(sched_prio_to_weight[0]);
+	return __calc_smp_shares(cfs_rq, nr * tg_shares, max_shares);
+}
+
 /*
  * Func: fraction(tg->shares)
  *
@@ -4236,6 +4260,8 @@ static long calc_group_shares(struct cfs
 	if (cgroup_mode == 0)
 		return calc_up_shares(cfs_rq);
 	if (cgroup_mode == 2)
+		return calc_concur_shares(cfs_rq);
+	if (cgroup_mode == 3)
 		return calc_max_shares(cfs_rq);
 
 	return calc_smp_shares(cfs_rq);
@@ -4381,7 +4407,7 @@ static inline bool cfs_rq_is_decayed(str
  */
 static inline void update_tg_load_avg(struct cfs_rq *cfs_rq)
 {
-	long delta;
+	long delta, dt;
 	u64 now;
 
 	/*
@@ -4403,16 +4429,19 @@ static inline void update_tg_load_avg(st
 		return;
 
 	delta = cfs_rq->avg.load_avg - cfs_rq->tg_load_avg_contrib;
-	if (abs(delta) > cfs_rq->tg_load_avg_contrib / 64) {
+	dt = cfs_rq->h_nr_queued - cfs_rq->tg_tasks_contrib;
+	if (dt || abs(delta) > cfs_rq->tg_load_avg_contrib / 64) {
 		atomic_long_add(delta, &cfs_rq->tg->load_avg);
+		atomic_long_add(dt, &cfs_rq->tg->tasks);
 		cfs_rq->tg_load_avg_contrib = cfs_rq->avg.load_avg;
+		cfs_rq->tg_tasks_contrib = cfs_rq->h_nr_queued;
 		cfs_rq->last_update_tg_load_avg = now;
 	}
 }
 
 static inline void clear_tg_load_avg(struct cfs_rq *cfs_rq)
 {
-	long delta;
+	long delta, dt;
 	u64 now;
 
 	/*
@@ -4423,8 +4452,11 @@ static inline void clear_tg_load_avg(str
 
 	now = sched_clock_cpu(cpu_of(rq_of(cfs_rq)));
 	delta = 0 - cfs_rq->tg_load_avg_contrib;
+	dt = 0 - cfs_rq->tg_tasks_contrib;
 	atomic_long_add(delta, &cfs_rq->tg->load_avg);
+	atomic_long_add(dt, &cfs_rq->tg->tasks);
 	cfs_rq->tg_load_avg_contrib = 0;
+	cfs_rq->tg_tasks_contrib = 0;
 	cfs_rq->last_update_tg_load_avg = now;
 }
 
kernel/sched/sched.h +3 -0
@@ -491,6 +491,8 @@ struct task_group {
 	 * will also be accessed at each tick.
 	 */
 	atomic_long_t		load_avg ____cacheline_aligned;
+	atomic_long_t		tasks;
+
 #endif /* CONFIG_FAIR_GROUP_SCHED */
 
 #ifdef CONFIG_RT_GROUP_SCHED
@@ -720,6 +722,7 @@ struct cfs_rq {
 #ifdef CONFIG_FAIR_GROUP_SCHED
 	u64			last_update_tg_load_avg;
 	unsigned long		tg_load_avg_contrib;
+	unsigned long		tg_tasks_contrib;
 	long			propagate;
 	long			prop_runnable_sum;
 
author maintainer
#6 lore raw

RFC PATCH 6/8 sched/fair: Add newidle balance to pick_task_fair()

In reply to Peter Zijlstra

With commit 50653216e4ff ("sched: Add support to pick functions to take rf") removing the balance callback, the pick_task() callback is in charge of newidle balancing.

This means pick_task_fair() should do so too. This hasn't been a problem in practise because pick_next_task_fair() is used. However, since we'll be removing that one shortly, make sure pick_next_task() is up to scratch.

---
 kernel/sched/fair.c |   13 ++++++++++++-
 1 file changed, 12 insertions(+), 1 deletion(-)
kernel/sched/fair.c +12 -1
@@ -8892,16 +8892,18 @@ static void wakeup_preempt_fair(struct r
 }
 
 static struct task_struct *pick_task_fair(struct rq *rq, struct rq_flags *rf)
+	__must_hold(__rq_lockp(rq))
 {
 	struct sched_entity *se;
 	struct cfs_rq *cfs_rq;
 	struct task_struct *p;
 	bool throttled;
+	int new_tasks;
 
 again:
 	cfs_rq = &rq->cfs;
 	if (!cfs_rq->nr_queued)
-		return NULL;
+		goto idle;
 
 	throttled = false;
 
@@ -8922,6 +8924,14 @@ static struct task_struct *pick_task_fai
 	if (unlikely(throttled))
 		task_throttle_setup_work(p);
 	return p;
+
+idle:
+	new_tasks = sched_balance_newidle(rq, rf);
+	if (new_tasks < 0)
+		return RETRY_TASK;
+	if (new_tasks > 0)
+		goto again;
+	return NULL;
 }
 
 static void __set_next_task_fair(struct rq *rq, struct task_struct *p, bool first);
@@ -9011,6 +9021,7 @@ pick_next_task_fair(struct rq *rq, struc
 
 static struct task_struct *
 fair_server_pick_task(struct sched_dl_entity *dl_se, struct rq_flags *rf)
+	__must_hold(__rq_lockp(dl_se->rq))
 {
 	return pick_task_fair(dl_se->rq, rf);
 }
author maintainer
#7 lore raw

RFC PATCH 7/8 sched: Remove sched_class::pick_next_task()

In reply to Peter Zijlstra

The reason for pick_next_task_fair() is the put/set optimization that avoids touching the common ancestors. However, it is possible to implement this in the put_prev_task() and set_next_task() calls as used in put_prev_set_next_task().

Notably, put_prev_set_next_task() is the only site that:

- calls put_prev_task() with a .next argument;
- calls set_next_task() with .first = true.

This means that put_prev_task() can determine the common hierarchy and stop there, and then set_next_task() can terminate where put_prev_task stopped.

---
 kernel/sched/core.c  |   27 +++------
 kernel/sched/fair.c  |  153 ++++++++++++++-------------------------------------
 kernel/sched/sched.h |   14 ----
 3 files changed, 52 insertions(+), 142 deletions(-)
kernel/sched/core.c +9 -18
@@ -5924,16 +5924,15 @@ __pick_next_task(struct rq *rq, struct t
 	if (likely(!sched_class_above(prev->sched_class, &fair_sched_class) &&
 		   rq->nr_running == rq->cfs.h_nr_queued)) {
 
-		p = pick_next_task_fair(rq, prev, rf);
+		p = pick_task_fair(rq, rf);
 		if (unlikely(p == RETRY_TASK))
 			goto restart;
 
 		/* Assume the next prioritized class is idle_sched_class */
-		if (!p) {
+		if (!p)
 			p = pick_task_idle(rq, rf);
-			put_prev_set_next_task(rq, prev, p);
-		}
 
+		put_prev_set_next_task(rq, prev, p);
 		return p;
 	}
 
@@ -5941,20 +5940,12 @@ __pick_next_task(struct rq *rq, struct t
 	prev_balance(rq, prev, rf);
 
 	for_each_active_class(class) {
-		if (class->pick_next_task) {
-			p = class->pick_next_task(rq, prev, rf);
-			if (unlikely(p == RETRY_TASK))
-				goto restart;
-			if (p)
-				return p;
-		} else {
-			p = class->pick_task(rq, rf);
-			if (unlikely(p == RETRY_TASK))
-				goto restart;
-			if (p) {
-				put_prev_set_next_task(rq, prev, p);
-				return p;
-			}
+		p = class->pick_task(rq, rf);
+		if (unlikely(p == RETRY_TASK))
+			goto restart;
+		if (p) {
+			put_prev_set_next_task(rq, prev, p);
+			return p;
 		}
 	}
 
kernel/sched/fair.c +42 -111

194 lines not on this page · raw message

kernel/sched/sched.h +1 -13
@@ -2538,17 +2538,6 @@ struct sched_class {
 	 * schedule/pick_next_task: rq->lock
 	 */
 	struct task_struct *(*pick_task)(struct rq *rq, struct rq_flags *rf);
-	/*
-	 * Optional! When implemented pick_next_task() should be equivalent to:
-	 *
-	 *   next = pick_task();
-	 *   if (next) {
-	 *       put_prev_task(prev);
-	 *       set_next_task_first(next);
-	 *   }
-	 */
-	struct task_struct *(*pick_next_task)(struct rq *rq, struct task_struct *prev,
-					      struct rq_flags *rf);
 
 	/*
 	 * sched_change:
@@ -2761,8 +2750,7 @@ static inline bool sched_fair_runnable(s
 	return rq->cfs.nr_queued > 0;
 }
 
-extern struct task_struct *pick_next_task_fair(struct rq *rq, struct task_struct *prev,
-					       struct rq_flags *rf);
+extern struct task_struct *pick_task_fair(struct rq *rq, struct rq_flags *rf);
 extern struct task_struct *pick_task_idle(struct rq *rq, struct rq_flags *rf);
 
 #define SCA_CHECK		0x01
author maintainer
#8 lore raw

RFC PATCH 8/8 sched/eevdf: Move to a single runqueue

In reply to Peter Zijlstra

Change fair/cgroup to a single runqueue.

Infamously fair/cgroup isn't working for a number of people; typically the complaint is latencies and/or overhead. The latency issue is due to the intermediate entries that represent a combination of tasks and thereby obfuscate the runnability of tasks.

The approach here is to leave the cgroup hierarchy as is; including the intermediate enqueue/dequeue but move the actual EEVDF runqueue outside. This means things like the shares_weight approximation are fully preserved.

That is, given a hierarchy like:

        R
        |
        se--G1
            / \
      G2--se   se--G3
     / \           |

T1--se se--T2 se--T3

This is fully maintained for load tracking, however the EEVDF parts of cfs_rq/se go unused for the intermediates and are instead connected like:

     _R_
    / | \

T1 T2 T3

Since the effective weight of the entities is determined by the hierarchy, this gets recomputed on enqueue,set_next_task and tick.

Notably, the effective weight (se->h_load) is computed from the hierarchical fraction: se->load / cfs_rq->load.

Since EEVDF is now exclusive operating on rq->cfs, it needs to consider cfs_rq->h_nr_queued rather than cfs_rq->nr_queued. Similarly, only tasks can get delayed, simplifying some of the cgroup cleanup.

One place where additional information was required was set_next_task() / put_prev_task(), where we need to track 'current' both in the hierarchical sense (cfs_rq->h_curr) and in the flat sense (cfs_rq->curr).

As a result of only having a single level to pick from, much of the complications in pick_next_task() and preemption go away.

Since many of the hierarchical operations are still there, this won't immediately fix the performance issues, but hopefully it will fix some of the latency issues.

TODO: split struct cfs_rq / struct sched_entity
TODO: try and get rid of h_curr

---
 include/linux/sched.h |    1 
 kernel/sched/debug.c  |    7 
 kernel/sched/fair.c   |  795 +++++++++++++++++++++-----------------------------
 kernel/sched/sched.h  |    2 
 4 files changed, 346 insertions(+), 459 deletions(-)
include/linux/sched.h +1 -0
@@ -575,6 +575,7 @@ struct sched_statistics {
 struct sched_entity {
 	/* For load-balancing: */
 	struct load_weight		load;
+	struct load_weight		h_load;
 	struct rb_node			run_node;
 	u64				deadline;
 	u64				min_vruntime;
kernel/sched/debug.c +5 -2
@@ -908,8 +908,9 @@ print_task(struct seq_file *m, struct rq
 	else
 		SEQ_printf(m, " %c", task_state_to_char(p));
 
-	SEQ_printf(m, " %15s %5d %9Ld.%06ld   %c   %9Ld.%06ld %c %9Ld.%06ld %9Ld.%06ld %9Ld   %5d ",
+	SEQ_printf(m, " %15s %5d %10ld %9Ld.%06ld   %c   %9Ld.%06ld %c %9Ld.%06ld %9Ld.%06ld %9Ld   %5d ",
 		p->comm, task_pid_nr(p),
+		p->se.h_load.weight,
 		SPLIT_NS(p->se.vruntime),
 		entity_eligible(cfs_rq_of(&p->se), &p->se) ? 'E' : 'N',
 		SPLIT_NS(p->se.deadline),
@@ -940,7 +941,7 @@ static void print_rq(struct seq_file *m,
 
 	SEQ_printf(m, "\n");
 	SEQ_printf(m, "runnable tasks:\n");
-	SEQ_printf(m, " S            task   PID       vruntime   eligible    "
+	SEQ_printf(m, " S            task   PID     weight       vruntime   eligible    "
 		   "deadline             slice          sum-exec      switches  "
 		   "prio         wait-time        sum-sleep       sum-block"
 #ifdef CONFIG_NUMA_BALANCING
@@ -1046,6 +1047,8 @@ void print_cfs_rq(struct seq_file *m, in
 			cfs_rq->tg_load_avg_contrib);
 	SEQ_printf(m, "  .%-30s: %ld\n", "tg_load_avg",
 			atomic_long_read(&cfs_rq->tg->load_avg));
+	SEQ_printf(m, "  .%-30s: %lu\n", "h_load",
+			cfs_rq->h_load);
 #endif /* CONFIG_FAIR_GROUP_SCHED */
 #ifdef CONFIG_CFS_BANDWIDTH
 	SEQ_printf(m, "  .%-30s: %d\n", "throttled",
kernel/sched/fair.c +338 -457

1,419 lines not on this page · raw message

kernel/sched/sched.h +2 -0
@@ -707,6 +707,7 @@ struct cfs_rq {
 	/*
 	 * CFS load tracking
 	 */
+	struct sched_entity	*h_curr;
 	struct sched_avg	avg;
 #ifndef CONFIG_64BIT
 	u64			last_update_time_copy;
@@ -2493,6 +2494,7 @@ extern const u32		sched_prio_to_wmult[40
 #define ENQUEUE_MIGRATED	0x00040000
 #define ENQUEUE_INITIAL		0x00080000
 #define ENQUEUE_RQ_SELECTED	0x00100000
+#define ENQUEUE_QUEUED		0x00200000
 
 #define RETRY_TASK		((void *)-1UL)
 
#9 lore raw

Re: [RFC][PATCH 8/8] sched/eevdf: Move to a single runqueue

Hello Peter,

Quoted 1 line · Peter Zijlstra

Change fair/cgroup to a single runqueue.

Looks like Christmas arrived early :-)

[..snip..]

Quoted 8 lines
kernel/sched/fair.c +6 -1
+	/*
+	 * XXX comment on the curr thing
+	 */
+	curr = (cfs_rq->curr == se);
+	if (curr)
+		place_entity(cfs_rq, se, flags);
 
-		se->slice = slice;

kernel/sched/fair.c enqueue_task_fair(struct rq *rq, struct · line 7209

Quoted 10 lines
kernel/sched/fair.c +3 -6
-		if (se != cfs_rq->curr)
-			min_vruntime_cb_propagate(&se->run_node, NULL);
-		slice = cfs_rq_min_slice(cfs_rq);
+	if (se->on_rq && se->sched_delayed)
+		requeue_delayed_entity(cfs_rq, se);
 
-		cfs_rq->h_nr_runnable += h_nr_runnable;
-		cfs_rq->h_nr_queued++;
-		cfs_rq->h_nr_idle += h_nr_idle;
+	weight = enqueue_hierarchy(p, flags);

Here is question I had when I first saw this on sched/flat and I've only looked at the series briefly:

enqueue_hierarchy() would end up updating the averages, and reweighing the hierarchical load of the entities in the new task's hierarchy ...

kernel/sched/fair.c enqueue_task_fair(struct rq *rq, struct · line 7213

Quoted 6 lines
kernel/sched/fair.c +3 -2
-		if (cfs_rq_is_idle(cfs_rq))
-			h_nr_idle = 1;
+	if (!curr) {
+		reweight_eevdf(cfs_rq, se, weight, false);
+		place_entity(cfs_rq, se, flags | ENQUEUE_QUEUED);

... and the hierarchical weight of the newly enqueued task would be based on this updated hierarchical proportion.

However, the tasks that are already queued have their deadlines calculated based on the old hierarchical proportions at the time they were enqueued / during the last task_tick_fair() for an entity that was put back.

Consider two tasks of equal weight on cgroups with equal weights:

    root    (weight: 1024)
   /    \
  CG0   CG1 (wight(CG0,CG1) = 512)
   |     |
   T0    T1 (h_weight(T0,T1) = 256)

and a third task of equal weight arrives (for the sake of simplicity also consider both cgroups have saturated their respective global shares on this CPU - similar to UP mode):

                            root        (weight: 1024)
                           /    \
         (weight: 512)   CG0    CG1     (weight: 512)
                         /     /   \

(h_weight(T0) = 256) T0 T1 T2 (h_weight(T2) = 128)

                           (h_weight(T1) = 256)

Logically, once T2 arrives, T1 should also be reweighed, it's hierarchical proportions be adjusted, and its vruntime and deadline be also adjusted accordingly based on the lag but that doesn't happen.

Instead, we continue with an approximation of h_load as seen sometime during the past. Is that alright with EEVDF or am I missing something?

Can it so happen that on SMP, future enqueues, and SMP conditions always lead to larger h_load for the newly enqueued tasks and as a result the older tasks become less favorable for the pick leading to starvation? (Am I being paranoid?)

kernel/sched/fair.c enqueue_task_fair(struct rq *rq, struct · line 7217

Quoted 4 lines
kernel/sched/fair.c +1 -0
+		__enqueue_entity(cfs_rq, se);
 	}
 
 	if (!rq_h_nr_queued && rq->cfs.h_nr_queued)

Anyhow, me goes and sees if any of this makes a difference to the benchmarks - I'll throw the biggest one at it first and see how that goes.

Signature · 2 lines
Thanks and Regards,
Prateek
author maintainer
#10 lore raw

Re: [RFC][PATCH 8/8] sched/eevdf: Move to a single runqueue

Quoted 51 lines · K Prateek Nayak

kernel/sched/fair.c enqueue_task_fair(struct rq *rq, struct · line 7209

Quoted 11 lines
kernel/sched/fair.c +9 -0
+	/*
+	 * XXX comment on the curr thing
+	 */
+	curr = (cfs_rq->curr == se);
+	if (curr)
+		place_entity(cfs_rq, se, flags);
 
+	if (se->on_rq && se->sched_delayed)
+		requeue_delayed_entity(cfs_rq, se);
 
+	weight = enqueue_hierarchy(p, flags);

Here is question I had when I first saw this on sched/flat and I've only looked at the series briefly:

enqueue_hierarchy() would end up updating the averages, and reweighing the hierarchical load of the entities in the new task's hierarchy ...

kernel/sched/fair.c enqueue_task_fair(struct rq *rq, struct · line 7213

Quoted 4 lines
kernel/sched/fair.c +3 -0
+	if (!curr) {
+		reweight_eevdf(cfs_rq, se, weight, false);
+		place_entity(cfs_rq, se, flags | ENQUEUE_QUEUED);

... and the hierarchical weight of the newly enqueued task would be based on this updated hierarchical proportion.

However, the tasks that are already queued have their deadlines calculated based on the old hierarchical proportions at the time they were enqueued / during the last task_tick_fair() for an entity that was put back.

Consider two tasks of equal weight on cgroups with equal weights:

    root    (weight: 1024)
   /    \
  CG0   CG1 (wight(CG0,CG1) = 512)
   |     |
   T0    T1 (h_weight(T0,T1) = 256)

and a third task of equal weight arrives (for the sake of simplicity also consider both cgroups have saturated their respective global shares on this CPU - similar to UP mode):

                            root        (weight: 1024)
                           /    \
         (weight: 512)   CG0    CG1     (weight: 512)
                         /     /   \

(h_weight(T0) = 256) T0 T1 T2 (h_weight(T2) = 128)

Quoted 7 lines
                           (h_weight(T1) = 256)

Logically, once T2 arrives, T1 should also be reweighed, it's hierarchical proportions be adjusted, and its vruntime and deadline be also adjusted accordingly based on the lag but that doesn't happen.

You are absolutely right.

Quoted 3 lines

Instead, we continue with an approximation of h_load as seen sometime during the past. Is that alright with EEVDF or am I missing something?

Strictly speaking it is dodgy as heck ;-) I was hoping that on average it would all work out. Esp. since PELT is a fairly slow and smooth function, the reweights will mostly be minor adjustments.

Quoted 4 lines

Can it so happen that on SMP, future enqueues, and SMP conditions always lead to larger h_load for the newly enqueued tasks and as a result the older tasks become less favorable for the pick leading to starvation? (Am I being paranoid?)

So typically the most recent enqueue will always have the smaller fraction of the group weight. This would lead to a slight favour to the older enqueue. So I think this would lead to a FIFO like bias.

But there is definitely some fun to be had here.

One definite fix is setting cgroup_mode to 'up' :-)

Quoted 8 lines

kernel/sched/fair.c enqueue_task_fair(struct rq *rq, struct · line 7217

Quoted 4 lines
kernel/sched/fair.c +1 -0
+		__enqueue_entity(cfs_rq, se);
 	}
 
 	if (!rq_h_nr_queued && rq->cfs.h_nr_queued)

Anyhow, me goes and sees if any of this makes a difference to the benchmarks - I'll throw the biggest one at it first and see how that goes.

Thanks, fingers crossed. :-)

#11 lore raw

Re: [RFC][PATCH 8/8] sched/eevdf: Move to a single runqueue

Hello Peter,

Quoted 63 lines · Peter Zijlstra
Quoted 51 lines · K Prateek Nayak

kernel/sched/fair.c enqueue_task_fair(struct rq *rq, struct · line 7209

Quoted 11 lines
kernel/sched/fair.c +9 -0
+	/*
+	 * XXX comment on the curr thing
+	 */
+	curr = (cfs_rq->curr == se);
+	if (curr)
+		place_entity(cfs_rq, se, flags);
 
+	if (se->on_rq && se->sched_delayed)
+		requeue_delayed_entity(cfs_rq, se);
 
+	weight = enqueue_hierarchy(p, flags);

Here is question I had when I first saw this on sched/flat and I've only looked at the series briefly:

enqueue_hierarchy() would end up updating the averages, and reweighing the hierarchical load of the entities in the new task's hierarchy ...

kernel/sched/fair.c enqueue_task_fair(struct rq *rq, struct · line 7213

Quoted 4 lines
kernel/sched/fair.c +3 -0
+	if (!curr) {
+		reweight_eevdf(cfs_rq, se, weight, false);
+		place_entity(cfs_rq, se, flags | ENQUEUE_QUEUED);

... and the hierarchical weight of the newly enqueued task would be based on this updated hierarchical proportion.

However, the tasks that are already queued have their deadlines calculated based on the old hierarchical proportions at the time they were enqueued / during the last task_tick_fair() for an entity that was put back.

Consider two tasks of equal weight on cgroups with equal weights:

    root    (weight: 1024)
   /    \
  CG0   CG1 (wight(CG0,CG1) = 512)
   |     |
   T0    T1 (h_weight(T0,T1) = 256)

and a third task of equal weight arrives (for the sake of simplicity also consider both cgroups have saturated their respective global shares on this CPU - similar to UP mode):

                            root        (weight: 1024)
                           /    \
         (weight: 512)   CG0    CG1     (weight: 512)
                         /     /   \

(h_weight(T0) = 256) T0 T1 T2 (h_weight(T2) = 128)

Quoted 7 lines
                           (h_weight(T1) = 256)

Logically, once T2 arrives, T1 should also be reweighed, it's hierarchical proportions be adjusted, and its vruntime and deadline be also adjusted accordingly based on the lag but that doesn't happen.

You are absolutely right.

Quoted 7 lines
Quoted 3 lines

Instead, we continue with an approximation of h_load as seen sometime during the past. Is that alright with EEVDF or am I missing something?

Strictly speaking it is dodgy as heck ;-) I was hoping that on average it would all work out. Esp. since PELT is a fairly slow and smooth function, the reweights will mostly be minor adjustments.

For a stable system, that is correct, but with a bunch of migration in the mix, even the averages tend to move quite rapidly which is why we already ratelimit the tg->shares calculation to once per millisecond :-)

Quoted 6 lines
Quoted 5 lines

Can it so happen that on SMP, future enqueues, and SMP conditions always lead to larger h_load for the newly enqueued tasks and as a result the older tasks become less favorable for the pick leading to starvation? (Am I being paranoid?)

Quoted 7 lines

So typically the most recent enqueue will always have the smaller fraction of the group weight. This would lead to a slight favour to the older enqueue. So I think this would lead to a FIFO like bias.

But there is definitely some fun to be had here.

One definite fix is setting cgroup_mode to 'up' :-)

A definite fix indeed but I'm pretty sure people will start complaining about more preemptions, etc. now that their cgroups have lost the nice -20 equivalent privilege on these large systems and people have to go and change these shares to painfully small values for performance.

Quoted 11 lines
Quoted 9 lines

kernel/sched/fair.c enqueue_task_fair(struct rq *rq, struct · line 7217

Quoted 5 lines
kernel/sched/fair.c +1 -0
+		__enqueue_entity(cfs_rq, se);
 	}
 
 	if (!rq_h_nr_queued && rq->cfs.h_nr_queued)

Anyhow, me goes and sees if any of this makes a difference to the benchmarks - I'll throw the biggest one at it first and see how that goes.

Thanks, fingers crossed. :-)

Last time I ran sched/flat, it held up surprisingly well actually. Performance was not terribly bad (and evne better in some cases) but that was before we flushed out the whole increased weight bits for EEVDF calculations so maybe it is all better now.

We'll know soon enough ;-)

Signature · 2 lines
Thanks and Regards,
Prateek