]>
Commit | Line | Data |
---|---|---|
b2441318 | 1 | // SPDX-License-Identifier: GPL-2.0 |
bf0f6f24 IM |
2 | /* |
3 | * Completely Fair Scheduling (CFS) Class (SCHED_NORMAL/SCHED_BATCH) | |
4 | * | |
5 | * Copyright (C) 2007 Red Hat, Inc., Ingo Molnar <mingo@redhat.com> | |
6 | * | |
7 | * Interactivity improvements by Mike Galbraith | |
8 | * (C) 2007 Mike Galbraith <efault@gmx.de> | |
9 | * | |
10 | * Various enhancements by Dmitry Adamushko. | |
11 | * (C) 2007 Dmitry Adamushko <dmitry.adamushko@gmail.com> | |
12 | * | |
13 | * Group scheduling enhancements by Srivatsa Vaddagiri | |
14 | * Copyright IBM Corporation, 2007 | |
15 | * Author: Srivatsa Vaddagiri <vatsa@linux.vnet.ibm.com> | |
16 | * | |
17 | * Scaled math optimizations by Thomas Gleixner | |
18 | * Copyright (C) 2007, Thomas Gleixner <tglx@linutronix.de> | |
21805085 PZ |
19 | * |
20 | * Adaptive scheduling granularity, math enhancements by Peter Zijlstra | |
90eec103 | 21 | * Copyright (C) 2007 Red Hat, Inc., Peter Zijlstra |
bf0f6f24 | 22 | */ |
325ea10c | 23 | #include "sched.h" |
029632fb | 24 | |
bf0f6f24 | 25 | /* |
21805085 | 26 | * Targeted preemption latency for CPU-bound tasks: |
bf0f6f24 | 27 | * |
21805085 | 28 | * NOTE: this latency value is not the same as the concept of |
d274a4ce IM |
29 | * 'timeslice length' - timeslices in CFS are of variable length |
30 | * and have no persistent notion like in traditional, time-slice | |
31 | * based scheduling concepts. | |
bf0f6f24 | 32 | * |
d274a4ce IM |
33 | * (to see the precise effective timeslice length of your workload, |
34 | * run vmstat and monitor the context-switches (cs) field) | |
2b4d5b25 IM |
35 | * |
36 | * (default: 6ms * (1 + ilog(ncpus)), units: nanoseconds) | |
bf0f6f24 | 37 | */ |
2b4d5b25 | 38 | unsigned int sysctl_sched_latency = 6000000ULL; |
ed8885a1 | 39 | static unsigned int normalized_sysctl_sched_latency = 6000000ULL; |
2bd8e6d4 | 40 | |
1983a922 CE |
41 | /* |
42 | * The initial- and re-scaling of tunables is configurable | |
1983a922 CE |
43 | * |
44 | * Options are: | |
2b4d5b25 IM |
45 | * |
46 | * SCHED_TUNABLESCALING_NONE - unscaled, always *1 | |
47 | * SCHED_TUNABLESCALING_LOG - scaled logarithmical, *1+ilog(ncpus) | |
48 | * SCHED_TUNABLESCALING_LINEAR - scaled linear, *ncpus | |
49 | * | |
50 | * (default SCHED_TUNABLESCALING_LOG = *(1+ilog(ncpus)) | |
1983a922 | 51 | */ |
8a99b683 | 52 | unsigned int sysctl_sched_tunable_scaling = SCHED_TUNABLESCALING_LOG; |
1983a922 | 53 | |
2bd8e6d4 | 54 | /* |
b2be5e96 | 55 | * Minimal preemption granularity for CPU-bound tasks: |
2b4d5b25 | 56 | * |
864616ee | 57 | * (default: 0.75 msec * (1 + ilog(ncpus)), units: nanoseconds) |
2bd8e6d4 | 58 | */ |
ed8885a1 MS |
59 | unsigned int sysctl_sched_min_granularity = 750000ULL; |
60 | static unsigned int normalized_sysctl_sched_min_granularity = 750000ULL; | |
21805085 | 61 | |
51ce83ed JD |
62 | /* |
63 | * Minimal preemption granularity for CPU-bound SCHED_IDLE tasks. | |
64 | * Applies only when SCHED_IDLE tasks compete with normal tasks. | |
65 | * | |
66 | * (default: 0.75 msec) | |
67 | */ | |
68 | unsigned int sysctl_sched_idle_min_granularity = 750000ULL; | |
69 | ||
21805085 | 70 | /* |
2b4d5b25 | 71 | * This value is kept at sysctl_sched_latency/sysctl_sched_min_granularity |
b2be5e96 | 72 | */ |
0bf377bb | 73 | static unsigned int sched_nr_latency = 8; |
b2be5e96 PZ |
74 | |
75 | /* | |
2bba22c5 | 76 | * After fork, child runs first. If set to 0 (default) then |
b2be5e96 | 77 | * parent will (try to) run first. |
21805085 | 78 | */ |
2bba22c5 | 79 | unsigned int sysctl_sched_child_runs_first __read_mostly; |
bf0f6f24 | 80 | |
bf0f6f24 IM |
81 | /* |
82 | * SCHED_OTHER wake-up granularity. | |
bf0f6f24 IM |
83 | * |
84 | * This option delays the preemption effects of decoupled workloads | |
85 | * and reduces their over-scheduling. Synchronous workloads will still | |
86 | * have immediate wakeup/sleep latencies. | |
2b4d5b25 IM |
87 | * |
88 | * (default: 1 msec * (1 + ilog(ncpus)), units: nanoseconds) | |
bf0f6f24 | 89 | */ |
ed8885a1 MS |
90 | unsigned int sysctl_sched_wakeup_granularity = 1000000UL; |
91 | static unsigned int normalized_sysctl_sched_wakeup_granularity = 1000000UL; | |
bf0f6f24 | 92 | |
2b4d5b25 | 93 | const_debug unsigned int sysctl_sched_migration_cost = 500000UL; |
da84d961 | 94 | |
05289b90 TG |
95 | int sched_thermal_decay_shift; |
96 | static int __init setup_sched_thermal_decay_shift(char *str) | |
97 | { | |
98 | int _shift = 0; | |
99 | ||
100 | if (kstrtoint(str, 0, &_shift)) | |
101 | pr_warn("Unable to set scheduler thermal pressure decay shift parameter\n"); | |
102 | ||
103 | sched_thermal_decay_shift = clamp(_shift, 0, 10); | |
104 | return 1; | |
105 | } | |
106 | __setup("sched_thermal_decay_shift=", setup_sched_thermal_decay_shift); | |
107 | ||
afe06efd TC |
108 | #ifdef CONFIG_SMP |
109 | /* | |
97fb7a0a | 110 | * For asym packing, by default the lower numbered CPU has higher priority. |
afe06efd TC |
111 | */ |
112 | int __weak arch_asym_cpu_priority(int cpu) | |
113 | { | |
114 | return -cpu; | |
115 | } | |
6d101ba6 OJ |
116 | |
117 | /* | |
60e17f5c | 118 | * The margin used when comparing utilization with CPU capacity. |
6d101ba6 OJ |
119 | * |
120 | * (default: ~20%) | |
121 | */ | |
60e17f5c VK |
122 | #define fits_capacity(cap, max) ((cap) * 1280 < (max) * 1024) |
123 | ||
4aed8aa4 VS |
124 | /* |
125 | * The margin used when comparing CPU capacities. | |
126 | * is 'cap1' noticeably greater than 'cap2' | |
127 | * | |
128 | * (default: ~5%) | |
129 | */ | |
130 | #define capacity_greater(cap1, cap2) ((cap1) * 1024 > (cap2) * 1078) | |
afe06efd TC |
131 | #endif |
132 | ||
ec12cb7f PT |
133 | #ifdef CONFIG_CFS_BANDWIDTH |
134 | /* | |
135 | * Amount of runtime to allocate from global (tg) to local (per-cfs_rq) pool | |
136 | * each time a cfs_rq requests quota. | |
137 | * | |
138 | * Note: in the case that the slice exceeds the runtime remaining (either due | |
139 | * to consumption or the quota being specified to be smaller than the slice) | |
140 | * we will always only issue the remaining available time. | |
141 | * | |
2b4d5b25 IM |
142 | * (default: 5 msec, units: microseconds) |
143 | */ | |
144 | unsigned int sysctl_sched_cfs_bandwidth_slice = 5000UL; | |
ec12cb7f PT |
145 | #endif |
146 | ||
8527632d PG |
147 | static inline void update_load_add(struct load_weight *lw, unsigned long inc) |
148 | { | |
149 | lw->weight += inc; | |
150 | lw->inv_weight = 0; | |
151 | } | |
152 | ||
153 | static inline void update_load_sub(struct load_weight *lw, unsigned long dec) | |
154 | { | |
155 | lw->weight -= dec; | |
156 | lw->inv_weight = 0; | |
157 | } | |
158 | ||
159 | static inline void update_load_set(struct load_weight *lw, unsigned long w) | |
160 | { | |
161 | lw->weight = w; | |
162 | lw->inv_weight = 0; | |
163 | } | |
164 | ||
029632fb PZ |
165 | /* |
166 | * Increase the granularity value when there are more CPUs, | |
167 | * because with more CPUs the 'effective latency' as visible | |
168 | * to users decreases. But the relationship is not linear, | |
169 | * so pick a second-best guess by going with the log2 of the | |
170 | * number of CPUs. | |
171 | * | |
172 | * This idea comes from the SD scheduler of Con Kolivas: | |
173 | */ | |
58ac93e4 | 174 | static unsigned int get_update_sysctl_factor(void) |
029632fb | 175 | { |
58ac93e4 | 176 | unsigned int cpus = min_t(unsigned int, num_online_cpus(), 8); |
029632fb PZ |
177 | unsigned int factor; |
178 | ||
179 | switch (sysctl_sched_tunable_scaling) { | |
180 | case SCHED_TUNABLESCALING_NONE: | |
181 | factor = 1; | |
182 | break; | |
183 | case SCHED_TUNABLESCALING_LINEAR: | |
184 | factor = cpus; | |
185 | break; | |
186 | case SCHED_TUNABLESCALING_LOG: | |
187 | default: | |
188 | factor = 1 + ilog2(cpus); | |
189 | break; | |
190 | } | |
191 | ||
192 | return factor; | |
193 | } | |
194 | ||
195 | static void update_sysctl(void) | |
196 | { | |
197 | unsigned int factor = get_update_sysctl_factor(); | |
198 | ||
199 | #define SET_SYSCTL(name) \ | |
200 | (sysctl_##name = (factor) * normalized_sysctl_##name) | |
201 | SET_SYSCTL(sched_min_granularity); | |
202 | SET_SYSCTL(sched_latency); | |
203 | SET_SYSCTL(sched_wakeup_granularity); | |
204 | #undef SET_SYSCTL | |
205 | } | |
206 | ||
f38f12d1 | 207 | void __init sched_init_granularity(void) |
029632fb PZ |
208 | { |
209 | update_sysctl(); | |
210 | } | |
211 | ||
9dbdb155 | 212 | #define WMULT_CONST (~0U) |
029632fb PZ |
213 | #define WMULT_SHIFT 32 |
214 | ||
9dbdb155 PZ |
215 | static void __update_inv_weight(struct load_weight *lw) |
216 | { | |
217 | unsigned long w; | |
218 | ||
219 | if (likely(lw->inv_weight)) | |
220 | return; | |
221 | ||
222 | w = scale_load_down(lw->weight); | |
223 | ||
224 | if (BITS_PER_LONG > 32 && unlikely(w >= WMULT_CONST)) | |
225 | lw->inv_weight = 1; | |
226 | else if (unlikely(!w)) | |
227 | lw->inv_weight = WMULT_CONST; | |
228 | else | |
229 | lw->inv_weight = WMULT_CONST / w; | |
230 | } | |
029632fb PZ |
231 | |
232 | /* | |
9dbdb155 PZ |
233 | * delta_exec * weight / lw.weight |
234 | * OR | |
235 | * (delta_exec * (weight * lw->inv_weight)) >> WMULT_SHIFT | |
236 | * | |
1c3de5e1 | 237 | * Either weight := NICE_0_LOAD and lw \e sched_prio_to_wmult[], in which case |
9dbdb155 PZ |
238 | * we're guaranteed shift stays positive because inv_weight is guaranteed to |
239 | * fit 32 bits, and NICE_0_LOAD gives another 10 bits; therefore shift >= 22. | |
240 | * | |
241 | * Or, weight =< lw.weight (because lw.weight is the runqueue weight), thus | |
242 | * weight/lw.weight <= 1, and therefore our shift will also be positive. | |
029632fb | 243 | */ |
9dbdb155 | 244 | static u64 __calc_delta(u64 delta_exec, unsigned long weight, struct load_weight *lw) |
029632fb | 245 | { |
9dbdb155 | 246 | u64 fact = scale_load_down(weight); |
1e17fb8e | 247 | u32 fact_hi = (u32)(fact >> 32); |
9dbdb155 | 248 | int shift = WMULT_SHIFT; |
1e17fb8e | 249 | int fs; |
029632fb | 250 | |
9dbdb155 | 251 | __update_inv_weight(lw); |
029632fb | 252 | |
1e17fb8e CC |
253 | if (unlikely(fact_hi)) { |
254 | fs = fls(fact_hi); | |
255 | shift -= fs; | |
256 | fact >>= fs; | |
029632fb PZ |
257 | } |
258 | ||
2eeb01a2 | 259 | fact = mul_u32_u32(fact, lw->inv_weight); |
029632fb | 260 | |
1e17fb8e CC |
261 | fact_hi = (u32)(fact >> 32); |
262 | if (fact_hi) { | |
263 | fs = fls(fact_hi); | |
264 | shift -= fs; | |
265 | fact >>= fs; | |
9dbdb155 | 266 | } |
029632fb | 267 | |
9dbdb155 | 268 | return mul_u64_u32_shr(delta_exec, fact, shift); |
029632fb PZ |
269 | } |
270 | ||
271 | ||
272 | const struct sched_class fair_sched_class; | |
a4c2f00f | 273 | |
bf0f6f24 IM |
274 | /************************************************************** |
275 | * CFS operations on generic schedulable entities: | |
276 | */ | |
277 | ||
62160e3f | 278 | #ifdef CONFIG_FAIR_GROUP_SCHED |
8f48894f | 279 | |
b758149c PZ |
280 | /* Walk up scheduling entities hierarchy */ |
281 | #define for_each_sched_entity(se) \ | |
282 | for (; se; se = se->parent) | |
283 | ||
3c93a0c0 QY |
284 | static inline void cfs_rq_tg_path(struct cfs_rq *cfs_rq, char *path, int len) |
285 | { | |
286 | if (!path) | |
287 | return; | |
288 | ||
289 | if (cfs_rq && task_group_is_autogroup(cfs_rq->tg)) | |
290 | autogroup_path(cfs_rq->tg, path, len); | |
291 | else if (cfs_rq && cfs_rq->tg->css.cgroup) | |
292 | cgroup_path(cfs_rq->tg->css.cgroup, path, len); | |
293 | else | |
294 | strlcpy(path, "(null)", len); | |
295 | } | |
296 | ||
f6783319 | 297 | static inline bool list_add_leaf_cfs_rq(struct cfs_rq *cfs_rq) |
3d4b47b4 | 298 | { |
5d299eab PZ |
299 | struct rq *rq = rq_of(cfs_rq); |
300 | int cpu = cpu_of(rq); | |
301 | ||
302 | if (cfs_rq->on_list) | |
f6783319 | 303 | return rq->tmp_alone_branch == &rq->leaf_cfs_rq_list; |
5d299eab PZ |
304 | |
305 | cfs_rq->on_list = 1; | |
306 | ||
307 | /* | |
308 | * Ensure we either appear before our parent (if already | |
309 | * enqueued) or force our parent to appear after us when it is | |
310 | * enqueued. The fact that we always enqueue bottom-up | |
311 | * reduces this to two cases and a special case for the root | |
312 | * cfs_rq. Furthermore, it also means that we will always reset | |
313 | * tmp_alone_branch either when the branch is connected | |
314 | * to a tree or when we reach the top of the tree | |
315 | */ | |
316 | if (cfs_rq->tg->parent && | |
317 | cfs_rq->tg->parent->cfs_rq[cpu]->on_list) { | |
67e86250 | 318 | /* |
5d299eab PZ |
319 | * If parent is already on the list, we add the child |
320 | * just before. Thanks to circular linked property of | |
321 | * the list, this means to put the child at the tail | |
322 | * of the list that starts by parent. | |
67e86250 | 323 | */ |
5d299eab PZ |
324 | list_add_tail_rcu(&cfs_rq->leaf_cfs_rq_list, |
325 | &(cfs_rq->tg->parent->cfs_rq[cpu]->leaf_cfs_rq_list)); | |
326 | /* | |
327 | * The branch is now connected to its tree so we can | |
328 | * reset tmp_alone_branch to the beginning of the | |
329 | * list. | |
330 | */ | |
331 | rq->tmp_alone_branch = &rq->leaf_cfs_rq_list; | |
f6783319 | 332 | return true; |
5d299eab | 333 | } |
3d4b47b4 | 334 | |
5d299eab PZ |
335 | if (!cfs_rq->tg->parent) { |
336 | /* | |
337 | * cfs rq without parent should be put | |
338 | * at the tail of the list. | |
339 | */ | |
340 | list_add_tail_rcu(&cfs_rq->leaf_cfs_rq_list, | |
341 | &rq->leaf_cfs_rq_list); | |
342 | /* | |
343 | * We have reach the top of a tree so we can reset | |
344 | * tmp_alone_branch to the beginning of the list. | |
345 | */ | |
346 | rq->tmp_alone_branch = &rq->leaf_cfs_rq_list; | |
f6783319 | 347 | return true; |
3d4b47b4 | 348 | } |
5d299eab PZ |
349 | |
350 | /* | |
351 | * The parent has not already been added so we want to | |
352 | * make sure that it will be put after us. | |
353 | * tmp_alone_branch points to the begin of the branch | |
354 | * where we will add parent. | |
355 | */ | |
356 | list_add_rcu(&cfs_rq->leaf_cfs_rq_list, rq->tmp_alone_branch); | |
357 | /* | |
358 | * update tmp_alone_branch to points to the new begin | |
359 | * of the branch | |
360 | */ | |
361 | rq->tmp_alone_branch = &cfs_rq->leaf_cfs_rq_list; | |
f6783319 | 362 | return false; |
3d4b47b4 PZ |
363 | } |
364 | ||
365 | static inline void list_del_leaf_cfs_rq(struct cfs_rq *cfs_rq) | |
366 | { | |
367 | if (cfs_rq->on_list) { | |
31bc6aea VG |
368 | struct rq *rq = rq_of(cfs_rq); |
369 | ||
370 | /* | |
371 | * With cfs_rq being unthrottled/throttled during an enqueue, | |
372 | * it can happen the tmp_alone_branch points the a leaf that | |
373 | * we finally want to del. In this case, tmp_alone_branch moves | |
374 | * to the prev element but it will point to rq->leaf_cfs_rq_list | |
375 | * at the end of the enqueue. | |
376 | */ | |
377 | if (rq->tmp_alone_branch == &cfs_rq->leaf_cfs_rq_list) | |
378 | rq->tmp_alone_branch = cfs_rq->leaf_cfs_rq_list.prev; | |
379 | ||
3d4b47b4 PZ |
380 | list_del_rcu(&cfs_rq->leaf_cfs_rq_list); |
381 | cfs_rq->on_list = 0; | |
382 | } | |
383 | } | |
384 | ||
5d299eab PZ |
385 | static inline void assert_list_leaf_cfs_rq(struct rq *rq) |
386 | { | |
387 | SCHED_WARN_ON(rq->tmp_alone_branch != &rq->leaf_cfs_rq_list); | |
388 | } | |
389 | ||
039ae8bc VG |
390 | /* Iterate thr' all leaf cfs_rq's on a runqueue */ |
391 | #define for_each_leaf_cfs_rq_safe(rq, cfs_rq, pos) \ | |
392 | list_for_each_entry_safe(cfs_rq, pos, &rq->leaf_cfs_rq_list, \ | |
393 | leaf_cfs_rq_list) | |
b758149c PZ |
394 | |
395 | /* Do the two (enqueued) entities belong to the same group ? */ | |
fed14d45 | 396 | static inline struct cfs_rq * |
b758149c PZ |
397 | is_same_group(struct sched_entity *se, struct sched_entity *pse) |
398 | { | |
399 | if (se->cfs_rq == pse->cfs_rq) | |
fed14d45 | 400 | return se->cfs_rq; |
b758149c | 401 | |
fed14d45 | 402 | return NULL; |
b758149c PZ |
403 | } |
404 | ||
405 | static inline struct sched_entity *parent_entity(struct sched_entity *se) | |
406 | { | |
407 | return se->parent; | |
408 | } | |
409 | ||
464b7527 PZ |
410 | static void |
411 | find_matching_se(struct sched_entity **se, struct sched_entity **pse) | |
412 | { | |
413 | int se_depth, pse_depth; | |
414 | ||
415 | /* | |
416 | * preemption test can be made between sibling entities who are in the | |
417 | * same cfs_rq i.e who have a common parent. Walk up the hierarchy of | |
418 | * both tasks until we find their ancestors who are siblings of common | |
419 | * parent. | |
420 | */ | |
421 | ||
422 | /* First walk up until both entities are at same depth */ | |
fed14d45 PZ |
423 | se_depth = (*se)->depth; |
424 | pse_depth = (*pse)->depth; | |
464b7527 PZ |
425 | |
426 | while (se_depth > pse_depth) { | |
427 | se_depth--; | |
428 | *se = parent_entity(*se); | |
429 | } | |
430 | ||
431 | while (pse_depth > se_depth) { | |
432 | pse_depth--; | |
433 | *pse = parent_entity(*pse); | |
434 | } | |
435 | ||
436 | while (!is_same_group(*se, *pse)) { | |
437 | *se = parent_entity(*se); | |
438 | *pse = parent_entity(*pse); | |
439 | } | |
440 | } | |
441 | ||
30400039 JD |
442 | static int tg_is_idle(struct task_group *tg) |
443 | { | |
444 | return tg->idle > 0; | |
445 | } | |
446 | ||
447 | static int cfs_rq_is_idle(struct cfs_rq *cfs_rq) | |
448 | { | |
449 | return cfs_rq->idle > 0; | |
450 | } | |
451 | ||
452 | static int se_is_idle(struct sched_entity *se) | |
453 | { | |
454 | if (entity_is_task(se)) | |
455 | return task_has_idle_policy(task_of(se)); | |
456 | return cfs_rq_is_idle(group_cfs_rq(se)); | |
457 | } | |
458 | ||
8f48894f PZ |
459 | #else /* !CONFIG_FAIR_GROUP_SCHED */ |
460 | ||
b758149c PZ |
461 | #define for_each_sched_entity(se) \ |
462 | for (; se; se = NULL) | |
bf0f6f24 | 463 | |
3c93a0c0 QY |
464 | static inline void cfs_rq_tg_path(struct cfs_rq *cfs_rq, char *path, int len) |
465 | { | |
466 | if (path) | |
467 | strlcpy(path, "(null)", len); | |
468 | } | |
469 | ||
f6783319 | 470 | static inline bool list_add_leaf_cfs_rq(struct cfs_rq *cfs_rq) |
3d4b47b4 | 471 | { |
f6783319 | 472 | return true; |
3d4b47b4 PZ |
473 | } |
474 | ||
475 | static inline void list_del_leaf_cfs_rq(struct cfs_rq *cfs_rq) | |
476 | { | |
477 | } | |
478 | ||
5d299eab PZ |
479 | static inline void assert_list_leaf_cfs_rq(struct rq *rq) |
480 | { | |
481 | } | |
482 | ||
039ae8bc VG |
483 | #define for_each_leaf_cfs_rq_safe(rq, cfs_rq, pos) \ |
484 | for (cfs_rq = &rq->cfs, pos = NULL; cfs_rq; cfs_rq = pos) | |
b758149c | 485 | |
b758149c PZ |
486 | static inline struct sched_entity *parent_entity(struct sched_entity *se) |
487 | { | |
488 | return NULL; | |
489 | } | |
490 | ||
464b7527 PZ |
491 | static inline void |
492 | find_matching_se(struct sched_entity **se, struct sched_entity **pse) | |
493 | { | |
494 | } | |
495 | ||
366e7ad6 | 496 | static inline int tg_is_idle(struct task_group *tg) |
30400039 JD |
497 | { |
498 | return 0; | |
499 | } | |
500 | ||
501 | static int cfs_rq_is_idle(struct cfs_rq *cfs_rq) | |
502 | { | |
503 | return 0; | |
504 | } | |
505 | ||
506 | static int se_is_idle(struct sched_entity *se) | |
507 | { | |
508 | return 0; | |
509 | } | |
510 | ||
b758149c PZ |
511 | #endif /* CONFIG_FAIR_GROUP_SCHED */ |
512 | ||
6c16a6dc | 513 | static __always_inline |
9dbdb155 | 514 | void account_cfs_rq_runtime(struct cfs_rq *cfs_rq, u64 delta_exec); |
bf0f6f24 IM |
515 | |
516 | /************************************************************** | |
517 | * Scheduling class tree data structure manipulation methods: | |
518 | */ | |
519 | ||
1bf08230 | 520 | static inline u64 max_vruntime(u64 max_vruntime, u64 vruntime) |
02e0431a | 521 | { |
1bf08230 | 522 | s64 delta = (s64)(vruntime - max_vruntime); |
368059a9 | 523 | if (delta > 0) |
1bf08230 | 524 | max_vruntime = vruntime; |
02e0431a | 525 | |
1bf08230 | 526 | return max_vruntime; |
02e0431a PZ |
527 | } |
528 | ||
0702e3eb | 529 | static inline u64 min_vruntime(u64 min_vruntime, u64 vruntime) |
b0ffd246 PZ |
530 | { |
531 | s64 delta = (s64)(vruntime - min_vruntime); | |
532 | if (delta < 0) | |
533 | min_vruntime = vruntime; | |
534 | ||
535 | return min_vruntime; | |
536 | } | |
537 | ||
bf9be9a1 | 538 | static inline bool entity_before(struct sched_entity *a, |
54fdc581 FC |
539 | struct sched_entity *b) |
540 | { | |
541 | return (s64)(a->vruntime - b->vruntime) < 0; | |
542 | } | |
543 | ||
bf9be9a1 PZ |
544 | #define __node_2_se(node) \ |
545 | rb_entry((node), struct sched_entity, run_node) | |
546 | ||
1af5f730 PZ |
547 | static void update_min_vruntime(struct cfs_rq *cfs_rq) |
548 | { | |
b60205c7 | 549 | struct sched_entity *curr = cfs_rq->curr; |
bfb06889 | 550 | struct rb_node *leftmost = rb_first_cached(&cfs_rq->tasks_timeline); |
b60205c7 | 551 | |
1af5f730 PZ |
552 | u64 vruntime = cfs_rq->min_vruntime; |
553 | ||
b60205c7 PZ |
554 | if (curr) { |
555 | if (curr->on_rq) | |
556 | vruntime = curr->vruntime; | |
557 | else | |
558 | curr = NULL; | |
559 | } | |
1af5f730 | 560 | |
bfb06889 | 561 | if (leftmost) { /* non-empty tree */ |
bf9be9a1 | 562 | struct sched_entity *se = __node_2_se(leftmost); |
1af5f730 | 563 | |
b60205c7 | 564 | if (!curr) |
1af5f730 PZ |
565 | vruntime = se->vruntime; |
566 | else | |
567 | vruntime = min_vruntime(vruntime, se->vruntime); | |
568 | } | |
569 | ||
1bf08230 | 570 | /* ensure we never gain time by being placed backwards. */ |
1af5f730 | 571 | cfs_rq->min_vruntime = max_vruntime(cfs_rq->min_vruntime, vruntime); |
3fe1698b PZ |
572 | #ifndef CONFIG_64BIT |
573 | smp_wmb(); | |
574 | cfs_rq->min_vruntime_copy = cfs_rq->min_vruntime; | |
575 | #endif | |
1af5f730 PZ |
576 | } |
577 | ||
bf9be9a1 PZ |
578 | static inline bool __entity_less(struct rb_node *a, const struct rb_node *b) |
579 | { | |
580 | return entity_before(__node_2_se(a), __node_2_se(b)); | |
581 | } | |
582 | ||
bf0f6f24 IM |
583 | /* |
584 | * Enqueue an entity into the rb-tree: | |
585 | */ | |
0702e3eb | 586 | static void __enqueue_entity(struct cfs_rq *cfs_rq, struct sched_entity *se) |
bf0f6f24 | 587 | { |
bf9be9a1 | 588 | rb_add_cached(&se->run_node, &cfs_rq->tasks_timeline, __entity_less); |
bf0f6f24 IM |
589 | } |
590 | ||
0702e3eb | 591 | static void __dequeue_entity(struct cfs_rq *cfs_rq, struct sched_entity *se) |
bf0f6f24 | 592 | { |
bfb06889 | 593 | rb_erase_cached(&se->run_node, &cfs_rq->tasks_timeline); |
bf0f6f24 IM |
594 | } |
595 | ||
029632fb | 596 | struct sched_entity *__pick_first_entity(struct cfs_rq *cfs_rq) |
bf0f6f24 | 597 | { |
bfb06889 | 598 | struct rb_node *left = rb_first_cached(&cfs_rq->tasks_timeline); |
f4b6755f PZ |
599 | |
600 | if (!left) | |
601 | return NULL; | |
602 | ||
bf9be9a1 | 603 | return __node_2_se(left); |
bf0f6f24 IM |
604 | } |
605 | ||
ac53db59 RR |
606 | static struct sched_entity *__pick_next_entity(struct sched_entity *se) |
607 | { | |
608 | struct rb_node *next = rb_next(&se->run_node); | |
609 | ||
610 | if (!next) | |
611 | return NULL; | |
612 | ||
bf9be9a1 | 613 | return __node_2_se(next); |
ac53db59 RR |
614 | } |
615 | ||
616 | #ifdef CONFIG_SCHED_DEBUG | |
029632fb | 617 | struct sched_entity *__pick_last_entity(struct cfs_rq *cfs_rq) |
aeb73b04 | 618 | { |
bfb06889 | 619 | struct rb_node *last = rb_last(&cfs_rq->tasks_timeline.rb_root); |
aeb73b04 | 620 | |
70eee74b BS |
621 | if (!last) |
622 | return NULL; | |
7eee3e67 | 623 | |
bf9be9a1 | 624 | return __node_2_se(last); |
aeb73b04 PZ |
625 | } |
626 | ||
bf0f6f24 IM |
627 | /************************************************************** |
628 | * Scheduling class statistics methods: | |
629 | */ | |
630 | ||
8a99b683 | 631 | int sched_update_scaling(void) |
b2be5e96 | 632 | { |
58ac93e4 | 633 | unsigned int factor = get_update_sysctl_factor(); |
b2be5e96 | 634 | |
b2be5e96 PZ |
635 | sched_nr_latency = DIV_ROUND_UP(sysctl_sched_latency, |
636 | sysctl_sched_min_granularity); | |
637 | ||
acb4a848 CE |
638 | #define WRT_SYSCTL(name) \ |
639 | (normalized_sysctl_##name = sysctl_##name / (factor)) | |
640 | WRT_SYSCTL(sched_min_granularity); | |
641 | WRT_SYSCTL(sched_latency); | |
642 | WRT_SYSCTL(sched_wakeup_granularity); | |
acb4a848 CE |
643 | #undef WRT_SYSCTL |
644 | ||
b2be5e96 PZ |
645 | return 0; |
646 | } | |
647 | #endif | |
647e7cac | 648 | |
a7be37ac | 649 | /* |
f9c0b095 | 650 | * delta /= w |
a7be37ac | 651 | */ |
9dbdb155 | 652 | static inline u64 calc_delta_fair(u64 delta, struct sched_entity *se) |
a7be37ac | 653 | { |
f9c0b095 | 654 | if (unlikely(se->load.weight != NICE_0_LOAD)) |
9dbdb155 | 655 | delta = __calc_delta(delta, NICE_0_LOAD, &se->load); |
a7be37ac PZ |
656 | |
657 | return delta; | |
658 | } | |
659 | ||
647e7cac IM |
660 | /* |
661 | * The idea is to set a period in which each task runs once. | |
662 | * | |
532b1858 | 663 | * When there are too many tasks (sched_nr_latency) we have to stretch |
647e7cac IM |
664 | * this period because otherwise the slices get too small. |
665 | * | |
666 | * p = (nr <= nl) ? l : l*nr/nl | |
667 | */ | |
4d78e7b6 PZ |
668 | static u64 __sched_period(unsigned long nr_running) |
669 | { | |
8e2b0bf3 BF |
670 | if (unlikely(nr_running > sched_nr_latency)) |
671 | return nr_running * sysctl_sched_min_granularity; | |
672 | else | |
673 | return sysctl_sched_latency; | |
4d78e7b6 PZ |
674 | } |
675 | ||
51ce83ed JD |
676 | static bool sched_idle_cfs_rq(struct cfs_rq *cfs_rq); |
677 | ||
647e7cac IM |
678 | /* |
679 | * We calculate the wall-time slice from the period by taking a part | |
680 | * proportional to the weight. | |
681 | * | |
f9c0b095 | 682 | * s = p*P[w/rw] |
647e7cac | 683 | */ |
6d0f0ebd | 684 | static u64 sched_slice(struct cfs_rq *cfs_rq, struct sched_entity *se) |
21805085 | 685 | { |
0c2de3f0 | 686 | unsigned int nr_running = cfs_rq->nr_running; |
51ce83ed JD |
687 | struct sched_entity *init_se = se; |
688 | unsigned int min_gran; | |
0c2de3f0 PZ |
689 | u64 slice; |
690 | ||
691 | if (sched_feat(ALT_PERIOD)) | |
692 | nr_running = rq_of(cfs_rq)->cfs.h_nr_running; | |
693 | ||
694 | slice = __sched_period(nr_running + !se->on_rq); | |
f9c0b095 | 695 | |
0a582440 | 696 | for_each_sched_entity(se) { |
6272d68c | 697 | struct load_weight *load; |
3104bf03 | 698 | struct load_weight lw; |
51ce83ed | 699 | struct cfs_rq *qcfs_rq; |
6272d68c | 700 | |
51ce83ed JD |
701 | qcfs_rq = cfs_rq_of(se); |
702 | load = &qcfs_rq->load; | |
f9c0b095 | 703 | |
0a582440 | 704 | if (unlikely(!se->on_rq)) { |
51ce83ed | 705 | lw = qcfs_rq->load; |
0a582440 MG |
706 | |
707 | update_load_add(&lw, se->load.weight); | |
708 | load = &lw; | |
709 | } | |
9dbdb155 | 710 | slice = __calc_delta(slice, se->load.weight, load); |
0a582440 | 711 | } |
0c2de3f0 | 712 | |
51ce83ed JD |
713 | if (sched_feat(BASE_SLICE)) { |
714 | if (se_is_idle(init_se) && !sched_idle_cfs_rq(cfs_rq)) | |
715 | min_gran = sysctl_sched_idle_min_granularity; | |
716 | else | |
717 | min_gran = sysctl_sched_min_granularity; | |
718 | ||
719 | slice = max_t(u64, slice, min_gran); | |
720 | } | |
0c2de3f0 | 721 | |
0a582440 | 722 | return slice; |
bf0f6f24 IM |
723 | } |
724 | ||
647e7cac | 725 | /* |
660cc00f | 726 | * We calculate the vruntime slice of a to-be-inserted task. |
647e7cac | 727 | * |
f9c0b095 | 728 | * vs = s/w |
647e7cac | 729 | */ |
f9c0b095 | 730 | static u64 sched_vslice(struct cfs_rq *cfs_rq, struct sched_entity *se) |
67e9fb2a | 731 | { |
f9c0b095 | 732 | return calc_delta_fair(sched_slice(cfs_rq, se), se); |
a7be37ac PZ |
733 | } |
734 | ||
c0796298 | 735 | #include "pelt.h" |
23127296 | 736 | #ifdef CONFIG_SMP |
283e2ed3 | 737 | |
772bd008 | 738 | static int select_idle_sibling(struct task_struct *p, int prev_cpu, int cpu); |
fb13c7ee | 739 | static unsigned long task_h_load(struct task_struct *p); |
3b1baa64 | 740 | static unsigned long capacity_of(int cpu); |
fb13c7ee | 741 | |
540247fb YD |
742 | /* Give new sched_entity start runnable values to heavy its load in infant time */ |
743 | void init_entity_runnable_average(struct sched_entity *se) | |
a75cdaa9 | 744 | { |
540247fb | 745 | struct sched_avg *sa = &se->avg; |
a75cdaa9 | 746 | |
f207934f PZ |
747 | memset(sa, 0, sizeof(*sa)); |
748 | ||
b5a9b340 | 749 | /* |
dfcb245e | 750 | * Tasks are initialized with full load to be seen as heavy tasks until |
b5a9b340 | 751 | * they get a chance to stabilize to their real load level. |
dfcb245e | 752 | * Group entities are initialized with zero load to reflect the fact that |
b5a9b340 VG |
753 | * nothing has been attached to the task group yet. |
754 | */ | |
755 | if (entity_is_task(se)) | |
0dacee1b | 756 | sa->load_avg = scale_load_down(se->load.weight); |
f207934f | 757 | |
9d89c257 | 758 | /* when this task enqueue'ed, it will contribute to its cfs_rq's load_avg */ |
a75cdaa9 | 759 | } |
7ea241af | 760 | |
df217913 | 761 | static void attach_entity_cfs_rq(struct sched_entity *se); |
7dc603c9 | 762 | |
2b8c41da YD |
763 | /* |
764 | * With new tasks being created, their initial util_avgs are extrapolated | |
765 | * based on the cfs_rq's current util_avg: | |
766 | * | |
767 | * util_avg = cfs_rq->util_avg / (cfs_rq->load_avg + 1) * se.load.weight | |
768 | * | |
769 | * However, in many cases, the above util_avg does not give a desired | |
770 | * value. Moreover, the sum of the util_avgs may be divergent, such | |
771 | * as when the series is a harmonic series. | |
772 | * | |
773 | * To solve this problem, we also cap the util_avg of successive tasks to | |
774 | * only 1/2 of the left utilization budget: | |
775 | * | |
8fe5c5a9 | 776 | * util_avg_cap = (cpu_scale - cfs_rq->avg.util_avg) / 2^n |
2b8c41da | 777 | * |
8fe5c5a9 | 778 | * where n denotes the nth task and cpu_scale the CPU capacity. |
2b8c41da | 779 | * |
8fe5c5a9 QP |
780 | * For example, for a CPU with 1024 of capacity, a simplest series from |
781 | * the beginning would be like: | |
2b8c41da YD |
782 | * |
783 | * task util_avg: 512, 256, 128, 64, 32, 16, 8, ... | |
784 | * cfs_rq util_avg: 512, 768, 896, 960, 992, 1008, 1016, ... | |
785 | * | |
786 | * Finally, that extrapolated util_avg is clamped to the cap (util_avg_cap) | |
787 | * if util_avg > util_avg_cap. | |
788 | */ | |
d0fe0b9c | 789 | void post_init_entity_util_avg(struct task_struct *p) |
2b8c41da | 790 | { |
d0fe0b9c | 791 | struct sched_entity *se = &p->se; |
2b8c41da YD |
792 | struct cfs_rq *cfs_rq = cfs_rq_of(se); |
793 | struct sched_avg *sa = &se->avg; | |
8ec59c0f | 794 | long cpu_scale = arch_scale_cpu_capacity(cpu_of(rq_of(cfs_rq))); |
8fe5c5a9 | 795 | long cap = (long)(cpu_scale - cfs_rq->avg.util_avg) / 2; |
2b8c41da YD |
796 | |
797 | if (cap > 0) { | |
798 | if (cfs_rq->avg.util_avg != 0) { | |
799 | sa->util_avg = cfs_rq->avg.util_avg * se->load.weight; | |
800 | sa->util_avg /= (cfs_rq->avg.load_avg + 1); | |
801 | ||
802 | if (sa->util_avg > cap) | |
803 | sa->util_avg = cap; | |
804 | } else { | |
805 | sa->util_avg = cap; | |
806 | } | |
2b8c41da | 807 | } |
7dc603c9 | 808 | |
e21cf434 | 809 | sa->runnable_avg = sa->util_avg; |
9f683953 | 810 | |
d0fe0b9c DE |
811 | if (p->sched_class != &fair_sched_class) { |
812 | /* | |
813 | * For !fair tasks do: | |
814 | * | |
815 | update_cfs_rq_load_avg(now, cfs_rq); | |
a4f9a0e5 | 816 | attach_entity_load_avg(cfs_rq, se); |
d0fe0b9c DE |
817 | switched_from_fair(rq, p); |
818 | * | |
819 | * such that the next switched_to_fair() has the | |
820 | * expected state. | |
821 | */ | |
822 | se->avg.last_update_time = cfs_rq_clock_pelt(cfs_rq); | |
823 | return; | |
7dc603c9 PZ |
824 | } |
825 | ||
df217913 | 826 | attach_entity_cfs_rq(se); |
2b8c41da YD |
827 | } |
828 | ||
7dc603c9 | 829 | #else /* !CONFIG_SMP */ |
540247fb | 830 | void init_entity_runnable_average(struct sched_entity *se) |
a75cdaa9 AS |
831 | { |
832 | } | |
d0fe0b9c | 833 | void post_init_entity_util_avg(struct task_struct *p) |
2b8c41da YD |
834 | { |
835 | } | |
fe749158 | 836 | static void update_tg_load_avg(struct cfs_rq *cfs_rq) |
3d30544f PZ |
837 | { |
838 | } | |
7dc603c9 | 839 | #endif /* CONFIG_SMP */ |
a75cdaa9 | 840 | |
bf0f6f24 | 841 | /* |
9dbdb155 | 842 | * Update the current task's runtime statistics. |
bf0f6f24 | 843 | */ |
b7cc0896 | 844 | static void update_curr(struct cfs_rq *cfs_rq) |
bf0f6f24 | 845 | { |
429d43bc | 846 | struct sched_entity *curr = cfs_rq->curr; |
78becc27 | 847 | u64 now = rq_clock_task(rq_of(cfs_rq)); |
9dbdb155 | 848 | u64 delta_exec; |
bf0f6f24 IM |
849 | |
850 | if (unlikely(!curr)) | |
851 | return; | |
852 | ||
9dbdb155 PZ |
853 | delta_exec = now - curr->exec_start; |
854 | if (unlikely((s64)delta_exec <= 0)) | |
34f28ecd | 855 | return; |
bf0f6f24 | 856 | |
8ebc91d9 | 857 | curr->exec_start = now; |
d842de87 | 858 | |
ceeadb83 YS |
859 | if (schedstat_enabled()) { |
860 | struct sched_statistics *stats; | |
861 | ||
862 | stats = __schedstats_from_se(curr); | |
863 | __schedstat_set(stats->exec_max, | |
864 | max(delta_exec, stats->exec_max)); | |
865 | } | |
9dbdb155 PZ |
866 | |
867 | curr->sum_exec_runtime += delta_exec; | |
ae92882e | 868 | schedstat_add(cfs_rq->exec_clock, delta_exec); |
9dbdb155 PZ |
869 | |
870 | curr->vruntime += calc_delta_fair(delta_exec, curr); | |
871 | update_min_vruntime(cfs_rq); | |
872 | ||
d842de87 SV |
873 | if (entity_is_task(curr)) { |
874 | struct task_struct *curtask = task_of(curr); | |
875 | ||
f977bb49 | 876 | trace_sched_stat_runtime(curtask, delta_exec, curr->vruntime); |
d2cc5ed6 | 877 | cgroup_account_cputime(curtask, delta_exec); |
f06febc9 | 878 | account_group_exec_runtime(curtask, delta_exec); |
d842de87 | 879 | } |
ec12cb7f PT |
880 | |
881 | account_cfs_rq_runtime(cfs_rq, delta_exec); | |
bf0f6f24 IM |
882 | } |
883 | ||
6e998916 SG |
884 | static void update_curr_fair(struct rq *rq) |
885 | { | |
886 | update_curr(cfs_rq_of(&rq->curr->se)); | |
887 | } | |
888 | ||
bf0f6f24 | 889 | static inline void |
60f2415e | 890 | update_stats_wait_start_fair(struct cfs_rq *cfs_rq, struct sched_entity *se) |
bf0f6f24 | 891 | { |
ceeadb83 | 892 | struct sched_statistics *stats; |
60f2415e | 893 | struct task_struct *p = NULL; |
4fa8d299 JP |
894 | |
895 | if (!schedstat_enabled()) | |
896 | return; | |
897 | ||
ceeadb83 YS |
898 | stats = __schedstats_from_se(se); |
899 | ||
60f2415e YS |
900 | if (entity_is_task(se)) |
901 | p = task_of(se); | |
3ea94de1 | 902 | |
60f2415e | 903 | __update_stats_wait_start(rq_of(cfs_rq), p, stats); |
bf0f6f24 IM |
904 | } |
905 | ||
4fa8d299 | 906 | static inline void |
60f2415e | 907 | update_stats_wait_end_fair(struct cfs_rq *cfs_rq, struct sched_entity *se) |
3ea94de1 | 908 | { |
ceeadb83 YS |
909 | struct sched_statistics *stats; |
910 | struct task_struct *p = NULL; | |
cb251765 | 911 | |
4fa8d299 JP |
912 | if (!schedstat_enabled()) |
913 | return; | |
914 | ||
ceeadb83 YS |
915 | stats = __schedstats_from_se(se); |
916 | ||
b9c88f75 | 917 | /* |
918 | * When the sched_schedstat changes from 0 to 1, some sched se | |
919 | * maybe already in the runqueue, the se->statistics.wait_start | |
920 | * will be 0.So it will let the delta wrong. We need to avoid this | |
921 | * scenario. | |
922 | */ | |
ceeadb83 | 923 | if (unlikely(!schedstat_val(stats->wait_start))) |
b9c88f75 | 924 | return; |
925 | ||
60f2415e | 926 | if (entity_is_task(se)) |
3ea94de1 | 927 | p = task_of(se); |
3ea94de1 | 928 | |
60f2415e | 929 | __update_stats_wait_end(rq_of(cfs_rq), p, stats); |
3ea94de1 | 930 | } |
3ea94de1 | 931 | |
4fa8d299 | 932 | static inline void |
60f2415e | 933 | update_stats_enqueue_sleeper_fair(struct cfs_rq *cfs_rq, struct sched_entity *se) |
1a3d027c | 934 | { |
ceeadb83 | 935 | struct sched_statistics *stats; |
1a3d027c | 936 | struct task_struct *tsk = NULL; |
4fa8d299 JP |
937 | |
938 | if (!schedstat_enabled()) | |
939 | return; | |
940 | ||
ceeadb83 YS |
941 | stats = __schedstats_from_se(se); |
942 | ||
1a3d027c JP |
943 | if (entity_is_task(se)) |
944 | tsk = task_of(se); | |
945 | ||
60f2415e | 946 | __update_stats_enqueue_sleeper(rq_of(cfs_rq), tsk, stats); |
3ea94de1 | 947 | } |
3ea94de1 | 948 | |
bf0f6f24 IM |
949 | /* |
950 | * Task is being enqueued - update stats: | |
951 | */ | |
cb251765 | 952 | static inline void |
60f2415e | 953 | update_stats_enqueue_fair(struct cfs_rq *cfs_rq, struct sched_entity *se, int flags) |
bf0f6f24 | 954 | { |
4fa8d299 JP |
955 | if (!schedstat_enabled()) |
956 | return; | |
957 | ||
bf0f6f24 IM |
958 | /* |
959 | * Are we enqueueing a waiting task? (for current tasks | |
960 | * a dequeue/enqueue event is a NOP) | |
961 | */ | |
429d43bc | 962 | if (se != cfs_rq->curr) |
60f2415e | 963 | update_stats_wait_start_fair(cfs_rq, se); |
1a3d027c JP |
964 | |
965 | if (flags & ENQUEUE_WAKEUP) | |
60f2415e | 966 | update_stats_enqueue_sleeper_fair(cfs_rq, se); |
bf0f6f24 IM |
967 | } |
968 | ||
bf0f6f24 | 969 | static inline void |
60f2415e | 970 | update_stats_dequeue_fair(struct cfs_rq *cfs_rq, struct sched_entity *se, int flags) |
bf0f6f24 | 971 | { |
4fa8d299 JP |
972 | |
973 | if (!schedstat_enabled()) | |
974 | return; | |
975 | ||
bf0f6f24 IM |
976 | /* |
977 | * Mark the end of the wait period if dequeueing a | |
978 | * waiting task: | |
979 | */ | |
429d43bc | 980 | if (se != cfs_rq->curr) |
60f2415e | 981 | update_stats_wait_end_fair(cfs_rq, se); |
cb251765 | 982 | |
4fa8d299 JP |
983 | if ((flags & DEQUEUE_SLEEP) && entity_is_task(se)) { |
984 | struct task_struct *tsk = task_of(se); | |
2f064a59 | 985 | unsigned int state; |
cb251765 | 986 | |
2f064a59 PZ |
987 | /* XXX racy against TTWU */ |
988 | state = READ_ONCE(tsk->__state); | |
989 | if (state & TASK_INTERRUPTIBLE) | |
ceeadb83 | 990 | __schedstat_set(tsk->stats.sleep_start, |
4fa8d299 | 991 | rq_clock(rq_of(cfs_rq))); |
2f064a59 | 992 | if (state & TASK_UNINTERRUPTIBLE) |
ceeadb83 | 993 | __schedstat_set(tsk->stats.block_start, |
4fa8d299 | 994 | rq_clock(rq_of(cfs_rq))); |
cb251765 | 995 | } |
cb251765 MG |
996 | } |
997 | ||
bf0f6f24 IM |
998 | /* |
999 | * We are picking a new current task - update its stats: | |
1000 | */ | |
1001 | static inline void | |
79303e9e | 1002 | update_stats_curr_start(struct cfs_rq *cfs_rq, struct sched_entity *se) |
bf0f6f24 IM |
1003 | { |
1004 | /* | |
1005 | * We are starting a new run period: | |
1006 | */ | |
78becc27 | 1007 | se->exec_start = rq_clock_task(rq_of(cfs_rq)); |
bf0f6f24 IM |
1008 | } |
1009 | ||
bf0f6f24 IM |
1010 | /************************************************** |
1011 | * Scheduling class queueing methods: | |
1012 | */ | |
1013 | ||
cbee9f88 PZ |
1014 | #ifdef CONFIG_NUMA_BALANCING |
1015 | /* | |
598f0ec0 MG |
1016 | * Approximate time to scan a full NUMA task in ms. The task scan period is |
1017 | * calculated based on the tasks virtual memory size and | |
1018 | * numa_balancing_scan_size. | |
cbee9f88 | 1019 | */ |
598f0ec0 MG |
1020 | unsigned int sysctl_numa_balancing_scan_period_min = 1000; |
1021 | unsigned int sysctl_numa_balancing_scan_period_max = 60000; | |
6e5fb223 PZ |
1022 | |
1023 | /* Portion of address space to scan in MB */ | |
1024 | unsigned int sysctl_numa_balancing_scan_size = 256; | |
cbee9f88 | 1025 | |
4b96a29b PZ |
1026 | /* Scan @scan_size MB every @scan_period after an initial @scan_delay in ms */ |
1027 | unsigned int sysctl_numa_balancing_scan_delay = 1000; | |
1028 | ||
b5dd77c8 | 1029 | struct numa_group { |
c45a7795 | 1030 | refcount_t refcount; |
b5dd77c8 RR |
1031 | |
1032 | spinlock_t lock; /* nr_tasks, tasks */ | |
1033 | int nr_tasks; | |
1034 | pid_t gid; | |
1035 | int active_nodes; | |
1036 | ||
1037 | struct rcu_head rcu; | |
1038 | unsigned long total_faults; | |
1039 | unsigned long max_faults_cpu; | |
1040 | /* | |
1041 | * Faults_cpu is used to decide whether memory should move | |
1042 | * towards the CPU. As a consequence, these stats are weighted | |
1043 | * more by CPU use than by memory faults. | |
1044 | */ | |
1045 | unsigned long *faults_cpu; | |
04f5c362 | 1046 | unsigned long faults[]; |
b5dd77c8 RR |
1047 | }; |
1048 | ||
cb361d8c JH |
1049 | /* |
1050 | * For functions that can be called in multiple contexts that permit reading | |
1051 | * ->numa_group (see struct task_struct for locking rules). | |
1052 | */ | |
1053 | static struct numa_group *deref_task_numa_group(struct task_struct *p) | |
1054 | { | |
1055 | return rcu_dereference_check(p->numa_group, p == current || | |
9ef7e7e3 | 1056 | (lockdep_is_held(__rq_lockp(task_rq(p))) && !READ_ONCE(p->on_cpu))); |
cb361d8c JH |
1057 | } |
1058 | ||
1059 | static struct numa_group *deref_curr_numa_group(struct task_struct *p) | |
1060 | { | |
1061 | return rcu_dereference_protected(p->numa_group, p == current); | |
1062 | } | |
1063 | ||
b5dd77c8 RR |
1064 | static inline unsigned long group_faults_priv(struct numa_group *ng); |
1065 | static inline unsigned long group_faults_shared(struct numa_group *ng); | |
1066 | ||
598f0ec0 MG |
1067 | static unsigned int task_nr_scan_windows(struct task_struct *p) |
1068 | { | |
1069 | unsigned long rss = 0; | |
1070 | unsigned long nr_scan_pages; | |
1071 | ||
1072 | /* | |
1073 | * Calculations based on RSS as non-present and empty pages are skipped | |
1074 | * by the PTE scanner and NUMA hinting faults should be trapped based | |
1075 | * on resident pages | |
1076 | */ | |
1077 | nr_scan_pages = sysctl_numa_balancing_scan_size << (20 - PAGE_SHIFT); | |
1078 | rss = get_mm_rss(p->mm); | |
1079 | if (!rss) | |
1080 | rss = nr_scan_pages; | |
1081 | ||
1082 | rss = round_up(rss, nr_scan_pages); | |
1083 | return rss / nr_scan_pages; | |
1084 | } | |
1085 | ||
3b03706f | 1086 | /* For sanity's sake, never scan more PTEs than MAX_SCAN_WINDOW MB/sec. */ |
598f0ec0 MG |
1087 | #define MAX_SCAN_WINDOW 2560 |
1088 | ||
1089 | static unsigned int task_scan_min(struct task_struct *p) | |
1090 | { | |
316c1608 | 1091 | unsigned int scan_size = READ_ONCE(sysctl_numa_balancing_scan_size); |
598f0ec0 MG |
1092 | unsigned int scan, floor; |
1093 | unsigned int windows = 1; | |
1094 | ||
64192658 KT |
1095 | if (scan_size < MAX_SCAN_WINDOW) |
1096 | windows = MAX_SCAN_WINDOW / scan_size; | |
598f0ec0 MG |
1097 | floor = 1000 / windows; |
1098 | ||
1099 | scan = sysctl_numa_balancing_scan_period_min / task_nr_scan_windows(p); | |
1100 | return max_t(unsigned int, floor, scan); | |
1101 | } | |
1102 | ||
b5dd77c8 RR |
1103 | static unsigned int task_scan_start(struct task_struct *p) |
1104 | { | |
1105 | unsigned long smin = task_scan_min(p); | |
1106 | unsigned long period = smin; | |
cb361d8c | 1107 | struct numa_group *ng; |
b5dd77c8 RR |
1108 | |
1109 | /* Scale the maximum scan period with the amount of shared memory. */ | |
cb361d8c JH |
1110 | rcu_read_lock(); |
1111 | ng = rcu_dereference(p->numa_group); | |
1112 | if (ng) { | |
b5dd77c8 RR |
1113 | unsigned long shared = group_faults_shared(ng); |
1114 | unsigned long private = group_faults_priv(ng); | |
1115 | ||
c45a7795 | 1116 | period *= refcount_read(&ng->refcount); |
b5dd77c8 RR |
1117 | period *= shared + 1; |
1118 | period /= private + shared + 1; | |
1119 | } | |
cb361d8c | 1120 | rcu_read_unlock(); |
b5dd77c8 RR |
1121 | |
1122 | return max(smin, period); | |
1123 | } | |
1124 | ||
598f0ec0 MG |
1125 | static unsigned int task_scan_max(struct task_struct *p) |
1126 | { | |
b5dd77c8 RR |
1127 | unsigned long smin = task_scan_min(p); |
1128 | unsigned long smax; | |
cb361d8c | 1129 | struct numa_group *ng; |
598f0ec0 MG |
1130 | |
1131 | /* Watch for min being lower than max due to floor calculations */ | |
1132 | smax = sysctl_numa_balancing_scan_period_max / task_nr_scan_windows(p); | |
b5dd77c8 RR |
1133 | |
1134 | /* Scale the maximum scan period with the amount of shared memory. */ | |
cb361d8c JH |
1135 | ng = deref_curr_numa_group(p); |
1136 | if (ng) { | |
b5dd77c8 RR |
1137 | unsigned long shared = group_faults_shared(ng); |
1138 | unsigned long private = group_faults_priv(ng); | |
1139 | unsigned long period = smax; | |
1140 | ||
c45a7795 | 1141 | period *= refcount_read(&ng->refcount); |
b5dd77c8 RR |
1142 | period *= shared + 1; |
1143 | period /= private + shared + 1; | |
1144 | ||
1145 | smax = max(smax, period); | |
1146 | } | |
1147 | ||
598f0ec0 MG |
1148 | return max(smin, smax); |
1149 | } | |
1150 | ||
0ec8aa00 PZ |
1151 | static void account_numa_enqueue(struct rq *rq, struct task_struct *p) |
1152 | { | |
98fa15f3 | 1153 | rq->nr_numa_running += (p->numa_preferred_nid != NUMA_NO_NODE); |
0ec8aa00 PZ |
1154 | rq->nr_preferred_running += (p->numa_preferred_nid == task_node(p)); |
1155 | } | |
1156 | ||
1157 | static void account_numa_dequeue(struct rq *rq, struct task_struct *p) | |
1158 | { | |
98fa15f3 | 1159 | rq->nr_numa_running -= (p->numa_preferred_nid != NUMA_NO_NODE); |
0ec8aa00 PZ |
1160 | rq->nr_preferred_running -= (p->numa_preferred_nid == task_node(p)); |
1161 | } | |
1162 | ||
be1e4e76 RR |
1163 | /* Shared or private faults. */ |
1164 | #define NR_NUMA_HINT_FAULT_TYPES 2 | |
1165 | ||
1166 | /* Memory and CPU locality */ | |
1167 | #define NR_NUMA_HINT_FAULT_STATS (NR_NUMA_HINT_FAULT_TYPES * 2) | |
1168 | ||
1169 | /* Averaged statistics, and temporary buffers. */ | |
1170 | #define NR_NUMA_HINT_FAULT_BUCKETS (NR_NUMA_HINT_FAULT_STATS * 2) | |
1171 | ||
e29cf08b MG |
1172 | pid_t task_numa_group_id(struct task_struct *p) |
1173 | { | |
cb361d8c JH |
1174 | struct numa_group *ng; |
1175 | pid_t gid = 0; | |
1176 | ||
1177 | rcu_read_lock(); | |
1178 | ng = rcu_dereference(p->numa_group); | |
1179 | if (ng) | |
1180 | gid = ng->gid; | |
1181 | rcu_read_unlock(); | |
1182 | ||
1183 | return gid; | |
e29cf08b MG |
1184 | } |
1185 | ||
44dba3d5 | 1186 | /* |
97fb7a0a | 1187 | * The averaged statistics, shared & private, memory & CPU, |
44dba3d5 IM |
1188 | * occupy the first half of the array. The second half of the |
1189 | * array is for current counters, which are averaged into the | |
1190 | * first set by task_numa_placement. | |
1191 | */ | |
1192 | static inline int task_faults_idx(enum numa_faults_stats s, int nid, int priv) | |
ac8e895b | 1193 | { |
44dba3d5 | 1194 | return NR_NUMA_HINT_FAULT_TYPES * (s * nr_node_ids + nid) + priv; |
ac8e895b MG |
1195 | } |
1196 | ||
1197 | static inline unsigned long task_faults(struct task_struct *p, int nid) | |
1198 | { | |
44dba3d5 | 1199 | if (!p->numa_faults) |
ac8e895b MG |
1200 | return 0; |
1201 | ||
44dba3d5 IM |
1202 | return p->numa_faults[task_faults_idx(NUMA_MEM, nid, 0)] + |
1203 | p->numa_faults[task_faults_idx(NUMA_MEM, nid, 1)]; | |
ac8e895b MG |
1204 | } |
1205 | ||
83e1d2cd MG |
1206 | static inline unsigned long group_faults(struct task_struct *p, int nid) |
1207 | { | |
cb361d8c JH |
1208 | struct numa_group *ng = deref_task_numa_group(p); |
1209 | ||
1210 | if (!ng) | |
83e1d2cd MG |
1211 | return 0; |
1212 | ||
cb361d8c JH |
1213 | return ng->faults[task_faults_idx(NUMA_MEM, nid, 0)] + |
1214 | ng->faults[task_faults_idx(NUMA_MEM, nid, 1)]; | |
83e1d2cd MG |
1215 | } |
1216 | ||
20e07dea RR |
1217 | static inline unsigned long group_faults_cpu(struct numa_group *group, int nid) |
1218 | { | |
44dba3d5 IM |
1219 | return group->faults_cpu[task_faults_idx(NUMA_MEM, nid, 0)] + |
1220 | group->faults_cpu[task_faults_idx(NUMA_MEM, nid, 1)]; | |
20e07dea RR |
1221 | } |
1222 | ||
b5dd77c8 RR |
1223 | static inline unsigned long group_faults_priv(struct numa_group *ng) |
1224 | { | |
1225 | unsigned long faults = 0; | |
1226 | int node; | |
1227 | ||
1228 | for_each_online_node(node) { | |
1229 | faults += ng->faults[task_faults_idx(NUMA_MEM, node, 1)]; | |
1230 | } | |
1231 | ||
1232 | return faults; | |
1233 | } | |
1234 | ||
1235 | static inline unsigned long group_faults_shared(struct numa_group *ng) | |
1236 | { | |
1237 | unsigned long faults = 0; | |
1238 | int node; | |
1239 | ||
1240 | for_each_online_node(node) { | |
1241 | faults += ng->faults[task_faults_idx(NUMA_MEM, node, 0)]; | |
1242 | } | |
1243 | ||
1244 | return faults; | |
1245 | } | |
1246 | ||
4142c3eb RR |
1247 | /* |
1248 | * A node triggering more than 1/3 as many NUMA faults as the maximum is | |
1249 | * considered part of a numa group's pseudo-interleaving set. Migrations | |
1250 | * between these nodes are slowed down, to allow things to settle down. | |
1251 | */ | |
1252 | #define ACTIVE_NODE_FRACTION 3 | |
1253 | ||
1254 | static bool numa_is_active_node(int nid, struct numa_group *ng) | |
1255 | { | |
1256 | return group_faults_cpu(ng, nid) * ACTIVE_NODE_FRACTION > ng->max_faults_cpu; | |
1257 | } | |
1258 | ||
6c6b1193 RR |
1259 | /* Handle placement on systems where not all nodes are directly connected. */ |
1260 | static unsigned long score_nearby_nodes(struct task_struct *p, int nid, | |
1261 | int maxdist, bool task) | |
1262 | { | |
1263 | unsigned long score = 0; | |
1264 | int node; | |
1265 | ||
1266 | /* | |
1267 | * All nodes are directly connected, and the same distance | |
1268 | * from each other. No need for fancy placement algorithms. | |
1269 | */ | |
1270 | if (sched_numa_topology_type == NUMA_DIRECT) | |
1271 | return 0; | |
1272 | ||
1273 | /* | |
1274 | * This code is called for each node, introducing N^2 complexity, | |
1275 | * which should be ok given the number of nodes rarely exceeds 8. | |
1276 | */ | |
1277 | for_each_online_node(node) { | |
1278 | unsigned long faults; | |
1279 | int dist = node_distance(nid, node); | |
1280 | ||
1281 | /* | |
1282 | * The furthest away nodes in the system are not interesting | |
1283 | * for placement; nid was already counted. | |
1284 | */ | |
1285 | if (dist == sched_max_numa_distance || node == nid) | |
1286 | continue; | |
1287 | ||
1288 | /* | |
1289 | * On systems with a backplane NUMA topology, compare groups | |
1290 | * of nodes, and move tasks towards the group with the most | |
1291 | * memory accesses. When comparing two nodes at distance | |
1292 | * "hoplimit", only nodes closer by than "hoplimit" are part | |
1293 | * of each group. Skip other nodes. | |
1294 | */ | |
1295 | if (sched_numa_topology_type == NUMA_BACKPLANE && | |
0ee7e74d | 1296 | dist >= maxdist) |
6c6b1193 RR |
1297 | continue; |
1298 | ||
1299 | /* Add up the faults from nearby nodes. */ | |
1300 | if (task) | |
1301 | faults = task_faults(p, node); | |
1302 | else | |
1303 | faults = group_faults(p, node); | |
1304 | ||
1305 | /* | |
1306 | * On systems with a glueless mesh NUMA topology, there are | |
1307 | * no fixed "groups of nodes". Instead, nodes that are not | |
1308 | * directly connected bounce traffic through intermediate | |
1309 | * nodes; a numa_group can occupy any set of nodes. | |
1310 | * The further away a node is, the less the faults count. | |
1311 | * This seems to result in good task placement. | |
1312 | */ | |
1313 | if (sched_numa_topology_type == NUMA_GLUELESS_MESH) { | |
1314 | faults *= (sched_max_numa_distance - dist); | |
1315 | faults /= (sched_max_numa_distance - LOCAL_DISTANCE); | |
1316 | } | |
1317 | ||
1318 | score += faults; | |
1319 | } | |
1320 | ||
1321 | return score; | |
1322 | } | |
1323 | ||
83e1d2cd MG |
1324 | /* |
1325 | * These return the fraction of accesses done by a particular task, or | |
1326 | * task group, on a particular numa node. The group weight is given a | |
1327 | * larger multiplier, in order to group tasks together that are almost | |
1328 | * evenly spread out between numa nodes. | |
1329 | */ | |
7bd95320 RR |
1330 | static inline unsigned long task_weight(struct task_struct *p, int nid, |
1331 | int dist) | |
83e1d2cd | 1332 | { |
7bd95320 | 1333 | unsigned long faults, total_faults; |
83e1d2cd | 1334 | |
44dba3d5 | 1335 | if (!p->numa_faults) |
83e1d2cd MG |
1336 | return 0; |
1337 | ||
1338 | total_faults = p->total_numa_faults; | |
1339 | ||
1340 | if (!total_faults) | |
1341 | return 0; | |
1342 | ||
7bd95320 | 1343 | faults = task_faults(p, nid); |
6c6b1193 RR |
1344 | faults += score_nearby_nodes(p, nid, dist, true); |
1345 | ||
7bd95320 | 1346 | return 1000 * faults / total_faults; |
83e1d2cd MG |
1347 | } |
1348 | ||
7bd95320 RR |
1349 | static inline unsigned long group_weight(struct task_struct *p, int nid, |
1350 | int dist) | |
83e1d2cd | 1351 | { |
cb361d8c | 1352 | struct numa_group *ng = deref_task_numa_group(p); |
7bd95320 RR |
1353 | unsigned long faults, total_faults; |
1354 | ||
cb361d8c | 1355 | if (!ng) |
7bd95320 RR |
1356 | return 0; |
1357 | ||
cb361d8c | 1358 | total_faults = ng->total_faults; |
7bd95320 RR |
1359 | |
1360 | if (!total_faults) | |
83e1d2cd MG |
1361 | return 0; |
1362 | ||
7bd95320 | 1363 | faults = group_faults(p, nid); |
6c6b1193 RR |
1364 | faults += score_nearby_nodes(p, nid, dist, false); |
1365 | ||
7bd95320 | 1366 | return 1000 * faults / total_faults; |
83e1d2cd MG |
1367 | } |
1368 | ||
10f39042 RR |
1369 | bool should_numa_migrate_memory(struct task_struct *p, struct page * page, |
1370 | int src_nid, int dst_cpu) | |
1371 | { | |
cb361d8c | 1372 | struct numa_group *ng = deref_curr_numa_group(p); |
10f39042 RR |
1373 | int dst_nid = cpu_to_node(dst_cpu); |
1374 | int last_cpupid, this_cpupid; | |
1375 | ||
1376 | this_cpupid = cpu_pid_to_cpupid(dst_cpu, current->pid); | |
37355bdc MG |
1377 | last_cpupid = page_cpupid_xchg_last(page, this_cpupid); |
1378 | ||
1379 | /* | |
1380 | * Allow first faults or private faults to migrate immediately early in | |
1381 | * the lifetime of a task. The magic number 4 is based on waiting for | |
1382 | * two full passes of the "multi-stage node selection" test that is | |
1383 | * executed below. | |
1384 | */ | |
98fa15f3 | 1385 | if ((p->numa_preferred_nid == NUMA_NO_NODE || p->numa_scan_seq <= 4) && |
37355bdc MG |
1386 | (cpupid_pid_unset(last_cpupid) || cpupid_match_pid(p, last_cpupid))) |
1387 | return true; | |
10f39042 RR |
1388 | |
1389 | /* | |
1390 | * Multi-stage node selection is used in conjunction with a periodic | |
1391 | * migration fault to build a temporal task<->page relation. By using | |
1392 | * a two-stage filter we remove short/unlikely relations. | |
1393 | * | |
1394 | * Using P(p) ~ n_p / n_t as per frequentist probability, we can equate | |
1395 | * a task's usage of a particular page (n_p) per total usage of this | |
1396 | * page (n_t) (in a given time-span) to a probability. | |
1397 | * | |
1398 | * Our periodic faults will sample this probability and getting the | |
1399 | * same result twice in a row, given these samples are fully | |
1400 | * independent, is then given by P(n)^2, provided our sample period | |
1401 | * is sufficiently short compared to the usage pattern. | |
1402 | * | |
1403 | * This quadric squishes small probabilities, making it less likely we | |
1404 | * act on an unlikely task<->page relation. | |
1405 | */ | |
10f39042 RR |
1406 | if (!cpupid_pid_unset(last_cpupid) && |
1407 | cpupid_to_nid(last_cpupid) != dst_nid) | |
1408 | return false; | |
1409 | ||
1410 | /* Always allow migrate on private faults */ | |
1411 | if (cpupid_match_pid(p, last_cpupid)) | |
1412 | return true; | |
1413 | ||
1414 | /* A shared fault, but p->numa_group has not been set up yet. */ | |
1415 | if (!ng) | |
1416 | return true; | |
1417 | ||
1418 | /* | |
4142c3eb RR |
1419 | * Destination node is much more heavily used than the source |
1420 | * node? Allow migration. | |
10f39042 | 1421 | */ |
4142c3eb RR |
1422 | if (group_faults_cpu(ng, dst_nid) > group_faults_cpu(ng, src_nid) * |
1423 | ACTIVE_NODE_FRACTION) | |
10f39042 RR |
1424 | return true; |
1425 | ||
1426 | /* | |
4142c3eb RR |
1427 | * Distribute memory according to CPU & memory use on each node, |
1428 | * with 3/4 hysteresis to avoid unnecessary memory migrations: | |
1429 | * | |
1430 | * faults_cpu(dst) 3 faults_cpu(src) | |
1431 | * --------------- * - > --------------- | |
1432 | * faults_mem(dst) 4 faults_mem(src) | |
10f39042 | 1433 | */ |
4142c3eb RR |
1434 | return group_faults_cpu(ng, dst_nid) * group_faults(p, src_nid) * 3 > |
1435 | group_faults_cpu(ng, src_nid) * group_faults(p, dst_nid) * 4; | |
10f39042 RR |
1436 | } |
1437 | ||
6499b1b2 VG |
1438 | /* |
1439 | * 'numa_type' describes the node at the moment of load balancing. | |
1440 | */ | |
1441 | enum numa_type { | |
1442 | /* The node has spare capacity that can be used to run more tasks. */ | |
1443 | node_has_spare = 0, | |
1444 | /* | |
1445 | * The node is fully used and the tasks don't compete for more CPU | |
1446 | * cycles. Nevertheless, some tasks might wait before running. | |
1447 | */ | |
1448 | node_fully_busy, | |
1449 | /* | |
1450 | * The node is overloaded and can't provide expected CPU cycles to all | |
1451 | * tasks. | |
1452 | */ | |
1453 | node_overloaded | |
1454 | }; | |
58d081b5 | 1455 | |
fb13c7ee | 1456 | /* Cached statistics for all CPUs within a node */ |
58d081b5 MG |
1457 | struct numa_stats { |
1458 | unsigned long load; | |
8e0e0eda | 1459 | unsigned long runnable; |
6499b1b2 | 1460 | unsigned long util; |
fb13c7ee | 1461 | /* Total compute capacity of CPUs on a node */ |
5ef20ca1 | 1462 | unsigned long compute_capacity; |
6499b1b2 VG |
1463 | unsigned int nr_running; |
1464 | unsigned int weight; | |
1465 | enum numa_type node_type; | |
ff7db0bf | 1466 | int idle_cpu; |
58d081b5 | 1467 | }; |
e6628d5b | 1468 | |
ff7db0bf MG |
1469 | static inline bool is_core_idle(int cpu) |
1470 | { | |
1471 | #ifdef CONFIG_SCHED_SMT | |
1472 | int sibling; | |
1473 | ||
1474 | for_each_cpu(sibling, cpu_smt_mask(cpu)) { | |
1475 | if (cpu == sibling) | |
1476 | continue; | |
1477 | ||
1c6829cf | 1478 | if (!idle_cpu(sibling)) |
ff7db0bf MG |
1479 | return false; |
1480 | } | |
1481 | #endif | |
1482 | ||
1483 | return true; | |
1484 | } | |
1485 | ||
58d081b5 MG |
1486 | struct task_numa_env { |
1487 | struct task_struct *p; | |
e6628d5b | 1488 | |
58d081b5 MG |
1489 | int src_cpu, src_nid; |
1490 | int dst_cpu, dst_nid; | |
e6628d5b | 1491 | |
58d081b5 | 1492 | struct numa_stats src_stats, dst_stats; |
e6628d5b | 1493 | |
40ea2b42 | 1494 | int imbalance_pct; |
7bd95320 | 1495 | int dist; |
fb13c7ee MG |
1496 | |
1497 | struct task_struct *best_task; | |
1498 | long best_imp; | |
58d081b5 MG |
1499 | int best_cpu; |
1500 | }; | |
1501 | ||
6499b1b2 | 1502 | static unsigned long cpu_load(struct rq *rq); |
8e0e0eda | 1503 | static unsigned long cpu_runnable(struct rq *rq); |
6499b1b2 | 1504 | static unsigned long cpu_util(int cpu); |
7d2b5dd0 MG |
1505 | static inline long adjust_numa_imbalance(int imbalance, |
1506 | int dst_running, int dst_weight); | |
6499b1b2 VG |
1507 | |
1508 | static inline enum | |
1509 | numa_type numa_classify(unsigned int imbalance_pct, | |
1510 | struct numa_stats *ns) | |
1511 | { | |
1512 | if ((ns->nr_running > ns->weight) && | |
8e0e0eda VG |
1513 | (((ns->compute_capacity * 100) < (ns->util * imbalance_pct)) || |
1514 | ((ns->compute_capacity * imbalance_pct) < (ns->runnable * 100)))) | |
6499b1b2 VG |
1515 | return node_overloaded; |
1516 | ||
1517 | if ((ns->nr_running < ns->weight) || | |
8e0e0eda VG |
1518 | (((ns->compute_capacity * 100) > (ns->util * imbalance_pct)) && |
1519 | ((ns->compute_capacity * imbalance_pct) > (ns->runnable * 100)))) | |
6499b1b2 VG |
1520 | return node_has_spare; |
1521 | ||
1522 | return node_fully_busy; | |
1523 | } | |
1524 | ||
76c389ab VS |
1525 | #ifdef CONFIG_SCHED_SMT |
1526 | /* Forward declarations of select_idle_sibling helpers */ | |
1527 | static inline bool test_idle_cores(int cpu, bool def); | |
ff7db0bf MG |
1528 | static inline int numa_idle_core(int idle_core, int cpu) |
1529 | { | |
ff7db0bf MG |
1530 | if (!static_branch_likely(&sched_smt_present) || |
1531 | idle_core >= 0 || !test_idle_cores(cpu, false)) | |
1532 | return idle_core; | |
1533 | ||
1534 | /* | |
1535 | * Prefer cores instead of packing HT siblings | |
1536 | * and triggering future load balancing. | |
1537 | */ | |
1538 | if (is_core_idle(cpu)) | |
1539 | idle_core = cpu; | |
ff7db0bf MG |
1540 | |
1541 | return idle_core; | |
1542 | } | |
76c389ab VS |
1543 | #else |
1544 | static inline int numa_idle_core(int idle_core, int cpu) | |
1545 | { | |
1546 | return idle_core; | |
1547 | } | |
1548 | #endif | |
ff7db0bf | 1549 | |
6499b1b2 | 1550 | /* |
ff7db0bf MG |
1551 | * Gather all necessary information to make NUMA balancing placement |
1552 | * decisions that are compatible with standard load balancer. This | |
1553 | * borrows code and logic from update_sg_lb_stats but sharing a | |
1554 | * common implementation is impractical. | |
6499b1b2 VG |
1555 | */ |
1556 | static void update_numa_stats(struct task_numa_env *env, | |
ff7db0bf MG |
1557 | struct numa_stats *ns, int nid, |
1558 | bool find_idle) | |
6499b1b2 | 1559 | { |
ff7db0bf | 1560 | int cpu, idle_core = -1; |
6499b1b2 VG |
1561 | |
1562 | memset(ns, 0, sizeof(*ns)); | |
ff7db0bf MG |
1563 | ns->idle_cpu = -1; |
1564 | ||
0621df31 | 1565 | rcu_read_lock(); |
6499b1b2 VG |
1566 | for_each_cpu(cpu, cpumask_of_node(nid)) { |
1567 | struct rq *rq = cpu_rq(cpu); | |
1568 | ||
1569 | ns->load += cpu_load(rq); | |
8e0e0eda | 1570 | ns->runnable += cpu_runnable(rq); |
6499b1b2 VG |
1571 | ns->util += cpu_util(cpu); |
1572 | ns->nr_running += rq->cfs.h_nr_running; | |
1573 | ns->compute_capacity += capacity_of(cpu); | |
ff7db0bf MG |
1574 | |
1575 | if (find_idle && !rq->nr_running && idle_cpu(cpu)) { | |
1576 | if (READ_ONCE(rq->numa_migrate_on) || | |
1577 | !cpumask_test_cpu(cpu, env->p->cpus_ptr)) | |
1578 | continue; | |
1579 | ||
1580 | if (ns->idle_cpu == -1) | |
1581 | ns->idle_cpu = cpu; | |
1582 | ||
1583 | idle_core = numa_idle_core(idle_core, cpu); | |
1584 | } | |
6499b1b2 | 1585 | } |
0621df31 | 1586 | rcu_read_unlock(); |
6499b1b2 VG |
1587 | |
1588 | ns->weight = cpumask_weight(cpumask_of_node(nid)); | |
1589 | ||
1590 | ns->node_type = numa_classify(env->imbalance_pct, ns); | |
ff7db0bf MG |
1591 | |
1592 | if (idle_core >= 0) | |
1593 | ns->idle_cpu = idle_core; | |
6499b1b2 VG |
1594 | } |
1595 | ||
fb13c7ee MG |
1596 | static void task_numa_assign(struct task_numa_env *env, |
1597 | struct task_struct *p, long imp) | |
1598 | { | |
a4739eca SD |
1599 | struct rq *rq = cpu_rq(env->dst_cpu); |
1600 | ||
5fb52dd9 MG |
1601 | /* Check if run-queue part of active NUMA balance. */ |
1602 | if (env->best_cpu != env->dst_cpu && xchg(&rq->numa_migrate_on, 1)) { | |
1603 | int cpu; | |
1604 | int start = env->dst_cpu; | |
1605 | ||
1606 | /* Find alternative idle CPU. */ | |
1607 | for_each_cpu_wrap(cpu, cpumask_of_node(env->dst_nid), start) { | |
1608 | if (cpu == env->best_cpu || !idle_cpu(cpu) || | |
1609 | !cpumask_test_cpu(cpu, env->p->cpus_ptr)) { | |
1610 | continue; | |
1611 | } | |
1612 | ||
1613 | env->dst_cpu = cpu; | |
1614 | rq = cpu_rq(env->dst_cpu); | |
1615 | if (!xchg(&rq->numa_migrate_on, 1)) | |
1616 | goto assign; | |
1617 | } | |
1618 | ||
1619 | /* Failed to find an alternative idle CPU */ | |
a4739eca | 1620 | return; |
5fb52dd9 | 1621 | } |
a4739eca | 1622 | |
5fb52dd9 | 1623 | assign: |
a4739eca SD |
1624 | /* |
1625 | * Clear previous best_cpu/rq numa-migrate flag, since task now | |
1626 | * found a better CPU to move/swap. | |
1627 | */ | |
5fb52dd9 | 1628 | if (env->best_cpu != -1 && env->best_cpu != env->dst_cpu) { |
a4739eca SD |
1629 | rq = cpu_rq(env->best_cpu); |
1630 | WRITE_ONCE(rq->numa_migrate_on, 0); | |
1631 | } | |
1632 | ||
fb13c7ee MG |
1633 | if (env->best_task) |
1634 | put_task_struct(env->best_task); | |
bac78573 ON |
1635 | if (p) |
1636 | get_task_struct(p); | |
fb13c7ee MG |
1637 | |
1638 | env->best_task = p; | |
1639 | env->best_imp = imp; | |
1640 | env->best_cpu = env->dst_cpu; | |
1641 | } | |
1642 | ||
28a21745 | 1643 | static bool load_too_imbalanced(long src_load, long dst_load, |
e63da036 RR |
1644 | struct task_numa_env *env) |
1645 | { | |
e4991b24 RR |
1646 | long imb, old_imb; |
1647 | long orig_src_load, orig_dst_load; | |
28a21745 RR |
1648 | long src_capacity, dst_capacity; |
1649 | ||
1650 | /* | |
1651 | * The load is corrected for the CPU capacity available on each node. | |
1652 | * | |
1653 | * src_load dst_load | |
1654 | * ------------ vs --------- | |
1655 | * src_capacity dst_capacity | |
1656 | */ | |
1657 | src_capacity = env->src_stats.compute_capacity; | |
1658 | dst_capacity = env->dst_stats.compute_capacity; | |
e63da036 | 1659 | |
5f95ba7a | 1660 | imb = abs(dst_load * src_capacity - src_load * dst_capacity); |
e63da036 | 1661 | |
28a21745 | 1662 | orig_src_load = env->src_stats.load; |
e4991b24 | 1663 | orig_dst_load = env->dst_stats.load; |
28a21745 | 1664 | |
5f95ba7a | 1665 | old_imb = abs(orig_dst_load * src_capacity - orig_src_load * dst_capacity); |
e4991b24 RR |
1666 | |
1667 | /* Would this change make things worse? */ | |
1668 | return (imb > old_imb); | |
e63da036 RR |
1669 | } |
1670 | ||
6fd98e77 SD |
1671 | /* |
1672 | * Maximum NUMA importance can be 1998 (2*999); | |
1673 | * SMALLIMP @ 30 would be close to 1998/64. | |
1674 | * Used to deter task migration. | |
1675 | */ | |
1676 | #define SMALLIMP 30 | |
1677 | ||
fb13c7ee MG |
1678 | /* |
1679 | * This checks if the overall compute and NUMA accesses of the system would | |
1680 | * be improved if the source tasks was migrated to the target dst_cpu taking | |
1681 | * into account that it might be best if task running on the dst_cpu should | |
1682 | * be exchanged with the source task | |
1683 | */ | |
a0f03b61 | 1684 | static bool task_numa_compare(struct task_numa_env *env, |
305c1fac | 1685 | long taskimp, long groupimp, bool maymove) |
fb13c7ee | 1686 | { |
cb361d8c | 1687 | struct numa_group *cur_ng, *p_ng = deref_curr_numa_group(env->p); |
fb13c7ee | 1688 | struct rq *dst_rq = cpu_rq(env->dst_cpu); |
cb361d8c | 1689 | long imp = p_ng ? groupimp : taskimp; |
fb13c7ee | 1690 | struct task_struct *cur; |
28a21745 | 1691 | long src_load, dst_load; |
7bd95320 | 1692 | int dist = env->dist; |
cb361d8c JH |
1693 | long moveimp = imp; |
1694 | long load; | |
a0f03b61 | 1695 | bool stopsearch = false; |
fb13c7ee | 1696 | |
a4739eca | 1697 | if (READ_ONCE(dst_rq->numa_migrate_on)) |
a0f03b61 | 1698 | return false; |
a4739eca | 1699 | |
fb13c7ee | 1700 | rcu_read_lock(); |
154abafc | 1701 | cur = rcu_dereference(dst_rq->curr); |
bac78573 | 1702 | if (cur && ((cur->flags & PF_EXITING) || is_idle_task(cur))) |
fb13c7ee MG |
1703 | cur = NULL; |
1704 | ||
7af68335 PZ |
1705 | /* |
1706 | * Because we have preemption enabled we can get migrated around and | |
1707 | * end try selecting ourselves (current == env->p) as a swap candidate. | |
1708 | */ | |
a0f03b61 MG |
1709 | if (cur == env->p) { |
1710 | stopsearch = true; | |
7af68335 | 1711 | goto unlock; |
a0f03b61 | 1712 | } |
7af68335 | 1713 | |
305c1fac | 1714 | if (!cur) { |
6fd98e77 | 1715 | if (maymove && moveimp >= env->best_imp) |
305c1fac SD |
1716 | goto assign; |
1717 | else | |
1718 | goto unlock; | |
1719 | } | |
1720 | ||
88cca72c MG |
1721 | /* Skip this swap candidate if cannot move to the source cpu. */ |
1722 | if (!cpumask_test_cpu(env->src_cpu, cur->cpus_ptr)) | |
1723 | goto unlock; | |
1724 | ||
1725 | /* | |
1726 | * Skip this swap candidate if it is not moving to its preferred | |
1727 | * node and the best task is. | |
1728 | */ | |
1729 | if (env->best_task && | |
1730 | env->best_task->numa_preferred_nid == env->src_nid && | |
1731 | cur->numa_preferred_nid != env->src_nid) { | |
1732 | goto unlock; | |
1733 | } | |
1734 | ||
fb13c7ee MG |
1735 | /* |
1736 | * "imp" is the fault differential for the source task between the | |
1737 | * source and destination node. Calculate the total differential for | |
1738 | * the source task and potential destination task. The more negative | |
305c1fac | 1739 | * the value is, the more remote accesses that would be expected to |
fb13c7ee | 1740 | * be incurred if the tasks were swapped. |
88cca72c | 1741 | * |
305c1fac SD |
1742 | * If dst and source tasks are in the same NUMA group, or not |
1743 | * in any group then look only at task weights. | |
1744 | */ | |
cb361d8c JH |
1745 | cur_ng = rcu_dereference(cur->numa_group); |
1746 | if (cur_ng == p_ng) { | |
305c1fac SD |
1747 | imp = taskimp + task_weight(cur, env->src_nid, dist) - |
1748 | task_weight(cur, env->dst_nid, dist); | |
887c290e | 1749 | /* |
305c1fac SD |
1750 | * Add some hysteresis to prevent swapping the |
1751 | * tasks within a group over tiny differences. | |
887c290e | 1752 | */ |
cb361d8c | 1753 | if (cur_ng) |
305c1fac SD |
1754 | imp -= imp / 16; |
1755 | } else { | |
1756 | /* | |
1757 | * Compare the group weights. If a task is all by itself | |
1758 | * (not part of a group), use the task weight instead. | |
1759 | */ | |
cb361d8c | 1760 | if (cur_ng && p_ng) |
305c1fac SD |
1761 | imp += group_weight(cur, env->src_nid, dist) - |
1762 | group_weight(cur, env->dst_nid, dist); | |
1763 | else | |
1764 | imp += task_weight(cur, env->src_nid, dist) - | |
1765 | task_weight(cur, env->dst_nid, dist); | |
fb13c7ee MG |
1766 | } |
1767 | ||
88cca72c MG |
1768 | /* Discourage picking a task already on its preferred node */ |
1769 | if (cur->numa_preferred_nid == env->dst_nid) | |
1770 | imp -= imp / 16; | |
1771 | ||
1772 | /* | |
1773 | * Encourage picking a task that moves to its preferred node. | |
1774 | * This potentially makes imp larger than it's maximum of | |
1775 | * 1998 (see SMALLIMP and task_weight for why) but in this | |
1776 | * case, it does not matter. | |
1777 | */ | |
1778 | if (cur->numa_preferred_nid == env->src_nid) | |
1779 | imp += imp / 8; | |
1780 | ||
305c1fac | 1781 | if (maymove && moveimp > imp && moveimp > env->best_imp) { |
6fd98e77 | 1782 | imp = moveimp; |
305c1fac | 1783 | cur = NULL; |
fb13c7ee | 1784 | goto assign; |
305c1fac | 1785 | } |
fb13c7ee | 1786 | |
88cca72c MG |
1787 | /* |
1788 | * Prefer swapping with a task moving to its preferred node over a | |
1789 | * task that is not. | |
1790 | */ | |
1791 | if (env->best_task && cur->numa_preferred_nid == env->src_nid && | |
1792 | env->best_task->numa_preferred_nid != env->src_nid) { | |
1793 | goto assign; | |
1794 | } | |
1795 | ||
6fd98e77 SD |
1796 | /* |
1797 | * If the NUMA importance is less than SMALLIMP, | |
1798 | * task migration might only result in ping pong | |
1799 | * of tasks and also hurt performance due to cache | |
1800 | * misses. | |
1801 | */ | |
1802 | if (imp < SMALLIMP || imp <= env->best_imp + SMALLIMP / 2) | |
1803 | goto unlock; | |
1804 | ||
fb13c7ee MG |
1805 | /* |
1806 | * In the overloaded case, try and keep the load balanced. | |
1807 | */ | |
305c1fac SD |
1808 | load = task_h_load(env->p) - task_h_load(cur); |
1809 | if (!load) | |
1810 | goto assign; | |
1811 | ||
e720fff6 PZ |
1812 | dst_load = env->dst_stats.load + load; |
1813 | src_load = env->src_stats.load - load; | |
fb13c7ee | 1814 | |
28a21745 | 1815 | if (load_too_imbalanced(src_load, dst_load, env)) |
fb13c7ee MG |
1816 | goto unlock; |
1817 | ||
305c1fac | 1818 | assign: |
ff7db0bf | 1819 | /* Evaluate an idle CPU for a task numa move. */ |
10e2f1ac | 1820 | if (!cur) { |
ff7db0bf MG |
1821 | int cpu = env->dst_stats.idle_cpu; |
1822 | ||
1823 | /* Nothing cached so current CPU went idle since the search. */ | |
1824 | if (cpu < 0) | |
1825 | cpu = env->dst_cpu; | |
1826 | ||
10e2f1ac | 1827 | /* |
ff7db0bf MG |
1828 | * If the CPU is no longer truly idle and the previous best CPU |
1829 | * is, keep using it. | |
10e2f1ac | 1830 | */ |
ff7db0bf MG |
1831 | if (!idle_cpu(cpu) && env->best_cpu >= 0 && |
1832 | idle_cpu(env->best_cpu)) { | |
1833 | cpu = env->best_cpu; | |
1834 | } | |
1835 | ||
ff7db0bf | 1836 | env->dst_cpu = cpu; |
10e2f1ac | 1837 | } |
ba7e5a27 | 1838 | |
fb13c7ee | 1839 | task_numa_assign(env, cur, imp); |
a0f03b61 MG |
1840 | |
1841 | /* | |
1842 | * If a move to idle is allowed because there is capacity or load | |
1843 | * balance improves then stop the search. While a better swap | |
1844 | * candidate may exist, a search is not free. | |
1845 | */ | |
1846 | if (maymove && !cur && env->best_cpu >= 0 && idle_cpu(env->best_cpu)) | |
1847 | stopsearch = true; | |
1848 | ||
1849 | /* | |
1850 | * If a swap candidate must be identified and the current best task | |
1851 | * moves its preferred node then stop the search. | |
1852 | */ | |
1853 | if (!maymove && env->best_task && | |
1854 | env->best_task->numa_preferred_nid == env->src_nid) { | |
1855 | stopsearch = true; | |
1856 | } | |
fb13c7ee MG |
1857 | unlock: |
1858 | rcu_read_unlock(); | |
a0f03b61 MG |
1859 | |
1860 | return stopsearch; | |
fb13c7ee MG |
1861 | } |
1862 | ||
887c290e RR |
1863 | static void task_numa_find_cpu(struct task_numa_env *env, |
1864 | long taskimp, long groupimp) | |
2c8a50aa | 1865 | { |
305c1fac | 1866 | bool maymove = false; |
2c8a50aa MG |
1867 | int cpu; |
1868 | ||
305c1fac | 1869 | /* |
fb86f5b2 MG |
1870 | * If dst node has spare capacity, then check if there is an |
1871 | * imbalance that would be overruled by the load balancer. | |
305c1fac | 1872 | */ |
fb86f5b2 MG |
1873 | if (env->dst_stats.node_type == node_has_spare) { |
1874 | unsigned int imbalance; | |
1875 | int src_running, dst_running; | |
1876 | ||
1877 | /* | |
1878 | * Would movement cause an imbalance? Note that if src has | |
1879 | * more running tasks that the imbalance is ignored as the | |
1880 | * move improves the imbalance from the perspective of the | |
1881 | * CPU load balancer. | |
1882 | * */ | |
1883 | src_running = env->src_stats.nr_running - 1; | |
1884 | dst_running = env->dst_stats.nr_running + 1; | |
1885 | imbalance = max(0, dst_running - src_running); | |
7d2b5dd0 MG |
1886 | imbalance = adjust_numa_imbalance(imbalance, dst_running, |
1887 | env->dst_stats.weight); | |
fb86f5b2 MG |
1888 | |
1889 | /* Use idle CPU if there is no imbalance */ | |
ff7db0bf | 1890 | if (!imbalance) { |
fb86f5b2 | 1891 | maymove = true; |
ff7db0bf MG |
1892 | if (env->dst_stats.idle_cpu >= 0) { |
1893 | env->dst_cpu = env->dst_stats.idle_cpu; | |
1894 | task_numa_assign(env, NULL, 0); | |
1895 | return; | |
1896 | } | |
1897 | } | |
fb86f5b2 MG |
1898 | } else { |
1899 | long src_load, dst_load, load; | |
1900 | /* | |
1901 | * If the improvement from just moving env->p direction is better | |
1902 | * than swapping tasks around, check if a move is possible. | |
1903 | */ | |
1904 | load = task_h_load(env->p); | |
1905 | dst_load = env->dst_stats.load + load; | |
1906 | src_load = env->src_stats.load - load; | |
1907 | maymove = !load_too_imbalanced(src_load, dst_load, env); | |
1908 | } | |
305c1fac | 1909 | |
2c8a50aa MG |
1910 | for_each_cpu(cpu, cpumask_of_node(env->dst_nid)) { |
1911 | /* Skip this CPU if the source task cannot migrate */ | |
3bd37062 | 1912 | if (!cpumask_test_cpu(cpu, env->p->cpus_ptr)) |
2c8a50aa MG |
1913 | continue; |
1914 | ||
1915 | env->dst_cpu = cpu; | |
a0f03b61 MG |
1916 | if (task_numa_compare(env, taskimp, groupimp, maymove)) |
1917 | break; | |
2c8a50aa MG |
1918 | } |
1919 | } | |
1920 | ||
58d081b5 MG |
1921 | static int task_numa_migrate(struct task_struct *p) |
1922 | { | |
58d081b5 MG |
1923 | struct task_numa_env env = { |
1924 | .p = p, | |
fb13c7ee | 1925 | |
58d081b5 | 1926 | .src_cpu = task_cpu(p), |
b32e86b4 | 1927 | .src_nid = task_node(p), |
fb13c7ee MG |
1928 | |
1929 | .imbalance_pct = 112, | |
1930 | ||
1931 | .best_task = NULL, | |
1932 | .best_imp = 0, | |
4142c3eb | 1933 | .best_cpu = -1, |
58d081b5 | 1934 | }; |
cb361d8c | 1935 | unsigned long taskweight, groupweight; |
58d081b5 | 1936 | struct sched_domain *sd; |
cb361d8c JH |
1937 | long taskimp, groupimp; |
1938 | struct numa_group *ng; | |
a4739eca | 1939 | struct rq *best_rq; |
7bd95320 | 1940 | int nid, ret, dist; |
e6628d5b | 1941 | |
58d081b5 | 1942 | /* |
fb13c7ee MG |
1943 | * Pick the lowest SD_NUMA domain, as that would have the smallest |
1944 | * imbalance and would be the first to start moving tasks about. | |
1945 | * | |
1946 | * And we want to avoid any moving of tasks about, as that would create | |
1947 | * random movement of tasks -- counter the numa conditions we're trying | |
1948 | * to satisfy here. | |
58d081b5 MG |
1949 | */ |
1950 | rcu_read_lock(); | |
fb13c7ee | 1951 | sd = rcu_dereference(per_cpu(sd_numa, env.src_cpu)); |
46a73e8a RR |
1952 | if (sd) |
1953 | env.imbalance_pct = 100 + (sd->imbalance_pct - 100) / 2; | |
e6628d5b MG |
1954 | rcu_read_unlock(); |
1955 | ||
46a73e8a RR |
1956 | /* |
1957 | * Cpusets can break the scheduler domain tree into smaller | |
1958 | * balance domains, some of which do not cross NUMA boundaries. | |
1959 | * Tasks that are "trapped" in such domains cannot be migrated | |
1960 | * elsewhere, so there is no point in (re)trying. | |
1961 | */ | |
1962 | if (unlikely(!sd)) { | |
8cd45eee | 1963 | sched_setnuma(p, task_node(p)); |
46a73e8a RR |
1964 | return -EINVAL; |
1965 | } | |
1966 | ||
2c8a50aa | 1967 | env.dst_nid = p->numa_preferred_nid; |
7bd95320 RR |
1968 | dist = env.dist = node_distance(env.src_nid, env.dst_nid); |
1969 | taskweight = task_weight(p, env.src_nid, dist); | |
1970 | groupweight = group_weight(p, env.src_nid, dist); | |
ff7db0bf | 1971 | update_numa_stats(&env, &env.src_stats, env.src_nid, false); |
7bd95320 RR |
1972 | taskimp = task_weight(p, env.dst_nid, dist) - taskweight; |
1973 | groupimp = group_weight(p, env.dst_nid, dist) - groupweight; | |
ff7db0bf | 1974 | update_numa_stats(&env, &env.dst_stats, env.dst_nid, true); |
58d081b5 | 1975 | |
a43455a1 | 1976 | /* Try to find a spot on the preferred nid. */ |
2d4056fa | 1977 | task_numa_find_cpu(&env, taskimp, groupimp); |
e1dda8a7 | 1978 | |
9de05d48 RR |
1979 | /* |
1980 | * Look at other nodes in these cases: | |
1981 | * - there is no space available on the preferred_nid | |
1982 | * - the task is part of a numa_group that is interleaved across | |
1983 | * multiple NUMA nodes; in order to better consolidate the group, | |
1984 | * we need to check other locations. | |
1985 | */ | |
cb361d8c JH |
1986 | ng = deref_curr_numa_group(p); |
1987 | if (env.best_cpu == -1 || (ng && ng->active_nodes > 1)) { | |
2c8a50aa MG |
1988 | for_each_online_node(nid) { |
1989 | if (nid == env.src_nid || nid == p->numa_preferred_nid) | |
1990 | continue; | |
58d081b5 | 1991 | |
7bd95320 | 1992 | dist = node_distance(env.src_nid, env.dst_nid); |
6c6b1193 RR |
1993 | if (sched_numa_topology_type == NUMA_BACKPLANE && |
1994 | dist != env.dist) { | |
1995 | taskweight = task_weight(p, env.src_nid, dist); | |
1996 | groupweight = group_weight(p, env.src_nid, dist); | |
1997 | } | |
7bd95320 | 1998 | |
83e1d2cd | 1999 | /* Only consider nodes where both task and groups benefit */ |
7bd95320 RR |
2000 | taskimp = task_weight(p, nid, dist) - taskweight; |
2001 | groupimp = group_weight(p, nid, dist) - groupweight; | |
887c290e | 2002 | if (taskimp < 0 && groupimp < 0) |
fb13c7ee MG |
2003 | continue; |
2004 | ||
7bd95320 | 2005 | env.dist = dist; |
2c8a50aa | 2006 | env.dst_nid = nid; |
ff7db0bf | 2007 | update_numa_stats(&env, &env.dst_stats, env.dst_nid, true); |
2d4056fa | 2008 | task_numa_find_cpu(&env, taskimp, groupimp); |
58d081b5 MG |
2009 | } |
2010 | } | |
2011 | ||
68d1b02a RR |
2012 | /* |
2013 | * If the task is part of a workload that spans multiple NUMA nodes, | |
2014 | * and is migrating into one of the workload's active nodes, remember | |
2015 | * this node as the task's preferred numa node, so the workload can | |
2016 | * settle down. | |
2017 | * A task that migrated to a second choice node will be better off | |
2018 | * trying for a better one later. Do not set the preferred node here. | |
2019 | */ | |
cb361d8c | 2020 | if (ng) { |
db015dae RR |
2021 | if (env.best_cpu == -1) |
2022 | nid = env.src_nid; | |
2023 | else | |
8cd45eee | 2024 | nid = cpu_to_node(env.best_cpu); |
db015dae | 2025 | |
8cd45eee SD |
2026 | if (nid != p->numa_preferred_nid) |
2027 | sched_setnuma(p, nid); | |
db015dae RR |
2028 | } |
2029 | ||
2030 | /* No better CPU than the current one was found. */ | |
f22aef4a | 2031 | if (env.best_cpu == -1) { |
b2b2042b | 2032 | trace_sched_stick_numa(p, env.src_cpu, NULL, -1); |
db015dae | 2033 | return -EAGAIN; |
f22aef4a | 2034 | } |
0ec8aa00 | 2035 | |
a4739eca | 2036 | best_rq = cpu_rq(env.best_cpu); |
fb13c7ee | 2037 | if (env.best_task == NULL) { |
286549dc | 2038 | ret = migrate_task_to(p, env.best_cpu); |
a4739eca | 2039 | WRITE_ONCE(best_rq->numa_migrate_on, 0); |
286549dc | 2040 | if (ret != 0) |
b2b2042b | 2041 | trace_sched_stick_numa(p, env.src_cpu, NULL, env.best_cpu); |
fb13c7ee MG |
2042 | return ret; |
2043 | } | |
2044 | ||
0ad4e3df | 2045 | ret = migrate_swap(p, env.best_task, env.best_cpu, env.src_cpu); |
a4739eca | 2046 | WRITE_ONCE(best_rq->numa_migrate_on, 0); |
0ad4e3df | 2047 | |
286549dc | 2048 | if (ret != 0) |
b2b2042b | 2049 | trace_sched_stick_numa(p, env.src_cpu, env.best_task, env.best_cpu); |
fb13c7ee MG |
2050 | put_task_struct(env.best_task); |
2051 | return ret; | |
e6628d5b MG |
2052 | } |
2053 | ||
6b9a7460 MG |
2054 | /* Attempt to migrate a task to a CPU on the preferred node. */ |
2055 | static void numa_migrate_preferred(struct task_struct *p) | |
2056 | { | |
5085e2a3 RR |
2057 | unsigned long interval = HZ; |
2058 | ||
2739d3ee | 2059 | /* This task has no NUMA fault statistics yet */ |
98fa15f3 | 2060 | if (unlikely(p->numa_preferred_nid == NUMA_NO_NODE || !p->numa_faults)) |
6b9a7460 MG |
2061 | return; |
2062 | ||
2739d3ee | 2063 | /* Periodically retry migrating the task to the preferred node */ |
5085e2a3 | 2064 | interval = min(interval, msecs_to_jiffies(p->numa_scan_period) / 16); |
789ba280 | 2065 | p->numa_migrate_retry = jiffies + interval; |
2739d3ee RR |
2066 | |
2067 | /* Success if task is already running on preferred CPU */ | |
de1b301a | 2068 | if (task_node(p) == p->numa_preferred_nid) |
6b9a7460 MG |
2069 | return; |
2070 | ||
2071 | /* Otherwise, try migrate to a CPU on the preferred node */ | |
2739d3ee | 2072 | task_numa_migrate(p); |
6b9a7460 MG |
2073 | } |
2074 | ||
20e07dea | 2075 | /* |
4142c3eb | 2076 | * Find out how many nodes on the workload is actively running on. Do this by |
20e07dea RR |
2077 | * tracking the nodes from which NUMA hinting faults are triggered. This can |
2078 | * be different from the set of nodes where the workload's memory is currently | |
2079 | * located. | |
20e07dea | 2080 | */ |
4142c3eb | 2081 | static void numa_group_count_active_nodes(struct numa_group *numa_group) |
20e07dea RR |
2082 | { |
2083 | unsigned long faults, max_faults = 0; | |
4142c3eb | 2084 | int nid, active_nodes = 0; |
20e07dea RR |
2085 | |
2086 | for_each_online_node(nid) { | |
2087 | faults = group_faults_cpu(numa_group, nid); | |
2088 | if (faults > max_faults) | |
2089 | max_faults = faults; | |
2090 | } | |
2091 | ||
2092 | for_each_online_node(nid) { | |
2093 | faults = group_faults_cpu(numa_group, nid); | |
4142c3eb RR |
2094 | if (faults * ACTIVE_NODE_FRACTION > max_faults) |
2095 | active_nodes++; | |
20e07dea | 2096 | } |
4142c3eb RR |
2097 | |
2098 | numa_group->max_faults_cpu = max_faults; | |
2099 | numa_group->active_nodes = active_nodes; | |
20e07dea RR |
2100 | } |
2101 | ||
04bb2f94 RR |
2102 | /* |
2103 | * When adapting the scan rate, the period is divided into NUMA_PERIOD_SLOTS | |
2104 | * increments. The more local the fault statistics are, the higher the scan | |
a22b4b01 RR |
2105 | * period will be for the next scan window. If local/(local+remote) ratio is |
2106 | * below NUMA_PERIOD_THRESHOLD (where range of ratio is 1..NUMA_PERIOD_SLOTS) | |
2107 | * the scan period will decrease. Aim for 70% local accesses. | |
04bb2f94 RR |
2108 | */ |
2109 | #define NUMA_PERIOD_SLOTS 10 | |
a22b4b01 | 2110 | #define NUMA_PERIOD_THRESHOLD 7 |
04bb2f94 RR |
2111 | |
2112 | /* | |
2113 | * Increase the scan period (slow down scanning) if the majority of | |
2114 | * our memory is already on our local node, or if the majority of | |
2115 | * the page accesses are shared with other processes. | |
2116 | * Otherwise, decrease the scan period. | |
2117 | */ | |
2118 | static void update_task_scan_period(struct task_struct *p, | |
2119 | unsigned long shared, unsigned long private) | |
2120 | { | |
2121 | unsigned int period_slot; | |
37ec97de | 2122 | int lr_ratio, ps_ratio; |
04bb2f94 RR |
2123 | int diff; |
2124 | ||
2125 | unsigned long remote = p->numa_faults_locality[0]; | |
2126 | unsigned long local = p->numa_faults_locality[1]; | |
2127 | ||
2128 | /* | |
2129 | * If there were no record hinting faults then either the task is | |
2130 | * completely idle or all activity is areas that are not of interest | |
074c2381 MG |
2131 | * to automatic numa balancing. Related to that, if there were failed |
2132 | * migration then it implies we are migrating too quickly or the local | |
2133 | * node is overloaded. In either case, scan slower | |
04bb2f94 | 2134 | */ |
074c2381 | 2135 | if (local + shared == 0 || p->numa_faults_locality[2]) { |
04bb2f94 RR |
2136 | p->numa_scan_period = min(p->numa_scan_period_max, |
2137 | p->numa_scan_period << 1); | |
2138 | ||
2139 | p->mm->numa_next_scan = jiffies + | |
2140 | msecs_to_jiffies(p->numa_scan_period); | |
2141 | ||
2142 | return; | |
2143 | } | |
2144 | ||
2145 | /* | |
2146 | * Prepare to scale scan period relative to the current period. | |
2147 | * == NUMA_PERIOD_THRESHOLD scan period stays the same | |
2148 | * < NUMA_PERIOD_THRESHOLD scan period decreases (scan faster) | |
2149 | * >= NUMA_PERIOD_THRESHOLD scan period increases (scan slower) | |
2150 | */ | |
2151 | period_slot = DIV_ROUND_UP(p->numa_scan_period, NUMA_PERIOD_SLOTS); | |
37ec97de RR |
2152 | lr_ratio = (local * NUMA_PERIOD_SLOTS) / (local + remote); |
2153 | ps_ratio = (private * NUMA_PERIOD_SLOTS) / (private + shared); | |
2154 | ||
2155 | if (ps_ratio >= NUMA_PERIOD_THRESHOLD) { | |
2156 | /* | |
2157 | * Most memory accesses are local. There is no need to | |
2158 | * do fast NUMA scanning, since memory is already local. | |
2159 | */ | |
2160 | int slot = ps_ratio - NUMA_PERIOD_THRESHOLD; | |
2161 | if (!slot) | |
2162 | slot = 1; | |
2163 | diff = slot * period_slot; | |
2164 | } else if (lr_ratio >= NUMA_PERIOD_THRESHOLD) { | |
2165 | /* | |
2166 | * Most memory accesses are shared with other tasks. | |
2167 | * There is no point in continuing fast NUMA scanning, | |
2168 | * since other tasks may just move the memory elsewhere. | |
2169 | */ | |
2170 | int slot = lr_ratio - NUMA_PERIOD_THRESHOLD; | |
04bb2f94 RR |
2171 | if (!slot) |
2172 | slot = 1; | |
2173 | diff = slot * period_slot; | |
2174 | } else { | |
04bb2f94 | 2175 | /* |
37ec97de RR |
2176 | * Private memory faults exceed (SLOTS-THRESHOLD)/SLOTS, |
2177 | * yet they are not on the local NUMA node. Speed up | |
2178 | * NUMA scanning to get the memory moved over. | |
04bb2f94 | 2179 | */ |
37ec97de RR |
2180 | int ratio = max(lr_ratio, ps_ratio); |
2181 | diff = -(NUMA_PERIOD_THRESHOLD - ratio) * period_slot; | |
04bb2f94 RR |
2182 | } |
2183 | ||
2184 | p->numa_scan_period = clamp(p->numa_scan_period + diff, | |
2185 | task_scan_min(p), task_scan_max(p)); | |
2186 | memset(p->numa_faults_locality, 0, sizeof(p->numa_faults_locality)); | |
2187 | } | |
2188 | ||
7e2703e6 RR |
2189 | /* |
2190 | * Get the fraction of time the task has been running since the last | |
2191 | * NUMA placement cycle. The scheduler keeps similar statistics, but | |
2192 | * decays those on a 32ms period, which is orders of magnitude off | |
2193 | * from the dozens-of-seconds NUMA balancing period. Use the scheduler | |
2194 | * stats only if the task is so new there are no NUMA statistics yet. | |
2195 | */ | |
2196 | static u64 numa_get_avg_runtime(struct task_struct *p, u64 *period) | |
2197 | { | |
2198 | u64 runtime, delta, now; | |
2199 | /* Use the start of this time slice to avoid calculations. */ | |
2200 | now = p->se.exec_start; | |
2201 | runtime = p->se.sum_exec_runtime; | |
2202 | ||
2203 | if (p->last_task_numa_placement) { | |
2204 | delta = runtime - p->last_sum_exec_runtime; | |
2205 | *period = now - p->last_task_numa_placement; | |
a860fa7b XX |
2206 | |
2207 | /* Avoid time going backwards, prevent potential divide error: */ | |
2208 | if (unlikely((s64)*period < 0)) | |
2209 | *period = 0; | |
7e2703e6 | 2210 | } else { |
c7b50216 | 2211 | delta = p->se.avg.load_sum; |
9d89c257 | 2212 | *period = LOAD_AVG_MAX; |
7e2703e6 RR |
2213 | } |
2214 | ||
2215 | p->last_sum_exec_runtime = runtime; | |
2216 | p->last_task_numa_placement = now; | |
2217 | ||
2218 | return delta; | |
2219 | } | |
2220 | ||
54009416 RR |
2221 | /* |
2222 | * Determine the preferred nid for a task in a numa_group. This needs to | |
2223 | * be done in a way that produces consistent results with group_weight, | |
2224 | * otherwise workloads might not converge. | |
2225 | */ | |
2226 | static int preferred_group_nid(struct task_struct *p, int nid) | |
2227 | { | |
2228 | nodemask_t nodes; | |
2229 | int dist; | |
2230 | ||
2231 | /* Direct connections between all NUMA nodes. */ | |
2232 | if (sched_numa_topology_type == NUMA_DIRECT) | |
2233 | return nid; | |
2234 | ||
2235 | /* | |
2236 | * On a system with glueless mesh NUMA topology, group_weight | |
2237 | * scores nodes according to the number of NUMA hinting faults on | |
2238 | * both the node itself, and on nearby nodes. | |
2239 | */ | |
2240 | if (sched_numa_topology_type == NUMA_GLUELESS_MESH) { | |
2241 | unsigned long score, max_score = 0; | |
2242 | int node, max_node = nid; | |
2243 | ||
2244 | dist = sched_max_numa_distance; | |
2245 | ||
2246 | for_each_online_node(node) { | |
2247 | score = group_weight(p, node, dist); | |
2248 | if (score > max_score) { | |
2249 | max_score = score; | |
2250 | max_node = node; | |
2251 | } | |
2252 | } | |
2253 | return max_node; | |
2254 | } | |
2255 | ||
2256 | /* | |
2257 | * Finding the preferred nid in a system with NUMA backplane | |
2258 | * interconnect topology is more involved. The goal is to locate | |
2259 | * tasks from numa_groups near each other in the system, and | |
2260 | * untangle workloads from different sides of the system. This requires | |
2261 | * searching down the hierarchy of node groups, recursively searching | |
2262 | * inside the highest scoring group of nodes. The nodemask tricks | |
2263 | * keep the complexity of the search down. | |
2264 | */ | |
2265 | nodes = node_online_map; | |
2266 | for (dist = sched_max_numa_distance; dist > LOCAL_DISTANCE; dist--) { | |
2267 | unsigned long max_faults = 0; | |
81907478 | 2268 | nodemask_t max_group = NODE_MASK_NONE; |
54009416 RR |
2269 | int a, b; |
2270 | ||
2271 | /* Are there nodes at this distance from each other? */ | |
2272 | if (!find_numa_distance(dist)) | |
2273 | continue; | |
2274 | ||
2275 | for_each_node_mask(a, nodes) { | |
2276 | unsigned long faults = 0; | |
2277 | nodemask_t this_group; | |
2278 | nodes_clear(this_group); | |
2279 | ||
2280 | /* Sum group's NUMA faults; includes a==b case. */ | |
2281 | for_each_node_mask(b, nodes) { | |
2282 | if (node_distance(a, b) < dist) { | |
2283 | faults += group_faults(p, b); | |
2284 | node_set(b, this_group); | |
2285 | node_clear(b, nodes); | |
2286 | } | |
2287 | } | |
2288 | ||
2289 | /* Remember the top group. */ | |
2290 | if (faults > max_faults) { | |
2291 | max_faults = faults; | |
2292 | max_group = this_group; | |
2293 | /* | |
2294 | * subtle: at the smallest distance there is | |
2295 | * just one node left in each "group", the | |
2296 | * winner is the preferred nid. | |
2297 | */ | |
2298 | nid = a; | |
2299 | } | |
2300 | } | |
2301 | /* Next round, evaluate the nodes within max_group. */ | |
890a5409 JB |
2302 | if (!max_faults) |
2303 | break; | |
54009416 RR |
2304 | nodes = max_group; |
2305 | } | |
2306 | return nid; | |
2307 | } | |
2308 | ||
cbee9f88 PZ |
2309 | static void task_numa_placement(struct task_struct *p) |
2310 | { | |
98fa15f3 | 2311 | int seq, nid, max_nid = NUMA_NO_NODE; |
f03bb676 | 2312 | unsigned long max_faults = 0; |
04bb2f94 | 2313 | unsigned long fault_types[2] = { 0, 0 }; |
7e2703e6 RR |
2314 | unsigned long total_faults; |
2315 | u64 runtime, period; | |
7dbd13ed | 2316 | spinlock_t *group_lock = NULL; |
cb361d8c | 2317 | struct numa_group *ng; |
cbee9f88 | 2318 | |
7e5a2c17 JL |
2319 | /* |
2320 | * The p->mm->numa_scan_seq field gets updated without | |
2321 | * exclusive access. Use READ_ONCE() here to ensure | |
2322 | * that the field is read in a single access: | |
2323 | */ | |
316c1608 | 2324 | seq = READ_ONCE(p->mm->numa_scan_seq); |
cbee9f88 PZ |
2325 | if (p->numa_scan_seq == seq) |
2326 | return; | |
2327 | p->numa_scan_seq = seq; | |
598f0ec0 | 2328 | p->numa_scan_period_max = task_scan_max(p); |
cbee9f88 | 2329 | |
7e2703e6 RR |
2330 | total_faults = p->numa_faults_locality[0] + |
2331 | p->numa_faults_locality[1]; | |
2332 | runtime = numa_get_avg_runtime(p, &period); | |
2333 | ||
7dbd13ed | 2334 | /* If the task is part of a group prevent parallel updates to group stats */ |
cb361d8c JH |
2335 | ng = deref_curr_numa_group(p); |
2336 | if (ng) { | |
2337 | group_lock = &ng->lock; | |
60e69eed | 2338 | spin_lock_irq(group_lock); |
7dbd13ed MG |
2339 | } |
2340 | ||
688b7585 MG |
2341 | /* Find the node with the highest number of faults */ |
2342 | for_each_online_node(nid) { | |
44dba3d5 IM |
2343 | /* Keep track of the offsets in numa_faults array */ |
2344 | int mem_idx, membuf_idx, cpu_idx, cpubuf_idx; | |
83e1d2cd | 2345 | unsigned long faults = 0, group_faults = 0; |
44dba3d5 | 2346 | int priv; |
745d6147 | 2347 | |
be1e4e76 | 2348 | for (priv = 0; priv < NR_NUMA_HINT_FAULT_TYPES; priv++) { |
7e2703e6 | 2349 | long diff, f_diff, f_weight; |
8c8a743c | 2350 | |
44dba3d5 IM |
2351 | mem_idx = task_faults_idx(NUMA_MEM, nid, priv); |
2352 | membuf_idx = task_faults_idx(NUMA_MEMBUF, nid, priv); | |
2353 | cpu_idx = task_faults_idx(NUMA_CPU, nid, priv); | |
2354 | cpubuf_idx = task_faults_idx(NUMA_CPUBUF, nid, priv); | |
745d6147 | 2355 | |
ac8e895b | 2356 | /* Decay existing window, copy faults since last scan */ |
44dba3d5 IM |
2357 | diff = p->numa_faults[membuf_idx] - p->numa_faults[mem_idx] / 2; |
2358 | fault_types[priv] += p->numa_faults[membuf_idx]; | |
2359 | p->numa_faults[membuf_idx] = 0; | |
fb13c7ee | 2360 | |
7e2703e6 RR |
2361 | /* |
2362 | * Normalize the faults_from, so all tasks in a group | |
2363 | * count according to CPU use, instead of by the raw | |
2364 | * number of faults. Tasks with little runtime have | |
2365 | * little over-all impact on throughput, and thus their | |
2366 | * faults are less important. | |
2367 | */ | |
2368 | f_weight = div64_u64(runtime << 16, period + 1); | |
44dba3d5 | 2369 | f_weight = (f_weight * p->numa_faults[cpubuf_idx]) / |
7e2703e6 | 2370 | (total_faults + 1); |
44dba3d5 IM |
2371 | f_diff = f_weight - p->numa_faults[cpu_idx] / 2; |
2372 | p->numa_faults[cpubuf_idx] = 0; | |
50ec8a40 | 2373 | |
44dba3d5 IM |
2374 | p->numa_faults[mem_idx] += diff; |
2375 | p->numa_faults[cpu_idx] += f_diff; | |
2376 | faults += p->numa_faults[mem_idx]; | |
83e1d2cd | 2377 | p->total_numa_faults += diff; |
cb361d8c | 2378 | if (ng) { |
44dba3d5 IM |
2379 | /* |
2380 | * safe because we can only change our own group | |
2381 | * | |
2382 | * mem_idx represents the offset for a given | |
2383 | * nid and priv in a specific region because it | |
2384 | * is at the beginning of the numa_faults array. | |
2385 | */ | |
cb361d8c JH |
2386 | ng->faults[mem_idx] += diff; |
2387 | ng->faults_cpu[mem_idx] += f_diff; | |
2388 | ng->total_faults += diff; | |
2389 | group_faults += ng->faults[mem_idx]; | |
8c8a743c | 2390 | } |
ac8e895b MG |
2391 | } |
2392 | ||
cb361d8c | 2393 | if (!ng) { |
f03bb676 SD |
2394 | if (faults > max_faults) { |
2395 | max_faults = faults; | |
2396 | max_nid = nid; | |
2397 | } | |
2398 | } else if (group_faults > max_faults) { | |
2399 | max_faults = group_faults; | |
688b7585 MG |
2400 | max_nid = nid; |
2401 | } | |
83e1d2cd MG |
2402 | } |
2403 | ||
cb361d8c JH |
2404 | if (ng) { |
2405 | numa_group_count_active_nodes(ng); | |
60e69eed | 2406 | spin_unlock_irq(group_lock); |
f03bb676 | 2407 | max_nid = preferred_group_nid(p, max_nid); |
688b7585 MG |
2408 | } |
2409 | ||
bb97fc31 RR |
2410 | if (max_faults) { |
2411 | /* Set the new preferred node */ | |
2412 | if (max_nid != p->numa_preferred_nid) | |
2413 | sched_setnuma(p, max_nid); | |
3a7053b3 | 2414 | } |
30619c89 SD |
2415 | |
2416 | update_task_scan_period(p, fault_types[0], fault_types[1]); | |
cbee9f88 PZ |
2417 | } |
2418 | ||
8c8a743c PZ |
2419 | static inline int get_numa_group(struct numa_group *grp) |
2420 | { | |
c45a7795 | 2421 | return refcount_inc_not_zero(&grp->refcount); |
8c8a743c PZ |
2422 | } |
2423 | ||
2424 | static inline void put_numa_group(struct numa_group *grp) | |
2425 | { | |
c45a7795 | 2426 | if (refcount_dec_and_test(&grp->refcount)) |
8c8a743c PZ |
2427 | kfree_rcu(grp, rcu); |
2428 | } | |
2429 | ||
3e6a9418 MG |
2430 | static void task_numa_group(struct task_struct *p, int cpupid, int flags, |
2431 | int *priv) | |
8c8a743c PZ |
2432 | { |
2433 | struct numa_group *grp, *my_grp; | |
2434 | struct task_struct *tsk; | |
2435 | bool join = false; | |
2436 | int cpu = cpupid_to_cpu(cpupid); | |
2437 | int i; | |
2438 | ||
cb361d8c | 2439 | if (unlikely(!deref_curr_numa_group(p))) { |
8c8a743c | 2440 | unsigned int size = sizeof(struct numa_group) + |
50ec8a40 | 2441 | 4*nr_node_ids*sizeof(unsigned long); |
8c8a743c PZ |
2442 | |
2443 | grp = kzalloc(size, GFP_KERNEL | __GFP_NOWARN); | |
2444 | if (!grp) | |
2445 | return; | |
2446 | ||
c45a7795 | 2447 | refcount_set(&grp->refcount, 1); |
4142c3eb RR |
2448 | grp->active_nodes = 1; |
2449 | grp->max_faults_cpu = 0; | |
8c8a743c | 2450 | spin_lock_init(&grp->lock); |
e29cf08b | 2451 | grp->gid = p->pid; |
50ec8a40 | 2452 | /* Second half of the array tracks nids where faults happen */ |
be1e4e76 RR |
2453 | grp->faults_cpu = grp->faults + NR_NUMA_HINT_FAULT_TYPES * |
2454 | nr_node_ids; | |
8c8a743c | 2455 | |
be1e4e76 | 2456 | for (i = 0; i < NR_NUMA_HINT_FAULT_STATS * nr_node_ids; i++) |
44dba3d5 | 2457 | grp->faults[i] = p->numa_faults[i]; |
8c8a743c | 2458 | |
989348b5 | 2459 | grp->total_faults = p->total_numa_faults; |
83e1d2cd | 2460 | |
8c8a743c PZ |
2461 | grp->nr_tasks++; |
2462 | rcu_assign_pointer(p->numa_group, grp); | |
2463 | } | |
2464 | ||
2465 | rcu_read_lock(); | |
316c1608 | 2466 | tsk = READ_ONCE(cpu_rq(cpu)->curr); |
8c8a743c PZ |
2467 | |
2468 | if (!cpupid_match_pid(tsk, cpupid)) | |
3354781a | 2469 | goto no_join; |
8c8a743c PZ |
2470 | |
2471 | grp = rcu_dereference(tsk->numa_group); | |
2472 | if (!grp) | |
3354781a | 2473 | goto no_join; |
8c8a743c | 2474 | |
cb361d8c | 2475 | my_grp = deref_curr_numa_group(p); |
8c8a743c | 2476 | if (grp == my_grp) |
3354781a | 2477 | goto no_join; |
8c8a743c PZ |
2478 | |
2479 | /* | |
2480 | * Only join the other group if its bigger; if we're the bigger group, | |
2481 | * the other task will join us. | |
2482 | */ | |
2483 | if (my_grp->nr_tasks > grp->nr_tasks) | |
3354781a | 2484 | goto no_join; |
8c8a743c PZ |
2485 | |
2486 | /* | |
2487 | * Tie-break on the grp address. | |
2488 | */ | |
2489 | if (my_grp->nr_tasks == grp->nr_tasks && my_grp > grp) | |
3354781a | 2490 | goto no_join; |
8c8a743c | 2491 | |
dabe1d99 RR |
2492 | /* Always join threads in the same process. */ |
2493 | if (tsk->mm == current->mm) | |
2494 | join = true; | |
2495 | ||
2496 | /* Simple filter to avoid false positives due to PID collisions */ | |
2497 | if (flags & TNF_SHARED) | |
2498 | join = true; | |
8c8a743c | 2499 | |
3e6a9418 MG |
2500 | /* Update priv based on whether false sharing was detected */ |
2501 | *priv = !join; | |
2502 | ||
dabe1d99 | 2503 | if (join && !get_numa_group(grp)) |
3354781a | 2504 | goto no_join; |
8c8a743c | 2505 | |
8c8a743c PZ |
2506 | rcu_read_unlock(); |
2507 | ||
2508 | if (!join) | |
2509 | return; | |
2510 | ||
60e69eed MG |
2511 | BUG_ON(irqs_disabled()); |
2512 | double_lock_irq(&my_grp->lock, &grp->lock); | |
989348b5 | 2513 | |
be1e4e76 | 2514 | for (i = 0; i < NR_NUMA_HINT_FAULT_STATS * nr_node_ids; i++) { |
44dba3d5 IM |
2515 | my_grp->faults[i] -= p->numa_faults[i]; |
2516 | grp->faults[i] += p->numa_faults[i]; | |
8c8a743c | 2517 | } |
989348b5 MG |
2518 | my_grp->total_faults -= p->total_numa_faults; |
2519 | grp->total_faults += p->total_numa_faults; | |
8c8a743c | 2520 | |
8c8a743c PZ |
2521 | my_grp->nr_tasks--; |
2522 | grp->nr_tasks++; | |
2523 | ||
2524 | spin_unlock(&my_grp->lock); | |
60e69eed | 2525 | spin_unlock_irq(&grp->lock); |
8c8a743c PZ |
2526 | |
2527 | rcu_assign_pointer(p->numa_group, grp); | |
2528 | ||
2529 | put_numa_group(my_grp); | |
3354781a PZ |
2530 | return; |
2531 | ||
2532 | no_join: | |
2533 | rcu_read_unlock(); | |
2534 | return; | |
8c8a743c PZ |
2535 | } |
2536 | ||
16d51a59 | 2537 | /* |
3b03706f | 2538 | * Get rid of NUMA statistics associated with a task (either current or dead). |
16d51a59 JH |
2539 | * If @final is set, the task is dead and has reached refcount zero, so we can |
2540 | * safely free all relevant data structures. Otherwise, there might be | |
2541 | * concurrent reads from places like load balancing and procfs, and we should | |
2542 | * reset the data back to default state without freeing ->numa_faults. | |
2543 | */ | |
2544 | void task_numa_free(struct task_struct *p, bool final) | |
8c8a743c | 2545 | { |
cb361d8c JH |
2546 | /* safe: p either is current or is being freed by current */ |
2547 | struct numa_group *grp = rcu_dereference_raw(p->numa_group); | |
16d51a59 | 2548 | unsigned long *numa_faults = p->numa_faults; |
e9dd685c SR |
2549 | unsigned long flags; |
2550 | int i; | |
8c8a743c | 2551 | |
16d51a59 JH |
2552 | if (!numa_faults) |
2553 | return; | |
2554 | ||
8c8a743c | 2555 | if (grp) { |
e9dd685c | 2556 | spin_lock_irqsave(&grp->lock, flags); |
be1e4e76 | 2557 | for (i = 0; i < NR_NUMA_HINT_FAULT_STATS * nr_node_ids; i++) |
44dba3d5 | 2558 | grp->faults[i] -= p->numa_faults[i]; |
989348b5 | 2559 | grp->total_faults -= p->total_numa_faults; |
83e1d2cd | 2560 | |
8c8a743c | 2561 | grp->nr_tasks--; |
e9dd685c | 2562 | spin_unlock_irqrestore(&grp->lock, flags); |
35b123e2 | 2563 | RCU_INIT_POINTER(p->numa_group, NULL); |
8c8a743c PZ |
2564 | put_numa_group(grp); |
2565 | } | |
2566 | ||
16d51a59 JH |
2567 | if (final) { |
2568 | p->numa_faults = NULL; | |
2569 | kfree(numa_faults); | |
2570 | } else { | |
2571 | p->total_numa_faults = 0; | |
2572 | for (i = 0; i < NR_NUMA_HINT_FAULT_STATS * nr_node_ids; i++) | |
2573 | numa_faults[i] = 0; | |
2574 | } | |
8c8a743c PZ |
2575 | } |
2576 | ||
cbee9f88 PZ |
2577 | /* |
2578 | * Got a PROT_NONE fault for a page on @node. | |
2579 | */ | |
58b46da3 | 2580 | void task_numa_fault(int last_cpupid, int mem_node, int pages, int flags) |
cbee9f88 PZ |
2581 | { |
2582 | struct task_struct *p = current; | |
6688cc05 | 2583 | bool migrated = flags & TNF_MIGRATED; |
58b46da3 | 2584 | int cpu_node = task_node(current); |
792568ec | 2585 | int local = !!(flags & TNF_FAULT_LOCAL); |
4142c3eb | 2586 | struct numa_group *ng; |
ac8e895b | 2587 | int priv; |
cbee9f88 | 2588 | |
2a595721 | 2589 | if (!static_branch_likely(&sched_numa_balancing)) |
1a687c2e MG |
2590 | return; |
2591 | ||
9ff1d9ff MG |
2592 | /* for example, ksmd faulting in a user's mm */ |
2593 | if (!p->mm) | |
2594 | return; | |
2595 | ||
f809ca9a | 2596 | /* Allocate buffer to track faults on a per-node basis */ |
44dba3d5 IM |
2597 | if (unlikely(!p->numa_faults)) { |
2598 | int size = sizeof(*p->numa_faults) * | |
be1e4e76 | 2599 | NR_NUMA_HINT_FAULT_BUCKETS * nr_node_ids; |
f809ca9a | 2600 | |
44dba3d5 IM |
2601 | p->numa_faults = kzalloc(size, GFP_KERNEL|__GFP_NOWARN); |
2602 | if (!p->numa_faults) | |
f809ca9a | 2603 | return; |
745d6147 | 2604 | |
83e1d2cd | 2605 | p->total_numa_faults = 0; |
04bb2f94 | 2606 | memset(p->numa_faults_locality, 0, sizeof(p->numa_faults_locality)); |
f809ca9a | 2607 | } |
cbee9f88 | 2608 | |
8c8a743c PZ |
2609 | /* |
2610 | * First accesses are treated as private, otherwise consider accesses | |
2611 | * to be private if the accessing pid has not changed | |
2612 | */ | |
2613 | if (unlikely(last_cpupid == (-1 & LAST_CPUPID_MASK))) { | |
2614 | priv = 1; | |
2615 | } else { | |
2616 | priv = cpupid_match_pid(p, last_cpupid); | |
6688cc05 | 2617 | if (!priv && !(flags & TNF_NO_GROUP)) |
3e6a9418 | 2618 | task_numa_group(p, last_cpupid, flags, &priv); |
8c8a743c PZ |
2619 | } |
2620 | ||
792568ec RR |
2621 | /* |
2622 | * If a workload spans multiple NUMA nodes, a shared fault that | |
2623 | * occurs wholly within the set of nodes that the workload is | |
2624 | * actively using should be counted as local. This allows the | |
2625 | * scan rate to slow down when a workload has settled down. | |
2626 | */ | |
cb361d8c | 2627 | ng = deref_curr_numa_group(p); |
4142c3eb RR |
2628 | if (!priv && !local && ng && ng->active_nodes > 1 && |
2629 | numa_is_active_node(cpu_node, ng) && | |
2630 | numa_is_active_node(mem_node, ng)) | |
792568ec RR |
2631 | local = 1; |
2632 | ||
2739d3ee | 2633 | /* |
e1ff516a YW |
2634 | * Retry to migrate task to preferred node periodically, in case it |
2635 | * previously failed, or the scheduler moved us. | |
2739d3ee | 2636 | */ |
b6a60cf3 SD |
2637 | if (time_after(jiffies, p->numa_migrate_retry)) { |
2638 | task_numa_placement(p); | |
6b9a7460 | 2639 | numa_migrate_preferred(p); |
b6a60cf3 | 2640 | } |
6b9a7460 | 2641 | |
b32e86b4 IM |
2642 | if (migrated) |
2643 | p->numa_pages_migrated += pages; | |
074c2381 MG |
2644 | if (flags & TNF_MIGRATE_FAIL) |
2645 | p->numa_faults_locality[2] += pages; | |
b32e86b4 | 2646 | |
44dba3d5 IM |
2647 | p->numa_faults[task_faults_idx(NUMA_MEMBUF, mem_node, priv)] += pages; |
2648 | p->numa_faults[task_faults_idx(NUMA_CPUBUF, cpu_node, priv)] += pages; | |
792568ec | 2649 | p->numa_faults_locality[local] += pages; |
cbee9f88 PZ |
2650 | } |
2651 | ||
6e5fb223 PZ |
2652 | static void reset_ptenuma_scan(struct task_struct *p) |
2653 | { | |
7e5a2c17 JL |
2654 | /* |
2655 | * We only did a read acquisition of the mmap sem, so | |
2656 | * p->mm->numa_scan_seq is written to without exclusive access | |
2657 | * and the update is not guaranteed to be atomic. That's not | |
2658 | * much of an issue though, since this is just used for | |
2659 | * statistical sampling. Use READ_ONCE/WRITE_ONCE, which are not | |
2660 | * expensive, to avoid any form of compiler optimizations: | |
2661 | */ | |
316c1608 | 2662 | WRITE_ONCE(p->mm->numa_scan_seq, READ_ONCE(p->mm->numa_scan_seq) + 1); |
6e5fb223 PZ |
2663 | p->mm->numa_scan_offset = 0; |
2664 | } | |
2665 | ||
cbee9f88 PZ |
2666 | /* |
2667 | * The expensive part of numa migration is done from task_work context. | |
2668 | * Triggered from task_tick_numa(). | |
2669 | */ | |
9434f9f5 | 2670 | static void task_numa_work(struct callback_head *work) |
cbee9f88 PZ |
2671 | { |
2672 | unsigned long migrate, next_scan, now = jiffies; | |
2673 | struct task_struct *p = current; | |
2674 | struct mm_struct *mm = p->mm; | |
51170840 | 2675 | u64 runtime = p->se.sum_exec_runtime; |
6e5fb223 | 2676 | struct vm_area_struct *vma; |
9f40604c | 2677 | unsigned long start, end; |
598f0ec0 | 2678 | unsigned long nr_pte_updates = 0; |
4620f8c1 | 2679 | long pages, virtpages; |
cbee9f88 | 2680 | |
9148a3a1 | 2681 | SCHED_WARN_ON(p != container_of(work, struct task_struct, numa_work)); |
cbee9f88 | 2682 | |
b34920d4 | 2683 | work->next = work; |
cbee9f88 PZ |
2684 | /* |
2685 | * Who cares about NUMA placement when they're dying. | |
2686 | * | |
2687 | * NOTE: make sure not to dereference p->mm before this check, | |
2688 | * exit_task_work() happens _after_ exit_mm() so we could be called | |
2689 | * without p->mm even though we still had it when we enqueued this | |
2690 | * work. | |
2691 | */ | |
2692 | if (p->flags & PF_EXITING) | |
2693 | return; | |
2694 | ||
930aa174 | 2695 | if (!mm->numa_next_scan) { |
7e8d16b6 MG |
2696 | mm->numa_next_scan = now + |
2697 | msecs_to_jiffies(sysctl_numa_balancing_scan_delay); | |
b8593bfd MG |
2698 | } |
2699 | ||
cbee9f88 PZ |
2700 | /* |
2701 | * Enforce maximal scan/migration frequency.. | |
2702 | */ | |
2703 | migrate = mm->numa_next_scan; | |
2704 | if (time_before(now, migrate)) | |
2705 | return; | |
2706 | ||
598f0ec0 MG |
2707 | if (p->numa_scan_period == 0) { |
2708 | p->numa_scan_period_max = task_scan_max(p); | |
b5dd77c8 | 2709 | p->numa_scan_period = task_scan_start(p); |
598f0ec0 | 2710 | } |
cbee9f88 | 2711 | |
fb003b80 | 2712 | next_scan = now + msecs_to_jiffies(p->numa_scan_period); |
cbee9f88 PZ |
2713 | if (cmpxchg(&mm->numa_next_scan, migrate, next_scan) != migrate) |
2714 | return; | |
2715 | ||
19a78d11 PZ |
2716 | /* |
2717 | * Delay this task enough that another task of this mm will likely win | |
2718 | * the next time around. | |
2719 | */ | |
2720 | p->node_stamp += 2 * TICK_NSEC; | |
2721 | ||
9f40604c MG |
2722 | start = mm->numa_scan_offset; |
2723 | pages = sysctl_numa_balancing_scan_size; | |
2724 | pages <<= 20 - PAGE_SHIFT; /* MB in pages */ | |
4620f8c1 | 2725 | virtpages = pages * 8; /* Scan up to this much virtual space */ |
9f40604c MG |
2726 | if (!pages) |
2727 | return; | |
cbee9f88 | 2728 | |
4620f8c1 | 2729 | |
d8ed45c5 | 2730 | if (!mmap_read_trylock(mm)) |
8655d549 | 2731 | return; |
9f40604c | 2732 | vma = find_vma(mm, start); |
6e5fb223 PZ |
2733 | if (!vma) { |
2734 | reset_ptenuma_scan(p); | |
9f40604c | 2735 | start = 0; |
6e5fb223 PZ |
2736 | vma = mm->mmap; |
2737 | } | |
9f40604c | 2738 | for (; vma; vma = vma->vm_next) { |
6b79c57b | 2739 | if (!vma_migratable(vma) || !vma_policy_mof(vma) || |
8e76d4ee | 2740 | is_vm_hugetlb_page(vma) || (vma->vm_flags & VM_MIXEDMAP)) { |
6e5fb223 | 2741 | continue; |
6b79c57b | 2742 | } |
6e5fb223 | 2743 | |
4591ce4f MG |
2744 | /* |
2745 | * Shared library pages mapped by multiple processes are not | |
2746 | * migrated as it is expected they are cache replicated. Avoid | |
2747 | * hinting faults in read-only file-backed mappings or the vdso | |
2748 | * as migrating the pages will be of marginal benefit. | |
2749 | */ | |
2750 | if (!vma->vm_mm || | |
2751 | (vma->vm_file && (vma->vm_flags & (VM_READ|VM_WRITE)) == (VM_READ))) | |
2752 | continue; | |
2753 | ||
3c67f474 MG |
2754 | /* |
2755 | * Skip inaccessible VMAs to avoid any confusion between | |
2756 | * PROT_NONE and NUMA hinting ptes | |
2757 | */ | |
3122e80e | 2758 | if (!vma_is_accessible(vma)) |
3c67f474 | 2759 | continue; |
4591ce4f | 2760 | |
9f40604c MG |
2761 | do { |
2762 | start = max(start, vma->vm_start); | |
2763 | end = ALIGN(start + (pages << PAGE_SHIFT), HPAGE_SIZE); | |
2764 | end = min(end, vma->vm_end); | |
4620f8c1 | 2765 | nr_pte_updates = change_prot_numa(vma, start, end); |
598f0ec0 MG |
2766 | |
2767 | /* | |
4620f8c1 RR |
2768 | * Try to scan sysctl_numa_balancing_size worth of |
2769 | * hpages that have at least one present PTE that | |
2770 | * is not already pte-numa. If the VMA contains | |
2771 | * areas that are unused or already full of prot_numa | |
2772 | * PTEs, scan up to virtpages, to skip through those | |
2773 | * areas faster. | |
598f0ec0 MG |
2774 | */ |
2775 | if (nr_pte_updates) | |
2776 | pages -= (end - start) >> PAGE_SHIFT; | |
4620f8c1 | 2777 | virtpages -= (end - start) >> PAGE_SHIFT; |
6e5fb223 | 2778 | |
9f40604c | 2779 | start = end; |
4620f8c1 | 2780 | if (pages <= 0 || virtpages <= 0) |
9f40604c | 2781 | goto out; |
3cf1962c RR |
2782 | |
2783 | cond_resched(); | |
9f40604c | 2784 | } while (end != vma->vm_end); |
cbee9f88 | 2785 | } |
6e5fb223 | 2786 | |
9f40604c | 2787 | out: |
6e5fb223 | 2788 | /* |
c69307d5 PZ |
2789 | * It is possible to reach the end of the VMA list but the last few |
2790 | * VMAs are not guaranteed to the vma_migratable. If they are not, we | |
2791 | * would find the !migratable VMA on the next scan but not reset the | |
2792 | * scanner to the start so check it now. | |
6e5fb223 PZ |
2793 | */ |
2794 | if (vma) | |
9f40604c | 2795 | mm->numa_scan_offset = start; |
6e5fb223 PZ |
2796 | else |
2797 | reset_ptenuma_scan(p); | |
d8ed45c5 | 2798 | mmap_read_unlock(mm); |
51170840 RR |
2799 | |
2800 | /* | |
2801 | * Make sure tasks use at least 32x as much time to run other code | |
2802 | * than they used here, to limit NUMA PTE scanning overhead to 3% max. | |
2803 | * Usually update_task_scan_period slows down scanning enough; on an | |
2804 | * overloaded system we need to limit overhead on a per task basis. | |
2805 | */ | |
2806 | if (unlikely(p->se.sum_exec_runtime != runtime)) { | |
2807 | u64 diff = p->se.sum_exec_runtime - runtime; | |
2808 | p->node_stamp += 32 * diff; | |
2809 | } | |
cbee9f88 PZ |
2810 | } |
2811 | ||
d35927a1 VS |
2812 | void init_numa_balancing(unsigned long clone_flags, struct task_struct *p) |
2813 | { | |
2814 | int mm_users = 0; | |
2815 | struct mm_struct *mm = p->mm; | |
2816 | ||
2817 | if (mm) { | |
2818 | mm_users = atomic_read(&mm->mm_users); | |
2819 | if (mm_users == 1) { | |
2820 | mm->numa_next_scan = jiffies + msecs_to_jiffies(sysctl_numa_balancing_scan_delay); | |
2821 | mm->numa_scan_seq = 0; | |
2822 | } | |
2823 | } | |
2824 | p->node_stamp = 0; | |
2825 | p->numa_scan_seq = mm ? mm->numa_scan_seq : 0; | |
2826 | p->numa_scan_period = sysctl_numa_balancing_scan_delay; | |
b34920d4 | 2827 | /* Protect against double add, see task_tick_numa and task_numa_work */ |
d35927a1 VS |
2828 | p->numa_work.next = &p->numa_work; |
2829 | p->numa_faults = NULL; | |
2830 | RCU_INIT_POINTER(p->numa_group, NULL); | |
2831 | p->last_task_numa_placement = 0; | |
2832 | p->last_sum_exec_runtime = 0; | |
2833 | ||
b34920d4 VS |
2834 | init_task_work(&p->numa_work, task_numa_work); |
2835 | ||
d35927a1 VS |
2836 | /* New address space, reset the preferred nid */ |
2837 | if (!(clone_flags & CLONE_VM)) { | |
2838 | p->numa_preferred_nid = NUMA_NO_NODE; | |
2839 | return; | |
2840 | } | |
2841 | ||
2842 | /* | |
2843 | * New thread, keep existing numa_preferred_nid which should be copied | |
2844 | * already by arch_dup_task_struct but stagger when scans start. | |
2845 | */ | |
2846 | if (mm) { | |
2847 | unsigned int delay; | |
2848 | ||
2849 | delay = min_t(unsigned int, task_scan_max(current), | |
2850 | current->numa_scan_period * mm_users * NSEC_PER_MSEC); | |
2851 | delay += 2 * TICK_NSEC; | |
2852 | p->node_stamp = delay; | |
2853 | } | |
2854 | } | |
2855 | ||
cbee9f88 PZ |
2856 | /* |
2857 | * Drive the periodic memory faults.. | |
2858 | */ | |
b1546edc | 2859 | static void task_tick_numa(struct rq *rq, struct task_struct *curr) |
cbee9f88 PZ |
2860 | { |
2861 | struct callback_head *work = &curr->numa_work; | |
2862 | u64 period, now; | |
2863 | ||
2864 | /* | |
2865 | * We don't care about NUMA placement if we don't have memory. | |
2866 | */ | |
18f855e5 | 2867 | if ((curr->flags & (PF_EXITING | PF_KTHREAD)) || work->next != work) |
cbee9f88 PZ |
2868 | return; |
2869 | ||
2870 | /* | |
2871 | * Using runtime rather than walltime has the dual advantage that | |
2872 | * we (mostly) drive the selection from busy threads and that the | |
2873 | * task needs to have done some actual work before we bother with | |
2874 | * NUMA placement. | |
2875 | */ | |
2876 | now = curr->se.sum_exec_runtime; | |
2877 | period = (u64)curr->numa_scan_period * NSEC_PER_MSEC; | |
2878 | ||
25b3e5a3 | 2879 | if (now > curr->node_stamp + period) { |
4b96a29b | 2880 | if (!curr->node_stamp) |
b5dd77c8 | 2881 | curr->numa_scan_period = task_scan_start(curr); |
19a78d11 | 2882 | curr->node_stamp += period; |
cbee9f88 | 2883 | |
b34920d4 | 2884 | if (!time_before(jiffies, curr->mm->numa_next_scan)) |
91989c70 | 2885 | task_work_add(curr, work, TWA_RESUME); |
cbee9f88 PZ |
2886 | } |
2887 | } | |
3fed382b | 2888 | |
3f9672ba SD |
2889 | static void update_scan_period(struct task_struct *p, int new_cpu) |
2890 | { | |
2891 | int src_nid = cpu_to_node(task_cpu(p)); | |
2892 | int dst_nid = cpu_to_node(new_cpu); | |
2893 | ||
05cbdf4f MG |
2894 | if (!static_branch_likely(&sched_numa_balancing)) |
2895 | return; | |
2896 | ||
3f9672ba SD |
2897 | if (!p->mm || !p->numa_faults || (p->flags & PF_EXITING)) |
2898 | return; | |
2899 | ||
05cbdf4f MG |
2900 | if (src_nid == dst_nid) |
2901 | return; | |
2902 | ||
2903 | /* | |
2904 | * Allow resets if faults have been trapped before one scan | |
2905 | * has completed. This is most likely due to a new task that | |
2906 | * is pulled cross-node due to wakeups or load balancing. | |
2907 | */ | |
2908 | if (p->numa_scan_seq) { | |
2909 | /* | |
2910 | * Avoid scan adjustments if moving to the preferred | |
2911 | * node or if the task was not previously running on | |
2912 | * the preferred node. | |
2913 | */ | |
2914 | if (dst_nid == p->numa_preferred_nid || | |
98fa15f3 AK |
2915 | (p->numa_preferred_nid != NUMA_NO_NODE && |
2916 | src_nid != p->numa_preferred_nid)) | |
05cbdf4f MG |
2917 | return; |
2918 | } | |
2919 | ||
2920 | p->numa_scan_period = task_scan_start(p); | |
3f9672ba SD |
2921 | } |
2922 | ||
cbee9f88 PZ |
2923 | #else |
2924 | static void task_tick_numa(struct rq *rq, struct task_struct *curr) | |
2925 | { | |
2926 | } | |
0ec8aa00 PZ |
2927 | |
2928 | static inline void account_numa_enqueue(struct rq *rq, struct task_struct *p) | |
2929 | { | |
2930 | } | |
2931 | ||
2932 | static inline void account_numa_dequeue(struct rq *rq, struct task_struct *p) | |
2933 | { | |
2934 | } | |
3fed382b | 2935 | |
3f9672ba SD |
2936 | static inline void update_scan_period(struct task_struct *p, int new_cpu) |
2937 | { | |
2938 | } | |
2939 | ||
cbee9f88 PZ |
2940 | #endif /* CONFIG_NUMA_BALANCING */ |
2941 | ||
30cfdcfc DA |
2942 | static void |
2943 | account_entity_enqueue(struct cfs_rq *cfs_rq, struct sched_entity *se) | |
2944 | { | |
2945 | update_load_add(&cfs_rq->load, se->load.weight); | |
367456c7 | 2946 | #ifdef CONFIG_SMP |
0ec8aa00 PZ |
2947 | if (entity_is_task(se)) { |
2948 | struct rq *rq = rq_of(cfs_rq); | |
2949 | ||
2950 | account_numa_enqueue(rq, task_of(se)); | |
2951 | list_add(&se->group_node, &rq->cfs_tasks); | |
2952 | } | |
367456c7 | 2953 | #endif |
30cfdcfc | 2954 | cfs_rq->nr_running++; |
a480adde JD |
2955 | if (se_is_idle(se)) |
2956 | cfs_rq->idle_nr_running++; | |
30cfdcfc DA |
2957 | } |
2958 | ||
2959 | static void | |
2960 | account_entity_dequeue(struct cfs_rq *cfs_rq, struct sched_entity *se) | |
2961 | { | |
2962 | update_load_sub(&cfs_rq->load, se->load.weight); | |
bfdb198c | 2963 | #ifdef CONFIG_SMP |
0ec8aa00 PZ |
2964 | if (entity_is_task(se)) { |
2965 | account_numa_dequeue(rq_of(cfs_rq), task_of(se)); | |
b87f1724 | 2966 | list_del_init(&se->group_node); |
0ec8aa00 | 2967 | } |
bfdb198c | 2968 | #endif |
30cfdcfc | 2969 | cfs_rq->nr_running--; |
a480adde JD |
2970 | if (se_is_idle(se)) |
2971 | cfs_rq->idle_nr_running--; | |
30cfdcfc DA |
2972 | } |
2973 | ||
8d5b9025 PZ |
2974 | /* |
2975 | * Signed add and clamp on underflow. | |
2976 | * | |
2977 | * Explicitly do a load-store to ensure the intermediate value never hits | |
2978 | * memory. This allows lockless observations without ever seeing the negative | |
2979 | * values. | |
2980 | */ | |
2981 | #define add_positive(_ptr, _val) do { \ | |
2982 | typeof(_ptr) ptr = (_ptr); \ | |
2983 | typeof(_val) val = (_val); \ | |
2984 | typeof(*ptr) res, var = READ_ONCE(*ptr); \ | |
2985 | \ | |
2986 | res = var + val; \ | |
2987 | \ | |
2988 | if (val < 0 && res > var) \ | |
2989 | res = 0; \ | |
2990 | \ | |
2991 | WRITE_ONCE(*ptr, res); \ | |
2992 | } while (0) | |
2993 | ||
2994 | /* | |
2995 | * Unsigned subtract and clamp on underflow. | |
2996 | * | |
2997 | * Explicitly do a load-store to ensure the intermediate value never hits | |
2998 | * memory. This allows lockless observations without ever seeing the negative | |
2999 | * values. | |
3000 | */ | |
3001 | #define sub_positive(_ptr, _val) do { \ | |
3002 | typeof(_ptr) ptr = (_ptr); \ | |
3003 | typeof(*ptr) val = (_val); \ | |
3004 | typeof(*ptr) res, var = READ_ONCE(*ptr); \ | |
3005 | res = var - val; \ | |
3006 | if (res > var) \ | |
3007 | res = 0; \ | |
3008 | WRITE_ONCE(*ptr, res); \ | |
3009 | } while (0) | |
3010 | ||
b5c0ce7b PB |
3011 | /* |
3012 | * Remove and clamp on negative, from a local variable. | |
3013 | * | |
3014 | * A variant of sub_positive(), which does not use explicit load-store | |
3015 | * and is thus optimized for local variable updates. | |
3016 | */ | |
3017 | #define lsub_positive(_ptr, _val) do { \ | |
3018 | typeof(_ptr) ptr = (_ptr); \ | |
3019 | *ptr -= min_t(typeof(*ptr), *ptr, _val); \ | |
3020 | } while (0) | |
3021 | ||
8d5b9025 | 3022 | #ifdef CONFIG_SMP |
8d5b9025 PZ |
3023 | static inline void |
3024 | enqueue_load_avg(struct cfs_rq *cfs_rq, struct sched_entity *se) | |
3025 | { | |
3026 | cfs_rq->avg.load_avg += se->avg.load_avg; | |
3027 | cfs_rq->avg.load_sum += se_weight(se) * se->avg.load_sum; | |
3028 | } | |
3029 | ||
3030 | static inline void | |
3031 | dequeue_load_avg(struct cfs_rq *cfs_rq, struct sched_entity *se) | |
3032 | { | |
ceb6ba45 | 3033 | u32 divider = get_pelt_divider(&se->avg); |
8d5b9025 | 3034 | sub_positive(&cfs_rq->avg.load_avg, se->avg.load_avg); |
ceb6ba45 | 3035 | cfs_rq->avg.load_sum = cfs_rq->avg.load_avg * divider; |
8d5b9025 PZ |
3036 | } |
3037 | #else | |
3038 | static inline void | |
8d5b9025 PZ |
3039 | enqueue_load_avg(struct cfs_rq *cfs_rq, struct sched_entity *se) { } |
3040 | static inline void | |
3041 | dequeue_load_avg(struct cfs_rq *cfs_rq, struct sched_entity *se) { } | |
3042 | #endif | |
3043 | ||
9059393e | 3044 | static void reweight_entity(struct cfs_rq *cfs_rq, struct sched_entity *se, |
0dacee1b | 3045 | unsigned long weight) |
9059393e VG |
3046 | { |
3047 | if (se->on_rq) { | |
3048 | /* commit outstanding execution time */ | |
3049 | if (cfs_rq->curr == se) | |
3050 | update_curr(cfs_rq); | |
1724b95b | 3051 | update_load_sub(&cfs_rq->load, se->load.weight); |
9059393e VG |
3052 | } |
3053 | dequeue_load_avg(cfs_rq, se); | |
3054 | ||
3055 | update_load_set(&se->load, weight); | |
3056 | ||
3057 | #ifdef CONFIG_SMP | |
1ea6c46a | 3058 | do { |
87e867b4 | 3059 | u32 divider = get_pelt_divider(&se->avg); |
1ea6c46a PZ |
3060 | |
3061 | se->avg.load_avg = div_u64(se_weight(se) * se->avg.load_sum, divider); | |
1ea6c46a | 3062 | } while (0); |
9059393e VG |
3063 | #endif |
3064 | ||
3065 | enqueue_load_avg(cfs_rq, se); | |
0dacee1b | 3066 | if (se->on_rq) |
1724b95b | 3067 | update_load_add(&cfs_rq->load, se->load.weight); |
0dacee1b | 3068 | |
9059393e VG |
3069 | } |
3070 | ||
3071 | void reweight_task(struct task_struct *p, int prio) | |
3072 | { | |
3073 | struct sched_entity *se = &p->se; | |
3074 | struct cfs_rq *cfs_rq = cfs_rq_of(se); | |
3075 | struct load_weight *load = &se->load; | |
3076 | unsigned long weight = scale_load(sched_prio_to_weight[prio]); | |
3077 | ||
0dacee1b | 3078 | reweight_entity(cfs_rq, se, weight); |
9059393e VG |
3079 | load->inv_weight = sched_prio_to_wmult[prio]; |
3080 | } | |
3081 | ||
3ff6dcac | 3082 | #ifdef CONFIG_FAIR_GROUP_SCHED |
387f77cc | 3083 | #ifdef CONFIG_SMP |
cef27403 PZ |
3084 | /* |
3085 | * All this does is approximate the hierarchical proportion which includes that | |
3086 | * global sum we all love to hate. | |
3087 | * | |
3088 | * That is, the weight of a group entity, is the proportional share of the | |
3089 | * group weight based on the group runqueue weights. That is: | |
3090 | * | |
3091 | * tg->weight * grq->load.weight | |
3092 | * ge->load.weight = ----------------------------- (1) | |
08f7c2f4 | 3093 | * \Sum grq->load.weight |
cef27403 PZ |
3094 | * |
3095 | * Now, because computing that sum is prohibitively expensive to compute (been | |
3096 | * there, done that) we approximate it with this average stuff. The average | |
3097 | * moves slower and therefore the approximation is cheaper and more stable. | |
3098 | * | |
3099 | * So instead of the above, we substitute: | |
3100 | * | |
3101 | * grq->load.weight -> grq->avg.load_avg (2) | |
3102 | * | |
3103 | * which yields the following: | |
3104 | * | |
3105 | * tg->weight * grq->avg.load_avg | |
3106 | * ge->load.weight = ------------------------------ (3) | |
08f7c2f4 | 3107 | * tg->load_avg |
cef27403 PZ |
3108 | * |
3109 | * Where: tg->load_avg ~= \Sum grq->avg.load_avg | |
3110 | * | |
3111 | * That is shares_avg, and it is right (given the approximation (2)). | |
3112 | * | |
3113 | * The problem with it is that because the average is slow -- it was designed | |
3114 | * to be exactly that of course -- this leads to transients in boundary | |
3115 | * conditions. In specific, the case where the group was idle and we start the | |
3116 | * one task. It takes time for our CPU's grq->avg.load_avg to build up, | |
3117 | * yielding bad latency etc.. | |
3118 | * | |
3119 | * Now, in that special case (1) reduces to: | |
3120 | * | |
3121 | * tg->weight * grq->load.weight | |
17de4ee0 | 3122 | * ge->load.weight = ----------------------------- = tg->weight (4) |
08f7c2f4 | 3123 | * grp->load.weight |
cef27403 PZ |
3124 | * |
3125 | * That is, the sum collapses because all other CPUs are idle; the UP scenario. | |
3126 | * | |
3127 | * So what we do is modify our approximation (3) to approach (4) in the (near) | |
3128 | * UP case, like: | |
3129 | * | |
3130 | * ge->load.weight = | |
3131 | * | |
3132 | * tg->weight * grq->load.weight | |
3133 | * --------------------------------------------------- (5) | |
3134 | * tg->load_avg - grq->avg.load_avg + grq->load.weight | |
3135 | * | |
17de4ee0 PZ |
3136 | * But because grq->load.weight can drop to 0, resulting in a divide by zero, |
3137 | * we need to use grq->avg.load_avg as its lower bound, which then gives: | |
3138 | * | |
3139 | * | |
3140 | * tg->weight * grq->load.weight | |
3141 | * ge->load.weight = ----------------------------- (6) | |
08f7c2f4 | 3142 | * tg_load_avg' |
17de4ee0 PZ |
3143 | * |
3144 | * Where: | |
3145 | * | |
3146 | * tg_load_avg' = tg->load_avg - grq->avg.load_avg + | |
3147 | * max(grq->load.weight, grq->avg.load_avg) | |
cef27403 PZ |
3148 | * |
3149 | * And that is shares_weight and is icky. In the (near) UP case it approaches | |
3150 | * (4) while in the normal case it approaches (3). It consistently | |
3151 | * overestimates the ge->load.weight and therefore: | |
3152 | * | |
3153 | * \Sum ge->load.weight >= tg->weight | |
3154 | * | |
3155 | * hence icky! | |
3156 | */ | |
2c8e4dce | 3157 | static long calc_group_shares(struct cfs_rq *cfs_rq) |
cf5f0acf | 3158 | { |
7c80cfc9 PZ |
3159 | long tg_weight, tg_shares, load, shares; |
3160 | struct task_group *tg = cfs_rq->tg; | |
3161 | ||
3162 | tg_shares = READ_ONCE(tg->shares); | |
cf5f0acf | 3163 | |
3d4b60d3 | 3164 | load = max(scale_load_down(cfs_rq->load.weight), cfs_rq->avg.load_avg); |
cf5f0acf | 3165 | |
ea1dc6fc | 3166 | tg_weight = atomic_long_read(&tg->load_avg); |
3ff6dcac | 3167 | |
ea1dc6fc PZ |
3168 | /* Ensure tg_weight >= load */ |
3169 | tg_weight -= cfs_rq->tg_load_avg_contrib; | |
3170 | tg_weight += load; | |
3ff6dcac | 3171 | |
7c80cfc9 | 3172 | shares = (tg_shares * load); |
cf5f0acf PZ |
3173 | if (tg_weight) |
3174 | shares /= tg_weight; | |
3ff6dcac | 3175 | |
b8fd8423 DE |
3176 | /* |
3177 | * MIN_SHARES has to be unscaled here to support per-CPU partitioning | |
3178 | * of a group with small tg->shares value. It is a floor value which is | |
3179 | * assigned as a minimum load.weight to the sched_entity representing | |
3180 | * the group on a CPU. | |
3181 | * | |
3182 | * E.g. on 64-bit for a group with tg->shares of scale_load(15)=15*1024 | |
3183 | * on an 8-core system with 8 tasks each runnable on one CPU shares has | |
3184 | * to be 15*1024*1/8=1920 instead of scale_load(MIN_SHARES)=2*1024. In | |
3185 | * case no task is runnable on a CPU MIN_SHARES=2 should be returned | |
3186 | * instead of 0. | |
3187 | */ | |
7c80cfc9 | 3188 | return clamp_t(long, shares, MIN_SHARES, tg_shares); |
3ff6dcac | 3189 | } |
387f77cc | 3190 | #endif /* CONFIG_SMP */ |
ea1dc6fc | 3191 | |
82958366 PT |
3192 | static inline int throttled_hierarchy(struct cfs_rq *cfs_rq); |
3193 | ||
1ea6c46a PZ |
3194 | /* |
3195 | * Recomputes the group entity based on the current state of its group | |
3196 | * runqueue. | |
3197 | */ | |
3198 | static void update_cfs_group(struct sched_entity *se) | |
2069dd75 | 3199 | { |
1ea6c46a | 3200 | struct cfs_rq *gcfs_rq = group_cfs_rq(se); |
0dacee1b | 3201 | long shares; |
2069dd75 | 3202 | |
1ea6c46a | 3203 | if (!gcfs_rq) |
89ee048f VG |
3204 | return; |
3205 | ||
1ea6c46a | 3206 | if (throttled_hierarchy(gcfs_rq)) |
2069dd75 | 3207 | return; |
89ee048f | 3208 | |
3ff6dcac | 3209 | #ifndef CONFIG_SMP |
0dacee1b | 3210 | shares = READ_ONCE(gcfs_rq->tg->shares); |
7c80cfc9 PZ |
3211 | |
3212 | if (likely(se->load.weight == shares)) | |
3ff6dcac | 3213 | return; |
7c80cfc9 | 3214 | #else |
2c8e4dce | 3215 | shares = calc_group_shares(gcfs_rq); |
3ff6dcac | 3216 | #endif |
2069dd75 | 3217 | |
0dacee1b | 3218 | reweight_entity(cfs_rq_of(se), se, shares); |
2069dd75 | 3219 | } |
89ee048f | 3220 | |
2069dd75 | 3221 | #else /* CONFIG_FAIR_GROUP_SCHED */ |
1ea6c46a | 3222 | static inline void update_cfs_group(struct sched_entity *se) |
2069dd75 PZ |
3223 | { |
3224 | } | |
3225 | #endif /* CONFIG_FAIR_GROUP_SCHED */ | |
3226 | ||
ea14b57e | 3227 | static inline void cfs_rq_util_change(struct cfs_rq *cfs_rq, int flags) |
a030d738 | 3228 | { |
43964409 LT |
3229 | struct rq *rq = rq_of(cfs_rq); |
3230 | ||
a4f9a0e5 | 3231 | if (&rq->cfs == cfs_rq) { |
a030d738 VK |
3232 | /* |
3233 | * There are a few boundary cases this might miss but it should | |
3234 | * get called often enough that that should (hopefully) not be | |
9783be2c | 3235 | * a real problem. |
a030d738 VK |
3236 | * |
3237 | * It will not get called when we go idle, because the idle | |
3238 | * thread is a different class (!fair), nor will the utilization | |
3239 | * number include things like RT tasks. | |
3240 | * | |
3241 | * As is, the util number is not freq-invariant (we'd have to | |
3242 | * implement arch_scale_freq_capacity() for that). | |
3243 | * | |
3244 | * See cpu_util(). | |
3245 | */ | |
ea14b57e | 3246 | cpufreq_update_util(rq, flags); |
a030d738 VK |
3247 | } |
3248 | } | |
3249 | ||
141965c7 | 3250 | #ifdef CONFIG_SMP |
c566e8e9 | 3251 | #ifdef CONFIG_FAIR_GROUP_SCHED |
fdaba61e RR |
3252 | /* |
3253 | * Because list_add_leaf_cfs_rq always places a child cfs_rq on the list | |
3254 | * immediately before a parent cfs_rq, and cfs_rqs are removed from the list | |
3255 | * bottom-up, we only have to test whether the cfs_rq before us on the list | |
3256 | * is our child. | |
3257 | * If cfs_rq is not on the list, test whether a child needs its to be added to | |
3258 | * connect a branch to the tree * (see list_add_leaf_cfs_rq() for details). | |
3259 | */ | |
3260 | static inline bool child_cfs_rq_on_list(struct cfs_rq *cfs_rq) | |
3261 | { | |
3262 | struct cfs_rq *prev_cfs_rq; | |
3263 | struct list_head *prev; | |
3264 | ||
3265 | if (cfs_rq->on_list) { | |
3266 | prev = cfs_rq->leaf_cfs_rq_list.prev; | |
3267 | } else { | |
3268 | struct rq *rq = rq_of(cfs_rq); | |
3269 | ||
3270 | prev = rq->tmp_alone_branch; | |
3271 | } | |
3272 | ||
3273 | prev_cfs_rq = container_of(prev, struct cfs_rq, leaf_cfs_rq_list); | |
3274 | ||
3275 | return (prev_cfs_rq->tg->parent == cfs_rq->tg); | |
3276 | } | |
a7b359fc OU |
3277 | |
3278 | static inline bool cfs_rq_is_decayed(struct cfs_rq *cfs_rq) | |
3279 | { | |
3280 | if (cfs_rq->load.weight) | |
3281 | return false; | |
3282 | ||
3283 | if (cfs_rq->avg.load_sum) | |
3284 | return false; | |
3285 | ||
3286 | if (cfs_rq->avg.util_sum) | |
3287 | return false; | |
3288 | ||
3289 | if (cfs_rq->avg.runnable_sum) | |
3290 | return false; | |
3291 | ||
fdaba61e RR |
3292 | if (child_cfs_rq_on_list(cfs_rq)) |
3293 | return false; | |
3294 | ||
b2c0931a IM |
3295 | /* |
3296 | * _avg must be null when _sum are null because _avg = _sum / divider | |
3297 | * Make sure that rounding and/or propagation of PELT values never | |
3298 | * break this. | |
3299 | */ | |
3300 | SCHED_WARN_ON(cfs_rq->avg.load_avg || | |
3301 | cfs_rq->avg.util_avg || | |
3302 | cfs_rq->avg.runnable_avg); | |
3303 | ||
a7b359fc OU |
3304 | return true; |
3305 | } | |
3306 | ||
7c3edd2c PZ |
3307 | /** |
3308 | * update_tg_load_avg - update the tg's load avg | |
3309 | * @cfs_rq: the cfs_rq whose avg changed | |
7c3edd2c PZ |
3310 | * |
3311 | * This function 'ensures': tg->load_avg := \Sum tg->cfs_rq[]->avg.load. | |
3312 | * However, because tg->load_avg is a global value there are performance | |
3313 | * considerations. | |
3314 | * | |
3315 | * In order to avoid having to look at the other cfs_rq's, we use a | |
3316 | * differential update where we store the last value we propagated. This in | |
3317 | * turn allows skipping updates if the differential is 'small'. | |
3318 | * | |
815abf5a | 3319 | * Updating tg's load_avg is necessary before update_cfs_share(). |
bb17f655 | 3320 | */ |
fe749158 | 3321 | static inline void update_tg_load_avg(struct cfs_rq *cfs_rq) |
bb17f655 | 3322 | { |
9d89c257 | 3323 | long delta = cfs_rq->avg.load_avg - cfs_rq->tg_load_avg_contrib; |
bb17f655 | 3324 | |
aa0b7ae0 WL |
3325 | /* |
3326 | * No need to update load_avg for root_task_group as it is not used. | |
3327 | */ | |
3328 | if (cfs_rq->tg == &root_task_group) | |
3329 | return; | |
3330 | ||
fe749158 | 3331 | if (abs(delta) > cfs_rq->tg_load_avg_contrib / 64) { |
9d89c257 YD |
3332 | atomic_long_add(delta, &cfs_rq->tg->load_avg); |
3333 | cfs_rq->tg_load_avg_contrib = cfs_rq->avg.load_avg; | |
bb17f655 | 3334 | } |
8165e145 | 3335 | } |
f5f9739d | 3336 | |
ad936d86 | 3337 | /* |
97fb7a0a | 3338 | * Called within set_task_rq() right before setting a task's CPU. The |
ad936d86 BP |
3339 | * caller only guarantees p->pi_lock is held; no other assumptions, |
3340 | * including the state of rq->lock, should be made. | |
3341 | */ | |
3342 | void set_task_rq_fair(struct sched_entity *se, | |
3343 | struct cfs_rq *prev, struct cfs_rq *next) | |
3344 | { | |
0ccb977f PZ |
3345 | u64 p_last_update_time; |
3346 | u64 n_last_update_time; | |
3347 | ||
ad936d86 BP |
3348 | if (!sched_feat(ATTACH_AGE_LOAD)) |
3349 | return; | |
3350 | ||
3351 | /* | |
3352 | * We are supposed to update the task to "current" time, then its up to | |
3353 | * date and ready to go to new CPU/cfs_rq. But we have difficulty in | |
3354 | * getting what current time is, so simply throw away the out-of-date | |
3355 | * time. This will result in the wakee task is less decayed, but giving | |
3356 | * the wakee more load sounds not bad. | |
3357 | */ | |
0ccb977f PZ |
3358 | if (!(se->avg.last_update_time && prev)) |
3359 | return; | |
ad936d86 BP |
3360 | |
3361 | #ifndef CONFIG_64BIT | |
0ccb977f | 3362 | { |
ad936d86 BP |
3363 | u64 p_last_update_time_copy; |
3364 | u64 n_last_update_time_copy; | |
3365 | ||
3366 | do { | |
3367 | p_last_update_time_copy = prev->load_last_update_time_copy; | |
3368 | n_last_update_time_copy = next->load_last_update_time_copy; | |
3369 | ||
3370 | smp_rmb(); | |
3371 | ||
3372 | p_last_update_time = prev->avg.last_update_time; | |
3373 | n_last_update_time = next->avg.last_update_time; | |
3374 | ||
3375 | } while (p_last_update_time != p_last_update_time_copy || | |
3376 | n_last_update_time != n_last_update_time_copy); | |
0ccb977f | 3377 | } |
ad936d86 | 3378 | #else |
0ccb977f PZ |
3379 | p_last_update_time = prev->avg.last_update_time; |
3380 | n_last_update_time = next->avg.last_update_time; | |
ad936d86 | 3381 | #endif |
23127296 | 3382 | __update_load_avg_blocked_se(p_last_update_time, se); |
0ccb977f | 3383 | se->avg.last_update_time = n_last_update_time; |
ad936d86 | 3384 | } |
09a43ace | 3385 | |
0e2d2aaa PZ |
3386 | |
3387 | /* | |
3388 | * When on migration a sched_entity joins/leaves the PELT hierarchy, we need to | |
3389 | * propagate its contribution. The key to this propagation is the invariant | |
3390 | * that for each group: | |
3391 | * | |
3392 | * ge->avg == grq->avg (1) | |
3393 | * | |
3394 | * _IFF_ we look at the pure running and runnable sums. Because they | |
3395 | * represent the very same entity, just at different points in the hierarchy. | |
3396 | * | |
9f683953 VG |
3397 | * Per the above update_tg_cfs_util() and update_tg_cfs_runnable() are trivial |
3398 | * and simply copies the running/runnable sum over (but still wrong, because | |
3399 | * the group entity and group rq do not have their PELT windows aligned). | |
0e2d2aaa | 3400 | * |
0dacee1b | 3401 | * However, update_tg_cfs_load() is more complex. So we have: |
0e2d2aaa PZ |
3402 | * |
3403 | * ge->avg.load_avg = ge->load.weight * ge->avg.runnable_avg (2) | |
3404 | * | |
3405 | * And since, like util, the runnable part should be directly transferable, | |
3406 | * the following would _appear_ to be the straight forward approach: | |
3407 | * | |
a4c3c049 | 3408 | * grq->avg.load_avg = grq->load.weight * grq->avg.runnable_avg (3) |
0e2d2aaa PZ |
3409 | * |
3410 | * And per (1) we have: | |
3411 | * | |
a4c3c049 | 3412 | * ge->avg.runnable_avg == grq->avg.runnable_avg |
0e2d2aaa PZ |
3413 | * |
3414 | * Which gives: | |
3415 | * | |
3416 | * ge->load.weight * grq->avg.load_avg | |
3417 | * ge->avg.load_avg = ----------------------------------- (4) | |
3418 | * grq->load.weight | |
3419 | * | |
3420 | * Except that is wrong! | |
3421 | * | |
3422 | * Because while for entities historical weight is not important and we | |
3423 | * really only care about our future and therefore can consider a pure | |
3424 | * runnable sum, runqueues can NOT do this. | |
3425 | * | |
3426 | * We specifically want runqueues to have a load_avg that includes | |
3427 | * historical weights. Those represent the blocked load, the load we expect | |
3428 | * to (shortly) return to us. This only works by keeping the weights as | |
3429 | * integral part of the sum. We therefore cannot decompose as per (3). | |
3430 | * | |
a4c3c049 VG |
3431 | * Another reason this doesn't work is that runnable isn't a 0-sum entity. |
3432 | * Imagine a rq with 2 tasks that each are runnable 2/3 of the time. Then the | |
3433 | * rq itself is runnable anywhere between 2/3 and 1 depending on how the | |
3434 | * runnable section of these tasks overlap (or not). If they were to perfectly | |
3435 | * align the rq as a whole would be runnable 2/3 of the time. If however we | |
3436 | * always have at least 1 runnable task, the rq as a whole is always runnable. | |
0e2d2aaa | 3437 | * |
a4c3c049 | 3438 | * So we'll have to approximate.. :/ |
0e2d2aaa | 3439 | * |
a4c3c049 | 3440 | * Given the constraint: |
0e2d2aaa | 3441 | * |
a4c3c049 | 3442 | * ge->avg.running_sum <= ge->avg.runnable_sum <= LOAD_AVG_MAX |
0e2d2aaa | 3443 | * |
a4c3c049 VG |
3444 | * We can construct a rule that adds runnable to a rq by assuming minimal |
3445 | * overlap. | |
0e2d2aaa | 3446 | * |
a4c3c049 | 3447 | * On removal, we'll assume each task is equally runnable; which yields: |
0e2d2aaa | 3448 | * |
a4c3c049 | 3449 | * grq->avg.runnable_sum = grq->avg.load_sum / grq->load.weight |
0e2d2aaa | 3450 | * |
a4c3c049 | 3451 | * XXX: only do this for the part of runnable > running ? |
0e2d2aaa | 3452 | * |
0e2d2aaa PZ |
3453 | */ |
3454 | ||
09a43ace | 3455 | static inline void |
0e2d2aaa | 3456 | update_tg_cfs_util(struct cfs_rq *cfs_rq, struct sched_entity *se, struct cfs_rq *gcfs_rq) |
09a43ace | 3457 | { |
09a43ace | 3458 | long delta = gcfs_rq->avg.util_avg - se->avg.util_avg; |
87e867b4 | 3459 | u32 divider; |
09a43ace VG |
3460 | |
3461 | /* Nothing to update */ | |
3462 | if (!delta) | |
3463 | return; | |
3464 | ||
87e867b4 VG |
3465 | /* |
3466 | * cfs_rq->avg.period_contrib can be used for both cfs_rq and se. | |
3467 | * See ___update_load_avg() for details. | |
3468 | */ | |
3469 | divider = get_pelt_divider(&cfs_rq->avg); | |
3470 | ||
09a43ace VG |
3471 | /* Set new sched_entity's utilization */ |
3472 | se->avg.util_avg = gcfs_rq->avg.util_avg; | |
95d68593 | 3473 | se->avg.util_sum = se->avg.util_avg * divider; |
09a43ace VG |
3474 | |
3475 | /* Update parent cfs_rq utilization */ | |
3476 | add_positive(&cfs_rq->avg.util_avg, delta); | |
95d68593 | 3477 | cfs_rq->avg.util_sum = cfs_rq->avg.util_avg * divider; |
09a43ace VG |
3478 | } |
3479 | ||
9f683953 VG |
3480 | static inline void |
3481 | update_tg_cfs_runnable(struct cfs_rq *cfs_rq, struct sched_entity *se, struct cfs_rq *gcfs_rq) | |
3482 | { | |
3483 | long delta = gcfs_rq->avg.runnable_avg - se->avg.runnable_avg; | |
87e867b4 | 3484 | u32 divider; |
9f683953 VG |
3485 | |
3486 | /* Nothing to update */ | |
3487 | if (!delta) | |
3488 | return; | |
3489 | ||
87e867b4 VG |
3490 | /* |
3491 | * cfs_rq->avg.period_contrib can be used for both cfs_rq and se. | |
3492 | * See ___update_load_avg() for details. | |
3493 | */ | |
3494 | divider = get_pelt_divider(&cfs_rq->avg); | |
3495 | ||
9f683953 VG |
3496 | /* Set new sched_entity's runnable */ |
3497 | se->avg.runnable_avg = gcfs_rq->avg.runnable_avg; | |
95d68593 | 3498 | se->avg.runnable_sum = se->avg.runnable_avg * divider; |
9f683953 VG |
3499 | |
3500 | /* Update parent cfs_rq runnable */ | |
3501 | add_positive(&cfs_rq->avg.runnable_avg, delta); | |
95d68593 | 3502 | cfs_rq->avg.runnable_sum = cfs_rq->avg.runnable_avg * divider; |
9f683953 VG |
3503 | } |
3504 | ||
09a43ace | 3505 | static inline void |
0dacee1b | 3506 | update_tg_cfs_load(struct cfs_rq *cfs_rq, struct sched_entity *se, struct cfs_rq *gcfs_rq) |
09a43ace | 3507 | { |
7c7ad626 | 3508 | long delta, running_sum, runnable_sum = gcfs_rq->prop_runnable_sum; |
0dacee1b VG |
3509 | unsigned long load_avg; |
3510 | u64 load_sum = 0; | |
95d68593 | 3511 | u32 divider; |
09a43ace | 3512 | |
0e2d2aaa PZ |
3513 | if (!runnable_sum) |
3514 | return; | |
09a43ace | 3515 | |
0e2d2aaa | 3516 | gcfs_rq->prop_runnable_sum = 0; |
09a43ace | 3517 | |
95d68593 VG |
3518 | /* |
3519 | * cfs_rq->avg.period_contrib can be used for both cfs_rq and se. | |
3520 | * See ___update_load_avg() for details. | |
3521 | */ | |
87e867b4 | 3522 | divider = get_pelt_divider(&cfs_rq->avg); |
95d68593 | 3523 | |
a4c3c049 VG |
3524 | if (runnable_sum >= 0) { |
3525 | /* | |
3526 | * Add runnable; clip at LOAD_AVG_MAX. Reflects that until | |
3527 | * the CPU is saturated running == runnable. | |
3528 | */ | |
3529 | runnable_sum += se->avg.load_sum; | |
95d68593 | 3530 | runnable_sum = min_t(long, runnable_sum, divider); |
a4c3c049 VG |
3531 | } else { |
3532 | /* | |
3533 | * Estimate the new unweighted runnable_sum of the gcfs_rq by | |
3534 | * assuming all tasks are equally runnable. | |
3535 | */ | |
3536 | if (scale_load_down(gcfs_rq->load.weight)) { | |
3537 | load_sum = div_s64(gcfs_rq->avg.load_sum, | |
3538 | scale_load_down(gcfs_rq->load.weight)); | |
3539 | } | |
3540 | ||
3541 | /* But make sure to not inflate se's runnable */ | |
3542 | runnable_sum = min(se->avg.load_sum, load_sum); | |
3543 | } | |
3544 | ||
3545 | /* | |
3546 | * runnable_sum can't be lower than running_sum | |
23127296 VG |
3547 | * Rescale running sum to be in the same range as runnable sum |
3548 | * running_sum is in [0 : LOAD_AVG_MAX << SCHED_CAPACITY_SHIFT] | |
3549 | * runnable_sum is in [0 : LOAD_AVG_MAX] | |
a4c3c049 | 3550 | */ |
23127296 | 3551 | running_sum = se->avg.util_sum >> SCHED_CAPACITY_SHIFT; |
a4c3c049 VG |
3552 | runnable_sum = max(runnable_sum, running_sum); |
3553 | ||
0e2d2aaa | 3554 | load_sum = (s64)se_weight(se) * runnable_sum; |
95d68593 | 3555 | load_avg = div_s64(load_sum, divider); |
09a43ace | 3556 | |
83c5e9d5 DE |
3557 | se->avg.load_sum = runnable_sum; |
3558 | ||
7c7ad626 | 3559 | delta = load_avg - se->avg.load_avg; |
83c5e9d5 DE |
3560 | if (!delta) |
3561 | return; | |
09a43ace | 3562 | |
a4c3c049 | 3563 | se->avg.load_avg = load_avg; |
7c7ad626 VG |
3564 | |
3565 | add_positive(&cfs_rq->avg.load_avg, delta); | |
3566 | cfs_rq->avg.load_sum = cfs_rq->avg.load_avg * divider; | |
09a43ace VG |
3567 | } |
3568 | ||
0e2d2aaa | 3569 | static inline void add_tg_cfs_propagate(struct cfs_rq *cfs_rq, long runnable_sum) |
09a43ace | 3570 | { |
0e2d2aaa PZ |
3571 | cfs_rq->propagate = 1; |
3572 | cfs_rq->prop_runnable_sum += runnable_sum; | |
09a43ace VG |
3573 | } |
3574 | ||
3575 | /* Update task and its cfs_rq load average */ | |
3576 | static inline int propagate_entity_load_avg(struct sched_entity *se) | |
3577 | { | |
0e2d2aaa | 3578 | struct cfs_rq *cfs_rq, *gcfs_rq; |
09a43ace VG |
3579 | |
3580 | if (entity_is_task(se)) | |
3581 | return 0; | |
3582 | ||
0e2d2aaa PZ |
3583 | gcfs_rq = group_cfs_rq(se); |
3584 | if (!gcfs_rq->propagate) | |
09a43ace VG |
3585 | return 0; |
3586 | ||
0e2d2aaa PZ |
3587 | gcfs_rq->propagate = 0; |
3588 | ||
09a43ace VG |
3589 | cfs_rq = cfs_rq_of(se); |
3590 | ||
0e2d2aaa | 3591 | add_tg_cfs_propagate(cfs_rq, gcfs_rq->prop_runnable_sum); |
09a43ace | 3592 | |
0e2d2aaa | 3593 | update_tg_cfs_util(cfs_rq, se, gcfs_rq); |
9f683953 | 3594 | update_tg_cfs_runnable(cfs_rq, se, gcfs_rq); |
0dacee1b | 3595 | update_tg_cfs_load(cfs_rq, se, gcfs_rq); |
09a43ace | 3596 | |
ba19f51f | 3597 | trace_pelt_cfs_tp(cfs_rq); |
8de6242c | 3598 | trace_pelt_se_tp(se); |
ba19f51f | 3599 | |
09a43ace VG |
3600 | return 1; |
3601 | } | |
3602 | ||
bc427898 VG |
3603 | /* |
3604 | * Check if we need to update the load and the utilization of a blocked | |
3605 | * group_entity: | |
3606 | */ | |
3607 | static inline bool skip_blocked_update(struct sched_entity *se) | |
3608 | { | |
3609 | struct cfs_rq *gcfs_rq = group_cfs_rq(se); | |
3610 | ||
3611 | /* | |
3612 | * If sched_entity still have not zero load or utilization, we have to | |
3613 | * decay it: | |
3614 | */ | |
3615 | if (se->avg.load_avg || se->avg.util_avg) | |
3616 | return false; | |
3617 | ||
3618 | /* | |
3619 | * If there is a pending propagation, we have to update the load and | |
3620 | * the utilization of the sched_entity: | |
3621 | */ | |
0e2d2aaa | 3622 | if (gcfs_rq->propagate) |
bc427898 VG |
3623 | return false; |
3624 | ||
3625 | /* | |
3626 | * Otherwise, the load and the utilization of the sched_entity is | |
3627 | * already zero and there is no pending propagation, so it will be a | |
3628 | * waste of time to try to decay it: | |
3629 | */ | |
3630 | return true; | |
3631 | } | |
3632 | ||
6e83125c | 3633 | #else /* CONFIG_FAIR_GROUP_SCHED */ |
09a43ace | 3634 | |
fe749158 | 3635 | static inline void update_tg_load_avg(struct cfs_rq *cfs_rq) {} |
09a43ace VG |
3636 | |
3637 | static inline int propagate_entity_load_avg(struct sched_entity *se) | |
3638 | { | |
3639 | return 0; | |
3640 | } | |
3641 | ||
0e2d2aaa | 3642 | static inline void add_tg_cfs_propagate(struct cfs_rq *cfs_rq, long runnable_sum) {} |
09a43ace | 3643 | |
6e83125c | 3644 | #endif /* CONFIG_FAIR_GROUP_SCHED */ |
c566e8e9 | 3645 | |
3d30544f PZ |
3646 | /** |
3647 | * update_cfs_rq_load_avg - update the cfs_rq's load/util averages | |
23127296 | 3648 | * @now: current time, as per cfs_rq_clock_pelt() |
3d30544f | 3649 | * @cfs_rq: cfs_rq to update |
3d30544f PZ |
3650 | * |
3651 | * The cfs_rq avg is the direct sum of all its entities (blocked and runnable) | |
3652 | * avg. The immediate corollary is that all (fair) tasks must be attached, see | |
3653 | * post_init_entity_util_avg(). | |
3654 | * | |
3655 | * cfs_rq->avg is used for task_h_load() and update_cfs_share() for example. | |
3656 | * | |
7c3edd2c PZ |
3657 | * Returns true if the load decayed or we removed load. |
3658 | * | |
3659 | * Since both these conditions indicate a changed cfs_rq->avg.load we should | |
3660 | * call update_tg_load_avg() when this function returns true. | |
3d30544f | 3661 | */ |
a2c6c91f | 3662 | static inline int |
3a123bbb | 3663 | update_cfs_rq_load_avg(u64 now, struct cfs_rq *cfs_rq) |
2dac754e | 3664 | { |
9f683953 | 3665 | unsigned long removed_load = 0, removed_util = 0, removed_runnable = 0; |
9d89c257 | 3666 | struct sched_avg *sa = &cfs_rq->avg; |
2a2f5d4e | 3667 | int decayed = 0; |
2dac754e | 3668 | |
2a2f5d4e PZ |
3669 | if (cfs_rq->removed.nr) { |
3670 | unsigned long r; | |
87e867b4 | 3671 | u32 divider = get_pelt_divider(&cfs_rq->avg); |
2a2f5d4e PZ |
3672 | |
3673 | raw_spin_lock(&cfs_rq->removed.lock); | |
3674 | swap(cfs_rq->removed.util_avg, removed_util); | |
3675 | swap(cfs_rq->removed.load_avg, removed_load); | |
9f683953 | 3676 | swap(cfs_rq->removed.runnable_avg, removed_runnable); |
2a2f5d4e PZ |
3677 | cfs_rq->removed.nr = 0; |
3678 | raw_spin_unlock(&cfs_rq->removed.lock); | |
3679 | ||
2a2f5d4e | 3680 | r = removed_load; |
89741892 | 3681 | sub_positive(&sa->load_avg, r); |
1c35b07e | 3682 | sa->load_sum = sa->load_avg * divider; |
2dac754e | 3683 | |
2a2f5d4e | 3684 | r = removed_util; |
89741892 | 3685 | sub_positive(&sa->util_avg, r); |
1c35b07e | 3686 | sa->util_sum = sa->util_avg * divider; |
2a2f5d4e | 3687 | |
9f683953 VG |
3688 | r = removed_runnable; |
3689 | sub_positive(&sa->runnable_avg, r); | |
1c35b07e | 3690 | sa->runnable_sum = sa->runnable_avg * divider; |
9f683953 VG |
3691 | |
3692 | /* | |
3693 | * removed_runnable is the unweighted version of removed_load so we | |
3694 | * can use it to estimate removed_load_sum. | |
3695 | */ | |
3696 | add_tg_cfs_propagate(cfs_rq, | |
3697 | -(long)(removed_runnable * divider) >> SCHED_CAPACITY_SHIFT); | |
2a2f5d4e PZ |
3698 | |
3699 | decayed = 1; | |
9d89c257 | 3700 | } |
36ee28e4 | 3701 | |
23127296 | 3702 | decayed |= __update_load_avg_cfs_rq(now, cfs_rq); |
36ee28e4 | 3703 | |
9d89c257 YD |
3704 | #ifndef CONFIG_64BIT |
3705 | smp_wmb(); | |
3706 | cfs_rq->load_last_update_time_copy = sa->last_update_time; | |
3707 | #endif | |
36ee28e4 | 3708 | |
2a2f5d4e | 3709 | return decayed; |
21e96f88 SM |
3710 | } |
3711 | ||
3d30544f PZ |
3712 | /** |
3713 | * attach_entity_load_avg - attach this entity to its cfs_rq load avg | |
3714 | * @cfs_rq: cfs_rq to attach to | |
3715 | * @se: sched_entity to attach | |
3716 | * | |
3717 | * Must call update_cfs_rq_load_avg() before this, since we rely on | |
3718 | * cfs_rq->avg.last_update_time being current. | |
3719 | */ | |
a4f9a0e5 | 3720 | static void attach_entity_load_avg(struct cfs_rq *cfs_rq, struct sched_entity *se) |
a05e8c51 | 3721 | { |
95d68593 VG |
3722 | /* |
3723 | * cfs_rq->avg.period_contrib can be used for both cfs_rq and se. | |
3724 | * See ___update_load_avg() for details. | |
3725 | */ | |
87e867b4 | 3726 | u32 divider = get_pelt_divider(&cfs_rq->avg); |
f207934f PZ |
3727 | |
3728 | /* | |
3729 | * When we attach the @se to the @cfs_rq, we must align the decay | |
3730 | * window because without that, really weird and wonderful things can | |
3731 | * happen. | |
3732 | * | |
3733 | * XXX illustrate | |
3734 | */ | |
a05e8c51 | 3735 | se->avg.last_update_time = cfs_rq->avg.last_update_time; |
f207934f PZ |
3736 | se->avg.period_contrib = cfs_rq->avg.period_contrib; |
3737 | ||
3738 | /* | |
3739 | * Hell(o) Nasty stuff.. we need to recompute _sum based on the new | |
3740 | * period_contrib. This isn't strictly correct, but since we're | |
3741 | * entirely outside of the PELT hierarchy, nobody cares if we truncate | |
3742 | * _sum a little. | |
3743 | */ | |
3744 | se->avg.util_sum = se->avg.util_avg * divider; | |
3745 | ||
9f683953 VG |
3746 | se->avg.runnable_sum = se->avg.runnable_avg * divider; |
3747 | ||
f207934f PZ |
3748 | se->avg.load_sum = divider; |
3749 | if (se_weight(se)) { | |
3750 | se->avg.load_sum = | |
3751 | div_u64(se->avg.load_avg * se->avg.load_sum, se_weight(se)); | |
3752 | } | |
3753 | ||
8d5b9025 | 3754 | enqueue_load_avg(cfs_rq, se); |
a05e8c51 BP |
3755 | cfs_rq->avg.util_avg += se->avg.util_avg; |
3756 | cfs_rq->avg.util_sum += se->avg.util_sum; | |
9f683953 VG |
3757 | cfs_rq->avg.runnable_avg += se->avg.runnable_avg; |
3758 | cfs_rq->avg.runnable_sum += se->avg.runnable_sum; | |
0e2d2aaa PZ |
3759 | |
3760 | add_tg_cfs_propagate(cfs_rq, se->avg.load_sum); | |
a2c6c91f | 3761 | |
a4f9a0e5 | 3762 | cfs_rq_util_change(cfs_rq, 0); |
ba19f51f QY |
3763 | |
3764 | trace_pelt_cfs_tp(cfs_rq); | |
a05e8c51 BP |
3765 | } |
3766 | ||
3d30544f PZ |
3767 | /** |
3768 | * detach_entity_load_avg - detach this entity from its cfs_rq load avg | |
3769 | * @cfs_rq: cfs_rq to detach from | |
3770 | * @se: sched_entity to detach | |
3771 | * | |
3772 | * Must call update_cfs_rq_load_avg() before this, since we rely on | |
3773 | * cfs_rq->avg.last_update_time being current. | |
3774 | */ | |
a05e8c51 BP |
3775 | static void detach_entity_load_avg(struct cfs_rq *cfs_rq, struct sched_entity *se) |
3776 | { | |
fcf6631f VG |
3777 | /* |
3778 | * cfs_rq->avg.period_contrib can be used for both cfs_rq and se. | |
3779 | * See ___update_load_avg() for details. | |
3780 | */ | |
3781 | u32 divider = get_pelt_divider(&cfs_rq->avg); | |
3782 | ||
8d5b9025 | 3783 | dequeue_load_avg(cfs_rq, se); |
89741892 | 3784 | sub_positive(&cfs_rq->avg.util_avg, se->avg.util_avg); |
fcf6631f | 3785 | cfs_rq->avg.util_sum = cfs_rq->avg.util_avg * divider; |
9f683953 | 3786 | sub_positive(&cfs_rq->avg.runnable_avg, se->avg.runnable_avg); |
fcf6631f | 3787 | cfs_rq->avg.runnable_sum = cfs_rq->avg.runnable_avg * divider; |
0e2d2aaa PZ |
3788 | |
3789 | add_tg_cfs_propagate(cfs_rq, -se->avg.load_sum); | |
a2c6c91f | 3790 | |
ea14b57e | 3791 | cfs_rq_util_change(cfs_rq, 0); |
ba19f51f QY |
3792 | |
3793 | trace_pelt_cfs_tp(cfs_rq); | |
a05e8c51 BP |
3794 | } |
3795 | ||
b382a531 PZ |
3796 | /* |
3797 | * Optional action to be done while updating the load average | |
3798 | */ | |
3799 | #define UPDATE_TG 0x1 | |
3800 | #define SKIP_AGE_LOAD 0x2 | |
3801 | #define DO_ATTACH 0x4 | |
3802 | ||
3803 | /* Update task and its cfs_rq load average */ | |
3804 | static inline void update_load_avg(struct cfs_rq *cfs_rq, struct sched_entity *se, int flags) | |
3805 | { | |
23127296 | 3806 | u64 now = cfs_rq_clock_pelt(cfs_rq); |
b382a531 PZ |
3807 | int decayed; |
3808 | ||
3809 | /* | |
3810 | * Track task load average for carrying it to new CPU after migrated, and | |
3811 | * track group sched_entity load average for task_h_load calc in migration | |
3812 | */ | |
3813 | if (se->avg.last_update_time && !(flags & SKIP_AGE_LOAD)) | |
23127296 | 3814 | __update_load_avg_se(now, cfs_rq, se); |
b382a531 PZ |
3815 | |
3816 | decayed = update_cfs_rq_load_avg(now, cfs_rq); | |
3817 | decayed |= propagate_entity_load_avg(se); | |
3818 | ||
3819 | if (!se->avg.last_update_time && (flags & DO_ATTACH)) { | |
3820 | ||
ea14b57e PZ |
3821 | /* |
3822 | * DO_ATTACH means we're here from enqueue_entity(). | |
3823 | * !last_update_time means we've passed through | |
3824 | * migrate_task_rq_fair() indicating we migrated. | |
3825 | * | |
3826 | * IOW we're enqueueing a task on a new CPU. | |
3827 | */ | |
a4f9a0e5 | 3828 | attach_entity_load_avg(cfs_rq, se); |
fe749158 | 3829 | update_tg_load_avg(cfs_rq); |
b382a531 | 3830 | |
bef69dd8 VG |
3831 | } else if (decayed) { |
3832 | cfs_rq_util_change(cfs_rq, 0); | |
3833 | ||
3834 | if (flags & UPDATE_TG) | |
fe749158 | 3835 | update_tg_load_avg(cfs_rq); |
bef69dd8 | 3836 | } |
b382a531 PZ |
3837 | } |
3838 | ||
9d89c257 | 3839 | #ifndef CONFIG_64BIT |
0905f04e YD |
3840 | static inline u64 cfs_rq_last_update_time(struct cfs_rq *cfs_rq) |
3841 | { | |
9d89c257 | 3842 | u64 last_update_time_copy; |
0905f04e | 3843 | u64 last_update_time; |
9ee474f5 | 3844 | |
9d89c257 YD |
3845 | do { |
3846 | last_update_time_copy = cfs_rq->load_last_update_time_copy; | |
3847 | smp_rmb(); | |
3848 | last_update_time = cfs_rq->avg.last_update_time; | |
3849 | } while (last_update_time != last_update_time_copy); | |
0905f04e YD |
3850 | |
3851 | return last_update_time; | |
3852 | } | |
9d89c257 | 3853 | #else |
0905f04e YD |
3854 | static inline u64 cfs_rq_last_update_time(struct cfs_rq *cfs_rq) |
3855 | { | |
3856 | return cfs_rq->avg.last_update_time; | |
3857 | } | |
9d89c257 YD |
3858 | #endif |
3859 | ||
104cb16d MR |
3860 | /* |
3861 | * Synchronize entity load avg of dequeued entity without locking | |
3862 | * the previous rq. | |
3863 | */ | |
71b47eaf | 3864 | static void sync_entity_load_avg(struct sched_entity *se) |
104cb16d MR |
3865 | { |
3866 | struct cfs_rq *cfs_rq = cfs_rq_of(se); | |
3867 | u64 last_update_time; | |
3868 | ||
3869 | last_update_time = cfs_rq_last_update_time(cfs_rq); | |
23127296 | 3870 | __update_load_avg_blocked_se(last_update_time, se); |
104cb16d MR |
3871 | } |
3872 | ||
0905f04e YD |
3873 | /* |
3874 | * Task first catches up with cfs_rq, and then subtract | |
3875 | * itself from the cfs_rq (task must be off the queue now). | |
3876 | */ | |
71b47eaf | 3877 | static void remove_entity_load_avg(struct sched_entity *se) |
0905f04e YD |
3878 | { |
3879 | struct cfs_rq *cfs_rq = cfs_rq_of(se); | |
2a2f5d4e | 3880 | unsigned long flags; |
0905f04e YD |
3881 | |
3882 | /* | |
7dc603c9 PZ |
3883 | * tasks cannot exit without having gone through wake_up_new_task() -> |
3884 | * post_init_entity_util_avg() which will have added things to the | |
3885 | * cfs_rq, so we can remove unconditionally. | |
0905f04e | 3886 | */ |
0905f04e | 3887 | |
104cb16d | 3888 | sync_entity_load_avg(se); |
2a2f5d4e PZ |
3889 | |
3890 | raw_spin_lock_irqsave(&cfs_rq->removed.lock, flags); | |
3891 | ++cfs_rq->removed.nr; | |
3892 | cfs_rq->removed.util_avg += se->avg.util_avg; | |
3893 | cfs_rq->removed.load_avg += se->avg.load_avg; | |
9f683953 | 3894 | cfs_rq->removed.runnable_avg += se->avg.runnable_avg; |
2a2f5d4e | 3895 | raw_spin_unlock_irqrestore(&cfs_rq->removed.lock, flags); |
2dac754e | 3896 | } |
642dbc39 | 3897 | |
9f683953 VG |
3898 | static inline unsigned long cfs_rq_runnable_avg(struct cfs_rq *cfs_rq) |
3899 | { | |
3900 | return cfs_rq->avg.runnable_avg; | |
3901 | } | |
3902 | ||
7ea241af YD |
3903 | static inline unsigned long cfs_rq_load_avg(struct cfs_rq *cfs_rq) |
3904 | { | |
3905 | return cfs_rq->avg.load_avg; | |
3906 | } | |
3907 | ||
d91cecc1 CY |
3908 | static int newidle_balance(struct rq *this_rq, struct rq_flags *rf); |
3909 | ||
7f65ea42 PB |
3910 | static inline unsigned long task_util(struct task_struct *p) |
3911 | { | |
3912 | return READ_ONCE(p->se.avg.util_avg); | |
3913 | } | |
3914 | ||
3915 | static inline unsigned long _task_util_est(struct task_struct *p) | |
3916 | { | |
3917 | struct util_est ue = READ_ONCE(p->se.avg.util_est); | |
3918 | ||
68d7a190 | 3919 | return max(ue.ewma, (ue.enqueued & ~UTIL_AVG_UNCHANGED)); |
7f65ea42 PB |
3920 | } |
3921 | ||
3922 | static inline unsigned long task_util_est(struct task_struct *p) | |
3923 | { | |
3924 | return max(task_util(p), _task_util_est(p)); | |
3925 | } | |
3926 | ||
a7008c07 VS |
3927 | #ifdef CONFIG_UCLAMP_TASK |
3928 | static inline unsigned long uclamp_task_util(struct task_struct *p) | |
3929 | { | |
3930 | return clamp(task_util_est(p), | |
3931 | uclamp_eff_value(p, UCLAMP_MIN), | |
3932 | uclamp_eff_value(p, UCLAMP_MAX)); | |
3933 | } | |
3934 | #else | |
3935 | static inline unsigned long uclamp_task_util(struct task_struct *p) | |
3936 | { | |
3937 | return task_util_est(p); | |
3938 | } | |
3939 | #endif | |
3940 | ||
7f65ea42 PB |
3941 | static inline void util_est_enqueue(struct cfs_rq *cfs_rq, |
3942 | struct task_struct *p) | |
3943 | { | |
3944 | unsigned int enqueued; | |
3945 | ||
3946 | if (!sched_feat(UTIL_EST)) | |
3947 | return; | |
3948 | ||
3949 | /* Update root cfs_rq's estimated utilization */ | |
3950 | enqueued = cfs_rq->avg.util_est.enqueued; | |
92a801e5 | 3951 | enqueued += _task_util_est(p); |
7f65ea42 | 3952 | WRITE_ONCE(cfs_rq->avg.util_est.enqueued, enqueued); |
4581bea8 VD |
3953 | |
3954 | trace_sched_util_est_cfs_tp(cfs_rq); | |
7f65ea42 PB |
3955 | } |
3956 | ||
8c1f560c XY |
3957 | static inline void util_est_dequeue(struct cfs_rq *cfs_rq, |
3958 | struct task_struct *p) | |
3959 | { | |
3960 | unsigned int enqueued; | |
3961 | ||
3962 | if (!sched_feat(UTIL_EST)) | |
3963 | return; | |
3964 | ||
3965 | /* Update root cfs_rq's estimated utilization */ | |
3966 | enqueued = cfs_rq->avg.util_est.enqueued; | |
3967 | enqueued -= min_t(unsigned int, enqueued, _task_util_est(p)); | |
3968 | WRITE_ONCE(cfs_rq->avg.util_est.enqueued, enqueued); | |
3969 | ||
3970 | trace_sched_util_est_cfs_tp(cfs_rq); | |
3971 | } | |
3972 | ||
b89997aa VD |
3973 | #define UTIL_EST_MARGIN (SCHED_CAPACITY_SCALE / 100) |
3974 | ||
7f65ea42 PB |
3975 | /* |
3976 | * Check if a (signed) value is within a specified (unsigned) margin, | |
3977 | * based on the observation that: | |
3978 | * | |
3979 | * abs(x) < y := (unsigned)(x + y - 1) < (2 * y - 1) | |
3980 | * | |
3b03706f | 3981 | * NOTE: this only works when value + margin < INT_MAX. |
7f65ea42 PB |
3982 | */ |
3983 | static inline bool within_margin(int value, int margin) | |
3984 | { | |
3985 | return ((unsigned int)(value + margin - 1) < (2 * margin - 1)); | |
3986 | } | |
3987 | ||
8c1f560c XY |
3988 | static inline void util_est_update(struct cfs_rq *cfs_rq, |
3989 | struct task_struct *p, | |
3990 | bool task_sleep) | |
7f65ea42 | 3991 | { |
b89997aa | 3992 | long last_ewma_diff, last_enqueued_diff; |
7f65ea42 PB |
3993 | struct util_est ue; |
3994 | ||
3995 | if (!sched_feat(UTIL_EST)) | |
3996 | return; | |
3997 | ||
7f65ea42 PB |
3998 | /* |
3999 | * Skip update of task's estimated utilization when the task has not | |
4000 | * yet completed an activation, e.g. being migrated. | |
4001 | */ | |
4002 | if (!task_sleep) | |
4003 | return; | |
4004 | ||
d519329f PB |
4005 | /* |
4006 | * If the PELT values haven't changed since enqueue time, | |
4007 | * skip the util_est update. | |
4008 | */ | |
4009 | ue = p->se.avg.util_est; | |
4010 | if (ue.enqueued & UTIL_AVG_UNCHANGED) | |
4011 | return; | |
4012 | ||
b89997aa VD |
4013 | last_enqueued_diff = ue.enqueued; |
4014 | ||
b8c96361 PB |
4015 | /* |
4016 | * Reset EWMA on utilization increases, the moving average is used only | |
4017 | * to smooth utilization decreases. | |
4018 | */ | |
68d7a190 | 4019 | ue.enqueued = task_util(p); |
b8c96361 PB |
4020 | if (sched_feat(UTIL_EST_FASTUP)) { |
4021 | if (ue.ewma < ue.enqueued) { | |
4022 | ue.ewma = ue.enqueued; | |
4023 | goto done; | |
4024 | } | |
4025 | } | |
4026 | ||
7f65ea42 | 4027 | /* |
b89997aa | 4028 | * Skip update of task's estimated utilization when its members are |
7f65ea42 PB |
4029 | * already ~1% close to its last activation value. |
4030 | */ | |
7f65ea42 | 4031 | last_ewma_diff = ue.enqueued - ue.ewma; |
b89997aa VD |
4032 | last_enqueued_diff -= ue.enqueued; |
4033 | if (within_margin(last_ewma_diff, UTIL_EST_MARGIN)) { | |
4034 | if (!within_margin(last_enqueued_diff, UTIL_EST_MARGIN)) | |
4035 | goto done; | |
4036 | ||
7f65ea42 | 4037 | return; |
b89997aa | 4038 | } |
7f65ea42 | 4039 | |
10a35e68 VG |
4040 | /* |
4041 | * To avoid overestimation of actual task utilization, skip updates if | |
4042 | * we cannot grant there is idle time in this CPU. | |
4043 | */ | |
8c1f560c | 4044 | if (task_util(p) > capacity_orig_of(cpu_of(rq_of(cfs_rq)))) |
10a35e68 VG |
4045 | return; |
4046 | ||
7f65ea42 PB |
4047 | /* |
4048 | * Update Task's estimated utilization | |
4049 | * | |
4050 | * When *p completes an activation we can consolidate another sample | |
4051 | * of the task size. This is done by storing the current PELT value | |
4052 | * as ue.enqueued and by using this value to update the Exponential | |
4053 | * Weighted Moving Average (EWMA): | |
4054 | * | |
4055 | * ewma(t) = w * task_util(p) + (1-w) * ewma(t-1) | |
4056 | * = w * task_util(p) + ewma(t-1) - w * ewma(t-1) | |
4057 | * = w * (task_util(p) - ewma(t-1)) + ewma(t-1) | |
4058 | * = w * ( last_ewma_diff ) + ewma(t-1) | |
4059 | * = w * (last_ewma_diff + ewma(t-1) / w) | |
4060 | * | |
4061 | * Where 'w' is the weight of new samples, which is configured to be | |
4062 | * 0.25, thus making w=1/4 ( >>= UTIL_EST_WEIGHT_SHIFT) | |
4063 | */ | |
4064 | ue.ewma <<= UTIL_EST_WEIGHT_SHIFT; | |
4065 | ue.ewma += last_ewma_diff; | |
4066 | ue.ewma >>= UTIL_EST_WEIGHT_SHIFT; | |
b8c96361 | 4067 | done: |
68d7a190 | 4068 | ue.enqueued |= UTIL_AVG_UNCHANGED; |
7f65ea42 | 4069 | WRITE_ONCE(p->se.avg.util_est, ue); |
4581bea8 VD |
4070 | |
4071 | trace_sched_util_est_se_tp(&p->se); | |
7f65ea42 PB |
4072 | } |
4073 | ||
3b1baa64 MR |
4074 | static inline int task_fits_capacity(struct task_struct *p, long capacity) |
4075 | { | |
a7008c07 | 4076 | return fits_capacity(uclamp_task_util(p), capacity); |
3b1baa64 MR |
4077 | } |
4078 | ||
4079 | static inline void update_misfit_status(struct task_struct *p, struct rq *rq) | |
4080 | { | |
4081 | if (!static_branch_unlikely(&sched_asym_cpucapacity)) | |
4082 | return; | |
4083 | ||
0ae78eec | 4084 | if (!p || p->nr_cpus_allowed == 1) { |
3b1baa64 MR |
4085 | rq->misfit_task_load = 0; |
4086 | return; | |
4087 | } | |
4088 | ||
4089 | if (task_fits_capacity(p, capacity_of(cpu_of(rq)))) { | |
4090 | rq->misfit_task_load = 0; | |
4091 | return; | |
4092 | } | |
4093 | ||
01cfcde9 VG |
4094 | /* |
4095 | * Make sure that misfit_task_load will not be null even if | |
4096 | * task_h_load() returns 0. | |
4097 | */ | |
4098 | rq->misfit_task_load = max_t(unsigned long, task_h_load(p), 1); | |
3b1baa64 MR |
4099 | } |
4100 | ||
38033c37 PZ |
4101 | #else /* CONFIG_SMP */ |
4102 | ||
a7b359fc OU |
4103 | static inline bool cfs_rq_is_decayed(struct cfs_rq *cfs_rq) |
4104 | { | |
4105 | return true; | |
4106 | } | |
4107 | ||
d31b1a66 VG |
4108 | #define UPDATE_TG 0x0 |
4109 | #define SKIP_AGE_LOAD 0x0 | |
b382a531 | 4110 | #define DO_ATTACH 0x0 |
d31b1a66 | 4111 | |
88c0616e | 4112 | static inline void update_load_avg(struct cfs_rq *cfs_rq, struct sched_entity *se, int not_used1) |
536bd00c | 4113 | { |
ea14b57e | 4114 | cfs_rq_util_change(cfs_rq, 0); |
536bd00c RW |
4115 | } |
4116 | ||
9d89c257 | 4117 | static inline void remove_entity_load_avg(struct sched_entity *se) {} |
6e83125c | 4118 | |
a05e8c51 | 4119 | static inline void |
a4f9a0e5 | 4120 | attach_entity_load_avg(struct cfs_rq *cfs_rq, struct sched_entity *se) {} |
a05e8c51 BP |
4121 | static inline void |
4122 | detach_entity_load_avg(struct cfs_rq *cfs_rq, struct sched_entity *se) {} | |
4123 | ||
d91cecc1 | 4124 | static inline int newidle_balance(struct rq *rq, struct rq_flags *rf) |
6e83125c PZ |
4125 | { |
4126 | return 0; | |
4127 | } | |
4128 | ||
7f65ea42 PB |
4129 | static inline void |
4130 | util_est_enqueue(struct cfs_rq *cfs_rq, struct task_struct *p) {} | |
4131 | ||
4132 | static inline void | |
8c1f560c XY |
4133 | util_est_dequeue(struct cfs_rq *cfs_rq, struct task_struct *p) {} |
4134 | ||
4135 | static inline void | |
4136 | util_est_update(struct cfs_rq *cfs_rq, struct task_struct *p, | |
4137 | bool task_sleep) {} | |
3b1baa64 | 4138 | static inline void update_misfit_status(struct task_struct *p, struct rq *rq) {} |
7f65ea42 | 4139 | |
38033c37 | 4140 | #endif /* CONFIG_SMP */ |
9d85f21c | 4141 | |
ddc97297 PZ |
4142 | static void check_spread(struct cfs_rq *cfs_rq, struct sched_entity *se) |
4143 | { | |
4144 | #ifdef CONFIG_SCHED_DEBUG | |
4145 | s64 d = se->vruntime - cfs_rq->min_vruntime; | |
4146 | ||
4147 | if (d < 0) | |
4148 | d = -d; | |
4149 | ||
4150 | if (d > 3*sysctl_sched_latency) | |
ae92882e | 4151 | schedstat_inc(cfs_rq->nr_spread_over); |
ddc97297 PZ |
4152 | #endif |
4153 | } | |
4154 | ||
aeb73b04 PZ |
4155 | static void |
4156 | place_entity(struct cfs_rq *cfs_rq, struct sched_entity *se, int initial) | |
4157 | { | |
1af5f730 | 4158 | u64 vruntime = cfs_rq->min_vruntime; |
94dfb5e7 | 4159 | |
2cb8600e PZ |
4160 | /* |
4161 | * The 'current' period is already promised to the current tasks, | |
4162 | * however the extra weight of the new task will slow them down a | |
4163 | * little, place the new task so that it fits in the slot that | |
4164 | * stays open at the end. | |
4165 | */ | |
94dfb5e7 | 4166 | if (initial && sched_feat(START_DEBIT)) |
f9c0b095 | 4167 | vruntime += sched_vslice(cfs_rq, se); |
aeb73b04 | 4168 | |
a2e7a7eb | 4169 | /* sleeps up to a single latency don't count. */ |
5ca9880c | 4170 | if (!initial) { |
2cae3948 JD |
4171 | unsigned long thresh; |
4172 | ||
4173 | if (se_is_idle(se)) | |
4174 | thresh = sysctl_sched_min_granularity; | |
4175 | else | |
4176 | thresh = sysctl_sched_latency; | |
a7be37ac | 4177 | |
a2e7a7eb MG |
4178 | /* |
4179 | * Halve their sleep time's effect, to allow | |
4180 | * for a gentler effect of sleepers: | |
4181 | */ | |
4182 | if (sched_feat(GENTLE_FAIR_SLEEPERS)) | |
4183 | thresh >>= 1; | |
51e0304c | 4184 | |
a2e7a7eb | 4185 | vruntime -= thresh; |
aeb73b04 PZ |
4186 | } |
4187 | ||
b5d9d734 | 4188 | /* ensure we never gain time by being placed backwards. */ |
16c8f1c7 | 4189 | se->vruntime = max_vruntime(se->vruntime, vruntime); |
aeb73b04 PZ |
4190 | } |
4191 | ||
d3d9dc33 PT |
4192 | static void check_enqueue_throttle(struct cfs_rq *cfs_rq); |
4193 | ||
fe61468b | 4194 | static inline bool cfs_bandwidth_used(void); |
b5179ac7 PZ |
4195 | |
4196 | /* | |
4197 | * MIGRATION | |
4198 | * | |
4199 | * dequeue | |
4200 | * update_curr() | |
4201 | * update_min_vruntime() | |
4202 | * vruntime -= min_vruntime | |
4203 | * | |
4204 | * enqueue | |
4205 | * update_curr() | |
4206 | * update_min_vruntime() | |
4207 | * vruntime += min_vruntime | |
4208 | * | |
4209 | * this way the vruntime transition between RQs is done when both | |
4210 | * min_vruntime are up-to-date. | |
4211 | * | |
4212 | * WAKEUP (remote) | |
4213 | * | |
59efa0ba | 4214 | * ->migrate_task_rq_fair() (p->state == TASK_WAKING) |
b5179ac7 PZ |
4215 | * vruntime -= min_vruntime |
4216 | * | |
4217 | * enqueue | |
4218 | * update_curr() | |
4219 | * update_min_vruntime() | |
4220 | * vruntime += min_vruntime | |
4221 | * | |
4222 | * this way we don't have the most up-to-date min_vruntime on the originating | |
4223 | * CPU and an up-to-date min_vruntime on the destination CPU. | |
4224 | */ | |
4225 | ||
bf0f6f24 | 4226 | static void |
88ec22d3 | 4227 | enqueue_entity(struct cfs_rq *cfs_rq, struct sched_entity *se, int flags) |
bf0f6f24 | 4228 | { |
2f950354 PZ |
4229 | bool renorm = !(flags & ENQUEUE_WAKEUP) || (flags & ENQUEUE_MIGRATED); |
4230 | bool curr = cfs_rq->curr == se; | |
4231 | ||
88ec22d3 | 4232 | /* |
2f950354 PZ |
4233 | * If we're the current task, we must renormalise before calling |
4234 | * update_curr(). | |
88ec22d3 | 4235 | */ |
2f950354 | 4236 | if (renorm && curr) |
88ec22d3 PZ |
4237 | se->vruntime += cfs_rq->min_vruntime; |
4238 | ||
2f950354 PZ |
4239 | update_curr(cfs_rq); |
4240 | ||
bf0f6f24 | 4241 | /* |
2f950354 PZ |
4242 | * Otherwise, renormalise after, such that we're placed at the current |
4243 | * moment in time, instead of some random moment in the past. Being | |
4244 | * placed in the past could significantly boost this task to the | |
4245 | * fairness detriment of existing tasks. | |
bf0f6f24 | 4246 | */ |
2f950354 PZ |
4247 | if (renorm && !curr) |
4248 | se->vruntime += cfs_rq->min_vruntime; | |
4249 | ||
89ee048f VG |
4250 | /* |
4251 | * When enqueuing a sched_entity, we must: | |
4252 | * - Update loads to have both entity and cfs_rq synced with now. | |
9f683953 | 4253 | * - Add its load to cfs_rq->runnable_avg |
89ee048f VG |
4254 | * - For group_entity, update its weight to reflect the new share of |
4255 | * its group cfs_rq | |
4256 | * - Add its new weight to cfs_rq->load.weight | |
4257 | */ | |
b382a531 | 4258 | update_load_avg(cfs_rq, se, UPDATE_TG | DO_ATTACH); |
9f683953 | 4259 | se_update_runnable(se); |
1ea6c46a | 4260 | update_cfs_group(se); |
17bc14b7 | 4261 | account_entity_enqueue(cfs_rq, se); |
bf0f6f24 | 4262 | |
1a3d027c | 4263 | if (flags & ENQUEUE_WAKEUP) |
aeb73b04 | 4264 | place_entity(cfs_rq, se, 0); |
bf0f6f24 | 4265 | |
cb251765 | 4266 | check_schedstat_required(); |
60f2415e | 4267 | update_stats_enqueue_fair(cfs_rq, se, flags); |
4fa8d299 | 4268 | check_spread(cfs_rq, se); |
2f950354 | 4269 | if (!curr) |
83b699ed | 4270 | __enqueue_entity(cfs_rq, se); |
2069dd75 | 4271 | se->on_rq = 1; |
3d4b47b4 | 4272 | |
fe61468b VG |
4273 | /* |
4274 | * When bandwidth control is enabled, cfs might have been removed | |
4275 | * because of a parent been throttled but cfs->nr_running > 1. Try to | |
3b03706f | 4276 | * add it unconditionally. |
fe61468b VG |
4277 | */ |
4278 | if (cfs_rq->nr_running == 1 || cfs_bandwidth_used()) | |
3d4b47b4 | 4279 | list_add_leaf_cfs_rq(cfs_rq); |
fe61468b VG |
4280 | |
4281 | if (cfs_rq->nr_running == 1) | |
d3d9dc33 | 4282 | check_enqueue_throttle(cfs_rq); |
bf0f6f24 IM |
4283 | } |
4284 | ||
2c13c919 | 4285 | static void __clear_buddies_last(struct sched_entity *se) |
2002c695 | 4286 | { |
2c13c919 RR |
4287 | for_each_sched_entity(se) { |
4288 | struct cfs_rq *cfs_rq = cfs_rq_of(se); | |
f1044799 | 4289 | if (cfs_rq->last != se) |
2c13c919 | 4290 | break; |
f1044799 PZ |
4291 | |
4292 | cfs_rq->last = NULL; | |
2c13c919 RR |
4293 | } |
4294 | } | |
2002c695 | 4295 | |
2c13c919 RR |
4296 | static void __clear_buddies_next(struct sched_entity *se) |
4297 | { | |
4298 | for_each_sched_entity(se) { | |
4299 | struct cfs_rq *cfs_rq = cfs_rq_of(se); | |
f1044799 | 4300 | if (cfs_rq->next != se) |
2c13c919 | 4301 | break; |
f1044799 PZ |
4302 | |
4303 | cfs_rq->next = NULL; | |
2c13c919 | 4304 | } |
2002c695 PZ |
4305 | } |
4306 | ||
ac53db59 RR |
4307 | static void __clear_buddies_skip(struct sched_entity *se) |
4308 | { | |
4309 | for_each_sched_entity(se) { | |
4310 | struct cfs_rq *cfs_rq = cfs_rq_of(se); | |
f1044799 | 4311 | if (cfs_rq->skip != se) |
ac53db59 | 4312 | break; |
f1044799 PZ |
4313 | |
4314 | cfs_rq->skip = NULL; | |
ac53db59 RR |
4315 | } |
4316 | } | |
4317 | ||
a571bbea PZ |
4318 | static void clear_buddies(struct cfs_rq *cfs_rq, struct sched_entity *se) |
4319 | { | |
2c13c919 RR |
4320 | if (cfs_rq->last == se) |
4321 | __clear_buddies_last(se); | |
4322 | ||
4323 | if (cfs_rq->next == se) | |
4324 | __clear_buddies_next(se); | |
ac53db59 RR |
4325 | |
4326 | if (cfs_rq->skip == se) | |
4327 | __clear_buddies_skip(se); | |
a571bbea PZ |
4328 | } |
4329 | ||
6c16a6dc | 4330 | static __always_inline void return_cfs_rq_runtime(struct cfs_rq *cfs_rq); |
d8b4986d | 4331 | |
bf0f6f24 | 4332 | static void |
371fd7e7 | 4333 | dequeue_entity(struct cfs_rq *cfs_rq, struct sched_entity *se, int flags) |
bf0f6f24 | 4334 | { |
a2a2d680 DA |
4335 | /* |
4336 | * Update run-time statistics of the 'current'. | |
4337 | */ | |
4338 | update_curr(cfs_rq); | |
89ee048f VG |
4339 | |
4340 | /* | |
4341 | * When dequeuing a sched_entity, we must: | |
4342 | * - Update loads to have both entity and cfs_rq synced with now. | |
9f683953 | 4343 | * - Subtract its load from the cfs_rq->runnable_avg. |
dfcb245e | 4344 | * - Subtract its previous weight from cfs_rq->load.weight. |
89ee048f VG |
4345 | * - For group entity, update its weight to reflect the new share |
4346 | * of its group cfs_rq. | |
4347 | */ | |
88c0616e | 4348 | update_load_avg(cfs_rq, se, UPDATE_TG); |
9f683953 | 4349 | se_update_runnable(se); |
a2a2d680 | 4350 | |
60f2415e | 4351 | update_stats_dequeue_fair(cfs_rq, se, flags); |
67e9fb2a | 4352 | |
2002c695 | 4353 | clear_buddies(cfs_rq, se); |
4793241b | 4354 | |
83b699ed | 4355 | if (se != cfs_rq->curr) |
30cfdcfc | 4356 | __dequeue_entity(cfs_rq, se); |
17bc14b7 | 4357 | se->on_rq = 0; |
30cfdcfc | 4358 | account_entity_dequeue(cfs_rq, se); |
88ec22d3 PZ |
4359 | |
4360 | /* | |
b60205c7 PZ |
4361 | * Normalize after update_curr(); which will also have moved |
4362 | * min_vruntime if @se is the one holding it back. But before doing | |
4363 | * update_min_vruntime() again, which will discount @se's position and | |
4364 | * can move min_vruntime forward still more. | |
88ec22d3 | 4365 | */ |
371fd7e7 | 4366 | if (!(flags & DEQUEUE_SLEEP)) |
88ec22d3 | 4367 | se->vruntime -= cfs_rq->min_vruntime; |
1e876231 | 4368 | |
d8b4986d PT |
4369 | /* return excess runtime on last dequeue */ |
4370 | return_cfs_rq_runtime(cfs_rq); | |
4371 | ||
1ea6c46a | 4372 | update_cfs_group(se); |
b60205c7 PZ |
4373 | |
4374 | /* | |
4375 | * Now advance min_vruntime if @se was the entity holding it back, | |
4376 | * except when: DEQUEUE_SAVE && !DEQUEUE_MOVE, in this case we'll be | |
4377 | * put back on, and if we advance min_vruntime, we'll be placed back | |
4378 | * further than we started -- ie. we'll be penalized. | |
4379 | */ | |
9845c49c | 4380 | if ((flags & (DEQUEUE_SAVE | DEQUEUE_MOVE)) != DEQUEUE_SAVE) |
b60205c7 | 4381 | update_min_vruntime(cfs_rq); |
bf0f6f24 IM |
4382 | } |
4383 | ||
4384 | /* | |
4385 | * Preempt the current task with a newly woken task if needed: | |
4386 | */ | |
7c92e54f | 4387 | static void |
2e09bf55 | 4388 | check_preempt_tick(struct cfs_rq *cfs_rq, struct sched_entity *curr) |
bf0f6f24 | 4389 | { |
11697830 | 4390 | unsigned long ideal_runtime, delta_exec; |
f4cfb33e WX |
4391 | struct sched_entity *se; |
4392 | s64 delta; | |
11697830 | 4393 | |
6d0f0ebd | 4394 | ideal_runtime = sched_slice(cfs_rq, curr); |
11697830 | 4395 | delta_exec = curr->sum_exec_runtime - curr->prev_sum_exec_runtime; |
a9f3e2b5 | 4396 | if (delta_exec > ideal_runtime) { |
8875125e | 4397 | resched_curr(rq_of(cfs_rq)); |
a9f3e2b5 MG |
4398 | /* |
4399 | * The current task ran long enough, ensure it doesn't get | |
4400 | * re-elected due to buddy favours. | |
4401 | */ | |
4402 | clear_buddies(cfs_rq, curr); | |
f685ceac MG |
4403 | return; |
4404 | } | |
4405 | ||
4406 | /* | |
4407 | * Ensure that a task that missed wakeup preemption by a | |
4408 | * narrow margin doesn't have to wait for a full slice. | |
4409 | * This also mitigates buddy induced latencies under load. | |
4410 | */ | |
f685ceac MG |
4411 | if (delta_exec < sysctl_sched_min_granularity) |
4412 | return; | |
4413 | ||
f4cfb33e WX |
4414 | se = __pick_first_entity(cfs_rq); |
4415 | delta = curr->vruntime - se->vruntime; | |
f685ceac | 4416 | |
f4cfb33e WX |
4417 | if (delta < 0) |
4418 | return; | |
d7d82944 | 4419 | |
f4cfb33e | 4420 | if (delta > ideal_runtime) |
8875125e | 4421 | resched_curr(rq_of(cfs_rq)); |
bf0f6f24 IM |
4422 | } |
4423 | ||
83b699ed | 4424 | static void |
8494f412 | 4425 | set_next_entity(struct cfs_rq *cfs_rq, struct sched_entity *se) |
bf0f6f24 | 4426 | { |
21f56ffe PZ |
4427 | clear_buddies(cfs_rq, se); |
4428 | ||
83b699ed SV |
4429 | /* 'current' is not kept within the tree. */ |
4430 | if (se->on_rq) { | |
4431 | /* | |
4432 | * Any task has to be enqueued before it get to execute on | |
4433 | * a CPU. So account for the time it spent waiting on the | |
4434 | * runqueue. | |
4435 | */ | |
60f2415e | 4436 | update_stats_wait_end_fair(cfs_rq, se); |
83b699ed | 4437 | __dequeue_entity(cfs_rq, se); |
88c0616e | 4438 | update_load_avg(cfs_rq, se, UPDATE_TG); |
83b699ed SV |
4439 | } |
4440 | ||
79303e9e | 4441 | update_stats_curr_start(cfs_rq, se); |
429d43bc | 4442 | cfs_rq->curr = se; |
4fa8d299 | 4443 | |
eba1ed4b IM |
4444 | /* |
4445 | * Track our maximum slice length, if the CPU's load is at | |
4446 | * least twice that of our own weight (i.e. dont track it | |
4447 | * when there are only lesser-weight tasks around): | |
4448 | */ | |
f2bedc47 DE |
4449 | if (schedstat_enabled() && |
4450 | rq_of(cfs_rq)->cfs.load.weight >= 2*se->load.weight) { | |
ceeadb83 YS |
4451 | struct sched_statistics *stats; |
4452 | ||
4453 | stats = __schedstats_from_se(se); | |
4454 | __schedstat_set(stats->slice_max, | |
4455 | max((u64)stats->slice_max, | |
a2dcb276 | 4456 | se->sum_exec_runtime - se->prev_sum_exec_runtime)); |
eba1ed4b | 4457 | } |
4fa8d299 | 4458 | |
4a55b450 | 4459 | se->prev_sum_exec_runtime = se->sum_exec_runtime; |
bf0f6f24 IM |
4460 | } |
4461 | ||
3f3a4904 PZ |
4462 | static int |
4463 | wakeup_preempt_entity(struct sched_entity *curr, struct sched_entity *se); | |
4464 | ||
ac53db59 RR |
4465 | /* |
4466 | * Pick the next process, keeping these things in mind, in this order: | |
4467 | * 1) keep things fair between processes/task groups | |
4468 | * 2) pick the "next" process, since someone really wants that to run | |
4469 | * 3) pick the "last" process, for cache locality | |
4470 | * 4) do not run the "skip" process, if something else is available | |
4471 | */ | |
678d5718 PZ |
4472 | static struct sched_entity * |
4473 | pick_next_entity(struct cfs_rq *cfs_rq, struct sched_entity *curr) | |
aa2ac252 | 4474 | { |
678d5718 PZ |
4475 | struct sched_entity *left = __pick_first_entity(cfs_rq); |
4476 | struct sched_entity *se; | |
4477 | ||
4478 | /* | |
4479 | * If curr is set we have to see if its left of the leftmost entity | |
4480 | * still in the tree, provided there was anything in the tree at all. | |
4481 | */ | |
4482 | if (!left || (curr && entity_before(curr, left))) | |
4483 | left = curr; | |
4484 | ||
4485 | se = left; /* ideally we run the leftmost entity */ | |
f4b6755f | 4486 | |
ac53db59 RR |
4487 | /* |
4488 | * Avoid running the skip buddy, if running something else can | |
4489 | * be done without getting too unfair. | |
4490 | */ | |
21f56ffe | 4491 | if (cfs_rq->skip && cfs_rq->skip == se) { |
678d5718 PZ |
4492 | struct sched_entity *second; |
4493 | ||
4494 | if (se == curr) { | |
4495 | second = __pick_first_entity(cfs_rq); | |
4496 | } else { | |
4497 | second = __pick_next_entity(se); | |
4498 | if (!second || (curr && entity_before(curr, second))) | |
4499 | second = curr; | |
4500 | } | |
4501 | ||
ac53db59 RR |
4502 | if (second && wakeup_preempt_entity(second, left) < 1) |
4503 | se = second; | |
4504 | } | |
aa2ac252 | 4505 | |
9abb8973 PO |
4506 | if (cfs_rq->next && wakeup_preempt_entity(cfs_rq->next, left) < 1) { |
4507 | /* | |
4508 | * Someone really wants this to run. If it's not unfair, run it. | |
4509 | */ | |
ac53db59 | 4510 | se = cfs_rq->next; |
9abb8973 PO |
4511 | } else if (cfs_rq->last && wakeup_preempt_entity(cfs_rq->last, left) < 1) { |
4512 | /* | |
4513 | * Prefer last buddy, try to return the CPU to a preempted task. | |
4514 | */ | |
4515 | se = cfs_rq->last; | |
4516 | } | |
ac53db59 | 4517 | |
4793241b | 4518 | return se; |
aa2ac252 PZ |
4519 | } |
4520 | ||
678d5718 | 4521 | static bool check_cfs_rq_runtime(struct cfs_rq *cfs_rq); |
d3d9dc33 | 4522 | |
ab6cde26 | 4523 | static void put_prev_entity(struct cfs_rq *cfs_rq, struct sched_entity *prev) |
bf0f6f24 IM |
4524 | { |
4525 | /* | |
4526 | * If still on the runqueue then deactivate_task() | |
4527 | * was not called and update_curr() has to be done: | |
4528 | */ | |
4529 | if (prev->on_rq) | |
b7cc0896 | 4530 | update_curr(cfs_rq); |
bf0f6f24 | 4531 | |
d3d9dc33 PT |
4532 | /* throttle cfs_rqs exceeding runtime */ |
4533 | check_cfs_rq_runtime(cfs_rq); | |
4534 | ||
4fa8d299 | 4535 | check_spread(cfs_rq, prev); |
cb251765 | 4536 | |
30cfdcfc | 4537 | if (prev->on_rq) { |
60f2415e | 4538 | update_stats_wait_start_fair(cfs_rq, prev); |
30cfdcfc DA |
4539 | /* Put 'current' back into the tree. */ |
4540 | __enqueue_entity(cfs_rq, prev); | |
9d85f21c | 4541 | /* in !on_rq case, update occurred at dequeue */ |
88c0616e | 4542 | update_load_avg(cfs_rq, prev, 0); |
30cfdcfc | 4543 | } |
429d43bc | 4544 | cfs_rq->curr = NULL; |
bf0f6f24 IM |
4545 | } |
4546 | ||
8f4d37ec PZ |
4547 | static void |
4548 | entity_tick(struct cfs_rq *cfs_rq, struct sched_entity *curr, int queued) | |
bf0f6f24 | 4549 | { |
bf0f6f24 | 4550 | /* |
30cfdcfc | 4551 | * Update run-time statistics of the 'current'. |
bf0f6f24 | 4552 | */ |
30cfdcfc | 4553 | update_curr(cfs_rq); |
bf0f6f24 | 4554 | |
9d85f21c PT |
4555 | /* |
4556 | * Ensure that runnable average is periodically updated. | |
4557 | */ | |
88c0616e | 4558 | update_load_avg(cfs_rq, curr, UPDATE_TG); |
1ea6c46a | 4559 | update_cfs_group(curr); |
9d85f21c | 4560 | |
8f4d37ec PZ |
4561 | #ifdef CONFIG_SCHED_HRTICK |
4562 | /* | |
4563 | * queued ticks are scheduled to match the slice, so don't bother | |
4564 | * validating it and just reschedule. | |
4565 | */ | |
983ed7a6 | 4566 | if (queued) { |
8875125e | 4567 | resched_curr(rq_of(cfs_rq)); |
983ed7a6 HH |
4568 | return; |
4569 | } | |
8f4d37ec PZ |
4570 | /* |
4571 | * don't let the period tick interfere with the hrtick preemption | |
4572 | */ | |
4573 | if (!sched_feat(DOUBLE_TICK) && | |
4574 | hrtimer_active(&rq_of(cfs_rq)->hrtick_timer)) | |
4575 | return; | |
4576 | #endif | |
4577 | ||
2c2efaed | 4578 | if (cfs_rq->nr_running > 1) |
2e09bf55 | 4579 | check_preempt_tick(cfs_rq, curr); |
bf0f6f24 IM |
4580 | } |
4581 | ||
ab84d31e PT |
4582 | |
4583 | /************************************************** | |
4584 | * CFS bandwidth control machinery | |
4585 | */ | |
4586 | ||
4587 | #ifdef CONFIG_CFS_BANDWIDTH | |
029632fb | 4588 | |
e9666d10 | 4589 | #ifdef CONFIG_JUMP_LABEL |
c5905afb | 4590 | static struct static_key __cfs_bandwidth_used; |
029632fb PZ |
4591 | |
4592 | static inline bool cfs_bandwidth_used(void) | |
4593 | { | |
c5905afb | 4594 | return static_key_false(&__cfs_bandwidth_used); |
029632fb PZ |
4595 | } |
4596 | ||
1ee14e6c | 4597 | void cfs_bandwidth_usage_inc(void) |
029632fb | 4598 | { |
ce48c146 | 4599 | static_key_slow_inc_cpuslocked(&__cfs_bandwidth_used); |
1ee14e6c BS |
4600 | } |
4601 | ||
4602 | void cfs_bandwidth_usage_dec(void) | |
4603 | { | |
ce48c146 | 4604 | static_key_slow_dec_cpuslocked(&__cfs_bandwidth_used); |
029632fb | 4605 | } |
e9666d10 | 4606 | #else /* CONFIG_JUMP_LABEL */ |
029632fb PZ |
4607 | static bool cfs_bandwidth_used(void) |
4608 | { | |
4609 | return true; | |
4610 | } | |
4611 | ||
1ee14e6c BS |
4612 | void cfs_bandwidth_usage_inc(void) {} |
4613 | void cfs_bandwidth_usage_dec(void) {} | |
e9666d10 | 4614 | #endif /* CONFIG_JUMP_LABEL */ |
029632fb | 4615 | |
ab84d31e PT |
4616 | /* |
4617 | * default period for cfs group bandwidth. | |
4618 | * default: 0.1s, units: nanoseconds | |
4619 | */ | |
4620 | static inline u64 default_cfs_period(void) | |
4621 | { | |
4622 | return 100000000ULL; | |
4623 | } | |
ec12cb7f PT |
4624 | |
4625 | static inline u64 sched_cfs_bandwidth_slice(void) | |
4626 | { | |
4627 | return (u64)sysctl_sched_cfs_bandwidth_slice * NSEC_PER_USEC; | |
4628 | } | |
4629 | ||
a9cf55b2 | 4630 | /* |
763a9ec0 QC |
4631 | * Replenish runtime according to assigned quota. We use sched_clock_cpu |
4632 | * directly instead of rq->clock to avoid adding additional synchronization | |
4633 | * around rq->lock. | |
a9cf55b2 PT |
4634 | * |
4635 | * requires cfs_b->lock | |
4636 | */ | |
029632fb | 4637 | void __refill_cfs_bandwidth_runtime(struct cfs_bandwidth *cfs_b) |
a9cf55b2 | 4638 | { |
bcb1704a HC |
4639 | s64 runtime; |
4640 | ||
f4183717 HC |
4641 | if (unlikely(cfs_b->quota == RUNTIME_INF)) |
4642 | return; | |
4643 | ||
4644 | cfs_b->runtime += cfs_b->quota; | |
bcb1704a HC |
4645 | runtime = cfs_b->runtime_snap - cfs_b->runtime; |
4646 | if (runtime > 0) { | |
4647 | cfs_b->burst_time += runtime; | |
4648 | cfs_b->nr_burst++; | |
4649 | } | |
4650 | ||
f4183717 | 4651 | cfs_b->runtime = min(cfs_b->runtime, cfs_b->quota + cfs_b->burst); |
bcb1704a | 4652 | cfs_b->runtime_snap = cfs_b->runtime; |
a9cf55b2 PT |
4653 | } |
4654 | ||
029632fb PZ |
4655 | static inline struct cfs_bandwidth *tg_cfs_bandwidth(struct task_group *tg) |
4656 | { | |
4657 | return &tg->cfs_bandwidth; | |
4658 | } | |
4659 | ||
85dac906 | 4660 | /* returns 0 on failure to allocate runtime */ |
e98fa02c PT |
4661 | static int __assign_cfs_rq_runtime(struct cfs_bandwidth *cfs_b, |
4662 | struct cfs_rq *cfs_rq, u64 target_runtime) | |
ec12cb7f | 4663 | { |
e98fa02c PT |
4664 | u64 min_amount, amount = 0; |
4665 | ||
4666 | lockdep_assert_held(&cfs_b->lock); | |
ec12cb7f PT |
4667 | |
4668 | /* note: this is a positive sum as runtime_remaining <= 0 */ | |
e98fa02c | 4669 | min_amount = target_runtime - cfs_rq->runtime_remaining; |
ec12cb7f | 4670 | |
ec12cb7f PT |
4671 | if (cfs_b->quota == RUNTIME_INF) |
4672 | amount = min_amount; | |
58088ad0 | 4673 | else { |
77a4d1a1 | 4674 | start_cfs_bandwidth(cfs_b); |
58088ad0 PT |
4675 | |
4676 | if (cfs_b->runtime > 0) { | |
4677 | amount = min(cfs_b->runtime, min_amount); | |
4678 | cfs_b->runtime -= amount; | |
4679 | cfs_b->idle = 0; | |
4680 | } | |
ec12cb7f | 4681 | } |
ec12cb7f PT |
4682 | |
4683 | cfs_rq->runtime_remaining += amount; | |
85dac906 PT |
4684 | |
4685 | return cfs_rq->runtime_remaining > 0; | |
ec12cb7f PT |
4686 | } |
4687 | ||
e98fa02c PT |
4688 | /* returns 0 on failure to allocate runtime */ |
4689 | static int assign_cfs_rq_runtime(struct cfs_rq *cfs_rq) | |
4690 | { | |
4691 | struct cfs_bandwidth *cfs_b = tg_cfs_bandwidth(cfs_rq->tg); | |
4692 | int ret; | |
4693 | ||
4694 | raw_spin_lock(&cfs_b->lock); | |
4695 | ret = __assign_cfs_rq_runtime(cfs_b, cfs_rq, sched_cfs_bandwidth_slice()); | |
4696 | raw_spin_unlock(&cfs_b->lock); | |
4697 | ||
4698 | return ret; | |
4699 | } | |
4700 | ||
9dbdb155 | 4701 | static void __account_cfs_rq_runtime(struct cfs_rq *cfs_rq, u64 delta_exec) |
a9cf55b2 PT |
4702 | { |
4703 | /* dock delta_exec before expiring quota (as it could span periods) */ | |
ec12cb7f | 4704 | cfs_rq->runtime_remaining -= delta_exec; |
a9cf55b2 PT |
4705 | |
4706 | if (likely(cfs_rq->runtime_remaining > 0)) | |
ec12cb7f PT |
4707 | return; |
4708 | ||
5e2d2cc2 L |
4709 | if (cfs_rq->throttled) |
4710 | return; | |
85dac906 PT |
4711 | /* |
4712 | * if we're unable to extend our runtime we resched so that the active | |
4713 | * hierarchy can be throttled | |
4714 | */ | |
4715 | if (!assign_cfs_rq_runtime(cfs_rq) && likely(cfs_rq->curr)) | |
8875125e | 4716 | resched_curr(rq_of(cfs_rq)); |
ec12cb7f PT |
4717 | } |
4718 | ||
6c16a6dc | 4719 | static __always_inline |
9dbdb155 | 4720 | void account_cfs_rq_runtime(struct cfs_rq *cfs_rq, u64 delta_exec) |
ec12cb7f | 4721 | { |
56f570e5 | 4722 | if (!cfs_bandwidth_used() || !cfs_rq->runtime_enabled) |
ec12cb7f PT |
4723 | return; |
4724 | ||
4725 | __account_cfs_rq_runtime(cfs_rq, delta_exec); | |
4726 | } | |
4727 | ||
85dac906 PT |
4728 | static inline int cfs_rq_throttled(struct cfs_rq *cfs_rq) |
4729 | { | |
56f570e5 | 4730 | return cfs_bandwidth_used() && cfs_rq->throttled; |
85dac906 PT |
4731 | } |
4732 | ||
64660c86 PT |
4733 | /* check whether cfs_rq, or any parent, is throttled */ |
4734 | static inline int throttled_hierarchy(struct cfs_rq *cfs_rq) | |
4735 | { | |
56f570e5 | 4736 | return cfs_bandwidth_used() && cfs_rq->throttle_count; |
64660c86 PT |
4737 | } |
4738 | ||
4739 | /* | |
4740 | * Ensure that neither of the group entities corresponding to src_cpu or | |
4741 | * dest_cpu are members of a throttled hierarchy when performing group | |
4742 | * load-balance operations. | |
4743 | */ | |
4744 | static inline int throttled_lb_pair(struct task_group *tg, | |
4745 | int src_cpu, int dest_cpu) | |
4746 | { | |
4747 | struct cfs_rq *src_cfs_rq, *dest_cfs_rq; | |
4748 | ||
4749 | src_cfs_rq = tg->cfs_rq[src_cpu]; | |
4750 | dest_cfs_rq = tg->cfs_rq[dest_cpu]; | |
4751 | ||
4752 | return throttled_hierarchy(src_cfs_rq) || | |
4753 | throttled_hierarchy(dest_cfs_rq); | |
4754 | } | |
4755 | ||
64660c86 PT |
4756 | static int tg_unthrottle_up(struct task_group *tg, void *data) |
4757 | { | |
4758 | struct rq *rq = data; | |
4759 | struct cfs_rq *cfs_rq = tg->cfs_rq[cpu_of(rq)]; | |
4760 | ||
4761 | cfs_rq->throttle_count--; | |
64660c86 | 4762 | if (!cfs_rq->throttle_count) { |
78becc27 | 4763 | cfs_rq->throttled_clock_task_time += rq_clock_task(rq) - |
f1b17280 | 4764 | cfs_rq->throttled_clock_task; |
31bc6aea | 4765 | |
a7b359fc OU |
4766 | /* Add cfs_rq with load or one or more already running entities to the list */ |
4767 | if (!cfs_rq_is_decayed(cfs_rq) || cfs_rq->nr_running) | |
31bc6aea | 4768 | list_add_leaf_cfs_rq(cfs_rq); |
64660c86 | 4769 | } |
64660c86 PT |
4770 | |
4771 | return 0; | |
4772 | } | |
4773 | ||
4774 | static int tg_throttle_down(struct task_group *tg, void *data) | |
4775 | { | |
4776 | struct rq *rq = data; | |
4777 | struct cfs_rq *cfs_rq = tg->cfs_rq[cpu_of(rq)]; | |
4778 | ||
82958366 | 4779 | /* group is entering throttled state, stop time */ |
31bc6aea | 4780 | if (!cfs_rq->throttle_count) { |
78becc27 | 4781 | cfs_rq->throttled_clock_task = rq_clock_task(rq); |
31bc6aea VG |
4782 | list_del_leaf_cfs_rq(cfs_rq); |
4783 | } | |
64660c86 PT |
4784 | cfs_rq->throttle_count++; |
4785 | ||
4786 | return 0; | |
4787 | } | |
4788 | ||
e98fa02c | 4789 | static bool throttle_cfs_rq(struct cfs_rq *cfs_rq) |
85dac906 PT |
4790 | { |
4791 | struct rq *rq = rq_of(cfs_rq); | |
4792 | struct cfs_bandwidth *cfs_b = tg_cfs_bandwidth(cfs_rq->tg); | |
4793 | struct sched_entity *se; | |
43e9f7f2 | 4794 | long task_delta, idle_task_delta, dequeue = 1; |
e98fa02c PT |
4795 | |
4796 | raw_spin_lock(&cfs_b->lock); | |
4797 | /* This will start the period timer if necessary */ | |
4798 | if (__assign_cfs_rq_runtime(cfs_b, cfs_rq, 1)) { | |
4799 | /* | |
4800 | * We have raced with bandwidth becoming available, and if we | |
4801 | * actually throttled the timer might not unthrottle us for an | |
4802 | * entire period. We additionally needed to make sure that any | |
4803 | * subsequent check_cfs_rq_runtime calls agree not to throttle | |
4804 | * us, as we may commit to do cfs put_prev+pick_next, so we ask | |
4805 | * for 1ns of runtime rather than just check cfs_b. | |
4806 | */ | |
4807 | dequeue = 0; | |
4808 | } else { | |
4809 | list_add_tail_rcu(&cfs_rq->throttled_list, | |
4810 | &cfs_b->throttled_cfs_rq); | |
4811 | } | |
4812 | raw_spin_unlock(&cfs_b->lock); | |
4813 | ||
4814 | if (!dequeue) | |
4815 | return false; /* Throttle no longer required. */ | |
85dac906 PT |
4816 | |
4817 | se = cfs_rq->tg->se[cpu_of(rq_of(cfs_rq))]; | |
4818 | ||
f1b17280 | 4819 | /* freeze hierarchy runnable averages while throttled */ |
64660c86 PT |
4820 | rcu_read_lock(); |
4821 | walk_tg_tree_from(cfs_rq->tg, tg_throttle_down, tg_nop, (void *)rq); | |
4822 | rcu_read_unlock(); | |
85dac906 PT |
4823 | |
4824 | task_delta = cfs_rq->h_nr_running; | |
43e9f7f2 | 4825 | idle_task_delta = cfs_rq->idle_h_nr_running; |
85dac906 PT |
4826 | for_each_sched_entity(se) { |
4827 | struct cfs_rq *qcfs_rq = cfs_rq_of(se); | |
4828 | /* throttled entity or throttle-on-deactivate */ | |
4829 | if (!se->on_rq) | |
b6d37a76 | 4830 | goto done; |
85dac906 | 4831 | |
b6d37a76 | 4832 | dequeue_entity(qcfs_rq, se, DEQUEUE_SLEEP); |
6212437f | 4833 | |
30400039 JD |
4834 | if (cfs_rq_is_idle(group_cfs_rq(se))) |
4835 | idle_task_delta = cfs_rq->h_nr_running; | |
4836 | ||
85dac906 | 4837 | qcfs_rq->h_nr_running -= task_delta; |
43e9f7f2 | 4838 | qcfs_rq->idle_h_nr_running -= idle_task_delta; |
85dac906 | 4839 | |
b6d37a76 PW |
4840 | if (qcfs_rq->load.weight) { |
4841 | /* Avoid re-evaluating load for this entity: */ | |
4842 | se = parent_entity(se); | |
4843 | break; | |
4844 | } | |
4845 | } | |
4846 | ||
4847 | for_each_sched_entity(se) { | |
4848 | struct cfs_rq *qcfs_rq = cfs_rq_of(se); | |
4849 | /* throttled entity or throttle-on-deactivate */ | |
4850 | if (!se->on_rq) | |
4851 | goto done; | |
4852 | ||
4853 | update_load_avg(qcfs_rq, se, 0); | |
4854 | se_update_runnable(se); | |
4855 | ||
30400039 JD |
4856 | if (cfs_rq_is_idle(group_cfs_rq(se))) |
4857 | idle_task_delta = cfs_rq->h_nr_running; | |
4858 | ||
b6d37a76 PW |
4859 | qcfs_rq->h_nr_running -= task_delta; |
4860 | qcfs_rq->idle_h_nr_running -= idle_task_delta; | |
85dac906 PT |
4861 | } |
4862 | ||
b6d37a76 PW |
4863 | /* At this point se is NULL and we are at root level*/ |
4864 | sub_nr_running(rq, task_delta); | |
85dac906 | 4865 | |
b6d37a76 | 4866 | done: |
c06f04c7 | 4867 | /* |
e98fa02c PT |
4868 | * Note: distribution will already see us throttled via the |
4869 | * throttled-list. rq->lock protects completion. | |
c06f04c7 | 4870 | */ |
e98fa02c PT |
4871 | cfs_rq->throttled = 1; |
4872 | cfs_rq->throttled_clock = rq_clock(rq); | |
4873 | return true; | |
85dac906 PT |
4874 | } |
4875 | ||
029632fb | 4876 | void unthrottle_cfs_rq(struct cfs_rq *cfs_rq) |
671fd9da PT |
4877 | { |
4878 | struct rq *rq = rq_of(cfs_rq); | |
4879 | struct cfs_bandwidth *cfs_b = tg_cfs_bandwidth(cfs_rq->tg); | |
4880 | struct sched_entity *se; | |
43e9f7f2 | 4881 | long task_delta, idle_task_delta; |
671fd9da | 4882 | |
22b958d8 | 4883 | se = cfs_rq->tg->se[cpu_of(rq)]; |
671fd9da PT |
4884 | |
4885 | cfs_rq->throttled = 0; | |
1a55af2e FW |
4886 | |
4887 | update_rq_clock(rq); | |
4888 | ||
671fd9da | 4889 | raw_spin_lock(&cfs_b->lock); |
78becc27 | 4890 | cfs_b->throttled_time += rq_clock(rq) - cfs_rq->throttled_clock; |
671fd9da PT |
4891 | list_del_rcu(&cfs_rq->throttled_list); |
4892 | raw_spin_unlock(&cfs_b->lock); | |
4893 | ||
64660c86 PT |
4894 | /* update hierarchical throttle state */ |
4895 | walk_tg_tree_from(cfs_rq->tg, tg_nop, tg_unthrottle_up, (void *)rq); | |
4896 | ||
2630cde2 MK |
4897 | /* Nothing to run but something to decay (on_list)? Complete the branch */ |
4898 | if (!cfs_rq->load.weight) { | |
4899 | if (cfs_rq->on_list) | |
4900 | goto unthrottle_throttle; | |
671fd9da | 4901 | return; |
2630cde2 | 4902 | } |
671fd9da PT |
4903 | |
4904 | task_delta = cfs_rq->h_nr_running; | |
43e9f7f2 | 4905 | idle_task_delta = cfs_rq->idle_h_nr_running; |
671fd9da | 4906 | for_each_sched_entity(se) { |
30400039 JD |
4907 | struct cfs_rq *qcfs_rq = cfs_rq_of(se); |
4908 | ||
671fd9da | 4909 | if (se->on_rq) |
39f23ce0 | 4910 | break; |
30400039 JD |
4911 | enqueue_entity(qcfs_rq, se, ENQUEUE_WAKEUP); |
4912 | ||
4913 | if (cfs_rq_is_idle(group_cfs_rq(se))) | |
4914 | idle_task_delta = cfs_rq->h_nr_running; | |
39f23ce0 | 4915 | |
30400039 JD |
4916 | qcfs_rq->h_nr_running += task_delta; |
4917 | qcfs_rq->idle_h_nr_running += idle_task_delta; | |
39f23ce0 VG |
4918 | |
4919 | /* end evaluation on encountering a throttled cfs_rq */ | |
30400039 | 4920 | if (cfs_rq_throttled(qcfs_rq)) |
39f23ce0 VG |
4921 | goto unthrottle_throttle; |
4922 | } | |
671fd9da | 4923 | |
39f23ce0 | 4924 | for_each_sched_entity(se) { |
30400039 | 4925 | struct cfs_rq *qcfs_rq = cfs_rq_of(se); |
39f23ce0 | 4926 | |
30400039 | 4927 | update_load_avg(qcfs_rq, se, UPDATE_TG); |
39f23ce0 | 4928 | se_update_runnable(se); |
6212437f | 4929 | |
30400039 JD |
4930 | if (cfs_rq_is_idle(group_cfs_rq(se))) |
4931 | idle_task_delta = cfs_rq->h_nr_running; | |
671fd9da | 4932 | |
30400039 JD |
4933 | qcfs_rq->h_nr_running += task_delta; |
4934 | qcfs_rq->idle_h_nr_running += idle_task_delta; | |
39f23ce0 VG |
4935 | |
4936 | /* end evaluation on encountering a throttled cfs_rq */ | |
30400039 | 4937 | if (cfs_rq_throttled(qcfs_rq)) |
39f23ce0 VG |
4938 | goto unthrottle_throttle; |
4939 | ||
4940 | /* | |
4941 | * One parent has been throttled and cfs_rq removed from the | |
4942 | * list. Add it back to not break the leaf list. | |
4943 | */ | |
30400039 JD |
4944 | if (throttled_hierarchy(qcfs_rq)) |
4945 | list_add_leaf_cfs_rq(qcfs_rq); | |
671fd9da PT |
4946 | } |
4947 | ||
39f23ce0 VG |
4948 | /* At this point se is NULL and we are at root level*/ |
4949 | add_nr_running(rq, task_delta); | |
671fd9da | 4950 | |
39f23ce0 | 4951 | unthrottle_throttle: |
fe61468b VG |
4952 | /* |
4953 | * The cfs_rq_throttled() breaks in the above iteration can result in | |
4954 | * incomplete leaf list maintenance, resulting in triggering the | |
4955 | * assertion below. | |
4956 | */ | |
4957 | for_each_sched_entity(se) { | |
30400039 | 4958 | struct cfs_rq *qcfs_rq = cfs_rq_of(se); |
fe61468b | 4959 | |
30400039 | 4960 | if (list_add_leaf_cfs_rq(qcfs_rq)) |
39f23ce0 | 4961 | break; |
fe61468b VG |
4962 | } |
4963 | ||
4964 | assert_list_leaf_cfs_rq(rq); | |
4965 | ||
97fb7a0a | 4966 | /* Determine whether we need to wake up potentially idle CPU: */ |
671fd9da | 4967 | if (rq->curr == rq->idle && rq->cfs.nr_running) |
8875125e | 4968 | resched_curr(rq); |
671fd9da PT |
4969 | } |
4970 | ||
26a8b127 | 4971 | static void distribute_cfs_runtime(struct cfs_bandwidth *cfs_b) |
671fd9da PT |
4972 | { |
4973 | struct cfs_rq *cfs_rq; | |
26a8b127 | 4974 | u64 runtime, remaining = 1; |
671fd9da PT |
4975 | |
4976 | rcu_read_lock(); | |
4977 | list_for_each_entry_rcu(cfs_rq, &cfs_b->throttled_cfs_rq, | |
4978 | throttled_list) { | |
4979 | struct rq *rq = rq_of(cfs_rq); | |
8a8c69c3 | 4980 | struct rq_flags rf; |
671fd9da | 4981 | |
c0ad4aa4 | 4982 | rq_lock_irqsave(rq, &rf); |
671fd9da PT |
4983 | if (!cfs_rq_throttled(cfs_rq)) |
4984 | goto next; | |
4985 | ||
5e2d2cc2 L |
4986 | /* By the above check, this should never be true */ |
4987 | SCHED_WARN_ON(cfs_rq->runtime_remaining > 0); | |
4988 | ||
26a8b127 | 4989 | raw_spin_lock(&cfs_b->lock); |
671fd9da | 4990 | runtime = -cfs_rq->runtime_remaining + 1; |
26a8b127 HC |
4991 | if (runtime > cfs_b->runtime) |
4992 | runtime = cfs_b->runtime; | |
4993 | cfs_b->runtime -= runtime; | |
4994 | remaining = cfs_b->runtime; | |
4995 | raw_spin_unlock(&cfs_b->lock); | |
671fd9da PT |
4996 | |
4997 | cfs_rq->runtime_remaining += runtime; | |
671fd9da PT |
4998 | |
4999 | /* we check whether we're throttled above */ | |
5000 | if (cfs_rq->runtime_remaining > 0) | |
5001 | unthrottle_cfs_rq(cfs_rq); | |
5002 | ||
5003 | next: | |
c0ad4aa4 | 5004 | rq_unlock_irqrestore(rq, &rf); |
671fd9da PT |
5005 | |
5006 | if (!remaining) | |
5007 | break; | |
5008 | } | |
5009 | rcu_read_unlock(); | |
671fd9da PT |
5010 | } |
5011 | ||
58088ad0 PT |
5012 | /* |
5013 | * Responsible for refilling a task_group's bandwidth and unthrottling its | |
5014 | * cfs_rqs as appropriate. If there has been no activity within the last | |
5015 | * period the timer is deactivated until scheduling resumes; cfs_b->idle is | |
5016 | * used to track this state. | |
5017 | */ | |
c0ad4aa4 | 5018 | static int do_sched_cfs_period_timer(struct cfs_bandwidth *cfs_b, int overrun, unsigned long flags) |
58088ad0 | 5019 | { |
51f2176d | 5020 | int throttled; |
58088ad0 | 5021 | |
58088ad0 PT |
5022 | /* no need to continue the timer with no bandwidth constraint */ |
5023 | if (cfs_b->quota == RUNTIME_INF) | |
51f2176d | 5024 | goto out_deactivate; |
58088ad0 | 5025 | |
671fd9da | 5026 | throttled = !list_empty(&cfs_b->throttled_cfs_rq); |
e8da1b18 | 5027 | cfs_b->nr_periods += overrun; |
671fd9da | 5028 | |
f4183717 HC |
5029 | /* Refill extra burst quota even if cfs_b->idle */ |
5030 | __refill_cfs_bandwidth_runtime(cfs_b); | |
5031 | ||
51f2176d BS |
5032 | /* |
5033 | * idle depends on !throttled (for the case of a large deficit), and if | |
5034 | * we're going inactive then everything else can be deferred | |
5035 | */ | |
5036 | if (cfs_b->idle && !throttled) | |
5037 | goto out_deactivate; | |
a9cf55b2 | 5038 | |
671fd9da PT |
5039 | if (!throttled) { |
5040 | /* mark as potentially idle for the upcoming period */ | |
5041 | cfs_b->idle = 1; | |
51f2176d | 5042 | return 0; |
671fd9da PT |
5043 | } |
5044 | ||
e8da1b18 NR |
5045 | /* account preceding periods in which throttling occurred */ |
5046 | cfs_b->nr_throttled += overrun; | |
5047 | ||
671fd9da | 5048 | /* |
26a8b127 | 5049 | * This check is repeated as we release cfs_b->lock while we unthrottle. |
671fd9da | 5050 | */ |
ab93a4bc | 5051 | while (throttled && cfs_b->runtime > 0) { |
c0ad4aa4 | 5052 | raw_spin_unlock_irqrestore(&cfs_b->lock, flags); |
671fd9da | 5053 | /* we can't nest cfs_b->lock while distributing bandwidth */ |
26a8b127 | 5054 | distribute_cfs_runtime(cfs_b); |
c0ad4aa4 | 5055 | raw_spin_lock_irqsave(&cfs_b->lock, flags); |
671fd9da PT |
5056 | |
5057 | throttled = !list_empty(&cfs_b->throttled_cfs_rq); | |
5058 | } | |
58088ad0 | 5059 | |
671fd9da PT |
5060 | /* |
5061 | * While we are ensured activity in the period following an | |
5062 | * unthrottle, this also covers the case in which the new bandwidth is | |
5063 | * insufficient to cover the existing bandwidth deficit. (Forcing the | |
5064 | * timer to remain active while there are any throttled entities.) | |
5065 | */ | |
5066 | cfs_b->idle = 0; | |
58088ad0 | 5067 | |
51f2176d BS |
5068 | return 0; |
5069 | ||
5070 | out_deactivate: | |
51f2176d | 5071 | return 1; |
58088ad0 | 5072 | } |
d3d9dc33 | 5073 | |
d8b4986d PT |
5074 | /* a cfs_rq won't donate quota below this amount */ |
5075 | static const u64 min_cfs_rq_runtime = 1 * NSEC_PER_MSEC; | |
5076 | /* minimum remaining period time to redistribute slack quota */ | |
5077 | static const u64 min_bandwidth_expiration = 2 * NSEC_PER_MSEC; | |
5078 | /* how long we wait to gather additional slack before distributing */ | |
5079 | static const u64 cfs_bandwidth_slack_period = 5 * NSEC_PER_MSEC; | |
5080 | ||
db06e78c BS |
5081 | /* |
5082 | * Are we near the end of the current quota period? | |
5083 | * | |
5084 | * Requires cfs_b->lock for hrtimer_expires_remaining to be safe against the | |
4961b6e1 | 5085 | * hrtimer base being cleared by hrtimer_start. In the case of |
db06e78c BS |
5086 | * migrate_hrtimers, base is never cleared, so we are fine. |
5087 | */ | |
d8b4986d PT |
5088 | static int runtime_refresh_within(struct cfs_bandwidth *cfs_b, u64 min_expire) |
5089 | { | |
5090 | struct hrtimer *refresh_timer = &cfs_b->period_timer; | |
72d0ad7c | 5091 | s64 remaining; |
d8b4986d PT |
5092 | |
5093 | /* if the call-back is running a quota refresh is already occurring */ | |
5094 | if (hrtimer_callback_running(refresh_timer)) | |
5095 | return 1; | |
5096 | ||
5097 | /* is a quota refresh about to occur? */ | |
5098 | remaining = ktime_to_ns(hrtimer_expires_remaining(refresh_timer)); | |
72d0ad7c | 5099 | if (remaining < (s64)min_expire) |
d8b4986d PT |
5100 | return 1; |
5101 | ||
5102 | return 0; | |
5103 | } | |
5104 | ||
5105 | static void start_cfs_slack_bandwidth(struct cfs_bandwidth *cfs_b) | |
5106 | { | |
5107 | u64 min_left = cfs_bandwidth_slack_period + min_bandwidth_expiration; | |
5108 | ||
5109 | /* if there's a quota refresh soon don't bother with slack */ | |
5110 | if (runtime_refresh_within(cfs_b, min_left)) | |
5111 | return; | |
5112 | ||
66567fcb | 5113 | /* don't push forwards an existing deferred unthrottle */ |
5114 | if (cfs_b->slack_started) | |
5115 | return; | |
5116 | cfs_b->slack_started = true; | |
5117 | ||
4cfafd30 PZ |
5118 | hrtimer_start(&cfs_b->slack_timer, |
5119 | ns_to_ktime(cfs_bandwidth_slack_period), | |
5120 | HRTIMER_MODE_REL); | |
d8b4986d PT |
5121 | } |
5122 | ||
5123 | /* we know any runtime found here is valid as update_curr() precedes return */ | |
5124 | static void __return_cfs_rq_runtime(struct cfs_rq *cfs_rq) | |
5125 | { | |
5126 | struct cfs_bandwidth *cfs_b = tg_cfs_bandwidth(cfs_rq->tg); | |
5127 | s64 slack_runtime = cfs_rq->runtime_remaining - min_cfs_rq_runtime; | |
5128 | ||
5129 | if (slack_runtime <= 0) | |
5130 | return; | |
5131 | ||
5132 | raw_spin_lock(&cfs_b->lock); | |
de53fd7a | 5133 | if (cfs_b->quota != RUNTIME_INF) { |
d8b4986d PT |
5134 | cfs_b->runtime += slack_runtime; |
5135 | ||
5136 | /* we are under rq->lock, defer unthrottling using a timer */ | |
5137 | if (cfs_b->runtime > sched_cfs_bandwidth_slice() && | |
5138 | !list_empty(&cfs_b->throttled_cfs_rq)) | |
5139 | start_cfs_slack_bandwidth(cfs_b); | |
5140 | } | |
5141 | raw_spin_unlock(&cfs_b->lock); | |
5142 | ||
5143 | /* even if it's not valid for return we don't want to try again */ | |
5144 | cfs_rq->runtime_remaining -= slack_runtime; | |
5145 | } | |
5146 | ||
5147 | static __always_inline void return_cfs_rq_runtime(struct cfs_rq *cfs_rq) | |
5148 | { | |
56f570e5 PT |
5149 | if (!cfs_bandwidth_used()) |
5150 | return; | |
5151 | ||
fccfdc6f | 5152 | if (!cfs_rq->runtime_enabled || cfs_rq->nr_running) |
d8b4986d PT |
5153 | return; |
5154 | ||
5155 | __return_cfs_rq_runtime(cfs_rq); | |
5156 | } | |
5157 | ||
5158 | /* | |
5159 | * This is done with a timer (instead of inline with bandwidth return) since | |
5160 | * it's necessary to juggle rq->locks to unthrottle their respective cfs_rqs. | |
5161 | */ | |
5162 | static void do_sched_cfs_slack_timer(struct cfs_bandwidth *cfs_b) | |
5163 | { | |
5164 | u64 runtime = 0, slice = sched_cfs_bandwidth_slice(); | |
c0ad4aa4 | 5165 | unsigned long flags; |
d8b4986d PT |
5166 | |
5167 | /* confirm we're still not at a refresh boundary */ | |
c0ad4aa4 | 5168 | raw_spin_lock_irqsave(&cfs_b->lock, flags); |
66567fcb | 5169 | cfs_b->slack_started = false; |
baa9be4f | 5170 | |
db06e78c | 5171 | if (runtime_refresh_within(cfs_b, min_bandwidth_expiration)) { |
c0ad4aa4 | 5172 | raw_spin_unlock_irqrestore(&cfs_b->lock, flags); |
d8b4986d | 5173 | return; |
db06e78c | 5174 | } |
d8b4986d | 5175 | |
c06f04c7 | 5176 | if (cfs_b->quota != RUNTIME_INF && cfs_b->runtime > slice) |
d8b4986d | 5177 | runtime = cfs_b->runtime; |
c06f04c7 | 5178 | |
c0ad4aa4 | 5179 | raw_spin_unlock_irqrestore(&cfs_b->lock, flags); |
d8b4986d PT |
5180 | |
5181 | if (!runtime) | |
5182 | return; | |
5183 | ||
26a8b127 | 5184 | distribute_cfs_runtime(cfs_b); |
d8b4986d PT |
5185 | } |
5186 | ||
d3d9dc33 PT |
5187 | /* |
5188 | * When a group wakes up we want to make sure that its quota is not already | |
5189 | * expired/exceeded, otherwise it may be allowed to steal additional ticks of | |
c034f48e | 5190 | * runtime as update_curr() throttling can not trigger until it's on-rq. |
d3d9dc33 PT |
5191 | */ |
5192 | static void check_enqueue_throttle(struct cfs_rq *cfs_rq) | |
5193 | { | |
56f570e5 PT |
5194 | if (!cfs_bandwidth_used()) |
5195 | return; | |
5196 | ||
d3d9dc33 PT |
5197 | /* an active group must be handled by the update_curr()->put() path */ |
5198 | if (!cfs_rq->runtime_enabled || cfs_rq->curr) | |
5199 | return; | |
5200 | ||
5201 | /* ensure the group is not already throttled */ | |
5202 | if (cfs_rq_throttled(cfs_rq)) | |
5203 | return; | |
5204 | ||
5205 | /* update runtime allocation */ | |
5206 | account_cfs_rq_runtime(cfs_rq, 0); | |
5207 | if (cfs_rq->runtime_remaining <= 0) | |
5208 | throttle_cfs_rq(cfs_rq); | |
5209 | } | |
5210 | ||
55e16d30 PZ |
5211 | static void sync_throttle(struct task_group *tg, int cpu) |
5212 | { | |
5213 | struct cfs_rq *pcfs_rq, *cfs_rq; | |
5214 | ||
5215 | if (!cfs_bandwidth_used()) | |
5216 | return; | |
5217 | ||
5218 | if (!tg->parent) | |
5219 | return; | |
5220 | ||
5221 | cfs_rq = tg->cfs_rq[cpu]; | |
5222 | pcfs_rq = tg->parent->cfs_rq[cpu]; | |
5223 | ||
5224 | cfs_rq->throttle_count = pcfs_rq->throttle_count; | |
b8922125 | 5225 | cfs_rq->throttled_clock_task = rq_clock_task(cpu_rq(cpu)); |
55e16d30 PZ |
5226 | } |
5227 | ||
d3d9dc33 | 5228 | /* conditionally throttle active cfs_rq's from put_prev_entity() */ |
678d5718 | 5229 | static bool check_cfs_rq_runtime(struct cfs_rq *cfs_rq) |
d3d9dc33 | 5230 | { |
56f570e5 | 5231 | if (!cfs_bandwidth_used()) |
678d5718 | 5232 | return false; |
56f570e5 | 5233 | |
d3d9dc33 | 5234 | if (likely(!cfs_rq->runtime_enabled || cfs_rq->runtime_remaining > 0)) |
678d5718 | 5235 | return false; |
d3d9dc33 PT |
5236 | |
5237 | /* | |
5238 | * it's possible for a throttled entity to be forced into a running | |
5239 | * state (e.g. set_curr_task), in this case we're finished. | |
5240 | */ | |
5241 | if (cfs_rq_throttled(cfs_rq)) | |
678d5718 | 5242 | return true; |
d3d9dc33 | 5243 | |
e98fa02c | 5244 | return throttle_cfs_rq(cfs_rq); |
d3d9dc33 | 5245 | } |
029632fb | 5246 | |
029632fb PZ |
5247 | static enum hrtimer_restart sched_cfs_slack_timer(struct hrtimer *timer) |
5248 | { | |
5249 | struct cfs_bandwidth *cfs_b = | |
5250 | container_of(timer, struct cfs_bandwidth, slack_timer); | |
77a4d1a1 | 5251 | |
029632fb PZ |
5252 | do_sched_cfs_slack_timer(cfs_b); |
5253 | ||
5254 | return HRTIMER_NORESTART; | |
5255 | } | |
5256 | ||
2e8e1922 PA |
5257 | extern const u64 max_cfs_quota_period; |
5258 | ||
029632fb PZ |
5259 | static enum hrtimer_restart sched_cfs_period_timer(struct hrtimer *timer) |
5260 | { | |
5261 | struct cfs_bandwidth *cfs_b = | |
5262 | container_of(timer, struct cfs_bandwidth, period_timer); | |
c0ad4aa4 | 5263 | unsigned long flags; |
029632fb PZ |
5264 | int overrun; |
5265 | int idle = 0; | |
2e8e1922 | 5266 | int count = 0; |
029632fb | 5267 | |
c0ad4aa4 | 5268 | raw_spin_lock_irqsave(&cfs_b->lock, flags); |
029632fb | 5269 | for (;;) { |
77a4d1a1 | 5270 | overrun = hrtimer_forward_now(timer, cfs_b->period); |
029632fb PZ |
5271 | if (!overrun) |
5272 | break; | |
5273 | ||
5a6d6a6c HC |
5274 | idle = do_sched_cfs_period_timer(cfs_b, overrun, flags); |
5275 | ||
2e8e1922 PA |
5276 | if (++count > 3) { |
5277 | u64 new, old = ktime_to_ns(cfs_b->period); | |
5278 | ||
4929a4e6 XZ |
5279 | /* |
5280 | * Grow period by a factor of 2 to avoid losing precision. | |
5281 | * Precision loss in the quota/period ratio can cause __cfs_schedulable | |
5282 | * to fail. | |
5283 | */ | |
5284 | new = old * 2; | |
5285 | if (new < max_cfs_quota_period) { | |
5286 | cfs_b->period = ns_to_ktime(new); | |
5287 | cfs_b->quota *= 2; | |
f4183717 | 5288 | cfs_b->burst *= 2; |
4929a4e6 XZ |
5289 | |
5290 | pr_warn_ratelimited( | |
5291 | "cfs_period_timer[cpu%d]: period too short, scaling up (new cfs_period_us = %lld, cfs_quota_us = %lld)\n", | |
5292 | smp_processor_id(), | |
5293 | div_u64(new, NSEC_PER_USEC), | |
5294 | div_u64(cfs_b->quota, NSEC_PER_USEC)); | |
5295 | } else { | |
5296 | pr_warn_ratelimited( | |
5297 | "cfs_period_timer[cpu%d]: period too short, but cannot scale up without losing precision (cfs_period_us = %lld, cfs_quota_us = %lld)\n", | |
5298 | smp_processor_id(), | |
5299 | div_u64(old, NSEC_PER_USEC), | |
5300 | div_u64(cfs_b->quota, NSEC_PER_USEC)); | |
5301 | } | |
2e8e1922 PA |
5302 | |
5303 | /* reset count so we don't come right back in here */ | |
5304 | count = 0; | |
5305 | } | |
029632fb | 5306 | } |
4cfafd30 PZ |
5307 | if (idle) |
5308 | cfs_b->period_active = 0; | |
c0ad4aa4 | 5309 | raw_spin_unlock_irqrestore(&cfs_b->lock, flags); |
029632fb PZ |
5310 | |
5311 | return idle ? HRTIMER_NORESTART : HRTIMER_RESTART; | |
5312 | } | |
5313 | ||
5314 | void init_cfs_bandwidth(struct cfs_bandwidth *cfs_b) | |
5315 | { | |
5316 | raw_spin_lock_init(&cfs_b->lock); | |
5317 | cfs_b->runtime = 0; | |
5318 | cfs_b->quota = RUNTIME_INF; | |
5319 | cfs_b->period = ns_to_ktime(default_cfs_period()); | |
f4183717 | 5320 | cfs_b->burst = 0; |
029632fb PZ |
5321 | |
5322 | INIT_LIST_HEAD(&cfs_b->throttled_cfs_rq); | |
4cfafd30 | 5323 | hrtimer_init(&cfs_b->period_timer, CLOCK_MONOTONIC, HRTIMER_MODE_ABS_PINNED); |
029632fb PZ |
5324 | cfs_b->period_timer.function = sched_cfs_period_timer; |
5325 | hrtimer_init(&cfs_b->slack_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL); | |
5326 | cfs_b->slack_timer.function = sched_cfs_slack_timer; | |
66567fcb | 5327 | cfs_b->slack_started = false; |
029632fb PZ |
5328 | } |
5329 | ||
5330 | static void init_cfs_rq_runtime(struct cfs_rq *cfs_rq) | |
5331 | { | |
5332 | cfs_rq->runtime_enabled = 0; | |
5333 | INIT_LIST_HEAD(&cfs_rq->throttled_list); | |
5334 | } | |
5335 | ||
77a4d1a1 | 5336 | void start_cfs_bandwidth(struct cfs_bandwidth *cfs_b) |
029632fb | 5337 | { |
4cfafd30 | 5338 | lockdep_assert_held(&cfs_b->lock); |
029632fb | 5339 | |
f1d1be8a XP |
5340 | if (cfs_b->period_active) |
5341 | return; | |
5342 | ||
5343 | cfs_b->period_active = 1; | |
763a9ec0 | 5344 | hrtimer_forward_now(&cfs_b->period_timer, cfs_b->period); |
f1d1be8a | 5345 | hrtimer_start_expires(&cfs_b->period_timer, HRTIMER_MODE_ABS_PINNED); |
029632fb PZ |
5346 | } |
5347 | ||
5348 | static void destroy_cfs_bandwidth(struct cfs_bandwidth *cfs_b) | |
5349 | { | |
7f1a169b TH |
5350 | /* init_cfs_bandwidth() was not called */ |
5351 | if (!cfs_b->throttled_cfs_rq.next) | |
5352 | return; | |
5353 | ||
029632fb PZ |
5354 | hrtimer_cancel(&cfs_b->period_timer); |
5355 | hrtimer_cancel(&cfs_b->slack_timer); | |
5356 | } | |
5357 | ||
502ce005 | 5358 | /* |
97fb7a0a | 5359 | * Both these CPU hotplug callbacks race against unregister_fair_sched_group() |
502ce005 PZ |
5360 | * |
5361 | * The race is harmless, since modifying bandwidth settings of unhooked group | |
5362 | * bits doesn't do much. | |
5363 | */ | |
5364 | ||
3b03706f | 5365 | /* cpu online callback */ |
0e59bdae KT |
5366 | static void __maybe_unused update_runtime_enabled(struct rq *rq) |
5367 | { | |
502ce005 | 5368 | struct task_group *tg; |
0e59bdae | 5369 | |
5cb9eaa3 | 5370 | lockdep_assert_rq_held(rq); |
502ce005 PZ |
5371 | |
5372 | rcu_read_lock(); | |
5373 | list_for_each_entry_rcu(tg, &task_groups, list) { | |
5374 | struct cfs_bandwidth *cfs_b = &tg->cfs_bandwidth; | |
5375 | struct cfs_rq *cfs_rq = tg->cfs_rq[cpu_of(rq)]; | |
0e59bdae KT |
5376 | |
5377 | raw_spin_lock(&cfs_b->lock); | |
5378 | cfs_rq->runtime_enabled = cfs_b->quota != RUNTIME_INF; | |
5379 | raw_spin_unlock(&cfs_b->lock); | |
5380 | } | |
502ce005 | 5381 | rcu_read_unlock(); |
0e59bdae KT |
5382 | } |
5383 | ||
502ce005 | 5384 | /* cpu offline callback */ |
38dc3348 | 5385 | static void __maybe_unused unthrottle_offline_cfs_rqs(struct rq *rq) |
029632fb | 5386 | { |
502ce005 PZ |
5387 | struct task_group *tg; |
5388 | ||
5cb9eaa3 | 5389 | lockdep_assert_rq_held(rq); |
502ce005 PZ |
5390 | |
5391 | rcu_read_lock(); | |
5392 | list_for_each_entry_rcu(tg, &task_groups, list) { | |
5393 | struct cfs_rq *cfs_rq = tg->cfs_rq[cpu_of(rq)]; | |
029632fb | 5394 | |
029632fb PZ |
5395 | if (!cfs_rq->runtime_enabled) |
5396 | continue; | |
5397 | ||
5398 | /* | |
5399 | * clock_task is not advancing so we just need to make sure | |
5400 | * there's some valid quota amount | |
5401 | */ | |
51f2176d | 5402 | cfs_rq->runtime_remaining = 1; |
0e59bdae | 5403 | /* |
97fb7a0a | 5404 | * Offline rq is schedulable till CPU is completely disabled |
0e59bdae KT |
5405 | * in take_cpu_down(), so we prevent new cfs throttling here. |
5406 | */ | |
5407 | cfs_rq->runtime_enabled = 0; | |
5408 | ||
029632fb PZ |
5409 | if (cfs_rq_throttled(cfs_rq)) |
5410 | unthrottle_cfs_rq(cfs_rq); | |
5411 | } | |
502ce005 | 5412 | rcu_read_unlock(); |
029632fb PZ |
5413 | } |
5414 | ||
5415 | #else /* CONFIG_CFS_BANDWIDTH */ | |
f6783319 VG |
5416 | |
5417 | static inline bool cfs_bandwidth_used(void) | |
5418 | { | |
5419 | return false; | |
5420 | } | |
5421 | ||
9dbdb155 | 5422 | static void account_cfs_rq_runtime(struct cfs_rq *cfs_rq, u64 delta_exec) {} |
678d5718 | 5423 | static bool check_cfs_rq_runtime(struct cfs_rq *cfs_rq) { return false; } |
d3d9dc33 | 5424 | static void check_enqueue_throttle(struct cfs_rq *cfs_rq) {} |
55e16d30 | 5425 | static inline void sync_throttle(struct task_group *tg, int cpu) {} |
6c16a6dc | 5426 | static __always_inline void return_cfs_rq_runtime(struct cfs_rq *cfs_rq) {} |
85dac906 PT |
5427 | |
5428 | static inline int cfs_rq_throttled(struct cfs_rq *cfs_rq) | |
5429 | { | |
5430 | return 0; | |
5431 | } | |
64660c86 PT |
5432 | |
5433 | static inline int throttled_hierarchy(struct cfs_rq *cfs_rq) | |
5434 | { | |
5435 | return 0; | |
5436 | } | |
5437 | ||
5438 | static inline int throttled_lb_pair(struct task_group *tg, | |
5439 | int src_cpu, int dest_cpu) | |
5440 | { | |
5441 | return 0; | |
5442 | } | |
029632fb PZ |
5443 | |
5444 | void init_cfs_bandwidth(struct cfs_bandwidth *cfs_b) {} | |
5445 | ||
5446 | #ifdef CONFIG_FAIR_GROUP_SCHED | |
5447 | static void init_cfs_rq_runtime(struct cfs_rq *cfs_rq) {} | |
ab84d31e PT |
5448 | #endif |
5449 | ||
029632fb PZ |
5450 | static inline struct cfs_bandwidth *tg_cfs_bandwidth(struct task_group *tg) |
5451 | { | |
5452 | return NULL; | |
5453 | } | |
5454 | static inline void destroy_cfs_bandwidth(struct cfs_bandwidth *cfs_b) {} | |
0e59bdae | 5455 | static inline void update_runtime_enabled(struct rq *rq) {} |
a4c96ae3 | 5456 | static inline void unthrottle_offline_cfs_rqs(struct rq *rq) {} |
029632fb PZ |
5457 | |
5458 | #endif /* CONFIG_CFS_BANDWIDTH */ | |
5459 | ||
bf0f6f24 IM |
5460 | /************************************************** |
5461 | * CFS operations on tasks: | |
5462 | */ | |
5463 | ||
8f4d37ec PZ |
5464 | #ifdef CONFIG_SCHED_HRTICK |
5465 | static void hrtick_start_fair(struct rq *rq, struct task_struct *p) | |
5466 | { | |
8f4d37ec PZ |
5467 | struct sched_entity *se = &p->se; |
5468 | struct cfs_rq *cfs_rq = cfs_rq_of(se); | |
5469 | ||
9148a3a1 | 5470 | SCHED_WARN_ON(task_rq(p) != rq); |
8f4d37ec | 5471 | |
8bf46a39 | 5472 | if (rq->cfs.h_nr_running > 1) { |
8f4d37ec PZ |
5473 | u64 slice = sched_slice(cfs_rq, se); |
5474 | u64 ran = se->sum_exec_runtime - se->prev_sum_exec_runtime; | |
5475 | s64 delta = slice - ran; | |
5476 | ||
5477 | if (delta < 0) { | |
65bcf072 | 5478 | if (task_current(rq, p)) |
8875125e | 5479 | resched_curr(rq); |
8f4d37ec PZ |
5480 | return; |
5481 | } | |
31656519 | 5482 | hrtick_start(rq, delta); |
8f4d37ec PZ |
5483 | } |
5484 | } | |
a4c2f00f PZ |
5485 | |
5486 | /* | |
5487 | * called from enqueue/dequeue and updates the hrtick when the | |
5488 | * current task is from our class and nr_running is low enough | |
5489 | * to matter. | |
5490 | */ | |
5491 | static void hrtick_update(struct rq *rq) | |
5492 | { | |
5493 | struct task_struct *curr = rq->curr; | |
5494 | ||
e0ee463c | 5495 | if (!hrtick_enabled_fair(rq) || curr->sched_class != &fair_sched_class) |
a4c2f00f PZ |
5496 | return; |
5497 | ||
5498 | if (cfs_rq_of(&curr->se)->nr_running < sched_nr_latency) | |
5499 | hrtick_start_fair(rq, curr); | |
5500 | } | |
55e12e5e | 5501 | #else /* !CONFIG_SCHED_HRTICK */ |
8f4d37ec PZ |
5502 | static inline void |
5503 | hrtick_start_fair(struct rq *rq, struct task_struct *p) | |
5504 | { | |
5505 | } | |
a4c2f00f PZ |
5506 | |
5507 | static inline void hrtick_update(struct rq *rq) | |
5508 | { | |
5509 | } | |
8f4d37ec PZ |
5510 | #endif |
5511 | ||
2802bf3c MR |
5512 | #ifdef CONFIG_SMP |
5513 | static inline unsigned long cpu_util(int cpu); | |
2802bf3c MR |
5514 | |
5515 | static inline bool cpu_overutilized(int cpu) | |
5516 | { | |
60e17f5c | 5517 | return !fits_capacity(cpu_util(cpu), capacity_of(cpu)); |
2802bf3c MR |
5518 | } |
5519 | ||
5520 | static inline void update_overutilized_status(struct rq *rq) | |
5521 | { | |
f9f240f9 | 5522 | if (!READ_ONCE(rq->rd->overutilized) && cpu_overutilized(rq->cpu)) { |
2802bf3c | 5523 | WRITE_ONCE(rq->rd->overutilized, SG_OVERUTILIZED); |
f9f240f9 QY |
5524 | trace_sched_overutilized_tp(rq->rd, SG_OVERUTILIZED); |
5525 | } | |
2802bf3c MR |
5526 | } |
5527 | #else | |
5528 | static inline void update_overutilized_status(struct rq *rq) { } | |
5529 | #endif | |
5530 | ||
323af6de VK |
5531 | /* Runqueue only has SCHED_IDLE tasks enqueued */ |
5532 | static int sched_idle_rq(struct rq *rq) | |
5533 | { | |
5534 | return unlikely(rq->nr_running == rq->cfs.idle_h_nr_running && | |
5535 | rq->nr_running); | |
5536 | } | |
5537 | ||
a480adde JD |
5538 | /* |
5539 | * Returns true if cfs_rq only has SCHED_IDLE entities enqueued. Note the use | |
5540 | * of idle_nr_running, which does not consider idle descendants of normal | |
5541 | * entities. | |
5542 | */ | |
5543 | static bool sched_idle_cfs_rq(struct cfs_rq *cfs_rq) | |
5544 | { | |
5545 | return cfs_rq->nr_running && | |
5546 | cfs_rq->nr_running == cfs_rq->idle_nr_running; | |
5547 | } | |
5548 | ||
afa70d94 | 5549 | #ifdef CONFIG_SMP |
323af6de VK |
5550 | static int sched_idle_cpu(int cpu) |
5551 | { | |
5552 | return sched_idle_rq(cpu_rq(cpu)); | |
5553 | } | |
afa70d94 | 5554 | #endif |
323af6de | 5555 | |
bf0f6f24 IM |
5556 | /* |
5557 | * The enqueue_task method is called before nr_running is | |
5558 | * increased. Here we update the fair scheduling stats and | |
5559 | * then put the task into the rbtree: | |
5560 | */ | |
ea87bb78 | 5561 | static void |
371fd7e7 | 5562 | enqueue_task_fair(struct rq *rq, struct task_struct *p, int flags) |
bf0f6f24 IM |
5563 | { |
5564 | struct cfs_rq *cfs_rq; | |
62fb1851 | 5565 | struct sched_entity *se = &p->se; |
43e9f7f2 | 5566 | int idle_h_nr_running = task_has_idle_policy(p); |
8e1ac429 | 5567 | int task_new = !(flags & ENQUEUE_WAKEUP); |
bf0f6f24 | 5568 | |
2539fc82 PB |
5569 | /* |
5570 | * The code below (indirectly) updates schedutil which looks at | |
5571 | * the cfs_rq utilization to select a frequency. | |
5572 | * Let's add the task's estimated utilization to the cfs_rq's | |
5573 | * estimated utilization, before we update schedutil. | |
5574 | */ | |
5575 | util_est_enqueue(&rq->cfs, p); | |
5576 | ||
8c34ab19 RW |
5577 | /* |
5578 | * If in_iowait is set, the code below may not trigger any cpufreq | |
5579 | * utilization updates, so do it here explicitly with the IOWAIT flag | |
5580 | * passed. | |
5581 | */ | |
5582 | if (p->in_iowait) | |
674e7541 | 5583 | cpufreq_update_util(rq, SCHED_CPUFREQ_IOWAIT); |
8c34ab19 | 5584 | |
bf0f6f24 | 5585 | for_each_sched_entity(se) { |
62fb1851 | 5586 | if (se->on_rq) |
bf0f6f24 IM |
5587 | break; |
5588 | cfs_rq = cfs_rq_of(se); | |
88ec22d3 | 5589 | enqueue_entity(cfs_rq, se, flags); |
85dac906 | 5590 | |
953bfcd1 | 5591 | cfs_rq->h_nr_running++; |
43e9f7f2 | 5592 | cfs_rq->idle_h_nr_running += idle_h_nr_running; |
85dac906 | 5593 | |
30400039 JD |
5594 | if (cfs_rq_is_idle(cfs_rq)) |
5595 | idle_h_nr_running = 1; | |
5596 | ||
6d4d2246 VG |
5597 | /* end evaluation on encountering a throttled cfs_rq */ |
5598 | if (cfs_rq_throttled(cfs_rq)) | |
5599 | goto enqueue_throttle; | |
5600 | ||
88ec22d3 | 5601 | flags = ENQUEUE_WAKEUP; |
bf0f6f24 | 5602 | } |
8f4d37ec | 5603 | |
2069dd75 | 5604 | for_each_sched_entity(se) { |
0f317143 | 5605 | cfs_rq = cfs_rq_of(se); |
2069dd75 | 5606 | |
88c0616e | 5607 | update_load_avg(cfs_rq, se, UPDATE_TG); |
9f683953 | 5608 | se_update_runnable(se); |
1ea6c46a | 5609 | update_cfs_group(se); |
6d4d2246 VG |
5610 | |
5611 | cfs_rq->h_nr_running++; | |
5612 | cfs_rq->idle_h_nr_running += idle_h_nr_running; | |
5ab297ba | 5613 | |
30400039 JD |
5614 | if (cfs_rq_is_idle(cfs_rq)) |
5615 | idle_h_nr_running = 1; | |
5616 | ||
5ab297ba VG |
5617 | /* end evaluation on encountering a throttled cfs_rq */ |
5618 | if (cfs_rq_throttled(cfs_rq)) | |
5619 | goto enqueue_throttle; | |
b34cb07d PA |
5620 | |
5621 | /* | |
5622 | * One parent has been throttled and cfs_rq removed from the | |
5623 | * list. Add it back to not break the leaf list. | |
5624 | */ | |
5625 | if (throttled_hierarchy(cfs_rq)) | |
5626 | list_add_leaf_cfs_rq(cfs_rq); | |
2069dd75 PZ |
5627 | } |
5628 | ||
7d148be6 VG |
5629 | /* At this point se is NULL and we are at root level*/ |
5630 | add_nr_running(rq, 1); | |
2802bf3c | 5631 | |
7d148be6 VG |
5632 | /* |
5633 | * Since new tasks are assigned an initial util_avg equal to | |
5634 | * half of the spare capacity of their CPU, tiny tasks have the | |
5635 | * ability to cross the overutilized threshold, which will | |
5636 | * result in the load balancer ruining all the task placement | |
5637 | * done by EAS. As a way to mitigate that effect, do not account | |
5638 | * for the first enqueue operation of new tasks during the | |
5639 | * overutilized flag detection. | |
5640 | * | |
5641 | * A better way of solving this problem would be to wait for | |
5642 | * the PELT signals of tasks to converge before taking them | |
5643 | * into account, but that is not straightforward to implement, | |
5644 | * and the following generally works well enough in practice. | |
5645 | */ | |
8e1ac429 | 5646 | if (!task_new) |
7d148be6 | 5647 | update_overutilized_status(rq); |
cd126afe | 5648 | |
7d148be6 | 5649 | enqueue_throttle: |
f6783319 VG |
5650 | if (cfs_bandwidth_used()) { |
5651 | /* | |
5652 | * When bandwidth control is enabled; the cfs_rq_throttled() | |
5653 | * breaks in the above iteration can result in incomplete | |
5654 | * leaf list maintenance, resulting in triggering the assertion | |
5655 | * below. | |
5656 | */ | |
5657 | for_each_sched_entity(se) { | |
5658 | cfs_rq = cfs_rq_of(se); | |
5659 | ||
5660 | if (list_add_leaf_cfs_rq(cfs_rq)) | |
5661 | break; | |
5662 | } | |
5663 | } | |
5664 | ||
5d299eab PZ |
5665 | assert_list_leaf_cfs_rq(rq); |
5666 | ||
a4c2f00f | 5667 | hrtick_update(rq); |
bf0f6f24 IM |
5668 | } |
5669 | ||
2f36825b VP |
5670 | static void set_next_buddy(struct sched_entity *se); |
5671 | ||
bf0f6f24 IM |
5672 | /* |
5673 | * The dequeue_task method is called before nr_running is | |
5674 | * decreased. We remove the task from the rbtree and | |
5675 | * update the fair scheduling stats: | |
5676 | */ | |
371fd7e7 | 5677 | static void dequeue_task_fair(struct rq *rq, struct task_struct *p, int flags) |
bf0f6f24 IM |
5678 | { |
5679 | struct cfs_rq *cfs_rq; | |
62fb1851 | 5680 | struct sched_entity *se = &p->se; |
2f36825b | 5681 | int task_sleep = flags & DEQUEUE_SLEEP; |
43e9f7f2 | 5682 | int idle_h_nr_running = task_has_idle_policy(p); |
323af6de | 5683 | bool was_sched_idle = sched_idle_rq(rq); |
bf0f6f24 | 5684 | |
8c1f560c XY |
5685 | util_est_dequeue(&rq->cfs, p); |
5686 | ||
bf0f6f24 IM |
5687 | for_each_sched_entity(se) { |
5688 | cfs_rq = cfs_rq_of(se); | |
371fd7e7 | 5689 | dequeue_entity(cfs_rq, se, flags); |
85dac906 | 5690 | |
953bfcd1 | 5691 | cfs_rq->h_nr_running--; |
43e9f7f2 | 5692 | cfs_rq->idle_h_nr_running -= idle_h_nr_running; |
2069dd75 | 5693 | |
30400039 JD |
5694 | if (cfs_rq_is_idle(cfs_rq)) |
5695 | idle_h_nr_running = 1; | |
5696 | ||
6d4d2246 VG |
5697 | /* end evaluation on encountering a throttled cfs_rq */ |
5698 | if (cfs_rq_throttled(cfs_rq)) | |
5699 | goto dequeue_throttle; | |
5700 | ||
bf0f6f24 | 5701 | /* Don't dequeue parent if it has other entities besides us */ |
2f36825b | 5702 | if (cfs_rq->load.weight) { |
754bd598 KK |
5703 | /* Avoid re-evaluating load for this entity: */ |
5704 | se = parent_entity(se); | |
2f36825b VP |
5705 | /* |
5706 | * Bias pick_next to pick a task from this cfs_rq, as | |
5707 | * p is sleeping when it is within its sched_slice. | |
5708 | */ | |
754bd598 KK |
5709 | if (task_sleep && se && !throttled_hierarchy(cfs_rq)) |
5710 | set_next_buddy(se); | |
bf0f6f24 | 5711 | break; |
2f36825b | 5712 | } |
371fd7e7 | 5713 | flags |= DEQUEUE_SLEEP; |
bf0f6f24 | 5714 | } |
8f4d37ec | 5715 | |
2069dd75 | 5716 | for_each_sched_entity(se) { |
0f317143 | 5717 | cfs_rq = cfs_rq_of(se); |
2069dd75 | 5718 | |
88c0616e | 5719 | update_load_avg(cfs_rq, se, UPDATE_TG); |
9f683953 | 5720 | se_update_runnable(se); |
1ea6c46a | 5721 | update_cfs_group(se); |
6d4d2246 VG |
5722 | |
5723 | cfs_rq->h_nr_running--; | |
5724 | cfs_rq->idle_h_nr_running -= idle_h_nr_running; | |
5ab297ba | 5725 | |
30400039 JD |
5726 | if (cfs_rq_is_idle(cfs_rq)) |
5727 | idle_h_nr_running = 1; | |
5728 | ||
5ab297ba VG |
5729 | /* end evaluation on encountering a throttled cfs_rq */ |
5730 | if (cfs_rq_throttled(cfs_rq)) | |
5731 | goto dequeue_throttle; | |
5732 | ||
2069dd75 PZ |
5733 | } |
5734 | ||
423d02e1 PW |
5735 | /* At this point se is NULL and we are at root level*/ |
5736 | sub_nr_running(rq, 1); | |
cd126afe | 5737 | |
323af6de VK |
5738 | /* balance early to pull high priority tasks */ |
5739 | if (unlikely(!was_sched_idle && sched_idle_rq(rq))) | |
5740 | rq->next_balance = jiffies; | |
5741 | ||
423d02e1 | 5742 | dequeue_throttle: |
8c1f560c | 5743 | util_est_update(&rq->cfs, p, task_sleep); |
a4c2f00f | 5744 | hrtick_update(rq); |
bf0f6f24 IM |
5745 | } |
5746 | ||
e7693a36 | 5747 | #ifdef CONFIG_SMP |
10e2f1ac PZ |
5748 | |
5749 | /* Working cpumask for: load_balance, load_balance_newidle. */ | |
5750 | DEFINE_PER_CPU(cpumask_var_t, load_balance_mask); | |
5751 | DEFINE_PER_CPU(cpumask_var_t, select_idle_mask); | |
5752 | ||
9fd81dd5 | 5753 | #ifdef CONFIG_NO_HZ_COMMON |
e022e0d3 PZ |
5754 | |
5755 | static struct { | |
5756 | cpumask_var_t idle_cpus_mask; | |
5757 | atomic_t nr_cpus; | |
f643ea22 | 5758 | int has_blocked; /* Idle CPUS has blocked load */ |
7fd7a9e0 | 5759 | int needs_update; /* Newly idle CPUs need their next_balance collated */ |
e022e0d3 | 5760 | unsigned long next_balance; /* in jiffy units */ |
f643ea22 | 5761 | unsigned long next_blocked; /* Next update of blocked load in jiffies */ |
e022e0d3 PZ |
5762 | } nohz ____cacheline_aligned; |
5763 | ||
9fd81dd5 | 5764 | #endif /* CONFIG_NO_HZ_COMMON */ |
3289bdb4 | 5765 | |
b0fb1eb4 VG |
5766 | static unsigned long cpu_load(struct rq *rq) |
5767 | { | |
5768 | return cfs_rq_load_avg(&rq->cfs); | |
5769 | } | |
5770 | ||
3318544b VG |
5771 | /* |
5772 | * cpu_load_without - compute CPU load without any contributions from *p | |
5773 | * @cpu: the CPU which load is requested | |
5774 | * @p: the task which load should be discounted | |
5775 | * | |
5776 | * The load of a CPU is defined by the load of tasks currently enqueued on that | |
5777 | * CPU as well as tasks which are currently sleeping after an execution on that | |
5778 | * CPU. | |
5779 | * | |
5780 | * This method returns the load of the specified CPU by discounting the load of | |
5781 | * the specified task, whenever the task is currently contributing to the CPU | |
5782 | * load. | |
5783 | */ | |
5784 | static unsigned long cpu_load_without(struct rq *rq, struct task_struct *p) | |
5785 | { | |
5786 | struct cfs_rq *cfs_rq; | |
5787 | unsigned int load; | |
5788 | ||
5789 | /* Task has no contribution or is new */ | |
5790 | if (cpu_of(rq) != task_cpu(p) || !READ_ONCE(p->se.avg.last_update_time)) | |
5791 | return cpu_load(rq); | |
5792 | ||
5793 | cfs_rq = &rq->cfs; | |
5794 | load = READ_ONCE(cfs_rq->avg.load_avg); | |
5795 | ||
5796 | /* Discount task's util from CPU's util */ | |
5797 | lsub_positive(&load, task_h_load(p)); | |
5798 | ||
5799 | return load; | |
5800 | } | |
5801 | ||
9f683953 VG |
5802 | static unsigned long cpu_runnable(struct rq *rq) |
5803 | { | |
5804 | return cfs_rq_runnable_avg(&rq->cfs); | |
5805 | } | |
5806 | ||
070f5e86 VG |
5807 | static unsigned long cpu_runnable_without(struct rq *rq, struct task_struct *p) |
5808 | { | |
5809 | struct cfs_rq *cfs_rq; | |
5810 | unsigned int runnable; | |
5811 | ||
5812 | /* Task has no contribution or is new */ | |
5813 | if (cpu_of(rq) != task_cpu(p) || !READ_ONCE(p->se.avg.last_update_time)) | |
5814 | return cpu_runnable(rq); | |
5815 | ||
5816 | cfs_rq = &rq->cfs; | |
5817 | runnable = READ_ONCE(cfs_rq->avg.runnable_avg); | |
5818 | ||
5819 | /* Discount task's runnable from CPU's runnable */ | |
5820 | lsub_positive(&runnable, p->se.avg.runnable_avg); | |
5821 | ||
5822 | return runnable; | |
5823 | } | |
5824 | ||
ced549fa | 5825 | static unsigned long capacity_of(int cpu) |
029632fb | 5826 | { |
ced549fa | 5827 | return cpu_rq(cpu)->cpu_capacity; |
029632fb PZ |
5828 | } |
5829 | ||
c58d25f3 PZ |
5830 | static void record_wakee(struct task_struct *p) |
5831 | { | |
5832 | /* | |
5833 | * Only decay a single time; tasks that have less then 1 wakeup per | |
5834 | * jiffy will not have built up many flips. | |
5835 | */ | |
5836 | if (time_after(jiffies, current->wakee_flip_decay_ts + HZ)) { | |
5837 | current->wakee_flips >>= 1; | |
5838 | current->wakee_flip_decay_ts = jiffies; | |
5839 | } | |
5840 | ||
5841 | if (current->last_wakee != p) { | |
5842 | current->last_wakee = p; | |
5843 | current->wakee_flips++; | |
5844 | } | |
5845 | } | |
5846 | ||
63b0e9ed MG |
5847 | /* |
5848 | * Detect M:N waker/wakee relationships via a switching-frequency heuristic. | |
c58d25f3 | 5849 | * |
63b0e9ed | 5850 | * A waker of many should wake a different task than the one last awakened |
c58d25f3 PZ |
5851 | * at a frequency roughly N times higher than one of its wakees. |
5852 | * | |
5853 | * In order to determine whether we should let the load spread vs consolidating | |
5854 | * to shared cache, we look for a minimum 'flip' frequency of llc_size in one | |
5855 | * partner, and a factor of lls_size higher frequency in the other. | |
5856 | * | |
5857 | * With both conditions met, we can be relatively sure that the relationship is | |
5858 | * non-monogamous, with partner count exceeding socket size. | |
5859 | * | |
5860 | * Waker/wakee being client/server, worker/dispatcher, interrupt source or | |
5861 | * whatever is irrelevant, spread criteria is apparent partner count exceeds | |
5862 | * socket size. | |
63b0e9ed | 5863 | */ |
62470419 MW |
5864 | static int wake_wide(struct task_struct *p) |
5865 | { | |
63b0e9ed MG |
5866 | unsigned int master = current->wakee_flips; |
5867 | unsigned int slave = p->wakee_flips; | |
17c891ab | 5868 | int factor = __this_cpu_read(sd_llc_size); |
62470419 | 5869 | |
63b0e9ed MG |
5870 | if (master < slave) |
5871 | swap(master, slave); | |
5872 | if (slave < factor || master < slave * factor) | |
5873 | return 0; | |
5874 | return 1; | |
62470419 MW |
5875 | } |
5876 | ||
90001d67 | 5877 | /* |
d153b153 PZ |
5878 | * The purpose of wake_affine() is to quickly determine on which CPU we can run |
5879 | * soonest. For the purpose of speed we only consider the waking and previous | |
5880 | * CPU. | |
90001d67 | 5881 | * |
7332dec0 MG |
5882 | * wake_affine_idle() - only considers 'now', it check if the waking CPU is |
5883 | * cache-affine and is (or will be) idle. | |
f2cdd9cc PZ |
5884 | * |
5885 | * wake_affine_weight() - considers the weight to reflect the average | |
5886 | * scheduling latency of the CPUs. This seems to work | |
5887 | * for the overloaded case. | |
90001d67 | 5888 | */ |
3b76c4a3 | 5889 | static int |
89a55f56 | 5890 | wake_affine_idle(int this_cpu, int prev_cpu, int sync) |
90001d67 | 5891 | { |
7332dec0 MG |
5892 | /* |
5893 | * If this_cpu is idle, it implies the wakeup is from interrupt | |
5894 | * context. Only allow the move if cache is shared. Otherwise an | |
5895 | * interrupt intensive workload could force all tasks onto one | |
5896 | * node depending on the IO topology or IRQ affinity settings. | |
806486c3 MG |
5897 | * |
5898 | * If the prev_cpu is idle and cache affine then avoid a migration. | |
5899 | * There is no guarantee that the cache hot data from an interrupt | |
5900 | * is more important than cache hot data on the prev_cpu and from | |
5901 | * a cpufreq perspective, it's better to have higher utilisation | |
5902 | * on one CPU. | |
7332dec0 | 5903 | */ |
943d355d RJ |
5904 | if (available_idle_cpu(this_cpu) && cpus_share_cache(this_cpu, prev_cpu)) |
5905 | return available_idle_cpu(prev_cpu) ? prev_cpu : this_cpu; | |
90001d67 | 5906 | |
d153b153 | 5907 | if (sync && cpu_rq(this_cpu)->nr_running == 1) |
3b76c4a3 | 5908 | return this_cpu; |
90001d67 | 5909 | |
d8fcb81f JL |
5910 | if (available_idle_cpu(prev_cpu)) |
5911 | return prev_cpu; | |
5912 | ||
3b76c4a3 | 5913 | return nr_cpumask_bits; |
90001d67 PZ |
5914 | } |
5915 | ||
3b76c4a3 | 5916 | static int |
f2cdd9cc PZ |
5917 | wake_affine_weight(struct sched_domain *sd, struct task_struct *p, |
5918 | int this_cpu, int prev_cpu, int sync) | |
90001d67 | 5919 | { |
90001d67 PZ |
5920 | s64 this_eff_load, prev_eff_load; |
5921 | unsigned long task_load; | |
5922 | ||
11f10e54 | 5923 | this_eff_load = cpu_load(cpu_rq(this_cpu)); |
90001d67 | 5924 | |
90001d67 PZ |
5925 | if (sync) { |
5926 | unsigned long current_load = task_h_load(current); | |
5927 | ||
f2cdd9cc | 5928 | if (current_load > this_eff_load) |
3b76c4a3 | 5929 | return this_cpu; |
90001d67 | 5930 | |
f2cdd9cc | 5931 | this_eff_load -= current_load; |
90001d67 PZ |
5932 | } |
5933 | ||
90001d67 PZ |
5934 | task_load = task_h_load(p); |
5935 | ||
f2cdd9cc PZ |
5936 | this_eff_load += task_load; |
5937 | if (sched_feat(WA_BIAS)) | |
5938 | this_eff_load *= 100; | |
5939 | this_eff_load *= capacity_of(prev_cpu); | |
90001d67 | 5940 | |
11f10e54 | 5941 | prev_eff_load = cpu_load(cpu_rq(prev_cpu)); |
f2cdd9cc PZ |
5942 | prev_eff_load -= task_load; |
5943 | if (sched_feat(WA_BIAS)) | |
5944 | prev_eff_load *= 100 + (sd->imbalance_pct - 100) / 2; | |
5945 | prev_eff_load *= capacity_of(this_cpu); | |
90001d67 | 5946 | |
082f764a MG |
5947 | /* |
5948 | * If sync, adjust the weight of prev_eff_load such that if | |
5949 | * prev_eff == this_eff that select_idle_sibling() will consider | |
5950 | * stacking the wakee on top of the waker if no other CPU is | |
5951 | * idle. | |
5952 | */ | |
5953 | if (sync) | |
5954 | prev_eff_load += 1; | |
5955 | ||
5956 | return this_eff_load < prev_eff_load ? this_cpu : nr_cpumask_bits; | |
90001d67 PZ |
5957 | } |
5958 | ||
772bd008 | 5959 | static int wake_affine(struct sched_domain *sd, struct task_struct *p, |
7ebb66a1 | 5960 | int this_cpu, int prev_cpu, int sync) |
098fb9db | 5961 | { |
3b76c4a3 | 5962 | int target = nr_cpumask_bits; |
098fb9db | 5963 | |
89a55f56 | 5964 | if (sched_feat(WA_IDLE)) |
3b76c4a3 | 5965 | target = wake_affine_idle(this_cpu, prev_cpu, sync); |
90001d67 | 5966 | |
3b76c4a3 MG |
5967 | if (sched_feat(WA_WEIGHT) && target == nr_cpumask_bits) |
5968 | target = wake_affine_weight(sd, p, this_cpu, prev_cpu, sync); | |
098fb9db | 5969 | |
ceeadb83 | 5970 | schedstat_inc(p->stats.nr_wakeups_affine_attempts); |
3b76c4a3 MG |
5971 | if (target == nr_cpumask_bits) |
5972 | return prev_cpu; | |
098fb9db | 5973 | |
3b76c4a3 | 5974 | schedstat_inc(sd->ttwu_move_affine); |
ceeadb83 | 5975 | schedstat_inc(p->stats.nr_wakeups_affine); |
3b76c4a3 | 5976 | return target; |
098fb9db IM |
5977 | } |
5978 | ||
aaee1203 | 5979 | static struct sched_group * |
45da2773 | 5980 | find_idlest_group(struct sched_domain *sd, struct task_struct *p, int this_cpu); |
aaee1203 PZ |
5981 | |
5982 | /* | |
97fb7a0a | 5983 | * find_idlest_group_cpu - find the idlest CPU among the CPUs in the group. |
aaee1203 PZ |
5984 | */ |
5985 | static int | |
18bd1b4b | 5986 | find_idlest_group_cpu(struct sched_group *group, struct task_struct *p, int this_cpu) |
aaee1203 PZ |
5987 | { |
5988 | unsigned long load, min_load = ULONG_MAX; | |
83a0a96a NP |
5989 | unsigned int min_exit_latency = UINT_MAX; |
5990 | u64 latest_idle_timestamp = 0; | |
5991 | int least_loaded_cpu = this_cpu; | |
17346452 | 5992 | int shallowest_idle_cpu = -1; |
aaee1203 PZ |
5993 | int i; |
5994 | ||
eaecf41f MR |
5995 | /* Check if we have any choice: */ |
5996 | if (group->group_weight == 1) | |
ae4df9d6 | 5997 | return cpumask_first(sched_group_span(group)); |
eaecf41f | 5998 | |
aaee1203 | 5999 | /* Traverse only the allowed CPUs */ |
3bd37062 | 6000 | for_each_cpu_and(i, sched_group_span(group), p->cpus_ptr) { |
97886d9d AL |
6001 | struct rq *rq = cpu_rq(i); |
6002 | ||
6003 | if (!sched_core_cookie_match(rq, p)) | |
6004 | continue; | |
6005 | ||
17346452 VK |
6006 | if (sched_idle_cpu(i)) |
6007 | return i; | |
6008 | ||
943d355d | 6009 | if (available_idle_cpu(i)) { |
83a0a96a NP |
6010 | struct cpuidle_state *idle = idle_get_state(rq); |
6011 | if (idle && idle->exit_latency < min_exit_latency) { | |
6012 | /* | |
6013 | * We give priority to a CPU whose idle state | |
6014 | * has the smallest exit latency irrespective | |
6015 | * of any idle timestamp. | |
6016 | */ | |
6017 | min_exit_latency = idle->exit_latency; | |
6018 | latest_idle_timestamp = rq->idle_stamp; | |
6019 | shallowest_idle_cpu = i; | |
6020 | } else if ((!idle || idle->exit_latency == min_exit_latency) && | |
6021 | rq->idle_stamp > latest_idle_timestamp) { | |
6022 | /* | |
6023 | * If equal or no active idle state, then | |
6024 | * the most recently idled CPU might have | |
6025 | * a warmer cache. | |
6026 | */ | |
6027 | latest_idle_timestamp = rq->idle_stamp; | |
6028 | shallowest_idle_cpu = i; | |
6029 | } | |
17346452 | 6030 | } else if (shallowest_idle_cpu == -1) { |
11f10e54 | 6031 | load = cpu_load(cpu_rq(i)); |
18cec7e0 | 6032 | if (load < min_load) { |
83a0a96a NP |
6033 | min_load = load; |
6034 | least_loaded_cpu = i; | |
6035 | } | |
e7693a36 GH |
6036 | } |
6037 | } | |
6038 | ||
17346452 | 6039 | return shallowest_idle_cpu != -1 ? shallowest_idle_cpu : least_loaded_cpu; |
aaee1203 | 6040 | } |
e7693a36 | 6041 | |
18bd1b4b BJ |
6042 | static inline int find_idlest_cpu(struct sched_domain *sd, struct task_struct *p, |
6043 | int cpu, int prev_cpu, int sd_flag) | |
6044 | { | |
93f50f90 | 6045 | int new_cpu = cpu; |
18bd1b4b | 6046 | |
3bd37062 | 6047 | if (!cpumask_intersects(sched_domain_span(sd), p->cpus_ptr)) |
6fee85cc BJ |
6048 | return prev_cpu; |
6049 | ||
c976a862 | 6050 | /* |
57abff06 | 6051 | * We need task's util for cpu_util_without, sync it up to |
c469933e | 6052 | * prev_cpu's last_update_time. |
c976a862 VK |
6053 | */ |
6054 | if (!(sd_flag & SD_BALANCE_FORK)) | |
6055 | sync_entity_load_avg(&p->se); | |
6056 | ||
18bd1b4b BJ |
6057 | while (sd) { |
6058 | struct sched_group *group; | |
6059 | struct sched_domain *tmp; | |
6060 | int weight; | |
6061 | ||
6062 | if (!(sd->flags & sd_flag)) { | |
6063 | sd = sd->child; | |
6064 | continue; | |
6065 | } | |
6066 | ||
45da2773 | 6067 | group = find_idlest_group(sd, p, cpu); |
18bd1b4b BJ |
6068 | if (!group) { |
6069 | sd = sd->child; | |
6070 | continue; | |
6071 | } | |
6072 | ||
6073 | new_cpu = find_idlest_group_cpu(group, p, cpu); | |
e90381ea | 6074 | if (new_cpu == cpu) { |
97fb7a0a | 6075 | /* Now try balancing at a lower domain level of 'cpu': */ |
18bd1b4b BJ |
6076 | sd = sd->child; |
6077 | continue; | |
6078 | } | |
6079 | ||
97fb7a0a | 6080 | /* Now try balancing at a lower domain level of 'new_cpu': */ |
18bd1b4b BJ |
6081 | cpu = new_cpu; |
6082 | weight = sd->span_weight; | |
6083 | sd = NULL; | |
6084 | for_each_domain(cpu, tmp) { | |
6085 | if (weight <= tmp->span_weight) | |
6086 | break; | |
6087 | if (tmp->flags & sd_flag) | |
6088 | sd = tmp; | |
6089 | } | |
18bd1b4b BJ |
6090 | } |
6091 | ||
6092 | return new_cpu; | |
6093 | } | |
6094 | ||
97886d9d | 6095 | static inline int __select_idle_cpu(int cpu, struct task_struct *p) |
9fe1f127 | 6096 | { |
97886d9d AL |
6097 | if ((available_idle_cpu(cpu) || sched_idle_cpu(cpu)) && |
6098 | sched_cpu_cookie_match(cpu_rq(cpu), p)) | |
9fe1f127 MG |
6099 | return cpu; |
6100 | ||
6101 | return -1; | |
6102 | } | |
6103 | ||
10e2f1ac | 6104 | #ifdef CONFIG_SCHED_SMT |
ba2591a5 | 6105 | DEFINE_STATIC_KEY_FALSE(sched_smt_present); |
b284909a | 6106 | EXPORT_SYMBOL_GPL(sched_smt_present); |
10e2f1ac PZ |
6107 | |
6108 | static inline void set_idle_cores(int cpu, int val) | |
6109 | { | |
6110 | struct sched_domain_shared *sds; | |
6111 | ||
6112 | sds = rcu_dereference(per_cpu(sd_llc_shared, cpu)); | |
6113 | if (sds) | |
6114 | WRITE_ONCE(sds->has_idle_cores, val); | |
6115 | } | |
6116 | ||
6117 | static inline bool test_idle_cores(int cpu, bool def) | |
6118 | { | |
6119 | struct sched_domain_shared *sds; | |
6120 | ||
c722f35b RR |
6121 | sds = rcu_dereference(per_cpu(sd_llc_shared, cpu)); |
6122 | if (sds) | |
6123 | return READ_ONCE(sds->has_idle_cores); | |
10e2f1ac PZ |
6124 | |
6125 | return def; | |
6126 | } | |
6127 | ||
6128 | /* | |
6129 | * Scans the local SMT mask to see if the entire core is idle, and records this | |
6130 | * information in sd_llc_shared->has_idle_cores. | |
6131 | * | |
6132 | * Since SMT siblings share all cache levels, inspecting this limited remote | |
6133 | * state should be fairly cheap. | |
6134 | */ | |
1b568f0a | 6135 | void __update_idle_core(struct rq *rq) |
10e2f1ac PZ |
6136 | { |
6137 | int core = cpu_of(rq); | |
6138 | int cpu; | |
6139 | ||
6140 | rcu_read_lock(); | |
6141 | if (test_idle_cores(core, true)) | |
6142 | goto unlock; | |
6143 | ||
6144 | for_each_cpu(cpu, cpu_smt_mask(core)) { | |
6145 | if (cpu == core) | |
6146 | continue; | |
6147 | ||
943d355d | 6148 | if (!available_idle_cpu(cpu)) |
10e2f1ac PZ |
6149 | goto unlock; |
6150 | } | |
6151 | ||
6152 | set_idle_cores(core, 1); | |
6153 | unlock: | |
6154 | rcu_read_unlock(); | |
6155 | } | |
6156 | ||
6157 | /* | |
6158 | * Scan the entire LLC domain for idle cores; this dynamically switches off if | |
6159 | * there are no idle cores left in the system; tracked through | |
6160 | * sd_llc->shared->has_idle_cores and enabled through update_idle_core() above. | |
6161 | */ | |
9fe1f127 | 6162 | static int select_idle_core(struct task_struct *p, int core, struct cpumask *cpus, int *idle_cpu) |
10e2f1ac | 6163 | { |
9fe1f127 MG |
6164 | bool idle = true; |
6165 | int cpu; | |
10e2f1ac | 6166 | |
1b568f0a | 6167 | if (!static_branch_likely(&sched_smt_present)) |
97886d9d | 6168 | return __select_idle_cpu(core, p); |
10e2f1ac | 6169 | |
9fe1f127 MG |
6170 | for_each_cpu(cpu, cpu_smt_mask(core)) { |
6171 | if (!available_idle_cpu(cpu)) { | |
6172 | idle = false; | |
6173 | if (*idle_cpu == -1) { | |
6174 | if (sched_idle_cpu(cpu) && cpumask_test_cpu(cpu, p->cpus_ptr)) { | |
6175 | *idle_cpu = cpu; | |
6176 | break; | |
6177 | } | |
6178 | continue; | |
bec2860a | 6179 | } |
9fe1f127 | 6180 | break; |
10e2f1ac | 6181 | } |
9fe1f127 MG |
6182 | if (*idle_cpu == -1 && cpumask_test_cpu(cpu, p->cpus_ptr)) |
6183 | *idle_cpu = cpu; | |
10e2f1ac PZ |
6184 | } |
6185 | ||
9fe1f127 MG |
6186 | if (idle) |
6187 | return core; | |
10e2f1ac | 6188 | |
9fe1f127 | 6189 | cpumask_andnot(cpus, cpus, cpu_smt_mask(core)); |
10e2f1ac PZ |
6190 | return -1; |
6191 | } | |
6192 | ||
c722f35b RR |
6193 | /* |
6194 | * Scan the local SMT mask for idle CPUs. | |
6195 | */ | |
6196 | static int select_idle_smt(struct task_struct *p, struct sched_domain *sd, int target) | |
6197 | { | |
6198 | int cpu; | |
6199 | ||
6200 | for_each_cpu(cpu, cpu_smt_mask(target)) { | |
6201 | if (!cpumask_test_cpu(cpu, p->cpus_ptr) || | |
6202 | !cpumask_test_cpu(cpu, sched_domain_span(sd))) | |
6203 | continue; | |
6204 | if (available_idle_cpu(cpu) || sched_idle_cpu(cpu)) | |
6205 | return cpu; | |
6206 | } | |
6207 | ||
6208 | return -1; | |
6209 | } | |
6210 | ||
10e2f1ac PZ |
6211 | #else /* CONFIG_SCHED_SMT */ |
6212 | ||
9fe1f127 | 6213 | static inline void set_idle_cores(int cpu, int val) |
10e2f1ac | 6214 | { |
9fe1f127 MG |
6215 | } |
6216 | ||
6217 | static inline bool test_idle_cores(int cpu, bool def) | |
6218 | { | |
6219 | return def; | |
6220 | } | |
6221 | ||
6222 | static inline int select_idle_core(struct task_struct *p, int core, struct cpumask *cpus, int *idle_cpu) | |
6223 | { | |
97886d9d | 6224 | return __select_idle_cpu(core, p); |
10e2f1ac PZ |
6225 | } |
6226 | ||
c722f35b RR |
6227 | static inline int select_idle_smt(struct task_struct *p, struct sched_domain *sd, int target) |
6228 | { | |
6229 | return -1; | |
6230 | } | |
6231 | ||
10e2f1ac PZ |
6232 | #endif /* CONFIG_SCHED_SMT */ |
6233 | ||
6234 | /* | |
6235 | * Scan the LLC domain for idle CPUs; this is dynamically regulated by | |
6236 | * comparing the average scan cost (tracked in sd->avg_scan_cost) against the | |
6237 | * average idle time for this rq (as found in rq->avg_idle). | |
a50bde51 | 6238 | */ |
c722f35b | 6239 | static int select_idle_cpu(struct task_struct *p, struct sched_domain *sd, bool has_idle_core, int target) |
10e2f1ac | 6240 | { |
60588bfa | 6241 | struct cpumask *cpus = this_cpu_cpumask_var_ptr(select_idle_mask); |
9fe1f127 | 6242 | int i, cpu, idle_cpu = -1, nr = INT_MAX; |
94aafc3e | 6243 | struct rq *this_rq = this_rq(); |
9fe1f127 | 6244 | int this = smp_processor_id(); |
9cfb38a7 | 6245 | struct sched_domain *this_sd; |
94aafc3e | 6246 | u64 time = 0; |
10e2f1ac | 6247 | |
9cfb38a7 WL |
6248 | this_sd = rcu_dereference(*this_cpu_ptr(&sd_llc)); |
6249 | if (!this_sd) | |
6250 | return -1; | |
6251 | ||
bae4ec13 MG |
6252 | cpumask_and(cpus, sched_domain_span(sd), p->cpus_ptr); |
6253 | ||
c722f35b | 6254 | if (sched_feat(SIS_PROP) && !has_idle_core) { |
e6e0dc2d | 6255 | u64 avg_cost, avg_idle, span_avg; |
94aafc3e | 6256 | unsigned long now = jiffies; |
1ad3aaf3 | 6257 | |
e6e0dc2d | 6258 | /* |
94aafc3e PZ |
6259 | * If we're busy, the assumption that the last idle period |
6260 | * predicts the future is flawed; age away the remaining | |
6261 | * predicted idle time. | |
e6e0dc2d | 6262 | */ |
94aafc3e PZ |
6263 | if (unlikely(this_rq->wake_stamp < now)) { |
6264 | while (this_rq->wake_stamp < now && this_rq->wake_avg_idle) { | |
6265 | this_rq->wake_stamp++; | |
6266 | this_rq->wake_avg_idle >>= 1; | |
6267 | } | |
6268 | } | |
6269 | ||
6270 | avg_idle = this_rq->wake_avg_idle; | |
e6e0dc2d | 6271 | avg_cost = this_sd->avg_scan_cost + 1; |
10e2f1ac | 6272 | |
e6e0dc2d | 6273 | span_avg = sd->span_weight * avg_idle; |
1ad3aaf3 PZ |
6274 | if (span_avg > 4*avg_cost) |
6275 | nr = div_u64(span_avg, avg_cost); | |
6276 | else | |
6277 | nr = 4; | |
10e2f1ac | 6278 | |
bae4ec13 MG |
6279 | time = cpu_clock(this); |
6280 | } | |
60588bfa | 6281 | |
56498cfb | 6282 | for_each_cpu_wrap(cpu, cpus, target + 1) { |
c722f35b | 6283 | if (has_idle_core) { |
9fe1f127 MG |
6284 | i = select_idle_core(p, cpu, cpus, &idle_cpu); |
6285 | if ((unsigned int)i < nr_cpumask_bits) | |
6286 | return i; | |
6287 | ||
6288 | } else { | |
6289 | if (!--nr) | |
6290 | return -1; | |
97886d9d | 6291 | idle_cpu = __select_idle_cpu(cpu, p); |
9fe1f127 MG |
6292 | if ((unsigned int)idle_cpu < nr_cpumask_bits) |
6293 | break; | |
6294 | } | |
10e2f1ac PZ |
6295 | } |
6296 | ||
c722f35b | 6297 | if (has_idle_core) |
02dbb724 | 6298 | set_idle_cores(target, false); |
9fe1f127 | 6299 | |
c722f35b | 6300 | if (sched_feat(SIS_PROP) && !has_idle_core) { |
bae4ec13 | 6301 | time = cpu_clock(this) - time; |
94aafc3e PZ |
6302 | |
6303 | /* | |
6304 | * Account for the scan cost of wakeups against the average | |
6305 | * idle time. | |
6306 | */ | |
6307 | this_rq->wake_avg_idle -= min(this_rq->wake_avg_idle, time); | |
6308 | ||
bae4ec13 MG |
6309 | update_avg(&this_sd->avg_scan_cost, time); |
6310 | } | |
10e2f1ac | 6311 | |
9fe1f127 | 6312 | return idle_cpu; |
10e2f1ac PZ |
6313 | } |
6314 | ||
b7a33161 MR |
6315 | /* |
6316 | * Scan the asym_capacity domain for idle CPUs; pick the first idle one on which | |
6317 | * the task fits. If no CPU is big enough, but there are idle ones, try to | |
6318 | * maximize capacity. | |
6319 | */ | |
6320 | static int | |
6321 | select_idle_capacity(struct task_struct *p, struct sched_domain *sd, int target) | |
6322 | { | |
b4c9c9f1 | 6323 | unsigned long task_util, best_cap = 0; |
b7a33161 MR |
6324 | int cpu, best_cpu = -1; |
6325 | struct cpumask *cpus; | |
6326 | ||
b7a33161 MR |
6327 | cpus = this_cpu_cpumask_var_ptr(select_idle_mask); |
6328 | cpumask_and(cpus, sched_domain_span(sd), p->cpus_ptr); | |
6329 | ||
b4c9c9f1 VG |
6330 | task_util = uclamp_task_util(p); |
6331 | ||
b7a33161 MR |
6332 | for_each_cpu_wrap(cpu, cpus, target) { |
6333 | unsigned long cpu_cap = capacity_of(cpu); | |
6334 | ||
6335 | if (!available_idle_cpu(cpu) && !sched_idle_cpu(cpu)) | |
6336 | continue; | |
b4c9c9f1 | 6337 | if (fits_capacity(task_util, cpu_cap)) |
b7a33161 MR |
6338 | return cpu; |
6339 | ||
6340 | if (cpu_cap > best_cap) { | |
6341 | best_cap = cpu_cap; | |
6342 | best_cpu = cpu; | |
6343 | } | |
6344 | } | |
6345 | ||
6346 | return best_cpu; | |
6347 | } | |
6348 | ||
b4c9c9f1 VG |
6349 | static inline bool asym_fits_capacity(int task_util, int cpu) |
6350 | { | |
6351 | if (static_branch_unlikely(&sched_asym_cpucapacity)) | |
6352 | return fits_capacity(task_util, capacity_of(cpu)); | |
6353 | ||
6354 | return true; | |
6355 | } | |
6356 | ||
10e2f1ac PZ |
6357 | /* |
6358 | * Try and locate an idle core/thread in the LLC cache domain. | |
a50bde51 | 6359 | */ |
772bd008 | 6360 | static int select_idle_sibling(struct task_struct *p, int prev, int target) |
a50bde51 | 6361 | { |
c722f35b | 6362 | bool has_idle_core = false; |
99bd5e2f | 6363 | struct sched_domain *sd; |
b4c9c9f1 | 6364 | unsigned long task_util; |
32e839dd | 6365 | int i, recent_used_cpu; |
a50bde51 | 6366 | |
b7a33161 | 6367 | /* |
b4c9c9f1 VG |
6368 | * On asymmetric system, update task utilization because we will check |
6369 | * that the task fits with cpu's capacity. | |
b7a33161 MR |
6370 | */ |
6371 | if (static_branch_unlikely(&sched_asym_cpucapacity)) { | |
b4c9c9f1 VG |
6372 | sync_entity_load_avg(&p->se); |
6373 | task_util = uclamp_task_util(p); | |
b7a33161 MR |
6374 | } |
6375 | ||
9099a147 PZ |
6376 | /* |
6377 | * per-cpu select_idle_mask usage | |
6378 | */ | |
6379 | lockdep_assert_irqs_disabled(); | |
6380 | ||
b4c9c9f1 VG |
6381 | if ((available_idle_cpu(target) || sched_idle_cpu(target)) && |
6382 | asym_fits_capacity(task_util, target)) | |
e0a79f52 | 6383 | return target; |
99bd5e2f SS |
6384 | |
6385 | /* | |
97fb7a0a | 6386 | * If the previous CPU is cache affine and idle, don't be stupid: |
99bd5e2f | 6387 | */ |
3c29e651 | 6388 | if (prev != target && cpus_share_cache(prev, target) && |
b4c9c9f1 VG |
6389 | (available_idle_cpu(prev) || sched_idle_cpu(prev)) && |
6390 | asym_fits_capacity(task_util, prev)) | |
772bd008 | 6391 | return prev; |
a50bde51 | 6392 | |
52262ee5 MG |
6393 | /* |
6394 | * Allow a per-cpu kthread to stack with the wakee if the | |
6395 | * kworker thread and the tasks previous CPUs are the same. | |
6396 | * The assumption is that the wakee queued work for the | |
6397 | * per-cpu kthread that is now complete and the wakeup is | |
6398 | * essentially a sync wakeup. An obvious example of this | |
6399 | * pattern is IO completions. | |
6400 | */ | |
6401 | if (is_per_cpu_kthread(current) && | |
6402 | prev == smp_processor_id() && | |
6403 | this_rq()->nr_running <= 1) { | |
6404 | return prev; | |
6405 | } | |
6406 | ||
97fb7a0a | 6407 | /* Check a recently used CPU as a potential idle candidate: */ |
32e839dd | 6408 | recent_used_cpu = p->recent_used_cpu; |
89aafd67 | 6409 | p->recent_used_cpu = prev; |
32e839dd MG |
6410 | if (recent_used_cpu != prev && |
6411 | recent_used_cpu != target && | |
6412 | cpus_share_cache(recent_used_cpu, target) && | |
3c29e651 | 6413 | (available_idle_cpu(recent_used_cpu) || sched_idle_cpu(recent_used_cpu)) && |
b4c9c9f1 VG |
6414 | cpumask_test_cpu(p->recent_used_cpu, p->cpus_ptr) && |
6415 | asym_fits_capacity(task_util, recent_used_cpu)) { | |
32e839dd MG |
6416 | return recent_used_cpu; |
6417 | } | |
6418 | ||
b4c9c9f1 VG |
6419 | /* |
6420 | * For asymmetric CPU capacity systems, our domain of interest is | |
6421 | * sd_asym_cpucapacity rather than sd_llc. | |
6422 | */ | |
6423 | if (static_branch_unlikely(&sched_asym_cpucapacity)) { | |
6424 | sd = rcu_dereference(per_cpu(sd_asym_cpucapacity, target)); | |
6425 | /* | |
6426 | * On an asymmetric CPU capacity system where an exclusive | |
6427 | * cpuset defines a symmetric island (i.e. one unique | |
6428 | * capacity_orig value through the cpuset), the key will be set | |
6429 | * but the CPUs within that cpuset will not have a domain with | |
6430 | * SD_ASYM_CPUCAPACITY. These should follow the usual symmetric | |
6431 | * capacity path. | |
6432 | */ | |
6433 | if (sd) { | |
6434 | i = select_idle_capacity(p, sd, target); | |
6435 | return ((unsigned)i < nr_cpumask_bits) ? i : target; | |
6436 | } | |
6437 | } | |
6438 | ||
518cd623 | 6439 | sd = rcu_dereference(per_cpu(sd_llc, target)); |
10e2f1ac PZ |
6440 | if (!sd) |
6441 | return target; | |
772bd008 | 6442 | |
c722f35b RR |
6443 | if (sched_smt_active()) { |
6444 | has_idle_core = test_idle_cores(target, false); | |
6445 | ||
6446 | if (!has_idle_core && cpus_share_cache(prev, target)) { | |
6447 | i = select_idle_smt(p, sd, prev); | |
6448 | if ((unsigned int)i < nr_cpumask_bits) | |
6449 | return i; | |
6450 | } | |
6451 | } | |
6452 | ||
6453 | i = select_idle_cpu(p, sd, has_idle_core, target); | |
10e2f1ac PZ |
6454 | if ((unsigned)i < nr_cpumask_bits) |
6455 | return i; | |
6456 | ||
a50bde51 PZ |
6457 | return target; |
6458 | } | |
231678b7 | 6459 | |
f9be3e59 | 6460 | /** |
59a74b15 | 6461 | * cpu_util - Estimates the amount of capacity of a CPU used by CFS tasks. |
f9be3e59 PB |
6462 | * @cpu: the CPU to get the utilization of |
6463 | * | |
6464 | * The unit of the return value must be the one of capacity so we can compare | |
6465 | * the utilization with the capacity of the CPU that is available for CFS task | |
6466 | * (ie cpu_capacity). | |
231678b7 DE |
6467 | * |
6468 | * cfs_rq.avg.util_avg is the sum of running time of runnable tasks plus the | |
6469 | * recent utilization of currently non-runnable tasks on a CPU. It represents | |
6470 | * the amount of utilization of a CPU in the range [0..capacity_orig] where | |
6471 | * capacity_orig is the cpu_capacity available at the highest frequency | |
6472 | * (arch_scale_freq_capacity()). | |
6473 | * The utilization of a CPU converges towards a sum equal to or less than the | |
6474 | * current capacity (capacity_curr <= capacity_orig) of the CPU because it is | |
6475 | * the running time on this CPU scaled by capacity_curr. | |
6476 | * | |
f9be3e59 PB |
6477 | * The estimated utilization of a CPU is defined to be the maximum between its |
6478 | * cfs_rq.avg.util_avg and the sum of the estimated utilization of the tasks | |
6479 | * currently RUNNABLE on that CPU. | |
6480 | * This allows to properly represent the expected utilization of a CPU which | |
6481 | * has just got a big task running since a long sleep period. At the same time | |
6482 | * however it preserves the benefits of the "blocked utilization" in | |
6483 | * describing the potential for other tasks waking up on the same CPU. | |
6484 | * | |
231678b7 DE |
6485 | * Nevertheless, cfs_rq.avg.util_avg can be higher than capacity_curr or even |
6486 | * higher than capacity_orig because of unfortunate rounding in | |
6487 | * cfs.avg.util_avg or just after migrating tasks and new task wakeups until | |
6488 | * the average stabilizes with the new running time. We need to check that the | |
6489 | * utilization stays within the range of [0..capacity_orig] and cap it if | |
6490 | * necessary. Without utilization capping, a group could be seen as overloaded | |
6491 | * (CPU0 utilization at 121% + CPU1 utilization at 80%) whereas CPU1 has 20% of | |
6492 | * available capacity. We allow utilization to overshoot capacity_curr (but not | |
6493 | * capacity_orig) as it useful for predicting the capacity required after task | |
6494 | * migrations (scheduler-driven DVFS). | |
f9be3e59 PB |
6495 | * |
6496 | * Return: the (estimated) utilization for the specified CPU | |
8bb5b00c | 6497 | */ |
f9be3e59 | 6498 | static inline unsigned long cpu_util(int cpu) |
8bb5b00c | 6499 | { |
f9be3e59 PB |
6500 | struct cfs_rq *cfs_rq; |
6501 | unsigned int util; | |
6502 | ||
6503 | cfs_rq = &cpu_rq(cpu)->cfs; | |
6504 | util = READ_ONCE(cfs_rq->avg.util_avg); | |
6505 | ||
6506 | if (sched_feat(UTIL_EST)) | |
6507 | util = max(util, READ_ONCE(cfs_rq->avg.util_est.enqueued)); | |
8bb5b00c | 6508 | |
f9be3e59 | 6509 | return min_t(unsigned long, util, capacity_orig_of(cpu)); |
8bb5b00c | 6510 | } |
a50bde51 | 6511 | |
104cb16d | 6512 | /* |
c469933e PB |
6513 | * cpu_util_without: compute cpu utilization without any contributions from *p |
6514 | * @cpu: the CPU which utilization is requested | |
6515 | * @p: the task which utilization should be discounted | |
6516 | * | |
6517 | * The utilization of a CPU is defined by the utilization of tasks currently | |
6518 | * enqueued on that CPU as well as tasks which are currently sleeping after an | |
6519 | * execution on that CPU. | |
6520 | * | |
6521 | * This method returns the utilization of the specified CPU by discounting the | |
6522 | * utilization of the specified task, whenever the task is currently | |
6523 | * contributing to the CPU utilization. | |
104cb16d | 6524 | */ |
c469933e | 6525 | static unsigned long cpu_util_without(int cpu, struct task_struct *p) |
104cb16d | 6526 | { |
f9be3e59 PB |
6527 | struct cfs_rq *cfs_rq; |
6528 | unsigned int util; | |
104cb16d MR |
6529 | |
6530 | /* Task has no contribution or is new */ | |
f9be3e59 | 6531 | if (cpu != task_cpu(p) || !READ_ONCE(p->se.avg.last_update_time)) |
104cb16d MR |
6532 | return cpu_util(cpu); |
6533 | ||
f9be3e59 PB |
6534 | cfs_rq = &cpu_rq(cpu)->cfs; |
6535 | util = READ_ONCE(cfs_rq->avg.util_avg); | |
6536 | ||
c469933e | 6537 | /* Discount task's util from CPU's util */ |
b5c0ce7b | 6538 | lsub_positive(&util, task_util(p)); |
104cb16d | 6539 | |
f9be3e59 PB |
6540 | /* |
6541 | * Covered cases: | |
6542 | * | |
6543 | * a) if *p is the only task sleeping on this CPU, then: | |
6544 | * cpu_util (== task_util) > util_est (== 0) | |
6545 | * and thus we return: | |
c469933e | 6546 | * cpu_util_without = (cpu_util - task_util) = 0 |
f9be3e59 PB |
6547 | * |
6548 | * b) if other tasks are SLEEPING on this CPU, which is now exiting | |
6549 | * IDLE, then: | |
6550 | * cpu_util >= task_util | |
6551 | * cpu_util > util_est (== 0) | |
6552 | * and thus we discount *p's blocked utilization to return: | |
c469933e | 6553 | * cpu_util_without = (cpu_util - task_util) >= 0 |
f9be3e59 PB |
6554 | * |
6555 | * c) if other tasks are RUNNABLE on that CPU and | |
6556 | * util_est > cpu_util | |
6557 | * then we use util_est since it returns a more restrictive | |
6558 | * estimation of the spare capacity on that CPU, by just | |
6559 | * considering the expected utilization of tasks already | |
6560 | * runnable on that CPU. | |
6561 | * | |
6562 | * Cases a) and b) are covered by the above code, while case c) is | |
6563 | * covered by the following code when estimated utilization is | |
6564 | * enabled. | |
6565 | */ | |
c469933e PB |
6566 | if (sched_feat(UTIL_EST)) { |
6567 | unsigned int estimated = | |
6568 | READ_ONCE(cfs_rq->avg.util_est.enqueued); | |
6569 | ||
6570 | /* | |
6571 | * Despite the following checks we still have a small window | |
6572 | * for a possible race, when an execl's select_task_rq_fair() | |
6573 | * races with LB's detach_task(): | |
6574 | * | |
6575 | * detach_task() | |
6576 | * p->on_rq = TASK_ON_RQ_MIGRATING; | |
6577 | * ---------------------------------- A | |
6578 | * deactivate_task() \ | |
6579 | * dequeue_task() + RaceTime | |
6580 | * util_est_dequeue() / | |
6581 | * ---------------------------------- B | |
6582 | * | |
6583 | * The additional check on "current == p" it's required to | |
6584 | * properly fix the execl regression and it helps in further | |
6585 | * reducing the chances for the above race. | |
6586 | */ | |
b5c0ce7b PB |
6587 | if (unlikely(task_on_rq_queued(p) || current == p)) |
6588 | lsub_positive(&estimated, _task_util_est(p)); | |
6589 | ||
c469933e PB |
6590 | util = max(util, estimated); |
6591 | } | |
f9be3e59 PB |
6592 | |
6593 | /* | |
6594 | * Utilization (estimated) can exceed the CPU capacity, thus let's | |
6595 | * clamp to the maximum CPU capacity to ensure consistency with | |
6596 | * the cpu_util call. | |
6597 | */ | |
6598 | return min_t(unsigned long, util, capacity_orig_of(cpu)); | |
104cb16d MR |
6599 | } |
6600 | ||
390031e4 QP |
6601 | /* |
6602 | * Predicts what cpu_util(@cpu) would return if @p was migrated (and enqueued) | |
6603 | * to @dst_cpu. | |
6604 | */ | |
6605 | static unsigned long cpu_util_next(int cpu, struct task_struct *p, int dst_cpu) | |
6606 | { | |
6607 | struct cfs_rq *cfs_rq = &cpu_rq(cpu)->cfs; | |
6608 | unsigned long util_est, util = READ_ONCE(cfs_rq->avg.util_avg); | |
6609 | ||
6610 | /* | |
6611 | * If @p migrates from @cpu to another, remove its contribution. Or, | |
6612 | * if @p migrates from another CPU to @cpu, add its contribution. In | |
6613 | * the other cases, @cpu is not impacted by the migration, so the | |
6614 | * util_avg should already be correct. | |
6615 | */ | |
6616 | if (task_cpu(p) == cpu && dst_cpu != cpu) | |
736cc6b3 | 6617 | lsub_positive(&util, task_util(p)); |
390031e4 QP |
6618 | else if (task_cpu(p) != cpu && dst_cpu == cpu) |
6619 | util += task_util(p); | |
6620 | ||
6621 | if (sched_feat(UTIL_EST)) { | |
6622 | util_est = READ_ONCE(cfs_rq->avg.util_est.enqueued); | |
6623 | ||
6624 | /* | |
6625 | * During wake-up, the task isn't enqueued yet and doesn't | |
6626 | * appear in the cfs_rq->avg.util_est.enqueued of any rq, | |
6627 | * so just add it (if needed) to "simulate" what will be | |
6628 | * cpu_util() after the task has been enqueued. | |
6629 | */ | |
6630 | if (dst_cpu == cpu) | |
6631 | util_est += _task_util_est(p); | |
6632 | ||
6633 | util = max(util, util_est); | |
6634 | } | |
6635 | ||
6636 | return min(util, capacity_orig_of(cpu)); | |
6637 | } | |
6638 | ||
6639 | /* | |
eb92692b | 6640 | * compute_energy(): Estimates the energy that @pd would consume if @p was |
390031e4 | 6641 | * migrated to @dst_cpu. compute_energy() predicts what will be the utilization |
eb92692b | 6642 | * landscape of @pd's CPUs after the task migration, and uses the Energy Model |
390031e4 QP |
6643 | * to compute what would be the energy if we decided to actually migrate that |
6644 | * task. | |
6645 | */ | |
6646 | static long | |
6647 | compute_energy(struct task_struct *p, int dst_cpu, struct perf_domain *pd) | |
6648 | { | |
eb92692b QP |
6649 | struct cpumask *pd_mask = perf_domain_span(pd); |
6650 | unsigned long cpu_cap = arch_scale_cpu_capacity(cpumask_first(pd_mask)); | |
6651 | unsigned long max_util = 0, sum_util = 0; | |
489f1645 | 6652 | unsigned long _cpu_cap = cpu_cap; |
390031e4 QP |
6653 | int cpu; |
6654 | ||
489f1645 LL |
6655 | _cpu_cap -= arch_scale_thermal_pressure(cpumask_first(pd_mask)); |
6656 | ||
eb92692b QP |
6657 | /* |
6658 | * The capacity state of CPUs of the current rd can be driven by CPUs | |
6659 | * of another rd if they belong to the same pd. So, account for the | |
6660 | * utilization of these CPUs too by masking pd with cpu_online_mask | |
6661 | * instead of the rd span. | |
6662 | * | |
6663 | * If an entire pd is outside of the current rd, it will not appear in | |
6664 | * its pd list and will not be accounted by compute_energy(). | |
6665 | */ | |
6666 | for_each_cpu_and(cpu, pd_mask, cpu_online_mask) { | |
0372e1cf VD |
6667 | unsigned long util_freq = cpu_util_next(cpu, p, dst_cpu); |
6668 | unsigned long cpu_util, util_running = util_freq; | |
6669 | struct task_struct *tsk = NULL; | |
6670 | ||
6671 | /* | |
6672 | * When @p is placed on @cpu: | |
6673 | * | |
6674 | * util_running = max(cpu_util, cpu_util_est) + | |
6675 | * max(task_util, _task_util_est) | |
6676 | * | |
6677 | * while cpu_util_next is: max(cpu_util + task_util, | |
6678 | * cpu_util_est + _task_util_est) | |
6679 | */ | |
6680 | if (cpu == dst_cpu) { | |
6681 | tsk = p; | |
6682 | util_running = | |
6683 | cpu_util_next(cpu, p, -1) + task_util_est(p); | |
6684 | } | |
af24bde8 PB |
6685 | |
6686 | /* | |
eb92692b QP |
6687 | * Busy time computation: utilization clamping is not |
6688 | * required since the ratio (sum_util / cpu_capacity) | |
6689 | * is already enough to scale the EM reported power | |
6690 | * consumption at the (eventually clamped) cpu_capacity. | |
af24bde8 | 6691 | */ |
489f1645 LL |
6692 | cpu_util = effective_cpu_util(cpu, util_running, cpu_cap, |
6693 | ENERGY_UTIL, NULL); | |
6694 | ||
6695 | sum_util += min(cpu_util, _cpu_cap); | |
af24bde8 | 6696 | |
390031e4 | 6697 | /* |
eb92692b QP |
6698 | * Performance domain frequency: utilization clamping |
6699 | * must be considered since it affects the selection | |
6700 | * of the performance domain frequency. | |
6701 | * NOTE: in case RT tasks are running, by default the | |
6702 | * FREQUENCY_UTIL's utilization can be max OPP. | |
390031e4 | 6703 | */ |
0372e1cf | 6704 | cpu_util = effective_cpu_util(cpu, util_freq, cpu_cap, |
eb92692b | 6705 | FREQUENCY_UTIL, tsk); |
489f1645 | 6706 | max_util = max(max_util, min(cpu_util, _cpu_cap)); |
390031e4 QP |
6707 | } |
6708 | ||
8f1b971b | 6709 | return em_cpu_energy(pd->em_pd, max_util, sum_util, _cpu_cap); |
390031e4 QP |
6710 | } |
6711 | ||
732cd75b QP |
6712 | /* |
6713 | * find_energy_efficient_cpu(): Find most energy-efficient target CPU for the | |
6714 | * waking task. find_energy_efficient_cpu() looks for the CPU with maximum | |
6715 | * spare capacity in each performance domain and uses it as a potential | |
6716 | * candidate to execute the task. Then, it uses the Energy Model to figure | |
6717 | * out which of the CPU candidates is the most energy-efficient. | |
6718 | * | |
6719 | * The rationale for this heuristic is as follows. In a performance domain, | |
6720 | * all the most energy efficient CPU candidates (according to the Energy | |
6721 | * Model) are those for which we'll request a low frequency. When there are | |
6722 | * several CPUs for which the frequency request will be the same, we don't | |
6723 | * have enough data to break the tie between them, because the Energy Model | |
6724 | * only includes active power costs. With this model, if we assume that | |
6725 | * frequency requests follow utilization (e.g. using schedutil), the CPU with | |
6726 | * the maximum spare capacity in a performance domain is guaranteed to be among | |
6727 | * the best candidates of the performance domain. | |
6728 | * | |
6729 | * In practice, it could be preferable from an energy standpoint to pack | |
6730 | * small tasks on a CPU in order to let other CPUs go in deeper idle states, | |
6731 | * but that could also hurt our chances to go cluster idle, and we have no | |
6732 | * ways to tell with the current Energy Model if this is actually a good | |
6733 | * idea or not. So, find_energy_efficient_cpu() basically favors | |
6734 | * cluster-packing, and spreading inside a cluster. That should at least be | |
6735 | * a good thing for latency, and this is consistent with the idea that most | |
6736 | * of the energy savings of EAS come from the asymmetry of the system, and | |
6737 | * not so much from breaking the tie between identical CPUs. That's also the | |
6738 | * reason why EAS is enabled in the topology code only for systems where | |
6739 | * SD_ASYM_CPUCAPACITY is set. | |
6740 | * | |
6741 | * NOTE: Forkees are not accepted in the energy-aware wake-up path because | |
6742 | * they don't have any useful utilization data yet and it's not possible to | |
6743 | * forecast their impact on energy consumption. Consequently, they will be | |
6744 | * placed by find_idlest_cpu() on the least loaded CPU, which might turn out | |
6745 | * to be energy-inefficient in some use-cases. The alternative would be to | |
6746 | * bias new tasks towards specific types of CPUs first, or to try to infer | |
6747 | * their util_avg from the parent task, but those heuristics could hurt | |
6748 | * other use-cases too. So, until someone finds a better way to solve this, | |
6749 | * let's keep things simple by re-using the existing slow path. | |
6750 | */ | |
732cd75b QP |
6751 | static int find_energy_efficient_cpu(struct task_struct *p, int prev_cpu) |
6752 | { | |
eb92692b | 6753 | unsigned long prev_delta = ULONG_MAX, best_delta = ULONG_MAX; |
732cd75b | 6754 | struct root_domain *rd = cpu_rq(smp_processor_id())->rd; |
619e090c | 6755 | int cpu, best_energy_cpu = prev_cpu, target = -1; |
eb92692b | 6756 | unsigned long cpu_cap, util, base_energy = 0; |
732cd75b | 6757 | struct sched_domain *sd; |
eb92692b | 6758 | struct perf_domain *pd; |
732cd75b QP |
6759 | |
6760 | rcu_read_lock(); | |
6761 | pd = rcu_dereference(rd->pd); | |
6762 | if (!pd || READ_ONCE(rd->overutilized)) | |
619e090c | 6763 | goto unlock; |
732cd75b QP |
6764 | |
6765 | /* | |
6766 | * Energy-aware wake-up happens on the lowest sched_domain starting | |
6767 | * from sd_asym_cpucapacity spanning over this_cpu and prev_cpu. | |
6768 | */ | |
6769 | sd = rcu_dereference(*this_cpu_ptr(&sd_asym_cpucapacity)); | |
6770 | while (sd && !cpumask_test_cpu(prev_cpu, sched_domain_span(sd))) | |
6771 | sd = sd->parent; | |
6772 | if (!sd) | |
619e090c PG |
6773 | goto unlock; |
6774 | ||
6775 | target = prev_cpu; | |
732cd75b QP |
6776 | |
6777 | sync_entity_load_avg(&p->se); | |
6778 | if (!task_util_est(p)) | |
6779 | goto unlock; | |
6780 | ||
6781 | for (; pd; pd = pd->next) { | |
eb92692b | 6782 | unsigned long cur_delta, spare_cap, max_spare_cap = 0; |
8d4c97c1 | 6783 | bool compute_prev_delta = false; |
eb92692b | 6784 | unsigned long base_energy_pd; |
732cd75b QP |
6785 | int max_spare_cap_cpu = -1; |
6786 | ||
6787 | for_each_cpu_and(cpu, perf_domain_span(pd), sched_domain_span(sd)) { | |
3bd37062 | 6788 | if (!cpumask_test_cpu(cpu, p->cpus_ptr)) |
732cd75b QP |
6789 | continue; |
6790 | ||
732cd75b QP |
6791 | util = cpu_util_next(cpu, p, cpu); |
6792 | cpu_cap = capacity_of(cpu); | |
da0777d3 LL |
6793 | spare_cap = cpu_cap; |
6794 | lsub_positive(&spare_cap, util); | |
1d42509e VS |
6795 | |
6796 | /* | |
6797 | * Skip CPUs that cannot satisfy the capacity request. | |
6798 | * IOW, placing the task there would make the CPU | |
6799 | * overutilized. Take uclamp into account to see how | |
6800 | * much capacity we can get out of the CPU; this is | |
a5418be9 | 6801 | * aligned with sched_cpu_util(). |
1d42509e VS |
6802 | */ |
6803 | util = uclamp_rq_util_with(cpu_rq(cpu), util, p); | |
60e17f5c | 6804 | if (!fits_capacity(util, cpu_cap)) |
732cd75b QP |
6805 | continue; |
6806 | ||
732cd75b | 6807 | if (cpu == prev_cpu) { |
8d4c97c1 PG |
6808 | /* Always use prev_cpu as a candidate. */ |
6809 | compute_prev_delta = true; | |
6810 | } else if (spare_cap > max_spare_cap) { | |
6811 | /* | |
6812 | * Find the CPU with the maximum spare capacity | |
6813 | * in the performance domain. | |
6814 | */ | |
732cd75b QP |
6815 | max_spare_cap = spare_cap; |
6816 | max_spare_cap_cpu = cpu; | |
6817 | } | |
6818 | } | |
6819 | ||
8d4c97c1 PG |
6820 | if (max_spare_cap_cpu < 0 && !compute_prev_delta) |
6821 | continue; | |
6822 | ||
6823 | /* Compute the 'base' energy of the pd, without @p */ | |
6824 | base_energy_pd = compute_energy(p, -1, pd); | |
6825 | base_energy += base_energy_pd; | |
6826 | ||
6827 | /* Evaluate the energy impact of using prev_cpu. */ | |
6828 | if (compute_prev_delta) { | |
6829 | prev_delta = compute_energy(p, prev_cpu, pd); | |
619e090c PG |
6830 | if (prev_delta < base_energy_pd) |
6831 | goto unlock; | |
8d4c97c1 PG |
6832 | prev_delta -= base_energy_pd; |
6833 | best_delta = min(best_delta, prev_delta); | |
6834 | } | |
6835 | ||
6836 | /* Evaluate the energy impact of using max_spare_cap_cpu. */ | |
6837 | if (max_spare_cap_cpu >= 0) { | |
eb92692b | 6838 | cur_delta = compute_energy(p, max_spare_cap_cpu, pd); |
619e090c PG |
6839 | if (cur_delta < base_energy_pd) |
6840 | goto unlock; | |
eb92692b QP |
6841 | cur_delta -= base_energy_pd; |
6842 | if (cur_delta < best_delta) { | |
6843 | best_delta = cur_delta; | |
732cd75b QP |
6844 | best_energy_cpu = max_spare_cap_cpu; |
6845 | } | |
6846 | } | |
6847 | } | |
732cd75b QP |
6848 | rcu_read_unlock(); |
6849 | ||
6850 | /* | |
6851 | * Pick the best CPU if prev_cpu cannot be used, or if it saves at | |
6852 | * least 6% of the energy used by prev_cpu. | |
6853 | */ | |
619e090c PG |
6854 | if ((prev_delta == ULONG_MAX) || |
6855 | (prev_delta - best_delta) > ((prev_delta + base_energy) >> 4)) | |
6856 | target = best_energy_cpu; | |
732cd75b | 6857 | |
619e090c | 6858 | return target; |
732cd75b | 6859 | |
619e090c | 6860 | unlock: |
732cd75b QP |
6861 | rcu_read_unlock(); |
6862 | ||
619e090c | 6863 | return target; |
732cd75b QP |
6864 | } |
6865 | ||
aaee1203 | 6866 | /* |
de91b9cb | 6867 | * select_task_rq_fair: Select target runqueue for the waking task in domains |
3aef1551 | 6868 | * that have the relevant SD flag set. In practice, this is SD_BALANCE_WAKE, |
de91b9cb | 6869 | * SD_BALANCE_FORK, or SD_BALANCE_EXEC. |
aaee1203 | 6870 | * |
97fb7a0a IM |
6871 | * Balances load by selecting the idlest CPU in the idlest group, or under |
6872 | * certain conditions an idle sibling CPU if the domain has SD_WAKE_AFFINE set. | |
aaee1203 | 6873 | * |
97fb7a0a | 6874 | * Returns the target CPU number. |
aaee1203 | 6875 | */ |
0017d735 | 6876 | static int |
3aef1551 | 6877 | select_task_rq_fair(struct task_struct *p, int prev_cpu, int wake_flags) |
aaee1203 | 6878 | { |
3aef1551 | 6879 | int sync = (wake_flags & WF_SYNC) && !(current->flags & PF_EXITING); |
f1d88b44 | 6880 | struct sched_domain *tmp, *sd = NULL; |
c88d5910 | 6881 | int cpu = smp_processor_id(); |
63b0e9ed | 6882 | int new_cpu = prev_cpu; |
99bd5e2f | 6883 | int want_affine = 0; |
3aef1551 VS |
6884 | /* SD_flags and WF_flags share the first nibble */ |
6885 | int sd_flag = wake_flags & 0xF; | |
c88d5910 | 6886 | |
9099a147 PZ |
6887 | /* |
6888 | * required for stable ->cpus_allowed | |
6889 | */ | |
6890 | lockdep_assert_held(&p->pi_lock); | |
dc824eb8 | 6891 | if (wake_flags & WF_TTWU) { |
c58d25f3 | 6892 | record_wakee(p); |
732cd75b | 6893 | |
f8a696f2 | 6894 | if (sched_energy_enabled()) { |
732cd75b QP |
6895 | new_cpu = find_energy_efficient_cpu(p, prev_cpu); |
6896 | if (new_cpu >= 0) | |
6897 | return new_cpu; | |
6898 | new_cpu = prev_cpu; | |
6899 | } | |
6900 | ||
00061968 | 6901 | want_affine = !wake_wide(p) && cpumask_test_cpu(cpu, p->cpus_ptr); |
c58d25f3 | 6902 | } |
aaee1203 | 6903 | |
dce840a0 | 6904 | rcu_read_lock(); |
aaee1203 | 6905 | for_each_domain(cpu, tmp) { |
fe3bcfe1 | 6906 | /* |
97fb7a0a | 6907 | * If both 'cpu' and 'prev_cpu' are part of this domain, |
99bd5e2f | 6908 | * cpu is a valid SD_WAKE_AFFINE target. |
fe3bcfe1 | 6909 | */ |
99bd5e2f SS |
6910 | if (want_affine && (tmp->flags & SD_WAKE_AFFINE) && |
6911 | cpumask_test_cpu(prev_cpu, sched_domain_span(tmp))) { | |
f1d88b44 VK |
6912 | if (cpu != prev_cpu) |
6913 | new_cpu = wake_affine(tmp, p, cpu, prev_cpu, sync); | |
6914 | ||
6915 | sd = NULL; /* Prefer wake_affine over balance flags */ | |
29cd8bae | 6916 | break; |
f03542a7 | 6917 | } |
29cd8bae | 6918 | |
f03542a7 | 6919 | if (tmp->flags & sd_flag) |
29cd8bae | 6920 | sd = tmp; |
63b0e9ed MG |
6921 | else if (!want_affine) |
6922 | break; | |
29cd8bae PZ |
6923 | } |
6924 | ||
f1d88b44 VK |
6925 | if (unlikely(sd)) { |
6926 | /* Slow path */ | |
18bd1b4b | 6927 | new_cpu = find_idlest_cpu(sd, p, cpu, prev_cpu, sd_flag); |
dc824eb8 | 6928 | } else if (wake_flags & WF_TTWU) { /* XXX always ? */ |
f1d88b44 | 6929 | /* Fast path */ |
f1d88b44 | 6930 | new_cpu = select_idle_sibling(p, prev_cpu, new_cpu); |
e7693a36 | 6931 | } |
dce840a0 | 6932 | rcu_read_unlock(); |
e7693a36 | 6933 | |
c88d5910 | 6934 | return new_cpu; |
e7693a36 | 6935 | } |
0a74bef8 | 6936 | |
144d8487 PZ |
6937 | static void detach_entity_cfs_rq(struct sched_entity *se); |
6938 | ||
0a74bef8 | 6939 | /* |
97fb7a0a | 6940 | * Called immediately before a task is migrated to a new CPU; task_cpu(p) and |
0a74bef8 | 6941 | * cfs_rq_of(p) references at time of call are still valid and identify the |
97fb7a0a | 6942 | * previous CPU. The caller guarantees p->pi_lock or task_rq(p)->lock is held. |
0a74bef8 | 6943 | */ |
3f9672ba | 6944 | static void migrate_task_rq_fair(struct task_struct *p, int new_cpu) |
0a74bef8 | 6945 | { |
59efa0ba PZ |
6946 | /* |
6947 | * As blocked tasks retain absolute vruntime the migration needs to | |
6948 | * deal with this by subtracting the old and adding the new | |
6949 | * min_vruntime -- the latter is done by enqueue_entity() when placing | |
6950 | * the task on the new runqueue. | |
6951 | */ | |
2f064a59 | 6952 | if (READ_ONCE(p->__state) == TASK_WAKING) { |
59efa0ba PZ |
6953 | struct sched_entity *se = &p->se; |
6954 | struct cfs_rq *cfs_rq = cfs_rq_of(se); | |
6955 | u64 min_vruntime; | |
6956 | ||
6957 | #ifndef CONFIG_64BIT | |
6958 | u64 min_vruntime_copy; | |
6959 | ||
6960 | do { | |
6961 | min_vruntime_copy = cfs_rq->min_vruntime_copy; | |
6962 | smp_rmb(); | |
6963 | min_vruntime = cfs_rq->min_vruntime; | |
6964 | } while (min_vruntime != min_vruntime_copy); | |
6965 | #else | |
6966 | min_vruntime = cfs_rq->min_vruntime; | |
6967 | #endif | |
6968 | ||
6969 | se->vruntime -= min_vruntime; | |
6970 | } | |
6971 | ||
144d8487 PZ |
6972 | if (p->on_rq == TASK_ON_RQ_MIGRATING) { |
6973 | /* | |
6974 | * In case of TASK_ON_RQ_MIGRATING we in fact hold the 'old' | |
6975 | * rq->lock and can modify state directly. | |
6976 | */ | |
5cb9eaa3 | 6977 | lockdep_assert_rq_held(task_rq(p)); |
144d8487 PZ |
6978 | detach_entity_cfs_rq(&p->se); |
6979 | ||
6980 | } else { | |
6981 | /* | |
6982 | * We are supposed to update the task to "current" time, then | |
6983 | * its up to date and ready to go to new CPU/cfs_rq. But we | |
6984 | * have difficulty in getting what current time is, so simply | |
6985 | * throw away the out-of-date time. This will result in the | |
6986 | * wakee task is less decayed, but giving the wakee more load | |
6987 | * sounds not bad. | |
6988 | */ | |
6989 | remove_entity_load_avg(&p->se); | |
6990 | } | |
9d89c257 YD |
6991 | |
6992 | /* Tell new CPU we are migrated */ | |
6993 | p->se.avg.last_update_time = 0; | |
3944a927 BS |
6994 | |
6995 | /* We have migrated, no longer consider this task hot */ | |
9d89c257 | 6996 | p->se.exec_start = 0; |
3f9672ba SD |
6997 | |
6998 | update_scan_period(p, new_cpu); | |
0a74bef8 | 6999 | } |
12695578 YD |
7000 | |
7001 | static void task_dead_fair(struct task_struct *p) | |
7002 | { | |
7003 | remove_entity_load_avg(&p->se); | |
7004 | } | |
6e2df058 PZ |
7005 | |
7006 | static int | |
7007 | balance_fair(struct rq *rq, struct task_struct *prev, struct rq_flags *rf) | |
7008 | { | |
7009 | if (rq->nr_running) | |
7010 | return 1; | |
7011 | ||
7012 | return newidle_balance(rq, rf) != 0; | |
7013 | } | |
e7693a36 GH |
7014 | #endif /* CONFIG_SMP */ |
7015 | ||
a555e9d8 | 7016 | static unsigned long wakeup_gran(struct sched_entity *se) |
0bbd3336 PZ |
7017 | { |
7018 | unsigned long gran = sysctl_sched_wakeup_granularity; | |
7019 | ||
7020 | /* | |
e52fb7c0 PZ |
7021 | * Since its curr running now, convert the gran from real-time |
7022 | * to virtual-time in his units. | |
13814d42 MG |
7023 | * |
7024 | * By using 'se' instead of 'curr' we penalize light tasks, so | |
7025 | * they get preempted easier. That is, if 'se' < 'curr' then | |
7026 | * the resulting gran will be larger, therefore penalizing the | |
7027 | * lighter, if otoh 'se' > 'curr' then the resulting gran will | |
7028 | * be smaller, again penalizing the lighter task. | |
7029 | * | |
7030 | * This is especially important for buddies when the leftmost | |
7031 | * task is higher priority than the buddy. | |
0bbd3336 | 7032 | */ |
f4ad9bd2 | 7033 | return calc_delta_fair(gran, se); |
0bbd3336 PZ |
7034 | } |
7035 | ||
464b7527 PZ |
7036 | /* |
7037 | * Should 'se' preempt 'curr'. | |
7038 | * | |
7039 | * |s1 | |
7040 | * |s2 | |
7041 | * |s3 | |
7042 | * g | |
7043 | * |<--->|c | |
7044 | * | |
7045 | * w(c, s1) = -1 | |
7046 | * w(c, s2) = 0 | |
7047 | * w(c, s3) = 1 | |
7048 | * | |
7049 | */ | |
7050 | static int | |
7051 | wakeup_preempt_entity(struct sched_entity *curr, struct sched_entity *se) | |
7052 | { | |
7053 | s64 gran, vdiff = curr->vruntime - se->vruntime; | |
7054 | ||
7055 | if (vdiff <= 0) | |
7056 | return -1; | |
7057 | ||
a555e9d8 | 7058 | gran = wakeup_gran(se); |
464b7527 PZ |
7059 | if (vdiff > gran) |
7060 | return 1; | |
7061 | ||
7062 | return 0; | |
7063 | } | |
7064 | ||
02479099 PZ |
7065 | static void set_last_buddy(struct sched_entity *se) |
7066 | { | |
c5ae366e DA |
7067 | for_each_sched_entity(se) { |
7068 | if (SCHED_WARN_ON(!se->on_rq)) | |
7069 | return; | |
30400039 JD |
7070 | if (se_is_idle(se)) |
7071 | return; | |
69c80f3e | 7072 | cfs_rq_of(se)->last = se; |
c5ae366e | 7073 | } |
02479099 PZ |
7074 | } |
7075 | ||
7076 | static void set_next_buddy(struct sched_entity *se) | |
7077 | { | |
c5ae366e DA |
7078 | for_each_sched_entity(se) { |
7079 | if (SCHED_WARN_ON(!se->on_rq)) | |
7080 | return; | |
30400039 JD |
7081 | if (se_is_idle(se)) |
7082 | return; | |
69c80f3e | 7083 | cfs_rq_of(se)->next = se; |
c5ae366e | 7084 | } |
02479099 PZ |
7085 | } |
7086 | ||
ac53db59 RR |
7087 | static void set_skip_buddy(struct sched_entity *se) |
7088 | { | |
69c80f3e VP |
7089 | for_each_sched_entity(se) |
7090 | cfs_rq_of(se)->skip = se; | |
ac53db59 RR |
7091 | } |
7092 | ||
bf0f6f24 IM |
7093 | /* |
7094 | * Preempt the current task with a newly woken task if needed: | |
7095 | */ | |
5a9b86f6 | 7096 | static void check_preempt_wakeup(struct rq *rq, struct task_struct *p, int wake_flags) |
bf0f6f24 IM |
7097 | { |
7098 | struct task_struct *curr = rq->curr; | |
8651a86c | 7099 | struct sched_entity *se = &curr->se, *pse = &p->se; |
03e89e45 | 7100 | struct cfs_rq *cfs_rq = task_cfs_rq(curr); |
f685ceac | 7101 | int scale = cfs_rq->nr_running >= sched_nr_latency; |
2f36825b | 7102 | int next_buddy_marked = 0; |
30400039 | 7103 | int cse_is_idle, pse_is_idle; |
bf0f6f24 | 7104 | |
4ae7d5ce IM |
7105 | if (unlikely(se == pse)) |
7106 | return; | |
7107 | ||
5238cdd3 | 7108 | /* |
163122b7 | 7109 | * This is possible from callers such as attach_tasks(), in which we |
3b03706f | 7110 | * unconditionally check_preempt_curr() after an enqueue (which may have |
5238cdd3 PT |
7111 | * lead to a throttle). This both saves work and prevents false |
7112 | * next-buddy nomination below. | |
7113 | */ | |
7114 | if (unlikely(throttled_hierarchy(cfs_rq_of(pse)))) | |
7115 | return; | |
7116 | ||
2f36825b | 7117 | if (sched_feat(NEXT_BUDDY) && scale && !(wake_flags & WF_FORK)) { |
3cb63d52 | 7118 | set_next_buddy(pse); |
2f36825b VP |
7119 | next_buddy_marked = 1; |
7120 | } | |
57fdc26d | 7121 | |
aec0a514 BR |
7122 | /* |
7123 | * We can come here with TIF_NEED_RESCHED already set from new task | |
7124 | * wake up path. | |
5238cdd3 PT |
7125 | * |
7126 | * Note: this also catches the edge-case of curr being in a throttled | |
7127 | * group (e.g. via set_curr_task), since update_curr() (in the | |
7128 | * enqueue of curr) will have resulted in resched being set. This | |
7129 | * prevents us from potentially nominating it as a false LAST_BUDDY | |
7130 | * below. | |
aec0a514 BR |
7131 | */ |
7132 | if (test_tsk_need_resched(curr)) | |
7133 | return; | |
7134 | ||
a2f5c9ab | 7135 | /* Idle tasks are by definition preempted by non-idle tasks. */ |
1da1843f VK |
7136 | if (unlikely(task_has_idle_policy(curr)) && |
7137 | likely(!task_has_idle_policy(p))) | |
a2f5c9ab DH |
7138 | goto preempt; |
7139 | ||
91c234b4 | 7140 | /* |
a2f5c9ab DH |
7141 | * Batch and idle tasks do not preempt non-idle tasks (their preemption |
7142 | * is driven by the tick): | |
91c234b4 | 7143 | */ |
8ed92e51 | 7144 | if (unlikely(p->policy != SCHED_NORMAL) || !sched_feat(WAKEUP_PREEMPTION)) |
91c234b4 | 7145 | return; |
bf0f6f24 | 7146 | |
464b7527 | 7147 | find_matching_se(&se, &pse); |
002f128b | 7148 | BUG_ON(!pse); |
30400039 JD |
7149 | |
7150 | cse_is_idle = se_is_idle(se); | |
7151 | pse_is_idle = se_is_idle(pse); | |
7152 | ||
7153 | /* | |
7154 | * Preempt an idle group in favor of a non-idle group (and don't preempt | |
7155 | * in the inverse case). | |
7156 | */ | |
7157 | if (cse_is_idle && !pse_is_idle) | |
7158 | goto preempt; | |
7159 | if (cse_is_idle != pse_is_idle) | |
7160 | return; | |
7161 | ||
7162 | update_curr(cfs_rq_of(se)); | |
2f36825b VP |
7163 | if (wakeup_preempt_entity(se, pse) == 1) { |
7164 | /* | |
7165 | * Bias pick_next to pick the sched entity that is | |
7166 | * triggering this preemption. | |
7167 | */ | |
7168 | if (!next_buddy_marked) | |
7169 | set_next_buddy(pse); | |
3a7e73a2 | 7170 | goto preempt; |
2f36825b | 7171 | } |
464b7527 | 7172 | |
3a7e73a2 | 7173 | return; |
a65ac745 | 7174 | |
3a7e73a2 | 7175 | preempt: |
8875125e | 7176 | resched_curr(rq); |
3a7e73a2 PZ |
7177 | /* |
7178 | * Only set the backward buddy when the current task is still | |
7179 | * on the rq. This can happen when a wakeup gets interleaved | |
7180 | * with schedule on the ->pre_schedule() or idle_balance() | |
7181 | * point, either of which can * drop the rq lock. | |
7182 | * | |
7183 | * Also, during early boot the idle thread is in the fair class, | |
7184 | * for obvious reasons its a bad idea to schedule back to it. | |
7185 | */ | |
7186 | if (unlikely(!se->on_rq || curr == rq->idle)) | |
7187 | return; | |
7188 | ||
7189 | if (sched_feat(LAST_BUDDY) && scale && entity_is_task(se)) | |
7190 | set_last_buddy(se); | |
bf0f6f24 IM |
7191 | } |
7192 | ||
21f56ffe PZ |
7193 | #ifdef CONFIG_SMP |
7194 | static struct task_struct *pick_task_fair(struct rq *rq) | |
7195 | { | |
7196 | struct sched_entity *se; | |
7197 | struct cfs_rq *cfs_rq; | |
7198 | ||
7199 | again: | |
7200 | cfs_rq = &rq->cfs; | |
7201 | if (!cfs_rq->nr_running) | |
7202 | return NULL; | |
7203 | ||
7204 | do { | |
7205 | struct sched_entity *curr = cfs_rq->curr; | |
7206 | ||
7207 | /* When we pick for a remote RQ, we'll not have done put_prev_entity() */ | |
7208 | if (curr) { | |
7209 | if (curr->on_rq) | |
7210 | update_curr(cfs_rq); | |
7211 | else | |
7212 | curr = NULL; | |
7213 | ||
7214 | if (unlikely(check_cfs_rq_runtime(cfs_rq))) | |
7215 | goto again; | |
7216 | } | |
7217 | ||
7218 | se = pick_next_entity(cfs_rq, curr); | |
7219 | cfs_rq = group_cfs_rq(se); | |
7220 | } while (cfs_rq); | |
7221 | ||
7222 | return task_of(se); | |
7223 | } | |
7224 | #endif | |
7225 | ||
5d7d6056 | 7226 | struct task_struct * |
d8ac8971 | 7227 | pick_next_task_fair(struct rq *rq, struct task_struct *prev, struct rq_flags *rf) |
bf0f6f24 IM |
7228 | { |
7229 | struct cfs_rq *cfs_rq = &rq->cfs; | |
7230 | struct sched_entity *se; | |
678d5718 | 7231 | struct task_struct *p; |
37e117c0 | 7232 | int new_tasks; |
678d5718 | 7233 | |
6e83125c | 7234 | again: |
6e2df058 | 7235 | if (!sched_fair_runnable(rq)) |
38033c37 | 7236 | goto idle; |
678d5718 | 7237 | |
9674f5ca | 7238 | #ifdef CONFIG_FAIR_GROUP_SCHED |
67692435 | 7239 | if (!prev || prev->sched_class != &fair_sched_class) |
678d5718 PZ |
7240 | goto simple; |
7241 | ||
7242 | /* | |
7243 | * Because of the set_next_buddy() in dequeue_task_fair() it is rather | |
7244 | * likely that a next task is from the same cgroup as the current. | |
7245 | * | |
7246 | * Therefore attempt to avoid putting and setting the entire cgroup | |
7247 | * hierarchy, only change the part that actually changes. | |
7248 | */ | |
7249 | ||
7250 | do { | |
7251 | struct sched_entity *curr = cfs_rq->curr; | |
7252 | ||
7253 | /* | |
7254 | * Since we got here without doing put_prev_entity() we also | |
7255 | * have to consider cfs_rq->curr. If it is still a runnable | |
7256 | * entity, update_curr() will update its vruntime, otherwise | |
7257 | * forget we've ever seen it. | |
7258 | */ | |
54d27365 BS |
7259 | if (curr) { |
7260 | if (curr->on_rq) | |
7261 | update_curr(cfs_rq); | |
7262 | else | |
7263 | curr = NULL; | |
678d5718 | 7264 | |
54d27365 BS |
7265 | /* |
7266 | * This call to check_cfs_rq_runtime() will do the | |
7267 | * throttle and dequeue its entity in the parent(s). | |
9674f5ca | 7268 | * Therefore the nr_running test will indeed |
54d27365 BS |
7269 | * be correct. |
7270 | */ | |
9674f5ca VK |
7271 | if (unlikely(check_cfs_rq_runtime(cfs_rq))) { |
7272 | cfs_rq = &rq->cfs; | |
7273 | ||
7274 | if (!cfs_rq->nr_running) | |
7275 | goto idle; | |
7276 | ||
54d27365 | 7277 | goto simple; |
9674f5ca | 7278 | } |
54d27365 | 7279 | } |
678d5718 PZ |
7280 | |
7281 | se = pick_next_entity(cfs_rq, curr); | |
7282 | cfs_rq = group_cfs_rq(se); | |
7283 | } while (cfs_rq); | |
7284 | ||
7285 | p = task_of(se); | |
7286 | ||
7287 | /* | |
7288 | * Since we haven't yet done put_prev_entity and if the selected task | |
7289 | * is a different task than we started out with, try and touch the | |
7290 | * least amount of cfs_rqs. | |
7291 | */ | |
7292 | if (prev != p) { | |
7293 | struct sched_entity *pse = &prev->se; | |
7294 | ||
7295 | while (!(cfs_rq = is_same_group(se, pse))) { | |
7296 | int se_depth = se->depth; | |
7297 | int pse_depth = pse->depth; | |
7298 | ||
7299 | if (se_depth <= pse_depth) { | |
7300 | put_prev_entity(cfs_rq_of(pse), pse); | |
7301 | pse = parent_entity(pse); | |
7302 | } | |
7303 | if (se_depth >= pse_depth) { | |
7304 | set_next_entity(cfs_rq_of(se), se); | |
7305 | se = parent_entity(se); | |
7306 | } | |
7307 | } | |
7308 | ||
7309 | put_prev_entity(cfs_rq, pse); | |
7310 | set_next_entity(cfs_rq, se); | |
7311 | } | |
7312 | ||
93824900 | 7313 | goto done; |
678d5718 | 7314 | simple: |
678d5718 | 7315 | #endif |
67692435 PZ |
7316 | if (prev) |
7317 | put_prev_task(rq, prev); | |
606dba2e | 7318 | |
bf0f6f24 | 7319 | do { |
678d5718 | 7320 | se = pick_next_entity(cfs_rq, NULL); |
f4b6755f | 7321 | set_next_entity(cfs_rq, se); |
bf0f6f24 IM |
7322 | cfs_rq = group_cfs_rq(se); |
7323 | } while (cfs_rq); | |
7324 | ||
8f4d37ec | 7325 | p = task_of(se); |
678d5718 | 7326 | |
13a453c2 | 7327 | done: __maybe_unused; |
93824900 UR |
7328 | #ifdef CONFIG_SMP |
7329 | /* | |
7330 | * Move the next running task to the front of | |
7331 | * the list, so our cfs_tasks list becomes MRU | |
7332 | * one. | |
7333 | */ | |
7334 | list_move(&p->se.group_node, &rq->cfs_tasks); | |
7335 | #endif | |
7336 | ||
e0ee463c | 7337 | if (hrtick_enabled_fair(rq)) |
b39e66ea | 7338 | hrtick_start_fair(rq, p); |
8f4d37ec | 7339 | |
3b1baa64 MR |
7340 | update_misfit_status(p, rq); |
7341 | ||
8f4d37ec | 7342 | return p; |
38033c37 PZ |
7343 | |
7344 | idle: | |
67692435 PZ |
7345 | if (!rf) |
7346 | return NULL; | |
7347 | ||
5ba553ef | 7348 | new_tasks = newidle_balance(rq, rf); |
46f69fa3 | 7349 | |
37e117c0 | 7350 | /* |
5ba553ef | 7351 | * Because newidle_balance() releases (and re-acquires) rq->lock, it is |
37e117c0 PZ |
7352 | * possible for any higher priority task to appear. In that case we |
7353 | * must re-start the pick_next_entity() loop. | |
7354 | */ | |
e4aa358b | 7355 | if (new_tasks < 0) |
37e117c0 PZ |
7356 | return RETRY_TASK; |
7357 | ||
e4aa358b | 7358 | if (new_tasks > 0) |
38033c37 | 7359 | goto again; |
38033c37 | 7360 | |
23127296 VG |
7361 | /* |
7362 | * rq is about to be idle, check if we need to update the | |
7363 | * lost_idle_time of clock_pelt | |
7364 | */ | |
7365 | update_idle_rq_clock_pelt(rq); | |
7366 | ||
38033c37 | 7367 | return NULL; |
bf0f6f24 IM |
7368 | } |
7369 | ||
98c2f700 PZ |
7370 | static struct task_struct *__pick_next_task_fair(struct rq *rq) |
7371 | { | |
7372 | return pick_next_task_fair(rq, NULL, NULL); | |
7373 | } | |
7374 | ||
bf0f6f24 IM |
7375 | /* |
7376 | * Account for a descheduled task: | |
7377 | */ | |
6e2df058 | 7378 | static void put_prev_task_fair(struct rq *rq, struct task_struct *prev) |
bf0f6f24 IM |
7379 | { |
7380 | struct sched_entity *se = &prev->se; | |
7381 | struct cfs_rq *cfs_rq; | |
7382 | ||
7383 | for_each_sched_entity(se) { | |
7384 | cfs_rq = cfs_rq_of(se); | |
ab6cde26 | 7385 | put_prev_entity(cfs_rq, se); |
bf0f6f24 IM |
7386 | } |
7387 | } | |
7388 | ||
ac53db59 RR |
7389 | /* |
7390 | * sched_yield() is very simple | |
7391 | * | |
7392 | * The magic of dealing with the ->skip buddy is in pick_next_entity. | |
7393 | */ | |
7394 | static void yield_task_fair(struct rq *rq) | |
7395 | { | |
7396 | struct task_struct *curr = rq->curr; | |
7397 | struct cfs_rq *cfs_rq = task_cfs_rq(curr); | |
7398 | struct sched_entity *se = &curr->se; | |
7399 | ||
7400 | /* | |
7401 | * Are we the only task in the tree? | |
7402 | */ | |
7403 | if (unlikely(rq->nr_running == 1)) | |
7404 | return; | |
7405 | ||
7406 | clear_buddies(cfs_rq, se); | |
7407 | ||
7408 | if (curr->policy != SCHED_BATCH) { | |
7409 | update_rq_clock(rq); | |
7410 | /* | |
7411 | * Update run-time statistics of the 'current'. | |
7412 | */ | |
7413 | update_curr(cfs_rq); | |
916671c0 MG |
7414 | /* |
7415 | * Tell update_rq_clock() that we've just updated, | |
7416 | * so we don't do microscopic update in schedule() | |
7417 | * and double the fastpath cost. | |
7418 | */ | |
adcc8da8 | 7419 | rq_clock_skip_update(rq); |
ac53db59 RR |
7420 | } |
7421 | ||
7422 | set_skip_buddy(se); | |
7423 | } | |
7424 | ||
0900acf2 | 7425 | static bool yield_to_task_fair(struct rq *rq, struct task_struct *p) |
d95f4122 MG |
7426 | { |
7427 | struct sched_entity *se = &p->se; | |
7428 | ||
5238cdd3 PT |
7429 | /* throttled hierarchies are not runnable */ |
7430 | if (!se->on_rq || throttled_hierarchy(cfs_rq_of(se))) | |
d95f4122 MG |
7431 | return false; |
7432 | ||
7433 | /* Tell the scheduler that we'd really like pse to run next. */ | |
7434 | set_next_buddy(se); | |
7435 | ||
d95f4122 MG |
7436 | yield_task_fair(rq); |
7437 | ||
7438 | return true; | |
7439 | } | |
7440 | ||
681f3e68 | 7441 | #ifdef CONFIG_SMP |
bf0f6f24 | 7442 | /************************************************** |
e9c84cb8 PZ |
7443 | * Fair scheduling class load-balancing methods. |
7444 | * | |
7445 | * BASICS | |
7446 | * | |
7447 | * The purpose of load-balancing is to achieve the same basic fairness the | |
97fb7a0a | 7448 | * per-CPU scheduler provides, namely provide a proportional amount of compute |
e9c84cb8 PZ |
7449 | * time to each task. This is expressed in the following equation: |
7450 | * | |
7451 | * W_i,n/P_i == W_j,n/P_j for all i,j (1) | |
7452 | * | |
97fb7a0a | 7453 | * Where W_i,n is the n-th weight average for CPU i. The instantaneous weight |
e9c84cb8 PZ |
7454 | * W_i,0 is defined as: |
7455 | * | |
7456 | * W_i,0 = \Sum_j w_i,j (2) | |
7457 | * | |
97fb7a0a | 7458 | * Where w_i,j is the weight of the j-th runnable task on CPU i. This weight |
1c3de5e1 | 7459 | * is derived from the nice value as per sched_prio_to_weight[]. |
e9c84cb8 PZ |
7460 | * |
7461 | * The weight average is an exponential decay average of the instantaneous | |
7462 | * weight: | |
7463 | * | |
7464 | * W'_i,n = (2^n - 1) / 2^n * W_i,n + 1 / 2^n * W_i,0 (3) | |
7465 | * | |
97fb7a0a | 7466 | * C_i is the compute capacity of CPU i, typically it is the |
e9c84cb8 PZ |
7467 | * fraction of 'recent' time available for SCHED_OTHER task execution. But it |
7468 | * can also include other factors [XXX]. | |
7469 | * | |
7470 | * To achieve this balance we define a measure of imbalance which follows | |
7471 | * directly from (1): | |
7472 | * | |
ced549fa | 7473 | * imb_i,j = max{ avg(W/C), W_i/C_i } - min{ avg(W/C), W_j/C_j } (4) |
e9c84cb8 PZ |
7474 | * |
7475 | * We them move tasks around to minimize the imbalance. In the continuous | |
7476 | * function space it is obvious this converges, in the discrete case we get | |
7477 | * a few fun cases generally called infeasible weight scenarios. | |
7478 | * | |
7479 | * [XXX expand on: | |
7480 | * - infeasible weights; | |
7481 | * - local vs global optima in the discrete case. ] | |
7482 | * | |
7483 | * | |
7484 | * SCHED DOMAINS | |
7485 | * | |
7486 | * In order to solve the imbalance equation (4), and avoid the obvious O(n^2) | |
97fb7a0a | 7487 | * for all i,j solution, we create a tree of CPUs that follows the hardware |
e9c84cb8 | 7488 | * topology where each level pairs two lower groups (or better). This results |
97fb7a0a | 7489 | * in O(log n) layers. Furthermore we reduce the number of CPUs going up the |
e9c84cb8 | 7490 | * tree to only the first of the previous level and we decrease the frequency |
97fb7a0a | 7491 | * of load-balance at each level inv. proportional to the number of CPUs in |
e9c84cb8 PZ |
7492 | * the groups. |
7493 | * | |
7494 | * This yields: | |
7495 | * | |
7496 | * log_2 n 1 n | |
7497 | * \Sum { --- * --- * 2^i } = O(n) (5) | |
7498 | * i = 0 2^i 2^i | |
7499 | * `- size of each group | |
97fb7a0a | 7500 | * | | `- number of CPUs doing load-balance |
e9c84cb8 PZ |
7501 | * | `- freq |
7502 | * `- sum over all levels | |
7503 | * | |
7504 | * Coupled with a limit on how many tasks we can migrate every balance pass, | |
7505 | * this makes (5) the runtime complexity of the balancer. | |
7506 | * | |
7507 | * An important property here is that each CPU is still (indirectly) connected | |
97fb7a0a | 7508 | * to every other CPU in at most O(log n) steps: |
e9c84cb8 PZ |
7509 | * |
7510 | * The adjacency matrix of the resulting graph is given by: | |
7511 | * | |
97a7142f | 7512 | * log_2 n |
e9c84cb8 PZ |
7513 | * A_i,j = \Union (i % 2^k == 0) && i / 2^(k+1) == j / 2^(k+1) (6) |
7514 | * k = 0 | |
7515 | * | |
7516 | * And you'll find that: | |
7517 | * | |
7518 | * A^(log_2 n)_i,j != 0 for all i,j (7) | |
7519 | * | |
97fb7a0a | 7520 | * Showing there's indeed a path between every CPU in at most O(log n) steps. |
e9c84cb8 PZ |
7521 | * The task movement gives a factor of O(m), giving a convergence complexity |
7522 | * of: | |
7523 | * | |
7524 | * O(nm log n), n := nr_cpus, m := nr_tasks (8) | |
7525 | * | |
7526 | * | |
7527 | * WORK CONSERVING | |
7528 | * | |
7529 | * In order to avoid CPUs going idle while there's still work to do, new idle | |
97fb7a0a | 7530 | * balancing is more aggressive and has the newly idle CPU iterate up the domain |
e9c84cb8 PZ |
7531 | * tree itself instead of relying on other CPUs to bring it work. |
7532 | * | |
7533 | * This adds some complexity to both (5) and (8) but it reduces the total idle | |
7534 | * time. | |
7535 | * | |
7536 | * [XXX more?] | |
7537 | * | |
7538 | * | |
7539 | * CGROUPS | |
7540 | * | |
7541 | * Cgroups make a horror show out of (2), instead of a simple sum we get: | |
7542 | * | |
7543 | * s_k,i | |
7544 | * W_i,0 = \Sum_j \Prod_k w_k * ----- (9) | |
7545 | * S_k | |
7546 | * | |
7547 | * Where | |
7548 | * | |
7549 | * s_k,i = \Sum_j w_i,j,k and S_k = \Sum_i s_k,i (10) | |
7550 | * | |
97fb7a0a | 7551 | * w_i,j,k is the weight of the j-th runnable task in the k-th cgroup on CPU i. |
e9c84cb8 PZ |
7552 | * |
7553 | * The big problem is S_k, its a global sum needed to compute a local (W_i) | |
7554 | * property. | |
7555 | * | |
7556 | * [XXX write more on how we solve this.. _after_ merging pjt's patches that | |
7557 | * rewrite all of this once again.] | |
97a7142f | 7558 | */ |
bf0f6f24 | 7559 | |
ed387b78 HS |
7560 | static unsigned long __read_mostly max_load_balance_interval = HZ/10; |
7561 | ||
0ec8aa00 PZ |
7562 | enum fbq_type { regular, remote, all }; |
7563 | ||
0b0695f2 | 7564 | /* |
a9723389 VG |
7565 | * 'group_type' describes the group of CPUs at the moment of load balancing. |
7566 | * | |
0b0695f2 | 7567 | * The enum is ordered by pulling priority, with the group with lowest priority |
a9723389 VG |
7568 | * first so the group_type can simply be compared when selecting the busiest |
7569 | * group. See update_sd_pick_busiest(). | |
0b0695f2 | 7570 | */ |
3b1baa64 | 7571 | enum group_type { |
a9723389 | 7572 | /* The group has spare capacity that can be used to run more tasks. */ |
0b0695f2 | 7573 | group_has_spare = 0, |
a9723389 VG |
7574 | /* |
7575 | * The group is fully used and the tasks don't compete for more CPU | |
7576 | * cycles. Nevertheless, some tasks might wait before running. | |
7577 | */ | |
0b0695f2 | 7578 | group_fully_busy, |
a9723389 VG |
7579 | /* |
7580 | * SD_ASYM_CPUCAPACITY only: One task doesn't fit with CPU's capacity | |
7581 | * and must be migrated to a more powerful CPU. | |
7582 | */ | |
3b1baa64 | 7583 | group_misfit_task, |
a9723389 VG |
7584 | /* |
7585 | * SD_ASYM_PACKING only: One local CPU with higher capacity is available, | |
7586 | * and the task should be migrated to it instead of running on the | |
7587 | * current CPU. | |
7588 | */ | |
0b0695f2 | 7589 | group_asym_packing, |
a9723389 VG |
7590 | /* |
7591 | * The tasks' affinity constraints previously prevented the scheduler | |
7592 | * from balancing the load across the system. | |
7593 | */ | |
3b1baa64 | 7594 | group_imbalanced, |
a9723389 VG |
7595 | /* |
7596 | * The CPU is overloaded and can't provide expected CPU cycles to all | |
7597 | * tasks. | |
7598 | */ | |
0b0695f2 VG |
7599 | group_overloaded |
7600 | }; | |
7601 | ||
7602 | enum migration_type { | |
7603 | migrate_load = 0, | |
7604 | migrate_util, | |
7605 | migrate_task, | |
7606 | migrate_misfit | |
3b1baa64 MR |
7607 | }; |
7608 | ||
ddcdf6e7 | 7609 | #define LBF_ALL_PINNED 0x01 |
367456c7 | 7610 | #define LBF_NEED_BREAK 0x02 |
6263322c PZ |
7611 | #define LBF_DST_PINNED 0x04 |
7612 | #define LBF_SOME_PINNED 0x08 | |
23fb06d9 | 7613 | #define LBF_ACTIVE_LB 0x10 |
ddcdf6e7 PZ |
7614 | |
7615 | struct lb_env { | |
7616 | struct sched_domain *sd; | |
7617 | ||
ddcdf6e7 | 7618 | struct rq *src_rq; |
85c1e7da | 7619 | int src_cpu; |
ddcdf6e7 PZ |
7620 | |
7621 | int dst_cpu; | |
7622 | struct rq *dst_rq; | |
7623 | ||
88b8dac0 SV |
7624 | struct cpumask *dst_grpmask; |
7625 | int new_dst_cpu; | |
ddcdf6e7 | 7626 | enum cpu_idle_type idle; |
bd939f45 | 7627 | long imbalance; |
b9403130 MW |
7628 | /* The set of CPUs under consideration for load-balancing */ |
7629 | struct cpumask *cpus; | |
7630 | ||
ddcdf6e7 | 7631 | unsigned int flags; |
367456c7 PZ |
7632 | |
7633 | unsigned int loop; | |
7634 | unsigned int loop_break; | |
7635 | unsigned int loop_max; | |
0ec8aa00 PZ |
7636 | |
7637 | enum fbq_type fbq_type; | |
0b0695f2 | 7638 | enum migration_type migration_type; |
163122b7 | 7639 | struct list_head tasks; |
ddcdf6e7 PZ |
7640 | }; |
7641 | ||
029632fb PZ |
7642 | /* |
7643 | * Is this task likely cache-hot: | |
7644 | */ | |
5d5e2b1b | 7645 | static int task_hot(struct task_struct *p, struct lb_env *env) |
029632fb PZ |
7646 | { |
7647 | s64 delta; | |
7648 | ||
5cb9eaa3 | 7649 | lockdep_assert_rq_held(env->src_rq); |
e5673f28 | 7650 | |
029632fb PZ |
7651 | if (p->sched_class != &fair_sched_class) |
7652 | return 0; | |
7653 | ||
1da1843f | 7654 | if (unlikely(task_has_idle_policy(p))) |
029632fb PZ |
7655 | return 0; |
7656 | ||
ec73240b JD |
7657 | /* SMT siblings share cache */ |
7658 | if (env->sd->flags & SD_SHARE_CPUCAPACITY) | |
7659 | return 0; | |
7660 | ||
029632fb PZ |
7661 | /* |
7662 | * Buddy candidates are cache hot: | |
7663 | */ | |
5d5e2b1b | 7664 | if (sched_feat(CACHE_HOT_BUDDY) && env->dst_rq->nr_running && |
029632fb PZ |
7665 | (&p->se == cfs_rq_of(&p->se)->next || |
7666 | &p->se == cfs_rq_of(&p->se)->last)) | |
7667 | return 1; | |
7668 | ||
7669 | if (sysctl_sched_migration_cost == -1) | |
7670 | return 1; | |
97886d9d AL |
7671 | |
7672 | /* | |
7673 | * Don't migrate task if the task's cookie does not match | |
7674 | * with the destination CPU's core cookie. | |
7675 | */ | |
7676 | if (!sched_core_cookie_match(cpu_rq(env->dst_cpu), p)) | |
7677 | return 1; | |
7678 | ||
029632fb PZ |
7679 | if (sysctl_sched_migration_cost == 0) |
7680 | return 0; | |
7681 | ||
5d5e2b1b | 7682 | delta = rq_clock_task(env->src_rq) - p->se.exec_start; |
029632fb PZ |
7683 | |
7684 | return delta < (s64)sysctl_sched_migration_cost; | |
7685 | } | |
7686 | ||
3a7053b3 | 7687 | #ifdef CONFIG_NUMA_BALANCING |
c1ceac62 | 7688 | /* |
2a1ed24c SD |
7689 | * Returns 1, if task migration degrades locality |
7690 | * Returns 0, if task migration improves locality i.e migration preferred. | |
7691 | * Returns -1, if task migration is not affected by locality. | |
c1ceac62 | 7692 | */ |
2a1ed24c | 7693 | static int migrate_degrades_locality(struct task_struct *p, struct lb_env *env) |
3a7053b3 | 7694 | { |
b1ad065e | 7695 | struct numa_group *numa_group = rcu_dereference(p->numa_group); |
f35678b6 SD |
7696 | unsigned long src_weight, dst_weight; |
7697 | int src_nid, dst_nid, dist; | |
3a7053b3 | 7698 | |
2a595721 | 7699 | if (!static_branch_likely(&sched_numa_balancing)) |
2a1ed24c SD |
7700 | return -1; |
7701 | ||
c3b9bc5b | 7702 | if (!p->numa_faults || !(env->sd->flags & SD_NUMA)) |
2a1ed24c | 7703 | return -1; |
7a0f3083 MG |
7704 | |
7705 | src_nid = cpu_to_node(env->src_cpu); | |
7706 | dst_nid = cpu_to_node(env->dst_cpu); | |
7707 | ||
83e1d2cd | 7708 | if (src_nid == dst_nid) |
2a1ed24c | 7709 | return -1; |
7a0f3083 | 7710 | |
2a1ed24c SD |
7711 | /* Migrating away from the preferred node is always bad. */ |
7712 | if (src_nid == p->numa_preferred_nid) { | |
7713 | if (env->src_rq->nr_running > env->src_rq->nr_preferred_running) | |
7714 | return 1; | |
7715 | else | |
7716 | return -1; | |
7717 | } | |
b1ad065e | 7718 | |
c1ceac62 RR |
7719 | /* Encourage migration to the preferred node. */ |
7720 | if (dst_nid == p->numa_preferred_nid) | |
2a1ed24c | 7721 | return 0; |
b1ad065e | 7722 | |
739294fb | 7723 | /* Leaving a core idle is often worse than degrading locality. */ |
f35678b6 | 7724 | if (env->idle == CPU_IDLE) |
739294fb RR |
7725 | return -1; |
7726 | ||
f35678b6 | 7727 | dist = node_distance(src_nid, dst_nid); |
c1ceac62 | 7728 | if (numa_group) { |
f35678b6 SD |
7729 | src_weight = group_weight(p, src_nid, dist); |
7730 | dst_weight = group_weight(p, dst_nid, dist); | |
c1ceac62 | 7731 | } else { |
f35678b6 SD |
7732 | src_weight = task_weight(p, src_nid, dist); |
7733 | dst_weight = task_weight(p, dst_nid, dist); | |
b1ad065e RR |
7734 | } |
7735 | ||
f35678b6 | 7736 | return dst_weight < src_weight; |
7a0f3083 MG |
7737 | } |
7738 | ||
3a7053b3 | 7739 | #else |
2a1ed24c | 7740 | static inline int migrate_degrades_locality(struct task_struct *p, |
3a7053b3 MG |
7741 | struct lb_env *env) |
7742 | { | |
2a1ed24c | 7743 | return -1; |
7a0f3083 | 7744 | } |
3a7053b3 MG |
7745 | #endif |
7746 | ||
1e3c88bd PZ |
7747 | /* |
7748 | * can_migrate_task - may task p from runqueue rq be migrated to this_cpu? | |
7749 | */ | |
7750 | static | |
8e45cb54 | 7751 | int can_migrate_task(struct task_struct *p, struct lb_env *env) |
1e3c88bd | 7752 | { |
2a1ed24c | 7753 | int tsk_cache_hot; |
e5673f28 | 7754 | |
5cb9eaa3 | 7755 | lockdep_assert_rq_held(env->src_rq); |
e5673f28 | 7756 | |
1e3c88bd PZ |
7757 | /* |
7758 | * We do not migrate tasks that are: | |
d3198084 | 7759 | * 1) throttled_lb_pair, or |
3bd37062 | 7760 | * 2) cannot be migrated to this CPU due to cpus_ptr, or |
d3198084 JK |
7761 | * 3) running (obviously), or |
7762 | * 4) are cache-hot on their current CPU. | |
1e3c88bd | 7763 | */ |
d3198084 JK |
7764 | if (throttled_lb_pair(task_group(p), env->src_cpu, env->dst_cpu)) |
7765 | return 0; | |
7766 | ||
9bcb959d | 7767 | /* Disregard pcpu kthreads; they are where they need to be. */ |
3a7956e2 | 7768 | if (kthread_is_per_cpu(p)) |
9bcb959d LC |
7769 | return 0; |
7770 | ||
3bd37062 | 7771 | if (!cpumask_test_cpu(env->dst_cpu, p->cpus_ptr)) { |
e02e60c1 | 7772 | int cpu; |
88b8dac0 | 7773 | |
ceeadb83 | 7774 | schedstat_inc(p->stats.nr_failed_migrations_affine); |
88b8dac0 | 7775 | |
6263322c PZ |
7776 | env->flags |= LBF_SOME_PINNED; |
7777 | ||
88b8dac0 | 7778 | /* |
97fb7a0a | 7779 | * Remember if this task can be migrated to any other CPU in |
88b8dac0 SV |
7780 | * our sched_group. We may want to revisit it if we couldn't |
7781 | * meet load balance goals by pulling other tasks on src_cpu. | |
7782 | * | |
23fb06d9 VS |
7783 | * Avoid computing new_dst_cpu |
7784 | * - for NEWLY_IDLE | |
7785 | * - if we have already computed one in current iteration | |
7786 | * - if it's an active balance | |
88b8dac0 | 7787 | */ |
23fb06d9 VS |
7788 | if (env->idle == CPU_NEWLY_IDLE || |
7789 | env->flags & (LBF_DST_PINNED | LBF_ACTIVE_LB)) | |
88b8dac0 SV |
7790 | return 0; |
7791 | ||
97fb7a0a | 7792 | /* Prevent to re-select dst_cpu via env's CPUs: */ |
e02e60c1 | 7793 | for_each_cpu_and(cpu, env->dst_grpmask, env->cpus) { |
3bd37062 | 7794 | if (cpumask_test_cpu(cpu, p->cpus_ptr)) { |
6263322c | 7795 | env->flags |= LBF_DST_PINNED; |
e02e60c1 JK |
7796 | env->new_dst_cpu = cpu; |
7797 | break; | |
7798 | } | |
88b8dac0 | 7799 | } |
e02e60c1 | 7800 | |
1e3c88bd PZ |
7801 | return 0; |
7802 | } | |
88b8dac0 | 7803 | |
3b03706f | 7804 | /* Record that we found at least one task that could run on dst_cpu */ |
8e45cb54 | 7805 | env->flags &= ~LBF_ALL_PINNED; |
1e3c88bd | 7806 | |
ddcdf6e7 | 7807 | if (task_running(env->src_rq, p)) { |
ceeadb83 | 7808 | schedstat_inc(p->stats.nr_failed_migrations_running); |
1e3c88bd PZ |
7809 | return 0; |
7810 | } | |
7811 | ||
7812 | /* | |
7813 | * Aggressive migration if: | |
23fb06d9 VS |
7814 | * 1) active balance |
7815 | * 2) destination numa is preferred | |
7816 | * 3) task is cache cold, or | |
7817 | * 4) too many balance attempts have failed. | |
1e3c88bd | 7818 | */ |
23fb06d9 VS |
7819 | if (env->flags & LBF_ACTIVE_LB) |
7820 | return 1; | |
7821 | ||
2a1ed24c SD |
7822 | tsk_cache_hot = migrate_degrades_locality(p, env); |
7823 | if (tsk_cache_hot == -1) | |
7824 | tsk_cache_hot = task_hot(p, env); | |
3a7053b3 | 7825 | |
2a1ed24c | 7826 | if (tsk_cache_hot <= 0 || |
7a96c231 | 7827 | env->sd->nr_balance_failed > env->sd->cache_nice_tries) { |
2a1ed24c | 7828 | if (tsk_cache_hot == 1) { |
ae92882e | 7829 | schedstat_inc(env->sd->lb_hot_gained[env->idle]); |
ceeadb83 | 7830 | schedstat_inc(p->stats.nr_forced_migrations); |
3a7053b3 | 7831 | } |
1e3c88bd PZ |
7832 | return 1; |
7833 | } | |
7834 | ||
ceeadb83 | 7835 | schedstat_inc(p->stats.nr_failed_migrations_hot); |
4e2dcb73 | 7836 | return 0; |
1e3c88bd PZ |
7837 | } |
7838 | ||
897c395f | 7839 | /* |
163122b7 KT |
7840 | * detach_task() -- detach the task for the migration specified in env |
7841 | */ | |
7842 | static void detach_task(struct task_struct *p, struct lb_env *env) | |
7843 | { | |
5cb9eaa3 | 7844 | lockdep_assert_rq_held(env->src_rq); |
163122b7 | 7845 | |
5704ac0a | 7846 | deactivate_task(env->src_rq, p, DEQUEUE_NOCLOCK); |
163122b7 KT |
7847 | set_task_cpu(p, env->dst_cpu); |
7848 | } | |
7849 | ||
897c395f | 7850 | /* |
e5673f28 | 7851 | * detach_one_task() -- tries to dequeue exactly one task from env->src_rq, as |
897c395f | 7852 | * part of active balancing operations within "domain". |
897c395f | 7853 | * |
e5673f28 | 7854 | * Returns a task if successful and NULL otherwise. |
897c395f | 7855 | */ |
e5673f28 | 7856 | static struct task_struct *detach_one_task(struct lb_env *env) |
897c395f | 7857 | { |
93824900 | 7858 | struct task_struct *p; |
897c395f | 7859 | |
5cb9eaa3 | 7860 | lockdep_assert_rq_held(env->src_rq); |
e5673f28 | 7861 | |
93824900 UR |
7862 | list_for_each_entry_reverse(p, |
7863 | &env->src_rq->cfs_tasks, se.group_node) { | |
367456c7 PZ |
7864 | if (!can_migrate_task(p, env)) |
7865 | continue; | |
897c395f | 7866 | |
163122b7 | 7867 | detach_task(p, env); |
e5673f28 | 7868 | |
367456c7 | 7869 | /* |
e5673f28 | 7870 | * Right now, this is only the second place where |
163122b7 | 7871 | * lb_gained[env->idle] is updated (other is detach_tasks) |
e5673f28 | 7872 | * so we can safely collect stats here rather than |
163122b7 | 7873 | * inside detach_tasks(). |
367456c7 | 7874 | */ |
ae92882e | 7875 | schedstat_inc(env->sd->lb_gained[env->idle]); |
e5673f28 | 7876 | return p; |
897c395f | 7877 | } |
e5673f28 | 7878 | return NULL; |
897c395f PZ |
7879 | } |
7880 | ||
eb95308e PZ |
7881 | static const unsigned int sched_nr_migrate_break = 32; |
7882 | ||
5d6523eb | 7883 | /* |
0b0695f2 | 7884 | * detach_tasks() -- tries to detach up to imbalance load/util/tasks from |
163122b7 | 7885 | * busiest_rq, as part of a balancing operation within domain "sd". |
5d6523eb | 7886 | * |
163122b7 | 7887 | * Returns number of detached tasks if successful and 0 otherwise. |
5d6523eb | 7888 | */ |
163122b7 | 7889 | static int detach_tasks(struct lb_env *env) |
1e3c88bd | 7890 | { |
5d6523eb | 7891 | struct list_head *tasks = &env->src_rq->cfs_tasks; |
0b0695f2 | 7892 | unsigned long util, load; |
5d6523eb | 7893 | struct task_struct *p; |
163122b7 KT |
7894 | int detached = 0; |
7895 | ||
5cb9eaa3 | 7896 | lockdep_assert_rq_held(env->src_rq); |
1e3c88bd | 7897 | |
acb4decc AL |
7898 | /* |
7899 | * Source run queue has been emptied by another CPU, clear | |
7900 | * LBF_ALL_PINNED flag as we will not test any task. | |
7901 | */ | |
7902 | if (env->src_rq->nr_running <= 1) { | |
7903 | env->flags &= ~LBF_ALL_PINNED; | |
7904 | return 0; | |
7905 | } | |
7906 | ||
bd939f45 | 7907 | if (env->imbalance <= 0) |
5d6523eb | 7908 | return 0; |
1e3c88bd | 7909 | |
5d6523eb | 7910 | while (!list_empty(tasks)) { |
985d3a4c YD |
7911 | /* |
7912 | * We don't want to steal all, otherwise we may be treated likewise, | |
7913 | * which could at worst lead to a livelock crash. | |
7914 | */ | |
7915 | if (env->idle != CPU_NOT_IDLE && env->src_rq->nr_running <= 1) | |
7916 | break; | |
7917 | ||
93824900 | 7918 | p = list_last_entry(tasks, struct task_struct, se.group_node); |
1e3c88bd | 7919 | |
367456c7 PZ |
7920 | env->loop++; |
7921 | /* We've more or less seen every task there is, call it quits */ | |
5d6523eb | 7922 | if (env->loop > env->loop_max) |
367456c7 | 7923 | break; |
5d6523eb PZ |
7924 | |
7925 | /* take a breather every nr_migrate tasks */ | |
367456c7 | 7926 | if (env->loop > env->loop_break) { |
eb95308e | 7927 | env->loop_break += sched_nr_migrate_break; |
8e45cb54 | 7928 | env->flags |= LBF_NEED_BREAK; |
ee00e66f | 7929 | break; |
a195f004 | 7930 | } |
1e3c88bd | 7931 | |
d3198084 | 7932 | if (!can_migrate_task(p, env)) |
367456c7 PZ |
7933 | goto next; |
7934 | ||
0b0695f2 VG |
7935 | switch (env->migration_type) { |
7936 | case migrate_load: | |
01cfcde9 VG |
7937 | /* |
7938 | * Depending of the number of CPUs and tasks and the | |
7939 | * cgroup hierarchy, task_h_load() can return a null | |
7940 | * value. Make sure that env->imbalance decreases | |
7941 | * otherwise detach_tasks() will stop only after | |
7942 | * detaching up to loop_max tasks. | |
7943 | */ | |
7944 | load = max_t(unsigned long, task_h_load(p), 1); | |
5d6523eb | 7945 | |
0b0695f2 VG |
7946 | if (sched_feat(LB_MIN) && |
7947 | load < 16 && !env->sd->nr_balance_failed) | |
7948 | goto next; | |
367456c7 | 7949 | |
6cf82d55 VG |
7950 | /* |
7951 | * Make sure that we don't migrate too much load. | |
7952 | * Nevertheless, let relax the constraint if | |
7953 | * scheduler fails to find a good waiting task to | |
7954 | * migrate. | |
7955 | */ | |
39a2a6eb | 7956 | if (shr_bound(load, env->sd->nr_balance_failed) > env->imbalance) |
0b0695f2 VG |
7957 | goto next; |
7958 | ||
7959 | env->imbalance -= load; | |
7960 | break; | |
7961 | ||
7962 | case migrate_util: | |
7963 | util = task_util_est(p); | |
7964 | ||
7965 | if (util > env->imbalance) | |
7966 | goto next; | |
7967 | ||
7968 | env->imbalance -= util; | |
7969 | break; | |
7970 | ||
7971 | case migrate_task: | |
7972 | env->imbalance--; | |
7973 | break; | |
7974 | ||
7975 | case migrate_misfit: | |
c63be7be VG |
7976 | /* This is not a misfit task */ |
7977 | if (task_fits_capacity(p, capacity_of(env->src_cpu))) | |
0b0695f2 VG |
7978 | goto next; |
7979 | ||
7980 | env->imbalance = 0; | |
7981 | break; | |
7982 | } | |
1e3c88bd | 7983 | |
163122b7 KT |
7984 | detach_task(p, env); |
7985 | list_add(&p->se.group_node, &env->tasks); | |
7986 | ||
7987 | detached++; | |
1e3c88bd | 7988 | |
c1a280b6 | 7989 | #ifdef CONFIG_PREEMPTION |
ee00e66f PZ |
7990 | /* |
7991 | * NEWIDLE balancing is a source of latency, so preemptible | |
163122b7 | 7992 | * kernels will stop after the first task is detached to minimize |
ee00e66f PZ |
7993 | * the critical section. |
7994 | */ | |
5d6523eb | 7995 | if (env->idle == CPU_NEWLY_IDLE) |
ee00e66f | 7996 | break; |
1e3c88bd PZ |
7997 | #endif |
7998 | ||
ee00e66f PZ |
7999 | /* |
8000 | * We only want to steal up to the prescribed amount of | |
0b0695f2 | 8001 | * load/util/tasks. |
ee00e66f | 8002 | */ |
bd939f45 | 8003 | if (env->imbalance <= 0) |
ee00e66f | 8004 | break; |
367456c7 PZ |
8005 | |
8006 | continue; | |
8007 | next: | |
93824900 | 8008 | list_move(&p->se.group_node, tasks); |
1e3c88bd | 8009 | } |
5d6523eb | 8010 | |
1e3c88bd | 8011 | /* |
163122b7 KT |
8012 | * Right now, this is one of only two places we collect this stat |
8013 | * so we can safely collect detach_one_task() stats here rather | |
8014 | * than inside detach_one_task(). | |
1e3c88bd | 8015 | */ |
ae92882e | 8016 | schedstat_add(env->sd->lb_gained[env->idle], detached); |
1e3c88bd | 8017 | |
163122b7 KT |
8018 | return detached; |
8019 | } | |
8020 | ||
8021 | /* | |
8022 | * attach_task() -- attach the task detached by detach_task() to its new rq. | |
8023 | */ | |
8024 | static void attach_task(struct rq *rq, struct task_struct *p) | |
8025 | { | |
5cb9eaa3 | 8026 | lockdep_assert_rq_held(rq); |
163122b7 KT |
8027 | |
8028 | BUG_ON(task_rq(p) != rq); | |
5704ac0a | 8029 | activate_task(rq, p, ENQUEUE_NOCLOCK); |
163122b7 KT |
8030 | check_preempt_curr(rq, p, 0); |
8031 | } | |
8032 | ||
8033 | /* | |
8034 | * attach_one_task() -- attaches the task returned from detach_one_task() to | |
8035 | * its new rq. | |
8036 | */ | |
8037 | static void attach_one_task(struct rq *rq, struct task_struct *p) | |
8038 | { | |
8a8c69c3 PZ |
8039 | struct rq_flags rf; |
8040 | ||
8041 | rq_lock(rq, &rf); | |
5704ac0a | 8042 | update_rq_clock(rq); |
163122b7 | 8043 | attach_task(rq, p); |
8a8c69c3 | 8044 | rq_unlock(rq, &rf); |
163122b7 KT |
8045 | } |
8046 | ||
8047 | /* | |
8048 | * attach_tasks() -- attaches all tasks detached by detach_tasks() to their | |
8049 | * new rq. | |
8050 | */ | |
8051 | static void attach_tasks(struct lb_env *env) | |
8052 | { | |
8053 | struct list_head *tasks = &env->tasks; | |
8054 | struct task_struct *p; | |
8a8c69c3 | 8055 | struct rq_flags rf; |
163122b7 | 8056 | |
8a8c69c3 | 8057 | rq_lock(env->dst_rq, &rf); |
5704ac0a | 8058 | update_rq_clock(env->dst_rq); |
163122b7 KT |
8059 | |
8060 | while (!list_empty(tasks)) { | |
8061 | p = list_first_entry(tasks, struct task_struct, se.group_node); | |
8062 | list_del_init(&p->se.group_node); | |
1e3c88bd | 8063 | |
163122b7 KT |
8064 | attach_task(env->dst_rq, p); |
8065 | } | |
8066 | ||
8a8c69c3 | 8067 | rq_unlock(env->dst_rq, &rf); |
1e3c88bd PZ |
8068 | } |
8069 | ||
b0c79224 | 8070 | #ifdef CONFIG_NO_HZ_COMMON |
1936c53c VG |
8071 | static inline bool cfs_rq_has_blocked(struct cfs_rq *cfs_rq) |
8072 | { | |
8073 | if (cfs_rq->avg.load_avg) | |
8074 | return true; | |
8075 | ||
8076 | if (cfs_rq->avg.util_avg) | |
8077 | return true; | |
8078 | ||
8079 | return false; | |
8080 | } | |
8081 | ||
91c27493 | 8082 | static inline bool others_have_blocked(struct rq *rq) |
371bf427 VG |
8083 | { |
8084 | if (READ_ONCE(rq->avg_rt.util_avg)) | |
8085 | return true; | |
8086 | ||
3727e0e1 VG |
8087 | if (READ_ONCE(rq->avg_dl.util_avg)) |
8088 | return true; | |
8089 | ||
b4eccf5f TG |
8090 | if (thermal_load_avg(rq)) |
8091 | return true; | |
8092 | ||
11d4afd4 | 8093 | #ifdef CONFIG_HAVE_SCHED_AVG_IRQ |
91c27493 VG |
8094 | if (READ_ONCE(rq->avg_irq.util_avg)) |
8095 | return true; | |
8096 | #endif | |
8097 | ||
371bf427 VG |
8098 | return false; |
8099 | } | |
8100 | ||
39b6a429 | 8101 | static inline void update_blocked_load_tick(struct rq *rq) |
b0c79224 | 8102 | { |
39b6a429 VG |
8103 | WRITE_ONCE(rq->last_blocked_load_update_tick, jiffies); |
8104 | } | |
b0c79224 | 8105 | |
39b6a429 VG |
8106 | static inline void update_blocked_load_status(struct rq *rq, bool has_blocked) |
8107 | { | |
b0c79224 VS |
8108 | if (!has_blocked) |
8109 | rq->has_blocked_load = 0; | |
8110 | } | |
8111 | #else | |
8112 | static inline bool cfs_rq_has_blocked(struct cfs_rq *cfs_rq) { return false; } | |
8113 | static inline bool others_have_blocked(struct rq *rq) { return false; } | |
39b6a429 | 8114 | static inline void update_blocked_load_tick(struct rq *rq) {} |
b0c79224 VS |
8115 | static inline void update_blocked_load_status(struct rq *rq, bool has_blocked) {} |
8116 | #endif | |
8117 | ||
bef69dd8 VG |
8118 | static bool __update_blocked_others(struct rq *rq, bool *done) |
8119 | { | |
8120 | const struct sched_class *curr_class; | |
8121 | u64 now = rq_clock_pelt(rq); | |
b4eccf5f | 8122 | unsigned long thermal_pressure; |
bef69dd8 VG |
8123 | bool decayed; |
8124 | ||
8125 | /* | |
8126 | * update_load_avg() can call cpufreq_update_util(). Make sure that RT, | |
8127 | * DL and IRQ signals have been updated before updating CFS. | |
8128 | */ | |
8129 | curr_class = rq->curr->sched_class; | |
8130 | ||
b4eccf5f TG |
8131 | thermal_pressure = arch_scale_thermal_pressure(cpu_of(rq)); |
8132 | ||
bef69dd8 VG |
8133 | decayed = update_rt_rq_load_avg(now, rq, curr_class == &rt_sched_class) | |
8134 | update_dl_rq_load_avg(now, rq, curr_class == &dl_sched_class) | | |
05289b90 | 8135 | update_thermal_load_avg(rq_clock_thermal(rq), rq, thermal_pressure) | |
bef69dd8 VG |
8136 | update_irq_load_avg(rq, 0); |
8137 | ||
8138 | if (others_have_blocked(rq)) | |
8139 | *done = false; | |
8140 | ||
8141 | return decayed; | |
8142 | } | |
8143 | ||
1936c53c VG |
8144 | #ifdef CONFIG_FAIR_GROUP_SCHED |
8145 | ||
bef69dd8 | 8146 | static bool __update_blocked_fair(struct rq *rq, bool *done) |
9e3081ca | 8147 | { |
039ae8bc | 8148 | struct cfs_rq *cfs_rq, *pos; |
bef69dd8 VG |
8149 | bool decayed = false; |
8150 | int cpu = cpu_of(rq); | |
b90f7c9d | 8151 | |
9763b67f PZ |
8152 | /* |
8153 | * Iterates the task_group tree in a bottom up fashion, see | |
8154 | * list_add_leaf_cfs_rq() for details. | |
8155 | */ | |
039ae8bc | 8156 | for_each_leaf_cfs_rq_safe(rq, cfs_rq, pos) { |
bc427898 VG |
8157 | struct sched_entity *se; |
8158 | ||
bef69dd8 | 8159 | if (update_cfs_rq_load_avg(cfs_rq_clock_pelt(cfs_rq), cfs_rq)) { |
fe749158 | 8160 | update_tg_load_avg(cfs_rq); |
4e516076 | 8161 | |
bef69dd8 VG |
8162 | if (cfs_rq == &rq->cfs) |
8163 | decayed = true; | |
8164 | } | |
8165 | ||
bc427898 VG |
8166 | /* Propagate pending load changes to the parent, if any: */ |
8167 | se = cfs_rq->tg->se[cpu]; | |
8168 | if (se && !skip_blocked_update(se)) | |
02da26ad | 8169 | update_load_avg(cfs_rq_of(se), se, UPDATE_TG); |
a9e7f654 | 8170 | |
039ae8bc VG |
8171 | /* |
8172 | * There can be a lot of idle CPU cgroups. Don't let fully | |
8173 | * decayed cfs_rqs linger on the list. | |
8174 | */ | |
8175 | if (cfs_rq_is_decayed(cfs_rq)) | |
8176 | list_del_leaf_cfs_rq(cfs_rq); | |
8177 | ||
1936c53c VG |
8178 | /* Don't need periodic decay once load/util_avg are null */ |
8179 | if (cfs_rq_has_blocked(cfs_rq)) | |
bef69dd8 | 8180 | *done = false; |
9d89c257 | 8181 | } |
12b04875 | 8182 | |
bef69dd8 | 8183 | return decayed; |
9e3081ca PZ |
8184 | } |
8185 | ||
9763b67f | 8186 | /* |
68520796 | 8187 | * Compute the hierarchical load factor for cfs_rq and all its ascendants. |
9763b67f PZ |
8188 | * This needs to be done in a top-down fashion because the load of a child |
8189 | * group is a fraction of its parents load. | |
8190 | */ | |
68520796 | 8191 | static void update_cfs_rq_h_load(struct cfs_rq *cfs_rq) |
9763b67f | 8192 | { |
68520796 VD |
8193 | struct rq *rq = rq_of(cfs_rq); |
8194 | struct sched_entity *se = cfs_rq->tg->se[cpu_of(rq)]; | |
a35b6466 | 8195 | unsigned long now = jiffies; |
68520796 | 8196 | unsigned long load; |
a35b6466 | 8197 | |
68520796 | 8198 | if (cfs_rq->last_h_load_update == now) |
a35b6466 PZ |
8199 | return; |
8200 | ||
0e9f0245 | 8201 | WRITE_ONCE(cfs_rq->h_load_next, NULL); |
68520796 VD |
8202 | for_each_sched_entity(se) { |
8203 | cfs_rq = cfs_rq_of(se); | |
0e9f0245 | 8204 | WRITE_ONCE(cfs_rq->h_load_next, se); |
68520796 VD |
8205 | if (cfs_rq->last_h_load_update == now) |
8206 | break; | |
8207 | } | |
a35b6466 | 8208 | |
68520796 | 8209 | if (!se) { |
7ea241af | 8210 | cfs_rq->h_load = cfs_rq_load_avg(cfs_rq); |
68520796 VD |
8211 | cfs_rq->last_h_load_update = now; |
8212 | } | |
8213 | ||
0e9f0245 | 8214 | while ((se = READ_ONCE(cfs_rq->h_load_next)) != NULL) { |
68520796 | 8215 | load = cfs_rq->h_load; |
7ea241af YD |
8216 | load = div64_ul(load * se->avg.load_avg, |
8217 | cfs_rq_load_avg(cfs_rq) + 1); | |
68520796 VD |
8218 | cfs_rq = group_cfs_rq(se); |
8219 | cfs_rq->h_load = load; | |
8220 | cfs_rq->last_h_load_update = now; | |
8221 | } | |
9763b67f PZ |
8222 | } |
8223 | ||
367456c7 | 8224 | static unsigned long task_h_load(struct task_struct *p) |
230059de | 8225 | { |
367456c7 | 8226 | struct cfs_rq *cfs_rq = task_cfs_rq(p); |
230059de | 8227 | |
68520796 | 8228 | update_cfs_rq_h_load(cfs_rq); |
9d89c257 | 8229 | return div64_ul(p->se.avg.load_avg * cfs_rq->h_load, |
7ea241af | 8230 | cfs_rq_load_avg(cfs_rq) + 1); |
230059de PZ |
8231 | } |
8232 | #else | |
bef69dd8 | 8233 | static bool __update_blocked_fair(struct rq *rq, bool *done) |
9e3081ca | 8234 | { |
6c1d47c0 | 8235 | struct cfs_rq *cfs_rq = &rq->cfs; |
bef69dd8 | 8236 | bool decayed; |
b90f7c9d | 8237 | |
bef69dd8 VG |
8238 | decayed = update_cfs_rq_load_avg(cfs_rq_clock_pelt(cfs_rq), cfs_rq); |
8239 | if (cfs_rq_has_blocked(cfs_rq)) | |
8240 | *done = false; | |
b90f7c9d | 8241 | |
bef69dd8 | 8242 | return decayed; |
9e3081ca PZ |
8243 | } |
8244 | ||
367456c7 | 8245 | static unsigned long task_h_load(struct task_struct *p) |
1e3c88bd | 8246 | { |
9d89c257 | 8247 | return p->se.avg.load_avg; |
1e3c88bd | 8248 | } |
230059de | 8249 | #endif |
1e3c88bd | 8250 | |
bef69dd8 VG |
8251 | static void update_blocked_averages(int cpu) |
8252 | { | |
8253 | bool decayed = false, done = true; | |
8254 | struct rq *rq = cpu_rq(cpu); | |
8255 | struct rq_flags rf; | |
8256 | ||
8257 | rq_lock_irqsave(rq, &rf); | |
39b6a429 | 8258 | update_blocked_load_tick(rq); |
bef69dd8 VG |
8259 | update_rq_clock(rq); |
8260 | ||
8261 | decayed |= __update_blocked_others(rq, &done); | |
8262 | decayed |= __update_blocked_fair(rq, &done); | |
8263 | ||
8264 | update_blocked_load_status(rq, !done); | |
8265 | if (decayed) | |
8266 | cpufreq_update_util(rq, 0); | |
8267 | rq_unlock_irqrestore(rq, &rf); | |
8268 | } | |
8269 | ||
1e3c88bd | 8270 | /********** Helpers for find_busiest_group ************************/ |
caeb178c | 8271 | |
1e3c88bd PZ |
8272 | /* |
8273 | * sg_lb_stats - stats of a sched_group required for load_balancing | |
8274 | */ | |
8275 | struct sg_lb_stats { | |
8276 | unsigned long avg_load; /*Avg load across the CPUs of the group */ | |
8277 | unsigned long group_load; /* Total load over the CPUs of the group */ | |
63b2ca30 | 8278 | unsigned long group_capacity; |
070f5e86 VG |
8279 | unsigned long group_util; /* Total utilization over the CPUs of the group */ |
8280 | unsigned long group_runnable; /* Total runnable time over the CPUs of the group */ | |
5e23e474 | 8281 | unsigned int sum_nr_running; /* Nr of tasks running in the group */ |
a3498347 | 8282 | unsigned int sum_h_nr_running; /* Nr of CFS tasks running in the group */ |
147c5fc2 PZ |
8283 | unsigned int idle_cpus; |
8284 | unsigned int group_weight; | |
caeb178c | 8285 | enum group_type group_type; |
490ba971 | 8286 | unsigned int group_asym_packing; /* Tasks should be moved to preferred CPU */ |
3b1baa64 | 8287 | unsigned long group_misfit_task_load; /* A CPU has a task too big for its capacity */ |
0ec8aa00 PZ |
8288 | #ifdef CONFIG_NUMA_BALANCING |
8289 | unsigned int nr_numa_running; | |
8290 | unsigned int nr_preferred_running; | |
8291 | #endif | |
1e3c88bd PZ |
8292 | }; |
8293 | ||
56cf515b JK |
8294 | /* |
8295 | * sd_lb_stats - Structure to store the statistics of a sched_domain | |
8296 | * during load balancing. | |
8297 | */ | |
8298 | struct sd_lb_stats { | |
8299 | struct sched_group *busiest; /* Busiest group in this sd */ | |
8300 | struct sched_group *local; /* Local group in this sd */ | |
8301 | unsigned long total_load; /* Total load of all groups in sd */ | |
63b2ca30 | 8302 | unsigned long total_capacity; /* Total capacity of all groups in sd */ |
56cf515b | 8303 | unsigned long avg_load; /* Average load across all groups in sd */ |
0b0695f2 | 8304 | unsigned int prefer_sibling; /* tasks should go to sibling first */ |
56cf515b | 8305 | |
56cf515b | 8306 | struct sg_lb_stats busiest_stat;/* Statistics of the busiest group */ |
147c5fc2 | 8307 | struct sg_lb_stats local_stat; /* Statistics of the local group */ |
56cf515b JK |
8308 | }; |
8309 | ||
147c5fc2 PZ |
8310 | static inline void init_sd_lb_stats(struct sd_lb_stats *sds) |
8311 | { | |
8312 | /* | |
8313 | * Skimp on the clearing to avoid duplicate work. We can avoid clearing | |
8314 | * local_stat because update_sg_lb_stats() does a full clear/assignment. | |
0b0695f2 VG |
8315 | * We must however set busiest_stat::group_type and |
8316 | * busiest_stat::idle_cpus to the worst busiest group because | |
8317 | * update_sd_pick_busiest() reads these before assignment. | |
147c5fc2 PZ |
8318 | */ |
8319 | *sds = (struct sd_lb_stats){ | |
8320 | .busiest = NULL, | |
8321 | .local = NULL, | |
8322 | .total_load = 0UL, | |
63b2ca30 | 8323 | .total_capacity = 0UL, |
147c5fc2 | 8324 | .busiest_stat = { |
0b0695f2 VG |
8325 | .idle_cpus = UINT_MAX, |
8326 | .group_type = group_has_spare, | |
147c5fc2 PZ |
8327 | }, |
8328 | }; | |
8329 | } | |
8330 | ||
1ca2034e | 8331 | static unsigned long scale_rt_capacity(int cpu) |
1e3c88bd PZ |
8332 | { |
8333 | struct rq *rq = cpu_rq(cpu); | |
8ec59c0f | 8334 | unsigned long max = arch_scale_cpu_capacity(cpu); |
523e979d | 8335 | unsigned long used, free; |
523e979d | 8336 | unsigned long irq; |
b654f7de | 8337 | |
2e62c474 | 8338 | irq = cpu_util_irq(rq); |
cadefd3d | 8339 | |
523e979d VG |
8340 | if (unlikely(irq >= max)) |
8341 | return 1; | |
aa483808 | 8342 | |
467b7d01 TG |
8343 | /* |
8344 | * avg_rt.util_avg and avg_dl.util_avg track binary signals | |
8345 | * (running and not running) with weights 0 and 1024 respectively. | |
8346 | * avg_thermal.load_avg tracks thermal pressure and the weighted | |
8347 | * average uses the actual delta max capacity(load). | |
8348 | */ | |
523e979d VG |
8349 | used = READ_ONCE(rq->avg_rt.util_avg); |
8350 | used += READ_ONCE(rq->avg_dl.util_avg); | |
467b7d01 | 8351 | used += thermal_load_avg(rq); |
1e3c88bd | 8352 | |
523e979d VG |
8353 | if (unlikely(used >= max)) |
8354 | return 1; | |
1e3c88bd | 8355 | |
523e979d | 8356 | free = max - used; |
2e62c474 VG |
8357 | |
8358 | return scale_irq_capacity(free, irq, max); | |
1e3c88bd PZ |
8359 | } |
8360 | ||
ced549fa | 8361 | static void update_cpu_capacity(struct sched_domain *sd, int cpu) |
1e3c88bd | 8362 | { |
1ca2034e | 8363 | unsigned long capacity = scale_rt_capacity(cpu); |
1e3c88bd PZ |
8364 | struct sched_group *sdg = sd->groups; |
8365 | ||
8ec59c0f | 8366 | cpu_rq(cpu)->cpu_capacity_orig = arch_scale_cpu_capacity(cpu); |
1e3c88bd | 8367 | |
ced549fa NP |
8368 | if (!capacity) |
8369 | capacity = 1; | |
1e3c88bd | 8370 | |
ced549fa | 8371 | cpu_rq(cpu)->cpu_capacity = capacity; |
51cf18c9 VD |
8372 | trace_sched_cpu_capacity_tp(cpu_rq(cpu)); |
8373 | ||
ced549fa | 8374 | sdg->sgc->capacity = capacity; |
bf475ce0 | 8375 | sdg->sgc->min_capacity = capacity; |
e3d6d0cb | 8376 | sdg->sgc->max_capacity = capacity; |
1e3c88bd PZ |
8377 | } |
8378 | ||
63b2ca30 | 8379 | void update_group_capacity(struct sched_domain *sd, int cpu) |
1e3c88bd PZ |
8380 | { |
8381 | struct sched_domain *child = sd->child; | |
8382 | struct sched_group *group, *sdg = sd->groups; | |
e3d6d0cb | 8383 | unsigned long capacity, min_capacity, max_capacity; |
4ec4412e VG |
8384 | unsigned long interval; |
8385 | ||
8386 | interval = msecs_to_jiffies(sd->balance_interval); | |
8387 | interval = clamp(interval, 1UL, max_load_balance_interval); | |
63b2ca30 | 8388 | sdg->sgc->next_update = jiffies + interval; |
1e3c88bd PZ |
8389 | |
8390 | if (!child) { | |
ced549fa | 8391 | update_cpu_capacity(sd, cpu); |
1e3c88bd PZ |
8392 | return; |
8393 | } | |
8394 | ||
dc7ff76e | 8395 | capacity = 0; |
bf475ce0 | 8396 | min_capacity = ULONG_MAX; |
e3d6d0cb | 8397 | max_capacity = 0; |
1e3c88bd | 8398 | |
74a5ce20 PZ |
8399 | if (child->flags & SD_OVERLAP) { |
8400 | /* | |
8401 | * SD_OVERLAP domains cannot assume that child groups | |
8402 | * span the current group. | |
8403 | */ | |
8404 | ||
ae4df9d6 | 8405 | for_each_cpu(cpu, sched_group_span(sdg)) { |
4c58f57f | 8406 | unsigned long cpu_cap = capacity_of(cpu); |
863bffc8 | 8407 | |
4c58f57f PL |
8408 | capacity += cpu_cap; |
8409 | min_capacity = min(cpu_cap, min_capacity); | |
8410 | max_capacity = max(cpu_cap, max_capacity); | |
863bffc8 | 8411 | } |
74a5ce20 PZ |
8412 | } else { |
8413 | /* | |
8414 | * !SD_OVERLAP domains can assume that child groups | |
8415 | * span the current group. | |
97a7142f | 8416 | */ |
74a5ce20 PZ |
8417 | |
8418 | group = child->groups; | |
8419 | do { | |
bf475ce0 MR |
8420 | struct sched_group_capacity *sgc = group->sgc; |
8421 | ||
8422 | capacity += sgc->capacity; | |
8423 | min_capacity = min(sgc->min_capacity, min_capacity); | |
e3d6d0cb | 8424 | max_capacity = max(sgc->max_capacity, max_capacity); |
74a5ce20 PZ |
8425 | group = group->next; |
8426 | } while (group != child->groups); | |
8427 | } | |
1e3c88bd | 8428 | |
63b2ca30 | 8429 | sdg->sgc->capacity = capacity; |
bf475ce0 | 8430 | sdg->sgc->min_capacity = min_capacity; |
e3d6d0cb | 8431 | sdg->sgc->max_capacity = max_capacity; |
1e3c88bd PZ |
8432 | } |
8433 | ||
9d5efe05 | 8434 | /* |
ea67821b VG |
8435 | * Check whether the capacity of the rq has been noticeably reduced by side |
8436 | * activity. The imbalance_pct is used for the threshold. | |
8437 | * Return true is the capacity is reduced | |
9d5efe05 SV |
8438 | */ |
8439 | static inline int | |
ea67821b | 8440 | check_cpu_capacity(struct rq *rq, struct sched_domain *sd) |
9d5efe05 | 8441 | { |
ea67821b VG |
8442 | return ((rq->cpu_capacity * sd->imbalance_pct) < |
8443 | (rq->cpu_capacity_orig * 100)); | |
9d5efe05 SV |
8444 | } |
8445 | ||
a0fe2cf0 VS |
8446 | /* |
8447 | * Check whether a rq has a misfit task and if it looks like we can actually | |
8448 | * help that task: we can migrate the task to a CPU of higher capacity, or | |
8449 | * the task's current CPU is heavily pressured. | |
8450 | */ | |
8451 | static inline int check_misfit_status(struct rq *rq, struct sched_domain *sd) | |
8452 | { | |
8453 | return rq->misfit_task_load && | |
8454 | (rq->cpu_capacity_orig < rq->rd->max_cpu_capacity || | |
8455 | check_cpu_capacity(rq, sd)); | |
8456 | } | |
8457 | ||
30ce5dab PZ |
8458 | /* |
8459 | * Group imbalance indicates (and tries to solve) the problem where balancing | |
3bd37062 | 8460 | * groups is inadequate due to ->cpus_ptr constraints. |
30ce5dab | 8461 | * |
97fb7a0a IM |
8462 | * Imagine a situation of two groups of 4 CPUs each and 4 tasks each with a |
8463 | * cpumask covering 1 CPU of the first group and 3 CPUs of the second group. | |
30ce5dab PZ |
8464 | * Something like: |
8465 | * | |
2b4d5b25 IM |
8466 | * { 0 1 2 3 } { 4 5 6 7 } |
8467 | * * * * * | |
30ce5dab PZ |
8468 | * |
8469 | * If we were to balance group-wise we'd place two tasks in the first group and | |
8470 | * two tasks in the second group. Clearly this is undesired as it will overload | |
97fb7a0a | 8471 | * cpu 3 and leave one of the CPUs in the second group unused. |
30ce5dab PZ |
8472 | * |
8473 | * The current solution to this issue is detecting the skew in the first group | |
6263322c PZ |
8474 | * by noticing the lower domain failed to reach balance and had difficulty |
8475 | * moving tasks due to affinity constraints. | |
30ce5dab PZ |
8476 | * |
8477 | * When this is so detected; this group becomes a candidate for busiest; see | |
ed1b7732 | 8478 | * update_sd_pick_busiest(). And calculate_imbalance() and |
6263322c | 8479 | * find_busiest_group() avoid some of the usual balance conditions to allow it |
30ce5dab PZ |
8480 | * to create an effective group imbalance. |
8481 | * | |
8482 | * This is a somewhat tricky proposition since the next run might not find the | |
8483 | * group imbalance and decide the groups need to be balanced again. A most | |
8484 | * subtle and fragile situation. | |
8485 | */ | |
8486 | ||
6263322c | 8487 | static inline int sg_imbalanced(struct sched_group *group) |
30ce5dab | 8488 | { |
63b2ca30 | 8489 | return group->sgc->imbalance; |
30ce5dab PZ |
8490 | } |
8491 | ||
b37d9316 | 8492 | /* |
ea67821b VG |
8493 | * group_has_capacity returns true if the group has spare capacity that could |
8494 | * be used by some tasks. | |
8495 | * We consider that a group has spare capacity if the * number of task is | |
9e91d61d DE |
8496 | * smaller than the number of CPUs or if the utilization is lower than the |
8497 | * available capacity for CFS tasks. | |
ea67821b VG |
8498 | * For the latter, we use a threshold to stabilize the state, to take into |
8499 | * account the variance of the tasks' load and to return true if the available | |
8500 | * capacity in meaningful for the load balancer. | |
8501 | * As an example, an available capacity of 1% can appear but it doesn't make | |
8502 | * any benefit for the load balance. | |
b37d9316 | 8503 | */ |
ea67821b | 8504 | static inline bool |
57abff06 | 8505 | group_has_capacity(unsigned int imbalance_pct, struct sg_lb_stats *sgs) |
b37d9316 | 8506 | { |
5e23e474 | 8507 | if (sgs->sum_nr_running < sgs->group_weight) |
ea67821b | 8508 | return true; |
c61037e9 | 8509 | |
070f5e86 VG |
8510 | if ((sgs->group_capacity * imbalance_pct) < |
8511 | (sgs->group_runnable * 100)) | |
8512 | return false; | |
8513 | ||
ea67821b | 8514 | if ((sgs->group_capacity * 100) > |
57abff06 | 8515 | (sgs->group_util * imbalance_pct)) |
ea67821b | 8516 | return true; |
b37d9316 | 8517 | |
ea67821b VG |
8518 | return false; |
8519 | } | |
8520 | ||
8521 | /* | |
8522 | * group_is_overloaded returns true if the group has more tasks than it can | |
8523 | * handle. | |
8524 | * group_is_overloaded is not equals to !group_has_capacity because a group | |
8525 | * with the exact right number of tasks, has no more spare capacity but is not | |
8526 | * overloaded so both group_has_capacity and group_is_overloaded return | |
8527 | * false. | |
8528 | */ | |
8529 | static inline bool | |
57abff06 | 8530 | group_is_overloaded(unsigned int imbalance_pct, struct sg_lb_stats *sgs) |
ea67821b | 8531 | { |
5e23e474 | 8532 | if (sgs->sum_nr_running <= sgs->group_weight) |
ea67821b | 8533 | return false; |
b37d9316 | 8534 | |
ea67821b | 8535 | if ((sgs->group_capacity * 100) < |
57abff06 | 8536 | (sgs->group_util * imbalance_pct)) |
ea67821b | 8537 | return true; |
b37d9316 | 8538 | |
070f5e86 VG |
8539 | if ((sgs->group_capacity * imbalance_pct) < |
8540 | (sgs->group_runnable * 100)) | |
8541 | return true; | |
8542 | ||
ea67821b | 8543 | return false; |
b37d9316 PZ |
8544 | } |
8545 | ||
79a89f92 | 8546 | static inline enum |
57abff06 | 8547 | group_type group_classify(unsigned int imbalance_pct, |
0b0695f2 | 8548 | struct sched_group *group, |
79a89f92 | 8549 | struct sg_lb_stats *sgs) |
caeb178c | 8550 | { |
57abff06 | 8551 | if (group_is_overloaded(imbalance_pct, sgs)) |
caeb178c RR |
8552 | return group_overloaded; |
8553 | ||
8554 | if (sg_imbalanced(group)) | |
8555 | return group_imbalanced; | |
8556 | ||
0b0695f2 VG |
8557 | if (sgs->group_asym_packing) |
8558 | return group_asym_packing; | |
8559 | ||
3b1baa64 MR |
8560 | if (sgs->group_misfit_task_load) |
8561 | return group_misfit_task; | |
8562 | ||
57abff06 | 8563 | if (!group_has_capacity(imbalance_pct, sgs)) |
0b0695f2 VG |
8564 | return group_fully_busy; |
8565 | ||
8566 | return group_has_spare; | |
caeb178c RR |
8567 | } |
8568 | ||
4006a72b RN |
8569 | /** |
8570 | * asym_smt_can_pull_tasks - Check whether the load balancing CPU can pull tasks | |
8571 | * @dst_cpu: Destination CPU of the load balancing | |
8572 | * @sds: Load-balancing data with statistics of the local group | |
8573 | * @sgs: Load-balancing statistics of the candidate busiest group | |
8574 | * @sg: The candidate busiest group | |
8575 | * | |
8576 | * Check the state of the SMT siblings of both @sds::local and @sg and decide | |
8577 | * if @dst_cpu can pull tasks. | |
8578 | * | |
8579 | * If @dst_cpu does not have SMT siblings, it can pull tasks if two or more of | |
8580 | * the SMT siblings of @sg are busy. If only one CPU in @sg is busy, pull tasks | |
8581 | * only if @dst_cpu has higher priority. | |
8582 | * | |
8583 | * If both @dst_cpu and @sg have SMT siblings, and @sg has exactly one more | |
8584 | * busy CPU than @sds::local, let @dst_cpu pull tasks if it has higher priority. | |
8585 | * Bigger imbalances in the number of busy CPUs will be dealt with in | |
8586 | * update_sd_pick_busiest(). | |
8587 | * | |
8588 | * If @sg does not have SMT siblings, only pull tasks if all of the SMT siblings | |
8589 | * of @dst_cpu are idle and @sg has lower priority. | |
8590 | */ | |
8591 | static bool asym_smt_can_pull_tasks(int dst_cpu, struct sd_lb_stats *sds, | |
8592 | struct sg_lb_stats *sgs, | |
8593 | struct sched_group *sg) | |
8594 | { | |
8595 | #ifdef CONFIG_SCHED_SMT | |
8596 | bool local_is_smt, sg_is_smt; | |
8597 | int sg_busy_cpus; | |
8598 | ||
8599 | local_is_smt = sds->local->flags & SD_SHARE_CPUCAPACITY; | |
8600 | sg_is_smt = sg->flags & SD_SHARE_CPUCAPACITY; | |
8601 | ||
8602 | sg_busy_cpus = sgs->group_weight - sgs->idle_cpus; | |
8603 | ||
8604 | if (!local_is_smt) { | |
8605 | /* | |
8606 | * If we are here, @dst_cpu is idle and does not have SMT | |
8607 | * siblings. Pull tasks if candidate group has two or more | |
8608 | * busy CPUs. | |
8609 | */ | |
8610 | if (sg_busy_cpus >= 2) /* implies sg_is_smt */ | |
8611 | return true; | |
8612 | ||
8613 | /* | |
8614 | * @dst_cpu does not have SMT siblings. @sg may have SMT | |
8615 | * siblings and only one is busy. In such case, @dst_cpu | |
8616 | * can help if it has higher priority and is idle (i.e., | |
8617 | * it has no running tasks). | |
8618 | */ | |
8619 | return sched_asym_prefer(dst_cpu, sg->asym_prefer_cpu); | |
8620 | } | |
8621 | ||
8622 | /* @dst_cpu has SMT siblings. */ | |
8623 | ||
8624 | if (sg_is_smt) { | |
8625 | int local_busy_cpus = sds->local->group_weight - | |
8626 | sds->local_stat.idle_cpus; | |
8627 | int busy_cpus_delta = sg_busy_cpus - local_busy_cpus; | |
8628 | ||
8629 | if (busy_cpus_delta == 1) | |
8630 | return sched_asym_prefer(dst_cpu, sg->asym_prefer_cpu); | |
8631 | ||
8632 | return false; | |
8633 | } | |
8634 | ||
8635 | /* | |
8636 | * @sg does not have SMT siblings. Ensure that @sds::local does not end | |
8637 | * up with more than one busy SMT sibling and only pull tasks if there | |
8638 | * are not busy CPUs (i.e., no CPU has running tasks). | |
8639 | */ | |
8640 | if (!sds->local_stat.sum_nr_running) | |
8641 | return sched_asym_prefer(dst_cpu, sg->asym_prefer_cpu); | |
8642 | ||
8643 | return false; | |
8644 | #else | |
8645 | /* Always return false so that callers deal with non-SMT cases. */ | |
8646 | return false; | |
8647 | #endif | |
8648 | } | |
8649 | ||
aafc917a RN |
8650 | static inline bool |
8651 | sched_asym(struct lb_env *env, struct sd_lb_stats *sds, struct sg_lb_stats *sgs, | |
8652 | struct sched_group *group) | |
8653 | { | |
4006a72b RN |
8654 | /* Only do SMT checks if either local or candidate have SMT siblings */ |
8655 | if ((sds->local->flags & SD_SHARE_CPUCAPACITY) || | |
8656 | (group->flags & SD_SHARE_CPUCAPACITY)) | |
8657 | return asym_smt_can_pull_tasks(env->dst_cpu, sds, sgs, group); | |
8658 | ||
aafc917a RN |
8659 | return sched_asym_prefer(env->dst_cpu, group->asym_prefer_cpu); |
8660 | } | |
8661 | ||
1e3c88bd PZ |
8662 | /** |
8663 | * update_sg_lb_stats - Update sched_group's statistics for load balancing. | |
cd96891d | 8664 | * @env: The load balancing environment. |
1e3c88bd | 8665 | * @group: sched_group whose statistics are to be updated. |
1e3c88bd | 8666 | * @sgs: variable to hold the statistics for this group. |
630246a0 | 8667 | * @sg_status: Holds flag indicating the status of the sched_group |
1e3c88bd | 8668 | */ |
bd939f45 | 8669 | static inline void update_sg_lb_stats(struct lb_env *env, |
c0d14b57 | 8670 | struct sd_lb_stats *sds, |
630246a0 QP |
8671 | struct sched_group *group, |
8672 | struct sg_lb_stats *sgs, | |
8673 | int *sg_status) | |
1e3c88bd | 8674 | { |
0b0695f2 | 8675 | int i, nr_running, local_group; |
1e3c88bd | 8676 | |
b72ff13c PZ |
8677 | memset(sgs, 0, sizeof(*sgs)); |
8678 | ||
c0d14b57 | 8679 | local_group = group == sds->local; |
0b0695f2 | 8680 | |
ae4df9d6 | 8681 | for_each_cpu_and(i, sched_group_span(group), env->cpus) { |
1e3c88bd PZ |
8682 | struct rq *rq = cpu_rq(i); |
8683 | ||
b0fb1eb4 | 8684 | sgs->group_load += cpu_load(rq); |
9e91d61d | 8685 | sgs->group_util += cpu_util(i); |
070f5e86 | 8686 | sgs->group_runnable += cpu_runnable(rq); |
a3498347 | 8687 | sgs->sum_h_nr_running += rq->cfs.h_nr_running; |
4486edd1 | 8688 | |
a426f99c | 8689 | nr_running = rq->nr_running; |
5e23e474 VG |
8690 | sgs->sum_nr_running += nr_running; |
8691 | ||
a426f99c | 8692 | if (nr_running > 1) |
630246a0 | 8693 | *sg_status |= SG_OVERLOAD; |
4486edd1 | 8694 | |
2802bf3c MR |
8695 | if (cpu_overutilized(i)) |
8696 | *sg_status |= SG_OVERUTILIZED; | |
4486edd1 | 8697 | |
0ec8aa00 PZ |
8698 | #ifdef CONFIG_NUMA_BALANCING |
8699 | sgs->nr_numa_running += rq->nr_numa_running; | |
8700 | sgs->nr_preferred_running += rq->nr_preferred_running; | |
8701 | #endif | |
a426f99c WL |
8702 | /* |
8703 | * No need to call idle_cpu() if nr_running is not 0 | |
8704 | */ | |
0b0695f2 | 8705 | if (!nr_running && idle_cpu(i)) { |
aae6d3dd | 8706 | sgs->idle_cpus++; |
0b0695f2 VG |
8707 | /* Idle cpu can't have misfit task */ |
8708 | continue; | |
8709 | } | |
8710 | ||
8711 | if (local_group) | |
8712 | continue; | |
3b1baa64 | 8713 | |
0b0695f2 | 8714 | /* Check for a misfit task on the cpu */ |
3b1baa64 | 8715 | if (env->sd->flags & SD_ASYM_CPUCAPACITY && |
757ffdd7 | 8716 | sgs->group_misfit_task_load < rq->misfit_task_load) { |
3b1baa64 | 8717 | sgs->group_misfit_task_load = rq->misfit_task_load; |
630246a0 | 8718 | *sg_status |= SG_OVERLOAD; |
757ffdd7 | 8719 | } |
1e3c88bd PZ |
8720 | } |
8721 | ||
aafc917a RN |
8722 | sgs->group_capacity = group->sgc->capacity; |
8723 | ||
8724 | sgs->group_weight = group->group_weight; | |
8725 | ||
0b0695f2 | 8726 | /* Check if dst CPU is idle and preferred to this group */ |
60256435 | 8727 | if (!local_group && env->sd->flags & SD_ASYM_PACKING && |
aafc917a RN |
8728 | env->idle != CPU_NOT_IDLE && sgs->sum_h_nr_running && |
8729 | sched_asym(env, sds, sgs, group)) { | |
0b0695f2 VG |
8730 | sgs->group_asym_packing = 1; |
8731 | } | |
8732 | ||
57abff06 | 8733 | sgs->group_type = group_classify(env->sd->imbalance_pct, group, sgs); |
0b0695f2 VG |
8734 | |
8735 | /* Computing avg_load makes sense only when group is overloaded */ | |
8736 | if (sgs->group_type == group_overloaded) | |
8737 | sgs->avg_load = (sgs->group_load * SCHED_CAPACITY_SCALE) / | |
8738 | sgs->group_capacity; | |
1e3c88bd PZ |
8739 | } |
8740 | ||
532cb4c4 MN |
8741 | /** |
8742 | * update_sd_pick_busiest - return 1 on busiest group | |
cd96891d | 8743 | * @env: The load balancing environment. |
532cb4c4 MN |
8744 | * @sds: sched_domain statistics |
8745 | * @sg: sched_group candidate to be checked for being the busiest | |
b6b12294 | 8746 | * @sgs: sched_group statistics |
532cb4c4 MN |
8747 | * |
8748 | * Determine if @sg is a busier group than the previously selected | |
8749 | * busiest group. | |
e69f6186 YB |
8750 | * |
8751 | * Return: %true if @sg is a busier group than the previously selected | |
8752 | * busiest group. %false otherwise. | |
532cb4c4 | 8753 | */ |
bd939f45 | 8754 | static bool update_sd_pick_busiest(struct lb_env *env, |
532cb4c4 MN |
8755 | struct sd_lb_stats *sds, |
8756 | struct sched_group *sg, | |
bd939f45 | 8757 | struct sg_lb_stats *sgs) |
532cb4c4 | 8758 | { |
caeb178c | 8759 | struct sg_lb_stats *busiest = &sds->busiest_stat; |
532cb4c4 | 8760 | |
0b0695f2 VG |
8761 | /* Make sure that there is at least one task to pull */ |
8762 | if (!sgs->sum_h_nr_running) | |
8763 | return false; | |
8764 | ||
cad68e55 MR |
8765 | /* |
8766 | * Don't try to pull misfit tasks we can't help. | |
8767 | * We can use max_capacity here as reduction in capacity on some | |
8768 | * CPUs in the group should either be possible to resolve | |
8769 | * internally or be covered by avg_load imbalance (eventually). | |
8770 | */ | |
8771 | if (sgs->group_type == group_misfit_task && | |
4aed8aa4 | 8772 | (!capacity_greater(capacity_of(env->dst_cpu), sg->sgc->max_capacity) || |
0b0695f2 | 8773 | sds->local_stat.group_type != group_has_spare)) |
cad68e55 MR |
8774 | return false; |
8775 | ||
caeb178c | 8776 | if (sgs->group_type > busiest->group_type) |
532cb4c4 MN |
8777 | return true; |
8778 | ||
caeb178c RR |
8779 | if (sgs->group_type < busiest->group_type) |
8780 | return false; | |
8781 | ||
9e0994c0 | 8782 | /* |
0b0695f2 VG |
8783 | * The candidate and the current busiest group are the same type of |
8784 | * group. Let check which one is the busiest according to the type. | |
9e0994c0 | 8785 | */ |
9e0994c0 | 8786 | |
0b0695f2 VG |
8787 | switch (sgs->group_type) { |
8788 | case group_overloaded: | |
8789 | /* Select the overloaded group with highest avg_load. */ | |
8790 | if (sgs->avg_load <= busiest->avg_load) | |
8791 | return false; | |
8792 | break; | |
8793 | ||
8794 | case group_imbalanced: | |
8795 | /* | |
8796 | * Select the 1st imbalanced group as we don't have any way to | |
8797 | * choose one more than another. | |
8798 | */ | |
9e0994c0 MR |
8799 | return false; |
8800 | ||
0b0695f2 VG |
8801 | case group_asym_packing: |
8802 | /* Prefer to move from lowest priority CPU's work */ | |
8803 | if (sched_asym_prefer(sg->asym_prefer_cpu, sds->busiest->asym_prefer_cpu)) | |
8804 | return false; | |
8805 | break; | |
532cb4c4 | 8806 | |
0b0695f2 VG |
8807 | case group_misfit_task: |
8808 | /* | |
8809 | * If we have more than one misfit sg go with the biggest | |
8810 | * misfit. | |
8811 | */ | |
8812 | if (sgs->group_misfit_task_load < busiest->group_misfit_task_load) | |
8813 | return false; | |
8814 | break; | |
532cb4c4 | 8815 | |
0b0695f2 VG |
8816 | case group_fully_busy: |
8817 | /* | |
8818 | * Select the fully busy group with highest avg_load. In | |
8819 | * theory, there is no need to pull task from such kind of | |
8820 | * group because tasks have all compute capacity that they need | |
8821 | * but we can still improve the overall throughput by reducing | |
8822 | * contention when accessing shared HW resources. | |
8823 | * | |
8824 | * XXX for now avg_load is not computed and always 0 so we | |
8825 | * select the 1st one. | |
8826 | */ | |
8827 | if (sgs->avg_load <= busiest->avg_load) | |
8828 | return false; | |
8829 | break; | |
8830 | ||
8831 | case group_has_spare: | |
8832 | /* | |
5f68eb19 VG |
8833 | * Select not overloaded group with lowest number of idle cpus |
8834 | * and highest number of running tasks. We could also compare | |
8835 | * the spare capacity which is more stable but it can end up | |
8836 | * that the group has less spare capacity but finally more idle | |
0b0695f2 VG |
8837 | * CPUs which means less opportunity to pull tasks. |
8838 | */ | |
5f68eb19 | 8839 | if (sgs->idle_cpus > busiest->idle_cpus) |
0b0695f2 | 8840 | return false; |
5f68eb19 VG |
8841 | else if ((sgs->idle_cpus == busiest->idle_cpus) && |
8842 | (sgs->sum_nr_running <= busiest->sum_nr_running)) | |
8843 | return false; | |
8844 | ||
0b0695f2 | 8845 | break; |
532cb4c4 MN |
8846 | } |
8847 | ||
0b0695f2 VG |
8848 | /* |
8849 | * Candidate sg has no more than one task per CPU and has higher | |
8850 | * per-CPU capacity. Migrating tasks to less capable CPUs may harm | |
8851 | * throughput. Maximize throughput, power/energy consequences are not | |
8852 | * considered. | |
8853 | */ | |
8854 | if ((env->sd->flags & SD_ASYM_CPUCAPACITY) && | |
8855 | (sgs->group_type <= group_fully_busy) && | |
4aed8aa4 | 8856 | (capacity_greater(sg->sgc->min_capacity, capacity_of(env->dst_cpu)))) |
0b0695f2 VG |
8857 | return false; |
8858 | ||
8859 | return true; | |
532cb4c4 MN |
8860 | } |
8861 | ||
0ec8aa00 PZ |
8862 | #ifdef CONFIG_NUMA_BALANCING |
8863 | static inline enum fbq_type fbq_classify_group(struct sg_lb_stats *sgs) | |
8864 | { | |
a3498347 | 8865 | if (sgs->sum_h_nr_running > sgs->nr_numa_running) |
0ec8aa00 | 8866 | return regular; |
a3498347 | 8867 | if (sgs->sum_h_nr_running > sgs->nr_preferred_running) |
0ec8aa00 PZ |
8868 | return remote; |
8869 | return all; | |
8870 | } | |
8871 | ||
8872 | static inline enum fbq_type fbq_classify_rq(struct rq *rq) | |
8873 | { | |
8874 | if (rq->nr_running > rq->nr_numa_running) | |
8875 | return regular; | |
8876 | if (rq->nr_running > rq->nr_preferred_running) | |
8877 | return remote; | |
8878 | return all; | |
8879 | } | |
8880 | #else | |
8881 | static inline enum fbq_type fbq_classify_group(struct sg_lb_stats *sgs) | |
8882 | { | |
8883 | return all; | |
8884 | } | |
8885 | ||
8886 | static inline enum fbq_type fbq_classify_rq(struct rq *rq) | |
8887 | { | |
8888 | return regular; | |
8889 | } | |
8890 | #endif /* CONFIG_NUMA_BALANCING */ | |
8891 | ||
57abff06 VG |
8892 | |
8893 | struct sg_lb_stats; | |
8894 | ||
3318544b VG |
8895 | /* |
8896 | * task_running_on_cpu - return 1 if @p is running on @cpu. | |
8897 | */ | |
8898 | ||
8899 | static unsigned int task_running_on_cpu(int cpu, struct task_struct *p) | |
8900 | { | |
8901 | /* Task has no contribution or is new */ | |
8902 | if (cpu != task_cpu(p) || !READ_ONCE(p->se.avg.last_update_time)) | |
8903 | return 0; | |
8904 | ||
8905 | if (task_on_rq_queued(p)) | |
8906 | return 1; | |
8907 | ||
8908 | return 0; | |
8909 | } | |
8910 | ||
8911 | /** | |
8912 | * idle_cpu_without - would a given CPU be idle without p ? | |
8913 | * @cpu: the processor on which idleness is tested. | |
8914 | * @p: task which should be ignored. | |
8915 | * | |
8916 | * Return: 1 if the CPU would be idle. 0 otherwise. | |
8917 | */ | |
8918 | static int idle_cpu_without(int cpu, struct task_struct *p) | |
8919 | { | |
8920 | struct rq *rq = cpu_rq(cpu); | |
8921 | ||
8922 | if (rq->curr != rq->idle && rq->curr != p) | |
8923 | return 0; | |
8924 | ||
8925 | /* | |
8926 | * rq->nr_running can't be used but an updated version without the | |
8927 | * impact of p on cpu must be used instead. The updated nr_running | |
8928 | * be computed and tested before calling idle_cpu_without(). | |
8929 | */ | |
8930 | ||
8931 | #ifdef CONFIG_SMP | |
126c2092 | 8932 | if (rq->ttwu_pending) |
3318544b VG |
8933 | return 0; |
8934 | #endif | |
8935 | ||
8936 | return 1; | |
8937 | } | |
8938 | ||
57abff06 VG |
8939 | /* |
8940 | * update_sg_wakeup_stats - Update sched_group's statistics for wakeup. | |
3318544b | 8941 | * @sd: The sched_domain level to look for idlest group. |
57abff06 VG |
8942 | * @group: sched_group whose statistics are to be updated. |
8943 | * @sgs: variable to hold the statistics for this group. | |
3318544b | 8944 | * @p: The task for which we look for the idlest group/CPU. |
57abff06 VG |
8945 | */ |
8946 | static inline void update_sg_wakeup_stats(struct sched_domain *sd, | |
8947 | struct sched_group *group, | |
8948 | struct sg_lb_stats *sgs, | |
8949 | struct task_struct *p) | |
8950 | { | |
8951 | int i, nr_running; | |
8952 | ||
8953 | memset(sgs, 0, sizeof(*sgs)); | |
8954 | ||
8955 | for_each_cpu(i, sched_group_span(group)) { | |
8956 | struct rq *rq = cpu_rq(i); | |
3318544b | 8957 | unsigned int local; |
57abff06 | 8958 | |
3318544b | 8959 | sgs->group_load += cpu_load_without(rq, p); |
57abff06 | 8960 | sgs->group_util += cpu_util_without(i, p); |
070f5e86 | 8961 | sgs->group_runnable += cpu_runnable_without(rq, p); |
3318544b VG |
8962 | local = task_running_on_cpu(i, p); |
8963 | sgs->sum_h_nr_running += rq->cfs.h_nr_running - local; | |
57abff06 | 8964 | |
3318544b | 8965 | nr_running = rq->nr_running - local; |
57abff06 VG |
8966 | sgs->sum_nr_running += nr_running; |
8967 | ||
8968 | /* | |
3318544b | 8969 | * No need to call idle_cpu_without() if nr_running is not 0 |
57abff06 | 8970 | */ |
3318544b | 8971 | if (!nr_running && idle_cpu_without(i, p)) |
57abff06 VG |
8972 | sgs->idle_cpus++; |
8973 | ||
57abff06 VG |
8974 | } |
8975 | ||
8976 | /* Check if task fits in the group */ | |
8977 | if (sd->flags & SD_ASYM_CPUCAPACITY && | |
8978 | !task_fits_capacity(p, group->sgc->max_capacity)) { | |
8979 | sgs->group_misfit_task_load = 1; | |
8980 | } | |
8981 | ||
8982 | sgs->group_capacity = group->sgc->capacity; | |
8983 | ||
289de359 VG |
8984 | sgs->group_weight = group->group_weight; |
8985 | ||
57abff06 VG |
8986 | sgs->group_type = group_classify(sd->imbalance_pct, group, sgs); |
8987 | ||
8988 | /* | |
8989 | * Computing avg_load makes sense only when group is fully busy or | |
8990 | * overloaded | |
8991 | */ | |
6c8116c9 TZ |
8992 | if (sgs->group_type == group_fully_busy || |
8993 | sgs->group_type == group_overloaded) | |
57abff06 VG |
8994 | sgs->avg_load = (sgs->group_load * SCHED_CAPACITY_SCALE) / |
8995 | sgs->group_capacity; | |
8996 | } | |
8997 | ||
8998 | static bool update_pick_idlest(struct sched_group *idlest, | |
8999 | struct sg_lb_stats *idlest_sgs, | |
9000 | struct sched_group *group, | |
9001 | struct sg_lb_stats *sgs) | |
9002 | { | |
9003 | if (sgs->group_type < idlest_sgs->group_type) | |
9004 | return true; | |
9005 | ||
9006 | if (sgs->group_type > idlest_sgs->group_type) | |
9007 | return false; | |
9008 | ||
9009 | /* | |
9010 | * The candidate and the current idlest group are the same type of | |
9011 | * group. Let check which one is the idlest according to the type. | |
9012 | */ | |
9013 | ||
9014 | switch (sgs->group_type) { | |
9015 | case group_overloaded: | |
9016 | case group_fully_busy: | |
9017 | /* Select the group with lowest avg_load. */ | |
9018 | if (idlest_sgs->avg_load <= sgs->avg_load) | |
9019 | return false; | |
9020 | break; | |
9021 | ||
9022 | case group_imbalanced: | |
9023 | case group_asym_packing: | |
9024 | /* Those types are not used in the slow wakeup path */ | |
9025 | return false; | |
9026 | ||
9027 | case group_misfit_task: | |
9028 | /* Select group with the highest max capacity */ | |
9029 | if (idlest->sgc->max_capacity >= group->sgc->max_capacity) | |
9030 | return false; | |
9031 | break; | |
9032 | ||
9033 | case group_has_spare: | |
9034 | /* Select group with most idle CPUs */ | |
3edecfef | 9035 | if (idlest_sgs->idle_cpus > sgs->idle_cpus) |
57abff06 | 9036 | return false; |
3edecfef PP |
9037 | |
9038 | /* Select group with lowest group_util */ | |
9039 | if (idlest_sgs->idle_cpus == sgs->idle_cpus && | |
9040 | idlest_sgs->group_util <= sgs->group_util) | |
9041 | return false; | |
9042 | ||
57abff06 VG |
9043 | break; |
9044 | } | |
9045 | ||
9046 | return true; | |
9047 | } | |
9048 | ||
23e6082a MG |
9049 | /* |
9050 | * Allow a NUMA imbalance if busy CPUs is less than 25% of the domain. | |
9051 | * This is an approximation as the number of running tasks may not be | |
9052 | * related to the number of busy CPUs due to sched_setaffinity. | |
9053 | */ | |
9054 | static inline bool allow_numa_imbalance(int dst_running, int dst_weight) | |
9055 | { | |
9056 | return (dst_running < (dst_weight >> 2)); | |
9057 | } | |
9058 | ||
57abff06 VG |
9059 | /* |
9060 | * find_idlest_group() finds and returns the least busy CPU group within the | |
9061 | * domain. | |
9062 | * | |
9063 | * Assumes p is allowed on at least one CPU in sd. | |
9064 | */ | |
9065 | static struct sched_group * | |
45da2773 | 9066 | find_idlest_group(struct sched_domain *sd, struct task_struct *p, int this_cpu) |
57abff06 VG |
9067 | { |
9068 | struct sched_group *idlest = NULL, *local = NULL, *group = sd->groups; | |
9069 | struct sg_lb_stats local_sgs, tmp_sgs; | |
9070 | struct sg_lb_stats *sgs; | |
9071 | unsigned long imbalance; | |
9072 | struct sg_lb_stats idlest_sgs = { | |
9073 | .avg_load = UINT_MAX, | |
9074 | .group_type = group_overloaded, | |
9075 | }; | |
9076 | ||
57abff06 VG |
9077 | do { |
9078 | int local_group; | |
9079 | ||
9080 | /* Skip over this group if it has no CPUs allowed */ | |
9081 | if (!cpumask_intersects(sched_group_span(group), | |
9082 | p->cpus_ptr)) | |
9083 | continue; | |
9084 | ||
97886d9d AL |
9085 | /* Skip over this group if no cookie matched */ |
9086 | if (!sched_group_cookie_match(cpu_rq(this_cpu), p, group)) | |
9087 | continue; | |
9088 | ||
57abff06 VG |
9089 | local_group = cpumask_test_cpu(this_cpu, |
9090 | sched_group_span(group)); | |
9091 | ||
9092 | if (local_group) { | |
9093 | sgs = &local_sgs; | |
9094 | local = group; | |
9095 | } else { | |
9096 | sgs = &tmp_sgs; | |
9097 | } | |
9098 | ||
9099 | update_sg_wakeup_stats(sd, group, sgs, p); | |
9100 | ||
9101 | if (!local_group && update_pick_idlest(idlest, &idlest_sgs, group, sgs)) { | |
9102 | idlest = group; | |
9103 | idlest_sgs = *sgs; | |
9104 | } | |
9105 | ||
9106 | } while (group = group->next, group != sd->groups); | |
9107 | ||
9108 | ||
9109 | /* There is no idlest group to push tasks to */ | |
9110 | if (!idlest) | |
9111 | return NULL; | |
9112 | ||
7ed735c3 VG |
9113 | /* The local group has been skipped because of CPU affinity */ |
9114 | if (!local) | |
9115 | return idlest; | |
9116 | ||
57abff06 VG |
9117 | /* |
9118 | * If the local group is idler than the selected idlest group | |
9119 | * don't try and push the task. | |
9120 | */ | |
9121 | if (local_sgs.group_type < idlest_sgs.group_type) | |
9122 | return NULL; | |
9123 | ||
9124 | /* | |
9125 | * If the local group is busier than the selected idlest group | |
9126 | * try and push the task. | |
9127 | */ | |
9128 | if (local_sgs.group_type > idlest_sgs.group_type) | |
9129 | return idlest; | |
9130 | ||
9131 | switch (local_sgs.group_type) { | |
9132 | case group_overloaded: | |
9133 | case group_fully_busy: | |
5c339005 MG |
9134 | |
9135 | /* Calculate allowed imbalance based on load */ | |
9136 | imbalance = scale_load_down(NICE_0_LOAD) * | |
9137 | (sd->imbalance_pct-100) / 100; | |
9138 | ||
57abff06 VG |
9139 | /* |
9140 | * When comparing groups across NUMA domains, it's possible for | |
9141 | * the local domain to be very lightly loaded relative to the | |
9142 | * remote domains but "imbalance" skews the comparison making | |
9143 | * remote CPUs look much more favourable. When considering | |
9144 | * cross-domain, add imbalance to the load on the remote node | |
9145 | * and consider staying local. | |
9146 | */ | |
9147 | ||
9148 | if ((sd->flags & SD_NUMA) && | |
9149 | ((idlest_sgs.avg_load + imbalance) >= local_sgs.avg_load)) | |
9150 | return NULL; | |
9151 | ||
9152 | /* | |
9153 | * If the local group is less loaded than the selected | |
9154 | * idlest group don't try and push any tasks. | |
9155 | */ | |
9156 | if (idlest_sgs.avg_load >= (local_sgs.avg_load + imbalance)) | |
9157 | return NULL; | |
9158 | ||
9159 | if (100 * local_sgs.avg_load <= sd->imbalance_pct * idlest_sgs.avg_load) | |
9160 | return NULL; | |
9161 | break; | |
9162 | ||
9163 | case group_imbalanced: | |
9164 | case group_asym_packing: | |
9165 | /* Those type are not used in the slow wakeup path */ | |
9166 | return NULL; | |
9167 | ||
9168 | case group_misfit_task: | |
9169 | /* Select group with the highest max capacity */ | |
9170 | if (local->sgc->max_capacity >= idlest->sgc->max_capacity) | |
9171 | return NULL; | |
9172 | break; | |
9173 | ||
9174 | case group_has_spare: | |
9175 | if (sd->flags & SD_NUMA) { | |
9176 | #ifdef CONFIG_NUMA_BALANCING | |
9177 | int idlest_cpu; | |
9178 | /* | |
9179 | * If there is spare capacity at NUMA, try to select | |
9180 | * the preferred node | |
9181 | */ | |
9182 | if (cpu_to_node(this_cpu) == p->numa_preferred_nid) | |
9183 | return NULL; | |
9184 | ||
9185 | idlest_cpu = cpumask_first(sched_group_span(idlest)); | |
9186 | if (cpu_to_node(idlest_cpu) == p->numa_preferred_nid) | |
9187 | return idlest; | |
9188 | #endif | |
9189 | /* | |
9190 | * Otherwise, keep the task on this node to stay close | |
9191 | * its wakeup source and improve locality. If there is | |
9192 | * a real need of migration, periodic load balance will | |
9193 | * take care of it. | |
9194 | */ | |
23e6082a | 9195 | if (allow_numa_imbalance(local_sgs.sum_nr_running, sd->span_weight)) |
57abff06 VG |
9196 | return NULL; |
9197 | } | |
9198 | ||
9199 | /* | |
9200 | * Select group with highest number of idle CPUs. We could also | |
9201 | * compare the utilization which is more stable but it can end | |
9202 | * up that the group has less spare capacity but finally more | |
9203 | * idle CPUs which means more opportunity to run task. | |
9204 | */ | |
9205 | if (local_sgs.idle_cpus >= idlest_sgs.idle_cpus) | |
9206 | return NULL; | |
9207 | break; | |
9208 | } | |
9209 | ||
9210 | return idlest; | |
9211 | } | |
9212 | ||
1e3c88bd | 9213 | /** |
461819ac | 9214 | * update_sd_lb_stats - Update sched_domain's statistics for load balancing. |
cd96891d | 9215 | * @env: The load balancing environment. |
1e3c88bd PZ |
9216 | * @sds: variable to hold the statistics for this sched_domain. |
9217 | */ | |
0b0695f2 | 9218 | |
0ec8aa00 | 9219 | static inline void update_sd_lb_stats(struct lb_env *env, struct sd_lb_stats *sds) |
1e3c88bd | 9220 | { |
bd939f45 PZ |
9221 | struct sched_domain *child = env->sd->child; |
9222 | struct sched_group *sg = env->sd->groups; | |
05b40e05 | 9223 | struct sg_lb_stats *local = &sds->local_stat; |
56cf515b | 9224 | struct sg_lb_stats tmp_sgs; |
630246a0 | 9225 | int sg_status = 0; |
1e3c88bd | 9226 | |
1e3c88bd | 9227 | do { |
56cf515b | 9228 | struct sg_lb_stats *sgs = &tmp_sgs; |
1e3c88bd PZ |
9229 | int local_group; |
9230 | ||
ae4df9d6 | 9231 | local_group = cpumask_test_cpu(env->dst_cpu, sched_group_span(sg)); |
56cf515b JK |
9232 | if (local_group) { |
9233 | sds->local = sg; | |
05b40e05 | 9234 | sgs = local; |
b72ff13c PZ |
9235 | |
9236 | if (env->idle != CPU_NEWLY_IDLE || | |
63b2ca30 NP |
9237 | time_after_eq(jiffies, sg->sgc->next_update)) |
9238 | update_group_capacity(env->sd, env->dst_cpu); | |
56cf515b | 9239 | } |
1e3c88bd | 9240 | |
c0d14b57 | 9241 | update_sg_lb_stats(env, sds, sg, sgs, &sg_status); |
1e3c88bd | 9242 | |
b72ff13c PZ |
9243 | if (local_group) |
9244 | goto next_group; | |
9245 | ||
1e3c88bd | 9246 | |
b72ff13c | 9247 | if (update_sd_pick_busiest(env, sds, sg, sgs)) { |
532cb4c4 | 9248 | sds->busiest = sg; |
56cf515b | 9249 | sds->busiest_stat = *sgs; |
1e3c88bd PZ |
9250 | } |
9251 | ||
b72ff13c PZ |
9252 | next_group: |
9253 | /* Now, start updating sd_lb_stats */ | |
9254 | sds->total_load += sgs->group_load; | |
63b2ca30 | 9255 | sds->total_capacity += sgs->group_capacity; |
b72ff13c | 9256 | |
532cb4c4 | 9257 | sg = sg->next; |
bd939f45 | 9258 | } while (sg != env->sd->groups); |
0ec8aa00 | 9259 | |
0b0695f2 VG |
9260 | /* Tag domain that child domain prefers tasks go to siblings first */ |
9261 | sds->prefer_sibling = child && child->flags & SD_PREFER_SIBLING; | |
9262 | ||
f643ea22 | 9263 | |
0ec8aa00 PZ |
9264 | if (env->sd->flags & SD_NUMA) |
9265 | env->fbq_type = fbq_classify_group(&sds->busiest_stat); | |
4486edd1 TC |
9266 | |
9267 | if (!env->sd->parent) { | |
2802bf3c MR |
9268 | struct root_domain *rd = env->dst_rq->rd; |
9269 | ||
4486edd1 | 9270 | /* update overload indicator if we are at root domain */ |
2802bf3c MR |
9271 | WRITE_ONCE(rd->overload, sg_status & SG_OVERLOAD); |
9272 | ||
9273 | /* Update over-utilization (tipping point, U >= 0) indicator */ | |
9274 | WRITE_ONCE(rd->overutilized, sg_status & SG_OVERUTILIZED); | |
f9f240f9 | 9275 | trace_sched_overutilized_tp(rd, sg_status & SG_OVERUTILIZED); |
2802bf3c | 9276 | } else if (sg_status & SG_OVERUTILIZED) { |
f9f240f9 QY |
9277 | struct root_domain *rd = env->dst_rq->rd; |
9278 | ||
9279 | WRITE_ONCE(rd->overutilized, SG_OVERUTILIZED); | |
9280 | trace_sched_overutilized_tp(rd, SG_OVERUTILIZED); | |
4486edd1 | 9281 | } |
532cb4c4 MN |
9282 | } |
9283 | ||
abeae76a MG |
9284 | #define NUMA_IMBALANCE_MIN 2 |
9285 | ||
7d2b5dd0 MG |
9286 | static inline long adjust_numa_imbalance(int imbalance, |
9287 | int dst_running, int dst_weight) | |
fb86f5b2 | 9288 | { |
23e6082a MG |
9289 | if (!allow_numa_imbalance(dst_running, dst_weight)) |
9290 | return imbalance; | |
9291 | ||
fb86f5b2 MG |
9292 | /* |
9293 | * Allow a small imbalance based on a simple pair of communicating | |
7d2b5dd0 | 9294 | * tasks that remain local when the destination is lightly loaded. |
fb86f5b2 | 9295 | */ |
23e6082a | 9296 | if (imbalance <= NUMA_IMBALANCE_MIN) |
fb86f5b2 MG |
9297 | return 0; |
9298 | ||
9299 | return imbalance; | |
9300 | } | |
9301 | ||
1e3c88bd PZ |
9302 | /** |
9303 | * calculate_imbalance - Calculate the amount of imbalance present within the | |
9304 | * groups of a given sched_domain during load balance. | |
bd939f45 | 9305 | * @env: load balance environment |
1e3c88bd | 9306 | * @sds: statistics of the sched_domain whose imbalance is to be calculated. |
1e3c88bd | 9307 | */ |
bd939f45 | 9308 | static inline void calculate_imbalance(struct lb_env *env, struct sd_lb_stats *sds) |
1e3c88bd | 9309 | { |
56cf515b JK |
9310 | struct sg_lb_stats *local, *busiest; |
9311 | ||
9312 | local = &sds->local_stat; | |
56cf515b | 9313 | busiest = &sds->busiest_stat; |
dd5feea1 | 9314 | |
0b0695f2 VG |
9315 | if (busiest->group_type == group_misfit_task) { |
9316 | /* Set imbalance to allow misfit tasks to be balanced. */ | |
9317 | env->migration_type = migrate_misfit; | |
c63be7be | 9318 | env->imbalance = 1; |
0b0695f2 VG |
9319 | return; |
9320 | } | |
9321 | ||
9322 | if (busiest->group_type == group_asym_packing) { | |
9323 | /* | |
9324 | * In case of asym capacity, we will try to migrate all load to | |
9325 | * the preferred CPU. | |
9326 | */ | |
9327 | env->migration_type = migrate_task; | |
9328 | env->imbalance = busiest->sum_h_nr_running; | |
9329 | return; | |
9330 | } | |
9331 | ||
9332 | if (busiest->group_type == group_imbalanced) { | |
9333 | /* | |
9334 | * In the group_imb case we cannot rely on group-wide averages | |
9335 | * to ensure CPU-load equilibrium, try to move any task to fix | |
9336 | * the imbalance. The next load balance will take care of | |
9337 | * balancing back the system. | |
9338 | */ | |
9339 | env->migration_type = migrate_task; | |
9340 | env->imbalance = 1; | |
490ba971 VG |
9341 | return; |
9342 | } | |
9343 | ||
1e3c88bd | 9344 | /* |
0b0695f2 | 9345 | * Try to use spare capacity of local group without overloading it or |
a9723389 | 9346 | * emptying busiest. |
1e3c88bd | 9347 | */ |
0b0695f2 | 9348 | if (local->group_type == group_has_spare) { |
16b0a7a1 VG |
9349 | if ((busiest->group_type > group_fully_busy) && |
9350 | !(env->sd->flags & SD_SHARE_PKG_RESOURCES)) { | |
0b0695f2 VG |
9351 | /* |
9352 | * If busiest is overloaded, try to fill spare | |
9353 | * capacity. This might end up creating spare capacity | |
9354 | * in busiest or busiest still being overloaded but | |
9355 | * there is no simple way to directly compute the | |
9356 | * amount of load to migrate in order to balance the | |
9357 | * system. | |
9358 | */ | |
9359 | env->migration_type = migrate_util; | |
9360 | env->imbalance = max(local->group_capacity, local->group_util) - | |
9361 | local->group_util; | |
9362 | ||
9363 | /* | |
9364 | * In some cases, the group's utilization is max or even | |
9365 | * higher than capacity because of migrations but the | |
9366 | * local CPU is (newly) idle. There is at least one | |
9367 | * waiting task in this overloaded busiest group. Let's | |
9368 | * try to pull it. | |
9369 | */ | |
9370 | if (env->idle != CPU_NOT_IDLE && env->imbalance == 0) { | |
9371 | env->migration_type = migrate_task; | |
9372 | env->imbalance = 1; | |
9373 | } | |
9374 | ||
9375 | return; | |
9376 | } | |
9377 | ||
9378 | if (busiest->group_weight == 1 || sds->prefer_sibling) { | |
5e23e474 | 9379 | unsigned int nr_diff = busiest->sum_nr_running; |
0b0695f2 VG |
9380 | /* |
9381 | * When prefer sibling, evenly spread running tasks on | |
9382 | * groups. | |
9383 | */ | |
9384 | env->migration_type = migrate_task; | |
5e23e474 | 9385 | lsub_positive(&nr_diff, local->sum_nr_running); |
0b0695f2 | 9386 | env->imbalance = nr_diff >> 1; |
b396f523 | 9387 | } else { |
0b0695f2 | 9388 | |
b396f523 MG |
9389 | /* |
9390 | * If there is no overload, we just want to even the number of | |
9391 | * idle cpus. | |
9392 | */ | |
9393 | env->migration_type = migrate_task; | |
9394 | env->imbalance = max_t(long, 0, (local->idle_cpus - | |
0b0695f2 | 9395 | busiest->idle_cpus) >> 1); |
b396f523 MG |
9396 | } |
9397 | ||
9398 | /* Consider allowing a small imbalance between NUMA groups */ | |
7d2b5dd0 | 9399 | if (env->sd->flags & SD_NUMA) { |
fb86f5b2 | 9400 | env->imbalance = adjust_numa_imbalance(env->imbalance, |
7d2b5dd0 MG |
9401 | busiest->sum_nr_running, busiest->group_weight); |
9402 | } | |
b396f523 | 9403 | |
fcf0553d | 9404 | return; |
1e3c88bd PZ |
9405 | } |
9406 | ||
9a5d9ba6 | 9407 | /* |
0b0695f2 VG |
9408 | * Local is fully busy but has to take more load to relieve the |
9409 | * busiest group | |
9a5d9ba6 | 9410 | */ |
0b0695f2 VG |
9411 | if (local->group_type < group_overloaded) { |
9412 | /* | |
9413 | * Local will become overloaded so the avg_load metrics are | |
9414 | * finally needed. | |
9415 | */ | |
9416 | ||
9417 | local->avg_load = (local->group_load * SCHED_CAPACITY_SCALE) / | |
9418 | local->group_capacity; | |
9419 | ||
9420 | sds->avg_load = (sds->total_load * SCHED_CAPACITY_SCALE) / | |
9421 | sds->total_capacity; | |
111688ca AL |
9422 | /* |
9423 | * If the local group is more loaded than the selected | |
9424 | * busiest group don't try to pull any tasks. | |
9425 | */ | |
9426 | if (local->avg_load >= busiest->avg_load) { | |
9427 | env->imbalance = 0; | |
9428 | return; | |
9429 | } | |
dd5feea1 SS |
9430 | } |
9431 | ||
9432 | /* | |
0b0695f2 VG |
9433 | * Both group are or will become overloaded and we're trying to get all |
9434 | * the CPUs to the average_load, so we don't want to push ourselves | |
9435 | * above the average load, nor do we wish to reduce the max loaded CPU | |
9436 | * below the average load. At the same time, we also don't want to | |
9437 | * reduce the group load below the group capacity. Thus we look for | |
9438 | * the minimum possible imbalance. | |
dd5feea1 | 9439 | */ |
0b0695f2 | 9440 | env->migration_type = migrate_load; |
56cf515b | 9441 | env->imbalance = min( |
0b0695f2 | 9442 | (busiest->avg_load - sds->avg_load) * busiest->group_capacity, |
63b2ca30 | 9443 | (sds->avg_load - local->avg_load) * local->group_capacity |
ca8ce3d0 | 9444 | ) / SCHED_CAPACITY_SCALE; |
1e3c88bd | 9445 | } |
fab47622 | 9446 | |
1e3c88bd PZ |
9447 | /******* find_busiest_group() helpers end here *********************/ |
9448 | ||
0b0695f2 VG |
9449 | /* |
9450 | * Decision matrix according to the local and busiest group type: | |
9451 | * | |
9452 | * busiest \ local has_spare fully_busy misfit asym imbalanced overloaded | |
9453 | * has_spare nr_idle balanced N/A N/A balanced balanced | |
9454 | * fully_busy nr_idle nr_idle N/A N/A balanced balanced | |
9455 | * misfit_task force N/A N/A N/A force force | |
9456 | * asym_packing force force N/A N/A force force | |
9457 | * imbalanced force force N/A N/A force force | |
9458 | * overloaded force force N/A N/A force avg_load | |
9459 | * | |
9460 | * N/A : Not Applicable because already filtered while updating | |
9461 | * statistics. | |
9462 | * balanced : The system is balanced for these 2 groups. | |
9463 | * force : Calculate the imbalance as load migration is probably needed. | |
9464 | * avg_load : Only if imbalance is significant enough. | |
9465 | * nr_idle : dst_cpu is not busy and the number of idle CPUs is quite | |
9466 | * different in groups. | |
9467 | */ | |
9468 | ||
1e3c88bd PZ |
9469 | /** |
9470 | * find_busiest_group - Returns the busiest group within the sched_domain | |
0a9b23ce | 9471 | * if there is an imbalance. |
1e3c88bd | 9472 | * |
a3df0679 | 9473 | * Also calculates the amount of runnable load which should be moved |
1e3c88bd PZ |
9474 | * to restore balance. |
9475 | * | |
cd96891d | 9476 | * @env: The load balancing environment. |
1e3c88bd | 9477 | * |
e69f6186 | 9478 | * Return: - The busiest group if imbalance exists. |
1e3c88bd | 9479 | */ |
56cf515b | 9480 | static struct sched_group *find_busiest_group(struct lb_env *env) |
1e3c88bd | 9481 | { |
56cf515b | 9482 | struct sg_lb_stats *local, *busiest; |
1e3c88bd PZ |
9483 | struct sd_lb_stats sds; |
9484 | ||
147c5fc2 | 9485 | init_sd_lb_stats(&sds); |
1e3c88bd PZ |
9486 | |
9487 | /* | |
b0fb1eb4 | 9488 | * Compute the various statistics relevant for load balancing at |
1e3c88bd PZ |
9489 | * this level. |
9490 | */ | |
23f0d209 | 9491 | update_sd_lb_stats(env, &sds); |
2802bf3c | 9492 | |
f8a696f2 | 9493 | if (sched_energy_enabled()) { |
2802bf3c MR |
9494 | struct root_domain *rd = env->dst_rq->rd; |
9495 | ||
9496 | if (rcu_dereference(rd->pd) && !READ_ONCE(rd->overutilized)) | |
9497 | goto out_balanced; | |
9498 | } | |
9499 | ||
56cf515b JK |
9500 | local = &sds.local_stat; |
9501 | busiest = &sds.busiest_stat; | |
1e3c88bd | 9502 | |
cc57aa8f | 9503 | /* There is no busy sibling group to pull tasks from */ |
0b0695f2 | 9504 | if (!sds.busiest) |
1e3c88bd PZ |
9505 | goto out_balanced; |
9506 | ||
0b0695f2 VG |
9507 | /* Misfit tasks should be dealt with regardless of the avg load */ |
9508 | if (busiest->group_type == group_misfit_task) | |
9509 | goto force_balance; | |
9510 | ||
9511 | /* ASYM feature bypasses nice load balance check */ | |
9512 | if (busiest->group_type == group_asym_packing) | |
9513 | goto force_balance; | |
b0432d8f | 9514 | |
866ab43e PZ |
9515 | /* |
9516 | * If the busiest group is imbalanced the below checks don't | |
30ce5dab | 9517 | * work because they assume all things are equal, which typically |
3bd37062 | 9518 | * isn't true due to cpus_ptr constraints and the like. |
866ab43e | 9519 | */ |
caeb178c | 9520 | if (busiest->group_type == group_imbalanced) |
866ab43e PZ |
9521 | goto force_balance; |
9522 | ||
cc57aa8f | 9523 | /* |
9c58c79a | 9524 | * If the local group is busier than the selected busiest group |
cc57aa8f PZ |
9525 | * don't try and pull any tasks. |
9526 | */ | |
0b0695f2 | 9527 | if (local->group_type > busiest->group_type) |
1e3c88bd PZ |
9528 | goto out_balanced; |
9529 | ||
cc57aa8f | 9530 | /* |
0b0695f2 VG |
9531 | * When groups are overloaded, use the avg_load to ensure fairness |
9532 | * between tasks. | |
cc57aa8f | 9533 | */ |
0b0695f2 VG |
9534 | if (local->group_type == group_overloaded) { |
9535 | /* | |
9536 | * If the local group is more loaded than the selected | |
9537 | * busiest group don't try to pull any tasks. | |
9538 | */ | |
9539 | if (local->avg_load >= busiest->avg_load) | |
9540 | goto out_balanced; | |
9541 | ||
9542 | /* XXX broken for overlapping NUMA groups */ | |
9543 | sds.avg_load = (sds.total_load * SCHED_CAPACITY_SCALE) / | |
9544 | sds.total_capacity; | |
1e3c88bd | 9545 | |
aae6d3dd | 9546 | /* |
0b0695f2 VG |
9547 | * Don't pull any tasks if this group is already above the |
9548 | * domain average load. | |
aae6d3dd | 9549 | */ |
0b0695f2 | 9550 | if (local->avg_load >= sds.avg_load) |
aae6d3dd | 9551 | goto out_balanced; |
0b0695f2 | 9552 | |
c186fafe | 9553 | /* |
0b0695f2 VG |
9554 | * If the busiest group is more loaded, use imbalance_pct to be |
9555 | * conservative. | |
c186fafe | 9556 | */ |
56cf515b JK |
9557 | if (100 * busiest->avg_load <= |
9558 | env->sd->imbalance_pct * local->avg_load) | |
c186fafe | 9559 | goto out_balanced; |
aae6d3dd | 9560 | } |
1e3c88bd | 9561 | |
0b0695f2 VG |
9562 | /* Try to move all excess tasks to child's sibling domain */ |
9563 | if (sds.prefer_sibling && local->group_type == group_has_spare && | |
5e23e474 | 9564 | busiest->sum_nr_running > local->sum_nr_running + 1) |
0b0695f2 VG |
9565 | goto force_balance; |
9566 | ||
2ab4092f VG |
9567 | if (busiest->group_type != group_overloaded) { |
9568 | if (env->idle == CPU_NOT_IDLE) | |
9569 | /* | |
9570 | * If the busiest group is not overloaded (and as a | |
9571 | * result the local one too) but this CPU is already | |
9572 | * busy, let another idle CPU try to pull task. | |
9573 | */ | |
9574 | goto out_balanced; | |
9575 | ||
9576 | if (busiest->group_weight > 1 && | |
9577 | local->idle_cpus <= (busiest->idle_cpus + 1)) | |
9578 | /* | |
9579 | * If the busiest group is not overloaded | |
9580 | * and there is no imbalance between this and busiest | |
9581 | * group wrt idle CPUs, it is balanced. The imbalance | |
9582 | * becomes significant if the diff is greater than 1 | |
9583 | * otherwise we might end up to just move the imbalance | |
9584 | * on another group. Of course this applies only if | |
9585 | * there is more than 1 CPU per group. | |
9586 | */ | |
9587 | goto out_balanced; | |
9588 | ||
9589 | if (busiest->sum_h_nr_running == 1) | |
9590 | /* | |
9591 | * busiest doesn't have any tasks waiting to run | |
9592 | */ | |
9593 | goto out_balanced; | |
9594 | } | |
0b0695f2 | 9595 | |
fab47622 | 9596 | force_balance: |
1e3c88bd | 9597 | /* Looks like there is an imbalance. Compute it */ |
bd939f45 | 9598 | calculate_imbalance(env, &sds); |
bb3485c8 | 9599 | return env->imbalance ? sds.busiest : NULL; |
1e3c88bd PZ |
9600 | |
9601 | out_balanced: | |
bd939f45 | 9602 | env->imbalance = 0; |
1e3c88bd PZ |
9603 | return NULL; |
9604 | } | |
9605 | ||
9606 | /* | |
97fb7a0a | 9607 | * find_busiest_queue - find the busiest runqueue among the CPUs in the group. |
1e3c88bd | 9608 | */ |
bd939f45 | 9609 | static struct rq *find_busiest_queue(struct lb_env *env, |
b9403130 | 9610 | struct sched_group *group) |
1e3c88bd PZ |
9611 | { |
9612 | struct rq *busiest = NULL, *rq; | |
0b0695f2 VG |
9613 | unsigned long busiest_util = 0, busiest_load = 0, busiest_capacity = 1; |
9614 | unsigned int busiest_nr = 0; | |
1e3c88bd PZ |
9615 | int i; |
9616 | ||
ae4df9d6 | 9617 | for_each_cpu_and(i, sched_group_span(group), env->cpus) { |
0b0695f2 VG |
9618 | unsigned long capacity, load, util; |
9619 | unsigned int nr_running; | |
0ec8aa00 PZ |
9620 | enum fbq_type rt; |
9621 | ||
9622 | rq = cpu_rq(i); | |
9623 | rt = fbq_classify_rq(rq); | |
1e3c88bd | 9624 | |
0ec8aa00 PZ |
9625 | /* |
9626 | * We classify groups/runqueues into three groups: | |
9627 | * - regular: there are !numa tasks | |
9628 | * - remote: there are numa tasks that run on the 'wrong' node | |
9629 | * - all: there is no distinction | |
9630 | * | |
9631 | * In order to avoid migrating ideally placed numa tasks, | |
9632 | * ignore those when there's better options. | |
9633 | * | |
9634 | * If we ignore the actual busiest queue to migrate another | |
9635 | * task, the next balance pass can still reduce the busiest | |
9636 | * queue by moving tasks around inside the node. | |
9637 | * | |
9638 | * If we cannot move enough load due to this classification | |
9639 | * the next pass will adjust the group classification and | |
9640 | * allow migration of more tasks. | |
9641 | * | |
9642 | * Both cases only affect the total convergence complexity. | |
9643 | */ | |
9644 | if (rt > env->fbq_type) | |
9645 | continue; | |
9646 | ||
0b0695f2 | 9647 | nr_running = rq->cfs.h_nr_running; |
fc488ffd VG |
9648 | if (!nr_running) |
9649 | continue; | |
9650 | ||
9651 | capacity = capacity_of(i); | |
9d5efe05 | 9652 | |
4ad3831a CR |
9653 | /* |
9654 | * For ASYM_CPUCAPACITY domains, don't pick a CPU that could | |
9655 | * eventually lead to active_balancing high->low capacity. | |
9656 | * Higher per-CPU capacity is considered better than balancing | |
9657 | * average load. | |
9658 | */ | |
9659 | if (env->sd->flags & SD_ASYM_CPUCAPACITY && | |
4aed8aa4 | 9660 | !capacity_greater(capacity_of(env->dst_cpu), capacity) && |
0b0695f2 | 9661 | nr_running == 1) |
4ad3831a CR |
9662 | continue; |
9663 | ||
4006a72b RN |
9664 | /* Make sure we only pull tasks from a CPU of lower priority */ |
9665 | if ((env->sd->flags & SD_ASYM_PACKING) && | |
9666 | sched_asym_prefer(i, env->dst_cpu) && | |
9667 | nr_running == 1) | |
9668 | continue; | |
9669 | ||
0b0695f2 VG |
9670 | switch (env->migration_type) { |
9671 | case migrate_load: | |
9672 | /* | |
b0fb1eb4 VG |
9673 | * When comparing with load imbalance, use cpu_load() |
9674 | * which is not scaled with the CPU capacity. | |
0b0695f2 | 9675 | */ |
b0fb1eb4 | 9676 | load = cpu_load(rq); |
1e3c88bd | 9677 | |
0b0695f2 VG |
9678 | if (nr_running == 1 && load > env->imbalance && |
9679 | !check_cpu_capacity(rq, env->sd)) | |
9680 | break; | |
ea67821b | 9681 | |
0b0695f2 VG |
9682 | /* |
9683 | * For the load comparisons with the other CPUs, | |
b0fb1eb4 VG |
9684 | * consider the cpu_load() scaled with the CPU |
9685 | * capacity, so that the load can be moved away | |
9686 | * from the CPU that is potentially running at a | |
9687 | * lower capacity. | |
0b0695f2 VG |
9688 | * |
9689 | * Thus we're looking for max(load_i / capacity_i), | |
9690 | * crosswise multiplication to rid ourselves of the | |
9691 | * division works out to: | |
9692 | * load_i * capacity_j > load_j * capacity_i; | |
9693 | * where j is our previous maximum. | |
9694 | */ | |
9695 | if (load * busiest_capacity > busiest_load * capacity) { | |
9696 | busiest_load = load; | |
9697 | busiest_capacity = capacity; | |
9698 | busiest = rq; | |
9699 | } | |
9700 | break; | |
9701 | ||
9702 | case migrate_util: | |
9703 | util = cpu_util(cpu_of(rq)); | |
9704 | ||
c32b4308 VG |
9705 | /* |
9706 | * Don't try to pull utilization from a CPU with one | |
9707 | * running task. Whatever its utilization, we will fail | |
9708 | * detach the task. | |
9709 | */ | |
9710 | if (nr_running <= 1) | |
9711 | continue; | |
9712 | ||
0b0695f2 VG |
9713 | if (busiest_util < util) { |
9714 | busiest_util = util; | |
9715 | busiest = rq; | |
9716 | } | |
9717 | break; | |
9718 | ||
9719 | case migrate_task: | |
9720 | if (busiest_nr < nr_running) { | |
9721 | busiest_nr = nr_running; | |
9722 | busiest = rq; | |
9723 | } | |
9724 | break; | |
9725 | ||
9726 | case migrate_misfit: | |
9727 | /* | |
9728 | * For ASYM_CPUCAPACITY domains with misfit tasks we | |
9729 | * simply seek the "biggest" misfit task. | |
9730 | */ | |
9731 | if (rq->misfit_task_load > busiest_load) { | |
9732 | busiest_load = rq->misfit_task_load; | |
9733 | busiest = rq; | |
9734 | } | |
9735 | ||
9736 | break; | |
1e3c88bd | 9737 | |
1e3c88bd PZ |
9738 | } |
9739 | } | |
9740 | ||
9741 | return busiest; | |
9742 | } | |
9743 | ||
9744 | /* | |
9745 | * Max backoff if we encounter pinned tasks. Pretty arbitrary value, but | |
9746 | * so long as it is large enough. | |
9747 | */ | |
9748 | #define MAX_PINNED_INTERVAL 512 | |
9749 | ||
46a745d9 VG |
9750 | static inline bool |
9751 | asym_active_balance(struct lb_env *env) | |
1af3ed3d | 9752 | { |
46a745d9 VG |
9753 | /* |
9754 | * ASYM_PACKING needs to force migrate tasks from busy but | |
9755 | * lower priority CPUs in order to pack all tasks in the | |
9756 | * highest priority CPUs. | |
9757 | */ | |
9758 | return env->idle != CPU_NOT_IDLE && (env->sd->flags & SD_ASYM_PACKING) && | |
9759 | sched_asym_prefer(env->dst_cpu, env->src_cpu); | |
9760 | } | |
bd939f45 | 9761 | |
46a745d9 | 9762 | static inline bool |
e9b9734b VG |
9763 | imbalanced_active_balance(struct lb_env *env) |
9764 | { | |
9765 | struct sched_domain *sd = env->sd; | |
9766 | ||
9767 | /* | |
9768 | * The imbalanced case includes the case of pinned tasks preventing a fair | |
9769 | * distribution of the load on the system but also the even distribution of the | |
9770 | * threads on a system with spare capacity | |
9771 | */ | |
9772 | if ((env->migration_type == migrate_task) && | |
9773 | (sd->nr_balance_failed > sd->cache_nice_tries+2)) | |
9774 | return 1; | |
9775 | ||
9776 | return 0; | |
9777 | } | |
9778 | ||
9779 | static int need_active_balance(struct lb_env *env) | |
46a745d9 VG |
9780 | { |
9781 | struct sched_domain *sd = env->sd; | |
532cb4c4 | 9782 | |
46a745d9 VG |
9783 | if (asym_active_balance(env)) |
9784 | return 1; | |
1af3ed3d | 9785 | |
e9b9734b VG |
9786 | if (imbalanced_active_balance(env)) |
9787 | return 1; | |
9788 | ||
1aaf90a4 VG |
9789 | /* |
9790 | * The dst_cpu is idle and the src_cpu CPU has only 1 CFS task. | |
9791 | * It's worth migrating the task if the src_cpu's capacity is reduced | |
9792 | * because of other sched_class or IRQs if more capacity stays | |
9793 | * available on dst_cpu. | |
9794 | */ | |
9795 | if ((env->idle != CPU_NOT_IDLE) && | |
9796 | (env->src_rq->cfs.h_nr_running == 1)) { | |
9797 | if ((check_cpu_capacity(env->src_rq, sd)) && | |
9798 | (capacity_of(env->src_cpu)*sd->imbalance_pct < capacity_of(env->dst_cpu)*100)) | |
9799 | return 1; | |
9800 | } | |
9801 | ||
0b0695f2 | 9802 | if (env->migration_type == migrate_misfit) |
cad68e55 MR |
9803 | return 1; |
9804 | ||
46a745d9 VG |
9805 | return 0; |
9806 | } | |
9807 | ||
969c7921 TH |
9808 | static int active_load_balance_cpu_stop(void *data); |
9809 | ||
23f0d209 JK |
9810 | static int should_we_balance(struct lb_env *env) |
9811 | { | |
9812 | struct sched_group *sg = env->sd->groups; | |
64297f2b | 9813 | int cpu; |
23f0d209 | 9814 | |
024c9d2f PZ |
9815 | /* |
9816 | * Ensure the balancing environment is consistent; can happen | |
9817 | * when the softirq triggers 'during' hotplug. | |
9818 | */ | |
9819 | if (!cpumask_test_cpu(env->dst_cpu, env->cpus)) | |
9820 | return 0; | |
9821 | ||
23f0d209 | 9822 | /* |
97fb7a0a | 9823 | * In the newly idle case, we will allow all the CPUs |
23f0d209 JK |
9824 | * to do the newly idle load balance. |
9825 | */ | |
9826 | if (env->idle == CPU_NEWLY_IDLE) | |
9827 | return 1; | |
9828 | ||
97fb7a0a | 9829 | /* Try to find first idle CPU */ |
e5c14b1f | 9830 | for_each_cpu_and(cpu, group_balance_mask(sg), env->cpus) { |
af218122 | 9831 | if (!idle_cpu(cpu)) |
23f0d209 JK |
9832 | continue; |
9833 | ||
64297f2b PW |
9834 | /* Are we the first idle CPU? */ |
9835 | return cpu == env->dst_cpu; | |
23f0d209 JK |
9836 | } |
9837 | ||
64297f2b PW |
9838 | /* Are we the first CPU of this group ? */ |
9839 | return group_balance_cpu(sg) == env->dst_cpu; | |
23f0d209 JK |
9840 | } |
9841 | ||
1e3c88bd PZ |
9842 | /* |
9843 | * Check this_cpu to ensure it is balanced within domain. Attempt to move | |
9844 | * tasks if there is an imbalance. | |
9845 | */ | |
9846 | static int load_balance(int this_cpu, struct rq *this_rq, | |
9847 | struct sched_domain *sd, enum cpu_idle_type idle, | |
23f0d209 | 9848 | int *continue_balancing) |
1e3c88bd | 9849 | { |
88b8dac0 | 9850 | int ld_moved, cur_ld_moved, active_balance = 0; |
6263322c | 9851 | struct sched_domain *sd_parent = sd->parent; |
1e3c88bd | 9852 | struct sched_group *group; |
1e3c88bd | 9853 | struct rq *busiest; |
8a8c69c3 | 9854 | struct rq_flags rf; |
4ba29684 | 9855 | struct cpumask *cpus = this_cpu_cpumask_var_ptr(load_balance_mask); |
1e3c88bd | 9856 | |
8e45cb54 PZ |
9857 | struct lb_env env = { |
9858 | .sd = sd, | |
ddcdf6e7 PZ |
9859 | .dst_cpu = this_cpu, |
9860 | .dst_rq = this_rq, | |
ae4df9d6 | 9861 | .dst_grpmask = sched_group_span(sd->groups), |
8e45cb54 | 9862 | .idle = idle, |
eb95308e | 9863 | .loop_break = sched_nr_migrate_break, |
b9403130 | 9864 | .cpus = cpus, |
0ec8aa00 | 9865 | .fbq_type = all, |
163122b7 | 9866 | .tasks = LIST_HEAD_INIT(env.tasks), |
8e45cb54 PZ |
9867 | }; |
9868 | ||
65a4433a | 9869 | cpumask_and(cpus, sched_domain_span(sd), cpu_active_mask); |
1e3c88bd | 9870 | |
ae92882e | 9871 | schedstat_inc(sd->lb_count[idle]); |
1e3c88bd PZ |
9872 | |
9873 | redo: | |
23f0d209 JK |
9874 | if (!should_we_balance(&env)) { |
9875 | *continue_balancing = 0; | |
1e3c88bd | 9876 | goto out_balanced; |
23f0d209 | 9877 | } |
1e3c88bd | 9878 | |
23f0d209 | 9879 | group = find_busiest_group(&env); |
1e3c88bd | 9880 | if (!group) { |
ae92882e | 9881 | schedstat_inc(sd->lb_nobusyg[idle]); |
1e3c88bd PZ |
9882 | goto out_balanced; |
9883 | } | |
9884 | ||
b9403130 | 9885 | busiest = find_busiest_queue(&env, group); |
1e3c88bd | 9886 | if (!busiest) { |
ae92882e | 9887 | schedstat_inc(sd->lb_nobusyq[idle]); |
1e3c88bd PZ |
9888 | goto out_balanced; |
9889 | } | |
9890 | ||
78feefc5 | 9891 | BUG_ON(busiest == env.dst_rq); |
1e3c88bd | 9892 | |
ae92882e | 9893 | schedstat_add(sd->lb_imbalance[idle], env.imbalance); |
1e3c88bd | 9894 | |
1aaf90a4 VG |
9895 | env.src_cpu = busiest->cpu; |
9896 | env.src_rq = busiest; | |
9897 | ||
1e3c88bd | 9898 | ld_moved = 0; |
8a41dfcd VG |
9899 | /* Clear this flag as soon as we find a pullable task */ |
9900 | env.flags |= LBF_ALL_PINNED; | |
1e3c88bd PZ |
9901 | if (busiest->nr_running > 1) { |
9902 | /* | |
9903 | * Attempt to move tasks. If find_busiest_group has found | |
9904 | * an imbalance but busiest->nr_running <= 1, the group is | |
9905 | * still unbalanced. ld_moved simply stays zero, so it is | |
9906 | * correctly treated as an imbalance. | |
9907 | */ | |
c82513e5 | 9908 | env.loop_max = min(sysctl_sched_nr_migrate, busiest->nr_running); |
8e45cb54 | 9909 | |
5d6523eb | 9910 | more_balance: |
8a8c69c3 | 9911 | rq_lock_irqsave(busiest, &rf); |
3bed5e21 | 9912 | update_rq_clock(busiest); |
88b8dac0 SV |
9913 | |
9914 | /* | |
9915 | * cur_ld_moved - load moved in current iteration | |
9916 | * ld_moved - cumulative load moved across iterations | |
9917 | */ | |
163122b7 | 9918 | cur_ld_moved = detach_tasks(&env); |
1e3c88bd PZ |
9919 | |
9920 | /* | |
163122b7 KT |
9921 | * We've detached some tasks from busiest_rq. Every |
9922 | * task is masked "TASK_ON_RQ_MIGRATING", so we can safely | |
9923 | * unlock busiest->lock, and we are able to be sure | |
9924 | * that nobody can manipulate the tasks in parallel. | |
9925 | * See task_rq_lock() family for the details. | |
1e3c88bd | 9926 | */ |
163122b7 | 9927 | |
8a8c69c3 | 9928 | rq_unlock(busiest, &rf); |
163122b7 KT |
9929 | |
9930 | if (cur_ld_moved) { | |
9931 | attach_tasks(&env); | |
9932 | ld_moved += cur_ld_moved; | |
9933 | } | |
9934 | ||
8a8c69c3 | 9935 | local_irq_restore(rf.flags); |
88b8dac0 | 9936 | |
f1cd0858 JK |
9937 | if (env.flags & LBF_NEED_BREAK) { |
9938 | env.flags &= ~LBF_NEED_BREAK; | |
9939 | goto more_balance; | |
9940 | } | |
9941 | ||
88b8dac0 SV |
9942 | /* |
9943 | * Revisit (affine) tasks on src_cpu that couldn't be moved to | |
9944 | * us and move them to an alternate dst_cpu in our sched_group | |
9945 | * where they can run. The upper limit on how many times we | |
97fb7a0a | 9946 | * iterate on same src_cpu is dependent on number of CPUs in our |
88b8dac0 SV |
9947 | * sched_group. |
9948 | * | |
9949 | * This changes load balance semantics a bit on who can move | |
9950 | * load to a given_cpu. In addition to the given_cpu itself | |
9951 | * (or a ilb_cpu acting on its behalf where given_cpu is | |
9952 | * nohz-idle), we now have balance_cpu in a position to move | |
9953 | * load to given_cpu. In rare situations, this may cause | |
9954 | * conflicts (balance_cpu and given_cpu/ilb_cpu deciding | |
9955 | * _independently_ and at _same_ time to move some load to | |
3b03706f | 9956 | * given_cpu) causing excess load to be moved to given_cpu. |
88b8dac0 SV |
9957 | * This however should not happen so much in practice and |
9958 | * moreover subsequent load balance cycles should correct the | |
9959 | * excess load moved. | |
9960 | */ | |
6263322c | 9961 | if ((env.flags & LBF_DST_PINNED) && env.imbalance > 0) { |
88b8dac0 | 9962 | |
97fb7a0a | 9963 | /* Prevent to re-select dst_cpu via env's CPUs */ |
c89d92ed | 9964 | __cpumask_clear_cpu(env.dst_cpu, env.cpus); |
7aff2e3a | 9965 | |
78feefc5 | 9966 | env.dst_rq = cpu_rq(env.new_dst_cpu); |
88b8dac0 | 9967 | env.dst_cpu = env.new_dst_cpu; |
6263322c | 9968 | env.flags &= ~LBF_DST_PINNED; |
88b8dac0 SV |
9969 | env.loop = 0; |
9970 | env.loop_break = sched_nr_migrate_break; | |
e02e60c1 | 9971 | |
88b8dac0 SV |
9972 | /* |
9973 | * Go back to "more_balance" rather than "redo" since we | |
9974 | * need to continue with same src_cpu. | |
9975 | */ | |
9976 | goto more_balance; | |
9977 | } | |
1e3c88bd | 9978 | |
6263322c PZ |
9979 | /* |
9980 | * We failed to reach balance because of affinity. | |
9981 | */ | |
9982 | if (sd_parent) { | |
63b2ca30 | 9983 | int *group_imbalance = &sd_parent->groups->sgc->imbalance; |
6263322c | 9984 | |
afdeee05 | 9985 | if ((env.flags & LBF_SOME_PINNED) && env.imbalance > 0) |
6263322c | 9986 | *group_imbalance = 1; |
6263322c PZ |
9987 | } |
9988 | ||
1e3c88bd | 9989 | /* All tasks on this runqueue were pinned by CPU affinity */ |
8e45cb54 | 9990 | if (unlikely(env.flags & LBF_ALL_PINNED)) { |
c89d92ed | 9991 | __cpumask_clear_cpu(cpu_of(busiest), cpus); |
65a4433a JH |
9992 | /* |
9993 | * Attempting to continue load balancing at the current | |
9994 | * sched_domain level only makes sense if there are | |
9995 | * active CPUs remaining as possible busiest CPUs to | |
9996 | * pull load from which are not contained within the | |
9997 | * destination group that is receiving any migrated | |
9998 | * load. | |
9999 | */ | |
10000 | if (!cpumask_subset(cpus, env.dst_grpmask)) { | |
bbf18b19 PN |
10001 | env.loop = 0; |
10002 | env.loop_break = sched_nr_migrate_break; | |
1e3c88bd | 10003 | goto redo; |
bbf18b19 | 10004 | } |
afdeee05 | 10005 | goto out_all_pinned; |
1e3c88bd PZ |
10006 | } |
10007 | } | |
10008 | ||
10009 | if (!ld_moved) { | |
ae92882e | 10010 | schedstat_inc(sd->lb_failed[idle]); |
58b26c4c VP |
10011 | /* |
10012 | * Increment the failure counter only on periodic balance. | |
10013 | * We do not want newidle balance, which can be very | |
10014 | * frequent, pollute the failure counter causing | |
10015 | * excessive cache_hot migrations and active balances. | |
10016 | */ | |
10017 | if (idle != CPU_NEWLY_IDLE) | |
10018 | sd->nr_balance_failed++; | |
1e3c88bd | 10019 | |
bd939f45 | 10020 | if (need_active_balance(&env)) { |
8a8c69c3 PZ |
10021 | unsigned long flags; |
10022 | ||
5cb9eaa3 | 10023 | raw_spin_rq_lock_irqsave(busiest, flags); |
1e3c88bd | 10024 | |
97fb7a0a IM |
10025 | /* |
10026 | * Don't kick the active_load_balance_cpu_stop, | |
10027 | * if the curr task on busiest CPU can't be | |
10028 | * moved to this_cpu: | |
1e3c88bd | 10029 | */ |
3bd37062 | 10030 | if (!cpumask_test_cpu(this_cpu, busiest->curr->cpus_ptr)) { |
5cb9eaa3 | 10031 | raw_spin_rq_unlock_irqrestore(busiest, flags); |
1e3c88bd PZ |
10032 | goto out_one_pinned; |
10033 | } | |
10034 | ||
8a41dfcd VG |
10035 | /* Record that we found at least one task that could run on this_cpu */ |
10036 | env.flags &= ~LBF_ALL_PINNED; | |
10037 | ||
969c7921 TH |
10038 | /* |
10039 | * ->active_balance synchronizes accesses to | |
10040 | * ->active_balance_work. Once set, it's cleared | |
10041 | * only after active load balance is finished. | |
10042 | */ | |
1e3c88bd PZ |
10043 | if (!busiest->active_balance) { |
10044 | busiest->active_balance = 1; | |
10045 | busiest->push_cpu = this_cpu; | |
10046 | active_balance = 1; | |
10047 | } | |
5cb9eaa3 | 10048 | raw_spin_rq_unlock_irqrestore(busiest, flags); |
969c7921 | 10049 | |
bd939f45 | 10050 | if (active_balance) { |
969c7921 TH |
10051 | stop_one_cpu_nowait(cpu_of(busiest), |
10052 | active_load_balance_cpu_stop, busiest, | |
10053 | &busiest->active_balance_work); | |
bd939f45 | 10054 | } |
1e3c88bd | 10055 | } |
e9b9734b | 10056 | } else { |
1e3c88bd | 10057 | sd->nr_balance_failed = 0; |
e9b9734b | 10058 | } |
1e3c88bd | 10059 | |
e9b9734b | 10060 | if (likely(!active_balance) || need_active_balance(&env)) { |
1e3c88bd PZ |
10061 | /* We were unbalanced, so reset the balancing interval */ |
10062 | sd->balance_interval = sd->min_interval; | |
1e3c88bd PZ |
10063 | } |
10064 | ||
1e3c88bd PZ |
10065 | goto out; |
10066 | ||
10067 | out_balanced: | |
afdeee05 VG |
10068 | /* |
10069 | * We reach balance although we may have faced some affinity | |
f6cad8df VG |
10070 | * constraints. Clear the imbalance flag only if other tasks got |
10071 | * a chance to move and fix the imbalance. | |
afdeee05 | 10072 | */ |
f6cad8df | 10073 | if (sd_parent && !(env.flags & LBF_ALL_PINNED)) { |
afdeee05 VG |
10074 | int *group_imbalance = &sd_parent->groups->sgc->imbalance; |
10075 | ||
10076 | if (*group_imbalance) | |
10077 | *group_imbalance = 0; | |
10078 | } | |
10079 | ||
10080 | out_all_pinned: | |
10081 | /* | |
10082 | * We reach balance because all tasks are pinned at this level so | |
10083 | * we can't migrate them. Let the imbalance flag set so parent level | |
10084 | * can try to migrate them. | |
10085 | */ | |
ae92882e | 10086 | schedstat_inc(sd->lb_balanced[idle]); |
1e3c88bd PZ |
10087 | |
10088 | sd->nr_balance_failed = 0; | |
10089 | ||
10090 | out_one_pinned: | |
3f130a37 VS |
10091 | ld_moved = 0; |
10092 | ||
10093 | /* | |
5ba553ef PZ |
10094 | * newidle_balance() disregards balance intervals, so we could |
10095 | * repeatedly reach this code, which would lead to balance_interval | |
3b03706f | 10096 | * skyrocketing in a short amount of time. Skip the balance_interval |
5ba553ef | 10097 | * increase logic to avoid that. |
3f130a37 VS |
10098 | */ |
10099 | if (env.idle == CPU_NEWLY_IDLE) | |
10100 | goto out; | |
10101 | ||
1e3c88bd | 10102 | /* tune up the balancing interval */ |
47b7aee1 VS |
10103 | if ((env.flags & LBF_ALL_PINNED && |
10104 | sd->balance_interval < MAX_PINNED_INTERVAL) || | |
10105 | sd->balance_interval < sd->max_interval) | |
1e3c88bd | 10106 | sd->balance_interval *= 2; |
1e3c88bd | 10107 | out: |
1e3c88bd PZ |
10108 | return ld_moved; |
10109 | } | |
10110 | ||
52a08ef1 JL |
10111 | static inline unsigned long |
10112 | get_sd_balance_interval(struct sched_domain *sd, int cpu_busy) | |
10113 | { | |
10114 | unsigned long interval = sd->balance_interval; | |
10115 | ||
10116 | if (cpu_busy) | |
10117 | interval *= sd->busy_factor; | |
10118 | ||
10119 | /* scale ms to jiffies */ | |
10120 | interval = msecs_to_jiffies(interval); | |
e4d32e4d VG |
10121 | |
10122 | /* | |
10123 | * Reduce likelihood of busy balancing at higher domains racing with | |
10124 | * balancing at lower domains by preventing their balancing periods | |
10125 | * from being multiples of each other. | |
10126 | */ | |
10127 | if (cpu_busy) | |
10128 | interval -= 1; | |
10129 | ||
52a08ef1 JL |
10130 | interval = clamp(interval, 1UL, max_load_balance_interval); |
10131 | ||
10132 | return interval; | |
10133 | } | |
10134 | ||
10135 | static inline void | |
31851a98 | 10136 | update_next_balance(struct sched_domain *sd, unsigned long *next_balance) |
52a08ef1 JL |
10137 | { |
10138 | unsigned long interval, next; | |
10139 | ||
31851a98 LY |
10140 | /* used by idle balance, so cpu_busy = 0 */ |
10141 | interval = get_sd_balance_interval(sd, 0); | |
52a08ef1 JL |
10142 | next = sd->last_balance + interval; |
10143 | ||
10144 | if (time_after(*next_balance, next)) | |
10145 | *next_balance = next; | |
10146 | } | |
10147 | ||
1e3c88bd | 10148 | /* |
97fb7a0a | 10149 | * active_load_balance_cpu_stop is run by the CPU stopper. It pushes |
969c7921 TH |
10150 | * running tasks off the busiest CPU onto idle CPUs. It requires at |
10151 | * least 1 task to be running on each physical CPU where possible, and | |
10152 | * avoids physical / logical imbalances. | |
1e3c88bd | 10153 | */ |
969c7921 | 10154 | static int active_load_balance_cpu_stop(void *data) |
1e3c88bd | 10155 | { |
969c7921 TH |
10156 | struct rq *busiest_rq = data; |
10157 | int busiest_cpu = cpu_of(busiest_rq); | |
1e3c88bd | 10158 | int target_cpu = busiest_rq->push_cpu; |
969c7921 | 10159 | struct rq *target_rq = cpu_rq(target_cpu); |
1e3c88bd | 10160 | struct sched_domain *sd; |
e5673f28 | 10161 | struct task_struct *p = NULL; |
8a8c69c3 | 10162 | struct rq_flags rf; |
969c7921 | 10163 | |
8a8c69c3 | 10164 | rq_lock_irq(busiest_rq, &rf); |
edd8e41d PZ |
10165 | /* |
10166 | * Between queueing the stop-work and running it is a hole in which | |
10167 | * CPUs can become inactive. We should not move tasks from or to | |
10168 | * inactive CPUs. | |
10169 | */ | |
10170 | if (!cpu_active(busiest_cpu) || !cpu_active(target_cpu)) | |
10171 | goto out_unlock; | |
969c7921 | 10172 | |
97fb7a0a | 10173 | /* Make sure the requested CPU hasn't gone down in the meantime: */ |
969c7921 TH |
10174 | if (unlikely(busiest_cpu != smp_processor_id() || |
10175 | !busiest_rq->active_balance)) | |
10176 | goto out_unlock; | |
1e3c88bd PZ |
10177 | |
10178 | /* Is there any task to move? */ | |
10179 | if (busiest_rq->nr_running <= 1) | |
969c7921 | 10180 | goto out_unlock; |
1e3c88bd PZ |
10181 | |
10182 | /* | |
10183 | * This condition is "impossible", if it occurs | |
10184 | * we need to fix it. Originally reported by | |
97fb7a0a | 10185 | * Bjorn Helgaas on a 128-CPU setup. |
1e3c88bd PZ |
10186 | */ |
10187 | BUG_ON(busiest_rq == target_rq); | |
10188 | ||
1e3c88bd | 10189 | /* Search for an sd spanning us and the target CPU. */ |
dce840a0 | 10190 | rcu_read_lock(); |
1e3c88bd | 10191 | for_each_domain(target_cpu, sd) { |
e669ac8a VS |
10192 | if (cpumask_test_cpu(busiest_cpu, sched_domain_span(sd))) |
10193 | break; | |
1e3c88bd PZ |
10194 | } |
10195 | ||
10196 | if (likely(sd)) { | |
8e45cb54 PZ |
10197 | struct lb_env env = { |
10198 | .sd = sd, | |
ddcdf6e7 PZ |
10199 | .dst_cpu = target_cpu, |
10200 | .dst_rq = target_rq, | |
10201 | .src_cpu = busiest_rq->cpu, | |
10202 | .src_rq = busiest_rq, | |
8e45cb54 | 10203 | .idle = CPU_IDLE, |
23fb06d9 | 10204 | .flags = LBF_ACTIVE_LB, |
8e45cb54 PZ |
10205 | }; |
10206 | ||
ae92882e | 10207 | schedstat_inc(sd->alb_count); |
3bed5e21 | 10208 | update_rq_clock(busiest_rq); |
1e3c88bd | 10209 | |
e5673f28 | 10210 | p = detach_one_task(&env); |
d02c0711 | 10211 | if (p) { |
ae92882e | 10212 | schedstat_inc(sd->alb_pushed); |
d02c0711 SD |
10213 | /* Active balancing done, reset the failure counter. */ |
10214 | sd->nr_balance_failed = 0; | |
10215 | } else { | |
ae92882e | 10216 | schedstat_inc(sd->alb_failed); |
d02c0711 | 10217 | } |
1e3c88bd | 10218 | } |
dce840a0 | 10219 | rcu_read_unlock(); |
969c7921 TH |
10220 | out_unlock: |
10221 | busiest_rq->active_balance = 0; | |
8a8c69c3 | 10222 | rq_unlock(busiest_rq, &rf); |
e5673f28 KT |
10223 | |
10224 | if (p) | |
10225 | attach_one_task(target_rq, p); | |
10226 | ||
10227 | local_irq_enable(); | |
10228 | ||
969c7921 | 10229 | return 0; |
1e3c88bd PZ |
10230 | } |
10231 | ||
af3fe03c PZ |
10232 | static DEFINE_SPINLOCK(balancing); |
10233 | ||
10234 | /* | |
10235 | * Scale the max load_balance interval with the number of CPUs in the system. | |
10236 | * This trades load-balance latency on larger machines for less cross talk. | |
10237 | */ | |
10238 | void update_max_interval(void) | |
10239 | { | |
10240 | max_load_balance_interval = HZ*num_online_cpus()/10; | |
10241 | } | |
10242 | ||
10243 | /* | |
10244 | * It checks each scheduling domain to see if it is due to be balanced, | |
10245 | * and initiates a balancing operation if so. | |
10246 | * | |
10247 | * Balancing parameters are set up in init_sched_domains. | |
10248 | */ | |
10249 | static void rebalance_domains(struct rq *rq, enum cpu_idle_type idle) | |
10250 | { | |
10251 | int continue_balancing = 1; | |
10252 | int cpu = rq->cpu; | |
323af6de | 10253 | int busy = idle != CPU_IDLE && !sched_idle_cpu(cpu); |
af3fe03c PZ |
10254 | unsigned long interval; |
10255 | struct sched_domain *sd; | |
10256 | /* Earliest time when we have to do rebalance again */ | |
10257 | unsigned long next_balance = jiffies + 60*HZ; | |
10258 | int update_next_balance = 0; | |
10259 | int need_serialize, need_decay = 0; | |
10260 | u64 max_cost = 0; | |
10261 | ||
10262 | rcu_read_lock(); | |
10263 | for_each_domain(cpu, sd) { | |
10264 | /* | |
10265 | * Decay the newidle max times here because this is a regular | |
10266 | * visit to all the domains. Decay ~1% per second. | |
10267 | */ | |
10268 | if (time_after(jiffies, sd->next_decay_max_lb_cost)) { | |
10269 | sd->max_newidle_lb_cost = | |
10270 | (sd->max_newidle_lb_cost * 253) / 256; | |
10271 | sd->next_decay_max_lb_cost = jiffies + HZ; | |
10272 | need_decay = 1; | |
10273 | } | |
10274 | max_cost += sd->max_newidle_lb_cost; | |
10275 | ||
af3fe03c PZ |
10276 | /* |
10277 | * Stop the load balance at this level. There is another | |
10278 | * CPU in our sched group which is doing load balancing more | |
10279 | * actively. | |
10280 | */ | |
10281 | if (!continue_balancing) { | |
10282 | if (need_decay) | |
10283 | continue; | |
10284 | break; | |
10285 | } | |
10286 | ||
323af6de | 10287 | interval = get_sd_balance_interval(sd, busy); |
af3fe03c PZ |
10288 | |
10289 | need_serialize = sd->flags & SD_SERIALIZE; | |
10290 | if (need_serialize) { | |
10291 | if (!spin_trylock(&balancing)) | |
10292 | goto out; | |
10293 | } | |
10294 | ||
10295 | if (time_after_eq(jiffies, sd->last_balance + interval)) { | |
10296 | if (load_balance(cpu, rq, sd, idle, &continue_balancing)) { | |
10297 | /* | |
10298 | * The LBF_DST_PINNED logic could have changed | |
10299 | * env->dst_cpu, so we can't know our idle | |
10300 | * state even if we migrated tasks. Update it. | |
10301 | */ | |
10302 | idle = idle_cpu(cpu) ? CPU_IDLE : CPU_NOT_IDLE; | |
323af6de | 10303 | busy = idle != CPU_IDLE && !sched_idle_cpu(cpu); |
af3fe03c PZ |
10304 | } |
10305 | sd->last_balance = jiffies; | |
323af6de | 10306 | interval = get_sd_balance_interval(sd, busy); |
af3fe03c PZ |
10307 | } |
10308 | if (need_serialize) | |
10309 | spin_unlock(&balancing); | |
10310 | out: | |
10311 | if (time_after(next_balance, sd->last_balance + interval)) { | |
10312 | next_balance = sd->last_balance + interval; | |
10313 | update_next_balance = 1; | |
10314 | } | |
10315 | } | |
10316 | if (need_decay) { | |
10317 | /* | |
10318 | * Ensure the rq-wide value also decays but keep it at a | |
10319 | * reasonable floor to avoid funnies with rq->avg_idle. | |
10320 | */ | |
10321 | rq->max_idle_balance_cost = | |
10322 | max((u64)sysctl_sched_migration_cost, max_cost); | |
10323 | } | |
10324 | rcu_read_unlock(); | |
10325 | ||
10326 | /* | |
10327 | * next_balance will be updated only when there is a need. | |
10328 | * When the cpu is attached to null domain for ex, it will not be | |
10329 | * updated. | |
10330 | */ | |
7a82e5f5 | 10331 | if (likely(update_next_balance)) |
af3fe03c PZ |
10332 | rq->next_balance = next_balance; |
10333 | ||
af3fe03c PZ |
10334 | } |
10335 | ||
d987fc7f MG |
10336 | static inline int on_null_domain(struct rq *rq) |
10337 | { | |
10338 | return unlikely(!rcu_dereference_sched(rq->sd)); | |
10339 | } | |
10340 | ||
3451d024 | 10341 | #ifdef CONFIG_NO_HZ_COMMON |
83cd4fe2 VP |
10342 | /* |
10343 | * idle load balancing details | |
83cd4fe2 VP |
10344 | * - When one of the busy CPUs notice that there may be an idle rebalancing |
10345 | * needed, they will kick the idle load balancer, which then does idle | |
10346 | * load balancing for all the idle CPUs. | |
9b019acb NP |
10347 | * - HK_FLAG_MISC CPUs are used for this task, because HK_FLAG_SCHED not set |
10348 | * anywhere yet. | |
83cd4fe2 | 10349 | */ |
1e3c88bd | 10350 | |
3dd0337d | 10351 | static inline int find_new_ilb(void) |
1e3c88bd | 10352 | { |
9b019acb | 10353 | int ilb; |
031e3bd8 | 10354 | const struct cpumask *hk_mask; |
1e3c88bd | 10355 | |
031e3bd8 | 10356 | hk_mask = housekeeping_cpumask(HK_FLAG_MISC); |
1e3c88bd | 10357 | |
031e3bd8 | 10358 | for_each_cpu_and(ilb, nohz.idle_cpus_mask, hk_mask) { |
45da7a2b PZ |
10359 | |
10360 | if (ilb == smp_processor_id()) | |
10361 | continue; | |
10362 | ||
9b019acb NP |
10363 | if (idle_cpu(ilb)) |
10364 | return ilb; | |
10365 | } | |
786d6dc7 SS |
10366 | |
10367 | return nr_cpu_ids; | |
1e3c88bd | 10368 | } |
1e3c88bd | 10369 | |
83cd4fe2 | 10370 | /* |
9b019acb NP |
10371 | * Kick a CPU to do the nohz balancing, if it is time for it. We pick any |
10372 | * idle CPU in the HK_FLAG_MISC housekeeping set (if there is one). | |
83cd4fe2 | 10373 | */ |
a4064fb6 | 10374 | static void kick_ilb(unsigned int flags) |
83cd4fe2 VP |
10375 | { |
10376 | int ilb_cpu; | |
10377 | ||
3ea2f097 VG |
10378 | /* |
10379 | * Increase nohz.next_balance only when if full ilb is triggered but | |
10380 | * not if we only update stats. | |
10381 | */ | |
10382 | if (flags & NOHZ_BALANCE_KICK) | |
10383 | nohz.next_balance = jiffies+1; | |
83cd4fe2 | 10384 | |
3dd0337d | 10385 | ilb_cpu = find_new_ilb(); |
83cd4fe2 | 10386 | |
0b005cf5 SS |
10387 | if (ilb_cpu >= nr_cpu_ids) |
10388 | return; | |
83cd4fe2 | 10389 | |
19a1f5ec PZ |
10390 | /* |
10391 | * Access to rq::nohz_csd is serialized by NOHZ_KICK_MASK; he who sets | |
10392 | * the first flag owns it; cleared by nohz_csd_func(). | |
10393 | */ | |
a4064fb6 | 10394 | flags = atomic_fetch_or(flags, nohz_flags(ilb_cpu)); |
b7031a02 | 10395 | if (flags & NOHZ_KICK_MASK) |
1c792db7 | 10396 | return; |
4550487a | 10397 | |
1c792db7 | 10398 | /* |
90b5363a | 10399 | * This way we generate an IPI on the target CPU which |
1c792db7 SS |
10400 | * is idle. And the softirq performing nohz idle load balance |
10401 | * will be run before returning from the IPI. | |
10402 | */ | |
90b5363a | 10403 | smp_call_function_single_async(ilb_cpu, &cpu_rq(ilb_cpu)->nohz_csd); |
4550487a PZ |
10404 | } |
10405 | ||
10406 | /* | |
9f132742 VS |
10407 | * Current decision point for kicking the idle load balancer in the presence |
10408 | * of idle CPUs in the system. | |
4550487a PZ |
10409 | */ |
10410 | static void nohz_balancer_kick(struct rq *rq) | |
10411 | { | |
10412 | unsigned long now = jiffies; | |
10413 | struct sched_domain_shared *sds; | |
10414 | struct sched_domain *sd; | |
10415 | int nr_busy, i, cpu = rq->cpu; | |
a4064fb6 | 10416 | unsigned int flags = 0; |
4550487a PZ |
10417 | |
10418 | if (unlikely(rq->idle_balance)) | |
10419 | return; | |
10420 | ||
10421 | /* | |
10422 | * We may be recently in ticked or tickless idle mode. At the first | |
10423 | * busy tick after returning from idle, we will update the busy stats. | |
10424 | */ | |
00357f5e | 10425 | nohz_balance_exit_idle(rq); |
4550487a PZ |
10426 | |
10427 | /* | |
10428 | * None are in tickless mode and hence no need for NOHZ idle load | |
10429 | * balancing. | |
10430 | */ | |
10431 | if (likely(!atomic_read(&nohz.nr_cpus))) | |
10432 | return; | |
10433 | ||
f643ea22 VG |
10434 | if (READ_ONCE(nohz.has_blocked) && |
10435 | time_after(now, READ_ONCE(nohz.next_blocked))) | |
a4064fb6 PZ |
10436 | flags = NOHZ_STATS_KICK; |
10437 | ||
4550487a | 10438 | if (time_before(now, nohz.next_balance)) |
a4064fb6 | 10439 | goto out; |
4550487a | 10440 | |
a0fe2cf0 | 10441 | if (rq->nr_running >= 2) { |
efd984c4 | 10442 | flags = NOHZ_STATS_KICK | NOHZ_BALANCE_KICK; |
4550487a PZ |
10443 | goto out; |
10444 | } | |
10445 | ||
10446 | rcu_read_lock(); | |
4550487a PZ |
10447 | |
10448 | sd = rcu_dereference(rq->sd); | |
10449 | if (sd) { | |
e25a7a94 VS |
10450 | /* |
10451 | * If there's a CFS task and the current CPU has reduced | |
10452 | * capacity; kick the ILB to see if there's a better CPU to run | |
10453 | * on. | |
10454 | */ | |
10455 | if (rq->cfs.h_nr_running >= 1 && check_cpu_capacity(rq, sd)) { | |
efd984c4 | 10456 | flags = NOHZ_STATS_KICK | NOHZ_BALANCE_KICK; |
4550487a PZ |
10457 | goto unlock; |
10458 | } | |
10459 | } | |
10460 | ||
011b27bb | 10461 | sd = rcu_dereference(per_cpu(sd_asym_packing, cpu)); |
4550487a | 10462 | if (sd) { |
b9a7b883 VS |
10463 | /* |
10464 | * When ASYM_PACKING; see if there's a more preferred CPU | |
10465 | * currently idle; in which case, kick the ILB to move tasks | |
10466 | * around. | |
10467 | */ | |
7edab78d | 10468 | for_each_cpu_and(i, sched_domain_span(sd), nohz.idle_cpus_mask) { |
4550487a | 10469 | if (sched_asym_prefer(i, cpu)) { |
efd984c4 | 10470 | flags = NOHZ_STATS_KICK | NOHZ_BALANCE_KICK; |
4550487a PZ |
10471 | goto unlock; |
10472 | } | |
10473 | } | |
10474 | } | |
b9a7b883 | 10475 | |
a0fe2cf0 VS |
10476 | sd = rcu_dereference(per_cpu(sd_asym_cpucapacity, cpu)); |
10477 | if (sd) { | |
10478 | /* | |
10479 | * When ASYM_CPUCAPACITY; see if there's a higher capacity CPU | |
10480 | * to run the misfit task on. | |
10481 | */ | |
10482 | if (check_misfit_status(rq, sd)) { | |
efd984c4 | 10483 | flags = NOHZ_STATS_KICK | NOHZ_BALANCE_KICK; |
a0fe2cf0 VS |
10484 | goto unlock; |
10485 | } | |
b9a7b883 VS |
10486 | |
10487 | /* | |
10488 | * For asymmetric systems, we do not want to nicely balance | |
10489 | * cache use, instead we want to embrace asymmetry and only | |
10490 | * ensure tasks have enough CPU capacity. | |
10491 | * | |
10492 | * Skip the LLC logic because it's not relevant in that case. | |
10493 | */ | |
10494 | goto unlock; | |
a0fe2cf0 VS |
10495 | } |
10496 | ||
b9a7b883 VS |
10497 | sds = rcu_dereference(per_cpu(sd_llc_shared, cpu)); |
10498 | if (sds) { | |
e25a7a94 | 10499 | /* |
b9a7b883 VS |
10500 | * If there is an imbalance between LLC domains (IOW we could |
10501 | * increase the overall cache use), we need some less-loaded LLC | |
10502 | * domain to pull some load. Likewise, we may need to spread | |
10503 | * load within the current LLC domain (e.g. packed SMT cores but | |
10504 | * other CPUs are idle). We can't really know from here how busy | |
10505 | * the others are - so just get a nohz balance going if it looks | |
10506 | * like this LLC domain has tasks we could move. | |
e25a7a94 | 10507 | */ |
b9a7b883 VS |
10508 | nr_busy = atomic_read(&sds->nr_busy_cpus); |
10509 | if (nr_busy > 1) { | |
efd984c4 | 10510 | flags = NOHZ_STATS_KICK | NOHZ_BALANCE_KICK; |
b9a7b883 | 10511 | goto unlock; |
4550487a PZ |
10512 | } |
10513 | } | |
10514 | unlock: | |
10515 | rcu_read_unlock(); | |
10516 | out: | |
7fd7a9e0 VS |
10517 | if (READ_ONCE(nohz.needs_update)) |
10518 | flags |= NOHZ_NEXT_KICK; | |
10519 | ||
a4064fb6 PZ |
10520 | if (flags) |
10521 | kick_ilb(flags); | |
83cd4fe2 VP |
10522 | } |
10523 | ||
00357f5e | 10524 | static void set_cpu_sd_state_busy(int cpu) |
71325960 | 10525 | { |
00357f5e | 10526 | struct sched_domain *sd; |
a22e47a4 | 10527 | |
00357f5e PZ |
10528 | rcu_read_lock(); |
10529 | sd = rcu_dereference(per_cpu(sd_llc, cpu)); | |
a22e47a4 | 10530 | |
00357f5e PZ |
10531 | if (!sd || !sd->nohz_idle) |
10532 | goto unlock; | |
10533 | sd->nohz_idle = 0; | |
10534 | ||
10535 | atomic_inc(&sd->shared->nr_busy_cpus); | |
10536 | unlock: | |
10537 | rcu_read_unlock(); | |
71325960 SS |
10538 | } |
10539 | ||
00357f5e PZ |
10540 | void nohz_balance_exit_idle(struct rq *rq) |
10541 | { | |
10542 | SCHED_WARN_ON(rq != this_rq()); | |
10543 | ||
10544 | if (likely(!rq->nohz_tick_stopped)) | |
10545 | return; | |
10546 | ||
10547 | rq->nohz_tick_stopped = 0; | |
10548 | cpumask_clear_cpu(rq->cpu, nohz.idle_cpus_mask); | |
10549 | atomic_dec(&nohz.nr_cpus); | |
10550 | ||
10551 | set_cpu_sd_state_busy(rq->cpu); | |
10552 | } | |
10553 | ||
10554 | static void set_cpu_sd_state_idle(int cpu) | |
69e1e811 SS |
10555 | { |
10556 | struct sched_domain *sd; | |
69e1e811 | 10557 | |
69e1e811 | 10558 | rcu_read_lock(); |
0e369d75 | 10559 | sd = rcu_dereference(per_cpu(sd_llc, cpu)); |
25f55d9d VG |
10560 | |
10561 | if (!sd || sd->nohz_idle) | |
10562 | goto unlock; | |
10563 | sd->nohz_idle = 1; | |
10564 | ||
0e369d75 | 10565 | atomic_dec(&sd->shared->nr_busy_cpus); |
25f55d9d | 10566 | unlock: |
69e1e811 SS |
10567 | rcu_read_unlock(); |
10568 | } | |
10569 | ||
1e3c88bd | 10570 | /* |
97fb7a0a | 10571 | * This routine will record that the CPU is going idle with tick stopped. |
0b005cf5 | 10572 | * This info will be used in performing idle load balancing in the future. |
1e3c88bd | 10573 | */ |
c1cc017c | 10574 | void nohz_balance_enter_idle(int cpu) |
1e3c88bd | 10575 | { |
00357f5e PZ |
10576 | struct rq *rq = cpu_rq(cpu); |
10577 | ||
10578 | SCHED_WARN_ON(cpu != smp_processor_id()); | |
10579 | ||
97fb7a0a | 10580 | /* If this CPU is going down, then nothing needs to be done: */ |
71325960 SS |
10581 | if (!cpu_active(cpu)) |
10582 | return; | |
10583 | ||
387bc8b5 | 10584 | /* Spare idle load balancing on CPUs that don't want to be disturbed: */ |
de201559 | 10585 | if (!housekeeping_cpu(cpu, HK_FLAG_SCHED)) |
387bc8b5 FW |
10586 | return; |
10587 | ||
f643ea22 VG |
10588 | /* |
10589 | * Can be set safely without rq->lock held | |
10590 | * If a clear happens, it will have evaluated last additions because | |
10591 | * rq->lock is held during the check and the clear | |
10592 | */ | |
10593 | rq->has_blocked_load = 1; | |
10594 | ||
10595 | /* | |
10596 | * The tick is still stopped but load could have been added in the | |
10597 | * meantime. We set the nohz.has_blocked flag to trig a check of the | |
10598 | * *_avg. The CPU is already part of nohz.idle_cpus_mask so the clear | |
10599 | * of nohz.has_blocked can only happen after checking the new load | |
10600 | */ | |
00357f5e | 10601 | if (rq->nohz_tick_stopped) |
f643ea22 | 10602 | goto out; |
1e3c88bd | 10603 | |
97fb7a0a | 10604 | /* If we're a completely isolated CPU, we don't play: */ |
00357f5e | 10605 | if (on_null_domain(rq)) |
d987fc7f MG |
10606 | return; |
10607 | ||
00357f5e PZ |
10608 | rq->nohz_tick_stopped = 1; |
10609 | ||
c1cc017c AS |
10610 | cpumask_set_cpu(cpu, nohz.idle_cpus_mask); |
10611 | atomic_inc(&nohz.nr_cpus); | |
00357f5e | 10612 | |
f643ea22 VG |
10613 | /* |
10614 | * Ensures that if nohz_idle_balance() fails to observe our | |
10615 | * @idle_cpus_mask store, it must observe the @has_blocked | |
7fd7a9e0 | 10616 | * and @needs_update stores. |
f643ea22 VG |
10617 | */ |
10618 | smp_mb__after_atomic(); | |
10619 | ||
00357f5e | 10620 | set_cpu_sd_state_idle(cpu); |
f643ea22 | 10621 | |
7fd7a9e0 | 10622 | WRITE_ONCE(nohz.needs_update, 1); |
f643ea22 VG |
10623 | out: |
10624 | /* | |
10625 | * Each time a cpu enter idle, we assume that it has blocked load and | |
10626 | * enable the periodic update of the load of idle cpus | |
10627 | */ | |
10628 | WRITE_ONCE(nohz.has_blocked, 1); | |
1e3c88bd | 10629 | } |
1e3c88bd | 10630 | |
3f5ad914 Y |
10631 | static bool update_nohz_stats(struct rq *rq) |
10632 | { | |
10633 | unsigned int cpu = rq->cpu; | |
10634 | ||
10635 | if (!rq->has_blocked_load) | |
10636 | return false; | |
10637 | ||
10638 | if (!cpumask_test_cpu(cpu, nohz.idle_cpus_mask)) | |
10639 | return false; | |
10640 | ||
10641 | if (!time_after(jiffies, READ_ONCE(rq->last_blocked_load_update_tick))) | |
10642 | return true; | |
10643 | ||
10644 | update_blocked_averages(cpu); | |
10645 | ||
10646 | return rq->has_blocked_load; | |
10647 | } | |
10648 | ||
1e3c88bd | 10649 | /* |
31e77c93 VG |
10650 | * Internal function that runs load balance for all idle cpus. The load balance |
10651 | * can be a simple update of blocked load or a complete load balance with | |
10652 | * tasks movement depending of flags. | |
1e3c88bd | 10653 | */ |
ab2dde5e | 10654 | static void _nohz_idle_balance(struct rq *this_rq, unsigned int flags, |
31e77c93 | 10655 | enum cpu_idle_type idle) |
83cd4fe2 | 10656 | { |
c5afb6a8 | 10657 | /* Earliest time when we have to do rebalance again */ |
a4064fb6 PZ |
10658 | unsigned long now = jiffies; |
10659 | unsigned long next_balance = now + 60*HZ; | |
f643ea22 | 10660 | bool has_blocked_load = false; |
c5afb6a8 | 10661 | int update_next_balance = 0; |
b7031a02 | 10662 | int this_cpu = this_rq->cpu; |
b7031a02 PZ |
10663 | int balance_cpu; |
10664 | struct rq *rq; | |
83cd4fe2 | 10665 | |
b7031a02 | 10666 | SCHED_WARN_ON((flags & NOHZ_KICK_MASK) == NOHZ_BALANCE_KICK); |
83cd4fe2 | 10667 | |
f643ea22 VG |
10668 | /* |
10669 | * We assume there will be no idle load after this update and clear | |
10670 | * the has_blocked flag. If a cpu enters idle in the mean time, it will | |
7fd7a9e0 | 10671 | * set the has_blocked flag and trigger another update of idle load. |
f643ea22 VG |
10672 | * Because a cpu that becomes idle, is added to idle_cpus_mask before |
10673 | * setting the flag, we are sure to not clear the state and not | |
10674 | * check the load of an idle cpu. | |
7fd7a9e0 VS |
10675 | * |
10676 | * Same applies to idle_cpus_mask vs needs_update. | |
f643ea22 | 10677 | */ |
efd984c4 VS |
10678 | if (flags & NOHZ_STATS_KICK) |
10679 | WRITE_ONCE(nohz.has_blocked, 0); | |
7fd7a9e0 VS |
10680 | if (flags & NOHZ_NEXT_KICK) |
10681 | WRITE_ONCE(nohz.needs_update, 0); | |
f643ea22 VG |
10682 | |
10683 | /* | |
10684 | * Ensures that if we miss the CPU, we must see the has_blocked | |
10685 | * store from nohz_balance_enter_idle(). | |
10686 | */ | |
10687 | smp_mb(); | |
10688 | ||
7a82e5f5 VG |
10689 | /* |
10690 | * Start with the next CPU after this_cpu so we will end with this_cpu and let a | |
10691 | * chance for other idle cpu to pull load. | |
10692 | */ | |
10693 | for_each_cpu_wrap(balance_cpu, nohz.idle_cpus_mask, this_cpu+1) { | |
10694 | if (!idle_cpu(balance_cpu)) | |
83cd4fe2 VP |
10695 | continue; |
10696 | ||
10697 | /* | |
97fb7a0a IM |
10698 | * If this CPU gets work to do, stop the load balancing |
10699 | * work being done for other CPUs. Next load | |
83cd4fe2 VP |
10700 | * balancing owner will pick it up. |
10701 | */ | |
f643ea22 | 10702 | if (need_resched()) { |
efd984c4 VS |
10703 | if (flags & NOHZ_STATS_KICK) |
10704 | has_blocked_load = true; | |
7fd7a9e0 VS |
10705 | if (flags & NOHZ_NEXT_KICK) |
10706 | WRITE_ONCE(nohz.needs_update, 1); | |
f643ea22 VG |
10707 | goto abort; |
10708 | } | |
83cd4fe2 | 10709 | |
5ed4f1d9 VG |
10710 | rq = cpu_rq(balance_cpu); |
10711 | ||
efd984c4 VS |
10712 | if (flags & NOHZ_STATS_KICK) |
10713 | has_blocked_load |= update_nohz_stats(rq); | |
f643ea22 | 10714 | |
ed61bbc6 TC |
10715 | /* |
10716 | * If time for next balance is due, | |
10717 | * do the balance. | |
10718 | */ | |
10719 | if (time_after_eq(jiffies, rq->next_balance)) { | |
8a8c69c3 PZ |
10720 | struct rq_flags rf; |
10721 | ||
31e77c93 | 10722 | rq_lock_irqsave(rq, &rf); |
ed61bbc6 | 10723 | update_rq_clock(rq); |
31e77c93 | 10724 | rq_unlock_irqrestore(rq, &rf); |
8a8c69c3 | 10725 | |
b7031a02 PZ |
10726 | if (flags & NOHZ_BALANCE_KICK) |
10727 | rebalance_domains(rq, CPU_IDLE); | |
ed61bbc6 | 10728 | } |
83cd4fe2 | 10729 | |
c5afb6a8 VG |
10730 | if (time_after(next_balance, rq->next_balance)) { |
10731 | next_balance = rq->next_balance; | |
10732 | update_next_balance = 1; | |
10733 | } | |
83cd4fe2 | 10734 | } |
c5afb6a8 | 10735 | |
3ea2f097 VG |
10736 | /* |
10737 | * next_balance will be updated only when there is a need. | |
10738 | * When the CPU is attached to null domain for ex, it will not be | |
10739 | * updated. | |
10740 | */ | |
10741 | if (likely(update_next_balance)) | |
10742 | nohz.next_balance = next_balance; | |
10743 | ||
efd984c4 VS |
10744 | if (flags & NOHZ_STATS_KICK) |
10745 | WRITE_ONCE(nohz.next_blocked, | |
10746 | now + msecs_to_jiffies(LOAD_AVG_PERIOD)); | |
f643ea22 VG |
10747 | |
10748 | abort: | |
10749 | /* There is still blocked load, enable periodic update */ | |
10750 | if (has_blocked_load) | |
10751 | WRITE_ONCE(nohz.has_blocked, 1); | |
31e77c93 VG |
10752 | } |
10753 | ||
10754 | /* | |
10755 | * In CONFIG_NO_HZ_COMMON case, the idle balance kickee will do the | |
10756 | * rebalancing for all the cpus for whom scheduler ticks are stopped. | |
10757 | */ | |
10758 | static bool nohz_idle_balance(struct rq *this_rq, enum cpu_idle_type idle) | |
10759 | { | |
19a1f5ec | 10760 | unsigned int flags = this_rq->nohz_idle_balance; |
31e77c93 | 10761 | |
19a1f5ec | 10762 | if (!flags) |
31e77c93 VG |
10763 | return false; |
10764 | ||
19a1f5ec | 10765 | this_rq->nohz_idle_balance = 0; |
31e77c93 | 10766 | |
19a1f5ec | 10767 | if (idle != CPU_IDLE) |
31e77c93 VG |
10768 | return false; |
10769 | ||
10770 | _nohz_idle_balance(this_rq, flags, idle); | |
10771 | ||
b7031a02 | 10772 | return true; |
83cd4fe2 | 10773 | } |
31e77c93 | 10774 | |
c6f88654 VG |
10775 | /* |
10776 | * Check if we need to run the ILB for updating blocked load before entering | |
10777 | * idle state. | |
10778 | */ | |
10779 | void nohz_run_idle_balance(int cpu) | |
10780 | { | |
10781 | unsigned int flags; | |
10782 | ||
10783 | flags = atomic_fetch_andnot(NOHZ_NEWILB_KICK, nohz_flags(cpu)); | |
10784 | ||
10785 | /* | |
10786 | * Update the blocked load only if no SCHED_SOFTIRQ is about to happen | |
10787 | * (ie NOHZ_STATS_KICK set) and will do the same. | |
10788 | */ | |
10789 | if ((flags == NOHZ_NEWILB_KICK) && !need_resched()) | |
10790 | _nohz_idle_balance(cpu_rq(cpu), NOHZ_STATS_KICK, CPU_IDLE); | |
10791 | } | |
10792 | ||
31e77c93 VG |
10793 | static void nohz_newidle_balance(struct rq *this_rq) |
10794 | { | |
10795 | int this_cpu = this_rq->cpu; | |
10796 | ||
10797 | /* | |
10798 | * This CPU doesn't want to be disturbed by scheduler | |
10799 | * housekeeping | |
10800 | */ | |
10801 | if (!housekeeping_cpu(this_cpu, HK_FLAG_SCHED)) | |
10802 | return; | |
10803 | ||
10804 | /* Will wake up very soon. No time for doing anything else*/ | |
10805 | if (this_rq->avg_idle < sysctl_sched_migration_cost) | |
10806 | return; | |
10807 | ||
10808 | /* Don't need to update blocked load of idle CPUs*/ | |
10809 | if (!READ_ONCE(nohz.has_blocked) || | |
10810 | time_before(jiffies, READ_ONCE(nohz.next_blocked))) | |
10811 | return; | |
10812 | ||
31e77c93 | 10813 | /* |
c6f88654 VG |
10814 | * Set the need to trigger ILB in order to update blocked load |
10815 | * before entering idle state. | |
31e77c93 | 10816 | */ |
c6f88654 | 10817 | atomic_or(NOHZ_NEWILB_KICK, nohz_flags(this_cpu)); |
31e77c93 VG |
10818 | } |
10819 | ||
dd707247 PZ |
10820 | #else /* !CONFIG_NO_HZ_COMMON */ |
10821 | static inline void nohz_balancer_kick(struct rq *rq) { } | |
10822 | ||
31e77c93 | 10823 | static inline bool nohz_idle_balance(struct rq *this_rq, enum cpu_idle_type idle) |
b7031a02 PZ |
10824 | { |
10825 | return false; | |
10826 | } | |
31e77c93 VG |
10827 | |
10828 | static inline void nohz_newidle_balance(struct rq *this_rq) { } | |
dd707247 | 10829 | #endif /* CONFIG_NO_HZ_COMMON */ |
83cd4fe2 | 10830 | |
47ea5412 | 10831 | /* |
5b78f2dc | 10832 | * newidle_balance is called by schedule() if this_cpu is about to become |
47ea5412 | 10833 | * idle. Attempts to pull tasks from other CPUs. |
7277a34c PZ |
10834 | * |
10835 | * Returns: | |
10836 | * < 0 - we released the lock and there are !fair tasks present | |
10837 | * 0 - failed, no new tasks | |
10838 | * > 0 - success, new (fair) tasks present | |
47ea5412 | 10839 | */ |
d91cecc1 | 10840 | static int newidle_balance(struct rq *this_rq, struct rq_flags *rf) |
47ea5412 PZ |
10841 | { |
10842 | unsigned long next_balance = jiffies + HZ; | |
10843 | int this_cpu = this_rq->cpu; | |
10844 | struct sched_domain *sd; | |
10845 | int pulled_task = 0; | |
10846 | u64 curr_cost = 0; | |
10847 | ||
5ba553ef | 10848 | update_misfit_status(NULL, this_rq); |
e5e678e4 RR |
10849 | |
10850 | /* | |
10851 | * There is a task waiting to run. No need to search for one. | |
10852 | * Return 0; the task will be enqueued when switching to idle. | |
10853 | */ | |
10854 | if (this_rq->ttwu_pending) | |
10855 | return 0; | |
10856 | ||
47ea5412 PZ |
10857 | /* |
10858 | * We must set idle_stamp _before_ calling idle_balance(), such that we | |
10859 | * measure the duration of idle_balance() as idle time. | |
10860 | */ | |
10861 | this_rq->idle_stamp = rq_clock(this_rq); | |
10862 | ||
10863 | /* | |
10864 | * Do not pull tasks towards !active CPUs... | |
10865 | */ | |
10866 | if (!cpu_active(this_cpu)) | |
10867 | return 0; | |
10868 | ||
10869 | /* | |
10870 | * This is OK, because current is on_cpu, which avoids it being picked | |
10871 | * for load-balance and preemption/IRQs are still disabled avoiding | |
10872 | * further scheduler activity on it and we're being very careful to | |
10873 | * re-start the picking loop. | |
10874 | */ | |
10875 | rq_unpin_lock(this_rq, rf); | |
10876 | ||
10877 | if (this_rq->avg_idle < sysctl_sched_migration_cost || | |
e90c8fe1 | 10878 | !READ_ONCE(this_rq->rd->overload)) { |
31e77c93 | 10879 | |
47ea5412 PZ |
10880 | rcu_read_lock(); |
10881 | sd = rcu_dereference_check_sched_domain(this_rq->sd); | |
10882 | if (sd) | |
10883 | update_next_balance(sd, &next_balance); | |
10884 | rcu_read_unlock(); | |
10885 | ||
10886 | goto out; | |
10887 | } | |
10888 | ||
5cb9eaa3 | 10889 | raw_spin_rq_unlock(this_rq); |
47ea5412 PZ |
10890 | |
10891 | update_blocked_averages(this_cpu); | |
10892 | rcu_read_lock(); | |
10893 | for_each_domain(this_cpu, sd) { | |
10894 | int continue_balancing = 1; | |
10895 | u64 t0, domain_cost; | |
10896 | ||
47ea5412 PZ |
10897 | if (this_rq->avg_idle < curr_cost + sd->max_newidle_lb_cost) { |
10898 | update_next_balance(sd, &next_balance); | |
10899 | break; | |
10900 | } | |
10901 | ||
10902 | if (sd->flags & SD_BALANCE_NEWIDLE) { | |
10903 | t0 = sched_clock_cpu(this_cpu); | |
10904 | ||
10905 | pulled_task = load_balance(this_cpu, this_rq, | |
10906 | sd, CPU_NEWLY_IDLE, | |
10907 | &continue_balancing); | |
10908 | ||
10909 | domain_cost = sched_clock_cpu(this_cpu) - t0; | |
10910 | if (domain_cost > sd->max_newidle_lb_cost) | |
10911 | sd->max_newidle_lb_cost = domain_cost; | |
10912 | ||
10913 | curr_cost += domain_cost; | |
10914 | } | |
10915 | ||
10916 | update_next_balance(sd, &next_balance); | |
10917 | ||
10918 | /* | |
10919 | * Stop searching for tasks to pull if there are | |
10920 | * now runnable tasks on this rq. | |
10921 | */ | |
e5e678e4 RR |
10922 | if (pulled_task || this_rq->nr_running > 0 || |
10923 | this_rq->ttwu_pending) | |
47ea5412 PZ |
10924 | break; |
10925 | } | |
10926 | rcu_read_unlock(); | |
10927 | ||
5cb9eaa3 | 10928 | raw_spin_rq_lock(this_rq); |
47ea5412 PZ |
10929 | |
10930 | if (curr_cost > this_rq->max_idle_balance_cost) | |
10931 | this_rq->max_idle_balance_cost = curr_cost; | |
10932 | ||
10933 | /* | |
10934 | * While browsing the domains, we released the rq lock, a task could | |
10935 | * have been enqueued in the meantime. Since we're not going idle, | |
10936 | * pretend we pulled a task. | |
10937 | */ | |
10938 | if (this_rq->cfs.h_nr_running && !pulled_task) | |
10939 | pulled_task = 1; | |
10940 | ||
47ea5412 PZ |
10941 | /* Is there a task of a high priority class? */ |
10942 | if (this_rq->nr_running != this_rq->cfs.h_nr_running) | |
10943 | pulled_task = -1; | |
10944 | ||
6553fc18 VG |
10945 | out: |
10946 | /* Move the next balance forward */ | |
10947 | if (time_after(this_rq->next_balance, next_balance)) | |
10948 | this_rq->next_balance = next_balance; | |
10949 | ||
47ea5412 PZ |
10950 | if (pulled_task) |
10951 | this_rq->idle_stamp = 0; | |
0826530d VG |
10952 | else |
10953 | nohz_newidle_balance(this_rq); | |
47ea5412 PZ |
10954 | |
10955 | rq_repin_lock(this_rq, rf); | |
10956 | ||
10957 | return pulled_task; | |
10958 | } | |
10959 | ||
83cd4fe2 VP |
10960 | /* |
10961 | * run_rebalance_domains is triggered when needed from the scheduler tick. | |
10962 | * Also triggered for nohz idle balancing (with nohz_balancing_kick set). | |
10963 | */ | |
0766f788 | 10964 | static __latent_entropy void run_rebalance_domains(struct softirq_action *h) |
1e3c88bd | 10965 | { |
208cb16b | 10966 | struct rq *this_rq = this_rq(); |
6eb57e0d | 10967 | enum cpu_idle_type idle = this_rq->idle_balance ? |
1e3c88bd PZ |
10968 | CPU_IDLE : CPU_NOT_IDLE; |
10969 | ||
1e3c88bd | 10970 | /* |
97fb7a0a IM |
10971 | * If this CPU has a pending nohz_balance_kick, then do the |
10972 | * balancing on behalf of the other idle CPUs whose ticks are | |
d4573c3e | 10973 | * stopped. Do nohz_idle_balance *before* rebalance_domains to |
97fb7a0a | 10974 | * give the idle CPUs a chance to load balance. Else we may |
d4573c3e PM |
10975 | * load balance only within the local sched_domain hierarchy |
10976 | * and abort nohz_idle_balance altogether if we pull some load. | |
1e3c88bd | 10977 | */ |
b7031a02 PZ |
10978 | if (nohz_idle_balance(this_rq, idle)) |
10979 | return; | |
10980 | ||
10981 | /* normal load balance */ | |
10982 | update_blocked_averages(this_rq->cpu); | |
d4573c3e | 10983 | rebalance_domains(this_rq, idle); |
1e3c88bd PZ |
10984 | } |
10985 | ||
1e3c88bd PZ |
10986 | /* |
10987 | * Trigger the SCHED_SOFTIRQ if it is time to do periodic load balancing. | |
1e3c88bd | 10988 | */ |
7caff66f | 10989 | void trigger_load_balance(struct rq *rq) |
1e3c88bd | 10990 | { |
e0b257c3 AMB |
10991 | /* |
10992 | * Don't need to rebalance while attached to NULL domain or | |
10993 | * runqueue CPU is not active | |
10994 | */ | |
10995 | if (unlikely(on_null_domain(rq) || !cpu_active(cpu_of(rq)))) | |
c726099e DL |
10996 | return; |
10997 | ||
10998 | if (time_after_eq(jiffies, rq->next_balance)) | |
1e3c88bd | 10999 | raise_softirq(SCHED_SOFTIRQ); |
4550487a PZ |
11000 | |
11001 | nohz_balancer_kick(rq); | |
1e3c88bd PZ |
11002 | } |
11003 | ||
0bcdcf28 CE |
11004 | static void rq_online_fair(struct rq *rq) |
11005 | { | |
11006 | update_sysctl(); | |
0e59bdae KT |
11007 | |
11008 | update_runtime_enabled(rq); | |
0bcdcf28 CE |
11009 | } |
11010 | ||
11011 | static void rq_offline_fair(struct rq *rq) | |
11012 | { | |
11013 | update_sysctl(); | |
a4c96ae3 PB |
11014 | |
11015 | /* Ensure any throttled groups are reachable by pick_next_task */ | |
11016 | unthrottle_offline_cfs_rqs(rq); | |
0bcdcf28 CE |
11017 | } |
11018 | ||
55e12e5e | 11019 | #endif /* CONFIG_SMP */ |
e1d1484f | 11020 | |
8039e96f VP |
11021 | #ifdef CONFIG_SCHED_CORE |
11022 | static inline bool | |
11023 | __entity_slice_used(struct sched_entity *se, int min_nr_tasks) | |
11024 | { | |
11025 | u64 slice = sched_slice(cfs_rq_of(se), se); | |
11026 | u64 rtime = se->sum_exec_runtime - se->prev_sum_exec_runtime; | |
11027 | ||
11028 | return (rtime * min_nr_tasks > slice); | |
11029 | } | |
11030 | ||
11031 | #define MIN_NR_TASKS_DURING_FORCEIDLE 2 | |
11032 | static inline void task_tick_core(struct rq *rq, struct task_struct *curr) | |
11033 | { | |
11034 | if (!sched_core_enabled(rq)) | |
11035 | return; | |
11036 | ||
11037 | /* | |
11038 | * If runqueue has only one task which used up its slice and | |
11039 | * if the sibling is forced idle, then trigger schedule to | |
11040 | * give forced idle task a chance. | |
11041 | * | |
11042 | * sched_slice() considers only this active rq and it gets the | |
11043 | * whole slice. But during force idle, we have siblings acting | |
11044 | * like a single runqueue and hence we need to consider runnable | |
cc00c198 | 11045 | * tasks on this CPU and the forced idle CPU. Ideally, we should |
8039e96f | 11046 | * go through the forced idle rq, but that would be a perf hit. |
cc00c198 | 11047 | * We can assume that the forced idle CPU has at least |
8039e96f | 11048 | * MIN_NR_TASKS_DURING_FORCEIDLE - 1 tasks and use that to check |
cc00c198 | 11049 | * if we need to give up the CPU. |
8039e96f VP |
11050 | */ |
11051 | if (rq->core->core_forceidle && rq->cfs.nr_running == 1 && | |
11052 | __entity_slice_used(&curr->se, MIN_NR_TASKS_DURING_FORCEIDLE)) | |
11053 | resched_curr(rq); | |
11054 | } | |
c6047c2e JFG |
11055 | |
11056 | /* | |
11057 | * se_fi_update - Update the cfs_rq->min_vruntime_fi in a CFS hierarchy if needed. | |
11058 | */ | |
11059 | static void se_fi_update(struct sched_entity *se, unsigned int fi_seq, bool forceidle) | |
11060 | { | |
11061 | for_each_sched_entity(se) { | |
11062 | struct cfs_rq *cfs_rq = cfs_rq_of(se); | |
11063 | ||
11064 | if (forceidle) { | |
11065 | if (cfs_rq->forceidle_seq == fi_seq) | |
11066 | break; | |
11067 | cfs_rq->forceidle_seq = fi_seq; | |
11068 | } | |
11069 | ||
11070 | cfs_rq->min_vruntime_fi = cfs_rq->min_vruntime; | |
11071 | } | |
11072 | } | |
11073 | ||
11074 | void task_vruntime_update(struct rq *rq, struct task_struct *p, bool in_fi) | |
11075 | { | |
11076 | struct sched_entity *se = &p->se; | |
11077 | ||
11078 | if (p->sched_class != &fair_sched_class) | |
11079 | return; | |
11080 | ||
11081 | se_fi_update(se, rq->core->core_forceidle_seq, in_fi); | |
11082 | } | |
11083 | ||
11084 | bool cfs_prio_less(struct task_struct *a, struct task_struct *b, bool in_fi) | |
11085 | { | |
11086 | struct rq *rq = task_rq(a); | |
11087 | struct sched_entity *sea = &a->se; | |
11088 | struct sched_entity *seb = &b->se; | |
11089 | struct cfs_rq *cfs_rqa; | |
11090 | struct cfs_rq *cfs_rqb; | |
11091 | s64 delta; | |
11092 | ||
11093 | SCHED_WARN_ON(task_rq(b)->core != rq->core); | |
11094 | ||
11095 | #ifdef CONFIG_FAIR_GROUP_SCHED | |
11096 | /* | |
11097 | * Find an se in the hierarchy for tasks a and b, such that the se's | |
11098 | * are immediate siblings. | |
11099 | */ | |
11100 | while (sea->cfs_rq->tg != seb->cfs_rq->tg) { | |
11101 | int sea_depth = sea->depth; | |
11102 | int seb_depth = seb->depth; | |
11103 | ||
11104 | if (sea_depth >= seb_depth) | |
11105 | sea = parent_entity(sea); | |
11106 | if (sea_depth <= seb_depth) | |
11107 | seb = parent_entity(seb); | |
11108 | } | |
11109 | ||
11110 | se_fi_update(sea, rq->core->core_forceidle_seq, in_fi); | |
11111 | se_fi_update(seb, rq->core->core_forceidle_seq, in_fi); | |
11112 | ||
11113 | cfs_rqa = sea->cfs_rq; | |
11114 | cfs_rqb = seb->cfs_rq; | |
11115 | #else | |
11116 | cfs_rqa = &task_rq(a)->cfs; | |
11117 | cfs_rqb = &task_rq(b)->cfs; | |
11118 | #endif | |
11119 | ||
11120 | /* | |
11121 | * Find delta after normalizing se's vruntime with its cfs_rq's | |
11122 | * min_vruntime_fi, which would have been updated in prior calls | |
11123 | * to se_fi_update(). | |
11124 | */ | |
11125 | delta = (s64)(sea->vruntime - seb->vruntime) + | |
11126 | (s64)(cfs_rqb->min_vruntime_fi - cfs_rqa->min_vruntime_fi); | |
11127 | ||
11128 | return delta > 0; | |
11129 | } | |
8039e96f VP |
11130 | #else |
11131 | static inline void task_tick_core(struct rq *rq, struct task_struct *curr) {} | |
11132 | #endif | |
11133 | ||
bf0f6f24 | 11134 | /* |
d84b3131 FW |
11135 | * scheduler tick hitting a task of our scheduling class. |
11136 | * | |
11137 | * NOTE: This function can be called remotely by the tick offload that | |
11138 | * goes along full dynticks. Therefore no local assumption can be made | |
11139 | * and everything must be accessed through the @rq and @curr passed in | |
11140 | * parameters. | |
bf0f6f24 | 11141 | */ |
8f4d37ec | 11142 | static void task_tick_fair(struct rq *rq, struct task_struct *curr, int queued) |
bf0f6f24 IM |
11143 | { |
11144 | struct cfs_rq *cfs_rq; | |
11145 | struct sched_entity *se = &curr->se; | |
11146 | ||
11147 | for_each_sched_entity(se) { | |
11148 | cfs_rq = cfs_rq_of(se); | |
8f4d37ec | 11149 | entity_tick(cfs_rq, se, queued); |
bf0f6f24 | 11150 | } |
18bf2805 | 11151 | |
b52da86e | 11152 | if (static_branch_unlikely(&sched_numa_balancing)) |
cbee9f88 | 11153 | task_tick_numa(rq, curr); |
3b1baa64 MR |
11154 | |
11155 | update_misfit_status(curr, rq); | |
2802bf3c | 11156 | update_overutilized_status(task_rq(curr)); |
8039e96f VP |
11157 | |
11158 | task_tick_core(rq, curr); | |
bf0f6f24 IM |
11159 | } |
11160 | ||
11161 | /* | |
cd29fe6f PZ |
11162 | * called on fork with the child task as argument from the parent's context |
11163 | * - child not yet on the tasklist | |
11164 | * - preemption disabled | |
bf0f6f24 | 11165 | */ |
cd29fe6f | 11166 | static void task_fork_fair(struct task_struct *p) |
bf0f6f24 | 11167 | { |
4fc420c9 DN |
11168 | struct cfs_rq *cfs_rq; |
11169 | struct sched_entity *se = &p->se, *curr; | |
cd29fe6f | 11170 | struct rq *rq = this_rq(); |
8a8c69c3 | 11171 | struct rq_flags rf; |
bf0f6f24 | 11172 | |
8a8c69c3 | 11173 | rq_lock(rq, &rf); |
861d034e PZ |
11174 | update_rq_clock(rq); |
11175 | ||
4fc420c9 DN |
11176 | cfs_rq = task_cfs_rq(current); |
11177 | curr = cfs_rq->curr; | |
e210bffd PZ |
11178 | if (curr) { |
11179 | update_curr(cfs_rq); | |
b5d9d734 | 11180 | se->vruntime = curr->vruntime; |
e210bffd | 11181 | } |
aeb73b04 | 11182 | place_entity(cfs_rq, se, 1); |
4d78e7b6 | 11183 | |
cd29fe6f | 11184 | if (sysctl_sched_child_runs_first && curr && entity_before(curr, se)) { |
87fefa38 | 11185 | /* |
edcb60a3 IM |
11186 | * Upon rescheduling, sched_class::put_prev_task() will place |
11187 | * 'current' within the tree based on its new key value. | |
11188 | */ | |
4d78e7b6 | 11189 | swap(curr->vruntime, se->vruntime); |
8875125e | 11190 | resched_curr(rq); |
4d78e7b6 | 11191 | } |
bf0f6f24 | 11192 | |
88ec22d3 | 11193 | se->vruntime -= cfs_rq->min_vruntime; |
8a8c69c3 | 11194 | rq_unlock(rq, &rf); |
bf0f6f24 IM |
11195 | } |
11196 | ||
cb469845 SR |
11197 | /* |
11198 | * Priority of the task has changed. Check to see if we preempt | |
11199 | * the current task. | |
11200 | */ | |
da7a735e PZ |
11201 | static void |
11202 | prio_changed_fair(struct rq *rq, struct task_struct *p, int oldprio) | |
cb469845 | 11203 | { |
da0c1e65 | 11204 | if (!task_on_rq_queued(p)) |
da7a735e PZ |
11205 | return; |
11206 | ||
7c2e8bbd FW |
11207 | if (rq->cfs.nr_running == 1) |
11208 | return; | |
11209 | ||
cb469845 SR |
11210 | /* |
11211 | * Reschedule if we are currently running on this runqueue and | |
11212 | * our priority decreased, or if we are not currently running on | |
11213 | * this runqueue and our priority is higher than the current's | |
11214 | */ | |
65bcf072 | 11215 | if (task_current(rq, p)) { |
cb469845 | 11216 | if (p->prio > oldprio) |
8875125e | 11217 | resched_curr(rq); |
cb469845 | 11218 | } else |
15afe09b | 11219 | check_preempt_curr(rq, p, 0); |
cb469845 SR |
11220 | } |
11221 | ||
daa59407 | 11222 | static inline bool vruntime_normalized(struct task_struct *p) |
da7a735e PZ |
11223 | { |
11224 | struct sched_entity *se = &p->se; | |
da7a735e PZ |
11225 | |
11226 | /* | |
daa59407 BP |
11227 | * In both the TASK_ON_RQ_QUEUED and TASK_ON_RQ_MIGRATING cases, |
11228 | * the dequeue_entity(.flags=0) will already have normalized the | |
11229 | * vruntime. | |
11230 | */ | |
11231 | if (p->on_rq) | |
11232 | return true; | |
11233 | ||
11234 | /* | |
11235 | * When !on_rq, vruntime of the task has usually NOT been normalized. | |
11236 | * But there are some cases where it has already been normalized: | |
da7a735e | 11237 | * |
daa59407 BP |
11238 | * - A forked child which is waiting for being woken up by |
11239 | * wake_up_new_task(). | |
11240 | * - A task which has been woken up by try_to_wake_up() and | |
11241 | * waiting for actually being woken up by sched_ttwu_pending(). | |
da7a735e | 11242 | */ |
d0cdb3ce | 11243 | if (!se->sum_exec_runtime || |
2f064a59 | 11244 | (READ_ONCE(p->__state) == TASK_WAKING && p->sched_remote_wakeup)) |
daa59407 BP |
11245 | return true; |
11246 | ||
11247 | return false; | |
11248 | } | |
11249 | ||
09a43ace VG |
11250 | #ifdef CONFIG_FAIR_GROUP_SCHED |
11251 | /* | |
11252 | * Propagate the changes of the sched_entity across the tg tree to make it | |
11253 | * visible to the root | |
11254 | */ | |
11255 | static void propagate_entity_cfs_rq(struct sched_entity *se) | |
11256 | { | |
11257 | struct cfs_rq *cfs_rq; | |
11258 | ||
0258bdfa OU |
11259 | list_add_leaf_cfs_rq(cfs_rq_of(se)); |
11260 | ||
09a43ace VG |
11261 | /* Start to propagate at parent */ |
11262 | se = se->parent; | |
11263 | ||
11264 | for_each_sched_entity(se) { | |
11265 | cfs_rq = cfs_rq_of(se); | |
11266 | ||
0258bdfa OU |
11267 | if (!cfs_rq_throttled(cfs_rq)){ |
11268 | update_load_avg(cfs_rq, se, UPDATE_TG); | |
11269 | list_add_leaf_cfs_rq(cfs_rq); | |
11270 | continue; | |
11271 | } | |
09a43ace | 11272 | |
0258bdfa OU |
11273 | if (list_add_leaf_cfs_rq(cfs_rq)) |
11274 | break; | |
09a43ace VG |
11275 | } |
11276 | } | |
11277 | #else | |
11278 | static void propagate_entity_cfs_rq(struct sched_entity *se) { } | |
11279 | #endif | |
11280 | ||
df217913 | 11281 | static void detach_entity_cfs_rq(struct sched_entity *se) |
daa59407 | 11282 | { |
daa59407 BP |
11283 | struct cfs_rq *cfs_rq = cfs_rq_of(se); |
11284 | ||
9d89c257 | 11285 | /* Catch up with the cfs_rq and remove our load when we leave */ |
88c0616e | 11286 | update_load_avg(cfs_rq, se, 0); |
a05e8c51 | 11287 | detach_entity_load_avg(cfs_rq, se); |
fe749158 | 11288 | update_tg_load_avg(cfs_rq); |
09a43ace | 11289 | propagate_entity_cfs_rq(se); |
da7a735e PZ |
11290 | } |
11291 | ||
df217913 | 11292 | static void attach_entity_cfs_rq(struct sched_entity *se) |
cb469845 | 11293 | { |
daa59407 | 11294 | struct cfs_rq *cfs_rq = cfs_rq_of(se); |
7855a35a BP |
11295 | |
11296 | #ifdef CONFIG_FAIR_GROUP_SCHED | |
eb7a59b2 M |
11297 | /* |
11298 | * Since the real-depth could have been changed (only FAIR | |
11299 | * class maintain depth value), reset depth properly. | |
11300 | */ | |
11301 | se->depth = se->parent ? se->parent->depth + 1 : 0; | |
11302 | #endif | |
7855a35a | 11303 | |
df217913 | 11304 | /* Synchronize entity with its cfs_rq */ |
88c0616e | 11305 | update_load_avg(cfs_rq, se, sched_feat(ATTACH_AGE_LOAD) ? 0 : SKIP_AGE_LOAD); |
a4f9a0e5 | 11306 | attach_entity_load_avg(cfs_rq, se); |
fe749158 | 11307 | update_tg_load_avg(cfs_rq); |
09a43ace | 11308 | propagate_entity_cfs_rq(se); |
df217913 VG |
11309 | } |
11310 | ||
11311 | static void detach_task_cfs_rq(struct task_struct *p) | |
11312 | { | |
11313 | struct sched_entity *se = &p->se; | |
11314 | struct cfs_rq *cfs_rq = cfs_rq_of(se); | |
11315 | ||
11316 | if (!vruntime_normalized(p)) { | |
11317 | /* | |
11318 | * Fix up our vruntime so that the current sleep doesn't | |
11319 | * cause 'unlimited' sleep bonus. | |
11320 | */ | |
11321 | place_entity(cfs_rq, se, 0); | |
11322 | se->vruntime -= cfs_rq->min_vruntime; | |
11323 | } | |
11324 | ||
11325 | detach_entity_cfs_rq(se); | |
11326 | } | |
11327 | ||
11328 | static void attach_task_cfs_rq(struct task_struct *p) | |
11329 | { | |
11330 | struct sched_entity *se = &p->se; | |
11331 | struct cfs_rq *cfs_rq = cfs_rq_of(se); | |
11332 | ||
11333 | attach_entity_cfs_rq(se); | |
daa59407 BP |
11334 | |
11335 | if (!vruntime_normalized(p)) | |
11336 | se->vruntime += cfs_rq->min_vruntime; | |
11337 | } | |
6efdb105 | 11338 | |
daa59407 BP |
11339 | static void switched_from_fair(struct rq *rq, struct task_struct *p) |
11340 | { | |
11341 | detach_task_cfs_rq(p); | |
11342 | } | |
11343 | ||
11344 | static void switched_to_fair(struct rq *rq, struct task_struct *p) | |
11345 | { | |
11346 | attach_task_cfs_rq(p); | |
7855a35a | 11347 | |
daa59407 | 11348 | if (task_on_rq_queued(p)) { |
7855a35a | 11349 | /* |
daa59407 BP |
11350 | * We were most likely switched from sched_rt, so |
11351 | * kick off the schedule if running, otherwise just see | |
11352 | * if we can still preempt the current task. | |
7855a35a | 11353 | */ |
65bcf072 | 11354 | if (task_current(rq, p)) |
daa59407 BP |
11355 | resched_curr(rq); |
11356 | else | |
11357 | check_preempt_curr(rq, p, 0); | |
7855a35a | 11358 | } |
cb469845 SR |
11359 | } |
11360 | ||
83b699ed SV |
11361 | /* Account for a task changing its policy or group. |
11362 | * | |
11363 | * This routine is mostly called to set cfs_rq->curr field when a task | |
11364 | * migrates between groups/classes. | |
11365 | */ | |
a0e813f2 | 11366 | static void set_next_task_fair(struct rq *rq, struct task_struct *p, bool first) |
83b699ed | 11367 | { |
03b7fad1 PZ |
11368 | struct sched_entity *se = &p->se; |
11369 | ||
11370 | #ifdef CONFIG_SMP | |
11371 | if (task_on_rq_queued(p)) { | |
11372 | /* | |
11373 | * Move the next running task to the front of the list, so our | |
11374 | * cfs_tasks list becomes MRU one. | |
11375 | */ | |
11376 | list_move(&se->group_node, &rq->cfs_tasks); | |
11377 | } | |
11378 | #endif | |
83b699ed | 11379 | |
ec12cb7f PT |
11380 | for_each_sched_entity(se) { |
11381 | struct cfs_rq *cfs_rq = cfs_rq_of(se); | |
11382 | ||
11383 | set_next_entity(cfs_rq, se); | |
11384 | /* ensure bandwidth has been allocated on our new cfs_rq */ | |
11385 | account_cfs_rq_runtime(cfs_rq, 0); | |
11386 | } | |
83b699ed SV |
11387 | } |
11388 | ||
029632fb PZ |
11389 | void init_cfs_rq(struct cfs_rq *cfs_rq) |
11390 | { | |
bfb06889 | 11391 | cfs_rq->tasks_timeline = RB_ROOT_CACHED; |
029632fb PZ |
11392 | cfs_rq->min_vruntime = (u64)(-(1LL << 20)); |
11393 | #ifndef CONFIG_64BIT | |
11394 | cfs_rq->min_vruntime_copy = cfs_rq->min_vruntime; | |
11395 | #endif | |
141965c7 | 11396 | #ifdef CONFIG_SMP |
2a2f5d4e | 11397 | raw_spin_lock_init(&cfs_rq->removed.lock); |
9ee474f5 | 11398 | #endif |
029632fb PZ |
11399 | } |
11400 | ||
810b3817 | 11401 | #ifdef CONFIG_FAIR_GROUP_SCHED |
ea86cb4b VG |
11402 | static void task_set_group_fair(struct task_struct *p) |
11403 | { | |
11404 | struct sched_entity *se = &p->se; | |
11405 | ||
11406 | set_task_rq(p, task_cpu(p)); | |
11407 | se->depth = se->parent ? se->parent->depth + 1 : 0; | |
11408 | } | |
11409 | ||
bc54da21 | 11410 | static void task_move_group_fair(struct task_struct *p) |
810b3817 | 11411 | { |
daa59407 | 11412 | detach_task_cfs_rq(p); |
b2b5ce02 | 11413 | set_task_rq(p, task_cpu(p)); |
6efdb105 BP |
11414 | |
11415 | #ifdef CONFIG_SMP | |
11416 | /* Tell se's cfs_rq has been changed -- migrated */ | |
11417 | p->se.avg.last_update_time = 0; | |
11418 | #endif | |
daa59407 | 11419 | attach_task_cfs_rq(p); |
810b3817 | 11420 | } |
029632fb | 11421 | |
ea86cb4b VG |
11422 | static void task_change_group_fair(struct task_struct *p, int type) |
11423 | { | |
11424 | switch (type) { | |
11425 | case TASK_SET_GROUP: | |
11426 | task_set_group_fair(p); | |
11427 | break; | |
11428 | ||
11429 | case TASK_MOVE_GROUP: | |
11430 | task_move_group_fair(p); | |
11431 | break; | |
11432 | } | |
11433 | } | |
11434 | ||
029632fb PZ |
11435 | void free_fair_sched_group(struct task_group *tg) |
11436 | { | |
11437 | int i; | |
11438 | ||
11439 | destroy_cfs_bandwidth(tg_cfs_bandwidth(tg)); | |
11440 | ||
11441 | for_each_possible_cpu(i) { | |
11442 | if (tg->cfs_rq) | |
11443 | kfree(tg->cfs_rq[i]); | |
6fe1f348 | 11444 | if (tg->se) |
029632fb PZ |
11445 | kfree(tg->se[i]); |
11446 | } | |
11447 | ||
11448 | kfree(tg->cfs_rq); | |
11449 | kfree(tg->se); | |
11450 | } | |
11451 | ||
11452 | int alloc_fair_sched_group(struct task_group *tg, struct task_group *parent) | |
11453 | { | |
029632fb | 11454 | struct sched_entity *se; |
b7fa30c9 | 11455 | struct cfs_rq *cfs_rq; |
029632fb PZ |
11456 | int i; |
11457 | ||
6396bb22 | 11458 | tg->cfs_rq = kcalloc(nr_cpu_ids, sizeof(cfs_rq), GFP_KERNEL); |
029632fb PZ |
11459 | if (!tg->cfs_rq) |
11460 | goto err; | |
6396bb22 | 11461 | tg->se = kcalloc(nr_cpu_ids, sizeof(se), GFP_KERNEL); |
029632fb PZ |
11462 | if (!tg->se) |
11463 | goto err; | |
11464 | ||
11465 | tg->shares = NICE_0_LOAD; | |
11466 | ||
11467 | init_cfs_bandwidth(tg_cfs_bandwidth(tg)); | |
11468 | ||
11469 | for_each_possible_cpu(i) { | |
11470 | cfs_rq = kzalloc_node(sizeof(struct cfs_rq), | |
11471 | GFP_KERNEL, cpu_to_node(i)); | |
11472 | if (!cfs_rq) | |
11473 | goto err; | |
11474 | ||
ceeadb83 | 11475 | se = kzalloc_node(sizeof(struct sched_entity_stats), |
029632fb PZ |
11476 | GFP_KERNEL, cpu_to_node(i)); |
11477 | if (!se) | |
11478 | goto err_free_rq; | |
11479 | ||
11480 | init_cfs_rq(cfs_rq); | |
11481 | init_tg_cfs_entry(tg, cfs_rq, se, i, parent->se[i]); | |
540247fb | 11482 | init_entity_runnable_average(se); |
029632fb PZ |
11483 | } |
11484 | ||
11485 | return 1; | |
11486 | ||
11487 | err_free_rq: | |
11488 | kfree(cfs_rq); | |
11489 | err: | |
11490 | return 0; | |
11491 | } | |
11492 | ||
8663e24d PZ |
11493 | void online_fair_sched_group(struct task_group *tg) |
11494 | { | |
11495 | struct sched_entity *se; | |
a46d14ec | 11496 | struct rq_flags rf; |
8663e24d PZ |
11497 | struct rq *rq; |
11498 | int i; | |
11499 | ||
11500 | for_each_possible_cpu(i) { | |
11501 | rq = cpu_rq(i); | |
11502 | se = tg->se[i]; | |
a46d14ec | 11503 | rq_lock_irq(rq, &rf); |
4126bad6 | 11504 | update_rq_clock(rq); |
d0326691 | 11505 | attach_entity_cfs_rq(se); |
55e16d30 | 11506 | sync_throttle(tg, i); |
a46d14ec | 11507 | rq_unlock_irq(rq, &rf); |
8663e24d PZ |
11508 | } |
11509 | } | |
11510 | ||
6fe1f348 | 11511 | void unregister_fair_sched_group(struct task_group *tg) |
029632fb | 11512 | { |
029632fb | 11513 | unsigned long flags; |
6fe1f348 PZ |
11514 | struct rq *rq; |
11515 | int cpu; | |
029632fb | 11516 | |
6fe1f348 PZ |
11517 | for_each_possible_cpu(cpu) { |
11518 | if (tg->se[cpu]) | |
11519 | remove_entity_load_avg(tg->se[cpu]); | |
029632fb | 11520 | |
6fe1f348 PZ |
11521 | /* |
11522 | * Only empty task groups can be destroyed; so we can speculatively | |
11523 | * check on_list without danger of it being re-added. | |
11524 | */ | |
11525 | if (!tg->cfs_rq[cpu]->on_list) | |
11526 | continue; | |
11527 | ||
11528 | rq = cpu_rq(cpu); | |
11529 | ||
5cb9eaa3 | 11530 | raw_spin_rq_lock_irqsave(rq, flags); |
6fe1f348 | 11531 | list_del_leaf_cfs_rq(tg->cfs_rq[cpu]); |
5cb9eaa3 | 11532 | raw_spin_rq_unlock_irqrestore(rq, flags); |
6fe1f348 | 11533 | } |
029632fb PZ |
11534 | } |
11535 | ||
11536 | void init_tg_cfs_entry(struct task_group *tg, struct cfs_rq *cfs_rq, | |
11537 | struct sched_entity *se, int cpu, | |
11538 | struct sched_entity *parent) | |
11539 | { | |
11540 | struct rq *rq = cpu_rq(cpu); | |
11541 | ||
11542 | cfs_rq->tg = tg; | |
11543 | cfs_rq->rq = rq; | |
029632fb PZ |
11544 | init_cfs_rq_runtime(cfs_rq); |
11545 | ||
11546 | tg->cfs_rq[cpu] = cfs_rq; | |
11547 | tg->se[cpu] = se; | |
11548 | ||
11549 | /* se could be NULL for root_task_group */ | |
11550 | if (!se) | |
11551 | return; | |
11552 | ||
fed14d45 | 11553 | if (!parent) { |
029632fb | 11554 | se->cfs_rq = &rq->cfs; |
fed14d45 PZ |
11555 | se->depth = 0; |
11556 | } else { | |
029632fb | 11557 | se->cfs_rq = parent->my_q; |
fed14d45 PZ |
11558 | se->depth = parent->depth + 1; |
11559 | } | |
029632fb PZ |
11560 | |
11561 | se->my_q = cfs_rq; | |
0ac9b1c2 PT |
11562 | /* guarantee group entities always have weight */ |
11563 | update_load_set(&se->load, NICE_0_LOAD); | |
029632fb PZ |
11564 | se->parent = parent; |
11565 | } | |
11566 | ||
11567 | static DEFINE_MUTEX(shares_mutex); | |
11568 | ||
30400039 | 11569 | static int __sched_group_set_shares(struct task_group *tg, unsigned long shares) |
029632fb PZ |
11570 | { |
11571 | int i; | |
029632fb | 11572 | |
30400039 JD |
11573 | lockdep_assert_held(&shares_mutex); |
11574 | ||
029632fb PZ |
11575 | /* |
11576 | * We can't change the weight of the root cgroup. | |
11577 | */ | |
11578 | if (!tg->se[0]) | |
11579 | return -EINVAL; | |
11580 | ||
11581 | shares = clamp(shares, scale_load(MIN_SHARES), scale_load(MAX_SHARES)); | |
11582 | ||
029632fb | 11583 | if (tg->shares == shares) |
30400039 | 11584 | return 0; |
029632fb PZ |
11585 | |
11586 | tg->shares = shares; | |
11587 | for_each_possible_cpu(i) { | |
11588 | struct rq *rq = cpu_rq(i); | |
8a8c69c3 PZ |
11589 | struct sched_entity *se = tg->se[i]; |
11590 | struct rq_flags rf; | |
029632fb | 11591 | |
029632fb | 11592 | /* Propagate contribution to hierarchy */ |
8a8c69c3 | 11593 | rq_lock_irqsave(rq, &rf); |
71b1da46 | 11594 | update_rq_clock(rq); |
89ee048f | 11595 | for_each_sched_entity(se) { |
88c0616e | 11596 | update_load_avg(cfs_rq_of(se), se, UPDATE_TG); |
1ea6c46a | 11597 | update_cfs_group(se); |
89ee048f | 11598 | } |
8a8c69c3 | 11599 | rq_unlock_irqrestore(rq, &rf); |
029632fb PZ |
11600 | } |
11601 | ||
30400039 JD |
11602 | return 0; |
11603 | } | |
11604 | ||
11605 | int sched_group_set_shares(struct task_group *tg, unsigned long shares) | |
11606 | { | |
11607 | int ret; | |
11608 | ||
11609 | mutex_lock(&shares_mutex); | |
11610 | if (tg_is_idle(tg)) | |
11611 | ret = -EINVAL; | |
11612 | else | |
11613 | ret = __sched_group_set_shares(tg, shares); | |
11614 | mutex_unlock(&shares_mutex); | |
11615 | ||
11616 | return ret; | |
11617 | } | |
11618 | ||
11619 | int sched_group_set_idle(struct task_group *tg, long idle) | |
11620 | { | |
11621 | int i; | |
11622 | ||
11623 | if (tg == &root_task_group) | |
11624 | return -EINVAL; | |
11625 | ||
11626 | if (idle < 0 || idle > 1) | |
11627 | return -EINVAL; | |
11628 | ||
11629 | mutex_lock(&shares_mutex); | |
11630 | ||
11631 | if (tg->idle == idle) { | |
11632 | mutex_unlock(&shares_mutex); | |
11633 | return 0; | |
11634 | } | |
11635 | ||
11636 | tg->idle = idle; | |
11637 | ||
11638 | for_each_possible_cpu(i) { | |
11639 | struct rq *rq = cpu_rq(i); | |
11640 | struct sched_entity *se = tg->se[i]; | |
a480adde | 11641 | struct cfs_rq *parent_cfs_rq, *grp_cfs_rq = tg->cfs_rq[i]; |
30400039 JD |
11642 | bool was_idle = cfs_rq_is_idle(grp_cfs_rq); |
11643 | long idle_task_delta; | |
11644 | struct rq_flags rf; | |
11645 | ||
11646 | rq_lock_irqsave(rq, &rf); | |
11647 | ||
11648 | grp_cfs_rq->idle = idle; | |
11649 | if (WARN_ON_ONCE(was_idle == cfs_rq_is_idle(grp_cfs_rq))) | |
11650 | goto next_cpu; | |
11651 | ||
a480adde JD |
11652 | if (se->on_rq) { |
11653 | parent_cfs_rq = cfs_rq_of(se); | |
11654 | if (cfs_rq_is_idle(grp_cfs_rq)) | |
11655 | parent_cfs_rq->idle_nr_running++; | |
11656 | else | |
11657 | parent_cfs_rq->idle_nr_running--; | |
11658 | } | |
11659 | ||
30400039 JD |
11660 | idle_task_delta = grp_cfs_rq->h_nr_running - |
11661 | grp_cfs_rq->idle_h_nr_running; | |
11662 | if (!cfs_rq_is_idle(grp_cfs_rq)) | |
11663 | idle_task_delta *= -1; | |
11664 | ||
11665 | for_each_sched_entity(se) { | |
11666 | struct cfs_rq *cfs_rq = cfs_rq_of(se); | |
11667 | ||
11668 | if (!se->on_rq) | |
11669 | break; | |
11670 | ||
11671 | cfs_rq->idle_h_nr_running += idle_task_delta; | |
11672 | ||
11673 | /* Already accounted at parent level and above. */ | |
11674 | if (cfs_rq_is_idle(cfs_rq)) | |
11675 | break; | |
11676 | } | |
11677 | ||
11678 | next_cpu: | |
11679 | rq_unlock_irqrestore(rq, &rf); | |
11680 | } | |
11681 | ||
11682 | /* Idle groups have minimum weight. */ | |
11683 | if (tg_is_idle(tg)) | |
11684 | __sched_group_set_shares(tg, scale_load(WEIGHT_IDLEPRIO)); | |
11685 | else | |
11686 | __sched_group_set_shares(tg, NICE_0_LOAD); | |
11687 | ||
029632fb PZ |
11688 | mutex_unlock(&shares_mutex); |
11689 | return 0; | |
11690 | } | |
30400039 | 11691 | |
029632fb PZ |
11692 | #else /* CONFIG_FAIR_GROUP_SCHED */ |
11693 | ||
11694 | void free_fair_sched_group(struct task_group *tg) { } | |
11695 | ||
11696 | int alloc_fair_sched_group(struct task_group *tg, struct task_group *parent) | |
11697 | { | |
11698 | return 1; | |
11699 | } | |
11700 | ||
8663e24d PZ |
11701 | void online_fair_sched_group(struct task_group *tg) { } |
11702 | ||
6fe1f348 | 11703 | void unregister_fair_sched_group(struct task_group *tg) { } |
029632fb PZ |
11704 | |
11705 | #endif /* CONFIG_FAIR_GROUP_SCHED */ | |
11706 | ||
810b3817 | 11707 | |
6d686f45 | 11708 | static unsigned int get_rr_interval_fair(struct rq *rq, struct task_struct *task) |
0d721cea PW |
11709 | { |
11710 | struct sched_entity *se = &task->se; | |
0d721cea PW |
11711 | unsigned int rr_interval = 0; |
11712 | ||
11713 | /* | |
11714 | * Time slice is 0 for SCHED_OTHER tasks that are on an otherwise | |
11715 | * idle runqueue: | |
11716 | */ | |
0d721cea | 11717 | if (rq->cfs.load.weight) |
a59f4e07 | 11718 | rr_interval = NS_TO_JIFFIES(sched_slice(cfs_rq_of(se), se)); |
0d721cea PW |
11719 | |
11720 | return rr_interval; | |
11721 | } | |
11722 | ||
bf0f6f24 IM |
11723 | /* |
11724 | * All the scheduling class methods: | |
11725 | */ | |
43c31ac0 PZ |
11726 | DEFINE_SCHED_CLASS(fair) = { |
11727 | ||
bf0f6f24 IM |
11728 | .enqueue_task = enqueue_task_fair, |
11729 | .dequeue_task = dequeue_task_fair, | |
11730 | .yield_task = yield_task_fair, | |
d95f4122 | 11731 | .yield_to_task = yield_to_task_fair, |
bf0f6f24 | 11732 | |
2e09bf55 | 11733 | .check_preempt_curr = check_preempt_wakeup, |
bf0f6f24 | 11734 | |
98c2f700 | 11735 | .pick_next_task = __pick_next_task_fair, |
bf0f6f24 | 11736 | .put_prev_task = put_prev_task_fair, |
03b7fad1 | 11737 | .set_next_task = set_next_task_fair, |
bf0f6f24 | 11738 | |
681f3e68 | 11739 | #ifdef CONFIG_SMP |
6e2df058 | 11740 | .balance = balance_fair, |
21f56ffe | 11741 | .pick_task = pick_task_fair, |
4ce72a2c | 11742 | .select_task_rq = select_task_rq_fair, |
0a74bef8 | 11743 | .migrate_task_rq = migrate_task_rq_fair, |
141965c7 | 11744 | |
0bcdcf28 CE |
11745 | .rq_online = rq_online_fair, |
11746 | .rq_offline = rq_offline_fair, | |
88ec22d3 | 11747 | |
12695578 | 11748 | .task_dead = task_dead_fair, |
c5b28038 | 11749 | .set_cpus_allowed = set_cpus_allowed_common, |
681f3e68 | 11750 | #endif |
bf0f6f24 | 11751 | |
bf0f6f24 | 11752 | .task_tick = task_tick_fair, |
cd29fe6f | 11753 | .task_fork = task_fork_fair, |
cb469845 SR |
11754 | |
11755 | .prio_changed = prio_changed_fair, | |
da7a735e | 11756 | .switched_from = switched_from_fair, |
cb469845 | 11757 | .switched_to = switched_to_fair, |
810b3817 | 11758 | |
0d721cea PW |
11759 | .get_rr_interval = get_rr_interval_fair, |
11760 | ||
6e998916 SG |
11761 | .update_curr = update_curr_fair, |
11762 | ||
810b3817 | 11763 | #ifdef CONFIG_FAIR_GROUP_SCHED |
ea86cb4b | 11764 | .task_change_group = task_change_group_fair, |
810b3817 | 11765 | #endif |
982d9cdc PB |
11766 | |
11767 | #ifdef CONFIG_UCLAMP_TASK | |
11768 | .uclamp_enabled = 1, | |
11769 | #endif | |
bf0f6f24 IM |
11770 | }; |
11771 | ||
11772 | #ifdef CONFIG_SCHED_DEBUG | |
029632fb | 11773 | void print_cfs_stats(struct seq_file *m, int cpu) |
bf0f6f24 | 11774 | { |
039ae8bc | 11775 | struct cfs_rq *cfs_rq, *pos; |
bf0f6f24 | 11776 | |
5973e5b9 | 11777 | rcu_read_lock(); |
039ae8bc | 11778 | for_each_leaf_cfs_rq_safe(cpu_rq(cpu), cfs_rq, pos) |
5cef9eca | 11779 | print_cfs_rq(m, cpu, cfs_rq); |
5973e5b9 | 11780 | rcu_read_unlock(); |
bf0f6f24 | 11781 | } |
397f2378 SD |
11782 | |
11783 | #ifdef CONFIG_NUMA_BALANCING | |
11784 | void show_numa_stats(struct task_struct *p, struct seq_file *m) | |
11785 | { | |
11786 | int node; | |
11787 | unsigned long tsf = 0, tpf = 0, gsf = 0, gpf = 0; | |
cb361d8c | 11788 | struct numa_group *ng; |
397f2378 | 11789 | |
cb361d8c JH |
11790 | rcu_read_lock(); |
11791 | ng = rcu_dereference(p->numa_group); | |
397f2378 SD |
11792 | for_each_online_node(node) { |
11793 | if (p->numa_faults) { | |
11794 | tsf = p->numa_faults[task_faults_idx(NUMA_MEM, node, 0)]; | |
11795 | tpf = p->numa_faults[task_faults_idx(NUMA_MEM, node, 1)]; | |
11796 | } | |
cb361d8c JH |
11797 | if (ng) { |
11798 | gsf = ng->faults[task_faults_idx(NUMA_MEM, node, 0)], | |
11799 | gpf = ng->faults[task_faults_idx(NUMA_MEM, node, 1)]; | |
397f2378 SD |
11800 | } |
11801 | print_numa_stats(m, node, tsf, tpf, gsf, gpf); | |
11802 | } | |
cb361d8c | 11803 | rcu_read_unlock(); |
397f2378 SD |
11804 | } |
11805 | #endif /* CONFIG_NUMA_BALANCING */ | |
11806 | #endif /* CONFIG_SCHED_DEBUG */ | |
029632fb PZ |
11807 | |
11808 | __init void init_sched_fair_class(void) | |
11809 | { | |
11810 | #ifdef CONFIG_SMP | |
11811 | open_softirq(SCHED_SOFTIRQ, run_rebalance_domains); | |
11812 | ||
3451d024 | 11813 | #ifdef CONFIG_NO_HZ_COMMON |
554cecaf | 11814 | nohz.next_balance = jiffies; |
f643ea22 | 11815 | nohz.next_blocked = jiffies; |
029632fb | 11816 | zalloc_cpumask_var(&nohz.idle_cpus_mask, GFP_NOWAIT); |
029632fb PZ |
11817 | #endif |
11818 | #endif /* SMP */ | |
11819 | ||
11820 | } | |
3c93a0c0 QY |
11821 | |
11822 | /* | |
11823 | * Helper functions to facilitate extracting info from tracepoints. | |
11824 | */ | |
11825 | ||
11826 | const struct sched_avg *sched_trace_cfs_rq_avg(struct cfs_rq *cfs_rq) | |
11827 | { | |
11828 | #ifdef CONFIG_SMP | |
11829 | return cfs_rq ? &cfs_rq->avg : NULL; | |
11830 | #else | |
11831 | return NULL; | |
11832 | #endif | |
11833 | } | |
11834 | EXPORT_SYMBOL_GPL(sched_trace_cfs_rq_avg); | |
11835 | ||
11836 | char *sched_trace_cfs_rq_path(struct cfs_rq *cfs_rq, char *str, int len) | |
11837 | { | |
11838 | if (!cfs_rq) { | |
11839 | if (str) | |
11840 | strlcpy(str, "(null)", len); | |
11841 | else | |
11842 | return NULL; | |
11843 | } | |
11844 | ||
11845 | cfs_rq_tg_path(cfs_rq, str, len); | |
11846 | return str; | |
11847 | } | |
11848 | EXPORT_SYMBOL_GPL(sched_trace_cfs_rq_path); | |
11849 | ||
11850 | int sched_trace_cfs_rq_cpu(struct cfs_rq *cfs_rq) | |
11851 | { | |
11852 | return cfs_rq ? cpu_of(rq_of(cfs_rq)) : -1; | |
11853 | } | |
11854 | EXPORT_SYMBOL_GPL(sched_trace_cfs_rq_cpu); | |
11855 | ||
11856 | const struct sched_avg *sched_trace_rq_avg_rt(struct rq *rq) | |
11857 | { | |
11858 | #ifdef CONFIG_SMP | |
11859 | return rq ? &rq->avg_rt : NULL; | |
11860 | #else | |
11861 | return NULL; | |
11862 | #endif | |
11863 | } | |
11864 | EXPORT_SYMBOL_GPL(sched_trace_rq_avg_rt); | |
11865 | ||
11866 | const struct sched_avg *sched_trace_rq_avg_dl(struct rq *rq) | |
11867 | { | |
11868 | #ifdef CONFIG_SMP | |
11869 | return rq ? &rq->avg_dl : NULL; | |
11870 | #else | |
11871 | return NULL; | |
11872 | #endif | |
11873 | } | |
11874 | EXPORT_SYMBOL_GPL(sched_trace_rq_avg_dl); | |
11875 | ||
11876 | const struct sched_avg *sched_trace_rq_avg_irq(struct rq *rq) | |
11877 | { | |
11878 | #if defined(CONFIG_SMP) && defined(CONFIG_HAVE_SCHED_AVG_IRQ) | |
11879 | return rq ? &rq->avg_irq : NULL; | |
11880 | #else | |
11881 | return NULL; | |
11882 | #endif | |
11883 | } | |
11884 | EXPORT_SYMBOL_GPL(sched_trace_rq_avg_irq); | |
11885 | ||
11886 | int sched_trace_rq_cpu(struct rq *rq) | |
11887 | { | |
11888 | return rq ? cpu_of(rq) : -1; | |
11889 | } | |
11890 | EXPORT_SYMBOL_GPL(sched_trace_rq_cpu); | |
11891 | ||
51cf18c9 VD |
11892 | int sched_trace_rq_cpu_capacity(struct rq *rq) |
11893 | { | |
11894 | return rq ? | |
11895 | #ifdef CONFIG_SMP | |
11896 | rq->cpu_capacity | |
11897 | #else | |
11898 | SCHED_CAPACITY_SCALE | |
11899 | #endif | |
11900 | : -1; | |
11901 | } | |
11902 | EXPORT_SYMBOL_GPL(sched_trace_rq_cpu_capacity); | |
11903 | ||
3c93a0c0 QY |
11904 | const struct cpumask *sched_trace_rd_span(struct root_domain *rd) |
11905 | { | |
11906 | #ifdef CONFIG_SMP | |
11907 | return rd ? rd->span : NULL; | |
11908 | #else | |
11909 | return NULL; | |
11910 | #endif | |
11911 | } | |
11912 | EXPORT_SYMBOL_GPL(sched_trace_rd_span); | |
9d246053 PA |
11913 | |
11914 | int sched_trace_rq_nr_running(struct rq *rq) | |
11915 | { | |
11916 | return rq ? rq->nr_running : -1; | |
11917 | } | |
11918 | EXPORT_SYMBOL_GPL(sched_trace_rq_nr_running); |