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 static bool schedtune_initialized = false;
18 unsigned int sysctl_sched_cfs_boost __read_mostly;
20 extern struct target_nrg schedtune_target_nrg;
22 /* Performance Boost region (B) threshold params */
23 static int perf_boost_idx;
25 /* Performance Constraint region (C) threshold params */
26 static int perf_constrain_idx;
29 * Performance-Energy (P-E) Space thresholds constants
31 struct threshold_params {
37 * System specific P-E space thresholds constants
39 static struct threshold_params
50 { 5, 0 } /* <= 100% */
54 __schedtune_accept_deltas(int nrg_delta, int cap_delta,
55 int perf_boost_idx, int perf_constrain_idx)
57 int payoff = -INT_MAX;
60 /* Performance Boost (B) region */
61 if (nrg_delta >= 0 && cap_delta > 0)
62 gain_idx = perf_boost_idx;
63 /* Performance Constraint (C) region */
64 else if (nrg_delta < 0 && cap_delta <= 0)
65 gain_idx = perf_constrain_idx;
67 /* Default: reject schedule candidate */
72 * Evaluate "Performance Boost" vs "Energy Increase"
74 * - Performance Boost (B) region
76 * Condition: nrg_delta > 0 && cap_delta > 0
78 * cap_gain / nrg_gain < cap_delta / nrg_delta =
79 * cap_gain * nrg_delta < cap_delta * nrg_gain
80 * Note that since both nrg_gain and nrg_delta are positive, the
81 * inequality does not change. Thus:
83 * payoff = (cap_delta * nrg_gain) - (cap_gain * nrg_delta)
85 * - Performance Constraint (C) region
87 * Condition: nrg_delta < 0 && cap_delta < 0
89 * cap_gain / nrg_gain > cap_delta / nrg_delta =
90 * cap_gain * nrg_delta < cap_delta * nrg_gain
91 * Note that since nrg_gain > 0 while nrg_delta < 0, the
92 * inequality change. Thus:
94 * payoff = (cap_delta * nrg_gain) - (cap_gain * nrg_delta)
96 * This means that, in case of same positive defined {cap,nrg}_gain
97 * for both the B and C regions, we can use the same payoff formula
98 * where a positive value represents the accept condition.
100 payoff = cap_delta * threshold_gains[gain_idx].nrg_gain;
101 payoff -= nrg_delta * threshold_gains[gain_idx].cap_gain;
106 #ifdef CONFIG_CGROUP_SCHEDTUNE
109 * EAS scheduler tunables for task groups.
112 /* SchdTune tunables for a group of tasks */
114 /* SchedTune CGroup subsystem */
115 struct cgroup_subsys_state css;
117 /* Boost group allocated ID */
120 /* Boost value for tasks on that SchedTune CGroup */
123 /* Performance Boost (B) region threshold params */
126 /* Performance Constraint (C) region threshold params */
127 int perf_constrain_idx;
130 static inline struct schedtune *css_st(struct cgroup_subsys_state *css)
132 return css ? container_of(css, struct schedtune, css) : NULL;
135 static inline struct schedtune *task_schedtune(struct task_struct *tsk)
137 return css_st(task_css(tsk, schedtune_cgrp_id));
140 static inline struct schedtune *parent_st(struct schedtune *st)
142 return css_st(st->css.parent);
146 * SchedTune root control group
147 * The root control group is used to defined a system-wide boosting tuning,
148 * which is applied to all tasks in the system.
149 * Task specific boost tuning could be specified by creating and
150 * configuring a child control group under the root one.
151 * By default, system-wide boosting is disabled, i.e. no boosting is applied
152 * to tasks which are not into a child control group.
154 static struct schedtune
158 .perf_constrain_idx = 0,
162 schedtune_accept_deltas(int nrg_delta, int cap_delta,
163 struct task_struct *task)
165 struct schedtune *ct;
167 int perf_constrain_idx;
169 /* Optimal (O) region */
170 if (nrg_delta < 0 && cap_delta > 0) {
171 trace_sched_tune_filter(nrg_delta, cap_delta, 0, 0, 1, 0);
175 /* Suboptimal (S) region */
176 if (nrg_delta > 0 && cap_delta < 0) {
177 trace_sched_tune_filter(nrg_delta, cap_delta, 0, 0, -1, 5);
181 /* Get task specific perf Boost/Constraints indexes */
183 ct = task_schedtune(task);
184 perf_boost_idx = ct->perf_boost_idx;
185 perf_constrain_idx = ct->perf_constrain_idx;
188 return __schedtune_accept_deltas(nrg_delta, cap_delta,
189 perf_boost_idx, perf_constrain_idx);
193 * Maximum number of boost groups to support
194 * When per-task boosting is used we still allow only limited number of
195 * boost groups for two main reasons:
196 * 1. on a real system we usually have only few classes of workloads which
197 * make sense to boost with different values (e.g. background vs foreground
198 * tasks, interactive vs low-priority tasks)
199 * 2. a limited number allows for a simpler and more memory/time efficient
200 * implementation especially for the computation of the per-CPU boost
203 #define BOOSTGROUPS_COUNT 4
205 /* Array of configured boostgroups */
206 static struct schedtune *allocated_group[BOOSTGROUPS_COUNT] = {
211 /* SchedTune boost groups
212 * Keep track of all the boost groups which impact on CPU, for example when a
213 * CPU has two RUNNABLE tasks belonging to two different boost groups and thus
214 * likely with different boost values.
215 * Since on each system we expect only a limited number of boost groups, here
216 * we use a simple array to keep track of the metrics required to compute the
217 * maximum per-CPU boosting value.
219 struct boost_groups {
220 /* Maximum boost value for all RUNNABLE tasks on a CPU */
224 /* The boost for tasks on that boost group */
226 /* Count of RUNNABLE tasks on that boost group */
228 } group[BOOSTGROUPS_COUNT];
229 /* CPU's boost group locking */
233 /* Boost groups affecting each CPU in the system */
234 DEFINE_PER_CPU(struct boost_groups, cpu_boost_groups);
237 schedtune_cpu_update(int cpu)
239 struct boost_groups *bg;
243 bg = &per_cpu(cpu_boost_groups, cpu);
245 /* The root boost group is always active */
246 boost_max = bg->group[0].boost;
247 for (idx = 1; idx < BOOSTGROUPS_COUNT; ++idx) {
249 * A boost group affects a CPU only if it has
250 * RUNNABLE tasks on that CPU
252 if (bg->group[idx].tasks == 0)
255 boost_max = max(boost_max, bg->group[idx].boost);
257 /* Ensures boost_max is non-negative when all cgroup boost values
258 * are neagtive. Avoids under-accounting of cpu capacity which may cause
259 * task stacking and frequency spikes.*/
260 boost_max = max(boost_max, 0);
261 bg->boost_max = boost_max;
265 schedtune_boostgroup_update(int idx, int boost)
267 struct boost_groups *bg;
272 /* Update per CPU boost groups */
273 for_each_possible_cpu(cpu) {
274 bg = &per_cpu(cpu_boost_groups, cpu);
277 * Keep track of current boost values to compute the per CPU
278 * maximum only when it has been affected by the new value of
279 * the updated boost group
281 cur_boost_max = bg->boost_max;
282 old_boost = bg->group[idx].boost;
284 /* Update the boost value of this boost group */
285 bg->group[idx].boost = boost;
287 /* Check if this update increase current max */
288 if (boost > cur_boost_max && bg->group[idx].tasks) {
289 bg->boost_max = boost;
290 trace_sched_tune_boostgroup_update(cpu, 1, bg->boost_max);
294 /* Check if this update has decreased current max */
295 if (cur_boost_max == old_boost && old_boost > boost) {
296 schedtune_cpu_update(cpu);
297 trace_sched_tune_boostgroup_update(cpu, -1, bg->boost_max);
301 trace_sched_tune_boostgroup_update(cpu, 0, bg->boost_max);
307 #define ENQUEUE_TASK 1
308 #define DEQUEUE_TASK -1
311 schedtune_tasks_update(struct task_struct *p, int cpu, int idx, int task_count)
313 struct boost_groups *bg = &per_cpu(cpu_boost_groups, cpu);
314 int tasks = bg->group[idx].tasks + task_count;
316 /* Update boosted tasks count while avoiding to make it negative */
317 bg->group[idx].tasks = max(0, tasks);
319 trace_sched_tune_tasks_update(p, cpu, tasks, idx,
320 bg->group[idx].boost, bg->boost_max);
322 /* Boost group activation or deactivation on that RQ */
323 if (tasks == 1 || tasks == 0)
324 schedtune_cpu_update(cpu);
328 * NOTE: This function must be called while holding the lock on the CPU RQ
330 void schedtune_enqueue_task(struct task_struct *p, int cpu)
332 struct boost_groups *bg = &per_cpu(cpu_boost_groups, cpu);
333 unsigned long irq_flags;
334 struct schedtune *st;
337 if (!unlikely(schedtune_initialized))
341 * When a task is marked PF_EXITING by do_exit() it's going to be
342 * dequeued and enqueued multiple times in the exit path.
343 * Thus we avoid any further update, since we do not want to change
344 * CPU boosting while the task is exiting.
346 if (p->flags & PF_EXITING)
350 * Boost group accouting is protected by a per-cpu lock and requires
351 * interrupt to be disabled to avoid race conditions for example on
352 * do_exit()::cgroup_exit() and task migration.
354 raw_spin_lock_irqsave(&bg->lock, irq_flags);
357 st = task_schedtune(p);
360 schedtune_tasks_update(p, cpu, idx, ENQUEUE_TASK);
363 raw_spin_unlock_irqrestore(&bg->lock, irq_flags);
366 int schedtune_allow_attach(struct cgroup_taskset *tset)
368 /* We always allows tasks to be moved between existing CGroups */
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 /* Get task boost value */
532 st = task_schedtune(p);
533 task_boost = st->boost;
540 boost_read(struct cgroup_subsys_state *css, struct cftype *cft)
542 struct schedtune *st = css_st(css);
548 boost_write(struct cgroup_subsys_state *css, struct cftype *cft,
551 struct schedtune *st = css_st(css);
552 unsigned threshold_idx;
555 if (boost < -100 || boost > 100)
560 * Update threshold params for Performance Boost (B)
561 * and Performance Constraint (C) regions.
562 * The current implementatio uses the same cuts for both
565 threshold_idx = clamp(boost_pct, 0, 99) / 10;
566 st->perf_boost_idx = threshold_idx;
567 st->perf_constrain_idx = threshold_idx;
570 if (css == &root_schedtune.css) {
571 sysctl_sched_cfs_boost = boost;
572 perf_boost_idx = threshold_idx;
573 perf_constrain_idx = threshold_idx;
576 /* Update CPU boost */
577 schedtune_boostgroup_update(st->idx, st->boost);
579 trace_sched_tune_config(st->boost);
584 static struct cftype files[] = {
587 .read_s64 = boost_read,
588 .write_s64 = boost_write,
594 schedtune_boostgroup_init(struct schedtune *st)
596 struct boost_groups *bg;
599 /* Keep track of allocated boost groups */
600 allocated_group[st->idx] = st;
602 /* Initialize the per CPU boost groups */
603 for_each_possible_cpu(cpu) {
604 bg = &per_cpu(cpu_boost_groups, cpu);
605 bg->group[st->idx].boost = 0;
606 bg->group[st->idx].tasks = 0;
612 static struct cgroup_subsys_state *
613 schedtune_css_alloc(struct cgroup_subsys_state *parent_css)
615 struct schedtune *st;
619 return &root_schedtune.css;
621 /* Allow only single level hierachies */
622 if (parent_css != &root_schedtune.css) {
623 pr_err("Nested SchedTune boosting groups not allowed\n");
624 return ERR_PTR(-ENOMEM);
627 /* Allow only a limited number of boosting groups */
628 for (idx = 1; idx < BOOSTGROUPS_COUNT; ++idx)
629 if (!allocated_group[idx])
631 if (idx == BOOSTGROUPS_COUNT) {
632 pr_err("Trying to create more than %d SchedTune boosting groups\n",
634 return ERR_PTR(-ENOSPC);
637 st = kzalloc(sizeof(*st), GFP_KERNEL);
641 /* Initialize per CPUs boost group support */
643 if (schedtune_boostgroup_init(st))
651 return ERR_PTR(-ENOMEM);
655 schedtune_boostgroup_release(struct schedtune *st)
657 /* Reset this boost group */
658 schedtune_boostgroup_update(st->idx, 0);
660 /* Keep track of allocated boost groups */
661 allocated_group[st->idx] = NULL;
665 schedtune_css_free(struct cgroup_subsys_state *css)
667 struct schedtune *st = css_st(css);
669 schedtune_boostgroup_release(st);
673 struct cgroup_subsys schedtune_cgrp_subsys = {
674 .css_alloc = schedtune_css_alloc,
675 .css_free = schedtune_css_free,
676 // .allow_attach = schedtune_allow_attach,
677 .can_attach = schedtune_can_attach,
678 .cancel_attach = schedtune_cancel_attach,
679 .legacy_cftypes = files,
684 schedtune_init_cgroups(void)
686 struct boost_groups *bg;
689 /* Initialize the per CPU boost groups */
690 for_each_possible_cpu(cpu) {
691 bg = &per_cpu(cpu_boost_groups, cpu);
692 memset(bg, 0, sizeof(struct boost_groups));
695 pr_info("schedtune: configured to support %d boost groups\n",
699 #else /* CONFIG_CGROUP_SCHEDTUNE */
702 schedtune_accept_deltas(int nrg_delta, int cap_delta,
703 struct task_struct *task)
705 /* Optimal (O) region */
706 if (nrg_delta < 0 && cap_delta > 0) {
707 trace_sched_tune_filter(nrg_delta, cap_delta, 0, 0, 1, 0);
711 /* Suboptimal (S) region */
712 if (nrg_delta > 0 && cap_delta < 0) {
713 trace_sched_tune_filter(nrg_delta, cap_delta, 0, 0, -1, 5);
717 return __schedtune_accept_deltas(nrg_delta, cap_delta,
718 perf_boost_idx, perf_constrain_idx);
721 #endif /* CONFIG_CGROUP_SCHEDTUNE */
724 sysctl_sched_cfs_boost_handler(struct ctl_table *table, int write,
725 void __user *buffer, size_t *lenp,
728 int ret = proc_dointvec_minmax(table, write, buffer, lenp, ppos);
729 unsigned threshold_idx;
735 if (sysctl_sched_cfs_boost < -100 || sysctl_sched_cfs_boost > 100)
737 boost_pct = sysctl_sched_cfs_boost;
740 * Update threshold params for Performance Boost (B)
741 * and Performance Constraint (C) regions.
742 * The current implementatio uses the same cuts for both
745 threshold_idx = clamp(boost_pct, 0, 99) / 10;
746 perf_boost_idx = threshold_idx;
747 perf_constrain_idx = threshold_idx;
752 #ifdef CONFIG_SCHED_DEBUG
754 schedtune_test_nrg(unsigned long delta_pwr)
756 unsigned long test_delta_pwr;
757 unsigned long test_norm_pwr;
761 * Check normalization constants using some constant system
764 pr_info("schedtune: verify normalization constants...\n");
765 for (idx = 0; idx < 6; ++idx) {
766 test_delta_pwr = delta_pwr >> idx;
768 /* Normalize on max energy for target platform */
769 test_norm_pwr = reciprocal_divide(
770 test_delta_pwr << SCHED_LOAD_SHIFT,
771 schedtune_target_nrg.rdiv);
773 pr_info("schedtune: max_pwr/2^%d: %4lu => norm_pwr: %5lu\n",
774 idx, test_delta_pwr, test_norm_pwr);
778 #define schedtune_test_nrg(delta_pwr)
782 * Compute the min/max power consumption of a cluster and all its CPUs
785 schedtune_add_cluster_nrg(
786 struct sched_domain *sd,
787 struct sched_group *sg,
788 struct target_nrg *ste)
790 struct sched_domain *sd2;
791 struct sched_group *sg2;
793 struct cpumask *cluster_cpus;
796 unsigned long min_pwr;
797 unsigned long max_pwr;
800 /* Get Cluster energy using EM data for the first CPU */
801 cluster_cpus = sched_group_cpus(sg);
802 snprintf(str, 32, "CLUSTER[%*pbl]",
803 cpumask_pr_args(cluster_cpus));
805 min_pwr = sg->sge->idle_states[sg->sge->nr_idle_states - 1].power;
806 max_pwr = sg->sge->cap_states[sg->sge->nr_cap_states - 1].power;
807 pr_info("schedtune: %-17s min_pwr: %5lu max_pwr: %5lu\n",
808 str, min_pwr, max_pwr);
811 * Keep track of this cluster's energy in the computation of the
812 * overall system energy
814 ste->min_power += min_pwr;
815 ste->max_power += max_pwr;
817 /* Get CPU energy using EM data for each CPU in the group */
818 for_each_cpu(cpu, cluster_cpus) {
819 /* Get a SD view for the specific CPU */
820 for_each_domain(cpu, sd2) {
821 /* Get the CPU group */
823 min_pwr = sg2->sge->idle_states[sg2->sge->nr_idle_states - 1].power;
824 max_pwr = sg2->sge->cap_states[sg2->sge->nr_cap_states - 1].power;
826 ste->min_power += min_pwr;
827 ste->max_power += max_pwr;
829 snprintf(str, 32, "CPU[%d]", cpu);
830 pr_info("schedtune: %-17s min_pwr: %5lu max_pwr: %5lu\n",
831 str, min_pwr, max_pwr);
834 * Assume we have EM data only at the CPU and
835 * the upper CLUSTER level
837 BUG_ON(!cpumask_equal(
838 sched_group_cpus(sg),
839 sched_group_cpus(sd2->parent->groups)
847 * Initialize the constants required to compute normalized energy.
848 * The values of these constants depends on the EM data for the specific
849 * target system and topology.
850 * Thus, this function is expected to be called by the code
851 * that bind the EM to the topology information.
856 struct target_nrg *ste = &schedtune_target_nrg;
857 unsigned long delta_pwr = 0;
858 struct sched_domain *sd;
859 struct sched_group *sg;
861 pr_info("schedtune: init normalization constants...\n");
868 * When EAS is in use, we always have a pointer to the highest SD
869 * which provides EM data.
871 sd = rcu_dereference(per_cpu(sd_ea, cpumask_first(cpu_online_mask)));
873 pr_info("schedtune: no energy model data\n");
879 schedtune_add_cluster_nrg(sd, sg, ste);
880 } while (sg = sg->next, sg != sd->groups);
884 pr_info("schedtune: %-17s min_pwr: %5lu max_pwr: %5lu\n",
885 "SYSTEM", ste->min_power, ste->max_power);
887 /* Compute normalization constants */
888 delta_pwr = ste->max_power - ste->min_power;
889 ste->rdiv = reciprocal_value(delta_pwr);
890 pr_info("schedtune: using normalization constants mul: %u sh1: %u sh2: %u\n",
891 ste->rdiv.m, ste->rdiv.sh1, ste->rdiv.sh2);
893 schedtune_test_nrg(delta_pwr);
895 #ifdef CONFIG_CGROUP_SCHEDTUNE
896 schedtune_init_cgroups();
898 pr_info("schedtune: configured to support global boosting only\n");
907 late_initcall(schedtune_init);