Patrick Bellasi | ffbceda | 2015-06-23 09:17:54 +0100 | [diff] [blame] | 1 | #include <linux/cgroup.h> |
| 2 | #include <linux/err.h> |
| 3 | #include <linux/percpu.h> |
| 4 | #include <linux/printk.h> |
| 5 | #include <linux/slab.h> |
Channagoud Kadabi | 8810e5f | 2017-02-17 16:01:05 -0800 | [diff] [blame] | 6 | #include <linux/kernel.h> |
Patrick Bellasi | edd28d3 | 2015-07-07 15:33:20 +0100 | [diff] [blame] | 7 | #include <linux/rcupdate.h> |
Patrick Bellasi | ae71030 | 2015-06-23 09:17:54 +0100 | [diff] [blame] | 8 | #include <linux/slab.h> |
Patrick Bellasi | 050dcb8 | 2015-06-22 13:49:07 +0100 | [diff] [blame] | 9 | #include <trace/events/sched.h> |
Patrick Bellasi | ffbceda | 2015-06-23 09:17:54 +0100 | [diff] [blame] | 10 | |
Patrick Bellasi | 62c1c06 | 2015-06-22 18:11:44 +0100 | [diff] [blame] | 11 | #include "sched.h" |
Patrick Bellasi | c5b2042 | 2016-07-29 15:45:57 +0100 | [diff] [blame] | 12 | #include "tune.h" |
Patrick Bellasi | 62c1c06 | 2015-06-22 18:11:44 +0100 | [diff] [blame] | 13 | |
| 14 | unsigned int sysctl_sched_cfs_boost __read_mostly; |
| 15 | |
Patrick Bellasi | ffbceda | 2015-06-23 09:17:54 +0100 | [diff] [blame] | 16 | #ifdef CONFIG_CGROUP_SCHEDTUNE |
Patrick Bellasi | d248900 | 2016-07-28 18:44:40 +0100 | [diff] [blame] | 17 | static bool schedtune_initialized = false; |
Channagoud Kadabi | 8810e5f | 2017-02-17 16:01:05 -0800 | [diff] [blame] | 18 | #endif /* CONFIG_CGROUP_SCHEDTUNE */ |
Patrick Bellasi | ffbceda | 2015-06-23 09:17:54 +0100 | [diff] [blame] | 19 | |
Patrick Bellasi | 69fa4c7 | 2015-06-22 18:11:44 +0100 | [diff] [blame] | 20 | unsigned int sysctl_sched_cfs_boost __read_mostly; |
| 21 | |
Patrick Bellasi | c5b2042 | 2016-07-29 15:45:57 +0100 | [diff] [blame] | 22 | extern struct target_nrg schedtune_target_nrg; |
Patrick Bellasi | 2f369bb | 2016-01-12 18:12:13 +0000 | [diff] [blame] | 23 | |
| 24 | /* Performance Boost region (B) threshold params */ |
| 25 | static int perf_boost_idx; |
| 26 | |
| 27 | /* Performance Constraint region (C) threshold params */ |
| 28 | static int perf_constrain_idx; |
| 29 | |
| 30 | /** |
| 31 | * Performance-Energy (P-E) Space thresholds constants |
| 32 | */ |
| 33 | struct threshold_params { |
| 34 | int nrg_gain; |
| 35 | int cap_gain; |
| 36 | }; |
| 37 | |
| 38 | /* |
| 39 | * System specific P-E space thresholds constants |
| 40 | */ |
| 41 | static struct threshold_params |
| 42 | threshold_gains[] = { |
Patrick Bellasi | d5563d3 | 2016-07-29 15:32:26 +0100 | [diff] [blame] | 43 | { 0, 5 }, /* < 10% */ |
| 44 | { 1, 5 }, /* < 20% */ |
| 45 | { 2, 5 }, /* < 30% */ |
| 46 | { 3, 5 }, /* < 40% */ |
| 47 | { 4, 5 }, /* < 50% */ |
| 48 | { 5, 4 }, /* < 60% */ |
| 49 | { 5, 3 }, /* < 70% */ |
| 50 | { 5, 2 }, /* < 80% */ |
| 51 | { 5, 1 }, /* < 90% */ |
| 52 | { 5, 0 } /* <= 100% */ |
Patrick Bellasi | 2f369bb | 2016-01-12 18:12:13 +0000 | [diff] [blame] | 53 | }; |
| 54 | |
| 55 | static int |
| 56 | __schedtune_accept_deltas(int nrg_delta, int cap_delta, |
| 57 | int perf_boost_idx, int perf_constrain_idx) |
| 58 | { |
| 59 | int payoff = -INT_MAX; |
Patrick Bellasi | 2ed513e | 2016-07-28 17:38:25 +0100 | [diff] [blame] | 60 | int gain_idx = -1; |
Patrick Bellasi | 2f369bb | 2016-01-12 18:12:13 +0000 | [diff] [blame] | 61 | |
| 62 | /* Performance Boost (B) region */ |
Patrick Bellasi | 2ed513e | 2016-07-28 17:38:25 +0100 | [diff] [blame] | 63 | if (nrg_delta >= 0 && cap_delta > 0) |
| 64 | gain_idx = perf_boost_idx; |
Patrick Bellasi | 2f369bb | 2016-01-12 18:12:13 +0000 | [diff] [blame] | 65 | /* Performance Constraint (C) region */ |
Patrick Bellasi | 2ed513e | 2016-07-28 17:38:25 +0100 | [diff] [blame] | 66 | else if (nrg_delta < 0 && cap_delta <= 0) |
| 67 | gain_idx = perf_constrain_idx; |
Patrick Bellasi | 2f369bb | 2016-01-12 18:12:13 +0000 | [diff] [blame] | 68 | |
| 69 | /* Default: reject schedule candidate */ |
Patrick Bellasi | 2ed513e | 2016-07-28 17:38:25 +0100 | [diff] [blame] | 70 | if (gain_idx == -1) |
| 71 | return payoff; |
| 72 | |
| 73 | /* |
| 74 | * Evaluate "Performance Boost" vs "Energy Increase" |
| 75 | * |
| 76 | * - Performance Boost (B) region |
| 77 | * |
| 78 | * Condition: nrg_delta > 0 && cap_delta > 0 |
| 79 | * Payoff criteria: |
| 80 | * cap_gain / nrg_gain < cap_delta / nrg_delta = |
| 81 | * cap_gain * nrg_delta < cap_delta * nrg_gain |
| 82 | * Note that since both nrg_gain and nrg_delta are positive, the |
| 83 | * inequality does not change. Thus: |
| 84 | * |
| 85 | * payoff = (cap_delta * nrg_gain) - (cap_gain * nrg_delta) |
| 86 | * |
| 87 | * - Performance Constraint (C) region |
| 88 | * |
| 89 | * Condition: nrg_delta < 0 && cap_delta < 0 |
| 90 | * payoff criteria: |
| 91 | * cap_gain / nrg_gain > cap_delta / nrg_delta = |
| 92 | * cap_gain * nrg_delta < cap_delta * nrg_gain |
| 93 | * Note that since nrg_gain > 0 while nrg_delta < 0, the |
| 94 | * inequality change. Thus: |
| 95 | * |
| 96 | * payoff = (cap_delta * nrg_gain) - (cap_gain * nrg_delta) |
| 97 | * |
| 98 | * This means that, in case of same positive defined {cap,nrg}_gain |
| 99 | * for both the B and C regions, we can use the same payoff formula |
| 100 | * where a positive value represents the accept condition. |
| 101 | */ |
| 102 | payoff = cap_delta * threshold_gains[gain_idx].nrg_gain; |
| 103 | payoff -= nrg_delta * threshold_gains[gain_idx].cap_gain; |
| 104 | |
Patrick Bellasi | 2f369bb | 2016-01-12 18:12:13 +0000 | [diff] [blame] | 105 | return payoff; |
| 106 | } |
| 107 | |
Patrick Bellasi | ae71030 | 2015-06-23 09:17:54 +0100 | [diff] [blame] | 108 | #ifdef CONFIG_CGROUP_SCHEDTUNE |
Vikram Mulukutla | d056dbc | 2017-02-07 18:58:07 -0800 | [diff] [blame] | 109 | |
Patrick Bellasi | ffbceda | 2015-06-23 09:17:54 +0100 | [diff] [blame] | 110 | /* |
| 111 | * EAS scheduler tunables for task groups. |
| 112 | */ |
| 113 | |
| 114 | /* SchdTune tunables for a group of tasks */ |
| 115 | struct schedtune { |
| 116 | /* SchedTune CGroup subsystem */ |
| 117 | struct cgroup_subsys_state css; |
| 118 | |
| 119 | /* Boost group allocated ID */ |
| 120 | int idx; |
| 121 | |
| 122 | /* Boost value for tasks on that SchedTune CGroup */ |
| 123 | int boost; |
| 124 | |
Joonwoo Park | f7d6cd4 | 2017-01-17 15:19:43 -0800 | [diff] [blame] | 125 | #ifdef CONFIG_SCHED_WALT |
Syed Rameez Mustafa | 084075b | 2016-08-31 16:54:12 -0700 | [diff] [blame] | 126 | /* Toggle ability to override sched boost enabled */ |
| 127 | bool sched_boost_no_override; |
| 128 | |
| 129 | /* |
| 130 | * Controls whether a cgroup is eligible for sched boost or not. This |
| 131 | * can temporariliy be disabled by the kernel based on the no_override |
| 132 | * flag above. |
| 133 | */ |
| 134 | bool sched_boost_enabled; |
| 135 | |
| 136 | /* |
| 137 | * This tracks the default value of sched_boost_enabled and is used |
| 138 | * restore the value following any temporary changes to that flag. |
| 139 | */ |
| 140 | bool sched_boost_enabled_backup; |
| 141 | |
| 142 | /* |
| 143 | * Controls whether tasks of this cgroup should be colocated with each |
| 144 | * other and tasks of other cgroups that have the same flag turned on. |
| 145 | */ |
| 146 | bool colocate; |
| 147 | |
| 148 | /* Controls whether further updates are allowed to the colocate flag */ |
| 149 | bool colocate_update_disabled; |
Joonwoo Park | f7d6cd4 | 2017-01-17 15:19:43 -0800 | [diff] [blame] | 150 | #endif /* CONFIG_SCHED_WALT */ |
Syed Rameez Mustafa | 084075b | 2016-08-31 16:54:12 -0700 | [diff] [blame] | 151 | |
Patrick Bellasi | 2f369bb | 2016-01-12 18:12:13 +0000 | [diff] [blame] | 152 | /* Performance Boost (B) region threshold params */ |
| 153 | int perf_boost_idx; |
| 154 | |
| 155 | /* Performance Constraint (C) region threshold params */ |
| 156 | int perf_constrain_idx; |
Srinath Sridharan | 42503db | 2016-07-14 13:09:03 -0700 | [diff] [blame] | 157 | |
| 158 | /* Hint to bias scheduling of tasks on that SchedTune CGroup |
| 159 | * towards idle CPUs */ |
| 160 | int prefer_idle; |
Patrick Bellasi | ffbceda | 2015-06-23 09:17:54 +0100 | [diff] [blame] | 161 | }; |
| 162 | |
| 163 | static inline struct schedtune *css_st(struct cgroup_subsys_state *css) |
| 164 | { |
Syed Rameez Mustafa | 642cef5 | 2016-10-11 18:24:43 -0700 | [diff] [blame] | 165 | return container_of(css, struct schedtune, css); |
Patrick Bellasi | ffbceda | 2015-06-23 09:17:54 +0100 | [diff] [blame] | 166 | } |
| 167 | |
| 168 | static inline struct schedtune *task_schedtune(struct task_struct *tsk) |
| 169 | { |
| 170 | return css_st(task_css(tsk, schedtune_cgrp_id)); |
| 171 | } |
| 172 | |
| 173 | static inline struct schedtune *parent_st(struct schedtune *st) |
| 174 | { |
| 175 | return css_st(st->css.parent); |
| 176 | } |
| 177 | |
| 178 | /* |
| 179 | * SchedTune root control group |
| 180 | * The root control group is used to defined a system-wide boosting tuning, |
| 181 | * which is applied to all tasks in the system. |
| 182 | * Task specific boost tuning could be specified by creating and |
| 183 | * configuring a child control group under the root one. |
| 184 | * By default, system-wide boosting is disabled, i.e. no boosting is applied |
| 185 | * to tasks which are not into a child control group. |
| 186 | */ |
| 187 | static struct schedtune |
| 188 | root_schedtune = { |
| 189 | .boost = 0, |
Joonwoo Park | f7d6cd4 | 2017-01-17 15:19:43 -0800 | [diff] [blame] | 190 | #ifdef CONFIG_SCHED_WALT |
Syed Rameez Mustafa | 084075b | 2016-08-31 16:54:12 -0700 | [diff] [blame] | 191 | .sched_boost_no_override = false, |
| 192 | .sched_boost_enabled = true, |
| 193 | .sched_boost_enabled_backup = true, |
| 194 | .colocate = false, |
| 195 | .colocate_update_disabled = false, |
| 196 | #endif |
Patrick Bellasi | 2f369bb | 2016-01-12 18:12:13 +0000 | [diff] [blame] | 197 | .perf_boost_idx = 0, |
| 198 | .perf_constrain_idx = 0, |
Srinath Sridharan | 42503db | 2016-07-14 13:09:03 -0700 | [diff] [blame] | 199 | .prefer_idle = 0, |
Patrick Bellasi | ffbceda | 2015-06-23 09:17:54 +0100 | [diff] [blame] | 200 | }; |
| 201 | |
Patrick Bellasi | 2f369bb | 2016-01-12 18:12:13 +0000 | [diff] [blame] | 202 | int |
| 203 | schedtune_accept_deltas(int nrg_delta, int cap_delta, |
| 204 | struct task_struct *task) |
| 205 | { |
| 206 | struct schedtune *ct; |
| 207 | int perf_boost_idx; |
| 208 | int perf_constrain_idx; |
| 209 | |
| 210 | /* Optimal (O) region */ |
Patrick Bellasi | 5824d98 | 2016-01-20 14:06:05 +0000 | [diff] [blame] | 211 | if (nrg_delta < 0 && cap_delta > 0) { |
| 212 | trace_sched_tune_filter(nrg_delta, cap_delta, 0, 0, 1, 0); |
Patrick Bellasi | 2f369bb | 2016-01-12 18:12:13 +0000 | [diff] [blame] | 213 | return INT_MAX; |
Patrick Bellasi | 5824d98 | 2016-01-20 14:06:05 +0000 | [diff] [blame] | 214 | } |
Patrick Bellasi | 2f369bb | 2016-01-12 18:12:13 +0000 | [diff] [blame] | 215 | |
| 216 | /* Suboptimal (S) region */ |
Patrick Bellasi | 5824d98 | 2016-01-20 14:06:05 +0000 | [diff] [blame] | 217 | if (nrg_delta > 0 && cap_delta < 0) { |
| 218 | trace_sched_tune_filter(nrg_delta, cap_delta, 0, 0, -1, 5); |
Patrick Bellasi | 2f369bb | 2016-01-12 18:12:13 +0000 | [diff] [blame] | 219 | return -INT_MAX; |
Patrick Bellasi | 5824d98 | 2016-01-20 14:06:05 +0000 | [diff] [blame] | 220 | } |
Patrick Bellasi | 2f369bb | 2016-01-12 18:12:13 +0000 | [diff] [blame] | 221 | |
| 222 | /* Get task specific perf Boost/Constraints indexes */ |
| 223 | rcu_read_lock(); |
| 224 | ct = task_schedtune(task); |
| 225 | perf_boost_idx = ct->perf_boost_idx; |
| 226 | perf_constrain_idx = ct->perf_constrain_idx; |
| 227 | rcu_read_unlock(); |
| 228 | |
| 229 | return __schedtune_accept_deltas(nrg_delta, cap_delta, |
| 230 | perf_boost_idx, perf_constrain_idx); |
| 231 | } |
| 232 | |
Patrick Bellasi | ffbceda | 2015-06-23 09:17:54 +0100 | [diff] [blame] | 233 | /* |
| 234 | * Maximum number of boost groups to support |
| 235 | * When per-task boosting is used we still allow only limited number of |
| 236 | * boost groups for two main reasons: |
| 237 | * 1. on a real system we usually have only few classes of workloads which |
| 238 | * make sense to boost with different values (e.g. background vs foreground |
| 239 | * tasks, interactive vs low-priority tasks) |
| 240 | * 2. a limited number allows for a simpler and more memory/time efficient |
| 241 | * implementation especially for the computation of the per-CPU boost |
| 242 | * value |
| 243 | */ |
Syed Rameez Mustafa | ca545c8 | 2016-09-02 17:51:39 -0700 | [diff] [blame] | 244 | #define BOOSTGROUPS_COUNT 5 |
Patrick Bellasi | ffbceda | 2015-06-23 09:17:54 +0100 | [diff] [blame] | 245 | |
| 246 | /* Array of configured boostgroups */ |
| 247 | static struct schedtune *allocated_group[BOOSTGROUPS_COUNT] = { |
| 248 | &root_schedtune, |
| 249 | NULL, |
| 250 | }; |
| 251 | |
| 252 | /* SchedTune boost groups |
| 253 | * Keep track of all the boost groups which impact on CPU, for example when a |
| 254 | * CPU has two RUNNABLE tasks belonging to two different boost groups and thus |
| 255 | * likely with different boost values. |
| 256 | * Since on each system we expect only a limited number of boost groups, here |
| 257 | * we use a simple array to keep track of the metrics required to compute the |
| 258 | * maximum per-CPU boosting value. |
| 259 | */ |
| 260 | struct boost_groups { |
Srinath Sridharan | e71c425 | 2016-07-28 17:28:55 +0100 | [diff] [blame] | 261 | bool idle; |
Patrick Bellasi | ffbceda | 2015-06-23 09:17:54 +0100 | [diff] [blame] | 262 | /* Maximum boost value for all RUNNABLE tasks on a CPU */ |
Srinath Sridharan | e71c425 | 2016-07-28 17:28:55 +0100 | [diff] [blame] | 263 | int boost_max; |
Patrick Bellasi | ffbceda | 2015-06-23 09:17:54 +0100 | [diff] [blame] | 264 | struct { |
| 265 | /* The boost for tasks on that boost group */ |
Srinath Sridharan | e71c425 | 2016-07-28 17:28:55 +0100 | [diff] [blame] | 266 | int boost; |
Patrick Bellasi | ffbceda | 2015-06-23 09:17:54 +0100 | [diff] [blame] | 267 | /* Count of RUNNABLE tasks on that boost group */ |
| 268 | unsigned tasks; |
| 269 | } group[BOOSTGROUPS_COUNT]; |
Patrick Bellasi | d248900 | 2016-07-28 18:44:40 +0100 | [diff] [blame] | 270 | /* CPU's boost group locking */ |
| 271 | raw_spinlock_t lock; |
Patrick Bellasi | ffbceda | 2015-06-23 09:17:54 +0100 | [diff] [blame] | 272 | }; |
| 273 | |
| 274 | /* Boost groups affecting each CPU in the system */ |
| 275 | DEFINE_PER_CPU(struct boost_groups, cpu_boost_groups); |
| 276 | |
Joonwoo Park | f7d6cd4 | 2017-01-17 15:19:43 -0800 | [diff] [blame] | 277 | #ifdef CONFIG_SCHED_WALT |
Vikram Mulukutla | d056dbc | 2017-02-07 18:58:07 -0800 | [diff] [blame] | 278 | static inline void init_sched_boost(struct schedtune *st) |
| 279 | { |
| 280 | st->sched_boost_no_override = false; |
| 281 | st->sched_boost_enabled = true; |
| 282 | st->sched_boost_enabled_backup = st->sched_boost_enabled; |
| 283 | st->colocate = false; |
| 284 | st->colocate_update_disabled = false; |
| 285 | } |
| 286 | |
| 287 | bool same_schedtune(struct task_struct *tsk1, struct task_struct *tsk2) |
| 288 | { |
| 289 | return task_schedtune(tsk1) == task_schedtune(tsk2); |
| 290 | } |
| 291 | |
| 292 | void update_cgroup_boost_settings(void) |
| 293 | { |
| 294 | int i; |
| 295 | |
| 296 | for (i = 0; i < BOOSTGROUPS_COUNT; i++) { |
| 297 | if (!allocated_group[i]) |
| 298 | break; |
| 299 | |
| 300 | if (allocated_group[i]->sched_boost_no_override) |
| 301 | continue; |
| 302 | |
| 303 | allocated_group[i]->sched_boost_enabled = false; |
| 304 | } |
| 305 | } |
| 306 | |
| 307 | void restore_cgroup_boost_settings(void) |
| 308 | { |
| 309 | int i; |
| 310 | |
| 311 | for (i = 0; i < BOOSTGROUPS_COUNT; i++) { |
| 312 | if (!allocated_group[i]) |
| 313 | break; |
| 314 | |
| 315 | allocated_group[i]->sched_boost_enabled = |
| 316 | allocated_group[i]->sched_boost_enabled_backup; |
| 317 | } |
| 318 | } |
| 319 | |
| 320 | bool task_sched_boost(struct task_struct *p) |
| 321 | { |
| 322 | struct schedtune *st = task_schedtune(p); |
| 323 | |
| 324 | return st->sched_boost_enabled; |
| 325 | } |
| 326 | |
| 327 | static u64 |
| 328 | sched_boost_override_read(struct cgroup_subsys_state *css, |
| 329 | struct cftype *cft) |
| 330 | { |
| 331 | struct schedtune *st = css_st(css); |
| 332 | |
| 333 | return st->sched_boost_no_override; |
| 334 | } |
| 335 | |
| 336 | static int sched_boost_override_write(struct cgroup_subsys_state *css, |
| 337 | struct cftype *cft, u64 override) |
| 338 | { |
| 339 | struct schedtune *st = css_st(css); |
| 340 | |
| 341 | st->sched_boost_no_override = !!override; |
| 342 | |
| 343 | return 0; |
| 344 | } |
| 345 | |
Joonwoo Park | f7d6cd4 | 2017-01-17 15:19:43 -0800 | [diff] [blame] | 346 | #endif /* CONFIG_SCHED_WALT */ |
Channagoud Kadabi | 8810e5f | 2017-02-17 16:01:05 -0800 | [diff] [blame] | 347 | |
Patrick Bellasi | 9a871ed | 2016-01-14 12:31:35 +0000 | [diff] [blame] | 348 | static void |
| 349 | schedtune_cpu_update(int cpu) |
| 350 | { |
| 351 | struct boost_groups *bg; |
Srinath Sridharan | e71c425 | 2016-07-28 17:28:55 +0100 | [diff] [blame] | 352 | int boost_max; |
Patrick Bellasi | 9a871ed | 2016-01-14 12:31:35 +0000 | [diff] [blame] | 353 | int idx; |
| 354 | |
| 355 | bg = &per_cpu(cpu_boost_groups, cpu); |
| 356 | |
| 357 | /* The root boost group is always active */ |
| 358 | boost_max = bg->group[0].boost; |
| 359 | for (idx = 1; idx < BOOSTGROUPS_COUNT; ++idx) { |
| 360 | /* |
| 361 | * A boost group affects a CPU only if it has |
| 362 | * RUNNABLE tasks on that CPU |
| 363 | */ |
| 364 | if (bg->group[idx].tasks == 0) |
| 365 | continue; |
Srinath Sridharan | e71c425 | 2016-07-28 17:28:55 +0100 | [diff] [blame] | 366 | |
Patrick Bellasi | 9a871ed | 2016-01-14 12:31:35 +0000 | [diff] [blame] | 367 | boost_max = max(boost_max, bg->group[idx].boost); |
| 368 | } |
Srinath Sridharan | e71c425 | 2016-07-28 17:28:55 +0100 | [diff] [blame] | 369 | /* Ensures boost_max is non-negative when all cgroup boost values |
| 370 | * are neagtive. Avoids under-accounting of cpu capacity which may cause |
| 371 | * task stacking and frequency spikes.*/ |
| 372 | boost_max = max(boost_max, 0); |
Patrick Bellasi | 9a871ed | 2016-01-14 12:31:35 +0000 | [diff] [blame] | 373 | bg->boost_max = boost_max; |
| 374 | } |
| 375 | |
| 376 | static int |
| 377 | schedtune_boostgroup_update(int idx, int boost) |
| 378 | { |
| 379 | struct boost_groups *bg; |
| 380 | int cur_boost_max; |
| 381 | int old_boost; |
| 382 | int cpu; |
| 383 | |
| 384 | /* Update per CPU boost groups */ |
| 385 | for_each_possible_cpu(cpu) { |
| 386 | bg = &per_cpu(cpu_boost_groups, cpu); |
| 387 | |
| 388 | /* |
| 389 | * Keep track of current boost values to compute the per CPU |
| 390 | * maximum only when it has been affected by the new value of |
| 391 | * the updated boost group |
| 392 | */ |
| 393 | cur_boost_max = bg->boost_max; |
| 394 | old_boost = bg->group[idx].boost; |
| 395 | |
| 396 | /* Update the boost value of this boost group */ |
| 397 | bg->group[idx].boost = boost; |
| 398 | |
| 399 | /* Check if this update increase current max */ |
| 400 | if (boost > cur_boost_max && bg->group[idx].tasks) { |
| 401 | bg->boost_max = boost; |
Patrick Bellasi | 953b104 | 2015-06-24 15:36:08 +0100 | [diff] [blame] | 402 | trace_sched_tune_boostgroup_update(cpu, 1, bg->boost_max); |
Patrick Bellasi | 9a871ed | 2016-01-14 12:31:35 +0000 | [diff] [blame] | 403 | continue; |
| 404 | } |
| 405 | |
| 406 | /* Check if this update has decreased current max */ |
Patrick Bellasi | 953b104 | 2015-06-24 15:36:08 +0100 | [diff] [blame] | 407 | if (cur_boost_max == old_boost && old_boost > boost) { |
Patrick Bellasi | 9a871ed | 2016-01-14 12:31:35 +0000 | [diff] [blame] | 408 | schedtune_cpu_update(cpu); |
Patrick Bellasi | 953b104 | 2015-06-24 15:36:08 +0100 | [diff] [blame] | 409 | trace_sched_tune_boostgroup_update(cpu, -1, bg->boost_max); |
| 410 | continue; |
| 411 | } |
| 412 | |
| 413 | trace_sched_tune_boostgroup_update(cpu, 0, bg->boost_max); |
Patrick Bellasi | 9a871ed | 2016-01-14 12:31:35 +0000 | [diff] [blame] | 414 | } |
| 415 | |
| 416 | return 0; |
| 417 | } |
| 418 | |
Patrick Bellasi | d248900 | 2016-07-28 18:44:40 +0100 | [diff] [blame] | 419 | #define ENQUEUE_TASK 1 |
| 420 | #define DEQUEUE_TASK -1 |
| 421 | |
Patrick Bellasi | edd28d3 | 2015-07-07 15:33:20 +0100 | [diff] [blame] | 422 | static inline void |
| 423 | schedtune_tasks_update(struct task_struct *p, int cpu, int idx, int task_count) |
| 424 | { |
Patrick Bellasi | d248900 | 2016-07-28 18:44:40 +0100 | [diff] [blame] | 425 | struct boost_groups *bg = &per_cpu(cpu_boost_groups, cpu); |
| 426 | int tasks = bg->group[idx].tasks + task_count; |
Patrick Bellasi | edd28d3 | 2015-07-07 15:33:20 +0100 | [diff] [blame] | 427 | |
| 428 | /* Update boosted tasks count while avoiding to make it negative */ |
Patrick Bellasi | d248900 | 2016-07-28 18:44:40 +0100 | [diff] [blame] | 429 | bg->group[idx].tasks = max(0, tasks); |
Patrick Bellasi | 953b104 | 2015-06-24 15:36:08 +0100 | [diff] [blame] | 430 | |
| 431 | trace_sched_tune_tasks_update(p, cpu, tasks, idx, |
| 432 | bg->group[idx].boost, bg->boost_max); |
| 433 | |
Patrick Bellasi | d248900 | 2016-07-28 18:44:40 +0100 | [diff] [blame] | 434 | /* Boost group activation or deactivation on that RQ */ |
| 435 | if (tasks == 1 || tasks == 0) |
| 436 | schedtune_cpu_update(cpu); |
Patrick Bellasi | edd28d3 | 2015-07-07 15:33:20 +0100 | [diff] [blame] | 437 | } |
| 438 | |
| 439 | /* |
| 440 | * NOTE: This function must be called while holding the lock on the CPU RQ |
| 441 | */ |
| 442 | void schedtune_enqueue_task(struct task_struct *p, int cpu) |
| 443 | { |
Patrick Bellasi | d248900 | 2016-07-28 18:44:40 +0100 | [diff] [blame] | 444 | struct boost_groups *bg = &per_cpu(cpu_boost_groups, cpu); |
| 445 | unsigned long irq_flags; |
Patrick Bellasi | edd28d3 | 2015-07-07 15:33:20 +0100 | [diff] [blame] | 446 | struct schedtune *st; |
| 447 | int idx; |
| 448 | |
Patrick Bellasi | d248900 | 2016-07-28 18:44:40 +0100 | [diff] [blame] | 449 | if (!unlikely(schedtune_initialized)) |
| 450 | return; |
| 451 | |
Patrick Bellasi | edd28d3 | 2015-07-07 15:33:20 +0100 | [diff] [blame] | 452 | /* |
| 453 | * When a task is marked PF_EXITING by do_exit() it's going to be |
| 454 | * dequeued and enqueued multiple times in the exit path. |
| 455 | * Thus we avoid any further update, since we do not want to change |
| 456 | * CPU boosting while the task is exiting. |
| 457 | */ |
| 458 | if (p->flags & PF_EXITING) |
| 459 | return; |
| 460 | |
Patrick Bellasi | d248900 | 2016-07-28 18:44:40 +0100 | [diff] [blame] | 461 | /* |
| 462 | * Boost group accouting is protected by a per-cpu lock and requires |
| 463 | * interrupt to be disabled to avoid race conditions for example on |
| 464 | * do_exit()::cgroup_exit() and task migration. |
| 465 | */ |
| 466 | raw_spin_lock_irqsave(&bg->lock, irq_flags); |
Patrick Bellasi | edd28d3 | 2015-07-07 15:33:20 +0100 | [diff] [blame] | 467 | rcu_read_lock(); |
Patrick Bellasi | d248900 | 2016-07-28 18:44:40 +0100 | [diff] [blame] | 468 | |
Patrick Bellasi | edd28d3 | 2015-07-07 15:33:20 +0100 | [diff] [blame] | 469 | st = task_schedtune(p); |
| 470 | idx = st->idx; |
Patrick Bellasi | edd28d3 | 2015-07-07 15:33:20 +0100 | [diff] [blame] | 471 | |
Patrick Bellasi | d248900 | 2016-07-28 18:44:40 +0100 | [diff] [blame] | 472 | schedtune_tasks_update(p, cpu, idx, ENQUEUE_TASK); |
| 473 | |
| 474 | rcu_read_unlock(); |
| 475 | raw_spin_unlock_irqrestore(&bg->lock, irq_flags); |
| 476 | } |
| 477 | |
Patrick Bellasi | d248900 | 2016-07-28 18:44:40 +0100 | [diff] [blame] | 478 | int schedtune_can_attach(struct cgroup_taskset *tset) |
| 479 | { |
| 480 | struct task_struct *task; |
| 481 | struct cgroup_subsys_state *css; |
| 482 | struct boost_groups *bg; |
| 483 | struct rq_flags irq_flags; |
| 484 | unsigned int cpu; |
| 485 | struct rq *rq; |
| 486 | int src_bg; /* Source boost group index */ |
| 487 | int dst_bg; /* Destination boost group index */ |
| 488 | int tasks; |
| 489 | |
| 490 | if (!unlikely(schedtune_initialized)) |
| 491 | return 0; |
| 492 | |
| 493 | |
| 494 | cgroup_taskset_for_each(task, css, tset) { |
| 495 | |
| 496 | /* |
| 497 | * Lock the CPU's RQ the task is enqueued to avoid race |
| 498 | * conditions with migration code while the task is being |
| 499 | * accounted |
| 500 | */ |
| 501 | rq = lock_rq_of(task, &irq_flags); |
| 502 | |
| 503 | if (!task->on_rq) { |
| 504 | unlock_rq_of(rq, task, &irq_flags); |
| 505 | continue; |
| 506 | } |
| 507 | |
| 508 | /* |
| 509 | * Boost group accouting is protected by a per-cpu lock and requires |
| 510 | * interrupt to be disabled to avoid race conditions on... |
| 511 | */ |
| 512 | cpu = cpu_of(rq); |
| 513 | bg = &per_cpu(cpu_boost_groups, cpu); |
| 514 | raw_spin_lock(&bg->lock); |
| 515 | |
| 516 | dst_bg = css_st(css)->idx; |
| 517 | src_bg = task_schedtune(task)->idx; |
| 518 | |
| 519 | /* |
| 520 | * Current task is not changing boostgroup, which can |
| 521 | * happen when the new hierarchy is in use. |
| 522 | */ |
| 523 | if (unlikely(dst_bg == src_bg)) { |
| 524 | raw_spin_unlock(&bg->lock); |
| 525 | unlock_rq_of(rq, task, &irq_flags); |
| 526 | continue; |
| 527 | } |
| 528 | |
| 529 | /* |
| 530 | * This is the case of a RUNNABLE task which is switching its |
| 531 | * current boost group. |
| 532 | */ |
| 533 | |
| 534 | /* Move task from src to dst boost group */ |
| 535 | tasks = bg->group[src_bg].tasks - 1; |
| 536 | bg->group[src_bg].tasks = max(0, tasks); |
| 537 | bg->group[dst_bg].tasks += 1; |
| 538 | |
| 539 | raw_spin_unlock(&bg->lock); |
| 540 | unlock_rq_of(rq, task, &irq_flags); |
| 541 | |
| 542 | /* Update CPU boost group */ |
| 543 | if (bg->group[src_bg].tasks == 0 || bg->group[dst_bg].tasks == 1) |
| 544 | schedtune_cpu_update(task_cpu(task)); |
| 545 | |
| 546 | } |
| 547 | |
| 548 | return 0; |
| 549 | } |
| 550 | |
Joonwoo Park | f7d6cd4 | 2017-01-17 15:19:43 -0800 | [diff] [blame] | 551 | #ifdef CONFIG_SCHED_WALT |
Vikram Mulukutla | d056dbc | 2017-02-07 18:58:07 -0800 | [diff] [blame] | 552 | static u64 sched_boost_enabled_read(struct cgroup_subsys_state *css, |
| 553 | struct cftype *cft) |
| 554 | { |
| 555 | struct schedtune *st = css_st(css); |
| 556 | |
| 557 | return st->sched_boost_enabled; |
| 558 | } |
| 559 | |
| 560 | static int sched_boost_enabled_write(struct cgroup_subsys_state *css, |
| 561 | struct cftype *cft, u64 enable) |
| 562 | { |
| 563 | struct schedtune *st = css_st(css); |
| 564 | |
| 565 | st->sched_boost_enabled = !!enable; |
| 566 | st->sched_boost_enabled_backup = st->sched_boost_enabled; |
| 567 | |
| 568 | return 0; |
| 569 | } |
| 570 | |
| 571 | static u64 sched_colocate_read(struct cgroup_subsys_state *css, |
| 572 | struct cftype *cft) |
| 573 | { |
| 574 | struct schedtune *st = css_st(css); |
| 575 | |
| 576 | return st->colocate; |
| 577 | } |
| 578 | |
| 579 | static int sched_colocate_write(struct cgroup_subsys_state *css, |
| 580 | struct cftype *cft, u64 colocate) |
| 581 | { |
| 582 | struct schedtune *st = css_st(css); |
| 583 | |
| 584 | if (st->colocate_update_disabled) |
| 585 | return -EPERM; |
| 586 | |
| 587 | st->colocate = !!colocate; |
| 588 | st->colocate_update_disabled = true; |
| 589 | return 0; |
| 590 | } |
| 591 | |
Joonwoo Park | f7d6cd4 | 2017-01-17 15:19:43 -0800 | [diff] [blame] | 592 | #else /* CONFIG_SCHED_WALT */ |
Vikram Mulukutla | d056dbc | 2017-02-07 18:58:07 -0800 | [diff] [blame] | 593 | |
| 594 | static inline void init_sched_boost(struct schedtune *st) { } |
| 595 | |
Joonwoo Park | f7d6cd4 | 2017-01-17 15:19:43 -0800 | [diff] [blame] | 596 | #endif /* CONFIG_SCHED_WALT */ |
Channagoud Kadabi | 8810e5f | 2017-02-17 16:01:05 -0800 | [diff] [blame] | 597 | |
Patrick Bellasi | d248900 | 2016-07-28 18:44:40 +0100 | [diff] [blame] | 598 | void schedtune_cancel_attach(struct cgroup_taskset *tset) |
| 599 | { |
| 600 | /* This can happen only if SchedTune controller is mounted with |
| 601 | * other hierarchies ane one of them fails. Since usually SchedTune is |
| 602 | * mouted on its own hierarcy, for the time being we do not implement |
| 603 | * a proper rollback mechanism */ |
| 604 | WARN(1, "SchedTune cancel attach not implemented"); |
Patrick Bellasi | edd28d3 | 2015-07-07 15:33:20 +0100 | [diff] [blame] | 605 | } |
| 606 | |
| 607 | /* |
| 608 | * NOTE: This function must be called while holding the lock on the CPU RQ |
| 609 | */ |
| 610 | void schedtune_dequeue_task(struct task_struct *p, int cpu) |
| 611 | { |
Patrick Bellasi | d248900 | 2016-07-28 18:44:40 +0100 | [diff] [blame] | 612 | struct boost_groups *bg = &per_cpu(cpu_boost_groups, cpu); |
| 613 | unsigned long irq_flags; |
Patrick Bellasi | edd28d3 | 2015-07-07 15:33:20 +0100 | [diff] [blame] | 614 | struct schedtune *st; |
| 615 | int idx; |
| 616 | |
Patrick Bellasi | d248900 | 2016-07-28 18:44:40 +0100 | [diff] [blame] | 617 | if (!unlikely(schedtune_initialized)) |
| 618 | return; |
| 619 | |
Patrick Bellasi | edd28d3 | 2015-07-07 15:33:20 +0100 | [diff] [blame] | 620 | /* |
| 621 | * When a task is marked PF_EXITING by do_exit() it's going to be |
| 622 | * dequeued and enqueued multiple times in the exit path. |
| 623 | * Thus we avoid any further update, since we do not want to change |
| 624 | * CPU boosting while the task is exiting. |
Patrick Bellasi | d248900 | 2016-07-28 18:44:40 +0100 | [diff] [blame] | 625 | * The last dequeue is already enforce by the do_exit() code path |
| 626 | * via schedtune_exit_task(). |
Patrick Bellasi | edd28d3 | 2015-07-07 15:33:20 +0100 | [diff] [blame] | 627 | */ |
| 628 | if (p->flags & PF_EXITING) |
| 629 | return; |
| 630 | |
Patrick Bellasi | d248900 | 2016-07-28 18:44:40 +0100 | [diff] [blame] | 631 | /* |
| 632 | * Boost group accouting is protected by a per-cpu lock and requires |
| 633 | * interrupt to be disabled to avoid race conditions on... |
| 634 | */ |
| 635 | raw_spin_lock_irqsave(&bg->lock, irq_flags); |
Patrick Bellasi | edd28d3 | 2015-07-07 15:33:20 +0100 | [diff] [blame] | 636 | rcu_read_lock(); |
Patrick Bellasi | d248900 | 2016-07-28 18:44:40 +0100 | [diff] [blame] | 637 | |
Patrick Bellasi | edd28d3 | 2015-07-07 15:33:20 +0100 | [diff] [blame] | 638 | st = task_schedtune(p); |
| 639 | idx = st->idx; |
Patrick Bellasi | edd28d3 | 2015-07-07 15:33:20 +0100 | [diff] [blame] | 640 | |
Patrick Bellasi | d248900 | 2016-07-28 18:44:40 +0100 | [diff] [blame] | 641 | schedtune_tasks_update(p, cpu, idx, DEQUEUE_TASK); |
| 642 | |
| 643 | rcu_read_unlock(); |
| 644 | raw_spin_unlock_irqrestore(&bg->lock, irq_flags); |
| 645 | } |
| 646 | |
| 647 | void schedtune_exit_task(struct task_struct *tsk) |
| 648 | { |
| 649 | struct schedtune *st; |
| 650 | struct rq_flags irq_flags; |
| 651 | unsigned int cpu; |
| 652 | struct rq *rq; |
| 653 | int idx; |
| 654 | |
| 655 | if (!unlikely(schedtune_initialized)) |
| 656 | return; |
| 657 | |
| 658 | rq = lock_rq_of(tsk, &irq_flags); |
| 659 | rcu_read_lock(); |
| 660 | |
| 661 | cpu = cpu_of(rq); |
| 662 | st = task_schedtune(tsk); |
| 663 | idx = st->idx; |
| 664 | schedtune_tasks_update(tsk, cpu, idx, DEQUEUE_TASK); |
| 665 | |
| 666 | rcu_read_unlock(); |
| 667 | unlock_rq_of(rq, tsk, &irq_flags); |
Patrick Bellasi | edd28d3 | 2015-07-07 15:33:20 +0100 | [diff] [blame] | 668 | } |
| 669 | |
| 670 | int schedtune_cpu_boost(int cpu) |
| 671 | { |
| 672 | struct boost_groups *bg; |
| 673 | |
| 674 | bg = &per_cpu(cpu_boost_groups, cpu); |
| 675 | return bg->boost_max; |
| 676 | } |
| 677 | |
Patrick Bellasi | 9b2b8da | 2016-01-14 18:31:53 +0000 | [diff] [blame] | 678 | int schedtune_task_boost(struct task_struct *p) |
| 679 | { |
| 680 | struct schedtune *st; |
| 681 | int task_boost; |
| 682 | |
| 683 | /* Get task boost value */ |
| 684 | rcu_read_lock(); |
| 685 | st = task_schedtune(p); |
| 686 | task_boost = st->boost; |
| 687 | rcu_read_unlock(); |
| 688 | |
| 689 | return task_boost; |
| 690 | } |
| 691 | |
Srinath Sridharan | 42503db | 2016-07-14 13:09:03 -0700 | [diff] [blame] | 692 | int schedtune_prefer_idle(struct task_struct *p) |
| 693 | { |
| 694 | struct schedtune *st; |
| 695 | int prefer_idle; |
| 696 | |
| 697 | /* Get prefer_idle value */ |
| 698 | rcu_read_lock(); |
| 699 | st = task_schedtune(p); |
| 700 | prefer_idle = st->prefer_idle; |
| 701 | rcu_read_unlock(); |
| 702 | |
| 703 | return prefer_idle; |
| 704 | } |
| 705 | |
| 706 | static u64 |
| 707 | prefer_idle_read(struct cgroup_subsys_state *css, struct cftype *cft) |
| 708 | { |
| 709 | struct schedtune *st = css_st(css); |
| 710 | |
| 711 | return st->prefer_idle; |
| 712 | } |
| 713 | |
| 714 | static int |
| 715 | prefer_idle_write(struct cgroup_subsys_state *css, struct cftype *cft, |
| 716 | u64 prefer_idle) |
| 717 | { |
| 718 | struct schedtune *st = css_st(css); |
| 719 | st->prefer_idle = prefer_idle; |
| 720 | |
| 721 | return 0; |
| 722 | } |
| 723 | |
Srinath Sridharan | e71c425 | 2016-07-28 17:28:55 +0100 | [diff] [blame] | 724 | static s64 |
Patrick Bellasi | ae71030 | 2015-06-23 09:17:54 +0100 | [diff] [blame] | 725 | boost_read(struct cgroup_subsys_state *css, struct cftype *cft) |
| 726 | { |
| 727 | struct schedtune *st = css_st(css); |
| 728 | |
| 729 | return st->boost; |
| 730 | } |
| 731 | |
Joonwoo Park | f7d6cd4 | 2017-01-17 15:19:43 -0800 | [diff] [blame] | 732 | #ifdef CONFIG_SCHED_WALT |
Channagoud Kadabi | 8810e5f | 2017-02-17 16:01:05 -0800 | [diff] [blame] | 733 | static void schedtune_attach(struct cgroup_taskset *tset) |
| 734 | { |
| 735 | struct task_struct *task; |
| 736 | struct cgroup_subsys_state *css; |
| 737 | struct schedtune *st; |
| 738 | bool colocate; |
| 739 | |
| 740 | cgroup_taskset_first(tset, &css); |
| 741 | st = css_st(css); |
| 742 | |
| 743 | colocate = st->colocate; |
| 744 | |
| 745 | cgroup_taskset_for_each(task, css, tset) |
| 746 | sync_cgroup_colocation(task, colocate); |
| 747 | |
| 748 | } |
| 749 | #endif |
| 750 | |
Patrick Bellasi | ae71030 | 2015-06-23 09:17:54 +0100 | [diff] [blame] | 751 | static int |
| 752 | boost_write(struct cgroup_subsys_state *css, struct cftype *cft, |
Srinath Sridharan | e71c425 | 2016-07-28 17:28:55 +0100 | [diff] [blame] | 753 | s64 boost) |
Patrick Bellasi | ae71030 | 2015-06-23 09:17:54 +0100 | [diff] [blame] | 754 | { |
| 755 | struct schedtune *st = css_st(css); |
Patrick Bellasi | d5563d3 | 2016-07-29 15:32:26 +0100 | [diff] [blame] | 756 | unsigned threshold_idx; |
| 757 | int boost_pct; |
Patrick Bellasi | ae71030 | 2015-06-23 09:17:54 +0100 | [diff] [blame] | 758 | |
Srinath Sridharan | e71c425 | 2016-07-28 17:28:55 +0100 | [diff] [blame] | 759 | if (boost < -100 || boost > 100) |
Patrick Bellasi | ae71030 | 2015-06-23 09:17:54 +0100 | [diff] [blame] | 760 | return -EINVAL; |
Patrick Bellasi | d5563d3 | 2016-07-29 15:32:26 +0100 | [diff] [blame] | 761 | boost_pct = boost; |
| 762 | |
| 763 | /* |
| 764 | * Update threshold params for Performance Boost (B) |
| 765 | * and Performance Constraint (C) regions. |
| 766 | * The current implementatio uses the same cuts for both |
| 767 | * B and C regions. |
| 768 | */ |
| 769 | threshold_idx = clamp(boost_pct, 0, 99) / 10; |
| 770 | st->perf_boost_idx = threshold_idx; |
| 771 | st->perf_constrain_idx = threshold_idx; |
Patrick Bellasi | ae71030 | 2015-06-23 09:17:54 +0100 | [diff] [blame] | 772 | |
| 773 | st->boost = boost; |
Patrick Bellasi | d5563d3 | 2016-07-29 15:32:26 +0100 | [diff] [blame] | 774 | if (css == &root_schedtune.css) { |
Patrick Bellasi | ae71030 | 2015-06-23 09:17:54 +0100 | [diff] [blame] | 775 | sysctl_sched_cfs_boost = boost; |
Patrick Bellasi | d5563d3 | 2016-07-29 15:32:26 +0100 | [diff] [blame] | 776 | perf_boost_idx = threshold_idx; |
| 777 | perf_constrain_idx = threshold_idx; |
| 778 | } |
Patrick Bellasi | ae71030 | 2015-06-23 09:17:54 +0100 | [diff] [blame] | 779 | |
Patrick Bellasi | 9a871ed | 2016-01-14 12:31:35 +0000 | [diff] [blame] | 780 | /* Update CPU boost */ |
| 781 | schedtune_boostgroup_update(st->idx, st->boost); |
| 782 | |
Patrick Bellasi | 050dcb8 | 2015-06-22 13:49:07 +0100 | [diff] [blame] | 783 | trace_sched_tune_config(st->boost); |
| 784 | |
Patrick Bellasi | ae71030 | 2015-06-23 09:17:54 +0100 | [diff] [blame] | 785 | return 0; |
| 786 | } |
| 787 | |
| 788 | static struct cftype files[] = { |
Joonwoo Park | f7d6cd4 | 2017-01-17 15:19:43 -0800 | [diff] [blame] | 789 | #ifdef CONFIG_SCHED_WALT |
Channagoud Kadabi | 8810e5f | 2017-02-17 16:01:05 -0800 | [diff] [blame] | 790 | { |
| 791 | .name = "sched_boost_no_override", |
| 792 | .read_u64 = sched_boost_override_read, |
| 793 | .write_u64 = sched_boost_override_write, |
| 794 | }, |
| 795 | { |
| 796 | .name = "sched_boost_enabled", |
| 797 | .read_u64 = sched_boost_enabled_read, |
| 798 | .write_u64 = sched_boost_enabled_write, |
| 799 | }, |
| 800 | { |
| 801 | .name = "colocate", |
| 802 | .read_u64 = sched_colocate_read, |
| 803 | .write_u64 = sched_colocate_write, |
| 804 | }, |
| 805 | #endif |
Patrick Bellasi | ae71030 | 2015-06-23 09:17:54 +0100 | [diff] [blame] | 806 | { |
| 807 | .name = "boost", |
Srinath Sridharan | e71c425 | 2016-07-28 17:28:55 +0100 | [diff] [blame] | 808 | .read_s64 = boost_read, |
| 809 | .write_s64 = boost_write, |
Patrick Bellasi | ae71030 | 2015-06-23 09:17:54 +0100 | [diff] [blame] | 810 | }, |
Srinath Sridharan | 42503db | 2016-07-14 13:09:03 -0700 | [diff] [blame] | 811 | { |
| 812 | .name = "prefer_idle", |
| 813 | .read_u64 = prefer_idle_read, |
| 814 | .write_u64 = prefer_idle_write, |
| 815 | }, |
Patrick Bellasi | ae71030 | 2015-06-23 09:17:54 +0100 | [diff] [blame] | 816 | { } /* terminate */ |
| 817 | }; |
| 818 | |
Channagoud Kadabi | 8810e5f | 2017-02-17 16:01:05 -0800 | [diff] [blame] | 819 | |
Patrick Bellasi | ae71030 | 2015-06-23 09:17:54 +0100 | [diff] [blame] | 820 | static int |
| 821 | schedtune_boostgroup_init(struct schedtune *st) |
| 822 | { |
Patrick Bellasi | 9a871ed | 2016-01-14 12:31:35 +0000 | [diff] [blame] | 823 | struct boost_groups *bg; |
| 824 | int cpu; |
| 825 | |
Patrick Bellasi | ae71030 | 2015-06-23 09:17:54 +0100 | [diff] [blame] | 826 | /* Keep track of allocated boost groups */ |
| 827 | allocated_group[st->idx] = st; |
| 828 | |
Patrick Bellasi | 9a871ed | 2016-01-14 12:31:35 +0000 | [diff] [blame] | 829 | /* Initialize the per CPU boost groups */ |
| 830 | for_each_possible_cpu(cpu) { |
| 831 | bg = &per_cpu(cpu_boost_groups, cpu); |
| 832 | bg->group[st->idx].boost = 0; |
| 833 | bg->group[st->idx].tasks = 0; |
| 834 | } |
| 835 | |
Patrick Bellasi | ae71030 | 2015-06-23 09:17:54 +0100 | [diff] [blame] | 836 | return 0; |
| 837 | } |
| 838 | |
Patrick Bellasi | ae71030 | 2015-06-23 09:17:54 +0100 | [diff] [blame] | 839 | static struct cgroup_subsys_state * |
| 840 | schedtune_css_alloc(struct cgroup_subsys_state *parent_css) |
| 841 | { |
| 842 | struct schedtune *st; |
| 843 | int idx; |
| 844 | |
Patrick Bellasi | 52cb67e | 2016-07-29 15:19:41 +0100 | [diff] [blame] | 845 | if (!parent_css) |
Patrick Bellasi | ae71030 | 2015-06-23 09:17:54 +0100 | [diff] [blame] | 846 | return &root_schedtune.css; |
Patrick Bellasi | ae71030 | 2015-06-23 09:17:54 +0100 | [diff] [blame] | 847 | |
| 848 | /* Allow only single level hierachies */ |
| 849 | if (parent_css != &root_schedtune.css) { |
| 850 | pr_err("Nested SchedTune boosting groups not allowed\n"); |
| 851 | return ERR_PTR(-ENOMEM); |
| 852 | } |
| 853 | |
| 854 | /* Allow only a limited number of boosting groups */ |
| 855 | for (idx = 1; idx < BOOSTGROUPS_COUNT; ++idx) |
| 856 | if (!allocated_group[idx]) |
| 857 | break; |
| 858 | if (idx == BOOSTGROUPS_COUNT) { |
| 859 | pr_err("Trying to create more than %d SchedTune boosting groups\n", |
| 860 | BOOSTGROUPS_COUNT); |
| 861 | return ERR_PTR(-ENOSPC); |
| 862 | } |
| 863 | |
| 864 | st = kzalloc(sizeof(*st), GFP_KERNEL); |
| 865 | if (!st) |
| 866 | goto out; |
| 867 | |
| 868 | /* Initialize per CPUs boost group support */ |
| 869 | st->idx = idx; |
Channagoud Kadabi | 8810e5f | 2017-02-17 16:01:05 -0800 | [diff] [blame] | 870 | init_sched_boost(st); |
Patrick Bellasi | ae71030 | 2015-06-23 09:17:54 +0100 | [diff] [blame] | 871 | if (schedtune_boostgroup_init(st)) |
| 872 | goto release; |
| 873 | |
| 874 | return &st->css; |
| 875 | |
| 876 | release: |
| 877 | kfree(st); |
| 878 | out: |
| 879 | return ERR_PTR(-ENOMEM); |
| 880 | } |
| 881 | |
| 882 | static void |
| 883 | schedtune_boostgroup_release(struct schedtune *st) |
| 884 | { |
Patrick Bellasi | 9a871ed | 2016-01-14 12:31:35 +0000 | [diff] [blame] | 885 | /* Reset this boost group */ |
| 886 | schedtune_boostgroup_update(st->idx, 0); |
| 887 | |
Patrick Bellasi | ae71030 | 2015-06-23 09:17:54 +0100 | [diff] [blame] | 888 | /* Keep track of allocated boost groups */ |
| 889 | allocated_group[st->idx] = NULL; |
| 890 | } |
| 891 | |
| 892 | static void |
| 893 | schedtune_css_free(struct cgroup_subsys_state *css) |
| 894 | { |
| 895 | struct schedtune *st = css_st(css); |
| 896 | |
| 897 | schedtune_boostgroup_release(st); |
| 898 | kfree(st); |
| 899 | } |
| 900 | |
| 901 | struct cgroup_subsys schedtune_cgrp_subsys = { |
| 902 | .css_alloc = schedtune_css_alloc, |
| 903 | .css_free = schedtune_css_free, |
Channagoud Kadabi | 8810e5f | 2017-02-17 16:01:05 -0800 | [diff] [blame] | 904 | .allow_attach = subsys_cgroup_allow_attach, |
| 905 | .attach = schedtune_attach, |
Patrick Bellasi | d248900 | 2016-07-28 18:44:40 +0100 | [diff] [blame] | 906 | .can_attach = schedtune_can_attach, |
| 907 | .cancel_attach = schedtune_cancel_attach, |
Patrick Bellasi | ae71030 | 2015-06-23 09:17:54 +0100 | [diff] [blame] | 908 | .legacy_cftypes = files, |
| 909 | .early_init = 1, |
| 910 | }; |
| 911 | |
Patrick Bellasi | 52cb67e | 2016-07-29 15:19:41 +0100 | [diff] [blame] | 912 | static inline void |
| 913 | schedtune_init_cgroups(void) |
| 914 | { |
| 915 | struct boost_groups *bg; |
| 916 | int cpu; |
| 917 | |
| 918 | /* Initialize the per CPU boost groups */ |
| 919 | for_each_possible_cpu(cpu) { |
| 920 | bg = &per_cpu(cpu_boost_groups, cpu); |
| 921 | memset(bg, 0, sizeof(struct boost_groups)); |
Ke Wang | 751e509 | 2016-11-25 13:38:45 +0800 | [diff] [blame] | 922 | raw_spin_lock_init(&bg->lock); |
Patrick Bellasi | 52cb67e | 2016-07-29 15:19:41 +0100 | [diff] [blame] | 923 | } |
| 924 | |
| 925 | pr_info("schedtune: configured to support %d boost groups\n", |
| 926 | BOOSTGROUPS_COUNT); |
Patrick Bellasi | 82ab243 | 2016-08-24 11:02:29 +0100 | [diff] [blame] | 927 | |
| 928 | schedtune_initialized = true; |
Patrick Bellasi | 52cb67e | 2016-07-29 15:19:41 +0100 | [diff] [blame] | 929 | } |
| 930 | |
Patrick Bellasi | 2f369bb | 2016-01-12 18:12:13 +0000 | [diff] [blame] | 931 | #else /* CONFIG_CGROUP_SCHEDTUNE */ |
| 932 | |
| 933 | int |
| 934 | schedtune_accept_deltas(int nrg_delta, int cap_delta, |
| 935 | struct task_struct *task) |
| 936 | { |
| 937 | /* Optimal (O) region */ |
Patrick Bellasi | 5824d98 | 2016-01-20 14:06:05 +0000 | [diff] [blame] | 938 | if (nrg_delta < 0 && cap_delta > 0) { |
| 939 | trace_sched_tune_filter(nrg_delta, cap_delta, 0, 0, 1, 0); |
Patrick Bellasi | 2f369bb | 2016-01-12 18:12:13 +0000 | [diff] [blame] | 940 | return INT_MAX; |
Patrick Bellasi | 5824d98 | 2016-01-20 14:06:05 +0000 | [diff] [blame] | 941 | } |
Patrick Bellasi | 2f369bb | 2016-01-12 18:12:13 +0000 | [diff] [blame] | 942 | |
| 943 | /* Suboptimal (S) region */ |
Patrick Bellasi | 5824d98 | 2016-01-20 14:06:05 +0000 | [diff] [blame] | 944 | if (nrg_delta > 0 && cap_delta < 0) { |
| 945 | trace_sched_tune_filter(nrg_delta, cap_delta, 0, 0, -1, 5); |
Patrick Bellasi | 2f369bb | 2016-01-12 18:12:13 +0000 | [diff] [blame] | 946 | return -INT_MAX; |
Patrick Bellasi | 5824d98 | 2016-01-20 14:06:05 +0000 | [diff] [blame] | 947 | } |
Patrick Bellasi | 2f369bb | 2016-01-12 18:12:13 +0000 | [diff] [blame] | 948 | |
| 949 | return __schedtune_accept_deltas(nrg_delta, cap_delta, |
| 950 | perf_boost_idx, perf_constrain_idx); |
| 951 | } |
| 952 | |
Patrick Bellasi | ae71030 | 2015-06-23 09:17:54 +0100 | [diff] [blame] | 953 | #endif /* CONFIG_CGROUP_SCHEDTUNE */ |
| 954 | |
Patrick Bellasi | 69fa4c7 | 2015-06-22 18:11:44 +0100 | [diff] [blame] | 955 | int |
| 956 | sysctl_sched_cfs_boost_handler(struct ctl_table *table, int write, |
| 957 | void __user *buffer, size_t *lenp, |
| 958 | loff_t *ppos) |
| 959 | { |
| 960 | int ret = proc_dointvec_minmax(table, write, buffer, lenp, ppos); |
Patrick Bellasi | d5563d3 | 2016-07-29 15:32:26 +0100 | [diff] [blame] | 961 | unsigned threshold_idx; |
| 962 | int boost_pct; |
Patrick Bellasi | 69fa4c7 | 2015-06-22 18:11:44 +0100 | [diff] [blame] | 963 | |
| 964 | if (ret || !write) |
| 965 | return ret; |
| 966 | |
Patrick Bellasi | d5563d3 | 2016-07-29 15:32:26 +0100 | [diff] [blame] | 967 | if (sysctl_sched_cfs_boost < -100 || sysctl_sched_cfs_boost > 100) |
| 968 | return -EINVAL; |
| 969 | boost_pct = sysctl_sched_cfs_boost; |
Patrick Bellasi | 2f369bb | 2016-01-12 18:12:13 +0000 | [diff] [blame] | 970 | |
Patrick Bellasi | d5563d3 | 2016-07-29 15:32:26 +0100 | [diff] [blame] | 971 | /* |
| 972 | * Update threshold params for Performance Boost (B) |
| 973 | * and Performance Constraint (C) regions. |
| 974 | * The current implementatio uses the same cuts for both |
| 975 | * B and C regions. |
| 976 | */ |
| 977 | threshold_idx = clamp(boost_pct, 0, 99) / 10; |
| 978 | perf_boost_idx = threshold_idx; |
| 979 | perf_constrain_idx = threshold_idx; |
Patrick Bellasi | 2f369bb | 2016-01-12 18:12:13 +0000 | [diff] [blame] | 980 | |
Patrick Bellasi | 69fa4c7 | 2015-06-22 18:11:44 +0100 | [diff] [blame] | 981 | return 0; |
| 982 | } |
Patrick Bellasi | 2f369bb | 2016-01-12 18:12:13 +0000 | [diff] [blame] | 983 | |
Patrick Bellasi | 2f369bb | 2016-01-12 18:12:13 +0000 | [diff] [blame] | 984 | #ifdef CONFIG_SCHED_DEBUG |
| 985 | static void |
| 986 | schedtune_test_nrg(unsigned long delta_pwr) |
| 987 | { |
| 988 | unsigned long test_delta_pwr; |
| 989 | unsigned long test_norm_pwr; |
| 990 | int idx; |
| 991 | |
| 992 | /* |
| 993 | * Check normalization constants using some constant system |
| 994 | * energy values |
| 995 | */ |
| 996 | pr_info("schedtune: verify normalization constants...\n"); |
| 997 | for (idx = 0; idx < 6; ++idx) { |
| 998 | test_delta_pwr = delta_pwr >> idx; |
| 999 | |
| 1000 | /* Normalize on max energy for target platform */ |
| 1001 | test_norm_pwr = reciprocal_divide( |
| 1002 | test_delta_pwr << SCHED_CAPACITY_SHIFT, |
| 1003 | schedtune_target_nrg.rdiv); |
| 1004 | |
| 1005 | pr_info("schedtune: max_pwr/2^%d: %4lu => norm_pwr: %5lu\n", |
| 1006 | idx, test_delta_pwr, test_norm_pwr); |
| 1007 | } |
| 1008 | } |
| 1009 | #else |
| 1010 | #define schedtune_test_nrg(delta_pwr) |
| 1011 | #endif |
| 1012 | |
| 1013 | /* |
| 1014 | * Compute the min/max power consumption of a cluster and all its CPUs |
| 1015 | */ |
| 1016 | static void |
| 1017 | schedtune_add_cluster_nrg( |
| 1018 | struct sched_domain *sd, |
| 1019 | struct sched_group *sg, |
| 1020 | struct target_nrg *ste) |
| 1021 | { |
| 1022 | struct sched_domain *sd2; |
| 1023 | struct sched_group *sg2; |
| 1024 | |
| 1025 | struct cpumask *cluster_cpus; |
| 1026 | char str[32]; |
| 1027 | |
| 1028 | unsigned long min_pwr; |
| 1029 | unsigned long max_pwr; |
| 1030 | int cpu; |
| 1031 | |
| 1032 | /* Get Cluster energy using EM data for the first CPU */ |
| 1033 | cluster_cpus = sched_group_cpus(sg); |
| 1034 | snprintf(str, 32, "CLUSTER[%*pbl]", |
| 1035 | cpumask_pr_args(cluster_cpus)); |
| 1036 | |
| 1037 | min_pwr = sg->sge->idle_states[sg->sge->nr_idle_states - 1].power; |
| 1038 | max_pwr = sg->sge->cap_states[sg->sge->nr_cap_states - 1].power; |
| 1039 | pr_info("schedtune: %-17s min_pwr: %5lu max_pwr: %5lu\n", |
| 1040 | str, min_pwr, max_pwr); |
| 1041 | |
| 1042 | /* |
| 1043 | * Keep track of this cluster's energy in the computation of the |
| 1044 | * overall system energy |
| 1045 | */ |
| 1046 | ste->min_power += min_pwr; |
| 1047 | ste->max_power += max_pwr; |
| 1048 | |
| 1049 | /* Get CPU energy using EM data for each CPU in the group */ |
| 1050 | for_each_cpu(cpu, cluster_cpus) { |
| 1051 | /* Get a SD view for the specific CPU */ |
| 1052 | for_each_domain(cpu, sd2) { |
| 1053 | /* Get the CPU group */ |
| 1054 | sg2 = sd2->groups; |
| 1055 | min_pwr = sg2->sge->idle_states[sg2->sge->nr_idle_states - 1].power; |
| 1056 | max_pwr = sg2->sge->cap_states[sg2->sge->nr_cap_states - 1].power; |
| 1057 | |
| 1058 | ste->min_power += min_pwr; |
| 1059 | ste->max_power += max_pwr; |
| 1060 | |
| 1061 | snprintf(str, 32, "CPU[%d]", cpu); |
| 1062 | pr_info("schedtune: %-17s min_pwr: %5lu max_pwr: %5lu\n", |
| 1063 | str, min_pwr, max_pwr); |
| 1064 | |
| 1065 | /* |
| 1066 | * Assume we have EM data only at the CPU and |
| 1067 | * the upper CLUSTER level |
| 1068 | */ |
Patrick Bellasi | 2f369bb | 2016-01-12 18:12:13 +0000 | [diff] [blame] | 1069 | break; |
| 1070 | } |
| 1071 | } |
| 1072 | } |
| 1073 | |
| 1074 | /* |
| 1075 | * Initialize the constants required to compute normalized energy. |
| 1076 | * The values of these constants depends on the EM data for the specific |
| 1077 | * target system and topology. |
| 1078 | * Thus, this function is expected to be called by the code |
| 1079 | * that bind the EM to the topology information. |
| 1080 | */ |
| 1081 | static int |
Patrick Bellasi | 52cb67e | 2016-07-29 15:19:41 +0100 | [diff] [blame] | 1082 | schedtune_init(void) |
Patrick Bellasi | 2f369bb | 2016-01-12 18:12:13 +0000 | [diff] [blame] | 1083 | { |
| 1084 | struct target_nrg *ste = &schedtune_target_nrg; |
| 1085 | unsigned long delta_pwr = 0; |
| 1086 | struct sched_domain *sd; |
| 1087 | struct sched_group *sg; |
| 1088 | |
| 1089 | pr_info("schedtune: init normalization constants...\n"); |
| 1090 | ste->max_power = 0; |
| 1091 | ste->min_power = 0; |
| 1092 | |
| 1093 | rcu_read_lock(); |
| 1094 | |
| 1095 | /* |
| 1096 | * When EAS is in use, we always have a pointer to the highest SD |
| 1097 | * which provides EM data. |
| 1098 | */ |
| 1099 | sd = rcu_dereference(per_cpu(sd_ea, cpumask_first(cpu_online_mask))); |
| 1100 | if (!sd) { |
| 1101 | pr_info("schedtune: no energy model data\n"); |
| 1102 | goto nodata; |
| 1103 | } |
| 1104 | |
| 1105 | sg = sd->groups; |
| 1106 | do { |
| 1107 | schedtune_add_cluster_nrg(sd, sg, ste); |
| 1108 | } while (sg = sg->next, sg != sd->groups); |
| 1109 | |
| 1110 | rcu_read_unlock(); |
| 1111 | |
| 1112 | pr_info("schedtune: %-17s min_pwr: %5lu max_pwr: %5lu\n", |
| 1113 | "SYSTEM", ste->min_power, ste->max_power); |
| 1114 | |
| 1115 | /* Compute normalization constants */ |
| 1116 | delta_pwr = ste->max_power - ste->min_power; |
| 1117 | ste->rdiv = reciprocal_value(delta_pwr); |
| 1118 | pr_info("schedtune: using normalization constants mul: %u sh1: %u sh2: %u\n", |
| 1119 | ste->rdiv.m, ste->rdiv.sh1, ste->rdiv.sh2); |
| 1120 | |
| 1121 | schedtune_test_nrg(delta_pwr); |
Patrick Bellasi | 52cb67e | 2016-07-29 15:19:41 +0100 | [diff] [blame] | 1122 | |
| 1123 | #ifdef CONFIG_CGROUP_SCHEDTUNE |
| 1124 | schedtune_init_cgroups(); |
| 1125 | #else |
| 1126 | pr_info("schedtune: configured to support global boosting only\n"); |
Channagoud Kadabi | 8810e5f | 2017-02-17 16:01:05 -0800 | [diff] [blame] | 1127 | #endif /* CONFIG_CGROUP_SCHEDTUNE */ |
Patrick Bellasi | 52cb67e | 2016-07-29 15:19:41 +0100 | [diff] [blame] | 1128 | |
Patrick Bellasi | 2f369bb | 2016-01-12 18:12:13 +0000 | [diff] [blame] | 1129 | return 0; |
| 1130 | |
| 1131 | nodata: |
| 1132 | rcu_read_unlock(); |
| 1133 | return -EINVAL; |
| 1134 | } |
Patrick Bellasi | f472539 | 2016-07-29 16:09:03 +0100 | [diff] [blame] | 1135 | postcore_initcall(schedtune_init); |