1 #include <linux/cgroup.h>
3 #include <linux/kernel.h>
4 #include <linux/percpu.h>
5 #include <linux/printk.h>
6 #include <linux/rcupdate.h>
7 #include <linux/slab.h>
9 #include <trace/events/sched.h>
14 #ifdef CONFIG_CGROUP_SCHEDTUNE
15 bool schedtune_initialized = false;
18 unsigned int sysctl_sched_cfs_boost __read_mostly;
20 extern struct reciprocal_value schedtune_spc_rdiv;
21 extern struct target_nrg schedtune_target_nrg;
23 /* Performance Boost region (B) threshold params */
24 static int perf_boost_idx;
26 /* Performance Constraint region (C) threshold params */
27 static int perf_constrain_idx;
30 * Performance-Energy (P-E) Space thresholds constants
32 struct threshold_params {
38 * System specific P-E space thresholds constants
40 static struct threshold_params
51 { 5, 0 } /* <= 100% */
55 __schedtune_accept_deltas(int nrg_delta, int cap_delta,
56 int perf_boost_idx, int perf_constrain_idx)
58 int payoff = -INT_MAX;
61 /* Performance Boost (B) region */
62 if (nrg_delta >= 0 && cap_delta > 0)
63 gain_idx = perf_boost_idx;
64 /* Performance Constraint (C) region */
65 else if (nrg_delta < 0 && cap_delta <= 0)
66 gain_idx = perf_constrain_idx;
68 /* Default: reject schedule candidate */
73 * Evaluate "Performance Boost" vs "Energy Increase"
75 * - Performance Boost (B) region
77 * Condition: nrg_delta > 0 && cap_delta > 0
79 * cap_gain / nrg_gain < cap_delta / nrg_delta =
80 * cap_gain * nrg_delta < cap_delta * nrg_gain
81 * Note that since both nrg_gain and nrg_delta are positive, the
82 * inequality does not change. Thus:
84 * payoff = (cap_delta * nrg_gain) - (cap_gain * nrg_delta)
86 * - Performance Constraint (C) region
88 * Condition: nrg_delta < 0 && cap_delta < 0
90 * cap_gain / nrg_gain > cap_delta / nrg_delta =
91 * cap_gain * nrg_delta < cap_delta * nrg_gain
92 * Note that since nrg_gain > 0 while nrg_delta < 0, the
93 * inequality change. Thus:
95 * payoff = (cap_delta * nrg_gain) - (cap_gain * nrg_delta)
97 * This means that, in case of same positive defined {cap,nrg}_gain
98 * for both the B and C regions, we can use the same payoff formula
99 * where a positive value represents the accept condition.
101 payoff = cap_delta * threshold_gains[gain_idx].nrg_gain;
102 payoff -= nrg_delta * threshold_gains[gain_idx].cap_gain;
107 #ifdef CONFIG_CGROUP_SCHEDTUNE
110 * EAS scheduler tunables for task groups.
113 /* SchdTune tunables for a group of tasks */
115 /* SchedTune CGroup subsystem */
116 struct cgroup_subsys_state css;
118 /* Boost group allocated ID */
121 /* Boost value for tasks on that SchedTune CGroup */
124 /* Performance Boost (B) region threshold params */
127 /* Performance Constraint (C) region threshold params */
128 int perf_constrain_idx;
130 /* Hint to bias scheduling of tasks on that SchedTune CGroup
131 * towards idle CPUs */
135 static inline struct schedtune *css_st(struct cgroup_subsys_state *css)
137 return css ? container_of(css, struct schedtune, css) : NULL;
140 static inline struct schedtune *task_schedtune(struct task_struct *tsk)
142 return css_st(task_css(tsk, schedtune_cgrp_id));
145 static inline struct schedtune *parent_st(struct schedtune *st)
147 return css_st(st->css.parent);
151 * SchedTune root control group
152 * The root control group is used to defined a system-wide boosting tuning,
153 * which is applied to all tasks in the system.
154 * Task specific boost tuning could be specified by creating and
155 * configuring a child control group under the root one.
156 * By default, system-wide boosting is disabled, i.e. no boosting is applied
157 * to tasks which are not into a child control group.
159 static struct schedtune
163 .perf_constrain_idx = 0,
168 schedtune_accept_deltas(int nrg_delta, int cap_delta,
169 struct task_struct *task)
171 struct schedtune *ct;
173 int perf_constrain_idx;
175 /* Optimal (O) region */
176 if (nrg_delta < 0 && cap_delta > 0) {
177 trace_sched_tune_filter(nrg_delta, cap_delta, 0, 0, 1, 0);
181 /* Suboptimal (S) region */
182 if (nrg_delta > 0 && cap_delta < 0) {
183 trace_sched_tune_filter(nrg_delta, cap_delta, 0, 0, -1, 5);
187 /* Get task specific perf Boost/Constraints indexes */
189 ct = task_schedtune(task);
190 perf_boost_idx = ct->perf_boost_idx;
191 perf_constrain_idx = ct->perf_constrain_idx;
194 return __schedtune_accept_deltas(nrg_delta, cap_delta,
195 perf_boost_idx, perf_constrain_idx);
199 * Maximum number of boost groups to support
200 * When per-task boosting is used we still allow only limited number of
201 * boost groups for two main reasons:
202 * 1. on a real system we usually have only few classes of workloads which
203 * make sense to boost with different values (e.g. background vs foreground
204 * tasks, interactive vs low-priority tasks)
205 * 2. a limited number allows for a simpler and more memory/time efficient
206 * implementation especially for the computation of the per-CPU boost
209 #define BOOSTGROUPS_COUNT 5
211 /* Array of configured boostgroups */
212 static struct schedtune *allocated_group[BOOSTGROUPS_COUNT] = {
217 /* SchedTune boost groups
218 * Keep track of all the boost groups which impact on CPU, for example when a
219 * CPU has two RUNNABLE tasks belonging to two different boost groups and thus
220 * likely with different boost values.
221 * Since on each system we expect only a limited number of boost groups, here
222 * we use a simple array to keep track of the metrics required to compute the
223 * maximum per-CPU boosting value.
225 struct boost_groups {
226 /* Maximum boost value for all RUNNABLE tasks on a CPU */
230 /* The boost for tasks on that boost group */
232 /* Count of RUNNABLE tasks on that boost group */
234 } group[BOOSTGROUPS_COUNT];
235 /* CPU's boost group locking */
239 /* Boost groups affecting each CPU in the system */
240 DEFINE_PER_CPU(struct boost_groups, cpu_boost_groups);
243 schedtune_cpu_update(int cpu)
245 struct boost_groups *bg;
249 bg = &per_cpu(cpu_boost_groups, cpu);
251 /* The root boost group is always active */
252 boost_max = bg->group[0].boost;
253 for (idx = 1; idx < BOOSTGROUPS_COUNT; ++idx) {
255 * A boost group affects a CPU only if it has
256 * RUNNABLE tasks on that CPU
258 if (bg->group[idx].tasks == 0)
261 boost_max = max(boost_max, bg->group[idx].boost);
263 /* Ensures boost_max is non-negative when all cgroup boost values
264 * are neagtive. Avoids under-accounting of cpu capacity which may cause
265 * task stacking and frequency spikes.*/
266 boost_max = max(boost_max, 0);
267 bg->boost_max = boost_max;
271 schedtune_boostgroup_update(int idx, int boost)
273 struct boost_groups *bg;
278 /* Update per CPU boost groups */
279 for_each_possible_cpu(cpu) {
280 bg = &per_cpu(cpu_boost_groups, cpu);
283 * Keep track of current boost values to compute the per CPU
284 * maximum only when it has been affected by the new value of
285 * the updated boost group
287 cur_boost_max = bg->boost_max;
288 old_boost = bg->group[idx].boost;
290 /* Update the boost value of this boost group */
291 bg->group[idx].boost = boost;
293 /* Check if this update increase current max */
294 if (boost > cur_boost_max && bg->group[idx].tasks) {
295 bg->boost_max = boost;
296 trace_sched_tune_boostgroup_update(cpu, 1, bg->boost_max);
300 /* Check if this update has decreased current max */
301 if (cur_boost_max == old_boost && old_boost > boost) {
302 schedtune_cpu_update(cpu);
303 trace_sched_tune_boostgroup_update(cpu, -1, bg->boost_max);
307 trace_sched_tune_boostgroup_update(cpu, 0, bg->boost_max);
313 #define ENQUEUE_TASK 1
314 #define DEQUEUE_TASK -1
317 schedtune_tasks_update(struct task_struct *p, int cpu, int idx, int task_count)
319 struct boost_groups *bg = &per_cpu(cpu_boost_groups, cpu);
320 int tasks = bg->group[idx].tasks + task_count;
322 /* Update boosted tasks count while avoiding to make it negative */
323 bg->group[idx].tasks = max(0, tasks);
325 trace_sched_tune_tasks_update(p, cpu, tasks, idx,
326 bg->group[idx].boost, bg->boost_max);
328 /* Boost group activation or deactivation on that RQ */
329 if (tasks == 1 || tasks == 0)
330 schedtune_cpu_update(cpu);
334 * NOTE: This function must be called while holding the lock on the CPU RQ
336 void schedtune_enqueue_task(struct task_struct *p, int cpu)
338 struct boost_groups *bg = &per_cpu(cpu_boost_groups, cpu);
339 unsigned long irq_flags;
340 struct schedtune *st;
343 if (!unlikely(schedtune_initialized))
347 * When a task is marked PF_EXITING by do_exit() it's going to be
348 * dequeued and enqueued multiple times in the exit path.
349 * Thus we avoid any further update, since we do not want to change
350 * CPU boosting while the task is exiting.
352 if (p->flags & PF_EXITING)
356 * Boost group accouting is protected by a per-cpu lock and requires
357 * interrupt to be disabled to avoid race conditions for example on
358 * do_exit()::cgroup_exit() and task migration.
360 raw_spin_lock_irqsave(&bg->lock, irq_flags);
363 st = task_schedtune(p);
366 schedtune_tasks_update(p, cpu, idx, ENQUEUE_TASK);
369 raw_spin_unlock_irqrestore(&bg->lock, irq_flags);
372 int schedtune_can_attach(struct cgroup_taskset *tset)
374 struct task_struct *task;
375 struct cgroup_subsys_state *css;
376 struct boost_groups *bg;
377 unsigned long irq_flags;
380 int src_bg; /* Source boost group index */
381 int dst_bg; /* Destination boost group index */
384 if (!unlikely(schedtune_initialized))
388 cgroup_taskset_for_each(task, css, tset) {
391 * Lock the CPU's RQ the task is enqueued to avoid race
392 * conditions with migration code while the task is being
395 rq = lock_rq_of(task, &irq_flags);
398 unlock_rq_of(rq, task, &irq_flags);
403 * Boost group accouting is protected by a per-cpu lock and requires
404 * interrupt to be disabled to avoid race conditions on...
407 bg = &per_cpu(cpu_boost_groups, cpu);
408 raw_spin_lock(&bg->lock);
410 dst_bg = css_st(css)->idx;
411 src_bg = task_schedtune(task)->idx;
414 * Current task is not changing boostgroup, which can
415 * happen when the new hierarchy is in use.
417 if (unlikely(dst_bg == src_bg)) {
418 raw_spin_unlock(&bg->lock);
419 unlock_rq_of(rq, task, &irq_flags);
424 * This is the case of a RUNNABLE task which is switching its
425 * current boost group.
428 /* Move task from src to dst boost group */
429 tasks = bg->group[src_bg].tasks - 1;
430 bg->group[src_bg].tasks = max(0, tasks);
431 bg->group[dst_bg].tasks += 1;
433 raw_spin_unlock(&bg->lock);
434 unlock_rq_of(rq, task, &irq_flags);
436 /* Update CPU boost group */
437 if (bg->group[src_bg].tasks == 0 || bg->group[dst_bg].tasks == 1)
438 schedtune_cpu_update(task_cpu(task));
445 void schedtune_cancel_attach(struct cgroup_taskset *tset)
447 /* This can happen only if SchedTune controller is mounted with
448 * other hierarchies ane one of them fails. Since usually SchedTune is
449 * mouted on its own hierarcy, for the time being we do not implement
450 * a proper rollback mechanism */
451 WARN(1, "SchedTune cancel attach not implemented");
455 * NOTE: This function must be called while holding the lock on the CPU RQ
457 void schedtune_dequeue_task(struct task_struct *p, int cpu)
459 struct boost_groups *bg = &per_cpu(cpu_boost_groups, cpu);
460 unsigned long irq_flags;
461 struct schedtune *st;
464 if (!unlikely(schedtune_initialized))
468 * When a task is marked PF_EXITING by do_exit() it's going to be
469 * dequeued and enqueued multiple times in the exit path.
470 * Thus we avoid any further update, since we do not want to change
471 * CPU boosting while the task is exiting.
472 * The last dequeue is already enforce by the do_exit() code path
473 * via schedtune_exit_task().
475 if (p->flags & PF_EXITING)
479 * Boost group accouting is protected by a per-cpu lock and requires
480 * interrupt to be disabled to avoid race conditions on...
482 raw_spin_lock_irqsave(&bg->lock, irq_flags);
485 st = task_schedtune(p);
488 schedtune_tasks_update(p, cpu, idx, DEQUEUE_TASK);
491 raw_spin_unlock_irqrestore(&bg->lock, irq_flags);
494 void schedtune_exit_task(struct task_struct *tsk)
496 struct schedtune *st;
497 unsigned long irq_flags;
502 if (!unlikely(schedtune_initialized))
505 rq = lock_rq_of(tsk, &irq_flags);
509 st = task_schedtune(tsk);
511 schedtune_tasks_update(tsk, cpu, idx, DEQUEUE_TASK);
514 unlock_rq_of(rq, tsk, &irq_flags);
517 int schedtune_cpu_boost(int cpu)
519 struct boost_groups *bg;
521 bg = &per_cpu(cpu_boost_groups, cpu);
522 return bg->boost_max;
525 int schedtune_task_boost(struct task_struct *p)
527 struct schedtune *st;
530 if (!unlikely(schedtune_initialized))
533 /* Get task boost value */
535 st = task_schedtune(p);
536 task_boost = st->boost;
542 int schedtune_prefer_idle(struct task_struct *p)
544 struct schedtune *st;
547 if (!unlikely(schedtune_initialized))
550 /* Get prefer_idle value */
552 st = task_schedtune(p);
553 prefer_idle = st->prefer_idle;
560 prefer_idle_read(struct cgroup_subsys_state *css, struct cftype *cft)
562 struct schedtune *st = css_st(css);
564 return st->prefer_idle;
568 prefer_idle_write(struct cgroup_subsys_state *css, struct cftype *cft,
571 struct schedtune *st = css_st(css);
572 st->prefer_idle = prefer_idle;
578 boost_read(struct cgroup_subsys_state *css, struct cftype *cft)
580 struct schedtune *st = css_st(css);
586 boost_write(struct cgroup_subsys_state *css, struct cftype *cft,
589 struct schedtune *st = css_st(css);
590 unsigned threshold_idx;
593 if (boost < -100 || boost > 100)
598 * Update threshold params for Performance Boost (B)
599 * and Performance Constraint (C) regions.
600 * The current implementatio uses the same cuts for both
603 threshold_idx = clamp(boost_pct, 0, 99) / 10;
604 st->perf_boost_idx = threshold_idx;
605 st->perf_constrain_idx = threshold_idx;
608 if (css == &root_schedtune.css) {
609 sysctl_sched_cfs_boost = boost;
610 perf_boost_idx = threshold_idx;
611 perf_constrain_idx = threshold_idx;
614 /* Update CPU boost */
615 schedtune_boostgroup_update(st->idx, st->boost);
617 trace_sched_tune_config(st->boost);
622 static struct cftype files[] = {
625 .read_s64 = boost_read,
626 .write_s64 = boost_write,
629 .name = "prefer_idle",
630 .read_u64 = prefer_idle_read,
631 .write_u64 = prefer_idle_write,
637 schedtune_boostgroup_init(struct schedtune *st)
639 struct boost_groups *bg;
642 /* Keep track of allocated boost groups */
643 allocated_group[st->idx] = st;
645 /* Initialize the per CPU boost groups */
646 for_each_possible_cpu(cpu) {
647 bg = &per_cpu(cpu_boost_groups, cpu);
648 bg->group[st->idx].boost = 0;
649 bg->group[st->idx].tasks = 0;
655 static struct cgroup_subsys_state *
656 schedtune_css_alloc(struct cgroup_subsys_state *parent_css)
658 struct schedtune *st;
662 return &root_schedtune.css;
664 /* Allow only single level hierachies */
665 if (parent_css != &root_schedtune.css) {
666 pr_err("Nested SchedTune boosting groups not allowed\n");
667 return ERR_PTR(-ENOMEM);
670 /* Allow only a limited number of boosting groups */
671 for (idx = 1; idx < BOOSTGROUPS_COUNT; ++idx)
672 if (!allocated_group[idx])
674 if (idx == BOOSTGROUPS_COUNT) {
675 pr_err("Trying to create more than %d SchedTune boosting groups\n",
677 return ERR_PTR(-ENOSPC);
680 st = kzalloc(sizeof(*st), GFP_KERNEL);
684 /* Initialize per CPUs boost group support */
686 if (schedtune_boostgroup_init(st))
694 return ERR_PTR(-ENOMEM);
698 schedtune_boostgroup_release(struct schedtune *st)
700 /* Reset this boost group */
701 schedtune_boostgroup_update(st->idx, 0);
703 /* Keep track of allocated boost groups */
704 allocated_group[st->idx] = NULL;
708 schedtune_css_free(struct cgroup_subsys_state *css)
710 struct schedtune *st = css_st(css);
712 schedtune_boostgroup_release(st);
716 struct cgroup_subsys schedtune_cgrp_subsys = {
717 .css_alloc = schedtune_css_alloc,
718 .css_free = schedtune_css_free,
719 .can_attach = schedtune_can_attach,
720 .cancel_attach = schedtune_cancel_attach,
721 .legacy_cftypes = files,
726 schedtune_init_cgroups(void)
728 struct boost_groups *bg;
731 /* Initialize the per CPU boost groups */
732 for_each_possible_cpu(cpu) {
733 bg = &per_cpu(cpu_boost_groups, cpu);
734 memset(bg, 0, sizeof(struct boost_groups));
735 raw_spin_lock_init(&bg->lock);
738 pr_info("schedtune: configured to support %d boost groups\n",
741 schedtune_initialized = true;
744 #else /* CONFIG_CGROUP_SCHEDTUNE */
747 schedtune_accept_deltas(int nrg_delta, int cap_delta,
748 struct task_struct *task)
750 /* Optimal (O) region */
751 if (nrg_delta < 0 && cap_delta > 0) {
752 trace_sched_tune_filter(nrg_delta, cap_delta, 0, 0, 1, 0);
756 /* Suboptimal (S) region */
757 if (nrg_delta > 0 && cap_delta < 0) {
758 trace_sched_tune_filter(nrg_delta, cap_delta, 0, 0, -1, 5);
762 return __schedtune_accept_deltas(nrg_delta, cap_delta,
763 perf_boost_idx, perf_constrain_idx);
766 #endif /* CONFIG_CGROUP_SCHEDTUNE */
769 sysctl_sched_cfs_boost_handler(struct ctl_table *table, int write,
770 void __user *buffer, size_t *lenp,
773 int ret = proc_dointvec_minmax(table, write, buffer, lenp, ppos);
774 unsigned threshold_idx;
780 if (sysctl_sched_cfs_boost < -100 || sysctl_sched_cfs_boost > 100)
782 boost_pct = sysctl_sched_cfs_boost;
785 * Update threshold params for Performance Boost (B)
786 * and Performance Constraint (C) regions.
787 * The current implementatio uses the same cuts for both
790 threshold_idx = clamp(boost_pct, 0, 99) / 10;
791 perf_boost_idx = threshold_idx;
792 perf_constrain_idx = threshold_idx;
797 #ifdef CONFIG_SCHED_DEBUG
799 schedtune_test_nrg(unsigned long delta_pwr)
801 unsigned long test_delta_pwr;
802 unsigned long test_norm_pwr;
806 * Check normalization constants using some constant system
809 pr_info("schedtune: verify normalization constants...\n");
810 for (idx = 0; idx < 6; ++idx) {
811 test_delta_pwr = delta_pwr >> idx;
813 /* Normalize on max energy for target platform */
814 test_norm_pwr = reciprocal_divide(
815 test_delta_pwr << SCHED_LOAD_SHIFT,
816 schedtune_target_nrg.rdiv);
818 pr_info("schedtune: max_pwr/2^%d: %4lu => norm_pwr: %5lu\n",
819 idx, test_delta_pwr, test_norm_pwr);
823 #define schedtune_test_nrg(delta_pwr)
827 * Compute the min/max power consumption of a cluster and all its CPUs
830 schedtune_add_cluster_nrg(
831 struct sched_domain *sd,
832 struct sched_group *sg,
833 struct target_nrg *ste)
835 struct sched_domain *sd2;
836 struct sched_group *sg2;
838 struct cpumask *cluster_cpus;
841 unsigned long min_pwr;
842 unsigned long max_pwr;
845 /* Get Cluster energy using EM data for the first CPU */
846 cluster_cpus = sched_group_cpus(sg);
847 snprintf(str, 32, "CLUSTER[%*pbl]",
848 cpumask_pr_args(cluster_cpus));
850 min_pwr = sg->sge->idle_states[sg->sge->nr_idle_states - 1].power;
851 max_pwr = sg->sge->cap_states[sg->sge->nr_cap_states - 1].power;
852 pr_info("schedtune: %-17s min_pwr: %5lu max_pwr: %5lu\n",
853 str, min_pwr, max_pwr);
856 * Keep track of this cluster's energy in the computation of the
857 * overall system energy
859 ste->min_power += min_pwr;
860 ste->max_power += max_pwr;
862 /* Get CPU energy using EM data for each CPU in the group */
863 for_each_cpu(cpu, cluster_cpus) {
864 /* Get a SD view for the specific CPU */
865 for_each_domain(cpu, sd2) {
866 /* Get the CPU group */
868 min_pwr = sg2->sge->idle_states[sg2->sge->nr_idle_states - 1].power;
869 max_pwr = sg2->sge->cap_states[sg2->sge->nr_cap_states - 1].power;
871 ste->min_power += min_pwr;
872 ste->max_power += max_pwr;
874 snprintf(str, 32, "CPU[%d]", cpu);
875 pr_info("schedtune: %-17s min_pwr: %5lu max_pwr: %5lu\n",
876 str, min_pwr, max_pwr);
879 * Assume we have EM data only at the CPU and
880 * the upper CLUSTER level
882 BUG_ON(!cpumask_equal(
883 sched_group_cpus(sg),
884 sched_group_cpus(sd2->parent->groups)
892 * Initialize the constants required to compute normalized energy.
893 * The values of these constants depends on the EM data for the specific
894 * target system and topology.
895 * Thus, this function is expected to be called by the code
896 * that bind the EM to the topology information.
901 struct target_nrg *ste = &schedtune_target_nrg;
902 unsigned long delta_pwr = 0;
903 struct sched_domain *sd;
904 struct sched_group *sg;
906 pr_info("schedtune: init normalization constants...\n");
913 * When EAS is in use, we always have a pointer to the highest SD
914 * which provides EM data.
916 sd = rcu_dereference(per_cpu(sd_ea, cpumask_first(cpu_online_mask)));
918 pr_info("schedtune: no energy model data\n");
924 schedtune_add_cluster_nrg(sd, sg, ste);
925 } while (sg = sg->next, sg != sd->groups);
929 pr_info("schedtune: %-17s min_pwr: %5lu max_pwr: %5lu\n",
930 "SYSTEM", ste->min_power, ste->max_power);
932 /* Compute normalization constants */
933 delta_pwr = ste->max_power - ste->min_power;
934 ste->rdiv = reciprocal_value(delta_pwr);
935 pr_info("schedtune: using normalization constants mul: %u sh1: %u sh2: %u\n",
936 ste->rdiv.m, ste->rdiv.sh1, ste->rdiv.sh2);
938 schedtune_test_nrg(delta_pwr);
940 #ifdef CONFIG_CGROUP_SCHEDTUNE
941 schedtune_init_cgroups();
943 pr_info("schedtune: configured to support global boosting only\n");
946 schedtune_spc_rdiv = reciprocal_value(100);
951 pr_warning("schedtune: disabled!\n");
955 postcore_initcall(schedtune_init);