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 PZ |
24 | |
25 | #include <trace/events/sched.h> | |
26 | ||
bf0f6f24 | 27 | /* |
21805085 | 28 | * Targeted preemption latency for CPU-bound tasks: |
bf0f6f24 | 29 | * |
21805085 | 30 | * NOTE: this latency value is not the same as the concept of |
d274a4ce IM |
31 | * 'timeslice length' - timeslices in CFS are of variable length |
32 | * and have no persistent notion like in traditional, time-slice | |
33 | * based scheduling concepts. | |
bf0f6f24 | 34 | * |
d274a4ce IM |
35 | * (to see the precise effective timeslice length of your workload, |
36 | * run vmstat and monitor the context-switches (cs) field) | |
2b4d5b25 IM |
37 | * |
38 | * (default: 6ms * (1 + ilog(ncpus)), units: nanoseconds) | |
bf0f6f24 | 39 | */ |
2b4d5b25 | 40 | unsigned int sysctl_sched_latency = 6000000ULL; |
ed8885a1 | 41 | static unsigned int normalized_sysctl_sched_latency = 6000000ULL; |
2bd8e6d4 | 42 | |
1983a922 CE |
43 | /* |
44 | * The initial- and re-scaling of tunables is configurable | |
1983a922 CE |
45 | * |
46 | * Options are: | |
2b4d5b25 IM |
47 | * |
48 | * SCHED_TUNABLESCALING_NONE - unscaled, always *1 | |
49 | * SCHED_TUNABLESCALING_LOG - scaled logarithmical, *1+ilog(ncpus) | |
50 | * SCHED_TUNABLESCALING_LINEAR - scaled linear, *ncpus | |
51 | * | |
52 | * (default SCHED_TUNABLESCALING_LOG = *(1+ilog(ncpus)) | |
1983a922 | 53 | */ |
2b4d5b25 | 54 | enum sched_tunable_scaling sysctl_sched_tunable_scaling = SCHED_TUNABLESCALING_LOG; |
1983a922 | 55 | |
2bd8e6d4 | 56 | /* |
b2be5e96 | 57 | * Minimal preemption granularity for CPU-bound tasks: |
2b4d5b25 | 58 | * |
864616ee | 59 | * (default: 0.75 msec * (1 + ilog(ncpus)), units: nanoseconds) |
2bd8e6d4 | 60 | */ |
ed8885a1 MS |
61 | unsigned int sysctl_sched_min_granularity = 750000ULL; |
62 | static unsigned int normalized_sysctl_sched_min_granularity = 750000ULL; | |
21805085 PZ |
63 | |
64 | /* | |
2b4d5b25 | 65 | * This value is kept at sysctl_sched_latency/sysctl_sched_min_granularity |
b2be5e96 | 66 | */ |
0bf377bb | 67 | static unsigned int sched_nr_latency = 8; |
b2be5e96 PZ |
68 | |
69 | /* | |
2bba22c5 | 70 | * After fork, child runs first. If set to 0 (default) then |
b2be5e96 | 71 | * parent will (try to) run first. |
21805085 | 72 | */ |
2bba22c5 | 73 | unsigned int sysctl_sched_child_runs_first __read_mostly; |
bf0f6f24 | 74 | |
bf0f6f24 IM |
75 | /* |
76 | * SCHED_OTHER wake-up granularity. | |
bf0f6f24 IM |
77 | * |
78 | * This option delays the preemption effects of decoupled workloads | |
79 | * and reduces their over-scheduling. Synchronous workloads will still | |
80 | * have immediate wakeup/sleep latencies. | |
2b4d5b25 IM |
81 | * |
82 | * (default: 1 msec * (1 + ilog(ncpus)), units: nanoseconds) | |
bf0f6f24 | 83 | */ |
ed8885a1 MS |
84 | unsigned int sysctl_sched_wakeup_granularity = 1000000UL; |
85 | static unsigned int normalized_sysctl_sched_wakeup_granularity = 1000000UL; | |
bf0f6f24 | 86 | |
2b4d5b25 | 87 | const_debug unsigned int sysctl_sched_migration_cost = 500000UL; |
da84d961 | 88 | |
afe06efd TC |
89 | #ifdef CONFIG_SMP |
90 | /* | |
97fb7a0a | 91 | * For asym packing, by default the lower numbered CPU has higher priority. |
afe06efd TC |
92 | */ |
93 | int __weak arch_asym_cpu_priority(int cpu) | |
94 | { | |
95 | return -cpu; | |
96 | } | |
6d101ba6 OJ |
97 | |
98 | /* | |
99 | * The margin used when comparing utilization with CPU capacity: | |
100 | * util * margin < capacity * 1024 | |
101 | * | |
102 | * (default: ~20%) | |
103 | */ | |
104 | static unsigned int capacity_margin = 1280; | |
afe06efd TC |
105 | #endif |
106 | ||
ec12cb7f PT |
107 | #ifdef CONFIG_CFS_BANDWIDTH |
108 | /* | |
109 | * Amount of runtime to allocate from global (tg) to local (per-cfs_rq) pool | |
110 | * each time a cfs_rq requests quota. | |
111 | * | |
112 | * Note: in the case that the slice exceeds the runtime remaining (either due | |
113 | * to consumption or the quota being specified to be smaller than the slice) | |
114 | * we will always only issue the remaining available time. | |
115 | * | |
2b4d5b25 IM |
116 | * (default: 5 msec, units: microseconds) |
117 | */ | |
118 | unsigned int sysctl_sched_cfs_bandwidth_slice = 5000UL; | |
ec12cb7f PT |
119 | #endif |
120 | ||
8527632d PG |
121 | static inline void update_load_add(struct load_weight *lw, unsigned long inc) |
122 | { | |
123 | lw->weight += inc; | |
124 | lw->inv_weight = 0; | |
125 | } | |
126 | ||
127 | static inline void update_load_sub(struct load_weight *lw, unsigned long dec) | |
128 | { | |
129 | lw->weight -= dec; | |
130 | lw->inv_weight = 0; | |
131 | } | |
132 | ||
133 | static inline void update_load_set(struct load_weight *lw, unsigned long w) | |
134 | { | |
135 | lw->weight = w; | |
136 | lw->inv_weight = 0; | |
137 | } | |
138 | ||
029632fb PZ |
139 | /* |
140 | * Increase the granularity value when there are more CPUs, | |
141 | * because with more CPUs the 'effective latency' as visible | |
142 | * to users decreases. But the relationship is not linear, | |
143 | * so pick a second-best guess by going with the log2 of the | |
144 | * number of CPUs. | |
145 | * | |
146 | * This idea comes from the SD scheduler of Con Kolivas: | |
147 | */ | |
58ac93e4 | 148 | static unsigned int get_update_sysctl_factor(void) |
029632fb | 149 | { |
58ac93e4 | 150 | unsigned int cpus = min_t(unsigned int, num_online_cpus(), 8); |
029632fb PZ |
151 | unsigned int factor; |
152 | ||
153 | switch (sysctl_sched_tunable_scaling) { | |
154 | case SCHED_TUNABLESCALING_NONE: | |
155 | factor = 1; | |
156 | break; | |
157 | case SCHED_TUNABLESCALING_LINEAR: | |
158 | factor = cpus; | |
159 | break; | |
160 | case SCHED_TUNABLESCALING_LOG: | |
161 | default: | |
162 | factor = 1 + ilog2(cpus); | |
163 | break; | |
164 | } | |
165 | ||
166 | return factor; | |
167 | } | |
168 | ||
169 | static void update_sysctl(void) | |
170 | { | |
171 | unsigned int factor = get_update_sysctl_factor(); | |
172 | ||
173 | #define SET_SYSCTL(name) \ | |
174 | (sysctl_##name = (factor) * normalized_sysctl_##name) | |
175 | SET_SYSCTL(sched_min_granularity); | |
176 | SET_SYSCTL(sched_latency); | |
177 | SET_SYSCTL(sched_wakeup_granularity); | |
178 | #undef SET_SYSCTL | |
179 | } | |
180 | ||
181 | void sched_init_granularity(void) | |
182 | { | |
183 | update_sysctl(); | |
184 | } | |
185 | ||
9dbdb155 | 186 | #define WMULT_CONST (~0U) |
029632fb PZ |
187 | #define WMULT_SHIFT 32 |
188 | ||
9dbdb155 PZ |
189 | static void __update_inv_weight(struct load_weight *lw) |
190 | { | |
191 | unsigned long w; | |
192 | ||
193 | if (likely(lw->inv_weight)) | |
194 | return; | |
195 | ||
196 | w = scale_load_down(lw->weight); | |
197 | ||
198 | if (BITS_PER_LONG > 32 && unlikely(w >= WMULT_CONST)) | |
199 | lw->inv_weight = 1; | |
200 | else if (unlikely(!w)) | |
201 | lw->inv_weight = WMULT_CONST; | |
202 | else | |
203 | lw->inv_weight = WMULT_CONST / w; | |
204 | } | |
029632fb PZ |
205 | |
206 | /* | |
9dbdb155 PZ |
207 | * delta_exec * weight / lw.weight |
208 | * OR | |
209 | * (delta_exec * (weight * lw->inv_weight)) >> WMULT_SHIFT | |
210 | * | |
1c3de5e1 | 211 | * Either weight := NICE_0_LOAD and lw \e sched_prio_to_wmult[], in which case |
9dbdb155 PZ |
212 | * we're guaranteed shift stays positive because inv_weight is guaranteed to |
213 | * fit 32 bits, and NICE_0_LOAD gives another 10 bits; therefore shift >= 22. | |
214 | * | |
215 | * Or, weight =< lw.weight (because lw.weight is the runqueue weight), thus | |
216 | * weight/lw.weight <= 1, and therefore our shift will also be positive. | |
029632fb | 217 | */ |
9dbdb155 | 218 | static u64 __calc_delta(u64 delta_exec, unsigned long weight, struct load_weight *lw) |
029632fb | 219 | { |
9dbdb155 PZ |
220 | u64 fact = scale_load_down(weight); |
221 | int shift = WMULT_SHIFT; | |
029632fb | 222 | |
9dbdb155 | 223 | __update_inv_weight(lw); |
029632fb | 224 | |
9dbdb155 PZ |
225 | if (unlikely(fact >> 32)) { |
226 | while (fact >> 32) { | |
227 | fact >>= 1; | |
228 | shift--; | |
229 | } | |
029632fb PZ |
230 | } |
231 | ||
9dbdb155 PZ |
232 | /* hint to use a 32x32->64 mul */ |
233 | fact = (u64)(u32)fact * lw->inv_weight; | |
029632fb | 234 | |
9dbdb155 PZ |
235 | while (fact >> 32) { |
236 | fact >>= 1; | |
237 | shift--; | |
238 | } | |
029632fb | 239 | |
9dbdb155 | 240 | return mul_u64_u32_shr(delta_exec, fact, shift); |
029632fb PZ |
241 | } |
242 | ||
243 | ||
244 | const struct sched_class fair_sched_class; | |
a4c2f00f | 245 | |
bf0f6f24 IM |
246 | /************************************************************** |
247 | * CFS operations on generic schedulable entities: | |
248 | */ | |
249 | ||
62160e3f | 250 | #ifdef CONFIG_FAIR_GROUP_SCHED |
8f48894f PZ |
251 | static inline struct task_struct *task_of(struct sched_entity *se) |
252 | { | |
9148a3a1 | 253 | SCHED_WARN_ON(!entity_is_task(se)); |
8f48894f PZ |
254 | return container_of(se, struct task_struct, se); |
255 | } | |
256 | ||
b758149c PZ |
257 | /* Walk up scheduling entities hierarchy */ |
258 | #define for_each_sched_entity(se) \ | |
259 | for (; se; se = se->parent) | |
260 | ||
261 | static inline struct cfs_rq *task_cfs_rq(struct task_struct *p) | |
262 | { | |
263 | return p->se.cfs_rq; | |
264 | } | |
265 | ||
266 | /* runqueue on which this entity is (to be) queued */ | |
267 | static inline struct cfs_rq *cfs_rq_of(struct sched_entity *se) | |
268 | { | |
269 | return se->cfs_rq; | |
270 | } | |
271 | ||
272 | /* runqueue "owned" by this group */ | |
273 | static inline struct cfs_rq *group_cfs_rq(struct sched_entity *grp) | |
274 | { | |
275 | return grp->my_q; | |
276 | } | |
277 | ||
f6783319 | 278 | static inline bool list_add_leaf_cfs_rq(struct cfs_rq *cfs_rq) |
3d4b47b4 | 279 | { |
5d299eab PZ |
280 | struct rq *rq = rq_of(cfs_rq); |
281 | int cpu = cpu_of(rq); | |
282 | ||
283 | if (cfs_rq->on_list) | |
f6783319 | 284 | return rq->tmp_alone_branch == &rq->leaf_cfs_rq_list; |
5d299eab PZ |
285 | |
286 | cfs_rq->on_list = 1; | |
287 | ||
288 | /* | |
289 | * Ensure we either appear before our parent (if already | |
290 | * enqueued) or force our parent to appear after us when it is | |
291 | * enqueued. The fact that we always enqueue bottom-up | |
292 | * reduces this to two cases and a special case for the root | |
293 | * cfs_rq. Furthermore, it also means that we will always reset | |
294 | * tmp_alone_branch either when the branch is connected | |
295 | * to a tree or when we reach the top of the tree | |
296 | */ | |
297 | if (cfs_rq->tg->parent && | |
298 | cfs_rq->tg->parent->cfs_rq[cpu]->on_list) { | |
67e86250 | 299 | /* |
5d299eab PZ |
300 | * If parent is already on the list, we add the child |
301 | * just before. Thanks to circular linked property of | |
302 | * the list, this means to put the child at the tail | |
303 | * of the list that starts by parent. | |
67e86250 | 304 | */ |
5d299eab PZ |
305 | list_add_tail_rcu(&cfs_rq->leaf_cfs_rq_list, |
306 | &(cfs_rq->tg->parent->cfs_rq[cpu]->leaf_cfs_rq_list)); | |
307 | /* | |
308 | * The branch is now connected to its tree so we can | |
309 | * reset tmp_alone_branch to the beginning of the | |
310 | * list. | |
311 | */ | |
312 | rq->tmp_alone_branch = &rq->leaf_cfs_rq_list; | |
f6783319 | 313 | return true; |
5d299eab | 314 | } |
3d4b47b4 | 315 | |
5d299eab PZ |
316 | if (!cfs_rq->tg->parent) { |
317 | /* | |
318 | * cfs rq without parent should be put | |
319 | * at the tail of the list. | |
320 | */ | |
321 | list_add_tail_rcu(&cfs_rq->leaf_cfs_rq_list, | |
322 | &rq->leaf_cfs_rq_list); | |
323 | /* | |
324 | * We have reach the top of a tree so we can reset | |
325 | * tmp_alone_branch to the beginning of the list. | |
326 | */ | |
327 | rq->tmp_alone_branch = &rq->leaf_cfs_rq_list; | |
f6783319 | 328 | return true; |
3d4b47b4 | 329 | } |
5d299eab PZ |
330 | |
331 | /* | |
332 | * The parent has not already been added so we want to | |
333 | * make sure that it will be put after us. | |
334 | * tmp_alone_branch points to the begin of the branch | |
335 | * where we will add parent. | |
336 | */ | |
337 | list_add_rcu(&cfs_rq->leaf_cfs_rq_list, rq->tmp_alone_branch); | |
338 | /* | |
339 | * update tmp_alone_branch to points to the new begin | |
340 | * of the branch | |
341 | */ | |
342 | rq->tmp_alone_branch = &cfs_rq->leaf_cfs_rq_list; | |
f6783319 | 343 | return false; |
3d4b47b4 PZ |
344 | } |
345 | ||
346 | static inline void list_del_leaf_cfs_rq(struct cfs_rq *cfs_rq) | |
347 | { | |
348 | if (cfs_rq->on_list) { | |
31bc6aea VG |
349 | struct rq *rq = rq_of(cfs_rq); |
350 | ||
351 | /* | |
352 | * With cfs_rq being unthrottled/throttled during an enqueue, | |
353 | * it can happen the tmp_alone_branch points the a leaf that | |
354 | * we finally want to del. In this case, tmp_alone_branch moves | |
355 | * to the prev element but it will point to rq->leaf_cfs_rq_list | |
356 | * at the end of the enqueue. | |
357 | */ | |
358 | if (rq->tmp_alone_branch == &cfs_rq->leaf_cfs_rq_list) | |
359 | rq->tmp_alone_branch = cfs_rq->leaf_cfs_rq_list.prev; | |
360 | ||
3d4b47b4 PZ |
361 | list_del_rcu(&cfs_rq->leaf_cfs_rq_list); |
362 | cfs_rq->on_list = 0; | |
363 | } | |
364 | } | |
365 | ||
5d299eab PZ |
366 | static inline void assert_list_leaf_cfs_rq(struct rq *rq) |
367 | { | |
368 | SCHED_WARN_ON(rq->tmp_alone_branch != &rq->leaf_cfs_rq_list); | |
369 | } | |
370 | ||
039ae8bc VG |
371 | /* Iterate thr' all leaf cfs_rq's on a runqueue */ |
372 | #define for_each_leaf_cfs_rq_safe(rq, cfs_rq, pos) \ | |
373 | list_for_each_entry_safe(cfs_rq, pos, &rq->leaf_cfs_rq_list, \ | |
374 | leaf_cfs_rq_list) | |
b758149c PZ |
375 | |
376 | /* Do the two (enqueued) entities belong to the same group ? */ | |
fed14d45 | 377 | static inline struct cfs_rq * |
b758149c PZ |
378 | is_same_group(struct sched_entity *se, struct sched_entity *pse) |
379 | { | |
380 | if (se->cfs_rq == pse->cfs_rq) | |
fed14d45 | 381 | return se->cfs_rq; |
b758149c | 382 | |
fed14d45 | 383 | return NULL; |
b758149c PZ |
384 | } |
385 | ||
386 | static inline struct sched_entity *parent_entity(struct sched_entity *se) | |
387 | { | |
388 | return se->parent; | |
389 | } | |
390 | ||
464b7527 PZ |
391 | static void |
392 | find_matching_se(struct sched_entity **se, struct sched_entity **pse) | |
393 | { | |
394 | int se_depth, pse_depth; | |
395 | ||
396 | /* | |
397 | * preemption test can be made between sibling entities who are in the | |
398 | * same cfs_rq i.e who have a common parent. Walk up the hierarchy of | |
399 | * both tasks until we find their ancestors who are siblings of common | |
400 | * parent. | |
401 | */ | |
402 | ||
403 | /* First walk up until both entities are at same depth */ | |
fed14d45 PZ |
404 | se_depth = (*se)->depth; |
405 | pse_depth = (*pse)->depth; | |
464b7527 PZ |
406 | |
407 | while (se_depth > pse_depth) { | |
408 | se_depth--; | |
409 | *se = parent_entity(*se); | |
410 | } | |
411 | ||
412 | while (pse_depth > se_depth) { | |
413 | pse_depth--; | |
414 | *pse = parent_entity(*pse); | |
415 | } | |
416 | ||
417 | while (!is_same_group(*se, *pse)) { | |
418 | *se = parent_entity(*se); | |
419 | *pse = parent_entity(*pse); | |
420 | } | |
421 | } | |
422 | ||
8f48894f PZ |
423 | #else /* !CONFIG_FAIR_GROUP_SCHED */ |
424 | ||
425 | static inline struct task_struct *task_of(struct sched_entity *se) | |
426 | { | |
427 | return container_of(se, struct task_struct, se); | |
428 | } | |
bf0f6f24 | 429 | |
b758149c PZ |
430 | #define for_each_sched_entity(se) \ |
431 | for (; se; se = NULL) | |
bf0f6f24 | 432 | |
b758149c | 433 | static inline struct cfs_rq *task_cfs_rq(struct task_struct *p) |
bf0f6f24 | 434 | { |
b758149c | 435 | return &task_rq(p)->cfs; |
bf0f6f24 IM |
436 | } |
437 | ||
b758149c PZ |
438 | static inline struct cfs_rq *cfs_rq_of(struct sched_entity *se) |
439 | { | |
440 | struct task_struct *p = task_of(se); | |
441 | struct rq *rq = task_rq(p); | |
442 | ||
443 | return &rq->cfs; | |
444 | } | |
445 | ||
446 | /* runqueue "owned" by this group */ | |
447 | static inline struct cfs_rq *group_cfs_rq(struct sched_entity *grp) | |
448 | { | |
449 | return NULL; | |
450 | } | |
451 | ||
f6783319 | 452 | static inline bool list_add_leaf_cfs_rq(struct cfs_rq *cfs_rq) |
3d4b47b4 | 453 | { |
f6783319 | 454 | return true; |
3d4b47b4 PZ |
455 | } |
456 | ||
457 | static inline void list_del_leaf_cfs_rq(struct cfs_rq *cfs_rq) | |
458 | { | |
459 | } | |
460 | ||
5d299eab PZ |
461 | static inline void assert_list_leaf_cfs_rq(struct rq *rq) |
462 | { | |
463 | } | |
464 | ||
039ae8bc VG |
465 | #define for_each_leaf_cfs_rq_safe(rq, cfs_rq, pos) \ |
466 | for (cfs_rq = &rq->cfs, pos = NULL; cfs_rq; cfs_rq = pos) | |
b758149c | 467 | |
b758149c PZ |
468 | static inline struct sched_entity *parent_entity(struct sched_entity *se) |
469 | { | |
470 | return NULL; | |
471 | } | |
472 | ||
464b7527 PZ |
473 | static inline void |
474 | find_matching_se(struct sched_entity **se, struct sched_entity **pse) | |
475 | { | |
476 | } | |
477 | ||
b758149c PZ |
478 | #endif /* CONFIG_FAIR_GROUP_SCHED */ |
479 | ||
6c16a6dc | 480 | static __always_inline |
9dbdb155 | 481 | void account_cfs_rq_runtime(struct cfs_rq *cfs_rq, u64 delta_exec); |
bf0f6f24 IM |
482 | |
483 | /************************************************************** | |
484 | * Scheduling class tree data structure manipulation methods: | |
485 | */ | |
486 | ||
1bf08230 | 487 | static inline u64 max_vruntime(u64 max_vruntime, u64 vruntime) |
02e0431a | 488 | { |
1bf08230 | 489 | s64 delta = (s64)(vruntime - max_vruntime); |
368059a9 | 490 | if (delta > 0) |
1bf08230 | 491 | max_vruntime = vruntime; |
02e0431a | 492 | |
1bf08230 | 493 | return max_vruntime; |
02e0431a PZ |
494 | } |
495 | ||
0702e3eb | 496 | static inline u64 min_vruntime(u64 min_vruntime, u64 vruntime) |
b0ffd246 PZ |
497 | { |
498 | s64 delta = (s64)(vruntime - min_vruntime); | |
499 | if (delta < 0) | |
500 | min_vruntime = vruntime; | |
501 | ||
502 | return min_vruntime; | |
503 | } | |
504 | ||
54fdc581 FC |
505 | static inline int entity_before(struct sched_entity *a, |
506 | struct sched_entity *b) | |
507 | { | |
508 | return (s64)(a->vruntime - b->vruntime) < 0; | |
509 | } | |
510 | ||
1af5f730 PZ |
511 | static void update_min_vruntime(struct cfs_rq *cfs_rq) |
512 | { | |
b60205c7 | 513 | struct sched_entity *curr = cfs_rq->curr; |
bfb06889 | 514 | struct rb_node *leftmost = rb_first_cached(&cfs_rq->tasks_timeline); |
b60205c7 | 515 | |
1af5f730 PZ |
516 | u64 vruntime = cfs_rq->min_vruntime; |
517 | ||
b60205c7 PZ |
518 | if (curr) { |
519 | if (curr->on_rq) | |
520 | vruntime = curr->vruntime; | |
521 | else | |
522 | curr = NULL; | |
523 | } | |
1af5f730 | 524 | |
bfb06889 DB |
525 | if (leftmost) { /* non-empty tree */ |
526 | struct sched_entity *se; | |
527 | se = rb_entry(leftmost, struct sched_entity, run_node); | |
1af5f730 | 528 | |
b60205c7 | 529 | if (!curr) |
1af5f730 PZ |
530 | vruntime = se->vruntime; |
531 | else | |
532 | vruntime = min_vruntime(vruntime, se->vruntime); | |
533 | } | |
534 | ||
1bf08230 | 535 | /* ensure we never gain time by being placed backwards. */ |
1af5f730 | 536 | cfs_rq->min_vruntime = max_vruntime(cfs_rq->min_vruntime, vruntime); |
3fe1698b PZ |
537 | #ifndef CONFIG_64BIT |
538 | smp_wmb(); | |
539 | cfs_rq->min_vruntime_copy = cfs_rq->min_vruntime; | |
540 | #endif | |
1af5f730 PZ |
541 | } |
542 | ||
bf0f6f24 IM |
543 | /* |
544 | * Enqueue an entity into the rb-tree: | |
545 | */ | |
0702e3eb | 546 | static void __enqueue_entity(struct cfs_rq *cfs_rq, struct sched_entity *se) |
bf0f6f24 | 547 | { |
bfb06889 | 548 | struct rb_node **link = &cfs_rq->tasks_timeline.rb_root.rb_node; |
bf0f6f24 IM |
549 | struct rb_node *parent = NULL; |
550 | struct sched_entity *entry; | |
bfb06889 | 551 | bool leftmost = true; |
bf0f6f24 IM |
552 | |
553 | /* | |
554 | * Find the right place in the rbtree: | |
555 | */ | |
556 | while (*link) { | |
557 | parent = *link; | |
558 | entry = rb_entry(parent, struct sched_entity, run_node); | |
559 | /* | |
560 | * We dont care about collisions. Nodes with | |
561 | * the same key stay together. | |
562 | */ | |
2bd2d6f2 | 563 | if (entity_before(se, entry)) { |
bf0f6f24 IM |
564 | link = &parent->rb_left; |
565 | } else { | |
566 | link = &parent->rb_right; | |
bfb06889 | 567 | leftmost = false; |
bf0f6f24 IM |
568 | } |
569 | } | |
570 | ||
bf0f6f24 | 571 | rb_link_node(&se->run_node, parent, link); |
bfb06889 DB |
572 | rb_insert_color_cached(&se->run_node, |
573 | &cfs_rq->tasks_timeline, leftmost); | |
bf0f6f24 IM |
574 | } |
575 | ||
0702e3eb | 576 | static void __dequeue_entity(struct cfs_rq *cfs_rq, struct sched_entity *se) |
bf0f6f24 | 577 | { |
bfb06889 | 578 | rb_erase_cached(&se->run_node, &cfs_rq->tasks_timeline); |
bf0f6f24 IM |
579 | } |
580 | ||
029632fb | 581 | struct sched_entity *__pick_first_entity(struct cfs_rq *cfs_rq) |
bf0f6f24 | 582 | { |
bfb06889 | 583 | struct rb_node *left = rb_first_cached(&cfs_rq->tasks_timeline); |
f4b6755f PZ |
584 | |
585 | if (!left) | |
586 | return NULL; | |
587 | ||
588 | return rb_entry(left, struct sched_entity, run_node); | |
bf0f6f24 IM |
589 | } |
590 | ||
ac53db59 RR |
591 | static struct sched_entity *__pick_next_entity(struct sched_entity *se) |
592 | { | |
593 | struct rb_node *next = rb_next(&se->run_node); | |
594 | ||
595 | if (!next) | |
596 | return NULL; | |
597 | ||
598 | return rb_entry(next, struct sched_entity, run_node); | |
599 | } | |
600 | ||
601 | #ifdef CONFIG_SCHED_DEBUG | |
029632fb | 602 | struct sched_entity *__pick_last_entity(struct cfs_rq *cfs_rq) |
aeb73b04 | 603 | { |
bfb06889 | 604 | struct rb_node *last = rb_last(&cfs_rq->tasks_timeline.rb_root); |
aeb73b04 | 605 | |
70eee74b BS |
606 | if (!last) |
607 | return NULL; | |
7eee3e67 IM |
608 | |
609 | return rb_entry(last, struct sched_entity, run_node); | |
aeb73b04 PZ |
610 | } |
611 | ||
bf0f6f24 IM |
612 | /************************************************************** |
613 | * Scheduling class statistics methods: | |
614 | */ | |
615 | ||
acb4a848 | 616 | int sched_proc_update_handler(struct ctl_table *table, int write, |
8d65af78 | 617 | void __user *buffer, size_t *lenp, |
b2be5e96 PZ |
618 | loff_t *ppos) |
619 | { | |
8d65af78 | 620 | int ret = proc_dointvec_minmax(table, write, buffer, lenp, ppos); |
58ac93e4 | 621 | unsigned int factor = get_update_sysctl_factor(); |
b2be5e96 PZ |
622 | |
623 | if (ret || !write) | |
624 | return ret; | |
625 | ||
626 | sched_nr_latency = DIV_ROUND_UP(sysctl_sched_latency, | |
627 | sysctl_sched_min_granularity); | |
628 | ||
acb4a848 CE |
629 | #define WRT_SYSCTL(name) \ |
630 | (normalized_sysctl_##name = sysctl_##name / (factor)) | |
631 | WRT_SYSCTL(sched_min_granularity); | |
632 | WRT_SYSCTL(sched_latency); | |
633 | WRT_SYSCTL(sched_wakeup_granularity); | |
acb4a848 CE |
634 | #undef WRT_SYSCTL |
635 | ||
b2be5e96 PZ |
636 | return 0; |
637 | } | |
638 | #endif | |
647e7cac | 639 | |
a7be37ac | 640 | /* |
f9c0b095 | 641 | * delta /= w |
a7be37ac | 642 | */ |
9dbdb155 | 643 | static inline u64 calc_delta_fair(u64 delta, struct sched_entity *se) |
a7be37ac | 644 | { |
f9c0b095 | 645 | if (unlikely(se->load.weight != NICE_0_LOAD)) |
9dbdb155 | 646 | delta = __calc_delta(delta, NICE_0_LOAD, &se->load); |
a7be37ac PZ |
647 | |
648 | return delta; | |
649 | } | |
650 | ||
647e7cac IM |
651 | /* |
652 | * The idea is to set a period in which each task runs once. | |
653 | * | |
532b1858 | 654 | * When there are too many tasks (sched_nr_latency) we have to stretch |
647e7cac IM |
655 | * this period because otherwise the slices get too small. |
656 | * | |
657 | * p = (nr <= nl) ? l : l*nr/nl | |
658 | */ | |
4d78e7b6 PZ |
659 | static u64 __sched_period(unsigned long nr_running) |
660 | { | |
8e2b0bf3 BF |
661 | if (unlikely(nr_running > sched_nr_latency)) |
662 | return nr_running * sysctl_sched_min_granularity; | |
663 | else | |
664 | return sysctl_sched_latency; | |
4d78e7b6 PZ |
665 | } |
666 | ||
647e7cac IM |
667 | /* |
668 | * We calculate the wall-time slice from the period by taking a part | |
669 | * proportional to the weight. | |
670 | * | |
f9c0b095 | 671 | * s = p*P[w/rw] |
647e7cac | 672 | */ |
6d0f0ebd | 673 | static u64 sched_slice(struct cfs_rq *cfs_rq, struct sched_entity *se) |
21805085 | 674 | { |
0a582440 | 675 | u64 slice = __sched_period(cfs_rq->nr_running + !se->on_rq); |
f9c0b095 | 676 | |
0a582440 | 677 | for_each_sched_entity(se) { |
6272d68c | 678 | struct load_weight *load; |
3104bf03 | 679 | struct load_weight lw; |
6272d68c LM |
680 | |
681 | cfs_rq = cfs_rq_of(se); | |
682 | load = &cfs_rq->load; | |
f9c0b095 | 683 | |
0a582440 | 684 | if (unlikely(!se->on_rq)) { |
3104bf03 | 685 | lw = cfs_rq->load; |
0a582440 MG |
686 | |
687 | update_load_add(&lw, se->load.weight); | |
688 | load = &lw; | |
689 | } | |
9dbdb155 | 690 | slice = __calc_delta(slice, se->load.weight, load); |
0a582440 MG |
691 | } |
692 | return slice; | |
bf0f6f24 IM |
693 | } |
694 | ||
647e7cac | 695 | /* |
660cc00f | 696 | * We calculate the vruntime slice of a to-be-inserted task. |
647e7cac | 697 | * |
f9c0b095 | 698 | * vs = s/w |
647e7cac | 699 | */ |
f9c0b095 | 700 | static u64 sched_vslice(struct cfs_rq *cfs_rq, struct sched_entity *se) |
67e9fb2a | 701 | { |
f9c0b095 | 702 | return calc_delta_fair(sched_slice(cfs_rq, se), se); |
a7be37ac PZ |
703 | } |
704 | ||
c0796298 | 705 | #include "pelt.h" |
23127296 | 706 | #ifdef CONFIG_SMP |
283e2ed3 | 707 | |
772bd008 | 708 | static int select_idle_sibling(struct task_struct *p, int prev_cpu, int cpu); |
fb13c7ee | 709 | static unsigned long task_h_load(struct task_struct *p); |
3b1baa64 | 710 | static unsigned long capacity_of(int cpu); |
fb13c7ee | 711 | |
540247fb YD |
712 | /* Give new sched_entity start runnable values to heavy its load in infant time */ |
713 | void init_entity_runnable_average(struct sched_entity *se) | |
a75cdaa9 | 714 | { |
540247fb | 715 | struct sched_avg *sa = &se->avg; |
a75cdaa9 | 716 | |
f207934f PZ |
717 | memset(sa, 0, sizeof(*sa)); |
718 | ||
b5a9b340 | 719 | /* |
dfcb245e | 720 | * Tasks are initialized with full load to be seen as heavy tasks until |
b5a9b340 | 721 | * they get a chance to stabilize to their real load level. |
dfcb245e | 722 | * Group entities are initialized with zero load to reflect the fact that |
b5a9b340 VG |
723 | * nothing has been attached to the task group yet. |
724 | */ | |
725 | if (entity_is_task(se)) | |
1ea6c46a | 726 | sa->runnable_load_avg = sa->load_avg = scale_load_down(se->load.weight); |
1ea6c46a | 727 | |
f207934f PZ |
728 | se->runnable_weight = se->load.weight; |
729 | ||
9d89c257 | 730 | /* when this task enqueue'ed, it will contribute to its cfs_rq's load_avg */ |
a75cdaa9 | 731 | } |
7ea241af | 732 | |
7dc603c9 | 733 | static inline u64 cfs_rq_clock_task(struct cfs_rq *cfs_rq); |
df217913 | 734 | static void attach_entity_cfs_rq(struct sched_entity *se); |
7dc603c9 | 735 | |
2b8c41da YD |
736 | /* |
737 | * With new tasks being created, their initial util_avgs are extrapolated | |
738 | * based on the cfs_rq's current util_avg: | |
739 | * | |
740 | * util_avg = cfs_rq->util_avg / (cfs_rq->load_avg + 1) * se.load.weight | |
741 | * | |
742 | * However, in many cases, the above util_avg does not give a desired | |
743 | * value. Moreover, the sum of the util_avgs may be divergent, such | |
744 | * as when the series is a harmonic series. | |
745 | * | |
746 | * To solve this problem, we also cap the util_avg of successive tasks to | |
747 | * only 1/2 of the left utilization budget: | |
748 | * | |
8fe5c5a9 | 749 | * util_avg_cap = (cpu_scale - cfs_rq->avg.util_avg) / 2^n |
2b8c41da | 750 | * |
8fe5c5a9 | 751 | * where n denotes the nth task and cpu_scale the CPU capacity. |
2b8c41da | 752 | * |
8fe5c5a9 QP |
753 | * For example, for a CPU with 1024 of capacity, a simplest series from |
754 | * the beginning would be like: | |
2b8c41da YD |
755 | * |
756 | * task util_avg: 512, 256, 128, 64, 32, 16, 8, ... | |
757 | * cfs_rq util_avg: 512, 768, 896, 960, 992, 1008, 1016, ... | |
758 | * | |
759 | * Finally, that extrapolated util_avg is clamped to the cap (util_avg_cap) | |
760 | * if util_avg > util_avg_cap. | |
761 | */ | |
762 | void post_init_entity_util_avg(struct sched_entity *se) | |
763 | { | |
764 | struct cfs_rq *cfs_rq = cfs_rq_of(se); | |
765 | struct sched_avg *sa = &se->avg; | |
8fe5c5a9 QP |
766 | long cpu_scale = arch_scale_cpu_capacity(NULL, cpu_of(rq_of(cfs_rq))); |
767 | long cap = (long)(cpu_scale - cfs_rq->avg.util_avg) / 2; | |
2b8c41da YD |
768 | |
769 | if (cap > 0) { | |
770 | if (cfs_rq->avg.util_avg != 0) { | |
771 | sa->util_avg = cfs_rq->avg.util_avg * se->load.weight; | |
772 | sa->util_avg /= (cfs_rq->avg.load_avg + 1); | |
773 | ||
774 | if (sa->util_avg > cap) | |
775 | sa->util_avg = cap; | |
776 | } else { | |
777 | sa->util_avg = cap; | |
778 | } | |
2b8c41da | 779 | } |
7dc603c9 PZ |
780 | |
781 | if (entity_is_task(se)) { | |
782 | struct task_struct *p = task_of(se); | |
783 | if (p->sched_class != &fair_sched_class) { | |
784 | /* | |
785 | * For !fair tasks do: | |
786 | * | |
3a123bbb | 787 | update_cfs_rq_load_avg(now, cfs_rq); |
ea14b57e | 788 | attach_entity_load_avg(cfs_rq, se, 0); |
7dc603c9 PZ |
789 | switched_from_fair(rq, p); |
790 | * | |
791 | * such that the next switched_to_fair() has the | |
792 | * expected state. | |
793 | */ | |
23127296 | 794 | se->avg.last_update_time = cfs_rq_clock_pelt(cfs_rq); |
7dc603c9 PZ |
795 | return; |
796 | } | |
797 | } | |
798 | ||
df217913 | 799 | attach_entity_cfs_rq(se); |
2b8c41da YD |
800 | } |
801 | ||
7dc603c9 | 802 | #else /* !CONFIG_SMP */ |
540247fb | 803 | void init_entity_runnable_average(struct sched_entity *se) |
a75cdaa9 AS |
804 | { |
805 | } | |
2b8c41da YD |
806 | void post_init_entity_util_avg(struct sched_entity *se) |
807 | { | |
808 | } | |
3d30544f PZ |
809 | static void update_tg_load_avg(struct cfs_rq *cfs_rq, int force) |
810 | { | |
811 | } | |
7dc603c9 | 812 | #endif /* CONFIG_SMP */ |
a75cdaa9 | 813 | |
bf0f6f24 | 814 | /* |
9dbdb155 | 815 | * Update the current task's runtime statistics. |
bf0f6f24 | 816 | */ |
b7cc0896 | 817 | static void update_curr(struct cfs_rq *cfs_rq) |
bf0f6f24 | 818 | { |
429d43bc | 819 | struct sched_entity *curr = cfs_rq->curr; |
78becc27 | 820 | u64 now = rq_clock_task(rq_of(cfs_rq)); |
9dbdb155 | 821 | u64 delta_exec; |
bf0f6f24 IM |
822 | |
823 | if (unlikely(!curr)) | |
824 | return; | |
825 | ||
9dbdb155 PZ |
826 | delta_exec = now - curr->exec_start; |
827 | if (unlikely((s64)delta_exec <= 0)) | |
34f28ecd | 828 | return; |
bf0f6f24 | 829 | |
8ebc91d9 | 830 | curr->exec_start = now; |
d842de87 | 831 | |
9dbdb155 PZ |
832 | schedstat_set(curr->statistics.exec_max, |
833 | max(delta_exec, curr->statistics.exec_max)); | |
834 | ||
835 | curr->sum_exec_runtime += delta_exec; | |
ae92882e | 836 | schedstat_add(cfs_rq->exec_clock, delta_exec); |
9dbdb155 PZ |
837 | |
838 | curr->vruntime += calc_delta_fair(delta_exec, curr); | |
839 | update_min_vruntime(cfs_rq); | |
840 | ||
d842de87 SV |
841 | if (entity_is_task(curr)) { |
842 | struct task_struct *curtask = task_of(curr); | |
843 | ||
f977bb49 | 844 | trace_sched_stat_runtime(curtask, delta_exec, curr->vruntime); |
d2cc5ed6 | 845 | cgroup_account_cputime(curtask, delta_exec); |
f06febc9 | 846 | account_group_exec_runtime(curtask, delta_exec); |
d842de87 | 847 | } |
ec12cb7f PT |
848 | |
849 | account_cfs_rq_runtime(cfs_rq, delta_exec); | |
bf0f6f24 IM |
850 | } |
851 | ||
6e998916 SG |
852 | static void update_curr_fair(struct rq *rq) |
853 | { | |
854 | update_curr(cfs_rq_of(&rq->curr->se)); | |
855 | } | |
856 | ||
bf0f6f24 | 857 | static inline void |
5870db5b | 858 | update_stats_wait_start(struct cfs_rq *cfs_rq, struct sched_entity *se) |
bf0f6f24 | 859 | { |
4fa8d299 JP |
860 | u64 wait_start, prev_wait_start; |
861 | ||
862 | if (!schedstat_enabled()) | |
863 | return; | |
864 | ||
865 | wait_start = rq_clock(rq_of(cfs_rq)); | |
866 | prev_wait_start = schedstat_val(se->statistics.wait_start); | |
3ea94de1 JP |
867 | |
868 | if (entity_is_task(se) && task_on_rq_migrating(task_of(se)) && | |
4fa8d299 JP |
869 | likely(wait_start > prev_wait_start)) |
870 | wait_start -= prev_wait_start; | |
3ea94de1 | 871 | |
2ed41a55 | 872 | __schedstat_set(se->statistics.wait_start, wait_start); |
bf0f6f24 IM |
873 | } |
874 | ||
4fa8d299 | 875 | static inline void |
3ea94de1 JP |
876 | update_stats_wait_end(struct cfs_rq *cfs_rq, struct sched_entity *se) |
877 | { | |
878 | struct task_struct *p; | |
cb251765 MG |
879 | u64 delta; |
880 | ||
4fa8d299 JP |
881 | if (!schedstat_enabled()) |
882 | return; | |
883 | ||
884 | delta = rq_clock(rq_of(cfs_rq)) - schedstat_val(se->statistics.wait_start); | |
3ea94de1 JP |
885 | |
886 | if (entity_is_task(se)) { | |
887 | p = task_of(se); | |
888 | if (task_on_rq_migrating(p)) { | |
889 | /* | |
890 | * Preserve migrating task's wait time so wait_start | |
891 | * time stamp can be adjusted to accumulate wait time | |
892 | * prior to migration. | |
893 | */ | |
2ed41a55 | 894 | __schedstat_set(se->statistics.wait_start, delta); |
3ea94de1 JP |
895 | return; |
896 | } | |
897 | trace_sched_stat_wait(p, delta); | |
898 | } | |
899 | ||
2ed41a55 | 900 | __schedstat_set(se->statistics.wait_max, |
4fa8d299 | 901 | max(schedstat_val(se->statistics.wait_max), delta)); |
2ed41a55 PZ |
902 | __schedstat_inc(se->statistics.wait_count); |
903 | __schedstat_add(se->statistics.wait_sum, delta); | |
904 | __schedstat_set(se->statistics.wait_start, 0); | |
3ea94de1 | 905 | } |
3ea94de1 | 906 | |
4fa8d299 | 907 | static inline void |
1a3d027c JP |
908 | update_stats_enqueue_sleeper(struct cfs_rq *cfs_rq, struct sched_entity *se) |
909 | { | |
910 | struct task_struct *tsk = NULL; | |
4fa8d299 JP |
911 | u64 sleep_start, block_start; |
912 | ||
913 | if (!schedstat_enabled()) | |
914 | return; | |
915 | ||
916 | sleep_start = schedstat_val(se->statistics.sleep_start); | |
917 | block_start = schedstat_val(se->statistics.block_start); | |
1a3d027c JP |
918 | |
919 | if (entity_is_task(se)) | |
920 | tsk = task_of(se); | |
921 | ||
4fa8d299 JP |
922 | if (sleep_start) { |
923 | u64 delta = rq_clock(rq_of(cfs_rq)) - sleep_start; | |
1a3d027c JP |
924 | |
925 | if ((s64)delta < 0) | |
926 | delta = 0; | |
927 | ||
4fa8d299 | 928 | if (unlikely(delta > schedstat_val(se->statistics.sleep_max))) |
2ed41a55 | 929 | __schedstat_set(se->statistics.sleep_max, delta); |
1a3d027c | 930 | |
2ed41a55 PZ |
931 | __schedstat_set(se->statistics.sleep_start, 0); |
932 | __schedstat_add(se->statistics.sum_sleep_runtime, delta); | |
1a3d027c JP |
933 | |
934 | if (tsk) { | |
935 | account_scheduler_latency(tsk, delta >> 10, 1); | |
936 | trace_sched_stat_sleep(tsk, delta); | |
937 | } | |
938 | } | |
4fa8d299 JP |
939 | if (block_start) { |
940 | u64 delta = rq_clock(rq_of(cfs_rq)) - block_start; | |
1a3d027c JP |
941 | |
942 | if ((s64)delta < 0) | |
943 | delta = 0; | |
944 | ||
4fa8d299 | 945 | if (unlikely(delta > schedstat_val(se->statistics.block_max))) |
2ed41a55 | 946 | __schedstat_set(se->statistics.block_max, delta); |
1a3d027c | 947 | |
2ed41a55 PZ |
948 | __schedstat_set(se->statistics.block_start, 0); |
949 | __schedstat_add(se->statistics.sum_sleep_runtime, delta); | |
1a3d027c JP |
950 | |
951 | if (tsk) { | |
952 | if (tsk->in_iowait) { | |
2ed41a55 PZ |
953 | __schedstat_add(se->statistics.iowait_sum, delta); |
954 | __schedstat_inc(se->statistics.iowait_count); | |
1a3d027c JP |
955 | trace_sched_stat_iowait(tsk, delta); |
956 | } | |
957 | ||
958 | trace_sched_stat_blocked(tsk, delta); | |
959 | ||
960 | /* | |
961 | * Blocking time is in units of nanosecs, so shift by | |
962 | * 20 to get a milliseconds-range estimation of the | |
963 | * amount of time that the task spent sleeping: | |
964 | */ | |
965 | if (unlikely(prof_on == SLEEP_PROFILING)) { | |
966 | profile_hits(SLEEP_PROFILING, | |
967 | (void *)get_wchan(tsk), | |
968 | delta >> 20); | |
969 | } | |
970 | account_scheduler_latency(tsk, delta >> 10, 0); | |
971 | } | |
972 | } | |
3ea94de1 | 973 | } |
3ea94de1 | 974 | |
bf0f6f24 IM |
975 | /* |
976 | * Task is being enqueued - update stats: | |
977 | */ | |
cb251765 | 978 | static inline void |
1a3d027c | 979 | update_stats_enqueue(struct cfs_rq *cfs_rq, struct sched_entity *se, int flags) |
bf0f6f24 | 980 | { |
4fa8d299 JP |
981 | if (!schedstat_enabled()) |
982 | return; | |
983 | ||
bf0f6f24 IM |
984 | /* |
985 | * Are we enqueueing a waiting task? (for current tasks | |
986 | * a dequeue/enqueue event is a NOP) | |
987 | */ | |
429d43bc | 988 | if (se != cfs_rq->curr) |
5870db5b | 989 | update_stats_wait_start(cfs_rq, se); |
1a3d027c JP |
990 | |
991 | if (flags & ENQUEUE_WAKEUP) | |
992 | update_stats_enqueue_sleeper(cfs_rq, se); | |
bf0f6f24 IM |
993 | } |
994 | ||
bf0f6f24 | 995 | static inline void |
cb251765 | 996 | update_stats_dequeue(struct cfs_rq *cfs_rq, struct sched_entity *se, int flags) |
bf0f6f24 | 997 | { |
4fa8d299 JP |
998 | |
999 | if (!schedstat_enabled()) | |
1000 | return; | |
1001 | ||
bf0f6f24 IM |
1002 | /* |
1003 | * Mark the end of the wait period if dequeueing a | |
1004 | * waiting task: | |
1005 | */ | |
429d43bc | 1006 | if (se != cfs_rq->curr) |
9ef0a961 | 1007 | update_stats_wait_end(cfs_rq, se); |
cb251765 | 1008 | |
4fa8d299 JP |
1009 | if ((flags & DEQUEUE_SLEEP) && entity_is_task(se)) { |
1010 | struct task_struct *tsk = task_of(se); | |
cb251765 | 1011 | |
4fa8d299 | 1012 | if (tsk->state & TASK_INTERRUPTIBLE) |
2ed41a55 | 1013 | __schedstat_set(se->statistics.sleep_start, |
4fa8d299 JP |
1014 | rq_clock(rq_of(cfs_rq))); |
1015 | if (tsk->state & TASK_UNINTERRUPTIBLE) | |
2ed41a55 | 1016 | __schedstat_set(se->statistics.block_start, |
4fa8d299 | 1017 | rq_clock(rq_of(cfs_rq))); |
cb251765 | 1018 | } |
cb251765 MG |
1019 | } |
1020 | ||
bf0f6f24 IM |
1021 | /* |
1022 | * We are picking a new current task - update its stats: | |
1023 | */ | |
1024 | static inline void | |
79303e9e | 1025 | update_stats_curr_start(struct cfs_rq *cfs_rq, struct sched_entity *se) |
bf0f6f24 IM |
1026 | { |
1027 | /* | |
1028 | * We are starting a new run period: | |
1029 | */ | |
78becc27 | 1030 | se->exec_start = rq_clock_task(rq_of(cfs_rq)); |
bf0f6f24 IM |
1031 | } |
1032 | ||
bf0f6f24 IM |
1033 | /************************************************** |
1034 | * Scheduling class queueing methods: | |
1035 | */ | |
1036 | ||
cbee9f88 PZ |
1037 | #ifdef CONFIG_NUMA_BALANCING |
1038 | /* | |
598f0ec0 MG |
1039 | * Approximate time to scan a full NUMA task in ms. The task scan period is |
1040 | * calculated based on the tasks virtual memory size and | |
1041 | * numa_balancing_scan_size. | |
cbee9f88 | 1042 | */ |
598f0ec0 MG |
1043 | unsigned int sysctl_numa_balancing_scan_period_min = 1000; |
1044 | unsigned int sysctl_numa_balancing_scan_period_max = 60000; | |
6e5fb223 PZ |
1045 | |
1046 | /* Portion of address space to scan in MB */ | |
1047 | unsigned int sysctl_numa_balancing_scan_size = 256; | |
cbee9f88 | 1048 | |
4b96a29b PZ |
1049 | /* Scan @scan_size MB every @scan_period after an initial @scan_delay in ms */ |
1050 | unsigned int sysctl_numa_balancing_scan_delay = 1000; | |
1051 | ||
b5dd77c8 | 1052 | struct numa_group { |
c45a7795 | 1053 | refcount_t refcount; |
b5dd77c8 RR |
1054 | |
1055 | spinlock_t lock; /* nr_tasks, tasks */ | |
1056 | int nr_tasks; | |
1057 | pid_t gid; | |
1058 | int active_nodes; | |
1059 | ||
1060 | struct rcu_head rcu; | |
1061 | unsigned long total_faults; | |
1062 | unsigned long max_faults_cpu; | |
1063 | /* | |
1064 | * Faults_cpu is used to decide whether memory should move | |
1065 | * towards the CPU. As a consequence, these stats are weighted | |
1066 | * more by CPU use than by memory faults. | |
1067 | */ | |
1068 | unsigned long *faults_cpu; | |
1069 | unsigned long faults[0]; | |
1070 | }; | |
1071 | ||
1072 | static inline unsigned long group_faults_priv(struct numa_group *ng); | |
1073 | static inline unsigned long group_faults_shared(struct numa_group *ng); | |
1074 | ||
598f0ec0 MG |
1075 | static unsigned int task_nr_scan_windows(struct task_struct *p) |
1076 | { | |
1077 | unsigned long rss = 0; | |
1078 | unsigned long nr_scan_pages; | |
1079 | ||
1080 | /* | |
1081 | * Calculations based on RSS as non-present and empty pages are skipped | |
1082 | * by the PTE scanner and NUMA hinting faults should be trapped based | |
1083 | * on resident pages | |
1084 | */ | |
1085 | nr_scan_pages = sysctl_numa_balancing_scan_size << (20 - PAGE_SHIFT); | |
1086 | rss = get_mm_rss(p->mm); | |
1087 | if (!rss) | |
1088 | rss = nr_scan_pages; | |
1089 | ||
1090 | rss = round_up(rss, nr_scan_pages); | |
1091 | return rss / nr_scan_pages; | |
1092 | } | |
1093 | ||
1094 | /* For sanitys sake, never scan more PTEs than MAX_SCAN_WINDOW MB/sec. */ | |
1095 | #define MAX_SCAN_WINDOW 2560 | |
1096 | ||
1097 | static unsigned int task_scan_min(struct task_struct *p) | |
1098 | { | |
316c1608 | 1099 | unsigned int scan_size = READ_ONCE(sysctl_numa_balancing_scan_size); |
598f0ec0 MG |
1100 | unsigned int scan, floor; |
1101 | unsigned int windows = 1; | |
1102 | ||
64192658 KT |
1103 | if (scan_size < MAX_SCAN_WINDOW) |
1104 | windows = MAX_SCAN_WINDOW / scan_size; | |
598f0ec0 MG |
1105 | floor = 1000 / windows; |
1106 | ||
1107 | scan = sysctl_numa_balancing_scan_period_min / task_nr_scan_windows(p); | |
1108 | return max_t(unsigned int, floor, scan); | |
1109 | } | |
1110 | ||
b5dd77c8 RR |
1111 | static unsigned int task_scan_start(struct task_struct *p) |
1112 | { | |
1113 | unsigned long smin = task_scan_min(p); | |
1114 | unsigned long period = smin; | |
1115 | ||
1116 | /* Scale the maximum scan period with the amount of shared memory. */ | |
1117 | if (p->numa_group) { | |
1118 | struct numa_group *ng = p->numa_group; | |
1119 | unsigned long shared = group_faults_shared(ng); | |
1120 | unsigned long private = group_faults_priv(ng); | |
1121 | ||
c45a7795 | 1122 | period *= refcount_read(&ng->refcount); |
b5dd77c8 RR |
1123 | period *= shared + 1; |
1124 | period /= private + shared + 1; | |
1125 | } | |
1126 | ||
1127 | return max(smin, period); | |
1128 | } | |
1129 | ||
598f0ec0 MG |
1130 | static unsigned int task_scan_max(struct task_struct *p) |
1131 | { | |
b5dd77c8 RR |
1132 | unsigned long smin = task_scan_min(p); |
1133 | unsigned long smax; | |
598f0ec0 MG |
1134 | |
1135 | /* Watch for min being lower than max due to floor calculations */ | |
1136 | smax = sysctl_numa_balancing_scan_period_max / task_nr_scan_windows(p); | |
b5dd77c8 RR |
1137 | |
1138 | /* Scale the maximum scan period with the amount of shared memory. */ | |
1139 | if (p->numa_group) { | |
1140 | struct numa_group *ng = p->numa_group; | |
1141 | unsigned long shared = group_faults_shared(ng); | |
1142 | unsigned long private = group_faults_priv(ng); | |
1143 | unsigned long period = smax; | |
1144 | ||
c45a7795 | 1145 | period *= refcount_read(&ng->refcount); |
b5dd77c8 RR |
1146 | period *= shared + 1; |
1147 | period /= private + shared + 1; | |
1148 | ||
1149 | smax = max(smax, period); | |
1150 | } | |
1151 | ||
598f0ec0 MG |
1152 | return max(smin, smax); |
1153 | } | |
1154 | ||
13784475 MG |
1155 | void init_numa_balancing(unsigned long clone_flags, struct task_struct *p) |
1156 | { | |
1157 | int mm_users = 0; | |
1158 | struct mm_struct *mm = p->mm; | |
1159 | ||
1160 | if (mm) { | |
1161 | mm_users = atomic_read(&mm->mm_users); | |
1162 | if (mm_users == 1) { | |
1163 | mm->numa_next_scan = jiffies + msecs_to_jiffies(sysctl_numa_balancing_scan_delay); | |
1164 | mm->numa_scan_seq = 0; | |
1165 | } | |
1166 | } | |
1167 | p->node_stamp = 0; | |
1168 | p->numa_scan_seq = mm ? mm->numa_scan_seq : 0; | |
1169 | p->numa_scan_period = sysctl_numa_balancing_scan_delay; | |
1170 | p->numa_work.next = &p->numa_work; | |
1171 | p->numa_faults = NULL; | |
1172 | p->numa_group = NULL; | |
1173 | p->last_task_numa_placement = 0; | |
1174 | p->last_sum_exec_runtime = 0; | |
1175 | ||
1176 | /* New address space, reset the preferred nid */ | |
1177 | if (!(clone_flags & CLONE_VM)) { | |
1178 | p->numa_preferred_nid = -1; | |
1179 | return; | |
1180 | } | |
1181 | ||
1182 | /* | |
1183 | * New thread, keep existing numa_preferred_nid which should be copied | |
1184 | * already by arch_dup_task_struct but stagger when scans start. | |
1185 | */ | |
1186 | if (mm) { | |
1187 | unsigned int delay; | |
1188 | ||
1189 | delay = min_t(unsigned int, task_scan_max(current), | |
1190 | current->numa_scan_period * mm_users * NSEC_PER_MSEC); | |
1191 | delay += 2 * TICK_NSEC; | |
1192 | p->node_stamp = delay; | |
1193 | } | |
1194 | } | |
1195 | ||
0ec8aa00 PZ |
1196 | static void account_numa_enqueue(struct rq *rq, struct task_struct *p) |
1197 | { | |
1198 | rq->nr_numa_running += (p->numa_preferred_nid != -1); | |
1199 | rq->nr_preferred_running += (p->numa_preferred_nid == task_node(p)); | |
1200 | } | |
1201 | ||
1202 | static void account_numa_dequeue(struct rq *rq, struct task_struct *p) | |
1203 | { | |
1204 | rq->nr_numa_running -= (p->numa_preferred_nid != -1); | |
1205 | rq->nr_preferred_running -= (p->numa_preferred_nid == task_node(p)); | |
1206 | } | |
1207 | ||
be1e4e76 RR |
1208 | /* Shared or private faults. */ |
1209 | #define NR_NUMA_HINT_FAULT_TYPES 2 | |
1210 | ||
1211 | /* Memory and CPU locality */ | |
1212 | #define NR_NUMA_HINT_FAULT_STATS (NR_NUMA_HINT_FAULT_TYPES * 2) | |
1213 | ||
1214 | /* Averaged statistics, and temporary buffers. */ | |
1215 | #define NR_NUMA_HINT_FAULT_BUCKETS (NR_NUMA_HINT_FAULT_STATS * 2) | |
1216 | ||
e29cf08b MG |
1217 | pid_t task_numa_group_id(struct task_struct *p) |
1218 | { | |
1219 | return p->numa_group ? p->numa_group->gid : 0; | |
1220 | } | |
1221 | ||
44dba3d5 | 1222 | /* |
97fb7a0a | 1223 | * The averaged statistics, shared & private, memory & CPU, |
44dba3d5 IM |
1224 | * occupy the first half of the array. The second half of the |
1225 | * array is for current counters, which are averaged into the | |
1226 | * first set by task_numa_placement. | |
1227 | */ | |
1228 | static inline int task_faults_idx(enum numa_faults_stats s, int nid, int priv) | |
ac8e895b | 1229 | { |
44dba3d5 | 1230 | return NR_NUMA_HINT_FAULT_TYPES * (s * nr_node_ids + nid) + priv; |
ac8e895b MG |
1231 | } |
1232 | ||
1233 | static inline unsigned long task_faults(struct task_struct *p, int nid) | |
1234 | { | |
44dba3d5 | 1235 | if (!p->numa_faults) |
ac8e895b MG |
1236 | return 0; |
1237 | ||
44dba3d5 IM |
1238 | return p->numa_faults[task_faults_idx(NUMA_MEM, nid, 0)] + |
1239 | p->numa_faults[task_faults_idx(NUMA_MEM, nid, 1)]; | |
ac8e895b MG |
1240 | } |
1241 | ||
83e1d2cd MG |
1242 | static inline unsigned long group_faults(struct task_struct *p, int nid) |
1243 | { | |
1244 | if (!p->numa_group) | |
1245 | return 0; | |
1246 | ||
44dba3d5 IM |
1247 | return p->numa_group->faults[task_faults_idx(NUMA_MEM, nid, 0)] + |
1248 | p->numa_group->faults[task_faults_idx(NUMA_MEM, nid, 1)]; | |
83e1d2cd MG |
1249 | } |
1250 | ||
20e07dea RR |
1251 | static inline unsigned long group_faults_cpu(struct numa_group *group, int nid) |
1252 | { | |
44dba3d5 IM |
1253 | return group->faults_cpu[task_faults_idx(NUMA_MEM, nid, 0)] + |
1254 | group->faults_cpu[task_faults_idx(NUMA_MEM, nid, 1)]; | |
20e07dea RR |
1255 | } |
1256 | ||
b5dd77c8 RR |
1257 | static inline unsigned long group_faults_priv(struct numa_group *ng) |
1258 | { | |
1259 | unsigned long faults = 0; | |
1260 | int node; | |
1261 | ||
1262 | for_each_online_node(node) { | |
1263 | faults += ng->faults[task_faults_idx(NUMA_MEM, node, 1)]; | |
1264 | } | |
1265 | ||
1266 | return faults; | |
1267 | } | |
1268 | ||
1269 | static inline unsigned long group_faults_shared(struct numa_group *ng) | |
1270 | { | |
1271 | unsigned long faults = 0; | |
1272 | int node; | |
1273 | ||
1274 | for_each_online_node(node) { | |
1275 | faults += ng->faults[task_faults_idx(NUMA_MEM, node, 0)]; | |
1276 | } | |
1277 | ||
1278 | return faults; | |
1279 | } | |
1280 | ||
4142c3eb RR |
1281 | /* |
1282 | * A node triggering more than 1/3 as many NUMA faults as the maximum is | |
1283 | * considered part of a numa group's pseudo-interleaving set. Migrations | |
1284 | * between these nodes are slowed down, to allow things to settle down. | |
1285 | */ | |
1286 | #define ACTIVE_NODE_FRACTION 3 | |
1287 | ||
1288 | static bool numa_is_active_node(int nid, struct numa_group *ng) | |
1289 | { | |
1290 | return group_faults_cpu(ng, nid) * ACTIVE_NODE_FRACTION > ng->max_faults_cpu; | |
1291 | } | |
1292 | ||
6c6b1193 RR |
1293 | /* Handle placement on systems where not all nodes are directly connected. */ |
1294 | static unsigned long score_nearby_nodes(struct task_struct *p, int nid, | |
1295 | int maxdist, bool task) | |
1296 | { | |
1297 | unsigned long score = 0; | |
1298 | int node; | |
1299 | ||
1300 | /* | |
1301 | * All nodes are directly connected, and the same distance | |
1302 | * from each other. No need for fancy placement algorithms. | |
1303 | */ | |
1304 | if (sched_numa_topology_type == NUMA_DIRECT) | |
1305 | return 0; | |
1306 | ||
1307 | /* | |
1308 | * This code is called for each node, introducing N^2 complexity, | |
1309 | * which should be ok given the number of nodes rarely exceeds 8. | |
1310 | */ | |
1311 | for_each_online_node(node) { | |
1312 | unsigned long faults; | |
1313 | int dist = node_distance(nid, node); | |
1314 | ||
1315 | /* | |
1316 | * The furthest away nodes in the system are not interesting | |
1317 | * for placement; nid was already counted. | |
1318 | */ | |
1319 | if (dist == sched_max_numa_distance || node == nid) | |
1320 | continue; | |
1321 | ||
1322 | /* | |
1323 | * On systems with a backplane NUMA topology, compare groups | |
1324 | * of nodes, and move tasks towards the group with the most | |
1325 | * memory accesses. When comparing two nodes at distance | |
1326 | * "hoplimit", only nodes closer by than "hoplimit" are part | |
1327 | * of each group. Skip other nodes. | |
1328 | */ | |
1329 | if (sched_numa_topology_type == NUMA_BACKPLANE && | |
0ee7e74d | 1330 | dist >= maxdist) |
6c6b1193 RR |
1331 | continue; |
1332 | ||
1333 | /* Add up the faults from nearby nodes. */ | |
1334 | if (task) | |
1335 | faults = task_faults(p, node); | |
1336 | else | |
1337 | faults = group_faults(p, node); | |
1338 | ||
1339 | /* | |
1340 | * On systems with a glueless mesh NUMA topology, there are | |
1341 | * no fixed "groups of nodes". Instead, nodes that are not | |
1342 | * directly connected bounce traffic through intermediate | |
1343 | * nodes; a numa_group can occupy any set of nodes. | |
1344 | * The further away a node is, the less the faults count. | |
1345 | * This seems to result in good task placement. | |
1346 | */ | |
1347 | if (sched_numa_topology_type == NUMA_GLUELESS_MESH) { | |
1348 | faults *= (sched_max_numa_distance - dist); | |
1349 | faults /= (sched_max_numa_distance - LOCAL_DISTANCE); | |
1350 | } | |
1351 | ||
1352 | score += faults; | |
1353 | } | |
1354 | ||
1355 | return score; | |
1356 | } | |
1357 | ||
83e1d2cd MG |
1358 | /* |
1359 | * These return the fraction of accesses done by a particular task, or | |
1360 | * task group, on a particular numa node. The group weight is given a | |
1361 | * larger multiplier, in order to group tasks together that are almost | |
1362 | * evenly spread out between numa nodes. | |
1363 | */ | |
7bd95320 RR |
1364 | static inline unsigned long task_weight(struct task_struct *p, int nid, |
1365 | int dist) | |
83e1d2cd | 1366 | { |
7bd95320 | 1367 | unsigned long faults, total_faults; |
83e1d2cd | 1368 | |
44dba3d5 | 1369 | if (!p->numa_faults) |
83e1d2cd MG |
1370 | return 0; |
1371 | ||
1372 | total_faults = p->total_numa_faults; | |
1373 | ||
1374 | if (!total_faults) | |
1375 | return 0; | |
1376 | ||
7bd95320 | 1377 | faults = task_faults(p, nid); |
6c6b1193 RR |
1378 | faults += score_nearby_nodes(p, nid, dist, true); |
1379 | ||
7bd95320 | 1380 | return 1000 * faults / total_faults; |
83e1d2cd MG |
1381 | } |
1382 | ||
7bd95320 RR |
1383 | static inline unsigned long group_weight(struct task_struct *p, int nid, |
1384 | int dist) | |
83e1d2cd | 1385 | { |
7bd95320 RR |
1386 | unsigned long faults, total_faults; |
1387 | ||
1388 | if (!p->numa_group) | |
1389 | return 0; | |
1390 | ||
1391 | total_faults = p->numa_group->total_faults; | |
1392 | ||
1393 | if (!total_faults) | |
83e1d2cd MG |
1394 | return 0; |
1395 | ||
7bd95320 | 1396 | faults = group_faults(p, nid); |
6c6b1193 RR |
1397 | faults += score_nearby_nodes(p, nid, dist, false); |
1398 | ||
7bd95320 | 1399 | return 1000 * faults / total_faults; |
83e1d2cd MG |
1400 | } |
1401 | ||
10f39042 RR |
1402 | bool should_numa_migrate_memory(struct task_struct *p, struct page * page, |
1403 | int src_nid, int dst_cpu) | |
1404 | { | |
1405 | struct numa_group *ng = p->numa_group; | |
1406 | int dst_nid = cpu_to_node(dst_cpu); | |
1407 | int last_cpupid, this_cpupid; | |
1408 | ||
1409 | this_cpupid = cpu_pid_to_cpupid(dst_cpu, current->pid); | |
37355bdc MG |
1410 | last_cpupid = page_cpupid_xchg_last(page, this_cpupid); |
1411 | ||
1412 | /* | |
1413 | * Allow first faults or private faults to migrate immediately early in | |
1414 | * the lifetime of a task. The magic number 4 is based on waiting for | |
1415 | * two full passes of the "multi-stage node selection" test that is | |
1416 | * executed below. | |
1417 | */ | |
1418 | if ((p->numa_preferred_nid == -1 || p->numa_scan_seq <= 4) && | |
1419 | (cpupid_pid_unset(last_cpupid) || cpupid_match_pid(p, last_cpupid))) | |
1420 | return true; | |
10f39042 RR |
1421 | |
1422 | /* | |
1423 | * Multi-stage node selection is used in conjunction with a periodic | |
1424 | * migration fault to build a temporal task<->page relation. By using | |
1425 | * a two-stage filter we remove short/unlikely relations. | |
1426 | * | |
1427 | * Using P(p) ~ n_p / n_t as per frequentist probability, we can equate | |
1428 | * a task's usage of a particular page (n_p) per total usage of this | |
1429 | * page (n_t) (in a given time-span) to a probability. | |
1430 | * | |
1431 | * Our periodic faults will sample this probability and getting the | |
1432 | * same result twice in a row, given these samples are fully | |
1433 | * independent, is then given by P(n)^2, provided our sample period | |
1434 | * is sufficiently short compared to the usage pattern. | |
1435 | * | |
1436 | * This quadric squishes small probabilities, making it less likely we | |
1437 | * act on an unlikely task<->page relation. | |
1438 | */ | |
10f39042 RR |
1439 | if (!cpupid_pid_unset(last_cpupid) && |
1440 | cpupid_to_nid(last_cpupid) != dst_nid) | |
1441 | return false; | |
1442 | ||
1443 | /* Always allow migrate on private faults */ | |
1444 | if (cpupid_match_pid(p, last_cpupid)) | |
1445 | return true; | |
1446 | ||
1447 | /* A shared fault, but p->numa_group has not been set up yet. */ | |
1448 | if (!ng) | |
1449 | return true; | |
1450 | ||
1451 | /* | |
4142c3eb RR |
1452 | * Destination node is much more heavily used than the source |
1453 | * node? Allow migration. | |
10f39042 | 1454 | */ |
4142c3eb RR |
1455 | if (group_faults_cpu(ng, dst_nid) > group_faults_cpu(ng, src_nid) * |
1456 | ACTIVE_NODE_FRACTION) | |
10f39042 RR |
1457 | return true; |
1458 | ||
1459 | /* | |
4142c3eb RR |
1460 | * Distribute memory according to CPU & memory use on each node, |
1461 | * with 3/4 hysteresis to avoid unnecessary memory migrations: | |
1462 | * | |
1463 | * faults_cpu(dst) 3 faults_cpu(src) | |
1464 | * --------------- * - > --------------- | |
1465 | * faults_mem(dst) 4 faults_mem(src) | |
10f39042 | 1466 | */ |
4142c3eb RR |
1467 | return group_faults_cpu(ng, dst_nid) * group_faults(p, src_nid) * 3 > |
1468 | group_faults_cpu(ng, src_nid) * group_faults(p, dst_nid) * 4; | |
10f39042 RR |
1469 | } |
1470 | ||
c7132dd6 | 1471 | static unsigned long weighted_cpuload(struct rq *rq); |
58d081b5 MG |
1472 | static unsigned long source_load(int cpu, int type); |
1473 | static unsigned long target_load(int cpu, int type); | |
58d081b5 | 1474 | |
fb13c7ee | 1475 | /* Cached statistics for all CPUs within a node */ |
58d081b5 MG |
1476 | struct numa_stats { |
1477 | unsigned long load; | |
fb13c7ee MG |
1478 | |
1479 | /* Total compute capacity of CPUs on a node */ | |
5ef20ca1 | 1480 | unsigned long compute_capacity; |
58d081b5 | 1481 | }; |
e6628d5b | 1482 | |
fb13c7ee MG |
1483 | /* |
1484 | * XXX borrowed from update_sg_lb_stats | |
1485 | */ | |
1486 | static void update_numa_stats(struct numa_stats *ns, int nid) | |
1487 | { | |
d90707eb | 1488 | int cpu; |
fb13c7ee MG |
1489 | |
1490 | memset(ns, 0, sizeof(*ns)); | |
1491 | for_each_cpu(cpu, cpumask_of_node(nid)) { | |
1492 | struct rq *rq = cpu_rq(cpu); | |
1493 | ||
c7132dd6 | 1494 | ns->load += weighted_cpuload(rq); |
ced549fa | 1495 | ns->compute_capacity += capacity_of(cpu); |
fb13c7ee MG |
1496 | } |
1497 | ||
fb13c7ee MG |
1498 | } |
1499 | ||
58d081b5 MG |
1500 | struct task_numa_env { |
1501 | struct task_struct *p; | |
e6628d5b | 1502 | |
58d081b5 MG |
1503 | int src_cpu, src_nid; |
1504 | int dst_cpu, dst_nid; | |
e6628d5b | 1505 | |
58d081b5 | 1506 | struct numa_stats src_stats, dst_stats; |
e6628d5b | 1507 | |
40ea2b42 | 1508 | int imbalance_pct; |
7bd95320 | 1509 | int dist; |
fb13c7ee MG |
1510 | |
1511 | struct task_struct *best_task; | |
1512 | long best_imp; | |
58d081b5 MG |
1513 | int best_cpu; |
1514 | }; | |
1515 | ||
fb13c7ee MG |
1516 | static void task_numa_assign(struct task_numa_env *env, |
1517 | struct task_struct *p, long imp) | |
1518 | { | |
a4739eca SD |
1519 | struct rq *rq = cpu_rq(env->dst_cpu); |
1520 | ||
1521 | /* Bail out if run-queue part of active NUMA balance. */ | |
1522 | if (xchg(&rq->numa_migrate_on, 1)) | |
1523 | return; | |
1524 | ||
1525 | /* | |
1526 | * Clear previous best_cpu/rq numa-migrate flag, since task now | |
1527 | * found a better CPU to move/swap. | |
1528 | */ | |
1529 | if (env->best_cpu != -1) { | |
1530 | rq = cpu_rq(env->best_cpu); | |
1531 | WRITE_ONCE(rq->numa_migrate_on, 0); | |
1532 | } | |
1533 | ||
fb13c7ee MG |
1534 | if (env->best_task) |
1535 | put_task_struct(env->best_task); | |
bac78573 ON |
1536 | if (p) |
1537 | get_task_struct(p); | |
fb13c7ee MG |
1538 | |
1539 | env->best_task = p; | |
1540 | env->best_imp = imp; | |
1541 | env->best_cpu = env->dst_cpu; | |
1542 | } | |
1543 | ||
28a21745 | 1544 | static bool load_too_imbalanced(long src_load, long dst_load, |
e63da036 RR |
1545 | struct task_numa_env *env) |
1546 | { | |
e4991b24 RR |
1547 | long imb, old_imb; |
1548 | long orig_src_load, orig_dst_load; | |
28a21745 RR |
1549 | long src_capacity, dst_capacity; |
1550 | ||
1551 | /* | |
1552 | * The load is corrected for the CPU capacity available on each node. | |
1553 | * | |
1554 | * src_load dst_load | |
1555 | * ------------ vs --------- | |
1556 | * src_capacity dst_capacity | |
1557 | */ | |
1558 | src_capacity = env->src_stats.compute_capacity; | |
1559 | dst_capacity = env->dst_stats.compute_capacity; | |
e63da036 | 1560 | |
5f95ba7a | 1561 | imb = abs(dst_load * src_capacity - src_load * dst_capacity); |
e63da036 | 1562 | |
28a21745 | 1563 | orig_src_load = env->src_stats.load; |
e4991b24 | 1564 | orig_dst_load = env->dst_stats.load; |
28a21745 | 1565 | |
5f95ba7a | 1566 | old_imb = abs(orig_dst_load * src_capacity - orig_src_load * dst_capacity); |
e4991b24 RR |
1567 | |
1568 | /* Would this change make things worse? */ | |
1569 | return (imb > old_imb); | |
e63da036 RR |
1570 | } |
1571 | ||
6fd98e77 SD |
1572 | /* |
1573 | * Maximum NUMA importance can be 1998 (2*999); | |
1574 | * SMALLIMP @ 30 would be close to 1998/64. | |
1575 | * Used to deter task migration. | |
1576 | */ | |
1577 | #define SMALLIMP 30 | |
1578 | ||
fb13c7ee MG |
1579 | /* |
1580 | * This checks if the overall compute and NUMA accesses of the system would | |
1581 | * be improved if the source tasks was migrated to the target dst_cpu taking | |
1582 | * into account that it might be best if task running on the dst_cpu should | |
1583 | * be exchanged with the source task | |
1584 | */ | |
887c290e | 1585 | static void task_numa_compare(struct task_numa_env *env, |
305c1fac | 1586 | long taskimp, long groupimp, bool maymove) |
fb13c7ee | 1587 | { |
fb13c7ee MG |
1588 | struct rq *dst_rq = cpu_rq(env->dst_cpu); |
1589 | struct task_struct *cur; | |
28a21745 | 1590 | long src_load, dst_load; |
fb13c7ee | 1591 | long load; |
1c5d3eb3 | 1592 | long imp = env->p->numa_group ? groupimp : taskimp; |
0132c3e1 | 1593 | long moveimp = imp; |
7bd95320 | 1594 | int dist = env->dist; |
fb13c7ee | 1595 | |
a4739eca SD |
1596 | if (READ_ONCE(dst_rq->numa_migrate_on)) |
1597 | return; | |
1598 | ||
fb13c7ee | 1599 | rcu_read_lock(); |
bac78573 ON |
1600 | cur = task_rcu_dereference(&dst_rq->curr); |
1601 | if (cur && ((cur->flags & PF_EXITING) || is_idle_task(cur))) | |
fb13c7ee MG |
1602 | cur = NULL; |
1603 | ||
7af68335 PZ |
1604 | /* |
1605 | * Because we have preemption enabled we can get migrated around and | |
1606 | * end try selecting ourselves (current == env->p) as a swap candidate. | |
1607 | */ | |
1608 | if (cur == env->p) | |
1609 | goto unlock; | |
1610 | ||
305c1fac | 1611 | if (!cur) { |
6fd98e77 | 1612 | if (maymove && moveimp >= env->best_imp) |
305c1fac SD |
1613 | goto assign; |
1614 | else | |
1615 | goto unlock; | |
1616 | } | |
1617 | ||
fb13c7ee MG |
1618 | /* |
1619 | * "imp" is the fault differential for the source task between the | |
1620 | * source and destination node. Calculate the total differential for | |
1621 | * the source task and potential destination task. The more negative | |
305c1fac | 1622 | * the value is, the more remote accesses that would be expected to |
fb13c7ee MG |
1623 | * be incurred if the tasks were swapped. |
1624 | */ | |
305c1fac SD |
1625 | /* Skip this swap candidate if cannot move to the source cpu */ |
1626 | if (!cpumask_test_cpu(env->src_cpu, &cur->cpus_allowed)) | |
1627 | goto unlock; | |
fb13c7ee | 1628 | |
305c1fac SD |
1629 | /* |
1630 | * If dst and source tasks are in the same NUMA group, or not | |
1631 | * in any group then look only at task weights. | |
1632 | */ | |
1633 | if (cur->numa_group == env->p->numa_group) { | |
1634 | imp = taskimp + task_weight(cur, env->src_nid, dist) - | |
1635 | task_weight(cur, env->dst_nid, dist); | |
887c290e | 1636 | /* |
305c1fac SD |
1637 | * Add some hysteresis to prevent swapping the |
1638 | * tasks within a group over tiny differences. | |
887c290e | 1639 | */ |
305c1fac SD |
1640 | if (cur->numa_group) |
1641 | imp -= imp / 16; | |
1642 | } else { | |
1643 | /* | |
1644 | * Compare the group weights. If a task is all by itself | |
1645 | * (not part of a group), use the task weight instead. | |
1646 | */ | |
1647 | if (cur->numa_group && env->p->numa_group) | |
1648 | imp += group_weight(cur, env->src_nid, dist) - | |
1649 | group_weight(cur, env->dst_nid, dist); | |
1650 | else | |
1651 | imp += task_weight(cur, env->src_nid, dist) - | |
1652 | task_weight(cur, env->dst_nid, dist); | |
fb13c7ee MG |
1653 | } |
1654 | ||
305c1fac | 1655 | if (maymove && moveimp > imp && moveimp > env->best_imp) { |
6fd98e77 | 1656 | imp = moveimp; |
305c1fac | 1657 | cur = NULL; |
fb13c7ee | 1658 | goto assign; |
305c1fac | 1659 | } |
fb13c7ee | 1660 | |
6fd98e77 SD |
1661 | /* |
1662 | * If the NUMA importance is less than SMALLIMP, | |
1663 | * task migration might only result in ping pong | |
1664 | * of tasks and also hurt performance due to cache | |
1665 | * misses. | |
1666 | */ | |
1667 | if (imp < SMALLIMP || imp <= env->best_imp + SMALLIMP / 2) | |
1668 | goto unlock; | |
1669 | ||
fb13c7ee MG |
1670 | /* |
1671 | * In the overloaded case, try and keep the load balanced. | |
1672 | */ | |
305c1fac SD |
1673 | load = task_h_load(env->p) - task_h_load(cur); |
1674 | if (!load) | |
1675 | goto assign; | |
1676 | ||
e720fff6 PZ |
1677 | dst_load = env->dst_stats.load + load; |
1678 | src_load = env->src_stats.load - load; | |
fb13c7ee | 1679 | |
28a21745 | 1680 | if (load_too_imbalanced(src_load, dst_load, env)) |
fb13c7ee MG |
1681 | goto unlock; |
1682 | ||
305c1fac | 1683 | assign: |
ba7e5a27 RR |
1684 | /* |
1685 | * One idle CPU per node is evaluated for a task numa move. | |
1686 | * Call select_idle_sibling to maybe find a better one. | |
1687 | */ | |
10e2f1ac PZ |
1688 | if (!cur) { |
1689 | /* | |
97fb7a0a | 1690 | * select_idle_siblings() uses an per-CPU cpumask that |
10e2f1ac PZ |
1691 | * can be used from IRQ context. |
1692 | */ | |
1693 | local_irq_disable(); | |
772bd008 MR |
1694 | env->dst_cpu = select_idle_sibling(env->p, env->src_cpu, |
1695 | env->dst_cpu); | |
10e2f1ac PZ |
1696 | local_irq_enable(); |
1697 | } | |
ba7e5a27 | 1698 | |
fb13c7ee MG |
1699 | task_numa_assign(env, cur, imp); |
1700 | unlock: | |
1701 | rcu_read_unlock(); | |
1702 | } | |
1703 | ||
887c290e RR |
1704 | static void task_numa_find_cpu(struct task_numa_env *env, |
1705 | long taskimp, long groupimp) | |
2c8a50aa | 1706 | { |
305c1fac SD |
1707 | long src_load, dst_load, load; |
1708 | bool maymove = false; | |
2c8a50aa MG |
1709 | int cpu; |
1710 | ||
305c1fac SD |
1711 | load = task_h_load(env->p); |
1712 | dst_load = env->dst_stats.load + load; | |
1713 | src_load = env->src_stats.load - load; | |
1714 | ||
1715 | /* | |
1716 | * If the improvement from just moving env->p direction is better | |
1717 | * than swapping tasks around, check if a move is possible. | |
1718 | */ | |
1719 | maymove = !load_too_imbalanced(src_load, dst_load, env); | |
1720 | ||
2c8a50aa MG |
1721 | for_each_cpu(cpu, cpumask_of_node(env->dst_nid)) { |
1722 | /* Skip this CPU if the source task cannot migrate */ | |
0c98d344 | 1723 | if (!cpumask_test_cpu(cpu, &env->p->cpus_allowed)) |
2c8a50aa MG |
1724 | continue; |
1725 | ||
1726 | env->dst_cpu = cpu; | |
305c1fac | 1727 | task_numa_compare(env, taskimp, groupimp, maymove); |
2c8a50aa MG |
1728 | } |
1729 | } | |
1730 | ||
58d081b5 MG |
1731 | static int task_numa_migrate(struct task_struct *p) |
1732 | { | |
58d081b5 MG |
1733 | struct task_numa_env env = { |
1734 | .p = p, | |
fb13c7ee | 1735 | |
58d081b5 | 1736 | .src_cpu = task_cpu(p), |
b32e86b4 | 1737 | .src_nid = task_node(p), |
fb13c7ee MG |
1738 | |
1739 | .imbalance_pct = 112, | |
1740 | ||
1741 | .best_task = NULL, | |
1742 | .best_imp = 0, | |
4142c3eb | 1743 | .best_cpu = -1, |
58d081b5 MG |
1744 | }; |
1745 | struct sched_domain *sd; | |
a4739eca | 1746 | struct rq *best_rq; |
887c290e | 1747 | unsigned long taskweight, groupweight; |
7bd95320 | 1748 | int nid, ret, dist; |
887c290e | 1749 | long taskimp, groupimp; |
e6628d5b | 1750 | |
58d081b5 | 1751 | /* |
fb13c7ee MG |
1752 | * Pick the lowest SD_NUMA domain, as that would have the smallest |
1753 | * imbalance and would be the first to start moving tasks about. | |
1754 | * | |
1755 | * And we want to avoid any moving of tasks about, as that would create | |
1756 | * random movement of tasks -- counter the numa conditions we're trying | |
1757 | * to satisfy here. | |
58d081b5 MG |
1758 | */ |
1759 | rcu_read_lock(); | |
fb13c7ee | 1760 | sd = rcu_dereference(per_cpu(sd_numa, env.src_cpu)); |
46a73e8a RR |
1761 | if (sd) |
1762 | env.imbalance_pct = 100 + (sd->imbalance_pct - 100) / 2; | |
e6628d5b MG |
1763 | rcu_read_unlock(); |
1764 | ||
46a73e8a RR |
1765 | /* |
1766 | * Cpusets can break the scheduler domain tree into smaller | |
1767 | * balance domains, some of which do not cross NUMA boundaries. | |
1768 | * Tasks that are "trapped" in such domains cannot be migrated | |
1769 | * elsewhere, so there is no point in (re)trying. | |
1770 | */ | |
1771 | if (unlikely(!sd)) { | |
8cd45eee | 1772 | sched_setnuma(p, task_node(p)); |
46a73e8a RR |
1773 | return -EINVAL; |
1774 | } | |
1775 | ||
2c8a50aa | 1776 | env.dst_nid = p->numa_preferred_nid; |
7bd95320 RR |
1777 | dist = env.dist = node_distance(env.src_nid, env.dst_nid); |
1778 | taskweight = task_weight(p, env.src_nid, dist); | |
1779 | groupweight = group_weight(p, env.src_nid, dist); | |
1780 | update_numa_stats(&env.src_stats, env.src_nid); | |
1781 | taskimp = task_weight(p, env.dst_nid, dist) - taskweight; | |
1782 | groupimp = group_weight(p, env.dst_nid, dist) - groupweight; | |
2c8a50aa | 1783 | update_numa_stats(&env.dst_stats, env.dst_nid); |
58d081b5 | 1784 | |
a43455a1 | 1785 | /* Try to find a spot on the preferred nid. */ |
2d4056fa | 1786 | task_numa_find_cpu(&env, taskimp, groupimp); |
e1dda8a7 | 1787 | |
9de05d48 RR |
1788 | /* |
1789 | * Look at other nodes in these cases: | |
1790 | * - there is no space available on the preferred_nid | |
1791 | * - the task is part of a numa_group that is interleaved across | |
1792 | * multiple NUMA nodes; in order to better consolidate the group, | |
1793 | * we need to check other locations. | |
1794 | */ | |
4142c3eb | 1795 | if (env.best_cpu == -1 || (p->numa_group && p->numa_group->active_nodes > 1)) { |
2c8a50aa MG |
1796 | for_each_online_node(nid) { |
1797 | if (nid == env.src_nid || nid == p->numa_preferred_nid) | |
1798 | continue; | |
58d081b5 | 1799 | |
7bd95320 | 1800 | dist = node_distance(env.src_nid, env.dst_nid); |
6c6b1193 RR |
1801 | if (sched_numa_topology_type == NUMA_BACKPLANE && |
1802 | dist != env.dist) { | |
1803 | taskweight = task_weight(p, env.src_nid, dist); | |
1804 | groupweight = group_weight(p, env.src_nid, dist); | |
1805 | } | |
7bd95320 | 1806 | |
83e1d2cd | 1807 | /* Only consider nodes where both task and groups benefit */ |
7bd95320 RR |
1808 | taskimp = task_weight(p, nid, dist) - taskweight; |
1809 | groupimp = group_weight(p, nid, dist) - groupweight; | |
887c290e | 1810 | if (taskimp < 0 && groupimp < 0) |
fb13c7ee MG |
1811 | continue; |
1812 | ||
7bd95320 | 1813 | env.dist = dist; |
2c8a50aa MG |
1814 | env.dst_nid = nid; |
1815 | update_numa_stats(&env.dst_stats, env.dst_nid); | |
2d4056fa | 1816 | task_numa_find_cpu(&env, taskimp, groupimp); |
58d081b5 MG |
1817 | } |
1818 | } | |
1819 | ||
68d1b02a RR |
1820 | /* |
1821 | * If the task is part of a workload that spans multiple NUMA nodes, | |
1822 | * and is migrating into one of the workload's active nodes, remember | |
1823 | * this node as the task's preferred numa node, so the workload can | |
1824 | * settle down. | |
1825 | * A task that migrated to a second choice node will be better off | |
1826 | * trying for a better one later. Do not set the preferred node here. | |
1827 | */ | |
db015dae RR |
1828 | if (p->numa_group) { |
1829 | if (env.best_cpu == -1) | |
1830 | nid = env.src_nid; | |
1831 | else | |
8cd45eee | 1832 | nid = cpu_to_node(env.best_cpu); |
db015dae | 1833 | |
8cd45eee SD |
1834 | if (nid != p->numa_preferred_nid) |
1835 | sched_setnuma(p, nid); | |
db015dae RR |
1836 | } |
1837 | ||
1838 | /* No better CPU than the current one was found. */ | |
1839 | if (env.best_cpu == -1) | |
1840 | return -EAGAIN; | |
0ec8aa00 | 1841 | |
a4739eca | 1842 | best_rq = cpu_rq(env.best_cpu); |
fb13c7ee | 1843 | if (env.best_task == NULL) { |
286549dc | 1844 | ret = migrate_task_to(p, env.best_cpu); |
a4739eca | 1845 | WRITE_ONCE(best_rq->numa_migrate_on, 0); |
286549dc MG |
1846 | if (ret != 0) |
1847 | trace_sched_stick_numa(p, env.src_cpu, env.best_cpu); | |
fb13c7ee MG |
1848 | return ret; |
1849 | } | |
1850 | ||
0ad4e3df | 1851 | ret = migrate_swap(p, env.best_task, env.best_cpu, env.src_cpu); |
a4739eca | 1852 | WRITE_ONCE(best_rq->numa_migrate_on, 0); |
0ad4e3df | 1853 | |
286549dc MG |
1854 | if (ret != 0) |
1855 | trace_sched_stick_numa(p, env.src_cpu, task_cpu(env.best_task)); | |
fb13c7ee MG |
1856 | put_task_struct(env.best_task); |
1857 | return ret; | |
e6628d5b MG |
1858 | } |
1859 | ||
6b9a7460 MG |
1860 | /* Attempt to migrate a task to a CPU on the preferred node. */ |
1861 | static void numa_migrate_preferred(struct task_struct *p) | |
1862 | { | |
5085e2a3 RR |
1863 | unsigned long interval = HZ; |
1864 | ||
2739d3ee | 1865 | /* This task has no NUMA fault statistics yet */ |
44dba3d5 | 1866 | if (unlikely(p->numa_preferred_nid == -1 || !p->numa_faults)) |
6b9a7460 MG |
1867 | return; |
1868 | ||
2739d3ee | 1869 | /* Periodically retry migrating the task to the preferred node */ |
5085e2a3 | 1870 | interval = min(interval, msecs_to_jiffies(p->numa_scan_period) / 16); |
789ba280 | 1871 | p->numa_migrate_retry = jiffies + interval; |
2739d3ee RR |
1872 | |
1873 | /* Success if task is already running on preferred CPU */ | |
de1b301a | 1874 | if (task_node(p) == p->numa_preferred_nid) |
6b9a7460 MG |
1875 | return; |
1876 | ||
1877 | /* Otherwise, try migrate to a CPU on the preferred node */ | |
2739d3ee | 1878 | task_numa_migrate(p); |
6b9a7460 MG |
1879 | } |
1880 | ||
20e07dea | 1881 | /* |
4142c3eb | 1882 | * Find out how many nodes on the workload is actively running on. Do this by |
20e07dea RR |
1883 | * tracking the nodes from which NUMA hinting faults are triggered. This can |
1884 | * be different from the set of nodes where the workload's memory is currently | |
1885 | * located. | |
20e07dea | 1886 | */ |
4142c3eb | 1887 | static void numa_group_count_active_nodes(struct numa_group *numa_group) |
20e07dea RR |
1888 | { |
1889 | unsigned long faults, max_faults = 0; | |
4142c3eb | 1890 | int nid, active_nodes = 0; |
20e07dea RR |
1891 | |
1892 | for_each_online_node(nid) { | |
1893 | faults = group_faults_cpu(numa_group, nid); | |
1894 | if (faults > max_faults) | |
1895 | max_faults = faults; | |
1896 | } | |
1897 | ||
1898 | for_each_online_node(nid) { | |
1899 | faults = group_faults_cpu(numa_group, nid); | |
4142c3eb RR |
1900 | if (faults * ACTIVE_NODE_FRACTION > max_faults) |
1901 | active_nodes++; | |
20e07dea | 1902 | } |
4142c3eb RR |
1903 | |
1904 | numa_group->max_faults_cpu = max_faults; | |
1905 | numa_group->active_nodes = active_nodes; | |
20e07dea RR |
1906 | } |
1907 | ||
04bb2f94 RR |
1908 | /* |
1909 | * When adapting the scan rate, the period is divided into NUMA_PERIOD_SLOTS | |
1910 | * increments. The more local the fault statistics are, the higher the scan | |
a22b4b01 RR |
1911 | * period will be for the next scan window. If local/(local+remote) ratio is |
1912 | * below NUMA_PERIOD_THRESHOLD (where range of ratio is 1..NUMA_PERIOD_SLOTS) | |
1913 | * the scan period will decrease. Aim for 70% local accesses. | |
04bb2f94 RR |
1914 | */ |
1915 | #define NUMA_PERIOD_SLOTS 10 | |
a22b4b01 | 1916 | #define NUMA_PERIOD_THRESHOLD 7 |
04bb2f94 RR |
1917 | |
1918 | /* | |
1919 | * Increase the scan period (slow down scanning) if the majority of | |
1920 | * our memory is already on our local node, or if the majority of | |
1921 | * the page accesses are shared with other processes. | |
1922 | * Otherwise, decrease the scan period. | |
1923 | */ | |
1924 | static void update_task_scan_period(struct task_struct *p, | |
1925 | unsigned long shared, unsigned long private) | |
1926 | { | |
1927 | unsigned int period_slot; | |
37ec97de | 1928 | int lr_ratio, ps_ratio; |
04bb2f94 RR |
1929 | int diff; |
1930 | ||
1931 | unsigned long remote = p->numa_faults_locality[0]; | |
1932 | unsigned long local = p->numa_faults_locality[1]; | |
1933 | ||
1934 | /* | |
1935 | * If there were no record hinting faults then either the task is | |
1936 | * completely idle or all activity is areas that are not of interest | |
074c2381 MG |
1937 | * to automatic numa balancing. Related to that, if there were failed |
1938 | * migration then it implies we are migrating too quickly or the local | |
1939 | * node is overloaded. In either case, scan slower | |
04bb2f94 | 1940 | */ |
074c2381 | 1941 | if (local + shared == 0 || p->numa_faults_locality[2]) { |
04bb2f94 RR |
1942 | p->numa_scan_period = min(p->numa_scan_period_max, |
1943 | p->numa_scan_period << 1); | |
1944 | ||
1945 | p->mm->numa_next_scan = jiffies + | |
1946 | msecs_to_jiffies(p->numa_scan_period); | |
1947 | ||
1948 | return; | |
1949 | } | |
1950 | ||
1951 | /* | |
1952 | * Prepare to scale scan period relative to the current period. | |
1953 | * == NUMA_PERIOD_THRESHOLD scan period stays the same | |
1954 | * < NUMA_PERIOD_THRESHOLD scan period decreases (scan faster) | |
1955 | * >= NUMA_PERIOD_THRESHOLD scan period increases (scan slower) | |
1956 | */ | |
1957 | period_slot = DIV_ROUND_UP(p->numa_scan_period, NUMA_PERIOD_SLOTS); | |
37ec97de RR |
1958 | lr_ratio = (local * NUMA_PERIOD_SLOTS) / (local + remote); |
1959 | ps_ratio = (private * NUMA_PERIOD_SLOTS) / (private + shared); | |
1960 | ||
1961 | if (ps_ratio >= NUMA_PERIOD_THRESHOLD) { | |
1962 | /* | |
1963 | * Most memory accesses are local. There is no need to | |
1964 | * do fast NUMA scanning, since memory is already local. | |
1965 | */ | |
1966 | int slot = ps_ratio - NUMA_PERIOD_THRESHOLD; | |
1967 | if (!slot) | |
1968 | slot = 1; | |
1969 | diff = slot * period_slot; | |
1970 | } else if (lr_ratio >= NUMA_PERIOD_THRESHOLD) { | |
1971 | /* | |
1972 | * Most memory accesses are shared with other tasks. | |
1973 | * There is no point in continuing fast NUMA scanning, | |
1974 | * since other tasks may just move the memory elsewhere. | |
1975 | */ | |
1976 | int slot = lr_ratio - NUMA_PERIOD_THRESHOLD; | |
04bb2f94 RR |
1977 | if (!slot) |
1978 | slot = 1; | |
1979 | diff = slot * period_slot; | |
1980 | } else { | |
04bb2f94 | 1981 | /* |
37ec97de RR |
1982 | * Private memory faults exceed (SLOTS-THRESHOLD)/SLOTS, |
1983 | * yet they are not on the local NUMA node. Speed up | |
1984 | * NUMA scanning to get the memory moved over. | |
04bb2f94 | 1985 | */ |
37ec97de RR |
1986 | int ratio = max(lr_ratio, ps_ratio); |
1987 | diff = -(NUMA_PERIOD_THRESHOLD - ratio) * period_slot; | |
04bb2f94 RR |
1988 | } |
1989 | ||
1990 | p->numa_scan_period = clamp(p->numa_scan_period + diff, | |
1991 | task_scan_min(p), task_scan_max(p)); | |
1992 | memset(p->numa_faults_locality, 0, sizeof(p->numa_faults_locality)); | |
1993 | } | |
1994 | ||
7e2703e6 RR |
1995 | /* |
1996 | * Get the fraction of time the task has been running since the last | |
1997 | * NUMA placement cycle. The scheduler keeps similar statistics, but | |
1998 | * decays those on a 32ms period, which is orders of magnitude off | |
1999 | * from the dozens-of-seconds NUMA balancing period. Use the scheduler | |
2000 | * stats only if the task is so new there are no NUMA statistics yet. | |
2001 | */ | |
2002 | static u64 numa_get_avg_runtime(struct task_struct *p, u64 *period) | |
2003 | { | |
2004 | u64 runtime, delta, now; | |
2005 | /* Use the start of this time slice to avoid calculations. */ | |
2006 | now = p->se.exec_start; | |
2007 | runtime = p->se.sum_exec_runtime; | |
2008 | ||
2009 | if (p->last_task_numa_placement) { | |
2010 | delta = runtime - p->last_sum_exec_runtime; | |
2011 | *period = now - p->last_task_numa_placement; | |
2012 | } else { | |
c7b50216 | 2013 | delta = p->se.avg.load_sum; |
9d89c257 | 2014 | *period = LOAD_AVG_MAX; |
7e2703e6 RR |
2015 | } |
2016 | ||
2017 | p->last_sum_exec_runtime = runtime; | |
2018 | p->last_task_numa_placement = now; | |
2019 | ||
2020 | return delta; | |
2021 | } | |
2022 | ||
54009416 RR |
2023 | /* |
2024 | * Determine the preferred nid for a task in a numa_group. This needs to | |
2025 | * be done in a way that produces consistent results with group_weight, | |
2026 | * otherwise workloads might not converge. | |
2027 | */ | |
2028 | static int preferred_group_nid(struct task_struct *p, int nid) | |
2029 | { | |
2030 | nodemask_t nodes; | |
2031 | int dist; | |
2032 | ||
2033 | /* Direct connections between all NUMA nodes. */ | |
2034 | if (sched_numa_topology_type == NUMA_DIRECT) | |
2035 | return nid; | |
2036 | ||
2037 | /* | |
2038 | * On a system with glueless mesh NUMA topology, group_weight | |
2039 | * scores nodes according to the number of NUMA hinting faults on | |
2040 | * both the node itself, and on nearby nodes. | |
2041 | */ | |
2042 | if (sched_numa_topology_type == NUMA_GLUELESS_MESH) { | |
2043 | unsigned long score, max_score = 0; | |
2044 | int node, max_node = nid; | |
2045 | ||
2046 | dist = sched_max_numa_distance; | |
2047 | ||
2048 | for_each_online_node(node) { | |
2049 | score = group_weight(p, node, dist); | |
2050 | if (score > max_score) { | |
2051 | max_score = score; | |
2052 | max_node = node; | |
2053 | } | |
2054 | } | |
2055 | return max_node; | |
2056 | } | |
2057 | ||
2058 | /* | |
2059 | * Finding the preferred nid in a system with NUMA backplane | |
2060 | * interconnect topology is more involved. The goal is to locate | |
2061 | * tasks from numa_groups near each other in the system, and | |
2062 | * untangle workloads from different sides of the system. This requires | |
2063 | * searching down the hierarchy of node groups, recursively searching | |
2064 | * inside the highest scoring group of nodes. The nodemask tricks | |
2065 | * keep the complexity of the search down. | |
2066 | */ | |
2067 | nodes = node_online_map; | |
2068 | for (dist = sched_max_numa_distance; dist > LOCAL_DISTANCE; dist--) { | |
2069 | unsigned long max_faults = 0; | |
81907478 | 2070 | nodemask_t max_group = NODE_MASK_NONE; |
54009416 RR |
2071 | int a, b; |
2072 | ||
2073 | /* Are there nodes at this distance from each other? */ | |
2074 | if (!find_numa_distance(dist)) | |
2075 | continue; | |
2076 | ||
2077 | for_each_node_mask(a, nodes) { | |
2078 | unsigned long faults = 0; | |
2079 | nodemask_t this_group; | |
2080 | nodes_clear(this_group); | |
2081 | ||
2082 | /* Sum group's NUMA faults; includes a==b case. */ | |
2083 | for_each_node_mask(b, nodes) { | |
2084 | if (node_distance(a, b) < dist) { | |
2085 | faults += group_faults(p, b); | |
2086 | node_set(b, this_group); | |
2087 | node_clear(b, nodes); | |
2088 | } | |
2089 | } | |
2090 | ||
2091 | /* Remember the top group. */ | |
2092 | if (faults > max_faults) { | |
2093 | max_faults = faults; | |
2094 | max_group = this_group; | |
2095 | /* | |
2096 | * subtle: at the smallest distance there is | |
2097 | * just one node left in each "group", the | |
2098 | * winner is the preferred nid. | |
2099 | */ | |
2100 | nid = a; | |
2101 | } | |
2102 | } | |
2103 | /* Next round, evaluate the nodes within max_group. */ | |
890a5409 JB |
2104 | if (!max_faults) |
2105 | break; | |
54009416 RR |
2106 | nodes = max_group; |
2107 | } | |
2108 | return nid; | |
2109 | } | |
2110 | ||
cbee9f88 PZ |
2111 | static void task_numa_placement(struct task_struct *p) |
2112 | { | |
f03bb676 SD |
2113 | int seq, nid, max_nid = -1; |
2114 | unsigned long max_faults = 0; | |
04bb2f94 | 2115 | unsigned long fault_types[2] = { 0, 0 }; |
7e2703e6 RR |
2116 | unsigned long total_faults; |
2117 | u64 runtime, period; | |
7dbd13ed | 2118 | spinlock_t *group_lock = NULL; |
cbee9f88 | 2119 | |
7e5a2c17 JL |
2120 | /* |
2121 | * The p->mm->numa_scan_seq field gets updated without | |
2122 | * exclusive access. Use READ_ONCE() here to ensure | |
2123 | * that the field is read in a single access: | |
2124 | */ | |
316c1608 | 2125 | seq = READ_ONCE(p->mm->numa_scan_seq); |
cbee9f88 PZ |
2126 | if (p->numa_scan_seq == seq) |
2127 | return; | |
2128 | p->numa_scan_seq = seq; | |
598f0ec0 | 2129 | p->numa_scan_period_max = task_scan_max(p); |
cbee9f88 | 2130 | |
7e2703e6 RR |
2131 | total_faults = p->numa_faults_locality[0] + |
2132 | p->numa_faults_locality[1]; | |
2133 | runtime = numa_get_avg_runtime(p, &period); | |
2134 | ||
7dbd13ed MG |
2135 | /* If the task is part of a group prevent parallel updates to group stats */ |
2136 | if (p->numa_group) { | |
2137 | group_lock = &p->numa_group->lock; | |
60e69eed | 2138 | spin_lock_irq(group_lock); |
7dbd13ed MG |
2139 | } |
2140 | ||
688b7585 MG |
2141 | /* Find the node with the highest number of faults */ |
2142 | for_each_online_node(nid) { | |
44dba3d5 IM |
2143 | /* Keep track of the offsets in numa_faults array */ |
2144 | int mem_idx, membuf_idx, cpu_idx, cpubuf_idx; | |
83e1d2cd | 2145 | unsigned long faults = 0, group_faults = 0; |
44dba3d5 | 2146 | int priv; |
745d6147 | 2147 | |
be1e4e76 | 2148 | for (priv = 0; priv < NR_NUMA_HINT_FAULT_TYPES; priv++) { |
7e2703e6 | 2149 | long diff, f_diff, f_weight; |
8c8a743c | 2150 | |
44dba3d5 IM |
2151 | mem_idx = task_faults_idx(NUMA_MEM, nid, priv); |
2152 | membuf_idx = task_faults_idx(NUMA_MEMBUF, nid, priv); | |
2153 | cpu_idx = task_faults_idx(NUMA_CPU, nid, priv); | |
2154 | cpubuf_idx = task_faults_idx(NUMA_CPUBUF, nid, priv); | |
745d6147 | 2155 | |
ac8e895b | 2156 | /* Decay existing window, copy faults since last scan */ |
44dba3d5 IM |
2157 | diff = p->numa_faults[membuf_idx] - p->numa_faults[mem_idx] / 2; |
2158 | fault_types[priv] += p->numa_faults[membuf_idx]; | |
2159 | p->numa_faults[membuf_idx] = 0; | |
fb13c7ee | 2160 | |
7e2703e6 RR |
2161 | /* |
2162 | * Normalize the faults_from, so all tasks in a group | |
2163 | * count according to CPU use, instead of by the raw | |
2164 | * number of faults. Tasks with little runtime have | |
2165 | * little over-all impact on throughput, and thus their | |
2166 | * faults are less important. | |
2167 | */ | |
2168 | f_weight = div64_u64(runtime << 16, period + 1); | |
44dba3d5 | 2169 | f_weight = (f_weight * p->numa_faults[cpubuf_idx]) / |
7e2703e6 | 2170 | (total_faults + 1); |
44dba3d5 IM |
2171 | f_diff = f_weight - p->numa_faults[cpu_idx] / 2; |
2172 | p->numa_faults[cpubuf_idx] = 0; | |
50ec8a40 | 2173 | |
44dba3d5 IM |
2174 | p->numa_faults[mem_idx] += diff; |
2175 | p->numa_faults[cpu_idx] += f_diff; | |
2176 | faults += p->numa_faults[mem_idx]; | |
83e1d2cd | 2177 | p->total_numa_faults += diff; |
8c8a743c | 2178 | if (p->numa_group) { |
44dba3d5 IM |
2179 | /* |
2180 | * safe because we can only change our own group | |
2181 | * | |
2182 | * mem_idx represents the offset for a given | |
2183 | * nid and priv in a specific region because it | |
2184 | * is at the beginning of the numa_faults array. | |
2185 | */ | |
2186 | p->numa_group->faults[mem_idx] += diff; | |
2187 | p->numa_group->faults_cpu[mem_idx] += f_diff; | |
989348b5 | 2188 | p->numa_group->total_faults += diff; |
44dba3d5 | 2189 | group_faults += p->numa_group->faults[mem_idx]; |
8c8a743c | 2190 | } |
ac8e895b MG |
2191 | } |
2192 | ||
f03bb676 SD |
2193 | if (!p->numa_group) { |
2194 | if (faults > max_faults) { | |
2195 | max_faults = faults; | |
2196 | max_nid = nid; | |
2197 | } | |
2198 | } else if (group_faults > max_faults) { | |
2199 | max_faults = group_faults; | |
688b7585 MG |
2200 | max_nid = nid; |
2201 | } | |
83e1d2cd MG |
2202 | } |
2203 | ||
7dbd13ed | 2204 | if (p->numa_group) { |
4142c3eb | 2205 | numa_group_count_active_nodes(p->numa_group); |
60e69eed | 2206 | spin_unlock_irq(group_lock); |
f03bb676 | 2207 | max_nid = preferred_group_nid(p, max_nid); |
688b7585 MG |
2208 | } |
2209 | ||
bb97fc31 RR |
2210 | if (max_faults) { |
2211 | /* Set the new preferred node */ | |
2212 | if (max_nid != p->numa_preferred_nid) | |
2213 | sched_setnuma(p, max_nid); | |
3a7053b3 | 2214 | } |
30619c89 SD |
2215 | |
2216 | update_task_scan_period(p, fault_types[0], fault_types[1]); | |
cbee9f88 PZ |
2217 | } |
2218 | ||
8c8a743c PZ |
2219 | static inline int get_numa_group(struct numa_group *grp) |
2220 | { | |
c45a7795 | 2221 | return refcount_inc_not_zero(&grp->refcount); |
8c8a743c PZ |
2222 | } |
2223 | ||
2224 | static inline void put_numa_group(struct numa_group *grp) | |
2225 | { | |
c45a7795 | 2226 | if (refcount_dec_and_test(&grp->refcount)) |
8c8a743c PZ |
2227 | kfree_rcu(grp, rcu); |
2228 | } | |
2229 | ||
3e6a9418 MG |
2230 | static void task_numa_group(struct task_struct *p, int cpupid, int flags, |
2231 | int *priv) | |
8c8a743c PZ |
2232 | { |
2233 | struct numa_group *grp, *my_grp; | |
2234 | struct task_struct *tsk; | |
2235 | bool join = false; | |
2236 | int cpu = cpupid_to_cpu(cpupid); | |
2237 | int i; | |
2238 | ||
2239 | if (unlikely(!p->numa_group)) { | |
2240 | unsigned int size = sizeof(struct numa_group) + | |
50ec8a40 | 2241 | 4*nr_node_ids*sizeof(unsigned long); |
8c8a743c PZ |
2242 | |
2243 | grp = kzalloc(size, GFP_KERNEL | __GFP_NOWARN); | |
2244 | if (!grp) | |
2245 | return; | |
2246 | ||
c45a7795 | 2247 | refcount_set(&grp->refcount, 1); |
4142c3eb RR |
2248 | grp->active_nodes = 1; |
2249 | grp->max_faults_cpu = 0; | |
8c8a743c | 2250 | spin_lock_init(&grp->lock); |
e29cf08b | 2251 | grp->gid = p->pid; |
50ec8a40 | 2252 | /* Second half of the array tracks nids where faults happen */ |
be1e4e76 RR |
2253 | grp->faults_cpu = grp->faults + NR_NUMA_HINT_FAULT_TYPES * |
2254 | nr_node_ids; | |
8c8a743c | 2255 | |
be1e4e76 | 2256 | for (i = 0; i < NR_NUMA_HINT_FAULT_STATS * nr_node_ids; i++) |
44dba3d5 | 2257 | grp->faults[i] = p->numa_faults[i]; |
8c8a743c | 2258 | |
989348b5 | 2259 | grp->total_faults = p->total_numa_faults; |
83e1d2cd | 2260 | |
8c8a743c PZ |
2261 | grp->nr_tasks++; |
2262 | rcu_assign_pointer(p->numa_group, grp); | |
2263 | } | |
2264 | ||
2265 | rcu_read_lock(); | |
316c1608 | 2266 | tsk = READ_ONCE(cpu_rq(cpu)->curr); |
8c8a743c PZ |
2267 | |
2268 | if (!cpupid_match_pid(tsk, cpupid)) | |
3354781a | 2269 | goto no_join; |
8c8a743c PZ |
2270 | |
2271 | grp = rcu_dereference(tsk->numa_group); | |
2272 | if (!grp) | |
3354781a | 2273 | goto no_join; |
8c8a743c PZ |
2274 | |
2275 | my_grp = p->numa_group; | |
2276 | if (grp == my_grp) | |
3354781a | 2277 | goto no_join; |
8c8a743c PZ |
2278 | |
2279 | /* | |
2280 | * Only join the other group if its bigger; if we're the bigger group, | |
2281 | * the other task will join us. | |
2282 | */ | |
2283 | if (my_grp->nr_tasks > grp->nr_tasks) | |
3354781a | 2284 | goto no_join; |
8c8a743c PZ |
2285 | |
2286 | /* | |
2287 | * Tie-break on the grp address. | |
2288 | */ | |
2289 | if (my_grp->nr_tasks == grp->nr_tasks && my_grp > grp) | |
3354781a | 2290 | goto no_join; |
8c8a743c | 2291 | |
dabe1d99 RR |
2292 | /* Always join threads in the same process. */ |
2293 | if (tsk->mm == current->mm) | |
2294 | join = true; | |
2295 | ||
2296 | /* Simple filter to avoid false positives due to PID collisions */ | |
2297 | if (flags & TNF_SHARED) | |
2298 | join = true; | |
8c8a743c | 2299 | |
3e6a9418 MG |
2300 | /* Update priv based on whether false sharing was detected */ |
2301 | *priv = !join; | |
2302 | ||
dabe1d99 | 2303 | if (join && !get_numa_group(grp)) |
3354781a | 2304 | goto no_join; |
8c8a743c | 2305 | |
8c8a743c PZ |
2306 | rcu_read_unlock(); |
2307 | ||
2308 | if (!join) | |
2309 | return; | |
2310 | ||
60e69eed MG |
2311 | BUG_ON(irqs_disabled()); |
2312 | double_lock_irq(&my_grp->lock, &grp->lock); | |
989348b5 | 2313 | |
be1e4e76 | 2314 | for (i = 0; i < NR_NUMA_HINT_FAULT_STATS * nr_node_ids; i++) { |
44dba3d5 IM |
2315 | my_grp->faults[i] -= p->numa_faults[i]; |
2316 | grp->faults[i] += p->numa_faults[i]; | |
8c8a743c | 2317 | } |
989348b5 MG |
2318 | my_grp->total_faults -= p->total_numa_faults; |
2319 | grp->total_faults += p->total_numa_faults; | |
8c8a743c | 2320 | |
8c8a743c PZ |
2321 | my_grp->nr_tasks--; |
2322 | grp->nr_tasks++; | |
2323 | ||
2324 | spin_unlock(&my_grp->lock); | |
60e69eed | 2325 | spin_unlock_irq(&grp->lock); |
8c8a743c PZ |
2326 | |
2327 | rcu_assign_pointer(p->numa_group, grp); | |
2328 | ||
2329 | put_numa_group(my_grp); | |
3354781a PZ |
2330 | return; |
2331 | ||
2332 | no_join: | |
2333 | rcu_read_unlock(); | |
2334 | return; | |
8c8a743c PZ |
2335 | } |
2336 | ||
2337 | void task_numa_free(struct task_struct *p) | |
2338 | { | |
2339 | struct numa_group *grp = p->numa_group; | |
44dba3d5 | 2340 | void *numa_faults = p->numa_faults; |
e9dd685c SR |
2341 | unsigned long flags; |
2342 | int i; | |
8c8a743c PZ |
2343 | |
2344 | if (grp) { | |
e9dd685c | 2345 | spin_lock_irqsave(&grp->lock, flags); |
be1e4e76 | 2346 | for (i = 0; i < NR_NUMA_HINT_FAULT_STATS * nr_node_ids; i++) |
44dba3d5 | 2347 | grp->faults[i] -= p->numa_faults[i]; |
989348b5 | 2348 | grp->total_faults -= p->total_numa_faults; |
83e1d2cd | 2349 | |
8c8a743c | 2350 | grp->nr_tasks--; |
e9dd685c | 2351 | spin_unlock_irqrestore(&grp->lock, flags); |
35b123e2 | 2352 | RCU_INIT_POINTER(p->numa_group, NULL); |
8c8a743c PZ |
2353 | put_numa_group(grp); |
2354 | } | |
2355 | ||
44dba3d5 | 2356 | p->numa_faults = NULL; |
82727018 | 2357 | kfree(numa_faults); |
8c8a743c PZ |
2358 | } |
2359 | ||
cbee9f88 PZ |
2360 | /* |
2361 | * Got a PROT_NONE fault for a page on @node. | |
2362 | */ | |
58b46da3 | 2363 | void task_numa_fault(int last_cpupid, int mem_node, int pages, int flags) |
cbee9f88 PZ |
2364 | { |
2365 | struct task_struct *p = current; | |
6688cc05 | 2366 | bool migrated = flags & TNF_MIGRATED; |
58b46da3 | 2367 | int cpu_node = task_node(current); |
792568ec | 2368 | int local = !!(flags & TNF_FAULT_LOCAL); |
4142c3eb | 2369 | struct numa_group *ng; |
ac8e895b | 2370 | int priv; |
cbee9f88 | 2371 | |
2a595721 | 2372 | if (!static_branch_likely(&sched_numa_balancing)) |
1a687c2e MG |
2373 | return; |
2374 | ||
9ff1d9ff MG |
2375 | /* for example, ksmd faulting in a user's mm */ |
2376 | if (!p->mm) | |
2377 | return; | |
2378 | ||
f809ca9a | 2379 | /* Allocate buffer to track faults on a per-node basis */ |
44dba3d5 IM |
2380 | if (unlikely(!p->numa_faults)) { |
2381 | int size = sizeof(*p->numa_faults) * | |
be1e4e76 | 2382 | NR_NUMA_HINT_FAULT_BUCKETS * nr_node_ids; |
f809ca9a | 2383 | |
44dba3d5 IM |
2384 | p->numa_faults = kzalloc(size, GFP_KERNEL|__GFP_NOWARN); |
2385 | if (!p->numa_faults) | |
f809ca9a | 2386 | return; |
745d6147 | 2387 | |
83e1d2cd | 2388 | p->total_numa_faults = 0; |
04bb2f94 | 2389 | memset(p->numa_faults_locality, 0, sizeof(p->numa_faults_locality)); |
f809ca9a | 2390 | } |
cbee9f88 | 2391 | |
8c8a743c PZ |
2392 | /* |
2393 | * First accesses are treated as private, otherwise consider accesses | |
2394 | * to be private if the accessing pid has not changed | |
2395 | */ | |
2396 | if (unlikely(last_cpupid == (-1 & LAST_CPUPID_MASK))) { | |
2397 | priv = 1; | |
2398 | } else { | |
2399 | priv = cpupid_match_pid(p, last_cpupid); | |
6688cc05 | 2400 | if (!priv && !(flags & TNF_NO_GROUP)) |
3e6a9418 | 2401 | task_numa_group(p, last_cpupid, flags, &priv); |
8c8a743c PZ |
2402 | } |
2403 | ||
792568ec RR |
2404 | /* |
2405 | * If a workload spans multiple NUMA nodes, a shared fault that | |
2406 | * occurs wholly within the set of nodes that the workload is | |
2407 | * actively using should be counted as local. This allows the | |
2408 | * scan rate to slow down when a workload has settled down. | |
2409 | */ | |
4142c3eb RR |
2410 | ng = p->numa_group; |
2411 | if (!priv && !local && ng && ng->active_nodes > 1 && | |
2412 | numa_is_active_node(cpu_node, ng) && | |
2413 | numa_is_active_node(mem_node, ng)) | |
792568ec RR |
2414 | local = 1; |
2415 | ||
2739d3ee | 2416 | /* |
e1ff516a YW |
2417 | * Retry to migrate task to preferred node periodically, in case it |
2418 | * previously failed, or the scheduler moved us. | |
2739d3ee | 2419 | */ |
b6a60cf3 SD |
2420 | if (time_after(jiffies, p->numa_migrate_retry)) { |
2421 | task_numa_placement(p); | |
6b9a7460 | 2422 | numa_migrate_preferred(p); |
b6a60cf3 | 2423 | } |
6b9a7460 | 2424 | |
b32e86b4 IM |
2425 | if (migrated) |
2426 | p->numa_pages_migrated += pages; | |
074c2381 MG |
2427 | if (flags & TNF_MIGRATE_FAIL) |
2428 | p->numa_faults_locality[2] += pages; | |
b32e86b4 | 2429 | |
44dba3d5 IM |
2430 | p->numa_faults[task_faults_idx(NUMA_MEMBUF, mem_node, priv)] += pages; |
2431 | p->numa_faults[task_faults_idx(NUMA_CPUBUF, cpu_node, priv)] += pages; | |
792568ec | 2432 | p->numa_faults_locality[local] += pages; |
cbee9f88 PZ |
2433 | } |
2434 | ||
6e5fb223 PZ |
2435 | static void reset_ptenuma_scan(struct task_struct *p) |
2436 | { | |
7e5a2c17 JL |
2437 | /* |
2438 | * We only did a read acquisition of the mmap sem, so | |
2439 | * p->mm->numa_scan_seq is written to without exclusive access | |
2440 | * and the update is not guaranteed to be atomic. That's not | |
2441 | * much of an issue though, since this is just used for | |
2442 | * statistical sampling. Use READ_ONCE/WRITE_ONCE, which are not | |
2443 | * expensive, to avoid any form of compiler optimizations: | |
2444 | */ | |
316c1608 | 2445 | WRITE_ONCE(p->mm->numa_scan_seq, READ_ONCE(p->mm->numa_scan_seq) + 1); |
6e5fb223 PZ |
2446 | p->mm->numa_scan_offset = 0; |
2447 | } | |
2448 | ||
cbee9f88 PZ |
2449 | /* |
2450 | * The expensive part of numa migration is done from task_work context. | |
2451 | * Triggered from task_tick_numa(). | |
2452 | */ | |
2453 | void task_numa_work(struct callback_head *work) | |
2454 | { | |
2455 | unsigned long migrate, next_scan, now = jiffies; | |
2456 | struct task_struct *p = current; | |
2457 | struct mm_struct *mm = p->mm; | |
51170840 | 2458 | u64 runtime = p->se.sum_exec_runtime; |
6e5fb223 | 2459 | struct vm_area_struct *vma; |
9f40604c | 2460 | unsigned long start, end; |
598f0ec0 | 2461 | unsigned long nr_pte_updates = 0; |
4620f8c1 | 2462 | long pages, virtpages; |
cbee9f88 | 2463 | |
9148a3a1 | 2464 | SCHED_WARN_ON(p != container_of(work, struct task_struct, numa_work)); |
cbee9f88 PZ |
2465 | |
2466 | work->next = work; /* protect against double add */ | |
2467 | /* | |
2468 | * Who cares about NUMA placement when they're dying. | |
2469 | * | |
2470 | * NOTE: make sure not to dereference p->mm before this check, | |
2471 | * exit_task_work() happens _after_ exit_mm() so we could be called | |
2472 | * without p->mm even though we still had it when we enqueued this | |
2473 | * work. | |
2474 | */ | |
2475 | if (p->flags & PF_EXITING) | |
2476 | return; | |
2477 | ||
930aa174 | 2478 | if (!mm->numa_next_scan) { |
7e8d16b6 MG |
2479 | mm->numa_next_scan = now + |
2480 | msecs_to_jiffies(sysctl_numa_balancing_scan_delay); | |
b8593bfd MG |
2481 | } |
2482 | ||
cbee9f88 PZ |
2483 | /* |
2484 | * Enforce maximal scan/migration frequency.. | |
2485 | */ | |
2486 | migrate = mm->numa_next_scan; | |
2487 | if (time_before(now, migrate)) | |
2488 | return; | |
2489 | ||
598f0ec0 MG |
2490 | if (p->numa_scan_period == 0) { |
2491 | p->numa_scan_period_max = task_scan_max(p); | |
b5dd77c8 | 2492 | p->numa_scan_period = task_scan_start(p); |
598f0ec0 | 2493 | } |
cbee9f88 | 2494 | |
fb003b80 | 2495 | next_scan = now + msecs_to_jiffies(p->numa_scan_period); |
cbee9f88 PZ |
2496 | if (cmpxchg(&mm->numa_next_scan, migrate, next_scan) != migrate) |
2497 | return; | |
2498 | ||
19a78d11 PZ |
2499 | /* |
2500 | * Delay this task enough that another task of this mm will likely win | |
2501 | * the next time around. | |
2502 | */ | |
2503 | p->node_stamp += 2 * TICK_NSEC; | |
2504 | ||
9f40604c MG |
2505 | start = mm->numa_scan_offset; |
2506 | pages = sysctl_numa_balancing_scan_size; | |
2507 | pages <<= 20 - PAGE_SHIFT; /* MB in pages */ | |
4620f8c1 | 2508 | virtpages = pages * 8; /* Scan up to this much virtual space */ |
9f40604c MG |
2509 | if (!pages) |
2510 | return; | |
cbee9f88 | 2511 | |
4620f8c1 | 2512 | |
8655d549 VB |
2513 | if (!down_read_trylock(&mm->mmap_sem)) |
2514 | return; | |
9f40604c | 2515 | vma = find_vma(mm, start); |
6e5fb223 PZ |
2516 | if (!vma) { |
2517 | reset_ptenuma_scan(p); | |
9f40604c | 2518 | start = 0; |
6e5fb223 PZ |
2519 | vma = mm->mmap; |
2520 | } | |
9f40604c | 2521 | for (; vma; vma = vma->vm_next) { |
6b79c57b | 2522 | if (!vma_migratable(vma) || !vma_policy_mof(vma) || |
8e76d4ee | 2523 | is_vm_hugetlb_page(vma) || (vma->vm_flags & VM_MIXEDMAP)) { |
6e5fb223 | 2524 | continue; |
6b79c57b | 2525 | } |
6e5fb223 | 2526 | |
4591ce4f MG |
2527 | /* |
2528 | * Shared library pages mapped by multiple processes are not | |
2529 | * migrated as it is expected they are cache replicated. Avoid | |
2530 | * hinting faults in read-only file-backed mappings or the vdso | |
2531 | * as migrating the pages will be of marginal benefit. | |
2532 | */ | |
2533 | if (!vma->vm_mm || | |
2534 | (vma->vm_file && (vma->vm_flags & (VM_READ|VM_WRITE)) == (VM_READ))) | |
2535 | continue; | |
2536 | ||
3c67f474 MG |
2537 | /* |
2538 | * Skip inaccessible VMAs to avoid any confusion between | |
2539 | * PROT_NONE and NUMA hinting ptes | |
2540 | */ | |
2541 | if (!(vma->vm_flags & (VM_READ | VM_EXEC | VM_WRITE))) | |
2542 | continue; | |
4591ce4f | 2543 | |
9f40604c MG |
2544 | do { |
2545 | start = max(start, vma->vm_start); | |
2546 | end = ALIGN(start + (pages << PAGE_SHIFT), HPAGE_SIZE); | |
2547 | end = min(end, vma->vm_end); | |
4620f8c1 | 2548 | nr_pte_updates = change_prot_numa(vma, start, end); |
598f0ec0 MG |
2549 | |
2550 | /* | |
4620f8c1 RR |
2551 | * Try to scan sysctl_numa_balancing_size worth of |
2552 | * hpages that have at least one present PTE that | |
2553 | * is not already pte-numa. If the VMA contains | |
2554 | * areas that are unused or already full of prot_numa | |
2555 | * PTEs, scan up to virtpages, to skip through those | |
2556 | * areas faster. | |
598f0ec0 MG |
2557 | */ |
2558 | if (nr_pte_updates) | |
2559 | pages -= (end - start) >> PAGE_SHIFT; | |
4620f8c1 | 2560 | virtpages -= (end - start) >> PAGE_SHIFT; |
6e5fb223 | 2561 | |
9f40604c | 2562 | start = end; |
4620f8c1 | 2563 | if (pages <= 0 || virtpages <= 0) |
9f40604c | 2564 | goto out; |
3cf1962c RR |
2565 | |
2566 | cond_resched(); | |
9f40604c | 2567 | } while (end != vma->vm_end); |
cbee9f88 | 2568 | } |
6e5fb223 | 2569 | |
9f40604c | 2570 | out: |
6e5fb223 | 2571 | /* |
c69307d5 PZ |
2572 | * It is possible to reach the end of the VMA list but the last few |
2573 | * VMAs are not guaranteed to the vma_migratable. If they are not, we | |
2574 | * would find the !migratable VMA on the next scan but not reset the | |
2575 | * scanner to the start so check it now. | |
6e5fb223 PZ |
2576 | */ |
2577 | if (vma) | |
9f40604c | 2578 | mm->numa_scan_offset = start; |
6e5fb223 PZ |
2579 | else |
2580 | reset_ptenuma_scan(p); | |
2581 | up_read(&mm->mmap_sem); | |
51170840 RR |
2582 | |
2583 | /* | |
2584 | * Make sure tasks use at least 32x as much time to run other code | |
2585 | * than they used here, to limit NUMA PTE scanning overhead to 3% max. | |
2586 | * Usually update_task_scan_period slows down scanning enough; on an | |
2587 | * overloaded system we need to limit overhead on a per task basis. | |
2588 | */ | |
2589 | if (unlikely(p->se.sum_exec_runtime != runtime)) { | |
2590 | u64 diff = p->se.sum_exec_runtime - runtime; | |
2591 | p->node_stamp += 32 * diff; | |
2592 | } | |
cbee9f88 PZ |
2593 | } |
2594 | ||
2595 | /* | |
2596 | * Drive the periodic memory faults.. | |
2597 | */ | |
2598 | void task_tick_numa(struct rq *rq, struct task_struct *curr) | |
2599 | { | |
2600 | struct callback_head *work = &curr->numa_work; | |
2601 | u64 period, now; | |
2602 | ||
2603 | /* | |
2604 | * We don't care about NUMA placement if we don't have memory. | |
2605 | */ | |
2606 | if (!curr->mm || (curr->flags & PF_EXITING) || work->next != work) | |
2607 | return; | |
2608 | ||
2609 | /* | |
2610 | * Using runtime rather than walltime has the dual advantage that | |
2611 | * we (mostly) drive the selection from busy threads and that the | |
2612 | * task needs to have done some actual work before we bother with | |
2613 | * NUMA placement. | |
2614 | */ | |
2615 | now = curr->se.sum_exec_runtime; | |
2616 | period = (u64)curr->numa_scan_period * NSEC_PER_MSEC; | |
2617 | ||
25b3e5a3 | 2618 | if (now > curr->node_stamp + period) { |
4b96a29b | 2619 | if (!curr->node_stamp) |
b5dd77c8 | 2620 | curr->numa_scan_period = task_scan_start(curr); |
19a78d11 | 2621 | curr->node_stamp += period; |
cbee9f88 PZ |
2622 | |
2623 | if (!time_before(jiffies, curr->mm->numa_next_scan)) { | |
2624 | init_task_work(work, task_numa_work); /* TODO: move this into sched_fork() */ | |
2625 | task_work_add(curr, work, true); | |
2626 | } | |
2627 | } | |
2628 | } | |
3fed382b | 2629 | |
3f9672ba SD |
2630 | static void update_scan_period(struct task_struct *p, int new_cpu) |
2631 | { | |
2632 | int src_nid = cpu_to_node(task_cpu(p)); | |
2633 | int dst_nid = cpu_to_node(new_cpu); | |
2634 | ||
05cbdf4f MG |
2635 | if (!static_branch_likely(&sched_numa_balancing)) |
2636 | return; | |
2637 | ||
3f9672ba SD |
2638 | if (!p->mm || !p->numa_faults || (p->flags & PF_EXITING)) |
2639 | return; | |
2640 | ||
05cbdf4f MG |
2641 | if (src_nid == dst_nid) |
2642 | return; | |
2643 | ||
2644 | /* | |
2645 | * Allow resets if faults have been trapped before one scan | |
2646 | * has completed. This is most likely due to a new task that | |
2647 | * is pulled cross-node due to wakeups or load balancing. | |
2648 | */ | |
2649 | if (p->numa_scan_seq) { | |
2650 | /* | |
2651 | * Avoid scan adjustments if moving to the preferred | |
2652 | * node or if the task was not previously running on | |
2653 | * the preferred node. | |
2654 | */ | |
2655 | if (dst_nid == p->numa_preferred_nid || | |
2656 | (p->numa_preferred_nid != -1 && src_nid != p->numa_preferred_nid)) | |
2657 | return; | |
2658 | } | |
2659 | ||
2660 | p->numa_scan_period = task_scan_start(p); | |
3f9672ba SD |
2661 | } |
2662 | ||
cbee9f88 PZ |
2663 | #else |
2664 | static void task_tick_numa(struct rq *rq, struct task_struct *curr) | |
2665 | { | |
2666 | } | |
0ec8aa00 PZ |
2667 | |
2668 | static inline void account_numa_enqueue(struct rq *rq, struct task_struct *p) | |
2669 | { | |
2670 | } | |
2671 | ||
2672 | static inline void account_numa_dequeue(struct rq *rq, struct task_struct *p) | |
2673 | { | |
2674 | } | |
3fed382b | 2675 | |
3f9672ba SD |
2676 | static inline void update_scan_period(struct task_struct *p, int new_cpu) |
2677 | { | |
2678 | } | |
2679 | ||
cbee9f88 PZ |
2680 | #endif /* CONFIG_NUMA_BALANCING */ |
2681 | ||
30cfdcfc DA |
2682 | static void |
2683 | account_entity_enqueue(struct cfs_rq *cfs_rq, struct sched_entity *se) | |
2684 | { | |
2685 | update_load_add(&cfs_rq->load, se->load.weight); | |
c09595f6 | 2686 | if (!parent_entity(se)) |
029632fb | 2687 | update_load_add(&rq_of(cfs_rq)->load, se->load.weight); |
367456c7 | 2688 | #ifdef CONFIG_SMP |
0ec8aa00 PZ |
2689 | if (entity_is_task(se)) { |
2690 | struct rq *rq = rq_of(cfs_rq); | |
2691 | ||
2692 | account_numa_enqueue(rq, task_of(se)); | |
2693 | list_add(&se->group_node, &rq->cfs_tasks); | |
2694 | } | |
367456c7 | 2695 | #endif |
30cfdcfc | 2696 | cfs_rq->nr_running++; |
30cfdcfc DA |
2697 | } |
2698 | ||
2699 | static void | |
2700 | account_entity_dequeue(struct cfs_rq *cfs_rq, struct sched_entity *se) | |
2701 | { | |
2702 | update_load_sub(&cfs_rq->load, se->load.weight); | |
c09595f6 | 2703 | if (!parent_entity(se)) |
029632fb | 2704 | update_load_sub(&rq_of(cfs_rq)->load, se->load.weight); |
bfdb198c | 2705 | #ifdef CONFIG_SMP |
0ec8aa00 PZ |
2706 | if (entity_is_task(se)) { |
2707 | account_numa_dequeue(rq_of(cfs_rq), task_of(se)); | |
b87f1724 | 2708 | list_del_init(&se->group_node); |
0ec8aa00 | 2709 | } |
bfdb198c | 2710 | #endif |
30cfdcfc | 2711 | cfs_rq->nr_running--; |
30cfdcfc DA |
2712 | } |
2713 | ||
8d5b9025 PZ |
2714 | /* |
2715 | * Signed add and clamp on underflow. | |
2716 | * | |
2717 | * Explicitly do a load-store to ensure the intermediate value never hits | |
2718 | * memory. This allows lockless observations without ever seeing the negative | |
2719 | * values. | |
2720 | */ | |
2721 | #define add_positive(_ptr, _val) do { \ | |
2722 | typeof(_ptr) ptr = (_ptr); \ | |
2723 | typeof(_val) val = (_val); \ | |
2724 | typeof(*ptr) res, var = READ_ONCE(*ptr); \ | |
2725 | \ | |
2726 | res = var + val; \ | |
2727 | \ | |
2728 | if (val < 0 && res > var) \ | |
2729 | res = 0; \ | |
2730 | \ | |
2731 | WRITE_ONCE(*ptr, res); \ | |
2732 | } while (0) | |
2733 | ||
2734 | /* | |
2735 | * Unsigned subtract and clamp on underflow. | |
2736 | * | |
2737 | * Explicitly do a load-store to ensure the intermediate value never hits | |
2738 | * memory. This allows lockless observations without ever seeing the negative | |
2739 | * values. | |
2740 | */ | |
2741 | #define sub_positive(_ptr, _val) do { \ | |
2742 | typeof(_ptr) ptr = (_ptr); \ | |
2743 | typeof(*ptr) val = (_val); \ | |
2744 | typeof(*ptr) res, var = READ_ONCE(*ptr); \ | |
2745 | res = var - val; \ | |
2746 | if (res > var) \ | |
2747 | res = 0; \ | |
2748 | WRITE_ONCE(*ptr, res); \ | |
2749 | } while (0) | |
2750 | ||
b5c0ce7b PB |
2751 | /* |
2752 | * Remove and clamp on negative, from a local variable. | |
2753 | * | |
2754 | * A variant of sub_positive(), which does not use explicit load-store | |
2755 | * and is thus optimized for local variable updates. | |
2756 | */ | |
2757 | #define lsub_positive(_ptr, _val) do { \ | |
2758 | typeof(_ptr) ptr = (_ptr); \ | |
2759 | *ptr -= min_t(typeof(*ptr), *ptr, _val); \ | |
2760 | } while (0) | |
2761 | ||
8d5b9025 | 2762 | #ifdef CONFIG_SMP |
8d5b9025 PZ |
2763 | static inline void |
2764 | enqueue_runnable_load_avg(struct cfs_rq *cfs_rq, struct sched_entity *se) | |
2765 | { | |
1ea6c46a PZ |
2766 | cfs_rq->runnable_weight += se->runnable_weight; |
2767 | ||
2768 | cfs_rq->avg.runnable_load_avg += se->avg.runnable_load_avg; | |
2769 | cfs_rq->avg.runnable_load_sum += se_runnable(se) * se->avg.runnable_load_sum; | |
8d5b9025 PZ |
2770 | } |
2771 | ||
2772 | static inline void | |
2773 | dequeue_runnable_load_avg(struct cfs_rq *cfs_rq, struct sched_entity *se) | |
2774 | { | |
1ea6c46a PZ |
2775 | cfs_rq->runnable_weight -= se->runnable_weight; |
2776 | ||
2777 | sub_positive(&cfs_rq->avg.runnable_load_avg, se->avg.runnable_load_avg); | |
2778 | sub_positive(&cfs_rq->avg.runnable_load_sum, | |
2779 | se_runnable(se) * se->avg.runnable_load_sum); | |
8d5b9025 PZ |
2780 | } |
2781 | ||
2782 | static inline void | |
2783 | enqueue_load_avg(struct cfs_rq *cfs_rq, struct sched_entity *se) | |
2784 | { | |
2785 | cfs_rq->avg.load_avg += se->avg.load_avg; | |
2786 | cfs_rq->avg.load_sum += se_weight(se) * se->avg.load_sum; | |
2787 | } | |
2788 | ||
2789 | static inline void | |
2790 | dequeue_load_avg(struct cfs_rq *cfs_rq, struct sched_entity *se) | |
2791 | { | |
2792 | sub_positive(&cfs_rq->avg.load_avg, se->avg.load_avg); | |
2793 | sub_positive(&cfs_rq->avg.load_sum, se_weight(se) * se->avg.load_sum); | |
2794 | } | |
2795 | #else | |
2796 | static inline void | |
2797 | enqueue_runnable_load_avg(struct cfs_rq *cfs_rq, struct sched_entity *se) { } | |
2798 | static inline void | |
2799 | dequeue_runnable_load_avg(struct cfs_rq *cfs_rq, struct sched_entity *se) { } | |
2800 | static inline void | |
2801 | enqueue_load_avg(struct cfs_rq *cfs_rq, struct sched_entity *se) { } | |
2802 | static inline void | |
2803 | dequeue_load_avg(struct cfs_rq *cfs_rq, struct sched_entity *se) { } | |
2804 | #endif | |
2805 | ||
9059393e | 2806 | static void reweight_entity(struct cfs_rq *cfs_rq, struct sched_entity *se, |
1ea6c46a | 2807 | unsigned long weight, unsigned long runnable) |
9059393e VG |
2808 | { |
2809 | if (se->on_rq) { | |
2810 | /* commit outstanding execution time */ | |
2811 | if (cfs_rq->curr == se) | |
2812 | update_curr(cfs_rq); | |
2813 | account_entity_dequeue(cfs_rq, se); | |
2814 | dequeue_runnable_load_avg(cfs_rq, se); | |
2815 | } | |
2816 | dequeue_load_avg(cfs_rq, se); | |
2817 | ||
1ea6c46a | 2818 | se->runnable_weight = runnable; |
9059393e VG |
2819 | update_load_set(&se->load, weight); |
2820 | ||
2821 | #ifdef CONFIG_SMP | |
1ea6c46a PZ |
2822 | do { |
2823 | u32 divider = LOAD_AVG_MAX - 1024 + se->avg.period_contrib; | |
2824 | ||
2825 | se->avg.load_avg = div_u64(se_weight(se) * se->avg.load_sum, divider); | |
2826 | se->avg.runnable_load_avg = | |
2827 | div_u64(se_runnable(se) * se->avg.runnable_load_sum, divider); | |
2828 | } while (0); | |
9059393e VG |
2829 | #endif |
2830 | ||
2831 | enqueue_load_avg(cfs_rq, se); | |
2832 | if (se->on_rq) { | |
2833 | account_entity_enqueue(cfs_rq, se); | |
2834 | enqueue_runnable_load_avg(cfs_rq, se); | |
2835 | } | |
2836 | } | |
2837 | ||
2838 | void reweight_task(struct task_struct *p, int prio) | |
2839 | { | |
2840 | struct sched_entity *se = &p->se; | |
2841 | struct cfs_rq *cfs_rq = cfs_rq_of(se); | |
2842 | struct load_weight *load = &se->load; | |
2843 | unsigned long weight = scale_load(sched_prio_to_weight[prio]); | |
2844 | ||
1ea6c46a | 2845 | reweight_entity(cfs_rq, se, weight, weight); |
9059393e VG |
2846 | load->inv_weight = sched_prio_to_wmult[prio]; |
2847 | } | |
2848 | ||
3ff6dcac | 2849 | #ifdef CONFIG_FAIR_GROUP_SCHED |
387f77cc | 2850 | #ifdef CONFIG_SMP |
cef27403 PZ |
2851 | /* |
2852 | * All this does is approximate the hierarchical proportion which includes that | |
2853 | * global sum we all love to hate. | |
2854 | * | |
2855 | * That is, the weight of a group entity, is the proportional share of the | |
2856 | * group weight based on the group runqueue weights. That is: | |
2857 | * | |
2858 | * tg->weight * grq->load.weight | |
2859 | * ge->load.weight = ----------------------------- (1) | |
2860 | * \Sum grq->load.weight | |
2861 | * | |
2862 | * Now, because computing that sum is prohibitively expensive to compute (been | |
2863 | * there, done that) we approximate it with this average stuff. The average | |
2864 | * moves slower and therefore the approximation is cheaper and more stable. | |
2865 | * | |
2866 | * So instead of the above, we substitute: | |
2867 | * | |
2868 | * grq->load.weight -> grq->avg.load_avg (2) | |
2869 | * | |
2870 | * which yields the following: | |
2871 | * | |
2872 | * tg->weight * grq->avg.load_avg | |
2873 | * ge->load.weight = ------------------------------ (3) | |
2874 | * tg->load_avg | |
2875 | * | |
2876 | * Where: tg->load_avg ~= \Sum grq->avg.load_avg | |
2877 | * | |
2878 | * That is shares_avg, and it is right (given the approximation (2)). | |
2879 | * | |
2880 | * The problem with it is that because the average is slow -- it was designed | |
2881 | * to be exactly that of course -- this leads to transients in boundary | |
2882 | * conditions. In specific, the case where the group was idle and we start the | |
2883 | * one task. It takes time for our CPU's grq->avg.load_avg to build up, | |
2884 | * yielding bad latency etc.. | |
2885 | * | |
2886 | * Now, in that special case (1) reduces to: | |
2887 | * | |
2888 | * tg->weight * grq->load.weight | |
17de4ee0 | 2889 | * ge->load.weight = ----------------------------- = tg->weight (4) |
cef27403 PZ |
2890 | * grp->load.weight |
2891 | * | |
2892 | * That is, the sum collapses because all other CPUs are idle; the UP scenario. | |
2893 | * | |
2894 | * So what we do is modify our approximation (3) to approach (4) in the (near) | |
2895 | * UP case, like: | |
2896 | * | |
2897 | * ge->load.weight = | |
2898 | * | |
2899 | * tg->weight * grq->load.weight | |
2900 | * --------------------------------------------------- (5) | |
2901 | * tg->load_avg - grq->avg.load_avg + grq->load.weight | |
2902 | * | |
17de4ee0 PZ |
2903 | * But because grq->load.weight can drop to 0, resulting in a divide by zero, |
2904 | * we need to use grq->avg.load_avg as its lower bound, which then gives: | |
2905 | * | |
2906 | * | |
2907 | * tg->weight * grq->load.weight | |
2908 | * ge->load.weight = ----------------------------- (6) | |
2909 | * tg_load_avg' | |
2910 | * | |
2911 | * Where: | |
2912 | * | |
2913 | * tg_load_avg' = tg->load_avg - grq->avg.load_avg + | |
2914 | * max(grq->load.weight, grq->avg.load_avg) | |
cef27403 PZ |
2915 | * |
2916 | * And that is shares_weight and is icky. In the (near) UP case it approaches | |
2917 | * (4) while in the normal case it approaches (3). It consistently | |
2918 | * overestimates the ge->load.weight and therefore: | |
2919 | * | |
2920 | * \Sum ge->load.weight >= tg->weight | |
2921 | * | |
2922 | * hence icky! | |
2923 | */ | |
2c8e4dce | 2924 | static long calc_group_shares(struct cfs_rq *cfs_rq) |
cf5f0acf | 2925 | { |
7c80cfc9 PZ |
2926 | long tg_weight, tg_shares, load, shares; |
2927 | struct task_group *tg = cfs_rq->tg; | |
2928 | ||
2929 | tg_shares = READ_ONCE(tg->shares); | |
cf5f0acf | 2930 | |
3d4b60d3 | 2931 | load = max(scale_load_down(cfs_rq->load.weight), cfs_rq->avg.load_avg); |
cf5f0acf | 2932 | |
ea1dc6fc | 2933 | tg_weight = atomic_long_read(&tg->load_avg); |
3ff6dcac | 2934 | |
ea1dc6fc PZ |
2935 | /* Ensure tg_weight >= load */ |
2936 | tg_weight -= cfs_rq->tg_load_avg_contrib; | |
2937 | tg_weight += load; | |
3ff6dcac | 2938 | |
7c80cfc9 | 2939 | shares = (tg_shares * load); |
cf5f0acf PZ |
2940 | if (tg_weight) |
2941 | shares /= tg_weight; | |
3ff6dcac | 2942 | |
b8fd8423 DE |
2943 | /* |
2944 | * MIN_SHARES has to be unscaled here to support per-CPU partitioning | |
2945 | * of a group with small tg->shares value. It is a floor value which is | |
2946 | * assigned as a minimum load.weight to the sched_entity representing | |
2947 | * the group on a CPU. | |
2948 | * | |
2949 | * E.g. on 64-bit for a group with tg->shares of scale_load(15)=15*1024 | |
2950 | * on an 8-core system with 8 tasks each runnable on one CPU shares has | |
2951 | * to be 15*1024*1/8=1920 instead of scale_load(MIN_SHARES)=2*1024. In | |
2952 | * case no task is runnable on a CPU MIN_SHARES=2 should be returned | |
2953 | * instead of 0. | |
2954 | */ | |
7c80cfc9 | 2955 | return clamp_t(long, shares, MIN_SHARES, tg_shares); |
3ff6dcac | 2956 | } |
2c8e4dce JB |
2957 | |
2958 | /* | |
17de4ee0 PZ |
2959 | * This calculates the effective runnable weight for a group entity based on |
2960 | * the group entity weight calculated above. | |
2961 | * | |
2962 | * Because of the above approximation (2), our group entity weight is | |
2963 | * an load_avg based ratio (3). This means that it includes blocked load and | |
2964 | * does not represent the runnable weight. | |
2965 | * | |
2966 | * Approximate the group entity's runnable weight per ratio from the group | |
2967 | * runqueue: | |
2968 | * | |
2969 | * grq->avg.runnable_load_avg | |
2970 | * ge->runnable_weight = ge->load.weight * -------------------------- (7) | |
2971 | * grq->avg.load_avg | |
2972 | * | |
2973 | * However, analogous to above, since the avg numbers are slow, this leads to | |
2974 | * transients in the from-idle case. Instead we use: | |
2975 | * | |
2976 | * ge->runnable_weight = ge->load.weight * | |
2977 | * | |
2978 | * max(grq->avg.runnable_load_avg, grq->runnable_weight) | |
2979 | * ----------------------------------------------------- (8) | |
2980 | * max(grq->avg.load_avg, grq->load.weight) | |
2981 | * | |
2982 | * Where these max() serve both to use the 'instant' values to fix the slow | |
2983 | * from-idle and avoid the /0 on to-idle, similar to (6). | |
2c8e4dce JB |
2984 | */ |
2985 | static long calc_group_runnable(struct cfs_rq *cfs_rq, long shares) | |
2986 | { | |
17de4ee0 PZ |
2987 | long runnable, load_avg; |
2988 | ||
2989 | load_avg = max(cfs_rq->avg.load_avg, | |
2990 | scale_load_down(cfs_rq->load.weight)); | |
2991 | ||
2992 | runnable = max(cfs_rq->avg.runnable_load_avg, | |
2993 | scale_load_down(cfs_rq->runnable_weight)); | |
2c8e4dce JB |
2994 | |
2995 | runnable *= shares; | |
2996 | if (load_avg) | |
2997 | runnable /= load_avg; | |
17de4ee0 | 2998 | |
2c8e4dce JB |
2999 | return clamp_t(long, runnable, MIN_SHARES, shares); |
3000 | } | |
387f77cc | 3001 | #endif /* CONFIG_SMP */ |
ea1dc6fc | 3002 | |
82958366 PT |
3003 | static inline int throttled_hierarchy(struct cfs_rq *cfs_rq); |
3004 | ||
1ea6c46a PZ |
3005 | /* |
3006 | * Recomputes the group entity based on the current state of its group | |
3007 | * runqueue. | |
3008 | */ | |
3009 | static void update_cfs_group(struct sched_entity *se) | |
2069dd75 | 3010 | { |
1ea6c46a PZ |
3011 | struct cfs_rq *gcfs_rq = group_cfs_rq(se); |
3012 | long shares, runnable; | |
2069dd75 | 3013 | |
1ea6c46a | 3014 | if (!gcfs_rq) |
89ee048f VG |
3015 | return; |
3016 | ||
1ea6c46a | 3017 | if (throttled_hierarchy(gcfs_rq)) |
2069dd75 | 3018 | return; |
89ee048f | 3019 | |
3ff6dcac | 3020 | #ifndef CONFIG_SMP |
1ea6c46a | 3021 | runnable = shares = READ_ONCE(gcfs_rq->tg->shares); |
7c80cfc9 PZ |
3022 | |
3023 | if (likely(se->load.weight == shares)) | |
3ff6dcac | 3024 | return; |
7c80cfc9 | 3025 | #else |
2c8e4dce JB |
3026 | shares = calc_group_shares(gcfs_rq); |
3027 | runnable = calc_group_runnable(gcfs_rq, shares); | |
3ff6dcac | 3028 | #endif |
2069dd75 | 3029 | |
1ea6c46a | 3030 | reweight_entity(cfs_rq_of(se), se, shares, runnable); |
2069dd75 | 3031 | } |
89ee048f | 3032 | |
2069dd75 | 3033 | #else /* CONFIG_FAIR_GROUP_SCHED */ |
1ea6c46a | 3034 | static inline void update_cfs_group(struct sched_entity *se) |
2069dd75 PZ |
3035 | { |
3036 | } | |
3037 | #endif /* CONFIG_FAIR_GROUP_SCHED */ | |
3038 | ||
ea14b57e | 3039 | static inline void cfs_rq_util_change(struct cfs_rq *cfs_rq, int flags) |
a030d738 | 3040 | { |
43964409 LT |
3041 | struct rq *rq = rq_of(cfs_rq); |
3042 | ||
ea14b57e | 3043 | if (&rq->cfs == cfs_rq || (flags & SCHED_CPUFREQ_MIGRATION)) { |
a030d738 VK |
3044 | /* |
3045 | * There are a few boundary cases this might miss but it should | |
3046 | * get called often enough that that should (hopefully) not be | |
9783be2c | 3047 | * a real problem. |
a030d738 VK |
3048 | * |
3049 | * It will not get called when we go idle, because the idle | |
3050 | * thread is a different class (!fair), nor will the utilization | |
3051 | * number include things like RT tasks. | |
3052 | * | |
3053 | * As is, the util number is not freq-invariant (we'd have to | |
3054 | * implement arch_scale_freq_capacity() for that). | |
3055 | * | |
3056 | * See cpu_util(). | |
3057 | */ | |
ea14b57e | 3058 | cpufreq_update_util(rq, flags); |
a030d738 VK |
3059 | } |
3060 | } | |
3061 | ||
141965c7 | 3062 | #ifdef CONFIG_SMP |
c566e8e9 | 3063 | #ifdef CONFIG_FAIR_GROUP_SCHED |
7c3edd2c PZ |
3064 | /** |
3065 | * update_tg_load_avg - update the tg's load avg | |
3066 | * @cfs_rq: the cfs_rq whose avg changed | |
3067 | * @force: update regardless of how small the difference | |
3068 | * | |
3069 | * This function 'ensures': tg->load_avg := \Sum tg->cfs_rq[]->avg.load. | |
3070 | * However, because tg->load_avg is a global value there are performance | |
3071 | * considerations. | |
3072 | * | |
3073 | * In order to avoid having to look at the other cfs_rq's, we use a | |
3074 | * differential update where we store the last value we propagated. This in | |
3075 | * turn allows skipping updates if the differential is 'small'. | |
3076 | * | |
815abf5a | 3077 | * Updating tg's load_avg is necessary before update_cfs_share(). |
bb17f655 | 3078 | */ |
9d89c257 | 3079 | static inline void update_tg_load_avg(struct cfs_rq *cfs_rq, int force) |
bb17f655 | 3080 | { |
9d89c257 | 3081 | long delta = cfs_rq->avg.load_avg - cfs_rq->tg_load_avg_contrib; |
bb17f655 | 3082 | |
aa0b7ae0 WL |
3083 | /* |
3084 | * No need to update load_avg for root_task_group as it is not used. | |
3085 | */ | |
3086 | if (cfs_rq->tg == &root_task_group) | |
3087 | return; | |
3088 | ||
9d89c257 YD |
3089 | if (force || abs(delta) > cfs_rq->tg_load_avg_contrib / 64) { |
3090 | atomic_long_add(delta, &cfs_rq->tg->load_avg); | |
3091 | cfs_rq->tg_load_avg_contrib = cfs_rq->avg.load_avg; | |
bb17f655 | 3092 | } |
8165e145 | 3093 | } |
f5f9739d | 3094 | |
ad936d86 | 3095 | /* |
97fb7a0a | 3096 | * Called within set_task_rq() right before setting a task's CPU. The |
ad936d86 BP |
3097 | * caller only guarantees p->pi_lock is held; no other assumptions, |
3098 | * including the state of rq->lock, should be made. | |
3099 | */ | |
3100 | void set_task_rq_fair(struct sched_entity *se, | |
3101 | struct cfs_rq *prev, struct cfs_rq *next) | |
3102 | { | |
0ccb977f PZ |
3103 | u64 p_last_update_time; |
3104 | u64 n_last_update_time; | |
3105 | ||
ad936d86 BP |
3106 | if (!sched_feat(ATTACH_AGE_LOAD)) |
3107 | return; | |
3108 | ||
3109 | /* | |
3110 | * We are supposed to update the task to "current" time, then its up to | |
3111 | * date and ready to go to new CPU/cfs_rq. But we have difficulty in | |
3112 | * getting what current time is, so simply throw away the out-of-date | |
3113 | * time. This will result in the wakee task is less decayed, but giving | |
3114 | * the wakee more load sounds not bad. | |
3115 | */ | |
0ccb977f PZ |
3116 | if (!(se->avg.last_update_time && prev)) |
3117 | return; | |
ad936d86 BP |
3118 | |
3119 | #ifndef CONFIG_64BIT | |
0ccb977f | 3120 | { |
ad936d86 BP |
3121 | u64 p_last_update_time_copy; |
3122 | u64 n_last_update_time_copy; | |
3123 | ||
3124 | do { | |
3125 | p_last_update_time_copy = prev->load_last_update_time_copy; | |
3126 | n_last_update_time_copy = next->load_last_update_time_copy; | |
3127 | ||
3128 | smp_rmb(); | |
3129 | ||
3130 | p_last_update_time = prev->avg.last_update_time; | |
3131 | n_last_update_time = next->avg.last_update_time; | |
3132 | ||
3133 | } while (p_last_update_time != p_last_update_time_copy || | |
3134 | n_last_update_time != n_last_update_time_copy); | |
0ccb977f | 3135 | } |
ad936d86 | 3136 | #else |
0ccb977f PZ |
3137 | p_last_update_time = prev->avg.last_update_time; |
3138 | n_last_update_time = next->avg.last_update_time; | |
ad936d86 | 3139 | #endif |
23127296 | 3140 | __update_load_avg_blocked_se(p_last_update_time, se); |
0ccb977f | 3141 | se->avg.last_update_time = n_last_update_time; |
ad936d86 | 3142 | } |
09a43ace | 3143 | |
0e2d2aaa PZ |
3144 | |
3145 | /* | |
3146 | * When on migration a sched_entity joins/leaves the PELT hierarchy, we need to | |
3147 | * propagate its contribution. The key to this propagation is the invariant | |
3148 | * that for each group: | |
3149 | * | |
3150 | * ge->avg == grq->avg (1) | |
3151 | * | |
3152 | * _IFF_ we look at the pure running and runnable sums. Because they | |
3153 | * represent the very same entity, just at different points in the hierarchy. | |
3154 | * | |
a4c3c049 VG |
3155 | * Per the above update_tg_cfs_util() is trivial and simply copies the running |
3156 | * sum over (but still wrong, because the group entity and group rq do not have | |
3157 | * their PELT windows aligned). | |
0e2d2aaa PZ |
3158 | * |
3159 | * However, update_tg_cfs_runnable() is more complex. So we have: | |
3160 | * | |
3161 | * ge->avg.load_avg = ge->load.weight * ge->avg.runnable_avg (2) | |
3162 | * | |
3163 | * And since, like util, the runnable part should be directly transferable, | |
3164 | * the following would _appear_ to be the straight forward approach: | |
3165 | * | |
a4c3c049 | 3166 | * grq->avg.load_avg = grq->load.weight * grq->avg.runnable_avg (3) |
0e2d2aaa PZ |
3167 | * |
3168 | * And per (1) we have: | |
3169 | * | |
a4c3c049 | 3170 | * ge->avg.runnable_avg == grq->avg.runnable_avg |
0e2d2aaa PZ |
3171 | * |
3172 | * Which gives: | |
3173 | * | |
3174 | * ge->load.weight * grq->avg.load_avg | |
3175 | * ge->avg.load_avg = ----------------------------------- (4) | |
3176 | * grq->load.weight | |
3177 | * | |
3178 | * Except that is wrong! | |
3179 | * | |
3180 | * Because while for entities historical weight is not important and we | |
3181 | * really only care about our future and therefore can consider a pure | |
3182 | * runnable sum, runqueues can NOT do this. | |
3183 | * | |
3184 | * We specifically want runqueues to have a load_avg that includes | |
3185 | * historical weights. Those represent the blocked load, the load we expect | |
3186 | * to (shortly) return to us. This only works by keeping the weights as | |
3187 | * integral part of the sum. We therefore cannot decompose as per (3). | |
3188 | * | |
a4c3c049 VG |
3189 | * Another reason this doesn't work is that runnable isn't a 0-sum entity. |
3190 | * Imagine a rq with 2 tasks that each are runnable 2/3 of the time. Then the | |
3191 | * rq itself is runnable anywhere between 2/3 and 1 depending on how the | |
3192 | * runnable section of these tasks overlap (or not). If they were to perfectly | |
3193 | * align the rq as a whole would be runnable 2/3 of the time. If however we | |
3194 | * always have at least 1 runnable task, the rq as a whole is always runnable. | |
0e2d2aaa | 3195 | * |
a4c3c049 | 3196 | * So we'll have to approximate.. :/ |
0e2d2aaa | 3197 | * |
a4c3c049 | 3198 | * Given the constraint: |
0e2d2aaa | 3199 | * |
a4c3c049 | 3200 | * ge->avg.running_sum <= ge->avg.runnable_sum <= LOAD_AVG_MAX |
0e2d2aaa | 3201 | * |
a4c3c049 VG |
3202 | * We can construct a rule that adds runnable to a rq by assuming minimal |
3203 | * overlap. | |
0e2d2aaa | 3204 | * |
a4c3c049 | 3205 | * On removal, we'll assume each task is equally runnable; which yields: |
0e2d2aaa | 3206 | * |
a4c3c049 | 3207 | * grq->avg.runnable_sum = grq->avg.load_sum / grq->load.weight |
0e2d2aaa | 3208 | * |
a4c3c049 | 3209 | * XXX: only do this for the part of runnable > running ? |
0e2d2aaa | 3210 | * |
0e2d2aaa PZ |
3211 | */ |
3212 | ||
09a43ace | 3213 | static inline void |
0e2d2aaa | 3214 | update_tg_cfs_util(struct cfs_rq *cfs_rq, struct sched_entity *se, struct cfs_rq *gcfs_rq) |
09a43ace | 3215 | { |
09a43ace VG |
3216 | long delta = gcfs_rq->avg.util_avg - se->avg.util_avg; |
3217 | ||
3218 | /* Nothing to update */ | |
3219 | if (!delta) | |
3220 | return; | |
3221 | ||
a4c3c049 VG |
3222 | /* |
3223 | * The relation between sum and avg is: | |
3224 | * | |
3225 | * LOAD_AVG_MAX - 1024 + sa->period_contrib | |
3226 | * | |
3227 | * however, the PELT windows are not aligned between grq and gse. | |
3228 | */ | |
3229 | ||
09a43ace VG |
3230 | /* Set new sched_entity's utilization */ |
3231 | se->avg.util_avg = gcfs_rq->avg.util_avg; | |
3232 | se->avg.util_sum = se->avg.util_avg * LOAD_AVG_MAX; | |
3233 | ||
3234 | /* Update parent cfs_rq utilization */ | |
3235 | add_positive(&cfs_rq->avg.util_avg, delta); | |
3236 | cfs_rq->avg.util_sum = cfs_rq->avg.util_avg * LOAD_AVG_MAX; | |
3237 | } | |
3238 | ||
09a43ace | 3239 | static inline void |
0e2d2aaa | 3240 | update_tg_cfs_runnable(struct cfs_rq *cfs_rq, struct sched_entity *se, struct cfs_rq *gcfs_rq) |
09a43ace | 3241 | { |
a4c3c049 VG |
3242 | long delta_avg, running_sum, runnable_sum = gcfs_rq->prop_runnable_sum; |
3243 | unsigned long runnable_load_avg, load_avg; | |
3244 | u64 runnable_load_sum, load_sum = 0; | |
3245 | s64 delta_sum; | |
09a43ace | 3246 | |
0e2d2aaa PZ |
3247 | if (!runnable_sum) |
3248 | return; | |
09a43ace | 3249 | |
0e2d2aaa | 3250 | gcfs_rq->prop_runnable_sum = 0; |
09a43ace | 3251 | |
a4c3c049 VG |
3252 | if (runnable_sum >= 0) { |
3253 | /* | |
3254 | * Add runnable; clip at LOAD_AVG_MAX. Reflects that until | |
3255 | * the CPU is saturated running == runnable. | |
3256 | */ | |
3257 | runnable_sum += se->avg.load_sum; | |
3258 | runnable_sum = min(runnable_sum, (long)LOAD_AVG_MAX); | |
3259 | } else { | |
3260 | /* | |
3261 | * Estimate the new unweighted runnable_sum of the gcfs_rq by | |
3262 | * assuming all tasks are equally runnable. | |
3263 | */ | |
3264 | if (scale_load_down(gcfs_rq->load.weight)) { | |
3265 | load_sum = div_s64(gcfs_rq->avg.load_sum, | |
3266 | scale_load_down(gcfs_rq->load.weight)); | |
3267 | } | |
3268 | ||
3269 | /* But make sure to not inflate se's runnable */ | |
3270 | runnable_sum = min(se->avg.load_sum, load_sum); | |
3271 | } | |
3272 | ||
3273 | /* | |
3274 | * runnable_sum can't be lower than running_sum | |
23127296 VG |
3275 | * Rescale running sum to be in the same range as runnable sum |
3276 | * running_sum is in [0 : LOAD_AVG_MAX << SCHED_CAPACITY_SHIFT] | |
3277 | * runnable_sum is in [0 : LOAD_AVG_MAX] | |
a4c3c049 | 3278 | */ |
23127296 | 3279 | running_sum = se->avg.util_sum >> SCHED_CAPACITY_SHIFT; |
a4c3c049 VG |
3280 | runnable_sum = max(runnable_sum, running_sum); |
3281 | ||
0e2d2aaa PZ |
3282 | load_sum = (s64)se_weight(se) * runnable_sum; |
3283 | load_avg = div_s64(load_sum, LOAD_AVG_MAX); | |
09a43ace | 3284 | |
a4c3c049 VG |
3285 | delta_sum = load_sum - (s64)se_weight(se) * se->avg.load_sum; |
3286 | delta_avg = load_avg - se->avg.load_avg; | |
09a43ace | 3287 | |
a4c3c049 VG |
3288 | se->avg.load_sum = runnable_sum; |
3289 | se->avg.load_avg = load_avg; | |
3290 | add_positive(&cfs_rq->avg.load_avg, delta_avg); | |
3291 | add_positive(&cfs_rq->avg.load_sum, delta_sum); | |
09a43ace | 3292 | |
1ea6c46a PZ |
3293 | runnable_load_sum = (s64)se_runnable(se) * runnable_sum; |
3294 | runnable_load_avg = div_s64(runnable_load_sum, LOAD_AVG_MAX); | |
a4c3c049 VG |
3295 | delta_sum = runnable_load_sum - se_weight(se) * se->avg.runnable_load_sum; |
3296 | delta_avg = runnable_load_avg - se->avg.runnable_load_avg; | |
1ea6c46a | 3297 | |
a4c3c049 VG |
3298 | se->avg.runnable_load_sum = runnable_sum; |
3299 | se->avg.runnable_load_avg = runnable_load_avg; | |
1ea6c46a | 3300 | |
09a43ace | 3301 | if (se->on_rq) { |
a4c3c049 VG |
3302 | add_positive(&cfs_rq->avg.runnable_load_avg, delta_avg); |
3303 | add_positive(&cfs_rq->avg.runnable_load_sum, delta_sum); | |
09a43ace VG |
3304 | } |
3305 | } | |
3306 | ||
0e2d2aaa | 3307 | static inline void add_tg_cfs_propagate(struct cfs_rq *cfs_rq, long runnable_sum) |
09a43ace | 3308 | { |
0e2d2aaa PZ |
3309 | cfs_rq->propagate = 1; |
3310 | cfs_rq->prop_runnable_sum += runnable_sum; | |
09a43ace VG |
3311 | } |
3312 | ||
3313 | /* Update task and its cfs_rq load average */ | |
3314 | static inline int propagate_entity_load_avg(struct sched_entity *se) | |
3315 | { | |
0e2d2aaa | 3316 | struct cfs_rq *cfs_rq, *gcfs_rq; |
09a43ace VG |
3317 | |
3318 | if (entity_is_task(se)) | |
3319 | return 0; | |
3320 | ||
0e2d2aaa PZ |
3321 | gcfs_rq = group_cfs_rq(se); |
3322 | if (!gcfs_rq->propagate) | |
09a43ace VG |
3323 | return 0; |
3324 | ||
0e2d2aaa PZ |
3325 | gcfs_rq->propagate = 0; |
3326 | ||
09a43ace VG |
3327 | cfs_rq = cfs_rq_of(se); |
3328 | ||
0e2d2aaa | 3329 | add_tg_cfs_propagate(cfs_rq, gcfs_rq->prop_runnable_sum); |
09a43ace | 3330 | |
0e2d2aaa PZ |
3331 | update_tg_cfs_util(cfs_rq, se, gcfs_rq); |
3332 | update_tg_cfs_runnable(cfs_rq, se, gcfs_rq); | |
09a43ace VG |
3333 | |
3334 | return 1; | |
3335 | } | |
3336 | ||
bc427898 VG |
3337 | /* |
3338 | * Check if we need to update the load and the utilization of a blocked | |
3339 | * group_entity: | |
3340 | */ | |
3341 | static inline bool skip_blocked_update(struct sched_entity *se) | |
3342 | { | |
3343 | struct cfs_rq *gcfs_rq = group_cfs_rq(se); | |
3344 | ||
3345 | /* | |
3346 | * If sched_entity still have not zero load or utilization, we have to | |
3347 | * decay it: | |
3348 | */ | |
3349 | if (se->avg.load_avg || se->avg.util_avg) | |
3350 | return false; | |
3351 | ||
3352 | /* | |
3353 | * If there is a pending propagation, we have to update the load and | |
3354 | * the utilization of the sched_entity: | |
3355 | */ | |
0e2d2aaa | 3356 | if (gcfs_rq->propagate) |
bc427898 VG |
3357 | return false; |
3358 | ||
3359 | /* | |
3360 | * Otherwise, the load and the utilization of the sched_entity is | |
3361 | * already zero and there is no pending propagation, so it will be a | |
3362 | * waste of time to try to decay it: | |
3363 | */ | |
3364 | return true; | |
3365 | } | |
3366 | ||
6e83125c | 3367 | #else /* CONFIG_FAIR_GROUP_SCHED */ |
09a43ace | 3368 | |
9d89c257 | 3369 | static inline void update_tg_load_avg(struct cfs_rq *cfs_rq, int force) {} |
09a43ace VG |
3370 | |
3371 | static inline int propagate_entity_load_avg(struct sched_entity *se) | |
3372 | { | |
3373 | return 0; | |
3374 | } | |
3375 | ||
0e2d2aaa | 3376 | static inline void add_tg_cfs_propagate(struct cfs_rq *cfs_rq, long runnable_sum) {} |
09a43ace | 3377 | |
6e83125c | 3378 | #endif /* CONFIG_FAIR_GROUP_SCHED */ |
c566e8e9 | 3379 | |
3d30544f PZ |
3380 | /** |
3381 | * update_cfs_rq_load_avg - update the cfs_rq's load/util averages | |
23127296 | 3382 | * @now: current time, as per cfs_rq_clock_pelt() |
3d30544f | 3383 | * @cfs_rq: cfs_rq to update |
3d30544f PZ |
3384 | * |
3385 | * The cfs_rq avg is the direct sum of all its entities (blocked and runnable) | |
3386 | * avg. The immediate corollary is that all (fair) tasks must be attached, see | |
3387 | * post_init_entity_util_avg(). | |
3388 | * | |
3389 | * cfs_rq->avg is used for task_h_load() and update_cfs_share() for example. | |
3390 | * | |
7c3edd2c PZ |
3391 | * Returns true if the load decayed or we removed load. |
3392 | * | |
3393 | * Since both these conditions indicate a changed cfs_rq->avg.load we should | |
3394 | * call update_tg_load_avg() when this function returns true. | |
3d30544f | 3395 | */ |
a2c6c91f | 3396 | static inline int |
3a123bbb | 3397 | update_cfs_rq_load_avg(u64 now, struct cfs_rq *cfs_rq) |
2dac754e | 3398 | { |
0e2d2aaa | 3399 | unsigned long removed_load = 0, removed_util = 0, removed_runnable_sum = 0; |
9d89c257 | 3400 | struct sched_avg *sa = &cfs_rq->avg; |
2a2f5d4e | 3401 | int decayed = 0; |
2dac754e | 3402 | |
2a2f5d4e PZ |
3403 | if (cfs_rq->removed.nr) { |
3404 | unsigned long r; | |
9a2dd585 | 3405 | u32 divider = LOAD_AVG_MAX - 1024 + sa->period_contrib; |
2a2f5d4e PZ |
3406 | |
3407 | raw_spin_lock(&cfs_rq->removed.lock); | |
3408 | swap(cfs_rq->removed.util_avg, removed_util); | |
3409 | swap(cfs_rq->removed.load_avg, removed_load); | |
0e2d2aaa | 3410 | swap(cfs_rq->removed.runnable_sum, removed_runnable_sum); |
2a2f5d4e PZ |
3411 | cfs_rq->removed.nr = 0; |
3412 | raw_spin_unlock(&cfs_rq->removed.lock); | |
3413 | ||
2a2f5d4e | 3414 | r = removed_load; |
89741892 | 3415 | sub_positive(&sa->load_avg, r); |
9a2dd585 | 3416 | sub_positive(&sa->load_sum, r * divider); |
2dac754e | 3417 | |
2a2f5d4e | 3418 | r = removed_util; |
89741892 | 3419 | sub_positive(&sa->util_avg, r); |
9a2dd585 | 3420 | sub_positive(&sa->util_sum, r * divider); |
2a2f5d4e | 3421 | |
0e2d2aaa | 3422 | add_tg_cfs_propagate(cfs_rq, -(long)removed_runnable_sum); |
2a2f5d4e PZ |
3423 | |
3424 | decayed = 1; | |
9d89c257 | 3425 | } |
36ee28e4 | 3426 | |
23127296 | 3427 | decayed |= __update_load_avg_cfs_rq(now, cfs_rq); |
36ee28e4 | 3428 | |
9d89c257 YD |
3429 | #ifndef CONFIG_64BIT |
3430 | smp_wmb(); | |
3431 | cfs_rq->load_last_update_time_copy = sa->last_update_time; | |
3432 | #endif | |
36ee28e4 | 3433 | |
2a2f5d4e | 3434 | if (decayed) |
ea14b57e | 3435 | cfs_rq_util_change(cfs_rq, 0); |
21e96f88 | 3436 | |
2a2f5d4e | 3437 | return decayed; |
21e96f88 SM |
3438 | } |
3439 | ||
3d30544f PZ |
3440 | /** |
3441 | * attach_entity_load_avg - attach this entity to its cfs_rq load avg | |
3442 | * @cfs_rq: cfs_rq to attach to | |
3443 | * @se: sched_entity to attach | |
882a78a9 | 3444 | * @flags: migration hints |
3d30544f PZ |
3445 | * |
3446 | * Must call update_cfs_rq_load_avg() before this, since we rely on | |
3447 | * cfs_rq->avg.last_update_time being current. | |
3448 | */ | |
ea14b57e | 3449 | static void attach_entity_load_avg(struct cfs_rq *cfs_rq, struct sched_entity *se, int flags) |
a05e8c51 | 3450 | { |
f207934f PZ |
3451 | u32 divider = LOAD_AVG_MAX - 1024 + cfs_rq->avg.period_contrib; |
3452 | ||
3453 | /* | |
3454 | * When we attach the @se to the @cfs_rq, we must align the decay | |
3455 | * window because without that, really weird and wonderful things can | |
3456 | * happen. | |
3457 | * | |
3458 | * XXX illustrate | |
3459 | */ | |
a05e8c51 | 3460 | se->avg.last_update_time = cfs_rq->avg.last_update_time; |
f207934f PZ |
3461 | se->avg.period_contrib = cfs_rq->avg.period_contrib; |
3462 | ||
3463 | /* | |
3464 | * Hell(o) Nasty stuff.. we need to recompute _sum based on the new | |
3465 | * period_contrib. This isn't strictly correct, but since we're | |
3466 | * entirely outside of the PELT hierarchy, nobody cares if we truncate | |
3467 | * _sum a little. | |
3468 | */ | |
3469 | se->avg.util_sum = se->avg.util_avg * divider; | |
3470 | ||
3471 | se->avg.load_sum = divider; | |
3472 | if (se_weight(se)) { | |
3473 | se->avg.load_sum = | |
3474 | div_u64(se->avg.load_avg * se->avg.load_sum, se_weight(se)); | |
3475 | } | |
3476 | ||
3477 | se->avg.runnable_load_sum = se->avg.load_sum; | |
3478 | ||
8d5b9025 | 3479 | enqueue_load_avg(cfs_rq, se); |
a05e8c51 BP |
3480 | cfs_rq->avg.util_avg += se->avg.util_avg; |
3481 | cfs_rq->avg.util_sum += se->avg.util_sum; | |
0e2d2aaa PZ |
3482 | |
3483 | add_tg_cfs_propagate(cfs_rq, se->avg.load_sum); | |
a2c6c91f | 3484 | |
ea14b57e | 3485 | cfs_rq_util_change(cfs_rq, flags); |
a05e8c51 BP |
3486 | } |
3487 | ||
3d30544f PZ |
3488 | /** |
3489 | * detach_entity_load_avg - detach this entity from its cfs_rq load avg | |
3490 | * @cfs_rq: cfs_rq to detach from | |
3491 | * @se: sched_entity to detach | |
3492 | * | |
3493 | * Must call update_cfs_rq_load_avg() before this, since we rely on | |
3494 | * cfs_rq->avg.last_update_time being current. | |
3495 | */ | |
a05e8c51 BP |
3496 | static void detach_entity_load_avg(struct cfs_rq *cfs_rq, struct sched_entity *se) |
3497 | { | |
8d5b9025 | 3498 | dequeue_load_avg(cfs_rq, se); |
89741892 PZ |
3499 | sub_positive(&cfs_rq->avg.util_avg, se->avg.util_avg); |
3500 | sub_positive(&cfs_rq->avg.util_sum, se->avg.util_sum); | |
0e2d2aaa PZ |
3501 | |
3502 | add_tg_cfs_propagate(cfs_rq, -se->avg.load_sum); | |
a2c6c91f | 3503 | |
ea14b57e | 3504 | cfs_rq_util_change(cfs_rq, 0); |
a05e8c51 BP |
3505 | } |
3506 | ||
b382a531 PZ |
3507 | /* |
3508 | * Optional action to be done while updating the load average | |
3509 | */ | |
3510 | #define UPDATE_TG 0x1 | |
3511 | #define SKIP_AGE_LOAD 0x2 | |
3512 | #define DO_ATTACH 0x4 | |
3513 | ||
3514 | /* Update task and its cfs_rq load average */ | |
3515 | static inline void update_load_avg(struct cfs_rq *cfs_rq, struct sched_entity *se, int flags) | |
3516 | { | |
23127296 | 3517 | u64 now = cfs_rq_clock_pelt(cfs_rq); |
b382a531 PZ |
3518 | int decayed; |
3519 | ||
3520 | /* | |
3521 | * Track task load average for carrying it to new CPU after migrated, and | |
3522 | * track group sched_entity load average for task_h_load calc in migration | |
3523 | */ | |
3524 | if (se->avg.last_update_time && !(flags & SKIP_AGE_LOAD)) | |
23127296 | 3525 | __update_load_avg_se(now, cfs_rq, se); |
b382a531 PZ |
3526 | |
3527 | decayed = update_cfs_rq_load_avg(now, cfs_rq); | |
3528 | decayed |= propagate_entity_load_avg(se); | |
3529 | ||
3530 | if (!se->avg.last_update_time && (flags & DO_ATTACH)) { | |
3531 | ||
ea14b57e PZ |
3532 | /* |
3533 | * DO_ATTACH means we're here from enqueue_entity(). | |
3534 | * !last_update_time means we've passed through | |
3535 | * migrate_task_rq_fair() indicating we migrated. | |
3536 | * | |
3537 | * IOW we're enqueueing a task on a new CPU. | |
3538 | */ | |
3539 | attach_entity_load_avg(cfs_rq, se, SCHED_CPUFREQ_MIGRATION); | |
b382a531 PZ |
3540 | update_tg_load_avg(cfs_rq, 0); |
3541 | ||
3542 | } else if (decayed && (flags & UPDATE_TG)) | |
3543 | update_tg_load_avg(cfs_rq, 0); | |
3544 | } | |
3545 | ||
9d89c257 | 3546 | #ifndef CONFIG_64BIT |
0905f04e YD |
3547 | static inline u64 cfs_rq_last_update_time(struct cfs_rq *cfs_rq) |
3548 | { | |
9d89c257 | 3549 | u64 last_update_time_copy; |
0905f04e | 3550 | u64 last_update_time; |
9ee474f5 | 3551 | |
9d89c257 YD |
3552 | do { |
3553 | last_update_time_copy = cfs_rq->load_last_update_time_copy; | |
3554 | smp_rmb(); | |
3555 | last_update_time = cfs_rq->avg.last_update_time; | |
3556 | } while (last_update_time != last_update_time_copy); | |
0905f04e YD |
3557 | |
3558 | return last_update_time; | |
3559 | } | |
9d89c257 | 3560 | #else |
0905f04e YD |
3561 | static inline u64 cfs_rq_last_update_time(struct cfs_rq *cfs_rq) |
3562 | { | |
3563 | return cfs_rq->avg.last_update_time; | |
3564 | } | |
9d89c257 YD |
3565 | #endif |
3566 | ||
104cb16d MR |
3567 | /* |
3568 | * Synchronize entity load avg of dequeued entity without locking | |
3569 | * the previous rq. | |
3570 | */ | |
3571 | void sync_entity_load_avg(struct sched_entity *se) | |
3572 | { | |
3573 | struct cfs_rq *cfs_rq = cfs_rq_of(se); | |
3574 | u64 last_update_time; | |
3575 | ||
3576 | last_update_time = cfs_rq_last_update_time(cfs_rq); | |
23127296 | 3577 | __update_load_avg_blocked_se(last_update_time, se); |
104cb16d MR |
3578 | } |
3579 | ||
0905f04e YD |
3580 | /* |
3581 | * Task first catches up with cfs_rq, and then subtract | |
3582 | * itself from the cfs_rq (task must be off the queue now). | |
3583 | */ | |
3584 | void remove_entity_load_avg(struct sched_entity *se) | |
3585 | { | |
3586 | struct cfs_rq *cfs_rq = cfs_rq_of(se); | |
2a2f5d4e | 3587 | unsigned long flags; |
0905f04e YD |
3588 | |
3589 | /* | |
7dc603c9 PZ |
3590 | * tasks cannot exit without having gone through wake_up_new_task() -> |
3591 | * post_init_entity_util_avg() which will have added things to the | |
3592 | * cfs_rq, so we can remove unconditionally. | |
3593 | * | |
3594 | * Similarly for groups, they will have passed through | |
3595 | * post_init_entity_util_avg() before unregister_sched_fair_group() | |
3596 | * calls this. | |
0905f04e | 3597 | */ |
0905f04e | 3598 | |
104cb16d | 3599 | sync_entity_load_avg(se); |
2a2f5d4e PZ |
3600 | |
3601 | raw_spin_lock_irqsave(&cfs_rq->removed.lock, flags); | |
3602 | ++cfs_rq->removed.nr; | |
3603 | cfs_rq->removed.util_avg += se->avg.util_avg; | |
3604 | cfs_rq->removed.load_avg += se->avg.load_avg; | |
0e2d2aaa | 3605 | cfs_rq->removed.runnable_sum += se->avg.load_sum; /* == runnable_sum */ |
2a2f5d4e | 3606 | raw_spin_unlock_irqrestore(&cfs_rq->removed.lock, flags); |
2dac754e | 3607 | } |
642dbc39 | 3608 | |
7ea241af YD |
3609 | static inline unsigned long cfs_rq_runnable_load_avg(struct cfs_rq *cfs_rq) |
3610 | { | |
1ea6c46a | 3611 | return cfs_rq->avg.runnable_load_avg; |
7ea241af YD |
3612 | } |
3613 | ||
3614 | static inline unsigned long cfs_rq_load_avg(struct cfs_rq *cfs_rq) | |
3615 | { | |
3616 | return cfs_rq->avg.load_avg; | |
3617 | } | |
3618 | ||
46f69fa3 | 3619 | static int idle_balance(struct rq *this_rq, struct rq_flags *rf); |
6e83125c | 3620 | |
7f65ea42 PB |
3621 | static inline unsigned long task_util(struct task_struct *p) |
3622 | { | |
3623 | return READ_ONCE(p->se.avg.util_avg); | |
3624 | } | |
3625 | ||
3626 | static inline unsigned long _task_util_est(struct task_struct *p) | |
3627 | { | |
3628 | struct util_est ue = READ_ONCE(p->se.avg.util_est); | |
3629 | ||
92a801e5 | 3630 | return (max(ue.ewma, ue.enqueued) | UTIL_AVG_UNCHANGED); |
7f65ea42 PB |
3631 | } |
3632 | ||
3633 | static inline unsigned long task_util_est(struct task_struct *p) | |
3634 | { | |
3635 | return max(task_util(p), _task_util_est(p)); | |
3636 | } | |
3637 | ||
3638 | static inline void util_est_enqueue(struct cfs_rq *cfs_rq, | |
3639 | struct task_struct *p) | |
3640 | { | |
3641 | unsigned int enqueued; | |
3642 | ||
3643 | if (!sched_feat(UTIL_EST)) | |
3644 | return; | |
3645 | ||
3646 | /* Update root cfs_rq's estimated utilization */ | |
3647 | enqueued = cfs_rq->avg.util_est.enqueued; | |
92a801e5 | 3648 | enqueued += _task_util_est(p); |
7f65ea42 PB |
3649 | WRITE_ONCE(cfs_rq->avg.util_est.enqueued, enqueued); |
3650 | } | |
3651 | ||
3652 | /* | |
3653 | * Check if a (signed) value is within a specified (unsigned) margin, | |
3654 | * based on the observation that: | |
3655 | * | |
3656 | * abs(x) < y := (unsigned)(x + y - 1) < (2 * y - 1) | |
3657 | * | |
3658 | * NOTE: this only works when value + maring < INT_MAX. | |
3659 | */ | |
3660 | static inline bool within_margin(int value, int margin) | |
3661 | { | |
3662 | return ((unsigned int)(value + margin - 1) < (2 * margin - 1)); | |
3663 | } | |
3664 | ||
3665 | static void | |
3666 | util_est_dequeue(struct cfs_rq *cfs_rq, struct task_struct *p, bool task_sleep) | |
3667 | { | |
3668 | long last_ewma_diff; | |
3669 | struct util_est ue; | |
10a35e68 | 3670 | int cpu; |
7f65ea42 PB |
3671 | |
3672 | if (!sched_feat(UTIL_EST)) | |
3673 | return; | |
3674 | ||
3482d98b VG |
3675 | /* Update root cfs_rq's estimated utilization */ |
3676 | ue.enqueued = cfs_rq->avg.util_est.enqueued; | |
92a801e5 | 3677 | ue.enqueued -= min_t(unsigned int, ue.enqueued, _task_util_est(p)); |
7f65ea42 PB |
3678 | WRITE_ONCE(cfs_rq->avg.util_est.enqueued, ue.enqueued); |
3679 | ||
3680 | /* | |
3681 | * Skip update of task's estimated utilization when the task has not | |
3682 | * yet completed an activation, e.g. being migrated. | |
3683 | */ | |
3684 | if (!task_sleep) | |
3685 | return; | |
3686 | ||
d519329f PB |
3687 | /* |
3688 | * If the PELT values haven't changed since enqueue time, | |
3689 | * skip the util_est update. | |
3690 | */ | |
3691 | ue = p->se.avg.util_est; | |
3692 | if (ue.enqueued & UTIL_AVG_UNCHANGED) | |
3693 | return; | |
3694 | ||
7f65ea42 PB |
3695 | /* |
3696 | * Skip update of task's estimated utilization when its EWMA is | |
3697 | * already ~1% close to its last activation value. | |
3698 | */ | |
d519329f | 3699 | ue.enqueued = (task_util(p) | UTIL_AVG_UNCHANGED); |
7f65ea42 PB |
3700 | last_ewma_diff = ue.enqueued - ue.ewma; |
3701 | if (within_margin(last_ewma_diff, (SCHED_CAPACITY_SCALE / 100))) | |
3702 | return; | |
3703 | ||
10a35e68 VG |
3704 | /* |
3705 | * To avoid overestimation of actual task utilization, skip updates if | |
3706 | * we cannot grant there is idle time in this CPU. | |
3707 | */ | |
3708 | cpu = cpu_of(rq_of(cfs_rq)); | |
3709 | if (task_util(p) > capacity_orig_of(cpu)) | |
3710 | return; | |
3711 | ||
7f65ea42 PB |
3712 | /* |
3713 | * Update Task's estimated utilization | |
3714 | * | |
3715 | * When *p completes an activation we can consolidate another sample | |
3716 | * of the task size. This is done by storing the current PELT value | |
3717 | * as ue.enqueued and by using this value to update the Exponential | |
3718 | * Weighted Moving Average (EWMA): | |
3719 | * | |
3720 | * ewma(t) = w * task_util(p) + (1-w) * ewma(t-1) | |
3721 | * = w * task_util(p) + ewma(t-1) - w * ewma(t-1) | |
3722 | * = w * (task_util(p) - ewma(t-1)) + ewma(t-1) | |
3723 | * = w * ( last_ewma_diff ) + ewma(t-1) | |
3724 | * = w * (last_ewma_diff + ewma(t-1) / w) | |
3725 | * | |
3726 | * Where 'w' is the weight of new samples, which is configured to be | |
3727 | * 0.25, thus making w=1/4 ( >>= UTIL_EST_WEIGHT_SHIFT) | |
3728 | */ | |
3729 | ue.ewma <<= UTIL_EST_WEIGHT_SHIFT; | |
3730 | ue.ewma += last_ewma_diff; | |
3731 | ue.ewma >>= UTIL_EST_WEIGHT_SHIFT; | |
3732 | WRITE_ONCE(p->se.avg.util_est, ue); | |
3733 | } | |
3734 | ||
3b1baa64 MR |
3735 | static inline int task_fits_capacity(struct task_struct *p, long capacity) |
3736 | { | |
3737 | return capacity * 1024 > task_util_est(p) * capacity_margin; | |
3738 | } | |
3739 | ||
3740 | static inline void update_misfit_status(struct task_struct *p, struct rq *rq) | |
3741 | { | |
3742 | if (!static_branch_unlikely(&sched_asym_cpucapacity)) | |
3743 | return; | |
3744 | ||
3745 | if (!p) { | |
3746 | rq->misfit_task_load = 0; | |
3747 | return; | |
3748 | } | |
3749 | ||
3750 | if (task_fits_capacity(p, capacity_of(cpu_of(rq)))) { | |
3751 | rq->misfit_task_load = 0; | |
3752 | return; | |
3753 | } | |
3754 | ||
3755 | rq->misfit_task_load = task_h_load(p); | |
3756 | } | |
3757 | ||
38033c37 PZ |
3758 | #else /* CONFIG_SMP */ |
3759 | ||
d31b1a66 VG |
3760 | #define UPDATE_TG 0x0 |
3761 | #define SKIP_AGE_LOAD 0x0 | |
b382a531 | 3762 | #define DO_ATTACH 0x0 |
d31b1a66 | 3763 | |
88c0616e | 3764 | static inline void update_load_avg(struct cfs_rq *cfs_rq, struct sched_entity *se, int not_used1) |
536bd00c | 3765 | { |
ea14b57e | 3766 | cfs_rq_util_change(cfs_rq, 0); |
536bd00c RW |
3767 | } |
3768 | ||
9d89c257 | 3769 | static inline void remove_entity_load_avg(struct sched_entity *se) {} |
6e83125c | 3770 | |
a05e8c51 | 3771 | static inline void |
ea14b57e | 3772 | attach_entity_load_avg(struct cfs_rq *cfs_rq, struct sched_entity *se, int flags) {} |
a05e8c51 BP |
3773 | static inline void |
3774 | detach_entity_load_avg(struct cfs_rq *cfs_rq, struct sched_entity *se) {} | |
3775 | ||
46f69fa3 | 3776 | static inline int idle_balance(struct rq *rq, struct rq_flags *rf) |
6e83125c PZ |
3777 | { |
3778 | return 0; | |
3779 | } | |
3780 | ||
7f65ea42 PB |
3781 | static inline void |
3782 | util_est_enqueue(struct cfs_rq *cfs_rq, struct task_struct *p) {} | |
3783 | ||
3784 | static inline void | |
3785 | util_est_dequeue(struct cfs_rq *cfs_rq, struct task_struct *p, | |
3786 | bool task_sleep) {} | |
3b1baa64 | 3787 | static inline void update_misfit_status(struct task_struct *p, struct rq *rq) {} |
7f65ea42 | 3788 | |
38033c37 | 3789 | #endif /* CONFIG_SMP */ |
9d85f21c | 3790 | |
ddc97297 PZ |
3791 | static void check_spread(struct cfs_rq *cfs_rq, struct sched_entity *se) |
3792 | { | |
3793 | #ifdef CONFIG_SCHED_DEBUG | |
3794 | s64 d = se->vruntime - cfs_rq->min_vruntime; | |
3795 | ||
3796 | if (d < 0) | |
3797 | d = -d; | |
3798 | ||
3799 | if (d > 3*sysctl_sched_latency) | |
ae92882e | 3800 | schedstat_inc(cfs_rq->nr_spread_over); |
ddc97297 PZ |
3801 | #endif |
3802 | } | |
3803 | ||
aeb73b04 PZ |
3804 | static void |
3805 | place_entity(struct cfs_rq *cfs_rq, struct sched_entity *se, int initial) | |
3806 | { | |
1af5f730 | 3807 | u64 vruntime = cfs_rq->min_vruntime; |
94dfb5e7 | 3808 | |
2cb8600e PZ |
3809 | /* |
3810 | * The 'current' period is already promised to the current tasks, | |
3811 | * however the extra weight of the new task will slow them down a | |
3812 | * little, place the new task so that it fits in the slot that | |
3813 | * stays open at the end. | |
3814 | */ | |
94dfb5e7 | 3815 | if (initial && sched_feat(START_DEBIT)) |
f9c0b095 | 3816 | vruntime += sched_vslice(cfs_rq, se); |
aeb73b04 | 3817 | |
a2e7a7eb | 3818 | /* sleeps up to a single latency don't count. */ |
5ca9880c | 3819 | if (!initial) { |
a2e7a7eb | 3820 | unsigned long thresh = sysctl_sched_latency; |
a7be37ac | 3821 | |
a2e7a7eb MG |
3822 | /* |
3823 | * Halve their sleep time's effect, to allow | |
3824 | * for a gentler effect of sleepers: | |
3825 | */ | |
3826 | if (sched_feat(GENTLE_FAIR_SLEEPERS)) | |
3827 | thresh >>= 1; | |
51e0304c | 3828 | |
a2e7a7eb | 3829 | vruntime -= thresh; |
aeb73b04 PZ |
3830 | } |
3831 | ||
b5d9d734 | 3832 | /* ensure we never gain time by being placed backwards. */ |
16c8f1c7 | 3833 | se->vruntime = max_vruntime(se->vruntime, vruntime); |
aeb73b04 PZ |
3834 | } |
3835 | ||
d3d9dc33 PT |
3836 | static void check_enqueue_throttle(struct cfs_rq *cfs_rq); |
3837 | ||
cb251765 MG |
3838 | static inline void check_schedstat_required(void) |
3839 | { | |
3840 | #ifdef CONFIG_SCHEDSTATS | |
3841 | if (schedstat_enabled()) | |
3842 | return; | |
3843 | ||
3844 | /* Force schedstat enabled if a dependent tracepoint is active */ | |
3845 | if (trace_sched_stat_wait_enabled() || | |
3846 | trace_sched_stat_sleep_enabled() || | |
3847 | trace_sched_stat_iowait_enabled() || | |
3848 | trace_sched_stat_blocked_enabled() || | |
3849 | trace_sched_stat_runtime_enabled()) { | |
eda8dca5 | 3850 | printk_deferred_once("Scheduler tracepoints stat_sleep, stat_iowait, " |
cb251765 | 3851 | "stat_blocked and stat_runtime require the " |
f67abed5 | 3852 | "kernel parameter schedstats=enable or " |
cb251765 MG |
3853 | "kernel.sched_schedstats=1\n"); |
3854 | } | |
3855 | #endif | |
3856 | } | |
3857 | ||
b5179ac7 PZ |
3858 | |
3859 | /* | |
3860 | * MIGRATION | |
3861 | * | |
3862 | * dequeue | |
3863 | * update_curr() | |
3864 | * update_min_vruntime() | |
3865 | * vruntime -= min_vruntime | |
3866 | * | |
3867 | * enqueue | |
3868 | * update_curr() | |
3869 | * update_min_vruntime() | |
3870 | * vruntime += min_vruntime | |
3871 | * | |
3872 | * this way the vruntime transition between RQs is done when both | |
3873 | * min_vruntime are up-to-date. | |
3874 | * | |
3875 | * WAKEUP (remote) | |
3876 | * | |
59efa0ba | 3877 | * ->migrate_task_rq_fair() (p->state == TASK_WAKING) |
b5179ac7 PZ |
3878 | * vruntime -= min_vruntime |
3879 | * | |
3880 | * enqueue | |
3881 | * update_curr() | |
3882 | * update_min_vruntime() | |
3883 | * vruntime += min_vruntime | |
3884 | * | |
3885 | * this way we don't have the most up-to-date min_vruntime on the originating | |
3886 | * CPU and an up-to-date min_vruntime on the destination CPU. | |
3887 | */ | |
3888 | ||
bf0f6f24 | 3889 | static void |
88ec22d3 | 3890 | enqueue_entity(struct cfs_rq *cfs_rq, struct sched_entity *se, int flags) |
bf0f6f24 | 3891 | { |
2f950354 PZ |
3892 | bool renorm = !(flags & ENQUEUE_WAKEUP) || (flags & ENQUEUE_MIGRATED); |
3893 | bool curr = cfs_rq->curr == se; | |
3894 | ||
88ec22d3 | 3895 | /* |
2f950354 PZ |
3896 | * If we're the current task, we must renormalise before calling |
3897 | * update_curr(). | |
88ec22d3 | 3898 | */ |
2f950354 | 3899 | if (renorm && curr) |
88ec22d3 PZ |
3900 | se->vruntime += cfs_rq->min_vruntime; |
3901 | ||
2f950354 PZ |
3902 | update_curr(cfs_rq); |
3903 | ||
bf0f6f24 | 3904 | /* |
2f950354 PZ |
3905 | * Otherwise, renormalise after, such that we're placed at the current |
3906 | * moment in time, instead of some random moment in the past. Being | |
3907 | * placed in the past could significantly boost this task to the | |
3908 | * fairness detriment of existing tasks. | |
bf0f6f24 | 3909 | */ |
2f950354 PZ |
3910 | if (renorm && !curr) |
3911 | se->vruntime += cfs_rq->min_vruntime; | |
3912 | ||
89ee048f VG |
3913 | /* |
3914 | * When enqueuing a sched_entity, we must: | |
3915 | * - Update loads to have both entity and cfs_rq synced with now. | |
3916 | * - Add its load to cfs_rq->runnable_avg | |
3917 | * - For group_entity, update its weight to reflect the new share of | |
3918 | * its group cfs_rq | |
3919 | * - Add its new weight to cfs_rq->load.weight | |
3920 | */ | |
b382a531 | 3921 | update_load_avg(cfs_rq, se, UPDATE_TG | DO_ATTACH); |
1ea6c46a | 3922 | update_cfs_group(se); |
b5b3e35f | 3923 | enqueue_runnable_load_avg(cfs_rq, se); |
17bc14b7 | 3924 | account_entity_enqueue(cfs_rq, se); |
bf0f6f24 | 3925 | |
1a3d027c | 3926 | if (flags & ENQUEUE_WAKEUP) |
aeb73b04 | 3927 | place_entity(cfs_rq, se, 0); |
bf0f6f24 | 3928 | |
cb251765 | 3929 | check_schedstat_required(); |
4fa8d299 JP |
3930 | update_stats_enqueue(cfs_rq, se, flags); |
3931 | check_spread(cfs_rq, se); | |
2f950354 | 3932 | if (!curr) |
83b699ed | 3933 | __enqueue_entity(cfs_rq, se); |
2069dd75 | 3934 | se->on_rq = 1; |
3d4b47b4 | 3935 | |
d3d9dc33 | 3936 | if (cfs_rq->nr_running == 1) { |
3d4b47b4 | 3937 | list_add_leaf_cfs_rq(cfs_rq); |
d3d9dc33 PT |
3938 | check_enqueue_throttle(cfs_rq); |
3939 | } | |
bf0f6f24 IM |
3940 | } |
3941 | ||
2c13c919 | 3942 | static void __clear_buddies_last(struct sched_entity *se) |
2002c695 | 3943 | { |
2c13c919 RR |
3944 | for_each_sched_entity(se) { |
3945 | struct cfs_rq *cfs_rq = cfs_rq_of(se); | |
f1044799 | 3946 | if (cfs_rq->last != se) |
2c13c919 | 3947 | break; |
f1044799 PZ |
3948 | |
3949 | cfs_rq->last = NULL; | |
2c13c919 RR |
3950 | } |
3951 | } | |
2002c695 | 3952 | |
2c13c919 RR |
3953 | static void __clear_buddies_next(struct sched_entity *se) |
3954 | { | |
3955 | for_each_sched_entity(se) { | |
3956 | struct cfs_rq *cfs_rq = cfs_rq_of(se); | |
f1044799 | 3957 | if (cfs_rq->next != se) |
2c13c919 | 3958 | break; |
f1044799 PZ |
3959 | |
3960 | cfs_rq->next = NULL; | |
2c13c919 | 3961 | } |
2002c695 PZ |
3962 | } |
3963 | ||
ac53db59 RR |
3964 | static void __clear_buddies_skip(struct sched_entity *se) |
3965 | { | |
3966 | for_each_sched_entity(se) { | |
3967 | struct cfs_rq *cfs_rq = cfs_rq_of(se); | |
f1044799 | 3968 | if (cfs_rq->skip != se) |
ac53db59 | 3969 | break; |
f1044799 PZ |
3970 | |
3971 | cfs_rq->skip = NULL; | |
ac53db59 RR |
3972 | } |
3973 | } | |
3974 | ||
a571bbea PZ |
3975 | static void clear_buddies(struct cfs_rq *cfs_rq, struct sched_entity *se) |
3976 | { | |
2c13c919 RR |
3977 | if (cfs_rq->last == se) |
3978 | __clear_buddies_last(se); | |
3979 | ||
3980 | if (cfs_rq->next == se) | |
3981 | __clear_buddies_next(se); | |
ac53db59 RR |
3982 | |
3983 | if (cfs_rq->skip == se) | |
3984 | __clear_buddies_skip(se); | |
a571bbea PZ |
3985 | } |
3986 | ||
6c16a6dc | 3987 | static __always_inline void return_cfs_rq_runtime(struct cfs_rq *cfs_rq); |
d8b4986d | 3988 | |
bf0f6f24 | 3989 | static void |
371fd7e7 | 3990 | dequeue_entity(struct cfs_rq *cfs_rq, struct sched_entity *se, int flags) |
bf0f6f24 | 3991 | { |
a2a2d680 DA |
3992 | /* |
3993 | * Update run-time statistics of the 'current'. | |
3994 | */ | |
3995 | update_curr(cfs_rq); | |
89ee048f VG |
3996 | |
3997 | /* | |
3998 | * When dequeuing a sched_entity, we must: | |
3999 | * - Update loads to have both entity and cfs_rq synced with now. | |
dfcb245e IM |
4000 | * - Subtract its load from the cfs_rq->runnable_avg. |
4001 | * - Subtract its previous weight from cfs_rq->load.weight. | |
89ee048f VG |
4002 | * - For group entity, update its weight to reflect the new share |
4003 | * of its group cfs_rq. | |
4004 | */ | |
88c0616e | 4005 | update_load_avg(cfs_rq, se, UPDATE_TG); |
b5b3e35f | 4006 | dequeue_runnable_load_avg(cfs_rq, se); |
a2a2d680 | 4007 | |
4fa8d299 | 4008 | update_stats_dequeue(cfs_rq, se, flags); |
67e9fb2a | 4009 | |
2002c695 | 4010 | clear_buddies(cfs_rq, se); |
4793241b | 4011 | |
83b699ed | 4012 | if (se != cfs_rq->curr) |
30cfdcfc | 4013 | __dequeue_entity(cfs_rq, se); |
17bc14b7 | 4014 | se->on_rq = 0; |
30cfdcfc | 4015 | account_entity_dequeue(cfs_rq, se); |
88ec22d3 PZ |
4016 | |
4017 | /* | |
b60205c7 PZ |
4018 | * Normalize after update_curr(); which will also have moved |
4019 | * min_vruntime if @se is the one holding it back. But before doing | |
4020 | * update_min_vruntime() again, which will discount @se's position and | |
4021 | * can move min_vruntime forward still more. | |
88ec22d3 | 4022 | */ |
371fd7e7 | 4023 | if (!(flags & DEQUEUE_SLEEP)) |
88ec22d3 | 4024 | se->vruntime -= cfs_rq->min_vruntime; |
1e876231 | 4025 | |
d8b4986d PT |
4026 | /* return excess runtime on last dequeue */ |
4027 | return_cfs_rq_runtime(cfs_rq); | |
4028 | ||
1ea6c46a | 4029 | update_cfs_group(se); |
b60205c7 PZ |
4030 | |
4031 | /* | |
4032 | * Now advance min_vruntime if @se was the entity holding it back, | |
4033 | * except when: DEQUEUE_SAVE && !DEQUEUE_MOVE, in this case we'll be | |
4034 | * put back on, and if we advance min_vruntime, we'll be placed back | |
4035 | * further than we started -- ie. we'll be penalized. | |
4036 | */ | |
9845c49c | 4037 | if ((flags & (DEQUEUE_SAVE | DEQUEUE_MOVE)) != DEQUEUE_SAVE) |
b60205c7 | 4038 | update_min_vruntime(cfs_rq); |
bf0f6f24 IM |
4039 | } |
4040 | ||
4041 | /* | |
4042 | * Preempt the current task with a newly woken task if needed: | |
4043 | */ | |
7c92e54f | 4044 | static void |
2e09bf55 | 4045 | check_preempt_tick(struct cfs_rq *cfs_rq, struct sched_entity *curr) |
bf0f6f24 | 4046 | { |
11697830 | 4047 | unsigned long ideal_runtime, delta_exec; |
f4cfb33e WX |
4048 | struct sched_entity *se; |
4049 | s64 delta; | |
11697830 | 4050 | |
6d0f0ebd | 4051 | ideal_runtime = sched_slice(cfs_rq, curr); |
11697830 | 4052 | delta_exec = curr->sum_exec_runtime - curr->prev_sum_exec_runtime; |
a9f3e2b5 | 4053 | if (delta_exec > ideal_runtime) { |
8875125e | 4054 | resched_curr(rq_of(cfs_rq)); |
a9f3e2b5 MG |
4055 | /* |
4056 | * The current task ran long enough, ensure it doesn't get | |
4057 | * re-elected due to buddy favours. | |
4058 | */ | |
4059 | clear_buddies(cfs_rq, curr); | |
f685ceac MG |
4060 | return; |
4061 | } | |
4062 | ||
4063 | /* | |
4064 | * Ensure that a task that missed wakeup preemption by a | |
4065 | * narrow margin doesn't have to wait for a full slice. | |
4066 | * This also mitigates buddy induced latencies under load. | |
4067 | */ | |
f685ceac MG |
4068 | if (delta_exec < sysctl_sched_min_granularity) |
4069 | return; | |
4070 | ||
f4cfb33e WX |
4071 | se = __pick_first_entity(cfs_rq); |
4072 | delta = curr->vruntime - se->vruntime; | |
f685ceac | 4073 | |
f4cfb33e WX |
4074 | if (delta < 0) |
4075 | return; | |
d7d82944 | 4076 | |
f4cfb33e | 4077 | if (delta > ideal_runtime) |
8875125e | 4078 | resched_curr(rq_of(cfs_rq)); |
bf0f6f24 IM |
4079 | } |
4080 | ||
83b699ed | 4081 | static void |
8494f412 | 4082 | set_next_entity(struct cfs_rq *cfs_rq, struct sched_entity *se) |
bf0f6f24 | 4083 | { |
83b699ed SV |
4084 | /* 'current' is not kept within the tree. */ |
4085 | if (se->on_rq) { | |
4086 | /* | |
4087 | * Any task has to be enqueued before it get to execute on | |
4088 | * a CPU. So account for the time it spent waiting on the | |
4089 | * runqueue. | |
4090 | */ | |
4fa8d299 | 4091 | update_stats_wait_end(cfs_rq, se); |
83b699ed | 4092 | __dequeue_entity(cfs_rq, se); |
88c0616e | 4093 | update_load_avg(cfs_rq, se, UPDATE_TG); |
83b699ed SV |
4094 | } |
4095 | ||
79303e9e | 4096 | update_stats_curr_start(cfs_rq, se); |
429d43bc | 4097 | cfs_rq->curr = se; |
4fa8d299 | 4098 | |
eba1ed4b IM |
4099 | /* |
4100 | * Track our maximum slice length, if the CPU's load is at | |
4101 | * least twice that of our own weight (i.e. dont track it | |
4102 | * when there are only lesser-weight tasks around): | |
4103 | */ | |
cb251765 | 4104 | if (schedstat_enabled() && rq_of(cfs_rq)->load.weight >= 2*se->load.weight) { |
4fa8d299 JP |
4105 | schedstat_set(se->statistics.slice_max, |
4106 | max((u64)schedstat_val(se->statistics.slice_max), | |
4107 | se->sum_exec_runtime - se->prev_sum_exec_runtime)); | |
eba1ed4b | 4108 | } |
4fa8d299 | 4109 | |
4a55b450 | 4110 | se->prev_sum_exec_runtime = se->sum_exec_runtime; |
bf0f6f24 IM |
4111 | } |
4112 | ||
3f3a4904 PZ |
4113 | static int |
4114 | wakeup_preempt_entity(struct sched_entity *curr, struct sched_entity *se); | |
4115 | ||
ac53db59 RR |
4116 | /* |
4117 | * Pick the next process, keeping these things in mind, in this order: | |
4118 | * 1) keep things fair between processes/task groups | |
4119 | * 2) pick the "next" process, since someone really wants that to run | |
4120 | * 3) pick the "last" process, for cache locality | |
4121 | * 4) do not run the "skip" process, if something else is available | |
4122 | */ | |
678d5718 PZ |
4123 | static struct sched_entity * |
4124 | pick_next_entity(struct cfs_rq *cfs_rq, struct sched_entity *curr) | |
aa2ac252 | 4125 | { |
678d5718 PZ |
4126 | struct sched_entity *left = __pick_first_entity(cfs_rq); |
4127 | struct sched_entity *se; | |
4128 | ||
4129 | /* | |
4130 | * If curr is set we have to see if its left of the leftmost entity | |
4131 | * still in the tree, provided there was anything in the tree at all. | |
4132 | */ | |
4133 | if (!left || (curr && entity_before(curr, left))) | |
4134 | left = curr; | |
4135 | ||
4136 | se = left; /* ideally we run the leftmost entity */ | |
f4b6755f | 4137 | |
ac53db59 RR |
4138 | /* |
4139 | * Avoid running the skip buddy, if running something else can | |
4140 | * be done without getting too unfair. | |
4141 | */ | |
4142 | if (cfs_rq->skip == se) { | |
678d5718 PZ |
4143 | struct sched_entity *second; |
4144 | ||
4145 | if (se == curr) { | |
4146 | second = __pick_first_entity(cfs_rq); | |
4147 | } else { | |
4148 | second = __pick_next_entity(se); | |
4149 | if (!second || (curr && entity_before(curr, second))) | |
4150 | second = curr; | |
4151 | } | |
4152 | ||
ac53db59 RR |
4153 | if (second && wakeup_preempt_entity(second, left) < 1) |
4154 | se = second; | |
4155 | } | |
aa2ac252 | 4156 | |
f685ceac MG |
4157 | /* |
4158 | * Prefer last buddy, try to return the CPU to a preempted task. | |
4159 | */ | |
4160 | if (cfs_rq->last && wakeup_preempt_entity(cfs_rq->last, left) < 1) | |
4161 | se = cfs_rq->last; | |
4162 | ||
ac53db59 RR |
4163 | /* |
4164 | * Someone really wants this to run. If it's not unfair, run it. | |
4165 | */ | |
4166 | if (cfs_rq->next && wakeup_preempt_entity(cfs_rq->next, left) < 1) | |
4167 | se = cfs_rq->next; | |
4168 | ||
f685ceac | 4169 | clear_buddies(cfs_rq, se); |
4793241b PZ |
4170 | |
4171 | return se; | |
aa2ac252 PZ |
4172 | } |
4173 | ||
678d5718 | 4174 | static bool check_cfs_rq_runtime(struct cfs_rq *cfs_rq); |
d3d9dc33 | 4175 | |
ab6cde26 | 4176 | static void put_prev_entity(struct cfs_rq *cfs_rq, struct sched_entity *prev) |
bf0f6f24 IM |
4177 | { |
4178 | /* | |
4179 | * If still on the runqueue then deactivate_task() | |
4180 | * was not called and update_curr() has to be done: | |
4181 | */ | |
4182 | if (prev->on_rq) | |
b7cc0896 | 4183 | update_curr(cfs_rq); |
bf0f6f24 | 4184 | |
d3d9dc33 PT |
4185 | /* throttle cfs_rqs exceeding runtime */ |
4186 | check_cfs_rq_runtime(cfs_rq); | |
4187 | ||
4fa8d299 | 4188 | check_spread(cfs_rq, prev); |
cb251765 | 4189 | |
30cfdcfc | 4190 | if (prev->on_rq) { |
4fa8d299 | 4191 | update_stats_wait_start(cfs_rq, prev); |
30cfdcfc DA |
4192 | /* Put 'current' back into the tree. */ |
4193 | __enqueue_entity(cfs_rq, prev); | |
9d85f21c | 4194 | /* in !on_rq case, update occurred at dequeue */ |
88c0616e | 4195 | update_load_avg(cfs_rq, prev, 0); |
30cfdcfc | 4196 | } |
429d43bc | 4197 | cfs_rq->curr = NULL; |
bf0f6f24 IM |
4198 | } |
4199 | ||
8f4d37ec PZ |
4200 | static void |
4201 | entity_tick(struct cfs_rq *cfs_rq, struct sched_entity *curr, int queued) | |
bf0f6f24 | 4202 | { |
bf0f6f24 | 4203 | /* |
30cfdcfc | 4204 | * Update run-time statistics of the 'current'. |
bf0f6f24 | 4205 | */ |
30cfdcfc | 4206 | update_curr(cfs_rq); |
bf0f6f24 | 4207 | |
9d85f21c PT |
4208 | /* |
4209 | * Ensure that runnable average is periodically updated. | |
4210 | */ | |
88c0616e | 4211 | update_load_avg(cfs_rq, curr, UPDATE_TG); |
1ea6c46a | 4212 | update_cfs_group(curr); |
9d85f21c | 4213 | |
8f4d37ec PZ |
4214 | #ifdef CONFIG_SCHED_HRTICK |
4215 | /* | |
4216 | * queued ticks are scheduled to match the slice, so don't bother | |
4217 | * validating it and just reschedule. | |
4218 | */ | |
983ed7a6 | 4219 | if (queued) { |
8875125e | 4220 | resched_curr(rq_of(cfs_rq)); |
983ed7a6 HH |
4221 | return; |
4222 | } | |
8f4d37ec PZ |
4223 | /* |
4224 | * don't let the period tick interfere with the hrtick preemption | |
4225 | */ | |
4226 | if (!sched_feat(DOUBLE_TICK) && | |
4227 | hrtimer_active(&rq_of(cfs_rq)->hrtick_timer)) | |
4228 | return; | |
4229 | #endif | |
4230 | ||
2c2efaed | 4231 | if (cfs_rq->nr_running > 1) |
2e09bf55 | 4232 | check_preempt_tick(cfs_rq, curr); |
bf0f6f24 IM |
4233 | } |
4234 | ||
ab84d31e PT |
4235 | |
4236 | /************************************************** | |
4237 | * CFS bandwidth control machinery | |
4238 | */ | |
4239 | ||
4240 | #ifdef CONFIG_CFS_BANDWIDTH | |
029632fb | 4241 | |
e9666d10 | 4242 | #ifdef CONFIG_JUMP_LABEL |
c5905afb | 4243 | static struct static_key __cfs_bandwidth_used; |
029632fb PZ |
4244 | |
4245 | static inline bool cfs_bandwidth_used(void) | |
4246 | { | |
c5905afb | 4247 | return static_key_false(&__cfs_bandwidth_used); |
029632fb PZ |
4248 | } |
4249 | ||
1ee14e6c | 4250 | void cfs_bandwidth_usage_inc(void) |
029632fb | 4251 | { |
ce48c146 | 4252 | static_key_slow_inc_cpuslocked(&__cfs_bandwidth_used); |
1ee14e6c BS |
4253 | } |
4254 | ||
4255 | void cfs_bandwidth_usage_dec(void) | |
4256 | { | |
ce48c146 | 4257 | static_key_slow_dec_cpuslocked(&__cfs_bandwidth_used); |
029632fb | 4258 | } |
e9666d10 | 4259 | #else /* CONFIG_JUMP_LABEL */ |
029632fb PZ |
4260 | static bool cfs_bandwidth_used(void) |
4261 | { | |
4262 | return true; | |
4263 | } | |
4264 | ||
1ee14e6c BS |
4265 | void cfs_bandwidth_usage_inc(void) {} |
4266 | void cfs_bandwidth_usage_dec(void) {} | |
e9666d10 | 4267 | #endif /* CONFIG_JUMP_LABEL */ |
029632fb | 4268 | |
ab84d31e PT |
4269 | /* |
4270 | * default period for cfs group bandwidth. | |
4271 | * default: 0.1s, units: nanoseconds | |
4272 | */ | |
4273 | static inline u64 default_cfs_period(void) | |
4274 | { | |
4275 | return 100000000ULL; | |
4276 | } | |
ec12cb7f PT |
4277 | |
4278 | static inline u64 sched_cfs_bandwidth_slice(void) | |
4279 | { | |
4280 | return (u64)sysctl_sched_cfs_bandwidth_slice * NSEC_PER_USEC; | |
4281 | } | |
4282 | ||
a9cf55b2 PT |
4283 | /* |
4284 | * Replenish runtime according to assigned quota and update expiration time. | |
4285 | * We use sched_clock_cpu directly instead of rq->clock to avoid adding | |
4286 | * additional synchronization around rq->lock. | |
4287 | * | |
4288 | * requires cfs_b->lock | |
4289 | */ | |
029632fb | 4290 | void __refill_cfs_bandwidth_runtime(struct cfs_bandwidth *cfs_b) |
a9cf55b2 PT |
4291 | { |
4292 | u64 now; | |
4293 | ||
4294 | if (cfs_b->quota == RUNTIME_INF) | |
4295 | return; | |
4296 | ||
4297 | now = sched_clock_cpu(smp_processor_id()); | |
4298 | cfs_b->runtime = cfs_b->quota; | |
4299 | cfs_b->runtime_expires = now + ktime_to_ns(cfs_b->period); | |
512ac999 | 4300 | cfs_b->expires_seq++; |
a9cf55b2 PT |
4301 | } |
4302 | ||
029632fb PZ |
4303 | static inline struct cfs_bandwidth *tg_cfs_bandwidth(struct task_group *tg) |
4304 | { | |
4305 | return &tg->cfs_bandwidth; | |
4306 | } | |
4307 | ||
f1b17280 PT |
4308 | /* rq->task_clock normalized against any time this cfs_rq has spent throttled */ |
4309 | static inline u64 cfs_rq_clock_task(struct cfs_rq *cfs_rq) | |
4310 | { | |
4311 | if (unlikely(cfs_rq->throttle_count)) | |
1a99ae3f | 4312 | return cfs_rq->throttled_clock_task - cfs_rq->throttled_clock_task_time; |
f1b17280 | 4313 | |
78becc27 | 4314 | return rq_clock_task(rq_of(cfs_rq)) - cfs_rq->throttled_clock_task_time; |
f1b17280 PT |
4315 | } |
4316 | ||
85dac906 PT |
4317 | /* returns 0 on failure to allocate runtime */ |
4318 | static int assign_cfs_rq_runtime(struct cfs_rq *cfs_rq) | |
ec12cb7f PT |
4319 | { |
4320 | struct task_group *tg = cfs_rq->tg; | |
4321 | struct cfs_bandwidth *cfs_b = tg_cfs_bandwidth(tg); | |
a9cf55b2 | 4322 | u64 amount = 0, min_amount, expires; |
512ac999 | 4323 | int expires_seq; |
ec12cb7f PT |
4324 | |
4325 | /* note: this is a positive sum as runtime_remaining <= 0 */ | |
4326 | min_amount = sched_cfs_bandwidth_slice() - cfs_rq->runtime_remaining; | |
4327 | ||
4328 | raw_spin_lock(&cfs_b->lock); | |
4329 | if (cfs_b->quota == RUNTIME_INF) | |
4330 | amount = min_amount; | |
58088ad0 | 4331 | else { |
77a4d1a1 | 4332 | start_cfs_bandwidth(cfs_b); |
58088ad0 PT |
4333 | |
4334 | if (cfs_b->runtime > 0) { | |
4335 | amount = min(cfs_b->runtime, min_amount); | |
4336 | cfs_b->runtime -= amount; | |
4337 | cfs_b->idle = 0; | |
4338 | } | |
ec12cb7f | 4339 | } |
512ac999 | 4340 | expires_seq = cfs_b->expires_seq; |
a9cf55b2 | 4341 | expires = cfs_b->runtime_expires; |
ec12cb7f PT |
4342 | raw_spin_unlock(&cfs_b->lock); |
4343 | ||
4344 | cfs_rq->runtime_remaining += amount; | |
a9cf55b2 PT |
4345 | /* |
4346 | * we may have advanced our local expiration to account for allowed | |
4347 | * spread between our sched_clock and the one on which runtime was | |
4348 | * issued. | |
4349 | */ | |
512ac999 XP |
4350 | if (cfs_rq->expires_seq != expires_seq) { |
4351 | cfs_rq->expires_seq = expires_seq; | |
a9cf55b2 | 4352 | cfs_rq->runtime_expires = expires; |
512ac999 | 4353 | } |
85dac906 PT |
4354 | |
4355 | return cfs_rq->runtime_remaining > 0; | |
ec12cb7f PT |
4356 | } |
4357 | ||
a9cf55b2 PT |
4358 | /* |
4359 | * Note: This depends on the synchronization provided by sched_clock and the | |
4360 | * fact that rq->clock snapshots this value. | |
4361 | */ | |
4362 | static void expire_cfs_rq_runtime(struct cfs_rq *cfs_rq) | |
ec12cb7f | 4363 | { |
a9cf55b2 | 4364 | struct cfs_bandwidth *cfs_b = tg_cfs_bandwidth(cfs_rq->tg); |
a9cf55b2 PT |
4365 | |
4366 | /* if the deadline is ahead of our clock, nothing to do */ | |
78becc27 | 4367 | if (likely((s64)(rq_clock(rq_of(cfs_rq)) - cfs_rq->runtime_expires) < 0)) |
ec12cb7f PT |
4368 | return; |
4369 | ||
a9cf55b2 PT |
4370 | if (cfs_rq->runtime_remaining < 0) |
4371 | return; | |
4372 | ||
4373 | /* | |
4374 | * If the local deadline has passed we have to consider the | |
4375 | * possibility that our sched_clock is 'fast' and the global deadline | |
4376 | * has not truly expired. | |
4377 | * | |
4378 | * Fortunately we can check determine whether this the case by checking | |
512ac999 | 4379 | * whether the global deadline(cfs_b->expires_seq) has advanced. |
a9cf55b2 | 4380 | */ |
512ac999 | 4381 | if (cfs_rq->expires_seq == cfs_b->expires_seq) { |
a9cf55b2 PT |
4382 | /* extend local deadline, drift is bounded above by 2 ticks */ |
4383 | cfs_rq->runtime_expires += TICK_NSEC; | |
4384 | } else { | |
4385 | /* global deadline is ahead, expiration has passed */ | |
4386 | cfs_rq->runtime_remaining = 0; | |
4387 | } | |
4388 | } | |
4389 | ||
9dbdb155 | 4390 | static void __account_cfs_rq_runtime(struct cfs_rq *cfs_rq, u64 delta_exec) |
a9cf55b2 PT |
4391 | { |
4392 | /* dock delta_exec before expiring quota (as it could span periods) */ | |
ec12cb7f | 4393 | cfs_rq->runtime_remaining -= delta_exec; |
a9cf55b2 PT |
4394 | expire_cfs_rq_runtime(cfs_rq); |
4395 | ||
4396 | if (likely(cfs_rq->runtime_remaining > 0)) | |
ec12cb7f PT |
4397 | return; |
4398 | ||
85dac906 PT |
4399 | /* |
4400 | * if we're unable to extend our runtime we resched so that the active | |
4401 | * hierarchy can be throttled | |
4402 | */ | |
4403 | if (!assign_cfs_rq_runtime(cfs_rq) && likely(cfs_rq->curr)) | |
8875125e | 4404 | resched_curr(rq_of(cfs_rq)); |
ec12cb7f PT |
4405 | } |
4406 | ||
6c16a6dc | 4407 | static __always_inline |
9dbdb155 | 4408 | void account_cfs_rq_runtime(struct cfs_rq *cfs_rq, u64 delta_exec) |
ec12cb7f | 4409 | { |
56f570e5 | 4410 | if (!cfs_bandwidth_used() || !cfs_rq->runtime_enabled) |
ec12cb7f PT |
4411 | return; |
4412 | ||
4413 | __account_cfs_rq_runtime(cfs_rq, delta_exec); | |
4414 | } | |
4415 | ||
85dac906 PT |
4416 | static inline int cfs_rq_throttled(struct cfs_rq *cfs_rq) |
4417 | { | |
56f570e5 | 4418 | return cfs_bandwidth_used() && cfs_rq->throttled; |
85dac906 PT |
4419 | } |
4420 | ||
64660c86 PT |
4421 | /* check whether cfs_rq, or any parent, is throttled */ |
4422 | static inline int throttled_hierarchy(struct cfs_rq *cfs_rq) | |
4423 | { | |
56f570e5 | 4424 | return cfs_bandwidth_used() && cfs_rq->throttle_count; |
64660c86 PT |
4425 | } |
4426 | ||
4427 | /* | |
4428 | * Ensure that neither of the group entities corresponding to src_cpu or | |
4429 | * dest_cpu are members of a throttled hierarchy when performing group | |
4430 | * load-balance operations. | |
4431 | */ | |
4432 | static inline int throttled_lb_pair(struct task_group *tg, | |
4433 | int src_cpu, int dest_cpu) | |
4434 | { | |
4435 | struct cfs_rq *src_cfs_rq, *dest_cfs_rq; | |
4436 | ||
4437 | src_cfs_rq = tg->cfs_rq[src_cpu]; | |
4438 | dest_cfs_rq = tg->cfs_rq[dest_cpu]; | |
4439 | ||
4440 | return throttled_hierarchy(src_cfs_rq) || | |
4441 | throttled_hierarchy(dest_cfs_rq); | |
4442 | } | |
4443 | ||
64660c86 PT |
4444 | static int tg_unthrottle_up(struct task_group *tg, void *data) |
4445 | { | |
4446 | struct rq *rq = data; | |
4447 | struct cfs_rq *cfs_rq = tg->cfs_rq[cpu_of(rq)]; | |
4448 | ||
4449 | cfs_rq->throttle_count--; | |
64660c86 | 4450 | if (!cfs_rq->throttle_count) { |
f1b17280 | 4451 | /* adjust cfs_rq_clock_task() */ |
78becc27 | 4452 | cfs_rq->throttled_clock_task_time += rq_clock_task(rq) - |
f1b17280 | 4453 | cfs_rq->throttled_clock_task; |
31bc6aea VG |
4454 | |
4455 | /* Add cfs_rq with already running entity in the list */ | |
4456 | if (cfs_rq->nr_running >= 1) | |
4457 | list_add_leaf_cfs_rq(cfs_rq); | |
64660c86 | 4458 | } |
64660c86 PT |
4459 | |
4460 | return 0; | |
4461 | } | |
4462 | ||
4463 | static int tg_throttle_down(struct task_group *tg, void *data) | |
4464 | { | |
4465 | struct rq *rq = data; | |
4466 | struct cfs_rq *cfs_rq = tg->cfs_rq[cpu_of(rq)]; | |
4467 | ||
82958366 | 4468 | /* group is entering throttled state, stop time */ |
31bc6aea | 4469 | if (!cfs_rq->throttle_count) { |
78becc27 | 4470 | cfs_rq->throttled_clock_task = rq_clock_task(rq); |
31bc6aea VG |
4471 | list_del_leaf_cfs_rq(cfs_rq); |
4472 | } | |
64660c86 PT |
4473 | cfs_rq->throttle_count++; |
4474 | ||
4475 | return 0; | |
4476 | } | |
4477 | ||
d3d9dc33 | 4478 | static void throttle_cfs_rq(struct cfs_rq *cfs_rq) |
85dac906 PT |
4479 | { |
4480 | struct rq *rq = rq_of(cfs_rq); | |
4481 | struct cfs_bandwidth *cfs_b = tg_cfs_bandwidth(cfs_rq->tg); | |
4482 | struct sched_entity *se; | |
4483 | long task_delta, dequeue = 1; | |
77a4d1a1 | 4484 | bool empty; |
85dac906 PT |
4485 | |
4486 | se = cfs_rq->tg->se[cpu_of(rq_of(cfs_rq))]; | |
4487 | ||
f1b17280 | 4488 | /* freeze hierarchy runnable averages while throttled */ |
64660c86 PT |
4489 | rcu_read_lock(); |
4490 | walk_tg_tree_from(cfs_rq->tg, tg_throttle_down, tg_nop, (void *)rq); | |
4491 | rcu_read_unlock(); | |
85dac906 PT |
4492 | |
4493 | task_delta = cfs_rq->h_nr_running; | |
4494 | for_each_sched_entity(se) { | |
4495 | struct cfs_rq *qcfs_rq = cfs_rq_of(se); | |
4496 | /* throttled entity or throttle-on-deactivate */ | |
4497 | if (!se->on_rq) | |
4498 | break; | |
4499 | ||
4500 | if (dequeue) | |
4501 | dequeue_entity(qcfs_rq, se, DEQUEUE_SLEEP); | |
4502 | qcfs_rq->h_nr_running -= task_delta; | |
4503 | ||
4504 | if (qcfs_rq->load.weight) | |
4505 | dequeue = 0; | |
4506 | } | |
4507 | ||
4508 | if (!se) | |
72465447 | 4509 | sub_nr_running(rq, task_delta); |
85dac906 PT |
4510 | |
4511 | cfs_rq->throttled = 1; | |
78becc27 | 4512 | cfs_rq->throttled_clock = rq_clock(rq); |
85dac906 | 4513 | raw_spin_lock(&cfs_b->lock); |
d49db342 | 4514 | empty = list_empty(&cfs_b->throttled_cfs_rq); |
77a4d1a1 | 4515 | |
c06f04c7 BS |
4516 | /* |
4517 | * Add to the _head_ of the list, so that an already-started | |
baa9be4f PA |
4518 | * distribute_cfs_runtime will not see us. If disribute_cfs_runtime is |
4519 | * not running add to the tail so that later runqueues don't get starved. | |
c06f04c7 | 4520 | */ |
baa9be4f PA |
4521 | if (cfs_b->distribute_running) |
4522 | list_add_rcu(&cfs_rq->throttled_list, &cfs_b->throttled_cfs_rq); | |
4523 | else | |
4524 | list_add_tail_rcu(&cfs_rq->throttled_list, &cfs_b->throttled_cfs_rq); | |
77a4d1a1 PZ |
4525 | |
4526 | /* | |
4527 | * If we're the first throttled task, make sure the bandwidth | |
4528 | * timer is running. | |
4529 | */ | |
4530 | if (empty) | |
4531 | start_cfs_bandwidth(cfs_b); | |
4532 | ||
85dac906 PT |
4533 | raw_spin_unlock(&cfs_b->lock); |
4534 | } | |
4535 | ||
029632fb | 4536 | void unthrottle_cfs_rq(struct cfs_rq *cfs_rq) |
671fd9da PT |
4537 | { |
4538 | struct rq *rq = rq_of(cfs_rq); | |
4539 | struct cfs_bandwidth *cfs_b = tg_cfs_bandwidth(cfs_rq->tg); | |
4540 | struct sched_entity *se; | |
4541 | int enqueue = 1; | |
4542 | long task_delta; | |
4543 | ||
22b958d8 | 4544 | se = cfs_rq->tg->se[cpu_of(rq)]; |
671fd9da PT |
4545 | |
4546 | cfs_rq->throttled = 0; | |
1a55af2e FW |
4547 | |
4548 | update_rq_clock(rq); | |
4549 | ||
671fd9da | 4550 | raw_spin_lock(&cfs_b->lock); |
78becc27 | 4551 | cfs_b->throttled_time += rq_clock(rq) - cfs_rq->throttled_clock; |
671fd9da PT |
4552 | list_del_rcu(&cfs_rq->throttled_list); |
4553 | raw_spin_unlock(&cfs_b->lock); | |
4554 | ||
64660c86 PT |
4555 | /* update hierarchical throttle state */ |
4556 | walk_tg_tree_from(cfs_rq->tg, tg_nop, tg_unthrottle_up, (void *)rq); | |
4557 | ||
671fd9da PT |
4558 | if (!cfs_rq->load.weight) |
4559 | return; | |
4560 | ||
4561 | task_delta = cfs_rq->h_nr_running; | |
4562 | for_each_sched_entity(se) { | |
4563 | if (se->on_rq) | |
4564 | enqueue = 0; | |
4565 | ||
4566 | cfs_rq = cfs_rq_of(se); | |
4567 | if (enqueue) | |
4568 | enqueue_entity(cfs_rq, se, ENQUEUE_WAKEUP); | |
4569 | cfs_rq->h_nr_running += task_delta; | |
4570 | ||
4571 | if (cfs_rq_throttled(cfs_rq)) | |
4572 | break; | |
4573 | } | |
4574 | ||
31bc6aea VG |
4575 | assert_list_leaf_cfs_rq(rq); |
4576 | ||
671fd9da | 4577 | if (!se) |
72465447 | 4578 | add_nr_running(rq, task_delta); |
671fd9da | 4579 | |
97fb7a0a | 4580 | /* Determine whether we need to wake up potentially idle CPU: */ |
671fd9da | 4581 | if (rq->curr == rq->idle && rq->cfs.nr_running) |
8875125e | 4582 | resched_curr(rq); |
671fd9da PT |
4583 | } |
4584 | ||
4585 | static u64 distribute_cfs_runtime(struct cfs_bandwidth *cfs_b, | |
4586 | u64 remaining, u64 expires) | |
4587 | { | |
4588 | struct cfs_rq *cfs_rq; | |
c06f04c7 BS |
4589 | u64 runtime; |
4590 | u64 starting_runtime = remaining; | |
671fd9da PT |
4591 | |
4592 | rcu_read_lock(); | |
4593 | list_for_each_entry_rcu(cfs_rq, &cfs_b->throttled_cfs_rq, | |
4594 | throttled_list) { | |
4595 | struct rq *rq = rq_of(cfs_rq); | |
8a8c69c3 | 4596 | struct rq_flags rf; |
671fd9da | 4597 | |
c0ad4aa4 | 4598 | rq_lock_irqsave(rq, &rf); |
671fd9da PT |
4599 | if (!cfs_rq_throttled(cfs_rq)) |
4600 | goto next; | |
4601 | ||
4602 | runtime = -cfs_rq->runtime_remaining + 1; | |
4603 | if (runtime > remaining) | |
4604 | runtime = remaining; | |
4605 | remaining -= runtime; | |
4606 | ||
4607 | cfs_rq->runtime_remaining += runtime; | |
4608 | cfs_rq->runtime_expires = expires; | |
4609 | ||
4610 | /* we check whether we're throttled above */ | |
4611 | if (cfs_rq->runtime_remaining > 0) | |
4612 | unthrottle_cfs_rq(cfs_rq); | |
4613 | ||
4614 | next: | |
c0ad4aa4 | 4615 | rq_unlock_irqrestore(rq, &rf); |
671fd9da PT |
4616 | |
4617 | if (!remaining) | |
4618 | break; | |
4619 | } | |
4620 | rcu_read_unlock(); | |
4621 | ||
c06f04c7 | 4622 | return starting_runtime - remaining; |
671fd9da PT |
4623 | } |
4624 | ||
58088ad0 PT |
4625 | /* |
4626 | * Responsible for refilling a task_group's bandwidth and unthrottling its | |
4627 | * cfs_rqs as appropriate. If there has been no activity within the last | |
4628 | * period the timer is deactivated until scheduling resumes; cfs_b->idle is | |
4629 | * used to track this state. | |
4630 | */ | |
c0ad4aa4 | 4631 | static int do_sched_cfs_period_timer(struct cfs_bandwidth *cfs_b, int overrun, unsigned long flags) |
58088ad0 | 4632 | { |
671fd9da | 4633 | u64 runtime, runtime_expires; |
51f2176d | 4634 | int throttled; |
58088ad0 | 4635 | |
58088ad0 PT |
4636 | /* no need to continue the timer with no bandwidth constraint */ |
4637 | if (cfs_b->quota == RUNTIME_INF) | |
51f2176d | 4638 | goto out_deactivate; |
58088ad0 | 4639 | |
671fd9da | 4640 | throttled = !list_empty(&cfs_b->throttled_cfs_rq); |
e8da1b18 | 4641 | cfs_b->nr_periods += overrun; |
671fd9da | 4642 | |
51f2176d BS |
4643 | /* |
4644 | * idle depends on !throttled (for the case of a large deficit), and if | |
4645 | * we're going inactive then everything else can be deferred | |
4646 | */ | |
4647 | if (cfs_b->idle && !throttled) | |
4648 | goto out_deactivate; | |
a9cf55b2 PT |
4649 | |
4650 | __refill_cfs_bandwidth_runtime(cfs_b); | |
4651 | ||
671fd9da PT |
4652 | if (!throttled) { |
4653 | /* mark as potentially idle for the upcoming period */ | |
4654 | cfs_b->idle = 1; | |
51f2176d | 4655 | return 0; |
671fd9da PT |
4656 | } |
4657 | ||
e8da1b18 NR |
4658 | /* account preceding periods in which throttling occurred */ |
4659 | cfs_b->nr_throttled += overrun; | |
4660 | ||
671fd9da | 4661 | runtime_expires = cfs_b->runtime_expires; |
671fd9da PT |
4662 | |
4663 | /* | |
c06f04c7 BS |
4664 | * This check is repeated as we are holding onto the new bandwidth while |
4665 | * we unthrottle. This can potentially race with an unthrottled group | |
4666 | * trying to acquire new bandwidth from the global pool. This can result | |
4667 | * in us over-using our runtime if it is all used during this loop, but | |
4668 | * only by limited amounts in that extreme case. | |
671fd9da | 4669 | */ |
baa9be4f | 4670 | while (throttled && cfs_b->runtime > 0 && !cfs_b->distribute_running) { |
c06f04c7 | 4671 | runtime = cfs_b->runtime; |
baa9be4f | 4672 | cfs_b->distribute_running = 1; |
c0ad4aa4 | 4673 | raw_spin_unlock_irqrestore(&cfs_b->lock, flags); |
671fd9da PT |
4674 | /* we can't nest cfs_b->lock while distributing bandwidth */ |
4675 | runtime = distribute_cfs_runtime(cfs_b, runtime, | |
4676 | runtime_expires); | |
c0ad4aa4 | 4677 | raw_spin_lock_irqsave(&cfs_b->lock, flags); |
671fd9da | 4678 | |
baa9be4f | 4679 | cfs_b->distribute_running = 0; |
671fd9da | 4680 | throttled = !list_empty(&cfs_b->throttled_cfs_rq); |
c06f04c7 | 4681 | |
b5c0ce7b | 4682 | lsub_positive(&cfs_b->runtime, runtime); |
671fd9da | 4683 | } |
58088ad0 | 4684 | |
671fd9da PT |
4685 | /* |
4686 | * While we are ensured activity in the period following an | |
4687 | * unthrottle, this also covers the case in which the new bandwidth is | |
4688 | * insufficient to cover the existing bandwidth deficit. (Forcing the | |
4689 | * timer to remain active while there are any throttled entities.) | |
4690 | */ | |
4691 | cfs_b->idle = 0; | |
58088ad0 | 4692 | |
51f2176d BS |
4693 | return 0; |
4694 | ||
4695 | out_deactivate: | |
51f2176d | 4696 | return 1; |
58088ad0 | 4697 | } |
d3d9dc33 | 4698 | |
d8b4986d PT |
4699 | /* a cfs_rq won't donate quota below this amount */ |
4700 | static const u64 min_cfs_rq_runtime = 1 * NSEC_PER_MSEC; | |
4701 | /* minimum remaining period time to redistribute slack quota */ | |
4702 | static const u64 min_bandwidth_expiration = 2 * NSEC_PER_MSEC; | |
4703 | /* how long we wait to gather additional slack before distributing */ | |
4704 | static const u64 cfs_bandwidth_slack_period = 5 * NSEC_PER_MSEC; | |
4705 | ||
db06e78c BS |
4706 | /* |
4707 | * Are we near the end of the current quota period? | |
4708 | * | |
4709 | * Requires cfs_b->lock for hrtimer_expires_remaining to be safe against the | |
4961b6e1 | 4710 | * hrtimer base being cleared by hrtimer_start. In the case of |
db06e78c BS |
4711 | * migrate_hrtimers, base is never cleared, so we are fine. |
4712 | */ | |
d8b4986d PT |
4713 | static int runtime_refresh_within(struct cfs_bandwidth *cfs_b, u64 min_expire) |
4714 | { | |
4715 | struct hrtimer *refresh_timer = &cfs_b->period_timer; | |
4716 | u64 remaining; | |
4717 | ||
4718 | /* if the call-back is running a quota refresh is already occurring */ | |
4719 | if (hrtimer_callback_running(refresh_timer)) | |
4720 | return 1; | |
4721 | ||
4722 | /* is a quota refresh about to occur? */ | |
4723 | remaining = ktime_to_ns(hrtimer_expires_remaining(refresh_timer)); | |
4724 | if (remaining < min_expire) | |
4725 | return 1; | |
4726 | ||
4727 | return 0; | |
4728 | } | |
4729 | ||
4730 | static void start_cfs_slack_bandwidth(struct cfs_bandwidth *cfs_b) | |
4731 | { | |
4732 | u64 min_left = cfs_bandwidth_slack_period + min_bandwidth_expiration; | |
4733 | ||
4734 | /* if there's a quota refresh soon don't bother with slack */ | |
4735 | if (runtime_refresh_within(cfs_b, min_left)) | |
4736 | return; | |
4737 | ||
4cfafd30 PZ |
4738 | hrtimer_start(&cfs_b->slack_timer, |
4739 | ns_to_ktime(cfs_bandwidth_slack_period), | |
4740 | HRTIMER_MODE_REL); | |
d8b4986d PT |
4741 | } |
4742 | ||
4743 | /* we know any runtime found here is valid as update_curr() precedes return */ | |
4744 | static void __return_cfs_rq_runtime(struct cfs_rq *cfs_rq) | |
4745 | { | |
4746 | struct cfs_bandwidth *cfs_b = tg_cfs_bandwidth(cfs_rq->tg); | |
4747 | s64 slack_runtime = cfs_rq->runtime_remaining - min_cfs_rq_runtime; | |
4748 | ||
4749 | if (slack_runtime <= 0) | |
4750 | return; | |
4751 | ||
4752 | raw_spin_lock(&cfs_b->lock); | |
4753 | if (cfs_b->quota != RUNTIME_INF && | |
4754 | cfs_rq->runtime_expires == cfs_b->runtime_expires) { | |
4755 | cfs_b->runtime += slack_runtime; | |
4756 | ||
4757 | /* we are under rq->lock, defer unthrottling using a timer */ | |
4758 | if (cfs_b->runtime > sched_cfs_bandwidth_slice() && | |
4759 | !list_empty(&cfs_b->throttled_cfs_rq)) | |
4760 | start_cfs_slack_bandwidth(cfs_b); | |
4761 | } | |
4762 | raw_spin_unlock(&cfs_b->lock); | |
4763 | ||
4764 | /* even if it's not valid for return we don't want to try again */ | |
4765 | cfs_rq->runtime_remaining -= slack_runtime; | |
4766 | } | |
4767 | ||
4768 | static __always_inline void return_cfs_rq_runtime(struct cfs_rq *cfs_rq) | |
4769 | { | |
56f570e5 PT |
4770 | if (!cfs_bandwidth_used()) |
4771 | return; | |
4772 | ||
fccfdc6f | 4773 | if (!cfs_rq->runtime_enabled || cfs_rq->nr_running) |
d8b4986d PT |
4774 | return; |
4775 | ||
4776 | __return_cfs_rq_runtime(cfs_rq); | |
4777 | } | |
4778 | ||
4779 | /* | |
4780 | * This is done with a timer (instead of inline with bandwidth return) since | |
4781 | * it's necessary to juggle rq->locks to unthrottle their respective cfs_rqs. | |
4782 | */ | |
4783 | static void do_sched_cfs_slack_timer(struct cfs_bandwidth *cfs_b) | |
4784 | { | |
4785 | u64 runtime = 0, slice = sched_cfs_bandwidth_slice(); | |
c0ad4aa4 | 4786 | unsigned long flags; |
d8b4986d PT |
4787 | u64 expires; |
4788 | ||
4789 | /* confirm we're still not at a refresh boundary */ | |
c0ad4aa4 | 4790 | raw_spin_lock_irqsave(&cfs_b->lock, flags); |
baa9be4f | 4791 | if (cfs_b->distribute_running) { |
c0ad4aa4 | 4792 | raw_spin_unlock_irqrestore(&cfs_b->lock, flags); |
baa9be4f PA |
4793 | return; |
4794 | } | |
4795 | ||
db06e78c | 4796 | if (runtime_refresh_within(cfs_b, min_bandwidth_expiration)) { |
c0ad4aa4 | 4797 | raw_spin_unlock_irqrestore(&cfs_b->lock, flags); |
d8b4986d | 4798 | return; |
db06e78c | 4799 | } |
d8b4986d | 4800 | |
c06f04c7 | 4801 | if (cfs_b->quota != RUNTIME_INF && cfs_b->runtime > slice) |
d8b4986d | 4802 | runtime = cfs_b->runtime; |
c06f04c7 | 4803 | |
d8b4986d | 4804 | expires = cfs_b->runtime_expires; |
baa9be4f PA |
4805 | if (runtime) |
4806 | cfs_b->distribute_running = 1; | |
4807 | ||
c0ad4aa4 | 4808 | raw_spin_unlock_irqrestore(&cfs_b->lock, flags); |
d8b4986d PT |
4809 | |
4810 | if (!runtime) | |
4811 | return; | |
4812 | ||
4813 | runtime = distribute_cfs_runtime(cfs_b, runtime, expires); | |
4814 | ||
c0ad4aa4 | 4815 | raw_spin_lock_irqsave(&cfs_b->lock, flags); |
d8b4986d | 4816 | if (expires == cfs_b->runtime_expires) |
b5c0ce7b | 4817 | lsub_positive(&cfs_b->runtime, runtime); |
baa9be4f | 4818 | cfs_b->distribute_running = 0; |
c0ad4aa4 | 4819 | raw_spin_unlock_irqrestore(&cfs_b->lock, flags); |
d8b4986d PT |
4820 | } |
4821 | ||
d3d9dc33 PT |
4822 | /* |
4823 | * When a group wakes up we want to make sure that its quota is not already | |
4824 | * expired/exceeded, otherwise it may be allowed to steal additional ticks of | |
4825 | * runtime as update_curr() throttling can not not trigger until it's on-rq. | |
4826 | */ | |
4827 | static void check_enqueue_throttle(struct cfs_rq *cfs_rq) | |
4828 | { | |
56f570e5 PT |
4829 | if (!cfs_bandwidth_used()) |
4830 | return; | |
4831 | ||
d3d9dc33 PT |
4832 | /* an active group must be handled by the update_curr()->put() path */ |
4833 | if (!cfs_rq->runtime_enabled || cfs_rq->curr) | |
4834 | return; | |
4835 | ||
4836 | /* ensure the group is not already throttled */ | |
4837 | if (cfs_rq_throttled(cfs_rq)) | |
4838 | return; | |
4839 | ||
4840 | /* update runtime allocation */ | |
4841 | account_cfs_rq_runtime(cfs_rq, 0); | |
4842 | if (cfs_rq->runtime_remaining <= 0) | |
4843 | throttle_cfs_rq(cfs_rq); | |
4844 | } | |
4845 | ||
55e16d30 PZ |
4846 | static void sync_throttle(struct task_group *tg, int cpu) |
4847 | { | |
4848 | struct cfs_rq *pcfs_rq, *cfs_rq; | |
4849 | ||
4850 | if (!cfs_bandwidth_used()) | |
4851 | return; | |
4852 | ||
4853 | if (!tg->parent) | |
4854 | return; | |
4855 | ||
4856 | cfs_rq = tg->cfs_rq[cpu]; | |
4857 | pcfs_rq = tg->parent->cfs_rq[cpu]; | |
4858 | ||
4859 | cfs_rq->throttle_count = pcfs_rq->throttle_count; | |
b8922125 | 4860 | cfs_rq->throttled_clock_task = rq_clock_task(cpu_rq(cpu)); |
55e16d30 PZ |
4861 | } |
4862 | ||
d3d9dc33 | 4863 | /* conditionally throttle active cfs_rq's from put_prev_entity() */ |
678d5718 | 4864 | static bool check_cfs_rq_runtime(struct cfs_rq *cfs_rq) |
d3d9dc33 | 4865 | { |
56f570e5 | 4866 | if (!cfs_bandwidth_used()) |
678d5718 | 4867 | return false; |
56f570e5 | 4868 | |
d3d9dc33 | 4869 | if (likely(!cfs_rq->runtime_enabled || cfs_rq->runtime_remaining > 0)) |
678d5718 | 4870 | return false; |
d3d9dc33 PT |
4871 | |
4872 | /* | |
4873 | * it's possible for a throttled entity to be forced into a running | |
4874 | * state (e.g. set_curr_task), in this case we're finished. | |
4875 | */ | |
4876 | if (cfs_rq_throttled(cfs_rq)) | |
678d5718 | 4877 | return true; |
d3d9dc33 PT |
4878 | |
4879 | throttle_cfs_rq(cfs_rq); | |
678d5718 | 4880 | return true; |
d3d9dc33 | 4881 | } |
029632fb | 4882 | |
029632fb PZ |
4883 | static enum hrtimer_restart sched_cfs_slack_timer(struct hrtimer *timer) |
4884 | { | |
4885 | struct cfs_bandwidth *cfs_b = | |
4886 | container_of(timer, struct cfs_bandwidth, slack_timer); | |
77a4d1a1 | 4887 | |
029632fb PZ |
4888 | do_sched_cfs_slack_timer(cfs_b); |
4889 | ||
4890 | return HRTIMER_NORESTART; | |
4891 | } | |
4892 | ||
4893 | static enum hrtimer_restart sched_cfs_period_timer(struct hrtimer *timer) | |
4894 | { | |
4895 | struct cfs_bandwidth *cfs_b = | |
4896 | container_of(timer, struct cfs_bandwidth, period_timer); | |
c0ad4aa4 | 4897 | unsigned long flags; |
029632fb PZ |
4898 | int overrun; |
4899 | int idle = 0; | |
4900 | ||
c0ad4aa4 | 4901 | raw_spin_lock_irqsave(&cfs_b->lock, flags); |
029632fb | 4902 | for (;;) { |
77a4d1a1 | 4903 | overrun = hrtimer_forward_now(timer, cfs_b->period); |
029632fb PZ |
4904 | if (!overrun) |
4905 | break; | |
4906 | ||
c0ad4aa4 | 4907 | idle = do_sched_cfs_period_timer(cfs_b, overrun, flags); |
029632fb | 4908 | } |
4cfafd30 PZ |
4909 | if (idle) |
4910 | cfs_b->period_active = 0; | |
c0ad4aa4 | 4911 | raw_spin_unlock_irqrestore(&cfs_b->lock, flags); |
029632fb PZ |
4912 | |
4913 | return idle ? HRTIMER_NORESTART : HRTIMER_RESTART; | |
4914 | } | |
4915 | ||
4916 | void init_cfs_bandwidth(struct cfs_bandwidth *cfs_b) | |
4917 | { | |
4918 | raw_spin_lock_init(&cfs_b->lock); | |
4919 | cfs_b->runtime = 0; | |
4920 | cfs_b->quota = RUNTIME_INF; | |
4921 | cfs_b->period = ns_to_ktime(default_cfs_period()); | |
4922 | ||
4923 | INIT_LIST_HEAD(&cfs_b->throttled_cfs_rq); | |
4cfafd30 | 4924 | hrtimer_init(&cfs_b->period_timer, CLOCK_MONOTONIC, HRTIMER_MODE_ABS_PINNED); |
029632fb PZ |
4925 | cfs_b->period_timer.function = sched_cfs_period_timer; |
4926 | hrtimer_init(&cfs_b->slack_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL); | |
4927 | cfs_b->slack_timer.function = sched_cfs_slack_timer; | |
baa9be4f | 4928 | cfs_b->distribute_running = 0; |
029632fb PZ |
4929 | } |
4930 | ||
4931 | static void init_cfs_rq_runtime(struct cfs_rq *cfs_rq) | |
4932 | { | |
4933 | cfs_rq->runtime_enabled = 0; | |
4934 | INIT_LIST_HEAD(&cfs_rq->throttled_list); | |
4935 | } | |
4936 | ||
77a4d1a1 | 4937 | void start_cfs_bandwidth(struct cfs_bandwidth *cfs_b) |
029632fb | 4938 | { |
f1d1be8a XP |
4939 | u64 overrun; |
4940 | ||
4cfafd30 | 4941 | lockdep_assert_held(&cfs_b->lock); |
029632fb | 4942 | |
f1d1be8a XP |
4943 | if (cfs_b->period_active) |
4944 | return; | |
4945 | ||
4946 | cfs_b->period_active = 1; | |
4947 | overrun = hrtimer_forward_now(&cfs_b->period_timer, cfs_b->period); | |
4948 | cfs_b->runtime_expires += (overrun + 1) * ktime_to_ns(cfs_b->period); | |
4949 | cfs_b->expires_seq++; | |
4950 | hrtimer_start_expires(&cfs_b->period_timer, HRTIMER_MODE_ABS_PINNED); | |
029632fb PZ |
4951 | } |
4952 | ||
4953 | static void destroy_cfs_bandwidth(struct cfs_bandwidth *cfs_b) | |
4954 | { | |
7f1a169b TH |
4955 | /* init_cfs_bandwidth() was not called */ |
4956 | if (!cfs_b->throttled_cfs_rq.next) | |
4957 | return; | |
4958 | ||
029632fb PZ |
4959 | hrtimer_cancel(&cfs_b->period_timer); |
4960 | hrtimer_cancel(&cfs_b->slack_timer); | |
4961 | } | |
4962 | ||
502ce005 | 4963 | /* |
97fb7a0a | 4964 | * Both these CPU hotplug callbacks race against unregister_fair_sched_group() |
502ce005 PZ |
4965 | * |
4966 | * The race is harmless, since modifying bandwidth settings of unhooked group | |
4967 | * bits doesn't do much. | |
4968 | */ | |
4969 | ||
4970 | /* cpu online calback */ | |
0e59bdae KT |
4971 | static void __maybe_unused update_runtime_enabled(struct rq *rq) |
4972 | { | |
502ce005 | 4973 | struct task_group *tg; |
0e59bdae | 4974 | |
502ce005 PZ |
4975 | lockdep_assert_held(&rq->lock); |
4976 | ||
4977 | rcu_read_lock(); | |
4978 | list_for_each_entry_rcu(tg, &task_groups, list) { | |
4979 | struct cfs_bandwidth *cfs_b = &tg->cfs_bandwidth; | |
4980 | struct cfs_rq *cfs_rq = tg->cfs_rq[cpu_of(rq)]; | |
0e59bdae KT |
4981 | |
4982 | raw_spin_lock(&cfs_b->lock); | |
4983 | cfs_rq->runtime_enabled = cfs_b->quota != RUNTIME_INF; | |
4984 | raw_spin_unlock(&cfs_b->lock); | |
4985 | } | |
502ce005 | 4986 | rcu_read_unlock(); |
0e59bdae KT |
4987 | } |
4988 | ||
502ce005 | 4989 | /* cpu offline callback */ |
38dc3348 | 4990 | static void __maybe_unused unthrottle_offline_cfs_rqs(struct rq *rq) |
029632fb | 4991 | { |
502ce005 PZ |
4992 | struct task_group *tg; |
4993 | ||
4994 | lockdep_assert_held(&rq->lock); | |
4995 | ||
4996 | rcu_read_lock(); | |
4997 | list_for_each_entry_rcu(tg, &task_groups, list) { | |
4998 | struct cfs_rq *cfs_rq = tg->cfs_rq[cpu_of(rq)]; | |
029632fb | 4999 | |
029632fb PZ |
5000 | if (!cfs_rq->runtime_enabled) |
5001 | continue; | |
5002 | ||
5003 | /* | |
5004 | * clock_task is not advancing so we just need to make sure | |
5005 | * there's some valid quota amount | |
5006 | */ | |
51f2176d | 5007 | cfs_rq->runtime_remaining = 1; |
0e59bdae | 5008 | /* |
97fb7a0a | 5009 | * Offline rq is schedulable till CPU is completely disabled |
0e59bdae KT |
5010 | * in take_cpu_down(), so we prevent new cfs throttling here. |
5011 | */ | |
5012 | cfs_rq->runtime_enabled = 0; | |
5013 | ||
029632fb PZ |
5014 | if (cfs_rq_throttled(cfs_rq)) |
5015 | unthrottle_cfs_rq(cfs_rq); | |
5016 | } | |
502ce005 | 5017 | rcu_read_unlock(); |
029632fb PZ |
5018 | } |
5019 | ||
5020 | #else /* CONFIG_CFS_BANDWIDTH */ | |
f6783319 VG |
5021 | |
5022 | static inline bool cfs_bandwidth_used(void) | |
5023 | { | |
5024 | return false; | |
5025 | } | |
5026 | ||
f1b17280 PT |
5027 | static inline u64 cfs_rq_clock_task(struct cfs_rq *cfs_rq) |
5028 | { | |
78becc27 | 5029 | return rq_clock_task(rq_of(cfs_rq)); |
f1b17280 PT |
5030 | } |
5031 | ||
9dbdb155 | 5032 | static void account_cfs_rq_runtime(struct cfs_rq *cfs_rq, u64 delta_exec) {} |
678d5718 | 5033 | static bool check_cfs_rq_runtime(struct cfs_rq *cfs_rq) { return false; } |
d3d9dc33 | 5034 | static void check_enqueue_throttle(struct cfs_rq *cfs_rq) {} |
55e16d30 | 5035 | static inline void sync_throttle(struct task_group *tg, int cpu) {} |
6c16a6dc | 5036 | static __always_inline void return_cfs_rq_runtime(struct cfs_rq *cfs_rq) {} |
85dac906 PT |
5037 | |
5038 | static inline int cfs_rq_throttled(struct cfs_rq *cfs_rq) | |
5039 | { | |
5040 | return 0; | |
5041 | } | |
64660c86 PT |
5042 | |
5043 | static inline int throttled_hierarchy(struct cfs_rq *cfs_rq) | |
5044 | { | |
5045 | return 0; | |
5046 | } | |
5047 | ||
5048 | static inline int throttled_lb_pair(struct task_group *tg, | |
5049 | int src_cpu, int dest_cpu) | |
5050 | { | |
5051 | return 0; | |
5052 | } | |
029632fb PZ |
5053 | |
5054 | void init_cfs_bandwidth(struct cfs_bandwidth *cfs_b) {} | |
5055 | ||
5056 | #ifdef CONFIG_FAIR_GROUP_SCHED | |
5057 | static void init_cfs_rq_runtime(struct cfs_rq *cfs_rq) {} | |
ab84d31e PT |
5058 | #endif |
5059 | ||
029632fb PZ |
5060 | static inline struct cfs_bandwidth *tg_cfs_bandwidth(struct task_group *tg) |
5061 | { | |
5062 | return NULL; | |
5063 | } | |
5064 | static inline void destroy_cfs_bandwidth(struct cfs_bandwidth *cfs_b) {} | |
0e59bdae | 5065 | static inline void update_runtime_enabled(struct rq *rq) {} |
a4c96ae3 | 5066 | static inline void unthrottle_offline_cfs_rqs(struct rq *rq) {} |
029632fb PZ |
5067 | |
5068 | #endif /* CONFIG_CFS_BANDWIDTH */ | |
5069 | ||
bf0f6f24 IM |
5070 | /************************************************** |
5071 | * CFS operations on tasks: | |
5072 | */ | |
5073 | ||
8f4d37ec PZ |
5074 | #ifdef CONFIG_SCHED_HRTICK |
5075 | static void hrtick_start_fair(struct rq *rq, struct task_struct *p) | |
5076 | { | |
8f4d37ec PZ |
5077 | struct sched_entity *se = &p->se; |
5078 | struct cfs_rq *cfs_rq = cfs_rq_of(se); | |
5079 | ||
9148a3a1 | 5080 | SCHED_WARN_ON(task_rq(p) != rq); |
8f4d37ec | 5081 | |
8bf46a39 | 5082 | if (rq->cfs.h_nr_running > 1) { |
8f4d37ec PZ |
5083 | u64 slice = sched_slice(cfs_rq, se); |
5084 | u64 ran = se->sum_exec_runtime - se->prev_sum_exec_runtime; | |
5085 | s64 delta = slice - ran; | |
5086 | ||
5087 | if (delta < 0) { | |
5088 | if (rq->curr == p) | |
8875125e | 5089 | resched_curr(rq); |
8f4d37ec PZ |
5090 | return; |
5091 | } | |
31656519 | 5092 | hrtick_start(rq, delta); |
8f4d37ec PZ |
5093 | } |
5094 | } | |
a4c2f00f PZ |
5095 | |
5096 | /* | |
5097 | * called from enqueue/dequeue and updates the hrtick when the | |
5098 | * current task is from our class and nr_running is low enough | |
5099 | * to matter. | |
5100 | */ | |
5101 | static void hrtick_update(struct rq *rq) | |
5102 | { | |
5103 | struct task_struct *curr = rq->curr; | |
5104 | ||
b39e66ea | 5105 | if (!hrtick_enabled(rq) || curr->sched_class != &fair_sched_class) |
a4c2f00f PZ |
5106 | return; |
5107 | ||
5108 | if (cfs_rq_of(&curr->se)->nr_running < sched_nr_latency) | |
5109 | hrtick_start_fair(rq, curr); | |
5110 | } | |
55e12e5e | 5111 | #else /* !CONFIG_SCHED_HRTICK */ |
8f4d37ec PZ |
5112 | static inline void |
5113 | hrtick_start_fair(struct rq *rq, struct task_struct *p) | |
5114 | { | |
5115 | } | |
a4c2f00f PZ |
5116 | |
5117 | static inline void hrtick_update(struct rq *rq) | |
5118 | { | |
5119 | } | |
8f4d37ec PZ |
5120 | #endif |
5121 | ||
2802bf3c MR |
5122 | #ifdef CONFIG_SMP |
5123 | static inline unsigned long cpu_util(int cpu); | |
5124 | static unsigned long capacity_of(int cpu); | |
5125 | ||
5126 | static inline bool cpu_overutilized(int cpu) | |
5127 | { | |
5128 | return (capacity_of(cpu) * 1024) < (cpu_util(cpu) * capacity_margin); | |
5129 | } | |
5130 | ||
5131 | static inline void update_overutilized_status(struct rq *rq) | |
5132 | { | |
5133 | if (!READ_ONCE(rq->rd->overutilized) && cpu_overutilized(rq->cpu)) | |
5134 | WRITE_ONCE(rq->rd->overutilized, SG_OVERUTILIZED); | |
5135 | } | |
5136 | #else | |
5137 | static inline void update_overutilized_status(struct rq *rq) { } | |
5138 | #endif | |
5139 | ||
bf0f6f24 IM |
5140 | /* |
5141 | * The enqueue_task method is called before nr_running is | |
5142 | * increased. Here we update the fair scheduling stats and | |
5143 | * then put the task into the rbtree: | |
5144 | */ | |
ea87bb78 | 5145 | static void |
371fd7e7 | 5146 | enqueue_task_fair(struct rq *rq, struct task_struct *p, int flags) |
bf0f6f24 IM |
5147 | { |
5148 | struct cfs_rq *cfs_rq; | |
62fb1851 | 5149 | struct sched_entity *se = &p->se; |
bf0f6f24 | 5150 | |
2539fc82 PB |
5151 | /* |
5152 | * The code below (indirectly) updates schedutil which looks at | |
5153 | * the cfs_rq utilization to select a frequency. | |
5154 | * Let's add the task's estimated utilization to the cfs_rq's | |
5155 | * estimated utilization, before we update schedutil. | |
5156 | */ | |
5157 | util_est_enqueue(&rq->cfs, p); | |
5158 | ||
8c34ab19 RW |
5159 | /* |
5160 | * If in_iowait is set, the code below may not trigger any cpufreq | |
5161 | * utilization updates, so do it here explicitly with the IOWAIT flag | |
5162 | * passed. | |
5163 | */ | |
5164 | if (p->in_iowait) | |
674e7541 | 5165 | cpufreq_update_util(rq, SCHED_CPUFREQ_IOWAIT); |
8c34ab19 | 5166 | |
bf0f6f24 | 5167 | for_each_sched_entity(se) { |
62fb1851 | 5168 | if (se->on_rq) |
bf0f6f24 IM |
5169 | break; |
5170 | cfs_rq = cfs_rq_of(se); | |
88ec22d3 | 5171 | enqueue_entity(cfs_rq, se, flags); |
85dac906 PT |
5172 | |
5173 | /* | |
5174 | * end evaluation on encountering a throttled cfs_rq | |
5175 | * | |
5176 | * note: in the case of encountering a throttled cfs_rq we will | |
5177 | * post the final h_nr_running increment below. | |
e210bffd | 5178 | */ |
85dac906 PT |
5179 | if (cfs_rq_throttled(cfs_rq)) |
5180 | break; | |
953bfcd1 | 5181 | cfs_rq->h_nr_running++; |
85dac906 | 5182 | |
88ec22d3 | 5183 | flags = ENQUEUE_WAKEUP; |
bf0f6f24 | 5184 | } |
8f4d37ec | 5185 | |
2069dd75 | 5186 | for_each_sched_entity(se) { |
0f317143 | 5187 | cfs_rq = cfs_rq_of(se); |
953bfcd1 | 5188 | cfs_rq->h_nr_running++; |
2069dd75 | 5189 | |
85dac906 PT |
5190 | if (cfs_rq_throttled(cfs_rq)) |
5191 | break; | |
5192 | ||
88c0616e | 5193 | update_load_avg(cfs_rq, se, UPDATE_TG); |
1ea6c46a | 5194 | update_cfs_group(se); |
2069dd75 PZ |
5195 | } |
5196 | ||
2802bf3c | 5197 | if (!se) { |
72465447 | 5198 | add_nr_running(rq, 1); |
2802bf3c MR |
5199 | /* |
5200 | * Since new tasks are assigned an initial util_avg equal to | |
5201 | * half of the spare capacity of their CPU, tiny tasks have the | |
5202 | * ability to cross the overutilized threshold, which will | |
5203 | * result in the load balancer ruining all the task placement | |
5204 | * done by EAS. As a way to mitigate that effect, do not account | |
5205 | * for the first enqueue operation of new tasks during the | |
5206 | * overutilized flag detection. | |
5207 | * | |
5208 | * A better way of solving this problem would be to wait for | |
5209 | * the PELT signals of tasks to converge before taking them | |
5210 | * into account, but that is not straightforward to implement, | |
5211 | * and the following generally works well enough in practice. | |
5212 | */ | |
5213 | if (flags & ENQUEUE_WAKEUP) | |
5214 | update_overutilized_status(rq); | |
5215 | ||
5216 | } | |
cd126afe | 5217 | |
f6783319 VG |
5218 | if (cfs_bandwidth_used()) { |
5219 | /* | |
5220 | * When bandwidth control is enabled; the cfs_rq_throttled() | |
5221 | * breaks in the above iteration can result in incomplete | |
5222 | * leaf list maintenance, resulting in triggering the assertion | |
5223 | * below. | |
5224 | */ | |
5225 | for_each_sched_entity(se) { | |
5226 | cfs_rq = cfs_rq_of(se); | |
5227 | ||
5228 | if (list_add_leaf_cfs_rq(cfs_rq)) | |
5229 | break; | |
5230 | } | |
5231 | } | |
5232 | ||
5d299eab PZ |
5233 | assert_list_leaf_cfs_rq(rq); |
5234 | ||
a4c2f00f | 5235 | hrtick_update(rq); |
bf0f6f24 IM |
5236 | } |
5237 | ||
2f36825b VP |
5238 | static void set_next_buddy(struct sched_entity *se); |
5239 | ||
bf0f6f24 IM |
5240 | /* |
5241 | * The dequeue_task method is called before nr_running is | |
5242 | * decreased. We remove the task from the rbtree and | |
5243 | * update the fair scheduling stats: | |
5244 | */ | |
371fd7e7 | 5245 | static void dequeue_task_fair(struct rq *rq, struct task_struct *p, int flags) |
bf0f6f24 IM |
5246 | { |
5247 | struct cfs_rq *cfs_rq; | |
62fb1851 | 5248 | struct sched_entity *se = &p->se; |
2f36825b | 5249 | int task_sleep = flags & DEQUEUE_SLEEP; |
bf0f6f24 IM |
5250 | |
5251 | for_each_sched_entity(se) { | |
5252 | cfs_rq = cfs_rq_of(se); | |
371fd7e7 | 5253 | dequeue_entity(cfs_rq, se, flags); |
85dac906 PT |
5254 | |
5255 | /* | |
5256 | * end evaluation on encountering a throttled cfs_rq | |
5257 | * | |
5258 | * note: in the case of encountering a throttled cfs_rq we will | |
5259 | * post the final h_nr_running decrement below. | |
5260 | */ | |
5261 | if (cfs_rq_throttled(cfs_rq)) | |
5262 | break; | |
953bfcd1 | 5263 | cfs_rq->h_nr_running--; |
2069dd75 | 5264 | |
bf0f6f24 | 5265 | /* Don't dequeue parent if it has other entities besides us */ |
2f36825b | 5266 | if (cfs_rq->load.weight) { |
754bd598 KK |
5267 | /* Avoid re-evaluating load for this entity: */ |
5268 | se = parent_entity(se); | |
2f36825b VP |
5269 | /* |
5270 | * Bias pick_next to pick a task from this cfs_rq, as | |
5271 | * p is sleeping when it is within its sched_slice. | |
5272 | */ | |
754bd598 KK |
5273 | if (task_sleep && se && !throttled_hierarchy(cfs_rq)) |
5274 | set_next_buddy(se); | |
bf0f6f24 | 5275 | break; |
2f36825b | 5276 | } |
371fd7e7 | 5277 | flags |= DEQUEUE_SLEEP; |
bf0f6f24 | 5278 | } |
8f4d37ec | 5279 | |
2069dd75 | 5280 | for_each_sched_entity(se) { |
0f317143 | 5281 | cfs_rq = cfs_rq_of(se); |
953bfcd1 | 5282 | cfs_rq->h_nr_running--; |
2069dd75 | 5283 | |
85dac906 PT |
5284 | if (cfs_rq_throttled(cfs_rq)) |
5285 | break; | |
5286 | ||
88c0616e | 5287 | update_load_avg(cfs_rq, se, UPDATE_TG); |
1ea6c46a | 5288 | update_cfs_group(se); |
2069dd75 PZ |
5289 | } |
5290 | ||
cd126afe | 5291 | if (!se) |
72465447 | 5292 | sub_nr_running(rq, 1); |
cd126afe | 5293 | |
7f65ea42 | 5294 | util_est_dequeue(&rq->cfs, p, task_sleep); |
a4c2f00f | 5295 | hrtick_update(rq); |
bf0f6f24 IM |
5296 | } |
5297 | ||
e7693a36 | 5298 | #ifdef CONFIG_SMP |
10e2f1ac PZ |
5299 | |
5300 | /* Working cpumask for: load_balance, load_balance_newidle. */ | |
5301 | DEFINE_PER_CPU(cpumask_var_t, load_balance_mask); | |
5302 | DEFINE_PER_CPU(cpumask_var_t, select_idle_mask); | |
5303 | ||
9fd81dd5 | 5304 | #ifdef CONFIG_NO_HZ_COMMON |
3289bdb4 PZ |
5305 | /* |
5306 | * per rq 'load' arrray crap; XXX kill this. | |
5307 | */ | |
5308 | ||
5309 | /* | |
d937cdc5 | 5310 | * The exact cpuload calculated at every tick would be: |
3289bdb4 | 5311 | * |
d937cdc5 PZ |
5312 | * load' = (1 - 1/2^i) * load + (1/2^i) * cur_load |
5313 | * | |
97fb7a0a IM |
5314 | * If a CPU misses updates for n ticks (as it was idle) and update gets |
5315 | * called on the n+1-th tick when CPU may be busy, then we have: | |
d937cdc5 PZ |
5316 | * |
5317 | * load_n = (1 - 1/2^i)^n * load_0 | |
5318 | * load_n+1 = (1 - 1/2^i) * load_n + (1/2^i) * cur_load | |
3289bdb4 PZ |
5319 | * |
5320 | * decay_load_missed() below does efficient calculation of | |
3289bdb4 | 5321 | * |
d937cdc5 PZ |
5322 | * load' = (1 - 1/2^i)^n * load |
5323 | * | |
5324 | * Because x^(n+m) := x^n * x^m we can decompose any x^n in power-of-2 factors. | |
5325 | * This allows us to precompute the above in said factors, thereby allowing the | |
5326 | * reduction of an arbitrary n in O(log_2 n) steps. (See also | |
5327 | * fixed_power_int()) | |
3289bdb4 | 5328 | * |
d937cdc5 | 5329 | * The calculation is approximated on a 128 point scale. |
3289bdb4 PZ |
5330 | */ |
5331 | #define DEGRADE_SHIFT 7 | |
d937cdc5 PZ |
5332 | |
5333 | static const u8 degrade_zero_ticks[CPU_LOAD_IDX_MAX] = {0, 8, 32, 64, 128}; | |
5334 | static const u8 degrade_factor[CPU_LOAD_IDX_MAX][DEGRADE_SHIFT + 1] = { | |
5335 | { 0, 0, 0, 0, 0, 0, 0, 0 }, | |
5336 | { 64, 32, 8, 0, 0, 0, 0, 0 }, | |
5337 | { 96, 72, 40, 12, 1, 0, 0, 0 }, | |
5338 | { 112, 98, 75, 43, 15, 1, 0, 0 }, | |
5339 | { 120, 112, 98, 76, 45, 16, 2, 0 } | |
5340 | }; | |
3289bdb4 PZ |
5341 | |
5342 | /* | |
5343 | * Update cpu_load for any missed ticks, due to tickless idle. The backlog | |
5344 | * would be when CPU is idle and so we just decay the old load without | |
5345 | * adding any new load. | |
5346 | */ | |
5347 | static unsigned long | |
5348 | decay_load_missed(unsigned long load, unsigned long missed_updates, int idx) | |
5349 | { | |
5350 | int j = 0; | |
5351 | ||
5352 | if (!missed_updates) | |
5353 | return load; | |
5354 | ||
5355 | if (missed_updates >= degrade_zero_ticks[idx]) | |
5356 | return 0; | |
5357 | ||
5358 | if (idx == 1) | |
5359 | return load >> missed_updates; | |
5360 | ||
5361 | while (missed_updates) { | |
5362 | if (missed_updates % 2) | |
5363 | load = (load * degrade_factor[idx][j]) >> DEGRADE_SHIFT; | |
5364 | ||
5365 | missed_updates >>= 1; | |
5366 | j++; | |
5367 | } | |
5368 | return load; | |
5369 | } | |
e022e0d3 PZ |
5370 | |
5371 | static struct { | |
5372 | cpumask_var_t idle_cpus_mask; | |
5373 | atomic_t nr_cpus; | |
f643ea22 | 5374 | int has_blocked; /* Idle CPUS has blocked load */ |
e022e0d3 | 5375 | unsigned long next_balance; /* in jiffy units */ |
f643ea22 | 5376 | unsigned long next_blocked; /* Next update of blocked load in jiffies */ |
e022e0d3 PZ |
5377 | } nohz ____cacheline_aligned; |
5378 | ||
9fd81dd5 | 5379 | #endif /* CONFIG_NO_HZ_COMMON */ |
3289bdb4 | 5380 | |
59543275 | 5381 | /** |
cee1afce | 5382 | * __cpu_load_update - update the rq->cpu_load[] statistics |
59543275 BP |
5383 | * @this_rq: The rq to update statistics for |
5384 | * @this_load: The current load | |
5385 | * @pending_updates: The number of missed updates | |
59543275 | 5386 | * |
3289bdb4 | 5387 | * Update rq->cpu_load[] statistics. This function is usually called every |
59543275 BP |
5388 | * scheduler tick (TICK_NSEC). |
5389 | * | |
5390 | * This function computes a decaying average: | |
5391 | * | |
5392 | * load[i]' = (1 - 1/2^i) * load[i] + (1/2^i) * load | |
5393 | * | |
5394 | * Because of NOHZ it might not get called on every tick which gives need for | |
5395 | * the @pending_updates argument. | |
5396 | * | |
5397 | * load[i]_n = (1 - 1/2^i) * load[i]_n-1 + (1/2^i) * load_n-1 | |
5398 | * = A * load[i]_n-1 + B ; A := (1 - 1/2^i), B := (1/2^i) * load | |
5399 | * = A * (A * load[i]_n-2 + B) + B | |
5400 | * = A * (A * (A * load[i]_n-3 + B) + B) + B | |
5401 | * = A^3 * load[i]_n-3 + (A^2 + A + 1) * B | |
5402 | * = A^n * load[i]_0 + (A^(n-1) + A^(n-2) + ... + 1) * B | |
5403 | * = A^n * load[i]_0 + ((1 - A^n) / (1 - A)) * B | |
5404 | * = (1 - 1/2^i)^n * (load[i]_0 - load) + load | |
5405 | * | |
5406 | * In the above we've assumed load_n := load, which is true for NOHZ_FULL as | |
5407 | * any change in load would have resulted in the tick being turned back on. | |
5408 | * | |
5409 | * For regular NOHZ, this reduces to: | |
5410 | * | |
5411 | * load[i]_n = (1 - 1/2^i)^n * load[i]_0 | |
5412 | * | |
5413 | * see decay_load_misses(). For NOHZ_FULL we get to subtract and add the extra | |
1f41906a | 5414 | * term. |
3289bdb4 | 5415 | */ |
1f41906a FW |
5416 | static void cpu_load_update(struct rq *this_rq, unsigned long this_load, |
5417 | unsigned long pending_updates) | |
3289bdb4 | 5418 | { |
9fd81dd5 | 5419 | unsigned long __maybe_unused tickless_load = this_rq->cpu_load[0]; |
3289bdb4 PZ |
5420 | int i, scale; |
5421 | ||
5422 | this_rq->nr_load_updates++; | |
5423 | ||
5424 | /* Update our load: */ | |
5425 | this_rq->cpu_load[0] = this_load; /* Fasttrack for idx 0 */ | |
5426 | for (i = 1, scale = 2; i < CPU_LOAD_IDX_MAX; i++, scale += scale) { | |
5427 | unsigned long old_load, new_load; | |
5428 | ||
5429 | /* scale is effectively 1 << i now, and >> i divides by scale */ | |
5430 | ||
7400d3bb | 5431 | old_load = this_rq->cpu_load[i]; |
9fd81dd5 | 5432 | #ifdef CONFIG_NO_HZ_COMMON |
3289bdb4 | 5433 | old_load = decay_load_missed(old_load, pending_updates - 1, i); |
7400d3bb BP |
5434 | if (tickless_load) { |
5435 | old_load -= decay_load_missed(tickless_load, pending_updates - 1, i); | |
5436 | /* | |
5437 | * old_load can never be a negative value because a | |
5438 | * decayed tickless_load cannot be greater than the | |
5439 | * original tickless_load. | |
5440 | */ | |
5441 | old_load += tickless_load; | |
5442 | } | |
9fd81dd5 | 5443 | #endif |
3289bdb4 PZ |
5444 | new_load = this_load; |
5445 | /* | |
5446 | * Round up the averaging division if load is increasing. This | |
5447 | * prevents us from getting stuck on 9 if the load is 10, for | |
5448 | * example. | |
5449 | */ | |
5450 | if (new_load > old_load) | |
5451 | new_load += scale - 1; | |
5452 | ||
5453 | this_rq->cpu_load[i] = (old_load * (scale - 1) + new_load) >> i; | |
5454 | } | |
3289bdb4 PZ |
5455 | } |
5456 | ||
7ea241af | 5457 | /* Used instead of source_load when we know the type == 0 */ |
c7132dd6 | 5458 | static unsigned long weighted_cpuload(struct rq *rq) |
7ea241af | 5459 | { |
c7132dd6 | 5460 | return cfs_rq_runnable_load_avg(&rq->cfs); |
7ea241af YD |
5461 | } |
5462 | ||
3289bdb4 | 5463 | #ifdef CONFIG_NO_HZ_COMMON |
1f41906a FW |
5464 | /* |
5465 | * There is no sane way to deal with nohz on smp when using jiffies because the | |
97fb7a0a | 5466 | * CPU doing the jiffies update might drift wrt the CPU doing the jiffy reading |
1f41906a FW |
5467 | * causing off-by-one errors in observed deltas; {0,2} instead of {1,1}. |
5468 | * | |
5469 | * Therefore we need to avoid the delta approach from the regular tick when | |
5470 | * possible since that would seriously skew the load calculation. This is why we | |
5471 | * use cpu_load_update_periodic() for CPUs out of nohz. However we'll rely on | |
5472 | * jiffies deltas for updates happening while in nohz mode (idle ticks, idle | |
5473 | * loop exit, nohz_idle_balance, nohz full exit...) | |
5474 | * | |
5475 | * This means we might still be one tick off for nohz periods. | |
5476 | */ | |
5477 | ||
5478 | static void cpu_load_update_nohz(struct rq *this_rq, | |
5479 | unsigned long curr_jiffies, | |
5480 | unsigned long load) | |
be68a682 FW |
5481 | { |
5482 | unsigned long pending_updates; | |
5483 | ||
5484 | pending_updates = curr_jiffies - this_rq->last_load_update_tick; | |
5485 | if (pending_updates) { | |
5486 | this_rq->last_load_update_tick = curr_jiffies; | |
5487 | /* | |
5488 | * In the regular NOHZ case, we were idle, this means load 0. | |
5489 | * In the NOHZ_FULL case, we were non-idle, we should consider | |
5490 | * its weighted load. | |
5491 | */ | |
1f41906a | 5492 | cpu_load_update(this_rq, load, pending_updates); |
be68a682 FW |
5493 | } |
5494 | } | |
5495 | ||
3289bdb4 PZ |
5496 | /* |
5497 | * Called from nohz_idle_balance() to update the load ratings before doing the | |
5498 | * idle balance. | |
5499 | */ | |
cee1afce | 5500 | static void cpu_load_update_idle(struct rq *this_rq) |
3289bdb4 | 5501 | { |
3289bdb4 PZ |
5502 | /* |
5503 | * bail if there's load or we're actually up-to-date. | |
5504 | */ | |
c7132dd6 | 5505 | if (weighted_cpuload(this_rq)) |
3289bdb4 PZ |
5506 | return; |
5507 | ||
1f41906a | 5508 | cpu_load_update_nohz(this_rq, READ_ONCE(jiffies), 0); |
3289bdb4 PZ |
5509 | } |
5510 | ||
5511 | /* | |
1f41906a FW |
5512 | * Record CPU load on nohz entry so we know the tickless load to account |
5513 | * on nohz exit. cpu_load[0] happens then to be updated more frequently | |
5514 | * than other cpu_load[idx] but it should be fine as cpu_load readers | |
5515 | * shouldn't rely into synchronized cpu_load[*] updates. | |
3289bdb4 | 5516 | */ |
1f41906a | 5517 | void cpu_load_update_nohz_start(void) |
3289bdb4 PZ |
5518 | { |
5519 | struct rq *this_rq = this_rq(); | |
1f41906a FW |
5520 | |
5521 | /* | |
5522 | * This is all lockless but should be fine. If weighted_cpuload changes | |
5523 | * concurrently we'll exit nohz. And cpu_load write can race with | |
5524 | * cpu_load_update_idle() but both updater would be writing the same. | |
5525 | */ | |
c7132dd6 | 5526 | this_rq->cpu_load[0] = weighted_cpuload(this_rq); |
1f41906a FW |
5527 | } |
5528 | ||
5529 | /* | |
5530 | * Account the tickless load in the end of a nohz frame. | |
5531 | */ | |
5532 | void cpu_load_update_nohz_stop(void) | |
5533 | { | |
316c1608 | 5534 | unsigned long curr_jiffies = READ_ONCE(jiffies); |
1f41906a FW |
5535 | struct rq *this_rq = this_rq(); |
5536 | unsigned long load; | |
8a8c69c3 | 5537 | struct rq_flags rf; |
3289bdb4 PZ |
5538 | |
5539 | if (curr_jiffies == this_rq->last_load_update_tick) | |
5540 | return; | |
5541 | ||
c7132dd6 | 5542 | load = weighted_cpuload(this_rq); |
8a8c69c3 | 5543 | rq_lock(this_rq, &rf); |
b52fad2d | 5544 | update_rq_clock(this_rq); |
1f41906a | 5545 | cpu_load_update_nohz(this_rq, curr_jiffies, load); |
8a8c69c3 | 5546 | rq_unlock(this_rq, &rf); |
3289bdb4 | 5547 | } |
1f41906a FW |
5548 | #else /* !CONFIG_NO_HZ_COMMON */ |
5549 | static inline void cpu_load_update_nohz(struct rq *this_rq, | |
5550 | unsigned long curr_jiffies, | |
5551 | unsigned long load) { } | |
5552 | #endif /* CONFIG_NO_HZ_COMMON */ | |
5553 | ||
5554 | static void cpu_load_update_periodic(struct rq *this_rq, unsigned long load) | |
5555 | { | |
9fd81dd5 | 5556 | #ifdef CONFIG_NO_HZ_COMMON |
1f41906a FW |
5557 | /* See the mess around cpu_load_update_nohz(). */ |
5558 | this_rq->last_load_update_tick = READ_ONCE(jiffies); | |
9fd81dd5 | 5559 | #endif |
1f41906a FW |
5560 | cpu_load_update(this_rq, load, 1); |
5561 | } | |
3289bdb4 PZ |
5562 | |
5563 | /* | |
5564 | * Called from scheduler_tick() | |
5565 | */ | |
cee1afce | 5566 | void cpu_load_update_active(struct rq *this_rq) |
3289bdb4 | 5567 | { |
c7132dd6 | 5568 | unsigned long load = weighted_cpuload(this_rq); |
1f41906a FW |
5569 | |
5570 | if (tick_nohz_tick_stopped()) | |
5571 | cpu_load_update_nohz(this_rq, READ_ONCE(jiffies), load); | |
5572 | else | |
5573 | cpu_load_update_periodic(this_rq, load); | |
3289bdb4 PZ |
5574 | } |
5575 | ||
029632fb | 5576 | /* |
97fb7a0a | 5577 | * Return a low guess at the load of a migration-source CPU weighted |
029632fb PZ |
5578 | * according to the scheduling class and "nice" value. |
5579 | * | |
5580 | * We want to under-estimate the load of migration sources, to | |
5581 | * balance conservatively. | |
5582 | */ | |
5583 | static unsigned long source_load(int cpu, int type) | |
5584 | { | |
5585 | struct rq *rq = cpu_rq(cpu); | |
c7132dd6 | 5586 | unsigned long total = weighted_cpuload(rq); |
029632fb PZ |
5587 | |
5588 | if (type == 0 || !sched_feat(LB_BIAS)) | |
5589 | return total; | |
5590 | ||
5591 | return min(rq->cpu_load[type-1], total); | |
5592 | } | |
5593 | ||
5594 | /* | |
97fb7a0a | 5595 | * Return a high guess at the load of a migration-target CPU weighted |
029632fb PZ |
5596 | * according to the scheduling class and "nice" value. |
5597 | */ | |
5598 | static unsigned long target_load(int cpu, int type) | |
5599 | { | |
5600 | struct rq *rq = cpu_rq(cpu); | |
c7132dd6 | 5601 | unsigned long total = weighted_cpuload(rq); |
029632fb PZ |
5602 | |
5603 | if (type == 0 || !sched_feat(LB_BIAS)) | |
5604 | return total; | |
5605 | ||
5606 | return max(rq->cpu_load[type-1], total); | |
5607 | } | |
5608 | ||
ced549fa | 5609 | static unsigned long capacity_of(int cpu) |
029632fb | 5610 | { |
ced549fa | 5611 | return cpu_rq(cpu)->cpu_capacity; |
029632fb PZ |
5612 | } |
5613 | ||
5614 | static unsigned long cpu_avg_load_per_task(int cpu) | |
5615 | { | |
5616 | struct rq *rq = cpu_rq(cpu); | |
316c1608 | 5617 | unsigned long nr_running = READ_ONCE(rq->cfs.h_nr_running); |
c7132dd6 | 5618 | unsigned long load_avg = weighted_cpuload(rq); |
029632fb PZ |
5619 | |
5620 | if (nr_running) | |
b92486cb | 5621 | return load_avg / nr_running; |
029632fb PZ |
5622 | |
5623 | return 0; | |
5624 | } | |
5625 | ||
c58d25f3 PZ |
5626 | static void record_wakee(struct task_struct *p) |
5627 | { | |
5628 | /* | |
5629 | * Only decay a single time; tasks that have less then 1 wakeup per | |
5630 | * jiffy will not have built up many flips. | |
5631 | */ | |
5632 | if (time_after(jiffies, current->wakee_flip_decay_ts + HZ)) { | |
5633 | current->wakee_flips >>= 1; | |
5634 | current->wakee_flip_decay_ts = jiffies; | |
5635 | } | |
5636 | ||
5637 | if (current->last_wakee != p) { | |
5638 | current->last_wakee = p; | |
5639 | current->wakee_flips++; | |
5640 | } | |
5641 | } | |
5642 | ||
63b0e9ed MG |
5643 | /* |
5644 | * Detect M:N waker/wakee relationships via a switching-frequency heuristic. | |
c58d25f3 | 5645 | * |
63b0e9ed | 5646 | * A waker of many should wake a different task than the one last awakened |
c58d25f3 PZ |
5647 | * at a frequency roughly N times higher than one of its wakees. |
5648 | * | |
5649 | * In order to determine whether we should let the load spread vs consolidating | |
5650 | * to shared cache, we look for a minimum 'flip' frequency of llc_size in one | |
5651 | * partner, and a factor of lls_size higher frequency in the other. | |
5652 | * | |
5653 | * With both conditions met, we can be relatively sure that the relationship is | |
5654 | * non-monogamous, with partner count exceeding socket size. | |
5655 | * | |
5656 | * Waker/wakee being client/server, worker/dispatcher, interrupt source or | |
5657 | * whatever is irrelevant, spread criteria is apparent partner count exceeds | |
5658 | * socket size. | |
63b0e9ed | 5659 | */ |
62470419 MW |
5660 | static int wake_wide(struct task_struct *p) |
5661 | { | |
63b0e9ed MG |
5662 | unsigned int master = current->wakee_flips; |
5663 | unsigned int slave = p->wakee_flips; | |
7d9ffa89 | 5664 | int factor = this_cpu_read(sd_llc_size); |
62470419 | 5665 | |
63b0e9ed MG |
5666 | if (master < slave) |
5667 | swap(master, slave); | |
5668 | if (slave < factor || master < slave * factor) | |
5669 | return 0; | |
5670 | return 1; | |
62470419 MW |
5671 | } |
5672 | ||
90001d67 | 5673 | /* |
d153b153 PZ |
5674 | * The purpose of wake_affine() is to quickly determine on which CPU we can run |
5675 | * soonest. For the purpose of speed we only consider the waking and previous | |
5676 | * CPU. | |
90001d67 | 5677 | * |
7332dec0 MG |
5678 | * wake_affine_idle() - only considers 'now', it check if the waking CPU is |
5679 | * cache-affine and is (or will be) idle. | |
f2cdd9cc PZ |
5680 | * |
5681 | * wake_affine_weight() - considers the weight to reflect the average | |
5682 | * scheduling latency of the CPUs. This seems to work | |
5683 | * for the overloaded case. | |
90001d67 | 5684 | */ |
3b76c4a3 | 5685 | static int |
89a55f56 | 5686 | wake_affine_idle(int this_cpu, int prev_cpu, int sync) |
90001d67 | 5687 | { |
7332dec0 MG |
5688 | /* |
5689 | * If this_cpu is idle, it implies the wakeup is from interrupt | |
5690 | * context. Only allow the move if cache is shared. Otherwise an | |
5691 | * interrupt intensive workload could force all tasks onto one | |
5692 | * node depending on the IO topology or IRQ affinity settings. | |
806486c3 MG |
5693 | * |
5694 | * If the prev_cpu is idle and cache affine then avoid a migration. | |
5695 | * There is no guarantee that the cache hot data from an interrupt | |
5696 | * is more important than cache hot data on the prev_cpu and from | |
5697 | * a cpufreq perspective, it's better to have higher utilisation | |
5698 | * on one CPU. | |
7332dec0 | 5699 | */ |
943d355d RJ |
5700 | if (available_idle_cpu(this_cpu) && cpus_share_cache(this_cpu, prev_cpu)) |
5701 | return available_idle_cpu(prev_cpu) ? prev_cpu : this_cpu; | |
90001d67 | 5702 | |
d153b153 | 5703 | if (sync && cpu_rq(this_cpu)->nr_running == 1) |
3b76c4a3 | 5704 | return this_cpu; |
90001d67 | 5705 | |
3b76c4a3 | 5706 | return nr_cpumask_bits; |
90001d67 PZ |
5707 | } |
5708 | ||
3b76c4a3 | 5709 | static int |
f2cdd9cc PZ |
5710 | wake_affine_weight(struct sched_domain *sd, struct task_struct *p, |
5711 | int this_cpu, int prev_cpu, int sync) | |
90001d67 | 5712 | { |
90001d67 PZ |
5713 | s64 this_eff_load, prev_eff_load; |
5714 | unsigned long task_load; | |
5715 | ||
f2cdd9cc | 5716 | this_eff_load = target_load(this_cpu, sd->wake_idx); |
90001d67 | 5717 | |
90001d67 PZ |
5718 | if (sync) { |
5719 | unsigned long current_load = task_h_load(current); | |
5720 | ||
f2cdd9cc | 5721 | if (current_load > this_eff_load) |
3b76c4a3 | 5722 | return this_cpu; |
90001d67 | 5723 | |
f2cdd9cc | 5724 | this_eff_load -= current_load; |
90001d67 PZ |
5725 | } |
5726 | ||
90001d67 PZ |
5727 | task_load = task_h_load(p); |
5728 | ||
f2cdd9cc PZ |
5729 | this_eff_load += task_load; |
5730 | if (sched_feat(WA_BIAS)) | |
5731 | this_eff_load *= 100; | |
5732 | this_eff_load *= capacity_of(prev_cpu); | |
90001d67 | 5733 | |
eeb60398 | 5734 | prev_eff_load = source_load(prev_cpu, sd->wake_idx); |
f2cdd9cc PZ |
5735 | prev_eff_load -= task_load; |
5736 | if (sched_feat(WA_BIAS)) | |
5737 | prev_eff_load *= 100 + (sd->imbalance_pct - 100) / 2; | |
5738 | prev_eff_load *= capacity_of(this_cpu); | |
90001d67 | 5739 | |
082f764a MG |
5740 | /* |
5741 | * If sync, adjust the weight of prev_eff_load such that if | |
5742 | * prev_eff == this_eff that select_idle_sibling() will consider | |
5743 | * stacking the wakee on top of the waker if no other CPU is | |
5744 | * idle. | |
5745 | */ | |
5746 | if (sync) | |
5747 | prev_eff_load += 1; | |
5748 | ||
5749 | return this_eff_load < prev_eff_load ? this_cpu : nr_cpumask_bits; | |
90001d67 PZ |
5750 | } |
5751 | ||
772bd008 | 5752 | static int wake_affine(struct sched_domain *sd, struct task_struct *p, |
7ebb66a1 | 5753 | int this_cpu, int prev_cpu, int sync) |
098fb9db | 5754 | { |
3b76c4a3 | 5755 | int target = nr_cpumask_bits; |
098fb9db | 5756 | |
89a55f56 | 5757 | if (sched_feat(WA_IDLE)) |
3b76c4a3 | 5758 | target = wake_affine_idle(this_cpu, prev_cpu, sync); |
90001d67 | 5759 | |
3b76c4a3 MG |
5760 | if (sched_feat(WA_WEIGHT) && target == nr_cpumask_bits) |
5761 | target = wake_affine_weight(sd, p, this_cpu, prev_cpu, sync); | |
098fb9db | 5762 | |
ae92882e | 5763 | schedstat_inc(p->se.statistics.nr_wakeups_affine_attempts); |
3b76c4a3 MG |
5764 | if (target == nr_cpumask_bits) |
5765 | return prev_cpu; | |
098fb9db | 5766 | |
3b76c4a3 MG |
5767 | schedstat_inc(sd->ttwu_move_affine); |
5768 | schedstat_inc(p->se.statistics.nr_wakeups_affine); | |
5769 | return target; | |
098fb9db IM |
5770 | } |
5771 | ||
c469933e | 5772 | static unsigned long cpu_util_without(int cpu, struct task_struct *p); |
6a0b19c0 | 5773 | |
c469933e | 5774 | static unsigned long capacity_spare_without(int cpu, struct task_struct *p) |
6a0b19c0 | 5775 | { |
c469933e | 5776 | return max_t(long, capacity_of(cpu) - cpu_util_without(cpu, p), 0); |
6a0b19c0 MR |
5777 | } |
5778 | ||
aaee1203 PZ |
5779 | /* |
5780 | * find_idlest_group finds and returns the least busy CPU group within the | |
5781 | * domain. | |
6fee85cc BJ |
5782 | * |
5783 | * Assumes p is allowed on at least one CPU in sd. | |
aaee1203 PZ |
5784 | */ |
5785 | static struct sched_group * | |
78e7ed53 | 5786 | find_idlest_group(struct sched_domain *sd, struct task_struct *p, |
c44f2a02 | 5787 | int this_cpu, int sd_flag) |
e7693a36 | 5788 | { |
b3bd3de6 | 5789 | struct sched_group *idlest = NULL, *group = sd->groups; |
6a0b19c0 | 5790 | struct sched_group *most_spare_sg = NULL; |
0d10ab95 BJ |
5791 | unsigned long min_runnable_load = ULONG_MAX; |
5792 | unsigned long this_runnable_load = ULONG_MAX; | |
5793 | unsigned long min_avg_load = ULONG_MAX, this_avg_load = ULONG_MAX; | |
6a0b19c0 | 5794 | unsigned long most_spare = 0, this_spare = 0; |
c44f2a02 | 5795 | int load_idx = sd->forkexec_idx; |
6b94780e VG |
5796 | int imbalance_scale = 100 + (sd->imbalance_pct-100)/2; |
5797 | unsigned long imbalance = scale_load_down(NICE_0_LOAD) * | |
5798 | (sd->imbalance_pct-100) / 100; | |
e7693a36 | 5799 | |
c44f2a02 VG |
5800 | if (sd_flag & SD_BALANCE_WAKE) |
5801 | load_idx = sd->wake_idx; | |
5802 | ||
aaee1203 | 5803 | do { |
6b94780e VG |
5804 | unsigned long load, avg_load, runnable_load; |
5805 | unsigned long spare_cap, max_spare_cap; | |
aaee1203 PZ |
5806 | int local_group; |
5807 | int i; | |
e7693a36 | 5808 | |
aaee1203 | 5809 | /* Skip over this group if it has no CPUs allowed */ |
ae4df9d6 | 5810 | if (!cpumask_intersects(sched_group_span(group), |
0c98d344 | 5811 | &p->cpus_allowed)) |
aaee1203 PZ |
5812 | continue; |
5813 | ||
5814 | local_group = cpumask_test_cpu(this_cpu, | |
ae4df9d6 | 5815 | sched_group_span(group)); |
aaee1203 | 5816 | |
6a0b19c0 MR |
5817 | /* |
5818 | * Tally up the load of all CPUs in the group and find | |
5819 | * the group containing the CPU with most spare capacity. | |
5820 | */ | |
aaee1203 | 5821 | avg_load = 0; |
6b94780e | 5822 | runnable_load = 0; |
6a0b19c0 | 5823 | max_spare_cap = 0; |
aaee1203 | 5824 | |
ae4df9d6 | 5825 | for_each_cpu(i, sched_group_span(group)) { |
97fb7a0a | 5826 | /* Bias balancing toward CPUs of our domain */ |
aaee1203 PZ |
5827 | if (local_group) |
5828 | load = source_load(i, load_idx); | |
5829 | else | |
5830 | load = target_load(i, load_idx); | |
5831 | ||
6b94780e VG |
5832 | runnable_load += load; |
5833 | ||
5834 | avg_load += cfs_rq_load_avg(&cpu_rq(i)->cfs); | |
6a0b19c0 | 5835 | |
c469933e | 5836 | spare_cap = capacity_spare_without(i, p); |
6a0b19c0 MR |
5837 | |
5838 | if (spare_cap > max_spare_cap) | |
5839 | max_spare_cap = spare_cap; | |
aaee1203 PZ |
5840 | } |
5841 | ||
63b2ca30 | 5842 | /* Adjust by relative CPU capacity of the group */ |
6b94780e VG |
5843 | avg_load = (avg_load * SCHED_CAPACITY_SCALE) / |
5844 | group->sgc->capacity; | |
5845 | runnable_load = (runnable_load * SCHED_CAPACITY_SCALE) / | |
5846 | group->sgc->capacity; | |
aaee1203 PZ |
5847 | |
5848 | if (local_group) { | |
6b94780e VG |
5849 | this_runnable_load = runnable_load; |
5850 | this_avg_load = avg_load; | |
6a0b19c0 MR |
5851 | this_spare = max_spare_cap; |
5852 | } else { | |
6b94780e VG |
5853 | if (min_runnable_load > (runnable_load + imbalance)) { |
5854 | /* | |
5855 | * The runnable load is significantly smaller | |
97fb7a0a | 5856 | * so we can pick this new CPU: |
6b94780e VG |
5857 | */ |
5858 | min_runnable_load = runnable_load; | |
5859 | min_avg_load = avg_load; | |
5860 | idlest = group; | |
5861 | } else if ((runnable_load < (min_runnable_load + imbalance)) && | |
5862 | (100*min_avg_load > imbalance_scale*avg_load)) { | |
5863 | /* | |
5864 | * The runnable loads are close so take the | |
97fb7a0a | 5865 | * blocked load into account through avg_load: |
6b94780e VG |
5866 | */ |
5867 | min_avg_load = avg_load; | |
6a0b19c0 MR |
5868 | idlest = group; |
5869 | } | |
5870 | ||
5871 | if (most_spare < max_spare_cap) { | |
5872 | most_spare = max_spare_cap; | |
5873 | most_spare_sg = group; | |
5874 | } | |
aaee1203 PZ |
5875 | } |
5876 | } while (group = group->next, group != sd->groups); | |
5877 | ||
6a0b19c0 MR |
5878 | /* |
5879 | * The cross-over point between using spare capacity or least load | |
5880 | * is too conservative for high utilization tasks on partially | |
5881 | * utilized systems if we require spare_capacity > task_util(p), | |
5882 | * so we allow for some task stuffing by using | |
5883 | * spare_capacity > task_util(p)/2. | |
f519a3f1 VG |
5884 | * |
5885 | * Spare capacity can't be used for fork because the utilization has | |
5886 | * not been set yet, we must first select a rq to compute the initial | |
5887 | * utilization. | |
6a0b19c0 | 5888 | */ |
f519a3f1 VG |
5889 | if (sd_flag & SD_BALANCE_FORK) |
5890 | goto skip_spare; | |
5891 | ||
6a0b19c0 | 5892 | if (this_spare > task_util(p) / 2 && |
6b94780e | 5893 | imbalance_scale*this_spare > 100*most_spare) |
6a0b19c0 | 5894 | return NULL; |
6b94780e VG |
5895 | |
5896 | if (most_spare > task_util(p) / 2) | |
6a0b19c0 MR |
5897 | return most_spare_sg; |
5898 | ||
f519a3f1 | 5899 | skip_spare: |
6b94780e VG |
5900 | if (!idlest) |
5901 | return NULL; | |
5902 | ||
2c833627 MG |
5903 | /* |
5904 | * When comparing groups across NUMA domains, it's possible for the | |
5905 | * local domain to be very lightly loaded relative to the remote | |
5906 | * domains but "imbalance" skews the comparison making remote CPUs | |
5907 | * look much more favourable. When considering cross-domain, add | |
5908 | * imbalance to the runnable load on the remote node and consider | |
5909 | * staying local. | |
5910 | */ | |
5911 | if ((sd->flags & SD_NUMA) && | |
5912 | min_runnable_load + imbalance >= this_runnable_load) | |
5913 | return NULL; | |
5914 | ||
6b94780e | 5915 | if (min_runnable_load > (this_runnable_load + imbalance)) |
aaee1203 | 5916 | return NULL; |
6b94780e VG |
5917 | |
5918 | if ((this_runnable_load < (min_runnable_load + imbalance)) && | |
5919 | (100*this_avg_load < imbalance_scale*min_avg_load)) | |
5920 | return NULL; | |
5921 | ||
aaee1203 PZ |
5922 | return idlest; |
5923 | } | |
5924 | ||
5925 | /* | |
97fb7a0a | 5926 | * find_idlest_group_cpu - find the idlest CPU among the CPUs in the group. |
aaee1203 PZ |
5927 | */ |
5928 | static int | |
18bd1b4b | 5929 | find_idlest_group_cpu(struct sched_group *group, struct task_struct *p, int this_cpu) |
aaee1203 PZ |
5930 | { |
5931 | unsigned long load, min_load = ULONG_MAX; | |
83a0a96a NP |
5932 | unsigned int min_exit_latency = UINT_MAX; |
5933 | u64 latest_idle_timestamp = 0; | |
5934 | int least_loaded_cpu = this_cpu; | |
5935 | int shallowest_idle_cpu = -1; | |
aaee1203 PZ |
5936 | int i; |
5937 | ||
eaecf41f MR |
5938 | /* Check if we have any choice: */ |
5939 | if (group->group_weight == 1) | |
ae4df9d6 | 5940 | return cpumask_first(sched_group_span(group)); |
eaecf41f | 5941 | |
aaee1203 | 5942 | /* Traverse only the allowed CPUs */ |
ae4df9d6 | 5943 | for_each_cpu_and(i, sched_group_span(group), &p->cpus_allowed) { |
943d355d | 5944 | if (available_idle_cpu(i)) { |
83a0a96a NP |
5945 | struct rq *rq = cpu_rq(i); |
5946 | struct cpuidle_state *idle = idle_get_state(rq); | |
5947 | if (idle && idle->exit_latency < min_exit_latency) { | |
5948 | /* | |
5949 | * We give priority to a CPU whose idle state | |
5950 | * has the smallest exit latency irrespective | |
5951 | * of any idle timestamp. | |
5952 | */ | |
5953 | min_exit_latency = idle->exit_latency; | |
5954 | latest_idle_timestamp = rq->idle_stamp; | |
5955 | shallowest_idle_cpu = i; | |
5956 | } else if ((!idle || idle->exit_latency == min_exit_latency) && | |
5957 | rq->idle_stamp > latest_idle_timestamp) { | |
5958 | /* | |
5959 | * If equal or no active idle state, then | |
5960 | * the most recently idled CPU might have | |
5961 | * a warmer cache. | |
5962 | */ | |
5963 | latest_idle_timestamp = rq->idle_stamp; | |
5964 | shallowest_idle_cpu = i; | |
5965 | } | |
9f96742a | 5966 | } else if (shallowest_idle_cpu == -1) { |
c7132dd6 | 5967 | load = weighted_cpuload(cpu_rq(i)); |
18cec7e0 | 5968 | if (load < min_load) { |
83a0a96a NP |
5969 | min_load = load; |
5970 | least_loaded_cpu = i; | |
5971 | } | |
e7693a36 GH |
5972 | } |
5973 | } | |
5974 | ||
83a0a96a | 5975 | return shallowest_idle_cpu != -1 ? shallowest_idle_cpu : least_loaded_cpu; |
aaee1203 | 5976 | } |
e7693a36 | 5977 | |
18bd1b4b BJ |
5978 | static inline int find_idlest_cpu(struct sched_domain *sd, struct task_struct *p, |
5979 | int cpu, int prev_cpu, int sd_flag) | |
5980 | { | |
93f50f90 | 5981 | int new_cpu = cpu; |
18bd1b4b | 5982 | |
6fee85cc BJ |
5983 | if (!cpumask_intersects(sched_domain_span(sd), &p->cpus_allowed)) |
5984 | return prev_cpu; | |
5985 | ||
c976a862 | 5986 | /* |
c469933e PB |
5987 | * We need task's util for capacity_spare_without, sync it up to |
5988 | * prev_cpu's last_update_time. | |
c976a862 VK |
5989 | */ |
5990 | if (!(sd_flag & SD_BALANCE_FORK)) | |
5991 | sync_entity_load_avg(&p->se); | |
5992 | ||
18bd1b4b BJ |
5993 | while (sd) { |
5994 | struct sched_group *group; | |
5995 | struct sched_domain *tmp; | |
5996 | int weight; | |
5997 | ||
5998 | if (!(sd->flags & sd_flag)) { | |
5999 | sd = sd->child; | |
6000 | continue; | |
6001 | } | |
6002 | ||
6003 | group = find_idlest_group(sd, p, cpu, sd_flag); | |
6004 | if (!group) { | |
6005 | sd = sd->child; | |
6006 | continue; | |
6007 | } | |
6008 | ||
6009 | new_cpu = find_idlest_group_cpu(group, p, cpu); | |
e90381ea | 6010 | if (new_cpu == cpu) { |
97fb7a0a | 6011 | /* Now try balancing at a lower domain level of 'cpu': */ |
18bd1b4b BJ |
6012 | sd = sd->child; |
6013 | continue; | |
6014 | } | |
6015 | ||
97fb7a0a | 6016 | /* Now try balancing at a lower domain level of 'new_cpu': */ |
18bd1b4b BJ |
6017 | cpu = new_cpu; |
6018 | weight = sd->span_weight; | |
6019 | sd = NULL; | |
6020 | for_each_domain(cpu, tmp) { | |
6021 | if (weight <= tmp->span_weight) | |
6022 | break; | |
6023 | if (tmp->flags & sd_flag) | |
6024 | sd = tmp; | |
6025 | } | |
18bd1b4b BJ |
6026 | } |
6027 | ||
6028 | return new_cpu; | |
6029 | } | |
6030 | ||
10e2f1ac | 6031 | #ifdef CONFIG_SCHED_SMT |
ba2591a5 | 6032 | DEFINE_STATIC_KEY_FALSE(sched_smt_present); |
b284909a | 6033 | EXPORT_SYMBOL_GPL(sched_smt_present); |
10e2f1ac PZ |
6034 | |
6035 | static inline void set_idle_cores(int cpu, int val) | |
6036 | { | |
6037 | struct sched_domain_shared *sds; | |
6038 | ||
6039 | sds = rcu_dereference(per_cpu(sd_llc_shared, cpu)); | |
6040 | if (sds) | |
6041 | WRITE_ONCE(sds->has_idle_cores, val); | |
6042 | } | |
6043 | ||
6044 | static inline bool test_idle_cores(int cpu, bool def) | |
6045 | { | |
6046 | struct sched_domain_shared *sds; | |
6047 | ||
6048 | sds = rcu_dereference(per_cpu(sd_llc_shared, cpu)); | |
6049 | if (sds) | |
6050 | return READ_ONCE(sds->has_idle_cores); | |
6051 | ||
6052 | return def; | |
6053 | } | |
6054 | ||
6055 | /* | |
6056 | * Scans the local SMT mask to see if the entire core is idle, and records this | |
6057 | * information in sd_llc_shared->has_idle_cores. | |
6058 | * | |
6059 | * Since SMT siblings share all cache levels, inspecting this limited remote | |
6060 | * state should be fairly cheap. | |
6061 | */ | |
1b568f0a | 6062 | void __update_idle_core(struct rq *rq) |
10e2f1ac PZ |
6063 | { |
6064 | int core = cpu_of(rq); | |
6065 | int cpu; | |
6066 | ||
6067 | rcu_read_lock(); | |
6068 | if (test_idle_cores(core, true)) | |
6069 | goto unlock; | |
6070 | ||
6071 | for_each_cpu(cpu, cpu_smt_mask(core)) { | |
6072 | if (cpu == core) | |
6073 | continue; | |
6074 | ||
943d355d | 6075 | if (!available_idle_cpu(cpu)) |
10e2f1ac PZ |
6076 | goto unlock; |
6077 | } | |
6078 | ||
6079 | set_idle_cores(core, 1); | |
6080 | unlock: | |
6081 | rcu_read_unlock(); | |
6082 | } | |
6083 | ||
6084 | /* | |
6085 | * Scan the entire LLC domain for idle cores; this dynamically switches off if | |
6086 | * there are no idle cores left in the system; tracked through | |
6087 | * sd_llc->shared->has_idle_cores and enabled through update_idle_core() above. | |
6088 | */ | |
6089 | static int select_idle_core(struct task_struct *p, struct sched_domain *sd, int target) | |
6090 | { | |
6091 | struct cpumask *cpus = this_cpu_cpumask_var_ptr(select_idle_mask); | |
c743f0a5 | 6092 | int core, cpu; |
10e2f1ac | 6093 | |
1b568f0a PZ |
6094 | if (!static_branch_likely(&sched_smt_present)) |
6095 | return -1; | |
6096 | ||
10e2f1ac PZ |
6097 | if (!test_idle_cores(target, false)) |
6098 | return -1; | |
6099 | ||
0c98d344 | 6100 | cpumask_and(cpus, sched_domain_span(sd), &p->cpus_allowed); |
10e2f1ac | 6101 | |
c743f0a5 | 6102 | for_each_cpu_wrap(core, cpus, target) { |
10e2f1ac PZ |
6103 | bool idle = true; |
6104 | ||
6105 | for_each_cpu(cpu, cpu_smt_mask(core)) { | |
6106 | cpumask_clear_cpu(cpu, cpus); | |
943d355d | 6107 | if (!available_idle_cpu(cpu)) |
10e2f1ac PZ |
6108 | idle = false; |
6109 | } | |
6110 | ||
6111 | if (idle) | |
6112 | return core; | |
6113 | } | |
6114 | ||
6115 | /* | |
6116 | * Failed to find an idle core; stop looking for one. | |
6117 | */ | |
6118 | set_idle_cores(target, 0); | |
6119 | ||
6120 | return -1; | |
6121 | } | |
6122 | ||
6123 | /* | |
6124 | * Scan the local SMT mask for idle CPUs. | |
6125 | */ | |
6126 | static int select_idle_smt(struct task_struct *p, struct sched_domain *sd, int target) | |
6127 | { | |
6128 | int cpu; | |
6129 | ||
1b568f0a PZ |
6130 | if (!static_branch_likely(&sched_smt_present)) |
6131 | return -1; | |
6132 | ||
10e2f1ac | 6133 | for_each_cpu(cpu, cpu_smt_mask(target)) { |
0c98d344 | 6134 | if (!cpumask_test_cpu(cpu, &p->cpus_allowed)) |
10e2f1ac | 6135 | continue; |
943d355d | 6136 | if (available_idle_cpu(cpu)) |
10e2f1ac PZ |
6137 | return cpu; |
6138 | } | |
6139 | ||
6140 | return -1; | |
6141 | } | |
6142 | ||
6143 | #else /* CONFIG_SCHED_SMT */ | |
6144 | ||
6145 | static inline int select_idle_core(struct task_struct *p, struct sched_domain *sd, int target) | |
6146 | { | |
6147 | return -1; | |
6148 | } | |
6149 | ||
6150 | static inline int select_idle_smt(struct task_struct *p, struct sched_domain *sd, int target) | |
6151 | { | |
6152 | return -1; | |
6153 | } | |
6154 | ||
6155 | #endif /* CONFIG_SCHED_SMT */ | |
6156 | ||
6157 | /* | |
6158 | * Scan the LLC domain for idle CPUs; this is dynamically regulated by | |
6159 | * comparing the average scan cost (tracked in sd->avg_scan_cost) against the | |
6160 | * average idle time for this rq (as found in rq->avg_idle). | |
a50bde51 | 6161 | */ |
10e2f1ac PZ |
6162 | static int select_idle_cpu(struct task_struct *p, struct sched_domain *sd, int target) |
6163 | { | |
9cfb38a7 | 6164 | struct sched_domain *this_sd; |
1ad3aaf3 | 6165 | u64 avg_cost, avg_idle; |
10e2f1ac PZ |
6166 | u64 time, cost; |
6167 | s64 delta; | |
1ad3aaf3 | 6168 | int cpu, nr = INT_MAX; |
10e2f1ac | 6169 | |
9cfb38a7 WL |
6170 | this_sd = rcu_dereference(*this_cpu_ptr(&sd_llc)); |
6171 | if (!this_sd) | |
6172 | return -1; | |
6173 | ||
10e2f1ac PZ |
6174 | /* |
6175 | * Due to large variance we need a large fuzz factor; hackbench in | |
6176 | * particularly is sensitive here. | |
6177 | */ | |
1ad3aaf3 PZ |
6178 | avg_idle = this_rq()->avg_idle / 512; |
6179 | avg_cost = this_sd->avg_scan_cost + 1; | |
6180 | ||
6181 | if (sched_feat(SIS_AVG_CPU) && avg_idle < avg_cost) | |
10e2f1ac PZ |
6182 | return -1; |
6183 | ||
1ad3aaf3 PZ |
6184 | if (sched_feat(SIS_PROP)) { |
6185 | u64 span_avg = sd->span_weight * avg_idle; | |
6186 | if (span_avg > 4*avg_cost) | |
6187 | nr = div_u64(span_avg, avg_cost); | |
6188 | else | |
6189 | nr = 4; | |
6190 | } | |
6191 | ||
10e2f1ac PZ |
6192 | time = local_clock(); |
6193 | ||
c743f0a5 | 6194 | for_each_cpu_wrap(cpu, sched_domain_span(sd), target) { |
1ad3aaf3 PZ |
6195 | if (!--nr) |
6196 | return -1; | |
0c98d344 | 6197 | if (!cpumask_test_cpu(cpu, &p->cpus_allowed)) |
10e2f1ac | 6198 | continue; |
943d355d | 6199 | if (available_idle_cpu(cpu)) |
10e2f1ac PZ |
6200 | break; |
6201 | } | |
6202 | ||
6203 | time = local_clock() - time; | |
6204 | cost = this_sd->avg_scan_cost; | |
6205 | delta = (s64)(time - cost) / 8; | |
6206 | this_sd->avg_scan_cost += delta; | |
6207 | ||
6208 | return cpu; | |
6209 | } | |
6210 | ||
6211 | /* | |
6212 | * Try and locate an idle core/thread in the LLC cache domain. | |
a50bde51 | 6213 | */ |
772bd008 | 6214 | static int select_idle_sibling(struct task_struct *p, int prev, int target) |
a50bde51 | 6215 | { |
99bd5e2f | 6216 | struct sched_domain *sd; |
32e839dd | 6217 | int i, recent_used_cpu; |
a50bde51 | 6218 | |
943d355d | 6219 | if (available_idle_cpu(target)) |
e0a79f52 | 6220 | return target; |
99bd5e2f SS |
6221 | |
6222 | /* | |
97fb7a0a | 6223 | * If the previous CPU is cache affine and idle, don't be stupid: |
99bd5e2f | 6224 | */ |
943d355d | 6225 | if (prev != target && cpus_share_cache(prev, target) && available_idle_cpu(prev)) |
772bd008 | 6226 | return prev; |
a50bde51 | 6227 | |
97fb7a0a | 6228 | /* Check a recently used CPU as a potential idle candidate: */ |
32e839dd MG |
6229 | recent_used_cpu = p->recent_used_cpu; |
6230 | if (recent_used_cpu != prev && | |
6231 | recent_used_cpu != target && | |
6232 | cpus_share_cache(recent_used_cpu, target) && | |
943d355d | 6233 | available_idle_cpu(recent_used_cpu) && |
32e839dd MG |
6234 | cpumask_test_cpu(p->recent_used_cpu, &p->cpus_allowed)) { |
6235 | /* | |
6236 | * Replace recent_used_cpu with prev as it is a potential | |
97fb7a0a | 6237 | * candidate for the next wake: |
32e839dd MG |
6238 | */ |
6239 | p->recent_used_cpu = prev; | |
6240 | return recent_used_cpu; | |
6241 | } | |
6242 | ||
518cd623 | 6243 | sd = rcu_dereference(per_cpu(sd_llc, target)); |
10e2f1ac PZ |
6244 | if (!sd) |
6245 | return target; | |
772bd008 | 6246 | |
10e2f1ac PZ |
6247 | i = select_idle_core(p, sd, target); |
6248 | if ((unsigned)i < nr_cpumask_bits) | |
6249 | return i; | |
37407ea7 | 6250 | |
10e2f1ac PZ |
6251 | i = select_idle_cpu(p, sd, target); |
6252 | if ((unsigned)i < nr_cpumask_bits) | |
6253 | return i; | |
6254 | ||
6255 | i = select_idle_smt(p, sd, target); | |
6256 | if ((unsigned)i < nr_cpumask_bits) | |
6257 | return i; | |
970e1789 | 6258 | |
a50bde51 PZ |
6259 | return target; |
6260 | } | |
231678b7 | 6261 | |
f9be3e59 PB |
6262 | /** |
6263 | * Amount of capacity of a CPU that is (estimated to be) used by CFS tasks | |
6264 | * @cpu: the CPU to get the utilization of | |
6265 | * | |
6266 | * The unit of the return value must be the one of capacity so we can compare | |
6267 | * the utilization with the capacity of the CPU that is available for CFS task | |
6268 | * (ie cpu_capacity). | |
231678b7 DE |
6269 | * |
6270 | * cfs_rq.avg.util_avg is the sum of running time of runnable tasks plus the | |
6271 | * recent utilization of currently non-runnable tasks on a CPU. It represents | |
6272 | * the amount of utilization of a CPU in the range [0..capacity_orig] where | |
6273 | * capacity_orig is the cpu_capacity available at the highest frequency | |
6274 | * (arch_scale_freq_capacity()). | |
6275 | * The utilization of a CPU converges towards a sum equal to or less than the | |
6276 | * current capacity (capacity_curr <= capacity_orig) of the CPU because it is | |
6277 | * the running time on this CPU scaled by capacity_curr. | |
6278 | * | |
f9be3e59 PB |
6279 | * The estimated utilization of a CPU is defined to be the maximum between its |
6280 | * cfs_rq.avg.util_avg and the sum of the estimated utilization of the tasks | |
6281 | * currently RUNNABLE on that CPU. | |
6282 | * This allows to properly represent the expected utilization of a CPU which | |
6283 | * has just got a big task running since a long sleep period. At the same time | |
6284 | * however it preserves the benefits of the "blocked utilization" in | |
6285 | * describing the potential for other tasks waking up on the same CPU. | |
6286 | * | |
231678b7 DE |
6287 | * Nevertheless, cfs_rq.avg.util_avg can be higher than capacity_curr or even |
6288 | * higher than capacity_orig because of unfortunate rounding in | |
6289 | * cfs.avg.util_avg or just after migrating tasks and new task wakeups until | |
6290 | * the average stabilizes with the new running time. We need to check that the | |
6291 | * utilization stays within the range of [0..capacity_orig] and cap it if | |
6292 | * necessary. Without utilization capping, a group could be seen as overloaded | |
6293 | * (CPU0 utilization at 121% + CPU1 utilization at 80%) whereas CPU1 has 20% of | |
6294 | * available capacity. We allow utilization to overshoot capacity_curr (but not | |
6295 | * capacity_orig) as it useful for predicting the capacity required after task | |
6296 | * migrations (scheduler-driven DVFS). | |
f9be3e59 PB |
6297 | * |
6298 | * Return: the (estimated) utilization for the specified CPU | |
8bb5b00c | 6299 | */ |
f9be3e59 | 6300 | static inline unsigned long cpu_util(int cpu) |
8bb5b00c | 6301 | { |
f9be3e59 PB |
6302 | struct cfs_rq *cfs_rq; |
6303 | unsigned int util; | |
6304 | ||
6305 | cfs_rq = &cpu_rq(cpu)->cfs; | |
6306 | util = READ_ONCE(cfs_rq->avg.util_avg); | |
6307 | ||
6308 | if (sched_feat(UTIL_EST)) | |
6309 | util = max(util, READ_ONCE(cfs_rq->avg.util_est.enqueued)); | |
8bb5b00c | 6310 | |
f9be3e59 | 6311 | return min_t(unsigned long, util, capacity_orig_of(cpu)); |
8bb5b00c | 6312 | } |
a50bde51 | 6313 | |
104cb16d | 6314 | /* |
c469933e PB |
6315 | * cpu_util_without: compute cpu utilization without any contributions from *p |
6316 | * @cpu: the CPU which utilization is requested | |
6317 | * @p: the task which utilization should be discounted | |
6318 | * | |
6319 | * The utilization of a CPU is defined by the utilization of tasks currently | |
6320 | * enqueued on that CPU as well as tasks which are currently sleeping after an | |
6321 | * execution on that CPU. | |
6322 | * | |
6323 | * This method returns the utilization of the specified CPU by discounting the | |
6324 | * utilization of the specified task, whenever the task is currently | |
6325 | * contributing to the CPU utilization. | |
104cb16d | 6326 | */ |
c469933e | 6327 | static unsigned long cpu_util_without(int cpu, struct task_struct *p) |
104cb16d | 6328 | { |
f9be3e59 PB |
6329 | struct cfs_rq *cfs_rq; |
6330 | unsigned int util; | |
104cb16d MR |
6331 | |
6332 | /* Task has no contribution or is new */ | |
f9be3e59 | 6333 | if (cpu != task_cpu(p) || !READ_ONCE(p->se.avg.last_update_time)) |
104cb16d MR |
6334 | return cpu_util(cpu); |
6335 | ||
f9be3e59 PB |
6336 | cfs_rq = &cpu_rq(cpu)->cfs; |
6337 | util = READ_ONCE(cfs_rq->avg.util_avg); | |
6338 | ||
c469933e | 6339 | /* Discount task's util from CPU's util */ |
b5c0ce7b | 6340 | lsub_positive(&util, task_util(p)); |
104cb16d | 6341 | |
f9be3e59 PB |
6342 | /* |
6343 | * Covered cases: | |
6344 | * | |
6345 | * a) if *p is the only task sleeping on this CPU, then: | |
6346 | * cpu_util (== task_util) > util_est (== 0) | |
6347 | * and thus we return: | |
c469933e | 6348 | * cpu_util_without = (cpu_util - task_util) = 0 |
f9be3e59 PB |
6349 | * |
6350 | * b) if other tasks are SLEEPING on this CPU, which is now exiting | |
6351 | * IDLE, then: | |
6352 | * cpu_util >= task_util | |
6353 | * cpu_util > util_est (== 0) | |
6354 | * and thus we discount *p's blocked utilization to return: | |
c469933e | 6355 | * cpu_util_without = (cpu_util - task_util) >= 0 |
f9be3e59 PB |
6356 | * |
6357 | * c) if other tasks are RUNNABLE on that CPU and | |
6358 | * util_est > cpu_util | |
6359 | * then we use util_est since it returns a more restrictive | |
6360 | * estimation of the spare capacity on that CPU, by just | |
6361 | * considering the expected utilization of tasks already | |
6362 | * runnable on that CPU. | |
6363 | * | |
6364 | * Cases a) and b) are covered by the above code, while case c) is | |
6365 | * covered by the following code when estimated utilization is | |
6366 | * enabled. | |
6367 | */ | |
c469933e PB |
6368 | if (sched_feat(UTIL_EST)) { |
6369 | unsigned int estimated = | |
6370 | READ_ONCE(cfs_rq->avg.util_est.enqueued); | |
6371 | ||
6372 | /* | |
6373 | * Despite the following checks we still have a small window | |
6374 | * for a possible race, when an execl's select_task_rq_fair() | |
6375 | * races with LB's detach_task(): | |
6376 | * | |
6377 | * detach_task() | |
6378 | * p->on_rq = TASK_ON_RQ_MIGRATING; | |
6379 | * ---------------------------------- A | |
6380 | * deactivate_task() \ | |
6381 | * dequeue_task() + RaceTime | |
6382 | * util_est_dequeue() / | |
6383 | * ---------------------------------- B | |
6384 | * | |
6385 | * The additional check on "current == p" it's required to | |
6386 | * properly fix the execl regression and it helps in further | |
6387 | * reducing the chances for the above race. | |
6388 | */ | |
b5c0ce7b PB |
6389 | if (unlikely(task_on_rq_queued(p) || current == p)) |
6390 | lsub_positive(&estimated, _task_util_est(p)); | |
6391 | ||
c469933e PB |
6392 | util = max(util, estimated); |
6393 | } | |
f9be3e59 PB |
6394 | |
6395 | /* | |
6396 | * Utilization (estimated) can exceed the CPU capacity, thus let's | |
6397 | * clamp to the maximum CPU capacity to ensure consistency with | |
6398 | * the cpu_util call. | |
6399 | */ | |
6400 | return min_t(unsigned long, util, capacity_orig_of(cpu)); | |
104cb16d MR |
6401 | } |
6402 | ||
3273163c MR |
6403 | /* |
6404 | * Disable WAKE_AFFINE in the case where task @p doesn't fit in the | |
6405 | * capacity of either the waking CPU @cpu or the previous CPU @prev_cpu. | |
6406 | * | |
6407 | * In that case WAKE_AFFINE doesn't make sense and we'll let | |
6408 | * BALANCE_WAKE sort things out. | |
6409 | */ | |
6410 | static int wake_cap(struct task_struct *p, int cpu, int prev_cpu) | |
6411 | { | |
6412 | long min_cap, max_cap; | |
6413 | ||
df054e84 MR |
6414 | if (!static_branch_unlikely(&sched_asym_cpucapacity)) |
6415 | return 0; | |
6416 | ||
3273163c MR |
6417 | min_cap = min(capacity_orig_of(prev_cpu), capacity_orig_of(cpu)); |
6418 | max_cap = cpu_rq(cpu)->rd->max_cpu_capacity; | |
6419 | ||
6420 | /* Minimum capacity is close to max, no need to abort wake_affine */ | |
6421 | if (max_cap - min_cap < max_cap >> 3) | |
6422 | return 0; | |
6423 | ||
104cb16d MR |
6424 | /* Bring task utilization in sync with prev_cpu */ |
6425 | sync_entity_load_avg(&p->se); | |
6426 | ||
3b1baa64 | 6427 | return !task_fits_capacity(p, min_cap); |
3273163c MR |
6428 | } |
6429 | ||
390031e4 QP |
6430 | /* |
6431 | * Predicts what cpu_util(@cpu) would return if @p was migrated (and enqueued) | |
6432 | * to @dst_cpu. | |
6433 | */ | |
6434 | static unsigned long cpu_util_next(int cpu, struct task_struct *p, int dst_cpu) | |
6435 | { | |
6436 | struct cfs_rq *cfs_rq = &cpu_rq(cpu)->cfs; | |
6437 | unsigned long util_est, util = READ_ONCE(cfs_rq->avg.util_avg); | |
6438 | ||
6439 | /* | |
6440 | * If @p migrates from @cpu to another, remove its contribution. Or, | |
6441 | * if @p migrates from another CPU to @cpu, add its contribution. In | |
6442 | * the other cases, @cpu is not impacted by the migration, so the | |
6443 | * util_avg should already be correct. | |
6444 | */ | |
6445 | if (task_cpu(p) == cpu && dst_cpu != cpu) | |
6446 | sub_positive(&util, task_util(p)); | |
6447 | else if (task_cpu(p) != cpu && dst_cpu == cpu) | |
6448 | util += task_util(p); | |
6449 | ||
6450 | if (sched_feat(UTIL_EST)) { | |
6451 | util_est = READ_ONCE(cfs_rq->avg.util_est.enqueued); | |
6452 | ||
6453 | /* | |
6454 | * During wake-up, the task isn't enqueued yet and doesn't | |
6455 | * appear in the cfs_rq->avg.util_est.enqueued of any rq, | |
6456 | * so just add it (if needed) to "simulate" what will be | |
6457 | * cpu_util() after the task has been enqueued. | |
6458 | */ | |
6459 | if (dst_cpu == cpu) | |
6460 | util_est += _task_util_est(p); | |
6461 | ||
6462 | util = max(util, util_est); | |
6463 | } | |
6464 | ||
6465 | return min(util, capacity_orig_of(cpu)); | |
6466 | } | |
6467 | ||
6468 | /* | |
6469 | * compute_energy(): Estimates the energy that would be consumed if @p was | |
6470 | * migrated to @dst_cpu. compute_energy() predicts what will be the utilization | |
6471 | * landscape of the * CPUs after the task migration, and uses the Energy Model | |
6472 | * to compute what would be the energy if we decided to actually migrate that | |
6473 | * task. | |
6474 | */ | |
6475 | static long | |
6476 | compute_energy(struct task_struct *p, int dst_cpu, struct perf_domain *pd) | |
6477 | { | |
6478 | long util, max_util, sum_util, energy = 0; | |
6479 | int cpu; | |
6480 | ||
6481 | for (; pd; pd = pd->next) { | |
6482 | max_util = sum_util = 0; | |
6483 | /* | |
6484 | * The capacity state of CPUs of the current rd can be driven by | |
6485 | * CPUs of another rd if they belong to the same performance | |
6486 | * domain. So, account for the utilization of these CPUs too | |
6487 | * by masking pd with cpu_online_mask instead of the rd span. | |
6488 | * | |
6489 | * If an entire performance domain is outside of the current rd, | |
6490 | * it will not appear in its pd list and will not be accounted | |
6491 | * by compute_energy(). | |
6492 | */ | |
6493 | for_each_cpu_and(cpu, perf_domain_span(pd), cpu_online_mask) { | |
6494 | util = cpu_util_next(cpu, p, dst_cpu); | |
6495 | util = schedutil_energy_util(cpu, util); | |
6496 | max_util = max(util, max_util); | |
6497 | sum_util += util; | |
6498 | } | |
6499 | ||
6500 | energy += em_pd_energy(pd->em_pd, max_util, sum_util); | |
6501 | } | |
6502 | ||
6503 | return energy; | |
6504 | } | |
6505 | ||
732cd75b QP |
6506 | /* |
6507 | * find_energy_efficient_cpu(): Find most energy-efficient target CPU for the | |
6508 | * waking task. find_energy_efficient_cpu() looks for the CPU with maximum | |
6509 | * spare capacity in each performance domain and uses it as a potential | |
6510 | * candidate to execute the task. Then, it uses the Energy Model to figure | |
6511 | * out which of the CPU candidates is the most energy-efficient. | |
6512 | * | |
6513 | * The rationale for this heuristic is as follows. In a performance domain, | |
6514 | * all the most energy efficient CPU candidates (according to the Energy | |
6515 | * Model) are those for which we'll request a low frequency. When there are | |
6516 | * several CPUs for which the frequency request will be the same, we don't | |
6517 | * have enough data to break the tie between them, because the Energy Model | |
6518 | * only includes active power costs. With this model, if we assume that | |
6519 | * frequency requests follow utilization (e.g. using schedutil), the CPU with | |
6520 | * the maximum spare capacity in a performance domain is guaranteed to be among | |
6521 | * the best candidates of the performance domain. | |
6522 | * | |
6523 | * In practice, it could be preferable from an energy standpoint to pack | |
6524 | * small tasks on a CPU in order to let other CPUs go in deeper idle states, | |
6525 | * but that could also hurt our chances to go cluster idle, and we have no | |
6526 | * ways to tell with the current Energy Model if this is actually a good | |
6527 | * idea or not. So, find_energy_efficient_cpu() basically favors | |
6528 | * cluster-packing, and spreading inside a cluster. That should at least be | |
6529 | * a good thing for latency, and this is consistent with the idea that most | |
6530 | * of the energy savings of EAS come from the asymmetry of the system, and | |
6531 | * not so much from breaking the tie between identical CPUs. That's also the | |
6532 | * reason why EAS is enabled in the topology code only for systems where | |
6533 | * SD_ASYM_CPUCAPACITY is set. | |
6534 | * | |
6535 | * NOTE: Forkees are not accepted in the energy-aware wake-up path because | |
6536 | * they don't have any useful utilization data yet and it's not possible to | |
6537 | * forecast their impact on energy consumption. Consequently, they will be | |
6538 | * placed by find_idlest_cpu() on the least loaded CPU, which might turn out | |
6539 | * to be energy-inefficient in some use-cases. The alternative would be to | |
6540 | * bias new tasks towards specific types of CPUs first, or to try to infer | |
6541 | * their util_avg from the parent task, but those heuristics could hurt | |
6542 | * other use-cases too. So, until someone finds a better way to solve this, | |
6543 | * let's keep things simple by re-using the existing slow path. | |
6544 | */ | |
6545 | ||
6546 | static int find_energy_efficient_cpu(struct task_struct *p, int prev_cpu) | |
6547 | { | |
6548 | unsigned long prev_energy = ULONG_MAX, best_energy = ULONG_MAX; | |
6549 | struct root_domain *rd = cpu_rq(smp_processor_id())->rd; | |
6550 | int cpu, best_energy_cpu = prev_cpu; | |
6551 | struct perf_domain *head, *pd; | |
6552 | unsigned long cpu_cap, util; | |
6553 | struct sched_domain *sd; | |
6554 | ||
6555 | rcu_read_lock(); | |
6556 | pd = rcu_dereference(rd->pd); | |
6557 | if (!pd || READ_ONCE(rd->overutilized)) | |
6558 | goto fail; | |
6559 | head = pd; | |
6560 | ||
6561 | /* | |
6562 | * Energy-aware wake-up happens on the lowest sched_domain starting | |
6563 | * from sd_asym_cpucapacity spanning over this_cpu and prev_cpu. | |
6564 | */ | |
6565 | sd = rcu_dereference(*this_cpu_ptr(&sd_asym_cpucapacity)); | |
6566 | while (sd && !cpumask_test_cpu(prev_cpu, sched_domain_span(sd))) | |
6567 | sd = sd->parent; | |
6568 | if (!sd) | |
6569 | goto fail; | |
6570 | ||
6571 | sync_entity_load_avg(&p->se); | |
6572 | if (!task_util_est(p)) | |
6573 | goto unlock; | |
6574 | ||
6575 | for (; pd; pd = pd->next) { | |
6576 | unsigned long cur_energy, spare_cap, max_spare_cap = 0; | |
6577 | int max_spare_cap_cpu = -1; | |
6578 | ||
6579 | for_each_cpu_and(cpu, perf_domain_span(pd), sched_domain_span(sd)) { | |
6580 | if (!cpumask_test_cpu(cpu, &p->cpus_allowed)) | |
6581 | continue; | |
6582 | ||
6583 | /* Skip CPUs that will be overutilized. */ | |
6584 | util = cpu_util_next(cpu, p, cpu); | |
6585 | cpu_cap = capacity_of(cpu); | |
6586 | if (cpu_cap * 1024 < util * capacity_margin) | |
6587 | continue; | |
6588 | ||
6589 | /* Always use prev_cpu as a candidate. */ | |
6590 | if (cpu == prev_cpu) { | |
6591 | prev_energy = compute_energy(p, prev_cpu, head); | |
6592 | best_energy = min(best_energy, prev_energy); | |
6593 | continue; | |
6594 | } | |
6595 | ||
6596 | /* | |
6597 | * Find the CPU with the maximum spare capacity in | |
6598 | * the performance domain | |
6599 | */ | |
6600 | spare_cap = cpu_cap - util; | |
6601 | if (spare_cap > max_spare_cap) { | |
6602 | max_spare_cap = spare_cap; | |
6603 | max_spare_cap_cpu = cpu; | |
6604 | } | |
6605 | } | |
6606 | ||
6607 | /* Evaluate the energy impact of using this CPU. */ | |
6608 | if (max_spare_cap_cpu >= 0) { | |
6609 | cur_energy = compute_energy(p, max_spare_cap_cpu, head); | |
6610 | if (cur_energy < best_energy) { | |
6611 | best_energy = cur_energy; | |
6612 | best_energy_cpu = max_spare_cap_cpu; | |
6613 | } | |
6614 | } | |
6615 | } | |
6616 | unlock: | |
6617 | rcu_read_unlock(); | |
6618 | ||
6619 | /* | |
6620 | * Pick the best CPU if prev_cpu cannot be used, or if it saves at | |
6621 | * least 6% of the energy used by prev_cpu. | |
6622 | */ | |
6623 | if (prev_energy == ULONG_MAX) | |
6624 | return best_energy_cpu; | |
6625 | ||
6626 | if ((prev_energy - best_energy) > (prev_energy >> 4)) | |
6627 | return best_energy_cpu; | |
6628 | ||
6629 | return prev_cpu; | |
6630 | ||
6631 | fail: | |
6632 | rcu_read_unlock(); | |
6633 | ||
6634 | return -1; | |
6635 | } | |
6636 | ||
aaee1203 | 6637 | /* |
de91b9cb MR |
6638 | * select_task_rq_fair: Select target runqueue for the waking task in domains |
6639 | * that have the 'sd_flag' flag set. In practice, this is SD_BALANCE_WAKE, | |
6640 | * SD_BALANCE_FORK, or SD_BALANCE_EXEC. | |
aaee1203 | 6641 | * |
97fb7a0a IM |
6642 | * Balances load by selecting the idlest CPU in the idlest group, or under |
6643 | * certain conditions an idle sibling CPU if the domain has SD_WAKE_AFFINE set. | |
aaee1203 | 6644 | * |
97fb7a0a | 6645 | * Returns the target CPU number. |
aaee1203 PZ |
6646 | * |
6647 | * preempt must be disabled. | |
6648 | */ | |
0017d735 | 6649 | static int |
ac66f547 | 6650 | select_task_rq_fair(struct task_struct *p, int prev_cpu, int sd_flag, int wake_flags) |
aaee1203 | 6651 | { |
f1d88b44 | 6652 | struct sched_domain *tmp, *sd = NULL; |
c88d5910 | 6653 | int cpu = smp_processor_id(); |
63b0e9ed | 6654 | int new_cpu = prev_cpu; |
99bd5e2f | 6655 | int want_affine = 0; |
24d0c1d6 | 6656 | int sync = (wake_flags & WF_SYNC) && !(current->flags & PF_EXITING); |
c88d5910 | 6657 | |
c58d25f3 PZ |
6658 | if (sd_flag & SD_BALANCE_WAKE) { |
6659 | record_wakee(p); | |
732cd75b | 6660 | |
f8a696f2 | 6661 | if (sched_energy_enabled()) { |
732cd75b QP |
6662 | new_cpu = find_energy_efficient_cpu(p, prev_cpu); |
6663 | if (new_cpu >= 0) | |
6664 | return new_cpu; | |
6665 | new_cpu = prev_cpu; | |
6666 | } | |
6667 | ||
6668 | want_affine = !wake_wide(p) && !wake_cap(p, cpu, prev_cpu) && | |
6669 | cpumask_test_cpu(cpu, &p->cpus_allowed); | |
c58d25f3 | 6670 | } |
aaee1203 | 6671 | |
dce840a0 | 6672 | rcu_read_lock(); |
aaee1203 | 6673 | for_each_domain(cpu, tmp) { |
e4f42888 | 6674 | if (!(tmp->flags & SD_LOAD_BALANCE)) |
63b0e9ed | 6675 | break; |
e4f42888 | 6676 | |
fe3bcfe1 | 6677 | /* |
97fb7a0a | 6678 | * If both 'cpu' and 'prev_cpu' are part of this domain, |
99bd5e2f | 6679 | * cpu is a valid SD_WAKE_AFFINE target. |
fe3bcfe1 | 6680 | */ |
99bd5e2f SS |
6681 | if (want_affine && (tmp->flags & SD_WAKE_AFFINE) && |
6682 | cpumask_test_cpu(prev_cpu, sched_domain_span(tmp))) { | |
f1d88b44 VK |
6683 | if (cpu != prev_cpu) |
6684 | new_cpu = wake_affine(tmp, p, cpu, prev_cpu, sync); | |
6685 | ||
6686 | sd = NULL; /* Prefer wake_affine over balance flags */ | |
29cd8bae | 6687 | break; |
f03542a7 | 6688 | } |
29cd8bae | 6689 | |
f03542a7 | 6690 | if (tmp->flags & sd_flag) |
29cd8bae | 6691 | sd = tmp; |
63b0e9ed MG |
6692 | else if (!want_affine) |
6693 | break; | |
29cd8bae PZ |
6694 | } |
6695 | ||
f1d88b44 VK |
6696 | if (unlikely(sd)) { |
6697 | /* Slow path */ | |
18bd1b4b | 6698 | new_cpu = find_idlest_cpu(sd, p, cpu, prev_cpu, sd_flag); |
f1d88b44 VK |
6699 | } else if (sd_flag & SD_BALANCE_WAKE) { /* XXX always ? */ |
6700 | /* Fast path */ | |
6701 | ||
6702 | new_cpu = select_idle_sibling(p, prev_cpu, new_cpu); | |
6703 | ||
6704 | if (want_affine) | |
6705 | current->recent_used_cpu = cpu; | |
e7693a36 | 6706 | } |
dce840a0 | 6707 | rcu_read_unlock(); |
e7693a36 | 6708 | |
c88d5910 | 6709 | return new_cpu; |
e7693a36 | 6710 | } |
0a74bef8 | 6711 | |
144d8487 PZ |
6712 | static void detach_entity_cfs_rq(struct sched_entity *se); |
6713 | ||
0a74bef8 | 6714 | /* |
97fb7a0a | 6715 | * Called immediately before a task is migrated to a new CPU; task_cpu(p) and |
0a74bef8 | 6716 | * cfs_rq_of(p) references at time of call are still valid and identify the |
97fb7a0a | 6717 | * previous CPU. The caller guarantees p->pi_lock or task_rq(p)->lock is held. |
0a74bef8 | 6718 | */ |
3f9672ba | 6719 | static void migrate_task_rq_fair(struct task_struct *p, int new_cpu) |
0a74bef8 | 6720 | { |
59efa0ba PZ |
6721 | /* |
6722 | * As blocked tasks retain absolute vruntime the migration needs to | |
6723 | * deal with this by subtracting the old and adding the new | |
6724 | * min_vruntime -- the latter is done by enqueue_entity() when placing | |
6725 | * the task on the new runqueue. | |
6726 | */ | |
6727 | if (p->state == TASK_WAKING) { | |
6728 | struct sched_entity *se = &p->se; | |
6729 | struct cfs_rq *cfs_rq = cfs_rq_of(se); | |
6730 | u64 min_vruntime; | |
6731 | ||
6732 | #ifndef CONFIG_64BIT | |
6733 | u64 min_vruntime_copy; | |
6734 | ||
6735 | do { | |
6736 | min_vruntime_copy = cfs_rq->min_vruntime_copy; | |
6737 | smp_rmb(); | |
6738 | min_vruntime = cfs_rq->min_vruntime; | |
6739 | } while (min_vruntime != min_vruntime_copy); | |
6740 | #else | |
6741 | min_vruntime = cfs_rq->min_vruntime; | |
6742 | #endif | |
6743 | ||
6744 | se->vruntime -= min_vruntime; | |
6745 | } | |
6746 | ||
144d8487 PZ |
6747 | if (p->on_rq == TASK_ON_RQ_MIGRATING) { |
6748 | /* | |
6749 | * In case of TASK_ON_RQ_MIGRATING we in fact hold the 'old' | |
6750 | * rq->lock and can modify state directly. | |
6751 | */ | |
6752 | lockdep_assert_held(&task_rq(p)->lock); | |
6753 | detach_entity_cfs_rq(&p->se); | |
6754 | ||
6755 | } else { | |
6756 | /* | |
6757 | * We are supposed to update the task to "current" time, then | |
6758 | * its up to date and ready to go to new CPU/cfs_rq. But we | |
6759 | * have difficulty in getting what current time is, so simply | |
6760 | * throw away the out-of-date time. This will result in the | |
6761 | * wakee task is less decayed, but giving the wakee more load | |
6762 | * sounds not bad. | |
6763 | */ | |
6764 | remove_entity_load_avg(&p->se); | |
6765 | } | |
9d89c257 YD |
6766 | |
6767 | /* Tell new CPU we are migrated */ | |
6768 | p->se.avg.last_update_time = 0; | |
3944a927 BS |
6769 | |
6770 | /* We have migrated, no longer consider this task hot */ | |
9d89c257 | 6771 | p->se.exec_start = 0; |
3f9672ba SD |
6772 | |
6773 | update_scan_period(p, new_cpu); | |
0a74bef8 | 6774 | } |
12695578 YD |
6775 | |
6776 | static void task_dead_fair(struct task_struct *p) | |
6777 | { | |
6778 | remove_entity_load_avg(&p->se); | |
6779 | } | |
e7693a36 GH |
6780 | #endif /* CONFIG_SMP */ |
6781 | ||
a555e9d8 | 6782 | static unsigned long wakeup_gran(struct sched_entity *se) |
0bbd3336 PZ |
6783 | { |
6784 | unsigned long gran = sysctl_sched_wakeup_granularity; | |
6785 | ||
6786 | /* | |
e52fb7c0 PZ |
6787 | * Since its curr running now, convert the gran from real-time |
6788 | * to virtual-time in his units. | |
13814d42 MG |
6789 | * |
6790 | * By using 'se' instead of 'curr' we penalize light tasks, so | |
6791 | * they get preempted easier. That is, if 'se' < 'curr' then | |
6792 | * the resulting gran will be larger, therefore penalizing the | |
6793 | * lighter, if otoh 'se' > 'curr' then the resulting gran will | |
6794 | * be smaller, again penalizing the lighter task. | |
6795 | * | |
6796 | * This is especially important for buddies when the leftmost | |
6797 | * task is higher priority than the buddy. | |
0bbd3336 | 6798 | */ |
f4ad9bd2 | 6799 | return calc_delta_fair(gran, se); |
0bbd3336 PZ |
6800 | } |
6801 | ||
464b7527 PZ |
6802 | /* |
6803 | * Should 'se' preempt 'curr'. | |
6804 | * | |
6805 | * |s1 | |
6806 | * |s2 | |
6807 | * |s3 | |
6808 | * g | |
6809 | * |<--->|c | |
6810 | * | |
6811 | * w(c, s1) = -1 | |
6812 | * w(c, s2) = 0 | |
6813 | * w(c, s3) = 1 | |
6814 | * | |
6815 | */ | |
6816 | static int | |
6817 | wakeup_preempt_entity(struct sched_entity *curr, struct sched_entity *se) | |
6818 | { | |
6819 | s64 gran, vdiff = curr->vruntime - se->vruntime; | |
6820 | ||
6821 | if (vdiff <= 0) | |
6822 | return -1; | |
6823 | ||
a555e9d8 | 6824 | gran = wakeup_gran(se); |
464b7527 PZ |
6825 | if (vdiff > gran) |
6826 | return 1; | |
6827 | ||
6828 | return 0; | |
6829 | } | |
6830 | ||
02479099 PZ |
6831 | static void set_last_buddy(struct sched_entity *se) |
6832 | { | |
1da1843f | 6833 | if (entity_is_task(se) && unlikely(task_has_idle_policy(task_of(se)))) |
69c80f3e VP |
6834 | return; |
6835 | ||
c5ae366e DA |
6836 | for_each_sched_entity(se) { |
6837 | if (SCHED_WARN_ON(!se->on_rq)) | |
6838 | return; | |
69c80f3e | 6839 | cfs_rq_of(se)->last = se; |
c5ae366e | 6840 | } |
02479099 PZ |
6841 | } |
6842 | ||
6843 | static void set_next_buddy(struct sched_entity *se) | |
6844 | { | |
1da1843f | 6845 | if (entity_is_task(se) && unlikely(task_has_idle_policy(task_of(se)))) |
69c80f3e VP |
6846 | return; |
6847 | ||
c5ae366e DA |
6848 | for_each_sched_entity(se) { |
6849 | if (SCHED_WARN_ON(!se->on_rq)) | |
6850 | return; | |
69c80f3e | 6851 | cfs_rq_of(se)->next = se; |
c5ae366e | 6852 | } |
02479099 PZ |
6853 | } |
6854 | ||
ac53db59 RR |
6855 | static void set_skip_buddy(struct sched_entity *se) |
6856 | { | |
69c80f3e VP |
6857 | for_each_sched_entity(se) |
6858 | cfs_rq_of(se)->skip = se; | |
ac53db59 RR |
6859 | } |
6860 | ||
bf0f6f24 IM |
6861 | /* |
6862 | * Preempt the current task with a newly woken task if needed: | |
6863 | */ | |
5a9b86f6 | 6864 | static void check_preempt_wakeup(struct rq *rq, struct task_struct *p, int wake_flags) |
bf0f6f24 IM |
6865 | { |
6866 | struct task_struct *curr = rq->curr; | |
8651a86c | 6867 | struct sched_entity *se = &curr->se, *pse = &p->se; |
03e89e45 | 6868 | struct cfs_rq *cfs_rq = task_cfs_rq(curr); |
f685ceac | 6869 | int scale = cfs_rq->nr_running >= sched_nr_latency; |
2f36825b | 6870 | int next_buddy_marked = 0; |
bf0f6f24 | 6871 | |
4ae7d5ce IM |
6872 | if (unlikely(se == pse)) |
6873 | return; | |
6874 | ||
5238cdd3 | 6875 | /* |
163122b7 | 6876 | * This is possible from callers such as attach_tasks(), in which we |
5238cdd3 PT |
6877 | * unconditionally check_prempt_curr() after an enqueue (which may have |
6878 | * lead to a throttle). This both saves work and prevents false | |
6879 | * next-buddy nomination below. | |
6880 | */ | |
6881 | if (unlikely(throttled_hierarchy(cfs_rq_of(pse)))) | |
6882 | return; | |
6883 | ||
2f36825b | 6884 | if (sched_feat(NEXT_BUDDY) && scale && !(wake_flags & WF_FORK)) { |
3cb63d52 | 6885 | set_next_buddy(pse); |
2f36825b VP |
6886 | next_buddy_marked = 1; |
6887 | } | |
57fdc26d | 6888 | |
aec0a514 BR |
6889 | /* |
6890 | * We can come here with TIF_NEED_RESCHED already set from new task | |
6891 | * wake up path. | |
5238cdd3 PT |
6892 | * |
6893 | * Note: this also catches the edge-case of curr being in a throttled | |
6894 | * group (e.g. via set_curr_task), since update_curr() (in the | |
6895 | * enqueue of curr) will have resulted in resched being set. This | |
6896 | * prevents us from potentially nominating it as a false LAST_BUDDY | |
6897 | * below. | |
aec0a514 BR |
6898 | */ |
6899 | if (test_tsk_need_resched(curr)) | |
6900 | return; | |
6901 | ||
a2f5c9ab | 6902 | /* Idle tasks are by definition preempted by non-idle tasks. */ |
1da1843f VK |
6903 | if (unlikely(task_has_idle_policy(curr)) && |
6904 | likely(!task_has_idle_policy(p))) | |
a2f5c9ab DH |
6905 | goto preempt; |
6906 | ||
91c234b4 | 6907 | /* |
a2f5c9ab DH |
6908 | * Batch and idle tasks do not preempt non-idle tasks (their preemption |
6909 | * is driven by the tick): | |
91c234b4 | 6910 | */ |
8ed92e51 | 6911 | if (unlikely(p->policy != SCHED_NORMAL) || !sched_feat(WAKEUP_PREEMPTION)) |
91c234b4 | 6912 | return; |
bf0f6f24 | 6913 | |
464b7527 | 6914 | find_matching_se(&se, &pse); |
9bbd7374 | 6915 | update_curr(cfs_rq_of(se)); |
002f128b | 6916 | BUG_ON(!pse); |
2f36825b VP |
6917 | if (wakeup_preempt_entity(se, pse) == 1) { |
6918 | /* | |
6919 | * Bias pick_next to pick the sched entity that is | |
6920 | * triggering this preemption. | |
6921 | */ | |
6922 | if (!next_buddy_marked) | |
6923 | set_next_buddy(pse); | |
3a7e73a2 | 6924 | goto preempt; |
2f36825b | 6925 | } |
464b7527 | 6926 | |
3a7e73a2 | 6927 | return; |
a65ac745 | 6928 | |
3a7e73a2 | 6929 | preempt: |
8875125e | 6930 | resched_curr(rq); |
3a7e73a2 PZ |
6931 | /* |
6932 | * Only set the backward buddy when the current task is still | |
6933 | * on the rq. This can happen when a wakeup gets interleaved | |
6934 | * with schedule on the ->pre_schedule() or idle_balance() | |
6935 | * point, either of which can * drop the rq lock. | |
6936 | * | |
6937 | * Also, during early boot the idle thread is in the fair class, | |
6938 | * for obvious reasons its a bad idea to schedule back to it. | |
6939 | */ | |
6940 | if (unlikely(!se->on_rq || curr == rq->idle)) | |
6941 | return; | |
6942 | ||
6943 | if (sched_feat(LAST_BUDDY) && scale && entity_is_task(se)) | |
6944 | set_last_buddy(se); | |
bf0f6f24 IM |
6945 | } |
6946 | ||
606dba2e | 6947 | static struct task_struct * |
d8ac8971 | 6948 | pick_next_task_fair(struct rq *rq, struct task_struct *prev, struct rq_flags *rf) |
bf0f6f24 IM |
6949 | { |
6950 | struct cfs_rq *cfs_rq = &rq->cfs; | |
6951 | struct sched_entity *se; | |
678d5718 | 6952 | struct task_struct *p; |
37e117c0 | 6953 | int new_tasks; |
678d5718 | 6954 | |
6e83125c | 6955 | again: |
678d5718 | 6956 | if (!cfs_rq->nr_running) |
38033c37 | 6957 | goto idle; |
678d5718 | 6958 | |
9674f5ca | 6959 | #ifdef CONFIG_FAIR_GROUP_SCHED |
3f1d2a31 | 6960 | if (prev->sched_class != &fair_sched_class) |
678d5718 PZ |
6961 | goto simple; |
6962 | ||
6963 | /* | |
6964 | * Because of the set_next_buddy() in dequeue_task_fair() it is rather | |
6965 | * likely that a next task is from the same cgroup as the current. | |
6966 | * | |
6967 | * Therefore attempt to avoid putting and setting the entire cgroup | |
6968 | * hierarchy, only change the part that actually changes. | |
6969 | */ | |
6970 | ||
6971 | do { | |
6972 | struct sched_entity *curr = cfs_rq->curr; | |
6973 | ||
6974 | /* | |
6975 | * Since we got here without doing put_prev_entity() we also | |
6976 | * have to consider cfs_rq->curr. If it is still a runnable | |
6977 | * entity, update_curr() will update its vruntime, otherwise | |
6978 | * forget we've ever seen it. | |
6979 | */ | |
54d27365 BS |
6980 | if (curr) { |
6981 | if (curr->on_rq) | |
6982 | update_curr(cfs_rq); | |
6983 | else | |
6984 | curr = NULL; | |
678d5718 | 6985 | |
54d27365 BS |
6986 | /* |
6987 | * This call to check_cfs_rq_runtime() will do the | |
6988 | * throttle and dequeue its entity in the parent(s). | |
9674f5ca | 6989 | * Therefore the nr_running test will indeed |
54d27365 BS |
6990 | * be correct. |
6991 | */ | |
9674f5ca VK |
6992 | if (unlikely(check_cfs_rq_runtime(cfs_rq))) { |
6993 | cfs_rq = &rq->cfs; | |
6994 | ||
6995 | if (!cfs_rq->nr_running) | |
6996 | goto idle; | |
6997 | ||
54d27365 | 6998 | goto simple; |
9674f5ca | 6999 | } |
54d27365 | 7000 | } |
678d5718 PZ |
7001 | |
7002 | se = pick_next_entity(cfs_rq, curr); | |
7003 | cfs_rq = group_cfs_rq(se); | |
7004 | } while (cfs_rq); | |
7005 | ||
7006 | p = task_of(se); | |
7007 | ||
7008 | /* | |
7009 | * Since we haven't yet done put_prev_entity and if the selected task | |
7010 | * is a different task than we started out with, try and touch the | |
7011 | * least amount of cfs_rqs. | |
7012 | */ | |
7013 | if (prev != p) { | |
7014 | struct sched_entity *pse = &prev->se; | |
7015 | ||
7016 | while (!(cfs_rq = is_same_group(se, pse))) { | |
7017 | int se_depth = se->depth; | |
7018 | int pse_depth = pse->depth; | |
7019 | ||
7020 | if (se_depth <= pse_depth) { | |
7021 | put_prev_entity(cfs_rq_of(pse), pse); | |
7022 | pse = parent_entity(pse); | |
7023 | } | |
7024 | if (se_depth >= pse_depth) { | |
7025 | set_next_entity(cfs_rq_of(se), se); | |
7026 | se = parent_entity(se); | |
7027 | } | |
7028 | } | |
7029 | ||
7030 | put_prev_entity(cfs_rq, pse); | |
7031 | set_next_entity(cfs_rq, se); | |
7032 | } | |
7033 | ||
93824900 | 7034 | goto done; |
678d5718 | 7035 | simple: |
678d5718 | 7036 | #endif |
bf0f6f24 | 7037 | |
3f1d2a31 | 7038 | put_prev_task(rq, prev); |
606dba2e | 7039 | |
bf0f6f24 | 7040 | do { |
678d5718 | 7041 | se = pick_next_entity(cfs_rq, NULL); |
f4b6755f | 7042 | set_next_entity(cfs_rq, se); |
bf0f6f24 IM |
7043 | cfs_rq = group_cfs_rq(se); |
7044 | } while (cfs_rq); | |
7045 | ||
8f4d37ec | 7046 | p = task_of(se); |
678d5718 | 7047 | |
13a453c2 | 7048 | done: __maybe_unused; |
93824900 UR |
7049 | #ifdef CONFIG_SMP |
7050 | /* | |
7051 | * Move the next running task to the front of | |
7052 | * the list, so our cfs_tasks list becomes MRU | |
7053 | * one. | |
7054 | */ | |
7055 | list_move(&p->se.group_node, &rq->cfs_tasks); | |
7056 | #endif | |
7057 | ||
b39e66ea MG |
7058 | if (hrtick_enabled(rq)) |
7059 | hrtick_start_fair(rq, p); | |
8f4d37ec | 7060 | |
3b1baa64 MR |
7061 | update_misfit_status(p, rq); |
7062 | ||
8f4d37ec | 7063 | return p; |
38033c37 PZ |
7064 | |
7065 | idle: | |
3b1baa64 | 7066 | update_misfit_status(NULL, rq); |
46f69fa3 MF |
7067 | new_tasks = idle_balance(rq, rf); |
7068 | ||
37e117c0 PZ |
7069 | /* |
7070 | * Because idle_balance() releases (and re-acquires) rq->lock, it is | |
7071 | * possible for any higher priority task to appear. In that case we | |
7072 | * must re-start the pick_next_entity() loop. | |
7073 | */ | |
e4aa358b | 7074 | if (new_tasks < 0) |
37e117c0 PZ |
7075 | return RETRY_TASK; |
7076 | ||
e4aa358b | 7077 | if (new_tasks > 0) |
38033c37 | 7078 | goto again; |
38033c37 | 7079 | |
23127296 VG |
7080 | /* |
7081 | * rq is about to be idle, check if we need to update the | |
7082 | * lost_idle_time of clock_pelt | |
7083 | */ | |
7084 | update_idle_rq_clock_pelt(rq); | |
7085 | ||
38033c37 | 7086 | return NULL; |
bf0f6f24 IM |
7087 | } |
7088 | ||
7089 | /* | |
7090 | * Account for a descheduled task: | |
7091 | */ | |
31ee529c | 7092 | static void put_prev_task_fair(struct rq *rq, struct task_struct *prev) |
bf0f6f24 IM |
7093 | { |
7094 | struct sched_entity *se = &prev->se; | |
7095 | struct cfs_rq *cfs_rq; | |
7096 | ||
7097 | for_each_sched_entity(se) { | |
7098 | cfs_rq = cfs_rq_of(se); | |
ab6cde26 | 7099 | put_prev_entity(cfs_rq, se); |
bf0f6f24 IM |
7100 | } |
7101 | } | |
7102 | ||
ac53db59 RR |
7103 | /* |
7104 | * sched_yield() is very simple | |
7105 | * | |
7106 | * The magic of dealing with the ->skip buddy is in pick_next_entity. | |
7107 | */ | |
7108 | static void yield_task_fair(struct rq *rq) | |
7109 | { | |
7110 | struct task_struct *curr = rq->curr; | |
7111 | struct cfs_rq *cfs_rq = task_cfs_rq(curr); | |
7112 | struct sched_entity *se = &curr->se; | |
7113 | ||
7114 | /* | |
7115 | * Are we the only task in the tree? | |
7116 | */ | |
7117 | if (unlikely(rq->nr_running == 1)) | |
7118 | return; | |
7119 | ||
7120 | clear_buddies(cfs_rq, se); | |
7121 | ||
7122 | if (curr->policy != SCHED_BATCH) { | |
7123 | update_rq_clock(rq); | |
7124 | /* | |
7125 | * Update run-time statistics of the 'current'. | |
7126 | */ | |
7127 | update_curr(cfs_rq); | |
916671c0 MG |
7128 | /* |
7129 | * Tell update_rq_clock() that we've just updated, | |
7130 | * so we don't do microscopic update in schedule() | |
7131 | * and double the fastpath cost. | |
7132 | */ | |
adcc8da8 | 7133 | rq_clock_skip_update(rq); |
ac53db59 RR |
7134 | } |
7135 | ||
7136 | set_skip_buddy(se); | |
7137 | } | |
7138 | ||
d95f4122 MG |
7139 | static bool yield_to_task_fair(struct rq *rq, struct task_struct *p, bool preempt) |
7140 | { | |
7141 | struct sched_entity *se = &p->se; | |
7142 | ||
5238cdd3 PT |
7143 | /* throttled hierarchies are not runnable */ |
7144 | if (!se->on_rq || throttled_hierarchy(cfs_rq_of(se))) | |
d95f4122 MG |
7145 | return false; |
7146 | ||
7147 | /* Tell the scheduler that we'd really like pse to run next. */ | |
7148 | set_next_buddy(se); | |
7149 | ||
d95f4122 MG |
7150 | yield_task_fair(rq); |
7151 | ||
7152 | return true; | |
7153 | } | |
7154 | ||
681f3e68 | 7155 | #ifdef CONFIG_SMP |
bf0f6f24 | 7156 | /************************************************** |
e9c84cb8 PZ |
7157 | * Fair scheduling class load-balancing methods. |
7158 | * | |
7159 | * BASICS | |
7160 | * | |
7161 | * The purpose of load-balancing is to achieve the same basic fairness the | |
97fb7a0a | 7162 | * per-CPU scheduler provides, namely provide a proportional amount of compute |
e9c84cb8 PZ |
7163 | * time to each task. This is expressed in the following equation: |
7164 | * | |
7165 | * W_i,n/P_i == W_j,n/P_j for all i,j (1) | |
7166 | * | |
97fb7a0a | 7167 | * Where W_i,n is the n-th weight average for CPU i. The instantaneous weight |
e9c84cb8 PZ |
7168 | * W_i,0 is defined as: |
7169 | * | |
7170 | * W_i,0 = \Sum_j w_i,j (2) | |
7171 | * | |
97fb7a0a | 7172 | * Where w_i,j is the weight of the j-th runnable task on CPU i. This weight |
1c3de5e1 | 7173 | * is derived from the nice value as per sched_prio_to_weight[]. |
e9c84cb8 PZ |
7174 | * |
7175 | * The weight average is an exponential decay average of the instantaneous | |
7176 | * weight: | |
7177 | * | |
7178 | * W'_i,n = (2^n - 1) / 2^n * W_i,n + 1 / 2^n * W_i,0 (3) | |
7179 | * | |
97fb7a0a | 7180 | * C_i is the compute capacity of CPU i, typically it is the |
e9c84cb8 PZ |
7181 | * fraction of 'recent' time available for SCHED_OTHER task execution. But it |
7182 | * can also include other factors [XXX]. | |
7183 | * | |
7184 | * To achieve this balance we define a measure of imbalance which follows | |
7185 | * directly from (1): | |
7186 | * | |
ced549fa | 7187 | * imb_i,j = max{ avg(W/C), W_i/C_i } - min{ avg(W/C), W_j/C_j } (4) |
e9c84cb8 PZ |
7188 | * |
7189 | * We them move tasks around to minimize the imbalance. In the continuous | |
7190 | * function space it is obvious this converges, in the discrete case we get | |
7191 | * a few fun cases generally called infeasible weight scenarios. | |
7192 | * | |
7193 | * [XXX expand on: | |
7194 | * - infeasible weights; | |
7195 | * - local vs global optima in the discrete case. ] | |
7196 | * | |
7197 | * | |
7198 | * SCHED DOMAINS | |
7199 | * | |
7200 | * In order to solve the imbalance equation (4), and avoid the obvious O(n^2) | |
97fb7a0a | 7201 | * for all i,j solution, we create a tree of CPUs that follows the hardware |
e9c84cb8 | 7202 | * topology where each level pairs two lower groups (or better). This results |
97fb7a0a | 7203 | * in O(log n) layers. Furthermore we reduce the number of CPUs going up the |
e9c84cb8 | 7204 | * tree to only the first of the previous level and we decrease the frequency |
97fb7a0a | 7205 | * of load-balance at each level inv. proportional to the number of CPUs in |
e9c84cb8 PZ |
7206 | * the groups. |
7207 | * | |
7208 | * This yields: | |
7209 | * | |
7210 | * log_2 n 1 n | |
7211 | * \Sum { --- * --- * 2^i } = O(n) (5) | |
7212 | * i = 0 2^i 2^i | |
7213 | * `- size of each group | |
97fb7a0a | 7214 | * | | `- number of CPUs doing load-balance |
e9c84cb8 PZ |
7215 | * | `- freq |
7216 | * `- sum over all levels | |
7217 | * | |
7218 | * Coupled with a limit on how many tasks we can migrate every balance pass, | |
7219 | * this makes (5) the runtime complexity of the balancer. | |
7220 | * | |
7221 | * An important property here is that each CPU is still (indirectly) connected | |
97fb7a0a | 7222 | * to every other CPU in at most O(log n) steps: |
e9c84cb8 PZ |
7223 | * |
7224 | * The adjacency matrix of the resulting graph is given by: | |
7225 | * | |
97a7142f | 7226 | * log_2 n |
e9c84cb8 PZ |
7227 | * A_i,j = \Union (i % 2^k == 0) && i / 2^(k+1) == j / 2^(k+1) (6) |
7228 | * k = 0 | |
7229 | * | |
7230 | * And you'll find that: | |
7231 | * | |
7232 | * A^(log_2 n)_i,j != 0 for all i,j (7) | |
7233 | * | |
97fb7a0a | 7234 | * Showing there's indeed a path between every CPU in at most O(log n) steps. |
e9c84cb8 PZ |
7235 | * The task movement gives a factor of O(m), giving a convergence complexity |
7236 | * of: | |
7237 | * | |
7238 | * O(nm log n), n := nr_cpus, m := nr_tasks (8) | |
7239 | * | |
7240 | * | |
7241 | * WORK CONSERVING | |
7242 | * | |
7243 | * In order to avoid CPUs going idle while there's still work to do, new idle | |
97fb7a0a | 7244 | * balancing is more aggressive and has the newly idle CPU iterate up the domain |
e9c84cb8 PZ |
7245 | * tree itself instead of relying on other CPUs to bring it work. |
7246 | * | |
7247 | * This adds some complexity to both (5) and (8) but it reduces the total idle | |
7248 | * time. | |
7249 | * | |
7250 | * [XXX more?] | |
7251 | * | |
7252 | * | |
7253 | * CGROUPS | |
7254 | * | |
7255 | * Cgroups make a horror show out of (2), instead of a simple sum we get: | |
7256 | * | |
7257 | * s_k,i | |
7258 | * W_i,0 = \Sum_j \Prod_k w_k * ----- (9) | |
7259 | * S_k | |
7260 | * | |
7261 | * Where | |
7262 | * | |
7263 | * s_k,i = \Sum_j w_i,j,k and S_k = \Sum_i s_k,i (10) | |
7264 | * | |
97fb7a0a | 7265 | * w_i,j,k is the weight of the j-th runnable task in the k-th cgroup on CPU i. |
e9c84cb8 PZ |
7266 | * |
7267 | * The big problem is S_k, its a global sum needed to compute a local (W_i) | |
7268 | * property. | |
7269 | * | |
7270 | * [XXX write more on how we solve this.. _after_ merging pjt's patches that | |
7271 | * rewrite all of this once again.] | |
97a7142f | 7272 | */ |
bf0f6f24 | 7273 | |
ed387b78 HS |
7274 | static unsigned long __read_mostly max_load_balance_interval = HZ/10; |
7275 | ||
0ec8aa00 PZ |
7276 | enum fbq_type { regular, remote, all }; |
7277 | ||
3b1baa64 MR |
7278 | enum group_type { |
7279 | group_other = 0, | |
7280 | group_misfit_task, | |
7281 | group_imbalanced, | |
7282 | group_overloaded, | |
7283 | }; | |
7284 | ||
ddcdf6e7 | 7285 | #define LBF_ALL_PINNED 0x01 |
367456c7 | 7286 | #define LBF_NEED_BREAK 0x02 |
6263322c PZ |
7287 | #define LBF_DST_PINNED 0x04 |
7288 | #define LBF_SOME_PINNED 0x08 | |
e022e0d3 | 7289 | #define LBF_NOHZ_STATS 0x10 |
f643ea22 | 7290 | #define LBF_NOHZ_AGAIN 0x20 |
ddcdf6e7 PZ |
7291 | |
7292 | struct lb_env { | |
7293 | struct sched_domain *sd; | |
7294 | ||
ddcdf6e7 | 7295 | struct rq *src_rq; |
85c1e7da | 7296 | int src_cpu; |
ddcdf6e7 PZ |
7297 | |
7298 | int dst_cpu; | |
7299 | struct rq *dst_rq; | |
7300 | ||
88b8dac0 SV |
7301 | struct cpumask *dst_grpmask; |
7302 | int new_dst_cpu; | |
ddcdf6e7 | 7303 | enum cpu_idle_type idle; |
bd939f45 | 7304 | long imbalance; |
b9403130 MW |
7305 | /* The set of CPUs under consideration for load-balancing */ |
7306 | struct cpumask *cpus; | |
7307 | ||
ddcdf6e7 | 7308 | unsigned int flags; |
367456c7 PZ |
7309 | |
7310 | unsigned int loop; | |
7311 | unsigned int loop_break; | |
7312 | unsigned int loop_max; | |
0ec8aa00 PZ |
7313 | |
7314 | enum fbq_type fbq_type; | |
cad68e55 | 7315 | enum group_type src_grp_type; |
163122b7 | 7316 | struct list_head tasks; |
ddcdf6e7 PZ |
7317 | }; |
7318 | ||
029632fb PZ |
7319 | /* |
7320 | * Is this task likely cache-hot: | |
7321 | */ | |
5d5e2b1b | 7322 | static int task_hot(struct task_struct *p, struct lb_env *env) |
029632fb PZ |
7323 | { |
7324 | s64 delta; | |
7325 | ||
e5673f28 KT |
7326 | lockdep_assert_held(&env->src_rq->lock); |
7327 | ||
029632fb PZ |
7328 | if (p->sched_class != &fair_sched_class) |
7329 | return 0; | |
7330 | ||
1da1843f | 7331 | if (unlikely(task_has_idle_policy(p))) |
029632fb PZ |
7332 | return 0; |
7333 | ||
7334 | /* | |
7335 | * Buddy candidates are cache hot: | |
7336 | */ | |
5d5e2b1b | 7337 | if (sched_feat(CACHE_HOT_BUDDY) && env->dst_rq->nr_running && |
029632fb PZ |
7338 | (&p->se == cfs_rq_of(&p->se)->next || |
7339 | &p->se == cfs_rq_of(&p->se)->last)) | |
7340 | return 1; | |
7341 | ||
7342 | if (sysctl_sched_migration_cost == -1) | |
7343 | return 1; | |
7344 | if (sysctl_sched_migration_cost == 0) | |
7345 | return 0; | |
7346 | ||
5d5e2b1b | 7347 | delta = rq_clock_task(env->src_rq) - p->se.exec_start; |
029632fb PZ |
7348 | |
7349 | return delta < (s64)sysctl_sched_migration_cost; | |
7350 | } | |
7351 | ||
3a7053b3 | 7352 | #ifdef CONFIG_NUMA_BALANCING |
c1ceac62 | 7353 | /* |
2a1ed24c SD |
7354 | * Returns 1, if task migration degrades locality |
7355 | * Returns 0, if task migration improves locality i.e migration preferred. | |
7356 | * Returns -1, if task migration is not affected by locality. | |
c1ceac62 | 7357 | */ |
2a1ed24c | 7358 | static int migrate_degrades_locality(struct task_struct *p, struct lb_env *env) |
3a7053b3 | 7359 | { |
b1ad065e | 7360 | struct numa_group *numa_group = rcu_dereference(p->numa_group); |
f35678b6 SD |
7361 | unsigned long src_weight, dst_weight; |
7362 | int src_nid, dst_nid, dist; | |
3a7053b3 | 7363 | |
2a595721 | 7364 | if (!static_branch_likely(&sched_numa_balancing)) |
2a1ed24c SD |
7365 | return -1; |
7366 | ||
c3b9bc5b | 7367 | if (!p->numa_faults || !(env->sd->flags & SD_NUMA)) |
2a1ed24c | 7368 | return -1; |
7a0f3083 MG |
7369 | |
7370 | src_nid = cpu_to_node(env->src_cpu); | |
7371 | dst_nid = cpu_to_node(env->dst_cpu); | |
7372 | ||
83e1d2cd | 7373 | if (src_nid == dst_nid) |
2a1ed24c | 7374 | return -1; |
7a0f3083 | 7375 | |
2a1ed24c SD |
7376 | /* Migrating away from the preferred node is always bad. */ |
7377 | if (src_nid == p->numa_preferred_nid) { | |
7378 | if (env->src_rq->nr_running > env->src_rq->nr_preferred_running) | |
7379 | return 1; | |
7380 | else | |
7381 | return -1; | |
7382 | } | |
b1ad065e | 7383 | |
c1ceac62 RR |
7384 | /* Encourage migration to the preferred node. */ |
7385 | if (dst_nid == p->numa_preferred_nid) | |
2a1ed24c | 7386 | return 0; |
b1ad065e | 7387 | |
739294fb | 7388 | /* Leaving a core idle is often worse than degrading locality. */ |
f35678b6 | 7389 | if (env->idle == CPU_IDLE) |
739294fb RR |
7390 | return -1; |
7391 | ||
f35678b6 | 7392 | dist = node_distance(src_nid, dst_nid); |
c1ceac62 | 7393 | if (numa_group) { |
f35678b6 SD |
7394 | src_weight = group_weight(p, src_nid, dist); |
7395 | dst_weight = group_weight(p, dst_nid, dist); | |
c1ceac62 | 7396 | } else { |
f35678b6 SD |
7397 | src_weight = task_weight(p, src_nid, dist); |
7398 | dst_weight = task_weight(p, dst_nid, dist); | |
b1ad065e RR |
7399 | } |
7400 | ||
f35678b6 | 7401 | return dst_weight < src_weight; |
7a0f3083 MG |
7402 | } |
7403 | ||
3a7053b3 | 7404 | #else |
2a1ed24c | 7405 | static inline int migrate_degrades_locality(struct task_struct *p, |
3a7053b3 MG |
7406 | struct lb_env *env) |
7407 | { | |
2a1ed24c | 7408 | return -1; |
7a0f3083 | 7409 | } |
3a7053b3 MG |
7410 | #endif |
7411 | ||
1e3c88bd PZ |
7412 | /* |
7413 | * can_migrate_task - may task p from runqueue rq be migrated to this_cpu? | |
7414 | */ | |
7415 | static | |
8e45cb54 | 7416 | int can_migrate_task(struct task_struct *p, struct lb_env *env) |
1e3c88bd | 7417 | { |
2a1ed24c | 7418 | int tsk_cache_hot; |
e5673f28 KT |
7419 | |
7420 | lockdep_assert_held(&env->src_rq->lock); | |
7421 | ||
1e3c88bd PZ |
7422 | /* |
7423 | * We do not migrate tasks that are: | |
d3198084 | 7424 | * 1) throttled_lb_pair, or |
1e3c88bd | 7425 | * 2) cannot be migrated to this CPU due to cpus_allowed, or |
d3198084 JK |
7426 | * 3) running (obviously), or |
7427 | * 4) are cache-hot on their current CPU. | |
1e3c88bd | 7428 | */ |
d3198084 JK |
7429 | if (throttled_lb_pair(task_group(p), env->src_cpu, env->dst_cpu)) |
7430 | return 0; | |
7431 | ||
0c98d344 | 7432 | if (!cpumask_test_cpu(env->dst_cpu, &p->cpus_allowed)) { |
e02e60c1 | 7433 | int cpu; |
88b8dac0 | 7434 | |
ae92882e | 7435 | schedstat_inc(p->se.statistics.nr_failed_migrations_affine); |
88b8dac0 | 7436 | |
6263322c PZ |
7437 | env->flags |= LBF_SOME_PINNED; |
7438 | ||
88b8dac0 | 7439 | /* |
97fb7a0a | 7440 | * Remember if this task can be migrated to any other CPU in |
88b8dac0 SV |
7441 | * our sched_group. We may want to revisit it if we couldn't |
7442 | * meet load balance goals by pulling other tasks on src_cpu. | |
7443 | * | |
65a4433a JH |
7444 | * Avoid computing new_dst_cpu for NEWLY_IDLE or if we have |
7445 | * already computed one in current iteration. | |
88b8dac0 | 7446 | */ |
65a4433a | 7447 | if (env->idle == CPU_NEWLY_IDLE || (env->flags & LBF_DST_PINNED)) |
88b8dac0 SV |
7448 | return 0; |
7449 | ||
97fb7a0a | 7450 | /* Prevent to re-select dst_cpu via env's CPUs: */ |
e02e60c1 | 7451 | for_each_cpu_and(cpu, env->dst_grpmask, env->cpus) { |
0c98d344 | 7452 | if (cpumask_test_cpu(cpu, &p->cpus_allowed)) { |
6263322c | 7453 | env->flags |= LBF_DST_PINNED; |
e02e60c1 JK |
7454 | env->new_dst_cpu = cpu; |
7455 | break; | |
7456 | } | |
88b8dac0 | 7457 | } |
e02e60c1 | 7458 | |
1e3c88bd PZ |
7459 | return 0; |
7460 | } | |
88b8dac0 SV |
7461 | |
7462 | /* Record that we found atleast one task that could run on dst_cpu */ | |
8e45cb54 | 7463 | env->flags &= ~LBF_ALL_PINNED; |
1e3c88bd | 7464 | |
ddcdf6e7 | 7465 | if (task_running(env->src_rq, p)) { |
ae92882e | 7466 | schedstat_inc(p->se.statistics.nr_failed_migrations_running); |
1e3c88bd PZ |
7467 | return 0; |
7468 | } | |
7469 | ||
7470 | /* | |
7471 | * Aggressive migration if: | |
3a7053b3 MG |
7472 | * 1) destination numa is preferred |
7473 | * 2) task is cache cold, or | |
7474 | * 3) too many balance attempts have failed. | |
1e3c88bd | 7475 | */ |
2a1ed24c SD |
7476 | tsk_cache_hot = migrate_degrades_locality(p, env); |
7477 | if (tsk_cache_hot == -1) | |
7478 | tsk_cache_hot = task_hot(p, env); | |
3a7053b3 | 7479 | |
2a1ed24c | 7480 | if (tsk_cache_hot <= 0 || |
7a96c231 | 7481 | env->sd->nr_balance_failed > env->sd->cache_nice_tries) { |
2a1ed24c | 7482 | if (tsk_cache_hot == 1) { |
ae92882e JP |
7483 | schedstat_inc(env->sd->lb_hot_gained[env->idle]); |
7484 | schedstat_inc(p->se.statistics.nr_forced_migrations); | |
3a7053b3 | 7485 | } |
1e3c88bd PZ |
7486 | return 1; |
7487 | } | |
7488 | ||
ae92882e | 7489 | schedstat_inc(p->se.statistics.nr_failed_migrations_hot); |
4e2dcb73 | 7490 | return 0; |
1e3c88bd PZ |
7491 | } |
7492 | ||
897c395f | 7493 | /* |
163122b7 KT |
7494 | * detach_task() -- detach the task for the migration specified in env |
7495 | */ | |
7496 | static void detach_task(struct task_struct *p, struct lb_env *env) | |
7497 | { | |
7498 | lockdep_assert_held(&env->src_rq->lock); | |
7499 | ||
163122b7 | 7500 | p->on_rq = TASK_ON_RQ_MIGRATING; |
5704ac0a | 7501 | deactivate_task(env->src_rq, p, DEQUEUE_NOCLOCK); |
163122b7 KT |
7502 | set_task_cpu(p, env->dst_cpu); |
7503 | } | |
7504 | ||
897c395f | 7505 | /* |
e5673f28 | 7506 | * detach_one_task() -- tries to dequeue exactly one task from env->src_rq, as |
897c395f | 7507 | * part of active balancing operations within "domain". |
897c395f | 7508 | * |
e5673f28 | 7509 | * Returns a task if successful and NULL otherwise. |
897c395f | 7510 | */ |
e5673f28 | 7511 | static struct task_struct *detach_one_task(struct lb_env *env) |
897c395f | 7512 | { |
93824900 | 7513 | struct task_struct *p; |
897c395f | 7514 | |
e5673f28 KT |
7515 | lockdep_assert_held(&env->src_rq->lock); |
7516 | ||
93824900 UR |
7517 | list_for_each_entry_reverse(p, |
7518 | &env->src_rq->cfs_tasks, se.group_node) { | |
367456c7 PZ |
7519 | if (!can_migrate_task(p, env)) |
7520 | continue; | |
897c395f | 7521 | |
163122b7 | 7522 | detach_task(p, env); |
e5673f28 | 7523 | |
367456c7 | 7524 | /* |
e5673f28 | 7525 | * Right now, this is only the second place where |
163122b7 | 7526 | * lb_gained[env->idle] is updated (other is detach_tasks) |
e5673f28 | 7527 | * so we can safely collect stats here rather than |
163122b7 | 7528 | * inside detach_tasks(). |
367456c7 | 7529 | */ |
ae92882e | 7530 | schedstat_inc(env->sd->lb_gained[env->idle]); |
e5673f28 | 7531 | return p; |
897c395f | 7532 | } |
e5673f28 | 7533 | return NULL; |
897c395f PZ |
7534 | } |
7535 | ||
eb95308e PZ |
7536 | static const unsigned int sched_nr_migrate_break = 32; |
7537 | ||
5d6523eb | 7538 | /* |
163122b7 KT |
7539 | * detach_tasks() -- tries to detach up to imbalance weighted load from |
7540 | * busiest_rq, as part of a balancing operation within domain "sd". | |
5d6523eb | 7541 | * |
163122b7 | 7542 | * Returns number of detached tasks if successful and 0 otherwise. |
5d6523eb | 7543 | */ |
163122b7 | 7544 | static int detach_tasks(struct lb_env *env) |
1e3c88bd | 7545 | { |
5d6523eb PZ |
7546 | struct list_head *tasks = &env->src_rq->cfs_tasks; |
7547 | struct task_struct *p; | |
367456c7 | 7548 | unsigned long load; |
163122b7 KT |
7549 | int detached = 0; |
7550 | ||
7551 | lockdep_assert_held(&env->src_rq->lock); | |
1e3c88bd | 7552 | |
bd939f45 | 7553 | if (env->imbalance <= 0) |
5d6523eb | 7554 | return 0; |
1e3c88bd | 7555 | |
5d6523eb | 7556 | while (!list_empty(tasks)) { |
985d3a4c YD |
7557 | /* |
7558 | * We don't want to steal all, otherwise we may be treated likewise, | |
7559 | * which could at worst lead to a livelock crash. | |
7560 | */ | |
7561 | if (env->idle != CPU_NOT_IDLE && env->src_rq->nr_running <= 1) | |
7562 | break; | |
7563 | ||
93824900 | 7564 | p = list_last_entry(tasks, struct task_struct, se.group_node); |
1e3c88bd | 7565 | |
367456c7 PZ |
7566 | env->loop++; |
7567 | /* We've more or less seen every task there is, call it quits */ | |
5d6523eb | 7568 | if (env->loop > env->loop_max) |
367456c7 | 7569 | break; |
5d6523eb PZ |
7570 | |
7571 | /* take a breather every nr_migrate tasks */ | |
367456c7 | 7572 | if (env->loop > env->loop_break) { |
eb95308e | 7573 | env->loop_break += sched_nr_migrate_break; |
8e45cb54 | 7574 | env->flags |= LBF_NEED_BREAK; |
ee00e66f | 7575 | break; |
a195f004 | 7576 | } |
1e3c88bd | 7577 | |
d3198084 | 7578 | if (!can_migrate_task(p, env)) |
367456c7 PZ |
7579 | goto next; |
7580 | ||
7581 | load = task_h_load(p); | |
5d6523eb | 7582 | |
eb95308e | 7583 | if (sched_feat(LB_MIN) && load < 16 && !env->sd->nr_balance_failed) |
367456c7 PZ |
7584 | goto next; |
7585 | ||
bd939f45 | 7586 | if ((load / 2) > env->imbalance) |
367456c7 | 7587 | goto next; |
1e3c88bd | 7588 | |
163122b7 KT |
7589 | detach_task(p, env); |
7590 | list_add(&p->se.group_node, &env->tasks); | |
7591 | ||
7592 | detached++; | |
bd939f45 | 7593 | env->imbalance -= load; |
1e3c88bd PZ |
7594 | |
7595 | #ifdef CONFIG_PREEMPT | |
ee00e66f PZ |
7596 | /* |
7597 | * NEWIDLE balancing is a source of latency, so preemptible | |
163122b7 | 7598 | * kernels will stop after the first task is detached to minimize |
ee00e66f PZ |
7599 | * the critical section. |
7600 | */ | |
5d6523eb | 7601 | if (env->idle == CPU_NEWLY_IDLE) |
ee00e66f | 7602 | break; |
1e3c88bd PZ |
7603 | #endif |
7604 | ||
ee00e66f PZ |
7605 | /* |
7606 | * We only want to steal up to the prescribed amount of | |
7607 | * weighted load. | |
7608 | */ | |
bd939f45 | 7609 | if (env->imbalance <= 0) |
ee00e66f | 7610 | break; |
367456c7 PZ |
7611 | |
7612 | continue; | |
7613 | next: | |
93824900 | 7614 | list_move(&p->se.group_node, tasks); |
1e3c88bd | 7615 | } |
5d6523eb | 7616 | |
1e3c88bd | 7617 | /* |
163122b7 KT |
7618 | * Right now, this is one of only two places we collect this stat |
7619 | * so we can safely collect detach_one_task() stats here rather | |
7620 | * than inside detach_one_task(). | |
1e3c88bd | 7621 | */ |
ae92882e | 7622 | schedstat_add(env->sd->lb_gained[env->idle], detached); |
1e3c88bd | 7623 | |
163122b7 KT |
7624 | return detached; |
7625 | } | |
7626 | ||
7627 | /* | |
7628 | * attach_task() -- attach the task detached by detach_task() to its new rq. | |
7629 | */ | |
7630 | static void attach_task(struct rq *rq, struct task_struct *p) | |
7631 | { | |
7632 | lockdep_assert_held(&rq->lock); | |
7633 | ||
7634 | BUG_ON(task_rq(p) != rq); | |
5704ac0a | 7635 | activate_task(rq, p, ENQUEUE_NOCLOCK); |
3ea94de1 | 7636 | p->on_rq = TASK_ON_RQ_QUEUED; |
163122b7 KT |
7637 | check_preempt_curr(rq, p, 0); |
7638 | } | |
7639 | ||
7640 | /* | |
7641 | * attach_one_task() -- attaches the task returned from detach_one_task() to | |
7642 | * its new rq. | |
7643 | */ | |
7644 | static void attach_one_task(struct rq *rq, struct task_struct *p) | |
7645 | { | |
8a8c69c3 PZ |
7646 | struct rq_flags rf; |
7647 | ||
7648 | rq_lock(rq, &rf); | |
5704ac0a | 7649 | update_rq_clock(rq); |
163122b7 | 7650 | attach_task(rq, p); |
8a8c69c3 | 7651 | rq_unlock(rq, &rf); |
163122b7 KT |
7652 | } |
7653 | ||
7654 | /* | |
7655 | * attach_tasks() -- attaches all tasks detached by detach_tasks() to their | |
7656 | * new rq. | |
7657 | */ | |
7658 | static void attach_tasks(struct lb_env *env) | |
7659 | { | |
7660 | struct list_head *tasks = &env->tasks; | |
7661 | struct task_struct *p; | |
8a8c69c3 | 7662 | struct rq_flags rf; |
163122b7 | 7663 | |
8a8c69c3 | 7664 | rq_lock(env->dst_rq, &rf); |
5704ac0a | 7665 | update_rq_clock(env->dst_rq); |
163122b7 KT |
7666 | |
7667 | while (!list_empty(tasks)) { | |
7668 | p = list_first_entry(tasks, struct task_struct, se.group_node); | |
7669 | list_del_init(&p->se.group_node); | |
1e3c88bd | 7670 | |
163122b7 KT |
7671 | attach_task(env->dst_rq, p); |
7672 | } | |
7673 | ||
8a8c69c3 | 7674 | rq_unlock(env->dst_rq, &rf); |
1e3c88bd PZ |
7675 | } |
7676 | ||
1936c53c VG |
7677 | static inline bool cfs_rq_has_blocked(struct cfs_rq *cfs_rq) |
7678 | { | |
7679 | if (cfs_rq->avg.load_avg) | |
7680 | return true; | |
7681 | ||
7682 | if (cfs_rq->avg.util_avg) | |
7683 | return true; | |
7684 | ||
7685 | return false; | |
7686 | } | |
7687 | ||
91c27493 | 7688 | static inline bool others_have_blocked(struct rq *rq) |
371bf427 VG |
7689 | { |
7690 | if (READ_ONCE(rq->avg_rt.util_avg)) | |
7691 | return true; | |
7692 | ||
3727e0e1 VG |
7693 | if (READ_ONCE(rq->avg_dl.util_avg)) |
7694 | return true; | |
7695 | ||
11d4afd4 | 7696 | #ifdef CONFIG_HAVE_SCHED_AVG_IRQ |
91c27493 VG |
7697 | if (READ_ONCE(rq->avg_irq.util_avg)) |
7698 | return true; | |
7699 | #endif | |
7700 | ||
371bf427 VG |
7701 | return false; |
7702 | } | |
7703 | ||
1936c53c VG |
7704 | #ifdef CONFIG_FAIR_GROUP_SCHED |
7705 | ||
039ae8bc VG |
7706 | static inline bool cfs_rq_is_decayed(struct cfs_rq *cfs_rq) |
7707 | { | |
7708 | if (cfs_rq->load.weight) | |
7709 | return false; | |
7710 | ||
7711 | if (cfs_rq->avg.load_sum) | |
7712 | return false; | |
7713 | ||
7714 | if (cfs_rq->avg.util_sum) | |
7715 | return false; | |
7716 | ||
7717 | if (cfs_rq->avg.runnable_load_sum) | |
7718 | return false; | |
7719 | ||
7720 | return true; | |
7721 | } | |
7722 | ||
48a16753 | 7723 | static void update_blocked_averages(int cpu) |
9e3081ca | 7724 | { |
9e3081ca | 7725 | struct rq *rq = cpu_rq(cpu); |
039ae8bc | 7726 | struct cfs_rq *cfs_rq, *pos; |
12b04875 | 7727 | const struct sched_class *curr_class; |
8a8c69c3 | 7728 | struct rq_flags rf; |
f643ea22 | 7729 | bool done = true; |
9e3081ca | 7730 | |
8a8c69c3 | 7731 | rq_lock_irqsave(rq, &rf); |
48a16753 | 7732 | update_rq_clock(rq); |
9d89c257 | 7733 | |
9763b67f PZ |
7734 | /* |
7735 | * Iterates the task_group tree in a bottom up fashion, see | |
7736 | * list_add_leaf_cfs_rq() for details. | |
7737 | */ | |
039ae8bc | 7738 | for_each_leaf_cfs_rq_safe(rq, cfs_rq, pos) { |
bc427898 VG |
7739 | struct sched_entity *se; |
7740 | ||
23127296 | 7741 | if (update_cfs_rq_load_avg(cfs_rq_clock_pelt(cfs_rq), cfs_rq)) |
9d89c257 | 7742 | update_tg_load_avg(cfs_rq, 0); |
4e516076 | 7743 | |
bc427898 VG |
7744 | /* Propagate pending load changes to the parent, if any: */ |
7745 | se = cfs_rq->tg->se[cpu]; | |
7746 | if (se && !skip_blocked_update(se)) | |
88c0616e | 7747 | update_load_avg(cfs_rq_of(se), se, 0); |
a9e7f654 | 7748 | |
039ae8bc VG |
7749 | /* |
7750 | * There can be a lot of idle CPU cgroups. Don't let fully | |
7751 | * decayed cfs_rqs linger on the list. | |
7752 | */ | |
7753 | if (cfs_rq_is_decayed(cfs_rq)) | |
7754 | list_del_leaf_cfs_rq(cfs_rq); | |
7755 | ||
1936c53c VG |
7756 | /* Don't need periodic decay once load/util_avg are null */ |
7757 | if (cfs_rq_has_blocked(cfs_rq)) | |
f643ea22 | 7758 | done = false; |
9d89c257 | 7759 | } |
12b04875 VG |
7760 | |
7761 | curr_class = rq->curr->sched_class; | |
23127296 VG |
7762 | update_rt_rq_load_avg(rq_clock_pelt(rq), rq, curr_class == &rt_sched_class); |
7763 | update_dl_rq_load_avg(rq_clock_pelt(rq), rq, curr_class == &dl_sched_class); | |
91c27493 | 7764 | update_irq_load_avg(rq, 0); |
371bf427 | 7765 | /* Don't need periodic decay once load/util_avg are null */ |
91c27493 | 7766 | if (others_have_blocked(rq)) |
371bf427 | 7767 | done = false; |
e022e0d3 PZ |
7768 | |
7769 | #ifdef CONFIG_NO_HZ_COMMON | |
7770 | rq->last_blocked_load_update_tick = jiffies; | |
f643ea22 VG |
7771 | if (done) |
7772 | rq->has_blocked_load = 0; | |
e022e0d3 | 7773 | #endif |
8a8c69c3 | 7774 | rq_unlock_irqrestore(rq, &rf); |
9e3081ca PZ |
7775 | } |
7776 | ||
9763b67f | 7777 | /* |
68520796 | 7778 | * Compute the hierarchical load factor for cfs_rq and all its ascendants. |
9763b67f PZ |
7779 | * This needs to be done in a top-down fashion because the load of a child |
7780 | * group is a fraction of its parents load. | |
7781 | */ | |
68520796 | 7782 | static void update_cfs_rq_h_load(struct cfs_rq *cfs_rq) |
9763b67f | 7783 | { |
68520796 VD |
7784 | struct rq *rq = rq_of(cfs_rq); |
7785 | struct sched_entity *se = cfs_rq->tg->se[cpu_of(rq)]; | |
a35b6466 | 7786 | unsigned long now = jiffies; |
68520796 | 7787 | unsigned long load; |
a35b6466 | 7788 | |
68520796 | 7789 | if (cfs_rq->last_h_load_update == now) |
a35b6466 PZ |
7790 | return; |
7791 | ||
68520796 VD |
7792 | cfs_rq->h_load_next = NULL; |
7793 | for_each_sched_entity(se) { | |
7794 | cfs_rq = cfs_rq_of(se); | |
7795 | cfs_rq->h_load_next = se; | |
7796 | if (cfs_rq->last_h_load_update == now) | |
7797 | break; | |
7798 | } | |
a35b6466 | 7799 | |
68520796 | 7800 | if (!se) { |
7ea241af | 7801 | cfs_rq->h_load = cfs_rq_load_avg(cfs_rq); |
68520796 VD |
7802 | cfs_rq->last_h_load_update = now; |
7803 | } | |
7804 | ||
7805 | while ((se = cfs_rq->h_load_next) != NULL) { | |
7806 | load = cfs_rq->h_load; | |
7ea241af YD |
7807 | load = div64_ul(load * se->avg.load_avg, |
7808 | cfs_rq_load_avg(cfs_rq) + 1); | |
68520796 VD |
7809 | cfs_rq = group_cfs_rq(se); |
7810 | cfs_rq->h_load = load; | |
7811 | cfs_rq->last_h_load_update = now; | |
7812 | } | |
9763b67f PZ |
7813 | } |
7814 | ||
367456c7 | 7815 | static unsigned long task_h_load(struct task_struct *p) |
230059de | 7816 | { |
367456c7 | 7817 | struct cfs_rq *cfs_rq = task_cfs_rq(p); |
230059de | 7818 | |
68520796 | 7819 | update_cfs_rq_h_load(cfs_rq); |
9d89c257 | 7820 | return div64_ul(p->se.avg.load_avg * cfs_rq->h_load, |
7ea241af | 7821 | cfs_rq_load_avg(cfs_rq) + 1); |
230059de PZ |
7822 | } |
7823 | #else | |
48a16753 | 7824 | static inline void update_blocked_averages(int cpu) |
9e3081ca | 7825 | { |
6c1d47c0 VG |
7826 | struct rq *rq = cpu_rq(cpu); |
7827 | struct cfs_rq *cfs_rq = &rq->cfs; | |
12b04875 | 7828 | const struct sched_class *curr_class; |
8a8c69c3 | 7829 | struct rq_flags rf; |
6c1d47c0 | 7830 | |
8a8c69c3 | 7831 | rq_lock_irqsave(rq, &rf); |
6c1d47c0 | 7832 | update_rq_clock(rq); |
23127296 | 7833 | update_cfs_rq_load_avg(cfs_rq_clock_pelt(cfs_rq), cfs_rq); |
12b04875 VG |
7834 | |
7835 | curr_class = rq->curr->sched_class; | |
23127296 VG |
7836 | update_rt_rq_load_avg(rq_clock_pelt(rq), rq, curr_class == &rt_sched_class); |
7837 | update_dl_rq_load_avg(rq_clock_pelt(rq), rq, curr_class == &dl_sched_class); | |
91c27493 | 7838 | update_irq_load_avg(rq, 0); |
e022e0d3 PZ |
7839 | #ifdef CONFIG_NO_HZ_COMMON |
7840 | rq->last_blocked_load_update_tick = jiffies; | |
91c27493 | 7841 | if (!cfs_rq_has_blocked(cfs_rq) && !others_have_blocked(rq)) |
f643ea22 | 7842 | rq->has_blocked_load = 0; |
e022e0d3 | 7843 | #endif |
8a8c69c3 | 7844 | rq_unlock_irqrestore(rq, &rf); |
9e3081ca PZ |
7845 | } |
7846 | ||
367456c7 | 7847 | static unsigned long task_h_load(struct task_struct *p) |
1e3c88bd | 7848 | { |
9d89c257 | 7849 | return p->se.avg.load_avg; |
1e3c88bd | 7850 | } |
230059de | 7851 | #endif |
1e3c88bd | 7852 | |
1e3c88bd | 7853 | /********** Helpers for find_busiest_group ************************/ |
caeb178c | 7854 | |
1e3c88bd PZ |
7855 | /* |
7856 | * sg_lb_stats - stats of a sched_group required for load_balancing | |
7857 | */ | |
7858 | struct sg_lb_stats { | |
7859 | unsigned long avg_load; /*Avg load across the CPUs of the group */ | |
7860 | unsigned long group_load; /* Total load over the CPUs of the group */ | |
1e3c88bd | 7861 | unsigned long sum_weighted_load; /* Weighted load of group's tasks */ |
56cf515b | 7862 | unsigned long load_per_task; |
63b2ca30 | 7863 | unsigned long group_capacity; |
9e91d61d | 7864 | unsigned long group_util; /* Total utilization of the group */ |
147c5fc2 | 7865 | unsigned int sum_nr_running; /* Nr tasks running in the group */ |
147c5fc2 PZ |
7866 | unsigned int idle_cpus; |
7867 | unsigned int group_weight; | |
caeb178c | 7868 | enum group_type group_type; |
ea67821b | 7869 | int group_no_capacity; |
3b1baa64 | 7870 | unsigned long group_misfit_task_load; /* A CPU has a task too big for its capacity */ |
0ec8aa00 PZ |
7871 | #ifdef CONFIG_NUMA_BALANCING |
7872 | unsigned int nr_numa_running; | |
7873 | unsigned int nr_preferred_running; | |
7874 | #endif | |
1e3c88bd PZ |
7875 | }; |
7876 | ||
56cf515b JK |
7877 | /* |
7878 | * sd_lb_stats - Structure to store the statistics of a sched_domain | |
7879 | * during load balancing. | |
7880 | */ | |
7881 | struct sd_lb_stats { | |
7882 | struct sched_group *busiest; /* Busiest group in this sd */ | |
7883 | struct sched_group *local; /* Local group in this sd */ | |
90001d67 | 7884 | unsigned long total_running; |
56cf515b | 7885 | unsigned long total_load; /* Total load of all groups in sd */ |
63b2ca30 | 7886 | unsigned long total_capacity; /* Total capacity of all groups in sd */ |
56cf515b JK |
7887 | unsigned long avg_load; /* Average load across all groups in sd */ |
7888 | ||
56cf515b | 7889 | struct sg_lb_stats busiest_stat;/* Statistics of the busiest group */ |
147c5fc2 | 7890 | struct sg_lb_stats local_stat; /* Statistics of the local group */ |
56cf515b JK |
7891 | }; |
7892 | ||
147c5fc2 PZ |
7893 | static inline void init_sd_lb_stats(struct sd_lb_stats *sds) |
7894 | { | |
7895 | /* | |
7896 | * Skimp on the clearing to avoid duplicate work. We can avoid clearing | |
7897 | * local_stat because update_sg_lb_stats() does a full clear/assignment. | |
7898 | * We must however clear busiest_stat::avg_load because | |
7899 | * update_sd_pick_busiest() reads this before assignment. | |
7900 | */ | |
7901 | *sds = (struct sd_lb_stats){ | |
7902 | .busiest = NULL, | |
7903 | .local = NULL, | |
90001d67 | 7904 | .total_running = 0UL, |
147c5fc2 | 7905 | .total_load = 0UL, |
63b2ca30 | 7906 | .total_capacity = 0UL, |
147c5fc2 PZ |
7907 | .busiest_stat = { |
7908 | .avg_load = 0UL, | |
caeb178c RR |
7909 | .sum_nr_running = 0, |
7910 | .group_type = group_other, | |
147c5fc2 PZ |
7911 | }, |
7912 | }; | |
7913 | } | |
7914 | ||
1e3c88bd PZ |
7915 | /** |
7916 | * get_sd_load_idx - Obtain the load index for a given sched domain. | |
7917 | * @sd: The sched_domain whose load_idx is to be obtained. | |
ed1b7732 | 7918 | * @idle: The idle status of the CPU for whose sd load_idx is obtained. |
e69f6186 YB |
7919 | * |
7920 | * Return: The load index. | |
1e3c88bd PZ |
7921 | */ |
7922 | static inline int get_sd_load_idx(struct sched_domain *sd, | |
7923 | enum cpu_idle_type idle) | |
7924 | { | |
7925 | int load_idx; | |
7926 | ||
7927 | switch (idle) { | |
7928 | case CPU_NOT_IDLE: | |
7929 | load_idx = sd->busy_idx; | |
7930 | break; | |
7931 | ||
7932 | case CPU_NEWLY_IDLE: | |
7933 | load_idx = sd->newidle_idx; | |
7934 | break; | |
7935 | default: | |
7936 | load_idx = sd->idle_idx; | |
7937 | break; | |
7938 | } | |
7939 | ||
7940 | return load_idx; | |
7941 | } | |
7942 | ||
287cdaac | 7943 | static unsigned long scale_rt_capacity(struct sched_domain *sd, int cpu) |
1e3c88bd PZ |
7944 | { |
7945 | struct rq *rq = cpu_rq(cpu); | |
287cdaac | 7946 | unsigned long max = arch_scale_cpu_capacity(sd, cpu); |
523e979d | 7947 | unsigned long used, free; |
523e979d | 7948 | unsigned long irq; |
b654f7de | 7949 | |
2e62c474 | 7950 | irq = cpu_util_irq(rq); |
cadefd3d | 7951 | |
523e979d VG |
7952 | if (unlikely(irq >= max)) |
7953 | return 1; | |
aa483808 | 7954 | |
523e979d VG |
7955 | used = READ_ONCE(rq->avg_rt.util_avg); |
7956 | used += READ_ONCE(rq->avg_dl.util_avg); | |
1e3c88bd | 7957 | |
523e979d VG |
7958 | if (unlikely(used >= max)) |
7959 | return 1; | |
1e3c88bd | 7960 | |
523e979d | 7961 | free = max - used; |
2e62c474 VG |
7962 | |
7963 | return scale_irq_capacity(free, irq, max); | |
1e3c88bd PZ |
7964 | } |
7965 | ||
ced549fa | 7966 | static void update_cpu_capacity(struct sched_domain *sd, int cpu) |
1e3c88bd | 7967 | { |
287cdaac | 7968 | unsigned long capacity = scale_rt_capacity(sd, cpu); |
1e3c88bd PZ |
7969 | struct sched_group *sdg = sd->groups; |
7970 | ||
523e979d | 7971 | cpu_rq(cpu)->cpu_capacity_orig = arch_scale_cpu_capacity(sd, cpu); |
1e3c88bd | 7972 | |
ced549fa NP |
7973 | if (!capacity) |
7974 | capacity = 1; | |
1e3c88bd | 7975 | |
ced549fa NP |
7976 | cpu_rq(cpu)->cpu_capacity = capacity; |
7977 | sdg->sgc->capacity = capacity; | |
bf475ce0 | 7978 | sdg->sgc->min_capacity = capacity; |
e3d6d0cb | 7979 | sdg->sgc->max_capacity = capacity; |
1e3c88bd PZ |
7980 | } |
7981 | ||
63b2ca30 | 7982 | void update_group_capacity(struct sched_domain *sd, int cpu) |
1e3c88bd PZ |
7983 | { |
7984 | struct sched_domain *child = sd->child; | |
7985 | struct sched_group *group, *sdg = sd->groups; | |
e3d6d0cb | 7986 | unsigned long capacity, min_capacity, max_capacity; |
4ec4412e VG |
7987 | unsigned long interval; |
7988 | ||
7989 | interval = msecs_to_jiffies(sd->balance_interval); | |
7990 | interval = clamp(interval, 1UL, max_load_balance_interval); | |
63b2ca30 | 7991 | sdg->sgc->next_update = jiffies + interval; |
1e3c88bd PZ |
7992 | |
7993 | if (!child) { | |
ced549fa | 7994 | update_cpu_capacity(sd, cpu); |
1e3c88bd PZ |
7995 | return; |
7996 | } | |
7997 | ||
dc7ff76e | 7998 | capacity = 0; |
bf475ce0 | 7999 | min_capacity = ULONG_MAX; |
e3d6d0cb | 8000 | max_capacity = 0; |
1e3c88bd | 8001 | |
74a5ce20 PZ |
8002 | if (child->flags & SD_OVERLAP) { |
8003 | /* | |
8004 | * SD_OVERLAP domains cannot assume that child groups | |
8005 | * span the current group. | |
8006 | */ | |
8007 | ||
ae4df9d6 | 8008 | for_each_cpu(cpu, sched_group_span(sdg)) { |
63b2ca30 | 8009 | struct sched_group_capacity *sgc; |
9abf24d4 | 8010 | struct rq *rq = cpu_rq(cpu); |
863bffc8 | 8011 | |
9abf24d4 | 8012 | /* |
63b2ca30 | 8013 | * build_sched_domains() -> init_sched_groups_capacity() |
9abf24d4 SD |
8014 | * gets here before we've attached the domains to the |
8015 | * runqueues. | |
8016 | * | |
ced549fa NP |
8017 | * Use capacity_of(), which is set irrespective of domains |
8018 | * in update_cpu_capacity(). | |
9abf24d4 | 8019 | * |
dc7ff76e | 8020 | * This avoids capacity from being 0 and |
9abf24d4 | 8021 | * causing divide-by-zero issues on boot. |
9abf24d4 SD |
8022 | */ |
8023 | if (unlikely(!rq->sd)) { | |
ced549fa | 8024 | capacity += capacity_of(cpu); |
bf475ce0 MR |
8025 | } else { |
8026 | sgc = rq->sd->groups->sgc; | |
8027 | capacity += sgc->capacity; | |
9abf24d4 | 8028 | } |
863bffc8 | 8029 | |
bf475ce0 | 8030 | min_capacity = min(capacity, min_capacity); |
e3d6d0cb | 8031 | max_capacity = max(capacity, max_capacity); |
863bffc8 | 8032 | } |
74a5ce20 PZ |
8033 | } else { |
8034 | /* | |
8035 | * !SD_OVERLAP domains can assume that child groups | |
8036 | * span the current group. | |
97a7142f | 8037 | */ |
74a5ce20 PZ |
8038 | |
8039 | group = child->groups; | |
8040 | do { | |
bf475ce0 MR |
8041 | struct sched_group_capacity *sgc = group->sgc; |
8042 | ||
8043 | capacity += sgc->capacity; | |
8044 | min_capacity = min(sgc->min_capacity, min_capacity); | |
e3d6d0cb | 8045 | max_capacity = max(sgc->max_capacity, max_capacity); |
74a5ce20 PZ |
8046 | group = group->next; |
8047 | } while (group != child->groups); | |
8048 | } | |
1e3c88bd | 8049 | |
63b2ca30 | 8050 | sdg->sgc->capacity = capacity; |
bf475ce0 | 8051 | sdg->sgc->min_capacity = min_capacity; |
e3d6d0cb | 8052 | sdg->sgc->max_capacity = max_capacity; |
1e3c88bd PZ |
8053 | } |
8054 | ||
9d5efe05 | 8055 | /* |
ea67821b VG |
8056 | * Check whether the capacity of the rq has been noticeably reduced by side |
8057 | * activity. The imbalance_pct is used for the threshold. | |
8058 | * Return true is the capacity is reduced | |
9d5efe05 SV |
8059 | */ |
8060 | static inline int | |
ea67821b | 8061 | check_cpu_capacity(struct rq *rq, struct sched_domain *sd) |
9d5efe05 | 8062 | { |
ea67821b VG |
8063 | return ((rq->cpu_capacity * sd->imbalance_pct) < |
8064 | (rq->cpu_capacity_orig * 100)); | |
9d5efe05 SV |
8065 | } |
8066 | ||
30ce5dab PZ |
8067 | /* |
8068 | * Group imbalance indicates (and tries to solve) the problem where balancing | |
0c98d344 | 8069 | * groups is inadequate due to ->cpus_allowed constraints. |
30ce5dab | 8070 | * |
97fb7a0a IM |
8071 | * Imagine a situation of two groups of 4 CPUs each and 4 tasks each with a |
8072 | * cpumask covering 1 CPU of the first group and 3 CPUs of the second group. | |
30ce5dab PZ |
8073 | * Something like: |
8074 | * | |
2b4d5b25 IM |
8075 | * { 0 1 2 3 } { 4 5 6 7 } |
8076 | * * * * * | |
30ce5dab PZ |
8077 | * |
8078 | * If we were to balance group-wise we'd place two tasks in the first group and | |
8079 | * two tasks in the second group. Clearly this is undesired as it will overload | |
97fb7a0a | 8080 | * cpu 3 and leave one of the CPUs in the second group unused. |
30ce5dab PZ |
8081 | * |
8082 | * The current solution to this issue is detecting the skew in the first group | |
6263322c PZ |
8083 | * by noticing the lower domain failed to reach balance and had difficulty |
8084 | * moving tasks due to affinity constraints. | |
30ce5dab PZ |
8085 | * |
8086 | * When this is so detected; this group becomes a candidate for busiest; see | |
ed1b7732 | 8087 | * update_sd_pick_busiest(). And calculate_imbalance() and |
6263322c | 8088 | * find_busiest_group() avoid some of the usual balance conditions to allow it |
30ce5dab PZ |
8089 | * to create an effective group imbalance. |
8090 | * | |
8091 | * This is a somewhat tricky proposition since the next run might not find the | |
8092 | * group imbalance and decide the groups need to be balanced again. A most | |
8093 | * subtle and fragile situation. | |
8094 | */ | |
8095 | ||
6263322c | 8096 | static inline int sg_imbalanced(struct sched_group *group) |
30ce5dab | 8097 | { |
63b2ca30 | 8098 | return group->sgc->imbalance; |
30ce5dab PZ |
8099 | } |
8100 | ||
b37d9316 | 8101 | /* |
ea67821b VG |
8102 | * group_has_capacity returns true if the group has spare capacity that could |
8103 | * be used by some tasks. | |
8104 | * We consider that a group has spare capacity if the * number of task is | |
9e91d61d DE |
8105 | * smaller than the number of CPUs or if the utilization is lower than the |
8106 | * available capacity for CFS tasks. | |
ea67821b VG |
8107 | * For the latter, we use a threshold to stabilize the state, to take into |
8108 | * account the variance of the tasks' load and to return true if the available | |
8109 | * capacity in meaningful for the load balancer. | |
8110 | * As an example, an available capacity of 1% can appear but it doesn't make | |
8111 | * any benefit for the load balance. | |
b37d9316 | 8112 | */ |
ea67821b VG |
8113 | static inline bool |
8114 | group_has_capacity(struct lb_env *env, struct sg_lb_stats *sgs) | |
b37d9316 | 8115 | { |
ea67821b VG |
8116 | if (sgs->sum_nr_running < sgs->group_weight) |
8117 | return true; | |
c61037e9 | 8118 | |
ea67821b | 8119 | if ((sgs->group_capacity * 100) > |
9e91d61d | 8120 | (sgs->group_util * env->sd->imbalance_pct)) |
ea67821b | 8121 | return true; |
b37d9316 | 8122 | |
ea67821b VG |
8123 | return false; |
8124 | } | |
8125 | ||
8126 | /* | |
8127 | * group_is_overloaded returns true if the group has more tasks than it can | |
8128 | * handle. | |
8129 | * group_is_overloaded is not equals to !group_has_capacity because a group | |
8130 | * with the exact right number of tasks, has no more spare capacity but is not | |
8131 | * overloaded so both group_has_capacity and group_is_overloaded return | |
8132 | * false. | |
8133 | */ | |
8134 | static inline bool | |
8135 | group_is_overloaded(struct lb_env *env, struct sg_lb_stats *sgs) | |
8136 | { | |
8137 | if (sgs->sum_nr_running <= sgs->group_weight) | |
8138 | return false; | |
b37d9316 | 8139 | |
ea67821b | 8140 | if ((sgs->group_capacity * 100) < |
9e91d61d | 8141 | (sgs->group_util * env->sd->imbalance_pct)) |
ea67821b | 8142 | return true; |
b37d9316 | 8143 | |
ea67821b | 8144 | return false; |
b37d9316 PZ |
8145 | } |
8146 | ||
9e0994c0 | 8147 | /* |
e3d6d0cb | 8148 | * group_smaller_min_cpu_capacity: Returns true if sched_group sg has smaller |
9e0994c0 MR |
8149 | * per-CPU capacity than sched_group ref. |
8150 | */ | |
8151 | static inline bool | |
e3d6d0cb | 8152 | group_smaller_min_cpu_capacity(struct sched_group *sg, struct sched_group *ref) |
9e0994c0 MR |
8153 | { |
8154 | return sg->sgc->min_capacity * capacity_margin < | |
8155 | ref->sgc->min_capacity * 1024; | |
8156 | } | |
8157 | ||
e3d6d0cb MR |
8158 | /* |
8159 | * group_smaller_max_cpu_capacity: Returns true if sched_group sg has smaller | |
8160 | * per-CPU capacity_orig than sched_group ref. | |
8161 | */ | |
8162 | static inline bool | |
8163 | group_smaller_max_cpu_capacity(struct sched_group *sg, struct sched_group *ref) | |
8164 | { | |
8165 | return sg->sgc->max_capacity * capacity_margin < | |
8166 | ref->sgc->max_capacity * 1024; | |
8167 | } | |
8168 | ||
79a89f92 LY |
8169 | static inline enum |
8170 | group_type group_classify(struct sched_group *group, | |
8171 | struct sg_lb_stats *sgs) | |
caeb178c | 8172 | { |
ea67821b | 8173 | if (sgs->group_no_capacity) |
caeb178c RR |
8174 | return group_overloaded; |
8175 | ||
8176 | if (sg_imbalanced(group)) | |
8177 | return group_imbalanced; | |
8178 | ||
3b1baa64 MR |
8179 | if (sgs->group_misfit_task_load) |
8180 | return group_misfit_task; | |
8181 | ||
caeb178c RR |
8182 | return group_other; |
8183 | } | |
8184 | ||
63928384 | 8185 | static bool update_nohz_stats(struct rq *rq, bool force) |
e022e0d3 PZ |
8186 | { |
8187 | #ifdef CONFIG_NO_HZ_COMMON | |
8188 | unsigned int cpu = rq->cpu; | |
8189 | ||
f643ea22 VG |
8190 | if (!rq->has_blocked_load) |
8191 | return false; | |
8192 | ||
e022e0d3 | 8193 | if (!cpumask_test_cpu(cpu, nohz.idle_cpus_mask)) |
f643ea22 | 8194 | return false; |
e022e0d3 | 8195 | |
63928384 | 8196 | if (!force && !time_after(jiffies, rq->last_blocked_load_update_tick)) |
f643ea22 | 8197 | return true; |
e022e0d3 PZ |
8198 | |
8199 | update_blocked_averages(cpu); | |
f643ea22 VG |
8200 | |
8201 | return rq->has_blocked_load; | |
8202 | #else | |
8203 | return false; | |
e022e0d3 PZ |
8204 | #endif |
8205 | } | |
8206 | ||
1e3c88bd PZ |
8207 | /** |
8208 | * update_sg_lb_stats - Update sched_group's statistics for load balancing. | |
cd96891d | 8209 | * @env: The load balancing environment. |
1e3c88bd | 8210 | * @group: sched_group whose statistics are to be updated. |
1e3c88bd | 8211 | * @sgs: variable to hold the statistics for this group. |
630246a0 | 8212 | * @sg_status: Holds flag indicating the status of the sched_group |
1e3c88bd | 8213 | */ |
bd939f45 | 8214 | static inline void update_sg_lb_stats(struct lb_env *env, |
630246a0 QP |
8215 | struct sched_group *group, |
8216 | struct sg_lb_stats *sgs, | |
8217 | int *sg_status) | |
1e3c88bd | 8218 | { |
630246a0 QP |
8219 | int local_group = cpumask_test_cpu(env->dst_cpu, sched_group_span(group)); |
8220 | int load_idx = get_sd_load_idx(env->sd, env->idle); | |
30ce5dab | 8221 | unsigned long load; |
a426f99c | 8222 | int i, nr_running; |
1e3c88bd | 8223 | |
b72ff13c PZ |
8224 | memset(sgs, 0, sizeof(*sgs)); |
8225 | ||
ae4df9d6 | 8226 | for_each_cpu_and(i, sched_group_span(group), env->cpus) { |
1e3c88bd PZ |
8227 | struct rq *rq = cpu_rq(i); |
8228 | ||
63928384 | 8229 | if ((env->flags & LBF_NOHZ_STATS) && update_nohz_stats(rq, false)) |
f643ea22 | 8230 | env->flags |= LBF_NOHZ_AGAIN; |
e022e0d3 | 8231 | |
97fb7a0a | 8232 | /* Bias balancing toward CPUs of our domain: */ |
6263322c | 8233 | if (local_group) |
04f733b4 | 8234 | load = target_load(i, load_idx); |
6263322c | 8235 | else |
1e3c88bd | 8236 | load = source_load(i, load_idx); |
1e3c88bd PZ |
8237 | |
8238 | sgs->group_load += load; | |
9e91d61d | 8239 | sgs->group_util += cpu_util(i); |
65fdac08 | 8240 | sgs->sum_nr_running += rq->cfs.h_nr_running; |
4486edd1 | 8241 | |
a426f99c WL |
8242 | nr_running = rq->nr_running; |
8243 | if (nr_running > 1) | |
630246a0 | 8244 | *sg_status |= SG_OVERLOAD; |
4486edd1 | 8245 | |
2802bf3c MR |
8246 | if (cpu_overutilized(i)) |
8247 | *sg_status |= SG_OVERUTILIZED; | |
4486edd1 | 8248 | |
0ec8aa00 PZ |
8249 | #ifdef CONFIG_NUMA_BALANCING |
8250 | sgs->nr_numa_running += rq->nr_numa_running; | |
8251 | sgs->nr_preferred_running += rq->nr_preferred_running; | |
8252 | #endif | |
c7132dd6 | 8253 | sgs->sum_weighted_load += weighted_cpuload(rq); |
a426f99c WL |
8254 | /* |
8255 | * No need to call idle_cpu() if nr_running is not 0 | |
8256 | */ | |
8257 | if (!nr_running && idle_cpu(i)) | |
aae6d3dd | 8258 | sgs->idle_cpus++; |
3b1baa64 MR |
8259 | |
8260 | if (env->sd->flags & SD_ASYM_CPUCAPACITY && | |
757ffdd7 | 8261 | sgs->group_misfit_task_load < rq->misfit_task_load) { |
3b1baa64 | 8262 | sgs->group_misfit_task_load = rq->misfit_task_load; |
630246a0 | 8263 | *sg_status |= SG_OVERLOAD; |
757ffdd7 | 8264 | } |
1e3c88bd PZ |
8265 | } |
8266 | ||
63b2ca30 NP |
8267 | /* Adjust by relative CPU capacity of the group */ |
8268 | sgs->group_capacity = group->sgc->capacity; | |
ca8ce3d0 | 8269 | sgs->avg_load = (sgs->group_load*SCHED_CAPACITY_SCALE) / sgs->group_capacity; |
1e3c88bd | 8270 | |
dd5feea1 | 8271 | if (sgs->sum_nr_running) |
38d0f770 | 8272 | sgs->load_per_task = sgs->sum_weighted_load / sgs->sum_nr_running; |
1e3c88bd | 8273 | |
aae6d3dd | 8274 | sgs->group_weight = group->group_weight; |
b37d9316 | 8275 | |
ea67821b | 8276 | sgs->group_no_capacity = group_is_overloaded(env, sgs); |
79a89f92 | 8277 | sgs->group_type = group_classify(group, sgs); |
1e3c88bd PZ |
8278 | } |
8279 | ||
532cb4c4 MN |
8280 | /** |
8281 | * update_sd_pick_busiest - return 1 on busiest group | |
cd96891d | 8282 | * @env: The load balancing environment. |
532cb4c4 MN |
8283 | * @sds: sched_domain statistics |
8284 | * @sg: sched_group candidate to be checked for being the busiest | |
b6b12294 | 8285 | * @sgs: sched_group statistics |
532cb4c4 MN |
8286 | * |
8287 | * Determine if @sg is a busier group than the previously selected | |
8288 | * busiest group. | |
e69f6186 YB |
8289 | * |
8290 | * Return: %true if @sg is a busier group than the previously selected | |
8291 | * busiest group. %false otherwise. | |
532cb4c4 | 8292 | */ |
bd939f45 | 8293 | static bool update_sd_pick_busiest(struct lb_env *env, |
532cb4c4 MN |
8294 | struct sd_lb_stats *sds, |
8295 | struct sched_group *sg, | |
bd939f45 | 8296 | struct sg_lb_stats *sgs) |
532cb4c4 | 8297 | { |
caeb178c | 8298 | struct sg_lb_stats *busiest = &sds->busiest_stat; |
532cb4c4 | 8299 | |
cad68e55 MR |
8300 | /* |
8301 | * Don't try to pull misfit tasks we can't help. | |
8302 | * We can use max_capacity here as reduction in capacity on some | |
8303 | * CPUs in the group should either be possible to resolve | |
8304 | * internally or be covered by avg_load imbalance (eventually). | |
8305 | */ | |
8306 | if (sgs->group_type == group_misfit_task && | |
8307 | (!group_smaller_max_cpu_capacity(sg, sds->local) || | |
8308 | !group_has_capacity(env, &sds->local_stat))) | |
8309 | return false; | |
8310 | ||
caeb178c | 8311 | if (sgs->group_type > busiest->group_type) |
532cb4c4 MN |
8312 | return true; |
8313 | ||
caeb178c RR |
8314 | if (sgs->group_type < busiest->group_type) |
8315 | return false; | |
8316 | ||
8317 | if (sgs->avg_load <= busiest->avg_load) | |
8318 | return false; | |
8319 | ||
9e0994c0 MR |
8320 | if (!(env->sd->flags & SD_ASYM_CPUCAPACITY)) |
8321 | goto asym_packing; | |
8322 | ||
8323 | /* | |
8324 | * Candidate sg has no more than one task per CPU and | |
8325 | * has higher per-CPU capacity. Migrating tasks to less | |
8326 | * capable CPUs may harm throughput. Maximize throughput, | |
8327 | * power/energy consequences are not considered. | |
8328 | */ | |
8329 | if (sgs->sum_nr_running <= sgs->group_weight && | |
e3d6d0cb | 8330 | group_smaller_min_cpu_capacity(sds->local, sg)) |
9e0994c0 MR |
8331 | return false; |
8332 | ||
cad68e55 MR |
8333 | /* |
8334 | * If we have more than one misfit sg go with the biggest misfit. | |
8335 | */ | |
8336 | if (sgs->group_type == group_misfit_task && | |
8337 | sgs->group_misfit_task_load < busiest->group_misfit_task_load) | |
9e0994c0 MR |
8338 | return false; |
8339 | ||
8340 | asym_packing: | |
caeb178c RR |
8341 | /* This is the busiest node in its class. */ |
8342 | if (!(env->sd->flags & SD_ASYM_PACKING)) | |
532cb4c4 MN |
8343 | return true; |
8344 | ||
97fb7a0a | 8345 | /* No ASYM_PACKING if target CPU is already busy */ |
1f621e02 SD |
8346 | if (env->idle == CPU_NOT_IDLE) |
8347 | return true; | |
532cb4c4 | 8348 | /* |
afe06efd TC |
8349 | * ASYM_PACKING needs to move all the work to the highest |
8350 | * prority CPUs in the group, therefore mark all groups | |
8351 | * of lower priority than ourself as busy. | |
532cb4c4 | 8352 | */ |
afe06efd TC |
8353 | if (sgs->sum_nr_running && |
8354 | sched_asym_prefer(env->dst_cpu, sg->asym_prefer_cpu)) { | |
532cb4c4 MN |
8355 | if (!sds->busiest) |
8356 | return true; | |
8357 | ||
97fb7a0a | 8358 | /* Prefer to move from lowest priority CPU's work */ |
afe06efd TC |
8359 | if (sched_asym_prefer(sds->busiest->asym_prefer_cpu, |
8360 | sg->asym_prefer_cpu)) | |
532cb4c4 MN |
8361 | return true; |
8362 | } | |
8363 | ||
8364 | return false; | |
8365 | } | |
8366 | ||
0ec8aa00 PZ |
8367 | #ifdef CONFIG_NUMA_BALANCING |
8368 | static inline enum fbq_type fbq_classify_group(struct sg_lb_stats *sgs) | |
8369 | { | |
8370 | if (sgs->sum_nr_running > sgs->nr_numa_running) | |
8371 | return regular; | |
8372 | if (sgs->sum_nr_running > sgs->nr_preferred_running) | |
8373 | return remote; | |
8374 | return all; | |
8375 | } | |
8376 | ||
8377 | static inline enum fbq_type fbq_classify_rq(struct rq *rq) | |
8378 | { | |
8379 | if (rq->nr_running > rq->nr_numa_running) | |
8380 | return regular; | |
8381 | if (rq->nr_running > rq->nr_preferred_running) | |
8382 | return remote; | |
8383 | return all; | |
8384 | } | |
8385 | #else | |
8386 | static inline enum fbq_type fbq_classify_group(struct sg_lb_stats *sgs) | |
8387 | { | |
8388 | return all; | |
8389 | } | |
8390 | ||
8391 | static inline enum fbq_type fbq_classify_rq(struct rq *rq) | |
8392 | { | |
8393 | return regular; | |
8394 | } | |
8395 | #endif /* CONFIG_NUMA_BALANCING */ | |
8396 | ||
1e3c88bd | 8397 | /** |
461819ac | 8398 | * update_sd_lb_stats - Update sched_domain's statistics for load balancing. |
cd96891d | 8399 | * @env: The load balancing environment. |
1e3c88bd PZ |
8400 | * @sds: variable to hold the statistics for this sched_domain. |
8401 | */ | |
0ec8aa00 | 8402 | static inline void update_sd_lb_stats(struct lb_env *env, struct sd_lb_stats *sds) |
1e3c88bd | 8403 | { |
bd939f45 PZ |
8404 | struct sched_domain *child = env->sd->child; |
8405 | struct sched_group *sg = env->sd->groups; | |
05b40e05 | 8406 | struct sg_lb_stats *local = &sds->local_stat; |
56cf515b | 8407 | struct sg_lb_stats tmp_sgs; |
dbbad719 | 8408 | bool prefer_sibling = child && child->flags & SD_PREFER_SIBLING; |
630246a0 | 8409 | int sg_status = 0; |
1e3c88bd | 8410 | |
e022e0d3 | 8411 | #ifdef CONFIG_NO_HZ_COMMON |
f643ea22 | 8412 | if (env->idle == CPU_NEWLY_IDLE && READ_ONCE(nohz.has_blocked)) |
e022e0d3 | 8413 | env->flags |= LBF_NOHZ_STATS; |
e022e0d3 PZ |
8414 | #endif |
8415 | ||
1e3c88bd | 8416 | do { |
56cf515b | 8417 | struct sg_lb_stats *sgs = &tmp_sgs; |
1e3c88bd PZ |
8418 | int local_group; |
8419 | ||
ae4df9d6 | 8420 | local_group = cpumask_test_cpu(env->dst_cpu, sched_group_span(sg)); |
56cf515b JK |
8421 | if (local_group) { |
8422 | sds->local = sg; | |
05b40e05 | 8423 | sgs = local; |
b72ff13c PZ |
8424 | |
8425 | if (env->idle != CPU_NEWLY_IDLE || | |
63b2ca30 NP |
8426 | time_after_eq(jiffies, sg->sgc->next_update)) |
8427 | update_group_capacity(env->sd, env->dst_cpu); | |
56cf515b | 8428 | } |
1e3c88bd | 8429 | |
630246a0 | 8430 | update_sg_lb_stats(env, sg, sgs, &sg_status); |
1e3c88bd | 8431 | |
b72ff13c PZ |
8432 | if (local_group) |
8433 | goto next_group; | |
8434 | ||
1e3c88bd PZ |
8435 | /* |
8436 | * In case the child domain prefers tasks go to siblings | |
ea67821b | 8437 | * first, lower the sg capacity so that we'll try |
75dd321d NR |
8438 | * and move all the excess tasks away. We lower the capacity |
8439 | * of a group only if the local group has the capacity to fit | |
ea67821b VG |
8440 | * these excess tasks. The extra check prevents the case where |
8441 | * you always pull from the heaviest group when it is already | |
8442 | * under-utilized (possible with a large weight task outweighs | |
8443 | * the tasks on the system). | |
1e3c88bd | 8444 | */ |
b72ff13c | 8445 | if (prefer_sibling && sds->local && |
05b40e05 SD |
8446 | group_has_capacity(env, local) && |
8447 | (sgs->sum_nr_running > local->sum_nr_running + 1)) { | |
ea67821b | 8448 | sgs->group_no_capacity = 1; |
79a89f92 | 8449 | sgs->group_type = group_classify(sg, sgs); |
cb0b9f24 | 8450 | } |
1e3c88bd | 8451 | |
b72ff13c | 8452 | if (update_sd_pick_busiest(env, sds, sg, sgs)) { |
532cb4c4 | 8453 | sds->busiest = sg; |
56cf515b | 8454 | sds->busiest_stat = *sgs; |
1e3c88bd PZ |
8455 | } |
8456 | ||
b72ff13c PZ |
8457 | next_group: |
8458 | /* Now, start updating sd_lb_stats */ | |
90001d67 | 8459 | sds->total_running += sgs->sum_nr_running; |
b72ff13c | 8460 | sds->total_load += sgs->group_load; |
63b2ca30 | 8461 | sds->total_capacity += sgs->group_capacity; |
b72ff13c | 8462 | |
532cb4c4 | 8463 | sg = sg->next; |
bd939f45 | 8464 | } while (sg != env->sd->groups); |
0ec8aa00 | 8465 | |
f643ea22 VG |
8466 | #ifdef CONFIG_NO_HZ_COMMON |
8467 | if ((env->flags & LBF_NOHZ_AGAIN) && | |
8468 | cpumask_subset(nohz.idle_cpus_mask, sched_domain_span(env->sd))) { | |
8469 | ||
8470 | WRITE_ONCE(nohz.next_blocked, | |
8471 | jiffies + msecs_to_jiffies(LOAD_AVG_PERIOD)); | |
8472 | } | |
8473 | #endif | |
8474 | ||
0ec8aa00 PZ |
8475 | if (env->sd->flags & SD_NUMA) |
8476 | env->fbq_type = fbq_classify_group(&sds->busiest_stat); | |
4486edd1 TC |
8477 | |
8478 | if (!env->sd->parent) { | |
2802bf3c MR |
8479 | struct root_domain *rd = env->dst_rq->rd; |
8480 | ||
4486edd1 | 8481 | /* update overload indicator if we are at root domain */ |
2802bf3c MR |
8482 | WRITE_ONCE(rd->overload, sg_status & SG_OVERLOAD); |
8483 | ||
8484 | /* Update over-utilization (tipping point, U >= 0) indicator */ | |
8485 | WRITE_ONCE(rd->overutilized, sg_status & SG_OVERUTILIZED); | |
8486 | } else if (sg_status & SG_OVERUTILIZED) { | |
8487 | WRITE_ONCE(env->dst_rq->rd->overutilized, SG_OVERUTILIZED); | |
4486edd1 | 8488 | } |
532cb4c4 MN |
8489 | } |
8490 | ||
532cb4c4 MN |
8491 | /** |
8492 | * check_asym_packing - Check to see if the group is packed into the | |
0ba42a59 | 8493 | * sched domain. |
532cb4c4 MN |
8494 | * |
8495 | * This is primarily intended to used at the sibling level. Some | |
8496 | * cores like POWER7 prefer to use lower numbered SMT threads. In the | |
8497 | * case of POWER7, it can move to lower SMT modes only when higher | |
8498 | * threads are idle. When in lower SMT modes, the threads will | |
8499 | * perform better since they share less core resources. Hence when we | |
8500 | * have idle threads, we want them to be the higher ones. | |
8501 | * | |
8502 | * This packing function is run on idle threads. It checks to see if | |
8503 | * the busiest CPU in this domain (core in the P7 case) has a higher | |
8504 | * CPU number than the packing function is being run on. Here we are | |
8505 | * assuming lower CPU number will be equivalent to lower a SMT thread | |
8506 | * number. | |
8507 | * | |
e69f6186 | 8508 | * Return: 1 when packing is required and a task should be moved to |
46123355 | 8509 | * this CPU. The amount of the imbalance is returned in env->imbalance. |
b6b12294 | 8510 | * |
cd96891d | 8511 | * @env: The load balancing environment. |
532cb4c4 | 8512 | * @sds: Statistics of the sched_domain which is to be packed |
532cb4c4 | 8513 | */ |
bd939f45 | 8514 | static int check_asym_packing(struct lb_env *env, struct sd_lb_stats *sds) |
532cb4c4 MN |
8515 | { |
8516 | int busiest_cpu; | |
8517 | ||
bd939f45 | 8518 | if (!(env->sd->flags & SD_ASYM_PACKING)) |
532cb4c4 MN |
8519 | return 0; |
8520 | ||
1f621e02 SD |
8521 | if (env->idle == CPU_NOT_IDLE) |
8522 | return 0; | |
8523 | ||
532cb4c4 MN |
8524 | if (!sds->busiest) |
8525 | return 0; | |
8526 | ||
afe06efd TC |
8527 | busiest_cpu = sds->busiest->asym_prefer_cpu; |
8528 | if (sched_asym_prefer(busiest_cpu, env->dst_cpu)) | |
532cb4c4 MN |
8529 | return 0; |
8530 | ||
4ad4e481 | 8531 | env->imbalance = sds->busiest_stat.group_load; |
bd939f45 | 8532 | |
532cb4c4 | 8533 | return 1; |
1e3c88bd PZ |
8534 | } |
8535 | ||
8536 | /** | |
8537 | * fix_small_imbalance - Calculate the minor imbalance that exists | |
8538 | * amongst the groups of a sched_domain, during | |
8539 | * load balancing. | |
cd96891d | 8540 | * @env: The load balancing environment. |
1e3c88bd | 8541 | * @sds: Statistics of the sched_domain whose imbalance is to be calculated. |
1e3c88bd | 8542 | */ |
bd939f45 PZ |
8543 | static inline |
8544 | void fix_small_imbalance(struct lb_env *env, struct sd_lb_stats *sds) | |
1e3c88bd | 8545 | { |
63b2ca30 | 8546 | unsigned long tmp, capa_now = 0, capa_move = 0; |
1e3c88bd | 8547 | unsigned int imbn = 2; |
dd5feea1 | 8548 | unsigned long scaled_busy_load_per_task; |
56cf515b | 8549 | struct sg_lb_stats *local, *busiest; |
1e3c88bd | 8550 | |
56cf515b JK |
8551 | local = &sds->local_stat; |
8552 | busiest = &sds->busiest_stat; | |
1e3c88bd | 8553 | |
56cf515b JK |
8554 | if (!local->sum_nr_running) |
8555 | local->load_per_task = cpu_avg_load_per_task(env->dst_cpu); | |
8556 | else if (busiest->load_per_task > local->load_per_task) | |
8557 | imbn = 1; | |
dd5feea1 | 8558 | |
56cf515b | 8559 | scaled_busy_load_per_task = |
ca8ce3d0 | 8560 | (busiest->load_per_task * SCHED_CAPACITY_SCALE) / |
63b2ca30 | 8561 | busiest->group_capacity; |
56cf515b | 8562 | |
3029ede3 VD |
8563 | if (busiest->avg_load + scaled_busy_load_per_task >= |
8564 | local->avg_load + (scaled_busy_load_per_task * imbn)) { | |
56cf515b | 8565 | env->imbalance = busiest->load_per_task; |
1e3c88bd PZ |
8566 | return; |
8567 | } | |
8568 | ||
8569 | /* | |
8570 | * OK, we don't have enough imbalance to justify moving tasks, | |
ced549fa | 8571 | * however we may be able to increase total CPU capacity used by |
1e3c88bd PZ |
8572 | * moving them. |
8573 | */ | |
8574 | ||
63b2ca30 | 8575 | capa_now += busiest->group_capacity * |
56cf515b | 8576 | min(busiest->load_per_task, busiest->avg_load); |
63b2ca30 | 8577 | capa_now += local->group_capacity * |
56cf515b | 8578 | min(local->load_per_task, local->avg_load); |
ca8ce3d0 | 8579 | capa_now /= SCHED_CAPACITY_SCALE; |
1e3c88bd PZ |
8580 | |
8581 | /* Amount of load we'd subtract */ | |
a2cd4260 | 8582 | if (busiest->avg_load > scaled_busy_load_per_task) { |
63b2ca30 | 8583 | capa_move += busiest->group_capacity * |
56cf515b | 8584 | min(busiest->load_per_task, |
a2cd4260 | 8585 | busiest->avg_load - scaled_busy_load_per_task); |
56cf515b | 8586 | } |
1e3c88bd PZ |
8587 | |
8588 | /* Amount of load we'd add */ | |
63b2ca30 | 8589 | if (busiest->avg_load * busiest->group_capacity < |
ca8ce3d0 | 8590 | busiest->load_per_task * SCHED_CAPACITY_SCALE) { |
63b2ca30 NP |
8591 | tmp = (busiest->avg_load * busiest->group_capacity) / |
8592 | local->group_capacity; | |
56cf515b | 8593 | } else { |
ca8ce3d0 | 8594 | tmp = (busiest->load_per_task * SCHED_CAPACITY_SCALE) / |
63b2ca30 | 8595 | local->group_capacity; |
56cf515b | 8596 | } |
63b2ca30 | 8597 | capa_move += local->group_capacity * |
3ae11c90 | 8598 | min(local->load_per_task, local->avg_load + tmp); |
ca8ce3d0 | 8599 | capa_move /= SCHED_CAPACITY_SCALE; |
1e3c88bd PZ |
8600 | |
8601 | /* Move if we gain throughput */ | |
63b2ca30 | 8602 | if (capa_move > capa_now) |
56cf515b | 8603 | env->imbalance = busiest->load_per_task; |
1e3c88bd PZ |
8604 | } |
8605 | ||
8606 | /** | |
8607 | * calculate_imbalance - Calculate the amount of imbalance present within the | |
8608 | * groups of a given sched_domain during load balance. | |
bd939f45 | 8609 | * @env: load balance environment |
1e3c88bd | 8610 | * @sds: statistics of the sched_domain whose imbalance is to be calculated. |
1e3c88bd | 8611 | */ |
bd939f45 | 8612 | static inline void calculate_imbalance(struct lb_env *env, struct sd_lb_stats *sds) |
1e3c88bd | 8613 | { |
dd5feea1 | 8614 | unsigned long max_pull, load_above_capacity = ~0UL; |
56cf515b JK |
8615 | struct sg_lb_stats *local, *busiest; |
8616 | ||
8617 | local = &sds->local_stat; | |
56cf515b | 8618 | busiest = &sds->busiest_stat; |
dd5feea1 | 8619 | |
caeb178c | 8620 | if (busiest->group_type == group_imbalanced) { |
30ce5dab PZ |
8621 | /* |
8622 | * In the group_imb case we cannot rely on group-wide averages | |
97fb7a0a | 8623 | * to ensure CPU-load equilibrium, look at wider averages. XXX |
30ce5dab | 8624 | */ |
56cf515b JK |
8625 | busiest->load_per_task = |
8626 | min(busiest->load_per_task, sds->avg_load); | |
dd5feea1 SS |
8627 | } |
8628 | ||
1e3c88bd | 8629 | /* |
885e542c DE |
8630 | * Avg load of busiest sg can be less and avg load of local sg can |
8631 | * be greater than avg load across all sgs of sd because avg load | |
8632 | * factors in sg capacity and sgs with smaller group_type are | |
8633 | * skipped when updating the busiest sg: | |
1e3c88bd | 8634 | */ |
cad68e55 MR |
8635 | if (busiest->group_type != group_misfit_task && |
8636 | (busiest->avg_load <= sds->avg_load || | |
8637 | local->avg_load >= sds->avg_load)) { | |
bd939f45 PZ |
8638 | env->imbalance = 0; |
8639 | return fix_small_imbalance(env, sds); | |
1e3c88bd PZ |
8640 | } |
8641 | ||
9a5d9ba6 | 8642 | /* |
97fb7a0a | 8643 | * If there aren't any idle CPUs, avoid creating some. |
9a5d9ba6 PZ |
8644 | */ |
8645 | if (busiest->group_type == group_overloaded && | |
8646 | local->group_type == group_overloaded) { | |
1be0eb2a | 8647 | load_above_capacity = busiest->sum_nr_running * SCHED_CAPACITY_SCALE; |
cfa10334 | 8648 | if (load_above_capacity > busiest->group_capacity) { |
ea67821b | 8649 | load_above_capacity -= busiest->group_capacity; |
26656215 | 8650 | load_above_capacity *= scale_load_down(NICE_0_LOAD); |
cfa10334 MR |
8651 | load_above_capacity /= busiest->group_capacity; |
8652 | } else | |
ea67821b | 8653 | load_above_capacity = ~0UL; |
dd5feea1 SS |
8654 | } |
8655 | ||
8656 | /* | |
97fb7a0a | 8657 | * We're trying to get all the CPUs to the average_load, so we don't |
dd5feea1 | 8658 | * want to push ourselves above the average load, nor do we wish to |
97fb7a0a | 8659 | * reduce the max loaded CPU below the average load. At the same time, |
0a9b23ce DE |
8660 | * we also don't want to reduce the group load below the group |
8661 | * capacity. Thus we look for the minimum possible imbalance. | |
dd5feea1 | 8662 | */ |
30ce5dab | 8663 | max_pull = min(busiest->avg_load - sds->avg_load, load_above_capacity); |
1e3c88bd PZ |
8664 | |
8665 | /* How much load to actually move to equalise the imbalance */ | |
56cf515b | 8666 | env->imbalance = min( |
63b2ca30 NP |
8667 | max_pull * busiest->group_capacity, |
8668 | (sds->avg_load - local->avg_load) * local->group_capacity | |
ca8ce3d0 | 8669 | ) / SCHED_CAPACITY_SCALE; |
1e3c88bd | 8670 | |
cad68e55 MR |
8671 | /* Boost imbalance to allow misfit task to be balanced. */ |
8672 | if (busiest->group_type == group_misfit_task) { | |
8673 | env->imbalance = max_t(long, env->imbalance, | |
8674 | busiest->group_misfit_task_load); | |
8675 | } | |
8676 | ||
1e3c88bd PZ |
8677 | /* |
8678 | * if *imbalance is less than the average load per runnable task | |
25985edc | 8679 | * there is no guarantee that any tasks will be moved so we'll have |
1e3c88bd PZ |
8680 | * a think about bumping its value to force at least one task to be |
8681 | * moved | |
8682 | */ | |
56cf515b | 8683 | if (env->imbalance < busiest->load_per_task) |
bd939f45 | 8684 | return fix_small_imbalance(env, sds); |
1e3c88bd | 8685 | } |
fab47622 | 8686 | |
1e3c88bd PZ |
8687 | /******* find_busiest_group() helpers end here *********************/ |
8688 | ||
8689 | /** | |
8690 | * find_busiest_group - Returns the busiest group within the sched_domain | |
0a9b23ce | 8691 | * if there is an imbalance. |
1e3c88bd PZ |
8692 | * |
8693 | * Also calculates the amount of weighted load which should be moved | |
8694 | * to restore balance. | |
8695 | * | |
cd96891d | 8696 | * @env: The load balancing environment. |
1e3c88bd | 8697 | * |
e69f6186 | 8698 | * Return: - The busiest group if imbalance exists. |
1e3c88bd | 8699 | */ |
56cf515b | 8700 | static struct sched_group *find_busiest_group(struct lb_env *env) |
1e3c88bd | 8701 | { |
56cf515b | 8702 | struct sg_lb_stats *local, *busiest; |
1e3c88bd PZ |
8703 | struct sd_lb_stats sds; |
8704 | ||
147c5fc2 | 8705 | init_sd_lb_stats(&sds); |
1e3c88bd PZ |
8706 | |
8707 | /* | |
8708 | * Compute the various statistics relavent for load balancing at | |
8709 | * this level. | |
8710 | */ | |
23f0d209 | 8711 | update_sd_lb_stats(env, &sds); |
2802bf3c | 8712 | |
f8a696f2 | 8713 | if (sched_energy_enabled()) { |
2802bf3c MR |
8714 | struct root_domain *rd = env->dst_rq->rd; |
8715 | ||
8716 | if (rcu_dereference(rd->pd) && !READ_ONCE(rd->overutilized)) | |
8717 | goto out_balanced; | |
8718 | } | |
8719 | ||
56cf515b JK |
8720 | local = &sds.local_stat; |
8721 | busiest = &sds.busiest_stat; | |
1e3c88bd | 8722 | |
ea67821b | 8723 | /* ASYM feature bypasses nice load balance check */ |
1f621e02 | 8724 | if (check_asym_packing(env, &sds)) |
532cb4c4 MN |
8725 | return sds.busiest; |
8726 | ||
cc57aa8f | 8727 | /* There is no busy sibling group to pull tasks from */ |
56cf515b | 8728 | if (!sds.busiest || busiest->sum_nr_running == 0) |
1e3c88bd PZ |
8729 | goto out_balanced; |
8730 | ||
90001d67 | 8731 | /* XXX broken for overlapping NUMA groups */ |
ca8ce3d0 NP |
8732 | sds.avg_load = (SCHED_CAPACITY_SCALE * sds.total_load) |
8733 | / sds.total_capacity; | |
b0432d8f | 8734 | |
866ab43e PZ |
8735 | /* |
8736 | * If the busiest group is imbalanced the below checks don't | |
30ce5dab | 8737 | * work because they assume all things are equal, which typically |
866ab43e PZ |
8738 | * isn't true due to cpus_allowed constraints and the like. |
8739 | */ | |
caeb178c | 8740 | if (busiest->group_type == group_imbalanced) |
866ab43e PZ |
8741 | goto force_balance; |
8742 | ||
583ffd99 BJ |
8743 | /* |
8744 | * When dst_cpu is idle, prevent SMP nice and/or asymmetric group | |
8745 | * capacities from resulting in underutilization due to avg_load. | |
8746 | */ | |
8747 | if (env->idle != CPU_NOT_IDLE && group_has_capacity(env, local) && | |
ea67821b | 8748 | busiest->group_no_capacity) |
fab47622 NR |
8749 | goto force_balance; |
8750 | ||
cad68e55 MR |
8751 | /* Misfit tasks should be dealt with regardless of the avg load */ |
8752 | if (busiest->group_type == group_misfit_task) | |
8753 | goto force_balance; | |
8754 | ||
cc57aa8f | 8755 | /* |
9c58c79a | 8756 | * If the local group is busier than the selected busiest group |
cc57aa8f PZ |
8757 | * don't try and pull any tasks. |
8758 | */ | |
56cf515b | 8759 | if (local->avg_load >= busiest->avg_load) |
1e3c88bd PZ |
8760 | goto out_balanced; |
8761 | ||
cc57aa8f PZ |
8762 | /* |
8763 | * Don't pull any tasks if this group is already above the domain | |
8764 | * average load. | |
8765 | */ | |
56cf515b | 8766 | if (local->avg_load >= sds.avg_load) |
1e3c88bd PZ |
8767 | goto out_balanced; |
8768 | ||
bd939f45 | 8769 | if (env->idle == CPU_IDLE) { |
aae6d3dd | 8770 | /* |
97fb7a0a | 8771 | * This CPU is idle. If the busiest group is not overloaded |
43f4d666 | 8772 | * and there is no imbalance between this and busiest group |
97fb7a0a | 8773 | * wrt idle CPUs, it is balanced. The imbalance becomes |
43f4d666 VG |
8774 | * significant if the diff is greater than 1 otherwise we |
8775 | * might end up to just move the imbalance on another group | |
aae6d3dd | 8776 | */ |
43f4d666 VG |
8777 | if ((busiest->group_type != group_overloaded) && |
8778 | (local->idle_cpus <= (busiest->idle_cpus + 1))) | |
aae6d3dd | 8779 | goto out_balanced; |
c186fafe PZ |
8780 | } else { |
8781 | /* | |
8782 | * In the CPU_NEWLY_IDLE, CPU_NOT_IDLE cases, use | |
8783 | * imbalance_pct to be conservative. | |
8784 | */ | |
56cf515b JK |
8785 | if (100 * busiest->avg_load <= |
8786 | env->sd->imbalance_pct * local->avg_load) | |
c186fafe | 8787 | goto out_balanced; |
aae6d3dd | 8788 | } |
1e3c88bd | 8789 | |
fab47622 | 8790 | force_balance: |
1e3c88bd | 8791 | /* Looks like there is an imbalance. Compute it */ |
cad68e55 | 8792 | env->src_grp_type = busiest->group_type; |
bd939f45 | 8793 | calculate_imbalance(env, &sds); |
bb3485c8 | 8794 | return env->imbalance ? sds.busiest : NULL; |
1e3c88bd PZ |
8795 | |
8796 | out_balanced: | |
bd939f45 | 8797 | env->imbalance = 0; |
1e3c88bd PZ |
8798 | return NULL; |
8799 | } | |
8800 | ||
8801 | /* | |
97fb7a0a | 8802 | * find_busiest_queue - find the busiest runqueue among the CPUs in the group. |
1e3c88bd | 8803 | */ |
bd939f45 | 8804 | static struct rq *find_busiest_queue(struct lb_env *env, |
b9403130 | 8805 | struct sched_group *group) |
1e3c88bd PZ |
8806 | { |
8807 | struct rq *busiest = NULL, *rq; | |
ced549fa | 8808 | unsigned long busiest_load = 0, busiest_capacity = 1; |
1e3c88bd PZ |
8809 | int i; |
8810 | ||
ae4df9d6 | 8811 | for_each_cpu_and(i, sched_group_span(group), env->cpus) { |
ea67821b | 8812 | unsigned long capacity, wl; |
0ec8aa00 PZ |
8813 | enum fbq_type rt; |
8814 | ||
8815 | rq = cpu_rq(i); | |
8816 | rt = fbq_classify_rq(rq); | |
1e3c88bd | 8817 | |
0ec8aa00 PZ |
8818 | /* |
8819 | * We classify groups/runqueues into three groups: | |
8820 | * - regular: there are !numa tasks | |
8821 | * - remote: there are numa tasks that run on the 'wrong' node | |
8822 | * - all: there is no distinction | |
8823 | * | |
8824 | * In order to avoid migrating ideally placed numa tasks, | |
8825 | * ignore those when there's better options. | |
8826 | * | |
8827 | * If we ignore the actual busiest queue to migrate another | |
8828 | * task, the next balance pass can still reduce the busiest | |
8829 | * queue by moving tasks around inside the node. | |
8830 | * | |
8831 | * If we cannot move enough load due to this classification | |
8832 | * the next pass will adjust the group classification and | |
8833 | * allow migration of more tasks. | |
8834 | * | |
8835 | * Both cases only affect the total convergence complexity. | |
8836 | */ | |
8837 | if (rt > env->fbq_type) | |
8838 | continue; | |
8839 | ||
cad68e55 MR |
8840 | /* |
8841 | * For ASYM_CPUCAPACITY domains with misfit tasks we simply | |
8842 | * seek the "biggest" misfit task. | |
8843 | */ | |
8844 | if (env->src_grp_type == group_misfit_task) { | |
8845 | if (rq->misfit_task_load > busiest_load) { | |
8846 | busiest_load = rq->misfit_task_load; | |
8847 | busiest = rq; | |
8848 | } | |
8849 | ||
8850 | continue; | |
8851 | } | |
8852 | ||
ced549fa | 8853 | capacity = capacity_of(i); |
9d5efe05 | 8854 | |
4ad3831a CR |
8855 | /* |
8856 | * For ASYM_CPUCAPACITY domains, don't pick a CPU that could | |
8857 | * eventually lead to active_balancing high->low capacity. | |
8858 | * Higher per-CPU capacity is considered better than balancing | |
8859 | * average load. | |
8860 | */ | |
8861 | if (env->sd->flags & SD_ASYM_CPUCAPACITY && | |
8862 | capacity_of(env->dst_cpu) < capacity && | |
8863 | rq->nr_running == 1) | |
8864 | continue; | |
8865 | ||
c7132dd6 | 8866 | wl = weighted_cpuload(rq); |
1e3c88bd | 8867 | |
6e40f5bb TG |
8868 | /* |
8869 | * When comparing with imbalance, use weighted_cpuload() | |
97fb7a0a | 8870 | * which is not scaled with the CPU capacity. |
6e40f5bb | 8871 | */ |
ea67821b VG |
8872 | |
8873 | if (rq->nr_running == 1 && wl > env->imbalance && | |
8874 | !check_cpu_capacity(rq, env->sd)) | |
1e3c88bd PZ |
8875 | continue; |
8876 | ||
6e40f5bb | 8877 | /* |
97fb7a0a IM |
8878 | * For the load comparisons with the other CPU's, consider |
8879 | * the weighted_cpuload() scaled with the CPU capacity, so | |
8880 | * that the load can be moved away from the CPU that is | |
ced549fa | 8881 | * potentially running at a lower capacity. |
95a79b80 | 8882 | * |
ced549fa | 8883 | * Thus we're looking for max(wl_i / capacity_i), crosswise |
95a79b80 | 8884 | * multiplication to rid ourselves of the division works out |
ced549fa NP |
8885 | * to: wl_i * capacity_j > wl_j * capacity_i; where j is |
8886 | * our previous maximum. | |
6e40f5bb | 8887 | */ |
ced549fa | 8888 | if (wl * busiest_capacity > busiest_load * capacity) { |
95a79b80 | 8889 | busiest_load = wl; |
ced549fa | 8890 | busiest_capacity = capacity; |
1e3c88bd PZ |
8891 | busiest = rq; |
8892 | } | |
8893 | } | |
8894 | ||
8895 | return busiest; | |
8896 | } | |
8897 | ||
8898 | /* | |
8899 | * Max backoff if we encounter pinned tasks. Pretty arbitrary value, but | |
8900 | * so long as it is large enough. | |
8901 | */ | |
8902 | #define MAX_PINNED_INTERVAL 512 | |
8903 | ||
46a745d9 VG |
8904 | static inline bool |
8905 | asym_active_balance(struct lb_env *env) | |
1af3ed3d | 8906 | { |
46a745d9 VG |
8907 | /* |
8908 | * ASYM_PACKING needs to force migrate tasks from busy but | |
8909 | * lower priority CPUs in order to pack all tasks in the | |
8910 | * highest priority CPUs. | |
8911 | */ | |
8912 | return env->idle != CPU_NOT_IDLE && (env->sd->flags & SD_ASYM_PACKING) && | |
8913 | sched_asym_prefer(env->dst_cpu, env->src_cpu); | |
8914 | } | |
bd939f45 | 8915 | |
46a745d9 VG |
8916 | static inline bool |
8917 | voluntary_active_balance(struct lb_env *env) | |
8918 | { | |
8919 | struct sched_domain *sd = env->sd; | |
532cb4c4 | 8920 | |
46a745d9 VG |
8921 | if (asym_active_balance(env)) |
8922 | return 1; | |
1af3ed3d | 8923 | |
1aaf90a4 VG |
8924 | /* |
8925 | * The dst_cpu is idle and the src_cpu CPU has only 1 CFS task. | |
8926 | * It's worth migrating the task if the src_cpu's capacity is reduced | |
8927 | * because of other sched_class or IRQs if more capacity stays | |
8928 | * available on dst_cpu. | |
8929 | */ | |
8930 | if ((env->idle != CPU_NOT_IDLE) && | |
8931 | (env->src_rq->cfs.h_nr_running == 1)) { | |
8932 | if ((check_cpu_capacity(env->src_rq, sd)) && | |
8933 | (capacity_of(env->src_cpu)*sd->imbalance_pct < capacity_of(env->dst_cpu)*100)) | |
8934 | return 1; | |
8935 | } | |
8936 | ||
cad68e55 MR |
8937 | if (env->src_grp_type == group_misfit_task) |
8938 | return 1; | |
8939 | ||
46a745d9 VG |
8940 | return 0; |
8941 | } | |
8942 | ||
8943 | static int need_active_balance(struct lb_env *env) | |
8944 | { | |
8945 | struct sched_domain *sd = env->sd; | |
8946 | ||
8947 | if (voluntary_active_balance(env)) | |
8948 | return 1; | |
8949 | ||
1af3ed3d PZ |
8950 | return unlikely(sd->nr_balance_failed > sd->cache_nice_tries+2); |
8951 | } | |
8952 | ||
969c7921 TH |
8953 | static int active_load_balance_cpu_stop(void *data); |
8954 | ||
23f0d209 JK |
8955 | static int should_we_balance(struct lb_env *env) |
8956 | { | |
8957 | struct sched_group *sg = env->sd->groups; | |
23f0d209 JK |
8958 | int cpu, balance_cpu = -1; |
8959 | ||
024c9d2f PZ |
8960 | /* |
8961 | * Ensure the balancing environment is consistent; can happen | |
8962 | * when the softirq triggers 'during' hotplug. | |
8963 | */ | |
8964 | if (!cpumask_test_cpu(env->dst_cpu, env->cpus)) | |
8965 | return 0; | |
8966 | ||
23f0d209 | 8967 | /* |
97fb7a0a | 8968 | * In the newly idle case, we will allow all the CPUs |
23f0d209 JK |
8969 | * to do the newly idle load balance. |
8970 | */ | |
8971 | if (env->idle == CPU_NEWLY_IDLE) | |
8972 | return 1; | |
8973 | ||
97fb7a0a | 8974 | /* Try to find first idle CPU */ |
e5c14b1f | 8975 | for_each_cpu_and(cpu, group_balance_mask(sg), env->cpus) { |
af218122 | 8976 | if (!idle_cpu(cpu)) |
23f0d209 JK |
8977 | continue; |
8978 | ||
8979 | balance_cpu = cpu; | |
8980 | break; | |
8981 | } | |
8982 | ||
8983 | if (balance_cpu == -1) | |
8984 | balance_cpu = group_balance_cpu(sg); | |
8985 | ||
8986 | /* | |
97fb7a0a | 8987 | * First idle CPU or the first CPU(busiest) in this sched group |
23f0d209 JK |
8988 | * is eligible for doing load balancing at this and above domains. |
8989 | */ | |
b0cff9d8 | 8990 | return balance_cpu == env->dst_cpu; |
23f0d209 JK |
8991 | } |
8992 | ||
1e3c88bd PZ |
8993 | /* |
8994 | * Check this_cpu to ensure it is balanced within domain. Attempt to move | |
8995 | * tasks if there is an imbalance. | |
8996 | */ | |
8997 | static int load_balance(int this_cpu, struct rq *this_rq, | |
8998 | struct sched_domain *sd, enum cpu_idle_type idle, | |
23f0d209 | 8999 | int *continue_balancing) |
1e3c88bd | 9000 | { |
88b8dac0 | 9001 | int ld_moved, cur_ld_moved, active_balance = 0; |
6263322c | 9002 | struct sched_domain *sd_parent = sd->parent; |
1e3c88bd | 9003 | struct sched_group *group; |
1e3c88bd | 9004 | struct rq *busiest; |
8a8c69c3 | 9005 | struct rq_flags rf; |
4ba29684 | 9006 | struct cpumask *cpus = this_cpu_cpumask_var_ptr(load_balance_mask); |
1e3c88bd | 9007 | |
8e45cb54 PZ |
9008 | struct lb_env env = { |
9009 | .sd = sd, | |
ddcdf6e7 PZ |
9010 | .dst_cpu = this_cpu, |
9011 | .dst_rq = this_rq, | |
ae4df9d6 | 9012 | .dst_grpmask = sched_group_span(sd->groups), |
8e45cb54 | 9013 | .idle = idle, |
eb95308e | 9014 | .loop_break = sched_nr_migrate_break, |
b9403130 | 9015 | .cpus = cpus, |
0ec8aa00 | 9016 | .fbq_type = all, |
163122b7 | 9017 | .tasks = LIST_HEAD_INIT(env.tasks), |
8e45cb54 PZ |
9018 | }; |
9019 | ||
65a4433a | 9020 | cpumask_and(cpus, sched_domain_span(sd), cpu_active_mask); |
1e3c88bd | 9021 | |
ae92882e | 9022 | schedstat_inc(sd->lb_count[idle]); |
1e3c88bd PZ |
9023 | |
9024 | redo: | |
23f0d209 JK |
9025 | if (!should_we_balance(&env)) { |
9026 | *continue_balancing = 0; | |
1e3c88bd | 9027 | goto out_balanced; |
23f0d209 | 9028 | } |
1e3c88bd | 9029 | |
23f0d209 | 9030 | group = find_busiest_group(&env); |
1e3c88bd | 9031 | if (!group) { |
ae92882e | 9032 | schedstat_inc(sd->lb_nobusyg[idle]); |
1e3c88bd PZ |
9033 | goto out_balanced; |
9034 | } | |
9035 | ||
b9403130 | 9036 | busiest = find_busiest_queue(&env, group); |
1e3c88bd | 9037 | if (!busiest) { |
ae92882e | 9038 | schedstat_inc(sd->lb_nobusyq[idle]); |
1e3c88bd PZ |
9039 | goto out_balanced; |
9040 | } | |
9041 | ||
78feefc5 | 9042 | BUG_ON(busiest == env.dst_rq); |
1e3c88bd | 9043 | |
ae92882e | 9044 | schedstat_add(sd->lb_imbalance[idle], env.imbalance); |
1e3c88bd | 9045 | |
1aaf90a4 VG |
9046 | env.src_cpu = busiest->cpu; |
9047 | env.src_rq = busiest; | |
9048 | ||
1e3c88bd PZ |
9049 | ld_moved = 0; |
9050 | if (busiest->nr_running > 1) { | |
9051 | /* | |
9052 | * Attempt to move tasks. If find_busiest_group has found | |
9053 | * an imbalance but busiest->nr_running <= 1, the group is | |
9054 | * still unbalanced. ld_moved simply stays zero, so it is | |
9055 | * correctly treated as an imbalance. | |
9056 | */ | |
8e45cb54 | 9057 | env.flags |= LBF_ALL_PINNED; |
c82513e5 | 9058 | env.loop_max = min(sysctl_sched_nr_migrate, busiest->nr_running); |
8e45cb54 | 9059 | |
5d6523eb | 9060 | more_balance: |
8a8c69c3 | 9061 | rq_lock_irqsave(busiest, &rf); |
3bed5e21 | 9062 | update_rq_clock(busiest); |
88b8dac0 SV |
9063 | |
9064 | /* | |
9065 | * cur_ld_moved - load moved in current iteration | |
9066 | * ld_moved - cumulative load moved across iterations | |
9067 | */ | |
163122b7 | 9068 | cur_ld_moved = detach_tasks(&env); |
1e3c88bd PZ |
9069 | |
9070 | /* | |
163122b7 KT |
9071 | * We've detached some tasks from busiest_rq. Every |
9072 | * task is masked "TASK_ON_RQ_MIGRATING", so we can safely | |
9073 | * unlock busiest->lock, and we are able to be sure | |
9074 | * that nobody can manipulate the tasks in parallel. | |
9075 | * See task_rq_lock() family for the details. | |
1e3c88bd | 9076 | */ |
163122b7 | 9077 | |
8a8c69c3 | 9078 | rq_unlock(busiest, &rf); |
163122b7 KT |
9079 | |
9080 | if (cur_ld_moved) { | |
9081 | attach_tasks(&env); | |
9082 | ld_moved += cur_ld_moved; | |
9083 | } | |
9084 | ||
8a8c69c3 | 9085 | local_irq_restore(rf.flags); |
88b8dac0 | 9086 | |
f1cd0858 JK |
9087 | if (env.flags & LBF_NEED_BREAK) { |
9088 | env.flags &= ~LBF_NEED_BREAK; | |
9089 | goto more_balance; | |
9090 | } | |
9091 | ||
88b8dac0 SV |
9092 | /* |
9093 | * Revisit (affine) tasks on src_cpu that couldn't be moved to | |
9094 | * us and move them to an alternate dst_cpu in our sched_group | |
9095 | * where they can run. The upper limit on how many times we | |
97fb7a0a | 9096 | * iterate on same src_cpu is dependent on number of CPUs in our |
88b8dac0 SV |
9097 | * sched_group. |
9098 | * | |
9099 | * This changes load balance semantics a bit on who can move | |
9100 | * load to a given_cpu. In addition to the given_cpu itself | |
9101 | * (or a ilb_cpu acting on its behalf where given_cpu is | |
9102 | * nohz-idle), we now have balance_cpu in a position to move | |
9103 | * load to given_cpu. In rare situations, this may cause | |
9104 | * conflicts (balance_cpu and given_cpu/ilb_cpu deciding | |
9105 | * _independently_ and at _same_ time to move some load to | |
9106 | * given_cpu) causing exceess load to be moved to given_cpu. | |
9107 | * This however should not happen so much in practice and | |
9108 | * moreover subsequent load balance cycles should correct the | |
9109 | * excess load moved. | |
9110 | */ | |
6263322c | 9111 | if ((env.flags & LBF_DST_PINNED) && env.imbalance > 0) { |
88b8dac0 | 9112 | |
97fb7a0a | 9113 | /* Prevent to re-select dst_cpu via env's CPUs */ |
7aff2e3a VD |
9114 | cpumask_clear_cpu(env.dst_cpu, env.cpus); |
9115 | ||
78feefc5 | 9116 | env.dst_rq = cpu_rq(env.new_dst_cpu); |
88b8dac0 | 9117 | env.dst_cpu = env.new_dst_cpu; |
6263322c | 9118 | env.flags &= ~LBF_DST_PINNED; |
88b8dac0 SV |
9119 | env.loop = 0; |
9120 | env.loop_break = sched_nr_migrate_break; | |
e02e60c1 | 9121 | |
88b8dac0 SV |
9122 | /* |
9123 | * Go back to "more_balance" rather than "redo" since we | |
9124 | * need to continue with same src_cpu. | |
9125 | */ | |
9126 | goto more_balance; | |
9127 | } | |
1e3c88bd | 9128 | |
6263322c PZ |
9129 | /* |
9130 | * We failed to reach balance because of affinity. | |
9131 | */ | |
9132 | if (sd_parent) { | |
63b2ca30 | 9133 | int *group_imbalance = &sd_parent->groups->sgc->imbalance; |
6263322c | 9134 | |
afdeee05 | 9135 | if ((env.flags & LBF_SOME_PINNED) && env.imbalance > 0) |
6263322c | 9136 | *group_imbalance = 1; |
6263322c PZ |
9137 | } |
9138 | ||
1e3c88bd | 9139 | /* All tasks on this runqueue were pinned by CPU affinity */ |
8e45cb54 | 9140 | if (unlikely(env.flags & LBF_ALL_PINNED)) { |
1e3c88bd | 9141 | cpumask_clear_cpu(cpu_of(busiest), cpus); |
65a4433a JH |
9142 | /* |
9143 | * Attempting to continue load balancing at the current | |
9144 | * sched_domain level only makes sense if there are | |
9145 | * active CPUs remaining as possible busiest CPUs to | |
9146 | * pull load from which are not contained within the | |
9147 | * destination group that is receiving any migrated | |
9148 | * load. | |
9149 | */ | |
9150 | if (!cpumask_subset(cpus, env.dst_grpmask)) { | |
bbf18b19 PN |
9151 | env.loop = 0; |
9152 | env.loop_break = sched_nr_migrate_break; | |
1e3c88bd | 9153 | goto redo; |
bbf18b19 | 9154 | } |
afdeee05 | 9155 | goto out_all_pinned; |
1e3c88bd PZ |
9156 | } |
9157 | } | |
9158 | ||
9159 | if (!ld_moved) { | |
ae92882e | 9160 | schedstat_inc(sd->lb_failed[idle]); |
58b26c4c VP |
9161 | /* |
9162 | * Increment the failure counter only on periodic balance. | |
9163 | * We do not want newidle balance, which can be very | |
9164 | * frequent, pollute the failure counter causing | |
9165 | * excessive cache_hot migrations and active balances. | |
9166 | */ | |
9167 | if (idle != CPU_NEWLY_IDLE) | |
9168 | sd->nr_balance_failed++; | |
1e3c88bd | 9169 | |
bd939f45 | 9170 | if (need_active_balance(&env)) { |
8a8c69c3 PZ |
9171 | unsigned long flags; |
9172 | ||
1e3c88bd PZ |
9173 | raw_spin_lock_irqsave(&busiest->lock, flags); |
9174 | ||
97fb7a0a IM |
9175 | /* |
9176 | * Don't kick the active_load_balance_cpu_stop, | |
9177 | * if the curr task on busiest CPU can't be | |
9178 | * moved to this_cpu: | |
1e3c88bd | 9179 | */ |
0c98d344 | 9180 | if (!cpumask_test_cpu(this_cpu, &busiest->curr->cpus_allowed)) { |
1e3c88bd PZ |
9181 | raw_spin_unlock_irqrestore(&busiest->lock, |
9182 | flags); | |
8e45cb54 | 9183 | env.flags |= LBF_ALL_PINNED; |
1e3c88bd PZ |
9184 | goto out_one_pinned; |
9185 | } | |
9186 | ||
969c7921 TH |
9187 | /* |
9188 | * ->active_balance synchronizes accesses to | |
9189 | * ->active_balance_work. Once set, it's cleared | |
9190 | * only after active load balance is finished. | |
9191 | */ | |
1e3c88bd PZ |
9192 | if (!busiest->active_balance) { |
9193 | busiest->active_balance = 1; | |
9194 | busiest->push_cpu = this_cpu; | |
9195 | active_balance = 1; | |
9196 | } | |
9197 | raw_spin_unlock_irqrestore(&busiest->lock, flags); | |
969c7921 | 9198 | |
bd939f45 | 9199 | if (active_balance) { |
969c7921 TH |
9200 | stop_one_cpu_nowait(cpu_of(busiest), |
9201 | active_load_balance_cpu_stop, busiest, | |
9202 | &busiest->active_balance_work); | |
bd939f45 | 9203 | } |
1e3c88bd | 9204 | |
d02c0711 | 9205 | /* We've kicked active balancing, force task migration. */ |
1e3c88bd PZ |
9206 | sd->nr_balance_failed = sd->cache_nice_tries+1; |
9207 | } | |
9208 | } else | |
9209 | sd->nr_balance_failed = 0; | |
9210 | ||
46a745d9 | 9211 | if (likely(!active_balance) || voluntary_active_balance(&env)) { |
1e3c88bd PZ |
9212 | /* We were unbalanced, so reset the balancing interval */ |
9213 | sd->balance_interval = sd->min_interval; | |
9214 | } else { | |
9215 | /* | |
9216 | * If we've begun active balancing, start to back off. This | |
9217 | * case may not be covered by the all_pinned logic if there | |
9218 | * is only 1 task on the busy runqueue (because we don't call | |
163122b7 | 9219 | * detach_tasks). |
1e3c88bd PZ |
9220 | */ |
9221 | if (sd->balance_interval < sd->max_interval) | |
9222 | sd->balance_interval *= 2; | |
9223 | } | |
9224 | ||
1e3c88bd PZ |
9225 | goto out; |
9226 | ||
9227 | out_balanced: | |
afdeee05 VG |
9228 | /* |
9229 | * We reach balance although we may have faced some affinity | |
9230 | * constraints. Clear the imbalance flag if it was set. | |
9231 | */ | |
9232 | if (sd_parent) { | |
9233 | int *group_imbalance = &sd_parent->groups->sgc->imbalance; | |
9234 | ||
9235 | if (*group_imbalance) | |
9236 | *group_imbalance = 0; | |
9237 | } | |
9238 | ||
9239 | out_all_pinned: | |
9240 | /* | |
9241 | * We reach balance because all tasks are pinned at this level so | |
9242 | * we can't migrate them. Let the imbalance flag set so parent level | |
9243 | * can try to migrate them. | |
9244 | */ | |
ae92882e | 9245 | schedstat_inc(sd->lb_balanced[idle]); |
1e3c88bd PZ |
9246 | |
9247 | sd->nr_balance_failed = 0; | |
9248 | ||
9249 | out_one_pinned: | |
3f130a37 VS |
9250 | ld_moved = 0; |
9251 | ||
9252 | /* | |
9253 | * idle_balance() disregards balance intervals, so we could repeatedly | |
9254 | * reach this code, which would lead to balance_interval skyrocketting | |
9255 | * in a short amount of time. Skip the balance_interval increase logic | |
9256 | * to avoid that. | |
9257 | */ | |
9258 | if (env.idle == CPU_NEWLY_IDLE) | |
9259 | goto out; | |
9260 | ||
1e3c88bd | 9261 | /* tune up the balancing interval */ |
47b7aee1 VS |
9262 | if ((env.flags & LBF_ALL_PINNED && |
9263 | sd->balance_interval < MAX_PINNED_INTERVAL) || | |
9264 | sd->balance_interval < sd->max_interval) | |
1e3c88bd | 9265 | sd->balance_interval *= 2; |
1e3c88bd | 9266 | out: |
1e3c88bd PZ |
9267 | return ld_moved; |
9268 | } | |
9269 | ||
52a08ef1 JL |
9270 | static inline unsigned long |
9271 | get_sd_balance_interval(struct sched_domain *sd, int cpu_busy) | |
9272 | { | |
9273 | unsigned long interval = sd->balance_interval; | |
9274 | ||
9275 | if (cpu_busy) | |
9276 | interval *= sd->busy_factor; | |
9277 | ||
9278 | /* scale ms to jiffies */ | |
9279 | interval = msecs_to_jiffies(interval); | |
9280 | interval = clamp(interval, 1UL, max_load_balance_interval); | |
9281 | ||
9282 | return interval; | |
9283 | } | |
9284 | ||
9285 | static inline void | |
31851a98 | 9286 | update_next_balance(struct sched_domain *sd, unsigned long *next_balance) |
52a08ef1 JL |
9287 | { |
9288 | unsigned long interval, next; | |
9289 | ||
31851a98 LY |
9290 | /* used by idle balance, so cpu_busy = 0 */ |
9291 | interval = get_sd_balance_interval(sd, 0); | |
52a08ef1 JL |
9292 | next = sd->last_balance + interval; |
9293 | ||
9294 | if (time_after(*next_balance, next)) | |
9295 | *next_balance = next; | |
9296 | } | |
9297 | ||
1e3c88bd | 9298 | /* |
97fb7a0a | 9299 | * active_load_balance_cpu_stop is run by the CPU stopper. It pushes |
969c7921 TH |
9300 | * running tasks off the busiest CPU onto idle CPUs. It requires at |
9301 | * least 1 task to be running on each physical CPU where possible, and | |
9302 | * avoids physical / logical imbalances. | |
1e3c88bd | 9303 | */ |
969c7921 | 9304 | static int active_load_balance_cpu_stop(void *data) |
1e3c88bd | 9305 | { |
969c7921 TH |
9306 | struct rq *busiest_rq = data; |
9307 | int busiest_cpu = cpu_of(busiest_rq); | |
1e3c88bd | 9308 | int target_cpu = busiest_rq->push_cpu; |
969c7921 | 9309 | struct rq *target_rq = cpu_rq(target_cpu); |
1e3c88bd | 9310 | struct sched_domain *sd; |
e5673f28 | 9311 | struct task_struct *p = NULL; |
8a8c69c3 | 9312 | struct rq_flags rf; |
969c7921 | 9313 | |
8a8c69c3 | 9314 | rq_lock_irq(busiest_rq, &rf); |
edd8e41d PZ |
9315 | /* |
9316 | * Between queueing the stop-work and running it is a hole in which | |
9317 | * CPUs can become inactive. We should not move tasks from or to | |
9318 | * inactive CPUs. | |
9319 | */ | |
9320 | if (!cpu_active(busiest_cpu) || !cpu_active(target_cpu)) | |
9321 | goto out_unlock; | |
969c7921 | 9322 | |
97fb7a0a | 9323 | /* Make sure the requested CPU hasn't gone down in the meantime: */ |
969c7921 TH |
9324 | if (unlikely(busiest_cpu != smp_processor_id() || |
9325 | !busiest_rq->active_balance)) | |
9326 | goto out_unlock; | |
1e3c88bd PZ |
9327 | |
9328 | /* Is there any task to move? */ | |
9329 | if (busiest_rq->nr_running <= 1) | |
969c7921 | 9330 | goto out_unlock; |
1e3c88bd PZ |
9331 | |
9332 | /* | |
9333 | * This condition is "impossible", if it occurs | |
9334 | * we need to fix it. Originally reported by | |
97fb7a0a | 9335 | * Bjorn Helgaas on a 128-CPU setup. |
1e3c88bd PZ |
9336 | */ |
9337 | BUG_ON(busiest_rq == target_rq); | |
9338 | ||
1e3c88bd | 9339 | /* Search for an sd spanning us and the target CPU. */ |
dce840a0 | 9340 | rcu_read_lock(); |
1e3c88bd PZ |
9341 | for_each_domain(target_cpu, sd) { |
9342 | if ((sd->flags & SD_LOAD_BALANCE) && | |
9343 | cpumask_test_cpu(busiest_cpu, sched_domain_span(sd))) | |
9344 | break; | |
9345 | } | |
9346 | ||
9347 | if (likely(sd)) { | |
8e45cb54 PZ |
9348 | struct lb_env env = { |
9349 | .sd = sd, | |
ddcdf6e7 PZ |
9350 | .dst_cpu = target_cpu, |
9351 | .dst_rq = target_rq, | |
9352 | .src_cpu = busiest_rq->cpu, | |
9353 | .src_rq = busiest_rq, | |
8e45cb54 | 9354 | .idle = CPU_IDLE, |
65a4433a JH |
9355 | /* |
9356 | * can_migrate_task() doesn't need to compute new_dst_cpu | |
9357 | * for active balancing. Since we have CPU_IDLE, but no | |
9358 | * @dst_grpmask we need to make that test go away with lying | |
9359 | * about DST_PINNED. | |
9360 | */ | |
9361 | .flags = LBF_DST_PINNED, | |
8e45cb54 PZ |
9362 | }; |
9363 | ||
ae92882e | 9364 | schedstat_inc(sd->alb_count); |
3bed5e21 | 9365 | update_rq_clock(busiest_rq); |
1e3c88bd | 9366 | |
e5673f28 | 9367 | p = detach_one_task(&env); |
d02c0711 | 9368 | if (p) { |
ae92882e | 9369 | schedstat_inc(sd->alb_pushed); |
d02c0711 SD |
9370 | /* Active balancing done, reset the failure counter. */ |
9371 | sd->nr_balance_failed = 0; | |
9372 | } else { | |
ae92882e | 9373 | schedstat_inc(sd->alb_failed); |
d02c0711 | 9374 | } |
1e3c88bd | 9375 | } |
dce840a0 | 9376 | rcu_read_unlock(); |
969c7921 TH |
9377 | out_unlock: |
9378 | busiest_rq->active_balance = 0; | |
8a8c69c3 | 9379 | rq_unlock(busiest_rq, &rf); |
e5673f28 KT |
9380 | |
9381 | if (p) | |
9382 | attach_one_task(target_rq, p); | |
9383 | ||
9384 | local_irq_enable(); | |
9385 | ||
969c7921 | 9386 | return 0; |
1e3c88bd PZ |
9387 | } |
9388 | ||
af3fe03c PZ |
9389 | static DEFINE_SPINLOCK(balancing); |
9390 | ||
9391 | /* | |
9392 | * Scale the max load_balance interval with the number of CPUs in the system. | |
9393 | * This trades load-balance latency on larger machines for less cross talk. | |
9394 | */ | |
9395 | void update_max_interval(void) | |
9396 | { | |
9397 | max_load_balance_interval = HZ*num_online_cpus()/10; | |
9398 | } | |
9399 | ||
9400 | /* | |
9401 | * It checks each scheduling domain to see if it is due to be balanced, | |
9402 | * and initiates a balancing operation if so. | |
9403 | * | |
9404 | * Balancing parameters are set up in init_sched_domains. | |
9405 | */ | |
9406 | static void rebalance_domains(struct rq *rq, enum cpu_idle_type idle) | |
9407 | { | |
9408 | int continue_balancing = 1; | |
9409 | int cpu = rq->cpu; | |
9410 | unsigned long interval; | |
9411 | struct sched_domain *sd; | |
9412 | /* Earliest time when we have to do rebalance again */ | |
9413 | unsigned long next_balance = jiffies + 60*HZ; | |
9414 | int update_next_balance = 0; | |
9415 | int need_serialize, need_decay = 0; | |
9416 | u64 max_cost = 0; | |
9417 | ||
9418 | rcu_read_lock(); | |
9419 | for_each_domain(cpu, sd) { | |
9420 | /* | |
9421 | * Decay the newidle max times here because this is a regular | |
9422 | * visit to all the domains. Decay ~1% per second. | |
9423 | */ | |
9424 | if (time_after(jiffies, sd->next_decay_max_lb_cost)) { | |
9425 | sd->max_newidle_lb_cost = | |
9426 | (sd->max_newidle_lb_cost * 253) / 256; | |
9427 | sd->next_decay_max_lb_cost = jiffies + HZ; | |
9428 | need_decay = 1; | |
9429 | } | |
9430 | max_cost += sd->max_newidle_lb_cost; | |
9431 | ||
9432 | if (!(sd->flags & SD_LOAD_BALANCE)) | |
9433 | continue; | |
9434 | ||
9435 | /* | |
9436 | * Stop the load balance at this level. There is another | |
9437 | * CPU in our sched group which is doing load balancing more | |
9438 | * actively. | |
9439 | */ | |
9440 | if (!continue_balancing) { | |
9441 | if (need_decay) | |
9442 | continue; | |
9443 | break; | |
9444 | } | |
9445 | ||
9446 | interval = get_sd_balance_interval(sd, idle != CPU_IDLE); | |
9447 | ||
9448 | need_serialize = sd->flags & SD_SERIALIZE; | |
9449 | if (need_serialize) { | |
9450 | if (!spin_trylock(&balancing)) | |
9451 | goto out; | |
9452 | } | |
9453 | ||
9454 | if (time_after_eq(jiffies, sd->last_balance + interval)) { | |
9455 | if (load_balance(cpu, rq, sd, idle, &continue_balancing)) { | |
9456 | /* | |
9457 | * The LBF_DST_PINNED logic could have changed | |
9458 | * env->dst_cpu, so we can't know our idle | |
9459 | * state even if we migrated tasks. Update it. | |
9460 | */ | |
9461 | idle = idle_cpu(cpu) ? CPU_IDLE : CPU_NOT_IDLE; | |
9462 | } | |
9463 | sd->last_balance = jiffies; | |
9464 | interval = get_sd_balance_interval(sd, idle != CPU_IDLE); | |
9465 | } | |
9466 | if (need_serialize) | |
9467 | spin_unlock(&balancing); | |
9468 | out: | |
9469 | if (time_after(next_balance, sd->last_balance + interval)) { | |
9470 | next_balance = sd->last_balance + interval; | |
9471 | update_next_balance = 1; | |
9472 | } | |
9473 | } | |
9474 | if (need_decay) { | |
9475 | /* | |
9476 | * Ensure the rq-wide value also decays but keep it at a | |
9477 | * reasonable floor to avoid funnies with rq->avg_idle. | |
9478 | */ | |
9479 | rq->max_idle_balance_cost = | |
9480 | max((u64)sysctl_sched_migration_cost, max_cost); | |
9481 | } | |
9482 | rcu_read_unlock(); | |
9483 | ||
9484 | /* | |
9485 | * next_balance will be updated only when there is a need. | |
9486 | * When the cpu is attached to null domain for ex, it will not be | |
9487 | * updated. | |
9488 | */ | |
9489 | if (likely(update_next_balance)) { | |
9490 | rq->next_balance = next_balance; | |
9491 | ||
9492 | #ifdef CONFIG_NO_HZ_COMMON | |
9493 | /* | |
9494 | * If this CPU has been elected to perform the nohz idle | |
9495 | * balance. Other idle CPUs have already rebalanced with | |
9496 | * nohz_idle_balance() and nohz.next_balance has been | |
9497 | * updated accordingly. This CPU is now running the idle load | |
9498 | * balance for itself and we need to update the | |
9499 | * nohz.next_balance accordingly. | |
9500 | */ | |
9501 | if ((idle == CPU_IDLE) && time_after(nohz.next_balance, rq->next_balance)) | |
9502 | nohz.next_balance = rq->next_balance; | |
9503 | #endif | |
9504 | } | |
9505 | } | |
9506 | ||
d987fc7f MG |
9507 | static inline int on_null_domain(struct rq *rq) |
9508 | { | |
9509 | return unlikely(!rcu_dereference_sched(rq->sd)); | |
9510 | } | |
9511 | ||
3451d024 | 9512 | #ifdef CONFIG_NO_HZ_COMMON |
83cd4fe2 VP |
9513 | /* |
9514 | * idle load balancing details | |
83cd4fe2 VP |
9515 | * - When one of the busy CPUs notice that there may be an idle rebalancing |
9516 | * needed, they will kick the idle load balancer, which then does idle | |
9517 | * load balancing for all the idle CPUs. | |
9518 | */ | |
1e3c88bd | 9519 | |
3dd0337d | 9520 | static inline int find_new_ilb(void) |
1e3c88bd | 9521 | { |
0b005cf5 | 9522 | int ilb = cpumask_first(nohz.idle_cpus_mask); |
1e3c88bd | 9523 | |
786d6dc7 SS |
9524 | if (ilb < nr_cpu_ids && idle_cpu(ilb)) |
9525 | return ilb; | |
9526 | ||
9527 | return nr_cpu_ids; | |
1e3c88bd | 9528 | } |
1e3c88bd | 9529 | |
83cd4fe2 VP |
9530 | /* |
9531 | * Kick a CPU to do the nohz balancing, if it is time for it. We pick the | |
9532 | * nohz_load_balancer CPU (if there is one) otherwise fallback to any idle | |
9533 | * CPU (if there is one). | |
9534 | */ | |
a4064fb6 | 9535 | static void kick_ilb(unsigned int flags) |
83cd4fe2 VP |
9536 | { |
9537 | int ilb_cpu; | |
9538 | ||
9539 | nohz.next_balance++; | |
9540 | ||
3dd0337d | 9541 | ilb_cpu = find_new_ilb(); |
83cd4fe2 | 9542 | |
0b005cf5 SS |
9543 | if (ilb_cpu >= nr_cpu_ids) |
9544 | return; | |
83cd4fe2 | 9545 | |
a4064fb6 | 9546 | flags = atomic_fetch_or(flags, nohz_flags(ilb_cpu)); |
b7031a02 | 9547 | if (flags & NOHZ_KICK_MASK) |
1c792db7 | 9548 | return; |
4550487a | 9549 | |
1c792db7 SS |
9550 | /* |
9551 | * Use smp_send_reschedule() instead of resched_cpu(). | |
97fb7a0a | 9552 | * This way we generate a sched IPI on the target CPU which |
1c792db7 SS |
9553 | * is idle. And the softirq performing nohz idle load balance |
9554 | * will be run before returning from the IPI. | |
9555 | */ | |
9556 | smp_send_reschedule(ilb_cpu); | |
4550487a PZ |
9557 | } |
9558 | ||
9559 | /* | |
9560 | * Current heuristic for kicking the idle load balancer in the presence | |
9561 | * of an idle cpu in the system. | |
9562 | * - This rq has more than one task. | |
9563 | * - This rq has at least one CFS task and the capacity of the CPU is | |
9564 | * significantly reduced because of RT tasks or IRQs. | |
9565 | * - At parent of LLC scheduler domain level, this cpu's scheduler group has | |
9566 | * multiple busy cpu. | |
9567 | * - For SD_ASYM_PACKING, if the lower numbered cpu's in the scheduler | |
9568 | * domain span are idle. | |
9569 | */ | |
9570 | static void nohz_balancer_kick(struct rq *rq) | |
9571 | { | |
9572 | unsigned long now = jiffies; | |
9573 | struct sched_domain_shared *sds; | |
9574 | struct sched_domain *sd; | |
9575 | int nr_busy, i, cpu = rq->cpu; | |
a4064fb6 | 9576 | unsigned int flags = 0; |
4550487a PZ |
9577 | |
9578 | if (unlikely(rq->idle_balance)) | |
9579 | return; | |
9580 | ||
9581 | /* | |
9582 | * We may be recently in ticked or tickless idle mode. At the first | |
9583 | * busy tick after returning from idle, we will update the busy stats. | |
9584 | */ | |
00357f5e | 9585 | nohz_balance_exit_idle(rq); |
4550487a PZ |
9586 | |
9587 | /* | |
9588 | * None are in tickless mode and hence no need for NOHZ idle load | |
9589 | * balancing. | |
9590 | */ | |
9591 | if (likely(!atomic_read(&nohz.nr_cpus))) | |
9592 | return; | |
9593 | ||
f643ea22 VG |
9594 | if (READ_ONCE(nohz.has_blocked) && |
9595 | time_after(now, READ_ONCE(nohz.next_blocked))) | |
a4064fb6 PZ |
9596 | flags = NOHZ_STATS_KICK; |
9597 | ||
4550487a | 9598 | if (time_before(now, nohz.next_balance)) |
a4064fb6 | 9599 | goto out; |
4550487a | 9600 | |
5fbdfae5 | 9601 | if (rq->nr_running >= 2 || rq->misfit_task_load) { |
a4064fb6 | 9602 | flags = NOHZ_KICK_MASK; |
4550487a PZ |
9603 | goto out; |
9604 | } | |
9605 | ||
9606 | rcu_read_lock(); | |
9607 | sds = rcu_dereference(per_cpu(sd_llc_shared, cpu)); | |
9608 | if (sds) { | |
9609 | /* | |
9610 | * XXX: write a coherent comment on why we do this. | |
9611 | * See also: http://lkml.kernel.org/r/20111202010832.602203411@sbsiddha-desk.sc.intel.com | |
9612 | */ | |
9613 | nr_busy = atomic_read(&sds->nr_busy_cpus); | |
9614 | if (nr_busy > 1) { | |
a4064fb6 | 9615 | flags = NOHZ_KICK_MASK; |
4550487a PZ |
9616 | goto unlock; |
9617 | } | |
9618 | ||
9619 | } | |
9620 | ||
9621 | sd = rcu_dereference(rq->sd); | |
9622 | if (sd) { | |
9623 | if ((rq->cfs.h_nr_running >= 1) && | |
9624 | check_cpu_capacity(rq, sd)) { | |
a4064fb6 | 9625 | flags = NOHZ_KICK_MASK; |
4550487a PZ |
9626 | goto unlock; |
9627 | } | |
9628 | } | |
9629 | ||
011b27bb | 9630 | sd = rcu_dereference(per_cpu(sd_asym_packing, cpu)); |
4550487a PZ |
9631 | if (sd) { |
9632 | for_each_cpu(i, sched_domain_span(sd)) { | |
9633 | if (i == cpu || | |
9634 | !cpumask_test_cpu(i, nohz.idle_cpus_mask)) | |
9635 | continue; | |
9636 | ||
9637 | if (sched_asym_prefer(i, cpu)) { | |
a4064fb6 | 9638 | flags = NOHZ_KICK_MASK; |
4550487a PZ |
9639 | goto unlock; |
9640 | } | |
9641 | } | |
9642 | } | |
9643 | unlock: | |
9644 | rcu_read_unlock(); | |
9645 | out: | |
a4064fb6 PZ |
9646 | if (flags) |
9647 | kick_ilb(flags); | |
83cd4fe2 VP |
9648 | } |
9649 | ||
00357f5e | 9650 | static void set_cpu_sd_state_busy(int cpu) |
71325960 | 9651 | { |
00357f5e | 9652 | struct sched_domain *sd; |
a22e47a4 | 9653 | |
00357f5e PZ |
9654 | rcu_read_lock(); |
9655 | sd = rcu_dereference(per_cpu(sd_llc, cpu)); | |
a22e47a4 | 9656 | |
00357f5e PZ |
9657 | if (!sd || !sd->nohz_idle) |
9658 | goto unlock; | |
9659 | sd->nohz_idle = 0; | |
9660 | ||
9661 | atomic_inc(&sd->shared->nr_busy_cpus); | |
9662 | unlock: | |
9663 | rcu_read_unlock(); | |
71325960 SS |
9664 | } |
9665 | ||
00357f5e PZ |
9666 | void nohz_balance_exit_idle(struct rq *rq) |
9667 | { | |
9668 | SCHED_WARN_ON(rq != this_rq()); | |
9669 | ||
9670 | if (likely(!rq->nohz_tick_stopped)) | |
9671 | return; | |
9672 | ||
9673 | rq->nohz_tick_stopped = 0; | |
9674 | cpumask_clear_cpu(rq->cpu, nohz.idle_cpus_mask); | |
9675 | atomic_dec(&nohz.nr_cpus); | |
9676 | ||
9677 | set_cpu_sd_state_busy(rq->cpu); | |
9678 | } | |
9679 | ||
9680 | static void set_cpu_sd_state_idle(int cpu) | |
69e1e811 SS |
9681 | { |
9682 | struct sched_domain *sd; | |
69e1e811 | 9683 | |
69e1e811 | 9684 | rcu_read_lock(); |
0e369d75 | 9685 | sd = rcu_dereference(per_cpu(sd_llc, cpu)); |
25f55d9d VG |
9686 | |
9687 | if (!sd || sd->nohz_idle) | |
9688 | goto unlock; | |
9689 | sd->nohz_idle = 1; | |
9690 | ||
0e369d75 | 9691 | atomic_dec(&sd->shared->nr_busy_cpus); |
25f55d9d | 9692 | unlock: |
69e1e811 SS |
9693 | rcu_read_unlock(); |
9694 | } | |
9695 | ||
1e3c88bd | 9696 | /* |
97fb7a0a | 9697 | * This routine will record that the CPU is going idle with tick stopped. |
0b005cf5 | 9698 | * This info will be used in performing idle load balancing in the future. |
1e3c88bd | 9699 | */ |
c1cc017c | 9700 | void nohz_balance_enter_idle(int cpu) |
1e3c88bd | 9701 | { |
00357f5e PZ |
9702 | struct rq *rq = cpu_rq(cpu); |
9703 | ||
9704 | SCHED_WARN_ON(cpu != smp_processor_id()); | |
9705 | ||
97fb7a0a | 9706 | /* If this CPU is going down, then nothing needs to be done: */ |
71325960 SS |
9707 | if (!cpu_active(cpu)) |
9708 | return; | |
9709 | ||
387bc8b5 | 9710 | /* Spare idle load balancing on CPUs that don't want to be disturbed: */ |
de201559 | 9711 | if (!housekeeping_cpu(cpu, HK_FLAG_SCHED)) |
387bc8b5 FW |
9712 | return; |
9713 | ||
f643ea22 VG |
9714 | /* |
9715 | * Can be set safely without rq->lock held | |
9716 | * If a clear happens, it will have evaluated last additions because | |
9717 | * rq->lock is held during the check and the clear | |
9718 | */ | |
9719 | rq->has_blocked_load = 1; | |
9720 | ||
9721 | /* | |
9722 | * The tick is still stopped but load could have been added in the | |
9723 | * meantime. We set the nohz.has_blocked flag to trig a check of the | |
9724 | * *_avg. The CPU is already part of nohz.idle_cpus_mask so the clear | |
9725 | * of nohz.has_blocked can only happen after checking the new load | |
9726 | */ | |
00357f5e | 9727 | if (rq->nohz_tick_stopped) |
f643ea22 | 9728 | goto out; |
1e3c88bd | 9729 | |
97fb7a0a | 9730 | /* If we're a completely isolated CPU, we don't play: */ |
00357f5e | 9731 | if (on_null_domain(rq)) |
d987fc7f MG |
9732 | return; |
9733 | ||
00357f5e PZ |
9734 | rq->nohz_tick_stopped = 1; |
9735 | ||
c1cc017c AS |
9736 | cpumask_set_cpu(cpu, nohz.idle_cpus_mask); |
9737 | atomic_inc(&nohz.nr_cpus); | |
00357f5e | 9738 | |
f643ea22 VG |
9739 | /* |
9740 | * Ensures that if nohz_idle_balance() fails to observe our | |
9741 | * @idle_cpus_mask store, it must observe the @has_blocked | |
9742 | * store. | |
9743 | */ | |
9744 | smp_mb__after_atomic(); | |
9745 | ||
00357f5e | 9746 | set_cpu_sd_state_idle(cpu); |
f643ea22 VG |
9747 | |
9748 | out: | |
9749 | /* | |
9750 | * Each time a cpu enter idle, we assume that it has blocked load and | |
9751 | * enable the periodic update of the load of idle cpus | |
9752 | */ | |
9753 | WRITE_ONCE(nohz.has_blocked, 1); | |
1e3c88bd | 9754 | } |
1e3c88bd | 9755 | |
1e3c88bd | 9756 | /* |
31e77c93 VG |
9757 | * Internal function that runs load balance for all idle cpus. The load balance |
9758 | * can be a simple update of blocked load or a complete load balance with | |
9759 | * tasks movement depending of flags. | |
9760 | * The function returns false if the loop has stopped before running | |
9761 | * through all idle CPUs. | |
1e3c88bd | 9762 | */ |
31e77c93 VG |
9763 | static bool _nohz_idle_balance(struct rq *this_rq, unsigned int flags, |
9764 | enum cpu_idle_type idle) | |
83cd4fe2 | 9765 | { |
c5afb6a8 | 9766 | /* Earliest time when we have to do rebalance again */ |
a4064fb6 PZ |
9767 | unsigned long now = jiffies; |
9768 | unsigned long next_balance = now + 60*HZ; | |
f643ea22 | 9769 | bool has_blocked_load = false; |
c5afb6a8 | 9770 | int update_next_balance = 0; |
b7031a02 | 9771 | int this_cpu = this_rq->cpu; |
b7031a02 | 9772 | int balance_cpu; |
31e77c93 | 9773 | int ret = false; |
b7031a02 | 9774 | struct rq *rq; |
83cd4fe2 | 9775 | |
b7031a02 | 9776 | SCHED_WARN_ON((flags & NOHZ_KICK_MASK) == NOHZ_BALANCE_KICK); |
83cd4fe2 | 9777 | |
f643ea22 VG |
9778 | /* |
9779 | * We assume there will be no idle load after this update and clear | |
9780 | * the has_blocked flag. If a cpu enters idle in the mean time, it will | |
9781 | * set the has_blocked flag and trig another update of idle load. | |
9782 | * Because a cpu that becomes idle, is added to idle_cpus_mask before | |
9783 | * setting the flag, we are sure to not clear the state and not | |
9784 | * check the load of an idle cpu. | |
9785 | */ | |
9786 | WRITE_ONCE(nohz.has_blocked, 0); | |
9787 | ||
9788 | /* | |
9789 | * Ensures that if we miss the CPU, we must see the has_blocked | |
9790 | * store from nohz_balance_enter_idle(). | |
9791 | */ | |
9792 | smp_mb(); | |
9793 | ||
83cd4fe2 | 9794 | for_each_cpu(balance_cpu, nohz.idle_cpus_mask) { |
8a6d42d1 | 9795 | if (balance_cpu == this_cpu || !idle_cpu(balance_cpu)) |
83cd4fe2 VP |
9796 | continue; |
9797 | ||
9798 | /* | |
97fb7a0a IM |
9799 | * If this CPU gets work to do, stop the load balancing |
9800 | * work being done for other CPUs. Next load | |
83cd4fe2 VP |
9801 | * balancing owner will pick it up. |
9802 | */ | |
f643ea22 VG |
9803 | if (need_resched()) { |
9804 | has_blocked_load = true; | |
9805 | goto abort; | |
9806 | } | |
83cd4fe2 | 9807 | |
5ed4f1d9 VG |
9808 | rq = cpu_rq(balance_cpu); |
9809 | ||
63928384 | 9810 | has_blocked_load |= update_nohz_stats(rq, true); |
f643ea22 | 9811 | |
ed61bbc6 TC |
9812 | /* |
9813 | * If time for next balance is due, | |
9814 | * do the balance. | |
9815 | */ | |
9816 | if (time_after_eq(jiffies, rq->next_balance)) { | |
8a8c69c3 PZ |
9817 | struct rq_flags rf; |
9818 | ||
31e77c93 | 9819 | rq_lock_irqsave(rq, &rf); |
ed61bbc6 | 9820 | update_rq_clock(rq); |
cee1afce | 9821 | cpu_load_update_idle(rq); |
31e77c93 | 9822 | rq_unlock_irqrestore(rq, &rf); |
8a8c69c3 | 9823 | |
b7031a02 PZ |
9824 | if (flags & NOHZ_BALANCE_KICK) |
9825 | rebalance_domains(rq, CPU_IDLE); | |
ed61bbc6 | 9826 | } |
83cd4fe2 | 9827 | |
c5afb6a8 VG |
9828 | if (time_after(next_balance, rq->next_balance)) { |
9829 | next_balance = rq->next_balance; | |
9830 | update_next_balance = 1; | |
9831 | } | |
83cd4fe2 | 9832 | } |
c5afb6a8 | 9833 | |
31e77c93 VG |
9834 | /* Newly idle CPU doesn't need an update */ |
9835 | if (idle != CPU_NEWLY_IDLE) { | |
9836 | update_blocked_averages(this_cpu); | |
9837 | has_blocked_load |= this_rq->has_blocked_load; | |
9838 | } | |
9839 | ||
b7031a02 PZ |
9840 | if (flags & NOHZ_BALANCE_KICK) |
9841 | rebalance_domains(this_rq, CPU_IDLE); | |
9842 | ||
f643ea22 VG |
9843 | WRITE_ONCE(nohz.next_blocked, |
9844 | now + msecs_to_jiffies(LOAD_AVG_PERIOD)); | |
9845 | ||
31e77c93 VG |
9846 | /* The full idle balance loop has been done */ |
9847 | ret = true; | |
9848 | ||
f643ea22 VG |
9849 | abort: |
9850 | /* There is still blocked load, enable periodic update */ | |
9851 | if (has_blocked_load) | |
9852 | WRITE_ONCE(nohz.has_blocked, 1); | |
a4064fb6 | 9853 | |
c5afb6a8 VG |
9854 | /* |
9855 | * next_balance will be updated only when there is a need. | |
9856 | * When the CPU is attached to null domain for ex, it will not be | |
9857 | * updated. | |
9858 | */ | |
9859 | if (likely(update_next_balance)) | |
9860 | nohz.next_balance = next_balance; | |
b7031a02 | 9861 | |
31e77c93 VG |
9862 | return ret; |
9863 | } | |
9864 | ||
9865 | /* | |
9866 | * In CONFIG_NO_HZ_COMMON case, the idle balance kickee will do the | |
9867 | * rebalancing for all the cpus for whom scheduler ticks are stopped. | |
9868 | */ | |
9869 | static bool nohz_idle_balance(struct rq *this_rq, enum cpu_idle_type idle) | |
9870 | { | |
9871 | int this_cpu = this_rq->cpu; | |
9872 | unsigned int flags; | |
9873 | ||
9874 | if (!(atomic_read(nohz_flags(this_cpu)) & NOHZ_KICK_MASK)) | |
9875 | return false; | |
9876 | ||
9877 | if (idle != CPU_IDLE) { | |
9878 | atomic_andnot(NOHZ_KICK_MASK, nohz_flags(this_cpu)); | |
9879 | return false; | |
9880 | } | |
9881 | ||
80eb8657 | 9882 | /* could be _relaxed() */ |
31e77c93 VG |
9883 | flags = atomic_fetch_andnot(NOHZ_KICK_MASK, nohz_flags(this_cpu)); |
9884 | if (!(flags & NOHZ_KICK_MASK)) | |
9885 | return false; | |
9886 | ||
9887 | _nohz_idle_balance(this_rq, flags, idle); | |
9888 | ||
b7031a02 | 9889 | return true; |
83cd4fe2 | 9890 | } |
31e77c93 VG |
9891 | |
9892 | static void nohz_newidle_balance(struct rq *this_rq) | |
9893 | { | |
9894 | int this_cpu = this_rq->cpu; | |
9895 | ||
9896 | /* | |
9897 | * This CPU doesn't want to be disturbed by scheduler | |
9898 | * housekeeping | |
9899 | */ | |
9900 | if (!housekeeping_cpu(this_cpu, HK_FLAG_SCHED)) | |
9901 | return; | |
9902 | ||
9903 | /* Will wake up very soon. No time for doing anything else*/ | |
9904 | if (this_rq->avg_idle < sysctl_sched_migration_cost) | |
9905 | return; | |
9906 | ||
9907 | /* Don't need to update blocked load of idle CPUs*/ | |
9908 | if (!READ_ONCE(nohz.has_blocked) || | |
9909 | time_before(jiffies, READ_ONCE(nohz.next_blocked))) | |
9910 | return; | |
9911 | ||
9912 | raw_spin_unlock(&this_rq->lock); | |
9913 | /* | |
9914 | * This CPU is going to be idle and blocked load of idle CPUs | |
9915 | * need to be updated. Run the ilb locally as it is a good | |
9916 | * candidate for ilb instead of waking up another idle CPU. | |
9917 | * Kick an normal ilb if we failed to do the update. | |
9918 | */ | |
9919 | if (!_nohz_idle_balance(this_rq, NOHZ_STATS_KICK, CPU_NEWLY_IDLE)) | |
9920 | kick_ilb(NOHZ_STATS_KICK); | |
9921 | raw_spin_lock(&this_rq->lock); | |
9922 | } | |
9923 | ||
dd707247 PZ |
9924 | #else /* !CONFIG_NO_HZ_COMMON */ |
9925 | static inline void nohz_balancer_kick(struct rq *rq) { } | |
9926 | ||
31e77c93 | 9927 | static inline bool nohz_idle_balance(struct rq *this_rq, enum cpu_idle_type idle) |
b7031a02 PZ |
9928 | { |
9929 | return false; | |
9930 | } | |
31e77c93 VG |
9931 | |
9932 | static inline void nohz_newidle_balance(struct rq *this_rq) { } | |
dd707247 | 9933 | #endif /* CONFIG_NO_HZ_COMMON */ |
83cd4fe2 | 9934 | |
47ea5412 PZ |
9935 | /* |
9936 | * idle_balance is called by schedule() if this_cpu is about to become | |
9937 | * idle. Attempts to pull tasks from other CPUs. | |
9938 | */ | |
9939 | static int idle_balance(struct rq *this_rq, struct rq_flags *rf) | |
9940 | { | |
9941 | unsigned long next_balance = jiffies + HZ; | |
9942 | int this_cpu = this_rq->cpu; | |
9943 | struct sched_domain *sd; | |
9944 | int pulled_task = 0; | |
9945 | u64 curr_cost = 0; | |
9946 | ||
9947 | /* | |
9948 | * We must set idle_stamp _before_ calling idle_balance(), such that we | |
9949 | * measure the duration of idle_balance() as idle time. | |
9950 | */ | |
9951 | this_rq->idle_stamp = rq_clock(this_rq); | |
9952 | ||
9953 | /* | |
9954 | * Do not pull tasks towards !active CPUs... | |
9955 | */ | |
9956 | if (!cpu_active(this_cpu)) | |
9957 | return 0; | |
9958 | ||
9959 | /* | |
9960 | * This is OK, because current is on_cpu, which avoids it being picked | |
9961 | * for load-balance and preemption/IRQs are still disabled avoiding | |
9962 | * further scheduler activity on it and we're being very careful to | |
9963 | * re-start the picking loop. | |
9964 | */ | |
9965 | rq_unpin_lock(this_rq, rf); | |
9966 | ||
9967 | if (this_rq->avg_idle < sysctl_sched_migration_cost || | |
e90c8fe1 | 9968 | !READ_ONCE(this_rq->rd->overload)) { |
31e77c93 | 9969 | |
47ea5412 PZ |
9970 | rcu_read_lock(); |
9971 | sd = rcu_dereference_check_sched_domain(this_rq->sd); | |
9972 | if (sd) | |
9973 | update_next_balance(sd, &next_balance); | |
9974 | rcu_read_unlock(); | |
9975 | ||
31e77c93 VG |
9976 | nohz_newidle_balance(this_rq); |
9977 | ||
47ea5412 PZ |
9978 | goto out; |
9979 | } | |
9980 | ||
9981 | raw_spin_unlock(&this_rq->lock); | |
9982 | ||
9983 | update_blocked_averages(this_cpu); | |
9984 | rcu_read_lock(); | |
9985 | for_each_domain(this_cpu, sd) { | |
9986 | int continue_balancing = 1; | |
9987 | u64 t0, domain_cost; | |
9988 | ||
9989 | if (!(sd->flags & SD_LOAD_BALANCE)) | |
9990 | continue; | |
9991 | ||
9992 | if (this_rq->avg_idle < curr_cost + sd->max_newidle_lb_cost) { | |
9993 | update_next_balance(sd, &next_balance); | |
9994 | break; | |
9995 | } | |
9996 | ||
9997 | if (sd->flags & SD_BALANCE_NEWIDLE) { | |
9998 | t0 = sched_clock_cpu(this_cpu); | |
9999 | ||
10000 | pulled_task = load_balance(this_cpu, this_rq, | |
10001 | sd, CPU_NEWLY_IDLE, | |
10002 | &continue_balancing); | |
10003 | ||
10004 | domain_cost = sched_clock_cpu(this_cpu) - t0; | |
10005 | if (domain_cost > sd->max_newidle_lb_cost) | |
10006 | sd->max_newidle_lb_cost = domain_cost; | |
10007 | ||
10008 | curr_cost += domain_cost; | |
10009 | } | |
10010 | ||
10011 | update_next_balance(sd, &next_balance); | |
10012 | ||
10013 | /* | |
10014 | * Stop searching for tasks to pull if there are | |
10015 | * now runnable tasks on this rq. | |
10016 | */ | |
10017 | if (pulled_task || this_rq->nr_running > 0) | |
10018 | break; | |
10019 | } | |
10020 | rcu_read_unlock(); | |
10021 | ||
10022 | raw_spin_lock(&this_rq->lock); | |
10023 | ||
10024 | if (curr_cost > this_rq->max_idle_balance_cost) | |
10025 | this_rq->max_idle_balance_cost = curr_cost; | |
10026 | ||
457be908 | 10027 | out: |
47ea5412 PZ |
10028 | /* |
10029 | * While browsing the domains, we released the rq lock, a task could | |
10030 | * have been enqueued in the meantime. Since we're not going idle, | |
10031 | * pretend we pulled a task. | |
10032 | */ | |
10033 | if (this_rq->cfs.h_nr_running && !pulled_task) | |
10034 | pulled_task = 1; | |
10035 | ||
47ea5412 PZ |
10036 | /* Move the next balance forward */ |
10037 | if (time_after(this_rq->next_balance, next_balance)) | |
10038 | this_rq->next_balance = next_balance; | |
10039 | ||
10040 | /* Is there a task of a high priority class? */ | |
10041 | if (this_rq->nr_running != this_rq->cfs.h_nr_running) | |
10042 | pulled_task = -1; | |
10043 | ||
10044 | if (pulled_task) | |
10045 | this_rq->idle_stamp = 0; | |
10046 | ||
10047 | rq_repin_lock(this_rq, rf); | |
10048 | ||
10049 | return pulled_task; | |
10050 | } | |
10051 | ||
83cd4fe2 VP |
10052 | /* |
10053 | * run_rebalance_domains is triggered when needed from the scheduler tick. | |
10054 | * Also triggered for nohz idle balancing (with nohz_balancing_kick set). | |
10055 | */ | |
0766f788 | 10056 | static __latent_entropy void run_rebalance_domains(struct softirq_action *h) |
1e3c88bd | 10057 | { |
208cb16b | 10058 | struct rq *this_rq = this_rq(); |
6eb57e0d | 10059 | enum cpu_idle_type idle = this_rq->idle_balance ? |
1e3c88bd PZ |
10060 | CPU_IDLE : CPU_NOT_IDLE; |
10061 | ||
1e3c88bd | 10062 | /* |
97fb7a0a IM |
10063 | * If this CPU has a pending nohz_balance_kick, then do the |
10064 | * balancing on behalf of the other idle CPUs whose ticks are | |
d4573c3e | 10065 | * stopped. Do nohz_idle_balance *before* rebalance_domains to |
97fb7a0a | 10066 | * give the idle CPUs a chance to load balance. Else we may |
d4573c3e PM |
10067 | * load balance only within the local sched_domain hierarchy |
10068 | * and abort nohz_idle_balance altogether if we pull some load. | |
1e3c88bd | 10069 | */ |
b7031a02 PZ |
10070 | if (nohz_idle_balance(this_rq, idle)) |
10071 | return; | |
10072 | ||
10073 | /* normal load balance */ | |
10074 | update_blocked_averages(this_rq->cpu); | |
d4573c3e | 10075 | rebalance_domains(this_rq, idle); |
1e3c88bd PZ |
10076 | } |
10077 | ||
1e3c88bd PZ |
10078 | /* |
10079 | * Trigger the SCHED_SOFTIRQ if it is time to do periodic load balancing. | |
1e3c88bd | 10080 | */ |
7caff66f | 10081 | void trigger_load_balance(struct rq *rq) |
1e3c88bd | 10082 | { |
1e3c88bd | 10083 | /* Don't need to rebalance while attached to NULL domain */ |
c726099e DL |
10084 | if (unlikely(on_null_domain(rq))) |
10085 | return; | |
10086 | ||
10087 | if (time_after_eq(jiffies, rq->next_balance)) | |
1e3c88bd | 10088 | raise_softirq(SCHED_SOFTIRQ); |
4550487a PZ |
10089 | |
10090 | nohz_balancer_kick(rq); | |
1e3c88bd PZ |
10091 | } |
10092 | ||
0bcdcf28 CE |
10093 | static void rq_online_fair(struct rq *rq) |
10094 | { | |
10095 | update_sysctl(); | |
0e59bdae KT |
10096 | |
10097 | update_runtime_enabled(rq); | |
0bcdcf28 CE |
10098 | } |
10099 | ||
10100 | static void rq_offline_fair(struct rq *rq) | |
10101 | { | |
10102 | update_sysctl(); | |
a4c96ae3 PB |
10103 | |
10104 | /* Ensure any throttled groups are reachable by pick_next_task */ | |
10105 | unthrottle_offline_cfs_rqs(rq); | |
0bcdcf28 CE |
10106 | } |
10107 | ||
55e12e5e | 10108 | #endif /* CONFIG_SMP */ |
e1d1484f | 10109 | |
bf0f6f24 | 10110 | /* |
d84b3131 FW |
10111 | * scheduler tick hitting a task of our scheduling class. |
10112 | * | |
10113 | * NOTE: This function can be called remotely by the tick offload that | |
10114 | * goes along full dynticks. Therefore no local assumption can be made | |
10115 | * and everything must be accessed through the @rq and @curr passed in | |
10116 | * parameters. | |
bf0f6f24 | 10117 | */ |
8f4d37ec | 10118 | static void task_tick_fair(struct rq *rq, struct task_struct *curr, int queued) |
bf0f6f24 IM |
10119 | { |
10120 | struct cfs_rq *cfs_rq; | |
10121 | struct sched_entity *se = &curr->se; | |
10122 | ||
10123 | for_each_sched_entity(se) { | |
10124 | cfs_rq = cfs_rq_of(se); | |
8f4d37ec | 10125 | entity_tick(cfs_rq, se, queued); |
bf0f6f24 | 10126 | } |
18bf2805 | 10127 | |
b52da86e | 10128 | if (static_branch_unlikely(&sched_numa_balancing)) |
cbee9f88 | 10129 | task_tick_numa(rq, curr); |
3b1baa64 MR |
10130 | |
10131 | update_misfit_status(curr, rq); | |
2802bf3c | 10132 | update_overutilized_status(task_rq(curr)); |
bf0f6f24 IM |
10133 | } |
10134 | ||
10135 | /* | |
cd29fe6f PZ |
10136 | * called on fork with the child task as argument from the parent's context |
10137 | * - child not yet on the tasklist | |
10138 | * - preemption disabled | |
bf0f6f24 | 10139 | */ |
cd29fe6f | 10140 | static void task_fork_fair(struct task_struct *p) |
bf0f6f24 | 10141 | { |
4fc420c9 DN |
10142 | struct cfs_rq *cfs_rq; |
10143 | struct sched_entity *se = &p->se, *curr; | |
cd29fe6f | 10144 | struct rq *rq = this_rq(); |
8a8c69c3 | 10145 | struct rq_flags rf; |
bf0f6f24 | 10146 | |
8a8c69c3 | 10147 | rq_lock(rq, &rf); |
861d034e PZ |
10148 | update_rq_clock(rq); |
10149 | ||
4fc420c9 DN |
10150 | cfs_rq = task_cfs_rq(current); |
10151 | curr = cfs_rq->curr; | |
e210bffd PZ |
10152 | if (curr) { |
10153 | update_curr(cfs_rq); | |
b5d9d734 | 10154 | se->vruntime = curr->vruntime; |
e210bffd | 10155 | } |
aeb73b04 | 10156 | place_entity(cfs_rq, se, 1); |
4d78e7b6 | 10157 | |
cd29fe6f | 10158 | if (sysctl_sched_child_runs_first && curr && entity_before(curr, se)) { |
87fefa38 | 10159 | /* |
edcb60a3 IM |
10160 | * Upon rescheduling, sched_class::put_prev_task() will place |
10161 | * 'current' within the tree based on its new key value. | |
10162 | */ | |
4d78e7b6 | 10163 | swap(curr->vruntime, se->vruntime); |
8875125e | 10164 | resched_curr(rq); |
4d78e7b6 | 10165 | } |
bf0f6f24 | 10166 | |
88ec22d3 | 10167 | se->vruntime -= cfs_rq->min_vruntime; |
8a8c69c3 | 10168 | rq_unlock(rq, &rf); |
bf0f6f24 IM |
10169 | } |
10170 | ||
cb469845 SR |
10171 | /* |
10172 | * Priority of the task has changed. Check to see if we preempt | |
10173 | * the current task. | |
10174 | */ | |
da7a735e PZ |
10175 | static void |
10176 | prio_changed_fair(struct rq *rq, struct task_struct *p, int oldprio) | |
cb469845 | 10177 | { |
da0c1e65 | 10178 | if (!task_on_rq_queued(p)) |
da7a735e PZ |
10179 | return; |
10180 | ||
cb469845 SR |
10181 | /* |
10182 | * Reschedule if we are currently running on this runqueue and | |
10183 | * our priority decreased, or if we are not currently running on | |
10184 | * this runqueue and our priority is higher than the current's | |
10185 | */ | |
da7a735e | 10186 | if (rq->curr == p) { |
cb469845 | 10187 | if (p->prio > oldprio) |
8875125e | 10188 | resched_curr(rq); |
cb469845 | 10189 | } else |
15afe09b | 10190 | check_preempt_curr(rq, p, 0); |
cb469845 SR |
10191 | } |
10192 | ||
daa59407 | 10193 | static inline bool vruntime_normalized(struct task_struct *p) |
da7a735e PZ |
10194 | { |
10195 | struct sched_entity *se = &p->se; | |
da7a735e PZ |
10196 | |
10197 | /* | |
daa59407 BP |
10198 | * In both the TASK_ON_RQ_QUEUED and TASK_ON_RQ_MIGRATING cases, |
10199 | * the dequeue_entity(.flags=0) will already have normalized the | |
10200 | * vruntime. | |
10201 | */ | |
10202 | if (p->on_rq) | |
10203 | return true; | |
10204 | ||
10205 | /* | |
10206 | * When !on_rq, vruntime of the task has usually NOT been normalized. | |
10207 | * But there are some cases where it has already been normalized: | |
da7a735e | 10208 | * |
daa59407 BP |
10209 | * - A forked child which is waiting for being woken up by |
10210 | * wake_up_new_task(). | |
10211 | * - A task which has been woken up by try_to_wake_up() and | |
10212 | * waiting for actually being woken up by sched_ttwu_pending(). | |
da7a735e | 10213 | */ |
d0cdb3ce SM |
10214 | if (!se->sum_exec_runtime || |
10215 | (p->state == TASK_WAKING && p->sched_remote_wakeup)) | |
daa59407 BP |
10216 | return true; |
10217 | ||
10218 | return false; | |
10219 | } | |
10220 | ||
09a43ace VG |
10221 | #ifdef CONFIG_FAIR_GROUP_SCHED |
10222 | /* | |
10223 | * Propagate the changes of the sched_entity across the tg tree to make it | |
10224 | * visible to the root | |
10225 | */ | |
10226 | static void propagate_entity_cfs_rq(struct sched_entity *se) | |
10227 | { | |
10228 | struct cfs_rq *cfs_rq; | |
10229 | ||
10230 | /* Start to propagate at parent */ | |
10231 | se = se->parent; | |
10232 | ||
10233 | for_each_sched_entity(se) { | |
10234 | cfs_rq = cfs_rq_of(se); | |
10235 | ||
10236 | if (cfs_rq_throttled(cfs_rq)) | |
10237 | break; | |
10238 | ||
88c0616e | 10239 | update_load_avg(cfs_rq, se, UPDATE_TG); |
09a43ace VG |
10240 | } |
10241 | } | |
10242 | #else | |
10243 | static void propagate_entity_cfs_rq(struct sched_entity *se) { } | |
10244 | #endif | |
10245 | ||
df217913 | 10246 | static void detach_entity_cfs_rq(struct sched_entity *se) |
daa59407 | 10247 | { |
daa59407 BP |
10248 | struct cfs_rq *cfs_rq = cfs_rq_of(se); |
10249 | ||
9d89c257 | 10250 | /* Catch up with the cfs_rq and remove our load when we leave */ |
88c0616e | 10251 | update_load_avg(cfs_rq, se, 0); |
a05e8c51 | 10252 | detach_entity_load_avg(cfs_rq, se); |
7c3edd2c | 10253 | update_tg_load_avg(cfs_rq, false); |
09a43ace | 10254 | propagate_entity_cfs_rq(se); |
da7a735e PZ |
10255 | } |
10256 | ||
df217913 | 10257 | static void attach_entity_cfs_rq(struct sched_entity *se) |
cb469845 | 10258 | { |
daa59407 | 10259 | struct cfs_rq *cfs_rq = cfs_rq_of(se); |
7855a35a BP |
10260 | |
10261 | #ifdef CONFIG_FAIR_GROUP_SCHED | |
eb7a59b2 M |
10262 | /* |
10263 | * Since the real-depth could have been changed (only FAIR | |
10264 | * class maintain depth value), reset depth properly. | |
10265 | */ | |
10266 | se->depth = se->parent ? se->parent->depth + 1 : 0; | |
10267 | #endif | |
7855a35a | 10268 | |
df217913 | 10269 | /* Synchronize entity with its cfs_rq */ |
88c0616e | 10270 | update_load_avg(cfs_rq, se, sched_feat(ATTACH_AGE_LOAD) ? 0 : SKIP_AGE_LOAD); |
ea14b57e | 10271 | attach_entity_load_avg(cfs_rq, se, 0); |
7c3edd2c | 10272 | update_tg_load_avg(cfs_rq, false); |
09a43ace | 10273 | propagate_entity_cfs_rq(se); |
df217913 VG |
10274 | } |
10275 | ||
10276 | static void detach_task_cfs_rq(struct task_struct *p) | |
10277 | { | |
10278 | struct sched_entity *se = &p->se; | |
10279 | struct cfs_rq *cfs_rq = cfs_rq_of(se); | |
10280 | ||
10281 | if (!vruntime_normalized(p)) { | |
10282 | /* | |
10283 | * Fix up our vruntime so that the current sleep doesn't | |
10284 | * cause 'unlimited' sleep bonus. | |
10285 | */ | |
10286 | place_entity(cfs_rq, se, 0); | |
10287 | se->vruntime -= cfs_rq->min_vruntime; | |
10288 | } | |
10289 | ||
10290 | detach_entity_cfs_rq(se); | |
10291 | } | |
10292 | ||
10293 | static void attach_task_cfs_rq(struct task_struct *p) | |
10294 | { | |
10295 | struct sched_entity *se = &p->se; | |
10296 | struct cfs_rq *cfs_rq = cfs_rq_of(se); | |
10297 | ||
10298 | attach_entity_cfs_rq(se); | |
daa59407 BP |
10299 | |
10300 | if (!vruntime_normalized(p)) | |
10301 | se->vruntime += cfs_rq->min_vruntime; | |
10302 | } | |
6efdb105 | 10303 | |
daa59407 BP |
10304 | static void switched_from_fair(struct rq *rq, struct task_struct *p) |
10305 | { | |
10306 | detach_task_cfs_rq(p); | |
10307 | } | |
10308 | ||
10309 | static void switched_to_fair(struct rq *rq, struct task_struct *p) | |
10310 | { | |
10311 | attach_task_cfs_rq(p); | |
7855a35a | 10312 | |
daa59407 | 10313 | if (task_on_rq_queued(p)) { |
7855a35a | 10314 | /* |
daa59407 BP |
10315 | * We were most likely switched from sched_rt, so |
10316 | * kick off the schedule if running, otherwise just see | |
10317 | * if we can still preempt the current task. | |
7855a35a | 10318 | */ |
daa59407 BP |
10319 | if (rq->curr == p) |
10320 | resched_curr(rq); | |
10321 | else | |
10322 | check_preempt_curr(rq, p, 0); | |
7855a35a | 10323 | } |
cb469845 SR |
10324 | } |
10325 | ||
83b699ed SV |
10326 | /* Account for a task changing its policy or group. |
10327 | * | |
10328 | * This routine is mostly called to set cfs_rq->curr field when a task | |
10329 | * migrates between groups/classes. | |
10330 | */ | |
10331 | static void set_curr_task_fair(struct rq *rq) | |
10332 | { | |
10333 | struct sched_entity *se = &rq->curr->se; | |
10334 | ||
ec12cb7f PT |
10335 | for_each_sched_entity(se) { |
10336 | struct cfs_rq *cfs_rq = cfs_rq_of(se); | |
10337 | ||
10338 | set_next_entity(cfs_rq, se); | |
10339 | /* ensure bandwidth has been allocated on our new cfs_rq */ | |
10340 | account_cfs_rq_runtime(cfs_rq, 0); | |
10341 | } | |
83b699ed SV |
10342 | } |
10343 | ||
029632fb PZ |
10344 | void init_cfs_rq(struct cfs_rq *cfs_rq) |
10345 | { | |
bfb06889 | 10346 | cfs_rq->tasks_timeline = RB_ROOT_CACHED; |
029632fb PZ |
10347 | cfs_rq->min_vruntime = (u64)(-(1LL << 20)); |
10348 | #ifndef CONFIG_64BIT | |
10349 | cfs_rq->min_vruntime_copy = cfs_rq->min_vruntime; | |
10350 | #endif | |
141965c7 | 10351 | #ifdef CONFIG_SMP |
2a2f5d4e | 10352 | raw_spin_lock_init(&cfs_rq->removed.lock); |
9ee474f5 | 10353 | #endif |
029632fb PZ |
10354 | } |
10355 | ||
810b3817 | 10356 | #ifdef CONFIG_FAIR_GROUP_SCHED |
ea86cb4b VG |
10357 | static void task_set_group_fair(struct task_struct *p) |
10358 | { | |
10359 | struct sched_entity *se = &p->se; | |
10360 | ||
10361 | set_task_rq(p, task_cpu(p)); | |
10362 | se->depth = se->parent ? se->parent->depth + 1 : 0; | |
10363 | } | |
10364 | ||
bc54da21 | 10365 | static void task_move_group_fair(struct task_struct *p) |
810b3817 | 10366 | { |
daa59407 | 10367 | detach_task_cfs_rq(p); |
b2b5ce02 | 10368 | set_task_rq(p, task_cpu(p)); |
6efdb105 BP |
10369 | |
10370 | #ifdef CONFIG_SMP | |
10371 | /* Tell se's cfs_rq has been changed -- migrated */ | |
10372 | p->se.avg.last_update_time = 0; | |
10373 | #endif | |
daa59407 | 10374 | attach_task_cfs_rq(p); |
810b3817 | 10375 | } |
029632fb | 10376 | |
ea86cb4b VG |
10377 | static void task_change_group_fair(struct task_struct *p, int type) |
10378 | { | |
10379 | switch (type) { | |
10380 | case TASK_SET_GROUP: | |
10381 | task_set_group_fair(p); | |
10382 | break; | |
10383 | ||
10384 | case TASK_MOVE_GROUP: | |
10385 | task_move_group_fair(p); | |
10386 | break; | |
10387 | } | |
10388 | } | |
10389 | ||
029632fb PZ |
10390 | void free_fair_sched_group(struct task_group *tg) |
10391 | { | |
10392 | int i; | |
10393 | ||
10394 | destroy_cfs_bandwidth(tg_cfs_bandwidth(tg)); | |
10395 | ||
10396 | for_each_possible_cpu(i) { | |
10397 | if (tg->cfs_rq) | |
10398 | kfree(tg->cfs_rq[i]); | |
6fe1f348 | 10399 | if (tg->se) |
029632fb PZ |
10400 | kfree(tg->se[i]); |
10401 | } | |
10402 | ||
10403 | kfree(tg->cfs_rq); | |
10404 | kfree(tg->se); | |
10405 | } | |
10406 | ||
10407 | int alloc_fair_sched_group(struct task_group *tg, struct task_group *parent) | |
10408 | { | |
029632fb | 10409 | struct sched_entity *se; |
b7fa30c9 | 10410 | struct cfs_rq *cfs_rq; |
029632fb PZ |
10411 | int i; |
10412 | ||
6396bb22 | 10413 | tg->cfs_rq = kcalloc(nr_cpu_ids, sizeof(cfs_rq), GFP_KERNEL); |
029632fb PZ |
10414 | if (!tg->cfs_rq) |
10415 | goto err; | |
6396bb22 | 10416 | tg->se = kcalloc(nr_cpu_ids, sizeof(se), GFP_KERNEL); |
029632fb PZ |
10417 | if (!tg->se) |
10418 | goto err; | |
10419 | ||
10420 | tg->shares = NICE_0_LOAD; | |
10421 | ||
10422 | init_cfs_bandwidth(tg_cfs_bandwidth(tg)); | |
10423 | ||
10424 | for_each_possible_cpu(i) { | |
10425 | cfs_rq = kzalloc_node(sizeof(struct cfs_rq), | |
10426 | GFP_KERNEL, cpu_to_node(i)); | |
10427 | if (!cfs_rq) | |
10428 | goto err; | |
10429 | ||
10430 | se = kzalloc_node(sizeof(struct sched_entity), | |
10431 | GFP_KERNEL, cpu_to_node(i)); | |
10432 | if (!se) | |
10433 | goto err_free_rq; | |
10434 | ||
10435 | init_cfs_rq(cfs_rq); | |
10436 | init_tg_cfs_entry(tg, cfs_rq, se, i, parent->se[i]); | |
540247fb | 10437 | init_entity_runnable_average(se); |
029632fb PZ |
10438 | } |
10439 | ||
10440 | return 1; | |
10441 | ||
10442 | err_free_rq: | |
10443 | kfree(cfs_rq); | |
10444 | err: | |
10445 | return 0; | |
10446 | } | |
10447 | ||
8663e24d PZ |
10448 | void online_fair_sched_group(struct task_group *tg) |
10449 | { | |
10450 | struct sched_entity *se; | |
10451 | struct rq *rq; | |
10452 | int i; | |
10453 | ||
10454 | for_each_possible_cpu(i) { | |
10455 | rq = cpu_rq(i); | |
10456 | se = tg->se[i]; | |
10457 | ||
10458 | raw_spin_lock_irq(&rq->lock); | |
4126bad6 | 10459 | update_rq_clock(rq); |
d0326691 | 10460 | attach_entity_cfs_rq(se); |
55e16d30 | 10461 | sync_throttle(tg, i); |
8663e24d PZ |
10462 | raw_spin_unlock_irq(&rq->lock); |
10463 | } | |
10464 | } | |
10465 | ||
6fe1f348 | 10466 | void unregister_fair_sched_group(struct task_group *tg) |
029632fb | 10467 | { |
029632fb | 10468 | unsigned long flags; |
6fe1f348 PZ |
10469 | struct rq *rq; |
10470 | int cpu; | |
029632fb | 10471 | |
6fe1f348 PZ |
10472 | for_each_possible_cpu(cpu) { |
10473 | if (tg->se[cpu]) | |
10474 | remove_entity_load_avg(tg->se[cpu]); | |
029632fb | 10475 | |
6fe1f348 PZ |
10476 | /* |
10477 | * Only empty task groups can be destroyed; so we can speculatively | |
10478 | * check on_list without danger of it being re-added. | |
10479 | */ | |
10480 | if (!tg->cfs_rq[cpu]->on_list) | |
10481 | continue; | |
10482 | ||
10483 | rq = cpu_rq(cpu); | |
10484 | ||
10485 | raw_spin_lock_irqsave(&rq->lock, flags); | |
10486 | list_del_leaf_cfs_rq(tg->cfs_rq[cpu]); | |
10487 | raw_spin_unlock_irqrestore(&rq->lock, flags); | |
10488 | } | |
029632fb PZ |
10489 | } |
10490 | ||
10491 | void init_tg_cfs_entry(struct task_group *tg, struct cfs_rq *cfs_rq, | |
10492 | struct sched_entity *se, int cpu, | |
10493 | struct sched_entity *parent) | |
10494 | { | |
10495 | struct rq *rq = cpu_rq(cpu); | |
10496 | ||
10497 | cfs_rq->tg = tg; | |
10498 | cfs_rq->rq = rq; | |
029632fb PZ |
10499 | init_cfs_rq_runtime(cfs_rq); |
10500 | ||
10501 | tg->cfs_rq[cpu] = cfs_rq; | |
10502 | tg->se[cpu] = se; | |
10503 | ||
10504 | /* se could be NULL for root_task_group */ | |
10505 | if (!se) | |
10506 | return; | |
10507 | ||
fed14d45 | 10508 | if (!parent) { |
029632fb | 10509 | se->cfs_rq = &rq->cfs; |
fed14d45 PZ |
10510 | se->depth = 0; |
10511 | } else { | |
029632fb | 10512 | se->cfs_rq = parent->my_q; |
fed14d45 PZ |
10513 | se->depth = parent->depth + 1; |
10514 | } | |
029632fb PZ |
10515 | |
10516 | se->my_q = cfs_rq; | |
0ac9b1c2 PT |
10517 | /* guarantee group entities always have weight */ |
10518 | update_load_set(&se->load, NICE_0_LOAD); | |
029632fb PZ |
10519 | se->parent = parent; |
10520 | } | |
10521 | ||
10522 | static DEFINE_MUTEX(shares_mutex); | |
10523 | ||
10524 | int sched_group_set_shares(struct task_group *tg, unsigned long shares) | |
10525 | { | |
10526 | int i; | |
029632fb PZ |
10527 | |
10528 | /* | |
10529 | * We can't change the weight of the root cgroup. | |
10530 | */ | |
10531 | if (!tg->se[0]) | |
10532 | return -EINVAL; | |
10533 | ||
10534 | shares = clamp(shares, scale_load(MIN_SHARES), scale_load(MAX_SHARES)); | |
10535 | ||
10536 | mutex_lock(&shares_mutex); | |
10537 | if (tg->shares == shares) | |
10538 | goto done; | |
10539 | ||
10540 | tg->shares = shares; | |
10541 | for_each_possible_cpu(i) { | |
10542 | struct rq *rq = cpu_rq(i); | |
8a8c69c3 PZ |
10543 | struct sched_entity *se = tg->se[i]; |
10544 | struct rq_flags rf; | |
029632fb | 10545 | |
029632fb | 10546 | /* Propagate contribution to hierarchy */ |
8a8c69c3 | 10547 | rq_lock_irqsave(rq, &rf); |
71b1da46 | 10548 | update_rq_clock(rq); |
89ee048f | 10549 | for_each_sched_entity(se) { |
88c0616e | 10550 | update_load_avg(cfs_rq_of(se), se, UPDATE_TG); |
1ea6c46a | 10551 | update_cfs_group(se); |
89ee048f | 10552 | } |
8a8c69c3 | 10553 | rq_unlock_irqrestore(rq, &rf); |
029632fb PZ |
10554 | } |
10555 | ||
10556 | done: | |
10557 | mutex_unlock(&shares_mutex); | |
10558 | return 0; | |
10559 | } | |
10560 | #else /* CONFIG_FAIR_GROUP_SCHED */ | |
10561 | ||
10562 | void free_fair_sched_group(struct task_group *tg) { } | |
10563 | ||
10564 | int alloc_fair_sched_group(struct task_group *tg, struct task_group *parent) | |
10565 | { | |
10566 | return 1; | |
10567 | } | |
10568 | ||
8663e24d PZ |
10569 | void online_fair_sched_group(struct task_group *tg) { } |
10570 | ||
6fe1f348 | 10571 | void unregister_fair_sched_group(struct task_group *tg) { } |
029632fb PZ |
10572 | |
10573 | #endif /* CONFIG_FAIR_GROUP_SCHED */ | |
10574 | ||
810b3817 | 10575 | |
6d686f45 | 10576 | static unsigned int get_rr_interval_fair(struct rq *rq, struct task_struct *task) |
0d721cea PW |
10577 | { |
10578 | struct sched_entity *se = &task->se; | |
0d721cea PW |
10579 | unsigned int rr_interval = 0; |
10580 | ||
10581 | /* | |
10582 | * Time slice is 0 for SCHED_OTHER tasks that are on an otherwise | |
10583 | * idle runqueue: | |
10584 | */ | |
0d721cea | 10585 | if (rq->cfs.load.weight) |
a59f4e07 | 10586 | rr_interval = NS_TO_JIFFIES(sched_slice(cfs_rq_of(se), se)); |
0d721cea PW |
10587 | |
10588 | return rr_interval; | |
10589 | } | |
10590 | ||
bf0f6f24 IM |
10591 | /* |
10592 | * All the scheduling class methods: | |
10593 | */ | |
029632fb | 10594 | const struct sched_class fair_sched_class = { |
5522d5d5 | 10595 | .next = &idle_sched_class, |
bf0f6f24 IM |
10596 | .enqueue_task = enqueue_task_fair, |
10597 | .dequeue_task = dequeue_task_fair, | |
10598 | .yield_task = yield_task_fair, | |
d95f4122 | 10599 | .yield_to_task = yield_to_task_fair, |
bf0f6f24 | 10600 | |
2e09bf55 | 10601 | .check_preempt_curr = check_preempt_wakeup, |
bf0f6f24 IM |
10602 | |
10603 | .pick_next_task = pick_next_task_fair, | |
10604 | .put_prev_task = put_prev_task_fair, | |
10605 | ||
681f3e68 | 10606 | #ifdef CONFIG_SMP |
4ce72a2c | 10607 | .select_task_rq = select_task_rq_fair, |
0a74bef8 | 10608 | .migrate_task_rq = migrate_task_rq_fair, |
141965c7 | 10609 | |
0bcdcf28 CE |
10610 | .rq_online = rq_online_fair, |
10611 | .rq_offline = rq_offline_fair, | |
88ec22d3 | 10612 | |
12695578 | 10613 | .task_dead = task_dead_fair, |
c5b28038 | 10614 | .set_cpus_allowed = set_cpus_allowed_common, |
681f3e68 | 10615 | #endif |
bf0f6f24 | 10616 | |
83b699ed | 10617 | .set_curr_task = set_curr_task_fair, |
bf0f6f24 | 10618 | .task_tick = task_tick_fair, |
cd29fe6f | 10619 | .task_fork = task_fork_fair, |
cb469845 SR |
10620 | |
10621 | .prio_changed = prio_changed_fair, | |
da7a735e | 10622 | .switched_from = switched_from_fair, |
cb469845 | 10623 | .switched_to = switched_to_fair, |
810b3817 | 10624 | |
0d721cea PW |
10625 | .get_rr_interval = get_rr_interval_fair, |
10626 | ||
6e998916 SG |
10627 | .update_curr = update_curr_fair, |
10628 | ||
810b3817 | 10629 | #ifdef CONFIG_FAIR_GROUP_SCHED |
ea86cb4b | 10630 | .task_change_group = task_change_group_fair, |
810b3817 | 10631 | #endif |
bf0f6f24 IM |
10632 | }; |
10633 | ||
10634 | #ifdef CONFIG_SCHED_DEBUG | |
029632fb | 10635 | void print_cfs_stats(struct seq_file *m, int cpu) |
bf0f6f24 | 10636 | { |
039ae8bc | 10637 | struct cfs_rq *cfs_rq, *pos; |
bf0f6f24 | 10638 | |
5973e5b9 | 10639 | rcu_read_lock(); |
039ae8bc | 10640 | for_each_leaf_cfs_rq_safe(cpu_rq(cpu), cfs_rq, pos) |
5cef9eca | 10641 | print_cfs_rq(m, cpu, cfs_rq); |
5973e5b9 | 10642 | rcu_read_unlock(); |
bf0f6f24 | 10643 | } |
397f2378 SD |
10644 | |
10645 | #ifdef CONFIG_NUMA_BALANCING | |
10646 | void show_numa_stats(struct task_struct *p, struct seq_file *m) | |
10647 | { | |
10648 | int node; | |
10649 | unsigned long tsf = 0, tpf = 0, gsf = 0, gpf = 0; | |
10650 | ||
10651 | for_each_online_node(node) { | |
10652 | if (p->numa_faults) { | |
10653 | tsf = p->numa_faults[task_faults_idx(NUMA_MEM, node, 0)]; | |
10654 | tpf = p->numa_faults[task_faults_idx(NUMA_MEM, node, 1)]; | |
10655 | } | |
10656 | if (p->numa_group) { | |
10657 | gsf = p->numa_group->faults[task_faults_idx(NUMA_MEM, node, 0)], | |
10658 | gpf = p->numa_group->faults[task_faults_idx(NUMA_MEM, node, 1)]; | |
10659 | } | |
10660 | print_numa_stats(m, node, tsf, tpf, gsf, gpf); | |
10661 | } | |
10662 | } | |
10663 | #endif /* CONFIG_NUMA_BALANCING */ | |
10664 | #endif /* CONFIG_SCHED_DEBUG */ | |
029632fb PZ |
10665 | |
10666 | __init void init_sched_fair_class(void) | |
10667 | { | |
10668 | #ifdef CONFIG_SMP | |
10669 | open_softirq(SCHED_SOFTIRQ, run_rebalance_domains); | |
10670 | ||
3451d024 | 10671 | #ifdef CONFIG_NO_HZ_COMMON |
554cecaf | 10672 | nohz.next_balance = jiffies; |
f643ea22 | 10673 | nohz.next_blocked = jiffies; |
029632fb | 10674 | zalloc_cpumask_var(&nohz.idle_cpus_mask, GFP_NOWAIT); |
029632fb PZ |
10675 | #endif |
10676 | #endif /* SMP */ | |
10677 | ||
10678 | } |